]>
Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
36fd6e86 DW |
2 | /* |
3 | * Copyright (C) 2017 Oracle. All Rights Reserved. | |
36fd6e86 | 4 | * Author: Darrick J. Wong <[email protected]> |
36fd6e86 DW |
5 | */ |
6 | #include "xfs.h" | |
7 | #include "xfs_fs.h" | |
8 | #include "xfs_shared.h" | |
9 | #include "xfs_format.h" | |
10 | #include "xfs_trans_resv.h" | |
11 | #include "xfs_mount.h" | |
12 | #include "xfs_defer.h" | |
13 | #include "xfs_btree.h" | |
14 | #include "xfs_bit.h" | |
15 | #include "xfs_log_format.h" | |
16 | #include "xfs_trans.h" | |
17 | #include "xfs_sb.h" | |
18 | #include "xfs_inode.h" | |
80e4e126 DW |
19 | #include "xfs_icache.h" |
20 | #include "xfs_itable.h" | |
36fd6e86 DW |
21 | #include "xfs_alloc.h" |
22 | #include "xfs_alloc_btree.h" | |
23 | #include "xfs_bmap.h" | |
24 | #include "xfs_bmap_btree.h" | |
25 | #include "xfs_ialloc.h" | |
26 | #include "xfs_ialloc_btree.h" | |
27 | #include "xfs_refcount.h" | |
28 | #include "xfs_refcount_btree.h" | |
29 | #include "xfs_rmap.h" | |
30 | #include "xfs_rmap_btree.h" | |
eb41c93f DW |
31 | #include "xfs_quota.h" |
32 | #include "xfs_qm.h" | |
84d42ea6 DW |
33 | #include "xfs_errortag.h" |
34 | #include "xfs_error.h" | |
35 | #include "xfs_log.h" | |
36 | #include "xfs_trans_priv.h" | |
36fd6e86 DW |
37 | #include "scrub/xfs_scrub.h" |
38 | #include "scrub/scrub.h" | |
dcb660f9 | 39 | #include "scrub/common.h" |
36fd6e86 | 40 | #include "scrub/trace.h" |
b6c1beb9 | 41 | #include "scrub/btree.h" |
84d42ea6 | 42 | #include "scrub/repair.h" |
36fd6e86 | 43 | |
a5637186 DW |
44 | /* |
45 | * Online Scrub and Repair | |
46 | * | |
47 | * Traditionally, XFS (the kernel driver) did not know how to check or | |
48 | * repair on-disk data structures. That task was left to the xfs_check | |
49 | * and xfs_repair tools, both of which require taking the filesystem | |
50 | * offline for a thorough but time consuming examination. Online | |
51 | * scrub & repair, on the other hand, enables us to check the metadata | |
52 | * for obvious errors while carefully stepping around the filesystem's | |
53 | * ongoing operations, locking rules, etc. | |
54 | * | |
55 | * Given that most XFS metadata consist of records stored in a btree, | |
56 | * most of the checking functions iterate the btree blocks themselves | |
57 | * looking for irregularities. When a record block is encountered, each | |
58 | * record can be checked for obviously bad values. Record values can | |
59 | * also be cross-referenced against other btrees to look for potential | |
60 | * misunderstandings between pieces of metadata. | |
61 | * | |
62 | * It is expected that the checkers responsible for per-AG metadata | |
63 | * structures will lock the AG headers (AGI, AGF, AGFL), iterate the | |
64 | * metadata structure, and perform any relevant cross-referencing before | |
65 | * unlocking the AG and returning the results to userspace. These | |
66 | * scrubbers must not keep an AG locked for too long to avoid tying up | |
67 | * the block and inode allocators. | |
68 | * | |
69 | * Block maps and b-trees rooted in an inode present a special challenge | |
70 | * because they can involve extents from any AG. The general scrubber | |
71 | * structure of lock -> check -> xref -> unlock still holds, but AG | |
72 | * locking order rules /must/ be obeyed to avoid deadlocks. The | |
73 | * ordering rule, of course, is that we must lock in increasing AG | |
74 | * order. Helper functions are provided to track which AG headers we've | |
75 | * already locked. If we detect an imminent locking order violation, we | |
76 | * can signal a potential deadlock, in which case the scrubber can jump | |
77 | * out to the top level, lock all the AGs in order, and retry the scrub. | |
78 | * | |
79 | * For file data (directories, extended attributes, symlinks) scrub, we | |
80 | * can simply lock the inode and walk the data. For btree data | |
81 | * (directories and attributes) we follow the same btree-scrubbing | |
82 | * strategy outlined previously to check the records. | |
83 | * | |
84 | * We use a bit of trickery with transactions to avoid buffer deadlocks | |
85 | * if there is a cycle in the metadata. The basic problem is that | |
86 | * travelling down a btree involves locking the current buffer at each | |
87 | * tree level. If a pointer should somehow point back to a buffer that | |
88 | * we've already examined, we will deadlock due to the second buffer | |
89 | * locking attempt. Note however that grabbing a buffer in transaction | |
90 | * context links the locked buffer to the transaction. If we try to | |
91 | * re-grab the buffer in the context of the same transaction, we avoid | |
92 | * the second lock attempt and continue. Between the verifier and the | |
93 | * scrubber, something will notice that something is amiss and report | |
94 | * the corruption. Therefore, each scrubber will allocate an empty | |
95 | * transaction, attach buffers to it, and cancel the transaction at the | |
96 | * end of the scrub run. Cancelling a non-dirty transaction simply | |
97 | * unlocks the buffers. | |
98 | * | |
99 | * There are four pieces of data that scrub can communicate to | |
100 | * userspace. The first is the error code (errno), which can be used to | |
101 | * communicate operational errors in performing the scrub. There are | |
102 | * also three flags that can be set in the scrub context. If the data | |
103 | * structure itself is corrupt, the CORRUPT flag will be set. If | |
104 | * the metadata is correct but otherwise suboptimal, the PREEN flag | |
105 | * will be set. | |
64b12563 DW |
106 | * |
107 | * We perform secondary validation of filesystem metadata by | |
108 | * cross-referencing every record with all other available metadata. | |
109 | * For example, for block mapping extents, we verify that there are no | |
110 | * records in the free space and inode btrees corresponding to that | |
111 | * space extent and that there is a corresponding entry in the reverse | |
112 | * mapping btree. Inconsistent metadata is noted by setting the | |
113 | * XCORRUPT flag; btree query function errors are noted by setting the | |
114 | * XFAIL flag and deleting the cursor to prevent further attempts to | |
115 | * cross-reference with a defective btree. | |
84d42ea6 DW |
116 | * |
117 | * If a piece of metadata proves corrupt or suboptimal, the userspace | |
118 | * program can ask the kernel to apply some tender loving care (TLC) to | |
119 | * the metadata object by setting the REPAIR flag and re-calling the | |
120 | * scrub ioctl. "Corruption" is defined by metadata violating the | |
121 | * on-disk specification; operations cannot continue if the violation is | |
122 | * left untreated. It is possible for XFS to continue if an object is | |
123 | * "suboptimal", however performance may be degraded. Repairs are | |
124 | * usually performed by rebuilding the metadata entirely out of | |
125 | * redundant metadata. Optimizing, on the other hand, can sometimes be | |
126 | * done without rebuilding entire structures. | |
127 | * | |
128 | * Generally speaking, the repair code has the following code structure: | |
129 | * Lock -> scrub -> repair -> commit -> re-lock -> re-scrub -> unlock. | |
130 | * The first check helps us figure out if we need to rebuild or simply | |
131 | * optimize the structure so that the rebuild knows what to do. The | |
132 | * second check evaluates the completeness of the repair; that is what | |
133 | * is reported to userspace. | |
c517b3aa DW |
134 | * |
135 | * A quick note on symbol prefixes: | |
136 | * - "xfs_" are general XFS symbols. | |
137 | * - "xchk_" are symbols related to metadata checking. | |
138 | * - "xrep_" are symbols related to metadata repair. | |
139 | * - "xfs_scrub_" are symbols that tie online fsck to the rest of XFS. | |
a5637186 DW |
140 | */ |
141 | ||
dcb660f9 DW |
142 | /* |
143 | * Scrub probe -- userspace uses this to probe if we're willing to scrub | |
144 | * or repair a given mountpoint. This will be used by xfs_scrub to | |
145 | * probe the kernel's abilities to scrub (and repair) the metadata. We | |
146 | * do this by validating the ioctl inputs from userspace, preparing the | |
147 | * filesystem for a scrub (or a repair) operation, and immediately | |
148 | * returning to userspace. Userspace can use the returned errno and | |
149 | * structure state to decide (in broad terms) if scrub/repair are | |
150 | * supported by the running kernel. | |
151 | */ | |
88aa5de4 | 152 | static int |
c517b3aa | 153 | xchk_probe( |
1d8a748a | 154 | struct xfs_scrub *sc) |
dcb660f9 | 155 | { |
032d91f9 | 156 | int error = 0; |
dcb660f9 | 157 | |
c517b3aa | 158 | if (xchk_should_terminate(sc, &error)) |
dcb660f9 DW |
159 | return error; |
160 | ||
161 | return 0; | |
162 | } | |
163 | ||
a5637186 DW |
164 | /* Scrub setup and teardown */ |
165 | ||
166 | /* Free all the resources and finish the transactions. */ | |
167 | STATIC int | |
c517b3aa | 168 | xchk_teardown( |
1d8a748a | 169 | struct xfs_scrub *sc, |
032d91f9 DW |
170 | struct xfs_inode *ip_in, |
171 | int error) | |
a5637186 | 172 | { |
c517b3aa | 173 | xchk_ag_free(sc, &sc->sa); |
a5637186 | 174 | if (sc->tp) { |
84d42ea6 DW |
175 | if (error == 0 && (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)) |
176 | error = xfs_trans_commit(sc->tp); | |
177 | else | |
178 | xfs_trans_cancel(sc->tp); | |
a5637186 DW |
179 | sc->tp = NULL; |
180 | } | |
80e4e126 | 181 | if (sc->ip) { |
638a7174 DW |
182 | if (sc->ilock_flags) |
183 | xfs_iunlock(sc->ip, sc->ilock_flags); | |
80e4e126 DW |
184 | if (sc->ip != ip_in && |
185 | !xfs_internal_inum(sc->mp, sc->ip->i_ino)) | |
44a8736b | 186 | xfs_irele(sc->ip); |
80e4e126 DW |
187 | sc->ip = NULL; |
188 | } | |
eb41c93f DW |
189 | if (sc->has_quotaofflock) |
190 | mutex_unlock(&sc->mp->m_quotainfo->qi_quotaofflock); | |
eec0482e DW |
191 | if (sc->buf) { |
192 | kmem_free(sc->buf); | |
193 | sc->buf = NULL; | |
194 | } | |
a5637186 DW |
195 | return error; |
196 | } | |
197 | ||
198 | /* Scrubbing dispatch. */ | |
199 | ||
c517b3aa | 200 | static const struct xchk_meta_ops meta_scrub_ops[] = { |
bfb3e9b9 | 201 | [XFS_SCRUB_TYPE_PROBE] = { /* ioctl presence test */ |
8e630837 | 202 | .type = ST_NONE, |
c517b3aa DW |
203 | .setup = xchk_setup_fs, |
204 | .scrub = xchk_probe, | |
b5e2196e | 205 | .repair = xrep_probe, |
dcb660f9 | 206 | }, |
bfb3e9b9 | 207 | [XFS_SCRUB_TYPE_SB] = { /* superblock */ |
8e630837 | 208 | .type = ST_PERAG, |
c517b3aa DW |
209 | .setup = xchk_setup_fs, |
210 | .scrub = xchk_superblock, | |
b5e2196e | 211 | .repair = xrep_superblock, |
21fb4cb1 | 212 | }, |
bfb3e9b9 | 213 | [XFS_SCRUB_TYPE_AGF] = { /* agf */ |
8e630837 | 214 | .type = ST_PERAG, |
c517b3aa DW |
215 | .setup = xchk_setup_fs, |
216 | .scrub = xchk_agf, | |
f9ed6deb | 217 | .repair = xrep_agf, |
ab9d5dc5 | 218 | }, |
bfb3e9b9 | 219 | [XFS_SCRUB_TYPE_AGFL]= { /* agfl */ |
8e630837 | 220 | .type = ST_PERAG, |
c517b3aa DW |
221 | .setup = xchk_setup_fs, |
222 | .scrub = xchk_agfl, | |
0e93d3f4 | 223 | .repair = xrep_agfl, |
ab9d5dc5 | 224 | }, |
bfb3e9b9 | 225 | [XFS_SCRUB_TYPE_AGI] = { /* agi */ |
8e630837 | 226 | .type = ST_PERAG, |
c517b3aa DW |
227 | .setup = xchk_setup_fs, |
228 | .scrub = xchk_agi, | |
13942aa9 | 229 | .repair = xrep_agi, |
a12890ae | 230 | }, |
bfb3e9b9 | 231 | [XFS_SCRUB_TYPE_BNOBT] = { /* bnobt */ |
8e630837 | 232 | .type = ST_PERAG, |
c517b3aa DW |
233 | .setup = xchk_setup_ag_allocbt, |
234 | .scrub = xchk_bnobt, | |
b5e2196e | 235 | .repair = xrep_notsupported, |
efa7a99c | 236 | }, |
bfb3e9b9 | 237 | [XFS_SCRUB_TYPE_CNTBT] = { /* cntbt */ |
8e630837 | 238 | .type = ST_PERAG, |
c517b3aa DW |
239 | .setup = xchk_setup_ag_allocbt, |
240 | .scrub = xchk_cntbt, | |
b5e2196e | 241 | .repair = xrep_notsupported, |
efa7a99c | 242 | }, |
bfb3e9b9 | 243 | [XFS_SCRUB_TYPE_INOBT] = { /* inobt */ |
8e630837 | 244 | .type = ST_PERAG, |
c517b3aa DW |
245 | .setup = xchk_setup_ag_iallocbt, |
246 | .scrub = xchk_inobt, | |
b5e2196e | 247 | .repair = xrep_notsupported, |
3daa6641 | 248 | }, |
bfb3e9b9 | 249 | [XFS_SCRUB_TYPE_FINOBT] = { /* finobt */ |
8e630837 | 250 | .type = ST_PERAG, |
c517b3aa DW |
251 | .setup = xchk_setup_ag_iallocbt, |
252 | .scrub = xchk_finobt, | |
3daa6641 | 253 | .has = xfs_sb_version_hasfinobt, |
b5e2196e | 254 | .repair = xrep_notsupported, |
3daa6641 | 255 | }, |
bfb3e9b9 | 256 | [XFS_SCRUB_TYPE_RMAPBT] = { /* rmapbt */ |
8e630837 | 257 | .type = ST_PERAG, |
c517b3aa DW |
258 | .setup = xchk_setup_ag_rmapbt, |
259 | .scrub = xchk_rmapbt, | |
c7e693d9 | 260 | .has = xfs_sb_version_hasrmapbt, |
b5e2196e | 261 | .repair = xrep_notsupported, |
c7e693d9 | 262 | }, |
bfb3e9b9 | 263 | [XFS_SCRUB_TYPE_REFCNTBT] = { /* refcountbt */ |
8e630837 | 264 | .type = ST_PERAG, |
c517b3aa DW |
265 | .setup = xchk_setup_ag_refcountbt, |
266 | .scrub = xchk_refcountbt, | |
edc09b52 | 267 | .has = xfs_sb_version_hasreflink, |
b5e2196e | 268 | .repair = xrep_notsupported, |
edc09b52 | 269 | }, |
bfb3e9b9 | 270 | [XFS_SCRUB_TYPE_INODE] = { /* inode record */ |
8e630837 | 271 | .type = ST_INODE, |
c517b3aa DW |
272 | .setup = xchk_setup_inode, |
273 | .scrub = xchk_inode, | |
b5e2196e | 274 | .repair = xrep_notsupported, |
80e4e126 | 275 | }, |
bfb3e9b9 | 276 | [XFS_SCRUB_TYPE_BMBTD] = { /* inode data fork */ |
8e630837 | 277 | .type = ST_INODE, |
c517b3aa DW |
278 | .setup = xchk_setup_inode_bmap, |
279 | .scrub = xchk_bmap_data, | |
b5e2196e | 280 | .repair = xrep_notsupported, |
99d9d8d0 | 281 | }, |
bfb3e9b9 | 282 | [XFS_SCRUB_TYPE_BMBTA] = { /* inode attr fork */ |
8e630837 | 283 | .type = ST_INODE, |
c517b3aa DW |
284 | .setup = xchk_setup_inode_bmap, |
285 | .scrub = xchk_bmap_attr, | |
b5e2196e | 286 | .repair = xrep_notsupported, |
99d9d8d0 | 287 | }, |
bfb3e9b9 | 288 | [XFS_SCRUB_TYPE_BMBTC] = { /* inode CoW fork */ |
8e630837 | 289 | .type = ST_INODE, |
c517b3aa DW |
290 | .setup = xchk_setup_inode_bmap, |
291 | .scrub = xchk_bmap_cow, | |
b5e2196e | 292 | .repair = xrep_notsupported, |
99d9d8d0 | 293 | }, |
bfb3e9b9 | 294 | [XFS_SCRUB_TYPE_DIR] = { /* directory */ |
8e630837 | 295 | .type = ST_INODE, |
c517b3aa DW |
296 | .setup = xchk_setup_directory, |
297 | .scrub = xchk_directory, | |
b5e2196e | 298 | .repair = xrep_notsupported, |
a5c46e5e | 299 | }, |
bfb3e9b9 | 300 | [XFS_SCRUB_TYPE_XATTR] = { /* extended attributes */ |
8e630837 | 301 | .type = ST_INODE, |
c517b3aa DW |
302 | .setup = xchk_setup_xattr, |
303 | .scrub = xchk_xattr, | |
b5e2196e | 304 | .repair = xrep_notsupported, |
eec0482e | 305 | }, |
bfb3e9b9 | 306 | [XFS_SCRUB_TYPE_SYMLINK] = { /* symbolic link */ |
8e630837 | 307 | .type = ST_INODE, |
c517b3aa DW |
308 | .setup = xchk_setup_symlink, |
309 | .scrub = xchk_symlink, | |
b5e2196e | 310 | .repair = xrep_notsupported, |
2a721dbb | 311 | }, |
bfb3e9b9 | 312 | [XFS_SCRUB_TYPE_PARENT] = { /* parent pointers */ |
8e630837 | 313 | .type = ST_INODE, |
c517b3aa DW |
314 | .setup = xchk_setup_parent, |
315 | .scrub = xchk_parent, | |
b5e2196e | 316 | .repair = xrep_notsupported, |
0f28b257 | 317 | }, |
bfb3e9b9 | 318 | [XFS_SCRUB_TYPE_RTBITMAP] = { /* realtime bitmap */ |
8e630837 | 319 | .type = ST_FS, |
c517b3aa DW |
320 | .setup = xchk_setup_rt, |
321 | .scrub = xchk_rtbitmap, | |
29b0767b | 322 | .has = xfs_sb_version_hasrealtime, |
b5e2196e | 323 | .repair = xrep_notsupported, |
29b0767b | 324 | }, |
bfb3e9b9 | 325 | [XFS_SCRUB_TYPE_RTSUM] = { /* realtime summary */ |
8e630837 | 326 | .type = ST_FS, |
c517b3aa DW |
327 | .setup = xchk_setup_rt, |
328 | .scrub = xchk_rtsummary, | |
29b0767b | 329 | .has = xfs_sb_version_hasrealtime, |
b5e2196e | 330 | .repair = xrep_notsupported, |
29b0767b | 331 | }, |
bfb3e9b9 | 332 | [XFS_SCRUB_TYPE_UQUOTA] = { /* user quota */ |
8e630837 | 333 | .type = ST_FS, |
c517b3aa DW |
334 | .setup = xchk_setup_quota, |
335 | .scrub = xchk_quota, | |
b5e2196e | 336 | .repair = xrep_notsupported, |
c2fc338c | 337 | }, |
bfb3e9b9 | 338 | [XFS_SCRUB_TYPE_GQUOTA] = { /* group quota */ |
8e630837 | 339 | .type = ST_FS, |
c517b3aa DW |
340 | .setup = xchk_setup_quota, |
341 | .scrub = xchk_quota, | |
b5e2196e | 342 | .repair = xrep_notsupported, |
c2fc338c | 343 | }, |
bfb3e9b9 | 344 | [XFS_SCRUB_TYPE_PQUOTA] = { /* project quota */ |
8e630837 | 345 | .type = ST_FS, |
c517b3aa DW |
346 | .setup = xchk_setup_quota, |
347 | .scrub = xchk_quota, | |
b5e2196e | 348 | .repair = xrep_notsupported, |
c2fc338c | 349 | }, |
a5637186 DW |
350 | }; |
351 | ||
352 | /* This isn't a stable feature, warn once per day. */ | |
353 | static inline void | |
c517b3aa | 354 | xchk_experimental_warning( |
a5637186 DW |
355 | struct xfs_mount *mp) |
356 | { | |
357 | static struct ratelimit_state scrub_warning = RATELIMIT_STATE_INIT( | |
c517b3aa | 358 | "xchk_warning", 86400 * HZ, 1); |
a5637186 DW |
359 | ratelimit_set_flags(&scrub_warning, RATELIMIT_MSG_ON_RELEASE); |
360 | ||
361 | if (__ratelimit(&scrub_warning)) | |
362 | xfs_alert(mp, | |
363 | "EXPERIMENTAL online scrub feature in use. Use at your own risk!"); | |
364 | } | |
365 | ||
0a085ddf | 366 | static int |
c517b3aa | 367 | xchk_validate_inputs( |
0a085ddf | 368 | struct xfs_mount *mp, |
36fd6e86 DW |
369 | struct xfs_scrub_metadata *sm) |
370 | { | |
0a085ddf | 371 | int error; |
c517b3aa | 372 | const struct xchk_meta_ops *ops; |
a5637186 | 373 | |
a5637186 | 374 | error = -EINVAL; |
0a085ddf | 375 | /* Check our inputs. */ |
a5637186 DW |
376 | sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT; |
377 | if (sm->sm_flags & ~XFS_SCRUB_FLAGS_IN) | |
378 | goto out; | |
8e630837 | 379 | /* sm_reserved[] must be zero */ |
a5637186 DW |
380 | if (memchr_inv(sm->sm_reserved, 0, sizeof(sm->sm_reserved))) |
381 | goto out; | |
382 | ||
a5637186 | 383 | error = -ENOENT; |
0a085ddf | 384 | /* Do we know about this type of metadata? */ |
a5637186 DW |
385 | if (sm->sm_type >= XFS_SCRUB_TYPE_NR) |
386 | goto out; | |
387 | ops = &meta_scrub_ops[sm->sm_type]; | |
bfb3e9b9 | 388 | if (ops->setup == NULL || ops->scrub == NULL) |
a5637186 | 389 | goto out; |
0a085ddf ES |
390 | /* Does this fs even support this type of metadata? */ |
391 | if (ops->has && !ops->has(&mp->m_sb)) | |
392 | goto out; | |
a5637186 | 393 | |
8e630837 ES |
394 | error = -EINVAL; |
395 | /* restricting fields must be appropriate for type */ | |
396 | switch (ops->type) { | |
397 | case ST_NONE: | |
398 | case ST_FS: | |
399 | if (sm->sm_ino || sm->sm_gen || sm->sm_agno) | |
400 | goto out; | |
401 | break; | |
402 | case ST_PERAG: | |
403 | if (sm->sm_ino || sm->sm_gen || | |
404 | sm->sm_agno >= mp->m_sb.sb_agcount) | |
405 | goto out; | |
406 | break; | |
407 | case ST_INODE: | |
408 | if (sm->sm_agno || (sm->sm_gen && !sm->sm_ino)) | |
409 | goto out; | |
410 | break; | |
411 | default: | |
412 | goto out; | |
413 | } | |
414 | ||
84d42ea6 DW |
415 | /* |
416 | * We only want to repair read-write v5+ filesystems. Defer the check | |
417 | * for ops->repair until after our scrub confirms that we need to | |
418 | * perform repairs so that we avoid failing due to not supporting | |
419 | * repairing an object that doesn't need repairs. | |
420 | */ | |
421 | if (sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) { | |
422 | error = -EOPNOTSUPP; | |
423 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
424 | goto out; | |
425 | ||
426 | error = -EROFS; | |
427 | if (mp->m_flags & XFS_MOUNT_RDONLY) | |
428 | goto out; | |
429 | } | |
a5637186 | 430 | |
0a085ddf ES |
431 | error = 0; |
432 | out: | |
433 | return error; | |
434 | } | |
435 | ||
84d42ea6 | 436 | #ifdef CONFIG_XFS_ONLINE_REPAIR |
1d8a748a | 437 | static inline void xchk_postmortem(struct xfs_scrub *sc) |
84d42ea6 DW |
438 | { |
439 | /* | |
440 | * Userspace asked us to repair something, we repaired it, rescanned | |
441 | * it, and the rescan says it's still broken. Scream about this in | |
442 | * the system logs. | |
443 | */ | |
444 | if ((sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) && | |
445 | (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT | | |
446 | XFS_SCRUB_OFLAG_XCORRUPT))) | |
b5e2196e | 447 | xrep_failure(sc->mp); |
84d42ea6 DW |
448 | } |
449 | #else | |
1d8a748a | 450 | static inline void xchk_postmortem(struct xfs_scrub *sc) |
84d42ea6 DW |
451 | { |
452 | /* | |
453 | * Userspace asked us to scrub something, it's broken, and we have no | |
454 | * way of fixing it. Scream in the logs. | |
455 | */ | |
456 | if (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT | | |
457 | XFS_SCRUB_OFLAG_XCORRUPT)) | |
458 | xfs_alert_ratelimited(sc->mp, | |
459 | "Corruption detected during scrub."); | |
460 | } | |
461 | #endif /* CONFIG_XFS_ONLINE_REPAIR */ | |
462 | ||
0a085ddf ES |
463 | /* Dispatch metadata scrubbing. */ |
464 | int | |
465 | xfs_scrub_metadata( | |
466 | struct xfs_inode *ip, | |
467 | struct xfs_scrub_metadata *sm) | |
468 | { | |
032d91f9 | 469 | struct xfs_scrub sc; |
0a085ddf ES |
470 | struct xfs_mount *mp = ip->i_mount; |
471 | bool try_harder = false; | |
84d42ea6 | 472 | bool already_fixed = false; |
0a085ddf ES |
473 | int error = 0; |
474 | ||
475 | BUILD_BUG_ON(sizeof(meta_scrub_ops) != | |
c517b3aa | 476 | (sizeof(struct xchk_meta_ops) * XFS_SCRUB_TYPE_NR)); |
0a085ddf | 477 | |
c517b3aa | 478 | trace_xchk_start(ip, sm, error); |
0a085ddf ES |
479 | |
480 | /* Forbidden if we are shut down or mounted norecovery. */ | |
481 | error = -ESHUTDOWN; | |
482 | if (XFS_FORCED_SHUTDOWN(mp)) | |
483 | goto out; | |
484 | error = -ENOTRECOVERABLE; | |
485 | if (mp->m_flags & XFS_MOUNT_NORECOVERY) | |
486 | goto out; | |
487 | ||
c517b3aa | 488 | error = xchk_validate_inputs(mp, sm); |
0a085ddf ES |
489 | if (error) |
490 | goto out; | |
491 | ||
c517b3aa | 492 | xchk_experimental_warning(mp); |
a5637186 DW |
493 | |
494 | retry_op: | |
495 | /* Set up for the operation. */ | |
496 | memset(&sc, 0, sizeof(sc)); | |
497 | sc.mp = ip->i_mount; | |
498 | sc.sm = sm; | |
0a085ddf | 499 | sc.ops = &meta_scrub_ops[sm->sm_type]; |
a5637186 | 500 | sc.try_harder = try_harder; |
b6c1beb9 | 501 | sc.sa.agno = NULLAGNUMBER; |
a5637186 DW |
502 | error = sc.ops->setup(&sc, ip); |
503 | if (error) | |
504 | goto out_teardown; | |
505 | ||
506 | /* Scrub for errors. */ | |
507 | error = sc.ops->scrub(&sc); | |
508 | if (!try_harder && error == -EDEADLOCK) { | |
509 | /* | |
510 | * Scrubbers return -EDEADLOCK to mean 'try harder'. | |
511 | * Tear down everything we hold, then set up again with | |
512 | * preparation for worst-case scenarios. | |
513 | */ | |
c517b3aa | 514 | error = xchk_teardown(&sc, ip, 0); |
a5637186 DW |
515 | if (error) |
516 | goto out; | |
517 | try_harder = true; | |
518 | goto retry_op; | |
519 | } else if (error) | |
520 | goto out_teardown; | |
521 | ||
84d42ea6 DW |
522 | if ((sc.sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) && !already_fixed) { |
523 | bool needs_fix; | |
524 | ||
525 | /* Let debug users force us into the repair routines. */ | |
526 | if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_FORCE_SCRUB_REPAIR)) | |
527 | sc.sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT; | |
528 | ||
529 | needs_fix = (sc.sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT | | |
530 | XFS_SCRUB_OFLAG_XCORRUPT | | |
531 | XFS_SCRUB_OFLAG_PREEN)); | |
532 | /* | |
533 | * If userspace asked for a repair but it wasn't necessary, | |
534 | * report that back to userspace. | |
535 | */ | |
536 | if (!needs_fix) { | |
537 | sc.sm->sm_flags |= XFS_SCRUB_OFLAG_NO_REPAIR_NEEDED; | |
538 | goto out_nofix; | |
539 | } | |
540 | ||
541 | /* | |
542 | * If it's broken, userspace wants us to fix it, and we haven't | |
543 | * already tried to fix it, then attempt a repair. | |
544 | */ | |
b5e2196e | 545 | error = xrep_attempt(ip, &sc, &already_fixed); |
84d42ea6 DW |
546 | if (error == -EAGAIN) { |
547 | if (sc.try_harder) | |
548 | try_harder = true; | |
c517b3aa | 549 | error = xchk_teardown(&sc, ip, 0); |
84d42ea6 | 550 | if (error) { |
b5e2196e | 551 | xrep_failure(mp); |
84d42ea6 DW |
552 | goto out; |
553 | } | |
554 | goto retry_op; | |
555 | } | |
556 | } | |
a5637186 | 557 | |
84d42ea6 | 558 | out_nofix: |
c517b3aa | 559 | xchk_postmortem(&sc); |
a5637186 | 560 | out_teardown: |
c517b3aa | 561 | error = xchk_teardown(&sc, ip, error); |
a5637186 | 562 | out: |
c517b3aa | 563 | trace_xchk_done(ip, sm, error); |
a5637186 DW |
564 | if (error == -EFSCORRUPTED || error == -EFSBADCRC) { |
565 | sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT; | |
566 | error = 0; | |
567 | } | |
568 | return error; | |
36fd6e86 | 569 | } |