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> 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" 503a686375SChris Mason #include "print-tree.h" 5114a958e6SFilipe David Borba Manana #include "hash.h" 5263541927SFilipe David Borba Manana #include "props.h" 535103e947SJosef Bacik #include "xattr.h" 548a4b83ccSChris Mason #include "volumes.h" 55be6e8dc0SBalaji Rao #include "export.h" 56c8b97818SChris Mason #include "compression.h" 579c5085c1SJosef Bacik #include "rcu-string.h" 588dabb742SStefan Behrens #include "dev-replace.h" 5974255aa0SJosef Bacik #include "free-space-cache.h" 60b9e9a6cbSWang Shilong #include "backref.h" 61dc11dd5dSJosef Bacik #include "tests/btrfs-tests.h" 622e635a27SChris Mason 63d3982100SMark Fasheh #include "qgroup.h" 641abe9b8aSliubo #define CREATE_TRACE_POINTS 651abe9b8aSliubo #include <trace/events/btrfs.h> 661abe9b8aSliubo 67b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops; 68830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type; 69e20d96d6SChris Mason 700723a047SHarald Hoyer static int btrfs_remount(struct super_block *sb, int *flags, char *data); 710723a047SHarald Hoyer 72e33e17eeSJeff Mahoney const char *btrfs_decode_error(int errno) 73acce952bSliubo { 7408748810SDavid Sterba char *errstr = "unknown"; 75acce952bSliubo 76acce952bSliubo switch (errno) { 77acce952bSliubo case -EIO: 78acce952bSliubo errstr = "IO failure"; 79acce952bSliubo break; 80acce952bSliubo case -ENOMEM: 81acce952bSliubo errstr = "Out of memory"; 82acce952bSliubo break; 83acce952bSliubo case -EROFS: 84acce952bSliubo errstr = "Readonly filesystem"; 85acce952bSliubo break; 868c342930SJeff Mahoney case -EEXIST: 878c342930SJeff Mahoney errstr = "Object already exists"; 888c342930SJeff Mahoney break; 8994ef7280SDavid Sterba case -ENOSPC: 9094ef7280SDavid Sterba errstr = "No space left"; 9194ef7280SDavid Sterba break; 9294ef7280SDavid Sterba case -ENOENT: 9394ef7280SDavid Sterba errstr = "No such entry"; 9494ef7280SDavid Sterba break; 95acce952bSliubo } 96acce952bSliubo 97acce952bSliubo return errstr; 98acce952bSliubo } 99acce952bSliubo 100bbece8a3SDavid Sterba static void save_error_info(struct btrfs_fs_info *fs_info) 101acce952bSliubo { 102acce952bSliubo /* 103acce952bSliubo * today we only save the error info into ram. Long term we'll 104acce952bSliubo * also send it down to the disk 105acce952bSliubo */ 10687533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 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 11787533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 118acce952bSliubo sb->s_flags |= MS_RDONLY; 119c2cf52ebSSimon Kirby btrfs_info(fs_info, "forced readonly"); 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 */ 130acce952bSliubo } 131acce952bSliubo } 132acce952bSliubo 133acce952bSliubo /* 134acce952bSliubo * __btrfs_std_error decodes expected errors from the caller and 135acce952bSliubo * invokes the approciate error response. 136acce952bSliubo */ 137c0d19e2bSDavid Sterba __cold 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; 14257d816a1SAnand Jain #ifdef CONFIG_PRINTK 143acce952bSliubo const char *errstr; 14457d816a1SAnand Jain #endif 145acce952bSliubo 146acce952bSliubo /* 147acce952bSliubo * Special case: if the error is EROFS, and we're already 148acce952bSliubo * under MS_RDONLY, then it is safe here. 149acce952bSliubo */ 150acce952bSliubo if (errno == -EROFS && (sb->s_flags & MS_RDONLY)) 151acce952bSliubo return; 152acce952bSliubo 15357d816a1SAnand Jain #ifdef CONFIG_PRINTK 15408748810SDavid Sterba errstr = btrfs_decode_error(errno); 1554da35113SJeff Mahoney if (fmt) { 15637252a66SEric Sandeen struct va_format vaf; 15737252a66SEric Sandeen va_list args; 15837252a66SEric Sandeen 15937252a66SEric Sandeen va_start(args, fmt); 16037252a66SEric Sandeen vaf.fmt = fmt; 16137252a66SEric Sandeen vaf.va = &args; 1624da35113SJeff Mahoney 163efe120a0SFrank Holton printk(KERN_CRIT 164efe120a0SFrank Holton "BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n", 16508748810SDavid Sterba sb->s_id, function, line, errno, errstr, &vaf); 16637252a66SEric Sandeen va_end(args); 1674da35113SJeff Mahoney } else { 168efe120a0SFrank Holton printk(KERN_CRIT "BTRFS: error (device %s) in %s:%d: errno=%d %s\n", 16908748810SDavid Sterba sb->s_id, function, line, errno, errstr); 1704da35113SJeff Mahoney } 17157d816a1SAnand Jain #endif 172acce952bSliubo 1734da35113SJeff Mahoney /* Don't go through full error handling during mount */ 1744da35113SJeff Mahoney save_error_info(fs_info); 175cf79ffb5SJosef Bacik if (sb->s_flags & MS_BORN) 176acce952bSliubo btrfs_handle_error(fs_info); 177acce952bSliubo } 1784da35113SJeff Mahoney 17957d816a1SAnand Jain #ifdef CONFIG_PRINTK 180533574c6SJoe Perches static const char * const logtypes[] = { 1814da35113SJeff Mahoney "emergency", 1824da35113SJeff Mahoney "alert", 1834da35113SJeff Mahoney "critical", 1844da35113SJeff Mahoney "error", 1854da35113SJeff Mahoney "warning", 1864da35113SJeff Mahoney "notice", 1874da35113SJeff Mahoney "info", 1884da35113SJeff Mahoney "debug", 1894da35113SJeff Mahoney }; 1904da35113SJeff Mahoney 191c2cf52ebSSimon Kirby void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...) 1924da35113SJeff Mahoney { 1934da35113SJeff Mahoney struct super_block *sb = fs_info->sb; 1944da35113SJeff Mahoney char lvl[4]; 1954da35113SJeff Mahoney struct va_format vaf; 1964da35113SJeff Mahoney va_list args; 1974da35113SJeff Mahoney const char *type = logtypes[4]; 198533574c6SJoe Perches int kern_level; 1994da35113SJeff Mahoney 2004da35113SJeff Mahoney va_start(args, fmt); 2014da35113SJeff Mahoney 202533574c6SJoe Perches kern_level = printk_get_level(fmt); 203533574c6SJoe Perches if (kern_level) { 204533574c6SJoe Perches size_t size = printk_skip_level(fmt) - fmt; 205533574c6SJoe Perches memcpy(lvl, fmt, size); 206533574c6SJoe Perches lvl[size] = '\0'; 207533574c6SJoe Perches fmt += size; 208533574c6SJoe Perches type = logtypes[kern_level - '0']; 2094da35113SJeff Mahoney } else 2104da35113SJeff Mahoney *lvl = '\0'; 2114da35113SJeff Mahoney 2124da35113SJeff Mahoney vaf.fmt = fmt; 2134da35113SJeff Mahoney vaf.va = &args; 214533574c6SJoe Perches 215c2cf52ebSSimon Kirby printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf); 216533574c6SJoe Perches 217533574c6SJoe Perches va_end(args); 2184da35113SJeff Mahoney } 219533574c6SJoe Perches #endif 220533574c6SJoe Perches 2218c342930SJeff Mahoney /* 22249b25e05SJeff Mahoney * We only mark the transaction aborted and then set the file system read-only. 22349b25e05SJeff Mahoney * This will prevent new transactions from starting or trying to join this 22449b25e05SJeff Mahoney * one. 22549b25e05SJeff Mahoney * 22649b25e05SJeff Mahoney * This means that error recovery at the call site is limited to freeing 22749b25e05SJeff Mahoney * any local memory allocations and passing the error code up without 22849b25e05SJeff Mahoney * further cleanup. The transaction should complete as it normally would 22949b25e05SJeff Mahoney * in the call path but will return -EIO. 23049b25e05SJeff Mahoney * 23149b25e05SJeff Mahoney * We'll complete the cleanup in btrfs_end_transaction and 23249b25e05SJeff Mahoney * btrfs_commit_transaction. 23349b25e05SJeff Mahoney */ 234c0d19e2bSDavid Sterba __cold 23549b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans, 23649b25e05SJeff Mahoney struct btrfs_root *root, const char *function, 23749b25e05SJeff Mahoney unsigned int line, int errno) 23849b25e05SJeff Mahoney { 23949b25e05SJeff Mahoney trans->aborted = errno; 24049b25e05SJeff Mahoney /* Nothing used. The other threads that have joined this 24149b25e05SJeff Mahoney * transaction may be able to continue. */ 242c92f6be3SFilipe Manana if (!trans->blocks_used && list_empty(&trans->new_bgs)) { 24369ce977aSMiao Xie const char *errstr; 24469ce977aSMiao Xie 24508748810SDavid Sterba errstr = btrfs_decode_error(errno); 246c2cf52ebSSimon Kirby btrfs_warn(root->fs_info, 247c2cf52ebSSimon Kirby "%s:%d: Aborting unused transaction(%s).", 24869ce977aSMiao Xie function, line, errstr); 24949b25e05SJeff Mahoney return; 25049b25e05SJeff Mahoney } 2518d25a086SMiao Xie ACCESS_ONCE(trans->transaction->aborted) = errno; 252501407aaSJosef Bacik /* Wake up anybody who may be waiting on this transaction */ 253501407aaSJosef Bacik wake_up(&root->fs_info->transaction_wait); 254501407aaSJosef Bacik wake_up(&root->fs_info->transaction_blocked_wait); 25549b25e05SJeff Mahoney __btrfs_std_error(root->fs_info, function, line, errno, NULL); 25649b25e05SJeff Mahoney } 25749b25e05SJeff Mahoney /* 2588c342930SJeff Mahoney * __btrfs_panic decodes unexpected, fatal errors from the caller, 2598c342930SJeff Mahoney * issues an alert, and either panics or BUGs, depending on mount options. 2608c342930SJeff Mahoney */ 261c0d19e2bSDavid Sterba __cold 2628c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, 2638c342930SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 2648c342930SJeff Mahoney { 2658c342930SJeff Mahoney char *s_id = "<unknown>"; 2668c342930SJeff Mahoney const char *errstr; 2678c342930SJeff Mahoney struct va_format vaf = { .fmt = fmt }; 2688c342930SJeff Mahoney va_list args; 2698c342930SJeff Mahoney 2708c342930SJeff Mahoney if (fs_info) 2718c342930SJeff Mahoney s_id = fs_info->sb->s_id; 2728c342930SJeff Mahoney 2738c342930SJeff Mahoney va_start(args, fmt); 2748c342930SJeff Mahoney vaf.va = &args; 2758c342930SJeff Mahoney 27608748810SDavid Sterba errstr = btrfs_decode_error(errno); 277aa43a17cSEric Sandeen if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR)) 27808748810SDavid Sterba panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n", 27908748810SDavid Sterba s_id, function, line, &vaf, errno, errstr); 2808c342930SJeff Mahoney 281efe120a0SFrank Holton btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)", 282efe120a0SFrank Holton function, line, &vaf, errno, errstr); 2838c342930SJeff Mahoney va_end(args); 2848c342930SJeff Mahoney /* Caller calls BUG() */ 2858c342930SJeff Mahoney } 286acce952bSliubo 287e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 288e20d96d6SChris Mason { 2893abdbd78SDavid Sterba close_ctree(btrfs_sb(sb)->tree_root); 290e20d96d6SChris Mason } 2912e635a27SChris Mason 29295e05289SChris Mason enum { 29373f73415SJosef Bacik Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum, 294287a0ab9SJosef Bacik Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, 295287a0ab9SJosef Bacik Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress, 296261507a0SLi Zefan Opt_compress_type, Opt_compress_force, Opt_compress_force_type, 297261507a0SLi Zefan Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard, 29870f6d82eSOmar Sandoval Opt_space_cache, Opt_space_cache_version, Opt_clear_cache, 29970f6d82eSOmar Sandoval Opt_user_subvol_rm_allowed, Opt_enospc_debug, Opt_subvolrootid, 30070f6d82eSOmar Sandoval Opt_defrag, Opt_inode_cache, Opt_no_space_cache, Opt_recovery, 30170f6d82eSOmar Sandoval Opt_skip_balance, Opt_check_integrity, 30270f6d82eSOmar Sandoval Opt_check_integrity_including_extent_data, 303f420ee1eSStefan Behrens Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree, 304e07a2adeSQu Wenruo Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard, 305a258af7aSQu Wenruo Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow, 3063818aea2SQu Wenruo Opt_datasum, Opt_treelog, Opt_noinode_cache, 307d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 308d0bd4560SJosef Bacik Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all, 309d0bd4560SJosef Bacik #endif 3109555c6c1SIlya Dryomov Opt_err, 31195e05289SChris Mason }; 31295e05289SChris Mason 3134d4ab6d6SDavid Sterba static const match_table_t tokens = { 314dfe25020SChris Mason {Opt_degraded, "degraded"}, 31595e05289SChris Mason {Opt_subvol, "subvol=%s"}, 3161493381fSWang Shilong {Opt_subvolid, "subvolid=%s"}, 31743e570b0SChristoph Hellwig {Opt_device, "device=%s"}, 318b6cda9bcSChris Mason {Opt_nodatasum, "nodatasum"}, 319d399167dSQu Wenruo {Opt_datasum, "datasum"}, 320be20aa9dSChris Mason {Opt_nodatacow, "nodatacow"}, 321a258af7aSQu Wenruo {Opt_datacow, "datacow"}, 32221ad10cfSChris Mason {Opt_nobarrier, "nobarrier"}, 323842bef58SQu Wenruo {Opt_barrier, "barrier"}, 3246f568d35SChris Mason {Opt_max_inline, "max_inline=%s"}, 3258f662a76SChris Mason {Opt_alloc_start, "alloc_start=%s"}, 3264543df7eSChris Mason {Opt_thread_pool, "thread_pool=%d"}, 327c8b97818SChris Mason {Opt_compress, "compress"}, 328261507a0SLi Zefan {Opt_compress_type, "compress=%s"}, 329a555f810SChris Mason {Opt_compress_force, "compress-force"}, 330261507a0SLi Zefan {Opt_compress_force_type, "compress-force=%s"}, 331e18e4809SChris Mason {Opt_ssd, "ssd"}, 332451d7585SChris Mason {Opt_ssd_spread, "ssd_spread"}, 3333b30c22fSChris Mason {Opt_nossd, "nossd"}, 334bd0330adSQu Wenruo {Opt_acl, "acl"}, 33533268eafSJosef Bacik {Opt_noacl, "noacl"}, 3363a5e1404SSage Weil {Opt_notreelog, "notreelog"}, 337a88998f2SQu Wenruo {Opt_treelog, "treelog"}, 338dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 3392c9ee856SQu Wenruo {Opt_noflushoncommit, "noflushoncommit"}, 34097e728d4SJosef Bacik {Opt_ratio, "metadata_ratio=%d"}, 341e244a0aeSChristoph Hellwig {Opt_discard, "discard"}, 342e07a2adeSQu Wenruo {Opt_nodiscard, "nodiscard"}, 3430af3d00bSJosef Bacik {Opt_space_cache, "space_cache"}, 34470f6d82eSOmar Sandoval {Opt_space_cache_version, "space_cache=%s"}, 34588c2ba3bSJosef Bacik {Opt_clear_cache, "clear_cache"}, 3464260f7c7SSage Weil {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, 34791435650SChris Mason {Opt_enospc_debug, "enospc_debug"}, 34853036293SQu Wenruo {Opt_noenospc_debug, "noenospc_debug"}, 349e15d0542SXin Zhong {Opt_subvolrootid, "subvolrootid=%d"}, 3504cb5300bSChris Mason {Opt_defrag, "autodefrag"}, 351fc0ca9afSQu Wenruo {Opt_nodefrag, "noautodefrag"}, 3524b9465cbSChris Mason {Opt_inode_cache, "inode_cache"}, 3533818aea2SQu Wenruo {Opt_noinode_cache, "noinode_cache"}, 3548965593eSDavid Sterba {Opt_no_space_cache, "nospace_cache"}, 355af31f5e5SChris Mason {Opt_recovery, "recovery"}, 3569555c6c1SIlya Dryomov {Opt_skip_balance, "skip_balance"}, 35721adbd5cSStefan Behrens {Opt_check_integrity, "check_int"}, 35821adbd5cSStefan Behrens {Opt_check_integrity_including_extent_data, "check_int_data"}, 35921adbd5cSStefan Behrens {Opt_check_integrity_print_mask, "check_int_print_mask=%d"}, 360f420ee1eSStefan Behrens {Opt_rescan_uuid_tree, "rescan_uuid_tree"}, 3618c342930SJeff Mahoney {Opt_fatal_errors, "fatal_errors=%s"}, 3628b87dc17SDavid Sterba {Opt_commit_interval, "commit=%d"}, 363d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 364d0bd4560SJosef Bacik {Opt_fragment_data, "fragment=data"}, 365d0bd4560SJosef Bacik {Opt_fragment_metadata, "fragment=metadata"}, 366d0bd4560SJosef Bacik {Opt_fragment_all, "fragment=all"}, 367d0bd4560SJosef Bacik #endif 36833268eafSJosef Bacik {Opt_err, NULL}, 36995e05289SChris Mason }; 37095e05289SChris Mason 371edf24abeSChristoph Hellwig /* 372edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 373edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 37449b25e05SJeff Mahoney * XXX JDM: This needs to be cleaned up for remount. 375edf24abeSChristoph Hellwig */ 376edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options) 37795e05289SChris Mason { 378edf24abeSChristoph Hellwig struct btrfs_fs_info *info = root->fs_info; 37995e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 38073bc1876SJosef Bacik char *p, *num, *orig = NULL; 38173bc1876SJosef Bacik u64 cache_gen; 3824543df7eSChris Mason int intarg; 383a7a3f7caSSage Weil int ret = 0; 384261507a0SLi Zefan char *compress_type; 385261507a0SLi Zefan bool compress_force = false; 386b7c47bbbSTsutomu Itoh enum btrfs_compression_type saved_compress_type; 387b7c47bbbSTsutomu Itoh bool saved_compress_force; 388b7c47bbbSTsutomu Itoh int no_compress = 0; 389b6cda9bcSChris Mason 3906c41761fSDavid Sterba cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy); 39170f6d82eSOmar Sandoval if (btrfs_fs_compat_ro(root->fs_info, FREE_SPACE_TREE)) 39270f6d82eSOmar Sandoval btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE); 39370f6d82eSOmar Sandoval else if (cache_gen) 39473bc1876SJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 39573bc1876SJosef Bacik 39695e05289SChris Mason if (!options) 39773bc1876SJosef Bacik goto out; 39895e05289SChris Mason 399be20aa9dSChris Mason /* 400be20aa9dSChris Mason * strsep changes the string, duplicate it because parse_options 401be20aa9dSChris Mason * gets called twice 402be20aa9dSChris Mason */ 403be20aa9dSChris Mason options = kstrdup(options, GFP_NOFS); 404be20aa9dSChris Mason if (!options) 405be20aa9dSChris Mason return -ENOMEM; 406be20aa9dSChris Mason 407da495eccSJosef Bacik orig = options; 408be20aa9dSChris Mason 40995e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 41095e05289SChris Mason int token; 41195e05289SChris Mason if (!*p) 41295e05289SChris Mason continue; 41395e05289SChris Mason 41495e05289SChris Mason token = match_token(p, tokens, args); 41595e05289SChris Mason switch (token) { 416dfe25020SChris Mason case Opt_degraded: 417efe120a0SFrank Holton btrfs_info(root->fs_info, "allowing degraded mounts"); 418dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 419dfe25020SChris Mason break; 42095e05289SChris Mason case Opt_subvol: 42173f73415SJosef Bacik case Opt_subvolid: 422e15d0542SXin Zhong case Opt_subvolrootid: 42343e570b0SChristoph Hellwig case Opt_device: 424edf24abeSChristoph Hellwig /* 42543e570b0SChristoph Hellwig * These are parsed by btrfs_parse_early_options 426edf24abeSChristoph Hellwig * and can be happily ignored here. 427edf24abeSChristoph Hellwig */ 42895e05289SChris Mason break; 429b6cda9bcSChris Mason case Opt_nodatasum: 43007802534SQu Wenruo btrfs_set_and_info(root, NODATASUM, 43107802534SQu Wenruo "setting nodatasum"); 432be20aa9dSChris Mason break; 433d399167dSQu Wenruo case Opt_datasum: 43407802534SQu Wenruo if (btrfs_test_opt(root, NODATASUM)) { 435d399167dSQu Wenruo if (btrfs_test_opt(root, NODATACOW)) 436d399167dSQu Wenruo btrfs_info(root->fs_info, "setting datasum, datacow enabled"); 437d399167dSQu Wenruo else 438d399167dSQu Wenruo btrfs_info(root->fs_info, "setting datasum"); 43907802534SQu Wenruo } 440d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATACOW); 441d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATASUM); 442d399167dSQu Wenruo break; 443be20aa9dSChris Mason case Opt_nodatacow: 44407802534SQu Wenruo if (!btrfs_test_opt(root, NODATACOW)) { 445bedb2ccaSAndrei Popa if (!btrfs_test_opt(root, COMPRESS) || 446bedb2ccaSAndrei Popa !btrfs_test_opt(root, FORCE_COMPRESS)) { 447efe120a0SFrank Holton btrfs_info(root->fs_info, 448efe120a0SFrank Holton "setting nodatacow, compression disabled"); 449bedb2ccaSAndrei Popa } else { 450efe120a0SFrank Holton btrfs_info(root->fs_info, "setting nodatacow"); 451bedb2ccaSAndrei Popa } 45207802534SQu Wenruo } 453bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, COMPRESS); 454bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 455be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 456be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 457b6cda9bcSChris Mason break; 458a258af7aSQu Wenruo case Opt_datacow: 45907802534SQu Wenruo btrfs_clear_and_info(root, NODATACOW, 46007802534SQu Wenruo "setting datacow"); 461a258af7aSQu Wenruo break; 462a555f810SChris Mason case Opt_compress_force: 463261507a0SLi Zefan case Opt_compress_force_type: 464261507a0SLi Zefan compress_force = true; 4651c697d4aSEric Sandeen /* Fallthrough */ 466261507a0SLi Zefan case Opt_compress: 467261507a0SLi Zefan case Opt_compress_type: 468b7c47bbbSTsutomu Itoh saved_compress_type = btrfs_test_opt(root, COMPRESS) ? 469b7c47bbbSTsutomu Itoh info->compress_type : BTRFS_COMPRESS_NONE; 470b7c47bbbSTsutomu Itoh saved_compress_force = 471b7c47bbbSTsutomu Itoh btrfs_test_opt(root, FORCE_COMPRESS); 472261507a0SLi Zefan if (token == Opt_compress || 473261507a0SLi Zefan token == Opt_compress_force || 474261507a0SLi Zefan strcmp(args[0].from, "zlib") == 0) { 475261507a0SLi Zefan compress_type = "zlib"; 476261507a0SLi Zefan info->compress_type = BTRFS_COMPRESS_ZLIB; 477063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 478bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 479bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 480b7c47bbbSTsutomu Itoh no_compress = 0; 481a6fa6faeSLi Zefan } else if (strcmp(args[0].from, "lzo") == 0) { 482a6fa6faeSLi Zefan compress_type = "lzo"; 483a6fa6faeSLi Zefan info->compress_type = BTRFS_COMPRESS_LZO; 484063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 485bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 486bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 4872b0ce2c2SMitch Harder btrfs_set_fs_incompat(info, COMPRESS_LZO); 488b7c47bbbSTsutomu Itoh no_compress = 0; 489063849eaSArnd Hannemann } else if (strncmp(args[0].from, "no", 2) == 0) { 490063849eaSArnd Hannemann compress_type = "no"; 491063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, COMPRESS); 492063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 493063849eaSArnd Hannemann compress_force = false; 494b7c47bbbSTsutomu Itoh no_compress++; 495261507a0SLi Zefan } else { 496261507a0SLi Zefan ret = -EINVAL; 497261507a0SLi Zefan goto out; 498261507a0SLi Zefan } 499261507a0SLi Zefan 500261507a0SLi Zefan if (compress_force) { 501b7c47bbbSTsutomu Itoh btrfs_set_opt(info->mount_opt, FORCE_COMPRESS); 502143f3636SDavid Sterba } else { 5034027e0f4SWang Shilong /* 5044027e0f4SWang Shilong * If we remount from compress-force=xxx to 5054027e0f4SWang Shilong * compress=xxx, we need clear FORCE_COMPRESS 5064027e0f4SWang Shilong * flag, otherwise, there is no way for users 5074027e0f4SWang Shilong * to disable forcible compression separately. 5084027e0f4SWang Shilong */ 5094027e0f4SWang Shilong btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 510a7e252afSMiao Xie } 511b7c47bbbSTsutomu Itoh if ((btrfs_test_opt(root, COMPRESS) && 512b7c47bbbSTsutomu Itoh (info->compress_type != saved_compress_type || 513b7c47bbbSTsutomu Itoh compress_force != saved_compress_force)) || 514b7c47bbbSTsutomu Itoh (!btrfs_test_opt(root, COMPRESS) && 515b7c47bbbSTsutomu Itoh no_compress == 1)) { 516b7c47bbbSTsutomu Itoh btrfs_info(root->fs_info, 517b7c47bbbSTsutomu Itoh "%s %s compression", 518b7c47bbbSTsutomu Itoh (compress_force) ? "force" : "use", 519b7c47bbbSTsutomu Itoh compress_type); 520b7c47bbbSTsutomu Itoh } 521b7c47bbbSTsutomu Itoh compress_force = false; 522a555f810SChris Mason break; 523e18e4809SChris Mason case Opt_ssd: 52407802534SQu Wenruo btrfs_set_and_info(root, SSD, 52507802534SQu Wenruo "use ssd allocation scheme"); 526e18e4809SChris Mason break; 527451d7585SChris Mason case Opt_ssd_spread: 52807802534SQu Wenruo btrfs_set_and_info(root, SSD_SPREAD, 52907802534SQu Wenruo "use spread ssd allocation scheme"); 5302aa06a35SEric Sandeen btrfs_set_opt(info->mount_opt, SSD); 531451d7585SChris Mason break; 5323b30c22fSChris Mason case Opt_nossd: 5332aa06a35SEric Sandeen btrfs_set_and_info(root, NOSSD, 53407802534SQu Wenruo "not using ssd allocation scheme"); 5353b30c22fSChris Mason btrfs_clear_opt(info->mount_opt, SSD); 5363b30c22fSChris Mason break; 537842bef58SQu Wenruo case Opt_barrier: 53807802534SQu Wenruo btrfs_clear_and_info(root, NOBARRIER, 53907802534SQu Wenruo "turning on barriers"); 540842bef58SQu Wenruo break; 54121ad10cfSChris Mason case Opt_nobarrier: 54207802534SQu Wenruo btrfs_set_and_info(root, NOBARRIER, 54307802534SQu Wenruo "turning off barriers"); 54421ad10cfSChris Mason break; 5454543df7eSChris Mason case Opt_thread_pool: 5462c334e87SWang Shilong ret = match_int(&args[0], &intarg); 5472c334e87SWang Shilong if (ret) { 5482c334e87SWang Shilong goto out; 5492c334e87SWang Shilong } else if (intarg > 0) { 5504543df7eSChris Mason info->thread_pool_size = intarg; 5512c334e87SWang Shilong } else { 5522c334e87SWang Shilong ret = -EINVAL; 5532c334e87SWang Shilong goto out; 5542c334e87SWang Shilong } 5554543df7eSChris Mason break; 5566f568d35SChris Mason case Opt_max_inline: 557edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5586f568d35SChris Mason if (num) { 55991748467SAkinobu Mita info->max_inline = memparse(num, NULL); 5606f568d35SChris Mason kfree(num); 5616f568d35SChris Mason 56215ada040SChris Mason if (info->max_inline) { 563feb5f965SMitch Harder info->max_inline = min_t(u64, 56415ada040SChris Mason info->max_inline, 56515ada040SChris Mason root->sectorsize); 56615ada040SChris Mason } 567efe120a0SFrank Holton btrfs_info(root->fs_info, "max_inline at %llu", 568c1c9ff7cSGeert Uytterhoeven info->max_inline); 5692c334e87SWang Shilong } else { 5702c334e87SWang Shilong ret = -ENOMEM; 5712c334e87SWang Shilong goto out; 5726f568d35SChris Mason } 5736f568d35SChris Mason break; 5748f662a76SChris Mason case Opt_alloc_start: 575edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5768f662a76SChris Mason if (num) { 577c018daecSMiao Xie mutex_lock(&info->chunk_mutex); 57891748467SAkinobu Mita info->alloc_start = memparse(num, NULL); 579c018daecSMiao Xie mutex_unlock(&info->chunk_mutex); 5808f662a76SChris Mason kfree(num); 581efe120a0SFrank Holton btrfs_info(root->fs_info, "allocations start at %llu", 582c1c9ff7cSGeert Uytterhoeven info->alloc_start); 5832c334e87SWang Shilong } else { 5842c334e87SWang Shilong ret = -ENOMEM; 5852c334e87SWang Shilong goto out; 5868f662a76SChris Mason } 5878f662a76SChris Mason break; 588bd0330adSQu Wenruo case Opt_acl: 58945ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL 590bd0330adSQu Wenruo root->fs_info->sb->s_flags |= MS_POSIXACL; 591bd0330adSQu Wenruo break; 59245ff35d6SGuangliang Zhao #else 59345ff35d6SGuangliang Zhao btrfs_err(root->fs_info, 59445ff35d6SGuangliang Zhao "support for ACL not compiled in!"); 59545ff35d6SGuangliang Zhao ret = -EINVAL; 59645ff35d6SGuangliang Zhao goto out; 59745ff35d6SGuangliang Zhao #endif 59833268eafSJosef Bacik case Opt_noacl: 59933268eafSJosef Bacik root->fs_info->sb->s_flags &= ~MS_POSIXACL; 60033268eafSJosef Bacik break; 6013a5e1404SSage Weil case Opt_notreelog: 60207802534SQu Wenruo btrfs_set_and_info(root, NOTREELOG, 60307802534SQu Wenruo "disabling tree log"); 6043a5e1404SSage Weil break; 605a88998f2SQu Wenruo case Opt_treelog: 60607802534SQu Wenruo btrfs_clear_and_info(root, NOTREELOG, 60707802534SQu Wenruo "enabling tree log"); 608a88998f2SQu Wenruo break; 609dccae999SSage Weil case Opt_flushoncommit: 61007802534SQu Wenruo btrfs_set_and_info(root, FLUSHONCOMMIT, 61107802534SQu Wenruo "turning on flush-on-commit"); 612dccae999SSage Weil break; 6132c9ee856SQu Wenruo case Opt_noflushoncommit: 61407802534SQu Wenruo btrfs_clear_and_info(root, FLUSHONCOMMIT, 61507802534SQu Wenruo "turning off flush-on-commit"); 6162c9ee856SQu Wenruo break; 61797e728d4SJosef Bacik case Opt_ratio: 6182c334e87SWang Shilong ret = match_int(&args[0], &intarg); 6192c334e87SWang Shilong if (ret) { 6202c334e87SWang Shilong goto out; 6212c334e87SWang Shilong } else if (intarg >= 0) { 62297e728d4SJosef Bacik info->metadata_ratio = intarg; 623efe120a0SFrank Holton btrfs_info(root->fs_info, "metadata ratio %d", 62497e728d4SJosef Bacik info->metadata_ratio); 6252c334e87SWang Shilong } else { 6262c334e87SWang Shilong ret = -EINVAL; 6272c334e87SWang Shilong goto out; 62897e728d4SJosef Bacik } 62997e728d4SJosef Bacik break; 630e244a0aeSChristoph Hellwig case Opt_discard: 63107802534SQu Wenruo btrfs_set_and_info(root, DISCARD, 63207802534SQu Wenruo "turning on discard"); 633e244a0aeSChristoph Hellwig break; 634e07a2adeSQu Wenruo case Opt_nodiscard: 63507802534SQu Wenruo btrfs_clear_and_info(root, DISCARD, 63607802534SQu Wenruo "turning off discard"); 637e07a2adeSQu Wenruo break; 6380af3d00bSJosef Bacik case Opt_space_cache: 63970f6d82eSOmar Sandoval case Opt_space_cache_version: 64070f6d82eSOmar Sandoval if (token == Opt_space_cache || 64170f6d82eSOmar Sandoval strcmp(args[0].from, "v1") == 0) { 64270f6d82eSOmar Sandoval btrfs_clear_opt(root->fs_info->mount_opt, 64370f6d82eSOmar Sandoval FREE_SPACE_TREE); 64407802534SQu Wenruo btrfs_set_and_info(root, SPACE_CACHE, 64507802534SQu Wenruo "enabling disk space caching"); 64670f6d82eSOmar Sandoval } else if (strcmp(args[0].from, "v2") == 0) { 64770f6d82eSOmar Sandoval btrfs_clear_opt(root->fs_info->mount_opt, 64870f6d82eSOmar Sandoval SPACE_CACHE); 64970f6d82eSOmar Sandoval btrfs_set_and_info(root, FREE_SPACE_TREE, 65070f6d82eSOmar Sandoval "enabling free space tree"); 65170f6d82eSOmar Sandoval } else { 65270f6d82eSOmar Sandoval ret = -EINVAL; 65370f6d82eSOmar Sandoval goto out; 65470f6d82eSOmar Sandoval } 6550de90876SJosef Bacik break; 656f420ee1eSStefan Behrens case Opt_rescan_uuid_tree: 657f420ee1eSStefan Behrens btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE); 658f420ee1eSStefan Behrens break; 65973bc1876SJosef Bacik case Opt_no_space_cache: 66070f6d82eSOmar Sandoval if (btrfs_test_opt(root, SPACE_CACHE)) { 66107802534SQu Wenruo btrfs_clear_and_info(root, SPACE_CACHE, 66207802534SQu Wenruo "disabling disk space caching"); 66370f6d82eSOmar Sandoval } 66470f6d82eSOmar Sandoval if (btrfs_test_opt(root, FREE_SPACE_TREE)) { 66570f6d82eSOmar Sandoval btrfs_clear_and_info(root, FREE_SPACE_TREE, 66670f6d82eSOmar Sandoval "disabling free space tree"); 66770f6d82eSOmar Sandoval } 66873bc1876SJosef Bacik break; 6694b9465cbSChris Mason case Opt_inode_cache: 6707e1876acSDavid Sterba btrfs_set_pending_and_info(info, INODE_MAP_CACHE, 67107802534SQu Wenruo "enabling inode map caching"); 6723818aea2SQu Wenruo break; 6733818aea2SQu Wenruo case Opt_noinode_cache: 6747e1876acSDavid Sterba btrfs_clear_pending_and_info(info, INODE_MAP_CACHE, 67507802534SQu Wenruo "disabling inode map caching"); 6764b9465cbSChris Mason break; 67788c2ba3bSJosef Bacik case Opt_clear_cache: 67807802534SQu Wenruo btrfs_set_and_info(root, CLEAR_CACHE, 67907802534SQu Wenruo "force clearing of disk cache"); 6800af3d00bSJosef Bacik break; 6814260f7c7SSage Weil case Opt_user_subvol_rm_allowed: 6824260f7c7SSage Weil btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 6834260f7c7SSage Weil break; 68491435650SChris Mason case Opt_enospc_debug: 68591435650SChris Mason btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 68691435650SChris Mason break; 68753036293SQu Wenruo case Opt_noenospc_debug: 68853036293SQu Wenruo btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG); 68953036293SQu Wenruo break; 6904cb5300bSChris Mason case Opt_defrag: 69107802534SQu Wenruo btrfs_set_and_info(root, AUTO_DEFRAG, 69207802534SQu Wenruo "enabling auto defrag"); 6934cb5300bSChris Mason break; 694fc0ca9afSQu Wenruo case Opt_nodefrag: 69507802534SQu Wenruo btrfs_clear_and_info(root, AUTO_DEFRAG, 69607802534SQu Wenruo "disabling auto defrag"); 697fc0ca9afSQu Wenruo break; 698af31f5e5SChris Mason case Opt_recovery: 699efe120a0SFrank Holton btrfs_info(root->fs_info, "enabling auto recovery"); 700af31f5e5SChris Mason btrfs_set_opt(info->mount_opt, RECOVERY); 701af31f5e5SChris Mason break; 7029555c6c1SIlya Dryomov case Opt_skip_balance: 7039555c6c1SIlya Dryomov btrfs_set_opt(info->mount_opt, SKIP_BALANCE); 7049555c6c1SIlya Dryomov break; 70521adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 70621adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 707efe120a0SFrank Holton btrfs_info(root->fs_info, 708efe120a0SFrank Holton "enabling check integrity including extent data"); 70921adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, 71021adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA); 71121adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 71221adbd5cSStefan Behrens break; 71321adbd5cSStefan Behrens case Opt_check_integrity: 714efe120a0SFrank Holton btrfs_info(root->fs_info, "enabling check integrity"); 71521adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 71621adbd5cSStefan Behrens break; 71721adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 7182c334e87SWang Shilong ret = match_int(&args[0], &intarg); 7192c334e87SWang Shilong if (ret) { 7202c334e87SWang Shilong goto out; 7212c334e87SWang Shilong } else if (intarg >= 0) { 72221adbd5cSStefan Behrens info->check_integrity_print_mask = intarg; 723efe120a0SFrank Holton btrfs_info(root->fs_info, "check_integrity_print_mask 0x%x", 72421adbd5cSStefan Behrens info->check_integrity_print_mask); 7252c334e87SWang Shilong } else { 7262c334e87SWang Shilong ret = -EINVAL; 7272c334e87SWang Shilong goto out; 72821adbd5cSStefan Behrens } 72921adbd5cSStefan Behrens break; 73021adbd5cSStefan Behrens #else 73121adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 73221adbd5cSStefan Behrens case Opt_check_integrity: 73321adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 734efe120a0SFrank Holton btrfs_err(root->fs_info, 735efe120a0SFrank Holton "support for check_integrity* not compiled in!"); 73621adbd5cSStefan Behrens ret = -EINVAL; 73721adbd5cSStefan Behrens goto out; 73821adbd5cSStefan Behrens #endif 7398c342930SJeff Mahoney case Opt_fatal_errors: 7408c342930SJeff Mahoney if (strcmp(args[0].from, "panic") == 0) 7418c342930SJeff Mahoney btrfs_set_opt(info->mount_opt, 7428c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7438c342930SJeff Mahoney else if (strcmp(args[0].from, "bug") == 0) 7448c342930SJeff Mahoney btrfs_clear_opt(info->mount_opt, 7458c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7468c342930SJeff Mahoney else { 7478c342930SJeff Mahoney ret = -EINVAL; 7488c342930SJeff Mahoney goto out; 7498c342930SJeff Mahoney } 7508c342930SJeff Mahoney break; 7518b87dc17SDavid Sterba case Opt_commit_interval: 7528b87dc17SDavid Sterba intarg = 0; 7538b87dc17SDavid Sterba ret = match_int(&args[0], &intarg); 7548b87dc17SDavid Sterba if (ret < 0) { 755efe120a0SFrank Holton btrfs_err(root->fs_info, "invalid commit interval"); 7568b87dc17SDavid Sterba ret = -EINVAL; 7578b87dc17SDavid Sterba goto out; 7588b87dc17SDavid Sterba } 7598b87dc17SDavid Sterba if (intarg > 0) { 7608b87dc17SDavid Sterba if (intarg > 300) { 761efe120a0SFrank Holton btrfs_warn(root->fs_info, "excessive commit interval %d", 7628b87dc17SDavid Sterba intarg); 7638b87dc17SDavid Sterba } 7648b87dc17SDavid Sterba info->commit_interval = intarg; 7658b87dc17SDavid Sterba } else { 766efe120a0SFrank Holton btrfs_info(root->fs_info, "using default commit interval %ds", 7678b87dc17SDavid Sterba BTRFS_DEFAULT_COMMIT_INTERVAL); 7688b87dc17SDavid Sterba info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 7698b87dc17SDavid Sterba } 7708b87dc17SDavid Sterba break; 771d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 772d0bd4560SJosef Bacik case Opt_fragment_all: 773d0bd4560SJosef Bacik btrfs_info(root->fs_info, "fragmenting all space"); 774d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 775d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA); 776d0bd4560SJosef Bacik break; 777d0bd4560SJosef Bacik case Opt_fragment_metadata: 778d0bd4560SJosef Bacik btrfs_info(root->fs_info, "fragmenting metadata"); 779d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, 780d0bd4560SJosef Bacik FRAGMENT_METADATA); 781d0bd4560SJosef Bacik break; 782d0bd4560SJosef Bacik case Opt_fragment_data: 783d0bd4560SJosef Bacik btrfs_info(root->fs_info, "fragmenting data"); 784d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 785d0bd4560SJosef Bacik break; 786d0bd4560SJosef Bacik #endif 787a7a3f7caSSage Weil case Opt_err: 788efe120a0SFrank Holton btrfs_info(root->fs_info, "unrecognized mount option '%s'", p); 789a7a3f7caSSage Weil ret = -EINVAL; 790a7a3f7caSSage Weil goto out; 79195e05289SChris Mason default: 792be20aa9dSChris Mason break; 79395e05289SChris Mason } 79495e05289SChris Mason } 795a7a3f7caSSage Weil out: 79670f6d82eSOmar Sandoval if (btrfs_fs_compat_ro(root->fs_info, FREE_SPACE_TREE) && 79770f6d82eSOmar Sandoval !btrfs_test_opt(root, FREE_SPACE_TREE) && 79870f6d82eSOmar Sandoval !btrfs_test_opt(root, CLEAR_CACHE)) { 79970f6d82eSOmar Sandoval btrfs_err(root->fs_info, "cannot disable free space tree"); 80070f6d82eSOmar Sandoval ret = -EINVAL; 80170f6d82eSOmar Sandoval 80270f6d82eSOmar Sandoval } 80373bc1876SJosef Bacik if (!ret && btrfs_test_opt(root, SPACE_CACHE)) 804efe120a0SFrank Holton btrfs_info(root->fs_info, "disk space caching is enabled"); 80570f6d82eSOmar Sandoval if (!ret && btrfs_test_opt(root, FREE_SPACE_TREE)) 80670f6d82eSOmar Sandoval btrfs_info(root->fs_info, "using free space tree"); 807da495eccSJosef Bacik kfree(orig); 808a7a3f7caSSage Weil return ret; 809edf24abeSChristoph Hellwig } 810edf24abeSChristoph Hellwig 811edf24abeSChristoph Hellwig /* 812edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 813edf24abeSChristoph Hellwig * 814edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 815edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 816edf24abeSChristoph Hellwig */ 81797288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 81873f73415SJosef Bacik void *holder, char **subvol_name, u64 *subvol_objectid, 8195e2a4b25SDavid Sterba struct btrfs_fs_devices **fs_devices) 820edf24abeSChristoph Hellwig { 821edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 82283c8c9bdSJeff Liu char *device_name, *opts, *orig, *p; 8231493381fSWang Shilong char *num = NULL; 824edf24abeSChristoph Hellwig int error = 0; 825edf24abeSChristoph Hellwig 826edf24abeSChristoph Hellwig if (!options) 827830c4adbSJosef Bacik return 0; 828edf24abeSChristoph Hellwig 829edf24abeSChristoph Hellwig /* 830edf24abeSChristoph Hellwig * strsep changes the string, duplicate it because parse_options 831edf24abeSChristoph Hellwig * gets called twice 832edf24abeSChristoph Hellwig */ 833edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 834edf24abeSChristoph Hellwig if (!opts) 835edf24abeSChristoph Hellwig return -ENOMEM; 8363f3d0bc0STero Roponen orig = opts; 837edf24abeSChristoph Hellwig 838edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 839edf24abeSChristoph Hellwig int token; 840edf24abeSChristoph Hellwig if (!*p) 841edf24abeSChristoph Hellwig continue; 842edf24abeSChristoph Hellwig 843edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 844edf24abeSChristoph Hellwig switch (token) { 845edf24abeSChristoph Hellwig case Opt_subvol: 846a90e8b6fSIlya Dryomov kfree(*subvol_name); 847edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 8482c334e87SWang Shilong if (!*subvol_name) { 8492c334e87SWang Shilong error = -ENOMEM; 8502c334e87SWang Shilong goto out; 8512c334e87SWang Shilong } 852edf24abeSChristoph Hellwig break; 85373f73415SJosef Bacik case Opt_subvolid: 8541493381fSWang Shilong num = match_strdup(&args[0]); 8551493381fSWang Shilong if (num) { 8561493381fSWang Shilong *subvol_objectid = memparse(num, NULL); 8571493381fSWang Shilong kfree(num); 8584849f01dSJosef Bacik /* we want the original fs_tree */ 8591493381fSWang Shilong if (!*subvol_objectid) 8604849f01dSJosef Bacik *subvol_objectid = 8614849f01dSJosef Bacik BTRFS_FS_TREE_OBJECTID; 8622c334e87SWang Shilong } else { 8632c334e87SWang Shilong error = -EINVAL; 8642c334e87SWang Shilong goto out; 8654849f01dSJosef Bacik } 86673f73415SJosef Bacik break; 867e15d0542SXin Zhong case Opt_subvolrootid: 8685e2a4b25SDavid Sterba printk(KERN_WARNING 869efe120a0SFrank Holton "BTRFS: 'subvolrootid' mount option is deprecated and has " 870efe120a0SFrank Holton "no effect\n"); 871e15d0542SXin Zhong break; 87243e570b0SChristoph Hellwig case Opt_device: 87383c8c9bdSJeff Liu device_name = match_strdup(&args[0]); 87483c8c9bdSJeff Liu if (!device_name) { 87583c8c9bdSJeff Liu error = -ENOMEM; 87683c8c9bdSJeff Liu goto out; 87783c8c9bdSJeff Liu } 87883c8c9bdSJeff Liu error = btrfs_scan_one_device(device_name, 87943e570b0SChristoph Hellwig flags, holder, fs_devices); 88083c8c9bdSJeff Liu kfree(device_name); 88143e570b0SChristoph Hellwig if (error) 882830c4adbSJosef Bacik goto out; 88343e570b0SChristoph Hellwig break; 884edf24abeSChristoph Hellwig default: 885edf24abeSChristoph Hellwig break; 886edf24abeSChristoph Hellwig } 887edf24abeSChristoph Hellwig } 888edf24abeSChristoph Hellwig 889edf24abeSChristoph Hellwig out: 890830c4adbSJosef Bacik kfree(orig); 891edf24abeSChristoph Hellwig return error; 89295e05289SChris Mason } 89395e05289SChris Mason 89405dbe683SOmar Sandoval static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info, 89573f73415SJosef Bacik u64 subvol_objectid) 89673f73415SJosef Bacik { 897815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 89805dbe683SOmar Sandoval struct btrfs_root *fs_root; 89905dbe683SOmar Sandoval struct btrfs_root_ref *root_ref; 90005dbe683SOmar Sandoval struct btrfs_inode_ref *inode_ref; 90105dbe683SOmar Sandoval struct btrfs_key key; 90205dbe683SOmar Sandoval struct btrfs_path *path = NULL; 90305dbe683SOmar Sandoval char *name = NULL, *ptr; 90405dbe683SOmar Sandoval u64 dirid; 90505dbe683SOmar Sandoval int len; 90605dbe683SOmar Sandoval int ret; 90705dbe683SOmar Sandoval 90805dbe683SOmar Sandoval path = btrfs_alloc_path(); 90905dbe683SOmar Sandoval if (!path) { 91005dbe683SOmar Sandoval ret = -ENOMEM; 91105dbe683SOmar Sandoval goto err; 91205dbe683SOmar Sandoval } 91305dbe683SOmar Sandoval path->leave_spinning = 1; 91405dbe683SOmar Sandoval 91505dbe683SOmar Sandoval name = kmalloc(PATH_MAX, GFP_NOFS); 91605dbe683SOmar Sandoval if (!name) { 91705dbe683SOmar Sandoval ret = -ENOMEM; 91805dbe683SOmar Sandoval goto err; 91905dbe683SOmar Sandoval } 92005dbe683SOmar Sandoval ptr = name + PATH_MAX - 1; 92105dbe683SOmar Sandoval ptr[0] = '\0'; 92205dbe683SOmar Sandoval 92305dbe683SOmar Sandoval /* 92405dbe683SOmar Sandoval * Walk up the subvolume trees in the tree of tree roots by root 92505dbe683SOmar Sandoval * backrefs until we hit the top-level subvolume. 92605dbe683SOmar Sandoval */ 92705dbe683SOmar Sandoval while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) { 92805dbe683SOmar Sandoval key.objectid = subvol_objectid; 92905dbe683SOmar Sandoval key.type = BTRFS_ROOT_BACKREF_KEY; 93005dbe683SOmar Sandoval key.offset = (u64)-1; 93105dbe683SOmar Sandoval 93205dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 93305dbe683SOmar Sandoval if (ret < 0) { 93405dbe683SOmar Sandoval goto err; 93505dbe683SOmar Sandoval } else if (ret > 0) { 93605dbe683SOmar Sandoval ret = btrfs_previous_item(root, path, subvol_objectid, 93705dbe683SOmar Sandoval BTRFS_ROOT_BACKREF_KEY); 93805dbe683SOmar Sandoval if (ret < 0) { 93905dbe683SOmar Sandoval goto err; 94005dbe683SOmar Sandoval } else if (ret > 0) { 94105dbe683SOmar Sandoval ret = -ENOENT; 94205dbe683SOmar Sandoval goto err; 94305dbe683SOmar Sandoval } 94405dbe683SOmar Sandoval } 94505dbe683SOmar Sandoval 94605dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 94705dbe683SOmar Sandoval subvol_objectid = key.offset; 94805dbe683SOmar Sandoval 94905dbe683SOmar Sandoval root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], 95005dbe683SOmar Sandoval struct btrfs_root_ref); 95105dbe683SOmar Sandoval len = btrfs_root_ref_name_len(path->nodes[0], root_ref); 95205dbe683SOmar Sandoval ptr -= len + 1; 95305dbe683SOmar Sandoval if (ptr < name) { 95405dbe683SOmar Sandoval ret = -ENAMETOOLONG; 95505dbe683SOmar Sandoval goto err; 95605dbe683SOmar Sandoval } 95705dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 95805dbe683SOmar Sandoval (unsigned long)(root_ref + 1), len); 95905dbe683SOmar Sandoval ptr[0] = '/'; 96005dbe683SOmar Sandoval dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref); 96105dbe683SOmar Sandoval btrfs_release_path(path); 96205dbe683SOmar Sandoval 96305dbe683SOmar Sandoval key.objectid = subvol_objectid; 96405dbe683SOmar Sandoval key.type = BTRFS_ROOT_ITEM_KEY; 96505dbe683SOmar Sandoval key.offset = (u64)-1; 96605dbe683SOmar Sandoval fs_root = btrfs_read_fs_root_no_name(fs_info, &key); 96705dbe683SOmar Sandoval if (IS_ERR(fs_root)) { 96805dbe683SOmar Sandoval ret = PTR_ERR(fs_root); 96905dbe683SOmar Sandoval goto err; 97005dbe683SOmar Sandoval } 97105dbe683SOmar Sandoval 97205dbe683SOmar Sandoval /* 97305dbe683SOmar Sandoval * Walk up the filesystem tree by inode refs until we hit the 97405dbe683SOmar Sandoval * root directory. 97505dbe683SOmar Sandoval */ 97605dbe683SOmar Sandoval while (dirid != BTRFS_FIRST_FREE_OBJECTID) { 97705dbe683SOmar Sandoval key.objectid = dirid; 97805dbe683SOmar Sandoval key.type = BTRFS_INODE_REF_KEY; 97905dbe683SOmar Sandoval key.offset = (u64)-1; 98005dbe683SOmar Sandoval 98105dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); 98205dbe683SOmar Sandoval if (ret < 0) { 98305dbe683SOmar Sandoval goto err; 98405dbe683SOmar Sandoval } else if (ret > 0) { 98505dbe683SOmar Sandoval ret = btrfs_previous_item(fs_root, path, dirid, 98605dbe683SOmar Sandoval BTRFS_INODE_REF_KEY); 98705dbe683SOmar Sandoval if (ret < 0) { 98805dbe683SOmar Sandoval goto err; 98905dbe683SOmar Sandoval } else if (ret > 0) { 99005dbe683SOmar Sandoval ret = -ENOENT; 99105dbe683SOmar Sandoval goto err; 99205dbe683SOmar Sandoval } 99305dbe683SOmar Sandoval } 99405dbe683SOmar Sandoval 99505dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 99605dbe683SOmar Sandoval dirid = key.offset; 99705dbe683SOmar Sandoval 99805dbe683SOmar Sandoval inode_ref = btrfs_item_ptr(path->nodes[0], 99905dbe683SOmar Sandoval path->slots[0], 100005dbe683SOmar Sandoval struct btrfs_inode_ref); 100105dbe683SOmar Sandoval len = btrfs_inode_ref_name_len(path->nodes[0], 100205dbe683SOmar Sandoval inode_ref); 100305dbe683SOmar Sandoval ptr -= len + 1; 100405dbe683SOmar Sandoval if (ptr < name) { 100505dbe683SOmar Sandoval ret = -ENAMETOOLONG; 100605dbe683SOmar Sandoval goto err; 100705dbe683SOmar Sandoval } 100805dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 100905dbe683SOmar Sandoval (unsigned long)(inode_ref + 1), len); 101005dbe683SOmar Sandoval ptr[0] = '/'; 101105dbe683SOmar Sandoval btrfs_release_path(path); 101205dbe683SOmar Sandoval } 101305dbe683SOmar Sandoval } 101405dbe683SOmar Sandoval 101505dbe683SOmar Sandoval btrfs_free_path(path); 101605dbe683SOmar Sandoval if (ptr == name + PATH_MAX - 1) { 101705dbe683SOmar Sandoval name[0] = '/'; 101805dbe683SOmar Sandoval name[1] = '\0'; 101905dbe683SOmar Sandoval } else { 102005dbe683SOmar Sandoval memmove(name, ptr, name + PATH_MAX - ptr); 102105dbe683SOmar Sandoval } 102205dbe683SOmar Sandoval return name; 102305dbe683SOmar Sandoval 102405dbe683SOmar Sandoval err: 102505dbe683SOmar Sandoval btrfs_free_path(path); 102605dbe683SOmar Sandoval kfree(name); 102705dbe683SOmar Sandoval return ERR_PTR(ret); 102805dbe683SOmar Sandoval } 102905dbe683SOmar Sandoval 103005dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid) 103105dbe683SOmar Sandoval { 103205dbe683SOmar Sandoval struct btrfs_root *root = fs_info->tree_root; 103373f73415SJosef Bacik struct btrfs_dir_item *di; 103473f73415SJosef Bacik struct btrfs_path *path; 103573f73415SJosef Bacik struct btrfs_key location; 103673f73415SJosef Bacik u64 dir_id; 103773f73415SJosef Bacik 103873f73415SJosef Bacik path = btrfs_alloc_path(); 103973f73415SJosef Bacik if (!path) 104005dbe683SOmar Sandoval return -ENOMEM; 104173f73415SJosef Bacik path->leave_spinning = 1; 104273f73415SJosef Bacik 104373f73415SJosef Bacik /* 104473f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 104573f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 104673f73415SJosef Bacik * to mount. 104773f73415SJosef Bacik */ 1048815745cfSAl Viro dir_id = btrfs_super_root_dir(fs_info->super_copy); 104973f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 1050b0839166SJulia Lawall if (IS_ERR(di)) { 1051b0839166SJulia Lawall btrfs_free_path(path); 105205dbe683SOmar Sandoval return PTR_ERR(di); 1053b0839166SJulia Lawall } 105473f73415SJosef Bacik if (!di) { 105573f73415SJosef Bacik /* 105673f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 105773f73415SJosef Bacik * it's always been there, but don't freak out, just try and 105805dbe683SOmar Sandoval * mount the top-level subvolume. 105973f73415SJosef Bacik */ 106073f73415SJosef Bacik btrfs_free_path(path); 106105dbe683SOmar Sandoval *objectid = BTRFS_FS_TREE_OBJECTID; 106205dbe683SOmar Sandoval return 0; 106373f73415SJosef Bacik } 106473f73415SJosef Bacik 106573f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 106673f73415SJosef Bacik btrfs_free_path(path); 106705dbe683SOmar Sandoval *objectid = location.objectid; 106805dbe683SOmar Sandoval return 0; 106973f73415SJosef Bacik } 107073f73415SJosef Bacik 10718a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 10728a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 10738a4b83ccSChris Mason void *data, int silent) 10742e635a27SChris Mason { 10752e635a27SChris Mason struct inode *inode; 1076815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 10775d4f98a2SYan Zheng struct btrfs_key key; 107839279cc3SChris Mason int err; 10792e635a27SChris Mason 10802e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 10812e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 1082e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 1083af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 1084be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 10855103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 10862e635a27SChris Mason sb->s_time_gran = 1; 10870eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 108833268eafSJosef Bacik sb->s_flags |= MS_POSIXACL; 108949cf6f45SChris Ball #endif 10900c4d2d95SJosef Bacik sb->s_flags |= MS_I_VERSION; 1091da2f0f74SChris Mason sb->s_iflags |= SB_I_CGROUPWB; 1092ad2b2c80SAl Viro err = open_ctree(sb, fs_devices, (char *)data); 1093ad2b2c80SAl Viro if (err) { 1094efe120a0SFrank Holton printk(KERN_ERR "BTRFS: open_ctree failed\n"); 1095ad2b2c80SAl Viro return err; 1096e20d96d6SChris Mason } 1097b888db2bSChris Mason 10985d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 10995d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 11005d4f98a2SYan Zheng key.offset = 0; 110198c7089cSAl Viro inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL); 11025d4f98a2SYan Zheng if (IS_ERR(inode)) { 11035d4f98a2SYan Zheng err = PTR_ERR(inode); 110439279cc3SChris Mason goto fail_close; 110539279cc3SChris Mason } 11062e635a27SChris Mason 110748fde701SAl Viro sb->s_root = d_make_root(inode); 110848fde701SAl Viro if (!sb->s_root) { 110939279cc3SChris Mason err = -ENOMEM; 111039279cc3SChris Mason goto fail_close; 11112e635a27SChris Mason } 111258176a96SJosef Bacik 11136885f308SChris Mason save_mount_options(sb, data); 111490a887c9SDan Magenheimer cleancache_init_fs(sb); 111559553edfSAl Viro sb->s_flags |= MS_ACTIVE; 11162e635a27SChris Mason return 0; 11172e635a27SChris Mason 111839279cc3SChris Mason fail_close: 1119815745cfSAl Viro close_ctree(fs_info->tree_root); 1120d5719762SChris Mason return err; 1121d5719762SChris Mason } 1122d5719762SChris Mason 11236bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 1124d5719762SChris Mason { 1125d5719762SChris Mason struct btrfs_trans_handle *trans; 1126815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1127815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 1128df2ce34cSChris Mason 11291abe9b8aSliubo trace_btrfs_sync_fs(wait); 11301abe9b8aSliubo 1131d561c025SChris Mason if (!wait) { 1132815745cfSAl Viro filemap_flush(fs_info->btree_inode->i_mapping); 1133df2ce34cSChris Mason return 0; 1134d561c025SChris Mason } 1135771ed689SChris Mason 1136b0244199SMiao Xie btrfs_wait_ordered_roots(fs_info, -1); 1137771ed689SChris Mason 1138d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 113960376ce4SJosef Bacik if (IS_ERR(trans)) { 1140354aa0fbSMiao Xie /* no transaction, don't bother */ 11416b5fe46dSDavid Sterba if (PTR_ERR(trans) == -ENOENT) { 11426b5fe46dSDavid Sterba /* 11436b5fe46dSDavid Sterba * Exit unless we have some pending changes 11446b5fe46dSDavid Sterba * that need to go through commit 11456b5fe46dSDavid Sterba */ 11466b5fe46dSDavid Sterba if (fs_info->pending_changes == 0) 1147bd7de2c9SJosef Bacik return 0; 1148a53f4f8eSQu Wenruo /* 1149a53f4f8eSQu Wenruo * A non-blocking test if the fs is frozen. We must not 1150a53f4f8eSQu Wenruo * start a new transaction here otherwise a deadlock 1151a53f4f8eSQu Wenruo * happens. The pending operations are delayed to the 1152a53f4f8eSQu Wenruo * next commit after thawing. 1153a53f4f8eSQu Wenruo */ 1154a53f4f8eSQu Wenruo if (__sb_start_write(sb, SB_FREEZE_WRITE, false)) 1155a53f4f8eSQu Wenruo __sb_end_write(sb, SB_FREEZE_WRITE); 1156a53f4f8eSQu Wenruo else 1157a53f4f8eSQu Wenruo return 0; 11586b5fe46dSDavid Sterba trans = btrfs_start_transaction(root, 0); 115960376ce4SJosef Bacik } 116098bd5c54SDavid Sterba if (IS_ERR(trans)) 116198bd5c54SDavid Sterba return PTR_ERR(trans); 11626b5fe46dSDavid Sterba } 1163bd7de2c9SJosef Bacik return btrfs_commit_transaction(trans, root); 1164d5719762SChris Mason } 1165d5719762SChris Mason 116634c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 1167a9572a15SEric Paris { 1168815745cfSAl Viro struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 1169815745cfSAl Viro struct btrfs_root *root = info->tree_root; 1170200da64eSTsutomu Itoh char *compress_type; 1171a9572a15SEric Paris 1172a9572a15SEric Paris if (btrfs_test_opt(root, DEGRADED)) 1173a9572a15SEric Paris seq_puts(seq, ",degraded"); 1174a9572a15SEric Paris if (btrfs_test_opt(root, NODATASUM)) 1175a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 1176a9572a15SEric Paris if (btrfs_test_opt(root, NODATACOW)) 1177a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 1178a9572a15SEric Paris if (btrfs_test_opt(root, NOBARRIER)) 1179a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 118095ac567aSFilipe David Borba Manana if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE) 1181c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",max_inline=%llu", info->max_inline); 1182a9572a15SEric Paris if (info->alloc_start != 0) 1183c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",alloc_start=%llu", info->alloc_start); 1184a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 1185a9572a15SEric Paris num_online_cpus() + 2, 8)) 1186a9572a15SEric Paris seq_printf(seq, ",thread_pool=%d", info->thread_pool_size); 1187200da64eSTsutomu Itoh if (btrfs_test_opt(root, COMPRESS)) { 1188200da64eSTsutomu Itoh if (info->compress_type == BTRFS_COMPRESS_ZLIB) 1189200da64eSTsutomu Itoh compress_type = "zlib"; 1190200da64eSTsutomu Itoh else 1191200da64eSTsutomu Itoh compress_type = "lzo"; 1192200da64eSTsutomu Itoh if (btrfs_test_opt(root, FORCE_COMPRESS)) 1193200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 1194200da64eSTsutomu Itoh else 1195200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 1196200da64eSTsutomu Itoh } 1197c289811cSChris Mason if (btrfs_test_opt(root, NOSSD)) 1198c289811cSChris Mason seq_puts(seq, ",nossd"); 1199451d7585SChris Mason if (btrfs_test_opt(root, SSD_SPREAD)) 1200451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 1201451d7585SChris Mason else if (btrfs_test_opt(root, SSD)) 1202a9572a15SEric Paris seq_puts(seq, ",ssd"); 12033a5e1404SSage Weil if (btrfs_test_opt(root, NOTREELOG)) 12046b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 1205dccae999SSage Weil if (btrfs_test_opt(root, FLUSHONCOMMIT)) 12066b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 120720a5239aSMatthew Wilcox if (btrfs_test_opt(root, DISCARD)) 120820a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 1209a9572a15SEric Paris if (!(root->fs_info->sb->s_flags & MS_POSIXACL)) 1210a9572a15SEric Paris seq_puts(seq, ",noacl"); 1211200da64eSTsutomu Itoh if (btrfs_test_opt(root, SPACE_CACHE)) 1212200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 121370f6d82eSOmar Sandoval else if (btrfs_test_opt(root, FREE_SPACE_TREE)) 121470f6d82eSOmar Sandoval seq_puts(seq, ",space_cache=v2"); 121573bc1876SJosef Bacik else 12168965593eSDavid Sterba seq_puts(seq, ",nospace_cache"); 1217f420ee1eSStefan Behrens if (btrfs_test_opt(root, RESCAN_UUID_TREE)) 1218f420ee1eSStefan Behrens seq_puts(seq, ",rescan_uuid_tree"); 1219200da64eSTsutomu Itoh if (btrfs_test_opt(root, CLEAR_CACHE)) 1220200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 1221200da64eSTsutomu Itoh if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 1222200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 12230942caa3SDavid Sterba if (btrfs_test_opt(root, ENOSPC_DEBUG)) 12240942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 12250942caa3SDavid Sterba if (btrfs_test_opt(root, AUTO_DEFRAG)) 12260942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 12270942caa3SDavid Sterba if (btrfs_test_opt(root, INODE_MAP_CACHE)) 12280942caa3SDavid Sterba seq_puts(seq, ",inode_cache"); 12299555c6c1SIlya Dryomov if (btrfs_test_opt(root, SKIP_BALANCE)) 12309555c6c1SIlya Dryomov seq_puts(seq, ",skip_balance"); 12318507d216SWang Shilong if (btrfs_test_opt(root, RECOVERY)) 12328507d216SWang Shilong seq_puts(seq, ",recovery"); 12338507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 12348507d216SWang Shilong if (btrfs_test_opt(root, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA)) 12358507d216SWang Shilong seq_puts(seq, ",check_int_data"); 12368507d216SWang Shilong else if (btrfs_test_opt(root, CHECK_INTEGRITY)) 12378507d216SWang Shilong seq_puts(seq, ",check_int"); 12388507d216SWang Shilong if (info->check_integrity_print_mask) 12398507d216SWang Shilong seq_printf(seq, ",check_int_print_mask=%d", 12408507d216SWang Shilong info->check_integrity_print_mask); 12418507d216SWang Shilong #endif 12428507d216SWang Shilong if (info->metadata_ratio) 12438507d216SWang Shilong seq_printf(seq, ",metadata_ratio=%d", 12448507d216SWang Shilong info->metadata_ratio); 12458c342930SJeff Mahoney if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR)) 12468c342930SJeff Mahoney seq_puts(seq, ",fatal_errors=panic"); 12478b87dc17SDavid Sterba if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL) 12488b87dc17SDavid Sterba seq_printf(seq, ",commit=%d", info->commit_interval); 1249d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1250d0bd4560SJosef Bacik if (btrfs_test_opt(root, FRAGMENT_DATA)) 1251d0bd4560SJosef Bacik seq_puts(seq, ",fragment=data"); 1252d0bd4560SJosef Bacik if (btrfs_test_opt(root, FRAGMENT_METADATA)) 1253d0bd4560SJosef Bacik seq_puts(seq, ",fragment=metadata"); 1254d0bd4560SJosef Bacik #endif 1255c8d3fe02SOmar Sandoval seq_printf(seq, ",subvolid=%llu", 1256c8d3fe02SOmar Sandoval BTRFS_I(d_inode(dentry))->root->root_key.objectid); 1257c8d3fe02SOmar Sandoval seq_puts(seq, ",subvol="); 1258c8d3fe02SOmar Sandoval seq_dentry(seq, dentry, " \t\n\\"); 1259a9572a15SEric Paris return 0; 1260a9572a15SEric Paris } 1261a9572a15SEric Paris 1262a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 12632e635a27SChris Mason { 1264815745cfSAl Viro struct btrfs_fs_info *p = data; 1265815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(s); 12664b82d6e4SYan 1267815745cfSAl Viro return fs_info->fs_devices == p->fs_devices; 12684b82d6e4SYan } 12694b82d6e4SYan 1270450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 1271450ba0eaSJosef Bacik { 12726de1d09dSAl Viro int err = set_anon_super(s, data); 12736de1d09dSAl Viro if (!err) 1274450ba0eaSJosef Bacik s->s_fs_info = data; 12756de1d09dSAl Viro return err; 1276450ba0eaSJosef Bacik } 1277450ba0eaSJosef Bacik 1278830c4adbSJosef Bacik /* 1279f9d9ef62SDavid Sterba * subvolumes are identified by ino 256 1280f9d9ef62SDavid Sterba */ 1281f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode) 1282f9d9ef62SDavid Sterba { 1283f9d9ef62SDavid Sterba if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 1284f9d9ef62SDavid Sterba return 1; 1285f9d9ef62SDavid Sterba return 0; 1286f9d9ef62SDavid Sterba } 1287f9d9ef62SDavid Sterba 1288f9d9ef62SDavid Sterba /* 1289e6e4dbe8SOmar Sandoval * This will add subvolid=0 to the argument string while removing any subvol= 1290e6e4dbe8SOmar Sandoval * and subvolid= arguments to make sure we get the top-level root for path 1291e6e4dbe8SOmar Sandoval * walking to the subvol we want. 1292830c4adbSJosef Bacik */ 1293830c4adbSJosef Bacik static char *setup_root_args(char *args) 1294830c4adbSJosef Bacik { 1295e6e4dbe8SOmar Sandoval char *buf, *dst, *sep; 1296830c4adbSJosef Bacik 1297e6e4dbe8SOmar Sandoval if (!args) 1298e6e4dbe8SOmar Sandoval return kstrdup("subvolid=0", GFP_NOFS); 1299830c4adbSJosef Bacik 1300e6e4dbe8SOmar Sandoval /* The worst case is that we add ",subvolid=0" to the end. */ 1301e6e4dbe8SOmar Sandoval buf = dst = kmalloc(strlen(args) + strlen(",subvolid=0") + 1, GFP_NOFS); 1302f60d16a8SJim Meyering if (!buf) 1303f60d16a8SJim Meyering return NULL; 1304830c4adbSJosef Bacik 1305e6e4dbe8SOmar Sandoval while (1) { 1306e6e4dbe8SOmar Sandoval sep = strchrnul(args, ','); 1307e6e4dbe8SOmar Sandoval if (!strstarts(args, "subvol=") && 1308e6e4dbe8SOmar Sandoval !strstarts(args, "subvolid=")) { 1309e6e4dbe8SOmar Sandoval memcpy(dst, args, sep - args); 1310e6e4dbe8SOmar Sandoval dst += sep - args; 1311e6e4dbe8SOmar Sandoval *dst++ = ','; 1312830c4adbSJosef Bacik } 1313e6e4dbe8SOmar Sandoval if (*sep) 1314e6e4dbe8SOmar Sandoval args = sep + 1; 1315e6e4dbe8SOmar Sandoval else 1316e6e4dbe8SOmar Sandoval break; 1317e6e4dbe8SOmar Sandoval } 1318f60d16a8SJim Meyering strcpy(dst, "subvolid=0"); 1319830c4adbSJosef Bacik 1320f60d16a8SJim Meyering return buf; 1321830c4adbSJosef Bacik } 1322830c4adbSJosef Bacik 1323bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid, 1324bb289b7bSOmar Sandoval int flags, const char *device_name, 1325bb289b7bSOmar Sandoval char *data) 1326830c4adbSJosef Bacik { 1327830c4adbSJosef Bacik struct dentry *root; 1328fa330659SOmar Sandoval struct vfsmount *mnt = NULL; 1329830c4adbSJosef Bacik char *newargs; 1330fa330659SOmar Sandoval int ret; 1331830c4adbSJosef Bacik 1332830c4adbSJosef Bacik newargs = setup_root_args(data); 1333fa330659SOmar Sandoval if (!newargs) { 1334fa330659SOmar Sandoval root = ERR_PTR(-ENOMEM); 1335fa330659SOmar Sandoval goto out; 1336fa330659SOmar Sandoval } 13370723a047SHarald Hoyer 1338fa330659SOmar Sandoval mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, newargs); 1339fa330659SOmar Sandoval if (PTR_ERR_OR_ZERO(mnt) == -EBUSY) { 13400723a047SHarald Hoyer if (flags & MS_RDONLY) { 1341fa330659SOmar Sandoval mnt = vfs_kern_mount(&btrfs_fs_type, flags & ~MS_RDONLY, 1342fa330659SOmar Sandoval device_name, newargs); 13430723a047SHarald Hoyer } else { 1344fa330659SOmar Sandoval mnt = vfs_kern_mount(&btrfs_fs_type, flags | MS_RDONLY, 1345fa330659SOmar Sandoval device_name, newargs); 13460040e606SChristoph Jaeger if (IS_ERR(mnt)) { 1347fa330659SOmar Sandoval root = ERR_CAST(mnt); 1348fa330659SOmar Sandoval mnt = NULL; 1349fa330659SOmar Sandoval goto out; 13500040e606SChristoph Jaeger } 13510723a047SHarald Hoyer 1352773cd04eSOmar Sandoval down_write(&mnt->mnt_sb->s_umount); 1353fa330659SOmar Sandoval ret = btrfs_remount(mnt->mnt_sb, &flags, NULL); 1354773cd04eSOmar Sandoval up_write(&mnt->mnt_sb->s_umount); 1355fa330659SOmar Sandoval if (ret < 0) { 1356fa330659SOmar Sandoval root = ERR_PTR(ret); 1357fa330659SOmar Sandoval goto out; 13580723a047SHarald Hoyer } 13590723a047SHarald Hoyer } 13600723a047SHarald Hoyer } 1361fa330659SOmar Sandoval if (IS_ERR(mnt)) { 1362fa330659SOmar Sandoval root = ERR_CAST(mnt); 1363fa330659SOmar Sandoval mnt = NULL; 1364fa330659SOmar Sandoval goto out; 1365fa330659SOmar Sandoval } 1366830c4adbSJosef Bacik 136705dbe683SOmar Sandoval if (!subvol_name) { 136805dbe683SOmar Sandoval if (!subvol_objectid) { 136905dbe683SOmar Sandoval ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb), 137005dbe683SOmar Sandoval &subvol_objectid); 137105dbe683SOmar Sandoval if (ret) { 137205dbe683SOmar Sandoval root = ERR_PTR(ret); 137305dbe683SOmar Sandoval goto out; 137405dbe683SOmar Sandoval } 137505dbe683SOmar Sandoval } 137605dbe683SOmar Sandoval subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb), 137705dbe683SOmar Sandoval subvol_objectid); 137805dbe683SOmar Sandoval if (IS_ERR(subvol_name)) { 137905dbe683SOmar Sandoval root = ERR_CAST(subvol_name); 138005dbe683SOmar Sandoval subvol_name = NULL; 138105dbe683SOmar Sandoval goto out; 138205dbe683SOmar Sandoval } 138305dbe683SOmar Sandoval 138405dbe683SOmar Sandoval } 138505dbe683SOmar Sandoval 1386ea441d11SAl Viro root = mount_subtree(mnt, subvol_name); 1387fa330659SOmar Sandoval /* mount_subtree() drops our reference on the vfsmount. */ 1388fa330659SOmar Sandoval mnt = NULL; 1389830c4adbSJosef Bacik 1390bb289b7bSOmar Sandoval if (!IS_ERR(root)) { 1391ea441d11SAl Viro struct super_block *s = root->d_sb; 1392bb289b7bSOmar Sandoval struct inode *root_inode = d_inode(root); 1393bb289b7bSOmar Sandoval u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid; 1394bb289b7bSOmar Sandoval 1395bb289b7bSOmar Sandoval ret = 0; 1396bb289b7bSOmar Sandoval if (!is_subvolume_inode(root_inode)) { 1397bb289b7bSOmar Sandoval pr_err("BTRFS: '%s' is not a valid subvolume\n", 1398bb289b7bSOmar Sandoval subvol_name); 1399bb289b7bSOmar Sandoval ret = -EINVAL; 1400bb289b7bSOmar Sandoval } 1401bb289b7bSOmar Sandoval if (subvol_objectid && root_objectid != subvol_objectid) { 140205dbe683SOmar Sandoval /* 140305dbe683SOmar Sandoval * This will also catch a race condition where a 140405dbe683SOmar Sandoval * subvolume which was passed by ID is renamed and 140505dbe683SOmar Sandoval * another subvolume is renamed over the old location. 140605dbe683SOmar Sandoval */ 1407bb289b7bSOmar Sandoval pr_err("BTRFS: subvol '%s' does not match subvolid %llu\n", 1408bb289b7bSOmar Sandoval subvol_name, subvol_objectid); 1409bb289b7bSOmar Sandoval ret = -EINVAL; 1410bb289b7bSOmar Sandoval } 1411bb289b7bSOmar Sandoval if (ret) { 1412ea441d11SAl Viro dput(root); 1413bb289b7bSOmar Sandoval root = ERR_PTR(ret); 1414ea441d11SAl Viro deactivate_locked_super(s); 1415bb289b7bSOmar Sandoval } 1416f9d9ef62SDavid Sterba } 1417f9d9ef62SDavid Sterba 1418fa330659SOmar Sandoval out: 1419fa330659SOmar Sandoval mntput(mnt); 1420fa330659SOmar Sandoval kfree(newargs); 1421fa330659SOmar Sandoval kfree(subvol_name); 1422830c4adbSJosef Bacik return root; 1423830c4adbSJosef Bacik } 1424450ba0eaSJosef Bacik 1425f667aef6SQu Wenruo static int parse_security_options(char *orig_opts, 1426f667aef6SQu Wenruo struct security_mnt_opts *sec_opts) 1427f667aef6SQu Wenruo { 1428f667aef6SQu Wenruo char *secdata = NULL; 1429f667aef6SQu Wenruo int ret = 0; 1430f667aef6SQu Wenruo 1431f667aef6SQu Wenruo secdata = alloc_secdata(); 1432f667aef6SQu Wenruo if (!secdata) 1433f667aef6SQu Wenruo return -ENOMEM; 1434f667aef6SQu Wenruo ret = security_sb_copy_data(orig_opts, secdata); 1435f667aef6SQu Wenruo if (ret) { 1436f667aef6SQu Wenruo free_secdata(secdata); 1437f667aef6SQu Wenruo return ret; 1438f667aef6SQu Wenruo } 1439f667aef6SQu Wenruo ret = security_sb_parse_opts_str(secdata, sec_opts); 1440f667aef6SQu Wenruo free_secdata(secdata); 1441f667aef6SQu Wenruo return ret; 1442f667aef6SQu Wenruo } 1443f667aef6SQu Wenruo 1444f667aef6SQu Wenruo static int setup_security_options(struct btrfs_fs_info *fs_info, 1445f667aef6SQu Wenruo struct super_block *sb, 1446f667aef6SQu Wenruo struct security_mnt_opts *sec_opts) 1447f667aef6SQu Wenruo { 1448f667aef6SQu Wenruo int ret = 0; 1449f667aef6SQu Wenruo 1450f667aef6SQu Wenruo /* 1451f667aef6SQu Wenruo * Call security_sb_set_mnt_opts() to check whether new sec_opts 1452f667aef6SQu Wenruo * is valid. 1453f667aef6SQu Wenruo */ 1454f667aef6SQu Wenruo ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL); 1455f667aef6SQu Wenruo if (ret) 1456f667aef6SQu Wenruo return ret; 1457f667aef6SQu Wenruo 1458a43bb39bSQu Wenruo #ifdef CONFIG_SECURITY 1459f667aef6SQu Wenruo if (!fs_info->security_opts.num_mnt_opts) { 1460f667aef6SQu Wenruo /* first time security setup, copy sec_opts to fs_info */ 1461f667aef6SQu Wenruo memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts)); 1462f667aef6SQu Wenruo } else { 1463f667aef6SQu Wenruo /* 1464f667aef6SQu Wenruo * Since SELinux(the only one supports security_mnt_opts) does 1465f667aef6SQu Wenruo * NOT support changing context during remount/mount same sb, 1466f667aef6SQu Wenruo * This must be the same or part of the same security options, 1467f667aef6SQu Wenruo * just free it. 1468f667aef6SQu Wenruo */ 1469f667aef6SQu Wenruo security_free_mnt_opts(sec_opts); 1470f667aef6SQu Wenruo } 1471a43bb39bSQu Wenruo #endif 1472f667aef6SQu Wenruo return ret; 1473f667aef6SQu Wenruo } 1474f667aef6SQu Wenruo 1475edf24abeSChristoph Hellwig /* 1476edf24abeSChristoph Hellwig * Find a superblock for the given device / mount point. 1477edf24abeSChristoph Hellwig * 1478edf24abeSChristoph Hellwig * Note: This is based on get_sb_bdev from fs/super.c with a few additions 1479edf24abeSChristoph Hellwig * for multiple device setup. Make sure to keep it in sync. 1480edf24abeSChristoph Hellwig */ 1481061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1482306e16ceSDavid Sterba const char *device_name, void *data) 14834b82d6e4SYan { 14844b82d6e4SYan struct block_device *bdev = NULL; 14854b82d6e4SYan struct super_block *s; 14868a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices = NULL; 1487450ba0eaSJosef Bacik struct btrfs_fs_info *fs_info = NULL; 1488f667aef6SQu Wenruo struct security_mnt_opts new_sec_opts; 148997288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 149073f73415SJosef Bacik char *subvol_name = NULL; 149173f73415SJosef Bacik u64 subvol_objectid = 0; 14924b82d6e4SYan int error = 0; 14934b82d6e4SYan 149497288f2cSChristoph Hellwig if (!(flags & MS_RDONLY)) 149597288f2cSChristoph Hellwig mode |= FMODE_WRITE; 149697288f2cSChristoph Hellwig 149797288f2cSChristoph Hellwig error = btrfs_parse_early_options(data, mode, fs_type, 149873f73415SJosef Bacik &subvol_name, &subvol_objectid, 14995e2a4b25SDavid Sterba &fs_devices); 1500f23c8af8SIlya Dryomov if (error) { 1501f23c8af8SIlya Dryomov kfree(subvol_name); 1502061dbc6bSAl Viro return ERR_PTR(error); 1503f23c8af8SIlya Dryomov } 1504edf24abeSChristoph Hellwig 150505dbe683SOmar Sandoval if (subvol_name || subvol_objectid != BTRFS_FS_TREE_OBJECTID) { 1506fa330659SOmar Sandoval /* mount_subvol() will free subvol_name. */ 1507bb289b7bSOmar Sandoval return mount_subvol(subvol_name, subvol_objectid, flags, 1508bb289b7bSOmar Sandoval device_name, data); 1509830c4adbSJosef Bacik } 1510830c4adbSJosef Bacik 1511f667aef6SQu Wenruo security_init_mnt_opts(&new_sec_opts); 1512f667aef6SQu Wenruo if (data) { 1513f667aef6SQu Wenruo error = parse_security_options(data, &new_sec_opts); 15148a4b83ccSChris Mason if (error) 1515830c4adbSJosef Bacik return ERR_PTR(error); 1516f667aef6SQu Wenruo } 1517f667aef6SQu Wenruo 1518f667aef6SQu Wenruo error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices); 1519f667aef6SQu Wenruo if (error) 1520f667aef6SQu Wenruo goto error_sec_opts; 15214b82d6e4SYan 1522450ba0eaSJosef Bacik /* 1523450ba0eaSJosef Bacik * Setup a dummy root and fs_info for test/set super. This is because 1524450ba0eaSJosef Bacik * we don't actually fill this stuff out until open_ctree, but we need 1525450ba0eaSJosef Bacik * it for searching for existing supers, so this lets us do that and 1526450ba0eaSJosef Bacik * then open_ctree will properly initialize everything later. 1527450ba0eaSJosef Bacik */ 1528450ba0eaSJosef Bacik fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS); 1529f667aef6SQu Wenruo if (!fs_info) { 1530f667aef6SQu Wenruo error = -ENOMEM; 1531f667aef6SQu Wenruo goto error_sec_opts; 1532f667aef6SQu Wenruo } 153304d21a24SIlya Dryomov 1534450ba0eaSJosef Bacik fs_info->fs_devices = fs_devices; 1535450ba0eaSJosef Bacik 15366c41761fSDavid Sterba fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 15376c41761fSDavid Sterba fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 1538f667aef6SQu Wenruo security_init_mnt_opts(&fs_info->security_opts); 15396c41761fSDavid Sterba if (!fs_info->super_copy || !fs_info->super_for_commit) { 15406c41761fSDavid Sterba error = -ENOMEM; 154104d21a24SIlya Dryomov goto error_fs_info; 154204d21a24SIlya Dryomov } 154304d21a24SIlya Dryomov 154404d21a24SIlya Dryomov error = btrfs_open_devices(fs_devices, mode, fs_type); 154504d21a24SIlya Dryomov if (error) 154604d21a24SIlya Dryomov goto error_fs_info; 154704d21a24SIlya Dryomov 154804d21a24SIlya Dryomov if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) { 154904d21a24SIlya Dryomov error = -EACCES; 15506c41761fSDavid Sterba goto error_close_devices; 15516c41761fSDavid Sterba } 15526c41761fSDavid Sterba 1553dfe25020SChris Mason bdev = fs_devices->latest_bdev; 15549249e17fSDavid Howells s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC, 15559249e17fSDavid Howells fs_info); 1556830c4adbSJosef Bacik if (IS_ERR(s)) { 1557830c4adbSJosef Bacik error = PTR_ERR(s); 1558830c4adbSJosef Bacik goto error_close_devices; 1559830c4adbSJosef Bacik } 15604b82d6e4SYan 15614b82d6e4SYan if (s->s_root) { 15622b82032cSYan Zheng btrfs_close_devices(fs_devices); 15636c41761fSDavid Sterba free_fs_info(fs_info); 156459553edfSAl Viro if ((flags ^ s->s_flags) & MS_RDONLY) 156559553edfSAl Viro error = -EBUSY; 15664b82d6e4SYan } else { 1567a1c6f057SDmitry Monakhov snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev); 1568815745cfSAl Viro btrfs_sb(s)->bdev_holder = fs_type; 15698a4b83ccSChris Mason error = btrfs_fill_super(s, fs_devices, data, 15708a4b83ccSChris Mason flags & MS_SILENT ? 1 : 0); 15714b82d6e4SYan } 157205dbe683SOmar Sandoval if (error) { 1573e15d0542SXin Zhong deactivate_locked_super(s); 1574f667aef6SQu Wenruo goto error_sec_opts; 1575f667aef6SQu Wenruo } 1576f667aef6SQu Wenruo 1577f667aef6SQu Wenruo fs_info = btrfs_sb(s); 1578f667aef6SQu Wenruo error = setup_security_options(fs_info, s, &new_sec_opts); 1579f667aef6SQu Wenruo if (error) { 1580f667aef6SQu Wenruo deactivate_locked_super(s); 1581f667aef6SQu Wenruo goto error_sec_opts; 1582f667aef6SQu Wenruo } 15834b82d6e4SYan 158405dbe683SOmar Sandoval return dget(s->s_root); 15854b82d6e4SYan 1586c146afadSYan Zheng error_close_devices: 15878a4b83ccSChris Mason btrfs_close_devices(fs_devices); 158804d21a24SIlya Dryomov error_fs_info: 15896c41761fSDavid Sterba free_fs_info(fs_info); 1590f667aef6SQu Wenruo error_sec_opts: 1591f667aef6SQu Wenruo security_free_mnt_opts(&new_sec_opts); 1592061dbc6bSAl Viro return ERR_PTR(error); 15934b82d6e4SYan } 15942e635a27SChris Mason 15950d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 15960d2450abSSergei Trofimovich int new_pool_size, int old_pool_size) 15970d2450abSSergei Trofimovich { 15980d2450abSSergei Trofimovich if (new_pool_size == old_pool_size) 15990d2450abSSergei Trofimovich return; 16000d2450abSSergei Trofimovich 16010d2450abSSergei Trofimovich fs_info->thread_pool_size = new_pool_size; 16020d2450abSSergei Trofimovich 1603efe120a0SFrank Holton btrfs_info(fs_info, "resize thread pool %d -> %d", 16040d2450abSSergei Trofimovich old_pool_size, new_pool_size); 16050d2450abSSergei Trofimovich 16065cdc7ad3SQu Wenruo btrfs_workqueue_set_max(fs_info->workers, new_pool_size); 1607afe3d242SQu Wenruo btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size); 1608a8c93d4eSQu Wenruo btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size); 1609e66f0bb1SQu Wenruo btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size); 1610fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size); 1611fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size); 1612fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_write_workers, 1613fccb5d86SQu Wenruo new_pool_size); 1614fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size); 1615fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size); 16165b3bc44eSQu Wenruo btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size); 1617736cfa15SQu Wenruo btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size); 16180339ef2fSQu Wenruo btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers, 1619ff023aacSStefan Behrens new_pool_size); 16200d2450abSSergei Trofimovich } 16210d2450abSSergei Trofimovich 1622f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info) 1623dc81cdc5SMiao Xie { 1624dc81cdc5SMiao Xie set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1625f42a34b2SMiao Xie } 1626dc81cdc5SMiao Xie 1627f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info, 1628f42a34b2SMiao Xie unsigned long old_opts, int flags) 1629f42a34b2SMiao Xie { 1630dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1631dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1632dc81cdc5SMiao Xie (flags & MS_RDONLY))) { 1633dc81cdc5SMiao Xie /* wait for any defraggers to finish */ 1634dc81cdc5SMiao Xie wait_event(fs_info->transaction_wait, 1635dc81cdc5SMiao Xie (atomic_read(&fs_info->defrag_running) == 0)); 1636dc81cdc5SMiao Xie if (flags & MS_RDONLY) 1637dc81cdc5SMiao Xie sync_filesystem(fs_info->sb); 1638dc81cdc5SMiao Xie } 1639dc81cdc5SMiao Xie } 1640dc81cdc5SMiao Xie 1641dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info, 1642dc81cdc5SMiao Xie unsigned long old_opts) 1643dc81cdc5SMiao Xie { 1644dc81cdc5SMiao Xie /* 1645dc81cdc5SMiao Xie * We need cleanup all defragable inodes if the autodefragment is 1646dc81cdc5SMiao Xie * close or the fs is R/O. 1647dc81cdc5SMiao Xie */ 1648dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1649dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1650dc81cdc5SMiao Xie (fs_info->sb->s_flags & MS_RDONLY))) { 1651dc81cdc5SMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 1652dc81cdc5SMiao Xie } 1653dc81cdc5SMiao Xie 1654dc81cdc5SMiao Xie clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1655dc81cdc5SMiao Xie } 1656dc81cdc5SMiao Xie 1657c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1658c146afadSYan Zheng { 1659815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1660815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 166149b25e05SJeff Mahoney unsigned old_flags = sb->s_flags; 166249b25e05SJeff Mahoney unsigned long old_opts = fs_info->mount_opt; 166349b25e05SJeff Mahoney unsigned long old_compress_type = fs_info->compress_type; 166449b25e05SJeff Mahoney u64 old_max_inline = fs_info->max_inline; 166549b25e05SJeff Mahoney u64 old_alloc_start = fs_info->alloc_start; 166649b25e05SJeff Mahoney int old_thread_pool_size = fs_info->thread_pool_size; 166749b25e05SJeff Mahoney unsigned int old_metadata_ratio = fs_info->metadata_ratio; 1668c146afadSYan Zheng int ret; 1669c146afadSYan Zheng 167002b9984dSTheodore Ts'o sync_filesystem(sb); 1671f42a34b2SMiao Xie btrfs_remount_prepare(fs_info); 1672dc81cdc5SMiao Xie 1673f667aef6SQu Wenruo if (data) { 1674f667aef6SQu Wenruo struct security_mnt_opts new_sec_opts; 1675f667aef6SQu Wenruo 1676f667aef6SQu Wenruo security_init_mnt_opts(&new_sec_opts); 1677f667aef6SQu Wenruo ret = parse_security_options(data, &new_sec_opts); 1678f667aef6SQu Wenruo if (ret) 1679f667aef6SQu Wenruo goto restore; 1680f667aef6SQu Wenruo ret = setup_security_options(fs_info, sb, 1681f667aef6SQu Wenruo &new_sec_opts); 1682f667aef6SQu Wenruo if (ret) { 1683f667aef6SQu Wenruo security_free_mnt_opts(&new_sec_opts); 1684f667aef6SQu Wenruo goto restore; 1685f667aef6SQu Wenruo } 1686f667aef6SQu Wenruo } 1687f667aef6SQu Wenruo 1688b288052eSChris Mason ret = btrfs_parse_options(root, data); 168949b25e05SJeff Mahoney if (ret) { 169049b25e05SJeff Mahoney ret = -EINVAL; 169149b25e05SJeff Mahoney goto restore; 169249b25e05SJeff Mahoney } 1693b288052eSChris Mason 1694f42a34b2SMiao Xie btrfs_remount_begin(fs_info, old_opts, *flags); 16950d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 16960d2450abSSergei Trofimovich fs_info->thread_pool_size, old_thread_pool_size); 16970d2450abSSergei Trofimovich 1698c146afadSYan Zheng if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 1699dc81cdc5SMiao Xie goto out; 1700c146afadSYan Zheng 1701c146afadSYan Zheng if (*flags & MS_RDONLY) { 17028dabb742SStefan Behrens /* 17038dabb742SStefan Behrens * this also happens on 'umount -rf' or on shutdown, when 17048dabb742SStefan Behrens * the filesystem is busy. 17058dabb742SStefan Behrens */ 170621c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 1707361c093dSStefan Behrens 1708361c093dSStefan Behrens /* wait for the uuid_scan task to finish */ 1709361c093dSStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 1710361c093dSStefan Behrens /* avoid complains from lockdep et al. */ 1711361c093dSStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 1712361c093dSStefan Behrens 1713c146afadSYan Zheng sb->s_flags |= MS_RDONLY; 1714c146afadSYan Zheng 1715e44163e1SJeff Mahoney /* 1716e44163e1SJeff Mahoney * Setting MS_RDONLY will put the cleaner thread to 1717e44163e1SJeff Mahoney * sleep at the next loop if it's already active. 1718e44163e1SJeff Mahoney * If it's already asleep, we'll leave unused block 1719e44163e1SJeff Mahoney * groups on disk until we're mounted read-write again 1720e44163e1SJeff Mahoney * unless we clean them up here. 1721e44163e1SJeff Mahoney */ 1722e44163e1SJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1723e44163e1SJeff Mahoney 17248dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 17258dabb742SStefan Behrens btrfs_scrub_cancel(fs_info); 1726061594efSMiao Xie btrfs_pause_balance(fs_info); 17278dabb742SStefan Behrens 1728c146afadSYan Zheng ret = btrfs_commit_super(root); 172949b25e05SJeff Mahoney if (ret) 173049b25e05SJeff Mahoney goto restore; 1731c146afadSYan Zheng } else { 17326ef3de9cSDavid Sterba if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 17336ef3de9cSDavid Sterba btrfs_err(fs_info, 1734efe120a0SFrank Holton "Remounting read-write after error is not allowed"); 17356ef3de9cSDavid Sterba ret = -EINVAL; 17366ef3de9cSDavid Sterba goto restore; 17376ef3de9cSDavid Sterba } 17388a3db184SSergei Trofimovich if (fs_info->fs_devices->rw_devices == 0) { 173949b25e05SJeff Mahoney ret = -EACCES; 174049b25e05SJeff Mahoney goto restore; 17418a3db184SSergei Trofimovich } 17422b82032cSYan Zheng 1743292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 1744292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 1745292fd7fcSStefan Behrens !(*flags & MS_RDONLY)) { 1746efe120a0SFrank Holton btrfs_warn(fs_info, 1747efe120a0SFrank Holton "too many missing devices, writeable remount is not allowed"); 1748292fd7fcSStefan Behrens ret = -EACCES; 1749292fd7fcSStefan Behrens goto restore; 1750292fd7fcSStefan Behrens } 1751292fd7fcSStefan Behrens 17528a3db184SSergei Trofimovich if (btrfs_super_log_root(fs_info->super_copy) != 0) { 175349b25e05SJeff Mahoney ret = -EINVAL; 175449b25e05SJeff Mahoney goto restore; 17558a3db184SSergei Trofimovich } 1756c146afadSYan Zheng 1757815745cfSAl Viro ret = btrfs_cleanup_fs_roots(fs_info); 175849b25e05SJeff Mahoney if (ret) 175949b25e05SJeff Mahoney goto restore; 1760c146afadSYan Zheng 1761d68fc57bSYan, Zheng /* recover relocation */ 17625f316481SWang Shilong mutex_lock(&fs_info->cleaner_mutex); 1763d68fc57bSYan, Zheng ret = btrfs_recover_relocation(root); 17645f316481SWang Shilong mutex_unlock(&fs_info->cleaner_mutex); 176549b25e05SJeff Mahoney if (ret) 176649b25e05SJeff Mahoney goto restore; 1767c146afadSYan Zheng 17682b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 17692b6ba629SIlya Dryomov if (ret) 17702b6ba629SIlya Dryomov goto restore; 17712b6ba629SIlya Dryomov 17728dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 17738dabb742SStefan Behrens if (ret) { 1774efe120a0SFrank Holton btrfs_warn(fs_info, "failed to resume dev_replace"); 17758dabb742SStefan Behrens goto restore; 17768dabb742SStefan Behrens } 177794aebfb2SJosef Bacik 177894aebfb2SJosef Bacik if (!fs_info->uuid_root) { 1779efe120a0SFrank Holton btrfs_info(fs_info, "creating UUID tree"); 178094aebfb2SJosef Bacik ret = btrfs_create_uuid_tree(fs_info); 178194aebfb2SJosef Bacik if (ret) { 1782efe120a0SFrank Holton btrfs_warn(fs_info, "failed to create the UUID tree %d", ret); 178394aebfb2SJosef Bacik goto restore; 178494aebfb2SJosef Bacik } 178594aebfb2SJosef Bacik } 1786c146afadSYan Zheng sb->s_flags &= ~MS_RDONLY; 1787c146afadSYan Zheng } 1788dc81cdc5SMiao Xie out: 17892c6a92b0SJustin Maggard wake_up_process(fs_info->transaction_kthread); 1790dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 1791c146afadSYan Zheng return 0; 179249b25e05SJeff Mahoney 179349b25e05SJeff Mahoney restore: 179449b25e05SJeff Mahoney /* We've hit an error - don't reset MS_RDONLY */ 179549b25e05SJeff Mahoney if (sb->s_flags & MS_RDONLY) 179649b25e05SJeff Mahoney old_flags |= MS_RDONLY; 179749b25e05SJeff Mahoney sb->s_flags = old_flags; 179849b25e05SJeff Mahoney fs_info->mount_opt = old_opts; 179949b25e05SJeff Mahoney fs_info->compress_type = old_compress_type; 180049b25e05SJeff Mahoney fs_info->max_inline = old_max_inline; 1801c018daecSMiao Xie mutex_lock(&fs_info->chunk_mutex); 180249b25e05SJeff Mahoney fs_info->alloc_start = old_alloc_start; 1803c018daecSMiao Xie mutex_unlock(&fs_info->chunk_mutex); 18040d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 18050d2450abSSergei Trofimovich old_thread_pool_size, fs_info->thread_pool_size); 180649b25e05SJeff Mahoney fs_info->metadata_ratio = old_metadata_ratio; 1807dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 180849b25e05SJeff Mahoney return ret; 1809c146afadSYan Zheng } 1810c146afadSYan Zheng 1811bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 1812bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1, 1813bcd53741SArne Jansen const void *dev_info2) 1814bcd53741SArne Jansen { 1815bcd53741SArne Jansen if (((struct btrfs_device_info *)dev_info1)->max_avail > 1816bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1817bcd53741SArne Jansen return -1; 1818bcd53741SArne Jansen else if (((struct btrfs_device_info *)dev_info1)->max_avail < 1819bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1820bcd53741SArne Jansen return 1; 1821bcd53741SArne Jansen else 1822bcd53741SArne Jansen return 0; 1823bcd53741SArne Jansen } 1824bcd53741SArne Jansen 1825bcd53741SArne Jansen /* 1826bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 1827bcd53741SArne Jansen * is stored.(Descending Sort) 1828bcd53741SArne Jansen */ 1829bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 1830bcd53741SArne Jansen struct btrfs_device_info *devices, 1831bcd53741SArne Jansen size_t nr_devices) 1832bcd53741SArne Jansen { 1833bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 1834bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 1835bcd53741SArne Jansen } 1836bcd53741SArne Jansen 18376d07bcecSMiao Xie /* 18386d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 18396d07bcecSMiao Xie * file data. 18406d07bcecSMiao Xie */ 18416d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes) 18426d07bcecSMiao Xie { 18436d07bcecSMiao Xie struct btrfs_fs_info *fs_info = root->fs_info; 18446d07bcecSMiao Xie struct btrfs_device_info *devices_info; 18456d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 18466d07bcecSMiao Xie struct btrfs_device *device; 18476d07bcecSMiao Xie u64 skip_space; 18486d07bcecSMiao Xie u64 type; 18496d07bcecSMiao Xie u64 avail_space; 18506d07bcecSMiao Xie u64 used_space; 18516d07bcecSMiao Xie u64 min_stripe_size; 185239fb26c3SMiao Xie int min_stripes = 1, num_stripes = 1; 18536d07bcecSMiao Xie int i = 0, nr_devices; 18546d07bcecSMiao Xie int ret; 18556d07bcecSMiao Xie 18567e33fd99SJosef Bacik /* 18577e33fd99SJosef Bacik * We aren't under the device list lock, so this is racey-ish, but good 18587e33fd99SJosef Bacik * enough for our purposes. 18597e33fd99SJosef Bacik */ 1860b772a86eSLi Zefan nr_devices = fs_info->fs_devices->open_devices; 18617e33fd99SJosef Bacik if (!nr_devices) { 18627e33fd99SJosef Bacik smp_mb(); 18637e33fd99SJosef Bacik nr_devices = fs_info->fs_devices->open_devices; 18647e33fd99SJosef Bacik ASSERT(nr_devices); 18657e33fd99SJosef Bacik if (!nr_devices) { 18667e33fd99SJosef Bacik *free_bytes = 0; 18677e33fd99SJosef Bacik return 0; 18687e33fd99SJosef Bacik } 18697e33fd99SJosef Bacik } 18706d07bcecSMiao Xie 1871d9b0d9baSDulshani Gunawardhana devices_info = kmalloc_array(nr_devices, sizeof(*devices_info), 18726d07bcecSMiao Xie GFP_NOFS); 18736d07bcecSMiao Xie if (!devices_info) 18746d07bcecSMiao Xie return -ENOMEM; 18756d07bcecSMiao Xie 18766d07bcecSMiao Xie /* calc min stripe number for data space alloction */ 18776d07bcecSMiao Xie type = btrfs_get_alloc_profile(root, 1); 187839fb26c3SMiao Xie if (type & BTRFS_BLOCK_GROUP_RAID0) { 18796d07bcecSMiao Xie min_stripes = 2; 188039fb26c3SMiao Xie num_stripes = nr_devices; 188139fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID1) { 18826d07bcecSMiao Xie min_stripes = 2; 188339fb26c3SMiao Xie num_stripes = 2; 188439fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID10) { 18856d07bcecSMiao Xie min_stripes = 4; 188639fb26c3SMiao Xie num_stripes = 4; 188739fb26c3SMiao Xie } 18886d07bcecSMiao Xie 18896d07bcecSMiao Xie if (type & BTRFS_BLOCK_GROUP_DUP) 18906d07bcecSMiao Xie min_stripe_size = 2 * BTRFS_STRIPE_LEN; 18916d07bcecSMiao Xie else 18926d07bcecSMiao Xie min_stripe_size = BTRFS_STRIPE_LEN; 18936d07bcecSMiao Xie 18947e33fd99SJosef Bacik if (fs_info->alloc_start) 18957e33fd99SJosef Bacik mutex_lock(&fs_devices->device_list_mutex); 18967e33fd99SJosef Bacik rcu_read_lock(); 18977e33fd99SJosef Bacik list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 189863a212abSStefan Behrens if (!device->in_fs_metadata || !device->bdev || 189963a212abSStefan Behrens device->is_tgtdev_for_dev_replace) 19006d07bcecSMiao Xie continue; 19016d07bcecSMiao Xie 19027e33fd99SJosef Bacik if (i >= nr_devices) 19037e33fd99SJosef Bacik break; 19047e33fd99SJosef Bacik 19056d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 19066d07bcecSMiao Xie 19076d07bcecSMiao Xie /* align with stripe_len */ 1908f8c269d7SDavid Sterba avail_space = div_u64(avail_space, BTRFS_STRIPE_LEN); 19096d07bcecSMiao Xie avail_space *= BTRFS_STRIPE_LEN; 19106d07bcecSMiao Xie 19116d07bcecSMiao Xie /* 19126d07bcecSMiao Xie * In order to avoid overwritting the superblock on the drive, 19136d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 19146d07bcecSMiao Xie * allocation. 19156d07bcecSMiao Xie */ 1916ee22184bSByongho Lee skip_space = SZ_1M; 19176d07bcecSMiao Xie 19186d07bcecSMiao Xie /* user can set the offset in fs_info->alloc_start. */ 19197e33fd99SJosef Bacik if (fs_info->alloc_start && 19207e33fd99SJosef Bacik fs_info->alloc_start + BTRFS_STRIPE_LEN <= 19217e33fd99SJosef Bacik device->total_bytes) { 19227e33fd99SJosef Bacik rcu_read_unlock(); 19236d07bcecSMiao Xie skip_space = max(fs_info->alloc_start, skip_space); 19246d07bcecSMiao Xie 19256d07bcecSMiao Xie /* 19267e33fd99SJosef Bacik * btrfs can not use the free space in 19277e33fd99SJosef Bacik * [0, skip_space - 1], we must subtract it from the 19287e33fd99SJosef Bacik * total. In order to implement it, we account the used 19297e33fd99SJosef Bacik * space in this range first. 19306d07bcecSMiao Xie */ 19317e33fd99SJosef Bacik ret = btrfs_account_dev_extents_size(device, 0, 19327e33fd99SJosef Bacik skip_space - 1, 19336d07bcecSMiao Xie &used_space); 19346d07bcecSMiao Xie if (ret) { 19356d07bcecSMiao Xie kfree(devices_info); 19367e33fd99SJosef Bacik mutex_unlock(&fs_devices->device_list_mutex); 19376d07bcecSMiao Xie return ret; 19386d07bcecSMiao Xie } 19396d07bcecSMiao Xie 19407e33fd99SJosef Bacik rcu_read_lock(); 19417e33fd99SJosef Bacik 19426d07bcecSMiao Xie /* calc the free space in [0, skip_space - 1] */ 19436d07bcecSMiao Xie skip_space -= used_space; 19447e33fd99SJosef Bacik } 19456d07bcecSMiao Xie 19466d07bcecSMiao Xie /* 19476d07bcecSMiao Xie * we can use the free space in [0, skip_space - 1], subtract 19486d07bcecSMiao Xie * it from the total. 19496d07bcecSMiao Xie */ 19506d07bcecSMiao Xie if (avail_space && avail_space >= skip_space) 19516d07bcecSMiao Xie avail_space -= skip_space; 19526d07bcecSMiao Xie else 19536d07bcecSMiao Xie avail_space = 0; 19546d07bcecSMiao Xie 19556d07bcecSMiao Xie if (avail_space < min_stripe_size) 19566d07bcecSMiao Xie continue; 19576d07bcecSMiao Xie 19586d07bcecSMiao Xie devices_info[i].dev = device; 19596d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 19606d07bcecSMiao Xie 19616d07bcecSMiao Xie i++; 19626d07bcecSMiao Xie } 19637e33fd99SJosef Bacik rcu_read_unlock(); 19647e33fd99SJosef Bacik if (fs_info->alloc_start) 19657e33fd99SJosef Bacik mutex_unlock(&fs_devices->device_list_mutex); 19666d07bcecSMiao Xie 19676d07bcecSMiao Xie nr_devices = i; 19686d07bcecSMiao Xie 19696d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 19706d07bcecSMiao Xie 19716d07bcecSMiao Xie i = nr_devices - 1; 19726d07bcecSMiao Xie avail_space = 0; 19736d07bcecSMiao Xie while (nr_devices >= min_stripes) { 197439fb26c3SMiao Xie if (num_stripes > nr_devices) 197539fb26c3SMiao Xie num_stripes = nr_devices; 197639fb26c3SMiao Xie 19776d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 19786d07bcecSMiao Xie int j; 19796d07bcecSMiao Xie u64 alloc_size; 19806d07bcecSMiao Xie 198139fb26c3SMiao Xie avail_space += devices_info[i].max_avail * num_stripes; 19826d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 198339fb26c3SMiao Xie for (j = i + 1 - num_stripes; j <= i; j++) 19846d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 19856d07bcecSMiao Xie } 19866d07bcecSMiao Xie i--; 19876d07bcecSMiao Xie nr_devices--; 19886d07bcecSMiao Xie } 19896d07bcecSMiao Xie 19906d07bcecSMiao Xie kfree(devices_info); 19916d07bcecSMiao Xie *free_bytes = avail_space; 19926d07bcecSMiao Xie return 0; 19936d07bcecSMiao Xie } 19946d07bcecSMiao Xie 1995ba7b6e62SDavid Sterba /* 1996ba7b6e62SDavid Sterba * Calculate numbers for 'df', pessimistic in case of mixed raid profiles. 1997ba7b6e62SDavid Sterba * 1998ba7b6e62SDavid Sterba * If there's a redundant raid level at DATA block groups, use the respective 1999ba7b6e62SDavid Sterba * multiplier to scale the sizes. 2000ba7b6e62SDavid Sterba * 2001ba7b6e62SDavid Sterba * Unused device space usage is based on simulating the chunk allocator 2002ba7b6e62SDavid Sterba * algorithm that respects the device sizes, order of allocations and the 2003ba7b6e62SDavid Sterba * 'alloc_start' value, this is a close approximation of the actual use but 2004ba7b6e62SDavid Sterba * there are other factors that may change the result (like a new metadata 2005ba7b6e62SDavid Sterba * chunk). 2006ba7b6e62SDavid Sterba * 2007ca8a51b3SDavid Sterba * If metadata is exhausted, f_bavail will be 0. 2008ca8a51b3SDavid Sterba * 2009ba7b6e62SDavid Sterba * FIXME: not accurate for mixed block groups, total and free/used are ok, 2010ba7b6e62SDavid Sterba * available appears slightly larger. 2011ba7b6e62SDavid Sterba */ 20128fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 20138fd17795SChris Mason { 2014815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 2015815745cfSAl Viro struct btrfs_super_block *disk_super = fs_info->super_copy; 2016815745cfSAl Viro struct list_head *head = &fs_info->space_info; 2017bd4d1088SJosef Bacik struct btrfs_space_info *found; 2018bd4d1088SJosef Bacik u64 total_used = 0; 20196d07bcecSMiao Xie u64 total_free_data = 0; 2020ca8a51b3SDavid Sterba u64 total_free_meta = 0; 2021db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 2022815745cfSAl Viro __be32 *fsid = (__be32 *)fs_info->fsid; 2023ba7b6e62SDavid Sterba unsigned factor = 1; 2024ba7b6e62SDavid Sterba struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 20256d07bcecSMiao Xie int ret; 2026ca8a51b3SDavid Sterba u64 thresh = 0; 20278fd17795SChris Mason 202815484377SMiao Xie /* 202915484377SMiao Xie * holding chunk_muext to avoid allocating new chunks, holding 203015484377SMiao Xie * device_list_mutex to avoid the device being removed 203115484377SMiao Xie */ 2032bd4d1088SJosef Bacik rcu_read_lock(); 203389a55897SJosef Bacik list_for_each_entry_rcu(found, head, list) { 20346d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 2035ba7b6e62SDavid Sterba int i; 2036ba7b6e62SDavid Sterba 20376d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 20386d07bcecSMiao Xie total_free_data -= 20396d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 2040ba7b6e62SDavid Sterba 2041ba7b6e62SDavid Sterba for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { 2042ba7b6e62SDavid Sterba if (!list_empty(&found->block_groups[i])) { 2043ba7b6e62SDavid Sterba switch (i) { 2044ba7b6e62SDavid Sterba case BTRFS_RAID_DUP: 2045ba7b6e62SDavid Sterba case BTRFS_RAID_RAID1: 2046ba7b6e62SDavid Sterba case BTRFS_RAID_RAID10: 2047ba7b6e62SDavid Sterba factor = 2; 2048ba7b6e62SDavid Sterba } 2049ba7b6e62SDavid Sterba } 2050ba7b6e62SDavid Sterba } 20516d07bcecSMiao Xie } 2052ca8a51b3SDavid Sterba if (found->flags & BTRFS_BLOCK_GROUP_METADATA) 2053ca8a51b3SDavid Sterba total_free_meta += found->disk_total - found->disk_used; 20546d07bcecSMiao Xie 2055b742bb82SYan, Zheng total_used += found->disk_used; 205689a55897SJosef Bacik } 2057ba7b6e62SDavid Sterba 2058bd4d1088SJosef Bacik rcu_read_unlock(); 2059bd4d1088SJosef Bacik 2060ba7b6e62SDavid Sterba buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor); 2061ba7b6e62SDavid Sterba buf->f_blocks >>= bits; 2062ba7b6e62SDavid Sterba buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits); 2063ba7b6e62SDavid Sterba 2064ba7b6e62SDavid Sterba /* Account global block reserve as used, it's in logical size already */ 2065ba7b6e62SDavid Sterba spin_lock(&block_rsv->lock); 2066ba7b6e62SDavid Sterba buf->f_bfree -= block_rsv->size >> bits; 2067ba7b6e62SDavid Sterba spin_unlock(&block_rsv->lock); 2068ba7b6e62SDavid Sterba 20690d95c1beSDavid Sterba buf->f_bavail = div_u64(total_free_data, factor); 2070815745cfSAl Viro ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data); 20717e33fd99SJosef Bacik if (ret) 20726d07bcecSMiao Xie return ret; 2073ba7b6e62SDavid Sterba buf->f_bavail += div_u64(total_free_data, factor); 20746d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 2075d397712bSChris Mason 2076ca8a51b3SDavid Sterba /* 2077ca8a51b3SDavid Sterba * We calculate the remaining metadata space minus global reserve. If 2078ca8a51b3SDavid Sterba * this is (supposedly) smaller than zero, there's no space. But this 2079ca8a51b3SDavid Sterba * does not hold in practice, the exhausted state happens where's still 2080ca8a51b3SDavid Sterba * some positive delta. So we apply some guesswork and compare the 2081ca8a51b3SDavid Sterba * delta to a 4M threshold. (Practically observed delta was ~2M.) 2082ca8a51b3SDavid Sterba * 2083ca8a51b3SDavid Sterba * We probably cannot calculate the exact threshold value because this 2084ca8a51b3SDavid Sterba * depends on the internal reservations requested by various 2085ca8a51b3SDavid Sterba * operations, so some operations that consume a few metadata will 2086ca8a51b3SDavid Sterba * succeed even if the Avail is zero. But this is better than the other 2087ca8a51b3SDavid Sterba * way around. 2088ca8a51b3SDavid Sterba */ 2089ca8a51b3SDavid Sterba thresh = 4 * 1024 * 1024; 2090ca8a51b3SDavid Sterba 2091ca8a51b3SDavid Sterba if (total_free_meta - thresh < block_rsv->size) 2092ca8a51b3SDavid Sterba buf->f_bavail = 0; 2093ca8a51b3SDavid Sterba 2094ba7b6e62SDavid Sterba buf->f_type = BTRFS_SUPER_MAGIC; 2095ba7b6e62SDavid Sterba buf->f_bsize = dentry->d_sb->s_blocksize; 2096ba7b6e62SDavid Sterba buf->f_namelen = BTRFS_NAME_LEN; 2097ba7b6e62SDavid Sterba 20989d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 20999d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 21009d03632eSDavid Woodhouse on a big-endian or little-endian host */ 21019d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 21029d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 210332d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 21042b0143b5SDavid Howells buf->f_fsid.val[0] ^= BTRFS_I(d_inode(dentry))->root->objectid >> 32; 21052b0143b5SDavid Howells buf->f_fsid.val[1] ^= BTRFS_I(d_inode(dentry))->root->objectid; 210632d48fa1SDavid Woodhouse 21078fd17795SChris Mason return 0; 21088fd17795SChris Mason } 2109b5133862SChris Mason 2110aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb) 2111aea52e19SAl Viro { 2112815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2113aea52e19SAl Viro kill_anon_super(sb); 2114aea52e19SAl Viro free_fs_info(fs_info); 2115aea52e19SAl Viro } 2116aea52e19SAl Viro 21172e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 21182e635a27SChris Mason .owner = THIS_MODULE, 21192e635a27SChris Mason .name = "btrfs", 2120061dbc6bSAl Viro .mount = btrfs_mount, 2121aea52e19SAl Viro .kill_sb = btrfs_kill_super, 2122f667aef6SQu Wenruo .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA, 21232e635a27SChris Mason }; 21247f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs"); 2125a9218f6bSChris Mason 2126d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file) 2127d8620958STom Van Braeckel { 2128d8620958STom Van Braeckel /* 2129d8620958STom Van Braeckel * The control file's private_data is used to hold the 2130d8620958STom Van Braeckel * transaction when it is started and is used to keep 2131d8620958STom Van Braeckel * track of whether a transaction is already in progress. 2132d8620958STom Van Braeckel */ 2133d8620958STom Van Braeckel file->private_data = NULL; 2134d8620958STom Van Braeckel return 0; 2135d8620958STom Van Braeckel } 2136d8620958STom Van Braeckel 2137d352ac68SChris Mason /* 2138d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 2139d352ac68SChris Mason */ 21408a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 21418a4b83ccSChris Mason unsigned long arg) 21428a4b83ccSChris Mason { 21438a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 21448a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 2145c071fcfdSChris Mason int ret = -ENOTTY; 21468a4b83ccSChris Mason 2147e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2148e441d54dSChris Mason return -EPERM; 2149e441d54dSChris Mason 2150dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 2151dae7b665SLi Zefan if (IS_ERR(vol)) 2152dae7b665SLi Zefan return PTR_ERR(vol); 2153c071fcfdSChris Mason 21548a4b83ccSChris Mason switch (cmd) { 21558a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 215697288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 21578a4b83ccSChris Mason &btrfs_fs_type, &fs_devices); 21588a4b83ccSChris Mason break; 215902db0844SJosef Bacik case BTRFS_IOC_DEVICES_READY: 216002db0844SJosef Bacik ret = btrfs_scan_one_device(vol->name, FMODE_READ, 216102db0844SJosef Bacik &btrfs_fs_type, &fs_devices); 216202db0844SJosef Bacik if (ret) 216302db0844SJosef Bacik break; 216402db0844SJosef Bacik ret = !(fs_devices->num_devices == fs_devices->total_devices); 216502db0844SJosef Bacik break; 2166c5868f83SDavid Sterba case BTRFS_IOC_GET_SUPPORTED_FEATURES: 2167*d5131b65SDavid Sterba ret = btrfs_ioctl_get_supported_features((void __user*)arg); 2168c5868f83SDavid Sterba break; 21698a4b83ccSChris Mason } 2170dae7b665SLi Zefan 21718a4b83ccSChris Mason kfree(vol); 2172f819d837SLinda Knippers return ret; 21738a4b83ccSChris Mason } 21748a4b83ccSChris Mason 21750176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 2176ed0dab6bSYan { 2177354aa0fbSMiao Xie struct btrfs_trans_handle *trans; 2178354aa0fbSMiao Xie struct btrfs_root *root = btrfs_sb(sb)->tree_root; 2179354aa0fbSMiao Xie 2180d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 2181354aa0fbSMiao Xie if (IS_ERR(trans)) { 2182354aa0fbSMiao Xie /* no transaction, don't bother */ 2183354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 21840176260fSLinus Torvalds return 0; 2185354aa0fbSMiao Xie return PTR_ERR(trans); 2186354aa0fbSMiao Xie } 2187354aa0fbSMiao Xie return btrfs_commit_transaction(trans, root); 2188ed0dab6bSYan } 2189ed0dab6bSYan 21909c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 21919c5085c1SJosef Bacik { 21929c5085c1SJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 21939c5085c1SJosef Bacik struct btrfs_fs_devices *cur_devices; 21949c5085c1SJosef Bacik struct btrfs_device *dev, *first_dev = NULL; 21959c5085c1SJosef Bacik struct list_head *head; 21969c5085c1SJosef Bacik struct rcu_string *name; 21979c5085c1SJosef Bacik 21989c5085c1SJosef Bacik mutex_lock(&fs_info->fs_devices->device_list_mutex); 21999c5085c1SJosef Bacik cur_devices = fs_info->fs_devices; 22009c5085c1SJosef Bacik while (cur_devices) { 22019c5085c1SJosef Bacik head = &cur_devices->devices; 22029c5085c1SJosef Bacik list_for_each_entry(dev, head, dev_list) { 2203aa9ddcd4SJosef Bacik if (dev->missing) 2204aa9ddcd4SJosef Bacik continue; 22050aeb8a6eSAnand Jain if (!dev->name) 22060aeb8a6eSAnand Jain continue; 22079c5085c1SJosef Bacik if (!first_dev || dev->devid < first_dev->devid) 22089c5085c1SJosef Bacik first_dev = dev; 22099c5085c1SJosef Bacik } 22109c5085c1SJosef Bacik cur_devices = cur_devices->seed; 22119c5085c1SJosef Bacik } 22129c5085c1SJosef Bacik 22139c5085c1SJosef Bacik if (first_dev) { 22149c5085c1SJosef Bacik rcu_read_lock(); 22159c5085c1SJosef Bacik name = rcu_dereference(first_dev->name); 22169c5085c1SJosef Bacik seq_escape(m, name->str, " \t\n\\"); 22179c5085c1SJosef Bacik rcu_read_unlock(); 22189c5085c1SJosef Bacik } else { 22199c5085c1SJosef Bacik WARN_ON(1); 22209c5085c1SJosef Bacik } 22219c5085c1SJosef Bacik mutex_unlock(&fs_info->fs_devices->device_list_mutex); 22229c5085c1SJosef Bacik return 0; 22239c5085c1SJosef Bacik } 22249c5085c1SJosef Bacik 2225b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 222676dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 2227bd555975SAl Viro .evict_inode = btrfs_evict_inode, 2228e20d96d6SChris Mason .put_super = btrfs_put_super, 2229d5719762SChris Mason .sync_fs = btrfs_sync_fs, 2230a9572a15SEric Paris .show_options = btrfs_show_options, 22319c5085c1SJosef Bacik .show_devname = btrfs_show_devname, 22324730a4bcSChris Mason .write_inode = btrfs_write_inode, 22332c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 22342c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 22358fd17795SChris Mason .statfs = btrfs_statfs, 2236c146afadSYan Zheng .remount_fs = btrfs_remount, 22370176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 2238e20d96d6SChris Mason }; 2239a9218f6bSChris Mason 2240a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 2241d8620958STom Van Braeckel .open = btrfs_control_open, 2242a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 2243a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 2244a9218f6bSChris Mason .owner = THIS_MODULE, 22456038f373SArnd Bergmann .llseek = noop_llseek, 2246a9218f6bSChris Mason }; 2247a9218f6bSChris Mason 2248a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 2249578454ffSKay Sievers .minor = BTRFS_MINOR, 2250a9218f6bSChris Mason .name = "btrfs-control", 2251a9218f6bSChris Mason .fops = &btrfs_ctl_fops 2252a9218f6bSChris Mason }; 2253a9218f6bSChris Mason 2254578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 2255578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 2256578454ffSKay Sievers 2257a9218f6bSChris Mason static int btrfs_interface_init(void) 2258a9218f6bSChris Mason { 2259a9218f6bSChris Mason return misc_register(&btrfs_misc); 2260a9218f6bSChris Mason } 2261a9218f6bSChris Mason 2262b2950863SChristoph Hellwig static void btrfs_interface_exit(void) 2263a9218f6bSChris Mason { 2264f368ed60SGreg Kroah-Hartman misc_deregister(&btrfs_misc); 2265a9218f6bSChris Mason } 2266a9218f6bSChris Mason 226785965600SDavid Sterba static void btrfs_print_info(void) 226885965600SDavid Sterba { 226985965600SDavid Sterba printk(KERN_INFO "Btrfs loaded" 227085965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 227185965600SDavid Sterba ", debug=on" 227285965600SDavid Sterba #endif 227379556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT 227479556c3dSStefan Behrens ", assert=on" 227579556c3dSStefan Behrens #endif 227685965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 227785965600SDavid Sterba ", integrity-checker=on" 227885965600SDavid Sterba #endif 227985965600SDavid Sterba "\n"); 228085965600SDavid Sterba } 228185965600SDavid Sterba 2282dc11dd5dSJosef Bacik static int btrfs_run_sanity_tests(void) 2283dc11dd5dSJosef Bacik { 228406ea65a3SJosef Bacik int ret; 228506ea65a3SJosef Bacik 2286294e30feSJosef Bacik ret = btrfs_init_test_fs(); 228706ea65a3SJosef Bacik if (ret) 228806ea65a3SJosef Bacik return ret; 2289294e30feSJosef Bacik 2290294e30feSJosef Bacik ret = btrfs_test_free_space_cache(); 2291294e30feSJosef Bacik if (ret) 2292294e30feSJosef Bacik goto out; 2293294e30feSJosef Bacik ret = btrfs_test_extent_buffer_operations(); 2294294e30feSJosef Bacik if (ret) 2295294e30feSJosef Bacik goto out; 2296294e30feSJosef Bacik ret = btrfs_test_extent_io(); 2297aaedb55bSJosef Bacik if (ret) 2298aaedb55bSJosef Bacik goto out; 2299aaedb55bSJosef Bacik ret = btrfs_test_inodes(); 2300faa2dbf0SJosef Bacik if (ret) 2301faa2dbf0SJosef Bacik goto out; 2302faa2dbf0SJosef Bacik ret = btrfs_test_qgroups(); 23037c55ee0cSOmar Sandoval if (ret) 23047c55ee0cSOmar Sandoval goto out; 23057c55ee0cSOmar Sandoval ret = btrfs_test_free_space_tree(); 2306294e30feSJosef Bacik out: 2307294e30feSJosef Bacik btrfs_destroy_test_fs(); 2308294e30feSJosef Bacik return ret; 2309dc11dd5dSJosef Bacik } 2310dc11dd5dSJosef Bacik 23112e635a27SChris Mason static int __init init_btrfs_fs(void) 23122e635a27SChris Mason { 23132c90e5d6SChris Mason int err; 231458176a96SJosef Bacik 231514a958e6SFilipe David Borba Manana err = btrfs_hash_init(); 231614a958e6SFilipe David Borba Manana if (err) 231714a958e6SFilipe David Borba Manana return err; 231814a958e6SFilipe David Borba Manana 231963541927SFilipe David Borba Manana btrfs_props_init(); 232063541927SFilipe David Borba Manana 232158176a96SJosef Bacik err = btrfs_init_sysfs(); 232258176a96SJosef Bacik if (err) 232314a958e6SFilipe David Borba Manana goto free_hash; 232458176a96SJosef Bacik 2325143bede5SJeff Mahoney btrfs_init_compress(); 2326d1310b2eSChris Mason 2327261507a0SLi Zefan err = btrfs_init_cachep(); 2328261507a0SLi Zefan if (err) 2329261507a0SLi Zefan goto free_compress; 2330261507a0SLi Zefan 2331d1310b2eSChris Mason err = extent_io_init(); 23322f4cbe64SWyatt Banks if (err) 23332f4cbe64SWyatt Banks goto free_cachep; 23342f4cbe64SWyatt Banks 2335d1310b2eSChris Mason err = extent_map_init(); 2336d1310b2eSChris Mason if (err) 2337d1310b2eSChris Mason goto free_extent_io; 2338d1310b2eSChris Mason 23396352b91dSMiao Xie err = ordered_data_init(); 23402f4cbe64SWyatt Banks if (err) 23412f4cbe64SWyatt Banks goto free_extent_map; 2342c8b97818SChris Mason 23436352b91dSMiao Xie err = btrfs_delayed_inode_init(); 23446352b91dSMiao Xie if (err) 23456352b91dSMiao Xie goto free_ordered_data; 23466352b91dSMiao Xie 23479247f317SMiao Xie err = btrfs_auto_defrag_init(); 234816cdcec7SMiao Xie if (err) 234916cdcec7SMiao Xie goto free_delayed_inode; 235016cdcec7SMiao Xie 235178a6184aSMiao Xie err = btrfs_delayed_ref_init(); 23529247f317SMiao Xie if (err) 23539247f317SMiao Xie goto free_auto_defrag; 23549247f317SMiao Xie 2355b9e9a6cbSWang Shilong err = btrfs_prelim_ref_init(); 2356b9e9a6cbSWang Shilong if (err) 2357af13b492SDavid Sterba goto free_delayed_ref; 2358b9e9a6cbSWang Shilong 235997eb6b69SDavid Sterba err = btrfs_end_io_wq_init(); 236078a6184aSMiao Xie if (err) 2361af13b492SDavid Sterba goto free_prelim_ref; 236278a6184aSMiao Xie 236397eb6b69SDavid Sterba err = btrfs_interface_init(); 236497eb6b69SDavid Sterba if (err) 236597eb6b69SDavid Sterba goto free_end_io_wq; 236697eb6b69SDavid Sterba 2367e565d4b9SJan Schmidt btrfs_init_lockdep(); 2368e565d4b9SJan Schmidt 236985965600SDavid Sterba btrfs_print_info(); 2370dc11dd5dSJosef Bacik 2371dc11dd5dSJosef Bacik err = btrfs_run_sanity_tests(); 2372dc11dd5dSJosef Bacik if (err) 2373dc11dd5dSJosef Bacik goto unregister_ioctl; 2374dc11dd5dSJosef Bacik 2375dc11dd5dSJosef Bacik err = register_filesystem(&btrfs_fs_type); 2376dc11dd5dSJosef Bacik if (err) 2377dc11dd5dSJosef Bacik goto unregister_ioctl; 237874255aa0SJosef Bacik 23792f4cbe64SWyatt Banks return 0; 23802f4cbe64SWyatt Banks 2381a9218f6bSChris Mason unregister_ioctl: 2382a9218f6bSChris Mason btrfs_interface_exit(); 238397eb6b69SDavid Sterba free_end_io_wq: 238497eb6b69SDavid Sterba btrfs_end_io_wq_exit(); 2385b9e9a6cbSWang Shilong free_prelim_ref: 2386b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 238778a6184aSMiao Xie free_delayed_ref: 238878a6184aSMiao Xie btrfs_delayed_ref_exit(); 23899247f317SMiao Xie free_auto_defrag: 23909247f317SMiao Xie btrfs_auto_defrag_exit(); 239116cdcec7SMiao Xie free_delayed_inode: 239216cdcec7SMiao Xie btrfs_delayed_inode_exit(); 23936352b91dSMiao Xie free_ordered_data: 23946352b91dSMiao Xie ordered_data_exit(); 23952f4cbe64SWyatt Banks free_extent_map: 23962f4cbe64SWyatt Banks extent_map_exit(); 2397d1310b2eSChris Mason free_extent_io: 2398d1310b2eSChris Mason extent_io_exit(); 23992f4cbe64SWyatt Banks free_cachep: 24002f4cbe64SWyatt Banks btrfs_destroy_cachep(); 2401261507a0SLi Zefan free_compress: 2402261507a0SLi Zefan btrfs_exit_compress(); 24032f4cbe64SWyatt Banks btrfs_exit_sysfs(); 240414a958e6SFilipe David Borba Manana free_hash: 240514a958e6SFilipe David Borba Manana btrfs_hash_exit(); 24062c90e5d6SChris Mason return err; 24072e635a27SChris Mason } 24082e635a27SChris Mason 24092e635a27SChris Mason static void __exit exit_btrfs_fs(void) 24102e635a27SChris Mason { 241139279cc3SChris Mason btrfs_destroy_cachep(); 241278a6184aSMiao Xie btrfs_delayed_ref_exit(); 24139247f317SMiao Xie btrfs_auto_defrag_exit(); 241416cdcec7SMiao Xie btrfs_delayed_inode_exit(); 2415b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 24166352b91dSMiao Xie ordered_data_exit(); 2417a52d9a80SChris Mason extent_map_exit(); 2418d1310b2eSChris Mason extent_io_exit(); 2419a9218f6bSChris Mason btrfs_interface_exit(); 24205ed5f588SJosef Bacik btrfs_end_io_wq_exit(); 24212e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 242258176a96SJosef Bacik btrfs_exit_sysfs(); 24238a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 2424261507a0SLi Zefan btrfs_exit_compress(); 242514a958e6SFilipe David Borba Manana btrfs_hash_exit(); 24262e635a27SChris Mason } 24272e635a27SChris Mason 242860efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs); 24292e635a27SChris Mason module_exit(exit_btrfs_fs) 24302e635a27SChris Mason 24312e635a27SChris Mason MODULE_LICENSE("GPL"); 2432