2 * Copyright (C) 2017 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "xfs_shared.h"
23 #include "xfs_format.h"
24 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_defer.h"
27 #include "xfs_btree.h"
29 #include "xfs_log_format.h"
30 #include "xfs_trans.h"
32 #include "xfs_inode.h"
33 #include "xfs_icache.h"
34 #include "xfs_itable.h"
35 #include "xfs_alloc.h"
36 #include "xfs_alloc_btree.h"
38 #include "xfs_bmap_btree.h"
39 #include "xfs_ialloc.h"
40 #include "xfs_ialloc_btree.h"
41 #include "xfs_refcount.h"
42 #include "xfs_refcount_btree.h"
44 #include "xfs_rmap_btree.h"
45 #include "scrub/xfs_scrub.h"
46 #include "scrub/scrub.h"
47 #include "scrub/common.h"
48 #include "scrub/trace.h"
49 #include "scrub/btree.h"
52 * Online Scrub and Repair
54 * Traditionally, XFS (the kernel driver) did not know how to check or
55 * repair on-disk data structures. That task was left to the xfs_check
56 * and xfs_repair tools, both of which require taking the filesystem
57 * offline for a thorough but time consuming examination. Online
58 * scrub & repair, on the other hand, enables us to check the metadata
59 * for obvious errors while carefully stepping around the filesystem's
60 * ongoing operations, locking rules, etc.
62 * Given that most XFS metadata consist of records stored in a btree,
63 * most of the checking functions iterate the btree blocks themselves
64 * looking for irregularities. When a record block is encountered, each
65 * record can be checked for obviously bad values. Record values can
66 * also be cross-referenced against other btrees to look for potential
67 * misunderstandings between pieces of metadata.
69 * It is expected that the checkers responsible for per-AG metadata
70 * structures will lock the AG headers (AGI, AGF, AGFL), iterate the
71 * metadata structure, and perform any relevant cross-referencing before
72 * unlocking the AG and returning the results to userspace. These
73 * scrubbers must not keep an AG locked for too long to avoid tying up
74 * the block and inode allocators.
76 * Block maps and b-trees rooted in an inode present a special challenge
77 * because they can involve extents from any AG. The general scrubber
78 * structure of lock -> check -> xref -> unlock still holds, but AG
79 * locking order rules /must/ be obeyed to avoid deadlocks. The
80 * ordering rule, of course, is that we must lock in increasing AG
81 * order. Helper functions are provided to track which AG headers we've
82 * already locked. If we detect an imminent locking order violation, we
83 * can signal a potential deadlock, in which case the scrubber can jump
84 * out to the top level, lock all the AGs in order, and retry the scrub.
86 * For file data (directories, extended attributes, symlinks) scrub, we
87 * can simply lock the inode and walk the data. For btree data
88 * (directories and attributes) we follow the same btree-scrubbing
89 * strategy outlined previously to check the records.
91 * We use a bit of trickery with transactions to avoid buffer deadlocks
92 * if there is a cycle in the metadata. The basic problem is that
93 * travelling down a btree involves locking the current buffer at each
94 * tree level. If a pointer should somehow point back to a buffer that
95 * we've already examined, we will deadlock due to the second buffer
96 * locking attempt. Note however that grabbing a buffer in transaction
97 * context links the locked buffer to the transaction. If we try to
98 * re-grab the buffer in the context of the same transaction, we avoid
99 * the second lock attempt and continue. Between the verifier and the
100 * scrubber, something will notice that something is amiss and report
101 * the corruption. Therefore, each scrubber will allocate an empty
102 * transaction, attach buffers to it, and cancel the transaction at the
103 * end of the scrub run. Cancelling a non-dirty transaction simply
104 * unlocks the buffers.
106 * There are four pieces of data that scrub can communicate to
107 * userspace. The first is the error code (errno), which can be used to
108 * communicate operational errors in performing the scrub. There are
109 * also three flags that can be set in the scrub context. If the data
110 * structure itself is corrupt, the CORRUPT flag will be set. If
111 * the metadata is correct but otherwise suboptimal, the PREEN flag
116 * Scrub probe -- userspace uses this to probe if we're willing to scrub
117 * or repair a given mountpoint. This will be used by xfs_scrub to
118 * probe the kernel's abilities to scrub (and repair) the metadata. We
119 * do this by validating the ioctl inputs from userspace, preparing the
120 * filesystem for a scrub (or a repair) operation, and immediately
121 * returning to userspace. Userspace can use the returned errno and
122 * structure state to decide (in broad terms) if scrub/repair are
123 * supported by the running kernel.
127 struct xfs_scrub_context *sc)
131 if (sc->sm->sm_ino || sc->sm->sm_agno)
133 if (xfs_scrub_should_terminate(sc, &error))
139 /* Scrub setup and teardown */
141 /* Free all the resources and finish the transactions. */
144 struct xfs_scrub_context *sc,
145 struct xfs_inode *ip_in,
148 xfs_scrub_ag_free(sc, &sc->sa);
150 xfs_trans_cancel(sc->tp);
154 xfs_iunlock(sc->ip, sc->ilock_flags);
155 if (sc->ip != ip_in &&
156 !xfs_internal_inum(sc->mp, sc->ip->i_ino))
167 /* Scrubbing dispatch. */
169 static const struct xfs_scrub_meta_ops meta_scrub_ops[] = {
170 { /* ioctl presence test */
171 .setup = xfs_scrub_setup_fs,
172 .scrub = xfs_scrub_probe,
175 .setup = xfs_scrub_setup_ag_header,
176 .scrub = xfs_scrub_superblock,
179 .setup = xfs_scrub_setup_ag_header,
180 .scrub = xfs_scrub_agf,
183 .setup = xfs_scrub_setup_ag_header,
184 .scrub = xfs_scrub_agfl,
187 .setup = xfs_scrub_setup_ag_header,
188 .scrub = xfs_scrub_agi,
191 .setup = xfs_scrub_setup_ag_allocbt,
192 .scrub = xfs_scrub_bnobt,
195 .setup = xfs_scrub_setup_ag_allocbt,
196 .scrub = xfs_scrub_cntbt,
199 .setup = xfs_scrub_setup_ag_iallocbt,
200 .scrub = xfs_scrub_inobt,
203 .setup = xfs_scrub_setup_ag_iallocbt,
204 .scrub = xfs_scrub_finobt,
205 .has = xfs_sb_version_hasfinobt,
208 .setup = xfs_scrub_setup_ag_rmapbt,
209 .scrub = xfs_scrub_rmapbt,
210 .has = xfs_sb_version_hasrmapbt,
213 .setup = xfs_scrub_setup_ag_refcountbt,
214 .scrub = xfs_scrub_refcountbt,
215 .has = xfs_sb_version_hasreflink,
218 .setup = xfs_scrub_setup_inode,
219 .scrub = xfs_scrub_inode,
221 { /* inode data fork */
222 .setup = xfs_scrub_setup_inode_bmap,
223 .scrub = xfs_scrub_bmap_data,
225 { /* inode attr fork */
226 .setup = xfs_scrub_setup_inode_bmap,
227 .scrub = xfs_scrub_bmap_attr,
229 { /* inode CoW fork */
230 .setup = xfs_scrub_setup_inode_bmap,
231 .scrub = xfs_scrub_bmap_cow,
234 .setup = xfs_scrub_setup_directory,
235 .scrub = xfs_scrub_directory,
237 { /* extended attributes */
238 .setup = xfs_scrub_setup_xattr,
239 .scrub = xfs_scrub_xattr,
241 { /* symbolic link */
242 .setup = xfs_scrub_setup_symlink,
243 .scrub = xfs_scrub_symlink,
245 { /* parent pointers */
246 .setup = xfs_scrub_setup_parent,
247 .scrub = xfs_scrub_parent,
249 { /* realtime bitmap */
250 .setup = xfs_scrub_setup_rt,
251 .scrub = xfs_scrub_rtbitmap,
252 .has = xfs_sb_version_hasrealtime,
254 { /* realtime summary */
255 .setup = xfs_scrub_setup_rt,
256 .scrub = xfs_scrub_rtsummary,
257 .has = xfs_sb_version_hasrealtime,
260 .setup = xfs_scrub_setup_quota,
261 .scrub = xfs_scrub_quota,
264 .setup = xfs_scrub_setup_quota,
265 .scrub = xfs_scrub_quota,
267 { /* project quota */
268 .setup = xfs_scrub_setup_quota,
269 .scrub = xfs_scrub_quota,
273 /* This isn't a stable feature, warn once per day. */
275 xfs_scrub_experimental_warning(
276 struct xfs_mount *mp)
278 static struct ratelimit_state scrub_warning = RATELIMIT_STATE_INIT(
279 "xfs_scrub_warning", 86400 * HZ, 1);
280 ratelimit_set_flags(&scrub_warning, RATELIMIT_MSG_ON_RELEASE);
282 if (__ratelimit(&scrub_warning))
284 "EXPERIMENTAL online scrub feature in use. Use at your own risk!");
287 /* Dispatch metadata scrubbing. */
290 struct xfs_inode *ip,
291 struct xfs_scrub_metadata *sm)
293 struct xfs_scrub_context sc;
294 struct xfs_mount *mp = ip->i_mount;
295 const struct xfs_scrub_meta_ops *ops;
296 bool try_harder = false;
299 trace_xfs_scrub_start(ip, sm, error);
301 /* Forbidden if we are shut down or mounted norecovery. */
303 if (XFS_FORCED_SHUTDOWN(mp))
305 error = -ENOTRECOVERABLE;
306 if (mp->m_flags & XFS_MOUNT_NORECOVERY)
309 /* Check our inputs. */
311 sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT;
312 if (sm->sm_flags & ~XFS_SCRUB_FLAGS_IN)
314 if (memchr_inv(sm->sm_reserved, 0, sizeof(sm->sm_reserved)))
317 /* Do we know about this type of metadata? */
319 if (sm->sm_type >= XFS_SCRUB_TYPE_NR)
321 ops = &meta_scrub_ops[sm->sm_type];
322 if (ops->scrub == NULL)
326 * We won't scrub any filesystem that doesn't have the ability
327 * to record unwritten extents. The option was made default in
328 * 2003, removed from mkfs in 2007, and cannot be disabled in
329 * v5, so if we find a filesystem without this flag it's either
330 * really old or totally unsupported. Avoid it either way.
331 * We also don't support v1-v3 filesystems, which aren't
335 if (!xfs_sb_version_hasextflgbit(&mp->m_sb))
338 /* Does this fs even support this type of metadata? */
340 if (ops->has && !ops->has(&mp->m_sb))
343 /* We don't know how to repair anything yet. */
345 if (sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)
348 xfs_scrub_experimental_warning(mp);
351 /* Set up for the operation. */
352 memset(&sc, 0, sizeof(sc));
356 sc.try_harder = try_harder;
357 sc.sa.agno = NULLAGNUMBER;
358 error = sc.ops->setup(&sc, ip);
362 /* Scrub for errors. */
363 error = sc.ops->scrub(&sc);
364 if (!try_harder && error == -EDEADLOCK) {
366 * Scrubbers return -EDEADLOCK to mean 'try harder'.
367 * Tear down everything we hold, then set up again with
368 * preparation for worst-case scenarios.
370 error = xfs_scrub_teardown(&sc, ip, 0);
378 if (sc.sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
379 XFS_SCRUB_OFLAG_XCORRUPT))
380 xfs_alert_ratelimited(mp, "Corruption detected during scrub.");
383 error = xfs_scrub_teardown(&sc, ip, error);
385 trace_xfs_scrub_done(ip, sm, error);
386 if (error == -EFSCORRUPTED || error == -EFSBADCRC) {
387 sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;