ialloc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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_btree.h"
  13. #include "xfs_log_format.h"
  14. #include "xfs_trans.h"
  15. #include "xfs_inode.h"
  16. #include "xfs_ialloc.h"
  17. #include "xfs_ialloc_btree.h"
  18. #include "xfs_icache.h"
  19. #include "xfs_rmap.h"
  20. #include "scrub/scrub.h"
  21. #include "scrub/common.h"
  22. #include "scrub/btree.h"
  23. #include "scrub/trace.h"
  24. #include "xfs_ag.h"
  25. /*
  26. * Set us up to scrub inode btrees.
  27. * If we detect a discrepancy between the inobt and the inode,
  28. * try again after forcing logged inode cores out to disk.
  29. */
  30. int
  31. xchk_setup_ag_iallocbt(
  32. struct xfs_scrub *sc)
  33. {
  34. if (xchk_need_intent_drain(sc))
  35. xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
  36. return xchk_setup_ag_btree(sc, sc->flags & XCHK_TRY_HARDER);
  37. }
  38. /* Inode btree scrubber. */
  39. struct xchk_iallocbt {
  40. /* Number of inodes we see while scanning inobt. */
  41. unsigned long long inodes;
  42. /* Expected next startino, for big block filesystems. */
  43. xfs_agino_t next_startino;
  44. /* Expected end of the current inode cluster. */
  45. xfs_agino_t next_cluster_ino;
  46. };
  47. /*
  48. * Does the finobt have a record for this inode with the same hole/free state?
  49. * This is a bit complicated because of the following:
  50. *
  51. * - The finobt need not have a record if all inodes in the inobt record are
  52. * allocated.
  53. * - The finobt need not have a record if all inodes in the inobt record are
  54. * free.
  55. * - The finobt need not have a record if the inobt record says this is a hole.
  56. * This likely doesn't happen in practice.
  57. */
  58. STATIC int
  59. xchk_inobt_xref_finobt(
  60. struct xfs_scrub *sc,
  61. struct xfs_inobt_rec_incore *irec,
  62. xfs_agino_t agino,
  63. bool free,
  64. bool hole)
  65. {
  66. struct xfs_inobt_rec_incore frec;
  67. struct xfs_btree_cur *cur = sc->sa.fino_cur;
  68. bool ffree, fhole;
  69. unsigned int frec_idx, fhole_idx;
  70. int has_record;
  71. int error;
  72. ASSERT(xfs_btree_is_fino(cur->bc_ops));
  73. error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &has_record);
  74. if (error)
  75. return error;
  76. if (!has_record)
  77. goto no_record;
  78. error = xfs_inobt_get_rec(cur, &frec, &has_record);
  79. if (!has_record)
  80. return -EFSCORRUPTED;
  81. if (frec.ir_startino + XFS_INODES_PER_CHUNK <= agino)
  82. goto no_record;
  83. /* There's a finobt record; free and hole status must match. */
  84. frec_idx = agino - frec.ir_startino;
  85. ffree = frec.ir_free & (1ULL << frec_idx);
  86. fhole_idx = frec_idx / XFS_INODES_PER_HOLEMASK_BIT;
  87. fhole = frec.ir_holemask & (1U << fhole_idx);
  88. if (ffree != free)
  89. xchk_btree_xref_set_corrupt(sc, cur, 0);
  90. if (fhole != hole)
  91. xchk_btree_xref_set_corrupt(sc, cur, 0);
  92. return 0;
  93. no_record:
  94. /* inobt record is fully allocated */
  95. if (irec->ir_free == 0)
  96. return 0;
  97. /* inobt record is totally unallocated */
  98. if (irec->ir_free == XFS_INOBT_ALL_FREE)
  99. return 0;
  100. /* inobt record says this is a hole */
  101. if (hole)
  102. return 0;
  103. /* finobt doesn't care about allocated inodes */
  104. if (!free)
  105. return 0;
  106. xchk_btree_xref_set_corrupt(sc, cur, 0);
  107. return 0;
  108. }
  109. /*
  110. * Make sure that each inode of this part of an inobt record has the same
  111. * sparse and free status as the finobt.
  112. */
  113. STATIC void
  114. xchk_inobt_chunk_xref_finobt(
  115. struct xfs_scrub *sc,
  116. struct xfs_inobt_rec_incore *irec,
  117. xfs_agino_t agino,
  118. unsigned int nr_inodes)
  119. {
  120. xfs_agino_t i;
  121. unsigned int rec_idx;
  122. int error;
  123. ASSERT(sc->sm->sm_type == XFS_SCRUB_TYPE_INOBT);
  124. if (!sc->sa.fino_cur || xchk_skip_xref(sc->sm))
  125. return;
  126. for (i = agino, rec_idx = agino - irec->ir_startino;
  127. i < agino + nr_inodes;
  128. i++, rec_idx++) {
  129. bool free, hole;
  130. unsigned int hole_idx;
  131. free = irec->ir_free & (1ULL << rec_idx);
  132. hole_idx = rec_idx / XFS_INODES_PER_HOLEMASK_BIT;
  133. hole = irec->ir_holemask & (1U << hole_idx);
  134. error = xchk_inobt_xref_finobt(sc, irec, i, free, hole);
  135. if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur))
  136. return;
  137. }
  138. }
  139. /*
  140. * Does the inobt have a record for this inode with the same hole/free state?
  141. * The inobt must always have a record if there's a finobt record.
  142. */
  143. STATIC int
  144. xchk_finobt_xref_inobt(
  145. struct xfs_scrub *sc,
  146. struct xfs_inobt_rec_incore *frec,
  147. xfs_agino_t agino,
  148. bool ffree,
  149. bool fhole)
  150. {
  151. struct xfs_inobt_rec_incore irec;
  152. struct xfs_btree_cur *cur = sc->sa.ino_cur;
  153. bool free, hole;
  154. unsigned int rec_idx, hole_idx;
  155. int has_record;
  156. int error;
  157. ASSERT(xfs_btree_is_ino(cur->bc_ops));
  158. error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &has_record);
  159. if (error)
  160. return error;
  161. if (!has_record)
  162. goto no_record;
  163. error = xfs_inobt_get_rec(cur, &irec, &has_record);
  164. if (!has_record)
  165. return -EFSCORRUPTED;
  166. if (irec.ir_startino + XFS_INODES_PER_CHUNK <= agino)
  167. goto no_record;
  168. /* There's an inobt record; free and hole status must match. */
  169. rec_idx = agino - irec.ir_startino;
  170. free = irec.ir_free & (1ULL << rec_idx);
  171. hole_idx = rec_idx / XFS_INODES_PER_HOLEMASK_BIT;
  172. hole = irec.ir_holemask & (1U << hole_idx);
  173. if (ffree != free)
  174. xchk_btree_xref_set_corrupt(sc, cur, 0);
  175. if (fhole != hole)
  176. xchk_btree_xref_set_corrupt(sc, cur, 0);
  177. return 0;
  178. no_record:
  179. /* finobt should never have a record for which the inobt does not */
  180. xchk_btree_xref_set_corrupt(sc, cur, 0);
  181. return 0;
  182. }
  183. /*
  184. * Make sure that each inode of this part of an finobt record has the same
  185. * sparse and free status as the inobt.
  186. */
  187. STATIC void
  188. xchk_finobt_chunk_xref_inobt(
  189. struct xfs_scrub *sc,
  190. struct xfs_inobt_rec_incore *frec,
  191. xfs_agino_t agino,
  192. unsigned int nr_inodes)
  193. {
  194. xfs_agino_t i;
  195. unsigned int rec_idx;
  196. int error;
  197. ASSERT(sc->sm->sm_type == XFS_SCRUB_TYPE_FINOBT);
  198. if (!sc->sa.ino_cur || xchk_skip_xref(sc->sm))
  199. return;
  200. for (i = agino, rec_idx = agino - frec->ir_startino;
  201. i < agino + nr_inodes;
  202. i++, rec_idx++) {
  203. bool ffree, fhole;
  204. unsigned int hole_idx;
  205. ffree = frec->ir_free & (1ULL << rec_idx);
  206. hole_idx = rec_idx / XFS_INODES_PER_HOLEMASK_BIT;
  207. fhole = frec->ir_holemask & (1U << hole_idx);
  208. error = xchk_finobt_xref_inobt(sc, frec, i, ffree, fhole);
  209. if (!xchk_should_check_xref(sc, &error, &sc->sa.ino_cur))
  210. return;
  211. }
  212. }
  213. /* Is this chunk worth checking and cross-referencing? */
  214. STATIC bool
  215. xchk_iallocbt_chunk(
  216. struct xchk_btree *bs,
  217. struct xfs_inobt_rec_incore *irec,
  218. xfs_agino_t agino,
  219. unsigned int nr_inodes)
  220. {
  221. struct xfs_scrub *sc = bs->sc;
  222. struct xfs_mount *mp = bs->cur->bc_mp;
  223. struct xfs_perag *pag = bs->cur->bc_ag.pag;
  224. xfs_agblock_t agbno;
  225. xfs_extlen_t len;
  226. agbno = XFS_AGINO_TO_AGBNO(mp, agino);
  227. len = XFS_B_TO_FSB(mp, nr_inodes * mp->m_sb.sb_inodesize);
  228. if (!xfs_verify_agbext(pag, agbno, len))
  229. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  230. if (bs->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  231. return false;
  232. xchk_xref_is_used_space(sc, agbno, len);
  233. if (sc->sm->sm_type == XFS_SCRUB_TYPE_INOBT)
  234. xchk_inobt_chunk_xref_finobt(sc, irec, agino, nr_inodes);
  235. else
  236. xchk_finobt_chunk_xref_inobt(sc, irec, agino, nr_inodes);
  237. xchk_xref_is_only_owned_by(sc, agbno, len, &XFS_RMAP_OINFO_INODES);
  238. xchk_xref_is_not_shared(sc, agbno, len);
  239. xchk_xref_is_not_cow_staging(sc, agbno, len);
  240. return true;
  241. }
  242. /*
  243. * Check that an inode's allocation status matches ir_free in the inobt
  244. * record. First we try querying the in-core inode state, and if the inode
  245. * isn't loaded we examine the on-disk inode directly.
  246. *
  247. * Since there can be 1:M and M:1 mappings between inobt records and inode
  248. * clusters, we pass in the inode location information as an inobt record;
  249. * the index of an inode cluster within the inobt record (as well as the
  250. * cluster buffer itself); and the index of the inode within the cluster.
  251. *
  252. * @irec is the inobt record.
  253. * @irec_ino is the inode offset from the start of the record.
  254. * @dip is the on-disk inode.
  255. */
  256. STATIC int
  257. xchk_iallocbt_check_cluster_ifree(
  258. struct xchk_btree *bs,
  259. struct xfs_inobt_rec_incore *irec,
  260. unsigned int irec_ino,
  261. struct xfs_dinode *dip)
  262. {
  263. struct xfs_mount *mp = bs->cur->bc_mp;
  264. xfs_ino_t fsino;
  265. xfs_agino_t agino;
  266. bool irec_free;
  267. bool ino_inuse;
  268. bool freemask_ok;
  269. int error = 0;
  270. if (xchk_should_terminate(bs->sc, &error))
  271. return error;
  272. /*
  273. * Given an inobt record and the offset of an inode from the start of
  274. * the record, compute which fs inode we're talking about.
  275. */
  276. agino = irec->ir_startino + irec_ino;
  277. fsino = XFS_AGINO_TO_INO(mp, bs->cur->bc_ag.pag->pag_agno, agino);
  278. irec_free = (irec->ir_free & XFS_INOBT_MASK(irec_ino));
  279. if (be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC ||
  280. (dip->di_version >= 3 && be64_to_cpu(dip->di_ino) != fsino)) {
  281. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  282. goto out;
  283. }
  284. error = xchk_inode_is_allocated(bs->sc, agino, &ino_inuse);
  285. if (error == -ENODATA) {
  286. /* Not cached, just read the disk buffer */
  287. freemask_ok = irec_free ^ !!(dip->di_mode);
  288. if (!(bs->sc->flags & XCHK_TRY_HARDER) && !freemask_ok)
  289. return -EDEADLOCK;
  290. } else if (error < 0) {
  291. /*
  292. * Inode is only half assembled, or there was an IO error,
  293. * or the verifier failed, so don't bother trying to check.
  294. * The inode scrubber can deal with this.
  295. */
  296. goto out;
  297. } else {
  298. /* Inode is all there. */
  299. freemask_ok = irec_free ^ ino_inuse;
  300. }
  301. if (!freemask_ok)
  302. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  303. out:
  304. return 0;
  305. }
  306. /*
  307. * Check that the holemask and freemask of a hypothetical inode cluster match
  308. * what's actually on disk. If sparse inodes are enabled, the cluster does
  309. * not actually have to map to inodes if the corresponding holemask bit is set.
  310. *
  311. * @cluster_base is the first inode in the cluster within the @irec.
  312. */
  313. STATIC int
  314. xchk_iallocbt_check_cluster(
  315. struct xchk_btree *bs,
  316. struct xfs_inobt_rec_incore *irec,
  317. unsigned int cluster_base)
  318. {
  319. struct xfs_imap imap;
  320. struct xfs_mount *mp = bs->cur->bc_mp;
  321. struct xfs_buf *cluster_bp;
  322. unsigned int nr_inodes;
  323. xfs_agnumber_t agno = bs->cur->bc_ag.pag->pag_agno;
  324. xfs_agblock_t agbno;
  325. unsigned int cluster_index;
  326. uint16_t cluster_mask = 0;
  327. uint16_t ir_holemask;
  328. int error = 0;
  329. nr_inodes = min_t(unsigned int, XFS_INODES_PER_CHUNK,
  330. M_IGEO(mp)->inodes_per_cluster);
  331. /* Map this inode cluster */
  332. agbno = XFS_AGINO_TO_AGBNO(mp, irec->ir_startino + cluster_base);
  333. /* Compute a bitmask for this cluster that can be used for holemask. */
  334. for (cluster_index = 0;
  335. cluster_index < nr_inodes;
  336. cluster_index += XFS_INODES_PER_HOLEMASK_BIT)
  337. cluster_mask |= XFS_INOBT_MASK((cluster_base + cluster_index) /
  338. XFS_INODES_PER_HOLEMASK_BIT);
  339. /*
  340. * Map the first inode of this cluster to a buffer and offset.
  341. * Be careful about inobt records that don't align with the start of
  342. * the inode buffer when block sizes are large enough to hold multiple
  343. * inode chunks. When this happens, cluster_base will be zero but
  344. * ir_startino can be large enough to make im_boffset nonzero.
  345. */
  346. ir_holemask = (irec->ir_holemask & cluster_mask);
  347. imap.im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
  348. imap.im_len = XFS_FSB_TO_BB(mp, M_IGEO(mp)->blocks_per_cluster);
  349. imap.im_boffset = XFS_INO_TO_OFFSET(mp, irec->ir_startino) <<
  350. mp->m_sb.sb_inodelog;
  351. if (imap.im_boffset != 0 && cluster_base != 0) {
  352. ASSERT(imap.im_boffset == 0 || cluster_base == 0);
  353. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  354. return 0;
  355. }
  356. trace_xchk_iallocbt_check_cluster(mp, agno, irec->ir_startino,
  357. imap.im_blkno, imap.im_len, cluster_base, nr_inodes,
  358. cluster_mask, ir_holemask,
  359. XFS_INO_TO_OFFSET(mp, irec->ir_startino +
  360. cluster_base));
  361. /* The whole cluster must be a hole or not a hole. */
  362. if (ir_holemask != cluster_mask && ir_holemask != 0) {
  363. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  364. return 0;
  365. }
  366. /* If any part of this is a hole, skip it. */
  367. if (ir_holemask) {
  368. xchk_xref_is_not_owned_by(bs->sc, agbno,
  369. M_IGEO(mp)->blocks_per_cluster,
  370. &XFS_RMAP_OINFO_INODES);
  371. return 0;
  372. }
  373. xchk_xref_is_only_owned_by(bs->sc, agbno, M_IGEO(mp)->blocks_per_cluster,
  374. &XFS_RMAP_OINFO_INODES);
  375. /* Grab the inode cluster buffer. */
  376. error = xfs_imap_to_bp(mp, bs->cur->bc_tp, &imap, &cluster_bp);
  377. if (!xchk_btree_xref_process_error(bs->sc, bs->cur, 0, &error))
  378. return error;
  379. /* Check free status of each inode within this cluster. */
  380. for (cluster_index = 0; cluster_index < nr_inodes; cluster_index++) {
  381. struct xfs_dinode *dip;
  382. if (imap.im_boffset >= BBTOB(cluster_bp->b_length)) {
  383. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  384. break;
  385. }
  386. dip = xfs_buf_offset(cluster_bp, imap.im_boffset);
  387. error = xchk_iallocbt_check_cluster_ifree(bs, irec,
  388. cluster_base + cluster_index, dip);
  389. if (error)
  390. break;
  391. imap.im_boffset += mp->m_sb.sb_inodesize;
  392. }
  393. xfs_trans_brelse(bs->cur->bc_tp, cluster_bp);
  394. return error;
  395. }
  396. /*
  397. * For all the inode clusters that could map to this inobt record, make sure
  398. * that the holemask makes sense and that the allocation status of each inode
  399. * matches the freemask.
  400. */
  401. STATIC int
  402. xchk_iallocbt_check_clusters(
  403. struct xchk_btree *bs,
  404. struct xfs_inobt_rec_incore *irec)
  405. {
  406. unsigned int cluster_base;
  407. int error = 0;
  408. /*
  409. * For the common case where this inobt record maps to multiple inode
  410. * clusters this will call _check_cluster for each cluster.
  411. *
  412. * For the case that multiple inobt records map to a single cluster,
  413. * this will call _check_cluster once.
  414. */
  415. for (cluster_base = 0;
  416. cluster_base < XFS_INODES_PER_CHUNK;
  417. cluster_base += M_IGEO(bs->sc->mp)->inodes_per_cluster) {
  418. error = xchk_iallocbt_check_cluster(bs, irec, cluster_base);
  419. if (error)
  420. break;
  421. }
  422. return error;
  423. }
  424. /*
  425. * Make sure this inode btree record is aligned properly. Because a fs block
  426. * contains multiple inodes, we check that the inobt record is aligned to the
  427. * correct inode, not just the correct block on disk. This results in a finer
  428. * grained corruption check.
  429. */
  430. STATIC void
  431. xchk_iallocbt_rec_alignment(
  432. struct xchk_btree *bs,
  433. struct xfs_inobt_rec_incore *irec)
  434. {
  435. struct xfs_mount *mp = bs->sc->mp;
  436. struct xchk_iallocbt *iabt = bs->private;
  437. struct xfs_ino_geometry *igeo = M_IGEO(mp);
  438. /*
  439. * finobt records have different positioning requirements than inobt
  440. * records: each finobt record must have a corresponding inobt record.
  441. * That is checked in the xref function, so for now we only catch the
  442. * obvious case where the record isn't at all aligned properly.
  443. *
  444. * Note that if a fs block contains more than a single chunk of inodes,
  445. * we will have finobt records only for those chunks containing free
  446. * inodes, and therefore expect chunk alignment of finobt records.
  447. * Otherwise, we expect that the finobt record is aligned to the
  448. * cluster alignment as told by the superblock.
  449. */
  450. if (xfs_btree_is_fino(bs->cur->bc_ops)) {
  451. unsigned int imask;
  452. imask = min_t(unsigned int, XFS_INODES_PER_CHUNK,
  453. igeo->cluster_align_inodes) - 1;
  454. if (irec->ir_startino & imask)
  455. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  456. return;
  457. }
  458. if (iabt->next_startino != NULLAGINO) {
  459. /*
  460. * We're midway through a cluster of inodes that is mapped by
  461. * multiple inobt records. Did we get the record for the next
  462. * irec in the sequence?
  463. */
  464. if (irec->ir_startino != iabt->next_startino) {
  465. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  466. return;
  467. }
  468. iabt->next_startino += XFS_INODES_PER_CHUNK;
  469. /* Are we done with the cluster? */
  470. if (iabt->next_startino >= iabt->next_cluster_ino) {
  471. iabt->next_startino = NULLAGINO;
  472. iabt->next_cluster_ino = NULLAGINO;
  473. }
  474. return;
  475. }
  476. /* inobt records must be aligned to cluster and inoalignmnt size. */
  477. if (irec->ir_startino & (igeo->cluster_align_inodes - 1)) {
  478. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  479. return;
  480. }
  481. if (irec->ir_startino & (igeo->inodes_per_cluster - 1)) {
  482. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  483. return;
  484. }
  485. if (igeo->inodes_per_cluster <= XFS_INODES_PER_CHUNK)
  486. return;
  487. /*
  488. * If this is the start of an inode cluster that can be mapped by
  489. * multiple inobt records, the next inobt record must follow exactly
  490. * after this one.
  491. */
  492. iabt->next_startino = irec->ir_startino + XFS_INODES_PER_CHUNK;
  493. iabt->next_cluster_ino = irec->ir_startino + igeo->inodes_per_cluster;
  494. }
  495. /* Scrub an inobt/finobt record. */
  496. STATIC int
  497. xchk_iallocbt_rec(
  498. struct xchk_btree *bs,
  499. const union xfs_btree_rec *rec)
  500. {
  501. struct xfs_mount *mp = bs->cur->bc_mp;
  502. struct xchk_iallocbt *iabt = bs->private;
  503. struct xfs_inobt_rec_incore irec;
  504. uint64_t holes;
  505. xfs_agino_t agino;
  506. int holecount;
  507. int i;
  508. int error = 0;
  509. uint16_t holemask;
  510. xfs_inobt_btrec_to_irec(mp, rec, &irec);
  511. if (xfs_inobt_check_irec(bs->cur->bc_ag.pag, &irec) != NULL) {
  512. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  513. return 0;
  514. }
  515. agino = irec.ir_startino;
  516. xchk_iallocbt_rec_alignment(bs, &irec);
  517. if (bs->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  518. goto out;
  519. iabt->inodes += irec.ir_count;
  520. /* Handle non-sparse inodes */
  521. if (!xfs_inobt_issparse(irec.ir_holemask)) {
  522. if (irec.ir_count != XFS_INODES_PER_CHUNK)
  523. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  524. if (!xchk_iallocbt_chunk(bs, &irec, agino,
  525. XFS_INODES_PER_CHUNK))
  526. goto out;
  527. goto check_clusters;
  528. }
  529. /* Check each chunk of a sparse inode cluster. */
  530. holemask = irec.ir_holemask;
  531. holecount = 0;
  532. holes = ~xfs_inobt_irec_to_allocmask(&irec);
  533. if ((holes & irec.ir_free) != holes ||
  534. irec.ir_freecount > irec.ir_count)
  535. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  536. for (i = 0; i < XFS_INOBT_HOLEMASK_BITS; i++) {
  537. if (holemask & 1)
  538. holecount += XFS_INODES_PER_HOLEMASK_BIT;
  539. else if (!xchk_iallocbt_chunk(bs, &irec, agino,
  540. XFS_INODES_PER_HOLEMASK_BIT))
  541. goto out;
  542. holemask >>= 1;
  543. agino += XFS_INODES_PER_HOLEMASK_BIT;
  544. }
  545. if (holecount > XFS_INODES_PER_CHUNK ||
  546. holecount + irec.ir_count != XFS_INODES_PER_CHUNK)
  547. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  548. check_clusters:
  549. if (bs->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  550. goto out;
  551. error = xchk_iallocbt_check_clusters(bs, &irec);
  552. if (error)
  553. goto out;
  554. out:
  555. return error;
  556. }
  557. /*
  558. * Make sure the inode btrees are as large as the rmap thinks they are.
  559. * Don't bother if we're missing btree cursors, as we're already corrupt.
  560. */
  561. STATIC void
  562. xchk_iallocbt_xref_rmap_btreeblks(
  563. struct xfs_scrub *sc)
  564. {
  565. xfs_filblks_t blocks;
  566. xfs_filblks_t inobt_blocks = 0;
  567. xfs_filblks_t finobt_blocks = 0;
  568. int error;
  569. if (!sc->sa.ino_cur || !sc->sa.rmap_cur ||
  570. (xfs_has_finobt(sc->mp) && !sc->sa.fino_cur) ||
  571. xchk_skip_xref(sc->sm))
  572. return;
  573. /* Check that we saw as many inobt blocks as the rmap says. */
  574. error = xfs_btree_count_blocks(sc->sa.ino_cur, &inobt_blocks);
  575. if (!xchk_process_error(sc, 0, 0, &error))
  576. return;
  577. if (sc->sa.fino_cur) {
  578. error = xfs_btree_count_blocks(sc->sa.fino_cur, &finobt_blocks);
  579. if (!xchk_process_error(sc, 0, 0, &error))
  580. return;
  581. }
  582. error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur,
  583. &XFS_RMAP_OINFO_INOBT, &blocks);
  584. if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
  585. return;
  586. if (blocks != inobt_blocks + finobt_blocks)
  587. xchk_btree_set_corrupt(sc, sc->sa.ino_cur, 0);
  588. }
  589. /*
  590. * Make sure that the inobt records point to the same number of blocks as
  591. * the rmap says are owned by inodes.
  592. */
  593. STATIC void
  594. xchk_iallocbt_xref_rmap_inodes(
  595. struct xfs_scrub *sc,
  596. unsigned long long inodes)
  597. {
  598. xfs_filblks_t blocks;
  599. xfs_filblks_t inode_blocks;
  600. int error;
  601. if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
  602. return;
  603. /* Check that we saw as many inode blocks as the rmap knows about. */
  604. error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur,
  605. &XFS_RMAP_OINFO_INODES, &blocks);
  606. if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
  607. return;
  608. inode_blocks = XFS_B_TO_FSB(sc->mp, inodes * sc->mp->m_sb.sb_inodesize);
  609. if (blocks != inode_blocks)
  610. xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
  611. }
  612. /* Scrub one of the inode btrees for some AG. */
  613. int
  614. xchk_iallocbt(
  615. struct xfs_scrub *sc)
  616. {
  617. struct xfs_btree_cur *cur;
  618. struct xchk_iallocbt iabt = {
  619. .inodes = 0,
  620. .next_startino = NULLAGINO,
  621. .next_cluster_ino = NULLAGINO,
  622. };
  623. int error;
  624. switch (sc->sm->sm_type) {
  625. case XFS_SCRUB_TYPE_INOBT:
  626. cur = sc->sa.ino_cur;
  627. break;
  628. case XFS_SCRUB_TYPE_FINOBT:
  629. cur = sc->sa.fino_cur;
  630. break;
  631. default:
  632. ASSERT(0);
  633. return -EIO;
  634. }
  635. error = xchk_btree(sc, cur, xchk_iallocbt_rec, &XFS_RMAP_OINFO_INOBT,
  636. &iabt);
  637. if (error)
  638. return error;
  639. xchk_iallocbt_xref_rmap_btreeblks(sc);
  640. /*
  641. * If we're scrubbing the inode btree, inode_blocks is the number of
  642. * blocks pointed to by all the inode chunk records. Therefore, we
  643. * should compare to the number of inode chunk blocks that the rmap
  644. * knows about. We can't do this for the finobt since it only points
  645. * to inode chunks with free inodes.
  646. */
  647. if (sc->sm->sm_type == XFS_SCRUB_TYPE_INOBT)
  648. xchk_iallocbt_xref_rmap_inodes(sc, iabt.inodes);
  649. return error;
  650. }
  651. /* See if an inode btree has (or doesn't have) an inode chunk record. */
  652. static inline void
  653. xchk_xref_inode_check(
  654. struct xfs_scrub *sc,
  655. xfs_agblock_t agbno,
  656. xfs_extlen_t len,
  657. struct xfs_btree_cur **icur,
  658. enum xbtree_recpacking expected)
  659. {
  660. enum xbtree_recpacking outcome;
  661. int error;
  662. if (!(*icur) || xchk_skip_xref(sc->sm))
  663. return;
  664. error = xfs_ialloc_has_inodes_at_extent(*icur, agbno, len, &outcome);
  665. if (!xchk_should_check_xref(sc, &error, icur))
  666. return;
  667. if (outcome != expected)
  668. xchk_btree_xref_set_corrupt(sc, *icur, 0);
  669. }
  670. /* xref check that the extent is not covered by inodes */
  671. void
  672. xchk_xref_is_not_inode_chunk(
  673. struct xfs_scrub *sc,
  674. xfs_agblock_t agbno,
  675. xfs_extlen_t len)
  676. {
  677. xchk_xref_inode_check(sc, agbno, len, &sc->sa.ino_cur,
  678. XBTREE_RECPACKING_EMPTY);
  679. xchk_xref_inode_check(sc, agbno, len, &sc->sa.fino_cur,
  680. XBTREE_RECPACKING_EMPTY);
  681. }
  682. /* xref check that the extent is covered by inodes */
  683. void
  684. xchk_xref_is_inode_chunk(
  685. struct xfs_scrub *sc,
  686. xfs_agblock_t agbno,
  687. xfs_extlen_t len)
  688. {
  689. xchk_xref_inode_check(sc, agbno, len, &sc->sa.ino_cur,
  690. XBTREE_RECPACKING_FULL);
  691. }