2 * Copyright (C) 2017 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "xfs_shared.h"
23 #include "xfs_format.h"
24 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_defer.h"
27 #include "xfs_btree.h"
29 #include "xfs_log_format.h"
30 #include "xfs_trans.h"
32 #include "xfs_inode.h"
33 #include "xfs_icache.h"
34 #include "xfs_itable.h"
35 #include "xfs_da_format.h"
36 #include "xfs_da_btree.h"
38 #include "xfs_dir2_priv.h"
39 #include "xfs_ialloc.h"
40 #include "scrub/xfs_scrub.h"
41 #include "scrub/scrub.h"
42 #include "scrub/common.h"
43 #include "scrub/trace.h"
44 #include "scrub/dabtree.h"
46 /* Set us up to scrub directories. */
48 xfs_scrub_setup_directory(
49 struct xfs_scrub_context *sc,
52 return xfs_scrub_setup_inode_contents(sc, ip, 0);
57 /* Scrub a directory entry. */
59 struct xfs_scrub_dir_ctx {
60 /* VFS fill-directory iterator */
61 struct dir_context dir_iter;
63 struct xfs_scrub_context *sc;
66 /* Check that an inode's mode matches a given DT_ type. */
68 xfs_scrub_dir_check_ftype(
69 struct xfs_scrub_dir_ctx *sdc,
74 struct xfs_mount *mp = sdc->sc->mp;
79 if (!xfs_sb_version_hasftype(&mp->m_sb)) {
80 if (dtype != DT_UNKNOWN && dtype != DT_DIR)
81 xfs_scrub_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
87 * Grab the inode pointed to by the dirent. We release the
88 * inode before we cancel the scrub transaction. Since we're
89 * don't know a priori that releasing the inode won't trigger
90 * eofblocks cleanup (which allocates what would be a nested
91 * transaction), we can't use DONTCACHE here because DONTCACHE
92 * inodes can trigger immediate inactive cleanup of the inode.
94 error = xfs_iget(mp, sdc->sc->tp, inum, 0, 0, &ip);
95 if (!xfs_scrub_fblock_xref_process_error(sdc->sc, XFS_DATA_FORK, offset,
99 /* Convert mode to the DT_* values that dir_emit uses. */
100 ino_dtype = xfs_dir3_get_dtype(mp,
101 xfs_mode_to_ftype(VFS_I(ip)->i_mode));
102 if (ino_dtype != dtype)
103 xfs_scrub_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
110 * Scrub a single directory entry.
112 * We use the VFS directory iterator (i.e. readdir) to call this
113 * function for every directory entry in a directory. Once we're here,
114 * we check the inode number to make sure it's sane, then we check that
115 * we can look up this filename. Finally, we check the ftype.
119 struct dir_context *dir_iter,
126 struct xfs_mount *mp;
127 struct xfs_inode *ip;
128 struct xfs_scrub_dir_ctx *sdc;
129 struct xfs_name xname;
130 xfs_ino_t lookup_ino;
134 sdc = container_of(dir_iter, struct xfs_scrub_dir_ctx, dir_iter);
137 offset = xfs_dir2_db_to_da(mp->m_dir_geo,
138 xfs_dir2_dataptr_to_db(mp->m_dir_geo, pos));
140 /* Does this inode number make sense? */
141 if (!xfs_verify_dir_ino(mp, ino)) {
142 xfs_scrub_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
146 if (!strncmp(".", name, namelen)) {
147 /* If this is "." then check that the inum matches the dir. */
148 if (xfs_sb_version_hasftype(&mp->m_sb) && type != DT_DIR)
149 xfs_scrub_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
151 if (ino != ip->i_ino)
152 xfs_scrub_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
154 } else if (!strncmp("..", name, namelen)) {
156 * If this is ".." in the root inode, check that the inum
159 if (xfs_sb_version_hasftype(&mp->m_sb) && type != DT_DIR)
160 xfs_scrub_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
162 if (ip->i_ino == mp->m_sb.sb_rootino && ino != ip->i_ino)
163 xfs_scrub_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
167 /* Verify that we can look up this name by hash. */
170 xname.type = XFS_DIR3_FT_UNKNOWN;
172 error = xfs_dir_lookup(sdc->sc->tp, ip, &xname, &lookup_ino, NULL);
173 if (!xfs_scrub_fblock_process_error(sdc->sc, XFS_DATA_FORK, offset,
176 if (lookup_ino != ino) {
177 xfs_scrub_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
181 /* Verify the file type. This function absorbs error codes. */
182 error = xfs_scrub_dir_check_ftype(sdc, offset, lookup_ino, type);
187 * A negative error code returned here is supposed to cause the
188 * dir_emit caller (xfs_readdir) to abort the directory iteration
189 * and return zero to xfs_scrub_directory.
191 if (error == 0 && sdc->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
192 return -EFSCORRUPTED;
196 /* Scrub a directory btree record. */
199 struct xfs_scrub_da_btree *ds,
203 struct xfs_mount *mp = ds->state->mp;
204 struct xfs_dir2_leaf_entry *ent = rec;
205 struct xfs_inode *dp = ds->dargs.dp;
206 struct xfs_dir2_data_entry *dent;
212 xfs_dir2_data_aoff_t off;
213 xfs_dir2_dataptr_t ptr;
214 xfs_dahash_t calc_hash;
219 /* Check the hash of the entry. */
220 error = xfs_scrub_da_btree_hash(ds, level, &ent->hashval);
224 /* Valid hash pointer? */
225 ptr = be32_to_cpu(ent->address);
229 /* Find the directory entry's location. */
230 db = xfs_dir2_dataptr_to_db(mp->m_dir_geo, ptr);
231 off = xfs_dir2_dataptr_to_off(mp->m_dir_geo, ptr);
232 rec_bno = xfs_dir2_db_to_da(mp->m_dir_geo, db);
234 if (rec_bno >= mp->m_dir_geo->leafblk) {
235 xfs_scrub_da_set_corrupt(ds, level);
238 error = xfs_dir3_data_read(ds->dargs.trans, dp, rec_bno, -2, &bp);
239 if (!xfs_scrub_fblock_process_error(ds->sc, XFS_DATA_FORK, rec_bno,
243 xfs_scrub_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
246 xfs_scrub_buffer_recheck(ds->sc, bp);
248 if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
251 dent = (struct xfs_dir2_data_entry *)(((char *)bp->b_addr) + off);
253 /* Make sure we got a real directory entry. */
254 p = (char *)mp->m_dir_inode_ops->data_entry_p(bp->b_addr);
255 endp = xfs_dir3_data_endp(mp->m_dir_geo, bp->b_addr);
257 xfs_scrub_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
261 struct xfs_dir2_data_entry *dep;
262 struct xfs_dir2_data_unused *dup;
264 dup = (struct xfs_dir2_data_unused *)p;
265 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
266 p += be16_to_cpu(dup->length);
269 dep = (struct xfs_dir2_data_entry *)p;
272 p += mp->m_dir_inode_ops->data_entsize(dep->namelen);
275 xfs_scrub_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
279 /* Retrieve the entry, sanity check it, and compare hashes. */
280 ino = be64_to_cpu(dent->inumber);
281 hash = be32_to_cpu(ent->hashval);
282 tag = be16_to_cpup(dp->d_ops->data_entry_tag_p(dent));
283 if (!xfs_verify_dir_ino(mp, ino) || tag != off)
284 xfs_scrub_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
285 if (dent->namelen == 0) {
286 xfs_scrub_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
289 calc_hash = xfs_da_hashname(dent->name, dent->namelen);
290 if (calc_hash != hash)
291 xfs_scrub_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
294 xfs_trans_brelse(ds->dargs.trans, bp);
300 * Is this unused entry either in the bestfree or smaller than all of
301 * them? We've already checked that the bestfrees are sorted longest to
302 * shortest, and that there aren't any bogus entries.
305 xfs_scrub_directory_check_free_entry(
306 struct xfs_scrub_context *sc,
308 struct xfs_dir2_data_free *bf,
309 struct xfs_dir2_data_unused *dup)
311 struct xfs_dir2_data_free *dfp;
312 unsigned int dup_length;
314 dup_length = be16_to_cpu(dup->length);
316 /* Unused entry is shorter than any of the bestfrees */
317 if (dup_length < be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
320 for (dfp = &bf[XFS_DIR2_DATA_FD_COUNT - 1]; dfp >= bf; dfp--)
321 if (dup_length == be16_to_cpu(dfp->length))
324 /* Unused entry should be in the bestfrees but wasn't found. */
325 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
328 /* Check free space info in a directory data block. */
330 xfs_scrub_directory_data_bestfree(
331 struct xfs_scrub_context *sc,
335 struct xfs_dir2_data_unused *dup;
336 struct xfs_dir2_data_free *dfp;
338 struct xfs_dir2_data_free *bf;
339 struct xfs_mount *mp = sc->mp;
340 const struct xfs_dir_ops *d_ops;
344 unsigned int nr_bestfrees = 0;
345 unsigned int nr_frees = 0;
346 unsigned int smallest_bestfree;
351 d_ops = sc->ip->d_ops;
354 /* dir block format */
355 if (lblk != XFS_B_TO_FSBT(mp, XFS_DIR2_DATA_OFFSET))
356 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
357 error = xfs_dir3_block_read(sc->tp, sc->ip, &bp);
359 /* dir data format */
360 error = xfs_dir3_data_read(sc->tp, sc->ip, lblk, -1, &bp);
362 if (!xfs_scrub_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
364 xfs_scrub_buffer_recheck(sc, bp);
366 /* XXX: Check xfs_dir3_data_hdr.pad is zero once we start setting it. */
368 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
371 /* Do the bestfrees correspond to actual free space? */
372 bf = d_ops->data_bestfree_p(bp->b_addr);
373 smallest_bestfree = UINT_MAX;
374 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
375 offset = be16_to_cpu(dfp->offset);
378 if (offset >= mp->m_dir_geo->blksize) {
379 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
382 dup = (struct xfs_dir2_data_unused *)(bp->b_addr + offset);
383 tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
385 /* bestfree doesn't match the entry it points at? */
386 if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG) ||
387 be16_to_cpu(dup->length) != be16_to_cpu(dfp->length) ||
388 tag != ((char *)dup - (char *)bp->b_addr)) {
389 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
393 /* bestfree records should be ordered largest to smallest */
394 if (smallest_bestfree < be16_to_cpu(dfp->length)) {
395 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
399 smallest_bestfree = be16_to_cpu(dfp->length);
403 /* Make sure the bestfrees are actually the best free spaces. */
404 ptr = (char *)d_ops->data_entry_p(bp->b_addr);
405 endptr = xfs_dir3_data_endp(mp->m_dir_geo, bp->b_addr);
407 /* Iterate the entries, stopping when we hit or go past the end. */
408 while (ptr < endptr) {
409 dup = (struct xfs_dir2_data_unused *)ptr;
410 /* Skip real entries */
411 if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG)) {
412 struct xfs_dir2_data_entry *dep;
414 dep = (struct xfs_dir2_data_entry *)ptr;
415 newlen = d_ops->data_entsize(dep->namelen);
417 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK,
425 /* Spot check this free entry */
426 tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
427 if (tag != ((char *)dup - (char *)bp->b_addr)) {
428 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
433 * Either this entry is a bestfree or it's smaller than
434 * any of the bestfrees.
436 xfs_scrub_directory_check_free_entry(sc, lblk, bf, dup);
437 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
441 newlen = be16_to_cpu(dup->length);
443 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
451 /* We're required to fill all the space. */
453 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
455 /* Did we see at least as many free slots as there are bestfrees? */
456 if (nr_frees < nr_bestfrees)
457 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
459 xfs_trans_brelse(sc->tp, bp);
465 * Does the free space length in the free space index block ($len) match
466 * the longest length in the directory data block's bestfree array?
467 * Assume that we've already checked that the data block's bestfree
471 xfs_scrub_directory_check_freesp(
472 struct xfs_scrub_context *sc,
477 struct xfs_dir2_data_free *dfp;
479 dfp = sc->ip->d_ops->data_bestfree_p(dbp->b_addr);
481 if (len != be16_to_cpu(dfp->length))
482 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
484 if (len > 0 && be16_to_cpu(dfp->offset) == 0)
485 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
488 /* Check free space info in a directory leaf1 block. */
490 xfs_scrub_directory_leaf1_bestfree(
491 struct xfs_scrub_context *sc,
492 struct xfs_da_args *args,
495 struct xfs_dir3_icleaf_hdr leafhdr;
496 struct xfs_dir2_leaf_entry *ents;
497 struct xfs_dir2_leaf_tail *ltp;
498 struct xfs_dir2_leaf *leaf;
501 const struct xfs_dir_ops *d_ops = sc->ip->d_ops;
502 struct xfs_da_geometry *geo = sc->mp->m_dir_geo;
508 unsigned int stale = 0;
512 /* Read the free space block. */
513 error = xfs_dir3_leaf_read(sc->tp, sc->ip, lblk, -1, &bp);
514 if (!xfs_scrub_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
516 xfs_scrub_buffer_recheck(sc, bp);
519 d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
520 ents = d_ops->leaf_ents_p(leaf);
521 ltp = xfs_dir2_leaf_tail_p(geo, leaf);
522 bestcount = be32_to_cpu(ltp->bestcount);
523 bestp = xfs_dir2_leaf_bests_p(ltp);
525 if (xfs_sb_version_hascrc(&sc->mp->m_sb)) {
526 struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
528 if (hdr3->pad != cpu_to_be32(0))
529 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
533 * There should be as many bestfree slots as there are dir data
534 * blocks that can fit under i_size.
536 if (bestcount != xfs_dir2_byte_to_db(geo, sc->ip->i_d.di_size)) {
537 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
541 /* Is the leaf count even remotely sane? */
542 if (leafhdr.count > d_ops->leaf_max_ents(geo)) {
543 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
547 /* Leaves and bests don't overlap in leaf format. */
548 if ((char *)&ents[leafhdr.count] > (char *)bestp) {
549 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
553 /* Check hash value order, count stale entries. */
554 for (i = 0; i < leafhdr.count; i++) {
555 hash = be32_to_cpu(ents[i].hashval);
556 if (i > 0 && lasthash > hash)
557 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
559 if (ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
562 if (leafhdr.stale != stale)
563 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
564 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
567 /* Check all the bestfree entries. */
568 for (i = 0; i < bestcount; i++, bestp++) {
569 best = be16_to_cpu(*bestp);
570 if (best == NULLDATAOFF)
572 error = xfs_dir3_data_read(sc->tp, sc->ip,
573 i * args->geo->fsbcount, -1, &dbp);
574 if (!xfs_scrub_fblock_process_error(sc, XFS_DATA_FORK, lblk,
577 xfs_scrub_directory_check_freesp(sc, lblk, dbp, best);
578 xfs_trans_brelse(sc->tp, dbp);
579 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
586 /* Check free space info in a directory freespace block. */
588 xfs_scrub_directory_free_bestfree(
589 struct xfs_scrub_context *sc,
590 struct xfs_da_args *args,
593 struct xfs_dir3_icfree_hdr freehdr;
598 unsigned int stale = 0;
602 /* Read the free space block */
603 error = xfs_dir2_free_read(sc->tp, sc->ip, lblk, &bp);
604 if (!xfs_scrub_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
606 xfs_scrub_buffer_recheck(sc, bp);
608 if (xfs_sb_version_hascrc(&sc->mp->m_sb)) {
609 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
611 if (hdr3->pad != cpu_to_be32(0))
612 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
615 /* Check all the entries. */
616 sc->ip->d_ops->free_hdr_from_disk(&freehdr, bp->b_addr);
617 bestp = sc->ip->d_ops->free_bests_p(bp->b_addr);
618 for (i = 0; i < freehdr.nvalid; i++, bestp++) {
619 best = be16_to_cpu(*bestp);
620 if (best == NULLDATAOFF) {
624 error = xfs_dir3_data_read(sc->tp, sc->ip,
625 (freehdr.firstdb + i) * args->geo->fsbcount,
627 if (!xfs_scrub_fblock_process_error(sc, XFS_DATA_FORK, lblk,
630 xfs_scrub_directory_check_freesp(sc, lblk, dbp, best);
631 xfs_trans_brelse(sc->tp, dbp);
634 if (freehdr.nused + stale != freehdr.nvalid)
635 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
640 /* Check free space information in directories. */
642 xfs_scrub_directory_blocks(
643 struct xfs_scrub_context *sc)
645 struct xfs_bmbt_irec got;
646 struct xfs_da_args args;
647 struct xfs_ifork *ifp;
648 struct xfs_mount *mp = sc->mp;
649 xfs_fileoff_t leaf_lblk;
650 xfs_fileoff_t free_lblk;
652 struct xfs_iext_cursor icur;
658 /* Ignore local format directories. */
659 if (sc->ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
660 sc->ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
663 ifp = XFS_IFORK_PTR(sc->ip, XFS_DATA_FORK);
664 lblk = XFS_B_TO_FSB(mp, XFS_DIR2_DATA_OFFSET);
665 leaf_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_LEAF_OFFSET);
666 free_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_FREE_OFFSET);
668 /* Is this a block dir? */
670 args.geo = mp->m_dir_geo;
672 error = xfs_dir2_isblock(&args, &is_block);
673 if (!xfs_scrub_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
676 /* Iterate all the data extents in the directory... */
677 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
678 while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
679 /* Block directories only have a single block at offset 0. */
681 (got.br_startoff > 0 ||
682 got.br_blockcount != args.geo->fsbcount)) {
683 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK,
688 /* No more data blocks... */
689 if (got.br_startoff >= leaf_lblk)
693 * Check each data block's bestfree data.
695 * Iterate all the fsbcount-aligned block offsets in
696 * this directory. The directory block reading code is
697 * smart enough to do its own bmap lookups to handle
698 * discontiguous directory blocks. When we're done
699 * with the extent record, re-query the bmap at the
700 * next fsbcount-aligned offset to avoid redundant
703 for (lblk = roundup((xfs_dablk_t)got.br_startoff,
705 lblk < got.br_startoff + got.br_blockcount;
706 lblk += args.geo->fsbcount) {
707 error = xfs_scrub_directory_data_bestfree(sc, lblk,
712 dabno = got.br_startoff + got.br_blockcount;
713 lblk = roundup(dabno, args.geo->fsbcount);
714 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
717 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
720 /* Look for a leaf1 block, which has free info. */
721 if (xfs_iext_lookup_extent(sc->ip, ifp, leaf_lblk, &icur, &got) &&
722 got.br_startoff == leaf_lblk &&
723 got.br_blockcount == args.geo->fsbcount &&
724 !xfs_iext_next_extent(ifp, &icur, &got)) {
726 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
729 error = xfs_scrub_directory_leaf1_bestfree(sc, &args,
735 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
738 /* Scan for free blocks */
740 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
741 while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
743 * Dirs can't have blocks mapped above 2^32.
744 * Single-block dirs shouldn't even be here.
746 lblk = got.br_startoff;
747 if (lblk & ~0xFFFFFFFFULL) {
748 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
752 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
757 * Check each dir free block's bestfree data.
759 * Iterate all the fsbcount-aligned block offsets in
760 * this directory. The directory block reading code is
761 * smart enough to do its own bmap lookups to handle
762 * discontiguous directory blocks. When we're done
763 * with the extent record, re-query the bmap at the
764 * next fsbcount-aligned offset to avoid redundant
767 for (lblk = roundup((xfs_dablk_t)got.br_startoff,
769 lblk < got.br_startoff + got.br_blockcount;
770 lblk += args.geo->fsbcount) {
771 error = xfs_scrub_directory_free_bestfree(sc, &args,
776 dabno = got.br_startoff + got.br_blockcount;
777 lblk = roundup(dabno, args.geo->fsbcount);
778 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
784 /* Scrub a whole directory. */
787 struct xfs_scrub_context *sc)
789 struct xfs_scrub_dir_ctx sdc = {
790 .dir_iter.actor = xfs_scrub_dir_actor,
798 if (!S_ISDIR(VFS_I(sc->ip)->i_mode))
801 /* Plausible size? */
802 if (sc->ip->i_d.di_size < xfs_dir2_sf_hdr_size(0)) {
803 xfs_scrub_ino_set_corrupt(sc, sc->ip->i_ino);
807 /* Check directory tree structure */
808 error = xfs_scrub_da_btree(sc, XFS_DATA_FORK, xfs_scrub_dir_rec, NULL);
812 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
815 /* Check the freespace. */
816 error = xfs_scrub_directory_blocks(sc);
820 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
824 * Check that every dirent we see can also be looked up by hash.
825 * Userspace usually asks for a 32k buffer, so we will too.
827 bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE,
828 sc->ip->i_d.di_size);
831 * Look up every name in this directory by hash.
833 * Use the xfs_readdir function to call xfs_scrub_dir_actor on
834 * every directory entry in this directory. In _actor, we check
835 * the name, inode number, and ftype (if applicable) of the
836 * entry. xfs_readdir uses the VFS filldir functions to provide
839 * The VFS grabs a read or write lock via i_rwsem before it reads
840 * or writes to a directory. If we've gotten this far we've
841 * already obtained IOLOCK_EXCL, which (since 4.10) is the same as
842 * getting a write lock on i_rwsem. Therefore, it is safe for us
843 * to drop the ILOCK here in order to reuse the _readdir and
844 * _dir_lookup routines, which do their own ILOCK locking.
847 sc->ilock_flags &= ~XFS_ILOCK_EXCL;
848 xfs_iunlock(sc->ip, XFS_ILOCK_EXCL);
850 error = xfs_readdir(sc->tp, sc->ip, &sdc.dir_iter, bufsize);
851 if (!xfs_scrub_fblock_process_error(sc, XFS_DATA_FORK, 0,
854 if (oldpos == sdc.dir_iter.pos)
856 oldpos = sdc.dir_iter.pos;