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_defer.h"
13 #include "xfs_btree.h"
15 #include "xfs_log_format.h"
16 #include "xfs_trans.h"
18 #include "xfs_inode.h"
19 #include "xfs_icache.h"
20 #include "xfs_itable.h"
21 #include "xfs_alloc.h"
22 #include "xfs_alloc_btree.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"
30 #include "xfs_rmap_btree.h"
32 #include "xfs_trans_priv.h"
34 #include "xfs_reflink.h"
35 #include "scrub/xfs_scrub.h"
36 #include "scrub/scrub.h"
37 #include "scrub/common.h"
38 #include "scrub/trace.h"
39 #include "scrub/btree.h"
40 #include "scrub/repair.h"
41 #include "scrub/health.h"
43 /* Common code for the metadata scrubbers. */
46 * Handling operational errors.
48 * The *_process_error() family of functions are used to process error return
49 * codes from functions called as part of a scrub operation.
51 * If there's no error, we return true to tell the caller that it's ok
52 * to move on to the next check in its list.
54 * For non-verifier errors (e.g. ENOMEM) we return false to tell the
55 * caller that something bad happened, and we preserve *error so that
56 * the caller can return the *error up the stack to userspace.
58 * Verifier errors (EFSBADCRC/EFSCORRUPTED) are recorded by setting
59 * OFLAG_CORRUPT in sm_flags and the *error is cleared. In other words,
60 * we track verifier errors (and failed scrub checks) via OFLAG_CORRUPT,
61 * not via return codes. We return false to tell the caller that
62 * something bad happened. Since the error has been cleared, the caller
63 * will (presumably) return that zero and scrubbing will move on to
66 * ftrace can be used to record the precise metadata location and the
67 * approximate code location of the failed operation.
70 /* Check for operational errors. */
84 /* Used to restart an op with deadlock avoidance. */
85 trace_xchk_deadlock_retry(sc->ip, sc->sm, *error);
89 /* Note the badness but don't abort. */
90 sc->sm->sm_flags |= errflag;
94 trace_xchk_op_error(sc, agno, bno, *error,
103 struct xfs_scrub *sc,
108 return __xchk_process_error(sc, agno, bno, error,
109 XFS_SCRUB_OFLAG_CORRUPT, __return_address);
113 xchk_xref_process_error(
114 struct xfs_scrub *sc,
119 return __xchk_process_error(sc, agno, bno, error,
120 XFS_SCRUB_OFLAG_XFAIL, __return_address);
123 /* Check for operational errors for a file offset. */
125 __xchk_fblock_process_error(
126 struct xfs_scrub *sc,
128 xfs_fileoff_t offset,
137 /* Used to restart an op with deadlock avoidance. */
138 trace_xchk_deadlock_retry(sc->ip, sc->sm, *error);
142 /* Note the badness but don't abort. */
143 sc->sm->sm_flags |= errflag;
147 trace_xchk_file_op_error(sc, whichfork, offset, *error,
155 xchk_fblock_process_error(
156 struct xfs_scrub *sc,
158 xfs_fileoff_t offset,
161 return __xchk_fblock_process_error(sc, whichfork, offset, error,
162 XFS_SCRUB_OFLAG_CORRUPT, __return_address);
166 xchk_fblock_xref_process_error(
167 struct xfs_scrub *sc,
169 xfs_fileoff_t offset,
172 return __xchk_fblock_process_error(sc, whichfork, offset, error,
173 XFS_SCRUB_OFLAG_XFAIL, __return_address);
177 * Handling scrub corruption/optimization/warning checks.
179 * The *_set_{corrupt,preen,warning}() family of functions are used to
180 * record the presence of metadata that is incorrect (corrupt), could be
181 * optimized somehow (preen), or should be flagged for administrative
182 * review but is not incorrect (warn).
184 * ftrace can be used to record the precise metadata location and
185 * approximate code location of the failed check.
188 /* Record a block which could be optimized. */
190 xchk_block_set_preen(
191 struct xfs_scrub *sc,
194 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
195 trace_xchk_block_preen(sc, bp->b_bn, __return_address);
199 * Record an inode which could be optimized. The trace data will
200 * include the block given by bp if bp is given; otherwise it will use
201 * the block location of the inode record itself.
205 struct xfs_scrub *sc,
208 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
209 trace_xchk_ino_preen(sc, ino, __return_address);
212 /* Record a corrupt block. */
214 xchk_block_set_corrupt(
215 struct xfs_scrub *sc,
218 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
219 trace_xchk_block_error(sc, bp->b_bn, __return_address);
222 /* Record a corruption while cross-referencing. */
224 xchk_block_xref_set_corrupt(
225 struct xfs_scrub *sc,
228 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
229 trace_xchk_block_error(sc, bp->b_bn, __return_address);
233 * Record a corrupt inode. The trace data will include the block given
234 * by bp if bp is given; otherwise it will use the block location of the
235 * inode record itself.
238 xchk_ino_set_corrupt(
239 struct xfs_scrub *sc,
242 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
243 trace_xchk_ino_error(sc, ino, __return_address);
246 /* Record a corruption while cross-referencing with an inode. */
248 xchk_ino_xref_set_corrupt(
249 struct xfs_scrub *sc,
252 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
253 trace_xchk_ino_error(sc, ino, __return_address);
256 /* Record corruption in a block indexed by a file fork. */
258 xchk_fblock_set_corrupt(
259 struct xfs_scrub *sc,
261 xfs_fileoff_t offset)
263 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
264 trace_xchk_fblock_error(sc, whichfork, offset, __return_address);
267 /* Record a corruption while cross-referencing a fork block. */
269 xchk_fblock_xref_set_corrupt(
270 struct xfs_scrub *sc,
272 xfs_fileoff_t offset)
274 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
275 trace_xchk_fblock_error(sc, whichfork, offset, __return_address);
279 * Warn about inodes that need administrative review but is not
283 xchk_ino_set_warning(
284 struct xfs_scrub *sc,
287 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
288 trace_xchk_ino_warning(sc, ino, __return_address);
291 /* Warn about a block indexed by a file fork that needs review. */
293 xchk_fblock_set_warning(
294 struct xfs_scrub *sc,
296 xfs_fileoff_t offset)
298 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
299 trace_xchk_fblock_warning(sc, whichfork, offset, __return_address);
302 /* Signal an incomplete scrub. */
305 struct xfs_scrub *sc)
307 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_INCOMPLETE;
308 trace_xchk_incomplete(sc, __return_address);
312 * rmap scrubbing -- compute the number of blocks with a given owner,
313 * at least according to the reverse mapping data.
316 struct xchk_rmap_ownedby_info {
317 const struct xfs_owner_info *oinfo;
318 xfs_filblks_t *blocks;
322 xchk_count_rmap_ownedby_irec(
323 struct xfs_btree_cur *cur,
324 struct xfs_rmap_irec *rec,
327 struct xchk_rmap_ownedby_info *sroi = priv;
331 irec_attr = rec->rm_flags & XFS_RMAP_ATTR_FORK;
332 oinfo_attr = sroi->oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK;
334 if (rec->rm_owner != sroi->oinfo->oi_owner)
337 if (XFS_RMAP_NON_INODE_OWNER(rec->rm_owner) || irec_attr == oinfo_attr)
338 (*sroi->blocks) += rec->rm_blockcount;
344 * Calculate the number of blocks the rmap thinks are owned by something.
345 * The caller should pass us an rmapbt cursor.
348 xchk_count_rmap_ownedby_ag(
349 struct xfs_scrub *sc,
350 struct xfs_btree_cur *cur,
351 const struct xfs_owner_info *oinfo,
352 xfs_filblks_t *blocks)
354 struct xchk_rmap_ownedby_info sroi = {
360 return xfs_rmap_query_all(cur, xchk_count_rmap_ownedby_irec,
367 * These helpers facilitate locking an allocation group's header
368 * buffers, setting up cursors for all btrees that are present, and
369 * cleaning everything up once we're through.
372 /* Decide if we want to return an AG header read failure. */
374 want_ag_read_header_failure(
375 struct xfs_scrub *sc,
378 /* Return all AG header read failures when scanning btrees. */
379 if (sc->sm->sm_type != XFS_SCRUB_TYPE_AGF &&
380 sc->sm->sm_type != XFS_SCRUB_TYPE_AGFL &&
381 sc->sm->sm_type != XFS_SCRUB_TYPE_AGI)
384 * If we're scanning a given type of AG header, we only want to
385 * see read failures from that specific header. We'd like the
386 * other headers to cross-check them, but this isn't required.
388 if (sc->sm->sm_type == type)
394 * Grab all the headers for an AG.
396 * The headers should be released by xchk_ag_free, but as a fail
397 * safe we attach all the buffers we grab to the scrub transaction so
398 * they'll all be freed when we cancel it.
401 xchk_ag_read_headers(
402 struct xfs_scrub *sc,
404 struct xfs_buf **agi,
405 struct xfs_buf **agf,
406 struct xfs_buf **agfl)
408 struct xfs_mount *mp = sc->mp;
411 error = xfs_ialloc_read_agi(mp, sc->tp, agno, agi);
412 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGI))
415 error = xfs_alloc_read_agf(mp, sc->tp, agno, 0, agf);
416 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGF))
419 error = xfs_alloc_read_agfl(mp, sc->tp, agno, agfl);
420 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGFL))
427 /* Release all the AG btree cursors. */
433 xfs_btree_del_cursor(sa->refc_cur, XFS_BTREE_ERROR);
435 xfs_btree_del_cursor(sa->rmap_cur, XFS_BTREE_ERROR);
437 xfs_btree_del_cursor(sa->fino_cur, XFS_BTREE_ERROR);
439 xfs_btree_del_cursor(sa->ino_cur, XFS_BTREE_ERROR);
441 xfs_btree_del_cursor(sa->cnt_cur, XFS_BTREE_ERROR);
443 xfs_btree_del_cursor(sa->bno_cur, XFS_BTREE_ERROR);
453 /* Initialize all the btree cursors for an AG. */
456 struct xfs_scrub *sc,
459 struct xfs_mount *mp = sc->mp;
460 xfs_agnumber_t agno = sa->agno;
462 xchk_perag_get(sc->mp, sa);
464 xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_BNO)) {
465 /* Set up a bnobt cursor for cross-referencing. */
466 sa->bno_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
467 agno, XFS_BTNUM_BNO);
473 xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_CNT)) {
474 /* Set up a cntbt cursor for cross-referencing. */
475 sa->cnt_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
476 agno, XFS_BTNUM_CNT);
481 /* Set up a inobt cursor for cross-referencing. */
483 xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_INO)) {
484 sa->ino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp,
485 agno, XFS_BTNUM_INO);
490 /* Set up a finobt cursor for cross-referencing. */
491 if (sa->agi_bp && xfs_sb_version_hasfinobt(&mp->m_sb) &&
492 xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_FINO)) {
493 sa->fino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp,
494 agno, XFS_BTNUM_FINO);
499 /* Set up a rmapbt cursor for cross-referencing. */
500 if (sa->agf_bp && xfs_sb_version_hasrmapbt(&mp->m_sb) &&
501 xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_RMAP)) {
502 sa->rmap_cur = xfs_rmapbt_init_cursor(mp, sc->tp, sa->agf_bp,
508 /* Set up a refcountbt cursor for cross-referencing. */
509 if (sa->agf_bp && xfs_sb_version_hasreflink(&mp->m_sb) &&
510 xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_REFC)) {
511 sa->refc_cur = xfs_refcountbt_init_cursor(mp, sc->tp,
522 /* Release the AG header context and btree cursors. */
525 struct xfs_scrub *sc,
528 xchk_ag_btcur_free(sa);
530 xfs_trans_brelse(sc->tp, sa->agfl_bp);
534 xfs_trans_brelse(sc->tp, sa->agf_bp);
538 xfs_trans_brelse(sc->tp, sa->agi_bp);
542 xfs_perag_put(sa->pag);
545 sa->agno = NULLAGNUMBER;
549 * For scrub, grab the AGI and the AGF headers, in that order. Locking
550 * order requires us to get the AGI before the AGF. We use the
551 * transaction to avoid deadlocking on crosslinked metadata buffers;
552 * either the caller passes one in (bmap scrub) or we have to create a
553 * transaction ourselves.
557 struct xfs_scrub *sc,
564 error = xchk_ag_read_headers(sc, agno, &sa->agi_bp,
565 &sa->agf_bp, &sa->agfl_bp);
569 return xchk_ag_btcur_init(sc, sa);
573 * Grab the per-ag structure if we haven't already gotten it. Teardown of the
574 * xchk_ag will release it for us.
578 struct xfs_mount *mp,
582 sa->pag = xfs_perag_get(mp, sa->agno);
585 /* Per-scrubber setup functions */
588 * Grab an empty transaction so that we can re-grab locked buffers if
589 * one of our btrees turns out to be cyclic.
591 * If we're going to repair something, we need to ask for the largest possible
592 * log reservation so that we can handle the worst case scenario for metadata
593 * updates while rebuilding a metadata item. We also need to reserve as many
594 * blocks in the head transaction as we think we're going to need to rebuild
595 * the metadata object.
599 struct xfs_scrub *sc,
602 if (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)
603 return xfs_trans_alloc(sc->mp, &M_RES(sc->mp)->tr_itruncate,
604 resblks, 0, 0, &sc->tp);
606 return xfs_trans_alloc_empty(sc->mp, &sc->tp);
609 /* Set us up with a transaction and an empty context. */
612 struct xfs_scrub *sc,
613 struct xfs_inode *ip)
617 resblks = xrep_calc_ag_resblks(sc);
618 return xchk_trans_alloc(sc, resblks);
621 /* Set us up with AG headers and btree cursors. */
624 struct xfs_scrub *sc,
625 struct xfs_inode *ip,
628 struct xfs_mount *mp = sc->mp;
632 * If the caller asks us to checkpont the log, do so. This
633 * expensive operation should be performed infrequently and only
634 * as a last resort. Any caller that sets force_log should
635 * document why they need to do so.
638 error = xchk_checkpoint_log(mp);
643 error = xchk_setup_fs(sc, ip);
647 return xchk_ag_init(sc, sc->sm->sm_agno, &sc->sa);
650 /* Push everything out of the log onto disk. */
653 struct xfs_mount *mp)
657 error = xfs_log_force(mp, XFS_LOG_SYNC);
660 xfs_ail_push_all_sync(mp->m_ail);
665 * Given an inode and the scrub control structure, grab either the
666 * inode referenced in the control structure or the inode passed in.
667 * The inode is not locked.
671 struct xfs_scrub *sc,
672 struct xfs_inode *ip_in)
674 struct xfs_imap imap;
675 struct xfs_mount *mp = sc->mp;
676 struct xfs_inode *ip = NULL;
679 /* We want to scan the inode we already had opened. */
680 if (sc->sm->sm_ino == 0 || sc->sm->sm_ino == ip_in->i_ino) {
685 /* Look up the inode, see if the generation number matches. */
686 if (xfs_internal_inum(mp, sc->sm->sm_ino))
688 error = xfs_iget(mp, NULL, sc->sm->sm_ino,
689 XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE, 0, &ip);
692 /* Inode doesn't exist, just bail out. */
695 /* Got an inode, continue. */
699 * -EINVAL with IGET_UNTRUSTED could mean one of several
700 * things: userspace gave us an inode number that doesn't
701 * correspond to fs space, or doesn't have an inobt entry;
702 * or it could simply mean that the inode buffer failed the
705 * Try just the inode mapping lookup -- if it succeeds, then
706 * the inode buffer verifier failed and something needs fixing.
707 * Otherwise, we really couldn't find it so tell userspace
708 * that it no longer exists.
710 error = xfs_imap(sc->mp, sc->tp, sc->sm->sm_ino, &imap,
711 XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE);
714 error = -EFSCORRUPTED;
717 trace_xchk_op_error(sc,
718 XFS_INO_TO_AGNO(mp, sc->sm->sm_ino),
719 XFS_INO_TO_AGBNO(mp, sc->sm->sm_ino),
720 error, __return_address);
723 if (VFS_I(ip)->i_generation != sc->sm->sm_gen) {
732 /* Set us up to scrub a file's contents. */
734 xchk_setup_inode_contents(
735 struct xfs_scrub *sc,
736 struct xfs_inode *ip,
737 unsigned int resblks)
741 error = xchk_get_inode(sc, ip);
745 /* Got the inode, lock it and we're ready to go. */
746 sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
747 xfs_ilock(sc->ip, sc->ilock_flags);
748 error = xchk_trans_alloc(sc, resblks);
751 sc->ilock_flags |= XFS_ILOCK_EXCL;
752 xfs_ilock(sc->ip, XFS_ILOCK_EXCL);
755 /* scrub teardown will unlock and release the inode for us */
760 * Predicate that decides if we need to evaluate the cross-reference check.
761 * If there was an error accessing the cross-reference btree, just delete
762 * the cursor and skip the check.
765 xchk_should_check_xref(
766 struct xfs_scrub *sc,
768 struct xfs_btree_cur **curpp)
770 /* No point in xref if we already know we're corrupt. */
771 if (xchk_skip_xref(sc->sm))
778 /* If we've already given up on xref, just bail out. */
782 /* xref error, delete cursor and bail out. */
783 xfs_btree_del_cursor(*curpp, XFS_BTREE_ERROR);
787 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XFAIL;
788 trace_xchk_xref_error(sc, *error, __return_address);
791 * Errors encountered during cross-referencing with another
792 * data structure should not cause this scrubber to abort.
798 /* Run the structure verifiers on in-memory buffers to detect bad memory. */
801 struct xfs_scrub *sc,
806 if (bp->b_ops == NULL) {
807 xchk_block_set_corrupt(sc, bp);
810 if (bp->b_ops->verify_struct == NULL) {
811 xchk_set_incomplete(sc);
814 fa = bp->b_ops->verify_struct(bp);
817 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
818 trace_xchk_block_error(sc, bp->b_bn, fa);
822 * Scrub the attr/data forks of a metadata inode. The metadata inode must be
823 * pointed to by sc->ip and the ILOCK must be held.
826 xchk_metadata_inode_forks(
827 struct xfs_scrub *sc)
833 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
836 /* Metadata inodes don't live on the rt device. */
837 if (sc->ip->i_d.di_flags & XFS_DIFLAG_REALTIME) {
838 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
842 /* They should never participate in reflink. */
843 if (xfs_is_reflink_inode(sc->ip)) {
844 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
848 /* They also should never have extended attributes. */
849 if (xfs_inode_hasattr(sc->ip)) {
850 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
854 /* Invoke the data fork scrubber. */
855 smtype = sc->sm->sm_type;
856 sc->sm->sm_type = XFS_SCRUB_TYPE_BMBTD;
857 error = xchk_bmap_data(sc);
858 sc->sm->sm_type = smtype;
859 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
862 /* Look for incorrect shared blocks. */
863 if (xfs_sb_version_hasreflink(&sc->mp->m_sb)) {
864 error = xfs_reflink_inode_has_shared_extents(sc->tp, sc->ip,
866 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0,
870 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
877 * Try to lock an inode in violation of the usual locking order rules. For
878 * example, trying to get the IOLOCK while in transaction context, or just
879 * plain breaking AG-order or inode-order inode locking rules. Either way,
880 * the only way to avoid an ABBA deadlock is to use trylock and back off if
885 struct xfs_inode *ip,
890 for (i = 0; i < 20; i++) {
891 if (xfs_ilock_nowait(ip, lock_mode))
898 /* Pause background reaping of resources. */
901 struct xfs_scrub *sc)
903 sc->flags |= XCHK_REAPING_DISABLED;
904 xfs_stop_block_reaping(sc->mp);
907 /* Restart background reaping of resources. */
910 struct xfs_scrub *sc)
912 xfs_start_block_reaping(sc->mp);
913 sc->flags &= ~XCHK_REAPING_DISABLED;