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_btree.h"
13 #include "xfs_log_format.h"
14 #include "xfs_trans.h"
15 #include "xfs_inode.h"
16 #include "xfs_icache.h"
17 #include "xfs_alloc.h"
18 #include "xfs_alloc_btree.h"
19 #include "xfs_ialloc.h"
20 #include "xfs_ialloc_btree.h"
21 #include "xfs_refcount_btree.h"
23 #include "xfs_rmap_btree.h"
25 #include "xfs_trans_priv.h"
26 #include "xfs_da_format.h"
27 #include "xfs_da_btree.h"
28 #include "xfs_dir2_priv.h"
30 #include "xfs_reflink.h"
32 #include "xfs_error.h"
33 #include "xfs_quota.h"
34 #include "scrub/scrub.h"
35 #include "scrub/common.h"
36 #include "scrub/trace.h"
37 #include "scrub/repair.h"
38 #include "scrub/health.h"
40 /* Common code for the metadata scrubbers. */
43 * Handling operational errors.
45 * The *_process_error() family of functions are used to process error return
46 * codes from functions called as part of a scrub operation.
48 * If there's no error, we return true to tell the caller that it's ok
49 * to move on to the next check in its list.
51 * For non-verifier errors (e.g. ENOMEM) we return false to tell the
52 * caller that something bad happened, and we preserve *error so that
53 * the caller can return the *error up the stack to userspace.
55 * Verifier errors (EFSBADCRC/EFSCORRUPTED) are recorded by setting
56 * OFLAG_CORRUPT in sm_flags and the *error is cleared. In other words,
57 * we track verifier errors (and failed scrub checks) via OFLAG_CORRUPT,
58 * not via return codes. We return false to tell the caller that
59 * something bad happened. Since the error has been cleared, the caller
60 * will (presumably) return that zero and scrubbing will move on to
63 * ftrace can be used to record the precise metadata location and the
64 * approximate code location of the failed operation.
67 /* Check for operational errors. */
82 /* Used to restart an op with deadlock avoidance. */
83 trace_xchk_deadlock_retry(
84 sc->ip ? sc->ip : XFS_I(file_inode(sc->file)),
89 * ECANCELED here means that the caller set one of the scrub
90 * outcome flags (corrupt, xfail, xcorrupt) and wants to exit
91 * quickly. Set error to zero and do not continue.
93 trace_xchk_op_error(sc, agno, bno, *error, ret_ip);
98 /* Note the badness but don't abort. */
99 sc->sm->sm_flags |= errflag;
103 trace_xchk_op_error(sc, agno, bno, *error, ret_ip);
111 struct xfs_scrub *sc,
116 return __xchk_process_error(sc, agno, bno, error,
117 XFS_SCRUB_OFLAG_CORRUPT, __return_address);
121 xchk_xref_process_error(
122 struct xfs_scrub *sc,
127 return __xchk_process_error(sc, agno, bno, error,
128 XFS_SCRUB_OFLAG_XFAIL, __return_address);
131 /* Check for operational errors for a file offset. */
133 __xchk_fblock_process_error(
134 struct xfs_scrub *sc,
136 xfs_fileoff_t offset,
146 /* Used to restart an op with deadlock avoidance. */
147 trace_xchk_deadlock_retry(sc->ip, sc->sm, *error);
151 * ECANCELED here means that the caller set one of the scrub
152 * outcome flags (corrupt, xfail, xcorrupt) and wants to exit
153 * quickly. Set error to zero and do not continue.
155 trace_xchk_file_op_error(sc, whichfork, offset, *error,
161 /* Note the badness but don't abort. */
162 sc->sm->sm_flags |= errflag;
166 trace_xchk_file_op_error(sc, whichfork, offset, *error,
174 xchk_fblock_process_error(
175 struct xfs_scrub *sc,
177 xfs_fileoff_t offset,
180 return __xchk_fblock_process_error(sc, whichfork, offset, error,
181 XFS_SCRUB_OFLAG_CORRUPT, __return_address);
185 xchk_fblock_xref_process_error(
186 struct xfs_scrub *sc,
188 xfs_fileoff_t offset,
191 return __xchk_fblock_process_error(sc, whichfork, offset, error,
192 XFS_SCRUB_OFLAG_XFAIL, __return_address);
196 * Handling scrub corruption/optimization/warning checks.
198 * The *_set_{corrupt,preen,warning}() family of functions are used to
199 * record the presence of metadata that is incorrect (corrupt), could be
200 * optimized somehow (preen), or should be flagged for administrative
201 * review but is not incorrect (warn).
203 * ftrace can be used to record the precise metadata location and
204 * approximate code location of the failed check.
207 /* Record a block which could be optimized. */
209 xchk_block_set_preen(
210 struct xfs_scrub *sc,
213 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
214 trace_xchk_block_preen(sc, xfs_buf_daddr(bp), __return_address);
218 * Record an inode which could be optimized. The trace data will
219 * include the block given by bp if bp is given; otherwise it will use
220 * the block location of the inode record itself.
224 struct xfs_scrub *sc,
227 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
228 trace_xchk_ino_preen(sc, ino, __return_address);
231 /* Record something being wrong with the filesystem primary superblock. */
234 struct xfs_scrub *sc)
236 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
237 trace_xchk_fs_error(sc, 0, __return_address);
240 /* Record a corrupt block. */
242 xchk_block_set_corrupt(
243 struct xfs_scrub *sc,
246 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
247 trace_xchk_block_error(sc, xfs_buf_daddr(bp), __return_address);
250 #ifdef CONFIG_XFS_QUOTA
251 /* Record a corrupt quota counter. */
253 xchk_qcheck_set_corrupt(
254 struct xfs_scrub *sc,
258 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
259 trace_xchk_qcheck_error(sc, dqtype, id, __return_address);
263 /* Record a corruption while cross-referencing. */
265 xchk_block_xref_set_corrupt(
266 struct xfs_scrub *sc,
269 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
270 trace_xchk_block_error(sc, xfs_buf_daddr(bp), __return_address);
274 * Record a corrupt inode. The trace data will include the block given
275 * by bp if bp is given; otherwise it will use the block location of the
276 * inode record itself.
279 xchk_ino_set_corrupt(
280 struct xfs_scrub *sc,
283 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
284 trace_xchk_ino_error(sc, ino, __return_address);
287 /* Record a corruption while cross-referencing with an inode. */
289 xchk_ino_xref_set_corrupt(
290 struct xfs_scrub *sc,
293 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
294 trace_xchk_ino_error(sc, ino, __return_address);
297 /* Record corruption in a block indexed by a file fork. */
299 xchk_fblock_set_corrupt(
300 struct xfs_scrub *sc,
302 xfs_fileoff_t offset)
304 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
305 trace_xchk_fblock_error(sc, whichfork, offset, __return_address);
308 /* Record a corruption while cross-referencing a fork block. */
310 xchk_fblock_xref_set_corrupt(
311 struct xfs_scrub *sc,
313 xfs_fileoff_t offset)
315 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
316 trace_xchk_fblock_error(sc, whichfork, offset, __return_address);
320 * Warn about inodes that need administrative review but is not
324 xchk_ino_set_warning(
325 struct xfs_scrub *sc,
328 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
329 trace_xchk_ino_warning(sc, ino, __return_address);
332 /* Warn about a block indexed by a file fork that needs review. */
334 xchk_fblock_set_warning(
335 struct xfs_scrub *sc,
337 xfs_fileoff_t offset)
339 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
340 trace_xchk_fblock_warning(sc, whichfork, offset, __return_address);
343 /* Signal an incomplete scrub. */
346 struct xfs_scrub *sc)
348 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_INCOMPLETE;
349 trace_xchk_incomplete(sc, __return_address);
353 * rmap scrubbing -- compute the number of blocks with a given owner,
354 * at least according to the reverse mapping data.
357 struct xchk_rmap_ownedby_info {
358 const struct xfs_owner_info *oinfo;
359 xfs_filblks_t *blocks;
363 xchk_count_rmap_ownedby_irec(
364 struct xfs_btree_cur *cur,
365 const struct xfs_rmap_irec *rec,
368 struct xchk_rmap_ownedby_info *sroi = priv;
372 irec_attr = rec->rm_flags & XFS_RMAP_ATTR_FORK;
373 oinfo_attr = sroi->oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK;
375 if (rec->rm_owner != sroi->oinfo->oi_owner)
378 if (XFS_RMAP_NON_INODE_OWNER(rec->rm_owner) || irec_attr == oinfo_attr)
379 (*sroi->blocks) += rec->rm_blockcount;
385 * Calculate the number of blocks the rmap thinks are owned by something.
386 * The caller should pass us an rmapbt cursor.
389 xchk_count_rmap_ownedby_ag(
390 struct xfs_scrub *sc,
391 struct xfs_btree_cur *cur,
392 const struct xfs_owner_info *oinfo,
393 xfs_filblks_t *blocks)
395 struct xchk_rmap_ownedby_info sroi = {
401 return xfs_rmap_query_all(cur, xchk_count_rmap_ownedby_irec,
408 * These helpers facilitate locking an allocation group's header
409 * buffers, setting up cursors for all btrees that are present, and
410 * cleaning everything up once we're through.
413 /* Decide if we want to return an AG header read failure. */
415 want_ag_read_header_failure(
416 struct xfs_scrub *sc,
419 /* Return all AG header read failures when scanning btrees. */
420 if (sc->sm->sm_type != XFS_SCRUB_TYPE_AGF &&
421 sc->sm->sm_type != XFS_SCRUB_TYPE_AGFL &&
422 sc->sm->sm_type != XFS_SCRUB_TYPE_AGI)
425 * If we're scanning a given type of AG header, we only want to
426 * see read failures from that specific header. We'd like the
427 * other headers to cross-check them, but this isn't required.
429 if (sc->sm->sm_type == type)
435 * Grab the AG header buffers for the attached perag structure.
437 * The headers should be released by xchk_ag_free, but as a fail safe we attach
438 * all the buffers we grab to the scrub transaction so they'll all be freed
442 xchk_perag_read_headers(
443 struct xfs_scrub *sc,
448 error = xfs_ialloc_read_agi(sa->pag, sc->tp, &sa->agi_bp);
449 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGI))
452 error = xfs_alloc_read_agf(sa->pag, sc->tp, 0, &sa->agf_bp);
453 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGF))
460 * Grab the AG headers for the attached perag structure and wait for pending
464 xchk_perag_drain_and_lock(
465 struct xfs_scrub *sc)
467 struct xchk_ag *sa = &sc->sa;
470 ASSERT(sa->pag != NULL);
471 ASSERT(sa->agi_bp == NULL);
472 ASSERT(sa->agf_bp == NULL);
475 if (xchk_should_terminate(sc, &error))
478 error = xchk_perag_read_headers(sc, sa);
483 * If we've grabbed an inode for scrubbing then we assume that
484 * holding its ILOCK will suffice to coordinate with any intent
485 * chains involving this inode.
491 * Decide if this AG is quiet enough for all metadata to be
492 * consistent with each other. XFS allows the AG header buffer
493 * locks to cycle across transaction rolls while processing
494 * chains of deferred ops, which means that there could be
495 * other threads in the middle of processing a chain of
496 * deferred ops. For regular operations we are careful about
497 * ordering operations to prevent collisions between threads
498 * (which is why we don't need a per-AG lock), but scrub and
499 * repair have to serialize against chained operations.
501 * We just locked all the AG headers buffers; now take a look
502 * to see if there are any intents in progress. If there are,
503 * drop the AG headers and wait for the intents to drain.
504 * Since we hold all the AG header locks for the duration of
505 * the scrub, this is the only time we have to sample the
506 * intents counter; any threads increasing it after this point
507 * can't possibly be in the middle of a chain of AG metadata
510 * Obviously, this should be slanted against scrub and in favor
511 * of runtime threads.
513 if (!xfs_perag_intent_busy(sa->pag))
517 xfs_trans_brelse(sc->tp, sa->agf_bp);
522 xfs_trans_brelse(sc->tp, sa->agi_bp);
526 if (!(sc->flags & XCHK_FSGATES_DRAIN))
528 error = xfs_perag_intent_drain(sa->pag);
529 if (error == -ERESTARTSYS)
537 * Grab the per-AG structure, grab all AG header buffers, and wait until there
538 * aren't any pending intents. Returns -ENOENT if we can't grab the perag
542 xchk_ag_read_headers(
543 struct xfs_scrub *sc,
547 struct xfs_mount *mp = sc->mp;
550 sa->pag = xfs_perag_get(mp, agno);
554 return xchk_perag_drain_and_lock(sc);
557 /* Release all the AG btree cursors. */
563 xfs_btree_del_cursor(sa->refc_cur, XFS_BTREE_ERROR);
565 xfs_btree_del_cursor(sa->rmap_cur, XFS_BTREE_ERROR);
567 xfs_btree_del_cursor(sa->fino_cur, XFS_BTREE_ERROR);
569 xfs_btree_del_cursor(sa->ino_cur, XFS_BTREE_ERROR);
571 xfs_btree_del_cursor(sa->cnt_cur, XFS_BTREE_ERROR);
573 xfs_btree_del_cursor(sa->bno_cur, XFS_BTREE_ERROR);
583 /* Initialize all the btree cursors for an AG. */
586 struct xfs_scrub *sc,
589 struct xfs_mount *mp = sc->mp;
592 /* Set up a bnobt cursor for cross-referencing. */
593 sa->bno_cur = xfs_bnobt_init_cursor(mp, sc->tp, sa->agf_bp,
595 xchk_ag_btree_del_cursor_if_sick(sc, &sa->bno_cur,
596 XFS_SCRUB_TYPE_BNOBT);
598 /* Set up a cntbt cursor for cross-referencing. */
599 sa->cnt_cur = xfs_cntbt_init_cursor(mp, sc->tp, sa->agf_bp,
601 xchk_ag_btree_del_cursor_if_sick(sc, &sa->cnt_cur,
602 XFS_SCRUB_TYPE_CNTBT);
604 /* Set up a rmapbt cursor for cross-referencing. */
605 if (xfs_has_rmapbt(mp)) {
606 sa->rmap_cur = xfs_rmapbt_init_cursor(mp, sc->tp,
607 sa->agf_bp, sa->pag);
608 xchk_ag_btree_del_cursor_if_sick(sc, &sa->rmap_cur,
609 XFS_SCRUB_TYPE_RMAPBT);
612 /* Set up a refcountbt cursor for cross-referencing. */
613 if (xfs_has_reflink(mp)) {
614 sa->refc_cur = xfs_refcountbt_init_cursor(mp, sc->tp,
615 sa->agf_bp, sa->pag);
616 xchk_ag_btree_del_cursor_if_sick(sc, &sa->refc_cur,
617 XFS_SCRUB_TYPE_REFCNTBT);
622 /* Set up a inobt cursor for cross-referencing. */
623 sa->ino_cur = xfs_inobt_init_cursor(sa->pag, sc->tp,
625 xchk_ag_btree_del_cursor_if_sick(sc, &sa->ino_cur,
626 XFS_SCRUB_TYPE_INOBT);
628 /* Set up a finobt cursor for cross-referencing. */
629 if (xfs_has_finobt(mp)) {
630 sa->fino_cur = xfs_finobt_init_cursor(sa->pag, sc->tp,
632 xchk_ag_btree_del_cursor_if_sick(sc, &sa->fino_cur,
633 XFS_SCRUB_TYPE_FINOBT);
638 /* Release the AG header context and btree cursors. */
641 struct xfs_scrub *sc,
644 xchk_ag_btcur_free(sa);
645 xrep_reset_perag_resv(sc);
647 xfs_trans_brelse(sc->tp, sa->agf_bp);
651 xfs_trans_brelse(sc->tp, sa->agi_bp);
655 xfs_perag_put(sa->pag);
661 * For scrub, grab the perag structure, the AGI, and the AGF headers, in that
662 * order. Locking order requires us to get the AGI before the AGF. We use the
663 * transaction to avoid deadlocking on crosslinked metadata buffers; either the
664 * caller passes one in (bmap scrub) or we have to create a transaction
665 * ourselves. Returns ENOENT if the perag struct cannot be grabbed.
669 struct xfs_scrub *sc,
675 error = xchk_ag_read_headers(sc, agno, sa);
679 xchk_ag_btcur_init(sc, sa);
683 /* Per-scrubber setup functions */
687 struct xfs_scrub *sc)
689 xfs_trans_cancel(sc->tp);
694 xchk_trans_alloc_empty(
695 struct xfs_scrub *sc)
697 return xfs_trans_alloc_empty(sc->mp, &sc->tp);
701 * Grab an empty transaction so that we can re-grab locked buffers if
702 * one of our btrees turns out to be cyclic.
704 * If we're going to repair something, we need to ask for the largest possible
705 * log reservation so that we can handle the worst case scenario for metadata
706 * updates while rebuilding a metadata item. We also need to reserve as many
707 * blocks in the head transaction as we think we're going to need to rebuild
708 * the metadata object.
712 struct xfs_scrub *sc,
715 if (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)
716 return xfs_trans_alloc(sc->mp, &M_RES(sc->mp)->tr_itruncate,
717 resblks, 0, 0, &sc->tp);
719 return xchk_trans_alloc_empty(sc);
722 /* Set us up with a transaction and an empty context. */
725 struct xfs_scrub *sc)
729 resblks = xrep_calc_ag_resblks(sc);
730 return xchk_trans_alloc(sc, resblks);
733 /* Set us up with AG headers and btree cursors. */
736 struct xfs_scrub *sc,
739 struct xfs_mount *mp = sc->mp;
743 * If the caller asks us to checkpont the log, do so. This
744 * expensive operation should be performed infrequently and only
745 * as a last resort. Any caller that sets force_log should
746 * document why they need to do so.
749 error = xchk_checkpoint_log(mp);
754 error = xchk_setup_fs(sc);
758 return xchk_ag_init(sc, sc->sm->sm_agno, &sc->sa);
761 /* Push everything out of the log onto disk. */
764 struct xfs_mount *mp)
768 error = xfs_log_force(mp, XFS_LOG_SYNC);
771 xfs_ail_push_all_sync(mp->m_ail);
775 /* Verify that an inode is allocated ondisk, then return its cached inode. */
778 struct xfs_scrub *sc,
780 struct xfs_inode **ipp)
782 ASSERT(sc->tp != NULL);
784 return xfs_iget(sc->mp, sc->tp, inum, XFS_IGET_UNTRUSTED, 0, ipp);
788 * Try to grab an inode in a manner that avoids races with physical inode
789 * allocation. If we can't, return the locked AGI buffer so that the caller
790 * can single-step the loading process to see where things went wrong.
791 * Callers must have a valid scrub transaction.
793 * If the iget succeeds, return 0, a NULL AGI, and the inode.
795 * If the iget fails, return the error, the locked AGI, and a NULL inode. This
796 * can include -EINVAL and -ENOENT for invalid inode numbers or inodes that are
797 * no longer allocated; or any other corruption or runtime error.
799 * If the AGI read fails, return the error, a NULL AGI, and NULL inode.
801 * If a fatal signal is pending, return -EINTR, a NULL AGI, and a NULL inode.
805 struct xfs_scrub *sc,
807 struct xfs_buf **agi_bpp,
808 struct xfs_inode **ipp)
810 struct xfs_mount *mp = sc->mp;
811 struct xfs_trans *tp = sc->tp;
812 struct xfs_perag *pag;
815 ASSERT(sc->tp != NULL);
822 if (xchk_should_terminate(sc, &error))
826 * Attach the AGI buffer to the scrub transaction to avoid deadlocks
827 * in the iget cache miss path.
829 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
830 error = xfs_ialloc_read_agi(pag, tp, agi_bpp);
835 error = xfs_iget(mp, tp, inum,
836 XFS_IGET_NORETRY | XFS_IGET_UNTRUSTED, 0, ipp);
837 if (error == -EAGAIN) {
839 * The inode may be in core but temporarily unavailable and may
840 * require the AGI buffer before it can be returned. Drop the
841 * AGI buffer and retry the lookup.
843 * Incore lookup will fail with EAGAIN on a cache hit if the
844 * inode is queued to the inactivation list. The inactivation
845 * worker may remove the inode from the unlinked list and hence
848 * Hence xchk_iget_agi() needs to drop the AGI lock on EAGAIN
849 * to allow inodegc to make progress and move the inode to
850 * IRECLAIMABLE state where xfs_iget will be able to return it
851 * again if it can lock the inode.
853 xfs_trans_brelse(tp, *agi_bpp);
860 /* We got the inode, so we can release the AGI. */
861 ASSERT(*ipp != NULL);
862 xfs_trans_brelse(tp, *agi_bpp);
867 #ifdef CONFIG_XFS_QUOTA
869 * Try to attach dquots to this inode if we think we might want to repair it.
870 * Callers must not hold any ILOCKs. If the dquots are broken and cannot be
871 * attached, a quotacheck will be scheduled.
875 struct xfs_scrub *sc)
877 ASSERT(sc->tp != NULL);
878 ASSERT(sc->ip != NULL);
880 if (!xchk_could_repair(sc))
883 return xrep_ino_dqattach(sc);
887 /* Install an inode that we opened by handle for scrubbing. */
889 xchk_install_handle_inode(
890 struct xfs_scrub *sc,
891 struct xfs_inode *ip)
893 if (VFS_I(ip)->i_generation != sc->sm->sm_gen) {
903 * Install an already-referenced inode for scrubbing. Get our own reference to
904 * the inode to make disposal simpler. The inode must not be in I_FREEING or
908 xchk_install_live_inode(
909 struct xfs_scrub *sc,
910 struct xfs_inode *ip)
912 if (!igrab(VFS_I(ip))) {
913 xchk_ino_set_corrupt(sc, ip->i_ino);
914 return -EFSCORRUPTED;
922 * In preparation to scrub metadata structures that hang off of an inode,
923 * grab either the inode referenced in the scrub control structure or the
924 * inode passed in. If the inumber does not reference an allocated inode
925 * record, the function returns ENOENT to end the scrub early. The inode
929 xchk_iget_for_scrubbing(
930 struct xfs_scrub *sc)
932 struct xfs_imap imap;
933 struct xfs_mount *mp = sc->mp;
934 struct xfs_perag *pag;
935 struct xfs_buf *agi_bp;
936 struct xfs_inode *ip_in = XFS_I(file_inode(sc->file));
937 struct xfs_inode *ip = NULL;
938 xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, sc->sm->sm_ino);
941 ASSERT(sc->tp == NULL);
943 /* We want to scan the inode we already had opened. */
944 if (sc->sm->sm_ino == 0 || sc->sm->sm_ino == ip_in->i_ino)
945 return xchk_install_live_inode(sc, ip_in);
947 /* Reject internal metadata files and obviously bad inode numbers. */
948 if (xfs_internal_inum(mp, sc->sm->sm_ino))
950 if (!xfs_verify_ino(sc->mp, sc->sm->sm_ino))
953 /* Try a safe untrusted iget. */
954 error = xchk_iget_safe(sc, sc->sm->sm_ino, &ip);
956 return xchk_install_handle_inode(sc, ip);
957 if (error == -ENOENT)
959 if (error != -EINVAL)
963 * EINVAL with IGET_UNTRUSTED probably means one of several things:
964 * userspace gave us an inode number that doesn't correspond to fs
965 * space; the inode btree lacks a record for this inode; or there is a
966 * record, and it says this inode is free.
968 * We want to look up this inode in the inobt to distinguish two
969 * scenarios: (1) the inobt says the inode is free, in which case
970 * there's nothing to do; and (2) the inobt says the inode is
971 * allocated, but loading it failed due to corruption.
973 * Allocate a transaction and grab the AGI to prevent inobt activity
974 * in this AG. Retry the iget in case someone allocated a new inode
975 * after the first iget failed.
977 error = xchk_trans_alloc(sc, 0);
981 error = xchk_iget_agi(sc, sc->sm->sm_ino, &agi_bp, &ip);
983 /* Actually got the inode, so install it. */
984 xchk_trans_cancel(sc);
985 return xchk_install_handle_inode(sc, ip);
987 if (error == -ENOENT)
989 if (error != -EINVAL)
992 /* Ensure that we have protected against inode allocation/freeing. */
993 if (agi_bp == NULL) {
994 ASSERT(agi_bp != NULL);
1000 * Untrusted iget failed a second time. Let's try an inobt lookup.
1001 * If the inobt thinks this the inode neither can exist inside the
1002 * filesystem nor is allocated, return ENOENT to signal that the check
1005 * If the lookup returns corruption, we'll mark this inode corrupt and
1006 * exit to userspace. There's little chance of fixing anything until
1007 * the inobt is straightened out, but there's nothing we can do here.
1009 * If the lookup encounters any other error, exit to userspace.
1011 * If the lookup succeeds, something else must be very wrong in the fs
1012 * such that setting up the incore inode failed in some strange way.
1013 * Treat those as corruptions.
1015 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, sc->sm->sm_ino));
1017 error = -EFSCORRUPTED;
1021 error = xfs_imap(pag, sc->tp, sc->sm->sm_ino, &imap,
1022 XFS_IGET_UNTRUSTED);
1024 if (error == -EINVAL || error == -ENOENT)
1027 error = -EFSCORRUPTED;
1030 xchk_trans_cancel(sc);
1032 trace_xchk_op_error(sc, agno, XFS_INO_TO_AGBNO(mp, sc->sm->sm_ino),
1033 error, __return_address);
1036 /* The file is gone, so there's nothing to check. */
1037 xchk_trans_cancel(sc);
1041 /* Release an inode, possibly dropping it in the process. */
1044 struct xfs_scrub *sc,
1045 struct xfs_inode *ip)
1049 * If we are in a transaction, we /cannot/ drop the inode
1050 * ourselves, because the VFS will trigger writeback, which
1051 * can require a transaction. Clear DONTCACHE to force the
1052 * inode to the LRU, where someone else can take care of
1055 * Note that when we grabbed our reference to the inode, it
1056 * could have had an active ref and DONTCACHE set if a sysadmin
1057 * is trying to coerce a change in file access mode. icache
1058 * hits do not clear DONTCACHE, so we must do it here.
1060 spin_lock(&VFS_I(ip)->i_lock);
1061 VFS_I(ip)->i_state &= ~I_DONTCACHE;
1062 spin_unlock(&VFS_I(ip)->i_lock);
1063 } else if (atomic_read(&VFS_I(ip)->i_count) == 1) {
1065 * If this is the last reference to the inode and the caller
1066 * permits it, set DONTCACHE to avoid thrashing.
1068 d_mark_dontcache(VFS_I(ip));
1075 * Set us up to scrub metadata mapped by a file's fork. Callers must not use
1076 * this to operate on user-accessible regular file data because the MMAPLOCK is
1080 xchk_setup_inode_contents(
1081 struct xfs_scrub *sc,
1082 unsigned int resblks)
1086 error = xchk_iget_for_scrubbing(sc);
1090 /* Lock the inode so the VFS cannot touch this file. */
1091 xchk_ilock(sc, XFS_IOLOCK_EXCL);
1093 error = xchk_trans_alloc(sc, resblks);
1097 error = xchk_ino_dqattach(sc);
1101 xchk_ilock(sc, XFS_ILOCK_EXCL);
1103 /* scrub teardown will unlock and release the inode for us */
1109 struct xfs_scrub *sc,
1110 unsigned int ilock_flags)
1112 xfs_ilock(sc->ip, ilock_flags);
1113 sc->ilock_flags |= ilock_flags;
1118 struct xfs_scrub *sc,
1119 unsigned int ilock_flags)
1121 if (xfs_ilock_nowait(sc->ip, ilock_flags)) {
1122 sc->ilock_flags |= ilock_flags;
1131 struct xfs_scrub *sc,
1132 unsigned int ilock_flags)
1134 sc->ilock_flags &= ~ilock_flags;
1135 xfs_iunlock(sc->ip, ilock_flags);
1139 * Predicate that decides if we need to evaluate the cross-reference check.
1140 * If there was an error accessing the cross-reference btree, just delete
1141 * the cursor and skip the check.
1144 xchk_should_check_xref(
1145 struct xfs_scrub *sc,
1147 struct xfs_btree_cur **curpp)
1149 /* No point in xref if we already know we're corrupt. */
1150 if (xchk_skip_xref(sc->sm))
1157 /* If we've already given up on xref, just bail out. */
1161 /* xref error, delete cursor and bail out. */
1162 xfs_btree_del_cursor(*curpp, XFS_BTREE_ERROR);
1166 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XFAIL;
1167 trace_xchk_xref_error(sc, *error, __return_address);
1170 * Errors encountered during cross-referencing with another
1171 * data structure should not cause this scrubber to abort.
1177 /* Run the structure verifiers on in-memory buffers to detect bad memory. */
1179 xchk_buffer_recheck(
1180 struct xfs_scrub *sc,
1185 if (bp->b_ops == NULL) {
1186 xchk_block_set_corrupt(sc, bp);
1189 if (bp->b_ops->verify_struct == NULL) {
1190 xchk_set_incomplete(sc);
1193 fa = bp->b_ops->verify_struct(bp);
1196 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
1197 trace_xchk_block_error(sc, xfs_buf_daddr(bp), fa);
1201 xchk_metadata_inode_subtype(
1202 struct xfs_scrub *sc,
1203 unsigned int scrub_type)
1205 __u32 smtype = sc->sm->sm_type;
1206 unsigned int sick_mask = sc->sick_mask;
1209 sc->sm->sm_type = scrub_type;
1211 switch (scrub_type) {
1212 case XFS_SCRUB_TYPE_INODE:
1213 error = xchk_inode(sc);
1215 case XFS_SCRUB_TYPE_BMBTD:
1216 error = xchk_bmap_data(sc);
1220 error = -EFSCORRUPTED;
1224 sc->sick_mask = sick_mask;
1225 sc->sm->sm_type = smtype;
1230 * Scrub the attr/data forks of a metadata inode. The metadata inode must be
1231 * pointed to by sc->ip and the ILOCK must be held.
1234 xchk_metadata_inode_forks(
1235 struct xfs_scrub *sc)
1240 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
1243 /* Check the inode record. */
1244 error = xchk_metadata_inode_subtype(sc, XFS_SCRUB_TYPE_INODE);
1245 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
1248 /* Metadata inodes don't live on the rt device. */
1249 if (sc->ip->i_diflags & XFS_DIFLAG_REALTIME) {
1250 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
1254 /* They should never participate in reflink. */
1255 if (xfs_is_reflink_inode(sc->ip)) {
1256 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
1260 /* They also should never have extended attributes. */
1261 if (xfs_inode_hasattr(sc->ip)) {
1262 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
1266 /* Invoke the data fork scrubber. */
1267 error = xchk_metadata_inode_subtype(sc, XFS_SCRUB_TYPE_BMBTD);
1268 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
1271 /* Look for incorrect shared blocks. */
1272 if (xfs_has_reflink(sc->mp)) {
1273 error = xfs_reflink_inode_has_shared_extents(sc->tp, sc->ip,
1275 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0,
1279 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
1286 * Enable filesystem hooks (i.e. runtime code patching) before starting a scrub
1287 * operation. Callers must not hold any locks that intersect with the CPU
1288 * hotplug lock (e.g. writeback locks) because code patching must halt the CPUs
1289 * to change kernel code.
1292 xchk_fsgates_enable(
1293 struct xfs_scrub *sc,
1294 unsigned int scrub_fsgates)
1296 ASSERT(!(scrub_fsgates & ~XCHK_FSGATES_ALL));
1297 ASSERT(!(sc->flags & scrub_fsgates));
1299 trace_xchk_fsgates_enable(sc, scrub_fsgates);
1301 if (scrub_fsgates & XCHK_FSGATES_DRAIN)
1302 xfs_drain_wait_enable();
1304 if (scrub_fsgates & XCHK_FSGATES_QUOTA)
1305 xfs_dqtrx_hook_enable();
1307 if (scrub_fsgates & XCHK_FSGATES_DIRENTS)
1308 xfs_dir_hook_enable();
1310 if (scrub_fsgates & XCHK_FSGATES_RMAP)
1311 xfs_rmap_hook_enable();
1313 sc->flags |= scrub_fsgates;
1317 * Decide if this is this a cached inode that's also allocated. The caller
1318 * must hold a reference to an AG and the AGI buffer lock to prevent inodes
1319 * from being allocated or freed.
1321 * Look up an inode by number in the given file system. If the inode number
1322 * is invalid, return -EINVAL. If the inode is not in cache, return -ENODATA.
1323 * If the inode is being reclaimed, return -ENODATA because we know the inode
1324 * cache cannot be updating the ondisk metadata.
1326 * Otherwise, the incore inode is the one we want, and it is either live,
1327 * somewhere in the inactivation machinery, or reclaimable. The inode is
1328 * allocated if i_mode is nonzero. In all three cases, the cached inode will
1329 * be more up to date than the ondisk inode buffer, so we must use the incore
1333 xchk_inode_is_allocated(
1334 struct xfs_scrub *sc,
1338 struct xfs_mount *mp = sc->mp;
1339 struct xfs_perag *pag = sc->sa.pag;
1341 struct xfs_inode *ip;
1344 /* caller must hold perag reference */
1346 ASSERT(pag != NULL);
1350 /* caller must have AGI buffer */
1351 if (sc->sa.agi_bp == NULL) {
1352 ASSERT(sc->sa.agi_bp != NULL);
1356 /* reject inode numbers outside existing AGs */
1357 ino = XFS_AGINO_TO_INO(sc->mp, pag->pag_agno, agino);
1358 if (!xfs_verify_ino(mp, ino))
1363 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
1370 * If the inode number doesn't match, the incore inode got reused
1371 * during an RCU grace period and the radix tree hasn't been updated.
1372 * This isn't the inode we want.
1374 spin_lock(&ip->i_flags_lock);
1375 if (ip->i_ino != ino)
1378 trace_xchk_inode_is_allocated(ip);
1381 * We have an incore inode that matches the inode we want, and the
1382 * caller holds the perag structure and the AGI buffer. Let's check
1383 * our assumptions below:
1388 * (1) If the incore inode is live (i.e. referenced from the dcache),
1389 * it will not be INEW, nor will it be in the inactivation or reclaim
1390 * machinery. The ondisk inode had better be allocated. This is the
1391 * most trivial case.
1393 if (!(ip->i_flags & (XFS_NEED_INACTIVE | XFS_INEW | XFS_IRECLAIMABLE |
1394 XFS_INACTIVATING))) {
1396 ASSERT(VFS_I(ip)->i_mode != 0);
1400 * If the incore inode is INEW, there are several possibilities:
1402 * (2) For a file that is being created, note that we allocate the
1403 * ondisk inode before allocating, initializing, and adding the incore
1404 * inode to the radix tree.
1406 * (3) If the incore inode is being recycled, the inode has to be
1407 * allocated because we don't allow freed inodes to be recycled.
1408 * Recycling doesn't touch i_mode.
1410 if (ip->i_flags & XFS_INEW) {
1411 /* created on disk already or recycling */
1412 ASSERT(VFS_I(ip)->i_mode != 0);
1416 * (4) If the inode is queued for inactivation (NEED_INACTIVE) but
1417 * inactivation has not started (!INACTIVATING), it is still allocated.
1419 if ((ip->i_flags & XFS_NEED_INACTIVE) &&
1420 !(ip->i_flags & XFS_INACTIVATING)) {
1421 /* definitely before difree */
1422 ASSERT(VFS_I(ip)->i_mode != 0);
1427 * If the incore inode is undergoing inactivation (INACTIVATING), there
1428 * are two possibilities:
1430 * (5) It is before the point where it would get freed ondisk, in which
1431 * case i_mode is still nonzero.
1433 * (6) It has already been freed, in which case i_mode is zero.
1435 * We don't take the ILOCK here, but difree and dialloc update the AGI,
1436 * and we've taken the AGI buffer lock, which prevents that from
1441 * (7) Inodes undergoing inactivation (INACTIVATING) or queued for
1442 * reclaim (IRECLAIMABLE) could be allocated or free. i_mode still
1443 * reflects the ondisk state.
1447 * (8) If the inode is in IFLUSHING, it's safe to query i_mode because
1448 * the flush code uses i_mode to format the ondisk inode.
1452 * (9) If the inode is in IRECLAIM and was reachable via the radix
1453 * tree, it still has the same i_mode as it did before it entered
1454 * reclaim. The inode object is still alive because we hold the RCU
1458 *inuse = VFS_I(ip)->i_mode != 0;
1462 spin_unlock(&ip->i_flags_lock);