Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : /*
3 : * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 : * Copyright (c) 2013 Red Hat, Inc.
5 : * All Rights Reserved.
6 : */
7 : #include "xfs.h"
8 : #include "xfs_fs.h"
9 : #include "xfs_shared.h"
10 : #include "xfs_format.h"
11 : #include "xfs_log_format.h"
12 : #include "xfs_trans_resv.h"
13 : #include "xfs_mount.h"
14 : #include "xfs_inode.h"
15 : #include "xfs_dir2.h"
16 : #include "xfs_dir2_priv.h"
17 : #include "xfs_trace.h"
18 : #include "xfs_bmap.h"
19 : #include "xfs_trans.h"
20 : #include "xfs_error.h"
21 : #include "xfs_health.h"
22 :
23 : /*
24 : * Directory file type support functions
25 : */
26 : static unsigned char xfs_dir3_filetype_table[] = {
27 : DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK,
28 : DT_FIFO, DT_SOCK, DT_LNK, DT_WHT,
29 : };
30 :
31 : unsigned char
32 0 : xfs_dir3_get_dtype(
33 : struct xfs_mount *mp,
34 : uint8_t filetype)
35 : {
36 0 : if (!xfs_has_ftype(mp))
37 : return DT_UNKNOWN;
38 :
39 23780167434 : if (filetype >= XFS_DIR3_FT_MAX)
40 : return DT_UNKNOWN;
41 :
42 23780167434 : return xfs_dir3_filetype_table[filetype];
43 : }
44 :
45 : STATIC int
46 53559068 : xfs_dir2_sf_getdents(
47 : struct xfs_da_args *args,
48 : struct dir_context *ctx)
49 : {
50 53559068 : int i; /* shortform entry number */
51 53559068 : struct xfs_inode *dp = args->dp; /* incore directory inode */
52 53559068 : struct xfs_mount *mp = dp->i_mount;
53 53559068 : xfs_dir2_dataptr_t off; /* current entry's offset */
54 53559068 : xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
55 53559068 : xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
56 53559068 : xfs_dir2_dataptr_t dot_offset;
57 53559068 : xfs_dir2_dataptr_t dotdot_offset;
58 53559068 : xfs_ino_t ino;
59 53559068 : struct xfs_da_geometry *geo = args->geo;
60 :
61 53559068 : ASSERT(dp->i_df.if_format == XFS_DINODE_FMT_LOCAL);
62 53559068 : ASSERT(dp->i_df.if_bytes == dp->i_disk_size);
63 53559068 : ASSERT(dp->i_df.if_u1.if_data != NULL);
64 :
65 53559068 : sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
66 :
67 : /*
68 : * If the block number in the offset is out of range, we're done.
69 : */
70 53559068 : if (xfs_dir2_dataptr_to_db(geo, ctx->pos) > geo->datablk)
71 : return 0;
72 :
73 : /*
74 : * Precalculate offsets for "." and ".." as we will always need them.
75 : * This relies on the fact that directories always start with the
76 : * entries for "." and "..".
77 : */
78 28084587 : dot_offset = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
79 28084587 : geo->data_entry_offset);
80 28025325 : dotdot_offset = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
81 28025325 : geo->data_entry_offset +
82 : xfs_dir2_data_entsize(mp, sizeof(".") - 1));
83 :
84 : /*
85 : * Put . entry unless we're starting past it.
86 : */
87 27995419 : if (ctx->pos <= dot_offset) {
88 28023233 : ctx->pos = dot_offset & 0x7fffffff;
89 28023233 : if (!dir_emit(ctx, ".", 1, dp->i_ino, DT_DIR))
90 : return 0;
91 : }
92 :
93 : /*
94 : * Put .. entry unless we're starting past it.
95 : */
96 28000015 : if (ctx->pos <= dotdot_offset) {
97 27993188 : ino = xfs_dir2_sf_get_parent_ino(sfp);
98 28094240 : ctx->pos = dotdot_offset & 0x7fffffff;
99 28094240 : if (!dir_emit(ctx, "..", 2, ino, DT_DIR))
100 : return 0;
101 : }
102 :
103 : /*
104 : * Loop while there are more entries and put'ing works.
105 : */
106 28101764 : sfep = xfs_dir2_sf_firstentry(sfp);
107 136958743 : for (i = 0; i < sfp->count; i++) {
108 111096646 : uint8_t filetype;
109 :
110 111096646 : off = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
111 : xfs_dir2_sf_get_offset(sfep));
112 :
113 111168552 : if (ctx->pos > off) {
114 11 : sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
115 11 : continue;
116 : }
117 :
118 111168541 : ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
119 111115112 : filetype = xfs_dir2_sf_get_ftype(mp, sfep);
120 111114602 : ctx->pos = off & 0x7fffffff;
121 111114602 : if (XFS_IS_CORRUPT(dp->i_mount,
122 : !xfs_dir2_namecheck(sfep->name,
123 : sfep->namelen))) {
124 22 : xfs_dirattr_mark_sick(dp, XFS_DATA_FORK);
125 22 : return -EFSCORRUPTED;
126 : }
127 111175181 : if (!dir_emit(ctx, (char *)sfep->name, sfep->namelen, ino,
128 111182528 : xfs_dir3_get_dtype(mp, filetype)))
129 : return 0;
130 108856324 : sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
131 : }
132 :
133 25862097 : ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) &
134 : 0x7fffffff;
135 25862520 : return 0;
136 : }
137 :
138 : /*
139 : * Readdir for block directories.
140 : */
141 : STATIC int
142 2020732 : xfs_dir2_block_getdents(
143 : struct xfs_da_args *args,
144 : struct dir_context *ctx,
145 : unsigned int *lock_mode)
146 : {
147 2020732 : struct xfs_inode *dp = args->dp; /* incore directory inode */
148 2020732 : struct xfs_buf *bp; /* buffer for block */
149 2020732 : int error; /* error return value */
150 2020732 : int wantoff; /* starting block offset */
151 2020732 : xfs_off_t cook;
152 2020732 : struct xfs_da_geometry *geo = args->geo;
153 2020732 : unsigned int offset, next_offset;
154 2020732 : unsigned int end;
155 :
156 : /*
157 : * If the block number in the offset is out of range, we're done.
158 : */
159 2020732 : if (xfs_dir2_dataptr_to_db(geo, ctx->pos) > geo->datablk)
160 : return 0;
161 :
162 1096162 : error = xfs_dir3_block_read(args->trans, dp, args->owner, &bp);
163 1098450 : if (error)
164 : return error;
165 :
166 1098773 : xfs_iunlock(dp, *lock_mode);
167 1101415 : *lock_mode = 0;
168 :
169 : /*
170 : * Extract the byte offset we start at from the seek pointer.
171 : * We'll skip entries before this.
172 : */
173 1101415 : wantoff = xfs_dir2_dataptr_to_off(geo, ctx->pos);
174 1101415 : xfs_dir3_data_check(dp, bp);
175 :
176 : /*
177 : * Loop over the data portion of the block.
178 : * Each object is a real entry (dep) or an unused one (dup).
179 : */
180 1113071 : end = xfs_dir3_data_end_offset(geo, bp->b_addr);
181 1112739 : for (offset = geo->data_entry_offset;
182 70315232 : offset < end;
183 : offset = next_offset) {
184 69360760 : struct xfs_dir2_data_unused *dup = bp->b_addr + offset;
185 69360760 : struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
186 69360760 : uint8_t filetype;
187 :
188 : /*
189 : * Unused, skip it.
190 : */
191 69360760 : if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
192 11931662 : next_offset = offset + be16_to_cpu(dup->length);
193 11931662 : continue;
194 : }
195 :
196 : /*
197 : * Bump pointer for the next iteration.
198 : */
199 114858196 : next_offset = offset +
200 57429098 : xfs_dir2_data_entsize(dp->i_mount, dep->namelen);
201 :
202 : /*
203 : * The entry is before the desired starting point, skip it.
204 : */
205 57429098 : if (offset < wantoff)
206 738484 : continue;
207 :
208 56690614 : cook = xfs_dir2_db_off_to_dataptr(geo, geo->datablk, offset);
209 :
210 56015927 : ctx->pos = cook & 0x7fffffff;
211 56015927 : filetype = xfs_dir2_data_get_ftype(dp->i_mount, dep);
212 : /*
213 : * If it didn't fit, set the final offset to here & return.
214 : */
215 55739001 : if (XFS_IS_CORRUPT(dp->i_mount,
216 : !xfs_dir2_namecheck(dep->name,
217 : dep->namelen))) {
218 0 : xfs_dirattr_mark_sick(dp, XFS_DATA_FORK);
219 0 : error = -EFSCORRUPTED;
220 0 : goto out_rele;
221 : }
222 56691009 : if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
223 55731164 : be64_to_cpu(dep->inumber),
224 55788793 : xfs_dir3_get_dtype(dp->i_mount, filetype)))
225 158662 : goto out_rele;
226 : }
227 :
228 : /*
229 : * Reached the end of the block.
230 : * Set the offset to a non-existent block 1 and return.
231 : */
232 954472 : ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) &
233 : 0x7fffffff;
234 1113095 : out_rele:
235 1113095 : xfs_trans_brelse(args->trans, bp);
236 1113095 : return error;
237 : }
238 :
239 : /*
240 : * Read a directory block and initiate readahead for blocks beyond that.
241 : * We maintain a sliding readahead window of the remaining space in the
242 : * buffer rounded up to the nearest block.
243 : */
244 : STATIC int
245 161314939 : xfs_dir2_leaf_readbuf(
246 : struct xfs_da_args *args,
247 : size_t bufsize,
248 : xfs_dir2_off_t *cur_off,
249 : xfs_dablk_t *ra_blk,
250 : struct xfs_buf **bpp)
251 : {
252 161314939 : struct xfs_inode *dp = args->dp;
253 161314939 : struct xfs_buf *bp = NULL;
254 161314939 : struct xfs_da_geometry *geo = args->geo;
255 161314939 : struct xfs_ifork *ifp = xfs_ifork_ptr(dp, XFS_DATA_FORK);
256 161314939 : struct xfs_bmbt_irec map;
257 161314939 : struct blk_plug plug;
258 161314939 : xfs_dir2_off_t new_off;
259 161314939 : xfs_dablk_t next_ra;
260 161314939 : xfs_dablk_t map_off;
261 161314939 : xfs_dablk_t last_da;
262 161314939 : struct xfs_iext_cursor icur;
263 161314939 : int ra_want;
264 161314939 : int error = 0;
265 :
266 161314939 : error = xfs_iread_extents(args->trans, dp, XFS_DATA_FORK);
267 161264472 : if (error)
268 0 : goto out;
269 :
270 : /*
271 : * Look for mapped directory blocks at or above the current offset.
272 : * Truncate down to the nearest directory block to start the scanning
273 : * operation.
274 : */
275 161264472 : last_da = xfs_dir2_byte_to_da(geo, XFS_DIR2_LEAF_OFFSET);
276 161232787 : map_off = xfs_dir2_db_to_da(geo, xfs_dir2_byte_to_db(geo, *cur_off));
277 161211296 : if (!xfs_iext_lookup_extent(dp, ifp, map_off, &icur, &map))
278 0 : goto out;
279 161300857 : if (map.br_startoff >= last_da)
280 17864083 : goto out;
281 143436774 : xfs_trim_extent(&map, map_off, last_da - map_off);
282 :
283 : /* Read the directory block of that first mapping. */
284 143409205 : new_off = xfs_dir2_da_to_byte(geo, map.br_startoff);
285 143385141 : if (new_off > *cur_off)
286 1496 : *cur_off = new_off;
287 143385141 : error = xfs_dir3_data_read(args->trans, dp, args->owner,
288 143385141 : map.br_startoff, 0, &bp);
289 143382376 : if (error)
290 386 : goto out;
291 :
292 : /*
293 : * Start readahead for the next bufsize's worth of dir data blocks.
294 : * We may have already issued readahead for some of that range;
295 : * ra_blk tracks the last block we tried to read(ahead).
296 : */
297 143381990 : ra_want = howmany(bufsize + geo->blksize, (1 << geo->fsblog));
298 143381990 : if (*ra_blk >= last_da)
299 38089052 : goto out;
300 105292938 : else if (*ra_blk == 0)
301 25792510 : *ra_blk = map.br_startoff;
302 105292938 : next_ra = map.br_startoff + geo->fsbcount;
303 105292938 : if (next_ra >= last_da)
304 0 : goto out_no_ra;
305 105293003 : if (map.br_blockcount < geo->fsbcount &&
306 65 : !xfs_iext_next_extent(ifp, &icur, &map))
307 0 : goto out_no_ra;
308 105292938 : if (map.br_startoff >= last_da)
309 0 : goto out_no_ra;
310 105292938 : xfs_trim_extent(&map, next_ra, last_da - next_ra);
311 :
312 : /* Start ra for each dir (not fs) block that has a mapping. */
313 105262095 : blk_start_plug(&plug);
314 885496400 : while (ra_want > 0) {
315 792203981 : next_ra = roundup((xfs_dablk_t)map.br_startoff, geo->fsbcount);
316 1487122664 : while (ra_want > 0 &&
317 1393838767 : next_ra < map.br_startoff + map.br_blockcount) {
318 840749188 : if (next_ra >= last_da) {
319 146744347 : *ra_blk = last_da;
320 146744347 : break;
321 : }
322 694004841 : if (next_ra > *ra_blk) {
323 183168979 : xfs_dir3_data_readahead(dp, next_ra,
324 : XFS_DABUF_MAP_HOLE_OK);
325 184082821 : *ra_blk = next_ra;
326 : }
327 694918683 : ra_want -= geo->fsbcount;
328 694918683 : next_ra += geo->fsbcount;
329 : }
330 793117823 : if (!xfs_iext_next_extent(ifp, &icur, &map)) {
331 12094658 : *ra_blk = last_da;
332 12094658 : break;
333 : }
334 : }
335 105369452 : blk_finish_plug(&plug);
336 :
337 161311571 : out:
338 161311571 : *bpp = bp;
339 161311571 : return error;
340 0 : out_no_ra:
341 0 : *ra_blk = last_da;
342 0 : goto out;
343 : }
344 :
345 : /*
346 : * Getdents (readdir) for leaf and node directories.
347 : * This reads the data blocks only, so is the same for both forms.
348 : */
349 : STATIC int
350 35101737 : xfs_dir2_leaf_getdents(
351 : struct xfs_da_args *args,
352 : struct dir_context *ctx,
353 : size_t bufsize,
354 : unsigned int *lock_mode)
355 : {
356 35101737 : struct xfs_inode *dp = args->dp;
357 35101737 : struct xfs_mount *mp = dp->i_mount;
358 35101737 : struct xfs_buf *bp = NULL; /* data block buffer */
359 35101737 : xfs_dir2_data_entry_t *dep; /* data entry */
360 35101737 : xfs_dir2_data_unused_t *dup; /* unused entry */
361 35101737 : struct xfs_da_geometry *geo = args->geo;
362 35101737 : xfs_dablk_t rablk = 0; /* current readahead block */
363 35101737 : xfs_dir2_off_t curoff; /* current overall offset */
364 35101737 : int length; /* temporary length value */
365 35101737 : int byteoff; /* offset in current block */
366 35101737 : unsigned int offset = 0;
367 35101737 : int error = 0; /* error return value */
368 :
369 : /*
370 : * If the offset is at or past the largest allowed value,
371 : * give up right away.
372 : */
373 35101737 : if (ctx->pos >= XFS_DIR2_MAX_DATAPTR)
374 : return 0;
375 :
376 : /*
377 : * Inside the loop we keep the main offset value as a byte offset
378 : * in the directory file.
379 : */
380 35101737 : curoff = xfs_dir2_dataptr_to_byte(ctx->pos);
381 :
382 : /*
383 : * Loop over directory entries until we reach the end offset.
384 : * Get more blocks and readahead as necessary.
385 : */
386 25261047995 : while (curoff < XFS_DIR2_LEAF_OFFSET) {
387 25261047995 : uint8_t filetype;
388 :
389 : /*
390 : * If we have no buffer, or we're off the end of the
391 : * current buffer, need to get another one.
392 : */
393 25261047995 : if (!bp || offset >= geo->blksize) {
394 161329036 : if (bp) {
395 126234851 : xfs_trans_brelse(args->trans, bp);
396 126245906 : bp = NULL;
397 : }
398 :
399 161340091 : if (*lock_mode == 0)
400 126248268 : *lock_mode = xfs_ilock_data_map_shared(dp);
401 161278629 : error = xfs_dir2_leaf_readbuf(args, bufsize, &curoff,
402 : &rablk, &bp);
403 161306358 : if (error || !bp)
404 : break;
405 :
406 143441736 : xfs_iunlock(dp, *lock_mode);
407 143232246 : *lock_mode = 0;
408 :
409 143232246 : xfs_dir3_data_check(dp, bp);
410 : /*
411 : * Find our position in the block.
412 : */
413 143475132 : offset = geo->data_entry_offset;
414 143475132 : byteoff = xfs_dir2_byte_to_off(geo, curoff);
415 : /*
416 : * Skip past the header.
417 : */
418 143475132 : if (byteoff == 0)
419 126854155 : curoff += geo->data_entry_offset;
420 : /*
421 : * Skip past entries until we reach our offset.
422 : */
423 : else {
424 1705827293 : while (offset < byteoff) {
425 1689206316 : dup = bp->b_addr + offset;
426 :
427 1689206316 : if (be16_to_cpu(dup->freetag)
428 : == XFS_DIR2_DATA_FREE_TAG) {
429 :
430 48310141 : length = be16_to_cpu(dup->length);
431 48310141 : offset += length;
432 48310141 : continue;
433 : }
434 1640896175 : dep = bp->b_addr + offset;
435 1640896175 : length = xfs_dir2_data_entsize(mp,
436 1640896175 : dep->namelen);
437 1640896175 : offset += length;
438 : }
439 : /*
440 : * Now set our real offset.
441 : */
442 33241474 : curoff =
443 16620977 : xfs_dir2_db_off_to_byte(geo,
444 : xfs_dir2_byte_to_db(geo, curoff),
445 : offset);
446 16620497 : if (offset >= geo->blksize)
447 131724 : continue;
448 : }
449 : }
450 :
451 : /*
452 : * We have a pointer to an entry. Is it a live one?
453 : */
454 25243061887 : dup = bp->b_addr + offset;
455 :
456 : /*
457 : * No, it's unused, skip over it.
458 : */
459 25243061887 : if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
460 883384564 : length = be16_to_cpu(dup->length);
461 883384564 : offset += length;
462 883384564 : curoff += length;
463 883384564 : continue;
464 : }
465 :
466 24359677323 : dep = bp->b_addr + offset;
467 24359677323 : length = xfs_dir2_data_entsize(mp, dep->namelen);
468 24359677323 : filetype = xfs_dir2_data_get_ftype(mp, dep);
469 :
470 23376759946 : ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
471 23376759946 : if (XFS_IS_CORRUPT(dp->i_mount,
472 : !xfs_dir2_namecheck(dep->name,
473 : dep->namelen))) {
474 0 : xfs_dirattr_mark_sick(dp, XFS_DATA_FORK);
475 0 : error = -EFSCORRUPTED;
476 0 : break;
477 : }
478 24359686858 : if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
479 23568479226 : be64_to_cpu(dep->inumber),
480 23613196113 : xfs_dir3_get_dtype(dp->i_mount, filetype)))
481 : break;
482 :
483 : /*
484 : * Advance to next entry in the block.
485 : */
486 24342429970 : offset += length;
487 24342429970 : curoff += length;
488 : /* bufsize may have just been a guess; don't go negative */
489 24342429970 : bufsize = bufsize > length ? bufsize - length : 0;
490 : }
491 :
492 : /*
493 : * All done. Set output offset value to current offset.
494 : */
495 35119033 : if (curoff > xfs_dir2_dataptr_to_byte(XFS_DIR2_MAX_DATAPTR))
496 0 : ctx->pos = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
497 : else
498 35119033 : ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
499 35119033 : if (bp)
500 17257069 : xfs_trans_brelse(args->trans, bp);
501 : return error;
502 : }
503 :
504 : /*
505 : * Read a directory.
506 : *
507 : * If supplied, the transaction collects locked dir buffers to avoid
508 : * nested buffer deadlocks. This function does not dirty the
509 : * transaction. The caller must hold the IOLOCK (shared or exclusive)
510 : * before calling this function.
511 : */
512 : int
513 90711403 : xfs_readdir(
514 : struct xfs_trans *tp,
515 : struct xfs_inode *dp,
516 : struct dir_context *ctx,
517 : size_t bufsize)
518 : {
519 90711403 : struct xfs_da_args args = { NULL };
520 90711403 : unsigned int lock_mode;
521 90711403 : bool isblock;
522 90711403 : int error;
523 :
524 90711403 : trace_xfs_readdir(dp);
525 :
526 181287030 : if (xfs_is_shutdown(dp->i_mount))
527 : return -EIO;
528 :
529 90643458 : ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
530 90643458 : ASSERT(xfs_isilocked(dp, XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
531 90530364 : XFS_STATS_INC(dp->i_mount, xs_dir_getdents);
532 :
533 90633702 : args.dp = dp;
534 90633702 : args.geo = dp->i_mount->m_dir_geo;
535 90633702 : args.trans = tp;
536 90633702 : args.owner = dp->i_ino;
537 :
538 90633702 : if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL)
539 53532820 : return xfs_dir2_sf_getdents(&args, ctx);
540 :
541 37100882 : lock_mode = xfs_ilock_data_map_shared(dp);
542 37108567 : error = xfs_dir2_isblock(&args, &isblock);
543 37120240 : if (error)
544 0 : goto out_unlock;
545 :
546 37120240 : if (isblock) {
547 2021835 : error = xfs_dir2_block_getdents(&args, ctx, &lock_mode);
548 2027697 : goto out_unlock;
549 : }
550 :
551 35098405 : error = xfs_dir2_leaf_getdents(&args, ctx, bufsize, &lock_mode);
552 :
553 37138626 : out_unlock:
554 37138626 : if (lock_mode)
555 18768221 : xfs_iunlock(dp, lock_mode);
556 : return error;
557 : }
|