blob: 020e5a83e31f8b29584a594be935f31b1e2a71aa [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. 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
Yan4b82d6e2007-08-29 09:11:44 -040019#include <linux/blkdev.h>
Chris Mason2e635a22007-03-21 11:12:56 -040020#include <linux/module.h>
Chris Masone20d96d2007-03-22 12:13:20 -040021#include <linux/buffer_head.h>
Chris Mason2e635a22007-03-21 11:12:56 -040022#include <linux/fs.h>
23#include <linux/pagemap.h>
24#include <linux/highmem.h>
25#include <linux/time.h>
26#include <linux/init.h>
27#include <linux/string.h>
28#include <linux/smp_lock.h>
29#include <linux/backing-dev.h>
Yan4b82d6e2007-08-29 09:11:44 -040030#include <linux/mount.h>
Chris Masondee26a92007-03-26 16:00:06 -040031#include <linux/mpage.h>
Chris Mason75dfe392007-03-29 11:56:46 -040032#include <linux/swap.h>
33#include <linux/writeback.h>
Chris Mason8fd17792007-04-19 21:01:03 -040034#include <linux/statfs.h>
Chris Mason08607c12007-06-08 15:33:54 -040035#include <linux/compat.h>
Chris Mason95e05282007-08-29 09:11:44 -040036#include <linux/parser.h>
Chris Masonc59f8952007-12-17 20:14:04 -050037#include <linux/ctype.h>
Chris Mason6da6aba2007-12-18 16:15:09 -050038#include <linux/namei.h>
Chris Masona9218f62008-03-24 15:02:04 -040039#include <linux/miscdevice.h>
Chris Mason2e635a22007-03-21 11:12:56 -040040#include "ctree.h"
Chris Masone20d96d2007-03-22 12:13:20 -040041#include "disk-io.h"
Chris Masond5719762007-03-23 10:01:08 -040042#include "transaction.h"
Chris Mason2c90e5d2007-04-02 10:50:19 -040043#include "btrfs_inode.h"
Chris Masonc5739bb2007-04-10 09:27:04 -040044#include "ioctl.h"
Chris Mason3a686372007-05-24 13:35:57 -040045#include "print-tree.h"
Josef Bacik5103e942007-11-16 11:45:54 -050046#include "xattr.h"
Chris Mason8a4b83c2008-03-24 15:02:07 -040047#include "volumes.h"
Chris Mason2e635a22007-03-21 11:12:56 -040048
Chris Mason5f39d392007-10-15 16:14:19 -040049#define BTRFS_SUPER_MAGIC 0x9123683E
Chris Masone20d96d2007-03-22 12:13:20 -040050
Chris Masone20d96d2007-03-22 12:13:20 -040051static struct super_operations btrfs_super_ops;
Chris Masone20d96d2007-03-22 12:13:20 -040052
53static void btrfs_put_super (struct super_block * sb)
54{
55 struct btrfs_root *root = btrfs_sb(sb);
Josef Bacik58176a92007-08-29 15:47:34 -040056 struct btrfs_fs_info *fs = root->fs_info;
Chris Masone20d96d2007-03-22 12:13:20 -040057 int ret;
58
59 ret = close_ctree(root);
60 if (ret) {
61 printk("close ctree returns %d\n", ret);
62 }
Josef Bacik58176a92007-08-29 15:47:34 -040063 btrfs_sysfs_del_super(fs);
Chris Masone20d96d2007-03-22 12:13:20 -040064 sb->s_fs_info = NULL;
65}
Chris Mason2e635a22007-03-21 11:12:56 -040066
Chris Mason95e05282007-08-29 09:11:44 -040067enum {
Chris Mason8f662a72008-01-02 10:01:11 -050068 Opt_subvol, Opt_nodatasum, Opt_nodatacow, Opt_max_extent,
Chris Mason6f568d32008-01-29 16:03:38 -050069 Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, Opt_err,
Chris Mason95e05282007-08-29 09:11:44 -040070};
71
72static match_table_t tokens = {
73 {Opt_subvol, "subvol=%s"},
Chris Masonb6cda9b2007-12-14 15:30:32 -050074 {Opt_nodatasum, "nodatasum"},
Chris Masonbe20aa92007-12-17 20:14:01 -050075 {Opt_nodatacow, "nodatacow"},
Chris Mason21ad10c2008-01-09 09:23:21 -050076 {Opt_nobarrier, "nobarrier"},
Chris Masonc59f8952007-12-17 20:14:04 -050077 {Opt_max_extent, "max_extent=%s"},
Chris Mason6f568d32008-01-29 16:03:38 -050078 {Opt_max_inline, "max_inline=%s"},
Chris Mason8f662a72008-01-02 10:01:11 -050079 {Opt_alloc_start, "alloc_start=%s"},
Chris Masone18e4802008-01-18 10:54:22 -050080 {Opt_ssd, "ssd"},
Chris Mason95e05282007-08-29 09:11:44 -040081 {Opt_err, NULL}
82};
83
Chris Masonedbd8d42007-12-21 16:27:24 -050084u64 btrfs_parse_size(char *str)
Chris Masonc59f8952007-12-17 20:14:04 -050085{
Chris Masonedbd8d42007-12-21 16:27:24 -050086 u64 res;
Chris Masonc59f8952007-12-17 20:14:04 -050087 int mult = 1;
88 char *end;
89 char last;
90
91 res = simple_strtoul(str, &end, 10);
92
93 last = end[0];
94 if (isalpha(last)) {
95 last = tolower(last);
96 switch (last) {
97 case 'g':
98 mult *= 1024;
99 case 'm':
100 mult *= 1024;
101 case 'k':
102 mult *= 1024;
103 }
104 res = res * mult;
105 }
106 return res;
107}
108
Chris Mason95e05282007-08-29 09:11:44 -0400109static int parse_options (char * options,
110 struct btrfs_root *root,
111 char **subvol_name)
112{
113 char * p;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500114 struct btrfs_fs_info *info = NULL;
Chris Mason95e05282007-08-29 09:11:44 -0400115 substring_t args[MAX_OPT_ARGS];
Chris Masonb6cda9b2007-12-14 15:30:32 -0500116
Chris Mason95e05282007-08-29 09:11:44 -0400117 if (!options)
118 return 1;
119
Chris Masonbe20aa92007-12-17 20:14:01 -0500120 /*
121 * strsep changes the string, duplicate it because parse_options
122 * gets called twice
123 */
124 options = kstrdup(options, GFP_NOFS);
125 if (!options)
126 return -ENOMEM;
127
128 if (root)
129 info = root->fs_info;
130
Chris Mason95e05282007-08-29 09:11:44 -0400131 while ((p = strsep (&options, ",")) != NULL) {
132 int token;
133 if (!*p)
134 continue;
135
136 token = match_token(p, tokens, args);
137 switch (token) {
138 case Opt_subvol:
Chris Masonbe20aa92007-12-17 20:14:01 -0500139 if (subvol_name) {
Chris Masonb6cda9b2007-12-14 15:30:32 -0500140 *subvol_name = match_strdup(&args[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500141 }
Chris Masonb6cda9b2007-12-14 15:30:32 -0500142 break;
143 case Opt_nodatasum:
Chris Masonbe20aa92007-12-17 20:14:01 -0500144 if (info) {
145 printk("btrfs: setting nodatacsum\n");
Chris Masonb6cda9b2007-12-14 15:30:32 -0500146 btrfs_set_opt(info->mount_opt, NODATASUM);
Chris Masonbe20aa92007-12-17 20:14:01 -0500147 }
148 break;
149 case Opt_nodatacow:
150 if (info) {
151 printk("btrfs: setting nodatacow\n");
152 btrfs_set_opt(info->mount_opt, NODATACOW);
153 btrfs_set_opt(info->mount_opt, NODATASUM);
154 }
Chris Mason95e05282007-08-29 09:11:44 -0400155 break;
Chris Masone18e4802008-01-18 10:54:22 -0500156 case Opt_ssd:
157 if (info) {
158 printk("btrfs: use ssd allocation scheme\n");
159 btrfs_set_opt(info->mount_opt, SSD);
160 }
161 break;
Chris Mason21ad10c2008-01-09 09:23:21 -0500162 case Opt_nobarrier:
163 if (info) {
164 printk("btrfs: turning off barriers\n");
165 btrfs_set_opt(info->mount_opt, NOBARRIER);
166 }
167 break;
Chris Masonc59f8952007-12-17 20:14:04 -0500168 case Opt_max_extent:
169 if (info) {
170 char *num = match_strdup(&args[0]);
171 if (num) {
Chris Masonedbd8d42007-12-21 16:27:24 -0500172 info->max_extent =
173 btrfs_parse_size(num);
Chris Masonc59f8952007-12-17 20:14:04 -0500174 kfree(num);
175
176 info->max_extent = max_t(u64,
177 info->max_extent,
178 root->sectorsize);
179 printk("btrfs: max_extent at %Lu\n",
180 info->max_extent);
181 }
182 }
183 break;
Chris Mason6f568d32008-01-29 16:03:38 -0500184 case Opt_max_inline:
185 if (info) {
186 char *num = match_strdup(&args[0]);
187 if (num) {
188 info->max_inline =
189 btrfs_parse_size(num);
190 kfree(num);
191
192 info->max_inline = max_t(u64,
193 info->max_inline,
194 root->sectorsize);
195 printk("btrfs: max_inline at %Lu\n",
196 info->max_inline);
197 }
198 }
199 break;
Chris Mason8f662a72008-01-02 10:01:11 -0500200 case Opt_alloc_start:
201 if (info) {
202 char *num = match_strdup(&args[0]);
203 if (num) {
204 info->alloc_start =
205 btrfs_parse_size(num);
206 kfree(num);
207 printk("btrfs: allocations start at "
208 "%Lu\n", info->alloc_start);
209 }
210 }
211 break;
Chris Mason95e05282007-08-29 09:11:44 -0400212 default:
Chris Masonbe20aa92007-12-17 20:14:01 -0500213 break;
Chris Mason95e05282007-08-29 09:11:44 -0400214 }
215 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500216 kfree(options);
Chris Mason95e05282007-08-29 09:11:44 -0400217 return 1;
218}
219
Chris Mason8a4b83c2008-03-24 15:02:07 -0400220static int btrfs_fill_super(struct super_block * sb,
221 struct btrfs_fs_devices *fs_devices,
222 void * data, int silent)
Chris Mason2e635a22007-03-21 11:12:56 -0400223{
224 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400225 struct dentry * root_dentry;
226 struct btrfs_super_block *disk_super;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400227 struct btrfs_root *tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -0400228 struct btrfs_inode *bi;
Chris Mason39279cc2007-06-12 06:35:45 -0400229 int err;
Chris Mason2e635a22007-03-21 11:12:56 -0400230
231 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -0400232 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400233 sb->s_op = &btrfs_super_ops;
Josef Bacik5103e942007-11-16 11:45:54 -0500234 sb->s_xattr = btrfs_xattr_handlers;
Chris Mason2e635a22007-03-21 11:12:56 -0400235 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400236
Chris Mason8a4b83c2008-03-24 15:02:07 -0400237 tree_root = open_ctree(sb, fs_devices);
Chris Masond98237b2007-03-28 13:57:48 -0400238
Yane58ca022008-04-01 11:21:34 -0400239 if (IS_ERR(tree_root)) {
Chris Masone20d96d2007-03-22 12:13:20 -0400240 printk("btrfs: open_ctree failed\n");
Yane58ca022008-04-01 11:21:34 -0400241 return PTR_ERR(tree_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400242 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400243 sb->s_fs_info = tree_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400244 disk_super = &tree_root->fs_info->super_copy;
Chris Masonc5739bb2007-04-10 09:27:04 -0400245 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
246 tree_root);
Chris Masond6e4a422007-04-06 15:37:36 -0400247 bi = BTRFS_I(inode);
248 bi->location.objectid = inode->i_ino;
249 bi->location.offset = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400250 bi->root = tree_root;
Chris Masonb888db22007-08-27 16:49:44 -0400251
Chris Masond6e4a422007-04-06 15:37:36 -0400252 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
253
Chris Mason39279cc2007-06-12 06:35:45 -0400254 if (!inode) {
255 err = -ENOMEM;
256 goto fail_close;
257 }
Chris Masone20d96d2007-03-22 12:13:20 -0400258 if (inode->i_state & I_NEW) {
259 btrfs_read_locked_inode(inode);
260 unlock_new_inode(inode);
261 }
Chris Mason2e635a22007-03-21 11:12:56 -0400262
Chris Masone20d96d2007-03-22 12:13:20 -0400263 root_dentry = d_alloc_root(inode);
264 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400265 iput(inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400266 err = -ENOMEM;
267 goto fail_close;
Chris Mason2e635a22007-03-21 11:12:56 -0400268 }
Josef Bacik58176a92007-08-29 15:47:34 -0400269
Chris Masonb6cda9b2007-12-14 15:30:32 -0500270 parse_options((char *)data, tree_root, NULL);
271
Josef Bacik58176a92007-08-29 15:47:34 -0400272 /* this does the super kobj at the same time */
273 err = btrfs_sysfs_add_super(tree_root->fs_info);
274 if (err)
275 goto fail_close;
276
Chris Masone20d96d2007-03-22 12:13:20 -0400277 sb->s_root = root_dentry;
Chris Mason08607c12007-06-08 15:33:54 -0400278 btrfs_transaction_queue_work(tree_root, HZ * 30);
Chris Mason6885f302008-02-20 16:11:05 -0500279
280#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
281 save_mount_options(sb, data);
282#endif
283
Chris Mason2e635a22007-03-21 11:12:56 -0400284 return 0;
Chris Mason2e635a22007-03-21 11:12:56 -0400285
Chris Mason39279cc2007-06-12 06:35:45 -0400286fail_close:
287 close_ctree(tree_root);
Chris Masond5719762007-03-23 10:01:08 -0400288 return err;
289}
290
Chris Masond5719762007-03-23 10:01:08 -0400291static int btrfs_sync_fs(struct super_block *sb, int wait)
292{
293 struct btrfs_trans_handle *trans;
294 struct btrfs_root *root;
295 int ret;
Chris Masond98237b2007-03-28 13:57:48 -0400296 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -0400297
Chris Masond5719762007-03-23 10:01:08 -0400298 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400299 if (!wait) {
Chris Mason7cfcc172007-04-02 14:53:59 -0400300 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400301 return 0;
302 }
Chris Masone9d0b132007-08-10 14:06:19 -0400303 btrfs_clean_old_snapshots(root);
Chris Masond561c022007-03-23 19:47:49 -0400304 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone9d0b132007-08-10 14:06:19 -0400305 btrfs_defrag_dirty_roots(root->fs_info);
Chris Masond5719762007-03-23 10:01:08 -0400306 trans = btrfs_start_transaction(root, 1);
307 ret = btrfs_commit_transaction(trans, root);
308 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400309 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason54aa1f42007-06-22 14:16:25 -0400310 return ret;
Chris Masond5719762007-03-23 10:01:08 -0400311}
312
Chris Masond561c022007-03-23 19:47:49 -0400313static void btrfs_write_super(struct super_block *sb)
314{
Chris Mason08607c12007-06-08 15:33:54 -0400315 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400316}
317
Chris Masona061fc82008-05-07 11:43:44 -0400318static int btrfs_test_super(struct super_block *s, void *data)
Chris Mason2e635a22007-03-21 11:12:56 -0400319{
Chris Masona061fc82008-05-07 11:43:44 -0400320 struct btrfs_fs_devices *test_fs_devices = data;
321 struct btrfs_root *root = btrfs_sb(s);
Yan4b82d6e2007-08-29 09:11:44 -0400322
Chris Masona061fc82008-05-07 11:43:44 -0400323 return root->fs_info->fs_devices == test_fs_devices;
Yan4b82d6e2007-08-29 09:11:44 -0400324}
325
326int btrfs_get_sb_bdev(struct file_system_type *fs_type,
327 int flags, const char *dev_name, void *data,
Yan4b82d6e2007-08-29 09:11:44 -0400328 struct vfsmount *mnt, const char *subvol)
329{
330 struct block_device *bdev = NULL;
331 struct super_block *s;
332 struct dentry *root;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400333 struct btrfs_fs_devices *fs_devices = NULL;
Yan4b82d6e2007-08-29 09:11:44 -0400334 int error = 0;
335
Chris Mason8a4b83c2008-03-24 15:02:07 -0400336 error = btrfs_scan_one_device(dev_name, flags, fs_type, &fs_devices);
337 if (error)
338 return error;
Yan4b82d6e2007-08-29 09:11:44 -0400339
Chris Mason8a4b83c2008-03-24 15:02:07 -0400340 error = btrfs_open_devices(fs_devices, flags, fs_type);
341 if (error)
342 return error;
343
344 bdev = fs_devices->lowest_bdev;
Chris Masona061fc82008-05-07 11:43:44 -0400345 btrfs_lock_volumes();
346 s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices);
347 btrfs_unlock_volumes();
Yan4b82d6e2007-08-29 09:11:44 -0400348 if (IS_ERR(s))
349 goto error_s;
350
351 if (s->s_root) {
352 if ((flags ^ s->s_flags) & MS_RDONLY) {
353 up_write(&s->s_umount);
354 deactivate_super(s);
355 error = -EBUSY;
356 goto error_bdev;
357 }
358
Yan4b82d6e2007-08-29 09:11:44 -0400359 } else {
360 char b[BDEVNAME_SIZE];
361
362 s->s_flags = flags;
363 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
Chris Mason8a4b83c2008-03-24 15:02:07 -0400364 error = btrfs_fill_super(s, fs_devices, data,
365 flags & MS_SILENT ? 1 : 0);
Yan4b82d6e2007-08-29 09:11:44 -0400366 if (error) {
367 up_write(&s->s_umount);
368 deactivate_super(s);
369 goto error;
370 }
371
Chris Mason788f20e2008-04-28 15:29:42 -0400372 btrfs_sb(s)->fs_info->bdev_holder = fs_type;
Yan4b82d6e2007-08-29 09:11:44 -0400373 s->s_flags |= MS_ACTIVE;
374 }
375
376 if (subvol) {
377 root = lookup_one_len(subvol, s->s_root, strlen(subvol));
378 if (IS_ERR(root)) {
379 up_write(&s->s_umount);
380 deactivate_super(s);
381 error = PTR_ERR(root);
382 goto error;
383 }
384 if (!root->d_inode) {
385 dput(root);
386 up_write(&s->s_umount);
387 deactivate_super(s);
388 error = -ENXIO;
389 goto error;
390 }
391 } else {
392 root = dget(s->s_root);
393 }
394
395 mnt->mnt_sb = s;
396 mnt->mnt_root = root;
397 return 0;
398
399error_s:
400 error = PTR_ERR(s);
401error_bdev:
Chris Mason8a4b83c2008-03-24 15:02:07 -0400402 btrfs_close_devices(fs_devices);
Yan4b82d6e2007-08-29 09:11:44 -0400403error:
404 return error;
405}
406/* end copy & paste */
407
408static int btrfs_get_sb(struct file_system_type *fs_type,
Chris Mason95e05282007-08-29 09:11:44 -0400409 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Yan4b82d6e2007-08-29 09:11:44 -0400410{
411 int ret;
Chris Mason95e05282007-08-29 09:11:44 -0400412 char *subvol_name = NULL;
Yan4b82d6e2007-08-29 09:11:44 -0400413
Chris Mason95e05282007-08-29 09:11:44 -0400414 parse_options((char *)data, NULL, &subvol_name);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400415 ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data, mnt,
Yan4b82d6e2007-08-29 09:11:44 -0400416 subvol_name ? subvol_name : "default");
Chris Masonc59f8952007-12-17 20:14:04 -0500417 if (subvol_name)
418 kfree(subvol_name);
Yan4b82d6e2007-08-29 09:11:44 -0400419 return ret;
Chris Mason2e635a22007-03-21 11:12:56 -0400420}
421
Chris Mason8fd17792007-04-19 21:01:03 -0400422static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
423{
424 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
Chris Mason4b52dff2007-06-26 10:06:50 -0400425 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
Chris Masondb945352007-10-15 16:15:53 -0400426 int bits = dentry->d_sb->s_blocksize_bits;
Chris Mason8fd17792007-04-19 21:01:03 -0400427
428 buf->f_namelen = BTRFS_NAME_LEN;
Chris Masondb945352007-10-15 16:15:53 -0400429 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
430 buf->f_bfree = buf->f_blocks -
431 (btrfs_super_bytes_used(disk_super) >> bits);
Chris Mason8fd17792007-04-19 21:01:03 -0400432 buf->f_bavail = buf->f_bfree;
433 buf->f_bsize = dentry->d_sb->s_blocksize;
434 buf->f_type = BTRFS_SUPER_MAGIC;
435 return 0;
436}
Chris Masonb5133862007-04-24 11:52:22 -0400437
Chris Mason2e635a22007-03-21 11:12:56 -0400438static struct file_system_type btrfs_fs_type = {
439 .owner = THIS_MODULE,
440 .name = "btrfs",
441 .get_sb = btrfs_get_sb,
Chris Masona061fc82008-05-07 11:43:44 -0400442 .kill_sb = kill_anon_super,
Chris Mason2e635a22007-03-21 11:12:56 -0400443 .fs_flags = FS_REQUIRES_DEV,
444};
Chris Masona9218f62008-03-24 15:02:04 -0400445
Chris Mason8a4b83c2008-03-24 15:02:07 -0400446static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
447 unsigned long arg)
448{
449 struct btrfs_ioctl_vol_args *vol;
450 struct btrfs_fs_devices *fs_devices;
451 int ret;
452 int len;
453
454 vol = kmalloc(sizeof(*vol), GFP_KERNEL);
455 if (copy_from_user(vol, (void __user *)arg, sizeof(*vol))) {
456 ret = -EFAULT;
457 goto out;
458 }
459 len = strnlen(vol->name, BTRFS_PATH_NAME_MAX);
460 switch (cmd) {
461 case BTRFS_IOC_SCAN_DEV:
462 ret = btrfs_scan_one_device(vol->name, MS_RDONLY,
463 &btrfs_fs_type, &fs_devices);
464 break;
465 }
466out:
467 kfree(vol);
468 return 0;
469}
470
Yaned0dab62008-01-22 12:46:56 -0500471static void btrfs_write_super_lockfs(struct super_block *sb)
472{
473 struct btrfs_root *root = btrfs_sb(sb);
474 btrfs_transaction_flush_work(root);
475}
476
477static void btrfs_unlockfs(struct super_block *sb)
478{
479 struct btrfs_root *root = btrfs_sb(sb);
480 btrfs_transaction_queue_work(root, HZ * 30);
481}
Chris Mason2e635a22007-03-21 11:12:56 -0400482
Chris Masone20d96d2007-03-22 12:13:20 -0400483static struct super_operations btrfs_super_ops = {
Chris Mason134e9732007-03-25 13:44:56 -0400484 .delete_inode = btrfs_delete_inode,
Chris Mason2da98f02008-01-16 11:44:43 -0500485 .put_inode = btrfs_put_inode,
Chris Masone20d96d2007-03-22 12:13:20 -0400486 .put_super = btrfs_put_super,
Chris Masond5719762007-03-23 10:01:08 -0400487 .write_super = btrfs_write_super,
488 .sync_fs = btrfs_sync_fs,
Chris Mason6885f302008-02-20 16:11:05 -0500489#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
490 .read_inode = btrfs_read_locked_inode,
491#else
492 .show_options = generic_show_options,
493#endif
Chris Mason4730a4b2007-03-26 12:00:39 -0400494 .write_inode = btrfs_write_inode,
Chris Masonb5133862007-04-24 11:52:22 -0400495 .dirty_inode = btrfs_dirty_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -0400496 .alloc_inode = btrfs_alloc_inode,
497 .destroy_inode = btrfs_destroy_inode,
Chris Mason8fd17792007-04-19 21:01:03 -0400498 .statfs = btrfs_statfs,
Yaned0dab62008-01-22 12:46:56 -0500499 .write_super_lockfs = btrfs_write_super_lockfs,
500 .unlockfs = btrfs_unlockfs,
Chris Masone20d96d2007-03-22 12:13:20 -0400501};
Chris Masona9218f62008-03-24 15:02:04 -0400502
503static const struct file_operations btrfs_ctl_fops = {
504 .unlocked_ioctl = btrfs_control_ioctl,
505 .compat_ioctl = btrfs_control_ioctl,
506 .owner = THIS_MODULE,
507};
508
509static struct miscdevice btrfs_misc = {
510 .minor = MISC_DYNAMIC_MINOR,
511 .name = "btrfs-control",
512 .fops = &btrfs_ctl_fops
513};
514
515static int btrfs_interface_init(void)
516{
517 return misc_register(&btrfs_misc);
518}
519
520void btrfs_interface_exit(void)
521{
522 if (misc_deregister(&btrfs_misc) < 0)
523 printk("misc_deregister failed for control device");
524}
525
Chris Mason2e635a22007-03-21 11:12:56 -0400526static int __init init_btrfs_fs(void)
527{
Chris Mason2c90e5d2007-04-02 10:50:19 -0400528 int err;
Josef Bacik58176a92007-08-29 15:47:34 -0400529
530 err = btrfs_init_sysfs();
531 if (err)
532 return err;
533
Chris Mason08607c12007-06-08 15:33:54 -0400534 btrfs_init_transaction_sys();
Chris Mason39279cc2007-06-12 06:35:45 -0400535 err = btrfs_init_cachep();
Chris Mason2c90e5d2007-04-02 10:50:19 -0400536 if (err)
Wyatt Banks2f4cbe62007-11-19 10:22:33 -0500537 goto free_transaction_sys;
Chris Masond1310b22008-01-24 16:13:08 -0500538
539 err = extent_io_init();
Wyatt Banks2f4cbe62007-11-19 10:22:33 -0500540 if (err)
541 goto free_cachep;
542
Chris Masond1310b22008-01-24 16:13:08 -0500543 err = extent_map_init();
544 if (err)
545 goto free_extent_io;
546
Chris Masona9218f62008-03-24 15:02:04 -0400547 err = btrfs_interface_init();
Wyatt Banks2f4cbe62007-11-19 10:22:33 -0500548 if (err)
549 goto free_extent_map;
Chris Masona9218f62008-03-24 15:02:04 -0400550 err = register_filesystem(&btrfs_fs_type);
551 if (err)
552 goto unregister_ioctl;
Wyatt Banks2f4cbe62007-11-19 10:22:33 -0500553 return 0;
554
Chris Masona9218f62008-03-24 15:02:04 -0400555unregister_ioctl:
556 btrfs_interface_exit();
Wyatt Banks2f4cbe62007-11-19 10:22:33 -0500557free_extent_map:
558 extent_map_exit();
Chris Masond1310b22008-01-24 16:13:08 -0500559free_extent_io:
560 extent_io_exit();
Wyatt Banks2f4cbe62007-11-19 10:22:33 -0500561free_cachep:
562 btrfs_destroy_cachep();
563free_transaction_sys:
564 btrfs_exit_transaction_sys();
565 btrfs_exit_sysfs();
566 return err;
Chris Mason2e635a22007-03-21 11:12:56 -0400567}
568
569static void __exit exit_btrfs_fs(void)
570{
Chris Mason08607c12007-06-08 15:33:54 -0400571 btrfs_exit_transaction_sys();
Chris Mason39279cc2007-06-12 06:35:45 -0400572 btrfs_destroy_cachep();
Chris Masona52d9a82007-08-27 16:49:44 -0400573 extent_map_exit();
Chris Masond1310b22008-01-24 16:13:08 -0500574 extent_io_exit();
Chris Masona9218f62008-03-24 15:02:04 -0400575 btrfs_interface_exit();
Chris Mason2e635a22007-03-21 11:12:56 -0400576 unregister_filesystem(&btrfs_fs_type);
Josef Bacik58176a92007-08-29 15:47:34 -0400577 btrfs_exit_sysfs();
Chris Mason8a4b83c2008-03-24 15:02:07 -0400578 btrfs_cleanup_fs_uuids();
Chris Mason2e635a22007-03-21 11:12:56 -0400579}
580
581module_init(init_btrfs_fs)
582module_exit(exit_btrfs_fs)
583
584MODULE_LICENSE("GPL");