ialloc.c 14 KB

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