From 948462294577a3870c407c16d89bb2314f0b0cfb Mon Sep 17 00:00:00 2001 From: Boris Burkov Date: Wed, 18 Nov 2020 15:06:22 -0800 Subject: btrfs: keep sb cache_generation consistent with space_cache When mounting, btrfs uses the cache_generation in the super block to determine if space cache v1 is in use. However, by mounting with nospace_cache or space_cache=v2, it is possible to disable space cache v1, which does not result in un-setting cache_generation back to 0. In order to base some logic, like mount option printing in /proc/mounts, on the current state of the space cache rather than just the values of the mount option, keep the value of cache_generation consistent with the status of space cache v1. We ensure that cache_generation > 0 iff the file system is using space_cache v1. This requires committing a transaction on any mount which changes whether we are using v1. (v1->nospace_cache, v1->v2, nospace_cache->v1, v2->v1). Since the mechanism for writing out the cache generation is transaction commit, but we want some finer grained control over when we un-set it, we can't just rely on the SPACE_CACHE mount option, and introduce an fs_info flag that mount can use when it wants to unset the generation. Reviewed-by: Josef Bacik Signed-off-by: Boris Burkov Signed-off-by: David Sterba --- fs/btrfs/disk-io.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'fs/btrfs/disk-io.c') diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index 77c7013f69d7..2dcc5e52140c 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -2888,6 +2888,7 @@ void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info) int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info) { int ret; + const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE); bool clear_free_space_tree = false; if (btrfs_test_opt(fs_info, CLEAR_CACHE) && @@ -2940,6 +2941,12 @@ int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info) } } + if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) { + ret = btrfs_set_free_space_cache_v1_active(fs_info, cache_opt); + if (ret) + goto out; + } + ret = btrfs_resume_balance_async(fs_info); if (ret) goto out; @@ -3410,6 +3417,7 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device return ret; } } + set_bit(BTRFS_FS_OPEN, &fs_info->flags); clear_oneshot: -- cgit v1.2.3