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/seq_file.h>
28 #include <linux/string.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>
40 #include <linux/magic.h>
44 #include "transaction.h"
45 #include "btrfs_inode.h"
47 #include "print-tree.h"
52 #include "compression.h"
54 static const struct super_operations btrfs_super_ops;
56 static void btrfs_put_super(struct super_block *sb)
58 struct btrfs_root *root = btrfs_sb(sb);
61 ret = close_ctree(root);
66 Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
67 Opt_nodatacow, Opt_max_extent, Opt_max_inline, Opt_alloc_start,
68 Opt_nobarrier, Opt_ssd, Opt_nossd, Opt_ssd_spread, Opt_thread_pool,
69 Opt_noacl, Opt_compress, Opt_compress_force, Opt_notreelog, Opt_ratio,
74 static match_table_t tokens = {
75 {Opt_degraded, "degraded"},
76 {Opt_subvol, "subvol=%s"},
77 {Opt_subvolid, "subvolid=%d"},
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"},
86 {Opt_compress, "compress"},
87 {Opt_compress_force, "compress-force"},
89 {Opt_ssd_spread, "ssd_spread"},
92 {Opt_notreelog, "notreelog"},
93 {Opt_flushoncommit, "flushoncommit"},
94 {Opt_ratio, "metadata_ratio=%d"},
95 {Opt_discard, "discard"},
99 u64 btrfs_parse_size(char *str)
106 res = simple_strtoul(str, &end, 10);
110 last = tolower(last);
125 * Regular mount options parser. Everything that is needed only when
126 * reading in a new superblock is parsed here.
128 int btrfs_parse_options(struct btrfs_root *root, char *options)
130 struct btrfs_fs_info *info = root->fs_info;
131 substring_t args[MAX_OPT_ARGS];
132 char *p, *num, *orig;
140 * strsep changes the string, duplicate it because parse_options
143 options = kstrdup(options, GFP_NOFS);
149 while ((p = strsep(&options, ",")) != NULL) {
154 token = match_token(p, tokens, args);
157 printk(KERN_INFO "btrfs: allowing degraded mounts\n");
158 btrfs_set_opt(info->mount_opt, DEGRADED);
164 * These are parsed by btrfs_parse_early_options
165 * and can be happily ignored here.
169 printk(KERN_INFO "btrfs: setting nodatasum\n");
170 btrfs_set_opt(info->mount_opt, NODATASUM);
173 printk(KERN_INFO "btrfs: setting nodatacow\n");
174 btrfs_set_opt(info->mount_opt, NODATACOW);
175 btrfs_set_opt(info->mount_opt, NODATASUM);
178 printk(KERN_INFO "btrfs: use compression\n");
179 btrfs_set_opt(info->mount_opt, COMPRESS);
181 case Opt_compress_force:
182 printk(KERN_INFO "btrfs: forcing compression\n");
183 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
184 btrfs_set_opt(info->mount_opt, COMPRESS);
187 printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
188 btrfs_set_opt(info->mount_opt, SSD);
191 printk(KERN_INFO "btrfs: use spread ssd "
192 "allocation scheme\n");
193 btrfs_set_opt(info->mount_opt, SSD);
194 btrfs_set_opt(info->mount_opt, SSD_SPREAD);
197 printk(KERN_INFO "btrfs: not using ssd allocation "
199 btrfs_set_opt(info->mount_opt, NOSSD);
200 btrfs_clear_opt(info->mount_opt, SSD);
201 btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
204 printk(KERN_INFO "btrfs: turning off barriers\n");
205 btrfs_set_opt(info->mount_opt, NOBARRIER);
207 case Opt_thread_pool:
209 match_int(&args[0], &intarg);
211 info->thread_pool_size = intarg;
212 printk(KERN_INFO "btrfs: thread pool %d\n",
213 info->thread_pool_size);
217 num = match_strdup(&args[0]);
219 info->max_extent = btrfs_parse_size(num);
222 info->max_extent = max_t(u64,
223 info->max_extent, root->sectorsize);
224 printk(KERN_INFO "btrfs: max_extent at %llu\n",
225 (unsigned long long)info->max_extent);
229 num = match_strdup(&args[0]);
231 info->max_inline = btrfs_parse_size(num);
234 if (info->max_inline) {
235 info->max_inline = max_t(u64,
239 printk(KERN_INFO "btrfs: max_inline at %llu\n",
240 (unsigned long long)info->max_inline);
243 case Opt_alloc_start:
244 num = match_strdup(&args[0]);
246 info->alloc_start = btrfs_parse_size(num);
249 "btrfs: allocations start at %llu\n",
250 (unsigned long long)info->alloc_start);
254 root->fs_info->sb->s_flags &= ~MS_POSIXACL;
257 printk(KERN_INFO "btrfs: disabling tree log\n");
258 btrfs_set_opt(info->mount_opt, NOTREELOG);
260 case Opt_flushoncommit:
261 printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
262 btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
266 match_int(&args[0], &intarg);
268 info->metadata_ratio = intarg;
269 printk(KERN_INFO "btrfs: metadata ratio %d\n",
270 info->metadata_ratio);
274 btrfs_set_opt(info->mount_opt, DISCARD);
277 printk(KERN_INFO "btrfs: unrecognized mount option "
291 * Parse mount options that are required early in the mount process.
293 * All other options will be parsed on much later in the mount process and
294 * only when we need to allocate a new super block.
296 static int btrfs_parse_early_options(const char *options, fmode_t flags,
297 void *holder, char **subvol_name, u64 *subvol_objectid,
298 struct btrfs_fs_devices **fs_devices)
300 substring_t args[MAX_OPT_ARGS];
309 * strsep changes the string, duplicate it because parse_options
312 opts = kstrdup(options, GFP_KERNEL);
316 while ((p = strsep(&opts, ",")) != NULL) {
321 token = match_token(p, tokens, args);
324 *subvol_name = match_strdup(&args[0]);
328 error = match_int(&args[0], &intarg);
330 /* we want the original fs_tree */
333 BTRFS_FS_TREE_OBJECTID;
335 *subvol_objectid = intarg;
339 error = btrfs_scan_one_device(match_strdup(&args[0]),
340 flags, holder, fs_devices);
353 * If no subvolume name is specified we use the default one. Allocate
354 * a copy of the string "." here so that code later in the
355 * mount path doesn't care if it's the default volume or another one.
358 *subvol_name = kstrdup(".", GFP_KERNEL);
365 static struct dentry *get_default_root(struct super_block *sb,
368 struct btrfs_root *root = sb->s_fs_info;
369 struct btrfs_root *new_root;
370 struct btrfs_dir_item *di;
371 struct btrfs_path *path;
372 struct btrfs_key location;
374 struct dentry *dentry;
379 * We have a specific subvol we want to mount, just setup location and
380 * go look up the root.
382 if (subvol_objectid) {
383 location.objectid = subvol_objectid;
384 location.type = BTRFS_ROOT_ITEM_KEY;
385 location.offset = (u64)-1;
389 path = btrfs_alloc_path();
391 return ERR_PTR(-ENOMEM);
392 path->leave_spinning = 1;
395 * Find the "default" dir item which points to the root item that we
396 * will mount by default if we haven't been given a specific subvolume
399 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
400 di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
403 * Ok the default dir item isn't there. This is weird since
404 * it's always been there, but don't freak out, just try and
405 * mount to root most subvolume.
407 btrfs_free_path(path);
408 dir_id = BTRFS_FIRST_FREE_OBJECTID;
409 new_root = root->fs_info->fs_root;
413 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
414 btrfs_free_path(path);
417 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
418 if (IS_ERR(new_root))
419 return ERR_PTR(PTR_ERR(new_root));
421 if (btrfs_root_refs(&new_root->root_item) == 0)
422 return ERR_PTR(-ENOENT);
424 dir_id = btrfs_root_dirid(&new_root->root_item);
426 location.objectid = dir_id;
427 location.type = BTRFS_INODE_ITEM_KEY;
430 inode = btrfs_iget(sb, &location, new_root, &new);
432 return ERR_PTR(-ENOMEM);
435 * If we're just mounting the root most subvol put the inode and return
436 * a reference to the dentry. We will have already gotten a reference
437 * to the inode in btrfs_fill_super so we're good to go.
439 if (!new && sb->s_root->d_inode == inode) {
441 return dget(sb->s_root);
445 const struct qstr name = { .name = "/", .len = 1 };
448 * New inode, we need to make the dentry a sibling of s_root so
449 * everything gets cleaned up properly on unmount.
451 dentry = d_alloc(sb->s_root, &name);
454 return ERR_PTR(-ENOMEM);
456 d_splice_alias(inode, dentry);
459 * We found the inode in cache, just find a dentry for it and
460 * put the reference to the inode we just got.
462 dentry = d_find_alias(inode);
469 static int btrfs_fill_super(struct super_block *sb,
470 struct btrfs_fs_devices *fs_devices,
471 void *data, int silent)
474 struct dentry *root_dentry;
475 struct btrfs_super_block *disk_super;
476 struct btrfs_root *tree_root;
477 struct btrfs_key key;
480 sb->s_maxbytes = MAX_LFS_FILESIZE;
481 sb->s_magic = BTRFS_SUPER_MAGIC;
482 sb->s_op = &btrfs_super_ops;
483 sb->s_export_op = &btrfs_export_ops;
484 sb->s_xattr = btrfs_xattr_handlers;
486 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
487 sb->s_flags |= MS_POSIXACL;
490 tree_root = open_ctree(sb, fs_devices, (char *)data);
492 if (IS_ERR(tree_root)) {
493 printk("btrfs: open_ctree failed\n");
494 return PTR_ERR(tree_root);
496 sb->s_fs_info = tree_root;
497 disk_super = &tree_root->fs_info->super_copy;
499 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
500 key.type = BTRFS_INODE_ITEM_KEY;
502 inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
504 err = PTR_ERR(inode);
508 root_dentry = d_alloc_root(inode);
515 sb->s_root = root_dentry;
517 save_mount_options(sb, data);
521 close_ctree(tree_root);
525 int btrfs_sync_fs(struct super_block *sb, int wait)
527 struct btrfs_trans_handle *trans;
528 struct btrfs_root *root = btrfs_sb(sb);
532 filemap_flush(root->fs_info->btree_inode->i_mapping);
536 btrfs_start_delalloc_inodes(root, 0);
537 btrfs_wait_ordered_extents(root, 0, 0);
539 trans = btrfs_start_transaction(root, 1);
540 ret = btrfs_commit_transaction(trans, root);
544 static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
546 struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
547 struct btrfs_fs_info *info = root->fs_info;
549 if (btrfs_test_opt(root, DEGRADED))
550 seq_puts(seq, ",degraded");
551 if (btrfs_test_opt(root, NODATASUM))
552 seq_puts(seq, ",nodatasum");
553 if (btrfs_test_opt(root, NODATACOW))
554 seq_puts(seq, ",nodatacow");
555 if (btrfs_test_opt(root, NOBARRIER))
556 seq_puts(seq, ",nobarrier");
557 if (info->max_extent != (u64)-1)
558 seq_printf(seq, ",max_extent=%llu",
559 (unsigned long long)info->max_extent);
560 if (info->max_inline != 8192 * 1024)
561 seq_printf(seq, ",max_inline=%llu",
562 (unsigned long long)info->max_inline);
563 if (info->alloc_start != 0)
564 seq_printf(seq, ",alloc_start=%llu",
565 (unsigned long long)info->alloc_start);
566 if (info->thread_pool_size != min_t(unsigned long,
567 num_online_cpus() + 2, 8))
568 seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
569 if (btrfs_test_opt(root, COMPRESS))
570 seq_puts(seq, ",compress");
571 if (btrfs_test_opt(root, NOSSD))
572 seq_puts(seq, ",nossd");
573 if (btrfs_test_opt(root, SSD_SPREAD))
574 seq_puts(seq, ",ssd_spread");
575 else if (btrfs_test_opt(root, SSD))
576 seq_puts(seq, ",ssd");
577 if (btrfs_test_opt(root, NOTREELOG))
578 seq_puts(seq, ",notreelog");
579 if (btrfs_test_opt(root, FLUSHONCOMMIT))
580 seq_puts(seq, ",flushoncommit");
581 if (btrfs_test_opt(root, DISCARD))
582 seq_puts(seq, ",discard");
583 if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
584 seq_puts(seq, ",noacl");
588 static int btrfs_test_super(struct super_block *s, void *data)
590 struct btrfs_fs_devices *test_fs_devices = data;
591 struct btrfs_root *root = btrfs_sb(s);
593 return root->fs_info->fs_devices == test_fs_devices;
597 * Find a superblock for the given device / mount point.
599 * Note: This is based on get_sb_bdev from fs/super.c with a few additions
600 * for multiple device setup. Make sure to keep it in sync.
602 static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
603 const char *dev_name, void *data, struct vfsmount *mnt)
605 struct block_device *bdev = NULL;
606 struct super_block *s;
608 struct btrfs_fs_devices *fs_devices = NULL;
609 fmode_t mode = FMODE_READ;
610 char *subvol_name = NULL;
611 u64 subvol_objectid = 0;
615 if (!(flags & MS_RDONLY))
618 error = btrfs_parse_early_options(data, mode, fs_type,
619 &subvol_name, &subvol_objectid,
624 error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices);
626 goto error_free_subvol_name;
628 error = btrfs_open_devices(fs_devices, mode, fs_type);
630 goto error_free_subvol_name;
632 if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
634 goto error_close_devices;
637 bdev = fs_devices->latest_bdev;
638 s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices);
643 if ((flags ^ s->s_flags) & MS_RDONLY) {
644 deactivate_locked_super(s);
646 goto error_close_devices;
650 btrfs_close_devices(fs_devices);
652 char b[BDEVNAME_SIZE];
655 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
656 error = btrfs_fill_super(s, fs_devices, data,
657 flags & MS_SILENT ? 1 : 0);
659 deactivate_locked_super(s);
660 goto error_free_subvol_name;
663 btrfs_sb(s)->fs_info->bdev_holder = fs_type;
664 s->s_flags |= MS_ACTIVE;
667 root = get_default_root(s, subvol_objectid);
669 error = PTR_ERR(root);
670 deactivate_locked_super(s);
673 /* if they gave us a subvolume name bind mount into that */
674 if (strcmp(subvol_name, ".")) {
675 struct dentry *new_root;
676 mutex_lock(&root->d_inode->i_mutex);
677 new_root = lookup_one_len(subvol_name, root,
678 strlen(subvol_name));
679 mutex_unlock(&root->d_inode->i_mutex);
681 if (IS_ERR(new_root)) {
682 deactivate_locked_super(s);
683 error = PTR_ERR(new_root);
685 goto error_close_devices;
687 if (!new_root->d_inode) {
690 deactivate_locked_super(s);
692 goto error_close_devices;
699 mnt->mnt_root = root;
707 btrfs_close_devices(fs_devices);
708 error_free_subvol_name:
714 static int btrfs_remount(struct super_block *sb, int *flags, char *data)
716 struct btrfs_root *root = btrfs_sb(sb);
719 ret = btrfs_parse_options(root, data);
723 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
726 if (*flags & MS_RDONLY) {
727 sb->s_flags |= MS_RDONLY;
729 ret = btrfs_commit_super(root);
732 if (root->fs_info->fs_devices->rw_devices == 0)
735 if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
738 /* recover relocation */
739 ret = btrfs_recover_relocation(root);
742 ret = btrfs_cleanup_fs_roots(root->fs_info);
745 sb->s_flags &= ~MS_RDONLY;
751 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
753 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
754 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
755 struct list_head *head = &root->fs_info->space_info;
756 struct btrfs_space_info *found;
759 int bits = dentry->d_sb->s_blocksize_bits;
760 __be32 *fsid = (__be32 *)root->fs_info->fsid;
763 list_for_each_entry_rcu(found, head, list) {
764 if (found->flags & (BTRFS_BLOCK_GROUP_DUP|
765 BTRFS_BLOCK_GROUP_RAID10|
766 BTRFS_BLOCK_GROUP_RAID1)) {
767 total_used += found->bytes_used;
768 if (found->flags & BTRFS_BLOCK_GROUP_DATA)
769 data_used += found->bytes_used;
771 data_used += found->total_bytes;
774 total_used += found->bytes_used;
775 if (found->flags & BTRFS_BLOCK_GROUP_DATA)
776 data_used += found->bytes_used;
778 data_used += found->total_bytes;
782 buf->f_namelen = BTRFS_NAME_LEN;
783 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
784 buf->f_bfree = buf->f_blocks - (total_used >> bits);
785 buf->f_bavail = buf->f_blocks - (data_used >> bits);
786 buf->f_bsize = dentry->d_sb->s_blocksize;
787 buf->f_type = BTRFS_SUPER_MAGIC;
789 /* We treat it as constant endianness (it doesn't matter _which_)
790 because we want the fsid to come out the same whether mounted
791 on a big-endian or little-endian host */
792 buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
793 buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
794 /* Mask in the root object ID too, to disambiguate subvols */
795 buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
796 buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
801 static struct file_system_type btrfs_fs_type = {
802 .owner = THIS_MODULE,
804 .get_sb = btrfs_get_sb,
805 .kill_sb = kill_anon_super,
806 .fs_flags = FS_REQUIRES_DEV,
810 * used by btrfsctl to scan devices when no FS is mounted
812 static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
815 struct btrfs_ioctl_vol_args *vol;
816 struct btrfs_fs_devices *fs_devices;
819 if (!capable(CAP_SYS_ADMIN))
822 vol = memdup_user((void __user *)arg, sizeof(*vol));
827 case BTRFS_IOC_SCAN_DEV:
828 ret = btrfs_scan_one_device(vol->name, FMODE_READ,
829 &btrfs_fs_type, &fs_devices);
837 static int btrfs_freeze(struct super_block *sb)
839 struct btrfs_root *root = btrfs_sb(sb);
840 mutex_lock(&root->fs_info->transaction_kthread_mutex);
841 mutex_lock(&root->fs_info->cleaner_mutex);
845 static int btrfs_unfreeze(struct super_block *sb)
847 struct btrfs_root *root = btrfs_sb(sb);
848 mutex_unlock(&root->fs_info->cleaner_mutex);
849 mutex_unlock(&root->fs_info->transaction_kthread_mutex);
853 static const struct super_operations btrfs_super_ops = {
854 .drop_inode = btrfs_drop_inode,
855 .delete_inode = btrfs_delete_inode,
856 .put_super = btrfs_put_super,
857 .sync_fs = btrfs_sync_fs,
858 .show_options = btrfs_show_options,
859 .write_inode = btrfs_write_inode,
860 .dirty_inode = btrfs_dirty_inode,
861 .alloc_inode = btrfs_alloc_inode,
862 .destroy_inode = btrfs_destroy_inode,
863 .statfs = btrfs_statfs,
864 .remount_fs = btrfs_remount,
865 .freeze_fs = btrfs_freeze,
866 .unfreeze_fs = btrfs_unfreeze,
869 static const struct file_operations btrfs_ctl_fops = {
870 .unlocked_ioctl = btrfs_control_ioctl,
871 .compat_ioctl = btrfs_control_ioctl,
872 .owner = THIS_MODULE,
875 static struct miscdevice btrfs_misc = {
876 .minor = MISC_DYNAMIC_MINOR,
877 .name = "btrfs-control",
878 .fops = &btrfs_ctl_fops
881 static int btrfs_interface_init(void)
883 return misc_register(&btrfs_misc);
886 static void btrfs_interface_exit(void)
888 if (misc_deregister(&btrfs_misc) < 0)
889 printk(KERN_INFO "misc_deregister failed for control device");
892 static int __init init_btrfs_fs(void)
896 err = btrfs_init_sysfs();
900 err = btrfs_init_cachep();
904 err = extent_io_init();
908 err = extent_map_init();
912 err = btrfs_interface_init();
914 goto free_extent_map;
916 err = register_filesystem(&btrfs_fs_type);
918 goto unregister_ioctl;
920 printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
924 btrfs_interface_exit();
930 btrfs_destroy_cachep();
936 static void __exit exit_btrfs_fs(void)
938 btrfs_destroy_cachep();
941 btrfs_interface_exit();
942 unregister_filesystem(&btrfs_fs_type);
944 btrfs_cleanup_fs_uuids();
948 module_init(init_btrfs_fs)
949 module_exit(exit_btrfs_fs)
951 MODULE_LICENSE("GPL");