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 error = xfs_iget(mp, sdc->sc->tp, inum, 0, 0, &ip);
71 if (!xchk_fblock_xref_process_error(sdc->sc, XFS_DATA_FORK, offset,
75 /* Convert mode to the DT_* values that dir_emit uses. */
76 ino_dtype = xfs_dir3_get_dtype(mp,
77 xfs_mode_to_ftype(VFS_I(ip)->i_mode));
78 if (ino_dtype != dtype)
79 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
86 * Scrub a single directory entry.
88 * We use the VFS directory iterator (i.e. readdir) to call this
89 * function for every directory entry in a directory. Once we're here,
90 * we check the inode number to make sure it's sane, then we check that
91 * we can look up this filename. Finally, we check the ftype.
95 struct dir_context *dir_iter,
102 struct xfs_mount *mp;
103 struct xfs_inode *ip;
104 struct xchk_dir_ctx *sdc;
105 struct xfs_name xname;
106 xfs_ino_t lookup_ino;
110 sdc = container_of(dir_iter, struct xchk_dir_ctx, dir_iter);
113 offset = xfs_dir2_db_to_da(mp->m_dir_geo,
114 xfs_dir2_dataptr_to_db(mp->m_dir_geo, pos));
116 /* Does this inode number make sense? */
117 if (!xfs_verify_dir_ino(mp, ino)) {
118 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
122 /* Does this name make sense? */
123 if (!xfs_dir2_namecheck(name, namelen)) {
124 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
128 if (!strncmp(".", name, namelen)) {
129 /* If this is "." then check that the inum matches the dir. */
130 if (xfs_sb_version_hasftype(&mp->m_sb) && type != DT_DIR)
131 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
133 if (ino != ip->i_ino)
134 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
136 } else if (!strncmp("..", name, namelen)) {
138 * If this is ".." in the root inode, check that the inum
141 if (xfs_sb_version_hasftype(&mp->m_sb) && type != DT_DIR)
142 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
144 if (ip->i_ino == mp->m_sb.sb_rootino && ino != ip->i_ino)
145 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
149 /* Verify that we can look up this name by hash. */
152 xname.type = XFS_DIR3_FT_UNKNOWN;
154 error = xfs_dir_lookup(sdc->sc->tp, ip, &xname, &lookup_ino, NULL);
155 if (!xchk_fblock_process_error(sdc->sc, XFS_DATA_FORK, offset,
158 if (lookup_ino != ino) {
159 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
163 /* Verify the file type. This function absorbs error codes. */
164 error = xchk_dir_check_ftype(sdc, offset, lookup_ino, type);
169 * A negative error code returned here is supposed to cause the
170 * dir_emit caller (xfs_readdir) to abort the directory iteration
171 * and return zero to xchk_directory.
173 if (error == 0 && sdc->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
174 return -EFSCORRUPTED;
178 /* Scrub a directory btree record. */
181 struct xchk_da_btree *ds,
185 struct xfs_mount *mp = ds->state->mp;
186 struct xfs_dir2_leaf_entry *ent = rec;
187 struct xfs_inode *dp = ds->dargs.dp;
188 struct xfs_dir2_data_entry *dent;
194 xfs_dir2_data_aoff_t off;
195 xfs_dir2_dataptr_t ptr;
196 xfs_dahash_t calc_hash;
201 /* Check the hash of the entry. */
202 error = xchk_da_btree_hash(ds, level, &ent->hashval);
206 /* Valid hash pointer? */
207 ptr = be32_to_cpu(ent->address);
211 /* Find the directory entry's location. */
212 db = xfs_dir2_dataptr_to_db(mp->m_dir_geo, ptr);
213 off = xfs_dir2_dataptr_to_off(mp->m_dir_geo, ptr);
214 rec_bno = xfs_dir2_db_to_da(mp->m_dir_geo, db);
216 if (rec_bno >= mp->m_dir_geo->leafblk) {
217 xchk_da_set_corrupt(ds, level);
220 error = xfs_dir3_data_read(ds->dargs.trans, dp, rec_bno, -2, &bp);
221 if (!xchk_fblock_process_error(ds->sc, XFS_DATA_FORK, rec_bno,
225 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
228 xchk_buffer_recheck(ds->sc, bp);
230 if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
233 dent = (struct xfs_dir2_data_entry *)(((char *)bp->b_addr) + off);
235 /* Make sure we got a real directory entry. */
236 p = (char *)mp->m_dir_inode_ops->data_entry_p(bp->b_addr);
237 endp = xfs_dir3_data_endp(mp->m_dir_geo, bp->b_addr);
239 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
243 struct xfs_dir2_data_entry *dep;
244 struct xfs_dir2_data_unused *dup;
246 dup = (struct xfs_dir2_data_unused *)p;
247 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
248 p += be16_to_cpu(dup->length);
251 dep = (struct xfs_dir2_data_entry *)p;
254 p += mp->m_dir_inode_ops->data_entsize(dep->namelen);
257 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
261 /* Retrieve the entry, sanity check it, and compare hashes. */
262 ino = be64_to_cpu(dent->inumber);
263 hash = be32_to_cpu(ent->hashval);
264 tag = be16_to_cpup(dp->d_ops->data_entry_tag_p(dent));
265 if (!xfs_verify_dir_ino(mp, ino) || tag != off)
266 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
267 if (dent->namelen == 0) {
268 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
271 calc_hash = xfs_da_hashname(dent->name, dent->namelen);
272 if (calc_hash != hash)
273 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
276 xfs_trans_brelse(ds->dargs.trans, bp);
282 * Is this unused entry either in the bestfree or smaller than all of
283 * them? We've already checked that the bestfrees are sorted longest to
284 * shortest, and that there aren't any bogus entries.
287 xchk_directory_check_free_entry(
288 struct xfs_scrub *sc,
290 struct xfs_dir2_data_free *bf,
291 struct xfs_dir2_data_unused *dup)
293 struct xfs_dir2_data_free *dfp;
294 unsigned int dup_length;
296 dup_length = be16_to_cpu(dup->length);
298 /* Unused entry is shorter than any of the bestfrees */
299 if (dup_length < be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
302 for (dfp = &bf[XFS_DIR2_DATA_FD_COUNT - 1]; dfp >= bf; dfp--)
303 if (dup_length == be16_to_cpu(dfp->length))
306 /* Unused entry should be in the bestfrees but wasn't found. */
307 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
310 /* Check free space info in a directory data block. */
312 xchk_directory_data_bestfree(
313 struct xfs_scrub *sc,
317 struct xfs_dir2_data_unused *dup;
318 struct xfs_dir2_data_free *dfp;
320 struct xfs_dir2_data_free *bf;
321 struct xfs_mount *mp = sc->mp;
322 const struct xfs_dir_ops *d_ops;
326 unsigned int nr_bestfrees = 0;
327 unsigned int nr_frees = 0;
328 unsigned int smallest_bestfree;
333 d_ops = sc->ip->d_ops;
336 /* dir block format */
337 if (lblk != XFS_B_TO_FSBT(mp, XFS_DIR2_DATA_OFFSET))
338 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
339 error = xfs_dir3_block_read(sc->tp, sc->ip, &bp);
341 /* dir data format */
342 error = xfs_dir3_data_read(sc->tp, sc->ip, lblk, -1, &bp);
344 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
346 xchk_buffer_recheck(sc, bp);
348 /* XXX: Check xfs_dir3_data_hdr.pad is zero once we start setting it. */
350 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
353 /* Do the bestfrees correspond to actual free space? */
354 bf = d_ops->data_bestfree_p(bp->b_addr);
355 smallest_bestfree = UINT_MAX;
356 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
357 offset = be16_to_cpu(dfp->offset);
360 if (offset >= mp->m_dir_geo->blksize) {
361 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
364 dup = (struct xfs_dir2_data_unused *)(bp->b_addr + offset);
365 tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
367 /* bestfree doesn't match the entry it points at? */
368 if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG) ||
369 be16_to_cpu(dup->length) != be16_to_cpu(dfp->length) ||
370 tag != ((char *)dup - (char *)bp->b_addr)) {
371 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
375 /* bestfree records should be ordered largest to smallest */
376 if (smallest_bestfree < be16_to_cpu(dfp->length)) {
377 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
381 smallest_bestfree = be16_to_cpu(dfp->length);
385 /* Make sure the bestfrees are actually the best free spaces. */
386 ptr = (char *)d_ops->data_entry_p(bp->b_addr);
387 endptr = xfs_dir3_data_endp(mp->m_dir_geo, bp->b_addr);
389 /* Iterate the entries, stopping when we hit or go past the end. */
390 while (ptr < endptr) {
391 dup = (struct xfs_dir2_data_unused *)ptr;
392 /* Skip real entries */
393 if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG)) {
394 struct xfs_dir2_data_entry *dep;
396 dep = (struct xfs_dir2_data_entry *)ptr;
397 newlen = d_ops->data_entsize(dep->namelen);
399 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
407 /* Spot check this free entry */
408 tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
409 if (tag != ((char *)dup - (char *)bp->b_addr)) {
410 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
415 * Either this entry is a bestfree or it's smaller than
416 * any of the bestfrees.
418 xchk_directory_check_free_entry(sc, lblk, bf, dup);
419 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
423 newlen = be16_to_cpu(dup->length);
425 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
433 /* We're required to fill all the space. */
435 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
437 /* Did we see at least as many free slots as there are bestfrees? */
438 if (nr_frees < nr_bestfrees)
439 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
441 xfs_trans_brelse(sc->tp, bp);
447 * Does the free space length in the free space index block ($len) match
448 * the longest length in the directory data block's bestfree array?
449 * Assume that we've already checked that the data block's bestfree
453 xchk_directory_check_freesp(
454 struct xfs_scrub *sc,
459 struct xfs_dir2_data_free *dfp;
461 dfp = sc->ip->d_ops->data_bestfree_p(dbp->b_addr);
463 if (len != be16_to_cpu(dfp->length))
464 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
466 if (len > 0 && be16_to_cpu(dfp->offset) == 0)
467 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
470 /* Check free space info in a directory leaf1 block. */
472 xchk_directory_leaf1_bestfree(
473 struct xfs_scrub *sc,
474 struct xfs_da_args *args,
477 struct xfs_dir3_icleaf_hdr leafhdr;
478 struct xfs_dir2_leaf_entry *ents;
479 struct xfs_dir2_leaf_tail *ltp;
480 struct xfs_dir2_leaf *leaf;
483 const struct xfs_dir_ops *d_ops = sc->ip->d_ops;
484 struct xfs_da_geometry *geo = sc->mp->m_dir_geo;
490 unsigned int stale = 0;
494 /* Read the free space block. */
495 error = xfs_dir3_leaf_read(sc->tp, sc->ip, lblk, -1, &bp);
496 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
498 xchk_buffer_recheck(sc, bp);
501 d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
502 ents = d_ops->leaf_ents_p(leaf);
503 ltp = xfs_dir2_leaf_tail_p(geo, leaf);
504 bestcount = be32_to_cpu(ltp->bestcount);
505 bestp = xfs_dir2_leaf_bests_p(ltp);
507 if (xfs_sb_version_hascrc(&sc->mp->m_sb)) {
508 struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
510 if (hdr3->pad != cpu_to_be32(0))
511 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
515 * There should be as many bestfree slots as there are dir data
516 * blocks that can fit under i_size.
518 if (bestcount != xfs_dir2_byte_to_db(geo, sc->ip->i_d.di_size)) {
519 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
523 /* Is the leaf count even remotely sane? */
524 if (leafhdr.count > d_ops->leaf_max_ents(geo)) {
525 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
529 /* Leaves and bests don't overlap in leaf format. */
530 if ((char *)&ents[leafhdr.count] > (char *)bestp) {
531 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
535 /* Check hash value order, count stale entries. */
536 for (i = 0; i < leafhdr.count; i++) {
537 hash = be32_to_cpu(ents[i].hashval);
538 if (i > 0 && lasthash > hash)
539 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
541 if (ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
544 if (leafhdr.stale != stale)
545 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
546 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
549 /* Check all the bestfree entries. */
550 for (i = 0; i < bestcount; i++, bestp++) {
551 best = be16_to_cpu(*bestp);
552 if (best == NULLDATAOFF)
554 error = xfs_dir3_data_read(sc->tp, sc->ip,
555 i * args->geo->fsbcount, -1, &dbp);
556 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
559 xchk_directory_check_freesp(sc, lblk, dbp, best);
560 xfs_trans_brelse(sc->tp, dbp);
561 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
568 /* Check free space info in a directory freespace block. */
570 xchk_directory_free_bestfree(
571 struct xfs_scrub *sc,
572 struct xfs_da_args *args,
575 struct xfs_dir3_icfree_hdr freehdr;
580 unsigned int stale = 0;
584 /* Read the free space block */
585 error = xfs_dir2_free_read(sc->tp, sc->ip, lblk, &bp);
586 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
588 xchk_buffer_recheck(sc, bp);
590 if (xfs_sb_version_hascrc(&sc->mp->m_sb)) {
591 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
593 if (hdr3->pad != cpu_to_be32(0))
594 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
597 /* Check all the entries. */
598 sc->ip->d_ops->free_hdr_from_disk(&freehdr, bp->b_addr);
599 bestp = sc->ip->d_ops->free_bests_p(bp->b_addr);
600 for (i = 0; i < freehdr.nvalid; i++, bestp++) {
601 best = be16_to_cpu(*bestp);
602 if (best == NULLDATAOFF) {
606 error = xfs_dir3_data_read(sc->tp, sc->ip,
607 (freehdr.firstdb + i) * args->geo->fsbcount,
609 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
612 xchk_directory_check_freesp(sc, lblk, dbp, best);
613 xfs_trans_brelse(sc->tp, dbp);
616 if (freehdr.nused + stale != freehdr.nvalid)
617 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
622 /* Check free space information in directories. */
624 xchk_directory_blocks(
625 struct xfs_scrub *sc)
627 struct xfs_bmbt_irec got;
628 struct xfs_da_args args;
629 struct xfs_ifork *ifp;
630 struct xfs_mount *mp = sc->mp;
631 xfs_fileoff_t leaf_lblk;
632 xfs_fileoff_t free_lblk;
634 struct xfs_iext_cursor icur;
640 /* Ignore local format directories. */
641 if (sc->ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
642 sc->ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
645 ifp = XFS_IFORK_PTR(sc->ip, XFS_DATA_FORK);
646 lblk = XFS_B_TO_FSB(mp, XFS_DIR2_DATA_OFFSET);
647 leaf_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_LEAF_OFFSET);
648 free_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_FREE_OFFSET);
650 /* Is this a block dir? */
652 args.geo = mp->m_dir_geo;
654 error = xfs_dir2_isblock(&args, &is_block);
655 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
658 /* Iterate all the data extents in the directory... */
659 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
660 while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
661 /* Block directories only have a single block at offset 0. */
663 (got.br_startoff > 0 ||
664 got.br_blockcount != args.geo->fsbcount)) {
665 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
670 /* No more data blocks... */
671 if (got.br_startoff >= leaf_lblk)
675 * Check each data block's bestfree data.
677 * Iterate all the fsbcount-aligned block offsets in
678 * this directory. The directory block reading code is
679 * smart enough to do its own bmap lookups to handle
680 * discontiguous directory blocks. When we're done
681 * with the extent record, re-query the bmap at the
682 * next fsbcount-aligned offset to avoid redundant
685 for (lblk = roundup((xfs_dablk_t)got.br_startoff,
687 lblk < got.br_startoff + got.br_blockcount;
688 lblk += args.geo->fsbcount) {
689 error = xchk_directory_data_bestfree(sc, lblk,
694 dabno = got.br_startoff + got.br_blockcount;
695 lblk = roundup(dabno, args.geo->fsbcount);
696 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
699 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
702 /* Look for a leaf1 block, which has free info. */
703 if (xfs_iext_lookup_extent(sc->ip, ifp, leaf_lblk, &icur, &got) &&
704 got.br_startoff == leaf_lblk &&
705 got.br_blockcount == args.geo->fsbcount &&
706 !xfs_iext_next_extent(ifp, &icur, &got)) {
708 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
711 error = xchk_directory_leaf1_bestfree(sc, &args,
717 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
720 /* Scan for free blocks */
722 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
723 while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
725 * Dirs can't have blocks mapped above 2^32.
726 * Single-block dirs shouldn't even be here.
728 lblk = got.br_startoff;
729 if (lblk & ~0xFFFFFFFFULL) {
730 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
734 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
739 * Check each dir free block's bestfree data.
741 * Iterate all the fsbcount-aligned block offsets in
742 * this directory. The directory block reading code is
743 * smart enough to do its own bmap lookups to handle
744 * discontiguous directory blocks. When we're done
745 * with the extent record, re-query the bmap at the
746 * next fsbcount-aligned offset to avoid redundant
749 for (lblk = roundup((xfs_dablk_t)got.br_startoff,
751 lblk < got.br_startoff + got.br_blockcount;
752 lblk += args.geo->fsbcount) {
753 error = xchk_directory_free_bestfree(sc, &args,
758 dabno = got.br_startoff + got.br_blockcount;
759 lblk = roundup(dabno, args.geo->fsbcount);
760 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
766 /* Scrub a whole directory. */
769 struct xfs_scrub *sc)
771 struct xchk_dir_ctx sdc = {
772 .dir_iter.actor = xchk_dir_actor,
780 if (!S_ISDIR(VFS_I(sc->ip)->i_mode))
783 /* Plausible size? */
784 if (sc->ip->i_d.di_size < xfs_dir2_sf_hdr_size(0)) {
785 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
789 /* Check directory tree structure */
790 error = xchk_da_btree(sc, XFS_DATA_FORK, xchk_dir_rec, NULL);
794 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
797 /* Check the freespace. */
798 error = xchk_directory_blocks(sc);
802 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
806 * Check that every dirent we see can also be looked up by hash.
807 * Userspace usually asks for a 32k buffer, so we will too.
809 bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE,
810 sc->ip->i_d.di_size);
813 * Look up every name in this directory by hash.
815 * Use the xfs_readdir function to call xchk_dir_actor on
816 * every directory entry in this directory. In _actor, we check
817 * the name, inode number, and ftype (if applicable) of the
818 * entry. xfs_readdir uses the VFS filldir functions to provide
821 * The VFS grabs a read or write lock via i_rwsem before it reads
822 * or writes to a directory. If we've gotten this far we've
823 * already obtained IOLOCK_EXCL, which (since 4.10) is the same as
824 * getting a write lock on i_rwsem. Therefore, it is safe for us
825 * to drop the ILOCK here in order to reuse the _readdir and
826 * _dir_lookup routines, which do their own ILOCK locking.
829 sc->ilock_flags &= ~XFS_ILOCK_EXCL;
830 xfs_iunlock(sc->ip, XFS_ILOCK_EXCL);
832 error = xfs_readdir(sc->tp, sc->ip, &sdc.dir_iter, bufsize);
833 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0,
836 if (oldpos == sdc.dir_iter.pos)
838 oldpos = sdc.dir_iter.pos;