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/smp_lock.h> 302e635a27SChris Mason #include <linux/backing-dev.h> 314b82d6e4SYan #include <linux/mount.h> 32dee26a9fSChris Mason #include <linux/mpage.h> 3375dfe396SChris Mason #include <linux/swap.h> 3475dfe396SChris Mason #include <linux/writeback.h> 358fd17795SChris Mason #include <linux/statfs.h> 3608607c1bSChris Mason #include <linux/compat.h> 3795e05289SChris Mason #include <linux/parser.h> 38c59f8951SChris Mason #include <linux/ctype.h> 396da6abaeSChris Mason #include <linux/namei.h> 40a9218f6bSChris Mason #include <linux/miscdevice.h> 411bcbf313SQinghuang Feng #include <linux/magic.h> 424b4e25f2SChris Mason #include "compat.h" 432e635a27SChris Mason #include "ctree.h" 44e20d96d6SChris Mason #include "disk-io.h" 45d5719762SChris Mason #include "transaction.h" 462c90e5d6SChris Mason #include "btrfs_inode.h" 47c5739bbaSChris Mason #include "ioctl.h" 483a686375SChris Mason #include "print-tree.h" 495103e947SJosef Bacik #include "xattr.h" 508a4b83ccSChris Mason #include "volumes.h" 51b3c3da71SChris Mason #include "version.h" 52be6e8dc0SBalaji Rao #include "export.h" 53c8b97818SChris Mason #include "compression.h" 542e635a27SChris Mason 55e20d96d6SChris Mason static struct super_operations btrfs_super_ops; 56e20d96d6SChris Mason 57e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 58e20d96d6SChris Mason { 59e20d96d6SChris Mason struct btrfs_root *root = btrfs_sb(sb); 60e20d96d6SChris Mason int ret; 61e20d96d6SChris Mason 62e20d96d6SChris Mason ret = close_ctree(root); 63e20d96d6SChris Mason sb->s_fs_info = NULL; 64e20d96d6SChris Mason } 652e635a27SChris Mason 6695e05289SChris Mason enum { 6743e570b0SChristoph Hellwig Opt_degraded, Opt_subvol, Opt_device, Opt_nodatasum, Opt_nodatacow, 68dfe25020SChris Mason Opt_max_extent, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, 69451d7585SChris Mason Opt_ssd, Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, 70451d7585SChris Mason Opt_compress, Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_err, 7195e05289SChris Mason }; 7295e05289SChris Mason 7395e05289SChris Mason static match_table_t tokens = { 74dfe25020SChris Mason {Opt_degraded, "degraded"}, 7595e05289SChris Mason {Opt_subvol, "subvol=%s"}, 7643e570b0SChristoph Hellwig {Opt_device, "device=%s"}, 77b6cda9bcSChris Mason {Opt_nodatasum, "nodatasum"}, 78be20aa9dSChris Mason {Opt_nodatacow, "nodatacow"}, 7921ad10cfSChris Mason {Opt_nobarrier, "nobarrier"}, 80c59f8951SChris Mason {Opt_max_extent, "max_extent=%s"}, 816f568d35SChris Mason {Opt_max_inline, "max_inline=%s"}, 828f662a76SChris Mason {Opt_alloc_start, "alloc_start=%s"}, 834543df7eSChris Mason {Opt_thread_pool, "thread_pool=%d"}, 84c8b97818SChris Mason {Opt_compress, "compress"}, 85e18e4809SChris Mason {Opt_ssd, "ssd"}, 86451d7585SChris Mason {Opt_ssd_spread, "ssd_spread"}, 873b30c22fSChris Mason {Opt_nossd, "nossd"}, 8833268eafSJosef Bacik {Opt_noacl, "noacl"}, 893a5e1404SSage Weil {Opt_notreelog, "notreelog"}, 90dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 9197e728d4SJosef Bacik {Opt_ratio, "metadata_ratio=%d"}, 9233268eafSJosef Bacik {Opt_err, NULL}, 9395e05289SChris Mason }; 9495e05289SChris Mason 95edbd8d4eSChris Mason u64 btrfs_parse_size(char *str) 96c59f8951SChris Mason { 97edbd8d4eSChris Mason u64 res; 98c59f8951SChris Mason int mult = 1; 99c59f8951SChris Mason char *end; 100c59f8951SChris Mason char last; 101c59f8951SChris Mason 102c59f8951SChris Mason res = simple_strtoul(str, &end, 10); 103c59f8951SChris Mason 104c59f8951SChris Mason last = end[0]; 105c59f8951SChris Mason if (isalpha(last)) { 106c59f8951SChris Mason last = tolower(last); 107c59f8951SChris Mason switch (last) { 108c59f8951SChris Mason case 'g': 109c59f8951SChris Mason mult *= 1024; 110c59f8951SChris Mason case 'm': 111c59f8951SChris Mason mult *= 1024; 112c59f8951SChris Mason case 'k': 113c59f8951SChris Mason mult *= 1024; 114c59f8951SChris Mason } 115c59f8951SChris Mason res = res * mult; 116c59f8951SChris Mason } 117c59f8951SChris Mason return res; 118c59f8951SChris Mason } 119c59f8951SChris Mason 120edf24abeSChristoph Hellwig /* 121edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 122edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 123edf24abeSChristoph Hellwig */ 124edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options) 12595e05289SChris Mason { 126edf24abeSChristoph Hellwig struct btrfs_fs_info *info = root->fs_info; 12795e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 128edf24abeSChristoph Hellwig char *p, *num; 1294543df7eSChris Mason int intarg; 130b6cda9bcSChris Mason 13195e05289SChris Mason if (!options) 132edf24abeSChristoph Hellwig return 0; 13395e05289SChris Mason 134be20aa9dSChris Mason /* 135be20aa9dSChris Mason * strsep changes the string, duplicate it because parse_options 136be20aa9dSChris Mason * gets called twice 137be20aa9dSChris Mason */ 138be20aa9dSChris Mason options = kstrdup(options, GFP_NOFS); 139be20aa9dSChris Mason if (!options) 140be20aa9dSChris Mason return -ENOMEM; 141be20aa9dSChris Mason 142be20aa9dSChris Mason 14395e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 14495e05289SChris Mason int token; 14595e05289SChris Mason if (!*p) 14695e05289SChris Mason continue; 14795e05289SChris Mason 14895e05289SChris Mason token = match_token(p, tokens, args); 14995e05289SChris Mason switch (token) { 150dfe25020SChris Mason case Opt_degraded: 151edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: allowing degraded mounts\n"); 152dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 153dfe25020SChris Mason break; 15495e05289SChris Mason case Opt_subvol: 15543e570b0SChristoph Hellwig case Opt_device: 156edf24abeSChristoph Hellwig /* 15743e570b0SChristoph Hellwig * These are parsed by btrfs_parse_early_options 158edf24abeSChristoph Hellwig * and can be happily ignored here. 159edf24abeSChristoph Hellwig */ 16095e05289SChris Mason break; 161b6cda9bcSChris Mason case Opt_nodatasum: 162*067c28adSChris Mason printk(KERN_INFO "btrfs: setting nodatasum\n"); 163b6cda9bcSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 164be20aa9dSChris Mason break; 165be20aa9dSChris Mason case Opt_nodatacow: 166edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: setting nodatacow\n"); 167be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 168be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 169b6cda9bcSChris Mason break; 170c8b97818SChris Mason case Opt_compress: 171c8b97818SChris Mason printk(KERN_INFO "btrfs: use compression\n"); 172c8b97818SChris Mason btrfs_set_opt(info->mount_opt, COMPRESS); 173c8b97818SChris Mason break; 174e18e4809SChris Mason case Opt_ssd: 175edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: use ssd allocation scheme\n"); 176e18e4809SChris Mason btrfs_set_opt(info->mount_opt, SSD); 177e18e4809SChris Mason break; 178451d7585SChris Mason case Opt_ssd_spread: 179451d7585SChris Mason printk(KERN_INFO "btrfs: use spread ssd " 180451d7585SChris Mason "allocation scheme\n"); 181451d7585SChris Mason btrfs_set_opt(info->mount_opt, SSD); 182451d7585SChris Mason btrfs_set_opt(info->mount_opt, SSD_SPREAD); 183451d7585SChris Mason break; 1843b30c22fSChris Mason case Opt_nossd: 185451d7585SChris Mason printk(KERN_INFO "btrfs: not using ssd allocation " 186451d7585SChris Mason "scheme\n"); 187c289811cSChris Mason btrfs_set_opt(info->mount_opt, NOSSD); 1883b30c22fSChris Mason btrfs_clear_opt(info->mount_opt, SSD); 189451d7585SChris Mason btrfs_clear_opt(info->mount_opt, SSD_SPREAD); 1903b30c22fSChris Mason break; 19121ad10cfSChris Mason case Opt_nobarrier: 192edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: turning off barriers\n"); 19321ad10cfSChris Mason btrfs_set_opt(info->mount_opt, NOBARRIER); 19421ad10cfSChris Mason break; 1954543df7eSChris Mason case Opt_thread_pool: 1964543df7eSChris Mason intarg = 0; 1974543df7eSChris Mason match_int(&args[0], &intarg); 1984543df7eSChris Mason if (intarg) { 1994543df7eSChris Mason info->thread_pool_size = intarg; 2004543df7eSChris Mason printk(KERN_INFO "btrfs: thread pool %d\n", 2014543df7eSChris Mason info->thread_pool_size); 2024543df7eSChris Mason } 2034543df7eSChris Mason break; 204c59f8951SChris Mason case Opt_max_extent: 205edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 206c59f8951SChris Mason if (num) { 207edf24abeSChristoph Hellwig info->max_extent = btrfs_parse_size(num); 208c59f8951SChris Mason kfree(num); 209c59f8951SChris Mason 210c59f8951SChris Mason info->max_extent = max_t(u64, 211edf24abeSChristoph Hellwig info->max_extent, root->sectorsize); 212edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: max_extent at %llu\n", 21321380931SJoel Becker (unsigned long long)info->max_extent); 214c59f8951SChris Mason } 215c59f8951SChris Mason break; 2166f568d35SChris Mason case Opt_max_inline: 217edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 2186f568d35SChris Mason if (num) { 219edf24abeSChristoph Hellwig info->max_inline = btrfs_parse_size(num); 2206f568d35SChris Mason kfree(num); 2216f568d35SChris Mason 22215ada040SChris Mason if (info->max_inline) { 2236f568d35SChris Mason info->max_inline = max_t(u64, 22415ada040SChris Mason info->max_inline, 22515ada040SChris Mason root->sectorsize); 22615ada040SChris Mason } 227edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: max_inline at %llu\n", 22821380931SJoel Becker (unsigned long long)info->max_inline); 2296f568d35SChris Mason } 2306f568d35SChris Mason break; 2318f662a76SChris Mason case Opt_alloc_start: 232edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 2338f662a76SChris Mason if (num) { 234edf24abeSChristoph Hellwig info->alloc_start = btrfs_parse_size(num); 2358f662a76SChris Mason kfree(num); 236edf24abeSChristoph Hellwig printk(KERN_INFO 237edf24abeSChristoph Hellwig "btrfs: allocations start at %llu\n", 23821380931SJoel Becker (unsigned long long)info->alloc_start); 2398f662a76SChris Mason } 2408f662a76SChris Mason break; 24133268eafSJosef Bacik case Opt_noacl: 24233268eafSJosef Bacik root->fs_info->sb->s_flags &= ~MS_POSIXACL; 24333268eafSJosef Bacik break; 2443a5e1404SSage Weil case Opt_notreelog: 2453a5e1404SSage Weil printk(KERN_INFO "btrfs: disabling tree log\n"); 2463a5e1404SSage Weil btrfs_set_opt(info->mount_opt, NOTREELOG); 2473a5e1404SSage Weil break; 248dccae999SSage Weil case Opt_flushoncommit: 249dccae999SSage Weil printk(KERN_INFO "btrfs: turning on flush-on-commit\n"); 250dccae999SSage Weil btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT); 251dccae999SSage Weil break; 25297e728d4SJosef Bacik case Opt_ratio: 25397e728d4SJosef Bacik intarg = 0; 25497e728d4SJosef Bacik match_int(&args[0], &intarg); 25597e728d4SJosef Bacik if (intarg) { 25697e728d4SJosef Bacik info->metadata_ratio = intarg; 25797e728d4SJosef Bacik printk(KERN_INFO "btrfs: metadata ratio %d\n", 25897e728d4SJosef Bacik info->metadata_ratio); 25997e728d4SJosef Bacik } 26097e728d4SJosef Bacik break; 26195e05289SChris Mason default: 262be20aa9dSChris Mason break; 26395e05289SChris Mason } 26495e05289SChris Mason } 265be20aa9dSChris Mason kfree(options); 266edf24abeSChristoph Hellwig return 0; 267edf24abeSChristoph Hellwig } 268edf24abeSChristoph Hellwig 269edf24abeSChristoph Hellwig /* 270edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 271edf24abeSChristoph Hellwig * 272edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 273edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 274edf24abeSChristoph Hellwig */ 27597288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 27643e570b0SChristoph Hellwig void *holder, char **subvol_name, 27743e570b0SChristoph Hellwig struct btrfs_fs_devices **fs_devices) 278edf24abeSChristoph Hellwig { 279edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 280edf24abeSChristoph Hellwig char *opts, *p; 281edf24abeSChristoph Hellwig int error = 0; 282edf24abeSChristoph Hellwig 283edf24abeSChristoph Hellwig if (!options) 284edf24abeSChristoph Hellwig goto out; 285edf24abeSChristoph Hellwig 286edf24abeSChristoph Hellwig /* 287edf24abeSChristoph Hellwig * strsep changes the string, duplicate it because parse_options 288edf24abeSChristoph Hellwig * gets called twice 289edf24abeSChristoph Hellwig */ 290edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 291edf24abeSChristoph Hellwig if (!opts) 292edf24abeSChristoph Hellwig return -ENOMEM; 293edf24abeSChristoph Hellwig 294edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 295edf24abeSChristoph Hellwig int token; 296edf24abeSChristoph Hellwig if (!*p) 297edf24abeSChristoph Hellwig continue; 298edf24abeSChristoph Hellwig 299edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 300edf24abeSChristoph Hellwig switch (token) { 301edf24abeSChristoph Hellwig case Opt_subvol: 302edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 303edf24abeSChristoph Hellwig break; 30443e570b0SChristoph Hellwig case Opt_device: 30543e570b0SChristoph Hellwig error = btrfs_scan_one_device(match_strdup(&args[0]), 30643e570b0SChristoph Hellwig flags, holder, fs_devices); 30743e570b0SChristoph Hellwig if (error) 30843e570b0SChristoph Hellwig goto out_free_opts; 30943e570b0SChristoph Hellwig break; 310edf24abeSChristoph Hellwig default: 311edf24abeSChristoph Hellwig break; 312edf24abeSChristoph Hellwig } 313edf24abeSChristoph Hellwig } 314edf24abeSChristoph Hellwig 31543e570b0SChristoph Hellwig out_free_opts: 316edf24abeSChristoph Hellwig kfree(opts); 317edf24abeSChristoph Hellwig out: 318edf24abeSChristoph Hellwig /* 319edf24abeSChristoph Hellwig * If no subvolume name is specified we use the default one. Allocate 3203de4586cSChris Mason * a copy of the string "." here so that code later in the 321edf24abeSChristoph Hellwig * mount path doesn't care if it's the default volume or another one. 322edf24abeSChristoph Hellwig */ 323edf24abeSChristoph Hellwig if (!*subvol_name) { 3243de4586cSChris Mason *subvol_name = kstrdup(".", GFP_KERNEL); 325edf24abeSChristoph Hellwig if (!*subvol_name) 326edf24abeSChristoph Hellwig return -ENOMEM; 327edf24abeSChristoph Hellwig } 328edf24abeSChristoph Hellwig return error; 32995e05289SChris Mason } 33095e05289SChris Mason 3318a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 3328a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 3338a4b83ccSChris Mason void *data, int silent) 3342e635a27SChris Mason { 3352e635a27SChris Mason struct inode *inode; 336e20d96d6SChris Mason struct dentry *root_dentry; 337e20d96d6SChris Mason struct btrfs_super_block *disk_super; 3380f7d52f4SChris Mason struct btrfs_root *tree_root; 3395d4f98a2SYan Zheng struct btrfs_key key; 34039279cc3SChris Mason int err; 3412e635a27SChris Mason 3422e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 3432e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 344e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 345be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 3465103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 3472e635a27SChris Mason sb->s_time_gran = 1; 34833268eafSJosef Bacik sb->s_flags |= MS_POSIXACL; 349e20d96d6SChris Mason 350dfe25020SChris Mason tree_root = open_ctree(sb, fs_devices, (char *)data); 351d98237b3SChris Mason 352e58ca020SYan if (IS_ERR(tree_root)) { 353e20d96d6SChris Mason printk("btrfs: open_ctree failed\n"); 354e58ca020SYan return PTR_ERR(tree_root); 355e20d96d6SChris Mason } 3560f7d52f4SChris Mason sb->s_fs_info = tree_root; 3575f39d397SChris Mason disk_super = &tree_root->fs_info->super_copy; 358b888db2bSChris Mason 3595d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 3605d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 3615d4f98a2SYan Zheng key.offset = 0; 3625d4f98a2SYan Zheng inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root); 3635d4f98a2SYan Zheng if (IS_ERR(inode)) { 3645d4f98a2SYan Zheng err = PTR_ERR(inode); 36539279cc3SChris Mason goto fail_close; 36639279cc3SChris Mason } 3672e635a27SChris Mason 368e20d96d6SChris Mason root_dentry = d_alloc_root(inode); 369e20d96d6SChris Mason if (!root_dentry) { 3702e635a27SChris Mason iput(inode); 37139279cc3SChris Mason err = -ENOMEM; 37239279cc3SChris Mason goto fail_close; 3732e635a27SChris Mason } 374e4404d6eSYan Zheng #if 0 37558176a96SJosef Bacik /* this does the super kobj at the same time */ 37658176a96SJosef Bacik err = btrfs_sysfs_add_super(tree_root->fs_info); 37758176a96SJosef Bacik if (err) 37858176a96SJosef Bacik goto fail_close; 379e4404d6eSYan Zheng #endif 38058176a96SJosef Bacik 381e20d96d6SChris Mason sb->s_root = root_dentry; 3826885f308SChris Mason 3836885f308SChris Mason save_mount_options(sb, data); 3842e635a27SChris Mason return 0; 3852e635a27SChris Mason 38639279cc3SChris Mason fail_close: 38739279cc3SChris Mason close_ctree(tree_root); 388d5719762SChris Mason return err; 389d5719762SChris Mason } 390d5719762SChris Mason 3916bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 392d5719762SChris Mason { 393d5719762SChris Mason struct btrfs_trans_handle *trans; 394dccae999SSage Weil struct btrfs_root *root = btrfs_sb(sb); 395d5719762SChris Mason int ret; 396df2ce34cSChris Mason 397c146afadSYan Zheng if (sb->s_flags & MS_RDONLY) 398c146afadSYan Zheng return 0; 399c146afadSYan Zheng 400d5719762SChris Mason sb->s_dirt = 0; 401d561c025SChris Mason if (!wait) { 4027cfcc17eSChris Mason filemap_flush(root->fs_info->btree_inode->i_mapping); 403df2ce34cSChris Mason return 0; 404d561c025SChris Mason } 405771ed689SChris Mason 406771ed689SChris Mason btrfs_start_delalloc_inodes(root); 407771ed689SChris Mason btrfs_wait_ordered_extents(root, 0); 408771ed689SChris Mason 409d5719762SChris Mason trans = btrfs_start_transaction(root, 1); 410d5719762SChris Mason ret = btrfs_commit_transaction(trans, root); 411d5719762SChris Mason sb->s_dirt = 0; 41254aa1f4dSChris Mason return ret; 413d5719762SChris Mason } 414d5719762SChris Mason 415a9572a15SEric Paris static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs) 416a9572a15SEric Paris { 417a9572a15SEric Paris struct btrfs_root *root = btrfs_sb(vfs->mnt_sb); 418a9572a15SEric Paris struct btrfs_fs_info *info = root->fs_info; 419a9572a15SEric Paris 420a9572a15SEric Paris if (btrfs_test_opt(root, DEGRADED)) 421a9572a15SEric Paris seq_puts(seq, ",degraded"); 422a9572a15SEric Paris if (btrfs_test_opt(root, NODATASUM)) 423a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 424a9572a15SEric Paris if (btrfs_test_opt(root, NODATACOW)) 425a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 426a9572a15SEric Paris if (btrfs_test_opt(root, NOBARRIER)) 427a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 428a9572a15SEric Paris if (info->max_extent != (u64)-1) 42921380931SJoel Becker seq_printf(seq, ",max_extent=%llu", 43021380931SJoel Becker (unsigned long long)info->max_extent); 431a9572a15SEric Paris if (info->max_inline != 8192 * 1024) 43221380931SJoel Becker seq_printf(seq, ",max_inline=%llu", 43321380931SJoel Becker (unsigned long long)info->max_inline); 434a9572a15SEric Paris if (info->alloc_start != 0) 43521380931SJoel Becker seq_printf(seq, ",alloc_start=%llu", 43621380931SJoel Becker (unsigned long long)info->alloc_start); 437a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 438a9572a15SEric Paris num_online_cpus() + 2, 8)) 439a9572a15SEric Paris seq_printf(seq, ",thread_pool=%d", info->thread_pool_size); 440a9572a15SEric Paris if (btrfs_test_opt(root, COMPRESS)) 441a9572a15SEric Paris seq_puts(seq, ",compress"); 442c289811cSChris Mason if (btrfs_test_opt(root, NOSSD)) 443c289811cSChris Mason seq_puts(seq, ",nossd"); 444451d7585SChris Mason if (btrfs_test_opt(root, SSD_SPREAD)) 445451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 446451d7585SChris Mason else if (btrfs_test_opt(root, SSD)) 447a9572a15SEric Paris seq_puts(seq, ",ssd"); 4483a5e1404SSage Weil if (btrfs_test_opt(root, NOTREELOG)) 4496b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 450dccae999SSage Weil if (btrfs_test_opt(root, FLUSHONCOMMIT)) 4516b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 452a9572a15SEric Paris if (!(root->fs_info->sb->s_flags & MS_POSIXACL)) 453a9572a15SEric Paris seq_puts(seq, ",noacl"); 454a9572a15SEric Paris return 0; 455a9572a15SEric Paris } 456a9572a15SEric Paris 457d561c025SChris Mason static void btrfs_write_super(struct super_block *sb) 458d561c025SChris Mason { 45908607c1bSChris Mason sb->s_dirt = 0; 460d561c025SChris Mason } 461d561c025SChris Mason 462a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 4632e635a27SChris Mason { 464a061fc8dSChris Mason struct btrfs_fs_devices *test_fs_devices = data; 465a061fc8dSChris Mason struct btrfs_root *root = btrfs_sb(s); 4664b82d6e4SYan 467a061fc8dSChris Mason return root->fs_info->fs_devices == test_fs_devices; 4684b82d6e4SYan } 4694b82d6e4SYan 470edf24abeSChristoph Hellwig /* 471edf24abeSChristoph Hellwig * Find a superblock for the given device / mount point. 472edf24abeSChristoph Hellwig * 473edf24abeSChristoph Hellwig * Note: This is based on get_sb_bdev from fs/super.c with a few additions 474edf24abeSChristoph Hellwig * for multiple device setup. Make sure to keep it in sync. 475edf24abeSChristoph Hellwig */ 476edf24abeSChristoph Hellwig static int btrfs_get_sb(struct file_system_type *fs_type, int flags, 477edf24abeSChristoph Hellwig const char *dev_name, void *data, struct vfsmount *mnt) 4784b82d6e4SYan { 479edf24abeSChristoph Hellwig char *subvol_name = NULL; 4804b82d6e4SYan struct block_device *bdev = NULL; 4814b82d6e4SYan struct super_block *s; 4824b82d6e4SYan struct dentry *root; 4838a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices = NULL; 48497288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 4854b82d6e4SYan int error = 0; 4864b82d6e4SYan 48797288f2cSChristoph Hellwig if (!(flags & MS_RDONLY)) 48897288f2cSChristoph Hellwig mode |= FMODE_WRITE; 48997288f2cSChristoph Hellwig 49097288f2cSChristoph Hellwig error = btrfs_parse_early_options(data, mode, fs_type, 49143e570b0SChristoph Hellwig &subvol_name, &fs_devices); 492edf24abeSChristoph Hellwig if (error) 4931f483660SShen Feng return error; 494edf24abeSChristoph Hellwig 49597288f2cSChristoph Hellwig error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices); 4968a4b83ccSChris Mason if (error) 497edf24abeSChristoph Hellwig goto error_free_subvol_name; 4984b82d6e4SYan 49997288f2cSChristoph Hellwig error = btrfs_open_devices(fs_devices, mode, fs_type); 5008a4b83ccSChris Mason if (error) 501edf24abeSChristoph Hellwig goto error_free_subvol_name; 5028a4b83ccSChris Mason 5032b82032cSYan Zheng if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) { 5042b82032cSYan Zheng error = -EACCES; 5052b82032cSYan Zheng goto error_close_devices; 5062b82032cSYan Zheng } 5072b82032cSYan Zheng 508dfe25020SChris Mason bdev = fs_devices->latest_bdev; 509a061fc8dSChris Mason s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices); 5104b82d6e4SYan if (IS_ERR(s)) 5114b82d6e4SYan goto error_s; 5124b82d6e4SYan 5134b82d6e4SYan if (s->s_root) { 5144b82d6e4SYan if ((flags ^ s->s_flags) & MS_RDONLY) { 5156f5bbff9SAl Viro deactivate_locked_super(s); 5164b82d6e4SYan error = -EBUSY; 517c146afadSYan Zheng goto error_close_devices; 5184b82d6e4SYan } 5194b82d6e4SYan 5202b82032cSYan Zheng btrfs_close_devices(fs_devices); 5214b82d6e4SYan } else { 5224b82d6e4SYan char b[BDEVNAME_SIZE]; 5234b82d6e4SYan 5244b82d6e4SYan s->s_flags = flags; 5254b82d6e4SYan strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); 5268a4b83ccSChris Mason error = btrfs_fill_super(s, fs_devices, data, 5278a4b83ccSChris Mason flags & MS_SILENT ? 1 : 0); 5284b82d6e4SYan if (error) { 5296f5bbff9SAl Viro deactivate_locked_super(s); 5301f483660SShen Feng goto error_free_subvol_name; 5314b82d6e4SYan } 5324b82d6e4SYan 533788f20ebSChris Mason btrfs_sb(s)->fs_info->bdev_holder = fs_type; 5344b82d6e4SYan s->s_flags |= MS_ACTIVE; 5354b82d6e4SYan } 5364b82d6e4SYan 53776fcef19SDavid Woodhouse if (!strcmp(subvol_name, ".")) 53876fcef19SDavid Woodhouse root = dget(s->s_root); 53976fcef19SDavid Woodhouse else { 540b48652c1SYan Zheng mutex_lock(&s->s_root->d_inode->i_mutex); 541d397712bSChris Mason root = lookup_one_len(subvol_name, s->s_root, 542d397712bSChris Mason strlen(subvol_name)); 543b48652c1SYan Zheng mutex_unlock(&s->s_root->d_inode->i_mutex); 544d397712bSChris Mason 5454b82d6e4SYan if (IS_ERR(root)) { 5466f5bbff9SAl Viro deactivate_locked_super(s); 5474b82d6e4SYan error = PTR_ERR(root); 5481f483660SShen Feng goto error_free_subvol_name; 5494b82d6e4SYan } 5504b82d6e4SYan if (!root->d_inode) { 5514b82d6e4SYan dput(root); 5526f5bbff9SAl Viro deactivate_locked_super(s); 5534b82d6e4SYan error = -ENXIO; 5541f483660SShen Feng goto error_free_subvol_name; 5554b82d6e4SYan } 55676fcef19SDavid Woodhouse } 5574b82d6e4SYan 5584b82d6e4SYan mnt->mnt_sb = s; 5594b82d6e4SYan mnt->mnt_root = root; 560edf24abeSChristoph Hellwig 561edf24abeSChristoph Hellwig kfree(subvol_name); 5624b82d6e4SYan return 0; 5634b82d6e4SYan 5644b82d6e4SYan error_s: 5654b82d6e4SYan error = PTR_ERR(s); 566c146afadSYan Zheng error_close_devices: 5678a4b83ccSChris Mason btrfs_close_devices(fs_devices); 568edf24abeSChristoph Hellwig error_free_subvol_name: 569edf24abeSChristoph Hellwig kfree(subvol_name); 5704b82d6e4SYan return error; 5714b82d6e4SYan } 5722e635a27SChris Mason 573c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 574c146afadSYan Zheng { 575c146afadSYan Zheng struct btrfs_root *root = btrfs_sb(sb); 576c146afadSYan Zheng int ret; 577c146afadSYan Zheng 578b288052eSChris Mason ret = btrfs_parse_options(root, data); 579b288052eSChris Mason if (ret) 580b288052eSChris Mason return -EINVAL; 581b288052eSChris Mason 582c146afadSYan Zheng if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 583c146afadSYan Zheng return 0; 584c146afadSYan Zheng 585c146afadSYan Zheng if (*flags & MS_RDONLY) { 586c146afadSYan Zheng sb->s_flags |= MS_RDONLY; 587c146afadSYan Zheng 588c146afadSYan Zheng ret = btrfs_commit_super(root); 589c146afadSYan Zheng WARN_ON(ret); 590c146afadSYan Zheng } else { 5912b82032cSYan Zheng if (root->fs_info->fs_devices->rw_devices == 0) 5922b82032cSYan Zheng return -EACCES; 5932b82032cSYan Zheng 594c146afadSYan Zheng if (btrfs_super_log_root(&root->fs_info->super_copy) != 0) 595c146afadSYan Zheng return -EINVAL; 596c146afadSYan Zheng 5975d4f98a2SYan Zheng /* recover relocation */ 5985d4f98a2SYan Zheng ret = btrfs_recover_relocation(root); 599c146afadSYan Zheng WARN_ON(ret); 600c146afadSYan Zheng 601c146afadSYan Zheng ret = btrfs_cleanup_fs_roots(root->fs_info); 602c146afadSYan Zheng WARN_ON(ret); 603c146afadSYan Zheng 604c146afadSYan Zheng sb->s_flags &= ~MS_RDONLY; 605c146afadSYan Zheng } 606c146afadSYan Zheng 607c146afadSYan Zheng return 0; 608c146afadSYan Zheng } 609c146afadSYan Zheng 6108fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 6118fd17795SChris Mason { 6128fd17795SChris Mason struct btrfs_root *root = btrfs_sb(dentry->d_sb); 6134b52dff6SChris Mason struct btrfs_super_block *disk_super = &root->fs_info->super_copy; 614db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 6159d03632eSDavid Woodhouse __be32 *fsid = (__be32 *)root->fs_info->fsid; 6168fd17795SChris Mason 6178fd17795SChris Mason buf->f_namelen = BTRFS_NAME_LEN; 618db94535dSChris Mason buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits; 619db94535dSChris Mason buf->f_bfree = buf->f_blocks - 620db94535dSChris Mason (btrfs_super_bytes_used(disk_super) >> bits); 6218fd17795SChris Mason buf->f_bavail = buf->f_bfree; 6228fd17795SChris Mason buf->f_bsize = dentry->d_sb->s_blocksize; 6238fd17795SChris Mason buf->f_type = BTRFS_SUPER_MAGIC; 624d397712bSChris Mason 6259d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 6269d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 6279d03632eSDavid Woodhouse on a big-endian or little-endian host */ 6289d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 6299d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 63032d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 63132d48fa1SDavid Woodhouse buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32; 63232d48fa1SDavid Woodhouse buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid; 63332d48fa1SDavid Woodhouse 6348fd17795SChris Mason return 0; 6358fd17795SChris Mason } 636b5133862SChris Mason 6372e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 6382e635a27SChris Mason .owner = THIS_MODULE, 6392e635a27SChris Mason .name = "btrfs", 6402e635a27SChris Mason .get_sb = btrfs_get_sb, 641a061fc8dSChris Mason .kill_sb = kill_anon_super, 6422e635a27SChris Mason .fs_flags = FS_REQUIRES_DEV, 6432e635a27SChris Mason }; 644a9218f6bSChris Mason 645d352ac68SChris Mason /* 646d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 647d352ac68SChris Mason */ 6488a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 6498a4b83ccSChris Mason unsigned long arg) 6508a4b83ccSChris Mason { 6518a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 6528a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 653c071fcfdSChris Mason int ret = -ENOTTY; 6548a4b83ccSChris Mason 655e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 656e441d54dSChris Mason return -EPERM; 657e441d54dSChris Mason 658dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 659dae7b665SLi Zefan if (IS_ERR(vol)) 660dae7b665SLi Zefan return PTR_ERR(vol); 661c071fcfdSChris Mason 6628a4b83ccSChris Mason switch (cmd) { 6638a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 66497288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 6658a4b83ccSChris Mason &btrfs_fs_type, &fs_devices); 6668a4b83ccSChris Mason break; 6678a4b83ccSChris Mason } 668dae7b665SLi Zefan 6698a4b83ccSChris Mason kfree(vol); 670f819d837SLinda Knippers return ret; 6718a4b83ccSChris Mason } 6728a4b83ccSChris Mason 6730176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 674ed0dab6bSYan { 675ed0dab6bSYan struct btrfs_root *root = btrfs_sb(sb); 676a74a4b97SChris Mason mutex_lock(&root->fs_info->transaction_kthread_mutex); 677a74a4b97SChris Mason mutex_lock(&root->fs_info->cleaner_mutex); 6780176260fSLinus Torvalds return 0; 679ed0dab6bSYan } 680ed0dab6bSYan 6810176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb) 682ed0dab6bSYan { 683ed0dab6bSYan struct btrfs_root *root = btrfs_sb(sb); 684a74a4b97SChris Mason mutex_unlock(&root->fs_info->cleaner_mutex); 685a74a4b97SChris Mason mutex_unlock(&root->fs_info->transaction_kthread_mutex); 6860176260fSLinus Torvalds return 0; 687ed0dab6bSYan } 6882e635a27SChris Mason 689e20d96d6SChris Mason static struct super_operations btrfs_super_ops = { 690134e9731SChris Mason .delete_inode = btrfs_delete_inode, 691e20d96d6SChris Mason .put_super = btrfs_put_super, 692d5719762SChris Mason .write_super = btrfs_write_super, 693d5719762SChris Mason .sync_fs = btrfs_sync_fs, 694a9572a15SEric Paris .show_options = btrfs_show_options, 6954730a4bcSChris Mason .write_inode = btrfs_write_inode, 696b5133862SChris Mason .dirty_inode = btrfs_dirty_inode, 6972c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 6982c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 6998fd17795SChris Mason .statfs = btrfs_statfs, 700c146afadSYan Zheng .remount_fs = btrfs_remount, 7010176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 7020176260fSLinus Torvalds .unfreeze_fs = btrfs_unfreeze, 703e20d96d6SChris Mason }; 704a9218f6bSChris Mason 705a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 706a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 707a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 708a9218f6bSChris Mason .owner = THIS_MODULE, 709a9218f6bSChris Mason }; 710a9218f6bSChris Mason 711a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 712a9218f6bSChris Mason .minor = MISC_DYNAMIC_MINOR, 713a9218f6bSChris Mason .name = "btrfs-control", 714a9218f6bSChris Mason .fops = &btrfs_ctl_fops 715a9218f6bSChris Mason }; 716a9218f6bSChris Mason 717a9218f6bSChris Mason static int btrfs_interface_init(void) 718a9218f6bSChris Mason { 719a9218f6bSChris Mason return misc_register(&btrfs_misc); 720a9218f6bSChris Mason } 721a9218f6bSChris Mason 722b2950863SChristoph Hellwig static void btrfs_interface_exit(void) 723a9218f6bSChris Mason { 724a9218f6bSChris Mason if (misc_deregister(&btrfs_misc) < 0) 725d397712bSChris Mason printk(KERN_INFO "misc_deregister failed for control device"); 726a9218f6bSChris Mason } 727a9218f6bSChris Mason 7282e635a27SChris Mason static int __init init_btrfs_fs(void) 7292e635a27SChris Mason { 7302c90e5d6SChris Mason int err; 73158176a96SJosef Bacik 73258176a96SJosef Bacik err = btrfs_init_sysfs(); 73358176a96SJosef Bacik if (err) 73458176a96SJosef Bacik return err; 73558176a96SJosef Bacik 73639279cc3SChris Mason err = btrfs_init_cachep(); 7372c90e5d6SChris Mason if (err) 738a74a4b97SChris Mason goto free_sysfs; 739d1310b2eSChris Mason 740d1310b2eSChris Mason err = extent_io_init(); 7412f4cbe64SWyatt Banks if (err) 7422f4cbe64SWyatt Banks goto free_cachep; 7432f4cbe64SWyatt Banks 744d1310b2eSChris Mason err = extent_map_init(); 745d1310b2eSChris Mason if (err) 746d1310b2eSChris Mason goto free_extent_io; 747d1310b2eSChris Mason 748a9218f6bSChris Mason err = btrfs_interface_init(); 7492f4cbe64SWyatt Banks if (err) 7502f4cbe64SWyatt Banks goto free_extent_map; 751c8b97818SChris Mason 752a9218f6bSChris Mason err = register_filesystem(&btrfs_fs_type); 753a9218f6bSChris Mason if (err) 754a9218f6bSChris Mason goto unregister_ioctl; 755b3c3da71SChris Mason 756b3c3da71SChris Mason printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION); 7572f4cbe64SWyatt Banks return 0; 7582f4cbe64SWyatt Banks 759a9218f6bSChris Mason unregister_ioctl: 760a9218f6bSChris Mason btrfs_interface_exit(); 7612f4cbe64SWyatt Banks free_extent_map: 7622f4cbe64SWyatt Banks extent_map_exit(); 763d1310b2eSChris Mason free_extent_io: 764d1310b2eSChris Mason extent_io_exit(); 7652f4cbe64SWyatt Banks free_cachep: 7662f4cbe64SWyatt Banks btrfs_destroy_cachep(); 767a74a4b97SChris Mason free_sysfs: 7682f4cbe64SWyatt Banks btrfs_exit_sysfs(); 7692c90e5d6SChris Mason return err; 7702e635a27SChris Mason } 7712e635a27SChris Mason 7722e635a27SChris Mason static void __exit exit_btrfs_fs(void) 7732e635a27SChris Mason { 77439279cc3SChris Mason btrfs_destroy_cachep(); 775a52d9a80SChris Mason extent_map_exit(); 776d1310b2eSChris Mason extent_io_exit(); 777a9218f6bSChris Mason btrfs_interface_exit(); 7782e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 77958176a96SJosef Bacik btrfs_exit_sysfs(); 7808a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 781c8b97818SChris Mason btrfs_zlib_exit(); 7822e635a27SChris Mason } 7832e635a27SChris Mason 7842e635a27SChris Mason module_init(init_btrfs_fs) 7852e635a27SChris Mason module_exit(exit_btrfs_fs) 7862e635a27SChris Mason 7872e635a27SChris Mason MODULE_LICENSE("GPL"); 788