GNU Linux-libre 4.14.290-gnu1
[releases.git] / fs / btrfs / tests / btrfs-tests.c
1 /*
2  * Copyright (C) 2013 Fusion IO.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include <linux/fs.h>
20 #include <linux/mount.h>
21 #include <linux/magic.h>
22 #include "btrfs-tests.h"
23 #include "../ctree.h"
24 #include "../free-space-cache.h"
25 #include "../free-space-tree.h"
26 #include "../transaction.h"
27 #include "../volumes.h"
28 #include "../disk-io.h"
29 #include "../qgroup.h"
30
31 static struct vfsmount *test_mnt = NULL;
32
33 static const struct super_operations btrfs_test_super_ops = {
34         .alloc_inode    = btrfs_alloc_inode,
35         .destroy_inode  = btrfs_test_destroy_inode,
36 };
37
38 static struct dentry *btrfs_test_mount(struct file_system_type *fs_type,
39                                        int flags, const char *dev_name,
40                                        void *data)
41 {
42         return mount_pseudo(fs_type, "btrfs_test:", &btrfs_test_super_ops,
43                             NULL, BTRFS_TEST_MAGIC);
44 }
45
46 static struct file_system_type test_type = {
47         .name           = "btrfs_test_fs",
48         .mount          = btrfs_test_mount,
49         .kill_sb        = kill_anon_super,
50 };
51
52 struct inode *btrfs_new_test_inode(void)
53 {
54         struct inode *inode;
55
56         inode = new_inode(test_mnt->mnt_sb);
57         if (inode)
58                 inode_init_owner(inode, NULL, S_IFREG);
59
60         return inode;
61 }
62
63 static int btrfs_init_test_fs(void)
64 {
65         int ret;
66
67         ret = register_filesystem(&test_type);
68         if (ret) {
69                 printk(KERN_ERR "btrfs: cannot register test file system\n");
70                 return ret;
71         }
72
73         test_mnt = kern_mount(&test_type);
74         if (IS_ERR(test_mnt)) {
75                 printk(KERN_ERR "btrfs: cannot mount test file system\n");
76                 unregister_filesystem(&test_type);
77                 return PTR_ERR(test_mnt);
78         }
79         return 0;
80 }
81
82 static void btrfs_destroy_test_fs(void)
83 {
84         kern_unmount(test_mnt);
85         unregister_filesystem(&test_type);
86 }
87
88 struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize)
89 {
90         struct btrfs_fs_info *fs_info = kzalloc(sizeof(struct btrfs_fs_info),
91                                                 GFP_KERNEL);
92
93         if (!fs_info)
94                 return fs_info;
95         fs_info->fs_devices = kzalloc(sizeof(struct btrfs_fs_devices),
96                                       GFP_KERNEL);
97         if (!fs_info->fs_devices) {
98                 kfree(fs_info);
99                 return NULL;
100         }
101         fs_info->super_copy = kzalloc(sizeof(struct btrfs_super_block),
102                                       GFP_KERNEL);
103         if (!fs_info->super_copy) {
104                 kfree(fs_info->fs_devices);
105                 kfree(fs_info);
106                 return NULL;
107         }
108
109         fs_info->nodesize = nodesize;
110         fs_info->sectorsize = sectorsize;
111
112         if (init_srcu_struct(&fs_info->subvol_srcu)) {
113                 kfree(fs_info->fs_devices);
114                 kfree(fs_info->super_copy);
115                 kfree(fs_info);
116                 return NULL;
117         }
118
119         spin_lock_init(&fs_info->buffer_lock);
120         spin_lock_init(&fs_info->qgroup_lock);
121         spin_lock_init(&fs_info->qgroup_op_lock);
122         spin_lock_init(&fs_info->super_lock);
123         spin_lock_init(&fs_info->fs_roots_radix_lock);
124         mutex_init(&fs_info->qgroup_ioctl_lock);
125         mutex_init(&fs_info->qgroup_rescan_lock);
126         rwlock_init(&fs_info->tree_mod_log_lock);
127         fs_info->running_transaction = NULL;
128         fs_info->qgroup_tree = RB_ROOT;
129         fs_info->qgroup_ulist = NULL;
130         atomic64_set(&fs_info->tree_mod_seq, 0);
131         INIT_LIST_HEAD(&fs_info->dirty_qgroups);
132         INIT_LIST_HEAD(&fs_info->dead_roots);
133         INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
134         INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC);
135         INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
136         extent_io_tree_init(&fs_info->freed_extents[0], NULL);
137         extent_io_tree_init(&fs_info->freed_extents[1], NULL);
138         fs_info->pinned_extents = &fs_info->freed_extents[0];
139         set_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
140
141         test_mnt->mnt_sb->s_fs_info = fs_info;
142
143         return fs_info;
144 }
145
146 void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info)
147 {
148         struct radix_tree_iter iter;
149         void **slot;
150
151         if (!fs_info)
152                 return;
153
154         if (WARN_ON(!test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO,
155                               &fs_info->fs_state)))
156                 return;
157
158         test_mnt->mnt_sb->s_fs_info = NULL;
159
160         spin_lock(&fs_info->buffer_lock);
161         radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter, 0) {
162                 struct extent_buffer *eb;
163
164                 eb = radix_tree_deref_slot_protected(slot, &fs_info->buffer_lock);
165                 if (!eb)
166                         continue;
167                 /* Shouldn't happen but that kind of thinking creates CVE's */
168                 if (radix_tree_exception(eb)) {
169                         if (radix_tree_deref_retry(eb))
170                                 slot = radix_tree_iter_retry(&iter);
171                         continue;
172                 }
173                 slot = radix_tree_iter_resume(slot, &iter);
174                 spin_unlock(&fs_info->buffer_lock);
175                 free_extent_buffer_stale(eb);
176                 spin_lock(&fs_info->buffer_lock);
177         }
178         spin_unlock(&fs_info->buffer_lock);
179
180         btrfs_free_qgroup_config(fs_info);
181         btrfs_free_fs_roots(fs_info);
182         cleanup_srcu_struct(&fs_info->subvol_srcu);
183         kfree(fs_info->super_copy);
184         kfree(fs_info->fs_devices);
185         kfree(fs_info);
186 }
187
188 void btrfs_free_dummy_root(struct btrfs_root *root)
189 {
190         if (!root)
191                 return;
192         /* Will be freed by btrfs_free_fs_roots */
193         if (WARN_ON(test_bit(BTRFS_ROOT_IN_RADIX, &root->state)))
194                 return;
195         if (root->node)
196                 free_extent_buffer(root->node);
197         kfree(root);
198 }
199
200 struct btrfs_block_group_cache *
201 btrfs_alloc_dummy_block_group(struct btrfs_fs_info *fs_info,
202                               unsigned long length)
203 {
204         struct btrfs_block_group_cache *cache;
205
206         cache = kzalloc(sizeof(*cache), GFP_KERNEL);
207         if (!cache)
208                 return NULL;
209         cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
210                                         GFP_KERNEL);
211         if (!cache->free_space_ctl) {
212                 kfree(cache);
213                 return NULL;
214         }
215
216         cache->key.objectid = 0;
217         cache->key.offset = length;
218         cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
219         cache->full_stripe_len = fs_info->sectorsize;
220         cache->fs_info = fs_info;
221
222         INIT_LIST_HEAD(&cache->list);
223         INIT_LIST_HEAD(&cache->cluster_list);
224         INIT_LIST_HEAD(&cache->bg_list);
225         btrfs_init_free_space_ctl(cache);
226         mutex_init(&cache->free_space_lock);
227
228         return cache;
229 }
230
231 void btrfs_free_dummy_block_group(struct btrfs_block_group_cache *cache)
232 {
233         if (!cache)
234                 return;
235         __btrfs_remove_free_space_cache(cache->free_space_ctl);
236         kfree(cache->free_space_ctl);
237         kfree(cache);
238 }
239
240 void btrfs_init_dummy_trans(struct btrfs_trans_handle *trans)
241 {
242         memset(trans, 0, sizeof(*trans));
243         trans->transid = 1;
244         trans->type = __TRANS_DUMMY;
245 }
246
247 int btrfs_run_sanity_tests(void)
248 {
249         int ret, i;
250         u32 sectorsize, nodesize;
251         u32 test_sectorsize[] = {
252                 PAGE_SIZE,
253         };
254         ret = btrfs_init_test_fs();
255         if (ret)
256                 return ret;
257         for (i = 0; i < ARRAY_SIZE(test_sectorsize); i++) {
258                 sectorsize = test_sectorsize[i];
259                 for (nodesize = sectorsize;
260                      nodesize <= BTRFS_MAX_METADATA_BLOCKSIZE;
261                      nodesize <<= 1) {
262                         pr_info("BTRFS: selftest: sectorsize: %u  nodesize: %u\n",
263                                 sectorsize, nodesize);
264                         ret = btrfs_test_free_space_cache(sectorsize, nodesize);
265                         if (ret)
266                                 goto out;
267                         ret = btrfs_test_extent_buffer_operations(sectorsize,
268                                 nodesize);
269                         if (ret)
270                                 goto out;
271                         ret = btrfs_test_extent_io(sectorsize, nodesize);
272                         if (ret)
273                                 goto out;
274                         ret = btrfs_test_inodes(sectorsize, nodesize);
275                         if (ret)
276                                 goto out;
277                         ret = btrfs_test_qgroups(sectorsize, nodesize);
278                         if (ret)
279                                 goto out;
280                         ret = btrfs_test_free_space_tree(sectorsize, nodesize);
281                         if (ret)
282                                 goto out;
283                 }
284         }
285 out:
286         btrfs_destroy_test_fs();
287         return ret;
288 }