1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_mount.h"
12 #include "xfs_log_format.h"
13 #include "xfs_trans.h"
14 #include "xfs_inode.h"
15 #include "xfs_icache.h"
17 #include "xfs_dir2_priv.h"
18 #include "scrub/scrub.h"
19 #include "scrub/common.h"
20 #include "scrub/dabtree.h"
22 /* Set us up to scrub directories. */
28 return xchk_setup_inode_contents(sc
, ip
, 0);
33 /* Scrub a directory entry. */
36 /* VFS fill-directory iterator */
37 struct dir_context dir_iter
;
42 /* Check that an inode's mode matches a given DT_ type. */
45 struct xchk_dir_ctx
*sdc
,
50 struct xfs_mount
*mp
= sdc
->sc
->mp
;
55 if (!xfs_sb_version_hasftype(&mp
->m_sb
)) {
56 if (dtype
!= DT_UNKNOWN
&& dtype
!= DT_DIR
)
57 xchk_fblock_set_corrupt(sdc
->sc
, XFS_DATA_FORK
,
63 * Grab the inode pointed to by the dirent. We release the
64 * inode before we cancel the scrub transaction. Since we're
65 * don't know a priori that releasing the inode won't trigger
66 * eofblocks cleanup (which allocates what would be a nested
67 * transaction), we can't use DONTCACHE here because DONTCACHE
68 * inodes can trigger immediate inactive cleanup of the inode.
70 * If _iget returns -EINVAL or -ENOENT then the child inode number is
71 * garbage and the directory is corrupt. If the _iget returns
72 * -EFSCORRUPTED or -EFSBADCRC then the child is corrupt which is a
73 * cross referencing error. Any other error is an operational error.
75 error
= xfs_iget(mp
, sdc
->sc
->tp
, inum
, 0, 0, &ip
);
76 if (error
== -EINVAL
|| error
== -ENOENT
) {
77 error
= -EFSCORRUPTED
;
78 xchk_fblock_process_error(sdc
->sc
, XFS_DATA_FORK
, 0, &error
);
81 if (!xchk_fblock_xref_process_error(sdc
->sc
, XFS_DATA_FORK
, offset
,
85 /* Convert mode to the DT_* values that dir_emit uses. */
86 ino_dtype
= xfs_dir3_get_dtype(mp
,
87 xfs_mode_to_ftype(VFS_I(ip
)->i_mode
));
88 if (ino_dtype
!= dtype
)
89 xchk_fblock_set_corrupt(sdc
->sc
, XFS_DATA_FORK
, offset
);
96 * Scrub a single directory entry.
98 * We use the VFS directory iterator (i.e. readdir) to call this
99 * function for every directory entry in a directory. Once we're here,
100 * we check the inode number to make sure it's sane, then we check that
101 * we can look up this filename. Finally, we check the ftype.
105 struct dir_context
*dir_iter
,
112 struct xfs_mount
*mp
;
113 struct xfs_inode
*ip
;
114 struct xchk_dir_ctx
*sdc
;
115 struct xfs_name xname
;
116 xfs_ino_t lookup_ino
;
118 bool checked_ftype
= false;
121 sdc
= container_of(dir_iter
, struct xchk_dir_ctx
, dir_iter
);
124 offset
= xfs_dir2_db_to_da(mp
->m_dir_geo
,
125 xfs_dir2_dataptr_to_db(mp
->m_dir_geo
, pos
));
127 if (xchk_should_terminate(sdc
->sc
, &error
))
130 /* Does this inode number make sense? */
131 if (!xfs_verify_dir_ino(mp
, ino
)) {
132 xchk_fblock_set_corrupt(sdc
->sc
, XFS_DATA_FORK
, offset
);
136 /* Does this name make sense? */
137 if (!xfs_dir2_namecheck(name
, namelen
)) {
138 xchk_fblock_set_corrupt(sdc
->sc
, XFS_DATA_FORK
, offset
);
142 if (!strncmp(".", name
, namelen
)) {
143 /* If this is "." then check that the inum matches the dir. */
144 if (xfs_sb_version_hasftype(&mp
->m_sb
) && type
!= DT_DIR
)
145 xchk_fblock_set_corrupt(sdc
->sc
, XFS_DATA_FORK
,
147 checked_ftype
= true;
148 if (ino
!= ip
->i_ino
)
149 xchk_fblock_set_corrupt(sdc
->sc
, XFS_DATA_FORK
,
151 } else if (!strncmp("..", name
, namelen
)) {
153 * If this is ".." in the root inode, check that the inum
156 if (xfs_sb_version_hasftype(&mp
->m_sb
) && type
!= DT_DIR
)
157 xchk_fblock_set_corrupt(sdc
->sc
, XFS_DATA_FORK
,
159 checked_ftype
= true;
160 if (ip
->i_ino
== mp
->m_sb
.sb_rootino
&& ino
!= ip
->i_ino
)
161 xchk_fblock_set_corrupt(sdc
->sc
, XFS_DATA_FORK
,
165 /* Verify that we can look up this name by hash. */
168 xname
.type
= XFS_DIR3_FT_UNKNOWN
;
170 error
= xfs_dir_lookup(sdc
->sc
->tp
, ip
, &xname
, &lookup_ino
, NULL
);
171 /* ENOENT means the hash lookup failed and the dir is corrupt */
172 if (error
== -ENOENT
)
173 error
= -EFSCORRUPTED
;
174 if (!xchk_fblock_process_error(sdc
->sc
, XFS_DATA_FORK
, offset
,
177 if (lookup_ino
!= ino
) {
178 xchk_fblock_set_corrupt(sdc
->sc
, XFS_DATA_FORK
, offset
);
182 /* Verify the file type. This function absorbs error codes. */
183 if (!checked_ftype
) {
184 error
= xchk_dir_check_ftype(sdc
, offset
, lookup_ino
, type
);
190 * A negative error code returned here is supposed to cause the
191 * dir_emit caller (xfs_readdir) to abort the directory iteration
192 * and return zero to xchk_directory.
194 if (error
== 0 && sdc
->sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
195 return -EFSCORRUPTED
;
199 /* Scrub a directory btree record. */
202 struct xchk_da_btree
*ds
,
205 struct xfs_da_state_blk
*blk
= &ds
->state
->path
.blk
[level
];
206 struct xfs_mount
*mp
= ds
->state
->mp
;
207 struct xfs_inode
*dp
= ds
->dargs
.dp
;
208 struct xfs_da_geometry
*geo
= mp
->m_dir_geo
;
209 struct xfs_dir2_data_entry
*dent
;
211 struct xfs_dir2_leaf_entry
*ent
;
213 unsigned int iter_off
;
217 xfs_dir2_data_aoff_t off
;
218 xfs_dir2_dataptr_t ptr
;
219 xfs_dahash_t calc_hash
;
221 struct xfs_dir3_icleaf_hdr hdr
;
225 ASSERT(blk
->magic
== XFS_DIR2_LEAF1_MAGIC
||
226 blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
228 xfs_dir2_leaf_hdr_from_disk(mp
, &hdr
, blk
->bp
->b_addr
);
229 ent
= hdr
.ents
+ blk
->index
;
231 /* Check the hash of the entry. */
232 error
= xchk_da_btree_hash(ds
, level
, &ent
->hashval
);
236 /* Valid hash pointer? */
237 ptr
= be32_to_cpu(ent
->address
);
241 /* Find the directory entry's location. */
242 db
= xfs_dir2_dataptr_to_db(geo
, ptr
);
243 off
= xfs_dir2_dataptr_to_off(geo
, ptr
);
244 rec_bno
= xfs_dir2_db_to_da(geo
, db
);
246 if (rec_bno
>= geo
->leafblk
) {
247 xchk_da_set_corrupt(ds
, level
);
250 error
= xfs_dir3_data_read(ds
->dargs
.trans
, dp
, rec_bno
,
251 XFS_DABUF_MAP_HOLE_OK
, &bp
);
252 if (!xchk_fblock_process_error(ds
->sc
, XFS_DATA_FORK
, rec_bno
,
256 xchk_fblock_set_corrupt(ds
->sc
, XFS_DATA_FORK
, rec_bno
);
259 xchk_buffer_recheck(ds
->sc
, bp
);
261 if (ds
->sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
264 dent
= bp
->b_addr
+ off
;
266 /* Make sure we got a real directory entry. */
267 iter_off
= geo
->data_entry_offset
;
268 end
= xfs_dir3_data_end_offset(geo
, bp
->b_addr
);
270 xchk_fblock_set_corrupt(ds
->sc
, XFS_DATA_FORK
, rec_bno
);
274 struct xfs_dir2_data_entry
*dep
= bp
->b_addr
+ iter_off
;
275 struct xfs_dir2_data_unused
*dup
= bp
->b_addr
+ iter_off
;
277 if (iter_off
>= end
) {
278 xchk_fblock_set_corrupt(ds
->sc
, XFS_DATA_FORK
, rec_bno
);
282 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
283 iter_off
+= be16_to_cpu(dup
->length
);
288 iter_off
+= xfs_dir2_data_entsize(mp
, dep
->namelen
);
291 /* Retrieve the entry, sanity check it, and compare hashes. */
292 ino
= be64_to_cpu(dent
->inumber
);
293 hash
= be32_to_cpu(ent
->hashval
);
294 tag
= be16_to_cpup(xfs_dir2_data_entry_tag_p(mp
, dent
));
295 if (!xfs_verify_dir_ino(mp
, ino
) || tag
!= off
)
296 xchk_fblock_set_corrupt(ds
->sc
, XFS_DATA_FORK
, rec_bno
);
297 if (dent
->namelen
== 0) {
298 xchk_fblock_set_corrupt(ds
->sc
, XFS_DATA_FORK
, rec_bno
);
301 calc_hash
= xfs_da_hashname(dent
->name
, dent
->namelen
);
302 if (calc_hash
!= hash
)
303 xchk_fblock_set_corrupt(ds
->sc
, XFS_DATA_FORK
, rec_bno
);
306 xfs_trans_brelse(ds
->dargs
.trans
, bp
);
312 * Is this unused entry either in the bestfree or smaller than all of
313 * them? We've already checked that the bestfrees are sorted longest to
314 * shortest, and that there aren't any bogus entries.
317 xchk_directory_check_free_entry(
318 struct xfs_scrub
*sc
,
320 struct xfs_dir2_data_free
*bf
,
321 struct xfs_dir2_data_unused
*dup
)
323 struct xfs_dir2_data_free
*dfp
;
324 unsigned int dup_length
;
326 dup_length
= be16_to_cpu(dup
->length
);
328 /* Unused entry is shorter than any of the bestfrees */
329 if (dup_length
< be16_to_cpu(bf
[XFS_DIR2_DATA_FD_COUNT
- 1].length
))
332 for (dfp
= &bf
[XFS_DIR2_DATA_FD_COUNT
- 1]; dfp
>= bf
; dfp
--)
333 if (dup_length
== be16_to_cpu(dfp
->length
))
336 /* Unused entry should be in the bestfrees but wasn't found. */
337 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
340 /* Check free space info in a directory data block. */
342 xchk_directory_data_bestfree(
343 struct xfs_scrub
*sc
,
347 struct xfs_dir2_data_unused
*dup
;
348 struct xfs_dir2_data_free
*dfp
;
350 struct xfs_dir2_data_free
*bf
;
351 struct xfs_mount
*mp
= sc
->mp
;
353 unsigned int nr_bestfrees
= 0;
354 unsigned int nr_frees
= 0;
355 unsigned int smallest_bestfree
;
362 /* dir block format */
363 if (lblk
!= XFS_B_TO_FSBT(mp
, XFS_DIR2_DATA_OFFSET
))
364 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
365 error
= xfs_dir3_block_read(sc
->tp
, sc
->ip
, &bp
);
367 /* dir data format */
368 error
= xfs_dir3_data_read(sc
->tp
, sc
->ip
, lblk
, 0, &bp
);
370 if (!xchk_fblock_process_error(sc
, XFS_DATA_FORK
, lblk
, &error
))
372 xchk_buffer_recheck(sc
, bp
);
374 /* XXX: Check xfs_dir3_data_hdr.pad is zero once we start setting it. */
376 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
379 /* Do the bestfrees correspond to actual free space? */
380 bf
= xfs_dir2_data_bestfree_p(mp
, bp
->b_addr
);
381 smallest_bestfree
= UINT_MAX
;
382 for (dfp
= &bf
[0]; dfp
< &bf
[XFS_DIR2_DATA_FD_COUNT
]; dfp
++) {
383 offset
= be16_to_cpu(dfp
->offset
);
386 if (offset
>= mp
->m_dir_geo
->blksize
) {
387 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
390 dup
= bp
->b_addr
+ offset
;
391 tag
= be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
));
393 /* bestfree doesn't match the entry it points at? */
394 if (dup
->freetag
!= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
) ||
395 be16_to_cpu(dup
->length
) != be16_to_cpu(dfp
->length
) ||
397 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
401 /* bestfree records should be ordered largest to smallest */
402 if (smallest_bestfree
< be16_to_cpu(dfp
->length
)) {
403 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
407 smallest_bestfree
= be16_to_cpu(dfp
->length
);
411 /* Make sure the bestfrees are actually the best free spaces. */
412 offset
= mp
->m_dir_geo
->data_entry_offset
;
413 end
= xfs_dir3_data_end_offset(mp
->m_dir_geo
, bp
->b_addr
);
415 /* Iterate the entries, stopping when we hit or go past the end. */
416 while (offset
< end
) {
417 dup
= bp
->b_addr
+ offset
;
419 /* Skip real entries */
420 if (dup
->freetag
!= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
)) {
421 struct xfs_dir2_data_entry
*dep
= bp
->b_addr
+ offset
;
423 newlen
= xfs_dir2_data_entsize(mp
, dep
->namelen
);
425 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
,
433 /* Spot check this free entry */
434 tag
= be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
));
436 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
441 * Either this entry is a bestfree or it's smaller than
442 * any of the bestfrees.
444 xchk_directory_check_free_entry(sc
, lblk
, bf
, dup
);
445 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
449 newlen
= be16_to_cpu(dup
->length
);
451 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
459 /* We're required to fill all the space. */
461 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
463 /* Did we see at least as many free slots as there are bestfrees? */
464 if (nr_frees
< nr_bestfrees
)
465 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
467 xfs_trans_brelse(sc
->tp
, bp
);
473 * Does the free space length in the free space index block ($len) match
474 * the longest length in the directory data block's bestfree array?
475 * Assume that we've already checked that the data block's bestfree
479 xchk_directory_check_freesp(
480 struct xfs_scrub
*sc
,
485 struct xfs_dir2_data_free
*dfp
;
487 dfp
= xfs_dir2_data_bestfree_p(sc
->mp
, dbp
->b_addr
);
489 if (len
!= be16_to_cpu(dfp
->length
))
490 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
492 if (len
> 0 && be16_to_cpu(dfp
->offset
) == 0)
493 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
496 /* Check free space info in a directory leaf1 block. */
498 xchk_directory_leaf1_bestfree(
499 struct xfs_scrub
*sc
,
500 struct xfs_da_args
*args
,
503 struct xfs_dir3_icleaf_hdr leafhdr
;
504 struct xfs_dir2_leaf_tail
*ltp
;
505 struct xfs_dir2_leaf
*leaf
;
508 struct xfs_da_geometry
*geo
= sc
->mp
->m_dir_geo
;
514 unsigned int stale
= 0;
518 /* Read the free space block. */
519 error
= xfs_dir3_leaf_read(sc
->tp
, sc
->ip
, lblk
, &bp
);
520 if (!xchk_fblock_process_error(sc
, XFS_DATA_FORK
, lblk
, &error
))
522 xchk_buffer_recheck(sc
, bp
);
525 xfs_dir2_leaf_hdr_from_disk(sc
->ip
->i_mount
, &leafhdr
, leaf
);
526 ltp
= xfs_dir2_leaf_tail_p(geo
, leaf
);
527 bestcount
= be32_to_cpu(ltp
->bestcount
);
528 bestp
= xfs_dir2_leaf_bests_p(ltp
);
530 if (xfs_sb_version_hascrc(&sc
->mp
->m_sb
)) {
531 struct xfs_dir3_leaf_hdr
*hdr3
= bp
->b_addr
;
533 if (hdr3
->pad
!= cpu_to_be32(0))
534 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
538 * There should be as many bestfree slots as there are dir data
539 * blocks that can fit under i_size.
541 if (bestcount
!= xfs_dir2_byte_to_db(geo
, sc
->ip
->i_d
.di_size
)) {
542 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
546 /* Is the leaf count even remotely sane? */
547 if (leafhdr
.count
> geo
->leaf_max_ents
) {
548 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
552 /* Leaves and bests don't overlap in leaf format. */
553 if ((char *)&leafhdr
.ents
[leafhdr
.count
] > (char *)bestp
) {
554 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
558 /* Check hash value order, count stale entries. */
559 for (i
= 0; i
< leafhdr
.count
; i
++) {
560 hash
= be32_to_cpu(leafhdr
.ents
[i
].hashval
);
561 if (i
> 0 && lasthash
> hash
)
562 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
564 if (leafhdr
.ents
[i
].address
==
565 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
568 if (leafhdr
.stale
!= stale
)
569 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
570 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
573 /* Check all the bestfree entries. */
574 for (i
= 0; i
< bestcount
; i
++, bestp
++) {
575 best
= be16_to_cpu(*bestp
);
576 error
= xfs_dir3_data_read(sc
->tp
, sc
->ip
,
577 xfs_dir2_db_to_da(args
->geo
, i
),
578 XFS_DABUF_MAP_HOLE_OK
,
580 if (!xchk_fblock_process_error(sc
, XFS_DATA_FORK
, lblk
,
585 if (best
!= NULLDATAOFF
) {
586 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
,
593 if (best
== NULLDATAOFF
)
594 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
596 xchk_directory_check_freesp(sc
, lblk
, dbp
, best
);
597 xfs_trans_brelse(sc
->tp
, dbp
);
598 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
602 xfs_trans_brelse(sc
->tp
, bp
);
606 /* Check free space info in a directory freespace block. */
608 xchk_directory_free_bestfree(
609 struct xfs_scrub
*sc
,
610 struct xfs_da_args
*args
,
613 struct xfs_dir3_icfree_hdr freehdr
;
617 unsigned int stale
= 0;
621 /* Read the free space block */
622 error
= xfs_dir2_free_read(sc
->tp
, sc
->ip
, lblk
, &bp
);
623 if (!xchk_fblock_process_error(sc
, XFS_DATA_FORK
, lblk
, &error
))
625 xchk_buffer_recheck(sc
, bp
);
627 if (xfs_sb_version_hascrc(&sc
->mp
->m_sb
)) {
628 struct xfs_dir3_free_hdr
*hdr3
= bp
->b_addr
;
630 if (hdr3
->pad
!= cpu_to_be32(0))
631 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
634 /* Check all the entries. */
635 xfs_dir2_free_hdr_from_disk(sc
->ip
->i_mount
, &freehdr
, bp
->b_addr
);
636 for (i
= 0; i
< freehdr
.nvalid
; i
++) {
637 best
= be16_to_cpu(freehdr
.bests
[i
]);
638 if (best
== NULLDATAOFF
) {
642 error
= xfs_dir3_data_read(sc
->tp
, sc
->ip
,
643 (freehdr
.firstdb
+ i
) * args
->geo
->fsbcount
,
645 if (!xchk_fblock_process_error(sc
, XFS_DATA_FORK
, lblk
,
648 xchk_directory_check_freesp(sc
, lblk
, dbp
, best
);
649 xfs_trans_brelse(sc
->tp
, dbp
);
652 if (freehdr
.nused
+ stale
!= freehdr
.nvalid
)
653 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
655 xfs_trans_brelse(sc
->tp
, bp
);
659 /* Check free space information in directories. */
661 xchk_directory_blocks(
662 struct xfs_scrub
*sc
)
664 struct xfs_bmbt_irec got
;
665 struct xfs_da_args args
;
666 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(sc
->ip
, XFS_DATA_FORK
);
667 struct xfs_mount
*mp
= sc
->mp
;
668 xfs_fileoff_t leaf_lblk
;
669 xfs_fileoff_t free_lblk
;
671 struct xfs_iext_cursor icur
;
677 /* Ignore local format directories. */
678 if (ifp
->if_format
!= XFS_DINODE_FMT_EXTENTS
&&
679 ifp
->if_format
!= XFS_DINODE_FMT_BTREE
)
682 lblk
= XFS_B_TO_FSB(mp
, XFS_DIR2_DATA_OFFSET
);
683 leaf_lblk
= XFS_B_TO_FSB(mp
, XFS_DIR2_LEAF_OFFSET
);
684 free_lblk
= XFS_B_TO_FSB(mp
, XFS_DIR2_FREE_OFFSET
);
686 /* Is this a block dir? */
688 args
.geo
= mp
->m_dir_geo
;
690 error
= xfs_dir2_isblock(&args
, &is_block
);
691 if (!xchk_fblock_process_error(sc
, XFS_DATA_FORK
, lblk
, &error
))
694 /* Iterate all the data extents in the directory... */
695 found
= xfs_iext_lookup_extent(sc
->ip
, ifp
, lblk
, &icur
, &got
);
696 while (found
&& !(sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)) {
697 /* Block directories only have a single block at offset 0. */
699 (got
.br_startoff
> 0 ||
700 got
.br_blockcount
!= args
.geo
->fsbcount
)) {
701 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
,
706 /* No more data blocks... */
707 if (got
.br_startoff
>= leaf_lblk
)
711 * Check each data block's bestfree data.
713 * Iterate all the fsbcount-aligned block offsets in
714 * this directory. The directory block reading code is
715 * smart enough to do its own bmap lookups to handle
716 * discontiguous directory blocks. When we're done
717 * with the extent record, re-query the bmap at the
718 * next fsbcount-aligned offset to avoid redundant
721 for (lblk
= roundup((xfs_dablk_t
)got
.br_startoff
,
723 lblk
< got
.br_startoff
+ got
.br_blockcount
;
724 lblk
+= args
.geo
->fsbcount
) {
725 error
= xchk_directory_data_bestfree(sc
, lblk
,
730 dabno
= got
.br_startoff
+ got
.br_blockcount
;
731 lblk
= roundup(dabno
, args
.geo
->fsbcount
);
732 found
= xfs_iext_lookup_extent(sc
->ip
, ifp
, lblk
, &icur
, &got
);
735 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
738 /* Look for a leaf1 block, which has free info. */
739 if (xfs_iext_lookup_extent(sc
->ip
, ifp
, leaf_lblk
, &icur
, &got
) &&
740 got
.br_startoff
== leaf_lblk
&&
741 got
.br_blockcount
== args
.geo
->fsbcount
&&
742 !xfs_iext_next_extent(ifp
, &icur
, &got
)) {
744 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
747 error
= xchk_directory_leaf1_bestfree(sc
, &args
,
753 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
756 /* Scan for free blocks */
758 found
= xfs_iext_lookup_extent(sc
->ip
, ifp
, lblk
, &icur
, &got
);
759 while (found
&& !(sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)) {
761 * Dirs can't have blocks mapped above 2^32.
762 * Single-block dirs shouldn't even be here.
764 lblk
= got
.br_startoff
;
765 if (lblk
& ~0xFFFFFFFFULL
) {
766 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
770 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
775 * Check each dir free block's bestfree data.
777 * Iterate all the fsbcount-aligned block offsets in
778 * this directory. The directory block reading code is
779 * smart enough to do its own bmap lookups to handle
780 * discontiguous directory blocks. When we're done
781 * with the extent record, re-query the bmap at the
782 * next fsbcount-aligned offset to avoid redundant
785 for (lblk
= roundup((xfs_dablk_t
)got
.br_startoff
,
787 lblk
< got
.br_startoff
+ got
.br_blockcount
;
788 lblk
+= args
.geo
->fsbcount
) {
789 error
= xchk_directory_free_bestfree(sc
, &args
,
794 dabno
= got
.br_startoff
+ got
.br_blockcount
;
795 lblk
= roundup(dabno
, args
.geo
->fsbcount
);
796 found
= xfs_iext_lookup_extent(sc
->ip
, ifp
, lblk
, &icur
, &got
);
802 /* Scrub a whole directory. */
805 struct xfs_scrub
*sc
)
807 struct xchk_dir_ctx sdc
= {
808 .dir_iter
.actor
= xchk_dir_actor
,
816 if (!S_ISDIR(VFS_I(sc
->ip
)->i_mode
))
819 /* Plausible size? */
820 if (sc
->ip
->i_d
.di_size
< xfs_dir2_sf_hdr_size(0)) {
821 xchk_ino_set_corrupt(sc
, sc
->ip
->i_ino
);
825 /* Check directory tree structure */
826 error
= xchk_da_btree(sc
, XFS_DATA_FORK
, xchk_dir_rec
, NULL
);
830 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
833 /* Check the freespace. */
834 error
= xchk_directory_blocks(sc
);
838 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
842 * Check that every dirent we see can also be looked up by hash.
843 * Userspace usually asks for a 32k buffer, so we will too.
845 bufsize
= (size_t)min_t(loff_t
, XFS_READDIR_BUFSIZE
,
846 sc
->ip
->i_d
.di_size
);
849 * Look up every name in this directory by hash.
851 * Use the xfs_readdir function to call xchk_dir_actor on
852 * every directory entry in this directory. In _actor, we check
853 * the name, inode number, and ftype (if applicable) of the
854 * entry. xfs_readdir uses the VFS filldir functions to provide
857 * The VFS grabs a read or write lock via i_rwsem before it reads
858 * or writes to a directory. If we've gotten this far we've
859 * already obtained IOLOCK_EXCL, which (since 4.10) is the same as
860 * getting a write lock on i_rwsem. Therefore, it is safe for us
861 * to drop the ILOCK here in order to reuse the _readdir and
862 * _dir_lookup routines, which do their own ILOCK locking.
865 sc
->ilock_flags
&= ~XFS_ILOCK_EXCL
;
866 xfs_iunlock(sc
->ip
, XFS_ILOCK_EXCL
);
868 error
= xfs_readdir(sc
->tp
, sc
->ip
, &sdc
.dir_iter
, bufsize
);
869 if (!xchk_fblock_process_error(sc
, XFS_DATA_FORK
, 0,
872 if (oldpos
== sdc
.dir_iter
.pos
)
874 oldpos
= sdc
.dir_iter
.pos
;