]> git.hungrycats.org Git - linux/commitdiff
xfs: update quota flags on secondary supers when metadir enabled
authorDarrick J. Wong <djwong@kernel.org>
Tue, 13 Oct 2026 06:09:38 +0000 (23:09 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 6 Nov 2026 17:06:02 +0000 (12:06 -0500)
commit 9d9ea6e6cbc4cb1a36fb79f873d97e1ac2fe1a64 upstream.

xfs/085 exposes an interesting bug when metadir is enabled, quotas are
enabled via mount option, and the primary superblock crc fails
validation:

 Phase 1 - find and verify superblock...
 bad primary superblock - bad CRC in superblock !!!

 attempting to find secondary superblock...
 .found candidate secondary superblock...
 verified secondary superblock...
 quota accounting and enforcement flags lost

<snip>

 Phase 3 - for each AG...
         - scan and clear agi unlinked lists...
         - process known inodes and perform inode discovery...
         - agno = 0
         - agno = 1
         - agno = 2
 unexpected regular file inode 16908418 with metadata flag will zap
 cleared inode 16908418
 unexpected regular file inode 16908419 with metadata flag will zap
 cleared inode 16908419
 unexpected regular file inode 16908420 with metadata flag will zap
 cleared inode 16908420
         - agno = 3
         - process newly discovered inodes...

Quota files have been created, but xfs_repair thinks that quotas aren't
enabled and trashes the quota files.  Looking at the filesystem reveals
that the quota accounting flags were indeed persisted to the primary
superblock but not the secondary:

 # xfs_db -c 'sb 0' -c 'print qflags' -c 'sb 1' -c 'print qflags' /dev/sdf
 Metadata corruption detected at 0x561c68436080, inode 0x81 dinode
 xfs_db: Failed to load metadir root directory, error 117
 Metadata corruption detected at 0x561c683f7c80, xfs_agf block 0x1/0x200
 xfs_db: cannot init perag data (117). Continuing anyway.
 Metadata CRC error detected at 0x561c6844d1b5, xfs_sb block 0x0/0x200
 qflags = 0x7cf
 qflags = 0

On a metadir filesystem, quota state is supposed to persist across
mount cycles if the sysadmin doesn't provide any quota mount options.
Therefore, any time we update the quota flags on a metadir filesystem,
we should write the same change to the secondary supers so that
xfs_repair will find the quota data if it ever has to restore from a
secondary.

Cc: stable@vger.kernel.org # v6.13
Fixes: d5d9dd5b3026a8 ("xfs: persist quota flags with metadir")
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Carlos Maiolino <cem@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/xfs/xfs_mount.c
fs/xfs/xfs_qm.c
fs/xfs/xfs_quota.h

index 8848aea99c87f4ebfb151fac1ac5f38f86f11470..a94697db788a58b6bc74902c7d640c12522335e1 100644 (file)
@@ -448,6 +448,8 @@ int
 xfs_mount_reset_sbqflags(
        struct xfs_mount        *mp)
 {
+       int                     error;
+
        mp->m_qflags = 0;
 
        /* It is OK to look at sb_qflags in the mount path without m_sb_lock. */
@@ -460,7 +462,12 @@ xfs_mount_reset_sbqflags(
        if (!xfs_fs_writable(mp, SB_FREEZE_WRITE))
                return 0;
 
-       return xfs_sync_sb(mp, false);
+       error = xfs_sync_sb(mp, false);
+       if (error)
+               return error;
+
+       xfs_qm_update_secondary_sb(mp);
+       return 0;
 }
 
 static const char *const xfs_free_pool_name[] = {
index 5008b174ebd2b23260458f77d3b9d34d5632fe44..cd2d7448dd979113c131bca7236bbb10430a0079 100644 (file)
@@ -741,6 +741,32 @@ xfs_qm_create_metadir_qinos(
        return 0;
 }
 
+/*
+ * For metadir filesystems, quota flag state persists across mounts, so update
+ * secondary superblocks in case xfs_repair ever has to recover the primary
+ * from a secondary.  growfs also updates backup supers so lock against that.
+ * Note that the secondary backups are opportunistic, so we log but do not
+ * return write errors.
+ */
+void
+__xfs_qm_update_secondary_sb(
+       struct xfs_mount        *mp,
+       const char              *func)
+{
+       int                     error;
+
+       if (!xfs_has_metadir(mp))
+               return;
+
+       mutex_lock(&mp->m_growlock);
+       error = xfs_update_secondary_sbs(mp);
+       mutex_unlock(&mp->m_growlock);
+
+       if (error)
+               xfs_alert(mp, "%s: Secondary super update failed, err=%d!",
+                               func, error);
+}
+
 /*
  * Add QUOTABIT to sb_versionnum and initialize qflags in preparation for
  * creating quota files on a metadir filesystem.
@@ -766,7 +792,12 @@ xfs_qm_prep_metadir_sb(
        spin_unlock(&mp->m_sb_lock);
        xfs_log_sb(tp);
 
-       return xfs_trans_commit(tp);
+       error = xfs_trans_commit(tp);
+       if (error)
+               return error;
+
+       xfs_qm_update_secondary_sb(mp);
+       return 0;
 }
 
 /*
@@ -1721,6 +1752,9 @@ xfs_qm_mount_quotas(
                        ASSERT(!(XFS_IS_QUOTA_ON(mp)));
                        xfs_alert(mp, "%s: Superblock update failed!",
                                __func__);
+               } else {
+                       /* Primary update succeeded, try to write secondary */
+                       xfs_qm_update_secondary_sb(mp);
                }
        }
 
index 105e6eb5762011ab5464955b3531ba7c9f5b8f93..b323a72dafbcde1d40ae9bfee9c3be951b41078b 100644 (file)
@@ -151,6 +151,8 @@ void xfs_dqtrx_hook_setup(struct xfs_dqtrx_hook *hook, notifier_fn_t mod_fn,
                xfs_trans_mod_dquot((tp), (dqp), (field), (delta))
 # endif /* CONFIG_XFS_LIVE_HOOKS */
 
+void __xfs_qm_update_secondary_sb(struct xfs_mount *mp,
+               const char *func);
 #else
 static inline int
 xfs_qm_vop_dqalloc(struct xfs_inode *ip, kuid_t kuid, kgid_t kgid,
@@ -216,8 +218,15 @@ static inline int xfs_quota_reserve_blkres(struct xfs_inode *ip, int64_t blocks)
 #  define xfs_dqtrx_hook_disable()             ((void)0)
 # endif /* CONFIG_XFS_LIVE_HOOKS */
 
+static inline void __xfs_qm_update_secondary_sb(struct xfs_mount *mp,
+               const char *func)
+{
+}
 #endif /* CONFIG_XFS_QUOTA */
 
+#define xfs_qm_update_secondary_sb(mp) \
+       __xfs_qm_update_secondary_sb((mp), __func__)
+
 static inline void
 xfs_quota_unreserve_blkres(struct xfs_inode *ip, uint64_t blocks)
 {