| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- /*
- * Copyright (C) 2017 Oracle. All Rights Reserved.
- *
- * Author: Darrick J. Wong <darrick.wong@oracle.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it would be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- #include "xfs.h"
- #include "xfs_fs.h"
- #include "xfs_shared.h"
- #include "xfs_format.h"
- #include "xfs_trans_resv.h"
- #include "xfs_mount.h"
- #include "xfs_defer.h"
- #include "xfs_btree.h"
- #include "xfs_bit.h"
- #include "xfs_log_format.h"
- #include "xfs_trans.h"
- #include "xfs_sb.h"
- #include "xfs_inode.h"
- #include "xfs_icache.h"
- #include "xfs_itable.h"
- #include "xfs_alloc.h"
- #include "xfs_alloc_btree.h"
- #include "xfs_bmap.h"
- #include "xfs_bmap_btree.h"
- #include "xfs_ialloc.h"
- #include "xfs_ialloc_btree.h"
- #include "xfs_refcount.h"
- #include "xfs_refcount_btree.h"
- #include "xfs_rmap.h"
- #include "xfs_rmap_btree.h"
- #include "xfs_log.h"
- #include "xfs_trans_priv.h"
- #include "scrub/xfs_scrub.h"
- #include "scrub/scrub.h"
- #include "scrub/common.h"
- #include "scrub/trace.h"
- #include "scrub/btree.h"
- /* Common code for the metadata scrubbers. */
- /*
- * Handling operational errors.
- *
- * The *_process_error() family of functions are used to process error return
- * codes from functions called as part of a scrub operation.
- *
- * If there's no error, we return true to tell the caller that it's ok
- * to move on to the next check in its list.
- *
- * For non-verifier errors (e.g. ENOMEM) we return false to tell the
- * caller that something bad happened, and we preserve *error so that
- * the caller can return the *error up the stack to userspace.
- *
- * Verifier errors (EFSBADCRC/EFSCORRUPTED) are recorded by setting
- * OFLAG_CORRUPT in sm_flags and the *error is cleared. In other words,
- * we track verifier errors (and failed scrub checks) via OFLAG_CORRUPT,
- * not via return codes. We return false to tell the caller that
- * something bad happened. Since the error has been cleared, the caller
- * will (presumably) return that zero and scrubbing will move on to
- * whatever's next.
- *
- * ftrace can be used to record the precise metadata location and the
- * approximate code location of the failed operation.
- */
- /* Check for operational errors. */
- bool
- xfs_scrub_process_error(
- struct xfs_scrub_context *sc,
- xfs_agnumber_t agno,
- xfs_agblock_t bno,
- int *error)
- {
- switch (*error) {
- case 0:
- return true;
- case -EDEADLOCK:
- /* Used to restart an op with deadlock avoidance. */
- trace_xfs_scrub_deadlock_retry(sc->ip, sc->sm, *error);
- break;
- case -EFSBADCRC:
- case -EFSCORRUPTED:
- /* Note the badness but don't abort. */
- sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
- *error = 0;
- /* fall through */
- default:
- trace_xfs_scrub_op_error(sc, agno, bno, *error,
- __return_address);
- break;
- }
- return false;
- }
- /* Check for operational errors for a file offset. */
- bool
- xfs_scrub_fblock_process_error(
- struct xfs_scrub_context *sc,
- int whichfork,
- xfs_fileoff_t offset,
- int *error)
- {
- switch (*error) {
- case 0:
- return true;
- case -EDEADLOCK:
- /* Used to restart an op with deadlock avoidance. */
- trace_xfs_scrub_deadlock_retry(sc->ip, sc->sm, *error);
- break;
- case -EFSBADCRC:
- case -EFSCORRUPTED:
- /* Note the badness but don't abort. */
- sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
- *error = 0;
- /* fall through */
- default:
- trace_xfs_scrub_file_op_error(sc, whichfork, offset, *error,
- __return_address);
- break;
- }
- return false;
- }
- /*
- * Handling scrub corruption/optimization/warning checks.
- *
- * The *_set_{corrupt,preen,warning}() family of functions are used to
- * record the presence of metadata that is incorrect (corrupt), could be
- * optimized somehow (preen), or should be flagged for administrative
- * review but is not incorrect (warn).
- *
- * ftrace can be used to record the precise metadata location and
- * approximate code location of the failed check.
- */
- /* Record a block which could be optimized. */
- void
- xfs_scrub_block_set_preen(
- struct xfs_scrub_context *sc,
- struct xfs_buf *bp)
- {
- sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
- trace_xfs_scrub_block_preen(sc, bp->b_bn, __return_address);
- }
- /*
- * Record an inode which could be optimized. The trace data will
- * include the block given by bp if bp is given; otherwise it will use
- * the block location of the inode record itself.
- */
- void
- xfs_scrub_ino_set_preen(
- struct xfs_scrub_context *sc,
- struct xfs_buf *bp)
- {
- sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
- trace_xfs_scrub_ino_preen(sc, sc->ip->i_ino, bp ? bp->b_bn : 0,
- __return_address);
- }
- /* Record a corrupt block. */
- void
- xfs_scrub_block_set_corrupt(
- struct xfs_scrub_context *sc,
- struct xfs_buf *bp)
- {
- sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
- trace_xfs_scrub_block_error(sc, bp->b_bn, __return_address);
- }
- /*
- * Record a corrupt inode. The trace data will include the block given
- * by bp if bp is given; otherwise it will use the block location of the
- * inode record itself.
- */
- void
- xfs_scrub_ino_set_corrupt(
- struct xfs_scrub_context *sc,
- xfs_ino_t ino,
- struct xfs_buf *bp)
- {
- sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
- trace_xfs_scrub_ino_error(sc, ino, bp ? bp->b_bn : 0, __return_address);
- }
- /* Record corruption in a block indexed by a file fork. */
- void
- xfs_scrub_fblock_set_corrupt(
- struct xfs_scrub_context *sc,
- int whichfork,
- xfs_fileoff_t offset)
- {
- sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
- trace_xfs_scrub_fblock_error(sc, whichfork, offset, __return_address);
- }
- /*
- * Warn about inodes that need administrative review but is not
- * incorrect.
- */
- void
- xfs_scrub_ino_set_warning(
- struct xfs_scrub_context *sc,
- struct xfs_buf *bp)
- {
- sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
- trace_xfs_scrub_ino_warning(sc, sc->ip->i_ino, bp ? bp->b_bn : 0,
- __return_address);
- }
- /* Warn about a block indexed by a file fork that needs review. */
- void
- xfs_scrub_fblock_set_warning(
- struct xfs_scrub_context *sc,
- int whichfork,
- xfs_fileoff_t offset)
- {
- sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
- trace_xfs_scrub_fblock_warning(sc, whichfork, offset, __return_address);
- }
- /* Signal an incomplete scrub. */
- void
- xfs_scrub_set_incomplete(
- struct xfs_scrub_context *sc)
- {
- sc->sm->sm_flags |= XFS_SCRUB_OFLAG_INCOMPLETE;
- trace_xfs_scrub_incomplete(sc, __return_address);
- }
- /*
- * AG scrubbing
- *
- * These helpers facilitate locking an allocation group's header
- * buffers, setting up cursors for all btrees that are present, and
- * cleaning everything up once we're through.
- */
- /* Decide if we want to return an AG header read failure. */
- static inline bool
- want_ag_read_header_failure(
- struct xfs_scrub_context *sc,
- unsigned int type)
- {
- /* Return all AG header read failures when scanning btrees. */
- if (sc->sm->sm_type != XFS_SCRUB_TYPE_AGF &&
- sc->sm->sm_type != XFS_SCRUB_TYPE_AGFL &&
- sc->sm->sm_type != XFS_SCRUB_TYPE_AGI)
- return true;
- /*
- * If we're scanning a given type of AG header, we only want to
- * see read failures from that specific header. We'd like the
- * other headers to cross-check them, but this isn't required.
- */
- if (sc->sm->sm_type == type)
- return true;
- return false;
- }
- /*
- * Grab all the headers for an AG.
- *
- * The headers should be released by xfs_scrub_ag_free, but as a fail
- * safe we attach all the buffers we grab to the scrub transaction so
- * they'll all be freed when we cancel it.
- */
- int
- xfs_scrub_ag_read_headers(
- struct xfs_scrub_context *sc,
- xfs_agnumber_t agno,
- struct xfs_buf **agi,
- struct xfs_buf **agf,
- struct xfs_buf **agfl)
- {
- struct xfs_mount *mp = sc->mp;
- int error;
- error = xfs_ialloc_read_agi(mp, sc->tp, agno, agi);
- if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGI))
- goto out;
- error = xfs_alloc_read_agf(mp, sc->tp, agno, 0, agf);
- if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGF))
- goto out;
- error = xfs_alloc_read_agfl(mp, sc->tp, agno, agfl);
- if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGFL))
- goto out;
- out:
- return error;
- }
- /* Release all the AG btree cursors. */
- void
- xfs_scrub_ag_btcur_free(
- struct xfs_scrub_ag *sa)
- {
- if (sa->refc_cur)
- xfs_btree_del_cursor(sa->refc_cur, XFS_BTREE_ERROR);
- if (sa->rmap_cur)
- xfs_btree_del_cursor(sa->rmap_cur, XFS_BTREE_ERROR);
- if (sa->fino_cur)
- xfs_btree_del_cursor(sa->fino_cur, XFS_BTREE_ERROR);
- if (sa->ino_cur)
- xfs_btree_del_cursor(sa->ino_cur, XFS_BTREE_ERROR);
- if (sa->cnt_cur)
- xfs_btree_del_cursor(sa->cnt_cur, XFS_BTREE_ERROR);
- if (sa->bno_cur)
- xfs_btree_del_cursor(sa->bno_cur, XFS_BTREE_ERROR);
- sa->refc_cur = NULL;
- sa->rmap_cur = NULL;
- sa->fino_cur = NULL;
- sa->ino_cur = NULL;
- sa->bno_cur = NULL;
- sa->cnt_cur = NULL;
- }
- /* Initialize all the btree cursors for an AG. */
- int
- xfs_scrub_ag_btcur_init(
- struct xfs_scrub_context *sc,
- struct xfs_scrub_ag *sa)
- {
- struct xfs_mount *mp = sc->mp;
- xfs_agnumber_t agno = sa->agno;
- if (sa->agf_bp) {
- /* Set up a bnobt cursor for cross-referencing. */
- sa->bno_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
- agno, XFS_BTNUM_BNO);
- if (!sa->bno_cur)
- goto err;
- /* Set up a cntbt cursor for cross-referencing. */
- sa->cnt_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
- agno, XFS_BTNUM_CNT);
- if (!sa->cnt_cur)
- goto err;
- }
- /* Set up a inobt cursor for cross-referencing. */
- if (sa->agi_bp) {
- sa->ino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp,
- agno, XFS_BTNUM_INO);
- if (!sa->ino_cur)
- goto err;
- }
- /* Set up a finobt cursor for cross-referencing. */
- if (sa->agi_bp && xfs_sb_version_hasfinobt(&mp->m_sb)) {
- sa->fino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp,
- agno, XFS_BTNUM_FINO);
- if (!sa->fino_cur)
- goto err;
- }
- /* Set up a rmapbt cursor for cross-referencing. */
- if (sa->agf_bp && xfs_sb_version_hasrmapbt(&mp->m_sb)) {
- sa->rmap_cur = xfs_rmapbt_init_cursor(mp, sc->tp, sa->agf_bp,
- agno);
- if (!sa->rmap_cur)
- goto err;
- }
- /* Set up a refcountbt cursor for cross-referencing. */
- if (sa->agf_bp && xfs_sb_version_hasreflink(&mp->m_sb)) {
- sa->refc_cur = xfs_refcountbt_init_cursor(mp, sc->tp,
- sa->agf_bp, agno, NULL);
- if (!sa->refc_cur)
- goto err;
- }
- return 0;
- err:
- return -ENOMEM;
- }
- /* Release the AG header context and btree cursors. */
- void
- xfs_scrub_ag_free(
- struct xfs_scrub_context *sc,
- struct xfs_scrub_ag *sa)
- {
- xfs_scrub_ag_btcur_free(sa);
- if (sa->agfl_bp) {
- xfs_trans_brelse(sc->tp, sa->agfl_bp);
- sa->agfl_bp = NULL;
- }
- if (sa->agf_bp) {
- xfs_trans_brelse(sc->tp, sa->agf_bp);
- sa->agf_bp = NULL;
- }
- if (sa->agi_bp) {
- xfs_trans_brelse(sc->tp, sa->agi_bp);
- sa->agi_bp = NULL;
- }
- sa->agno = NULLAGNUMBER;
- }
- /*
- * For scrub, grab the AGI and the AGF headers, in that order. Locking
- * order requires us to get the AGI before the AGF. We use the
- * transaction to avoid deadlocking on crosslinked metadata buffers;
- * either the caller passes one in (bmap scrub) or we have to create a
- * transaction ourselves.
- */
- int
- xfs_scrub_ag_init(
- struct xfs_scrub_context *sc,
- xfs_agnumber_t agno,
- struct xfs_scrub_ag *sa)
- {
- int error;
- sa->agno = agno;
- error = xfs_scrub_ag_read_headers(sc, agno, &sa->agi_bp,
- &sa->agf_bp, &sa->agfl_bp);
- if (error)
- return error;
- return xfs_scrub_ag_btcur_init(sc, sa);
- }
- /* Per-scrubber setup functions */
- /* Set us up with a transaction and an empty context. */
- int
- xfs_scrub_setup_fs(
- struct xfs_scrub_context *sc,
- struct xfs_inode *ip)
- {
- return xfs_scrub_trans_alloc(sc->sm, sc->mp, &sc->tp);
- }
- /* Set us up with AG headers and btree cursors. */
- int
- xfs_scrub_setup_ag_btree(
- struct xfs_scrub_context *sc,
- struct xfs_inode *ip,
- bool force_log)
- {
- struct xfs_mount *mp = sc->mp;
- int error;
- /*
- * If the caller asks us to checkpont the log, do so. This
- * expensive operation should be performed infrequently and only
- * as a last resort. Any caller that sets force_log should
- * document why they need to do so.
- */
- if (force_log) {
- error = xfs_scrub_checkpoint_log(mp);
- if (error)
- return error;
- }
- error = xfs_scrub_setup_ag_header(sc, ip);
- if (error)
- return error;
- return xfs_scrub_ag_init(sc, sc->sm->sm_agno, &sc->sa);
- }
- /* Push everything out of the log onto disk. */
- int
- xfs_scrub_checkpoint_log(
- struct xfs_mount *mp)
- {
- int error;
- error = _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
- if (error)
- return error;
- xfs_ail_push_all_sync(mp->m_ail);
- return 0;
- }
- /*
- * Given an inode and the scrub control structure, grab either the
- * inode referenced in the control structure or the inode passed in.
- * The inode is not locked.
- */
- int
- xfs_scrub_get_inode(
- struct xfs_scrub_context *sc,
- struct xfs_inode *ip_in)
- {
- struct xfs_mount *mp = sc->mp;
- struct xfs_inode *ip = NULL;
- int error;
- /*
- * If userspace passed us an AG number or a generation number
- * without an inode number, they haven't got a clue so bail out
- * immediately.
- */
- if (sc->sm->sm_agno || (sc->sm->sm_gen && !sc->sm->sm_ino))
- return -EINVAL;
- /* We want to scan the inode we already had opened. */
- if (sc->sm->sm_ino == 0 || sc->sm->sm_ino == ip_in->i_ino) {
- sc->ip = ip_in;
- return 0;
- }
- /* Look up the inode, see if the generation number matches. */
- if (xfs_internal_inum(mp, sc->sm->sm_ino))
- return -ENOENT;
- error = xfs_iget(mp, NULL, sc->sm->sm_ino,
- XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE, 0, &ip);
- if (error == -ENOENT || error == -EINVAL) {
- /* inode doesn't exist... */
- return -ENOENT;
- } else if (error) {
- trace_xfs_scrub_op_error(sc,
- XFS_INO_TO_AGNO(mp, sc->sm->sm_ino),
- XFS_INO_TO_AGBNO(mp, sc->sm->sm_ino),
- error, __return_address);
- return error;
- }
- if (VFS_I(ip)->i_generation != sc->sm->sm_gen) {
- iput(VFS_I(ip));
- return -ENOENT;
- }
- sc->ip = ip;
- return 0;
- }
|