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 690723a047SHarald Hoyer static int btrfs_remount(struct super_block *sb, int *flags, char *data); 700723a047SHarald Hoyer 7108748810SDavid Sterba static const char *btrfs_decode_error(int errno) 72acce952bSliubo { 7308748810SDavid Sterba char *errstr = "unknown"; 74acce952bSliubo 75acce952bSliubo switch (errno) { 76acce952bSliubo case -EIO: 77acce952bSliubo errstr = "IO failure"; 78acce952bSliubo break; 79acce952bSliubo case -ENOMEM: 80acce952bSliubo errstr = "Out of memory"; 81acce952bSliubo break; 82acce952bSliubo case -EROFS: 83acce952bSliubo errstr = "Readonly filesystem"; 84acce952bSliubo break; 858c342930SJeff Mahoney case -EEXIST: 868c342930SJeff Mahoney errstr = "Object already exists"; 878c342930SJeff Mahoney break; 8894ef7280SDavid Sterba case -ENOSPC: 8994ef7280SDavid Sterba errstr = "No space left"; 9094ef7280SDavid Sterba break; 9194ef7280SDavid Sterba case -ENOENT: 9294ef7280SDavid Sterba errstr = "No such entry"; 9394ef7280SDavid Sterba break; 94acce952bSliubo } 95acce952bSliubo 96acce952bSliubo return errstr; 97acce952bSliubo } 98acce952bSliubo 99bbece8a3SDavid Sterba static void save_error_info(struct btrfs_fs_info *fs_info) 100acce952bSliubo { 101acce952bSliubo /* 102acce952bSliubo * today we only save the error info into ram. Long term we'll 103acce952bSliubo * also send it down to the disk 104acce952bSliubo */ 10587533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 106acce952bSliubo } 107acce952bSliubo 108acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */ 109acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info) 110acce952bSliubo { 111acce952bSliubo struct super_block *sb = fs_info->sb; 112acce952bSliubo 113acce952bSliubo if (sb->s_flags & MS_RDONLY) 114acce952bSliubo return; 115acce952bSliubo 11687533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 117acce952bSliubo sb->s_flags |= MS_RDONLY; 118c2cf52ebSSimon Kirby btrfs_info(fs_info, "forced readonly"); 1191acd6831SStefan Behrens /* 1201acd6831SStefan Behrens * Note that a running device replace operation is not 1211acd6831SStefan Behrens * canceled here although there is no way to update 1221acd6831SStefan Behrens * the progress. It would add the risk of a deadlock, 1231acd6831SStefan Behrens * therefore the canceling is ommited. The only penalty 1241acd6831SStefan Behrens * is that some I/O remains active until the procedure 1251acd6831SStefan Behrens * completes. The next time when the filesystem is 1261acd6831SStefan Behrens * mounted writeable again, the device replace 1271acd6831SStefan Behrens * operation continues. 1281acd6831SStefan Behrens */ 129acce952bSliubo } 130acce952bSliubo } 131acce952bSliubo 132533574c6SJoe Perches #ifdef CONFIG_PRINTK 133acce952bSliubo /* 134acce952bSliubo * __btrfs_std_error decodes expected errors from the caller and 135acce952bSliubo * invokes the approciate error response. 136acce952bSliubo */ 137acce952bSliubo void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function, 1384da35113SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 139acce952bSliubo { 140acce952bSliubo struct super_block *sb = fs_info->sb; 141acce952bSliubo const char *errstr; 142acce952bSliubo 143acce952bSliubo /* 144acce952bSliubo * Special case: if the error is EROFS, and we're already 145acce952bSliubo * under MS_RDONLY, then it is safe here. 146acce952bSliubo */ 147acce952bSliubo if (errno == -EROFS && (sb->s_flags & MS_RDONLY)) 148acce952bSliubo return; 149acce952bSliubo 15008748810SDavid Sterba errstr = btrfs_decode_error(errno); 1514da35113SJeff Mahoney if (fmt) { 15237252a66SEric Sandeen struct va_format vaf; 15337252a66SEric Sandeen va_list args; 15437252a66SEric Sandeen 15537252a66SEric Sandeen va_start(args, fmt); 15637252a66SEric Sandeen vaf.fmt = fmt; 15737252a66SEric Sandeen vaf.va = &args; 1584da35113SJeff Mahoney 159efe120a0SFrank Holton printk(KERN_CRIT 160efe120a0SFrank Holton "BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n", 16108748810SDavid Sterba sb->s_id, function, line, errno, errstr, &vaf); 16237252a66SEric Sandeen va_end(args); 1634da35113SJeff Mahoney } else { 164efe120a0SFrank Holton printk(KERN_CRIT "BTRFS: error (device %s) in %s:%d: errno=%d %s\n", 16508748810SDavid Sterba sb->s_id, function, line, errno, errstr); 1664da35113SJeff Mahoney } 167acce952bSliubo 1684da35113SJeff Mahoney /* Don't go through full error handling during mount */ 1694da35113SJeff Mahoney save_error_info(fs_info); 170cf79ffb5SJosef Bacik if (sb->s_flags & MS_BORN) 171acce952bSliubo btrfs_handle_error(fs_info); 172acce952bSliubo } 1734da35113SJeff Mahoney 174533574c6SJoe Perches static const char * const logtypes[] = { 1754da35113SJeff Mahoney "emergency", 1764da35113SJeff Mahoney "alert", 1774da35113SJeff Mahoney "critical", 1784da35113SJeff Mahoney "error", 1794da35113SJeff Mahoney "warning", 1804da35113SJeff Mahoney "notice", 1814da35113SJeff Mahoney "info", 1824da35113SJeff Mahoney "debug", 1834da35113SJeff Mahoney }; 1844da35113SJeff Mahoney 185c2cf52ebSSimon Kirby void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...) 1864da35113SJeff Mahoney { 1874da35113SJeff Mahoney struct super_block *sb = fs_info->sb; 1884da35113SJeff Mahoney char lvl[4]; 1894da35113SJeff Mahoney struct va_format vaf; 1904da35113SJeff Mahoney va_list args; 1914da35113SJeff Mahoney const char *type = logtypes[4]; 192533574c6SJoe Perches int kern_level; 1934da35113SJeff Mahoney 1944da35113SJeff Mahoney va_start(args, fmt); 1954da35113SJeff Mahoney 196533574c6SJoe Perches kern_level = printk_get_level(fmt); 197533574c6SJoe Perches if (kern_level) { 198533574c6SJoe Perches size_t size = printk_skip_level(fmt) - fmt; 199533574c6SJoe Perches memcpy(lvl, fmt, size); 200533574c6SJoe Perches lvl[size] = '\0'; 201533574c6SJoe Perches fmt += size; 202533574c6SJoe Perches type = logtypes[kern_level - '0']; 2034da35113SJeff Mahoney } else 2044da35113SJeff Mahoney *lvl = '\0'; 2054da35113SJeff Mahoney 2064da35113SJeff Mahoney vaf.fmt = fmt; 2074da35113SJeff Mahoney vaf.va = &args; 208533574c6SJoe Perches 209c2cf52ebSSimon Kirby printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf); 210533574c6SJoe Perches 211533574c6SJoe Perches va_end(args); 2124da35113SJeff Mahoney } 213acce952bSliubo 214533574c6SJoe Perches #else 215533574c6SJoe Perches 216533574c6SJoe Perches void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function, 217533574c6SJoe Perches unsigned int line, int errno, const char *fmt, ...) 218533574c6SJoe Perches { 219533574c6SJoe Perches struct super_block *sb = fs_info->sb; 220533574c6SJoe Perches 221533574c6SJoe Perches /* 222533574c6SJoe Perches * Special case: if the error is EROFS, and we're already 223533574c6SJoe Perches * under MS_RDONLY, then it is safe here. 224533574c6SJoe Perches */ 225533574c6SJoe Perches if (errno == -EROFS && (sb->s_flags & MS_RDONLY)) 226533574c6SJoe Perches return; 227533574c6SJoe Perches 228533574c6SJoe Perches /* Don't go through full error handling during mount */ 229533574c6SJoe Perches if (sb->s_flags & MS_BORN) { 230533574c6SJoe Perches save_error_info(fs_info); 231533574c6SJoe Perches btrfs_handle_error(fs_info); 232533574c6SJoe Perches } 233533574c6SJoe Perches } 234533574c6SJoe Perches #endif 235533574c6SJoe Perches 2368c342930SJeff Mahoney /* 23749b25e05SJeff Mahoney * We only mark the transaction aborted and then set the file system read-only. 23849b25e05SJeff Mahoney * This will prevent new transactions from starting or trying to join this 23949b25e05SJeff Mahoney * one. 24049b25e05SJeff Mahoney * 24149b25e05SJeff Mahoney * This means that error recovery at the call site is limited to freeing 24249b25e05SJeff Mahoney * any local memory allocations and passing the error code up without 24349b25e05SJeff Mahoney * further cleanup. The transaction should complete as it normally would 24449b25e05SJeff Mahoney * in the call path but will return -EIO. 24549b25e05SJeff Mahoney * 24649b25e05SJeff Mahoney * We'll complete the cleanup in btrfs_end_transaction and 24749b25e05SJeff Mahoney * btrfs_commit_transaction. 24849b25e05SJeff Mahoney */ 24949b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans, 25049b25e05SJeff Mahoney struct btrfs_root *root, const char *function, 25149b25e05SJeff Mahoney unsigned int line, int errno) 25249b25e05SJeff Mahoney { 25308748810SDavid Sterba /* 25408748810SDavid Sterba * Report first abort since mount 25508748810SDavid Sterba */ 25608748810SDavid Sterba if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED, 25708748810SDavid Sterba &root->fs_info->fs_state)) { 258efe120a0SFrank Holton WARN(1, KERN_DEBUG "BTRFS: Transaction aborted (error %d)\n", 25908748810SDavid Sterba errno); 26008748810SDavid Sterba } 26149b25e05SJeff Mahoney trans->aborted = errno; 26249b25e05SJeff Mahoney /* Nothing used. The other threads that have joined this 26349b25e05SJeff Mahoney * transaction may be able to continue. */ 26449b25e05SJeff Mahoney if (!trans->blocks_used) { 26569ce977aSMiao Xie const char *errstr; 26669ce977aSMiao Xie 26708748810SDavid Sterba errstr = btrfs_decode_error(errno); 268c2cf52ebSSimon Kirby btrfs_warn(root->fs_info, 269c2cf52ebSSimon Kirby "%s:%d: Aborting unused transaction(%s).", 27069ce977aSMiao Xie function, line, errstr); 27149b25e05SJeff Mahoney return; 27249b25e05SJeff Mahoney } 2738d25a086SMiao Xie ACCESS_ONCE(trans->transaction->aborted) = errno; 274501407aaSJosef Bacik /* Wake up anybody who may be waiting on this transaction */ 275501407aaSJosef Bacik wake_up(&root->fs_info->transaction_wait); 276501407aaSJosef Bacik wake_up(&root->fs_info->transaction_blocked_wait); 27749b25e05SJeff Mahoney __btrfs_std_error(root->fs_info, function, line, errno, NULL); 27849b25e05SJeff Mahoney } 27949b25e05SJeff Mahoney /* 2808c342930SJeff Mahoney * __btrfs_panic decodes unexpected, fatal errors from the caller, 2818c342930SJeff Mahoney * issues an alert, and either panics or BUGs, depending on mount options. 2828c342930SJeff Mahoney */ 2838c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, 2848c342930SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 2858c342930SJeff Mahoney { 2868c342930SJeff Mahoney char *s_id = "<unknown>"; 2878c342930SJeff Mahoney const char *errstr; 2888c342930SJeff Mahoney struct va_format vaf = { .fmt = fmt }; 2898c342930SJeff Mahoney va_list args; 2908c342930SJeff Mahoney 2918c342930SJeff Mahoney if (fs_info) 2928c342930SJeff Mahoney s_id = fs_info->sb->s_id; 2938c342930SJeff Mahoney 2948c342930SJeff Mahoney va_start(args, fmt); 2958c342930SJeff Mahoney vaf.va = &args; 2968c342930SJeff Mahoney 29708748810SDavid Sterba errstr = btrfs_decode_error(errno); 298aa43a17cSEric Sandeen if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR)) 29908748810SDavid Sterba panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n", 30008748810SDavid Sterba s_id, function, line, &vaf, errno, errstr); 3018c342930SJeff Mahoney 302efe120a0SFrank Holton btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)", 303efe120a0SFrank Holton function, line, &vaf, errno, errstr); 3048c342930SJeff Mahoney va_end(args); 3058c342930SJeff Mahoney /* Caller calls BUG() */ 3068c342930SJeff Mahoney } 307e20d96d6SChris Mason 308e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 309e20d96d6SChris Mason { 310815745cfSAl Viro (void)close_ctree(btrfs_sb(sb)->tree_root); 311aea52e19SAl Viro /* FIXME: need to fix VFS to return error? */ 312aea52e19SAl Viro /* AV: return it _where_? ->put_super() can be triggered by any number 313aea52e19SAl Viro * of async events, up to and including delivery of SIGKILL to the 314aea52e19SAl Viro * last process that kept it busy. Or segfault in the aforementioned 315aea52e19SAl Viro * process... Whom would you report that to? 316aea52e19SAl Viro */ 317e20d96d6SChris Mason } 3182e635a27SChris Mason 31995e05289SChris Mason enum { 32073f73415SJosef Bacik Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum, 321287a0ab9SJosef Bacik Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, 322287a0ab9SJosef Bacik Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress, 323261507a0SLi Zefan Opt_compress_type, Opt_compress_force, Opt_compress_force_type, 324261507a0SLi Zefan Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard, 32591435650SChris Mason Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed, 3269555c6c1SIlya Dryomov Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache, 3279555c6c1SIlya Dryomov Opt_no_space_cache, Opt_recovery, Opt_skip_balance, 32821adbd5cSStefan Behrens Opt_check_integrity, Opt_check_integrity_including_extent_data, 329f420ee1eSStefan Behrens Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree, 330e07a2adeSQu Wenruo Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard, 331a258af7aSQu Wenruo Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow, 3323818aea2SQu Wenruo Opt_datasum, Opt_treelog, Opt_noinode_cache, 3339555c6c1SIlya Dryomov Opt_err, 33495e05289SChris Mason }; 33595e05289SChris Mason 33695e05289SChris Mason static match_table_t tokens = { 337dfe25020SChris Mason {Opt_degraded, "degraded"}, 33895e05289SChris Mason {Opt_subvol, "subvol=%s"}, 3391493381fSWang Shilong {Opt_subvolid, "subvolid=%s"}, 34043e570b0SChristoph Hellwig {Opt_device, "device=%s"}, 341b6cda9bcSChris Mason {Opt_nodatasum, "nodatasum"}, 342d399167dSQu Wenruo {Opt_datasum, "datasum"}, 343be20aa9dSChris Mason {Opt_nodatacow, "nodatacow"}, 344a258af7aSQu Wenruo {Opt_datacow, "datacow"}, 34521ad10cfSChris Mason {Opt_nobarrier, "nobarrier"}, 346842bef58SQu Wenruo {Opt_barrier, "barrier"}, 3476f568d35SChris Mason {Opt_max_inline, "max_inline=%s"}, 3488f662a76SChris Mason {Opt_alloc_start, "alloc_start=%s"}, 3494543df7eSChris Mason {Opt_thread_pool, "thread_pool=%d"}, 350c8b97818SChris Mason {Opt_compress, "compress"}, 351261507a0SLi Zefan {Opt_compress_type, "compress=%s"}, 352a555f810SChris Mason {Opt_compress_force, "compress-force"}, 353261507a0SLi Zefan {Opt_compress_force_type, "compress-force=%s"}, 354e18e4809SChris Mason {Opt_ssd, "ssd"}, 355451d7585SChris Mason {Opt_ssd_spread, "ssd_spread"}, 3563b30c22fSChris Mason {Opt_nossd, "nossd"}, 357bd0330adSQu Wenruo {Opt_acl, "acl"}, 35833268eafSJosef Bacik {Opt_noacl, "noacl"}, 3593a5e1404SSage Weil {Opt_notreelog, "notreelog"}, 360a88998f2SQu Wenruo {Opt_treelog, "treelog"}, 361dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 3622c9ee856SQu Wenruo {Opt_noflushoncommit, "noflushoncommit"}, 36397e728d4SJosef Bacik {Opt_ratio, "metadata_ratio=%d"}, 364e244a0aeSChristoph Hellwig {Opt_discard, "discard"}, 365e07a2adeSQu Wenruo {Opt_nodiscard, "nodiscard"}, 3660af3d00bSJosef Bacik {Opt_space_cache, "space_cache"}, 36788c2ba3bSJosef Bacik {Opt_clear_cache, "clear_cache"}, 3684260f7c7SSage Weil {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, 36991435650SChris Mason {Opt_enospc_debug, "enospc_debug"}, 37053036293SQu Wenruo {Opt_noenospc_debug, "noenospc_debug"}, 371e15d0542SXin Zhong {Opt_subvolrootid, "subvolrootid=%d"}, 3724cb5300bSChris Mason {Opt_defrag, "autodefrag"}, 373fc0ca9afSQu Wenruo {Opt_nodefrag, "noautodefrag"}, 3744b9465cbSChris Mason {Opt_inode_cache, "inode_cache"}, 3753818aea2SQu Wenruo {Opt_noinode_cache, "noinode_cache"}, 3768965593eSDavid Sterba {Opt_no_space_cache, "nospace_cache"}, 377af31f5e5SChris Mason {Opt_recovery, "recovery"}, 3789555c6c1SIlya Dryomov {Opt_skip_balance, "skip_balance"}, 37921adbd5cSStefan Behrens {Opt_check_integrity, "check_int"}, 38021adbd5cSStefan Behrens {Opt_check_integrity_including_extent_data, "check_int_data"}, 38121adbd5cSStefan Behrens {Opt_check_integrity_print_mask, "check_int_print_mask=%d"}, 382f420ee1eSStefan Behrens {Opt_rescan_uuid_tree, "rescan_uuid_tree"}, 3838c342930SJeff Mahoney {Opt_fatal_errors, "fatal_errors=%s"}, 3848b87dc17SDavid Sterba {Opt_commit_interval, "commit=%d"}, 38533268eafSJosef Bacik {Opt_err, NULL}, 38695e05289SChris Mason }; 38795e05289SChris Mason 388edf24abeSChristoph Hellwig /* 389edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 390edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 39149b25e05SJeff Mahoney * XXX JDM: This needs to be cleaned up for remount. 392edf24abeSChristoph Hellwig */ 393edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options) 39495e05289SChris Mason { 395edf24abeSChristoph Hellwig struct btrfs_fs_info *info = root->fs_info; 39695e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 39773bc1876SJosef Bacik char *p, *num, *orig = NULL; 39873bc1876SJosef Bacik u64 cache_gen; 3994543df7eSChris Mason int intarg; 400a7a3f7caSSage Weil int ret = 0; 401261507a0SLi Zefan char *compress_type; 402261507a0SLi Zefan bool compress_force = false; 40307802534SQu Wenruo bool compress = false; 404b6cda9bcSChris Mason 4056c41761fSDavid Sterba cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy); 40673bc1876SJosef Bacik if (cache_gen) 40773bc1876SJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 40873bc1876SJosef Bacik 40995e05289SChris Mason if (!options) 41073bc1876SJosef Bacik goto out; 41195e05289SChris Mason 412be20aa9dSChris Mason /* 413be20aa9dSChris Mason * strsep changes the string, duplicate it because parse_options 414be20aa9dSChris Mason * gets called twice 415be20aa9dSChris Mason */ 416be20aa9dSChris Mason options = kstrdup(options, GFP_NOFS); 417be20aa9dSChris Mason if (!options) 418be20aa9dSChris Mason return -ENOMEM; 419be20aa9dSChris Mason 420da495eccSJosef Bacik orig = options; 421be20aa9dSChris Mason 42295e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 42395e05289SChris Mason int token; 42495e05289SChris Mason if (!*p) 42595e05289SChris Mason continue; 42695e05289SChris Mason 42795e05289SChris Mason token = match_token(p, tokens, args); 42895e05289SChris Mason switch (token) { 429dfe25020SChris Mason case Opt_degraded: 430efe120a0SFrank Holton btrfs_info(root->fs_info, "allowing degraded mounts"); 431dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 432dfe25020SChris Mason break; 43395e05289SChris Mason case Opt_subvol: 43473f73415SJosef Bacik case Opt_subvolid: 435e15d0542SXin Zhong case Opt_subvolrootid: 43643e570b0SChristoph Hellwig case Opt_device: 437edf24abeSChristoph Hellwig /* 43843e570b0SChristoph Hellwig * These are parsed by btrfs_parse_early_options 439edf24abeSChristoph Hellwig * and can be happily ignored here. 440edf24abeSChristoph Hellwig */ 44195e05289SChris Mason break; 442b6cda9bcSChris Mason case Opt_nodatasum: 44307802534SQu Wenruo btrfs_set_and_info(root, NODATASUM, 44407802534SQu Wenruo "setting nodatasum"); 445be20aa9dSChris Mason break; 446d399167dSQu Wenruo case Opt_datasum: 44707802534SQu Wenruo if (btrfs_test_opt(root, NODATASUM)) { 448d399167dSQu Wenruo if (btrfs_test_opt(root, NODATACOW)) 449d399167dSQu Wenruo btrfs_info(root->fs_info, "setting datasum, datacow enabled"); 450d399167dSQu Wenruo else 451d399167dSQu Wenruo btrfs_info(root->fs_info, "setting datasum"); 45207802534SQu Wenruo } 453d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATACOW); 454d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATASUM); 455d399167dSQu Wenruo break; 456be20aa9dSChris Mason case Opt_nodatacow: 45707802534SQu Wenruo if (!btrfs_test_opt(root, NODATACOW)) { 458bedb2ccaSAndrei Popa if (!btrfs_test_opt(root, COMPRESS) || 459bedb2ccaSAndrei Popa !btrfs_test_opt(root, FORCE_COMPRESS)) { 460efe120a0SFrank Holton btrfs_info(root->fs_info, 461efe120a0SFrank Holton "setting nodatacow, compression disabled"); 462bedb2ccaSAndrei Popa } else { 463efe120a0SFrank Holton btrfs_info(root->fs_info, "setting nodatacow"); 464bedb2ccaSAndrei Popa } 46507802534SQu Wenruo } 466bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, COMPRESS); 467bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 468be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 469be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 470b6cda9bcSChris Mason break; 471a258af7aSQu Wenruo case Opt_datacow: 47207802534SQu Wenruo btrfs_clear_and_info(root, NODATACOW, 47307802534SQu Wenruo "setting datacow"); 474a258af7aSQu Wenruo break; 475a555f810SChris Mason case Opt_compress_force: 476261507a0SLi Zefan case Opt_compress_force_type: 477261507a0SLi Zefan compress_force = true; 4781c697d4aSEric Sandeen /* Fallthrough */ 479261507a0SLi Zefan case Opt_compress: 480261507a0SLi Zefan case Opt_compress_type: 48107802534SQu Wenruo compress = true; 482261507a0SLi Zefan if (token == Opt_compress || 483261507a0SLi Zefan token == Opt_compress_force || 484261507a0SLi Zefan strcmp(args[0].from, "zlib") == 0) { 485261507a0SLi Zefan compress_type = "zlib"; 486261507a0SLi Zefan info->compress_type = BTRFS_COMPRESS_ZLIB; 487063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 488bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 489bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 490a6fa6faeSLi Zefan } else if (strcmp(args[0].from, "lzo") == 0) { 491a6fa6faeSLi Zefan compress_type = "lzo"; 492a6fa6faeSLi Zefan info->compress_type = BTRFS_COMPRESS_LZO; 493063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 494bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 495bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 4962b0ce2c2SMitch Harder btrfs_set_fs_incompat(info, COMPRESS_LZO); 497063849eaSArnd Hannemann } else if (strncmp(args[0].from, "no", 2) == 0) { 498063849eaSArnd Hannemann compress_type = "no"; 499063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, COMPRESS); 500063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 501063849eaSArnd Hannemann compress_force = false; 502261507a0SLi Zefan } else { 503261507a0SLi Zefan ret = -EINVAL; 504261507a0SLi Zefan goto out; 505261507a0SLi Zefan } 506261507a0SLi Zefan 507261507a0SLi Zefan if (compress_force) { 50807802534SQu Wenruo btrfs_set_and_info(root, FORCE_COMPRESS, 50907802534SQu Wenruo "force %s compression", 510261507a0SLi Zefan compress_type); 51107802534SQu Wenruo } else if (compress) { 51207802534SQu Wenruo if (!btrfs_test_opt(root, COMPRESS)) 51307802534SQu Wenruo btrfs_info(root->fs_info, 514351fd353SDavid Sterba "btrfs: use %s compression", 515261507a0SLi Zefan compress_type); 516a7e252afSMiao Xie } 517a555f810SChris Mason break; 518e18e4809SChris Mason case Opt_ssd: 51907802534SQu Wenruo btrfs_set_and_info(root, SSD, 52007802534SQu Wenruo "use ssd allocation scheme"); 521e18e4809SChris Mason break; 522451d7585SChris Mason case Opt_ssd_spread: 52307802534SQu Wenruo btrfs_set_and_info(root, SSD_SPREAD, 52407802534SQu Wenruo "use spread ssd allocation scheme"); 525*2aa06a35SEric Sandeen btrfs_set_opt(info->mount_opt, SSD); 526451d7585SChris Mason break; 5273b30c22fSChris Mason case Opt_nossd: 528*2aa06a35SEric Sandeen btrfs_set_and_info(root, NOSSD, 52907802534SQu Wenruo "not using ssd allocation scheme"); 5303b30c22fSChris Mason btrfs_clear_opt(info->mount_opt, SSD); 5313b30c22fSChris Mason break; 532842bef58SQu Wenruo case Opt_barrier: 53307802534SQu Wenruo btrfs_clear_and_info(root, NOBARRIER, 53407802534SQu Wenruo "turning on barriers"); 535842bef58SQu Wenruo break; 53621ad10cfSChris Mason case Opt_nobarrier: 53707802534SQu Wenruo btrfs_set_and_info(root, NOBARRIER, 53807802534SQu Wenruo "turning off barriers"); 53921ad10cfSChris Mason break; 5404543df7eSChris Mason case Opt_thread_pool: 5412c334e87SWang Shilong ret = match_int(&args[0], &intarg); 5422c334e87SWang Shilong if (ret) { 5432c334e87SWang Shilong goto out; 5442c334e87SWang Shilong } else if (intarg > 0) { 5454543df7eSChris Mason info->thread_pool_size = intarg; 5462c334e87SWang Shilong } else { 5472c334e87SWang Shilong ret = -EINVAL; 5482c334e87SWang Shilong goto out; 5492c334e87SWang Shilong } 5504543df7eSChris Mason break; 5516f568d35SChris Mason case Opt_max_inline: 552edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5536f568d35SChris Mason if (num) { 55491748467SAkinobu Mita info->max_inline = memparse(num, NULL); 5556f568d35SChris Mason kfree(num); 5566f568d35SChris Mason 55715ada040SChris Mason if (info->max_inline) { 558feb5f965SMitch Harder info->max_inline = min_t(u64, 55915ada040SChris Mason info->max_inline, 56015ada040SChris Mason root->sectorsize); 56115ada040SChris Mason } 562efe120a0SFrank Holton btrfs_info(root->fs_info, "max_inline at %llu", 563c1c9ff7cSGeert Uytterhoeven info->max_inline); 5642c334e87SWang Shilong } else { 5652c334e87SWang Shilong ret = -ENOMEM; 5662c334e87SWang Shilong goto out; 5676f568d35SChris Mason } 5686f568d35SChris Mason break; 5698f662a76SChris Mason case Opt_alloc_start: 570edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5718f662a76SChris Mason if (num) { 572c018daecSMiao Xie mutex_lock(&info->chunk_mutex); 57391748467SAkinobu Mita info->alloc_start = memparse(num, NULL); 574c018daecSMiao Xie mutex_unlock(&info->chunk_mutex); 5758f662a76SChris Mason kfree(num); 576efe120a0SFrank Holton btrfs_info(root->fs_info, "allocations start at %llu", 577c1c9ff7cSGeert Uytterhoeven info->alloc_start); 5782c334e87SWang Shilong } else { 5792c334e87SWang Shilong ret = -ENOMEM; 5802c334e87SWang Shilong goto out; 5818f662a76SChris Mason } 5828f662a76SChris Mason break; 583bd0330adSQu Wenruo case Opt_acl: 58445ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL 585bd0330adSQu Wenruo root->fs_info->sb->s_flags |= MS_POSIXACL; 586bd0330adSQu Wenruo break; 58745ff35d6SGuangliang Zhao #else 58845ff35d6SGuangliang Zhao btrfs_err(root->fs_info, 58945ff35d6SGuangliang Zhao "support for ACL not compiled in!"); 59045ff35d6SGuangliang Zhao ret = -EINVAL; 59145ff35d6SGuangliang Zhao goto out; 59245ff35d6SGuangliang Zhao #endif 59333268eafSJosef Bacik case Opt_noacl: 59433268eafSJosef Bacik root->fs_info->sb->s_flags &= ~MS_POSIXACL; 59533268eafSJosef Bacik break; 5963a5e1404SSage Weil case Opt_notreelog: 59707802534SQu Wenruo btrfs_set_and_info(root, NOTREELOG, 59807802534SQu Wenruo "disabling tree log"); 5993a5e1404SSage Weil break; 600a88998f2SQu Wenruo case Opt_treelog: 60107802534SQu Wenruo btrfs_clear_and_info(root, NOTREELOG, 60207802534SQu Wenruo "enabling tree log"); 603a88998f2SQu Wenruo break; 604dccae999SSage Weil case Opt_flushoncommit: 60507802534SQu Wenruo btrfs_set_and_info(root, FLUSHONCOMMIT, 60607802534SQu Wenruo "turning on flush-on-commit"); 607dccae999SSage Weil break; 6082c9ee856SQu Wenruo case Opt_noflushoncommit: 60907802534SQu Wenruo btrfs_clear_and_info(root, FLUSHONCOMMIT, 61007802534SQu Wenruo "turning off flush-on-commit"); 6112c9ee856SQu Wenruo break; 61297e728d4SJosef Bacik case Opt_ratio: 6132c334e87SWang Shilong ret = match_int(&args[0], &intarg); 6142c334e87SWang Shilong if (ret) { 6152c334e87SWang Shilong goto out; 6162c334e87SWang Shilong } else if (intarg >= 0) { 61797e728d4SJosef Bacik info->metadata_ratio = intarg; 618efe120a0SFrank Holton btrfs_info(root->fs_info, "metadata ratio %d", 61997e728d4SJosef Bacik info->metadata_ratio); 6202c334e87SWang Shilong } else { 6212c334e87SWang Shilong ret = -EINVAL; 6222c334e87SWang Shilong goto out; 62397e728d4SJosef Bacik } 62497e728d4SJosef Bacik break; 625e244a0aeSChristoph Hellwig case Opt_discard: 62607802534SQu Wenruo btrfs_set_and_info(root, DISCARD, 62707802534SQu Wenruo "turning on discard"); 628e244a0aeSChristoph Hellwig break; 629e07a2adeSQu Wenruo case Opt_nodiscard: 63007802534SQu Wenruo btrfs_clear_and_info(root, DISCARD, 63107802534SQu Wenruo "turning off discard"); 632e07a2adeSQu Wenruo break; 6330af3d00bSJosef Bacik case Opt_space_cache: 63407802534SQu Wenruo btrfs_set_and_info(root, SPACE_CACHE, 63507802534SQu Wenruo "enabling disk space caching"); 6360de90876SJosef Bacik break; 637f420ee1eSStefan Behrens case Opt_rescan_uuid_tree: 638f420ee1eSStefan Behrens btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE); 639f420ee1eSStefan Behrens break; 64073bc1876SJosef Bacik case Opt_no_space_cache: 64107802534SQu Wenruo btrfs_clear_and_info(root, SPACE_CACHE, 64207802534SQu Wenruo "disabling disk space caching"); 64373bc1876SJosef Bacik break; 6444b9465cbSChris Mason case Opt_inode_cache: 64507802534SQu Wenruo btrfs_set_and_info(root, CHANGE_INODE_CACHE, 64607802534SQu Wenruo "enabling inode map caching"); 6473818aea2SQu Wenruo break; 6483818aea2SQu Wenruo case Opt_noinode_cache: 64907802534SQu Wenruo btrfs_clear_and_info(root, CHANGE_INODE_CACHE, 65007802534SQu Wenruo "disabling inode map caching"); 6514b9465cbSChris Mason break; 65288c2ba3bSJosef Bacik case Opt_clear_cache: 65307802534SQu Wenruo btrfs_set_and_info(root, CLEAR_CACHE, 65407802534SQu Wenruo "force clearing of disk cache"); 6550af3d00bSJosef Bacik break; 6564260f7c7SSage Weil case Opt_user_subvol_rm_allowed: 6574260f7c7SSage Weil btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 6584260f7c7SSage Weil break; 65991435650SChris Mason case Opt_enospc_debug: 66091435650SChris Mason btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 66191435650SChris Mason break; 66253036293SQu Wenruo case Opt_noenospc_debug: 66353036293SQu Wenruo btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG); 66453036293SQu Wenruo break; 6654cb5300bSChris Mason case Opt_defrag: 66607802534SQu Wenruo btrfs_set_and_info(root, AUTO_DEFRAG, 66707802534SQu Wenruo "enabling auto defrag"); 6684cb5300bSChris Mason break; 669fc0ca9afSQu Wenruo case Opt_nodefrag: 67007802534SQu Wenruo btrfs_clear_and_info(root, AUTO_DEFRAG, 67107802534SQu Wenruo "disabling auto defrag"); 672fc0ca9afSQu Wenruo break; 673af31f5e5SChris Mason case Opt_recovery: 674efe120a0SFrank Holton btrfs_info(root->fs_info, "enabling auto recovery"); 675af31f5e5SChris Mason btrfs_set_opt(info->mount_opt, RECOVERY); 676af31f5e5SChris Mason break; 6779555c6c1SIlya Dryomov case Opt_skip_balance: 6789555c6c1SIlya Dryomov btrfs_set_opt(info->mount_opt, SKIP_BALANCE); 6799555c6c1SIlya Dryomov break; 68021adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 68121adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 682efe120a0SFrank Holton btrfs_info(root->fs_info, 683efe120a0SFrank Holton "enabling check integrity including extent data"); 68421adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, 68521adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA); 68621adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 68721adbd5cSStefan Behrens break; 68821adbd5cSStefan Behrens case Opt_check_integrity: 689efe120a0SFrank Holton btrfs_info(root->fs_info, "enabling check integrity"); 69021adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 69121adbd5cSStefan Behrens break; 69221adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 6932c334e87SWang Shilong ret = match_int(&args[0], &intarg); 6942c334e87SWang Shilong if (ret) { 6952c334e87SWang Shilong goto out; 6962c334e87SWang Shilong } else if (intarg >= 0) { 69721adbd5cSStefan Behrens info->check_integrity_print_mask = intarg; 698efe120a0SFrank Holton btrfs_info(root->fs_info, "check_integrity_print_mask 0x%x", 69921adbd5cSStefan Behrens info->check_integrity_print_mask); 7002c334e87SWang Shilong } else { 7012c334e87SWang Shilong ret = -EINVAL; 7022c334e87SWang Shilong goto out; 70321adbd5cSStefan Behrens } 70421adbd5cSStefan Behrens break; 70521adbd5cSStefan Behrens #else 70621adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 70721adbd5cSStefan Behrens case Opt_check_integrity: 70821adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 709efe120a0SFrank Holton btrfs_err(root->fs_info, 710efe120a0SFrank Holton "support for check_integrity* not compiled in!"); 71121adbd5cSStefan Behrens ret = -EINVAL; 71221adbd5cSStefan Behrens goto out; 71321adbd5cSStefan Behrens #endif 7148c342930SJeff Mahoney case Opt_fatal_errors: 7158c342930SJeff Mahoney if (strcmp(args[0].from, "panic") == 0) 7168c342930SJeff Mahoney btrfs_set_opt(info->mount_opt, 7178c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7188c342930SJeff Mahoney else if (strcmp(args[0].from, "bug") == 0) 7198c342930SJeff Mahoney btrfs_clear_opt(info->mount_opt, 7208c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7218c342930SJeff Mahoney else { 7228c342930SJeff Mahoney ret = -EINVAL; 7238c342930SJeff Mahoney goto out; 7248c342930SJeff Mahoney } 7258c342930SJeff Mahoney break; 7268b87dc17SDavid Sterba case Opt_commit_interval: 7278b87dc17SDavid Sterba intarg = 0; 7288b87dc17SDavid Sterba ret = match_int(&args[0], &intarg); 7298b87dc17SDavid Sterba if (ret < 0) { 730efe120a0SFrank Holton btrfs_err(root->fs_info, "invalid commit interval"); 7318b87dc17SDavid Sterba ret = -EINVAL; 7328b87dc17SDavid Sterba goto out; 7338b87dc17SDavid Sterba } 7348b87dc17SDavid Sterba if (intarg > 0) { 7358b87dc17SDavid Sterba if (intarg > 300) { 736efe120a0SFrank Holton btrfs_warn(root->fs_info, "excessive commit interval %d", 7378b87dc17SDavid Sterba intarg); 7388b87dc17SDavid Sterba } 7398b87dc17SDavid Sterba info->commit_interval = intarg; 7408b87dc17SDavid Sterba } else { 741efe120a0SFrank Holton btrfs_info(root->fs_info, "using default commit interval %ds", 7428b87dc17SDavid Sterba BTRFS_DEFAULT_COMMIT_INTERVAL); 7438b87dc17SDavid Sterba info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 7448b87dc17SDavid Sterba } 7458b87dc17SDavid Sterba break; 746a7a3f7caSSage Weil case Opt_err: 747efe120a0SFrank Holton btrfs_info(root->fs_info, "unrecognized mount option '%s'", p); 748a7a3f7caSSage Weil ret = -EINVAL; 749a7a3f7caSSage Weil goto out; 75095e05289SChris Mason default: 751be20aa9dSChris Mason break; 75295e05289SChris Mason } 75395e05289SChris Mason } 754a7a3f7caSSage Weil out: 75573bc1876SJosef Bacik if (!ret && btrfs_test_opt(root, SPACE_CACHE)) 756efe120a0SFrank Holton btrfs_info(root->fs_info, "disk space caching is enabled"); 757da495eccSJosef Bacik kfree(orig); 758a7a3f7caSSage Weil return ret; 759edf24abeSChristoph Hellwig } 760edf24abeSChristoph Hellwig 761edf24abeSChristoph Hellwig /* 762edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 763edf24abeSChristoph Hellwig * 764edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 765edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 766edf24abeSChristoph Hellwig */ 76797288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 76873f73415SJosef Bacik void *holder, char **subvol_name, u64 *subvol_objectid, 7695e2a4b25SDavid Sterba struct btrfs_fs_devices **fs_devices) 770edf24abeSChristoph Hellwig { 771edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 77283c8c9bdSJeff Liu char *device_name, *opts, *orig, *p; 7731493381fSWang Shilong char *num = NULL; 774edf24abeSChristoph Hellwig int error = 0; 775edf24abeSChristoph Hellwig 776edf24abeSChristoph Hellwig if (!options) 777830c4adbSJosef Bacik return 0; 778edf24abeSChristoph Hellwig 779edf24abeSChristoph Hellwig /* 780edf24abeSChristoph Hellwig * strsep changes the string, duplicate it because parse_options 781edf24abeSChristoph Hellwig * gets called twice 782edf24abeSChristoph Hellwig */ 783edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 784edf24abeSChristoph Hellwig if (!opts) 785edf24abeSChristoph Hellwig return -ENOMEM; 7863f3d0bc0STero Roponen orig = opts; 787edf24abeSChristoph Hellwig 788edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 789edf24abeSChristoph Hellwig int token; 790edf24abeSChristoph Hellwig if (!*p) 791edf24abeSChristoph Hellwig continue; 792edf24abeSChristoph Hellwig 793edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 794edf24abeSChristoph Hellwig switch (token) { 795edf24abeSChristoph Hellwig case Opt_subvol: 796a90e8b6fSIlya Dryomov kfree(*subvol_name); 797edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 7982c334e87SWang Shilong if (!*subvol_name) { 7992c334e87SWang Shilong error = -ENOMEM; 8002c334e87SWang Shilong goto out; 8012c334e87SWang Shilong } 802edf24abeSChristoph Hellwig break; 80373f73415SJosef Bacik case Opt_subvolid: 8041493381fSWang Shilong num = match_strdup(&args[0]); 8051493381fSWang Shilong if (num) { 8061493381fSWang Shilong *subvol_objectid = memparse(num, NULL); 8071493381fSWang Shilong kfree(num); 8084849f01dSJosef Bacik /* we want the original fs_tree */ 8091493381fSWang Shilong if (!*subvol_objectid) 8104849f01dSJosef Bacik *subvol_objectid = 8114849f01dSJosef Bacik BTRFS_FS_TREE_OBJECTID; 8122c334e87SWang Shilong } else { 8132c334e87SWang Shilong error = -EINVAL; 8142c334e87SWang Shilong goto out; 8154849f01dSJosef Bacik } 81673f73415SJosef Bacik break; 817e15d0542SXin Zhong case Opt_subvolrootid: 8185e2a4b25SDavid Sterba printk(KERN_WARNING 819efe120a0SFrank Holton "BTRFS: 'subvolrootid' mount option is deprecated and has " 820efe120a0SFrank Holton "no effect\n"); 821e15d0542SXin Zhong break; 82243e570b0SChristoph Hellwig case Opt_device: 82383c8c9bdSJeff Liu device_name = match_strdup(&args[0]); 82483c8c9bdSJeff Liu if (!device_name) { 82583c8c9bdSJeff Liu error = -ENOMEM; 82683c8c9bdSJeff Liu goto out; 82783c8c9bdSJeff Liu } 82883c8c9bdSJeff Liu error = btrfs_scan_one_device(device_name, 82943e570b0SChristoph Hellwig flags, holder, fs_devices); 83083c8c9bdSJeff Liu kfree(device_name); 83143e570b0SChristoph Hellwig if (error) 832830c4adbSJosef Bacik goto out; 83343e570b0SChristoph Hellwig break; 834edf24abeSChristoph Hellwig default: 835edf24abeSChristoph Hellwig break; 836edf24abeSChristoph Hellwig } 837edf24abeSChristoph Hellwig } 838edf24abeSChristoph Hellwig 839edf24abeSChristoph Hellwig out: 840830c4adbSJosef Bacik kfree(orig); 841edf24abeSChristoph Hellwig return error; 84295e05289SChris Mason } 84395e05289SChris Mason 84473f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb, 84573f73415SJosef Bacik u64 subvol_objectid) 84673f73415SJosef Bacik { 847815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 848815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 84973f73415SJosef Bacik struct btrfs_root *new_root; 85073f73415SJosef Bacik struct btrfs_dir_item *di; 85173f73415SJosef Bacik struct btrfs_path *path; 85273f73415SJosef Bacik struct btrfs_key location; 85373f73415SJosef Bacik struct inode *inode; 8543a0dfa6aSJosef Bacik struct dentry *dentry; 85573f73415SJosef Bacik u64 dir_id; 85673f73415SJosef Bacik int new = 0; 85773f73415SJosef Bacik 85873f73415SJosef Bacik /* 85973f73415SJosef Bacik * We have a specific subvol we want to mount, just setup location and 86073f73415SJosef Bacik * go look up the root. 86173f73415SJosef Bacik */ 86273f73415SJosef Bacik if (subvol_objectid) { 86373f73415SJosef Bacik location.objectid = subvol_objectid; 86473f73415SJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 86573f73415SJosef Bacik location.offset = (u64)-1; 86673f73415SJosef Bacik goto find_root; 86773f73415SJosef Bacik } 86873f73415SJosef Bacik 86973f73415SJosef Bacik path = btrfs_alloc_path(); 87073f73415SJosef Bacik if (!path) 87173f73415SJosef Bacik return ERR_PTR(-ENOMEM); 87273f73415SJosef Bacik path->leave_spinning = 1; 87373f73415SJosef Bacik 87473f73415SJosef Bacik /* 87573f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 87673f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 87773f73415SJosef Bacik * to mount. 87873f73415SJosef Bacik */ 879815745cfSAl Viro dir_id = btrfs_super_root_dir(fs_info->super_copy); 88073f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 881b0839166SJulia Lawall if (IS_ERR(di)) { 882b0839166SJulia Lawall btrfs_free_path(path); 883fb4f6f91SDan Carpenter return ERR_CAST(di); 884b0839166SJulia Lawall } 88573f73415SJosef Bacik if (!di) { 88673f73415SJosef Bacik /* 88773f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 88873f73415SJosef Bacik * it's always been there, but don't freak out, just try and 88973f73415SJosef Bacik * mount to root most subvolume. 89073f73415SJosef Bacik */ 89173f73415SJosef Bacik btrfs_free_path(path); 89273f73415SJosef Bacik dir_id = BTRFS_FIRST_FREE_OBJECTID; 893815745cfSAl Viro new_root = fs_info->fs_root; 89473f73415SJosef Bacik goto setup_root; 89573f73415SJosef Bacik } 89673f73415SJosef Bacik 89773f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 89873f73415SJosef Bacik btrfs_free_path(path); 89973f73415SJosef Bacik 90073f73415SJosef Bacik find_root: 901815745cfSAl Viro new_root = btrfs_read_fs_root_no_name(fs_info, &location); 90273f73415SJosef Bacik if (IS_ERR(new_root)) 903d0b678cbSJulia Lawall return ERR_CAST(new_root); 90473f73415SJosef Bacik 90573f73415SJosef Bacik dir_id = btrfs_root_dirid(&new_root->root_item); 90673f73415SJosef Bacik setup_root: 90773f73415SJosef Bacik location.objectid = dir_id; 90873f73415SJosef Bacik location.type = BTRFS_INODE_ITEM_KEY; 90973f73415SJosef Bacik location.offset = 0; 91073f73415SJosef Bacik 91173f73415SJosef Bacik inode = btrfs_iget(sb, &location, new_root, &new); 9124cbd1149SDan Carpenter if (IS_ERR(inode)) 9134cbd1149SDan Carpenter return ERR_CAST(inode); 91473f73415SJosef Bacik 91573f73415SJosef Bacik /* 91673f73415SJosef Bacik * If we're just mounting the root most subvol put the inode and return 91773f73415SJosef Bacik * a reference to the dentry. We will have already gotten a reference 91873f73415SJosef Bacik * to the inode in btrfs_fill_super so we're good to go. 91973f73415SJosef Bacik */ 92073f73415SJosef Bacik if (!new && sb->s_root->d_inode == inode) { 92173f73415SJosef Bacik iput(inode); 92273f73415SJosef Bacik return dget(sb->s_root); 92373f73415SJosef Bacik } 92473f73415SJosef Bacik 9253a0dfa6aSJosef Bacik dentry = d_obtain_alias(inode); 9263a0dfa6aSJosef Bacik if (!IS_ERR(dentry)) { 9273a0dfa6aSJosef Bacik spin_lock(&dentry->d_lock); 9283a0dfa6aSJosef Bacik dentry->d_flags &= ~DCACHE_DISCONNECTED; 9293a0dfa6aSJosef Bacik spin_unlock(&dentry->d_lock); 9303a0dfa6aSJosef Bacik } 9313a0dfa6aSJosef Bacik return dentry; 93273f73415SJosef Bacik } 93373f73415SJosef Bacik 9348a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 9358a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 9368a4b83ccSChris Mason void *data, int silent) 9372e635a27SChris Mason { 9382e635a27SChris Mason struct inode *inode; 939815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 9405d4f98a2SYan Zheng struct btrfs_key key; 94139279cc3SChris Mason int err; 9422e635a27SChris Mason 9432e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 9442e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 945e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 946af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 947be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 9485103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 9492e635a27SChris Mason sb->s_time_gran = 1; 9500eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 95133268eafSJosef Bacik sb->s_flags |= MS_POSIXACL; 95249cf6f45SChris Ball #endif 9530c4d2d95SJosef Bacik sb->s_flags |= MS_I_VERSION; 954ad2b2c80SAl Viro err = open_ctree(sb, fs_devices, (char *)data); 955ad2b2c80SAl Viro if (err) { 956efe120a0SFrank Holton printk(KERN_ERR "BTRFS: open_ctree failed\n"); 957ad2b2c80SAl Viro return err; 958e20d96d6SChris Mason } 959b888db2bSChris Mason 9605d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 9615d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 9625d4f98a2SYan Zheng key.offset = 0; 96398c7089cSAl Viro inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL); 9645d4f98a2SYan Zheng if (IS_ERR(inode)) { 9655d4f98a2SYan Zheng err = PTR_ERR(inode); 96639279cc3SChris Mason goto fail_close; 96739279cc3SChris Mason } 9682e635a27SChris Mason 96948fde701SAl Viro sb->s_root = d_make_root(inode); 97048fde701SAl Viro if (!sb->s_root) { 97139279cc3SChris Mason err = -ENOMEM; 97239279cc3SChris Mason goto fail_close; 9732e635a27SChris Mason } 97458176a96SJosef Bacik 9756885f308SChris Mason save_mount_options(sb, data); 97690a887c9SDan Magenheimer cleancache_init_fs(sb); 97759553edfSAl Viro sb->s_flags |= MS_ACTIVE; 9782e635a27SChris Mason return 0; 9792e635a27SChris Mason 98039279cc3SChris Mason fail_close: 981815745cfSAl Viro close_ctree(fs_info->tree_root); 982d5719762SChris Mason return err; 983d5719762SChris Mason } 984d5719762SChris Mason 9856bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 986d5719762SChris Mason { 987d5719762SChris Mason struct btrfs_trans_handle *trans; 988815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 989815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 990df2ce34cSChris Mason 9911abe9b8aSliubo trace_btrfs_sync_fs(wait); 9921abe9b8aSliubo 993d561c025SChris Mason if (!wait) { 994815745cfSAl Viro filemap_flush(fs_info->btree_inode->i_mapping); 995df2ce34cSChris Mason return 0; 996d561c025SChris Mason } 997771ed689SChris Mason 998b0244199SMiao Xie btrfs_wait_ordered_roots(fs_info, -1); 999771ed689SChris Mason 1000d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 100160376ce4SJosef Bacik if (IS_ERR(trans)) { 1002354aa0fbSMiao Xie /* no transaction, don't bother */ 1003354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 1004bd7de2c9SJosef Bacik return 0; 100598d5dc13STsutomu Itoh return PTR_ERR(trans); 100660376ce4SJosef Bacik } 1007bd7de2c9SJosef Bacik return btrfs_commit_transaction(trans, root); 1008d5719762SChris Mason } 1009d5719762SChris Mason 101034c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 1011a9572a15SEric Paris { 1012815745cfSAl Viro struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 1013815745cfSAl Viro struct btrfs_root *root = info->tree_root; 1014200da64eSTsutomu Itoh char *compress_type; 1015a9572a15SEric Paris 1016a9572a15SEric Paris if (btrfs_test_opt(root, DEGRADED)) 1017a9572a15SEric Paris seq_puts(seq, ",degraded"); 1018a9572a15SEric Paris if (btrfs_test_opt(root, NODATASUM)) 1019a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 1020a9572a15SEric Paris if (btrfs_test_opt(root, NODATACOW)) 1021a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 1022a9572a15SEric Paris if (btrfs_test_opt(root, NOBARRIER)) 1023a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 1024a9572a15SEric Paris if (info->max_inline != 8192 * 1024) 1025c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",max_inline=%llu", info->max_inline); 1026a9572a15SEric Paris if (info->alloc_start != 0) 1027c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",alloc_start=%llu", info->alloc_start); 1028a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 1029a9572a15SEric Paris num_online_cpus() + 2, 8)) 1030a9572a15SEric Paris seq_printf(seq, ",thread_pool=%d", info->thread_pool_size); 1031200da64eSTsutomu Itoh if (btrfs_test_opt(root, COMPRESS)) { 1032200da64eSTsutomu Itoh if (info->compress_type == BTRFS_COMPRESS_ZLIB) 1033200da64eSTsutomu Itoh compress_type = "zlib"; 1034200da64eSTsutomu Itoh else 1035200da64eSTsutomu Itoh compress_type = "lzo"; 1036200da64eSTsutomu Itoh if (btrfs_test_opt(root, FORCE_COMPRESS)) 1037200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 1038200da64eSTsutomu Itoh else 1039200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 1040200da64eSTsutomu Itoh } 1041c289811cSChris Mason if (btrfs_test_opt(root, NOSSD)) 1042c289811cSChris Mason seq_puts(seq, ",nossd"); 1043451d7585SChris Mason if (btrfs_test_opt(root, SSD_SPREAD)) 1044451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 1045451d7585SChris Mason else if (btrfs_test_opt(root, SSD)) 1046a9572a15SEric Paris seq_puts(seq, ",ssd"); 10473a5e1404SSage Weil if (btrfs_test_opt(root, NOTREELOG)) 10486b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 1049dccae999SSage Weil if (btrfs_test_opt(root, FLUSHONCOMMIT)) 10506b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 105120a5239aSMatthew Wilcox if (btrfs_test_opt(root, DISCARD)) 105220a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 1053a9572a15SEric Paris if (!(root->fs_info->sb->s_flags & MS_POSIXACL)) 1054a9572a15SEric Paris seq_puts(seq, ",noacl"); 1055200da64eSTsutomu Itoh if (btrfs_test_opt(root, SPACE_CACHE)) 1056200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 105773bc1876SJosef Bacik else 10588965593eSDavid Sterba seq_puts(seq, ",nospace_cache"); 1059f420ee1eSStefan Behrens if (btrfs_test_opt(root, RESCAN_UUID_TREE)) 1060f420ee1eSStefan Behrens seq_puts(seq, ",rescan_uuid_tree"); 1061200da64eSTsutomu Itoh if (btrfs_test_opt(root, CLEAR_CACHE)) 1062200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 1063200da64eSTsutomu Itoh if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 1064200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 10650942caa3SDavid Sterba if (btrfs_test_opt(root, ENOSPC_DEBUG)) 10660942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 10670942caa3SDavid Sterba if (btrfs_test_opt(root, AUTO_DEFRAG)) 10680942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 10690942caa3SDavid Sterba if (btrfs_test_opt(root, INODE_MAP_CACHE)) 10700942caa3SDavid Sterba seq_puts(seq, ",inode_cache"); 10719555c6c1SIlya Dryomov if (btrfs_test_opt(root, SKIP_BALANCE)) 10729555c6c1SIlya Dryomov seq_puts(seq, ",skip_balance"); 10738507d216SWang Shilong if (btrfs_test_opt(root, RECOVERY)) 10748507d216SWang Shilong seq_puts(seq, ",recovery"); 10758507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 10768507d216SWang Shilong if (btrfs_test_opt(root, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA)) 10778507d216SWang Shilong seq_puts(seq, ",check_int_data"); 10788507d216SWang Shilong else if (btrfs_test_opt(root, CHECK_INTEGRITY)) 10798507d216SWang Shilong seq_puts(seq, ",check_int"); 10808507d216SWang Shilong if (info->check_integrity_print_mask) 10818507d216SWang Shilong seq_printf(seq, ",check_int_print_mask=%d", 10828507d216SWang Shilong info->check_integrity_print_mask); 10838507d216SWang Shilong #endif 10848507d216SWang Shilong if (info->metadata_ratio) 10858507d216SWang Shilong seq_printf(seq, ",metadata_ratio=%d", 10868507d216SWang Shilong info->metadata_ratio); 10878c342930SJeff Mahoney if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR)) 10888c342930SJeff Mahoney seq_puts(seq, ",fatal_errors=panic"); 10898b87dc17SDavid Sterba if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL) 10908b87dc17SDavid Sterba seq_printf(seq, ",commit=%d", info->commit_interval); 1091a9572a15SEric Paris return 0; 1092a9572a15SEric Paris } 1093a9572a15SEric Paris 1094a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 10952e635a27SChris Mason { 1096815745cfSAl Viro struct btrfs_fs_info *p = data; 1097815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(s); 10984b82d6e4SYan 1099815745cfSAl Viro return fs_info->fs_devices == p->fs_devices; 11004b82d6e4SYan } 11014b82d6e4SYan 1102450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 1103450ba0eaSJosef Bacik { 11046de1d09dSAl Viro int err = set_anon_super(s, data); 11056de1d09dSAl Viro if (!err) 1106450ba0eaSJosef Bacik s->s_fs_info = data; 11076de1d09dSAl Viro return err; 1108450ba0eaSJosef Bacik } 1109450ba0eaSJosef Bacik 1110830c4adbSJosef Bacik /* 1111f9d9ef62SDavid Sterba * subvolumes are identified by ino 256 1112f9d9ef62SDavid Sterba */ 1113f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode) 1114f9d9ef62SDavid Sterba { 1115f9d9ef62SDavid Sterba if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 1116f9d9ef62SDavid Sterba return 1; 1117f9d9ef62SDavid Sterba return 0; 1118f9d9ef62SDavid Sterba } 1119f9d9ef62SDavid Sterba 1120f9d9ef62SDavid Sterba /* 1121830c4adbSJosef Bacik * This will strip out the subvol=%s argument for an argument string and add 1122830c4adbSJosef Bacik * subvolid=0 to make sure we get the actual tree root for path walking to the 1123830c4adbSJosef Bacik * subvol we want. 1124830c4adbSJosef Bacik */ 1125830c4adbSJosef Bacik static char *setup_root_args(char *args) 1126830c4adbSJosef Bacik { 1127f60d16a8SJim Meyering unsigned len = strlen(args) + 2 + 1; 1128f60d16a8SJim Meyering char *src, *dst, *buf; 1129830c4adbSJosef Bacik 1130830c4adbSJosef Bacik /* 1131f60d16a8SJim Meyering * We need the same args as before, but with this substitution: 1132f60d16a8SJim Meyering * s!subvol=[^,]+!subvolid=0! 1133830c4adbSJosef Bacik * 1134f60d16a8SJim Meyering * Since the replacement string is up to 2 bytes longer than the 1135f60d16a8SJim Meyering * original, allocate strlen(args) + 2 + 1 bytes. 1136830c4adbSJosef Bacik */ 1137830c4adbSJosef Bacik 1138f60d16a8SJim Meyering src = strstr(args, "subvol="); 1139830c4adbSJosef Bacik /* This shouldn't happen, but just in case.. */ 1140f60d16a8SJim Meyering if (!src) 1141830c4adbSJosef Bacik return NULL; 1142f60d16a8SJim Meyering 1143f60d16a8SJim Meyering buf = dst = kmalloc(len, GFP_NOFS); 1144f60d16a8SJim Meyering if (!buf) 1145f60d16a8SJim Meyering return NULL; 1146830c4adbSJosef Bacik 1147830c4adbSJosef Bacik /* 1148f60d16a8SJim Meyering * If the subvol= arg is not at the start of the string, 1149f60d16a8SJim Meyering * copy whatever precedes it into buf. 1150830c4adbSJosef Bacik */ 1151f60d16a8SJim Meyering if (src != args) { 1152f60d16a8SJim Meyering *src++ = '\0'; 1153f60d16a8SJim Meyering strcpy(buf, args); 1154f60d16a8SJim Meyering dst += strlen(args); 1155830c4adbSJosef Bacik } 1156830c4adbSJosef Bacik 1157f60d16a8SJim Meyering strcpy(dst, "subvolid=0"); 1158f60d16a8SJim Meyering dst += strlen("subvolid=0"); 1159830c4adbSJosef Bacik 1160830c4adbSJosef Bacik /* 1161f60d16a8SJim Meyering * If there is a "," after the original subvol=... string, 1162f60d16a8SJim Meyering * copy that suffix into our buffer. Otherwise, we're done. 1163830c4adbSJosef Bacik */ 1164f60d16a8SJim Meyering src = strchr(src, ','); 1165f60d16a8SJim Meyering if (src) 1166f60d16a8SJim Meyering strcpy(dst, src); 1167830c4adbSJosef Bacik 1168f60d16a8SJim Meyering return buf; 1169830c4adbSJosef Bacik } 1170830c4adbSJosef Bacik 1171830c4adbSJosef Bacik static struct dentry *mount_subvol(const char *subvol_name, int flags, 1172830c4adbSJosef Bacik const char *device_name, char *data) 1173830c4adbSJosef Bacik { 1174830c4adbSJosef Bacik struct dentry *root; 1175830c4adbSJosef Bacik struct vfsmount *mnt; 1176830c4adbSJosef Bacik char *newargs; 1177830c4adbSJosef Bacik 1178830c4adbSJosef Bacik newargs = setup_root_args(data); 1179830c4adbSJosef Bacik if (!newargs) 1180830c4adbSJosef Bacik return ERR_PTR(-ENOMEM); 1181830c4adbSJosef Bacik mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, 1182830c4adbSJosef Bacik newargs); 11830723a047SHarald Hoyer 11840723a047SHarald Hoyer if (PTR_RET(mnt) == -EBUSY) { 11850723a047SHarald Hoyer if (flags & MS_RDONLY) { 11860723a047SHarald Hoyer mnt = vfs_kern_mount(&btrfs_fs_type, flags & ~MS_RDONLY, device_name, 11870723a047SHarald Hoyer newargs); 11880723a047SHarald Hoyer } else { 11890723a047SHarald Hoyer int r; 11900723a047SHarald Hoyer mnt = vfs_kern_mount(&btrfs_fs_type, flags | MS_RDONLY, device_name, 11910723a047SHarald Hoyer newargs); 11920040e606SChristoph Jaeger if (IS_ERR(mnt)) { 11930040e606SChristoph Jaeger kfree(newargs); 11940723a047SHarald Hoyer return ERR_CAST(mnt); 11950040e606SChristoph Jaeger } 11960723a047SHarald Hoyer 11970723a047SHarald Hoyer r = btrfs_remount(mnt->mnt_sb, &flags, NULL); 11980723a047SHarald Hoyer if (r < 0) { 11990723a047SHarald Hoyer /* FIXME: release vfsmount mnt ??*/ 12000040e606SChristoph Jaeger kfree(newargs); 12010723a047SHarald Hoyer return ERR_PTR(r); 12020723a047SHarald Hoyer } 12030723a047SHarald Hoyer } 12040723a047SHarald Hoyer } 12050723a047SHarald Hoyer 12060040e606SChristoph Jaeger kfree(newargs); 12070040e606SChristoph Jaeger 1208830c4adbSJosef Bacik if (IS_ERR(mnt)) 1209830c4adbSJosef Bacik return ERR_CAST(mnt); 1210830c4adbSJosef Bacik 1211ea441d11SAl Viro root = mount_subtree(mnt, subvol_name); 1212830c4adbSJosef Bacik 1213ea441d11SAl Viro if (!IS_ERR(root) && !is_subvolume_inode(root->d_inode)) { 1214ea441d11SAl Viro struct super_block *s = root->d_sb; 1215ea441d11SAl Viro dput(root); 1216ea441d11SAl Viro root = ERR_PTR(-EINVAL); 1217ea441d11SAl Viro deactivate_locked_super(s); 1218efe120a0SFrank Holton printk(KERN_ERR "BTRFS: '%s' is not a valid subvolume\n", 1219f9d9ef62SDavid Sterba subvol_name); 1220f9d9ef62SDavid Sterba } 1221f9d9ef62SDavid Sterba 1222830c4adbSJosef Bacik return root; 1223830c4adbSJosef Bacik } 1224450ba0eaSJosef Bacik 1225edf24abeSChristoph Hellwig /* 1226edf24abeSChristoph Hellwig * Find a superblock for the given device / mount point. 1227edf24abeSChristoph Hellwig * 1228edf24abeSChristoph Hellwig * Note: This is based on get_sb_bdev from fs/super.c with a few additions 1229edf24abeSChristoph Hellwig * for multiple device setup. Make sure to keep it in sync. 1230edf24abeSChristoph Hellwig */ 1231061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1232306e16ceSDavid Sterba const char *device_name, void *data) 12334b82d6e4SYan { 12344b82d6e4SYan struct block_device *bdev = NULL; 12354b82d6e4SYan struct super_block *s; 12364b82d6e4SYan struct dentry *root; 12378a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices = NULL; 1238450ba0eaSJosef Bacik struct btrfs_fs_info *fs_info = NULL; 123997288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 124073f73415SJosef Bacik char *subvol_name = NULL; 124173f73415SJosef Bacik u64 subvol_objectid = 0; 12424b82d6e4SYan int error = 0; 12434b82d6e4SYan 124497288f2cSChristoph Hellwig if (!(flags & MS_RDONLY)) 124597288f2cSChristoph Hellwig mode |= FMODE_WRITE; 124697288f2cSChristoph Hellwig 124797288f2cSChristoph Hellwig error = btrfs_parse_early_options(data, mode, fs_type, 124873f73415SJosef Bacik &subvol_name, &subvol_objectid, 12495e2a4b25SDavid Sterba &fs_devices); 1250f23c8af8SIlya Dryomov if (error) { 1251f23c8af8SIlya Dryomov kfree(subvol_name); 1252061dbc6bSAl Viro return ERR_PTR(error); 1253f23c8af8SIlya Dryomov } 1254edf24abeSChristoph Hellwig 1255830c4adbSJosef Bacik if (subvol_name) { 1256830c4adbSJosef Bacik root = mount_subvol(subvol_name, flags, device_name, data); 1257830c4adbSJosef Bacik kfree(subvol_name); 1258830c4adbSJosef Bacik return root; 1259830c4adbSJosef Bacik } 1260830c4adbSJosef Bacik 1261306e16ceSDavid Sterba error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices); 12628a4b83ccSChris Mason if (error) 1263830c4adbSJosef Bacik return ERR_PTR(error); 12644b82d6e4SYan 1265450ba0eaSJosef Bacik /* 1266450ba0eaSJosef Bacik * Setup a dummy root and fs_info for test/set super. This is because 1267450ba0eaSJosef Bacik * we don't actually fill this stuff out until open_ctree, but we need 1268450ba0eaSJosef Bacik * it for searching for existing supers, so this lets us do that and 1269450ba0eaSJosef Bacik * then open_ctree will properly initialize everything later. 1270450ba0eaSJosef Bacik */ 1271450ba0eaSJosef Bacik fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS); 127204d21a24SIlya Dryomov if (!fs_info) 127304d21a24SIlya Dryomov return ERR_PTR(-ENOMEM); 127404d21a24SIlya Dryomov 1275450ba0eaSJosef Bacik fs_info->fs_devices = fs_devices; 1276450ba0eaSJosef Bacik 12776c41761fSDavid Sterba fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 12786c41761fSDavid Sterba fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 12796c41761fSDavid Sterba if (!fs_info->super_copy || !fs_info->super_for_commit) { 12806c41761fSDavid Sterba error = -ENOMEM; 128104d21a24SIlya Dryomov goto error_fs_info; 128204d21a24SIlya Dryomov } 128304d21a24SIlya Dryomov 128404d21a24SIlya Dryomov error = btrfs_open_devices(fs_devices, mode, fs_type); 128504d21a24SIlya Dryomov if (error) 128604d21a24SIlya Dryomov goto error_fs_info; 128704d21a24SIlya Dryomov 128804d21a24SIlya Dryomov if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) { 128904d21a24SIlya Dryomov error = -EACCES; 12906c41761fSDavid Sterba goto error_close_devices; 12916c41761fSDavid Sterba } 12926c41761fSDavid Sterba 1293dfe25020SChris Mason bdev = fs_devices->latest_bdev; 12949249e17fSDavid Howells s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC, 12959249e17fSDavid Howells fs_info); 1296830c4adbSJosef Bacik if (IS_ERR(s)) { 1297830c4adbSJosef Bacik error = PTR_ERR(s); 1298830c4adbSJosef Bacik goto error_close_devices; 1299830c4adbSJosef Bacik } 13004b82d6e4SYan 13014b82d6e4SYan if (s->s_root) { 13022b82032cSYan Zheng btrfs_close_devices(fs_devices); 13036c41761fSDavid Sterba free_fs_info(fs_info); 130459553edfSAl Viro if ((flags ^ s->s_flags) & MS_RDONLY) 130559553edfSAl Viro error = -EBUSY; 13064b82d6e4SYan } else { 13074b82d6e4SYan char b[BDEVNAME_SIZE]; 13084b82d6e4SYan 13094b82d6e4SYan strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); 1310815745cfSAl Viro btrfs_sb(s)->bdev_holder = fs_type; 13118a4b83ccSChris Mason error = btrfs_fill_super(s, fs_devices, data, 13128a4b83ccSChris Mason flags & MS_SILENT ? 1 : 0); 13134b82d6e4SYan } 13144b82d6e4SYan 131559553edfSAl Viro root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error); 131659553edfSAl Viro if (IS_ERR(root)) 1317e15d0542SXin Zhong deactivate_locked_super(s); 13184b82d6e4SYan 1319061dbc6bSAl Viro return root; 13204b82d6e4SYan 1321c146afadSYan Zheng error_close_devices: 13228a4b83ccSChris Mason btrfs_close_devices(fs_devices); 132304d21a24SIlya Dryomov error_fs_info: 13246c41761fSDavid Sterba free_fs_info(fs_info); 1325061dbc6bSAl Viro return ERR_PTR(error); 13264b82d6e4SYan } 13272e635a27SChris Mason 13280d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 13290d2450abSSergei Trofimovich int new_pool_size, int old_pool_size) 13300d2450abSSergei Trofimovich { 13310d2450abSSergei Trofimovich if (new_pool_size == old_pool_size) 13320d2450abSSergei Trofimovich return; 13330d2450abSSergei Trofimovich 13340d2450abSSergei Trofimovich fs_info->thread_pool_size = new_pool_size; 13350d2450abSSergei Trofimovich 1336efe120a0SFrank Holton btrfs_info(fs_info, "resize thread pool %d -> %d", 13370d2450abSSergei Trofimovich old_pool_size, new_pool_size); 13380d2450abSSergei Trofimovich 13395cdc7ad3SQu Wenruo btrfs_workqueue_set_max(fs_info->workers, new_pool_size); 1340afe3d242SQu Wenruo btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size); 1341a8c93d4eSQu Wenruo btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size); 1342e66f0bb1SQu Wenruo btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size); 1343fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size); 1344fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size); 1345fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_write_workers, 1346fccb5d86SQu Wenruo new_pool_size); 1347fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size); 1348fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size); 13495b3bc44eSQu Wenruo btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size); 1350736cfa15SQu Wenruo btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size); 13510339ef2fSQu Wenruo btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers, 1352ff023aacSStefan Behrens new_pool_size); 13530d2450abSSergei Trofimovich } 13540d2450abSSergei Trofimovich 1355f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info) 1356dc81cdc5SMiao Xie { 1357dc81cdc5SMiao Xie set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1358f42a34b2SMiao Xie } 1359dc81cdc5SMiao Xie 1360f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info, 1361f42a34b2SMiao Xie unsigned long old_opts, int flags) 1362f42a34b2SMiao Xie { 1363dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1364dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1365dc81cdc5SMiao Xie (flags & MS_RDONLY))) { 1366dc81cdc5SMiao Xie /* wait for any defraggers to finish */ 1367dc81cdc5SMiao Xie wait_event(fs_info->transaction_wait, 1368dc81cdc5SMiao Xie (atomic_read(&fs_info->defrag_running) == 0)); 1369dc81cdc5SMiao Xie if (flags & MS_RDONLY) 1370dc81cdc5SMiao Xie sync_filesystem(fs_info->sb); 1371dc81cdc5SMiao Xie } 1372dc81cdc5SMiao Xie } 1373dc81cdc5SMiao Xie 1374dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info, 1375dc81cdc5SMiao Xie unsigned long old_opts) 1376dc81cdc5SMiao Xie { 1377dc81cdc5SMiao Xie /* 1378dc81cdc5SMiao Xie * We need cleanup all defragable inodes if the autodefragment is 1379dc81cdc5SMiao Xie * close or the fs is R/O. 1380dc81cdc5SMiao Xie */ 1381dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1382dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1383dc81cdc5SMiao Xie (fs_info->sb->s_flags & MS_RDONLY))) { 1384dc81cdc5SMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 1385dc81cdc5SMiao Xie } 1386dc81cdc5SMiao Xie 1387dc81cdc5SMiao Xie clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1388dc81cdc5SMiao Xie } 1389dc81cdc5SMiao Xie 1390c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1391c146afadSYan Zheng { 1392815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1393815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 139449b25e05SJeff Mahoney unsigned old_flags = sb->s_flags; 139549b25e05SJeff Mahoney unsigned long old_opts = fs_info->mount_opt; 139649b25e05SJeff Mahoney unsigned long old_compress_type = fs_info->compress_type; 139749b25e05SJeff Mahoney u64 old_max_inline = fs_info->max_inline; 139849b25e05SJeff Mahoney u64 old_alloc_start = fs_info->alloc_start; 139949b25e05SJeff Mahoney int old_thread_pool_size = fs_info->thread_pool_size; 140049b25e05SJeff Mahoney unsigned int old_metadata_ratio = fs_info->metadata_ratio; 1401c146afadSYan Zheng int ret; 1402c146afadSYan Zheng 140302b9984dSTheodore Ts'o sync_filesystem(sb); 1404f42a34b2SMiao Xie btrfs_remount_prepare(fs_info); 1405dc81cdc5SMiao Xie 1406b288052eSChris Mason ret = btrfs_parse_options(root, data); 140749b25e05SJeff Mahoney if (ret) { 140849b25e05SJeff Mahoney ret = -EINVAL; 140949b25e05SJeff Mahoney goto restore; 141049b25e05SJeff Mahoney } 1411b288052eSChris Mason 1412f42a34b2SMiao Xie btrfs_remount_begin(fs_info, old_opts, *flags); 14130d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 14140d2450abSSergei Trofimovich fs_info->thread_pool_size, old_thread_pool_size); 14150d2450abSSergei Trofimovich 1416c146afadSYan Zheng if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 1417dc81cdc5SMiao Xie goto out; 1418c146afadSYan Zheng 1419c146afadSYan Zheng if (*flags & MS_RDONLY) { 14208dabb742SStefan Behrens /* 14218dabb742SStefan Behrens * this also happens on 'umount -rf' or on shutdown, when 14228dabb742SStefan Behrens * the filesystem is busy. 14238dabb742SStefan Behrens */ 142421c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 1425361c093dSStefan Behrens 1426361c093dSStefan Behrens /* wait for the uuid_scan task to finish */ 1427361c093dSStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 1428361c093dSStefan Behrens /* avoid complains from lockdep et al. */ 1429361c093dSStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 1430361c093dSStefan Behrens 1431c146afadSYan Zheng sb->s_flags |= MS_RDONLY; 1432c146afadSYan Zheng 14338dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 14348dabb742SStefan Behrens btrfs_scrub_cancel(fs_info); 1435061594efSMiao Xie btrfs_pause_balance(fs_info); 14368dabb742SStefan Behrens 1437c146afadSYan Zheng ret = btrfs_commit_super(root); 143849b25e05SJeff Mahoney if (ret) 143949b25e05SJeff Mahoney goto restore; 1440c146afadSYan Zheng } else { 14416ef3de9cSDavid Sterba if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 14426ef3de9cSDavid Sterba btrfs_err(fs_info, 1443efe120a0SFrank Holton "Remounting read-write after error is not allowed"); 14446ef3de9cSDavid Sterba ret = -EINVAL; 14456ef3de9cSDavid Sterba goto restore; 14466ef3de9cSDavid Sterba } 14478a3db184SSergei Trofimovich if (fs_info->fs_devices->rw_devices == 0) { 144849b25e05SJeff Mahoney ret = -EACCES; 144949b25e05SJeff Mahoney goto restore; 14508a3db184SSergei Trofimovich } 14512b82032cSYan Zheng 1452292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 1453292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 1454292fd7fcSStefan Behrens !(*flags & MS_RDONLY)) { 1455efe120a0SFrank Holton btrfs_warn(fs_info, 1456efe120a0SFrank Holton "too many missing devices, writeable remount is not allowed"); 1457292fd7fcSStefan Behrens ret = -EACCES; 1458292fd7fcSStefan Behrens goto restore; 1459292fd7fcSStefan Behrens } 1460292fd7fcSStefan Behrens 14618a3db184SSergei Trofimovich if (btrfs_super_log_root(fs_info->super_copy) != 0) { 146249b25e05SJeff Mahoney ret = -EINVAL; 146349b25e05SJeff Mahoney goto restore; 14648a3db184SSergei Trofimovich } 1465c146afadSYan Zheng 1466815745cfSAl Viro ret = btrfs_cleanup_fs_roots(fs_info); 146749b25e05SJeff Mahoney if (ret) 146849b25e05SJeff Mahoney goto restore; 1469c146afadSYan Zheng 1470d68fc57bSYan, Zheng /* recover relocation */ 14715f316481SWang Shilong mutex_lock(&fs_info->cleaner_mutex); 1472d68fc57bSYan, Zheng ret = btrfs_recover_relocation(root); 14735f316481SWang Shilong mutex_unlock(&fs_info->cleaner_mutex); 147449b25e05SJeff Mahoney if (ret) 147549b25e05SJeff Mahoney goto restore; 1476c146afadSYan Zheng 14772b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 14782b6ba629SIlya Dryomov if (ret) 14792b6ba629SIlya Dryomov goto restore; 14802b6ba629SIlya Dryomov 14818dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 14828dabb742SStefan Behrens if (ret) { 1483efe120a0SFrank Holton btrfs_warn(fs_info, "failed to resume dev_replace"); 14848dabb742SStefan Behrens goto restore; 14858dabb742SStefan Behrens } 148694aebfb2SJosef Bacik 148794aebfb2SJosef Bacik if (!fs_info->uuid_root) { 1488efe120a0SFrank Holton btrfs_info(fs_info, "creating UUID tree"); 148994aebfb2SJosef Bacik ret = btrfs_create_uuid_tree(fs_info); 149094aebfb2SJosef Bacik if (ret) { 1491efe120a0SFrank Holton btrfs_warn(fs_info, "failed to create the UUID tree %d", ret); 149294aebfb2SJosef Bacik goto restore; 149394aebfb2SJosef Bacik } 149494aebfb2SJosef Bacik } 1495c146afadSYan Zheng sb->s_flags &= ~MS_RDONLY; 1496c146afadSYan Zheng } 1497dc81cdc5SMiao Xie out: 14982c6a92b0SJustin Maggard wake_up_process(fs_info->transaction_kthread); 1499dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 1500c146afadSYan Zheng return 0; 150149b25e05SJeff Mahoney 150249b25e05SJeff Mahoney restore: 150349b25e05SJeff Mahoney /* We've hit an error - don't reset MS_RDONLY */ 150449b25e05SJeff Mahoney if (sb->s_flags & MS_RDONLY) 150549b25e05SJeff Mahoney old_flags |= MS_RDONLY; 150649b25e05SJeff Mahoney sb->s_flags = old_flags; 150749b25e05SJeff Mahoney fs_info->mount_opt = old_opts; 150849b25e05SJeff Mahoney fs_info->compress_type = old_compress_type; 150949b25e05SJeff Mahoney fs_info->max_inline = old_max_inline; 1510c018daecSMiao Xie mutex_lock(&fs_info->chunk_mutex); 151149b25e05SJeff Mahoney fs_info->alloc_start = old_alloc_start; 1512c018daecSMiao Xie mutex_unlock(&fs_info->chunk_mutex); 15130d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 15140d2450abSSergei Trofimovich old_thread_pool_size, fs_info->thread_pool_size); 151549b25e05SJeff Mahoney fs_info->metadata_ratio = old_metadata_ratio; 1516dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 151749b25e05SJeff Mahoney return ret; 1518c146afadSYan Zheng } 1519c146afadSYan Zheng 1520bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 1521bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1, 1522bcd53741SArne Jansen const void *dev_info2) 1523bcd53741SArne Jansen { 1524bcd53741SArne Jansen if (((struct btrfs_device_info *)dev_info1)->max_avail > 1525bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1526bcd53741SArne Jansen return -1; 1527bcd53741SArne Jansen else if (((struct btrfs_device_info *)dev_info1)->max_avail < 1528bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1529bcd53741SArne Jansen return 1; 1530bcd53741SArne Jansen else 1531bcd53741SArne Jansen return 0; 1532bcd53741SArne Jansen } 1533bcd53741SArne Jansen 1534bcd53741SArne Jansen /* 1535bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 1536bcd53741SArne Jansen * is stored.(Descending Sort) 1537bcd53741SArne Jansen */ 1538bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 1539bcd53741SArne Jansen struct btrfs_device_info *devices, 1540bcd53741SArne Jansen size_t nr_devices) 1541bcd53741SArne Jansen { 1542bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 1543bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 1544bcd53741SArne Jansen } 1545bcd53741SArne Jansen 15466d07bcecSMiao Xie /* 15476d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 15486d07bcecSMiao Xie * file data. 15496d07bcecSMiao Xie */ 15506d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes) 15516d07bcecSMiao Xie { 15526d07bcecSMiao Xie struct btrfs_fs_info *fs_info = root->fs_info; 15536d07bcecSMiao Xie struct btrfs_device_info *devices_info; 15546d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 15556d07bcecSMiao Xie struct btrfs_device *device; 15566d07bcecSMiao Xie u64 skip_space; 15576d07bcecSMiao Xie u64 type; 15586d07bcecSMiao Xie u64 avail_space; 15596d07bcecSMiao Xie u64 used_space; 15606d07bcecSMiao Xie u64 min_stripe_size; 156139fb26c3SMiao Xie int min_stripes = 1, num_stripes = 1; 15626d07bcecSMiao Xie int i = 0, nr_devices; 15636d07bcecSMiao Xie int ret; 15646d07bcecSMiao Xie 1565b772a86eSLi Zefan nr_devices = fs_info->fs_devices->open_devices; 15666d07bcecSMiao Xie BUG_ON(!nr_devices); 15676d07bcecSMiao Xie 1568d9b0d9baSDulshani Gunawardhana devices_info = kmalloc_array(nr_devices, sizeof(*devices_info), 15696d07bcecSMiao Xie GFP_NOFS); 15706d07bcecSMiao Xie if (!devices_info) 15716d07bcecSMiao Xie return -ENOMEM; 15726d07bcecSMiao Xie 15736d07bcecSMiao Xie /* calc min stripe number for data space alloction */ 15746d07bcecSMiao Xie type = btrfs_get_alloc_profile(root, 1); 157539fb26c3SMiao Xie if (type & BTRFS_BLOCK_GROUP_RAID0) { 15766d07bcecSMiao Xie min_stripes = 2; 157739fb26c3SMiao Xie num_stripes = nr_devices; 157839fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID1) { 15796d07bcecSMiao Xie min_stripes = 2; 158039fb26c3SMiao Xie num_stripes = 2; 158139fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID10) { 15826d07bcecSMiao Xie min_stripes = 4; 158339fb26c3SMiao Xie num_stripes = 4; 158439fb26c3SMiao Xie } 15856d07bcecSMiao Xie 15866d07bcecSMiao Xie if (type & BTRFS_BLOCK_GROUP_DUP) 15876d07bcecSMiao Xie min_stripe_size = 2 * BTRFS_STRIPE_LEN; 15886d07bcecSMiao Xie else 15896d07bcecSMiao Xie min_stripe_size = BTRFS_STRIPE_LEN; 15906d07bcecSMiao Xie 1591b772a86eSLi Zefan list_for_each_entry(device, &fs_devices->devices, dev_list) { 159263a212abSStefan Behrens if (!device->in_fs_metadata || !device->bdev || 159363a212abSStefan Behrens device->is_tgtdev_for_dev_replace) 15946d07bcecSMiao Xie continue; 15956d07bcecSMiao Xie 15966d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 15976d07bcecSMiao Xie 15986d07bcecSMiao Xie /* align with stripe_len */ 15996d07bcecSMiao Xie do_div(avail_space, BTRFS_STRIPE_LEN); 16006d07bcecSMiao Xie avail_space *= BTRFS_STRIPE_LEN; 16016d07bcecSMiao Xie 16026d07bcecSMiao Xie /* 16036d07bcecSMiao Xie * In order to avoid overwritting the superblock on the drive, 16046d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 16056d07bcecSMiao Xie * allocation. 16066d07bcecSMiao Xie */ 16076d07bcecSMiao Xie skip_space = 1024 * 1024; 16086d07bcecSMiao Xie 16096d07bcecSMiao Xie /* user can set the offset in fs_info->alloc_start. */ 16106d07bcecSMiao Xie if (fs_info->alloc_start + BTRFS_STRIPE_LEN <= 16116d07bcecSMiao Xie device->total_bytes) 16126d07bcecSMiao Xie skip_space = max(fs_info->alloc_start, skip_space); 16136d07bcecSMiao Xie 16146d07bcecSMiao Xie /* 16156d07bcecSMiao Xie * btrfs can not use the free space in [0, skip_space - 1], 16166d07bcecSMiao Xie * we must subtract it from the total. In order to implement 16176d07bcecSMiao Xie * it, we account the used space in this range first. 16186d07bcecSMiao Xie */ 16196d07bcecSMiao Xie ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1, 16206d07bcecSMiao Xie &used_space); 16216d07bcecSMiao Xie if (ret) { 16226d07bcecSMiao Xie kfree(devices_info); 16236d07bcecSMiao Xie return ret; 16246d07bcecSMiao Xie } 16256d07bcecSMiao Xie 16266d07bcecSMiao Xie /* calc the free space in [0, skip_space - 1] */ 16276d07bcecSMiao Xie skip_space -= used_space; 16286d07bcecSMiao Xie 16296d07bcecSMiao Xie /* 16306d07bcecSMiao Xie * we can use the free space in [0, skip_space - 1], subtract 16316d07bcecSMiao Xie * it from the total. 16326d07bcecSMiao Xie */ 16336d07bcecSMiao Xie if (avail_space && avail_space >= skip_space) 16346d07bcecSMiao Xie avail_space -= skip_space; 16356d07bcecSMiao Xie else 16366d07bcecSMiao Xie avail_space = 0; 16376d07bcecSMiao Xie 16386d07bcecSMiao Xie if (avail_space < min_stripe_size) 16396d07bcecSMiao Xie continue; 16406d07bcecSMiao Xie 16416d07bcecSMiao Xie devices_info[i].dev = device; 16426d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 16436d07bcecSMiao Xie 16446d07bcecSMiao Xie i++; 16456d07bcecSMiao Xie } 16466d07bcecSMiao Xie 16476d07bcecSMiao Xie nr_devices = i; 16486d07bcecSMiao Xie 16496d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 16506d07bcecSMiao Xie 16516d07bcecSMiao Xie i = nr_devices - 1; 16526d07bcecSMiao Xie avail_space = 0; 16536d07bcecSMiao Xie while (nr_devices >= min_stripes) { 165439fb26c3SMiao Xie if (num_stripes > nr_devices) 165539fb26c3SMiao Xie num_stripes = nr_devices; 165639fb26c3SMiao Xie 16576d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 16586d07bcecSMiao Xie int j; 16596d07bcecSMiao Xie u64 alloc_size; 16606d07bcecSMiao Xie 166139fb26c3SMiao Xie avail_space += devices_info[i].max_avail * num_stripes; 16626d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 166339fb26c3SMiao Xie for (j = i + 1 - num_stripes; j <= i; j++) 16646d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 16656d07bcecSMiao Xie } 16666d07bcecSMiao Xie i--; 16676d07bcecSMiao Xie nr_devices--; 16686d07bcecSMiao Xie } 16696d07bcecSMiao Xie 16706d07bcecSMiao Xie kfree(devices_info); 16716d07bcecSMiao Xie *free_bytes = avail_space; 16726d07bcecSMiao Xie return 0; 16736d07bcecSMiao Xie } 16746d07bcecSMiao Xie 16758fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 16768fd17795SChris Mason { 1677815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 1678815745cfSAl Viro struct btrfs_super_block *disk_super = fs_info->super_copy; 1679815745cfSAl Viro struct list_head *head = &fs_info->space_info; 1680bd4d1088SJosef Bacik struct btrfs_space_info *found; 1681bd4d1088SJosef Bacik u64 total_used = 0; 16826d07bcecSMiao Xie u64 total_free_data = 0; 1683db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 1684815745cfSAl Viro __be32 *fsid = (__be32 *)fs_info->fsid; 16856d07bcecSMiao Xie int ret; 16868fd17795SChris Mason 16876d07bcecSMiao Xie /* holding chunk_muext to avoid allocating new chunks */ 1688815745cfSAl Viro mutex_lock(&fs_info->chunk_mutex); 1689bd4d1088SJosef Bacik rcu_read_lock(); 169089a55897SJosef Bacik list_for_each_entry_rcu(found, head, list) { 16916d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 16926d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 16936d07bcecSMiao Xie total_free_data -= 16946d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 16956d07bcecSMiao Xie } 16966d07bcecSMiao Xie 1697b742bb82SYan, Zheng total_used += found->disk_used; 169889a55897SJosef Bacik } 1699bd4d1088SJosef Bacik rcu_read_unlock(); 1700bd4d1088SJosef Bacik 17018fd17795SChris Mason buf->f_namelen = BTRFS_NAME_LEN; 1702db94535dSChris Mason buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits; 1703bd4d1088SJosef Bacik buf->f_bfree = buf->f_blocks - (total_used >> bits); 17048fd17795SChris Mason buf->f_bsize = dentry->d_sb->s_blocksize; 17058fd17795SChris Mason buf->f_type = BTRFS_SUPER_MAGIC; 17066d07bcecSMiao Xie buf->f_bavail = total_free_data; 1707815745cfSAl Viro ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data); 17086d07bcecSMiao Xie if (ret) { 1709815745cfSAl Viro mutex_unlock(&fs_info->chunk_mutex); 17106d07bcecSMiao Xie return ret; 17116d07bcecSMiao Xie } 17126d07bcecSMiao Xie buf->f_bavail += total_free_data; 17136d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 1714815745cfSAl Viro mutex_unlock(&fs_info->chunk_mutex); 1715d397712bSChris Mason 17169d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 17179d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 17189d03632eSDavid Woodhouse on a big-endian or little-endian host */ 17199d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 17209d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 172132d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 172232d48fa1SDavid Woodhouse buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32; 172332d48fa1SDavid Woodhouse buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid; 172432d48fa1SDavid Woodhouse 17258fd17795SChris Mason return 0; 17268fd17795SChris Mason } 1727b5133862SChris Mason 1728aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb) 1729aea52e19SAl Viro { 1730815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1731aea52e19SAl Viro kill_anon_super(sb); 1732aea52e19SAl Viro free_fs_info(fs_info); 1733aea52e19SAl Viro } 1734aea52e19SAl Viro 17352e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 17362e635a27SChris Mason .owner = THIS_MODULE, 17372e635a27SChris Mason .name = "btrfs", 1738061dbc6bSAl Viro .mount = btrfs_mount, 1739aea52e19SAl Viro .kill_sb = btrfs_kill_super, 17402e635a27SChris Mason .fs_flags = FS_REQUIRES_DEV, 17412e635a27SChris Mason }; 17427f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs"); 1743a9218f6bSChris Mason 1744d352ac68SChris Mason /* 1745d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 1746d352ac68SChris Mason */ 17478a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 17488a4b83ccSChris Mason unsigned long arg) 17498a4b83ccSChris Mason { 17508a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 17518a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 1752c071fcfdSChris Mason int ret = -ENOTTY; 17538a4b83ccSChris Mason 1754e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1755e441d54dSChris Mason return -EPERM; 1756e441d54dSChris Mason 1757dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 1758dae7b665SLi Zefan if (IS_ERR(vol)) 1759dae7b665SLi Zefan return PTR_ERR(vol); 1760c071fcfdSChris Mason 17618a4b83ccSChris Mason switch (cmd) { 17628a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 176397288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 17648a4b83ccSChris Mason &btrfs_fs_type, &fs_devices); 17658a4b83ccSChris Mason break; 176602db0844SJosef Bacik case BTRFS_IOC_DEVICES_READY: 176702db0844SJosef Bacik ret = btrfs_scan_one_device(vol->name, FMODE_READ, 176802db0844SJosef Bacik &btrfs_fs_type, &fs_devices); 176902db0844SJosef Bacik if (ret) 177002db0844SJosef Bacik break; 177102db0844SJosef Bacik ret = !(fs_devices->num_devices == fs_devices->total_devices); 177202db0844SJosef Bacik break; 17738a4b83ccSChris Mason } 1774dae7b665SLi Zefan 17758a4b83ccSChris Mason kfree(vol); 1776f819d837SLinda Knippers return ret; 17778a4b83ccSChris Mason } 17788a4b83ccSChris Mason 17790176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 1780ed0dab6bSYan { 1781354aa0fbSMiao Xie struct btrfs_trans_handle *trans; 1782354aa0fbSMiao Xie struct btrfs_root *root = btrfs_sb(sb)->tree_root; 1783354aa0fbSMiao Xie 1784d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 1785354aa0fbSMiao Xie if (IS_ERR(trans)) { 1786354aa0fbSMiao Xie /* no transaction, don't bother */ 1787354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 17880176260fSLinus Torvalds return 0; 1789354aa0fbSMiao Xie return PTR_ERR(trans); 1790354aa0fbSMiao Xie } 1791354aa0fbSMiao Xie return btrfs_commit_transaction(trans, root); 1792ed0dab6bSYan } 1793ed0dab6bSYan 17940176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb) 1795ed0dab6bSYan { 17960176260fSLinus Torvalds return 0; 1797ed0dab6bSYan } 17982e635a27SChris Mason 17999c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 18009c5085c1SJosef Bacik { 18019c5085c1SJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 18029c5085c1SJosef Bacik struct btrfs_fs_devices *cur_devices; 18039c5085c1SJosef Bacik struct btrfs_device *dev, *first_dev = NULL; 18049c5085c1SJosef Bacik struct list_head *head; 18059c5085c1SJosef Bacik struct rcu_string *name; 18069c5085c1SJosef Bacik 18079c5085c1SJosef Bacik mutex_lock(&fs_info->fs_devices->device_list_mutex); 18089c5085c1SJosef Bacik cur_devices = fs_info->fs_devices; 18099c5085c1SJosef Bacik while (cur_devices) { 18109c5085c1SJosef Bacik head = &cur_devices->devices; 18119c5085c1SJosef Bacik list_for_each_entry(dev, head, dev_list) { 1812aa9ddcd4SJosef Bacik if (dev->missing) 1813aa9ddcd4SJosef Bacik continue; 18149c5085c1SJosef Bacik if (!first_dev || dev->devid < first_dev->devid) 18159c5085c1SJosef Bacik first_dev = dev; 18169c5085c1SJosef Bacik } 18179c5085c1SJosef Bacik cur_devices = cur_devices->seed; 18189c5085c1SJosef Bacik } 18199c5085c1SJosef Bacik 18209c5085c1SJosef Bacik if (first_dev) { 18219c5085c1SJosef Bacik rcu_read_lock(); 18229c5085c1SJosef Bacik name = rcu_dereference(first_dev->name); 18239c5085c1SJosef Bacik seq_escape(m, name->str, " \t\n\\"); 18249c5085c1SJosef Bacik rcu_read_unlock(); 18259c5085c1SJosef Bacik } else { 18269c5085c1SJosef Bacik WARN_ON(1); 18279c5085c1SJosef Bacik } 18289c5085c1SJosef Bacik mutex_unlock(&fs_info->fs_devices->device_list_mutex); 18299c5085c1SJosef Bacik return 0; 18309c5085c1SJosef Bacik } 18319c5085c1SJosef Bacik 1832b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 183376dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 1834bd555975SAl Viro .evict_inode = btrfs_evict_inode, 1835e20d96d6SChris Mason .put_super = btrfs_put_super, 1836d5719762SChris Mason .sync_fs = btrfs_sync_fs, 1837a9572a15SEric Paris .show_options = btrfs_show_options, 18389c5085c1SJosef Bacik .show_devname = btrfs_show_devname, 18394730a4bcSChris Mason .write_inode = btrfs_write_inode, 18402c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 18412c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 18428fd17795SChris Mason .statfs = btrfs_statfs, 1843c146afadSYan Zheng .remount_fs = btrfs_remount, 18440176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 18450176260fSLinus Torvalds .unfreeze_fs = btrfs_unfreeze, 1846e20d96d6SChris Mason }; 1847a9218f6bSChris Mason 1848a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 1849a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 1850a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 1851a9218f6bSChris Mason .owner = THIS_MODULE, 18526038f373SArnd Bergmann .llseek = noop_llseek, 1853a9218f6bSChris Mason }; 1854a9218f6bSChris Mason 1855a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 1856578454ffSKay Sievers .minor = BTRFS_MINOR, 1857a9218f6bSChris Mason .name = "btrfs-control", 1858a9218f6bSChris Mason .fops = &btrfs_ctl_fops 1859a9218f6bSChris Mason }; 1860a9218f6bSChris Mason 1861578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 1862578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 1863578454ffSKay Sievers 1864a9218f6bSChris Mason static int btrfs_interface_init(void) 1865a9218f6bSChris Mason { 1866a9218f6bSChris Mason return misc_register(&btrfs_misc); 1867a9218f6bSChris Mason } 1868a9218f6bSChris Mason 1869b2950863SChristoph Hellwig static void btrfs_interface_exit(void) 1870a9218f6bSChris Mason { 1871a9218f6bSChris Mason if (misc_deregister(&btrfs_misc) < 0) 1872efe120a0SFrank Holton printk(KERN_INFO "BTRFS: misc_deregister failed for control device\n"); 1873a9218f6bSChris Mason } 1874a9218f6bSChris Mason 187585965600SDavid Sterba static void btrfs_print_info(void) 187685965600SDavid Sterba { 187785965600SDavid Sterba printk(KERN_INFO "Btrfs loaded" 187885965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 187985965600SDavid Sterba ", debug=on" 188085965600SDavid Sterba #endif 188179556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT 188279556c3dSStefan Behrens ", assert=on" 188379556c3dSStefan Behrens #endif 188485965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 188585965600SDavid Sterba ", integrity-checker=on" 188685965600SDavid Sterba #endif 188785965600SDavid Sterba "\n"); 188885965600SDavid Sterba } 188985965600SDavid Sterba 1890dc11dd5dSJosef Bacik static int btrfs_run_sanity_tests(void) 1891dc11dd5dSJosef Bacik { 189206ea65a3SJosef Bacik int ret; 189306ea65a3SJosef Bacik 1894294e30feSJosef Bacik ret = btrfs_init_test_fs(); 189506ea65a3SJosef Bacik if (ret) 189606ea65a3SJosef Bacik return ret; 1897294e30feSJosef Bacik 1898294e30feSJosef Bacik ret = btrfs_test_free_space_cache(); 1899294e30feSJosef Bacik if (ret) 1900294e30feSJosef Bacik goto out; 1901294e30feSJosef Bacik ret = btrfs_test_extent_buffer_operations(); 1902294e30feSJosef Bacik if (ret) 1903294e30feSJosef Bacik goto out; 1904294e30feSJosef Bacik ret = btrfs_test_extent_io(); 1905aaedb55bSJosef Bacik if (ret) 1906aaedb55bSJosef Bacik goto out; 1907aaedb55bSJosef Bacik ret = btrfs_test_inodes(); 1908faa2dbf0SJosef Bacik if (ret) 1909faa2dbf0SJosef Bacik goto out; 1910faa2dbf0SJosef Bacik ret = btrfs_test_qgroups(); 1911294e30feSJosef Bacik out: 1912294e30feSJosef Bacik btrfs_destroy_test_fs(); 1913294e30feSJosef Bacik return ret; 1914dc11dd5dSJosef Bacik } 1915dc11dd5dSJosef Bacik 19162e635a27SChris Mason static int __init init_btrfs_fs(void) 19172e635a27SChris Mason { 19182c90e5d6SChris Mason int err; 191958176a96SJosef Bacik 192014a958e6SFilipe David Borba Manana err = btrfs_hash_init(); 192114a958e6SFilipe David Borba Manana if (err) 192214a958e6SFilipe David Borba Manana return err; 192314a958e6SFilipe David Borba Manana 192463541927SFilipe David Borba Manana btrfs_props_init(); 192563541927SFilipe David Borba Manana 192658176a96SJosef Bacik err = btrfs_init_sysfs(); 192758176a96SJosef Bacik if (err) 192814a958e6SFilipe David Borba Manana goto free_hash; 192958176a96SJosef Bacik 1930143bede5SJeff Mahoney btrfs_init_compress(); 1931d1310b2eSChris Mason 1932261507a0SLi Zefan err = btrfs_init_cachep(); 1933261507a0SLi Zefan if (err) 1934261507a0SLi Zefan goto free_compress; 1935261507a0SLi Zefan 1936d1310b2eSChris Mason err = extent_io_init(); 19372f4cbe64SWyatt Banks if (err) 19382f4cbe64SWyatt Banks goto free_cachep; 19392f4cbe64SWyatt Banks 1940d1310b2eSChris Mason err = extent_map_init(); 1941d1310b2eSChris Mason if (err) 1942d1310b2eSChris Mason goto free_extent_io; 1943d1310b2eSChris Mason 19446352b91dSMiao Xie err = ordered_data_init(); 19452f4cbe64SWyatt Banks if (err) 19462f4cbe64SWyatt Banks goto free_extent_map; 1947c8b97818SChris Mason 19486352b91dSMiao Xie err = btrfs_delayed_inode_init(); 19496352b91dSMiao Xie if (err) 19506352b91dSMiao Xie goto free_ordered_data; 19516352b91dSMiao Xie 19529247f317SMiao Xie err = btrfs_auto_defrag_init(); 195316cdcec7SMiao Xie if (err) 195416cdcec7SMiao Xie goto free_delayed_inode; 195516cdcec7SMiao Xie 195678a6184aSMiao Xie err = btrfs_delayed_ref_init(); 19579247f317SMiao Xie if (err) 19589247f317SMiao Xie goto free_auto_defrag; 19599247f317SMiao Xie 1960b9e9a6cbSWang Shilong err = btrfs_prelim_ref_init(); 1961b9e9a6cbSWang Shilong if (err) 1962b9e9a6cbSWang Shilong goto free_prelim_ref; 1963b9e9a6cbSWang Shilong 196478a6184aSMiao Xie err = btrfs_interface_init(); 196578a6184aSMiao Xie if (err) 196678a6184aSMiao Xie goto free_delayed_ref; 196778a6184aSMiao Xie 1968e565d4b9SJan Schmidt btrfs_init_lockdep(); 1969e565d4b9SJan Schmidt 197085965600SDavid Sterba btrfs_print_info(); 1971dc11dd5dSJosef Bacik 1972dc11dd5dSJosef Bacik err = btrfs_run_sanity_tests(); 1973dc11dd5dSJosef Bacik if (err) 1974dc11dd5dSJosef Bacik goto unregister_ioctl; 1975dc11dd5dSJosef Bacik 1976dc11dd5dSJosef Bacik err = register_filesystem(&btrfs_fs_type); 1977dc11dd5dSJosef Bacik if (err) 1978dc11dd5dSJosef Bacik goto unregister_ioctl; 197974255aa0SJosef Bacik 19802f4cbe64SWyatt Banks return 0; 19812f4cbe64SWyatt Banks 1982a9218f6bSChris Mason unregister_ioctl: 1983a9218f6bSChris Mason btrfs_interface_exit(); 1984b9e9a6cbSWang Shilong free_prelim_ref: 1985b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 198678a6184aSMiao Xie free_delayed_ref: 198778a6184aSMiao Xie btrfs_delayed_ref_exit(); 19889247f317SMiao Xie free_auto_defrag: 19899247f317SMiao Xie btrfs_auto_defrag_exit(); 199016cdcec7SMiao Xie free_delayed_inode: 199116cdcec7SMiao Xie btrfs_delayed_inode_exit(); 19926352b91dSMiao Xie free_ordered_data: 19936352b91dSMiao Xie ordered_data_exit(); 19942f4cbe64SWyatt Banks free_extent_map: 19952f4cbe64SWyatt Banks extent_map_exit(); 1996d1310b2eSChris Mason free_extent_io: 1997d1310b2eSChris Mason extent_io_exit(); 19982f4cbe64SWyatt Banks free_cachep: 19992f4cbe64SWyatt Banks btrfs_destroy_cachep(); 2000261507a0SLi Zefan free_compress: 2001261507a0SLi Zefan btrfs_exit_compress(); 20022f4cbe64SWyatt Banks btrfs_exit_sysfs(); 200314a958e6SFilipe David Borba Manana free_hash: 200414a958e6SFilipe David Borba Manana btrfs_hash_exit(); 20052c90e5d6SChris Mason return err; 20062e635a27SChris Mason } 20072e635a27SChris Mason 20082e635a27SChris Mason static void __exit exit_btrfs_fs(void) 20092e635a27SChris Mason { 201039279cc3SChris Mason btrfs_destroy_cachep(); 201178a6184aSMiao Xie btrfs_delayed_ref_exit(); 20129247f317SMiao Xie btrfs_auto_defrag_exit(); 201316cdcec7SMiao Xie btrfs_delayed_inode_exit(); 2014b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 20156352b91dSMiao Xie ordered_data_exit(); 2016a52d9a80SChris Mason extent_map_exit(); 2017d1310b2eSChris Mason extent_io_exit(); 2018a9218f6bSChris Mason btrfs_interface_exit(); 20192e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 202058176a96SJosef Bacik btrfs_exit_sysfs(); 20218a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 2022261507a0SLi Zefan btrfs_exit_compress(); 202314a958e6SFilipe David Borba Manana btrfs_hash_exit(); 20242e635a27SChris Mason } 20252e635a27SChris Mason 202660efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs); 20272e635a27SChris Mason module_exit(exit_btrfs_fs) 20282e635a27SChris Mason 20292e635a27SChris Mason MODULE_LICENSE("GPL"); 2030