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"
16 #include "xfs_dir2_priv.h"
17 #include "xfs_attr_leaf.h"
18 #include "scrub/scrub.h"
19 #include "scrub/common.h"
20 #include "scrub/trace.h"
21 #include "scrub/dabtree.h"
23 /* Directory/Attribute Btree */
26 * Check for da btree operation errors. See the section about handling
27 * operational errors in common.c.
30 xchk_da_process_error(
31 struct xchk_da_btree *ds,
35 struct xfs_scrub *sc = ds->sc;
42 /* Used to restart an op with deadlock avoidance. */
43 trace_xchk_deadlock_retry(sc->ip, sc->sm, *error);
47 /* Note the badness but don't abort. */
48 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
52 trace_xchk_file_op_error(sc, ds->dargs.whichfork,
53 xfs_dir2_da_to_db(ds->dargs.geo,
54 ds->state->path.blk[level].blkno),
55 *error, __return_address);
62 * Check for da btree corruption. See the section about handling
63 * operational errors in common.c.
67 struct xchk_da_btree *ds,
70 struct xfs_scrub *sc = ds->sc;
72 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
74 trace_xchk_fblock_error(sc, ds->dargs.whichfork,
75 xfs_dir2_da_to_db(ds->dargs.geo,
76 ds->state->path.blk[level].blkno),
80 static struct xfs_da_node_entry *
81 xchk_da_btree_node_entry(
82 struct xchk_da_btree *ds,
85 struct xfs_da_state_blk *blk = &ds->state->path.blk[level];
86 struct xfs_da3_icnode_hdr hdr;
88 ASSERT(blk->magic == XFS_DA_NODE_MAGIC);
90 xfs_da3_node_hdr_from_disk(ds->sc->mp, &hdr, blk->bp->b_addr);
91 return hdr.btree + blk->index;
94 /* Scrub a da btree hash (key). */
97 struct xchk_da_btree *ds,
101 struct xfs_da_node_entry *entry;
103 xfs_dahash_t parent_hash;
105 /* Is this hash in order? */
106 hash = be32_to_cpu(*hashp);
107 if (hash < ds->hashes[level])
108 xchk_da_set_corrupt(ds, level);
109 ds->hashes[level] = hash;
114 /* Is this hash no larger than the parent hash? */
115 entry = xchk_da_btree_node_entry(ds, level - 1);
116 parent_hash = be32_to_cpu(entry->hashval);
117 if (parent_hash < hash)
118 xchk_da_set_corrupt(ds, level);
124 * Check a da btree pointer. Returns true if it's ok to use this
128 xchk_da_btree_ptr_ok(
129 struct xchk_da_btree *ds,
133 if (blkno < ds->lowest || (ds->highest != 0 && blkno >= ds->highest)) {
134 xchk_da_set_corrupt(ds, level);
142 * The da btree scrubber can handle leaf1 blocks as a degenerate
143 * form of leafn blocks. Since the regular da code doesn't handle
144 * leaf1, we must multiplex the verifiers.
147 xchk_da_btree_read_verify(
150 struct xfs_da_blkinfo *info = bp->b_addr;
152 switch (be16_to_cpu(info->magic)) {
153 case XFS_DIR2_LEAF1_MAGIC:
154 case XFS_DIR3_LEAF1_MAGIC:
155 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
156 bp->b_ops->verify_read(bp);
160 * xfs_da3_node_buf_ops already know how to handle
161 * DA*_NODE, ATTR*_LEAF, and DIR*_LEAFN blocks.
163 bp->b_ops = &xfs_da3_node_buf_ops;
164 bp->b_ops->verify_read(bp);
169 xchk_da_btree_write_verify(
172 struct xfs_da_blkinfo *info = bp->b_addr;
174 switch (be16_to_cpu(info->magic)) {
175 case XFS_DIR2_LEAF1_MAGIC:
176 case XFS_DIR3_LEAF1_MAGIC:
177 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
178 bp->b_ops->verify_write(bp);
182 * xfs_da3_node_buf_ops already know how to handle
183 * DA*_NODE, ATTR*_LEAF, and DIR*_LEAFN blocks.
185 bp->b_ops = &xfs_da3_node_buf_ops;
186 bp->b_ops->verify_write(bp);
191 xchk_da_btree_verify(
194 struct xfs_da_blkinfo *info = bp->b_addr;
196 switch (be16_to_cpu(info->magic)) {
197 case XFS_DIR2_LEAF1_MAGIC:
198 case XFS_DIR3_LEAF1_MAGIC:
199 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
200 return bp->b_ops->verify_struct(bp);
202 bp->b_ops = &xfs_da3_node_buf_ops;
203 return bp->b_ops->verify_struct(bp);
207 static const struct xfs_buf_ops xchk_da_btree_buf_ops = {
208 .name = "xchk_da_btree",
209 .verify_read = xchk_da_btree_read_verify,
210 .verify_write = xchk_da_btree_write_verify,
211 .verify_struct = xchk_da_btree_verify,
214 /* Check a block's sibling. */
216 xchk_da_btree_block_check_sibling(
217 struct xchk_da_btree *ds,
222 struct xfs_da_state_path *path = &ds->state->path;
223 struct xfs_da_state_path *altpath = &ds->state->altpath;
228 memcpy(altpath, path, sizeof(ds->state->altpath));
231 * If the pointer is null, we shouldn't be able to move the upper
232 * level pointer anywhere.
235 error = xfs_da3_path_shift(ds->state, altpath, direction,
237 if (error == 0 && retval == 0)
238 xchk_da_set_corrupt(ds, level);
243 /* Move the alternate cursor one block in the direction given. */
244 error = xfs_da3_path_shift(ds->state, altpath, direction, false,
246 if (!xchk_da_process_error(ds, level, &error))
249 xchk_da_set_corrupt(ds, level);
252 if (altpath->blk[level].bp)
253 xchk_buffer_recheck(ds->sc, altpath->blk[level].bp);
255 /* Compare upper level pointer to sibling pointer. */
256 if (altpath->blk[level].blkno != sibling)
257 xchk_da_set_corrupt(ds, level);
260 /* Free all buffers in the altpath that aren't referenced from path. */
261 for (plevel = 0; plevel < altpath->active; plevel++) {
262 if (altpath->blk[plevel].bp == NULL ||
263 (plevel < path->active &&
264 altpath->blk[plevel].bp == path->blk[plevel].bp))
267 xfs_trans_brelse(ds->dargs.trans, altpath->blk[plevel].bp);
268 altpath->blk[plevel].bp = NULL;
274 /* Check a block's sibling pointers. */
276 xchk_da_btree_block_check_siblings(
277 struct xchk_da_btree *ds,
279 struct xfs_da_blkinfo *hdr)
285 forw = be32_to_cpu(hdr->forw);
286 back = be32_to_cpu(hdr->back);
288 /* Top level blocks should not have sibling pointers. */
290 if (forw != 0 || back != 0)
291 xchk_da_set_corrupt(ds, level);
296 * Check back (left) and forw (right) pointers. These functions
297 * absorb error codes for us.
299 error = xchk_da_btree_block_check_sibling(ds, level, 0, back);
302 error = xchk_da_btree_block_check_sibling(ds, level, 1, forw);
305 memset(&ds->state->altpath, 0, sizeof(ds->state->altpath));
309 /* Load a dir/attribute block from a btree. */
312 struct xchk_da_btree *ds,
316 struct xfs_da_state_blk *blk;
317 struct xfs_da_intnode *node;
318 struct xfs_da_node_entry *btree;
319 struct xfs_da3_blkinfo *hdr3;
320 struct xfs_da_args *dargs = &ds->dargs;
321 struct xfs_inode *ip = ds->dargs.dp;
324 struct xfs_da3_icnode_hdr nodehdr;
327 blk = &ds->state->path.blk[level];
328 ds->state->path.active = level + 1;
330 /* Release old block. */
332 xfs_trans_brelse(dargs->trans, blk->bp);
336 /* Check the pointer. */
338 if (!xchk_da_btree_ptr_ok(ds, level, blkno))
341 /* Read the buffer. */
342 error = xfs_da_read_buf(dargs->trans, dargs->dp, blk->blkno,
343 XFS_DABUF_MAP_HOLE_OK, &blk->bp, dargs->whichfork,
344 &xchk_da_btree_buf_ops);
345 if (!xchk_da_process_error(ds, level, &error))
348 xchk_buffer_recheck(ds->sc, blk->bp);
351 * We didn't find a dir btree root block, which means that
352 * there's no LEAF1/LEAFN tree (at least not where it's supposed
353 * to be), so jump out now.
355 if (ds->dargs.whichfork == XFS_DATA_FORK && level == 0 &&
359 /* It's /not/ ok for attr trees not to have a da btree. */
360 if (blk->bp == NULL) {
361 xchk_da_set_corrupt(ds, level);
365 hdr3 = blk->bp->b_addr;
366 blk->magic = be16_to_cpu(hdr3->hdr.magic);
367 pmaxrecs = &ds->maxrecs[level];
369 /* We only started zeroing the header on v5 filesystems. */
370 if (xfs_sb_version_hascrc(&ds->sc->mp->m_sb) && hdr3->hdr.pad)
371 xchk_da_set_corrupt(ds, level);
373 /* Check the owner. */
374 if (xfs_sb_version_hascrc(&ip->i_mount->m_sb)) {
375 owner = be64_to_cpu(hdr3->owner);
376 if (owner != ip->i_ino)
377 xchk_da_set_corrupt(ds, level);
380 /* Check the siblings. */
381 error = xchk_da_btree_block_check_siblings(ds, level, &hdr3->hdr);
385 /* Interpret the buffer. */
386 switch (blk->magic) {
387 case XFS_ATTR_LEAF_MAGIC:
388 case XFS_ATTR3_LEAF_MAGIC:
389 xfs_trans_buf_set_type(dargs->trans, blk->bp,
390 XFS_BLFT_ATTR_LEAF_BUF);
391 blk->magic = XFS_ATTR_LEAF_MAGIC;
392 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, pmaxrecs);
393 if (ds->tree_level != 0)
394 xchk_da_set_corrupt(ds, level);
396 case XFS_DIR2_LEAFN_MAGIC:
397 case XFS_DIR3_LEAFN_MAGIC:
398 xfs_trans_buf_set_type(dargs->trans, blk->bp,
399 XFS_BLFT_DIR_LEAFN_BUF);
400 blk->magic = XFS_DIR2_LEAFN_MAGIC;
401 blk->hashval = xfs_dir2_leaf_lasthash(ip, blk->bp, pmaxrecs);
402 if (ds->tree_level != 0)
403 xchk_da_set_corrupt(ds, level);
405 case XFS_DIR2_LEAF1_MAGIC:
406 case XFS_DIR3_LEAF1_MAGIC:
407 xfs_trans_buf_set_type(dargs->trans, blk->bp,
408 XFS_BLFT_DIR_LEAF1_BUF);
409 blk->magic = XFS_DIR2_LEAF1_MAGIC;
410 blk->hashval = xfs_dir2_leaf_lasthash(ip, blk->bp, pmaxrecs);
411 if (ds->tree_level != 0)
412 xchk_da_set_corrupt(ds, level);
414 case XFS_DA_NODE_MAGIC:
415 case XFS_DA3_NODE_MAGIC:
416 xfs_trans_buf_set_type(dargs->trans, blk->bp,
417 XFS_BLFT_DA_NODE_BUF);
418 blk->magic = XFS_DA_NODE_MAGIC;
419 node = blk->bp->b_addr;
420 xfs_da3_node_hdr_from_disk(ip->i_mount, &nodehdr, node);
421 btree = nodehdr.btree;
422 *pmaxrecs = nodehdr.count;
423 blk->hashval = be32_to_cpu(btree[*pmaxrecs - 1].hashval);
425 if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH) {
426 xchk_da_set_corrupt(ds, level);
429 ds->tree_level = nodehdr.level;
431 if (ds->tree_level != nodehdr.level) {
432 xchk_da_set_corrupt(ds, level);
437 /* XXX: Check hdr3.pad32 once we know how to fix it. */
440 xchk_da_set_corrupt(ds, level);
445 * If we've been handed a block that is below the dabtree root, does
446 * its hashval match what the parent block expected to see?
449 struct xfs_da_node_entry *key;
451 key = xchk_da_btree_node_entry(ds, level - 1);
452 if (be32_to_cpu(key->hashval) != blk->hashval) {
453 xchk_da_set_corrupt(ds, level);
461 xfs_trans_brelse(dargs->trans, blk->bp);
468 /* Visit all nodes and leaves of a da btree. */
471 struct xfs_scrub *sc,
473 xchk_da_btree_rec_fn scrub_fn,
476 struct xchk_da_btree ds = {};
477 struct xfs_mount *mp = sc->mp;
478 struct xfs_da_state_blk *blks;
479 struct xfs_da_node_entry *key;
484 /* Skip short format data structures; no btree to scan. */
485 if (!xfs_ifork_has_extents(XFS_IFORK_PTR(sc->ip, whichfork)))
488 /* Set up initial da state. */
489 ds.dargs.dp = sc->ip;
490 ds.dargs.whichfork = whichfork;
491 ds.dargs.trans = sc->tp;
492 ds.dargs.op_flags = XFS_DA_OP_OKNOENT;
493 ds.state = xfs_da_state_alloc(&ds.dargs);
495 ds.private = private;
496 if (whichfork == XFS_ATTR_FORK) {
497 ds.dargs.geo = mp->m_attr_geo;
501 ds.dargs.geo = mp->m_dir_geo;
502 ds.lowest = ds.dargs.geo->leafblk;
503 ds.highest = ds.dargs.geo->freeblk;
508 /* Find the root of the da tree, if present. */
509 blks = ds.state->path.blk;
510 error = xchk_da_btree_block(&ds, level, blkno);
514 * We didn't find a block at ds.lowest, which means that there's
515 * no LEAF1/LEAFN tree (at least not where it's supposed to be),
518 if (blks[level].bp == NULL)
521 blks[level].index = 0;
522 while (level >= 0 && level < XFS_DA_NODE_MAXDEPTH) {
523 /* Handle leaf block. */
524 if (blks[level].magic != XFS_DA_NODE_MAGIC) {
525 /* End of leaf, pop back towards the root. */
526 if (blks[level].index >= ds.maxrecs[level]) {
528 blks[level - 1].index++;
534 /* Dispatch record scrubbing. */
535 error = scrub_fn(&ds, level);
538 if (xchk_should_terminate(sc, &error) ||
539 (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
547 /* End of node, pop back towards the root. */
548 if (blks[level].index >= ds.maxrecs[level]) {
550 blks[level - 1].index++;
556 /* Hashes in order for scrub? */
557 key = xchk_da_btree_node_entry(&ds, level);
558 error = xchk_da_btree_hash(&ds, level, &key->hashval);
562 /* Drill another level deeper. */
563 blkno = be32_to_cpu(key->before);
565 if (level >= XFS_DA_NODE_MAXDEPTH) {
567 xchk_da_set_corrupt(&ds, level - 1);
571 error = xchk_da_btree_block(&ds, level, blkno);
574 if (blks[level].bp == NULL)
577 blks[level].index = 0;
581 /* Release all the buffers we're tracking. */
582 for (level = 0; level < XFS_DA_NODE_MAXDEPTH; level++) {
583 if (blks[level].bp == NULL)
585 xfs_trans_brelse(sc->tp, blks[level].bp);
586 blks[level].bp = NULL;
590 xfs_da_state_free(ds.state);