xref: /openbmc/linux/fs/btrfs/tests/btrfs-tests.c (revision c28f2420635b7000f7b9cde6cdbe6e7a0f8beed1)
1294e30feSJosef Bacik /*
2294e30feSJosef Bacik  * Copyright (C) 2013 Fusion IO.  All rights reserved.
3294e30feSJosef Bacik  *
4294e30feSJosef Bacik  * This program is free software; you can redistribute it and/or
5294e30feSJosef Bacik  * modify it under the terms of the GNU General Public
6294e30feSJosef Bacik  * License v2 as published by the Free Software Foundation.
7294e30feSJosef Bacik  *
8294e30feSJosef Bacik  * This program is distributed in the hope that it will be useful,
9294e30feSJosef Bacik  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10294e30feSJosef Bacik  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11294e30feSJosef Bacik  * General Public License for more details.
12294e30feSJosef Bacik  *
13294e30feSJosef Bacik  * You should have received a copy of the GNU General Public
14294e30feSJosef Bacik  * License along with this program; if not, write to the
15294e30feSJosef Bacik  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16294e30feSJosef Bacik  * Boston, MA 021110-1307, USA.
17294e30feSJosef Bacik  */
18294e30feSJosef Bacik 
19294e30feSJosef Bacik #include <linux/fs.h>
20294e30feSJosef Bacik #include <linux/mount.h>
21294e30feSJosef Bacik #include <linux/magic.h>
22294e30feSJosef Bacik #include "btrfs-tests.h"
23294e30feSJosef Bacik #include "../ctree.h"
247c55ee0cSOmar Sandoval #include "../free-space-cache.h"
257c55ee0cSOmar Sandoval #include "../free-space-tree.h"
267c55ee0cSOmar Sandoval #include "../transaction.h"
27faa2dbf0SJosef Bacik #include "../volumes.h"
28faa2dbf0SJosef Bacik #include "../disk-io.h"
29faa2dbf0SJosef Bacik #include "../qgroup.h"
30294e30feSJosef Bacik 
31294e30feSJosef Bacik static struct vfsmount *test_mnt = NULL;
32294e30feSJosef Bacik 
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 
38294e30feSJosef Bacik static struct dentry *btrfs_test_mount(struct file_system_type *fs_type,
39294e30feSJosef Bacik 				       int flags, const char *dev_name,
40294e30feSJosef Bacik 				       void *data)
41294e30feSJosef Bacik {
42aaedb55bSJosef Bacik 	return mount_pseudo(fs_type, "btrfs_test:", &btrfs_test_super_ops,
43aaedb55bSJosef Bacik 			    NULL, BTRFS_TEST_MAGIC);
44294e30feSJosef Bacik }
45294e30feSJosef Bacik 
46294e30feSJosef Bacik static struct file_system_type test_type = {
47294e30feSJosef Bacik 	.name		= "btrfs_test_fs",
48294e30feSJosef Bacik 	.mount		= btrfs_test_mount,
49294e30feSJosef Bacik 	.kill_sb	= kill_anon_super,
50294e30feSJosef Bacik };
51294e30feSJosef Bacik 
52294e30feSJosef Bacik struct inode *btrfs_new_test_inode(void)
53294e30feSJosef Bacik {
54294e30feSJosef Bacik 	return new_inode(test_mnt->mnt_sb);
55294e30feSJosef Bacik }
56294e30feSJosef Bacik 
57294e30feSJosef Bacik int btrfs_init_test_fs(void)
58294e30feSJosef Bacik {
59294e30feSJosef Bacik 	int ret;
60294e30feSJosef Bacik 
61294e30feSJosef Bacik 	ret = register_filesystem(&test_type);
62294e30feSJosef Bacik 	if (ret) {
63294e30feSJosef Bacik 		printk(KERN_ERR "btrfs: cannot register test file system\n");
64294e30feSJosef Bacik 		return ret;
65294e30feSJosef Bacik 	}
66294e30feSJosef Bacik 
67294e30feSJosef Bacik 	test_mnt = kern_mount(&test_type);
68294e30feSJosef Bacik 	if (IS_ERR(test_mnt)) {
69294e30feSJosef Bacik 		printk(KERN_ERR "btrfs: cannot mount test file system\n");
70294e30feSJosef Bacik 		unregister_filesystem(&test_type);
71294e30feSJosef Bacik 		return ret;
72294e30feSJosef Bacik 	}
73294e30feSJosef Bacik 	return 0;
74294e30feSJosef Bacik }
75294e30feSJosef Bacik 
76294e30feSJosef Bacik void btrfs_destroy_test_fs(void)
77294e30feSJosef Bacik {
78294e30feSJosef Bacik 	kern_unmount(test_mnt);
79294e30feSJosef Bacik 	unregister_filesystem(&test_type);
80294e30feSJosef Bacik }
81faa2dbf0SJosef Bacik 
82faa2dbf0SJosef Bacik struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(void)
83faa2dbf0SJosef Bacik {
84faa2dbf0SJosef Bacik 	struct btrfs_fs_info *fs_info = kzalloc(sizeof(struct btrfs_fs_info),
858cce83baSDavid Sterba 						GFP_KERNEL);
86faa2dbf0SJosef Bacik 
87faa2dbf0SJosef Bacik 	if (!fs_info)
88faa2dbf0SJosef Bacik 		return fs_info;
89faa2dbf0SJosef Bacik 	fs_info->fs_devices = kzalloc(sizeof(struct btrfs_fs_devices),
908cce83baSDavid Sterba 				      GFP_KERNEL);
91faa2dbf0SJosef Bacik 	if (!fs_info->fs_devices) {
92faa2dbf0SJosef Bacik 		kfree(fs_info);
93faa2dbf0SJosef Bacik 		return NULL;
94faa2dbf0SJosef Bacik 	}
95faa2dbf0SJosef Bacik 	fs_info->super_copy = kzalloc(sizeof(struct btrfs_super_block),
968cce83baSDavid Sterba 				      GFP_KERNEL);
97faa2dbf0SJosef Bacik 	if (!fs_info->super_copy) {
98faa2dbf0SJosef Bacik 		kfree(fs_info->fs_devices);
99faa2dbf0SJosef Bacik 		kfree(fs_info);
100faa2dbf0SJosef Bacik 		return NULL;
101faa2dbf0SJosef Bacik 	}
102faa2dbf0SJosef Bacik 
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);
1287c55ee0cSOmar Sandoval 	extent_io_tree_init(&fs_info->freed_extents[0], NULL);
1297c55ee0cSOmar Sandoval 	extent_io_tree_init(&fs_info->freed_extents[1], NULL);
1307c55ee0cSOmar Sandoval 	fs_info->pinned_extents = &fs_info->freed_extents[0];
131faa2dbf0SJosef Bacik 	return fs_info;
132faa2dbf0SJosef Bacik }
133faa2dbf0SJosef Bacik 
134faa2dbf0SJosef Bacik static void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info)
135faa2dbf0SJosef Bacik {
136faa2dbf0SJosef Bacik 	struct radix_tree_iter iter;
137faa2dbf0SJosef Bacik 	void **slot;
138faa2dbf0SJosef Bacik 
139faa2dbf0SJosef Bacik 	spin_lock(&fs_info->buffer_lock);
140faa2dbf0SJosef Bacik 	radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter, 0) {
141faa2dbf0SJosef Bacik 		struct extent_buffer *eb;
142faa2dbf0SJosef Bacik 
143f1e3c289SSasha Levin 		eb = radix_tree_deref_slot_protected(slot, &fs_info->buffer_lock);
144faa2dbf0SJosef Bacik 		if (!eb)
145faa2dbf0SJosef Bacik 			continue;
146faa2dbf0SJosef Bacik 		/* Shouldn't happen but that kind of thinking creates CVE's */
147faa2dbf0SJosef Bacik 		if (radix_tree_exception(eb)) {
148faa2dbf0SJosef Bacik 			if (radix_tree_deref_retry(eb))
149*c28f2420SMatthew Wilcox 				slot = radix_tree_iter_retry(&iter);
150faa2dbf0SJosef Bacik 			continue;
151faa2dbf0SJosef Bacik 		}
152faa2dbf0SJosef Bacik 		spin_unlock(&fs_info->buffer_lock);
153faa2dbf0SJosef Bacik 		free_extent_buffer_stale(eb);
154faa2dbf0SJosef Bacik 		spin_lock(&fs_info->buffer_lock);
155faa2dbf0SJosef Bacik 	}
156faa2dbf0SJosef Bacik 	spin_unlock(&fs_info->buffer_lock);
157faa2dbf0SJosef Bacik 
158faa2dbf0SJosef Bacik 	btrfs_free_qgroup_config(fs_info);
159faa2dbf0SJosef Bacik 	btrfs_free_fs_roots(fs_info);
160faa2dbf0SJosef Bacik 	cleanup_srcu_struct(&fs_info->subvol_srcu);
161faa2dbf0SJosef Bacik 	kfree(fs_info->super_copy);
162faa2dbf0SJosef Bacik 	kfree(fs_info->fs_devices);
163faa2dbf0SJosef Bacik 	kfree(fs_info);
164faa2dbf0SJosef Bacik }
165faa2dbf0SJosef Bacik 
166faa2dbf0SJosef Bacik void btrfs_free_dummy_root(struct btrfs_root *root)
167faa2dbf0SJosef Bacik {
168faa2dbf0SJosef Bacik 	if (!root)
169faa2dbf0SJosef Bacik 		return;
170faa2dbf0SJosef Bacik 	if (root->node)
171faa2dbf0SJosef Bacik 		free_extent_buffer(root->node);
172faa2dbf0SJosef Bacik 	if (root->fs_info)
173faa2dbf0SJosef Bacik 		btrfs_free_dummy_fs_info(root->fs_info);
174faa2dbf0SJosef Bacik 	kfree(root);
175faa2dbf0SJosef Bacik }
176faa2dbf0SJosef Bacik 
1777c55ee0cSOmar Sandoval struct btrfs_block_group_cache *
1787c55ee0cSOmar Sandoval btrfs_alloc_dummy_block_group(unsigned long length)
1797c55ee0cSOmar Sandoval {
1807c55ee0cSOmar Sandoval 	struct btrfs_block_group_cache *cache;
1817c55ee0cSOmar Sandoval 
1828cce83baSDavid Sterba 	cache = kzalloc(sizeof(*cache), GFP_KERNEL);
1837c55ee0cSOmar Sandoval 	if (!cache)
1847c55ee0cSOmar Sandoval 		return NULL;
1857c55ee0cSOmar Sandoval 	cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
1868cce83baSDavid Sterba 					GFP_KERNEL);
1877c55ee0cSOmar Sandoval 	if (!cache->free_space_ctl) {
1887c55ee0cSOmar Sandoval 		kfree(cache);
1897c55ee0cSOmar Sandoval 		return NULL;
1907c55ee0cSOmar Sandoval 	}
191f7d3d2f9SChris Mason 	cache->fs_info = btrfs_alloc_dummy_fs_info();
192f7d3d2f9SChris Mason 	if (!cache->fs_info) {
193f7d3d2f9SChris Mason 		kfree(cache->free_space_ctl);
194f7d3d2f9SChris Mason 		kfree(cache);
195f7d3d2f9SChris Mason 		return NULL;
196f7d3d2f9SChris Mason 	}
1977c55ee0cSOmar Sandoval 
1987c55ee0cSOmar Sandoval 	cache->key.objectid = 0;
1997c55ee0cSOmar Sandoval 	cache->key.offset = length;
2007c55ee0cSOmar Sandoval 	cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
2017c55ee0cSOmar Sandoval 	cache->sectorsize = 4096;
2027c55ee0cSOmar Sandoval 	cache->full_stripe_len = 4096;
2037c55ee0cSOmar Sandoval 
2047c55ee0cSOmar Sandoval 	INIT_LIST_HEAD(&cache->list);
2057c55ee0cSOmar Sandoval 	INIT_LIST_HEAD(&cache->cluster_list);
2067c55ee0cSOmar Sandoval 	INIT_LIST_HEAD(&cache->bg_list);
2077c55ee0cSOmar Sandoval 	btrfs_init_free_space_ctl(cache);
2087c55ee0cSOmar Sandoval 	mutex_init(&cache->free_space_lock);
2097c55ee0cSOmar Sandoval 
2107c55ee0cSOmar Sandoval 	return cache;
2117c55ee0cSOmar Sandoval }
2127c55ee0cSOmar Sandoval 
2137c55ee0cSOmar Sandoval void btrfs_free_dummy_block_group(struct btrfs_block_group_cache *cache)
2147c55ee0cSOmar Sandoval {
2157c55ee0cSOmar Sandoval 	if (!cache)
2167c55ee0cSOmar Sandoval 		return;
2177c55ee0cSOmar Sandoval 	__btrfs_remove_free_space_cache(cache->free_space_ctl);
2187c55ee0cSOmar Sandoval 	kfree(cache->free_space_ctl);
2197c55ee0cSOmar Sandoval 	kfree(cache);
2207c55ee0cSOmar Sandoval }
2217c55ee0cSOmar Sandoval 
2227c55ee0cSOmar Sandoval void btrfs_init_dummy_trans(struct btrfs_trans_handle *trans)
2237c55ee0cSOmar Sandoval {
2247c55ee0cSOmar Sandoval 	memset(trans, 0, sizeof(*trans));
2257c55ee0cSOmar Sandoval 	trans->transid = 1;
2267c55ee0cSOmar Sandoval 	INIT_LIST_HEAD(&trans->qgroup_ref_list);
2277c55ee0cSOmar Sandoval 	trans->type = __TRANS_DUMMY;
2287c55ee0cSOmar Sandoval }
229