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. */
27 return xchk_setup_inode_contents(sc, 0);
32 /* Scrub a directory entry. */
35 /* VFS fill-directory iterator */
36 struct dir_context dir_iter;
41 /* Check that an inode's mode matches a given DT_ type. */
44 struct xchk_dir_ctx *sdc,
49 struct xfs_mount *mp = sdc->sc->mp;
54 if (!xfs_has_ftype(mp)) {
55 if (dtype != DT_UNKNOWN && dtype != DT_DIR)
56 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
62 * Grab the inode pointed to by the dirent. We release the
63 * inode before we cancel the scrub transaction. Since we're
64 * don't know a priori that releasing the inode won't trigger
65 * eofblocks cleanup (which allocates what would be a nested
66 * transaction), we can't use DONTCACHE here because DONTCACHE
67 * inodes can trigger immediate inactive cleanup of the inode.
69 * If _iget returns -EINVAL or -ENOENT then the child inode number is
70 * garbage and the directory is corrupt. If the _iget returns
71 * -EFSCORRUPTED or -EFSBADCRC then the child is corrupt which is a
72 * cross referencing error. Any other error is an operational error.
74 error = xfs_iget(mp, sdc->sc->tp, inum, 0, 0, &ip);
75 if (error == -EINVAL || error == -ENOENT) {
76 error = -EFSCORRUPTED;
77 xchk_fblock_process_error(sdc->sc, XFS_DATA_FORK, 0, &error);
80 if (!xchk_fblock_xref_process_error(sdc->sc, XFS_DATA_FORK, offset,
84 /* Convert mode to the DT_* values that dir_emit uses. */
85 ino_dtype = xfs_dir3_get_dtype(mp,
86 xfs_mode_to_ftype(VFS_I(ip)->i_mode));
87 if (ino_dtype != dtype)
88 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
95 * Scrub a single directory entry.
97 * We use the VFS directory iterator (i.e. readdir) to call this
98 * function for every directory entry in a directory. Once we're here,
99 * we check the inode number to make sure it's sane, then we check that
100 * we can look up this filename. Finally, we check the ftype.
104 struct dir_context *dir_iter,
111 struct xfs_mount *mp;
112 struct xfs_inode *ip;
113 struct xchk_dir_ctx *sdc;
114 struct xfs_name xname;
115 xfs_ino_t lookup_ino;
117 bool checked_ftype = false;
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 if (xchk_should_terminate(sdc->sc, &error))
129 /* Does this inode number make sense? */
130 if (!xfs_verify_dir_ino(mp, ino)) {
131 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
135 /* Does this name make sense? */
136 if (!xfs_dir2_namecheck(name, namelen)) {
137 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
141 if (!strncmp(".", name, namelen)) {
142 /* If this is "." then check that the inum matches the dir. */
143 if (xfs_has_ftype(mp) && type != DT_DIR)
144 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
146 checked_ftype = true;
147 if (ino != ip->i_ino)
148 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
150 } else if (!strncmp("..", name, namelen)) {
152 * If this is ".." in the root inode, check that the inum
155 if (xfs_has_ftype(mp) && type != DT_DIR)
156 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
158 checked_ftype = true;
159 if (ip->i_ino == mp->m_sb.sb_rootino && ino != ip->i_ino)
160 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
164 /* Verify that we can look up this name by hash. */
167 xname.type = XFS_DIR3_FT_UNKNOWN;
169 error = xfs_dir_lookup(sdc->sc->tp, ip, &xname, &lookup_ino, NULL);
170 /* ENOENT means the hash lookup failed and the dir is corrupt */
171 if (error == -ENOENT)
172 error = -EFSCORRUPTED;
173 if (!xchk_fblock_process_error(sdc->sc, XFS_DATA_FORK, offset,
176 if (lookup_ino != ino) {
177 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
181 /* Verify the file type. This function absorbs error codes. */
182 if (!checked_ftype) {
183 error = xchk_dir_check_ftype(sdc, offset, lookup_ino, type);
189 * A negative error code returned here is supposed to cause the
190 * dir_emit caller (xfs_readdir) to abort the directory iteration
191 * and return zero to xchk_directory.
193 if (error == 0 && sdc->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
194 return -EFSCORRUPTED;
198 /* Scrub a directory btree record. */
201 struct xchk_da_btree *ds,
204 struct xfs_da_state_blk *blk = &ds->state->path.blk[level];
205 struct xfs_mount *mp = ds->state->mp;
206 struct xfs_inode *dp = ds->dargs.dp;
207 struct xfs_da_geometry *geo = mp->m_dir_geo;
208 struct xfs_dir2_data_entry *dent;
210 struct xfs_dir2_leaf_entry *ent;
212 unsigned int iter_off;
216 xfs_dir2_data_aoff_t off;
217 xfs_dir2_dataptr_t ptr;
218 xfs_dahash_t calc_hash;
220 struct xfs_dir3_icleaf_hdr hdr;
224 ASSERT(blk->magic == XFS_DIR2_LEAF1_MAGIC ||
225 blk->magic == XFS_DIR2_LEAFN_MAGIC);
227 xfs_dir2_leaf_hdr_from_disk(mp, &hdr, blk->bp->b_addr);
228 ent = hdr.ents + blk->index;
230 /* Check the hash of the entry. */
231 error = xchk_da_btree_hash(ds, level, &ent->hashval);
235 /* Valid hash pointer? */
236 ptr = be32_to_cpu(ent->address);
240 /* Find the directory entry's location. */
241 db = xfs_dir2_dataptr_to_db(geo, ptr);
242 off = xfs_dir2_dataptr_to_off(geo, ptr);
243 rec_bno = xfs_dir2_db_to_da(geo, db);
245 if (rec_bno >= geo->leafblk) {
246 xchk_da_set_corrupt(ds, level);
249 error = xfs_dir3_data_read(ds->dargs.trans, dp, rec_bno,
250 XFS_DABUF_MAP_HOLE_OK, &bp);
251 if (!xchk_fblock_process_error(ds->sc, XFS_DATA_FORK, rec_bno,
255 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
258 xchk_buffer_recheck(ds->sc, bp);
260 if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
263 dent = bp->b_addr + off;
265 /* Make sure we got a real directory entry. */
266 iter_off = geo->data_entry_offset;
267 end = xfs_dir3_data_end_offset(geo, bp->b_addr);
269 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
273 struct xfs_dir2_data_entry *dep = bp->b_addr + iter_off;
274 struct xfs_dir2_data_unused *dup = bp->b_addr + iter_off;
276 if (iter_off >= end) {
277 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
281 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
282 iter_off += be16_to_cpu(dup->length);
287 iter_off += xfs_dir2_data_entsize(mp, dep->namelen);
290 /* Retrieve the entry, sanity check it, and compare hashes. */
291 ino = be64_to_cpu(dent->inumber);
292 hash = be32_to_cpu(ent->hashval);
293 tag = be16_to_cpup(xfs_dir2_data_entry_tag_p(mp, dent));
294 if (!xfs_verify_dir_ino(mp, ino) || tag != off)
295 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
296 if (dent->namelen == 0) {
297 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
300 calc_hash = xfs_da_hashname(dent->name, dent->namelen);
301 if (calc_hash != hash)
302 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
305 xfs_trans_brelse(ds->dargs.trans, bp);
311 * Is this unused entry either in the bestfree or smaller than all of
312 * them? We've already checked that the bestfrees are sorted longest to
313 * shortest, and that there aren't any bogus entries.
316 xchk_directory_check_free_entry(
317 struct xfs_scrub *sc,
319 struct xfs_dir2_data_free *bf,
320 struct xfs_dir2_data_unused *dup)
322 struct xfs_dir2_data_free *dfp;
323 unsigned int dup_length;
325 dup_length = be16_to_cpu(dup->length);
327 /* Unused entry is shorter than any of the bestfrees */
328 if (dup_length < be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
331 for (dfp = &bf[XFS_DIR2_DATA_FD_COUNT - 1]; dfp >= bf; dfp--)
332 if (dup_length == be16_to_cpu(dfp->length))
335 /* Unused entry should be in the bestfrees but wasn't found. */
336 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
339 /* Check free space info in a directory data block. */
341 xchk_directory_data_bestfree(
342 struct xfs_scrub *sc,
346 struct xfs_dir2_data_unused *dup;
347 struct xfs_dir2_data_free *dfp;
349 struct xfs_dir2_data_free *bf;
350 struct xfs_mount *mp = sc->mp;
352 unsigned int nr_bestfrees = 0;
353 unsigned int nr_frees = 0;
354 unsigned int smallest_bestfree;
361 /* dir block format */
362 if (lblk != XFS_B_TO_FSBT(mp, XFS_DIR2_DATA_OFFSET))
363 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
364 error = xfs_dir3_block_read(sc->tp, sc->ip, &bp);
366 /* dir data format */
367 error = xfs_dir3_data_read(sc->tp, sc->ip, lblk, 0, &bp);
369 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
371 xchk_buffer_recheck(sc, bp);
373 /* XXX: Check xfs_dir3_data_hdr.pad is zero once we start setting it. */
375 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
378 /* Do the bestfrees correspond to actual free space? */
379 bf = xfs_dir2_data_bestfree_p(mp, bp->b_addr);
380 smallest_bestfree = UINT_MAX;
381 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
382 offset = be16_to_cpu(dfp->offset);
385 if (offset >= mp->m_dir_geo->blksize) {
386 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
389 dup = bp->b_addr + offset;
390 tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
392 /* bestfree doesn't match the entry it points at? */
393 if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG) ||
394 be16_to_cpu(dup->length) != be16_to_cpu(dfp->length) ||
396 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
400 /* bestfree records should be ordered largest to smallest */
401 if (smallest_bestfree < be16_to_cpu(dfp->length)) {
402 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
406 smallest_bestfree = be16_to_cpu(dfp->length);
410 /* Make sure the bestfrees are actually the best free spaces. */
411 offset = mp->m_dir_geo->data_entry_offset;
412 end = xfs_dir3_data_end_offset(mp->m_dir_geo, bp->b_addr);
414 /* Iterate the entries, stopping when we hit or go past the end. */
415 while (offset < end) {
416 dup = bp->b_addr + offset;
418 /* Skip real entries */
419 if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG)) {
420 struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
422 newlen = xfs_dir2_data_entsize(mp, dep->namelen);
424 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
432 /* Spot check this free entry */
433 tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
435 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
440 * Either this entry is a bestfree or it's smaller than
441 * any of the bestfrees.
443 xchk_directory_check_free_entry(sc, lblk, bf, dup);
444 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
448 newlen = be16_to_cpu(dup->length);
450 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
458 /* We're required to fill all the space. */
460 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
462 /* Did we see at least as many free slots as there are bestfrees? */
463 if (nr_frees < nr_bestfrees)
464 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
466 xfs_trans_brelse(sc->tp, bp);
472 * Does the free space length in the free space index block ($len) match
473 * the longest length in the directory data block's bestfree array?
474 * Assume that we've already checked that the data block's bestfree
478 xchk_directory_check_freesp(
479 struct xfs_scrub *sc,
484 struct xfs_dir2_data_free *dfp;
486 dfp = xfs_dir2_data_bestfree_p(sc->mp, dbp->b_addr);
488 if (len != be16_to_cpu(dfp->length))
489 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
491 if (len > 0 && be16_to_cpu(dfp->offset) == 0)
492 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
495 /* Check free space info in a directory leaf1 block. */
497 xchk_directory_leaf1_bestfree(
498 struct xfs_scrub *sc,
499 struct xfs_da_args *args,
500 xfs_dir2_db_t last_data_db,
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_has_crc(sc->mp)) {
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 must be enough bestfree slots to cover all the directory data
539 * blocks that we scanned. It is possible for there to be a hole
540 * between the last data block and i_disk_size. This seems like an
541 * oversight to the scrub author, but as we have been writing out
542 * directories like this (and xfs_repair doesn't mind them) for years,
543 * that's what we have to check.
545 if (bestcount != last_data_db + 1) {
546 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
550 /* Is the leaf count even remotely sane? */
551 if (leafhdr.count > geo->leaf_max_ents) {
552 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
556 /* Leaves and bests don't overlap in leaf format. */
557 if ((char *)&leafhdr.ents[leafhdr.count] > (char *)bestp) {
558 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
562 /* Check hash value order, count stale entries. */
563 for (i = 0; i < leafhdr.count; i++) {
564 hash = be32_to_cpu(leafhdr.ents[i].hashval);
565 if (i > 0 && lasthash > hash)
566 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
568 if (leafhdr.ents[i].address ==
569 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
572 if (leafhdr.stale != stale)
573 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
574 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
577 /* Check all the bestfree entries. */
578 for (i = 0; i < bestcount; i++, bestp++) {
579 best = be16_to_cpu(*bestp);
580 error = xfs_dir3_data_read(sc->tp, sc->ip,
581 xfs_dir2_db_to_da(args->geo, i),
582 XFS_DABUF_MAP_HOLE_OK,
584 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
589 if (best != NULLDATAOFF) {
590 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
597 if (best == NULLDATAOFF)
598 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
600 xchk_directory_check_freesp(sc, lblk, dbp, best);
601 xfs_trans_brelse(sc->tp, dbp);
602 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
606 xfs_trans_brelse(sc->tp, bp);
610 /* Check free space info in a directory freespace block. */
612 xchk_directory_free_bestfree(
613 struct xfs_scrub *sc,
614 struct xfs_da_args *args,
617 struct xfs_dir3_icfree_hdr freehdr;
621 unsigned int stale = 0;
625 /* Read the free space block */
626 error = xfs_dir2_free_read(sc->tp, sc->ip, lblk, &bp);
627 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
629 xchk_buffer_recheck(sc, bp);
631 if (xfs_has_crc(sc->mp)) {
632 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
634 if (hdr3->pad != cpu_to_be32(0))
635 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
638 /* Check all the entries. */
639 xfs_dir2_free_hdr_from_disk(sc->ip->i_mount, &freehdr, bp->b_addr);
640 for (i = 0; i < freehdr.nvalid; i++) {
641 best = be16_to_cpu(freehdr.bests[i]);
642 if (best == NULLDATAOFF) {
646 error = xfs_dir3_data_read(sc->tp, sc->ip,
647 (freehdr.firstdb + i) * args->geo->fsbcount,
649 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
652 xchk_directory_check_freesp(sc, lblk, dbp, best);
653 xfs_trans_brelse(sc->tp, dbp);
656 if (freehdr.nused + stale != freehdr.nvalid)
657 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
659 xfs_trans_brelse(sc->tp, bp);
663 /* Check free space information in directories. */
665 xchk_directory_blocks(
666 struct xfs_scrub *sc)
668 struct xfs_bmbt_irec got;
669 struct xfs_da_args args;
670 struct xfs_ifork *ifp = xfs_ifork_ptr(sc->ip, XFS_DATA_FORK);
671 struct xfs_mount *mp = sc->mp;
672 xfs_fileoff_t leaf_lblk;
673 xfs_fileoff_t free_lblk;
675 struct xfs_iext_cursor icur;
677 xfs_dir2_db_t last_data_db = 0;
679 bool is_block = false;
682 /* Ignore local format directories. */
683 if (ifp->if_format != XFS_DINODE_FMT_EXTENTS &&
684 ifp->if_format != XFS_DINODE_FMT_BTREE)
687 lblk = XFS_B_TO_FSB(mp, XFS_DIR2_DATA_OFFSET);
688 leaf_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_LEAF_OFFSET);
689 free_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_FREE_OFFSET);
691 /* Is this a block dir? */
693 args.geo = mp->m_dir_geo;
695 error = xfs_dir2_isblock(&args, &is_block);
696 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
699 /* Iterate all the data extents in the directory... */
700 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
701 while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
702 /* No more data blocks... */
703 if (got.br_startoff >= leaf_lblk)
707 * Check each data block's bestfree data.
709 * Iterate all the fsbcount-aligned block offsets in
710 * this directory. The directory block reading code is
711 * smart enough to do its own bmap lookups to handle
712 * discontiguous directory blocks. When we're done
713 * with the extent record, re-query the bmap at the
714 * next fsbcount-aligned offset to avoid redundant
717 for (lblk = roundup((xfs_dablk_t)got.br_startoff,
719 lblk < got.br_startoff + got.br_blockcount;
720 lblk += args.geo->fsbcount) {
721 last_data_db = xfs_dir2_da_to_db(args.geo, lblk);
722 error = xchk_directory_data_bestfree(sc, lblk,
727 dabno = got.br_startoff + got.br_blockcount;
728 lblk = roundup(dabno, args.geo->fsbcount);
729 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
732 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
735 /* Look for a leaf1 block, which has free info. */
736 if (xfs_iext_lookup_extent(sc->ip, ifp, leaf_lblk, &icur, &got) &&
737 got.br_startoff == leaf_lblk &&
738 got.br_blockcount == args.geo->fsbcount &&
739 !xfs_iext_next_extent(ifp, &icur, &got)) {
741 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
744 error = xchk_directory_leaf1_bestfree(sc, &args, last_data_db,
750 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
753 /* Scan for free blocks */
755 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
756 while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
758 * Dirs can't have blocks mapped above 2^32.
759 * Single-block dirs shouldn't even be here.
761 lblk = got.br_startoff;
762 if (lblk & ~0xFFFFFFFFULL) {
763 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
767 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
772 * Check each dir free block's bestfree data.
774 * Iterate all the fsbcount-aligned block offsets in
775 * this directory. The directory block reading code is
776 * smart enough to do its own bmap lookups to handle
777 * discontiguous directory blocks. When we're done
778 * with the extent record, re-query the bmap at the
779 * next fsbcount-aligned offset to avoid redundant
782 for (lblk = roundup((xfs_dablk_t)got.br_startoff,
784 lblk < got.br_startoff + got.br_blockcount;
785 lblk += args.geo->fsbcount) {
786 error = xchk_directory_free_bestfree(sc, &args,
791 dabno = got.br_startoff + got.br_blockcount;
792 lblk = roundup(dabno, args.geo->fsbcount);
793 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
799 /* Scrub a whole directory. */
802 struct xfs_scrub *sc)
804 struct xchk_dir_ctx sdc = {
805 .dir_iter.actor = xchk_dir_actor,
813 if (!S_ISDIR(VFS_I(sc->ip)->i_mode))
816 /* Plausible size? */
817 if (sc->ip->i_disk_size < xfs_dir2_sf_hdr_size(0)) {
818 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
822 /* Check directory tree structure */
823 error = xchk_da_btree(sc, XFS_DATA_FORK, xchk_dir_rec, NULL);
827 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
830 /* Check the freespace. */
831 error = xchk_directory_blocks(sc);
835 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
839 * Check that every dirent we see can also be looked up by hash.
840 * Userspace usually asks for a 32k buffer, so we will too.
842 bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE,
843 sc->ip->i_disk_size);
846 * Look up every name in this directory by hash.
848 * Use the xfs_readdir function to call xchk_dir_actor on
849 * every directory entry in this directory. In _actor, we check
850 * the name, inode number, and ftype (if applicable) of the
851 * entry. xfs_readdir uses the VFS filldir functions to provide
854 * The VFS grabs a read or write lock via i_rwsem before it reads
855 * or writes to a directory. If we've gotten this far we've
856 * already obtained IOLOCK_EXCL, which (since 4.10) is the same as
857 * getting a write lock on i_rwsem. Therefore, it is safe for us
858 * to drop the ILOCK here in order to reuse the _readdir and
859 * _dir_lookup routines, which do their own ILOCK locking.
862 sc->ilock_flags &= ~XFS_ILOCK_EXCL;
863 xfs_iunlock(sc->ip, XFS_ILOCK_EXCL);
865 error = xfs_readdir(sc->tp, sc->ip, &sdc.dir_iter, bufsize);
866 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0,
869 if (oldpos == sdc.dir_iter.pos)
871 oldpos = sdc.dir_iter.pos;