1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Oracle. All Rights Reserved.
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 if (xchk_should_terminate(sdc->sc, &error))
119 /* Does this inode number make sense? */
120 if (!xfs_verify_dir_ino(mp, ino)) {
121 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
125 /* Does this name make sense? */
126 if (!xfs_dir2_namecheck(name, namelen)) {
127 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
131 if (!strncmp(".", name, namelen)) {
132 /* If this is "." then check that the inum matches the dir. */
133 if (xfs_sb_version_hasftype(&mp->m_sb) && type != DT_DIR)
134 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
136 if (ino != ip->i_ino)
137 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
139 } else if (!strncmp("..", name, namelen)) {
141 * If this is ".." in the root inode, check that the inum
144 if (xfs_sb_version_hasftype(&mp->m_sb) && type != DT_DIR)
145 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
147 if (ip->i_ino == mp->m_sb.sb_rootino && ino != ip->i_ino)
148 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
152 /* Verify that we can look up this name by hash. */
155 xname.type = XFS_DIR3_FT_UNKNOWN;
157 error = xfs_dir_lookup(sdc->sc->tp, ip, &xname, &lookup_ino, NULL);
158 /* ENOENT means the hash lookup failed and the dir is corrupt */
159 if (error == -ENOENT)
160 error = -EFSCORRUPTED;
161 if (!xchk_fblock_process_error(sdc->sc, XFS_DATA_FORK, offset,
164 if (lookup_ino != ino) {
165 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
169 /* Verify the file type. This function absorbs error codes. */
170 error = xchk_dir_check_ftype(sdc, offset, lookup_ino, type);
175 * A negative error code returned here is supposed to cause the
176 * dir_emit caller (xfs_readdir) to abort the directory iteration
177 * and return zero to xchk_directory.
179 if (error == 0 && sdc->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
180 return -EFSCORRUPTED;
184 /* Scrub a directory btree record. */
187 struct xchk_da_btree *ds,
190 struct xfs_da_state_blk *blk = &ds->state->path.blk[level];
191 struct xfs_mount *mp = ds->state->mp;
192 struct xfs_inode *dp = ds->dargs.dp;
193 struct xfs_da_geometry *geo = mp->m_dir_geo;
194 struct xfs_dir2_data_entry *dent;
196 struct xfs_dir2_leaf_entry *ent;
198 unsigned int iter_off;
202 xfs_dir2_data_aoff_t off;
203 xfs_dir2_dataptr_t ptr;
204 xfs_dahash_t calc_hash;
206 struct xfs_dir3_icleaf_hdr hdr;
210 ASSERT(blk->magic == XFS_DIR2_LEAF1_MAGIC ||
211 blk->magic == XFS_DIR2_LEAFN_MAGIC);
213 xfs_dir2_leaf_hdr_from_disk(mp, &hdr, blk->bp->b_addr);
214 ent = hdr.ents + blk->index;
216 /* Check the hash of the entry. */
217 error = xchk_da_btree_hash(ds, level, &ent->hashval);
221 /* Valid hash pointer? */
222 ptr = be32_to_cpu(ent->address);
226 /* Find the directory entry's location. */
227 db = xfs_dir2_dataptr_to_db(geo, ptr);
228 off = xfs_dir2_dataptr_to_off(geo, ptr);
229 rec_bno = xfs_dir2_db_to_da(geo, db);
231 if (rec_bno >= geo->leafblk) {
232 xchk_da_set_corrupt(ds, level);
235 error = xfs_dir3_data_read(ds->dargs.trans, dp, rec_bno,
236 XFS_DABUF_MAP_HOLE_OK, &bp);
237 if (!xchk_fblock_process_error(ds->sc, XFS_DATA_FORK, rec_bno,
241 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
244 xchk_buffer_recheck(ds->sc, bp);
246 if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
249 dent = bp->b_addr + off;
251 /* Make sure we got a real directory entry. */
252 iter_off = geo->data_entry_offset;
253 end = xfs_dir3_data_end_offset(geo, bp->b_addr);
255 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
259 struct xfs_dir2_data_entry *dep = bp->b_addr + iter_off;
260 struct xfs_dir2_data_unused *dup = bp->b_addr + iter_off;
262 if (iter_off >= end) {
263 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
267 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
268 iter_off += be16_to_cpu(dup->length);
273 iter_off += xfs_dir2_data_entsize(mp, dep->namelen);
276 /* Retrieve the entry, sanity check it, and compare hashes. */
277 ino = be64_to_cpu(dent->inumber);
278 hash = be32_to_cpu(ent->hashval);
279 tag = be16_to_cpup(xfs_dir2_data_entry_tag_p(mp, dent));
280 if (!xfs_verify_dir_ino(mp, ino) || tag != off)
281 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
282 if (dent->namelen == 0) {
283 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
286 calc_hash = xfs_da_hashname(dent->name, dent->namelen);
287 if (calc_hash != hash)
288 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
291 xfs_trans_brelse(ds->dargs.trans, bp);
297 * Is this unused entry either in the bestfree or smaller than all of
298 * them? We've already checked that the bestfrees are sorted longest to
299 * shortest, and that there aren't any bogus entries.
302 xchk_directory_check_free_entry(
303 struct xfs_scrub *sc,
305 struct xfs_dir2_data_free *bf,
306 struct xfs_dir2_data_unused *dup)
308 struct xfs_dir2_data_free *dfp;
309 unsigned int dup_length;
311 dup_length = be16_to_cpu(dup->length);
313 /* Unused entry is shorter than any of the bestfrees */
314 if (dup_length < be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
317 for (dfp = &bf[XFS_DIR2_DATA_FD_COUNT - 1]; dfp >= bf; dfp--)
318 if (dup_length == be16_to_cpu(dfp->length))
321 /* Unused entry should be in the bestfrees but wasn't found. */
322 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
325 /* Check free space info in a directory data block. */
327 xchk_directory_data_bestfree(
328 struct xfs_scrub *sc,
332 struct xfs_dir2_data_unused *dup;
333 struct xfs_dir2_data_free *dfp;
335 struct xfs_dir2_data_free *bf;
336 struct xfs_mount *mp = sc->mp;
338 unsigned int nr_bestfrees = 0;
339 unsigned int nr_frees = 0;
340 unsigned int smallest_bestfree;
347 /* dir block format */
348 if (lblk != XFS_B_TO_FSBT(mp, XFS_DIR2_DATA_OFFSET))
349 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
350 error = xfs_dir3_block_read(sc->tp, sc->ip, &bp);
352 /* dir data format */
353 error = xfs_dir3_data_read(sc->tp, sc->ip, lblk, 0, &bp);
355 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
357 xchk_buffer_recheck(sc, bp);
359 /* XXX: Check xfs_dir3_data_hdr.pad is zero once we start setting it. */
361 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
364 /* Do the bestfrees correspond to actual free space? */
365 bf = xfs_dir2_data_bestfree_p(mp, bp->b_addr);
366 smallest_bestfree = UINT_MAX;
367 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
368 offset = be16_to_cpu(dfp->offset);
371 if (offset >= mp->m_dir_geo->blksize) {
372 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
375 dup = bp->b_addr + offset;
376 tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
378 /* bestfree doesn't match the entry it points at? */
379 if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG) ||
380 be16_to_cpu(dup->length) != be16_to_cpu(dfp->length) ||
382 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
386 /* bestfree records should be ordered largest to smallest */
387 if (smallest_bestfree < be16_to_cpu(dfp->length)) {
388 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
392 smallest_bestfree = be16_to_cpu(dfp->length);
396 /* Make sure the bestfrees are actually the best free spaces. */
397 offset = mp->m_dir_geo->data_entry_offset;
398 end = xfs_dir3_data_end_offset(mp->m_dir_geo, bp->b_addr);
400 /* Iterate the entries, stopping when we hit or go past the end. */
401 while (offset < end) {
402 dup = bp->b_addr + offset;
404 /* Skip real entries */
405 if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG)) {
406 struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
408 newlen = xfs_dir2_data_entsize(mp, dep->namelen);
410 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
418 /* Spot check this free entry */
419 tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
421 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
426 * Either this entry is a bestfree or it's smaller than
427 * any of the bestfrees.
429 xchk_directory_check_free_entry(sc, lblk, bf, dup);
430 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
434 newlen = be16_to_cpu(dup->length);
436 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
444 /* We're required to fill all the space. */
446 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
448 /* Did we see at least as many free slots as there are bestfrees? */
449 if (nr_frees < nr_bestfrees)
450 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
452 xfs_trans_brelse(sc->tp, bp);
458 * Does the free space length in the free space index block ($len) match
459 * the longest length in the directory data block's bestfree array?
460 * Assume that we've already checked that the data block's bestfree
464 xchk_directory_check_freesp(
465 struct xfs_scrub *sc,
470 struct xfs_dir2_data_free *dfp;
472 dfp = xfs_dir2_data_bestfree_p(sc->mp, dbp->b_addr);
474 if (len != be16_to_cpu(dfp->length))
475 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
477 if (len > 0 && be16_to_cpu(dfp->offset) == 0)
478 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
481 /* Check free space info in a directory leaf1 block. */
483 xchk_directory_leaf1_bestfree(
484 struct xfs_scrub *sc,
485 struct xfs_da_args *args,
488 struct xfs_dir3_icleaf_hdr leafhdr;
489 struct xfs_dir2_leaf_tail *ltp;
490 struct xfs_dir2_leaf *leaf;
493 struct xfs_da_geometry *geo = sc->mp->m_dir_geo;
499 unsigned int stale = 0;
503 /* Read the free space block. */
504 error = xfs_dir3_leaf_read(sc->tp, sc->ip, lblk, &bp);
505 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
507 xchk_buffer_recheck(sc, bp);
510 xfs_dir2_leaf_hdr_from_disk(sc->ip->i_mount, &leafhdr, leaf);
511 ltp = xfs_dir2_leaf_tail_p(geo, leaf);
512 bestcount = be32_to_cpu(ltp->bestcount);
513 bestp = xfs_dir2_leaf_bests_p(ltp);
515 if (xfs_sb_version_hascrc(&sc->mp->m_sb)) {
516 struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
518 if (hdr3->pad != cpu_to_be32(0))
519 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
523 * There should be as many bestfree slots as there are dir data
524 * blocks that can fit under i_size.
526 if (bestcount != xfs_dir2_byte_to_db(geo, sc->ip->i_d.di_size)) {
527 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
531 /* Is the leaf count even remotely sane? */
532 if (leafhdr.count > geo->leaf_max_ents) {
533 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
537 /* Leaves and bests don't overlap in leaf format. */
538 if ((char *)&leafhdr.ents[leafhdr.count] > (char *)bestp) {
539 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
543 /* Check hash value order, count stale entries. */
544 for (i = 0; i < leafhdr.count; i++) {
545 hash = be32_to_cpu(leafhdr.ents[i].hashval);
546 if (i > 0 && lasthash > hash)
547 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
549 if (leafhdr.ents[i].address ==
550 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
553 if (leafhdr.stale != stale)
554 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
555 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
558 /* Check all the bestfree entries. */
559 for (i = 0; i < bestcount; i++, bestp++) {
560 best = be16_to_cpu(*bestp);
561 if (best == NULLDATAOFF)
563 error = xfs_dir3_data_read(sc->tp, sc->ip,
564 i * args->geo->fsbcount, 0, &dbp);
565 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
568 xchk_directory_check_freesp(sc, lblk, dbp, best);
569 xfs_trans_brelse(sc->tp, dbp);
570 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
574 xfs_trans_brelse(sc->tp, bp);
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;
589 unsigned int stale = 0;
593 /* Read the free space block */
594 error = xfs_dir2_free_read(sc->tp, sc->ip, lblk, &bp);
595 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
597 xchk_buffer_recheck(sc, bp);
599 if (xfs_sb_version_hascrc(&sc->mp->m_sb)) {
600 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
602 if (hdr3->pad != cpu_to_be32(0))
603 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
606 /* Check all the entries. */
607 xfs_dir2_free_hdr_from_disk(sc->ip->i_mount, &freehdr, bp->b_addr);
608 for (i = 0; i < freehdr.nvalid; i++) {
609 best = be16_to_cpu(freehdr.bests[i]);
610 if (best == NULLDATAOFF) {
614 error = xfs_dir3_data_read(sc->tp, sc->ip,
615 (freehdr.firstdb + i) * args->geo->fsbcount,
617 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
620 xchk_directory_check_freesp(sc, lblk, dbp, best);
621 xfs_trans_brelse(sc->tp, dbp);
624 if (freehdr.nused + stale != freehdr.nvalid)
625 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
627 xfs_trans_brelse(sc->tp, bp);
631 /* Check free space information in directories. */
633 xchk_directory_blocks(
634 struct xfs_scrub *sc)
636 struct xfs_bmbt_irec got;
637 struct xfs_da_args args;
638 struct xfs_ifork *ifp;
639 struct xfs_mount *mp = sc->mp;
640 xfs_fileoff_t leaf_lblk;
641 xfs_fileoff_t free_lblk;
643 struct xfs_iext_cursor icur;
649 /* Ignore local format directories. */
650 if (sc->ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
651 sc->ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
654 ifp = XFS_IFORK_PTR(sc->ip, XFS_DATA_FORK);
655 lblk = XFS_B_TO_FSB(mp, XFS_DIR2_DATA_OFFSET);
656 leaf_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_LEAF_OFFSET);
657 free_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_FREE_OFFSET);
659 /* Is this a block dir? */
661 args.geo = mp->m_dir_geo;
663 error = xfs_dir2_isblock(&args, &is_block);
664 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
667 /* Iterate all the data extents in the directory... */
668 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
669 while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
670 /* Block directories only have a single block at offset 0. */
672 (got.br_startoff > 0 ||
673 got.br_blockcount != args.geo->fsbcount)) {
674 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
679 /* No more data blocks... */
680 if (got.br_startoff >= leaf_lblk)
684 * Check each data block's bestfree data.
686 * Iterate all the fsbcount-aligned block offsets in
687 * this directory. The directory block reading code is
688 * smart enough to do its own bmap lookups to handle
689 * discontiguous directory blocks. When we're done
690 * with the extent record, re-query the bmap at the
691 * next fsbcount-aligned offset to avoid redundant
694 for (lblk = roundup((xfs_dablk_t)got.br_startoff,
696 lblk < got.br_startoff + got.br_blockcount;
697 lblk += args.geo->fsbcount) {
698 error = xchk_directory_data_bestfree(sc, lblk,
703 dabno = got.br_startoff + got.br_blockcount;
704 lblk = roundup(dabno, args.geo->fsbcount);
705 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
708 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
711 /* Look for a leaf1 block, which has free info. */
712 if (xfs_iext_lookup_extent(sc->ip, ifp, leaf_lblk, &icur, &got) &&
713 got.br_startoff == leaf_lblk &&
714 got.br_blockcount == args.geo->fsbcount &&
715 !xfs_iext_next_extent(ifp, &icur, &got)) {
717 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
720 error = xchk_directory_leaf1_bestfree(sc, &args,
726 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
729 /* Scan for free blocks */
731 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
732 while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
734 * Dirs can't have blocks mapped above 2^32.
735 * Single-block dirs shouldn't even be here.
737 lblk = got.br_startoff;
738 if (lblk & ~0xFFFFFFFFULL) {
739 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
743 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
748 * Check each dir free block's bestfree data.
750 * Iterate all the fsbcount-aligned block offsets in
751 * this directory. The directory block reading code is
752 * smart enough to do its own bmap lookups to handle
753 * discontiguous directory blocks. When we're done
754 * with the extent record, re-query the bmap at the
755 * next fsbcount-aligned offset to avoid redundant
758 for (lblk = roundup((xfs_dablk_t)got.br_startoff,
760 lblk < got.br_startoff + got.br_blockcount;
761 lblk += args.geo->fsbcount) {
762 error = xchk_directory_free_bestfree(sc, &args,
767 dabno = got.br_startoff + got.br_blockcount;
768 lblk = roundup(dabno, args.geo->fsbcount);
769 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
775 /* Scrub a whole directory. */
778 struct xfs_scrub *sc)
780 struct xchk_dir_ctx sdc = {
781 .dir_iter.actor = xchk_dir_actor,
789 if (!S_ISDIR(VFS_I(sc->ip)->i_mode))
792 /* Plausible size? */
793 if (sc->ip->i_d.di_size < xfs_dir2_sf_hdr_size(0)) {
794 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
798 /* Check directory tree structure */
799 error = xchk_da_btree(sc, XFS_DATA_FORK, xchk_dir_rec, NULL);
803 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
806 /* Check the freespace. */
807 error = xchk_directory_blocks(sc);
811 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
815 * Check that every dirent we see can also be looked up by hash.
816 * Userspace usually asks for a 32k buffer, so we will too.
818 bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE,
819 sc->ip->i_d.di_size);
822 * Look up every name in this directory by hash.
824 * Use the xfs_readdir function to call xchk_dir_actor on
825 * every directory entry in this directory. In _actor, we check
826 * the name, inode number, and ftype (if applicable) of the
827 * entry. xfs_readdir uses the VFS filldir functions to provide
830 * The VFS grabs a read or write lock via i_rwsem before it reads
831 * or writes to a directory. If we've gotten this far we've
832 * already obtained IOLOCK_EXCL, which (since 4.10) is the same as
833 * getting a write lock on i_rwsem. Therefore, it is safe for us
834 * to drop the ILOCK here in order to reuse the _readdir and
835 * _dir_lookup routines, which do their own ILOCK locking.
838 sc->ilock_flags &= ~XFS_ILOCK_EXCL;
839 xfs_iunlock(sc->ip, XFS_ILOCK_EXCL);
841 error = xfs_readdir(sc->tp, sc->ip, &sdc.dir_iter, bufsize);
842 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0,
845 if (oldpos == sdc.dir_iter.pos)
847 oldpos = sdc.dir_iter.pos;