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_device, Opt_nodatasum, Opt_nodatacow,
67 Opt_max_extent, Opt_max_inline, Opt_alloc_start, Opt_nobarrier,
68 Opt_ssd, Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl,
69 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_device, "device=%s"},
78 {Opt_nodatasum, "nodatasum"},
79 {Opt_nodatacow, "nodatacow"},
80 {Opt_nobarrier, "nobarrier"},
81 {Opt_max_extent, "max_extent=%s"},
82 {Opt_max_inline, "max_inline=%s"},
83 {Opt_alloc_start, "alloc_start=%s"},
84 {Opt_thread_pool, "thread_pool=%d"},
85 {Opt_compress, "compress"},
86 {Opt_compress_force, "compress-force"},
88 {Opt_ssd_spread, "ssd_spread"},
91 {Opt_notreelog, "notreelog"},
92 {Opt_flushoncommit, "flushoncommit"},
93 {Opt_ratio, "metadata_ratio=%d"},
94 {Opt_discard, "discard"},
98 u64 btrfs_parse_size(char *str)
105 res = simple_strtoul(str, &end, 10);
109 last = tolower(last);
124 * Regular mount options parser. Everything that is needed only when
125 * reading in a new superblock is parsed here.
127 int btrfs_parse_options(struct btrfs_root *root, char *options)
129 struct btrfs_fs_info *info = root->fs_info;
130 substring_t args[MAX_OPT_ARGS];
131 char *p, *num, *orig;
139 * strsep changes the string, duplicate it because parse_options
142 options = kstrdup(options, GFP_NOFS);
148 while ((p = strsep(&options, ",")) != NULL) {
153 token = match_token(p, tokens, args);
156 printk(KERN_INFO "btrfs: allowing degraded mounts\n");
157 btrfs_set_opt(info->mount_opt, DEGRADED);
162 * These are parsed by btrfs_parse_early_options
163 * and can be happily ignored here.
167 printk(KERN_INFO "btrfs: setting nodatasum\n");
168 btrfs_set_opt(info->mount_opt, NODATASUM);
171 printk(KERN_INFO "btrfs: setting nodatacow\n");
172 btrfs_set_opt(info->mount_opt, NODATACOW);
173 btrfs_set_opt(info->mount_opt, NODATASUM);
176 printk(KERN_INFO "btrfs: use compression\n");
177 btrfs_set_opt(info->mount_opt, COMPRESS);
179 case Opt_compress_force:
180 printk(KERN_INFO "btrfs: forcing compression\n");
181 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
182 btrfs_set_opt(info->mount_opt, COMPRESS);
185 printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
186 btrfs_set_opt(info->mount_opt, SSD);
189 printk(KERN_INFO "btrfs: use spread ssd "
190 "allocation scheme\n");
191 btrfs_set_opt(info->mount_opt, SSD);
192 btrfs_set_opt(info->mount_opt, SSD_SPREAD);
195 printk(KERN_INFO "btrfs: not using ssd allocation "
197 btrfs_set_opt(info->mount_opt, NOSSD);
198 btrfs_clear_opt(info->mount_opt, SSD);
199 btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
202 printk(KERN_INFO "btrfs: turning off barriers\n");
203 btrfs_set_opt(info->mount_opt, NOBARRIER);
205 case Opt_thread_pool:
207 match_int(&args[0], &intarg);
209 info->thread_pool_size = intarg;
210 printk(KERN_INFO "btrfs: thread pool %d\n",
211 info->thread_pool_size);
215 num = match_strdup(&args[0]);
217 info->max_extent = btrfs_parse_size(num);
220 info->max_extent = max_t(u64,
221 info->max_extent, root->sectorsize);
222 printk(KERN_INFO "btrfs: max_extent at %llu\n",
223 (unsigned long long)info->max_extent);
227 num = match_strdup(&args[0]);
229 info->max_inline = btrfs_parse_size(num);
232 if (info->max_inline) {
233 info->max_inline = max_t(u64,
237 printk(KERN_INFO "btrfs: max_inline at %llu\n",
238 (unsigned long long)info->max_inline);
241 case Opt_alloc_start:
242 num = match_strdup(&args[0]);
244 info->alloc_start = btrfs_parse_size(num);
247 "btrfs: allocations start at %llu\n",
248 (unsigned long long)info->alloc_start);
252 root->fs_info->sb->s_flags &= ~MS_POSIXACL;
255 printk(KERN_INFO "btrfs: disabling tree log\n");
256 btrfs_set_opt(info->mount_opt, NOTREELOG);
258 case Opt_flushoncommit:
259 printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
260 btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
264 match_int(&args[0], &intarg);
266 info->metadata_ratio = intarg;
267 printk(KERN_INFO "btrfs: metadata ratio %d\n",
268 info->metadata_ratio);
272 btrfs_set_opt(info->mount_opt, DISCARD);
275 printk(KERN_INFO "btrfs: unrecognized mount option "
289 * Parse mount options that are required early in the mount process.
291 * All other options will be parsed on much later in the mount process and
292 * only when we need to allocate a new super block.
294 static int btrfs_parse_early_options(const char *options, fmode_t flags,
295 void *holder, char **subvol_name,
296 struct btrfs_fs_devices **fs_devices)
298 substring_t args[MAX_OPT_ARGS];
306 * strsep changes the string, duplicate it because parse_options
309 opts = kstrdup(options, GFP_KERNEL);
313 while ((p = strsep(&opts, ",")) != NULL) {
318 token = match_token(p, tokens, args);
321 *subvol_name = match_strdup(&args[0]);
324 error = btrfs_scan_one_device(match_strdup(&args[0]),
325 flags, holder, fs_devices);
338 * If no subvolume name is specified we use the default one. Allocate
339 * a copy of the string "." here so that code later in the
340 * mount path doesn't care if it's the default volume or another one.
343 *subvol_name = kstrdup(".", GFP_KERNEL);
350 static int btrfs_fill_super(struct super_block *sb,
351 struct btrfs_fs_devices *fs_devices,
352 void *data, int silent)
355 struct dentry *root_dentry;
356 struct btrfs_super_block *disk_super;
357 struct btrfs_root *tree_root;
358 struct btrfs_key key;
361 sb->s_maxbytes = MAX_LFS_FILESIZE;
362 sb->s_magic = BTRFS_SUPER_MAGIC;
363 sb->s_op = &btrfs_super_ops;
364 sb->s_export_op = &btrfs_export_ops;
365 sb->s_xattr = btrfs_xattr_handlers;
367 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
368 sb->s_flags |= MS_POSIXACL;
371 tree_root = open_ctree(sb, fs_devices, (char *)data);
373 if (IS_ERR(tree_root)) {
374 printk("btrfs: open_ctree failed\n");
375 return PTR_ERR(tree_root);
377 sb->s_fs_info = tree_root;
378 disk_super = &tree_root->fs_info->super_copy;
380 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
381 key.type = BTRFS_INODE_ITEM_KEY;
383 inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root);
385 err = PTR_ERR(inode);
389 root_dentry = d_alloc_root(inode);
396 /* this does the super kobj at the same time */
397 err = btrfs_sysfs_add_super(tree_root->fs_info);
402 sb->s_root = root_dentry;
404 save_mount_options(sb, data);
408 close_ctree(tree_root);
412 int btrfs_sync_fs(struct super_block *sb, int wait)
414 struct btrfs_trans_handle *trans;
415 struct btrfs_root *root = btrfs_sb(sb);
419 filemap_flush(root->fs_info->btree_inode->i_mapping);
423 btrfs_start_delalloc_inodes(root, 0);
424 btrfs_wait_ordered_extents(root, 0, 0);
426 trans = btrfs_start_transaction(root, 1);
427 ret = btrfs_commit_transaction(trans, root);
431 static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
433 struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
434 struct btrfs_fs_info *info = root->fs_info;
436 if (btrfs_test_opt(root, DEGRADED))
437 seq_puts(seq, ",degraded");
438 if (btrfs_test_opt(root, NODATASUM))
439 seq_puts(seq, ",nodatasum");
440 if (btrfs_test_opt(root, NODATACOW))
441 seq_puts(seq, ",nodatacow");
442 if (btrfs_test_opt(root, NOBARRIER))
443 seq_puts(seq, ",nobarrier");
444 if (info->max_extent != (u64)-1)
445 seq_printf(seq, ",max_extent=%llu",
446 (unsigned long long)info->max_extent);
447 if (info->max_inline != 8192 * 1024)
448 seq_printf(seq, ",max_inline=%llu",
449 (unsigned long long)info->max_inline);
450 if (info->alloc_start != 0)
451 seq_printf(seq, ",alloc_start=%llu",
452 (unsigned long long)info->alloc_start);
453 if (info->thread_pool_size != min_t(unsigned long,
454 num_online_cpus() + 2, 8))
455 seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
456 if (btrfs_test_opt(root, COMPRESS))
457 seq_puts(seq, ",compress");
458 if (btrfs_test_opt(root, NOSSD))
459 seq_puts(seq, ",nossd");
460 if (btrfs_test_opt(root, SSD_SPREAD))
461 seq_puts(seq, ",ssd_spread");
462 else if (btrfs_test_opt(root, SSD))
463 seq_puts(seq, ",ssd");
464 if (btrfs_test_opt(root, NOTREELOG))
465 seq_puts(seq, ",notreelog");
466 if (btrfs_test_opt(root, FLUSHONCOMMIT))
467 seq_puts(seq, ",flushoncommit");
468 if (btrfs_test_opt(root, DISCARD))
469 seq_puts(seq, ",discard");
470 if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
471 seq_puts(seq, ",noacl");
475 static int btrfs_test_super(struct super_block *s, void *data)
477 struct btrfs_fs_devices *test_fs_devices = data;
478 struct btrfs_root *root = btrfs_sb(s);
480 return root->fs_info->fs_devices == test_fs_devices;
484 * Find a superblock for the given device / mount point.
486 * Note: This is based on get_sb_bdev from fs/super.c with a few additions
487 * for multiple device setup. Make sure to keep it in sync.
489 static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
490 const char *dev_name, void *data, struct vfsmount *mnt)
492 char *subvol_name = NULL;
493 struct block_device *bdev = NULL;
494 struct super_block *s;
496 struct btrfs_fs_devices *fs_devices = NULL;
497 fmode_t mode = FMODE_READ;
500 if (!(flags & MS_RDONLY))
503 error = btrfs_parse_early_options(data, mode, fs_type,
504 &subvol_name, &fs_devices);
508 error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices);
510 goto error_free_subvol_name;
512 error = btrfs_open_devices(fs_devices, mode, fs_type);
514 goto error_free_subvol_name;
516 if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
518 goto error_close_devices;
521 bdev = fs_devices->latest_bdev;
522 s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices);
527 if ((flags ^ s->s_flags) & MS_RDONLY) {
528 deactivate_locked_super(s);
530 goto error_close_devices;
533 btrfs_close_devices(fs_devices);
535 char b[BDEVNAME_SIZE];
538 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
539 error = btrfs_fill_super(s, fs_devices, data,
540 flags & MS_SILENT ? 1 : 0);
542 deactivate_locked_super(s);
543 goto error_free_subvol_name;
546 btrfs_sb(s)->fs_info->bdev_holder = fs_type;
547 s->s_flags |= MS_ACTIVE;
550 if (!strcmp(subvol_name, "."))
551 root = dget(s->s_root);
553 mutex_lock(&s->s_root->d_inode->i_mutex);
554 root = lookup_one_len(subvol_name, s->s_root,
555 strlen(subvol_name));
556 mutex_unlock(&s->s_root->d_inode->i_mutex);
559 deactivate_locked_super(s);
560 error = PTR_ERR(root);
561 goto error_free_subvol_name;
563 if (!root->d_inode) {
565 deactivate_locked_super(s);
567 goto error_free_subvol_name;
572 mnt->mnt_root = root;
580 btrfs_close_devices(fs_devices);
581 error_free_subvol_name:
586 static int btrfs_remount(struct super_block *sb, int *flags, char *data)
588 struct btrfs_root *root = btrfs_sb(sb);
591 ret = btrfs_parse_options(root, data);
595 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
598 if (*flags & MS_RDONLY) {
599 sb->s_flags |= MS_RDONLY;
601 ret = btrfs_commit_super(root);
604 if (root->fs_info->fs_devices->rw_devices == 0)
607 if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
610 /* recover relocation */
611 ret = btrfs_recover_relocation(root);
614 ret = btrfs_cleanup_fs_roots(root->fs_info);
617 sb->s_flags &= ~MS_RDONLY;
623 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
625 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
626 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
627 int bits = dentry->d_sb->s_blocksize_bits;
628 __be32 *fsid = (__be32 *)root->fs_info->fsid;
630 buf->f_namelen = BTRFS_NAME_LEN;
631 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
632 buf->f_bfree = buf->f_blocks -
633 (btrfs_super_bytes_used(disk_super) >> bits);
634 buf->f_bavail = buf->f_bfree;
635 buf->f_bsize = dentry->d_sb->s_blocksize;
636 buf->f_type = BTRFS_SUPER_MAGIC;
638 /* We treat it as constant endianness (it doesn't matter _which_)
639 because we want the fsid to come out the same whether mounted
640 on a big-endian or little-endian host */
641 buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
642 buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
643 /* Mask in the root object ID too, to disambiguate subvols */
644 buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
645 buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
650 static struct file_system_type btrfs_fs_type = {
651 .owner = THIS_MODULE,
653 .get_sb = btrfs_get_sb,
654 .kill_sb = kill_anon_super,
655 .fs_flags = FS_REQUIRES_DEV,
659 * used by btrfsctl to scan devices when no FS is mounted
661 static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
664 struct btrfs_ioctl_vol_args *vol;
665 struct btrfs_fs_devices *fs_devices;
668 if (!capable(CAP_SYS_ADMIN))
671 vol = memdup_user((void __user *)arg, sizeof(*vol));
676 case BTRFS_IOC_SCAN_DEV:
677 ret = btrfs_scan_one_device(vol->name, FMODE_READ,
678 &btrfs_fs_type, &fs_devices);
686 static int btrfs_freeze(struct super_block *sb)
688 struct btrfs_root *root = btrfs_sb(sb);
689 mutex_lock(&root->fs_info->transaction_kthread_mutex);
690 mutex_lock(&root->fs_info->cleaner_mutex);
694 static int btrfs_unfreeze(struct super_block *sb)
696 struct btrfs_root *root = btrfs_sb(sb);
697 mutex_unlock(&root->fs_info->cleaner_mutex);
698 mutex_unlock(&root->fs_info->transaction_kthread_mutex);
702 static const struct super_operations btrfs_super_ops = {
703 .drop_inode = btrfs_drop_inode,
704 .delete_inode = btrfs_delete_inode,
705 .put_super = btrfs_put_super,
706 .sync_fs = btrfs_sync_fs,
707 .show_options = btrfs_show_options,
708 .write_inode = btrfs_write_inode,
709 .dirty_inode = btrfs_dirty_inode,
710 .alloc_inode = btrfs_alloc_inode,
711 .destroy_inode = btrfs_destroy_inode,
712 .statfs = btrfs_statfs,
713 .remount_fs = btrfs_remount,
714 .freeze_fs = btrfs_freeze,
715 .unfreeze_fs = btrfs_unfreeze,
718 static const struct file_operations btrfs_ctl_fops = {
719 .unlocked_ioctl = btrfs_control_ioctl,
720 .compat_ioctl = btrfs_control_ioctl,
721 .owner = THIS_MODULE,
724 static struct miscdevice btrfs_misc = {
725 .minor = MISC_DYNAMIC_MINOR,
726 .name = "btrfs-control",
727 .fops = &btrfs_ctl_fops
730 static int btrfs_interface_init(void)
732 return misc_register(&btrfs_misc);
735 static void btrfs_interface_exit(void)
737 if (misc_deregister(&btrfs_misc) < 0)
738 printk(KERN_INFO "misc_deregister failed for control device");
741 static int __init init_btrfs_fs(void)
745 err = btrfs_init_sysfs();
749 err = btrfs_init_cachep();
753 err = extent_io_init();
757 err = extent_map_init();
761 err = btrfs_interface_init();
763 goto free_extent_map;
765 err = register_filesystem(&btrfs_fs_type);
767 goto unregister_ioctl;
769 printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
773 btrfs_interface_exit();
779 btrfs_destroy_cachep();
785 static void __exit exit_btrfs_fs(void)
787 btrfs_destroy_cachep();
790 btrfs_interface_exit();
791 unregister_filesystem(&btrfs_fs_type);
793 btrfs_cleanup_fs_uuids();
797 module_init(init_btrfs_fs)
798 module_exit(exit_btrfs_fs)
800 MODULE_LICENSE("GPL");