agheader.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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_rmap.h"
  22. #include "scrub/xfs_scrub.h"
  23. #include "scrub/scrub.h"
  24. #include "scrub/common.h"
  25. #include "scrub/trace.h"
  26. /* Superblock */
  27. /* Cross-reference with the other btrees. */
  28. STATIC void
  29. xchk_superblock_xref(
  30. struct xfs_scrub *sc,
  31. struct xfs_buf *bp)
  32. {
  33. struct xfs_owner_info oinfo;
  34. struct xfs_mount *mp = sc->mp;
  35. xfs_agnumber_t agno = sc->sm->sm_agno;
  36. xfs_agblock_t agbno;
  37. int error;
  38. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  39. return;
  40. agbno = XFS_SB_BLOCK(mp);
  41. error = xchk_ag_init(sc, agno, &sc->sa);
  42. if (!xchk_xref_process_error(sc, agno, agbno, &error))
  43. return;
  44. xchk_xref_is_used_space(sc, agbno, 1);
  45. xchk_xref_is_not_inode_chunk(sc, agbno, 1);
  46. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_FS);
  47. xchk_xref_is_owned_by(sc, agbno, 1, &oinfo);
  48. xchk_xref_is_not_shared(sc, agbno, 1);
  49. /* scrub teardown will take care of sc->sa for us */
  50. }
  51. /*
  52. * Scrub the filesystem superblock.
  53. *
  54. * Note: We do /not/ attempt to check AG 0's superblock. Mount is
  55. * responsible for validating all the geometry information in sb 0, so
  56. * if the filesystem is capable of initiating online scrub, then clearly
  57. * sb 0 is ok and we can use its information to check everything else.
  58. */
  59. int
  60. xchk_superblock(
  61. struct xfs_scrub *sc)
  62. {
  63. struct xfs_mount *mp = sc->mp;
  64. struct xfs_buf *bp;
  65. struct xfs_dsb *sb;
  66. xfs_agnumber_t agno;
  67. uint32_t v2_ok;
  68. __be32 features_mask;
  69. int error;
  70. __be16 vernum_mask;
  71. agno = sc->sm->sm_agno;
  72. if (agno == 0)
  73. return 0;
  74. error = xfs_sb_read_secondary(mp, sc->tp, agno, &bp);
  75. /*
  76. * The superblock verifier can return several different error codes
  77. * if it thinks the superblock doesn't look right. For a mount these
  78. * would all get bounced back to userspace, but if we're here then the
  79. * fs mounted successfully, which means that this secondary superblock
  80. * is simply incorrect. Treat all these codes the same way we treat
  81. * any corruption.
  82. */
  83. switch (error) {
  84. case -EINVAL: /* also -EWRONGFS */
  85. case -ENOSYS:
  86. case -EFBIG:
  87. error = -EFSCORRUPTED;
  88. default:
  89. break;
  90. }
  91. if (!xchk_process_error(sc, agno, XFS_SB_BLOCK(mp), &error))
  92. return error;
  93. sb = XFS_BUF_TO_SBP(bp);
  94. /*
  95. * Verify the geometries match. Fields that are permanently
  96. * set by mkfs are checked; fields that can be updated later
  97. * (and are not propagated to backup superblocks) are preen
  98. * checked.
  99. */
  100. if (sb->sb_blocksize != cpu_to_be32(mp->m_sb.sb_blocksize))
  101. xchk_block_set_corrupt(sc, bp);
  102. if (sb->sb_dblocks != cpu_to_be64(mp->m_sb.sb_dblocks))
  103. xchk_block_set_corrupt(sc, bp);
  104. if (sb->sb_rblocks != cpu_to_be64(mp->m_sb.sb_rblocks))
  105. xchk_block_set_corrupt(sc, bp);
  106. if (sb->sb_rextents != cpu_to_be64(mp->m_sb.sb_rextents))
  107. xchk_block_set_corrupt(sc, bp);
  108. if (!uuid_equal(&sb->sb_uuid, &mp->m_sb.sb_uuid))
  109. xchk_block_set_preen(sc, bp);
  110. if (sb->sb_logstart != cpu_to_be64(mp->m_sb.sb_logstart))
  111. xchk_block_set_corrupt(sc, bp);
  112. if (sb->sb_rootino != cpu_to_be64(mp->m_sb.sb_rootino))
  113. xchk_block_set_preen(sc, bp);
  114. if (sb->sb_rbmino != cpu_to_be64(mp->m_sb.sb_rbmino))
  115. xchk_block_set_preen(sc, bp);
  116. if (sb->sb_rsumino != cpu_to_be64(mp->m_sb.sb_rsumino))
  117. xchk_block_set_preen(sc, bp);
  118. if (sb->sb_rextsize != cpu_to_be32(mp->m_sb.sb_rextsize))
  119. xchk_block_set_corrupt(sc, bp);
  120. if (sb->sb_agblocks != cpu_to_be32(mp->m_sb.sb_agblocks))
  121. xchk_block_set_corrupt(sc, bp);
  122. if (sb->sb_agcount != cpu_to_be32(mp->m_sb.sb_agcount))
  123. xchk_block_set_corrupt(sc, bp);
  124. if (sb->sb_rbmblocks != cpu_to_be32(mp->m_sb.sb_rbmblocks))
  125. xchk_block_set_corrupt(sc, bp);
  126. if (sb->sb_logblocks != cpu_to_be32(mp->m_sb.sb_logblocks))
  127. xchk_block_set_corrupt(sc, bp);
  128. /* Check sb_versionnum bits that are set at mkfs time. */
  129. vernum_mask = cpu_to_be16(~XFS_SB_VERSION_OKBITS |
  130. XFS_SB_VERSION_NUMBITS |
  131. XFS_SB_VERSION_ALIGNBIT |
  132. XFS_SB_VERSION_DALIGNBIT |
  133. XFS_SB_VERSION_SHAREDBIT |
  134. XFS_SB_VERSION_LOGV2BIT |
  135. XFS_SB_VERSION_SECTORBIT |
  136. XFS_SB_VERSION_EXTFLGBIT |
  137. XFS_SB_VERSION_DIRV2BIT);
  138. if ((sb->sb_versionnum & vernum_mask) !=
  139. (cpu_to_be16(mp->m_sb.sb_versionnum) & vernum_mask))
  140. xchk_block_set_corrupt(sc, bp);
  141. /* Check sb_versionnum bits that can be set after mkfs time. */
  142. vernum_mask = cpu_to_be16(XFS_SB_VERSION_ATTRBIT |
  143. XFS_SB_VERSION_NLINKBIT |
  144. XFS_SB_VERSION_QUOTABIT);
  145. if ((sb->sb_versionnum & vernum_mask) !=
  146. (cpu_to_be16(mp->m_sb.sb_versionnum) & vernum_mask))
  147. xchk_block_set_preen(sc, bp);
  148. if (sb->sb_sectsize != cpu_to_be16(mp->m_sb.sb_sectsize))
  149. xchk_block_set_corrupt(sc, bp);
  150. if (sb->sb_inodesize != cpu_to_be16(mp->m_sb.sb_inodesize))
  151. xchk_block_set_corrupt(sc, bp);
  152. if (sb->sb_inopblock != cpu_to_be16(mp->m_sb.sb_inopblock))
  153. xchk_block_set_corrupt(sc, bp);
  154. if (memcmp(sb->sb_fname, mp->m_sb.sb_fname, sizeof(sb->sb_fname)))
  155. xchk_block_set_preen(sc, bp);
  156. if (sb->sb_blocklog != mp->m_sb.sb_blocklog)
  157. xchk_block_set_corrupt(sc, bp);
  158. if (sb->sb_sectlog != mp->m_sb.sb_sectlog)
  159. xchk_block_set_corrupt(sc, bp);
  160. if (sb->sb_inodelog != mp->m_sb.sb_inodelog)
  161. xchk_block_set_corrupt(sc, bp);
  162. if (sb->sb_inopblog != mp->m_sb.sb_inopblog)
  163. xchk_block_set_corrupt(sc, bp);
  164. if (sb->sb_agblklog != mp->m_sb.sb_agblklog)
  165. xchk_block_set_corrupt(sc, bp);
  166. if (sb->sb_rextslog != mp->m_sb.sb_rextslog)
  167. xchk_block_set_corrupt(sc, bp);
  168. if (sb->sb_imax_pct != mp->m_sb.sb_imax_pct)
  169. xchk_block_set_preen(sc, bp);
  170. /*
  171. * Skip the summary counters since we track them in memory anyway.
  172. * sb_icount, sb_ifree, sb_fdblocks, sb_frexents
  173. */
  174. if (sb->sb_uquotino != cpu_to_be64(mp->m_sb.sb_uquotino))
  175. xchk_block_set_preen(sc, bp);
  176. if (sb->sb_gquotino != cpu_to_be64(mp->m_sb.sb_gquotino))
  177. xchk_block_set_preen(sc, bp);
  178. /*
  179. * Skip the quota flags since repair will force quotacheck.
  180. * sb_qflags
  181. */
  182. if (sb->sb_flags != mp->m_sb.sb_flags)
  183. xchk_block_set_corrupt(sc, bp);
  184. if (sb->sb_shared_vn != mp->m_sb.sb_shared_vn)
  185. xchk_block_set_corrupt(sc, bp);
  186. if (sb->sb_inoalignmt != cpu_to_be32(mp->m_sb.sb_inoalignmt))
  187. xchk_block_set_corrupt(sc, bp);
  188. if (sb->sb_unit != cpu_to_be32(mp->m_sb.sb_unit))
  189. xchk_block_set_preen(sc, bp);
  190. if (sb->sb_width != cpu_to_be32(mp->m_sb.sb_width))
  191. xchk_block_set_preen(sc, bp);
  192. if (sb->sb_dirblklog != mp->m_sb.sb_dirblklog)
  193. xchk_block_set_corrupt(sc, bp);
  194. if (sb->sb_logsectlog != mp->m_sb.sb_logsectlog)
  195. xchk_block_set_corrupt(sc, bp);
  196. if (sb->sb_logsectsize != cpu_to_be16(mp->m_sb.sb_logsectsize))
  197. xchk_block_set_corrupt(sc, bp);
  198. if (sb->sb_logsunit != cpu_to_be32(mp->m_sb.sb_logsunit))
  199. xchk_block_set_corrupt(sc, bp);
  200. /* Do we see any invalid bits in sb_features2? */
  201. if (!xfs_sb_version_hasmorebits(&mp->m_sb)) {
  202. if (sb->sb_features2 != 0)
  203. xchk_block_set_corrupt(sc, bp);
  204. } else {
  205. v2_ok = XFS_SB_VERSION2_OKBITS;
  206. if (XFS_SB_VERSION_NUM(&mp->m_sb) >= XFS_SB_VERSION_5)
  207. v2_ok |= XFS_SB_VERSION2_CRCBIT;
  208. if (!!(sb->sb_features2 & cpu_to_be32(~v2_ok)))
  209. xchk_block_set_corrupt(sc, bp);
  210. if (sb->sb_features2 != sb->sb_bad_features2)
  211. xchk_block_set_preen(sc, bp);
  212. }
  213. /* Check sb_features2 flags that are set at mkfs time. */
  214. features_mask = cpu_to_be32(XFS_SB_VERSION2_LAZYSBCOUNTBIT |
  215. XFS_SB_VERSION2_PROJID32BIT |
  216. XFS_SB_VERSION2_CRCBIT |
  217. XFS_SB_VERSION2_FTYPE);
  218. if ((sb->sb_features2 & features_mask) !=
  219. (cpu_to_be32(mp->m_sb.sb_features2) & features_mask))
  220. xchk_block_set_corrupt(sc, bp);
  221. /* Check sb_features2 flags that can be set after mkfs time. */
  222. features_mask = cpu_to_be32(XFS_SB_VERSION2_ATTR2BIT);
  223. if ((sb->sb_features2 & features_mask) !=
  224. (cpu_to_be32(mp->m_sb.sb_features2) & features_mask))
  225. xchk_block_set_corrupt(sc, bp);
  226. if (!xfs_sb_version_hascrc(&mp->m_sb)) {
  227. /* all v5 fields must be zero */
  228. if (memchr_inv(&sb->sb_features_compat, 0,
  229. sizeof(struct xfs_dsb) -
  230. offsetof(struct xfs_dsb, sb_features_compat)))
  231. xchk_block_set_corrupt(sc, bp);
  232. } else {
  233. /* Check compat flags; all are set at mkfs time. */
  234. features_mask = cpu_to_be32(XFS_SB_FEAT_COMPAT_UNKNOWN);
  235. if ((sb->sb_features_compat & features_mask) !=
  236. (cpu_to_be32(mp->m_sb.sb_features_compat) & features_mask))
  237. xchk_block_set_corrupt(sc, bp);
  238. /* Check ro compat flags; all are set at mkfs time. */
  239. features_mask = cpu_to_be32(XFS_SB_FEAT_RO_COMPAT_UNKNOWN |
  240. XFS_SB_FEAT_RO_COMPAT_FINOBT |
  241. XFS_SB_FEAT_RO_COMPAT_RMAPBT |
  242. XFS_SB_FEAT_RO_COMPAT_REFLINK);
  243. if ((sb->sb_features_ro_compat & features_mask) !=
  244. (cpu_to_be32(mp->m_sb.sb_features_ro_compat) &
  245. features_mask))
  246. xchk_block_set_corrupt(sc, bp);
  247. /* Check incompat flags; all are set at mkfs time. */
  248. features_mask = cpu_to_be32(XFS_SB_FEAT_INCOMPAT_UNKNOWN |
  249. XFS_SB_FEAT_INCOMPAT_FTYPE |
  250. XFS_SB_FEAT_INCOMPAT_SPINODES |
  251. XFS_SB_FEAT_INCOMPAT_META_UUID);
  252. if ((sb->sb_features_incompat & features_mask) !=
  253. (cpu_to_be32(mp->m_sb.sb_features_incompat) &
  254. features_mask))
  255. xchk_block_set_corrupt(sc, bp);
  256. /* Check log incompat flags; all are set at mkfs time. */
  257. features_mask = cpu_to_be32(XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN);
  258. if ((sb->sb_features_log_incompat & features_mask) !=
  259. (cpu_to_be32(mp->m_sb.sb_features_log_incompat) &
  260. features_mask))
  261. xchk_block_set_corrupt(sc, bp);
  262. /* Don't care about sb_crc */
  263. if (sb->sb_spino_align != cpu_to_be32(mp->m_sb.sb_spino_align))
  264. xchk_block_set_corrupt(sc, bp);
  265. if (sb->sb_pquotino != cpu_to_be64(mp->m_sb.sb_pquotino))
  266. xchk_block_set_preen(sc, bp);
  267. /* Don't care about sb_lsn */
  268. }
  269. if (xfs_sb_version_hasmetauuid(&mp->m_sb)) {
  270. /* The metadata UUID must be the same for all supers */
  271. if (!uuid_equal(&sb->sb_meta_uuid, &mp->m_sb.sb_meta_uuid))
  272. xchk_block_set_corrupt(sc, bp);
  273. }
  274. /* Everything else must be zero. */
  275. if (memchr_inv(sb + 1, 0,
  276. BBTOB(bp->b_length) - sizeof(struct xfs_dsb)))
  277. xchk_block_set_corrupt(sc, bp);
  278. xchk_superblock_xref(sc, bp);
  279. return error;
  280. }
  281. /* AGF */
  282. /* Tally freespace record lengths. */
  283. STATIC int
  284. xchk_agf_record_bno_lengths(
  285. struct xfs_btree_cur *cur,
  286. struct xfs_alloc_rec_incore *rec,
  287. void *priv)
  288. {
  289. xfs_extlen_t *blocks = priv;
  290. (*blocks) += rec->ar_blockcount;
  291. return 0;
  292. }
  293. /* Check agf_freeblks */
  294. static inline void
  295. xchk_agf_xref_freeblks(
  296. struct xfs_scrub *sc)
  297. {
  298. struct xfs_agf *agf = XFS_BUF_TO_AGF(sc->sa.agf_bp);
  299. xfs_extlen_t blocks = 0;
  300. int error;
  301. if (!sc->sa.bno_cur)
  302. return;
  303. error = xfs_alloc_query_all(sc->sa.bno_cur,
  304. xchk_agf_record_bno_lengths, &blocks);
  305. if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
  306. return;
  307. if (blocks != be32_to_cpu(agf->agf_freeblks))
  308. xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
  309. }
  310. /* Cross reference the AGF with the cntbt (freespace by length btree) */
  311. static inline void
  312. xchk_agf_xref_cntbt(
  313. struct xfs_scrub *sc)
  314. {
  315. struct xfs_agf *agf = XFS_BUF_TO_AGF(sc->sa.agf_bp);
  316. xfs_agblock_t agbno;
  317. xfs_extlen_t blocks;
  318. int have;
  319. int error;
  320. if (!sc->sa.cnt_cur)
  321. return;
  322. /* Any freespace at all? */
  323. error = xfs_alloc_lookup_le(sc->sa.cnt_cur, 0, -1U, &have);
  324. if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
  325. return;
  326. if (!have) {
  327. if (agf->agf_freeblks != be32_to_cpu(0))
  328. xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
  329. return;
  330. }
  331. /* Check agf_longest */
  332. error = xfs_alloc_get_rec(sc->sa.cnt_cur, &agbno, &blocks, &have);
  333. if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
  334. return;
  335. if (!have || blocks != be32_to_cpu(agf->agf_longest))
  336. xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
  337. }
  338. /* Check the btree block counts in the AGF against the btrees. */
  339. STATIC void
  340. xchk_agf_xref_btreeblks(
  341. struct xfs_scrub *sc)
  342. {
  343. struct xfs_agf *agf = XFS_BUF_TO_AGF(sc->sa.agf_bp);
  344. struct xfs_mount *mp = sc->mp;
  345. xfs_agblock_t blocks;
  346. xfs_agblock_t btreeblks;
  347. int error;
  348. /* Check agf_rmap_blocks; set up for agf_btreeblks check */
  349. if (sc->sa.rmap_cur) {
  350. error = xfs_btree_count_blocks(sc->sa.rmap_cur, &blocks);
  351. if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
  352. return;
  353. btreeblks = blocks - 1;
  354. if (blocks != be32_to_cpu(agf->agf_rmap_blocks))
  355. xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
  356. } else {
  357. btreeblks = 0;
  358. }
  359. /*
  360. * No rmap cursor; we can't xref if we have the rmapbt feature.
  361. * We also can't do it if we're missing the free space btree cursors.
  362. */
  363. if ((xfs_sb_version_hasrmapbt(&mp->m_sb) && !sc->sa.rmap_cur) ||
  364. !sc->sa.bno_cur || !sc->sa.cnt_cur)
  365. return;
  366. /* Check agf_btreeblks */
  367. error = xfs_btree_count_blocks(sc->sa.bno_cur, &blocks);
  368. if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
  369. return;
  370. btreeblks += blocks - 1;
  371. error = xfs_btree_count_blocks(sc->sa.cnt_cur, &blocks);
  372. if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
  373. return;
  374. btreeblks += blocks - 1;
  375. if (btreeblks != be32_to_cpu(agf->agf_btreeblks))
  376. xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
  377. }
  378. /* Check agf_refcount_blocks against tree size */
  379. static inline void
  380. xchk_agf_xref_refcblks(
  381. struct xfs_scrub *sc)
  382. {
  383. struct xfs_agf *agf = XFS_BUF_TO_AGF(sc->sa.agf_bp);
  384. xfs_agblock_t blocks;
  385. int error;
  386. if (!sc->sa.refc_cur)
  387. return;
  388. error = xfs_btree_count_blocks(sc->sa.refc_cur, &blocks);
  389. if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
  390. return;
  391. if (blocks != be32_to_cpu(agf->agf_refcount_blocks))
  392. xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
  393. }
  394. /* Cross-reference with the other btrees. */
  395. STATIC void
  396. xchk_agf_xref(
  397. struct xfs_scrub *sc)
  398. {
  399. struct xfs_owner_info oinfo;
  400. struct xfs_mount *mp = sc->mp;
  401. xfs_agblock_t agbno;
  402. int error;
  403. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  404. return;
  405. agbno = XFS_AGF_BLOCK(mp);
  406. error = xchk_ag_btcur_init(sc, &sc->sa);
  407. if (error)
  408. return;
  409. xchk_xref_is_used_space(sc, agbno, 1);
  410. xchk_agf_xref_freeblks(sc);
  411. xchk_agf_xref_cntbt(sc);
  412. xchk_xref_is_not_inode_chunk(sc, agbno, 1);
  413. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_FS);
  414. xchk_xref_is_owned_by(sc, agbno, 1, &oinfo);
  415. xchk_agf_xref_btreeblks(sc);
  416. xchk_xref_is_not_shared(sc, agbno, 1);
  417. xchk_agf_xref_refcblks(sc);
  418. /* scrub teardown will take care of sc->sa for us */
  419. }
  420. /* Scrub the AGF. */
  421. int
  422. xchk_agf(
  423. struct xfs_scrub *sc)
  424. {
  425. struct xfs_mount *mp = sc->mp;
  426. struct xfs_agf *agf;
  427. xfs_agnumber_t agno;
  428. xfs_agblock_t agbno;
  429. xfs_agblock_t eoag;
  430. xfs_agblock_t agfl_first;
  431. xfs_agblock_t agfl_last;
  432. xfs_agblock_t agfl_count;
  433. xfs_agblock_t fl_count;
  434. int level;
  435. int error = 0;
  436. agno = sc->sa.agno = sc->sm->sm_agno;
  437. error = xchk_ag_read_headers(sc, agno, &sc->sa.agi_bp,
  438. &sc->sa.agf_bp, &sc->sa.agfl_bp);
  439. if (!xchk_process_error(sc, agno, XFS_AGF_BLOCK(sc->mp), &error))
  440. goto out;
  441. xchk_buffer_recheck(sc, sc->sa.agf_bp);
  442. agf = XFS_BUF_TO_AGF(sc->sa.agf_bp);
  443. /* Check the AG length */
  444. eoag = be32_to_cpu(agf->agf_length);
  445. if (eoag != xfs_ag_block_count(mp, agno))
  446. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  447. /* Check the AGF btree roots and levels */
  448. agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_BNO]);
  449. if (!xfs_verify_agbno(mp, agno, agbno))
  450. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  451. agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_CNT]);
  452. if (!xfs_verify_agbno(mp, agno, agbno))
  453. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  454. level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]);
  455. if (level <= 0 || level > XFS_BTREE_MAXLEVELS)
  456. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  457. level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]);
  458. if (level <= 0 || level > XFS_BTREE_MAXLEVELS)
  459. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  460. if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
  461. agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_RMAP]);
  462. if (!xfs_verify_agbno(mp, agno, agbno))
  463. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  464. level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]);
  465. if (level <= 0 || level > XFS_BTREE_MAXLEVELS)
  466. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  467. }
  468. if (xfs_sb_version_hasreflink(&mp->m_sb)) {
  469. agbno = be32_to_cpu(agf->agf_refcount_root);
  470. if (!xfs_verify_agbno(mp, agno, agbno))
  471. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  472. level = be32_to_cpu(agf->agf_refcount_level);
  473. if (level <= 0 || level > XFS_BTREE_MAXLEVELS)
  474. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  475. }
  476. /* Check the AGFL counters */
  477. agfl_first = be32_to_cpu(agf->agf_flfirst);
  478. agfl_last = be32_to_cpu(agf->agf_fllast);
  479. agfl_count = be32_to_cpu(agf->agf_flcount);
  480. if (agfl_last > agfl_first)
  481. fl_count = agfl_last - agfl_first + 1;
  482. else
  483. fl_count = xfs_agfl_size(mp) - agfl_first + agfl_last + 1;
  484. if (agfl_count != 0 && fl_count != agfl_count)
  485. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  486. xchk_agf_xref(sc);
  487. out:
  488. return error;
  489. }
  490. /* AGFL */
  491. struct xchk_agfl_info {
  492. struct xfs_owner_info oinfo;
  493. unsigned int sz_entries;
  494. unsigned int nr_entries;
  495. xfs_agblock_t *entries;
  496. struct xfs_scrub *sc;
  497. };
  498. /* Cross-reference with the other btrees. */
  499. STATIC void
  500. xchk_agfl_block_xref(
  501. struct xfs_scrub *sc,
  502. xfs_agblock_t agbno,
  503. struct xfs_owner_info *oinfo)
  504. {
  505. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  506. return;
  507. xchk_xref_is_used_space(sc, agbno, 1);
  508. xchk_xref_is_not_inode_chunk(sc, agbno, 1);
  509. xchk_xref_is_owned_by(sc, agbno, 1, oinfo);
  510. xchk_xref_is_not_shared(sc, agbno, 1);
  511. }
  512. /* Scrub an AGFL block. */
  513. STATIC int
  514. xchk_agfl_block(
  515. struct xfs_mount *mp,
  516. xfs_agblock_t agbno,
  517. void *priv)
  518. {
  519. struct xchk_agfl_info *sai = priv;
  520. struct xfs_scrub *sc = sai->sc;
  521. xfs_agnumber_t agno = sc->sa.agno;
  522. if (xfs_verify_agbno(mp, agno, agbno) &&
  523. sai->nr_entries < sai->sz_entries)
  524. sai->entries[sai->nr_entries++] = agbno;
  525. else
  526. xchk_block_set_corrupt(sc, sc->sa.agfl_bp);
  527. xchk_agfl_block_xref(sc, agbno, priv);
  528. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  529. return XFS_BTREE_QUERY_RANGE_ABORT;
  530. return 0;
  531. }
  532. static int
  533. xchk_agblock_cmp(
  534. const void *pa,
  535. const void *pb)
  536. {
  537. const xfs_agblock_t *a = pa;
  538. const xfs_agblock_t *b = pb;
  539. return (int)*a - (int)*b;
  540. }
  541. /* Cross-reference with the other btrees. */
  542. STATIC void
  543. xchk_agfl_xref(
  544. struct xfs_scrub *sc)
  545. {
  546. struct xfs_owner_info oinfo;
  547. struct xfs_mount *mp = sc->mp;
  548. xfs_agblock_t agbno;
  549. int error;
  550. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  551. return;
  552. agbno = XFS_AGFL_BLOCK(mp);
  553. error = xchk_ag_btcur_init(sc, &sc->sa);
  554. if (error)
  555. return;
  556. xchk_xref_is_used_space(sc, agbno, 1);
  557. xchk_xref_is_not_inode_chunk(sc, agbno, 1);
  558. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_FS);
  559. xchk_xref_is_owned_by(sc, agbno, 1, &oinfo);
  560. xchk_xref_is_not_shared(sc, agbno, 1);
  561. /*
  562. * Scrub teardown will take care of sc->sa for us. Leave sc->sa
  563. * active so that the agfl block xref can use it too.
  564. */
  565. }
  566. /* Scrub the AGFL. */
  567. int
  568. xchk_agfl(
  569. struct xfs_scrub *sc)
  570. {
  571. struct xchk_agfl_info sai;
  572. struct xfs_agf *agf;
  573. xfs_agnumber_t agno;
  574. unsigned int agflcount;
  575. unsigned int i;
  576. int error;
  577. agno = sc->sa.agno = sc->sm->sm_agno;
  578. error = xchk_ag_read_headers(sc, agno, &sc->sa.agi_bp,
  579. &sc->sa.agf_bp, &sc->sa.agfl_bp);
  580. if (!xchk_process_error(sc, agno, XFS_AGFL_BLOCK(sc->mp), &error))
  581. goto out;
  582. if (!sc->sa.agf_bp)
  583. return -EFSCORRUPTED;
  584. xchk_buffer_recheck(sc, sc->sa.agfl_bp);
  585. xchk_agfl_xref(sc);
  586. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  587. goto out;
  588. /* Allocate buffer to ensure uniqueness of AGFL entries. */
  589. agf = XFS_BUF_TO_AGF(sc->sa.agf_bp);
  590. agflcount = be32_to_cpu(agf->agf_flcount);
  591. if (agflcount > xfs_agfl_size(sc->mp)) {
  592. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  593. goto out;
  594. }
  595. memset(&sai, 0, sizeof(sai));
  596. sai.sc = sc;
  597. sai.sz_entries = agflcount;
  598. sai.entries = kmem_zalloc(sizeof(xfs_agblock_t) * agflcount,
  599. KM_MAYFAIL);
  600. if (!sai.entries) {
  601. error = -ENOMEM;
  602. goto out;
  603. }
  604. /* Check the blocks in the AGFL. */
  605. xfs_rmap_ag_owner(&sai.oinfo, XFS_RMAP_OWN_AG);
  606. error = xfs_agfl_walk(sc->mp, XFS_BUF_TO_AGF(sc->sa.agf_bp),
  607. sc->sa.agfl_bp, xchk_agfl_block, &sai);
  608. if (error == XFS_BTREE_QUERY_RANGE_ABORT) {
  609. error = 0;
  610. goto out_free;
  611. }
  612. if (error)
  613. goto out_free;
  614. if (agflcount != sai.nr_entries) {
  615. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  616. goto out_free;
  617. }
  618. /* Sort entries, check for duplicates. */
  619. sort(sai.entries, sai.nr_entries, sizeof(sai.entries[0]),
  620. xchk_agblock_cmp, NULL);
  621. for (i = 1; i < sai.nr_entries; i++) {
  622. if (sai.entries[i] == sai.entries[i - 1]) {
  623. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  624. break;
  625. }
  626. }
  627. out_free:
  628. kmem_free(sai.entries);
  629. out:
  630. return error;
  631. }
  632. /* AGI */
  633. /* Check agi_count/agi_freecount */
  634. static inline void
  635. xchk_agi_xref_icounts(
  636. struct xfs_scrub *sc)
  637. {
  638. struct xfs_agi *agi = XFS_BUF_TO_AGI(sc->sa.agi_bp);
  639. xfs_agino_t icount;
  640. xfs_agino_t freecount;
  641. int error;
  642. if (!sc->sa.ino_cur)
  643. return;
  644. error = xfs_ialloc_count_inodes(sc->sa.ino_cur, &icount, &freecount);
  645. if (!xchk_should_check_xref(sc, &error, &sc->sa.ino_cur))
  646. return;
  647. if (be32_to_cpu(agi->agi_count) != icount ||
  648. be32_to_cpu(agi->agi_freecount) != freecount)
  649. xchk_block_xref_set_corrupt(sc, sc->sa.agi_bp);
  650. }
  651. /* Cross-reference with the other btrees. */
  652. STATIC void
  653. xchk_agi_xref(
  654. struct xfs_scrub *sc)
  655. {
  656. struct xfs_owner_info oinfo;
  657. struct xfs_mount *mp = sc->mp;
  658. xfs_agblock_t agbno;
  659. int error;
  660. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  661. return;
  662. agbno = XFS_AGI_BLOCK(mp);
  663. error = xchk_ag_btcur_init(sc, &sc->sa);
  664. if (error)
  665. return;
  666. xchk_xref_is_used_space(sc, agbno, 1);
  667. xchk_xref_is_not_inode_chunk(sc, agbno, 1);
  668. xchk_agi_xref_icounts(sc);
  669. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_FS);
  670. xchk_xref_is_owned_by(sc, agbno, 1, &oinfo);
  671. xchk_xref_is_not_shared(sc, agbno, 1);
  672. /* scrub teardown will take care of sc->sa for us */
  673. }
  674. /* Scrub the AGI. */
  675. int
  676. xchk_agi(
  677. struct xfs_scrub *sc)
  678. {
  679. struct xfs_mount *mp = sc->mp;
  680. struct xfs_agi *agi;
  681. xfs_agnumber_t agno;
  682. xfs_agblock_t agbno;
  683. xfs_agblock_t eoag;
  684. xfs_agino_t agino;
  685. xfs_agino_t first_agino;
  686. xfs_agino_t last_agino;
  687. xfs_agino_t icount;
  688. int i;
  689. int level;
  690. int error = 0;
  691. agno = sc->sa.agno = sc->sm->sm_agno;
  692. error = xchk_ag_read_headers(sc, agno, &sc->sa.agi_bp,
  693. &sc->sa.agf_bp, &sc->sa.agfl_bp);
  694. if (!xchk_process_error(sc, agno, XFS_AGI_BLOCK(sc->mp), &error))
  695. goto out;
  696. xchk_buffer_recheck(sc, sc->sa.agi_bp);
  697. agi = XFS_BUF_TO_AGI(sc->sa.agi_bp);
  698. /* Check the AG length */
  699. eoag = be32_to_cpu(agi->agi_length);
  700. if (eoag != xfs_ag_block_count(mp, agno))
  701. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  702. /* Check btree roots and levels */
  703. agbno = be32_to_cpu(agi->agi_root);
  704. if (!xfs_verify_agbno(mp, agno, agbno))
  705. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  706. level = be32_to_cpu(agi->agi_level);
  707. if (level <= 0 || level > XFS_BTREE_MAXLEVELS)
  708. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  709. if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
  710. agbno = be32_to_cpu(agi->agi_free_root);
  711. if (!xfs_verify_agbno(mp, agno, agbno))
  712. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  713. level = be32_to_cpu(agi->agi_free_level);
  714. if (level <= 0 || level > XFS_BTREE_MAXLEVELS)
  715. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  716. }
  717. /* Check inode counters */
  718. xfs_agino_range(mp, agno, &first_agino, &last_agino);
  719. icount = be32_to_cpu(agi->agi_count);
  720. if (icount > last_agino - first_agino + 1 ||
  721. icount < be32_to_cpu(agi->agi_freecount))
  722. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  723. /* Check inode pointers */
  724. agino = be32_to_cpu(agi->agi_newino);
  725. if (agino != NULLAGINO && !xfs_verify_agino(mp, agno, agino))
  726. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  727. agino = be32_to_cpu(agi->agi_dirino);
  728. if (agino != NULLAGINO && !xfs_verify_agino(mp, agno, agino))
  729. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  730. /* Check unlinked inode buckets */
  731. for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
  732. agino = be32_to_cpu(agi->agi_unlinked[i]);
  733. if (agino == NULLAGINO)
  734. continue;
  735. if (!xfs_verify_agino(mp, agno, agino))
  736. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  737. }
  738. if (agi->agi_pad32 != cpu_to_be32(0))
  739. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  740. xchk_agi_xref(sc);
  741. out:
  742. return error;
  743. }