blob: 67ed216df4755c750c3d27f4fe4259710f80ea67 [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 Mason2e635a22007-03-21 11:12:56 -040047
Chris Mason5f39d392007-10-15 16:14:19 -040048#define BTRFS_SUPER_MAGIC 0x9123683E
Chris Masone20d96d2007-03-22 12:13:20 -040049
Chris Masone20d96d2007-03-22 12:13:20 -040050static struct super_operations btrfs_super_ops;
Chris Masone20d96d2007-03-22 12:13:20 -040051
52static void btrfs_put_super (struct super_block * sb)
53{
54 struct btrfs_root *root = btrfs_sb(sb);
Josef Bacik58176a92007-08-29 15:47:34 -040055 struct btrfs_fs_info *fs = root->fs_info;
Chris Masone20d96d2007-03-22 12:13:20 -040056 int ret;
57
58 ret = close_ctree(root);
59 if (ret) {
60 printk("close ctree returns %d\n", ret);
61 }
Josef Bacik58176a92007-08-29 15:47:34 -040062 btrfs_sysfs_del_super(fs);
Chris Masone20d96d2007-03-22 12:13:20 -040063 sb->s_fs_info = NULL;
64}
Chris Mason2e635a22007-03-21 11:12:56 -040065
Chris Mason95e05282007-08-29 09:11:44 -040066enum {
Chris Mason8f662a72008-01-02 10:01:11 -050067 Opt_subvol, Opt_nodatasum, Opt_nodatacow, Opt_max_extent,
Chris Mason6f568d32008-01-29 16:03:38 -050068 Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, Opt_err,
Chris Mason95e05282007-08-29 09:11:44 -040069};
70
71static match_table_t tokens = {
72 {Opt_subvol, "subvol=%s"},
Chris Masonb6cda9b2007-12-14 15:30:32 -050073 {Opt_nodatasum, "nodatasum"},
Chris Masonbe20aa92007-12-17 20:14:01 -050074 {Opt_nodatacow, "nodatacow"},
Chris Mason21ad10c2008-01-09 09:23:21 -050075 {Opt_nobarrier, "nobarrier"},
Chris Masonc59f8952007-12-17 20:14:04 -050076 {Opt_max_extent, "max_extent=%s"},
Chris Mason6f568d32008-01-29 16:03:38 -050077 {Opt_max_inline, "max_inline=%s"},
Chris Mason8f662a72008-01-02 10:01:11 -050078 {Opt_alloc_start, "alloc_start=%s"},
Chris Masone18e4802008-01-18 10:54:22 -050079 {Opt_ssd, "ssd"},
Chris Mason95e05282007-08-29 09:11:44 -040080 {Opt_err, NULL}
81};
82
Chris Masonedbd8d42007-12-21 16:27:24 -050083u64 btrfs_parse_size(char *str)
Chris Masonc59f8952007-12-17 20:14:04 -050084{
Chris Masonedbd8d42007-12-21 16:27:24 -050085 u64 res;
Chris Masonc59f8952007-12-17 20:14:04 -050086 int mult = 1;
87 char *end;
88 char last;
89
90 res = simple_strtoul(str, &end, 10);
91
92 last = end[0];
93 if (isalpha(last)) {
94 last = tolower(last);
95 switch (last) {
96 case 'g':
97 mult *= 1024;
98 case 'm':
99 mult *= 1024;
100 case 'k':
101 mult *= 1024;
102 }
103 res = res * mult;
104 }
105 return res;
106}
107
Chris Mason95e05282007-08-29 09:11:44 -0400108static int parse_options (char * options,
109 struct btrfs_root *root,
110 char **subvol_name)
111{
112 char * p;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500113 struct btrfs_fs_info *info = NULL;
Chris Mason95e05282007-08-29 09:11:44 -0400114 substring_t args[MAX_OPT_ARGS];
Chris Masonb6cda9b2007-12-14 15:30:32 -0500115
Chris Mason95e05282007-08-29 09:11:44 -0400116 if (!options)
117 return 1;
118
Chris Masonbe20aa92007-12-17 20:14:01 -0500119 /*
120 * strsep changes the string, duplicate it because parse_options
121 * gets called twice
122 */
123 options = kstrdup(options, GFP_NOFS);
124 if (!options)
125 return -ENOMEM;
126
127 if (root)
128 info = root->fs_info;
129
Chris Mason95e05282007-08-29 09:11:44 -0400130 while ((p = strsep (&options, ",")) != NULL) {
131 int token;
132 if (!*p)
133 continue;
134
135 token = match_token(p, tokens, args);
136 switch (token) {
137 case Opt_subvol:
Chris Masonbe20aa92007-12-17 20:14:01 -0500138 if (subvol_name) {
Chris Masonb6cda9b2007-12-14 15:30:32 -0500139 *subvol_name = match_strdup(&args[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500140 }
Chris Masonb6cda9b2007-12-14 15:30:32 -0500141 break;
142 case Opt_nodatasum:
Chris Masonbe20aa92007-12-17 20:14:01 -0500143 if (info) {
144 printk("btrfs: setting nodatacsum\n");
Chris Masonb6cda9b2007-12-14 15:30:32 -0500145 btrfs_set_opt(info->mount_opt, NODATASUM);
Chris Masonbe20aa92007-12-17 20:14:01 -0500146 }
147 break;
148 case Opt_nodatacow:
149 if (info) {
150 printk("btrfs: setting nodatacow\n");
151 btrfs_set_opt(info->mount_opt, NODATACOW);
152 btrfs_set_opt(info->mount_opt, NODATASUM);
153 }
Chris Mason95e05282007-08-29 09:11:44 -0400154 break;
Chris Masone18e4802008-01-18 10:54:22 -0500155 case Opt_ssd:
156 if (info) {
157 printk("btrfs: use ssd allocation scheme\n");
158 btrfs_set_opt(info->mount_opt, SSD);
159 }
160 break;
Chris Mason21ad10c2008-01-09 09:23:21 -0500161 case Opt_nobarrier:
162 if (info) {
163 printk("btrfs: turning off barriers\n");
164 btrfs_set_opt(info->mount_opt, NOBARRIER);
165 }
166 break;
Chris Masonc59f8952007-12-17 20:14:04 -0500167 case Opt_max_extent:
168 if (info) {
169 char *num = match_strdup(&args[0]);
170 if (num) {
Chris Masonedbd8d42007-12-21 16:27:24 -0500171 info->max_extent =
172 btrfs_parse_size(num);
Chris Masonc59f8952007-12-17 20:14:04 -0500173 kfree(num);
174
175 info->max_extent = max_t(u64,
176 info->max_extent,
177 root->sectorsize);
178 printk("btrfs: max_extent at %Lu\n",
179 info->max_extent);
180 }
181 }
182 break;
Chris Mason6f568d32008-01-29 16:03:38 -0500183 case Opt_max_inline:
184 if (info) {
185 char *num = match_strdup(&args[0]);
186 if (num) {
187 info->max_inline =
188 btrfs_parse_size(num);
189 kfree(num);
190
191 info->max_inline = max_t(u64,
192 info->max_inline,
193 root->sectorsize);
194 printk("btrfs: max_inline at %Lu\n",
195 info->max_inline);
196 }
197 }
198 break;
Chris Mason8f662a72008-01-02 10:01:11 -0500199 case Opt_alloc_start:
200 if (info) {
201 char *num = match_strdup(&args[0]);
202 if (num) {
203 info->alloc_start =
204 btrfs_parse_size(num);
205 kfree(num);
206 printk("btrfs: allocations start at "
207 "%Lu\n", info->alloc_start);
208 }
209 }
210 break;
Chris Mason95e05282007-08-29 09:11:44 -0400211 default:
Chris Masonbe20aa92007-12-17 20:14:01 -0500212 break;
Chris Mason95e05282007-08-29 09:11:44 -0400213 }
214 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500215 kfree(options);
Chris Mason95e05282007-08-29 09:11:44 -0400216 return 1;
217}
218
Chris Mason2e635a22007-03-21 11:12:56 -0400219static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
220{
221 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400222 struct dentry * root_dentry;
223 struct btrfs_super_block *disk_super;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400224 struct btrfs_root *tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -0400225 struct btrfs_inode *bi;
Chris Mason39279cc2007-06-12 06:35:45 -0400226 int err;
Chris Mason2e635a22007-03-21 11:12:56 -0400227
228 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -0400229 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400230 sb->s_op = &btrfs_super_ops;
Josef Bacik5103e942007-11-16 11:45:54 -0500231 sb->s_xattr = btrfs_xattr_handlers;
Chris Mason2e635a22007-03-21 11:12:56 -0400232 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400233
Chris Mason0f7d52f2007-04-09 10:42:37 -0400234 tree_root = open_ctree(sb);
Chris Masond98237b2007-03-28 13:57:48 -0400235
Chris Mason39279cc2007-06-12 06:35:45 -0400236 if (!tree_root || IS_ERR(tree_root)) {
Chris Masone20d96d2007-03-22 12:13:20 -0400237 printk("btrfs: open_ctree failed\n");
238 return -EIO;
239 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400240 sb->s_fs_info = tree_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400241 disk_super = &tree_root->fs_info->super_copy;
Chris Masonc5739bb2007-04-10 09:27:04 -0400242 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
243 tree_root);
Chris Masond6e4a422007-04-06 15:37:36 -0400244 bi = BTRFS_I(inode);
245 bi->location.objectid = inode->i_ino;
246 bi->location.offset = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400247 bi->root = tree_root;
Chris Masonb888db22007-08-27 16:49:44 -0400248
Chris Masond6e4a422007-04-06 15:37:36 -0400249 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
250
Chris Mason39279cc2007-06-12 06:35:45 -0400251 if (!inode) {
252 err = -ENOMEM;
253 goto fail_close;
254 }
Chris Masone20d96d2007-03-22 12:13:20 -0400255 if (inode->i_state & I_NEW) {
256 btrfs_read_locked_inode(inode);
257 unlock_new_inode(inode);
258 }
Chris Mason2e635a22007-03-21 11:12:56 -0400259
Chris Masone20d96d2007-03-22 12:13:20 -0400260 root_dentry = d_alloc_root(inode);
261 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400262 iput(inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400263 err = -ENOMEM;
264 goto fail_close;
Chris Mason2e635a22007-03-21 11:12:56 -0400265 }
Josef Bacik58176a92007-08-29 15:47:34 -0400266
Chris Masonb6cda9b2007-12-14 15:30:32 -0500267 parse_options((char *)data, tree_root, NULL);
268
Josef Bacik58176a92007-08-29 15:47:34 -0400269 /* this does the super kobj at the same time */
270 err = btrfs_sysfs_add_super(tree_root->fs_info);
271 if (err)
272 goto fail_close;
273
Chris Masone20d96d2007-03-22 12:13:20 -0400274 sb->s_root = root_dentry;
Chris Mason08607c12007-06-08 15:33:54 -0400275 btrfs_transaction_queue_work(tree_root, HZ * 30);
Chris Mason6885f302008-02-20 16:11:05 -0500276
277#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
278 save_mount_options(sb, data);
279#endif
280
Chris Mason2e635a22007-03-21 11:12:56 -0400281 return 0;
Chris Mason2e635a22007-03-21 11:12:56 -0400282
Chris Mason39279cc2007-06-12 06:35:45 -0400283fail_close:
284 close_ctree(tree_root);
Chris Masond5719762007-03-23 10:01:08 -0400285 return err;
286}
287
Chris Masond5719762007-03-23 10:01:08 -0400288static int btrfs_sync_fs(struct super_block *sb, int wait)
289{
290 struct btrfs_trans_handle *trans;
291 struct btrfs_root *root;
292 int ret;
Chris Masond98237b2007-03-28 13:57:48 -0400293 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -0400294
Chris Masond5719762007-03-23 10:01:08 -0400295 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400296 if (!wait) {
Chris Mason7cfcc172007-04-02 14:53:59 -0400297 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400298 return 0;
299 }
Chris Masone9d0b132007-08-10 14:06:19 -0400300 btrfs_clean_old_snapshots(root);
Chris Masond561c022007-03-23 19:47:49 -0400301 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone9d0b132007-08-10 14:06:19 -0400302 btrfs_defrag_dirty_roots(root->fs_info);
Chris Masond5719762007-03-23 10:01:08 -0400303 trans = btrfs_start_transaction(root, 1);
304 ret = btrfs_commit_transaction(trans, root);
305 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400306 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason54aa1f42007-06-22 14:16:25 -0400307 return ret;
Chris Masond5719762007-03-23 10:01:08 -0400308}
309
Chris Masond561c022007-03-23 19:47:49 -0400310static void btrfs_write_super(struct super_block *sb)
311{
Chris Mason08607c12007-06-08 15:33:54 -0400312 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400313}
314
Yan4b82d6e2007-08-29 09:11:44 -0400315/*
316 * This is almost a copy of get_sb_bdev in fs/super.c.
317 * We need the local copy to allow direct mounting of
318 * subvolumes, but this could be easily integrated back
319 * into the generic version. --hch
320 */
321
322/* start copy & paste */
323static int set_bdev_super(struct super_block *s, void *data)
Chris Mason2e635a22007-03-21 11:12:56 -0400324{
Yan4b82d6e2007-08-29 09:11:44 -0400325 s->s_bdev = data;
326 s->s_dev = s->s_bdev->bd_dev;
327 return 0;
328}
329
330static int test_bdev_super(struct super_block *s, void *data)
331{
332 return (void *)s->s_bdev == data;
333}
334
335int btrfs_get_sb_bdev(struct file_system_type *fs_type,
336 int flags, const char *dev_name, void *data,
337 int (*fill_super)(struct super_block *, void *, int),
338 struct vfsmount *mnt, const char *subvol)
339{
340 struct block_device *bdev = NULL;
341 struct super_block *s;
342 struct dentry *root;
343 int error = 0;
344
345 bdev = open_bdev_excl(dev_name, flags, fs_type);
346 if (IS_ERR(bdev))
347 return PTR_ERR(bdev);
348
349 /*
350 * once the super is inserted into the list by sget, s_umount
351 * will protect the lockfs code from trying to start a snapshot
352 * while we are mounting
353 */
354 down(&bdev->bd_mount_sem);
355 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
356 up(&bdev->bd_mount_sem);
357 if (IS_ERR(s))
358 goto error_s;
359
360 if (s->s_root) {
361 if ((flags ^ s->s_flags) & MS_RDONLY) {
362 up_write(&s->s_umount);
363 deactivate_super(s);
364 error = -EBUSY;
365 goto error_bdev;
366 }
367
368 close_bdev_excl(bdev);
369 } else {
370 char b[BDEVNAME_SIZE];
371
372 s->s_flags = flags;
373 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
374 sb_set_blocksize(s, block_size(bdev));
375 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
376 if (error) {
377 up_write(&s->s_umount);
378 deactivate_super(s);
379 goto error;
380 }
381
382 s->s_flags |= MS_ACTIVE;
383 }
384
385 if (subvol) {
386 root = lookup_one_len(subvol, s->s_root, strlen(subvol));
387 if (IS_ERR(root)) {
388 up_write(&s->s_umount);
389 deactivate_super(s);
390 error = PTR_ERR(root);
391 goto error;
392 }
393 if (!root->d_inode) {
394 dput(root);
395 up_write(&s->s_umount);
396 deactivate_super(s);
397 error = -ENXIO;
398 goto error;
399 }
400 } else {
401 root = dget(s->s_root);
402 }
403
404 mnt->mnt_sb = s;
405 mnt->mnt_root = root;
406 return 0;
407
408error_s:
409 error = PTR_ERR(s);
410error_bdev:
411 close_bdev_excl(bdev);
412error:
413 return error;
414}
415/* end copy & paste */
416
417static int btrfs_get_sb(struct file_system_type *fs_type,
Chris Mason95e05282007-08-29 09:11:44 -0400418 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Yan4b82d6e2007-08-29 09:11:44 -0400419{
420 int ret;
Chris Mason95e05282007-08-29 09:11:44 -0400421 char *subvol_name = NULL;
Yan4b82d6e2007-08-29 09:11:44 -0400422
Chris Mason95e05282007-08-29 09:11:44 -0400423 parse_options((char *)data, NULL, &subvol_name);
Yan4b82d6e2007-08-29 09:11:44 -0400424 ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data,
425 btrfs_fill_super, mnt,
426 subvol_name ? subvol_name : "default");
Chris Masonc59f8952007-12-17 20:14:04 -0500427 if (subvol_name)
428 kfree(subvol_name);
Yan4b82d6e2007-08-29 09:11:44 -0400429 return ret;
Chris Mason2e635a22007-03-21 11:12:56 -0400430}
431
Chris Mason8fd17792007-04-19 21:01:03 -0400432static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
433{
434 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
Chris Mason4b52dff2007-06-26 10:06:50 -0400435 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
Chris Masondb945352007-10-15 16:15:53 -0400436 int bits = dentry->d_sb->s_blocksize_bits;
Chris Mason8fd17792007-04-19 21:01:03 -0400437
438 buf->f_namelen = BTRFS_NAME_LEN;
Chris Masondb945352007-10-15 16:15:53 -0400439 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
440 buf->f_bfree = buf->f_blocks -
441 (btrfs_super_bytes_used(disk_super) >> bits);
Chris Mason8fd17792007-04-19 21:01:03 -0400442 buf->f_bavail = buf->f_bfree;
443 buf->f_bsize = dentry->d_sb->s_blocksize;
444 buf->f_type = BTRFS_SUPER_MAGIC;
445 return 0;
446}
Chris Masonb5133862007-04-24 11:52:22 -0400447
Chris Masona9218f62008-03-24 15:02:04 -0400448static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
449 unsigned long arg)
450{
451 printk("btrfs control ioctl %d\n", cmd);
452 return 0;
453}
454
Chris Mason2e635a22007-03-21 11:12:56 -0400455static struct file_system_type btrfs_fs_type = {
456 .owner = THIS_MODULE,
457 .name = "btrfs",
458 .get_sb = btrfs_get_sb,
459 .kill_sb = kill_block_super,
460 .fs_flags = FS_REQUIRES_DEV,
461};
Chris Masona9218f62008-03-24 15:02:04 -0400462
Yaned0dab62008-01-22 12:46:56 -0500463static void btrfs_write_super_lockfs(struct super_block *sb)
464{
465 struct btrfs_root *root = btrfs_sb(sb);
466 btrfs_transaction_flush_work(root);
467}
468
469static void btrfs_unlockfs(struct super_block *sb)
470{
471 struct btrfs_root *root = btrfs_sb(sb);
472 btrfs_transaction_queue_work(root, HZ * 30);
473}
Chris Mason2e635a22007-03-21 11:12:56 -0400474
Chris Masone20d96d2007-03-22 12:13:20 -0400475static struct super_operations btrfs_super_ops = {
Chris Mason134e9732007-03-25 13:44:56 -0400476 .delete_inode = btrfs_delete_inode,
Chris Mason2da98f02008-01-16 11:44:43 -0500477 .put_inode = btrfs_put_inode,
Chris Masone20d96d2007-03-22 12:13:20 -0400478 .put_super = btrfs_put_super,
Chris Masond5719762007-03-23 10:01:08 -0400479 .write_super = btrfs_write_super,
480 .sync_fs = btrfs_sync_fs,
Chris Mason6885f302008-02-20 16:11:05 -0500481#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
482 .read_inode = btrfs_read_locked_inode,
483#else
484 .show_options = generic_show_options,
485#endif
Chris Mason4730a4b2007-03-26 12:00:39 -0400486 .write_inode = btrfs_write_inode,
Chris Masonb5133862007-04-24 11:52:22 -0400487 .dirty_inode = btrfs_dirty_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -0400488 .alloc_inode = btrfs_alloc_inode,
489 .destroy_inode = btrfs_destroy_inode,
Chris Mason8fd17792007-04-19 21:01:03 -0400490 .statfs = btrfs_statfs,
Yaned0dab62008-01-22 12:46:56 -0500491 .write_super_lockfs = btrfs_write_super_lockfs,
492 .unlockfs = btrfs_unlockfs,
Chris Masone20d96d2007-03-22 12:13:20 -0400493};
Chris Masona9218f62008-03-24 15:02:04 -0400494
495static const struct file_operations btrfs_ctl_fops = {
496 .unlocked_ioctl = btrfs_control_ioctl,
497 .compat_ioctl = btrfs_control_ioctl,
498 .owner = THIS_MODULE,
499};
500
501static struct miscdevice btrfs_misc = {
502 .minor = MISC_DYNAMIC_MINOR,
503 .name = "btrfs-control",
504 .fops = &btrfs_ctl_fops
505};
506
507static int btrfs_interface_init(void)
508{
509 return misc_register(&btrfs_misc);
510}
511
512void btrfs_interface_exit(void)
513{
514 if (misc_deregister(&btrfs_misc) < 0)
515 printk("misc_deregister failed for control device");
516}
517
Chris Mason2e635a22007-03-21 11:12:56 -0400518static int __init init_btrfs_fs(void)
519{
Chris Mason2c90e5d2007-04-02 10:50:19 -0400520 int err;
Josef Bacik58176a92007-08-29 15:47:34 -0400521
522 err = btrfs_init_sysfs();
523 if (err)
524 return err;
525
Chris Mason08607c12007-06-08 15:33:54 -0400526 btrfs_init_transaction_sys();
Chris Mason39279cc2007-06-12 06:35:45 -0400527 err = btrfs_init_cachep();
Chris Mason2c90e5d2007-04-02 10:50:19 -0400528 if (err)
Wyatt Banks2f4cbe62007-11-19 10:22:33 -0500529 goto free_transaction_sys;
Chris Masond1310b22008-01-24 16:13:08 -0500530
531 err = extent_io_init();
Wyatt Banks2f4cbe62007-11-19 10:22:33 -0500532 if (err)
533 goto free_cachep;
534
Chris Masond1310b22008-01-24 16:13:08 -0500535 err = extent_map_init();
536 if (err)
537 goto free_extent_io;
538
Chris Masona9218f62008-03-24 15:02:04 -0400539 err = btrfs_interface_init();
Wyatt Banks2f4cbe62007-11-19 10:22:33 -0500540 if (err)
541 goto free_extent_map;
Chris Masona9218f62008-03-24 15:02:04 -0400542 err = register_filesystem(&btrfs_fs_type);
543 if (err)
544 goto unregister_ioctl;
Wyatt Banks2f4cbe62007-11-19 10:22:33 -0500545 return 0;
546
Chris Masona9218f62008-03-24 15:02:04 -0400547unregister_ioctl:
548 btrfs_interface_exit();
Wyatt Banks2f4cbe62007-11-19 10:22:33 -0500549free_extent_map:
550 extent_map_exit();
Chris Masond1310b22008-01-24 16:13:08 -0500551free_extent_io:
552 extent_io_exit();
Wyatt Banks2f4cbe62007-11-19 10:22:33 -0500553free_cachep:
554 btrfs_destroy_cachep();
555free_transaction_sys:
556 btrfs_exit_transaction_sys();
557 btrfs_exit_sysfs();
558 return err;
Chris Mason2e635a22007-03-21 11:12:56 -0400559}
560
561static void __exit exit_btrfs_fs(void)
562{
Chris Mason08607c12007-06-08 15:33:54 -0400563 btrfs_exit_transaction_sys();
Chris Mason39279cc2007-06-12 06:35:45 -0400564 btrfs_destroy_cachep();
Chris Masona52d9a82007-08-27 16:49:44 -0400565 extent_map_exit();
Chris Masond1310b22008-01-24 16:13:08 -0500566 extent_io_exit();
Chris Masona9218f62008-03-24 15:02:04 -0400567 btrfs_interface_exit();
Chris Mason2e635a22007-03-21 11:12:56 -0400568 unregister_filesystem(&btrfs_fs_type);
Josef Bacik58176a92007-08-29 15:47:34 -0400569 btrfs_exit_sysfs();
Chris Mason2e635a22007-03-21 11:12:56 -0400570}
571
572module_init(init_btrfs_fs)
573module_exit(exit_btrfs_fs)
574
575MODULE_LICENSE("GPL");