ialloc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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_alloc.h"
  20. #include "xfs_ialloc.h"
  21. #include "xfs_ialloc_btree.h"
  22. #include "xfs_icache.h"
  23. #include "xfs_rmap.h"
  24. #include "xfs_log.h"
  25. #include "xfs_trans_priv.h"
  26. #include "scrub/xfs_scrub.h"
  27. #include "scrub/scrub.h"
  28. #include "scrub/common.h"
  29. #include "scrub/btree.h"
  30. #include "scrub/trace.h"
  31. /*
  32. * Set us up to scrub inode btrees.
  33. * If we detect a discrepancy between the inobt and the inode,
  34. * try again after forcing logged inode cores out to disk.
  35. */
  36. int
  37. xchk_setup_ag_iallocbt(
  38. struct xfs_scrub *sc,
  39. struct xfs_inode *ip)
  40. {
  41. return xchk_setup_ag_btree(sc, ip, sc->try_harder);
  42. }
  43. /* Inode btree scrubber. */
  44. /*
  45. * If we're checking the finobt, cross-reference with the inobt.
  46. * Otherwise we're checking the inobt; if there is an finobt, make sure
  47. * we have a record or not depending on freecount.
  48. */
  49. static inline void
  50. xchk_iallocbt_chunk_xref_other(
  51. struct xfs_scrub *sc,
  52. struct xfs_inobt_rec_incore *irec,
  53. xfs_agino_t agino)
  54. {
  55. struct xfs_btree_cur **pcur;
  56. bool has_irec;
  57. int error;
  58. if (sc->sm->sm_type == XFS_SCRUB_TYPE_FINOBT)
  59. pcur = &sc->sa.ino_cur;
  60. else
  61. pcur = &sc->sa.fino_cur;
  62. if (!(*pcur))
  63. return;
  64. error = xfs_ialloc_has_inode_record(*pcur, agino, agino, &has_irec);
  65. if (!xchk_should_check_xref(sc, &error, pcur))
  66. return;
  67. if (((irec->ir_freecount > 0 && !has_irec) ||
  68. (irec->ir_freecount == 0 && has_irec)))
  69. xchk_btree_xref_set_corrupt(sc, *pcur, 0);
  70. }
  71. /* Cross-reference with the other btrees. */
  72. STATIC void
  73. xchk_iallocbt_chunk_xref(
  74. struct xfs_scrub *sc,
  75. struct xfs_inobt_rec_incore *irec,
  76. xfs_agino_t agino,
  77. xfs_agblock_t agbno,
  78. xfs_extlen_t len)
  79. {
  80. struct xfs_owner_info oinfo;
  81. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  82. return;
  83. xchk_xref_is_used_space(sc, agbno, len);
  84. xchk_iallocbt_chunk_xref_other(sc, irec, agino);
  85. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INODES);
  86. xchk_xref_is_owned_by(sc, agbno, len, &oinfo);
  87. xchk_xref_is_not_shared(sc, agbno, len);
  88. }
  89. /* Is this chunk worth checking? */
  90. STATIC bool
  91. xchk_iallocbt_chunk(
  92. struct xchk_btree *bs,
  93. struct xfs_inobt_rec_incore *irec,
  94. xfs_agino_t agino,
  95. xfs_extlen_t len)
  96. {
  97. struct xfs_mount *mp = bs->cur->bc_mp;
  98. xfs_agnumber_t agno = bs->cur->bc_private.a.agno;
  99. xfs_agblock_t bno;
  100. bno = XFS_AGINO_TO_AGBNO(mp, agino);
  101. if (bno + len <= bno ||
  102. !xfs_verify_agbno(mp, agno, bno) ||
  103. !xfs_verify_agbno(mp, agno, bno + len - 1))
  104. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  105. xchk_iallocbt_chunk_xref(bs->sc, irec, agino, bno, len);
  106. return true;
  107. }
  108. /* Count the number of free inodes. */
  109. static unsigned int
  110. xchk_iallocbt_freecount(
  111. xfs_inofree_t freemask)
  112. {
  113. BUILD_BUG_ON(sizeof(freemask) != sizeof(__u64));
  114. return hweight64(freemask);
  115. }
  116. /* Check a particular inode with ir_free. */
  117. STATIC int
  118. xchk_iallocbt_check_cluster_freemask(
  119. struct xchk_btree *bs,
  120. xfs_ino_t fsino,
  121. xfs_agino_t chunkino,
  122. xfs_agino_t clusterino,
  123. struct xfs_inobt_rec_incore *irec,
  124. struct xfs_buf *bp)
  125. {
  126. struct xfs_dinode *dip;
  127. struct xfs_mount *mp = bs->cur->bc_mp;
  128. bool inode_is_free = false;
  129. bool freemask_ok;
  130. bool inuse;
  131. int error = 0;
  132. if (xchk_should_terminate(bs->sc, &error))
  133. return error;
  134. dip = xfs_buf_offset(bp, clusterino * mp->m_sb.sb_inodesize);
  135. if (be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC ||
  136. (dip->di_version >= 3 &&
  137. be64_to_cpu(dip->di_ino) != fsino + clusterino)) {
  138. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  139. goto out;
  140. }
  141. if (irec->ir_free & XFS_INOBT_MASK(chunkino + clusterino))
  142. inode_is_free = true;
  143. error = xfs_icache_inode_is_allocated(mp, bs->cur->bc_tp,
  144. fsino + clusterino, &inuse);
  145. if (error == -ENODATA) {
  146. /* Not cached, just read the disk buffer */
  147. freemask_ok = inode_is_free ^ !!(dip->di_mode);
  148. if (!bs->sc->try_harder && !freemask_ok)
  149. return -EDEADLOCK;
  150. } else if (error < 0) {
  151. /*
  152. * Inode is only half assembled, or there was an IO error,
  153. * or the verifier failed, so don't bother trying to check.
  154. * The inode scrubber can deal with this.
  155. */
  156. goto out;
  157. } else {
  158. /* Inode is all there. */
  159. freemask_ok = inode_is_free ^ inuse;
  160. }
  161. if (!freemask_ok)
  162. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  163. out:
  164. return 0;
  165. }
  166. /* Make sure the free mask is consistent with what the inodes think. */
  167. STATIC int
  168. xchk_iallocbt_check_freemask(
  169. struct xchk_btree *bs,
  170. struct xfs_inobt_rec_incore *irec)
  171. {
  172. struct xfs_owner_info oinfo;
  173. struct xfs_imap imap;
  174. struct xfs_mount *mp = bs->cur->bc_mp;
  175. struct xfs_dinode *dip;
  176. struct xfs_buf *bp;
  177. xfs_ino_t fsino;
  178. xfs_agino_t nr_inodes;
  179. xfs_agino_t agino;
  180. xfs_agino_t chunkino;
  181. xfs_agino_t clusterino;
  182. xfs_agblock_t agbno;
  183. int blks_per_cluster;
  184. uint16_t holemask;
  185. uint16_t ir_holemask;
  186. int error = 0;
  187. /* Make sure the freemask matches the inode records. */
  188. blks_per_cluster = xfs_icluster_size_fsb(mp);
  189. nr_inodes = XFS_OFFBNO_TO_AGINO(mp, blks_per_cluster, 0);
  190. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INODES);
  191. for (agino = irec->ir_startino;
  192. agino < irec->ir_startino + XFS_INODES_PER_CHUNK;
  193. agino += blks_per_cluster * mp->m_sb.sb_inopblock) {
  194. fsino = XFS_AGINO_TO_INO(mp, bs->cur->bc_private.a.agno, agino);
  195. chunkino = agino - irec->ir_startino;
  196. agbno = XFS_AGINO_TO_AGBNO(mp, agino);
  197. /* Compute the holemask mask for this cluster. */
  198. for (clusterino = 0, holemask = 0; clusterino < nr_inodes;
  199. clusterino += XFS_INODES_PER_HOLEMASK_BIT)
  200. holemask |= XFS_INOBT_MASK((chunkino + clusterino) /
  201. XFS_INODES_PER_HOLEMASK_BIT);
  202. /* The whole cluster must be a hole or not a hole. */
  203. ir_holemask = (irec->ir_holemask & holemask);
  204. if (ir_holemask != holemask && ir_holemask != 0) {
  205. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  206. continue;
  207. }
  208. /* If any part of this is a hole, skip it. */
  209. if (ir_holemask) {
  210. xchk_xref_is_not_owned_by(bs->sc, agbno,
  211. blks_per_cluster, &oinfo);
  212. continue;
  213. }
  214. xchk_xref_is_owned_by(bs->sc, agbno, blks_per_cluster,
  215. &oinfo);
  216. /* Grab the inode cluster buffer. */
  217. imap.im_blkno = XFS_AGB_TO_DADDR(mp, bs->cur->bc_private.a.agno,
  218. agbno);
  219. imap.im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
  220. imap.im_boffset = 0;
  221. error = xfs_imap_to_bp(mp, bs->cur->bc_tp, &imap,
  222. &dip, &bp, 0, 0);
  223. if (!xchk_btree_xref_process_error(bs->sc, bs->cur, 0,
  224. &error))
  225. continue;
  226. /* Which inodes are free? */
  227. for (clusterino = 0; clusterino < nr_inodes; clusterino++) {
  228. error = xchk_iallocbt_check_cluster_freemask(bs,
  229. fsino, chunkino, clusterino, irec, bp);
  230. if (error) {
  231. xfs_trans_brelse(bs->cur->bc_tp, bp);
  232. return error;
  233. }
  234. }
  235. xfs_trans_brelse(bs->cur->bc_tp, bp);
  236. }
  237. return error;
  238. }
  239. /* Scrub an inobt/finobt record. */
  240. STATIC int
  241. xchk_iallocbt_rec(
  242. struct xchk_btree *bs,
  243. union xfs_btree_rec *rec)
  244. {
  245. struct xfs_mount *mp = bs->cur->bc_mp;
  246. xfs_filblks_t *inode_blocks = bs->private;
  247. struct xfs_inobt_rec_incore irec;
  248. uint64_t holes;
  249. xfs_agnumber_t agno = bs->cur->bc_private.a.agno;
  250. xfs_agino_t agino;
  251. xfs_agblock_t agbno;
  252. xfs_extlen_t len;
  253. int holecount;
  254. int i;
  255. int error = 0;
  256. unsigned int real_freecount;
  257. uint16_t holemask;
  258. xfs_inobt_btrec_to_irec(mp, rec, &irec);
  259. if (irec.ir_count > XFS_INODES_PER_CHUNK ||
  260. irec.ir_freecount > XFS_INODES_PER_CHUNK)
  261. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  262. real_freecount = irec.ir_freecount +
  263. (XFS_INODES_PER_CHUNK - irec.ir_count);
  264. if (real_freecount != xchk_iallocbt_freecount(irec.ir_free))
  265. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  266. agino = irec.ir_startino;
  267. /* Record has to be properly aligned within the AG. */
  268. if (!xfs_verify_agino(mp, agno, agino) ||
  269. !xfs_verify_agino(mp, agno, agino + XFS_INODES_PER_CHUNK - 1)) {
  270. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  271. goto out;
  272. }
  273. /* Make sure this record is aligned to cluster and inoalignmnt size. */
  274. agbno = XFS_AGINO_TO_AGBNO(mp, irec.ir_startino);
  275. if ((agbno & (xfs_ialloc_cluster_alignment(mp) - 1)) ||
  276. (agbno & (xfs_icluster_size_fsb(mp) - 1)))
  277. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  278. *inode_blocks += XFS_B_TO_FSB(mp,
  279. irec.ir_count * mp->m_sb.sb_inodesize);
  280. /* Handle non-sparse inodes */
  281. if (!xfs_inobt_issparse(irec.ir_holemask)) {
  282. len = XFS_B_TO_FSB(mp,
  283. XFS_INODES_PER_CHUNK * mp->m_sb.sb_inodesize);
  284. if (irec.ir_count != XFS_INODES_PER_CHUNK)
  285. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  286. if (!xchk_iallocbt_chunk(bs, &irec, agino, len))
  287. goto out;
  288. goto check_freemask;
  289. }
  290. /* Check each chunk of a sparse inode cluster. */
  291. holemask = irec.ir_holemask;
  292. holecount = 0;
  293. len = XFS_B_TO_FSB(mp,
  294. XFS_INODES_PER_HOLEMASK_BIT * mp->m_sb.sb_inodesize);
  295. holes = ~xfs_inobt_irec_to_allocmask(&irec);
  296. if ((holes & irec.ir_free) != holes ||
  297. irec.ir_freecount > irec.ir_count)
  298. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  299. for (i = 0; i < XFS_INOBT_HOLEMASK_BITS; i++) {
  300. if (holemask & 1)
  301. holecount += XFS_INODES_PER_HOLEMASK_BIT;
  302. else if (!xchk_iallocbt_chunk(bs, &irec, agino, len))
  303. break;
  304. holemask >>= 1;
  305. agino += XFS_INODES_PER_HOLEMASK_BIT;
  306. }
  307. if (holecount > XFS_INODES_PER_CHUNK ||
  308. holecount + irec.ir_count != XFS_INODES_PER_CHUNK)
  309. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  310. check_freemask:
  311. error = xchk_iallocbt_check_freemask(bs, &irec);
  312. if (error)
  313. goto out;
  314. out:
  315. return error;
  316. }
  317. /*
  318. * Make sure the inode btrees are as large as the rmap thinks they are.
  319. * Don't bother if we're missing btree cursors, as we're already corrupt.
  320. */
  321. STATIC void
  322. xchk_iallocbt_xref_rmap_btreeblks(
  323. struct xfs_scrub *sc,
  324. int which)
  325. {
  326. struct xfs_owner_info oinfo;
  327. xfs_filblks_t blocks;
  328. xfs_extlen_t inobt_blocks = 0;
  329. xfs_extlen_t finobt_blocks = 0;
  330. int error;
  331. if (!sc->sa.ino_cur || !sc->sa.rmap_cur ||
  332. (xfs_sb_version_hasfinobt(&sc->mp->m_sb) && !sc->sa.fino_cur) ||
  333. xchk_skip_xref(sc->sm))
  334. return;
  335. /* Check that we saw as many inobt blocks as the rmap says. */
  336. error = xfs_btree_count_blocks(sc->sa.ino_cur, &inobt_blocks);
  337. if (!xchk_process_error(sc, 0, 0, &error))
  338. return;
  339. if (sc->sa.fino_cur) {
  340. error = xfs_btree_count_blocks(sc->sa.fino_cur, &finobt_blocks);
  341. if (!xchk_process_error(sc, 0, 0, &error))
  342. return;
  343. }
  344. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INOBT);
  345. error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur, &oinfo,
  346. &blocks);
  347. if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
  348. return;
  349. if (blocks != inobt_blocks + finobt_blocks)
  350. xchk_btree_set_corrupt(sc, sc->sa.ino_cur, 0);
  351. }
  352. /*
  353. * Make sure that the inobt records point to the same number of blocks as
  354. * the rmap says are owned by inodes.
  355. */
  356. STATIC void
  357. xchk_iallocbt_xref_rmap_inodes(
  358. struct xfs_scrub *sc,
  359. int which,
  360. xfs_filblks_t inode_blocks)
  361. {
  362. struct xfs_owner_info oinfo;
  363. xfs_filblks_t blocks;
  364. int error;
  365. if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
  366. return;
  367. /* Check that we saw as many inode blocks as the rmap knows about. */
  368. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INODES);
  369. error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur, &oinfo,
  370. &blocks);
  371. if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
  372. return;
  373. if (blocks != inode_blocks)
  374. xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
  375. }
  376. /* Scrub the inode btrees for some AG. */
  377. STATIC int
  378. xchk_iallocbt(
  379. struct xfs_scrub *sc,
  380. xfs_btnum_t which)
  381. {
  382. struct xfs_btree_cur *cur;
  383. struct xfs_owner_info oinfo;
  384. xfs_filblks_t inode_blocks = 0;
  385. int error;
  386. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INOBT);
  387. cur = which == XFS_BTNUM_INO ? sc->sa.ino_cur : sc->sa.fino_cur;
  388. error = xchk_btree(sc, cur, xchk_iallocbt_rec, &oinfo,
  389. &inode_blocks);
  390. if (error)
  391. return error;
  392. xchk_iallocbt_xref_rmap_btreeblks(sc, which);
  393. /*
  394. * If we're scrubbing the inode btree, inode_blocks is the number of
  395. * blocks pointed to by all the inode chunk records. Therefore, we
  396. * should compare to the number of inode chunk blocks that the rmap
  397. * knows about. We can't do this for the finobt since it only points
  398. * to inode chunks with free inodes.
  399. */
  400. if (which == XFS_BTNUM_INO)
  401. xchk_iallocbt_xref_rmap_inodes(sc, which, inode_blocks);
  402. return error;
  403. }
  404. int
  405. xchk_inobt(
  406. struct xfs_scrub *sc)
  407. {
  408. return xchk_iallocbt(sc, XFS_BTNUM_INO);
  409. }
  410. int
  411. xchk_finobt(
  412. struct xfs_scrub *sc)
  413. {
  414. return xchk_iallocbt(sc, XFS_BTNUM_FINO);
  415. }
  416. /* See if an inode btree has (or doesn't have) an inode chunk record. */
  417. static inline void
  418. xchk_xref_inode_check(
  419. struct xfs_scrub *sc,
  420. xfs_agblock_t agbno,
  421. xfs_extlen_t len,
  422. struct xfs_btree_cur **icur,
  423. bool should_have_inodes)
  424. {
  425. bool has_inodes;
  426. int error;
  427. if (!(*icur) || xchk_skip_xref(sc->sm))
  428. return;
  429. error = xfs_ialloc_has_inodes_at_extent(*icur, agbno, len, &has_inodes);
  430. if (!xchk_should_check_xref(sc, &error, icur))
  431. return;
  432. if (has_inodes != should_have_inodes)
  433. xchk_btree_xref_set_corrupt(sc, *icur, 0);
  434. }
  435. /* xref check that the extent is not covered by inodes */
  436. void
  437. xchk_xref_is_not_inode_chunk(
  438. struct xfs_scrub *sc,
  439. xfs_agblock_t agbno,
  440. xfs_extlen_t len)
  441. {
  442. xchk_xref_inode_check(sc, agbno, len, &sc->sa.ino_cur, false);
  443. xchk_xref_inode_check(sc, agbno, len, &sc->sa.fino_cur, false);
  444. }
  445. /* xref check that the extent is covered by inodes */
  446. void
  447. xchk_xref_is_inode_chunk(
  448. struct xfs_scrub *sc,
  449. xfs_agblock_t agbno,
  450. xfs_extlen_t len)
  451. {
  452. xchk_xref_inode_check(sc, agbno, len, &sc->sa.ino_cur, true);
  453. }