| .. | .. |
|---|
| 5 | 5 | |
|---|
| 6 | 6 | #include <linux/fs.h> |
|---|
| 7 | 7 | #include <linux/mount.h> |
|---|
| 8 | +#include <linux/pseudo_fs.h> |
|---|
| 8 | 9 | #include <linux/magic.h> |
|---|
| 9 | 10 | #include "btrfs-tests.h" |
|---|
| 10 | 11 | #include "../ctree.h" |
|---|
| .. | .. |
|---|
| 14 | 15 | #include "../volumes.h" |
|---|
| 15 | 16 | #include "../disk-io.h" |
|---|
| 16 | 17 | #include "../qgroup.h" |
|---|
| 18 | +#include "../block-group.h" |
|---|
| 17 | 19 | |
|---|
| 18 | 20 | static struct vfsmount *test_mnt = NULL; |
|---|
| 21 | + |
|---|
| 22 | +const char *test_error[] = { |
|---|
| 23 | + [TEST_ALLOC_FS_INFO] = "cannot allocate fs_info", |
|---|
| 24 | + [TEST_ALLOC_ROOT] = "cannot allocate root", |
|---|
| 25 | + [TEST_ALLOC_EXTENT_BUFFER] = "cannot extent buffer", |
|---|
| 26 | + [TEST_ALLOC_PATH] = "cannot allocate path", |
|---|
| 27 | + [TEST_ALLOC_INODE] = "cannot allocate inode", |
|---|
| 28 | + [TEST_ALLOC_BLOCK_GROUP] = "cannot allocate block group", |
|---|
| 29 | + [TEST_ALLOC_EXTENT_MAP] = "cannot allocate extent map", |
|---|
| 30 | +}; |
|---|
| 19 | 31 | |
|---|
| 20 | 32 | static const struct super_operations btrfs_test_super_ops = { |
|---|
| 21 | 33 | .alloc_inode = btrfs_alloc_inode, |
|---|
| 22 | 34 | .destroy_inode = btrfs_test_destroy_inode, |
|---|
| 23 | 35 | }; |
|---|
| 24 | 36 | |
|---|
| 25 | | -static struct dentry *btrfs_test_mount(struct file_system_type *fs_type, |
|---|
| 26 | | - int flags, const char *dev_name, |
|---|
| 27 | | - void *data) |
|---|
| 37 | + |
|---|
| 38 | +static int btrfs_test_init_fs_context(struct fs_context *fc) |
|---|
| 28 | 39 | { |
|---|
| 29 | | - return mount_pseudo(fs_type, "btrfs_test:", &btrfs_test_super_ops, |
|---|
| 30 | | - NULL, BTRFS_TEST_MAGIC); |
|---|
| 40 | + struct pseudo_fs_context *ctx = init_pseudo(fc, BTRFS_TEST_MAGIC); |
|---|
| 41 | + if (!ctx) |
|---|
| 42 | + return -ENOMEM; |
|---|
| 43 | + ctx->ops = &btrfs_test_super_ops; |
|---|
| 44 | + return 0; |
|---|
| 31 | 45 | } |
|---|
| 32 | 46 | |
|---|
| 33 | 47 | static struct file_system_type test_type = { |
|---|
| 34 | 48 | .name = "btrfs_test_fs", |
|---|
| 35 | | - .mount = btrfs_test_mount, |
|---|
| 49 | + .init_fs_context = btrfs_test_init_fs_context, |
|---|
| 36 | 50 | .kill_sb = kill_anon_super, |
|---|
| 37 | 51 | }; |
|---|
| 38 | 52 | |
|---|
| .. | .. |
|---|
| 72 | 86 | unregister_filesystem(&test_type); |
|---|
| 73 | 87 | } |
|---|
| 74 | 88 | |
|---|
| 89 | +struct btrfs_device *btrfs_alloc_dummy_device(struct btrfs_fs_info *fs_info) |
|---|
| 90 | +{ |
|---|
| 91 | + struct btrfs_device *dev; |
|---|
| 92 | + |
|---|
| 93 | + dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
|---|
| 94 | + if (!dev) |
|---|
| 95 | + return ERR_PTR(-ENOMEM); |
|---|
| 96 | + |
|---|
| 97 | + extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL); |
|---|
| 98 | + INIT_LIST_HEAD(&dev->dev_list); |
|---|
| 99 | + list_add(&dev->dev_list, &fs_info->fs_devices->devices); |
|---|
| 100 | + |
|---|
| 101 | + return dev; |
|---|
| 102 | +} |
|---|
| 103 | + |
|---|
| 104 | +static void btrfs_free_dummy_device(struct btrfs_device *dev) |
|---|
| 105 | +{ |
|---|
| 106 | + extent_io_tree_release(&dev->alloc_state); |
|---|
| 107 | + kfree(dev); |
|---|
| 108 | +} |
|---|
| 109 | + |
|---|
| 75 | 110 | struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize) |
|---|
| 76 | 111 | { |
|---|
| 77 | 112 | struct btrfs_fs_info *fs_info = kzalloc(sizeof(struct btrfs_fs_info), |
|---|
| .. | .. |
|---|
| 85 | 120 | kfree(fs_info); |
|---|
| 86 | 121 | return NULL; |
|---|
| 87 | 122 | } |
|---|
| 123 | + INIT_LIST_HEAD(&fs_info->fs_devices->devices); |
|---|
| 124 | + |
|---|
| 88 | 125 | fs_info->super_copy = kzalloc(sizeof(struct btrfs_super_block), |
|---|
| 89 | 126 | GFP_KERNEL); |
|---|
| 90 | 127 | if (!fs_info->super_copy) { |
|---|
| .. | .. |
|---|
| 93 | 130 | return NULL; |
|---|
| 94 | 131 | } |
|---|
| 95 | 132 | |
|---|
| 133 | + btrfs_init_fs_info(fs_info); |
|---|
| 134 | + |
|---|
| 96 | 135 | fs_info->nodesize = nodesize; |
|---|
| 97 | 136 | fs_info->sectorsize = sectorsize; |
|---|
| 98 | | - |
|---|
| 99 | | - if (init_srcu_struct(&fs_info->subvol_srcu)) { |
|---|
| 100 | | - kfree(fs_info->fs_devices); |
|---|
| 101 | | - kfree(fs_info->super_copy); |
|---|
| 102 | | - kfree(fs_info); |
|---|
| 103 | | - return NULL; |
|---|
| 104 | | - } |
|---|
| 105 | | - |
|---|
| 106 | | - spin_lock_init(&fs_info->buffer_lock); |
|---|
| 107 | | - spin_lock_init(&fs_info->qgroup_lock); |
|---|
| 108 | | - spin_lock_init(&fs_info->qgroup_op_lock); |
|---|
| 109 | | - spin_lock_init(&fs_info->super_lock); |
|---|
| 110 | | - spin_lock_init(&fs_info->fs_roots_radix_lock); |
|---|
| 111 | | - mutex_init(&fs_info->qgroup_ioctl_lock); |
|---|
| 112 | | - mutex_init(&fs_info->qgroup_rescan_lock); |
|---|
| 113 | | - rwlock_init(&fs_info->tree_mod_log_lock); |
|---|
| 114 | | - fs_info->running_transaction = NULL; |
|---|
| 115 | | - fs_info->qgroup_tree = RB_ROOT; |
|---|
| 116 | | - fs_info->qgroup_ulist = NULL; |
|---|
| 117 | | - atomic64_set(&fs_info->tree_mod_seq, 0); |
|---|
| 118 | | - INIT_LIST_HEAD(&fs_info->dirty_qgroups); |
|---|
| 119 | | - INIT_LIST_HEAD(&fs_info->dead_roots); |
|---|
| 120 | | - INIT_LIST_HEAD(&fs_info->tree_mod_seq_list); |
|---|
| 121 | | - INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); |
|---|
| 122 | | - INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); |
|---|
| 123 | | - extent_io_tree_init(&fs_info->freed_extents[0], NULL); |
|---|
| 124 | | - extent_io_tree_init(&fs_info->freed_extents[1], NULL); |
|---|
| 125 | | - fs_info->pinned_extents = &fs_info->freed_extents[0]; |
|---|
| 126 | 137 | set_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); |
|---|
| 127 | 138 | |
|---|
| 128 | 139 | test_mnt->mnt_sb->s_fs_info = fs_info; |
|---|
| .. | .. |
|---|
| 134 | 145 | { |
|---|
| 135 | 146 | struct radix_tree_iter iter; |
|---|
| 136 | 147 | void **slot; |
|---|
| 148 | + struct btrfs_device *dev, *tmp; |
|---|
| 137 | 149 | |
|---|
| 138 | 150 | if (!fs_info) |
|---|
| 139 | 151 | return; |
|---|
| .. | .. |
|---|
| 164 | 176 | } |
|---|
| 165 | 177 | spin_unlock(&fs_info->buffer_lock); |
|---|
| 166 | 178 | |
|---|
| 179 | + btrfs_mapping_tree_free(&fs_info->mapping_tree); |
|---|
| 180 | + list_for_each_entry_safe(dev, tmp, &fs_info->fs_devices->devices, |
|---|
| 181 | + dev_list) { |
|---|
| 182 | + btrfs_free_dummy_device(dev); |
|---|
| 183 | + } |
|---|
| 167 | 184 | btrfs_free_qgroup_config(fs_info); |
|---|
| 168 | 185 | btrfs_free_fs_roots(fs_info); |
|---|
| 169 | | - cleanup_srcu_struct(&fs_info->subvol_srcu); |
|---|
| 170 | 186 | kfree(fs_info->super_copy); |
|---|
| 187 | + btrfs_check_leaked_roots(fs_info); |
|---|
| 188 | + btrfs_extent_buffer_leak_debug_check(fs_info); |
|---|
| 171 | 189 | kfree(fs_info->fs_devices); |
|---|
| 172 | 190 | kfree(fs_info); |
|---|
| 173 | 191 | } |
|---|
| 174 | 192 | |
|---|
| 175 | 193 | void btrfs_free_dummy_root(struct btrfs_root *root) |
|---|
| 176 | 194 | { |
|---|
| 177 | | - if (!root) |
|---|
| 195 | + if (IS_ERR_OR_NULL(root)) |
|---|
| 178 | 196 | return; |
|---|
| 179 | 197 | /* Will be freed by btrfs_free_fs_roots */ |
|---|
| 180 | 198 | if (WARN_ON(test_bit(BTRFS_ROOT_IN_RADIX, &root->state))) |
|---|
| 181 | 199 | return; |
|---|
| 182 | | - if (root->node) |
|---|
| 183 | | - free_extent_buffer(root->node); |
|---|
| 184 | | - kfree(root); |
|---|
| 200 | + btrfs_put_root(root); |
|---|
| 185 | 201 | } |
|---|
| 186 | 202 | |
|---|
| 187 | | -struct btrfs_block_group_cache * |
|---|
| 203 | +struct btrfs_block_group * |
|---|
| 188 | 204 | btrfs_alloc_dummy_block_group(struct btrfs_fs_info *fs_info, |
|---|
| 189 | 205 | unsigned long length) |
|---|
| 190 | 206 | { |
|---|
| 191 | | - struct btrfs_block_group_cache *cache; |
|---|
| 207 | + struct btrfs_block_group *cache; |
|---|
| 192 | 208 | |
|---|
| 193 | 209 | cache = kzalloc(sizeof(*cache), GFP_KERNEL); |
|---|
| 194 | 210 | if (!cache) |
|---|
| .. | .. |
|---|
| 200 | 216 | return NULL; |
|---|
| 201 | 217 | } |
|---|
| 202 | 218 | |
|---|
| 203 | | - cache->key.objectid = 0; |
|---|
| 204 | | - cache->key.offset = length; |
|---|
| 205 | | - cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
|---|
| 219 | + cache->start = 0; |
|---|
| 220 | + cache->length = length; |
|---|
| 206 | 221 | cache->full_stripe_len = fs_info->sectorsize; |
|---|
| 207 | 222 | cache->fs_info = fs_info; |
|---|
| 208 | 223 | |
|---|
| .. | .. |
|---|
| 215 | 230 | return cache; |
|---|
| 216 | 231 | } |
|---|
| 217 | 232 | |
|---|
| 218 | | -void btrfs_free_dummy_block_group(struct btrfs_block_group_cache *cache) |
|---|
| 233 | +void btrfs_free_dummy_block_group(struct btrfs_block_group *cache) |
|---|
| 219 | 234 | { |
|---|
| 220 | 235 | if (!cache) |
|---|
| 221 | 236 | return; |
|---|