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> 444b4e25f2SChris Mason #include "compat.h" 4516cdcec7SMiao Xie #include "delayed-inode.h" 462e635a27SChris Mason #include "ctree.h" 47e20d96d6SChris Mason #include "disk-io.h" 48d5719762SChris Mason #include "transaction.h" 492c90e5d6SChris Mason #include "btrfs_inode.h" 50c5739bbaSChris Mason #include "ioctl.h" 513a686375SChris Mason #include "print-tree.h" 525103e947SJosef Bacik #include "xattr.h" 538a4b83ccSChris Mason #include "volumes.h" 54b3c3da71SChris Mason #include "version.h" 55be6e8dc0SBalaji Rao #include "export.h" 56c8b97818SChris Mason #include "compression.h" 579c5085c1SJosef Bacik #include "rcu-string.h" 582e635a27SChris Mason 591abe9b8aSliubo #define CREATE_TRACE_POINTS 601abe9b8aSliubo #include <trace/events/btrfs.h> 611abe9b8aSliubo 62b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops; 63830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type; 64e20d96d6SChris Mason 65acce952bSliubo static const char *btrfs_decode_error(struct btrfs_fs_info *fs_info, int errno, 66acce952bSliubo 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 */ 100acce952bSliubo fs_info->fs_state = BTRFS_SUPER_FLAG_ERROR; 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 116acce952bSliubo if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) { 117acce952bSliubo sb->s_flags |= MS_RDONLY; 118acce952bSliubo printk(KERN_INFO "btrfs is forced readonly\n"); 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 154acce952bSliubo errstr = btrfs_decode_error(fs_info, 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 26369ce977aSMiao Xie errstr = btrfs_decode_error(root->fs_info, 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 } 26949b25e05SJeff Mahoney 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 2918c342930SJeff Mahoney errstr = btrfs_decode_error(fs_info, errno, nbuf); 2928c342930SJeff Mahoney if (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) { 52191748467SAkinobu Mita info->alloc_start = memparse(num, NULL); 5228f662a76SChris Mason kfree(num); 523edf24abeSChristoph Hellwig printk(KERN_INFO 524edf24abeSChristoph Hellwig "btrfs: allocations start at %llu\n", 52521380931SJoel Becker (unsigned long long)info->alloc_start); 5268f662a76SChris Mason } 5278f662a76SChris Mason break; 52833268eafSJosef Bacik case Opt_noacl: 52933268eafSJosef Bacik root->fs_info->sb->s_flags &= ~MS_POSIXACL; 53033268eafSJosef Bacik break; 5313a5e1404SSage Weil case Opt_notreelog: 5323a5e1404SSage Weil printk(KERN_INFO "btrfs: disabling tree log\n"); 5333a5e1404SSage Weil btrfs_set_opt(info->mount_opt, NOTREELOG); 5343a5e1404SSage Weil break; 535dccae999SSage Weil case Opt_flushoncommit: 536dccae999SSage Weil printk(KERN_INFO "btrfs: turning on flush-on-commit\n"); 537dccae999SSage Weil btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT); 538dccae999SSage Weil break; 53997e728d4SJosef Bacik case Opt_ratio: 54097e728d4SJosef Bacik intarg = 0; 54197e728d4SJosef Bacik match_int(&args[0], &intarg); 54297e728d4SJosef Bacik if (intarg) { 54397e728d4SJosef Bacik info->metadata_ratio = intarg; 54497e728d4SJosef Bacik printk(KERN_INFO "btrfs: metadata ratio %d\n", 54597e728d4SJosef Bacik info->metadata_ratio); 54697e728d4SJosef Bacik } 54797e728d4SJosef Bacik break; 548e244a0aeSChristoph Hellwig case Opt_discard: 549e244a0aeSChristoph Hellwig btrfs_set_opt(info->mount_opt, DISCARD); 550e244a0aeSChristoph Hellwig break; 5510af3d00bSJosef Bacik case Opt_space_cache: 5520af3d00bSJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 5530de90876SJosef Bacik break; 55473bc1876SJosef Bacik case Opt_no_space_cache: 55573bc1876SJosef Bacik printk(KERN_INFO "btrfs: disabling disk space caching\n"); 55673bc1876SJosef Bacik btrfs_clear_opt(info->mount_opt, SPACE_CACHE); 55773bc1876SJosef Bacik break; 5584b9465cbSChris Mason case Opt_inode_cache: 5594b9465cbSChris Mason printk(KERN_INFO "btrfs: enabling inode map caching\n"); 5604b9465cbSChris Mason btrfs_set_opt(info->mount_opt, INODE_MAP_CACHE); 5614b9465cbSChris Mason break; 56288c2ba3bSJosef Bacik case Opt_clear_cache: 56388c2ba3bSJosef Bacik printk(KERN_INFO "btrfs: force clearing of disk cache\n"); 56488c2ba3bSJosef Bacik btrfs_set_opt(info->mount_opt, CLEAR_CACHE); 5650af3d00bSJosef Bacik break; 5664260f7c7SSage Weil case Opt_user_subvol_rm_allowed: 5674260f7c7SSage Weil btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 5684260f7c7SSage Weil break; 56991435650SChris Mason case Opt_enospc_debug: 57091435650SChris Mason btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 57191435650SChris Mason break; 5724cb5300bSChris Mason case Opt_defrag: 57348940662SDaniel J Blueman printk(KERN_INFO "btrfs: enabling auto defrag\n"); 5744cb5300bSChris Mason btrfs_set_opt(info->mount_opt, AUTO_DEFRAG); 5754cb5300bSChris Mason break; 576af31f5e5SChris Mason case Opt_recovery: 57748940662SDaniel J Blueman printk(KERN_INFO "btrfs: enabling auto recovery\n"); 578af31f5e5SChris Mason btrfs_set_opt(info->mount_opt, RECOVERY); 579af31f5e5SChris Mason break; 5809555c6c1SIlya Dryomov case Opt_skip_balance: 5819555c6c1SIlya Dryomov btrfs_set_opt(info->mount_opt, SKIP_BALANCE); 5829555c6c1SIlya Dryomov break; 58321adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 58421adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 58521adbd5cSStefan Behrens printk(KERN_INFO "btrfs: enabling check integrity" 58621adbd5cSStefan Behrens " including extent data\n"); 58721adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, 58821adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA); 58921adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 59021adbd5cSStefan Behrens break; 59121adbd5cSStefan Behrens case Opt_check_integrity: 59221adbd5cSStefan Behrens printk(KERN_INFO "btrfs: enabling check integrity\n"); 59321adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 59421adbd5cSStefan Behrens break; 59521adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 59621adbd5cSStefan Behrens intarg = 0; 59721adbd5cSStefan Behrens match_int(&args[0], &intarg); 59821adbd5cSStefan Behrens if (intarg) { 59921adbd5cSStefan Behrens info->check_integrity_print_mask = intarg; 60021adbd5cSStefan Behrens printk(KERN_INFO "btrfs:" 60121adbd5cSStefan Behrens " check_integrity_print_mask 0x%x\n", 60221adbd5cSStefan Behrens info->check_integrity_print_mask); 60321adbd5cSStefan Behrens } 60421adbd5cSStefan Behrens break; 60521adbd5cSStefan Behrens #else 60621adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 60721adbd5cSStefan Behrens case Opt_check_integrity: 60821adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 60921adbd5cSStefan Behrens printk(KERN_ERR "btrfs: support for check_integrity*" 61021adbd5cSStefan Behrens " not compiled in!\n"); 61121adbd5cSStefan Behrens ret = -EINVAL; 61221adbd5cSStefan Behrens goto out; 61321adbd5cSStefan Behrens #endif 6148c342930SJeff Mahoney case Opt_fatal_errors: 6158c342930SJeff Mahoney if (strcmp(args[0].from, "panic") == 0) 6168c342930SJeff Mahoney btrfs_set_opt(info->mount_opt, 6178c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 6188c342930SJeff Mahoney else if (strcmp(args[0].from, "bug") == 0) 6198c342930SJeff Mahoney btrfs_clear_opt(info->mount_opt, 6208c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 6218c342930SJeff Mahoney else { 6228c342930SJeff Mahoney ret = -EINVAL; 6238c342930SJeff Mahoney goto out; 6248c342930SJeff Mahoney } 6258c342930SJeff Mahoney break; 626a7a3f7caSSage Weil case Opt_err: 627a7a3f7caSSage Weil printk(KERN_INFO "btrfs: unrecognized mount option " 628a7a3f7caSSage Weil "'%s'\n", p); 629a7a3f7caSSage Weil ret = -EINVAL; 630a7a3f7caSSage Weil goto out; 63195e05289SChris Mason default: 632be20aa9dSChris Mason break; 63395e05289SChris Mason } 63495e05289SChris Mason } 635a7a3f7caSSage Weil out: 63673bc1876SJosef Bacik if (!ret && btrfs_test_opt(root, SPACE_CACHE)) 63773bc1876SJosef Bacik printk(KERN_INFO "btrfs: disk space caching is enabled\n"); 638da495eccSJosef Bacik kfree(orig); 639a7a3f7caSSage Weil return ret; 640edf24abeSChristoph Hellwig } 641edf24abeSChristoph Hellwig 642edf24abeSChristoph Hellwig /* 643edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 644edf24abeSChristoph Hellwig * 645edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 646edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 647edf24abeSChristoph Hellwig */ 64897288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 64973f73415SJosef Bacik void *holder, char **subvol_name, u64 *subvol_objectid, 650e15d0542SXin Zhong u64 *subvol_rootid, struct btrfs_fs_devices **fs_devices) 651edf24abeSChristoph Hellwig { 652edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 65383c8c9bdSJeff Liu char *device_name, *opts, *orig, *p; 654edf24abeSChristoph Hellwig int error = 0; 65573f73415SJosef Bacik int intarg; 656edf24abeSChristoph Hellwig 657edf24abeSChristoph Hellwig if (!options) 658830c4adbSJosef Bacik return 0; 659edf24abeSChristoph Hellwig 660edf24abeSChristoph Hellwig /* 661edf24abeSChristoph Hellwig * strsep changes the string, duplicate it because parse_options 662edf24abeSChristoph Hellwig * gets called twice 663edf24abeSChristoph Hellwig */ 664edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 665edf24abeSChristoph Hellwig if (!opts) 666edf24abeSChristoph Hellwig return -ENOMEM; 6673f3d0bc0STero Roponen orig = opts; 668edf24abeSChristoph Hellwig 669edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 670edf24abeSChristoph Hellwig int token; 671edf24abeSChristoph Hellwig if (!*p) 672edf24abeSChristoph Hellwig continue; 673edf24abeSChristoph Hellwig 674edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 675edf24abeSChristoph Hellwig switch (token) { 676edf24abeSChristoph Hellwig case Opt_subvol: 677a90e8b6fSIlya Dryomov kfree(*subvol_name); 678edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 679edf24abeSChristoph Hellwig break; 68073f73415SJosef Bacik case Opt_subvolid: 68173f73415SJosef Bacik intarg = 0; 6824849f01dSJosef Bacik error = match_int(&args[0], &intarg); 6834849f01dSJosef Bacik if (!error) { 6844849f01dSJosef Bacik /* we want the original fs_tree */ 6854849f01dSJosef Bacik if (!intarg) 6864849f01dSJosef Bacik *subvol_objectid = 6874849f01dSJosef Bacik BTRFS_FS_TREE_OBJECTID; 6884849f01dSJosef Bacik else 68973f73415SJosef Bacik *subvol_objectid = intarg; 6904849f01dSJosef Bacik } 69173f73415SJosef Bacik break; 692e15d0542SXin Zhong case Opt_subvolrootid: 693e15d0542SXin Zhong intarg = 0; 694e15d0542SXin Zhong error = match_int(&args[0], &intarg); 695e15d0542SXin Zhong if (!error) { 696e15d0542SXin Zhong /* we want the original fs_tree */ 697e15d0542SXin Zhong if (!intarg) 698e15d0542SXin Zhong *subvol_rootid = 699e15d0542SXin Zhong BTRFS_FS_TREE_OBJECTID; 700e15d0542SXin Zhong else 701e15d0542SXin Zhong *subvol_rootid = intarg; 702e15d0542SXin Zhong } 703e15d0542SXin Zhong break; 70443e570b0SChristoph Hellwig case Opt_device: 70583c8c9bdSJeff Liu device_name = match_strdup(&args[0]); 70683c8c9bdSJeff Liu if (!device_name) { 70783c8c9bdSJeff Liu error = -ENOMEM; 70883c8c9bdSJeff Liu goto out; 70983c8c9bdSJeff Liu } 71083c8c9bdSJeff Liu error = btrfs_scan_one_device(device_name, 71143e570b0SChristoph Hellwig flags, holder, fs_devices); 71283c8c9bdSJeff Liu kfree(device_name); 71343e570b0SChristoph Hellwig if (error) 714830c4adbSJosef Bacik goto out; 71543e570b0SChristoph Hellwig break; 716edf24abeSChristoph Hellwig default: 717edf24abeSChristoph Hellwig break; 718edf24abeSChristoph Hellwig } 719edf24abeSChristoph Hellwig } 720edf24abeSChristoph Hellwig 721edf24abeSChristoph Hellwig out: 722830c4adbSJosef Bacik kfree(orig); 723edf24abeSChristoph Hellwig return error; 72495e05289SChris Mason } 72595e05289SChris Mason 72673f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb, 72773f73415SJosef Bacik u64 subvol_objectid) 72873f73415SJosef Bacik { 729815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 730815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 73173f73415SJosef Bacik struct btrfs_root *new_root; 73273f73415SJosef Bacik struct btrfs_dir_item *di; 73373f73415SJosef Bacik struct btrfs_path *path; 73473f73415SJosef Bacik struct btrfs_key location; 73573f73415SJosef Bacik struct inode *inode; 73673f73415SJosef Bacik u64 dir_id; 73773f73415SJosef Bacik int new = 0; 73873f73415SJosef Bacik 73973f73415SJosef Bacik /* 74073f73415SJosef Bacik * We have a specific subvol we want to mount, just setup location and 74173f73415SJosef Bacik * go look up the root. 74273f73415SJosef Bacik */ 74373f73415SJosef Bacik if (subvol_objectid) { 74473f73415SJosef Bacik location.objectid = subvol_objectid; 74573f73415SJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 74673f73415SJosef Bacik location.offset = (u64)-1; 74773f73415SJosef Bacik goto find_root; 74873f73415SJosef Bacik } 74973f73415SJosef Bacik 75073f73415SJosef Bacik path = btrfs_alloc_path(); 75173f73415SJosef Bacik if (!path) 75273f73415SJosef Bacik return ERR_PTR(-ENOMEM); 75373f73415SJosef Bacik path->leave_spinning = 1; 75473f73415SJosef Bacik 75573f73415SJosef Bacik /* 75673f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 75773f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 75873f73415SJosef Bacik * to mount. 75973f73415SJosef Bacik */ 760815745cfSAl Viro dir_id = btrfs_super_root_dir(fs_info->super_copy); 76173f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 762b0839166SJulia Lawall if (IS_ERR(di)) { 763b0839166SJulia Lawall btrfs_free_path(path); 764fb4f6f91SDan Carpenter return ERR_CAST(di); 765b0839166SJulia Lawall } 76673f73415SJosef Bacik if (!di) { 76773f73415SJosef Bacik /* 76873f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 76973f73415SJosef Bacik * it's always been there, but don't freak out, just try and 77073f73415SJosef Bacik * mount to root most subvolume. 77173f73415SJosef Bacik */ 77273f73415SJosef Bacik btrfs_free_path(path); 77373f73415SJosef Bacik dir_id = BTRFS_FIRST_FREE_OBJECTID; 774815745cfSAl Viro new_root = fs_info->fs_root; 77573f73415SJosef Bacik goto setup_root; 77673f73415SJosef Bacik } 77773f73415SJosef Bacik 77873f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 77973f73415SJosef Bacik btrfs_free_path(path); 78073f73415SJosef Bacik 78173f73415SJosef Bacik find_root: 782815745cfSAl Viro new_root = btrfs_read_fs_root_no_name(fs_info, &location); 78373f73415SJosef Bacik if (IS_ERR(new_root)) 784d0b678cbSJulia Lawall return ERR_CAST(new_root); 78573f73415SJosef Bacik 78673f73415SJosef Bacik if (btrfs_root_refs(&new_root->root_item) == 0) 78773f73415SJosef Bacik return ERR_PTR(-ENOENT); 78873f73415SJosef Bacik 78973f73415SJosef Bacik dir_id = btrfs_root_dirid(&new_root->root_item); 79073f73415SJosef Bacik setup_root: 79173f73415SJosef Bacik location.objectid = dir_id; 79273f73415SJosef Bacik location.type = BTRFS_INODE_ITEM_KEY; 79373f73415SJosef Bacik location.offset = 0; 79473f73415SJosef Bacik 79573f73415SJosef Bacik inode = btrfs_iget(sb, &location, new_root, &new); 7964cbd1149SDan Carpenter if (IS_ERR(inode)) 7974cbd1149SDan Carpenter return ERR_CAST(inode); 79873f73415SJosef Bacik 79973f73415SJosef Bacik /* 80073f73415SJosef Bacik * If we're just mounting the root most subvol put the inode and return 80173f73415SJosef Bacik * a reference to the dentry. We will have already gotten a reference 80273f73415SJosef Bacik * to the inode in btrfs_fill_super so we're good to go. 80373f73415SJosef Bacik */ 80473f73415SJosef Bacik if (!new && sb->s_root->d_inode == inode) { 80573f73415SJosef Bacik iput(inode); 80673f73415SJosef Bacik return dget(sb->s_root); 80773f73415SJosef Bacik } 80873f73415SJosef Bacik 809ba5b8958SJosef Bacik return d_obtain_alias(inode); 81073f73415SJosef Bacik } 81173f73415SJosef Bacik 8128a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 8138a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 8148a4b83ccSChris Mason void *data, int silent) 8152e635a27SChris Mason { 8162e635a27SChris Mason struct inode *inode; 817815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 8185d4f98a2SYan Zheng struct btrfs_key key; 81939279cc3SChris Mason int err; 8202e635a27SChris Mason 8212e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 8222e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 823e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 824af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 825be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 8265103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 8272e635a27SChris Mason sb->s_time_gran = 1; 8280eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 82933268eafSJosef Bacik sb->s_flags |= MS_POSIXACL; 83049cf6f45SChris Ball #endif 8310c4d2d95SJosef Bacik sb->s_flags |= MS_I_VERSION; 832ad2b2c80SAl Viro err = open_ctree(sb, fs_devices, (char *)data); 833ad2b2c80SAl Viro if (err) { 834e20d96d6SChris Mason printk("btrfs: open_ctree failed\n"); 835ad2b2c80SAl Viro return err; 836e20d96d6SChris Mason } 837b888db2bSChris Mason 8385d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 8395d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 8405d4f98a2SYan Zheng key.offset = 0; 84198c7089cSAl Viro inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL); 8425d4f98a2SYan Zheng if (IS_ERR(inode)) { 8435d4f98a2SYan Zheng err = PTR_ERR(inode); 84439279cc3SChris Mason goto fail_close; 84539279cc3SChris Mason } 8462e635a27SChris Mason 84748fde701SAl Viro sb->s_root = d_make_root(inode); 84848fde701SAl Viro if (!sb->s_root) { 84939279cc3SChris Mason err = -ENOMEM; 85039279cc3SChris Mason goto fail_close; 8512e635a27SChris Mason } 85258176a96SJosef Bacik 8536885f308SChris Mason save_mount_options(sb, data); 85490a887c9SDan Magenheimer cleancache_init_fs(sb); 85559553edfSAl Viro sb->s_flags |= MS_ACTIVE; 8562e635a27SChris Mason return 0; 8572e635a27SChris Mason 85839279cc3SChris Mason fail_close: 859815745cfSAl Viro close_ctree(fs_info->tree_root); 860d5719762SChris Mason return err; 861d5719762SChris Mason } 862d5719762SChris Mason 8636bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 864d5719762SChris Mason { 865d5719762SChris Mason struct btrfs_trans_handle *trans; 866815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 867815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 868df2ce34cSChris Mason 8691abe9b8aSliubo trace_btrfs_sync_fs(wait); 8701abe9b8aSliubo 871d561c025SChris Mason if (!wait) { 872815745cfSAl Viro filemap_flush(fs_info->btree_inode->i_mapping); 873df2ce34cSChris Mason return 0; 874d561c025SChris Mason } 875771ed689SChris Mason 8766bbe3a9cSLiu Bo btrfs_wait_ordered_extents(root, 0); 877771ed689SChris Mason 878354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 87960376ce4SJosef Bacik if (IS_ERR(trans)) { 880354aa0fbSMiao Xie /* no transaction, don't bother */ 881354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 882bd7de2c9SJosef Bacik return 0; 88398d5dc13STsutomu Itoh return PTR_ERR(trans); 88460376ce4SJosef Bacik } 885bd7de2c9SJosef Bacik return btrfs_commit_transaction(trans, root); 886d5719762SChris Mason } 887d5719762SChris Mason 88834c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 889a9572a15SEric Paris { 890815745cfSAl Viro struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 891815745cfSAl Viro struct btrfs_root *root = info->tree_root; 892200da64eSTsutomu Itoh char *compress_type; 893a9572a15SEric Paris 894a9572a15SEric Paris if (btrfs_test_opt(root, DEGRADED)) 895a9572a15SEric Paris seq_puts(seq, ",degraded"); 896a9572a15SEric Paris if (btrfs_test_opt(root, NODATASUM)) 897a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 898a9572a15SEric Paris if (btrfs_test_opt(root, NODATACOW)) 899a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 900a9572a15SEric Paris if (btrfs_test_opt(root, NOBARRIER)) 901a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 902a9572a15SEric Paris if (info->max_inline != 8192 * 1024) 90321380931SJoel Becker seq_printf(seq, ",max_inline=%llu", 90421380931SJoel Becker (unsigned long long)info->max_inline); 905a9572a15SEric Paris if (info->alloc_start != 0) 90621380931SJoel Becker seq_printf(seq, ",alloc_start=%llu", 90721380931SJoel Becker (unsigned long long)info->alloc_start); 908a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 909a9572a15SEric Paris num_online_cpus() + 2, 8)) 910a9572a15SEric Paris seq_printf(seq, ",thread_pool=%d", info->thread_pool_size); 911200da64eSTsutomu Itoh if (btrfs_test_opt(root, COMPRESS)) { 912200da64eSTsutomu Itoh if (info->compress_type == BTRFS_COMPRESS_ZLIB) 913200da64eSTsutomu Itoh compress_type = "zlib"; 914200da64eSTsutomu Itoh else 915200da64eSTsutomu Itoh compress_type = "lzo"; 916200da64eSTsutomu Itoh if (btrfs_test_opt(root, FORCE_COMPRESS)) 917200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 918200da64eSTsutomu Itoh else 919200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 920200da64eSTsutomu Itoh } 921c289811cSChris Mason if (btrfs_test_opt(root, NOSSD)) 922c289811cSChris Mason seq_puts(seq, ",nossd"); 923451d7585SChris Mason if (btrfs_test_opt(root, SSD_SPREAD)) 924451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 925451d7585SChris Mason else if (btrfs_test_opt(root, SSD)) 926a9572a15SEric Paris seq_puts(seq, ",ssd"); 9273a5e1404SSage Weil if (btrfs_test_opt(root, NOTREELOG)) 9286b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 929dccae999SSage Weil if (btrfs_test_opt(root, FLUSHONCOMMIT)) 9306b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 93120a5239aSMatthew Wilcox if (btrfs_test_opt(root, DISCARD)) 93220a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 933a9572a15SEric Paris if (!(root->fs_info->sb->s_flags & MS_POSIXACL)) 934a9572a15SEric Paris seq_puts(seq, ",noacl"); 935200da64eSTsutomu Itoh if (btrfs_test_opt(root, SPACE_CACHE)) 936200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 93773bc1876SJosef Bacik else 9388965593eSDavid Sterba seq_puts(seq, ",nospace_cache"); 939200da64eSTsutomu Itoh if (btrfs_test_opt(root, CLEAR_CACHE)) 940200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 941200da64eSTsutomu Itoh if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 942200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 9430942caa3SDavid Sterba if (btrfs_test_opt(root, ENOSPC_DEBUG)) 9440942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 9450942caa3SDavid Sterba if (btrfs_test_opt(root, AUTO_DEFRAG)) 9460942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 9470942caa3SDavid Sterba if (btrfs_test_opt(root, INODE_MAP_CACHE)) 9480942caa3SDavid Sterba seq_puts(seq, ",inode_cache"); 9499555c6c1SIlya Dryomov if (btrfs_test_opt(root, SKIP_BALANCE)) 9509555c6c1SIlya Dryomov seq_puts(seq, ",skip_balance"); 9518c342930SJeff Mahoney if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR)) 9528c342930SJeff Mahoney seq_puts(seq, ",fatal_errors=panic"); 953a9572a15SEric Paris return 0; 954a9572a15SEric Paris } 955a9572a15SEric Paris 956a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 9572e635a27SChris Mason { 958815745cfSAl Viro struct btrfs_fs_info *p = data; 959815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(s); 9604b82d6e4SYan 961815745cfSAl Viro return fs_info->fs_devices == p->fs_devices; 9624b82d6e4SYan } 9634b82d6e4SYan 964450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 965450ba0eaSJosef Bacik { 9666de1d09dSAl Viro int err = set_anon_super(s, data); 9676de1d09dSAl Viro if (!err) 968450ba0eaSJosef Bacik s->s_fs_info = data; 9696de1d09dSAl Viro return err; 970450ba0eaSJosef Bacik } 971450ba0eaSJosef Bacik 972830c4adbSJosef Bacik /* 973f9d9ef62SDavid Sterba * subvolumes are identified by ino 256 974f9d9ef62SDavid Sterba */ 975f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode) 976f9d9ef62SDavid Sterba { 977f9d9ef62SDavid Sterba if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 978f9d9ef62SDavid Sterba return 1; 979f9d9ef62SDavid Sterba return 0; 980f9d9ef62SDavid Sterba } 981f9d9ef62SDavid Sterba 982f9d9ef62SDavid Sterba /* 983830c4adbSJosef Bacik * This will strip out the subvol=%s argument for an argument string and add 984830c4adbSJosef Bacik * subvolid=0 to make sure we get the actual tree root for path walking to the 985830c4adbSJosef Bacik * subvol we want. 986830c4adbSJosef Bacik */ 987830c4adbSJosef Bacik static char *setup_root_args(char *args) 988830c4adbSJosef Bacik { 989f60d16a8SJim Meyering unsigned len = strlen(args) + 2 + 1; 990f60d16a8SJim Meyering char *src, *dst, *buf; 991830c4adbSJosef Bacik 992830c4adbSJosef Bacik /* 993f60d16a8SJim Meyering * We need the same args as before, but with this substitution: 994f60d16a8SJim Meyering * s!subvol=[^,]+!subvolid=0! 995830c4adbSJosef Bacik * 996f60d16a8SJim Meyering * Since the replacement string is up to 2 bytes longer than the 997f60d16a8SJim Meyering * original, allocate strlen(args) + 2 + 1 bytes. 998830c4adbSJosef Bacik */ 999830c4adbSJosef Bacik 1000f60d16a8SJim Meyering src = strstr(args, "subvol="); 1001830c4adbSJosef Bacik /* This shouldn't happen, but just in case.. */ 1002f60d16a8SJim Meyering if (!src) 1003830c4adbSJosef Bacik return NULL; 1004f60d16a8SJim Meyering 1005f60d16a8SJim Meyering buf = dst = kmalloc(len, GFP_NOFS); 1006f60d16a8SJim Meyering if (!buf) 1007f60d16a8SJim Meyering return NULL; 1008830c4adbSJosef Bacik 1009830c4adbSJosef Bacik /* 1010f60d16a8SJim Meyering * If the subvol= arg is not at the start of the string, 1011f60d16a8SJim Meyering * copy whatever precedes it into buf. 1012830c4adbSJosef Bacik */ 1013f60d16a8SJim Meyering if (src != args) { 1014f60d16a8SJim Meyering *src++ = '\0'; 1015f60d16a8SJim Meyering strcpy(buf, args); 1016f60d16a8SJim Meyering dst += strlen(args); 1017830c4adbSJosef Bacik } 1018830c4adbSJosef Bacik 1019f60d16a8SJim Meyering strcpy(dst, "subvolid=0"); 1020f60d16a8SJim Meyering dst += strlen("subvolid=0"); 1021830c4adbSJosef Bacik 1022830c4adbSJosef Bacik /* 1023f60d16a8SJim Meyering * If there is a "," after the original subvol=... string, 1024f60d16a8SJim Meyering * copy that suffix into our buffer. Otherwise, we're done. 1025830c4adbSJosef Bacik */ 1026f60d16a8SJim Meyering src = strchr(src, ','); 1027f60d16a8SJim Meyering if (src) 1028f60d16a8SJim Meyering strcpy(dst, src); 1029830c4adbSJosef Bacik 1030f60d16a8SJim Meyering return buf; 1031830c4adbSJosef Bacik } 1032830c4adbSJosef Bacik 1033830c4adbSJosef Bacik static struct dentry *mount_subvol(const char *subvol_name, int flags, 1034830c4adbSJosef Bacik const char *device_name, char *data) 1035830c4adbSJosef Bacik { 1036830c4adbSJosef Bacik struct dentry *root; 1037830c4adbSJosef Bacik struct vfsmount *mnt; 1038830c4adbSJosef Bacik char *newargs; 1039830c4adbSJosef Bacik 1040830c4adbSJosef Bacik newargs = setup_root_args(data); 1041830c4adbSJosef Bacik if (!newargs) 1042830c4adbSJosef Bacik return ERR_PTR(-ENOMEM); 1043830c4adbSJosef Bacik mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, 1044830c4adbSJosef Bacik newargs); 1045830c4adbSJosef Bacik kfree(newargs); 1046830c4adbSJosef Bacik if (IS_ERR(mnt)) 1047830c4adbSJosef Bacik return ERR_CAST(mnt); 1048830c4adbSJosef Bacik 1049ea441d11SAl Viro root = mount_subtree(mnt, subvol_name); 1050830c4adbSJosef Bacik 1051ea441d11SAl Viro if (!IS_ERR(root) && !is_subvolume_inode(root->d_inode)) { 1052ea441d11SAl Viro struct super_block *s = root->d_sb; 1053ea441d11SAl Viro dput(root); 1054ea441d11SAl Viro root = ERR_PTR(-EINVAL); 1055ea441d11SAl Viro deactivate_locked_super(s); 1056f9d9ef62SDavid Sterba printk(KERN_ERR "btrfs: '%s' is not a valid subvolume\n", 1057f9d9ef62SDavid Sterba subvol_name); 1058f9d9ef62SDavid Sterba } 1059f9d9ef62SDavid Sterba 1060830c4adbSJosef Bacik return root; 1061830c4adbSJosef Bacik } 1062450ba0eaSJosef Bacik 1063edf24abeSChristoph Hellwig /* 1064edf24abeSChristoph Hellwig * Find a superblock for the given device / mount point. 1065edf24abeSChristoph Hellwig * 1066edf24abeSChristoph Hellwig * Note: This is based on get_sb_bdev from fs/super.c with a few additions 1067edf24abeSChristoph Hellwig * for multiple device setup. Make sure to keep it in sync. 1068edf24abeSChristoph Hellwig */ 1069061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1070306e16ceSDavid Sterba const char *device_name, void *data) 10714b82d6e4SYan { 10724b82d6e4SYan struct block_device *bdev = NULL; 10734b82d6e4SYan struct super_block *s; 10744b82d6e4SYan struct dentry *root; 10758a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices = NULL; 1076450ba0eaSJosef Bacik struct btrfs_fs_info *fs_info = NULL; 107797288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 107873f73415SJosef Bacik char *subvol_name = NULL; 107973f73415SJosef Bacik u64 subvol_objectid = 0; 1080e15d0542SXin Zhong u64 subvol_rootid = 0; 10814b82d6e4SYan int error = 0; 10824b82d6e4SYan 108397288f2cSChristoph Hellwig if (!(flags & MS_RDONLY)) 108497288f2cSChristoph Hellwig mode |= FMODE_WRITE; 108597288f2cSChristoph Hellwig 108697288f2cSChristoph Hellwig error = btrfs_parse_early_options(data, mode, fs_type, 108773f73415SJosef Bacik &subvol_name, &subvol_objectid, 1088e15d0542SXin Zhong &subvol_rootid, &fs_devices); 1089f23c8af8SIlya Dryomov if (error) { 1090f23c8af8SIlya Dryomov kfree(subvol_name); 1091061dbc6bSAl Viro return ERR_PTR(error); 1092f23c8af8SIlya Dryomov } 1093edf24abeSChristoph Hellwig 1094830c4adbSJosef Bacik if (subvol_name) { 1095830c4adbSJosef Bacik root = mount_subvol(subvol_name, flags, device_name, data); 1096830c4adbSJosef Bacik kfree(subvol_name); 1097830c4adbSJosef Bacik return root; 1098830c4adbSJosef Bacik } 1099830c4adbSJosef Bacik 1100306e16ceSDavid Sterba error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices); 11018a4b83ccSChris Mason if (error) 1102830c4adbSJosef Bacik return ERR_PTR(error); 11034b82d6e4SYan 1104450ba0eaSJosef Bacik /* 1105450ba0eaSJosef Bacik * Setup a dummy root and fs_info for test/set super. This is because 1106450ba0eaSJosef Bacik * we don't actually fill this stuff out until open_ctree, but we need 1107450ba0eaSJosef Bacik * it for searching for existing supers, so this lets us do that and 1108450ba0eaSJosef Bacik * then open_ctree will properly initialize everything later. 1109450ba0eaSJosef Bacik */ 1110450ba0eaSJosef Bacik fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS); 111104d21a24SIlya Dryomov if (!fs_info) 111204d21a24SIlya Dryomov return ERR_PTR(-ENOMEM); 111304d21a24SIlya Dryomov 1114450ba0eaSJosef Bacik fs_info->fs_devices = fs_devices; 1115450ba0eaSJosef Bacik 11166c41761fSDavid Sterba fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 11176c41761fSDavid Sterba fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 11186c41761fSDavid Sterba if (!fs_info->super_copy || !fs_info->super_for_commit) { 11196c41761fSDavid Sterba error = -ENOMEM; 112004d21a24SIlya Dryomov goto error_fs_info; 112104d21a24SIlya Dryomov } 112204d21a24SIlya Dryomov 112304d21a24SIlya Dryomov error = btrfs_open_devices(fs_devices, mode, fs_type); 112404d21a24SIlya Dryomov if (error) 112504d21a24SIlya Dryomov goto error_fs_info; 112604d21a24SIlya Dryomov 112704d21a24SIlya Dryomov if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) { 112804d21a24SIlya Dryomov error = -EACCES; 11296c41761fSDavid Sterba goto error_close_devices; 11306c41761fSDavid Sterba } 11316c41761fSDavid Sterba 1132dfe25020SChris Mason bdev = fs_devices->latest_bdev; 11339249e17fSDavid Howells s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC, 11349249e17fSDavid Howells fs_info); 1135830c4adbSJosef Bacik if (IS_ERR(s)) { 1136830c4adbSJosef Bacik error = PTR_ERR(s); 1137830c4adbSJosef Bacik goto error_close_devices; 1138830c4adbSJosef Bacik } 11394b82d6e4SYan 11404b82d6e4SYan if (s->s_root) { 11412b82032cSYan Zheng btrfs_close_devices(fs_devices); 11426c41761fSDavid Sterba free_fs_info(fs_info); 114359553edfSAl Viro if ((flags ^ s->s_flags) & MS_RDONLY) 114459553edfSAl Viro error = -EBUSY; 11454b82d6e4SYan } else { 11464b82d6e4SYan char b[BDEVNAME_SIZE]; 11474b82d6e4SYan 11484b82d6e4SYan strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); 1149815745cfSAl Viro btrfs_sb(s)->bdev_holder = fs_type; 11508a4b83ccSChris Mason error = btrfs_fill_super(s, fs_devices, data, 11518a4b83ccSChris Mason flags & MS_SILENT ? 1 : 0); 11524b82d6e4SYan } 11534b82d6e4SYan 115459553edfSAl Viro root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error); 115559553edfSAl Viro if (IS_ERR(root)) 1156e15d0542SXin Zhong deactivate_locked_super(s); 11574b82d6e4SYan 1158061dbc6bSAl Viro return root; 11594b82d6e4SYan 1160c146afadSYan Zheng error_close_devices: 11618a4b83ccSChris Mason btrfs_close_devices(fs_devices); 116204d21a24SIlya Dryomov error_fs_info: 11636c41761fSDavid Sterba free_fs_info(fs_info); 1164061dbc6bSAl Viro return ERR_PTR(error); 11654b82d6e4SYan } 11662e635a27SChris Mason 11670d2450abSSergei Trofimovich static void btrfs_set_max_workers(struct btrfs_workers *workers, int new_limit) 11680d2450abSSergei Trofimovich { 11690d2450abSSergei Trofimovich spin_lock_irq(&workers->lock); 11700d2450abSSergei Trofimovich workers->max_workers = new_limit; 11710d2450abSSergei Trofimovich spin_unlock_irq(&workers->lock); 11720d2450abSSergei Trofimovich } 11730d2450abSSergei Trofimovich 11740d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 11750d2450abSSergei Trofimovich int new_pool_size, int old_pool_size) 11760d2450abSSergei Trofimovich { 11770d2450abSSergei Trofimovich if (new_pool_size == old_pool_size) 11780d2450abSSergei Trofimovich return; 11790d2450abSSergei Trofimovich 11800d2450abSSergei Trofimovich fs_info->thread_pool_size = new_pool_size; 11810d2450abSSergei Trofimovich 11820d2450abSSergei Trofimovich printk(KERN_INFO "btrfs: resize thread pool %d -> %d\n", 11830d2450abSSergei Trofimovich old_pool_size, new_pool_size); 11840d2450abSSergei Trofimovich 11850d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->generic_worker, new_pool_size); 11860d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->workers, new_pool_size); 11870d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->delalloc_workers, new_pool_size); 11880d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->submit_workers, new_pool_size); 11890d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->caching_workers, new_pool_size); 11900d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->fixup_workers, new_pool_size); 11910d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_workers, new_pool_size); 11920d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_meta_workers, new_pool_size); 11930d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_meta_write_workers, new_pool_size); 11940d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_write_workers, new_pool_size); 11950d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_freespace_worker, new_pool_size); 11960d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->delayed_workers, new_pool_size); 11970d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->readahead_workers, new_pool_size); 11980d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->scrub_workers, new_pool_size); 11990d2450abSSergei Trofimovich } 12000d2450abSSergei Trofimovich 1201c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1202c146afadSYan Zheng { 1203815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1204815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 120549b25e05SJeff Mahoney unsigned old_flags = sb->s_flags; 120649b25e05SJeff Mahoney unsigned long old_opts = fs_info->mount_opt; 120749b25e05SJeff Mahoney unsigned long old_compress_type = fs_info->compress_type; 120849b25e05SJeff Mahoney u64 old_max_inline = fs_info->max_inline; 120949b25e05SJeff Mahoney u64 old_alloc_start = fs_info->alloc_start; 121049b25e05SJeff Mahoney int old_thread_pool_size = fs_info->thread_pool_size; 121149b25e05SJeff Mahoney unsigned int old_metadata_ratio = fs_info->metadata_ratio; 1212c146afadSYan Zheng int ret; 1213c146afadSYan Zheng 1214b288052eSChris Mason ret = btrfs_parse_options(root, data); 121549b25e05SJeff Mahoney if (ret) { 121649b25e05SJeff Mahoney ret = -EINVAL; 121749b25e05SJeff Mahoney goto restore; 121849b25e05SJeff Mahoney } 1219b288052eSChris Mason 12200d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 12210d2450abSSergei Trofimovich fs_info->thread_pool_size, old_thread_pool_size); 12220d2450abSSergei Trofimovich 1223c146afadSYan Zheng if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 1224c146afadSYan Zheng return 0; 1225c146afadSYan Zheng 1226c146afadSYan Zheng if (*flags & MS_RDONLY) { 1227c146afadSYan Zheng sb->s_flags |= MS_RDONLY; 1228c146afadSYan Zheng 1229c146afadSYan Zheng ret = btrfs_commit_super(root); 123049b25e05SJeff Mahoney if (ret) 123149b25e05SJeff Mahoney goto restore; 1232c146afadSYan Zheng } else { 12338a3db184SSergei Trofimovich if (fs_info->fs_devices->rw_devices == 0) { 123449b25e05SJeff Mahoney ret = -EACCES; 123549b25e05SJeff Mahoney goto restore; 12368a3db184SSergei Trofimovich } 12372b82032cSYan Zheng 1238292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 1239292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 1240292fd7fcSStefan Behrens !(*flags & MS_RDONLY)) { 1241292fd7fcSStefan Behrens printk(KERN_WARNING 1242292fd7fcSStefan Behrens "Btrfs: too many missing devices, writeable remount is not allowed\n"); 1243292fd7fcSStefan Behrens ret = -EACCES; 1244292fd7fcSStefan Behrens goto restore; 1245292fd7fcSStefan Behrens } 1246292fd7fcSStefan Behrens 12478a3db184SSergei Trofimovich if (btrfs_super_log_root(fs_info->super_copy) != 0) { 124849b25e05SJeff Mahoney ret = -EINVAL; 124949b25e05SJeff Mahoney goto restore; 12508a3db184SSergei Trofimovich } 1251c146afadSYan Zheng 1252815745cfSAl Viro ret = btrfs_cleanup_fs_roots(fs_info); 125349b25e05SJeff Mahoney if (ret) 125449b25e05SJeff Mahoney goto restore; 1255c146afadSYan Zheng 1256d68fc57bSYan, Zheng /* recover relocation */ 1257d68fc57bSYan, Zheng ret = btrfs_recover_relocation(root); 125849b25e05SJeff Mahoney if (ret) 125949b25e05SJeff Mahoney goto restore; 1260c146afadSYan Zheng 12612b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 12622b6ba629SIlya Dryomov if (ret) 12632b6ba629SIlya Dryomov goto restore; 12642b6ba629SIlya Dryomov 1265c146afadSYan Zheng sb->s_flags &= ~MS_RDONLY; 1266c146afadSYan Zheng } 1267c146afadSYan Zheng 1268c146afadSYan Zheng return 0; 126949b25e05SJeff Mahoney 127049b25e05SJeff Mahoney restore: 127149b25e05SJeff Mahoney /* We've hit an error - don't reset MS_RDONLY */ 127249b25e05SJeff Mahoney if (sb->s_flags & MS_RDONLY) 127349b25e05SJeff Mahoney old_flags |= MS_RDONLY; 127449b25e05SJeff Mahoney sb->s_flags = old_flags; 127549b25e05SJeff Mahoney fs_info->mount_opt = old_opts; 127649b25e05SJeff Mahoney fs_info->compress_type = old_compress_type; 127749b25e05SJeff Mahoney fs_info->max_inline = old_max_inline; 127849b25e05SJeff Mahoney fs_info->alloc_start = old_alloc_start; 12790d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 12800d2450abSSergei Trofimovich old_thread_pool_size, fs_info->thread_pool_size); 128149b25e05SJeff Mahoney fs_info->metadata_ratio = old_metadata_ratio; 128249b25e05SJeff Mahoney return ret; 1283c146afadSYan Zheng } 1284c146afadSYan Zheng 1285bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 1286bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1, 1287bcd53741SArne Jansen const void *dev_info2) 1288bcd53741SArne Jansen { 1289bcd53741SArne Jansen if (((struct btrfs_device_info *)dev_info1)->max_avail > 1290bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1291bcd53741SArne Jansen return -1; 1292bcd53741SArne Jansen else if (((struct btrfs_device_info *)dev_info1)->max_avail < 1293bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1294bcd53741SArne Jansen return 1; 1295bcd53741SArne Jansen else 1296bcd53741SArne Jansen return 0; 1297bcd53741SArne Jansen } 1298bcd53741SArne Jansen 1299bcd53741SArne Jansen /* 1300bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 1301bcd53741SArne Jansen * is stored.(Descending Sort) 1302bcd53741SArne Jansen */ 1303bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 1304bcd53741SArne Jansen struct btrfs_device_info *devices, 1305bcd53741SArne Jansen size_t nr_devices) 1306bcd53741SArne Jansen { 1307bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 1308bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 1309bcd53741SArne Jansen } 1310bcd53741SArne Jansen 13116d07bcecSMiao Xie /* 13126d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 13136d07bcecSMiao Xie * file data. 13146d07bcecSMiao Xie */ 13156d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes) 13166d07bcecSMiao Xie { 13176d07bcecSMiao Xie struct btrfs_fs_info *fs_info = root->fs_info; 13186d07bcecSMiao Xie struct btrfs_device_info *devices_info; 13196d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 13206d07bcecSMiao Xie struct btrfs_device *device; 13216d07bcecSMiao Xie u64 skip_space; 13226d07bcecSMiao Xie u64 type; 13236d07bcecSMiao Xie u64 avail_space; 13246d07bcecSMiao Xie u64 used_space; 13256d07bcecSMiao Xie u64 min_stripe_size; 132639fb26c3SMiao Xie int min_stripes = 1, num_stripes = 1; 13276d07bcecSMiao Xie int i = 0, nr_devices; 13286d07bcecSMiao Xie int ret; 13296d07bcecSMiao Xie 1330b772a86eSLi Zefan nr_devices = fs_info->fs_devices->open_devices; 13316d07bcecSMiao Xie BUG_ON(!nr_devices); 13326d07bcecSMiao Xie 13336d07bcecSMiao Xie devices_info = kmalloc(sizeof(*devices_info) * nr_devices, 13346d07bcecSMiao Xie GFP_NOFS); 13356d07bcecSMiao Xie if (!devices_info) 13366d07bcecSMiao Xie return -ENOMEM; 13376d07bcecSMiao Xie 13386d07bcecSMiao Xie /* calc min stripe number for data space alloction */ 13396d07bcecSMiao Xie type = btrfs_get_alloc_profile(root, 1); 134039fb26c3SMiao Xie if (type & BTRFS_BLOCK_GROUP_RAID0) { 13416d07bcecSMiao Xie min_stripes = 2; 134239fb26c3SMiao Xie num_stripes = nr_devices; 134339fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID1) { 13446d07bcecSMiao Xie min_stripes = 2; 134539fb26c3SMiao Xie num_stripes = 2; 134639fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID10) { 13476d07bcecSMiao Xie min_stripes = 4; 134839fb26c3SMiao Xie num_stripes = 4; 134939fb26c3SMiao Xie } 13506d07bcecSMiao Xie 13516d07bcecSMiao Xie if (type & BTRFS_BLOCK_GROUP_DUP) 13526d07bcecSMiao Xie min_stripe_size = 2 * BTRFS_STRIPE_LEN; 13536d07bcecSMiao Xie else 13546d07bcecSMiao Xie min_stripe_size = BTRFS_STRIPE_LEN; 13556d07bcecSMiao Xie 1356b772a86eSLi Zefan list_for_each_entry(device, &fs_devices->devices, dev_list) { 1357*63a212abSStefan Behrens if (!device->in_fs_metadata || !device->bdev || 1358*63a212abSStefan Behrens device->is_tgtdev_for_dev_replace) 13596d07bcecSMiao Xie continue; 13606d07bcecSMiao Xie 13616d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 13626d07bcecSMiao Xie 13636d07bcecSMiao Xie /* align with stripe_len */ 13646d07bcecSMiao Xie do_div(avail_space, BTRFS_STRIPE_LEN); 13656d07bcecSMiao Xie avail_space *= BTRFS_STRIPE_LEN; 13666d07bcecSMiao Xie 13676d07bcecSMiao Xie /* 13686d07bcecSMiao Xie * In order to avoid overwritting the superblock on the drive, 13696d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 13706d07bcecSMiao Xie * allocation. 13716d07bcecSMiao Xie */ 13726d07bcecSMiao Xie skip_space = 1024 * 1024; 13736d07bcecSMiao Xie 13746d07bcecSMiao Xie /* user can set the offset in fs_info->alloc_start. */ 13756d07bcecSMiao Xie if (fs_info->alloc_start + BTRFS_STRIPE_LEN <= 13766d07bcecSMiao Xie device->total_bytes) 13776d07bcecSMiao Xie skip_space = max(fs_info->alloc_start, skip_space); 13786d07bcecSMiao Xie 13796d07bcecSMiao Xie /* 13806d07bcecSMiao Xie * btrfs can not use the free space in [0, skip_space - 1], 13816d07bcecSMiao Xie * we must subtract it from the total. In order to implement 13826d07bcecSMiao Xie * it, we account the used space in this range first. 13836d07bcecSMiao Xie */ 13846d07bcecSMiao Xie ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1, 13856d07bcecSMiao Xie &used_space); 13866d07bcecSMiao Xie if (ret) { 13876d07bcecSMiao Xie kfree(devices_info); 13886d07bcecSMiao Xie return ret; 13896d07bcecSMiao Xie } 13906d07bcecSMiao Xie 13916d07bcecSMiao Xie /* calc the free space in [0, skip_space - 1] */ 13926d07bcecSMiao Xie skip_space -= used_space; 13936d07bcecSMiao Xie 13946d07bcecSMiao Xie /* 13956d07bcecSMiao Xie * we can use the free space in [0, skip_space - 1], subtract 13966d07bcecSMiao Xie * it from the total. 13976d07bcecSMiao Xie */ 13986d07bcecSMiao Xie if (avail_space && avail_space >= skip_space) 13996d07bcecSMiao Xie avail_space -= skip_space; 14006d07bcecSMiao Xie else 14016d07bcecSMiao Xie avail_space = 0; 14026d07bcecSMiao Xie 14036d07bcecSMiao Xie if (avail_space < min_stripe_size) 14046d07bcecSMiao Xie continue; 14056d07bcecSMiao Xie 14066d07bcecSMiao Xie devices_info[i].dev = device; 14076d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 14086d07bcecSMiao Xie 14096d07bcecSMiao Xie i++; 14106d07bcecSMiao Xie } 14116d07bcecSMiao Xie 14126d07bcecSMiao Xie nr_devices = i; 14136d07bcecSMiao Xie 14146d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 14156d07bcecSMiao Xie 14166d07bcecSMiao Xie i = nr_devices - 1; 14176d07bcecSMiao Xie avail_space = 0; 14186d07bcecSMiao Xie while (nr_devices >= min_stripes) { 141939fb26c3SMiao Xie if (num_stripes > nr_devices) 142039fb26c3SMiao Xie num_stripes = nr_devices; 142139fb26c3SMiao Xie 14226d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 14236d07bcecSMiao Xie int j; 14246d07bcecSMiao Xie u64 alloc_size; 14256d07bcecSMiao Xie 142639fb26c3SMiao Xie avail_space += devices_info[i].max_avail * num_stripes; 14276d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 142839fb26c3SMiao Xie for (j = i + 1 - num_stripes; j <= i; j++) 14296d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 14306d07bcecSMiao Xie } 14316d07bcecSMiao Xie i--; 14326d07bcecSMiao Xie nr_devices--; 14336d07bcecSMiao Xie } 14346d07bcecSMiao Xie 14356d07bcecSMiao Xie kfree(devices_info); 14366d07bcecSMiao Xie *free_bytes = avail_space; 14376d07bcecSMiao Xie return 0; 14386d07bcecSMiao Xie } 14396d07bcecSMiao Xie 14408fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 14418fd17795SChris Mason { 1442815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 1443815745cfSAl Viro struct btrfs_super_block *disk_super = fs_info->super_copy; 1444815745cfSAl Viro struct list_head *head = &fs_info->space_info; 1445bd4d1088SJosef Bacik struct btrfs_space_info *found; 1446bd4d1088SJosef Bacik u64 total_used = 0; 14476d07bcecSMiao Xie u64 total_free_data = 0; 1448db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 1449815745cfSAl Viro __be32 *fsid = (__be32 *)fs_info->fsid; 14506d07bcecSMiao Xie int ret; 14518fd17795SChris Mason 14526d07bcecSMiao Xie /* holding chunk_muext to avoid allocating new chunks */ 1453815745cfSAl Viro mutex_lock(&fs_info->chunk_mutex); 1454bd4d1088SJosef Bacik rcu_read_lock(); 145589a55897SJosef Bacik list_for_each_entry_rcu(found, head, list) { 14566d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 14576d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 14586d07bcecSMiao Xie total_free_data -= 14596d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 14606d07bcecSMiao Xie } 14616d07bcecSMiao Xie 1462b742bb82SYan, Zheng total_used += found->disk_used; 146389a55897SJosef Bacik } 1464bd4d1088SJosef Bacik rcu_read_unlock(); 1465bd4d1088SJosef Bacik 14668fd17795SChris Mason buf->f_namelen = BTRFS_NAME_LEN; 1467db94535dSChris Mason buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits; 1468bd4d1088SJosef Bacik buf->f_bfree = buf->f_blocks - (total_used >> bits); 14698fd17795SChris Mason buf->f_bsize = dentry->d_sb->s_blocksize; 14708fd17795SChris Mason buf->f_type = BTRFS_SUPER_MAGIC; 14716d07bcecSMiao Xie buf->f_bavail = total_free_data; 1472815745cfSAl Viro ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data); 14736d07bcecSMiao Xie if (ret) { 1474815745cfSAl Viro mutex_unlock(&fs_info->chunk_mutex); 14756d07bcecSMiao Xie return ret; 14766d07bcecSMiao Xie } 14776d07bcecSMiao Xie buf->f_bavail += total_free_data; 14786d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 1479815745cfSAl Viro mutex_unlock(&fs_info->chunk_mutex); 1480d397712bSChris Mason 14819d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 14829d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 14839d03632eSDavid Woodhouse on a big-endian or little-endian host */ 14849d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 14859d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 148632d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 148732d48fa1SDavid Woodhouse buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32; 148832d48fa1SDavid Woodhouse buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid; 148932d48fa1SDavid Woodhouse 14908fd17795SChris Mason return 0; 14918fd17795SChris Mason } 1492b5133862SChris Mason 1493aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb) 1494aea52e19SAl Viro { 1495815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1496aea52e19SAl Viro kill_anon_super(sb); 1497aea52e19SAl Viro free_fs_info(fs_info); 1498aea52e19SAl Viro } 1499aea52e19SAl Viro 15002e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 15012e635a27SChris Mason .owner = THIS_MODULE, 15022e635a27SChris Mason .name = "btrfs", 1503061dbc6bSAl Viro .mount = btrfs_mount, 1504aea52e19SAl Viro .kill_sb = btrfs_kill_super, 15052e635a27SChris Mason .fs_flags = FS_REQUIRES_DEV, 15062e635a27SChris Mason }; 1507a9218f6bSChris Mason 1508d352ac68SChris Mason /* 1509d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 1510d352ac68SChris Mason */ 15118a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 15128a4b83ccSChris Mason unsigned long arg) 15138a4b83ccSChris Mason { 15148a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 15158a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 1516c071fcfdSChris Mason int ret = -ENOTTY; 15178a4b83ccSChris Mason 1518e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1519e441d54dSChris Mason return -EPERM; 1520e441d54dSChris Mason 1521dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 1522dae7b665SLi Zefan if (IS_ERR(vol)) 1523dae7b665SLi Zefan return PTR_ERR(vol); 1524c071fcfdSChris Mason 15258a4b83ccSChris Mason switch (cmd) { 15268a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 152797288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 15288a4b83ccSChris Mason &btrfs_fs_type, &fs_devices); 15298a4b83ccSChris Mason break; 153002db0844SJosef Bacik case BTRFS_IOC_DEVICES_READY: 153102db0844SJosef Bacik ret = btrfs_scan_one_device(vol->name, FMODE_READ, 153202db0844SJosef Bacik &btrfs_fs_type, &fs_devices); 153302db0844SJosef Bacik if (ret) 153402db0844SJosef Bacik break; 153502db0844SJosef Bacik ret = !(fs_devices->num_devices == fs_devices->total_devices); 153602db0844SJosef Bacik break; 15378a4b83ccSChris Mason } 1538dae7b665SLi Zefan 15398a4b83ccSChris Mason kfree(vol); 1540f819d837SLinda Knippers return ret; 15418a4b83ccSChris Mason } 15428a4b83ccSChris Mason 15430176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 1544ed0dab6bSYan { 1545354aa0fbSMiao Xie struct btrfs_trans_handle *trans; 1546354aa0fbSMiao Xie struct btrfs_root *root = btrfs_sb(sb)->tree_root; 1547354aa0fbSMiao Xie 1548354aa0fbSMiao Xie trans = btrfs_attach_transaction(root); 1549354aa0fbSMiao Xie if (IS_ERR(trans)) { 1550354aa0fbSMiao Xie /* no transaction, don't bother */ 1551354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 15520176260fSLinus Torvalds return 0; 1553354aa0fbSMiao Xie return PTR_ERR(trans); 1554354aa0fbSMiao Xie } 1555354aa0fbSMiao Xie return btrfs_commit_transaction(trans, root); 1556ed0dab6bSYan } 1557ed0dab6bSYan 15580176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb) 1559ed0dab6bSYan { 15600176260fSLinus Torvalds return 0; 1561ed0dab6bSYan } 15622e635a27SChris Mason 15639c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 15649c5085c1SJosef Bacik { 15659c5085c1SJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 15669c5085c1SJosef Bacik struct btrfs_fs_devices *cur_devices; 15679c5085c1SJosef Bacik struct btrfs_device *dev, *first_dev = NULL; 15689c5085c1SJosef Bacik struct list_head *head; 15699c5085c1SJosef Bacik struct rcu_string *name; 15709c5085c1SJosef Bacik 15719c5085c1SJosef Bacik mutex_lock(&fs_info->fs_devices->device_list_mutex); 15729c5085c1SJosef Bacik cur_devices = fs_info->fs_devices; 15739c5085c1SJosef Bacik while (cur_devices) { 15749c5085c1SJosef Bacik head = &cur_devices->devices; 15759c5085c1SJosef Bacik list_for_each_entry(dev, head, dev_list) { 1576aa9ddcd4SJosef Bacik if (dev->missing) 1577aa9ddcd4SJosef Bacik continue; 15789c5085c1SJosef Bacik if (!first_dev || dev->devid < first_dev->devid) 15799c5085c1SJosef Bacik first_dev = dev; 15809c5085c1SJosef Bacik } 15819c5085c1SJosef Bacik cur_devices = cur_devices->seed; 15829c5085c1SJosef Bacik } 15839c5085c1SJosef Bacik 15849c5085c1SJosef Bacik if (first_dev) { 15859c5085c1SJosef Bacik rcu_read_lock(); 15869c5085c1SJosef Bacik name = rcu_dereference(first_dev->name); 15879c5085c1SJosef Bacik seq_escape(m, name->str, " \t\n\\"); 15889c5085c1SJosef Bacik rcu_read_unlock(); 15899c5085c1SJosef Bacik } else { 15909c5085c1SJosef Bacik WARN_ON(1); 15919c5085c1SJosef Bacik } 15929c5085c1SJosef Bacik mutex_unlock(&fs_info->fs_devices->device_list_mutex); 15939c5085c1SJosef Bacik return 0; 15949c5085c1SJosef Bacik } 15959c5085c1SJosef Bacik 1596b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 159776dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 1598bd555975SAl Viro .evict_inode = btrfs_evict_inode, 1599e20d96d6SChris Mason .put_super = btrfs_put_super, 1600d5719762SChris Mason .sync_fs = btrfs_sync_fs, 1601a9572a15SEric Paris .show_options = btrfs_show_options, 16029c5085c1SJosef Bacik .show_devname = btrfs_show_devname, 16034730a4bcSChris Mason .write_inode = btrfs_write_inode, 16042c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 16052c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 16068fd17795SChris Mason .statfs = btrfs_statfs, 1607c146afadSYan Zheng .remount_fs = btrfs_remount, 16080176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 16090176260fSLinus Torvalds .unfreeze_fs = btrfs_unfreeze, 1610e20d96d6SChris Mason }; 1611a9218f6bSChris Mason 1612a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 1613a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 1614a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 1615a9218f6bSChris Mason .owner = THIS_MODULE, 16166038f373SArnd Bergmann .llseek = noop_llseek, 1617a9218f6bSChris Mason }; 1618a9218f6bSChris Mason 1619a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 1620578454ffSKay Sievers .minor = BTRFS_MINOR, 1621a9218f6bSChris Mason .name = "btrfs-control", 1622a9218f6bSChris Mason .fops = &btrfs_ctl_fops 1623a9218f6bSChris Mason }; 1624a9218f6bSChris Mason 1625578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 1626578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 1627578454ffSKay Sievers 1628a9218f6bSChris Mason static int btrfs_interface_init(void) 1629a9218f6bSChris Mason { 1630a9218f6bSChris Mason return misc_register(&btrfs_misc); 1631a9218f6bSChris Mason } 1632a9218f6bSChris Mason 1633b2950863SChristoph Hellwig static void btrfs_interface_exit(void) 1634a9218f6bSChris Mason { 1635a9218f6bSChris Mason if (misc_deregister(&btrfs_misc) < 0) 163648940662SDaniel J Blueman printk(KERN_INFO "btrfs: misc_deregister failed for control device\n"); 1637a9218f6bSChris Mason } 1638a9218f6bSChris Mason 16392e635a27SChris Mason static int __init init_btrfs_fs(void) 16402e635a27SChris Mason { 16412c90e5d6SChris Mason int err; 164258176a96SJosef Bacik 164358176a96SJosef Bacik err = btrfs_init_sysfs(); 164458176a96SJosef Bacik if (err) 164558176a96SJosef Bacik return err; 164658176a96SJosef Bacik 1647143bede5SJeff Mahoney btrfs_init_compress(); 1648d1310b2eSChris Mason 1649261507a0SLi Zefan err = btrfs_init_cachep(); 1650261507a0SLi Zefan if (err) 1651261507a0SLi Zefan goto free_compress; 1652261507a0SLi Zefan 1653d1310b2eSChris Mason err = extent_io_init(); 16542f4cbe64SWyatt Banks if (err) 16552f4cbe64SWyatt Banks goto free_cachep; 16562f4cbe64SWyatt Banks 1657d1310b2eSChris Mason err = extent_map_init(); 1658d1310b2eSChris Mason if (err) 1659d1310b2eSChris Mason goto free_extent_io; 1660d1310b2eSChris Mason 16616352b91dSMiao Xie err = ordered_data_init(); 16622f4cbe64SWyatt Banks if (err) 16632f4cbe64SWyatt Banks goto free_extent_map; 1664c8b97818SChris Mason 16656352b91dSMiao Xie err = btrfs_delayed_inode_init(); 16666352b91dSMiao Xie if (err) 16676352b91dSMiao Xie goto free_ordered_data; 16686352b91dSMiao Xie 166916cdcec7SMiao Xie err = btrfs_interface_init(); 167016cdcec7SMiao Xie if (err) 167116cdcec7SMiao Xie goto free_delayed_inode; 167216cdcec7SMiao Xie 1673a9218f6bSChris Mason err = register_filesystem(&btrfs_fs_type); 1674a9218f6bSChris Mason if (err) 1675a9218f6bSChris Mason goto unregister_ioctl; 1676b3c3da71SChris Mason 1677e565d4b9SJan Schmidt btrfs_init_lockdep(); 1678e565d4b9SJan Schmidt 1679b3c3da71SChris Mason printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION); 16802f4cbe64SWyatt Banks return 0; 16812f4cbe64SWyatt Banks 1682a9218f6bSChris Mason unregister_ioctl: 1683a9218f6bSChris Mason btrfs_interface_exit(); 168416cdcec7SMiao Xie free_delayed_inode: 168516cdcec7SMiao Xie btrfs_delayed_inode_exit(); 16866352b91dSMiao Xie free_ordered_data: 16876352b91dSMiao Xie ordered_data_exit(); 16882f4cbe64SWyatt Banks free_extent_map: 16892f4cbe64SWyatt Banks extent_map_exit(); 1690d1310b2eSChris Mason free_extent_io: 1691d1310b2eSChris Mason extent_io_exit(); 16922f4cbe64SWyatt Banks free_cachep: 16932f4cbe64SWyatt Banks btrfs_destroy_cachep(); 1694261507a0SLi Zefan free_compress: 1695261507a0SLi Zefan btrfs_exit_compress(); 16962f4cbe64SWyatt Banks btrfs_exit_sysfs(); 16972c90e5d6SChris Mason return err; 16982e635a27SChris Mason } 16992e635a27SChris Mason 17002e635a27SChris Mason static void __exit exit_btrfs_fs(void) 17012e635a27SChris Mason { 170239279cc3SChris Mason btrfs_destroy_cachep(); 170316cdcec7SMiao Xie btrfs_delayed_inode_exit(); 17046352b91dSMiao Xie ordered_data_exit(); 1705a52d9a80SChris Mason extent_map_exit(); 1706d1310b2eSChris Mason extent_io_exit(); 1707a9218f6bSChris Mason btrfs_interface_exit(); 17082e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 170958176a96SJosef Bacik btrfs_exit_sysfs(); 17108a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 1711261507a0SLi Zefan btrfs_exit_compress(); 17122e635a27SChris Mason } 17132e635a27SChris Mason 17142e635a27SChris Mason module_init(init_btrfs_fs) 17152e635a27SChris Mason module_exit(exit_btrfs_fs) 17162e635a27SChris Mason 17172e635a27SChris Mason MODULE_LICENSE("GPL"); 1718