dabtree.c 14 KB

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