OSDN Git Service

btrfs: make the init of static elements in fs_info separate
authorJosef Bacik <josef@toxicpanda.com>
Fri, 24 Jan 2020 14:32:59 +0000 (09:32 -0500)
committerDavid Sterba <dsterba@suse.com>
Mon, 23 Mar 2020 16:01:33 +0000 (17:01 +0100)
In adding things like eb leak checking and root leak checking there were
a lot of weird corner cases that come from the fact that

  1) We do not init the fs_info until we get to open_ctree time in the
     normal case and

  2) The test infrastructure half-init's the fs_info for things that it
     needs.

This makes it really annoying to make changes because you have to add
init in two different places, have special cases for testing fs_info's
that may not have certain things initialized, and cases for fs_info's
that didn't make it to open_ctree and thus are not fully set up.

Fix this by extracting out the non-allocating init of the fs info into
it's own public function and use that to make sure we're all getting
consistent views of an allocated fs_info.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/disk-io.c
fs/btrfs/disk-io.h
fs/btrfs/super.c
fs/btrfs/tests/btrfs-tests.c

index 0bc6bc2..3e4df0c 100644 (file)
@@ -2649,10 +2649,8 @@ static int __cold init_tree_roots(struct btrfs_fs_info *fs_info)
        return ret;
 }
 
-static int init_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb)
+void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
 {
-       int ret;
-
        INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
        INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC);
        INIT_LIST_HEAD(&fs_info->trans_list);
@@ -2695,7 +2693,6 @@ static int init_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb)
        atomic_set(&fs_info->reada_works_cnt, 0);
        atomic_set(&fs_info->nr_delayed_iputs, 0);
        atomic64_set(&fs_info->tree_mod_seq, 0);
-       fs_info->sb = sb;
        fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE;
        fs_info->metadata_ratio = 0;
        fs_info->defrag_inodes = RB_ROOT;
@@ -2721,9 +2718,6 @@ static int init_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb)
        btrfs_init_balance(fs_info);
        btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work);
 
-       sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE;
-       sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE);
-
        spin_lock_init(&fs_info->block_group_cache_lock);
        fs_info->block_group_cache_tree = RB_ROOT;
        fs_info->first_logical_byte = (u64)-1;
@@ -2768,6 +2762,15 @@ static int init_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb)
        fs_info->swapfile_pins = RB_ROOT;
 
        fs_info->send_in_progress = 0;
+}
+
+static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb)
+{
+       int ret;
+
+       fs_info->sb = sb;
+       sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE;
+       sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE);
 
        ret = init_srcu_struct(&fs_info->subvol_srcu);
        if (ret)
@@ -2831,7 +2834,7 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
        int clear_free_space_tree = 0;
        int level;
 
-       ret = init_fs_info(fs_info, sb);
+       ret = init_mount_fs_info(fs_info, sb);
        if (ret) {
                err = ret;
                goto fail;
index 97e7ac4..2414d57 100644 (file)
@@ -39,6 +39,7 @@ static inline u64 btrfs_sb_offset(int mirror)
 struct btrfs_device;
 struct btrfs_fs_devices;
 
+void btrfs_init_fs_info(struct btrfs_fs_info *fs_info);
 int btrfs_verify_level_key(struct extent_buffer *eb, int level,
                           struct btrfs_key *first_key, u64 parent_transid);
 struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
index f1ad118..d6629e6 100644 (file)
@@ -1522,14 +1522,17 @@ static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
        /*
         * Setup a dummy root and fs_info for test/set super.  This is because
         * we don't actually fill this stuff out until open_ctree, but we need
-        * it for searching for existing supers, so this lets us do that and
-        * then open_ctree will properly initialize everything later.
+        * then open_ctree will properly initialize the file system specific
+        * settings later.  btrfs_init_fs_info initializes the static elements
+        * of the fs_info (locks and such) to make cleanup easier if we find a
+        * superblock with our given fs_devices later on at sget() time.
         */
        fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
        if (!fs_info) {
                error = -ENOMEM;
                goto error_sec_opts;
        }
+       btrfs_init_fs_info(fs_info);
 
        fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
        fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
index 5efd800..683381a 100644 (file)
@@ -120,6 +120,8 @@ struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize)
                kfree(fs_info);
                return NULL;
        }
+       INIT_LIST_HEAD(&fs_info->fs_devices->devices);
+
        fs_info->super_copy = kzalloc(sizeof(struct btrfs_super_block),
                                      GFP_KERNEL);
        if (!fs_info->super_copy) {
@@ -128,6 +130,8 @@ struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize)
                return NULL;
        }
 
+       btrfs_init_fs_info(fs_info);
+
        fs_info->nodesize = nodesize;
        fs_info->sectorsize = sectorsize;
 
@@ -138,29 +142,6 @@ struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize)
                return NULL;
        }
 
-       spin_lock_init(&fs_info->buffer_lock);
-       spin_lock_init(&fs_info->qgroup_lock);
-       spin_lock_init(&fs_info->super_lock);
-       spin_lock_init(&fs_info->fs_roots_radix_lock);
-       mutex_init(&fs_info->qgroup_ioctl_lock);
-       mutex_init(&fs_info->qgroup_rescan_lock);
-       rwlock_init(&fs_info->tree_mod_log_lock);
-       fs_info->running_transaction = NULL;
-       fs_info->qgroup_tree = RB_ROOT;
-       fs_info->qgroup_ulist = NULL;
-       atomic64_set(&fs_info->tree_mod_seq, 0);
-       INIT_LIST_HEAD(&fs_info->dirty_qgroups);
-       INIT_LIST_HEAD(&fs_info->dead_roots);
-       INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
-       INIT_LIST_HEAD(&fs_info->fs_devices->devices);
-       INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC);
-       INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
-       extent_io_tree_init(fs_info, &fs_info->freed_extents[0],
-                           IO_TREE_FS_INFO_FREED_EXTENTS0, NULL);
-       extent_io_tree_init(fs_info, &fs_info->freed_extents[1],
-                           IO_TREE_FS_INFO_FREED_EXTENTS1, NULL);
-       extent_map_tree_init(&fs_info->mapping_tree);
-       fs_info->pinned_extents = &fs_info->freed_extents[0];
        set_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
 
        test_mnt->mnt_sb->s_fs_info = fs_info;