xref: /linux/fs/xfs/scrub/repair.h (revision 770b7eec04c986ace0e632527ee7e1fafc2e5964)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2018-2023 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <djwong@kernel.org>
5  */
6 #ifndef __XFS_SCRUB_REPAIR_H__
7 #define __XFS_SCRUB_REPAIR_H__
8 
9 #include "xfs_quota_defs.h"
10 
11 struct xfs_rtgroup;
12 struct xchk_stats_run;
13 
xrep_notsupported(struct xfs_scrub * sc)14 static inline int xrep_notsupported(struct xfs_scrub *sc)
15 {
16 	return -EOPNOTSUPP;
17 }
18 
19 #ifdef CONFIG_XFS_ONLINE_REPAIR
20 
21 /*
22  * This is the maximum number of deferred extent freeing item extents (EFIs)
23  * that we'll attach to a transaction without rolling the transaction to avoid
24  * overrunning a tr_itruncate reservation.
25  */
26 #define XREP_MAX_ITRUNCATE_EFIS	(128)
27 
28 
29 /* Repair helpers */
30 
31 int xrep_attempt(struct xfs_scrub *sc, struct xchk_stats_run *run);
32 bool xrep_will_attempt(struct xfs_scrub *sc);
33 void xrep_failure(struct xfs_mount *mp);
34 int xrep_roll_ag_trans(struct xfs_scrub *sc);
35 int xrep_roll_trans(struct xfs_scrub *sc);
36 int xrep_defer_finish(struct xfs_scrub *sc);
37 bool xrep_ag_has_space(struct xfs_perag *pag, xfs_extlen_t nr_blocks,
38 		enum xfs_ag_resv_type type);
39 xfs_extlen_t xrep_calc_ag_resblks(struct xfs_scrub *sc);
40 
41 static inline int
xrep_trans_commit(struct xfs_scrub * sc)42 xrep_trans_commit(
43 	struct xfs_scrub	*sc)
44 {
45 	int error = xfs_trans_commit(sc->tp);
46 
47 	sc->tp = NULL;
48 	return error;
49 }
50 
51 struct xbitmap;
52 struct xagb_bitmap;
53 struct xrgb_bitmap;
54 struct xfsb_bitmap;
55 struct xrtb_bitmap;
56 
57 int xrep_fix_freelist(struct xfs_scrub *sc, int alloc_flags);
58 
59 struct xrep_find_ag_btree {
60 	/* in: rmap owner of the btree we're looking for */
61 	uint64_t			rmap_owner;
62 
63 	/* in: buffer ops */
64 	const struct xfs_buf_ops	*buf_ops;
65 
66 	/* in: maximum btree height */
67 	unsigned int			maxlevels;
68 
69 	/* out: the highest btree block found and the tree height */
70 	xfs_agblock_t			root;
71 	unsigned int			height;
72 };
73 
74 int xrep_find_ag_btree_roots(struct xfs_scrub *sc, struct xfs_buf *agf_bp,
75 		struct xrep_find_ag_btree *btree_info, struct xfs_buf *agfl_bp);
76 
77 #ifdef CONFIG_XFS_QUOTA
78 void xrep_update_qflags(struct xfs_scrub *sc, unsigned int clear_flags,
79 		unsigned int set_flags);
80 void xrep_force_quotacheck(struct xfs_scrub *sc, xfs_dqtype_t type);
81 int xrep_ino_dqattach(struct xfs_scrub *sc);
82 #else
83 # define xrep_force_quotacheck(sc, type)	((void)0)
84 # define xrep_ino_dqattach(sc)			(0)
85 #endif /* CONFIG_XFS_QUOTA */
86 
87 int xrep_setup_xfbtree(struct xfs_scrub *sc, const char *descr);
88 
89 int xrep_ino_ensure_extent_count(struct xfs_scrub *sc, int whichfork,
90 		xfs_extnum_t nextents);
91 int xrep_reset_perag_resv(struct xfs_scrub *sc);
92 int xrep_bmap(struct xfs_scrub *sc, int whichfork, bool allow_unwritten);
93 int xrep_metadata_inode_forks(struct xfs_scrub *sc);
94 int xrep_setup_ag_rmapbt(struct xfs_scrub *sc);
95 int xrep_setup_ag_refcountbt(struct xfs_scrub *sc);
96 int xrep_setup_xattr(struct xfs_scrub *sc);
97 int xrep_setup_directory(struct xfs_scrub *sc);
98 int xrep_setup_parent(struct xfs_scrub *sc);
99 int xrep_setup_nlinks(struct xfs_scrub *sc);
100 int xrep_setup_symlink(struct xfs_scrub *sc, unsigned int *resblks);
101 int xrep_setup_dirtree(struct xfs_scrub *sc);
102 int xrep_setup_rtrmapbt(struct xfs_scrub *sc);
103 int xrep_setup_rtrefcountbt(struct xfs_scrub *sc);
104 
105 /* Repair setup functions */
106 int xrep_setup_ag_allocbt(struct xfs_scrub *sc);
107 
108 struct xfs_imap;
109 int xrep_setup_inode(struct xfs_scrub *sc, const struct xfs_imap *imap);
110 
111 void xrep_ag_btcur_init(struct xfs_scrub *sc, struct xchk_ag *sa);
112 int xrep_ag_init(struct xfs_scrub *sc, struct xfs_perag *pag,
113 		struct xchk_ag *sa);
114 #ifdef CONFIG_XFS_RT
115 int xrep_rtgroup_init(struct xfs_scrub *sc, struct xfs_rtgroup *rtg,
116 		struct xchk_rt *sr, unsigned int rtglock_flags);
117 void xrep_rtgroup_btcur_init(struct xfs_scrub *sc, struct xchk_rt *sr);
118 int xrep_require_rtext_inuse(struct xfs_scrub *sc, xfs_rgblock_t rgbno,
119 		xfs_filblks_t len);
120 xfs_extlen_t xrep_calc_rtgroup_resblks(struct xfs_scrub *sc);
121 #else
122 # define xrep_rtgroup_init(sc, rtg, sr, lockflags)	(-ENOSYS)
123 # define xrep_calc_rtgroup_resblks(sc)			(0)
124 #endif /* CONFIG_XFS_RT */
125 
126 int xrep_check_ino_btree_mapping(struct xfs_scrub *sc,
127 		const struct xfs_rmap_irec *rec);
128 
129 /* Metadata revalidators */
130 
131 int xrep_revalidate_allocbt(struct xfs_scrub *sc);
132 int xrep_revalidate_iallocbt(struct xfs_scrub *sc);
133 
134 /* Metadata repairers */
135 
136 int xrep_probe(struct xfs_scrub *sc);
137 int xrep_superblock(struct xfs_scrub *sc);
138 int xrep_agf(struct xfs_scrub *sc);
139 int xrep_agfl(struct xfs_scrub *sc);
140 int xrep_agi(struct xfs_scrub *sc);
141 int xrep_allocbt(struct xfs_scrub *sc);
142 int xrep_iallocbt(struct xfs_scrub *sc);
143 int xrep_rmapbt(struct xfs_scrub *sc);
144 int xrep_refcountbt(struct xfs_scrub *sc);
145 int xrep_inode(struct xfs_scrub *sc);
146 int xrep_bmap_data(struct xfs_scrub *sc);
147 int xrep_bmap_attr(struct xfs_scrub *sc);
148 int xrep_bmap_cow(struct xfs_scrub *sc);
149 int xrep_nlinks(struct xfs_scrub *sc);
150 int xrep_fscounters(struct xfs_scrub *sc);
151 int xrep_xattr(struct xfs_scrub *sc);
152 int xrep_directory(struct xfs_scrub *sc);
153 int xrep_parent(struct xfs_scrub *sc);
154 int xrep_symlink(struct xfs_scrub *sc);
155 int xrep_dirtree(struct xfs_scrub *sc);
156 int xrep_metapath(struct xfs_scrub *sc);
157 
158 #ifdef CONFIG_XFS_RT
159 int xrep_rtbitmap(struct xfs_scrub *sc);
160 int xrep_rtsummary(struct xfs_scrub *sc);
161 int xrep_rgsuperblock(struct xfs_scrub *sc);
162 int xrep_rtrmapbt(struct xfs_scrub *sc);
163 int xrep_rtrefcountbt(struct xfs_scrub *sc);
164 #else
165 # define xrep_rtbitmap			xrep_notsupported
166 # define xrep_rtsummary			xrep_notsupported
167 # define xrep_rgsuperblock		xrep_notsupported
168 # define xrep_rtrmapbt			xrep_notsupported
169 # define xrep_rtrefcountbt		xrep_notsupported
170 #endif /* CONFIG_XFS_RT */
171 
172 #ifdef CONFIG_XFS_QUOTA
173 int xrep_quota(struct xfs_scrub *sc);
174 int xrep_quotacheck(struct xfs_scrub *sc);
175 #else
176 # define xrep_quota			xrep_notsupported
177 # define xrep_quotacheck		xrep_notsupported
178 #endif /* CONFIG_XFS_QUOTA */
179 
180 int xrep_reinit_pagf(struct xfs_scrub *sc);
181 int xrep_reinit_pagi(struct xfs_scrub *sc);
182 
183 int xrep_trans_alloc_hook_dummy(struct xfs_mount *mp, void **cookiep,
184 		struct xfs_trans **tpp);
185 void xrep_trans_cancel_hook_dummy(void **cookiep, struct xfs_trans *tp);
186 
187 bool xrep_buf_verify_struct(struct xfs_buf *bp, const struct xfs_buf_ops *ops);
188 void xrep_inode_set_nblocks(struct xfs_scrub *sc, int64_t new_blocks);
189 int xrep_reset_metafile_resv(struct xfs_scrub *sc);
190 
191 #else
192 
193 #define xrep_ino_dqattach(sc)	(0)
194 
195 /*
196  * When online repair is not built into the kernel, we still want to attempt
197  * the repair so that the stub xrep_attempt below will return EOPNOTSUPP.
198  */
xrep_will_attempt(const struct xfs_scrub * sc)199 static inline bool xrep_will_attempt(const struct xfs_scrub *sc)
200 {
201 	return (sc->sm->sm_flags & XFS_SCRUB_IFLAG_FORCE_REBUILD) ||
202 		xchk_needs_repair(sc->sm);
203 }
204 
205 static inline int
xrep_attempt(struct xfs_scrub * sc,struct xchk_stats_run * run)206 xrep_attempt(
207 	struct xfs_scrub	*sc,
208 	struct xchk_stats_run	*run)
209 {
210 	return -EOPNOTSUPP;
211 }
212 
xrep_failure(struct xfs_mount * mp)213 static inline void xrep_failure(struct xfs_mount *mp) {}
214 
215 static inline xfs_extlen_t
xrep_calc_ag_resblks(struct xfs_scrub * sc)216 xrep_calc_ag_resblks(
217 	struct xfs_scrub	*sc)
218 {
219 	return 0;
220 }
221 
222 #define xrep_calc_rtgroup_resblks	xrep_calc_ag_resblks
223 
224 static inline int
xrep_reset_perag_resv(struct xfs_scrub * sc)225 xrep_reset_perag_resv(
226 	struct xfs_scrub	*sc)
227 {
228 	if (!(sc->flags & XREP_RESET_PERAG_RESV))
229 		return 0;
230 
231 	ASSERT(0);
232 	return -EOPNOTSUPP;
233 }
234 
235 /* repair setup functions for no-repair */
236 static inline int
xrep_setup_nothing(struct xfs_scrub * sc)237 xrep_setup_nothing(
238 	struct xfs_scrub	*sc)
239 {
240 	return 0;
241 }
242 #define xrep_setup_ag_allocbt		xrep_setup_nothing
243 #define xrep_setup_ag_rmapbt		xrep_setup_nothing
244 #define xrep_setup_ag_refcountbt	xrep_setup_nothing
245 #define xrep_setup_xattr		xrep_setup_nothing
246 #define xrep_setup_directory		xrep_setup_nothing
247 #define xrep_setup_parent		xrep_setup_nothing
248 #define xrep_setup_nlinks		xrep_setup_nothing
249 #define xrep_setup_dirtree		xrep_setup_nothing
250 #define xrep_setup_metapath		xrep_setup_nothing
251 #define xrep_setup_rtrmapbt		xrep_setup_nothing
252 #define xrep_setup_rtrefcountbt		xrep_setup_nothing
253 
254 #define xrep_setup_inode(sc, imap)	((void)0)
255 
xrep_setup_symlink(struct xfs_scrub * sc,unsigned int * x)256 static inline int xrep_setup_symlink(struct xfs_scrub *sc, unsigned int *x)
257 {
258 	return 0;
259 }
260 
261 #define xrep_revalidate_allocbt		(NULL)
262 #define xrep_revalidate_iallocbt	(NULL)
263 
264 #define xrep_probe			xrep_notsupported
265 #define xrep_superblock			xrep_notsupported
266 #define xrep_agf			xrep_notsupported
267 #define xrep_agfl			xrep_notsupported
268 #define xrep_agi			xrep_notsupported
269 #define xrep_allocbt			xrep_notsupported
270 #define xrep_iallocbt			xrep_notsupported
271 #define xrep_rmapbt			xrep_notsupported
272 #define xrep_refcountbt			xrep_notsupported
273 #define xrep_inode			xrep_notsupported
274 #define xrep_bmap_data			xrep_notsupported
275 #define xrep_bmap_attr			xrep_notsupported
276 #define xrep_bmap_cow			xrep_notsupported
277 #define xrep_rtbitmap			xrep_notsupported
278 #define xrep_quota			xrep_notsupported
279 #define xrep_quotacheck			xrep_notsupported
280 #define xrep_nlinks			xrep_notsupported
281 #define xrep_fscounters			xrep_notsupported
282 #define xrep_rtsummary			xrep_notsupported
283 #define xrep_xattr			xrep_notsupported
284 #define xrep_directory			xrep_notsupported
285 #define xrep_parent			xrep_notsupported
286 #define xrep_symlink			xrep_notsupported
287 #define xrep_dirtree			xrep_notsupported
288 #define xrep_metapath			xrep_notsupported
289 #define xrep_rgsuperblock		xrep_notsupported
290 #define xrep_rtrmapbt			xrep_notsupported
291 #define xrep_rtrefcountbt		xrep_notsupported
292 
293 #endif /* CONFIG_XFS_ONLINE_REPAIR */
294 
295 #endif	/* __XFS_SCRUB_REPAIR_H__ */
296