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 26171006231 : if (filetype >= XFS_DIR3_FT_MAX)
40 : return DT_UNKNOWN;
41 :
42 26171006231 : return xfs_dir3_filetype_table[filetype];
43 : }
44 :
45 : STATIC int
46 57036616 : xfs_dir2_sf_getdents(
47 : struct xfs_da_args *args,
48 : struct dir_context *ctx)
49 : {
50 57036616 : int i; /* shortform entry number */
51 57036616 : struct xfs_inode *dp = args->dp; /* incore directory inode */
52 57036616 : struct xfs_mount *mp = dp->i_mount;
53 57036616 : xfs_dir2_dataptr_t off; /* current entry's offset */
54 57036616 : xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
55 57036616 : xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
56 57036616 : xfs_dir2_dataptr_t dot_offset;
57 57036616 : xfs_dir2_dataptr_t dotdot_offset;
58 57036616 : xfs_ino_t ino;
59 57036616 : struct xfs_da_geometry *geo = args->geo;
60 :
61 57036616 : ASSERT(dp->i_df.if_format == XFS_DINODE_FMT_LOCAL);
62 57036616 : ASSERT(dp->i_df.if_bytes == dp->i_disk_size);
63 57036616 : ASSERT(dp->i_df.if_u1.if_data != NULL);
64 :
65 57036616 : 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 57036616 : 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 30307899 : dot_offset = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
79 30307899 : geo->data_entry_offset);
80 30272878 : dotdot_offset = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
81 30272878 : 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 30251708 : if (ctx->pos <= dot_offset) {
88 30275775 : ctx->pos = dot_offset & 0x7fffffff;
89 30275775 : 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 30199026 : if (ctx->pos <= dotdot_offset) {
97 30195314 : ino = xfs_dir2_sf_get_parent_ino(sfp);
98 30364044 : ctx->pos = dotdot_offset & 0x7fffffff;
99 30364044 : 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 30336417 : sfep = xfs_dir2_sf_firstentry(sfp);
107 151253265 : for (i = 0; i < sfp->count; i++) {
108 124126396 : uint8_t filetype;
109 :
110 124126396 : off = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
111 : xfs_dir2_sf_get_offset(sfep));
112 :
113 124197514 : if (ctx->pos > off) {
114 11 : sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
115 11 : continue;
116 : }
117 :
118 124197503 : ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
119 124125880 : filetype = xfs_dir2_sf_get_ftype(mp, sfep);
120 124187421 : ctx->pos = off & 0x7fffffff;
121 124187421 : 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 124215953 : if (!dir_emit(ctx, (char *)sfep->name, sfep->namelen, ino,
128 124224927 : xfs_dir3_get_dtype(mp, filetype)))
129 : return 0;
130 120933799 : sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
131 : }
132 :
133 27126869 : ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) &
134 : 0x7fffffff;
135 27124976 : return 0;
136 : }
137 :
138 : /*
139 : * Readdir for block directories.
140 : */
141 : STATIC int
142 2214345 : xfs_dir2_block_getdents(
143 : struct xfs_da_args *args,
144 : struct dir_context *ctx,
145 : unsigned int *lock_mode)
146 : {
147 2214345 : struct xfs_inode *dp = args->dp; /* incore directory inode */
148 2214345 : struct xfs_buf *bp; /* buffer for block */
149 2214345 : int error; /* error return value */
150 2214345 : int wantoff; /* starting block offset */
151 2214345 : xfs_off_t cook;
152 2214345 : struct xfs_da_geometry *geo = args->geo;
153 2214345 : unsigned int offset, next_offset;
154 2214345 : unsigned int end;
155 :
156 : /*
157 : * If the block number in the offset is out of range, we're done.
158 : */
159 2214345 : if (xfs_dir2_dataptr_to_db(geo, ctx->pos) > geo->datablk)
160 : return 0;
161 :
162 1247861 : error = xfs_dir3_block_read(args->trans, dp, args->owner, &bp);
163 1255004 : if (error)
164 : return error;
165 :
166 1255039 : xfs_iunlock(dp, *lock_mode);
167 1252761 : *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 1252761 : wantoff = xfs_dir2_dataptr_to_off(geo, ctx->pos);
174 1252761 : 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 1262570 : end = xfs_dir3_data_end_offset(geo, bp->b_addr);
181 1262195 : for (offset = geo->data_entry_offset;
182 74591767 : offset < end;
183 : offset = next_offset) {
184 73594189 : struct xfs_dir2_data_unused *dup = bp->b_addr + offset;
185 73594189 : struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
186 73594189 : uint8_t filetype;
187 :
188 : /*
189 : * Unused, skip it.
190 : */
191 73594189 : if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
192 13082341 : next_offset = offset + be16_to_cpu(dup->length);
193 13082341 : continue;
194 : }
195 :
196 : /*
197 : * Bump pointer for the next iteration.
198 : */
199 121023696 : next_offset = offset +
200 60511848 : xfs_dir2_data_entsize(dp->i_mount, dep->namelen);
201 :
202 : /*
203 : * The entry is before the desired starting point, skip it.
204 : */
205 60511848 : if (offset < wantoff)
206 738740 : continue;
207 :
208 59773108 : cook = xfs_dir2_db_off_to_dataptr(geo, geo->datablk, offset);
209 :
210 59242677 : ctx->pos = cook & 0x7fffffff;
211 59242677 : 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 59308224 : 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 59773488 : if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
223 59097203 : be64_to_cpu(dep->inumber),
224 59137411 : xfs_dir3_get_dtype(dp->i_mount, filetype)))
225 264997 : 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 997578 : ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) &
233 : 0x7fffffff;
234 1262527 : out_rele:
235 1262527 : xfs_trans_brelse(args->trans, bp);
236 1262527 : 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 177376162 : 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 177376162 : struct xfs_inode *dp = args->dp;
253 177376162 : struct xfs_buf *bp = NULL;
254 177376162 : struct xfs_da_geometry *geo = args->geo;
255 177376162 : struct xfs_ifork *ifp = xfs_ifork_ptr(dp, XFS_DATA_FORK);
256 177376162 : struct xfs_bmbt_irec map;
257 177376162 : struct blk_plug plug;
258 177376162 : xfs_dir2_off_t new_off;
259 177376162 : xfs_dablk_t next_ra;
260 177376162 : xfs_dablk_t map_off;
261 177376162 : xfs_dablk_t last_da;
262 177376162 : struct xfs_iext_cursor icur;
263 177376162 : int ra_want;
264 177376162 : int error = 0;
265 :
266 177376162 : error = xfs_iread_extents(args->trans, dp, XFS_DATA_FORK);
267 177308180 : 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 177308180 : last_da = xfs_dir2_byte_to_da(geo, XFS_DIR2_LEAF_OFFSET);
276 177282194 : map_off = xfs_dir2_db_to_da(geo, xfs_dir2_byte_to_db(geo, *cur_off));
277 177275561 : if (!xfs_iext_lookup_extent(dp, ifp, map_off, &icur, &map))
278 0 : goto out;
279 177349275 : if (map.br_startoff >= last_da)
280 18539204 : goto out;
281 158810071 : xfs_trim_extent(&map, map_off, last_da - map_off);
282 :
283 : /* Read the directory block of that first mapping. */
284 158782956 : new_off = xfs_dir2_da_to_byte(geo, map.br_startoff);
285 158756522 : if (new_off > *cur_off)
286 1841 : *cur_off = new_off;
287 158756522 : error = xfs_dir3_data_read(args->trans, dp, args->owner,
288 158756522 : map.br_startoff, 0, &bp);
289 158762771 : if (error)
290 428 : 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 158762343 : ra_want = howmany(bufsize + geo->blksize, (1 << geo->fsblog));
298 158762343 : if (*ra_blk >= last_da)
299 40585578 : goto out;
300 118176765 : else if (*ra_blk == 0)
301 28034100 : *ra_blk = map.br_startoff;
302 118176765 : next_ra = map.br_startoff + geo->fsbcount;
303 118176765 : if (next_ra >= last_da)
304 0 : goto out_no_ra;
305 118176830 : if (map.br_blockcount < geo->fsbcount &&
306 65 : !xfs_iext_next_extent(ifp, &icur, &map))
307 0 : goto out_no_ra;
308 118176765 : if (map.br_startoff >= last_da)
309 0 : goto out_no_ra;
310 118176765 : 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 118144347 : blk_start_plug(&plug);
314 1003254070 : while (ra_want > 0) {
315 897740298 : next_ra = roundup((xfs_dablk_t)map.br_startoff, geo->fsbcount);
316 1679092619 : while (ra_want > 0 &&
317 1573577730 : next_ra < map.br_startoff + map.br_blockcount) {
318 946034504 : if (next_ra >= last_da) {
319 165756253 : *ra_blk = last_da;
320 165756253 : break;
321 : }
322 780278251 : if (next_ra > *ra_blk) {
323 203272294 : xfs_dir3_data_readahead(dp, next_ra,
324 : XFS_DABUF_MAP_HOLE_OK);
325 204346364 : *ra_blk = next_ra;
326 : }
327 781352321 : ra_want -= geo->fsbcount;
328 781352321 : next_ra += geo->fsbcount;
329 : }
330 898814368 : if (!xfs_iext_next_extent(ifp, &icur, &map)) {
331 12750375 : *ra_blk = last_da;
332 12750375 : break;
333 : }
334 : }
335 118254658 : blk_finish_plug(&plug);
336 :
337 177368217 : out:
338 177368217 : *bpp = bp;
339 177368217 : 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 37603700 : 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 37603700 : struct xfs_inode *dp = args->dp;
357 37603700 : struct xfs_mount *mp = dp->i_mount;
358 37603700 : struct xfs_buf *bp = NULL; /* data block buffer */
359 37603700 : xfs_dir2_data_entry_t *dep; /* data entry */
360 37603700 : xfs_dir2_data_unused_t *dup; /* unused entry */
361 37603700 : struct xfs_da_geometry *geo = args->geo;
362 37603700 : xfs_dablk_t rablk = 0; /* current readahead block */
363 37603700 : xfs_dir2_off_t curoff; /* current overall offset */
364 37603700 : int length; /* temporary length value */
365 37603700 : int byteoff; /* offset in current block */
366 37603700 : unsigned int offset = 0;
367 37603700 : 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 37603700 : 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 37603700 : 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 28105794699 : while (curoff < XFS_DIR2_LEAF_OFFSET) {
387 28105794699 : 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 28105794699 : if (!bp || offset >= geo->blksize) {
394 177383380 : if (bp) {
395 139785707 : xfs_trans_brelse(args->trans, bp);
396 139793859 : bp = NULL;
397 : }
398 :
399 177391532 : if (*lock_mode == 0)
400 139797126 : *lock_mode = xfs_ilock_data_map_shared(dp);
401 177336200 : error = xfs_dir2_leaf_readbuf(args, bufsize, &curoff,
402 : &rablk, &bp);
403 177377126 : if (error || !bp)
404 : break;
405 :
406 158836697 : xfs_iunlock(dp, *lock_mode);
407 158579665 : *lock_mode = 0;
408 :
409 158579665 : xfs_dir3_data_check(dp, bp);
410 : /*
411 : * Find our position in the block.
412 : */
413 158850266 : offset = geo->data_entry_offset;
414 158850266 : byteoff = xfs_dir2_byte_to_off(geo, curoff);
415 : /*
416 : * Skip past the header.
417 : */
418 158850266 : if (byteoff == 0)
419 140371995 : curoff += geo->data_entry_offset;
420 : /*
421 : * Skip past entries until we reach our offset.
422 : */
423 : else {
424 1850330346 : while (offset < byteoff) {
425 1831852075 : dup = bp->b_addr + offset;
426 :
427 1831852075 : if (be16_to_cpu(dup->freetag)
428 : == XFS_DIR2_DATA_FREE_TAG) {
429 :
430 64361609 : length = be16_to_cpu(dup->length);
431 64361609 : offset += length;
432 64361609 : continue;
433 : }
434 1767490466 : dep = bp->b_addr + offset;
435 1767490466 : length = xfs_dir2_data_entsize(mp,
436 1767490466 : dep->namelen);
437 1767490466 : offset += length;
438 : }
439 : /*
440 : * Now set our real offset.
441 : */
442 36955908 : curoff =
443 18478271 : xfs_dir2_db_off_to_byte(geo,
444 : xfs_dir2_byte_to_db(geo, curoff),
445 : offset);
446 18477637 : if (offset >= geo->blksize)
447 207515 : continue;
448 : }
449 : }
450 :
451 : /*
452 : * We have a pointer to an entry. Is it a live one?
453 : */
454 28087053436 : dup = bp->b_addr + offset;
455 :
456 : /*
457 : * No, it's unused, skip over it.
458 : */
459 28087053436 : if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
460 1295261383 : length = be16_to_cpu(dup->length);
461 1295261383 : offset += length;
462 1295261383 : curoff += length;
463 1295261383 : continue;
464 : }
465 :
466 26791792053 : dep = bp->b_addr + offset;
467 26791792053 : length = xfs_dir2_data_entsize(mp, dep->namelen);
468 26791792053 : filetype = xfs_dir2_data_get_ftype(mp, dep);
469 :
470 25721780667 : ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
471 25721780667 : 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 26791799062 : if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
479 25955242235 : be64_to_cpu(dep->inumber),
480 25987643893 : xfs_dir3_get_dtype(dp->i_mount, filetype)))
481 : break;
482 :
483 : /*
484 : * Advance to next entry in the block.
485 : */
486 26772722101 : offset += length;
487 26772722101 : curoff += length;
488 : /* bufsize may have just been a guess; don't go negative */
489 26772722101 : bufsize = bufsize > length ? bufsize - length : 0;
490 : }
491 :
492 : /*
493 : * All done. Set output offset value to current offset.
494 : */
495 37615395 : if (curoff > xfs_dir2_dataptr_to_byte(XFS_DIR2_MAX_DATAPTR))
496 0 : ctx->pos = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
497 : else
498 37615395 : ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
499 37615395 : if (bp)
500 19077122 : 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 96840015 : xfs_readdir(
514 : struct xfs_trans *tp,
515 : struct xfs_inode *dp,
516 : struct dir_context *ctx,
517 : size_t bufsize)
518 : {
519 96840015 : struct xfs_da_args args = { NULL };
520 96840015 : unsigned int lock_mode;
521 96840015 : bool isblock;
522 96840015 : int error;
523 :
524 96840015 : trace_xfs_readdir(dp);
525 :
526 193575652 : if (xfs_is_shutdown(dp->i_mount))
527 : return -EIO;
528 :
529 96787753 : ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
530 96787753 : ASSERT(xfs_isilocked(dp, XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
531 96676376 : XFS_STATS_INC(dp->i_mount, xs_dir_getdents);
532 :
533 96799851 : args.dp = dp;
534 96799851 : args.geo = dp->i_mount->m_dir_geo;
535 96799851 : args.trans = tp;
536 96799851 : args.owner = dp->i_ino;
537 :
538 96799851 : if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL)
539 57001703 : return xfs_dir2_sf_getdents(&args, ctx);
540 :
541 39798148 : lock_mode = xfs_ilock_data_map_shared(dp);
542 39803106 : error = xfs_dir2_isblock(&args, &isblock);
543 39823074 : if (error)
544 0 : goto out_unlock;
545 :
546 39823074 : if (isblock) {
547 2216622 : error = xfs_dir2_block_getdents(&args, ctx, &lock_mode);
548 2220381 : goto out_unlock;
549 : }
550 :
551 37606452 : error = xfs_dir2_leaf_getdents(&args, ctx, bufsize, &lock_mode);
552 :
553 39829813 : out_unlock:
554 39829813 : if (lock_mode)
555 19489343 : xfs_iunlock(dp, lock_mode);
556 : return error;
557 : }
|