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_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 /* Find an entry at a certain level in a da btree. */
83 struct xchk_da_btree *ds,
88 struct xfs_da_state_blk *blk;
91 /* Dispatch the entry finding function. */
92 blk = &ds->state->path.blk[level];
93 baddr = blk->bp->b_addr;
95 case XFS_ATTR_LEAF_MAGIC:
96 case XFS_ATTR3_LEAF_MAGIC:
97 ents = (char *)xfs_attr3_leaf_entryp(baddr);
98 return ents + (rec * sizeof(struct xfs_attr_leaf_entry));
99 case XFS_DIR2_LEAFN_MAGIC:
100 case XFS_DIR3_LEAFN_MAGIC:
101 ents = (char *)ds->dargs.dp->d_ops->leaf_ents_p(baddr);
102 return ents + (rec * sizeof(struct xfs_dir2_leaf_entry));
103 case XFS_DIR2_LEAF1_MAGIC:
104 case XFS_DIR3_LEAF1_MAGIC:
105 ents = (char *)ds->dargs.dp->d_ops->leaf_ents_p(baddr);
106 return ents + (rec * sizeof(struct xfs_dir2_leaf_entry));
107 case XFS_DA_NODE_MAGIC:
108 case XFS_DA3_NODE_MAGIC:
109 ents = (char *)ds->dargs.dp->d_ops->node_tree_p(baddr);
110 return ents + (rec * sizeof(struct xfs_da_node_entry));
116 /* Scrub a da btree hash (key). */
119 struct xchk_da_btree *ds,
123 struct xfs_da_state_blk *blks;
124 struct xfs_da_node_entry *entry;
126 xfs_dahash_t parent_hash;
128 /* Is this hash in order? */
129 hash = be32_to_cpu(*hashp);
130 if (hash < ds->hashes[level])
131 xchk_da_set_corrupt(ds, level);
132 ds->hashes[level] = hash;
137 /* Is this hash no larger than the parent hash? */
138 blks = ds->state->path.blk;
139 entry = xchk_da_btree_entry(ds, level - 1, blks[level - 1].index);
140 parent_hash = be32_to_cpu(entry->hashval);
141 if (parent_hash < hash)
142 xchk_da_set_corrupt(ds, level);
148 * Check a da btree pointer. Returns true if it's ok to use this
152 xchk_da_btree_ptr_ok(
153 struct xchk_da_btree *ds,
157 if (blkno < ds->lowest || (ds->highest != 0 && blkno >= ds->highest)) {
158 xchk_da_set_corrupt(ds, level);
166 * The da btree scrubber can handle leaf1 blocks as a degenerate
167 * form of leafn blocks. Since the regular da code doesn't handle
168 * leaf1, we must multiplex the verifiers.
171 xchk_da_btree_read_verify(
174 struct xfs_da_blkinfo *info = bp->b_addr;
176 switch (be16_to_cpu(info->magic)) {
177 case XFS_DIR2_LEAF1_MAGIC:
178 case XFS_DIR3_LEAF1_MAGIC:
179 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
180 bp->b_ops->verify_read(bp);
184 * xfs_da3_node_buf_ops already know how to handle
185 * DA*_NODE, ATTR*_LEAF, and DIR*_LEAFN blocks.
187 bp->b_ops = &xfs_da3_node_buf_ops;
188 bp->b_ops->verify_read(bp);
193 xchk_da_btree_write_verify(
196 struct xfs_da_blkinfo *info = bp->b_addr;
198 switch (be16_to_cpu(info->magic)) {
199 case XFS_DIR2_LEAF1_MAGIC:
200 case XFS_DIR3_LEAF1_MAGIC:
201 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
202 bp->b_ops->verify_write(bp);
206 * xfs_da3_node_buf_ops already know how to handle
207 * DA*_NODE, ATTR*_LEAF, and DIR*_LEAFN blocks.
209 bp->b_ops = &xfs_da3_node_buf_ops;
210 bp->b_ops->verify_write(bp);
215 xchk_da_btree_verify(
218 struct xfs_da_blkinfo *info = bp->b_addr;
220 switch (be16_to_cpu(info->magic)) {
221 case XFS_DIR2_LEAF1_MAGIC:
222 case XFS_DIR3_LEAF1_MAGIC:
223 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
224 return bp->b_ops->verify_struct(bp);
226 bp->b_ops = &xfs_da3_node_buf_ops;
227 return bp->b_ops->verify_struct(bp);
231 static const struct xfs_buf_ops xchk_da_btree_buf_ops = {
232 .name = "xchk_da_btree",
233 .verify_read = xchk_da_btree_read_verify,
234 .verify_write = xchk_da_btree_write_verify,
235 .verify_struct = xchk_da_btree_verify,
238 /* Check a block's sibling. */
240 xchk_da_btree_block_check_sibling(
241 struct xchk_da_btree *ds,
249 memcpy(&ds->state->altpath, &ds->state->path,
250 sizeof(ds->state->altpath));
253 * If the pointer is null, we shouldn't be able to move the upper
254 * level pointer anywhere.
257 error = xfs_da3_path_shift(ds->state, &ds->state->altpath,
258 direction, false, &retval);
259 if (error == 0 && retval == 0)
260 xchk_da_set_corrupt(ds, level);
265 /* Move the alternate cursor one block in the direction given. */
266 error = xfs_da3_path_shift(ds->state, &ds->state->altpath,
267 direction, false, &retval);
268 if (!xchk_da_process_error(ds, level, &error))
271 xchk_da_set_corrupt(ds, level);
274 if (ds->state->altpath.blk[level].bp)
275 xchk_buffer_recheck(ds->sc,
276 ds->state->altpath.blk[level].bp);
278 /* Compare upper level pointer to sibling pointer. */
279 if (ds->state->altpath.blk[level].blkno != sibling)
280 xchk_da_set_corrupt(ds, level);
281 xfs_trans_brelse(ds->dargs.trans, ds->state->altpath.blk[level].bp);
286 /* Check a block's sibling pointers. */
288 xchk_da_btree_block_check_siblings(
289 struct xchk_da_btree *ds,
291 struct xfs_da_blkinfo *hdr)
297 forw = be32_to_cpu(hdr->forw);
298 back = be32_to_cpu(hdr->back);
300 /* Top level blocks should not have sibling pointers. */
302 if (forw != 0 || back != 0)
303 xchk_da_set_corrupt(ds, level);
308 * Check back (left) and forw (right) pointers. These functions
309 * absorb error codes for us.
311 error = xchk_da_btree_block_check_sibling(ds, level, 0, back);
314 error = xchk_da_btree_block_check_sibling(ds, level, 1, forw);
317 memset(&ds->state->altpath, 0, sizeof(ds->state->altpath));
321 /* Load a dir/attribute block from a btree. */
324 struct xchk_da_btree *ds,
328 struct xfs_da_state_blk *blk;
329 struct xfs_da_intnode *node;
330 struct xfs_da_node_entry *btree;
331 struct xfs_da3_blkinfo *hdr3;
332 struct xfs_da_args *dargs = &ds->dargs;
333 struct xfs_inode *ip = ds->dargs.dp;
336 struct xfs_da3_icnode_hdr nodehdr;
339 blk = &ds->state->path.blk[level];
340 ds->state->path.active = level + 1;
342 /* Release old block. */
344 xfs_trans_brelse(dargs->trans, blk->bp);
348 /* Check the pointer. */
350 if (!xchk_da_btree_ptr_ok(ds, level, blkno))
353 /* Read the buffer. */
354 error = xfs_da_read_buf(dargs->trans, dargs->dp, blk->blkno, -2,
355 &blk->bp, dargs->whichfork,
356 &xchk_da_btree_buf_ops);
357 if (!xchk_da_process_error(ds, level, &error))
360 xchk_buffer_recheck(ds->sc, blk->bp);
363 * We didn't find a dir btree root block, which means that
364 * there's no LEAF1/LEAFN tree (at least not where it's supposed
365 * to be), so jump out now.
367 if (ds->dargs.whichfork == XFS_DATA_FORK && level == 0 &&
371 /* It's /not/ ok for attr trees not to have a da btree. */
372 if (blk->bp == NULL) {
373 xchk_da_set_corrupt(ds, level);
377 hdr3 = blk->bp->b_addr;
378 blk->magic = be16_to_cpu(hdr3->hdr.magic);
379 pmaxrecs = &ds->maxrecs[level];
381 /* We only started zeroing the header on v5 filesystems. */
382 if (xfs_sb_version_hascrc(&ds->sc->mp->m_sb) && hdr3->hdr.pad)
383 xchk_da_set_corrupt(ds, level);
385 /* Check the owner. */
386 if (xfs_sb_version_hascrc(&ip->i_mount->m_sb)) {
387 owner = be64_to_cpu(hdr3->owner);
388 if (owner != ip->i_ino)
389 xchk_da_set_corrupt(ds, level);
392 /* Check the siblings. */
393 error = xchk_da_btree_block_check_siblings(ds, level, &hdr3->hdr);
397 /* Interpret the buffer. */
398 switch (blk->magic) {
399 case XFS_ATTR_LEAF_MAGIC:
400 case XFS_ATTR3_LEAF_MAGIC:
401 xfs_trans_buf_set_type(dargs->trans, blk->bp,
402 XFS_BLFT_ATTR_LEAF_BUF);
403 blk->magic = XFS_ATTR_LEAF_MAGIC;
404 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, pmaxrecs);
405 if (ds->tree_level != 0)
406 xchk_da_set_corrupt(ds, level);
408 case XFS_DIR2_LEAFN_MAGIC:
409 case XFS_DIR3_LEAFN_MAGIC:
410 xfs_trans_buf_set_type(dargs->trans, blk->bp,
411 XFS_BLFT_DIR_LEAFN_BUF);
412 blk->magic = XFS_DIR2_LEAFN_MAGIC;
413 blk->hashval = xfs_dir2_leaf_lasthash(ip, blk->bp, pmaxrecs);
414 if (ds->tree_level != 0)
415 xchk_da_set_corrupt(ds, level);
417 case XFS_DIR2_LEAF1_MAGIC:
418 case XFS_DIR3_LEAF1_MAGIC:
419 xfs_trans_buf_set_type(dargs->trans, blk->bp,
420 XFS_BLFT_DIR_LEAF1_BUF);
421 blk->magic = XFS_DIR2_LEAF1_MAGIC;
422 blk->hashval = xfs_dir2_leaf_lasthash(ip, blk->bp, pmaxrecs);
423 if (ds->tree_level != 0)
424 xchk_da_set_corrupt(ds, level);
426 case XFS_DA_NODE_MAGIC:
427 case XFS_DA3_NODE_MAGIC:
428 xfs_trans_buf_set_type(dargs->trans, blk->bp,
429 XFS_BLFT_DA_NODE_BUF);
430 blk->magic = XFS_DA_NODE_MAGIC;
431 node = blk->bp->b_addr;
432 ip->d_ops->node_hdr_from_disk(&nodehdr, node);
433 btree = ip->d_ops->node_tree_p(node);
434 *pmaxrecs = nodehdr.count;
435 blk->hashval = be32_to_cpu(btree[*pmaxrecs - 1].hashval);
437 if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH) {
438 xchk_da_set_corrupt(ds, level);
441 ds->tree_level = nodehdr.level;
443 if (ds->tree_level != nodehdr.level) {
444 xchk_da_set_corrupt(ds, level);
449 /* XXX: Check hdr3.pad32 once we know how to fix it. */
452 xchk_da_set_corrupt(ds, level);
459 xfs_trans_brelse(dargs->trans, blk->bp);
466 /* Visit all nodes and leaves of a da btree. */
469 struct xfs_scrub *sc,
471 xchk_da_btree_rec_fn scrub_fn,
474 struct xchk_da_btree ds = {};
475 struct xfs_mount *mp = sc->mp;
476 struct xfs_da_state_blk *blks;
477 struct xfs_da_node_entry *key;
483 /* Skip short format data structures; no btree to scan. */
484 if (XFS_IFORK_FORMAT(sc->ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
485 XFS_IFORK_FORMAT(sc->ip, whichfork) != XFS_DINODE_FMT_BTREE)
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();
494 ds.state->args = &ds.dargs;
497 ds.private = private;
498 if (whichfork == XFS_ATTR_FORK) {
499 ds.dargs.geo = mp->m_attr_geo;
503 ds.dargs.geo = mp->m_dir_geo;
504 ds.lowest = ds.dargs.geo->leafblk;
505 ds.highest = ds.dargs.geo->freeblk;
510 /* Find the root of the da tree, if present. */
511 blks = ds.state->path.blk;
512 error = xchk_da_btree_block(&ds, level, blkno);
516 * We didn't find a block at ds.lowest, which means that there's
517 * no LEAF1/LEAFN tree (at least not where it's supposed to be),
520 if (blks[level].bp == NULL)
523 blks[level].index = 0;
524 while (level >= 0 && level < XFS_DA_NODE_MAXDEPTH) {
525 /* Handle leaf block. */
526 if (blks[level].magic != XFS_DA_NODE_MAGIC) {
527 /* End of leaf, pop back towards the root. */
528 if (blks[level].index >= ds.maxrecs[level]) {
530 blks[level - 1].index++;
536 /* Dispatch record scrubbing. */
537 rec = xchk_da_btree_entry(&ds, level,
539 error = scrub_fn(&ds, level, rec);
542 if (xchk_should_terminate(sc, &error) ||
543 (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
551 /* End of node, pop back towards the root. */
552 if (blks[level].index >= ds.maxrecs[level]) {
554 blks[level - 1].index++;
560 /* Hashes in order for scrub? */
561 key = xchk_da_btree_entry(&ds, level, blks[level].index);
562 error = xchk_da_btree_hash(&ds, level, &key->hashval);
566 /* Drill another level deeper. */
567 blkno = be32_to_cpu(key->before);
569 if (level >= XFS_DA_NODE_MAXDEPTH) {
571 xchk_da_set_corrupt(&ds, level - 1);
575 error = xchk_da_btree_block(&ds, level, blkno);
578 if (blks[level].bp == NULL)
581 blks[level].index = 0;
585 /* Release all the buffers we're tracking. */
586 for (level = 0; level < XFS_DA_NODE_MAXDEPTH; level++) {
587 if (blks[level].bp == NULL)
589 xfs_trans_brelse(sc->tp, blks[level].bp);
590 blks[level].bp = NULL;
594 xfs_da_state_free(ds.state);