hc
2024-09-20 cf4ce59b3b70238352c7f1729f0f7223214828ad
kernel/fs/btrfs/tests/btrfs-tests.c
....@@ -5,6 +5,7 @@
55
66 #include <linux/fs.h>
77 #include <linux/mount.h>
8
+#include <linux/pseudo_fs.h>
89 #include <linux/magic.h>
910 #include "btrfs-tests.h"
1011 #include "../ctree.h"
....@@ -14,25 +15,38 @@
1415 #include "../volumes.h"
1516 #include "../disk-io.h"
1617 #include "../qgroup.h"
18
+#include "../block-group.h"
1719
1820 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
+};
1931
2032 static const struct super_operations btrfs_test_super_ops = {
2133 .alloc_inode = btrfs_alloc_inode,
2234 .destroy_inode = btrfs_test_destroy_inode,
2335 };
2436
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)
2839 {
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;
3145 }
3246
3347 static struct file_system_type test_type = {
3448 .name = "btrfs_test_fs",
35
- .mount = btrfs_test_mount,
49
+ .init_fs_context = btrfs_test_init_fs_context,
3650 .kill_sb = kill_anon_super,
3751 };
3852
....@@ -72,6 +86,27 @@
7286 unregister_filesystem(&test_type);
7387 }
7488
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
+
75110 struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize)
76111 {
77112 struct btrfs_fs_info *fs_info = kzalloc(sizeof(struct btrfs_fs_info),
....@@ -85,6 +120,8 @@
85120 kfree(fs_info);
86121 return NULL;
87122 }
123
+ INIT_LIST_HEAD(&fs_info->fs_devices->devices);
124
+
88125 fs_info->super_copy = kzalloc(sizeof(struct btrfs_super_block),
89126 GFP_KERNEL);
90127 if (!fs_info->super_copy) {
....@@ -93,36 +130,10 @@
93130 return NULL;
94131 }
95132
133
+ btrfs_init_fs_info(fs_info);
134
+
96135 fs_info->nodesize = nodesize;
97136 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];
126137 set_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
127138
128139 test_mnt->mnt_sb->s_fs_info = fs_info;
....@@ -134,6 +145,7 @@
134145 {
135146 struct radix_tree_iter iter;
136147 void **slot;
148
+ struct btrfs_device *dev, *tmp;
137149
138150 if (!fs_info)
139151 return;
....@@ -164,31 +176,35 @@
164176 }
165177 spin_unlock(&fs_info->buffer_lock);
166178
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
+ }
167184 btrfs_free_qgroup_config(fs_info);
168185 btrfs_free_fs_roots(fs_info);
169
- cleanup_srcu_struct(&fs_info->subvol_srcu);
170186 kfree(fs_info->super_copy);
187
+ btrfs_check_leaked_roots(fs_info);
188
+ btrfs_extent_buffer_leak_debug_check(fs_info);
171189 kfree(fs_info->fs_devices);
172190 kfree(fs_info);
173191 }
174192
175193 void btrfs_free_dummy_root(struct btrfs_root *root)
176194 {
177
- if (!root)
195
+ if (IS_ERR_OR_NULL(root))
178196 return;
179197 /* Will be freed by btrfs_free_fs_roots */
180198 if (WARN_ON(test_bit(BTRFS_ROOT_IN_RADIX, &root->state)))
181199 return;
182
- if (root->node)
183
- free_extent_buffer(root->node);
184
- kfree(root);
200
+ btrfs_put_root(root);
185201 }
186202
187
-struct btrfs_block_group_cache *
203
+struct btrfs_block_group *
188204 btrfs_alloc_dummy_block_group(struct btrfs_fs_info *fs_info,
189205 unsigned long length)
190206 {
191
- struct btrfs_block_group_cache *cache;
207
+ struct btrfs_block_group *cache;
192208
193209 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
194210 if (!cache)
....@@ -200,9 +216,8 @@
200216 return NULL;
201217 }
202218
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;
206221 cache->full_stripe_len = fs_info->sectorsize;
207222 cache->fs_info = fs_info;
208223
....@@ -215,7 +230,7 @@
215230 return cache;
216231 }
217232
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)
219234 {
220235 if (!cache)
221236 return;