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 if (!strncmp(".", name, namelen)) {
133 /* If this is "." then check that the inum matches the dir. */
134 if (xfs_sb_version_hasftype(&mp->m_sb) && type != DT_DIR)
135 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
137 if (ino != ip->i_ino)
138 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
140 } else if (!strncmp("..", name, namelen)) {
142 * If this is ".." in the root inode, check that the inum
145 if (xfs_sb_version_hasftype(&mp->m_sb) && type != DT_DIR)
146 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
148 if (ip->i_ino == mp->m_sb.sb_rootino && ino != ip->i_ino)
149 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
153 /* Verify that we can look up this name by hash. */
156 xname.type = XFS_DIR3_FT_UNKNOWN;
158 error = xfs_dir_lookup(sdc->sc->tp, ip, &xname, &lookup_ino, NULL);
159 /* ENOENT means the hash lookup failed and the dir is corrupt */
160 if (error == -ENOENT)
161 error = -EFSCORRUPTED;
162 if (!xchk_fblock_process_error(sdc->sc, XFS_DATA_FORK, offset,
165 if (lookup_ino != ino) {
166 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
170 /* Verify the file type. This function absorbs error codes. */
171 error = xchk_dir_check_ftype(sdc, offset, lookup_ino, type);
176 * A negative error code returned here is supposed to cause the
177 * dir_emit caller (xfs_readdir) to abort the directory iteration
178 * and return zero to xchk_directory.
180 if (error == 0 && sdc->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
181 return -EFSCORRUPTED;
185 /* Scrub a directory btree record. */
188 struct xchk_da_btree *ds,
192 struct xfs_mount *mp = ds->state->mp;
193 struct xfs_dir2_leaf_entry *ent = rec;
194 struct xfs_inode *dp = ds->dargs.dp;
195 struct xfs_dir2_data_entry *dent;
201 xfs_dir2_data_aoff_t off;
202 xfs_dir2_dataptr_t ptr;
203 xfs_dahash_t calc_hash;
208 /* Check the hash of the entry. */
209 error = xchk_da_btree_hash(ds, level, &ent->hashval);
213 /* Valid hash pointer? */
214 ptr = be32_to_cpu(ent->address);
218 /* Find the directory entry's location. */
219 db = xfs_dir2_dataptr_to_db(mp->m_dir_geo, ptr);
220 off = xfs_dir2_dataptr_to_off(mp->m_dir_geo, ptr);
221 rec_bno = xfs_dir2_db_to_da(mp->m_dir_geo, db);
223 if (rec_bno >= mp->m_dir_geo->leafblk) {
224 xchk_da_set_corrupt(ds, level);
227 error = xfs_dir3_data_read(ds->dargs.trans, dp, rec_bno, -2, &bp);
228 if (!xchk_fblock_process_error(ds->sc, XFS_DATA_FORK, rec_bno,
232 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
235 xchk_buffer_recheck(ds->sc, bp);
237 if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
240 dent = (struct xfs_dir2_data_entry *)(((char *)bp->b_addr) + off);
242 /* Make sure we got a real directory entry. */
243 p = (char *)mp->m_dir_inode_ops->data_entry_p(bp->b_addr);
244 endp = xfs_dir3_data_endp(mp->m_dir_geo, bp->b_addr);
246 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
250 struct xfs_dir2_data_entry *dep;
251 struct xfs_dir2_data_unused *dup;
253 dup = (struct xfs_dir2_data_unused *)p;
254 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
255 p += be16_to_cpu(dup->length);
258 dep = (struct xfs_dir2_data_entry *)p;
261 p += mp->m_dir_inode_ops->data_entsize(dep->namelen);
264 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
268 /* Retrieve the entry, sanity check it, and compare hashes. */
269 ino = be64_to_cpu(dent->inumber);
270 hash = be32_to_cpu(ent->hashval);
271 tag = be16_to_cpup(dp->d_ops->data_entry_tag_p(dent));
272 if (!xfs_verify_dir_ino(mp, ino) || tag != off)
273 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
274 if (dent->namelen == 0) {
275 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
278 calc_hash = xfs_da_hashname(dent->name, dent->namelen);
279 if (calc_hash != hash)
280 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
283 xfs_trans_brelse(ds->dargs.trans, bp);
289 * Is this unused entry either in the bestfree or smaller than all of
290 * them? We've already checked that the bestfrees are sorted longest to
291 * shortest, and that there aren't any bogus entries.
294 xchk_directory_check_free_entry(
295 struct xfs_scrub *sc,
297 struct xfs_dir2_data_free *bf,
298 struct xfs_dir2_data_unused *dup)
300 struct xfs_dir2_data_free *dfp;
301 unsigned int dup_length;
303 dup_length = be16_to_cpu(dup->length);
305 /* Unused entry is shorter than any of the bestfrees */
306 if (dup_length < be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
309 for (dfp = &bf[XFS_DIR2_DATA_FD_COUNT - 1]; dfp >= bf; dfp--)
310 if (dup_length == be16_to_cpu(dfp->length))
313 /* Unused entry should be in the bestfrees but wasn't found. */
314 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
317 /* Check free space info in a directory data block. */
319 xchk_directory_data_bestfree(
320 struct xfs_scrub *sc,
324 struct xfs_dir2_data_unused *dup;
325 struct xfs_dir2_data_free *dfp;
327 struct xfs_dir2_data_free *bf;
328 struct xfs_mount *mp = sc->mp;
329 const struct xfs_dir_ops *d_ops;
333 unsigned int nr_bestfrees = 0;
334 unsigned int nr_frees = 0;
335 unsigned int smallest_bestfree;
340 d_ops = sc->ip->d_ops;
343 /* dir block format */
344 if (lblk != XFS_B_TO_FSBT(mp, XFS_DIR2_DATA_OFFSET))
345 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
346 error = xfs_dir3_block_read(sc->tp, sc->ip, &bp);
348 /* dir data format */
349 error = xfs_dir3_data_read(sc->tp, sc->ip, lblk, -1, &bp);
351 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
353 xchk_buffer_recheck(sc, bp);
355 /* XXX: Check xfs_dir3_data_hdr.pad is zero once we start setting it. */
357 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
360 /* Do the bestfrees correspond to actual free space? */
361 bf = d_ops->data_bestfree_p(bp->b_addr);
362 smallest_bestfree = UINT_MAX;
363 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
364 offset = be16_to_cpu(dfp->offset);
367 if (offset >= mp->m_dir_geo->blksize) {
368 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
371 dup = (struct xfs_dir2_data_unused *)(bp->b_addr + offset);
372 tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
374 /* bestfree doesn't match the entry it points at? */
375 if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG) ||
376 be16_to_cpu(dup->length) != be16_to_cpu(dfp->length) ||
377 tag != ((char *)dup - (char *)bp->b_addr)) {
378 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
382 /* bestfree records should be ordered largest to smallest */
383 if (smallest_bestfree < be16_to_cpu(dfp->length)) {
384 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
388 smallest_bestfree = be16_to_cpu(dfp->length);
392 /* Make sure the bestfrees are actually the best free spaces. */
393 ptr = (char *)d_ops->data_entry_p(bp->b_addr);
394 endptr = xfs_dir3_data_endp(mp->m_dir_geo, bp->b_addr);
396 /* Iterate the entries, stopping when we hit or go past the end. */
397 while (ptr < endptr) {
398 dup = (struct xfs_dir2_data_unused *)ptr;
399 /* Skip real entries */
400 if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG)) {
401 struct xfs_dir2_data_entry *dep;
403 dep = (struct xfs_dir2_data_entry *)ptr;
404 newlen = d_ops->data_entsize(dep->namelen);
406 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
414 /* Spot check this free entry */
415 tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
416 if (tag != ((char *)dup - (char *)bp->b_addr)) {
417 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
422 * Either this entry is a bestfree or it's smaller than
423 * any of the bestfrees.
425 xchk_directory_check_free_entry(sc, lblk, bf, dup);
426 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
430 newlen = be16_to_cpu(dup->length);
432 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
440 /* We're required to fill all the space. */
442 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
444 /* Did we see at least as many free slots as there are bestfrees? */
445 if (nr_frees < nr_bestfrees)
446 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
448 xfs_trans_brelse(sc->tp, bp);
454 * Does the free space length in the free space index block ($len) match
455 * the longest length in the directory data block's bestfree array?
456 * Assume that we've already checked that the data block's bestfree
460 xchk_directory_check_freesp(
461 struct xfs_scrub *sc,
466 struct xfs_dir2_data_free *dfp;
468 dfp = sc->ip->d_ops->data_bestfree_p(dbp->b_addr);
470 if (len != be16_to_cpu(dfp->length))
471 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
473 if (len > 0 && be16_to_cpu(dfp->offset) == 0)
474 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
477 /* Check free space info in a directory leaf1 block. */
479 xchk_directory_leaf1_bestfree(
480 struct xfs_scrub *sc,
481 struct xfs_da_args *args,
484 struct xfs_dir3_icleaf_hdr leafhdr;
485 struct xfs_dir2_leaf_entry *ents;
486 struct xfs_dir2_leaf_tail *ltp;
487 struct xfs_dir2_leaf *leaf;
490 const struct xfs_dir_ops *d_ops = sc->ip->d_ops;
491 struct xfs_da_geometry *geo = sc->mp->m_dir_geo;
497 unsigned int stale = 0;
501 /* Read the free space block. */
502 error = xfs_dir3_leaf_read(sc->tp, sc->ip, lblk, -1, &bp);
503 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
505 xchk_buffer_recheck(sc, bp);
508 d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
509 ents = d_ops->leaf_ents_p(leaf);
510 ltp = xfs_dir2_leaf_tail_p(geo, leaf);
511 bestcount = be32_to_cpu(ltp->bestcount);
512 bestp = xfs_dir2_leaf_bests_p(ltp);
514 if (xfs_sb_version_hascrc(&sc->mp->m_sb)) {
515 struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
517 if (hdr3->pad != cpu_to_be32(0))
518 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
522 * There should be as many bestfree slots as there are dir data
523 * blocks that can fit under i_size.
525 if (bestcount != xfs_dir2_byte_to_db(geo, sc->ip->i_d.di_size)) {
526 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
530 /* Is the leaf count even remotely sane? */
531 if (leafhdr.count > d_ops->leaf_max_ents(geo)) {
532 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
536 /* Leaves and bests don't overlap in leaf format. */
537 if ((char *)&ents[leafhdr.count] > (char *)bestp) {
538 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
542 /* Check hash value order, count stale entries. */
543 for (i = 0; i < leafhdr.count; i++) {
544 hash = be32_to_cpu(ents[i].hashval);
545 if (i > 0 && lasthash > hash)
546 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
548 if (ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
551 if (leafhdr.stale != stale)
552 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
553 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
556 /* Check all the bestfree entries. */
557 for (i = 0; i < bestcount; i++, bestp++) {
558 best = be16_to_cpu(*bestp);
559 if (best == NULLDATAOFF)
561 error = xfs_dir3_data_read(sc->tp, sc->ip,
562 i * args->geo->fsbcount, -1, &dbp);
563 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
566 xchk_directory_check_freesp(sc, lblk, dbp, best);
567 xfs_trans_brelse(sc->tp, dbp);
568 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
575 /* Check free space info in a directory freespace block. */
577 xchk_directory_free_bestfree(
578 struct xfs_scrub *sc,
579 struct xfs_da_args *args,
582 struct xfs_dir3_icfree_hdr freehdr;
587 unsigned int stale = 0;
591 /* Read the free space block */
592 error = xfs_dir2_free_read(sc->tp, sc->ip, lblk, &bp);
593 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
595 xchk_buffer_recheck(sc, bp);
597 if (xfs_sb_version_hascrc(&sc->mp->m_sb)) {
598 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
600 if (hdr3->pad != cpu_to_be32(0))
601 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
604 /* Check all the entries. */
605 sc->ip->d_ops->free_hdr_from_disk(&freehdr, bp->b_addr);
606 bestp = sc->ip->d_ops->free_bests_p(bp->b_addr);
607 for (i = 0; i < freehdr.nvalid; i++, bestp++) {
608 best = be16_to_cpu(*bestp);
609 if (best == NULLDATAOFF) {
613 error = xfs_dir3_data_read(sc->tp, sc->ip,
614 (freehdr.firstdb + i) * args->geo->fsbcount,
616 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
619 xchk_directory_check_freesp(sc, lblk, dbp, best);
620 xfs_trans_brelse(sc->tp, dbp);
623 if (freehdr.nused + stale != freehdr.nvalid)
624 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
629 /* Check free space information in directories. */
631 xchk_directory_blocks(
632 struct xfs_scrub *sc)
634 struct xfs_bmbt_irec got;
635 struct xfs_da_args args;
636 struct xfs_ifork *ifp;
637 struct xfs_mount *mp = sc->mp;
638 xfs_fileoff_t leaf_lblk;
639 xfs_fileoff_t free_lblk;
641 struct xfs_iext_cursor icur;
647 /* Ignore local format directories. */
648 if (sc->ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
649 sc->ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
652 ifp = XFS_IFORK_PTR(sc->ip, XFS_DATA_FORK);
653 lblk = XFS_B_TO_FSB(mp, XFS_DIR2_DATA_OFFSET);
654 leaf_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_LEAF_OFFSET);
655 free_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_FREE_OFFSET);
657 /* Is this a block dir? */
659 args.geo = mp->m_dir_geo;
661 error = xfs_dir2_isblock(&args, &is_block);
662 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
665 /* Iterate all the data extents in the directory... */
666 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
667 while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
668 /* Block directories only have a single block at offset 0. */
670 (got.br_startoff > 0 ||
671 got.br_blockcount != args.geo->fsbcount)) {
672 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
677 /* No more data blocks... */
678 if (got.br_startoff >= leaf_lblk)
682 * Check each data block's bestfree data.
684 * Iterate all the fsbcount-aligned block offsets in
685 * this directory. The directory block reading code is
686 * smart enough to do its own bmap lookups to handle
687 * discontiguous directory blocks. When we're done
688 * with the extent record, re-query the bmap at the
689 * next fsbcount-aligned offset to avoid redundant
692 for (lblk = roundup((xfs_dablk_t)got.br_startoff,
694 lblk < got.br_startoff + got.br_blockcount;
695 lblk += args.geo->fsbcount) {
696 error = xchk_directory_data_bestfree(sc, lblk,
701 dabno = got.br_startoff + got.br_blockcount;
702 lblk = roundup(dabno, args.geo->fsbcount);
703 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
706 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
709 /* Look for a leaf1 block, which has free info. */
710 if (xfs_iext_lookup_extent(sc->ip, ifp, leaf_lblk, &icur, &got) &&
711 got.br_startoff == leaf_lblk &&
712 got.br_blockcount == args.geo->fsbcount &&
713 !xfs_iext_next_extent(ifp, &icur, &got)) {
715 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
718 error = xchk_directory_leaf1_bestfree(sc, &args,
724 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
727 /* Scan for free blocks */
729 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
730 while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
732 * Dirs can't have blocks mapped above 2^32.
733 * Single-block dirs shouldn't even be here.
735 lblk = got.br_startoff;
736 if (lblk & ~0xFFFFFFFFULL) {
737 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
741 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
746 * Check each dir free block's bestfree data.
748 * Iterate all the fsbcount-aligned block offsets in
749 * this directory. The directory block reading code is
750 * smart enough to do its own bmap lookups to handle
751 * discontiguous directory blocks. When we're done
752 * with the extent record, re-query the bmap at the
753 * next fsbcount-aligned offset to avoid redundant
756 for (lblk = roundup((xfs_dablk_t)got.br_startoff,
758 lblk < got.br_startoff + got.br_blockcount;
759 lblk += args.geo->fsbcount) {
760 error = xchk_directory_free_bestfree(sc, &args,
765 dabno = got.br_startoff + got.br_blockcount;
766 lblk = roundup(dabno, args.geo->fsbcount);
767 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
773 /* Scrub a whole directory. */
776 struct xfs_scrub *sc)
778 struct xchk_dir_ctx sdc = {
779 .dir_iter.actor = xchk_dir_actor,
787 if (!S_ISDIR(VFS_I(sc->ip)->i_mode))
790 /* Plausible size? */
791 if (sc->ip->i_d.di_size < xfs_dir2_sf_hdr_size(0)) {
792 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
796 /* Check directory tree structure */
797 error = xchk_da_btree(sc, XFS_DATA_FORK, xchk_dir_rec, NULL);
801 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
804 /* Check the freespace. */
805 error = xchk_directory_blocks(sc);
809 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
813 * Check that every dirent we see can also be looked up by hash.
814 * Userspace usually asks for a 32k buffer, so we will too.
816 bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE,
817 sc->ip->i_d.di_size);
820 * Look up every name in this directory by hash.
822 * Use the xfs_readdir function to call xchk_dir_actor on
823 * every directory entry in this directory. In _actor, we check
824 * the name, inode number, and ftype (if applicable) of the
825 * entry. xfs_readdir uses the VFS filldir functions to provide
828 * The VFS grabs a read or write lock via i_rwsem before it reads
829 * or writes to a directory. If we've gotten this far we've
830 * already obtained IOLOCK_EXCL, which (since 4.10) is the same as
831 * getting a write lock on i_rwsem. Therefore, it is safe for us
832 * to drop the ILOCK here in order to reuse the _readdir and
833 * _dir_lookup routines, which do their own ILOCK locking.
836 sc->ilock_flags &= ~XFS_ILOCK_EXCL;
837 xfs_iunlock(sc->ip, XFS_ILOCK_EXCL);
839 error = xfs_readdir(sc->tp, sc->ip, &sdc.dir_iter, bufsize);
840 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0,
843 if (oldpos == sdc.dir_iter.pos)
845 oldpos = sdc.dir_iter.pos;