agheader.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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_sb.h"
  14. #include "xfs_alloc.h"
  15. #include "xfs_ialloc.h"
  16. #include "xfs_rmap.h"
  17. #include "xfs_ag.h"
  18. #include "xfs_inode.h"
  19. #include "scrub/scrub.h"
  20. #include "scrub/common.h"
  21. int
  22. xchk_setup_agheader(
  23. struct xfs_scrub *sc)
  24. {
  25. if (xchk_need_intent_drain(sc))
  26. xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
  27. return xchk_setup_fs(sc);
  28. }
  29. /* Superblock */
  30. /* Cross-reference with the other btrees. */
  31. STATIC void
  32. xchk_superblock_xref(
  33. struct xfs_scrub *sc,
  34. struct xfs_buf *bp)
  35. {
  36. struct xfs_mount *mp = sc->mp;
  37. xfs_agnumber_t agno = sc->sm->sm_agno;
  38. xfs_agblock_t agbno;
  39. int error;
  40. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  41. return;
  42. agbno = XFS_SB_BLOCK(mp);
  43. error = xchk_ag_init_existing(sc, agno, &sc->sa);
  44. if (!xchk_xref_process_error(sc, agno, agbno, &error))
  45. return;
  46. xchk_xref_is_used_space(sc, agbno, 1);
  47. xchk_xref_is_not_inode_chunk(sc, agbno, 1);
  48. xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
  49. xchk_xref_is_not_shared(sc, agbno, 1);
  50. xchk_xref_is_not_cow_staging(sc, agbno, 1);
  51. /* scrub teardown will take care of sc->sa for us */
  52. }
  53. /*
  54. * Calculate the ondisk superblock size in bytes given the feature set of the
  55. * mounted filesystem (aka the primary sb). This is subtlely different from
  56. * the logic in xfs_repair, which computes the size of a secondary sb given the
  57. * featureset listed in the secondary sb.
  58. */
  59. STATIC size_t
  60. xchk_superblock_ondisk_size(
  61. struct xfs_mount *mp)
  62. {
  63. if (xfs_has_metauuid(mp))
  64. return offsetofend(struct xfs_dsb, sb_meta_uuid);
  65. if (xfs_has_crc(mp))
  66. return offsetofend(struct xfs_dsb, sb_lsn);
  67. if (xfs_sb_version_hasmorebits(&mp->m_sb))
  68. return offsetofend(struct xfs_dsb, sb_bad_features2);
  69. if (xfs_has_logv2(mp))
  70. return offsetofend(struct xfs_dsb, sb_logsunit);
  71. if (xfs_has_sector(mp))
  72. return offsetofend(struct xfs_dsb, sb_logsectsize);
  73. /* only support dirv2 or more recent */
  74. return offsetofend(struct xfs_dsb, sb_dirblklog);
  75. }
  76. /*
  77. * Scrub the filesystem superblock.
  78. *
  79. * Note: We do /not/ attempt to check AG 0's superblock. Mount is
  80. * responsible for validating all the geometry information in sb 0, so
  81. * if the filesystem is capable of initiating online scrub, then clearly
  82. * sb 0 is ok and we can use its information to check everything else.
  83. */
  84. int
  85. xchk_superblock(
  86. struct xfs_scrub *sc)
  87. {
  88. struct xfs_mount *mp = sc->mp;
  89. struct xfs_buf *bp;
  90. struct xfs_dsb *sb;
  91. struct xfs_perag *pag;
  92. size_t sblen;
  93. xfs_agnumber_t agno;
  94. uint32_t v2_ok;
  95. __be32 features_mask;
  96. int error;
  97. __be16 vernum_mask;
  98. agno = sc->sm->sm_agno;
  99. if (agno == 0)
  100. return 0;
  101. /*
  102. * Grab an active reference to the perag structure. If we can't get
  103. * it, we're racing with something that's tearing down the AG, so
  104. * signal that the AG no longer exists.
  105. */
  106. pag = xfs_perag_get(mp, agno);
  107. if (!pag)
  108. return -ENOENT;
  109. error = xfs_sb_read_secondary(mp, sc->tp, agno, &bp);
  110. /*
  111. * The superblock verifier can return several different error codes
  112. * if it thinks the superblock doesn't look right. For a mount these
  113. * would all get bounced back to userspace, but if we're here then the
  114. * fs mounted successfully, which means that this secondary superblock
  115. * is simply incorrect. Treat all these codes the same way we treat
  116. * any corruption.
  117. */
  118. switch (error) {
  119. case -EINVAL: /* also -EWRONGFS */
  120. case -ENOSYS:
  121. case -EFBIG:
  122. error = -EFSCORRUPTED;
  123. fallthrough;
  124. default:
  125. break;
  126. }
  127. if (!xchk_process_error(sc, agno, XFS_SB_BLOCK(mp), &error))
  128. goto out_pag;
  129. sb = bp->b_addr;
  130. /*
  131. * Verify the geometries match. Fields that are permanently
  132. * set by mkfs are checked; fields that can be updated later
  133. * (and are not propagated to backup superblocks) are preen
  134. * checked.
  135. */
  136. if (sb->sb_blocksize != cpu_to_be32(mp->m_sb.sb_blocksize))
  137. xchk_block_set_corrupt(sc, bp);
  138. if (sb->sb_dblocks != cpu_to_be64(mp->m_sb.sb_dblocks))
  139. xchk_block_set_corrupt(sc, bp);
  140. if (sb->sb_rblocks != cpu_to_be64(mp->m_sb.sb_rblocks))
  141. xchk_block_set_corrupt(sc, bp);
  142. if (sb->sb_rextents != cpu_to_be64(mp->m_sb.sb_rextents))
  143. xchk_block_set_corrupt(sc, bp);
  144. if (!uuid_equal(&sb->sb_uuid, &mp->m_sb.sb_uuid))
  145. xchk_block_set_preen(sc, bp);
  146. if (sb->sb_logstart != cpu_to_be64(mp->m_sb.sb_logstart))
  147. xchk_block_set_corrupt(sc, bp);
  148. if (sb->sb_rootino != cpu_to_be64(mp->m_sb.sb_rootino))
  149. xchk_block_set_preen(sc, bp);
  150. if (sb->sb_rbmino != cpu_to_be64(mp->m_sb.sb_rbmino))
  151. xchk_block_set_preen(sc, bp);
  152. if (sb->sb_rsumino != cpu_to_be64(mp->m_sb.sb_rsumino))
  153. xchk_block_set_preen(sc, bp);
  154. if (sb->sb_rextsize != cpu_to_be32(mp->m_sb.sb_rextsize))
  155. xchk_block_set_corrupt(sc, bp);
  156. if (sb->sb_agblocks != cpu_to_be32(mp->m_sb.sb_agblocks))
  157. xchk_block_set_corrupt(sc, bp);
  158. if (sb->sb_agcount != cpu_to_be32(mp->m_sb.sb_agcount))
  159. xchk_block_set_corrupt(sc, bp);
  160. if (sb->sb_rbmblocks != cpu_to_be32(mp->m_sb.sb_rbmblocks))
  161. xchk_block_set_corrupt(sc, bp);
  162. if (sb->sb_logblocks != cpu_to_be32(mp->m_sb.sb_logblocks))
  163. xchk_block_set_corrupt(sc, bp);
  164. /* Check sb_versionnum bits that are set at mkfs time. */
  165. vernum_mask = cpu_to_be16(XFS_SB_VERSION_NUMBITS |
  166. XFS_SB_VERSION_ALIGNBIT |
  167. XFS_SB_VERSION_DALIGNBIT |
  168. XFS_SB_VERSION_SHAREDBIT |
  169. XFS_SB_VERSION_LOGV2BIT |
  170. XFS_SB_VERSION_SECTORBIT |
  171. XFS_SB_VERSION_EXTFLGBIT |
  172. XFS_SB_VERSION_DIRV2BIT);
  173. if ((sb->sb_versionnum & vernum_mask) !=
  174. (cpu_to_be16(mp->m_sb.sb_versionnum) & vernum_mask))
  175. xchk_block_set_corrupt(sc, bp);
  176. /* Check sb_versionnum bits that can be set after mkfs time. */
  177. vernum_mask = cpu_to_be16(XFS_SB_VERSION_ATTRBIT |
  178. XFS_SB_VERSION_NLINKBIT |
  179. XFS_SB_VERSION_QUOTABIT);
  180. if ((sb->sb_versionnum & vernum_mask) !=
  181. (cpu_to_be16(mp->m_sb.sb_versionnum) & vernum_mask))
  182. xchk_block_set_preen(sc, bp);
  183. if (sb->sb_sectsize != cpu_to_be16(mp->m_sb.sb_sectsize))
  184. xchk_block_set_corrupt(sc, bp);
  185. if (sb->sb_inodesize != cpu_to_be16(mp->m_sb.sb_inodesize))
  186. xchk_block_set_corrupt(sc, bp);
  187. if (sb->sb_inopblock != cpu_to_be16(mp->m_sb.sb_inopblock))
  188. xchk_block_set_corrupt(sc, bp);
  189. if (memcmp(sb->sb_fname, mp->m_sb.sb_fname, sizeof(sb->sb_fname)))
  190. xchk_block_set_preen(sc, bp);
  191. if (sb->sb_blocklog != mp->m_sb.sb_blocklog)
  192. xchk_block_set_corrupt(sc, bp);
  193. if (sb->sb_sectlog != mp->m_sb.sb_sectlog)
  194. xchk_block_set_corrupt(sc, bp);
  195. if (sb->sb_inodelog != mp->m_sb.sb_inodelog)
  196. xchk_block_set_corrupt(sc, bp);
  197. if (sb->sb_inopblog != mp->m_sb.sb_inopblog)
  198. xchk_block_set_corrupt(sc, bp);
  199. if (sb->sb_agblklog != mp->m_sb.sb_agblklog)
  200. xchk_block_set_corrupt(sc, bp);
  201. if (sb->sb_rextslog != mp->m_sb.sb_rextslog)
  202. xchk_block_set_corrupt(sc, bp);
  203. if (sb->sb_imax_pct != mp->m_sb.sb_imax_pct)
  204. xchk_block_set_preen(sc, bp);
  205. /*
  206. * Skip the summary counters since we track them in memory anyway.
  207. * sb_icount, sb_ifree, sb_fdblocks, sb_frexents
  208. */
  209. if (sb->sb_uquotino != cpu_to_be64(mp->m_sb.sb_uquotino))
  210. xchk_block_set_preen(sc, bp);
  211. if (sb->sb_gquotino != cpu_to_be64(mp->m_sb.sb_gquotino))
  212. xchk_block_set_preen(sc, bp);
  213. /*
  214. * Skip the quota flags since repair will force quotacheck.
  215. * sb_qflags
  216. */
  217. if (sb->sb_flags != mp->m_sb.sb_flags)
  218. xchk_block_set_corrupt(sc, bp);
  219. if (sb->sb_shared_vn != mp->m_sb.sb_shared_vn)
  220. xchk_block_set_corrupt(sc, bp);
  221. if (sb->sb_inoalignmt != cpu_to_be32(mp->m_sb.sb_inoalignmt))
  222. xchk_block_set_corrupt(sc, bp);
  223. if (sb->sb_unit != cpu_to_be32(mp->m_sb.sb_unit))
  224. xchk_block_set_preen(sc, bp);
  225. if (sb->sb_width != cpu_to_be32(mp->m_sb.sb_width))
  226. xchk_block_set_preen(sc, bp);
  227. if (sb->sb_dirblklog != mp->m_sb.sb_dirblklog)
  228. xchk_block_set_corrupt(sc, bp);
  229. if (sb->sb_logsectlog != mp->m_sb.sb_logsectlog)
  230. xchk_block_set_corrupt(sc, bp);
  231. if (sb->sb_logsectsize != cpu_to_be16(mp->m_sb.sb_logsectsize))
  232. xchk_block_set_corrupt(sc, bp);
  233. if (sb->sb_logsunit != cpu_to_be32(mp->m_sb.sb_logsunit))
  234. xchk_block_set_corrupt(sc, bp);
  235. /* Do we see any invalid bits in sb_features2? */
  236. if (!xfs_sb_version_hasmorebits(&mp->m_sb)) {
  237. if (sb->sb_features2 != 0)
  238. xchk_block_set_corrupt(sc, bp);
  239. } else {
  240. v2_ok = XFS_SB_VERSION2_OKBITS;
  241. if (xfs_sb_is_v5(&mp->m_sb))
  242. v2_ok |= XFS_SB_VERSION2_CRCBIT;
  243. if (!!(sb->sb_features2 & cpu_to_be32(~v2_ok)))
  244. xchk_block_set_corrupt(sc, bp);
  245. if (sb->sb_features2 != sb->sb_bad_features2)
  246. xchk_block_set_preen(sc, bp);
  247. }
  248. /* Check sb_features2 flags that are set at mkfs time. */
  249. features_mask = cpu_to_be32(XFS_SB_VERSION2_LAZYSBCOUNTBIT |
  250. XFS_SB_VERSION2_PROJID32BIT |
  251. XFS_SB_VERSION2_CRCBIT |
  252. XFS_SB_VERSION2_FTYPE);
  253. if ((sb->sb_features2 & features_mask) !=
  254. (cpu_to_be32(mp->m_sb.sb_features2) & features_mask))
  255. xchk_block_set_corrupt(sc, bp);
  256. /* Check sb_features2 flags that can be set after mkfs time. */
  257. features_mask = cpu_to_be32(XFS_SB_VERSION2_ATTR2BIT);
  258. if ((sb->sb_features2 & features_mask) !=
  259. (cpu_to_be32(mp->m_sb.sb_features2) & features_mask))
  260. xchk_block_set_preen(sc, bp);
  261. if (!xfs_has_crc(mp)) {
  262. /* all v5 fields must be zero */
  263. if (memchr_inv(&sb->sb_features_compat, 0,
  264. sizeof(struct xfs_dsb) -
  265. offsetof(struct xfs_dsb, sb_features_compat)))
  266. xchk_block_set_corrupt(sc, bp);
  267. } else {
  268. /* compat features must match */
  269. if (sb->sb_features_compat !=
  270. cpu_to_be32(mp->m_sb.sb_features_compat))
  271. xchk_block_set_corrupt(sc, bp);
  272. /* ro compat features must match */
  273. if (sb->sb_features_ro_compat !=
  274. cpu_to_be32(mp->m_sb.sb_features_ro_compat))
  275. xchk_block_set_corrupt(sc, bp);
  276. /*
  277. * NEEDSREPAIR is ignored on a secondary super, so we should
  278. * clear it when we find it, though it's not a corruption.
  279. */
  280. features_mask = cpu_to_be32(XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR);
  281. if ((cpu_to_be32(mp->m_sb.sb_features_incompat) ^
  282. sb->sb_features_incompat) & features_mask)
  283. xchk_block_set_preen(sc, bp);
  284. /* all other incompat features must match */
  285. if ((cpu_to_be32(mp->m_sb.sb_features_incompat) ^
  286. sb->sb_features_incompat) & ~features_mask)
  287. xchk_block_set_corrupt(sc, bp);
  288. /*
  289. * log incompat features protect newer log record types from
  290. * older log recovery code. Log recovery doesn't check the
  291. * secondary supers, so we can clear these if needed.
  292. */
  293. if (sb->sb_features_log_incompat)
  294. xchk_block_set_preen(sc, bp);
  295. /* Don't care about sb_crc */
  296. if (sb->sb_spino_align != cpu_to_be32(mp->m_sb.sb_spino_align))
  297. xchk_block_set_corrupt(sc, bp);
  298. if (sb->sb_pquotino != cpu_to_be64(mp->m_sb.sb_pquotino))
  299. xchk_block_set_preen(sc, bp);
  300. /* Don't care about sb_lsn */
  301. }
  302. if (xfs_has_metauuid(mp)) {
  303. /* The metadata UUID must be the same for all supers */
  304. if (!uuid_equal(&sb->sb_meta_uuid, &mp->m_sb.sb_meta_uuid))
  305. xchk_block_set_corrupt(sc, bp);
  306. }
  307. /* Everything else must be zero. */
  308. sblen = xchk_superblock_ondisk_size(mp);
  309. if (memchr_inv((char *)sb + sblen, 0, BBTOB(bp->b_length) - sblen))
  310. xchk_block_set_corrupt(sc, bp);
  311. xchk_superblock_xref(sc, bp);
  312. out_pag:
  313. xfs_perag_put(pag);
  314. return error;
  315. }
  316. /* AGF */
  317. /* Tally freespace record lengths. */
  318. STATIC int
  319. xchk_agf_record_bno_lengths(
  320. struct xfs_btree_cur *cur,
  321. const struct xfs_alloc_rec_incore *rec,
  322. void *priv)
  323. {
  324. xfs_extlen_t *blocks = priv;
  325. (*blocks) += rec->ar_blockcount;
  326. return 0;
  327. }
  328. /* Check agf_freeblks */
  329. static inline void
  330. xchk_agf_xref_freeblks(
  331. struct xfs_scrub *sc)
  332. {
  333. struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
  334. xfs_extlen_t blocks = 0;
  335. int error;
  336. if (!sc->sa.bno_cur)
  337. return;
  338. error = xfs_alloc_query_all(sc->sa.bno_cur,
  339. xchk_agf_record_bno_lengths, &blocks);
  340. if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
  341. return;
  342. if (blocks != be32_to_cpu(agf->agf_freeblks))
  343. xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
  344. }
  345. /* Cross reference the AGF with the cntbt (freespace by length btree) */
  346. static inline void
  347. xchk_agf_xref_cntbt(
  348. struct xfs_scrub *sc)
  349. {
  350. struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
  351. xfs_agblock_t agbno;
  352. xfs_extlen_t blocks;
  353. int have;
  354. int error;
  355. if (!sc->sa.cnt_cur)
  356. return;
  357. /* Any freespace at all? */
  358. error = xfs_alloc_lookup_le(sc->sa.cnt_cur, 0, -1U, &have);
  359. if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
  360. return;
  361. if (!have) {
  362. if (agf->agf_freeblks != cpu_to_be32(0))
  363. xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
  364. return;
  365. }
  366. /* Check agf_longest */
  367. error = xfs_alloc_get_rec(sc->sa.cnt_cur, &agbno, &blocks, &have);
  368. if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
  369. return;
  370. if (!have || blocks != be32_to_cpu(agf->agf_longest))
  371. xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
  372. }
  373. /* Check the btree block counts in the AGF against the btrees. */
  374. STATIC void
  375. xchk_agf_xref_btreeblks(
  376. struct xfs_scrub *sc)
  377. {
  378. struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
  379. struct xfs_mount *mp = sc->mp;
  380. xfs_filblks_t blocks;
  381. xfs_agblock_t btreeblks;
  382. int error;
  383. /* agf_btreeblks didn't exist before lazysbcount */
  384. if (!xfs_has_lazysbcount(sc->mp))
  385. return;
  386. /* Check agf_rmap_blocks; set up for agf_btreeblks check */
  387. if (sc->sa.rmap_cur) {
  388. error = xfs_btree_count_blocks(sc->sa.rmap_cur, &blocks);
  389. if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
  390. return;
  391. btreeblks = blocks - 1;
  392. if (blocks != be32_to_cpu(agf->agf_rmap_blocks))
  393. xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
  394. } else {
  395. btreeblks = 0;
  396. }
  397. /*
  398. * No rmap cursor; we can't xref if we have the rmapbt feature.
  399. * We also can't do it if we're missing the free space btree cursors.
  400. */
  401. if ((xfs_has_rmapbt(mp) && !sc->sa.rmap_cur) ||
  402. !sc->sa.bno_cur || !sc->sa.cnt_cur)
  403. return;
  404. /* Check agf_btreeblks */
  405. error = xfs_btree_count_blocks(sc->sa.bno_cur, &blocks);
  406. if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
  407. return;
  408. btreeblks += blocks - 1;
  409. error = xfs_btree_count_blocks(sc->sa.cnt_cur, &blocks);
  410. if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
  411. return;
  412. btreeblks += blocks - 1;
  413. if (btreeblks != be32_to_cpu(agf->agf_btreeblks))
  414. xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
  415. }
  416. /* Check agf_refcount_blocks against tree size */
  417. static inline void
  418. xchk_agf_xref_refcblks(
  419. struct xfs_scrub *sc)
  420. {
  421. struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
  422. xfs_filblks_t blocks;
  423. int error;
  424. if (!sc->sa.refc_cur)
  425. return;
  426. error = xfs_btree_count_blocks(sc->sa.refc_cur, &blocks);
  427. if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
  428. return;
  429. if (blocks != be32_to_cpu(agf->agf_refcount_blocks))
  430. xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
  431. }
  432. /* Cross-reference with the other btrees. */
  433. STATIC void
  434. xchk_agf_xref(
  435. struct xfs_scrub *sc)
  436. {
  437. struct xfs_mount *mp = sc->mp;
  438. xfs_agblock_t agbno;
  439. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  440. return;
  441. agbno = XFS_AGF_BLOCK(mp);
  442. xchk_ag_btcur_init(sc, &sc->sa);
  443. xchk_xref_is_used_space(sc, agbno, 1);
  444. xchk_agf_xref_freeblks(sc);
  445. xchk_agf_xref_cntbt(sc);
  446. xchk_xref_is_not_inode_chunk(sc, agbno, 1);
  447. xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
  448. xchk_agf_xref_btreeblks(sc);
  449. xchk_xref_is_not_shared(sc, agbno, 1);
  450. xchk_xref_is_not_cow_staging(sc, agbno, 1);
  451. xchk_agf_xref_refcblks(sc);
  452. /* scrub teardown will take care of sc->sa for us */
  453. }
  454. /* Scrub the AGF. */
  455. int
  456. xchk_agf(
  457. struct xfs_scrub *sc)
  458. {
  459. struct xfs_mount *mp = sc->mp;
  460. struct xfs_agf *agf;
  461. struct xfs_perag *pag;
  462. xfs_agnumber_t agno = sc->sm->sm_agno;
  463. xfs_agblock_t agbno;
  464. xfs_agblock_t eoag;
  465. xfs_agblock_t agfl_first;
  466. xfs_agblock_t agfl_last;
  467. xfs_agblock_t agfl_count;
  468. xfs_agblock_t fl_count;
  469. int level;
  470. int error = 0;
  471. error = xchk_ag_read_headers(sc, agno, &sc->sa);
  472. if (!xchk_process_error(sc, agno, XFS_AGF_BLOCK(sc->mp), &error))
  473. goto out;
  474. xchk_buffer_recheck(sc, sc->sa.agf_bp);
  475. agf = sc->sa.agf_bp->b_addr;
  476. pag = sc->sa.pag;
  477. /* Check the AG length */
  478. eoag = be32_to_cpu(agf->agf_length);
  479. if (eoag != pag->block_count)
  480. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  481. /* Check the AGF btree roots and levels */
  482. agbno = be32_to_cpu(agf->agf_bno_root);
  483. if (!xfs_verify_agbno(pag, agbno))
  484. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  485. agbno = be32_to_cpu(agf->agf_cnt_root);
  486. if (!xfs_verify_agbno(pag, agbno))
  487. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  488. level = be32_to_cpu(agf->agf_bno_level);
  489. if (level <= 0 || level > mp->m_alloc_maxlevels)
  490. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  491. level = be32_to_cpu(agf->agf_cnt_level);
  492. if (level <= 0 || level > mp->m_alloc_maxlevels)
  493. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  494. if (xfs_has_rmapbt(mp)) {
  495. agbno = be32_to_cpu(agf->agf_rmap_root);
  496. if (!xfs_verify_agbno(pag, agbno))
  497. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  498. level = be32_to_cpu(agf->agf_rmap_level);
  499. if (level <= 0 || level > mp->m_rmap_maxlevels)
  500. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  501. }
  502. if (xfs_has_reflink(mp)) {
  503. agbno = be32_to_cpu(agf->agf_refcount_root);
  504. if (!xfs_verify_agbno(pag, agbno))
  505. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  506. level = be32_to_cpu(agf->agf_refcount_level);
  507. if (level <= 0 || level > mp->m_refc_maxlevels)
  508. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  509. }
  510. /* Check the AGFL counters */
  511. agfl_first = be32_to_cpu(agf->agf_flfirst);
  512. agfl_last = be32_to_cpu(agf->agf_fllast);
  513. agfl_count = be32_to_cpu(agf->agf_flcount);
  514. if (agfl_last > agfl_first)
  515. fl_count = agfl_last - agfl_first + 1;
  516. else
  517. fl_count = xfs_agfl_size(mp) - agfl_first + agfl_last + 1;
  518. if (agfl_count != 0 && fl_count != agfl_count)
  519. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  520. /* Do the incore counters match? */
  521. if (pag->pagf_freeblks != be32_to_cpu(agf->agf_freeblks))
  522. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  523. if (pag->pagf_flcount != be32_to_cpu(agf->agf_flcount))
  524. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  525. if (xfs_has_lazysbcount(sc->mp) &&
  526. pag->pagf_btreeblks != be32_to_cpu(agf->agf_btreeblks))
  527. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  528. xchk_agf_xref(sc);
  529. out:
  530. return error;
  531. }
  532. /* AGFL */
  533. struct xchk_agfl_info {
  534. /* Number of AGFL entries that the AGF claims are in use. */
  535. unsigned int agflcount;
  536. /* Number of AGFL entries that we found. */
  537. unsigned int nr_entries;
  538. /* Buffer to hold AGFL entries for extent checking. */
  539. xfs_agblock_t *entries;
  540. struct xfs_buf *agfl_bp;
  541. struct xfs_scrub *sc;
  542. };
  543. /* Cross-reference with the other btrees. */
  544. STATIC void
  545. xchk_agfl_block_xref(
  546. struct xfs_scrub *sc,
  547. xfs_agblock_t agbno)
  548. {
  549. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  550. return;
  551. xchk_xref_is_used_space(sc, agbno, 1);
  552. xchk_xref_is_not_inode_chunk(sc, agbno, 1);
  553. xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_AG);
  554. xchk_xref_is_not_shared(sc, agbno, 1);
  555. xchk_xref_is_not_cow_staging(sc, agbno, 1);
  556. }
  557. /* Scrub an AGFL block. */
  558. STATIC int
  559. xchk_agfl_block(
  560. struct xfs_mount *mp,
  561. xfs_agblock_t agbno,
  562. void *priv)
  563. {
  564. struct xchk_agfl_info *sai = priv;
  565. struct xfs_scrub *sc = sai->sc;
  566. if (xfs_verify_agbno(sc->sa.pag, agbno) &&
  567. sai->nr_entries < sai->agflcount)
  568. sai->entries[sai->nr_entries++] = agbno;
  569. else
  570. xchk_block_set_corrupt(sc, sai->agfl_bp);
  571. xchk_agfl_block_xref(sc, agbno);
  572. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  573. return -ECANCELED;
  574. return 0;
  575. }
  576. static int
  577. xchk_agblock_cmp(
  578. const void *pa,
  579. const void *pb)
  580. {
  581. const xfs_agblock_t *a = pa;
  582. const xfs_agblock_t *b = pb;
  583. return (int)*a - (int)*b;
  584. }
  585. /* Cross-reference with the other btrees. */
  586. STATIC void
  587. xchk_agfl_xref(
  588. struct xfs_scrub *sc)
  589. {
  590. struct xfs_mount *mp = sc->mp;
  591. xfs_agblock_t agbno;
  592. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  593. return;
  594. agbno = XFS_AGFL_BLOCK(mp);
  595. xchk_ag_btcur_init(sc, &sc->sa);
  596. xchk_xref_is_used_space(sc, agbno, 1);
  597. xchk_xref_is_not_inode_chunk(sc, agbno, 1);
  598. xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
  599. xchk_xref_is_not_shared(sc, agbno, 1);
  600. xchk_xref_is_not_cow_staging(sc, agbno, 1);
  601. /*
  602. * Scrub teardown will take care of sc->sa for us. Leave sc->sa
  603. * active so that the agfl block xref can use it too.
  604. */
  605. }
  606. /* Scrub the AGFL. */
  607. int
  608. xchk_agfl(
  609. struct xfs_scrub *sc)
  610. {
  611. struct xchk_agfl_info sai = {
  612. .sc = sc,
  613. };
  614. struct xfs_agf *agf;
  615. xfs_agnumber_t agno = sc->sm->sm_agno;
  616. unsigned int i;
  617. int error;
  618. /* Lock the AGF and AGI so that nobody can touch this AG. */
  619. error = xchk_ag_read_headers(sc, agno, &sc->sa);
  620. if (!xchk_process_error(sc, agno, XFS_AGFL_BLOCK(sc->mp), &error))
  621. return error;
  622. if (!sc->sa.agf_bp)
  623. return -EFSCORRUPTED;
  624. /* Try to read the AGFL, and verify its structure if we get it. */
  625. error = xfs_alloc_read_agfl(sc->sa.pag, sc->tp, &sai.agfl_bp);
  626. if (!xchk_process_error(sc, agno, XFS_AGFL_BLOCK(sc->mp), &error))
  627. return error;
  628. xchk_buffer_recheck(sc, sai.agfl_bp);
  629. xchk_agfl_xref(sc);
  630. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  631. goto out;
  632. /* Allocate buffer to ensure uniqueness of AGFL entries. */
  633. agf = sc->sa.agf_bp->b_addr;
  634. sai.agflcount = be32_to_cpu(agf->agf_flcount);
  635. if (sai.agflcount > xfs_agfl_size(sc->mp)) {
  636. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  637. goto out;
  638. }
  639. sai.entries = kvcalloc(sai.agflcount, sizeof(xfs_agblock_t),
  640. XCHK_GFP_FLAGS);
  641. if (!sai.entries) {
  642. error = -ENOMEM;
  643. goto out;
  644. }
  645. /* Check the blocks in the AGFL. */
  646. error = xfs_agfl_walk(sc->mp, sc->sa.agf_bp->b_addr, sai.agfl_bp,
  647. xchk_agfl_block, &sai);
  648. if (error == -ECANCELED) {
  649. error = 0;
  650. goto out_free;
  651. }
  652. if (error)
  653. goto out_free;
  654. if (sai.agflcount != sai.nr_entries) {
  655. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  656. goto out_free;
  657. }
  658. /* Sort entries, check for duplicates. */
  659. sort(sai.entries, sai.nr_entries, sizeof(sai.entries[0]),
  660. xchk_agblock_cmp, NULL);
  661. for (i = 1; i < sai.nr_entries; i++) {
  662. if (sai.entries[i] == sai.entries[i - 1]) {
  663. xchk_block_set_corrupt(sc, sc->sa.agf_bp);
  664. break;
  665. }
  666. }
  667. out_free:
  668. kvfree(sai.entries);
  669. out:
  670. return error;
  671. }
  672. /* AGI */
  673. /* Check agi_count/agi_freecount */
  674. static inline void
  675. xchk_agi_xref_icounts(
  676. struct xfs_scrub *sc)
  677. {
  678. struct xfs_agi *agi = sc->sa.agi_bp->b_addr;
  679. xfs_agino_t icount;
  680. xfs_agino_t freecount;
  681. int error;
  682. if (!sc->sa.ino_cur)
  683. return;
  684. error = xfs_ialloc_count_inodes(sc->sa.ino_cur, &icount, &freecount);
  685. if (!xchk_should_check_xref(sc, &error, &sc->sa.ino_cur))
  686. return;
  687. if (be32_to_cpu(agi->agi_count) != icount ||
  688. be32_to_cpu(agi->agi_freecount) != freecount)
  689. xchk_block_xref_set_corrupt(sc, sc->sa.agi_bp);
  690. }
  691. /* Check agi_[fi]blocks against tree size */
  692. static inline void
  693. xchk_agi_xref_fiblocks(
  694. struct xfs_scrub *sc)
  695. {
  696. struct xfs_agi *agi = sc->sa.agi_bp->b_addr;
  697. xfs_filblks_t blocks;
  698. int error = 0;
  699. if (!xfs_has_inobtcounts(sc->mp))
  700. return;
  701. if (sc->sa.ino_cur) {
  702. error = xfs_btree_count_blocks(sc->sa.ino_cur, &blocks);
  703. if (!xchk_should_check_xref(sc, &error, &sc->sa.ino_cur))
  704. return;
  705. if (blocks != be32_to_cpu(agi->agi_iblocks))
  706. xchk_block_xref_set_corrupt(sc, sc->sa.agi_bp);
  707. }
  708. if (sc->sa.fino_cur) {
  709. error = xfs_btree_count_blocks(sc->sa.fino_cur, &blocks);
  710. if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur))
  711. return;
  712. if (blocks != be32_to_cpu(agi->agi_fblocks))
  713. xchk_block_xref_set_corrupt(sc, sc->sa.agi_bp);
  714. }
  715. }
  716. /* Cross-reference with the other btrees. */
  717. STATIC void
  718. xchk_agi_xref(
  719. struct xfs_scrub *sc)
  720. {
  721. struct xfs_mount *mp = sc->mp;
  722. xfs_agblock_t agbno;
  723. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  724. return;
  725. agbno = XFS_AGI_BLOCK(mp);
  726. xchk_ag_btcur_init(sc, &sc->sa);
  727. xchk_xref_is_used_space(sc, agbno, 1);
  728. xchk_xref_is_not_inode_chunk(sc, agbno, 1);
  729. xchk_agi_xref_icounts(sc);
  730. xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
  731. xchk_xref_is_not_shared(sc, agbno, 1);
  732. xchk_xref_is_not_cow_staging(sc, agbno, 1);
  733. xchk_agi_xref_fiblocks(sc);
  734. /* scrub teardown will take care of sc->sa for us */
  735. }
  736. /*
  737. * Check the unlinked buckets for links to bad inodes. We hold the AGI, so
  738. * there cannot be any threads updating unlinked list pointers in this AG.
  739. */
  740. STATIC void
  741. xchk_iunlink(
  742. struct xfs_scrub *sc,
  743. struct xfs_agi *agi)
  744. {
  745. unsigned int i;
  746. struct xfs_inode *ip;
  747. for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
  748. xfs_agino_t agino = be32_to_cpu(agi->agi_unlinked[i]);
  749. while (agino != NULLAGINO) {
  750. if (agino % XFS_AGI_UNLINKED_BUCKETS != i) {
  751. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  752. return;
  753. }
  754. ip = xfs_iunlink_lookup(sc->sa.pag, agino);
  755. if (!ip) {
  756. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  757. return;
  758. }
  759. if (!xfs_inode_on_unlinked_list(ip)) {
  760. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  761. return;
  762. }
  763. agino = ip->i_next_unlinked;
  764. }
  765. }
  766. }
  767. /* Scrub the AGI. */
  768. int
  769. xchk_agi(
  770. struct xfs_scrub *sc)
  771. {
  772. struct xfs_mount *mp = sc->mp;
  773. struct xfs_agi *agi;
  774. struct xfs_perag *pag;
  775. struct xfs_ino_geometry *igeo = M_IGEO(sc->mp);
  776. xfs_agnumber_t agno = sc->sm->sm_agno;
  777. xfs_agblock_t agbno;
  778. xfs_agblock_t eoag;
  779. xfs_agino_t agino;
  780. xfs_agino_t first_agino;
  781. xfs_agino_t last_agino;
  782. xfs_agino_t icount;
  783. int i;
  784. int level;
  785. int error = 0;
  786. error = xchk_ag_read_headers(sc, agno, &sc->sa);
  787. if (!xchk_process_error(sc, agno, XFS_AGI_BLOCK(sc->mp), &error))
  788. goto out;
  789. xchk_buffer_recheck(sc, sc->sa.agi_bp);
  790. agi = sc->sa.agi_bp->b_addr;
  791. pag = sc->sa.pag;
  792. /* Check the AG length */
  793. eoag = be32_to_cpu(agi->agi_length);
  794. if (eoag != pag->block_count)
  795. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  796. /* Check btree roots and levels */
  797. agbno = be32_to_cpu(agi->agi_root);
  798. if (!xfs_verify_agbno(pag, agbno))
  799. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  800. level = be32_to_cpu(agi->agi_level);
  801. if (level <= 0 || level > igeo->inobt_maxlevels)
  802. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  803. if (xfs_has_finobt(mp)) {
  804. agbno = be32_to_cpu(agi->agi_free_root);
  805. if (!xfs_verify_agbno(pag, agbno))
  806. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  807. level = be32_to_cpu(agi->agi_free_level);
  808. if (level <= 0 || level > igeo->inobt_maxlevels)
  809. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  810. }
  811. /* Check inode counters */
  812. xfs_agino_range(mp, agno, &first_agino, &last_agino);
  813. icount = be32_to_cpu(agi->agi_count);
  814. if (icount > last_agino - first_agino + 1 ||
  815. icount < be32_to_cpu(agi->agi_freecount))
  816. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  817. /* Check inode pointers */
  818. agino = be32_to_cpu(agi->agi_newino);
  819. if (!xfs_verify_agino_or_null(pag, agino))
  820. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  821. agino = be32_to_cpu(agi->agi_dirino);
  822. if (!xfs_verify_agino_or_null(pag, agino))
  823. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  824. /* Check unlinked inode buckets */
  825. for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
  826. agino = be32_to_cpu(agi->agi_unlinked[i]);
  827. if (!xfs_verify_agino_or_null(pag, agino))
  828. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  829. }
  830. if (agi->agi_pad32 != cpu_to_be32(0))
  831. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  832. /* Do the incore counters match? */
  833. if (pag->pagi_count != be32_to_cpu(agi->agi_count))
  834. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  835. if (pag->pagi_freecount != be32_to_cpu(agi->agi_freecount))
  836. xchk_block_set_corrupt(sc, sc->sa.agi_bp);
  837. xchk_iunlink(sc, agi);
  838. xchk_agi_xref(sc);
  839. out:
  840. return error;
  841. }