From efb0f93ddd0bb3f1f6321d21c374429fedfb3508 Mon Sep 17 00:00:00 2001 From: "Darrick J. Wong" Date: Wed, 1 Sep 2021 11:25:37 -0700 Subject: xfs: teach repair to fix file nlinks Fix the nlinks now too. Signed-off-by: Darrick J. Wong --- fs/xfs/scrub/nlinks.c | 9 +- fs/xfs/scrub/nlinks.h | 6 ++ fs/xfs/scrub/nlinks_repair.c | 197 +++++++++++++++++++++++++++++++++++++++++++ fs/xfs/scrub/repair.h | 4 + fs/xfs/scrub/scrub.c | 2 +- fs/xfs/scrub/trace.h | 3 + 6 files changed, 219 insertions(+), 2 deletions(-) create mode 100644 fs/xfs/scrub/nlinks_repair.c (limited to 'fs/xfs/scrub') diff --git a/fs/xfs/scrub/nlinks.c b/fs/xfs/scrub/nlinks.c index f13bb0176f3d..c833e06c7cc0 100644 --- a/fs/xfs/scrub/nlinks.c +++ b/fs/xfs/scrub/nlinks.c @@ -53,6 +53,13 @@ int xchk_setup_nlinks( struct xfs_scrub *sc) { + int error; + + if (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) { + error = xrep_setup_nlinks(sc); + if (error) + return error; + } sc->buf = kmem_zalloc(sizeof(struct xchk_nlink_ctrs), KM_NOFS | KM_MAYFAIL); if (!sc->buf) @@ -62,7 +69,7 @@ xchk_setup_nlinks( } /* Retrieve the observed link count record for the given inode. */ -STATIC int +int xchk_nlinks_get_record( struct xchk_nlink_ctrs *xnc, xfs_ino_t ino, diff --git a/fs/xfs/scrub/nlinks.h b/fs/xfs/scrub/nlinks.h index 0ece2ab5dd38..868bdf6e4172 100644 --- a/fs/xfs/scrub/nlinks.h +++ b/fs/xfs/scrub/nlinks.h @@ -44,6 +44,9 @@ struct xchk_nlink { /* This data item was seen by the check-time compare function. */ #define XCHK_NLINK_COMPARE_SCANNED (1U << 0) +/* Item was modified by the repair function. */ +#define XREP_NLINK_DIRTY (1U << 1) + /* Compute total link count, using large enough variables to detect overflow. */ static inline uint64_t xchk_nlink_total(const struct xchk_nlink *live) @@ -53,4 +56,7 @@ xchk_nlink_total(const struct xchk_nlink *live) return ret + live->child; } +int xchk_nlinks_get_record(struct xchk_nlink_ctrs *xnc, xfs_ino_t ino, + struct xchk_nlink *nl); + #endif /* __XFS_SCRUB_NLINKS_H__ */ diff --git a/fs/xfs/scrub/nlinks_repair.c b/fs/xfs/scrub/nlinks_repair.c new file mode 100644 index 000000000000..f2b05358a593 --- /dev/null +++ b/fs/xfs/scrub/nlinks_repair.c @@ -0,0 +1,197 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2021 Oracle. All Rights Reserved. + * Author: Darrick J. Wong + */ +#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_log_format.h" +#include "xfs_trans.h" +#include "xfs_inode.h" +#include "xfs_icache.h" +#include "xfs_bmap_util.h" +#include "xfs_iwalk.h" +#include "xfs_ialloc.h" +#include "xfs_sb.h" +#include "scrub/scrub.h" +#include "scrub/common.h" +#include "scrub/repair.h" +#include "scrub/xfarray.h" +#include "scrub/iscan.h" +#include "scrub/nlinks.h" +#include "scrub/trace.h" +#include "scrub/orphanage.h" + +/* + * Live Inode Link Count Repair + * ============================ + * + * Use the live inode link count information that we collected to replace the + * nlink values of the incore inodes. A scrub->repair cycle should have left + * the live data and hooks active, so this is safe so long as we make sure the + * inode is locked. + */ + +/* Set up to repair inode link counts. */ +int +xrep_setup_nlinks( + struct xfs_scrub *sc) +{ + return xrep_orphanage_try_create(sc); +} + +/* + * Correct the link count of the given inode. Because we have to grab locks + * and resources in a certain order, it's possible that this will be a no-op. + */ +STATIC int +xrep_nlinks_repair_inode( + struct xchk_nlink_ctrs *xnc) +{ + struct xchk_nlink obs; + struct xfs_scrub *sc = xnc->sc; + struct xfs_mount *mp = sc->mp; + struct xfs_inode *ip = sc->ip; + uint64_t total_links; + int error; + + xfs_ilock(ip, XFS_IOLOCK_EXCL); + + error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, 0, 0, 0, &sc->tp); + if (error) + goto out_iolock; + + xfs_ilock(ip, XFS_ILOCK_EXCL); + xfs_trans_ijoin(sc->tp, ip, 0); + + mutex_lock(&xnc->lock); + + if (xchk_iscan_aborted(&xnc->collect_iscan)) { + error = -ECANCELED; + goto out_scanlock; + } + + error = xchk_nlinks_get_record(xnc, ip->i_ino, &obs); + if (error) + goto out_scanlock; + total_links = xchk_nlink_total(&obs); + + /* Cannot set more than the maxiumum possible link count. */ + if (total_links > U32_MAX) { + trace_xrep_nlinks_unfixable_inode(mp, ip, &obs); + goto out_scanlock; + } + + /* Non-directories cannot have directories pointing up to them. */ + if (!S_ISDIR(VFS_I(ip)->i_mode) && obs.child > 0) { + trace_xrep_nlinks_unfixable_inode(mp, ip, &obs); + goto out_scanlock; + } + + /* + * Directories should have at least one "child" (the dot entry) + * pointing up to them. + */ + if (S_ISDIR(VFS_I(ip)->i_mode) && obs.child == 0) { + trace_xrep_nlinks_unfixable_inode(mp, ip, &obs); + goto out_scanlock; + } + + /* + * We did not find any links to this inode. If the inode agrees, we + * have nothing further to do. If not, the inode has a nonzero link + * count, and we don't have an orphanage to adopt the child. Dropping + * a live inode's link count to zero is evil, so leave it alone. + */ + if (total_links == 0) { + if (VFS_I(ip)->i_nlink != 0) + trace_xrep_nlinks_unfixable_inode(mp, ip, &obs); + goto out_scanlock; + } + + /* Perfect match means we're done. */ + if (total_links == VFS_I(ip)->i_nlink) + goto out_scanlock; + mutex_unlock(&xnc->lock); + + /* Commit the new link count. */ + trace_xrep_nlinks_update_inode(mp, ip, &obs); + + set_nlink(VFS_I(ip), total_links); + xfs_trans_log_inode(sc->tp, ip, XFS_ILOG_CORE); + error = xfs_trans_commit(sc->tp); + sc->tp = NULL; + if (error) + goto out_ilock; + + xfs_iunlock(ip, XFS_ILOCK_EXCL); + xfs_iunlock(ip, XFS_IOLOCK_EXCL); + return 0; + +out_scanlock: + mutex_unlock(&xnc->lock); + xchk_trans_cancel(sc); +out_ilock: + xfs_iunlock(ip, XFS_ILOCK_EXCL); +out_iolock: + xfs_iunlock(ip, XFS_IOLOCK_EXCL); + return error; +} + +/* Commit the new inode link counters. */ +int +xrep_nlinks( + struct xfs_scrub *sc) +{ + struct xchk_nlink_ctrs *xnc = sc->buf; + int error; + + /* + * Use the inobt to walk all allocated inodes to compare and fix the + * link counts. If we can't iget the inode, we cannot repair it. + */ + xnc->compare_iscan.iget_tries = 20; + xnc->compare_iscan.iget_retry_delay = HZ / 10; + xchk_iscan_start(&xnc->compare_iscan); + while ((error = xchk_iscan_advance(sc, &xnc->compare_iscan)) == 1) { + ASSERT(sc->ip == NULL); + + error = xchk_iscan_iget(sc, &xnc->compare_iscan, &sc->ip); + if (error == -EAGAIN || error == -ECANCELED) + continue; + if (error) + break; + + /* + * Commit the scrub transaction so that we can create repair + * transactions with the correct reservations. + */ + xchk_trans_cancel(sc); + + error = xrep_nlinks_repair_inode(xnc); + xchk_iscan_mark_visited(&xnc->compare_iscan, sc->ip); + xchk_irele(sc, sc->ip); + sc->ip = NULL; + if (error) + break; + + if (xchk_should_terminate(sc, &error)) + break; + + /* + * Create a new empty transaction so that we can advance the + * iscan cursor without deadlocking if the inobt has a cycle. + * We can only push the inactivation workqueues with an empty + * transaction. + */ + error = xchk_trans_alloc_empty(sc); + if (error) + break; + } + + return error; +} diff --git a/fs/xfs/scrub/repair.h b/fs/xfs/scrub/repair.h index 7e66918cce36..7a56244a8708 100644 --- a/fs/xfs/scrub/repair.h +++ b/fs/xfs/scrub/repair.h @@ -80,6 +80,7 @@ int xrep_setup_xattr(struct xfs_scrub *sc); int xrep_setup_directory(struct xfs_scrub *sc); int xrep_setup_parent(struct xfs_scrub *sc); int xrep_setup_rtbitmap(struct xfs_scrub *sc, unsigned int *resblks); +int xrep_setup_nlinks(struct xfs_scrub *sc); int xrep_xattr_reset_fork(struct xfs_scrub *sc, struct xfs_inode *ip); @@ -141,6 +142,7 @@ int xrep_fscounters(struct xfs_scrub *sc); int xrep_xattr(struct xfs_scrub *sc); int xrep_directory(struct xfs_scrub *sc); int xrep_parent(struct xfs_scrub *sc); +int xrep_nlinks(struct xfs_scrub *sc); #ifdef CONFIG_XFS_QUOTA int xrep_quota(struct xfs_scrub *sc); @@ -286,6 +288,7 @@ xrep_setup_xattr( #define xrep_setup_directory xrep_setup_xattr #define xrep_setup_parent xrep_setup_xattr +#define xrep_setup_nlinks xrep_setup_xattr #define xrep_revalidate_allocbt (NULL) #define xrep_revalidate_iallocbt (NULL) @@ -313,6 +316,7 @@ xrep_setup_xattr( #define xrep_rtbitmap xrep_notsupported #define xrep_rtrmapbt xrep_notsupported #define xrep_rtrefcountbt xrep_notsupported +#define xrep_nlinks xrep_notsupported #endif /* CONFIG_XFS_ONLINE_REPAIR */ diff --git a/fs/xfs/scrub/scrub.c b/fs/xfs/scrub/scrub.c index 55f2c37d01fd..181d66773b45 100644 --- a/fs/xfs/scrub/scrub.c +++ b/fs/xfs/scrub/scrub.c @@ -397,7 +397,7 @@ static const struct xchk_meta_ops meta_scrub_ops[] = { .type = ST_FS, .setup = xchk_setup_nlinks, .scrub = xchk_nlinks, - .repair = xrep_notsupported, + .repair = xrep_nlinks, }, }; diff --git a/fs/xfs/scrub/trace.h b/fs/xfs/scrub/trace.h index 6c2d874b9c56..e0f7cd0de061 100644 --- a/fs/xfs/scrub/trace.h +++ b/fs/xfs/scrub/trace.h @@ -2075,6 +2075,9 @@ TRACE_EVENT(xrep_rtrefc_found, __entry->refcount) ) +DEFINE_SCRUB_NLINK_DIFF_EVENT(xrep_nlinks_update_inode); +DEFINE_SCRUB_NLINK_DIFF_EVENT(xrep_nlinks_unfixable_inode); + #endif /* IS_ENABLED(CONFIG_XFS_ONLINE_REPAIR) */ #endif /* _TRACE_XFS_SCRUB_TRACE_H */ -- cgit v1.2.3