dabtree.c 15 KB

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