dabtree.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2017-2023 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <djwong@kernel.org>
  5. */
  6. #include "xfs_platform.h"
  7. #include "xfs_fs.h"
  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_dir2.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"
  22. /* Directory/Attribute Btree */
  23. /*
  24. * Check for da btree operation errors. See the section about handling
  25. * operational errors in common.c.
  26. */
  27. bool
  28. xchk_da_process_error(
  29. struct xchk_da_btree *ds,
  30. int level,
  31. int *error)
  32. {
  33. struct xfs_scrub *sc = ds->sc;
  34. if (*error == 0)
  35. return true;
  36. switch (*error) {
  37. case -EDEADLOCK:
  38. case -ECHRNG:
  39. /* Used to restart an op with deadlock avoidance. */
  40. trace_xchk_deadlock_retry(sc->ip, sc->sm, *error);
  41. break;
  42. case -EFSBADCRC:
  43. case -EFSCORRUPTED:
  44. case -EIO:
  45. case -ENODATA:
  46. /* Note the badness but don't abort. */
  47. sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
  48. *error = 0;
  49. fallthrough;
  50. default:
  51. trace_xchk_file_op_error(sc, ds->dargs.whichfork,
  52. xfs_dir2_da_to_db(ds->dargs.geo,
  53. ds->state->path.blk[level].blkno),
  54. *error, __return_address);
  55. break;
  56. }
  57. return false;
  58. }
  59. /*
  60. * Check for da btree corruption. See the section about handling
  61. * operational errors in common.c.
  62. */
  63. void
  64. xchk_da_set_corrupt(
  65. struct xchk_da_btree *ds,
  66. int level)
  67. {
  68. struct xfs_scrub *sc = ds->sc;
  69. sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
  70. trace_xchk_fblock_error(sc, ds->dargs.whichfork,
  71. xfs_dir2_da_to_db(ds->dargs.geo,
  72. ds->state->path.blk[level].blkno),
  73. __return_address);
  74. }
  75. /* Flag a da btree node in need of optimization. */
  76. void
  77. xchk_da_set_preen(
  78. struct xchk_da_btree *ds,
  79. int level)
  80. {
  81. struct xfs_scrub *sc = ds->sc;
  82. sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
  83. trace_xchk_fblock_preen(sc, ds->dargs.whichfork,
  84. xfs_dir2_da_to_db(ds->dargs.geo,
  85. ds->state->path.blk[level].blkno),
  86. __return_address);
  87. }
  88. /* Find an entry at a certain level in a da btree. */
  89. static struct xfs_da_node_entry *
  90. xchk_da_btree_node_entry(
  91. struct xchk_da_btree *ds,
  92. int level)
  93. {
  94. struct xfs_da_state_blk *blk = &ds->state->path.blk[level];
  95. struct xfs_da3_icnode_hdr hdr;
  96. ASSERT(blk->magic == XFS_DA_NODE_MAGIC);
  97. xfs_da3_node_hdr_from_disk(ds->sc->mp, &hdr, blk->bp->b_addr);
  98. return hdr.btree + blk->index;
  99. }
  100. /* Scrub a da btree hash (key). */
  101. int
  102. xchk_da_btree_hash(
  103. struct xchk_da_btree *ds,
  104. int level,
  105. __be32 *hashp)
  106. {
  107. struct xfs_da_node_entry *entry;
  108. xfs_dahash_t hash;
  109. xfs_dahash_t parent_hash;
  110. /* Is this hash in order? */
  111. hash = be32_to_cpu(*hashp);
  112. if (hash < ds->hashes[level])
  113. xchk_da_set_corrupt(ds, level);
  114. ds->hashes[level] = hash;
  115. if (level == 0)
  116. return 0;
  117. /* Is this hash no larger than the parent hash? */
  118. entry = xchk_da_btree_node_entry(ds, level - 1);
  119. parent_hash = be32_to_cpu(entry->hashval);
  120. if (parent_hash < hash)
  121. xchk_da_set_corrupt(ds, level);
  122. return 0;
  123. }
  124. /*
  125. * Check a da btree pointer. Returns true if it's ok to use this
  126. * pointer.
  127. */
  128. STATIC bool
  129. xchk_da_btree_ptr_ok(
  130. struct xchk_da_btree *ds,
  131. int level,
  132. xfs_dablk_t blkno)
  133. {
  134. if (blkno < ds->lowest || (ds->highest != 0 && blkno >= ds->highest)) {
  135. xchk_da_set_corrupt(ds, level);
  136. return false;
  137. }
  138. return true;
  139. }
  140. /*
  141. * The da btree scrubber can handle leaf1 blocks as a degenerate
  142. * form of leafn blocks. Since the regular da code doesn't handle
  143. * leaf1, we must multiplex the verifiers.
  144. */
  145. static void
  146. xchk_da_btree_read_verify(
  147. struct xfs_buf *bp)
  148. {
  149. struct xfs_da_blkinfo *info = bp->b_addr;
  150. switch (be16_to_cpu(info->magic)) {
  151. case XFS_DIR2_LEAF1_MAGIC:
  152. case XFS_DIR3_LEAF1_MAGIC:
  153. bp->b_ops = &xfs_dir3_leaf1_buf_ops;
  154. bp->b_ops->verify_read(bp);
  155. return;
  156. default:
  157. /*
  158. * xfs_da3_node_buf_ops already know how to handle
  159. * DA*_NODE, ATTR*_LEAF, and DIR*_LEAFN blocks.
  160. */
  161. bp->b_ops = &xfs_da3_node_buf_ops;
  162. bp->b_ops->verify_read(bp);
  163. return;
  164. }
  165. }
  166. static void
  167. xchk_da_btree_write_verify(
  168. struct xfs_buf *bp)
  169. {
  170. struct xfs_da_blkinfo *info = bp->b_addr;
  171. switch (be16_to_cpu(info->magic)) {
  172. case XFS_DIR2_LEAF1_MAGIC:
  173. case XFS_DIR3_LEAF1_MAGIC:
  174. bp->b_ops = &xfs_dir3_leaf1_buf_ops;
  175. bp->b_ops->verify_write(bp);
  176. return;
  177. default:
  178. /*
  179. * xfs_da3_node_buf_ops already know how to handle
  180. * DA*_NODE, ATTR*_LEAF, and DIR*_LEAFN blocks.
  181. */
  182. bp->b_ops = &xfs_da3_node_buf_ops;
  183. bp->b_ops->verify_write(bp);
  184. return;
  185. }
  186. }
  187. static void *
  188. xchk_da_btree_verify(
  189. struct xfs_buf *bp)
  190. {
  191. struct xfs_da_blkinfo *info = bp->b_addr;
  192. switch (be16_to_cpu(info->magic)) {
  193. case XFS_DIR2_LEAF1_MAGIC:
  194. case XFS_DIR3_LEAF1_MAGIC:
  195. bp->b_ops = &xfs_dir3_leaf1_buf_ops;
  196. return bp->b_ops->verify_struct(bp);
  197. default:
  198. bp->b_ops = &xfs_da3_node_buf_ops;
  199. return bp->b_ops->verify_struct(bp);
  200. }
  201. }
  202. static const struct xfs_buf_ops xchk_da_btree_buf_ops = {
  203. .name = "xchk_da_btree",
  204. .verify_read = xchk_da_btree_read_verify,
  205. .verify_write = xchk_da_btree_write_verify,
  206. .verify_struct = xchk_da_btree_verify,
  207. };
  208. /* Check a block's sibling. */
  209. STATIC int
  210. xchk_da_btree_block_check_sibling(
  211. struct xchk_da_btree *ds,
  212. int level,
  213. int direction,
  214. xfs_dablk_t sibling)
  215. {
  216. struct xfs_da_state_path *path = &ds->state->path;
  217. struct xfs_da_state_path *altpath = &ds->state->altpath;
  218. int retval;
  219. int plevel;
  220. int error;
  221. memcpy(altpath, path, sizeof(ds->state->altpath));
  222. /*
  223. * If the pointer is null, we shouldn't be able to move the upper
  224. * level pointer anywhere.
  225. */
  226. if (sibling == 0) {
  227. error = xfs_da3_path_shift(ds->state, altpath, direction,
  228. false, &retval);
  229. if (error == 0 && retval == 0)
  230. xchk_da_set_corrupt(ds, level);
  231. error = 0;
  232. goto out;
  233. }
  234. /* Move the alternate cursor one block in the direction given. */
  235. error = xfs_da3_path_shift(ds->state, altpath, direction, false,
  236. &retval);
  237. if (!xchk_da_process_error(ds, level, &error))
  238. goto out;
  239. if (retval) {
  240. xchk_da_set_corrupt(ds, level);
  241. goto out;
  242. }
  243. if (altpath->blk[level].bp)
  244. xchk_buffer_recheck(ds->sc, altpath->blk[level].bp);
  245. /* Compare upper level pointer to sibling pointer. */
  246. if (altpath->blk[level].blkno != sibling)
  247. xchk_da_set_corrupt(ds, level);
  248. out:
  249. /* Free all buffers in the altpath that aren't referenced from path. */
  250. for (plevel = 0; plevel < altpath->active; plevel++) {
  251. if (altpath->blk[plevel].bp == NULL ||
  252. (plevel < path->active &&
  253. altpath->blk[plevel].bp == path->blk[plevel].bp))
  254. continue;
  255. xfs_trans_brelse(ds->dargs.trans, altpath->blk[plevel].bp);
  256. altpath->blk[plevel].bp = NULL;
  257. }
  258. return error;
  259. }
  260. /* Check a block's sibling pointers. */
  261. STATIC int
  262. xchk_da_btree_block_check_siblings(
  263. struct xchk_da_btree *ds,
  264. int level,
  265. struct xfs_da_blkinfo *hdr)
  266. {
  267. xfs_dablk_t forw;
  268. xfs_dablk_t back;
  269. int error = 0;
  270. forw = be32_to_cpu(hdr->forw);
  271. back = be32_to_cpu(hdr->back);
  272. /* Top level blocks should not have sibling pointers. */
  273. if (level == 0) {
  274. if (forw != 0 || back != 0)
  275. xchk_da_set_corrupt(ds, level);
  276. return 0;
  277. }
  278. /*
  279. * Check back (left) and forw (right) pointers. These functions
  280. * absorb error codes for us.
  281. */
  282. error = xchk_da_btree_block_check_sibling(ds, level, 0, back);
  283. if (error)
  284. goto out;
  285. error = xchk_da_btree_block_check_sibling(ds, level, 1, forw);
  286. out:
  287. memset(&ds->state->altpath, 0, sizeof(ds->state->altpath));
  288. return error;
  289. }
  290. /* Load a dir/attribute block from a btree. */
  291. STATIC int
  292. xchk_da_btree_block(
  293. struct xchk_da_btree *ds,
  294. int level,
  295. xfs_dablk_t blkno)
  296. {
  297. struct xfs_da_state_blk *blk;
  298. struct xfs_da_intnode *node;
  299. struct xfs_da_node_entry *btree;
  300. struct xfs_da3_blkinfo *hdr3;
  301. struct xfs_da_args *dargs = &ds->dargs;
  302. struct xfs_inode *ip = ds->dargs.dp;
  303. xfs_failaddr_t fa;
  304. xfs_ino_t owner;
  305. int *pmaxrecs;
  306. struct xfs_da3_icnode_hdr nodehdr;
  307. int error = 0;
  308. blk = &ds->state->path.blk[level];
  309. ds->state->path.active = level + 1;
  310. /* Release old block. */
  311. if (blk->bp) {
  312. xfs_trans_brelse(dargs->trans, blk->bp);
  313. blk->bp = NULL;
  314. }
  315. /* Check the pointer. */
  316. blk->blkno = blkno;
  317. if (!xchk_da_btree_ptr_ok(ds, level, blkno))
  318. goto out_nobuf;
  319. /* Read the buffer. */
  320. error = xfs_da_read_buf(dargs->trans, dargs->dp, blk->blkno,
  321. XFS_DABUF_MAP_HOLE_OK, &blk->bp, dargs->whichfork,
  322. &xchk_da_btree_buf_ops);
  323. if (!xchk_da_process_error(ds, level, &error))
  324. goto out_nobuf;
  325. if (blk->bp)
  326. xchk_buffer_recheck(ds->sc, blk->bp);
  327. /*
  328. * We didn't find a dir btree root block, which means that
  329. * there's no LEAF1/LEAFN tree (at least not where it's supposed
  330. * to be), so jump out now.
  331. */
  332. if (ds->dargs.whichfork == XFS_DATA_FORK && level == 0 &&
  333. blk->bp == NULL)
  334. goto out_nobuf;
  335. /* It's /not/ ok for attr trees not to have a da btree. */
  336. if (blk->bp == NULL) {
  337. xchk_da_set_corrupt(ds, level);
  338. goto out_nobuf;
  339. }
  340. hdr3 = blk->bp->b_addr;
  341. blk->magic = be16_to_cpu(hdr3->hdr.magic);
  342. pmaxrecs = &ds->maxrecs[level];
  343. /* We only started zeroing the header on v5 filesystems. */
  344. if (xfs_has_crc(ds->sc->mp) && hdr3->hdr.pad)
  345. xchk_da_set_corrupt(ds, level);
  346. /* Check the owner. */
  347. if (xfs_has_crc(ip->i_mount)) {
  348. owner = be64_to_cpu(hdr3->owner);
  349. if (owner != ip->i_ino)
  350. xchk_da_set_corrupt(ds, level);
  351. }
  352. /* Check the siblings. */
  353. error = xchk_da_btree_block_check_siblings(ds, level, &hdr3->hdr);
  354. if (error)
  355. goto out;
  356. /* Interpret the buffer. */
  357. switch (blk->magic) {
  358. case XFS_ATTR_LEAF_MAGIC:
  359. case XFS_ATTR3_LEAF_MAGIC:
  360. xfs_trans_buf_set_type(dargs->trans, blk->bp,
  361. XFS_BLFT_ATTR_LEAF_BUF);
  362. blk->magic = XFS_ATTR_LEAF_MAGIC;
  363. blk->hashval = xfs_attr_leaf_lasthash(blk->bp, pmaxrecs);
  364. if (ds->tree_level != 0)
  365. xchk_da_set_corrupt(ds, level);
  366. break;
  367. case XFS_DIR2_LEAFN_MAGIC:
  368. case XFS_DIR3_LEAFN_MAGIC:
  369. xfs_trans_buf_set_type(dargs->trans, blk->bp,
  370. XFS_BLFT_DIR_LEAFN_BUF);
  371. blk->magic = XFS_DIR2_LEAFN_MAGIC;
  372. blk->hashval = xfs_dir2_leaf_lasthash(ip, blk->bp, pmaxrecs);
  373. if (ds->tree_level != 0)
  374. xchk_da_set_corrupt(ds, level);
  375. break;
  376. case XFS_DIR2_LEAF1_MAGIC:
  377. case XFS_DIR3_LEAF1_MAGIC:
  378. xfs_trans_buf_set_type(dargs->trans, blk->bp,
  379. XFS_BLFT_DIR_LEAF1_BUF);
  380. blk->magic = XFS_DIR2_LEAF1_MAGIC;
  381. blk->hashval = xfs_dir2_leaf_lasthash(ip, blk->bp, pmaxrecs);
  382. if (ds->tree_level != 0)
  383. xchk_da_set_corrupt(ds, level);
  384. break;
  385. case XFS_DA_NODE_MAGIC:
  386. case XFS_DA3_NODE_MAGIC:
  387. xfs_trans_buf_set_type(dargs->trans, blk->bp,
  388. XFS_BLFT_DA_NODE_BUF);
  389. blk->magic = XFS_DA_NODE_MAGIC;
  390. node = blk->bp->b_addr;
  391. xfs_da3_node_hdr_from_disk(ip->i_mount, &nodehdr, node);
  392. btree = nodehdr.btree;
  393. *pmaxrecs = nodehdr.count;
  394. blk->hashval = be32_to_cpu(btree[*pmaxrecs - 1].hashval);
  395. if (level == 0) {
  396. if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH) {
  397. xchk_da_set_corrupt(ds, level);
  398. goto out_freebp;
  399. }
  400. ds->tree_level = nodehdr.level;
  401. } else {
  402. if (ds->tree_level != nodehdr.level) {
  403. xchk_da_set_corrupt(ds, level);
  404. goto out_freebp;
  405. }
  406. }
  407. /* XXX: Check hdr3.pad32 once we know how to fix it. */
  408. break;
  409. default:
  410. xchk_da_set_corrupt(ds, level);
  411. goto out_freebp;
  412. }
  413. fa = xfs_da3_header_check(blk->bp, dargs->owner);
  414. if (fa) {
  415. xchk_da_set_corrupt(ds, level);
  416. goto out_freebp;
  417. }
  418. /*
  419. * If we've been handed a block that is below the dabtree root, does
  420. * its hashval match what the parent block expected to see?
  421. */
  422. if (level > 0) {
  423. struct xfs_da_node_entry *key;
  424. key = xchk_da_btree_node_entry(ds, level - 1);
  425. if (be32_to_cpu(key->hashval) != blk->hashval) {
  426. xchk_da_set_corrupt(ds, level);
  427. goto out_freebp;
  428. }
  429. }
  430. out:
  431. return error;
  432. out_freebp:
  433. xfs_trans_brelse(dargs->trans, blk->bp);
  434. blk->bp = NULL;
  435. out_nobuf:
  436. blk->blkno = 0;
  437. return error;
  438. }
  439. /* Visit all nodes and leaves of a da btree. */
  440. int
  441. xchk_da_btree(
  442. struct xfs_scrub *sc,
  443. int whichfork,
  444. xchk_da_btree_rec_fn scrub_fn,
  445. void *private)
  446. {
  447. struct xchk_da_btree *ds;
  448. struct xfs_mount *mp = sc->mp;
  449. struct xfs_da_state_blk *blks;
  450. struct xfs_da_node_entry *key;
  451. xfs_dablk_t blkno;
  452. int level;
  453. int error;
  454. /* Skip short format data structures; no btree to scan. */
  455. if (!xfs_ifork_has_extents(xfs_ifork_ptr(sc->ip, whichfork)))
  456. return 0;
  457. /* Set up initial da state. */
  458. ds = kzalloc_obj(struct xchk_da_btree, XCHK_GFP_FLAGS);
  459. if (!ds)
  460. return -ENOMEM;
  461. ds->dargs.dp = sc->ip;
  462. ds->dargs.whichfork = whichfork;
  463. ds->dargs.trans = sc->tp;
  464. ds->dargs.op_flags = XFS_DA_OP_OKNOENT;
  465. ds->dargs.owner = sc->ip->i_ino;
  466. ds->state = xfs_da_state_alloc(&ds->dargs);
  467. ds->sc = sc;
  468. ds->private = private;
  469. if (whichfork == XFS_ATTR_FORK) {
  470. ds->dargs.geo = mp->m_attr_geo;
  471. ds->lowest = 0;
  472. ds->highest = 0;
  473. } else {
  474. ds->dargs.geo = mp->m_dir_geo;
  475. ds->lowest = ds->dargs.geo->leafblk;
  476. ds->highest = ds->dargs.geo->freeblk;
  477. }
  478. blkno = ds->lowest;
  479. level = 0;
  480. /* Find the root of the da tree, if present. */
  481. blks = ds->state->path.blk;
  482. error = xchk_da_btree_block(ds, level, blkno);
  483. if (error)
  484. goto out_state;
  485. /*
  486. * We didn't find a block at ds->lowest, which means that there's
  487. * no LEAF1/LEAFN tree (at least not where it's supposed to be),
  488. * so jump out now.
  489. */
  490. if (blks[level].bp == NULL)
  491. goto out_state;
  492. blks[level].index = 0;
  493. while (level >= 0 && level < XFS_DA_NODE_MAXDEPTH) {
  494. /* Handle leaf block. */
  495. if (blks[level].magic != XFS_DA_NODE_MAGIC) {
  496. /* End of leaf, pop back towards the root. */
  497. if (blks[level].index >= ds->maxrecs[level]) {
  498. if (level > 0)
  499. blks[level - 1].index++;
  500. ds->tree_level++;
  501. level--;
  502. continue;
  503. }
  504. /* Dispatch record scrubbing. */
  505. error = scrub_fn(ds, level);
  506. if (error)
  507. break;
  508. if (xchk_should_terminate(sc, &error) ||
  509. (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
  510. break;
  511. blks[level].index++;
  512. continue;
  513. }
  514. /* End of node, pop back towards the root. */
  515. if (blks[level].index >= ds->maxrecs[level]) {
  516. if (level > 0)
  517. blks[level - 1].index++;
  518. ds->tree_level++;
  519. level--;
  520. continue;
  521. }
  522. /* Hashes in order for scrub? */
  523. key = xchk_da_btree_node_entry(ds, level);
  524. error = xchk_da_btree_hash(ds, level, &key->hashval);
  525. if (error)
  526. goto out;
  527. /* Drill another level deeper. */
  528. blkno = be32_to_cpu(key->before);
  529. level++;
  530. if (level >= XFS_DA_NODE_MAXDEPTH) {
  531. /* Too deep! */
  532. xchk_da_set_corrupt(ds, level - 1);
  533. break;
  534. }
  535. ds->tree_level--;
  536. error = xchk_da_btree_block(ds, level, blkno);
  537. if (error)
  538. goto out;
  539. if (blks[level].bp == NULL)
  540. goto out;
  541. blks[level].index = 0;
  542. }
  543. out:
  544. /* Release all the buffers we're tracking. */
  545. for (level = 0; level < XFS_DA_NODE_MAXDEPTH; level++) {
  546. if (blks[level].bp == NULL)
  547. continue;
  548. xfs_trans_brelse(sc->tp, blks[level].bp);
  549. blks[level].bp = NULL;
  550. }
  551. out_state:
  552. xfs_da_state_free(ds->state);
  553. kfree(ds);
  554. return error;
  555. }