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 66acce952bSliubo static const char *btrfs_decode_error(struct btrfs_fs_info *fs_info, int errno, 67acce952bSliubo char nbuf[16]) 68acce952bSliubo { 69acce952bSliubo char *errstr = NULL; 70acce952bSliubo 71acce952bSliubo switch (errno) { 72acce952bSliubo case -EIO: 73acce952bSliubo errstr = "IO failure"; 74acce952bSliubo break; 75acce952bSliubo case -ENOMEM: 76acce952bSliubo errstr = "Out of memory"; 77acce952bSliubo break; 78acce952bSliubo case -EROFS: 79acce952bSliubo errstr = "Readonly filesystem"; 80acce952bSliubo break; 818c342930SJeff Mahoney case -EEXIST: 828c342930SJeff Mahoney errstr = "Object already exists"; 838c342930SJeff Mahoney break; 84acce952bSliubo default: 85acce952bSliubo if (nbuf) { 86acce952bSliubo if (snprintf(nbuf, 16, "error %d", -errno) >= 0) 87acce952bSliubo errstr = nbuf; 88acce952bSliubo } 89acce952bSliubo break; 90acce952bSliubo } 91acce952bSliubo 92acce952bSliubo return errstr; 93acce952bSliubo } 94acce952bSliubo 95acce952bSliubo static void __save_error_info(struct btrfs_fs_info *fs_info) 96acce952bSliubo { 97acce952bSliubo /* 98acce952bSliubo * today we only save the error info into ram. Long term we'll 99acce952bSliubo * also send it down to the disk 100acce952bSliubo */ 101*87533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 102acce952bSliubo } 103acce952bSliubo 104acce952bSliubo static void save_error_info(struct btrfs_fs_info *fs_info) 105acce952bSliubo { 106acce952bSliubo __save_error_info(fs_info); 107acce952bSliubo } 108acce952bSliubo 109acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */ 110acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info) 111acce952bSliubo { 112acce952bSliubo struct super_block *sb = fs_info->sb; 113acce952bSliubo 114acce952bSliubo if (sb->s_flags & MS_RDONLY) 115acce952bSliubo return; 116acce952bSliubo 117*87533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 118acce952bSliubo sb->s_flags |= MS_RDONLY; 119acce952bSliubo printk(KERN_INFO "btrfs is forced readonly\n"); 1201acd6831SStefan Behrens /* 1211acd6831SStefan Behrens * Note that a running device replace operation is not 1221acd6831SStefan Behrens * canceled here although there is no way to update 1231acd6831SStefan Behrens * the progress. It would add the risk of a deadlock, 1241acd6831SStefan Behrens * therefore the canceling is ommited. The only penalty 1251acd6831SStefan Behrens * is that some I/O remains active until the procedure 1261acd6831SStefan Behrens * completes. The next time when the filesystem is 1271acd6831SStefan Behrens * mounted writeable again, the device replace 1281acd6831SStefan Behrens * operation continues. 1291acd6831SStefan Behrens */ 13049b25e05SJeff Mahoney // WARN_ON(1); 131acce952bSliubo } 132acce952bSliubo } 133acce952bSliubo 134533574c6SJoe Perches #ifdef CONFIG_PRINTK 135acce952bSliubo /* 136acce952bSliubo * __btrfs_std_error decodes expected errors from the caller and 137acce952bSliubo * invokes the approciate error response. 138acce952bSliubo */ 139acce952bSliubo void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function, 1404da35113SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 141acce952bSliubo { 142acce952bSliubo struct super_block *sb = fs_info->sb; 143acce952bSliubo char nbuf[16]; 144acce952bSliubo const char *errstr; 1454da35113SJeff Mahoney va_list args; 1464da35113SJeff Mahoney va_start(args, fmt); 147acce952bSliubo 148acce952bSliubo /* 149acce952bSliubo * Special case: if the error is EROFS, and we're already 150acce952bSliubo * under MS_RDONLY, then it is safe here. 151acce952bSliubo */ 152acce952bSliubo if (errno == -EROFS && (sb->s_flags & MS_RDONLY)) 153acce952bSliubo return; 154acce952bSliubo 155acce952bSliubo errstr = btrfs_decode_error(fs_info, errno, nbuf); 1564da35113SJeff Mahoney if (fmt) { 1574da35113SJeff Mahoney struct va_format vaf = { 1584da35113SJeff Mahoney .fmt = fmt, 1594da35113SJeff Mahoney .va = &args, 1604da35113SJeff Mahoney }; 1614da35113SJeff Mahoney 1624da35113SJeff Mahoney printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: %s (%pV)\n", 1634da35113SJeff Mahoney sb->s_id, function, line, errstr, &vaf); 1644da35113SJeff Mahoney } else { 165acce952bSliubo printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: %s\n", 166acce952bSliubo sb->s_id, function, line, errstr); 1674da35113SJeff Mahoney } 168acce952bSliubo 1694da35113SJeff Mahoney /* Don't go through full error handling during mount */ 1704da35113SJeff Mahoney if (sb->s_flags & MS_BORN) { 1714da35113SJeff Mahoney save_error_info(fs_info); 172acce952bSliubo btrfs_handle_error(fs_info); 173acce952bSliubo } 1744da35113SJeff Mahoney va_end(args); 1754da35113SJeff Mahoney } 1764da35113SJeff Mahoney 177533574c6SJoe Perches static const char * const logtypes[] = { 1784da35113SJeff Mahoney "emergency", 1794da35113SJeff Mahoney "alert", 1804da35113SJeff Mahoney "critical", 1814da35113SJeff Mahoney "error", 1824da35113SJeff Mahoney "warning", 1834da35113SJeff Mahoney "notice", 1844da35113SJeff Mahoney "info", 1854da35113SJeff Mahoney "debug", 1864da35113SJeff Mahoney }; 1874da35113SJeff Mahoney 1884da35113SJeff Mahoney void btrfs_printk(struct btrfs_fs_info *fs_info, const char *fmt, ...) 1894da35113SJeff Mahoney { 1904da35113SJeff Mahoney struct super_block *sb = fs_info->sb; 1914da35113SJeff Mahoney char lvl[4]; 1924da35113SJeff Mahoney struct va_format vaf; 1934da35113SJeff Mahoney va_list args; 1944da35113SJeff Mahoney const char *type = logtypes[4]; 195533574c6SJoe Perches int kern_level; 1964da35113SJeff Mahoney 1974da35113SJeff Mahoney va_start(args, fmt); 1984da35113SJeff Mahoney 199533574c6SJoe Perches kern_level = printk_get_level(fmt); 200533574c6SJoe Perches if (kern_level) { 201533574c6SJoe Perches size_t size = printk_skip_level(fmt) - fmt; 202533574c6SJoe Perches memcpy(lvl, fmt, size); 203533574c6SJoe Perches lvl[size] = '\0'; 204533574c6SJoe Perches fmt += size; 205533574c6SJoe Perches type = logtypes[kern_level - '0']; 2064da35113SJeff Mahoney } else 2074da35113SJeff Mahoney *lvl = '\0'; 2084da35113SJeff Mahoney 2094da35113SJeff Mahoney vaf.fmt = fmt; 2104da35113SJeff Mahoney vaf.va = &args; 211533574c6SJoe Perches 2124da35113SJeff Mahoney printk("%sBTRFS %s (device %s): %pV", lvl, type, sb->s_id, &vaf); 213533574c6SJoe Perches 214533574c6SJoe Perches va_end(args); 2154da35113SJeff Mahoney } 216acce952bSliubo 217533574c6SJoe Perches #else 218533574c6SJoe Perches 219533574c6SJoe Perches void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function, 220533574c6SJoe Perches unsigned int line, int errno, const char *fmt, ...) 221533574c6SJoe Perches { 222533574c6SJoe Perches struct super_block *sb = fs_info->sb; 223533574c6SJoe Perches 224533574c6SJoe Perches /* 225533574c6SJoe Perches * Special case: if the error is EROFS, and we're already 226533574c6SJoe Perches * under MS_RDONLY, then it is safe here. 227533574c6SJoe Perches */ 228533574c6SJoe Perches if (errno == -EROFS && (sb->s_flags & MS_RDONLY)) 229533574c6SJoe Perches return; 230533574c6SJoe Perches 231533574c6SJoe Perches /* Don't go through full error handling during mount */ 232533574c6SJoe Perches if (sb->s_flags & MS_BORN) { 233533574c6SJoe Perches save_error_info(fs_info); 234533574c6SJoe Perches btrfs_handle_error(fs_info); 235533574c6SJoe Perches } 236533574c6SJoe Perches } 237533574c6SJoe Perches #endif 238533574c6SJoe Perches 2398c342930SJeff Mahoney /* 24049b25e05SJeff Mahoney * We only mark the transaction aborted and then set the file system read-only. 24149b25e05SJeff Mahoney * This will prevent new transactions from starting or trying to join this 24249b25e05SJeff Mahoney * one. 24349b25e05SJeff Mahoney * 24449b25e05SJeff Mahoney * This means that error recovery at the call site is limited to freeing 24549b25e05SJeff Mahoney * any local memory allocations and passing the error code up without 24649b25e05SJeff Mahoney * further cleanup. The transaction should complete as it normally would 24749b25e05SJeff Mahoney * in the call path but will return -EIO. 24849b25e05SJeff Mahoney * 24949b25e05SJeff Mahoney * We'll complete the cleanup in btrfs_end_transaction and 25049b25e05SJeff Mahoney * btrfs_commit_transaction. 25149b25e05SJeff Mahoney */ 25249b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans, 25349b25e05SJeff Mahoney struct btrfs_root *root, const char *function, 25449b25e05SJeff Mahoney unsigned int line, int errno) 25549b25e05SJeff Mahoney { 25648940662SDaniel J Blueman WARN_ONCE(1, KERN_DEBUG "btrfs: Transaction aborted\n"); 25749b25e05SJeff Mahoney trans->aborted = errno; 25849b25e05SJeff Mahoney /* Nothing used. The other threads that have joined this 25949b25e05SJeff Mahoney * transaction may be able to continue. */ 26049b25e05SJeff Mahoney if (!trans->blocks_used) { 26169ce977aSMiao Xie char nbuf[16]; 26269ce977aSMiao Xie const char *errstr; 26369ce977aSMiao Xie 26469ce977aSMiao Xie errstr = btrfs_decode_error(root->fs_info, errno, nbuf); 26569ce977aSMiao Xie btrfs_printk(root->fs_info, 26669ce977aSMiao Xie "%s:%d: Aborting unused transaction(%s).\n", 26769ce977aSMiao Xie function, line, errstr); 26849b25e05SJeff Mahoney return; 26949b25e05SJeff Mahoney } 2708d25a086SMiao Xie ACCESS_ONCE(trans->transaction->aborted) = errno; 27149b25e05SJeff Mahoney __btrfs_std_error(root->fs_info, function, line, errno, NULL); 27249b25e05SJeff Mahoney } 27349b25e05SJeff Mahoney /* 2748c342930SJeff Mahoney * __btrfs_panic decodes unexpected, fatal errors from the caller, 2758c342930SJeff Mahoney * issues an alert, and either panics or BUGs, depending on mount options. 2768c342930SJeff Mahoney */ 2778c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, 2788c342930SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 2798c342930SJeff Mahoney { 2808c342930SJeff Mahoney char nbuf[16]; 2818c342930SJeff Mahoney char *s_id = "<unknown>"; 2828c342930SJeff Mahoney const char *errstr; 2838c342930SJeff Mahoney struct va_format vaf = { .fmt = fmt }; 2848c342930SJeff Mahoney va_list args; 2858c342930SJeff Mahoney 2868c342930SJeff Mahoney if (fs_info) 2878c342930SJeff Mahoney s_id = fs_info->sb->s_id; 2888c342930SJeff Mahoney 2898c342930SJeff Mahoney va_start(args, fmt); 2908c342930SJeff Mahoney vaf.va = &args; 2918c342930SJeff Mahoney 2928c342930SJeff Mahoney errstr = btrfs_decode_error(fs_info, errno, nbuf); 2938c342930SJeff Mahoney if (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR) 2948c342930SJeff Mahoney panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (%s)\n", 2958c342930SJeff Mahoney s_id, function, line, &vaf, errstr); 2968c342930SJeff Mahoney 2978c342930SJeff Mahoney printk(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (%s)\n", 2988c342930SJeff Mahoney s_id, function, line, &vaf, errstr); 2998c342930SJeff Mahoney va_end(args); 3008c342930SJeff Mahoney /* Caller calls BUG() */ 3018c342930SJeff Mahoney } 302e20d96d6SChris Mason 303e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 304e20d96d6SChris Mason { 305815745cfSAl Viro (void)close_ctree(btrfs_sb(sb)->tree_root); 306aea52e19SAl Viro /* FIXME: need to fix VFS to return error? */ 307aea52e19SAl Viro /* AV: return it _where_? ->put_super() can be triggered by any number 308aea52e19SAl Viro * of async events, up to and including delivery of SIGKILL to the 309aea52e19SAl Viro * last process that kept it busy. Or segfault in the aforementioned 310aea52e19SAl Viro * process... Whom would you report that to? 311aea52e19SAl Viro */ 312e20d96d6SChris Mason } 3132e635a27SChris Mason 31495e05289SChris Mason enum { 31573f73415SJosef Bacik Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum, 316287a0ab9SJosef Bacik Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, 317287a0ab9SJosef Bacik Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress, 318261507a0SLi Zefan Opt_compress_type, Opt_compress_force, Opt_compress_force_type, 319261507a0SLi Zefan Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard, 32091435650SChris Mason Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed, 3219555c6c1SIlya Dryomov Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache, 3229555c6c1SIlya Dryomov Opt_no_space_cache, Opt_recovery, Opt_skip_balance, 32321adbd5cSStefan Behrens Opt_check_integrity, Opt_check_integrity_including_extent_data, 3248c342930SJeff Mahoney Opt_check_integrity_print_mask, Opt_fatal_errors, 3259555c6c1SIlya Dryomov Opt_err, 32695e05289SChris Mason }; 32795e05289SChris Mason 32895e05289SChris Mason static match_table_t tokens = { 329dfe25020SChris Mason {Opt_degraded, "degraded"}, 33095e05289SChris Mason {Opt_subvol, "subvol=%s"}, 33173f73415SJosef Bacik {Opt_subvolid, "subvolid=%d"}, 33243e570b0SChristoph Hellwig {Opt_device, "device=%s"}, 333b6cda9bcSChris Mason {Opt_nodatasum, "nodatasum"}, 334be20aa9dSChris Mason {Opt_nodatacow, "nodatacow"}, 33521ad10cfSChris Mason {Opt_nobarrier, "nobarrier"}, 3366f568d35SChris Mason {Opt_max_inline, "max_inline=%s"}, 3378f662a76SChris Mason {Opt_alloc_start, "alloc_start=%s"}, 3384543df7eSChris Mason {Opt_thread_pool, "thread_pool=%d"}, 339c8b97818SChris Mason {Opt_compress, "compress"}, 340261507a0SLi Zefan {Opt_compress_type, "compress=%s"}, 341a555f810SChris Mason {Opt_compress_force, "compress-force"}, 342261507a0SLi Zefan {Opt_compress_force_type, "compress-force=%s"}, 343e18e4809SChris Mason {Opt_ssd, "ssd"}, 344451d7585SChris Mason {Opt_ssd_spread, "ssd_spread"}, 3453b30c22fSChris Mason {Opt_nossd, "nossd"}, 34633268eafSJosef Bacik {Opt_noacl, "noacl"}, 3473a5e1404SSage Weil {Opt_notreelog, "notreelog"}, 348dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 34997e728d4SJosef Bacik {Opt_ratio, "metadata_ratio=%d"}, 350e244a0aeSChristoph Hellwig {Opt_discard, "discard"}, 3510af3d00bSJosef Bacik {Opt_space_cache, "space_cache"}, 35288c2ba3bSJosef Bacik {Opt_clear_cache, "clear_cache"}, 3534260f7c7SSage Weil {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, 35491435650SChris Mason {Opt_enospc_debug, "enospc_debug"}, 355e15d0542SXin Zhong {Opt_subvolrootid, "subvolrootid=%d"}, 3564cb5300bSChris Mason {Opt_defrag, "autodefrag"}, 3574b9465cbSChris Mason {Opt_inode_cache, "inode_cache"}, 3588965593eSDavid Sterba {Opt_no_space_cache, "nospace_cache"}, 359af31f5e5SChris Mason {Opt_recovery, "recovery"}, 3609555c6c1SIlya Dryomov {Opt_skip_balance, "skip_balance"}, 36121adbd5cSStefan Behrens {Opt_check_integrity, "check_int"}, 36221adbd5cSStefan Behrens {Opt_check_integrity_including_extent_data, "check_int_data"}, 36321adbd5cSStefan Behrens {Opt_check_integrity_print_mask, "check_int_print_mask=%d"}, 3648c342930SJeff Mahoney {Opt_fatal_errors, "fatal_errors=%s"}, 36533268eafSJosef Bacik {Opt_err, NULL}, 36695e05289SChris Mason }; 36795e05289SChris Mason 368edf24abeSChristoph Hellwig /* 369edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 370edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 37149b25e05SJeff Mahoney * XXX JDM: This needs to be cleaned up for remount. 372edf24abeSChristoph Hellwig */ 373edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options) 37495e05289SChris Mason { 375edf24abeSChristoph Hellwig struct btrfs_fs_info *info = root->fs_info; 37695e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 37773bc1876SJosef Bacik char *p, *num, *orig = NULL; 37873bc1876SJosef Bacik u64 cache_gen; 3794543df7eSChris Mason int intarg; 380a7a3f7caSSage Weil int ret = 0; 381261507a0SLi Zefan char *compress_type; 382261507a0SLi Zefan bool compress_force = false; 383b6cda9bcSChris Mason 3846c41761fSDavid Sterba cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy); 38573bc1876SJosef Bacik if (cache_gen) 38673bc1876SJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 38773bc1876SJosef Bacik 38895e05289SChris Mason if (!options) 38973bc1876SJosef Bacik goto out; 39095e05289SChris Mason 391be20aa9dSChris Mason /* 392be20aa9dSChris Mason * strsep changes the string, duplicate it because parse_options 393be20aa9dSChris Mason * gets called twice 394be20aa9dSChris Mason */ 395be20aa9dSChris Mason options = kstrdup(options, GFP_NOFS); 396be20aa9dSChris Mason if (!options) 397be20aa9dSChris Mason return -ENOMEM; 398be20aa9dSChris Mason 399da495eccSJosef Bacik orig = options; 400be20aa9dSChris Mason 40195e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 40295e05289SChris Mason int token; 40395e05289SChris Mason if (!*p) 40495e05289SChris Mason continue; 40595e05289SChris Mason 40695e05289SChris Mason token = match_token(p, tokens, args); 40795e05289SChris Mason switch (token) { 408dfe25020SChris Mason case Opt_degraded: 409edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: allowing degraded mounts\n"); 410dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 411dfe25020SChris Mason break; 41295e05289SChris Mason case Opt_subvol: 41373f73415SJosef Bacik case Opt_subvolid: 414e15d0542SXin Zhong case Opt_subvolrootid: 41543e570b0SChristoph Hellwig case Opt_device: 416edf24abeSChristoph Hellwig /* 41743e570b0SChristoph Hellwig * These are parsed by btrfs_parse_early_options 418edf24abeSChristoph Hellwig * and can be happily ignored here. 419edf24abeSChristoph Hellwig */ 42095e05289SChris Mason break; 421b6cda9bcSChris Mason case Opt_nodatasum: 422067c28adSChris Mason printk(KERN_INFO "btrfs: setting nodatasum\n"); 423b6cda9bcSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 424be20aa9dSChris Mason break; 425be20aa9dSChris Mason case Opt_nodatacow: 426bedb2ccaSAndrei Popa if (!btrfs_test_opt(root, COMPRESS) || 427bedb2ccaSAndrei Popa !btrfs_test_opt(root, FORCE_COMPRESS)) { 428bedb2ccaSAndrei Popa printk(KERN_INFO "btrfs: setting nodatacow, compression disabled\n"); 429bedb2ccaSAndrei Popa } else { 430edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: setting nodatacow\n"); 431bedb2ccaSAndrei Popa } 432bedb2ccaSAndrei Popa info->compress_type = BTRFS_COMPRESS_NONE; 433bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, COMPRESS); 434bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 435be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 436be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 437b6cda9bcSChris Mason break; 438a555f810SChris Mason case Opt_compress_force: 439261507a0SLi Zefan case Opt_compress_force_type: 440261507a0SLi Zefan compress_force = true; 441261507a0SLi Zefan case Opt_compress: 442261507a0SLi Zefan case Opt_compress_type: 443261507a0SLi Zefan if (token == Opt_compress || 444261507a0SLi Zefan token == Opt_compress_force || 445261507a0SLi Zefan strcmp(args[0].from, "zlib") == 0) { 446261507a0SLi Zefan compress_type = "zlib"; 447261507a0SLi Zefan info->compress_type = BTRFS_COMPRESS_ZLIB; 448063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 449bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 450bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 451a6fa6faeSLi Zefan } else if (strcmp(args[0].from, "lzo") == 0) { 452a6fa6faeSLi Zefan compress_type = "lzo"; 453a6fa6faeSLi Zefan info->compress_type = BTRFS_COMPRESS_LZO; 454063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 455bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 456bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 4572b0ce2c2SMitch Harder btrfs_set_fs_incompat(info, COMPRESS_LZO); 458063849eaSArnd Hannemann } else if (strncmp(args[0].from, "no", 2) == 0) { 459063849eaSArnd Hannemann compress_type = "no"; 460063849eaSArnd Hannemann info->compress_type = BTRFS_COMPRESS_NONE; 461063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, COMPRESS); 462063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 463063849eaSArnd Hannemann compress_force = false; 464261507a0SLi Zefan } else { 465261507a0SLi Zefan ret = -EINVAL; 466261507a0SLi Zefan goto out; 467261507a0SLi Zefan } 468261507a0SLi Zefan 469261507a0SLi Zefan if (compress_force) { 470261507a0SLi Zefan btrfs_set_opt(info->mount_opt, FORCE_COMPRESS); 471261507a0SLi Zefan pr_info("btrfs: force %s compression\n", 472261507a0SLi Zefan compress_type); 473261507a0SLi Zefan } else 474261507a0SLi Zefan pr_info("btrfs: use %s compression\n", 475261507a0SLi Zefan compress_type); 476a555f810SChris Mason break; 477e18e4809SChris Mason case Opt_ssd: 478edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: use ssd allocation scheme\n"); 479e18e4809SChris Mason btrfs_set_opt(info->mount_opt, SSD); 480e18e4809SChris Mason break; 481451d7585SChris Mason case Opt_ssd_spread: 482451d7585SChris Mason printk(KERN_INFO "btrfs: use spread ssd " 483451d7585SChris Mason "allocation scheme\n"); 484451d7585SChris Mason btrfs_set_opt(info->mount_opt, SSD); 485451d7585SChris Mason btrfs_set_opt(info->mount_opt, SSD_SPREAD); 486451d7585SChris Mason break; 4873b30c22fSChris Mason case Opt_nossd: 488451d7585SChris Mason printk(KERN_INFO "btrfs: not using ssd allocation " 489451d7585SChris Mason "scheme\n"); 490c289811cSChris Mason btrfs_set_opt(info->mount_opt, NOSSD); 4913b30c22fSChris Mason btrfs_clear_opt(info->mount_opt, SSD); 492451d7585SChris Mason btrfs_clear_opt(info->mount_opt, SSD_SPREAD); 4933b30c22fSChris Mason break; 49421ad10cfSChris Mason case Opt_nobarrier: 495edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: turning off barriers\n"); 49621ad10cfSChris Mason btrfs_set_opt(info->mount_opt, NOBARRIER); 49721ad10cfSChris Mason break; 4984543df7eSChris Mason case Opt_thread_pool: 4994543df7eSChris Mason intarg = 0; 5004543df7eSChris Mason match_int(&args[0], &intarg); 5010d2450abSSergei Trofimovich if (intarg) 5024543df7eSChris Mason info->thread_pool_size = intarg; 5034543df7eSChris Mason break; 5046f568d35SChris Mason case Opt_max_inline: 505edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5066f568d35SChris Mason if (num) { 50791748467SAkinobu Mita info->max_inline = memparse(num, NULL); 5086f568d35SChris Mason kfree(num); 5096f568d35SChris Mason 51015ada040SChris Mason if (info->max_inline) { 5116f568d35SChris Mason info->max_inline = max_t(u64, 51215ada040SChris Mason info->max_inline, 51315ada040SChris Mason root->sectorsize); 51415ada040SChris Mason } 515edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: max_inline at %llu\n", 51621380931SJoel Becker (unsigned long long)info->max_inline); 5176f568d35SChris Mason } 5186f568d35SChris Mason break; 5198f662a76SChris Mason case Opt_alloc_start: 520edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5218f662a76SChris Mason if (num) { 522c018daecSMiao Xie mutex_lock(&info->chunk_mutex); 52391748467SAkinobu Mita info->alloc_start = memparse(num, NULL); 524c018daecSMiao Xie mutex_unlock(&info->chunk_mutex); 5258f662a76SChris Mason kfree(num); 526edf24abeSChristoph Hellwig printk(KERN_INFO 527edf24abeSChristoph Hellwig "btrfs: allocations start at %llu\n", 52821380931SJoel Becker (unsigned long long)info->alloc_start); 5298f662a76SChris Mason } 5308f662a76SChris Mason break; 53133268eafSJosef Bacik case Opt_noacl: 53233268eafSJosef Bacik root->fs_info->sb->s_flags &= ~MS_POSIXACL; 53333268eafSJosef Bacik break; 5343a5e1404SSage Weil case Opt_notreelog: 5353a5e1404SSage Weil printk(KERN_INFO "btrfs: disabling tree log\n"); 5363a5e1404SSage Weil btrfs_set_opt(info->mount_opt, NOTREELOG); 5373a5e1404SSage Weil break; 538dccae999SSage Weil case Opt_flushoncommit: 539dccae999SSage Weil printk(KERN_INFO "btrfs: turning on flush-on-commit\n"); 540dccae999SSage Weil btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT); 541dccae999SSage Weil break; 54297e728d4SJosef Bacik case Opt_ratio: 54397e728d4SJosef Bacik intarg = 0; 54497e728d4SJosef Bacik match_int(&args[0], &intarg); 54597e728d4SJosef Bacik if (intarg) { 54697e728d4SJosef Bacik info->metadata_ratio = intarg; 54797e728d4SJosef Bacik printk(KERN_INFO "btrfs: metadata ratio %d\n", 54897e728d4SJosef Bacik info->metadata_ratio); 54997e728d4SJosef Bacik } 55097e728d4SJosef Bacik break; 551e244a0aeSChristoph Hellwig case Opt_discard: 552e244a0aeSChristoph Hellwig btrfs_set_opt(info->mount_opt, DISCARD); 553e244a0aeSChristoph Hellwig break; 5540af3d00bSJosef Bacik case Opt_space_cache: 5550af3d00bSJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 5560de90876SJosef Bacik break; 55773bc1876SJosef Bacik case Opt_no_space_cache: 55873bc1876SJosef Bacik printk(KERN_INFO "btrfs: disabling disk space caching\n"); 55973bc1876SJosef Bacik btrfs_clear_opt(info->mount_opt, SPACE_CACHE); 56073bc1876SJosef Bacik break; 5614b9465cbSChris Mason case Opt_inode_cache: 5624b9465cbSChris Mason printk(KERN_INFO "btrfs: enabling inode map caching\n"); 5634b9465cbSChris Mason btrfs_set_opt(info->mount_opt, INODE_MAP_CACHE); 5644b9465cbSChris Mason break; 56588c2ba3bSJosef Bacik case Opt_clear_cache: 56688c2ba3bSJosef Bacik printk(KERN_INFO "btrfs: force clearing of disk cache\n"); 56788c2ba3bSJosef Bacik btrfs_set_opt(info->mount_opt, CLEAR_CACHE); 5680af3d00bSJosef Bacik break; 5694260f7c7SSage Weil case Opt_user_subvol_rm_allowed: 5704260f7c7SSage Weil btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 5714260f7c7SSage Weil break; 57291435650SChris Mason case Opt_enospc_debug: 57391435650SChris Mason btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 57491435650SChris Mason break; 5754cb5300bSChris Mason case Opt_defrag: 57648940662SDaniel J Blueman printk(KERN_INFO "btrfs: enabling auto defrag\n"); 5774cb5300bSChris Mason btrfs_set_opt(info->mount_opt, AUTO_DEFRAG); 5784cb5300bSChris Mason break; 579af31f5e5SChris Mason case Opt_recovery: 58048940662SDaniel J Blueman printk(KERN_INFO "btrfs: enabling auto recovery\n"); 581af31f5e5SChris Mason btrfs_set_opt(info->mount_opt, RECOVERY); 582af31f5e5SChris Mason break; 5839555c6c1SIlya Dryomov case Opt_skip_balance: 5849555c6c1SIlya Dryomov btrfs_set_opt(info->mount_opt, SKIP_BALANCE); 5859555c6c1SIlya Dryomov break; 58621adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 58721adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 58821adbd5cSStefan Behrens printk(KERN_INFO "btrfs: enabling check integrity" 58921adbd5cSStefan Behrens " including extent data\n"); 59021adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, 59121adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA); 59221adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 59321adbd5cSStefan Behrens break; 59421adbd5cSStefan Behrens case Opt_check_integrity: 59521adbd5cSStefan Behrens printk(KERN_INFO "btrfs: enabling check integrity\n"); 59621adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 59721adbd5cSStefan Behrens break; 59821adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 59921adbd5cSStefan Behrens intarg = 0; 60021adbd5cSStefan Behrens match_int(&args[0], &intarg); 60121adbd5cSStefan Behrens if (intarg) { 60221adbd5cSStefan Behrens info->check_integrity_print_mask = intarg; 60321adbd5cSStefan Behrens printk(KERN_INFO "btrfs:" 60421adbd5cSStefan Behrens " check_integrity_print_mask 0x%x\n", 60521adbd5cSStefan Behrens info->check_integrity_print_mask); 60621adbd5cSStefan Behrens } 60721adbd5cSStefan Behrens break; 60821adbd5cSStefan Behrens #else 60921adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 61021adbd5cSStefan Behrens case Opt_check_integrity: 61121adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 61221adbd5cSStefan Behrens printk(KERN_ERR "btrfs: support for check_integrity*" 61321adbd5cSStefan Behrens " not compiled in!\n"); 61421adbd5cSStefan Behrens ret = -EINVAL; 61521adbd5cSStefan Behrens goto out; 61621adbd5cSStefan Behrens #endif 6178c342930SJeff Mahoney case Opt_fatal_errors: 6188c342930SJeff Mahoney if (strcmp(args[0].from, "panic") == 0) 6198c342930SJeff Mahoney btrfs_set_opt(info->mount_opt, 6208c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 6218c342930SJeff Mahoney else if (strcmp(args[0].from, "bug") == 0) 6228c342930SJeff Mahoney btrfs_clear_opt(info->mount_opt, 6238c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 6248c342930SJeff Mahoney else { 6258c342930SJeff Mahoney ret = -EINVAL; 6268c342930SJeff Mahoney goto out; 6278c342930SJeff Mahoney } 6288c342930SJeff Mahoney break; 629a7a3f7caSSage Weil case Opt_err: 630a7a3f7caSSage Weil printk(KERN_INFO "btrfs: unrecognized mount option " 631a7a3f7caSSage Weil "'%s'\n", p); 632a7a3f7caSSage Weil ret = -EINVAL; 633a7a3f7caSSage Weil goto out; 63495e05289SChris Mason default: 635be20aa9dSChris Mason break; 63695e05289SChris Mason } 63795e05289SChris Mason } 638a7a3f7caSSage Weil out: 63973bc1876SJosef Bacik if (!ret && btrfs_test_opt(root, SPACE_CACHE)) 64073bc1876SJosef Bacik printk(KERN_INFO "btrfs: disk space caching is enabled\n"); 641da495eccSJosef Bacik kfree(orig); 642a7a3f7caSSage Weil return ret; 643edf24abeSChristoph Hellwig } 644edf24abeSChristoph Hellwig 645edf24abeSChristoph Hellwig /* 646edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 647edf24abeSChristoph Hellwig * 648edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 649edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 650edf24abeSChristoph Hellwig */ 65197288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 65273f73415SJosef Bacik void *holder, char **subvol_name, u64 *subvol_objectid, 653e15d0542SXin Zhong u64 *subvol_rootid, struct btrfs_fs_devices **fs_devices) 654edf24abeSChristoph Hellwig { 655edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 65683c8c9bdSJeff Liu char *device_name, *opts, *orig, *p; 657edf24abeSChristoph Hellwig int error = 0; 65873f73415SJosef Bacik int intarg; 659edf24abeSChristoph Hellwig 660edf24abeSChristoph Hellwig if (!options) 661830c4adbSJosef Bacik return 0; 662edf24abeSChristoph Hellwig 663edf24abeSChristoph Hellwig /* 664edf24abeSChristoph Hellwig * strsep changes the string, duplicate it because parse_options 665edf24abeSChristoph Hellwig * gets called twice 666edf24abeSChristoph Hellwig */ 667edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 668edf24abeSChristoph Hellwig if (!opts) 669edf24abeSChristoph Hellwig return -ENOMEM; 6703f3d0bc0STero Roponen orig = opts; 671edf24abeSChristoph Hellwig 672edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 673edf24abeSChristoph Hellwig int token; 674edf24abeSChristoph Hellwig if (!*p) 675edf24abeSChristoph Hellwig continue; 676edf24abeSChristoph Hellwig 677edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 678edf24abeSChristoph Hellwig switch (token) { 679edf24abeSChristoph Hellwig case Opt_subvol: 680a90e8b6fSIlya Dryomov kfree(*subvol_name); 681edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 682edf24abeSChristoph Hellwig break; 68373f73415SJosef Bacik case Opt_subvolid: 68473f73415SJosef Bacik intarg = 0; 6854849f01dSJosef Bacik error = match_int(&args[0], &intarg); 6864849f01dSJosef Bacik if (!error) { 6874849f01dSJosef Bacik /* we want the original fs_tree */ 6884849f01dSJosef Bacik if (!intarg) 6894849f01dSJosef Bacik *subvol_objectid = 6904849f01dSJosef Bacik BTRFS_FS_TREE_OBJECTID; 6914849f01dSJosef Bacik else 69273f73415SJosef Bacik *subvol_objectid = intarg; 6934849f01dSJosef Bacik } 69473f73415SJosef Bacik break; 695e15d0542SXin Zhong case Opt_subvolrootid: 696e15d0542SXin Zhong intarg = 0; 697e15d0542SXin Zhong error = match_int(&args[0], &intarg); 698e15d0542SXin Zhong if (!error) { 699e15d0542SXin Zhong /* we want the original fs_tree */ 700e15d0542SXin Zhong if (!intarg) 701e15d0542SXin Zhong *subvol_rootid = 702e15d0542SXin Zhong BTRFS_FS_TREE_OBJECTID; 703e15d0542SXin Zhong else 704e15d0542SXin Zhong *subvol_rootid = intarg; 705e15d0542SXin Zhong } 706e15d0542SXin Zhong break; 70743e570b0SChristoph Hellwig case Opt_device: 70883c8c9bdSJeff Liu device_name = match_strdup(&args[0]); 70983c8c9bdSJeff Liu if (!device_name) { 71083c8c9bdSJeff Liu error = -ENOMEM; 71183c8c9bdSJeff Liu goto out; 71283c8c9bdSJeff Liu } 71383c8c9bdSJeff Liu error = btrfs_scan_one_device(device_name, 71443e570b0SChristoph Hellwig flags, holder, fs_devices); 71583c8c9bdSJeff Liu kfree(device_name); 71643e570b0SChristoph Hellwig if (error) 717830c4adbSJosef Bacik goto out; 71843e570b0SChristoph Hellwig break; 719edf24abeSChristoph Hellwig default: 720edf24abeSChristoph Hellwig break; 721edf24abeSChristoph Hellwig } 722edf24abeSChristoph Hellwig } 723edf24abeSChristoph Hellwig 724edf24abeSChristoph Hellwig out: 725830c4adbSJosef Bacik kfree(orig); 726edf24abeSChristoph Hellwig return error; 72795e05289SChris Mason } 72895e05289SChris Mason 72973f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb, 73073f73415SJosef Bacik u64 subvol_objectid) 73173f73415SJosef Bacik { 732815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 733815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 73473f73415SJosef Bacik struct btrfs_root *new_root; 73573f73415SJosef Bacik struct btrfs_dir_item *di; 73673f73415SJosef Bacik struct btrfs_path *path; 73773f73415SJosef Bacik struct btrfs_key location; 73873f73415SJosef Bacik struct inode *inode; 73973f73415SJosef Bacik u64 dir_id; 74073f73415SJosef Bacik int new = 0; 74173f73415SJosef Bacik 74273f73415SJosef Bacik /* 74373f73415SJosef Bacik * We have a specific subvol we want to mount, just setup location and 74473f73415SJosef Bacik * go look up the root. 74573f73415SJosef Bacik */ 74673f73415SJosef Bacik if (subvol_objectid) { 74773f73415SJosef Bacik location.objectid = subvol_objectid; 74873f73415SJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 74973f73415SJosef Bacik location.offset = (u64)-1; 75073f73415SJosef Bacik goto find_root; 75173f73415SJosef Bacik } 75273f73415SJosef Bacik 75373f73415SJosef Bacik path = btrfs_alloc_path(); 75473f73415SJosef Bacik if (!path) 75573f73415SJosef Bacik return ERR_PTR(-ENOMEM); 75673f73415SJosef Bacik path->leave_spinning = 1; 75773f73415SJosef Bacik 75873f73415SJosef Bacik /* 75973f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 76073f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 76173f73415SJosef Bacik * to mount. 76273f73415SJosef Bacik */ 763815745cfSAl Viro dir_id = btrfs_super_root_dir(fs_info->super_copy); 76473f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 765b0839166SJulia Lawall if (IS_ERR(di)) { 766b0839166SJulia Lawall btrfs_free_path(path); 767fb4f6f91SDan Carpenter return ERR_CAST(di); 768b0839166SJulia Lawall } 76973f73415SJosef Bacik if (!di) { 77073f73415SJosef Bacik /* 77173f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 77273f73415SJosef Bacik * it's always been there, but don't freak out, just try and 77373f73415SJosef Bacik * mount to root most subvolume. 77473f73415SJosef Bacik */ 77573f73415SJosef Bacik btrfs_free_path(path); 77673f73415SJosef Bacik dir_id = BTRFS_FIRST_FREE_OBJECTID; 777815745cfSAl Viro new_root = fs_info->fs_root; 77873f73415SJosef Bacik goto setup_root; 77973f73415SJosef Bacik } 78073f73415SJosef Bacik 78173f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 78273f73415SJosef Bacik btrfs_free_path(path); 78373f73415SJosef Bacik 78473f73415SJosef Bacik find_root: 785815745cfSAl Viro new_root = btrfs_read_fs_root_no_name(fs_info, &location); 78673f73415SJosef Bacik if (IS_ERR(new_root)) 787d0b678cbSJulia Lawall return ERR_CAST(new_root); 78873f73415SJosef Bacik 78973f73415SJosef Bacik if (btrfs_root_refs(&new_root->root_item) == 0) 79073f73415SJosef Bacik return ERR_PTR(-ENOENT); 79173f73415SJosef Bacik 79273f73415SJosef Bacik dir_id = btrfs_root_dirid(&new_root->root_item); 79373f73415SJosef Bacik setup_root: 79473f73415SJosef Bacik location.objectid = dir_id; 79573f73415SJosef Bacik location.type = BTRFS_INODE_ITEM_KEY; 79673f73415SJosef Bacik location.offset = 0; 79773f73415SJosef Bacik 79873f73415SJosef Bacik inode = btrfs_iget(sb, &location, new_root, &new); 7994cbd1149SDan Carpenter if (IS_ERR(inode)) 8004cbd1149SDan Carpenter return ERR_CAST(inode); 80173f73415SJosef Bacik 80273f73415SJosef Bacik /* 80373f73415SJosef Bacik * If we're just mounting the root most subvol put the inode and return 80473f73415SJosef Bacik * a reference to the dentry. We will have already gotten a reference 80573f73415SJosef Bacik * to the inode in btrfs_fill_super so we're good to go. 80673f73415SJosef Bacik */ 80773f73415SJosef Bacik if (!new && sb->s_root->d_inode == inode) { 80873f73415SJosef Bacik iput(inode); 80973f73415SJosef Bacik return dget(sb->s_root); 81073f73415SJosef Bacik } 81173f73415SJosef Bacik 812ba5b8958SJosef Bacik return d_obtain_alias(inode); 81373f73415SJosef Bacik } 81473f73415SJosef Bacik 8158a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 8168a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 8178a4b83ccSChris Mason void *data, int silent) 8182e635a27SChris Mason { 8192e635a27SChris Mason struct inode *inode; 820815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 8215d4f98a2SYan Zheng struct btrfs_key key; 82239279cc3SChris Mason int err; 8232e635a27SChris Mason 8242e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 8252e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 826e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 827af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 828be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 8295103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 8302e635a27SChris Mason sb->s_time_gran = 1; 8310eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 83233268eafSJosef Bacik sb->s_flags |= MS_POSIXACL; 83349cf6f45SChris Ball #endif 8340c4d2d95SJosef Bacik sb->s_flags |= MS_I_VERSION; 835ad2b2c80SAl Viro err = open_ctree(sb, fs_devices, (char *)data); 836ad2b2c80SAl Viro if (err) { 837e20d96d6SChris Mason printk("btrfs: open_ctree failed\n"); 838ad2b2c80SAl Viro return err; 839e20d96d6SChris Mason } 840b888db2bSChris Mason 8415d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 8425d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 8435d4f98a2SYan Zheng key.offset = 0; 84498c7089cSAl Viro inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL); 8455d4f98a2SYan Zheng if (IS_ERR(inode)) { 8465d4f98a2SYan Zheng err = PTR_ERR(inode); 84739279cc3SChris Mason goto fail_close; 84839279cc3SChris Mason } 8492e635a27SChris Mason 85048fde701SAl Viro sb->s_root = d_make_root(inode); 85148fde701SAl Viro if (!sb->s_root) { 85239279cc3SChris Mason err = -ENOMEM; 85339279cc3SChris Mason goto fail_close; 8542e635a27SChris Mason } 85558176a96SJosef Bacik 8566885f308SChris Mason save_mount_options(sb, data); 85790a887c9SDan Magenheimer cleancache_init_fs(sb); 85859553edfSAl Viro sb->s_flags |= MS_ACTIVE; 8592e635a27SChris Mason return 0; 8602e635a27SChris Mason 86139279cc3SChris Mason fail_close: 862815745cfSAl Viro close_ctree(fs_info->tree_root); 863d5719762SChris Mason return err; 864d5719762SChris Mason } 865d5719762SChris Mason 8666bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 867d5719762SChris Mason { 868d5719762SChris Mason struct btrfs_trans_handle *trans; 869815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 870815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 871df2ce34cSChris Mason 8721abe9b8aSliubo trace_btrfs_sync_fs(wait); 8731abe9b8aSliubo 874d561c025SChris Mason if (!wait) { 875815745cfSAl Viro filemap_flush(fs_info->btree_inode->i_mapping); 876df2ce34cSChris Mason return 0; 877d561c025SChris Mason } 878771ed689SChris Mason 8796bbe3a9cSLiu Bo btrfs_wait_ordered_extents(root, 0); 880771ed689SChris Mason 881354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 88260376ce4SJosef Bacik if (IS_ERR(trans)) { 883354aa0fbSMiao Xie /* no transaction, don't bother */ 884354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 885bd7de2c9SJosef Bacik return 0; 88698d5dc13STsutomu Itoh return PTR_ERR(trans); 88760376ce4SJosef Bacik } 888bd7de2c9SJosef Bacik return btrfs_commit_transaction(trans, root); 889d5719762SChris Mason } 890d5719762SChris Mason 89134c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 892a9572a15SEric Paris { 893815745cfSAl Viro struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 894815745cfSAl Viro struct btrfs_root *root = info->tree_root; 895200da64eSTsutomu Itoh char *compress_type; 896a9572a15SEric Paris 897a9572a15SEric Paris if (btrfs_test_opt(root, DEGRADED)) 898a9572a15SEric Paris seq_puts(seq, ",degraded"); 899a9572a15SEric Paris if (btrfs_test_opt(root, NODATASUM)) 900a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 901a9572a15SEric Paris if (btrfs_test_opt(root, NODATACOW)) 902a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 903a9572a15SEric Paris if (btrfs_test_opt(root, NOBARRIER)) 904a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 905a9572a15SEric Paris if (info->max_inline != 8192 * 1024) 90621380931SJoel Becker seq_printf(seq, ",max_inline=%llu", 90721380931SJoel Becker (unsigned long long)info->max_inline); 908a9572a15SEric Paris if (info->alloc_start != 0) 90921380931SJoel Becker seq_printf(seq, ",alloc_start=%llu", 91021380931SJoel Becker (unsigned long long)info->alloc_start); 911a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 912a9572a15SEric Paris num_online_cpus() + 2, 8)) 913a9572a15SEric Paris seq_printf(seq, ",thread_pool=%d", info->thread_pool_size); 914200da64eSTsutomu Itoh if (btrfs_test_opt(root, COMPRESS)) { 915200da64eSTsutomu Itoh if (info->compress_type == BTRFS_COMPRESS_ZLIB) 916200da64eSTsutomu Itoh compress_type = "zlib"; 917200da64eSTsutomu Itoh else 918200da64eSTsutomu Itoh compress_type = "lzo"; 919200da64eSTsutomu Itoh if (btrfs_test_opt(root, FORCE_COMPRESS)) 920200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 921200da64eSTsutomu Itoh else 922200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 923200da64eSTsutomu Itoh } 924c289811cSChris Mason if (btrfs_test_opt(root, NOSSD)) 925c289811cSChris Mason seq_puts(seq, ",nossd"); 926451d7585SChris Mason if (btrfs_test_opt(root, SSD_SPREAD)) 927451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 928451d7585SChris Mason else if (btrfs_test_opt(root, SSD)) 929a9572a15SEric Paris seq_puts(seq, ",ssd"); 9303a5e1404SSage Weil if (btrfs_test_opt(root, NOTREELOG)) 9316b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 932dccae999SSage Weil if (btrfs_test_opt(root, FLUSHONCOMMIT)) 9336b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 93420a5239aSMatthew Wilcox if (btrfs_test_opt(root, DISCARD)) 93520a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 936a9572a15SEric Paris if (!(root->fs_info->sb->s_flags & MS_POSIXACL)) 937a9572a15SEric Paris seq_puts(seq, ",noacl"); 938200da64eSTsutomu Itoh if (btrfs_test_opt(root, SPACE_CACHE)) 939200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 94073bc1876SJosef Bacik else 9418965593eSDavid Sterba seq_puts(seq, ",nospace_cache"); 942200da64eSTsutomu Itoh if (btrfs_test_opt(root, CLEAR_CACHE)) 943200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 944200da64eSTsutomu Itoh if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 945200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 9460942caa3SDavid Sterba if (btrfs_test_opt(root, ENOSPC_DEBUG)) 9470942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 9480942caa3SDavid Sterba if (btrfs_test_opt(root, AUTO_DEFRAG)) 9490942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 9500942caa3SDavid Sterba if (btrfs_test_opt(root, INODE_MAP_CACHE)) 9510942caa3SDavid Sterba seq_puts(seq, ",inode_cache"); 9529555c6c1SIlya Dryomov if (btrfs_test_opt(root, SKIP_BALANCE)) 9539555c6c1SIlya Dryomov seq_puts(seq, ",skip_balance"); 9548c342930SJeff Mahoney if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR)) 9558c342930SJeff Mahoney seq_puts(seq, ",fatal_errors=panic"); 956a9572a15SEric Paris return 0; 957a9572a15SEric Paris } 958a9572a15SEric Paris 959a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 9602e635a27SChris Mason { 961815745cfSAl Viro struct btrfs_fs_info *p = data; 962815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(s); 9634b82d6e4SYan 964815745cfSAl Viro return fs_info->fs_devices == p->fs_devices; 9654b82d6e4SYan } 9664b82d6e4SYan 967450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 968450ba0eaSJosef Bacik { 9696de1d09dSAl Viro int err = set_anon_super(s, data); 9706de1d09dSAl Viro if (!err) 971450ba0eaSJosef Bacik s->s_fs_info = data; 9726de1d09dSAl Viro return err; 973450ba0eaSJosef Bacik } 974450ba0eaSJosef Bacik 975830c4adbSJosef Bacik /* 976f9d9ef62SDavid Sterba * subvolumes are identified by ino 256 977f9d9ef62SDavid Sterba */ 978f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode) 979f9d9ef62SDavid Sterba { 980f9d9ef62SDavid Sterba if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 981f9d9ef62SDavid Sterba return 1; 982f9d9ef62SDavid Sterba return 0; 983f9d9ef62SDavid Sterba } 984f9d9ef62SDavid Sterba 985f9d9ef62SDavid Sterba /* 986830c4adbSJosef Bacik * This will strip out the subvol=%s argument for an argument string and add 987830c4adbSJosef Bacik * subvolid=0 to make sure we get the actual tree root for path walking to the 988830c4adbSJosef Bacik * subvol we want. 989830c4adbSJosef Bacik */ 990830c4adbSJosef Bacik static char *setup_root_args(char *args) 991830c4adbSJosef Bacik { 992f60d16a8SJim Meyering unsigned len = strlen(args) + 2 + 1; 993f60d16a8SJim Meyering char *src, *dst, *buf; 994830c4adbSJosef Bacik 995830c4adbSJosef Bacik /* 996f60d16a8SJim Meyering * We need the same args as before, but with this substitution: 997f60d16a8SJim Meyering * s!subvol=[^,]+!subvolid=0! 998830c4adbSJosef Bacik * 999f60d16a8SJim Meyering * Since the replacement string is up to 2 bytes longer than the 1000f60d16a8SJim Meyering * original, allocate strlen(args) + 2 + 1 bytes. 1001830c4adbSJosef Bacik */ 1002830c4adbSJosef Bacik 1003f60d16a8SJim Meyering src = strstr(args, "subvol="); 1004830c4adbSJosef Bacik /* This shouldn't happen, but just in case.. */ 1005f60d16a8SJim Meyering if (!src) 1006830c4adbSJosef Bacik return NULL; 1007f60d16a8SJim Meyering 1008f60d16a8SJim Meyering buf = dst = kmalloc(len, GFP_NOFS); 1009f60d16a8SJim Meyering if (!buf) 1010f60d16a8SJim Meyering return NULL; 1011830c4adbSJosef Bacik 1012830c4adbSJosef Bacik /* 1013f60d16a8SJim Meyering * If the subvol= arg is not at the start of the string, 1014f60d16a8SJim Meyering * copy whatever precedes it into buf. 1015830c4adbSJosef Bacik */ 1016f60d16a8SJim Meyering if (src != args) { 1017f60d16a8SJim Meyering *src++ = '\0'; 1018f60d16a8SJim Meyering strcpy(buf, args); 1019f60d16a8SJim Meyering dst += strlen(args); 1020830c4adbSJosef Bacik } 1021830c4adbSJosef Bacik 1022f60d16a8SJim Meyering strcpy(dst, "subvolid=0"); 1023f60d16a8SJim Meyering dst += strlen("subvolid=0"); 1024830c4adbSJosef Bacik 1025830c4adbSJosef Bacik /* 1026f60d16a8SJim Meyering * If there is a "," after the original subvol=... string, 1027f60d16a8SJim Meyering * copy that suffix into our buffer. Otherwise, we're done. 1028830c4adbSJosef Bacik */ 1029f60d16a8SJim Meyering src = strchr(src, ','); 1030f60d16a8SJim Meyering if (src) 1031f60d16a8SJim Meyering strcpy(dst, src); 1032830c4adbSJosef Bacik 1033f60d16a8SJim Meyering return buf; 1034830c4adbSJosef Bacik } 1035830c4adbSJosef Bacik 1036830c4adbSJosef Bacik static struct dentry *mount_subvol(const char *subvol_name, int flags, 1037830c4adbSJosef Bacik const char *device_name, char *data) 1038830c4adbSJosef Bacik { 1039830c4adbSJosef Bacik struct dentry *root; 1040830c4adbSJosef Bacik struct vfsmount *mnt; 1041830c4adbSJosef Bacik char *newargs; 1042830c4adbSJosef Bacik 1043830c4adbSJosef Bacik newargs = setup_root_args(data); 1044830c4adbSJosef Bacik if (!newargs) 1045830c4adbSJosef Bacik return ERR_PTR(-ENOMEM); 1046830c4adbSJosef Bacik mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, 1047830c4adbSJosef Bacik newargs); 1048830c4adbSJosef Bacik kfree(newargs); 1049830c4adbSJosef Bacik if (IS_ERR(mnt)) 1050830c4adbSJosef Bacik return ERR_CAST(mnt); 1051830c4adbSJosef Bacik 1052ea441d11SAl Viro root = mount_subtree(mnt, subvol_name); 1053830c4adbSJosef Bacik 1054ea441d11SAl Viro if (!IS_ERR(root) && !is_subvolume_inode(root->d_inode)) { 1055ea441d11SAl Viro struct super_block *s = root->d_sb; 1056ea441d11SAl Viro dput(root); 1057ea441d11SAl Viro root = ERR_PTR(-EINVAL); 1058ea441d11SAl Viro deactivate_locked_super(s); 1059f9d9ef62SDavid Sterba printk(KERN_ERR "btrfs: '%s' is not a valid subvolume\n", 1060f9d9ef62SDavid Sterba subvol_name); 1061f9d9ef62SDavid Sterba } 1062f9d9ef62SDavid Sterba 1063830c4adbSJosef Bacik return root; 1064830c4adbSJosef Bacik } 1065450ba0eaSJosef Bacik 1066edf24abeSChristoph Hellwig /* 1067edf24abeSChristoph Hellwig * Find a superblock for the given device / mount point. 1068edf24abeSChristoph Hellwig * 1069edf24abeSChristoph Hellwig * Note: This is based on get_sb_bdev from fs/super.c with a few additions 1070edf24abeSChristoph Hellwig * for multiple device setup. Make sure to keep it in sync. 1071edf24abeSChristoph Hellwig */ 1072061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1073306e16ceSDavid Sterba const char *device_name, void *data) 10744b82d6e4SYan { 10754b82d6e4SYan struct block_device *bdev = NULL; 10764b82d6e4SYan struct super_block *s; 10774b82d6e4SYan struct dentry *root; 10788a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices = NULL; 1079450ba0eaSJosef Bacik struct btrfs_fs_info *fs_info = NULL; 108097288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 108173f73415SJosef Bacik char *subvol_name = NULL; 108273f73415SJosef Bacik u64 subvol_objectid = 0; 1083e15d0542SXin Zhong u64 subvol_rootid = 0; 10844b82d6e4SYan int error = 0; 10854b82d6e4SYan 108697288f2cSChristoph Hellwig if (!(flags & MS_RDONLY)) 108797288f2cSChristoph Hellwig mode |= FMODE_WRITE; 108897288f2cSChristoph Hellwig 108997288f2cSChristoph Hellwig error = btrfs_parse_early_options(data, mode, fs_type, 109073f73415SJosef Bacik &subvol_name, &subvol_objectid, 1091e15d0542SXin Zhong &subvol_rootid, &fs_devices); 1092f23c8af8SIlya Dryomov if (error) { 1093f23c8af8SIlya Dryomov kfree(subvol_name); 1094061dbc6bSAl Viro return ERR_PTR(error); 1095f23c8af8SIlya Dryomov } 1096edf24abeSChristoph Hellwig 1097830c4adbSJosef Bacik if (subvol_name) { 1098830c4adbSJosef Bacik root = mount_subvol(subvol_name, flags, device_name, data); 1099830c4adbSJosef Bacik kfree(subvol_name); 1100830c4adbSJosef Bacik return root; 1101830c4adbSJosef Bacik } 1102830c4adbSJosef Bacik 1103306e16ceSDavid Sterba error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices); 11048a4b83ccSChris Mason if (error) 1105830c4adbSJosef Bacik return ERR_PTR(error); 11064b82d6e4SYan 1107450ba0eaSJosef Bacik /* 1108450ba0eaSJosef Bacik * Setup a dummy root and fs_info for test/set super. This is because 1109450ba0eaSJosef Bacik * we don't actually fill this stuff out until open_ctree, but we need 1110450ba0eaSJosef Bacik * it for searching for existing supers, so this lets us do that and 1111450ba0eaSJosef Bacik * then open_ctree will properly initialize everything later. 1112450ba0eaSJosef Bacik */ 1113450ba0eaSJosef Bacik fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS); 111404d21a24SIlya Dryomov if (!fs_info) 111504d21a24SIlya Dryomov return ERR_PTR(-ENOMEM); 111604d21a24SIlya Dryomov 1117450ba0eaSJosef Bacik fs_info->fs_devices = fs_devices; 1118450ba0eaSJosef Bacik 11196c41761fSDavid Sterba fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 11206c41761fSDavid Sterba fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 11216c41761fSDavid Sterba if (!fs_info->super_copy || !fs_info->super_for_commit) { 11226c41761fSDavid Sterba error = -ENOMEM; 112304d21a24SIlya Dryomov goto error_fs_info; 112404d21a24SIlya Dryomov } 112504d21a24SIlya Dryomov 112604d21a24SIlya Dryomov error = btrfs_open_devices(fs_devices, mode, fs_type); 112704d21a24SIlya Dryomov if (error) 112804d21a24SIlya Dryomov goto error_fs_info; 112904d21a24SIlya Dryomov 113004d21a24SIlya Dryomov if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) { 113104d21a24SIlya Dryomov error = -EACCES; 11326c41761fSDavid Sterba goto error_close_devices; 11336c41761fSDavid Sterba } 11346c41761fSDavid Sterba 1135dfe25020SChris Mason bdev = fs_devices->latest_bdev; 11369249e17fSDavid Howells s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC, 11379249e17fSDavid Howells fs_info); 1138830c4adbSJosef Bacik if (IS_ERR(s)) { 1139830c4adbSJosef Bacik error = PTR_ERR(s); 1140830c4adbSJosef Bacik goto error_close_devices; 1141830c4adbSJosef Bacik } 11424b82d6e4SYan 11434b82d6e4SYan if (s->s_root) { 11442b82032cSYan Zheng btrfs_close_devices(fs_devices); 11456c41761fSDavid Sterba free_fs_info(fs_info); 114659553edfSAl Viro if ((flags ^ s->s_flags) & MS_RDONLY) 114759553edfSAl Viro error = -EBUSY; 11484b82d6e4SYan } else { 11494b82d6e4SYan char b[BDEVNAME_SIZE]; 11504b82d6e4SYan 11514b82d6e4SYan strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); 1152815745cfSAl Viro btrfs_sb(s)->bdev_holder = fs_type; 11538a4b83ccSChris Mason error = btrfs_fill_super(s, fs_devices, data, 11548a4b83ccSChris Mason flags & MS_SILENT ? 1 : 0); 11554b82d6e4SYan } 11564b82d6e4SYan 115759553edfSAl Viro root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error); 115859553edfSAl Viro if (IS_ERR(root)) 1159e15d0542SXin Zhong deactivate_locked_super(s); 11604b82d6e4SYan 1161061dbc6bSAl Viro return root; 11624b82d6e4SYan 1163c146afadSYan Zheng error_close_devices: 11648a4b83ccSChris Mason btrfs_close_devices(fs_devices); 116504d21a24SIlya Dryomov error_fs_info: 11666c41761fSDavid Sterba free_fs_info(fs_info); 1167061dbc6bSAl Viro return ERR_PTR(error); 11684b82d6e4SYan } 11692e635a27SChris Mason 11700d2450abSSergei Trofimovich static void btrfs_set_max_workers(struct btrfs_workers *workers, int new_limit) 11710d2450abSSergei Trofimovich { 11720d2450abSSergei Trofimovich spin_lock_irq(&workers->lock); 11730d2450abSSergei Trofimovich workers->max_workers = new_limit; 11740d2450abSSergei Trofimovich spin_unlock_irq(&workers->lock); 11750d2450abSSergei Trofimovich } 11760d2450abSSergei Trofimovich 11770d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 11780d2450abSSergei Trofimovich int new_pool_size, int old_pool_size) 11790d2450abSSergei Trofimovich { 11800d2450abSSergei Trofimovich if (new_pool_size == old_pool_size) 11810d2450abSSergei Trofimovich return; 11820d2450abSSergei Trofimovich 11830d2450abSSergei Trofimovich fs_info->thread_pool_size = new_pool_size; 11840d2450abSSergei Trofimovich 11850d2450abSSergei Trofimovich printk(KERN_INFO "btrfs: resize thread pool %d -> %d\n", 11860d2450abSSergei Trofimovich old_pool_size, new_pool_size); 11870d2450abSSergei Trofimovich 11880d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->generic_worker, new_pool_size); 11890d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->workers, new_pool_size); 11900d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->delalloc_workers, new_pool_size); 11910d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->submit_workers, new_pool_size); 11920d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->caching_workers, new_pool_size); 11930d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->fixup_workers, new_pool_size); 11940d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_workers, new_pool_size); 11950d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_meta_workers, new_pool_size); 11960d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_meta_write_workers, new_pool_size); 11970d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_write_workers, new_pool_size); 11980d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_freespace_worker, new_pool_size); 11990d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->delayed_workers, new_pool_size); 12000d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->readahead_workers, new_pool_size); 1201ff023aacSStefan Behrens btrfs_set_max_workers(&fs_info->scrub_wr_completion_workers, 1202ff023aacSStefan Behrens new_pool_size); 12030d2450abSSergei Trofimovich } 12040d2450abSSergei Trofimovich 1205c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1206c146afadSYan Zheng { 1207815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1208815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 120949b25e05SJeff Mahoney unsigned old_flags = sb->s_flags; 121049b25e05SJeff Mahoney unsigned long old_opts = fs_info->mount_opt; 121149b25e05SJeff Mahoney unsigned long old_compress_type = fs_info->compress_type; 121249b25e05SJeff Mahoney u64 old_max_inline = fs_info->max_inline; 121349b25e05SJeff Mahoney u64 old_alloc_start = fs_info->alloc_start; 121449b25e05SJeff Mahoney int old_thread_pool_size = fs_info->thread_pool_size; 121549b25e05SJeff Mahoney unsigned int old_metadata_ratio = fs_info->metadata_ratio; 1216c146afadSYan Zheng int ret; 1217c146afadSYan Zheng 1218b288052eSChris Mason ret = btrfs_parse_options(root, data); 121949b25e05SJeff Mahoney if (ret) { 122049b25e05SJeff Mahoney ret = -EINVAL; 122149b25e05SJeff Mahoney goto restore; 122249b25e05SJeff Mahoney } 1223b288052eSChris Mason 12240d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 12250d2450abSSergei Trofimovich fs_info->thread_pool_size, old_thread_pool_size); 12260d2450abSSergei Trofimovich 1227c146afadSYan Zheng if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 1228c146afadSYan Zheng return 0; 1229c146afadSYan Zheng 1230c146afadSYan Zheng if (*flags & MS_RDONLY) { 12318dabb742SStefan Behrens /* 12328dabb742SStefan Behrens * this also happens on 'umount -rf' or on shutdown, when 12338dabb742SStefan Behrens * the filesystem is busy. 12348dabb742SStefan Behrens */ 1235c146afadSYan Zheng sb->s_flags |= MS_RDONLY; 1236c146afadSYan Zheng 12378dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 12388dabb742SStefan Behrens btrfs_scrub_cancel(fs_info); 12398dabb742SStefan Behrens 1240c146afadSYan Zheng ret = btrfs_commit_super(root); 124149b25e05SJeff Mahoney if (ret) 124249b25e05SJeff Mahoney goto restore; 1243c146afadSYan Zheng } else { 12448a3db184SSergei Trofimovich if (fs_info->fs_devices->rw_devices == 0) { 124549b25e05SJeff Mahoney ret = -EACCES; 124649b25e05SJeff Mahoney goto restore; 12478a3db184SSergei Trofimovich } 12482b82032cSYan Zheng 1249292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 1250292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 1251292fd7fcSStefan Behrens !(*flags & MS_RDONLY)) { 1252292fd7fcSStefan Behrens printk(KERN_WARNING 1253292fd7fcSStefan Behrens "Btrfs: too many missing devices, writeable remount is not allowed\n"); 1254292fd7fcSStefan Behrens ret = -EACCES; 1255292fd7fcSStefan Behrens goto restore; 1256292fd7fcSStefan Behrens } 1257292fd7fcSStefan Behrens 12588a3db184SSergei Trofimovich if (btrfs_super_log_root(fs_info->super_copy) != 0) { 125949b25e05SJeff Mahoney ret = -EINVAL; 126049b25e05SJeff Mahoney goto restore; 12618a3db184SSergei Trofimovich } 1262c146afadSYan Zheng 1263815745cfSAl Viro ret = btrfs_cleanup_fs_roots(fs_info); 126449b25e05SJeff Mahoney if (ret) 126549b25e05SJeff Mahoney goto restore; 1266c146afadSYan Zheng 1267d68fc57bSYan, Zheng /* recover relocation */ 1268d68fc57bSYan, Zheng ret = btrfs_recover_relocation(root); 126949b25e05SJeff Mahoney if (ret) 127049b25e05SJeff Mahoney goto restore; 1271c146afadSYan Zheng 12722b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 12732b6ba629SIlya Dryomov if (ret) 12742b6ba629SIlya Dryomov goto restore; 12752b6ba629SIlya Dryomov 12768dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 12778dabb742SStefan Behrens if (ret) { 12788dabb742SStefan Behrens pr_warn("btrfs: failed to resume dev_replace\n"); 12798dabb742SStefan Behrens goto restore; 12808dabb742SStefan Behrens } 1281c146afadSYan Zheng sb->s_flags &= ~MS_RDONLY; 1282c146afadSYan Zheng } 1283c146afadSYan Zheng 1284c146afadSYan Zheng return 0; 128549b25e05SJeff Mahoney 128649b25e05SJeff Mahoney restore: 128749b25e05SJeff Mahoney /* We've hit an error - don't reset MS_RDONLY */ 128849b25e05SJeff Mahoney if (sb->s_flags & MS_RDONLY) 128949b25e05SJeff Mahoney old_flags |= MS_RDONLY; 129049b25e05SJeff Mahoney sb->s_flags = old_flags; 129149b25e05SJeff Mahoney fs_info->mount_opt = old_opts; 129249b25e05SJeff Mahoney fs_info->compress_type = old_compress_type; 129349b25e05SJeff Mahoney fs_info->max_inline = old_max_inline; 1294c018daecSMiao Xie mutex_lock(&fs_info->chunk_mutex); 129549b25e05SJeff Mahoney fs_info->alloc_start = old_alloc_start; 1296c018daecSMiao Xie mutex_unlock(&fs_info->chunk_mutex); 12970d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 12980d2450abSSergei Trofimovich old_thread_pool_size, fs_info->thread_pool_size); 129949b25e05SJeff Mahoney fs_info->metadata_ratio = old_metadata_ratio; 130049b25e05SJeff Mahoney return ret; 1301c146afadSYan Zheng } 1302c146afadSYan Zheng 1303bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 1304bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1, 1305bcd53741SArne Jansen const void *dev_info2) 1306bcd53741SArne Jansen { 1307bcd53741SArne Jansen if (((struct btrfs_device_info *)dev_info1)->max_avail > 1308bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1309bcd53741SArne Jansen return -1; 1310bcd53741SArne Jansen else if (((struct btrfs_device_info *)dev_info1)->max_avail < 1311bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1312bcd53741SArne Jansen return 1; 1313bcd53741SArne Jansen else 1314bcd53741SArne Jansen return 0; 1315bcd53741SArne Jansen } 1316bcd53741SArne Jansen 1317bcd53741SArne Jansen /* 1318bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 1319bcd53741SArne Jansen * is stored.(Descending Sort) 1320bcd53741SArne Jansen */ 1321bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 1322bcd53741SArne Jansen struct btrfs_device_info *devices, 1323bcd53741SArne Jansen size_t nr_devices) 1324bcd53741SArne Jansen { 1325bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 1326bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 1327bcd53741SArne Jansen } 1328bcd53741SArne Jansen 13296d07bcecSMiao Xie /* 13306d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 13316d07bcecSMiao Xie * file data. 13326d07bcecSMiao Xie */ 13336d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes) 13346d07bcecSMiao Xie { 13356d07bcecSMiao Xie struct btrfs_fs_info *fs_info = root->fs_info; 13366d07bcecSMiao Xie struct btrfs_device_info *devices_info; 13376d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 13386d07bcecSMiao Xie struct btrfs_device *device; 13396d07bcecSMiao Xie u64 skip_space; 13406d07bcecSMiao Xie u64 type; 13416d07bcecSMiao Xie u64 avail_space; 13426d07bcecSMiao Xie u64 used_space; 13436d07bcecSMiao Xie u64 min_stripe_size; 134439fb26c3SMiao Xie int min_stripes = 1, num_stripes = 1; 13456d07bcecSMiao Xie int i = 0, nr_devices; 13466d07bcecSMiao Xie int ret; 13476d07bcecSMiao Xie 1348b772a86eSLi Zefan nr_devices = fs_info->fs_devices->open_devices; 13496d07bcecSMiao Xie BUG_ON(!nr_devices); 13506d07bcecSMiao Xie 13516d07bcecSMiao Xie devices_info = kmalloc(sizeof(*devices_info) * nr_devices, 13526d07bcecSMiao Xie GFP_NOFS); 13536d07bcecSMiao Xie if (!devices_info) 13546d07bcecSMiao Xie return -ENOMEM; 13556d07bcecSMiao Xie 13566d07bcecSMiao Xie /* calc min stripe number for data space alloction */ 13576d07bcecSMiao Xie type = btrfs_get_alloc_profile(root, 1); 135839fb26c3SMiao Xie if (type & BTRFS_BLOCK_GROUP_RAID0) { 13596d07bcecSMiao Xie min_stripes = 2; 136039fb26c3SMiao Xie num_stripes = nr_devices; 136139fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID1) { 13626d07bcecSMiao Xie min_stripes = 2; 136339fb26c3SMiao Xie num_stripes = 2; 136439fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID10) { 13656d07bcecSMiao Xie min_stripes = 4; 136639fb26c3SMiao Xie num_stripes = 4; 136739fb26c3SMiao Xie } 13686d07bcecSMiao Xie 13696d07bcecSMiao Xie if (type & BTRFS_BLOCK_GROUP_DUP) 13706d07bcecSMiao Xie min_stripe_size = 2 * BTRFS_STRIPE_LEN; 13716d07bcecSMiao Xie else 13726d07bcecSMiao Xie min_stripe_size = BTRFS_STRIPE_LEN; 13736d07bcecSMiao Xie 1374b772a86eSLi Zefan list_for_each_entry(device, &fs_devices->devices, dev_list) { 137563a212abSStefan Behrens if (!device->in_fs_metadata || !device->bdev || 137663a212abSStefan Behrens device->is_tgtdev_for_dev_replace) 13776d07bcecSMiao Xie continue; 13786d07bcecSMiao Xie 13796d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 13806d07bcecSMiao Xie 13816d07bcecSMiao Xie /* align with stripe_len */ 13826d07bcecSMiao Xie do_div(avail_space, BTRFS_STRIPE_LEN); 13836d07bcecSMiao Xie avail_space *= BTRFS_STRIPE_LEN; 13846d07bcecSMiao Xie 13856d07bcecSMiao Xie /* 13866d07bcecSMiao Xie * In order to avoid overwritting the superblock on the drive, 13876d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 13886d07bcecSMiao Xie * allocation. 13896d07bcecSMiao Xie */ 13906d07bcecSMiao Xie skip_space = 1024 * 1024; 13916d07bcecSMiao Xie 13926d07bcecSMiao Xie /* user can set the offset in fs_info->alloc_start. */ 13936d07bcecSMiao Xie if (fs_info->alloc_start + BTRFS_STRIPE_LEN <= 13946d07bcecSMiao Xie device->total_bytes) 13956d07bcecSMiao Xie skip_space = max(fs_info->alloc_start, skip_space); 13966d07bcecSMiao Xie 13976d07bcecSMiao Xie /* 13986d07bcecSMiao Xie * btrfs can not use the free space in [0, skip_space - 1], 13996d07bcecSMiao Xie * we must subtract it from the total. In order to implement 14006d07bcecSMiao Xie * it, we account the used space in this range first. 14016d07bcecSMiao Xie */ 14026d07bcecSMiao Xie ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1, 14036d07bcecSMiao Xie &used_space); 14046d07bcecSMiao Xie if (ret) { 14056d07bcecSMiao Xie kfree(devices_info); 14066d07bcecSMiao Xie return ret; 14076d07bcecSMiao Xie } 14086d07bcecSMiao Xie 14096d07bcecSMiao Xie /* calc the free space in [0, skip_space - 1] */ 14106d07bcecSMiao Xie skip_space -= used_space; 14116d07bcecSMiao Xie 14126d07bcecSMiao Xie /* 14136d07bcecSMiao Xie * we can use the free space in [0, skip_space - 1], subtract 14146d07bcecSMiao Xie * it from the total. 14156d07bcecSMiao Xie */ 14166d07bcecSMiao Xie if (avail_space && avail_space >= skip_space) 14176d07bcecSMiao Xie avail_space -= skip_space; 14186d07bcecSMiao Xie else 14196d07bcecSMiao Xie avail_space = 0; 14206d07bcecSMiao Xie 14216d07bcecSMiao Xie if (avail_space < min_stripe_size) 14226d07bcecSMiao Xie continue; 14236d07bcecSMiao Xie 14246d07bcecSMiao Xie devices_info[i].dev = device; 14256d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 14266d07bcecSMiao Xie 14276d07bcecSMiao Xie i++; 14286d07bcecSMiao Xie } 14296d07bcecSMiao Xie 14306d07bcecSMiao Xie nr_devices = i; 14316d07bcecSMiao Xie 14326d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 14336d07bcecSMiao Xie 14346d07bcecSMiao Xie i = nr_devices - 1; 14356d07bcecSMiao Xie avail_space = 0; 14366d07bcecSMiao Xie while (nr_devices >= min_stripes) { 143739fb26c3SMiao Xie if (num_stripes > nr_devices) 143839fb26c3SMiao Xie num_stripes = nr_devices; 143939fb26c3SMiao Xie 14406d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 14416d07bcecSMiao Xie int j; 14426d07bcecSMiao Xie u64 alloc_size; 14436d07bcecSMiao Xie 144439fb26c3SMiao Xie avail_space += devices_info[i].max_avail * num_stripes; 14456d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 144639fb26c3SMiao Xie for (j = i + 1 - num_stripes; j <= i; j++) 14476d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 14486d07bcecSMiao Xie } 14496d07bcecSMiao Xie i--; 14506d07bcecSMiao Xie nr_devices--; 14516d07bcecSMiao Xie } 14526d07bcecSMiao Xie 14536d07bcecSMiao Xie kfree(devices_info); 14546d07bcecSMiao Xie *free_bytes = avail_space; 14556d07bcecSMiao Xie return 0; 14566d07bcecSMiao Xie } 14576d07bcecSMiao Xie 14588fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 14598fd17795SChris Mason { 1460815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 1461815745cfSAl Viro struct btrfs_super_block *disk_super = fs_info->super_copy; 1462815745cfSAl Viro struct list_head *head = &fs_info->space_info; 1463bd4d1088SJosef Bacik struct btrfs_space_info *found; 1464bd4d1088SJosef Bacik u64 total_used = 0; 14656d07bcecSMiao Xie u64 total_free_data = 0; 1466db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 1467815745cfSAl Viro __be32 *fsid = (__be32 *)fs_info->fsid; 14686d07bcecSMiao Xie int ret; 14698fd17795SChris Mason 14706d07bcecSMiao Xie /* holding chunk_muext to avoid allocating new chunks */ 1471815745cfSAl Viro mutex_lock(&fs_info->chunk_mutex); 1472bd4d1088SJosef Bacik rcu_read_lock(); 147389a55897SJosef Bacik list_for_each_entry_rcu(found, head, list) { 14746d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 14756d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 14766d07bcecSMiao Xie total_free_data -= 14776d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 14786d07bcecSMiao Xie } 14796d07bcecSMiao Xie 1480b742bb82SYan, Zheng total_used += found->disk_used; 148189a55897SJosef Bacik } 1482bd4d1088SJosef Bacik rcu_read_unlock(); 1483bd4d1088SJosef Bacik 14848fd17795SChris Mason buf->f_namelen = BTRFS_NAME_LEN; 1485db94535dSChris Mason buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits; 1486bd4d1088SJosef Bacik buf->f_bfree = buf->f_blocks - (total_used >> bits); 14878fd17795SChris Mason buf->f_bsize = dentry->d_sb->s_blocksize; 14888fd17795SChris Mason buf->f_type = BTRFS_SUPER_MAGIC; 14896d07bcecSMiao Xie buf->f_bavail = total_free_data; 1490815745cfSAl Viro ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data); 14916d07bcecSMiao Xie if (ret) { 1492815745cfSAl Viro mutex_unlock(&fs_info->chunk_mutex); 14936d07bcecSMiao Xie return ret; 14946d07bcecSMiao Xie } 14956d07bcecSMiao Xie buf->f_bavail += total_free_data; 14966d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 1497815745cfSAl Viro mutex_unlock(&fs_info->chunk_mutex); 1498d397712bSChris Mason 14999d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 15009d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 15019d03632eSDavid Woodhouse on a big-endian or little-endian host */ 15029d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 15039d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 150432d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 150532d48fa1SDavid Woodhouse buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32; 150632d48fa1SDavid Woodhouse buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid; 150732d48fa1SDavid Woodhouse 15088fd17795SChris Mason return 0; 15098fd17795SChris Mason } 1510b5133862SChris Mason 1511aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb) 1512aea52e19SAl Viro { 1513815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1514aea52e19SAl Viro kill_anon_super(sb); 1515aea52e19SAl Viro free_fs_info(fs_info); 1516aea52e19SAl Viro } 1517aea52e19SAl Viro 15182e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 15192e635a27SChris Mason .owner = THIS_MODULE, 15202e635a27SChris Mason .name = "btrfs", 1521061dbc6bSAl Viro .mount = btrfs_mount, 1522aea52e19SAl Viro .kill_sb = btrfs_kill_super, 15232e635a27SChris Mason .fs_flags = FS_REQUIRES_DEV, 15242e635a27SChris Mason }; 1525a9218f6bSChris Mason 1526d352ac68SChris Mason /* 1527d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 1528d352ac68SChris Mason */ 15298a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 15308a4b83ccSChris Mason unsigned long arg) 15318a4b83ccSChris Mason { 15328a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 15338a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 1534c071fcfdSChris Mason int ret = -ENOTTY; 15358a4b83ccSChris Mason 1536e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1537e441d54dSChris Mason return -EPERM; 1538e441d54dSChris Mason 1539dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 1540dae7b665SLi Zefan if (IS_ERR(vol)) 1541dae7b665SLi Zefan return PTR_ERR(vol); 1542c071fcfdSChris Mason 15438a4b83ccSChris Mason switch (cmd) { 15448a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 154597288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 15468a4b83ccSChris Mason &btrfs_fs_type, &fs_devices); 15478a4b83ccSChris Mason break; 154802db0844SJosef Bacik case BTRFS_IOC_DEVICES_READY: 154902db0844SJosef Bacik ret = btrfs_scan_one_device(vol->name, FMODE_READ, 155002db0844SJosef Bacik &btrfs_fs_type, &fs_devices); 155102db0844SJosef Bacik if (ret) 155202db0844SJosef Bacik break; 155302db0844SJosef Bacik ret = !(fs_devices->num_devices == fs_devices->total_devices); 155402db0844SJosef Bacik break; 15558a4b83ccSChris Mason } 1556dae7b665SLi Zefan 15578a4b83ccSChris Mason kfree(vol); 1558f819d837SLinda Knippers return ret; 15598a4b83ccSChris Mason } 15608a4b83ccSChris Mason 15610176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 1562ed0dab6bSYan { 1563354aa0fbSMiao Xie struct btrfs_trans_handle *trans; 1564354aa0fbSMiao Xie struct btrfs_root *root = btrfs_sb(sb)->tree_root; 1565354aa0fbSMiao Xie 1566354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1567354aa0fbSMiao Xie if (IS_ERR(trans)) { 1568354aa0fbSMiao Xie /* no transaction, don't bother */ 1569354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 15700176260fSLinus Torvalds return 0; 1571354aa0fbSMiao Xie return PTR_ERR(trans); 1572354aa0fbSMiao Xie } 1573354aa0fbSMiao Xie return btrfs_commit_transaction(trans, root); 1574ed0dab6bSYan } 1575ed0dab6bSYan 15760176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb) 1577ed0dab6bSYan { 15780176260fSLinus Torvalds return 0; 1579ed0dab6bSYan } 15802e635a27SChris Mason 15819c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 15829c5085c1SJosef Bacik { 15839c5085c1SJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 15849c5085c1SJosef Bacik struct btrfs_fs_devices *cur_devices; 15859c5085c1SJosef Bacik struct btrfs_device *dev, *first_dev = NULL; 15869c5085c1SJosef Bacik struct list_head *head; 15879c5085c1SJosef Bacik struct rcu_string *name; 15889c5085c1SJosef Bacik 15899c5085c1SJosef Bacik mutex_lock(&fs_info->fs_devices->device_list_mutex); 15909c5085c1SJosef Bacik cur_devices = fs_info->fs_devices; 15919c5085c1SJosef Bacik while (cur_devices) { 15929c5085c1SJosef Bacik head = &cur_devices->devices; 15939c5085c1SJosef Bacik list_for_each_entry(dev, head, dev_list) { 1594aa9ddcd4SJosef Bacik if (dev->missing) 1595aa9ddcd4SJosef Bacik continue; 15969c5085c1SJosef Bacik if (!first_dev || dev->devid < first_dev->devid) 15979c5085c1SJosef Bacik first_dev = dev; 15989c5085c1SJosef Bacik } 15999c5085c1SJosef Bacik cur_devices = cur_devices->seed; 16009c5085c1SJosef Bacik } 16019c5085c1SJosef Bacik 16029c5085c1SJosef Bacik if (first_dev) { 16039c5085c1SJosef Bacik rcu_read_lock(); 16049c5085c1SJosef Bacik name = rcu_dereference(first_dev->name); 16059c5085c1SJosef Bacik seq_escape(m, name->str, " \t\n\\"); 16069c5085c1SJosef Bacik rcu_read_unlock(); 16079c5085c1SJosef Bacik } else { 16089c5085c1SJosef Bacik WARN_ON(1); 16099c5085c1SJosef Bacik } 16109c5085c1SJosef Bacik mutex_unlock(&fs_info->fs_devices->device_list_mutex); 16119c5085c1SJosef Bacik return 0; 16129c5085c1SJosef Bacik } 16139c5085c1SJosef Bacik 1614b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 161576dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 1616bd555975SAl Viro .evict_inode = btrfs_evict_inode, 1617e20d96d6SChris Mason .put_super = btrfs_put_super, 1618d5719762SChris Mason .sync_fs = btrfs_sync_fs, 1619a9572a15SEric Paris .show_options = btrfs_show_options, 16209c5085c1SJosef Bacik .show_devname = btrfs_show_devname, 16214730a4bcSChris Mason .write_inode = btrfs_write_inode, 16222c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 16232c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 16248fd17795SChris Mason .statfs = btrfs_statfs, 1625c146afadSYan Zheng .remount_fs = btrfs_remount, 16260176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 16270176260fSLinus Torvalds .unfreeze_fs = btrfs_unfreeze, 1628e20d96d6SChris Mason }; 1629a9218f6bSChris Mason 1630a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 1631a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 1632a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 1633a9218f6bSChris Mason .owner = THIS_MODULE, 16346038f373SArnd Bergmann .llseek = noop_llseek, 1635a9218f6bSChris Mason }; 1636a9218f6bSChris Mason 1637a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 1638578454ffSKay Sievers .minor = BTRFS_MINOR, 1639a9218f6bSChris Mason .name = "btrfs-control", 1640a9218f6bSChris Mason .fops = &btrfs_ctl_fops 1641a9218f6bSChris Mason }; 1642a9218f6bSChris Mason 1643578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 1644578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 1645578454ffSKay Sievers 1646a9218f6bSChris Mason static int btrfs_interface_init(void) 1647a9218f6bSChris Mason { 1648a9218f6bSChris Mason return misc_register(&btrfs_misc); 1649a9218f6bSChris Mason } 1650a9218f6bSChris Mason 1651b2950863SChristoph Hellwig static void btrfs_interface_exit(void) 1652a9218f6bSChris Mason { 1653a9218f6bSChris Mason if (misc_deregister(&btrfs_misc) < 0) 165448940662SDaniel J Blueman printk(KERN_INFO "btrfs: misc_deregister failed for control device\n"); 1655a9218f6bSChris Mason } 1656a9218f6bSChris Mason 16572e635a27SChris Mason static int __init init_btrfs_fs(void) 16582e635a27SChris Mason { 16592c90e5d6SChris Mason int err; 166058176a96SJosef Bacik 166158176a96SJosef Bacik err = btrfs_init_sysfs(); 166258176a96SJosef Bacik if (err) 166358176a96SJosef Bacik return err; 166458176a96SJosef Bacik 1665143bede5SJeff Mahoney btrfs_init_compress(); 1666d1310b2eSChris Mason 1667261507a0SLi Zefan err = btrfs_init_cachep(); 1668261507a0SLi Zefan if (err) 1669261507a0SLi Zefan goto free_compress; 1670261507a0SLi Zefan 1671d1310b2eSChris Mason err = extent_io_init(); 16722f4cbe64SWyatt Banks if (err) 16732f4cbe64SWyatt Banks goto free_cachep; 16742f4cbe64SWyatt Banks 1675d1310b2eSChris Mason err = extent_map_init(); 1676d1310b2eSChris Mason if (err) 1677d1310b2eSChris Mason goto free_extent_io; 1678d1310b2eSChris Mason 16796352b91dSMiao Xie err = ordered_data_init(); 16802f4cbe64SWyatt Banks if (err) 16812f4cbe64SWyatt Banks goto free_extent_map; 1682c8b97818SChris Mason 16836352b91dSMiao Xie err = btrfs_delayed_inode_init(); 16846352b91dSMiao Xie if (err) 16856352b91dSMiao Xie goto free_ordered_data; 16866352b91dSMiao Xie 16879247f317SMiao Xie err = btrfs_auto_defrag_init(); 168816cdcec7SMiao Xie if (err) 168916cdcec7SMiao Xie goto free_delayed_inode; 169016cdcec7SMiao Xie 169178a6184aSMiao Xie err = btrfs_delayed_ref_init(); 16929247f317SMiao Xie if (err) 16939247f317SMiao Xie goto free_auto_defrag; 16949247f317SMiao Xie 169578a6184aSMiao Xie err = btrfs_interface_init(); 169678a6184aSMiao Xie if (err) 169778a6184aSMiao Xie goto free_delayed_ref; 169878a6184aSMiao Xie 1699a9218f6bSChris Mason err = register_filesystem(&btrfs_fs_type); 1700a9218f6bSChris Mason if (err) 1701a9218f6bSChris Mason goto unregister_ioctl; 1702b3c3da71SChris Mason 1703e565d4b9SJan Schmidt btrfs_init_lockdep(); 1704e565d4b9SJan Schmidt 1705b3c3da71SChris Mason printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION); 17062f4cbe64SWyatt Banks return 0; 17072f4cbe64SWyatt Banks 1708a9218f6bSChris Mason unregister_ioctl: 1709a9218f6bSChris Mason btrfs_interface_exit(); 171078a6184aSMiao Xie free_delayed_ref: 171178a6184aSMiao Xie btrfs_delayed_ref_exit(); 17129247f317SMiao Xie free_auto_defrag: 17139247f317SMiao Xie btrfs_auto_defrag_exit(); 171416cdcec7SMiao Xie free_delayed_inode: 171516cdcec7SMiao Xie btrfs_delayed_inode_exit(); 17166352b91dSMiao Xie free_ordered_data: 17176352b91dSMiao Xie ordered_data_exit(); 17182f4cbe64SWyatt Banks free_extent_map: 17192f4cbe64SWyatt Banks extent_map_exit(); 1720d1310b2eSChris Mason free_extent_io: 1721d1310b2eSChris Mason extent_io_exit(); 17222f4cbe64SWyatt Banks free_cachep: 17232f4cbe64SWyatt Banks btrfs_destroy_cachep(); 1724261507a0SLi Zefan free_compress: 1725261507a0SLi Zefan btrfs_exit_compress(); 17262f4cbe64SWyatt Banks btrfs_exit_sysfs(); 17272c90e5d6SChris Mason return err; 17282e635a27SChris Mason } 17292e635a27SChris Mason 17302e635a27SChris Mason static void __exit exit_btrfs_fs(void) 17312e635a27SChris Mason { 173239279cc3SChris Mason btrfs_destroy_cachep(); 173378a6184aSMiao Xie btrfs_delayed_ref_exit(); 17349247f317SMiao Xie btrfs_auto_defrag_exit(); 173516cdcec7SMiao Xie btrfs_delayed_inode_exit(); 17366352b91dSMiao Xie ordered_data_exit(); 1737a52d9a80SChris Mason extent_map_exit(); 1738d1310b2eSChris Mason extent_io_exit(); 1739a9218f6bSChris Mason btrfs_interface_exit(); 17402e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 174158176a96SJosef Bacik btrfs_exit_sysfs(); 17428a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 1743261507a0SLi Zefan btrfs_exit_compress(); 17442e635a27SChris Mason } 17452e635a27SChris Mason 17462e635a27SChris Mason module_init(init_btrfs_fs) 17472e635a27SChris Mason module_exit(exit_btrfs_fs) 17482e635a27SChris Mason 17492e635a27SChris Mason MODULE_LICENSE("GPL"); 1750