scrub.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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_icache.h"
  20. #include "xfs_itable.h"
  21. #include "xfs_alloc.h"
  22. #include "xfs_alloc_btree.h"
  23. #include "xfs_bmap.h"
  24. #include "xfs_bmap_btree.h"
  25. #include "xfs_ialloc.h"
  26. #include "xfs_ialloc_btree.h"
  27. #include "xfs_refcount.h"
  28. #include "xfs_refcount_btree.h"
  29. #include "xfs_rmap.h"
  30. #include "xfs_rmap_btree.h"
  31. #include "xfs_quota.h"
  32. #include "xfs_qm.h"
  33. #include "xfs_errortag.h"
  34. #include "xfs_error.h"
  35. #include "xfs_log.h"
  36. #include "xfs_trans_priv.h"
  37. #include "scrub/xfs_scrub.h"
  38. #include "scrub/scrub.h"
  39. #include "scrub/common.h"
  40. #include "scrub/trace.h"
  41. #include "scrub/btree.h"
  42. #include "scrub/repair.h"
  43. /*
  44. * Online Scrub and Repair
  45. *
  46. * Traditionally, XFS (the kernel driver) did not know how to check or
  47. * repair on-disk data structures. That task was left to the xfs_check
  48. * and xfs_repair tools, both of which require taking the filesystem
  49. * offline for a thorough but time consuming examination. Online
  50. * scrub & repair, on the other hand, enables us to check the metadata
  51. * for obvious errors while carefully stepping around the filesystem's
  52. * ongoing operations, locking rules, etc.
  53. *
  54. * Given that most XFS metadata consist of records stored in a btree,
  55. * most of the checking functions iterate the btree blocks themselves
  56. * looking for irregularities. When a record block is encountered, each
  57. * record can be checked for obviously bad values. Record values can
  58. * also be cross-referenced against other btrees to look for potential
  59. * misunderstandings between pieces of metadata.
  60. *
  61. * It is expected that the checkers responsible for per-AG metadata
  62. * structures will lock the AG headers (AGI, AGF, AGFL), iterate the
  63. * metadata structure, and perform any relevant cross-referencing before
  64. * unlocking the AG and returning the results to userspace. These
  65. * scrubbers must not keep an AG locked for too long to avoid tying up
  66. * the block and inode allocators.
  67. *
  68. * Block maps and b-trees rooted in an inode present a special challenge
  69. * because they can involve extents from any AG. The general scrubber
  70. * structure of lock -> check -> xref -> unlock still holds, but AG
  71. * locking order rules /must/ be obeyed to avoid deadlocks. The
  72. * ordering rule, of course, is that we must lock in increasing AG
  73. * order. Helper functions are provided to track which AG headers we've
  74. * already locked. If we detect an imminent locking order violation, we
  75. * can signal a potential deadlock, in which case the scrubber can jump
  76. * out to the top level, lock all the AGs in order, and retry the scrub.
  77. *
  78. * For file data (directories, extended attributes, symlinks) scrub, we
  79. * can simply lock the inode and walk the data. For btree data
  80. * (directories and attributes) we follow the same btree-scrubbing
  81. * strategy outlined previously to check the records.
  82. *
  83. * We use a bit of trickery with transactions to avoid buffer deadlocks
  84. * if there is a cycle in the metadata. The basic problem is that
  85. * travelling down a btree involves locking the current buffer at each
  86. * tree level. If a pointer should somehow point back to a buffer that
  87. * we've already examined, we will deadlock due to the second buffer
  88. * locking attempt. Note however that grabbing a buffer in transaction
  89. * context links the locked buffer to the transaction. If we try to
  90. * re-grab the buffer in the context of the same transaction, we avoid
  91. * the second lock attempt and continue. Between the verifier and the
  92. * scrubber, something will notice that something is amiss and report
  93. * the corruption. Therefore, each scrubber will allocate an empty
  94. * transaction, attach buffers to it, and cancel the transaction at the
  95. * end of the scrub run. Cancelling a non-dirty transaction simply
  96. * unlocks the buffers.
  97. *
  98. * There are four pieces of data that scrub can communicate to
  99. * userspace. The first is the error code (errno), which can be used to
  100. * communicate operational errors in performing the scrub. There are
  101. * also three flags that can be set in the scrub context. If the data
  102. * structure itself is corrupt, the CORRUPT flag will be set. If
  103. * the metadata is correct but otherwise suboptimal, the PREEN flag
  104. * will be set.
  105. *
  106. * We perform secondary validation of filesystem metadata by
  107. * cross-referencing every record with all other available metadata.
  108. * For example, for block mapping extents, we verify that there are no
  109. * records in the free space and inode btrees corresponding to that
  110. * space extent and that there is a corresponding entry in the reverse
  111. * mapping btree. Inconsistent metadata is noted by setting the
  112. * XCORRUPT flag; btree query function errors are noted by setting the
  113. * XFAIL flag and deleting the cursor to prevent further attempts to
  114. * cross-reference with a defective btree.
  115. *
  116. * If a piece of metadata proves corrupt or suboptimal, the userspace
  117. * program can ask the kernel to apply some tender loving care (TLC) to
  118. * the metadata object by setting the REPAIR flag and re-calling the
  119. * scrub ioctl. "Corruption" is defined by metadata violating the
  120. * on-disk specification; operations cannot continue if the violation is
  121. * left untreated. It is possible for XFS to continue if an object is
  122. * "suboptimal", however performance may be degraded. Repairs are
  123. * usually performed by rebuilding the metadata entirely out of
  124. * redundant metadata. Optimizing, on the other hand, can sometimes be
  125. * done without rebuilding entire structures.
  126. *
  127. * Generally speaking, the repair code has the following code structure:
  128. * Lock -> scrub -> repair -> commit -> re-lock -> re-scrub -> unlock.
  129. * The first check helps us figure out if we need to rebuild or simply
  130. * optimize the structure so that the rebuild knows what to do. The
  131. * second check evaluates the completeness of the repair; that is what
  132. * is reported to userspace.
  133. *
  134. * A quick note on symbol prefixes:
  135. * - "xfs_" are general XFS symbols.
  136. * - "xchk_" are symbols related to metadata checking.
  137. * - "xrep_" are symbols related to metadata repair.
  138. * - "xfs_scrub_" are symbols that tie online fsck to the rest of XFS.
  139. */
  140. /*
  141. * Scrub probe -- userspace uses this to probe if we're willing to scrub
  142. * or repair a given mountpoint. This will be used by xfs_scrub to
  143. * probe the kernel's abilities to scrub (and repair) the metadata. We
  144. * do this by validating the ioctl inputs from userspace, preparing the
  145. * filesystem for a scrub (or a repair) operation, and immediately
  146. * returning to userspace. Userspace can use the returned errno and
  147. * structure state to decide (in broad terms) if scrub/repair are
  148. * supported by the running kernel.
  149. */
  150. static int
  151. xchk_probe(
  152. struct xfs_scrub *sc)
  153. {
  154. int error = 0;
  155. if (xchk_should_terminate(sc, &error))
  156. return error;
  157. return 0;
  158. }
  159. /* Scrub setup and teardown */
  160. /* Free all the resources and finish the transactions. */
  161. STATIC int
  162. xchk_teardown(
  163. struct xfs_scrub *sc,
  164. struct xfs_inode *ip_in,
  165. int error)
  166. {
  167. xchk_ag_free(sc, &sc->sa);
  168. if (sc->tp) {
  169. if (error == 0 && (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR))
  170. error = xfs_trans_commit(sc->tp);
  171. else
  172. xfs_trans_cancel(sc->tp);
  173. sc->tp = NULL;
  174. }
  175. if (sc->ip) {
  176. if (sc->ilock_flags)
  177. xfs_iunlock(sc->ip, sc->ilock_flags);
  178. if (sc->ip != ip_in &&
  179. !xfs_internal_inum(sc->mp, sc->ip->i_ino))
  180. xfs_irele(sc->ip);
  181. sc->ip = NULL;
  182. }
  183. if (sc->has_quotaofflock)
  184. mutex_unlock(&sc->mp->m_quotainfo->qi_quotaofflock);
  185. if (sc->buf) {
  186. kmem_free(sc->buf);
  187. sc->buf = NULL;
  188. }
  189. return error;
  190. }
  191. /* Scrubbing dispatch. */
  192. static const struct xchk_meta_ops meta_scrub_ops[] = {
  193. [XFS_SCRUB_TYPE_PROBE] = { /* ioctl presence test */
  194. .type = ST_NONE,
  195. .setup = xchk_setup_fs,
  196. .scrub = xchk_probe,
  197. .repair = xrep_probe,
  198. },
  199. [XFS_SCRUB_TYPE_SB] = { /* superblock */
  200. .type = ST_PERAG,
  201. .setup = xchk_setup_fs,
  202. .scrub = xchk_superblock,
  203. .repair = xrep_superblock,
  204. },
  205. [XFS_SCRUB_TYPE_AGF] = { /* agf */
  206. .type = ST_PERAG,
  207. .setup = xchk_setup_fs,
  208. .scrub = xchk_agf,
  209. .repair = xrep_agf,
  210. },
  211. [XFS_SCRUB_TYPE_AGFL]= { /* agfl */
  212. .type = ST_PERAG,
  213. .setup = xchk_setup_fs,
  214. .scrub = xchk_agfl,
  215. .repair = xrep_agfl,
  216. },
  217. [XFS_SCRUB_TYPE_AGI] = { /* agi */
  218. .type = ST_PERAG,
  219. .setup = xchk_setup_fs,
  220. .scrub = xchk_agi,
  221. .repair = xrep_agi,
  222. },
  223. [XFS_SCRUB_TYPE_BNOBT] = { /* bnobt */
  224. .type = ST_PERAG,
  225. .setup = xchk_setup_ag_allocbt,
  226. .scrub = xchk_bnobt,
  227. .repair = xrep_notsupported,
  228. },
  229. [XFS_SCRUB_TYPE_CNTBT] = { /* cntbt */
  230. .type = ST_PERAG,
  231. .setup = xchk_setup_ag_allocbt,
  232. .scrub = xchk_cntbt,
  233. .repair = xrep_notsupported,
  234. },
  235. [XFS_SCRUB_TYPE_INOBT] = { /* inobt */
  236. .type = ST_PERAG,
  237. .setup = xchk_setup_ag_iallocbt,
  238. .scrub = xchk_inobt,
  239. .repair = xrep_notsupported,
  240. },
  241. [XFS_SCRUB_TYPE_FINOBT] = { /* finobt */
  242. .type = ST_PERAG,
  243. .setup = xchk_setup_ag_iallocbt,
  244. .scrub = xchk_finobt,
  245. .has = xfs_sb_version_hasfinobt,
  246. .repair = xrep_notsupported,
  247. },
  248. [XFS_SCRUB_TYPE_RMAPBT] = { /* rmapbt */
  249. .type = ST_PERAG,
  250. .setup = xchk_setup_ag_rmapbt,
  251. .scrub = xchk_rmapbt,
  252. .has = xfs_sb_version_hasrmapbt,
  253. .repair = xrep_notsupported,
  254. },
  255. [XFS_SCRUB_TYPE_REFCNTBT] = { /* refcountbt */
  256. .type = ST_PERAG,
  257. .setup = xchk_setup_ag_refcountbt,
  258. .scrub = xchk_refcountbt,
  259. .has = xfs_sb_version_hasreflink,
  260. .repair = xrep_notsupported,
  261. },
  262. [XFS_SCRUB_TYPE_INODE] = { /* inode record */
  263. .type = ST_INODE,
  264. .setup = xchk_setup_inode,
  265. .scrub = xchk_inode,
  266. .repair = xrep_notsupported,
  267. },
  268. [XFS_SCRUB_TYPE_BMBTD] = { /* inode data fork */
  269. .type = ST_INODE,
  270. .setup = xchk_setup_inode_bmap,
  271. .scrub = xchk_bmap_data,
  272. .repair = xrep_notsupported,
  273. },
  274. [XFS_SCRUB_TYPE_BMBTA] = { /* inode attr fork */
  275. .type = ST_INODE,
  276. .setup = xchk_setup_inode_bmap,
  277. .scrub = xchk_bmap_attr,
  278. .repair = xrep_notsupported,
  279. },
  280. [XFS_SCRUB_TYPE_BMBTC] = { /* inode CoW fork */
  281. .type = ST_INODE,
  282. .setup = xchk_setup_inode_bmap,
  283. .scrub = xchk_bmap_cow,
  284. .repair = xrep_notsupported,
  285. },
  286. [XFS_SCRUB_TYPE_DIR] = { /* directory */
  287. .type = ST_INODE,
  288. .setup = xchk_setup_directory,
  289. .scrub = xchk_directory,
  290. .repair = xrep_notsupported,
  291. },
  292. [XFS_SCRUB_TYPE_XATTR] = { /* extended attributes */
  293. .type = ST_INODE,
  294. .setup = xchk_setup_xattr,
  295. .scrub = xchk_xattr,
  296. .repair = xrep_notsupported,
  297. },
  298. [XFS_SCRUB_TYPE_SYMLINK] = { /* symbolic link */
  299. .type = ST_INODE,
  300. .setup = xchk_setup_symlink,
  301. .scrub = xchk_symlink,
  302. .repair = xrep_notsupported,
  303. },
  304. [XFS_SCRUB_TYPE_PARENT] = { /* parent pointers */
  305. .type = ST_INODE,
  306. .setup = xchk_setup_parent,
  307. .scrub = xchk_parent,
  308. .repair = xrep_notsupported,
  309. },
  310. [XFS_SCRUB_TYPE_RTBITMAP] = { /* realtime bitmap */
  311. .type = ST_FS,
  312. .setup = xchk_setup_rt,
  313. .scrub = xchk_rtbitmap,
  314. .has = xfs_sb_version_hasrealtime,
  315. .repair = xrep_notsupported,
  316. },
  317. [XFS_SCRUB_TYPE_RTSUM] = { /* realtime summary */
  318. .type = ST_FS,
  319. .setup = xchk_setup_rt,
  320. .scrub = xchk_rtsummary,
  321. .has = xfs_sb_version_hasrealtime,
  322. .repair = xrep_notsupported,
  323. },
  324. [XFS_SCRUB_TYPE_UQUOTA] = { /* user quota */
  325. .type = ST_FS,
  326. .setup = xchk_setup_quota,
  327. .scrub = xchk_quota,
  328. .repair = xrep_notsupported,
  329. },
  330. [XFS_SCRUB_TYPE_GQUOTA] = { /* group quota */
  331. .type = ST_FS,
  332. .setup = xchk_setup_quota,
  333. .scrub = xchk_quota,
  334. .repair = xrep_notsupported,
  335. },
  336. [XFS_SCRUB_TYPE_PQUOTA] = { /* project quota */
  337. .type = ST_FS,
  338. .setup = xchk_setup_quota,
  339. .scrub = xchk_quota,
  340. .repair = xrep_notsupported,
  341. },
  342. };
  343. /* This isn't a stable feature, warn once per day. */
  344. static inline void
  345. xchk_experimental_warning(
  346. struct xfs_mount *mp)
  347. {
  348. static struct ratelimit_state scrub_warning = RATELIMIT_STATE_INIT(
  349. "xchk_warning", 86400 * HZ, 1);
  350. ratelimit_set_flags(&scrub_warning, RATELIMIT_MSG_ON_RELEASE);
  351. if (__ratelimit(&scrub_warning))
  352. xfs_alert(mp,
  353. "EXPERIMENTAL online scrub feature in use. Use at your own risk!");
  354. }
  355. static int
  356. xchk_validate_inputs(
  357. struct xfs_mount *mp,
  358. struct xfs_scrub_metadata *sm)
  359. {
  360. int error;
  361. const struct xchk_meta_ops *ops;
  362. error = -EINVAL;
  363. /* Check our inputs. */
  364. sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT;
  365. if (sm->sm_flags & ~XFS_SCRUB_FLAGS_IN)
  366. goto out;
  367. /* sm_reserved[] must be zero */
  368. if (memchr_inv(sm->sm_reserved, 0, sizeof(sm->sm_reserved)))
  369. goto out;
  370. error = -ENOENT;
  371. /* Do we know about this type of metadata? */
  372. if (sm->sm_type >= XFS_SCRUB_TYPE_NR)
  373. goto out;
  374. ops = &meta_scrub_ops[sm->sm_type];
  375. if (ops->setup == NULL || ops->scrub == NULL)
  376. goto out;
  377. /* Does this fs even support this type of metadata? */
  378. if (ops->has && !ops->has(&mp->m_sb))
  379. goto out;
  380. error = -EINVAL;
  381. /* restricting fields must be appropriate for type */
  382. switch (ops->type) {
  383. case ST_NONE:
  384. case ST_FS:
  385. if (sm->sm_ino || sm->sm_gen || sm->sm_agno)
  386. goto out;
  387. break;
  388. case ST_PERAG:
  389. if (sm->sm_ino || sm->sm_gen ||
  390. sm->sm_agno >= mp->m_sb.sb_agcount)
  391. goto out;
  392. break;
  393. case ST_INODE:
  394. if (sm->sm_agno || (sm->sm_gen && !sm->sm_ino))
  395. goto out;
  396. break;
  397. default:
  398. goto out;
  399. }
  400. error = -EOPNOTSUPP;
  401. /*
  402. * We won't scrub any filesystem that doesn't have the ability
  403. * to record unwritten extents. The option was made default in
  404. * 2003, removed from mkfs in 2007, and cannot be disabled in
  405. * v5, so if we find a filesystem without this flag it's either
  406. * really old or totally unsupported. Avoid it either way.
  407. * We also don't support v1-v3 filesystems, which aren't
  408. * mountable.
  409. */
  410. if (!xfs_sb_version_hasextflgbit(&mp->m_sb))
  411. goto out;
  412. /*
  413. * We only want to repair read-write v5+ filesystems. Defer the check
  414. * for ops->repair until after our scrub confirms that we need to
  415. * perform repairs so that we avoid failing due to not supporting
  416. * repairing an object that doesn't need repairs.
  417. */
  418. if (sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) {
  419. error = -EOPNOTSUPP;
  420. if (!xfs_sb_version_hascrc(&mp->m_sb))
  421. goto out;
  422. error = -EROFS;
  423. if (mp->m_flags & XFS_MOUNT_RDONLY)
  424. goto out;
  425. }
  426. error = 0;
  427. out:
  428. return error;
  429. }
  430. #ifdef CONFIG_XFS_ONLINE_REPAIR
  431. static inline void xchk_postmortem(struct xfs_scrub *sc)
  432. {
  433. /*
  434. * Userspace asked us to repair something, we repaired it, rescanned
  435. * it, and the rescan says it's still broken. Scream about this in
  436. * the system logs.
  437. */
  438. if ((sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) &&
  439. (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
  440. XFS_SCRUB_OFLAG_XCORRUPT)))
  441. xrep_failure(sc->mp);
  442. }
  443. #else
  444. static inline void xchk_postmortem(struct xfs_scrub *sc)
  445. {
  446. /*
  447. * Userspace asked us to scrub something, it's broken, and we have no
  448. * way of fixing it. Scream in the logs.
  449. */
  450. if (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
  451. XFS_SCRUB_OFLAG_XCORRUPT))
  452. xfs_alert_ratelimited(sc->mp,
  453. "Corruption detected during scrub.");
  454. }
  455. #endif /* CONFIG_XFS_ONLINE_REPAIR */
  456. /* Dispatch metadata scrubbing. */
  457. int
  458. xfs_scrub_metadata(
  459. struct xfs_inode *ip,
  460. struct xfs_scrub_metadata *sm)
  461. {
  462. struct xfs_scrub sc;
  463. struct xfs_mount *mp = ip->i_mount;
  464. bool try_harder = false;
  465. bool already_fixed = false;
  466. int error = 0;
  467. BUILD_BUG_ON(sizeof(meta_scrub_ops) !=
  468. (sizeof(struct xchk_meta_ops) * XFS_SCRUB_TYPE_NR));
  469. trace_xchk_start(ip, sm, error);
  470. /* Forbidden if we are shut down or mounted norecovery. */
  471. error = -ESHUTDOWN;
  472. if (XFS_FORCED_SHUTDOWN(mp))
  473. goto out;
  474. error = -ENOTRECOVERABLE;
  475. if (mp->m_flags & XFS_MOUNT_NORECOVERY)
  476. goto out;
  477. error = xchk_validate_inputs(mp, sm);
  478. if (error)
  479. goto out;
  480. xchk_experimental_warning(mp);
  481. retry_op:
  482. /* Set up for the operation. */
  483. memset(&sc, 0, sizeof(sc));
  484. sc.mp = ip->i_mount;
  485. sc.sm = sm;
  486. sc.ops = &meta_scrub_ops[sm->sm_type];
  487. sc.try_harder = try_harder;
  488. sc.sa.agno = NULLAGNUMBER;
  489. error = sc.ops->setup(&sc, ip);
  490. if (error)
  491. goto out_teardown;
  492. /* Scrub for errors. */
  493. error = sc.ops->scrub(&sc);
  494. if (!try_harder && error == -EDEADLOCK) {
  495. /*
  496. * Scrubbers return -EDEADLOCK to mean 'try harder'.
  497. * Tear down everything we hold, then set up again with
  498. * preparation for worst-case scenarios.
  499. */
  500. error = xchk_teardown(&sc, ip, 0);
  501. if (error)
  502. goto out;
  503. try_harder = true;
  504. goto retry_op;
  505. } else if (error)
  506. goto out_teardown;
  507. if ((sc.sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) && !already_fixed) {
  508. bool needs_fix;
  509. /* Let debug users force us into the repair routines. */
  510. if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_FORCE_SCRUB_REPAIR))
  511. sc.sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
  512. needs_fix = (sc.sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
  513. XFS_SCRUB_OFLAG_XCORRUPT |
  514. XFS_SCRUB_OFLAG_PREEN));
  515. /*
  516. * If userspace asked for a repair but it wasn't necessary,
  517. * report that back to userspace.
  518. */
  519. if (!needs_fix) {
  520. sc.sm->sm_flags |= XFS_SCRUB_OFLAG_NO_REPAIR_NEEDED;
  521. goto out_nofix;
  522. }
  523. /*
  524. * If it's broken, userspace wants us to fix it, and we haven't
  525. * already tried to fix it, then attempt a repair.
  526. */
  527. error = xrep_attempt(ip, &sc, &already_fixed);
  528. if (error == -EAGAIN) {
  529. if (sc.try_harder)
  530. try_harder = true;
  531. error = xchk_teardown(&sc, ip, 0);
  532. if (error) {
  533. xrep_failure(mp);
  534. goto out;
  535. }
  536. goto retry_op;
  537. }
  538. }
  539. out_nofix:
  540. xchk_postmortem(&sc);
  541. out_teardown:
  542. error = xchk_teardown(&sc, ip, error);
  543. out:
  544. trace_xchk_done(ip, sm, error);
  545. if (error == -EFSCORRUPTED || error == -EFSBADCRC) {
  546. sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
  547. error = 0;
  548. }
  549. return error;
  550. }