1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Oracle. All Rights Reserved.
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"
42 /* Common code for the metadata scrubbers. */
45 * Handling operational errors.
47 * The *_process_error() family of functions are used to process error return
48 * codes from functions called as part of a scrub operation.
50 * If there's no error, we return true to tell the caller that it's ok
51 * to move on to the next check in its list.
53 * For non-verifier errors (e.g. ENOMEM) we return false to tell the
54 * caller that something bad happened, and we preserve *error so that
55 * the caller can return the *error up the stack to userspace.
57 * Verifier errors (EFSBADCRC/EFSCORRUPTED) are recorded by setting
58 * OFLAG_CORRUPT in sm_flags and the *error is cleared. In other words,
59 * we track verifier errors (and failed scrub checks) via OFLAG_CORRUPT,
60 * not via return codes. We return false to tell the caller that
61 * something bad happened. Since the error has been cleared, the caller
62 * will (presumably) return that zero and scrubbing will move on to
65 * ftrace can be used to record the precise metadata location and the
66 * approximate code location of the failed operation.
69 /* Check for operational errors. */
83 /* Used to restart an op with deadlock avoidance. */
84 trace_xchk_deadlock_retry(sc->ip, sc->sm, *error);
88 /* Note the badness but don't abort. */
89 sc->sm->sm_flags |= errflag;
93 trace_xchk_op_error(sc, agno, bno, *error,
102 struct xfs_scrub *sc,
107 return __xchk_process_error(sc, agno, bno, error,
108 XFS_SCRUB_OFLAG_CORRUPT, __return_address);
112 xchk_xref_process_error(
113 struct xfs_scrub *sc,
118 return __xchk_process_error(sc, agno, bno, error,
119 XFS_SCRUB_OFLAG_XFAIL, __return_address);
122 /* Check for operational errors for a file offset. */
124 __xchk_fblock_process_error(
125 struct xfs_scrub *sc,
127 xfs_fileoff_t offset,
136 /* Used to restart an op with deadlock avoidance. */
137 trace_xchk_deadlock_retry(sc->ip, sc->sm, *error);
141 /* Note the badness but don't abort. */
142 sc->sm->sm_flags |= errflag;
146 trace_xchk_file_op_error(sc, whichfork, offset, *error,
154 xchk_fblock_process_error(
155 struct xfs_scrub *sc,
157 xfs_fileoff_t offset,
160 return __xchk_fblock_process_error(sc, whichfork, offset, error,
161 XFS_SCRUB_OFLAG_CORRUPT, __return_address);
165 xchk_fblock_xref_process_error(
166 struct xfs_scrub *sc,
168 xfs_fileoff_t offset,
171 return __xchk_fblock_process_error(sc, whichfork, offset, error,
172 XFS_SCRUB_OFLAG_XFAIL, __return_address);
176 * Handling scrub corruption/optimization/warning checks.
178 * The *_set_{corrupt,preen,warning}() family of functions are used to
179 * record the presence of metadata that is incorrect (corrupt), could be
180 * optimized somehow (preen), or should be flagged for administrative
181 * review but is not incorrect (warn).
183 * ftrace can be used to record the precise metadata location and
184 * approximate code location of the failed check.
187 /* Record a block which could be optimized. */
189 xchk_block_set_preen(
190 struct xfs_scrub *sc,
193 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
194 trace_xchk_block_preen(sc, bp->b_bn, __return_address);
198 * Record an inode which could be optimized. The trace data will
199 * include the block given by bp if bp is given; otherwise it will use
200 * the block location of the inode record itself.
204 struct xfs_scrub *sc,
207 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
208 trace_xchk_ino_preen(sc, ino, __return_address);
211 /* Record a corrupt block. */
213 xchk_block_set_corrupt(
214 struct xfs_scrub *sc,
217 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
218 trace_xchk_block_error(sc, bp->b_bn, __return_address);
221 /* Record a corruption while cross-referencing. */
223 xchk_block_xref_set_corrupt(
224 struct xfs_scrub *sc,
227 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
228 trace_xchk_block_error(sc, bp->b_bn, __return_address);
232 * Record a corrupt inode. The trace data will include the block given
233 * by bp if bp is given; otherwise it will use the block location of the
234 * inode record itself.
237 xchk_ino_set_corrupt(
238 struct xfs_scrub *sc,
241 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
242 trace_xchk_ino_error(sc, ino, __return_address);
245 /* Record a corruption while cross-referencing with an inode. */
247 xchk_ino_xref_set_corrupt(
248 struct xfs_scrub *sc,
251 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
252 trace_xchk_ino_error(sc, ino, __return_address);
255 /* Record corruption in a block indexed by a file fork. */
257 xchk_fblock_set_corrupt(
258 struct xfs_scrub *sc,
260 xfs_fileoff_t offset)
262 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
263 trace_xchk_fblock_error(sc, whichfork, offset, __return_address);
266 /* Record a corruption while cross-referencing a fork block. */
268 xchk_fblock_xref_set_corrupt(
269 struct xfs_scrub *sc,
271 xfs_fileoff_t offset)
273 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
274 trace_xchk_fblock_error(sc, whichfork, offset, __return_address);
278 * Warn about inodes that need administrative review but is not
282 xchk_ino_set_warning(
283 struct xfs_scrub *sc,
286 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
287 trace_xchk_ino_warning(sc, ino, __return_address);
290 /* Warn about a block indexed by a file fork that needs review. */
292 xchk_fblock_set_warning(
293 struct xfs_scrub *sc,
295 xfs_fileoff_t offset)
297 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
298 trace_xchk_fblock_warning(sc, whichfork, offset, __return_address);
301 /* Signal an incomplete scrub. */
304 struct xfs_scrub *sc)
306 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_INCOMPLETE;
307 trace_xchk_incomplete(sc, __return_address);
311 * rmap scrubbing -- compute the number of blocks with a given owner,
312 * at least according to the reverse mapping data.
315 struct xchk_rmap_ownedby_info {
316 struct xfs_owner_info *oinfo;
317 xfs_filblks_t *blocks;
321 xchk_count_rmap_ownedby_irec(
322 struct xfs_btree_cur *cur,
323 struct xfs_rmap_irec *rec,
326 struct xchk_rmap_ownedby_info *sroi = priv;
330 irec_attr = rec->rm_flags & XFS_RMAP_ATTR_FORK;
331 oinfo_attr = sroi->oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK;
333 if (rec->rm_owner != sroi->oinfo->oi_owner)
336 if (XFS_RMAP_NON_INODE_OWNER(rec->rm_owner) || irec_attr == oinfo_attr)
337 (*sroi->blocks) += rec->rm_blockcount;
343 * Calculate the number of blocks the rmap thinks are owned by something.
344 * The caller should pass us an rmapbt cursor.
347 xchk_count_rmap_ownedby_ag(
348 struct xfs_scrub *sc,
349 struct xfs_btree_cur *cur,
350 struct xfs_owner_info *oinfo,
351 xfs_filblks_t *blocks)
353 struct xchk_rmap_ownedby_info sroi;
357 sroi.blocks = blocks;
359 return xfs_rmap_query_all(cur, xchk_count_rmap_ownedby_irec,
366 * These helpers facilitate locking an allocation group's header
367 * buffers, setting up cursors for all btrees that are present, and
368 * cleaning everything up once we're through.
371 /* Decide if we want to return an AG header read failure. */
373 want_ag_read_header_failure(
374 struct xfs_scrub *sc,
377 /* Return all AG header read failures when scanning btrees. */
378 if (sc->sm->sm_type != XFS_SCRUB_TYPE_AGF &&
379 sc->sm->sm_type != XFS_SCRUB_TYPE_AGFL &&
380 sc->sm->sm_type != XFS_SCRUB_TYPE_AGI)
383 * If we're scanning a given type of AG header, we only want to
384 * see read failures from that specific header. We'd like the
385 * other headers to cross-check them, but this isn't required.
387 if (sc->sm->sm_type == type)
393 * Grab all the headers for an AG.
395 * The headers should be released by xchk_ag_free, but as a fail
396 * safe we attach all the buffers we grab to the scrub transaction so
397 * they'll all be freed when we cancel it.
400 xchk_ag_read_headers(
401 struct xfs_scrub *sc,
403 struct xfs_buf **agi,
404 struct xfs_buf **agf,
405 struct xfs_buf **agfl)
407 struct xfs_mount *mp = sc->mp;
410 error = xfs_ialloc_read_agi(mp, sc->tp, agno, agi);
411 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGI))
414 error = xfs_alloc_read_agf(mp, sc->tp, agno, 0, agf);
415 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGF))
418 error = xfs_alloc_read_agfl(mp, sc->tp, agno, agfl);
419 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGFL))
426 /* Release all the AG btree cursors. */
432 xfs_btree_del_cursor(sa->refc_cur, XFS_BTREE_ERROR);
434 xfs_btree_del_cursor(sa->rmap_cur, XFS_BTREE_ERROR);
436 xfs_btree_del_cursor(sa->fino_cur, XFS_BTREE_ERROR);
438 xfs_btree_del_cursor(sa->ino_cur, XFS_BTREE_ERROR);
440 xfs_btree_del_cursor(sa->cnt_cur, XFS_BTREE_ERROR);
442 xfs_btree_del_cursor(sa->bno_cur, XFS_BTREE_ERROR);
452 /* Initialize all the btree cursors for an AG. */
455 struct xfs_scrub *sc,
458 struct xfs_mount *mp = sc->mp;
459 xfs_agnumber_t agno = sa->agno;
462 /* Set up a bnobt cursor for cross-referencing. */
463 sa->bno_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
464 agno, XFS_BTNUM_BNO);
468 /* Set up a cntbt cursor for cross-referencing. */
469 sa->cnt_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
470 agno, XFS_BTNUM_CNT);
475 /* Set up a inobt cursor for cross-referencing. */
477 sa->ino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp,
478 agno, XFS_BTNUM_INO);
483 /* Set up a finobt cursor for cross-referencing. */
484 if (sa->agi_bp && xfs_sb_version_hasfinobt(&mp->m_sb)) {
485 sa->fino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp,
486 agno, XFS_BTNUM_FINO);
491 /* Set up a rmapbt cursor for cross-referencing. */
492 if (sa->agf_bp && xfs_sb_version_hasrmapbt(&mp->m_sb)) {
493 sa->rmap_cur = xfs_rmapbt_init_cursor(mp, sc->tp, sa->agf_bp,
499 /* Set up a refcountbt cursor for cross-referencing. */
500 if (sa->agf_bp && xfs_sb_version_hasreflink(&mp->m_sb)) {
501 sa->refc_cur = xfs_refcountbt_init_cursor(mp, sc->tp,
512 /* Release the AG header context and btree cursors. */
515 struct xfs_scrub *sc,
518 xchk_ag_btcur_free(sa);
520 xfs_trans_brelse(sc->tp, sa->agfl_bp);
524 xfs_trans_brelse(sc->tp, sa->agf_bp);
528 xfs_trans_brelse(sc->tp, sa->agi_bp);
532 xfs_perag_put(sa->pag);
535 sa->agno = NULLAGNUMBER;
539 * For scrub, grab the AGI and the AGF headers, in that order. Locking
540 * order requires us to get the AGI before the AGF. We use the
541 * transaction to avoid deadlocking on crosslinked metadata buffers;
542 * either the caller passes one in (bmap scrub) or we have to create a
543 * transaction ourselves.
547 struct xfs_scrub *sc,
554 error = xchk_ag_read_headers(sc, agno, &sa->agi_bp,
555 &sa->agf_bp, &sa->agfl_bp);
559 return xchk_ag_btcur_init(sc, sa);
563 * Grab the per-ag structure if we haven't already gotten it. Teardown of the
564 * xchk_ag will release it for us.
568 struct xfs_mount *mp,
572 sa->pag = xfs_perag_get(mp, sa->agno);
575 /* Per-scrubber setup functions */
578 * Grab an empty transaction so that we can re-grab locked buffers if
579 * one of our btrees turns out to be cyclic.
581 * If we're going to repair something, we need to ask for the largest possible
582 * log reservation so that we can handle the worst case scenario for metadata
583 * updates while rebuilding a metadata item. We also need to reserve as many
584 * blocks in the head transaction as we think we're going to need to rebuild
585 * the metadata object.
589 struct xfs_scrub *sc,
592 if (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)
593 return xfs_trans_alloc(sc->mp, &M_RES(sc->mp)->tr_itruncate,
594 resblks, 0, 0, &sc->tp);
596 return xfs_trans_alloc_empty(sc->mp, &sc->tp);
599 /* Set us up with a transaction and an empty context. */
602 struct xfs_scrub *sc,
603 struct xfs_inode *ip)
607 resblks = xrep_calc_ag_resblks(sc);
608 return xchk_trans_alloc(sc, resblks);
611 /* Set us up with AG headers and btree cursors. */
614 struct xfs_scrub *sc,
615 struct xfs_inode *ip,
618 struct xfs_mount *mp = sc->mp;
622 * If the caller asks us to checkpont the log, do so. This
623 * expensive operation should be performed infrequently and only
624 * as a last resort. Any caller that sets force_log should
625 * document why they need to do so.
628 error = xchk_checkpoint_log(mp);
633 error = xchk_setup_fs(sc, ip);
637 return xchk_ag_init(sc, sc->sm->sm_agno, &sc->sa);
640 /* Push everything out of the log onto disk. */
643 struct xfs_mount *mp)
647 error = xfs_log_force(mp, XFS_LOG_SYNC);
650 xfs_ail_push_all_sync(mp->m_ail);
655 * Given an inode and the scrub control structure, grab either the
656 * inode referenced in the control structure or the inode passed in.
657 * The inode is not locked.
661 struct xfs_scrub *sc,
662 struct xfs_inode *ip_in)
664 struct xfs_imap imap;
665 struct xfs_mount *mp = sc->mp;
666 struct xfs_inode *ip = NULL;
669 /* We want to scan the inode we already had opened. */
670 if (sc->sm->sm_ino == 0 || sc->sm->sm_ino == ip_in->i_ino) {
675 /* Look up the inode, see if the generation number matches. */
676 if (xfs_internal_inum(mp, sc->sm->sm_ino))
678 error = xfs_iget(mp, NULL, sc->sm->sm_ino,
679 XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE, 0, &ip);
682 /* Inode doesn't exist, just bail out. */
685 /* Got an inode, continue. */
689 * -EINVAL with IGET_UNTRUSTED could mean one of several
690 * things: userspace gave us an inode number that doesn't
691 * correspond to fs space, or doesn't have an inobt entry;
692 * or it could simply mean that the inode buffer failed the
695 * Try just the inode mapping lookup -- if it succeeds, then
696 * the inode buffer verifier failed and something needs fixing.
697 * Otherwise, we really couldn't find it so tell userspace
698 * that it no longer exists.
700 error = xfs_imap(sc->mp, sc->tp, sc->sm->sm_ino, &imap,
701 XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE);
704 error = -EFSCORRUPTED;
707 trace_xchk_op_error(sc,
708 XFS_INO_TO_AGNO(mp, sc->sm->sm_ino),
709 XFS_INO_TO_AGBNO(mp, sc->sm->sm_ino),
710 error, __return_address);
713 if (VFS_I(ip)->i_generation != sc->sm->sm_gen) {
722 /* Set us up to scrub a file's contents. */
724 xchk_setup_inode_contents(
725 struct xfs_scrub *sc,
726 struct xfs_inode *ip,
727 unsigned int resblks)
731 error = xchk_get_inode(sc, ip);
735 /* Got the inode, lock it and we're ready to go. */
736 sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
737 xfs_ilock(sc->ip, sc->ilock_flags);
738 error = xchk_trans_alloc(sc, resblks);
741 sc->ilock_flags |= XFS_ILOCK_EXCL;
742 xfs_ilock(sc->ip, XFS_ILOCK_EXCL);
745 /* scrub teardown will unlock and release the inode for us */
750 * Predicate that decides if we need to evaluate the cross-reference check.
751 * If there was an error accessing the cross-reference btree, just delete
752 * the cursor and skip the check.
755 xchk_should_check_xref(
756 struct xfs_scrub *sc,
758 struct xfs_btree_cur **curpp)
760 /* No point in xref if we already know we're corrupt. */
761 if (xchk_skip_xref(sc->sm))
768 /* If we've already given up on xref, just bail out. */
772 /* xref error, delete cursor and bail out. */
773 xfs_btree_del_cursor(*curpp, XFS_BTREE_ERROR);
777 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XFAIL;
778 trace_xchk_xref_error(sc, *error, __return_address);
781 * Errors encountered during cross-referencing with another
782 * data structure should not cause this scrubber to abort.
788 /* Run the structure verifiers on in-memory buffers to detect bad memory. */
791 struct xfs_scrub *sc,
796 if (bp->b_ops == NULL) {
797 xchk_block_set_corrupt(sc, bp);
800 if (bp->b_ops->verify_struct == NULL) {
801 xchk_set_incomplete(sc);
804 fa = bp->b_ops->verify_struct(bp);
807 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
808 trace_xchk_block_error(sc, bp->b_bn, fa);
812 * Scrub the attr/data forks of a metadata inode. The metadata inode must be
813 * pointed to by sc->ip and the ILOCK must be held.
816 xchk_metadata_inode_forks(
817 struct xfs_scrub *sc)
823 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
826 /* Metadata inodes don't live on the rt device. */
827 if (sc->ip->i_d.di_flags & XFS_DIFLAG_REALTIME) {
828 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
832 /* They should never participate in reflink. */
833 if (xfs_is_reflink_inode(sc->ip)) {
834 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
838 /* They also should never have extended attributes. */
839 if (xfs_inode_hasattr(sc->ip)) {
840 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
844 /* Invoke the data fork scrubber. */
845 smtype = sc->sm->sm_type;
846 sc->sm->sm_type = XFS_SCRUB_TYPE_BMBTD;
847 error = xchk_bmap_data(sc);
848 sc->sm->sm_type = smtype;
849 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
852 /* Look for incorrect shared blocks. */
853 if (xfs_sb_version_hasreflink(&sc->mp->m_sb)) {
854 error = xfs_reflink_inode_has_shared_extents(sc->tp, sc->ip,
856 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0,
860 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
867 * Try to lock an inode in violation of the usual locking order rules. For
868 * example, trying to get the IOLOCK while in transaction context, or just
869 * plain breaking AG-order or inode-order inode locking rules. Either way,
870 * the only way to avoid an ABBA deadlock is to use trylock and back off if
875 struct xfs_inode *ip,
880 for (i = 0; i < 20; i++) {
881 if (xfs_ilock_nowait(ip, lock_mode))