xref: /openbmc/linux/fs/btrfs/tests/btrfs-tests.c (revision 703de4266f63ba1703fd04d4838fefd555dfbf9a)
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>
8294e30feSJosef Bacik #include <linux/magic.h>
9294e30feSJosef Bacik #include "btrfs-tests.h"
10294e30feSJosef Bacik #include "../ctree.h"
117c55ee0cSOmar Sandoval #include "../free-space-cache.h"
127c55ee0cSOmar Sandoval #include "../free-space-tree.h"
137c55ee0cSOmar Sandoval #include "../transaction.h"
14faa2dbf0SJosef Bacik #include "../volumes.h"
15faa2dbf0SJosef Bacik #include "../disk-io.h"
16faa2dbf0SJosef Bacik #include "../qgroup.h"
17294e30feSJosef Bacik 
18294e30feSJosef Bacik static struct vfsmount *test_mnt = NULL;
19294e30feSJosef Bacik 
20*703de426SDavid Sterba const char *test_error[] = {
21*703de426SDavid Sterba 	[TEST_ALLOC_FS_INFO]	     = "cannot allocate fs_info",
22*703de426SDavid Sterba 	[TEST_ALLOC_ROOT]	     = "cannot allocate root",
23*703de426SDavid Sterba 	[TEST_ALLOC_EXTENT_BUFFER]   = "cannot extent buffer",
24*703de426SDavid Sterba 	[TEST_ALLOC_PATH]	     = "cannot allocate path",
25*703de426SDavid Sterba 	[TEST_ALLOC_INODE]	     = "cannot allocate inode",
26*703de426SDavid Sterba 	[TEST_ALLOC_BLOCK_GROUP]     = "cannot allocate block group",
27*703de426SDavid Sterba 	[TEST_ALLOC_EXTENT_MAP]      = "cannot allocate extent map",
28*703de426SDavid Sterba };
29*703de426SDavid Sterba 
30aaedb55bSJosef Bacik static const struct super_operations btrfs_test_super_ops = {
31aaedb55bSJosef Bacik 	.alloc_inode	= btrfs_alloc_inode,
32aaedb55bSJosef Bacik 	.destroy_inode	= btrfs_test_destroy_inode,
33aaedb55bSJosef Bacik };
34aaedb55bSJosef Bacik 
35294e30feSJosef Bacik static struct dentry *btrfs_test_mount(struct file_system_type *fs_type,
36294e30feSJosef Bacik 				       int flags, const char *dev_name,
37294e30feSJosef Bacik 				       void *data)
38294e30feSJosef Bacik {
39aaedb55bSJosef Bacik 	return mount_pseudo(fs_type, "btrfs_test:", &btrfs_test_super_ops,
40aaedb55bSJosef Bacik 			    NULL, BTRFS_TEST_MAGIC);
41294e30feSJosef Bacik }
42294e30feSJosef Bacik 
43294e30feSJosef Bacik static struct file_system_type test_type = {
44294e30feSJosef Bacik 	.name		= "btrfs_test_fs",
45294e30feSJosef Bacik 	.mount		= btrfs_test_mount,
46294e30feSJosef Bacik 	.kill_sb	= kill_anon_super,
47294e30feSJosef Bacik };
48294e30feSJosef Bacik 
49294e30feSJosef Bacik struct inode *btrfs_new_test_inode(void)
50294e30feSJosef Bacik {
51294e30feSJosef Bacik 	return new_inode(test_mnt->mnt_sb);
52294e30feSJosef Bacik }
53294e30feSJosef Bacik 
548632daaeSJeff Mahoney static int btrfs_init_test_fs(void)
55294e30feSJosef Bacik {
56294e30feSJosef Bacik 	int ret;
57294e30feSJosef Bacik 
58294e30feSJosef Bacik 	ret = register_filesystem(&test_type);
59294e30feSJosef Bacik 	if (ret) {
60294e30feSJosef Bacik 		printk(KERN_ERR "btrfs: cannot register test file system\n");
61294e30feSJosef Bacik 		return ret;
62294e30feSJosef Bacik 	}
63294e30feSJosef Bacik 
64294e30feSJosef Bacik 	test_mnt = kern_mount(&test_type);
65294e30feSJosef Bacik 	if (IS_ERR(test_mnt)) {
66294e30feSJosef Bacik 		printk(KERN_ERR "btrfs: cannot mount test file system\n");
67294e30feSJosef Bacik 		unregister_filesystem(&test_type);
6804e1b65aSWei Yongjun 		return PTR_ERR(test_mnt);
69294e30feSJosef Bacik 	}
70294e30feSJosef Bacik 	return 0;
71294e30feSJosef Bacik }
72294e30feSJosef Bacik 
738632daaeSJeff Mahoney static void btrfs_destroy_test_fs(void)
74294e30feSJosef Bacik {
75294e30feSJosef Bacik 	kern_unmount(test_mnt);
76294e30feSJosef Bacik 	unregister_filesystem(&test_type);
77294e30feSJosef Bacik }
78faa2dbf0SJosef Bacik 
79da17066cSJeff Mahoney struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize)
80faa2dbf0SJosef Bacik {
81faa2dbf0SJosef Bacik 	struct btrfs_fs_info *fs_info = kzalloc(sizeof(struct btrfs_fs_info),
828cce83baSDavid Sterba 						GFP_KERNEL);
83faa2dbf0SJosef Bacik 
84faa2dbf0SJosef Bacik 	if (!fs_info)
85faa2dbf0SJosef Bacik 		return fs_info;
86faa2dbf0SJosef Bacik 	fs_info->fs_devices = kzalloc(sizeof(struct btrfs_fs_devices),
878cce83baSDavid Sterba 				      GFP_KERNEL);
88faa2dbf0SJosef Bacik 	if (!fs_info->fs_devices) {
89faa2dbf0SJosef Bacik 		kfree(fs_info);
90faa2dbf0SJosef Bacik 		return NULL;
91faa2dbf0SJosef Bacik 	}
92faa2dbf0SJosef Bacik 	fs_info->super_copy = kzalloc(sizeof(struct btrfs_super_block),
938cce83baSDavid Sterba 				      GFP_KERNEL);
94faa2dbf0SJosef Bacik 	if (!fs_info->super_copy) {
95faa2dbf0SJosef Bacik 		kfree(fs_info->fs_devices);
96faa2dbf0SJosef Bacik 		kfree(fs_info);
97faa2dbf0SJosef Bacik 		return NULL;
98faa2dbf0SJosef Bacik 	}
99faa2dbf0SJosef Bacik 
100da17066cSJeff Mahoney 	fs_info->nodesize = nodesize;
101da17066cSJeff Mahoney 	fs_info->sectorsize = sectorsize;
102da17066cSJeff Mahoney 
103faa2dbf0SJosef Bacik 	if (init_srcu_struct(&fs_info->subvol_srcu)) {
104faa2dbf0SJosef Bacik 		kfree(fs_info->fs_devices);
105faa2dbf0SJosef Bacik 		kfree(fs_info->super_copy);
106faa2dbf0SJosef Bacik 		kfree(fs_info);
107faa2dbf0SJosef Bacik 		return NULL;
108faa2dbf0SJosef Bacik 	}
109faa2dbf0SJosef Bacik 
110faa2dbf0SJosef Bacik 	spin_lock_init(&fs_info->buffer_lock);
111faa2dbf0SJosef Bacik 	spin_lock_init(&fs_info->qgroup_lock);
112faa2dbf0SJosef Bacik 	spin_lock_init(&fs_info->qgroup_op_lock);
113faa2dbf0SJosef Bacik 	spin_lock_init(&fs_info->super_lock);
114faa2dbf0SJosef Bacik 	spin_lock_init(&fs_info->fs_roots_radix_lock);
115faa2dbf0SJosef Bacik 	spin_lock_init(&fs_info->tree_mod_seq_lock);
116faa2dbf0SJosef Bacik 	mutex_init(&fs_info->qgroup_ioctl_lock);
117faa2dbf0SJosef Bacik 	mutex_init(&fs_info->qgroup_rescan_lock);
118faa2dbf0SJosef Bacik 	rwlock_init(&fs_info->tree_mod_log_lock);
119faa2dbf0SJosef Bacik 	fs_info->running_transaction = NULL;
120faa2dbf0SJosef Bacik 	fs_info->qgroup_tree = RB_ROOT;
121faa2dbf0SJosef Bacik 	fs_info->qgroup_ulist = NULL;
122faa2dbf0SJosef Bacik 	atomic64_set(&fs_info->tree_mod_seq, 0);
123faa2dbf0SJosef Bacik 	INIT_LIST_HEAD(&fs_info->dirty_qgroups);
124faa2dbf0SJosef Bacik 	INIT_LIST_HEAD(&fs_info->dead_roots);
125faa2dbf0SJosef Bacik 	INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
126faa2dbf0SJosef Bacik 	INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC);
127faa2dbf0SJosef Bacik 	INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
12843eb5f29SQu Wenruo 	extent_io_tree_init(fs_info, &fs_info->freed_extents[0],
12943eb5f29SQu Wenruo 			    IO_TREE_FS_INFO_FREED_EXTENTS0, NULL);
13043eb5f29SQu Wenruo 	extent_io_tree_init(fs_info, &fs_info->freed_extents[1],
13143eb5f29SQu Wenruo 			    IO_TREE_FS_INFO_FREED_EXTENTS1, NULL);
1327c55ee0cSOmar Sandoval 	fs_info->pinned_extents = &fs_info->freed_extents[0];
1337c0260eeSJeff Mahoney 	set_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
1347c0260eeSJeff Mahoney 
1357c0260eeSJeff Mahoney 	test_mnt->mnt_sb->s_fs_info = fs_info;
1367c0260eeSJeff Mahoney 
137faa2dbf0SJosef Bacik 	return fs_info;
138faa2dbf0SJosef Bacik }
139faa2dbf0SJosef Bacik 
1407c0260eeSJeff Mahoney void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info)
141faa2dbf0SJosef Bacik {
142faa2dbf0SJosef Bacik 	struct radix_tree_iter iter;
143faa2dbf0SJosef Bacik 	void **slot;
144faa2dbf0SJosef Bacik 
1457c0260eeSJeff Mahoney 	if (!fs_info)
1467c0260eeSJeff Mahoney 		return;
1477c0260eeSJeff Mahoney 
1487c0260eeSJeff Mahoney 	if (WARN_ON(!test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO,
1497c0260eeSJeff Mahoney 			      &fs_info->fs_state)))
1507c0260eeSJeff Mahoney 		return;
1517c0260eeSJeff Mahoney 
1527c0260eeSJeff Mahoney 	test_mnt->mnt_sb->s_fs_info = NULL;
1537c0260eeSJeff Mahoney 
154faa2dbf0SJosef Bacik 	spin_lock(&fs_info->buffer_lock);
155faa2dbf0SJosef Bacik 	radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter, 0) {
156faa2dbf0SJosef Bacik 		struct extent_buffer *eb;
157faa2dbf0SJosef Bacik 
158f1e3c289SSasha Levin 		eb = radix_tree_deref_slot_protected(slot, &fs_info->buffer_lock);
159faa2dbf0SJosef Bacik 		if (!eb)
160faa2dbf0SJosef Bacik 			continue;
161faa2dbf0SJosef Bacik 		/* Shouldn't happen but that kind of thinking creates CVE's */
162faa2dbf0SJosef Bacik 		if (radix_tree_exception(eb)) {
163faa2dbf0SJosef Bacik 			if (radix_tree_deref_retry(eb))
164c28f2420SMatthew Wilcox 				slot = radix_tree_iter_retry(&iter);
165faa2dbf0SJosef Bacik 			continue;
166faa2dbf0SJosef Bacik 		}
167148deab2SMatthew Wilcox 		slot = radix_tree_iter_resume(slot, &iter);
168faa2dbf0SJosef Bacik 		spin_unlock(&fs_info->buffer_lock);
169faa2dbf0SJosef Bacik 		free_extent_buffer_stale(eb);
170faa2dbf0SJosef Bacik 		spin_lock(&fs_info->buffer_lock);
171faa2dbf0SJosef Bacik 	}
172faa2dbf0SJosef Bacik 	spin_unlock(&fs_info->buffer_lock);
173faa2dbf0SJosef Bacik 
174faa2dbf0SJosef Bacik 	btrfs_free_qgroup_config(fs_info);
175faa2dbf0SJosef Bacik 	btrfs_free_fs_roots(fs_info);
176faa2dbf0SJosef Bacik 	cleanup_srcu_struct(&fs_info->subvol_srcu);
177faa2dbf0SJosef Bacik 	kfree(fs_info->super_copy);
178faa2dbf0SJosef Bacik 	kfree(fs_info->fs_devices);
179faa2dbf0SJosef Bacik 	kfree(fs_info);
180faa2dbf0SJosef Bacik }
181faa2dbf0SJosef Bacik 
182faa2dbf0SJosef Bacik void btrfs_free_dummy_root(struct btrfs_root *root)
183faa2dbf0SJosef Bacik {
184faa2dbf0SJosef Bacik 	if (!root)
185faa2dbf0SJosef Bacik 		return;
1867c0260eeSJeff Mahoney 	/* Will be freed by btrfs_free_fs_roots */
1877c0260eeSJeff Mahoney 	if (WARN_ON(test_bit(BTRFS_ROOT_IN_RADIX, &root->state)))
1887c0260eeSJeff Mahoney 		return;
1899cfc8ba7SNikolay Borisov 	if (root->node) {
1909cfc8ba7SNikolay Borisov 		/* One for allocate_extent_buffer */
191faa2dbf0SJosef Bacik 		free_extent_buffer(root->node);
1929cfc8ba7SNikolay Borisov 	}
193faa2dbf0SJosef Bacik 	kfree(root);
194faa2dbf0SJosef Bacik }
195faa2dbf0SJosef Bacik 
1967c55ee0cSOmar Sandoval struct btrfs_block_group_cache *
197da17066cSJeff Mahoney btrfs_alloc_dummy_block_group(struct btrfs_fs_info *fs_info,
198da17066cSJeff Mahoney 			      unsigned long length)
1997c55ee0cSOmar Sandoval {
2007c55ee0cSOmar Sandoval 	struct btrfs_block_group_cache *cache;
2017c55ee0cSOmar Sandoval 
2028cce83baSDavid Sterba 	cache = kzalloc(sizeof(*cache), GFP_KERNEL);
2037c55ee0cSOmar Sandoval 	if (!cache)
2047c55ee0cSOmar Sandoval 		return NULL;
2057c55ee0cSOmar Sandoval 	cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
2068cce83baSDavid Sterba 					GFP_KERNEL);
2077c55ee0cSOmar Sandoval 	if (!cache->free_space_ctl) {
2087c55ee0cSOmar Sandoval 		kfree(cache);
2097c55ee0cSOmar Sandoval 		return NULL;
2107c55ee0cSOmar Sandoval 	}
2117c55ee0cSOmar Sandoval 
2127c55ee0cSOmar Sandoval 	cache->key.objectid = 0;
2137c55ee0cSOmar Sandoval 	cache->key.offset = length;
2147c55ee0cSOmar Sandoval 	cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
215da17066cSJeff Mahoney 	cache->full_stripe_len = fs_info->sectorsize;
216da17066cSJeff Mahoney 	cache->fs_info = fs_info;
2177c55ee0cSOmar Sandoval 
2187c55ee0cSOmar Sandoval 	INIT_LIST_HEAD(&cache->list);
2197c55ee0cSOmar Sandoval 	INIT_LIST_HEAD(&cache->cluster_list);
2207c55ee0cSOmar Sandoval 	INIT_LIST_HEAD(&cache->bg_list);
2217c55ee0cSOmar Sandoval 	btrfs_init_free_space_ctl(cache);
2227c55ee0cSOmar Sandoval 	mutex_init(&cache->free_space_lock);
2237c55ee0cSOmar Sandoval 
2247c55ee0cSOmar Sandoval 	return cache;
2257c55ee0cSOmar Sandoval }
2267c55ee0cSOmar Sandoval 
2277c55ee0cSOmar Sandoval void btrfs_free_dummy_block_group(struct btrfs_block_group_cache *cache)
2287c55ee0cSOmar Sandoval {
2297c55ee0cSOmar Sandoval 	if (!cache)
2307c55ee0cSOmar Sandoval 		return;
2317c55ee0cSOmar Sandoval 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
2327c55ee0cSOmar Sandoval 	kfree(cache->free_space_ctl);
2337c55ee0cSOmar Sandoval 	kfree(cache);
2347c55ee0cSOmar Sandoval }
2357c55ee0cSOmar Sandoval 
236483bce06SNikolay Borisov void btrfs_init_dummy_trans(struct btrfs_trans_handle *trans,
237483bce06SNikolay Borisov 			    struct btrfs_fs_info *fs_info)
2387c55ee0cSOmar Sandoval {
2397c55ee0cSOmar Sandoval 	memset(trans, 0, sizeof(*trans));
2407c55ee0cSOmar Sandoval 	trans->transid = 1;
2417c55ee0cSOmar Sandoval 	trans->type = __TRANS_DUMMY;
242483bce06SNikolay Borisov 	trans->fs_info = fs_info;
2437c55ee0cSOmar Sandoval }
2448632daaeSJeff Mahoney 
2458632daaeSJeff Mahoney int btrfs_run_sanity_tests(void)
2468632daaeSJeff Mahoney {
2478632daaeSJeff Mahoney 	int ret, i;
2488632daaeSJeff Mahoney 	u32 sectorsize, nodesize;
2498632daaeSJeff Mahoney 	u32 test_sectorsize[] = {
2508632daaeSJeff Mahoney 		PAGE_SIZE,
2518632daaeSJeff Mahoney 	};
2528632daaeSJeff Mahoney 	ret = btrfs_init_test_fs();
2538632daaeSJeff Mahoney 	if (ret)
2548632daaeSJeff Mahoney 		return ret;
2558632daaeSJeff Mahoney 	for (i = 0; i < ARRAY_SIZE(test_sectorsize); i++) {
2568632daaeSJeff Mahoney 		sectorsize = test_sectorsize[i];
2578632daaeSJeff Mahoney 		for (nodesize = sectorsize;
2588632daaeSJeff Mahoney 		     nodesize <= BTRFS_MAX_METADATA_BLOCKSIZE;
2598632daaeSJeff Mahoney 		     nodesize <<= 1) {
2608632daaeSJeff Mahoney 			pr_info("BTRFS: selftest: sectorsize: %u  nodesize: %u\n",
2618632daaeSJeff Mahoney 				sectorsize, nodesize);
2628632daaeSJeff Mahoney 			ret = btrfs_test_free_space_cache(sectorsize, nodesize);
2638632daaeSJeff Mahoney 			if (ret)
2648632daaeSJeff Mahoney 				goto out;
2658632daaeSJeff Mahoney 			ret = btrfs_test_extent_buffer_operations(sectorsize,
2668632daaeSJeff Mahoney 				nodesize);
2678632daaeSJeff Mahoney 			if (ret)
2688632daaeSJeff Mahoney 				goto out;
2698632daaeSJeff Mahoney 			ret = btrfs_test_extent_io(sectorsize, nodesize);
2708632daaeSJeff Mahoney 			if (ret)
2718632daaeSJeff Mahoney 				goto out;
2728632daaeSJeff Mahoney 			ret = btrfs_test_inodes(sectorsize, nodesize);
2738632daaeSJeff Mahoney 			if (ret)
2748632daaeSJeff Mahoney 				goto out;
2758632daaeSJeff Mahoney 			ret = btrfs_test_qgroups(sectorsize, nodesize);
2768632daaeSJeff Mahoney 			if (ret)
2778632daaeSJeff Mahoney 				goto out;
2788632daaeSJeff Mahoney 			ret = btrfs_test_free_space_tree(sectorsize, nodesize);
2798632daaeSJeff Mahoney 			if (ret)
2808632daaeSJeff Mahoney 				goto out;
2818632daaeSJeff Mahoney 		}
2828632daaeSJeff Mahoney 	}
28372b28077SLiu Bo 	ret = btrfs_test_extent_map();
2847a61f880SColin Ian King 
2858632daaeSJeff Mahoney out:
2868632daaeSJeff Mahoney 	btrfs_destroy_test_fs();
2878632daaeSJeff Mahoney 	return ret;
2888632daaeSJeff Mahoney }
289