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