xref: /openbmc/linux/fs/btrfs/tests/btrfs-tests.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
2294e30feSJosef Bacik /*
3294e30feSJosef Bacik  * Copyright (C) 2013 Fusion IO.  All rights reserved.
4294e30feSJosef Bacik  */
5294e30feSJosef Bacik 
6294e30feSJosef Bacik #include <linux/fs.h>
7294e30feSJosef Bacik #include <linux/mount.h>
8389e22fbSDavid Howells #include <linux/pseudo_fs.h>
9294e30feSJosef Bacik #include <linux/magic.h>
10294e30feSJosef Bacik #include "btrfs-tests.h"
11294e30feSJosef Bacik #include "../ctree.h"
127c55ee0cSOmar Sandoval #include "../free-space-cache.h"
137c55ee0cSOmar Sandoval #include "../free-space-tree.h"
147c55ee0cSOmar Sandoval #include "../transaction.h"
15faa2dbf0SJosef Bacik #include "../volumes.h"
16faa2dbf0SJosef Bacik #include "../disk-io.h"
17faa2dbf0SJosef Bacik #include "../qgroup.h"
18aac0023cSJosef Bacik #include "../block-group.h"
19ec8eb376SJosef Bacik #include "../fs.h"
20294e30feSJosef Bacik 
21294e30feSJosef Bacik static struct vfsmount *test_mnt = NULL;
22294e30feSJosef Bacik 
23703de426SDavid Sterba const char *test_error[] = {
24703de426SDavid Sterba 	[TEST_ALLOC_FS_INFO]	     = "cannot allocate fs_info",
25703de426SDavid Sterba 	[TEST_ALLOC_ROOT]	     = "cannot allocate root",
26703de426SDavid Sterba 	[TEST_ALLOC_EXTENT_BUFFER]   = "cannot extent buffer",
27703de426SDavid Sterba 	[TEST_ALLOC_PATH]	     = "cannot allocate path",
28703de426SDavid Sterba 	[TEST_ALLOC_INODE]	     = "cannot allocate inode",
29703de426SDavid Sterba 	[TEST_ALLOC_BLOCK_GROUP]     = "cannot allocate block group",
30703de426SDavid Sterba 	[TEST_ALLOC_EXTENT_MAP]      = "cannot allocate extent map",
31703de426SDavid Sterba };
32703de426SDavid Sterba 
33aaedb55bSJosef Bacik static const struct super_operations btrfs_test_super_ops = {
34aaedb55bSJosef Bacik 	.alloc_inode	= btrfs_alloc_inode,
35aaedb55bSJosef Bacik 	.destroy_inode	= btrfs_test_destroy_inode,
36aaedb55bSJosef Bacik };
37aaedb55bSJosef Bacik 
38389e22fbSDavid Howells 
btrfs_test_init_fs_context(struct fs_context * fc)39389e22fbSDavid Howells static int btrfs_test_init_fs_context(struct fs_context *fc)
40294e30feSJosef Bacik {
41389e22fbSDavid Howells 	struct pseudo_fs_context *ctx = init_pseudo(fc, BTRFS_TEST_MAGIC);
42389e22fbSDavid Howells 	if (!ctx)
43389e22fbSDavid Howells 		return -ENOMEM;
44389e22fbSDavid Howells 	ctx->ops = &btrfs_test_super_ops;
45389e22fbSDavid Howells 	return 0;
46294e30feSJosef Bacik }
47294e30feSJosef Bacik 
48294e30feSJosef Bacik static struct file_system_type test_type = {
49294e30feSJosef Bacik 	.name		= "btrfs_test_fs",
50389e22fbSDavid Howells 	.init_fs_context = btrfs_test_init_fs_context,
51294e30feSJosef Bacik 	.kill_sb	= kill_anon_super,
52294e30feSJosef Bacik };
53294e30feSJosef Bacik 
btrfs_new_test_inode(void)54294e30feSJosef Bacik struct inode *btrfs_new_test_inode(void)
55294e30feSJosef Bacik {
569f7fec0bSFilipe Manana 	struct inode *inode;
579f7fec0bSFilipe Manana 
589f7fec0bSFilipe Manana 	inode = new_inode(test_mnt->mnt_sb);
59675a4fc8SJosef Bacik 	if (!inode)
60675a4fc8SJosef Bacik 		return NULL;
61675a4fc8SJosef Bacik 
62675a4fc8SJosef Bacik 	inode->i_mode = S_IFREG;
63cf2404a9SFilipe Manana 	inode->i_ino = BTRFS_FIRST_FREE_OBJECTID;
64675a4fc8SJosef Bacik 	BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
65675a4fc8SJosef Bacik 	BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
66675a4fc8SJosef Bacik 	BTRFS_I(inode)->location.offset = 0;
67*f2d40141SChristian Brauner 	inode_init_owner(&nop_mnt_idmap, inode, NULL, S_IFREG);
689f7fec0bSFilipe Manana 
699f7fec0bSFilipe Manana 	return inode;
70294e30feSJosef Bacik }
71294e30feSJosef Bacik 
btrfs_init_test_fs(void)728632daaeSJeff Mahoney static int btrfs_init_test_fs(void)
73294e30feSJosef Bacik {
74294e30feSJosef Bacik 	int ret;
75294e30feSJosef Bacik 
76294e30feSJosef Bacik 	ret = register_filesystem(&test_type);
77294e30feSJosef Bacik 	if (ret) {
78294e30feSJosef Bacik 		printk(KERN_ERR "btrfs: cannot register test file system\n");
79294e30feSJosef Bacik 		return ret;
80294e30feSJosef Bacik 	}
81294e30feSJosef Bacik 
82294e30feSJosef Bacik 	test_mnt = kern_mount(&test_type);
83294e30feSJosef Bacik 	if (IS_ERR(test_mnt)) {
84294e30feSJosef Bacik 		printk(KERN_ERR "btrfs: cannot mount test file system\n");
85294e30feSJosef Bacik 		unregister_filesystem(&test_type);
8604e1b65aSWei Yongjun 		return PTR_ERR(test_mnt);
87294e30feSJosef Bacik 	}
88294e30feSJosef Bacik 	return 0;
89294e30feSJosef Bacik }
90294e30feSJosef Bacik 
btrfs_destroy_test_fs(void)918632daaeSJeff Mahoney static void btrfs_destroy_test_fs(void)
92294e30feSJosef Bacik {
93294e30feSJosef Bacik 	kern_unmount(test_mnt);
94294e30feSJosef Bacik 	unregister_filesystem(&test_type);
95294e30feSJosef Bacik }
96faa2dbf0SJosef Bacik 
btrfs_alloc_dummy_device(struct btrfs_fs_info * fs_info)97b3ad2c17SNikolay Borisov struct btrfs_device *btrfs_alloc_dummy_device(struct btrfs_fs_info *fs_info)
98b3ad2c17SNikolay Borisov {
99b3ad2c17SNikolay Borisov 	struct btrfs_device *dev;
100b3ad2c17SNikolay Borisov 
101b3ad2c17SNikolay Borisov 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
102b3ad2c17SNikolay Borisov 	if (!dev)
103b3ad2c17SNikolay Borisov 		return ERR_PTR(-ENOMEM);
104b3ad2c17SNikolay Borisov 
10535da5a7eSDavid Sterba 	extent_io_tree_init(NULL, &dev->alloc_state, 0);
106b3ad2c17SNikolay Borisov 	INIT_LIST_HEAD(&dev->dev_list);
107b3ad2c17SNikolay Borisov 	list_add(&dev->dev_list, &fs_info->fs_devices->devices);
108b3ad2c17SNikolay Borisov 
109b3ad2c17SNikolay Borisov 	return dev;
110b3ad2c17SNikolay Borisov }
111b3ad2c17SNikolay Borisov 
btrfs_free_dummy_device(struct btrfs_device * dev)112b3ad2c17SNikolay Borisov static void btrfs_free_dummy_device(struct btrfs_device *dev)
113b3ad2c17SNikolay Borisov {
114b3ad2c17SNikolay Borisov 	extent_io_tree_release(&dev->alloc_state);
115b3ad2c17SNikolay Borisov 	kfree(dev);
116b3ad2c17SNikolay Borisov }
117b3ad2c17SNikolay Borisov 
btrfs_alloc_dummy_fs_info(u32 nodesize,u32 sectorsize)118da17066cSJeff Mahoney struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize)
119faa2dbf0SJosef Bacik {
120faa2dbf0SJosef Bacik 	struct btrfs_fs_info *fs_info = kzalloc(sizeof(struct btrfs_fs_info),
1218cce83baSDavid Sterba 						GFP_KERNEL);
122faa2dbf0SJosef Bacik 
123faa2dbf0SJosef Bacik 	if (!fs_info)
124faa2dbf0SJosef Bacik 		return fs_info;
125faa2dbf0SJosef Bacik 	fs_info->fs_devices = kzalloc(sizeof(struct btrfs_fs_devices),
1268cce83baSDavid Sterba 				      GFP_KERNEL);
127faa2dbf0SJosef Bacik 	if (!fs_info->fs_devices) {
128faa2dbf0SJosef Bacik 		kfree(fs_info);
129faa2dbf0SJosef Bacik 		return NULL;
130faa2dbf0SJosef Bacik 	}
1318260edbaSJosef Bacik 	INIT_LIST_HEAD(&fs_info->fs_devices->devices);
1328260edbaSJosef Bacik 
133faa2dbf0SJosef Bacik 	fs_info->super_copy = kzalloc(sizeof(struct btrfs_super_block),
1348cce83baSDavid Sterba 				      GFP_KERNEL);
135faa2dbf0SJosef Bacik 	if (!fs_info->super_copy) {
136faa2dbf0SJosef Bacik 		kfree(fs_info->fs_devices);
137faa2dbf0SJosef Bacik 		kfree(fs_info);
138faa2dbf0SJosef Bacik 		return NULL;
139faa2dbf0SJosef Bacik 	}
140faa2dbf0SJosef Bacik 
1418260edbaSJosef Bacik 	btrfs_init_fs_info(fs_info);
1428260edbaSJosef Bacik 
143da17066cSJeff Mahoney 	fs_info->nodesize = nodesize;
144da17066cSJeff Mahoney 	fs_info->sectorsize = sectorsize;
145ab108d99SDavid Sterba 	fs_info->sectorsize_bits = ilog2(sectorsize);
1467c0260eeSJeff Mahoney 	set_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
1477c0260eeSJeff Mahoney 
1487c0260eeSJeff Mahoney 	test_mnt->mnt_sb->s_fs_info = fs_info;
1497c0260eeSJeff Mahoney 
150faa2dbf0SJosef Bacik 	return fs_info;
151faa2dbf0SJosef Bacik }
152faa2dbf0SJosef Bacik 
btrfs_free_dummy_fs_info(struct btrfs_fs_info * fs_info)1537c0260eeSJeff Mahoney void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info)
154faa2dbf0SJosef Bacik {
15501cd3909SDavid Sterba 	struct radix_tree_iter iter;
15601cd3909SDavid Sterba 	void **slot;
157b3ad2c17SNikolay Borisov 	struct btrfs_device *dev, *tmp;
158faa2dbf0SJosef Bacik 
1597c0260eeSJeff Mahoney 	if (!fs_info)
1607c0260eeSJeff Mahoney 		return;
1617c0260eeSJeff Mahoney 
1627c0260eeSJeff Mahoney 	if (WARN_ON(!test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO,
1637c0260eeSJeff Mahoney 			      &fs_info->fs_state)))
1647c0260eeSJeff Mahoney 		return;
1657c0260eeSJeff Mahoney 
1667c0260eeSJeff Mahoney 	test_mnt->mnt_sb->s_fs_info = NULL;
1677c0260eeSJeff Mahoney 
16801cd3909SDavid Sterba 	spin_lock(&fs_info->buffer_lock);
16901cd3909SDavid Sterba 	radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter, 0) {
17001cd3909SDavid Sterba 		struct extent_buffer *eb;
17101cd3909SDavid Sterba 
17201cd3909SDavid Sterba 		eb = radix_tree_deref_slot_protected(slot, &fs_info->buffer_lock);
17301cd3909SDavid Sterba 		if (!eb)
17401cd3909SDavid Sterba 			continue;
17501cd3909SDavid Sterba 		/* Shouldn't happen but that kind of thinking creates CVE's */
17601cd3909SDavid Sterba 		if (radix_tree_exception(eb)) {
17701cd3909SDavid Sterba 			if (radix_tree_deref_retry(eb))
17801cd3909SDavid Sterba 				slot = radix_tree_iter_retry(&iter);
17901cd3909SDavid Sterba 			continue;
180faa2dbf0SJosef Bacik 		}
18101cd3909SDavid Sterba 		slot = radix_tree_iter_resume(slot, &iter);
18201cd3909SDavid Sterba 		spin_unlock(&fs_info->buffer_lock);
18301cd3909SDavid Sterba 		free_extent_buffer_stale(eb);
18401cd3909SDavid Sterba 		spin_lock(&fs_info->buffer_lock);
18501cd3909SDavid Sterba 	}
18601cd3909SDavid Sterba 	spin_unlock(&fs_info->buffer_lock);
187faa2dbf0SJosef Bacik 
188b3ad2c17SNikolay Borisov 	btrfs_mapping_tree_free(&fs_info->mapping_tree);
189b3ad2c17SNikolay Borisov 	list_for_each_entry_safe(dev, tmp, &fs_info->fs_devices->devices,
190b3ad2c17SNikolay Borisov 				 dev_list) {
191b3ad2c17SNikolay Borisov 		btrfs_free_dummy_device(dev);
192b3ad2c17SNikolay Borisov 	}
193faa2dbf0SJosef Bacik 	btrfs_free_qgroup_config(fs_info);
194faa2dbf0SJosef Bacik 	btrfs_free_fs_roots(fs_info);
195faa2dbf0SJosef Bacik 	kfree(fs_info->super_copy);
196bd647ce3SJosef Bacik 	btrfs_check_leaked_roots(fs_info);
1978c38938cSJosef Bacik 	btrfs_extent_buffer_leak_debug_check(fs_info);
198faa2dbf0SJosef Bacik 	kfree(fs_info->fs_devices);
199faa2dbf0SJosef Bacik 	kfree(fs_info);
200faa2dbf0SJosef Bacik }
201faa2dbf0SJosef Bacik 
btrfs_free_dummy_root(struct btrfs_root * root)202faa2dbf0SJosef Bacik void btrfs_free_dummy_root(struct btrfs_root *root)
203faa2dbf0SJosef Bacik {
2049b2f2034SZhang Xiaoxu 	if (IS_ERR_OR_NULL(root))
205faa2dbf0SJosef Bacik 		return;
2067c0260eeSJeff Mahoney 	/* Will be freed by btrfs_free_fs_roots */
207fc7cbcd4SDavid Sterba 	if (WARN_ON(test_bit(BTRFS_ROOT_IN_RADIX, &root->state)))
2087c0260eeSJeff Mahoney 		return;
209abed4aaaSJosef Bacik 	btrfs_global_root_delete(root);
21000246528SJosef Bacik 	btrfs_put_root(root);
211faa2dbf0SJosef Bacik }
212faa2dbf0SJosef Bacik 
21332da5386SDavid Sterba struct btrfs_block_group *
btrfs_alloc_dummy_block_group(struct btrfs_fs_info * fs_info,unsigned long length)214da17066cSJeff Mahoney btrfs_alloc_dummy_block_group(struct btrfs_fs_info *fs_info,
215da17066cSJeff Mahoney 			      unsigned long length)
2167c55ee0cSOmar Sandoval {
21732da5386SDavid Sterba 	struct btrfs_block_group *cache;
2187c55ee0cSOmar Sandoval 
2198cce83baSDavid Sterba 	cache = kzalloc(sizeof(*cache), GFP_KERNEL);
2207c55ee0cSOmar Sandoval 	if (!cache)
2217c55ee0cSOmar Sandoval 		return NULL;
2227c55ee0cSOmar Sandoval 	cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
2238cce83baSDavid Sterba 					GFP_KERNEL);
2247c55ee0cSOmar Sandoval 	if (!cache->free_space_ctl) {
2257c55ee0cSOmar Sandoval 		kfree(cache);
2267c55ee0cSOmar Sandoval 		return NULL;
2277c55ee0cSOmar Sandoval 	}
2287c55ee0cSOmar Sandoval 
229b3470b5dSDavid Sterba 	cache->start = 0;
230b3470b5dSDavid Sterba 	cache->length = length;
231da17066cSJeff Mahoney 	cache->full_stripe_len = fs_info->sectorsize;
232da17066cSJeff Mahoney 	cache->fs_info = fs_info;
2337c55ee0cSOmar Sandoval 
2347c55ee0cSOmar Sandoval 	INIT_LIST_HEAD(&cache->list);
2357c55ee0cSOmar Sandoval 	INIT_LIST_HEAD(&cache->cluster_list);
2367c55ee0cSOmar Sandoval 	INIT_LIST_HEAD(&cache->bg_list);
237cd79909bSJosef Bacik 	btrfs_init_free_space_ctl(cache, cache->free_space_ctl);
2387c55ee0cSOmar Sandoval 	mutex_init(&cache->free_space_lock);
2397c55ee0cSOmar Sandoval 
2407c55ee0cSOmar Sandoval 	return cache;
2417c55ee0cSOmar Sandoval }
2427c55ee0cSOmar Sandoval 
btrfs_free_dummy_block_group(struct btrfs_block_group * cache)24332da5386SDavid Sterba void btrfs_free_dummy_block_group(struct btrfs_block_group *cache)
2447c55ee0cSOmar Sandoval {
2457c55ee0cSOmar Sandoval 	if (!cache)
2467c55ee0cSOmar Sandoval 		return;
247fc80f7acSJosef Bacik 	btrfs_remove_free_space_cache(cache);
2487c55ee0cSOmar Sandoval 	kfree(cache->free_space_ctl);
2497c55ee0cSOmar Sandoval 	kfree(cache);
2507c55ee0cSOmar Sandoval }
2517c55ee0cSOmar Sandoval 
btrfs_init_dummy_trans(struct btrfs_trans_handle * trans,struct btrfs_fs_info * fs_info)252483bce06SNikolay Borisov void btrfs_init_dummy_trans(struct btrfs_trans_handle *trans,
253483bce06SNikolay Borisov 			    struct btrfs_fs_info *fs_info)
2547c55ee0cSOmar Sandoval {
2557c55ee0cSOmar Sandoval 	memset(trans, 0, sizeof(*trans));
2567c55ee0cSOmar Sandoval 	trans->transid = 1;
2577c55ee0cSOmar Sandoval 	trans->type = __TRANS_DUMMY;
258483bce06SNikolay Borisov 	trans->fs_info = fs_info;
2597c55ee0cSOmar Sandoval }
2608632daaeSJeff Mahoney 
btrfs_run_sanity_tests(void)2618632daaeSJeff Mahoney int btrfs_run_sanity_tests(void)
2628632daaeSJeff Mahoney {
2638632daaeSJeff Mahoney 	int ret, i;
2648632daaeSJeff Mahoney 	u32 sectorsize, nodesize;
2658632daaeSJeff Mahoney 	u32 test_sectorsize[] = {
2668632daaeSJeff Mahoney 		PAGE_SIZE,
2678632daaeSJeff Mahoney 	};
2688632daaeSJeff Mahoney 	ret = btrfs_init_test_fs();
2698632daaeSJeff Mahoney 	if (ret)
2708632daaeSJeff Mahoney 		return ret;
2718632daaeSJeff Mahoney 	for (i = 0; i < ARRAY_SIZE(test_sectorsize); i++) {
2728632daaeSJeff Mahoney 		sectorsize = test_sectorsize[i];
2738632daaeSJeff Mahoney 		for (nodesize = sectorsize;
2748632daaeSJeff Mahoney 		     nodesize <= BTRFS_MAX_METADATA_BLOCKSIZE;
2758632daaeSJeff Mahoney 		     nodesize <<= 1) {
2768632daaeSJeff Mahoney 			pr_info("BTRFS: selftest: sectorsize: %u  nodesize: %u\n",
2778632daaeSJeff Mahoney 				sectorsize, nodesize);
2788632daaeSJeff Mahoney 			ret = btrfs_test_free_space_cache(sectorsize, nodesize);
2798632daaeSJeff Mahoney 			if (ret)
2808632daaeSJeff Mahoney 				goto out;
2818632daaeSJeff Mahoney 			ret = btrfs_test_extent_buffer_operations(sectorsize,
2828632daaeSJeff Mahoney 				nodesize);
2838632daaeSJeff Mahoney 			if (ret)
2848632daaeSJeff Mahoney 				goto out;
2858632daaeSJeff Mahoney 			ret = btrfs_test_extent_io(sectorsize, nodesize);
2868632daaeSJeff Mahoney 			if (ret)
2878632daaeSJeff Mahoney 				goto out;
2888632daaeSJeff Mahoney 			ret = btrfs_test_inodes(sectorsize, nodesize);
2898632daaeSJeff Mahoney 			if (ret)
2908632daaeSJeff Mahoney 				goto out;
2918632daaeSJeff Mahoney 			ret = btrfs_test_qgroups(sectorsize, nodesize);
2928632daaeSJeff Mahoney 			if (ret)
2938632daaeSJeff Mahoney 				goto out;
2948632daaeSJeff Mahoney 			ret = btrfs_test_free_space_tree(sectorsize, nodesize);
2958632daaeSJeff Mahoney 			if (ret)
2968632daaeSJeff Mahoney 				goto out;
2978632daaeSJeff Mahoney 		}
2988632daaeSJeff Mahoney 	}
29972b28077SLiu Bo 	ret = btrfs_test_extent_map();
3007a61f880SColin Ian King 
3018632daaeSJeff Mahoney out:
3028632daaeSJeff Mahoney 	btrfs_destroy_test_fs();
3038632daaeSJeff Mahoney 	return ret;
3048632daaeSJeff Mahoney }
305