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