xfs: update superblock counters correctly for !lazysbcount
authorDave Chinner <dchinner@redhat.com>
Tue, 27 Apr 2021 01:28:31 +0000 (18:28 -0700)
committerDarrick J. Wong <djwong@kernel.org>
Thu, 29 Apr 2021 14:44:18 +0000 (07:44 -0700)
Keep the mount superblock counters up to date for !lazysbcount
filesystems so that when we log the superblock they do not need
updating in any way because they are already correct.

It's found by what Zorro reported:
1. mkfs.xfs -f -l lazy-count=0 -m crc=0 $dev
2. mount $dev $mnt
3. fsstress -d $mnt -p 100 -n 1000 (maybe need more or less io load)
4. umount $mnt
5. xfs_repair -n $dev
and I've seen no problem with this patch.

Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reported-by: Zorro Lang <zlang@redhat.com>
Reviewed-by: Gao Xiang <hsiangkao@redhat.com>
Signed-off-by: Gao Xiang <hsiangkao@redhat.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Brian Foster <bfoster@redhat.com>
fs/xfs/libxfs/xfs_sb.c
fs/xfs/xfs_trans.c

index 60e6d255e5e2c83a3f7f11d0d52c217a2653b338..dfbbcbd448c1a6be8b93c5333ad93a80df1a5a23 100644 (file)
@@ -926,9 +926,19 @@ xfs_log_sb(
        struct xfs_mount        *mp = tp->t_mountp;
        struct xfs_buf          *bp = xfs_trans_getsb(tp);
 
-       mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
-       mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree);
-       mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
+       /*
+        * Lazy sb counters don't update the in-core superblock so do that now.
+        * If this is at unmount, the counters will be exactly correct, but at
+        * any other time they will only be ballpark correct because of
+        * reservations that have been taken out percpu counters. If we have an
+        * unclean shutdown, this will be corrected by log recovery rebuilding
+        * the counters from the AGF block counts.
+        */
+       if (xfs_sb_version_haslazysbcount(&mp->m_sb)) {
+               mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
+               mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree);
+               mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
+       }
 
        xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
        xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
index 2b46b4f713d1aad02104d9c5737e0f329f93f350..586f2992b7892251561b40bba9f752454ba73ab1 100644 (file)
@@ -622,6 +622,9 @@ xfs_trans_unreserve_and_mod_sb(
 
        /* apply remaining deltas */
        spin_lock(&mp->m_sb_lock);
+       mp->m_sb.sb_fdblocks += tp->t_fdblocks_delta + tp->t_res_fdblocks_delta;
+       mp->m_sb.sb_icount += idelta;
+       mp->m_sb.sb_ifree += ifreedelta;
        mp->m_sb.sb_frextents += rtxdelta;
        mp->m_sb.sb_dblocks += tp->t_dblocks_delta;
        mp->m_sb.sb_agcount += tp->t_agcount_delta;