blob: 1e937fc9e83309e24dd74136f1230ecd9f8db86f [file] [log] [blame]
Dave Chinner4a8af272013-08-12 20:49:36 +10001/*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
4 * All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19#include "xfs.h"
20#include "xfs_fs.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110021#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Dave Chinner4a8af272013-08-12 20:49:36 +100024#include "xfs_bit.h"
Dave Chinner4a8af272013-08-12 20:49:36 +100025#include "xfs_sb.h"
26#include "xfs_ag.h"
27#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110028#include "xfs_da_format.h"
Dave Chinner4a8af272013-08-12 20:49:36 +100029#include "xfs_da_btree.h"
Dave Chinner4a8af272013-08-12 20:49:36 +100030#include "xfs_inode.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100031#include "xfs_dir2.h"
Dave Chinner4a8af272013-08-12 20:49:36 +100032#include "xfs_dir2_priv.h"
33#include "xfs_error.h"
34#include "xfs_trace.h"
35#include "xfs_bmap.h"
Dave Chinner239880e2013-10-23 10:50:10 +110036#include "xfs_trans.h"
Dave Chinner4a8af272013-08-12 20:49:36 +100037
Dave Chinner0cb97762013-08-12 20:50:09 +100038/*
39 * Directory file type support functions
40 */
41static unsigned char xfs_dir3_filetype_table[] = {
42 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK,
43 DT_FIFO, DT_SOCK, DT_LNK, DT_WHT,
44};
45
46unsigned char
47xfs_dir3_get_dtype(
48 struct xfs_mount *mp,
49 __uint8_t filetype)
50{
51 if (!xfs_sb_version_hasftype(&mp->m_sb))
52 return DT_UNKNOWN;
53
54 if (filetype >= XFS_DIR3_FT_MAX)
55 return DT_UNKNOWN;
56
57 return xfs_dir3_filetype_table[filetype];
58}
59/*
60 * @mode, if set, indicates that the type field needs to be set up.
61 * This uses the transformation from file mode to DT_* as defined in linux/fs.h
62 * for file type specification. This will be propagated into the directory
63 * structure if appropriate for the given operation and filesystem config.
64 */
65const unsigned char xfs_mode_to_ftype[S_IFMT >> S_SHIFT] = {
66 [0] = XFS_DIR3_FT_UNKNOWN,
67 [S_IFREG >> S_SHIFT] = XFS_DIR3_FT_REG_FILE,
68 [S_IFDIR >> S_SHIFT] = XFS_DIR3_FT_DIR,
69 [S_IFCHR >> S_SHIFT] = XFS_DIR3_FT_CHRDEV,
70 [S_IFBLK >> S_SHIFT] = XFS_DIR3_FT_BLKDEV,
71 [S_IFIFO >> S_SHIFT] = XFS_DIR3_FT_FIFO,
72 [S_IFSOCK >> S_SHIFT] = XFS_DIR3_FT_SOCK,
73 [S_IFLNK >> S_SHIFT] = XFS_DIR3_FT_SYMLINK,
74};
75
Dave Chinner4a8af272013-08-12 20:49:36 +100076STATIC int
77xfs_dir2_sf_getdents(
Dave Chinner53f82db2014-06-06 15:20:32 +100078 struct xfs_da_args *args,
Dave Chinner4a8af272013-08-12 20:49:36 +100079 struct dir_context *ctx)
80{
81 int i; /* shortform entry number */
Dave Chinner53f82db2014-06-06 15:20:32 +100082 struct xfs_inode *dp = args->dp; /* incore directory inode */
Dave Chinner4a8af272013-08-12 20:49:36 +100083 xfs_dir2_dataptr_t off; /* current entry's offset */
84 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
85 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
86 xfs_dir2_dataptr_t dot_offset;
87 xfs_dir2_dataptr_t dotdot_offset;
88 xfs_ino_t ino;
Dave Chinner53f82db2014-06-06 15:20:32 +100089 struct xfs_da_geometry *geo = args->geo;
Dave Chinner4a8af272013-08-12 20:49:36 +100090
91 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
92 /*
93 * Give up if the directory is way too short.
94 */
95 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
Dave Chinner53f82db2014-06-06 15:20:32 +100096 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
Dave Chinner24513372014-06-25 14:58:08 +100097 return -EIO;
Dave Chinner4a8af272013-08-12 20:49:36 +100098 }
99
100 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
101 ASSERT(dp->i_df.if_u1.if_data != NULL);
102
103 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
104
105 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
106
107 /*
108 * If the block number in the offset is out of range, we're done.
109 */
Dave Chinner7dda6e82014-06-06 15:11:18 +1000110 if (xfs_dir2_dataptr_to_db(geo, ctx->pos) > geo->datablk)
Dave Chinner4a8af272013-08-12 20:49:36 +1000111 return 0;
112
113 /*
114 * Precalculate offsets for . and .. as we will always need them.
115 *
116 * XXX(hch): the second argument is sometimes 0 and sometimes
Dave Chinner7dda6e82014-06-06 15:11:18 +1000117 * geo->datablk
Dave Chinner4a8af272013-08-12 20:49:36 +1000118 */
Dave Chinner7dda6e82014-06-06 15:11:18 +1000119 dot_offset = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100120 dp->d_ops->data_dot_offset);
Dave Chinner7dda6e82014-06-06 15:11:18 +1000121 dotdot_offset = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100122 dp->d_ops->data_dotdot_offset);
Dave Chinner4a8af272013-08-12 20:49:36 +1000123
124 /*
125 * Put . entry unless we're starting past it.
126 */
127 if (ctx->pos <= dot_offset) {
128 ctx->pos = dot_offset & 0x7fffffff;
129 if (!dir_emit(ctx, ".", 1, dp->i_ino, DT_DIR))
130 return 0;
131 }
132
133 /*
134 * Put .. entry unless we're starting past it.
135 */
136 if (ctx->pos <= dotdot_offset) {
Dave Chinner47401752013-10-29 22:11:47 +1100137 ino = dp->d_ops->sf_get_parent_ino(sfp);
Dave Chinner4a8af272013-08-12 20:49:36 +1000138 ctx->pos = dotdot_offset & 0x7fffffff;
139 if (!dir_emit(ctx, "..", 2, ino, DT_DIR))
140 return 0;
141 }
142
143 /*
144 * Loop while there are more entries and put'ing works.
145 */
146 sfep = xfs_dir2_sf_firstentry(sfp);
147 for (i = 0; i < sfp->count; i++) {
Dave Chinner0cb97762013-08-12 20:50:09 +1000148 __uint8_t filetype;
149
Dave Chinner7dda6e82014-06-06 15:11:18 +1000150 off = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
Dave Chinner4a8af272013-08-12 20:49:36 +1000151 xfs_dir2_sf_get_offset(sfep));
152
153 if (ctx->pos > off) {
Dave Chinner32c54832013-10-29 22:11:46 +1100154 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Dave Chinner4a8af272013-08-12 20:49:36 +1000155 continue;
156 }
157
Dave Chinner47401752013-10-29 22:11:47 +1100158 ino = dp->d_ops->sf_get_ino(sfp, sfep);
159 filetype = dp->d_ops->sf_get_ftype(sfep);
Dave Chinner4a8af272013-08-12 20:49:36 +1000160 ctx->pos = off & 0x7fffffff;
Dave Chinner0cb97762013-08-12 20:50:09 +1000161 if (!dir_emit(ctx, (char *)sfep->name, sfep->namelen, ino,
Dave Chinner53f82db2014-06-06 15:20:32 +1000162 xfs_dir3_get_dtype(dp->i_mount, filetype)))
Dave Chinner4a8af272013-08-12 20:49:36 +1000163 return 0;
Dave Chinner32c54832013-10-29 22:11:46 +1100164 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Dave Chinner4a8af272013-08-12 20:49:36 +1000165 }
166
Dave Chinner7dda6e82014-06-06 15:11:18 +1000167 ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) &
Dave Chinner53f82db2014-06-06 15:20:32 +1000168 0x7fffffff;
Dave Chinner4a8af272013-08-12 20:49:36 +1000169 return 0;
170}
171
172/*
173 * Readdir for block directories.
174 */
175STATIC int
176xfs_dir2_block_getdents(
Dave Chinner53f82db2014-06-06 15:20:32 +1000177 struct xfs_da_args *args,
Dave Chinner4a8af272013-08-12 20:49:36 +1000178 struct dir_context *ctx)
179{
Dave Chinner53f82db2014-06-06 15:20:32 +1000180 struct xfs_inode *dp = args->dp; /* incore directory inode */
Dave Chinner4a8af272013-08-12 20:49:36 +1000181 xfs_dir2_data_hdr_t *hdr; /* block header */
182 struct xfs_buf *bp; /* buffer for block */
183 xfs_dir2_block_tail_t *btp; /* block tail */
184 xfs_dir2_data_entry_t *dep; /* block data entry */
185 xfs_dir2_data_unused_t *dup; /* block unused entry */
186 char *endptr; /* end of the data entries */
187 int error; /* error return value */
Dave Chinner4a8af272013-08-12 20:49:36 +1000188 char *ptr; /* current data entry */
189 int wantoff; /* starting block offset */
190 xfs_off_t cook;
Dave Chinner53f82db2014-06-06 15:20:32 +1000191 struct xfs_da_geometry *geo = args->geo;
Dave Chinner4a8af272013-08-12 20:49:36 +1000192
Dave Chinner4a8af272013-08-12 20:49:36 +1000193 /*
194 * If the block number in the offset is out of range, we're done.
195 */
Dave Chinner7dda6e82014-06-06 15:11:18 +1000196 if (xfs_dir2_dataptr_to_db(geo, ctx->pos) > geo->datablk)
Dave Chinner4a8af272013-08-12 20:49:36 +1000197 return 0;
198
199 error = xfs_dir3_block_read(NULL, dp, &bp);
200 if (error)
201 return error;
202
203 /*
204 * Extract the byte offset we start at from the seek pointer.
205 * We'll skip entries before this.
206 */
Dave Chinner30028032014-06-06 15:08:18 +1000207 wantoff = xfs_dir2_dataptr_to_off(geo, ctx->pos);
Dave Chinner4a8af272013-08-12 20:49:36 +1000208 hdr = bp->b_addr;
209 xfs_dir3_data_check(dp, bp);
210 /*
211 * Set up values for the loop.
212 */
Dave Chinner8f661932014-06-06 15:15:59 +1000213 btp = xfs_dir2_block_tail_p(geo, hdr);
Dave Chinner2ca98772013-10-29 22:11:49 +1100214 ptr = (char *)dp->d_ops->data_entry_p(hdr);
Dave Chinner4a8af272013-08-12 20:49:36 +1000215 endptr = (char *)xfs_dir2_block_leaf_p(btp);
216
217 /*
218 * Loop over the data portion of the block.
219 * Each object is a real entry (dep) or an unused one (dup).
220 */
221 while (ptr < endptr) {
Dave Chinner0cb97762013-08-12 20:50:09 +1000222 __uint8_t filetype;
223
Dave Chinner4a8af272013-08-12 20:49:36 +1000224 dup = (xfs_dir2_data_unused_t *)ptr;
225 /*
226 * Unused, skip it.
227 */
228 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
229 ptr += be16_to_cpu(dup->length);
230 continue;
231 }
232
233 dep = (xfs_dir2_data_entry_t *)ptr;
234
235 /*
236 * Bump pointer for the next iteration.
237 */
Dave Chinner9d23fc82013-10-29 22:11:48 +1100238 ptr += dp->d_ops->data_entsize(dep->namelen);
Dave Chinner4a8af272013-08-12 20:49:36 +1000239 /*
240 * The entry is before the desired starting point, skip it.
241 */
242 if ((char *)dep - (char *)hdr < wantoff)
243 continue;
244
Dave Chinner7dda6e82014-06-06 15:11:18 +1000245 cook = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
Dave Chinner4a8af272013-08-12 20:49:36 +1000246 (char *)dep - (char *)hdr);
247
248 ctx->pos = cook & 0x7fffffff;
Dave Chinner9d23fc82013-10-29 22:11:48 +1100249 filetype = dp->d_ops->data_get_ftype(dep);
Dave Chinner4a8af272013-08-12 20:49:36 +1000250 /*
251 * If it didn't fit, set the final offset to here & return.
252 */
253 if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
Dave Chinner0cb97762013-08-12 20:50:09 +1000254 be64_to_cpu(dep->inumber),
Dave Chinner53f82db2014-06-06 15:20:32 +1000255 xfs_dir3_get_dtype(dp->i_mount, filetype))) {
Dave Chinner4a8af272013-08-12 20:49:36 +1000256 xfs_trans_brelse(NULL, bp);
257 return 0;
258 }
259 }
260
261 /*
262 * Reached the end of the block.
263 * Set the offset to a non-existent block 1 and return.
264 */
Dave Chinner7dda6e82014-06-06 15:11:18 +1000265 ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) &
Dave Chinner53f82db2014-06-06 15:20:32 +1000266 0x7fffffff;
Dave Chinner4a8af272013-08-12 20:49:36 +1000267 xfs_trans_brelse(NULL, bp);
268 return 0;
269}
270
271struct xfs_dir2_leaf_map_info {
272 xfs_extlen_t map_blocks; /* number of fsbs in map */
273 xfs_dablk_t map_off; /* last mapped file offset */
274 int map_size; /* total entries in *map */
275 int map_valid; /* valid entries in *map */
276 int nmap; /* mappings to ask xfs_bmapi */
277 xfs_dir2_db_t curdb; /* db for current block */
278 int ra_current; /* number of read-ahead blks */
279 int ra_index; /* *map index for read-ahead */
280 int ra_offset; /* map entry offset for ra */
281 int ra_want; /* readahead count wanted */
282 struct xfs_bmbt_irec map[]; /* map vector for blocks */
283};
284
285STATIC int
286xfs_dir2_leaf_readbuf(
Dave Chinner53f82db2014-06-06 15:20:32 +1000287 struct xfs_da_args *args,
Dave Chinner4a8af272013-08-12 20:49:36 +1000288 size_t bufsize,
289 struct xfs_dir2_leaf_map_info *mip,
290 xfs_dir2_off_t *curoff,
291 struct xfs_buf **bpp)
292{
Dave Chinner53f82db2014-06-06 15:20:32 +1000293 struct xfs_inode *dp = args->dp;
Dave Chinner4a8af272013-08-12 20:49:36 +1000294 struct xfs_buf *bp = *bpp;
295 struct xfs_bmbt_irec *map = mip->map;
296 struct blk_plug plug;
297 int error = 0;
298 int length;
299 int i;
300 int j;
Dave Chinner53f82db2014-06-06 15:20:32 +1000301 struct xfs_da_geometry *geo = args->geo;
Dave Chinner4a8af272013-08-12 20:49:36 +1000302
303 /*
304 * If we have a buffer, we need to release it and
305 * take it out of the mapping.
306 */
307
308 if (bp) {
309 xfs_trans_brelse(NULL, bp);
310 bp = NULL;
Dave Chinnerd6cf1302014-06-06 15:14:11 +1000311 mip->map_blocks -= geo->fsbcount;
Dave Chinner4a8af272013-08-12 20:49:36 +1000312 /*
313 * Loop to get rid of the extents for the
314 * directory block.
315 */
Dave Chinnerd6cf1302014-06-06 15:14:11 +1000316 for (i = geo->fsbcount; i > 0; ) {
Dave Chinner4a8af272013-08-12 20:49:36 +1000317 j = min_t(int, map->br_blockcount, i);
318 map->br_blockcount -= j;
319 map->br_startblock += j;
320 map->br_startoff += j;
321 /*
322 * If mapping is done, pitch it from
323 * the table.
324 */
325 if (!map->br_blockcount && --mip->map_valid)
326 memmove(&map[0], &map[1],
327 sizeof(map[0]) * mip->map_valid);
328 i -= j;
329 }
330 }
331
332 /*
333 * Recalculate the readahead blocks wanted.
334 */
Dave Chinner53f82db2014-06-06 15:20:32 +1000335 mip->ra_want = howmany(bufsize + geo->blksize, (1 << geo->fsblog)) - 1;
Dave Chinner4a8af272013-08-12 20:49:36 +1000336 ASSERT(mip->ra_want >= 0);
337
338 /*
339 * If we don't have as many as we want, and we haven't
340 * run out of data blocks, get some more mappings.
341 */
342 if (1 + mip->ra_want > mip->map_blocks &&
Dave Chinner30028032014-06-06 15:08:18 +1000343 mip->map_off < xfs_dir2_byte_to_da(geo, XFS_DIR2_LEAF_OFFSET)) {
Dave Chinner4a8af272013-08-12 20:49:36 +1000344 /*
345 * Get more bmaps, fill in after the ones
346 * we already have in the table.
347 */
348 mip->nmap = mip->map_size - mip->map_valid;
349 error = xfs_bmapi_read(dp, mip->map_off,
Dave Chinner30028032014-06-06 15:08:18 +1000350 xfs_dir2_byte_to_da(geo, XFS_DIR2_LEAF_OFFSET) -
Dave Chinner4a8af272013-08-12 20:49:36 +1000351 mip->map_off,
352 &map[mip->map_valid], &mip->nmap, 0);
353
354 /*
355 * Don't know if we should ignore this or try to return an
356 * error. The trouble with returning errors is that readdir
357 * will just stop without actually passing the error through.
358 */
359 if (error)
360 goto out; /* XXX */
361
362 /*
363 * If we got all the mappings we asked for, set the final map
364 * offset based on the last bmap value received. Otherwise,
365 * we've reached the end.
366 */
367 if (mip->nmap == mip->map_size - mip->map_valid) {
368 i = mip->map_valid + mip->nmap - 1;
369 mip->map_off = map[i].br_startoff + map[i].br_blockcount;
370 } else
Dave Chinner30028032014-06-06 15:08:18 +1000371 mip->map_off = xfs_dir2_byte_to_da(geo,
Dave Chinner4a8af272013-08-12 20:49:36 +1000372 XFS_DIR2_LEAF_OFFSET);
373
374 /*
375 * Look for holes in the mapping, and eliminate them. Count up
376 * the valid blocks.
377 */
378 for (i = mip->map_valid; i < mip->map_valid + mip->nmap; ) {
379 if (map[i].br_startblock == HOLESTARTBLOCK) {
380 mip->nmap--;
381 length = mip->map_valid + mip->nmap - i;
382 if (length)
383 memmove(&map[i], &map[i + 1],
384 sizeof(map[i]) * length);
385 } else {
386 mip->map_blocks += map[i].br_blockcount;
387 i++;
388 }
389 }
390 mip->map_valid += mip->nmap;
391 }
392
393 /*
394 * No valid mappings, so no more data blocks.
395 */
396 if (!mip->map_valid) {
Dave Chinner30028032014-06-06 15:08:18 +1000397 *curoff = xfs_dir2_da_to_byte(geo, mip->map_off);
Dave Chinner4a8af272013-08-12 20:49:36 +1000398 goto out;
399 }
400
401 /*
402 * Read the directory block starting at the first mapping.
403 */
Dave Chinner30028032014-06-06 15:08:18 +1000404 mip->curdb = xfs_dir2_da_to_db(geo, map->br_startoff);
Dave Chinner4a8af272013-08-12 20:49:36 +1000405 error = xfs_dir3_data_read(NULL, dp, map->br_startoff,
Dave Chinnerd6cf1302014-06-06 15:14:11 +1000406 map->br_blockcount >= geo->fsbcount ?
Dave Chinner53f82db2014-06-06 15:20:32 +1000407 XFS_FSB_TO_DADDR(dp->i_mount, map->br_startblock) :
408 -1, &bp);
Dave Chinner4a8af272013-08-12 20:49:36 +1000409 /*
410 * Should just skip over the data block instead of giving up.
411 */
412 if (error)
413 goto out; /* XXX */
414
415 /*
416 * Adjust the current amount of read-ahead: we just read a block that
417 * was previously ra.
418 */
419 if (mip->ra_current)
Dave Chinnerd6cf1302014-06-06 15:14:11 +1000420 mip->ra_current -= geo->fsbcount;
Dave Chinner4a8af272013-08-12 20:49:36 +1000421
422 /*
423 * Do we need more readahead?
424 */
425 blk_start_plug(&plug);
426 for (mip->ra_index = mip->ra_offset = i = 0;
427 mip->ra_want > mip->ra_current && i < mip->map_blocks;
Dave Chinnerd6cf1302014-06-06 15:14:11 +1000428 i += geo->fsbcount) {
Dave Chinner4a8af272013-08-12 20:49:36 +1000429 ASSERT(mip->ra_index < mip->map_valid);
430 /*
431 * Read-ahead a contiguous directory block.
432 */
433 if (i > mip->ra_current &&
Dave Chinnerd6cf1302014-06-06 15:14:11 +1000434 map[mip->ra_index].br_blockcount >= geo->fsbcount) {
Eric Sandeen9df2dd02014-04-14 19:01:59 +1000435 xfs_dir3_data_readahead(dp,
Dave Chinner4a8af272013-08-12 20:49:36 +1000436 map[mip->ra_index].br_startoff + mip->ra_offset,
Dave Chinner53f82db2014-06-06 15:20:32 +1000437 XFS_FSB_TO_DADDR(dp->i_mount,
Dave Chinner4a8af272013-08-12 20:49:36 +1000438 map[mip->ra_index].br_startblock +
439 mip->ra_offset));
440 mip->ra_current = i;
441 }
442
443 /*
444 * Read-ahead a non-contiguous directory block. This doesn't
445 * use our mapping, but this is a very rare case.
446 */
447 else if (i > mip->ra_current) {
Eric Sandeen9df2dd02014-04-14 19:01:59 +1000448 xfs_dir3_data_readahead(dp,
Dave Chinner4a8af272013-08-12 20:49:36 +1000449 map[mip->ra_index].br_startoff +
450 mip->ra_offset, -1);
451 mip->ra_current = i;
452 }
453
454 /*
455 * Advance offset through the mapping table.
456 */
Dave Chinnerd6cf1302014-06-06 15:14:11 +1000457 for (j = 0; j < geo->fsbcount; j += length ) {
Dave Chinner4a8af272013-08-12 20:49:36 +1000458 /*
459 * The rest of this extent but not more than a dir
460 * block.
461 */
Dave Chinnerd6cf1302014-06-06 15:14:11 +1000462 length = min_t(int, geo->fsbcount,
Dave Chinner4a8af272013-08-12 20:49:36 +1000463 map[mip->ra_index].br_blockcount -
464 mip->ra_offset);
Dave Chinner4a8af272013-08-12 20:49:36 +1000465 mip->ra_offset += length;
466
467 /*
468 * Advance to the next mapping if this one is used up.
469 */
470 if (mip->ra_offset == map[mip->ra_index].br_blockcount) {
471 mip->ra_offset = 0;
472 mip->ra_index++;
473 }
474 }
475 }
476 blk_finish_plug(&plug);
477
478out:
479 *bpp = bp;
480 return error;
481}
482
483/*
484 * Getdents (readdir) for leaf and node directories.
485 * This reads the data blocks only, so is the same for both forms.
486 */
487STATIC int
488xfs_dir2_leaf_getdents(
Dave Chinner53f82db2014-06-06 15:20:32 +1000489 struct xfs_da_args *args,
Dave Chinner4a8af272013-08-12 20:49:36 +1000490 struct dir_context *ctx,
491 size_t bufsize)
492{
Dave Chinner53f82db2014-06-06 15:20:32 +1000493 struct xfs_inode *dp = args->dp;
Dave Chinner4a8af272013-08-12 20:49:36 +1000494 struct xfs_buf *bp = NULL; /* data block buffer */
495 xfs_dir2_data_hdr_t *hdr; /* data block header */
496 xfs_dir2_data_entry_t *dep; /* data entry */
497 xfs_dir2_data_unused_t *dup; /* unused entry */
498 int error = 0; /* error return value */
499 int length; /* temporary length value */
Dave Chinner4a8af272013-08-12 20:49:36 +1000500 int byteoff; /* offset in current block */
501 xfs_dir2_off_t curoff; /* current overall offset */
502 xfs_dir2_off_t newoff; /* new curoff after new blk */
503 char *ptr = NULL; /* pointer to current data */
504 struct xfs_dir2_leaf_map_info *map_info;
Dave Chinner53f82db2014-06-06 15:20:32 +1000505 struct xfs_da_geometry *geo = args->geo;
Dave Chinner4a8af272013-08-12 20:49:36 +1000506
507 /*
508 * If the offset is at or past the largest allowed value,
509 * give up right away.
510 */
511 if (ctx->pos >= XFS_DIR2_MAX_DATAPTR)
512 return 0;
513
Dave Chinner4a8af272013-08-12 20:49:36 +1000514 /*
515 * Set up to bmap a number of blocks based on the caller's
516 * buffer size, the directory block size, and the filesystem
517 * block size.
518 */
Dave Chinner53f82db2014-06-06 15:20:32 +1000519 length = howmany(bufsize + geo->blksize, (1 << geo->fsblog));
Dave Chinner4a8af272013-08-12 20:49:36 +1000520 map_info = kmem_zalloc(offsetof(struct xfs_dir2_leaf_map_info, map) +
521 (length * sizeof(struct xfs_bmbt_irec)),
522 KM_SLEEP | KM_NOFS);
523 map_info->map_size = length;
524
525 /*
526 * Inside the loop we keep the main offset value as a byte offset
527 * in the directory file.
528 */
Eric Sandeen25994052014-04-14 19:02:30 +1000529 curoff = xfs_dir2_dataptr_to_byte(ctx->pos);
Dave Chinner4a8af272013-08-12 20:49:36 +1000530
531 /*
532 * Force this conversion through db so we truncate the offset
533 * down to get the start of the data block.
534 */
Dave Chinner30028032014-06-06 15:08:18 +1000535 map_info->map_off = xfs_dir2_db_to_da(geo,
536 xfs_dir2_byte_to_db(geo, curoff));
Dave Chinner4a8af272013-08-12 20:49:36 +1000537
538 /*
539 * Loop over directory entries until we reach the end offset.
540 * Get more blocks and readahead as necessary.
541 */
542 while (curoff < XFS_DIR2_LEAF_OFFSET) {
Dave Chinner0cb97762013-08-12 20:50:09 +1000543 __uint8_t filetype;
544
Dave Chinner4a8af272013-08-12 20:49:36 +1000545 /*
546 * If we have no buffer, or we're off the end of the
547 * current buffer, need to get another one.
548 */
Dave Chinner8f661932014-06-06 15:15:59 +1000549 if (!bp || ptr >= (char *)bp->b_addr + geo->blksize) {
Dave Chinner4a8af272013-08-12 20:49:36 +1000550
Dave Chinner53f82db2014-06-06 15:20:32 +1000551 error = xfs_dir2_leaf_readbuf(args, bufsize, map_info,
Dave Chinner4a8af272013-08-12 20:49:36 +1000552 &curoff, &bp);
553 if (error || !map_info->map_valid)
554 break;
555
556 /*
557 * Having done a read, we need to set a new offset.
558 */
Dave Chinner53f82db2014-06-06 15:20:32 +1000559 newoff = xfs_dir2_db_off_to_byte(geo,
Dave Chinner9b3b5522014-06-06 15:06:53 +1000560 map_info->curdb, 0);
Dave Chinner4a8af272013-08-12 20:49:36 +1000561 /*
562 * Start of the current block.
563 */
564 if (curoff < newoff)
565 curoff = newoff;
566 /*
567 * Make sure we're in the right block.
568 */
569 else if (curoff > newoff)
Dave Chinner30028032014-06-06 15:08:18 +1000570 ASSERT(xfs_dir2_byte_to_db(geo, curoff) ==
Dave Chinner4a8af272013-08-12 20:49:36 +1000571 map_info->curdb);
572 hdr = bp->b_addr;
573 xfs_dir3_data_check(dp, bp);
574 /*
575 * Find our position in the block.
576 */
Dave Chinner2ca98772013-10-29 22:11:49 +1100577 ptr = (char *)dp->d_ops->data_entry_p(hdr);
Dave Chinner53f82db2014-06-06 15:20:32 +1000578 byteoff = xfs_dir2_byte_to_off(geo, curoff);
Dave Chinner4a8af272013-08-12 20:49:36 +1000579 /*
580 * Skip past the header.
581 */
582 if (byteoff == 0)
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100583 curoff += dp->d_ops->data_entry_offset;
Dave Chinner4a8af272013-08-12 20:49:36 +1000584 /*
585 * Skip past entries until we reach our offset.
586 */
587 else {
588 while ((char *)ptr - (char *)hdr < byteoff) {
589 dup = (xfs_dir2_data_unused_t *)ptr;
590
591 if (be16_to_cpu(dup->freetag)
592 == XFS_DIR2_DATA_FREE_TAG) {
593
594 length = be16_to_cpu(dup->length);
595 ptr += length;
596 continue;
597 }
598 dep = (xfs_dir2_data_entry_t *)ptr;
599 length =
Dave Chinner9d23fc82013-10-29 22:11:48 +1100600 dp->d_ops->data_entsize(dep->namelen);
Dave Chinner4a8af272013-08-12 20:49:36 +1000601 ptr += length;
602 }
603 /*
604 * Now set our real offset.
605 */
606 curoff =
Dave Chinner30028032014-06-06 15:08:18 +1000607 xfs_dir2_db_off_to_byte(geo,
608 xfs_dir2_byte_to_db(geo, curoff),
Dave Chinner4a8af272013-08-12 20:49:36 +1000609 (char *)ptr - (char *)hdr);
Dave Chinner8f661932014-06-06 15:15:59 +1000610 if (ptr >= (char *)hdr + geo->blksize) {
Dave Chinner4a8af272013-08-12 20:49:36 +1000611 continue;
612 }
613 }
614 }
615 /*
616 * We have a pointer to an entry.
617 * Is it a live one?
618 */
619 dup = (xfs_dir2_data_unused_t *)ptr;
620 /*
621 * No, it's unused, skip over it.
622 */
623 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
624 length = be16_to_cpu(dup->length);
625 ptr += length;
626 curoff += length;
627 continue;
628 }
629
630 dep = (xfs_dir2_data_entry_t *)ptr;
Dave Chinner9d23fc82013-10-29 22:11:48 +1100631 length = dp->d_ops->data_entsize(dep->namelen);
632 filetype = dp->d_ops->data_get_ftype(dep);
Dave Chinner4a8af272013-08-12 20:49:36 +1000633
Eric Sandeen25994052014-04-14 19:02:30 +1000634 ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
Dave Chinner4a8af272013-08-12 20:49:36 +1000635 if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
Dave Chinner0cb97762013-08-12 20:50:09 +1000636 be64_to_cpu(dep->inumber),
Dave Chinner53f82db2014-06-06 15:20:32 +1000637 xfs_dir3_get_dtype(dp->i_mount, filetype)))
Dave Chinner4a8af272013-08-12 20:49:36 +1000638 break;
639
640 /*
641 * Advance to next entry in the block.
642 */
643 ptr += length;
644 curoff += length;
645 /* bufsize may have just been a guess; don't go negative */
646 bufsize = bufsize > length ? bufsize - length : 0;
647 }
648
649 /*
650 * All done. Set output offset value to current offset.
651 */
Eric Sandeen25994052014-04-14 19:02:30 +1000652 if (curoff > xfs_dir2_dataptr_to_byte(XFS_DIR2_MAX_DATAPTR))
Dave Chinner4a8af272013-08-12 20:49:36 +1000653 ctx->pos = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
654 else
Eric Sandeen25994052014-04-14 19:02:30 +1000655 ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
Dave Chinner4a8af272013-08-12 20:49:36 +1000656 kmem_free(map_info);
657 if (bp)
658 xfs_trans_brelse(NULL, bp);
659 return error;
660}
661
662/*
663 * Read a directory.
664 */
665int
666xfs_readdir(
Dave Chinner53f82db2014-06-06 15:20:32 +1000667 struct xfs_inode *dp,
668 struct dir_context *ctx,
669 size_t bufsize)
Dave Chinner4a8af272013-08-12 20:49:36 +1000670{
Dave Chinner35f46c52014-06-10 07:30:36 +1000671 struct xfs_da_args args = { NULL };
Dave Chinner53f82db2014-06-06 15:20:32 +1000672 int rval;
673 int v;
674 uint lock_mode;
Dave Chinner4a8af272013-08-12 20:49:36 +1000675
676 trace_xfs_readdir(dp);
677
678 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000679 return -EIO;
Dave Chinner4a8af272013-08-12 20:49:36 +1000680
681 ASSERT(S_ISDIR(dp->i_d.di_mode));
682 XFS_STATS_INC(xs_dir_getdents);
683
Dave Chinner53f82db2014-06-06 15:20:32 +1000684 args.dp = dp;
685 args.geo = dp->i_mount->m_dir_geo;
686
Ben Myers40194ec2013-12-06 12:30:11 -0800687 lock_mode = xfs_ilock_data_map_shared(dp);
Dave Chinner4a8af272013-08-12 20:49:36 +1000688 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
Dave Chinner53f82db2014-06-06 15:20:32 +1000689 rval = xfs_dir2_sf_getdents(&args, ctx);
690 else if ((rval = xfs_dir2_isblock(&args, &v)))
Dave Chinner4a8af272013-08-12 20:49:36 +1000691 ;
692 else if (v)
Dave Chinner53f82db2014-06-06 15:20:32 +1000693 rval = xfs_dir2_block_getdents(&args, ctx);
Dave Chinner4a8af272013-08-12 20:49:36 +1000694 else
Dave Chinner53f82db2014-06-06 15:20:32 +1000695 rval = xfs_dir2_leaf_getdents(&args, ctx, bufsize);
Ben Myers40194ec2013-12-06 12:30:11 -0800696 xfs_iunlock(dp, lock_mode);
697
Dave Chinner4a8af272013-08-12 20:49:36 +1000698 return rval;
699}