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
);
191 /* Scrub a directory btree record. */
194 struct xfs_scrub_da_btree
*ds
,
198 struct xfs_mount
*mp
= ds
->state
->mp
;
199 struct xfs_dir2_leaf_entry
*ent
= rec
;
200 struct xfs_inode
*dp
= ds
->dargs
.dp
;
201 struct xfs_dir2_data_entry
*dent
;
207 xfs_dir2_data_aoff_t off
;
208 xfs_dir2_dataptr_t ptr
;
209 xfs_dahash_t calc_hash
;
214 /* Check the hash of the entry. */
215 error
= xfs_scrub_da_btree_hash(ds
, level
, &ent
->hashval
);
219 /* Valid hash pointer? */
220 ptr
= be32_to_cpu(ent
->address
);
224 /* Find the directory entry's location. */
225 db
= xfs_dir2_dataptr_to_db(mp
->m_dir_geo
, ptr
);
226 off
= xfs_dir2_dataptr_to_off(mp
->m_dir_geo
, ptr
);
227 rec_bno
= xfs_dir2_db_to_da(mp
->m_dir_geo
, db
);
229 if (rec_bno
>= mp
->m_dir_geo
->leafblk
) {
230 xfs_scrub_da_set_corrupt(ds
, level
);
233 error
= xfs_dir3_data_read(ds
->dargs
.trans
, dp
, rec_bno
, -2, &bp
);
234 if (!xfs_scrub_fblock_process_error(ds
->sc
, XFS_DATA_FORK
, rec_bno
,
238 xfs_scrub_fblock_set_corrupt(ds
->sc
, XFS_DATA_FORK
, rec_bno
);
241 xfs_scrub_buffer_recheck(ds
->sc
, bp
);
243 dent
= (struct xfs_dir2_data_entry
*)(((char *)bp
->b_addr
) + off
);
245 /* Make sure we got a real directory entry. */
246 p
= (char *)mp
->m_dir_inode_ops
->data_entry_p(bp
->b_addr
);
247 endp
= xfs_dir3_data_endp(mp
->m_dir_geo
, bp
->b_addr
);
249 xfs_scrub_fblock_set_corrupt(ds
->sc
, XFS_DATA_FORK
, rec_bno
);
253 struct xfs_dir2_data_entry
*dep
;
254 struct xfs_dir2_data_unused
*dup
;
256 dup
= (struct xfs_dir2_data_unused
*)p
;
257 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
258 p
+= be16_to_cpu(dup
->length
);
261 dep
= (struct xfs_dir2_data_entry
*)p
;
264 p
+= mp
->m_dir_inode_ops
->data_entsize(dep
->namelen
);
267 xfs_scrub_fblock_set_corrupt(ds
->sc
, XFS_DATA_FORK
, rec_bno
);
271 /* Retrieve the entry, sanity check it, and compare hashes. */
272 ino
= be64_to_cpu(dent
->inumber
);
273 hash
= be32_to_cpu(ent
->hashval
);
274 tag
= be16_to_cpup(dp
->d_ops
->data_entry_tag_p(dent
));
275 if (!xfs_verify_dir_ino(mp
, ino
) || tag
!= off
)
276 xfs_scrub_fblock_set_corrupt(ds
->sc
, XFS_DATA_FORK
, rec_bno
);
277 if (dent
->namelen
== 0) {
278 xfs_scrub_fblock_set_corrupt(ds
->sc
, XFS_DATA_FORK
, rec_bno
);
281 calc_hash
= xfs_da_hashname(dent
->name
, dent
->namelen
);
282 if (calc_hash
!= hash
)
283 xfs_scrub_fblock_set_corrupt(ds
->sc
, XFS_DATA_FORK
, rec_bno
);
286 xfs_trans_brelse(ds
->dargs
.trans
, bp
);
292 * Is this unused entry either in the bestfree or smaller than all of
293 * them? We've already checked that the bestfrees are sorted longest to
294 * shortest, and that there aren't any bogus entries.
297 xfs_scrub_directory_check_free_entry(
298 struct xfs_scrub_context
*sc
,
300 struct xfs_dir2_data_free
*bf
,
301 struct xfs_dir2_data_unused
*dup
)
303 struct xfs_dir2_data_free
*dfp
;
304 unsigned int dup_length
;
306 dup_length
= be16_to_cpu(dup
->length
);
308 /* Unused entry is shorter than any of the bestfrees */
309 if (dup_length
< be16_to_cpu(bf
[XFS_DIR2_DATA_FD_COUNT
- 1].length
))
312 for (dfp
= &bf
[XFS_DIR2_DATA_FD_COUNT
- 1]; dfp
>= bf
; dfp
--)
313 if (dup_length
== be16_to_cpu(dfp
->length
))
316 /* Unused entry should be in the bestfrees but wasn't found. */
317 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
320 /* Check free space info in a directory data block. */
322 xfs_scrub_directory_data_bestfree(
323 struct xfs_scrub_context
*sc
,
327 struct xfs_dir2_data_unused
*dup
;
328 struct xfs_dir2_data_free
*dfp
;
330 struct xfs_dir2_data_free
*bf
;
331 struct xfs_mount
*mp
= sc
->mp
;
332 const struct xfs_dir_ops
*d_ops
;
336 unsigned int nr_bestfrees
= 0;
337 unsigned int nr_frees
= 0;
338 unsigned int smallest_bestfree
;
343 d_ops
= sc
->ip
->d_ops
;
346 /* dir block format */
347 if (lblk
!= XFS_B_TO_FSBT(mp
, XFS_DIR2_DATA_OFFSET
))
348 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
349 error
= xfs_dir3_block_read(sc
->tp
, sc
->ip
, &bp
);
351 /* dir data format */
352 error
= xfs_dir3_data_read(sc
->tp
, sc
->ip
, lblk
, -1, &bp
);
354 if (!xfs_scrub_fblock_process_error(sc
, XFS_DATA_FORK
, lblk
, &error
))
356 xfs_scrub_buffer_recheck(sc
, bp
);
358 /* XXX: Check xfs_dir3_data_hdr.pad is zero once we start setting it. */
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 xfs_scrub_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 xfs_scrub_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 xfs_scrub_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 xfs_scrub_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 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
420 * Either this entry is a bestfree or it's smaller than
421 * any of the bestfrees.
423 xfs_scrub_directory_check_free_entry(sc
, lblk
, bf
, dup
);
426 newlen
= be16_to_cpu(dup
->length
);
428 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
436 /* We're required to fill all the space. */
438 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
440 /* Did we see at least as many free slots as there are bestfrees? */
441 if (nr_frees
< nr_bestfrees
)
442 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
444 xfs_trans_brelse(sc
->tp
, bp
);
450 * Does the free space length in the free space index block ($len) match
451 * the longest length in the directory data block's bestfree array?
452 * Assume that we've already checked that the data block's bestfree
456 xfs_scrub_directory_check_freesp(
457 struct xfs_scrub_context
*sc
,
462 struct xfs_dir2_data_free
*dfp
;
464 dfp
= sc
->ip
->d_ops
->data_bestfree_p(dbp
->b_addr
);
466 if (len
!= be16_to_cpu(dfp
->length
))
467 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
469 if (len
> 0 && be16_to_cpu(dfp
->offset
) == 0)
470 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
473 /* Check free space info in a directory leaf1 block. */
475 xfs_scrub_directory_leaf1_bestfree(
476 struct xfs_scrub_context
*sc
,
477 struct xfs_da_args
*args
,
480 struct xfs_dir3_icleaf_hdr leafhdr
;
481 struct xfs_dir2_leaf_entry
*ents
;
482 struct xfs_dir2_leaf_tail
*ltp
;
483 struct xfs_dir2_leaf
*leaf
;
486 const struct xfs_dir_ops
*d_ops
= sc
->ip
->d_ops
;
487 struct xfs_da_geometry
*geo
= sc
->mp
->m_dir_geo
;
493 unsigned int stale
= 0;
497 /* Read the free space block. */
498 error
= xfs_dir3_leaf_read(sc
->tp
, sc
->ip
, lblk
, -1, &bp
);
499 if (!xfs_scrub_fblock_process_error(sc
, XFS_DATA_FORK
, lblk
, &error
))
501 xfs_scrub_buffer_recheck(sc
, bp
);
504 d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
505 ents
= d_ops
->leaf_ents_p(leaf
);
506 ltp
= xfs_dir2_leaf_tail_p(geo
, leaf
);
507 bestcount
= be32_to_cpu(ltp
->bestcount
);
508 bestp
= xfs_dir2_leaf_bests_p(ltp
);
510 if (xfs_sb_version_hascrc(&sc
->mp
->m_sb
)) {
511 struct xfs_dir3_leaf_hdr
*hdr3
= bp
->b_addr
;
513 if (hdr3
->pad
!= cpu_to_be32(0))
514 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
518 * There should be as many bestfree slots as there are dir data
519 * blocks that can fit under i_size.
521 if (bestcount
!= xfs_dir2_byte_to_db(geo
, sc
->ip
->i_d
.di_size
)) {
522 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
526 /* Is the leaf count even remotely sane? */
527 if (leafhdr
.count
> d_ops
->leaf_max_ents(geo
)) {
528 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
532 /* Leaves and bests don't overlap in leaf format. */
533 if ((char *)&ents
[leafhdr
.count
] > (char *)bestp
) {
534 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
538 /* Check hash value order, count stale entries. */
539 for (i
= 0; i
< leafhdr
.count
; i
++) {
540 hash
= be32_to_cpu(ents
[i
].hashval
);
541 if (i
> 0 && lasthash
> hash
)
542 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
544 if (ents
[i
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
547 if (leafhdr
.stale
!= stale
)
548 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
550 /* Check all the bestfree entries. */
551 for (i
= 0; i
< bestcount
; i
++, bestp
++) {
552 best
= be16_to_cpu(*bestp
);
553 if (best
== NULLDATAOFF
)
555 error
= xfs_dir3_data_read(sc
->tp
, sc
->ip
,
556 i
* args
->geo
->fsbcount
, -1, &dbp
);
557 if (!xfs_scrub_fblock_process_error(sc
, XFS_DATA_FORK
, lblk
,
560 xfs_scrub_directory_check_freesp(sc
, lblk
, dbp
, best
);
561 xfs_trans_brelse(sc
->tp
, dbp
);
567 /* Check free space info in a directory freespace block. */
569 xfs_scrub_directory_free_bestfree(
570 struct xfs_scrub_context
*sc
,
571 struct xfs_da_args
*args
,
574 struct xfs_dir3_icfree_hdr freehdr
;
579 unsigned int stale
= 0;
583 /* Read the free space block */
584 error
= xfs_dir2_free_read(sc
->tp
, sc
->ip
, lblk
, &bp
);
585 if (!xfs_scrub_fblock_process_error(sc
, XFS_DATA_FORK
, lblk
, &error
))
587 xfs_scrub_buffer_recheck(sc
, bp
);
589 if (xfs_sb_version_hascrc(&sc
->mp
->m_sb
)) {
590 struct xfs_dir3_free_hdr
*hdr3
= bp
->b_addr
;
592 if (hdr3
->pad
!= cpu_to_be32(0))
593 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
596 /* Check all the entries. */
597 sc
->ip
->d_ops
->free_hdr_from_disk(&freehdr
, bp
->b_addr
);
598 bestp
= sc
->ip
->d_ops
->free_bests_p(bp
->b_addr
);
599 for (i
= 0; i
< freehdr
.nvalid
; i
++, bestp
++) {
600 best
= be16_to_cpu(*bestp
);
601 if (best
== NULLDATAOFF
) {
605 error
= xfs_dir3_data_read(sc
->tp
, sc
->ip
,
606 (freehdr
.firstdb
+ i
) * args
->geo
->fsbcount
,
608 if (!xfs_scrub_fblock_process_error(sc
, XFS_DATA_FORK
, lblk
,
611 xfs_scrub_directory_check_freesp(sc
, lblk
, dbp
, best
);
612 xfs_trans_brelse(sc
->tp
, dbp
);
615 if (freehdr
.nused
+ stale
!= freehdr
.nvalid
)
616 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
621 /* Check free space information in directories. */
623 xfs_scrub_directory_blocks(
624 struct xfs_scrub_context
*sc
)
626 struct xfs_bmbt_irec got
;
627 struct xfs_da_args args
;
628 struct xfs_ifork
*ifp
;
629 struct xfs_mount
*mp
= sc
->mp
;
630 xfs_fileoff_t leaf_lblk
;
631 xfs_fileoff_t free_lblk
;
633 struct xfs_iext_cursor icur
;
639 /* Ignore local format directories. */
640 if (sc
->ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
&&
641 sc
->ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
)
644 ifp
= XFS_IFORK_PTR(sc
->ip
, XFS_DATA_FORK
);
645 lblk
= XFS_B_TO_FSB(mp
, XFS_DIR2_DATA_OFFSET
);
646 leaf_lblk
= XFS_B_TO_FSB(mp
, XFS_DIR2_LEAF_OFFSET
);
647 free_lblk
= XFS_B_TO_FSB(mp
, XFS_DIR2_FREE_OFFSET
);
649 /* Is this a block dir? */
651 args
.geo
= mp
->m_dir_geo
;
653 error
= xfs_dir2_isblock(&args
, &is_block
);
654 if (!xfs_scrub_fblock_process_error(sc
, XFS_DATA_FORK
, lblk
, &error
))
657 /* Iterate all the data extents in the directory... */
658 found
= xfs_iext_lookup_extent(sc
->ip
, ifp
, lblk
, &icur
, &got
);
660 /* Block directories only have a single block at offset 0. */
662 (got
.br_startoff
> 0 ||
663 got
.br_blockcount
!= args
.geo
->fsbcount
)) {
664 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
,
669 /* No more data blocks... */
670 if (got
.br_startoff
>= leaf_lblk
)
674 * Check each data block's bestfree data.
676 * Iterate all the fsbcount-aligned block offsets in
677 * this directory. The directory block reading code is
678 * smart enough to do its own bmap lookups to handle
679 * discontiguous directory blocks. When we're done
680 * with the extent record, re-query the bmap at the
681 * next fsbcount-aligned offset to avoid redundant
684 for (lblk
= roundup((xfs_dablk_t
)got
.br_startoff
,
686 lblk
< got
.br_startoff
+ got
.br_blockcount
;
687 lblk
+= args
.geo
->fsbcount
) {
688 error
= xfs_scrub_directory_data_bestfree(sc
, lblk
,
693 dabno
= got
.br_startoff
+ got
.br_blockcount
;
694 lblk
= roundup(dabno
, args
.geo
->fsbcount
);
695 found
= xfs_iext_lookup_extent(sc
->ip
, ifp
, lblk
, &icur
, &got
);
698 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
701 /* Look for a leaf1 block, which has free info. */
702 if (xfs_iext_lookup_extent(sc
->ip
, ifp
, leaf_lblk
, &icur
, &got
) &&
703 got
.br_startoff
== leaf_lblk
&&
704 got
.br_blockcount
== args
.geo
->fsbcount
&&
705 !xfs_iext_next_extent(ifp
, &icur
, &got
)) {
707 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
710 error
= xfs_scrub_directory_leaf1_bestfree(sc
, &args
,
716 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
719 /* Scan for free blocks */
721 found
= xfs_iext_lookup_extent(sc
->ip
, ifp
, lblk
, &icur
, &got
);
724 * Dirs can't have blocks mapped above 2^32.
725 * Single-block dirs shouldn't even be here.
727 lblk
= got
.br_startoff
;
728 if (lblk
& ~0xFFFFFFFFULL
) {
729 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
733 xfs_scrub_fblock_set_corrupt(sc
, XFS_DATA_FORK
, lblk
);
738 * Check each dir free block's bestfree data.
740 * Iterate all the fsbcount-aligned block offsets in
741 * this directory. The directory block reading code is
742 * smart enough to do its own bmap lookups to handle
743 * discontiguous directory blocks. When we're done
744 * with the extent record, re-query the bmap at the
745 * next fsbcount-aligned offset to avoid redundant
748 for (lblk
= roundup((xfs_dablk_t
)got
.br_startoff
,
750 lblk
< got
.br_startoff
+ got
.br_blockcount
;
751 lblk
+= args
.geo
->fsbcount
) {
752 error
= xfs_scrub_directory_free_bestfree(sc
, &args
,
757 dabno
= got
.br_startoff
+ got
.br_blockcount
;
758 lblk
= roundup(dabno
, args
.geo
->fsbcount
);
759 found
= xfs_iext_lookup_extent(sc
->ip
, ifp
, lblk
, &icur
, &got
);
765 /* Scrub a whole directory. */
768 struct xfs_scrub_context
*sc
)
770 struct xfs_scrub_dir_ctx sdc
= {
771 .dir_iter
.actor
= xfs_scrub_dir_actor
,
779 if (!S_ISDIR(VFS_I(sc
->ip
)->i_mode
))
782 /* Plausible size? */
783 if (sc
->ip
->i_d
.di_size
< xfs_dir2_sf_hdr_size(0)) {
784 xfs_scrub_ino_set_corrupt(sc
, sc
->ip
->i_ino
, NULL
);
788 /* Check directory tree structure */
789 error
= xfs_scrub_da_btree(sc
, XFS_DATA_FORK
, xfs_scrub_dir_rec
, NULL
);
793 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
796 /* Check the freespace. */
797 error
= xfs_scrub_directory_blocks(sc
);
801 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
805 * Check that every dirent we see can also be looked up by hash.
806 * Userspace usually asks for a 32k buffer, so we will too.
808 bufsize
= (size_t)min_t(loff_t
, XFS_READDIR_BUFSIZE
,
809 sc
->ip
->i_d
.di_size
);
812 * Look up every name in this directory by hash.
814 * Use the xfs_readdir function to call xfs_scrub_dir_actor on
815 * every directory entry in this directory. In _actor, we check
816 * the name, inode number, and ftype (if applicable) of the
817 * entry. xfs_readdir uses the VFS filldir functions to provide
820 * The VFS grabs a read or write lock via i_rwsem before it reads
821 * or writes to a directory. If we've gotten this far we've
822 * already obtained IOLOCK_EXCL, which (since 4.10) is the same as
823 * getting a write lock on i_rwsem. Therefore, it is safe for us
824 * to drop the ILOCK here in order to reuse the _readdir and
825 * _dir_lookup routines, which do their own ILOCK locking.
828 sc
->ilock_flags
&= ~XFS_ILOCK_EXCL
;
829 xfs_iunlock(sc
->ip
, XFS_ILOCK_EXCL
);
831 error
= xfs_readdir(sc
->tp
, sc
->ip
, &sdc
.dir_iter
, bufsize
);
832 if (!xfs_scrub_fblock_process_error(sc
, XFS_DATA_FORK
, 0,
835 if (oldpos
== sdc
.dir_iter
.pos
)
837 oldpos
= sdc
.dir_iter
.pos
;