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