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 int xchk_trans_alloc(struct xfs_scrub *sc, uint resblks);
10 void xchk_trans_alloc_empty(struct xfs_scrub *sc);
11 void xchk_trans_cancel(struct xfs_scrub *sc);
12
13 bool xchk_process_error(struct xfs_scrub *sc, xfs_agnumber_t agno,
14 xfs_agblock_t bno, int *error);
15 bool xchk_process_rt_error(struct xfs_scrub *sc, xfs_rgnumber_t rgno,
16 xfs_rgblock_t rgbno, int *error);
17 bool xchk_fblock_process_error(struct xfs_scrub *sc, int whichfork,
18 xfs_fileoff_t offset, int *error);
19
20 bool xchk_xref_process_error(struct xfs_scrub *sc,
21 xfs_agnumber_t agno, xfs_agblock_t bno, int *error);
22 bool xchk_fblock_xref_process_error(struct xfs_scrub *sc,
23 int whichfork, xfs_fileoff_t offset, int *error);
24
25 void xchk_block_set_preen(struct xfs_scrub *sc,
26 struct xfs_buf *bp);
27 void xchk_ino_set_preen(struct xfs_scrub *sc, xfs_ino_t ino);
28 void xchk_fblock_set_preen(struct xfs_scrub *sc,
29 int whichfork, xfs_fileoff_t offset);
30
31 void xchk_set_corrupt(struct xfs_scrub *sc);
32 void xchk_block_set_corrupt(struct xfs_scrub *sc,
33 struct xfs_buf *bp);
34 void xchk_ino_set_corrupt(struct xfs_scrub *sc, xfs_ino_t ino);
35 #define xchk_ip_set_corrupt(_sc, _ip) \
36 xchk_ino_set_corrupt((_sc), I_INO(_ip))
37 void xchk_fblock_set_corrupt(struct xfs_scrub *sc, int whichfork,
38 xfs_fileoff_t offset);
39 #ifdef CONFIG_XFS_QUOTA
40 void xchk_qcheck_set_corrupt(struct xfs_scrub *sc, unsigned int dqtype,
41 xfs_dqid_t id);
42 #endif
43
44 void xchk_block_xref_set_corrupt(struct xfs_scrub *sc,
45 struct xfs_buf *bp);
46 void xchk_ip_xref_set_corrupt(struct xfs_scrub *sc,
47 struct xfs_inode *ip);
48 void xchk_fblock_xref_set_corrupt(struct xfs_scrub *sc,
49 int whichfork, xfs_fileoff_t offset);
50
51 void xchk_ino_set_warning(struct xfs_scrub *sc, xfs_ino_t ino);
52 void xchk_fblock_set_warning(struct xfs_scrub *sc, int whichfork,
53 xfs_fileoff_t offset);
54
55 void xchk_set_incomplete(struct xfs_scrub *sc);
56 int xchk_checkpoint_log(struct xfs_mount *mp);
57
58 /* Are we set up for a cross-referencing check? */
59 bool xchk_should_check_xref(struct xfs_scrub *sc, int *error,
60 struct xfs_btree_cur **curpp);
61
xchk_setup_nothing(struct xfs_scrub * sc)62 static inline int xchk_setup_nothing(struct xfs_scrub *sc)
63 {
64 return -ENOENT;
65 }
66
67 /* Setup functions */
68 int xchk_setup_agheader(struct xfs_scrub *sc);
69 int xchk_setup_fs(struct xfs_scrub *sc);
70 int xchk_setup_rt(struct xfs_scrub *sc);
71 int xchk_setup_ag_allocbt(struct xfs_scrub *sc);
72 int xchk_setup_ag_iallocbt(struct xfs_scrub *sc);
73 int xchk_setup_ag_rmapbt(struct xfs_scrub *sc);
74 int xchk_setup_ag_refcountbt(struct xfs_scrub *sc);
75 int xchk_setup_inode(struct xfs_scrub *sc);
76 int xchk_setup_inode_bmap(struct xfs_scrub *sc);
77 int xchk_setup_directory(struct xfs_scrub *sc);
78 int xchk_setup_xattr(struct xfs_scrub *sc);
79 int xchk_setup_symlink(struct xfs_scrub *sc);
80 int xchk_setup_parent(struct xfs_scrub *sc);
81 int xchk_setup_dirtree(struct xfs_scrub *sc);
82 int xchk_setup_metapath(struct xfs_scrub *sc);
83 #ifdef CONFIG_XFS_RT
84 int xchk_setup_rtbitmap(struct xfs_scrub *sc);
85 int xchk_setup_rtsummary(struct xfs_scrub *sc);
86 int xchk_setup_rgsuperblock(struct xfs_scrub *sc);
87 int xchk_setup_rtrmapbt(struct xfs_scrub *sc);
88 int xchk_setup_rtrefcountbt(struct xfs_scrub *sc);
89 #else
90 # define xchk_setup_rtbitmap xchk_setup_nothing
91 # define xchk_setup_rtsummary xchk_setup_nothing
92 # define xchk_setup_rgsuperblock xchk_setup_nothing
93 # define xchk_setup_rtrmapbt xchk_setup_nothing
94 # define xchk_setup_rtrefcountbt xchk_setup_nothing
95 #endif
96 #ifdef CONFIG_XFS_QUOTA
97 int xchk_ino_dqattach(struct xfs_scrub *sc);
98 int xchk_setup_quota(struct xfs_scrub *sc);
99 int xchk_setup_quotacheck(struct xfs_scrub *sc);
100 #else
101 static inline int
xchk_ino_dqattach(struct xfs_scrub * sc)102 xchk_ino_dqattach(struct xfs_scrub *sc)
103 {
104 return 0;
105 }
106 # define xchk_setup_quota xchk_setup_nothing
107 # define xchk_setup_quotacheck xchk_setup_nothing
108 #endif
109 int xchk_setup_fscounters(struct xfs_scrub *sc);
110 int xchk_setup_nlinks(struct xfs_scrub *sc);
111
112 void xchk_ag_free(struct xfs_scrub *sc, struct xchk_ag *sa);
113 int xchk_ag_init(struct xfs_scrub *sc, xfs_agnumber_t agno,
114 struct xchk_ag *sa);
115 int xchk_perag_drain_and_lock(struct xfs_scrub *sc);
116
117 /*
118 * Grab all AG resources, treating the inability to grab the perag structure as
119 * a fs corruption. This is intended for callers checking an ondisk reference
120 * to a given AG, which means that the AG must still exist.
121 */
122 static inline int
xchk_ag_init_existing(struct xfs_scrub * sc,xfs_agnumber_t agno,struct xchk_ag * sa)123 xchk_ag_init_existing(
124 struct xfs_scrub *sc,
125 xfs_agnumber_t agno,
126 struct xchk_ag *sa)
127 {
128 int error = xchk_ag_init(sc, agno, sa);
129
130 return error == -ENOENT ? -EFSCORRUPTED : error;
131 }
132
133 #ifdef CONFIG_XFS_RT
134
135 /* All the locks we need to check an rtgroup. */
136 #define XCHK_RTGLOCK_ALL (XFS_RTGLOCK_BITMAP | \
137 XFS_RTGLOCK_RMAP | \
138 XFS_RTGLOCK_REFCOUNT)
139
140 int xchk_rtgroup_init(struct xfs_scrub *sc, xfs_rgnumber_t rgno,
141 struct xchk_rt *sr);
142
143 static inline int
xchk_rtgroup_init_existing(struct xfs_scrub * sc,xfs_rgnumber_t rgno,struct xchk_rt * sr)144 xchk_rtgroup_init_existing(
145 struct xfs_scrub *sc,
146 xfs_rgnumber_t rgno,
147 struct xchk_rt *sr)
148 {
149 int error = xchk_rtgroup_init(sc, rgno, sr);
150
151 return error == -ENOENT ? -EFSCORRUPTED : error;
152 }
153
154 int xchk_rtgroup_lock(struct xfs_scrub *sc, struct xchk_rt *sr,
155 unsigned int rtglock_flags);
156 void xchk_rtgroup_unlock(struct xchk_rt *sr);
157 void xchk_rtgroup_btcur_free(struct xchk_rt *sr);
158 void xchk_rtgroup_free(struct xfs_scrub *sc, struct xchk_rt *sr);
159 #else
160 # define xchk_rtgroup_init(sc, rgno, sr) (-EFSCORRUPTED)
161 # define xchk_rtgroup_init_existing(sc, rgno, sr) (-EFSCORRUPTED)
162 # define xchk_rtgroup_lock(sc, sr, lockflags) (-EFSCORRUPTED)
163 # define xchk_rtgroup_unlock(sr) do { } while (0)
164 # define xchk_rtgroup_btcur_free(sr) do { } while (0)
165 # define xchk_rtgroup_free(sc, sr) do { } while (0)
166 #endif /* CONFIG_XFS_RT */
167
168 int xchk_ag_read_headers(struct xfs_scrub *sc, xfs_agnumber_t agno,
169 struct xchk_ag *sa);
170 void xchk_ag_btcur_free(struct xchk_ag *sa);
171 void xchk_ag_btcur_init(struct xfs_scrub *sc, struct xchk_ag *sa);
172 int xchk_count_rmap_ownedby_ag(struct xfs_scrub *sc, struct xfs_btree_cur *cur,
173 const struct xfs_owner_info *oinfo, xfs_filblks_t *blocks);
174
175 int xchk_setup_ag_btree(struct xfs_scrub *sc, bool force_log);
176 int xchk_iget_for_scrubbing(struct xfs_scrub *sc);
177 int xchk_setup_inode_contents(struct xfs_scrub *sc, unsigned int resblks);
178 int xchk_install_live_inode(struct xfs_scrub *sc, struct xfs_inode *ip);
179
180 void xchk_ilock(struct xfs_scrub *sc, unsigned int ilock_flags);
181 bool xchk_ilock_nowait(struct xfs_scrub *sc, unsigned int ilock_flags);
182 void xchk_iunlock(struct xfs_scrub *sc, unsigned int ilock_flags);
183
184 void xchk_buffer_recheck(struct xfs_scrub *sc, struct xfs_buf *bp);
185
186 /*
187 * Grab the inode at @inum. The caller must have created a scrub transaction
188 * so that we can confirm the inumber by walking the inobt and not deadlock on
189 * a loop in the inobt.
190 */
191 int xchk_iget(struct xfs_scrub *sc, xfs_ino_t inum, struct xfs_inode **ipp);
192 int xchk_iget_agi(struct xfs_scrub *sc, xfs_ino_t inum,
193 struct xfs_buf **agi_bpp, struct xfs_inode **ipp);
194 void xchk_irele(struct xfs_scrub *sc, struct xfs_inode *ip);
195 int xchk_install_handle_inode(struct xfs_scrub *sc, struct xfs_inode *ip);
196
197 /*
198 * Safe version of (untrusted) xchk_iget that uses an empty transaction to
199 * avoid deadlocking on loops in the inobt. This should only be used in a
200 * scrub or repair setup routine, and only prior to grabbing a transaction.
201 */
202 static inline int
xchk_iget_safe(struct xfs_scrub * sc,xfs_ino_t inum,struct xfs_inode ** ipp)203 xchk_iget_safe(struct xfs_scrub *sc, xfs_ino_t inum, struct xfs_inode **ipp)
204 {
205 int error;
206
207 ASSERT(sc->tp == NULL);
208
209 error = xchk_trans_alloc(sc, 0);
210 if (error)
211 return error;
212 error = xchk_iget(sc, inum, ipp);
213 xchk_trans_cancel(sc);
214 return error;
215 }
216
217 /*
218 * Don't bother cross-referencing if we already found corruption or cross
219 * referencing discrepancies.
220 */
xchk_skip_xref(struct xfs_scrub_metadata * sm)221 static inline bool xchk_skip_xref(struct xfs_scrub_metadata *sm)
222 {
223 return sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
224 XFS_SCRUB_OFLAG_XCORRUPT);
225 }
226
227 bool xchk_dir_looks_zapped(struct xfs_inode *dp);
228 bool xchk_pptr_looks_zapped(struct xfs_inode *ip);
229
230 /* Decide if a repair is required. */
xchk_needs_repair(const struct xfs_scrub_metadata * sm)231 static inline bool xchk_needs_repair(const struct xfs_scrub_metadata *sm)
232 {
233 return sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
234 XFS_SCRUB_OFLAG_XCORRUPT |
235 XFS_SCRUB_OFLAG_PREEN);
236 }
237
238 /*
239 * "Should we prepare for a repair?"
240 *
241 * Return true if the caller permits us to repair metadata and we're not
242 * setting up for a post-repair evaluation.
243 */
xchk_could_repair(const struct xfs_scrub * sc)244 static inline bool xchk_could_repair(const struct xfs_scrub *sc)
245 {
246 return (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) &&
247 !(sc->flags & XREP_ALREADY_FIXED);
248 }
249
250 int xchk_metadata_inode_forks(struct xfs_scrub *sc);
251
252 /*
253 * Setting up a hook to wait for intents to drain is costly -- we have to take
254 * the CPU hotplug lock and force an i-cache flush on all CPUs once to set it
255 * up, and again to tear it down. These costs add up quickly, so we only want
256 * to enable the drain waiter if the drain actually detected a conflict with
257 * running intent chains.
258 */
xchk_need_intent_drain(struct xfs_scrub * sc)259 static inline bool xchk_need_intent_drain(struct xfs_scrub *sc)
260 {
261 return sc->flags & XCHK_NEED_DRAIN;
262 }
263
264 void xchk_fsgates_enable(struct xfs_scrub *sc, unsigned int scrub_fshooks);
265
266 int xchk_inode_is_allocated(struct xfs_scrub *sc, xfs_agino_t agino,
267 bool *inuse);
268 int xchk_inode_count_blocks(struct xfs_scrub *sc, int whichfork,
269 xfs_extnum_t *nextents, xfs_filblks_t *count);
270
271 bool xchk_inode_is_dirtree_root(const struct xfs_inode *ip);
272 bool xchk_inode_is_sb_rooted(const struct xfs_inode *ip);
273 xfs_ino_t xchk_inode_rootdir_inum(const struct xfs_inode *ip);
274
275 #endif /* __XFS_SCRUB_COMMON_H__ */
276