xfs: stabilize fs summary counters for online fsck
PATCH v2 1/1 xfs: stabilize fs summary counters for online fsck
From: Darrick J. Wong <[email protected]>
If the fscounters scrubber notices incorrect summary counters, it's entirely possible that scrub is simply racing with other threads that are updating the incore counters. There isn't a good way to stabilize percpu counters or set ourselves up to observe live updates with hooks like we do for the quotacheck or nlinks scanners, so we instead choose to freeze the filesystem long enough to walk the incore per-AG structures.
Past me thought that it was going to be commonplace to have to freeze the filesystem to perform some kind of repair and set up a whole separate infrastructure to freeze the filesystem in such a way that userspace could not unfreeze while we were running. This involved adding a mutex and freeze_super/thaw_super functions and dealing with the fact that the VFS freeze/thaw functions can free the VFS superblock references on return.
This was all very overwrought, since fscounters turned out to be the only user of scrub freezes, and it doesn't require the log to quiesce, only the incore superblock counters. We prevent other threads from changing the freeze level by calling freeze_super_excl with a custom freeze cookie to keep everyone else out of the filesystem.
The end result is that fscounters should be much more efficient. When we're checking a busy system and we can't stabilize the counters, the custom freeze will do less work, which should result in less downtime. Repair should be similarly speedy, but that's in the next patch.
---
v2: remove fscounters.h
--- fs/xfs/scrub/fscounters.c | 188 ++++++++++++++++++++++++++++++++++++--------- fs/xfs/scrub/scrub.c | 6 + fs/xfs/scrub/scrub.h | 1 fs/xfs/scrub/trace.h | 26 ++++++ 4 files changed, 183 insertions(+), 38 deletions(-)
fs/xfs/scrub/fscounters.c +151 -37
@@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (C) 2019-2023 Oracle. All Rights Reserved. * Author: Darrick J. Wong <[email protected]>
@@ -8,6 +8,8 @@#include "xfs_shared.h" #include "xfs_format.h" #include "xfs_trans_resv.h" +#include "xfs_log_format.h" +#include "xfs_trans.h" #include "xfs_mount.h" #include "xfs_alloc.h" #include "xfs_ialloc.h"
@@ -16,6 +18,7 @@#include "xfs_ag.h" #include "xfs_rtalloc.h" #include "xfs_inode.h" +#include "xfs_icache.h" #include "scrub/scrub.h" #include "scrub/common.h" #include "scrub/trace.h"
@@ -53,6 +56,7 @@ struct xchk_fscounters {uint64_t frextents;unsigned long long icount_min;unsigned long long icount_max; +bool frozen; };/*
@@ -123,6 +127,82 @@ xchk_fscount_warmup(return error; } +static inline int +xchk_fsfreeze ( +struct xfs_scrub *sc) +{ +int error; + + error =freeze_super (sc->mp->m_super,FREEZE_HOLDER_KERNEL ); +trace_xchk_fsfreeze (sc, error); +return error; +} + +static inline int +xchk_fsthaw ( +struct xfs_scrub *sc) +{ +int error; + +/* This should always succeed, we have a kernel freeze */ + error =thaw_super (sc->mp->m_super,FREEZE_HOLDER_KERNEL ); +trace_xchk_fsthaw (sc, error); +return error; +} + +/* +* We couldn't stabilize the filesystem long enough to sample all the variables +* that comprise the summary counters and compare them to the percpu counters. +* We need to disable all writer threads, which means taking the first two +* freeze levels to put userspace to sleep, and the third freeze level to +* prevent background threads from starting new transactions. Take one level +* more to prevent other callers from unfreezing the filesystem while we run. +*/ +STATIC int +xchk_fscounters_freeze ( +struct xfs_scrub *sc) +{ +struct xchk_fscounters *fsc = sc->buf; +int error =0 ; + +if (sc->flags &XCHK_HAVE_FREEZE_PROT ) { + sc->flags &= ~XCHK_HAVE_FREEZE_PROT ; +mnt_drop_write_file (sc->file); + } + +/* Try to grab a kernel freeze. */ +while ((error =xchk_fsfreeze (sc)) == -EBUSY ) { +if (xchk_should_terminate (sc, &error)) +return error; + +delay (HZ /10 ); + } +if (error) +return error; + + fsc->frozen = true; +return 0 ; +} + +/* Thaw the filesystem after checking or repairing fscounters. */ +STATIC void +xchk_fscounters_cleanup ( +void *buf) +{ +struct xchk_fscounters *fsc = buf; +struct xfs_scrub *sc = fsc->sc; +int error; + +if (!fsc->frozen) +return ; + + error =xchk_fsthaw (sc); +if (error) +xfs_emerg (sc->mp,"still frozen after scrub, err=%d" , error); +else + fsc->frozen = false; +} +int xchk_setup_fscounters (struct xfs_scrub *sc)
@@ -140,6 +220,7 @@ xchk_setup_fscounters( sc->buf =kzalloc (sizeof (struct xchk_fscounters ),XCHK_GFP_FLAGS );if (!sc->buf)return -ENOMEM ; + sc->buf_cleanup = xchk_fscounters_cleanup; fsc = sc->buf; fsc->sc = sc;
@@ -150,7 +231,18 @@ xchk_setup_fscounters(if (error)return error; -return xchk_trans_alloc (sc,0 ); +/* +* Pause all writer activity in the filesystem while we're scrubbing to +* reduce the likelihood of background perturbations to the counters +* throwing off our calculations. +*/ +if (sc->flags &XCHK_TRY_HARDER ) { + error =xchk_fscounters_freeze (sc); +if (error) +return error; + } + +return xfs_trans_alloc_empty (sc->mp, &sc->tp); }/*
@@ -290,8 +382,7 @@ xchk_fscount_aggregate_agcounts(if (fsc->ifree > fsc->icount) {if (tries--) goto retry; -xchk_set_incomplete (sc); -return 0 ; +return -EDEADLOCK ; }return 0 ;
@@ -367,6 +458,8 @@ xchk_fscount_count_frextents(* Otherwise, we /might/ have a problem. If the change in the summations is * more than we want to tolerate, the filesystem is probably busy and we should * just send back INCOMPLETE and see if userspace will try again. +* +* If we're repairing then we require an exact match. */ static inline bool xchk_fscount_within_range(
@@ -396,21 +489,7 @@ xchk_fscount_within_range(if (expected >= min_value && expected <= max_value)return true; -/* -* If the difference between the two summations is too large, the fs -* might just be busy and so we'll mark the scrub incomplete. Return -* true here so that we don't mark the counter corrupt. -* -* XXX: In the future when userspace can grant scrub permission to -* quiesce the filesystem to solve the outsized variance problem, this -* check should be moved up and the return code changed to signal to -* userspace that we need quiesce permission. -*/ -if (max_value - min_value >=XCHK_FSCOUNT_MIN_VARIANCE ) { -xchk_set_incomplete (sc); -return true; - } - +/* Everything else is bad. */ return false; }
@@ -422,6 +501,7 @@ xchk_fscounters(struct xfs_mount *mp = sc->mp;struct xchk_fscounters *fsc = sc->buf;int64_t icount, ifree, fdblocks, frextents; +bool try_again = false;int error;/* Snapshot the percpu counters. */
@@ -431,9 +511,26 @@ xchk_fscounters( frextents =percpu_counter_sum (&mp->m_frextents);/* No negative values, please! */ -if (icount <0 || ifree <0 || fdblocks <0 || frextents <0 ) +if (icount <0 || ifree <0 )xchk_set_corrupt (sc); +/* +* If the filesystem is not frozen, the counter summation calls above +* can race with xfs_mod_freecounter, which subtracts a requested space +* reservation from the counter and undoes the subtraction if that made +* the counter go negative. Therefore, it's possible to see negative +* values here, and we should only flag that as a corruption if we +* froze the fs. This is much more likely to happen with frextents +* since there are no reserved pools. +*/ +if (fdblocks <0 || frextents <0 ) { +if (!fsc->frozen) +return -EDEADLOCK ; + +xchk_set_corrupt (sc); +return 0 ; + } +/* See if icount is obviously wrong. */ if (icount < fsc->icount_min || icount > fsc->icount_max)xchk_set_corrupt (sc);
@@ -446,12 +543,6 @@ xchk_fscounters(if (frextents > mp->m_sb.sb_rextents)xchk_set_corrupt (sc); -/* -* XXX: We can't quiesce percpu counter updates, so exit early. -* This can be re-enabled when we gain exclusive freeze functionality. -*/ -return 0 ; -/* * If ifree exceeds icount by more than the minimum variance then * something's probably wrong with the counters.
@@ -463,8 +554,6 @@ xchk_fscounters( error =xchk_fscount_aggregate_agcounts (sc, fsc);if (!xchk_process_error (sc,0 ,XFS_SB_BLOCK (mp), &error))return error; -if (sc->sm->sm_flags &XFS_SCRUB_OFLAG_INCOMPLETE ) -return 0 ;/* Count the free extents counter for rt volumes. */ error =xchk_fscount_count_frextents (sc, fsc);
@@ -473,20 +562,45 @@ xchk_fscounters(if (sc->sm->sm_flags &XFS_SCRUB_OFLAG_INCOMPLETE )return 0 ; -/* Compare the in-core counters with whatever we counted. */ -if (!xchk_fscount_within_range (sc, icount, &mp->m_icount, fsc->icount)) -xchk_set_corrupt (sc); +/* +* Compare the in-core counters with whatever we counted. If the fs is +* frozen, we treat the discrepancy as a corruption because the freeze +* should have stabilized the counter values. Otherwise, we need +* userspace to call us back having granted us freeze permission. +*/ +if (!xchk_fscount_within_range (sc, icount, &mp->m_icount, + fsc->icount)) { +if (fsc->frozen) +xchk_set_corrupt (sc); +else + try_again = true; + } -if (!xchk_fscount_within_range (sc, ifree, &mp->m_ifree, fsc->ifree)) -xchk_set_corrupt (sc); +if (!xchk_fscount_within_range (sc, ifree, &mp->m_ifree, fsc->ifree)) { +if (fsc->frozen) +xchk_set_corrupt (sc); +else + try_again = true; + }if (!xchk_fscount_within_range (sc, fdblocks, &mp->m_fdblocks, - fsc->fdblocks)) -xchk_set_corrupt (sc); + fsc->fdblocks)) { +if (fsc->frozen) +xchk_set_corrupt (sc); +else + try_again = true; + }if (!xchk_fscount_within_range (sc, frextents, &mp->m_frextents, - fsc->frextents)) -xchk_set_corrupt (sc); + fsc->frextents)) { +if (fsc->frozen) +xchk_set_corrupt (sc); +else + try_again = true; + } + +if (try_again) +return -EDEADLOCK ;return 0 ; }
fs/xfs/scrub/scrub.c +5 -1
@@ -184,8 +184,10 @@ xchk_teardown(xchk_irele (sc, sc->ip); sc->ip =NULL ; } -if (sc->sm->sm_flags &XFS_SCRUB_IFLAG_REPAIR ) +if (sc->flags &XCHK_HAVE_FREEZE_PROT ) { + sc->flags &= ~XCHK_HAVE_FREEZE_PROT ;mnt_drop_write_file (sc->file); + }if (sc->buf) {if (sc->buf_cleanup) sc->buf_cleanup (sc->buf);
@@ -505,6 +507,8 @@ xfs_scrub_metadata( error =mnt_want_write_file (sc->file);if (error) goto out_sc; + + sc->flags |=XCHK_HAVE_FREEZE_PROT ; }/* Set up for the operation. */
fs/xfs/scrub/scrub.h +1 -0
@@ -106,6 +106,7 @@ struct xfs_scrub {/* XCHK state flags grow up from zero, XREP state flags grown down from 2^31 */ #define XCHK_TRY_HARDER (1U << 0)/* can't get resources, try again */ +#define XCHK_HAVE_FREEZE_PROT (1U << 1)/* do we have freeze protection? */ #define XCHK_FSGATES_DRAIN (1U << 2)/* defer ops draining enabled */ #define XCHK_NEED_DRAIN (1U << 3)/* scrub needs to drain defer ops */ #define XREP_ALREADY_FIXED (1U << 31)/* checking our repair work */
fs/xfs/scrub/trace.h +26 -0
@@ -98,6 +98,7 @@ TRACE_DEFINE_ENUM(XFS_SCRUB_TYPE_FSCOUNTERS);#define XFS_SCRUB_STATE_STRINGS \ { XCHK_TRY_HARDER, "try_harder" }, \ + { XCHK_HAVE_FREEZE_PROT, "nofreeze" }, \ { XCHK_FSGATES_DRAIN, "fsgates_drain" }, \ { XCHK_NEED_DRAIN, "need_drain" }, \ { XREP_ALREADY_FIXED, "already_fixed" }
@@ -693,6 +694,31 @@ TRACE_EVENT(xchk_fscounters_within_range, __entry->old_value) ) +DECLARE_EVENT_CLASS (xchk_fsfreeze_class, + TP_PROTO(struct xfs_scrub *sc, int error), +TP_ARGS (sc, error), +TP_STRUCT__entry ( + __field(dev_t, dev) + __field(unsigned int, type) +__field (int, error) + ), +TP_fast_assign ( + __entry->dev = sc->mp->m_super->s_dev; + __entry->type = sc->sm->sm_type; + __entry->error = error; + ), +TP_printk ("dev %d:%d type %s error %d" , +MAJOR (__entry->dev),MINOR (__entry->dev), +__print_symbolic (__entry->type,XFS_SCRUB_TYPE_STRINGS ), + __entry->error) +); +#define DEFINE_XCHK_FSFREEZE_EVENT (name) \ +DEFINE_EVENT(xchk_fsfreeze_class, name, \ + TP_PROTO(struct xfs_scrub *sc, int error), \ + TP_ARGS(sc, error)) +DEFINE_XCHK_FSFREEZE_EVENT (xchk_fsfreeze); +DEFINE_XCHK_FSFREEZE_EVENT (xchk_fsthaw); + TRACE_EVENT(xchk_refcount_incorrect, TP_PROTO(struct xfs_perag *pag, const struct xfs_refcount_irec *irec, xfs_nlink_t seen),
Quoted 30 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
If the fscounters scrubber notices incorrect summary counters, it's entirely possible that scrub is simply racing with other threads that are updating the incore counters. There isn't a good way to stabilize percpu counters or set ourselves up to observe live updates with hooks like we do for the quotacheck or nlinks scanners, so we instead choose to freeze the filesystem long enough to walk the incore per-AG structures.
Past me thought that it was going to be commonplace to have to freeze the filesystem to perform some kind of repair and set up a whole separate infrastructure to freeze the filesystem in such a way that userspace could not unfreeze while we were running. This involved adding a mutex and freeze_super/thaw_super functions and dealing with the fact that the VFS freeze/thaw functions can free the VFS superblock references on return.
This was all very overwrought, since fscounters turned out to be the only user of scrub freezes, and it doesn't require the log to quiesce, only the incore superblock counters. We prevent other threads from changing the freeze level by calling
freeze_super_exclwith a custom freeze cookie to keep everyone else out of the filesystem.The end result is that fscounters should be much more efficient. When we're checking a busy system and we can't stabilize the counters, the custom freeze will do less work, which should result in less downtime. Repair should be similarly speedy, but that's in the next patch.
Signed-off-by Darrick J. Wong <[email protected]>
Quoted 8 lines
---
v2: removefscounters.h--- fs/xfs/scrub/fscounters.c | 188 ++++++++++++++++++++++++++++++++++++--------- fs/xfs/scrub/scrub.c | 6 + fs/xfs/scrub/scrub.h | 1 fs/xfs/scrub/trace.h | 26 ++++++ 4 files changed, 183 insertions(+), 38 deletions(-)
looks good.
Signature · 2 lines
Dave Chinner [email protected]
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Darrick,
kernel test robot noticed the following build errors:
[auto build test ERROR on xfs-linux/for-next]
[cannot apply to dgc-xfs/for-next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Darrick-J-Wong/xfs-stabilize-fs-summary-counters-for-online-fsck/20230804-073132 base: https://git.kernel.org/pub/scm/fs/xfs/xfs-linux.git for-next patch link: https://lore.kernel.org/r/20230803233028.GG11352%40frogsfrogsfrogs patch subject: [PATCH v2] xfs: stabilize fs summary counters for online fsck config: hexagon-randconfig-r022-20230803 (https://download.01.org/0day-ci/archive/20230804/[email protected]/config) compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project.git 4a5ac14ee968ff0ad5d2cc1ffa0299048db4c88a) reproduce: (https://download.01.org/0day-ci/archive/20230804/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from fs/xfs/scrub/fscounters.c:6:
In file included from fs/xfs/xfs.h:22:
In file included from fs/xfs/xfs_linux.h:31:
In file included from include/linux/blkdev.h:9:
In file included from include/linux/blk_types.h:10:
In file included from include/linux/bvec.h:10:
In file included from include/linux/highmem.h:12:
In file included from include/linux/hardirq.h:11:
In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:547:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
547 | val = __raw_readb(PCI_IOBASE + addr);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:560:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
560 | val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
| ~~~~~~~~~~ ^
include/uapi/linux/byteorder/little_endian.h:37:51: note: expanded from macro '__le16_to_cpu'
37 | #define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
| ^
In file included from fs/xfs/scrub/fscounters.c:6:
In file included from fs/xfs/xfs.h:22:
In file included from fs/xfs/xfs_linux.h:31:
In file included from include/linux/blkdev.h:9:
In file included from include/linux/blk_types.h:10:
In file included from include/linux/bvec.h:10:
In file included from include/linux/highmem.h:12:
In file included from include/linux/hardirq.h:11:
In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:573:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
573 | val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
| ~~~~~~~~~~ ^
include/uapi/linux/byteorder/little_endian.h:35:51: note: expanded from macro '__le32_to_cpu'
35 | #define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
| ^
In file included from fs/xfs/scrub/fscounters.c:6:
In file included from fs/xfs/xfs.h:22:
In file included from fs/xfs/xfs_linux.h:31:
In file included from include/linux/blkdev.h:9:
In file included from include/linux/blk_types.h:10:
In file included from include/linux/bvec.h:10:
In file included from include/linux/highmem.h:12:
In file included from include/linux/hardirq.h:11:
In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:584:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
584 | __raw_writeb(value, PCI_IOBASE + addr);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:594:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
594 | __raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:604:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
604 | __raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
| ~~~~~~~~~~ ^
>> fs/xfs/scrub/fscounters.c:136:40: error: use of undeclared identifier 'FREEZE_HOLDER_KERNEL'
136 | error = freeze_super(sc->mp->m_super, FREEZE_HOLDER_KERNEL);
| ^
fs/xfs/scrub/fscounters.c:148:38: error: use of undeclared identifier 'FREEZE_HOLDER_KERNEL'
148 | error = thaw_super(sc->mp->m_super, FREEZE_HOLDER_KERNEL);
| ^6 warnings and 2 errors generated.
vim +/FREEZE_HOLDER_KERNEL +136 fs/xfs/scrub/fscounters.c
129
130 static inline int
131 xchk_fsfreeze(
132 struct xfs_scrub *sc)
133 {
134 int error;
135
> 136 error = freeze_super(sc->mp->m_super, FREEZE_HOLDER_KERNEL);
137 trace_xchk_fsfreeze(sc, error);
138 return error;
139 }
140
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki