16cbd5570SChris Mason /* 26cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 36cbd5570SChris Mason * 46cbd5570SChris Mason * This program is free software; you can redistribute it and/or 56cbd5570SChris Mason * modify it under the terms of the GNU General Public 66cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 76cbd5570SChris Mason * 86cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 96cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 106cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 116cbd5570SChris Mason * General Public License for more details. 126cbd5570SChris Mason * 136cbd5570SChris Mason * You should have received a copy of the GNU General Public 146cbd5570SChris Mason * License along with this program; if not, write to the 156cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 166cbd5570SChris Mason * Boston, MA 021110-1307, USA. 176cbd5570SChris Mason */ 186cbd5570SChris Mason 194b82d6e4SYan #include <linux/blkdev.h> 202e635a27SChris Mason #include <linux/module.h> 21e20d96d6SChris Mason #include <linux/buffer_head.h> 222e635a27SChris Mason #include <linux/fs.h> 232e635a27SChris Mason #include <linux/pagemap.h> 242e635a27SChris Mason #include <linux/highmem.h> 252e635a27SChris Mason #include <linux/time.h> 262e635a27SChris Mason #include <linux/init.h> 27a9572a15SEric Paris #include <linux/seq_file.h> 282e635a27SChris Mason #include <linux/string.h> 292e635a27SChris Mason #include <linux/backing-dev.h> 304b82d6e4SYan #include <linux/mount.h> 31dee26a9fSChris Mason #include <linux/mpage.h> 3275dfe396SChris Mason #include <linux/swap.h> 3375dfe396SChris Mason #include <linux/writeback.h> 348fd17795SChris Mason #include <linux/statfs.h> 3508607c1bSChris Mason #include <linux/compat.h> 3695e05289SChris Mason #include <linux/parser.h> 37c59f8951SChris Mason #include <linux/ctype.h> 386da6abaeSChris Mason #include <linux/namei.h> 39a9218f6bSChris Mason #include <linux/miscdevice.h> 401bcbf313SQinghuang Feng #include <linux/magic.h> 414b4e25f2SChris Mason #include "compat.h" 422e635a27SChris Mason #include "ctree.h" 43e20d96d6SChris Mason #include "disk-io.h" 44d5719762SChris Mason #include "transaction.h" 452c90e5d6SChris Mason #include "btrfs_inode.h" 46c5739bbaSChris Mason #include "ioctl.h" 473a686375SChris Mason #include "print-tree.h" 485103e947SJosef Bacik #include "xattr.h" 498a4b83ccSChris Mason #include "volumes.h" 50b3c3da71SChris Mason #include "version.h" 51be6e8dc0SBalaji Rao #include "export.h" 52c8b97818SChris Mason #include "compression.h" 532e635a27SChris Mason 54e20d96d6SChris Mason static struct super_operations btrfs_super_ops; 55e20d96d6SChris Mason 56e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 57e20d96d6SChris Mason { 58e20d96d6SChris Mason struct btrfs_root *root = btrfs_sb(sb); 59e20d96d6SChris Mason int ret; 60e20d96d6SChris Mason 61e20d96d6SChris Mason ret = close_ctree(root); 62e20d96d6SChris Mason sb->s_fs_info = NULL; 63e20d96d6SChris Mason } 642e635a27SChris Mason 6595e05289SChris Mason enum { 6643e570b0SChristoph Hellwig Opt_degraded, Opt_subvol, Opt_device, Opt_nodatasum, Opt_nodatacow, 67dfe25020SChris Mason Opt_max_extent, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, 68451d7585SChris Mason Opt_ssd, Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, 69451d7585SChris Mason Opt_compress, Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_err, 7095e05289SChris Mason }; 7195e05289SChris Mason 7295e05289SChris Mason static match_table_t tokens = { 73dfe25020SChris Mason {Opt_degraded, "degraded"}, 7495e05289SChris Mason {Opt_subvol, "subvol=%s"}, 7543e570b0SChristoph Hellwig {Opt_device, "device=%s"}, 76b6cda9bcSChris Mason {Opt_nodatasum, "nodatasum"}, 77be20aa9dSChris Mason {Opt_nodatacow, "nodatacow"}, 7821ad10cfSChris Mason {Opt_nobarrier, "nobarrier"}, 79c59f8951SChris Mason {Opt_max_extent, "max_extent=%s"}, 806f568d35SChris Mason {Opt_max_inline, "max_inline=%s"}, 818f662a76SChris Mason {Opt_alloc_start, "alloc_start=%s"}, 824543df7eSChris Mason {Opt_thread_pool, "thread_pool=%d"}, 83c8b97818SChris Mason {Opt_compress, "compress"}, 84e18e4809SChris Mason {Opt_ssd, "ssd"}, 85451d7585SChris Mason {Opt_ssd_spread, "ssd_spread"}, 863b30c22fSChris Mason {Opt_nossd, "nossd"}, 8733268eafSJosef Bacik {Opt_noacl, "noacl"}, 883a5e1404SSage Weil {Opt_notreelog, "notreelog"}, 89dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 9097e728d4SJosef Bacik {Opt_ratio, "metadata_ratio=%d"}, 9133268eafSJosef Bacik {Opt_err, NULL}, 9295e05289SChris Mason }; 9395e05289SChris Mason 94edbd8d4eSChris Mason u64 btrfs_parse_size(char *str) 95c59f8951SChris Mason { 96edbd8d4eSChris Mason u64 res; 97c59f8951SChris Mason int mult = 1; 98c59f8951SChris Mason char *end; 99c59f8951SChris Mason char last; 100c59f8951SChris Mason 101c59f8951SChris Mason res = simple_strtoul(str, &end, 10); 102c59f8951SChris Mason 103c59f8951SChris Mason last = end[0]; 104c59f8951SChris Mason if (isalpha(last)) { 105c59f8951SChris Mason last = tolower(last); 106c59f8951SChris Mason switch (last) { 107c59f8951SChris Mason case 'g': 108c59f8951SChris Mason mult *= 1024; 109c59f8951SChris Mason case 'm': 110c59f8951SChris Mason mult *= 1024; 111c59f8951SChris Mason case 'k': 112c59f8951SChris Mason mult *= 1024; 113c59f8951SChris Mason } 114c59f8951SChris Mason res = res * mult; 115c59f8951SChris Mason } 116c59f8951SChris Mason return res; 117c59f8951SChris Mason } 118c59f8951SChris Mason 119edf24abeSChristoph Hellwig /* 120edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 121edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 122edf24abeSChristoph Hellwig */ 123edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options) 12495e05289SChris Mason { 125edf24abeSChristoph Hellwig struct btrfs_fs_info *info = root->fs_info; 12695e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 127edf24abeSChristoph Hellwig char *p, *num; 1284543df7eSChris Mason int intarg; 129b6cda9bcSChris Mason 13095e05289SChris Mason if (!options) 131edf24abeSChristoph Hellwig return 0; 13295e05289SChris Mason 133be20aa9dSChris Mason /* 134be20aa9dSChris Mason * strsep changes the string, duplicate it because parse_options 135be20aa9dSChris Mason * gets called twice 136be20aa9dSChris Mason */ 137be20aa9dSChris Mason options = kstrdup(options, GFP_NOFS); 138be20aa9dSChris Mason if (!options) 139be20aa9dSChris Mason return -ENOMEM; 140be20aa9dSChris Mason 141be20aa9dSChris Mason 14295e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 14395e05289SChris Mason int token; 14495e05289SChris Mason if (!*p) 14595e05289SChris Mason continue; 14695e05289SChris Mason 14795e05289SChris Mason token = match_token(p, tokens, args); 14895e05289SChris Mason switch (token) { 149dfe25020SChris Mason case Opt_degraded: 150edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: allowing degraded mounts\n"); 151dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 152dfe25020SChris Mason break; 15395e05289SChris Mason case Opt_subvol: 15443e570b0SChristoph Hellwig case Opt_device: 155edf24abeSChristoph Hellwig /* 15643e570b0SChristoph Hellwig * These are parsed by btrfs_parse_early_options 157edf24abeSChristoph Hellwig * and can be happily ignored here. 158edf24abeSChristoph Hellwig */ 15995e05289SChris Mason break; 160b6cda9bcSChris Mason case Opt_nodatasum: 161067c28adSChris Mason printk(KERN_INFO "btrfs: setting nodatasum\n"); 162b6cda9bcSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 163be20aa9dSChris Mason break; 164be20aa9dSChris Mason case Opt_nodatacow: 165edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: setting nodatacow\n"); 166be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 167be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 168b6cda9bcSChris Mason break; 169c8b97818SChris Mason case Opt_compress: 170c8b97818SChris Mason printk(KERN_INFO "btrfs: use compression\n"); 171c8b97818SChris Mason btrfs_set_opt(info->mount_opt, COMPRESS); 172c8b97818SChris Mason break; 173e18e4809SChris Mason case Opt_ssd: 174edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: use ssd allocation scheme\n"); 175e18e4809SChris Mason btrfs_set_opt(info->mount_opt, SSD); 176e18e4809SChris Mason break; 177451d7585SChris Mason case Opt_ssd_spread: 178451d7585SChris Mason printk(KERN_INFO "btrfs: use spread ssd " 179451d7585SChris Mason "allocation scheme\n"); 180451d7585SChris Mason btrfs_set_opt(info->mount_opt, SSD); 181451d7585SChris Mason btrfs_set_opt(info->mount_opt, SSD_SPREAD); 182451d7585SChris Mason break; 1833b30c22fSChris Mason case Opt_nossd: 184451d7585SChris Mason printk(KERN_INFO "btrfs: not using ssd allocation " 185451d7585SChris Mason "scheme\n"); 186c289811cSChris Mason btrfs_set_opt(info->mount_opt, NOSSD); 1873b30c22fSChris Mason btrfs_clear_opt(info->mount_opt, SSD); 188451d7585SChris Mason btrfs_clear_opt(info->mount_opt, SSD_SPREAD); 1893b30c22fSChris Mason break; 19021ad10cfSChris Mason case Opt_nobarrier: 191edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: turning off barriers\n"); 19221ad10cfSChris Mason btrfs_set_opt(info->mount_opt, NOBARRIER); 19321ad10cfSChris Mason break; 1944543df7eSChris Mason case Opt_thread_pool: 1954543df7eSChris Mason intarg = 0; 1964543df7eSChris Mason match_int(&args[0], &intarg); 1974543df7eSChris Mason if (intarg) { 1984543df7eSChris Mason info->thread_pool_size = intarg; 1994543df7eSChris Mason printk(KERN_INFO "btrfs: thread pool %d\n", 2004543df7eSChris Mason info->thread_pool_size); 2014543df7eSChris Mason } 2024543df7eSChris Mason break; 203c59f8951SChris Mason case Opt_max_extent: 204edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 205c59f8951SChris Mason if (num) { 206edf24abeSChristoph Hellwig info->max_extent = btrfs_parse_size(num); 207c59f8951SChris Mason kfree(num); 208c59f8951SChris Mason 209c59f8951SChris Mason info->max_extent = max_t(u64, 210edf24abeSChristoph Hellwig info->max_extent, root->sectorsize); 211edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: max_extent at %llu\n", 21221380931SJoel Becker (unsigned long long)info->max_extent); 213c59f8951SChris Mason } 214c59f8951SChris Mason break; 2156f568d35SChris Mason case Opt_max_inline: 216edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 2176f568d35SChris Mason if (num) { 218edf24abeSChristoph Hellwig info->max_inline = btrfs_parse_size(num); 2196f568d35SChris Mason kfree(num); 2206f568d35SChris Mason 22115ada040SChris Mason if (info->max_inline) { 2226f568d35SChris Mason info->max_inline = max_t(u64, 22315ada040SChris Mason info->max_inline, 22415ada040SChris Mason root->sectorsize); 22515ada040SChris Mason } 226edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: max_inline at %llu\n", 22721380931SJoel Becker (unsigned long long)info->max_inline); 2286f568d35SChris Mason } 2296f568d35SChris Mason break; 2308f662a76SChris Mason case Opt_alloc_start: 231edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 2328f662a76SChris Mason if (num) { 233edf24abeSChristoph Hellwig info->alloc_start = btrfs_parse_size(num); 2348f662a76SChris Mason kfree(num); 235edf24abeSChristoph Hellwig printk(KERN_INFO 236edf24abeSChristoph Hellwig "btrfs: allocations start at %llu\n", 23721380931SJoel Becker (unsigned long long)info->alloc_start); 2388f662a76SChris Mason } 2398f662a76SChris Mason break; 24033268eafSJosef Bacik case Opt_noacl: 24133268eafSJosef Bacik root->fs_info->sb->s_flags &= ~MS_POSIXACL; 24233268eafSJosef Bacik break; 2433a5e1404SSage Weil case Opt_notreelog: 2443a5e1404SSage Weil printk(KERN_INFO "btrfs: disabling tree log\n"); 2453a5e1404SSage Weil btrfs_set_opt(info->mount_opt, NOTREELOG); 2463a5e1404SSage Weil break; 247dccae999SSage Weil case Opt_flushoncommit: 248dccae999SSage Weil printk(KERN_INFO "btrfs: turning on flush-on-commit\n"); 249dccae999SSage Weil btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT); 250dccae999SSage Weil break; 25197e728d4SJosef Bacik case Opt_ratio: 25297e728d4SJosef Bacik intarg = 0; 25397e728d4SJosef Bacik match_int(&args[0], &intarg); 25497e728d4SJosef Bacik if (intarg) { 25597e728d4SJosef Bacik info->metadata_ratio = intarg; 25697e728d4SJosef Bacik printk(KERN_INFO "btrfs: metadata ratio %d\n", 25797e728d4SJosef Bacik info->metadata_ratio); 25897e728d4SJosef Bacik } 25997e728d4SJosef Bacik break; 26095e05289SChris Mason default: 261be20aa9dSChris Mason break; 26295e05289SChris Mason } 26395e05289SChris Mason } 264be20aa9dSChris Mason kfree(options); 265edf24abeSChristoph Hellwig return 0; 266edf24abeSChristoph Hellwig } 267edf24abeSChristoph Hellwig 268edf24abeSChristoph Hellwig /* 269edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 270edf24abeSChristoph Hellwig * 271edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 272edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 273edf24abeSChristoph Hellwig */ 27497288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 27543e570b0SChristoph Hellwig void *holder, char **subvol_name, 27643e570b0SChristoph Hellwig struct btrfs_fs_devices **fs_devices) 277edf24abeSChristoph Hellwig { 278edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 279edf24abeSChristoph Hellwig char *opts, *p; 280edf24abeSChristoph Hellwig int error = 0; 281edf24abeSChristoph Hellwig 282edf24abeSChristoph Hellwig if (!options) 283edf24abeSChristoph Hellwig goto out; 284edf24abeSChristoph Hellwig 285edf24abeSChristoph Hellwig /* 286edf24abeSChristoph Hellwig * strsep changes the string, duplicate it because parse_options 287edf24abeSChristoph Hellwig * gets called twice 288edf24abeSChristoph Hellwig */ 289edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 290edf24abeSChristoph Hellwig if (!opts) 291edf24abeSChristoph Hellwig return -ENOMEM; 292edf24abeSChristoph Hellwig 293edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 294edf24abeSChristoph Hellwig int token; 295edf24abeSChristoph Hellwig if (!*p) 296edf24abeSChristoph Hellwig continue; 297edf24abeSChristoph Hellwig 298edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 299edf24abeSChristoph Hellwig switch (token) { 300edf24abeSChristoph Hellwig case Opt_subvol: 301edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 302edf24abeSChristoph Hellwig break; 30343e570b0SChristoph Hellwig case Opt_device: 30443e570b0SChristoph Hellwig error = btrfs_scan_one_device(match_strdup(&args[0]), 30543e570b0SChristoph Hellwig flags, holder, fs_devices); 30643e570b0SChristoph Hellwig if (error) 30743e570b0SChristoph Hellwig goto out_free_opts; 30843e570b0SChristoph Hellwig break; 309edf24abeSChristoph Hellwig default: 310edf24abeSChristoph Hellwig break; 311edf24abeSChristoph Hellwig } 312edf24abeSChristoph Hellwig } 313edf24abeSChristoph Hellwig 31443e570b0SChristoph Hellwig out_free_opts: 315edf24abeSChristoph Hellwig kfree(opts); 316edf24abeSChristoph Hellwig out: 317edf24abeSChristoph Hellwig /* 318edf24abeSChristoph Hellwig * If no subvolume name is specified we use the default one. Allocate 3193de4586cSChris Mason * a copy of the string "." here so that code later in the 320edf24abeSChristoph Hellwig * mount path doesn't care if it's the default volume or another one. 321edf24abeSChristoph Hellwig */ 322edf24abeSChristoph Hellwig if (!*subvol_name) { 3233de4586cSChris Mason *subvol_name = kstrdup(".", GFP_KERNEL); 324edf24abeSChristoph Hellwig if (!*subvol_name) 325edf24abeSChristoph Hellwig return -ENOMEM; 326edf24abeSChristoph Hellwig } 327edf24abeSChristoph Hellwig return error; 32895e05289SChris Mason } 32995e05289SChris Mason 3308a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 3318a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 3328a4b83ccSChris Mason void *data, int silent) 3332e635a27SChris Mason { 3342e635a27SChris Mason struct inode *inode; 335e20d96d6SChris Mason struct dentry *root_dentry; 336e20d96d6SChris Mason struct btrfs_super_block *disk_super; 3370f7d52f4SChris Mason struct btrfs_root *tree_root; 3385d4f98a2SYan Zheng struct btrfs_key key; 33939279cc3SChris Mason int err; 3402e635a27SChris Mason 3412e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 3422e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 343e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 344be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 3455103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 3462e635a27SChris Mason sb->s_time_gran = 1; 347*49cf6f45SChris Ball #ifdef CONFIG_BTRFS_POSIX_ACL 34833268eafSJosef Bacik sb->s_flags |= MS_POSIXACL; 349*49cf6f45SChris Ball #endif 350e20d96d6SChris Mason 351dfe25020SChris Mason tree_root = open_ctree(sb, fs_devices, (char *)data); 352d98237b3SChris Mason 353e58ca020SYan if (IS_ERR(tree_root)) { 354e20d96d6SChris Mason printk("btrfs: open_ctree failed\n"); 355e58ca020SYan return PTR_ERR(tree_root); 356e20d96d6SChris Mason } 3570f7d52f4SChris Mason sb->s_fs_info = tree_root; 3585f39d397SChris Mason disk_super = &tree_root->fs_info->super_copy; 359b888db2bSChris Mason 3605d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 3615d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 3625d4f98a2SYan Zheng key.offset = 0; 3635d4f98a2SYan Zheng inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root); 3645d4f98a2SYan Zheng if (IS_ERR(inode)) { 3655d4f98a2SYan Zheng err = PTR_ERR(inode); 36639279cc3SChris Mason goto fail_close; 36739279cc3SChris Mason } 3682e635a27SChris Mason 369e20d96d6SChris Mason root_dentry = d_alloc_root(inode); 370e20d96d6SChris Mason if (!root_dentry) { 3712e635a27SChris Mason iput(inode); 37239279cc3SChris Mason err = -ENOMEM; 37339279cc3SChris Mason goto fail_close; 3742e635a27SChris Mason } 375e4404d6eSYan Zheng #if 0 37658176a96SJosef Bacik /* this does the super kobj at the same time */ 37758176a96SJosef Bacik err = btrfs_sysfs_add_super(tree_root->fs_info); 37858176a96SJosef Bacik if (err) 37958176a96SJosef Bacik goto fail_close; 380e4404d6eSYan Zheng #endif 38158176a96SJosef Bacik 382e20d96d6SChris Mason sb->s_root = root_dentry; 3836885f308SChris Mason 3846885f308SChris Mason save_mount_options(sb, data); 3852e635a27SChris Mason return 0; 3862e635a27SChris Mason 38739279cc3SChris Mason fail_close: 38839279cc3SChris Mason close_ctree(tree_root); 389d5719762SChris Mason return err; 390d5719762SChris Mason } 391d5719762SChris Mason 3926bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 393d5719762SChris Mason { 394d5719762SChris Mason struct btrfs_trans_handle *trans; 395dccae999SSage Weil struct btrfs_root *root = btrfs_sb(sb); 396d5719762SChris Mason int ret; 397df2ce34cSChris Mason 398d561c025SChris Mason if (!wait) { 3997cfcc17eSChris Mason filemap_flush(root->fs_info->btree_inode->i_mapping); 400df2ce34cSChris Mason return 0; 401d561c025SChris Mason } 402771ed689SChris Mason 403771ed689SChris Mason btrfs_start_delalloc_inodes(root); 404771ed689SChris Mason btrfs_wait_ordered_extents(root, 0); 405771ed689SChris Mason 406d5719762SChris Mason trans = btrfs_start_transaction(root, 1); 407d5719762SChris Mason ret = btrfs_commit_transaction(trans, root); 40854aa1f4dSChris Mason return ret; 409d5719762SChris Mason } 410d5719762SChris Mason 411a9572a15SEric Paris static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs) 412a9572a15SEric Paris { 413a9572a15SEric Paris struct btrfs_root *root = btrfs_sb(vfs->mnt_sb); 414a9572a15SEric Paris struct btrfs_fs_info *info = root->fs_info; 415a9572a15SEric Paris 416a9572a15SEric Paris if (btrfs_test_opt(root, DEGRADED)) 417a9572a15SEric Paris seq_puts(seq, ",degraded"); 418a9572a15SEric Paris if (btrfs_test_opt(root, NODATASUM)) 419a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 420a9572a15SEric Paris if (btrfs_test_opt(root, NODATACOW)) 421a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 422a9572a15SEric Paris if (btrfs_test_opt(root, NOBARRIER)) 423a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 424a9572a15SEric Paris if (info->max_extent != (u64)-1) 42521380931SJoel Becker seq_printf(seq, ",max_extent=%llu", 42621380931SJoel Becker (unsigned long long)info->max_extent); 427a9572a15SEric Paris if (info->max_inline != 8192 * 1024) 42821380931SJoel Becker seq_printf(seq, ",max_inline=%llu", 42921380931SJoel Becker (unsigned long long)info->max_inline); 430a9572a15SEric Paris if (info->alloc_start != 0) 43121380931SJoel Becker seq_printf(seq, ",alloc_start=%llu", 43221380931SJoel Becker (unsigned long long)info->alloc_start); 433a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 434a9572a15SEric Paris num_online_cpus() + 2, 8)) 435a9572a15SEric Paris seq_printf(seq, ",thread_pool=%d", info->thread_pool_size); 436a9572a15SEric Paris if (btrfs_test_opt(root, COMPRESS)) 437a9572a15SEric Paris seq_puts(seq, ",compress"); 438c289811cSChris Mason if (btrfs_test_opt(root, NOSSD)) 439c289811cSChris Mason seq_puts(seq, ",nossd"); 440451d7585SChris Mason if (btrfs_test_opt(root, SSD_SPREAD)) 441451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 442451d7585SChris Mason else if (btrfs_test_opt(root, SSD)) 443a9572a15SEric Paris seq_puts(seq, ",ssd"); 4443a5e1404SSage Weil if (btrfs_test_opt(root, NOTREELOG)) 4456b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 446dccae999SSage Weil if (btrfs_test_opt(root, FLUSHONCOMMIT)) 4476b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 448a9572a15SEric Paris if (!(root->fs_info->sb->s_flags & MS_POSIXACL)) 449a9572a15SEric Paris seq_puts(seq, ",noacl"); 450a9572a15SEric Paris return 0; 451a9572a15SEric Paris } 452a9572a15SEric Paris 453a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 4542e635a27SChris Mason { 455a061fc8dSChris Mason struct btrfs_fs_devices *test_fs_devices = data; 456a061fc8dSChris Mason struct btrfs_root *root = btrfs_sb(s); 4574b82d6e4SYan 458a061fc8dSChris Mason return root->fs_info->fs_devices == test_fs_devices; 4594b82d6e4SYan } 4604b82d6e4SYan 461edf24abeSChristoph Hellwig /* 462edf24abeSChristoph Hellwig * Find a superblock for the given device / mount point. 463edf24abeSChristoph Hellwig * 464edf24abeSChristoph Hellwig * Note: This is based on get_sb_bdev from fs/super.c with a few additions 465edf24abeSChristoph Hellwig * for multiple device setup. Make sure to keep it in sync. 466edf24abeSChristoph Hellwig */ 467edf24abeSChristoph Hellwig static int btrfs_get_sb(struct file_system_type *fs_type, int flags, 468edf24abeSChristoph Hellwig const char *dev_name, void *data, struct vfsmount *mnt) 4694b82d6e4SYan { 470edf24abeSChristoph Hellwig char *subvol_name = NULL; 4714b82d6e4SYan struct block_device *bdev = NULL; 4724b82d6e4SYan struct super_block *s; 4734b82d6e4SYan struct dentry *root; 4748a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices = NULL; 47597288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 4764b82d6e4SYan int error = 0; 4774b82d6e4SYan 47897288f2cSChristoph Hellwig if (!(flags & MS_RDONLY)) 47997288f2cSChristoph Hellwig mode |= FMODE_WRITE; 48097288f2cSChristoph Hellwig 48197288f2cSChristoph Hellwig error = btrfs_parse_early_options(data, mode, fs_type, 48243e570b0SChristoph Hellwig &subvol_name, &fs_devices); 483edf24abeSChristoph Hellwig if (error) 4841f483660SShen Feng return error; 485edf24abeSChristoph Hellwig 48697288f2cSChristoph Hellwig error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices); 4878a4b83ccSChris Mason if (error) 488edf24abeSChristoph Hellwig goto error_free_subvol_name; 4894b82d6e4SYan 49097288f2cSChristoph Hellwig error = btrfs_open_devices(fs_devices, mode, fs_type); 4918a4b83ccSChris Mason if (error) 492edf24abeSChristoph Hellwig goto error_free_subvol_name; 4938a4b83ccSChris Mason 4942b82032cSYan Zheng if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) { 4952b82032cSYan Zheng error = -EACCES; 4962b82032cSYan Zheng goto error_close_devices; 4972b82032cSYan Zheng } 4982b82032cSYan Zheng 499dfe25020SChris Mason bdev = fs_devices->latest_bdev; 500a061fc8dSChris Mason s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices); 5014b82d6e4SYan if (IS_ERR(s)) 5024b82d6e4SYan goto error_s; 5034b82d6e4SYan 5044b82d6e4SYan if (s->s_root) { 5054b82d6e4SYan if ((flags ^ s->s_flags) & MS_RDONLY) { 5066f5bbff9SAl Viro deactivate_locked_super(s); 5074b82d6e4SYan error = -EBUSY; 508c146afadSYan Zheng goto error_close_devices; 5094b82d6e4SYan } 5104b82d6e4SYan 5112b82032cSYan Zheng btrfs_close_devices(fs_devices); 5124b82d6e4SYan } else { 5134b82d6e4SYan char b[BDEVNAME_SIZE]; 5144b82d6e4SYan 5154b82d6e4SYan s->s_flags = flags; 5164b82d6e4SYan strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); 5178a4b83ccSChris Mason error = btrfs_fill_super(s, fs_devices, data, 5188a4b83ccSChris Mason flags & MS_SILENT ? 1 : 0); 5194b82d6e4SYan if (error) { 5206f5bbff9SAl Viro deactivate_locked_super(s); 5211f483660SShen Feng goto error_free_subvol_name; 5224b82d6e4SYan } 5234b82d6e4SYan 524788f20ebSChris Mason btrfs_sb(s)->fs_info->bdev_holder = fs_type; 5254b82d6e4SYan s->s_flags |= MS_ACTIVE; 5264b82d6e4SYan } 5274b82d6e4SYan 52876fcef19SDavid Woodhouse if (!strcmp(subvol_name, ".")) 52976fcef19SDavid Woodhouse root = dget(s->s_root); 53076fcef19SDavid Woodhouse else { 531b48652c1SYan Zheng mutex_lock(&s->s_root->d_inode->i_mutex); 532d397712bSChris Mason root = lookup_one_len(subvol_name, s->s_root, 533d397712bSChris Mason strlen(subvol_name)); 534b48652c1SYan Zheng mutex_unlock(&s->s_root->d_inode->i_mutex); 535d397712bSChris Mason 5364b82d6e4SYan if (IS_ERR(root)) { 5376f5bbff9SAl Viro deactivate_locked_super(s); 5384b82d6e4SYan error = PTR_ERR(root); 5391f483660SShen Feng goto error_free_subvol_name; 5404b82d6e4SYan } 5414b82d6e4SYan if (!root->d_inode) { 5424b82d6e4SYan dput(root); 5436f5bbff9SAl Viro deactivate_locked_super(s); 5444b82d6e4SYan error = -ENXIO; 5451f483660SShen Feng goto error_free_subvol_name; 5464b82d6e4SYan } 54776fcef19SDavid Woodhouse } 5484b82d6e4SYan 5494b82d6e4SYan mnt->mnt_sb = s; 5504b82d6e4SYan mnt->mnt_root = root; 551edf24abeSChristoph Hellwig 552edf24abeSChristoph Hellwig kfree(subvol_name); 5534b82d6e4SYan return 0; 5544b82d6e4SYan 5554b82d6e4SYan error_s: 5564b82d6e4SYan error = PTR_ERR(s); 557c146afadSYan Zheng error_close_devices: 5588a4b83ccSChris Mason btrfs_close_devices(fs_devices); 559edf24abeSChristoph Hellwig error_free_subvol_name: 560edf24abeSChristoph Hellwig kfree(subvol_name); 5614b82d6e4SYan return error; 5624b82d6e4SYan } 5632e635a27SChris Mason 564c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 565c146afadSYan Zheng { 566c146afadSYan Zheng struct btrfs_root *root = btrfs_sb(sb); 567c146afadSYan Zheng int ret; 568c146afadSYan Zheng 569b288052eSChris Mason ret = btrfs_parse_options(root, data); 570b288052eSChris Mason if (ret) 571b288052eSChris Mason return -EINVAL; 572b288052eSChris Mason 573c146afadSYan Zheng if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 574c146afadSYan Zheng return 0; 575c146afadSYan Zheng 576c146afadSYan Zheng if (*flags & MS_RDONLY) { 577c146afadSYan Zheng sb->s_flags |= MS_RDONLY; 578c146afadSYan Zheng 579c146afadSYan Zheng ret = btrfs_commit_super(root); 580c146afadSYan Zheng WARN_ON(ret); 581c146afadSYan Zheng } else { 5822b82032cSYan Zheng if (root->fs_info->fs_devices->rw_devices == 0) 5832b82032cSYan Zheng return -EACCES; 5842b82032cSYan Zheng 585c146afadSYan Zheng if (btrfs_super_log_root(&root->fs_info->super_copy) != 0) 586c146afadSYan Zheng return -EINVAL; 587c146afadSYan Zheng 5885d4f98a2SYan Zheng /* recover relocation */ 5895d4f98a2SYan Zheng ret = btrfs_recover_relocation(root); 590c146afadSYan Zheng WARN_ON(ret); 591c146afadSYan Zheng 592c146afadSYan Zheng ret = btrfs_cleanup_fs_roots(root->fs_info); 593c146afadSYan Zheng WARN_ON(ret); 594c146afadSYan Zheng 595c146afadSYan Zheng sb->s_flags &= ~MS_RDONLY; 596c146afadSYan Zheng } 597c146afadSYan Zheng 598c146afadSYan Zheng return 0; 599c146afadSYan Zheng } 600c146afadSYan Zheng 6018fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 6028fd17795SChris Mason { 6038fd17795SChris Mason struct btrfs_root *root = btrfs_sb(dentry->d_sb); 6044b52dff6SChris Mason struct btrfs_super_block *disk_super = &root->fs_info->super_copy; 605db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 6069d03632eSDavid Woodhouse __be32 *fsid = (__be32 *)root->fs_info->fsid; 6078fd17795SChris Mason 6088fd17795SChris Mason buf->f_namelen = BTRFS_NAME_LEN; 609db94535dSChris Mason buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits; 610db94535dSChris Mason buf->f_bfree = buf->f_blocks - 611db94535dSChris Mason (btrfs_super_bytes_used(disk_super) >> bits); 6128fd17795SChris Mason buf->f_bavail = buf->f_bfree; 6138fd17795SChris Mason buf->f_bsize = dentry->d_sb->s_blocksize; 6148fd17795SChris Mason buf->f_type = BTRFS_SUPER_MAGIC; 615d397712bSChris Mason 6169d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 6179d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 6189d03632eSDavid Woodhouse on a big-endian or little-endian host */ 6199d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 6209d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 62132d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 62232d48fa1SDavid Woodhouse buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32; 62332d48fa1SDavid Woodhouse buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid; 62432d48fa1SDavid Woodhouse 6258fd17795SChris Mason return 0; 6268fd17795SChris Mason } 627b5133862SChris Mason 6282e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 6292e635a27SChris Mason .owner = THIS_MODULE, 6302e635a27SChris Mason .name = "btrfs", 6312e635a27SChris Mason .get_sb = btrfs_get_sb, 632a061fc8dSChris Mason .kill_sb = kill_anon_super, 6332e635a27SChris Mason .fs_flags = FS_REQUIRES_DEV, 6342e635a27SChris Mason }; 635a9218f6bSChris Mason 636d352ac68SChris Mason /* 637d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 638d352ac68SChris Mason */ 6398a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 6408a4b83ccSChris Mason unsigned long arg) 6418a4b83ccSChris Mason { 6428a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 6438a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 644c071fcfdSChris Mason int ret = -ENOTTY; 6458a4b83ccSChris Mason 646e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 647e441d54dSChris Mason return -EPERM; 648e441d54dSChris Mason 649dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 650dae7b665SLi Zefan if (IS_ERR(vol)) 651dae7b665SLi Zefan return PTR_ERR(vol); 652c071fcfdSChris Mason 6538a4b83ccSChris Mason switch (cmd) { 6548a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 65597288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 6568a4b83ccSChris Mason &btrfs_fs_type, &fs_devices); 6578a4b83ccSChris Mason break; 6588a4b83ccSChris Mason } 659dae7b665SLi Zefan 6608a4b83ccSChris Mason kfree(vol); 661f819d837SLinda Knippers return ret; 6628a4b83ccSChris Mason } 6638a4b83ccSChris Mason 6640176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 665ed0dab6bSYan { 666ed0dab6bSYan struct btrfs_root *root = btrfs_sb(sb); 667a74a4b97SChris Mason mutex_lock(&root->fs_info->transaction_kthread_mutex); 668a74a4b97SChris Mason mutex_lock(&root->fs_info->cleaner_mutex); 6690176260fSLinus Torvalds return 0; 670ed0dab6bSYan } 671ed0dab6bSYan 6720176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb) 673ed0dab6bSYan { 674ed0dab6bSYan struct btrfs_root *root = btrfs_sb(sb); 675a74a4b97SChris Mason mutex_unlock(&root->fs_info->cleaner_mutex); 676a74a4b97SChris Mason mutex_unlock(&root->fs_info->transaction_kthread_mutex); 6770176260fSLinus Torvalds return 0; 678ed0dab6bSYan } 6792e635a27SChris Mason 680e20d96d6SChris Mason static struct super_operations btrfs_super_ops = { 68176dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 682134e9731SChris Mason .delete_inode = btrfs_delete_inode, 683e20d96d6SChris Mason .put_super = btrfs_put_super, 684d5719762SChris Mason .sync_fs = btrfs_sync_fs, 685a9572a15SEric Paris .show_options = btrfs_show_options, 6864730a4bcSChris Mason .write_inode = btrfs_write_inode, 687b5133862SChris Mason .dirty_inode = btrfs_dirty_inode, 6882c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 6892c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 6908fd17795SChris Mason .statfs = btrfs_statfs, 691c146afadSYan Zheng .remount_fs = btrfs_remount, 6920176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 6930176260fSLinus Torvalds .unfreeze_fs = btrfs_unfreeze, 694e20d96d6SChris Mason }; 695a9218f6bSChris Mason 696a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 697a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 698a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 699a9218f6bSChris Mason .owner = THIS_MODULE, 700a9218f6bSChris Mason }; 701a9218f6bSChris Mason 702a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 703a9218f6bSChris Mason .minor = MISC_DYNAMIC_MINOR, 704a9218f6bSChris Mason .name = "btrfs-control", 705a9218f6bSChris Mason .fops = &btrfs_ctl_fops 706a9218f6bSChris Mason }; 707a9218f6bSChris Mason 708a9218f6bSChris Mason static int btrfs_interface_init(void) 709a9218f6bSChris Mason { 710a9218f6bSChris Mason return misc_register(&btrfs_misc); 711a9218f6bSChris Mason } 712a9218f6bSChris Mason 713b2950863SChristoph Hellwig static void btrfs_interface_exit(void) 714a9218f6bSChris Mason { 715a9218f6bSChris Mason if (misc_deregister(&btrfs_misc) < 0) 716d397712bSChris Mason printk(KERN_INFO "misc_deregister failed for control device"); 717a9218f6bSChris Mason } 718a9218f6bSChris Mason 7192e635a27SChris Mason static int __init init_btrfs_fs(void) 7202e635a27SChris Mason { 7212c90e5d6SChris Mason int err; 72258176a96SJosef Bacik 72358176a96SJosef Bacik err = btrfs_init_sysfs(); 72458176a96SJosef Bacik if (err) 72558176a96SJosef Bacik return err; 72658176a96SJosef Bacik 72739279cc3SChris Mason err = btrfs_init_cachep(); 7282c90e5d6SChris Mason if (err) 729a74a4b97SChris Mason goto free_sysfs; 730d1310b2eSChris Mason 731d1310b2eSChris Mason err = extent_io_init(); 7322f4cbe64SWyatt Banks if (err) 7332f4cbe64SWyatt Banks goto free_cachep; 7342f4cbe64SWyatt Banks 735d1310b2eSChris Mason err = extent_map_init(); 736d1310b2eSChris Mason if (err) 737d1310b2eSChris Mason goto free_extent_io; 738d1310b2eSChris Mason 739a9218f6bSChris Mason err = btrfs_interface_init(); 7402f4cbe64SWyatt Banks if (err) 7412f4cbe64SWyatt Banks goto free_extent_map; 742c8b97818SChris Mason 743a9218f6bSChris Mason err = register_filesystem(&btrfs_fs_type); 744a9218f6bSChris Mason if (err) 745a9218f6bSChris Mason goto unregister_ioctl; 746b3c3da71SChris Mason 747b3c3da71SChris Mason printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION); 7482f4cbe64SWyatt Banks return 0; 7492f4cbe64SWyatt Banks 750a9218f6bSChris Mason unregister_ioctl: 751a9218f6bSChris Mason btrfs_interface_exit(); 7522f4cbe64SWyatt Banks free_extent_map: 7532f4cbe64SWyatt Banks extent_map_exit(); 754d1310b2eSChris Mason free_extent_io: 755d1310b2eSChris Mason extent_io_exit(); 7562f4cbe64SWyatt Banks free_cachep: 7572f4cbe64SWyatt Banks btrfs_destroy_cachep(); 758a74a4b97SChris Mason free_sysfs: 7592f4cbe64SWyatt Banks btrfs_exit_sysfs(); 7602c90e5d6SChris Mason return err; 7612e635a27SChris Mason } 7622e635a27SChris Mason 7632e635a27SChris Mason static void __exit exit_btrfs_fs(void) 7642e635a27SChris Mason { 76539279cc3SChris Mason btrfs_destroy_cachep(); 766a52d9a80SChris Mason extent_map_exit(); 767d1310b2eSChris Mason extent_io_exit(); 768a9218f6bSChris Mason btrfs_interface_exit(); 7692e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 77058176a96SJosef Bacik btrfs_exit_sysfs(); 7718a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 772c8b97818SChris Mason btrfs_zlib_exit(); 7732e635a27SChris Mason } 7742e635a27SChris Mason 7752e635a27SChris Mason module_init(init_btrfs_fs) 7762e635a27SChris Mason module_exit(exit_btrfs_fs) 7772e635a27SChris Mason 7782e635a27SChris Mason MODULE_LICENSE("GPL"); 779