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> 415a0e3ad6STejun Heo #include <linux/slab.h> 4290a887c9SDan Magenheimer #include <linux/cleancache.h> 43830c4adbSJosef Bacik #include <linux/mnt_namespace.h> 444b4e25f2SChris Mason #include "compat.h" 4516cdcec7SMiao Xie #include "delayed-inode.h" 462e635a27SChris Mason #include "ctree.h" 47e20d96d6SChris Mason #include "disk-io.h" 48d5719762SChris Mason #include "transaction.h" 492c90e5d6SChris Mason #include "btrfs_inode.h" 50c5739bbaSChris Mason #include "ioctl.h" 513a686375SChris Mason #include "print-tree.h" 525103e947SJosef Bacik #include "xattr.h" 538a4b83ccSChris Mason #include "volumes.h" 54b3c3da71SChris Mason #include "version.h" 55be6e8dc0SBalaji Rao #include "export.h" 56c8b97818SChris Mason #include "compression.h" 572e635a27SChris Mason 581abe9b8aSliubo #define CREATE_TRACE_POINTS 591abe9b8aSliubo #include <trace/events/btrfs.h> 601abe9b8aSliubo 61b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops; 62830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type; 63e20d96d6SChris Mason 64acce952bSliubo static const char *btrfs_decode_error(struct btrfs_fs_info *fs_info, int errno, 65acce952bSliubo char nbuf[16]) 66acce952bSliubo { 67acce952bSliubo char *errstr = NULL; 68acce952bSliubo 69acce952bSliubo switch (errno) { 70acce952bSliubo case -EIO: 71acce952bSliubo errstr = "IO failure"; 72acce952bSliubo break; 73acce952bSliubo case -ENOMEM: 74acce952bSliubo errstr = "Out of memory"; 75acce952bSliubo break; 76acce952bSliubo case -EROFS: 77acce952bSliubo errstr = "Readonly filesystem"; 78acce952bSliubo break; 79acce952bSliubo default: 80acce952bSliubo if (nbuf) { 81acce952bSliubo if (snprintf(nbuf, 16, "error %d", -errno) >= 0) 82acce952bSliubo errstr = nbuf; 83acce952bSliubo } 84acce952bSliubo break; 85acce952bSliubo } 86acce952bSliubo 87acce952bSliubo return errstr; 88acce952bSliubo } 89acce952bSliubo 90acce952bSliubo static void __save_error_info(struct btrfs_fs_info *fs_info) 91acce952bSliubo { 92acce952bSliubo /* 93acce952bSliubo * today we only save the error info into ram. Long term we'll 94acce952bSliubo * also send it down to the disk 95acce952bSliubo */ 96acce952bSliubo fs_info->fs_state = BTRFS_SUPER_FLAG_ERROR; 97acce952bSliubo } 98acce952bSliubo 99acce952bSliubo /* NOTE: 100acce952bSliubo * We move write_super stuff at umount in order to avoid deadlock 101acce952bSliubo * for umount hold all lock. 102acce952bSliubo */ 103acce952bSliubo static void save_error_info(struct btrfs_fs_info *fs_info) 104acce952bSliubo { 105acce952bSliubo __save_error_info(fs_info); 106acce952bSliubo } 107acce952bSliubo 108acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */ 109acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info) 110acce952bSliubo { 111acce952bSliubo struct super_block *sb = fs_info->sb; 112acce952bSliubo 113acce952bSliubo if (sb->s_flags & MS_RDONLY) 114acce952bSliubo return; 115acce952bSliubo 116acce952bSliubo if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) { 117acce952bSliubo sb->s_flags |= MS_RDONLY; 118acce952bSliubo printk(KERN_INFO "btrfs is forced readonly\n"); 119acce952bSliubo } 120acce952bSliubo } 121acce952bSliubo 122acce952bSliubo /* 123acce952bSliubo * __btrfs_std_error decodes expected errors from the caller and 124acce952bSliubo * invokes the approciate error response. 125acce952bSliubo */ 126acce952bSliubo void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function, 127acce952bSliubo unsigned int line, int errno) 128acce952bSliubo { 129acce952bSliubo struct super_block *sb = fs_info->sb; 130acce952bSliubo char nbuf[16]; 131acce952bSliubo const char *errstr; 132acce952bSliubo 133acce952bSliubo /* 134acce952bSliubo * Special case: if the error is EROFS, and we're already 135acce952bSliubo * under MS_RDONLY, then it is safe here. 136acce952bSliubo */ 137acce952bSliubo if (errno == -EROFS && (sb->s_flags & MS_RDONLY)) 138acce952bSliubo return; 139acce952bSliubo 140acce952bSliubo errstr = btrfs_decode_error(fs_info, errno, nbuf); 141acce952bSliubo printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: %s\n", 142acce952bSliubo sb->s_id, function, line, errstr); 143acce952bSliubo save_error_info(fs_info); 144acce952bSliubo 145acce952bSliubo btrfs_handle_error(fs_info); 146acce952bSliubo } 147acce952bSliubo 148e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 149e20d96d6SChris Mason { 150e20d96d6SChris Mason struct btrfs_root *root = btrfs_sb(sb); 151e20d96d6SChris Mason int ret; 152e20d96d6SChris Mason 153e20d96d6SChris Mason ret = close_ctree(root); 154e20d96d6SChris Mason sb->s_fs_info = NULL; 155559af821SAndi Kleen 156559af821SAndi Kleen (void)ret; /* FIXME: need to fix VFS to return error? */ 157e20d96d6SChris Mason } 1582e635a27SChris Mason 15995e05289SChris Mason enum { 16073f73415SJosef Bacik Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum, 161287a0ab9SJosef Bacik Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, 162287a0ab9SJosef Bacik Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress, 163261507a0SLi Zefan Opt_compress_type, Opt_compress_force, Opt_compress_force_type, 164261507a0SLi Zefan Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard, 16591435650SChris Mason Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed, 1664b9465cbSChris Mason Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, 167*73bc1876SJosef Bacik Opt_inode_cache, Opt_no_space_cache, Opt_err, 16895e05289SChris Mason }; 16995e05289SChris Mason 17095e05289SChris Mason static match_table_t tokens = { 171dfe25020SChris Mason {Opt_degraded, "degraded"}, 17295e05289SChris Mason {Opt_subvol, "subvol=%s"}, 17373f73415SJosef Bacik {Opt_subvolid, "subvolid=%d"}, 17443e570b0SChristoph Hellwig {Opt_device, "device=%s"}, 175b6cda9bcSChris Mason {Opt_nodatasum, "nodatasum"}, 176be20aa9dSChris Mason {Opt_nodatacow, "nodatacow"}, 17721ad10cfSChris Mason {Opt_nobarrier, "nobarrier"}, 1786f568d35SChris Mason {Opt_max_inline, "max_inline=%s"}, 1798f662a76SChris Mason {Opt_alloc_start, "alloc_start=%s"}, 1804543df7eSChris Mason {Opt_thread_pool, "thread_pool=%d"}, 181c8b97818SChris Mason {Opt_compress, "compress"}, 182261507a0SLi Zefan {Opt_compress_type, "compress=%s"}, 183a555f810SChris Mason {Opt_compress_force, "compress-force"}, 184261507a0SLi Zefan {Opt_compress_force_type, "compress-force=%s"}, 185e18e4809SChris Mason {Opt_ssd, "ssd"}, 186451d7585SChris Mason {Opt_ssd_spread, "ssd_spread"}, 1873b30c22fSChris Mason {Opt_nossd, "nossd"}, 18833268eafSJosef Bacik {Opt_noacl, "noacl"}, 1893a5e1404SSage Weil {Opt_notreelog, "notreelog"}, 190dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 19197e728d4SJosef Bacik {Opt_ratio, "metadata_ratio=%d"}, 192e244a0aeSChristoph Hellwig {Opt_discard, "discard"}, 1930af3d00bSJosef Bacik {Opt_space_cache, "space_cache"}, 19488c2ba3bSJosef Bacik {Opt_clear_cache, "clear_cache"}, 1954260f7c7SSage Weil {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, 19691435650SChris Mason {Opt_enospc_debug, "enospc_debug"}, 197e15d0542SXin Zhong {Opt_subvolrootid, "subvolrootid=%d"}, 1984cb5300bSChris Mason {Opt_defrag, "autodefrag"}, 1994b9465cbSChris Mason {Opt_inode_cache, "inode_cache"}, 200*73bc1876SJosef Bacik {Opt_no_space_cache, "no_space_cache"}, 20133268eafSJosef Bacik {Opt_err, NULL}, 20295e05289SChris Mason }; 20395e05289SChris Mason 204edf24abeSChristoph Hellwig /* 205edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 206edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 207edf24abeSChristoph Hellwig */ 208edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options) 20995e05289SChris Mason { 210edf24abeSChristoph Hellwig struct btrfs_fs_info *info = root->fs_info; 21195e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 212*73bc1876SJosef Bacik char *p, *num, *orig = NULL; 213*73bc1876SJosef Bacik u64 cache_gen; 2144543df7eSChris Mason int intarg; 215a7a3f7caSSage Weil int ret = 0; 216261507a0SLi Zefan char *compress_type; 217261507a0SLi Zefan bool compress_force = false; 218b6cda9bcSChris Mason 219*73bc1876SJosef Bacik cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy); 220*73bc1876SJosef Bacik if (cache_gen) 221*73bc1876SJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 222*73bc1876SJosef Bacik 22395e05289SChris Mason if (!options) 224*73bc1876SJosef Bacik goto out; 22595e05289SChris Mason 226be20aa9dSChris Mason /* 227be20aa9dSChris Mason * strsep changes the string, duplicate it because parse_options 228be20aa9dSChris Mason * gets called twice 229be20aa9dSChris Mason */ 230be20aa9dSChris Mason options = kstrdup(options, GFP_NOFS); 231be20aa9dSChris Mason if (!options) 232be20aa9dSChris Mason return -ENOMEM; 233be20aa9dSChris Mason 234da495eccSJosef Bacik orig = options; 235be20aa9dSChris Mason 23695e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 23795e05289SChris Mason int token; 23895e05289SChris Mason if (!*p) 23995e05289SChris Mason continue; 24095e05289SChris Mason 24195e05289SChris Mason token = match_token(p, tokens, args); 24295e05289SChris Mason switch (token) { 243dfe25020SChris Mason case Opt_degraded: 244edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: allowing degraded mounts\n"); 245dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 246dfe25020SChris Mason break; 24795e05289SChris Mason case Opt_subvol: 24873f73415SJosef Bacik case Opt_subvolid: 249e15d0542SXin Zhong case Opt_subvolrootid: 25043e570b0SChristoph Hellwig case Opt_device: 251edf24abeSChristoph Hellwig /* 25243e570b0SChristoph Hellwig * These are parsed by btrfs_parse_early_options 253edf24abeSChristoph Hellwig * and can be happily ignored here. 254edf24abeSChristoph Hellwig */ 25595e05289SChris Mason break; 256b6cda9bcSChris Mason case Opt_nodatasum: 257067c28adSChris Mason printk(KERN_INFO "btrfs: setting nodatasum\n"); 258b6cda9bcSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 259be20aa9dSChris Mason break; 260be20aa9dSChris Mason case Opt_nodatacow: 261edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: setting nodatacow\n"); 262be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 263be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 264b6cda9bcSChris Mason break; 265a555f810SChris Mason case Opt_compress_force: 266261507a0SLi Zefan case Opt_compress_force_type: 267261507a0SLi Zefan compress_force = true; 268261507a0SLi Zefan case Opt_compress: 269261507a0SLi Zefan case Opt_compress_type: 270261507a0SLi Zefan if (token == Opt_compress || 271261507a0SLi Zefan token == Opt_compress_force || 272261507a0SLi Zefan strcmp(args[0].from, "zlib") == 0) { 273261507a0SLi Zefan compress_type = "zlib"; 274261507a0SLi Zefan info->compress_type = BTRFS_COMPRESS_ZLIB; 275a6fa6faeSLi Zefan } else if (strcmp(args[0].from, "lzo") == 0) { 276a6fa6faeSLi Zefan compress_type = "lzo"; 277a6fa6faeSLi Zefan info->compress_type = BTRFS_COMPRESS_LZO; 278261507a0SLi Zefan } else { 279261507a0SLi Zefan ret = -EINVAL; 280261507a0SLi Zefan goto out; 281261507a0SLi Zefan } 282261507a0SLi Zefan 283a555f810SChris Mason btrfs_set_opt(info->mount_opt, COMPRESS); 284261507a0SLi Zefan if (compress_force) { 285261507a0SLi Zefan btrfs_set_opt(info->mount_opt, FORCE_COMPRESS); 286261507a0SLi Zefan pr_info("btrfs: force %s compression\n", 287261507a0SLi Zefan compress_type); 288261507a0SLi Zefan } else 289261507a0SLi Zefan pr_info("btrfs: use %s compression\n", 290261507a0SLi Zefan compress_type); 291a555f810SChris Mason break; 292e18e4809SChris Mason case Opt_ssd: 293edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: use ssd allocation scheme\n"); 294e18e4809SChris Mason btrfs_set_opt(info->mount_opt, SSD); 295e18e4809SChris Mason break; 296451d7585SChris Mason case Opt_ssd_spread: 297451d7585SChris Mason printk(KERN_INFO "btrfs: use spread ssd " 298451d7585SChris Mason "allocation scheme\n"); 299451d7585SChris Mason btrfs_set_opt(info->mount_opt, SSD); 300451d7585SChris Mason btrfs_set_opt(info->mount_opt, SSD_SPREAD); 301451d7585SChris Mason break; 3023b30c22fSChris Mason case Opt_nossd: 303451d7585SChris Mason printk(KERN_INFO "btrfs: not using ssd allocation " 304451d7585SChris Mason "scheme\n"); 305c289811cSChris Mason btrfs_set_opt(info->mount_opt, NOSSD); 3063b30c22fSChris Mason btrfs_clear_opt(info->mount_opt, SSD); 307451d7585SChris Mason btrfs_clear_opt(info->mount_opt, SSD_SPREAD); 3083b30c22fSChris Mason break; 30921ad10cfSChris Mason case Opt_nobarrier: 310edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: turning off barriers\n"); 31121ad10cfSChris Mason btrfs_set_opt(info->mount_opt, NOBARRIER); 31221ad10cfSChris Mason break; 3134543df7eSChris Mason case Opt_thread_pool: 3144543df7eSChris Mason intarg = 0; 3154543df7eSChris Mason match_int(&args[0], &intarg); 3164543df7eSChris Mason if (intarg) { 3174543df7eSChris Mason info->thread_pool_size = intarg; 3184543df7eSChris Mason printk(KERN_INFO "btrfs: thread pool %d\n", 3194543df7eSChris Mason info->thread_pool_size); 3204543df7eSChris Mason } 3214543df7eSChris Mason break; 3226f568d35SChris Mason case Opt_max_inline: 323edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 3246f568d35SChris Mason if (num) { 32591748467SAkinobu Mita info->max_inline = memparse(num, NULL); 3266f568d35SChris Mason kfree(num); 3276f568d35SChris Mason 32815ada040SChris Mason if (info->max_inline) { 3296f568d35SChris Mason info->max_inline = max_t(u64, 33015ada040SChris Mason info->max_inline, 33115ada040SChris Mason root->sectorsize); 33215ada040SChris Mason } 333edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: max_inline at %llu\n", 33421380931SJoel Becker (unsigned long long)info->max_inline); 3356f568d35SChris Mason } 3366f568d35SChris Mason break; 3378f662a76SChris Mason case Opt_alloc_start: 338edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 3398f662a76SChris Mason if (num) { 34091748467SAkinobu Mita info->alloc_start = memparse(num, NULL); 3418f662a76SChris Mason kfree(num); 342edf24abeSChristoph Hellwig printk(KERN_INFO 343edf24abeSChristoph Hellwig "btrfs: allocations start at %llu\n", 34421380931SJoel Becker (unsigned long long)info->alloc_start); 3458f662a76SChris Mason } 3468f662a76SChris Mason break; 34733268eafSJosef Bacik case Opt_noacl: 34833268eafSJosef Bacik root->fs_info->sb->s_flags &= ~MS_POSIXACL; 34933268eafSJosef Bacik break; 3503a5e1404SSage Weil case Opt_notreelog: 3513a5e1404SSage Weil printk(KERN_INFO "btrfs: disabling tree log\n"); 3523a5e1404SSage Weil btrfs_set_opt(info->mount_opt, NOTREELOG); 3533a5e1404SSage Weil break; 354dccae999SSage Weil case Opt_flushoncommit: 355dccae999SSage Weil printk(KERN_INFO "btrfs: turning on flush-on-commit\n"); 356dccae999SSage Weil btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT); 357dccae999SSage Weil break; 35897e728d4SJosef Bacik case Opt_ratio: 35997e728d4SJosef Bacik intarg = 0; 36097e728d4SJosef Bacik match_int(&args[0], &intarg); 36197e728d4SJosef Bacik if (intarg) { 36297e728d4SJosef Bacik info->metadata_ratio = intarg; 36397e728d4SJosef Bacik printk(KERN_INFO "btrfs: metadata ratio %d\n", 36497e728d4SJosef Bacik info->metadata_ratio); 36597e728d4SJosef Bacik } 36697e728d4SJosef Bacik break; 367e244a0aeSChristoph Hellwig case Opt_discard: 368e244a0aeSChristoph Hellwig btrfs_set_opt(info->mount_opt, DISCARD); 369e244a0aeSChristoph Hellwig break; 3700af3d00bSJosef Bacik case Opt_space_cache: 3710af3d00bSJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 3720de90876SJosef Bacik break; 373*73bc1876SJosef Bacik case Opt_no_space_cache: 374*73bc1876SJosef Bacik printk(KERN_INFO "btrfs: disabling disk space caching\n"); 375*73bc1876SJosef Bacik btrfs_clear_opt(info->mount_opt, SPACE_CACHE); 376*73bc1876SJosef Bacik break; 3774b9465cbSChris Mason case Opt_inode_cache: 3784b9465cbSChris Mason printk(KERN_INFO "btrfs: enabling inode map caching\n"); 3794b9465cbSChris Mason btrfs_set_opt(info->mount_opt, INODE_MAP_CACHE); 3804b9465cbSChris Mason break; 38188c2ba3bSJosef Bacik case Opt_clear_cache: 38288c2ba3bSJosef Bacik printk(KERN_INFO "btrfs: force clearing of disk cache\n"); 38388c2ba3bSJosef Bacik btrfs_set_opt(info->mount_opt, CLEAR_CACHE); 3840af3d00bSJosef Bacik break; 3854260f7c7SSage Weil case Opt_user_subvol_rm_allowed: 3864260f7c7SSage Weil btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 3874260f7c7SSage Weil break; 38891435650SChris Mason case Opt_enospc_debug: 38991435650SChris Mason btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 39091435650SChris Mason break; 3914cb5300bSChris Mason case Opt_defrag: 3924cb5300bSChris Mason printk(KERN_INFO "btrfs: enabling auto defrag"); 3934cb5300bSChris Mason btrfs_set_opt(info->mount_opt, AUTO_DEFRAG); 3944cb5300bSChris Mason break; 395a7a3f7caSSage Weil case Opt_err: 396a7a3f7caSSage Weil printk(KERN_INFO "btrfs: unrecognized mount option " 397a7a3f7caSSage Weil "'%s'\n", p); 398a7a3f7caSSage Weil ret = -EINVAL; 399a7a3f7caSSage Weil goto out; 40095e05289SChris Mason default: 401be20aa9dSChris Mason break; 40295e05289SChris Mason } 40395e05289SChris Mason } 404a7a3f7caSSage Weil out: 405*73bc1876SJosef Bacik if (!ret && btrfs_test_opt(root, SPACE_CACHE)) 406*73bc1876SJosef Bacik printk(KERN_INFO "btrfs: disk space caching is enabled\n"); 407da495eccSJosef Bacik kfree(orig); 408a7a3f7caSSage Weil return ret; 409edf24abeSChristoph Hellwig } 410edf24abeSChristoph Hellwig 411edf24abeSChristoph Hellwig /* 412edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 413edf24abeSChristoph Hellwig * 414edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 415edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 416edf24abeSChristoph Hellwig */ 41797288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 41873f73415SJosef Bacik void *holder, char **subvol_name, u64 *subvol_objectid, 419e15d0542SXin Zhong u64 *subvol_rootid, struct btrfs_fs_devices **fs_devices) 420edf24abeSChristoph Hellwig { 421edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 4223f3d0bc0STero Roponen char *opts, *orig, *p; 423edf24abeSChristoph Hellwig int error = 0; 42473f73415SJosef Bacik int intarg; 425edf24abeSChristoph Hellwig 426edf24abeSChristoph Hellwig if (!options) 427830c4adbSJosef Bacik return 0; 428edf24abeSChristoph Hellwig 429edf24abeSChristoph Hellwig /* 430edf24abeSChristoph Hellwig * strsep changes the string, duplicate it because parse_options 431edf24abeSChristoph Hellwig * gets called twice 432edf24abeSChristoph Hellwig */ 433edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 434edf24abeSChristoph Hellwig if (!opts) 435edf24abeSChristoph Hellwig return -ENOMEM; 4363f3d0bc0STero Roponen orig = opts; 437edf24abeSChristoph Hellwig 438edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 439edf24abeSChristoph Hellwig int token; 440edf24abeSChristoph Hellwig if (!*p) 441edf24abeSChristoph Hellwig continue; 442edf24abeSChristoph Hellwig 443edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 444edf24abeSChristoph Hellwig switch (token) { 445edf24abeSChristoph Hellwig case Opt_subvol: 446edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 447edf24abeSChristoph Hellwig break; 44873f73415SJosef Bacik case Opt_subvolid: 44973f73415SJosef Bacik intarg = 0; 4504849f01dSJosef Bacik error = match_int(&args[0], &intarg); 4514849f01dSJosef Bacik if (!error) { 4524849f01dSJosef Bacik /* we want the original fs_tree */ 4534849f01dSJosef Bacik if (!intarg) 4544849f01dSJosef Bacik *subvol_objectid = 4554849f01dSJosef Bacik BTRFS_FS_TREE_OBJECTID; 4564849f01dSJosef Bacik else 45773f73415SJosef Bacik *subvol_objectid = intarg; 4584849f01dSJosef Bacik } 45973f73415SJosef Bacik break; 460e15d0542SXin Zhong case Opt_subvolrootid: 461e15d0542SXin Zhong intarg = 0; 462e15d0542SXin Zhong error = match_int(&args[0], &intarg); 463e15d0542SXin Zhong if (!error) { 464e15d0542SXin Zhong /* we want the original fs_tree */ 465e15d0542SXin Zhong if (!intarg) 466e15d0542SXin Zhong *subvol_rootid = 467e15d0542SXin Zhong BTRFS_FS_TREE_OBJECTID; 468e15d0542SXin Zhong else 469e15d0542SXin Zhong *subvol_rootid = intarg; 470e15d0542SXin Zhong } 471e15d0542SXin Zhong break; 47243e570b0SChristoph Hellwig case Opt_device: 47343e570b0SChristoph Hellwig error = btrfs_scan_one_device(match_strdup(&args[0]), 47443e570b0SChristoph Hellwig flags, holder, fs_devices); 47543e570b0SChristoph Hellwig if (error) 476830c4adbSJosef Bacik goto out; 47743e570b0SChristoph Hellwig break; 478edf24abeSChristoph Hellwig default: 479edf24abeSChristoph Hellwig break; 480edf24abeSChristoph Hellwig } 481edf24abeSChristoph Hellwig } 482edf24abeSChristoph Hellwig 483edf24abeSChristoph Hellwig out: 484830c4adbSJosef Bacik kfree(orig); 485edf24abeSChristoph Hellwig return error; 48695e05289SChris Mason } 48795e05289SChris Mason 48873f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb, 48973f73415SJosef Bacik u64 subvol_objectid) 49073f73415SJosef Bacik { 49173f73415SJosef Bacik struct btrfs_root *root = sb->s_fs_info; 49273f73415SJosef Bacik struct btrfs_root *new_root; 49373f73415SJosef Bacik struct btrfs_dir_item *di; 49473f73415SJosef Bacik struct btrfs_path *path; 49573f73415SJosef Bacik struct btrfs_key location; 49673f73415SJosef Bacik struct inode *inode; 49773f73415SJosef Bacik u64 dir_id; 49873f73415SJosef Bacik int new = 0; 49973f73415SJosef Bacik 50073f73415SJosef Bacik /* 50173f73415SJosef Bacik * We have a specific subvol we want to mount, just setup location and 50273f73415SJosef Bacik * go look up the root. 50373f73415SJosef Bacik */ 50473f73415SJosef Bacik if (subvol_objectid) { 50573f73415SJosef Bacik location.objectid = subvol_objectid; 50673f73415SJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 50773f73415SJosef Bacik location.offset = (u64)-1; 50873f73415SJosef Bacik goto find_root; 50973f73415SJosef Bacik } 51073f73415SJosef Bacik 51173f73415SJosef Bacik path = btrfs_alloc_path(); 51273f73415SJosef Bacik if (!path) 51373f73415SJosef Bacik return ERR_PTR(-ENOMEM); 51473f73415SJosef Bacik path->leave_spinning = 1; 51573f73415SJosef Bacik 51673f73415SJosef Bacik /* 51773f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 51873f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 51973f73415SJosef Bacik * to mount. 52073f73415SJosef Bacik */ 52173f73415SJosef Bacik dir_id = btrfs_super_root_dir(&root->fs_info->super_copy); 52273f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 523b0839166SJulia Lawall if (IS_ERR(di)) { 524b0839166SJulia Lawall btrfs_free_path(path); 525fb4f6f91SDan Carpenter return ERR_CAST(di); 526b0839166SJulia Lawall } 52773f73415SJosef Bacik if (!di) { 52873f73415SJosef Bacik /* 52973f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 53073f73415SJosef Bacik * it's always been there, but don't freak out, just try and 53173f73415SJosef Bacik * mount to root most subvolume. 53273f73415SJosef Bacik */ 53373f73415SJosef Bacik btrfs_free_path(path); 53473f73415SJosef Bacik dir_id = BTRFS_FIRST_FREE_OBJECTID; 53573f73415SJosef Bacik new_root = root->fs_info->fs_root; 53673f73415SJosef Bacik goto setup_root; 53773f73415SJosef Bacik } 53873f73415SJosef Bacik 53973f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 54073f73415SJosef Bacik btrfs_free_path(path); 54173f73415SJosef Bacik 54273f73415SJosef Bacik find_root: 54373f73415SJosef Bacik new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); 54473f73415SJosef Bacik if (IS_ERR(new_root)) 545d0b678cbSJulia Lawall return ERR_CAST(new_root); 54673f73415SJosef Bacik 54773f73415SJosef Bacik if (btrfs_root_refs(&new_root->root_item) == 0) 54873f73415SJosef Bacik return ERR_PTR(-ENOENT); 54973f73415SJosef Bacik 55073f73415SJosef Bacik dir_id = btrfs_root_dirid(&new_root->root_item); 55173f73415SJosef Bacik setup_root: 55273f73415SJosef Bacik location.objectid = dir_id; 55373f73415SJosef Bacik location.type = BTRFS_INODE_ITEM_KEY; 55473f73415SJosef Bacik location.offset = 0; 55573f73415SJosef Bacik 55673f73415SJosef Bacik inode = btrfs_iget(sb, &location, new_root, &new); 5574cbd1149SDan Carpenter if (IS_ERR(inode)) 5584cbd1149SDan Carpenter return ERR_CAST(inode); 55973f73415SJosef Bacik 56073f73415SJosef Bacik /* 56173f73415SJosef Bacik * If we're just mounting the root most subvol put the inode and return 56273f73415SJosef Bacik * a reference to the dentry. We will have already gotten a reference 56373f73415SJosef Bacik * to the inode in btrfs_fill_super so we're good to go. 56473f73415SJosef Bacik */ 56573f73415SJosef Bacik if (!new && sb->s_root->d_inode == inode) { 56673f73415SJosef Bacik iput(inode); 56773f73415SJosef Bacik return dget(sb->s_root); 56873f73415SJosef Bacik } 56973f73415SJosef Bacik 570ba5b8958SJosef Bacik return d_obtain_alias(inode); 57173f73415SJosef Bacik } 57273f73415SJosef Bacik 5738a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 5748a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 5758a4b83ccSChris Mason void *data, int silent) 5762e635a27SChris Mason { 5772e635a27SChris Mason struct inode *inode; 578e20d96d6SChris Mason struct dentry *root_dentry; 5790f7d52f4SChris Mason struct btrfs_root *tree_root; 5805d4f98a2SYan Zheng struct btrfs_key key; 58139279cc3SChris Mason int err; 5822e635a27SChris Mason 5832e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 5842e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 585e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 586af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 587be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 5885103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 5892e635a27SChris Mason sb->s_time_gran = 1; 5900eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 59133268eafSJosef Bacik sb->s_flags |= MS_POSIXACL; 59249cf6f45SChris Ball #endif 593e20d96d6SChris Mason 594dfe25020SChris Mason tree_root = open_ctree(sb, fs_devices, (char *)data); 595d98237b3SChris Mason 596e58ca020SYan if (IS_ERR(tree_root)) { 597e20d96d6SChris Mason printk("btrfs: open_ctree failed\n"); 598e58ca020SYan return PTR_ERR(tree_root); 599e20d96d6SChris Mason } 6000f7d52f4SChris Mason sb->s_fs_info = tree_root; 601b888db2bSChris Mason 6025d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 6035d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 6045d4f98a2SYan Zheng key.offset = 0; 60573f73415SJosef Bacik inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL); 6065d4f98a2SYan Zheng if (IS_ERR(inode)) { 6075d4f98a2SYan Zheng err = PTR_ERR(inode); 60839279cc3SChris Mason goto fail_close; 60939279cc3SChris Mason } 6102e635a27SChris Mason 611e20d96d6SChris Mason root_dentry = d_alloc_root(inode); 612e20d96d6SChris Mason if (!root_dentry) { 6132e635a27SChris Mason iput(inode); 61439279cc3SChris Mason err = -ENOMEM; 61539279cc3SChris Mason goto fail_close; 6162e635a27SChris Mason } 61758176a96SJosef Bacik 618e20d96d6SChris Mason sb->s_root = root_dentry; 6196885f308SChris Mason 6206885f308SChris Mason save_mount_options(sb, data); 62190a887c9SDan Magenheimer cleancache_init_fs(sb); 6222e635a27SChris Mason return 0; 6232e635a27SChris Mason 62439279cc3SChris Mason fail_close: 62539279cc3SChris Mason close_ctree(tree_root); 626d5719762SChris Mason return err; 627d5719762SChris Mason } 628d5719762SChris Mason 6296bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 630d5719762SChris Mason { 631d5719762SChris Mason struct btrfs_trans_handle *trans; 632dccae999SSage Weil struct btrfs_root *root = btrfs_sb(sb); 633d5719762SChris Mason int ret; 634df2ce34cSChris Mason 6351abe9b8aSliubo trace_btrfs_sync_fs(wait); 6361abe9b8aSliubo 637d561c025SChris Mason if (!wait) { 6387cfcc17eSChris Mason filemap_flush(root->fs_info->btree_inode->i_mapping); 639df2ce34cSChris Mason return 0; 640d561c025SChris Mason } 641771ed689SChris Mason 64224bbcf04SYan, Zheng btrfs_start_delalloc_inodes(root, 0); 64324bbcf04SYan, Zheng btrfs_wait_ordered_extents(root, 0, 0); 644771ed689SChris Mason 645a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 64698d5dc13STsutomu Itoh if (IS_ERR(trans)) 64798d5dc13STsutomu Itoh return PTR_ERR(trans); 648d5719762SChris Mason ret = btrfs_commit_transaction(trans, root); 64954aa1f4dSChris Mason return ret; 650d5719762SChris Mason } 651d5719762SChris Mason 652a9572a15SEric Paris static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs) 653a9572a15SEric Paris { 654a9572a15SEric Paris struct btrfs_root *root = btrfs_sb(vfs->mnt_sb); 655a9572a15SEric Paris struct btrfs_fs_info *info = root->fs_info; 656200da64eSTsutomu Itoh char *compress_type; 657a9572a15SEric Paris 658a9572a15SEric Paris if (btrfs_test_opt(root, DEGRADED)) 659a9572a15SEric Paris seq_puts(seq, ",degraded"); 660a9572a15SEric Paris if (btrfs_test_opt(root, NODATASUM)) 661a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 662a9572a15SEric Paris if (btrfs_test_opt(root, NODATACOW)) 663a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 664a9572a15SEric Paris if (btrfs_test_opt(root, NOBARRIER)) 665a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 666a9572a15SEric Paris if (info->max_inline != 8192 * 1024) 66721380931SJoel Becker seq_printf(seq, ",max_inline=%llu", 66821380931SJoel Becker (unsigned long long)info->max_inline); 669a9572a15SEric Paris if (info->alloc_start != 0) 67021380931SJoel Becker seq_printf(seq, ",alloc_start=%llu", 67121380931SJoel Becker (unsigned long long)info->alloc_start); 672a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 673a9572a15SEric Paris num_online_cpus() + 2, 8)) 674a9572a15SEric Paris seq_printf(seq, ",thread_pool=%d", info->thread_pool_size); 675200da64eSTsutomu Itoh if (btrfs_test_opt(root, COMPRESS)) { 676200da64eSTsutomu Itoh if (info->compress_type == BTRFS_COMPRESS_ZLIB) 677200da64eSTsutomu Itoh compress_type = "zlib"; 678200da64eSTsutomu Itoh else 679200da64eSTsutomu Itoh compress_type = "lzo"; 680200da64eSTsutomu Itoh if (btrfs_test_opt(root, FORCE_COMPRESS)) 681200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 682200da64eSTsutomu Itoh else 683200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 684200da64eSTsutomu Itoh } 685c289811cSChris Mason if (btrfs_test_opt(root, NOSSD)) 686c289811cSChris Mason seq_puts(seq, ",nossd"); 687451d7585SChris Mason if (btrfs_test_opt(root, SSD_SPREAD)) 688451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 689451d7585SChris Mason else if (btrfs_test_opt(root, SSD)) 690a9572a15SEric Paris seq_puts(seq, ",ssd"); 6913a5e1404SSage Weil if (btrfs_test_opt(root, NOTREELOG)) 6926b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 693dccae999SSage Weil if (btrfs_test_opt(root, FLUSHONCOMMIT)) 6946b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 69520a5239aSMatthew Wilcox if (btrfs_test_opt(root, DISCARD)) 69620a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 697a9572a15SEric Paris if (!(root->fs_info->sb->s_flags & MS_POSIXACL)) 698a9572a15SEric Paris seq_puts(seq, ",noacl"); 699200da64eSTsutomu Itoh if (btrfs_test_opt(root, SPACE_CACHE)) 700200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 701*73bc1876SJosef Bacik else 702*73bc1876SJosef Bacik seq_puts(seq, ",no_space_cache"); 703200da64eSTsutomu Itoh if (btrfs_test_opt(root, CLEAR_CACHE)) 704200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 705200da64eSTsutomu Itoh if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 706200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 7070942caa3SDavid Sterba if (btrfs_test_opt(root, ENOSPC_DEBUG)) 7080942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 7090942caa3SDavid Sterba if (btrfs_test_opt(root, AUTO_DEFRAG)) 7100942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 7110942caa3SDavid Sterba if (btrfs_test_opt(root, INODE_MAP_CACHE)) 7120942caa3SDavid Sterba seq_puts(seq, ",inode_cache"); 713a9572a15SEric Paris return 0; 714a9572a15SEric Paris } 715a9572a15SEric Paris 716a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 7172e635a27SChris Mason { 718450ba0eaSJosef Bacik struct btrfs_root *test_root = data; 719a061fc8dSChris Mason struct btrfs_root *root = btrfs_sb(s); 7204b82d6e4SYan 721619c8c76SIan Kent /* 722619c8c76SIan Kent * If this super block is going away, return false as it 723619c8c76SIan Kent * can't match as an existing super block. 724619c8c76SIan Kent */ 725619c8c76SIan Kent if (!atomic_read(&s->s_active)) 726619c8c76SIan Kent return 0; 727450ba0eaSJosef Bacik return root->fs_info->fs_devices == test_root->fs_info->fs_devices; 7284b82d6e4SYan } 7294b82d6e4SYan 730450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 731450ba0eaSJosef Bacik { 732450ba0eaSJosef Bacik s->s_fs_info = data; 733450ba0eaSJosef Bacik 734450ba0eaSJosef Bacik return set_anon_super(s, data); 735450ba0eaSJosef Bacik } 736450ba0eaSJosef Bacik 737830c4adbSJosef Bacik /* 738830c4adbSJosef Bacik * This will strip out the subvol=%s argument for an argument string and add 739830c4adbSJosef Bacik * subvolid=0 to make sure we get the actual tree root for path walking to the 740830c4adbSJosef Bacik * subvol we want. 741830c4adbSJosef Bacik */ 742830c4adbSJosef Bacik static char *setup_root_args(char *args) 743830c4adbSJosef Bacik { 744830c4adbSJosef Bacik unsigned copied = 0; 745830c4adbSJosef Bacik unsigned len = strlen(args) + 2; 746830c4adbSJosef Bacik char *pos; 747830c4adbSJosef Bacik char *ret; 748830c4adbSJosef Bacik 749830c4adbSJosef Bacik /* 750830c4adbSJosef Bacik * We need the same args as before, but minus 751830c4adbSJosef Bacik * 752830c4adbSJosef Bacik * subvol=a 753830c4adbSJosef Bacik * 754830c4adbSJosef Bacik * and add 755830c4adbSJosef Bacik * 756830c4adbSJosef Bacik * subvolid=0 757830c4adbSJosef Bacik * 758830c4adbSJosef Bacik * which is a difference of 2 characters, so we allocate strlen(args) + 759830c4adbSJosef Bacik * 2 characters. 760830c4adbSJosef Bacik */ 761830c4adbSJosef Bacik ret = kzalloc(len * sizeof(char), GFP_NOFS); 762830c4adbSJosef Bacik if (!ret) 763830c4adbSJosef Bacik return NULL; 764830c4adbSJosef Bacik pos = strstr(args, "subvol="); 765830c4adbSJosef Bacik 766830c4adbSJosef Bacik /* This shouldn't happen, but just in case.. */ 767830c4adbSJosef Bacik if (!pos) { 768830c4adbSJosef Bacik kfree(ret); 769830c4adbSJosef Bacik return NULL; 770830c4adbSJosef Bacik } 771830c4adbSJosef Bacik 772830c4adbSJosef Bacik /* 773830c4adbSJosef Bacik * The subvol=<> arg is not at the front of the string, copy everybody 774830c4adbSJosef Bacik * up to that into ret. 775830c4adbSJosef Bacik */ 776830c4adbSJosef Bacik if (pos != args) { 777830c4adbSJosef Bacik *pos = '\0'; 778830c4adbSJosef Bacik strcpy(ret, args); 779830c4adbSJosef Bacik copied += strlen(args); 780830c4adbSJosef Bacik pos++; 781830c4adbSJosef Bacik } 782830c4adbSJosef Bacik 783830c4adbSJosef Bacik strncpy(ret + copied, "subvolid=0", len - copied); 784830c4adbSJosef Bacik 785830c4adbSJosef Bacik /* Length of subvolid=0 */ 786830c4adbSJosef Bacik copied += 10; 787830c4adbSJosef Bacik 788830c4adbSJosef Bacik /* 789830c4adbSJosef Bacik * If there is no , after the subvol= option then we know there's no 790830c4adbSJosef Bacik * other options and we can just return. 791830c4adbSJosef Bacik */ 792830c4adbSJosef Bacik pos = strchr(pos, ','); 793830c4adbSJosef Bacik if (!pos) 794830c4adbSJosef Bacik return ret; 795830c4adbSJosef Bacik 796830c4adbSJosef Bacik /* Copy the rest of the arguments into our buffer */ 797830c4adbSJosef Bacik strncpy(ret + copied, pos, len - copied); 798830c4adbSJosef Bacik copied += strlen(pos); 799830c4adbSJosef Bacik 800830c4adbSJosef Bacik return ret; 801830c4adbSJosef Bacik } 802830c4adbSJosef Bacik 803830c4adbSJosef Bacik static struct dentry *mount_subvol(const char *subvol_name, int flags, 804830c4adbSJosef Bacik const char *device_name, char *data) 805830c4adbSJosef Bacik { 806830c4adbSJosef Bacik struct super_block *s; 807830c4adbSJosef Bacik struct dentry *root; 808830c4adbSJosef Bacik struct vfsmount *mnt; 809830c4adbSJosef Bacik struct mnt_namespace *ns_private; 810830c4adbSJosef Bacik char *newargs; 811830c4adbSJosef Bacik struct path path; 812830c4adbSJosef Bacik int error; 813830c4adbSJosef Bacik 814830c4adbSJosef Bacik newargs = setup_root_args(data); 815830c4adbSJosef Bacik if (!newargs) 816830c4adbSJosef Bacik return ERR_PTR(-ENOMEM); 817830c4adbSJosef Bacik mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, 818830c4adbSJosef Bacik newargs); 819830c4adbSJosef Bacik kfree(newargs); 820830c4adbSJosef Bacik if (IS_ERR(mnt)) 821830c4adbSJosef Bacik return ERR_CAST(mnt); 822830c4adbSJosef Bacik 823830c4adbSJosef Bacik ns_private = create_mnt_ns(mnt); 824830c4adbSJosef Bacik if (IS_ERR(ns_private)) { 825830c4adbSJosef Bacik mntput(mnt); 826830c4adbSJosef Bacik return ERR_CAST(ns_private); 827830c4adbSJosef Bacik } 828830c4adbSJosef Bacik 829830c4adbSJosef Bacik /* 830830c4adbSJosef Bacik * This will trigger the automount of the subvol so we can just 831830c4adbSJosef Bacik * drop the mnt we have here and return the dentry that we 832830c4adbSJosef Bacik * found. 833830c4adbSJosef Bacik */ 834830c4adbSJosef Bacik error = vfs_path_lookup(mnt->mnt_root, mnt, subvol_name, 835830c4adbSJosef Bacik LOOKUP_FOLLOW, &path); 836830c4adbSJosef Bacik put_mnt_ns(ns_private); 837830c4adbSJosef Bacik if (error) 838830c4adbSJosef Bacik return ERR_PTR(error); 839830c4adbSJosef Bacik 840830c4adbSJosef Bacik /* Get a ref to the sb and the dentry we found and return it */ 841830c4adbSJosef Bacik s = path.mnt->mnt_sb; 842830c4adbSJosef Bacik atomic_inc(&s->s_active); 843830c4adbSJosef Bacik root = dget(path.dentry); 844830c4adbSJosef Bacik path_put(&path); 845830c4adbSJosef Bacik down_write(&s->s_umount); 846830c4adbSJosef Bacik 847830c4adbSJosef Bacik return root; 848830c4adbSJosef Bacik } 849450ba0eaSJosef Bacik 850edf24abeSChristoph Hellwig /* 851edf24abeSChristoph Hellwig * Find a superblock for the given device / mount point. 852edf24abeSChristoph Hellwig * 853edf24abeSChristoph Hellwig * Note: This is based on get_sb_bdev from fs/super.c with a few additions 854edf24abeSChristoph Hellwig * for multiple device setup. Make sure to keep it in sync. 855edf24abeSChristoph Hellwig */ 856061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 857306e16ceSDavid Sterba const char *device_name, void *data) 8584b82d6e4SYan { 8594b82d6e4SYan struct block_device *bdev = NULL; 8604b82d6e4SYan struct super_block *s; 8614b82d6e4SYan struct dentry *root; 8628a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices = NULL; 863450ba0eaSJosef Bacik struct btrfs_root *tree_root = NULL; 864450ba0eaSJosef Bacik struct btrfs_fs_info *fs_info = NULL; 86597288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 86673f73415SJosef Bacik char *subvol_name = NULL; 86773f73415SJosef Bacik u64 subvol_objectid = 0; 868e15d0542SXin Zhong u64 subvol_rootid = 0; 8694b82d6e4SYan int error = 0; 8704b82d6e4SYan 87197288f2cSChristoph Hellwig if (!(flags & MS_RDONLY)) 87297288f2cSChristoph Hellwig mode |= FMODE_WRITE; 87397288f2cSChristoph Hellwig 87497288f2cSChristoph Hellwig error = btrfs_parse_early_options(data, mode, fs_type, 87573f73415SJosef Bacik &subvol_name, &subvol_objectid, 876e15d0542SXin Zhong &subvol_rootid, &fs_devices); 877edf24abeSChristoph Hellwig if (error) 878061dbc6bSAl Viro return ERR_PTR(error); 879edf24abeSChristoph Hellwig 880830c4adbSJosef Bacik if (subvol_name) { 881830c4adbSJosef Bacik root = mount_subvol(subvol_name, flags, device_name, data); 882830c4adbSJosef Bacik kfree(subvol_name); 883830c4adbSJosef Bacik return root; 884830c4adbSJosef Bacik } 885830c4adbSJosef Bacik 886306e16ceSDavid Sterba error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices); 8878a4b83ccSChris Mason if (error) 888830c4adbSJosef Bacik return ERR_PTR(error); 8894b82d6e4SYan 89097288f2cSChristoph Hellwig error = btrfs_open_devices(fs_devices, mode, fs_type); 8918a4b83ccSChris Mason if (error) 892830c4adbSJosef Bacik return ERR_PTR(error); 8938a4b83ccSChris Mason 8942b82032cSYan Zheng if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) { 8952b82032cSYan Zheng error = -EACCES; 8962b82032cSYan Zheng goto error_close_devices; 8972b82032cSYan Zheng } 8982b82032cSYan Zheng 899450ba0eaSJosef Bacik /* 900450ba0eaSJosef Bacik * Setup a dummy root and fs_info for test/set super. This is because 901450ba0eaSJosef Bacik * we don't actually fill this stuff out until open_ctree, but we need 902450ba0eaSJosef Bacik * it for searching for existing supers, so this lets us do that and 903450ba0eaSJosef Bacik * then open_ctree will properly initialize everything later. 904450ba0eaSJosef Bacik */ 905450ba0eaSJosef Bacik fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS); 906450ba0eaSJosef Bacik tree_root = kzalloc(sizeof(struct btrfs_root), GFP_NOFS); 907450ba0eaSJosef Bacik if (!fs_info || !tree_root) { 908450ba0eaSJosef Bacik error = -ENOMEM; 909450ba0eaSJosef Bacik goto error_close_devices; 910450ba0eaSJosef Bacik } 911450ba0eaSJosef Bacik fs_info->tree_root = tree_root; 912450ba0eaSJosef Bacik fs_info->fs_devices = fs_devices; 913450ba0eaSJosef Bacik tree_root->fs_info = fs_info; 914450ba0eaSJosef Bacik 915dfe25020SChris Mason bdev = fs_devices->latest_bdev; 916450ba0eaSJosef Bacik s = sget(fs_type, btrfs_test_super, btrfs_set_super, tree_root); 917830c4adbSJosef Bacik if (IS_ERR(s)) { 918830c4adbSJosef Bacik error = PTR_ERR(s); 919830c4adbSJosef Bacik goto error_close_devices; 920830c4adbSJosef Bacik } 9214b82d6e4SYan 9224b82d6e4SYan if (s->s_root) { 9234b82d6e4SYan if ((flags ^ s->s_flags) & MS_RDONLY) { 9246f5bbff9SAl Viro deactivate_locked_super(s); 925830c4adbSJosef Bacik return ERR_PTR(-EBUSY); 9264b82d6e4SYan } 9274b82d6e4SYan 9282b82032cSYan Zheng btrfs_close_devices(fs_devices); 929bdc924bbSIan Kent kfree(fs_info); 930bdc924bbSIan Kent kfree(tree_root); 9314b82d6e4SYan } else { 9324b82d6e4SYan char b[BDEVNAME_SIZE]; 9334b82d6e4SYan 9349e1f1de0SAl Viro s->s_flags = flags | MS_NOSEC; 9354b82d6e4SYan strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); 9368a4b83ccSChris Mason error = btrfs_fill_super(s, fs_devices, data, 9378a4b83ccSChris Mason flags & MS_SILENT ? 1 : 0); 9384b82d6e4SYan if (error) { 9396f5bbff9SAl Viro deactivate_locked_super(s); 940830c4adbSJosef Bacik return ERR_PTR(error); 9414b82d6e4SYan } 9424b82d6e4SYan 943788f20ebSChris Mason btrfs_sb(s)->fs_info->bdev_holder = fs_type; 9444b82d6e4SYan s->s_flags |= MS_ACTIVE; 9454b82d6e4SYan } 9464b82d6e4SYan 947e15d0542SXin Zhong root = get_default_root(s, subvol_objectid); 948e15d0542SXin Zhong if (IS_ERR(root)) { 949e15d0542SXin Zhong deactivate_locked_super(s); 950830c4adbSJosef Bacik return root; 95176fcef19SDavid Woodhouse } 9524b82d6e4SYan 953061dbc6bSAl Viro return root; 9544b82d6e4SYan 955c146afadSYan Zheng error_close_devices: 9568a4b83ccSChris Mason btrfs_close_devices(fs_devices); 957450ba0eaSJosef Bacik kfree(fs_info); 958450ba0eaSJosef Bacik kfree(tree_root); 959061dbc6bSAl Viro return ERR_PTR(error); 9604b82d6e4SYan } 9612e635a27SChris Mason 962c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 963c146afadSYan Zheng { 964c146afadSYan Zheng struct btrfs_root *root = btrfs_sb(sb); 965c146afadSYan Zheng int ret; 966c146afadSYan Zheng 967b288052eSChris Mason ret = btrfs_parse_options(root, data); 968b288052eSChris Mason if (ret) 969b288052eSChris Mason return -EINVAL; 970b288052eSChris Mason 971c146afadSYan Zheng if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 972c146afadSYan Zheng return 0; 973c146afadSYan Zheng 974c146afadSYan Zheng if (*flags & MS_RDONLY) { 975c146afadSYan Zheng sb->s_flags |= MS_RDONLY; 976c146afadSYan Zheng 977c146afadSYan Zheng ret = btrfs_commit_super(root); 978c146afadSYan Zheng WARN_ON(ret); 979c146afadSYan Zheng } else { 9802b82032cSYan Zheng if (root->fs_info->fs_devices->rw_devices == 0) 9812b82032cSYan Zheng return -EACCES; 9822b82032cSYan Zheng 983c146afadSYan Zheng if (btrfs_super_log_root(&root->fs_info->super_copy) != 0) 984c146afadSYan Zheng return -EINVAL; 985c146afadSYan Zheng 986d68fc57bSYan, Zheng ret = btrfs_cleanup_fs_roots(root->fs_info); 987c146afadSYan Zheng WARN_ON(ret); 988c146afadSYan Zheng 989d68fc57bSYan, Zheng /* recover relocation */ 990d68fc57bSYan, Zheng ret = btrfs_recover_relocation(root); 991c146afadSYan Zheng WARN_ON(ret); 992c146afadSYan Zheng 993c146afadSYan Zheng sb->s_flags &= ~MS_RDONLY; 994c146afadSYan Zheng } 995c146afadSYan Zheng 996c146afadSYan Zheng return 0; 997c146afadSYan Zheng } 998c146afadSYan Zheng 999bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 1000bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1, 1001bcd53741SArne Jansen const void *dev_info2) 1002bcd53741SArne Jansen { 1003bcd53741SArne Jansen if (((struct btrfs_device_info *)dev_info1)->max_avail > 1004bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1005bcd53741SArne Jansen return -1; 1006bcd53741SArne Jansen else if (((struct btrfs_device_info *)dev_info1)->max_avail < 1007bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1008bcd53741SArne Jansen return 1; 1009bcd53741SArne Jansen else 1010bcd53741SArne Jansen return 0; 1011bcd53741SArne Jansen } 1012bcd53741SArne Jansen 1013bcd53741SArne Jansen /* 1014bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 1015bcd53741SArne Jansen * is stored.(Descending Sort) 1016bcd53741SArne Jansen */ 1017bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 1018bcd53741SArne Jansen struct btrfs_device_info *devices, 1019bcd53741SArne Jansen size_t nr_devices) 1020bcd53741SArne Jansen { 1021bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 1022bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 1023bcd53741SArne Jansen } 1024bcd53741SArne Jansen 10256d07bcecSMiao Xie /* 10266d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 10276d07bcecSMiao Xie * file data. 10286d07bcecSMiao Xie */ 10296d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes) 10306d07bcecSMiao Xie { 10316d07bcecSMiao Xie struct btrfs_fs_info *fs_info = root->fs_info; 10326d07bcecSMiao Xie struct btrfs_device_info *devices_info; 10336d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 10346d07bcecSMiao Xie struct btrfs_device *device; 10356d07bcecSMiao Xie u64 skip_space; 10366d07bcecSMiao Xie u64 type; 10376d07bcecSMiao Xie u64 avail_space; 10386d07bcecSMiao Xie u64 used_space; 10396d07bcecSMiao Xie u64 min_stripe_size; 10406d07bcecSMiao Xie int min_stripes = 1; 10416d07bcecSMiao Xie int i = 0, nr_devices; 10426d07bcecSMiao Xie int ret; 10436d07bcecSMiao Xie 10446d07bcecSMiao Xie nr_devices = fs_info->fs_devices->rw_devices; 10456d07bcecSMiao Xie BUG_ON(!nr_devices); 10466d07bcecSMiao Xie 10476d07bcecSMiao Xie devices_info = kmalloc(sizeof(*devices_info) * nr_devices, 10486d07bcecSMiao Xie GFP_NOFS); 10496d07bcecSMiao Xie if (!devices_info) 10506d07bcecSMiao Xie return -ENOMEM; 10516d07bcecSMiao Xie 10526d07bcecSMiao Xie /* calc min stripe number for data space alloction */ 10536d07bcecSMiao Xie type = btrfs_get_alloc_profile(root, 1); 10546d07bcecSMiao Xie if (type & BTRFS_BLOCK_GROUP_RAID0) 10556d07bcecSMiao Xie min_stripes = 2; 10566d07bcecSMiao Xie else if (type & BTRFS_BLOCK_GROUP_RAID1) 10576d07bcecSMiao Xie min_stripes = 2; 10586d07bcecSMiao Xie else if (type & BTRFS_BLOCK_GROUP_RAID10) 10596d07bcecSMiao Xie min_stripes = 4; 10606d07bcecSMiao Xie 10616d07bcecSMiao Xie if (type & BTRFS_BLOCK_GROUP_DUP) 10626d07bcecSMiao Xie min_stripe_size = 2 * BTRFS_STRIPE_LEN; 10636d07bcecSMiao Xie else 10646d07bcecSMiao Xie min_stripe_size = BTRFS_STRIPE_LEN; 10656d07bcecSMiao Xie 10666d07bcecSMiao Xie list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { 10676d07bcecSMiao Xie if (!device->in_fs_metadata) 10686d07bcecSMiao Xie continue; 10696d07bcecSMiao Xie 10706d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 10716d07bcecSMiao Xie 10726d07bcecSMiao Xie /* align with stripe_len */ 10736d07bcecSMiao Xie do_div(avail_space, BTRFS_STRIPE_LEN); 10746d07bcecSMiao Xie avail_space *= BTRFS_STRIPE_LEN; 10756d07bcecSMiao Xie 10766d07bcecSMiao Xie /* 10776d07bcecSMiao Xie * In order to avoid overwritting the superblock on the drive, 10786d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 10796d07bcecSMiao Xie * allocation. 10806d07bcecSMiao Xie */ 10816d07bcecSMiao Xie skip_space = 1024 * 1024; 10826d07bcecSMiao Xie 10836d07bcecSMiao Xie /* user can set the offset in fs_info->alloc_start. */ 10846d07bcecSMiao Xie if (fs_info->alloc_start + BTRFS_STRIPE_LEN <= 10856d07bcecSMiao Xie device->total_bytes) 10866d07bcecSMiao Xie skip_space = max(fs_info->alloc_start, skip_space); 10876d07bcecSMiao Xie 10886d07bcecSMiao Xie /* 10896d07bcecSMiao Xie * btrfs can not use the free space in [0, skip_space - 1], 10906d07bcecSMiao Xie * we must subtract it from the total. In order to implement 10916d07bcecSMiao Xie * it, we account the used space in this range first. 10926d07bcecSMiao Xie */ 10936d07bcecSMiao Xie ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1, 10946d07bcecSMiao Xie &used_space); 10956d07bcecSMiao Xie if (ret) { 10966d07bcecSMiao Xie kfree(devices_info); 10976d07bcecSMiao Xie return ret; 10986d07bcecSMiao Xie } 10996d07bcecSMiao Xie 11006d07bcecSMiao Xie /* calc the free space in [0, skip_space - 1] */ 11016d07bcecSMiao Xie skip_space -= used_space; 11026d07bcecSMiao Xie 11036d07bcecSMiao Xie /* 11046d07bcecSMiao Xie * we can use the free space in [0, skip_space - 1], subtract 11056d07bcecSMiao Xie * it from the total. 11066d07bcecSMiao Xie */ 11076d07bcecSMiao Xie if (avail_space && avail_space >= skip_space) 11086d07bcecSMiao Xie avail_space -= skip_space; 11096d07bcecSMiao Xie else 11106d07bcecSMiao Xie avail_space = 0; 11116d07bcecSMiao Xie 11126d07bcecSMiao Xie if (avail_space < min_stripe_size) 11136d07bcecSMiao Xie continue; 11146d07bcecSMiao Xie 11156d07bcecSMiao Xie devices_info[i].dev = device; 11166d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 11176d07bcecSMiao Xie 11186d07bcecSMiao Xie i++; 11196d07bcecSMiao Xie } 11206d07bcecSMiao Xie 11216d07bcecSMiao Xie nr_devices = i; 11226d07bcecSMiao Xie 11236d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 11246d07bcecSMiao Xie 11256d07bcecSMiao Xie i = nr_devices - 1; 11266d07bcecSMiao Xie avail_space = 0; 11276d07bcecSMiao Xie while (nr_devices >= min_stripes) { 11286d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 11296d07bcecSMiao Xie int j; 11306d07bcecSMiao Xie u64 alloc_size; 11316d07bcecSMiao Xie 11326d07bcecSMiao Xie avail_space += devices_info[i].max_avail * min_stripes; 11336d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 11346d07bcecSMiao Xie for (j = i + 1 - min_stripes; j <= i; j++) 11356d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 11366d07bcecSMiao Xie } 11376d07bcecSMiao Xie i--; 11386d07bcecSMiao Xie nr_devices--; 11396d07bcecSMiao Xie } 11406d07bcecSMiao Xie 11416d07bcecSMiao Xie kfree(devices_info); 11426d07bcecSMiao Xie *free_bytes = avail_space; 11436d07bcecSMiao Xie return 0; 11446d07bcecSMiao Xie } 11456d07bcecSMiao Xie 11468fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 11478fd17795SChris Mason { 11488fd17795SChris Mason struct btrfs_root *root = btrfs_sb(dentry->d_sb); 11494b52dff6SChris Mason struct btrfs_super_block *disk_super = &root->fs_info->super_copy; 1150bd4d1088SJosef Bacik struct list_head *head = &root->fs_info->space_info; 1151bd4d1088SJosef Bacik struct btrfs_space_info *found; 1152bd4d1088SJosef Bacik u64 total_used = 0; 11536d07bcecSMiao Xie u64 total_free_data = 0; 1154db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 11559d03632eSDavid Woodhouse __be32 *fsid = (__be32 *)root->fs_info->fsid; 11566d07bcecSMiao Xie int ret; 11578fd17795SChris Mason 11586d07bcecSMiao Xie /* holding chunk_muext to avoid allocating new chunks */ 11596d07bcecSMiao Xie mutex_lock(&root->fs_info->chunk_mutex); 1160bd4d1088SJosef Bacik rcu_read_lock(); 116189a55897SJosef Bacik list_for_each_entry_rcu(found, head, list) { 11626d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 11636d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 11646d07bcecSMiao Xie total_free_data -= 11656d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 11666d07bcecSMiao Xie } 11676d07bcecSMiao Xie 1168b742bb82SYan, Zheng total_used += found->disk_used; 116989a55897SJosef Bacik } 1170bd4d1088SJosef Bacik rcu_read_unlock(); 1171bd4d1088SJosef Bacik 11728fd17795SChris Mason buf->f_namelen = BTRFS_NAME_LEN; 1173db94535dSChris Mason buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits; 1174bd4d1088SJosef Bacik buf->f_bfree = buf->f_blocks - (total_used >> bits); 11758fd17795SChris Mason buf->f_bsize = dentry->d_sb->s_blocksize; 11768fd17795SChris Mason buf->f_type = BTRFS_SUPER_MAGIC; 11776d07bcecSMiao Xie buf->f_bavail = total_free_data; 11786d07bcecSMiao Xie ret = btrfs_calc_avail_data_space(root, &total_free_data); 11796d07bcecSMiao Xie if (ret) { 11806d07bcecSMiao Xie mutex_unlock(&root->fs_info->chunk_mutex); 11816d07bcecSMiao Xie return ret; 11826d07bcecSMiao Xie } 11836d07bcecSMiao Xie buf->f_bavail += total_free_data; 11846d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 11856d07bcecSMiao Xie mutex_unlock(&root->fs_info->chunk_mutex); 1186d397712bSChris Mason 11879d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 11889d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 11899d03632eSDavid Woodhouse on a big-endian or little-endian host */ 11909d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 11919d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 119232d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 119332d48fa1SDavid Woodhouse buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32; 119432d48fa1SDavid Woodhouse buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid; 119532d48fa1SDavid Woodhouse 11968fd17795SChris Mason return 0; 11978fd17795SChris Mason } 1198b5133862SChris Mason 11992e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 12002e635a27SChris Mason .owner = THIS_MODULE, 12012e635a27SChris Mason .name = "btrfs", 1202061dbc6bSAl Viro .mount = btrfs_mount, 1203a061fc8dSChris Mason .kill_sb = kill_anon_super, 12042e635a27SChris Mason .fs_flags = FS_REQUIRES_DEV, 12052e635a27SChris Mason }; 1206a9218f6bSChris Mason 1207d352ac68SChris Mason /* 1208d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 1209d352ac68SChris Mason */ 12108a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 12118a4b83ccSChris Mason unsigned long arg) 12128a4b83ccSChris Mason { 12138a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 12148a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 1215c071fcfdSChris Mason int ret = -ENOTTY; 12168a4b83ccSChris Mason 1217e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1218e441d54dSChris Mason return -EPERM; 1219e441d54dSChris Mason 1220dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 1221dae7b665SLi Zefan if (IS_ERR(vol)) 1222dae7b665SLi Zefan return PTR_ERR(vol); 1223c071fcfdSChris Mason 12248a4b83ccSChris Mason switch (cmd) { 12258a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 122697288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 12278a4b83ccSChris Mason &btrfs_fs_type, &fs_devices); 12288a4b83ccSChris Mason break; 12298a4b83ccSChris Mason } 1230dae7b665SLi Zefan 12318a4b83ccSChris Mason kfree(vol); 1232f819d837SLinda Knippers return ret; 12338a4b83ccSChris Mason } 12348a4b83ccSChris Mason 12350176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 1236ed0dab6bSYan { 1237ed0dab6bSYan struct btrfs_root *root = btrfs_sb(sb); 1238a74a4b97SChris Mason mutex_lock(&root->fs_info->transaction_kthread_mutex); 1239a74a4b97SChris Mason mutex_lock(&root->fs_info->cleaner_mutex); 12400176260fSLinus Torvalds return 0; 1241ed0dab6bSYan } 1242ed0dab6bSYan 12430176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb) 1244ed0dab6bSYan { 1245ed0dab6bSYan struct btrfs_root *root = btrfs_sb(sb); 1246a74a4b97SChris Mason mutex_unlock(&root->fs_info->cleaner_mutex); 1247a74a4b97SChris Mason mutex_unlock(&root->fs_info->transaction_kthread_mutex); 12480176260fSLinus Torvalds return 0; 1249ed0dab6bSYan } 12502e635a27SChris Mason 1251b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 125276dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 1253bd555975SAl Viro .evict_inode = btrfs_evict_inode, 1254e20d96d6SChris Mason .put_super = btrfs_put_super, 1255d5719762SChris Mason .sync_fs = btrfs_sync_fs, 1256a9572a15SEric Paris .show_options = btrfs_show_options, 12574730a4bcSChris Mason .write_inode = btrfs_write_inode, 1258b5133862SChris Mason .dirty_inode = btrfs_dirty_inode, 12592c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 12602c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 12618fd17795SChris Mason .statfs = btrfs_statfs, 1262c146afadSYan Zheng .remount_fs = btrfs_remount, 12630176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 12640176260fSLinus Torvalds .unfreeze_fs = btrfs_unfreeze, 1265e20d96d6SChris Mason }; 1266a9218f6bSChris Mason 1267a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 1268a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 1269a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 1270a9218f6bSChris Mason .owner = THIS_MODULE, 12716038f373SArnd Bergmann .llseek = noop_llseek, 1272a9218f6bSChris Mason }; 1273a9218f6bSChris Mason 1274a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 1275578454ffSKay Sievers .minor = BTRFS_MINOR, 1276a9218f6bSChris Mason .name = "btrfs-control", 1277a9218f6bSChris Mason .fops = &btrfs_ctl_fops 1278a9218f6bSChris Mason }; 1279a9218f6bSChris Mason 1280578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 1281578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 1282578454ffSKay Sievers 1283a9218f6bSChris Mason static int btrfs_interface_init(void) 1284a9218f6bSChris Mason { 1285a9218f6bSChris Mason return misc_register(&btrfs_misc); 1286a9218f6bSChris Mason } 1287a9218f6bSChris Mason 1288b2950863SChristoph Hellwig static void btrfs_interface_exit(void) 1289a9218f6bSChris Mason { 1290a9218f6bSChris Mason if (misc_deregister(&btrfs_misc) < 0) 1291d397712bSChris Mason printk(KERN_INFO "misc_deregister failed for control device"); 1292a9218f6bSChris Mason } 1293a9218f6bSChris Mason 12942e635a27SChris Mason static int __init init_btrfs_fs(void) 12952e635a27SChris Mason { 12962c90e5d6SChris Mason int err; 129758176a96SJosef Bacik 129858176a96SJosef Bacik err = btrfs_init_sysfs(); 129958176a96SJosef Bacik if (err) 130058176a96SJosef Bacik return err; 130158176a96SJosef Bacik 1302261507a0SLi Zefan err = btrfs_init_compress(); 13032c90e5d6SChris Mason if (err) 1304a74a4b97SChris Mason goto free_sysfs; 1305d1310b2eSChris Mason 1306261507a0SLi Zefan err = btrfs_init_cachep(); 1307261507a0SLi Zefan if (err) 1308261507a0SLi Zefan goto free_compress; 1309261507a0SLi Zefan 1310d1310b2eSChris Mason err = extent_io_init(); 13112f4cbe64SWyatt Banks if (err) 13122f4cbe64SWyatt Banks goto free_cachep; 13132f4cbe64SWyatt Banks 1314d1310b2eSChris Mason err = extent_map_init(); 1315d1310b2eSChris Mason if (err) 1316d1310b2eSChris Mason goto free_extent_io; 1317d1310b2eSChris Mason 131816cdcec7SMiao Xie err = btrfs_delayed_inode_init(); 13192f4cbe64SWyatt Banks if (err) 13202f4cbe64SWyatt Banks goto free_extent_map; 1321c8b97818SChris Mason 132216cdcec7SMiao Xie err = btrfs_interface_init(); 132316cdcec7SMiao Xie if (err) 132416cdcec7SMiao Xie goto free_delayed_inode; 132516cdcec7SMiao Xie 1326a9218f6bSChris Mason err = register_filesystem(&btrfs_fs_type); 1327a9218f6bSChris Mason if (err) 1328a9218f6bSChris Mason goto unregister_ioctl; 1329b3c3da71SChris Mason 1330b3c3da71SChris Mason printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION); 13312f4cbe64SWyatt Banks return 0; 13322f4cbe64SWyatt Banks 1333a9218f6bSChris Mason unregister_ioctl: 1334a9218f6bSChris Mason btrfs_interface_exit(); 133516cdcec7SMiao Xie free_delayed_inode: 133616cdcec7SMiao Xie btrfs_delayed_inode_exit(); 13372f4cbe64SWyatt Banks free_extent_map: 13382f4cbe64SWyatt Banks extent_map_exit(); 1339d1310b2eSChris Mason free_extent_io: 1340d1310b2eSChris Mason extent_io_exit(); 13412f4cbe64SWyatt Banks free_cachep: 13422f4cbe64SWyatt Banks btrfs_destroy_cachep(); 1343261507a0SLi Zefan free_compress: 1344261507a0SLi Zefan btrfs_exit_compress(); 1345a74a4b97SChris Mason free_sysfs: 13462f4cbe64SWyatt Banks btrfs_exit_sysfs(); 13472c90e5d6SChris Mason return err; 13482e635a27SChris Mason } 13492e635a27SChris Mason 13502e635a27SChris Mason static void __exit exit_btrfs_fs(void) 13512e635a27SChris Mason { 135239279cc3SChris Mason btrfs_destroy_cachep(); 135316cdcec7SMiao Xie btrfs_delayed_inode_exit(); 1354a52d9a80SChris Mason extent_map_exit(); 1355d1310b2eSChris Mason extent_io_exit(); 1356a9218f6bSChris Mason btrfs_interface_exit(); 13572e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 135858176a96SJosef Bacik btrfs_exit_sysfs(); 13598a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 1360261507a0SLi Zefan btrfs_exit_compress(); 13612e635a27SChris Mason } 13622e635a27SChris Mason 13632e635a27SChris Mason module_init(init_btrfs_fs) 13642e635a27SChris Mason module_exit(exit_btrfs_fs) 13652e635a27SChris Mason 13662e635a27SChris Mason MODULE_LICENSE("GPL"); 1367