fscounters.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2019-2023 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <djwong@kernel.org>
  5. */
  6. #include "xfs_platform.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_log_format.h"
  12. #include "xfs_trans.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_alloc.h"
  15. #include "xfs_ialloc.h"
  16. #include "xfs_health.h"
  17. #include "xfs_btree.h"
  18. #include "xfs_ag.h"
  19. #include "xfs_rtbitmap.h"
  20. #include "xfs_inode.h"
  21. #include "xfs_icache.h"
  22. #include "xfs_rtgroup.h"
  23. #include "scrub/scrub.h"
  24. #include "scrub/common.h"
  25. #include "scrub/trace.h"
  26. #include "scrub/fscounters.h"
  27. /*
  28. * FS Summary Counters
  29. * ===================
  30. *
  31. * The basics of filesystem summary counter checking are that we iterate the
  32. * AGs counting the number of free blocks, free space btree blocks, per-AG
  33. * reservations, inodes, delayed allocation reservations, and free inodes.
  34. * Then we compare what we computed against the in-core counters.
  35. *
  36. * However, the reality is that summary counters are a tricky beast to check.
  37. * While we /could/ freeze the filesystem and scramble around the AGs counting
  38. * the free blocks, in practice we prefer not do that for a scan because
  39. * freezing is costly. To get around this, we added a per-cpu counter of the
  40. * delalloc reservations so that we can rotor around the AGs relatively
  41. * quickly, and we allow the counts to be slightly off because we're not taking
  42. * any locks while we do this.
  43. *
  44. * So the first thing we do is warm up the buffer cache in the setup routine by
  45. * walking all the AGs to make sure the incore per-AG structure has been
  46. * initialized. The expected value calculation then iterates the incore per-AG
  47. * structures as quickly as it can. We snapshot the percpu counters before and
  48. * after this operation and use the difference in counter values to guess at
  49. * our tolerance for mismatch between expected and actual counter values.
  50. */
  51. /*
  52. * Since the expected value computation is lockless but only browses incore
  53. * values, the percpu counters should be fairly close to each other. However,
  54. * we'll allow ourselves to be off by at least this (arbitrary) amount.
  55. */
  56. #define XCHK_FSCOUNT_MIN_VARIANCE (512)
  57. /*
  58. * Make sure the per-AG structure has been initialized from the on-disk header
  59. * contents and trust that the incore counters match the ondisk counters. (The
  60. * AGF and AGI scrubbers check them, and a normal xfs_scrub run checks the
  61. * summary counters after checking all AG headers). Do this from the setup
  62. * function so that the inner AG aggregation loop runs as quickly as possible.
  63. *
  64. * This function runs during the setup phase /before/ we start checking any
  65. * metadata.
  66. */
  67. STATIC int
  68. xchk_fscount_warmup(
  69. struct xfs_scrub *sc)
  70. {
  71. struct xfs_mount *mp = sc->mp;
  72. struct xfs_buf *agi_bp = NULL;
  73. struct xfs_buf *agf_bp = NULL;
  74. struct xfs_perag *pag = NULL;
  75. int error = 0;
  76. while ((pag = xfs_perag_next(mp, pag))) {
  77. if (xchk_should_terminate(sc, &error))
  78. break;
  79. if (xfs_perag_initialised_agi(pag) &&
  80. xfs_perag_initialised_agf(pag))
  81. continue;
  82. /* Lock both AG headers. */
  83. error = xfs_ialloc_read_agi(pag, sc->tp, 0, &agi_bp);
  84. if (error)
  85. break;
  86. error = xfs_alloc_read_agf(pag, sc->tp, 0, &agf_bp);
  87. if (error)
  88. break;
  89. /*
  90. * These are supposed to be initialized by the header read
  91. * function.
  92. */
  93. if (!xfs_perag_initialised_agi(pag) ||
  94. !xfs_perag_initialised_agf(pag)) {
  95. error = -EFSCORRUPTED;
  96. break;
  97. }
  98. xfs_buf_relse(agf_bp);
  99. agf_bp = NULL;
  100. xfs_buf_relse(agi_bp);
  101. agi_bp = NULL;
  102. }
  103. if (agf_bp)
  104. xfs_buf_relse(agf_bp);
  105. if (agi_bp)
  106. xfs_buf_relse(agi_bp);
  107. if (pag)
  108. xfs_perag_rele(pag);
  109. return error;
  110. }
  111. static inline int
  112. xchk_fsfreeze(
  113. struct xfs_scrub *sc)
  114. {
  115. int error;
  116. error = freeze_super(sc->mp->m_super, FREEZE_HOLDER_KERNEL, NULL);
  117. trace_xchk_fsfreeze(sc, error);
  118. return error;
  119. }
  120. static inline int
  121. xchk_fsthaw(
  122. struct xfs_scrub *sc)
  123. {
  124. int error;
  125. /* This should always succeed, we have a kernel freeze */
  126. error = thaw_super(sc->mp->m_super, FREEZE_HOLDER_KERNEL, NULL);
  127. trace_xchk_fsthaw(sc, error);
  128. return error;
  129. }
  130. /*
  131. * We couldn't stabilize the filesystem long enough to sample all the variables
  132. * that comprise the summary counters and compare them to the percpu counters.
  133. * We need to disable all writer threads, which means taking the first two
  134. * freeze levels to put userspace to sleep, and the third freeze level to
  135. * prevent background threads from starting new transactions. Take one level
  136. * more to prevent other callers from unfreezing the filesystem while we run.
  137. */
  138. STATIC int
  139. xchk_fscounters_freeze(
  140. struct xfs_scrub *sc)
  141. {
  142. struct xchk_fscounters *fsc = sc->buf;
  143. int error = 0;
  144. if (sc->flags & XCHK_HAVE_FREEZE_PROT) {
  145. sc->flags &= ~XCHK_HAVE_FREEZE_PROT;
  146. mnt_drop_write_file(sc->file);
  147. }
  148. /* Try to grab a kernel freeze. */
  149. while ((error = xchk_fsfreeze(sc)) == -EBUSY) {
  150. if (xchk_should_terminate(sc, &error))
  151. return error;
  152. delay(HZ / 10);
  153. }
  154. if (error)
  155. return error;
  156. fsc->frozen = true;
  157. return 0;
  158. }
  159. /* Thaw the filesystem after checking or repairing fscounters. */
  160. STATIC void
  161. xchk_fscounters_cleanup(
  162. void *buf)
  163. {
  164. struct xchk_fscounters *fsc = buf;
  165. struct xfs_scrub *sc = fsc->sc;
  166. int error;
  167. if (!fsc->frozen)
  168. return;
  169. error = xchk_fsthaw(sc);
  170. if (error)
  171. xfs_emerg(sc->mp, "still frozen after scrub, err=%d", error);
  172. else
  173. fsc->frozen = false;
  174. }
  175. int
  176. xchk_setup_fscounters(
  177. struct xfs_scrub *sc)
  178. {
  179. struct xchk_fscounters *fsc;
  180. int error;
  181. /*
  182. * If the AGF doesn't track btreeblks, we have to lock the AGF to count
  183. * btree block usage by walking the actual btrees.
  184. */
  185. if (!xfs_has_lazysbcount(sc->mp))
  186. xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
  187. sc->buf = kzalloc_obj(struct xchk_fscounters, XCHK_GFP_FLAGS);
  188. if (!sc->buf)
  189. return -ENOMEM;
  190. sc->buf_cleanup = xchk_fscounters_cleanup;
  191. fsc = sc->buf;
  192. fsc->sc = sc;
  193. xfs_icount_range(sc->mp, &fsc->icount_min, &fsc->icount_max);
  194. /* We must get the incore counters set up before we can proceed. */
  195. error = xchk_fscount_warmup(sc);
  196. if (error)
  197. return error;
  198. /*
  199. * Pause all writer activity in the filesystem while we're scrubbing to
  200. * reduce the likelihood of background perturbations to the counters
  201. * throwing off our calculations.
  202. *
  203. * If we're repairing, we need to prevent any other thread from
  204. * changing the global fs summary counters while we're repairing them.
  205. * This requires the fs to be frozen, which will disable background
  206. * reclaim and purge all inactive inodes.
  207. */
  208. if ((sc->flags & XCHK_TRY_HARDER) || xchk_could_repair(sc)) {
  209. error = xchk_fscounters_freeze(sc);
  210. if (error)
  211. return error;
  212. }
  213. xchk_trans_alloc_empty(sc);
  214. return 0;
  215. }
  216. /*
  217. * Part 1: Collecting filesystem summary counts. For each AG, we add its
  218. * summary counts (total inodes, free inodes, free data blocks) to an incore
  219. * copy of the overall filesystem summary counts.
  220. *
  221. * To avoid false corruption reports in part 2, any failure in this part must
  222. * set the INCOMPLETE flag even when a negative errno is returned. This care
  223. * must be taken with certain errno values (i.e. EFSBADCRC, EFSCORRUPTED,
  224. * ECANCELED) that are absorbed into a scrub state flag update by
  225. * xchk_*_process_error. Scrub and repair share the same incore data
  226. * structures, so the INCOMPLETE flag is critical to prevent a repair based on
  227. * insufficient information.
  228. */
  229. /* Count free space btree blocks manually for pre-lazysbcount filesystems. */
  230. static int
  231. xchk_fscount_btreeblks(
  232. struct xfs_scrub *sc,
  233. struct xchk_fscounters *fsc,
  234. xfs_agnumber_t agno)
  235. {
  236. xfs_filblks_t blocks;
  237. int error;
  238. error = xchk_ag_init_existing(sc, agno, &sc->sa);
  239. if (error)
  240. goto out_free;
  241. error = xfs_btree_count_blocks(sc->sa.bno_cur, &blocks);
  242. if (error)
  243. goto out_free;
  244. fsc->fdblocks += blocks - 1;
  245. error = xfs_btree_count_blocks(sc->sa.cnt_cur, &blocks);
  246. if (error)
  247. goto out_free;
  248. fsc->fdblocks += blocks - 1;
  249. out_free:
  250. xchk_ag_free(sc, &sc->sa);
  251. return error;
  252. }
  253. /*
  254. * Calculate what the global in-core counters ought to be from the incore
  255. * per-AG structure. Callers can compare this to the actual in-core counters
  256. * to estimate by how much both in-core and on-disk counters need to be
  257. * adjusted.
  258. */
  259. STATIC int
  260. xchk_fscount_aggregate_agcounts(
  261. struct xfs_scrub *sc,
  262. struct xchk_fscounters *fsc)
  263. {
  264. struct xfs_mount *mp = sc->mp;
  265. struct xfs_perag *pag = NULL;
  266. uint64_t delayed;
  267. int tries = 8;
  268. int error = 0;
  269. retry:
  270. fsc->icount = 0;
  271. fsc->ifree = 0;
  272. fsc->fdblocks = 0;
  273. while ((pag = xfs_perag_next(mp, pag))) {
  274. if (xchk_should_terminate(sc, &error))
  275. break;
  276. /* This somehow got unset since the warmup? */
  277. if (!xfs_perag_initialised_agi(pag) ||
  278. !xfs_perag_initialised_agf(pag)) {
  279. error = -EFSCORRUPTED;
  280. break;
  281. }
  282. /* Count all the inodes */
  283. fsc->icount += pag->pagi_count;
  284. fsc->ifree += pag->pagi_freecount;
  285. /* Add up the free/freelist/bnobt/cntbt blocks */
  286. fsc->fdblocks += pag->pagf_freeblks;
  287. fsc->fdblocks += pag->pagf_flcount;
  288. if (xfs_has_lazysbcount(sc->mp)) {
  289. fsc->fdblocks += pag->pagf_btreeblks;
  290. } else {
  291. error = xchk_fscount_btreeblks(sc, fsc, pag_agno(pag));
  292. if (error)
  293. break;
  294. }
  295. /*
  296. * Per-AG reservations are taken out of the incore counters,
  297. * so they must be left out of the free blocks computation.
  298. */
  299. fsc->fdblocks -= pag->pag_meta_resv.ar_reserved;
  300. fsc->fdblocks -= pag->pag_rmapbt_resv.ar_orig_reserved;
  301. }
  302. if (pag)
  303. xfs_perag_rele(pag);
  304. if (error) {
  305. xchk_set_incomplete(sc);
  306. return error;
  307. }
  308. /*
  309. * The global incore space reservation is taken from the incore
  310. * counters, so leave that out of the computation.
  311. */
  312. fsc->fdblocks -= mp->m_free[XC_FREE_BLOCKS].res_avail;
  313. /*
  314. * Delayed allocation reservations are taken out of the incore counters
  315. * but not recorded on disk, so leave them and their indlen blocks out
  316. * of the computation.
  317. */
  318. delayed = percpu_counter_sum(&mp->m_delalloc_blks);
  319. fsc->fdblocks -= delayed;
  320. trace_xchk_fscounters_calc(mp, fsc->icount, fsc->ifree, fsc->fdblocks,
  321. delayed);
  322. /* Bail out if the values we compute are totally nonsense. */
  323. if (fsc->icount < fsc->icount_min || fsc->icount > fsc->icount_max ||
  324. fsc->fdblocks > mp->m_sb.sb_dblocks ||
  325. fsc->ifree > fsc->icount_max)
  326. return -EFSCORRUPTED;
  327. /*
  328. * If ifree > icount then we probably had some perturbation in the
  329. * counters while we were calculating things. We'll try a few times
  330. * to maintain ifree <= icount before giving up.
  331. */
  332. if (fsc->ifree > fsc->icount) {
  333. if (tries--)
  334. goto retry;
  335. return -EDEADLOCK;
  336. }
  337. return 0;
  338. }
  339. #ifdef CONFIG_XFS_RT
  340. STATIC int
  341. xchk_fscount_add_frextent(
  342. struct xfs_rtgroup *rtg,
  343. struct xfs_trans *tp,
  344. const struct xfs_rtalloc_rec *rec,
  345. void *priv)
  346. {
  347. struct xchk_fscounters *fsc = priv;
  348. int error = 0;
  349. fsc->frextents += rec->ar_extcount;
  350. xchk_should_terminate(fsc->sc, &error);
  351. return error;
  352. }
  353. /* Calculate the number of free realtime extents from the realtime bitmap. */
  354. STATIC int
  355. xchk_fscount_count_frextents(
  356. struct xfs_scrub *sc,
  357. struct xchk_fscounters *fsc)
  358. {
  359. struct xfs_mount *mp = sc->mp;
  360. struct xfs_rtgroup *rtg = NULL;
  361. int error;
  362. fsc->frextents = 0;
  363. fsc->frextents_delayed = 0;
  364. /*
  365. * Don't bother verifying and repairing the fs counters for zoned file
  366. * systems as they don't track an on-disk frextents count, and the
  367. * in-memory percpu counter also includes reservations.
  368. */
  369. if (!xfs_has_realtime(mp) || xfs_has_zoned(mp))
  370. return 0;
  371. while ((rtg = xfs_rtgroup_next(mp, rtg))) {
  372. xfs_rtgroup_lock(rtg, XFS_RTGLOCK_BITMAP_SHARED);
  373. error = xfs_rtalloc_query_all(rtg, sc->tp,
  374. xchk_fscount_add_frextent, fsc);
  375. xfs_rtgroup_unlock(rtg, XFS_RTGLOCK_BITMAP_SHARED);
  376. if (error) {
  377. xchk_set_incomplete(sc);
  378. xfs_rtgroup_rele(rtg);
  379. return error;
  380. }
  381. }
  382. fsc->frextents_delayed = percpu_counter_sum(&mp->m_delalloc_rtextents);
  383. return 0;
  384. }
  385. #else
  386. STATIC int
  387. xchk_fscount_count_frextents(
  388. struct xfs_scrub *sc,
  389. struct xchk_fscounters *fsc)
  390. {
  391. fsc->frextents = 0;
  392. fsc->frextents_delayed = 0;
  393. return 0;
  394. }
  395. #endif /* CONFIG_XFS_RT */
  396. /*
  397. * Part 2: Comparing filesystem summary counters. All we have to do here is
  398. * sum the percpu counters and compare them to what we've observed.
  399. */
  400. /*
  401. * Is the @counter reasonably close to the @expected value?
  402. *
  403. * We neither locked nor froze anything in the filesystem while aggregating the
  404. * per-AG data to compute the @expected value, which means that the counter
  405. * could have changed. We know the @old_value of the summation of the counter
  406. * before the aggregation, and we re-sum the counter now. If the expected
  407. * value falls between the two summations, we're ok.
  408. *
  409. * Otherwise, we /might/ have a problem. If the change in the summations is
  410. * more than we want to tolerate, the filesystem is probably busy and we should
  411. * just send back INCOMPLETE and see if userspace will try again.
  412. *
  413. * If we're repairing then we require an exact match.
  414. */
  415. static inline bool
  416. xchk_fscount_within_range(
  417. struct xfs_scrub *sc,
  418. const int64_t old_value,
  419. struct percpu_counter *counter,
  420. uint64_t expected)
  421. {
  422. int64_t min_value, max_value;
  423. int64_t curr_value = percpu_counter_sum(counter);
  424. trace_xchk_fscounters_within_range(sc->mp, expected, curr_value,
  425. old_value);
  426. /* Negative values are always wrong. */
  427. if (curr_value < 0)
  428. return false;
  429. /* Exact matches are always ok. */
  430. if (curr_value == expected)
  431. return true;
  432. /* We require exact matches when repair is running. */
  433. if (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)
  434. return false;
  435. min_value = min(old_value, curr_value);
  436. max_value = max(old_value, curr_value);
  437. /* Within the before-and-after range is ok. */
  438. if (expected >= min_value && expected <= max_value)
  439. return true;
  440. /* Everything else is bad. */
  441. return false;
  442. }
  443. /* Check the superblock counters. */
  444. int
  445. xchk_fscounters(
  446. struct xfs_scrub *sc)
  447. {
  448. struct xfs_mount *mp = sc->mp;
  449. struct xchk_fscounters *fsc = sc->buf;
  450. int64_t icount, ifree, fdblocks, frextents;
  451. bool try_again = false;
  452. int error;
  453. /* Snapshot the percpu counters. */
  454. icount = percpu_counter_sum(&mp->m_icount);
  455. ifree = percpu_counter_sum(&mp->m_ifree);
  456. fdblocks = xfs_sum_freecounter_raw(mp, XC_FREE_BLOCKS);
  457. frextents = xfs_sum_freecounter_raw(mp, XC_FREE_RTEXTENTS);
  458. /* No negative values, please! */
  459. if (icount < 0 || ifree < 0)
  460. xchk_set_corrupt(sc);
  461. /*
  462. * If the filesystem is not frozen, the counter summation calls above
  463. * can race with xfs_dec_freecounter, which subtracts a requested space
  464. * reservation from the counter and undoes the subtraction if that made
  465. * the counter go negative. Therefore, it's possible to see negative
  466. * values here, and we should only flag that as a corruption if we
  467. * froze the fs. This is much more likely to happen with frextents
  468. * since there are no reserved pools.
  469. */
  470. if (fdblocks < 0 || frextents < 0) {
  471. if (!fsc->frozen)
  472. return -EDEADLOCK;
  473. xchk_set_corrupt(sc);
  474. return 0;
  475. }
  476. /* See if icount is obviously wrong. */
  477. if (icount < fsc->icount_min || icount > fsc->icount_max)
  478. xchk_set_corrupt(sc);
  479. /* See if fdblocks is obviously wrong. */
  480. if (fdblocks > mp->m_sb.sb_dblocks)
  481. xchk_set_corrupt(sc);
  482. /* See if frextents is obviously wrong. */
  483. if (frextents > mp->m_sb.sb_rextents)
  484. xchk_set_corrupt(sc);
  485. /*
  486. * If ifree exceeds icount by more than the minimum variance then
  487. * something's probably wrong with the counters.
  488. */
  489. if (ifree > icount && ifree - icount > XCHK_FSCOUNT_MIN_VARIANCE)
  490. xchk_set_corrupt(sc);
  491. /* Walk the incore AG headers to calculate the expected counters. */
  492. error = xchk_fscount_aggregate_agcounts(sc, fsc);
  493. if (!xchk_process_error(sc, 0, XFS_SB_BLOCK(mp), &error))
  494. return error;
  495. /* Count the free extents counter for rt volumes. */
  496. error = xchk_fscount_count_frextents(sc, fsc);
  497. if (!xchk_process_error(sc, 0, XFS_SB_BLOCK(mp), &error))
  498. return error;
  499. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_INCOMPLETE)
  500. return 0;
  501. /*
  502. * Compare the in-core counters with whatever we counted. If the fs is
  503. * frozen, we treat the discrepancy as a corruption because the freeze
  504. * should have stabilized the counter values. Otherwise, we need
  505. * userspace to call us back having granted us freeze permission.
  506. */
  507. if (!xchk_fscount_within_range(sc, icount, &mp->m_icount,
  508. fsc->icount)) {
  509. if (fsc->frozen)
  510. xchk_set_corrupt(sc);
  511. else
  512. try_again = true;
  513. }
  514. if (!xchk_fscount_within_range(sc, ifree, &mp->m_ifree, fsc->ifree)) {
  515. if (fsc->frozen)
  516. xchk_set_corrupt(sc);
  517. else
  518. try_again = true;
  519. }
  520. if (!xchk_fscount_within_range(sc, fdblocks,
  521. &mp->m_free[XC_FREE_BLOCKS].count, fsc->fdblocks)) {
  522. if (fsc->frozen)
  523. xchk_set_corrupt(sc);
  524. else
  525. try_again = true;
  526. }
  527. if (!xfs_has_zoned(mp) &&
  528. !xchk_fscount_within_range(sc, frextents,
  529. &mp->m_free[XC_FREE_RTEXTENTS].count,
  530. fsc->frextents - fsc->frextents_delayed)) {
  531. if (fsc->frozen)
  532. xchk_set_corrupt(sc);
  533. else
  534. try_again = true;
  535. }
  536. if (try_again)
  537. return -EDEADLOCK;
  538. return 0;
  539. }