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"
51 #define BTRFS_SUPER_MAGIC 0x9123683E
53 static struct super_operations btrfs_super_ops;
55 static void btrfs_put_super (struct super_block * sb)
57 struct btrfs_root *root = btrfs_sb(sb);
58 struct btrfs_fs_info *fs = root->fs_info;
61 ret = close_ctree(root);
63 printk("close ctree returns %d\n", ret);
65 btrfs_sysfs_del_super(fs);
70 Opt_degraded, Opt_subvol, Opt_device, Opt_nodatasum, Opt_nodatacow,
71 Opt_max_extent, Opt_max_inline, Opt_alloc_start, Opt_nobarrier,
72 Opt_ssd, Opt_thread_pool, Opt_noacl, Opt_err,
75 static match_table_t tokens = {
76 {Opt_degraded, "degraded"},
77 {Opt_subvol, "subvol=%s"},
78 {Opt_device, "device=%s"},
79 {Opt_nodatasum, "nodatasum"},
80 {Opt_nodatacow, "nodatacow"},
81 {Opt_nobarrier, "nobarrier"},
82 {Opt_max_extent, "max_extent=%s"},
83 {Opt_max_inline, "max_inline=%s"},
84 {Opt_alloc_start, "alloc_start=%s"},
85 {Opt_thread_pool, "thread_pool=%d"},
91 u64 btrfs_parse_size(char *str)
98 res = simple_strtoul(str, &end, 10);
102 last = tolower(last);
117 * Regular mount options parser. Everything that is needed only when
118 * reading in a new superblock is parsed here.
120 int btrfs_parse_options(struct btrfs_root *root, char *options)
122 struct btrfs_fs_info *info = root->fs_info;
123 substring_t args[MAX_OPT_ARGS];
131 * strsep changes the string, duplicate it because parse_options
134 options = kstrdup(options, GFP_NOFS);
139 while ((p = strsep(&options, ",")) != NULL) {
144 token = match_token(p, tokens, args);
147 printk(KERN_INFO "btrfs: allowing degraded mounts\n");
148 btrfs_set_opt(info->mount_opt, DEGRADED);
153 * These are parsed by btrfs_parse_early_options
154 * and can be happily ignored here.
158 printk(KERN_INFO "btrfs: setting nodatacsum\n");
159 btrfs_set_opt(info->mount_opt, NODATASUM);
162 printk(KERN_INFO "btrfs: setting nodatacow\n");
163 btrfs_set_opt(info->mount_opt, NODATACOW);
164 btrfs_set_opt(info->mount_opt, NODATASUM);
167 printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
168 btrfs_set_opt(info->mount_opt, SSD);
171 printk(KERN_INFO "btrfs: turning off barriers\n");
172 btrfs_set_opt(info->mount_opt, NOBARRIER);
174 case Opt_thread_pool:
176 match_int(&args[0], &intarg);
178 info->thread_pool_size = intarg;
179 printk(KERN_INFO "btrfs: thread pool %d\n",
180 info->thread_pool_size);
184 num = match_strdup(&args[0]);
186 info->max_extent = btrfs_parse_size(num);
189 info->max_extent = max_t(u64,
190 info->max_extent, root->sectorsize);
191 printk(KERN_INFO "btrfs: max_extent at %llu\n",
196 num = match_strdup(&args[0]);
198 info->max_inline = btrfs_parse_size(num);
201 if (info->max_inline) {
202 info->max_inline = max_t(u64,
206 printk(KERN_INFO "btrfs: max_inline at %llu\n",
210 case Opt_alloc_start:
211 num = match_strdup(&args[0]);
213 info->alloc_start = btrfs_parse_size(num);
216 "btrfs: allocations start at %llu\n",
221 root->fs_info->sb->s_flags &= ~MS_POSIXACL;
232 * Parse mount options that are required early in the mount process.
234 * All other options will be parsed on much later in the mount process and
235 * only when we need to allocate a new super block.
237 static int btrfs_parse_early_options(const char *options, int flags,
238 void *holder, char **subvol_name,
239 struct btrfs_fs_devices **fs_devices)
241 substring_t args[MAX_OPT_ARGS];
249 * strsep changes the string, duplicate it because parse_options
252 opts = kstrdup(options, GFP_KERNEL);
256 while ((p = strsep(&opts, ",")) != NULL) {
261 token = match_token(p, tokens, args);
264 *subvol_name = match_strdup(&args[0]);
267 error = btrfs_scan_one_device(match_strdup(&args[0]),
268 flags, holder, fs_devices);
281 * If no subvolume name is specified we use the default one. Allocate
282 * a copy of the string "default" here so that code later in the
283 * mount path doesn't care if it's the default volume or another one.
286 *subvol_name = kstrdup("default", GFP_KERNEL);
293 static int btrfs_fill_super(struct super_block * sb,
294 struct btrfs_fs_devices *fs_devices,
295 void * data, int silent)
297 struct inode * inode;
298 struct dentry * root_dentry;
299 struct btrfs_super_block *disk_super;
300 struct btrfs_root *tree_root;
301 struct btrfs_inode *bi;
304 sb->s_maxbytes = MAX_LFS_FILESIZE;
305 sb->s_magic = BTRFS_SUPER_MAGIC;
306 sb->s_op = &btrfs_super_ops;
307 sb->s_export_op = &btrfs_export_ops;
308 sb->s_xattr = btrfs_xattr_handlers;
310 sb->s_flags |= MS_POSIXACL;
312 tree_root = open_ctree(sb, fs_devices, (char *)data);
314 if (IS_ERR(tree_root)) {
315 printk("btrfs: open_ctree failed\n");
316 return PTR_ERR(tree_root);
318 sb->s_fs_info = tree_root;
319 disk_super = &tree_root->fs_info->super_copy;
320 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
323 bi->location.objectid = inode->i_ino;
324 bi->location.offset = 0;
325 bi->root = tree_root;
327 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
333 if (inode->i_state & I_NEW) {
334 btrfs_read_locked_inode(inode);
335 unlock_new_inode(inode);
338 root_dentry = d_alloc_root(inode);
345 /* this does the super kobj at the same time */
346 err = btrfs_sysfs_add_super(tree_root->fs_info);
350 sb->s_root = root_dentry;
352 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
353 save_mount_options(sb, data);
359 close_ctree(tree_root);
363 int btrfs_sync_fs(struct super_block *sb, int wait)
365 struct btrfs_trans_handle *trans;
366 struct btrfs_root *root;
372 filemap_flush(root->fs_info->btree_inode->i_mapping);
375 btrfs_clean_old_snapshots(root);
376 trans = btrfs_start_transaction(root, 1);
377 ret = btrfs_commit_transaction(trans, root);
382 static void btrfs_write_super(struct super_block *sb)
387 static int btrfs_test_super(struct super_block *s, void *data)
389 struct btrfs_fs_devices *test_fs_devices = data;
390 struct btrfs_root *root = btrfs_sb(s);
392 return root->fs_info->fs_devices == test_fs_devices;
396 * Find a superblock for the given device / mount point.
398 * Note: This is based on get_sb_bdev from fs/super.c with a few additions
399 * for multiple device setup. Make sure to keep it in sync.
401 static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
402 const char *dev_name, void *data, struct vfsmount *mnt)
404 char *subvol_name = NULL;
405 struct block_device *bdev = NULL;
406 struct super_block *s;
408 struct btrfs_fs_devices *fs_devices = NULL;
411 error = btrfs_parse_early_options(data, flags, fs_type,
412 &subvol_name, &fs_devices);
416 error = btrfs_scan_one_device(dev_name, flags, fs_type, &fs_devices);
418 goto error_free_subvol_name;
420 error = btrfs_open_devices(fs_devices, flags, fs_type);
422 goto error_free_subvol_name;
424 bdev = fs_devices->latest_bdev;
425 s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices);
430 if ((flags ^ s->s_flags) & MS_RDONLY) {
431 up_write(&s->s_umount);
438 char b[BDEVNAME_SIZE];
441 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
442 error = btrfs_fill_super(s, fs_devices, data,
443 flags & MS_SILENT ? 1 : 0);
445 up_write(&s->s_umount);
450 btrfs_sb(s)->fs_info->bdev_holder = fs_type;
451 s->s_flags |= MS_ACTIVE;
454 mutex_lock(&s->s_root->d_inode->i_mutex);
455 root = lookup_one_len(subvol_name, s->s_root, strlen(subvol_name));
456 mutex_unlock(&s->s_root->d_inode->i_mutex);
458 up_write(&s->s_umount);
460 error = PTR_ERR(root);
463 if (!root->d_inode) {
465 up_write(&s->s_umount);
472 mnt->mnt_root = root;
480 btrfs_close_devices(fs_devices);
481 error_free_subvol_name:
487 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
489 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
490 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
491 int bits = dentry->d_sb->s_blocksize_bits;
493 buf->f_namelen = BTRFS_NAME_LEN;
494 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
495 buf->f_bfree = buf->f_blocks -
496 (btrfs_super_bytes_used(disk_super) >> bits);
497 buf->f_bavail = buf->f_bfree;
498 buf->f_bsize = dentry->d_sb->s_blocksize;
499 buf->f_type = BTRFS_SUPER_MAGIC;
503 static struct file_system_type btrfs_fs_type = {
504 .owner = THIS_MODULE,
506 .get_sb = btrfs_get_sb,
507 .kill_sb = kill_anon_super,
508 .fs_flags = FS_REQUIRES_DEV,
511 static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
514 struct btrfs_ioctl_vol_args *vol;
515 struct btrfs_fs_devices *fs_devices;
519 vol = kmalloc(sizeof(*vol), GFP_KERNEL);
520 if (copy_from_user(vol, (void __user *)arg, sizeof(*vol))) {
524 len = strnlen(vol->name, BTRFS_PATH_NAME_MAX);
526 case BTRFS_IOC_SCAN_DEV:
527 ret = btrfs_scan_one_device(vol->name, MS_RDONLY,
528 &btrfs_fs_type, &fs_devices);
536 static void btrfs_write_super_lockfs(struct super_block *sb)
538 struct btrfs_root *root = btrfs_sb(sb);
539 mutex_lock(&root->fs_info->transaction_kthread_mutex);
540 mutex_lock(&root->fs_info->cleaner_mutex);
543 static void btrfs_unlockfs(struct super_block *sb)
545 struct btrfs_root *root = btrfs_sb(sb);
546 mutex_unlock(&root->fs_info->cleaner_mutex);
547 mutex_unlock(&root->fs_info->transaction_kthread_mutex);
550 static struct super_operations btrfs_super_ops = {
551 .delete_inode = btrfs_delete_inode,
552 .put_super = btrfs_put_super,
553 .write_super = btrfs_write_super,
554 .sync_fs = btrfs_sync_fs,
555 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
556 .read_inode = btrfs_read_locked_inode,
558 .show_options = generic_show_options,
560 .write_inode = btrfs_write_inode,
561 .dirty_inode = btrfs_dirty_inode,
562 .alloc_inode = btrfs_alloc_inode,
563 .destroy_inode = btrfs_destroy_inode,
564 .statfs = btrfs_statfs,
565 .write_super_lockfs = btrfs_write_super_lockfs,
566 .unlockfs = btrfs_unlockfs,
569 static const struct file_operations btrfs_ctl_fops = {
570 .unlocked_ioctl = btrfs_control_ioctl,
571 .compat_ioctl = btrfs_control_ioctl,
572 .owner = THIS_MODULE,
575 static struct miscdevice btrfs_misc = {
576 .minor = MISC_DYNAMIC_MINOR,
577 .name = "btrfs-control",
578 .fops = &btrfs_ctl_fops
581 static int btrfs_interface_init(void)
583 return misc_register(&btrfs_misc);
586 void btrfs_interface_exit(void)
588 if (misc_deregister(&btrfs_misc) < 0)
589 printk("misc_deregister failed for control device");
592 static int __init init_btrfs_fs(void)
596 err = btrfs_init_sysfs();
600 err = btrfs_init_cachep();
604 err = extent_io_init();
608 err = extent_map_init();
612 err = btrfs_interface_init();
614 goto free_extent_map;
615 err = register_filesystem(&btrfs_fs_type);
617 goto unregister_ioctl;
619 printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
623 btrfs_interface_exit();
629 btrfs_destroy_cachep();
635 static void __exit exit_btrfs_fs(void)
637 btrfs_destroy_cachep();
640 btrfs_interface_exit();
641 unregister_filesystem(&btrfs_fs_type);
643 btrfs_cleanup_fs_uuids();
646 module_init(init_btrfs_fs)
647 module_exit(exit_btrfs_fs)
649 MODULE_LICENSE("GPL");