1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
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"
16 #include "xfs_inode.h"
17 #include "xfs_icache.h"
18 #include "xfs_alloc.h"
19 #include "xfs_alloc_btree.h"
20 #include "xfs_ialloc.h"
21 #include "xfs_ialloc_btree.h"
22 #include "xfs_refcount_btree.h"
24 #include "xfs_rmap_btree.h"
26 #include "xfs_trans_priv.h"
28 #include "xfs_reflink.h"
29 #include "scrub/scrub.h"
30 #include "scrub/common.h"
31 #include "scrub/trace.h"
32 #include "scrub/repair.h"
33 #include "scrub/health.h"
35 /* Common code for the metadata scrubbers. */
38 * Handling operational errors.
40 * The *_process_error() family of functions are used to process error return
41 * codes from functions called as part of a scrub operation.
43 * If there's no error, we return true to tell the caller that it's ok
44 * to move on to the next check in its list.
46 * For non-verifier errors (e.g. ENOMEM) we return false to tell the
47 * caller that something bad happened, and we preserve *error so that
48 * the caller can return the *error up the stack to userspace.
50 * Verifier errors (EFSBADCRC/EFSCORRUPTED) are recorded by setting
51 * OFLAG_CORRUPT in sm_flags and the *error is cleared. In other words,
52 * we track verifier errors (and failed scrub checks) via OFLAG_CORRUPT,
53 * not via return codes. We return false to tell the caller that
54 * something bad happened. Since the error has been cleared, the caller
55 * will (presumably) return that zero and scrubbing will move on to
58 * ftrace can be used to record the precise metadata location and the
59 * approximate code location of the failed operation.
62 /* Check for operational errors. */
76 /* Used to restart an op with deadlock avoidance. */
77 trace_xchk_deadlock_retry(
78 sc->ip ? sc->ip : XFS_I(file_inode(sc->file)),
83 /* Note the badness but don't abort. */
84 sc->sm->sm_flags |= errflag;
88 trace_xchk_op_error(sc, agno, bno, *error,
102 return __xchk_process_error(sc, agno, bno, error,
103 XFS_SCRUB_OFLAG_CORRUPT, __return_address);
107 xchk_xref_process_error(
108 struct xfs_scrub *sc,
113 return __xchk_process_error(sc, agno, bno, error,
114 XFS_SCRUB_OFLAG_XFAIL, __return_address);
117 /* Check for operational errors for a file offset. */
119 __xchk_fblock_process_error(
120 struct xfs_scrub *sc,
122 xfs_fileoff_t offset,
131 /* Used to restart an op with deadlock avoidance. */
132 trace_xchk_deadlock_retry(sc->ip, sc->sm, *error);
136 /* Note the badness but don't abort. */
137 sc->sm->sm_flags |= errflag;
141 trace_xchk_file_op_error(sc, whichfork, offset, *error,
149 xchk_fblock_process_error(
150 struct xfs_scrub *sc,
152 xfs_fileoff_t offset,
155 return __xchk_fblock_process_error(sc, whichfork, offset, error,
156 XFS_SCRUB_OFLAG_CORRUPT, __return_address);
160 xchk_fblock_xref_process_error(
161 struct xfs_scrub *sc,
163 xfs_fileoff_t offset,
166 return __xchk_fblock_process_error(sc, whichfork, offset, error,
167 XFS_SCRUB_OFLAG_XFAIL, __return_address);
171 * Handling scrub corruption/optimization/warning checks.
173 * The *_set_{corrupt,preen,warning}() family of functions are used to
174 * record the presence of metadata that is incorrect (corrupt), could be
175 * optimized somehow (preen), or should be flagged for administrative
176 * review but is not incorrect (warn).
178 * ftrace can be used to record the precise metadata location and
179 * approximate code location of the failed check.
182 /* Record a block which could be optimized. */
184 xchk_block_set_preen(
185 struct xfs_scrub *sc,
188 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
189 trace_xchk_block_preen(sc, bp->b_bn, __return_address);
193 * Record an inode which could be optimized. The trace data will
194 * include the block given by bp if bp is given; otherwise it will use
195 * the block location of the inode record itself.
199 struct xfs_scrub *sc,
202 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
203 trace_xchk_ino_preen(sc, ino, __return_address);
206 /* Record something being wrong with the filesystem primary superblock. */
209 struct xfs_scrub *sc)
211 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
212 trace_xchk_fs_error(sc, 0, __return_address);
215 /* Record a corrupt block. */
217 xchk_block_set_corrupt(
218 struct xfs_scrub *sc,
221 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
222 trace_xchk_block_error(sc, bp->b_bn, __return_address);
225 /* Record a corruption while cross-referencing. */
227 xchk_block_xref_set_corrupt(
228 struct xfs_scrub *sc,
231 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
232 trace_xchk_block_error(sc, bp->b_bn, __return_address);
236 * Record a corrupt inode. The trace data will include the block given
237 * by bp if bp is given; otherwise it will use the block location of the
238 * inode record itself.
241 xchk_ino_set_corrupt(
242 struct xfs_scrub *sc,
245 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
246 trace_xchk_ino_error(sc, ino, __return_address);
249 /* Record a corruption while cross-referencing with an inode. */
251 xchk_ino_xref_set_corrupt(
252 struct xfs_scrub *sc,
255 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
256 trace_xchk_ino_error(sc, ino, __return_address);
259 /* Record corruption in a block indexed by a file fork. */
261 xchk_fblock_set_corrupt(
262 struct xfs_scrub *sc,
264 xfs_fileoff_t offset)
266 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
267 trace_xchk_fblock_error(sc, whichfork, offset, __return_address);
270 /* Record a corruption while cross-referencing a fork block. */
272 xchk_fblock_xref_set_corrupt(
273 struct xfs_scrub *sc,
275 xfs_fileoff_t offset)
277 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
278 trace_xchk_fblock_error(sc, whichfork, offset, __return_address);
282 * Warn about inodes that need administrative review but is not
286 xchk_ino_set_warning(
287 struct xfs_scrub *sc,
290 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
291 trace_xchk_ino_warning(sc, ino, __return_address);
294 /* Warn about a block indexed by a file fork that needs review. */
296 xchk_fblock_set_warning(
297 struct xfs_scrub *sc,
299 xfs_fileoff_t offset)
301 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
302 trace_xchk_fblock_warning(sc, whichfork, offset, __return_address);
305 /* Signal an incomplete scrub. */
308 struct xfs_scrub *sc)
310 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_INCOMPLETE;
311 trace_xchk_incomplete(sc, __return_address);
315 * rmap scrubbing -- compute the number of blocks with a given owner,
316 * at least according to the reverse mapping data.
319 struct xchk_rmap_ownedby_info {
320 const struct xfs_owner_info *oinfo;
321 xfs_filblks_t *blocks;
325 xchk_count_rmap_ownedby_irec(
326 struct xfs_btree_cur *cur,
327 struct xfs_rmap_irec *rec,
330 struct xchk_rmap_ownedby_info *sroi = priv;
334 irec_attr = rec->rm_flags & XFS_RMAP_ATTR_FORK;
335 oinfo_attr = sroi->oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK;
337 if (rec->rm_owner != sroi->oinfo->oi_owner)
340 if (XFS_RMAP_NON_INODE_OWNER(rec->rm_owner) || irec_attr == oinfo_attr)
341 (*sroi->blocks) += rec->rm_blockcount;
347 * Calculate the number of blocks the rmap thinks are owned by something.
348 * The caller should pass us an rmapbt cursor.
351 xchk_count_rmap_ownedby_ag(
352 struct xfs_scrub *sc,
353 struct xfs_btree_cur *cur,
354 const struct xfs_owner_info *oinfo,
355 xfs_filblks_t *blocks)
357 struct xchk_rmap_ownedby_info sroi = {
363 return xfs_rmap_query_all(cur, xchk_count_rmap_ownedby_irec,
370 * These helpers facilitate locking an allocation group's header
371 * buffers, setting up cursors for all btrees that are present, and
372 * cleaning everything up once we're through.
375 /* Decide if we want to return an AG header read failure. */
377 want_ag_read_header_failure(
378 struct xfs_scrub *sc,
381 /* Return all AG header read failures when scanning btrees. */
382 if (sc->sm->sm_type != XFS_SCRUB_TYPE_AGF &&
383 sc->sm->sm_type != XFS_SCRUB_TYPE_AGFL &&
384 sc->sm->sm_type != XFS_SCRUB_TYPE_AGI)
387 * If we're scanning a given type of AG header, we only want to
388 * see read failures from that specific header. We'd like the
389 * other headers to cross-check them, but this isn't required.
391 if (sc->sm->sm_type == type)
397 * Grab all the headers for an AG.
399 * The headers should be released by xchk_ag_free, but as a fail
400 * safe we attach all the buffers we grab to the scrub transaction so
401 * they'll all be freed when we cancel it.
404 xchk_ag_read_headers(
405 struct xfs_scrub *sc,
409 struct xfs_mount *mp = sc->mp;
414 error = xfs_ialloc_read_agi(mp, sc->tp, agno, &sa->agi_bp);
415 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGI))
418 error = xfs_alloc_read_agf(mp, sc->tp, agno, 0, &sa->agf_bp);
419 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGF))
422 error = xfs_alloc_read_agfl(mp, sc->tp, agno, &sa->agfl_bp);
423 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGFL))
430 /* Release all the AG btree cursors. */
436 xfs_btree_del_cursor(sa->refc_cur, XFS_BTREE_ERROR);
438 xfs_btree_del_cursor(sa->rmap_cur, XFS_BTREE_ERROR);
440 xfs_btree_del_cursor(sa->fino_cur, XFS_BTREE_ERROR);
442 xfs_btree_del_cursor(sa->ino_cur, XFS_BTREE_ERROR);
444 xfs_btree_del_cursor(sa->cnt_cur, XFS_BTREE_ERROR);
446 xfs_btree_del_cursor(sa->bno_cur, XFS_BTREE_ERROR);
456 /* Initialize all the btree cursors for an AG. */
459 struct xfs_scrub *sc,
462 struct xfs_mount *mp = sc->mp;
463 xfs_agnumber_t agno = sa->agno;
465 xchk_perag_get(sc->mp, sa);
467 xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_BNO)) {
468 /* Set up a bnobt cursor for cross-referencing. */
469 sa->bno_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
470 agno, XFS_BTNUM_BNO);
474 xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_CNT)) {
475 /* Set up a cntbt cursor for cross-referencing. */
476 sa->cnt_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
477 agno, XFS_BTNUM_CNT);
480 /* Set up a inobt cursor for cross-referencing. */
482 xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_INO)) {
483 sa->ino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp,
484 agno, XFS_BTNUM_INO);
487 /* Set up a finobt cursor for cross-referencing. */
488 if (sa->agi_bp && xfs_sb_version_hasfinobt(&mp->m_sb) &&
489 xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_FINO)) {
490 sa->fino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp,
491 agno, XFS_BTNUM_FINO);
494 /* Set up a rmapbt cursor for cross-referencing. */
495 if (sa->agf_bp && xfs_sb_version_hasrmapbt(&mp->m_sb) &&
496 xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_RMAP)) {
497 sa->rmap_cur = xfs_rmapbt_init_cursor(mp, sc->tp, sa->agf_bp,
501 /* Set up a refcountbt cursor for cross-referencing. */
502 if (sa->agf_bp && xfs_sb_version_hasreflink(&mp->m_sb) &&
503 xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_REFC)) {
504 sa->refc_cur = xfs_refcountbt_init_cursor(mp, sc->tp,
509 /* Release the AG header context and btree cursors. */
512 struct xfs_scrub *sc,
515 xchk_ag_btcur_free(sa);
517 xfs_trans_brelse(sc->tp, sa->agfl_bp);
521 xfs_trans_brelse(sc->tp, sa->agf_bp);
525 xfs_trans_brelse(sc->tp, sa->agi_bp);
529 xfs_perag_put(sa->pag);
532 sa->agno = NULLAGNUMBER;
536 * For scrub, grab the AGI and the AGF headers, in that order. Locking
537 * order requires us to get the AGI before the AGF. We use the
538 * transaction to avoid deadlocking on crosslinked metadata buffers;
539 * either the caller passes one in (bmap scrub) or we have to create a
540 * transaction ourselves.
544 struct xfs_scrub *sc,
550 error = xchk_ag_read_headers(sc, agno, sa);
554 xchk_ag_btcur_init(sc, sa);
559 * Grab the per-ag structure if we haven't already gotten it. Teardown of the
560 * xchk_ag will release it for us.
564 struct xfs_mount *mp,
568 sa->pag = xfs_perag_get(mp, sa->agno);
571 /* Per-scrubber setup functions */
574 * Grab an empty transaction so that we can re-grab locked buffers if
575 * one of our btrees turns out to be cyclic.
577 * If we're going to repair something, we need to ask for the largest possible
578 * log reservation so that we can handle the worst case scenario for metadata
579 * updates while rebuilding a metadata item. We also need to reserve as many
580 * blocks in the head transaction as we think we're going to need to rebuild
581 * the metadata object.
585 struct xfs_scrub *sc,
588 if (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)
589 return xfs_trans_alloc(sc->mp, &M_RES(sc->mp)->tr_itruncate,
590 resblks, 0, 0, &sc->tp);
592 return xfs_trans_alloc_empty(sc->mp, &sc->tp);
595 /* Set us up with a transaction and an empty context. */
598 struct xfs_scrub *sc)
602 resblks = xrep_calc_ag_resblks(sc);
603 return xchk_trans_alloc(sc, resblks);
606 /* Set us up with AG headers and btree cursors. */
609 struct xfs_scrub *sc,
612 struct xfs_mount *mp = sc->mp;
616 * If the caller asks us to checkpont the log, do so. This
617 * expensive operation should be performed infrequently and only
618 * as a last resort. Any caller that sets force_log should
619 * document why they need to do so.
622 error = xchk_checkpoint_log(mp);
627 error = xchk_setup_fs(sc);
631 return xchk_ag_init(sc, sc->sm->sm_agno, &sc->sa);
634 /* Push everything out of the log onto disk. */
637 struct xfs_mount *mp)
641 error = xfs_log_force(mp, XFS_LOG_SYNC);
644 xfs_ail_push_all_sync(mp->m_ail);
649 * Given an inode and the scrub control structure, grab either the
650 * inode referenced in the control structure or the inode passed in.
651 * The inode is not locked.
655 struct xfs_scrub *sc)
657 struct xfs_imap imap;
658 struct xfs_mount *mp = sc->mp;
659 struct xfs_inode *ip_in = XFS_I(file_inode(sc->file));
660 struct xfs_inode *ip = NULL;
663 /* We want to scan the inode we already had opened. */
664 if (sc->sm->sm_ino == 0 || sc->sm->sm_ino == ip_in->i_ino) {
669 /* Look up the inode, see if the generation number matches. */
670 if (xfs_internal_inum(mp, sc->sm->sm_ino))
672 error = xfs_iget(mp, NULL, sc->sm->sm_ino,
673 XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE, 0, &ip);
676 /* Inode doesn't exist, just bail out. */
679 /* Got an inode, continue. */
683 * -EINVAL with IGET_UNTRUSTED could mean one of several
684 * things: userspace gave us an inode number that doesn't
685 * correspond to fs space, or doesn't have an inobt entry;
686 * or it could simply mean that the inode buffer failed the
689 * Try just the inode mapping lookup -- if it succeeds, then
690 * the inode buffer verifier failed and something needs fixing.
691 * Otherwise, we really couldn't find it so tell userspace
692 * that it no longer exists.
694 error = xfs_imap(sc->mp, sc->tp, sc->sm->sm_ino, &imap,
695 XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE);
698 error = -EFSCORRUPTED;
701 trace_xchk_op_error(sc,
702 XFS_INO_TO_AGNO(mp, sc->sm->sm_ino),
703 XFS_INO_TO_AGBNO(mp, sc->sm->sm_ino),
704 error, __return_address);
707 if (VFS_I(ip)->i_generation != sc->sm->sm_gen) {
716 /* Set us up to scrub a file's contents. */
718 xchk_setup_inode_contents(
719 struct xfs_scrub *sc,
720 unsigned int resblks)
724 error = xchk_get_inode(sc);
728 /* Got the inode, lock it and we're ready to go. */
729 sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
730 xfs_ilock(sc->ip, sc->ilock_flags);
731 error = xchk_trans_alloc(sc, resblks);
734 sc->ilock_flags |= XFS_ILOCK_EXCL;
735 xfs_ilock(sc->ip, XFS_ILOCK_EXCL);
738 /* scrub teardown will unlock and release the inode for us */
743 * Predicate that decides if we need to evaluate the cross-reference check.
744 * If there was an error accessing the cross-reference btree, just delete
745 * the cursor and skip the check.
748 xchk_should_check_xref(
749 struct xfs_scrub *sc,
751 struct xfs_btree_cur **curpp)
753 /* No point in xref if we already know we're corrupt. */
754 if (xchk_skip_xref(sc->sm))
761 /* If we've already given up on xref, just bail out. */
765 /* xref error, delete cursor and bail out. */
766 xfs_btree_del_cursor(*curpp, XFS_BTREE_ERROR);
770 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XFAIL;
771 trace_xchk_xref_error(sc, *error, __return_address);
774 * Errors encountered during cross-referencing with another
775 * data structure should not cause this scrubber to abort.
781 /* Run the structure verifiers on in-memory buffers to detect bad memory. */
784 struct xfs_scrub *sc,
789 if (bp->b_ops == NULL) {
790 xchk_block_set_corrupt(sc, bp);
793 if (bp->b_ops->verify_struct == NULL) {
794 xchk_set_incomplete(sc);
797 fa = bp->b_ops->verify_struct(bp);
800 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
801 trace_xchk_block_error(sc, bp->b_bn, fa);
805 * Scrub the attr/data forks of a metadata inode. The metadata inode must be
806 * pointed to by sc->ip and the ILOCK must be held.
809 xchk_metadata_inode_forks(
810 struct xfs_scrub *sc)
816 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
819 /* Metadata inodes don't live on the rt device. */
820 if (sc->ip->i_diflags & XFS_DIFLAG_REALTIME) {
821 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
825 /* They should never participate in reflink. */
826 if (xfs_is_reflink_inode(sc->ip)) {
827 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
831 /* They also should never have extended attributes. */
832 if (xfs_inode_hasattr(sc->ip)) {
833 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
837 /* Invoke the data fork scrubber. */
838 smtype = sc->sm->sm_type;
839 sc->sm->sm_type = XFS_SCRUB_TYPE_BMBTD;
840 error = xchk_bmap_data(sc);
841 sc->sm->sm_type = smtype;
842 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
845 /* Look for incorrect shared blocks. */
846 if (xfs_sb_version_hasreflink(&sc->mp->m_sb)) {
847 error = xfs_reflink_inode_has_shared_extents(sc->tp, sc->ip,
849 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0,
853 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
860 * Try to lock an inode in violation of the usual locking order rules. For
861 * example, trying to get the IOLOCK while in transaction context, or just
862 * plain breaking AG-order or inode-order inode locking rules. Either way,
863 * the only way to avoid an ABBA deadlock is to use trylock and back off if
868 struct xfs_inode *ip,
873 for (i = 0; i < 20; i++) {
874 if (xfs_ilock_nowait(ip, lock_mode))
881 /* Pause background reaping of resources. */
884 struct xfs_scrub *sc)
886 sc->flags |= XCHK_REAPING_DISABLED;
887 xfs_blockgc_stop(sc->mp);
890 /* Restart background reaping of resources. */
893 struct xfs_scrub *sc)
895 xfs_blockgc_start(sc->mp);
896 sc->flags &= ~XCHK_REAPING_DISABLED;