1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017-2023 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <djwong@kernel.org>
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_mount.h"
12 #include "xfs_log_format.h"
13 #include "xfs_trans.h"
14 #include "xfs_inode.h"
15 #include "xfs_quota.h"
17 #include "xfs_scrub.h"
18 #include "xfs_buf_mem.h"
20 #include "xfs_exchrange.h"
21 #include "xfs_exchmaps.h"
23 #include "xfs_parent.h"
24 #include "xfs_icache.h"
25 #include "scrub/scrub.h"
26 #include "scrub/common.h"
27 #include "scrub/trace.h"
28 #include "scrub/repair.h"
29 #include "scrub/health.h"
30 #include "scrub/stats.h"
31 #include "scrub/xfile.h"
32 #include "scrub/tempfile.h"
33 #include "scrub/orphanage.h"
36 * Online Scrub and Repair
38 * Traditionally, XFS (the kernel driver) did not know how to check or
39 * repair on-disk data structures. That task was left to the xfs_check
40 * and xfs_repair tools, both of which require taking the filesystem
41 * offline for a thorough but time consuming examination. Online
42 * scrub & repair, on the other hand, enables us to check the metadata
43 * for obvious errors while carefully stepping around the filesystem's
44 * ongoing operations, locking rules, etc.
46 * Given that most XFS metadata consist of records stored in a btree,
47 * most of the checking functions iterate the btree blocks themselves
48 * looking for irregularities. When a record block is encountered, each
49 * record can be checked for obviously bad values. Record values can
50 * also be cross-referenced against other btrees to look for potential
51 * misunderstandings between pieces of metadata.
53 * It is expected that the checkers responsible for per-AG metadata
54 * structures will lock the AG headers (AGI, AGF, AGFL), iterate the
55 * metadata structure, and perform any relevant cross-referencing before
56 * unlocking the AG and returning the results to userspace. These
57 * scrubbers must not keep an AG locked for too long to avoid tying up
58 * the block and inode allocators.
60 * Block maps and b-trees rooted in an inode present a special challenge
61 * because they can involve extents from any AG. The general scrubber
62 * structure of lock -> check -> xref -> unlock still holds, but AG
63 * locking order rules /must/ be obeyed to avoid deadlocks. The
64 * ordering rule, of course, is that we must lock in increasing AG
65 * order. Helper functions are provided to track which AG headers we've
66 * already locked. If we detect an imminent locking order violation, we
67 * can signal a potential deadlock, in which case the scrubber can jump
68 * out to the top level, lock all the AGs in order, and retry the scrub.
70 * For file data (directories, extended attributes, symlinks) scrub, we
71 * can simply lock the inode and walk the data. For btree data
72 * (directories and attributes) we follow the same btree-scrubbing
73 * strategy outlined previously to check the records.
75 * We use a bit of trickery with transactions to avoid buffer deadlocks
76 * if there is a cycle in the metadata. The basic problem is that
77 * travelling down a btree involves locking the current buffer at each
78 * tree level. If a pointer should somehow point back to a buffer that
79 * we've already examined, we will deadlock due to the second buffer
80 * locking attempt. Note however that grabbing a buffer in transaction
81 * context links the locked buffer to the transaction. If we try to
82 * re-grab the buffer in the context of the same transaction, we avoid
83 * the second lock attempt and continue. Between the verifier and the
84 * scrubber, something will notice that something is amiss and report
85 * the corruption. Therefore, each scrubber will allocate an empty
86 * transaction, attach buffers to it, and cancel the transaction at the
87 * end of the scrub run. Cancelling a non-dirty transaction simply
88 * unlocks the buffers.
90 * There are four pieces of data that scrub can communicate to
91 * userspace. The first is the error code (errno), which can be used to
92 * communicate operational errors in performing the scrub. There are
93 * also three flags that can be set in the scrub context. If the data
94 * structure itself is corrupt, the CORRUPT flag will be set. If
95 * the metadata is correct but otherwise suboptimal, the PREEN flag
98 * We perform secondary validation of filesystem metadata by
99 * cross-referencing every record with all other available metadata.
100 * For example, for block mapping extents, we verify that there are no
101 * records in the free space and inode btrees corresponding to that
102 * space extent and that there is a corresponding entry in the reverse
103 * mapping btree. Inconsistent metadata is noted by setting the
104 * XCORRUPT flag; btree query function errors are noted by setting the
105 * XFAIL flag and deleting the cursor to prevent further attempts to
106 * cross-reference with a defective btree.
108 * If a piece of metadata proves corrupt or suboptimal, the userspace
109 * program can ask the kernel to apply some tender loving care (TLC) to
110 * the metadata object by setting the REPAIR flag and re-calling the
111 * scrub ioctl. "Corruption" is defined by metadata violating the
112 * on-disk specification; operations cannot continue if the violation is
113 * left untreated. It is possible for XFS to continue if an object is
114 * "suboptimal", however performance may be degraded. Repairs are
115 * usually performed by rebuilding the metadata entirely out of
116 * redundant metadata. Optimizing, on the other hand, can sometimes be
117 * done without rebuilding entire structures.
119 * Generally speaking, the repair code has the following code structure:
120 * Lock -> scrub -> repair -> commit -> re-lock -> re-scrub -> unlock.
121 * The first check helps us figure out if we need to rebuild or simply
122 * optimize the structure so that the rebuild knows what to do. The
123 * second check evaluates the completeness of the repair; that is what
124 * is reported to userspace.
126 * A quick note on symbol prefixes:
127 * - "xfs_" are general XFS symbols.
128 * - "xchk_" are symbols related to metadata checking.
129 * - "xrep_" are symbols related to metadata repair.
130 * - "xfs_scrub_" are symbols that tie online fsck to the rest of XFS.
134 * Scrub probe -- userspace uses this to probe if we're willing to scrub
135 * or repair a given mountpoint. This will be used by xfs_scrub to
136 * probe the kernel's abilities to scrub (and repair) the metadata. We
137 * do this by validating the ioctl inputs from userspace, preparing the
138 * filesystem for a scrub (or a repair) operation, and immediately
139 * returning to userspace. Userspace can use the returned errno and
140 * structure state to decide (in broad terms) if scrub/repair are
141 * supported by the running kernel.
145 struct xfs_scrub
*sc
)
149 if (xchk_should_terminate(sc
, &error
))
155 /* Scrub setup and teardown */
158 xchk_fsgates_disable(
159 struct xfs_scrub
*sc
)
161 if (!(sc
->flags
& XCHK_FSGATES_ALL
))
164 trace_xchk_fsgates_disable(sc
, sc
->flags
& XCHK_FSGATES_ALL
);
166 if (sc
->flags
& XCHK_FSGATES_DRAIN
)
167 xfs_drain_wait_disable();
169 if (sc
->flags
& XCHK_FSGATES_QUOTA
)
170 xfs_dqtrx_hook_disable();
172 if (sc
->flags
& XCHK_FSGATES_DIRENTS
)
173 xfs_dir_hook_disable();
175 if (sc
->flags
& XCHK_FSGATES_RMAP
)
176 xfs_rmap_hook_disable();
178 sc
->flags
&= ~XCHK_FSGATES_ALL
;
181 /* Free the resources associated with a scrub subtype. */
183 xchk_scrub_free_subord(
184 struct xfs_scrub_subord
*sub
)
186 struct xfs_scrub
*sc
= sub
->parent_sc
;
188 ASSERT(sc
->ip
== sub
->sc
.ip
);
189 ASSERT(sc
->orphanage
== sub
->sc
.orphanage
);
190 ASSERT(sc
->tempip
== sub
->sc
.tempip
);
192 sc
->sm
->sm_type
= sub
->old_smtype
;
193 sc
->sm
->sm_flags
= sub
->old_smflags
|
194 (sc
->sm
->sm_flags
& XFS_SCRUB_FLAGS_OUT
);
198 if (sub
->sc
.buf_cleanup
)
199 sub
->sc
.buf_cleanup(sub
->sc
.buf
);
203 xmbuf_free(sub
->sc
.xmbtp
);
205 xfile_destroy(sub
->sc
.xfile
);
207 sc
->ilock_flags
= sub
->sc
.ilock_flags
;
208 sc
->orphanage_ilock_flags
= sub
->sc
.orphanage_ilock_flags
;
209 sc
->temp_ilock_flags
= sub
->sc
.temp_ilock_flags
;
214 /* Free all the resources and finish the transactions. */
217 struct xfs_scrub
*sc
,
220 xchk_ag_free(sc
, &sc
->sa
);
222 if (error
== 0 && (sc
->sm
->sm_flags
& XFS_SCRUB_IFLAG_REPAIR
))
223 error
= xfs_trans_commit(sc
->tp
);
225 xfs_trans_cancel(sc
->tp
);
230 xchk_iunlock(sc
, sc
->ilock_flags
);
231 xchk_irele(sc
, sc
->ip
);
234 if (sc
->flags
& XCHK_HAVE_FREEZE_PROT
) {
235 sc
->flags
&= ~XCHK_HAVE_FREEZE_PROT
;
236 mnt_drop_write_file(sc
->file
);
239 xmbuf_free(sc
->xmbtp
);
243 xfile_destroy(sc
->xfile
);
248 sc
->buf_cleanup(sc
->buf
);
250 sc
->buf_cleanup
= NULL
;
254 xrep_tempfile_rele(sc
);
255 xrep_orphanage_rele(sc
);
256 xchk_fsgates_disable(sc
);
260 /* Scrubbing dispatch. */
262 static const struct xchk_meta_ops meta_scrub_ops
[] = {
263 [XFS_SCRUB_TYPE_PROBE
] = { /* ioctl presence test */
265 .setup
= xchk_setup_fs
,
267 .repair
= xrep_probe
,
269 [XFS_SCRUB_TYPE_SB
] = { /* superblock */
271 .setup
= xchk_setup_agheader
,
272 .scrub
= xchk_superblock
,
273 .repair
= xrep_superblock
,
275 [XFS_SCRUB_TYPE_AGF
] = { /* agf */
277 .setup
= xchk_setup_agheader
,
281 [XFS_SCRUB_TYPE_AGFL
]= { /* agfl */
283 .setup
= xchk_setup_agheader
,
287 [XFS_SCRUB_TYPE_AGI
] = { /* agi */
289 .setup
= xchk_setup_agheader
,
293 [XFS_SCRUB_TYPE_BNOBT
] = { /* bnobt */
295 .setup
= xchk_setup_ag_allocbt
,
296 .scrub
= xchk_allocbt
,
297 .repair
= xrep_allocbt
,
298 .repair_eval
= xrep_revalidate_allocbt
,
300 [XFS_SCRUB_TYPE_CNTBT
] = { /* cntbt */
302 .setup
= xchk_setup_ag_allocbt
,
303 .scrub
= xchk_allocbt
,
304 .repair
= xrep_allocbt
,
305 .repair_eval
= xrep_revalidate_allocbt
,
307 [XFS_SCRUB_TYPE_INOBT
] = { /* inobt */
309 .setup
= xchk_setup_ag_iallocbt
,
310 .scrub
= xchk_iallocbt
,
311 .repair
= xrep_iallocbt
,
312 .repair_eval
= xrep_revalidate_iallocbt
,
314 [XFS_SCRUB_TYPE_FINOBT
] = { /* finobt */
316 .setup
= xchk_setup_ag_iallocbt
,
317 .scrub
= xchk_iallocbt
,
318 .has
= xfs_has_finobt
,
319 .repair
= xrep_iallocbt
,
320 .repair_eval
= xrep_revalidate_iallocbt
,
322 [XFS_SCRUB_TYPE_RMAPBT
] = { /* rmapbt */
324 .setup
= xchk_setup_ag_rmapbt
,
325 .scrub
= xchk_rmapbt
,
326 .has
= xfs_has_rmapbt
,
327 .repair
= xrep_rmapbt
,
329 [XFS_SCRUB_TYPE_REFCNTBT
] = { /* refcountbt */
331 .setup
= xchk_setup_ag_refcountbt
,
332 .scrub
= xchk_refcountbt
,
333 .has
= xfs_has_reflink
,
334 .repair
= xrep_refcountbt
,
336 [XFS_SCRUB_TYPE_INODE
] = { /* inode record */
338 .setup
= xchk_setup_inode
,
340 .repair
= xrep_inode
,
342 [XFS_SCRUB_TYPE_BMBTD
] = { /* inode data fork */
344 .setup
= xchk_setup_inode_bmap
,
345 .scrub
= xchk_bmap_data
,
346 .repair
= xrep_bmap_data
,
348 [XFS_SCRUB_TYPE_BMBTA
] = { /* inode attr fork */
350 .setup
= xchk_setup_inode_bmap
,
351 .scrub
= xchk_bmap_attr
,
352 .repair
= xrep_bmap_attr
,
354 [XFS_SCRUB_TYPE_BMBTC
] = { /* inode CoW fork */
356 .setup
= xchk_setup_inode_bmap
,
357 .scrub
= xchk_bmap_cow
,
358 .repair
= xrep_bmap_cow
,
360 [XFS_SCRUB_TYPE_DIR
] = { /* directory */
362 .setup
= xchk_setup_directory
,
363 .scrub
= xchk_directory
,
364 .repair
= xrep_directory
,
366 [XFS_SCRUB_TYPE_XATTR
] = { /* extended attributes */
368 .setup
= xchk_setup_xattr
,
370 .repair
= xrep_xattr
,
372 [XFS_SCRUB_TYPE_SYMLINK
] = { /* symbolic link */
374 .setup
= xchk_setup_symlink
,
375 .scrub
= xchk_symlink
,
376 .repair
= xrep_symlink
,
378 [XFS_SCRUB_TYPE_PARENT
] = { /* parent pointers */
380 .setup
= xchk_setup_parent
,
381 .scrub
= xchk_parent
,
382 .repair
= xrep_parent
,
384 [XFS_SCRUB_TYPE_RTBITMAP
] = { /* realtime bitmap */
386 .setup
= xchk_setup_rtbitmap
,
387 .scrub
= xchk_rtbitmap
,
388 .repair
= xrep_rtbitmap
,
390 [XFS_SCRUB_TYPE_RTSUM
] = { /* realtime summary */
392 .setup
= xchk_setup_rtsummary
,
393 .scrub
= xchk_rtsummary
,
394 .repair
= xrep_rtsummary
,
396 [XFS_SCRUB_TYPE_UQUOTA
] = { /* user quota */
398 .setup
= xchk_setup_quota
,
400 .repair
= xrep_quota
,
402 [XFS_SCRUB_TYPE_GQUOTA
] = { /* group quota */
404 .setup
= xchk_setup_quota
,
406 .repair
= xrep_quota
,
408 [XFS_SCRUB_TYPE_PQUOTA
] = { /* project quota */
410 .setup
= xchk_setup_quota
,
412 .repair
= xrep_quota
,
414 [XFS_SCRUB_TYPE_FSCOUNTERS
] = { /* fs summary counters */
416 .setup
= xchk_setup_fscounters
,
417 .scrub
= xchk_fscounters
,
418 .repair
= xrep_fscounters
,
420 [XFS_SCRUB_TYPE_QUOTACHECK
] = { /* quota counters */
422 .setup
= xchk_setup_quotacheck
,
423 .scrub
= xchk_quotacheck
,
424 .repair
= xrep_quotacheck
,
426 [XFS_SCRUB_TYPE_NLINKS
] = { /* inode link counts */
428 .setup
= xchk_setup_nlinks
,
429 .scrub
= xchk_nlinks
,
430 .repair
= xrep_nlinks
,
432 [XFS_SCRUB_TYPE_HEALTHY
] = { /* fs healthy; clean all reminders */
434 .setup
= xchk_setup_fs
,
435 .scrub
= xchk_health_record
,
436 .repair
= xrep_notsupported
,
438 [XFS_SCRUB_TYPE_DIRTREE
] = { /* directory tree structure */
440 .setup
= xchk_setup_dirtree
,
441 .scrub
= xchk_dirtree
,
442 .has
= xfs_has_parent
,
443 .repair
= xrep_dirtree
,
448 xchk_validate_inputs(
449 struct xfs_mount
*mp
,
450 struct xfs_scrub_metadata
*sm
)
453 const struct xchk_meta_ops
*ops
;
456 /* Check our inputs. */
457 sm
->sm_flags
&= ~XFS_SCRUB_FLAGS_OUT
;
458 if (sm
->sm_flags
& ~XFS_SCRUB_FLAGS_IN
)
460 /* sm_reserved[] must be zero */
461 if (memchr_inv(sm
->sm_reserved
, 0, sizeof(sm
->sm_reserved
)))
465 /* Do we know about this type of metadata? */
466 if (sm
->sm_type
>= XFS_SCRUB_TYPE_NR
)
468 ops
= &meta_scrub_ops
[sm
->sm_type
];
469 if (ops
->setup
== NULL
|| ops
->scrub
== NULL
)
471 /* Does this fs even support this type of metadata? */
472 if (ops
->has
&& !ops
->has(mp
))
476 /* restricting fields must be appropriate for type */
480 if (sm
->sm_ino
|| sm
->sm_gen
|| sm
->sm_agno
)
484 if (sm
->sm_ino
|| sm
->sm_gen
||
485 sm
->sm_agno
>= mp
->m_sb
.sb_agcount
)
489 if (sm
->sm_agno
|| (sm
->sm_gen
&& !sm
->sm_ino
))
496 /* No rebuild without repair. */
497 if ((sm
->sm_flags
& XFS_SCRUB_IFLAG_FORCE_REBUILD
) &&
498 !(sm
->sm_flags
& XFS_SCRUB_IFLAG_REPAIR
))
502 * We only want to repair read-write v5+ filesystems. Defer the check
503 * for ops->repair until after our scrub confirms that we need to
504 * perform repairs so that we avoid failing due to not supporting
505 * repairing an object that doesn't need repairs.
507 if (sm
->sm_flags
& XFS_SCRUB_IFLAG_REPAIR
) {
509 if (!xfs_has_crc(mp
))
513 if (xfs_is_readonly(mp
))
522 #ifdef CONFIG_XFS_ONLINE_REPAIR
523 static inline void xchk_postmortem(struct xfs_scrub
*sc
)
526 * Userspace asked us to repair something, we repaired it, rescanned
527 * it, and the rescan says it's still broken. Scream about this in
530 if ((sc
->sm
->sm_flags
& XFS_SCRUB_IFLAG_REPAIR
) &&
531 (sc
->sm
->sm_flags
& (XFS_SCRUB_OFLAG_CORRUPT
|
532 XFS_SCRUB_OFLAG_XCORRUPT
)))
533 xrep_failure(sc
->mp
);
536 static inline void xchk_postmortem(struct xfs_scrub
*sc
)
539 * Userspace asked us to scrub something, it's broken, and we have no
540 * way of fixing it. Scream in the logs.
542 if (sc
->sm
->sm_flags
& (XFS_SCRUB_OFLAG_CORRUPT
|
543 XFS_SCRUB_OFLAG_XCORRUPT
))
544 xfs_alert_ratelimited(sc
->mp
,
545 "Corruption detected during scrub.");
547 #endif /* CONFIG_XFS_ONLINE_REPAIR */
550 * Create a new scrub context from an existing one, but with a different scrub
553 struct xfs_scrub_subord
*
554 xchk_scrub_create_subord(
555 struct xfs_scrub
*sc
,
556 unsigned int subtype
)
558 struct xfs_scrub_subord
*sub
;
560 sub
= kzalloc(sizeof(*sub
), XCHK_GFP_FLAGS
);
562 return ERR_PTR(-ENOMEM
);
564 sub
->old_smtype
= sc
->sm
->sm_type
;
565 sub
->old_smflags
= sc
->sm
->sm_flags
;
567 memcpy(&sub
->sc
, sc
, sizeof(struct xfs_scrub
));
568 sub
->sc
.ops
= &meta_scrub_ops
[subtype
];
569 sub
->sc
.sm
->sm_type
= subtype
;
570 sub
->sc
.sm
->sm_flags
&= ~XFS_SCRUB_FLAGS_OUT
;
572 sub
->sc
.buf_cleanup
= NULL
;
573 sub
->sc
.xfile
= NULL
;
574 sub
->sc
.xmbtp
= NULL
;
579 /* Dispatch metadata scrubbing. */
583 struct xfs_scrub_metadata
*sm
)
585 struct xchk_stats_run run
= { };
586 struct xfs_scrub
*sc
;
587 struct xfs_mount
*mp
= XFS_I(file_inode(file
))->i_mount
;
591 BUILD_BUG_ON(sizeof(meta_scrub_ops
) !=
592 (sizeof(struct xchk_meta_ops
) * XFS_SCRUB_TYPE_NR
));
594 trace_xchk_start(XFS_I(file_inode(file
)), sm
, error
);
596 /* Forbidden if we are shut down or mounted norecovery. */
598 if (xfs_is_shutdown(mp
))
600 error
= -ENOTRECOVERABLE
;
601 if (xfs_has_norecovery(mp
))
604 error
= xchk_validate_inputs(mp
, sm
);
608 xfs_warn_mount(mp
, XFS_OPSTATE_WARNED_SCRUB
,
609 "EXPERIMENTAL online scrub feature in use. Use at your own risk!");
611 sc
= kzalloc(sizeof(struct xfs_scrub
), XCHK_GFP_FLAGS
);
620 sc
->ops
= &meta_scrub_ops
[sm
->sm_type
];
621 sc
->sick_mask
= xchk_health_mask_for_scrub_type(sm
->sm_type
);
622 sc
->relax
= INIT_XCHK_RELAX
;
625 * When repairs are allowed, prevent freezing or readonly remount while
626 * scrub is running with a real transaction.
628 if (sm
->sm_flags
& XFS_SCRUB_IFLAG_REPAIR
) {
629 error
= mnt_want_write_file(sc
->file
);
633 sc
->flags
|= XCHK_HAVE_FREEZE_PROT
;
636 /* Set up for the operation. */
637 error
= sc
->ops
->setup(sc
);
638 if (error
== -EDEADLOCK
&& !(sc
->flags
& XCHK_TRY_HARDER
))
640 if (error
== -ECHRNG
&& !(sc
->flags
& XCHK_NEED_DRAIN
))
645 /* Scrub for errors. */
646 check_start
= xchk_stats_now();
647 if ((sc
->flags
& XREP_ALREADY_FIXED
) && sc
->ops
->repair_eval
!= NULL
)
648 error
= sc
->ops
->repair_eval(sc
);
650 error
= sc
->ops
->scrub(sc
);
651 run
.scrub_ns
+= xchk_stats_elapsed_ns(check_start
);
652 if (error
== -EDEADLOCK
&& !(sc
->flags
& XCHK_TRY_HARDER
))
654 if (error
== -ECHRNG
&& !(sc
->flags
& XCHK_NEED_DRAIN
))
656 if (error
|| (sm
->sm_flags
& XFS_SCRUB_OFLAG_INCOMPLETE
))
659 xchk_update_health(sc
);
661 if (xchk_could_repair(sc
)) {
663 * If userspace asked for a repair but it wasn't necessary,
664 * report that back to userspace.
666 if (!xrep_will_attempt(sc
)) {
667 sc
->sm
->sm_flags
|= XFS_SCRUB_OFLAG_NO_REPAIR_NEEDED
;
672 * If it's broken, userspace wants us to fix it, and we haven't
673 * already tried to fix it, then attempt a repair.
675 error
= xrep_attempt(sc
, &run
);
676 if (error
== -EAGAIN
) {
678 * Either the repair function succeeded or it couldn't
679 * get all the resources it needs; either way, we go
680 * back to the beginning and call the scrub function.
682 error
= xchk_teardown(sc
, 0);
694 error
= xchk_teardown(sc
, error
);
696 if (error
!= -ENOENT
)
697 xchk_stats_merge(mp
, sm
, &run
);
700 trace_xchk_done(XFS_I(file_inode(file
)), sm
, error
);
701 if (error
== -EFSCORRUPTED
|| error
== -EFSBADCRC
) {
702 sm
->sm_flags
|= XFS_SCRUB_OFLAG_CORRUPT
;
707 error
= xchk_teardown(sc
, 0);
710 sc
->flags
|= XCHK_NEED_DRAIN
;
715 * Scrubbers return -EDEADLOCK to mean 'try harder'. Tear down
716 * everything we hold, then set up again with preparation for
717 * worst-case scenarios.
719 error
= xchk_teardown(sc
, 0);
722 sc
->flags
|= XCHK_TRY_HARDER
;
727 /* Scrub one aspect of one piece of metadata. */
729 xfs_ioc_scrub_metadata(
733 struct xfs_scrub_metadata scrub
;
736 if (!capable(CAP_SYS_ADMIN
))
739 if (copy_from_user(&scrub
, arg
, sizeof(scrub
)))
742 error
= xfs_scrub_metadata(file
, &scrub
);
746 if (copy_to_user(arg
, &scrub
, sizeof(scrub
)))
752 /* Decide if there have been any scrub failures up to this point. */
754 xfs_scrubv_check_barrier(
755 struct xfs_mount
*mp
,
756 const struct xfs_scrub_vec
*vectors
,
757 const struct xfs_scrub_vec
*stop_vec
)
759 const struct xfs_scrub_vec
*v
;
762 failmask
= stop_vec
->sv_flags
& XFS_SCRUB_FLAGS_OUT
;
764 for (v
= vectors
; v
< stop_vec
; v
++) {
765 if (v
->sv_type
== XFS_SCRUB_TYPE_BARRIER
)
769 * Runtime errors count as a previous failure, except the ones
770 * used to ask userspace to retry.
783 * If any of the out-flags on the scrub vector match the mask
784 * that was set on the barrier vector, that's a previous fail.
786 if (v
->sv_flags
& failmask
)
794 * If the caller provided us with a nonzero inode number that isn't the ioctl
795 * file, try to grab a reference to it to eliminate all further untrusted inode
796 * lookups. If we can't get the inode, let each scrub function try again.
798 STATIC
struct xfs_inode
*
799 xchk_scrubv_open_by_handle(
800 struct xfs_mount
*mp
,
801 const struct xfs_scrub_vec_head
*head
)
803 struct xfs_trans
*tp
;
804 struct xfs_inode
*ip
;
807 error
= xfs_trans_alloc_empty(mp
, &tp
);
811 error
= xfs_iget(mp
, tp
, head
->svh_ino
, XCHK_IGET_FLAGS
, 0, &ip
);
812 xfs_trans_cancel(tp
);
816 if (VFS_I(ip
)->i_generation
!= head
->svh_gen
) {
824 /* Vectored scrub implementation to reduce ioctl calls. */
826 xfs_ioc_scrubv_metadata(
830 struct xfs_scrub_vec_head head
;
831 struct xfs_scrub_vec_head __user
*uhead
= arg
;
832 struct xfs_scrub_vec
*vectors
;
833 struct xfs_scrub_vec __user
*uvectors
;
834 struct xfs_inode
*ip_in
= XFS_I(file_inode(file
));
835 struct xfs_mount
*mp
= ip_in
->i_mount
;
836 struct xfs_inode
*handle_ip
= NULL
;
837 struct xfs_scrub_vec
*v
;
842 if (!capable(CAP_SYS_ADMIN
))
845 if (copy_from_user(&head
, uhead
, sizeof(head
)))
848 if (head
.svh_reserved
)
850 if (head
.svh_flags
& ~XFS_SCRUB_VEC_FLAGS_ALL
)
852 if (head
.svh_nr
== 0)
855 vec_bytes
= array_size(head
.svh_nr
, sizeof(struct xfs_scrub_vec
));
856 if (vec_bytes
> PAGE_SIZE
)
859 uvectors
= u64_to_user_ptr(head
.svh_vectors
);
860 vectors
= memdup_user(uvectors
, vec_bytes
);
862 return PTR_ERR(vectors
);
864 trace_xchk_scrubv_start(ip_in
, &head
);
866 for (i
= 0, v
= vectors
; i
< head
.svh_nr
; i
++, v
++) {
867 if (v
->sv_reserved
) {
872 if (v
->sv_type
== XFS_SCRUB_TYPE_BARRIER
&&
873 (v
->sv_flags
& ~XFS_SCRUB_FLAGS_OUT
)) {
878 trace_xchk_scrubv_item(mp
, &head
, i
, v
);
882 * If the caller wants us to do a scrub-by-handle and the file used to
883 * call the ioctl is not the same file, load the incore inode and pin
884 * it across all the scrubv actions to avoid repeated UNTRUSTED
885 * lookups. The reference is not passed to deeper layers of scrub
886 * because each scrubber gets to decide its own strategy and return
887 * values for getting an inode.
889 if (head
.svh_ino
&& head
.svh_ino
!= ip_in
->i_ino
)
890 handle_ip
= xchk_scrubv_open_by_handle(mp
, &head
);
892 /* Run all the scrubbers. */
893 for (i
= 0, v
= vectors
; i
< head
.svh_nr
; i
++, v
++) {
894 struct xfs_scrub_metadata sm
= {
895 .sm_type
= v
->sv_type
,
896 .sm_flags
= v
->sv_flags
,
897 .sm_ino
= head
.svh_ino
,
898 .sm_gen
= head
.svh_gen
,
899 .sm_agno
= head
.svh_agno
,
902 if (v
->sv_type
== XFS_SCRUB_TYPE_BARRIER
) {
903 v
->sv_ret
= xfs_scrubv_check_barrier(mp
, vectors
, v
);
905 trace_xchk_scrubv_barrier_fail(mp
, &head
, i
, v
);
912 v
->sv_ret
= xfs_scrub_metadata(file
, &sm
);
913 v
->sv_flags
= sm
.sm_flags
;
915 trace_xchk_scrubv_outcome(mp
, &head
, i
, v
);
917 if (head
.svh_rest_us
) {
920 expires
= ktime_add_ns(ktime_get(),
921 head
.svh_rest_us
* 1000);
922 set_current_state(TASK_KILLABLE
);
923 schedule_hrtimeout(&expires
, HRTIMER_MODE_ABS
);
926 if (fatal_signal_pending(current
)) {
932 if (copy_to_user(uvectors
, vectors
, vec_bytes
) ||
933 copy_to_user(uhead
, &head
, sizeof(head
))) {
940 xfs_irele(handle_ip
);