xref: /linux/fs/xfs/scrub/common.h (revision 1113a6d6d5d1336f4415fa1367aac0f853f0892d)
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 
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_inode_bmap_data(struct xfs_scrub *sc);
78 int xchk_setup_directory(struct xfs_scrub *sc);
79 int xchk_setup_xattr(struct xfs_scrub *sc);
80 int xchk_setup_symlink(struct xfs_scrub *sc);
81 int xchk_setup_parent(struct xfs_scrub *sc);
82 int xchk_setup_dirtree(struct xfs_scrub *sc);
83 int xchk_setup_metapath(struct xfs_scrub *sc);
84 #ifdef CONFIG_XFS_RT
85 int xchk_setup_rtbitmap(struct xfs_scrub *sc);
86 int xchk_setup_rtsummary(struct xfs_scrub *sc);
87 int xchk_setup_rgsuperblock(struct xfs_scrub *sc);
88 int xchk_setup_rtrmapbt(struct xfs_scrub *sc);
89 int xchk_setup_rtrefcountbt(struct xfs_scrub *sc);
90 #else
91 # define xchk_setup_rtbitmap		xchk_setup_nothing
92 # define xchk_setup_rtsummary		xchk_setup_nothing
93 # define xchk_setup_rgsuperblock	xchk_setup_nothing
94 # define xchk_setup_rtrmapbt		xchk_setup_nothing
95 # define xchk_setup_rtrefcountbt	xchk_setup_nothing
96 #endif
97 #ifdef CONFIG_XFS_QUOTA
98 int xchk_ino_dqattach(struct xfs_scrub *sc);
99 int xchk_setup_quota(struct xfs_scrub *sc);
100 int xchk_setup_quotacheck(struct xfs_scrub *sc);
101 #else
102 static inline int
103 xchk_ino_dqattach(struct xfs_scrub *sc)
104 {
105 	return 0;
106 }
107 # define xchk_setup_quota		xchk_setup_nothing
108 # define xchk_setup_quotacheck		xchk_setup_nothing
109 #endif
110 int xchk_setup_fscounters(struct xfs_scrub *sc);
111 int xchk_setup_nlinks(struct xfs_scrub *sc);
112 
113 void xchk_ag_free(struct xfs_scrub *sc, struct xchk_ag *sa);
114 int xchk_ag_init(struct xfs_scrub *sc, xfs_agnumber_t agno,
115 		struct xchk_ag *sa);
116 int xchk_perag_drain_and_lock(struct xfs_scrub *sc);
117 
118 /*
119  * Grab all AG resources, treating the inability to grab the perag structure as
120  * a fs corruption.  This is intended for callers checking an ondisk reference
121  * to a given AG, which means that the AG must still exist.
122  */
123 static inline int
124 xchk_ag_init_existing(
125 	struct xfs_scrub	*sc,
126 	xfs_agnumber_t		agno,
127 	struct xchk_ag		*sa)
128 {
129 	int			error = xchk_ag_init(sc, agno, sa);
130 
131 	return error == -ENOENT ? -EFSCORRUPTED : error;
132 }
133 
134 #ifdef CONFIG_XFS_RT
135 
136 /* All the locks we need to check an rtgroup. */
137 #define XCHK_RTGLOCK_ALL	(XFS_RTGLOCK_BITMAP | \
138 				 XFS_RTGLOCK_RMAP | \
139 				 XFS_RTGLOCK_REFCOUNT)
140 
141 int xchk_rtgroup_init(struct xfs_scrub *sc, xfs_rgnumber_t rgno,
142 		struct xchk_rt *sr);
143 
144 static inline int
145 xchk_rtgroup_init_existing(
146 	struct xfs_scrub	*sc,
147 	xfs_rgnumber_t		rgno,
148 	struct xchk_rt		*sr)
149 {
150 	int			error = xchk_rtgroup_init(sc, rgno, sr);
151 
152 	return error == -ENOENT ? -EFSCORRUPTED : error;
153 }
154 
155 int xchk_rtgroup_lock(struct xfs_scrub *sc, struct xchk_rt *sr,
156 		unsigned int rtglock_flags);
157 void xchk_rtgroup_unlock(struct xchk_rt *sr);
158 void xchk_rtgroup_btcur_free(struct xchk_rt *sr);
159 void xchk_rtgroup_free(struct xfs_scrub *sc, struct xchk_rt *sr);
160 #else
161 # define xchk_rtgroup_init(sc, rgno, sr)		(-EFSCORRUPTED)
162 # define xchk_rtgroup_init_existing(sc, rgno, sr)	(-EFSCORRUPTED)
163 # define xchk_rtgroup_lock(sc, sr, lockflags)		(-EFSCORRUPTED)
164 # define xchk_rtgroup_unlock(sr)			do { } while (0)
165 # define xchk_rtgroup_btcur_free(sr)			do { } while (0)
166 # define xchk_rtgroup_free(sc, sr)			do { } while (0)
167 #endif /* CONFIG_XFS_RT */
168 
169 int xchk_ag_read_headers(struct xfs_scrub *sc, xfs_agnumber_t agno,
170 		struct xchk_ag *sa);
171 void xchk_ag_btcur_free(struct xchk_ag *sa);
172 void xchk_ag_btcur_init(struct xfs_scrub *sc, struct xchk_ag *sa);
173 int xchk_count_rmap_ownedby_ag(struct xfs_scrub *sc, struct xfs_btree_cur *cur,
174 		const struct xfs_owner_info *oinfo, xfs_filblks_t *blocks);
175 
176 int xchk_setup_ag_btree(struct xfs_scrub *sc, bool force_log);
177 int xchk_iget_for_scrubbing(struct xfs_scrub *sc);
178 int xchk_setup_inode_contents(struct xfs_scrub *sc, unsigned int resblks);
179 int xchk_install_live_inode(struct xfs_scrub *sc, struct xfs_inode *ip);
180 
181 void xchk_ilock(struct xfs_scrub *sc, unsigned int ilock_flags);
182 bool xchk_ilock_nowait(struct xfs_scrub *sc, unsigned int ilock_flags);
183 void xchk_iunlock(struct xfs_scrub *sc, unsigned int ilock_flags);
184 
185 void xchk_buffer_recheck(struct xfs_scrub *sc, struct xfs_buf *bp);
186 
187 /*
188  * Grab the inode at @inum.  The caller must have created a scrub transaction
189  * so that we can confirm the inumber by walking the inobt and not deadlock on
190  * a loop in the inobt.
191  */
192 int xchk_iget(struct xfs_scrub *sc, xfs_ino_t inum, struct xfs_inode **ipp);
193 int xchk_iget_agi(struct xfs_scrub *sc, xfs_ino_t inum,
194 		struct xfs_buf **agi_bpp, struct xfs_inode **ipp);
195 void xchk_irele(struct xfs_scrub *sc, struct xfs_inode *ip);
196 int xchk_install_handle_inode(struct xfs_scrub *sc, struct xfs_inode *ip);
197 
198 /*
199  * Safe version of (untrusted) xchk_iget that uses an empty transaction to
200  * avoid deadlocking on loops in the inobt.  This should only be used in a
201  * scrub or repair setup routine, and only prior to grabbing a transaction.
202  */
203 static inline int
204 xchk_iget_safe(struct xfs_scrub *sc, xfs_ino_t inum, struct xfs_inode **ipp)
205 {
206 	int	error;
207 
208 	ASSERT(sc->tp == NULL);
209 
210 	error = xchk_trans_alloc(sc, 0);
211 	if (error)
212 		return error;
213 	error = xchk_iget(sc, inum, ipp);
214 	xchk_trans_cancel(sc);
215 	return error;
216 }
217 
218 /*
219  * Don't bother cross-referencing if we already found corruption or cross
220  * referencing discrepancies.
221  */
222 static inline bool xchk_skip_xref(struct xfs_scrub_metadata *sm)
223 {
224 	return sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
225 			       XFS_SCRUB_OFLAG_XCORRUPT);
226 }
227 
228 bool xchk_dir_looks_zapped(struct xfs_inode *dp);
229 bool xchk_pptr_looks_zapped(struct xfs_inode *ip);
230 
231 /* Decide if a repair is required. */
232 static inline bool xchk_needs_repair(const struct xfs_scrub_metadata *sm)
233 {
234 	return sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
235 			       XFS_SCRUB_OFLAG_XCORRUPT |
236 			       XFS_SCRUB_OFLAG_PREEN);
237 }
238 
239 /*
240  * "Should we prepare for a repair?"
241  *
242  * Return true if the caller permits us to repair metadata and we're not
243  * setting up for a post-repair evaluation.
244  */
245 static inline bool xchk_could_repair(const struct xfs_scrub *sc)
246 {
247 	return (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) &&
248 		!(sc->flags & XREP_ALREADY_FIXED);
249 }
250 
251 int xchk_metadata_inode_forks(struct xfs_scrub *sc);
252 
253 /*
254  * Setting up a hook to wait for intents to drain is costly -- we have to take
255  * the CPU hotplug lock and force an i-cache flush on all CPUs once to set it
256  * up, and again to tear it down.  These costs add up quickly, so we only want
257  * to enable the drain waiter if the drain actually detected a conflict with
258  * running intent chains.
259  */
260 static inline bool xchk_need_intent_drain(struct xfs_scrub *sc)
261 {
262 	return sc->flags & XCHK_NEED_DRAIN;
263 }
264 
265 void xchk_fsgates_enable(struct xfs_scrub *sc, unsigned int scrub_fshooks);
266 
267 int xchk_inode_is_allocated(struct xfs_scrub *sc, xfs_agino_t agino,
268 		bool *inuse);
269 int xchk_inode_count_blocks(struct xfs_scrub *sc, int whichfork,
270 		xfs_extnum_t *nextents, xfs_filblks_t *count);
271 
272 bool xchk_inode_is_dirtree_root(const struct xfs_inode *ip);
273 bool xchk_inode_is_sb_rooted(const struct xfs_inode *ip);
274 xfs_ino_t xchk_inode_rootdir_inum(const struct xfs_inode *ip);
275 
276 #endif	/* __XFS_SCRUB_COMMON_H__ */
277