GNU Linux-libre 4.4.295-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 "../volumes.h"
25 #include "../disk-io.h"
26 #include "../qgroup.h"
27
28 static struct vfsmount *test_mnt = NULL;
29
30 static const struct super_operations btrfs_test_super_ops = {
31         .alloc_inode    = btrfs_alloc_inode,
32         .destroy_inode  = btrfs_test_destroy_inode,
33 };
34
35 static struct dentry *btrfs_test_mount(struct file_system_type *fs_type,
36                                        int flags, const char *dev_name,
37                                        void *data)
38 {
39         return mount_pseudo(fs_type, "btrfs_test:", &btrfs_test_super_ops,
40                             NULL, BTRFS_TEST_MAGIC);
41 }
42
43 static struct file_system_type test_type = {
44         .name           = "btrfs_test_fs",
45         .mount          = btrfs_test_mount,
46         .kill_sb        = kill_anon_super,
47 };
48
49 struct inode *btrfs_new_test_inode(void)
50 {
51         struct inode *inode;
52
53         inode = new_inode(test_mnt->mnt_sb);
54         if (inode)
55                 inode_init_owner(inode, NULL, S_IFREG);
56
57         return inode;
58 }
59
60 int btrfs_init_test_fs(void)
61 {
62         int ret;
63
64         ret = register_filesystem(&test_type);
65         if (ret) {
66                 printk(KERN_ERR "btrfs: cannot register test file system\n");
67                 return ret;
68         }
69
70         test_mnt = kern_mount(&test_type);
71         if (IS_ERR(test_mnt)) {
72                 printk(KERN_ERR "btrfs: cannot mount test file system\n");
73                 unregister_filesystem(&test_type);
74                 return ret;
75         }
76         return 0;
77 }
78
79 void btrfs_destroy_test_fs(void)
80 {
81         kern_unmount(test_mnt);
82         unregister_filesystem(&test_type);
83 }
84
85 struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(void)
86 {
87         struct btrfs_fs_info *fs_info = kzalloc(sizeof(struct btrfs_fs_info),
88                                                 GFP_NOFS);
89
90         if (!fs_info)
91                 return fs_info;
92         fs_info->fs_devices = kzalloc(sizeof(struct btrfs_fs_devices),
93                                       GFP_NOFS);
94         if (!fs_info->fs_devices) {
95                 kfree(fs_info);
96                 return NULL;
97         }
98         fs_info->super_copy = kzalloc(sizeof(struct btrfs_super_block),
99                                       GFP_NOFS);
100         if (!fs_info->super_copy) {
101                 kfree(fs_info->fs_devices);
102                 kfree(fs_info);
103                 return NULL;
104         }
105
106         if (init_srcu_struct(&fs_info->subvol_srcu)) {
107                 kfree(fs_info->fs_devices);
108                 kfree(fs_info->super_copy);
109                 kfree(fs_info);
110                 return NULL;
111         }
112
113         spin_lock_init(&fs_info->buffer_lock);
114         spin_lock_init(&fs_info->qgroup_lock);
115         spin_lock_init(&fs_info->qgroup_op_lock);
116         spin_lock_init(&fs_info->super_lock);
117         spin_lock_init(&fs_info->fs_roots_radix_lock);
118         mutex_init(&fs_info->qgroup_ioctl_lock);
119         mutex_init(&fs_info->qgroup_rescan_lock);
120         rwlock_init(&fs_info->tree_mod_log_lock);
121         fs_info->running_transaction = NULL;
122         fs_info->qgroup_tree = RB_ROOT;
123         fs_info->qgroup_ulist = NULL;
124         atomic64_set(&fs_info->tree_mod_seq, 0);
125         INIT_LIST_HEAD(&fs_info->dirty_qgroups);
126         INIT_LIST_HEAD(&fs_info->dead_roots);
127         INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
128         INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC);
129         INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
130         return fs_info;
131 }
132
133 static void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info)
134 {
135         struct radix_tree_iter iter;
136         void **slot;
137
138         spin_lock(&fs_info->buffer_lock);
139 restart:
140         radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter, 0) {
141                 struct extent_buffer *eb;
142
143                 eb = radix_tree_deref_slot_protected(slot, &fs_info->buffer_lock);
144                 if (!eb)
145                         continue;
146                 /* Shouldn't happen but that kind of thinking creates CVE's */
147                 if (radix_tree_exception(eb)) {
148                         if (radix_tree_deref_retry(eb))
149                                 goto restart;
150                         continue;
151                 }
152                 spin_unlock(&fs_info->buffer_lock);
153                 free_extent_buffer_stale(eb);
154                 spin_lock(&fs_info->buffer_lock);
155         }
156         spin_unlock(&fs_info->buffer_lock);
157
158         btrfs_free_qgroup_config(fs_info);
159         btrfs_free_fs_roots(fs_info);
160         cleanup_srcu_struct(&fs_info->subvol_srcu);
161         kfree(fs_info->super_copy);
162         kfree(fs_info->fs_devices);
163         kfree(fs_info);
164 }
165
166 void btrfs_free_dummy_root(struct btrfs_root *root)
167 {
168         if (!root)
169                 return;
170         if (root->node)
171                 free_extent_buffer(root->node);
172         if (root->fs_info)
173                 btrfs_free_dummy_fs_info(root->fs_info);
174         kfree(root);
175 }
176