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> 4322c44fe6SJosef Bacik #include <linux/ratelimit.h> 4455e301fdSFilipe Brandenburger #include <linux/btrfs.h> 454b4e25f2SChris Mason #include "compat.h" 4616cdcec7SMiao Xie #include "delayed-inode.h" 472e635a27SChris Mason #include "ctree.h" 48e20d96d6SChris Mason #include "disk-io.h" 49d5719762SChris Mason #include "transaction.h" 502c90e5d6SChris Mason #include "btrfs_inode.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" 579c5085c1SJosef Bacik #include "rcu-string.h" 588dabb742SStefan Behrens #include "dev-replace.h" 592e635a27SChris Mason 601abe9b8aSliubo #define CREATE_TRACE_POINTS 611abe9b8aSliubo #include <trace/events/btrfs.h> 621abe9b8aSliubo 63b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops; 64830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type; 65e20d96d6SChris Mason 665a016047SEric Sandeen static const char *btrfs_decode_error(int errno, char nbuf[16]) 67acce952bSliubo { 68acce952bSliubo char *errstr = NULL; 69acce952bSliubo 70acce952bSliubo switch (errno) { 71acce952bSliubo case -EIO: 72acce952bSliubo errstr = "IO failure"; 73acce952bSliubo break; 74acce952bSliubo case -ENOMEM: 75acce952bSliubo errstr = "Out of memory"; 76acce952bSliubo break; 77acce952bSliubo case -EROFS: 78acce952bSliubo errstr = "Readonly filesystem"; 79acce952bSliubo break; 808c342930SJeff Mahoney case -EEXIST: 818c342930SJeff Mahoney errstr = "Object already exists"; 828c342930SJeff Mahoney break; 83acce952bSliubo default: 84acce952bSliubo if (nbuf) { 85acce952bSliubo if (snprintf(nbuf, 16, "error %d", -errno) >= 0) 86acce952bSliubo errstr = nbuf; 87acce952bSliubo } 88acce952bSliubo break; 89acce952bSliubo } 90acce952bSliubo 91acce952bSliubo return errstr; 92acce952bSliubo } 93acce952bSliubo 94acce952bSliubo static void __save_error_info(struct btrfs_fs_info *fs_info) 95acce952bSliubo { 96acce952bSliubo /* 97acce952bSliubo * today we only save the error info into ram. Long term we'll 98acce952bSliubo * also send it down to the disk 99acce952bSliubo */ 10087533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 101acce952bSliubo } 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 11687533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 117acce952bSliubo sb->s_flags |= MS_RDONLY; 118acce952bSliubo printk(KERN_INFO "btrfs is forced readonly\n"); 1191acd6831SStefan Behrens /* 1201acd6831SStefan Behrens * Note that a running device replace operation is not 1211acd6831SStefan Behrens * canceled here although there is no way to update 1221acd6831SStefan Behrens * the progress. It would add the risk of a deadlock, 1231acd6831SStefan Behrens * therefore the canceling is ommited. The only penalty 1241acd6831SStefan Behrens * is that some I/O remains active until the procedure 1251acd6831SStefan Behrens * completes. The next time when the filesystem is 1261acd6831SStefan Behrens * mounted writeable again, the device replace 1271acd6831SStefan Behrens * operation continues. 1281acd6831SStefan Behrens */ 12949b25e05SJeff Mahoney // WARN_ON(1); 130acce952bSliubo } 131acce952bSliubo } 132acce952bSliubo 133533574c6SJoe Perches #ifdef CONFIG_PRINTK 134acce952bSliubo /* 135acce952bSliubo * __btrfs_std_error decodes expected errors from the caller and 136acce952bSliubo * invokes the approciate error response. 137acce952bSliubo */ 138acce952bSliubo void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function, 1394da35113SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 140acce952bSliubo { 141acce952bSliubo struct super_block *sb = fs_info->sb; 142acce952bSliubo char nbuf[16]; 143acce952bSliubo const char *errstr; 1444da35113SJeff Mahoney va_list args; 1454da35113SJeff Mahoney va_start(args, fmt); 146acce952bSliubo 147acce952bSliubo /* 148acce952bSliubo * Special case: if the error is EROFS, and we're already 149acce952bSliubo * under MS_RDONLY, then it is safe here. 150acce952bSliubo */ 151acce952bSliubo if (errno == -EROFS && (sb->s_flags & MS_RDONLY)) 152acce952bSliubo return; 153acce952bSliubo 1545a016047SEric Sandeen errstr = btrfs_decode_error(errno, nbuf); 1554da35113SJeff Mahoney if (fmt) { 1564da35113SJeff Mahoney struct va_format vaf = { 1574da35113SJeff Mahoney .fmt = fmt, 1584da35113SJeff Mahoney .va = &args, 1594da35113SJeff Mahoney }; 1604da35113SJeff Mahoney 1614da35113SJeff Mahoney printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: %s (%pV)\n", 1624da35113SJeff Mahoney sb->s_id, function, line, errstr, &vaf); 1634da35113SJeff Mahoney } else { 164acce952bSliubo printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: %s\n", 165acce952bSliubo sb->s_id, function, line, errstr); 1664da35113SJeff Mahoney } 167acce952bSliubo 1684da35113SJeff Mahoney /* Don't go through full error handling during mount */ 1694da35113SJeff Mahoney if (sb->s_flags & MS_BORN) { 1704da35113SJeff Mahoney save_error_info(fs_info); 171acce952bSliubo btrfs_handle_error(fs_info); 172acce952bSliubo } 1734da35113SJeff Mahoney va_end(args); 1744da35113SJeff Mahoney } 1754da35113SJeff Mahoney 176533574c6SJoe Perches static const char * const logtypes[] = { 1774da35113SJeff Mahoney "emergency", 1784da35113SJeff Mahoney "alert", 1794da35113SJeff Mahoney "critical", 1804da35113SJeff Mahoney "error", 1814da35113SJeff Mahoney "warning", 1824da35113SJeff Mahoney "notice", 1834da35113SJeff Mahoney "info", 1844da35113SJeff Mahoney "debug", 1854da35113SJeff Mahoney }; 1864da35113SJeff Mahoney 1874da35113SJeff Mahoney void btrfs_printk(struct btrfs_fs_info *fs_info, const char *fmt, ...) 1884da35113SJeff Mahoney { 1894da35113SJeff Mahoney struct super_block *sb = fs_info->sb; 1904da35113SJeff Mahoney char lvl[4]; 1914da35113SJeff Mahoney struct va_format vaf; 1924da35113SJeff Mahoney va_list args; 1934da35113SJeff Mahoney const char *type = logtypes[4]; 194533574c6SJoe Perches int kern_level; 1954da35113SJeff Mahoney 1964da35113SJeff Mahoney va_start(args, fmt); 1974da35113SJeff Mahoney 198533574c6SJoe Perches kern_level = printk_get_level(fmt); 199533574c6SJoe Perches if (kern_level) { 200533574c6SJoe Perches size_t size = printk_skip_level(fmt) - fmt; 201533574c6SJoe Perches memcpy(lvl, fmt, size); 202533574c6SJoe Perches lvl[size] = '\0'; 203533574c6SJoe Perches fmt += size; 204533574c6SJoe Perches type = logtypes[kern_level - '0']; 2054da35113SJeff Mahoney } else 2064da35113SJeff Mahoney *lvl = '\0'; 2074da35113SJeff Mahoney 2084da35113SJeff Mahoney vaf.fmt = fmt; 2094da35113SJeff Mahoney vaf.va = &args; 210533574c6SJoe Perches 2114da35113SJeff Mahoney printk("%sBTRFS %s (device %s): %pV", lvl, type, sb->s_id, &vaf); 212533574c6SJoe Perches 213533574c6SJoe Perches va_end(args); 2144da35113SJeff Mahoney } 215acce952bSliubo 216533574c6SJoe Perches #else 217533574c6SJoe Perches 218533574c6SJoe Perches void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function, 219533574c6SJoe Perches unsigned int line, int errno, const char *fmt, ...) 220533574c6SJoe Perches { 221533574c6SJoe Perches struct super_block *sb = fs_info->sb; 222533574c6SJoe Perches 223533574c6SJoe Perches /* 224533574c6SJoe Perches * Special case: if the error is EROFS, and we're already 225533574c6SJoe Perches * under MS_RDONLY, then it is safe here. 226533574c6SJoe Perches */ 227533574c6SJoe Perches if (errno == -EROFS && (sb->s_flags & MS_RDONLY)) 228533574c6SJoe Perches return; 229533574c6SJoe Perches 230533574c6SJoe Perches /* Don't go through full error handling during mount */ 231533574c6SJoe Perches if (sb->s_flags & MS_BORN) { 232533574c6SJoe Perches save_error_info(fs_info); 233533574c6SJoe Perches btrfs_handle_error(fs_info); 234533574c6SJoe Perches } 235533574c6SJoe Perches } 236533574c6SJoe Perches #endif 237533574c6SJoe Perches 2388c342930SJeff Mahoney /* 23949b25e05SJeff Mahoney * We only mark the transaction aborted and then set the file system read-only. 24049b25e05SJeff Mahoney * This will prevent new transactions from starting or trying to join this 24149b25e05SJeff Mahoney * one. 24249b25e05SJeff Mahoney * 24349b25e05SJeff Mahoney * This means that error recovery at the call site is limited to freeing 24449b25e05SJeff Mahoney * any local memory allocations and passing the error code up without 24549b25e05SJeff Mahoney * further cleanup. The transaction should complete as it normally would 24649b25e05SJeff Mahoney * in the call path but will return -EIO. 24749b25e05SJeff Mahoney * 24849b25e05SJeff Mahoney * We'll complete the cleanup in btrfs_end_transaction and 24949b25e05SJeff Mahoney * btrfs_commit_transaction. 25049b25e05SJeff Mahoney */ 25149b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans, 25249b25e05SJeff Mahoney struct btrfs_root *root, const char *function, 25349b25e05SJeff Mahoney unsigned int line, int errno) 25449b25e05SJeff Mahoney { 25548940662SDaniel J Blueman WARN_ONCE(1, KERN_DEBUG "btrfs: Transaction aborted\n"); 25649b25e05SJeff Mahoney trans->aborted = errno; 25749b25e05SJeff Mahoney /* Nothing used. The other threads that have joined this 25849b25e05SJeff Mahoney * transaction may be able to continue. */ 25949b25e05SJeff Mahoney if (!trans->blocks_used) { 26069ce977aSMiao Xie char nbuf[16]; 26169ce977aSMiao Xie const char *errstr; 26269ce977aSMiao Xie 2635a016047SEric Sandeen errstr = btrfs_decode_error(errno, nbuf); 26469ce977aSMiao Xie btrfs_printk(root->fs_info, 26569ce977aSMiao Xie "%s:%d: Aborting unused transaction(%s).\n", 26669ce977aSMiao Xie function, line, errstr); 26749b25e05SJeff Mahoney return; 26849b25e05SJeff Mahoney } 2698d25a086SMiao Xie ACCESS_ONCE(trans->transaction->aborted) = errno; 27049b25e05SJeff Mahoney __btrfs_std_error(root->fs_info, function, line, errno, NULL); 27149b25e05SJeff Mahoney } 27249b25e05SJeff Mahoney /* 2738c342930SJeff Mahoney * __btrfs_panic decodes unexpected, fatal errors from the caller, 2748c342930SJeff Mahoney * issues an alert, and either panics or BUGs, depending on mount options. 2758c342930SJeff Mahoney */ 2768c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, 2778c342930SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 2788c342930SJeff Mahoney { 2798c342930SJeff Mahoney char nbuf[16]; 2808c342930SJeff Mahoney char *s_id = "<unknown>"; 2818c342930SJeff Mahoney const char *errstr; 2828c342930SJeff Mahoney struct va_format vaf = { .fmt = fmt }; 2838c342930SJeff Mahoney va_list args; 2848c342930SJeff Mahoney 2858c342930SJeff Mahoney if (fs_info) 2868c342930SJeff Mahoney s_id = fs_info->sb->s_id; 2878c342930SJeff Mahoney 2888c342930SJeff Mahoney va_start(args, fmt); 2898c342930SJeff Mahoney vaf.va = &args; 2908c342930SJeff Mahoney 2915a016047SEric Sandeen errstr = btrfs_decode_error(errno, nbuf); 292*aa43a17cSEric Sandeen if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR)) 2938c342930SJeff Mahoney panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (%s)\n", 2948c342930SJeff Mahoney s_id, function, line, &vaf, errstr); 2958c342930SJeff Mahoney 2968c342930SJeff Mahoney printk(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (%s)\n", 2978c342930SJeff Mahoney s_id, function, line, &vaf, errstr); 2988c342930SJeff Mahoney va_end(args); 2998c342930SJeff Mahoney /* Caller calls BUG() */ 3008c342930SJeff Mahoney } 301e20d96d6SChris Mason 302e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 303e20d96d6SChris Mason { 304815745cfSAl Viro (void)close_ctree(btrfs_sb(sb)->tree_root); 305aea52e19SAl Viro /* FIXME: need to fix VFS to return error? */ 306aea52e19SAl Viro /* AV: return it _where_? ->put_super() can be triggered by any number 307aea52e19SAl Viro * of async events, up to and including delivery of SIGKILL to the 308aea52e19SAl Viro * last process that kept it busy. Or segfault in the aforementioned 309aea52e19SAl Viro * process... Whom would you report that to? 310aea52e19SAl Viro */ 311e20d96d6SChris Mason } 3122e635a27SChris Mason 31395e05289SChris Mason enum { 31473f73415SJosef Bacik Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum, 315287a0ab9SJosef Bacik Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, 316287a0ab9SJosef Bacik Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress, 317261507a0SLi Zefan Opt_compress_type, Opt_compress_force, Opt_compress_force_type, 318261507a0SLi Zefan Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard, 31991435650SChris Mason Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed, 3209555c6c1SIlya Dryomov Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache, 3219555c6c1SIlya Dryomov Opt_no_space_cache, Opt_recovery, Opt_skip_balance, 32221adbd5cSStefan Behrens Opt_check_integrity, Opt_check_integrity_including_extent_data, 3238c342930SJeff Mahoney Opt_check_integrity_print_mask, Opt_fatal_errors, 3249555c6c1SIlya Dryomov Opt_err, 32595e05289SChris Mason }; 32695e05289SChris Mason 32795e05289SChris Mason static match_table_t tokens = { 328dfe25020SChris Mason {Opt_degraded, "degraded"}, 32995e05289SChris Mason {Opt_subvol, "subvol=%s"}, 33073f73415SJosef Bacik {Opt_subvolid, "subvolid=%d"}, 33143e570b0SChristoph Hellwig {Opt_device, "device=%s"}, 332b6cda9bcSChris Mason {Opt_nodatasum, "nodatasum"}, 333be20aa9dSChris Mason {Opt_nodatacow, "nodatacow"}, 33421ad10cfSChris Mason {Opt_nobarrier, "nobarrier"}, 3356f568d35SChris Mason {Opt_max_inline, "max_inline=%s"}, 3368f662a76SChris Mason {Opt_alloc_start, "alloc_start=%s"}, 3374543df7eSChris Mason {Opt_thread_pool, "thread_pool=%d"}, 338c8b97818SChris Mason {Opt_compress, "compress"}, 339261507a0SLi Zefan {Opt_compress_type, "compress=%s"}, 340a555f810SChris Mason {Opt_compress_force, "compress-force"}, 341261507a0SLi Zefan {Opt_compress_force_type, "compress-force=%s"}, 342e18e4809SChris Mason {Opt_ssd, "ssd"}, 343451d7585SChris Mason {Opt_ssd_spread, "ssd_spread"}, 3443b30c22fSChris Mason {Opt_nossd, "nossd"}, 34533268eafSJosef Bacik {Opt_noacl, "noacl"}, 3463a5e1404SSage Weil {Opt_notreelog, "notreelog"}, 347dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 34897e728d4SJosef Bacik {Opt_ratio, "metadata_ratio=%d"}, 349e244a0aeSChristoph Hellwig {Opt_discard, "discard"}, 3500af3d00bSJosef Bacik {Opt_space_cache, "space_cache"}, 35188c2ba3bSJosef Bacik {Opt_clear_cache, "clear_cache"}, 3524260f7c7SSage Weil {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, 35391435650SChris Mason {Opt_enospc_debug, "enospc_debug"}, 354e15d0542SXin Zhong {Opt_subvolrootid, "subvolrootid=%d"}, 3554cb5300bSChris Mason {Opt_defrag, "autodefrag"}, 3564b9465cbSChris Mason {Opt_inode_cache, "inode_cache"}, 3578965593eSDavid Sterba {Opt_no_space_cache, "nospace_cache"}, 358af31f5e5SChris Mason {Opt_recovery, "recovery"}, 3599555c6c1SIlya Dryomov {Opt_skip_balance, "skip_balance"}, 36021adbd5cSStefan Behrens {Opt_check_integrity, "check_int"}, 36121adbd5cSStefan Behrens {Opt_check_integrity_including_extent_data, "check_int_data"}, 36221adbd5cSStefan Behrens {Opt_check_integrity_print_mask, "check_int_print_mask=%d"}, 3638c342930SJeff Mahoney {Opt_fatal_errors, "fatal_errors=%s"}, 36433268eafSJosef Bacik {Opt_err, NULL}, 36595e05289SChris Mason }; 36695e05289SChris Mason 367edf24abeSChristoph Hellwig /* 368edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 369edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 37049b25e05SJeff Mahoney * XXX JDM: This needs to be cleaned up for remount. 371edf24abeSChristoph Hellwig */ 372edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options) 37395e05289SChris Mason { 374edf24abeSChristoph Hellwig struct btrfs_fs_info *info = root->fs_info; 37595e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 37673bc1876SJosef Bacik char *p, *num, *orig = NULL; 37773bc1876SJosef Bacik u64 cache_gen; 3784543df7eSChris Mason int intarg; 379a7a3f7caSSage Weil int ret = 0; 380261507a0SLi Zefan char *compress_type; 381261507a0SLi Zefan bool compress_force = false; 382b6cda9bcSChris Mason 3836c41761fSDavid Sterba cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy); 38473bc1876SJosef Bacik if (cache_gen) 38573bc1876SJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 38673bc1876SJosef Bacik 38795e05289SChris Mason if (!options) 38873bc1876SJosef Bacik goto out; 38995e05289SChris Mason 390be20aa9dSChris Mason /* 391be20aa9dSChris Mason * strsep changes the string, duplicate it because parse_options 392be20aa9dSChris Mason * gets called twice 393be20aa9dSChris Mason */ 394be20aa9dSChris Mason options = kstrdup(options, GFP_NOFS); 395be20aa9dSChris Mason if (!options) 396be20aa9dSChris Mason return -ENOMEM; 397be20aa9dSChris Mason 398da495eccSJosef Bacik orig = options; 399be20aa9dSChris Mason 40095e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 40195e05289SChris Mason int token; 40295e05289SChris Mason if (!*p) 40395e05289SChris Mason continue; 40495e05289SChris Mason 40595e05289SChris Mason token = match_token(p, tokens, args); 40695e05289SChris Mason switch (token) { 407dfe25020SChris Mason case Opt_degraded: 408edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: allowing degraded mounts\n"); 409dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 410dfe25020SChris Mason break; 41195e05289SChris Mason case Opt_subvol: 41273f73415SJosef Bacik case Opt_subvolid: 413e15d0542SXin Zhong case Opt_subvolrootid: 41443e570b0SChristoph Hellwig case Opt_device: 415edf24abeSChristoph Hellwig /* 41643e570b0SChristoph Hellwig * These are parsed by btrfs_parse_early_options 417edf24abeSChristoph Hellwig * and can be happily ignored here. 418edf24abeSChristoph Hellwig */ 41995e05289SChris Mason break; 420b6cda9bcSChris Mason case Opt_nodatasum: 421067c28adSChris Mason printk(KERN_INFO "btrfs: setting nodatasum\n"); 422b6cda9bcSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 423be20aa9dSChris Mason break; 424be20aa9dSChris Mason case Opt_nodatacow: 425bedb2ccaSAndrei Popa if (!btrfs_test_opt(root, COMPRESS) || 426bedb2ccaSAndrei Popa !btrfs_test_opt(root, FORCE_COMPRESS)) { 427bedb2ccaSAndrei Popa printk(KERN_INFO "btrfs: setting nodatacow, compression disabled\n"); 428bedb2ccaSAndrei Popa } else { 429edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: setting nodatacow\n"); 430bedb2ccaSAndrei Popa } 431bedb2ccaSAndrei Popa info->compress_type = BTRFS_COMPRESS_NONE; 432bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, COMPRESS); 433bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 434be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 435be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 436b6cda9bcSChris Mason break; 437a555f810SChris Mason case Opt_compress_force: 438261507a0SLi Zefan case Opt_compress_force_type: 439261507a0SLi Zefan compress_force = true; 440261507a0SLi Zefan case Opt_compress: 441261507a0SLi Zefan case Opt_compress_type: 442261507a0SLi Zefan if (token == Opt_compress || 443261507a0SLi Zefan token == Opt_compress_force || 444261507a0SLi Zefan strcmp(args[0].from, "zlib") == 0) { 445261507a0SLi Zefan compress_type = "zlib"; 446261507a0SLi Zefan info->compress_type = BTRFS_COMPRESS_ZLIB; 447063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 448bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 449bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 450a6fa6faeSLi Zefan } else if (strcmp(args[0].from, "lzo") == 0) { 451a6fa6faeSLi Zefan compress_type = "lzo"; 452a6fa6faeSLi Zefan info->compress_type = BTRFS_COMPRESS_LZO; 453063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 454bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 455bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 4562b0ce2c2SMitch Harder btrfs_set_fs_incompat(info, COMPRESS_LZO); 457063849eaSArnd Hannemann } else if (strncmp(args[0].from, "no", 2) == 0) { 458063849eaSArnd Hannemann compress_type = "no"; 459063849eaSArnd Hannemann info->compress_type = BTRFS_COMPRESS_NONE; 460063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, COMPRESS); 461063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 462063849eaSArnd Hannemann compress_force = false; 463261507a0SLi Zefan } else { 464261507a0SLi Zefan ret = -EINVAL; 465261507a0SLi Zefan goto out; 466261507a0SLi Zefan } 467261507a0SLi Zefan 468261507a0SLi Zefan if (compress_force) { 469261507a0SLi Zefan btrfs_set_opt(info->mount_opt, FORCE_COMPRESS); 470261507a0SLi Zefan pr_info("btrfs: force %s compression\n", 471261507a0SLi Zefan compress_type); 472261507a0SLi Zefan } else 473261507a0SLi Zefan pr_info("btrfs: use %s compression\n", 474261507a0SLi Zefan compress_type); 475a555f810SChris Mason break; 476e18e4809SChris Mason case Opt_ssd: 477edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: use ssd allocation scheme\n"); 478e18e4809SChris Mason btrfs_set_opt(info->mount_opt, SSD); 479e18e4809SChris Mason break; 480451d7585SChris Mason case Opt_ssd_spread: 481451d7585SChris Mason printk(KERN_INFO "btrfs: use spread ssd " 482451d7585SChris Mason "allocation scheme\n"); 483451d7585SChris Mason btrfs_set_opt(info->mount_opt, SSD); 484451d7585SChris Mason btrfs_set_opt(info->mount_opt, SSD_SPREAD); 485451d7585SChris Mason break; 4863b30c22fSChris Mason case Opt_nossd: 487451d7585SChris Mason printk(KERN_INFO "btrfs: not using ssd allocation " 488451d7585SChris Mason "scheme\n"); 489c289811cSChris Mason btrfs_set_opt(info->mount_opt, NOSSD); 4903b30c22fSChris Mason btrfs_clear_opt(info->mount_opt, SSD); 491451d7585SChris Mason btrfs_clear_opt(info->mount_opt, SSD_SPREAD); 4923b30c22fSChris Mason break; 49321ad10cfSChris Mason case Opt_nobarrier: 494edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: turning off barriers\n"); 49521ad10cfSChris Mason btrfs_set_opt(info->mount_opt, NOBARRIER); 49621ad10cfSChris Mason break; 4974543df7eSChris Mason case Opt_thread_pool: 4984543df7eSChris Mason intarg = 0; 4994543df7eSChris Mason match_int(&args[0], &intarg); 5000d2450abSSergei Trofimovich if (intarg) 5014543df7eSChris Mason info->thread_pool_size = intarg; 5024543df7eSChris Mason break; 5036f568d35SChris Mason case Opt_max_inline: 504edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5056f568d35SChris Mason if (num) { 50691748467SAkinobu Mita info->max_inline = memparse(num, NULL); 5076f568d35SChris Mason kfree(num); 5086f568d35SChris Mason 50915ada040SChris Mason if (info->max_inline) { 5106f568d35SChris Mason info->max_inline = max_t(u64, 51115ada040SChris Mason info->max_inline, 51215ada040SChris Mason root->sectorsize); 51315ada040SChris Mason } 514edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: max_inline at %llu\n", 51521380931SJoel Becker (unsigned long long)info->max_inline); 5166f568d35SChris Mason } 5176f568d35SChris Mason break; 5188f662a76SChris Mason case Opt_alloc_start: 519edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5208f662a76SChris Mason if (num) { 521c018daecSMiao Xie mutex_lock(&info->chunk_mutex); 52291748467SAkinobu Mita info->alloc_start = memparse(num, NULL); 523c018daecSMiao Xie mutex_unlock(&info->chunk_mutex); 5248f662a76SChris Mason kfree(num); 525edf24abeSChristoph Hellwig printk(KERN_INFO 526edf24abeSChristoph Hellwig "btrfs: allocations start at %llu\n", 52721380931SJoel Becker (unsigned long long)info->alloc_start); 5288f662a76SChris Mason } 5298f662a76SChris Mason break; 53033268eafSJosef Bacik case Opt_noacl: 53133268eafSJosef Bacik root->fs_info->sb->s_flags &= ~MS_POSIXACL; 53233268eafSJosef Bacik break; 5333a5e1404SSage Weil case Opt_notreelog: 5343a5e1404SSage Weil printk(KERN_INFO "btrfs: disabling tree log\n"); 5353a5e1404SSage Weil btrfs_set_opt(info->mount_opt, NOTREELOG); 5363a5e1404SSage Weil break; 537dccae999SSage Weil case Opt_flushoncommit: 538dccae999SSage Weil printk(KERN_INFO "btrfs: turning on flush-on-commit\n"); 539dccae999SSage Weil btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT); 540dccae999SSage Weil break; 54197e728d4SJosef Bacik case Opt_ratio: 54297e728d4SJosef Bacik intarg = 0; 54397e728d4SJosef Bacik match_int(&args[0], &intarg); 54497e728d4SJosef Bacik if (intarg) { 54597e728d4SJosef Bacik info->metadata_ratio = intarg; 54697e728d4SJosef Bacik printk(KERN_INFO "btrfs: metadata ratio %d\n", 54797e728d4SJosef Bacik info->metadata_ratio); 54897e728d4SJosef Bacik } 54997e728d4SJosef Bacik break; 550e244a0aeSChristoph Hellwig case Opt_discard: 551e244a0aeSChristoph Hellwig btrfs_set_opt(info->mount_opt, DISCARD); 552e244a0aeSChristoph Hellwig break; 5530af3d00bSJosef Bacik case Opt_space_cache: 5540af3d00bSJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 5550de90876SJosef Bacik break; 55673bc1876SJosef Bacik case Opt_no_space_cache: 55773bc1876SJosef Bacik printk(KERN_INFO "btrfs: disabling disk space caching\n"); 55873bc1876SJosef Bacik btrfs_clear_opt(info->mount_opt, SPACE_CACHE); 55973bc1876SJosef Bacik break; 5604b9465cbSChris Mason case Opt_inode_cache: 5614b9465cbSChris Mason printk(KERN_INFO "btrfs: enabling inode map caching\n"); 5624b9465cbSChris Mason btrfs_set_opt(info->mount_opt, INODE_MAP_CACHE); 5634b9465cbSChris Mason break; 56488c2ba3bSJosef Bacik case Opt_clear_cache: 56588c2ba3bSJosef Bacik printk(KERN_INFO "btrfs: force clearing of disk cache\n"); 56688c2ba3bSJosef Bacik btrfs_set_opt(info->mount_opt, CLEAR_CACHE); 5670af3d00bSJosef Bacik break; 5684260f7c7SSage Weil case Opt_user_subvol_rm_allowed: 5694260f7c7SSage Weil btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 5704260f7c7SSage Weil break; 57191435650SChris Mason case Opt_enospc_debug: 57291435650SChris Mason btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 57391435650SChris Mason break; 5744cb5300bSChris Mason case Opt_defrag: 57548940662SDaniel J Blueman printk(KERN_INFO "btrfs: enabling auto defrag\n"); 5764cb5300bSChris Mason btrfs_set_opt(info->mount_opt, AUTO_DEFRAG); 5774cb5300bSChris Mason break; 578af31f5e5SChris Mason case Opt_recovery: 57948940662SDaniel J Blueman printk(KERN_INFO "btrfs: enabling auto recovery\n"); 580af31f5e5SChris Mason btrfs_set_opt(info->mount_opt, RECOVERY); 581af31f5e5SChris Mason break; 5829555c6c1SIlya Dryomov case Opt_skip_balance: 5839555c6c1SIlya Dryomov btrfs_set_opt(info->mount_opt, SKIP_BALANCE); 5849555c6c1SIlya Dryomov break; 58521adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 58621adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 58721adbd5cSStefan Behrens printk(KERN_INFO "btrfs: enabling check integrity" 58821adbd5cSStefan Behrens " including extent data\n"); 58921adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, 59021adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA); 59121adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 59221adbd5cSStefan Behrens break; 59321adbd5cSStefan Behrens case Opt_check_integrity: 59421adbd5cSStefan Behrens printk(KERN_INFO "btrfs: enabling check integrity\n"); 59521adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 59621adbd5cSStefan Behrens break; 59721adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 59821adbd5cSStefan Behrens intarg = 0; 59921adbd5cSStefan Behrens match_int(&args[0], &intarg); 60021adbd5cSStefan Behrens if (intarg) { 60121adbd5cSStefan Behrens info->check_integrity_print_mask = intarg; 60221adbd5cSStefan Behrens printk(KERN_INFO "btrfs:" 60321adbd5cSStefan Behrens " check_integrity_print_mask 0x%x\n", 60421adbd5cSStefan Behrens info->check_integrity_print_mask); 60521adbd5cSStefan Behrens } 60621adbd5cSStefan Behrens break; 60721adbd5cSStefan Behrens #else 60821adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 60921adbd5cSStefan Behrens case Opt_check_integrity: 61021adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 61121adbd5cSStefan Behrens printk(KERN_ERR "btrfs: support for check_integrity*" 61221adbd5cSStefan Behrens " not compiled in!\n"); 61321adbd5cSStefan Behrens ret = -EINVAL; 61421adbd5cSStefan Behrens goto out; 61521adbd5cSStefan Behrens #endif 6168c342930SJeff Mahoney case Opt_fatal_errors: 6178c342930SJeff Mahoney if (strcmp(args[0].from, "panic") == 0) 6188c342930SJeff Mahoney btrfs_set_opt(info->mount_opt, 6198c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 6208c342930SJeff Mahoney else if (strcmp(args[0].from, "bug") == 0) 6218c342930SJeff Mahoney btrfs_clear_opt(info->mount_opt, 6228c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 6238c342930SJeff Mahoney else { 6248c342930SJeff Mahoney ret = -EINVAL; 6258c342930SJeff Mahoney goto out; 6268c342930SJeff Mahoney } 6278c342930SJeff Mahoney break; 628a7a3f7caSSage Weil case Opt_err: 629a7a3f7caSSage Weil printk(KERN_INFO "btrfs: unrecognized mount option " 630a7a3f7caSSage Weil "'%s'\n", p); 631a7a3f7caSSage Weil ret = -EINVAL; 632a7a3f7caSSage Weil goto out; 63395e05289SChris Mason default: 634be20aa9dSChris Mason break; 63595e05289SChris Mason } 63695e05289SChris Mason } 637a7a3f7caSSage Weil out: 63873bc1876SJosef Bacik if (!ret && btrfs_test_opt(root, SPACE_CACHE)) 63973bc1876SJosef Bacik printk(KERN_INFO "btrfs: disk space caching is enabled\n"); 640da495eccSJosef Bacik kfree(orig); 641a7a3f7caSSage Weil return ret; 642edf24abeSChristoph Hellwig } 643edf24abeSChristoph Hellwig 644edf24abeSChristoph Hellwig /* 645edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 646edf24abeSChristoph Hellwig * 647edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 648edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 649edf24abeSChristoph Hellwig */ 65097288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 65173f73415SJosef Bacik void *holder, char **subvol_name, u64 *subvol_objectid, 652e15d0542SXin Zhong u64 *subvol_rootid, struct btrfs_fs_devices **fs_devices) 653edf24abeSChristoph Hellwig { 654edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 65583c8c9bdSJeff Liu char *device_name, *opts, *orig, *p; 656edf24abeSChristoph Hellwig int error = 0; 65773f73415SJosef Bacik int intarg; 658edf24abeSChristoph Hellwig 659edf24abeSChristoph Hellwig if (!options) 660830c4adbSJosef Bacik return 0; 661edf24abeSChristoph Hellwig 662edf24abeSChristoph Hellwig /* 663edf24abeSChristoph Hellwig * strsep changes the string, duplicate it because parse_options 664edf24abeSChristoph Hellwig * gets called twice 665edf24abeSChristoph Hellwig */ 666edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 667edf24abeSChristoph Hellwig if (!opts) 668edf24abeSChristoph Hellwig return -ENOMEM; 6693f3d0bc0STero Roponen orig = opts; 670edf24abeSChristoph Hellwig 671edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 672edf24abeSChristoph Hellwig int token; 673edf24abeSChristoph Hellwig if (!*p) 674edf24abeSChristoph Hellwig continue; 675edf24abeSChristoph Hellwig 676edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 677edf24abeSChristoph Hellwig switch (token) { 678edf24abeSChristoph Hellwig case Opt_subvol: 679a90e8b6fSIlya Dryomov kfree(*subvol_name); 680edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 681edf24abeSChristoph Hellwig break; 68273f73415SJosef Bacik case Opt_subvolid: 68373f73415SJosef Bacik intarg = 0; 6844849f01dSJosef Bacik error = match_int(&args[0], &intarg); 6854849f01dSJosef Bacik if (!error) { 6864849f01dSJosef Bacik /* we want the original fs_tree */ 6874849f01dSJosef Bacik if (!intarg) 6884849f01dSJosef Bacik *subvol_objectid = 6894849f01dSJosef Bacik BTRFS_FS_TREE_OBJECTID; 6904849f01dSJosef Bacik else 69173f73415SJosef Bacik *subvol_objectid = intarg; 6924849f01dSJosef Bacik } 69373f73415SJosef Bacik break; 694e15d0542SXin Zhong case Opt_subvolrootid: 695e15d0542SXin Zhong intarg = 0; 696e15d0542SXin Zhong error = match_int(&args[0], &intarg); 697e15d0542SXin Zhong if (!error) { 698e15d0542SXin Zhong /* we want the original fs_tree */ 699e15d0542SXin Zhong if (!intarg) 700e15d0542SXin Zhong *subvol_rootid = 701e15d0542SXin Zhong BTRFS_FS_TREE_OBJECTID; 702e15d0542SXin Zhong else 703e15d0542SXin Zhong *subvol_rootid = intarg; 704e15d0542SXin Zhong } 705e15d0542SXin Zhong break; 70643e570b0SChristoph Hellwig case Opt_device: 70783c8c9bdSJeff Liu device_name = match_strdup(&args[0]); 70883c8c9bdSJeff Liu if (!device_name) { 70983c8c9bdSJeff Liu error = -ENOMEM; 71083c8c9bdSJeff Liu goto out; 71183c8c9bdSJeff Liu } 71283c8c9bdSJeff Liu error = btrfs_scan_one_device(device_name, 71343e570b0SChristoph Hellwig flags, holder, fs_devices); 71483c8c9bdSJeff Liu kfree(device_name); 71543e570b0SChristoph Hellwig if (error) 716830c4adbSJosef Bacik goto out; 71743e570b0SChristoph Hellwig break; 718edf24abeSChristoph Hellwig default: 719edf24abeSChristoph Hellwig break; 720edf24abeSChristoph Hellwig } 721edf24abeSChristoph Hellwig } 722edf24abeSChristoph Hellwig 723edf24abeSChristoph Hellwig out: 724830c4adbSJosef Bacik kfree(orig); 725edf24abeSChristoph Hellwig return error; 72695e05289SChris Mason } 72795e05289SChris Mason 72873f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb, 72973f73415SJosef Bacik u64 subvol_objectid) 73073f73415SJosef Bacik { 731815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 732815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 73373f73415SJosef Bacik struct btrfs_root *new_root; 73473f73415SJosef Bacik struct btrfs_dir_item *di; 73573f73415SJosef Bacik struct btrfs_path *path; 73673f73415SJosef Bacik struct btrfs_key location; 73773f73415SJosef Bacik struct inode *inode; 73873f73415SJosef Bacik u64 dir_id; 73973f73415SJosef Bacik int new = 0; 74073f73415SJosef Bacik 74173f73415SJosef Bacik /* 74273f73415SJosef Bacik * We have a specific subvol we want to mount, just setup location and 74373f73415SJosef Bacik * go look up the root. 74473f73415SJosef Bacik */ 74573f73415SJosef Bacik if (subvol_objectid) { 74673f73415SJosef Bacik location.objectid = subvol_objectid; 74773f73415SJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 74873f73415SJosef Bacik location.offset = (u64)-1; 74973f73415SJosef Bacik goto find_root; 75073f73415SJosef Bacik } 75173f73415SJosef Bacik 75273f73415SJosef Bacik path = btrfs_alloc_path(); 75373f73415SJosef Bacik if (!path) 75473f73415SJosef Bacik return ERR_PTR(-ENOMEM); 75573f73415SJosef Bacik path->leave_spinning = 1; 75673f73415SJosef Bacik 75773f73415SJosef Bacik /* 75873f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 75973f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 76073f73415SJosef Bacik * to mount. 76173f73415SJosef Bacik */ 762815745cfSAl Viro dir_id = btrfs_super_root_dir(fs_info->super_copy); 76373f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 764b0839166SJulia Lawall if (IS_ERR(di)) { 765b0839166SJulia Lawall btrfs_free_path(path); 766fb4f6f91SDan Carpenter return ERR_CAST(di); 767b0839166SJulia Lawall } 76873f73415SJosef Bacik if (!di) { 76973f73415SJosef Bacik /* 77073f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 77173f73415SJosef Bacik * it's always been there, but don't freak out, just try and 77273f73415SJosef Bacik * mount to root most subvolume. 77373f73415SJosef Bacik */ 77473f73415SJosef Bacik btrfs_free_path(path); 77573f73415SJosef Bacik dir_id = BTRFS_FIRST_FREE_OBJECTID; 776815745cfSAl Viro new_root = fs_info->fs_root; 77773f73415SJosef Bacik goto setup_root; 77873f73415SJosef Bacik } 77973f73415SJosef Bacik 78073f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 78173f73415SJosef Bacik btrfs_free_path(path); 78273f73415SJosef Bacik 78373f73415SJosef Bacik find_root: 784815745cfSAl Viro new_root = btrfs_read_fs_root_no_name(fs_info, &location); 78573f73415SJosef Bacik if (IS_ERR(new_root)) 786d0b678cbSJulia Lawall return ERR_CAST(new_root); 78773f73415SJosef Bacik 78873f73415SJosef Bacik if (btrfs_root_refs(&new_root->root_item) == 0) 78973f73415SJosef Bacik return ERR_PTR(-ENOENT); 79073f73415SJosef Bacik 79173f73415SJosef Bacik dir_id = btrfs_root_dirid(&new_root->root_item); 79273f73415SJosef Bacik setup_root: 79373f73415SJosef Bacik location.objectid = dir_id; 79473f73415SJosef Bacik location.type = BTRFS_INODE_ITEM_KEY; 79573f73415SJosef Bacik location.offset = 0; 79673f73415SJosef Bacik 79773f73415SJosef Bacik inode = btrfs_iget(sb, &location, new_root, &new); 7984cbd1149SDan Carpenter if (IS_ERR(inode)) 7994cbd1149SDan Carpenter return ERR_CAST(inode); 80073f73415SJosef Bacik 80173f73415SJosef Bacik /* 80273f73415SJosef Bacik * If we're just mounting the root most subvol put the inode and return 80373f73415SJosef Bacik * a reference to the dentry. We will have already gotten a reference 80473f73415SJosef Bacik * to the inode in btrfs_fill_super so we're good to go. 80573f73415SJosef Bacik */ 80673f73415SJosef Bacik if (!new && sb->s_root->d_inode == inode) { 80773f73415SJosef Bacik iput(inode); 80873f73415SJosef Bacik return dget(sb->s_root); 80973f73415SJosef Bacik } 81073f73415SJosef Bacik 811ba5b8958SJosef Bacik return d_obtain_alias(inode); 81273f73415SJosef Bacik } 81373f73415SJosef Bacik 8148a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 8158a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 8168a4b83ccSChris Mason void *data, int silent) 8172e635a27SChris Mason { 8182e635a27SChris Mason struct inode *inode; 819815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 8205d4f98a2SYan Zheng struct btrfs_key key; 82139279cc3SChris Mason int err; 8222e635a27SChris Mason 8232e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 8242e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 825e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 826af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 827be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 8285103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 8292e635a27SChris Mason sb->s_time_gran = 1; 8300eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 83133268eafSJosef Bacik sb->s_flags |= MS_POSIXACL; 83249cf6f45SChris Ball #endif 8330c4d2d95SJosef Bacik sb->s_flags |= MS_I_VERSION; 834ad2b2c80SAl Viro err = open_ctree(sb, fs_devices, (char *)data); 835ad2b2c80SAl Viro if (err) { 836e20d96d6SChris Mason printk("btrfs: open_ctree failed\n"); 837ad2b2c80SAl Viro return err; 838e20d96d6SChris Mason } 839b888db2bSChris Mason 8405d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 8415d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 8425d4f98a2SYan Zheng key.offset = 0; 84398c7089cSAl Viro inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL); 8445d4f98a2SYan Zheng if (IS_ERR(inode)) { 8455d4f98a2SYan Zheng err = PTR_ERR(inode); 84639279cc3SChris Mason goto fail_close; 84739279cc3SChris Mason } 8482e635a27SChris Mason 84948fde701SAl Viro sb->s_root = d_make_root(inode); 85048fde701SAl Viro if (!sb->s_root) { 85139279cc3SChris Mason err = -ENOMEM; 85239279cc3SChris Mason goto fail_close; 8532e635a27SChris Mason } 85458176a96SJosef Bacik 8556885f308SChris Mason save_mount_options(sb, data); 85690a887c9SDan Magenheimer cleancache_init_fs(sb); 85759553edfSAl Viro sb->s_flags |= MS_ACTIVE; 8582e635a27SChris Mason return 0; 8592e635a27SChris Mason 86039279cc3SChris Mason fail_close: 861815745cfSAl Viro close_ctree(fs_info->tree_root); 862d5719762SChris Mason return err; 863d5719762SChris Mason } 864d5719762SChris Mason 8656bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 866d5719762SChris Mason { 867d5719762SChris Mason struct btrfs_trans_handle *trans; 868815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 869815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 870df2ce34cSChris Mason 8711abe9b8aSliubo trace_btrfs_sync_fs(wait); 8721abe9b8aSliubo 873d561c025SChris Mason if (!wait) { 874815745cfSAl Viro filemap_flush(fs_info->btree_inode->i_mapping); 875df2ce34cSChris Mason return 0; 876d561c025SChris Mason } 877771ed689SChris Mason 8786bbe3a9cSLiu Bo btrfs_wait_ordered_extents(root, 0); 879771ed689SChris Mason 880354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 88160376ce4SJosef Bacik if (IS_ERR(trans)) { 882354aa0fbSMiao Xie /* no transaction, don't bother */ 883354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 884bd7de2c9SJosef Bacik return 0; 88598d5dc13STsutomu Itoh return PTR_ERR(trans); 88660376ce4SJosef Bacik } 887bd7de2c9SJosef Bacik return btrfs_commit_transaction(trans, root); 888d5719762SChris Mason } 889d5719762SChris Mason 89034c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 891a9572a15SEric Paris { 892815745cfSAl Viro struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 893815745cfSAl Viro struct btrfs_root *root = info->tree_root; 894200da64eSTsutomu Itoh char *compress_type; 895a9572a15SEric Paris 896a9572a15SEric Paris if (btrfs_test_opt(root, DEGRADED)) 897a9572a15SEric Paris seq_puts(seq, ",degraded"); 898a9572a15SEric Paris if (btrfs_test_opt(root, NODATASUM)) 899a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 900a9572a15SEric Paris if (btrfs_test_opt(root, NODATACOW)) 901a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 902a9572a15SEric Paris if (btrfs_test_opt(root, NOBARRIER)) 903a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 904a9572a15SEric Paris if (info->max_inline != 8192 * 1024) 90521380931SJoel Becker seq_printf(seq, ",max_inline=%llu", 90621380931SJoel Becker (unsigned long long)info->max_inline); 907a9572a15SEric Paris if (info->alloc_start != 0) 90821380931SJoel Becker seq_printf(seq, ",alloc_start=%llu", 90921380931SJoel Becker (unsigned long long)info->alloc_start); 910a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 911a9572a15SEric Paris num_online_cpus() + 2, 8)) 912a9572a15SEric Paris seq_printf(seq, ",thread_pool=%d", info->thread_pool_size); 913200da64eSTsutomu Itoh if (btrfs_test_opt(root, COMPRESS)) { 914200da64eSTsutomu Itoh if (info->compress_type == BTRFS_COMPRESS_ZLIB) 915200da64eSTsutomu Itoh compress_type = "zlib"; 916200da64eSTsutomu Itoh else 917200da64eSTsutomu Itoh compress_type = "lzo"; 918200da64eSTsutomu Itoh if (btrfs_test_opt(root, FORCE_COMPRESS)) 919200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 920200da64eSTsutomu Itoh else 921200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 922200da64eSTsutomu Itoh } 923c289811cSChris Mason if (btrfs_test_opt(root, NOSSD)) 924c289811cSChris Mason seq_puts(seq, ",nossd"); 925451d7585SChris Mason if (btrfs_test_opt(root, SSD_SPREAD)) 926451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 927451d7585SChris Mason else if (btrfs_test_opt(root, SSD)) 928a9572a15SEric Paris seq_puts(seq, ",ssd"); 9293a5e1404SSage Weil if (btrfs_test_opt(root, NOTREELOG)) 9306b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 931dccae999SSage Weil if (btrfs_test_opt(root, FLUSHONCOMMIT)) 9326b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 93320a5239aSMatthew Wilcox if (btrfs_test_opt(root, DISCARD)) 93420a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 935a9572a15SEric Paris if (!(root->fs_info->sb->s_flags & MS_POSIXACL)) 936a9572a15SEric Paris seq_puts(seq, ",noacl"); 937200da64eSTsutomu Itoh if (btrfs_test_opt(root, SPACE_CACHE)) 938200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 93973bc1876SJosef Bacik else 9408965593eSDavid Sterba seq_puts(seq, ",nospace_cache"); 941200da64eSTsutomu Itoh if (btrfs_test_opt(root, CLEAR_CACHE)) 942200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 943200da64eSTsutomu Itoh if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 944200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 9450942caa3SDavid Sterba if (btrfs_test_opt(root, ENOSPC_DEBUG)) 9460942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 9470942caa3SDavid Sterba if (btrfs_test_opt(root, AUTO_DEFRAG)) 9480942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 9490942caa3SDavid Sterba if (btrfs_test_opt(root, INODE_MAP_CACHE)) 9500942caa3SDavid Sterba seq_puts(seq, ",inode_cache"); 9519555c6c1SIlya Dryomov if (btrfs_test_opt(root, SKIP_BALANCE)) 9529555c6c1SIlya Dryomov seq_puts(seq, ",skip_balance"); 9538c342930SJeff Mahoney if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR)) 9548c342930SJeff Mahoney seq_puts(seq, ",fatal_errors=panic"); 955a9572a15SEric Paris return 0; 956a9572a15SEric Paris } 957a9572a15SEric Paris 958a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 9592e635a27SChris Mason { 960815745cfSAl Viro struct btrfs_fs_info *p = data; 961815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(s); 9624b82d6e4SYan 963815745cfSAl Viro return fs_info->fs_devices == p->fs_devices; 9644b82d6e4SYan } 9654b82d6e4SYan 966450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 967450ba0eaSJosef Bacik { 9686de1d09dSAl Viro int err = set_anon_super(s, data); 9696de1d09dSAl Viro if (!err) 970450ba0eaSJosef Bacik s->s_fs_info = data; 9716de1d09dSAl Viro return err; 972450ba0eaSJosef Bacik } 973450ba0eaSJosef Bacik 974830c4adbSJosef Bacik /* 975f9d9ef62SDavid Sterba * subvolumes are identified by ino 256 976f9d9ef62SDavid Sterba */ 977f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode) 978f9d9ef62SDavid Sterba { 979f9d9ef62SDavid Sterba if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 980f9d9ef62SDavid Sterba return 1; 981f9d9ef62SDavid Sterba return 0; 982f9d9ef62SDavid Sterba } 983f9d9ef62SDavid Sterba 984f9d9ef62SDavid Sterba /* 985830c4adbSJosef Bacik * This will strip out the subvol=%s argument for an argument string and add 986830c4adbSJosef Bacik * subvolid=0 to make sure we get the actual tree root for path walking to the 987830c4adbSJosef Bacik * subvol we want. 988830c4adbSJosef Bacik */ 989830c4adbSJosef Bacik static char *setup_root_args(char *args) 990830c4adbSJosef Bacik { 991f60d16a8SJim Meyering unsigned len = strlen(args) + 2 + 1; 992f60d16a8SJim Meyering char *src, *dst, *buf; 993830c4adbSJosef Bacik 994830c4adbSJosef Bacik /* 995f60d16a8SJim Meyering * We need the same args as before, but with this substitution: 996f60d16a8SJim Meyering * s!subvol=[^,]+!subvolid=0! 997830c4adbSJosef Bacik * 998f60d16a8SJim Meyering * Since the replacement string is up to 2 bytes longer than the 999f60d16a8SJim Meyering * original, allocate strlen(args) + 2 + 1 bytes. 1000830c4adbSJosef Bacik */ 1001830c4adbSJosef Bacik 1002f60d16a8SJim Meyering src = strstr(args, "subvol="); 1003830c4adbSJosef Bacik /* This shouldn't happen, but just in case.. */ 1004f60d16a8SJim Meyering if (!src) 1005830c4adbSJosef Bacik return NULL; 1006f60d16a8SJim Meyering 1007f60d16a8SJim Meyering buf = dst = kmalloc(len, GFP_NOFS); 1008f60d16a8SJim Meyering if (!buf) 1009f60d16a8SJim Meyering return NULL; 1010830c4adbSJosef Bacik 1011830c4adbSJosef Bacik /* 1012f60d16a8SJim Meyering * If the subvol= arg is not at the start of the string, 1013f60d16a8SJim Meyering * copy whatever precedes it into buf. 1014830c4adbSJosef Bacik */ 1015f60d16a8SJim Meyering if (src != args) { 1016f60d16a8SJim Meyering *src++ = '\0'; 1017f60d16a8SJim Meyering strcpy(buf, args); 1018f60d16a8SJim Meyering dst += strlen(args); 1019830c4adbSJosef Bacik } 1020830c4adbSJosef Bacik 1021f60d16a8SJim Meyering strcpy(dst, "subvolid=0"); 1022f60d16a8SJim Meyering dst += strlen("subvolid=0"); 1023830c4adbSJosef Bacik 1024830c4adbSJosef Bacik /* 1025f60d16a8SJim Meyering * If there is a "," after the original subvol=... string, 1026f60d16a8SJim Meyering * copy that suffix into our buffer. Otherwise, we're done. 1027830c4adbSJosef Bacik */ 1028f60d16a8SJim Meyering src = strchr(src, ','); 1029f60d16a8SJim Meyering if (src) 1030f60d16a8SJim Meyering strcpy(dst, src); 1031830c4adbSJosef Bacik 1032f60d16a8SJim Meyering return buf; 1033830c4adbSJosef Bacik } 1034830c4adbSJosef Bacik 1035830c4adbSJosef Bacik static struct dentry *mount_subvol(const char *subvol_name, int flags, 1036830c4adbSJosef Bacik const char *device_name, char *data) 1037830c4adbSJosef Bacik { 1038830c4adbSJosef Bacik struct dentry *root; 1039830c4adbSJosef Bacik struct vfsmount *mnt; 1040830c4adbSJosef Bacik char *newargs; 1041830c4adbSJosef Bacik 1042830c4adbSJosef Bacik newargs = setup_root_args(data); 1043830c4adbSJosef Bacik if (!newargs) 1044830c4adbSJosef Bacik return ERR_PTR(-ENOMEM); 1045830c4adbSJosef Bacik mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, 1046830c4adbSJosef Bacik newargs); 1047830c4adbSJosef Bacik kfree(newargs); 1048830c4adbSJosef Bacik if (IS_ERR(mnt)) 1049830c4adbSJosef Bacik return ERR_CAST(mnt); 1050830c4adbSJosef Bacik 1051ea441d11SAl Viro root = mount_subtree(mnt, subvol_name); 1052830c4adbSJosef Bacik 1053ea441d11SAl Viro if (!IS_ERR(root) && !is_subvolume_inode(root->d_inode)) { 1054ea441d11SAl Viro struct super_block *s = root->d_sb; 1055ea441d11SAl Viro dput(root); 1056ea441d11SAl Viro root = ERR_PTR(-EINVAL); 1057ea441d11SAl Viro deactivate_locked_super(s); 1058f9d9ef62SDavid Sterba printk(KERN_ERR "btrfs: '%s' is not a valid subvolume\n", 1059f9d9ef62SDavid Sterba subvol_name); 1060f9d9ef62SDavid Sterba } 1061f9d9ef62SDavid Sterba 1062830c4adbSJosef Bacik return root; 1063830c4adbSJosef Bacik } 1064450ba0eaSJosef Bacik 1065edf24abeSChristoph Hellwig /* 1066edf24abeSChristoph Hellwig * Find a superblock for the given device / mount point. 1067edf24abeSChristoph Hellwig * 1068edf24abeSChristoph Hellwig * Note: This is based on get_sb_bdev from fs/super.c with a few additions 1069edf24abeSChristoph Hellwig * for multiple device setup. Make sure to keep it in sync. 1070edf24abeSChristoph Hellwig */ 1071061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1072306e16ceSDavid Sterba const char *device_name, void *data) 10734b82d6e4SYan { 10744b82d6e4SYan struct block_device *bdev = NULL; 10754b82d6e4SYan struct super_block *s; 10764b82d6e4SYan struct dentry *root; 10778a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices = NULL; 1078450ba0eaSJosef Bacik struct btrfs_fs_info *fs_info = NULL; 107997288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 108073f73415SJosef Bacik char *subvol_name = NULL; 108173f73415SJosef Bacik u64 subvol_objectid = 0; 1082e15d0542SXin Zhong u64 subvol_rootid = 0; 10834b82d6e4SYan int error = 0; 10844b82d6e4SYan 108597288f2cSChristoph Hellwig if (!(flags & MS_RDONLY)) 108697288f2cSChristoph Hellwig mode |= FMODE_WRITE; 108797288f2cSChristoph Hellwig 108897288f2cSChristoph Hellwig error = btrfs_parse_early_options(data, mode, fs_type, 108973f73415SJosef Bacik &subvol_name, &subvol_objectid, 1090e15d0542SXin Zhong &subvol_rootid, &fs_devices); 1091f23c8af8SIlya Dryomov if (error) { 1092f23c8af8SIlya Dryomov kfree(subvol_name); 1093061dbc6bSAl Viro return ERR_PTR(error); 1094f23c8af8SIlya Dryomov } 1095edf24abeSChristoph Hellwig 1096830c4adbSJosef Bacik if (subvol_name) { 1097830c4adbSJosef Bacik root = mount_subvol(subvol_name, flags, device_name, data); 1098830c4adbSJosef Bacik kfree(subvol_name); 1099830c4adbSJosef Bacik return root; 1100830c4adbSJosef Bacik } 1101830c4adbSJosef Bacik 1102306e16ceSDavid Sterba error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices); 11038a4b83ccSChris Mason if (error) 1104830c4adbSJosef Bacik return ERR_PTR(error); 11054b82d6e4SYan 1106450ba0eaSJosef Bacik /* 1107450ba0eaSJosef Bacik * Setup a dummy root and fs_info for test/set super. This is because 1108450ba0eaSJosef Bacik * we don't actually fill this stuff out until open_ctree, but we need 1109450ba0eaSJosef Bacik * it for searching for existing supers, so this lets us do that and 1110450ba0eaSJosef Bacik * then open_ctree will properly initialize everything later. 1111450ba0eaSJosef Bacik */ 1112450ba0eaSJosef Bacik fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS); 111304d21a24SIlya Dryomov if (!fs_info) 111404d21a24SIlya Dryomov return ERR_PTR(-ENOMEM); 111504d21a24SIlya Dryomov 1116450ba0eaSJosef Bacik fs_info->fs_devices = fs_devices; 1117450ba0eaSJosef Bacik 11186c41761fSDavid Sterba fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 11196c41761fSDavid Sterba fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 11206c41761fSDavid Sterba if (!fs_info->super_copy || !fs_info->super_for_commit) { 11216c41761fSDavid Sterba error = -ENOMEM; 112204d21a24SIlya Dryomov goto error_fs_info; 112304d21a24SIlya Dryomov } 112404d21a24SIlya Dryomov 112504d21a24SIlya Dryomov error = btrfs_open_devices(fs_devices, mode, fs_type); 112604d21a24SIlya Dryomov if (error) 112704d21a24SIlya Dryomov goto error_fs_info; 112804d21a24SIlya Dryomov 112904d21a24SIlya Dryomov if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) { 113004d21a24SIlya Dryomov error = -EACCES; 11316c41761fSDavid Sterba goto error_close_devices; 11326c41761fSDavid Sterba } 11336c41761fSDavid Sterba 1134dfe25020SChris Mason bdev = fs_devices->latest_bdev; 11359249e17fSDavid Howells s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC, 11369249e17fSDavid Howells fs_info); 1137830c4adbSJosef Bacik if (IS_ERR(s)) { 1138830c4adbSJosef Bacik error = PTR_ERR(s); 1139830c4adbSJosef Bacik goto error_close_devices; 1140830c4adbSJosef Bacik } 11414b82d6e4SYan 11424b82d6e4SYan if (s->s_root) { 11432b82032cSYan Zheng btrfs_close_devices(fs_devices); 11446c41761fSDavid Sterba free_fs_info(fs_info); 114559553edfSAl Viro if ((flags ^ s->s_flags) & MS_RDONLY) 114659553edfSAl Viro error = -EBUSY; 11474b82d6e4SYan } else { 11484b82d6e4SYan char b[BDEVNAME_SIZE]; 11494b82d6e4SYan 11504b82d6e4SYan strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); 1151815745cfSAl Viro btrfs_sb(s)->bdev_holder = fs_type; 11528a4b83ccSChris Mason error = btrfs_fill_super(s, fs_devices, data, 11538a4b83ccSChris Mason flags & MS_SILENT ? 1 : 0); 11544b82d6e4SYan } 11554b82d6e4SYan 115659553edfSAl Viro root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error); 115759553edfSAl Viro if (IS_ERR(root)) 1158e15d0542SXin Zhong deactivate_locked_super(s); 11594b82d6e4SYan 1160061dbc6bSAl Viro return root; 11614b82d6e4SYan 1162c146afadSYan Zheng error_close_devices: 11638a4b83ccSChris Mason btrfs_close_devices(fs_devices); 116404d21a24SIlya Dryomov error_fs_info: 11656c41761fSDavid Sterba free_fs_info(fs_info); 1166061dbc6bSAl Viro return ERR_PTR(error); 11674b82d6e4SYan } 11682e635a27SChris Mason 11690d2450abSSergei Trofimovich static void btrfs_set_max_workers(struct btrfs_workers *workers, int new_limit) 11700d2450abSSergei Trofimovich { 11710d2450abSSergei Trofimovich spin_lock_irq(&workers->lock); 11720d2450abSSergei Trofimovich workers->max_workers = new_limit; 11730d2450abSSergei Trofimovich spin_unlock_irq(&workers->lock); 11740d2450abSSergei Trofimovich } 11750d2450abSSergei Trofimovich 11760d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 11770d2450abSSergei Trofimovich int new_pool_size, int old_pool_size) 11780d2450abSSergei Trofimovich { 11790d2450abSSergei Trofimovich if (new_pool_size == old_pool_size) 11800d2450abSSergei Trofimovich return; 11810d2450abSSergei Trofimovich 11820d2450abSSergei Trofimovich fs_info->thread_pool_size = new_pool_size; 11830d2450abSSergei Trofimovich 11840d2450abSSergei Trofimovich printk(KERN_INFO "btrfs: resize thread pool %d -> %d\n", 11850d2450abSSergei Trofimovich old_pool_size, new_pool_size); 11860d2450abSSergei Trofimovich 11870d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->generic_worker, new_pool_size); 11880d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->workers, new_pool_size); 11890d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->delalloc_workers, new_pool_size); 11900d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->submit_workers, new_pool_size); 11910d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->caching_workers, new_pool_size); 11920d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->fixup_workers, new_pool_size); 11930d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_workers, new_pool_size); 11940d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_meta_workers, new_pool_size); 11950d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_meta_write_workers, new_pool_size); 11960d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_write_workers, new_pool_size); 11970d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_freespace_worker, new_pool_size); 11980d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->delayed_workers, new_pool_size); 11990d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->readahead_workers, new_pool_size); 1200ff023aacSStefan Behrens btrfs_set_max_workers(&fs_info->scrub_wr_completion_workers, 1201ff023aacSStefan Behrens new_pool_size); 12020d2450abSSergei Trofimovich } 12030d2450abSSergei Trofimovich 1204c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1205c146afadSYan Zheng { 1206815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1207815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 120849b25e05SJeff Mahoney unsigned old_flags = sb->s_flags; 120949b25e05SJeff Mahoney unsigned long old_opts = fs_info->mount_opt; 121049b25e05SJeff Mahoney unsigned long old_compress_type = fs_info->compress_type; 121149b25e05SJeff Mahoney u64 old_max_inline = fs_info->max_inline; 121249b25e05SJeff Mahoney u64 old_alloc_start = fs_info->alloc_start; 121349b25e05SJeff Mahoney int old_thread_pool_size = fs_info->thread_pool_size; 121449b25e05SJeff Mahoney unsigned int old_metadata_ratio = fs_info->metadata_ratio; 1215c146afadSYan Zheng int ret; 1216c146afadSYan Zheng 1217b288052eSChris Mason ret = btrfs_parse_options(root, data); 121849b25e05SJeff Mahoney if (ret) { 121949b25e05SJeff Mahoney ret = -EINVAL; 122049b25e05SJeff Mahoney goto restore; 122149b25e05SJeff Mahoney } 1222b288052eSChris Mason 12230d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 12240d2450abSSergei Trofimovich fs_info->thread_pool_size, old_thread_pool_size); 12250d2450abSSergei Trofimovich 1226c146afadSYan Zheng if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 1227c146afadSYan Zheng return 0; 1228c146afadSYan Zheng 1229c146afadSYan Zheng if (*flags & MS_RDONLY) { 12308dabb742SStefan Behrens /* 12318dabb742SStefan Behrens * this also happens on 'umount -rf' or on shutdown, when 12328dabb742SStefan Behrens * the filesystem is busy. 12338dabb742SStefan Behrens */ 1234c146afadSYan Zheng sb->s_flags |= MS_RDONLY; 1235c146afadSYan Zheng 12368dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 12378dabb742SStefan Behrens btrfs_scrub_cancel(fs_info); 12388dabb742SStefan Behrens 1239c146afadSYan Zheng ret = btrfs_commit_super(root); 124049b25e05SJeff Mahoney if (ret) 124149b25e05SJeff Mahoney goto restore; 1242c146afadSYan Zheng } else { 12438a3db184SSergei Trofimovich if (fs_info->fs_devices->rw_devices == 0) { 124449b25e05SJeff Mahoney ret = -EACCES; 124549b25e05SJeff Mahoney goto restore; 12468a3db184SSergei Trofimovich } 12472b82032cSYan Zheng 1248292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 1249292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 1250292fd7fcSStefan Behrens !(*flags & MS_RDONLY)) { 1251292fd7fcSStefan Behrens printk(KERN_WARNING 1252292fd7fcSStefan Behrens "Btrfs: too many missing devices, writeable remount is not allowed\n"); 1253292fd7fcSStefan Behrens ret = -EACCES; 1254292fd7fcSStefan Behrens goto restore; 1255292fd7fcSStefan Behrens } 1256292fd7fcSStefan Behrens 12578a3db184SSergei Trofimovich if (btrfs_super_log_root(fs_info->super_copy) != 0) { 125849b25e05SJeff Mahoney ret = -EINVAL; 125949b25e05SJeff Mahoney goto restore; 12608a3db184SSergei Trofimovich } 1261c146afadSYan Zheng 1262815745cfSAl Viro ret = btrfs_cleanup_fs_roots(fs_info); 126349b25e05SJeff Mahoney if (ret) 126449b25e05SJeff Mahoney goto restore; 1265c146afadSYan Zheng 1266d68fc57bSYan, Zheng /* recover relocation */ 1267d68fc57bSYan, Zheng ret = btrfs_recover_relocation(root); 126849b25e05SJeff Mahoney if (ret) 126949b25e05SJeff Mahoney goto restore; 1270c146afadSYan Zheng 12712b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 12722b6ba629SIlya Dryomov if (ret) 12732b6ba629SIlya Dryomov goto restore; 12742b6ba629SIlya Dryomov 12758dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 12768dabb742SStefan Behrens if (ret) { 12778dabb742SStefan Behrens pr_warn("btrfs: failed to resume dev_replace\n"); 12788dabb742SStefan Behrens goto restore; 12798dabb742SStefan Behrens } 1280c146afadSYan Zheng sb->s_flags &= ~MS_RDONLY; 1281c146afadSYan Zheng } 1282c146afadSYan Zheng 1283c146afadSYan Zheng return 0; 128449b25e05SJeff Mahoney 128549b25e05SJeff Mahoney restore: 128649b25e05SJeff Mahoney /* We've hit an error - don't reset MS_RDONLY */ 128749b25e05SJeff Mahoney if (sb->s_flags & MS_RDONLY) 128849b25e05SJeff Mahoney old_flags |= MS_RDONLY; 128949b25e05SJeff Mahoney sb->s_flags = old_flags; 129049b25e05SJeff Mahoney fs_info->mount_opt = old_opts; 129149b25e05SJeff Mahoney fs_info->compress_type = old_compress_type; 129249b25e05SJeff Mahoney fs_info->max_inline = old_max_inline; 1293c018daecSMiao Xie mutex_lock(&fs_info->chunk_mutex); 129449b25e05SJeff Mahoney fs_info->alloc_start = old_alloc_start; 1295c018daecSMiao Xie mutex_unlock(&fs_info->chunk_mutex); 12960d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 12970d2450abSSergei Trofimovich old_thread_pool_size, fs_info->thread_pool_size); 129849b25e05SJeff Mahoney fs_info->metadata_ratio = old_metadata_ratio; 129949b25e05SJeff Mahoney return ret; 1300c146afadSYan Zheng } 1301c146afadSYan Zheng 1302bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 1303bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1, 1304bcd53741SArne Jansen const void *dev_info2) 1305bcd53741SArne Jansen { 1306bcd53741SArne Jansen if (((struct btrfs_device_info *)dev_info1)->max_avail > 1307bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1308bcd53741SArne Jansen return -1; 1309bcd53741SArne Jansen else if (((struct btrfs_device_info *)dev_info1)->max_avail < 1310bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1311bcd53741SArne Jansen return 1; 1312bcd53741SArne Jansen else 1313bcd53741SArne Jansen return 0; 1314bcd53741SArne Jansen } 1315bcd53741SArne Jansen 1316bcd53741SArne Jansen /* 1317bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 1318bcd53741SArne Jansen * is stored.(Descending Sort) 1319bcd53741SArne Jansen */ 1320bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 1321bcd53741SArne Jansen struct btrfs_device_info *devices, 1322bcd53741SArne Jansen size_t nr_devices) 1323bcd53741SArne Jansen { 1324bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 1325bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 1326bcd53741SArne Jansen } 1327bcd53741SArne Jansen 13286d07bcecSMiao Xie /* 13296d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 13306d07bcecSMiao Xie * file data. 13316d07bcecSMiao Xie */ 13326d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes) 13336d07bcecSMiao Xie { 13346d07bcecSMiao Xie struct btrfs_fs_info *fs_info = root->fs_info; 13356d07bcecSMiao Xie struct btrfs_device_info *devices_info; 13366d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 13376d07bcecSMiao Xie struct btrfs_device *device; 13386d07bcecSMiao Xie u64 skip_space; 13396d07bcecSMiao Xie u64 type; 13406d07bcecSMiao Xie u64 avail_space; 13416d07bcecSMiao Xie u64 used_space; 13426d07bcecSMiao Xie u64 min_stripe_size; 134339fb26c3SMiao Xie int min_stripes = 1, num_stripes = 1; 13446d07bcecSMiao Xie int i = 0, nr_devices; 13456d07bcecSMiao Xie int ret; 13466d07bcecSMiao Xie 1347b772a86eSLi Zefan nr_devices = fs_info->fs_devices->open_devices; 13486d07bcecSMiao Xie BUG_ON(!nr_devices); 13496d07bcecSMiao Xie 13506d07bcecSMiao Xie devices_info = kmalloc(sizeof(*devices_info) * nr_devices, 13516d07bcecSMiao Xie GFP_NOFS); 13526d07bcecSMiao Xie if (!devices_info) 13536d07bcecSMiao Xie return -ENOMEM; 13546d07bcecSMiao Xie 13556d07bcecSMiao Xie /* calc min stripe number for data space alloction */ 13566d07bcecSMiao Xie type = btrfs_get_alloc_profile(root, 1); 135739fb26c3SMiao Xie if (type & BTRFS_BLOCK_GROUP_RAID0) { 13586d07bcecSMiao Xie min_stripes = 2; 135939fb26c3SMiao Xie num_stripes = nr_devices; 136039fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID1) { 13616d07bcecSMiao Xie min_stripes = 2; 136239fb26c3SMiao Xie num_stripes = 2; 136339fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID10) { 13646d07bcecSMiao Xie min_stripes = 4; 136539fb26c3SMiao Xie num_stripes = 4; 136639fb26c3SMiao Xie } 13676d07bcecSMiao Xie 13686d07bcecSMiao Xie if (type & BTRFS_BLOCK_GROUP_DUP) 13696d07bcecSMiao Xie min_stripe_size = 2 * BTRFS_STRIPE_LEN; 13706d07bcecSMiao Xie else 13716d07bcecSMiao Xie min_stripe_size = BTRFS_STRIPE_LEN; 13726d07bcecSMiao Xie 1373b772a86eSLi Zefan list_for_each_entry(device, &fs_devices->devices, dev_list) { 137463a212abSStefan Behrens if (!device->in_fs_metadata || !device->bdev || 137563a212abSStefan Behrens device->is_tgtdev_for_dev_replace) 13766d07bcecSMiao Xie continue; 13776d07bcecSMiao Xie 13786d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 13796d07bcecSMiao Xie 13806d07bcecSMiao Xie /* align with stripe_len */ 13816d07bcecSMiao Xie do_div(avail_space, BTRFS_STRIPE_LEN); 13826d07bcecSMiao Xie avail_space *= BTRFS_STRIPE_LEN; 13836d07bcecSMiao Xie 13846d07bcecSMiao Xie /* 13856d07bcecSMiao Xie * In order to avoid overwritting the superblock on the drive, 13866d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 13876d07bcecSMiao Xie * allocation. 13886d07bcecSMiao Xie */ 13896d07bcecSMiao Xie skip_space = 1024 * 1024; 13906d07bcecSMiao Xie 13916d07bcecSMiao Xie /* user can set the offset in fs_info->alloc_start. */ 13926d07bcecSMiao Xie if (fs_info->alloc_start + BTRFS_STRIPE_LEN <= 13936d07bcecSMiao Xie device->total_bytes) 13946d07bcecSMiao Xie skip_space = max(fs_info->alloc_start, skip_space); 13956d07bcecSMiao Xie 13966d07bcecSMiao Xie /* 13976d07bcecSMiao Xie * btrfs can not use the free space in [0, skip_space - 1], 13986d07bcecSMiao Xie * we must subtract it from the total. In order to implement 13996d07bcecSMiao Xie * it, we account the used space in this range first. 14006d07bcecSMiao Xie */ 14016d07bcecSMiao Xie ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1, 14026d07bcecSMiao Xie &used_space); 14036d07bcecSMiao Xie if (ret) { 14046d07bcecSMiao Xie kfree(devices_info); 14056d07bcecSMiao Xie return ret; 14066d07bcecSMiao Xie } 14076d07bcecSMiao Xie 14086d07bcecSMiao Xie /* calc the free space in [0, skip_space - 1] */ 14096d07bcecSMiao Xie skip_space -= used_space; 14106d07bcecSMiao Xie 14116d07bcecSMiao Xie /* 14126d07bcecSMiao Xie * we can use the free space in [0, skip_space - 1], subtract 14136d07bcecSMiao Xie * it from the total. 14146d07bcecSMiao Xie */ 14156d07bcecSMiao Xie if (avail_space && avail_space >= skip_space) 14166d07bcecSMiao Xie avail_space -= skip_space; 14176d07bcecSMiao Xie else 14186d07bcecSMiao Xie avail_space = 0; 14196d07bcecSMiao Xie 14206d07bcecSMiao Xie if (avail_space < min_stripe_size) 14216d07bcecSMiao Xie continue; 14226d07bcecSMiao Xie 14236d07bcecSMiao Xie devices_info[i].dev = device; 14246d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 14256d07bcecSMiao Xie 14266d07bcecSMiao Xie i++; 14276d07bcecSMiao Xie } 14286d07bcecSMiao Xie 14296d07bcecSMiao Xie nr_devices = i; 14306d07bcecSMiao Xie 14316d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 14326d07bcecSMiao Xie 14336d07bcecSMiao Xie i = nr_devices - 1; 14346d07bcecSMiao Xie avail_space = 0; 14356d07bcecSMiao Xie while (nr_devices >= min_stripes) { 143639fb26c3SMiao Xie if (num_stripes > nr_devices) 143739fb26c3SMiao Xie num_stripes = nr_devices; 143839fb26c3SMiao Xie 14396d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 14406d07bcecSMiao Xie int j; 14416d07bcecSMiao Xie u64 alloc_size; 14426d07bcecSMiao Xie 144339fb26c3SMiao Xie avail_space += devices_info[i].max_avail * num_stripes; 14446d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 144539fb26c3SMiao Xie for (j = i + 1 - num_stripes; j <= i; j++) 14466d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 14476d07bcecSMiao Xie } 14486d07bcecSMiao Xie i--; 14496d07bcecSMiao Xie nr_devices--; 14506d07bcecSMiao Xie } 14516d07bcecSMiao Xie 14526d07bcecSMiao Xie kfree(devices_info); 14536d07bcecSMiao Xie *free_bytes = avail_space; 14546d07bcecSMiao Xie return 0; 14556d07bcecSMiao Xie } 14566d07bcecSMiao Xie 14578fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 14588fd17795SChris Mason { 1459815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 1460815745cfSAl Viro struct btrfs_super_block *disk_super = fs_info->super_copy; 1461815745cfSAl Viro struct list_head *head = &fs_info->space_info; 1462bd4d1088SJosef Bacik struct btrfs_space_info *found; 1463bd4d1088SJosef Bacik u64 total_used = 0; 14646d07bcecSMiao Xie u64 total_free_data = 0; 1465db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 1466815745cfSAl Viro __be32 *fsid = (__be32 *)fs_info->fsid; 14676d07bcecSMiao Xie int ret; 14688fd17795SChris Mason 14696d07bcecSMiao Xie /* holding chunk_muext to avoid allocating new chunks */ 1470815745cfSAl Viro mutex_lock(&fs_info->chunk_mutex); 1471bd4d1088SJosef Bacik rcu_read_lock(); 147289a55897SJosef Bacik list_for_each_entry_rcu(found, head, list) { 14736d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 14746d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 14756d07bcecSMiao Xie total_free_data -= 14766d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 14776d07bcecSMiao Xie } 14786d07bcecSMiao Xie 1479b742bb82SYan, Zheng total_used += found->disk_used; 148089a55897SJosef Bacik } 1481bd4d1088SJosef Bacik rcu_read_unlock(); 1482bd4d1088SJosef Bacik 14838fd17795SChris Mason buf->f_namelen = BTRFS_NAME_LEN; 1484db94535dSChris Mason buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits; 1485bd4d1088SJosef Bacik buf->f_bfree = buf->f_blocks - (total_used >> bits); 14868fd17795SChris Mason buf->f_bsize = dentry->d_sb->s_blocksize; 14878fd17795SChris Mason buf->f_type = BTRFS_SUPER_MAGIC; 14886d07bcecSMiao Xie buf->f_bavail = total_free_data; 1489815745cfSAl Viro ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data); 14906d07bcecSMiao Xie if (ret) { 1491815745cfSAl Viro mutex_unlock(&fs_info->chunk_mutex); 14926d07bcecSMiao Xie return ret; 14936d07bcecSMiao Xie } 14946d07bcecSMiao Xie buf->f_bavail += total_free_data; 14956d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 1496815745cfSAl Viro mutex_unlock(&fs_info->chunk_mutex); 1497d397712bSChris Mason 14989d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 14999d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 15009d03632eSDavid Woodhouse on a big-endian or little-endian host */ 15019d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 15029d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 150332d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 150432d48fa1SDavid Woodhouse buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32; 150532d48fa1SDavid Woodhouse buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid; 150632d48fa1SDavid Woodhouse 15078fd17795SChris Mason return 0; 15088fd17795SChris Mason } 1509b5133862SChris Mason 1510aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb) 1511aea52e19SAl Viro { 1512815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1513aea52e19SAl Viro kill_anon_super(sb); 1514aea52e19SAl Viro free_fs_info(fs_info); 1515aea52e19SAl Viro } 1516aea52e19SAl Viro 15172e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 15182e635a27SChris Mason .owner = THIS_MODULE, 15192e635a27SChris Mason .name = "btrfs", 1520061dbc6bSAl Viro .mount = btrfs_mount, 1521aea52e19SAl Viro .kill_sb = btrfs_kill_super, 15222e635a27SChris Mason .fs_flags = FS_REQUIRES_DEV, 15232e635a27SChris Mason }; 1524a9218f6bSChris Mason 1525d352ac68SChris Mason /* 1526d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 1527d352ac68SChris Mason */ 15288a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 15298a4b83ccSChris Mason unsigned long arg) 15308a4b83ccSChris Mason { 15318a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 15328a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 1533c071fcfdSChris Mason int ret = -ENOTTY; 15348a4b83ccSChris Mason 1535e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1536e441d54dSChris Mason return -EPERM; 1537e441d54dSChris Mason 1538dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 1539dae7b665SLi Zefan if (IS_ERR(vol)) 1540dae7b665SLi Zefan return PTR_ERR(vol); 1541c071fcfdSChris Mason 15428a4b83ccSChris Mason switch (cmd) { 15438a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 154497288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 15458a4b83ccSChris Mason &btrfs_fs_type, &fs_devices); 15468a4b83ccSChris Mason break; 154702db0844SJosef Bacik case BTRFS_IOC_DEVICES_READY: 154802db0844SJosef Bacik ret = btrfs_scan_one_device(vol->name, FMODE_READ, 154902db0844SJosef Bacik &btrfs_fs_type, &fs_devices); 155002db0844SJosef Bacik if (ret) 155102db0844SJosef Bacik break; 155202db0844SJosef Bacik ret = !(fs_devices->num_devices == fs_devices->total_devices); 155302db0844SJosef Bacik break; 15548a4b83ccSChris Mason } 1555dae7b665SLi Zefan 15568a4b83ccSChris Mason kfree(vol); 1557f819d837SLinda Knippers return ret; 15588a4b83ccSChris Mason } 15598a4b83ccSChris Mason 15600176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 1561ed0dab6bSYan { 1562354aa0fbSMiao Xie struct btrfs_trans_handle *trans; 1563354aa0fbSMiao Xie struct btrfs_root *root = btrfs_sb(sb)->tree_root; 1564354aa0fbSMiao Xie 1565354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1566354aa0fbSMiao Xie if (IS_ERR(trans)) { 1567354aa0fbSMiao Xie /* no transaction, don't bother */ 1568354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 15690176260fSLinus Torvalds return 0; 1570354aa0fbSMiao Xie return PTR_ERR(trans); 1571354aa0fbSMiao Xie } 1572354aa0fbSMiao Xie return btrfs_commit_transaction(trans, root); 1573ed0dab6bSYan } 1574ed0dab6bSYan 15750176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb) 1576ed0dab6bSYan { 15770176260fSLinus Torvalds return 0; 1578ed0dab6bSYan } 15792e635a27SChris Mason 15809c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 15819c5085c1SJosef Bacik { 15829c5085c1SJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 15839c5085c1SJosef Bacik struct btrfs_fs_devices *cur_devices; 15849c5085c1SJosef Bacik struct btrfs_device *dev, *first_dev = NULL; 15859c5085c1SJosef Bacik struct list_head *head; 15869c5085c1SJosef Bacik struct rcu_string *name; 15879c5085c1SJosef Bacik 15889c5085c1SJosef Bacik mutex_lock(&fs_info->fs_devices->device_list_mutex); 15899c5085c1SJosef Bacik cur_devices = fs_info->fs_devices; 15909c5085c1SJosef Bacik while (cur_devices) { 15919c5085c1SJosef Bacik head = &cur_devices->devices; 15929c5085c1SJosef Bacik list_for_each_entry(dev, head, dev_list) { 1593aa9ddcd4SJosef Bacik if (dev->missing) 1594aa9ddcd4SJosef Bacik continue; 15959c5085c1SJosef Bacik if (!first_dev || dev->devid < first_dev->devid) 15969c5085c1SJosef Bacik first_dev = dev; 15979c5085c1SJosef Bacik } 15989c5085c1SJosef Bacik cur_devices = cur_devices->seed; 15999c5085c1SJosef Bacik } 16009c5085c1SJosef Bacik 16019c5085c1SJosef Bacik if (first_dev) { 16029c5085c1SJosef Bacik rcu_read_lock(); 16039c5085c1SJosef Bacik name = rcu_dereference(first_dev->name); 16049c5085c1SJosef Bacik seq_escape(m, name->str, " \t\n\\"); 16059c5085c1SJosef Bacik rcu_read_unlock(); 16069c5085c1SJosef Bacik } else { 16079c5085c1SJosef Bacik WARN_ON(1); 16089c5085c1SJosef Bacik } 16099c5085c1SJosef Bacik mutex_unlock(&fs_info->fs_devices->device_list_mutex); 16109c5085c1SJosef Bacik return 0; 16119c5085c1SJosef Bacik } 16129c5085c1SJosef Bacik 1613b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 161476dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 1615bd555975SAl Viro .evict_inode = btrfs_evict_inode, 1616e20d96d6SChris Mason .put_super = btrfs_put_super, 1617d5719762SChris Mason .sync_fs = btrfs_sync_fs, 1618a9572a15SEric Paris .show_options = btrfs_show_options, 16199c5085c1SJosef Bacik .show_devname = btrfs_show_devname, 16204730a4bcSChris Mason .write_inode = btrfs_write_inode, 16212c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 16222c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 16238fd17795SChris Mason .statfs = btrfs_statfs, 1624c146afadSYan Zheng .remount_fs = btrfs_remount, 16250176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 16260176260fSLinus Torvalds .unfreeze_fs = btrfs_unfreeze, 1627e20d96d6SChris Mason }; 1628a9218f6bSChris Mason 1629a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 1630a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 1631a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 1632a9218f6bSChris Mason .owner = THIS_MODULE, 16336038f373SArnd Bergmann .llseek = noop_llseek, 1634a9218f6bSChris Mason }; 1635a9218f6bSChris Mason 1636a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 1637578454ffSKay Sievers .minor = BTRFS_MINOR, 1638a9218f6bSChris Mason .name = "btrfs-control", 1639a9218f6bSChris Mason .fops = &btrfs_ctl_fops 1640a9218f6bSChris Mason }; 1641a9218f6bSChris Mason 1642578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 1643578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 1644578454ffSKay Sievers 1645a9218f6bSChris Mason static int btrfs_interface_init(void) 1646a9218f6bSChris Mason { 1647a9218f6bSChris Mason return misc_register(&btrfs_misc); 1648a9218f6bSChris Mason } 1649a9218f6bSChris Mason 1650b2950863SChristoph Hellwig static void btrfs_interface_exit(void) 1651a9218f6bSChris Mason { 1652a9218f6bSChris Mason if (misc_deregister(&btrfs_misc) < 0) 165348940662SDaniel J Blueman printk(KERN_INFO "btrfs: misc_deregister failed for control device\n"); 1654a9218f6bSChris Mason } 1655a9218f6bSChris Mason 16562e635a27SChris Mason static int __init init_btrfs_fs(void) 16572e635a27SChris Mason { 16582c90e5d6SChris Mason int err; 165958176a96SJosef Bacik 166058176a96SJosef Bacik err = btrfs_init_sysfs(); 166158176a96SJosef Bacik if (err) 166258176a96SJosef Bacik return err; 166358176a96SJosef Bacik 1664143bede5SJeff Mahoney btrfs_init_compress(); 1665d1310b2eSChris Mason 1666261507a0SLi Zefan err = btrfs_init_cachep(); 1667261507a0SLi Zefan if (err) 1668261507a0SLi Zefan goto free_compress; 1669261507a0SLi Zefan 1670d1310b2eSChris Mason err = extent_io_init(); 16712f4cbe64SWyatt Banks if (err) 16722f4cbe64SWyatt Banks goto free_cachep; 16732f4cbe64SWyatt Banks 1674d1310b2eSChris Mason err = extent_map_init(); 1675d1310b2eSChris Mason if (err) 1676d1310b2eSChris Mason goto free_extent_io; 1677d1310b2eSChris Mason 16786352b91dSMiao Xie err = ordered_data_init(); 16792f4cbe64SWyatt Banks if (err) 16802f4cbe64SWyatt Banks goto free_extent_map; 1681c8b97818SChris Mason 16826352b91dSMiao Xie err = btrfs_delayed_inode_init(); 16836352b91dSMiao Xie if (err) 16846352b91dSMiao Xie goto free_ordered_data; 16856352b91dSMiao Xie 16869247f317SMiao Xie err = btrfs_auto_defrag_init(); 168716cdcec7SMiao Xie if (err) 168816cdcec7SMiao Xie goto free_delayed_inode; 168916cdcec7SMiao Xie 169078a6184aSMiao Xie err = btrfs_delayed_ref_init(); 16919247f317SMiao Xie if (err) 16929247f317SMiao Xie goto free_auto_defrag; 16939247f317SMiao Xie 169478a6184aSMiao Xie err = btrfs_interface_init(); 169578a6184aSMiao Xie if (err) 169678a6184aSMiao Xie goto free_delayed_ref; 169778a6184aSMiao Xie 1698a9218f6bSChris Mason err = register_filesystem(&btrfs_fs_type); 1699a9218f6bSChris Mason if (err) 1700a9218f6bSChris Mason goto unregister_ioctl; 1701b3c3da71SChris Mason 1702e565d4b9SJan Schmidt btrfs_init_lockdep(); 1703e565d4b9SJan Schmidt 1704b3c3da71SChris Mason printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION); 17052f4cbe64SWyatt Banks return 0; 17062f4cbe64SWyatt Banks 1707a9218f6bSChris Mason unregister_ioctl: 1708a9218f6bSChris Mason btrfs_interface_exit(); 170978a6184aSMiao Xie free_delayed_ref: 171078a6184aSMiao Xie btrfs_delayed_ref_exit(); 17119247f317SMiao Xie free_auto_defrag: 17129247f317SMiao Xie btrfs_auto_defrag_exit(); 171316cdcec7SMiao Xie free_delayed_inode: 171416cdcec7SMiao Xie btrfs_delayed_inode_exit(); 17156352b91dSMiao Xie free_ordered_data: 17166352b91dSMiao Xie ordered_data_exit(); 17172f4cbe64SWyatt Banks free_extent_map: 17182f4cbe64SWyatt Banks extent_map_exit(); 1719d1310b2eSChris Mason free_extent_io: 1720d1310b2eSChris Mason extent_io_exit(); 17212f4cbe64SWyatt Banks free_cachep: 17222f4cbe64SWyatt Banks btrfs_destroy_cachep(); 1723261507a0SLi Zefan free_compress: 1724261507a0SLi Zefan btrfs_exit_compress(); 17252f4cbe64SWyatt Banks btrfs_exit_sysfs(); 17262c90e5d6SChris Mason return err; 17272e635a27SChris Mason } 17282e635a27SChris Mason 17292e635a27SChris Mason static void __exit exit_btrfs_fs(void) 17302e635a27SChris Mason { 173139279cc3SChris Mason btrfs_destroy_cachep(); 173278a6184aSMiao Xie btrfs_delayed_ref_exit(); 17339247f317SMiao Xie btrfs_auto_defrag_exit(); 173416cdcec7SMiao Xie btrfs_delayed_inode_exit(); 17356352b91dSMiao Xie ordered_data_exit(); 1736a52d9a80SChris Mason extent_map_exit(); 1737d1310b2eSChris Mason extent_io_exit(); 1738a9218f6bSChris Mason btrfs_interface_exit(); 17392e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 174058176a96SJosef Bacik btrfs_exit_sysfs(); 17418a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 1742261507a0SLi Zefan btrfs_exit_compress(); 17432e635a27SChris Mason } 17442e635a27SChris Mason 17452e635a27SChris Mason module_init(init_btrfs_fs) 17462e635a27SChris Mason module_exit(exit_btrfs_fs) 17472e635a27SChris Mason 17482e635a27SChris Mason MODULE_LICENSE("GPL"); 1749