Line data Source code
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_COMMON_H__
7 : #define __XFS_SCRUB_COMMON_H__
8 :
9 : /*
10 : * We /could/ terminate a scrub/repair operation early. If we're not
11 : * in a good place to continue (fatal signal, etc.) then bail out.
12 : * Note that we're careful not to make any judgements about *error.
13 : */
14 : static inline bool
15 >17671*10^7 : xchk_should_terminate(
16 : struct xfs_scrub *sc,
17 : int *error)
18 : {
19 : /*
20 : * If preemption is disabled, we need to yield to the scheduler every
21 : * few seconds so that we don't run afoul of the soft lockup watchdog
22 : * or RCU stall detector.
23 : */
24 >17671*10^7 : cond_resched();
25 :
26 >17726*10^7 : if (fatal_signal_pending(current)) {
27 175 : if (*error == 0)
28 175 : *error = -EINTR;
29 175 : return true;
30 : }
31 : return false;
32 : }
33 :
34 : int xchk_trans_alloc(struct xfs_scrub *sc, uint resblks);
35 : int xchk_trans_alloc_empty(struct xfs_scrub *sc);
36 : void xchk_trans_cancel(struct xfs_scrub *sc);
37 :
38 : bool xchk_process_error(struct xfs_scrub *sc, xfs_agnumber_t agno,
39 : xfs_agblock_t bno, int *error);
40 : bool xchk_process_rt_error(struct xfs_scrub *sc, xfs_rgnumber_t rgno,
41 : xfs_rgblock_t rgbno, int *error);
42 : bool xchk_fblock_process_error(struct xfs_scrub *sc, int whichfork,
43 : xfs_fileoff_t offset, int *error);
44 :
45 : bool xchk_xref_process_error(struct xfs_scrub *sc,
46 : xfs_agnumber_t agno, xfs_agblock_t bno, int *error);
47 : bool xchk_xref_process_rt_error(struct xfs_scrub *sc,
48 : xfs_rgnumber_t rgno, xfs_rgblock_t rgbno, int *error);
49 : bool xchk_fblock_xref_process_error(struct xfs_scrub *sc,
50 : int whichfork, xfs_fileoff_t offset, int *error);
51 :
52 : void xchk_block_set_preen(struct xfs_scrub *sc,
53 : struct xfs_buf *bp);
54 : void xchk_ino_set_preen(struct xfs_scrub *sc, xfs_ino_t ino);
55 :
56 : void xchk_set_corrupt(struct xfs_scrub *sc);
57 : void xchk_block_set_corrupt(struct xfs_scrub *sc,
58 : struct xfs_buf *bp);
59 : void xchk_ino_set_corrupt(struct xfs_scrub *sc, xfs_ino_t ino);
60 : void xchk_fblock_set_corrupt(struct xfs_scrub *sc, int whichfork,
61 : xfs_fileoff_t offset);
62 : #ifdef CONFIG_XFS_QUOTA
63 : void xchk_qcheck_set_corrupt(struct xfs_scrub *sc, unsigned int dqtype,
64 : xfs_dqid_t id);
65 : #endif /* CONFIG_XFS_QUOTA */
66 :
67 : void xchk_block_xref_set_corrupt(struct xfs_scrub *sc,
68 : struct xfs_buf *bp);
69 : void xchk_ino_xref_set_corrupt(struct xfs_scrub *sc,
70 : xfs_ino_t ino);
71 : void xchk_fblock_xref_set_corrupt(struct xfs_scrub *sc,
72 : int whichfork, xfs_fileoff_t offset);
73 :
74 : void xchk_ino_set_warning(struct xfs_scrub *sc, xfs_ino_t ino);
75 : void xchk_fblock_set_warning(struct xfs_scrub *sc, int whichfork,
76 : xfs_fileoff_t offset);
77 :
78 : void xchk_set_incomplete(struct xfs_scrub *sc);
79 : int xchk_checkpoint_log(struct xfs_mount *mp);
80 :
81 : /* Are we set up for a cross-referencing check? */
82 : bool xchk_should_check_xref(struct xfs_scrub *sc, int *error,
83 : struct xfs_btree_cur **curpp);
84 :
85 : static inline int xchk_setup_nothing(struct xfs_scrub *sc)
86 : {
87 : return -ENOENT;
88 : }
89 :
90 : /* Setup functions */
91 : int xchk_setup_agheader(struct xfs_scrub *sc);
92 : int xchk_setup_fs(struct xfs_scrub *sc);
93 : int xchk_setup_rt(struct xfs_scrub *sc);
94 : int xchk_setup_ag_allocbt(struct xfs_scrub *sc);
95 : int xchk_setup_ag_iallocbt(struct xfs_scrub *sc);
96 : int xchk_setup_ag_rmapbt(struct xfs_scrub *sc);
97 : int xchk_setup_ag_refcountbt(struct xfs_scrub *sc);
98 : int xchk_setup_inode(struct xfs_scrub *sc);
99 : int xchk_setup_inode_bmap(struct xfs_scrub *sc);
100 : int xchk_setup_inode_bmap_data(struct xfs_scrub *sc);
101 : int xchk_setup_directory(struct xfs_scrub *sc);
102 : int xchk_setup_xattr(struct xfs_scrub *sc);
103 : int xchk_setup_symlink(struct xfs_scrub *sc);
104 : int xchk_setup_parent(struct xfs_scrub *sc);
105 : int xchk_setup_dirtree(struct xfs_scrub *sc);
106 : #ifdef CONFIG_XFS_RT
107 : int xchk_setup_rtbitmap(struct xfs_scrub *sc);
108 : int xchk_setup_rtsummary(struct xfs_scrub *sc);
109 : int xchk_setup_rgsuperblock(struct xfs_scrub *sc);
110 : int xchk_setup_rgbitmap(struct xfs_scrub *sc);
111 : int xchk_setup_rtrmapbt(struct xfs_scrub *sc);
112 : int xchk_setup_rtrefcountbt(struct xfs_scrub *sc);
113 : #else
114 : # define xchk_setup_rtbitmap xchk_setup_nothing
115 : # define xchk_setup_rtsummary xchk_setup_nothing
116 : # define xchk_setup_rgsuperblock xchk_setup_nothing
117 : # define xchk_setup_rgbitmap xchk_setup_nothing
118 : # define xchk_setup_rtrmapbt xchk_setup_nothing
119 : # define xchk_setup_rtrefcountbt xchk_setup_nothing
120 : #endif
121 : #ifdef CONFIG_XFS_QUOTA
122 : int xchk_ino_dqattach(struct xfs_scrub *sc);
123 : int xchk_setup_quota(struct xfs_scrub *sc);
124 : int xchk_setup_quotacheck(struct xfs_scrub *sc);
125 : #else
126 : static inline int
127 : xchk_ino_dqattach(struct xfs_scrub *sc)
128 : {
129 : return 0;
130 : }
131 : # define xchk_setup_quota xchk_setup_nothing
132 : # define xchk_setup_quotacheck xchk_setup_nothing
133 : #endif
134 : int xchk_setup_fscounters(struct xfs_scrub *sc);
135 : int xchk_setup_nlinks(struct xfs_scrub *sc);
136 :
137 : void xchk_ag_free(struct xfs_scrub *sc, struct xchk_ag *sa);
138 : int xchk_ag_init(struct xfs_scrub *sc, xfs_agnumber_t agno,
139 : struct xchk_ag *sa);
140 : int xchk_perag_drain_and_lock(struct xfs_scrub *sc);
141 :
142 : /*
143 : * Grab all AG resources, treating the inability to grab the perag structure as
144 : * a fs corruption. This is intended for callers checking an ondisk reference
145 : * to a given AG, which means that the AG must still exist.
146 : */
147 : static inline int
148 : xchk_ag_init_existing(
149 : struct xfs_scrub *sc,
150 : xfs_agnumber_t agno,
151 : struct xchk_ag *sa)
152 : {
153 723246111 : int error = xchk_ag_init(sc, agno, sa);
154 :
155 724367963 : return error == -ENOENT ? -EFSCORRUPTED : error;
156 : }
157 :
158 : /* Lock the rt bitmap in exclusive mode */
159 : #define XCHK_RTLOCK_BITMAP (1U << 31)
160 : /* Lock the rt bitmap in shared mode */
161 : #define XCHK_RTLOCK_BITMAP_SHARED (1U << 30)
162 : /* Lock the rt summary in exclusive mode */
163 : #define XCHK_RTLOCK_SUMMARY (1U << 29)
164 : /* Lock the rt summary in shared mode */
165 : #define XCHK_RTLOCK_SUMMARY_SHARED (1U << 28)
166 :
167 : #define XCHK_RTLOCK_ALL (XCHK_RTLOCK_BITMAP | \
168 : XCHK_RTLOCK_BITMAP_SHARED | \
169 : XCHK_RTLOCK_SUMMARY | \
170 : XCHK_RTLOCK_SUMMARY_SHARED)
171 :
172 : void xchk_rt_init(struct xfs_scrub *sc, struct xchk_rt *sr,
173 : unsigned int xchk_rtlock_flags);
174 : void xchk_rt_unlock(struct xfs_scrub *sc, struct xchk_rt *sr);
175 :
176 : #ifdef CONFIG_XFS_RT
177 :
178 : /* All the locks we need to check an rtgroup. */
179 : #define XCHK_RTGLOCK_ALL (XFS_RTGLOCK_BITMAP_SHARED | \
180 : XFS_RTGLOCK_RMAP | \
181 : XFS_RTGLOCK_REFCOUNT)
182 :
183 : int xchk_rtgroup_init(struct xfs_scrub *sc, xfs_rgnumber_t rgno,
184 : struct xchk_rt *sr, unsigned int rtglock_flags);
185 : void xchk_rtgroup_unlock(struct xfs_scrub *sc, struct xchk_rt *sr);
186 : void xchk_rtgroup_btcur_free(struct xchk_rt *sr);
187 : void xchk_rtgroup_free(struct xfs_scrub *sc, struct xchk_rt *sr);
188 : int xchk_rtgroup_drain_and_lock(struct xfs_scrub *sc, struct xchk_rt *sr,
189 : unsigned int rtglock_flags);
190 : #else
191 : # define xchk_rtgroup_init(sc, rgno, sr, lockflags) (-ENOSYS)
192 : # define xchk_rtgroup_btcur_free(sr) ((void)0)
193 : # define xchk_rtgroup_free(sc, sr) ((void)0)
194 : # define xchk_rtgroup_drain_and_lock(sc, sr, lockflags) (-ENOSYS)
195 : #endif /* CONFIG_XFS_RT */
196 :
197 : int xchk_ag_read_headers(struct xfs_scrub *sc, xfs_agnumber_t agno,
198 : struct xchk_ag *sa);
199 : void xchk_ag_btcur_free(struct xchk_ag *sa);
200 : void xchk_ag_btcur_init(struct xfs_scrub *sc, struct xchk_ag *sa);
201 : int xchk_count_rmap_ownedby_ag(struct xfs_scrub *sc, struct xfs_btree_cur *cur,
202 : const struct xfs_owner_info *oinfo, xfs_filblks_t *blocks);
203 :
204 : int xchk_setup_ag_btree(struct xfs_scrub *sc, bool force_log);
205 : int xchk_iget_for_scrubbing(struct xfs_scrub *sc);
206 : int xchk_setup_inode_contents(struct xfs_scrub *sc, unsigned int resblks);
207 : int xchk_install_live_inode(struct xfs_scrub *sc, struct xfs_inode *ip);
208 :
209 : void xchk_ilock(struct xfs_scrub *sc, unsigned int ilock_flags);
210 : bool xchk_ilock_nowait(struct xfs_scrub *sc, unsigned int ilock_flags);
211 : void xchk_iunlock(struct xfs_scrub *sc, unsigned int ilock_flags);
212 :
213 : void xchk_buffer_recheck(struct xfs_scrub *sc, struct xfs_buf *bp);
214 : void xchk_whine(const struct xfs_mount *mp, const char *fmt, ...);
215 :
216 : int xchk_iget(struct xfs_scrub *sc, xfs_ino_t inum, struct xfs_inode **ipp);
217 : int xchk_iget_agi(struct xfs_scrub *sc, xfs_ino_t inum,
218 : struct xfs_buf **agi_bpp, struct xfs_inode **ipp);
219 : void xchk_irele(struct xfs_scrub *sc, struct xfs_inode *ip);
220 : int xchk_install_handle_inode(struct xfs_scrub *sc, struct xfs_inode *ip);
221 :
222 : /*
223 : * Don't bother cross-referencing if we already found corruption or cross
224 : * referencing discrepancies.
225 : */
226 : static inline bool xchk_skip_xref(struct xfs_scrub_metadata *sm)
227 : {
228 33481575901 : return sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
229 : XFS_SCRUB_OFLAG_XCORRUPT);
230 : }
231 :
232 : bool xchk_dir_looks_zapped(struct xfs_inode *dp);
233 :
234 : #ifdef CONFIG_XFS_ONLINE_REPAIR
235 : /* Decide if a repair is required. */
236 : static inline bool xchk_needs_repair(const struct xfs_scrub_metadata *sm)
237 : {
238 856519267 : return sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
239 : XFS_SCRUB_OFLAG_XCORRUPT |
240 : XFS_SCRUB_OFLAG_PREEN);
241 : }
242 :
243 : /*
244 : * "Should we prepare for a repair?"
245 : *
246 : * Return true if the caller permits us to repair metadata and we're not
247 : * setting up for a post-repair evaluation.
248 : */
249 : static inline bool xchk_could_repair(const struct xfs_scrub *sc)
250 : {
251 4071367206 : return (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) &&
252 95233324 : !(sc->flags & XREP_ALREADY_FIXED);
253 : }
254 : #else
255 : # define xchk_needs_repair(sc) (false)
256 : # define xchk_could_repair(sc) (false)
257 : #endif /* CONFIG_XFS_ONLINE_REPAIR */
258 :
259 : int xchk_metadata_inode_forks(struct xfs_scrub *sc);
260 :
261 : /*
262 : * Helper macros to allocate and format xfile description strings.
263 : * Callers must kfree the pointer returned.
264 : */
265 : #define xchk_xfile_descr(sc, fmt, ...) \
266 : kasprintf(XCHK_GFP_FLAGS, "XFS (%s): " fmt, \
267 : (sc)->mp->m_super->s_id, ##__VA_ARGS__)
268 : #define xchk_xfile_ag_descr(sc, fmt, ...) \
269 : kasprintf(XCHK_GFP_FLAGS, "XFS (%s): AG 0x%x " fmt, \
270 : (sc)->mp->m_super->s_id, \
271 : (sc)->sa.pag ? (sc)->sa.pag->pag_agno : (sc)->sm->sm_agno, \
272 : ##__VA_ARGS__)
273 : #define xchk_xfile_ino_descr(sc, fmt, ...) \
274 : kasprintf(XCHK_GFP_FLAGS, "XFS (%s): inode 0x%llx " fmt, \
275 : (sc)->mp->m_super->s_id, \
276 : (sc)->ip ? (sc)->ip->i_ino : (sc)->sm->sm_ino, \
277 : ##__VA_ARGS__)
278 : #define xchk_xfile_rtgroup_descr(sc, fmt, ...) \
279 : kasprintf(XCHK_GFP_FLAGS, "XFS (%s): rtgroup 0x%x " fmt, \
280 : (sc)->mp->m_super->s_id, \
281 : (sc)->sa.pag ? (sc)->sr.rtg->rtg_rgno : (sc)->sm->sm_agno, \
282 : ##__VA_ARGS__)
283 :
284 : /*
285 : * Setting up a hook to wait for intents to drain is costly -- we have to take
286 : * the CPU hotplug lock and force an i-cache flush on all CPUs once to set it
287 : * up, and again to tear it down. These costs add up quickly, so we only want
288 : * to enable the drain waiter if the drain actually detected a conflict with
289 : * running intent chains.
290 : */
291 : static inline bool xchk_need_intent_drain(struct xfs_scrub *sc)
292 : {
293 548428193 : return sc->flags & XCHK_NEED_DRAIN;
294 : }
295 :
296 : void xchk_fsgates_enable(struct xfs_scrub *sc, unsigned int scrub_fshooks);
297 :
298 : int xchk_inode_is_allocated(struct xfs_scrub *sc, xfs_agino_t agino,
299 : bool *inuse);
300 : int xchk_inode_count_blocks(struct xfs_scrub *sc, int whichfork,
301 : xfs_extnum_t *nextents, xfs_filblks_t *count);
302 :
303 : #endif /* __XFS_SCRUB_COMMON_H__ */
|