2 * Copyright (C) 2017 Oracle. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "xfs_shared.h"
23 #include "xfs_format.h"
24 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_defer.h"
27 #include "xfs_btree.h"
29 #include "xfs_log_format.h"
30 #include "xfs_trans.h"
32 #include "xfs_inode.h"
33 #include "xfs_icache.h"
34 #include "xfs_itable.h"
35 #include "xfs_alloc.h"
36 #include "xfs_alloc_btree.h"
38 #include "xfs_bmap_btree.h"
39 #include "xfs_ialloc.h"
40 #include "xfs_ialloc_btree.h"
41 #include "xfs_refcount.h"
42 #include "xfs_refcount_btree.h"
44 #include "xfs_rmap_btree.h"
46 #include "xfs_trans_priv.h"
48 #include "xfs_reflink.h"
49 #include "scrub/xfs_scrub.h"
50 #include "scrub/scrub.h"
51 #include "scrub/common.h"
52 #include "scrub/trace.h"
53 #include "scrub/btree.h"
54 #include "scrub/repair.h"
56 /* Common code for the metadata scrubbers. */
59 * Handling operational errors.
61 * The *_process_error() family of functions are used to process error return
62 * codes from functions called as part of a scrub operation.
64 * If there's no error, we return true to tell the caller that it's ok
65 * to move on to the next check in its list.
67 * For non-verifier errors (e.g. ENOMEM) we return false to tell the
68 * caller that something bad happened, and we preserve *error so that
69 * the caller can return the *error up the stack to userspace.
71 * Verifier errors (EFSBADCRC/EFSCORRUPTED) are recorded by setting
72 * OFLAG_CORRUPT in sm_flags and the *error is cleared. In other words,
73 * we track verifier errors (and failed scrub checks) via OFLAG_CORRUPT,
74 * not via return codes. We return false to tell the caller that
75 * something bad happened. Since the error has been cleared, the caller
76 * will (presumably) return that zero and scrubbing will move on to
79 * ftrace can be used to record the precise metadata location and the
80 * approximate code location of the failed operation.
83 /* Check for operational errors. */
85 __xfs_scrub_process_error(
86 struct xfs_scrub_context *sc,
97 /* Used to restart an op with deadlock avoidance. */
98 trace_xfs_scrub_deadlock_retry(sc->ip, sc->sm, *error);
102 /* Note the badness but don't abort. */
103 sc->sm->sm_flags |= errflag;
107 trace_xfs_scrub_op_error(sc, agno, bno, *error,
115 xfs_scrub_process_error(
116 struct xfs_scrub_context *sc,
121 return __xfs_scrub_process_error(sc, agno, bno, error,
122 XFS_SCRUB_OFLAG_CORRUPT, __return_address);
126 xfs_scrub_xref_process_error(
127 struct xfs_scrub_context *sc,
132 return __xfs_scrub_process_error(sc, agno, bno, error,
133 XFS_SCRUB_OFLAG_XFAIL, __return_address);
136 /* Check for operational errors for a file offset. */
138 __xfs_scrub_fblock_process_error(
139 struct xfs_scrub_context *sc,
141 xfs_fileoff_t offset,
150 /* Used to restart an op with deadlock avoidance. */
151 trace_xfs_scrub_deadlock_retry(sc->ip, sc->sm, *error);
155 /* Note the badness but don't abort. */
156 sc->sm->sm_flags |= errflag;
160 trace_xfs_scrub_file_op_error(sc, whichfork, offset, *error,
168 xfs_scrub_fblock_process_error(
169 struct xfs_scrub_context *sc,
171 xfs_fileoff_t offset,
174 return __xfs_scrub_fblock_process_error(sc, whichfork, offset, error,
175 XFS_SCRUB_OFLAG_CORRUPT, __return_address);
179 xfs_scrub_fblock_xref_process_error(
180 struct xfs_scrub_context *sc,
182 xfs_fileoff_t offset,
185 return __xfs_scrub_fblock_process_error(sc, whichfork, offset, error,
186 XFS_SCRUB_OFLAG_XFAIL, __return_address);
190 * Handling scrub corruption/optimization/warning checks.
192 * The *_set_{corrupt,preen,warning}() family of functions are used to
193 * record the presence of metadata that is incorrect (corrupt), could be
194 * optimized somehow (preen), or should be flagged for administrative
195 * review but is not incorrect (warn).
197 * ftrace can be used to record the precise metadata location and
198 * approximate code location of the failed check.
201 /* Record a block which could be optimized. */
203 xfs_scrub_block_set_preen(
204 struct xfs_scrub_context *sc,
207 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
208 trace_xfs_scrub_block_preen(sc, bp->b_bn, __return_address);
212 * Record an inode which could be optimized. The trace data will
213 * include the block given by bp if bp is given; otherwise it will use
214 * the block location of the inode record itself.
217 xfs_scrub_ino_set_preen(
218 struct xfs_scrub_context *sc,
221 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
222 trace_xfs_scrub_ino_preen(sc, ino, __return_address);
225 /* Record a corrupt block. */
227 xfs_scrub_block_set_corrupt(
228 struct xfs_scrub_context *sc,
231 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
232 trace_xfs_scrub_block_error(sc, bp->b_bn, __return_address);
235 /* Record a corruption while cross-referencing. */
237 xfs_scrub_block_xref_set_corrupt(
238 struct xfs_scrub_context *sc,
241 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
242 trace_xfs_scrub_block_error(sc, bp->b_bn, __return_address);
246 * Record a corrupt inode. The trace data will include the block given
247 * by bp if bp is given; otherwise it will use the block location of the
248 * inode record itself.
251 xfs_scrub_ino_set_corrupt(
252 struct xfs_scrub_context *sc,
255 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
256 trace_xfs_scrub_ino_error(sc, ino, __return_address);
259 /* Record a corruption while cross-referencing with an inode. */
261 xfs_scrub_ino_xref_set_corrupt(
262 struct xfs_scrub_context *sc,
265 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
266 trace_xfs_scrub_ino_error(sc, ino, __return_address);
269 /* Record corruption in a block indexed by a file fork. */
271 xfs_scrub_fblock_set_corrupt(
272 struct xfs_scrub_context *sc,
274 xfs_fileoff_t offset)
276 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
277 trace_xfs_scrub_fblock_error(sc, whichfork, offset, __return_address);
280 /* Record a corruption while cross-referencing a fork block. */
282 xfs_scrub_fblock_xref_set_corrupt(
283 struct xfs_scrub_context *sc,
285 xfs_fileoff_t offset)
287 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT;
288 trace_xfs_scrub_fblock_error(sc, whichfork, offset, __return_address);
292 * Warn about inodes that need administrative review but is not
296 xfs_scrub_ino_set_warning(
297 struct xfs_scrub_context *sc,
300 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
301 trace_xfs_scrub_ino_warning(sc, ino, __return_address);
304 /* Warn about a block indexed by a file fork that needs review. */
306 xfs_scrub_fblock_set_warning(
307 struct xfs_scrub_context *sc,
309 xfs_fileoff_t offset)
311 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
312 trace_xfs_scrub_fblock_warning(sc, whichfork, offset, __return_address);
315 /* Signal an incomplete scrub. */
317 xfs_scrub_set_incomplete(
318 struct xfs_scrub_context *sc)
320 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_INCOMPLETE;
321 trace_xfs_scrub_incomplete(sc, __return_address);
325 * rmap scrubbing -- compute the number of blocks with a given owner,
326 * at least according to the reverse mapping data.
329 struct xfs_scrub_rmap_ownedby_info {
330 struct xfs_owner_info *oinfo;
331 xfs_filblks_t *blocks;
335 xfs_scrub_count_rmap_ownedby_irec(
336 struct xfs_btree_cur *cur,
337 struct xfs_rmap_irec *rec,
340 struct xfs_scrub_rmap_ownedby_info *sroi = priv;
344 irec_attr = rec->rm_flags & XFS_RMAP_ATTR_FORK;
345 oinfo_attr = sroi->oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK;
347 if (rec->rm_owner != sroi->oinfo->oi_owner)
350 if (XFS_RMAP_NON_INODE_OWNER(rec->rm_owner) || irec_attr == oinfo_attr)
351 (*sroi->blocks) += rec->rm_blockcount;
357 * Calculate the number of blocks the rmap thinks are owned by something.
358 * The caller should pass us an rmapbt cursor.
361 xfs_scrub_count_rmap_ownedby_ag(
362 struct xfs_scrub_context *sc,
363 struct xfs_btree_cur *cur,
364 struct xfs_owner_info *oinfo,
365 xfs_filblks_t *blocks)
367 struct xfs_scrub_rmap_ownedby_info sroi;
371 sroi.blocks = blocks;
373 return xfs_rmap_query_all(cur, xfs_scrub_count_rmap_ownedby_irec,
380 * These helpers facilitate locking an allocation group's header
381 * buffers, setting up cursors for all btrees that are present, and
382 * cleaning everything up once we're through.
385 /* Decide if we want to return an AG header read failure. */
387 want_ag_read_header_failure(
388 struct xfs_scrub_context *sc,
391 /* Return all AG header read failures when scanning btrees. */
392 if (sc->sm->sm_type != XFS_SCRUB_TYPE_AGF &&
393 sc->sm->sm_type != XFS_SCRUB_TYPE_AGFL &&
394 sc->sm->sm_type != XFS_SCRUB_TYPE_AGI)
397 * If we're scanning a given type of AG header, we only want to
398 * see read failures from that specific header. We'd like the
399 * other headers to cross-check them, but this isn't required.
401 if (sc->sm->sm_type == type)
407 * Grab all the headers for an AG.
409 * The headers should be released by xfs_scrub_ag_free, but as a fail
410 * safe we attach all the buffers we grab to the scrub transaction so
411 * they'll all be freed when we cancel it.
414 xfs_scrub_ag_read_headers(
415 struct xfs_scrub_context *sc,
417 struct xfs_buf **agi,
418 struct xfs_buf **agf,
419 struct xfs_buf **agfl)
421 struct xfs_mount *mp = sc->mp;
424 error = xfs_ialloc_read_agi(mp, sc->tp, agno, agi);
425 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGI))
428 error = xfs_alloc_read_agf(mp, sc->tp, agno, 0, agf);
429 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGF))
432 error = xfs_alloc_read_agfl(mp, sc->tp, agno, agfl);
433 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGFL))
440 /* Release all the AG btree cursors. */
442 xfs_scrub_ag_btcur_free(
443 struct xfs_scrub_ag *sa)
446 xfs_btree_del_cursor(sa->refc_cur, XFS_BTREE_ERROR);
448 xfs_btree_del_cursor(sa->rmap_cur, XFS_BTREE_ERROR);
450 xfs_btree_del_cursor(sa->fino_cur, XFS_BTREE_ERROR);
452 xfs_btree_del_cursor(sa->ino_cur, XFS_BTREE_ERROR);
454 xfs_btree_del_cursor(sa->cnt_cur, XFS_BTREE_ERROR);
456 xfs_btree_del_cursor(sa->bno_cur, XFS_BTREE_ERROR);
466 /* Initialize all the btree cursors for an AG. */
468 xfs_scrub_ag_btcur_init(
469 struct xfs_scrub_context *sc,
470 struct xfs_scrub_ag *sa)
472 struct xfs_mount *mp = sc->mp;
473 xfs_agnumber_t agno = sa->agno;
476 /* Set up a bnobt cursor for cross-referencing. */
477 sa->bno_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
478 agno, XFS_BTNUM_BNO);
482 /* Set up a cntbt cursor for cross-referencing. */
483 sa->cnt_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
484 agno, XFS_BTNUM_CNT);
489 /* Set up a inobt cursor for cross-referencing. */
491 sa->ino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp,
492 agno, XFS_BTNUM_INO);
497 /* Set up a finobt cursor for cross-referencing. */
498 if (sa->agi_bp && xfs_sb_version_hasfinobt(&mp->m_sb)) {
499 sa->fino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp,
500 agno, XFS_BTNUM_FINO);
505 /* Set up a rmapbt cursor for cross-referencing. */
506 if (sa->agf_bp && xfs_sb_version_hasrmapbt(&mp->m_sb)) {
507 sa->rmap_cur = xfs_rmapbt_init_cursor(mp, sc->tp, sa->agf_bp,
513 /* Set up a refcountbt cursor for cross-referencing. */
514 if (sa->agf_bp && xfs_sb_version_hasreflink(&mp->m_sb)) {
515 sa->refc_cur = xfs_refcountbt_init_cursor(mp, sc->tp,
516 sa->agf_bp, agno, NULL);
526 /* Release the AG header context and btree cursors. */
529 struct xfs_scrub_context *sc,
530 struct xfs_scrub_ag *sa)
532 xfs_scrub_ag_btcur_free(sa);
534 xfs_trans_brelse(sc->tp, sa->agfl_bp);
538 xfs_trans_brelse(sc->tp, sa->agf_bp);
542 xfs_trans_brelse(sc->tp, sa->agi_bp);
546 xfs_perag_put(sa->pag);
549 sa->agno = NULLAGNUMBER;
553 * For scrub, grab the AGI and the AGF headers, in that order. Locking
554 * order requires us to get the AGI before the AGF. We use the
555 * transaction to avoid deadlocking on crosslinked metadata buffers;
556 * either the caller passes one in (bmap scrub) or we have to create a
557 * transaction ourselves.
561 struct xfs_scrub_context *sc,
563 struct xfs_scrub_ag *sa)
568 error = xfs_scrub_ag_read_headers(sc, agno, &sa->agi_bp,
569 &sa->agf_bp, &sa->agfl_bp);
573 return xfs_scrub_ag_btcur_init(sc, sa);
577 * Grab the per-ag structure if we haven't already gotten it. Teardown of the
578 * xfs_scrub_ag will release it for us.
582 struct xfs_mount *mp,
583 struct xfs_scrub_ag *sa)
586 sa->pag = xfs_perag_get(mp, sa->agno);
589 /* Per-scrubber setup functions */
592 * Grab an empty transaction so that we can re-grab locked buffers if
593 * one of our btrees turns out to be cyclic.
595 * If we're going to repair something, we need to ask for the largest possible
596 * log reservation so that we can handle the worst case scenario for metadata
597 * updates while rebuilding a metadata item. We also need to reserve as many
598 * blocks in the head transaction as we think we're going to need to rebuild
599 * the metadata object.
602 xfs_scrub_trans_alloc(
603 struct xfs_scrub_context *sc,
606 if (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)
607 return xfs_trans_alloc(sc->mp, &M_RES(sc->mp)->tr_itruncate,
608 resblks, 0, 0, &sc->tp);
610 return xfs_trans_alloc_empty(sc->mp, &sc->tp);
613 /* Set us up with a transaction and an empty context. */
616 struct xfs_scrub_context *sc,
617 struct xfs_inode *ip)
621 resblks = xfs_repair_calc_ag_resblks(sc);
622 return xfs_scrub_trans_alloc(sc, resblks);
625 /* Set us up with AG headers and btree cursors. */
627 xfs_scrub_setup_ag_btree(
628 struct xfs_scrub_context *sc,
629 struct xfs_inode *ip,
632 struct xfs_mount *mp = sc->mp;
636 * If the caller asks us to checkpont the log, do so. This
637 * expensive operation should be performed infrequently and only
638 * as a last resort. Any caller that sets force_log should
639 * document why they need to do so.
642 error = xfs_scrub_checkpoint_log(mp);
647 error = xfs_scrub_setup_fs(sc, ip);
651 return xfs_scrub_ag_init(sc, sc->sm->sm_agno, &sc->sa);
654 /* Push everything out of the log onto disk. */
656 xfs_scrub_checkpoint_log(
657 struct xfs_mount *mp)
661 error = xfs_log_force(mp, XFS_LOG_SYNC);
664 xfs_ail_push_all_sync(mp->m_ail);
669 * Given an inode and the scrub control structure, grab either the
670 * inode referenced in the control structure or the inode passed in.
671 * The inode is not locked.
675 struct xfs_scrub_context *sc,
676 struct xfs_inode *ip_in)
678 struct xfs_imap imap;
679 struct xfs_mount *mp = sc->mp;
680 struct xfs_inode *ip = NULL;
683 /* We want to scan the inode we already had opened. */
684 if (sc->sm->sm_ino == 0 || sc->sm->sm_ino == ip_in->i_ino) {
689 /* Look up the inode, see if the generation number matches. */
690 if (xfs_internal_inum(mp, sc->sm->sm_ino))
692 error = xfs_iget(mp, NULL, sc->sm->sm_ino,
693 XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE, 0, &ip);
696 /* Inode doesn't exist, just bail out. */
699 /* Got an inode, continue. */
703 * -EINVAL with IGET_UNTRUSTED could mean one of several
704 * things: userspace gave us an inode number that doesn't
705 * correspond to fs space, or doesn't have an inobt entry;
706 * or it could simply mean that the inode buffer failed the
709 * Try just the inode mapping lookup -- if it succeeds, then
710 * the inode buffer verifier failed and something needs fixing.
711 * Otherwise, we really couldn't find it so tell userspace
712 * that it no longer exists.
714 error = xfs_imap(sc->mp, sc->tp, sc->sm->sm_ino, &imap,
715 XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE);
718 error = -EFSCORRUPTED;
721 trace_xfs_scrub_op_error(sc,
722 XFS_INO_TO_AGNO(mp, sc->sm->sm_ino),
723 XFS_INO_TO_AGBNO(mp, sc->sm->sm_ino),
724 error, __return_address);
727 if (VFS_I(ip)->i_generation != sc->sm->sm_gen) {
736 /* Set us up to scrub a file's contents. */
738 xfs_scrub_setup_inode_contents(
739 struct xfs_scrub_context *sc,
740 struct xfs_inode *ip,
741 unsigned int resblks)
745 error = xfs_scrub_get_inode(sc, ip);
749 /* Got the inode, lock it and we're ready to go. */
750 sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
751 xfs_ilock(sc->ip, sc->ilock_flags);
752 error = xfs_scrub_trans_alloc(sc, resblks);
755 sc->ilock_flags |= XFS_ILOCK_EXCL;
756 xfs_ilock(sc->ip, XFS_ILOCK_EXCL);
759 /* scrub teardown will unlock and release the inode for us */
764 * Predicate that decides if we need to evaluate the cross-reference check.
765 * If there was an error accessing the cross-reference btree, just delete
766 * the cursor and skip the check.
769 xfs_scrub_should_check_xref(
770 struct xfs_scrub_context *sc,
772 struct xfs_btree_cur **curpp)
774 /* No point in xref if we already know we're corrupt. */
775 if (xfs_scrub_skip_xref(sc->sm))
782 /* If we've already given up on xref, just bail out. */
786 /* xref error, delete cursor and bail out. */
787 xfs_btree_del_cursor(*curpp, XFS_BTREE_ERROR);
791 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XFAIL;
792 trace_xfs_scrub_xref_error(sc, *error, __return_address);
795 * Errors encountered during cross-referencing with another
796 * data structure should not cause this scrubber to abort.
802 /* Run the structure verifiers on in-memory buffers to detect bad memory. */
804 xfs_scrub_buffer_recheck(
805 struct xfs_scrub_context *sc,
810 if (bp->b_ops == NULL) {
811 xfs_scrub_block_set_corrupt(sc, bp);
814 if (bp->b_ops->verify_struct == NULL) {
815 xfs_scrub_set_incomplete(sc);
818 fa = bp->b_ops->verify_struct(bp);
821 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
822 trace_xfs_scrub_block_error(sc, bp->b_bn, fa);
826 * Scrub the attr/data forks of a metadata inode. The metadata inode must be
827 * pointed to by sc->ip and the ILOCK must be held.
830 xfs_scrub_metadata_inode_forks(
831 struct xfs_scrub_context *sc)
837 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
840 /* Metadata inodes don't live on the rt device. */
841 if (sc->ip->i_d.di_flags & XFS_DIFLAG_REALTIME) {
842 xfs_scrub_ino_set_corrupt(sc, sc->ip->i_ino);
846 /* They should never participate in reflink. */
847 if (xfs_is_reflink_inode(sc->ip)) {
848 xfs_scrub_ino_set_corrupt(sc, sc->ip->i_ino);
852 /* They also should never have extended attributes. */
853 if (xfs_inode_hasattr(sc->ip)) {
854 xfs_scrub_ino_set_corrupt(sc, sc->ip->i_ino);
858 /* Invoke the data fork scrubber. */
859 smtype = sc->sm->sm_type;
860 sc->sm->sm_type = XFS_SCRUB_TYPE_BMBTD;
861 error = xfs_scrub_bmap_data(sc);
862 sc->sm->sm_type = smtype;
863 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
866 /* Look for incorrect shared blocks. */
867 if (xfs_sb_version_hasreflink(&sc->mp->m_sb)) {
868 error = xfs_reflink_inode_has_shared_extents(sc->tp, sc->ip,
870 if (!xfs_scrub_fblock_process_error(sc, XFS_DATA_FORK, 0,
874 xfs_scrub_ino_set_corrupt(sc, sc->ip->i_ino);
881 * Try to lock an inode in violation of the usual locking order rules. For
882 * example, trying to get the IOLOCK while in transaction context, or just
883 * plain breaking AG-order or inode-order inode locking rules. Either way,
884 * the only way to avoid an ABBA deadlock is to use trylock and back off if
888 xfs_scrub_ilock_inverted(
889 struct xfs_inode *ip,
894 for (i = 0; i < 20; i++) {
895 if (xfs_ilock_nowait(ip, lock_mode))