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"); 525451d7585SChris Mason break; 5263b30c22fSChris Mason case Opt_nossd: 52707802534SQu Wenruo btrfs_clear_and_info(root, NOSSD, 52807802534SQu Wenruo "not using ssd allocation scheme"); 5293b30c22fSChris Mason btrfs_clear_opt(info->mount_opt, SSD); 5303b30c22fSChris Mason break; 531842bef58SQu Wenruo case Opt_barrier: 53207802534SQu Wenruo btrfs_clear_and_info(root, NOBARRIER, 53307802534SQu Wenruo "turning on barriers"); 534842bef58SQu Wenruo break; 53521ad10cfSChris Mason case Opt_nobarrier: 53607802534SQu Wenruo btrfs_set_and_info(root, NOBARRIER, 53707802534SQu Wenruo "turning off barriers"); 53821ad10cfSChris Mason break; 5394543df7eSChris Mason case Opt_thread_pool: 5402c334e87SWang Shilong ret = match_int(&args[0], &intarg); 5412c334e87SWang Shilong if (ret) { 5422c334e87SWang Shilong goto out; 5432c334e87SWang Shilong } else if (intarg > 0) { 5444543df7eSChris Mason info->thread_pool_size = intarg; 5452c334e87SWang Shilong } else { 5462c334e87SWang Shilong ret = -EINVAL; 5472c334e87SWang Shilong goto out; 5482c334e87SWang Shilong } 5494543df7eSChris Mason break; 5506f568d35SChris Mason case Opt_max_inline: 551edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5526f568d35SChris Mason if (num) { 55391748467SAkinobu Mita info->max_inline = memparse(num, NULL); 5546f568d35SChris Mason kfree(num); 5556f568d35SChris Mason 55615ada040SChris Mason if (info->max_inline) { 557feb5f965SMitch Harder info->max_inline = min_t(u64, 55815ada040SChris Mason info->max_inline, 55915ada040SChris Mason root->sectorsize); 56015ada040SChris Mason } 561efe120a0SFrank Holton btrfs_info(root->fs_info, "max_inline at %llu", 562c1c9ff7cSGeert Uytterhoeven info->max_inline); 5632c334e87SWang Shilong } else { 5642c334e87SWang Shilong ret = -ENOMEM; 5652c334e87SWang Shilong goto out; 5666f568d35SChris Mason } 5676f568d35SChris Mason break; 5688f662a76SChris Mason case Opt_alloc_start: 569edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5708f662a76SChris Mason if (num) { 571c018daecSMiao Xie mutex_lock(&info->chunk_mutex); 57291748467SAkinobu Mita info->alloc_start = memparse(num, NULL); 573c018daecSMiao Xie mutex_unlock(&info->chunk_mutex); 5748f662a76SChris Mason kfree(num); 575efe120a0SFrank Holton btrfs_info(root->fs_info, "allocations start at %llu", 576c1c9ff7cSGeert Uytterhoeven info->alloc_start); 5772c334e87SWang Shilong } else { 5782c334e87SWang Shilong ret = -ENOMEM; 5792c334e87SWang Shilong goto out; 5808f662a76SChris Mason } 5818f662a76SChris Mason break; 582bd0330adSQu Wenruo case Opt_acl: 58345ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL 584bd0330adSQu Wenruo root->fs_info->sb->s_flags |= MS_POSIXACL; 585bd0330adSQu Wenruo break; 58645ff35d6SGuangliang Zhao #else 58745ff35d6SGuangliang Zhao btrfs_err(root->fs_info, 58845ff35d6SGuangliang Zhao "support for ACL not compiled in!"); 58945ff35d6SGuangliang Zhao ret = -EINVAL; 59045ff35d6SGuangliang Zhao goto out; 59145ff35d6SGuangliang Zhao #endif 59233268eafSJosef Bacik case Opt_noacl: 59333268eafSJosef Bacik root->fs_info->sb->s_flags &= ~MS_POSIXACL; 59433268eafSJosef Bacik break; 5953a5e1404SSage Weil case Opt_notreelog: 59607802534SQu Wenruo btrfs_set_and_info(root, NOTREELOG, 59707802534SQu Wenruo "disabling tree log"); 5983a5e1404SSage Weil break; 599a88998f2SQu Wenruo case Opt_treelog: 60007802534SQu Wenruo btrfs_clear_and_info(root, NOTREELOG, 60107802534SQu Wenruo "enabling tree log"); 602a88998f2SQu Wenruo break; 603dccae999SSage Weil case Opt_flushoncommit: 60407802534SQu Wenruo btrfs_set_and_info(root, FLUSHONCOMMIT, 60507802534SQu Wenruo "turning on flush-on-commit"); 606dccae999SSage Weil break; 6072c9ee856SQu Wenruo case Opt_noflushoncommit: 60807802534SQu Wenruo btrfs_clear_and_info(root, FLUSHONCOMMIT, 60907802534SQu Wenruo "turning off flush-on-commit"); 6102c9ee856SQu Wenruo break; 61197e728d4SJosef Bacik case Opt_ratio: 6122c334e87SWang Shilong ret = match_int(&args[0], &intarg); 6132c334e87SWang Shilong if (ret) { 6142c334e87SWang Shilong goto out; 6152c334e87SWang Shilong } else if (intarg >= 0) { 61697e728d4SJosef Bacik info->metadata_ratio = intarg; 617efe120a0SFrank Holton btrfs_info(root->fs_info, "metadata ratio %d", 61897e728d4SJosef Bacik info->metadata_ratio); 6192c334e87SWang Shilong } else { 6202c334e87SWang Shilong ret = -EINVAL; 6212c334e87SWang Shilong goto out; 62297e728d4SJosef Bacik } 62397e728d4SJosef Bacik break; 624e244a0aeSChristoph Hellwig case Opt_discard: 62507802534SQu Wenruo btrfs_set_and_info(root, DISCARD, 62607802534SQu Wenruo "turning on discard"); 627e244a0aeSChristoph Hellwig break; 628e07a2adeSQu Wenruo case Opt_nodiscard: 62907802534SQu Wenruo btrfs_clear_and_info(root, DISCARD, 63007802534SQu Wenruo "turning off discard"); 631e07a2adeSQu Wenruo break; 6320af3d00bSJosef Bacik case Opt_space_cache: 63307802534SQu Wenruo btrfs_set_and_info(root, SPACE_CACHE, 63407802534SQu Wenruo "enabling disk space caching"); 6350de90876SJosef Bacik break; 636f420ee1eSStefan Behrens case Opt_rescan_uuid_tree: 637f420ee1eSStefan Behrens btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE); 638f420ee1eSStefan Behrens break; 63973bc1876SJosef Bacik case Opt_no_space_cache: 64007802534SQu Wenruo btrfs_clear_and_info(root, SPACE_CACHE, 64107802534SQu Wenruo "disabling disk space caching"); 64273bc1876SJosef Bacik break; 6434b9465cbSChris Mason case Opt_inode_cache: 64407802534SQu Wenruo btrfs_set_and_info(root, CHANGE_INODE_CACHE, 64507802534SQu Wenruo "enabling inode map caching"); 6463818aea2SQu Wenruo break; 6473818aea2SQu Wenruo case Opt_noinode_cache: 64807802534SQu Wenruo btrfs_clear_and_info(root, CHANGE_INODE_CACHE, 64907802534SQu Wenruo "disabling inode map caching"); 6504b9465cbSChris Mason break; 65188c2ba3bSJosef Bacik case Opt_clear_cache: 65207802534SQu Wenruo btrfs_set_and_info(root, CLEAR_CACHE, 65307802534SQu Wenruo "force clearing of disk cache"); 6540af3d00bSJosef Bacik break; 6554260f7c7SSage Weil case Opt_user_subvol_rm_allowed: 6564260f7c7SSage Weil btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 6574260f7c7SSage Weil break; 65891435650SChris Mason case Opt_enospc_debug: 65991435650SChris Mason btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 66091435650SChris Mason break; 66153036293SQu Wenruo case Opt_noenospc_debug: 66253036293SQu Wenruo btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG); 66353036293SQu Wenruo break; 6644cb5300bSChris Mason case Opt_defrag: 66507802534SQu Wenruo btrfs_set_and_info(root, AUTO_DEFRAG, 66607802534SQu Wenruo "enabling auto defrag"); 6674cb5300bSChris Mason break; 668fc0ca9afSQu Wenruo case Opt_nodefrag: 66907802534SQu Wenruo btrfs_clear_and_info(root, AUTO_DEFRAG, 67007802534SQu Wenruo "disabling auto defrag"); 671fc0ca9afSQu Wenruo break; 672af31f5e5SChris Mason case Opt_recovery: 673efe120a0SFrank Holton btrfs_info(root->fs_info, "enabling auto recovery"); 674af31f5e5SChris Mason btrfs_set_opt(info->mount_opt, RECOVERY); 675af31f5e5SChris Mason break; 6769555c6c1SIlya Dryomov case Opt_skip_balance: 6779555c6c1SIlya Dryomov btrfs_set_opt(info->mount_opt, SKIP_BALANCE); 6789555c6c1SIlya Dryomov break; 67921adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 68021adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 681efe120a0SFrank Holton btrfs_info(root->fs_info, 682efe120a0SFrank Holton "enabling check integrity including extent data"); 68321adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, 68421adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA); 68521adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 68621adbd5cSStefan Behrens break; 68721adbd5cSStefan Behrens case Opt_check_integrity: 688efe120a0SFrank Holton btrfs_info(root->fs_info, "enabling check integrity"); 68921adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 69021adbd5cSStefan Behrens break; 69121adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 6922c334e87SWang Shilong ret = match_int(&args[0], &intarg); 6932c334e87SWang Shilong if (ret) { 6942c334e87SWang Shilong goto out; 6952c334e87SWang Shilong } else if (intarg >= 0) { 69621adbd5cSStefan Behrens info->check_integrity_print_mask = intarg; 697efe120a0SFrank Holton btrfs_info(root->fs_info, "check_integrity_print_mask 0x%x", 69821adbd5cSStefan Behrens info->check_integrity_print_mask); 6992c334e87SWang Shilong } else { 7002c334e87SWang Shilong ret = -EINVAL; 7012c334e87SWang Shilong goto out; 70221adbd5cSStefan Behrens } 70321adbd5cSStefan Behrens break; 70421adbd5cSStefan Behrens #else 70521adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 70621adbd5cSStefan Behrens case Opt_check_integrity: 70721adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 708efe120a0SFrank Holton btrfs_err(root->fs_info, 709efe120a0SFrank Holton "support for check_integrity* not compiled in!"); 71021adbd5cSStefan Behrens ret = -EINVAL; 71121adbd5cSStefan Behrens goto out; 71221adbd5cSStefan Behrens #endif 7138c342930SJeff Mahoney case Opt_fatal_errors: 7148c342930SJeff Mahoney if (strcmp(args[0].from, "panic") == 0) 7158c342930SJeff Mahoney btrfs_set_opt(info->mount_opt, 7168c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7178c342930SJeff Mahoney else if (strcmp(args[0].from, "bug") == 0) 7188c342930SJeff Mahoney btrfs_clear_opt(info->mount_opt, 7198c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7208c342930SJeff Mahoney else { 7218c342930SJeff Mahoney ret = -EINVAL; 7228c342930SJeff Mahoney goto out; 7238c342930SJeff Mahoney } 7248c342930SJeff Mahoney break; 7258b87dc17SDavid Sterba case Opt_commit_interval: 7268b87dc17SDavid Sterba intarg = 0; 7278b87dc17SDavid Sterba ret = match_int(&args[0], &intarg); 7288b87dc17SDavid Sterba if (ret < 0) { 729efe120a0SFrank Holton btrfs_err(root->fs_info, "invalid commit interval"); 7308b87dc17SDavid Sterba ret = -EINVAL; 7318b87dc17SDavid Sterba goto out; 7328b87dc17SDavid Sterba } 7338b87dc17SDavid Sterba if (intarg > 0) { 7348b87dc17SDavid Sterba if (intarg > 300) { 735efe120a0SFrank Holton btrfs_warn(root->fs_info, "excessive commit interval %d", 7368b87dc17SDavid Sterba intarg); 7378b87dc17SDavid Sterba } 7388b87dc17SDavid Sterba info->commit_interval = intarg; 7398b87dc17SDavid Sterba } else { 740efe120a0SFrank Holton btrfs_info(root->fs_info, "using default commit interval %ds", 7418b87dc17SDavid Sterba BTRFS_DEFAULT_COMMIT_INTERVAL); 7428b87dc17SDavid Sterba info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 7438b87dc17SDavid Sterba } 7448b87dc17SDavid Sterba break; 745a7a3f7caSSage Weil case Opt_err: 746efe120a0SFrank Holton btrfs_info(root->fs_info, "unrecognized mount option '%s'", p); 747a7a3f7caSSage Weil ret = -EINVAL; 748a7a3f7caSSage Weil goto out; 74995e05289SChris Mason default: 750be20aa9dSChris Mason break; 75195e05289SChris Mason } 75295e05289SChris Mason } 753a7a3f7caSSage Weil out: 75473bc1876SJosef Bacik if (!ret && btrfs_test_opt(root, SPACE_CACHE)) 755efe120a0SFrank Holton btrfs_info(root->fs_info, "disk space caching is enabled"); 756da495eccSJosef Bacik kfree(orig); 757a7a3f7caSSage Weil return ret; 758edf24abeSChristoph Hellwig } 759edf24abeSChristoph Hellwig 760edf24abeSChristoph Hellwig /* 761edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 762edf24abeSChristoph Hellwig * 763edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 764edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 765edf24abeSChristoph Hellwig */ 76697288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 76773f73415SJosef Bacik void *holder, char **subvol_name, u64 *subvol_objectid, 7685e2a4b25SDavid Sterba struct btrfs_fs_devices **fs_devices) 769edf24abeSChristoph Hellwig { 770edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 77183c8c9bdSJeff Liu char *device_name, *opts, *orig, *p; 7721493381fSWang Shilong char *num = NULL; 773edf24abeSChristoph Hellwig int error = 0; 774edf24abeSChristoph Hellwig 775edf24abeSChristoph Hellwig if (!options) 776830c4adbSJosef Bacik return 0; 777edf24abeSChristoph Hellwig 778edf24abeSChristoph Hellwig /* 779edf24abeSChristoph Hellwig * strsep changes the string, duplicate it because parse_options 780edf24abeSChristoph Hellwig * gets called twice 781edf24abeSChristoph Hellwig */ 782edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 783edf24abeSChristoph Hellwig if (!opts) 784edf24abeSChristoph Hellwig return -ENOMEM; 7853f3d0bc0STero Roponen orig = opts; 786edf24abeSChristoph Hellwig 787edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 788edf24abeSChristoph Hellwig int token; 789edf24abeSChristoph Hellwig if (!*p) 790edf24abeSChristoph Hellwig continue; 791edf24abeSChristoph Hellwig 792edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 793edf24abeSChristoph Hellwig switch (token) { 794edf24abeSChristoph Hellwig case Opt_subvol: 795a90e8b6fSIlya Dryomov kfree(*subvol_name); 796edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 7972c334e87SWang Shilong if (!*subvol_name) { 7982c334e87SWang Shilong error = -ENOMEM; 7992c334e87SWang Shilong goto out; 8002c334e87SWang Shilong } 801edf24abeSChristoph Hellwig break; 80273f73415SJosef Bacik case Opt_subvolid: 8031493381fSWang Shilong num = match_strdup(&args[0]); 8041493381fSWang Shilong if (num) { 8051493381fSWang Shilong *subvol_objectid = memparse(num, NULL); 8061493381fSWang Shilong kfree(num); 8074849f01dSJosef Bacik /* we want the original fs_tree */ 8081493381fSWang Shilong if (!*subvol_objectid) 8094849f01dSJosef Bacik *subvol_objectid = 8104849f01dSJosef Bacik BTRFS_FS_TREE_OBJECTID; 8112c334e87SWang Shilong } else { 8122c334e87SWang Shilong error = -EINVAL; 8132c334e87SWang Shilong goto out; 8144849f01dSJosef Bacik } 81573f73415SJosef Bacik break; 816e15d0542SXin Zhong case Opt_subvolrootid: 8175e2a4b25SDavid Sterba printk(KERN_WARNING 818efe120a0SFrank Holton "BTRFS: 'subvolrootid' mount option is deprecated and has " 819efe120a0SFrank Holton "no effect\n"); 820e15d0542SXin Zhong break; 82143e570b0SChristoph Hellwig case Opt_device: 82283c8c9bdSJeff Liu device_name = match_strdup(&args[0]); 82383c8c9bdSJeff Liu if (!device_name) { 82483c8c9bdSJeff Liu error = -ENOMEM; 82583c8c9bdSJeff Liu goto out; 82683c8c9bdSJeff Liu } 82783c8c9bdSJeff Liu error = btrfs_scan_one_device(device_name, 82843e570b0SChristoph Hellwig flags, holder, fs_devices); 82983c8c9bdSJeff Liu kfree(device_name); 83043e570b0SChristoph Hellwig if (error) 831830c4adbSJosef Bacik goto out; 83243e570b0SChristoph Hellwig break; 833edf24abeSChristoph Hellwig default: 834edf24abeSChristoph Hellwig break; 835edf24abeSChristoph Hellwig } 836edf24abeSChristoph Hellwig } 837edf24abeSChristoph Hellwig 838edf24abeSChristoph Hellwig out: 839830c4adbSJosef Bacik kfree(orig); 840edf24abeSChristoph Hellwig return error; 84195e05289SChris Mason } 84295e05289SChris Mason 84373f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb, 84473f73415SJosef Bacik u64 subvol_objectid) 84573f73415SJosef Bacik { 846815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 847815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 84873f73415SJosef Bacik struct btrfs_root *new_root; 84973f73415SJosef Bacik struct btrfs_dir_item *di; 85073f73415SJosef Bacik struct btrfs_path *path; 85173f73415SJosef Bacik struct btrfs_key location; 85273f73415SJosef Bacik struct inode *inode; 8533a0dfa6aSJosef Bacik struct dentry *dentry; 85473f73415SJosef Bacik u64 dir_id; 85573f73415SJosef Bacik int new = 0; 85673f73415SJosef Bacik 85773f73415SJosef Bacik /* 85873f73415SJosef Bacik * We have a specific subvol we want to mount, just setup location and 85973f73415SJosef Bacik * go look up the root. 86073f73415SJosef Bacik */ 86173f73415SJosef Bacik if (subvol_objectid) { 86273f73415SJosef Bacik location.objectid = subvol_objectid; 86373f73415SJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 86473f73415SJosef Bacik location.offset = (u64)-1; 86573f73415SJosef Bacik goto find_root; 86673f73415SJosef Bacik } 86773f73415SJosef Bacik 86873f73415SJosef Bacik path = btrfs_alloc_path(); 86973f73415SJosef Bacik if (!path) 87073f73415SJosef Bacik return ERR_PTR(-ENOMEM); 87173f73415SJosef Bacik path->leave_spinning = 1; 87273f73415SJosef Bacik 87373f73415SJosef Bacik /* 87473f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 87573f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 87673f73415SJosef Bacik * to mount. 87773f73415SJosef Bacik */ 878815745cfSAl Viro dir_id = btrfs_super_root_dir(fs_info->super_copy); 87973f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 880b0839166SJulia Lawall if (IS_ERR(di)) { 881b0839166SJulia Lawall btrfs_free_path(path); 882fb4f6f91SDan Carpenter return ERR_CAST(di); 883b0839166SJulia Lawall } 88473f73415SJosef Bacik if (!di) { 88573f73415SJosef Bacik /* 88673f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 88773f73415SJosef Bacik * it's always been there, but don't freak out, just try and 88873f73415SJosef Bacik * mount to root most subvolume. 88973f73415SJosef Bacik */ 89073f73415SJosef Bacik btrfs_free_path(path); 89173f73415SJosef Bacik dir_id = BTRFS_FIRST_FREE_OBJECTID; 892815745cfSAl Viro new_root = fs_info->fs_root; 89373f73415SJosef Bacik goto setup_root; 89473f73415SJosef Bacik } 89573f73415SJosef Bacik 89673f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 89773f73415SJosef Bacik btrfs_free_path(path); 89873f73415SJosef Bacik 89973f73415SJosef Bacik find_root: 900815745cfSAl Viro new_root = btrfs_read_fs_root_no_name(fs_info, &location); 90173f73415SJosef Bacik if (IS_ERR(new_root)) 902d0b678cbSJulia Lawall return ERR_CAST(new_root); 90373f73415SJosef Bacik 90473f73415SJosef Bacik dir_id = btrfs_root_dirid(&new_root->root_item); 90573f73415SJosef Bacik setup_root: 90673f73415SJosef Bacik location.objectid = dir_id; 90773f73415SJosef Bacik location.type = BTRFS_INODE_ITEM_KEY; 90873f73415SJosef Bacik location.offset = 0; 90973f73415SJosef Bacik 91073f73415SJosef Bacik inode = btrfs_iget(sb, &location, new_root, &new); 9114cbd1149SDan Carpenter if (IS_ERR(inode)) 9124cbd1149SDan Carpenter return ERR_CAST(inode); 91373f73415SJosef Bacik 91473f73415SJosef Bacik /* 91573f73415SJosef Bacik * If we're just mounting the root most subvol put the inode and return 91673f73415SJosef Bacik * a reference to the dentry. We will have already gotten a reference 91773f73415SJosef Bacik * to the inode in btrfs_fill_super so we're good to go. 91873f73415SJosef Bacik */ 91973f73415SJosef Bacik if (!new && sb->s_root->d_inode == inode) { 92073f73415SJosef Bacik iput(inode); 92173f73415SJosef Bacik return dget(sb->s_root); 92273f73415SJosef Bacik } 92373f73415SJosef Bacik 9243a0dfa6aSJosef Bacik dentry = d_obtain_alias(inode); 9253a0dfa6aSJosef Bacik if (!IS_ERR(dentry)) { 9263a0dfa6aSJosef Bacik spin_lock(&dentry->d_lock); 9273a0dfa6aSJosef Bacik dentry->d_flags &= ~DCACHE_DISCONNECTED; 9283a0dfa6aSJosef Bacik spin_unlock(&dentry->d_lock); 9293a0dfa6aSJosef Bacik } 9303a0dfa6aSJosef Bacik return dentry; 93173f73415SJosef Bacik } 93273f73415SJosef Bacik 9338a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 9348a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 9358a4b83ccSChris Mason void *data, int silent) 9362e635a27SChris Mason { 9372e635a27SChris Mason struct inode *inode; 938815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 9395d4f98a2SYan Zheng struct btrfs_key key; 94039279cc3SChris Mason int err; 9412e635a27SChris Mason 9422e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 9432e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 944e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 945af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 946be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 9475103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 9482e635a27SChris Mason sb->s_time_gran = 1; 9490eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 95033268eafSJosef Bacik sb->s_flags |= MS_POSIXACL; 95149cf6f45SChris Ball #endif 9520c4d2d95SJosef Bacik sb->s_flags |= MS_I_VERSION; 953ad2b2c80SAl Viro err = open_ctree(sb, fs_devices, (char *)data); 954ad2b2c80SAl Viro if (err) { 955efe120a0SFrank Holton printk(KERN_ERR "BTRFS: open_ctree failed\n"); 956ad2b2c80SAl Viro return err; 957e20d96d6SChris Mason } 958b888db2bSChris Mason 9595d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 9605d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 9615d4f98a2SYan Zheng key.offset = 0; 96298c7089cSAl Viro inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL); 9635d4f98a2SYan Zheng if (IS_ERR(inode)) { 9645d4f98a2SYan Zheng err = PTR_ERR(inode); 96539279cc3SChris Mason goto fail_close; 96639279cc3SChris Mason } 9672e635a27SChris Mason 96848fde701SAl Viro sb->s_root = d_make_root(inode); 96948fde701SAl Viro if (!sb->s_root) { 97039279cc3SChris Mason err = -ENOMEM; 97139279cc3SChris Mason goto fail_close; 9722e635a27SChris Mason } 97358176a96SJosef Bacik 9746885f308SChris Mason save_mount_options(sb, data); 97590a887c9SDan Magenheimer cleancache_init_fs(sb); 97659553edfSAl Viro sb->s_flags |= MS_ACTIVE; 9772e635a27SChris Mason return 0; 9782e635a27SChris Mason 97939279cc3SChris Mason fail_close: 980815745cfSAl Viro close_ctree(fs_info->tree_root); 981d5719762SChris Mason return err; 982d5719762SChris Mason } 983d5719762SChris Mason 9846bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 985d5719762SChris Mason { 986d5719762SChris Mason struct btrfs_trans_handle *trans; 987815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 988815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 989df2ce34cSChris Mason 9901abe9b8aSliubo trace_btrfs_sync_fs(wait); 9911abe9b8aSliubo 992d561c025SChris Mason if (!wait) { 993815745cfSAl Viro filemap_flush(fs_info->btree_inode->i_mapping); 994df2ce34cSChris Mason return 0; 995d561c025SChris Mason } 996771ed689SChris Mason 997b0244199SMiao Xie btrfs_wait_ordered_roots(fs_info, -1); 998771ed689SChris Mason 999d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 100060376ce4SJosef Bacik if (IS_ERR(trans)) { 1001354aa0fbSMiao Xie /* no transaction, don't bother */ 1002354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 1003bd7de2c9SJosef Bacik return 0; 100498d5dc13STsutomu Itoh return PTR_ERR(trans); 100560376ce4SJosef Bacik } 1006bd7de2c9SJosef Bacik return btrfs_commit_transaction(trans, root); 1007d5719762SChris Mason } 1008d5719762SChris Mason 100934c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 1010a9572a15SEric Paris { 1011815745cfSAl Viro struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 1012815745cfSAl Viro struct btrfs_root *root = info->tree_root; 1013200da64eSTsutomu Itoh char *compress_type; 1014a9572a15SEric Paris 1015a9572a15SEric Paris if (btrfs_test_opt(root, DEGRADED)) 1016a9572a15SEric Paris seq_puts(seq, ",degraded"); 1017a9572a15SEric Paris if (btrfs_test_opt(root, NODATASUM)) 1018a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 1019a9572a15SEric Paris if (btrfs_test_opt(root, NODATACOW)) 1020a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 1021a9572a15SEric Paris if (btrfs_test_opt(root, NOBARRIER)) 1022a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 1023a9572a15SEric Paris if (info->max_inline != 8192 * 1024) 1024c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",max_inline=%llu", info->max_inline); 1025a9572a15SEric Paris if (info->alloc_start != 0) 1026c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",alloc_start=%llu", info->alloc_start); 1027a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 1028a9572a15SEric Paris num_online_cpus() + 2, 8)) 1029a9572a15SEric Paris seq_printf(seq, ",thread_pool=%d", info->thread_pool_size); 1030200da64eSTsutomu Itoh if (btrfs_test_opt(root, COMPRESS)) { 1031200da64eSTsutomu Itoh if (info->compress_type == BTRFS_COMPRESS_ZLIB) 1032200da64eSTsutomu Itoh compress_type = "zlib"; 1033200da64eSTsutomu Itoh else 1034200da64eSTsutomu Itoh compress_type = "lzo"; 1035200da64eSTsutomu Itoh if (btrfs_test_opt(root, FORCE_COMPRESS)) 1036200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 1037200da64eSTsutomu Itoh else 1038200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 1039200da64eSTsutomu Itoh } 1040c289811cSChris Mason if (btrfs_test_opt(root, NOSSD)) 1041c289811cSChris Mason seq_puts(seq, ",nossd"); 1042451d7585SChris Mason if (btrfs_test_opt(root, SSD_SPREAD)) 1043451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 1044451d7585SChris Mason else if (btrfs_test_opt(root, SSD)) 1045a9572a15SEric Paris seq_puts(seq, ",ssd"); 10463a5e1404SSage Weil if (btrfs_test_opt(root, NOTREELOG)) 10476b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 1048dccae999SSage Weil if (btrfs_test_opt(root, FLUSHONCOMMIT)) 10496b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 105020a5239aSMatthew Wilcox if (btrfs_test_opt(root, DISCARD)) 105120a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 1052a9572a15SEric Paris if (!(root->fs_info->sb->s_flags & MS_POSIXACL)) 1053a9572a15SEric Paris seq_puts(seq, ",noacl"); 1054200da64eSTsutomu Itoh if (btrfs_test_opt(root, SPACE_CACHE)) 1055200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 105673bc1876SJosef Bacik else 10578965593eSDavid Sterba seq_puts(seq, ",nospace_cache"); 1058f420ee1eSStefan Behrens if (btrfs_test_opt(root, RESCAN_UUID_TREE)) 1059f420ee1eSStefan Behrens seq_puts(seq, ",rescan_uuid_tree"); 1060200da64eSTsutomu Itoh if (btrfs_test_opt(root, CLEAR_CACHE)) 1061200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 1062200da64eSTsutomu Itoh if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 1063200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 10640942caa3SDavid Sterba if (btrfs_test_opt(root, ENOSPC_DEBUG)) 10650942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 10660942caa3SDavid Sterba if (btrfs_test_opt(root, AUTO_DEFRAG)) 10670942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 10680942caa3SDavid Sterba if (btrfs_test_opt(root, INODE_MAP_CACHE)) 10690942caa3SDavid Sterba seq_puts(seq, ",inode_cache"); 10709555c6c1SIlya Dryomov if (btrfs_test_opt(root, SKIP_BALANCE)) 10719555c6c1SIlya Dryomov seq_puts(seq, ",skip_balance"); 10728507d216SWang Shilong if (btrfs_test_opt(root, RECOVERY)) 10738507d216SWang Shilong seq_puts(seq, ",recovery"); 10748507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 10758507d216SWang Shilong if (btrfs_test_opt(root, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA)) 10768507d216SWang Shilong seq_puts(seq, ",check_int_data"); 10778507d216SWang Shilong else if (btrfs_test_opt(root, CHECK_INTEGRITY)) 10788507d216SWang Shilong seq_puts(seq, ",check_int"); 10798507d216SWang Shilong if (info->check_integrity_print_mask) 10808507d216SWang Shilong seq_printf(seq, ",check_int_print_mask=%d", 10818507d216SWang Shilong info->check_integrity_print_mask); 10828507d216SWang Shilong #endif 10838507d216SWang Shilong if (info->metadata_ratio) 10848507d216SWang Shilong seq_printf(seq, ",metadata_ratio=%d", 10858507d216SWang Shilong info->metadata_ratio); 10868c342930SJeff Mahoney if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR)) 10878c342930SJeff Mahoney seq_puts(seq, ",fatal_errors=panic"); 10888b87dc17SDavid Sterba if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL) 10898b87dc17SDavid Sterba seq_printf(seq, ",commit=%d", info->commit_interval); 1090a9572a15SEric Paris return 0; 1091a9572a15SEric Paris } 1092a9572a15SEric Paris 1093a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 10942e635a27SChris Mason { 1095815745cfSAl Viro struct btrfs_fs_info *p = data; 1096815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(s); 10974b82d6e4SYan 1098815745cfSAl Viro return fs_info->fs_devices == p->fs_devices; 10994b82d6e4SYan } 11004b82d6e4SYan 1101450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 1102450ba0eaSJosef Bacik { 11036de1d09dSAl Viro int err = set_anon_super(s, data); 11046de1d09dSAl Viro if (!err) 1105450ba0eaSJosef Bacik s->s_fs_info = data; 11066de1d09dSAl Viro return err; 1107450ba0eaSJosef Bacik } 1108450ba0eaSJosef Bacik 1109830c4adbSJosef Bacik /* 1110f9d9ef62SDavid Sterba * subvolumes are identified by ino 256 1111f9d9ef62SDavid Sterba */ 1112f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode) 1113f9d9ef62SDavid Sterba { 1114f9d9ef62SDavid Sterba if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 1115f9d9ef62SDavid Sterba return 1; 1116f9d9ef62SDavid Sterba return 0; 1117f9d9ef62SDavid Sterba } 1118f9d9ef62SDavid Sterba 1119f9d9ef62SDavid Sterba /* 1120830c4adbSJosef Bacik * This will strip out the subvol=%s argument for an argument string and add 1121830c4adbSJosef Bacik * subvolid=0 to make sure we get the actual tree root for path walking to the 1122830c4adbSJosef Bacik * subvol we want. 1123830c4adbSJosef Bacik */ 1124830c4adbSJosef Bacik static char *setup_root_args(char *args) 1125830c4adbSJosef Bacik { 1126f60d16a8SJim Meyering unsigned len = strlen(args) + 2 + 1; 1127f60d16a8SJim Meyering char *src, *dst, *buf; 1128830c4adbSJosef Bacik 1129830c4adbSJosef Bacik /* 1130f60d16a8SJim Meyering * We need the same args as before, but with this substitution: 1131f60d16a8SJim Meyering * s!subvol=[^,]+!subvolid=0! 1132830c4adbSJosef Bacik * 1133f60d16a8SJim Meyering * Since the replacement string is up to 2 bytes longer than the 1134f60d16a8SJim Meyering * original, allocate strlen(args) + 2 + 1 bytes. 1135830c4adbSJosef Bacik */ 1136830c4adbSJosef Bacik 1137f60d16a8SJim Meyering src = strstr(args, "subvol="); 1138830c4adbSJosef Bacik /* This shouldn't happen, but just in case.. */ 1139f60d16a8SJim Meyering if (!src) 1140830c4adbSJosef Bacik return NULL; 1141f60d16a8SJim Meyering 1142f60d16a8SJim Meyering buf = dst = kmalloc(len, GFP_NOFS); 1143f60d16a8SJim Meyering if (!buf) 1144f60d16a8SJim Meyering return NULL; 1145830c4adbSJosef Bacik 1146830c4adbSJosef Bacik /* 1147f60d16a8SJim Meyering * If the subvol= arg is not at the start of the string, 1148f60d16a8SJim Meyering * copy whatever precedes it into buf. 1149830c4adbSJosef Bacik */ 1150f60d16a8SJim Meyering if (src != args) { 1151f60d16a8SJim Meyering *src++ = '\0'; 1152f60d16a8SJim Meyering strcpy(buf, args); 1153f60d16a8SJim Meyering dst += strlen(args); 1154830c4adbSJosef Bacik } 1155830c4adbSJosef Bacik 1156f60d16a8SJim Meyering strcpy(dst, "subvolid=0"); 1157f60d16a8SJim Meyering dst += strlen("subvolid=0"); 1158830c4adbSJosef Bacik 1159830c4adbSJosef Bacik /* 1160f60d16a8SJim Meyering * If there is a "," after the original subvol=... string, 1161f60d16a8SJim Meyering * copy that suffix into our buffer. Otherwise, we're done. 1162830c4adbSJosef Bacik */ 1163f60d16a8SJim Meyering src = strchr(src, ','); 1164f60d16a8SJim Meyering if (src) 1165f60d16a8SJim Meyering strcpy(dst, src); 1166830c4adbSJosef Bacik 1167f60d16a8SJim Meyering return buf; 1168830c4adbSJosef Bacik } 1169830c4adbSJosef Bacik 1170830c4adbSJosef Bacik static struct dentry *mount_subvol(const char *subvol_name, int flags, 1171830c4adbSJosef Bacik const char *device_name, char *data) 1172830c4adbSJosef Bacik { 1173830c4adbSJosef Bacik struct dentry *root; 1174830c4adbSJosef Bacik struct vfsmount *mnt; 1175830c4adbSJosef Bacik char *newargs; 1176830c4adbSJosef Bacik 1177830c4adbSJosef Bacik newargs = setup_root_args(data); 1178830c4adbSJosef Bacik if (!newargs) 1179830c4adbSJosef Bacik return ERR_PTR(-ENOMEM); 1180830c4adbSJosef Bacik mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, 1181830c4adbSJosef Bacik newargs); 11820723a047SHarald Hoyer 11830723a047SHarald Hoyer if (PTR_RET(mnt) == -EBUSY) { 11840723a047SHarald Hoyer if (flags & MS_RDONLY) { 11850723a047SHarald Hoyer mnt = vfs_kern_mount(&btrfs_fs_type, flags & ~MS_RDONLY, device_name, 11860723a047SHarald Hoyer newargs); 11870723a047SHarald Hoyer } else { 11880723a047SHarald Hoyer int r; 11890723a047SHarald Hoyer mnt = vfs_kern_mount(&btrfs_fs_type, flags | MS_RDONLY, device_name, 11900723a047SHarald Hoyer newargs); 11910040e606SChristoph Jaeger if (IS_ERR(mnt)) { 11920040e606SChristoph Jaeger kfree(newargs); 11930723a047SHarald Hoyer return ERR_CAST(mnt); 11940040e606SChristoph Jaeger } 11950723a047SHarald Hoyer 11960723a047SHarald Hoyer r = btrfs_remount(mnt->mnt_sb, &flags, NULL); 11970723a047SHarald Hoyer if (r < 0) { 11980723a047SHarald Hoyer /* FIXME: release vfsmount mnt ??*/ 11990040e606SChristoph Jaeger kfree(newargs); 12000723a047SHarald Hoyer return ERR_PTR(r); 12010723a047SHarald Hoyer } 12020723a047SHarald Hoyer } 12030723a047SHarald Hoyer } 12040723a047SHarald Hoyer 12050040e606SChristoph Jaeger kfree(newargs); 12060040e606SChristoph Jaeger 1207830c4adbSJosef Bacik if (IS_ERR(mnt)) 1208830c4adbSJosef Bacik return ERR_CAST(mnt); 1209830c4adbSJosef Bacik 1210ea441d11SAl Viro root = mount_subtree(mnt, subvol_name); 1211830c4adbSJosef Bacik 1212ea441d11SAl Viro if (!IS_ERR(root) && !is_subvolume_inode(root->d_inode)) { 1213ea441d11SAl Viro struct super_block *s = root->d_sb; 1214ea441d11SAl Viro dput(root); 1215ea441d11SAl Viro root = ERR_PTR(-EINVAL); 1216ea441d11SAl Viro deactivate_locked_super(s); 1217efe120a0SFrank Holton printk(KERN_ERR "BTRFS: '%s' is not a valid subvolume\n", 1218f9d9ef62SDavid Sterba subvol_name); 1219f9d9ef62SDavid Sterba } 1220f9d9ef62SDavid Sterba 1221830c4adbSJosef Bacik return root; 1222830c4adbSJosef Bacik } 1223450ba0eaSJosef Bacik 1224edf24abeSChristoph Hellwig /* 1225edf24abeSChristoph Hellwig * Find a superblock for the given device / mount point. 1226edf24abeSChristoph Hellwig * 1227edf24abeSChristoph Hellwig * Note: This is based on get_sb_bdev from fs/super.c with a few additions 1228edf24abeSChristoph Hellwig * for multiple device setup. Make sure to keep it in sync. 1229edf24abeSChristoph Hellwig */ 1230061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1231306e16ceSDavid Sterba const char *device_name, void *data) 12324b82d6e4SYan { 12334b82d6e4SYan struct block_device *bdev = NULL; 12344b82d6e4SYan struct super_block *s; 12354b82d6e4SYan struct dentry *root; 12368a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices = NULL; 1237450ba0eaSJosef Bacik struct btrfs_fs_info *fs_info = NULL; 123897288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 123973f73415SJosef Bacik char *subvol_name = NULL; 124073f73415SJosef Bacik u64 subvol_objectid = 0; 12414b82d6e4SYan int error = 0; 12424b82d6e4SYan 124397288f2cSChristoph Hellwig if (!(flags & MS_RDONLY)) 124497288f2cSChristoph Hellwig mode |= FMODE_WRITE; 124597288f2cSChristoph Hellwig 124697288f2cSChristoph Hellwig error = btrfs_parse_early_options(data, mode, fs_type, 124773f73415SJosef Bacik &subvol_name, &subvol_objectid, 12485e2a4b25SDavid Sterba &fs_devices); 1249f23c8af8SIlya Dryomov if (error) { 1250f23c8af8SIlya Dryomov kfree(subvol_name); 1251061dbc6bSAl Viro return ERR_PTR(error); 1252f23c8af8SIlya Dryomov } 1253edf24abeSChristoph Hellwig 1254830c4adbSJosef Bacik if (subvol_name) { 1255830c4adbSJosef Bacik root = mount_subvol(subvol_name, flags, device_name, data); 1256830c4adbSJosef Bacik kfree(subvol_name); 1257830c4adbSJosef Bacik return root; 1258830c4adbSJosef Bacik } 1259830c4adbSJosef Bacik 1260306e16ceSDavid Sterba error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices); 12618a4b83ccSChris Mason if (error) 1262830c4adbSJosef Bacik return ERR_PTR(error); 12634b82d6e4SYan 1264450ba0eaSJosef Bacik /* 1265450ba0eaSJosef Bacik * Setup a dummy root and fs_info for test/set super. This is because 1266450ba0eaSJosef Bacik * we don't actually fill this stuff out until open_ctree, but we need 1267450ba0eaSJosef Bacik * it for searching for existing supers, so this lets us do that and 1268450ba0eaSJosef Bacik * then open_ctree will properly initialize everything later. 1269450ba0eaSJosef Bacik */ 1270450ba0eaSJosef Bacik fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS); 127104d21a24SIlya Dryomov if (!fs_info) 127204d21a24SIlya Dryomov return ERR_PTR(-ENOMEM); 127304d21a24SIlya Dryomov 1274450ba0eaSJosef Bacik fs_info->fs_devices = fs_devices; 1275450ba0eaSJosef Bacik 12766c41761fSDavid Sterba fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 12776c41761fSDavid Sterba fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 12786c41761fSDavid Sterba if (!fs_info->super_copy || !fs_info->super_for_commit) { 12796c41761fSDavid Sterba error = -ENOMEM; 128004d21a24SIlya Dryomov goto error_fs_info; 128104d21a24SIlya Dryomov } 128204d21a24SIlya Dryomov 128304d21a24SIlya Dryomov error = btrfs_open_devices(fs_devices, mode, fs_type); 128404d21a24SIlya Dryomov if (error) 128504d21a24SIlya Dryomov goto error_fs_info; 128604d21a24SIlya Dryomov 128704d21a24SIlya Dryomov if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) { 128804d21a24SIlya Dryomov error = -EACCES; 12896c41761fSDavid Sterba goto error_close_devices; 12906c41761fSDavid Sterba } 12916c41761fSDavid Sterba 1292dfe25020SChris Mason bdev = fs_devices->latest_bdev; 12939249e17fSDavid Howells s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC, 12949249e17fSDavid Howells fs_info); 1295830c4adbSJosef Bacik if (IS_ERR(s)) { 1296830c4adbSJosef Bacik error = PTR_ERR(s); 1297830c4adbSJosef Bacik goto error_close_devices; 1298830c4adbSJosef Bacik } 12994b82d6e4SYan 13004b82d6e4SYan if (s->s_root) { 13012b82032cSYan Zheng btrfs_close_devices(fs_devices); 13026c41761fSDavid Sterba free_fs_info(fs_info); 130359553edfSAl Viro if ((flags ^ s->s_flags) & MS_RDONLY) 130459553edfSAl Viro error = -EBUSY; 13054b82d6e4SYan } else { 13064b82d6e4SYan char b[BDEVNAME_SIZE]; 13074b82d6e4SYan 13084b82d6e4SYan strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); 1309815745cfSAl Viro btrfs_sb(s)->bdev_holder = fs_type; 13108a4b83ccSChris Mason error = btrfs_fill_super(s, fs_devices, data, 13118a4b83ccSChris Mason flags & MS_SILENT ? 1 : 0); 13124b82d6e4SYan } 13134b82d6e4SYan 131459553edfSAl Viro root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error); 131559553edfSAl Viro if (IS_ERR(root)) 1316e15d0542SXin Zhong deactivate_locked_super(s); 13174b82d6e4SYan 1318061dbc6bSAl Viro return root; 13194b82d6e4SYan 1320c146afadSYan Zheng error_close_devices: 13218a4b83ccSChris Mason btrfs_close_devices(fs_devices); 132204d21a24SIlya Dryomov error_fs_info: 13236c41761fSDavid Sterba free_fs_info(fs_info); 1324061dbc6bSAl Viro return ERR_PTR(error); 13254b82d6e4SYan } 13262e635a27SChris Mason 13270d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 13280d2450abSSergei Trofimovich int new_pool_size, int old_pool_size) 13290d2450abSSergei Trofimovich { 13300d2450abSSergei Trofimovich if (new_pool_size == old_pool_size) 13310d2450abSSergei Trofimovich return; 13320d2450abSSergei Trofimovich 13330d2450abSSergei Trofimovich fs_info->thread_pool_size = new_pool_size; 13340d2450abSSergei Trofimovich 1335efe120a0SFrank Holton btrfs_info(fs_info, "resize thread pool %d -> %d", 13360d2450abSSergei Trofimovich old_pool_size, new_pool_size); 13370d2450abSSergei Trofimovich 13385cdc7ad3SQu Wenruo btrfs_workqueue_set_max(fs_info->workers, new_pool_size); 1339afe3d242SQu Wenruo btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size); 1340a8c93d4eSQu Wenruo btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size); 1341e66f0bb1SQu Wenruo btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size); 1342fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size); 1343fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size); 1344fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_write_workers, 1345fccb5d86SQu Wenruo new_pool_size); 1346fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size); 1347fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size); 13485b3bc44eSQu Wenruo btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size); 1349736cfa15SQu Wenruo btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size); 13500339ef2fSQu Wenruo btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers, 1351ff023aacSStefan Behrens new_pool_size); 13520d2450abSSergei Trofimovich } 13530d2450abSSergei Trofimovich 1354f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info) 1355dc81cdc5SMiao Xie { 1356dc81cdc5SMiao Xie set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1357f42a34b2SMiao Xie } 1358dc81cdc5SMiao Xie 1359f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info, 1360f42a34b2SMiao Xie unsigned long old_opts, int flags) 1361f42a34b2SMiao Xie { 1362dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1363dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1364dc81cdc5SMiao Xie (flags & MS_RDONLY))) { 1365dc81cdc5SMiao Xie /* wait for any defraggers to finish */ 1366dc81cdc5SMiao Xie wait_event(fs_info->transaction_wait, 1367dc81cdc5SMiao Xie (atomic_read(&fs_info->defrag_running) == 0)); 1368dc81cdc5SMiao Xie if (flags & MS_RDONLY) 1369dc81cdc5SMiao Xie sync_filesystem(fs_info->sb); 1370dc81cdc5SMiao Xie } 1371dc81cdc5SMiao Xie } 1372dc81cdc5SMiao Xie 1373dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info, 1374dc81cdc5SMiao Xie unsigned long old_opts) 1375dc81cdc5SMiao Xie { 1376dc81cdc5SMiao Xie /* 1377dc81cdc5SMiao Xie * We need cleanup all defragable inodes if the autodefragment is 1378dc81cdc5SMiao Xie * close or the fs is R/O. 1379dc81cdc5SMiao Xie */ 1380dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1381dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1382dc81cdc5SMiao Xie (fs_info->sb->s_flags & MS_RDONLY))) { 1383dc81cdc5SMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 1384dc81cdc5SMiao Xie } 1385dc81cdc5SMiao Xie 1386dc81cdc5SMiao Xie clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1387dc81cdc5SMiao Xie } 1388dc81cdc5SMiao Xie 1389c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1390c146afadSYan Zheng { 1391815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1392815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 139349b25e05SJeff Mahoney unsigned old_flags = sb->s_flags; 139449b25e05SJeff Mahoney unsigned long old_opts = fs_info->mount_opt; 139549b25e05SJeff Mahoney unsigned long old_compress_type = fs_info->compress_type; 139649b25e05SJeff Mahoney u64 old_max_inline = fs_info->max_inline; 139749b25e05SJeff Mahoney u64 old_alloc_start = fs_info->alloc_start; 139849b25e05SJeff Mahoney int old_thread_pool_size = fs_info->thread_pool_size; 139949b25e05SJeff Mahoney unsigned int old_metadata_ratio = fs_info->metadata_ratio; 1400c146afadSYan Zheng int ret; 1401c146afadSYan Zheng 140202b9984dSTheodore Ts'o sync_filesystem(sb); 1403f42a34b2SMiao Xie btrfs_remount_prepare(fs_info); 1404dc81cdc5SMiao Xie 1405b288052eSChris Mason ret = btrfs_parse_options(root, data); 140649b25e05SJeff Mahoney if (ret) { 140749b25e05SJeff Mahoney ret = -EINVAL; 140849b25e05SJeff Mahoney goto restore; 140949b25e05SJeff Mahoney } 1410b288052eSChris Mason 1411f42a34b2SMiao Xie btrfs_remount_begin(fs_info, old_opts, *flags); 14120d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 14130d2450abSSergei Trofimovich fs_info->thread_pool_size, old_thread_pool_size); 14140d2450abSSergei Trofimovich 1415c146afadSYan Zheng if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 1416dc81cdc5SMiao Xie goto out; 1417c146afadSYan Zheng 1418c146afadSYan Zheng if (*flags & MS_RDONLY) { 14198dabb742SStefan Behrens /* 14208dabb742SStefan Behrens * this also happens on 'umount -rf' or on shutdown, when 14218dabb742SStefan Behrens * the filesystem is busy. 14228dabb742SStefan Behrens */ 142321c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 1424361c093dSStefan Behrens 1425361c093dSStefan Behrens /* wait for the uuid_scan task to finish */ 1426361c093dSStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 1427361c093dSStefan Behrens /* avoid complains from lockdep et al. */ 1428361c093dSStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 1429361c093dSStefan Behrens 1430c146afadSYan Zheng sb->s_flags |= MS_RDONLY; 1431c146afadSYan Zheng 14328dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 14338dabb742SStefan Behrens btrfs_scrub_cancel(fs_info); 1434061594efSMiao Xie btrfs_pause_balance(fs_info); 14358dabb742SStefan Behrens 1436c146afadSYan Zheng ret = btrfs_commit_super(root); 143749b25e05SJeff Mahoney if (ret) 143849b25e05SJeff Mahoney goto restore; 1439c146afadSYan Zheng } else { 14406ef3de9cSDavid Sterba if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 14416ef3de9cSDavid Sterba btrfs_err(fs_info, 1442efe120a0SFrank Holton "Remounting read-write after error is not allowed"); 14436ef3de9cSDavid Sterba ret = -EINVAL; 14446ef3de9cSDavid Sterba goto restore; 14456ef3de9cSDavid Sterba } 14468a3db184SSergei Trofimovich if (fs_info->fs_devices->rw_devices == 0) { 144749b25e05SJeff Mahoney ret = -EACCES; 144849b25e05SJeff Mahoney goto restore; 14498a3db184SSergei Trofimovich } 14502b82032cSYan Zheng 1451292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 1452292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 1453292fd7fcSStefan Behrens !(*flags & MS_RDONLY)) { 1454efe120a0SFrank Holton btrfs_warn(fs_info, 1455efe120a0SFrank Holton "too many missing devices, writeable remount is not allowed"); 1456292fd7fcSStefan Behrens ret = -EACCES; 1457292fd7fcSStefan Behrens goto restore; 1458292fd7fcSStefan Behrens } 1459292fd7fcSStefan Behrens 14608a3db184SSergei Trofimovich if (btrfs_super_log_root(fs_info->super_copy) != 0) { 146149b25e05SJeff Mahoney ret = -EINVAL; 146249b25e05SJeff Mahoney goto restore; 14638a3db184SSergei Trofimovich } 1464c146afadSYan Zheng 1465815745cfSAl Viro ret = btrfs_cleanup_fs_roots(fs_info); 146649b25e05SJeff Mahoney if (ret) 146749b25e05SJeff Mahoney goto restore; 1468c146afadSYan Zheng 1469d68fc57bSYan, Zheng /* recover relocation */ 1470*5f316481SWang Shilong mutex_lock(&fs_info->cleaner_mutex); 1471d68fc57bSYan, Zheng ret = btrfs_recover_relocation(root); 1472*5f316481SWang Shilong mutex_unlock(&fs_info->cleaner_mutex); 147349b25e05SJeff Mahoney if (ret) 147449b25e05SJeff Mahoney goto restore; 1475c146afadSYan Zheng 14762b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 14772b6ba629SIlya Dryomov if (ret) 14782b6ba629SIlya Dryomov goto restore; 14792b6ba629SIlya Dryomov 14808dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 14818dabb742SStefan Behrens if (ret) { 1482efe120a0SFrank Holton btrfs_warn(fs_info, "failed to resume dev_replace"); 14838dabb742SStefan Behrens goto restore; 14848dabb742SStefan Behrens } 148594aebfb2SJosef Bacik 148694aebfb2SJosef Bacik if (!fs_info->uuid_root) { 1487efe120a0SFrank Holton btrfs_info(fs_info, "creating UUID tree"); 148894aebfb2SJosef Bacik ret = btrfs_create_uuid_tree(fs_info); 148994aebfb2SJosef Bacik if (ret) { 1490efe120a0SFrank Holton btrfs_warn(fs_info, "failed to create the UUID tree %d", ret); 149194aebfb2SJosef Bacik goto restore; 149294aebfb2SJosef Bacik } 149394aebfb2SJosef Bacik } 1494c146afadSYan Zheng sb->s_flags &= ~MS_RDONLY; 1495c146afadSYan Zheng } 1496dc81cdc5SMiao Xie out: 14972c6a92b0SJustin Maggard wake_up_process(fs_info->transaction_kthread); 1498dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 1499c146afadSYan Zheng return 0; 150049b25e05SJeff Mahoney 150149b25e05SJeff Mahoney restore: 150249b25e05SJeff Mahoney /* We've hit an error - don't reset MS_RDONLY */ 150349b25e05SJeff Mahoney if (sb->s_flags & MS_RDONLY) 150449b25e05SJeff Mahoney old_flags |= MS_RDONLY; 150549b25e05SJeff Mahoney sb->s_flags = old_flags; 150649b25e05SJeff Mahoney fs_info->mount_opt = old_opts; 150749b25e05SJeff Mahoney fs_info->compress_type = old_compress_type; 150849b25e05SJeff Mahoney fs_info->max_inline = old_max_inline; 1509c018daecSMiao Xie mutex_lock(&fs_info->chunk_mutex); 151049b25e05SJeff Mahoney fs_info->alloc_start = old_alloc_start; 1511c018daecSMiao Xie mutex_unlock(&fs_info->chunk_mutex); 15120d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 15130d2450abSSergei Trofimovich old_thread_pool_size, fs_info->thread_pool_size); 151449b25e05SJeff Mahoney fs_info->metadata_ratio = old_metadata_ratio; 1515dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 151649b25e05SJeff Mahoney return ret; 1517c146afadSYan Zheng } 1518c146afadSYan Zheng 1519bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 1520bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1, 1521bcd53741SArne Jansen const void *dev_info2) 1522bcd53741SArne Jansen { 1523bcd53741SArne Jansen if (((struct btrfs_device_info *)dev_info1)->max_avail > 1524bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1525bcd53741SArne Jansen return -1; 1526bcd53741SArne Jansen else if (((struct btrfs_device_info *)dev_info1)->max_avail < 1527bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1528bcd53741SArne Jansen return 1; 1529bcd53741SArne Jansen else 1530bcd53741SArne Jansen return 0; 1531bcd53741SArne Jansen } 1532bcd53741SArne Jansen 1533bcd53741SArne Jansen /* 1534bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 1535bcd53741SArne Jansen * is stored.(Descending Sort) 1536bcd53741SArne Jansen */ 1537bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 1538bcd53741SArne Jansen struct btrfs_device_info *devices, 1539bcd53741SArne Jansen size_t nr_devices) 1540bcd53741SArne Jansen { 1541bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 1542bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 1543bcd53741SArne Jansen } 1544bcd53741SArne Jansen 15456d07bcecSMiao Xie /* 15466d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 15476d07bcecSMiao Xie * file data. 15486d07bcecSMiao Xie */ 15496d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes) 15506d07bcecSMiao Xie { 15516d07bcecSMiao Xie struct btrfs_fs_info *fs_info = root->fs_info; 15526d07bcecSMiao Xie struct btrfs_device_info *devices_info; 15536d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 15546d07bcecSMiao Xie struct btrfs_device *device; 15556d07bcecSMiao Xie u64 skip_space; 15566d07bcecSMiao Xie u64 type; 15576d07bcecSMiao Xie u64 avail_space; 15586d07bcecSMiao Xie u64 used_space; 15596d07bcecSMiao Xie u64 min_stripe_size; 156039fb26c3SMiao Xie int min_stripes = 1, num_stripes = 1; 15616d07bcecSMiao Xie int i = 0, nr_devices; 15626d07bcecSMiao Xie int ret; 15636d07bcecSMiao Xie 1564b772a86eSLi Zefan nr_devices = fs_info->fs_devices->open_devices; 15656d07bcecSMiao Xie BUG_ON(!nr_devices); 15666d07bcecSMiao Xie 1567d9b0d9baSDulshani Gunawardhana devices_info = kmalloc_array(nr_devices, sizeof(*devices_info), 15686d07bcecSMiao Xie GFP_NOFS); 15696d07bcecSMiao Xie if (!devices_info) 15706d07bcecSMiao Xie return -ENOMEM; 15716d07bcecSMiao Xie 15726d07bcecSMiao Xie /* calc min stripe number for data space alloction */ 15736d07bcecSMiao Xie type = btrfs_get_alloc_profile(root, 1); 157439fb26c3SMiao Xie if (type & BTRFS_BLOCK_GROUP_RAID0) { 15756d07bcecSMiao Xie min_stripes = 2; 157639fb26c3SMiao Xie num_stripes = nr_devices; 157739fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID1) { 15786d07bcecSMiao Xie min_stripes = 2; 157939fb26c3SMiao Xie num_stripes = 2; 158039fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID10) { 15816d07bcecSMiao Xie min_stripes = 4; 158239fb26c3SMiao Xie num_stripes = 4; 158339fb26c3SMiao Xie } 15846d07bcecSMiao Xie 15856d07bcecSMiao Xie if (type & BTRFS_BLOCK_GROUP_DUP) 15866d07bcecSMiao Xie min_stripe_size = 2 * BTRFS_STRIPE_LEN; 15876d07bcecSMiao Xie else 15886d07bcecSMiao Xie min_stripe_size = BTRFS_STRIPE_LEN; 15896d07bcecSMiao Xie 1590b772a86eSLi Zefan list_for_each_entry(device, &fs_devices->devices, dev_list) { 159163a212abSStefan Behrens if (!device->in_fs_metadata || !device->bdev || 159263a212abSStefan Behrens device->is_tgtdev_for_dev_replace) 15936d07bcecSMiao Xie continue; 15946d07bcecSMiao Xie 15956d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 15966d07bcecSMiao Xie 15976d07bcecSMiao Xie /* align with stripe_len */ 15986d07bcecSMiao Xie do_div(avail_space, BTRFS_STRIPE_LEN); 15996d07bcecSMiao Xie avail_space *= BTRFS_STRIPE_LEN; 16006d07bcecSMiao Xie 16016d07bcecSMiao Xie /* 16026d07bcecSMiao Xie * In order to avoid overwritting the superblock on the drive, 16036d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 16046d07bcecSMiao Xie * allocation. 16056d07bcecSMiao Xie */ 16066d07bcecSMiao Xie skip_space = 1024 * 1024; 16076d07bcecSMiao Xie 16086d07bcecSMiao Xie /* user can set the offset in fs_info->alloc_start. */ 16096d07bcecSMiao Xie if (fs_info->alloc_start + BTRFS_STRIPE_LEN <= 16106d07bcecSMiao Xie device->total_bytes) 16116d07bcecSMiao Xie skip_space = max(fs_info->alloc_start, skip_space); 16126d07bcecSMiao Xie 16136d07bcecSMiao Xie /* 16146d07bcecSMiao Xie * btrfs can not use the free space in [0, skip_space - 1], 16156d07bcecSMiao Xie * we must subtract it from the total. In order to implement 16166d07bcecSMiao Xie * it, we account the used space in this range first. 16176d07bcecSMiao Xie */ 16186d07bcecSMiao Xie ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1, 16196d07bcecSMiao Xie &used_space); 16206d07bcecSMiao Xie if (ret) { 16216d07bcecSMiao Xie kfree(devices_info); 16226d07bcecSMiao Xie return ret; 16236d07bcecSMiao Xie } 16246d07bcecSMiao Xie 16256d07bcecSMiao Xie /* calc the free space in [0, skip_space - 1] */ 16266d07bcecSMiao Xie skip_space -= used_space; 16276d07bcecSMiao Xie 16286d07bcecSMiao Xie /* 16296d07bcecSMiao Xie * we can use the free space in [0, skip_space - 1], subtract 16306d07bcecSMiao Xie * it from the total. 16316d07bcecSMiao Xie */ 16326d07bcecSMiao Xie if (avail_space && avail_space >= skip_space) 16336d07bcecSMiao Xie avail_space -= skip_space; 16346d07bcecSMiao Xie else 16356d07bcecSMiao Xie avail_space = 0; 16366d07bcecSMiao Xie 16376d07bcecSMiao Xie if (avail_space < min_stripe_size) 16386d07bcecSMiao Xie continue; 16396d07bcecSMiao Xie 16406d07bcecSMiao Xie devices_info[i].dev = device; 16416d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 16426d07bcecSMiao Xie 16436d07bcecSMiao Xie i++; 16446d07bcecSMiao Xie } 16456d07bcecSMiao Xie 16466d07bcecSMiao Xie nr_devices = i; 16476d07bcecSMiao Xie 16486d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 16496d07bcecSMiao Xie 16506d07bcecSMiao Xie i = nr_devices - 1; 16516d07bcecSMiao Xie avail_space = 0; 16526d07bcecSMiao Xie while (nr_devices >= min_stripes) { 165339fb26c3SMiao Xie if (num_stripes > nr_devices) 165439fb26c3SMiao Xie num_stripes = nr_devices; 165539fb26c3SMiao Xie 16566d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 16576d07bcecSMiao Xie int j; 16586d07bcecSMiao Xie u64 alloc_size; 16596d07bcecSMiao Xie 166039fb26c3SMiao Xie avail_space += devices_info[i].max_avail * num_stripes; 16616d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 166239fb26c3SMiao Xie for (j = i + 1 - num_stripes; j <= i; j++) 16636d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 16646d07bcecSMiao Xie } 16656d07bcecSMiao Xie i--; 16666d07bcecSMiao Xie nr_devices--; 16676d07bcecSMiao Xie } 16686d07bcecSMiao Xie 16696d07bcecSMiao Xie kfree(devices_info); 16706d07bcecSMiao Xie *free_bytes = avail_space; 16716d07bcecSMiao Xie return 0; 16726d07bcecSMiao Xie } 16736d07bcecSMiao Xie 16748fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 16758fd17795SChris Mason { 1676815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 1677815745cfSAl Viro struct btrfs_super_block *disk_super = fs_info->super_copy; 1678815745cfSAl Viro struct list_head *head = &fs_info->space_info; 1679bd4d1088SJosef Bacik struct btrfs_space_info *found; 1680bd4d1088SJosef Bacik u64 total_used = 0; 16816d07bcecSMiao Xie u64 total_free_data = 0; 1682db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 1683815745cfSAl Viro __be32 *fsid = (__be32 *)fs_info->fsid; 16846d07bcecSMiao Xie int ret; 16858fd17795SChris Mason 16866d07bcecSMiao Xie /* holding chunk_muext to avoid allocating new chunks */ 1687815745cfSAl Viro mutex_lock(&fs_info->chunk_mutex); 1688bd4d1088SJosef Bacik rcu_read_lock(); 168989a55897SJosef Bacik list_for_each_entry_rcu(found, head, list) { 16906d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 16916d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 16926d07bcecSMiao Xie total_free_data -= 16936d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 16946d07bcecSMiao Xie } 16956d07bcecSMiao Xie 1696b742bb82SYan, Zheng total_used += found->disk_used; 169789a55897SJosef Bacik } 1698bd4d1088SJosef Bacik rcu_read_unlock(); 1699bd4d1088SJosef Bacik 17008fd17795SChris Mason buf->f_namelen = BTRFS_NAME_LEN; 1701db94535dSChris Mason buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits; 1702bd4d1088SJosef Bacik buf->f_bfree = buf->f_blocks - (total_used >> bits); 17038fd17795SChris Mason buf->f_bsize = dentry->d_sb->s_blocksize; 17048fd17795SChris Mason buf->f_type = BTRFS_SUPER_MAGIC; 17056d07bcecSMiao Xie buf->f_bavail = total_free_data; 1706815745cfSAl Viro ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data); 17076d07bcecSMiao Xie if (ret) { 1708815745cfSAl Viro mutex_unlock(&fs_info->chunk_mutex); 17096d07bcecSMiao Xie return ret; 17106d07bcecSMiao Xie } 17116d07bcecSMiao Xie buf->f_bavail += total_free_data; 17126d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 1713815745cfSAl Viro mutex_unlock(&fs_info->chunk_mutex); 1714d397712bSChris Mason 17159d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 17169d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 17179d03632eSDavid Woodhouse on a big-endian or little-endian host */ 17189d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 17199d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 172032d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 172132d48fa1SDavid Woodhouse buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32; 172232d48fa1SDavid Woodhouse buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid; 172332d48fa1SDavid Woodhouse 17248fd17795SChris Mason return 0; 17258fd17795SChris Mason } 1726b5133862SChris Mason 1727aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb) 1728aea52e19SAl Viro { 1729815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1730aea52e19SAl Viro kill_anon_super(sb); 1731aea52e19SAl Viro free_fs_info(fs_info); 1732aea52e19SAl Viro } 1733aea52e19SAl Viro 17342e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 17352e635a27SChris Mason .owner = THIS_MODULE, 17362e635a27SChris Mason .name = "btrfs", 1737061dbc6bSAl Viro .mount = btrfs_mount, 1738aea52e19SAl Viro .kill_sb = btrfs_kill_super, 17392e635a27SChris Mason .fs_flags = FS_REQUIRES_DEV, 17402e635a27SChris Mason }; 17417f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs"); 1742a9218f6bSChris Mason 1743d352ac68SChris Mason /* 1744d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 1745d352ac68SChris Mason */ 17468a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 17478a4b83ccSChris Mason unsigned long arg) 17488a4b83ccSChris Mason { 17498a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 17508a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 1751c071fcfdSChris Mason int ret = -ENOTTY; 17528a4b83ccSChris Mason 1753e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1754e441d54dSChris Mason return -EPERM; 1755e441d54dSChris Mason 1756dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 1757dae7b665SLi Zefan if (IS_ERR(vol)) 1758dae7b665SLi Zefan return PTR_ERR(vol); 1759c071fcfdSChris Mason 17608a4b83ccSChris Mason switch (cmd) { 17618a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 176297288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 17638a4b83ccSChris Mason &btrfs_fs_type, &fs_devices); 17648a4b83ccSChris Mason break; 176502db0844SJosef Bacik case BTRFS_IOC_DEVICES_READY: 176602db0844SJosef Bacik ret = btrfs_scan_one_device(vol->name, FMODE_READ, 176702db0844SJosef Bacik &btrfs_fs_type, &fs_devices); 176802db0844SJosef Bacik if (ret) 176902db0844SJosef Bacik break; 177002db0844SJosef Bacik ret = !(fs_devices->num_devices == fs_devices->total_devices); 177102db0844SJosef Bacik break; 17728a4b83ccSChris Mason } 1773dae7b665SLi Zefan 17748a4b83ccSChris Mason kfree(vol); 1775f819d837SLinda Knippers return ret; 17768a4b83ccSChris Mason } 17778a4b83ccSChris Mason 17780176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 1779ed0dab6bSYan { 1780354aa0fbSMiao Xie struct btrfs_trans_handle *trans; 1781354aa0fbSMiao Xie struct btrfs_root *root = btrfs_sb(sb)->tree_root; 1782354aa0fbSMiao Xie 1783d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 1784354aa0fbSMiao Xie if (IS_ERR(trans)) { 1785354aa0fbSMiao Xie /* no transaction, don't bother */ 1786354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 17870176260fSLinus Torvalds return 0; 1788354aa0fbSMiao Xie return PTR_ERR(trans); 1789354aa0fbSMiao Xie } 1790354aa0fbSMiao Xie return btrfs_commit_transaction(trans, root); 1791ed0dab6bSYan } 1792ed0dab6bSYan 17930176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb) 1794ed0dab6bSYan { 17950176260fSLinus Torvalds return 0; 1796ed0dab6bSYan } 17972e635a27SChris Mason 17989c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 17999c5085c1SJosef Bacik { 18009c5085c1SJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 18019c5085c1SJosef Bacik struct btrfs_fs_devices *cur_devices; 18029c5085c1SJosef Bacik struct btrfs_device *dev, *first_dev = NULL; 18039c5085c1SJosef Bacik struct list_head *head; 18049c5085c1SJosef Bacik struct rcu_string *name; 18059c5085c1SJosef Bacik 18069c5085c1SJosef Bacik mutex_lock(&fs_info->fs_devices->device_list_mutex); 18079c5085c1SJosef Bacik cur_devices = fs_info->fs_devices; 18089c5085c1SJosef Bacik while (cur_devices) { 18099c5085c1SJosef Bacik head = &cur_devices->devices; 18109c5085c1SJosef Bacik list_for_each_entry(dev, head, dev_list) { 1811aa9ddcd4SJosef Bacik if (dev->missing) 1812aa9ddcd4SJosef Bacik continue; 18139c5085c1SJosef Bacik if (!first_dev || dev->devid < first_dev->devid) 18149c5085c1SJosef Bacik first_dev = dev; 18159c5085c1SJosef Bacik } 18169c5085c1SJosef Bacik cur_devices = cur_devices->seed; 18179c5085c1SJosef Bacik } 18189c5085c1SJosef Bacik 18199c5085c1SJosef Bacik if (first_dev) { 18209c5085c1SJosef Bacik rcu_read_lock(); 18219c5085c1SJosef Bacik name = rcu_dereference(first_dev->name); 18229c5085c1SJosef Bacik seq_escape(m, name->str, " \t\n\\"); 18239c5085c1SJosef Bacik rcu_read_unlock(); 18249c5085c1SJosef Bacik } else { 18259c5085c1SJosef Bacik WARN_ON(1); 18269c5085c1SJosef Bacik } 18279c5085c1SJosef Bacik mutex_unlock(&fs_info->fs_devices->device_list_mutex); 18289c5085c1SJosef Bacik return 0; 18299c5085c1SJosef Bacik } 18309c5085c1SJosef Bacik 1831b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 183276dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 1833bd555975SAl Viro .evict_inode = btrfs_evict_inode, 1834e20d96d6SChris Mason .put_super = btrfs_put_super, 1835d5719762SChris Mason .sync_fs = btrfs_sync_fs, 1836a9572a15SEric Paris .show_options = btrfs_show_options, 18379c5085c1SJosef Bacik .show_devname = btrfs_show_devname, 18384730a4bcSChris Mason .write_inode = btrfs_write_inode, 18392c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 18402c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 18418fd17795SChris Mason .statfs = btrfs_statfs, 1842c146afadSYan Zheng .remount_fs = btrfs_remount, 18430176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 18440176260fSLinus Torvalds .unfreeze_fs = btrfs_unfreeze, 1845e20d96d6SChris Mason }; 1846a9218f6bSChris Mason 1847a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 1848a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 1849a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 1850a9218f6bSChris Mason .owner = THIS_MODULE, 18516038f373SArnd Bergmann .llseek = noop_llseek, 1852a9218f6bSChris Mason }; 1853a9218f6bSChris Mason 1854a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 1855578454ffSKay Sievers .minor = BTRFS_MINOR, 1856a9218f6bSChris Mason .name = "btrfs-control", 1857a9218f6bSChris Mason .fops = &btrfs_ctl_fops 1858a9218f6bSChris Mason }; 1859a9218f6bSChris Mason 1860578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 1861578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 1862578454ffSKay Sievers 1863a9218f6bSChris Mason static int btrfs_interface_init(void) 1864a9218f6bSChris Mason { 1865a9218f6bSChris Mason return misc_register(&btrfs_misc); 1866a9218f6bSChris Mason } 1867a9218f6bSChris Mason 1868b2950863SChristoph Hellwig static void btrfs_interface_exit(void) 1869a9218f6bSChris Mason { 1870a9218f6bSChris Mason if (misc_deregister(&btrfs_misc) < 0) 1871efe120a0SFrank Holton printk(KERN_INFO "BTRFS: misc_deregister failed for control device\n"); 1872a9218f6bSChris Mason } 1873a9218f6bSChris Mason 187485965600SDavid Sterba static void btrfs_print_info(void) 187585965600SDavid Sterba { 187685965600SDavid Sterba printk(KERN_INFO "Btrfs loaded" 187785965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 187885965600SDavid Sterba ", debug=on" 187985965600SDavid Sterba #endif 188079556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT 188179556c3dSStefan Behrens ", assert=on" 188279556c3dSStefan Behrens #endif 188385965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 188485965600SDavid Sterba ", integrity-checker=on" 188585965600SDavid Sterba #endif 188685965600SDavid Sterba "\n"); 188785965600SDavid Sterba } 188885965600SDavid Sterba 1889dc11dd5dSJosef Bacik static int btrfs_run_sanity_tests(void) 1890dc11dd5dSJosef Bacik { 189106ea65a3SJosef Bacik int ret; 189206ea65a3SJosef Bacik 1893294e30feSJosef Bacik ret = btrfs_init_test_fs(); 189406ea65a3SJosef Bacik if (ret) 189506ea65a3SJosef Bacik return ret; 1896294e30feSJosef Bacik 1897294e30feSJosef Bacik ret = btrfs_test_free_space_cache(); 1898294e30feSJosef Bacik if (ret) 1899294e30feSJosef Bacik goto out; 1900294e30feSJosef Bacik ret = btrfs_test_extent_buffer_operations(); 1901294e30feSJosef Bacik if (ret) 1902294e30feSJosef Bacik goto out; 1903294e30feSJosef Bacik ret = btrfs_test_extent_io(); 1904aaedb55bSJosef Bacik if (ret) 1905aaedb55bSJosef Bacik goto out; 1906aaedb55bSJosef Bacik ret = btrfs_test_inodes(); 1907faa2dbf0SJosef Bacik if (ret) 1908faa2dbf0SJosef Bacik goto out; 1909faa2dbf0SJosef Bacik ret = btrfs_test_qgroups(); 1910294e30feSJosef Bacik out: 1911294e30feSJosef Bacik btrfs_destroy_test_fs(); 1912294e30feSJosef Bacik return ret; 1913dc11dd5dSJosef Bacik } 1914dc11dd5dSJosef Bacik 19152e635a27SChris Mason static int __init init_btrfs_fs(void) 19162e635a27SChris Mason { 19172c90e5d6SChris Mason int err; 191858176a96SJosef Bacik 191914a958e6SFilipe David Borba Manana err = btrfs_hash_init(); 192014a958e6SFilipe David Borba Manana if (err) 192114a958e6SFilipe David Borba Manana return err; 192214a958e6SFilipe David Borba Manana 192363541927SFilipe David Borba Manana btrfs_props_init(); 192463541927SFilipe David Borba Manana 192558176a96SJosef Bacik err = btrfs_init_sysfs(); 192658176a96SJosef Bacik if (err) 192714a958e6SFilipe David Borba Manana goto free_hash; 192858176a96SJosef Bacik 1929143bede5SJeff Mahoney btrfs_init_compress(); 1930d1310b2eSChris Mason 1931261507a0SLi Zefan err = btrfs_init_cachep(); 1932261507a0SLi Zefan if (err) 1933261507a0SLi Zefan goto free_compress; 1934261507a0SLi Zefan 1935d1310b2eSChris Mason err = extent_io_init(); 19362f4cbe64SWyatt Banks if (err) 19372f4cbe64SWyatt Banks goto free_cachep; 19382f4cbe64SWyatt Banks 1939d1310b2eSChris Mason err = extent_map_init(); 1940d1310b2eSChris Mason if (err) 1941d1310b2eSChris Mason goto free_extent_io; 1942d1310b2eSChris Mason 19436352b91dSMiao Xie err = ordered_data_init(); 19442f4cbe64SWyatt Banks if (err) 19452f4cbe64SWyatt Banks goto free_extent_map; 1946c8b97818SChris Mason 19476352b91dSMiao Xie err = btrfs_delayed_inode_init(); 19486352b91dSMiao Xie if (err) 19496352b91dSMiao Xie goto free_ordered_data; 19506352b91dSMiao Xie 19519247f317SMiao Xie err = btrfs_auto_defrag_init(); 195216cdcec7SMiao Xie if (err) 195316cdcec7SMiao Xie goto free_delayed_inode; 195416cdcec7SMiao Xie 195578a6184aSMiao Xie err = btrfs_delayed_ref_init(); 19569247f317SMiao Xie if (err) 19579247f317SMiao Xie goto free_auto_defrag; 19589247f317SMiao Xie 1959b9e9a6cbSWang Shilong err = btrfs_prelim_ref_init(); 1960b9e9a6cbSWang Shilong if (err) 1961b9e9a6cbSWang Shilong goto free_prelim_ref; 1962b9e9a6cbSWang Shilong 196378a6184aSMiao Xie err = btrfs_interface_init(); 196478a6184aSMiao Xie if (err) 196578a6184aSMiao Xie goto free_delayed_ref; 196678a6184aSMiao Xie 1967e565d4b9SJan Schmidt btrfs_init_lockdep(); 1968e565d4b9SJan Schmidt 196985965600SDavid Sterba btrfs_print_info(); 1970dc11dd5dSJosef Bacik 1971dc11dd5dSJosef Bacik err = btrfs_run_sanity_tests(); 1972dc11dd5dSJosef Bacik if (err) 1973dc11dd5dSJosef Bacik goto unregister_ioctl; 1974dc11dd5dSJosef Bacik 1975dc11dd5dSJosef Bacik err = register_filesystem(&btrfs_fs_type); 1976dc11dd5dSJosef Bacik if (err) 1977dc11dd5dSJosef Bacik goto unregister_ioctl; 197874255aa0SJosef Bacik 19792f4cbe64SWyatt Banks return 0; 19802f4cbe64SWyatt Banks 1981a9218f6bSChris Mason unregister_ioctl: 1982a9218f6bSChris Mason btrfs_interface_exit(); 1983b9e9a6cbSWang Shilong free_prelim_ref: 1984b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 198578a6184aSMiao Xie free_delayed_ref: 198678a6184aSMiao Xie btrfs_delayed_ref_exit(); 19879247f317SMiao Xie free_auto_defrag: 19889247f317SMiao Xie btrfs_auto_defrag_exit(); 198916cdcec7SMiao Xie free_delayed_inode: 199016cdcec7SMiao Xie btrfs_delayed_inode_exit(); 19916352b91dSMiao Xie free_ordered_data: 19926352b91dSMiao Xie ordered_data_exit(); 19932f4cbe64SWyatt Banks free_extent_map: 19942f4cbe64SWyatt Banks extent_map_exit(); 1995d1310b2eSChris Mason free_extent_io: 1996d1310b2eSChris Mason extent_io_exit(); 19972f4cbe64SWyatt Banks free_cachep: 19982f4cbe64SWyatt Banks btrfs_destroy_cachep(); 1999261507a0SLi Zefan free_compress: 2000261507a0SLi Zefan btrfs_exit_compress(); 20012f4cbe64SWyatt Banks btrfs_exit_sysfs(); 200214a958e6SFilipe David Borba Manana free_hash: 200314a958e6SFilipe David Borba Manana btrfs_hash_exit(); 20042c90e5d6SChris Mason return err; 20052e635a27SChris Mason } 20062e635a27SChris Mason 20072e635a27SChris Mason static void __exit exit_btrfs_fs(void) 20082e635a27SChris Mason { 200939279cc3SChris Mason btrfs_destroy_cachep(); 201078a6184aSMiao Xie btrfs_delayed_ref_exit(); 20119247f317SMiao Xie btrfs_auto_defrag_exit(); 201216cdcec7SMiao Xie btrfs_delayed_inode_exit(); 2013b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 20146352b91dSMiao Xie ordered_data_exit(); 2015a52d9a80SChris Mason extent_map_exit(); 2016d1310b2eSChris Mason extent_io_exit(); 2017a9218f6bSChris Mason btrfs_interface_exit(); 20182e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 201958176a96SJosef Bacik btrfs_exit_sysfs(); 20208a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 2021261507a0SLi Zefan btrfs_exit_compress(); 202214a958e6SFilipe David Borba Manana btrfs_hash_exit(); 20232e635a27SChris Mason } 20242e635a27SChris Mason 202560efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs); 20262e635a27SChris Mason module_exit(exit_btrfs_fs) 20272e635a27SChris Mason 20282e635a27SChris Mason MODULE_LICENSE("GPL"); 2029