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 631abe9b8aSliubo #define CREATE_TRACE_POINTS 641abe9b8aSliubo #include <trace/events/btrfs.h> 651abe9b8aSliubo 66b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops; 67830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type; 68e20d96d6SChris Mason 6908748810SDavid Sterba static const char *btrfs_decode_error(int errno) 70acce952bSliubo { 7108748810SDavid Sterba char *errstr = "unknown"; 72acce952bSliubo 73acce952bSliubo switch (errno) { 74acce952bSliubo case -EIO: 75acce952bSliubo errstr = "IO failure"; 76acce952bSliubo break; 77acce952bSliubo case -ENOMEM: 78acce952bSliubo errstr = "Out of memory"; 79acce952bSliubo break; 80acce952bSliubo case -EROFS: 81acce952bSliubo errstr = "Readonly filesystem"; 82acce952bSliubo break; 838c342930SJeff Mahoney case -EEXIST: 848c342930SJeff Mahoney errstr = "Object already exists"; 858c342930SJeff Mahoney break; 8694ef7280SDavid Sterba case -ENOSPC: 8794ef7280SDavid Sterba errstr = "No space left"; 8894ef7280SDavid Sterba break; 8994ef7280SDavid Sterba case -ENOENT: 9094ef7280SDavid Sterba errstr = "No such entry"; 9194ef7280SDavid Sterba break; 92acce952bSliubo } 93acce952bSliubo 94acce952bSliubo return errstr; 95acce952bSliubo } 96acce952bSliubo 97bbece8a3SDavid Sterba static void save_error_info(struct btrfs_fs_info *fs_info) 98acce952bSliubo { 99acce952bSliubo /* 100acce952bSliubo * today we only save the error info into ram. Long term we'll 101acce952bSliubo * also send it down to the disk 102acce952bSliubo */ 10387533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 104acce952bSliubo } 105acce952bSliubo 106acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */ 107acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info) 108acce952bSliubo { 109acce952bSliubo struct super_block *sb = fs_info->sb; 110acce952bSliubo 111acce952bSliubo if (sb->s_flags & MS_RDONLY) 112acce952bSliubo return; 113acce952bSliubo 11487533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 115acce952bSliubo sb->s_flags |= MS_RDONLY; 116c2cf52ebSSimon Kirby btrfs_info(fs_info, "forced readonly"); 1171acd6831SStefan Behrens /* 1181acd6831SStefan Behrens * Note that a running device replace operation is not 1191acd6831SStefan Behrens * canceled here although there is no way to update 1201acd6831SStefan Behrens * the progress. It would add the risk of a deadlock, 1211acd6831SStefan Behrens * therefore the canceling is ommited. The only penalty 1221acd6831SStefan Behrens * is that some I/O remains active until the procedure 1231acd6831SStefan Behrens * completes. The next time when the filesystem is 1241acd6831SStefan Behrens * mounted writeable again, the device replace 1251acd6831SStefan Behrens * operation continues. 1261acd6831SStefan Behrens */ 127acce952bSliubo } 128acce952bSliubo } 129acce952bSliubo 130533574c6SJoe Perches #ifdef CONFIG_PRINTK 131acce952bSliubo /* 132acce952bSliubo * __btrfs_std_error decodes expected errors from the caller and 133acce952bSliubo * invokes the approciate error response. 134acce952bSliubo */ 135acce952bSliubo void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function, 1364da35113SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 137acce952bSliubo { 138acce952bSliubo struct super_block *sb = fs_info->sb; 139acce952bSliubo const char *errstr; 140acce952bSliubo 141acce952bSliubo /* 142acce952bSliubo * Special case: if the error is EROFS, and we're already 143acce952bSliubo * under MS_RDONLY, then it is safe here. 144acce952bSliubo */ 145acce952bSliubo if (errno == -EROFS && (sb->s_flags & MS_RDONLY)) 146acce952bSliubo return; 147acce952bSliubo 14808748810SDavid Sterba errstr = btrfs_decode_error(errno); 1494da35113SJeff Mahoney if (fmt) { 15037252a66SEric Sandeen struct va_format vaf; 15137252a66SEric Sandeen va_list args; 15237252a66SEric Sandeen 15337252a66SEric Sandeen va_start(args, fmt); 15437252a66SEric Sandeen vaf.fmt = fmt; 15537252a66SEric Sandeen vaf.va = &args; 1564da35113SJeff Mahoney 157efe120a0SFrank Holton printk(KERN_CRIT 158efe120a0SFrank Holton "BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n", 15908748810SDavid Sterba sb->s_id, function, line, errno, errstr, &vaf); 16037252a66SEric Sandeen va_end(args); 1614da35113SJeff Mahoney } else { 162efe120a0SFrank Holton printk(KERN_CRIT "BTRFS: error (device %s) in %s:%d: errno=%d %s\n", 16308748810SDavid Sterba sb->s_id, function, line, errno, errstr); 1644da35113SJeff Mahoney } 165acce952bSliubo 1664da35113SJeff Mahoney /* Don't go through full error handling during mount */ 1674da35113SJeff Mahoney save_error_info(fs_info); 168cf79ffb5SJosef Bacik if (sb->s_flags & MS_BORN) 169acce952bSliubo btrfs_handle_error(fs_info); 170acce952bSliubo } 1714da35113SJeff Mahoney 172533574c6SJoe Perches static const char * const logtypes[] = { 1734da35113SJeff Mahoney "emergency", 1744da35113SJeff Mahoney "alert", 1754da35113SJeff Mahoney "critical", 1764da35113SJeff Mahoney "error", 1774da35113SJeff Mahoney "warning", 1784da35113SJeff Mahoney "notice", 1794da35113SJeff Mahoney "info", 1804da35113SJeff Mahoney "debug", 1814da35113SJeff Mahoney }; 1824da35113SJeff Mahoney 183c2cf52ebSSimon Kirby void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...) 1844da35113SJeff Mahoney { 1854da35113SJeff Mahoney struct super_block *sb = fs_info->sb; 1864da35113SJeff Mahoney char lvl[4]; 1874da35113SJeff Mahoney struct va_format vaf; 1884da35113SJeff Mahoney va_list args; 1894da35113SJeff Mahoney const char *type = logtypes[4]; 190533574c6SJoe Perches int kern_level; 1914da35113SJeff Mahoney 1924da35113SJeff Mahoney va_start(args, fmt); 1934da35113SJeff Mahoney 194533574c6SJoe Perches kern_level = printk_get_level(fmt); 195533574c6SJoe Perches if (kern_level) { 196533574c6SJoe Perches size_t size = printk_skip_level(fmt) - fmt; 197533574c6SJoe Perches memcpy(lvl, fmt, size); 198533574c6SJoe Perches lvl[size] = '\0'; 199533574c6SJoe Perches fmt += size; 200533574c6SJoe Perches type = logtypes[kern_level - '0']; 2014da35113SJeff Mahoney } else 2024da35113SJeff Mahoney *lvl = '\0'; 2034da35113SJeff Mahoney 2044da35113SJeff Mahoney vaf.fmt = fmt; 2054da35113SJeff Mahoney vaf.va = &args; 206533574c6SJoe Perches 207c2cf52ebSSimon Kirby printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf); 208533574c6SJoe Perches 209533574c6SJoe Perches va_end(args); 2104da35113SJeff Mahoney } 211acce952bSliubo 212533574c6SJoe Perches #else 213533574c6SJoe Perches 214533574c6SJoe Perches void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function, 215533574c6SJoe Perches unsigned int line, int errno, const char *fmt, ...) 216533574c6SJoe Perches { 217533574c6SJoe Perches struct super_block *sb = fs_info->sb; 218533574c6SJoe Perches 219533574c6SJoe Perches /* 220533574c6SJoe Perches * Special case: if the error is EROFS, and we're already 221533574c6SJoe Perches * under MS_RDONLY, then it is safe here. 222533574c6SJoe Perches */ 223533574c6SJoe Perches if (errno == -EROFS && (sb->s_flags & MS_RDONLY)) 224533574c6SJoe Perches return; 225533574c6SJoe Perches 226533574c6SJoe Perches /* Don't go through full error handling during mount */ 227533574c6SJoe Perches if (sb->s_flags & MS_BORN) { 228533574c6SJoe Perches save_error_info(fs_info); 229533574c6SJoe Perches btrfs_handle_error(fs_info); 230533574c6SJoe Perches } 231533574c6SJoe Perches } 232533574c6SJoe Perches #endif 233533574c6SJoe Perches 2348c342930SJeff Mahoney /* 23549b25e05SJeff Mahoney * We only mark the transaction aborted and then set the file system read-only. 23649b25e05SJeff Mahoney * This will prevent new transactions from starting or trying to join this 23749b25e05SJeff Mahoney * one. 23849b25e05SJeff Mahoney * 23949b25e05SJeff Mahoney * This means that error recovery at the call site is limited to freeing 24049b25e05SJeff Mahoney * any local memory allocations and passing the error code up without 24149b25e05SJeff Mahoney * further cleanup. The transaction should complete as it normally would 24249b25e05SJeff Mahoney * in the call path but will return -EIO. 24349b25e05SJeff Mahoney * 24449b25e05SJeff Mahoney * We'll complete the cleanup in btrfs_end_transaction and 24549b25e05SJeff Mahoney * btrfs_commit_transaction. 24649b25e05SJeff Mahoney */ 24749b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans, 24849b25e05SJeff Mahoney struct btrfs_root *root, const char *function, 24949b25e05SJeff Mahoney unsigned int line, int errno) 25049b25e05SJeff Mahoney { 25108748810SDavid Sterba /* 25208748810SDavid Sterba * Report first abort since mount 25308748810SDavid Sterba */ 25408748810SDavid Sterba if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED, 25508748810SDavid Sterba &root->fs_info->fs_state)) { 256efe120a0SFrank Holton WARN(1, KERN_DEBUG "BTRFS: Transaction aborted (error %d)\n", 25708748810SDavid Sterba errno); 25808748810SDavid Sterba } 25949b25e05SJeff Mahoney trans->aborted = errno; 26049b25e05SJeff Mahoney /* Nothing used. The other threads that have joined this 26149b25e05SJeff Mahoney * transaction may be able to continue. */ 26249b25e05SJeff Mahoney if (!trans->blocks_used) { 26369ce977aSMiao Xie const char *errstr; 26469ce977aSMiao Xie 26508748810SDavid Sterba errstr = btrfs_decode_error(errno); 266c2cf52ebSSimon Kirby btrfs_warn(root->fs_info, 267c2cf52ebSSimon Kirby "%s:%d: Aborting unused transaction(%s).", 26869ce977aSMiao Xie function, line, errstr); 26949b25e05SJeff Mahoney return; 27049b25e05SJeff Mahoney } 2718d25a086SMiao Xie ACCESS_ONCE(trans->transaction->aborted) = errno; 272501407aaSJosef Bacik /* Wake up anybody who may be waiting on this transaction */ 273501407aaSJosef Bacik wake_up(&root->fs_info->transaction_wait); 274501407aaSJosef Bacik wake_up(&root->fs_info->transaction_blocked_wait); 27549b25e05SJeff Mahoney __btrfs_std_error(root->fs_info, function, line, errno, NULL); 27649b25e05SJeff Mahoney } 27749b25e05SJeff Mahoney /* 2788c342930SJeff Mahoney * __btrfs_panic decodes unexpected, fatal errors from the caller, 2798c342930SJeff Mahoney * issues an alert, and either panics or BUGs, depending on mount options. 2808c342930SJeff Mahoney */ 2818c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, 2828c342930SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 2838c342930SJeff Mahoney { 2848c342930SJeff Mahoney char *s_id = "<unknown>"; 2858c342930SJeff Mahoney const char *errstr; 2868c342930SJeff Mahoney struct va_format vaf = { .fmt = fmt }; 2878c342930SJeff Mahoney va_list args; 2888c342930SJeff Mahoney 2898c342930SJeff Mahoney if (fs_info) 2908c342930SJeff Mahoney s_id = fs_info->sb->s_id; 2918c342930SJeff Mahoney 2928c342930SJeff Mahoney va_start(args, fmt); 2938c342930SJeff Mahoney vaf.va = &args; 2948c342930SJeff Mahoney 29508748810SDavid Sterba errstr = btrfs_decode_error(errno); 296aa43a17cSEric Sandeen if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR)) 29708748810SDavid Sterba panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n", 29808748810SDavid Sterba s_id, function, line, &vaf, errno, errstr); 2998c342930SJeff Mahoney 300efe120a0SFrank Holton btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)", 301efe120a0SFrank Holton function, line, &vaf, errno, errstr); 3028c342930SJeff Mahoney va_end(args); 3038c342930SJeff Mahoney /* Caller calls BUG() */ 3048c342930SJeff Mahoney } 305e20d96d6SChris Mason 306e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 307e20d96d6SChris Mason { 308815745cfSAl Viro (void)close_ctree(btrfs_sb(sb)->tree_root); 309aea52e19SAl Viro /* FIXME: need to fix VFS to return error? */ 310aea52e19SAl Viro /* AV: return it _where_? ->put_super() can be triggered by any number 311aea52e19SAl Viro * of async events, up to and including delivery of SIGKILL to the 312aea52e19SAl Viro * last process that kept it busy. Or segfault in the aforementioned 313aea52e19SAl Viro * process... Whom would you report that to? 314aea52e19SAl Viro */ 315e20d96d6SChris Mason } 3162e635a27SChris Mason 31795e05289SChris Mason enum { 31873f73415SJosef Bacik Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum, 319287a0ab9SJosef Bacik Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, 320287a0ab9SJosef Bacik Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress, 321261507a0SLi Zefan Opt_compress_type, Opt_compress_force, Opt_compress_force_type, 322261507a0SLi Zefan Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard, 32391435650SChris Mason Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed, 3249555c6c1SIlya Dryomov Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache, 3259555c6c1SIlya Dryomov Opt_no_space_cache, Opt_recovery, Opt_skip_balance, 32621adbd5cSStefan Behrens Opt_check_integrity, Opt_check_integrity_including_extent_data, 327f420ee1eSStefan Behrens Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree, 328e07a2adeSQu Wenruo Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard, 329a258af7aSQu Wenruo Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow, 3303818aea2SQu Wenruo Opt_datasum, Opt_treelog, Opt_noinode_cache, 3319555c6c1SIlya Dryomov Opt_err, 33295e05289SChris Mason }; 33395e05289SChris Mason 33495e05289SChris Mason static match_table_t tokens = { 335dfe25020SChris Mason {Opt_degraded, "degraded"}, 33695e05289SChris Mason {Opt_subvol, "subvol=%s"}, 3371493381fSWang Shilong {Opt_subvolid, "subvolid=%s"}, 33843e570b0SChristoph Hellwig {Opt_device, "device=%s"}, 339b6cda9bcSChris Mason {Opt_nodatasum, "nodatasum"}, 340d399167dSQu Wenruo {Opt_datasum, "datasum"}, 341be20aa9dSChris Mason {Opt_nodatacow, "nodatacow"}, 342a258af7aSQu Wenruo {Opt_datacow, "datacow"}, 34321ad10cfSChris Mason {Opt_nobarrier, "nobarrier"}, 344842bef58SQu Wenruo {Opt_barrier, "barrier"}, 3456f568d35SChris Mason {Opt_max_inline, "max_inline=%s"}, 3468f662a76SChris Mason {Opt_alloc_start, "alloc_start=%s"}, 3474543df7eSChris Mason {Opt_thread_pool, "thread_pool=%d"}, 348c8b97818SChris Mason {Opt_compress, "compress"}, 349261507a0SLi Zefan {Opt_compress_type, "compress=%s"}, 350a555f810SChris Mason {Opt_compress_force, "compress-force"}, 351261507a0SLi Zefan {Opt_compress_force_type, "compress-force=%s"}, 352e18e4809SChris Mason {Opt_ssd, "ssd"}, 353451d7585SChris Mason {Opt_ssd_spread, "ssd_spread"}, 3543b30c22fSChris Mason {Opt_nossd, "nossd"}, 355bd0330adSQu Wenruo {Opt_acl, "acl"}, 35633268eafSJosef Bacik {Opt_noacl, "noacl"}, 3573a5e1404SSage Weil {Opt_notreelog, "notreelog"}, 358a88998f2SQu Wenruo {Opt_treelog, "treelog"}, 359dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 3602c9ee856SQu Wenruo {Opt_noflushoncommit, "noflushoncommit"}, 36197e728d4SJosef Bacik {Opt_ratio, "metadata_ratio=%d"}, 362e244a0aeSChristoph Hellwig {Opt_discard, "discard"}, 363e07a2adeSQu Wenruo {Opt_nodiscard, "nodiscard"}, 3640af3d00bSJosef Bacik {Opt_space_cache, "space_cache"}, 36588c2ba3bSJosef Bacik {Opt_clear_cache, "clear_cache"}, 3664260f7c7SSage Weil {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, 36791435650SChris Mason {Opt_enospc_debug, "enospc_debug"}, 36853036293SQu Wenruo {Opt_noenospc_debug, "noenospc_debug"}, 369e15d0542SXin Zhong {Opt_subvolrootid, "subvolrootid=%d"}, 3704cb5300bSChris Mason {Opt_defrag, "autodefrag"}, 371fc0ca9afSQu Wenruo {Opt_nodefrag, "noautodefrag"}, 3724b9465cbSChris Mason {Opt_inode_cache, "inode_cache"}, 3733818aea2SQu Wenruo {Opt_noinode_cache, "noinode_cache"}, 3748965593eSDavid Sterba {Opt_no_space_cache, "nospace_cache"}, 375af31f5e5SChris Mason {Opt_recovery, "recovery"}, 3769555c6c1SIlya Dryomov {Opt_skip_balance, "skip_balance"}, 37721adbd5cSStefan Behrens {Opt_check_integrity, "check_int"}, 37821adbd5cSStefan Behrens {Opt_check_integrity_including_extent_data, "check_int_data"}, 37921adbd5cSStefan Behrens {Opt_check_integrity_print_mask, "check_int_print_mask=%d"}, 380f420ee1eSStefan Behrens {Opt_rescan_uuid_tree, "rescan_uuid_tree"}, 3818c342930SJeff Mahoney {Opt_fatal_errors, "fatal_errors=%s"}, 3828b87dc17SDavid Sterba {Opt_commit_interval, "commit=%d"}, 38333268eafSJosef Bacik {Opt_err, NULL}, 38495e05289SChris Mason }; 38595e05289SChris Mason 38607802534SQu Wenruo #define btrfs_set_and_info(root, opt, fmt, args...) \ 38707802534SQu Wenruo { \ 38807802534SQu Wenruo if (!btrfs_test_opt(root, opt)) \ 38907802534SQu Wenruo btrfs_info(root->fs_info, fmt, ##args); \ 39007802534SQu Wenruo btrfs_set_opt(root->fs_info->mount_opt, opt); \ 39107802534SQu Wenruo } 39207802534SQu Wenruo 39307802534SQu Wenruo #define btrfs_clear_and_info(root, opt, fmt, args...) \ 39407802534SQu Wenruo { \ 39507802534SQu Wenruo if (btrfs_test_opt(root, opt)) \ 39607802534SQu Wenruo btrfs_info(root->fs_info, fmt, ##args); \ 39707802534SQu Wenruo btrfs_clear_opt(root->fs_info->mount_opt, opt); \ 39807802534SQu Wenruo } 39907802534SQu Wenruo 400edf24abeSChristoph Hellwig /* 401edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 402edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 40349b25e05SJeff Mahoney * XXX JDM: This needs to be cleaned up for remount. 404edf24abeSChristoph Hellwig */ 405edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options) 40695e05289SChris Mason { 407edf24abeSChristoph Hellwig struct btrfs_fs_info *info = root->fs_info; 40895e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 40973bc1876SJosef Bacik char *p, *num, *orig = NULL; 41073bc1876SJosef Bacik u64 cache_gen; 4114543df7eSChris Mason int intarg; 412a7a3f7caSSage Weil int ret = 0; 413261507a0SLi Zefan char *compress_type; 414261507a0SLi Zefan bool compress_force = false; 41507802534SQu Wenruo bool compress = false; 416b6cda9bcSChris Mason 4176c41761fSDavid Sterba cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy); 41873bc1876SJosef Bacik if (cache_gen) 41973bc1876SJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 42073bc1876SJosef Bacik 42195e05289SChris Mason if (!options) 42273bc1876SJosef Bacik goto out; 42395e05289SChris Mason 424be20aa9dSChris Mason /* 425be20aa9dSChris Mason * strsep changes the string, duplicate it because parse_options 426be20aa9dSChris Mason * gets called twice 427be20aa9dSChris Mason */ 428be20aa9dSChris Mason options = kstrdup(options, GFP_NOFS); 429be20aa9dSChris Mason if (!options) 430be20aa9dSChris Mason return -ENOMEM; 431be20aa9dSChris Mason 432da495eccSJosef Bacik orig = options; 433be20aa9dSChris Mason 43495e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 43595e05289SChris Mason int token; 43695e05289SChris Mason if (!*p) 43795e05289SChris Mason continue; 43895e05289SChris Mason 43995e05289SChris Mason token = match_token(p, tokens, args); 44095e05289SChris Mason switch (token) { 441dfe25020SChris Mason case Opt_degraded: 442efe120a0SFrank Holton btrfs_info(root->fs_info, "allowing degraded mounts"); 443dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 444dfe25020SChris Mason break; 44595e05289SChris Mason case Opt_subvol: 44673f73415SJosef Bacik case Opt_subvolid: 447e15d0542SXin Zhong case Opt_subvolrootid: 44843e570b0SChristoph Hellwig case Opt_device: 449edf24abeSChristoph Hellwig /* 45043e570b0SChristoph Hellwig * These are parsed by btrfs_parse_early_options 451edf24abeSChristoph Hellwig * and can be happily ignored here. 452edf24abeSChristoph Hellwig */ 45395e05289SChris Mason break; 454b6cda9bcSChris Mason case Opt_nodatasum: 45507802534SQu Wenruo btrfs_set_and_info(root, NODATASUM, 45607802534SQu Wenruo "setting nodatasum"); 457be20aa9dSChris Mason break; 458d399167dSQu Wenruo case Opt_datasum: 45907802534SQu Wenruo if (btrfs_test_opt(root, NODATASUM)) { 460d399167dSQu Wenruo if (btrfs_test_opt(root, NODATACOW)) 461d399167dSQu Wenruo btrfs_info(root->fs_info, "setting datasum, datacow enabled"); 462d399167dSQu Wenruo else 463d399167dSQu Wenruo btrfs_info(root->fs_info, "setting datasum"); 46407802534SQu Wenruo } 465d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATACOW); 466d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATASUM); 467d399167dSQu Wenruo break; 468be20aa9dSChris Mason case Opt_nodatacow: 46907802534SQu Wenruo if (!btrfs_test_opt(root, NODATACOW)) { 470bedb2ccaSAndrei Popa if (!btrfs_test_opt(root, COMPRESS) || 471bedb2ccaSAndrei Popa !btrfs_test_opt(root, FORCE_COMPRESS)) { 472efe120a0SFrank Holton btrfs_info(root->fs_info, 473efe120a0SFrank Holton "setting nodatacow, compression disabled"); 474bedb2ccaSAndrei Popa } else { 475efe120a0SFrank Holton btrfs_info(root->fs_info, "setting nodatacow"); 476bedb2ccaSAndrei Popa } 47707802534SQu Wenruo } 478bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, COMPRESS); 479bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 480be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 481be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 482b6cda9bcSChris Mason break; 483a258af7aSQu Wenruo case Opt_datacow: 48407802534SQu Wenruo btrfs_clear_and_info(root, NODATACOW, 48507802534SQu Wenruo "setting datacow"); 486a258af7aSQu Wenruo break; 487a555f810SChris Mason case Opt_compress_force: 488261507a0SLi Zefan case Opt_compress_force_type: 489261507a0SLi Zefan compress_force = true; 4901c697d4aSEric Sandeen /* Fallthrough */ 491261507a0SLi Zefan case Opt_compress: 492261507a0SLi Zefan case Opt_compress_type: 49307802534SQu Wenruo compress = true; 494261507a0SLi Zefan if (token == Opt_compress || 495261507a0SLi Zefan token == Opt_compress_force || 496261507a0SLi Zefan strcmp(args[0].from, "zlib") == 0) { 497261507a0SLi Zefan compress_type = "zlib"; 498261507a0SLi Zefan info->compress_type = BTRFS_COMPRESS_ZLIB; 499063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 500bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 501bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 502a6fa6faeSLi Zefan } else if (strcmp(args[0].from, "lzo") == 0) { 503a6fa6faeSLi Zefan compress_type = "lzo"; 504a6fa6faeSLi Zefan info->compress_type = BTRFS_COMPRESS_LZO; 505063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 506bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 507bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 5082b0ce2c2SMitch Harder btrfs_set_fs_incompat(info, COMPRESS_LZO); 509063849eaSArnd Hannemann } else if (strncmp(args[0].from, "no", 2) == 0) { 510063849eaSArnd Hannemann compress_type = "no"; 511063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, COMPRESS); 512063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 513063849eaSArnd Hannemann compress_force = false; 514261507a0SLi Zefan } else { 515261507a0SLi Zefan ret = -EINVAL; 516261507a0SLi Zefan goto out; 517261507a0SLi Zefan } 518261507a0SLi Zefan 519261507a0SLi Zefan if (compress_force) { 52007802534SQu Wenruo btrfs_set_and_info(root, FORCE_COMPRESS, 52107802534SQu Wenruo "force %s compression", 522261507a0SLi Zefan compress_type); 52307802534SQu Wenruo } else if (compress) { 52407802534SQu Wenruo if (!btrfs_test_opt(root, COMPRESS)) 52507802534SQu Wenruo btrfs_info(root->fs_info, 52607802534SQu Wenruo "btrfs: use %s compression\n", 527261507a0SLi Zefan compress_type); 528a7e252afSMiao Xie } 529a555f810SChris Mason break; 530e18e4809SChris Mason case Opt_ssd: 53107802534SQu Wenruo btrfs_set_and_info(root, SSD, 53207802534SQu Wenruo "use ssd allocation scheme"); 533e18e4809SChris Mason break; 534451d7585SChris Mason case Opt_ssd_spread: 53507802534SQu Wenruo btrfs_set_and_info(root, SSD_SPREAD, 53607802534SQu Wenruo "use spread ssd allocation scheme"); 537451d7585SChris Mason break; 5383b30c22fSChris Mason case Opt_nossd: 53907802534SQu Wenruo btrfs_clear_and_info(root, NOSSD, 54007802534SQu Wenruo "not using ssd allocation scheme"); 5413b30c22fSChris Mason btrfs_clear_opt(info->mount_opt, SSD); 5423b30c22fSChris Mason break; 543842bef58SQu Wenruo case Opt_barrier: 54407802534SQu Wenruo btrfs_clear_and_info(root, NOBARRIER, 54507802534SQu Wenruo "turning on barriers"); 546842bef58SQu Wenruo break; 54721ad10cfSChris Mason case Opt_nobarrier: 54807802534SQu Wenruo btrfs_set_and_info(root, NOBARRIER, 54907802534SQu Wenruo "turning off barriers"); 55021ad10cfSChris Mason break; 5514543df7eSChris Mason case Opt_thread_pool: 5522c334e87SWang Shilong ret = match_int(&args[0], &intarg); 5532c334e87SWang Shilong if (ret) { 5542c334e87SWang Shilong goto out; 5552c334e87SWang Shilong } else if (intarg > 0) { 5564543df7eSChris Mason info->thread_pool_size = intarg; 5572c334e87SWang Shilong } else { 5582c334e87SWang Shilong ret = -EINVAL; 5592c334e87SWang Shilong goto out; 5602c334e87SWang Shilong } 5614543df7eSChris Mason break; 5626f568d35SChris Mason case Opt_max_inline: 563edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5646f568d35SChris Mason if (num) { 56591748467SAkinobu Mita info->max_inline = memparse(num, NULL); 5666f568d35SChris Mason kfree(num); 5676f568d35SChris Mason 56815ada040SChris Mason if (info->max_inline) { 569feb5f965SMitch Harder info->max_inline = min_t(u64, 57015ada040SChris Mason info->max_inline, 57115ada040SChris Mason root->sectorsize); 57215ada040SChris Mason } 573efe120a0SFrank Holton btrfs_info(root->fs_info, "max_inline at %llu", 574c1c9ff7cSGeert Uytterhoeven info->max_inline); 5752c334e87SWang Shilong } else { 5762c334e87SWang Shilong ret = -ENOMEM; 5772c334e87SWang Shilong goto out; 5786f568d35SChris Mason } 5796f568d35SChris Mason break; 5808f662a76SChris Mason case Opt_alloc_start: 581edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5828f662a76SChris Mason if (num) { 583c018daecSMiao Xie mutex_lock(&info->chunk_mutex); 58491748467SAkinobu Mita info->alloc_start = memparse(num, NULL); 585c018daecSMiao Xie mutex_unlock(&info->chunk_mutex); 5868f662a76SChris Mason kfree(num); 587efe120a0SFrank Holton btrfs_info(root->fs_info, "allocations start at %llu", 588c1c9ff7cSGeert Uytterhoeven info->alloc_start); 5892c334e87SWang Shilong } else { 5902c334e87SWang Shilong ret = -ENOMEM; 5912c334e87SWang Shilong goto out; 5928f662a76SChris Mason } 5938f662a76SChris Mason break; 594bd0330adSQu Wenruo case Opt_acl: 595bd0330adSQu Wenruo root->fs_info->sb->s_flags |= MS_POSIXACL; 596bd0330adSQu Wenruo break; 59733268eafSJosef Bacik case Opt_noacl: 59833268eafSJosef Bacik root->fs_info->sb->s_flags &= ~MS_POSIXACL; 59933268eafSJosef Bacik break; 6003a5e1404SSage Weil case Opt_notreelog: 60107802534SQu Wenruo btrfs_set_and_info(root, NOTREELOG, 60207802534SQu Wenruo "disabling tree log"); 6033a5e1404SSage Weil break; 604a88998f2SQu Wenruo case Opt_treelog: 60507802534SQu Wenruo btrfs_clear_and_info(root, NOTREELOG, 60607802534SQu Wenruo "enabling tree log"); 607a88998f2SQu Wenruo break; 608dccae999SSage Weil case Opt_flushoncommit: 60907802534SQu Wenruo btrfs_set_and_info(root, FLUSHONCOMMIT, 61007802534SQu Wenruo "turning on flush-on-commit"); 611dccae999SSage Weil break; 6122c9ee856SQu Wenruo case Opt_noflushoncommit: 61307802534SQu Wenruo btrfs_clear_and_info(root, FLUSHONCOMMIT, 61407802534SQu Wenruo "turning off flush-on-commit"); 6152c9ee856SQu Wenruo break; 61697e728d4SJosef Bacik case Opt_ratio: 6172c334e87SWang Shilong ret = match_int(&args[0], &intarg); 6182c334e87SWang Shilong if (ret) { 6192c334e87SWang Shilong goto out; 6202c334e87SWang Shilong } else if (intarg >= 0) { 62197e728d4SJosef Bacik info->metadata_ratio = intarg; 622efe120a0SFrank Holton btrfs_info(root->fs_info, "metadata ratio %d", 62397e728d4SJosef Bacik info->metadata_ratio); 6242c334e87SWang Shilong } else { 6252c334e87SWang Shilong ret = -EINVAL; 6262c334e87SWang Shilong goto out; 62797e728d4SJosef Bacik } 62897e728d4SJosef Bacik break; 629e244a0aeSChristoph Hellwig case Opt_discard: 63007802534SQu Wenruo btrfs_set_and_info(root, DISCARD, 63107802534SQu Wenruo "turning on discard"); 632e244a0aeSChristoph Hellwig break; 633e07a2adeSQu Wenruo case Opt_nodiscard: 63407802534SQu Wenruo btrfs_clear_and_info(root, DISCARD, 63507802534SQu Wenruo "turning off discard"); 636e07a2adeSQu Wenruo break; 6370af3d00bSJosef Bacik case Opt_space_cache: 63807802534SQu Wenruo btrfs_set_and_info(root, SPACE_CACHE, 63907802534SQu Wenruo "enabling disk space caching"); 6400de90876SJosef Bacik break; 641f420ee1eSStefan Behrens case Opt_rescan_uuid_tree: 642f420ee1eSStefan Behrens btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE); 643f420ee1eSStefan Behrens break; 64473bc1876SJosef Bacik case Opt_no_space_cache: 64507802534SQu Wenruo btrfs_clear_and_info(root, SPACE_CACHE, 64607802534SQu Wenruo "disabling disk space caching"); 64773bc1876SJosef Bacik break; 6484b9465cbSChris Mason case Opt_inode_cache: 64907802534SQu Wenruo btrfs_set_and_info(root, CHANGE_INODE_CACHE, 65007802534SQu Wenruo "enabling inode map caching"); 6513818aea2SQu Wenruo break; 6523818aea2SQu Wenruo case Opt_noinode_cache: 65307802534SQu Wenruo btrfs_clear_and_info(root, CHANGE_INODE_CACHE, 65407802534SQu Wenruo "disabling inode map caching"); 6554b9465cbSChris Mason break; 65688c2ba3bSJosef Bacik case Opt_clear_cache: 65707802534SQu Wenruo btrfs_set_and_info(root, CLEAR_CACHE, 65807802534SQu Wenruo "force clearing of disk cache"); 6590af3d00bSJosef Bacik break; 6604260f7c7SSage Weil case Opt_user_subvol_rm_allowed: 6614260f7c7SSage Weil btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 6624260f7c7SSage Weil break; 66391435650SChris Mason case Opt_enospc_debug: 66491435650SChris Mason btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 66591435650SChris Mason break; 66653036293SQu Wenruo case Opt_noenospc_debug: 66753036293SQu Wenruo btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG); 66853036293SQu Wenruo break; 6694cb5300bSChris Mason case Opt_defrag: 67007802534SQu Wenruo btrfs_set_and_info(root, AUTO_DEFRAG, 67107802534SQu Wenruo "enabling auto defrag"); 6724cb5300bSChris Mason break; 673fc0ca9afSQu Wenruo case Opt_nodefrag: 67407802534SQu Wenruo btrfs_clear_and_info(root, AUTO_DEFRAG, 67507802534SQu Wenruo "disabling auto defrag"); 676fc0ca9afSQu Wenruo break; 677af31f5e5SChris Mason case Opt_recovery: 678efe120a0SFrank Holton btrfs_info(root->fs_info, "enabling auto recovery"); 679af31f5e5SChris Mason btrfs_set_opt(info->mount_opt, RECOVERY); 680af31f5e5SChris Mason break; 6819555c6c1SIlya Dryomov case Opt_skip_balance: 6829555c6c1SIlya Dryomov btrfs_set_opt(info->mount_opt, SKIP_BALANCE); 6839555c6c1SIlya Dryomov break; 68421adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 68521adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 686efe120a0SFrank Holton btrfs_info(root->fs_info, 687efe120a0SFrank Holton "enabling check integrity including extent data"); 68821adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, 68921adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA); 69021adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 69121adbd5cSStefan Behrens break; 69221adbd5cSStefan Behrens case Opt_check_integrity: 693efe120a0SFrank Holton btrfs_info(root->fs_info, "enabling check integrity"); 69421adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 69521adbd5cSStefan Behrens break; 69621adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 6972c334e87SWang Shilong ret = match_int(&args[0], &intarg); 6982c334e87SWang Shilong if (ret) { 6992c334e87SWang Shilong goto out; 7002c334e87SWang Shilong } else if (intarg >= 0) { 70121adbd5cSStefan Behrens info->check_integrity_print_mask = intarg; 702efe120a0SFrank Holton btrfs_info(root->fs_info, "check_integrity_print_mask 0x%x", 70321adbd5cSStefan Behrens info->check_integrity_print_mask); 7042c334e87SWang Shilong } else { 7052c334e87SWang Shilong ret = -EINVAL; 7062c334e87SWang Shilong goto out; 70721adbd5cSStefan Behrens } 70821adbd5cSStefan Behrens break; 70921adbd5cSStefan Behrens #else 71021adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 71121adbd5cSStefan Behrens case Opt_check_integrity: 71221adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 713efe120a0SFrank Holton btrfs_err(root->fs_info, 714efe120a0SFrank Holton "support for check_integrity* not compiled in!"); 71521adbd5cSStefan Behrens ret = -EINVAL; 71621adbd5cSStefan Behrens goto out; 71721adbd5cSStefan Behrens #endif 7188c342930SJeff Mahoney case Opt_fatal_errors: 7198c342930SJeff Mahoney if (strcmp(args[0].from, "panic") == 0) 7208c342930SJeff Mahoney btrfs_set_opt(info->mount_opt, 7218c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7228c342930SJeff Mahoney else if (strcmp(args[0].from, "bug") == 0) 7238c342930SJeff Mahoney btrfs_clear_opt(info->mount_opt, 7248c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7258c342930SJeff Mahoney else { 7268c342930SJeff Mahoney ret = -EINVAL; 7278c342930SJeff Mahoney goto out; 7288c342930SJeff Mahoney } 7298c342930SJeff Mahoney break; 7308b87dc17SDavid Sterba case Opt_commit_interval: 7318b87dc17SDavid Sterba intarg = 0; 7328b87dc17SDavid Sterba ret = match_int(&args[0], &intarg); 7338b87dc17SDavid Sterba if (ret < 0) { 734efe120a0SFrank Holton btrfs_err(root->fs_info, "invalid commit interval"); 7358b87dc17SDavid Sterba ret = -EINVAL; 7368b87dc17SDavid Sterba goto out; 7378b87dc17SDavid Sterba } 7388b87dc17SDavid Sterba if (intarg > 0) { 7398b87dc17SDavid Sterba if (intarg > 300) { 740efe120a0SFrank Holton btrfs_warn(root->fs_info, "excessive commit interval %d", 7418b87dc17SDavid Sterba intarg); 7428b87dc17SDavid Sterba } 7438b87dc17SDavid Sterba info->commit_interval = intarg; 7448b87dc17SDavid Sterba } else { 745efe120a0SFrank Holton btrfs_info(root->fs_info, "using default commit interval %ds", 7468b87dc17SDavid Sterba BTRFS_DEFAULT_COMMIT_INTERVAL); 7478b87dc17SDavid Sterba info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 7488b87dc17SDavid Sterba } 7498b87dc17SDavid Sterba break; 750a7a3f7caSSage Weil case Opt_err: 751efe120a0SFrank Holton btrfs_info(root->fs_info, "unrecognized mount option '%s'", p); 752a7a3f7caSSage Weil ret = -EINVAL; 753a7a3f7caSSage Weil goto out; 75495e05289SChris Mason default: 755be20aa9dSChris Mason break; 75695e05289SChris Mason } 75795e05289SChris Mason } 758a7a3f7caSSage Weil out: 75973bc1876SJosef Bacik if (!ret && btrfs_test_opt(root, SPACE_CACHE)) 760efe120a0SFrank Holton btrfs_info(root->fs_info, "disk space caching is enabled"); 761da495eccSJosef Bacik kfree(orig); 762a7a3f7caSSage Weil return ret; 763edf24abeSChristoph Hellwig } 764edf24abeSChristoph Hellwig 765edf24abeSChristoph Hellwig /* 766edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 767edf24abeSChristoph Hellwig * 768edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 769edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 770edf24abeSChristoph Hellwig */ 77197288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 77273f73415SJosef Bacik void *holder, char **subvol_name, u64 *subvol_objectid, 7735e2a4b25SDavid Sterba struct btrfs_fs_devices **fs_devices) 774edf24abeSChristoph Hellwig { 775edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 77683c8c9bdSJeff Liu char *device_name, *opts, *orig, *p; 7771493381fSWang Shilong char *num = NULL; 778edf24abeSChristoph Hellwig int error = 0; 779edf24abeSChristoph Hellwig 780edf24abeSChristoph Hellwig if (!options) 781830c4adbSJosef Bacik return 0; 782edf24abeSChristoph Hellwig 783edf24abeSChristoph Hellwig /* 784edf24abeSChristoph Hellwig * strsep changes the string, duplicate it because parse_options 785edf24abeSChristoph Hellwig * gets called twice 786edf24abeSChristoph Hellwig */ 787edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 788edf24abeSChristoph Hellwig if (!opts) 789edf24abeSChristoph Hellwig return -ENOMEM; 7903f3d0bc0STero Roponen orig = opts; 791edf24abeSChristoph Hellwig 792edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 793edf24abeSChristoph Hellwig int token; 794edf24abeSChristoph Hellwig if (!*p) 795edf24abeSChristoph Hellwig continue; 796edf24abeSChristoph Hellwig 797edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 798edf24abeSChristoph Hellwig switch (token) { 799edf24abeSChristoph Hellwig case Opt_subvol: 800a90e8b6fSIlya Dryomov kfree(*subvol_name); 801edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 8022c334e87SWang Shilong if (!*subvol_name) { 8032c334e87SWang Shilong error = -ENOMEM; 8042c334e87SWang Shilong goto out; 8052c334e87SWang Shilong } 806edf24abeSChristoph Hellwig break; 80773f73415SJosef Bacik case Opt_subvolid: 8081493381fSWang Shilong num = match_strdup(&args[0]); 8091493381fSWang Shilong if (num) { 8101493381fSWang Shilong *subvol_objectid = memparse(num, NULL); 8111493381fSWang Shilong kfree(num); 8124849f01dSJosef Bacik /* we want the original fs_tree */ 8131493381fSWang Shilong if (!*subvol_objectid) 8144849f01dSJosef Bacik *subvol_objectid = 8154849f01dSJosef Bacik BTRFS_FS_TREE_OBJECTID; 8162c334e87SWang Shilong } else { 8172c334e87SWang Shilong error = -EINVAL; 8182c334e87SWang Shilong goto out; 8194849f01dSJosef Bacik } 82073f73415SJosef Bacik break; 821e15d0542SXin Zhong case Opt_subvolrootid: 8225e2a4b25SDavid Sterba printk(KERN_WARNING 823efe120a0SFrank Holton "BTRFS: 'subvolrootid' mount option is deprecated and has " 824efe120a0SFrank Holton "no effect\n"); 825e15d0542SXin Zhong break; 82643e570b0SChristoph Hellwig case Opt_device: 82783c8c9bdSJeff Liu device_name = match_strdup(&args[0]); 82883c8c9bdSJeff Liu if (!device_name) { 82983c8c9bdSJeff Liu error = -ENOMEM; 83083c8c9bdSJeff Liu goto out; 83183c8c9bdSJeff Liu } 83283c8c9bdSJeff Liu error = btrfs_scan_one_device(device_name, 83343e570b0SChristoph Hellwig flags, holder, fs_devices); 83483c8c9bdSJeff Liu kfree(device_name); 83543e570b0SChristoph Hellwig if (error) 836830c4adbSJosef Bacik goto out; 83743e570b0SChristoph Hellwig break; 838edf24abeSChristoph Hellwig default: 839edf24abeSChristoph Hellwig break; 840edf24abeSChristoph Hellwig } 841edf24abeSChristoph Hellwig } 842edf24abeSChristoph Hellwig 843edf24abeSChristoph Hellwig out: 844830c4adbSJosef Bacik kfree(orig); 845edf24abeSChristoph Hellwig return error; 84695e05289SChris Mason } 84795e05289SChris Mason 84873f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb, 84973f73415SJosef Bacik u64 subvol_objectid) 85073f73415SJosef Bacik { 851815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 852815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 85373f73415SJosef Bacik struct btrfs_root *new_root; 85473f73415SJosef Bacik struct btrfs_dir_item *di; 85573f73415SJosef Bacik struct btrfs_path *path; 85673f73415SJosef Bacik struct btrfs_key location; 85773f73415SJosef Bacik struct inode *inode; 8583a0dfa6aSJosef Bacik struct dentry *dentry; 85973f73415SJosef Bacik u64 dir_id; 86073f73415SJosef Bacik int new = 0; 86173f73415SJosef Bacik 86273f73415SJosef Bacik /* 86373f73415SJosef Bacik * We have a specific subvol we want to mount, just setup location and 86473f73415SJosef Bacik * go look up the root. 86573f73415SJosef Bacik */ 86673f73415SJosef Bacik if (subvol_objectid) { 86773f73415SJosef Bacik location.objectid = subvol_objectid; 86873f73415SJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 86973f73415SJosef Bacik location.offset = (u64)-1; 87073f73415SJosef Bacik goto find_root; 87173f73415SJosef Bacik } 87273f73415SJosef Bacik 87373f73415SJosef Bacik path = btrfs_alloc_path(); 87473f73415SJosef Bacik if (!path) 87573f73415SJosef Bacik return ERR_PTR(-ENOMEM); 87673f73415SJosef Bacik path->leave_spinning = 1; 87773f73415SJosef Bacik 87873f73415SJosef Bacik /* 87973f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 88073f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 88173f73415SJosef Bacik * to mount. 88273f73415SJosef Bacik */ 883815745cfSAl Viro dir_id = btrfs_super_root_dir(fs_info->super_copy); 88473f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 885b0839166SJulia Lawall if (IS_ERR(di)) { 886b0839166SJulia Lawall btrfs_free_path(path); 887fb4f6f91SDan Carpenter return ERR_CAST(di); 888b0839166SJulia Lawall } 88973f73415SJosef Bacik if (!di) { 89073f73415SJosef Bacik /* 89173f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 89273f73415SJosef Bacik * it's always been there, but don't freak out, just try and 89373f73415SJosef Bacik * mount to root most subvolume. 89473f73415SJosef Bacik */ 89573f73415SJosef Bacik btrfs_free_path(path); 89673f73415SJosef Bacik dir_id = BTRFS_FIRST_FREE_OBJECTID; 897815745cfSAl Viro new_root = fs_info->fs_root; 89873f73415SJosef Bacik goto setup_root; 89973f73415SJosef Bacik } 90073f73415SJosef Bacik 90173f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 90273f73415SJosef Bacik btrfs_free_path(path); 90373f73415SJosef Bacik 90473f73415SJosef Bacik find_root: 905815745cfSAl Viro new_root = btrfs_read_fs_root_no_name(fs_info, &location); 90673f73415SJosef Bacik if (IS_ERR(new_root)) 907d0b678cbSJulia Lawall return ERR_CAST(new_root); 90873f73415SJosef Bacik 90973f73415SJosef Bacik dir_id = btrfs_root_dirid(&new_root->root_item); 91073f73415SJosef Bacik setup_root: 91173f73415SJosef Bacik location.objectid = dir_id; 91273f73415SJosef Bacik location.type = BTRFS_INODE_ITEM_KEY; 91373f73415SJosef Bacik location.offset = 0; 91473f73415SJosef Bacik 91573f73415SJosef Bacik inode = btrfs_iget(sb, &location, new_root, &new); 9164cbd1149SDan Carpenter if (IS_ERR(inode)) 9174cbd1149SDan Carpenter return ERR_CAST(inode); 91873f73415SJosef Bacik 91973f73415SJosef Bacik /* 92073f73415SJosef Bacik * If we're just mounting the root most subvol put the inode and return 92173f73415SJosef Bacik * a reference to the dentry. We will have already gotten a reference 92273f73415SJosef Bacik * to the inode in btrfs_fill_super so we're good to go. 92373f73415SJosef Bacik */ 92473f73415SJosef Bacik if (!new && sb->s_root->d_inode == inode) { 92573f73415SJosef Bacik iput(inode); 92673f73415SJosef Bacik return dget(sb->s_root); 92773f73415SJosef Bacik } 92873f73415SJosef Bacik 9293a0dfa6aSJosef Bacik dentry = d_obtain_alias(inode); 9303a0dfa6aSJosef Bacik if (!IS_ERR(dentry)) { 9313a0dfa6aSJosef Bacik spin_lock(&dentry->d_lock); 9323a0dfa6aSJosef Bacik dentry->d_flags &= ~DCACHE_DISCONNECTED; 9333a0dfa6aSJosef Bacik spin_unlock(&dentry->d_lock); 9343a0dfa6aSJosef Bacik } 9353a0dfa6aSJosef Bacik return dentry; 93673f73415SJosef Bacik } 93773f73415SJosef Bacik 9388a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 9398a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 9408a4b83ccSChris Mason void *data, int silent) 9412e635a27SChris Mason { 9422e635a27SChris Mason struct inode *inode; 943815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 9445d4f98a2SYan Zheng struct btrfs_key key; 94539279cc3SChris Mason int err; 9462e635a27SChris Mason 9472e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 9482e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 949e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 950af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 951be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 9525103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 9532e635a27SChris Mason sb->s_time_gran = 1; 9540eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 95533268eafSJosef Bacik sb->s_flags |= MS_POSIXACL; 95649cf6f45SChris Ball #endif 9570c4d2d95SJosef Bacik sb->s_flags |= MS_I_VERSION; 958ad2b2c80SAl Viro err = open_ctree(sb, fs_devices, (char *)data); 959ad2b2c80SAl Viro if (err) { 960efe120a0SFrank Holton printk(KERN_ERR "BTRFS: open_ctree failed\n"); 961ad2b2c80SAl Viro return err; 962e20d96d6SChris Mason } 963b888db2bSChris Mason 9645d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 9655d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 9665d4f98a2SYan Zheng key.offset = 0; 96798c7089cSAl Viro inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL); 9685d4f98a2SYan Zheng if (IS_ERR(inode)) { 9695d4f98a2SYan Zheng err = PTR_ERR(inode); 97039279cc3SChris Mason goto fail_close; 97139279cc3SChris Mason } 9722e635a27SChris Mason 97348fde701SAl Viro sb->s_root = d_make_root(inode); 97448fde701SAl Viro if (!sb->s_root) { 97539279cc3SChris Mason err = -ENOMEM; 97639279cc3SChris Mason goto fail_close; 9772e635a27SChris Mason } 97858176a96SJosef Bacik 9796885f308SChris Mason save_mount_options(sb, data); 98090a887c9SDan Magenheimer cleancache_init_fs(sb); 98159553edfSAl Viro sb->s_flags |= MS_ACTIVE; 9822e635a27SChris Mason return 0; 9832e635a27SChris Mason 98439279cc3SChris Mason fail_close: 985815745cfSAl Viro close_ctree(fs_info->tree_root); 986d5719762SChris Mason return err; 987d5719762SChris Mason } 988d5719762SChris Mason 9896bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 990d5719762SChris Mason { 991d5719762SChris Mason struct btrfs_trans_handle *trans; 992815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 993815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 994df2ce34cSChris Mason 9951abe9b8aSliubo trace_btrfs_sync_fs(wait); 9961abe9b8aSliubo 997d561c025SChris Mason if (!wait) { 998815745cfSAl Viro filemap_flush(fs_info->btree_inode->i_mapping); 999df2ce34cSChris Mason return 0; 1000d561c025SChris Mason } 1001771ed689SChris Mason 1002b0244199SMiao Xie btrfs_wait_ordered_roots(fs_info, -1); 1003771ed689SChris Mason 1004d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 100560376ce4SJosef Bacik if (IS_ERR(trans)) { 1006354aa0fbSMiao Xie /* no transaction, don't bother */ 1007354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 1008bd7de2c9SJosef Bacik return 0; 100998d5dc13STsutomu Itoh return PTR_ERR(trans); 101060376ce4SJosef Bacik } 1011bd7de2c9SJosef Bacik return btrfs_commit_transaction(trans, root); 1012d5719762SChris Mason } 1013d5719762SChris Mason 101434c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 1015a9572a15SEric Paris { 1016815745cfSAl Viro struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 1017815745cfSAl Viro struct btrfs_root *root = info->tree_root; 1018200da64eSTsutomu Itoh char *compress_type; 1019a9572a15SEric Paris 1020a9572a15SEric Paris if (btrfs_test_opt(root, DEGRADED)) 1021a9572a15SEric Paris seq_puts(seq, ",degraded"); 1022a9572a15SEric Paris if (btrfs_test_opt(root, NODATASUM)) 1023a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 1024a9572a15SEric Paris if (btrfs_test_opt(root, NODATACOW)) 1025a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 1026a9572a15SEric Paris if (btrfs_test_opt(root, NOBARRIER)) 1027a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 1028a9572a15SEric Paris if (info->max_inline != 8192 * 1024) 1029c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",max_inline=%llu", info->max_inline); 1030a9572a15SEric Paris if (info->alloc_start != 0) 1031c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",alloc_start=%llu", info->alloc_start); 1032a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 1033a9572a15SEric Paris num_online_cpus() + 2, 8)) 1034a9572a15SEric Paris seq_printf(seq, ",thread_pool=%d", info->thread_pool_size); 1035200da64eSTsutomu Itoh if (btrfs_test_opt(root, COMPRESS)) { 1036200da64eSTsutomu Itoh if (info->compress_type == BTRFS_COMPRESS_ZLIB) 1037200da64eSTsutomu Itoh compress_type = "zlib"; 1038200da64eSTsutomu Itoh else 1039200da64eSTsutomu Itoh compress_type = "lzo"; 1040200da64eSTsutomu Itoh if (btrfs_test_opt(root, FORCE_COMPRESS)) 1041200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 1042200da64eSTsutomu Itoh else 1043200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 1044200da64eSTsutomu Itoh } 1045c289811cSChris Mason if (btrfs_test_opt(root, NOSSD)) 1046c289811cSChris Mason seq_puts(seq, ",nossd"); 1047451d7585SChris Mason if (btrfs_test_opt(root, SSD_SPREAD)) 1048451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 1049451d7585SChris Mason else if (btrfs_test_opt(root, SSD)) 1050a9572a15SEric Paris seq_puts(seq, ",ssd"); 10513a5e1404SSage Weil if (btrfs_test_opt(root, NOTREELOG)) 10526b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 1053dccae999SSage Weil if (btrfs_test_opt(root, FLUSHONCOMMIT)) 10546b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 105520a5239aSMatthew Wilcox if (btrfs_test_opt(root, DISCARD)) 105620a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 1057a9572a15SEric Paris if (!(root->fs_info->sb->s_flags & MS_POSIXACL)) 1058a9572a15SEric Paris seq_puts(seq, ",noacl"); 1059200da64eSTsutomu Itoh if (btrfs_test_opt(root, SPACE_CACHE)) 1060200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 106173bc1876SJosef Bacik else 10628965593eSDavid Sterba seq_puts(seq, ",nospace_cache"); 1063f420ee1eSStefan Behrens if (btrfs_test_opt(root, RESCAN_UUID_TREE)) 1064f420ee1eSStefan Behrens seq_puts(seq, ",rescan_uuid_tree"); 1065200da64eSTsutomu Itoh if (btrfs_test_opt(root, CLEAR_CACHE)) 1066200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 1067200da64eSTsutomu Itoh if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 1068200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 10690942caa3SDavid Sterba if (btrfs_test_opt(root, ENOSPC_DEBUG)) 10700942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 10710942caa3SDavid Sterba if (btrfs_test_opt(root, AUTO_DEFRAG)) 10720942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 10730942caa3SDavid Sterba if (btrfs_test_opt(root, INODE_MAP_CACHE)) 10740942caa3SDavid Sterba seq_puts(seq, ",inode_cache"); 10759555c6c1SIlya Dryomov if (btrfs_test_opt(root, SKIP_BALANCE)) 10769555c6c1SIlya Dryomov seq_puts(seq, ",skip_balance"); 10778507d216SWang Shilong if (btrfs_test_opt(root, RECOVERY)) 10788507d216SWang Shilong seq_puts(seq, ",recovery"); 10798507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 10808507d216SWang Shilong if (btrfs_test_opt(root, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA)) 10818507d216SWang Shilong seq_puts(seq, ",check_int_data"); 10828507d216SWang Shilong else if (btrfs_test_opt(root, CHECK_INTEGRITY)) 10838507d216SWang Shilong seq_puts(seq, ",check_int"); 10848507d216SWang Shilong if (info->check_integrity_print_mask) 10858507d216SWang Shilong seq_printf(seq, ",check_int_print_mask=%d", 10868507d216SWang Shilong info->check_integrity_print_mask); 10878507d216SWang Shilong #endif 10888507d216SWang Shilong if (info->metadata_ratio) 10898507d216SWang Shilong seq_printf(seq, ",metadata_ratio=%d", 10908507d216SWang Shilong info->metadata_ratio); 10918c342930SJeff Mahoney if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR)) 10928c342930SJeff Mahoney seq_puts(seq, ",fatal_errors=panic"); 10938b87dc17SDavid Sterba if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL) 10948b87dc17SDavid Sterba seq_printf(seq, ",commit=%d", info->commit_interval); 1095a9572a15SEric Paris return 0; 1096a9572a15SEric Paris } 1097a9572a15SEric Paris 1098a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 10992e635a27SChris Mason { 1100815745cfSAl Viro struct btrfs_fs_info *p = data; 1101815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(s); 11024b82d6e4SYan 1103815745cfSAl Viro return fs_info->fs_devices == p->fs_devices; 11044b82d6e4SYan } 11054b82d6e4SYan 1106450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 1107450ba0eaSJosef Bacik { 11086de1d09dSAl Viro int err = set_anon_super(s, data); 11096de1d09dSAl Viro if (!err) 1110450ba0eaSJosef Bacik s->s_fs_info = data; 11116de1d09dSAl Viro return err; 1112450ba0eaSJosef Bacik } 1113450ba0eaSJosef Bacik 1114830c4adbSJosef Bacik /* 1115f9d9ef62SDavid Sterba * subvolumes are identified by ino 256 1116f9d9ef62SDavid Sterba */ 1117f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode) 1118f9d9ef62SDavid Sterba { 1119f9d9ef62SDavid Sterba if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 1120f9d9ef62SDavid Sterba return 1; 1121f9d9ef62SDavid Sterba return 0; 1122f9d9ef62SDavid Sterba } 1123f9d9ef62SDavid Sterba 1124f9d9ef62SDavid Sterba /* 1125830c4adbSJosef Bacik * This will strip out the subvol=%s argument for an argument string and add 1126830c4adbSJosef Bacik * subvolid=0 to make sure we get the actual tree root for path walking to the 1127830c4adbSJosef Bacik * subvol we want. 1128830c4adbSJosef Bacik */ 1129830c4adbSJosef Bacik static char *setup_root_args(char *args) 1130830c4adbSJosef Bacik { 1131f60d16a8SJim Meyering unsigned len = strlen(args) + 2 + 1; 1132f60d16a8SJim Meyering char *src, *dst, *buf; 1133830c4adbSJosef Bacik 1134830c4adbSJosef Bacik /* 1135f60d16a8SJim Meyering * We need the same args as before, but with this substitution: 1136f60d16a8SJim Meyering * s!subvol=[^,]+!subvolid=0! 1137830c4adbSJosef Bacik * 1138f60d16a8SJim Meyering * Since the replacement string is up to 2 bytes longer than the 1139f60d16a8SJim Meyering * original, allocate strlen(args) + 2 + 1 bytes. 1140830c4adbSJosef Bacik */ 1141830c4adbSJosef Bacik 1142f60d16a8SJim Meyering src = strstr(args, "subvol="); 1143830c4adbSJosef Bacik /* This shouldn't happen, but just in case.. */ 1144f60d16a8SJim Meyering if (!src) 1145830c4adbSJosef Bacik return NULL; 1146f60d16a8SJim Meyering 1147f60d16a8SJim Meyering buf = dst = kmalloc(len, GFP_NOFS); 1148f60d16a8SJim Meyering if (!buf) 1149f60d16a8SJim Meyering return NULL; 1150830c4adbSJosef Bacik 1151830c4adbSJosef Bacik /* 1152f60d16a8SJim Meyering * If the subvol= arg is not at the start of the string, 1153f60d16a8SJim Meyering * copy whatever precedes it into buf. 1154830c4adbSJosef Bacik */ 1155f60d16a8SJim Meyering if (src != args) { 1156f60d16a8SJim Meyering *src++ = '\0'; 1157f60d16a8SJim Meyering strcpy(buf, args); 1158f60d16a8SJim Meyering dst += strlen(args); 1159830c4adbSJosef Bacik } 1160830c4adbSJosef Bacik 1161f60d16a8SJim Meyering strcpy(dst, "subvolid=0"); 1162f60d16a8SJim Meyering dst += strlen("subvolid=0"); 1163830c4adbSJosef Bacik 1164830c4adbSJosef Bacik /* 1165f60d16a8SJim Meyering * If there is a "," after the original subvol=... string, 1166f60d16a8SJim Meyering * copy that suffix into our buffer. Otherwise, we're done. 1167830c4adbSJosef Bacik */ 1168f60d16a8SJim Meyering src = strchr(src, ','); 1169f60d16a8SJim Meyering if (src) 1170f60d16a8SJim Meyering strcpy(dst, src); 1171830c4adbSJosef Bacik 1172f60d16a8SJim Meyering return buf; 1173830c4adbSJosef Bacik } 1174830c4adbSJosef Bacik 1175830c4adbSJosef Bacik static struct dentry *mount_subvol(const char *subvol_name, int flags, 1176830c4adbSJosef Bacik const char *device_name, char *data) 1177830c4adbSJosef Bacik { 1178830c4adbSJosef Bacik struct dentry *root; 1179830c4adbSJosef Bacik struct vfsmount *mnt; 1180830c4adbSJosef Bacik char *newargs; 1181830c4adbSJosef Bacik 1182830c4adbSJosef Bacik newargs = setup_root_args(data); 1183830c4adbSJosef Bacik if (!newargs) 1184830c4adbSJosef Bacik return ERR_PTR(-ENOMEM); 1185830c4adbSJosef Bacik mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, 1186830c4adbSJosef Bacik newargs); 1187830c4adbSJosef Bacik kfree(newargs); 1188830c4adbSJosef Bacik if (IS_ERR(mnt)) 1189830c4adbSJosef Bacik return ERR_CAST(mnt); 1190830c4adbSJosef Bacik 1191ea441d11SAl Viro root = mount_subtree(mnt, subvol_name); 1192830c4adbSJosef Bacik 1193ea441d11SAl Viro if (!IS_ERR(root) && !is_subvolume_inode(root->d_inode)) { 1194ea441d11SAl Viro struct super_block *s = root->d_sb; 1195ea441d11SAl Viro dput(root); 1196ea441d11SAl Viro root = ERR_PTR(-EINVAL); 1197ea441d11SAl Viro deactivate_locked_super(s); 1198efe120a0SFrank Holton printk(KERN_ERR "BTRFS: '%s' is not a valid subvolume\n", 1199f9d9ef62SDavid Sterba subvol_name); 1200f9d9ef62SDavid Sterba } 1201f9d9ef62SDavid Sterba 1202830c4adbSJosef Bacik return root; 1203830c4adbSJosef Bacik } 1204450ba0eaSJosef Bacik 1205edf24abeSChristoph Hellwig /* 1206edf24abeSChristoph Hellwig * Find a superblock for the given device / mount point. 1207edf24abeSChristoph Hellwig * 1208edf24abeSChristoph Hellwig * Note: This is based on get_sb_bdev from fs/super.c with a few additions 1209edf24abeSChristoph Hellwig * for multiple device setup. Make sure to keep it in sync. 1210edf24abeSChristoph Hellwig */ 1211061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1212306e16ceSDavid Sterba const char *device_name, void *data) 12134b82d6e4SYan { 12144b82d6e4SYan struct block_device *bdev = NULL; 12154b82d6e4SYan struct super_block *s; 12164b82d6e4SYan struct dentry *root; 12178a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices = NULL; 1218450ba0eaSJosef Bacik struct btrfs_fs_info *fs_info = NULL; 121997288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 122073f73415SJosef Bacik char *subvol_name = NULL; 122173f73415SJosef Bacik u64 subvol_objectid = 0; 12224b82d6e4SYan int error = 0; 12234b82d6e4SYan 122497288f2cSChristoph Hellwig if (!(flags & MS_RDONLY)) 122597288f2cSChristoph Hellwig mode |= FMODE_WRITE; 122697288f2cSChristoph Hellwig 122797288f2cSChristoph Hellwig error = btrfs_parse_early_options(data, mode, fs_type, 122873f73415SJosef Bacik &subvol_name, &subvol_objectid, 12295e2a4b25SDavid Sterba &fs_devices); 1230f23c8af8SIlya Dryomov if (error) { 1231f23c8af8SIlya Dryomov kfree(subvol_name); 1232061dbc6bSAl Viro return ERR_PTR(error); 1233f23c8af8SIlya Dryomov } 1234edf24abeSChristoph Hellwig 1235830c4adbSJosef Bacik if (subvol_name) { 1236830c4adbSJosef Bacik root = mount_subvol(subvol_name, flags, device_name, data); 1237830c4adbSJosef Bacik kfree(subvol_name); 1238830c4adbSJosef Bacik return root; 1239830c4adbSJosef Bacik } 1240830c4adbSJosef Bacik 1241306e16ceSDavid Sterba error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices); 12428a4b83ccSChris Mason if (error) 1243830c4adbSJosef Bacik return ERR_PTR(error); 12444b82d6e4SYan 1245450ba0eaSJosef Bacik /* 1246450ba0eaSJosef Bacik * Setup a dummy root and fs_info for test/set super. This is because 1247450ba0eaSJosef Bacik * we don't actually fill this stuff out until open_ctree, but we need 1248450ba0eaSJosef Bacik * it for searching for existing supers, so this lets us do that and 1249450ba0eaSJosef Bacik * then open_ctree will properly initialize everything later. 1250450ba0eaSJosef Bacik */ 1251450ba0eaSJosef Bacik fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS); 125204d21a24SIlya Dryomov if (!fs_info) 125304d21a24SIlya Dryomov return ERR_PTR(-ENOMEM); 125404d21a24SIlya Dryomov 1255450ba0eaSJosef Bacik fs_info->fs_devices = fs_devices; 1256450ba0eaSJosef Bacik 12576c41761fSDavid Sterba fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 12586c41761fSDavid Sterba fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 12596c41761fSDavid Sterba if (!fs_info->super_copy || !fs_info->super_for_commit) { 12606c41761fSDavid Sterba error = -ENOMEM; 126104d21a24SIlya Dryomov goto error_fs_info; 126204d21a24SIlya Dryomov } 126304d21a24SIlya Dryomov 126404d21a24SIlya Dryomov error = btrfs_open_devices(fs_devices, mode, fs_type); 126504d21a24SIlya Dryomov if (error) 126604d21a24SIlya Dryomov goto error_fs_info; 126704d21a24SIlya Dryomov 126804d21a24SIlya Dryomov if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) { 126904d21a24SIlya Dryomov error = -EACCES; 12706c41761fSDavid Sterba goto error_close_devices; 12716c41761fSDavid Sterba } 12726c41761fSDavid Sterba 1273dfe25020SChris Mason bdev = fs_devices->latest_bdev; 12749249e17fSDavid Howells s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC, 12759249e17fSDavid Howells fs_info); 1276830c4adbSJosef Bacik if (IS_ERR(s)) { 1277830c4adbSJosef Bacik error = PTR_ERR(s); 1278830c4adbSJosef Bacik goto error_close_devices; 1279830c4adbSJosef Bacik } 12804b82d6e4SYan 12814b82d6e4SYan if (s->s_root) { 12822b82032cSYan Zheng btrfs_close_devices(fs_devices); 12836c41761fSDavid Sterba free_fs_info(fs_info); 128459553edfSAl Viro if ((flags ^ s->s_flags) & MS_RDONLY) 128559553edfSAl Viro error = -EBUSY; 12864b82d6e4SYan } else { 12874b82d6e4SYan char b[BDEVNAME_SIZE]; 12884b82d6e4SYan 12894b82d6e4SYan strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); 1290815745cfSAl Viro btrfs_sb(s)->bdev_holder = fs_type; 12918a4b83ccSChris Mason error = btrfs_fill_super(s, fs_devices, data, 12928a4b83ccSChris Mason flags & MS_SILENT ? 1 : 0); 12934b82d6e4SYan } 12944b82d6e4SYan 129559553edfSAl Viro root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error); 129659553edfSAl Viro if (IS_ERR(root)) 1297e15d0542SXin Zhong deactivate_locked_super(s); 12984b82d6e4SYan 1299061dbc6bSAl Viro return root; 13004b82d6e4SYan 1301c146afadSYan Zheng error_close_devices: 13028a4b83ccSChris Mason btrfs_close_devices(fs_devices); 130304d21a24SIlya Dryomov error_fs_info: 13046c41761fSDavid Sterba free_fs_info(fs_info); 1305061dbc6bSAl Viro return ERR_PTR(error); 13064b82d6e4SYan } 13072e635a27SChris Mason 13080d2450abSSergei Trofimovich static void btrfs_set_max_workers(struct btrfs_workers *workers, int new_limit) 13090d2450abSSergei Trofimovich { 13100d2450abSSergei Trofimovich spin_lock_irq(&workers->lock); 13110d2450abSSergei Trofimovich workers->max_workers = new_limit; 13120d2450abSSergei Trofimovich spin_unlock_irq(&workers->lock); 13130d2450abSSergei Trofimovich } 13140d2450abSSergei Trofimovich 13150d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 13160d2450abSSergei Trofimovich int new_pool_size, int old_pool_size) 13170d2450abSSergei Trofimovich { 13180d2450abSSergei Trofimovich if (new_pool_size == old_pool_size) 13190d2450abSSergei Trofimovich return; 13200d2450abSSergei Trofimovich 13210d2450abSSergei Trofimovich fs_info->thread_pool_size = new_pool_size; 13220d2450abSSergei Trofimovich 1323efe120a0SFrank Holton btrfs_info(fs_info, "resize thread pool %d -> %d", 13240d2450abSSergei Trofimovich old_pool_size, new_pool_size); 13250d2450abSSergei Trofimovich 13260d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->generic_worker, new_pool_size); 13275cdc7ad3SQu Wenruo btrfs_workqueue_set_max(fs_info->workers, new_pool_size); 1328afe3d242SQu Wenruo btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size); 1329a8c93d4eSQu Wenruo btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size); 1330*e66f0bb1SQu Wenruo btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size); 13310d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->fixup_workers, new_pool_size); 1332fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size); 1333fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size); 1334fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_write_workers, 1335fccb5d86SQu Wenruo new_pool_size); 1336fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size); 1337fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size); 13380d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->delayed_workers, new_pool_size); 13390d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->readahead_workers, new_pool_size); 1340ff023aacSStefan Behrens btrfs_set_max_workers(&fs_info->scrub_wr_completion_workers, 1341ff023aacSStefan Behrens new_pool_size); 13420d2450abSSergei Trofimovich } 13430d2450abSSergei Trofimovich 1344f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info) 1345dc81cdc5SMiao Xie { 1346dc81cdc5SMiao Xie set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1347f42a34b2SMiao Xie } 1348dc81cdc5SMiao Xie 1349f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info, 1350f42a34b2SMiao Xie unsigned long old_opts, int flags) 1351f42a34b2SMiao Xie { 1352dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1353dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1354dc81cdc5SMiao Xie (flags & MS_RDONLY))) { 1355dc81cdc5SMiao Xie /* wait for any defraggers to finish */ 1356dc81cdc5SMiao Xie wait_event(fs_info->transaction_wait, 1357dc81cdc5SMiao Xie (atomic_read(&fs_info->defrag_running) == 0)); 1358dc81cdc5SMiao Xie if (flags & MS_RDONLY) 1359dc81cdc5SMiao Xie sync_filesystem(fs_info->sb); 1360dc81cdc5SMiao Xie } 1361dc81cdc5SMiao Xie } 1362dc81cdc5SMiao Xie 1363dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info, 1364dc81cdc5SMiao Xie unsigned long old_opts) 1365dc81cdc5SMiao Xie { 1366dc81cdc5SMiao Xie /* 1367dc81cdc5SMiao Xie * We need cleanup all defragable inodes if the autodefragment is 1368dc81cdc5SMiao Xie * close or the fs is R/O. 1369dc81cdc5SMiao Xie */ 1370dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1371dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1372dc81cdc5SMiao Xie (fs_info->sb->s_flags & MS_RDONLY))) { 1373dc81cdc5SMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 1374dc81cdc5SMiao Xie } 1375dc81cdc5SMiao Xie 1376dc81cdc5SMiao Xie clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1377dc81cdc5SMiao Xie } 1378dc81cdc5SMiao Xie 1379c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1380c146afadSYan Zheng { 1381815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1382815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 138349b25e05SJeff Mahoney unsigned old_flags = sb->s_flags; 138449b25e05SJeff Mahoney unsigned long old_opts = fs_info->mount_opt; 138549b25e05SJeff Mahoney unsigned long old_compress_type = fs_info->compress_type; 138649b25e05SJeff Mahoney u64 old_max_inline = fs_info->max_inline; 138749b25e05SJeff Mahoney u64 old_alloc_start = fs_info->alloc_start; 138849b25e05SJeff Mahoney int old_thread_pool_size = fs_info->thread_pool_size; 138949b25e05SJeff Mahoney unsigned int old_metadata_ratio = fs_info->metadata_ratio; 1390c146afadSYan Zheng int ret; 1391c146afadSYan Zheng 1392f42a34b2SMiao Xie btrfs_remount_prepare(fs_info); 1393dc81cdc5SMiao Xie 1394b288052eSChris Mason ret = btrfs_parse_options(root, data); 139549b25e05SJeff Mahoney if (ret) { 139649b25e05SJeff Mahoney ret = -EINVAL; 139749b25e05SJeff Mahoney goto restore; 139849b25e05SJeff Mahoney } 1399b288052eSChris Mason 1400f42a34b2SMiao Xie btrfs_remount_begin(fs_info, old_opts, *flags); 14010d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 14020d2450abSSergei Trofimovich fs_info->thread_pool_size, old_thread_pool_size); 14030d2450abSSergei Trofimovich 1404c146afadSYan Zheng if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 1405dc81cdc5SMiao Xie goto out; 1406c146afadSYan Zheng 1407c146afadSYan Zheng if (*flags & MS_RDONLY) { 14088dabb742SStefan Behrens /* 14098dabb742SStefan Behrens * this also happens on 'umount -rf' or on shutdown, when 14108dabb742SStefan Behrens * the filesystem is busy. 14118dabb742SStefan Behrens */ 1412361c093dSStefan Behrens 1413361c093dSStefan Behrens /* wait for the uuid_scan task to finish */ 1414361c093dSStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 1415361c093dSStefan Behrens /* avoid complains from lockdep et al. */ 1416361c093dSStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 1417361c093dSStefan Behrens 1418c146afadSYan Zheng sb->s_flags |= MS_RDONLY; 1419c146afadSYan Zheng 14208dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 14218dabb742SStefan Behrens btrfs_scrub_cancel(fs_info); 1422061594efSMiao Xie btrfs_pause_balance(fs_info); 14238dabb742SStefan Behrens 1424c146afadSYan Zheng ret = btrfs_commit_super(root); 142549b25e05SJeff Mahoney if (ret) 142649b25e05SJeff Mahoney goto restore; 1427c146afadSYan Zheng } else { 14286ef3de9cSDavid Sterba if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 14296ef3de9cSDavid Sterba btrfs_err(fs_info, 1430efe120a0SFrank Holton "Remounting read-write after error is not allowed"); 14316ef3de9cSDavid Sterba ret = -EINVAL; 14326ef3de9cSDavid Sterba goto restore; 14336ef3de9cSDavid Sterba } 14348a3db184SSergei Trofimovich if (fs_info->fs_devices->rw_devices == 0) { 143549b25e05SJeff Mahoney ret = -EACCES; 143649b25e05SJeff Mahoney goto restore; 14378a3db184SSergei Trofimovich } 14382b82032cSYan Zheng 1439292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 1440292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 1441292fd7fcSStefan Behrens !(*flags & MS_RDONLY)) { 1442efe120a0SFrank Holton btrfs_warn(fs_info, 1443efe120a0SFrank Holton "too many missing devices, writeable remount is not allowed"); 1444292fd7fcSStefan Behrens ret = -EACCES; 1445292fd7fcSStefan Behrens goto restore; 1446292fd7fcSStefan Behrens } 1447292fd7fcSStefan Behrens 14488a3db184SSergei Trofimovich if (btrfs_super_log_root(fs_info->super_copy) != 0) { 144949b25e05SJeff Mahoney ret = -EINVAL; 145049b25e05SJeff Mahoney goto restore; 14518a3db184SSergei Trofimovich } 1452c146afadSYan Zheng 1453815745cfSAl Viro ret = btrfs_cleanup_fs_roots(fs_info); 145449b25e05SJeff Mahoney if (ret) 145549b25e05SJeff Mahoney goto restore; 1456c146afadSYan Zheng 1457d68fc57bSYan, Zheng /* recover relocation */ 1458d68fc57bSYan, Zheng ret = btrfs_recover_relocation(root); 145949b25e05SJeff Mahoney if (ret) 146049b25e05SJeff Mahoney goto restore; 1461c146afadSYan Zheng 14622b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 14632b6ba629SIlya Dryomov if (ret) 14642b6ba629SIlya Dryomov goto restore; 14652b6ba629SIlya Dryomov 14668dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 14678dabb742SStefan Behrens if (ret) { 1468efe120a0SFrank Holton btrfs_warn(fs_info, "failed to resume dev_replace"); 14698dabb742SStefan Behrens goto restore; 14708dabb742SStefan Behrens } 147194aebfb2SJosef Bacik 147294aebfb2SJosef Bacik if (!fs_info->uuid_root) { 1473efe120a0SFrank Holton btrfs_info(fs_info, "creating UUID tree"); 147494aebfb2SJosef Bacik ret = btrfs_create_uuid_tree(fs_info); 147594aebfb2SJosef Bacik if (ret) { 1476efe120a0SFrank Holton btrfs_warn(fs_info, "failed to create the UUID tree %d", ret); 147794aebfb2SJosef Bacik goto restore; 147894aebfb2SJosef Bacik } 147994aebfb2SJosef Bacik } 1480c146afadSYan Zheng sb->s_flags &= ~MS_RDONLY; 1481c146afadSYan Zheng } 1482dc81cdc5SMiao Xie out: 14832c6a92b0SJustin Maggard wake_up_process(fs_info->transaction_kthread); 1484dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 1485c146afadSYan Zheng return 0; 148649b25e05SJeff Mahoney 148749b25e05SJeff Mahoney restore: 148849b25e05SJeff Mahoney /* We've hit an error - don't reset MS_RDONLY */ 148949b25e05SJeff Mahoney if (sb->s_flags & MS_RDONLY) 149049b25e05SJeff Mahoney old_flags |= MS_RDONLY; 149149b25e05SJeff Mahoney sb->s_flags = old_flags; 149249b25e05SJeff Mahoney fs_info->mount_opt = old_opts; 149349b25e05SJeff Mahoney fs_info->compress_type = old_compress_type; 149449b25e05SJeff Mahoney fs_info->max_inline = old_max_inline; 1495c018daecSMiao Xie mutex_lock(&fs_info->chunk_mutex); 149649b25e05SJeff Mahoney fs_info->alloc_start = old_alloc_start; 1497c018daecSMiao Xie mutex_unlock(&fs_info->chunk_mutex); 14980d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 14990d2450abSSergei Trofimovich old_thread_pool_size, fs_info->thread_pool_size); 150049b25e05SJeff Mahoney fs_info->metadata_ratio = old_metadata_ratio; 1501dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 150249b25e05SJeff Mahoney return ret; 1503c146afadSYan Zheng } 1504c146afadSYan Zheng 1505bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 1506bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1, 1507bcd53741SArne Jansen const void *dev_info2) 1508bcd53741SArne Jansen { 1509bcd53741SArne Jansen if (((struct btrfs_device_info *)dev_info1)->max_avail > 1510bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1511bcd53741SArne Jansen return -1; 1512bcd53741SArne Jansen else if (((struct btrfs_device_info *)dev_info1)->max_avail < 1513bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1514bcd53741SArne Jansen return 1; 1515bcd53741SArne Jansen else 1516bcd53741SArne Jansen return 0; 1517bcd53741SArne Jansen } 1518bcd53741SArne Jansen 1519bcd53741SArne Jansen /* 1520bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 1521bcd53741SArne Jansen * is stored.(Descending Sort) 1522bcd53741SArne Jansen */ 1523bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 1524bcd53741SArne Jansen struct btrfs_device_info *devices, 1525bcd53741SArne Jansen size_t nr_devices) 1526bcd53741SArne Jansen { 1527bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 1528bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 1529bcd53741SArne Jansen } 1530bcd53741SArne Jansen 15316d07bcecSMiao Xie /* 15326d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 15336d07bcecSMiao Xie * file data. 15346d07bcecSMiao Xie */ 15356d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes) 15366d07bcecSMiao Xie { 15376d07bcecSMiao Xie struct btrfs_fs_info *fs_info = root->fs_info; 15386d07bcecSMiao Xie struct btrfs_device_info *devices_info; 15396d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 15406d07bcecSMiao Xie struct btrfs_device *device; 15416d07bcecSMiao Xie u64 skip_space; 15426d07bcecSMiao Xie u64 type; 15436d07bcecSMiao Xie u64 avail_space; 15446d07bcecSMiao Xie u64 used_space; 15456d07bcecSMiao Xie u64 min_stripe_size; 154639fb26c3SMiao Xie int min_stripes = 1, num_stripes = 1; 15476d07bcecSMiao Xie int i = 0, nr_devices; 15486d07bcecSMiao Xie int ret; 15496d07bcecSMiao Xie 1550b772a86eSLi Zefan nr_devices = fs_info->fs_devices->open_devices; 15516d07bcecSMiao Xie BUG_ON(!nr_devices); 15526d07bcecSMiao Xie 1553d9b0d9baSDulshani Gunawardhana devices_info = kmalloc_array(nr_devices, sizeof(*devices_info), 15546d07bcecSMiao Xie GFP_NOFS); 15556d07bcecSMiao Xie if (!devices_info) 15566d07bcecSMiao Xie return -ENOMEM; 15576d07bcecSMiao Xie 15586d07bcecSMiao Xie /* calc min stripe number for data space alloction */ 15596d07bcecSMiao Xie type = btrfs_get_alloc_profile(root, 1); 156039fb26c3SMiao Xie if (type & BTRFS_BLOCK_GROUP_RAID0) { 15616d07bcecSMiao Xie min_stripes = 2; 156239fb26c3SMiao Xie num_stripes = nr_devices; 156339fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID1) { 15646d07bcecSMiao Xie min_stripes = 2; 156539fb26c3SMiao Xie num_stripes = 2; 156639fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID10) { 15676d07bcecSMiao Xie min_stripes = 4; 156839fb26c3SMiao Xie num_stripes = 4; 156939fb26c3SMiao Xie } 15706d07bcecSMiao Xie 15716d07bcecSMiao Xie if (type & BTRFS_BLOCK_GROUP_DUP) 15726d07bcecSMiao Xie min_stripe_size = 2 * BTRFS_STRIPE_LEN; 15736d07bcecSMiao Xie else 15746d07bcecSMiao Xie min_stripe_size = BTRFS_STRIPE_LEN; 15756d07bcecSMiao Xie 1576b772a86eSLi Zefan list_for_each_entry(device, &fs_devices->devices, dev_list) { 157763a212abSStefan Behrens if (!device->in_fs_metadata || !device->bdev || 157863a212abSStefan Behrens device->is_tgtdev_for_dev_replace) 15796d07bcecSMiao Xie continue; 15806d07bcecSMiao Xie 15816d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 15826d07bcecSMiao Xie 15836d07bcecSMiao Xie /* align with stripe_len */ 15846d07bcecSMiao Xie do_div(avail_space, BTRFS_STRIPE_LEN); 15856d07bcecSMiao Xie avail_space *= BTRFS_STRIPE_LEN; 15866d07bcecSMiao Xie 15876d07bcecSMiao Xie /* 15886d07bcecSMiao Xie * In order to avoid overwritting the superblock on the drive, 15896d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 15906d07bcecSMiao Xie * allocation. 15916d07bcecSMiao Xie */ 15926d07bcecSMiao Xie skip_space = 1024 * 1024; 15936d07bcecSMiao Xie 15946d07bcecSMiao Xie /* user can set the offset in fs_info->alloc_start. */ 15956d07bcecSMiao Xie if (fs_info->alloc_start + BTRFS_STRIPE_LEN <= 15966d07bcecSMiao Xie device->total_bytes) 15976d07bcecSMiao Xie skip_space = max(fs_info->alloc_start, skip_space); 15986d07bcecSMiao Xie 15996d07bcecSMiao Xie /* 16006d07bcecSMiao Xie * btrfs can not use the free space in [0, skip_space - 1], 16016d07bcecSMiao Xie * we must subtract it from the total. In order to implement 16026d07bcecSMiao Xie * it, we account the used space in this range first. 16036d07bcecSMiao Xie */ 16046d07bcecSMiao Xie ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1, 16056d07bcecSMiao Xie &used_space); 16066d07bcecSMiao Xie if (ret) { 16076d07bcecSMiao Xie kfree(devices_info); 16086d07bcecSMiao Xie return ret; 16096d07bcecSMiao Xie } 16106d07bcecSMiao Xie 16116d07bcecSMiao Xie /* calc the free space in [0, skip_space - 1] */ 16126d07bcecSMiao Xie skip_space -= used_space; 16136d07bcecSMiao Xie 16146d07bcecSMiao Xie /* 16156d07bcecSMiao Xie * we can use the free space in [0, skip_space - 1], subtract 16166d07bcecSMiao Xie * it from the total. 16176d07bcecSMiao Xie */ 16186d07bcecSMiao Xie if (avail_space && avail_space >= skip_space) 16196d07bcecSMiao Xie avail_space -= skip_space; 16206d07bcecSMiao Xie else 16216d07bcecSMiao Xie avail_space = 0; 16226d07bcecSMiao Xie 16236d07bcecSMiao Xie if (avail_space < min_stripe_size) 16246d07bcecSMiao Xie continue; 16256d07bcecSMiao Xie 16266d07bcecSMiao Xie devices_info[i].dev = device; 16276d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 16286d07bcecSMiao Xie 16296d07bcecSMiao Xie i++; 16306d07bcecSMiao Xie } 16316d07bcecSMiao Xie 16326d07bcecSMiao Xie nr_devices = i; 16336d07bcecSMiao Xie 16346d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 16356d07bcecSMiao Xie 16366d07bcecSMiao Xie i = nr_devices - 1; 16376d07bcecSMiao Xie avail_space = 0; 16386d07bcecSMiao Xie while (nr_devices >= min_stripes) { 163939fb26c3SMiao Xie if (num_stripes > nr_devices) 164039fb26c3SMiao Xie num_stripes = nr_devices; 164139fb26c3SMiao Xie 16426d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 16436d07bcecSMiao Xie int j; 16446d07bcecSMiao Xie u64 alloc_size; 16456d07bcecSMiao Xie 164639fb26c3SMiao Xie avail_space += devices_info[i].max_avail * num_stripes; 16476d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 164839fb26c3SMiao Xie for (j = i + 1 - num_stripes; j <= i; j++) 16496d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 16506d07bcecSMiao Xie } 16516d07bcecSMiao Xie i--; 16526d07bcecSMiao Xie nr_devices--; 16536d07bcecSMiao Xie } 16546d07bcecSMiao Xie 16556d07bcecSMiao Xie kfree(devices_info); 16566d07bcecSMiao Xie *free_bytes = avail_space; 16576d07bcecSMiao Xie return 0; 16586d07bcecSMiao Xie } 16596d07bcecSMiao Xie 16608fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 16618fd17795SChris Mason { 1662815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 1663815745cfSAl Viro struct btrfs_super_block *disk_super = fs_info->super_copy; 1664815745cfSAl Viro struct list_head *head = &fs_info->space_info; 1665bd4d1088SJosef Bacik struct btrfs_space_info *found; 1666bd4d1088SJosef Bacik u64 total_used = 0; 16676d07bcecSMiao Xie u64 total_free_data = 0; 1668db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 1669815745cfSAl Viro __be32 *fsid = (__be32 *)fs_info->fsid; 16706d07bcecSMiao Xie int ret; 16718fd17795SChris Mason 16726d07bcecSMiao Xie /* holding chunk_muext to avoid allocating new chunks */ 1673815745cfSAl Viro mutex_lock(&fs_info->chunk_mutex); 1674bd4d1088SJosef Bacik rcu_read_lock(); 167589a55897SJosef Bacik list_for_each_entry_rcu(found, head, list) { 16766d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 16776d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 16786d07bcecSMiao Xie total_free_data -= 16796d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 16806d07bcecSMiao Xie } 16816d07bcecSMiao Xie 1682b742bb82SYan, Zheng total_used += found->disk_used; 168389a55897SJosef Bacik } 1684bd4d1088SJosef Bacik rcu_read_unlock(); 1685bd4d1088SJosef Bacik 16868fd17795SChris Mason buf->f_namelen = BTRFS_NAME_LEN; 1687db94535dSChris Mason buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits; 1688bd4d1088SJosef Bacik buf->f_bfree = buf->f_blocks - (total_used >> bits); 16898fd17795SChris Mason buf->f_bsize = dentry->d_sb->s_blocksize; 16908fd17795SChris Mason buf->f_type = BTRFS_SUPER_MAGIC; 16916d07bcecSMiao Xie buf->f_bavail = total_free_data; 1692815745cfSAl Viro ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data); 16936d07bcecSMiao Xie if (ret) { 1694815745cfSAl Viro mutex_unlock(&fs_info->chunk_mutex); 16956d07bcecSMiao Xie return ret; 16966d07bcecSMiao Xie } 16976d07bcecSMiao Xie buf->f_bavail += total_free_data; 16986d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 1699815745cfSAl Viro mutex_unlock(&fs_info->chunk_mutex); 1700d397712bSChris Mason 17019d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 17029d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 17039d03632eSDavid Woodhouse on a big-endian or little-endian host */ 17049d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 17059d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 170632d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 170732d48fa1SDavid Woodhouse buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32; 170832d48fa1SDavid Woodhouse buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid; 170932d48fa1SDavid Woodhouse 17108fd17795SChris Mason return 0; 17118fd17795SChris Mason } 1712b5133862SChris Mason 1713aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb) 1714aea52e19SAl Viro { 1715815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1716aea52e19SAl Viro kill_anon_super(sb); 1717aea52e19SAl Viro free_fs_info(fs_info); 1718aea52e19SAl Viro } 1719aea52e19SAl Viro 17202e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 17212e635a27SChris Mason .owner = THIS_MODULE, 17222e635a27SChris Mason .name = "btrfs", 1723061dbc6bSAl Viro .mount = btrfs_mount, 1724aea52e19SAl Viro .kill_sb = btrfs_kill_super, 17252e635a27SChris Mason .fs_flags = FS_REQUIRES_DEV, 17262e635a27SChris Mason }; 17277f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs"); 1728a9218f6bSChris Mason 1729d352ac68SChris Mason /* 1730d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 1731d352ac68SChris Mason */ 17328a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 17338a4b83ccSChris Mason unsigned long arg) 17348a4b83ccSChris Mason { 17358a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 17368a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 1737c071fcfdSChris Mason int ret = -ENOTTY; 17388a4b83ccSChris Mason 1739e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1740e441d54dSChris Mason return -EPERM; 1741e441d54dSChris Mason 1742dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 1743dae7b665SLi Zefan if (IS_ERR(vol)) 1744dae7b665SLi Zefan return PTR_ERR(vol); 1745c071fcfdSChris Mason 17468a4b83ccSChris Mason switch (cmd) { 17478a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 174897288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 17498a4b83ccSChris Mason &btrfs_fs_type, &fs_devices); 17508a4b83ccSChris Mason break; 175102db0844SJosef Bacik case BTRFS_IOC_DEVICES_READY: 175202db0844SJosef Bacik ret = btrfs_scan_one_device(vol->name, FMODE_READ, 175302db0844SJosef Bacik &btrfs_fs_type, &fs_devices); 175402db0844SJosef Bacik if (ret) 175502db0844SJosef Bacik break; 175602db0844SJosef Bacik ret = !(fs_devices->num_devices == fs_devices->total_devices); 175702db0844SJosef Bacik break; 17588a4b83ccSChris Mason } 1759dae7b665SLi Zefan 17608a4b83ccSChris Mason kfree(vol); 1761f819d837SLinda Knippers return ret; 17628a4b83ccSChris Mason } 17638a4b83ccSChris Mason 17640176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 1765ed0dab6bSYan { 1766354aa0fbSMiao Xie struct btrfs_trans_handle *trans; 1767354aa0fbSMiao Xie struct btrfs_root *root = btrfs_sb(sb)->tree_root; 1768354aa0fbSMiao Xie 1769d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 1770354aa0fbSMiao Xie if (IS_ERR(trans)) { 1771354aa0fbSMiao Xie /* no transaction, don't bother */ 1772354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 17730176260fSLinus Torvalds return 0; 1774354aa0fbSMiao Xie return PTR_ERR(trans); 1775354aa0fbSMiao Xie } 1776354aa0fbSMiao Xie return btrfs_commit_transaction(trans, root); 1777ed0dab6bSYan } 1778ed0dab6bSYan 17790176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb) 1780ed0dab6bSYan { 17810176260fSLinus Torvalds return 0; 1782ed0dab6bSYan } 17832e635a27SChris Mason 17849c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 17859c5085c1SJosef Bacik { 17869c5085c1SJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 17879c5085c1SJosef Bacik struct btrfs_fs_devices *cur_devices; 17889c5085c1SJosef Bacik struct btrfs_device *dev, *first_dev = NULL; 17899c5085c1SJosef Bacik struct list_head *head; 17909c5085c1SJosef Bacik struct rcu_string *name; 17919c5085c1SJosef Bacik 17929c5085c1SJosef Bacik mutex_lock(&fs_info->fs_devices->device_list_mutex); 17939c5085c1SJosef Bacik cur_devices = fs_info->fs_devices; 17949c5085c1SJosef Bacik while (cur_devices) { 17959c5085c1SJosef Bacik head = &cur_devices->devices; 17969c5085c1SJosef Bacik list_for_each_entry(dev, head, dev_list) { 1797aa9ddcd4SJosef Bacik if (dev->missing) 1798aa9ddcd4SJosef Bacik continue; 17999c5085c1SJosef Bacik if (!first_dev || dev->devid < first_dev->devid) 18009c5085c1SJosef Bacik first_dev = dev; 18019c5085c1SJosef Bacik } 18029c5085c1SJosef Bacik cur_devices = cur_devices->seed; 18039c5085c1SJosef Bacik } 18049c5085c1SJosef Bacik 18059c5085c1SJosef Bacik if (first_dev) { 18069c5085c1SJosef Bacik rcu_read_lock(); 18079c5085c1SJosef Bacik name = rcu_dereference(first_dev->name); 18089c5085c1SJosef Bacik seq_escape(m, name->str, " \t\n\\"); 18099c5085c1SJosef Bacik rcu_read_unlock(); 18109c5085c1SJosef Bacik } else { 18119c5085c1SJosef Bacik WARN_ON(1); 18129c5085c1SJosef Bacik } 18139c5085c1SJosef Bacik mutex_unlock(&fs_info->fs_devices->device_list_mutex); 18149c5085c1SJosef Bacik return 0; 18159c5085c1SJosef Bacik } 18169c5085c1SJosef Bacik 1817b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 181876dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 1819bd555975SAl Viro .evict_inode = btrfs_evict_inode, 1820e20d96d6SChris Mason .put_super = btrfs_put_super, 1821d5719762SChris Mason .sync_fs = btrfs_sync_fs, 1822a9572a15SEric Paris .show_options = btrfs_show_options, 18239c5085c1SJosef Bacik .show_devname = btrfs_show_devname, 18244730a4bcSChris Mason .write_inode = btrfs_write_inode, 18252c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 18262c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 18278fd17795SChris Mason .statfs = btrfs_statfs, 1828c146afadSYan Zheng .remount_fs = btrfs_remount, 18290176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 18300176260fSLinus Torvalds .unfreeze_fs = btrfs_unfreeze, 1831e20d96d6SChris Mason }; 1832a9218f6bSChris Mason 1833a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 1834a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 1835a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 1836a9218f6bSChris Mason .owner = THIS_MODULE, 18376038f373SArnd Bergmann .llseek = noop_llseek, 1838a9218f6bSChris Mason }; 1839a9218f6bSChris Mason 1840a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 1841578454ffSKay Sievers .minor = BTRFS_MINOR, 1842a9218f6bSChris Mason .name = "btrfs-control", 1843a9218f6bSChris Mason .fops = &btrfs_ctl_fops 1844a9218f6bSChris Mason }; 1845a9218f6bSChris Mason 1846578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 1847578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 1848578454ffSKay Sievers 1849a9218f6bSChris Mason static int btrfs_interface_init(void) 1850a9218f6bSChris Mason { 1851a9218f6bSChris Mason return misc_register(&btrfs_misc); 1852a9218f6bSChris Mason } 1853a9218f6bSChris Mason 1854b2950863SChristoph Hellwig static void btrfs_interface_exit(void) 1855a9218f6bSChris Mason { 1856a9218f6bSChris Mason if (misc_deregister(&btrfs_misc) < 0) 1857efe120a0SFrank Holton printk(KERN_INFO "BTRFS: misc_deregister failed for control device\n"); 1858a9218f6bSChris Mason } 1859a9218f6bSChris Mason 186085965600SDavid Sterba static void btrfs_print_info(void) 186185965600SDavid Sterba { 186285965600SDavid Sterba printk(KERN_INFO "Btrfs loaded" 186385965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 186485965600SDavid Sterba ", debug=on" 186585965600SDavid Sterba #endif 186679556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT 186779556c3dSStefan Behrens ", assert=on" 186879556c3dSStefan Behrens #endif 186985965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 187085965600SDavid Sterba ", integrity-checker=on" 187185965600SDavid Sterba #endif 187285965600SDavid Sterba "\n"); 187385965600SDavid Sterba } 187485965600SDavid Sterba 1875dc11dd5dSJosef Bacik static int btrfs_run_sanity_tests(void) 1876dc11dd5dSJosef Bacik { 187706ea65a3SJosef Bacik int ret; 187806ea65a3SJosef Bacik 1879294e30feSJosef Bacik ret = btrfs_init_test_fs(); 188006ea65a3SJosef Bacik if (ret) 188106ea65a3SJosef Bacik return ret; 1882294e30feSJosef Bacik 1883294e30feSJosef Bacik ret = btrfs_test_free_space_cache(); 1884294e30feSJosef Bacik if (ret) 1885294e30feSJosef Bacik goto out; 1886294e30feSJosef Bacik ret = btrfs_test_extent_buffer_operations(); 1887294e30feSJosef Bacik if (ret) 1888294e30feSJosef Bacik goto out; 1889294e30feSJosef Bacik ret = btrfs_test_extent_io(); 1890aaedb55bSJosef Bacik if (ret) 1891aaedb55bSJosef Bacik goto out; 1892aaedb55bSJosef Bacik ret = btrfs_test_inodes(); 1893294e30feSJosef Bacik out: 1894294e30feSJosef Bacik btrfs_destroy_test_fs(); 1895294e30feSJosef Bacik return ret; 1896dc11dd5dSJosef Bacik } 1897dc11dd5dSJosef Bacik 18982e635a27SChris Mason static int __init init_btrfs_fs(void) 18992e635a27SChris Mason { 19002c90e5d6SChris Mason int err; 190158176a96SJosef Bacik 190214a958e6SFilipe David Borba Manana err = btrfs_hash_init(); 190314a958e6SFilipe David Borba Manana if (err) 190414a958e6SFilipe David Borba Manana return err; 190514a958e6SFilipe David Borba Manana 190663541927SFilipe David Borba Manana btrfs_props_init(); 190763541927SFilipe David Borba Manana 190858176a96SJosef Bacik err = btrfs_init_sysfs(); 190958176a96SJosef Bacik if (err) 191014a958e6SFilipe David Borba Manana goto free_hash; 191158176a96SJosef Bacik 1912143bede5SJeff Mahoney btrfs_init_compress(); 1913d1310b2eSChris Mason 1914261507a0SLi Zefan err = btrfs_init_cachep(); 1915261507a0SLi Zefan if (err) 1916261507a0SLi Zefan goto free_compress; 1917261507a0SLi Zefan 1918d1310b2eSChris Mason err = extent_io_init(); 19192f4cbe64SWyatt Banks if (err) 19202f4cbe64SWyatt Banks goto free_cachep; 19212f4cbe64SWyatt Banks 1922d1310b2eSChris Mason err = extent_map_init(); 1923d1310b2eSChris Mason if (err) 1924d1310b2eSChris Mason goto free_extent_io; 1925d1310b2eSChris Mason 19266352b91dSMiao Xie err = ordered_data_init(); 19272f4cbe64SWyatt Banks if (err) 19282f4cbe64SWyatt Banks goto free_extent_map; 1929c8b97818SChris Mason 19306352b91dSMiao Xie err = btrfs_delayed_inode_init(); 19316352b91dSMiao Xie if (err) 19326352b91dSMiao Xie goto free_ordered_data; 19336352b91dSMiao Xie 19349247f317SMiao Xie err = btrfs_auto_defrag_init(); 193516cdcec7SMiao Xie if (err) 193616cdcec7SMiao Xie goto free_delayed_inode; 193716cdcec7SMiao Xie 193878a6184aSMiao Xie err = btrfs_delayed_ref_init(); 19399247f317SMiao Xie if (err) 19409247f317SMiao Xie goto free_auto_defrag; 19419247f317SMiao Xie 1942b9e9a6cbSWang Shilong err = btrfs_prelim_ref_init(); 1943b9e9a6cbSWang Shilong if (err) 1944b9e9a6cbSWang Shilong goto free_prelim_ref; 1945b9e9a6cbSWang Shilong 194678a6184aSMiao Xie err = btrfs_interface_init(); 194778a6184aSMiao Xie if (err) 194878a6184aSMiao Xie goto free_delayed_ref; 194978a6184aSMiao Xie 1950e565d4b9SJan Schmidt btrfs_init_lockdep(); 1951e565d4b9SJan Schmidt 195285965600SDavid Sterba btrfs_print_info(); 1953dc11dd5dSJosef Bacik 1954dc11dd5dSJosef Bacik err = btrfs_run_sanity_tests(); 1955dc11dd5dSJosef Bacik if (err) 1956dc11dd5dSJosef Bacik goto unregister_ioctl; 1957dc11dd5dSJosef Bacik 1958dc11dd5dSJosef Bacik err = register_filesystem(&btrfs_fs_type); 1959dc11dd5dSJosef Bacik if (err) 1960dc11dd5dSJosef Bacik goto unregister_ioctl; 196174255aa0SJosef Bacik 19622f4cbe64SWyatt Banks return 0; 19632f4cbe64SWyatt Banks 1964a9218f6bSChris Mason unregister_ioctl: 1965a9218f6bSChris Mason btrfs_interface_exit(); 1966b9e9a6cbSWang Shilong free_prelim_ref: 1967b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 196878a6184aSMiao Xie free_delayed_ref: 196978a6184aSMiao Xie btrfs_delayed_ref_exit(); 19709247f317SMiao Xie free_auto_defrag: 19719247f317SMiao Xie btrfs_auto_defrag_exit(); 197216cdcec7SMiao Xie free_delayed_inode: 197316cdcec7SMiao Xie btrfs_delayed_inode_exit(); 19746352b91dSMiao Xie free_ordered_data: 19756352b91dSMiao Xie ordered_data_exit(); 19762f4cbe64SWyatt Banks free_extent_map: 19772f4cbe64SWyatt Banks extent_map_exit(); 1978d1310b2eSChris Mason free_extent_io: 1979d1310b2eSChris Mason extent_io_exit(); 19802f4cbe64SWyatt Banks free_cachep: 19812f4cbe64SWyatt Banks btrfs_destroy_cachep(); 1982261507a0SLi Zefan free_compress: 1983261507a0SLi Zefan btrfs_exit_compress(); 19842f4cbe64SWyatt Banks btrfs_exit_sysfs(); 198514a958e6SFilipe David Borba Manana free_hash: 198614a958e6SFilipe David Borba Manana btrfs_hash_exit(); 19872c90e5d6SChris Mason return err; 19882e635a27SChris Mason } 19892e635a27SChris Mason 19902e635a27SChris Mason static void __exit exit_btrfs_fs(void) 19912e635a27SChris Mason { 199239279cc3SChris Mason btrfs_destroy_cachep(); 199378a6184aSMiao Xie btrfs_delayed_ref_exit(); 19949247f317SMiao Xie btrfs_auto_defrag_exit(); 199516cdcec7SMiao Xie btrfs_delayed_inode_exit(); 1996b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 19976352b91dSMiao Xie ordered_data_exit(); 1998a52d9a80SChris Mason extent_map_exit(); 1999d1310b2eSChris Mason extent_io_exit(); 2000a9218f6bSChris Mason btrfs_interface_exit(); 20012e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 200258176a96SJosef Bacik btrfs_exit_sysfs(); 20038a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 2004261507a0SLi Zefan btrfs_exit_compress(); 200514a958e6SFilipe David Borba Manana btrfs_hash_exit(); 20062e635a27SChris Mason } 20072e635a27SChris Mason 200860efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs); 20092e635a27SChris Mason module_exit(exit_btrfs_fs) 20102e635a27SChris Mason 20112e635a27SChris Mason MODULE_LICENSE("GPL"); 2012