]> git.ipfire.org Git - people/ms/linux.git/blame - fs/xfs/xfs_dir2_readdir.c
xfs: devirtualize ->sf_entsize and ->sf_nextentry
[people/ms/linux.git] / fs / xfs / xfs_dir2_readdir.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
4a8af273
DC
2/*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
5 * All Rights Reserved.
4a8af273
DC
6 */
7#include "xfs.h"
8#include "xfs_fs.h"
5467b34b 9#include "xfs_shared.h"
a4fbe6ab 10#include "xfs_format.h"
239880ef
DC
11#include "xfs_log_format.h"
12#include "xfs_trans_resv.h"
4a8af273 13#include "xfs_mount.h"
4a8af273 14#include "xfs_inode.h"
2b9ab5ab 15#include "xfs_dir2.h"
4a8af273 16#include "xfs_dir2_priv.h"
4a8af273
DC
17#include "xfs_trace.h"
18#include "xfs_bmap.h"
239880ef 19#include "xfs_trans.h"
04df34ac 20#include "xfs_error.h"
4a8af273 21
0cb97766
DC
22/*
23 * Directory file type support functions
24 */
25static unsigned char xfs_dir3_filetype_table[] = {
26 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK,
27 DT_FIFO, DT_SOCK, DT_LNK, DT_WHT,
28};
29
a5c46e5e 30unsigned char
0cb97766
DC
31xfs_dir3_get_dtype(
32 struct xfs_mount *mp,
c8ce540d 33 uint8_t filetype)
0cb97766
DC
34{
35 if (!xfs_sb_version_hasftype(&mp->m_sb))
36 return DT_UNKNOWN;
37
38 if (filetype >= XFS_DIR3_FT_MAX)
39 return DT_UNKNOWN;
40
41 return xfs_dir3_filetype_table[filetype];
42}
0cb97766 43
4a8af273
DC
44STATIC int
45xfs_dir2_sf_getdents(
53f82db0 46 struct xfs_da_args *args,
4a8af273
DC
47 struct dir_context *ctx)
48{
49 int i; /* shortform entry number */
53f82db0 50 struct xfs_inode *dp = args->dp; /* incore directory inode */
50f6bb6b 51 struct xfs_mount *mp = dp->i_mount;
4a8af273
DC
52 xfs_dir2_dataptr_t off; /* current entry's offset */
53 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
54 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
55 xfs_dir2_dataptr_t dot_offset;
56 xfs_dir2_dataptr_t dotdot_offset;
57 xfs_ino_t ino;
53f82db0 58 struct xfs_da_geometry *geo = args->geo;
4a8af273
DC
59
60 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
4a8af273
DC
61 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
62 ASSERT(dp->i_df.if_u1.if_data != NULL);
63
64 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
65
4a8af273
DC
66 /*
67 * If the block number in the offset is out of range, we're done.
68 */
7dda6e86 69 if (xfs_dir2_dataptr_to_db(geo, ctx->pos) > geo->datablk)
4a8af273
DC
70 return 0;
71
72 /*
73 * Precalculate offsets for . and .. as we will always need them.
74 *
75 * XXX(hch): the second argument is sometimes 0 and sometimes
7dda6e86 76 * geo->datablk
4a8af273 77 */
7dda6e86 78 dot_offset = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
1c9a5b2e 79 dp->d_ops->data_dot_offset);
7dda6e86 80 dotdot_offset = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
1c9a5b2e 81 dp->d_ops->data_dotdot_offset);
4a8af273
DC
82
83 /*
84 * Put . entry unless we're starting past it.
85 */
86 if (ctx->pos <= dot_offset) {
87 ctx->pos = dot_offset & 0x7fffffff;
88 if (!dir_emit(ctx, ".", 1, dp->i_ino, DT_DIR))
89 return 0;
90 }
91
92 /*
93 * Put .. entry unless we're starting past it.
94 */
95 if (ctx->pos <= dotdot_offset) {
84915e1b 96 ino = xfs_dir2_sf_get_parent_ino(sfp);
4a8af273
DC
97 ctx->pos = dotdot_offset & 0x7fffffff;
98 if (!dir_emit(ctx, "..", 2, ino, DT_DIR))
99 return 0;
100 }
101
102 /*
103 * Loop while there are more entries and put'ing works.
104 */
105 sfep = xfs_dir2_sf_firstentry(sfp);
106 for (i = 0; i < sfp->count; i++) {
c8ce540d 107 uint8_t filetype;
0cb97766 108
7dda6e86 109 off = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
4a8af273
DC
110 xfs_dir2_sf_get_offset(sfep));
111
112 if (ctx->pos > off) {
50f6bb6b 113 sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
4a8af273
DC
114 continue;
115 }
116
4740175e
DC
117 ino = dp->d_ops->sf_get_ino(sfp, sfep);
118 filetype = dp->d_ops->sf_get_ftype(sfep);
4a8af273 119 ctx->pos = off & 0x7fffffff;
04df34ac
DW
120 if (!xfs_dir2_namecheck(sfep->name, sfep->namelen)) {
121 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW,
122 dp->i_mount);
123 return -EFSCORRUPTED;
124 }
0cb97766 125 if (!dir_emit(ctx, (char *)sfep->name, sfep->namelen, ino,
50f6bb6b 126 xfs_dir3_get_dtype(mp, filetype)))
4a8af273 127 return 0;
50f6bb6b 128 sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
4a8af273
DC
129 }
130
7dda6e86 131 ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) &
53f82db0 132 0x7fffffff;
4a8af273
DC
133 return 0;
134}
135
136/*
137 * Readdir for block directories.
138 */
139STATIC int
140xfs_dir2_block_getdents(
53f82db0 141 struct xfs_da_args *args,
4a8af273
DC
142 struct dir_context *ctx)
143{
53f82db0 144 struct xfs_inode *dp = args->dp; /* incore directory inode */
4a8af273
DC
145 xfs_dir2_data_hdr_t *hdr; /* block header */
146 struct xfs_buf *bp; /* buffer for block */
4a8af273
DC
147 xfs_dir2_data_entry_t *dep; /* block data entry */
148 xfs_dir2_data_unused_t *dup; /* block unused entry */
149 char *endptr; /* end of the data entries */
150 int error; /* error return value */
4a8af273
DC
151 char *ptr; /* current data entry */
152 int wantoff; /* starting block offset */
153 xfs_off_t cook;
53f82db0 154 struct xfs_da_geometry *geo = args->geo;
dbad7c99 155 int lock_mode;
4a8af273 156
4a8af273
DC
157 /*
158 * If the block number in the offset is out of range, we're done.
159 */
7dda6e86 160 if (xfs_dir2_dataptr_to_db(geo, ctx->pos) > geo->datablk)
4a8af273
DC
161 return 0;
162
dbad7c99 163 lock_mode = xfs_ilock_data_map_shared(dp);
acb9553c 164 error = xfs_dir3_block_read(args->trans, dp, &bp);
dbad7c99 165 xfs_iunlock(dp, lock_mode);
4a8af273
DC
166 if (error)
167 return error;
168
169 /*
170 * Extract the byte offset we start at from the seek pointer.
171 * We'll skip entries before this.
172 */
30028030 173 wantoff = xfs_dir2_dataptr_to_off(geo, ctx->pos);
4a8af273
DC
174 hdr = bp->b_addr;
175 xfs_dir3_data_check(dp, bp);
176 /*
177 * Set up values for the loop.
178 */
2ca98774 179 ptr = (char *)dp->d_ops->data_entry_p(hdr);
ce92d29d 180 endptr = xfs_dir3_data_endp(geo, hdr);
4a8af273
DC
181
182 /*
183 * Loop over the data portion of the block.
184 * Each object is a real entry (dep) or an unused one (dup).
185 */
186 while (ptr < endptr) {
c8ce540d 187 uint8_t filetype;
0cb97766 188
4a8af273
DC
189 dup = (xfs_dir2_data_unused_t *)ptr;
190 /*
191 * Unused, skip it.
192 */
193 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
194 ptr += be16_to_cpu(dup->length);
195 continue;
196 }
197
198 dep = (xfs_dir2_data_entry_t *)ptr;
199
200 /*
201 * Bump pointer for the next iteration.
202 */
9d23fc85 203 ptr += dp->d_ops->data_entsize(dep->namelen);
4a8af273
DC
204 /*
205 * The entry is before the desired starting point, skip it.
206 */
207 if ((char *)dep - (char *)hdr < wantoff)
208 continue;
209
7dda6e86 210 cook = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
4a8af273
DC
211 (char *)dep - (char *)hdr);
212
213 ctx->pos = cook & 0x7fffffff;
9d23fc85 214 filetype = dp->d_ops->data_get_ftype(dep);
4a8af273
DC
215 /*
216 * If it didn't fit, set the final offset to here & return.
217 */
04df34ac
DW
218 if (!xfs_dir2_namecheck(dep->name, dep->namelen)) {
219 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW,
220 dp->i_mount);
221 error = -EFSCORRUPTED;
222 goto out_rele;
223 }
4a8af273 224 if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
0cb97766 225 be64_to_cpu(dep->inumber),
04df34ac
DW
226 xfs_dir3_get_dtype(dp->i_mount, filetype)))
227 goto out_rele;
4a8af273
DC
228 }
229
230 /*
231 * Reached the end of the block.
232 * Set the offset to a non-existent block 1 and return.
233 */
7dda6e86 234 ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) &
53f82db0 235 0x7fffffff;
04df34ac 236out_rele:
acb9553c 237 xfs_trans_brelse(args->trans, bp);
04df34ac 238 return error;
4a8af273
DC
239}
240
d205a7d0
DW
241/*
242 * Read a directory block and initiate readahead for blocks beyond that.
243 * We maintain a sliding readahead window of the remaining space in the
244 * buffer rounded up to the nearest block.
245 */
4a8af273
DC
246STATIC int
247xfs_dir2_leaf_readbuf(
53f82db0 248 struct xfs_da_args *args,
4a8af273 249 size_t bufsize,
d205a7d0
DW
250 xfs_dir2_off_t *cur_off,
251 xfs_dablk_t *ra_blk,
252 struct xfs_buf **bpp)
4a8af273 253{
53f82db0 254 struct xfs_inode *dp = args->dp;
9f541801 255 struct xfs_buf *bp = NULL;
d205a7d0
DW
256 struct xfs_da_geometry *geo = args->geo;
257 struct xfs_ifork *ifp = XFS_IFORK_PTR(dp, XFS_DATA_FORK);
258 struct xfs_bmbt_irec map;
4a8af273 259 struct blk_plug plug;
d205a7d0
DW
260 xfs_dir2_off_t new_off;
261 xfs_dablk_t next_ra;
262 xfs_dablk_t map_off;
263 xfs_dablk_t last_da;
b2b1712a 264 struct xfs_iext_cursor icur;
d205a7d0 265 int ra_want;
4a8af273 266 int error = 0;
4a8af273 267
d205a7d0
DW
268 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
269 error = xfs_iread_extents(args->trans, dp, XFS_DATA_FORK);
4a8af273 270 if (error)
d205a7d0 271 goto out;
4a8af273
DC
272 }
273
274 /*
d205a7d0
DW
275 * Look for mapped directory blocks at or above the current offset.
276 * Truncate down to the nearest directory block to start the scanning
277 * operation.
4a8af273 278 */
d205a7d0
DW
279 last_da = xfs_dir2_byte_to_da(geo, XFS_DIR2_LEAF_OFFSET);
280 map_off = xfs_dir2_db_to_da(geo, xfs_dir2_byte_to_db(geo, *cur_off));
b2b1712a 281 if (!xfs_iext_lookup_extent(dp, ifp, map_off, &icur, &map))
4a8af273 282 goto out;
d205a7d0
DW
283 if (map.br_startoff >= last_da)
284 goto out;
285 xfs_trim_extent(&map, map_off, last_da - map_off);
4a8af273 286
d205a7d0
DW
287 /* Read the directory block of that first mapping. */
288 new_off = xfs_dir2_da_to_byte(geo, map.br_startoff);
289 if (new_off > *cur_off)
290 *cur_off = new_off;
291 error = xfs_dir3_data_read(args->trans, dp, map.br_startoff, -1, &bp);
4a8af273 292 if (error)
d205a7d0 293 goto out;
4a8af273
DC
294
295 /*
d205a7d0
DW
296 * Start readahead for the next bufsize's worth of dir data blocks.
297 * We may have already issued readahead for some of that range;
298 * ra_blk tracks the last block we tried to read(ahead).
4a8af273 299 */
d205a7d0
DW
300 ra_want = howmany(bufsize + geo->blksize, (1 << geo->fsblog));
301 if (*ra_blk >= last_da)
302 goto out;
303 else if (*ra_blk == 0)
304 *ra_blk = map.br_startoff;
305 next_ra = map.br_startoff + geo->fsbcount;
306 if (next_ra >= last_da)
307 goto out_no_ra;
308 if (map.br_blockcount < geo->fsbcount &&
b2b1712a 309 !xfs_iext_next_extent(ifp, &icur, &map))
d205a7d0
DW
310 goto out_no_ra;
311 if (map.br_startoff >= last_da)
312 goto out_no_ra;
313 xfs_trim_extent(&map, next_ra, last_da - next_ra);
314
315 /* Start ra for each dir (not fs) block that has a mapping. */
4a8af273 316 blk_start_plug(&plug);
d205a7d0
DW
317 while (ra_want > 0) {
318 next_ra = roundup((xfs_dablk_t)map.br_startoff, geo->fsbcount);
319 while (ra_want > 0 &&
320 next_ra < map.br_startoff + map.br_blockcount) {
321 if (next_ra >= last_da) {
322 *ra_blk = last_da;
323 break;
324 }
325 if (next_ra > *ra_blk) {
326 xfs_dir3_data_readahead(dp, next_ra, -2);
327 *ra_blk = next_ra;
4a8af273 328 }
d205a7d0
DW
329 ra_want -= geo->fsbcount;
330 next_ra += geo->fsbcount;
331 }
b2b1712a 332 if (!xfs_iext_next_extent(ifp, &icur, &map)) {
d205a7d0
DW
333 *ra_blk = last_da;
334 break;
4a8af273
DC
335 }
336 }
337 blk_finish_plug(&plug);
338
339out:
340 *bpp = bp;
341 return error;
d205a7d0
DW
342out_no_ra:
343 *ra_blk = last_da;
344 goto out;
4a8af273
DC
345}
346
347/*
348 * Getdents (readdir) for leaf and node directories.
349 * This reads the data blocks only, so is the same for both forms.
350 */
351STATIC int
352xfs_dir2_leaf_getdents(
53f82db0 353 struct xfs_da_args *args,
4a8af273
DC
354 struct dir_context *ctx,
355 size_t bufsize)
356{
53f82db0 357 struct xfs_inode *dp = args->dp;
4a8af273
DC
358 struct xfs_buf *bp = NULL; /* data block buffer */
359 xfs_dir2_data_hdr_t *hdr; /* data block header */
360 xfs_dir2_data_entry_t *dep; /* data entry */
361 xfs_dir2_data_unused_t *dup; /* unused entry */
4a8af273 362 char *ptr = NULL; /* pointer to current data */
53f82db0 363 struct xfs_da_geometry *geo = args->geo;
d205a7d0
DW
364 xfs_dablk_t rablk = 0; /* current readahead block */
365 xfs_dir2_off_t curoff; /* current overall offset */
366 int length; /* temporary length value */
367 int byteoff; /* offset in current block */
368 int lock_mode;
369 int error = 0; /* error return value */
4a8af273
DC
370
371 /*
372 * If the offset is at or past the largest allowed value,
373 * give up right away.
374 */
375 if (ctx->pos >= XFS_DIR2_MAX_DATAPTR)
376 return 0;
377
4a8af273
DC
378 /*
379 * Inside the loop we keep the main offset value as a byte offset
380 * in the directory file.
381 */
25994053 382 curoff = xfs_dir2_dataptr_to_byte(ctx->pos);
4a8af273 383
4a8af273
DC
384 /*
385 * Loop over directory entries until we reach the end offset.
386 * Get more blocks and readahead as necessary.
387 */
388 while (curoff < XFS_DIR2_LEAF_OFFSET) {
c8ce540d 389 uint8_t filetype;
0cb97766 390
4a8af273
DC
391 /*
392 * If we have no buffer, or we're off the end of the
393 * current buffer, need to get another one.
394 */
8f66193c 395 if (!bp || ptr >= (char *)bp->b_addr + geo->blksize) {
9f541801 396 if (bp) {
d205a7d0 397 xfs_trans_brelse(args->trans, bp);
9f541801 398 bp = NULL;
9f541801 399 }
4a8af273 400
dbad7c99 401 lock_mode = xfs_ilock_data_map_shared(dp);
d205a7d0
DW
402 error = xfs_dir2_leaf_readbuf(args, bufsize, &curoff,
403 &rablk, &bp);
dbad7c99 404 xfs_iunlock(dp, lock_mode);
d205a7d0 405 if (error || !bp)
4a8af273
DC
406 break;
407
4a8af273
DC
408 hdr = bp->b_addr;
409 xfs_dir3_data_check(dp, bp);
410 /*
411 * Find our position in the block.
412 */
2ca98774 413 ptr = (char *)dp->d_ops->data_entry_p(hdr);
53f82db0 414 byteoff = xfs_dir2_byte_to_off(geo, curoff);
4a8af273
DC
415 /*
416 * Skip past the header.
417 */
418 if (byteoff == 0)
1c9a5b2e 419 curoff += dp->d_ops->data_entry_offset;
4a8af273
DC
420 /*
421 * Skip past entries until we reach our offset.
422 */
423 else {
424 while ((char *)ptr - (char *)hdr < byteoff) {
425 dup = (xfs_dir2_data_unused_t *)ptr;
426
427 if (be16_to_cpu(dup->freetag)
428 == XFS_DIR2_DATA_FREE_TAG) {
429
430 length = be16_to_cpu(dup->length);
431 ptr += length;
432 continue;
433 }
434 dep = (xfs_dir2_data_entry_t *)ptr;
435 length =
9d23fc85 436 dp->d_ops->data_entsize(dep->namelen);
4a8af273
DC
437 ptr += length;
438 }
439 /*
440 * Now set our real offset.
441 */
442 curoff =
30028030
DC
443 xfs_dir2_db_off_to_byte(geo,
444 xfs_dir2_byte_to_db(geo, curoff),
4a8af273 445 (char *)ptr - (char *)hdr);
8f66193c 446 if (ptr >= (char *)hdr + geo->blksize) {
4a8af273
DC
447 continue;
448 }
449 }
450 }
451 /*
452 * We have a pointer to an entry.
453 * Is it a live one?
454 */
455 dup = (xfs_dir2_data_unused_t *)ptr;
456 /*
457 * No, it's unused, skip over it.
458 */
459 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
460 length = be16_to_cpu(dup->length);
461 ptr += length;
462 curoff += length;
463 continue;
464 }
465
466 dep = (xfs_dir2_data_entry_t *)ptr;
9d23fc85
DC
467 length = dp->d_ops->data_entsize(dep->namelen);
468 filetype = dp->d_ops->data_get_ftype(dep);
4a8af273 469
25994053 470 ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
04df34ac
DW
471 if (!xfs_dir2_namecheck(dep->name, dep->namelen)) {
472 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW,
473 dp->i_mount);
474 error = -EFSCORRUPTED;
475 break;
476 }
4a8af273 477 if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
0cb97766 478 be64_to_cpu(dep->inumber),
53f82db0 479 xfs_dir3_get_dtype(dp->i_mount, filetype)))
4a8af273
DC
480 break;
481
482 /*
483 * Advance to next entry in the block.
484 */
485 ptr += length;
486 curoff += length;
487 /* bufsize may have just been a guess; don't go negative */
488 bufsize = bufsize > length ? bufsize - length : 0;
489 }
490
491 /*
492 * All done. Set output offset value to current offset.
493 */
25994053 494 if (curoff > xfs_dir2_dataptr_to_byte(XFS_DIR2_MAX_DATAPTR))
4a8af273
DC
495 ctx->pos = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
496 else
25994053 497 ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
4a8af273 498 if (bp)
acb9553c 499 xfs_trans_brelse(args->trans, bp);
4a8af273
DC
500 return error;
501}
502
503/*
504 * Read a directory.
acb9553c
DW
505 *
506 * If supplied, the transaction collects locked dir buffers to avoid
507 * nested buffer deadlocks. This function does not dirty the
508 * transaction. The caller should ensure that the inode is locked
509 * before calling this function.
4a8af273
DC
510 */
511int
512xfs_readdir(
acb9553c 513 struct xfs_trans *tp,
53f82db0
DC
514 struct xfs_inode *dp,
515 struct dir_context *ctx,
516 size_t bufsize)
4a8af273 517{
35f46c5f 518 struct xfs_da_args args = { NULL };
53f82db0
DC
519 int rval;
520 int v;
4a8af273
DC
521
522 trace_xfs_readdir(dp);
523
524 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
2451337d 525 return -EIO;
4a8af273 526
c19b3b05 527 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
ff6d6af2 528 XFS_STATS_INC(dp->i_mount, xs_dir_getdents);
4a8af273 529
53f82db0
DC
530 args.dp = dp;
531 args.geo = dp->i_mount->m_dir_geo;
acb9553c 532 args.trans = tp;
53f82db0 533
4a8af273 534 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
53f82db0
DC
535 rval = xfs_dir2_sf_getdents(&args, ctx);
536 else if ((rval = xfs_dir2_isblock(&args, &v)))
4a8af273
DC
537 ;
538 else if (v)
53f82db0 539 rval = xfs_dir2_block_getdents(&args, ctx);
4a8af273 540 else
53f82db0 541 rval = xfs_dir2_leaf_getdents(&args, ctx, bufsize);
40194ecc 542
4a8af273
DC
543 return rval;
544}