1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2013 Fusion IO. All rights reserved.
4 */
5
6 #include <linux/fs.h>
7 #include <linux/mount.h>
8 #include <linux/pseudo_fs.h>
9 #include <linux/magic.h>
10 #include "btrfs-tests.h"
11 #include "../ctree.h"
12 #include "../free-space-cache.h"
13 #include "../free-space-tree.h"
14 #include "../transaction.h"
15 #include "../volumes.h"
16 #include "../disk-io.h"
17 #include "../qgroup.h"
18 #include "../block-group.h"
19
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 };
31
32 static const struct super_operations btrfs_test_super_ops = {
33 .alloc_inode = btrfs_alloc_inode,
34 .destroy_inode = btrfs_test_destroy_inode,
35 };
36
37
btrfs_test_init_fs_context(struct fs_context * fc)38 static int btrfs_test_init_fs_context(struct fs_context *fc)
39 {
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;
45 }
46
47 static struct file_system_type test_type = {
48 .name = "btrfs_test_fs",
49 .init_fs_context = btrfs_test_init_fs_context,
50 .kill_sb = kill_anon_super,
51 };
52
btrfs_new_test_inode(void)53 struct inode *btrfs_new_test_inode(void)
54 {
55 struct inode *inode;
56
57 inode = new_inode(test_mnt->mnt_sb);
58 if (!inode)
59 return NULL;
60
61 inode->i_mode = S_IFREG;
62 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
63 BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
64 BTRFS_I(inode)->location.offset = 0;
65 inode_init_owner(&init_user_ns, inode, NULL, S_IFREG);
66
67 return inode;
68 }
69
btrfs_init_test_fs(void)70 static int btrfs_init_test_fs(void)
71 {
72 int ret;
73
74 ret = register_filesystem(&test_type);
75 if (ret) {
76 printk(KERN_ERR "btrfs: cannot register test file system\n");
77 return ret;
78 }
79
80 test_mnt = kern_mount(&test_type);
81 if (IS_ERR(test_mnt)) {
82 printk(KERN_ERR "btrfs: cannot mount test file system\n");
83 unregister_filesystem(&test_type);
84 return PTR_ERR(test_mnt);
85 }
86 return 0;
87 }
88
btrfs_destroy_test_fs(void)89 static void btrfs_destroy_test_fs(void)
90 {
91 kern_unmount(test_mnt);
92 unregister_filesystem(&test_type);
93 }
94
btrfs_alloc_dummy_device(struct btrfs_fs_info * fs_info)95 struct btrfs_device *btrfs_alloc_dummy_device(struct btrfs_fs_info *fs_info)
96 {
97 struct btrfs_device *dev;
98
99 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
100 if (!dev)
101 return ERR_PTR(-ENOMEM);
102
103 extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL);
104 INIT_LIST_HEAD(&dev->dev_list);
105 list_add(&dev->dev_list, &fs_info->fs_devices->devices);
106
107 return dev;
108 }
109
btrfs_free_dummy_device(struct btrfs_device * dev)110 static void btrfs_free_dummy_device(struct btrfs_device *dev)
111 {
112 extent_io_tree_release(&dev->alloc_state);
113 kfree(dev);
114 }
115
btrfs_alloc_dummy_fs_info(u32 nodesize,u32 sectorsize)116 struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize)
117 {
118 struct btrfs_fs_info *fs_info = kzalloc(sizeof(struct btrfs_fs_info),
119 GFP_KERNEL);
120
121 if (!fs_info)
122 return fs_info;
123 fs_info->fs_devices = kzalloc(sizeof(struct btrfs_fs_devices),
124 GFP_KERNEL);
125 if (!fs_info->fs_devices) {
126 kfree(fs_info);
127 return NULL;
128 }
129 INIT_LIST_HEAD(&fs_info->fs_devices->devices);
130
131 fs_info->super_copy = kzalloc(sizeof(struct btrfs_super_block),
132 GFP_KERNEL);
133 if (!fs_info->super_copy) {
134 kfree(fs_info->fs_devices);
135 kfree(fs_info);
136 return NULL;
137 }
138
139 btrfs_init_fs_info(fs_info);
140
141 fs_info->nodesize = nodesize;
142 fs_info->sectorsize = sectorsize;
143 fs_info->sectorsize_bits = ilog2(sectorsize);
144 set_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
145
146 test_mnt->mnt_sb->s_fs_info = fs_info;
147
148 return fs_info;
149 }
150
btrfs_free_dummy_fs_info(struct btrfs_fs_info * fs_info)151 void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info)
152 {
153 struct radix_tree_iter iter;
154 void **slot;
155 struct btrfs_device *dev, *tmp;
156
157 if (!fs_info)
158 return;
159
160 if (WARN_ON(!test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO,
161 &fs_info->fs_state)))
162 return;
163
164 test_mnt->mnt_sb->s_fs_info = NULL;
165
166 spin_lock(&fs_info->buffer_lock);
167 radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter, 0) {
168 struct extent_buffer *eb;
169
170 eb = radix_tree_deref_slot_protected(slot, &fs_info->buffer_lock);
171 if (!eb)
172 continue;
173 /* Shouldn't happen but that kind of thinking creates CVE's */
174 if (radix_tree_exception(eb)) {
175 if (radix_tree_deref_retry(eb))
176 slot = radix_tree_iter_retry(&iter);
177 continue;
178 }
179 slot = radix_tree_iter_resume(slot, &iter);
180 spin_unlock(&fs_info->buffer_lock);
181 free_extent_buffer_stale(eb);
182 spin_lock(&fs_info->buffer_lock);
183 }
184 spin_unlock(&fs_info->buffer_lock);
185
186 btrfs_mapping_tree_free(&fs_info->mapping_tree);
187 list_for_each_entry_safe(dev, tmp, &fs_info->fs_devices->devices,
188 dev_list) {
189 btrfs_free_dummy_device(dev);
190 }
191 btrfs_free_qgroup_config(fs_info);
192 btrfs_free_fs_roots(fs_info);
193 kfree(fs_info->super_copy);
194 btrfs_check_leaked_roots(fs_info);
195 btrfs_extent_buffer_leak_debug_check(fs_info);
196 kfree(fs_info->fs_devices);
197 kfree(fs_info);
198 }
199
btrfs_free_dummy_root(struct btrfs_root * root)200 void btrfs_free_dummy_root(struct btrfs_root *root)
201 {
202 if (IS_ERR_OR_NULL(root))
203 return;
204 /* Will be freed by btrfs_free_fs_roots */
205 if (WARN_ON(test_bit(BTRFS_ROOT_IN_RADIX, &root->state)))
206 return;
207 btrfs_put_root(root);
208 }
209
210 struct btrfs_block_group *
btrfs_alloc_dummy_block_group(struct btrfs_fs_info * fs_info,unsigned long length)211 btrfs_alloc_dummy_block_group(struct btrfs_fs_info *fs_info,
212 unsigned long length)
213 {
214 struct btrfs_block_group *cache;
215
216 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
217 if (!cache)
218 return NULL;
219 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
220 GFP_KERNEL);
221 if (!cache->free_space_ctl) {
222 kfree(cache);
223 return NULL;
224 }
225
226 cache->start = 0;
227 cache->length = length;
228 cache->full_stripe_len = fs_info->sectorsize;
229 cache->fs_info = fs_info;
230
231 INIT_LIST_HEAD(&cache->list);
232 INIT_LIST_HEAD(&cache->cluster_list);
233 INIT_LIST_HEAD(&cache->bg_list);
234 btrfs_init_free_space_ctl(cache, cache->free_space_ctl);
235 mutex_init(&cache->free_space_lock);
236
237 return cache;
238 }
239
btrfs_free_dummy_block_group(struct btrfs_block_group * cache)240 void btrfs_free_dummy_block_group(struct btrfs_block_group *cache)
241 {
242 if (!cache)
243 return;
244 __btrfs_remove_free_space_cache(cache->free_space_ctl);
245 kfree(cache->free_space_ctl);
246 kfree(cache);
247 }
248
btrfs_init_dummy_trans(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info)249 void btrfs_init_dummy_trans(struct btrfs_trans_handle *trans,
250 struct btrfs_fs_info *fs_info)
251 {
252 memset(trans, 0, sizeof(*trans));
253 trans->transid = 1;
254 trans->type = __TRANS_DUMMY;
255 trans->fs_info = fs_info;
256 }
257
btrfs_run_sanity_tests(void)258 int btrfs_run_sanity_tests(void)
259 {
260 int ret, i;
261 u32 sectorsize, nodesize;
262 u32 test_sectorsize[] = {
263 PAGE_SIZE,
264 };
265 ret = btrfs_init_test_fs();
266 if (ret)
267 return ret;
268 for (i = 0; i < ARRAY_SIZE(test_sectorsize); i++) {
269 sectorsize = test_sectorsize[i];
270 for (nodesize = sectorsize;
271 nodesize <= BTRFS_MAX_METADATA_BLOCKSIZE;
272 nodesize <<= 1) {
273 pr_info("BTRFS: selftest: sectorsize: %u nodesize: %u\n",
274 sectorsize, nodesize);
275 ret = btrfs_test_free_space_cache(sectorsize, nodesize);
276 if (ret)
277 goto out;
278 ret = btrfs_test_extent_buffer_operations(sectorsize,
279 nodesize);
280 if (ret)
281 goto out;
282 ret = btrfs_test_extent_io(sectorsize, nodesize);
283 if (ret)
284 goto out;
285 ret = btrfs_test_inodes(sectorsize, nodesize);
286 if (ret)
287 goto out;
288 ret = btrfs_test_qgroups(sectorsize, nodesize);
289 if (ret)
290 goto out;
291 ret = btrfs_test_free_space_tree(sectorsize, nodesize);
292 if (ret)
293 goto out;
294 }
295 }
296 ret = btrfs_test_extent_map();
297
298 out:
299 btrfs_destroy_test_fs();
300 return ret;
301 }
302