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 24995144396 : if (filetype >= XFS_DIR3_FT_MAX)
40 : return DT_UNKNOWN;
41 :
42 24995144396 : return xfs_dir3_filetype_table[filetype];
43 : }
44 :
45 : STATIC int
46 34112738 : xfs_dir2_sf_getdents(
47 : struct xfs_da_args *args,
48 : struct dir_context *ctx)
49 : {
50 34112738 : int i; /* shortform entry number */
51 34112738 : struct xfs_inode *dp = args->dp; /* incore directory inode */
52 34112738 : struct xfs_mount *mp = dp->i_mount;
53 34112738 : xfs_dir2_dataptr_t off; /* current entry's offset */
54 34112738 : xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
55 34112738 : xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
56 34112738 : xfs_dir2_dataptr_t dot_offset;
57 34112738 : xfs_dir2_dataptr_t dotdot_offset;
58 34112738 : xfs_ino_t ino;
59 34112738 : struct xfs_da_geometry *geo = args->geo;
60 :
61 34112738 : ASSERT(dp->i_df.if_format == XFS_DINODE_FMT_LOCAL);
62 34112738 : ASSERT(dp->i_df.if_bytes == dp->i_disk_size);
63 34112738 : ASSERT(dp->i_df.if_u1.if_data != NULL);
64 :
65 34112738 : 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 34112738 : 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 18322267 : dot_offset = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
79 18322267 : geo->data_entry_offset);
80 18322267 : 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 18322267 : if (ctx->pos <= dot_offset) {
88 18322206 : ctx->pos = dot_offset & 0x7fffffff;
89 18322206 : 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 18322192 : if (ctx->pos <= dotdot_offset) {
97 18322187 : ino = xfs_dir2_sf_get_parent_ino(sfp);
98 18322071 : ctx->pos = dotdot_offset & 0x7fffffff;
99 18322071 : 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 18322429 : sfep = xfs_dir2_sf_firstentry(sfp);
107 88751116 : for (i = 0; i < sfp->count; i++) {
108 72902162 : uint8_t filetype;
109 :
110 72902162 : off = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
111 : xfs_dir2_sf_get_offset(sfep));
112 :
113 72902162 : if (ctx->pos > off) {
114 2 : sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
115 2 : continue;
116 : }
117 :
118 72902160 : ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
119 72902634 : filetype = xfs_dir2_sf_get_ftype(mp, sfep);
120 72902462 : ctx->pos = off & 0x7fffffff;
121 72902462 : 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 145803222 : if (!dir_emit(ctx, (char *)sfep->name, sfep->namelen, ino,
128 : xfs_dir3_get_dtype(mp, filetype)))
129 : return 0;
130 70429269 : sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
131 : }
132 :
133 15848954 : ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) &
134 : 0x7fffffff;
135 15848954 : return 0;
136 : }
137 :
138 : /*
139 : * Readdir for block directories.
140 : */
141 : STATIC int
142 1327929 : xfs_dir2_block_getdents(
143 : struct xfs_da_args *args,
144 : struct dir_context *ctx,
145 : unsigned int *lock_mode)
146 : {
147 1327929 : struct xfs_inode *dp = args->dp; /* incore directory inode */
148 1327929 : struct xfs_buf *bp; /* buffer for block */
149 1327929 : int error; /* error return value */
150 1327929 : int wantoff; /* starting block offset */
151 1327929 : xfs_off_t cook;
152 1327929 : struct xfs_da_geometry *geo = args->geo;
153 1327929 : unsigned int offset, next_offset;
154 1327929 : unsigned int end;
155 :
156 : /*
157 : * If the block number in the offset is out of range, we're done.
158 : */
159 1327929 : if (xfs_dir2_dataptr_to_db(geo, ctx->pos) > geo->datablk)
160 : return 0;
161 :
162 827483 : error = xfs_dir3_block_read(args->trans, dp, args->owner, &bp);
163 827488 : if (error)
164 : return error;
165 :
166 827324 : xfs_iunlock(dp, *lock_mode);
167 827487 : *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 827487 : wantoff = xfs_dir2_dataptr_to_off(geo, ctx->pos);
174 827487 : 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 827232 : end = xfs_dir3_data_end_offset(geo, bp->b_addr);
181 827444 : for (offset = geo->data_entry_offset;
182 38942419 : offset < end;
183 : offset = next_offset) {
184 38433738 : struct xfs_dir2_data_unused *dup = bp->b_addr + offset;
185 38433738 : struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
186 38433738 : uint8_t filetype;
187 :
188 : /*
189 : * Unused, skip it.
190 : */
191 38433738 : if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
192 7586289 : next_offset = offset + be16_to_cpu(dup->length);
193 7586289 : continue;
194 : }
195 :
196 : /*
197 : * Bump pointer for the next iteration.
198 : */
199 61694898 : next_offset = offset +
200 30847449 : xfs_dir2_data_entsize(dp->i_mount, dep->namelen);
201 :
202 : /*
203 : * The entry is before the desired starting point, skip it.
204 : */
205 30847449 : if (offset < wantoff)
206 131959 : continue;
207 :
208 30715490 : cook = xfs_dir2_db_off_to_dataptr(geo, geo->datablk, offset);
209 :
210 30715490 : ctx->pos = cook & 0x7fffffff;
211 30715490 : 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 31135689 : 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 31119486 : if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
223 31119486 : be64_to_cpu(dep->inumber),
224 31119486 : xfs_dir3_get_dtype(dp->i_mount, filetype)))
225 318806 : 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 508681 : ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) &
233 : 0x7fffffff;
234 827487 : out_rele:
235 827487 : xfs_trans_brelse(args->trans, bp);
236 827487 : 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 150955211 : 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 150955211 : struct xfs_inode *dp = args->dp;
253 150955211 : struct xfs_buf *bp = NULL;
254 150955211 : struct xfs_da_geometry *geo = args->geo;
255 150955211 : struct xfs_ifork *ifp = xfs_ifork_ptr(dp, XFS_DATA_FORK);
256 150955211 : struct xfs_bmbt_irec map;
257 150955211 : struct blk_plug plug;
258 150955211 : xfs_dir2_off_t new_off;
259 150955211 : xfs_dablk_t next_ra;
260 150955211 : xfs_dablk_t map_off;
261 150955211 : xfs_dablk_t last_da;
262 150955211 : struct xfs_iext_cursor icur;
263 150955211 : int ra_want;
264 150955211 : int error = 0;
265 :
266 150955211 : error = xfs_iread_extents(args->trans, dp, XFS_DATA_FORK);
267 150988645 : 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 150988645 : last_da = xfs_dir2_byte_to_da(geo, XFS_DIR2_LEAF_OFFSET);
276 150988645 : map_off = xfs_dir2_db_to_da(geo, xfs_dir2_byte_to_db(geo, *cur_off));
277 150988645 : if (!xfs_iext_lookup_extent(dp, ifp, map_off, &icur, &map))
278 0 : goto out;
279 150983261 : if (map.br_startoff >= last_da)
280 11921720 : goto out;
281 139061541 : xfs_trim_extent(&map, map_off, last_da - map_off);
282 :
283 : /* Read the directory block of that first mapping. */
284 139079796 : new_off = xfs_dir2_da_to_byte(geo, map.br_startoff);
285 139079796 : if (new_off > *cur_off)
286 514 : *cur_off = new_off;
287 139079796 : error = xfs_dir3_data_read(args->trans, dp, args->owner,
288 : map.br_startoff, 0, &bp);
289 139080825 : if (error)
290 1747 : 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 139079078 : ra_want = howmany(bufsize + geo->blksize, (1 << geo->fsblog));
298 139079078 : if (*ra_blk >= last_da)
299 21379467 : goto out;
300 117699611 : else if (*ra_blk == 0)
301 13978701 : *ra_blk = map.br_startoff;
302 117699611 : next_ra = map.br_startoff + geo->fsbcount;
303 117699611 : if (next_ra >= last_da)
304 0 : goto out_no_ra;
305 117699611 : if (map.br_blockcount < geo->fsbcount &&
306 0 : !xfs_iext_next_extent(ifp, &icur, &map))
307 0 : goto out_no_ra;
308 117699611 : if (map.br_startoff >= last_da)
309 0 : goto out_no_ra;
310 117699611 : 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 117708289 : blk_start_plug(&plug);
314 760041232 : while (ra_want > 0) {
315 648531694 : next_ra = roundup((xfs_dablk_t)map.br_startoff, geo->fsbcount);
316 1176951017 : while (ra_want > 0 &&
317 1065473993 : next_ra < map.br_startoff + map.br_blockcount) {
318 632737834 : if (next_ra >= last_da) {
319 104454526 : *ra_blk = last_da;
320 104454526 : break;
321 : }
322 528283308 : if (next_ra > *ra_blk) {
323 133020531 : xfs_dir3_data_readahead(dp, next_ra,
324 : XFS_DABUF_MAP_HOLE_OK);
325 133156546 : *ra_blk = next_ra;
326 : }
327 528419323 : ra_want -= geo->fsbcount;
328 528419323 : next_ra += geo->fsbcount;
329 : }
330 648667709 : if (!xfs_iext_next_extent(ifp, &icur, &map)) {
331 6223972 : *ra_blk = last_da;
332 6223972 : break;
333 : }
334 : }
335 117707226 : blk_finish_plug(&plug);
336 :
337 151010755 : out:
338 151010755 : *bpp = bp;
339 151010755 : 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 19953801 : 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 19953801 : struct xfs_inode *dp = args->dp;
357 19953801 : struct xfs_mount *mp = dp->i_mount;
358 19953801 : struct xfs_buf *bp = NULL; /* data block buffer */
359 19953801 : xfs_dir2_data_entry_t *dep; /* data entry */
360 19953801 : xfs_dir2_data_unused_t *dup; /* unused entry */
361 19953801 : struct xfs_da_geometry *geo = args->geo;
362 19953801 : xfs_dablk_t rablk = 0; /* current readahead block */
363 19953801 : xfs_dir2_off_t curoff; /* current overall offset */
364 19953801 : int length; /* temporary length value */
365 19953801 : int byteoff; /* offset in current block */
366 19953801 : unsigned int offset = 0;
367 19953801 : 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 19953801 : 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 19953801 : 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 24864118453 : while (curoff < XFS_DIR2_LEAF_OFFSET) {
387 24864118453 : 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 24864118453 : if (!bp || offset >= geo->blksize) {
394 697813 : if (bp) {
395 131008607 : xfs_trans_brelse(args->trans, bp);
396 131059726 : bp = NULL;
397 : }
398 :
399 697813 : if (*lock_mode == 0)
400 131058997 : *lock_mode = xfs_ilock_data_map_shared(dp);
401 697813 : error = xfs_dir2_leaf_readbuf(args, bufsize, &curoff,
402 : &rablk, &bp);
403 150990810 : if (error || !bp)
404 : break;
405 :
406 139067665 : xfs_iunlock(dp, *lock_mode);
407 139077339 : *lock_mode = 0;
408 :
409 139077339 : xfs_dir3_data_check(dp, bp);
410 : /*
411 : * Find our position in the block.
412 : */
413 139030576 : offset = geo->data_entry_offset;
414 139030576 : byteoff = xfs_dir2_byte_to_off(geo, curoff);
415 : /*
416 : * Skip past the header.
417 : */
418 139030576 : if (byteoff == 0)
419 131079839 : curoff += geo->data_entry_offset;
420 : /*
421 : * Skip past entries until we reach our offset.
422 : */
423 : else {
424 774010849 : while (offset < byteoff) {
425 766060112 : dup = bp->b_addr + offset;
426 :
427 766060112 : if (be16_to_cpu(dup->freetag)
428 766060112 : == XFS_DIR2_DATA_FREE_TAG) {
429 :
430 23758744 : length = be16_to_cpu(dup->length);
431 23758744 : offset += length;
432 23758744 : continue;
433 : }
434 742301368 : dep = bp->b_addr + offset;
435 742301368 : length = xfs_dir2_data_entsize(mp,
436 742301368 : dep->namelen);
437 742301368 : offset += length;
438 : }
439 : /*
440 : * Now set our real offset.
441 : */
442 7950737 : curoff =
443 : xfs_dir2_db_off_to_byte(geo,
444 : xfs_dir2_byte_to_db(geo, curoff),
445 : offset);
446 7950737 : if (offset >= geo->blksize)
447 23448 : continue;
448 : }
449 : }
450 :
451 : /*
452 : * We have a pointer to an entry. Is it a live one?
453 : */
454 26326377255 : dup = bp->b_addr + offset;
455 :
456 : /*
457 : * No, it's unused, skip over it.
458 : */
459 26326377255 : if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
460 1448566199 : length = be16_to_cpu(dup->length);
461 1448566199 : offset += length;
462 1448566199 : curoff += length;
463 1448566199 : continue;
464 : }
465 :
466 24877811056 : dep = bp->b_addr + offset;
467 24877811056 : length = xfs_dir2_data_entsize(mp, dep->namelen);
468 24877811056 : filetype = xfs_dir2_data_get_ftype(mp, dep);
469 :
470 24957364942 : ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
471 24957364942 : 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 24807210244 : if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
479 24807210244 : be64_to_cpu(dep->inumber),
480 24807210244 : xfs_dir3_get_dtype(dp->i_mount, filetype)))
481 : break;
482 :
483 : /*
484 : * Advance to next entry in the block.
485 : */
486 23395575005 : offset += length;
487 23395575005 : curoff += length;
488 : /* bufsize may have just been a guess; don't go negative */
489 23395575005 : bufsize = bufsize > length ? bufsize - length : 0;
490 : }
491 :
492 : /*
493 : * All done. Set output offset value to current offset.
494 : */
495 19954994 : if (curoff > xfs_dir2_dataptr_to_byte(XFS_DIR2_MAX_DATAPTR))
496 0 : ctx->pos = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
497 : else
498 19954994 : ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
499 19954994 : if (bp)
500 8031563 : 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 55395951 : xfs_readdir(
514 : struct xfs_trans *tp,
515 : struct xfs_inode *dp,
516 : struct dir_context *ctx,
517 : size_t bufsize)
518 : {
519 55395951 : struct xfs_da_args args = { NULL };
520 55395951 : unsigned int lock_mode;
521 55395951 : bool isblock;
522 55395951 : int error;
523 :
524 55395951 : trace_xfs_readdir(dp);
525 :
526 110789024 : if (xfs_is_shutdown(dp->i_mount))
527 : return -EIO;
528 :
529 55394457 : ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
530 55394457 : ASSERT(xfs_isilocked(dp, XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
531 55396074 : XFS_STATS_INC(dp->i_mount, xs_dir_getdents);
532 :
533 55396074 : args.dp = dp;
534 55396074 : args.geo = dp->i_mount->m_dir_geo;
535 55396074 : args.trans = tp;
536 55396074 : args.owner = dp->i_ino;
537 :
538 55396074 : if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL)
539 34112717 : return xfs_dir2_sf_getdents(&args, ctx);
540 :
541 21283357 : lock_mode = xfs_ilock_data_map_shared(dp);
542 21283683 : error = xfs_dir2_isblock(&args, &isblock);
543 21284028 : if (error)
544 0 : goto out_unlock;
545 :
546 21284028 : if (isblock) {
547 1328693 : error = xfs_dir2_block_getdents(&args, ctx, &lock_mode);
548 1328453 : goto out_unlock;
549 : }
550 :
551 19955335 : error = xfs_dir2_leaf_getdents(&args, ctx, bufsize, &lock_mode);
552 :
553 21282030 : out_unlock:
554 21282030 : if (lock_mode)
555 12424493 : xfs_iunlock(dp, lock_mode);
556 : return error;
557 : }
|