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 21583779939 : if (filetype >= XFS_DIR3_FT_MAX)
40 : return DT_UNKNOWN;
41 :
42 21583779939 : return xfs_dir3_filetype_table[filetype];
43 : }
44 :
45 : STATIC int
46 30633887 : xfs_dir2_sf_getdents(
47 : struct xfs_da_args *args,
48 : struct dir_context *ctx)
49 : {
50 30633887 : int i; /* shortform entry number */
51 30633887 : struct xfs_inode *dp = args->dp; /* incore directory inode */
52 30633887 : struct xfs_mount *mp = dp->i_mount;
53 30633887 : xfs_dir2_dataptr_t off; /* current entry's offset */
54 30633887 : xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
55 30633887 : xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
56 30633887 : xfs_dir2_dataptr_t dot_offset;
57 30633887 : xfs_dir2_dataptr_t dotdot_offset;
58 30633887 : xfs_ino_t ino;
59 30633887 : struct xfs_da_geometry *geo = args->geo;
60 :
61 30633887 : ASSERT(dp->i_df.if_format == XFS_DINODE_FMT_LOCAL);
62 30633887 : ASSERT(dp->i_df.if_bytes == dp->i_disk_size);
63 30633887 : ASSERT(dp->i_df.if_u1.if_data != NULL);
64 :
65 30633887 : 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 30633887 : 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 16065977 : dot_offset = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
79 16065977 : geo->data_entry_offset);
80 16065977 : dotdot_offset = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
81 : 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 16065977 : if (ctx->pos <= dot_offset) {
88 16065928 : ctx->pos = dot_offset & 0x7fffffff;
89 16065928 : 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 16065943 : if (ctx->pos <= dotdot_offset) {
97 16065894 : ino = xfs_dir2_sf_get_parent_ino(sfp);
98 16065879 : ctx->pos = dotdot_offset & 0x7fffffff;
99 16065879 : 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 16066019 : sfep = xfs_dir2_sf_firstentry(sfp);
107 76792253 : for (i = 0; i < sfp->count; i++) {
108 62162490 : uint8_t filetype;
109 :
110 62162490 : off = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
111 : xfs_dir2_sf_get_offset(sfep));
112 :
113 62162490 : if (ctx->pos > off) {
114 2 : sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
115 2 : continue;
116 : }
117 :
118 62162488 : ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
119 62162252 : filetype = xfs_dir2_sf_get_ftype(mp, sfep);
120 62162232 : ctx->pos = off & 0x7fffffff;
121 62162232 : if (XFS_IS_CORRUPT(dp->i_mount,
122 : !xfs_dir2_namecheck(sfep->name,
123 : sfep->namelen))) {
124 4 : xfs_dirattr_mark_sick(dp, XFS_DATA_FORK);
125 4 : return -EFSCORRUPTED;
126 : }
127 124324518 : if (!dir_emit(ctx, (char *)sfep->name, sfep->namelen, ino,
128 : xfs_dir3_get_dtype(mp, filetype)))
129 : return 0;
130 60726047 : sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
131 : }
132 :
133 14629763 : ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) &
134 : 0x7fffffff;
135 14629763 : return 0;
136 : }
137 :
138 : /*
139 : * Readdir for block directories.
140 : */
141 : STATIC int
142 1101966 : xfs_dir2_block_getdents(
143 : struct xfs_da_args *args,
144 : struct dir_context *ctx,
145 : unsigned int *lock_mode)
146 : {
147 1101966 : struct xfs_inode *dp = args->dp; /* incore directory inode */
148 1101966 : struct xfs_buf *bp; /* buffer for block */
149 1101966 : int error; /* error return value */
150 1101966 : int wantoff; /* starting block offset */
151 1101966 : xfs_off_t cook;
152 1101966 : struct xfs_da_geometry *geo = args->geo;
153 1101966 : unsigned int offset, next_offset;
154 1101966 : unsigned int end;
155 :
156 : /*
157 : * If the block number in the offset is out of range, we're done.
158 : */
159 1101966 : if (xfs_dir2_dataptr_to_db(geo, ctx->pos) > geo->datablk)
160 : return 0;
161 :
162 641685 : error = xfs_dir3_block_read(args->trans, dp, args->owner, &bp);
163 641681 : if (error)
164 : return error;
165 :
166 641681 : xfs_iunlock(dp, *lock_mode);
167 641681 : *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 641681 : wantoff = xfs_dir2_dataptr_to_off(geo, ctx->pos);
174 641681 : 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 641671 : end = xfs_dir3_data_end_offset(geo, bp->b_addr);
181 641668 : for (offset = geo->data_entry_offset;
182 34907518 : offset < end;
183 : offset = next_offset) {
184 34438921 : struct xfs_dir2_data_unused *dup = bp->b_addr + offset;
185 34438921 : struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
186 34438921 : uint8_t filetype;
187 :
188 : /*
189 : * Unused, skip it.
190 : */
191 34438921 : if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
192 6555441 : next_offset = offset + be16_to_cpu(dup->length);
193 6555441 : continue;
194 : }
195 :
196 : /*
197 : * Bump pointer for the next iteration.
198 : */
199 55766960 : next_offset = offset +
200 27883480 : xfs_dir2_data_entsize(dp->i_mount, dep->namelen);
201 :
202 : /*
203 : * The entry is before the desired starting point, skip it.
204 : */
205 27883480 : if (offset < wantoff)
206 131959 : continue;
207 :
208 27751521 : cook = xfs_dir2_db_off_to_dataptr(geo, geo->datablk, offset);
209 :
210 27751521 : ctx->pos = cook & 0x7fffffff;
211 27751521 : 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 27802247 : 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 27572842 : if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
223 27572842 : be64_to_cpu(dep->inumber),
224 27572842 : xfs_dir3_get_dtype(dp->i_mount, filetype)))
225 173089 : 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 468597 : ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) &
233 : 0x7fffffff;
234 641686 : out_rele:
235 641686 : xfs_trans_brelse(args->trans, bp);
236 641686 : 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 128781394 : 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 128781394 : struct xfs_inode *dp = args->dp;
253 128781394 : struct xfs_buf *bp = NULL;
254 128781394 : struct xfs_da_geometry *geo = args->geo;
255 128781394 : struct xfs_ifork *ifp = xfs_ifork_ptr(dp, XFS_DATA_FORK);
256 128781394 : struct xfs_bmbt_irec map;
257 128781394 : struct blk_plug plug;
258 128781394 : xfs_dir2_off_t new_off;
259 128781394 : xfs_dablk_t next_ra;
260 128781394 : xfs_dablk_t map_off;
261 128781394 : xfs_dablk_t last_da;
262 128781394 : struct xfs_iext_cursor icur;
263 128781394 : int ra_want;
264 128781394 : int error = 0;
265 :
266 128781394 : error = xfs_iread_extents(args->trans, dp, XFS_DATA_FORK);
267 128799520 : 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 128799520 : last_da = xfs_dir2_byte_to_da(geo, XFS_DIR2_LEAF_OFFSET);
276 128799520 : map_off = xfs_dir2_db_to_da(geo, xfs_dir2_byte_to_db(geo, *cur_off));
277 128799520 : if (!xfs_iext_lookup_extent(dp, ifp, map_off, &icur, &map))
278 0 : goto out;
279 128785954 : if (map.br_startoff >= last_da)
280 11000335 : goto out;
281 117785619 : xfs_trim_extent(&map, map_off, last_da - map_off);
282 :
283 : /* Read the directory block of that first mapping. */
284 117811592 : new_off = xfs_dir2_da_to_byte(geo, map.br_startoff);
285 117811592 : if (new_off > *cur_off)
286 446 : *cur_off = new_off;
287 117811592 : error = xfs_dir3_data_read(args->trans, dp, args->owner,
288 : map.br_startoff, 0, &bp);
289 117809976 : if (error)
290 989 : 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 117808987 : ra_want = howmany(bufsize + geo->blksize, (1 << geo->fsblog));
298 117808987 : if (*ra_blk >= last_da)
299 19398200 : goto out;
300 98410787 : else if (*ra_blk == 0)
301 12206263 : *ra_blk = map.br_startoff;
302 98410787 : next_ra = map.br_startoff + geo->fsbcount;
303 98410787 : if (next_ra >= last_da)
304 0 : goto out_no_ra;
305 98410787 : if (map.br_blockcount < geo->fsbcount &&
306 0 : !xfs_iext_next_extent(ifp, &icur, &map))
307 0 : goto out_no_ra;
308 98410787 : if (map.br_startoff >= last_da)
309 0 : goto out_no_ra;
310 98410787 : 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 98419237 : blk_start_plug(&plug);
314 632158991 : while (ra_want > 0) {
315 539444708 : next_ra = roundup((xfs_dablk_t)map.br_startoff, geo->fsbcount);
316 982027993 : while (ra_want > 0 &&
317 889318900 : next_ra < map.br_startoff + map.br_blockcount) {
318 531142390 : if (next_ra >= last_da) {
319 88518240 : *ra_blk = last_da;
320 88518240 : break;
321 : }
322 442624150 : if (next_ra > *ra_blk) {
323 112261803 : xfs_dir3_data_readahead(dp, next_ra,
324 : XFS_DABUF_MAP_HOLE_OK);
325 112220938 : *ra_blk = next_ra;
326 : }
327 442583285 : ra_want -= geo->fsbcount;
328 442583285 : next_ra += geo->fsbcount;
329 : }
330 539403843 : if (!xfs_iext_next_extent(ifp, &icur, &map)) {
331 5705934 : *ra_blk = last_da;
332 5705934 : break;
333 : }
334 : }
335 98421023 : blk_finish_plug(&plug);
336 :
337 128820387 : out:
338 128820387 : *bpp = bp;
339 128820387 : 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 17725063 : 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 17725063 : struct xfs_inode *dp = args->dp;
357 17725063 : struct xfs_mount *mp = dp->i_mount;
358 17725063 : struct xfs_buf *bp = NULL; /* data block buffer */
359 17725063 : xfs_dir2_data_entry_t *dep; /* data entry */
360 17725063 : xfs_dir2_data_unused_t *dup; /* unused entry */
361 17725063 : struct xfs_da_geometry *geo = args->geo;
362 17725063 : xfs_dablk_t rablk = 0; /* current readahead block */
363 17725063 : xfs_dir2_off_t curoff; /* current overall offset */
364 17725063 : int length; /* temporary length value */
365 17725063 : int byteoff; /* offset in current block */
366 17725063 : unsigned int offset = 0;
367 17725063 : 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 17725063 : 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 17725063 : 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 22584663560 : while (curoff < XFS_DIR2_LEAF_OFFSET) {
387 22584663560 : 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 22584663560 : if (!bp || offset >= geo->blksize) {
394 111508248 : if (bp) {
395 111038602 : xfs_trans_brelse(args->trans, bp);
396 111098467 : bp = NULL;
397 : }
398 :
399 111568113 : if (*lock_mode == 0)
400 111096519 : *lock_mode = xfs_ilock_data_map_shared(dp);
401 111547613 : error = xfs_dir2_leaf_readbuf(args, bufsize, &curoff,
402 : &rablk, &bp);
403 128802120 : if (error || !bp)
404 : break;
405 :
406 117800846 : xfs_iunlock(dp, *lock_mode);
407 117810040 : *lock_mode = 0;
408 :
409 117810040 : xfs_dir3_data_check(dp, bp);
410 : /*
411 : * Find our position in the block.
412 : */
413 117793889 : offset = geo->data_entry_offset;
414 117793889 : byteoff = xfs_dir2_byte_to_off(geo, curoff);
415 : /*
416 : * Skip past the header.
417 : */
418 117793889 : if (byteoff == 0)
419 111139026 : curoff += geo->data_entry_offset;
420 : /*
421 : * Skip past entries until we reach our offset.
422 : */
423 : else {
424 663008784 : while (offset < byteoff) {
425 656353921 : dup = bp->b_addr + offset;
426 :
427 656353921 : if (be16_to_cpu(dup->freetag)
428 656353921 : == XFS_DIR2_DATA_FREE_TAG) {
429 :
430 24163010 : length = be16_to_cpu(dup->length);
431 24163010 : offset += length;
432 24163010 : continue;
433 : }
434 632190911 : dep = bp->b_addr + offset;
435 632190911 : length = xfs_dir2_data_entsize(mp,
436 632190911 : dep->namelen);
437 632190911 : offset += length;
438 : }
439 : /*
440 : * Now set our real offset.
441 : */
442 6654863 : curoff =
443 : xfs_dir2_db_off_to_byte(geo,
444 : xfs_dir2_byte_to_db(geo, curoff),
445 : offset);
446 6654863 : if (offset >= geo->blksize)
447 17599 : continue;
448 : }
449 : }
450 :
451 : /*
452 : * We have a pointer to an entry. Is it a live one?
453 : */
454 22590931602 : dup = bp->b_addr + offset;
455 :
456 : /*
457 : * No, it's unused, skip over it.
458 : */
459 22590931602 : if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
460 1097001680 : length = be16_to_cpu(dup->length);
461 1097001680 : offset += length;
462 1097001680 : curoff += length;
463 1097001680 : continue;
464 : }
465 :
466 21493929922 : dep = bp->b_addr + offset;
467 21493929922 : length = xfs_dir2_data_entsize(mp, dep->namelen);
468 21493929922 : filetype = xfs_dir2_data_get_ftype(mp, dep);
469 :
470 21560419148 : ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
471 21560419148 : 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 21432640027 : if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
479 21432640027 : be64_to_cpu(dep->inumber),
480 21432640027 : xfs_dir3_get_dtype(dp->i_mount, filetype)))
481 : break;
482 :
483 : /*
484 : * Advance to next entry in the block.
485 : */
486 21469919218 : offset += length;
487 21469919218 : curoff += length;
488 : /* bufsize may have just been a guess; don't go negative */
489 21469919218 : bufsize = bufsize > length ? bufsize - length : 0;
490 : }
491 :
492 : /*
493 : * All done. Set output offset value to current offset.
494 : */
495 17725018 : if (curoff > xfs_dir2_dataptr_to_byte(XFS_DIR2_MAX_DATAPTR))
496 0 : ctx->pos = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
497 : else
498 17725018 : ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
499 17725018 : if (bp)
500 6723470 : 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 49460879 : xfs_readdir(
514 : struct xfs_trans *tp,
515 : struct xfs_inode *dp,
516 : struct dir_context *ctx,
517 : size_t bufsize)
518 : {
519 49460879 : struct xfs_da_args args = { NULL };
520 49460879 : unsigned int lock_mode;
521 49460879 : bool isblock;
522 49460879 : int error;
523 :
524 49460879 : trace_xfs_readdir(dp);
525 :
526 98919162 : if (xfs_is_shutdown(dp->i_mount))
527 : return -EIO;
528 :
529 49459515 : ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
530 49459515 : ASSERT(xfs_isilocked(dp, XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
531 49461242 : XFS_STATS_INC(dp->i_mount, xs_dir_getdents);
532 :
533 49461242 : args.dp = dp;
534 49461242 : args.geo = dp->i_mount->m_dir_geo;
535 49461242 : args.trans = tp;
536 49461242 : args.owner = dp->i_ino;
537 :
538 49461242 : if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL)
539 30633899 : return xfs_dir2_sf_getdents(&args, ctx);
540 :
541 18827343 : lock_mode = xfs_ilock_data_map_shared(dp);
542 18827725 : error = xfs_dir2_isblock(&args, &isblock);
543 18828086 : if (error)
544 0 : goto out_unlock;
545 :
546 18828086 : if (isblock) {
547 1102512 : error = xfs_dir2_block_getdents(&args, ctx, &lock_mode);
548 1100392 : goto out_unlock;
549 : }
550 :
551 17725574 : error = xfs_dir2_leaf_getdents(&args, ctx, bufsize, &lock_mode);
552 :
553 18824179 : out_unlock:
554 18824179 : if (lock_mode)
555 11462076 : xfs_iunlock(dp, lock_mode);
556 : return error;
557 : }
|