2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #include <linux/blkdev.h>
20 #include <linux/module.h>
21 #include <linux/buffer_head.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>
30 #include <linux/mount.h>
31 #include <linux/mpage.h>
32 #include <linux/swap.h>
33 #include <linux/writeback.h>
34 #include <linux/statfs.h>
35 #include <linux/compat.h>
36 #include <linux/parser.h>
37 #include <linux/ctype.h>
38 #include <linux/namei.h>
39 #include <linux/miscdevice.h>
42 #include "transaction.h"
43 #include "btrfs_inode.h"
45 #include "print-tree.h"
48 #define BTRFS_SUPER_MAGIC 0x9123683E
50 static struct super_operations btrfs_super_ops
;
52 static void btrfs_put_super (struct super_block
* sb
)
54 struct btrfs_root
*root
= btrfs_sb(sb
);
55 struct btrfs_fs_info
*fs
= root
->fs_info
;
58 ret
= close_ctree(root
);
60 printk("close ctree returns %d\n", ret
);
62 btrfs_sysfs_del_super(fs
);
67 Opt_subvol
, Opt_nodatasum
, Opt_nodatacow
, Opt_max_extent
,
68 Opt_max_inline
, Opt_alloc_start
, Opt_nobarrier
, Opt_ssd
, Opt_err
,
71 static match_table_t tokens
= {
72 {Opt_subvol
, "subvol=%s"},
73 {Opt_nodatasum
, "nodatasum"},
74 {Opt_nodatacow
, "nodatacow"},
75 {Opt_nobarrier
, "nobarrier"},
76 {Opt_max_extent
, "max_extent=%s"},
77 {Opt_max_inline
, "max_inline=%s"},
78 {Opt_alloc_start
, "alloc_start=%s"},
83 u64
btrfs_parse_size(char *str
)
90 res
= simple_strtoul(str
, &end
, 10);
108 static int parse_options (char * options
,
109 struct btrfs_root
*root
,
113 struct btrfs_fs_info
*info
= NULL
;
114 substring_t args
[MAX_OPT_ARGS
];
120 * strsep changes the string, duplicate it because parse_options
123 options
= kstrdup(options
, GFP_NOFS
);
128 info
= root
->fs_info
;
130 while ((p
= strsep (&options
, ",")) != NULL
) {
135 token
= match_token(p
, tokens
, args
);
139 *subvol_name
= match_strdup(&args
[0]);
144 printk("btrfs: setting nodatacsum\n");
145 btrfs_set_opt(info
->mount_opt
, NODATASUM
);
150 printk("btrfs: setting nodatacow\n");
151 btrfs_set_opt(info
->mount_opt
, NODATACOW
);
152 btrfs_set_opt(info
->mount_opt
, NODATASUM
);
157 printk("btrfs: use ssd allocation scheme\n");
158 btrfs_set_opt(info
->mount_opt
, SSD
);
163 printk("btrfs: turning off barriers\n");
164 btrfs_set_opt(info
->mount_opt
, NOBARRIER
);
169 char *num
= match_strdup(&args
[0]);
172 btrfs_parse_size(num
);
175 info
->max_extent
= max_t(u64
,
178 printk("btrfs: max_extent at %Lu\n",
185 char *num
= match_strdup(&args
[0]);
188 btrfs_parse_size(num
);
191 info
->max_inline
= max_t(u64
,
194 printk("btrfs: max_inline at %Lu\n",
199 case Opt_alloc_start
:
201 char *num
= match_strdup(&args
[0]);
204 btrfs_parse_size(num
);
206 printk("btrfs: allocations start at "
207 "%Lu\n", info
->alloc_start
);
219 static int btrfs_fill_super(struct super_block
* sb
, void * data
, int silent
)
221 struct inode
* inode
;
222 struct dentry
* root_dentry
;
223 struct btrfs_super_block
*disk_super
;
224 struct btrfs_root
*tree_root
;
225 struct btrfs_inode
*bi
;
228 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
229 sb
->s_magic
= BTRFS_SUPER_MAGIC
;
230 sb
->s_op
= &btrfs_super_ops
;
231 sb
->s_xattr
= btrfs_xattr_handlers
;
234 tree_root
= open_ctree(sb
);
236 if (!tree_root
|| IS_ERR(tree_root
)) {
237 printk("btrfs: open_ctree failed\n");
240 sb
->s_fs_info
= tree_root
;
241 disk_super
= &tree_root
->fs_info
->super_copy
;
242 inode
= btrfs_iget_locked(sb
, btrfs_super_root_dir(disk_super
),
245 bi
->location
.objectid
= inode
->i_ino
;
246 bi
->location
.offset
= 0;
247 bi
->root
= tree_root
;
249 btrfs_set_key_type(&bi
->location
, BTRFS_INODE_ITEM_KEY
);
255 if (inode
->i_state
& I_NEW
) {
256 btrfs_read_locked_inode(inode
);
257 unlock_new_inode(inode
);
260 root_dentry
= d_alloc_root(inode
);
267 parse_options((char *)data
, tree_root
, NULL
);
269 /* this does the super kobj at the same time */
270 err
= btrfs_sysfs_add_super(tree_root
->fs_info
);
274 sb
->s_root
= root_dentry
;
275 btrfs_transaction_queue_work(tree_root
, HZ
* 30);
277 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
278 save_mount_options(sb
, data
);
284 close_ctree(tree_root
);
288 static int btrfs_sync_fs(struct super_block
*sb
, int wait
)
290 struct btrfs_trans_handle
*trans
;
291 struct btrfs_root
*root
;
297 filemap_flush(root
->fs_info
->btree_inode
->i_mapping
);
300 btrfs_clean_old_snapshots(root
);
301 mutex_lock(&root
->fs_info
->fs_mutex
);
302 btrfs_defrag_dirty_roots(root
->fs_info
);
303 trans
= btrfs_start_transaction(root
, 1);
304 ret
= btrfs_commit_transaction(trans
, root
);
306 mutex_unlock(&root
->fs_info
->fs_mutex
);
310 static void btrfs_write_super(struct super_block
*sb
)
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
322 /* start copy & paste */
323 static int set_bdev_super(struct super_block
*s
, void *data
)
326 s
->s_dev
= s
->s_bdev
->bd_dev
;
330 static int test_bdev_super(struct super_block
*s
, void *data
)
332 return (void *)s
->s_bdev
== data
;
335 int 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
)
340 struct block_device
*bdev
= NULL
;
341 struct super_block
*s
;
345 bdev
= open_bdev_excl(dev_name
, flags
, fs_type
);
347 return PTR_ERR(bdev
);
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
354 down(&bdev
->bd_mount_sem
);
355 s
= sget(fs_type
, test_bdev_super
, set_bdev_super
, bdev
);
356 up(&bdev
->bd_mount_sem
);
361 if ((flags
^ s
->s_flags
) & MS_RDONLY
) {
362 up_write(&s
->s_umount
);
368 close_bdev_excl(bdev
);
370 char b
[BDEVNAME_SIZE
];
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);
377 up_write(&s
->s_umount
);
382 s
->s_flags
|= MS_ACTIVE
;
386 root
= lookup_one_len(subvol
, s
->s_root
, strlen(subvol
));
388 up_write(&s
->s_umount
);
390 error
= PTR_ERR(root
);
393 if (!root
->d_inode
) {
395 up_write(&s
->s_umount
);
401 root
= dget(s
->s_root
);
405 mnt
->mnt_root
= root
;
411 close_bdev_excl(bdev
);
415 /* end copy & paste */
417 static int btrfs_get_sb(struct file_system_type
*fs_type
,
418 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
421 char *subvol_name
= NULL
;
423 parse_options((char *)data
, NULL
, &subvol_name
);
424 ret
= btrfs_get_sb_bdev(fs_type
, flags
, dev_name
, data
,
425 btrfs_fill_super
, mnt
,
426 subvol_name
? subvol_name
: "default");
432 static int btrfs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
434 struct btrfs_root
*root
= btrfs_sb(dentry
->d_sb
);
435 struct btrfs_super_block
*disk_super
= &root
->fs_info
->super_copy
;
436 int bits
= dentry
->d_sb
->s_blocksize_bits
;
438 buf
->f_namelen
= BTRFS_NAME_LEN
;
439 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
);
442 buf
->f_bavail
= buf
->f_bfree
;
443 buf
->f_bsize
= dentry
->d_sb
->s_blocksize
;
444 buf
->f_type
= BTRFS_SUPER_MAGIC
;
448 static long btrfs_control_ioctl(struct file
*file
, unsigned int cmd
,
451 printk("btrfs control ioctl %d\n", cmd
);
455 static struct file_system_type btrfs_fs_type
= {
456 .owner
= THIS_MODULE
,
458 .get_sb
= btrfs_get_sb
,
459 .kill_sb
= kill_block_super
,
460 .fs_flags
= FS_REQUIRES_DEV
,
463 static void btrfs_write_super_lockfs(struct super_block
*sb
)
465 struct btrfs_root
*root
= btrfs_sb(sb
);
466 btrfs_transaction_flush_work(root
);
469 static void btrfs_unlockfs(struct super_block
*sb
)
471 struct btrfs_root
*root
= btrfs_sb(sb
);
472 btrfs_transaction_queue_work(root
, HZ
* 30);
475 static struct super_operations btrfs_super_ops
= {
476 .delete_inode
= btrfs_delete_inode
,
477 .put_inode
= btrfs_put_inode
,
478 .put_super
= btrfs_put_super
,
479 .write_super
= btrfs_write_super
,
480 .sync_fs
= btrfs_sync_fs
,
481 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
482 .read_inode
= btrfs_read_locked_inode
,
484 .show_options
= generic_show_options
,
486 .write_inode
= btrfs_write_inode
,
487 .dirty_inode
= btrfs_dirty_inode
,
488 .alloc_inode
= btrfs_alloc_inode
,
489 .destroy_inode
= btrfs_destroy_inode
,
490 .statfs
= btrfs_statfs
,
491 .write_super_lockfs
= btrfs_write_super_lockfs
,
492 .unlockfs
= btrfs_unlockfs
,
495 static const struct file_operations btrfs_ctl_fops
= {
496 .unlocked_ioctl
= btrfs_control_ioctl
,
497 .compat_ioctl
= btrfs_control_ioctl
,
498 .owner
= THIS_MODULE
,
501 static struct miscdevice btrfs_misc
= {
502 .minor
= MISC_DYNAMIC_MINOR
,
503 .name
= "btrfs-control",
504 .fops
= &btrfs_ctl_fops
507 static int btrfs_interface_init(void)
509 return misc_register(&btrfs_misc
);
512 void btrfs_interface_exit(void)
514 if (misc_deregister(&btrfs_misc
) < 0)
515 printk("misc_deregister failed for control device");
518 static int __init
init_btrfs_fs(void)
522 err
= btrfs_init_sysfs();
526 btrfs_init_transaction_sys();
527 err
= btrfs_init_cachep();
529 goto free_transaction_sys
;
531 err
= extent_io_init();
535 err
= extent_map_init();
539 err
= btrfs_interface_init();
541 goto free_extent_map
;
542 err
= register_filesystem(&btrfs_fs_type
);
544 goto unregister_ioctl
;
548 btrfs_interface_exit();
554 btrfs_destroy_cachep();
555 free_transaction_sys
:
556 btrfs_exit_transaction_sys();
561 static void __exit
exit_btrfs_fs(void)
563 btrfs_exit_transaction_sys();
564 btrfs_destroy_cachep();
567 btrfs_interface_exit();
568 unregister_filesystem(&btrfs_fs_type
);
572 module_init(init_btrfs_fs
)
573 module_exit(exit_btrfs_fs
)
575 MODULE_LICENSE("GPL");