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/magic.h>
9 #include "btrfs-tests.h"
10 #include "../ctree.h"
11 #include "../free-space-cache.h"
12 #include "../free-space-tree.h"
13 #include "../transaction.h"
14 #include "../volumes.h"
15 #include "../disk-io.h"
16 #include "../qgroup.h"
17 
18 static struct vfsmount *test_mnt = NULL;
19 
20 static const struct super_operations btrfs_test_super_ops = {
21 	.alloc_inode	= btrfs_alloc_inode,
22 	.destroy_inode	= btrfs_test_destroy_inode,
23 };
24 
btrfs_test_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)25 static struct dentry *btrfs_test_mount(struct file_system_type *fs_type,
26 				       int flags, const char *dev_name,
27 				       void *data)
28 {
29 	return mount_pseudo(fs_type, "btrfs_test:", &btrfs_test_super_ops,
30 			    NULL, BTRFS_TEST_MAGIC);
31 }
32 
33 static struct file_system_type test_type = {
34 	.name		= "btrfs_test_fs",
35 	.mount		= btrfs_test_mount,
36 	.kill_sb	= kill_anon_super,
37 };
38 
btrfs_new_test_inode(void)39 struct inode *btrfs_new_test_inode(void)
40 {
41 	struct inode *inode;
42 
43 	inode = new_inode(test_mnt->mnt_sb);
44 	if (inode)
45 		inode_init_owner(inode, NULL, S_IFREG);
46 
47 	return inode;
48 }
49 
btrfs_init_test_fs(void)50 static int btrfs_init_test_fs(void)
51 {
52 	int ret;
53 
54 	ret = register_filesystem(&test_type);
55 	if (ret) {
56 		printk(KERN_ERR "btrfs: cannot register test file system\n");
57 		return ret;
58 	}
59 
60 	test_mnt = kern_mount(&test_type);
61 	if (IS_ERR(test_mnt)) {
62 		printk(KERN_ERR "btrfs: cannot mount test file system\n");
63 		unregister_filesystem(&test_type);
64 		return PTR_ERR(test_mnt);
65 	}
66 	return 0;
67 }
68 
btrfs_destroy_test_fs(void)69 static void btrfs_destroy_test_fs(void)
70 {
71 	kern_unmount(test_mnt);
72 	unregister_filesystem(&test_type);
73 }
74 
btrfs_alloc_dummy_fs_info(u32 nodesize,u32 sectorsize)75 struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize)
76 {
77 	struct btrfs_fs_info *fs_info = kzalloc(sizeof(struct btrfs_fs_info),
78 						GFP_KERNEL);
79 
80 	if (!fs_info)
81 		return fs_info;
82 	fs_info->fs_devices = kzalloc(sizeof(struct btrfs_fs_devices),
83 				      GFP_KERNEL);
84 	if (!fs_info->fs_devices) {
85 		kfree(fs_info);
86 		return NULL;
87 	}
88 	fs_info->super_copy = kzalloc(sizeof(struct btrfs_super_block),
89 				      GFP_KERNEL);
90 	if (!fs_info->super_copy) {
91 		kfree(fs_info->fs_devices);
92 		kfree(fs_info);
93 		return NULL;
94 	}
95 
96 	fs_info->nodesize = nodesize;
97 	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 	set_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
127 
128 	test_mnt->mnt_sb->s_fs_info = fs_info;
129 
130 	return fs_info;
131 }
132 
btrfs_free_dummy_fs_info(struct btrfs_fs_info * fs_info)133 void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info)
134 {
135 	struct radix_tree_iter iter;
136 	void **slot;
137 
138 	if (!fs_info)
139 		return;
140 
141 	if (WARN_ON(!test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO,
142 			      &fs_info->fs_state)))
143 		return;
144 
145 	test_mnt->mnt_sb->s_fs_info = NULL;
146 
147 	spin_lock(&fs_info->buffer_lock);
148 	radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter, 0) {
149 		struct extent_buffer *eb;
150 
151 		eb = radix_tree_deref_slot_protected(slot, &fs_info->buffer_lock);
152 		if (!eb)
153 			continue;
154 		/* Shouldn't happen but that kind of thinking creates CVE's */
155 		if (radix_tree_exception(eb)) {
156 			if (radix_tree_deref_retry(eb))
157 				slot = radix_tree_iter_retry(&iter);
158 			continue;
159 		}
160 		slot = radix_tree_iter_resume(slot, &iter);
161 		spin_unlock(&fs_info->buffer_lock);
162 		free_extent_buffer_stale(eb);
163 		spin_lock(&fs_info->buffer_lock);
164 	}
165 	spin_unlock(&fs_info->buffer_lock);
166 
167 	btrfs_free_qgroup_config(fs_info);
168 	btrfs_free_fs_roots(fs_info);
169 	cleanup_srcu_struct(&fs_info->subvol_srcu);
170 	kfree(fs_info->super_copy);
171 	kfree(fs_info->fs_devices);
172 	kfree(fs_info);
173 }
174 
btrfs_free_dummy_root(struct btrfs_root * root)175 void btrfs_free_dummy_root(struct btrfs_root *root)
176 {
177 	if (IS_ERR_OR_NULL(root))
178 		return;
179 	/* Will be freed by btrfs_free_fs_roots */
180 	if (WARN_ON(test_bit(BTRFS_ROOT_IN_RADIX, &root->state)))
181 		return;
182 	if (root->node)
183 		free_extent_buffer(root->node);
184 	kfree(root);
185 }
186 
187 struct btrfs_block_group_cache *
btrfs_alloc_dummy_block_group(struct btrfs_fs_info * fs_info,unsigned long length)188 btrfs_alloc_dummy_block_group(struct btrfs_fs_info *fs_info,
189 			      unsigned long length)
190 {
191 	struct btrfs_block_group_cache *cache;
192 
193 	cache = kzalloc(sizeof(*cache), GFP_KERNEL);
194 	if (!cache)
195 		return NULL;
196 	cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
197 					GFP_KERNEL);
198 	if (!cache->free_space_ctl) {
199 		kfree(cache);
200 		return NULL;
201 	}
202 
203 	cache->key.objectid = 0;
204 	cache->key.offset = length;
205 	cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
206 	cache->full_stripe_len = fs_info->sectorsize;
207 	cache->fs_info = fs_info;
208 
209 	INIT_LIST_HEAD(&cache->list);
210 	INIT_LIST_HEAD(&cache->cluster_list);
211 	INIT_LIST_HEAD(&cache->bg_list);
212 	btrfs_init_free_space_ctl(cache);
213 	mutex_init(&cache->free_space_lock);
214 
215 	return cache;
216 }
217 
btrfs_free_dummy_block_group(struct btrfs_block_group_cache * cache)218 void btrfs_free_dummy_block_group(struct btrfs_block_group_cache *cache)
219 {
220 	if (!cache)
221 		return;
222 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
223 	kfree(cache->free_space_ctl);
224 	kfree(cache);
225 }
226 
btrfs_init_dummy_trans(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info)227 void btrfs_init_dummy_trans(struct btrfs_trans_handle *trans,
228 			    struct btrfs_fs_info *fs_info)
229 {
230 	memset(trans, 0, sizeof(*trans));
231 	trans->transid = 1;
232 	trans->type = __TRANS_DUMMY;
233 	trans->fs_info = fs_info;
234 }
235 
btrfs_run_sanity_tests(void)236 int btrfs_run_sanity_tests(void)
237 {
238 	int ret, i;
239 	u32 sectorsize, nodesize;
240 	u32 test_sectorsize[] = {
241 		PAGE_SIZE,
242 	};
243 	ret = btrfs_init_test_fs();
244 	if (ret)
245 		return ret;
246 	for (i = 0; i < ARRAY_SIZE(test_sectorsize); i++) {
247 		sectorsize = test_sectorsize[i];
248 		for (nodesize = sectorsize;
249 		     nodesize <= BTRFS_MAX_METADATA_BLOCKSIZE;
250 		     nodesize <<= 1) {
251 			pr_info("BTRFS: selftest: sectorsize: %u  nodesize: %u\n",
252 				sectorsize, nodesize);
253 			ret = btrfs_test_free_space_cache(sectorsize, nodesize);
254 			if (ret)
255 				goto out;
256 			ret = btrfs_test_extent_buffer_operations(sectorsize,
257 				nodesize);
258 			if (ret)
259 				goto out;
260 			ret = btrfs_test_extent_io(sectorsize, nodesize);
261 			if (ret)
262 				goto out;
263 			ret = btrfs_test_inodes(sectorsize, nodesize);
264 			if (ret)
265 				goto out;
266 			ret = btrfs_test_qgroups(sectorsize, nodesize);
267 			if (ret)
268 				goto out;
269 			ret = btrfs_test_free_space_tree(sectorsize, nodesize);
270 			if (ret)
271 				goto out;
272 		}
273 	}
274 	ret = btrfs_test_extent_map();
275 
276 out:
277 	btrfs_destroy_test_fs();
278 	return ret;
279 }
280