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 56e20d96d6SChris Mason static struct super_operations btrfs_super_ops; 57e20d96d6SChris Mason 58e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 59e20d96d6SChris Mason { 60e20d96d6SChris Mason struct btrfs_root *root = btrfs_sb(sb); 61e20d96d6SChris Mason int ret; 62e20d96d6SChris Mason 63e20d96d6SChris Mason ret = close_ctree(root); 64e20d96d6SChris Mason sb->s_fs_info = NULL; 65e20d96d6SChris Mason } 662e635a27SChris Mason 6795e05289SChris Mason enum { 6843e570b0SChristoph Hellwig Opt_degraded, Opt_subvol, Opt_device, Opt_nodatasum, Opt_nodatacow, 69dfe25020SChris Mason Opt_max_extent, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, 703a5e1404SSage Weil Opt_ssd, Opt_thread_pool, Opt_noacl, Opt_compress, Opt_notreelog, 71dccae999SSage Weil Opt_flushoncommit, Opt_err, 7295e05289SChris Mason }; 7395e05289SChris Mason 7495e05289SChris Mason static match_table_t tokens = { 75dfe25020SChris Mason {Opt_degraded, "degraded"}, 7695e05289SChris Mason {Opt_subvol, "subvol=%s"}, 7743e570b0SChristoph Hellwig {Opt_device, "device=%s"}, 78b6cda9bcSChris Mason {Opt_nodatasum, "nodatasum"}, 79be20aa9dSChris Mason {Opt_nodatacow, "nodatacow"}, 8021ad10cfSChris Mason {Opt_nobarrier, "nobarrier"}, 81c59f8951SChris Mason {Opt_max_extent, "max_extent=%s"}, 826f568d35SChris Mason {Opt_max_inline, "max_inline=%s"}, 838f662a76SChris Mason {Opt_alloc_start, "alloc_start=%s"}, 844543df7eSChris Mason {Opt_thread_pool, "thread_pool=%d"}, 85c8b97818SChris Mason {Opt_compress, "compress"}, 86e18e4809SChris Mason {Opt_ssd, "ssd"}, 8733268eafSJosef Bacik {Opt_noacl, "noacl"}, 883a5e1404SSage Weil {Opt_notreelog, "notreelog"}, 89dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 9033268eafSJosef Bacik {Opt_err, NULL}, 9195e05289SChris Mason }; 9295e05289SChris Mason 93edbd8d4eSChris Mason u64 btrfs_parse_size(char *str) 94c59f8951SChris Mason { 95edbd8d4eSChris Mason u64 res; 96c59f8951SChris Mason int mult = 1; 97c59f8951SChris Mason char *end; 98c59f8951SChris Mason char last; 99c59f8951SChris Mason 100c59f8951SChris Mason res = simple_strtoul(str, &end, 10); 101c59f8951SChris Mason 102c59f8951SChris Mason last = end[0]; 103c59f8951SChris Mason if (isalpha(last)) { 104c59f8951SChris Mason last = tolower(last); 105c59f8951SChris Mason switch (last) { 106c59f8951SChris Mason case 'g': 107c59f8951SChris Mason mult *= 1024; 108c59f8951SChris Mason case 'm': 109c59f8951SChris Mason mult *= 1024; 110c59f8951SChris Mason case 'k': 111c59f8951SChris Mason mult *= 1024; 112c59f8951SChris Mason } 113c59f8951SChris Mason res = res * mult; 114c59f8951SChris Mason } 115c59f8951SChris Mason return res; 116c59f8951SChris Mason } 117c59f8951SChris Mason 118edf24abeSChristoph Hellwig /* 119edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 120edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 121edf24abeSChristoph Hellwig */ 122edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options) 12395e05289SChris Mason { 124edf24abeSChristoph Hellwig struct btrfs_fs_info *info = root->fs_info; 12595e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 126edf24abeSChristoph Hellwig char *p, *num; 1274543df7eSChris Mason int intarg; 128b6cda9bcSChris Mason 12995e05289SChris Mason if (!options) 130edf24abeSChristoph Hellwig return 0; 13195e05289SChris Mason 132be20aa9dSChris Mason /* 133be20aa9dSChris Mason * strsep changes the string, duplicate it because parse_options 134be20aa9dSChris Mason * gets called twice 135be20aa9dSChris Mason */ 136be20aa9dSChris Mason options = kstrdup(options, GFP_NOFS); 137be20aa9dSChris Mason if (!options) 138be20aa9dSChris Mason return -ENOMEM; 139be20aa9dSChris Mason 140be20aa9dSChris Mason 14195e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 14295e05289SChris Mason int token; 14395e05289SChris Mason if (!*p) 14495e05289SChris Mason continue; 14595e05289SChris Mason 14695e05289SChris Mason token = match_token(p, tokens, args); 14795e05289SChris Mason switch (token) { 148dfe25020SChris Mason case Opt_degraded: 149edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: allowing degraded mounts\n"); 150dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 151dfe25020SChris Mason break; 15295e05289SChris Mason case Opt_subvol: 15343e570b0SChristoph Hellwig case Opt_device: 154edf24abeSChristoph Hellwig /* 15543e570b0SChristoph Hellwig * These are parsed by btrfs_parse_early_options 156edf24abeSChristoph Hellwig * and can be happily ignored here. 157edf24abeSChristoph Hellwig */ 15895e05289SChris Mason break; 159b6cda9bcSChris Mason case Opt_nodatasum: 160edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: setting nodatacsum\n"); 161b6cda9bcSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 162be20aa9dSChris Mason break; 163be20aa9dSChris Mason case Opt_nodatacow: 164edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: setting nodatacow\n"); 165be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 166be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 167b6cda9bcSChris Mason break; 168c8b97818SChris Mason case Opt_compress: 169c8b97818SChris Mason printk(KERN_INFO "btrfs: use compression\n"); 170c8b97818SChris Mason btrfs_set_opt(info->mount_opt, COMPRESS); 171c8b97818SChris Mason break; 172e18e4809SChris Mason case Opt_ssd: 173edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: use ssd allocation scheme\n"); 174e18e4809SChris Mason btrfs_set_opt(info->mount_opt, SSD); 175e18e4809SChris Mason break; 17621ad10cfSChris Mason case Opt_nobarrier: 177edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: turning off barriers\n"); 17821ad10cfSChris Mason btrfs_set_opt(info->mount_opt, NOBARRIER); 17921ad10cfSChris Mason break; 1804543df7eSChris Mason case Opt_thread_pool: 1814543df7eSChris Mason intarg = 0; 1824543df7eSChris Mason match_int(&args[0], &intarg); 1834543df7eSChris Mason if (intarg) { 1844543df7eSChris Mason info->thread_pool_size = intarg; 1854543df7eSChris Mason printk(KERN_INFO "btrfs: thread pool %d\n", 1864543df7eSChris Mason info->thread_pool_size); 1874543df7eSChris Mason } 1884543df7eSChris Mason break; 189c59f8951SChris Mason case Opt_max_extent: 190edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 191c59f8951SChris Mason if (num) { 192edf24abeSChristoph Hellwig info->max_extent = btrfs_parse_size(num); 193c59f8951SChris Mason kfree(num); 194c59f8951SChris Mason 195c59f8951SChris Mason info->max_extent = max_t(u64, 196edf24abeSChristoph Hellwig info->max_extent, root->sectorsize); 197edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: max_extent at %llu\n", 198c59f8951SChris Mason info->max_extent); 199c59f8951SChris Mason } 200c59f8951SChris Mason break; 2016f568d35SChris Mason case Opt_max_inline: 202edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 2036f568d35SChris Mason if (num) { 204edf24abeSChristoph Hellwig info->max_inline = btrfs_parse_size(num); 2056f568d35SChris Mason kfree(num); 2066f568d35SChris Mason 20715ada040SChris Mason if (info->max_inline) { 2086f568d35SChris Mason info->max_inline = max_t(u64, 20915ada040SChris Mason info->max_inline, 21015ada040SChris Mason root->sectorsize); 21115ada040SChris Mason } 212edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: max_inline at %llu\n", 2136f568d35SChris Mason info->max_inline); 2146f568d35SChris Mason } 2156f568d35SChris Mason break; 2168f662a76SChris Mason case Opt_alloc_start: 217edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 2188f662a76SChris Mason if (num) { 219edf24abeSChristoph Hellwig info->alloc_start = btrfs_parse_size(num); 2208f662a76SChris Mason kfree(num); 221edf24abeSChristoph Hellwig printk(KERN_INFO 222edf24abeSChristoph Hellwig "btrfs: allocations start at %llu\n", 223edf24abeSChristoph Hellwig info->alloc_start); 2248f662a76SChris Mason } 2258f662a76SChris Mason break; 22633268eafSJosef Bacik case Opt_noacl: 22733268eafSJosef Bacik root->fs_info->sb->s_flags &= ~MS_POSIXACL; 22833268eafSJosef Bacik break; 2293a5e1404SSage Weil case Opt_notreelog: 2303a5e1404SSage Weil printk(KERN_INFO "btrfs: disabling tree log\n"); 2313a5e1404SSage Weil btrfs_set_opt(info->mount_opt, NOTREELOG); 2323a5e1404SSage Weil break; 233dccae999SSage Weil case Opt_flushoncommit: 234dccae999SSage Weil printk(KERN_INFO "btrfs: turning on flush-on-commit\n"); 235dccae999SSage Weil btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT); 236dccae999SSage Weil break; 23795e05289SChris Mason default: 238be20aa9dSChris Mason break; 23995e05289SChris Mason } 24095e05289SChris Mason } 241be20aa9dSChris Mason kfree(options); 242edf24abeSChristoph Hellwig return 0; 243edf24abeSChristoph Hellwig } 244edf24abeSChristoph Hellwig 245edf24abeSChristoph Hellwig /* 246edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 247edf24abeSChristoph Hellwig * 248edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 249edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 250edf24abeSChristoph Hellwig */ 25197288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 25243e570b0SChristoph Hellwig void *holder, char **subvol_name, 25343e570b0SChristoph Hellwig struct btrfs_fs_devices **fs_devices) 254edf24abeSChristoph Hellwig { 255edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 256edf24abeSChristoph Hellwig char *opts, *p; 257edf24abeSChristoph Hellwig int error = 0; 258edf24abeSChristoph Hellwig 259edf24abeSChristoph Hellwig if (!options) 260edf24abeSChristoph Hellwig goto out; 261edf24abeSChristoph Hellwig 262edf24abeSChristoph Hellwig /* 263edf24abeSChristoph Hellwig * strsep changes the string, duplicate it because parse_options 264edf24abeSChristoph Hellwig * gets called twice 265edf24abeSChristoph Hellwig */ 266edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 267edf24abeSChristoph Hellwig if (!opts) 268edf24abeSChristoph Hellwig return -ENOMEM; 269edf24abeSChristoph Hellwig 270edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 271edf24abeSChristoph Hellwig int token; 272edf24abeSChristoph Hellwig if (!*p) 273edf24abeSChristoph Hellwig continue; 274edf24abeSChristoph Hellwig 275edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 276edf24abeSChristoph Hellwig switch (token) { 277edf24abeSChristoph Hellwig case Opt_subvol: 278edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 279edf24abeSChristoph Hellwig break; 28043e570b0SChristoph Hellwig case Opt_device: 28143e570b0SChristoph Hellwig error = btrfs_scan_one_device(match_strdup(&args[0]), 28243e570b0SChristoph Hellwig flags, holder, fs_devices); 28343e570b0SChristoph Hellwig if (error) 28443e570b0SChristoph Hellwig goto out_free_opts; 28543e570b0SChristoph Hellwig break; 286edf24abeSChristoph Hellwig default: 287edf24abeSChristoph Hellwig break; 288edf24abeSChristoph Hellwig } 289edf24abeSChristoph Hellwig } 290edf24abeSChristoph Hellwig 29143e570b0SChristoph Hellwig out_free_opts: 292edf24abeSChristoph Hellwig kfree(opts); 293edf24abeSChristoph Hellwig out: 294edf24abeSChristoph Hellwig /* 295edf24abeSChristoph Hellwig * If no subvolume name is specified we use the default one. Allocate 2963de4586cSChris Mason * a copy of the string "." here so that code later in the 297edf24abeSChristoph Hellwig * mount path doesn't care if it's the default volume or another one. 298edf24abeSChristoph Hellwig */ 299edf24abeSChristoph Hellwig if (!*subvol_name) { 3003de4586cSChris Mason *subvol_name = kstrdup(".", GFP_KERNEL); 301edf24abeSChristoph Hellwig if (!*subvol_name) 302edf24abeSChristoph Hellwig return -ENOMEM; 303edf24abeSChristoph Hellwig } 304edf24abeSChristoph Hellwig return error; 30595e05289SChris Mason } 30695e05289SChris Mason 3078a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 3088a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 3098a4b83ccSChris Mason void *data, int silent) 3102e635a27SChris Mason { 3112e635a27SChris Mason struct inode *inode; 312e20d96d6SChris Mason struct dentry *root_dentry; 313e20d96d6SChris Mason struct btrfs_super_block *disk_super; 3140f7d52f4SChris Mason struct btrfs_root *tree_root; 315d6e4a428SChris Mason struct btrfs_inode *bi; 31639279cc3SChris Mason int err; 3172e635a27SChris Mason 3182e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 3192e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 320e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 321be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 3225103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 3232e635a27SChris Mason sb->s_time_gran = 1; 32433268eafSJosef Bacik sb->s_flags |= MS_POSIXACL; 325e20d96d6SChris Mason 326dfe25020SChris Mason tree_root = open_ctree(sb, fs_devices, (char *)data); 327d98237b3SChris Mason 328e58ca020SYan if (IS_ERR(tree_root)) { 329e20d96d6SChris Mason printk("btrfs: open_ctree failed\n"); 330e58ca020SYan return PTR_ERR(tree_root); 331e20d96d6SChris Mason } 3320f7d52f4SChris Mason sb->s_fs_info = tree_root; 3335f39d397SChris Mason disk_super = &tree_root->fs_info->super_copy; 3343de4586cSChris Mason inode = btrfs_iget_locked(sb, BTRFS_FIRST_FREE_OBJECTID, 3353de4586cSChris Mason tree_root->fs_info->fs_root); 336d6e4a428SChris Mason bi = BTRFS_I(inode); 337d6e4a428SChris Mason bi->location.objectid = inode->i_ino; 338d6e4a428SChris Mason bi->location.offset = 0; 3393de4586cSChris Mason bi->root = tree_root->fs_info->fs_root; 340b888db2bSChris Mason 341d6e4a428SChris Mason btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY); 342d6e4a428SChris Mason 34339279cc3SChris Mason if (!inode) { 34439279cc3SChris Mason err = -ENOMEM; 34539279cc3SChris Mason goto fail_close; 34639279cc3SChris Mason } 347e20d96d6SChris Mason if (inode->i_state & I_NEW) { 348e20d96d6SChris Mason btrfs_read_locked_inode(inode); 349e20d96d6SChris Mason unlock_new_inode(inode); 350e20d96d6SChris Mason } 3512e635a27SChris Mason 352e20d96d6SChris Mason root_dentry = d_alloc_root(inode); 353e20d96d6SChris Mason if (!root_dentry) { 3542e635a27SChris Mason iput(inode); 35539279cc3SChris Mason err = -ENOMEM; 35639279cc3SChris Mason goto fail_close; 3572e635a27SChris Mason } 358e4404d6eSYan Zheng #if 0 35958176a96SJosef Bacik /* this does the super kobj at the same time */ 36058176a96SJosef Bacik err = btrfs_sysfs_add_super(tree_root->fs_info); 36158176a96SJosef Bacik if (err) 36258176a96SJosef Bacik goto fail_close; 363e4404d6eSYan Zheng #endif 36458176a96SJosef Bacik 365e20d96d6SChris Mason sb->s_root = root_dentry; 3666885f308SChris Mason 3676885f308SChris Mason save_mount_options(sb, data); 3682e635a27SChris Mason return 0; 3692e635a27SChris Mason 37039279cc3SChris Mason fail_close: 37139279cc3SChris Mason close_ctree(tree_root); 372d5719762SChris Mason return err; 373d5719762SChris Mason } 374d5719762SChris Mason 3756bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 376d5719762SChris Mason { 377d5719762SChris Mason struct btrfs_trans_handle *trans; 378dccae999SSage Weil struct btrfs_root *root = btrfs_sb(sb); 379d5719762SChris Mason int ret; 380df2ce34cSChris Mason 381c146afadSYan Zheng if (sb->s_flags & MS_RDONLY) 382c146afadSYan Zheng return 0; 383c146afadSYan Zheng 384d5719762SChris Mason sb->s_dirt = 0; 385d561c025SChris Mason if (!wait) { 3867cfcc17eSChris Mason filemap_flush(root->fs_info->btree_inode->i_mapping); 387df2ce34cSChris Mason return 0; 388d561c025SChris Mason } 389771ed689SChris Mason 390771ed689SChris Mason btrfs_start_delalloc_inodes(root); 391771ed689SChris Mason btrfs_wait_ordered_extents(root, 0); 392771ed689SChris Mason 393d5719762SChris Mason trans = btrfs_start_transaction(root, 1); 394d5719762SChris Mason ret = btrfs_commit_transaction(trans, root); 395d5719762SChris Mason sb->s_dirt = 0; 39654aa1f4dSChris Mason return ret; 397d5719762SChris Mason } 398d5719762SChris Mason 399a9572a15SEric Paris static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs) 400a9572a15SEric Paris { 401a9572a15SEric Paris struct btrfs_root *root = btrfs_sb(vfs->mnt_sb); 402a9572a15SEric Paris struct btrfs_fs_info *info = root->fs_info; 403a9572a15SEric Paris 404a9572a15SEric Paris if (btrfs_test_opt(root, DEGRADED)) 405a9572a15SEric Paris seq_puts(seq, ",degraded"); 406a9572a15SEric Paris if (btrfs_test_opt(root, NODATASUM)) 407a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 408a9572a15SEric Paris if (btrfs_test_opt(root, NODATACOW)) 409a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 410a9572a15SEric Paris if (btrfs_test_opt(root, NOBARRIER)) 411a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 412a9572a15SEric Paris if (info->max_extent != (u64)-1) 413a9572a15SEric Paris seq_printf(seq, ",max_extent=%llu", info->max_extent); 414a9572a15SEric Paris if (info->max_inline != 8192 * 1024) 415a9572a15SEric Paris seq_printf(seq, ",max_inline=%llu", info->max_inline); 416a9572a15SEric Paris if (info->alloc_start != 0) 417a9572a15SEric Paris seq_printf(seq, ",alloc_start=%llu", info->alloc_start); 418a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 419a9572a15SEric Paris num_online_cpus() + 2, 8)) 420a9572a15SEric Paris seq_printf(seq, ",thread_pool=%d", info->thread_pool_size); 421a9572a15SEric Paris if (btrfs_test_opt(root, COMPRESS)) 422a9572a15SEric Paris seq_puts(seq, ",compress"); 423a9572a15SEric Paris if (btrfs_test_opt(root, SSD)) 424a9572a15SEric Paris seq_puts(seq, ",ssd"); 4253a5e1404SSage Weil if (btrfs_test_opt(root, NOTREELOG)) 426dccae999SSage Weil seq_puts(seq, ",no-treelog"); 427dccae999SSage Weil if (btrfs_test_opt(root, FLUSHONCOMMIT)) 428dccae999SSage Weil seq_puts(seq, ",flush-on-commit"); 429a9572a15SEric Paris if (!(root->fs_info->sb->s_flags & MS_POSIXACL)) 430a9572a15SEric Paris seq_puts(seq, ",noacl"); 431a9572a15SEric Paris return 0; 432a9572a15SEric Paris } 433a9572a15SEric Paris 434d561c025SChris Mason static void btrfs_write_super(struct super_block *sb) 435d561c025SChris Mason { 43608607c1bSChris Mason sb->s_dirt = 0; 437d561c025SChris Mason } 438d561c025SChris Mason 439a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 4402e635a27SChris Mason { 441a061fc8dSChris Mason struct btrfs_fs_devices *test_fs_devices = data; 442a061fc8dSChris Mason struct btrfs_root *root = btrfs_sb(s); 4434b82d6e4SYan 444a061fc8dSChris Mason return root->fs_info->fs_devices == test_fs_devices; 4454b82d6e4SYan } 4464b82d6e4SYan 447edf24abeSChristoph Hellwig /* 448edf24abeSChristoph Hellwig * Find a superblock for the given device / mount point. 449edf24abeSChristoph Hellwig * 450edf24abeSChristoph Hellwig * Note: This is based on get_sb_bdev from fs/super.c with a few additions 451edf24abeSChristoph Hellwig * for multiple device setup. Make sure to keep it in sync. 452edf24abeSChristoph Hellwig */ 453edf24abeSChristoph Hellwig static int btrfs_get_sb(struct file_system_type *fs_type, int flags, 454edf24abeSChristoph Hellwig const char *dev_name, void *data, struct vfsmount *mnt) 4554b82d6e4SYan { 456edf24abeSChristoph Hellwig char *subvol_name = NULL; 4574b82d6e4SYan struct block_device *bdev = NULL; 4584b82d6e4SYan struct super_block *s; 4594b82d6e4SYan struct dentry *root; 4608a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices = NULL; 46197288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 4624b82d6e4SYan int error = 0; 4634b82d6e4SYan 46497288f2cSChristoph Hellwig if (!(flags & MS_RDONLY)) 46597288f2cSChristoph Hellwig mode |= FMODE_WRITE; 46697288f2cSChristoph Hellwig 46797288f2cSChristoph Hellwig error = btrfs_parse_early_options(data, mode, fs_type, 46843e570b0SChristoph Hellwig &subvol_name, &fs_devices); 469edf24abeSChristoph Hellwig if (error) 4701f483660SShen Feng return error; 471edf24abeSChristoph Hellwig 47297288f2cSChristoph Hellwig error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices); 4738a4b83ccSChris Mason if (error) 474edf24abeSChristoph Hellwig goto error_free_subvol_name; 4754b82d6e4SYan 47697288f2cSChristoph Hellwig error = btrfs_open_devices(fs_devices, mode, fs_type); 4778a4b83ccSChris Mason if (error) 478edf24abeSChristoph Hellwig goto error_free_subvol_name; 4798a4b83ccSChris Mason 4802b82032cSYan Zheng if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) { 4812b82032cSYan Zheng error = -EACCES; 4822b82032cSYan Zheng goto error_close_devices; 4832b82032cSYan Zheng } 4842b82032cSYan Zheng 485dfe25020SChris Mason bdev = fs_devices->latest_bdev; 486a061fc8dSChris Mason s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices); 4874b82d6e4SYan if (IS_ERR(s)) 4884b82d6e4SYan goto error_s; 4894b82d6e4SYan 4904b82d6e4SYan if (s->s_root) { 4914b82d6e4SYan if ((flags ^ s->s_flags) & MS_RDONLY) { 4924b82d6e4SYan up_write(&s->s_umount); 4934b82d6e4SYan deactivate_super(s); 4944b82d6e4SYan error = -EBUSY; 495c146afadSYan Zheng goto error_close_devices; 4964b82d6e4SYan } 4974b82d6e4SYan 4982b82032cSYan Zheng btrfs_close_devices(fs_devices); 4994b82d6e4SYan } else { 5004b82d6e4SYan char b[BDEVNAME_SIZE]; 5014b82d6e4SYan 5024b82d6e4SYan s->s_flags = flags; 5034b82d6e4SYan strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); 5048a4b83ccSChris Mason error = btrfs_fill_super(s, fs_devices, data, 5058a4b83ccSChris Mason flags & MS_SILENT ? 1 : 0); 5064b82d6e4SYan if (error) { 5074b82d6e4SYan up_write(&s->s_umount); 5084b82d6e4SYan deactivate_super(s); 5091f483660SShen Feng goto error_free_subvol_name; 5104b82d6e4SYan } 5114b82d6e4SYan 512788f20ebSChris Mason btrfs_sb(s)->fs_info->bdev_holder = fs_type; 5134b82d6e4SYan s->s_flags |= MS_ACTIVE; 5144b82d6e4SYan } 5154b82d6e4SYan 51676fcef19SDavid Woodhouse if (!strcmp(subvol_name, ".")) 51776fcef19SDavid Woodhouse root = dget(s->s_root); 51876fcef19SDavid Woodhouse else { 519b48652c1SYan Zheng mutex_lock(&s->s_root->d_inode->i_mutex); 520d397712bSChris Mason root = lookup_one_len(subvol_name, s->s_root, 521d397712bSChris Mason strlen(subvol_name)); 522b48652c1SYan Zheng mutex_unlock(&s->s_root->d_inode->i_mutex); 523d397712bSChris Mason 5244b82d6e4SYan if (IS_ERR(root)) { 5254b82d6e4SYan up_write(&s->s_umount); 5264b82d6e4SYan deactivate_super(s); 5274b82d6e4SYan error = PTR_ERR(root); 5281f483660SShen Feng goto error_free_subvol_name; 5294b82d6e4SYan } 5304b82d6e4SYan if (!root->d_inode) { 5314b82d6e4SYan dput(root); 5324b82d6e4SYan up_write(&s->s_umount); 5334b82d6e4SYan deactivate_super(s); 5344b82d6e4SYan error = -ENXIO; 5351f483660SShen Feng goto error_free_subvol_name; 5364b82d6e4SYan } 53776fcef19SDavid Woodhouse } 5384b82d6e4SYan 5394b82d6e4SYan mnt->mnt_sb = s; 5404b82d6e4SYan mnt->mnt_root = root; 541edf24abeSChristoph Hellwig 542edf24abeSChristoph Hellwig kfree(subvol_name); 5434b82d6e4SYan return 0; 5444b82d6e4SYan 5454b82d6e4SYan error_s: 5464b82d6e4SYan error = PTR_ERR(s); 547c146afadSYan Zheng error_close_devices: 5488a4b83ccSChris Mason btrfs_close_devices(fs_devices); 549edf24abeSChristoph Hellwig error_free_subvol_name: 550edf24abeSChristoph Hellwig kfree(subvol_name); 5514b82d6e4SYan return error; 5524b82d6e4SYan } 5532e635a27SChris Mason 554c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 555c146afadSYan Zheng { 556c146afadSYan Zheng struct btrfs_root *root = btrfs_sb(sb); 557c146afadSYan Zheng int ret; 558c146afadSYan Zheng 559b288052eSChris Mason ret = btrfs_parse_options(root, data); 560b288052eSChris Mason if (ret) 561b288052eSChris Mason return -EINVAL; 562b288052eSChris Mason 563c146afadSYan Zheng if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 564c146afadSYan Zheng return 0; 565c146afadSYan Zheng 566c146afadSYan Zheng if (*flags & MS_RDONLY) { 567c146afadSYan Zheng sb->s_flags |= MS_RDONLY; 568c146afadSYan Zheng 569c146afadSYan Zheng ret = btrfs_commit_super(root); 570c146afadSYan Zheng WARN_ON(ret); 571c146afadSYan Zheng } else { 5722b82032cSYan Zheng if (root->fs_info->fs_devices->rw_devices == 0) 5732b82032cSYan Zheng return -EACCES; 5742b82032cSYan Zheng 575c146afadSYan Zheng if (btrfs_super_log_root(&root->fs_info->super_copy) != 0) 576c146afadSYan Zheng return -EINVAL; 577c146afadSYan Zheng 578c146afadSYan Zheng ret = btrfs_cleanup_reloc_trees(root); 579c146afadSYan Zheng WARN_ON(ret); 580c146afadSYan Zheng 581c146afadSYan Zheng ret = btrfs_cleanup_fs_roots(root->fs_info); 582c146afadSYan Zheng WARN_ON(ret); 583c146afadSYan Zheng 584c146afadSYan Zheng sb->s_flags &= ~MS_RDONLY; 585c146afadSYan Zheng } 586c146afadSYan Zheng 587c146afadSYan Zheng return 0; 588c146afadSYan Zheng } 589c146afadSYan Zheng 5908fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 5918fd17795SChris Mason { 5928fd17795SChris Mason struct btrfs_root *root = btrfs_sb(dentry->d_sb); 5934b52dff6SChris Mason struct btrfs_super_block *disk_super = &root->fs_info->super_copy; 594db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 5959d03632eSDavid Woodhouse __be32 *fsid = (__be32 *)root->fs_info->fsid; 5968fd17795SChris Mason 5978fd17795SChris Mason buf->f_namelen = BTRFS_NAME_LEN; 598db94535dSChris Mason buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits; 599db94535dSChris Mason buf->f_bfree = buf->f_blocks - 600db94535dSChris Mason (btrfs_super_bytes_used(disk_super) >> bits); 6018fd17795SChris Mason buf->f_bavail = buf->f_bfree; 6028fd17795SChris Mason buf->f_bsize = dentry->d_sb->s_blocksize; 6038fd17795SChris Mason buf->f_type = BTRFS_SUPER_MAGIC; 604d397712bSChris Mason 6059d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 6069d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 6079d03632eSDavid Woodhouse on a big-endian or little-endian host */ 6089d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 6099d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 61032d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 61132d48fa1SDavid Woodhouse buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32; 61232d48fa1SDavid Woodhouse buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid; 61332d48fa1SDavid Woodhouse 6148fd17795SChris Mason return 0; 6158fd17795SChris Mason } 616b5133862SChris Mason 6172e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 6182e635a27SChris Mason .owner = THIS_MODULE, 6192e635a27SChris Mason .name = "btrfs", 6202e635a27SChris Mason .get_sb = btrfs_get_sb, 621a061fc8dSChris Mason .kill_sb = kill_anon_super, 6222e635a27SChris Mason .fs_flags = FS_REQUIRES_DEV, 6232e635a27SChris Mason }; 624a9218f6bSChris Mason 625d352ac68SChris Mason /* 626d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 627d352ac68SChris Mason */ 6288a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 6298a4b83ccSChris Mason unsigned long arg) 6308a4b83ccSChris Mason { 6318a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 6328a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 633c071fcfdSChris Mason int ret = -ENOTTY; 6348a4b83ccSChris Mason 635e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 636e441d54dSChris Mason return -EPERM; 637e441d54dSChris Mason 638*dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 639*dae7b665SLi Zefan if (IS_ERR(vol)) 640*dae7b665SLi Zefan return PTR_ERR(vol); 641c071fcfdSChris Mason 6428a4b83ccSChris Mason switch (cmd) { 6438a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 64497288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 6458a4b83ccSChris Mason &btrfs_fs_type, &fs_devices); 6468a4b83ccSChris Mason break; 6478a4b83ccSChris Mason } 648*dae7b665SLi Zefan 6498a4b83ccSChris Mason kfree(vol); 650f819d837SLinda Knippers return ret; 6518a4b83ccSChris Mason } 6528a4b83ccSChris Mason 6530176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 654ed0dab6bSYan { 655ed0dab6bSYan struct btrfs_root *root = btrfs_sb(sb); 656a74a4b97SChris Mason mutex_lock(&root->fs_info->transaction_kthread_mutex); 657a74a4b97SChris Mason mutex_lock(&root->fs_info->cleaner_mutex); 6580176260fSLinus Torvalds return 0; 659ed0dab6bSYan } 660ed0dab6bSYan 6610176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb) 662ed0dab6bSYan { 663ed0dab6bSYan struct btrfs_root *root = btrfs_sb(sb); 664a74a4b97SChris Mason mutex_unlock(&root->fs_info->cleaner_mutex); 665a74a4b97SChris Mason mutex_unlock(&root->fs_info->transaction_kthread_mutex); 6660176260fSLinus Torvalds return 0; 667ed0dab6bSYan } 6682e635a27SChris Mason 669e20d96d6SChris Mason static struct super_operations btrfs_super_ops = { 670134e9731SChris Mason .delete_inode = btrfs_delete_inode, 671e20d96d6SChris Mason .put_super = btrfs_put_super, 672d5719762SChris Mason .write_super = btrfs_write_super, 673d5719762SChris Mason .sync_fs = btrfs_sync_fs, 674a9572a15SEric Paris .show_options = btrfs_show_options, 6754730a4bcSChris Mason .write_inode = btrfs_write_inode, 676b5133862SChris Mason .dirty_inode = btrfs_dirty_inode, 6772c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 6782c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 6798fd17795SChris Mason .statfs = btrfs_statfs, 680c146afadSYan Zheng .remount_fs = btrfs_remount, 6810176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 6820176260fSLinus Torvalds .unfreeze_fs = btrfs_unfreeze, 683e20d96d6SChris Mason }; 684a9218f6bSChris Mason 685a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 686a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 687a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 688a9218f6bSChris Mason .owner = THIS_MODULE, 689a9218f6bSChris Mason }; 690a9218f6bSChris Mason 691a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 692a9218f6bSChris Mason .minor = MISC_DYNAMIC_MINOR, 693a9218f6bSChris Mason .name = "btrfs-control", 694a9218f6bSChris Mason .fops = &btrfs_ctl_fops 695a9218f6bSChris Mason }; 696a9218f6bSChris Mason 697a9218f6bSChris Mason static int btrfs_interface_init(void) 698a9218f6bSChris Mason { 699a9218f6bSChris Mason return misc_register(&btrfs_misc); 700a9218f6bSChris Mason } 701a9218f6bSChris Mason 702b2950863SChristoph Hellwig static void btrfs_interface_exit(void) 703a9218f6bSChris Mason { 704a9218f6bSChris Mason if (misc_deregister(&btrfs_misc) < 0) 705d397712bSChris Mason printk(KERN_INFO "misc_deregister failed for control device"); 706a9218f6bSChris Mason } 707a9218f6bSChris Mason 7082e635a27SChris Mason static int __init init_btrfs_fs(void) 7092e635a27SChris Mason { 7102c90e5d6SChris Mason int err; 71158176a96SJosef Bacik 71258176a96SJosef Bacik err = btrfs_init_sysfs(); 71358176a96SJosef Bacik if (err) 71458176a96SJosef Bacik return err; 71558176a96SJosef Bacik 71639279cc3SChris Mason err = btrfs_init_cachep(); 7172c90e5d6SChris Mason if (err) 718a74a4b97SChris Mason goto free_sysfs; 719d1310b2eSChris Mason 720d1310b2eSChris Mason err = extent_io_init(); 7212f4cbe64SWyatt Banks if (err) 7222f4cbe64SWyatt Banks goto free_cachep; 7232f4cbe64SWyatt Banks 724d1310b2eSChris Mason err = extent_map_init(); 725d1310b2eSChris Mason if (err) 726d1310b2eSChris Mason goto free_extent_io; 727d1310b2eSChris Mason 728a9218f6bSChris Mason err = btrfs_interface_init(); 7292f4cbe64SWyatt Banks if (err) 7302f4cbe64SWyatt Banks goto free_extent_map; 731c8b97818SChris Mason 732a9218f6bSChris Mason err = register_filesystem(&btrfs_fs_type); 733a9218f6bSChris Mason if (err) 734a9218f6bSChris Mason goto unregister_ioctl; 735b3c3da71SChris Mason 736b3c3da71SChris Mason printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION); 7372f4cbe64SWyatt Banks return 0; 7382f4cbe64SWyatt Banks 739a9218f6bSChris Mason unregister_ioctl: 740a9218f6bSChris Mason btrfs_interface_exit(); 7412f4cbe64SWyatt Banks free_extent_map: 7422f4cbe64SWyatt Banks extent_map_exit(); 743d1310b2eSChris Mason free_extent_io: 744d1310b2eSChris Mason extent_io_exit(); 7452f4cbe64SWyatt Banks free_cachep: 7462f4cbe64SWyatt Banks btrfs_destroy_cachep(); 747a74a4b97SChris Mason free_sysfs: 7482f4cbe64SWyatt Banks btrfs_exit_sysfs(); 7492c90e5d6SChris Mason return err; 7502e635a27SChris Mason } 7512e635a27SChris Mason 7522e635a27SChris Mason static void __exit exit_btrfs_fs(void) 7532e635a27SChris Mason { 75439279cc3SChris Mason btrfs_destroy_cachep(); 755a52d9a80SChris Mason extent_map_exit(); 756d1310b2eSChris Mason extent_io_exit(); 757a9218f6bSChris Mason btrfs_interface_exit(); 7582e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 75958176a96SJosef Bacik btrfs_exit_sysfs(); 7608a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 761c8b97818SChris Mason btrfs_zlib_exit(); 7622e635a27SChris Mason } 7632e635a27SChris Mason 7642e635a27SChris Mason module_init(init_btrfs_fs) 7652e635a27SChris Mason module_exit(exit_btrfs_fs) 7662e635a27SChris Mason 7672e635a27SChris Mason MODULE_LICENSE("GPL"); 768