dabtree.c 14 KB

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