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"
49 #define BTRFS_SUPER_MAGIC 0x9123683E
51 static struct super_operations btrfs_super_ops;
53 static void btrfs_put_super (struct super_block * sb)
55 struct btrfs_root *root = btrfs_sb(sb);
56 struct btrfs_fs_info *fs = root->fs_info;
59 ret = close_ctree(root);
61 printk("close ctree returns %d\n", ret);
63 btrfs_sysfs_del_super(fs);
68 Opt_subvol, Opt_nodatasum, Opt_nodatacow, Opt_max_extent,
69 Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, Opt_err,
72 static match_table_t tokens = {
73 {Opt_subvol, "subvol=%s"},
74 {Opt_nodatasum, "nodatasum"},
75 {Opt_nodatacow, "nodatacow"},
76 {Opt_nobarrier, "nobarrier"},
77 {Opt_max_extent, "max_extent=%s"},
78 {Opt_max_inline, "max_inline=%s"},
79 {Opt_alloc_start, "alloc_start=%s"},
84 u64 btrfs_parse_size(char *str)
91 res = simple_strtoul(str, &end, 10);
109 static int parse_options (char * options,
110 struct btrfs_root *root,
114 struct btrfs_fs_info *info = NULL;
115 substring_t args[MAX_OPT_ARGS];
121 * strsep changes the string, duplicate it because parse_options
124 options = kstrdup(options, GFP_NOFS);
129 info = root->fs_info;
131 while ((p = strsep (&options, ",")) != NULL) {
136 token = match_token(p, tokens, args);
140 *subvol_name = match_strdup(&args[0]);
145 printk("btrfs: setting nodatacsum\n");
146 btrfs_set_opt(info->mount_opt, NODATASUM);
151 printk("btrfs: setting nodatacow\n");
152 btrfs_set_opt(info->mount_opt, NODATACOW);
153 btrfs_set_opt(info->mount_opt, NODATASUM);
158 printk("btrfs: use ssd allocation scheme\n");
159 btrfs_set_opt(info->mount_opt, SSD);
164 printk("btrfs: turning off barriers\n");
165 btrfs_set_opt(info->mount_opt, NOBARRIER);
170 char *num = match_strdup(&args[0]);
173 btrfs_parse_size(num);
176 info->max_extent = max_t(u64,
179 printk("btrfs: max_extent at %Lu\n",
186 char *num = match_strdup(&args[0]);
189 btrfs_parse_size(num);
192 info->max_inline = max_t(u64,
195 printk("btrfs: max_inline at %Lu\n",
200 case Opt_alloc_start:
202 char *num = match_strdup(&args[0]);
205 btrfs_parse_size(num);
207 printk("btrfs: allocations start at "
208 "%Lu\n", info->alloc_start);
220 static int btrfs_fill_super(struct super_block * sb,
221 struct btrfs_fs_devices *fs_devices,
222 void * data, int silent)
224 struct inode * inode;
225 struct dentry * root_dentry;
226 struct btrfs_super_block *disk_super;
227 struct btrfs_root *tree_root;
228 struct btrfs_inode *bi;
231 sb->s_maxbytes = MAX_LFS_FILESIZE;
232 sb->s_magic = BTRFS_SUPER_MAGIC;
233 sb->s_op = &btrfs_super_ops;
234 sb->s_xattr = btrfs_xattr_handlers;
237 tree_root = open_ctree(sb, fs_devices);
239 if (IS_ERR(tree_root)) {
240 printk("btrfs: open_ctree failed\n");
241 return PTR_ERR(tree_root);
243 sb->s_fs_info = tree_root;
244 disk_super = &tree_root->fs_info->super_copy;
245 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
248 bi->location.objectid = inode->i_ino;
249 bi->location.offset = 0;
250 bi->root = tree_root;
252 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
258 if (inode->i_state & I_NEW) {
259 btrfs_read_locked_inode(inode);
260 unlock_new_inode(inode);
263 root_dentry = d_alloc_root(inode);
270 parse_options((char *)data, tree_root, NULL);
272 /* this does the super kobj at the same time */
273 err = btrfs_sysfs_add_super(tree_root->fs_info);
277 sb->s_root = root_dentry;
278 btrfs_transaction_queue_work(tree_root, HZ * 30);
280 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
281 save_mount_options(sb, data);
287 close_ctree(tree_root);
291 static int btrfs_sync_fs(struct super_block *sb, int wait)
293 struct btrfs_trans_handle *trans;
294 struct btrfs_root *root;
300 filemap_flush(root->fs_info->btree_inode->i_mapping);
303 btrfs_clean_old_snapshots(root);
304 mutex_lock(&root->fs_info->fs_mutex);
305 btrfs_defrag_dirty_roots(root->fs_info);
306 trans = btrfs_start_transaction(root, 1);
307 ret = btrfs_commit_transaction(trans, root);
309 mutex_unlock(&root->fs_info->fs_mutex);
313 static void btrfs_write_super(struct super_block *sb)
318 static int btrfs_test_super(struct super_block *s, void *data)
320 struct btrfs_fs_devices *test_fs_devices = data;
321 struct btrfs_root *root = btrfs_sb(s);
323 return root->fs_info->fs_devices == test_fs_devices;
326 int btrfs_get_sb_bdev(struct file_system_type *fs_type,
327 int flags, const char *dev_name, void *data,
328 struct vfsmount *mnt, const char *subvol)
330 struct block_device *bdev = NULL;
331 struct super_block *s;
333 struct btrfs_fs_devices *fs_devices = NULL;
336 error = btrfs_scan_one_device(dev_name, flags, fs_type, &fs_devices);
340 error = btrfs_open_devices(fs_devices, flags, fs_type);
344 bdev = fs_devices->lowest_bdev;
345 btrfs_lock_volumes();
346 s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices);
347 btrfs_unlock_volumes();
352 if ((flags ^ s->s_flags) & MS_RDONLY) {
353 up_write(&s->s_umount);
360 char b[BDEVNAME_SIZE];
363 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
364 error = btrfs_fill_super(s, fs_devices, data,
365 flags & MS_SILENT ? 1 : 0);
367 up_write(&s->s_umount);
372 btrfs_sb(s)->fs_info->bdev_holder = fs_type;
373 s->s_flags |= MS_ACTIVE;
377 root = lookup_one_len(subvol, s->s_root, strlen(subvol));
379 up_write(&s->s_umount);
381 error = PTR_ERR(root);
384 if (!root->d_inode) {
386 up_write(&s->s_umount);
392 root = dget(s->s_root);
396 mnt->mnt_root = root;
402 btrfs_close_devices(fs_devices);
406 /* end copy & paste */
408 static int btrfs_get_sb(struct file_system_type *fs_type,
409 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
412 char *subvol_name = NULL;
414 parse_options((char *)data, NULL, &subvol_name);
415 ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data, mnt,
416 subvol_name ? subvol_name : "default");
422 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
424 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
425 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
426 int bits = dentry->d_sb->s_blocksize_bits;
428 buf->f_namelen = BTRFS_NAME_LEN;
429 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);
432 buf->f_bavail = buf->f_bfree;
433 buf->f_bsize = dentry->d_sb->s_blocksize;
434 buf->f_type = BTRFS_SUPER_MAGIC;
438 static struct file_system_type btrfs_fs_type = {
439 .owner = THIS_MODULE,
441 .get_sb = btrfs_get_sb,
442 .kill_sb = kill_anon_super,
443 .fs_flags = FS_REQUIRES_DEV,
446 static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
449 struct btrfs_ioctl_vol_args *vol;
450 struct btrfs_fs_devices *fs_devices;
454 vol = kmalloc(sizeof(*vol), GFP_KERNEL);
455 if (copy_from_user(vol, (void __user *)arg, sizeof(*vol))) {
459 len = strnlen(vol->name, BTRFS_PATH_NAME_MAX);
461 case BTRFS_IOC_SCAN_DEV:
462 ret = btrfs_scan_one_device(vol->name, MS_RDONLY,
463 &btrfs_fs_type, &fs_devices);
471 static void btrfs_write_super_lockfs(struct super_block *sb)
473 struct btrfs_root *root = btrfs_sb(sb);
474 btrfs_transaction_flush_work(root);
477 static void btrfs_unlockfs(struct super_block *sb)
479 struct btrfs_root *root = btrfs_sb(sb);
480 btrfs_transaction_queue_work(root, HZ * 30);
483 static struct super_operations btrfs_super_ops = {
484 .delete_inode = btrfs_delete_inode,
485 .put_inode = btrfs_put_inode,
486 .put_super = btrfs_put_super,
487 .write_super = btrfs_write_super,
488 .sync_fs = btrfs_sync_fs,
489 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
490 .read_inode = btrfs_read_locked_inode,
492 .show_options = generic_show_options,
494 .write_inode = btrfs_write_inode,
495 .dirty_inode = btrfs_dirty_inode,
496 .alloc_inode = btrfs_alloc_inode,
497 .destroy_inode = btrfs_destroy_inode,
498 .statfs = btrfs_statfs,
499 .write_super_lockfs = btrfs_write_super_lockfs,
500 .unlockfs = btrfs_unlockfs,
503 static const struct file_operations btrfs_ctl_fops = {
504 .unlocked_ioctl = btrfs_control_ioctl,
505 .compat_ioctl = btrfs_control_ioctl,
506 .owner = THIS_MODULE,
509 static struct miscdevice btrfs_misc = {
510 .minor = MISC_DYNAMIC_MINOR,
511 .name = "btrfs-control",
512 .fops = &btrfs_ctl_fops
515 static int btrfs_interface_init(void)
517 return misc_register(&btrfs_misc);
520 void btrfs_interface_exit(void)
522 if (misc_deregister(&btrfs_misc) < 0)
523 printk("misc_deregister failed for control device");
526 static int __init init_btrfs_fs(void)
530 err = btrfs_init_sysfs();
534 btrfs_init_transaction_sys();
535 err = btrfs_init_cachep();
537 goto free_transaction_sys;
539 err = extent_io_init();
543 err = extent_map_init();
547 err = btrfs_interface_init();
549 goto free_extent_map;
550 err = register_filesystem(&btrfs_fs_type);
552 goto unregister_ioctl;
556 btrfs_interface_exit();
562 btrfs_destroy_cachep();
563 free_transaction_sys:
564 btrfs_exit_transaction_sys();
569 static void __exit exit_btrfs_fs(void)
571 btrfs_exit_transaction_sys();
572 btrfs_destroy_cachep();
575 btrfs_interface_exit();
576 unregister_filesystem(&btrfs_fs_type);
578 btrfs_cleanup_fs_uuids();
581 module_init(init_btrfs_fs)
582 module_exit(exit_btrfs_fs)
584 MODULE_LICENSE("GPL");