scrub.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. #ifndef __XFS_SCRUB_SCRUB_H__
  7. #define __XFS_SCRUB_SCRUB_H__
  8. struct xfs_scrub;
  9. struct xchk_relax {
  10. unsigned long next_resched;
  11. unsigned int resched_nr;
  12. bool interruptible;
  13. };
  14. /* Yield to the scheduler at most 10x per second. */
  15. #define XCHK_RELAX_NEXT (jiffies + (HZ / 10))
  16. #define INIT_XCHK_RELAX \
  17. (struct xchk_relax){ \
  18. .next_resched = XCHK_RELAX_NEXT, \
  19. .resched_nr = 0, \
  20. .interruptible = true, \
  21. }
  22. /*
  23. * Relax during a scrub operation and exit if there's a fatal signal pending.
  24. *
  25. * If preemption is disabled, we need to yield to the scheduler every now and
  26. * then so that we don't run afoul of the soft lockup watchdog or RCU stall
  27. * detector. cond_resched calls are somewhat expensive (~5ns) so we want to
  28. * ratelimit this to 10x per second. Amortize the cost of the other checks by
  29. * only doing it once every 100 calls.
  30. */
  31. static inline int xchk_maybe_relax(struct xchk_relax *widget)
  32. {
  33. /* Amortize the cost of scheduling and checking signals. */
  34. if (likely(++widget->resched_nr < 100))
  35. return 0;
  36. widget->resched_nr = 0;
  37. if (unlikely(widget->next_resched <= jiffies)) {
  38. cond_resched();
  39. widget->next_resched = XCHK_RELAX_NEXT;
  40. }
  41. if (widget->interruptible && fatal_signal_pending(current))
  42. return -EINTR;
  43. return 0;
  44. }
  45. /*
  46. * Standard flags for allocating memory within scrub. NOFS context is
  47. * configured by the process allocation scope. Scrub and repair must be able
  48. * to back out gracefully if there isn't enough memory. Force-cast to avoid
  49. * complaints from static checkers.
  50. */
  51. #define XCHK_GFP_FLAGS ((__force gfp_t)(GFP_KERNEL | __GFP_NOWARN | \
  52. __GFP_RETRY_MAYFAIL))
  53. /*
  54. * For opening files by handle for fsck operations, we don't trust the inumber
  55. * or the allocation state; therefore, perform an untrusted lookup. We don't
  56. * want these inodes to pollute the cache, so mark them for immediate removal.
  57. */
  58. #define XCHK_IGET_FLAGS (XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE)
  59. /* Type info and names for the scrub types. */
  60. enum xchk_type {
  61. ST_NONE = 1, /* disabled */
  62. ST_PERAG, /* per-AG metadata */
  63. ST_FS, /* per-FS metadata */
  64. ST_INODE, /* per-inode metadata */
  65. };
  66. struct xchk_meta_ops {
  67. /* Acquire whatever resources are needed for the operation. */
  68. int (*setup)(struct xfs_scrub *sc);
  69. /* Examine metadata for errors. */
  70. int (*scrub)(struct xfs_scrub *);
  71. /* Repair or optimize the metadata. */
  72. int (*repair)(struct xfs_scrub *);
  73. /*
  74. * Re-scrub the metadata we repaired, in case there's extra work that
  75. * we need to do to check our repair work. If this is NULL, we'll use
  76. * the ->scrub function pointer, assuming that the regular scrub is
  77. * sufficient.
  78. */
  79. int (*repair_eval)(struct xfs_scrub *sc);
  80. /* Decide if we even have this piece of metadata. */
  81. bool (*has)(struct xfs_mount *);
  82. /* type describing required/allowed inputs */
  83. enum xchk_type type;
  84. };
  85. /* Buffer pointers and btree cursors for an entire AG. */
  86. struct xchk_ag {
  87. struct xfs_perag *pag;
  88. /* AG btree roots */
  89. struct xfs_buf *agf_bp;
  90. struct xfs_buf *agi_bp;
  91. /* AG btrees */
  92. struct xfs_btree_cur *bno_cur;
  93. struct xfs_btree_cur *cnt_cur;
  94. struct xfs_btree_cur *ino_cur;
  95. struct xfs_btree_cur *fino_cur;
  96. struct xfs_btree_cur *rmap_cur;
  97. struct xfs_btree_cur *refc_cur;
  98. };
  99. struct xfs_scrub {
  100. /* General scrub state. */
  101. struct xfs_mount *mp;
  102. struct xfs_scrub_metadata *sm;
  103. const struct xchk_meta_ops *ops;
  104. struct xfs_trans *tp;
  105. /* File that scrub was called with. */
  106. struct file *file;
  107. /*
  108. * File that is undergoing the scrub operation. This can differ from
  109. * the file that scrub was called with if we're checking file-based fs
  110. * metadata (e.g. rt bitmaps) or if we're doing a scrub-by-handle for
  111. * something that can't be opened directly (e.g. symlinks).
  112. */
  113. struct xfs_inode *ip;
  114. /* Kernel memory buffer used by scrubbers; freed at teardown. */
  115. void *buf;
  116. /*
  117. * Clean up resources owned by whatever is in the buffer. Cleanup can
  118. * be deferred with this hook as a means for scrub functions to pass
  119. * data to repair functions. This function must not free the buffer
  120. * itself.
  121. */
  122. void (*buf_cleanup)(void *buf);
  123. /* xfile used by the scrubbers; freed at teardown. */
  124. struct xfile *xfile;
  125. /* buffer target for in-memory btrees; also freed at teardown. */
  126. struct xfs_buftarg *xmbtp;
  127. /* Lock flags for @ip. */
  128. uint ilock_flags;
  129. /* The orphanage, for stashing files that have lost their parent. */
  130. uint orphanage_ilock_flags;
  131. struct xfs_inode *orphanage;
  132. /* A temporary file on this filesystem, for staging new metadata. */
  133. struct xfs_inode *tempip;
  134. uint temp_ilock_flags;
  135. /* See the XCHK/XREP state flags below. */
  136. unsigned int flags;
  137. /*
  138. * The XFS_SICK_* flags that correspond to the metadata being scrubbed
  139. * or repaired. We will use this mask to update the in-core fs health
  140. * status with whatever we find.
  141. */
  142. unsigned int sick_mask;
  143. /* next time we want to cond_resched() */
  144. struct xchk_relax relax;
  145. /* State tracking for single-AG operations. */
  146. struct xchk_ag sa;
  147. };
  148. /* XCHK state flags grow up from zero, XREP state flags grown down from 2^31 */
  149. #define XCHK_TRY_HARDER (1U << 0) /* can't get resources, try again */
  150. #define XCHK_HAVE_FREEZE_PROT (1U << 1) /* do we have freeze protection? */
  151. #define XCHK_FSGATES_DRAIN (1U << 2) /* defer ops draining enabled */
  152. #define XCHK_NEED_DRAIN (1U << 3) /* scrub needs to drain defer ops */
  153. #define XCHK_FSGATES_QUOTA (1U << 4) /* quota live update enabled */
  154. #define XCHK_FSGATES_DIRENTS (1U << 5) /* directory live update enabled */
  155. #define XCHK_FSGATES_RMAP (1U << 6) /* rmapbt live update enabled */
  156. #define XREP_RESET_PERAG_RESV (1U << 30) /* must reset AG space reservation */
  157. #define XREP_ALREADY_FIXED (1U << 31) /* checking our repair work */
  158. /*
  159. * The XCHK_FSGATES* flags reflect functionality in the main filesystem that
  160. * are only enabled for this particular online fsck. When not in use, the
  161. * features are gated off via dynamic code patching, which is why the state
  162. * must be enabled during scrub setup and can only be torn down afterwards.
  163. */
  164. #define XCHK_FSGATES_ALL (XCHK_FSGATES_DRAIN | \
  165. XCHK_FSGATES_QUOTA | \
  166. XCHK_FSGATES_DIRENTS | \
  167. XCHK_FSGATES_RMAP)
  168. struct xfs_scrub_subord {
  169. struct xfs_scrub sc;
  170. struct xfs_scrub *parent_sc;
  171. unsigned int old_smtype;
  172. unsigned int old_smflags;
  173. };
  174. struct xfs_scrub_subord *xchk_scrub_create_subord(struct xfs_scrub *sc,
  175. unsigned int subtype);
  176. void xchk_scrub_free_subord(struct xfs_scrub_subord *sub);
  177. /*
  178. * We /could/ terminate a scrub/repair operation early. If we're not
  179. * in a good place to continue (fatal signal, etc.) then bail out.
  180. * Note that we're careful not to make any judgements about *error.
  181. */
  182. static inline bool
  183. xchk_should_terminate(
  184. struct xfs_scrub *sc,
  185. int *error)
  186. {
  187. if (xchk_maybe_relax(&sc->relax)) {
  188. if (*error == 0)
  189. *error = -EINTR;
  190. return true;
  191. }
  192. return false;
  193. }
  194. static inline int xchk_nothing(struct xfs_scrub *sc)
  195. {
  196. return -ENOENT;
  197. }
  198. /* Metadata scrubbers */
  199. int xchk_tester(struct xfs_scrub *sc);
  200. int xchk_superblock(struct xfs_scrub *sc);
  201. int xchk_agf(struct xfs_scrub *sc);
  202. int xchk_agfl(struct xfs_scrub *sc);
  203. int xchk_agi(struct xfs_scrub *sc);
  204. int xchk_allocbt(struct xfs_scrub *sc);
  205. int xchk_iallocbt(struct xfs_scrub *sc);
  206. int xchk_rmapbt(struct xfs_scrub *sc);
  207. int xchk_refcountbt(struct xfs_scrub *sc);
  208. int xchk_inode(struct xfs_scrub *sc);
  209. int xchk_bmap_data(struct xfs_scrub *sc);
  210. int xchk_bmap_attr(struct xfs_scrub *sc);
  211. int xchk_bmap_cow(struct xfs_scrub *sc);
  212. int xchk_directory(struct xfs_scrub *sc);
  213. int xchk_xattr(struct xfs_scrub *sc);
  214. int xchk_symlink(struct xfs_scrub *sc);
  215. int xchk_parent(struct xfs_scrub *sc);
  216. int xchk_dirtree(struct xfs_scrub *sc);
  217. #ifdef CONFIG_XFS_RT
  218. int xchk_rtbitmap(struct xfs_scrub *sc);
  219. int xchk_rtsummary(struct xfs_scrub *sc);
  220. #else
  221. # define xchk_rtbitmap xchk_nothing
  222. # define xchk_rtsummary xchk_nothing
  223. #endif
  224. #ifdef CONFIG_XFS_QUOTA
  225. int xchk_quota(struct xfs_scrub *sc);
  226. int xchk_quotacheck(struct xfs_scrub *sc);
  227. #else
  228. # define xchk_quota xchk_nothing
  229. # define xchk_quotacheck xchk_nothing
  230. #endif
  231. int xchk_fscounters(struct xfs_scrub *sc);
  232. int xchk_nlinks(struct xfs_scrub *sc);
  233. /* cross-referencing helpers */
  234. void xchk_xref_is_used_space(struct xfs_scrub *sc, xfs_agblock_t agbno,
  235. xfs_extlen_t len);
  236. void xchk_xref_is_not_inode_chunk(struct xfs_scrub *sc, xfs_agblock_t agbno,
  237. xfs_extlen_t len);
  238. void xchk_xref_is_inode_chunk(struct xfs_scrub *sc, xfs_agblock_t agbno,
  239. xfs_extlen_t len);
  240. void xchk_xref_is_only_owned_by(struct xfs_scrub *sc, xfs_agblock_t agbno,
  241. xfs_extlen_t len, const struct xfs_owner_info *oinfo);
  242. void xchk_xref_is_not_owned_by(struct xfs_scrub *sc, xfs_agblock_t agbno,
  243. xfs_extlen_t len, const struct xfs_owner_info *oinfo);
  244. void xchk_xref_has_no_owner(struct xfs_scrub *sc, xfs_agblock_t agbno,
  245. xfs_extlen_t len);
  246. void xchk_xref_is_cow_staging(struct xfs_scrub *sc, xfs_agblock_t bno,
  247. xfs_extlen_t len);
  248. void xchk_xref_is_not_shared(struct xfs_scrub *sc, xfs_agblock_t bno,
  249. xfs_extlen_t len);
  250. void xchk_xref_is_not_cow_staging(struct xfs_scrub *sc, xfs_agblock_t bno,
  251. xfs_extlen_t len);
  252. #ifdef CONFIG_XFS_RT
  253. void xchk_xref_is_used_rt_space(struct xfs_scrub *sc, xfs_rtblock_t rtbno,
  254. xfs_extlen_t len);
  255. #else
  256. # define xchk_xref_is_used_rt_space(sc, rtbno, len) do { } while (0)
  257. #endif
  258. #endif /* __XFS_SCRUB_SCRUB_H__ */