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 7208748810SDavid Sterba static 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 133533574c6SJoe Perches #ifdef CONFIG_PRINTK 134acce952bSliubo /* 135acce952bSliubo * __btrfs_std_error decodes expected errors from the caller and 136acce952bSliubo * invokes the approciate error response. 137acce952bSliubo */ 138c0d19e2bSDavid Sterba __cold 139acce952bSliubo void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function, 1404da35113SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 141acce952bSliubo { 142acce952bSliubo struct super_block *sb = fs_info->sb; 143acce952bSliubo const char *errstr; 144acce952bSliubo 145acce952bSliubo /* 146acce952bSliubo * Special case: if the error is EROFS, and we're already 147acce952bSliubo * under MS_RDONLY, then it is safe here. 148acce952bSliubo */ 149acce952bSliubo if (errno == -EROFS && (sb->s_flags & MS_RDONLY)) 150acce952bSliubo return; 151acce952bSliubo 15208748810SDavid Sterba errstr = btrfs_decode_error(errno); 1534da35113SJeff Mahoney if (fmt) { 15437252a66SEric Sandeen struct va_format vaf; 15537252a66SEric Sandeen va_list args; 15637252a66SEric Sandeen 15737252a66SEric Sandeen va_start(args, fmt); 15837252a66SEric Sandeen vaf.fmt = fmt; 15937252a66SEric Sandeen vaf.va = &args; 1604da35113SJeff Mahoney 161efe120a0SFrank Holton printk(KERN_CRIT 162efe120a0SFrank Holton "BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n", 16308748810SDavid Sterba sb->s_id, function, line, errno, errstr, &vaf); 16437252a66SEric Sandeen va_end(args); 1654da35113SJeff Mahoney } else { 166efe120a0SFrank Holton printk(KERN_CRIT "BTRFS: error (device %s) in %s:%d: errno=%d %s\n", 16708748810SDavid Sterba sb->s_id, function, line, errno, errstr); 1684da35113SJeff Mahoney } 169acce952bSliubo 1704da35113SJeff Mahoney /* Don't go through full error handling during mount */ 1714da35113SJeff Mahoney save_error_info(fs_info); 172cf79ffb5SJosef Bacik if (sb->s_flags & MS_BORN) 173acce952bSliubo btrfs_handle_error(fs_info); 174acce952bSliubo } 1754da35113SJeff Mahoney 176533574c6SJoe Perches static const char * const logtypes[] = { 1774da35113SJeff Mahoney "emergency", 1784da35113SJeff Mahoney "alert", 1794da35113SJeff Mahoney "critical", 1804da35113SJeff Mahoney "error", 1814da35113SJeff Mahoney "warning", 1824da35113SJeff Mahoney "notice", 1834da35113SJeff Mahoney "info", 1844da35113SJeff Mahoney "debug", 1854da35113SJeff Mahoney }; 1864da35113SJeff Mahoney 187c2cf52ebSSimon Kirby void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...) 1884da35113SJeff Mahoney { 1894da35113SJeff Mahoney struct super_block *sb = fs_info->sb; 1904da35113SJeff Mahoney char lvl[4]; 1914da35113SJeff Mahoney struct va_format vaf; 1924da35113SJeff Mahoney va_list args; 1934da35113SJeff Mahoney const char *type = logtypes[4]; 194533574c6SJoe Perches int kern_level; 1954da35113SJeff Mahoney 1964da35113SJeff Mahoney va_start(args, fmt); 1974da35113SJeff Mahoney 198533574c6SJoe Perches kern_level = printk_get_level(fmt); 199533574c6SJoe Perches if (kern_level) { 200533574c6SJoe Perches size_t size = printk_skip_level(fmt) - fmt; 201533574c6SJoe Perches memcpy(lvl, fmt, size); 202533574c6SJoe Perches lvl[size] = '\0'; 203533574c6SJoe Perches fmt += size; 204533574c6SJoe Perches type = logtypes[kern_level - '0']; 2054da35113SJeff Mahoney } else 2064da35113SJeff Mahoney *lvl = '\0'; 2074da35113SJeff Mahoney 2084da35113SJeff Mahoney vaf.fmt = fmt; 2094da35113SJeff Mahoney vaf.va = &args; 210533574c6SJoe Perches 211c2cf52ebSSimon Kirby printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf); 212533574c6SJoe Perches 213533574c6SJoe Perches va_end(args); 2144da35113SJeff Mahoney } 215acce952bSliubo 216533574c6SJoe Perches #else 217533574c6SJoe Perches 218533574c6SJoe Perches void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function, 219533574c6SJoe Perches unsigned int line, int errno, const char *fmt, ...) 220533574c6SJoe Perches { 221533574c6SJoe Perches struct super_block *sb = fs_info->sb; 222533574c6SJoe Perches 223533574c6SJoe Perches /* 224533574c6SJoe Perches * Special case: if the error is EROFS, and we're already 225533574c6SJoe Perches * under MS_RDONLY, then it is safe here. 226533574c6SJoe Perches */ 227533574c6SJoe Perches if (errno == -EROFS && (sb->s_flags & MS_RDONLY)) 228533574c6SJoe Perches return; 229533574c6SJoe Perches 230533574c6SJoe Perches /* Don't go through full error handling during mount */ 231533574c6SJoe Perches if (sb->s_flags & MS_BORN) { 232533574c6SJoe Perches save_error_info(fs_info); 233533574c6SJoe Perches btrfs_handle_error(fs_info); 234533574c6SJoe Perches } 235533574c6SJoe Perches } 236533574c6SJoe Perches #endif 237533574c6SJoe Perches 2388c342930SJeff Mahoney /* 23949b25e05SJeff Mahoney * We only mark the transaction aborted and then set the file system read-only. 24049b25e05SJeff Mahoney * This will prevent new transactions from starting or trying to join this 24149b25e05SJeff Mahoney * one. 24249b25e05SJeff Mahoney * 24349b25e05SJeff Mahoney * This means that error recovery at the call site is limited to freeing 24449b25e05SJeff Mahoney * any local memory allocations and passing the error code up without 24549b25e05SJeff Mahoney * further cleanup. The transaction should complete as it normally would 24649b25e05SJeff Mahoney * in the call path but will return -EIO. 24749b25e05SJeff Mahoney * 24849b25e05SJeff Mahoney * We'll complete the cleanup in btrfs_end_transaction and 24949b25e05SJeff Mahoney * btrfs_commit_transaction. 25049b25e05SJeff Mahoney */ 251c0d19e2bSDavid Sterba __cold 25249b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans, 25349b25e05SJeff Mahoney struct btrfs_root *root, const char *function, 25449b25e05SJeff Mahoney unsigned int line, int errno) 25549b25e05SJeff Mahoney { 25649b25e05SJeff Mahoney trans->aborted = errno; 25749b25e05SJeff Mahoney /* Nothing used. The other threads that have joined this 25849b25e05SJeff Mahoney * transaction may be able to continue. */ 259c92f6be3SFilipe Manana if (!trans->blocks_used && list_empty(&trans->new_bgs)) { 26069ce977aSMiao Xie const char *errstr; 26169ce977aSMiao Xie 26208748810SDavid Sterba errstr = btrfs_decode_error(errno); 263c2cf52ebSSimon Kirby btrfs_warn(root->fs_info, 264c2cf52ebSSimon Kirby "%s:%d: Aborting unused transaction(%s).", 26569ce977aSMiao Xie function, line, errstr); 26649b25e05SJeff Mahoney return; 26749b25e05SJeff Mahoney } 2688d25a086SMiao Xie ACCESS_ONCE(trans->transaction->aborted) = errno; 269501407aaSJosef Bacik /* Wake up anybody who may be waiting on this transaction */ 270501407aaSJosef Bacik wake_up(&root->fs_info->transaction_wait); 271501407aaSJosef Bacik wake_up(&root->fs_info->transaction_blocked_wait); 27249b25e05SJeff Mahoney __btrfs_std_error(root->fs_info, function, line, errno, NULL); 27349b25e05SJeff Mahoney } 27449b25e05SJeff Mahoney /* 2758c342930SJeff Mahoney * __btrfs_panic decodes unexpected, fatal errors from the caller, 2768c342930SJeff Mahoney * issues an alert, and either panics or BUGs, depending on mount options. 2778c342930SJeff Mahoney */ 278c0d19e2bSDavid Sterba __cold 2798c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, 2808c342930SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 2818c342930SJeff Mahoney { 2828c342930SJeff Mahoney char *s_id = "<unknown>"; 2838c342930SJeff Mahoney const char *errstr; 2848c342930SJeff Mahoney struct va_format vaf = { .fmt = fmt }; 2858c342930SJeff Mahoney va_list args; 2868c342930SJeff Mahoney 2878c342930SJeff Mahoney if (fs_info) 2888c342930SJeff Mahoney s_id = fs_info->sb->s_id; 2898c342930SJeff Mahoney 2908c342930SJeff Mahoney va_start(args, fmt); 2918c342930SJeff Mahoney vaf.va = &args; 2928c342930SJeff Mahoney 29308748810SDavid Sterba errstr = btrfs_decode_error(errno); 294aa43a17cSEric Sandeen if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR)) 29508748810SDavid Sterba panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n", 29608748810SDavid Sterba s_id, function, line, &vaf, errno, errstr); 2978c342930SJeff Mahoney 298efe120a0SFrank Holton btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)", 299efe120a0SFrank Holton function, line, &vaf, errno, errstr); 3008c342930SJeff Mahoney va_end(args); 3018c342930SJeff Mahoney /* Caller calls BUG() */ 3028c342930SJeff Mahoney } 303e20d96d6SChris Mason 304e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 305e20d96d6SChris Mason { 3063abdbd78SDavid Sterba close_ctree(btrfs_sb(sb)->tree_root); 307e20d96d6SChris Mason } 3082e635a27SChris Mason 30995e05289SChris Mason enum { 31073f73415SJosef Bacik Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum, 311287a0ab9SJosef Bacik Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, 312287a0ab9SJosef Bacik Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress, 313261507a0SLi Zefan Opt_compress_type, Opt_compress_force, Opt_compress_force_type, 314261507a0SLi Zefan Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard, 31591435650SChris Mason Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed, 3169555c6c1SIlya Dryomov Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache, 3179555c6c1SIlya Dryomov Opt_no_space_cache, Opt_recovery, Opt_skip_balance, 31821adbd5cSStefan Behrens Opt_check_integrity, Opt_check_integrity_including_extent_data, 319f420ee1eSStefan Behrens Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree, 320e07a2adeSQu Wenruo Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard, 321a258af7aSQu Wenruo Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow, 3223818aea2SQu Wenruo Opt_datasum, Opt_treelog, Opt_noinode_cache, 3239555c6c1SIlya Dryomov Opt_err, 32495e05289SChris Mason }; 32595e05289SChris Mason 32695e05289SChris Mason static match_table_t tokens = { 327dfe25020SChris Mason {Opt_degraded, "degraded"}, 32895e05289SChris Mason {Opt_subvol, "subvol=%s"}, 3291493381fSWang Shilong {Opt_subvolid, "subvolid=%s"}, 33043e570b0SChristoph Hellwig {Opt_device, "device=%s"}, 331b6cda9bcSChris Mason {Opt_nodatasum, "nodatasum"}, 332d399167dSQu Wenruo {Opt_datasum, "datasum"}, 333be20aa9dSChris Mason {Opt_nodatacow, "nodatacow"}, 334a258af7aSQu Wenruo {Opt_datacow, "datacow"}, 33521ad10cfSChris Mason {Opt_nobarrier, "nobarrier"}, 336842bef58SQu Wenruo {Opt_barrier, "barrier"}, 3376f568d35SChris Mason {Opt_max_inline, "max_inline=%s"}, 3388f662a76SChris Mason {Opt_alloc_start, "alloc_start=%s"}, 3394543df7eSChris Mason {Opt_thread_pool, "thread_pool=%d"}, 340c8b97818SChris Mason {Opt_compress, "compress"}, 341261507a0SLi Zefan {Opt_compress_type, "compress=%s"}, 342a555f810SChris Mason {Opt_compress_force, "compress-force"}, 343261507a0SLi Zefan {Opt_compress_force_type, "compress-force=%s"}, 344e18e4809SChris Mason {Opt_ssd, "ssd"}, 345451d7585SChris Mason {Opt_ssd_spread, "ssd_spread"}, 3463b30c22fSChris Mason {Opt_nossd, "nossd"}, 347bd0330adSQu Wenruo {Opt_acl, "acl"}, 34833268eafSJosef Bacik {Opt_noacl, "noacl"}, 3493a5e1404SSage Weil {Opt_notreelog, "notreelog"}, 350a88998f2SQu Wenruo {Opt_treelog, "treelog"}, 351dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 3522c9ee856SQu Wenruo {Opt_noflushoncommit, "noflushoncommit"}, 35397e728d4SJosef Bacik {Opt_ratio, "metadata_ratio=%d"}, 354e244a0aeSChristoph Hellwig {Opt_discard, "discard"}, 355e07a2adeSQu Wenruo {Opt_nodiscard, "nodiscard"}, 3560af3d00bSJosef Bacik {Opt_space_cache, "space_cache"}, 35788c2ba3bSJosef Bacik {Opt_clear_cache, "clear_cache"}, 3584260f7c7SSage Weil {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, 35991435650SChris Mason {Opt_enospc_debug, "enospc_debug"}, 36053036293SQu Wenruo {Opt_noenospc_debug, "noenospc_debug"}, 361e15d0542SXin Zhong {Opt_subvolrootid, "subvolrootid=%d"}, 3624cb5300bSChris Mason {Opt_defrag, "autodefrag"}, 363fc0ca9afSQu Wenruo {Opt_nodefrag, "noautodefrag"}, 3644b9465cbSChris Mason {Opt_inode_cache, "inode_cache"}, 3653818aea2SQu Wenruo {Opt_noinode_cache, "noinode_cache"}, 3668965593eSDavid Sterba {Opt_no_space_cache, "nospace_cache"}, 367af31f5e5SChris Mason {Opt_recovery, "recovery"}, 3689555c6c1SIlya Dryomov {Opt_skip_balance, "skip_balance"}, 36921adbd5cSStefan Behrens {Opt_check_integrity, "check_int"}, 37021adbd5cSStefan Behrens {Opt_check_integrity_including_extent_data, "check_int_data"}, 37121adbd5cSStefan Behrens {Opt_check_integrity_print_mask, "check_int_print_mask=%d"}, 372f420ee1eSStefan Behrens {Opt_rescan_uuid_tree, "rescan_uuid_tree"}, 3738c342930SJeff Mahoney {Opt_fatal_errors, "fatal_errors=%s"}, 3748b87dc17SDavid Sterba {Opt_commit_interval, "commit=%d"}, 37533268eafSJosef Bacik {Opt_err, NULL}, 37695e05289SChris Mason }; 37795e05289SChris Mason 378edf24abeSChristoph Hellwig /* 379edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 380edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 38149b25e05SJeff Mahoney * XXX JDM: This needs to be cleaned up for remount. 382edf24abeSChristoph Hellwig */ 383edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options) 38495e05289SChris Mason { 385edf24abeSChristoph Hellwig struct btrfs_fs_info *info = root->fs_info; 38695e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 38773bc1876SJosef Bacik char *p, *num, *orig = NULL; 38873bc1876SJosef Bacik u64 cache_gen; 3894543df7eSChris Mason int intarg; 390a7a3f7caSSage Weil int ret = 0; 391261507a0SLi Zefan char *compress_type; 392261507a0SLi Zefan bool compress_force = false; 393b6cda9bcSChris Mason 3946c41761fSDavid Sterba cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy); 39573bc1876SJosef Bacik if (cache_gen) 39673bc1876SJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 39773bc1876SJosef Bacik 39895e05289SChris Mason if (!options) 39973bc1876SJosef Bacik goto out; 40095e05289SChris Mason 401be20aa9dSChris Mason /* 402be20aa9dSChris Mason * strsep changes the string, duplicate it because parse_options 403be20aa9dSChris Mason * gets called twice 404be20aa9dSChris Mason */ 405be20aa9dSChris Mason options = kstrdup(options, GFP_NOFS); 406be20aa9dSChris Mason if (!options) 407be20aa9dSChris Mason return -ENOMEM; 408be20aa9dSChris Mason 409da495eccSJosef Bacik orig = options; 410be20aa9dSChris Mason 41195e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 41295e05289SChris Mason int token; 41395e05289SChris Mason if (!*p) 41495e05289SChris Mason continue; 41595e05289SChris Mason 41695e05289SChris Mason token = match_token(p, tokens, args); 41795e05289SChris Mason switch (token) { 418dfe25020SChris Mason case Opt_degraded: 419efe120a0SFrank Holton btrfs_info(root->fs_info, "allowing degraded mounts"); 420dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 421dfe25020SChris Mason break; 42295e05289SChris Mason case Opt_subvol: 42373f73415SJosef Bacik case Opt_subvolid: 424e15d0542SXin Zhong case Opt_subvolrootid: 42543e570b0SChristoph Hellwig case Opt_device: 426edf24abeSChristoph Hellwig /* 42743e570b0SChristoph Hellwig * These are parsed by btrfs_parse_early_options 428edf24abeSChristoph Hellwig * and can be happily ignored here. 429edf24abeSChristoph Hellwig */ 43095e05289SChris Mason break; 431b6cda9bcSChris Mason case Opt_nodatasum: 43207802534SQu Wenruo btrfs_set_and_info(root, NODATASUM, 43307802534SQu Wenruo "setting nodatasum"); 434be20aa9dSChris Mason break; 435d399167dSQu Wenruo case Opt_datasum: 43607802534SQu Wenruo if (btrfs_test_opt(root, NODATASUM)) { 437d399167dSQu Wenruo if (btrfs_test_opt(root, NODATACOW)) 438d399167dSQu Wenruo btrfs_info(root->fs_info, "setting datasum, datacow enabled"); 439d399167dSQu Wenruo else 440d399167dSQu Wenruo btrfs_info(root->fs_info, "setting datasum"); 44107802534SQu Wenruo } 442d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATACOW); 443d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATASUM); 444d399167dSQu Wenruo break; 445be20aa9dSChris Mason case Opt_nodatacow: 44607802534SQu Wenruo if (!btrfs_test_opt(root, NODATACOW)) { 447bedb2ccaSAndrei Popa if (!btrfs_test_opt(root, COMPRESS) || 448bedb2ccaSAndrei Popa !btrfs_test_opt(root, FORCE_COMPRESS)) { 449efe120a0SFrank Holton btrfs_info(root->fs_info, 450efe120a0SFrank Holton "setting nodatacow, compression disabled"); 451bedb2ccaSAndrei Popa } else { 452efe120a0SFrank Holton btrfs_info(root->fs_info, "setting nodatacow"); 453bedb2ccaSAndrei Popa } 45407802534SQu Wenruo } 455bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, COMPRESS); 456bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 457be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 458be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 459b6cda9bcSChris Mason break; 460a258af7aSQu Wenruo case Opt_datacow: 46107802534SQu Wenruo btrfs_clear_and_info(root, NODATACOW, 46207802534SQu Wenruo "setting datacow"); 463a258af7aSQu Wenruo break; 464a555f810SChris Mason case Opt_compress_force: 465261507a0SLi Zefan case Opt_compress_force_type: 466261507a0SLi Zefan compress_force = true; 4671c697d4aSEric Sandeen /* Fallthrough */ 468261507a0SLi Zefan case Opt_compress: 469261507a0SLi Zefan case Opt_compress_type: 470261507a0SLi Zefan if (token == Opt_compress || 471261507a0SLi Zefan token == Opt_compress_force || 472261507a0SLi Zefan strcmp(args[0].from, "zlib") == 0) { 473261507a0SLi Zefan compress_type = "zlib"; 474261507a0SLi Zefan info->compress_type = BTRFS_COMPRESS_ZLIB; 475063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 476bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 477bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 478a6fa6faeSLi Zefan } else if (strcmp(args[0].from, "lzo") == 0) { 479a6fa6faeSLi Zefan compress_type = "lzo"; 480a6fa6faeSLi Zefan info->compress_type = BTRFS_COMPRESS_LZO; 481063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 482bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 483bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 4842b0ce2c2SMitch Harder btrfs_set_fs_incompat(info, COMPRESS_LZO); 485063849eaSArnd Hannemann } else if (strncmp(args[0].from, "no", 2) == 0) { 486063849eaSArnd Hannemann compress_type = "no"; 487063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, COMPRESS); 488063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 489063849eaSArnd Hannemann compress_force = false; 490261507a0SLi Zefan } else { 491261507a0SLi Zefan ret = -EINVAL; 492261507a0SLi Zefan goto out; 493261507a0SLi Zefan } 494261507a0SLi Zefan 495261507a0SLi Zefan if (compress_force) { 49607802534SQu Wenruo btrfs_set_and_info(root, FORCE_COMPRESS, 49707802534SQu Wenruo "force %s compression", 498261507a0SLi Zefan compress_type); 499143f3636SDavid Sterba } else { 50007802534SQu Wenruo if (!btrfs_test_opt(root, COMPRESS)) 50107802534SQu Wenruo btrfs_info(root->fs_info, 502351fd353SDavid Sterba "btrfs: use %s compression", 503261507a0SLi Zefan compress_type); 5044027e0f4SWang Shilong /* 5054027e0f4SWang Shilong * If we remount from compress-force=xxx to 5064027e0f4SWang Shilong * compress=xxx, we need clear FORCE_COMPRESS 5074027e0f4SWang Shilong * flag, otherwise, there is no way for users 5084027e0f4SWang Shilong * to disable forcible compression separately. 5094027e0f4SWang Shilong */ 5104027e0f4SWang Shilong btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 511a7e252afSMiao Xie } 512a555f810SChris Mason break; 513e18e4809SChris Mason case Opt_ssd: 51407802534SQu Wenruo btrfs_set_and_info(root, SSD, 51507802534SQu Wenruo "use ssd allocation scheme"); 516e18e4809SChris Mason break; 517451d7585SChris Mason case Opt_ssd_spread: 51807802534SQu Wenruo btrfs_set_and_info(root, SSD_SPREAD, 51907802534SQu Wenruo "use spread ssd allocation scheme"); 5202aa06a35SEric Sandeen btrfs_set_opt(info->mount_opt, SSD); 521451d7585SChris Mason break; 5223b30c22fSChris Mason case Opt_nossd: 5232aa06a35SEric Sandeen btrfs_set_and_info(root, NOSSD, 52407802534SQu Wenruo "not using ssd allocation scheme"); 5253b30c22fSChris Mason btrfs_clear_opt(info->mount_opt, SSD); 5263b30c22fSChris Mason break; 527842bef58SQu Wenruo case Opt_barrier: 52807802534SQu Wenruo btrfs_clear_and_info(root, NOBARRIER, 52907802534SQu Wenruo "turning on barriers"); 530842bef58SQu Wenruo break; 53121ad10cfSChris Mason case Opt_nobarrier: 53207802534SQu Wenruo btrfs_set_and_info(root, NOBARRIER, 53307802534SQu Wenruo "turning off barriers"); 53421ad10cfSChris Mason break; 5354543df7eSChris Mason case Opt_thread_pool: 5362c334e87SWang Shilong ret = match_int(&args[0], &intarg); 5372c334e87SWang Shilong if (ret) { 5382c334e87SWang Shilong goto out; 5392c334e87SWang Shilong } else if (intarg > 0) { 5404543df7eSChris Mason info->thread_pool_size = intarg; 5412c334e87SWang Shilong } else { 5422c334e87SWang Shilong ret = -EINVAL; 5432c334e87SWang Shilong goto out; 5442c334e87SWang Shilong } 5454543df7eSChris Mason break; 5466f568d35SChris Mason case Opt_max_inline: 547edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5486f568d35SChris Mason if (num) { 54991748467SAkinobu Mita info->max_inline = memparse(num, NULL); 5506f568d35SChris Mason kfree(num); 5516f568d35SChris Mason 55215ada040SChris Mason if (info->max_inline) { 553feb5f965SMitch Harder info->max_inline = min_t(u64, 55415ada040SChris Mason info->max_inline, 55515ada040SChris Mason root->sectorsize); 55615ada040SChris Mason } 557efe120a0SFrank Holton btrfs_info(root->fs_info, "max_inline at %llu", 558c1c9ff7cSGeert Uytterhoeven info->max_inline); 5592c334e87SWang Shilong } else { 5602c334e87SWang Shilong ret = -ENOMEM; 5612c334e87SWang Shilong goto out; 5626f568d35SChris Mason } 5636f568d35SChris Mason break; 5648f662a76SChris Mason case Opt_alloc_start: 565edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5668f662a76SChris Mason if (num) { 567c018daecSMiao Xie mutex_lock(&info->chunk_mutex); 56891748467SAkinobu Mita info->alloc_start = memparse(num, NULL); 569c018daecSMiao Xie mutex_unlock(&info->chunk_mutex); 5708f662a76SChris Mason kfree(num); 571efe120a0SFrank Holton btrfs_info(root->fs_info, "allocations start at %llu", 572c1c9ff7cSGeert Uytterhoeven info->alloc_start); 5732c334e87SWang Shilong } else { 5742c334e87SWang Shilong ret = -ENOMEM; 5752c334e87SWang Shilong goto out; 5768f662a76SChris Mason } 5778f662a76SChris Mason break; 578bd0330adSQu Wenruo case Opt_acl: 57945ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL 580bd0330adSQu Wenruo root->fs_info->sb->s_flags |= MS_POSIXACL; 581bd0330adSQu Wenruo break; 58245ff35d6SGuangliang Zhao #else 58345ff35d6SGuangliang Zhao btrfs_err(root->fs_info, 58445ff35d6SGuangliang Zhao "support for ACL not compiled in!"); 58545ff35d6SGuangliang Zhao ret = -EINVAL; 58645ff35d6SGuangliang Zhao goto out; 58745ff35d6SGuangliang Zhao #endif 58833268eafSJosef Bacik case Opt_noacl: 58933268eafSJosef Bacik root->fs_info->sb->s_flags &= ~MS_POSIXACL; 59033268eafSJosef Bacik break; 5913a5e1404SSage Weil case Opt_notreelog: 59207802534SQu Wenruo btrfs_set_and_info(root, NOTREELOG, 59307802534SQu Wenruo "disabling tree log"); 5943a5e1404SSage Weil break; 595a88998f2SQu Wenruo case Opt_treelog: 59607802534SQu Wenruo btrfs_clear_and_info(root, NOTREELOG, 59707802534SQu Wenruo "enabling tree log"); 598a88998f2SQu Wenruo break; 599dccae999SSage Weil case Opt_flushoncommit: 60007802534SQu Wenruo btrfs_set_and_info(root, FLUSHONCOMMIT, 60107802534SQu Wenruo "turning on flush-on-commit"); 602dccae999SSage Weil break; 6032c9ee856SQu Wenruo case Opt_noflushoncommit: 60407802534SQu Wenruo btrfs_clear_and_info(root, FLUSHONCOMMIT, 60507802534SQu Wenruo "turning off flush-on-commit"); 6062c9ee856SQu Wenruo break; 60797e728d4SJosef Bacik case Opt_ratio: 6082c334e87SWang Shilong ret = match_int(&args[0], &intarg); 6092c334e87SWang Shilong if (ret) { 6102c334e87SWang Shilong goto out; 6112c334e87SWang Shilong } else if (intarg >= 0) { 61297e728d4SJosef Bacik info->metadata_ratio = intarg; 613efe120a0SFrank Holton btrfs_info(root->fs_info, "metadata ratio %d", 61497e728d4SJosef Bacik info->metadata_ratio); 6152c334e87SWang Shilong } else { 6162c334e87SWang Shilong ret = -EINVAL; 6172c334e87SWang Shilong goto out; 61897e728d4SJosef Bacik } 61997e728d4SJosef Bacik break; 620e244a0aeSChristoph Hellwig case Opt_discard: 62107802534SQu Wenruo btrfs_set_and_info(root, DISCARD, 62207802534SQu Wenruo "turning on discard"); 623e244a0aeSChristoph Hellwig break; 624e07a2adeSQu Wenruo case Opt_nodiscard: 62507802534SQu Wenruo btrfs_clear_and_info(root, DISCARD, 62607802534SQu Wenruo "turning off discard"); 627e07a2adeSQu Wenruo break; 6280af3d00bSJosef Bacik case Opt_space_cache: 62907802534SQu Wenruo btrfs_set_and_info(root, SPACE_CACHE, 63007802534SQu Wenruo "enabling disk space caching"); 6310de90876SJosef Bacik break; 632f420ee1eSStefan Behrens case Opt_rescan_uuid_tree: 633f420ee1eSStefan Behrens btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE); 634f420ee1eSStefan Behrens break; 63573bc1876SJosef Bacik case Opt_no_space_cache: 63607802534SQu Wenruo btrfs_clear_and_info(root, SPACE_CACHE, 63707802534SQu Wenruo "disabling disk space caching"); 63873bc1876SJosef Bacik break; 6394b9465cbSChris Mason case Opt_inode_cache: 6407e1876acSDavid Sterba btrfs_set_pending_and_info(info, INODE_MAP_CACHE, 64107802534SQu Wenruo "enabling inode map caching"); 6423818aea2SQu Wenruo break; 6433818aea2SQu Wenruo case Opt_noinode_cache: 6447e1876acSDavid Sterba btrfs_clear_pending_and_info(info, INODE_MAP_CACHE, 64507802534SQu Wenruo "disabling inode map caching"); 6464b9465cbSChris Mason break; 64788c2ba3bSJosef Bacik case Opt_clear_cache: 64807802534SQu Wenruo btrfs_set_and_info(root, CLEAR_CACHE, 64907802534SQu Wenruo "force clearing of disk cache"); 6500af3d00bSJosef Bacik break; 6514260f7c7SSage Weil case Opt_user_subvol_rm_allowed: 6524260f7c7SSage Weil btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 6534260f7c7SSage Weil break; 65491435650SChris Mason case Opt_enospc_debug: 65591435650SChris Mason btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 65691435650SChris Mason break; 65753036293SQu Wenruo case Opt_noenospc_debug: 65853036293SQu Wenruo btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG); 65953036293SQu Wenruo break; 6604cb5300bSChris Mason case Opt_defrag: 66107802534SQu Wenruo btrfs_set_and_info(root, AUTO_DEFRAG, 66207802534SQu Wenruo "enabling auto defrag"); 6634cb5300bSChris Mason break; 664fc0ca9afSQu Wenruo case Opt_nodefrag: 66507802534SQu Wenruo btrfs_clear_and_info(root, AUTO_DEFRAG, 66607802534SQu Wenruo "disabling auto defrag"); 667fc0ca9afSQu Wenruo break; 668af31f5e5SChris Mason case Opt_recovery: 669efe120a0SFrank Holton btrfs_info(root->fs_info, "enabling auto recovery"); 670af31f5e5SChris Mason btrfs_set_opt(info->mount_opt, RECOVERY); 671af31f5e5SChris Mason break; 6729555c6c1SIlya Dryomov case Opt_skip_balance: 6739555c6c1SIlya Dryomov btrfs_set_opt(info->mount_opt, SKIP_BALANCE); 6749555c6c1SIlya Dryomov break; 67521adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 67621adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 677efe120a0SFrank Holton btrfs_info(root->fs_info, 678efe120a0SFrank Holton "enabling check integrity including extent data"); 67921adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, 68021adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA); 68121adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 68221adbd5cSStefan Behrens break; 68321adbd5cSStefan Behrens case Opt_check_integrity: 684efe120a0SFrank Holton btrfs_info(root->fs_info, "enabling check integrity"); 68521adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 68621adbd5cSStefan Behrens break; 68721adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 6882c334e87SWang Shilong ret = match_int(&args[0], &intarg); 6892c334e87SWang Shilong if (ret) { 6902c334e87SWang Shilong goto out; 6912c334e87SWang Shilong } else if (intarg >= 0) { 69221adbd5cSStefan Behrens info->check_integrity_print_mask = intarg; 693efe120a0SFrank Holton btrfs_info(root->fs_info, "check_integrity_print_mask 0x%x", 69421adbd5cSStefan Behrens info->check_integrity_print_mask); 6952c334e87SWang Shilong } else { 6962c334e87SWang Shilong ret = -EINVAL; 6972c334e87SWang Shilong goto out; 69821adbd5cSStefan Behrens } 69921adbd5cSStefan Behrens break; 70021adbd5cSStefan Behrens #else 70121adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 70221adbd5cSStefan Behrens case Opt_check_integrity: 70321adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 704efe120a0SFrank Holton btrfs_err(root->fs_info, 705efe120a0SFrank Holton "support for check_integrity* not compiled in!"); 70621adbd5cSStefan Behrens ret = -EINVAL; 70721adbd5cSStefan Behrens goto out; 70821adbd5cSStefan Behrens #endif 7098c342930SJeff Mahoney case Opt_fatal_errors: 7108c342930SJeff Mahoney if (strcmp(args[0].from, "panic") == 0) 7118c342930SJeff Mahoney btrfs_set_opt(info->mount_opt, 7128c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7138c342930SJeff Mahoney else if (strcmp(args[0].from, "bug") == 0) 7148c342930SJeff Mahoney btrfs_clear_opt(info->mount_opt, 7158c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7168c342930SJeff Mahoney else { 7178c342930SJeff Mahoney ret = -EINVAL; 7188c342930SJeff Mahoney goto out; 7198c342930SJeff Mahoney } 7208c342930SJeff Mahoney break; 7218b87dc17SDavid Sterba case Opt_commit_interval: 7228b87dc17SDavid Sterba intarg = 0; 7238b87dc17SDavid Sterba ret = match_int(&args[0], &intarg); 7248b87dc17SDavid Sterba if (ret < 0) { 725efe120a0SFrank Holton btrfs_err(root->fs_info, "invalid commit interval"); 7268b87dc17SDavid Sterba ret = -EINVAL; 7278b87dc17SDavid Sterba goto out; 7288b87dc17SDavid Sterba } 7298b87dc17SDavid Sterba if (intarg > 0) { 7308b87dc17SDavid Sterba if (intarg > 300) { 731efe120a0SFrank Holton btrfs_warn(root->fs_info, "excessive commit interval %d", 7328b87dc17SDavid Sterba intarg); 7338b87dc17SDavid Sterba } 7348b87dc17SDavid Sterba info->commit_interval = intarg; 7358b87dc17SDavid Sterba } else { 736efe120a0SFrank Holton btrfs_info(root->fs_info, "using default commit interval %ds", 7378b87dc17SDavid Sterba BTRFS_DEFAULT_COMMIT_INTERVAL); 7388b87dc17SDavid Sterba info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 7398b87dc17SDavid Sterba } 7408b87dc17SDavid Sterba break; 741a7a3f7caSSage Weil case Opt_err: 742efe120a0SFrank Holton btrfs_info(root->fs_info, "unrecognized mount option '%s'", p); 743a7a3f7caSSage Weil ret = -EINVAL; 744a7a3f7caSSage Weil goto out; 74595e05289SChris Mason default: 746be20aa9dSChris Mason break; 74795e05289SChris Mason } 74895e05289SChris Mason } 749a7a3f7caSSage Weil out: 75073bc1876SJosef Bacik if (!ret && btrfs_test_opt(root, SPACE_CACHE)) 751efe120a0SFrank Holton btrfs_info(root->fs_info, "disk space caching is enabled"); 752da495eccSJosef Bacik kfree(orig); 753a7a3f7caSSage Weil return ret; 754edf24abeSChristoph Hellwig } 755edf24abeSChristoph Hellwig 756edf24abeSChristoph Hellwig /* 757edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 758edf24abeSChristoph Hellwig * 759edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 760edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 761edf24abeSChristoph Hellwig */ 76297288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 76373f73415SJosef Bacik void *holder, char **subvol_name, u64 *subvol_objectid, 7645e2a4b25SDavid Sterba struct btrfs_fs_devices **fs_devices) 765edf24abeSChristoph Hellwig { 766edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 76783c8c9bdSJeff Liu char *device_name, *opts, *orig, *p; 7681493381fSWang Shilong char *num = NULL; 769edf24abeSChristoph Hellwig int error = 0; 770edf24abeSChristoph Hellwig 771edf24abeSChristoph Hellwig if (!options) 772830c4adbSJosef Bacik return 0; 773edf24abeSChristoph Hellwig 774edf24abeSChristoph Hellwig /* 775edf24abeSChristoph Hellwig * strsep changes the string, duplicate it because parse_options 776edf24abeSChristoph Hellwig * gets called twice 777edf24abeSChristoph Hellwig */ 778edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 779edf24abeSChristoph Hellwig if (!opts) 780edf24abeSChristoph Hellwig return -ENOMEM; 7813f3d0bc0STero Roponen orig = opts; 782edf24abeSChristoph Hellwig 783edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 784edf24abeSChristoph Hellwig int token; 785edf24abeSChristoph Hellwig if (!*p) 786edf24abeSChristoph Hellwig continue; 787edf24abeSChristoph Hellwig 788edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 789edf24abeSChristoph Hellwig switch (token) { 790edf24abeSChristoph Hellwig case Opt_subvol: 791a90e8b6fSIlya Dryomov kfree(*subvol_name); 792edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 7932c334e87SWang Shilong if (!*subvol_name) { 7942c334e87SWang Shilong error = -ENOMEM; 7952c334e87SWang Shilong goto out; 7962c334e87SWang Shilong } 797edf24abeSChristoph Hellwig break; 79873f73415SJosef Bacik case Opt_subvolid: 7991493381fSWang Shilong num = match_strdup(&args[0]); 8001493381fSWang Shilong if (num) { 8011493381fSWang Shilong *subvol_objectid = memparse(num, NULL); 8021493381fSWang Shilong kfree(num); 8034849f01dSJosef Bacik /* we want the original fs_tree */ 8041493381fSWang Shilong if (!*subvol_objectid) 8054849f01dSJosef Bacik *subvol_objectid = 8064849f01dSJosef Bacik BTRFS_FS_TREE_OBJECTID; 8072c334e87SWang Shilong } else { 8082c334e87SWang Shilong error = -EINVAL; 8092c334e87SWang Shilong goto out; 8104849f01dSJosef Bacik } 81173f73415SJosef Bacik break; 812e15d0542SXin Zhong case Opt_subvolrootid: 8135e2a4b25SDavid Sterba printk(KERN_WARNING 814efe120a0SFrank Holton "BTRFS: 'subvolrootid' mount option is deprecated and has " 815efe120a0SFrank Holton "no effect\n"); 816e15d0542SXin Zhong break; 81743e570b0SChristoph Hellwig case Opt_device: 81883c8c9bdSJeff Liu device_name = match_strdup(&args[0]); 81983c8c9bdSJeff Liu if (!device_name) { 82083c8c9bdSJeff Liu error = -ENOMEM; 82183c8c9bdSJeff Liu goto out; 82283c8c9bdSJeff Liu } 82383c8c9bdSJeff Liu error = btrfs_scan_one_device(device_name, 82443e570b0SChristoph Hellwig flags, holder, fs_devices); 82583c8c9bdSJeff Liu kfree(device_name); 82643e570b0SChristoph Hellwig if (error) 827830c4adbSJosef Bacik goto out; 82843e570b0SChristoph Hellwig break; 829edf24abeSChristoph Hellwig default: 830edf24abeSChristoph Hellwig break; 831edf24abeSChristoph Hellwig } 832edf24abeSChristoph Hellwig } 833edf24abeSChristoph Hellwig 834edf24abeSChristoph Hellwig out: 835830c4adbSJosef Bacik kfree(orig); 836edf24abeSChristoph Hellwig return error; 83795e05289SChris Mason } 83895e05289SChris Mason 83973f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb, 84073f73415SJosef Bacik u64 subvol_objectid) 84173f73415SJosef Bacik { 842815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 843815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 84473f73415SJosef Bacik struct btrfs_root *new_root; 84573f73415SJosef Bacik struct btrfs_dir_item *di; 84673f73415SJosef Bacik struct btrfs_path *path; 84773f73415SJosef Bacik struct btrfs_key location; 84873f73415SJosef Bacik struct inode *inode; 84973f73415SJosef Bacik u64 dir_id; 85073f73415SJosef Bacik int new = 0; 85173f73415SJosef Bacik 85273f73415SJosef Bacik /* 85373f73415SJosef Bacik * We have a specific subvol we want to mount, just setup location and 85473f73415SJosef Bacik * go look up the root. 85573f73415SJosef Bacik */ 85673f73415SJosef Bacik if (subvol_objectid) { 85773f73415SJosef Bacik location.objectid = subvol_objectid; 85873f73415SJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 85973f73415SJosef Bacik location.offset = (u64)-1; 86073f73415SJosef Bacik goto find_root; 86173f73415SJosef Bacik } 86273f73415SJosef Bacik 86373f73415SJosef Bacik path = btrfs_alloc_path(); 86473f73415SJosef Bacik if (!path) 86573f73415SJosef Bacik return ERR_PTR(-ENOMEM); 86673f73415SJosef Bacik path->leave_spinning = 1; 86773f73415SJosef Bacik 86873f73415SJosef Bacik /* 86973f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 87073f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 87173f73415SJosef Bacik * to mount. 87273f73415SJosef Bacik */ 873815745cfSAl Viro dir_id = btrfs_super_root_dir(fs_info->super_copy); 87473f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 875b0839166SJulia Lawall if (IS_ERR(di)) { 876b0839166SJulia Lawall btrfs_free_path(path); 877fb4f6f91SDan Carpenter return ERR_CAST(di); 878b0839166SJulia Lawall } 87973f73415SJosef Bacik if (!di) { 88073f73415SJosef Bacik /* 88173f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 88273f73415SJosef Bacik * it's always been there, but don't freak out, just try and 88373f73415SJosef Bacik * mount to root most subvolume. 88473f73415SJosef Bacik */ 88573f73415SJosef Bacik btrfs_free_path(path); 88673f73415SJosef Bacik dir_id = BTRFS_FIRST_FREE_OBJECTID; 887815745cfSAl Viro new_root = fs_info->fs_root; 88873f73415SJosef Bacik goto setup_root; 88973f73415SJosef Bacik } 89073f73415SJosef Bacik 89173f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 89273f73415SJosef Bacik btrfs_free_path(path); 89373f73415SJosef Bacik 89473f73415SJosef Bacik find_root: 895815745cfSAl Viro new_root = btrfs_read_fs_root_no_name(fs_info, &location); 89673f73415SJosef Bacik if (IS_ERR(new_root)) 897d0b678cbSJulia Lawall return ERR_CAST(new_root); 89873f73415SJosef Bacik 899727b9784SJeff Mahoney if (!(sb->s_flags & MS_RDONLY)) { 900727b9784SJeff Mahoney int ret; 901727b9784SJeff Mahoney down_read(&fs_info->cleanup_work_sem); 902727b9784SJeff Mahoney ret = btrfs_orphan_cleanup(new_root); 903727b9784SJeff Mahoney up_read(&fs_info->cleanup_work_sem); 904727b9784SJeff Mahoney if (ret) 905727b9784SJeff Mahoney return ERR_PTR(ret); 906727b9784SJeff Mahoney } 907727b9784SJeff Mahoney 90873f73415SJosef Bacik dir_id = btrfs_root_dirid(&new_root->root_item); 90973f73415SJosef Bacik setup_root: 91073f73415SJosef Bacik location.objectid = dir_id; 91173f73415SJosef Bacik location.type = BTRFS_INODE_ITEM_KEY; 91273f73415SJosef Bacik location.offset = 0; 91373f73415SJosef Bacik 91473f73415SJosef Bacik inode = btrfs_iget(sb, &location, new_root, &new); 9154cbd1149SDan Carpenter if (IS_ERR(inode)) 9164cbd1149SDan Carpenter return ERR_CAST(inode); 91773f73415SJosef Bacik 91873f73415SJosef Bacik /* 91973f73415SJosef Bacik * If we're just mounting the root most subvol put the inode and return 92073f73415SJosef Bacik * a reference to the dentry. We will have already gotten a reference 92173f73415SJosef Bacik * to the inode in btrfs_fill_super so we're good to go. 92273f73415SJosef Bacik */ 9232b0143b5SDavid Howells if (!new && d_inode(sb->s_root) == inode) { 92473f73415SJosef Bacik iput(inode); 92573f73415SJosef Bacik return dget(sb->s_root); 92673f73415SJosef Bacik } 92773f73415SJosef Bacik 9281a0a397eSJ. Bruce Fields return d_obtain_root(inode); 92973f73415SJosef Bacik } 93073f73415SJosef Bacik 9318a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 9328a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 9338a4b83ccSChris Mason void *data, int silent) 9342e635a27SChris Mason { 9352e635a27SChris Mason struct inode *inode; 936815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 9375d4f98a2SYan Zheng struct btrfs_key key; 93839279cc3SChris Mason int err; 9392e635a27SChris Mason 9402e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 9412e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 942e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 943af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 944be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 9455103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 9462e635a27SChris Mason sb->s_time_gran = 1; 9470eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 94833268eafSJosef Bacik sb->s_flags |= MS_POSIXACL; 94949cf6f45SChris Ball #endif 9500c4d2d95SJosef Bacik sb->s_flags |= MS_I_VERSION; 951ad2b2c80SAl Viro err = open_ctree(sb, fs_devices, (char *)data); 952ad2b2c80SAl Viro if (err) { 953efe120a0SFrank Holton printk(KERN_ERR "BTRFS: open_ctree failed\n"); 954ad2b2c80SAl Viro return err; 955e20d96d6SChris Mason } 956b888db2bSChris Mason 9575d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 9585d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 9595d4f98a2SYan Zheng key.offset = 0; 96098c7089cSAl Viro inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL); 9615d4f98a2SYan Zheng if (IS_ERR(inode)) { 9625d4f98a2SYan Zheng err = PTR_ERR(inode); 96339279cc3SChris Mason goto fail_close; 96439279cc3SChris Mason } 9652e635a27SChris Mason 96648fde701SAl Viro sb->s_root = d_make_root(inode); 96748fde701SAl Viro if (!sb->s_root) { 96839279cc3SChris Mason err = -ENOMEM; 96939279cc3SChris Mason goto fail_close; 9702e635a27SChris Mason } 97158176a96SJosef Bacik 9726885f308SChris Mason save_mount_options(sb, data); 97390a887c9SDan Magenheimer cleancache_init_fs(sb); 97459553edfSAl Viro sb->s_flags |= MS_ACTIVE; 9752e635a27SChris Mason return 0; 9762e635a27SChris Mason 97739279cc3SChris Mason fail_close: 978815745cfSAl Viro close_ctree(fs_info->tree_root); 979d5719762SChris Mason return err; 980d5719762SChris Mason } 981d5719762SChris Mason 9826bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 983d5719762SChris Mason { 984d5719762SChris Mason struct btrfs_trans_handle *trans; 985815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 986815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 987df2ce34cSChris Mason 9881abe9b8aSliubo trace_btrfs_sync_fs(wait); 9891abe9b8aSliubo 990d561c025SChris Mason if (!wait) { 991815745cfSAl Viro filemap_flush(fs_info->btree_inode->i_mapping); 992df2ce34cSChris Mason return 0; 993d561c025SChris Mason } 994771ed689SChris Mason 995b0244199SMiao Xie btrfs_wait_ordered_roots(fs_info, -1); 996771ed689SChris Mason 997d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 99860376ce4SJosef Bacik if (IS_ERR(trans)) { 999354aa0fbSMiao Xie /* no transaction, don't bother */ 10006b5fe46dSDavid Sterba if (PTR_ERR(trans) == -ENOENT) { 10016b5fe46dSDavid Sterba /* 10026b5fe46dSDavid Sterba * Exit unless we have some pending changes 10036b5fe46dSDavid Sterba * that need to go through commit 10046b5fe46dSDavid Sterba */ 10056b5fe46dSDavid Sterba if (fs_info->pending_changes == 0) 1006bd7de2c9SJosef Bacik return 0; 1007a53f4f8eSQu Wenruo /* 1008a53f4f8eSQu Wenruo * A non-blocking test if the fs is frozen. We must not 1009a53f4f8eSQu Wenruo * start a new transaction here otherwise a deadlock 1010a53f4f8eSQu Wenruo * happens. The pending operations are delayed to the 1011a53f4f8eSQu Wenruo * next commit after thawing. 1012a53f4f8eSQu Wenruo */ 1013a53f4f8eSQu Wenruo if (__sb_start_write(sb, SB_FREEZE_WRITE, false)) 1014a53f4f8eSQu Wenruo __sb_end_write(sb, SB_FREEZE_WRITE); 1015a53f4f8eSQu Wenruo else 1016a53f4f8eSQu Wenruo return 0; 10176b5fe46dSDavid Sterba trans = btrfs_start_transaction(root, 0); 101860376ce4SJosef Bacik } 101998bd5c54SDavid Sterba if (IS_ERR(trans)) 102098bd5c54SDavid Sterba return PTR_ERR(trans); 10216b5fe46dSDavid Sterba } 1022bd7de2c9SJosef Bacik return btrfs_commit_transaction(trans, root); 1023d5719762SChris Mason } 1024d5719762SChris Mason 102534c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 1026a9572a15SEric Paris { 1027815745cfSAl Viro struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 1028815745cfSAl Viro struct btrfs_root *root = info->tree_root; 1029200da64eSTsutomu Itoh char *compress_type; 1030a9572a15SEric Paris 1031a9572a15SEric Paris if (btrfs_test_opt(root, DEGRADED)) 1032a9572a15SEric Paris seq_puts(seq, ",degraded"); 1033a9572a15SEric Paris if (btrfs_test_opt(root, NODATASUM)) 1034a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 1035a9572a15SEric Paris if (btrfs_test_opt(root, NODATACOW)) 1036a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 1037a9572a15SEric Paris if (btrfs_test_opt(root, NOBARRIER)) 1038a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 103995ac567aSFilipe David Borba Manana if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE) 1040c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",max_inline=%llu", info->max_inline); 1041a9572a15SEric Paris if (info->alloc_start != 0) 1042c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",alloc_start=%llu", info->alloc_start); 1043a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 1044a9572a15SEric Paris num_online_cpus() + 2, 8)) 1045a9572a15SEric Paris seq_printf(seq, ",thread_pool=%d", info->thread_pool_size); 1046200da64eSTsutomu Itoh if (btrfs_test_opt(root, COMPRESS)) { 1047200da64eSTsutomu Itoh if (info->compress_type == BTRFS_COMPRESS_ZLIB) 1048200da64eSTsutomu Itoh compress_type = "zlib"; 1049200da64eSTsutomu Itoh else 1050200da64eSTsutomu Itoh compress_type = "lzo"; 1051200da64eSTsutomu Itoh if (btrfs_test_opt(root, FORCE_COMPRESS)) 1052200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 1053200da64eSTsutomu Itoh else 1054200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 1055200da64eSTsutomu Itoh } 1056c289811cSChris Mason if (btrfs_test_opt(root, NOSSD)) 1057c289811cSChris Mason seq_puts(seq, ",nossd"); 1058451d7585SChris Mason if (btrfs_test_opt(root, SSD_SPREAD)) 1059451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 1060451d7585SChris Mason else if (btrfs_test_opt(root, SSD)) 1061a9572a15SEric Paris seq_puts(seq, ",ssd"); 10623a5e1404SSage Weil if (btrfs_test_opt(root, NOTREELOG)) 10636b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 1064dccae999SSage Weil if (btrfs_test_opt(root, FLUSHONCOMMIT)) 10656b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 106620a5239aSMatthew Wilcox if (btrfs_test_opt(root, DISCARD)) 106720a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 1068a9572a15SEric Paris if (!(root->fs_info->sb->s_flags & MS_POSIXACL)) 1069a9572a15SEric Paris seq_puts(seq, ",noacl"); 1070200da64eSTsutomu Itoh if (btrfs_test_opt(root, SPACE_CACHE)) 1071200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 107273bc1876SJosef Bacik else 10738965593eSDavid Sterba seq_puts(seq, ",nospace_cache"); 1074f420ee1eSStefan Behrens if (btrfs_test_opt(root, RESCAN_UUID_TREE)) 1075f420ee1eSStefan Behrens seq_puts(seq, ",rescan_uuid_tree"); 1076200da64eSTsutomu Itoh if (btrfs_test_opt(root, CLEAR_CACHE)) 1077200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 1078200da64eSTsutomu Itoh if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 1079200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 10800942caa3SDavid Sterba if (btrfs_test_opt(root, ENOSPC_DEBUG)) 10810942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 10820942caa3SDavid Sterba if (btrfs_test_opt(root, AUTO_DEFRAG)) 10830942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 10840942caa3SDavid Sterba if (btrfs_test_opt(root, INODE_MAP_CACHE)) 10850942caa3SDavid Sterba seq_puts(seq, ",inode_cache"); 10869555c6c1SIlya Dryomov if (btrfs_test_opt(root, SKIP_BALANCE)) 10879555c6c1SIlya Dryomov seq_puts(seq, ",skip_balance"); 10888507d216SWang Shilong if (btrfs_test_opt(root, RECOVERY)) 10898507d216SWang Shilong seq_puts(seq, ",recovery"); 10908507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 10918507d216SWang Shilong if (btrfs_test_opt(root, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA)) 10928507d216SWang Shilong seq_puts(seq, ",check_int_data"); 10938507d216SWang Shilong else if (btrfs_test_opt(root, CHECK_INTEGRITY)) 10948507d216SWang Shilong seq_puts(seq, ",check_int"); 10958507d216SWang Shilong if (info->check_integrity_print_mask) 10968507d216SWang Shilong seq_printf(seq, ",check_int_print_mask=%d", 10978507d216SWang Shilong info->check_integrity_print_mask); 10988507d216SWang Shilong #endif 10998507d216SWang Shilong if (info->metadata_ratio) 11008507d216SWang Shilong seq_printf(seq, ",metadata_ratio=%d", 11018507d216SWang Shilong info->metadata_ratio); 11028c342930SJeff Mahoney if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR)) 11038c342930SJeff Mahoney seq_puts(seq, ",fatal_errors=panic"); 11048b87dc17SDavid Sterba if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL) 11058b87dc17SDavid Sterba seq_printf(seq, ",commit=%d", info->commit_interval); 1106a9572a15SEric Paris return 0; 1107a9572a15SEric Paris } 1108a9572a15SEric Paris 1109a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 11102e635a27SChris Mason { 1111815745cfSAl Viro struct btrfs_fs_info *p = data; 1112815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(s); 11134b82d6e4SYan 1114815745cfSAl Viro return fs_info->fs_devices == p->fs_devices; 11154b82d6e4SYan } 11164b82d6e4SYan 1117450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 1118450ba0eaSJosef Bacik { 11196de1d09dSAl Viro int err = set_anon_super(s, data); 11206de1d09dSAl Viro if (!err) 1121450ba0eaSJosef Bacik s->s_fs_info = data; 11226de1d09dSAl Viro return err; 1123450ba0eaSJosef Bacik } 1124450ba0eaSJosef Bacik 1125830c4adbSJosef Bacik /* 1126f9d9ef62SDavid Sterba * subvolumes are identified by ino 256 1127f9d9ef62SDavid Sterba */ 1128f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode) 1129f9d9ef62SDavid Sterba { 1130f9d9ef62SDavid Sterba if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 1131f9d9ef62SDavid Sterba return 1; 1132f9d9ef62SDavid Sterba return 0; 1133f9d9ef62SDavid Sterba } 1134f9d9ef62SDavid Sterba 1135f9d9ef62SDavid Sterba /* 1136*e6e4dbe8SOmar Sandoval * This will add subvolid=0 to the argument string while removing any subvol= 1137*e6e4dbe8SOmar Sandoval * and subvolid= arguments to make sure we get the top-level root for path 1138*e6e4dbe8SOmar Sandoval * walking to the subvol we want. 1139830c4adbSJosef Bacik */ 1140830c4adbSJosef Bacik static char *setup_root_args(char *args) 1141830c4adbSJosef Bacik { 1142*e6e4dbe8SOmar Sandoval char *buf, *dst, *sep; 1143830c4adbSJosef Bacik 1144*e6e4dbe8SOmar Sandoval if (!args) 1145*e6e4dbe8SOmar Sandoval return kstrdup("subvolid=0", GFP_NOFS); 1146830c4adbSJosef Bacik 1147*e6e4dbe8SOmar Sandoval /* The worst case is that we add ",subvolid=0" to the end. */ 1148*e6e4dbe8SOmar Sandoval buf = dst = kmalloc(strlen(args) + strlen(",subvolid=0") + 1, GFP_NOFS); 1149f60d16a8SJim Meyering if (!buf) 1150f60d16a8SJim Meyering return NULL; 1151830c4adbSJosef Bacik 1152*e6e4dbe8SOmar Sandoval while (1) { 1153*e6e4dbe8SOmar Sandoval sep = strchrnul(args, ','); 1154*e6e4dbe8SOmar Sandoval if (!strstarts(args, "subvol=") && 1155*e6e4dbe8SOmar Sandoval !strstarts(args, "subvolid=")) { 1156*e6e4dbe8SOmar Sandoval memcpy(dst, args, sep - args); 1157*e6e4dbe8SOmar Sandoval dst += sep - args; 1158*e6e4dbe8SOmar Sandoval *dst++ = ','; 1159830c4adbSJosef Bacik } 1160*e6e4dbe8SOmar Sandoval if (*sep) 1161*e6e4dbe8SOmar Sandoval args = sep + 1; 1162*e6e4dbe8SOmar Sandoval else 1163*e6e4dbe8SOmar Sandoval break; 1164*e6e4dbe8SOmar Sandoval } 1165f60d16a8SJim Meyering strcpy(dst, "subvolid=0"); 1166830c4adbSJosef Bacik 1167f60d16a8SJim Meyering return buf; 1168830c4adbSJosef Bacik } 1169830c4adbSJosef Bacik 1170830c4adbSJosef Bacik static struct dentry *mount_subvol(const char *subvol_name, int flags, 1171830c4adbSJosef Bacik const char *device_name, char *data) 1172830c4adbSJosef Bacik { 1173830c4adbSJosef Bacik struct dentry *root; 1174830c4adbSJosef Bacik struct vfsmount *mnt; 1175830c4adbSJosef Bacik char *newargs; 1176830c4adbSJosef Bacik 1177830c4adbSJosef Bacik newargs = setup_root_args(data); 1178830c4adbSJosef Bacik if (!newargs) 1179830c4adbSJosef Bacik return ERR_PTR(-ENOMEM); 1180830c4adbSJosef Bacik mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, 1181830c4adbSJosef Bacik newargs); 11820723a047SHarald Hoyer 11830723a047SHarald Hoyer if (PTR_RET(mnt) == -EBUSY) { 11840723a047SHarald Hoyer if (flags & MS_RDONLY) { 11850723a047SHarald Hoyer mnt = vfs_kern_mount(&btrfs_fs_type, flags & ~MS_RDONLY, device_name, 11860723a047SHarald Hoyer newargs); 11870723a047SHarald Hoyer } else { 11880723a047SHarald Hoyer int r; 11890723a047SHarald Hoyer mnt = vfs_kern_mount(&btrfs_fs_type, flags | MS_RDONLY, device_name, 11900723a047SHarald Hoyer newargs); 11910040e606SChristoph Jaeger if (IS_ERR(mnt)) { 11920040e606SChristoph Jaeger kfree(newargs); 11930723a047SHarald Hoyer return ERR_CAST(mnt); 11940040e606SChristoph Jaeger } 11950723a047SHarald Hoyer 1196773cd04eSOmar Sandoval down_write(&mnt->mnt_sb->s_umount); 11970723a047SHarald Hoyer r = btrfs_remount(mnt->mnt_sb, &flags, NULL); 1198773cd04eSOmar Sandoval up_write(&mnt->mnt_sb->s_umount); 11990723a047SHarald Hoyer if (r < 0) { 12000723a047SHarald Hoyer /* FIXME: release vfsmount mnt ??*/ 12010040e606SChristoph Jaeger kfree(newargs); 12020723a047SHarald Hoyer return ERR_PTR(r); 12030723a047SHarald Hoyer } 12040723a047SHarald Hoyer } 12050723a047SHarald Hoyer } 12060723a047SHarald Hoyer 12070040e606SChristoph Jaeger kfree(newargs); 12080040e606SChristoph Jaeger 1209830c4adbSJosef Bacik if (IS_ERR(mnt)) 1210830c4adbSJosef Bacik return ERR_CAST(mnt); 1211830c4adbSJosef Bacik 1212ea441d11SAl Viro root = mount_subtree(mnt, subvol_name); 1213830c4adbSJosef Bacik 12142b0143b5SDavid Howells if (!IS_ERR(root) && !is_subvolume_inode(d_inode(root))) { 1215ea441d11SAl Viro struct super_block *s = root->d_sb; 1216ea441d11SAl Viro dput(root); 1217ea441d11SAl Viro root = ERR_PTR(-EINVAL); 1218ea441d11SAl Viro deactivate_locked_super(s); 1219efe120a0SFrank Holton printk(KERN_ERR "BTRFS: '%s' is not a valid subvolume\n", 1220f9d9ef62SDavid Sterba subvol_name); 1221f9d9ef62SDavid Sterba } 1222f9d9ef62SDavid Sterba 1223830c4adbSJosef Bacik return root; 1224830c4adbSJosef Bacik } 1225450ba0eaSJosef Bacik 1226f667aef6SQu Wenruo static int parse_security_options(char *orig_opts, 1227f667aef6SQu Wenruo struct security_mnt_opts *sec_opts) 1228f667aef6SQu Wenruo { 1229f667aef6SQu Wenruo char *secdata = NULL; 1230f667aef6SQu Wenruo int ret = 0; 1231f667aef6SQu Wenruo 1232f667aef6SQu Wenruo secdata = alloc_secdata(); 1233f667aef6SQu Wenruo if (!secdata) 1234f667aef6SQu Wenruo return -ENOMEM; 1235f667aef6SQu Wenruo ret = security_sb_copy_data(orig_opts, secdata); 1236f667aef6SQu Wenruo if (ret) { 1237f667aef6SQu Wenruo free_secdata(secdata); 1238f667aef6SQu Wenruo return ret; 1239f667aef6SQu Wenruo } 1240f667aef6SQu Wenruo ret = security_sb_parse_opts_str(secdata, sec_opts); 1241f667aef6SQu Wenruo free_secdata(secdata); 1242f667aef6SQu Wenruo return ret; 1243f667aef6SQu Wenruo } 1244f667aef6SQu Wenruo 1245f667aef6SQu Wenruo static int setup_security_options(struct btrfs_fs_info *fs_info, 1246f667aef6SQu Wenruo struct super_block *sb, 1247f667aef6SQu Wenruo struct security_mnt_opts *sec_opts) 1248f667aef6SQu Wenruo { 1249f667aef6SQu Wenruo int ret = 0; 1250f667aef6SQu Wenruo 1251f667aef6SQu Wenruo /* 1252f667aef6SQu Wenruo * Call security_sb_set_mnt_opts() to check whether new sec_opts 1253f667aef6SQu Wenruo * is valid. 1254f667aef6SQu Wenruo */ 1255f667aef6SQu Wenruo ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL); 1256f667aef6SQu Wenruo if (ret) 1257f667aef6SQu Wenruo return ret; 1258f667aef6SQu Wenruo 1259a43bb39bSQu Wenruo #ifdef CONFIG_SECURITY 1260f667aef6SQu Wenruo if (!fs_info->security_opts.num_mnt_opts) { 1261f667aef6SQu Wenruo /* first time security setup, copy sec_opts to fs_info */ 1262f667aef6SQu Wenruo memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts)); 1263f667aef6SQu Wenruo } else { 1264f667aef6SQu Wenruo /* 1265f667aef6SQu Wenruo * Since SELinux(the only one supports security_mnt_opts) does 1266f667aef6SQu Wenruo * NOT support changing context during remount/mount same sb, 1267f667aef6SQu Wenruo * This must be the same or part of the same security options, 1268f667aef6SQu Wenruo * just free it. 1269f667aef6SQu Wenruo */ 1270f667aef6SQu Wenruo security_free_mnt_opts(sec_opts); 1271f667aef6SQu Wenruo } 1272a43bb39bSQu Wenruo #endif 1273f667aef6SQu Wenruo return ret; 1274f667aef6SQu Wenruo } 1275f667aef6SQu Wenruo 1276edf24abeSChristoph Hellwig /* 1277edf24abeSChristoph Hellwig * Find a superblock for the given device / mount point. 1278edf24abeSChristoph Hellwig * 1279edf24abeSChristoph Hellwig * Note: This is based on get_sb_bdev from fs/super.c with a few additions 1280edf24abeSChristoph Hellwig * for multiple device setup. Make sure to keep it in sync. 1281edf24abeSChristoph Hellwig */ 1282061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1283306e16ceSDavid Sterba const char *device_name, void *data) 12844b82d6e4SYan { 12854b82d6e4SYan struct block_device *bdev = NULL; 12864b82d6e4SYan struct super_block *s; 12874b82d6e4SYan struct dentry *root; 12888a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices = NULL; 1289450ba0eaSJosef Bacik struct btrfs_fs_info *fs_info = NULL; 1290f667aef6SQu Wenruo struct security_mnt_opts new_sec_opts; 129197288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 129273f73415SJosef Bacik char *subvol_name = NULL; 129373f73415SJosef Bacik u64 subvol_objectid = 0; 12944b82d6e4SYan int error = 0; 12954b82d6e4SYan 129697288f2cSChristoph Hellwig if (!(flags & MS_RDONLY)) 129797288f2cSChristoph Hellwig mode |= FMODE_WRITE; 129897288f2cSChristoph Hellwig 129997288f2cSChristoph Hellwig error = btrfs_parse_early_options(data, mode, fs_type, 130073f73415SJosef Bacik &subvol_name, &subvol_objectid, 13015e2a4b25SDavid Sterba &fs_devices); 1302f23c8af8SIlya Dryomov if (error) { 1303f23c8af8SIlya Dryomov kfree(subvol_name); 1304061dbc6bSAl Viro return ERR_PTR(error); 1305f23c8af8SIlya Dryomov } 1306edf24abeSChristoph Hellwig 1307830c4adbSJosef Bacik if (subvol_name) { 1308830c4adbSJosef Bacik root = mount_subvol(subvol_name, flags, device_name, data); 1309830c4adbSJosef Bacik kfree(subvol_name); 1310830c4adbSJosef Bacik return root; 1311830c4adbSJosef Bacik } 1312830c4adbSJosef Bacik 1313f667aef6SQu Wenruo security_init_mnt_opts(&new_sec_opts); 1314f667aef6SQu Wenruo if (data) { 1315f667aef6SQu Wenruo error = parse_security_options(data, &new_sec_opts); 13168a4b83ccSChris Mason if (error) 1317830c4adbSJosef Bacik return ERR_PTR(error); 1318f667aef6SQu Wenruo } 1319f667aef6SQu Wenruo 1320f667aef6SQu Wenruo error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices); 1321f667aef6SQu Wenruo if (error) 1322f667aef6SQu Wenruo goto error_sec_opts; 13234b82d6e4SYan 1324450ba0eaSJosef Bacik /* 1325450ba0eaSJosef Bacik * Setup a dummy root and fs_info for test/set super. This is because 1326450ba0eaSJosef Bacik * we don't actually fill this stuff out until open_ctree, but we need 1327450ba0eaSJosef Bacik * it for searching for existing supers, so this lets us do that and 1328450ba0eaSJosef Bacik * then open_ctree will properly initialize everything later. 1329450ba0eaSJosef Bacik */ 1330450ba0eaSJosef Bacik fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS); 1331f667aef6SQu Wenruo if (!fs_info) { 1332f667aef6SQu Wenruo error = -ENOMEM; 1333f667aef6SQu Wenruo goto error_sec_opts; 1334f667aef6SQu Wenruo } 133504d21a24SIlya Dryomov 1336450ba0eaSJosef Bacik fs_info->fs_devices = fs_devices; 1337450ba0eaSJosef Bacik 13386c41761fSDavid Sterba fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 13396c41761fSDavid Sterba fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 1340f667aef6SQu Wenruo security_init_mnt_opts(&fs_info->security_opts); 13416c41761fSDavid Sterba if (!fs_info->super_copy || !fs_info->super_for_commit) { 13426c41761fSDavid Sterba error = -ENOMEM; 134304d21a24SIlya Dryomov goto error_fs_info; 134404d21a24SIlya Dryomov } 134504d21a24SIlya Dryomov 134604d21a24SIlya Dryomov error = btrfs_open_devices(fs_devices, mode, fs_type); 134704d21a24SIlya Dryomov if (error) 134804d21a24SIlya Dryomov goto error_fs_info; 134904d21a24SIlya Dryomov 135004d21a24SIlya Dryomov if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) { 135104d21a24SIlya Dryomov error = -EACCES; 13526c41761fSDavid Sterba goto error_close_devices; 13536c41761fSDavid Sterba } 13546c41761fSDavid Sterba 1355dfe25020SChris Mason bdev = fs_devices->latest_bdev; 13569249e17fSDavid Howells s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC, 13579249e17fSDavid Howells fs_info); 1358830c4adbSJosef Bacik if (IS_ERR(s)) { 1359830c4adbSJosef Bacik error = PTR_ERR(s); 1360830c4adbSJosef Bacik goto error_close_devices; 1361830c4adbSJosef Bacik } 13624b82d6e4SYan 13634b82d6e4SYan if (s->s_root) { 13642b82032cSYan Zheng btrfs_close_devices(fs_devices); 13656c41761fSDavid Sterba free_fs_info(fs_info); 136659553edfSAl Viro if ((flags ^ s->s_flags) & MS_RDONLY) 136759553edfSAl Viro error = -EBUSY; 13684b82d6e4SYan } else { 13694b82d6e4SYan char b[BDEVNAME_SIZE]; 13704b82d6e4SYan 13714b82d6e4SYan strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); 1372815745cfSAl Viro btrfs_sb(s)->bdev_holder = fs_type; 13738a4b83ccSChris Mason error = btrfs_fill_super(s, fs_devices, data, 13748a4b83ccSChris Mason flags & MS_SILENT ? 1 : 0); 13754b82d6e4SYan } 13764b82d6e4SYan 137759553edfSAl Viro root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error); 1378f667aef6SQu Wenruo if (IS_ERR(root)) { 1379e15d0542SXin Zhong deactivate_locked_super(s); 1380f667aef6SQu Wenruo error = PTR_ERR(root); 1381f667aef6SQu Wenruo goto error_sec_opts; 1382f667aef6SQu Wenruo } 1383f667aef6SQu Wenruo 1384f667aef6SQu Wenruo fs_info = btrfs_sb(s); 1385f667aef6SQu Wenruo error = setup_security_options(fs_info, s, &new_sec_opts); 1386f667aef6SQu Wenruo if (error) { 1387f667aef6SQu Wenruo dput(root); 1388f667aef6SQu Wenruo deactivate_locked_super(s); 1389f667aef6SQu Wenruo goto error_sec_opts; 1390f667aef6SQu Wenruo } 13914b82d6e4SYan 1392061dbc6bSAl Viro return root; 13934b82d6e4SYan 1394c146afadSYan Zheng error_close_devices: 13958a4b83ccSChris Mason btrfs_close_devices(fs_devices); 139604d21a24SIlya Dryomov error_fs_info: 13976c41761fSDavid Sterba free_fs_info(fs_info); 1398f667aef6SQu Wenruo error_sec_opts: 1399f667aef6SQu Wenruo security_free_mnt_opts(&new_sec_opts); 1400061dbc6bSAl Viro return ERR_PTR(error); 14014b82d6e4SYan } 14022e635a27SChris Mason 14030d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 14040d2450abSSergei Trofimovich int new_pool_size, int old_pool_size) 14050d2450abSSergei Trofimovich { 14060d2450abSSergei Trofimovich if (new_pool_size == old_pool_size) 14070d2450abSSergei Trofimovich return; 14080d2450abSSergei Trofimovich 14090d2450abSSergei Trofimovich fs_info->thread_pool_size = new_pool_size; 14100d2450abSSergei Trofimovich 1411efe120a0SFrank Holton btrfs_info(fs_info, "resize thread pool %d -> %d", 14120d2450abSSergei Trofimovich old_pool_size, new_pool_size); 14130d2450abSSergei Trofimovich 14145cdc7ad3SQu Wenruo btrfs_workqueue_set_max(fs_info->workers, new_pool_size); 1415afe3d242SQu Wenruo btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size); 1416a8c93d4eSQu Wenruo btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size); 1417e66f0bb1SQu Wenruo btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size); 1418fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size); 1419fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size); 1420fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_write_workers, 1421fccb5d86SQu Wenruo new_pool_size); 1422fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size); 1423fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size); 14245b3bc44eSQu Wenruo btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size); 1425736cfa15SQu Wenruo btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size); 14260339ef2fSQu Wenruo btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers, 1427ff023aacSStefan Behrens new_pool_size); 14280d2450abSSergei Trofimovich } 14290d2450abSSergei Trofimovich 1430f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info) 1431dc81cdc5SMiao Xie { 1432dc81cdc5SMiao Xie set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1433f42a34b2SMiao Xie } 1434dc81cdc5SMiao Xie 1435f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info, 1436f42a34b2SMiao Xie unsigned long old_opts, int flags) 1437f42a34b2SMiao Xie { 1438dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1439dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1440dc81cdc5SMiao Xie (flags & MS_RDONLY))) { 1441dc81cdc5SMiao Xie /* wait for any defraggers to finish */ 1442dc81cdc5SMiao Xie wait_event(fs_info->transaction_wait, 1443dc81cdc5SMiao Xie (atomic_read(&fs_info->defrag_running) == 0)); 1444dc81cdc5SMiao Xie if (flags & MS_RDONLY) 1445dc81cdc5SMiao Xie sync_filesystem(fs_info->sb); 1446dc81cdc5SMiao Xie } 1447dc81cdc5SMiao Xie } 1448dc81cdc5SMiao Xie 1449dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info, 1450dc81cdc5SMiao Xie unsigned long old_opts) 1451dc81cdc5SMiao Xie { 1452dc81cdc5SMiao Xie /* 1453dc81cdc5SMiao Xie * We need cleanup all defragable inodes if the autodefragment is 1454dc81cdc5SMiao Xie * close or the fs is R/O. 1455dc81cdc5SMiao Xie */ 1456dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1457dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1458dc81cdc5SMiao Xie (fs_info->sb->s_flags & MS_RDONLY))) { 1459dc81cdc5SMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 1460dc81cdc5SMiao Xie } 1461dc81cdc5SMiao Xie 1462dc81cdc5SMiao Xie clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1463dc81cdc5SMiao Xie } 1464dc81cdc5SMiao Xie 1465c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1466c146afadSYan Zheng { 1467815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1468815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 146949b25e05SJeff Mahoney unsigned old_flags = sb->s_flags; 147049b25e05SJeff Mahoney unsigned long old_opts = fs_info->mount_opt; 147149b25e05SJeff Mahoney unsigned long old_compress_type = fs_info->compress_type; 147249b25e05SJeff Mahoney u64 old_max_inline = fs_info->max_inline; 147349b25e05SJeff Mahoney u64 old_alloc_start = fs_info->alloc_start; 147449b25e05SJeff Mahoney int old_thread_pool_size = fs_info->thread_pool_size; 147549b25e05SJeff Mahoney unsigned int old_metadata_ratio = fs_info->metadata_ratio; 1476c146afadSYan Zheng int ret; 1477c146afadSYan Zheng 147802b9984dSTheodore Ts'o sync_filesystem(sb); 1479f42a34b2SMiao Xie btrfs_remount_prepare(fs_info); 1480dc81cdc5SMiao Xie 1481f667aef6SQu Wenruo if (data) { 1482f667aef6SQu Wenruo struct security_mnt_opts new_sec_opts; 1483f667aef6SQu Wenruo 1484f667aef6SQu Wenruo security_init_mnt_opts(&new_sec_opts); 1485f667aef6SQu Wenruo ret = parse_security_options(data, &new_sec_opts); 1486f667aef6SQu Wenruo if (ret) 1487f667aef6SQu Wenruo goto restore; 1488f667aef6SQu Wenruo ret = setup_security_options(fs_info, sb, 1489f667aef6SQu Wenruo &new_sec_opts); 1490f667aef6SQu Wenruo if (ret) { 1491f667aef6SQu Wenruo security_free_mnt_opts(&new_sec_opts); 1492f667aef6SQu Wenruo goto restore; 1493f667aef6SQu Wenruo } 1494f667aef6SQu Wenruo } 1495f667aef6SQu Wenruo 1496b288052eSChris Mason ret = btrfs_parse_options(root, data); 149749b25e05SJeff Mahoney if (ret) { 149849b25e05SJeff Mahoney ret = -EINVAL; 149949b25e05SJeff Mahoney goto restore; 150049b25e05SJeff Mahoney } 1501b288052eSChris Mason 1502f42a34b2SMiao Xie btrfs_remount_begin(fs_info, old_opts, *flags); 15030d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 15040d2450abSSergei Trofimovich fs_info->thread_pool_size, old_thread_pool_size); 15050d2450abSSergei Trofimovich 1506c146afadSYan Zheng if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 1507dc81cdc5SMiao Xie goto out; 1508c146afadSYan Zheng 1509c146afadSYan Zheng if (*flags & MS_RDONLY) { 15108dabb742SStefan Behrens /* 15118dabb742SStefan Behrens * this also happens on 'umount -rf' or on shutdown, when 15128dabb742SStefan Behrens * the filesystem is busy. 15138dabb742SStefan Behrens */ 151421c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 1515361c093dSStefan Behrens 1516361c093dSStefan Behrens /* wait for the uuid_scan task to finish */ 1517361c093dSStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 1518361c093dSStefan Behrens /* avoid complains from lockdep et al. */ 1519361c093dSStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 1520361c093dSStefan Behrens 1521c146afadSYan Zheng sb->s_flags |= MS_RDONLY; 1522c146afadSYan Zheng 15238dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 15248dabb742SStefan Behrens btrfs_scrub_cancel(fs_info); 1525061594efSMiao Xie btrfs_pause_balance(fs_info); 15268dabb742SStefan Behrens 1527c146afadSYan Zheng ret = btrfs_commit_super(root); 152849b25e05SJeff Mahoney if (ret) 152949b25e05SJeff Mahoney goto restore; 1530c146afadSYan Zheng } else { 15316ef3de9cSDavid Sterba if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 15326ef3de9cSDavid Sterba btrfs_err(fs_info, 1533efe120a0SFrank Holton "Remounting read-write after error is not allowed"); 15346ef3de9cSDavid Sterba ret = -EINVAL; 15356ef3de9cSDavid Sterba goto restore; 15366ef3de9cSDavid Sterba } 15378a3db184SSergei Trofimovich if (fs_info->fs_devices->rw_devices == 0) { 153849b25e05SJeff Mahoney ret = -EACCES; 153949b25e05SJeff Mahoney goto restore; 15408a3db184SSergei Trofimovich } 15412b82032cSYan Zheng 1542292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 1543292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 1544292fd7fcSStefan Behrens !(*flags & MS_RDONLY)) { 1545efe120a0SFrank Holton btrfs_warn(fs_info, 1546efe120a0SFrank Holton "too many missing devices, writeable remount is not allowed"); 1547292fd7fcSStefan Behrens ret = -EACCES; 1548292fd7fcSStefan Behrens goto restore; 1549292fd7fcSStefan Behrens } 1550292fd7fcSStefan Behrens 15518a3db184SSergei Trofimovich if (btrfs_super_log_root(fs_info->super_copy) != 0) { 155249b25e05SJeff Mahoney ret = -EINVAL; 155349b25e05SJeff Mahoney goto restore; 15548a3db184SSergei Trofimovich } 1555c146afadSYan Zheng 1556815745cfSAl Viro ret = btrfs_cleanup_fs_roots(fs_info); 155749b25e05SJeff Mahoney if (ret) 155849b25e05SJeff Mahoney goto restore; 1559c146afadSYan Zheng 1560d68fc57bSYan, Zheng /* recover relocation */ 15615f316481SWang Shilong mutex_lock(&fs_info->cleaner_mutex); 1562d68fc57bSYan, Zheng ret = btrfs_recover_relocation(root); 15635f316481SWang Shilong mutex_unlock(&fs_info->cleaner_mutex); 156449b25e05SJeff Mahoney if (ret) 156549b25e05SJeff Mahoney goto restore; 1566c146afadSYan Zheng 15672b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 15682b6ba629SIlya Dryomov if (ret) 15692b6ba629SIlya Dryomov goto restore; 15702b6ba629SIlya Dryomov 15718dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 15728dabb742SStefan Behrens if (ret) { 1573efe120a0SFrank Holton btrfs_warn(fs_info, "failed to resume dev_replace"); 15748dabb742SStefan Behrens goto restore; 15758dabb742SStefan Behrens } 157694aebfb2SJosef Bacik 157794aebfb2SJosef Bacik if (!fs_info->uuid_root) { 1578efe120a0SFrank Holton btrfs_info(fs_info, "creating UUID tree"); 157994aebfb2SJosef Bacik ret = btrfs_create_uuid_tree(fs_info); 158094aebfb2SJosef Bacik if (ret) { 1581efe120a0SFrank Holton btrfs_warn(fs_info, "failed to create the UUID tree %d", ret); 158294aebfb2SJosef Bacik goto restore; 158394aebfb2SJosef Bacik } 158494aebfb2SJosef Bacik } 1585c146afadSYan Zheng sb->s_flags &= ~MS_RDONLY; 1586c146afadSYan Zheng } 1587dc81cdc5SMiao Xie out: 15882c6a92b0SJustin Maggard wake_up_process(fs_info->transaction_kthread); 1589dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 1590c146afadSYan Zheng return 0; 159149b25e05SJeff Mahoney 159249b25e05SJeff Mahoney restore: 159349b25e05SJeff Mahoney /* We've hit an error - don't reset MS_RDONLY */ 159449b25e05SJeff Mahoney if (sb->s_flags & MS_RDONLY) 159549b25e05SJeff Mahoney old_flags |= MS_RDONLY; 159649b25e05SJeff Mahoney sb->s_flags = old_flags; 159749b25e05SJeff Mahoney fs_info->mount_opt = old_opts; 159849b25e05SJeff Mahoney fs_info->compress_type = old_compress_type; 159949b25e05SJeff Mahoney fs_info->max_inline = old_max_inline; 1600c018daecSMiao Xie mutex_lock(&fs_info->chunk_mutex); 160149b25e05SJeff Mahoney fs_info->alloc_start = old_alloc_start; 1602c018daecSMiao Xie mutex_unlock(&fs_info->chunk_mutex); 16030d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 16040d2450abSSergei Trofimovich old_thread_pool_size, fs_info->thread_pool_size); 160549b25e05SJeff Mahoney fs_info->metadata_ratio = old_metadata_ratio; 1606dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 160749b25e05SJeff Mahoney return ret; 1608c146afadSYan Zheng } 1609c146afadSYan Zheng 1610bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 1611bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1, 1612bcd53741SArne Jansen const void *dev_info2) 1613bcd53741SArne Jansen { 1614bcd53741SArne Jansen if (((struct btrfs_device_info *)dev_info1)->max_avail > 1615bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1616bcd53741SArne Jansen return -1; 1617bcd53741SArne Jansen else if (((struct btrfs_device_info *)dev_info1)->max_avail < 1618bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1619bcd53741SArne Jansen return 1; 1620bcd53741SArne Jansen else 1621bcd53741SArne Jansen return 0; 1622bcd53741SArne Jansen } 1623bcd53741SArne Jansen 1624bcd53741SArne Jansen /* 1625bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 1626bcd53741SArne Jansen * is stored.(Descending Sort) 1627bcd53741SArne Jansen */ 1628bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 1629bcd53741SArne Jansen struct btrfs_device_info *devices, 1630bcd53741SArne Jansen size_t nr_devices) 1631bcd53741SArne Jansen { 1632bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 1633bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 1634bcd53741SArne Jansen } 1635bcd53741SArne Jansen 16366d07bcecSMiao Xie /* 16376d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 16386d07bcecSMiao Xie * file data. 16396d07bcecSMiao Xie */ 16406d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes) 16416d07bcecSMiao Xie { 16426d07bcecSMiao Xie struct btrfs_fs_info *fs_info = root->fs_info; 16436d07bcecSMiao Xie struct btrfs_device_info *devices_info; 16446d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 16456d07bcecSMiao Xie struct btrfs_device *device; 16466d07bcecSMiao Xie u64 skip_space; 16476d07bcecSMiao Xie u64 type; 16486d07bcecSMiao Xie u64 avail_space; 16496d07bcecSMiao Xie u64 used_space; 16506d07bcecSMiao Xie u64 min_stripe_size; 165139fb26c3SMiao Xie int min_stripes = 1, num_stripes = 1; 16526d07bcecSMiao Xie int i = 0, nr_devices; 16536d07bcecSMiao Xie int ret; 16546d07bcecSMiao Xie 16557e33fd99SJosef Bacik /* 16567e33fd99SJosef Bacik * We aren't under the device list lock, so this is racey-ish, but good 16577e33fd99SJosef Bacik * enough for our purposes. 16587e33fd99SJosef Bacik */ 1659b772a86eSLi Zefan nr_devices = fs_info->fs_devices->open_devices; 16607e33fd99SJosef Bacik if (!nr_devices) { 16617e33fd99SJosef Bacik smp_mb(); 16627e33fd99SJosef Bacik nr_devices = fs_info->fs_devices->open_devices; 16637e33fd99SJosef Bacik ASSERT(nr_devices); 16647e33fd99SJosef Bacik if (!nr_devices) { 16657e33fd99SJosef Bacik *free_bytes = 0; 16667e33fd99SJosef Bacik return 0; 16677e33fd99SJosef Bacik } 16687e33fd99SJosef Bacik } 16696d07bcecSMiao Xie 1670d9b0d9baSDulshani Gunawardhana devices_info = kmalloc_array(nr_devices, sizeof(*devices_info), 16716d07bcecSMiao Xie GFP_NOFS); 16726d07bcecSMiao Xie if (!devices_info) 16736d07bcecSMiao Xie return -ENOMEM; 16746d07bcecSMiao Xie 16756d07bcecSMiao Xie /* calc min stripe number for data space alloction */ 16766d07bcecSMiao Xie type = btrfs_get_alloc_profile(root, 1); 167739fb26c3SMiao Xie if (type & BTRFS_BLOCK_GROUP_RAID0) { 16786d07bcecSMiao Xie min_stripes = 2; 167939fb26c3SMiao Xie num_stripes = nr_devices; 168039fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID1) { 16816d07bcecSMiao Xie min_stripes = 2; 168239fb26c3SMiao Xie num_stripes = 2; 168339fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID10) { 16846d07bcecSMiao Xie min_stripes = 4; 168539fb26c3SMiao Xie num_stripes = 4; 168639fb26c3SMiao Xie } 16876d07bcecSMiao Xie 16886d07bcecSMiao Xie if (type & BTRFS_BLOCK_GROUP_DUP) 16896d07bcecSMiao Xie min_stripe_size = 2 * BTRFS_STRIPE_LEN; 16906d07bcecSMiao Xie else 16916d07bcecSMiao Xie min_stripe_size = BTRFS_STRIPE_LEN; 16926d07bcecSMiao Xie 16937e33fd99SJosef Bacik if (fs_info->alloc_start) 16947e33fd99SJosef Bacik mutex_lock(&fs_devices->device_list_mutex); 16957e33fd99SJosef Bacik rcu_read_lock(); 16967e33fd99SJosef Bacik list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 169763a212abSStefan Behrens if (!device->in_fs_metadata || !device->bdev || 169863a212abSStefan Behrens device->is_tgtdev_for_dev_replace) 16996d07bcecSMiao Xie continue; 17006d07bcecSMiao Xie 17017e33fd99SJosef Bacik if (i >= nr_devices) 17027e33fd99SJosef Bacik break; 17037e33fd99SJosef Bacik 17046d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 17056d07bcecSMiao Xie 17066d07bcecSMiao Xie /* align with stripe_len */ 1707f8c269d7SDavid Sterba avail_space = div_u64(avail_space, BTRFS_STRIPE_LEN); 17086d07bcecSMiao Xie avail_space *= BTRFS_STRIPE_LEN; 17096d07bcecSMiao Xie 17106d07bcecSMiao Xie /* 17116d07bcecSMiao Xie * In order to avoid overwritting the superblock on the drive, 17126d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 17136d07bcecSMiao Xie * allocation. 17146d07bcecSMiao Xie */ 17156d07bcecSMiao Xie skip_space = 1024 * 1024; 17166d07bcecSMiao Xie 17176d07bcecSMiao Xie /* user can set the offset in fs_info->alloc_start. */ 17187e33fd99SJosef Bacik if (fs_info->alloc_start && 17197e33fd99SJosef Bacik fs_info->alloc_start + BTRFS_STRIPE_LEN <= 17207e33fd99SJosef Bacik device->total_bytes) { 17217e33fd99SJosef Bacik rcu_read_unlock(); 17226d07bcecSMiao Xie skip_space = max(fs_info->alloc_start, skip_space); 17236d07bcecSMiao Xie 17246d07bcecSMiao Xie /* 17257e33fd99SJosef Bacik * btrfs can not use the free space in 17267e33fd99SJosef Bacik * [0, skip_space - 1], we must subtract it from the 17277e33fd99SJosef Bacik * total. In order to implement it, we account the used 17287e33fd99SJosef Bacik * space in this range first. 17296d07bcecSMiao Xie */ 17307e33fd99SJosef Bacik ret = btrfs_account_dev_extents_size(device, 0, 17317e33fd99SJosef Bacik skip_space - 1, 17326d07bcecSMiao Xie &used_space); 17336d07bcecSMiao Xie if (ret) { 17346d07bcecSMiao Xie kfree(devices_info); 17357e33fd99SJosef Bacik mutex_unlock(&fs_devices->device_list_mutex); 17366d07bcecSMiao Xie return ret; 17376d07bcecSMiao Xie } 17386d07bcecSMiao Xie 17397e33fd99SJosef Bacik rcu_read_lock(); 17407e33fd99SJosef Bacik 17416d07bcecSMiao Xie /* calc the free space in [0, skip_space - 1] */ 17426d07bcecSMiao Xie skip_space -= used_space; 17437e33fd99SJosef Bacik } 17446d07bcecSMiao Xie 17456d07bcecSMiao Xie /* 17466d07bcecSMiao Xie * we can use the free space in [0, skip_space - 1], subtract 17476d07bcecSMiao Xie * it from the total. 17486d07bcecSMiao Xie */ 17496d07bcecSMiao Xie if (avail_space && avail_space >= skip_space) 17506d07bcecSMiao Xie avail_space -= skip_space; 17516d07bcecSMiao Xie else 17526d07bcecSMiao Xie avail_space = 0; 17536d07bcecSMiao Xie 17546d07bcecSMiao Xie if (avail_space < min_stripe_size) 17556d07bcecSMiao Xie continue; 17566d07bcecSMiao Xie 17576d07bcecSMiao Xie devices_info[i].dev = device; 17586d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 17596d07bcecSMiao Xie 17606d07bcecSMiao Xie i++; 17616d07bcecSMiao Xie } 17627e33fd99SJosef Bacik rcu_read_unlock(); 17637e33fd99SJosef Bacik if (fs_info->alloc_start) 17647e33fd99SJosef Bacik mutex_unlock(&fs_devices->device_list_mutex); 17656d07bcecSMiao Xie 17666d07bcecSMiao Xie nr_devices = i; 17676d07bcecSMiao Xie 17686d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 17696d07bcecSMiao Xie 17706d07bcecSMiao Xie i = nr_devices - 1; 17716d07bcecSMiao Xie avail_space = 0; 17726d07bcecSMiao Xie while (nr_devices >= min_stripes) { 177339fb26c3SMiao Xie if (num_stripes > nr_devices) 177439fb26c3SMiao Xie num_stripes = nr_devices; 177539fb26c3SMiao Xie 17766d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 17776d07bcecSMiao Xie int j; 17786d07bcecSMiao Xie u64 alloc_size; 17796d07bcecSMiao Xie 178039fb26c3SMiao Xie avail_space += devices_info[i].max_avail * num_stripes; 17816d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 178239fb26c3SMiao Xie for (j = i + 1 - num_stripes; j <= i; j++) 17836d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 17846d07bcecSMiao Xie } 17856d07bcecSMiao Xie i--; 17866d07bcecSMiao Xie nr_devices--; 17876d07bcecSMiao Xie } 17886d07bcecSMiao Xie 17896d07bcecSMiao Xie kfree(devices_info); 17906d07bcecSMiao Xie *free_bytes = avail_space; 17916d07bcecSMiao Xie return 0; 17926d07bcecSMiao Xie } 17936d07bcecSMiao Xie 1794ba7b6e62SDavid Sterba /* 1795ba7b6e62SDavid Sterba * Calculate numbers for 'df', pessimistic in case of mixed raid profiles. 1796ba7b6e62SDavid Sterba * 1797ba7b6e62SDavid Sterba * If there's a redundant raid level at DATA block groups, use the respective 1798ba7b6e62SDavid Sterba * multiplier to scale the sizes. 1799ba7b6e62SDavid Sterba * 1800ba7b6e62SDavid Sterba * Unused device space usage is based on simulating the chunk allocator 1801ba7b6e62SDavid Sterba * algorithm that respects the device sizes, order of allocations and the 1802ba7b6e62SDavid Sterba * 'alloc_start' value, this is a close approximation of the actual use but 1803ba7b6e62SDavid Sterba * there are other factors that may change the result (like a new metadata 1804ba7b6e62SDavid Sterba * chunk). 1805ba7b6e62SDavid Sterba * 1806ba7b6e62SDavid Sterba * FIXME: not accurate for mixed block groups, total and free/used are ok, 1807ba7b6e62SDavid Sterba * available appears slightly larger. 1808ba7b6e62SDavid Sterba */ 18098fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 18108fd17795SChris Mason { 1811815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 1812815745cfSAl Viro struct btrfs_super_block *disk_super = fs_info->super_copy; 1813815745cfSAl Viro struct list_head *head = &fs_info->space_info; 1814bd4d1088SJosef Bacik struct btrfs_space_info *found; 1815bd4d1088SJosef Bacik u64 total_used = 0; 18166d07bcecSMiao Xie u64 total_free_data = 0; 1817db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 1818815745cfSAl Viro __be32 *fsid = (__be32 *)fs_info->fsid; 1819ba7b6e62SDavid Sterba unsigned factor = 1; 1820ba7b6e62SDavid Sterba struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 18216d07bcecSMiao Xie int ret; 18228fd17795SChris Mason 182315484377SMiao Xie /* 182415484377SMiao Xie * holding chunk_muext to avoid allocating new chunks, holding 182515484377SMiao Xie * device_list_mutex to avoid the device being removed 182615484377SMiao Xie */ 1827bd4d1088SJosef Bacik rcu_read_lock(); 182889a55897SJosef Bacik list_for_each_entry_rcu(found, head, list) { 18296d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 1830ba7b6e62SDavid Sterba int i; 1831ba7b6e62SDavid Sterba 18326d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 18336d07bcecSMiao Xie total_free_data -= 18346d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 1835ba7b6e62SDavid Sterba 1836ba7b6e62SDavid Sterba for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { 1837ba7b6e62SDavid Sterba if (!list_empty(&found->block_groups[i])) { 1838ba7b6e62SDavid Sterba switch (i) { 1839ba7b6e62SDavid Sterba case BTRFS_RAID_DUP: 1840ba7b6e62SDavid Sterba case BTRFS_RAID_RAID1: 1841ba7b6e62SDavid Sterba case BTRFS_RAID_RAID10: 1842ba7b6e62SDavid Sterba factor = 2; 1843ba7b6e62SDavid Sterba } 1844ba7b6e62SDavid Sterba } 1845ba7b6e62SDavid Sterba } 18466d07bcecSMiao Xie } 18476d07bcecSMiao Xie 1848b742bb82SYan, Zheng total_used += found->disk_used; 184989a55897SJosef Bacik } 1850ba7b6e62SDavid Sterba 1851bd4d1088SJosef Bacik rcu_read_unlock(); 1852bd4d1088SJosef Bacik 1853ba7b6e62SDavid Sterba buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor); 1854ba7b6e62SDavid Sterba buf->f_blocks >>= bits; 1855ba7b6e62SDavid Sterba buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits); 1856ba7b6e62SDavid Sterba 1857ba7b6e62SDavid Sterba /* Account global block reserve as used, it's in logical size already */ 1858ba7b6e62SDavid Sterba spin_lock(&block_rsv->lock); 1859ba7b6e62SDavid Sterba buf->f_bfree -= block_rsv->size >> bits; 1860ba7b6e62SDavid Sterba spin_unlock(&block_rsv->lock); 1861ba7b6e62SDavid Sterba 18620d95c1beSDavid Sterba buf->f_bavail = div_u64(total_free_data, factor); 1863815745cfSAl Viro ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data); 18647e33fd99SJosef Bacik if (ret) 18656d07bcecSMiao Xie return ret; 1866ba7b6e62SDavid Sterba buf->f_bavail += div_u64(total_free_data, factor); 18676d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 1868d397712bSChris Mason 1869ba7b6e62SDavid Sterba buf->f_type = BTRFS_SUPER_MAGIC; 1870ba7b6e62SDavid Sterba buf->f_bsize = dentry->d_sb->s_blocksize; 1871ba7b6e62SDavid Sterba buf->f_namelen = BTRFS_NAME_LEN; 1872ba7b6e62SDavid Sterba 18739d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 18749d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 18759d03632eSDavid Woodhouse on a big-endian or little-endian host */ 18769d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 18779d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 187832d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 18792b0143b5SDavid Howells buf->f_fsid.val[0] ^= BTRFS_I(d_inode(dentry))->root->objectid >> 32; 18802b0143b5SDavid Howells buf->f_fsid.val[1] ^= BTRFS_I(d_inode(dentry))->root->objectid; 188132d48fa1SDavid Woodhouse 18828fd17795SChris Mason return 0; 18838fd17795SChris Mason } 1884b5133862SChris Mason 1885aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb) 1886aea52e19SAl Viro { 1887815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1888aea52e19SAl Viro kill_anon_super(sb); 1889aea52e19SAl Viro free_fs_info(fs_info); 1890aea52e19SAl Viro } 1891aea52e19SAl Viro 18922e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 18932e635a27SChris Mason .owner = THIS_MODULE, 18942e635a27SChris Mason .name = "btrfs", 1895061dbc6bSAl Viro .mount = btrfs_mount, 1896aea52e19SAl Viro .kill_sb = btrfs_kill_super, 1897f667aef6SQu Wenruo .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA, 18982e635a27SChris Mason }; 18997f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs"); 1900a9218f6bSChris Mason 1901d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file) 1902d8620958STom Van Braeckel { 1903d8620958STom Van Braeckel /* 1904d8620958STom Van Braeckel * The control file's private_data is used to hold the 1905d8620958STom Van Braeckel * transaction when it is started and is used to keep 1906d8620958STom Van Braeckel * track of whether a transaction is already in progress. 1907d8620958STom Van Braeckel */ 1908d8620958STom Van Braeckel file->private_data = NULL; 1909d8620958STom Van Braeckel return 0; 1910d8620958STom Van Braeckel } 1911d8620958STom Van Braeckel 1912d352ac68SChris Mason /* 1913d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 1914d352ac68SChris Mason */ 19158a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 19168a4b83ccSChris Mason unsigned long arg) 19178a4b83ccSChris Mason { 19188a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 19198a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 1920c071fcfdSChris Mason int ret = -ENOTTY; 19218a4b83ccSChris Mason 1922e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1923e441d54dSChris Mason return -EPERM; 1924e441d54dSChris Mason 1925dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 1926dae7b665SLi Zefan if (IS_ERR(vol)) 1927dae7b665SLi Zefan return PTR_ERR(vol); 1928c071fcfdSChris Mason 19298a4b83ccSChris Mason switch (cmd) { 19308a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 193197288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 19328a4b83ccSChris Mason &btrfs_fs_type, &fs_devices); 19338a4b83ccSChris Mason break; 193402db0844SJosef Bacik case BTRFS_IOC_DEVICES_READY: 193502db0844SJosef Bacik ret = btrfs_scan_one_device(vol->name, FMODE_READ, 193602db0844SJosef Bacik &btrfs_fs_type, &fs_devices); 193702db0844SJosef Bacik if (ret) 193802db0844SJosef Bacik break; 193902db0844SJosef Bacik ret = !(fs_devices->num_devices == fs_devices->total_devices); 194002db0844SJosef Bacik break; 19418a4b83ccSChris Mason } 1942dae7b665SLi Zefan 19438a4b83ccSChris Mason kfree(vol); 1944f819d837SLinda Knippers return ret; 19458a4b83ccSChris Mason } 19468a4b83ccSChris Mason 19470176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 1948ed0dab6bSYan { 1949354aa0fbSMiao Xie struct btrfs_trans_handle *trans; 1950354aa0fbSMiao Xie struct btrfs_root *root = btrfs_sb(sb)->tree_root; 1951354aa0fbSMiao Xie 1952d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 1953354aa0fbSMiao Xie if (IS_ERR(trans)) { 1954354aa0fbSMiao Xie /* no transaction, don't bother */ 1955354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 19560176260fSLinus Torvalds return 0; 1957354aa0fbSMiao Xie return PTR_ERR(trans); 1958354aa0fbSMiao Xie } 1959354aa0fbSMiao Xie return btrfs_commit_transaction(trans, root); 1960ed0dab6bSYan } 1961ed0dab6bSYan 19629c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 19639c5085c1SJosef Bacik { 19649c5085c1SJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 19659c5085c1SJosef Bacik struct btrfs_fs_devices *cur_devices; 19669c5085c1SJosef Bacik struct btrfs_device *dev, *first_dev = NULL; 19679c5085c1SJosef Bacik struct list_head *head; 19689c5085c1SJosef Bacik struct rcu_string *name; 19699c5085c1SJosef Bacik 19709c5085c1SJosef Bacik mutex_lock(&fs_info->fs_devices->device_list_mutex); 19719c5085c1SJosef Bacik cur_devices = fs_info->fs_devices; 19729c5085c1SJosef Bacik while (cur_devices) { 19739c5085c1SJosef Bacik head = &cur_devices->devices; 19749c5085c1SJosef Bacik list_for_each_entry(dev, head, dev_list) { 1975aa9ddcd4SJosef Bacik if (dev->missing) 1976aa9ddcd4SJosef Bacik continue; 19770aeb8a6eSAnand Jain if (!dev->name) 19780aeb8a6eSAnand Jain continue; 19799c5085c1SJosef Bacik if (!first_dev || dev->devid < first_dev->devid) 19809c5085c1SJosef Bacik first_dev = dev; 19819c5085c1SJosef Bacik } 19829c5085c1SJosef Bacik cur_devices = cur_devices->seed; 19839c5085c1SJosef Bacik } 19849c5085c1SJosef Bacik 19859c5085c1SJosef Bacik if (first_dev) { 19869c5085c1SJosef Bacik rcu_read_lock(); 19879c5085c1SJosef Bacik name = rcu_dereference(first_dev->name); 19889c5085c1SJosef Bacik seq_escape(m, name->str, " \t\n\\"); 19899c5085c1SJosef Bacik rcu_read_unlock(); 19909c5085c1SJosef Bacik } else { 19919c5085c1SJosef Bacik WARN_ON(1); 19929c5085c1SJosef Bacik } 19939c5085c1SJosef Bacik mutex_unlock(&fs_info->fs_devices->device_list_mutex); 19949c5085c1SJosef Bacik return 0; 19959c5085c1SJosef Bacik } 19969c5085c1SJosef Bacik 1997b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 199876dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 1999bd555975SAl Viro .evict_inode = btrfs_evict_inode, 2000e20d96d6SChris Mason .put_super = btrfs_put_super, 2001d5719762SChris Mason .sync_fs = btrfs_sync_fs, 2002a9572a15SEric Paris .show_options = btrfs_show_options, 20039c5085c1SJosef Bacik .show_devname = btrfs_show_devname, 20044730a4bcSChris Mason .write_inode = btrfs_write_inode, 20052c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 20062c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 20078fd17795SChris Mason .statfs = btrfs_statfs, 2008c146afadSYan Zheng .remount_fs = btrfs_remount, 20090176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 2010e20d96d6SChris Mason }; 2011a9218f6bSChris Mason 2012a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 2013d8620958STom Van Braeckel .open = btrfs_control_open, 2014a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 2015a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 2016a9218f6bSChris Mason .owner = THIS_MODULE, 20176038f373SArnd Bergmann .llseek = noop_llseek, 2018a9218f6bSChris Mason }; 2019a9218f6bSChris Mason 2020a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 2021578454ffSKay Sievers .minor = BTRFS_MINOR, 2022a9218f6bSChris Mason .name = "btrfs-control", 2023a9218f6bSChris Mason .fops = &btrfs_ctl_fops 2024a9218f6bSChris Mason }; 2025a9218f6bSChris Mason 2026578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 2027578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 2028578454ffSKay Sievers 2029a9218f6bSChris Mason static int btrfs_interface_init(void) 2030a9218f6bSChris Mason { 2031a9218f6bSChris Mason return misc_register(&btrfs_misc); 2032a9218f6bSChris Mason } 2033a9218f6bSChris Mason 2034b2950863SChristoph Hellwig static void btrfs_interface_exit(void) 2035a9218f6bSChris Mason { 2036a9218f6bSChris Mason if (misc_deregister(&btrfs_misc) < 0) 2037efe120a0SFrank Holton printk(KERN_INFO "BTRFS: misc_deregister failed for control device\n"); 2038a9218f6bSChris Mason } 2039a9218f6bSChris Mason 204085965600SDavid Sterba static void btrfs_print_info(void) 204185965600SDavid Sterba { 204285965600SDavid Sterba printk(KERN_INFO "Btrfs loaded" 204385965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 204485965600SDavid Sterba ", debug=on" 204585965600SDavid Sterba #endif 204679556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT 204779556c3dSStefan Behrens ", assert=on" 204879556c3dSStefan Behrens #endif 204985965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 205085965600SDavid Sterba ", integrity-checker=on" 205185965600SDavid Sterba #endif 205285965600SDavid Sterba "\n"); 205385965600SDavid Sterba } 205485965600SDavid Sterba 2055dc11dd5dSJosef Bacik static int btrfs_run_sanity_tests(void) 2056dc11dd5dSJosef Bacik { 205706ea65a3SJosef Bacik int ret; 205806ea65a3SJosef Bacik 2059294e30feSJosef Bacik ret = btrfs_init_test_fs(); 206006ea65a3SJosef Bacik if (ret) 206106ea65a3SJosef Bacik return ret; 2062294e30feSJosef Bacik 2063294e30feSJosef Bacik ret = btrfs_test_free_space_cache(); 2064294e30feSJosef Bacik if (ret) 2065294e30feSJosef Bacik goto out; 2066294e30feSJosef Bacik ret = btrfs_test_extent_buffer_operations(); 2067294e30feSJosef Bacik if (ret) 2068294e30feSJosef Bacik goto out; 2069294e30feSJosef Bacik ret = btrfs_test_extent_io(); 2070aaedb55bSJosef Bacik if (ret) 2071aaedb55bSJosef Bacik goto out; 2072aaedb55bSJosef Bacik ret = btrfs_test_inodes(); 2073faa2dbf0SJosef Bacik if (ret) 2074faa2dbf0SJosef Bacik goto out; 2075faa2dbf0SJosef Bacik ret = btrfs_test_qgroups(); 2076294e30feSJosef Bacik out: 2077294e30feSJosef Bacik btrfs_destroy_test_fs(); 2078294e30feSJosef Bacik return ret; 2079dc11dd5dSJosef Bacik } 2080dc11dd5dSJosef Bacik 20812e635a27SChris Mason static int __init init_btrfs_fs(void) 20822e635a27SChris Mason { 20832c90e5d6SChris Mason int err; 208458176a96SJosef Bacik 208514a958e6SFilipe David Borba Manana err = btrfs_hash_init(); 208614a958e6SFilipe David Borba Manana if (err) 208714a958e6SFilipe David Borba Manana return err; 208814a958e6SFilipe David Borba Manana 208963541927SFilipe David Borba Manana btrfs_props_init(); 209063541927SFilipe David Borba Manana 209158176a96SJosef Bacik err = btrfs_init_sysfs(); 209258176a96SJosef Bacik if (err) 209314a958e6SFilipe David Borba Manana goto free_hash; 209458176a96SJosef Bacik 2095143bede5SJeff Mahoney btrfs_init_compress(); 2096d1310b2eSChris Mason 2097261507a0SLi Zefan err = btrfs_init_cachep(); 2098261507a0SLi Zefan if (err) 2099261507a0SLi Zefan goto free_compress; 2100261507a0SLi Zefan 2101d1310b2eSChris Mason err = extent_io_init(); 21022f4cbe64SWyatt Banks if (err) 21032f4cbe64SWyatt Banks goto free_cachep; 21042f4cbe64SWyatt Banks 2105d1310b2eSChris Mason err = extent_map_init(); 2106d1310b2eSChris Mason if (err) 2107d1310b2eSChris Mason goto free_extent_io; 2108d1310b2eSChris Mason 21096352b91dSMiao Xie err = ordered_data_init(); 21102f4cbe64SWyatt Banks if (err) 21112f4cbe64SWyatt Banks goto free_extent_map; 2112c8b97818SChris Mason 21136352b91dSMiao Xie err = btrfs_delayed_inode_init(); 21146352b91dSMiao Xie if (err) 21156352b91dSMiao Xie goto free_ordered_data; 21166352b91dSMiao Xie 21179247f317SMiao Xie err = btrfs_auto_defrag_init(); 211816cdcec7SMiao Xie if (err) 211916cdcec7SMiao Xie goto free_delayed_inode; 212016cdcec7SMiao Xie 212178a6184aSMiao Xie err = btrfs_delayed_ref_init(); 21229247f317SMiao Xie if (err) 21239247f317SMiao Xie goto free_auto_defrag; 21249247f317SMiao Xie 2125b9e9a6cbSWang Shilong err = btrfs_prelim_ref_init(); 2126b9e9a6cbSWang Shilong if (err) 2127af13b492SDavid Sterba goto free_delayed_ref; 2128b9e9a6cbSWang Shilong 212997eb6b69SDavid Sterba err = btrfs_end_io_wq_init(); 213078a6184aSMiao Xie if (err) 2131af13b492SDavid Sterba goto free_prelim_ref; 213278a6184aSMiao Xie 213397eb6b69SDavid Sterba err = btrfs_interface_init(); 213497eb6b69SDavid Sterba if (err) 213597eb6b69SDavid Sterba goto free_end_io_wq; 213697eb6b69SDavid Sterba 2137e565d4b9SJan Schmidt btrfs_init_lockdep(); 2138e565d4b9SJan Schmidt 213985965600SDavid Sterba btrfs_print_info(); 2140dc11dd5dSJosef Bacik 2141dc11dd5dSJosef Bacik err = btrfs_run_sanity_tests(); 2142dc11dd5dSJosef Bacik if (err) 2143dc11dd5dSJosef Bacik goto unregister_ioctl; 2144dc11dd5dSJosef Bacik 2145dc11dd5dSJosef Bacik err = register_filesystem(&btrfs_fs_type); 2146dc11dd5dSJosef Bacik if (err) 2147dc11dd5dSJosef Bacik goto unregister_ioctl; 214874255aa0SJosef Bacik 21492f4cbe64SWyatt Banks return 0; 21502f4cbe64SWyatt Banks 2151a9218f6bSChris Mason unregister_ioctl: 2152a9218f6bSChris Mason btrfs_interface_exit(); 215397eb6b69SDavid Sterba free_end_io_wq: 215497eb6b69SDavid Sterba btrfs_end_io_wq_exit(); 2155b9e9a6cbSWang Shilong free_prelim_ref: 2156b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 215778a6184aSMiao Xie free_delayed_ref: 215878a6184aSMiao Xie btrfs_delayed_ref_exit(); 21599247f317SMiao Xie free_auto_defrag: 21609247f317SMiao Xie btrfs_auto_defrag_exit(); 216116cdcec7SMiao Xie free_delayed_inode: 216216cdcec7SMiao Xie btrfs_delayed_inode_exit(); 21636352b91dSMiao Xie free_ordered_data: 21646352b91dSMiao Xie ordered_data_exit(); 21652f4cbe64SWyatt Banks free_extent_map: 21662f4cbe64SWyatt Banks extent_map_exit(); 2167d1310b2eSChris Mason free_extent_io: 2168d1310b2eSChris Mason extent_io_exit(); 21692f4cbe64SWyatt Banks free_cachep: 21702f4cbe64SWyatt Banks btrfs_destroy_cachep(); 2171261507a0SLi Zefan free_compress: 2172261507a0SLi Zefan btrfs_exit_compress(); 21732f4cbe64SWyatt Banks btrfs_exit_sysfs(); 217414a958e6SFilipe David Borba Manana free_hash: 217514a958e6SFilipe David Borba Manana btrfs_hash_exit(); 21762c90e5d6SChris Mason return err; 21772e635a27SChris Mason } 21782e635a27SChris Mason 21792e635a27SChris Mason static void __exit exit_btrfs_fs(void) 21802e635a27SChris Mason { 218139279cc3SChris Mason btrfs_destroy_cachep(); 218278a6184aSMiao Xie btrfs_delayed_ref_exit(); 21839247f317SMiao Xie btrfs_auto_defrag_exit(); 218416cdcec7SMiao Xie btrfs_delayed_inode_exit(); 2185b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 21866352b91dSMiao Xie ordered_data_exit(); 2187a52d9a80SChris Mason extent_map_exit(); 2188d1310b2eSChris Mason extent_io_exit(); 2189a9218f6bSChris Mason btrfs_interface_exit(); 21905ed5f588SJosef Bacik btrfs_end_io_wq_exit(); 21912e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 219258176a96SJosef Bacik btrfs_exit_sysfs(); 21938a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 2194261507a0SLi Zefan btrfs_exit_compress(); 219514a958e6SFilipe David Borba Manana btrfs_hash_exit(); 21962e635a27SChris Mason } 21972e635a27SChris Mason 219860efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs); 21992e635a27SChris Mason module_exit(exit_btrfs_fs) 22002e635a27SChris Mason 22012e635a27SChris Mason MODULE_LICENSE("GPL"); 2202