btrfs: warn when remount will not change the free space tree
authorBoris Burkov <boris@bur.io>
Wed, 18 Nov 2020 23:06:24 +0000 (15:06 -0800)
committerDavid Sterba <dsterba@suse.com>
Wed, 9 Dec 2020 18:16:08 +0000 (19:16 +0100)
If the remount is ro->ro, rw->ro, or rw->rw, we will not create or
clear the free space tree. This can be surprising, so print a warning
to dmesg to make the failure more visible. It is also important to
ensure that the space cache options (SPACE_CACHE, FREE_SPACE_TREE) are
consistent, so ensure those are set to properly match the current on
disk state (which won't be changing).

Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Boris Burkov <boris@bur.io>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/super.c

index bd3ce2b4383873e1b36bd0aed5713b175cf4f027..022f2081008921fb9bab167d972c53360da7e35e 100644 (file)
@@ -1919,6 +1919,22 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data)
        btrfs_resize_thread_pool(fs_info,
                fs_info->thread_pool_size, old_thread_pool_size);
 
+       if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) !=
+           btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
+           (!sb_rdonly(sb) || (*flags & SB_RDONLY))) {
+               btrfs_warn(fs_info,
+               "remount supports changing free space tree only from ro to rw");
+               /* Make sure free space cache options match the state on disk */
+               if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
+                       btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
+                       btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
+               }
+               if (btrfs_free_space_cache_v1_active(fs_info)) {
+                       btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
+                       btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
+               }
+       }
+
        if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
                goto out;