btrfs: set update the uuid generation as soon as possible
authorJosef Bacik <josef@toxicpanda.com>
Fri, 14 Feb 2020 20:22:06 +0000 (15:22 -0500)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 17 Apr 2020 08:50:15 +0000 (10:50 +0200)
commit 75ec1db8717a8f0a9d9c8d033e542fdaa7b73898 upstream.

In my EIO stress testing I noticed I was getting forced to rescan the
uuid tree pretty often, which was weird.  This is because my error
injection stuff would sometimes inject an error after log replay but
before we loaded the UUID tree.  If log replay committed the transaction
it wouldn't have updated the uuid tree generation, but the tree was
valid and didn't change, so there's no reason to not update the
generation here.

Fix this by setting the BTRFS_FS_UPDATE_UUID_TREE_GEN bit immediately
after reading all the fs roots if the uuid tree generation matches the
fs generation.  Then any transaction commits that happen during mount
won't screw up our uuid tree state, forcing us to do needless uuid
rescans.

Fixes: 70f801754728 ("Btrfs: check UUID tree during mount if required")
CC: stable@vger.kernel.org # 4.19+
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/btrfs/disk-io.c

index 858b41f..273d1cc 100644 (file)
@@ -3057,6 +3057,18 @@ retry_root_backup:
        fs_info->generation = generation;
        fs_info->last_trans_committed = generation;
 
+       /*
+        * If we have a uuid root and we're not being told to rescan we need to
+        * check the generation here so we can set the
+        * BTRFS_FS_UPDATE_UUID_TREE_GEN bit.  Otherwise we could commit the
+        * transaction during a balance or the log replay without updating the
+        * uuid generation, and then if we crash we would rescan the uuid tree,
+        * even though it was perfectly fine.
+        */
+       if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) &&
+           fs_info->generation == btrfs_super_uuid_tree_generation(disk_super))
+               set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
+
        ret = btrfs_verify_dev_extents(fs_info);
        if (ret) {
                btrfs_err(fs_info,
@@ -3287,8 +3299,6 @@ retry_root_backup:
                        close_ctree(fs_info);
                        return ret;
                }
-       } else {
-               set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
        }
        set_bit(BTRFS_FS_OPEN, &fs_info->flags);