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_defer.h"
13 #include "xfs_btree.h"
15 #include "xfs_log_format.h"
16 #include "xfs_trans.h"
18 #include "xfs_inode.h"
19 #include "xfs_icache.h"
20 #include "xfs_itable.h"
21 #include "xfs_da_format.h"
22 #include "xfs_da_btree.h"
24 #include "xfs_dir2_priv.h"
25 #include "xfs_ialloc.h"
26 #include "scrub/xfs_scrub.h"
27 #include "scrub/scrub.h"
28 #include "scrub/common.h"
29 #include "scrub/trace.h"
30 #include "scrub/dabtree.h"
32 /* Set us up to scrub directories. */
38 return xchk_setup_inode_contents(sc
, ip
, 0);
43 /* Scrub a directory entry. */
46 /* VFS fill-directory iterator */
47 struct dir_context dir_iter
;
52 /* Check that an inode's mode matches a given DT_ type. */
55 struct xchk_dir_ctx
*sdc
,
60 struct xfs_mount
*mp
= sdc
->sc
->mp
;
65 if (!xfs_sb_version_hasftype(&mp
->m_sb
)) {
66 if (dtype
!= DT_UNKNOWN
&& dtype
!= DT_DIR
)
67 xchk_fblock_set_corrupt(sdc
->sc
, XFS_DATA_FORK
,
73 * Grab the inode pointed to by the dirent. We release the
74 * inode before we cancel the scrub transaction. Since we're
75 * don't know a priori that releasing the inode won't trigger
76 * eofblocks cleanup (which allocates what would be a nested
77 * transaction), we can't use DONTCACHE here because DONTCACHE
78 * inodes can trigger immediate inactive cleanup of the inode.
80 error
= xfs_iget(mp
, sdc
->sc
->tp
, inum
, 0, 0, &ip
);
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
;
120 sdc
= container_of(dir_iter
, struct xchk_dir_ctx
, dir_iter
);
123 offset
= xfs_dir2_db_to_da(mp
->m_dir_geo
,
124 xfs_dir2_dataptr_to_db(mp
->m_dir_geo
, pos
));
126 /* Does this inode number make sense? */
127 if (!xfs_verify_dir_ino(mp
, ino
)) {
128 xchk_fblock_set_corrupt(sdc
->sc
, XFS_DATA_FORK
, offset
);
132 /* Does this name make sense? */
133 if (!xfs_dir2_namecheck(name
, namelen
)) {
134 xchk_fblock_set_corrupt(sdc
->sc
, XFS_DATA_FORK
, offset
);
138 if (!strncmp(".", name
, namelen
)) {
139 /* If this is "." then check that the inum matches the dir. */
140 if (xfs_sb_version_hasftype(&mp
->m_sb
) && type
!= DT_DIR
)
141 xchk_fblock_set_corrupt(sdc
->sc
, XFS_DATA_FORK
,
143 if (ino
!= ip
->i_ino
)
144 xchk_fblock_set_corrupt(sdc
->sc
, XFS_DATA_FORK
,
146 } else if (!strncmp("..", name
, namelen
)) {
148 * If this is ".." in the root inode, check that the inum
151 if (xfs_sb_version_hasftype(&mp
->m_sb
) && type
!= DT_DIR
)
152 xchk_fblock_set_corrupt(sdc
->sc
, XFS_DATA_FORK
,
154 if (ip
->i_ino
== mp
->m_sb
.sb_rootino
&& ino
!= ip
->i_ino
)
155 xchk_fblock_set_corrupt(sdc
->sc
, XFS_DATA_FORK
,
159 /* Verify that we can look up this name by hash. */
162 xname
.type
= XFS_DIR3_FT_UNKNOWN
;
164 error
= xfs_dir_lookup(sdc
->sc
->tp
, ip
, &xname
, &lookup_ino
, NULL
);
165 if (!xchk_fblock_process_error(sdc
->sc
, XFS_DATA_FORK
, offset
,
168 if (lookup_ino
!= ino
) {
169 xchk_fblock_set_corrupt(sdc
->sc
, XFS_DATA_FORK
, offset
);
173 /* Verify the file type. This function absorbs error codes. */
174 error
= xchk_dir_check_ftype(sdc
, offset
, lookup_ino
, type
);
179 * A negative error code returned here is supposed to cause the
180 * dir_emit caller (xfs_readdir) to abort the directory iteration
181 * and return zero to xchk_directory.
183 if (error
== 0 && sdc
->sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
184 return -EFSCORRUPTED
;
188 /* Scrub a directory btree record. */
191 struct xchk_da_btree
*ds
,
195 struct xfs_mount
*mp
= ds
->state
->mp
;
196 struct xfs_dir2_leaf_entry
*ent
= rec
;
197 struct xfs_inode
*dp
= ds
->dargs
.dp
;
198 struct xfs_dir2_data_entry
*dent
;
204 xfs_dir2_data_aoff_t off
;
205 xfs_dir2_dataptr_t ptr
;
206 xfs_dahash_t calc_hash
;
211 /* Check the hash of the entry. */
212 error
= xchk_da_btree_hash(ds
, level
, &ent
->hashval
);
216 /* Valid hash pointer? */
217 ptr
= be32_to_cpu(ent
->address
);
221 /* Find the directory entry's location. */
222 db
= xfs_dir2_dataptr_to_db(mp
->m_dir_geo
, ptr
);
223 off
= xfs_dir2_dataptr_to_off(mp
->m_dir_geo
, ptr
);
224 rec_bno
= xfs_dir2_db_to_da(mp
->m_dir_geo
, db
);
226 if (rec_bno
>= mp
->m_dir_geo
->leafblk
) {
227 xchk_da_set_corrupt(ds
, level
);
230 error
= xfs_dir3_data_read(ds
->dargs
.trans
, dp
, rec_bno
, -2, &bp
);
231 if (!xchk_fblock_process_error(ds
->sc
, XFS_DATA_FORK
, rec_bno
,
235 xchk_fblock_set_corrupt(ds
->sc
, XFS_DATA_FORK
, rec_bno
);
238 xchk_buffer_recheck(ds
->sc
, bp
);
240 if (ds
->sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
243 dent
= (struct xfs_dir2_data_entry
*)(((char *)bp
->b_addr
) + off
);
245 /* Make sure we got a real directory entry. */
246 p
= (char *)mp
->m_dir_inode_ops
->data_entry_p(bp
->b_addr
);
247 endp
= xfs_dir3_data_endp(mp
->m_dir_geo
, bp
->b_addr
);
249 xchk_fblock_set_corrupt(ds
->sc
, XFS_DATA_FORK
, rec_bno
);
253 struct xfs_dir2_data_entry
*dep
;
254 struct xfs_dir2_data_unused
*dup
;
256 dup
= (struct xfs_dir2_data_unused
*)p
;
257 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
258 p
+= be16_to_cpu(dup
->length
);
261 dep
= (struct xfs_dir2_data_entry
*)p
;
264 p
+= mp
->m_dir_inode_ops
->data_entsize(dep
->namelen
);
267 xchk_fblock_set_corrupt(ds
->sc
, XFS_DATA_FORK
, rec_bno
);
271 /* Retrieve the entry, sanity check it, and compare hashes. */
272 ino
= be64_to_cpu(dent
->inumber
);
273 hash
= be32_to_cpu(ent
->hashval
);
274 tag
= be16_to_cpup(dp
->d_ops
->data_entry_tag_p(dent
));
275 if (!xfs_verify_dir_ino(mp
, ino
) || tag
!= off
)
276 xchk_fblock_set_corrupt(ds
->sc
, XFS_DATA_FORK
, rec_bno
);
277 if (dent
->namelen
== 0) {
278 xchk_fblock_set_corrupt(ds
->sc
, XFS_DATA_FORK
, rec_bno
);
281 calc_hash
= xfs_da_hashname(dent
->name
, dent
->namelen
);
282 if (calc_hash
!= hash
)
283 xchk_fblock_set_corrupt(ds
->sc
, XFS_DATA_FORK
, rec_bno
);
286 xfs_trans_brelse(ds
->dargs
.trans
, bp
);
292 * Is this unused entry either in the bestfree or smaller than all of
293 * them? We've already checked that the bestfrees are sorted longest to
294 * shortest, and that there aren't any bogus entries.
297 xchk_directory_check_free_entry(
298 struct xfs_scrub
*sc
,
300 struct xfs_dir2_data_free
*bf
,
301 struct xfs_dir2_data_unused
*dup
)
303 struct xfs_dir2_data_free
*dfp
;
304 unsigned int dup_length
;
306 dup_length
= be16_to_cpu(dup
->length
);
308 /* Unused entry is shorter than any of the bestfrees */
309 if (dup_length
< be16_to_cpu(bf
[XFS_DIR2_DATA_FD_COUNT
- 1].length
))
312 for (dfp
= &bf
[XFS_DIR2_DATA_FD_COUNT
- 1]; dfp
>= bf
; dfp
--)
313 if (dup_length
== be16_to_cpu(dfp
->length
))
316 /* Unused entry should be in the bestfrees but wasn't found. */
317 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
320 /* Check free space info in a directory data block. */
322 xchk_directory_data_bestfree(
323 struct xfs_scrub
*sc
,
327 struct xfs_dir2_data_unused
*dup
;
328 struct xfs_dir2_data_free
*dfp
;
330 struct xfs_dir2_data_free
*bf
;
331 struct xfs_mount
*mp
= sc
->mp
;
332 const struct xfs_dir_ops
*d_ops
;
336 unsigned int nr_bestfrees
= 0;
337 unsigned int nr_frees
= 0;
338 unsigned int smallest_bestfree
;
343 d_ops
= sc
->ip
->d_ops
;
346 /* dir block format */
347 if (lblk
!= XFS_B_TO_FSBT(mp
, XFS_DIR2_DATA_OFFSET
))
348 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
349 error
= xfs_dir3_block_read(sc
->tp
, sc
->ip
, &bp
);
351 /* dir data format */
352 error
= xfs_dir3_data_read(sc
->tp
, sc
->ip
, lblk
, -1, &bp
);
354 if (!xchk_fblock_process_error(sc
, XFS_DATA_FORK
, lblk
, &error
))
356 xchk_buffer_recheck(sc
, bp
);
358 /* XXX: Check xfs_dir3_data_hdr.pad is zero once we start setting it. */
360 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
363 /* Do the bestfrees correspond to actual free space? */
364 bf
= d_ops
->data_bestfree_p(bp
->b_addr
);
365 smallest_bestfree
= UINT_MAX
;
366 for (dfp
= &bf
[0]; dfp
< &bf
[XFS_DIR2_DATA_FD_COUNT
]; dfp
++) {
367 offset
= be16_to_cpu(dfp
->offset
);
370 if (offset
>= mp
->m_dir_geo
->blksize
) {
371 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
374 dup
= (struct xfs_dir2_data_unused
*)(bp
->b_addr
+ offset
);
375 tag
= be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
));
377 /* bestfree doesn't match the entry it points at? */
378 if (dup
->freetag
!= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
) ||
379 be16_to_cpu(dup
->length
) != be16_to_cpu(dfp
->length
) ||
380 tag
!= ((char *)dup
- (char *)bp
->b_addr
)) {
381 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
385 /* bestfree records should be ordered largest to smallest */
386 if (smallest_bestfree
< be16_to_cpu(dfp
->length
)) {
387 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
391 smallest_bestfree
= be16_to_cpu(dfp
->length
);
395 /* Make sure the bestfrees are actually the best free spaces. */
396 ptr
= (char *)d_ops
->data_entry_p(bp
->b_addr
);
397 endptr
= xfs_dir3_data_endp(mp
->m_dir_geo
, bp
->b_addr
);
399 /* Iterate the entries, stopping when we hit or go past the end. */
400 while (ptr
< endptr
) {
401 dup
= (struct xfs_dir2_data_unused
*)ptr
;
402 /* Skip real entries */
403 if (dup
->freetag
!= cpu_to_be16(XFS_DIR2_DATA_FREE_TAG
)) {
404 struct xfs_dir2_data_entry
*dep
;
406 dep
= (struct xfs_dir2_data_entry
*)ptr
;
407 newlen
= d_ops
->data_entsize(dep
->namelen
);
409 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
,
417 /* Spot check this free entry */
418 tag
= be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
));
419 if (tag
!= ((char *)dup
- (char *)bp
->b_addr
)) {
420 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
425 * Either this entry is a bestfree or it's smaller than
426 * any of the bestfrees.
428 xchk_directory_check_free_entry(sc
, lblk
, bf
, dup
);
429 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
433 newlen
= be16_to_cpu(dup
->length
);
435 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
443 /* We're required to fill all the space. */
445 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
447 /* Did we see at least as many free slots as there are bestfrees? */
448 if (nr_frees
< nr_bestfrees
)
449 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
451 xfs_trans_brelse(sc
->tp
, bp
);
457 * Does the free space length in the free space index block ($len) match
458 * the longest length in the directory data block's bestfree array?
459 * Assume that we've already checked that the data block's bestfree
463 xchk_directory_check_freesp(
464 struct xfs_scrub
*sc
,
469 struct xfs_dir2_data_free
*dfp
;
471 dfp
= sc
->ip
->d_ops
->data_bestfree_p(dbp
->b_addr
);
473 if (len
!= be16_to_cpu(dfp
->length
))
474 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
476 if (len
> 0 && be16_to_cpu(dfp
->offset
) == 0)
477 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
480 /* Check free space info in a directory leaf1 block. */
482 xchk_directory_leaf1_bestfree(
483 struct xfs_scrub
*sc
,
484 struct xfs_da_args
*args
,
487 struct xfs_dir3_icleaf_hdr leafhdr
;
488 struct xfs_dir2_leaf_entry
*ents
;
489 struct xfs_dir2_leaf_tail
*ltp
;
490 struct xfs_dir2_leaf
*leaf
;
493 const struct xfs_dir_ops
*d_ops
= sc
->ip
->d_ops
;
494 struct xfs_da_geometry
*geo
= sc
->mp
->m_dir_geo
;
500 unsigned int stale
= 0;
504 /* Read the free space block. */
505 error
= xfs_dir3_leaf_read(sc
->tp
, sc
->ip
, lblk
, -1, &bp
);
506 if (!xchk_fblock_process_error(sc
, XFS_DATA_FORK
, lblk
, &error
))
508 xchk_buffer_recheck(sc
, bp
);
511 d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
512 ents
= d_ops
->leaf_ents_p(leaf
);
513 ltp
= xfs_dir2_leaf_tail_p(geo
, leaf
);
514 bestcount
= be32_to_cpu(ltp
->bestcount
);
515 bestp
= xfs_dir2_leaf_bests_p(ltp
);
517 if (xfs_sb_version_hascrc(&sc
->mp
->m_sb
)) {
518 struct xfs_dir3_leaf_hdr
*hdr3
= bp
->b_addr
;
520 if (hdr3
->pad
!= cpu_to_be32(0))
521 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
525 * There should be as many bestfree slots as there are dir data
526 * blocks that can fit under i_size.
528 if (bestcount
!= xfs_dir2_byte_to_db(geo
, sc
->ip
->i_d
.di_size
)) {
529 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
533 /* Is the leaf count even remotely sane? */
534 if (leafhdr
.count
> d_ops
->leaf_max_ents(geo
)) {
535 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
539 /* Leaves and bests don't overlap in leaf format. */
540 if ((char *)&ents
[leafhdr
.count
] > (char *)bestp
) {
541 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
545 /* Check hash value order, count stale entries. */
546 for (i
= 0; i
< leafhdr
.count
; i
++) {
547 hash
= be32_to_cpu(ents
[i
].hashval
);
548 if (i
> 0 && lasthash
> hash
)
549 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
551 if (ents
[i
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
554 if (leafhdr
.stale
!= stale
)
555 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
556 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
559 /* Check all the bestfree entries. */
560 for (i
= 0; i
< bestcount
; i
++, bestp
++) {
561 best
= be16_to_cpu(*bestp
);
562 if (best
== NULLDATAOFF
)
564 error
= xfs_dir3_data_read(sc
->tp
, sc
->ip
,
565 i
* args
->geo
->fsbcount
, -1, &dbp
);
566 if (!xchk_fblock_process_error(sc
, XFS_DATA_FORK
, lblk
,
569 xchk_directory_check_freesp(sc
, lblk
, dbp
, best
);
570 xfs_trans_brelse(sc
->tp
, dbp
);
571 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
578 /* Check free space info in a directory freespace block. */
580 xchk_directory_free_bestfree(
581 struct xfs_scrub
*sc
,
582 struct xfs_da_args
*args
,
585 struct xfs_dir3_icfree_hdr freehdr
;
590 unsigned int stale
= 0;
594 /* Read the free space block */
595 error
= xfs_dir2_free_read(sc
->tp
, sc
->ip
, lblk
, &bp
);
596 if (!xchk_fblock_process_error(sc
, XFS_DATA_FORK
, lblk
, &error
))
598 xchk_buffer_recheck(sc
, bp
);
600 if (xfs_sb_version_hascrc(&sc
->mp
->m_sb
)) {
601 struct xfs_dir3_free_hdr
*hdr3
= bp
->b_addr
;
603 if (hdr3
->pad
!= cpu_to_be32(0))
604 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
607 /* Check all the entries. */
608 sc
->ip
->d_ops
->free_hdr_from_disk(&freehdr
, bp
->b_addr
);
609 bestp
= sc
->ip
->d_ops
->free_bests_p(bp
->b_addr
);
610 for (i
= 0; i
< freehdr
.nvalid
; i
++, bestp
++) {
611 best
= be16_to_cpu(*bestp
);
612 if (best
== NULLDATAOFF
) {
616 error
= xfs_dir3_data_read(sc
->tp
, sc
->ip
,
617 (freehdr
.firstdb
+ i
) * args
->geo
->fsbcount
,
619 if (!xchk_fblock_process_error(sc
, XFS_DATA_FORK
, lblk
,
622 xchk_directory_check_freesp(sc
, lblk
, dbp
, best
);
623 xfs_trans_brelse(sc
->tp
, dbp
);
626 if (freehdr
.nused
+ stale
!= freehdr
.nvalid
)
627 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
632 /* Check free space information in directories. */
634 xchk_directory_blocks(
635 struct xfs_scrub
*sc
)
637 struct xfs_bmbt_irec got
;
638 struct xfs_da_args args
;
639 struct xfs_ifork
*ifp
;
640 struct xfs_mount
*mp
= sc
->mp
;
641 xfs_fileoff_t leaf_lblk
;
642 xfs_fileoff_t free_lblk
;
644 struct xfs_iext_cursor icur
;
650 /* Ignore local format directories. */
651 if (sc
->ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
&&
652 sc
->ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
)
655 ifp
= XFS_IFORK_PTR(sc
->ip
, XFS_DATA_FORK
);
656 lblk
= XFS_B_TO_FSB(mp
, XFS_DIR2_DATA_OFFSET
);
657 leaf_lblk
= XFS_B_TO_FSB(mp
, XFS_DIR2_LEAF_OFFSET
);
658 free_lblk
= XFS_B_TO_FSB(mp
, XFS_DIR2_FREE_OFFSET
);
660 /* Is this a block dir? */
662 args
.geo
= mp
->m_dir_geo
;
664 error
= xfs_dir2_isblock(&args
, &is_block
);
665 if (!xchk_fblock_process_error(sc
, XFS_DATA_FORK
, lblk
, &error
))
668 /* Iterate all the data extents in the directory... */
669 found
= xfs_iext_lookup_extent(sc
->ip
, ifp
, lblk
, &icur
, &got
);
670 while (found
&& !(sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)) {
671 /* Block directories only have a single block at offset 0. */
673 (got
.br_startoff
> 0 ||
674 got
.br_blockcount
!= args
.geo
->fsbcount
)) {
675 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
,
680 /* No more data blocks... */
681 if (got
.br_startoff
>= leaf_lblk
)
685 * Check each data block's bestfree data.
687 * Iterate all the fsbcount-aligned block offsets in
688 * this directory. The directory block reading code is
689 * smart enough to do its own bmap lookups to handle
690 * discontiguous directory blocks. When we're done
691 * with the extent record, re-query the bmap at the
692 * next fsbcount-aligned offset to avoid redundant
695 for (lblk
= roundup((xfs_dablk_t
)got
.br_startoff
,
697 lblk
< got
.br_startoff
+ got
.br_blockcount
;
698 lblk
+= args
.geo
->fsbcount
) {
699 error
= xchk_directory_data_bestfree(sc
, lblk
,
704 dabno
= got
.br_startoff
+ got
.br_blockcount
;
705 lblk
= roundup(dabno
, args
.geo
->fsbcount
);
706 found
= xfs_iext_lookup_extent(sc
->ip
, ifp
, lblk
, &icur
, &got
);
709 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
712 /* Look for a leaf1 block, which has free info. */
713 if (xfs_iext_lookup_extent(sc
->ip
, ifp
, leaf_lblk
, &icur
, &got
) &&
714 got
.br_startoff
== leaf_lblk
&&
715 got
.br_blockcount
== args
.geo
->fsbcount
&&
716 !xfs_iext_next_extent(ifp
, &icur
, &got
)) {
718 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
721 error
= xchk_directory_leaf1_bestfree(sc
, &args
,
727 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
730 /* Scan for free blocks */
732 found
= xfs_iext_lookup_extent(sc
->ip
, ifp
, lblk
, &icur
, &got
);
733 while (found
&& !(sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)) {
735 * Dirs can't have blocks mapped above 2^32.
736 * Single-block dirs shouldn't even be here.
738 lblk
= got
.br_startoff
;
739 if (lblk
& ~0xFFFFFFFFULL
) {
740 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
744 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
749 * Check each dir free block's bestfree data.
751 * Iterate all the fsbcount-aligned block offsets in
752 * this directory. The directory block reading code is
753 * smart enough to do its own bmap lookups to handle
754 * discontiguous directory blocks. When we're done
755 * with the extent record, re-query the bmap at the
756 * next fsbcount-aligned offset to avoid redundant
759 for (lblk
= roundup((xfs_dablk_t
)got
.br_startoff
,
761 lblk
< got
.br_startoff
+ got
.br_blockcount
;
762 lblk
+= args
.geo
->fsbcount
) {
763 error
= xchk_directory_free_bestfree(sc
, &args
,
768 dabno
= got
.br_startoff
+ got
.br_blockcount
;
769 lblk
= roundup(dabno
, args
.geo
->fsbcount
);
770 found
= xfs_iext_lookup_extent(sc
->ip
, ifp
, lblk
, &icur
, &got
);
776 /* Scrub a whole directory. */
779 struct xfs_scrub
*sc
)
781 struct xchk_dir_ctx sdc
= {
782 .dir_iter
.actor
= xchk_dir_actor
,
790 if (!S_ISDIR(VFS_I(sc
->ip
)->i_mode
))
793 /* Plausible size? */
794 if (sc
->ip
->i_d
.di_size
< xfs_dir2_sf_hdr_size(0)) {
795 xchk_ino_set_corrupt(sc
, sc
->ip
->i_ino
);
799 /* Check directory tree structure */
800 error
= xchk_da_btree(sc
, XFS_DATA_FORK
, xchk_dir_rec
, NULL
);
804 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
807 /* Check the freespace. */
808 error
= xchk_directory_blocks(sc
);
812 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
816 * Check that every dirent we see can also be looked up by hash.
817 * Userspace usually asks for a 32k buffer, so we will too.
819 bufsize
= (size_t)min_t(loff_t
, XFS_READDIR_BUFSIZE
,
820 sc
->ip
->i_d
.di_size
);
823 * Look up every name in this directory by hash.
825 * Use the xfs_readdir function to call xchk_dir_actor on
826 * every directory entry in this directory. In _actor, we check
827 * the name, inode number, and ftype (if applicable) of the
828 * entry. xfs_readdir uses the VFS filldir functions to provide
831 * The VFS grabs a read or write lock via i_rwsem before it reads
832 * or writes to a directory. If we've gotten this far we've
833 * already obtained IOLOCK_EXCL, which (since 4.10) is the same as
834 * getting a write lock on i_rwsem. Therefore, it is safe for us
835 * to drop the ILOCK here in order to reuse the _readdir and
836 * _dir_lookup routines, which do their own ILOCK locking.
839 sc
->ilock_flags
&= ~XFS_ILOCK_EXCL
;
840 xfs_iunlock(sc
->ip
, XFS_ILOCK_EXCL
);
842 error
= xfs_readdir(sc
->tp
, sc
->ip
, &sdc
.dir_iter
, bufsize
);
843 if (!xchk_fblock_process_error(sc
, XFS_DATA_FORK
, 0,
846 if (oldpos
== sdc
.dir_iter
.pos
)
848 oldpos
= sdc
.dir_iter
.pos
;