16cbd5570SChris Mason /* 26cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 36cbd5570SChris Mason * 46cbd5570SChris Mason * This program is free software; you can redistribute it and/or 56cbd5570SChris Mason * modify it under the terms of the GNU General Public 66cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 76cbd5570SChris Mason * 86cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 96cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 106cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 116cbd5570SChris Mason * General Public License for more details. 126cbd5570SChris Mason * 136cbd5570SChris Mason * You should have received a copy of the GNU General Public 146cbd5570SChris Mason * License along with this program; if not, write to the 156cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 166cbd5570SChris Mason * Boston, MA 021110-1307, USA. 176cbd5570SChris Mason */ 186cbd5570SChris Mason 194b82d6e4SYan #include <linux/blkdev.h> 202e635a27SChris Mason #include <linux/module.h> 21e20d96d6SChris Mason #include <linux/buffer_head.h> 222e635a27SChris Mason #include <linux/fs.h> 232e635a27SChris Mason #include <linux/pagemap.h> 242e635a27SChris Mason #include <linux/highmem.h> 252e635a27SChris Mason #include <linux/time.h> 262e635a27SChris Mason #include <linux/init.h> 27a9572a15SEric Paris #include <linux/seq_file.h> 282e635a27SChris Mason #include <linux/string.h> 292e635a27SChris Mason #include <linux/backing-dev.h> 304b82d6e4SYan #include <linux/mount.h> 31dee26a9fSChris Mason #include <linux/mpage.h> 3275dfe396SChris Mason #include <linux/swap.h> 3375dfe396SChris Mason #include <linux/writeback.h> 348fd17795SChris Mason #include <linux/statfs.h> 3508607c1bSChris Mason #include <linux/compat.h> 3695e05289SChris Mason #include <linux/parser.h> 37c59f8951SChris Mason #include <linux/ctype.h> 386da6abaeSChris Mason #include <linux/namei.h> 39a9218f6bSChris Mason #include <linux/miscdevice.h> 401bcbf313SQinghuang Feng #include <linux/magic.h> 415a0e3ad6STejun Heo #include <linux/slab.h> 4290a887c9SDan Magenheimer #include <linux/cleancache.h> 4322c44fe6SJosef Bacik #include <linux/ratelimit.h> 4455e301fdSFilipe Brandenburger #include <linux/btrfs.h> 4516cdcec7SMiao Xie #include "delayed-inode.h" 462e635a27SChris Mason #include "ctree.h" 47e20d96d6SChris Mason #include "disk-io.h" 48d5719762SChris Mason #include "transaction.h" 492c90e5d6SChris Mason #include "btrfs_inode.h" 503a686375SChris Mason #include "print-tree.h" 5114a958e6SFilipe David Borba Manana #include "hash.h" 5263541927SFilipe David Borba Manana #include "props.h" 535103e947SJosef Bacik #include "xattr.h" 548a4b83ccSChris Mason #include "volumes.h" 55be6e8dc0SBalaji Rao #include "export.h" 56c8b97818SChris Mason #include "compression.h" 579c5085c1SJosef Bacik #include "rcu-string.h" 588dabb742SStefan Behrens #include "dev-replace.h" 5974255aa0SJosef Bacik #include "free-space-cache.h" 60b9e9a6cbSWang Shilong #include "backref.h" 61dc11dd5dSJosef Bacik #include "tests/btrfs-tests.h" 622e635a27SChris Mason 63d3982100SMark Fasheh #include "qgroup.h" 641abe9b8aSliubo #define CREATE_TRACE_POINTS 651abe9b8aSliubo #include <trace/events/btrfs.h> 661abe9b8aSliubo 67b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops; 68830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type; 69e20d96d6SChris Mason 700723a047SHarald Hoyer static int btrfs_remount(struct super_block *sb, int *flags, char *data); 710723a047SHarald Hoyer 72e33e17eeSJeff Mahoney const char *btrfs_decode_error(int errno) 73acce952bSliubo { 7408748810SDavid Sterba char *errstr = "unknown"; 75acce952bSliubo 76acce952bSliubo switch (errno) { 77acce952bSliubo case -EIO: 78acce952bSliubo errstr = "IO failure"; 79acce952bSliubo break; 80acce952bSliubo case -ENOMEM: 81acce952bSliubo errstr = "Out of memory"; 82acce952bSliubo break; 83acce952bSliubo case -EROFS: 84acce952bSliubo errstr = "Readonly filesystem"; 85acce952bSliubo break; 868c342930SJeff Mahoney case -EEXIST: 878c342930SJeff Mahoney errstr = "Object already exists"; 888c342930SJeff Mahoney break; 8994ef7280SDavid Sterba case -ENOSPC: 9094ef7280SDavid Sterba errstr = "No space left"; 9194ef7280SDavid Sterba break; 9294ef7280SDavid Sterba case -ENOENT: 9394ef7280SDavid Sterba errstr = "No such entry"; 9494ef7280SDavid Sterba break; 95acce952bSliubo } 96acce952bSliubo 97acce952bSliubo return errstr; 98acce952bSliubo } 99acce952bSliubo 100bbece8a3SDavid Sterba static void save_error_info(struct btrfs_fs_info *fs_info) 101acce952bSliubo { 102acce952bSliubo /* 103acce952bSliubo * today we only save the error info into ram. Long term we'll 104acce952bSliubo * also send it down to the disk 105acce952bSliubo */ 10687533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 107acce952bSliubo } 108acce952bSliubo 109acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */ 110acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info) 111acce952bSliubo { 112acce952bSliubo struct super_block *sb = fs_info->sb; 113acce952bSliubo 114acce952bSliubo if (sb->s_flags & MS_RDONLY) 115acce952bSliubo return; 116acce952bSliubo 11787533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 118acce952bSliubo sb->s_flags |= MS_RDONLY; 119c2cf52ebSSimon Kirby btrfs_info(fs_info, "forced readonly"); 1201acd6831SStefan Behrens /* 1211acd6831SStefan Behrens * Note that a running device replace operation is not 1221acd6831SStefan Behrens * canceled here although there is no way to update 1231acd6831SStefan Behrens * the progress. It would add the risk of a deadlock, 1241acd6831SStefan Behrens * therefore the canceling is ommited. The only penalty 1251acd6831SStefan Behrens * is that some I/O remains active until the procedure 1261acd6831SStefan Behrens * completes. The next time when the filesystem is 1271acd6831SStefan Behrens * mounted writeable again, the device replace 1281acd6831SStefan Behrens * operation continues. 1291acd6831SStefan Behrens */ 130acce952bSliubo } 131acce952bSliubo } 132acce952bSliubo 133acce952bSliubo /* 134acce952bSliubo * __btrfs_std_error decodes expected errors from the caller and 135acce952bSliubo * invokes the approciate error response. 136acce952bSliubo */ 137c0d19e2bSDavid Sterba __cold 138acce952bSliubo void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function, 1394da35113SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 140acce952bSliubo { 141acce952bSliubo struct super_block *sb = fs_info->sb; 14257d816a1SAnand Jain #ifdef CONFIG_PRINTK 143acce952bSliubo const char *errstr; 14457d816a1SAnand Jain #endif 145acce952bSliubo 146acce952bSliubo /* 147acce952bSliubo * Special case: if the error is EROFS, and we're already 148acce952bSliubo * under MS_RDONLY, then it is safe here. 149acce952bSliubo */ 150acce952bSliubo if (errno == -EROFS && (sb->s_flags & MS_RDONLY)) 151acce952bSliubo return; 152acce952bSliubo 15357d816a1SAnand Jain #ifdef CONFIG_PRINTK 15408748810SDavid Sterba errstr = btrfs_decode_error(errno); 1554da35113SJeff Mahoney if (fmt) { 15637252a66SEric Sandeen struct va_format vaf; 15737252a66SEric Sandeen va_list args; 15837252a66SEric Sandeen 15937252a66SEric Sandeen va_start(args, fmt); 16037252a66SEric Sandeen vaf.fmt = fmt; 16137252a66SEric Sandeen vaf.va = &args; 1624da35113SJeff Mahoney 163efe120a0SFrank Holton printk(KERN_CRIT 164efe120a0SFrank Holton "BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n", 16508748810SDavid Sterba sb->s_id, function, line, errno, errstr, &vaf); 16637252a66SEric Sandeen va_end(args); 1674da35113SJeff Mahoney } else { 168efe120a0SFrank Holton printk(KERN_CRIT "BTRFS: error (device %s) in %s:%d: errno=%d %s\n", 16908748810SDavid Sterba sb->s_id, function, line, errno, errstr); 1704da35113SJeff Mahoney } 17157d816a1SAnand Jain #endif 172acce952bSliubo 1734da35113SJeff Mahoney /* Don't go through full error handling during mount */ 1744da35113SJeff Mahoney save_error_info(fs_info); 175cf79ffb5SJosef Bacik if (sb->s_flags & MS_BORN) 176acce952bSliubo btrfs_handle_error(fs_info); 177acce952bSliubo } 1784da35113SJeff Mahoney 17957d816a1SAnand Jain #ifdef CONFIG_PRINTK 180533574c6SJoe Perches static const char * const logtypes[] = { 1814da35113SJeff Mahoney "emergency", 1824da35113SJeff Mahoney "alert", 1834da35113SJeff Mahoney "critical", 1844da35113SJeff Mahoney "error", 1854da35113SJeff Mahoney "warning", 1864da35113SJeff Mahoney "notice", 1874da35113SJeff Mahoney "info", 1884da35113SJeff Mahoney "debug", 1894da35113SJeff Mahoney }; 1904da35113SJeff Mahoney 191c2cf52ebSSimon Kirby void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...) 1924da35113SJeff Mahoney { 1934da35113SJeff Mahoney struct super_block *sb = fs_info->sb; 1944da35113SJeff Mahoney char lvl[4]; 1954da35113SJeff Mahoney struct va_format vaf; 1964da35113SJeff Mahoney va_list args; 1974da35113SJeff Mahoney const char *type = logtypes[4]; 198533574c6SJoe Perches int kern_level; 1994da35113SJeff Mahoney 2004da35113SJeff Mahoney va_start(args, fmt); 2014da35113SJeff Mahoney 202533574c6SJoe Perches kern_level = printk_get_level(fmt); 203533574c6SJoe Perches if (kern_level) { 204533574c6SJoe Perches size_t size = printk_skip_level(fmt) - fmt; 205533574c6SJoe Perches memcpy(lvl, fmt, size); 206533574c6SJoe Perches lvl[size] = '\0'; 207533574c6SJoe Perches fmt += size; 208533574c6SJoe Perches type = logtypes[kern_level - '0']; 2094da35113SJeff Mahoney } else 2104da35113SJeff Mahoney *lvl = '\0'; 2114da35113SJeff Mahoney 2124da35113SJeff Mahoney vaf.fmt = fmt; 2134da35113SJeff Mahoney vaf.va = &args; 214533574c6SJoe Perches 215c2cf52ebSSimon Kirby printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf); 216533574c6SJoe Perches 217533574c6SJoe Perches va_end(args); 2184da35113SJeff Mahoney } 219533574c6SJoe Perches #endif 220533574c6SJoe Perches 2218c342930SJeff Mahoney /* 22249b25e05SJeff Mahoney * We only mark the transaction aborted and then set the file system read-only. 22349b25e05SJeff Mahoney * This will prevent new transactions from starting or trying to join this 22449b25e05SJeff Mahoney * one. 22549b25e05SJeff Mahoney * 22649b25e05SJeff Mahoney * This means that error recovery at the call site is limited to freeing 22749b25e05SJeff Mahoney * any local memory allocations and passing the error code up without 22849b25e05SJeff Mahoney * further cleanup. The transaction should complete as it normally would 22949b25e05SJeff Mahoney * in the call path but will return -EIO. 23049b25e05SJeff Mahoney * 23149b25e05SJeff Mahoney * We'll complete the cleanup in btrfs_end_transaction and 23249b25e05SJeff Mahoney * btrfs_commit_transaction. 23349b25e05SJeff Mahoney */ 234c0d19e2bSDavid Sterba __cold 23549b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans, 23649b25e05SJeff Mahoney struct btrfs_root *root, const char *function, 23749b25e05SJeff Mahoney unsigned int line, int errno) 23849b25e05SJeff Mahoney { 23949b25e05SJeff Mahoney trans->aborted = errno; 24049b25e05SJeff Mahoney /* Nothing used. The other threads that have joined this 24149b25e05SJeff Mahoney * transaction may be able to continue. */ 242c92f6be3SFilipe Manana if (!trans->blocks_used && list_empty(&trans->new_bgs)) { 24369ce977aSMiao Xie const char *errstr; 24469ce977aSMiao Xie 24508748810SDavid Sterba errstr = btrfs_decode_error(errno); 246c2cf52ebSSimon Kirby btrfs_warn(root->fs_info, 247c2cf52ebSSimon Kirby "%s:%d: Aborting unused transaction(%s).", 24869ce977aSMiao Xie function, line, errstr); 24949b25e05SJeff Mahoney return; 25049b25e05SJeff Mahoney } 2518d25a086SMiao Xie ACCESS_ONCE(trans->transaction->aborted) = errno; 252501407aaSJosef Bacik /* Wake up anybody who may be waiting on this transaction */ 253501407aaSJosef Bacik wake_up(&root->fs_info->transaction_wait); 254501407aaSJosef Bacik wake_up(&root->fs_info->transaction_blocked_wait); 25549b25e05SJeff Mahoney __btrfs_std_error(root->fs_info, function, line, errno, NULL); 25649b25e05SJeff Mahoney } 25749b25e05SJeff Mahoney /* 2588c342930SJeff Mahoney * __btrfs_panic decodes unexpected, fatal errors from the caller, 2598c342930SJeff Mahoney * issues an alert, and either panics or BUGs, depending on mount options. 2608c342930SJeff Mahoney */ 261c0d19e2bSDavid Sterba __cold 2628c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, 2638c342930SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 2648c342930SJeff Mahoney { 2658c342930SJeff Mahoney char *s_id = "<unknown>"; 2668c342930SJeff Mahoney const char *errstr; 2678c342930SJeff Mahoney struct va_format vaf = { .fmt = fmt }; 2688c342930SJeff Mahoney va_list args; 2698c342930SJeff Mahoney 2708c342930SJeff Mahoney if (fs_info) 2718c342930SJeff Mahoney s_id = fs_info->sb->s_id; 2728c342930SJeff Mahoney 2738c342930SJeff Mahoney va_start(args, fmt); 2748c342930SJeff Mahoney vaf.va = &args; 2758c342930SJeff Mahoney 27608748810SDavid Sterba errstr = btrfs_decode_error(errno); 277aa43a17cSEric Sandeen if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR)) 27808748810SDavid Sterba panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n", 27908748810SDavid Sterba s_id, function, line, &vaf, errno, errstr); 2808c342930SJeff Mahoney 281efe120a0SFrank Holton btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)", 282efe120a0SFrank Holton function, line, &vaf, errno, errstr); 2838c342930SJeff Mahoney va_end(args); 2848c342930SJeff Mahoney /* Caller calls BUG() */ 2858c342930SJeff Mahoney } 286acce952bSliubo 287e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 288e20d96d6SChris Mason { 2893abdbd78SDavid Sterba close_ctree(btrfs_sb(sb)->tree_root); 290e20d96d6SChris Mason } 2912e635a27SChris Mason 29295e05289SChris Mason enum { 29373f73415SJosef Bacik Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum, 294287a0ab9SJosef Bacik Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, 295287a0ab9SJosef Bacik Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress, 296261507a0SLi Zefan Opt_compress_type, Opt_compress_force, Opt_compress_force_type, 297261507a0SLi Zefan Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard, 29891435650SChris Mason Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed, 2999555c6c1SIlya Dryomov Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache, 3009555c6c1SIlya Dryomov Opt_no_space_cache, Opt_recovery, Opt_skip_balance, 30121adbd5cSStefan Behrens Opt_check_integrity, Opt_check_integrity_including_extent_data, 302f420ee1eSStefan Behrens Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree, 303e07a2adeSQu Wenruo Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard, 304a258af7aSQu Wenruo Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow, 3053818aea2SQu Wenruo Opt_datasum, Opt_treelog, Opt_noinode_cache, 306d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 307d0bd4560SJosef Bacik Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all, 308d0bd4560SJosef Bacik #endif 3099555c6c1SIlya Dryomov Opt_err, 31095e05289SChris Mason }; 31195e05289SChris Mason 31295e05289SChris Mason static match_table_t tokens = { 313dfe25020SChris Mason {Opt_degraded, "degraded"}, 31495e05289SChris Mason {Opt_subvol, "subvol=%s"}, 3151493381fSWang Shilong {Opt_subvolid, "subvolid=%s"}, 31643e570b0SChristoph Hellwig {Opt_device, "device=%s"}, 317b6cda9bcSChris Mason {Opt_nodatasum, "nodatasum"}, 318d399167dSQu Wenruo {Opt_datasum, "datasum"}, 319be20aa9dSChris Mason {Opt_nodatacow, "nodatacow"}, 320a258af7aSQu Wenruo {Opt_datacow, "datacow"}, 32121ad10cfSChris Mason {Opt_nobarrier, "nobarrier"}, 322842bef58SQu Wenruo {Opt_barrier, "barrier"}, 3236f568d35SChris Mason {Opt_max_inline, "max_inline=%s"}, 3248f662a76SChris Mason {Opt_alloc_start, "alloc_start=%s"}, 3254543df7eSChris Mason {Opt_thread_pool, "thread_pool=%d"}, 326c8b97818SChris Mason {Opt_compress, "compress"}, 327261507a0SLi Zefan {Opt_compress_type, "compress=%s"}, 328a555f810SChris Mason {Opt_compress_force, "compress-force"}, 329261507a0SLi Zefan {Opt_compress_force_type, "compress-force=%s"}, 330e18e4809SChris Mason {Opt_ssd, "ssd"}, 331451d7585SChris Mason {Opt_ssd_spread, "ssd_spread"}, 3323b30c22fSChris Mason {Opt_nossd, "nossd"}, 333bd0330adSQu Wenruo {Opt_acl, "acl"}, 33433268eafSJosef Bacik {Opt_noacl, "noacl"}, 3353a5e1404SSage Weil {Opt_notreelog, "notreelog"}, 336a88998f2SQu Wenruo {Opt_treelog, "treelog"}, 337dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 3382c9ee856SQu Wenruo {Opt_noflushoncommit, "noflushoncommit"}, 33997e728d4SJosef Bacik {Opt_ratio, "metadata_ratio=%d"}, 340e244a0aeSChristoph Hellwig {Opt_discard, "discard"}, 341e07a2adeSQu Wenruo {Opt_nodiscard, "nodiscard"}, 3420af3d00bSJosef Bacik {Opt_space_cache, "space_cache"}, 34388c2ba3bSJosef Bacik {Opt_clear_cache, "clear_cache"}, 3444260f7c7SSage Weil {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, 34591435650SChris Mason {Opt_enospc_debug, "enospc_debug"}, 34653036293SQu Wenruo {Opt_noenospc_debug, "noenospc_debug"}, 347e15d0542SXin Zhong {Opt_subvolrootid, "subvolrootid=%d"}, 3484cb5300bSChris Mason {Opt_defrag, "autodefrag"}, 349fc0ca9afSQu Wenruo {Opt_nodefrag, "noautodefrag"}, 3504b9465cbSChris Mason {Opt_inode_cache, "inode_cache"}, 3513818aea2SQu Wenruo {Opt_noinode_cache, "noinode_cache"}, 3528965593eSDavid Sterba {Opt_no_space_cache, "nospace_cache"}, 353af31f5e5SChris Mason {Opt_recovery, "recovery"}, 3549555c6c1SIlya Dryomov {Opt_skip_balance, "skip_balance"}, 35521adbd5cSStefan Behrens {Opt_check_integrity, "check_int"}, 35621adbd5cSStefan Behrens {Opt_check_integrity_including_extent_data, "check_int_data"}, 35721adbd5cSStefan Behrens {Opt_check_integrity_print_mask, "check_int_print_mask=%d"}, 358f420ee1eSStefan Behrens {Opt_rescan_uuid_tree, "rescan_uuid_tree"}, 3598c342930SJeff Mahoney {Opt_fatal_errors, "fatal_errors=%s"}, 3608b87dc17SDavid Sterba {Opt_commit_interval, "commit=%d"}, 361d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 362d0bd4560SJosef Bacik {Opt_fragment_data, "fragment=data"}, 363d0bd4560SJosef Bacik {Opt_fragment_metadata, "fragment=metadata"}, 364d0bd4560SJosef Bacik {Opt_fragment_all, "fragment=all"}, 365d0bd4560SJosef Bacik #endif 36633268eafSJosef Bacik {Opt_err, NULL}, 36795e05289SChris Mason }; 36895e05289SChris Mason 369edf24abeSChristoph Hellwig /* 370edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 371edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 37249b25e05SJeff Mahoney * XXX JDM: This needs to be cleaned up for remount. 373edf24abeSChristoph Hellwig */ 374edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options) 37595e05289SChris Mason { 376edf24abeSChristoph Hellwig struct btrfs_fs_info *info = root->fs_info; 37795e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 37873bc1876SJosef Bacik char *p, *num, *orig = NULL; 37973bc1876SJosef Bacik u64 cache_gen; 3804543df7eSChris Mason int intarg; 381a7a3f7caSSage Weil int ret = 0; 382261507a0SLi Zefan char *compress_type; 383261507a0SLi Zefan bool compress_force = false; 384b6cda9bcSChris Mason 3856c41761fSDavid Sterba cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy); 38673bc1876SJosef Bacik if (cache_gen) 38773bc1876SJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 38873bc1876SJosef Bacik 38995e05289SChris Mason if (!options) 39073bc1876SJosef Bacik goto out; 39195e05289SChris Mason 392be20aa9dSChris Mason /* 393be20aa9dSChris Mason * strsep changes the string, duplicate it because parse_options 394be20aa9dSChris Mason * gets called twice 395be20aa9dSChris Mason */ 396be20aa9dSChris Mason options = kstrdup(options, GFP_NOFS); 397be20aa9dSChris Mason if (!options) 398be20aa9dSChris Mason return -ENOMEM; 399be20aa9dSChris Mason 400da495eccSJosef Bacik orig = options; 401be20aa9dSChris Mason 40295e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 40395e05289SChris Mason int token; 40495e05289SChris Mason if (!*p) 40595e05289SChris Mason continue; 40695e05289SChris Mason 40795e05289SChris Mason token = match_token(p, tokens, args); 40895e05289SChris Mason switch (token) { 409dfe25020SChris Mason case Opt_degraded: 410efe120a0SFrank Holton btrfs_info(root->fs_info, "allowing degraded mounts"); 411dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 412dfe25020SChris Mason break; 41395e05289SChris Mason case Opt_subvol: 41473f73415SJosef Bacik case Opt_subvolid: 415e15d0542SXin Zhong case Opt_subvolrootid: 41643e570b0SChristoph Hellwig case Opt_device: 417edf24abeSChristoph Hellwig /* 41843e570b0SChristoph Hellwig * These are parsed by btrfs_parse_early_options 419edf24abeSChristoph Hellwig * and can be happily ignored here. 420edf24abeSChristoph Hellwig */ 42195e05289SChris Mason break; 422b6cda9bcSChris Mason case Opt_nodatasum: 42307802534SQu Wenruo btrfs_set_and_info(root, NODATASUM, 42407802534SQu Wenruo "setting nodatasum"); 425be20aa9dSChris Mason break; 426d399167dSQu Wenruo case Opt_datasum: 42707802534SQu Wenruo if (btrfs_test_opt(root, NODATASUM)) { 428d399167dSQu Wenruo if (btrfs_test_opt(root, NODATACOW)) 429d399167dSQu Wenruo btrfs_info(root->fs_info, "setting datasum, datacow enabled"); 430d399167dSQu Wenruo else 431d399167dSQu Wenruo btrfs_info(root->fs_info, "setting datasum"); 43207802534SQu Wenruo } 433d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATACOW); 434d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATASUM); 435d399167dSQu Wenruo break; 436be20aa9dSChris Mason case Opt_nodatacow: 43707802534SQu Wenruo if (!btrfs_test_opt(root, NODATACOW)) { 438bedb2ccaSAndrei Popa if (!btrfs_test_opt(root, COMPRESS) || 439bedb2ccaSAndrei Popa !btrfs_test_opt(root, FORCE_COMPRESS)) { 440efe120a0SFrank Holton btrfs_info(root->fs_info, 441efe120a0SFrank Holton "setting nodatacow, compression disabled"); 442bedb2ccaSAndrei Popa } else { 443efe120a0SFrank Holton btrfs_info(root->fs_info, "setting nodatacow"); 444bedb2ccaSAndrei Popa } 44507802534SQu Wenruo } 446bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, COMPRESS); 447bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 448be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 449be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 450b6cda9bcSChris Mason break; 451a258af7aSQu Wenruo case Opt_datacow: 45207802534SQu Wenruo btrfs_clear_and_info(root, NODATACOW, 45307802534SQu Wenruo "setting datacow"); 454a258af7aSQu Wenruo break; 455a555f810SChris Mason case Opt_compress_force: 456261507a0SLi Zefan case Opt_compress_force_type: 457261507a0SLi Zefan compress_force = true; 4581c697d4aSEric Sandeen /* Fallthrough */ 459261507a0SLi Zefan case Opt_compress: 460261507a0SLi Zefan case Opt_compress_type: 461261507a0SLi Zefan if (token == Opt_compress || 462261507a0SLi Zefan token == Opt_compress_force || 463261507a0SLi Zefan strcmp(args[0].from, "zlib") == 0) { 464261507a0SLi Zefan compress_type = "zlib"; 465261507a0SLi Zefan info->compress_type = BTRFS_COMPRESS_ZLIB; 466063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 467bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 468bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 469a6fa6faeSLi Zefan } else if (strcmp(args[0].from, "lzo") == 0) { 470a6fa6faeSLi Zefan compress_type = "lzo"; 471a6fa6faeSLi Zefan info->compress_type = BTRFS_COMPRESS_LZO; 472063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 473bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 474bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 4752b0ce2c2SMitch Harder btrfs_set_fs_incompat(info, COMPRESS_LZO); 476063849eaSArnd Hannemann } else if (strncmp(args[0].from, "no", 2) == 0) { 477063849eaSArnd Hannemann compress_type = "no"; 478063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, COMPRESS); 479063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 480063849eaSArnd Hannemann compress_force = false; 481261507a0SLi Zefan } else { 482261507a0SLi Zefan ret = -EINVAL; 483261507a0SLi Zefan goto out; 484261507a0SLi Zefan } 485261507a0SLi Zefan 486261507a0SLi Zefan if (compress_force) { 48707802534SQu Wenruo btrfs_set_and_info(root, FORCE_COMPRESS, 48807802534SQu Wenruo "force %s compression", 489261507a0SLi Zefan compress_type); 490143f3636SDavid Sterba } else { 49107802534SQu Wenruo if (!btrfs_test_opt(root, COMPRESS)) 49207802534SQu Wenruo btrfs_info(root->fs_info, 493351fd353SDavid Sterba "btrfs: use %s compression", 494261507a0SLi Zefan compress_type); 4954027e0f4SWang Shilong /* 4964027e0f4SWang Shilong * If we remount from compress-force=xxx to 4974027e0f4SWang Shilong * compress=xxx, we need clear FORCE_COMPRESS 4984027e0f4SWang Shilong * flag, otherwise, there is no way for users 4994027e0f4SWang Shilong * to disable forcible compression separately. 5004027e0f4SWang Shilong */ 5014027e0f4SWang Shilong btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 502a7e252afSMiao Xie } 503a555f810SChris Mason break; 504e18e4809SChris Mason case Opt_ssd: 50507802534SQu Wenruo btrfs_set_and_info(root, SSD, 50607802534SQu Wenruo "use ssd allocation scheme"); 507e18e4809SChris Mason break; 508451d7585SChris Mason case Opt_ssd_spread: 50907802534SQu Wenruo btrfs_set_and_info(root, SSD_SPREAD, 51007802534SQu Wenruo "use spread ssd allocation scheme"); 5112aa06a35SEric Sandeen btrfs_set_opt(info->mount_opt, SSD); 512451d7585SChris Mason break; 5133b30c22fSChris Mason case Opt_nossd: 5142aa06a35SEric Sandeen btrfs_set_and_info(root, NOSSD, 51507802534SQu Wenruo "not using ssd allocation scheme"); 5163b30c22fSChris Mason btrfs_clear_opt(info->mount_opt, SSD); 5173b30c22fSChris Mason break; 518842bef58SQu Wenruo case Opt_barrier: 51907802534SQu Wenruo btrfs_clear_and_info(root, NOBARRIER, 52007802534SQu Wenruo "turning on barriers"); 521842bef58SQu Wenruo break; 52221ad10cfSChris Mason case Opt_nobarrier: 52307802534SQu Wenruo btrfs_set_and_info(root, NOBARRIER, 52407802534SQu Wenruo "turning off barriers"); 52521ad10cfSChris Mason break; 5264543df7eSChris Mason case Opt_thread_pool: 5272c334e87SWang Shilong ret = match_int(&args[0], &intarg); 5282c334e87SWang Shilong if (ret) { 5292c334e87SWang Shilong goto out; 5302c334e87SWang Shilong } else if (intarg > 0) { 5314543df7eSChris Mason info->thread_pool_size = intarg; 5322c334e87SWang Shilong } else { 5332c334e87SWang Shilong ret = -EINVAL; 5342c334e87SWang Shilong goto out; 5352c334e87SWang Shilong } 5364543df7eSChris Mason break; 5376f568d35SChris Mason case Opt_max_inline: 538edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5396f568d35SChris Mason if (num) { 54091748467SAkinobu Mita info->max_inline = memparse(num, NULL); 5416f568d35SChris Mason kfree(num); 5426f568d35SChris Mason 54315ada040SChris Mason if (info->max_inline) { 544feb5f965SMitch Harder info->max_inline = min_t(u64, 54515ada040SChris Mason info->max_inline, 54615ada040SChris Mason root->sectorsize); 54715ada040SChris Mason } 548efe120a0SFrank Holton btrfs_info(root->fs_info, "max_inline at %llu", 549c1c9ff7cSGeert Uytterhoeven info->max_inline); 5502c334e87SWang Shilong } else { 5512c334e87SWang Shilong ret = -ENOMEM; 5522c334e87SWang Shilong goto out; 5536f568d35SChris Mason } 5546f568d35SChris Mason break; 5558f662a76SChris Mason case Opt_alloc_start: 556edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5578f662a76SChris Mason if (num) { 558c018daecSMiao Xie mutex_lock(&info->chunk_mutex); 55991748467SAkinobu Mita info->alloc_start = memparse(num, NULL); 560c018daecSMiao Xie mutex_unlock(&info->chunk_mutex); 5618f662a76SChris Mason kfree(num); 562efe120a0SFrank Holton btrfs_info(root->fs_info, "allocations start at %llu", 563c1c9ff7cSGeert Uytterhoeven info->alloc_start); 5642c334e87SWang Shilong } else { 5652c334e87SWang Shilong ret = -ENOMEM; 5662c334e87SWang Shilong goto out; 5678f662a76SChris Mason } 5688f662a76SChris Mason break; 569bd0330adSQu Wenruo case Opt_acl: 57045ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL 571bd0330adSQu Wenruo root->fs_info->sb->s_flags |= MS_POSIXACL; 572bd0330adSQu Wenruo break; 57345ff35d6SGuangliang Zhao #else 57445ff35d6SGuangliang Zhao btrfs_err(root->fs_info, 57545ff35d6SGuangliang Zhao "support for ACL not compiled in!"); 57645ff35d6SGuangliang Zhao ret = -EINVAL; 57745ff35d6SGuangliang Zhao goto out; 57845ff35d6SGuangliang Zhao #endif 57933268eafSJosef Bacik case Opt_noacl: 58033268eafSJosef Bacik root->fs_info->sb->s_flags &= ~MS_POSIXACL; 58133268eafSJosef Bacik break; 5823a5e1404SSage Weil case Opt_notreelog: 58307802534SQu Wenruo btrfs_set_and_info(root, NOTREELOG, 58407802534SQu Wenruo "disabling tree log"); 5853a5e1404SSage Weil break; 586a88998f2SQu Wenruo case Opt_treelog: 58707802534SQu Wenruo btrfs_clear_and_info(root, NOTREELOG, 58807802534SQu Wenruo "enabling tree log"); 589a88998f2SQu Wenruo break; 590dccae999SSage Weil case Opt_flushoncommit: 59107802534SQu Wenruo btrfs_set_and_info(root, FLUSHONCOMMIT, 59207802534SQu Wenruo "turning on flush-on-commit"); 593dccae999SSage Weil break; 5942c9ee856SQu Wenruo case Opt_noflushoncommit: 59507802534SQu Wenruo btrfs_clear_and_info(root, FLUSHONCOMMIT, 59607802534SQu Wenruo "turning off flush-on-commit"); 5972c9ee856SQu Wenruo break; 59897e728d4SJosef Bacik case Opt_ratio: 5992c334e87SWang Shilong ret = match_int(&args[0], &intarg); 6002c334e87SWang Shilong if (ret) { 6012c334e87SWang Shilong goto out; 6022c334e87SWang Shilong } else if (intarg >= 0) { 60397e728d4SJosef Bacik info->metadata_ratio = intarg; 604efe120a0SFrank Holton btrfs_info(root->fs_info, "metadata ratio %d", 60597e728d4SJosef Bacik info->metadata_ratio); 6062c334e87SWang Shilong } else { 6072c334e87SWang Shilong ret = -EINVAL; 6082c334e87SWang Shilong goto out; 60997e728d4SJosef Bacik } 61097e728d4SJosef Bacik break; 611e244a0aeSChristoph Hellwig case Opt_discard: 61207802534SQu Wenruo btrfs_set_and_info(root, DISCARD, 61307802534SQu Wenruo "turning on discard"); 614e244a0aeSChristoph Hellwig break; 615e07a2adeSQu Wenruo case Opt_nodiscard: 61607802534SQu Wenruo btrfs_clear_and_info(root, DISCARD, 61707802534SQu Wenruo "turning off discard"); 618e07a2adeSQu Wenruo break; 6190af3d00bSJosef Bacik case Opt_space_cache: 62007802534SQu Wenruo btrfs_set_and_info(root, SPACE_CACHE, 62107802534SQu Wenruo "enabling disk space caching"); 6220de90876SJosef Bacik break; 623f420ee1eSStefan Behrens case Opt_rescan_uuid_tree: 624f420ee1eSStefan Behrens btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE); 625f420ee1eSStefan Behrens break; 62673bc1876SJosef Bacik case Opt_no_space_cache: 62707802534SQu Wenruo btrfs_clear_and_info(root, SPACE_CACHE, 62807802534SQu Wenruo "disabling disk space caching"); 62973bc1876SJosef Bacik break; 6304b9465cbSChris Mason case Opt_inode_cache: 6317e1876acSDavid Sterba btrfs_set_pending_and_info(info, INODE_MAP_CACHE, 63207802534SQu Wenruo "enabling inode map caching"); 6333818aea2SQu Wenruo break; 6343818aea2SQu Wenruo case Opt_noinode_cache: 6357e1876acSDavid Sterba btrfs_clear_pending_and_info(info, INODE_MAP_CACHE, 63607802534SQu Wenruo "disabling inode map caching"); 6374b9465cbSChris Mason break; 63888c2ba3bSJosef Bacik case Opt_clear_cache: 63907802534SQu Wenruo btrfs_set_and_info(root, CLEAR_CACHE, 64007802534SQu Wenruo "force clearing of disk cache"); 6410af3d00bSJosef Bacik break; 6424260f7c7SSage Weil case Opt_user_subvol_rm_allowed: 6434260f7c7SSage Weil btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 6444260f7c7SSage Weil break; 64591435650SChris Mason case Opt_enospc_debug: 64691435650SChris Mason btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 64791435650SChris Mason break; 64853036293SQu Wenruo case Opt_noenospc_debug: 64953036293SQu Wenruo btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG); 65053036293SQu Wenruo break; 6514cb5300bSChris Mason case Opt_defrag: 65207802534SQu Wenruo btrfs_set_and_info(root, AUTO_DEFRAG, 65307802534SQu Wenruo "enabling auto defrag"); 6544cb5300bSChris Mason break; 655fc0ca9afSQu Wenruo case Opt_nodefrag: 65607802534SQu Wenruo btrfs_clear_and_info(root, AUTO_DEFRAG, 65707802534SQu Wenruo "disabling auto defrag"); 658fc0ca9afSQu Wenruo break; 659af31f5e5SChris Mason case Opt_recovery: 660efe120a0SFrank Holton btrfs_info(root->fs_info, "enabling auto recovery"); 661af31f5e5SChris Mason btrfs_set_opt(info->mount_opt, RECOVERY); 662af31f5e5SChris Mason break; 6639555c6c1SIlya Dryomov case Opt_skip_balance: 6649555c6c1SIlya Dryomov btrfs_set_opt(info->mount_opt, SKIP_BALANCE); 6659555c6c1SIlya Dryomov break; 66621adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 66721adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 668efe120a0SFrank Holton btrfs_info(root->fs_info, 669efe120a0SFrank Holton "enabling check integrity including extent data"); 67021adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, 67121adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA); 67221adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 67321adbd5cSStefan Behrens break; 67421adbd5cSStefan Behrens case Opt_check_integrity: 675efe120a0SFrank Holton btrfs_info(root->fs_info, "enabling check integrity"); 67621adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 67721adbd5cSStefan Behrens break; 67821adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 6792c334e87SWang Shilong ret = match_int(&args[0], &intarg); 6802c334e87SWang Shilong if (ret) { 6812c334e87SWang Shilong goto out; 6822c334e87SWang Shilong } else if (intarg >= 0) { 68321adbd5cSStefan Behrens info->check_integrity_print_mask = intarg; 684efe120a0SFrank Holton btrfs_info(root->fs_info, "check_integrity_print_mask 0x%x", 68521adbd5cSStefan Behrens info->check_integrity_print_mask); 6862c334e87SWang Shilong } else { 6872c334e87SWang Shilong ret = -EINVAL; 6882c334e87SWang Shilong goto out; 68921adbd5cSStefan Behrens } 69021adbd5cSStefan Behrens break; 69121adbd5cSStefan Behrens #else 69221adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 69321adbd5cSStefan Behrens case Opt_check_integrity: 69421adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 695efe120a0SFrank Holton btrfs_err(root->fs_info, 696efe120a0SFrank Holton "support for check_integrity* not compiled in!"); 69721adbd5cSStefan Behrens ret = -EINVAL; 69821adbd5cSStefan Behrens goto out; 69921adbd5cSStefan Behrens #endif 7008c342930SJeff Mahoney case Opt_fatal_errors: 7018c342930SJeff Mahoney if (strcmp(args[0].from, "panic") == 0) 7028c342930SJeff Mahoney btrfs_set_opt(info->mount_opt, 7038c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7048c342930SJeff Mahoney else if (strcmp(args[0].from, "bug") == 0) 7058c342930SJeff Mahoney btrfs_clear_opt(info->mount_opt, 7068c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7078c342930SJeff Mahoney else { 7088c342930SJeff Mahoney ret = -EINVAL; 7098c342930SJeff Mahoney goto out; 7108c342930SJeff Mahoney } 7118c342930SJeff Mahoney break; 7128b87dc17SDavid Sterba case Opt_commit_interval: 7138b87dc17SDavid Sterba intarg = 0; 7148b87dc17SDavid Sterba ret = match_int(&args[0], &intarg); 7158b87dc17SDavid Sterba if (ret < 0) { 716efe120a0SFrank Holton btrfs_err(root->fs_info, "invalid commit interval"); 7178b87dc17SDavid Sterba ret = -EINVAL; 7188b87dc17SDavid Sterba goto out; 7198b87dc17SDavid Sterba } 7208b87dc17SDavid Sterba if (intarg > 0) { 7218b87dc17SDavid Sterba if (intarg > 300) { 722efe120a0SFrank Holton btrfs_warn(root->fs_info, "excessive commit interval %d", 7238b87dc17SDavid Sterba intarg); 7248b87dc17SDavid Sterba } 7258b87dc17SDavid Sterba info->commit_interval = intarg; 7268b87dc17SDavid Sterba } else { 727efe120a0SFrank Holton btrfs_info(root->fs_info, "using default commit interval %ds", 7288b87dc17SDavid Sterba BTRFS_DEFAULT_COMMIT_INTERVAL); 7298b87dc17SDavid Sterba info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 7308b87dc17SDavid Sterba } 7318b87dc17SDavid Sterba break; 732d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 733d0bd4560SJosef Bacik case Opt_fragment_all: 734d0bd4560SJosef Bacik btrfs_info(root->fs_info, "fragmenting all space"); 735d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 736d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA); 737d0bd4560SJosef Bacik break; 738d0bd4560SJosef Bacik case Opt_fragment_metadata: 739d0bd4560SJosef Bacik btrfs_info(root->fs_info, "fragmenting metadata"); 740d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, 741d0bd4560SJosef Bacik FRAGMENT_METADATA); 742d0bd4560SJosef Bacik break; 743d0bd4560SJosef Bacik case Opt_fragment_data: 744d0bd4560SJosef Bacik btrfs_info(root->fs_info, "fragmenting data"); 745d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 746d0bd4560SJosef Bacik break; 747d0bd4560SJosef Bacik #endif 748a7a3f7caSSage Weil case Opt_err: 749efe120a0SFrank Holton btrfs_info(root->fs_info, "unrecognized mount option '%s'", p); 750a7a3f7caSSage Weil ret = -EINVAL; 751a7a3f7caSSage Weil goto out; 75295e05289SChris Mason default: 753be20aa9dSChris Mason break; 75495e05289SChris Mason } 75595e05289SChris Mason } 756a7a3f7caSSage Weil out: 75773bc1876SJosef Bacik if (!ret && btrfs_test_opt(root, SPACE_CACHE)) 758efe120a0SFrank Holton btrfs_info(root->fs_info, "disk space caching is enabled"); 759da495eccSJosef Bacik kfree(orig); 760a7a3f7caSSage Weil return ret; 761edf24abeSChristoph Hellwig } 762edf24abeSChristoph Hellwig 763edf24abeSChristoph Hellwig /* 764edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 765edf24abeSChristoph Hellwig * 766edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 767edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 768edf24abeSChristoph Hellwig */ 76997288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 77073f73415SJosef Bacik void *holder, char **subvol_name, u64 *subvol_objectid, 7715e2a4b25SDavid Sterba struct btrfs_fs_devices **fs_devices) 772edf24abeSChristoph Hellwig { 773edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 77483c8c9bdSJeff Liu char *device_name, *opts, *orig, *p; 7751493381fSWang Shilong char *num = NULL; 776edf24abeSChristoph Hellwig int error = 0; 777edf24abeSChristoph Hellwig 778edf24abeSChristoph Hellwig if (!options) 779830c4adbSJosef Bacik return 0; 780edf24abeSChristoph Hellwig 781edf24abeSChristoph Hellwig /* 782edf24abeSChristoph Hellwig * strsep changes the string, duplicate it because parse_options 783edf24abeSChristoph Hellwig * gets called twice 784edf24abeSChristoph Hellwig */ 785edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 786edf24abeSChristoph Hellwig if (!opts) 787edf24abeSChristoph Hellwig return -ENOMEM; 7883f3d0bc0STero Roponen orig = opts; 789edf24abeSChristoph Hellwig 790edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 791edf24abeSChristoph Hellwig int token; 792edf24abeSChristoph Hellwig if (!*p) 793edf24abeSChristoph Hellwig continue; 794edf24abeSChristoph Hellwig 795edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 796edf24abeSChristoph Hellwig switch (token) { 797edf24abeSChristoph Hellwig case Opt_subvol: 798a90e8b6fSIlya Dryomov kfree(*subvol_name); 799edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 8002c334e87SWang Shilong if (!*subvol_name) { 8012c334e87SWang Shilong error = -ENOMEM; 8022c334e87SWang Shilong goto out; 8032c334e87SWang Shilong } 804edf24abeSChristoph Hellwig break; 80573f73415SJosef Bacik case Opt_subvolid: 8061493381fSWang Shilong num = match_strdup(&args[0]); 8071493381fSWang Shilong if (num) { 8081493381fSWang Shilong *subvol_objectid = memparse(num, NULL); 8091493381fSWang Shilong kfree(num); 8104849f01dSJosef Bacik /* we want the original fs_tree */ 8111493381fSWang Shilong if (!*subvol_objectid) 8124849f01dSJosef Bacik *subvol_objectid = 8134849f01dSJosef Bacik BTRFS_FS_TREE_OBJECTID; 8142c334e87SWang Shilong } else { 8152c334e87SWang Shilong error = -EINVAL; 8162c334e87SWang Shilong goto out; 8174849f01dSJosef Bacik } 81873f73415SJosef Bacik break; 819e15d0542SXin Zhong case Opt_subvolrootid: 8205e2a4b25SDavid Sterba printk(KERN_WARNING 821efe120a0SFrank Holton "BTRFS: 'subvolrootid' mount option is deprecated and has " 822efe120a0SFrank Holton "no effect\n"); 823e15d0542SXin Zhong break; 82443e570b0SChristoph Hellwig case Opt_device: 82583c8c9bdSJeff Liu device_name = match_strdup(&args[0]); 82683c8c9bdSJeff Liu if (!device_name) { 82783c8c9bdSJeff Liu error = -ENOMEM; 82883c8c9bdSJeff Liu goto out; 82983c8c9bdSJeff Liu } 83083c8c9bdSJeff Liu error = btrfs_scan_one_device(device_name, 83143e570b0SChristoph Hellwig flags, holder, fs_devices); 83283c8c9bdSJeff Liu kfree(device_name); 83343e570b0SChristoph Hellwig if (error) 834830c4adbSJosef Bacik goto out; 83543e570b0SChristoph Hellwig break; 836edf24abeSChristoph Hellwig default: 837edf24abeSChristoph Hellwig break; 838edf24abeSChristoph Hellwig } 839edf24abeSChristoph Hellwig } 840edf24abeSChristoph Hellwig 841edf24abeSChristoph Hellwig out: 842830c4adbSJosef Bacik kfree(orig); 843edf24abeSChristoph Hellwig return error; 84495e05289SChris Mason } 84595e05289SChris Mason 84605dbe683SOmar Sandoval static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info, 84773f73415SJosef Bacik u64 subvol_objectid) 84873f73415SJosef Bacik { 849815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 85005dbe683SOmar Sandoval struct btrfs_root *fs_root; 85105dbe683SOmar Sandoval struct btrfs_root_ref *root_ref; 85205dbe683SOmar Sandoval struct btrfs_inode_ref *inode_ref; 85305dbe683SOmar Sandoval struct btrfs_key key; 85405dbe683SOmar Sandoval struct btrfs_path *path = NULL; 85505dbe683SOmar Sandoval char *name = NULL, *ptr; 85605dbe683SOmar Sandoval u64 dirid; 85705dbe683SOmar Sandoval int len; 85805dbe683SOmar Sandoval int ret; 85905dbe683SOmar Sandoval 86005dbe683SOmar Sandoval path = btrfs_alloc_path(); 86105dbe683SOmar Sandoval if (!path) { 86205dbe683SOmar Sandoval ret = -ENOMEM; 86305dbe683SOmar Sandoval goto err; 86405dbe683SOmar Sandoval } 86505dbe683SOmar Sandoval path->leave_spinning = 1; 86605dbe683SOmar Sandoval 86705dbe683SOmar Sandoval name = kmalloc(PATH_MAX, GFP_NOFS); 86805dbe683SOmar Sandoval if (!name) { 86905dbe683SOmar Sandoval ret = -ENOMEM; 87005dbe683SOmar Sandoval goto err; 87105dbe683SOmar Sandoval } 87205dbe683SOmar Sandoval ptr = name + PATH_MAX - 1; 87305dbe683SOmar Sandoval ptr[0] = '\0'; 87405dbe683SOmar Sandoval 87505dbe683SOmar Sandoval /* 87605dbe683SOmar Sandoval * Walk up the subvolume trees in the tree of tree roots by root 87705dbe683SOmar Sandoval * backrefs until we hit the top-level subvolume. 87805dbe683SOmar Sandoval */ 87905dbe683SOmar Sandoval while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) { 88005dbe683SOmar Sandoval key.objectid = subvol_objectid; 88105dbe683SOmar Sandoval key.type = BTRFS_ROOT_BACKREF_KEY; 88205dbe683SOmar Sandoval key.offset = (u64)-1; 88305dbe683SOmar Sandoval 88405dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 88505dbe683SOmar Sandoval if (ret < 0) { 88605dbe683SOmar Sandoval goto err; 88705dbe683SOmar Sandoval } else if (ret > 0) { 88805dbe683SOmar Sandoval ret = btrfs_previous_item(root, path, subvol_objectid, 88905dbe683SOmar Sandoval BTRFS_ROOT_BACKREF_KEY); 89005dbe683SOmar Sandoval if (ret < 0) { 89105dbe683SOmar Sandoval goto err; 89205dbe683SOmar Sandoval } else if (ret > 0) { 89305dbe683SOmar Sandoval ret = -ENOENT; 89405dbe683SOmar Sandoval goto err; 89505dbe683SOmar Sandoval } 89605dbe683SOmar Sandoval } 89705dbe683SOmar Sandoval 89805dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 89905dbe683SOmar Sandoval subvol_objectid = key.offset; 90005dbe683SOmar Sandoval 90105dbe683SOmar Sandoval root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], 90205dbe683SOmar Sandoval struct btrfs_root_ref); 90305dbe683SOmar Sandoval len = btrfs_root_ref_name_len(path->nodes[0], root_ref); 90405dbe683SOmar Sandoval ptr -= len + 1; 90505dbe683SOmar Sandoval if (ptr < name) { 90605dbe683SOmar Sandoval ret = -ENAMETOOLONG; 90705dbe683SOmar Sandoval goto err; 90805dbe683SOmar Sandoval } 90905dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 91005dbe683SOmar Sandoval (unsigned long)(root_ref + 1), len); 91105dbe683SOmar Sandoval ptr[0] = '/'; 91205dbe683SOmar Sandoval dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref); 91305dbe683SOmar Sandoval btrfs_release_path(path); 91405dbe683SOmar Sandoval 91505dbe683SOmar Sandoval key.objectid = subvol_objectid; 91605dbe683SOmar Sandoval key.type = BTRFS_ROOT_ITEM_KEY; 91705dbe683SOmar Sandoval key.offset = (u64)-1; 91805dbe683SOmar Sandoval fs_root = btrfs_read_fs_root_no_name(fs_info, &key); 91905dbe683SOmar Sandoval if (IS_ERR(fs_root)) { 92005dbe683SOmar Sandoval ret = PTR_ERR(fs_root); 92105dbe683SOmar Sandoval goto err; 92205dbe683SOmar Sandoval } 92305dbe683SOmar Sandoval 92405dbe683SOmar Sandoval /* 92505dbe683SOmar Sandoval * Walk up the filesystem tree by inode refs until we hit the 92605dbe683SOmar Sandoval * root directory. 92705dbe683SOmar Sandoval */ 92805dbe683SOmar Sandoval while (dirid != BTRFS_FIRST_FREE_OBJECTID) { 92905dbe683SOmar Sandoval key.objectid = dirid; 93005dbe683SOmar Sandoval key.type = BTRFS_INODE_REF_KEY; 93105dbe683SOmar Sandoval key.offset = (u64)-1; 93205dbe683SOmar Sandoval 93305dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); 93405dbe683SOmar Sandoval if (ret < 0) { 93505dbe683SOmar Sandoval goto err; 93605dbe683SOmar Sandoval } else if (ret > 0) { 93705dbe683SOmar Sandoval ret = btrfs_previous_item(fs_root, path, dirid, 93805dbe683SOmar Sandoval BTRFS_INODE_REF_KEY); 93905dbe683SOmar Sandoval if (ret < 0) { 94005dbe683SOmar Sandoval goto err; 94105dbe683SOmar Sandoval } else if (ret > 0) { 94205dbe683SOmar Sandoval ret = -ENOENT; 94305dbe683SOmar Sandoval goto err; 94405dbe683SOmar Sandoval } 94505dbe683SOmar Sandoval } 94605dbe683SOmar Sandoval 94705dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 94805dbe683SOmar Sandoval dirid = key.offset; 94905dbe683SOmar Sandoval 95005dbe683SOmar Sandoval inode_ref = btrfs_item_ptr(path->nodes[0], 95105dbe683SOmar Sandoval path->slots[0], 95205dbe683SOmar Sandoval struct btrfs_inode_ref); 95305dbe683SOmar Sandoval len = btrfs_inode_ref_name_len(path->nodes[0], 95405dbe683SOmar Sandoval inode_ref); 95505dbe683SOmar Sandoval ptr -= len + 1; 95605dbe683SOmar Sandoval if (ptr < name) { 95705dbe683SOmar Sandoval ret = -ENAMETOOLONG; 95805dbe683SOmar Sandoval goto err; 95905dbe683SOmar Sandoval } 96005dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 96105dbe683SOmar Sandoval (unsigned long)(inode_ref + 1), len); 96205dbe683SOmar Sandoval ptr[0] = '/'; 96305dbe683SOmar Sandoval btrfs_release_path(path); 96405dbe683SOmar Sandoval } 96505dbe683SOmar Sandoval } 96605dbe683SOmar Sandoval 96705dbe683SOmar Sandoval btrfs_free_path(path); 96805dbe683SOmar Sandoval if (ptr == name + PATH_MAX - 1) { 96905dbe683SOmar Sandoval name[0] = '/'; 97005dbe683SOmar Sandoval name[1] = '\0'; 97105dbe683SOmar Sandoval } else { 97205dbe683SOmar Sandoval memmove(name, ptr, name + PATH_MAX - ptr); 97305dbe683SOmar Sandoval } 97405dbe683SOmar Sandoval return name; 97505dbe683SOmar Sandoval 97605dbe683SOmar Sandoval err: 97705dbe683SOmar Sandoval btrfs_free_path(path); 97805dbe683SOmar Sandoval kfree(name); 97905dbe683SOmar Sandoval return ERR_PTR(ret); 98005dbe683SOmar Sandoval } 98105dbe683SOmar Sandoval 98205dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid) 98305dbe683SOmar Sandoval { 98405dbe683SOmar Sandoval struct btrfs_root *root = fs_info->tree_root; 98573f73415SJosef Bacik struct btrfs_dir_item *di; 98673f73415SJosef Bacik struct btrfs_path *path; 98773f73415SJosef Bacik struct btrfs_key location; 98873f73415SJosef Bacik u64 dir_id; 98973f73415SJosef Bacik 99073f73415SJosef Bacik path = btrfs_alloc_path(); 99173f73415SJosef Bacik if (!path) 99205dbe683SOmar Sandoval return -ENOMEM; 99373f73415SJosef Bacik path->leave_spinning = 1; 99473f73415SJosef Bacik 99573f73415SJosef Bacik /* 99673f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 99773f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 99873f73415SJosef Bacik * to mount. 99973f73415SJosef Bacik */ 1000815745cfSAl Viro dir_id = btrfs_super_root_dir(fs_info->super_copy); 100173f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 1002b0839166SJulia Lawall if (IS_ERR(di)) { 1003b0839166SJulia Lawall btrfs_free_path(path); 100405dbe683SOmar Sandoval return PTR_ERR(di); 1005b0839166SJulia Lawall } 100673f73415SJosef Bacik if (!di) { 100773f73415SJosef Bacik /* 100873f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 100973f73415SJosef Bacik * it's always been there, but don't freak out, just try and 101005dbe683SOmar Sandoval * mount the top-level subvolume. 101173f73415SJosef Bacik */ 101273f73415SJosef Bacik btrfs_free_path(path); 101305dbe683SOmar Sandoval *objectid = BTRFS_FS_TREE_OBJECTID; 101405dbe683SOmar Sandoval return 0; 101573f73415SJosef Bacik } 101673f73415SJosef Bacik 101773f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 101873f73415SJosef Bacik btrfs_free_path(path); 101905dbe683SOmar Sandoval *objectid = location.objectid; 102005dbe683SOmar Sandoval return 0; 102173f73415SJosef Bacik } 102273f73415SJosef Bacik 10238a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 10248a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 10258a4b83ccSChris Mason void *data, int silent) 10262e635a27SChris Mason { 10272e635a27SChris Mason struct inode *inode; 1028815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 10295d4f98a2SYan Zheng struct btrfs_key key; 103039279cc3SChris Mason int err; 10312e635a27SChris Mason 10322e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 10332e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 1034e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 1035af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 1036be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 10375103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 10382e635a27SChris Mason sb->s_time_gran = 1; 10390eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 104033268eafSJosef Bacik sb->s_flags |= MS_POSIXACL; 104149cf6f45SChris Ball #endif 10420c4d2d95SJosef Bacik sb->s_flags |= MS_I_VERSION; 1043da2f0f74SChris Mason sb->s_iflags |= SB_I_CGROUPWB; 1044ad2b2c80SAl Viro err = open_ctree(sb, fs_devices, (char *)data); 1045ad2b2c80SAl Viro if (err) { 1046efe120a0SFrank Holton printk(KERN_ERR "BTRFS: open_ctree failed\n"); 1047ad2b2c80SAl Viro return err; 1048e20d96d6SChris Mason } 1049b888db2bSChris Mason 10505d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 10515d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 10525d4f98a2SYan Zheng key.offset = 0; 105398c7089cSAl Viro inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL); 10545d4f98a2SYan Zheng if (IS_ERR(inode)) { 10555d4f98a2SYan Zheng err = PTR_ERR(inode); 105639279cc3SChris Mason goto fail_close; 105739279cc3SChris Mason } 10582e635a27SChris Mason 105948fde701SAl Viro sb->s_root = d_make_root(inode); 106048fde701SAl Viro if (!sb->s_root) { 106139279cc3SChris Mason err = -ENOMEM; 106239279cc3SChris Mason goto fail_close; 10632e635a27SChris Mason } 106458176a96SJosef Bacik 10656885f308SChris Mason save_mount_options(sb, data); 106690a887c9SDan Magenheimer cleancache_init_fs(sb); 106759553edfSAl Viro sb->s_flags |= MS_ACTIVE; 10682e635a27SChris Mason return 0; 10692e635a27SChris Mason 107039279cc3SChris Mason fail_close: 1071815745cfSAl Viro close_ctree(fs_info->tree_root); 1072d5719762SChris Mason return err; 1073d5719762SChris Mason } 1074d5719762SChris Mason 10756bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 1076d5719762SChris Mason { 1077d5719762SChris Mason struct btrfs_trans_handle *trans; 1078815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1079815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 1080df2ce34cSChris Mason 10811abe9b8aSliubo trace_btrfs_sync_fs(wait); 10821abe9b8aSliubo 1083d561c025SChris Mason if (!wait) { 1084815745cfSAl Viro filemap_flush(fs_info->btree_inode->i_mapping); 1085df2ce34cSChris Mason return 0; 1086d561c025SChris Mason } 1087771ed689SChris Mason 1088b0244199SMiao Xie btrfs_wait_ordered_roots(fs_info, -1); 1089771ed689SChris Mason 1090d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 109160376ce4SJosef Bacik if (IS_ERR(trans)) { 1092354aa0fbSMiao Xie /* no transaction, don't bother */ 10936b5fe46dSDavid Sterba if (PTR_ERR(trans) == -ENOENT) { 10946b5fe46dSDavid Sterba /* 10956b5fe46dSDavid Sterba * Exit unless we have some pending changes 10966b5fe46dSDavid Sterba * that need to go through commit 10976b5fe46dSDavid Sterba */ 10986b5fe46dSDavid Sterba if (fs_info->pending_changes == 0) 1099bd7de2c9SJosef Bacik return 0; 1100a53f4f8eSQu Wenruo /* 1101a53f4f8eSQu Wenruo * A non-blocking test if the fs is frozen. We must not 1102a53f4f8eSQu Wenruo * start a new transaction here otherwise a deadlock 1103a53f4f8eSQu Wenruo * happens. The pending operations are delayed to the 1104a53f4f8eSQu Wenruo * next commit after thawing. 1105a53f4f8eSQu Wenruo */ 1106a53f4f8eSQu Wenruo if (__sb_start_write(sb, SB_FREEZE_WRITE, false)) 1107a53f4f8eSQu Wenruo __sb_end_write(sb, SB_FREEZE_WRITE); 1108a53f4f8eSQu Wenruo else 1109a53f4f8eSQu Wenruo return 0; 11106b5fe46dSDavid Sterba trans = btrfs_start_transaction(root, 0); 111160376ce4SJosef Bacik } 111298bd5c54SDavid Sterba if (IS_ERR(trans)) 111398bd5c54SDavid Sterba return PTR_ERR(trans); 11146b5fe46dSDavid Sterba } 1115bd7de2c9SJosef Bacik return btrfs_commit_transaction(trans, root); 1116d5719762SChris Mason } 1117d5719762SChris Mason 111834c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 1119a9572a15SEric Paris { 1120815745cfSAl Viro struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 1121815745cfSAl Viro struct btrfs_root *root = info->tree_root; 1122200da64eSTsutomu Itoh char *compress_type; 1123a9572a15SEric Paris 1124a9572a15SEric Paris if (btrfs_test_opt(root, DEGRADED)) 1125a9572a15SEric Paris seq_puts(seq, ",degraded"); 1126a9572a15SEric Paris if (btrfs_test_opt(root, NODATASUM)) 1127a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 1128a9572a15SEric Paris if (btrfs_test_opt(root, NODATACOW)) 1129a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 1130a9572a15SEric Paris if (btrfs_test_opt(root, NOBARRIER)) 1131a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 113295ac567aSFilipe David Borba Manana if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE) 1133c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",max_inline=%llu", info->max_inline); 1134a9572a15SEric Paris if (info->alloc_start != 0) 1135c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",alloc_start=%llu", info->alloc_start); 1136a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 1137a9572a15SEric Paris num_online_cpus() + 2, 8)) 1138a9572a15SEric Paris seq_printf(seq, ",thread_pool=%d", info->thread_pool_size); 1139200da64eSTsutomu Itoh if (btrfs_test_opt(root, COMPRESS)) { 1140200da64eSTsutomu Itoh if (info->compress_type == BTRFS_COMPRESS_ZLIB) 1141200da64eSTsutomu Itoh compress_type = "zlib"; 1142200da64eSTsutomu Itoh else 1143200da64eSTsutomu Itoh compress_type = "lzo"; 1144200da64eSTsutomu Itoh if (btrfs_test_opt(root, FORCE_COMPRESS)) 1145200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 1146200da64eSTsutomu Itoh else 1147200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 1148200da64eSTsutomu Itoh } 1149c289811cSChris Mason if (btrfs_test_opt(root, NOSSD)) 1150c289811cSChris Mason seq_puts(seq, ",nossd"); 1151451d7585SChris Mason if (btrfs_test_opt(root, SSD_SPREAD)) 1152451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 1153451d7585SChris Mason else if (btrfs_test_opt(root, SSD)) 1154a9572a15SEric Paris seq_puts(seq, ",ssd"); 11553a5e1404SSage Weil if (btrfs_test_opt(root, NOTREELOG)) 11566b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 1157dccae999SSage Weil if (btrfs_test_opt(root, FLUSHONCOMMIT)) 11586b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 115920a5239aSMatthew Wilcox if (btrfs_test_opt(root, DISCARD)) 116020a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 1161a9572a15SEric Paris if (!(root->fs_info->sb->s_flags & MS_POSIXACL)) 1162a9572a15SEric Paris seq_puts(seq, ",noacl"); 1163200da64eSTsutomu Itoh if (btrfs_test_opt(root, SPACE_CACHE)) 1164200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 116573bc1876SJosef Bacik else 11668965593eSDavid Sterba seq_puts(seq, ",nospace_cache"); 1167f420ee1eSStefan Behrens if (btrfs_test_opt(root, RESCAN_UUID_TREE)) 1168f420ee1eSStefan Behrens seq_puts(seq, ",rescan_uuid_tree"); 1169200da64eSTsutomu Itoh if (btrfs_test_opt(root, CLEAR_CACHE)) 1170200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 1171200da64eSTsutomu Itoh if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 1172200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 11730942caa3SDavid Sterba if (btrfs_test_opt(root, ENOSPC_DEBUG)) 11740942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 11750942caa3SDavid Sterba if (btrfs_test_opt(root, AUTO_DEFRAG)) 11760942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 11770942caa3SDavid Sterba if (btrfs_test_opt(root, INODE_MAP_CACHE)) 11780942caa3SDavid Sterba seq_puts(seq, ",inode_cache"); 11799555c6c1SIlya Dryomov if (btrfs_test_opt(root, SKIP_BALANCE)) 11809555c6c1SIlya Dryomov seq_puts(seq, ",skip_balance"); 11818507d216SWang Shilong if (btrfs_test_opt(root, RECOVERY)) 11828507d216SWang Shilong seq_puts(seq, ",recovery"); 11838507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 11848507d216SWang Shilong if (btrfs_test_opt(root, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA)) 11858507d216SWang Shilong seq_puts(seq, ",check_int_data"); 11868507d216SWang Shilong else if (btrfs_test_opt(root, CHECK_INTEGRITY)) 11878507d216SWang Shilong seq_puts(seq, ",check_int"); 11888507d216SWang Shilong if (info->check_integrity_print_mask) 11898507d216SWang Shilong seq_printf(seq, ",check_int_print_mask=%d", 11908507d216SWang Shilong info->check_integrity_print_mask); 11918507d216SWang Shilong #endif 11928507d216SWang Shilong if (info->metadata_ratio) 11938507d216SWang Shilong seq_printf(seq, ",metadata_ratio=%d", 11948507d216SWang Shilong info->metadata_ratio); 11958c342930SJeff Mahoney if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR)) 11968c342930SJeff Mahoney seq_puts(seq, ",fatal_errors=panic"); 11978b87dc17SDavid Sterba if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL) 11988b87dc17SDavid Sterba seq_printf(seq, ",commit=%d", info->commit_interval); 1199d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1200d0bd4560SJosef Bacik if (btrfs_test_opt(root, FRAGMENT_DATA)) 1201d0bd4560SJosef Bacik seq_puts(seq, ",fragment=data"); 1202d0bd4560SJosef Bacik if (btrfs_test_opt(root, FRAGMENT_METADATA)) 1203d0bd4560SJosef Bacik seq_puts(seq, ",fragment=metadata"); 1204d0bd4560SJosef Bacik #endif 1205c8d3fe02SOmar Sandoval seq_printf(seq, ",subvolid=%llu", 1206c8d3fe02SOmar Sandoval BTRFS_I(d_inode(dentry))->root->root_key.objectid); 1207c8d3fe02SOmar Sandoval seq_puts(seq, ",subvol="); 1208c8d3fe02SOmar Sandoval seq_dentry(seq, dentry, " \t\n\\"); 1209a9572a15SEric Paris return 0; 1210a9572a15SEric Paris } 1211a9572a15SEric Paris 1212a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 12132e635a27SChris Mason { 1214815745cfSAl Viro struct btrfs_fs_info *p = data; 1215815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(s); 12164b82d6e4SYan 1217815745cfSAl Viro return fs_info->fs_devices == p->fs_devices; 12184b82d6e4SYan } 12194b82d6e4SYan 1220450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 1221450ba0eaSJosef Bacik { 12226de1d09dSAl Viro int err = set_anon_super(s, data); 12236de1d09dSAl Viro if (!err) 1224450ba0eaSJosef Bacik s->s_fs_info = data; 12256de1d09dSAl Viro return err; 1226450ba0eaSJosef Bacik } 1227450ba0eaSJosef Bacik 1228830c4adbSJosef Bacik /* 1229f9d9ef62SDavid Sterba * subvolumes are identified by ino 256 1230f9d9ef62SDavid Sterba */ 1231f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode) 1232f9d9ef62SDavid Sterba { 1233f9d9ef62SDavid Sterba if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 1234f9d9ef62SDavid Sterba return 1; 1235f9d9ef62SDavid Sterba return 0; 1236f9d9ef62SDavid Sterba } 1237f9d9ef62SDavid Sterba 1238f9d9ef62SDavid Sterba /* 1239e6e4dbe8SOmar Sandoval * This will add subvolid=0 to the argument string while removing any subvol= 1240e6e4dbe8SOmar Sandoval * and subvolid= arguments to make sure we get the top-level root for path 1241e6e4dbe8SOmar Sandoval * walking to the subvol we want. 1242830c4adbSJosef Bacik */ 1243830c4adbSJosef Bacik static char *setup_root_args(char *args) 1244830c4adbSJosef Bacik { 1245e6e4dbe8SOmar Sandoval char *buf, *dst, *sep; 1246830c4adbSJosef Bacik 1247e6e4dbe8SOmar Sandoval if (!args) 1248e6e4dbe8SOmar Sandoval return kstrdup("subvolid=0", GFP_NOFS); 1249830c4adbSJosef Bacik 1250e6e4dbe8SOmar Sandoval /* The worst case is that we add ",subvolid=0" to the end. */ 1251e6e4dbe8SOmar Sandoval buf = dst = kmalloc(strlen(args) + strlen(",subvolid=0") + 1, GFP_NOFS); 1252f60d16a8SJim Meyering if (!buf) 1253f60d16a8SJim Meyering return NULL; 1254830c4adbSJosef Bacik 1255e6e4dbe8SOmar Sandoval while (1) { 1256e6e4dbe8SOmar Sandoval sep = strchrnul(args, ','); 1257e6e4dbe8SOmar Sandoval if (!strstarts(args, "subvol=") && 1258e6e4dbe8SOmar Sandoval !strstarts(args, "subvolid=")) { 1259e6e4dbe8SOmar Sandoval memcpy(dst, args, sep - args); 1260e6e4dbe8SOmar Sandoval dst += sep - args; 1261e6e4dbe8SOmar Sandoval *dst++ = ','; 1262830c4adbSJosef Bacik } 1263e6e4dbe8SOmar Sandoval if (*sep) 1264e6e4dbe8SOmar Sandoval args = sep + 1; 1265e6e4dbe8SOmar Sandoval else 1266e6e4dbe8SOmar Sandoval break; 1267e6e4dbe8SOmar Sandoval } 1268f60d16a8SJim Meyering strcpy(dst, "subvolid=0"); 1269830c4adbSJosef Bacik 1270f60d16a8SJim Meyering return buf; 1271830c4adbSJosef Bacik } 1272830c4adbSJosef Bacik 1273bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid, 1274bb289b7bSOmar Sandoval int flags, const char *device_name, 1275bb289b7bSOmar Sandoval char *data) 1276830c4adbSJosef Bacik { 1277830c4adbSJosef Bacik struct dentry *root; 1278fa330659SOmar Sandoval struct vfsmount *mnt = NULL; 1279830c4adbSJosef Bacik char *newargs; 1280fa330659SOmar Sandoval int ret; 1281830c4adbSJosef Bacik 1282830c4adbSJosef Bacik newargs = setup_root_args(data); 1283fa330659SOmar Sandoval if (!newargs) { 1284fa330659SOmar Sandoval root = ERR_PTR(-ENOMEM); 1285fa330659SOmar Sandoval goto out; 1286fa330659SOmar Sandoval } 12870723a047SHarald Hoyer 1288fa330659SOmar Sandoval mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, newargs); 1289fa330659SOmar Sandoval if (PTR_ERR_OR_ZERO(mnt) == -EBUSY) { 12900723a047SHarald Hoyer if (flags & MS_RDONLY) { 1291fa330659SOmar Sandoval mnt = vfs_kern_mount(&btrfs_fs_type, flags & ~MS_RDONLY, 1292fa330659SOmar Sandoval device_name, newargs); 12930723a047SHarald Hoyer } else { 1294fa330659SOmar Sandoval mnt = vfs_kern_mount(&btrfs_fs_type, flags | MS_RDONLY, 1295fa330659SOmar Sandoval device_name, newargs); 12960040e606SChristoph Jaeger if (IS_ERR(mnt)) { 1297fa330659SOmar Sandoval root = ERR_CAST(mnt); 1298fa330659SOmar Sandoval mnt = NULL; 1299fa330659SOmar Sandoval goto out; 13000040e606SChristoph Jaeger } 13010723a047SHarald Hoyer 1302773cd04eSOmar Sandoval down_write(&mnt->mnt_sb->s_umount); 1303fa330659SOmar Sandoval ret = btrfs_remount(mnt->mnt_sb, &flags, NULL); 1304773cd04eSOmar Sandoval up_write(&mnt->mnt_sb->s_umount); 1305fa330659SOmar Sandoval if (ret < 0) { 1306fa330659SOmar Sandoval root = ERR_PTR(ret); 1307fa330659SOmar Sandoval goto out; 13080723a047SHarald Hoyer } 13090723a047SHarald Hoyer } 13100723a047SHarald Hoyer } 1311fa330659SOmar Sandoval if (IS_ERR(mnt)) { 1312fa330659SOmar Sandoval root = ERR_CAST(mnt); 1313fa330659SOmar Sandoval mnt = NULL; 1314fa330659SOmar Sandoval goto out; 1315fa330659SOmar Sandoval } 1316830c4adbSJosef Bacik 131705dbe683SOmar Sandoval if (!subvol_name) { 131805dbe683SOmar Sandoval if (!subvol_objectid) { 131905dbe683SOmar Sandoval ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb), 132005dbe683SOmar Sandoval &subvol_objectid); 132105dbe683SOmar Sandoval if (ret) { 132205dbe683SOmar Sandoval root = ERR_PTR(ret); 132305dbe683SOmar Sandoval goto out; 132405dbe683SOmar Sandoval } 132505dbe683SOmar Sandoval } 132605dbe683SOmar Sandoval subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb), 132705dbe683SOmar Sandoval subvol_objectid); 132805dbe683SOmar Sandoval if (IS_ERR(subvol_name)) { 132905dbe683SOmar Sandoval root = ERR_CAST(subvol_name); 133005dbe683SOmar Sandoval subvol_name = NULL; 133105dbe683SOmar Sandoval goto out; 133205dbe683SOmar Sandoval } 133305dbe683SOmar Sandoval 133405dbe683SOmar Sandoval } 133505dbe683SOmar Sandoval 1336ea441d11SAl Viro root = mount_subtree(mnt, subvol_name); 1337fa330659SOmar Sandoval /* mount_subtree() drops our reference on the vfsmount. */ 1338fa330659SOmar Sandoval mnt = NULL; 1339830c4adbSJosef Bacik 1340bb289b7bSOmar Sandoval if (!IS_ERR(root)) { 1341ea441d11SAl Viro struct super_block *s = root->d_sb; 1342bb289b7bSOmar Sandoval struct inode *root_inode = d_inode(root); 1343bb289b7bSOmar Sandoval u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid; 1344bb289b7bSOmar Sandoval 1345bb289b7bSOmar Sandoval ret = 0; 1346bb289b7bSOmar Sandoval if (!is_subvolume_inode(root_inode)) { 1347bb289b7bSOmar Sandoval pr_err("BTRFS: '%s' is not a valid subvolume\n", 1348bb289b7bSOmar Sandoval subvol_name); 1349bb289b7bSOmar Sandoval ret = -EINVAL; 1350bb289b7bSOmar Sandoval } 1351bb289b7bSOmar Sandoval if (subvol_objectid && root_objectid != subvol_objectid) { 135205dbe683SOmar Sandoval /* 135305dbe683SOmar Sandoval * This will also catch a race condition where a 135405dbe683SOmar Sandoval * subvolume which was passed by ID is renamed and 135505dbe683SOmar Sandoval * another subvolume is renamed over the old location. 135605dbe683SOmar Sandoval */ 1357bb289b7bSOmar Sandoval pr_err("BTRFS: subvol '%s' does not match subvolid %llu\n", 1358bb289b7bSOmar Sandoval subvol_name, subvol_objectid); 1359bb289b7bSOmar Sandoval ret = -EINVAL; 1360bb289b7bSOmar Sandoval } 1361bb289b7bSOmar Sandoval if (ret) { 1362ea441d11SAl Viro dput(root); 1363bb289b7bSOmar Sandoval root = ERR_PTR(ret); 1364ea441d11SAl Viro deactivate_locked_super(s); 1365bb289b7bSOmar Sandoval } 1366f9d9ef62SDavid Sterba } 1367f9d9ef62SDavid Sterba 1368fa330659SOmar Sandoval out: 1369fa330659SOmar Sandoval mntput(mnt); 1370fa330659SOmar Sandoval kfree(newargs); 1371fa330659SOmar Sandoval kfree(subvol_name); 1372830c4adbSJosef Bacik return root; 1373830c4adbSJosef Bacik } 1374450ba0eaSJosef Bacik 1375f667aef6SQu Wenruo static int parse_security_options(char *orig_opts, 1376f667aef6SQu Wenruo struct security_mnt_opts *sec_opts) 1377f667aef6SQu Wenruo { 1378f667aef6SQu Wenruo char *secdata = NULL; 1379f667aef6SQu Wenruo int ret = 0; 1380f667aef6SQu Wenruo 1381f667aef6SQu Wenruo secdata = alloc_secdata(); 1382f667aef6SQu Wenruo if (!secdata) 1383f667aef6SQu Wenruo return -ENOMEM; 1384f667aef6SQu Wenruo ret = security_sb_copy_data(orig_opts, secdata); 1385f667aef6SQu Wenruo if (ret) { 1386f667aef6SQu Wenruo free_secdata(secdata); 1387f667aef6SQu Wenruo return ret; 1388f667aef6SQu Wenruo } 1389f667aef6SQu Wenruo ret = security_sb_parse_opts_str(secdata, sec_opts); 1390f667aef6SQu Wenruo free_secdata(secdata); 1391f667aef6SQu Wenruo return ret; 1392f667aef6SQu Wenruo } 1393f667aef6SQu Wenruo 1394f667aef6SQu Wenruo static int setup_security_options(struct btrfs_fs_info *fs_info, 1395f667aef6SQu Wenruo struct super_block *sb, 1396f667aef6SQu Wenruo struct security_mnt_opts *sec_opts) 1397f667aef6SQu Wenruo { 1398f667aef6SQu Wenruo int ret = 0; 1399f667aef6SQu Wenruo 1400f667aef6SQu Wenruo /* 1401f667aef6SQu Wenruo * Call security_sb_set_mnt_opts() to check whether new sec_opts 1402f667aef6SQu Wenruo * is valid. 1403f667aef6SQu Wenruo */ 1404f667aef6SQu Wenruo ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL); 1405f667aef6SQu Wenruo if (ret) 1406f667aef6SQu Wenruo return ret; 1407f667aef6SQu Wenruo 1408a43bb39bSQu Wenruo #ifdef CONFIG_SECURITY 1409f667aef6SQu Wenruo if (!fs_info->security_opts.num_mnt_opts) { 1410f667aef6SQu Wenruo /* first time security setup, copy sec_opts to fs_info */ 1411f667aef6SQu Wenruo memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts)); 1412f667aef6SQu Wenruo } else { 1413f667aef6SQu Wenruo /* 1414f667aef6SQu Wenruo * Since SELinux(the only one supports security_mnt_opts) does 1415f667aef6SQu Wenruo * NOT support changing context during remount/mount same sb, 1416f667aef6SQu Wenruo * This must be the same or part of the same security options, 1417f667aef6SQu Wenruo * just free it. 1418f667aef6SQu Wenruo */ 1419f667aef6SQu Wenruo security_free_mnt_opts(sec_opts); 1420f667aef6SQu Wenruo } 1421a43bb39bSQu Wenruo #endif 1422f667aef6SQu Wenruo return ret; 1423f667aef6SQu Wenruo } 1424f667aef6SQu Wenruo 1425edf24abeSChristoph Hellwig /* 1426edf24abeSChristoph Hellwig * Find a superblock for the given device / mount point. 1427edf24abeSChristoph Hellwig * 1428edf24abeSChristoph Hellwig * Note: This is based on get_sb_bdev from fs/super.c with a few additions 1429edf24abeSChristoph Hellwig * for multiple device setup. Make sure to keep it in sync. 1430edf24abeSChristoph Hellwig */ 1431061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1432306e16ceSDavid Sterba const char *device_name, void *data) 14334b82d6e4SYan { 14344b82d6e4SYan struct block_device *bdev = NULL; 14354b82d6e4SYan struct super_block *s; 14368a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices = NULL; 1437450ba0eaSJosef Bacik struct btrfs_fs_info *fs_info = NULL; 1438f667aef6SQu Wenruo struct security_mnt_opts new_sec_opts; 143997288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 144073f73415SJosef Bacik char *subvol_name = NULL; 144173f73415SJosef Bacik u64 subvol_objectid = 0; 14424b82d6e4SYan int error = 0; 14434b82d6e4SYan 144497288f2cSChristoph Hellwig if (!(flags & MS_RDONLY)) 144597288f2cSChristoph Hellwig mode |= FMODE_WRITE; 144697288f2cSChristoph Hellwig 144797288f2cSChristoph Hellwig error = btrfs_parse_early_options(data, mode, fs_type, 144873f73415SJosef Bacik &subvol_name, &subvol_objectid, 14495e2a4b25SDavid Sterba &fs_devices); 1450f23c8af8SIlya Dryomov if (error) { 1451f23c8af8SIlya Dryomov kfree(subvol_name); 1452061dbc6bSAl Viro return ERR_PTR(error); 1453f23c8af8SIlya Dryomov } 1454edf24abeSChristoph Hellwig 145505dbe683SOmar Sandoval if (subvol_name || subvol_objectid != BTRFS_FS_TREE_OBJECTID) { 1456fa330659SOmar Sandoval /* mount_subvol() will free subvol_name. */ 1457bb289b7bSOmar Sandoval return mount_subvol(subvol_name, subvol_objectid, flags, 1458bb289b7bSOmar Sandoval device_name, data); 1459830c4adbSJosef Bacik } 1460830c4adbSJosef Bacik 1461f667aef6SQu Wenruo security_init_mnt_opts(&new_sec_opts); 1462f667aef6SQu Wenruo if (data) { 1463f667aef6SQu Wenruo error = parse_security_options(data, &new_sec_opts); 14648a4b83ccSChris Mason if (error) 1465830c4adbSJosef Bacik return ERR_PTR(error); 1466f667aef6SQu Wenruo } 1467f667aef6SQu Wenruo 1468f667aef6SQu Wenruo error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices); 1469f667aef6SQu Wenruo if (error) 1470f667aef6SQu Wenruo goto error_sec_opts; 14714b82d6e4SYan 1472450ba0eaSJosef Bacik /* 1473450ba0eaSJosef Bacik * Setup a dummy root and fs_info for test/set super. This is because 1474450ba0eaSJosef Bacik * we don't actually fill this stuff out until open_ctree, but we need 1475450ba0eaSJosef Bacik * it for searching for existing supers, so this lets us do that and 1476450ba0eaSJosef Bacik * then open_ctree will properly initialize everything later. 1477450ba0eaSJosef Bacik */ 1478450ba0eaSJosef Bacik fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS); 1479f667aef6SQu Wenruo if (!fs_info) { 1480f667aef6SQu Wenruo error = -ENOMEM; 1481f667aef6SQu Wenruo goto error_sec_opts; 1482f667aef6SQu Wenruo } 148304d21a24SIlya Dryomov 1484450ba0eaSJosef Bacik fs_info->fs_devices = fs_devices; 1485450ba0eaSJosef Bacik 14866c41761fSDavid Sterba fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 14876c41761fSDavid Sterba fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 1488f667aef6SQu Wenruo security_init_mnt_opts(&fs_info->security_opts); 14896c41761fSDavid Sterba if (!fs_info->super_copy || !fs_info->super_for_commit) { 14906c41761fSDavid Sterba error = -ENOMEM; 149104d21a24SIlya Dryomov goto error_fs_info; 149204d21a24SIlya Dryomov } 149304d21a24SIlya Dryomov 149404d21a24SIlya Dryomov error = btrfs_open_devices(fs_devices, mode, fs_type); 149504d21a24SIlya Dryomov if (error) 149604d21a24SIlya Dryomov goto error_fs_info; 149704d21a24SIlya Dryomov 149804d21a24SIlya Dryomov if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) { 149904d21a24SIlya Dryomov error = -EACCES; 15006c41761fSDavid Sterba goto error_close_devices; 15016c41761fSDavid Sterba } 15026c41761fSDavid Sterba 1503dfe25020SChris Mason bdev = fs_devices->latest_bdev; 15049249e17fSDavid Howells s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC, 15059249e17fSDavid Howells fs_info); 1506830c4adbSJosef Bacik if (IS_ERR(s)) { 1507830c4adbSJosef Bacik error = PTR_ERR(s); 1508830c4adbSJosef Bacik goto error_close_devices; 1509830c4adbSJosef Bacik } 15104b82d6e4SYan 15114b82d6e4SYan if (s->s_root) { 15122b82032cSYan Zheng btrfs_close_devices(fs_devices); 15136c41761fSDavid Sterba free_fs_info(fs_info); 151459553edfSAl Viro if ((flags ^ s->s_flags) & MS_RDONLY) 151559553edfSAl Viro error = -EBUSY; 15164b82d6e4SYan } else { 15174b82d6e4SYan char b[BDEVNAME_SIZE]; 15184b82d6e4SYan 15194b82d6e4SYan strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); 1520815745cfSAl Viro btrfs_sb(s)->bdev_holder = fs_type; 15218a4b83ccSChris Mason error = btrfs_fill_super(s, fs_devices, data, 15228a4b83ccSChris Mason flags & MS_SILENT ? 1 : 0); 15234b82d6e4SYan } 152405dbe683SOmar Sandoval if (error) { 1525e15d0542SXin Zhong deactivate_locked_super(s); 1526f667aef6SQu Wenruo goto error_sec_opts; 1527f667aef6SQu Wenruo } 1528f667aef6SQu Wenruo 1529f667aef6SQu Wenruo fs_info = btrfs_sb(s); 1530f667aef6SQu Wenruo error = setup_security_options(fs_info, s, &new_sec_opts); 1531f667aef6SQu Wenruo if (error) { 1532f667aef6SQu Wenruo deactivate_locked_super(s); 1533f667aef6SQu Wenruo goto error_sec_opts; 1534f667aef6SQu Wenruo } 15354b82d6e4SYan 153605dbe683SOmar Sandoval return dget(s->s_root); 15374b82d6e4SYan 1538c146afadSYan Zheng error_close_devices: 15398a4b83ccSChris Mason btrfs_close_devices(fs_devices); 154004d21a24SIlya Dryomov error_fs_info: 15416c41761fSDavid Sterba free_fs_info(fs_info); 1542f667aef6SQu Wenruo error_sec_opts: 1543f667aef6SQu Wenruo security_free_mnt_opts(&new_sec_opts); 1544061dbc6bSAl Viro return ERR_PTR(error); 15454b82d6e4SYan } 15462e635a27SChris Mason 15470d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 15480d2450abSSergei Trofimovich int new_pool_size, int old_pool_size) 15490d2450abSSergei Trofimovich { 15500d2450abSSergei Trofimovich if (new_pool_size == old_pool_size) 15510d2450abSSergei Trofimovich return; 15520d2450abSSergei Trofimovich 15530d2450abSSergei Trofimovich fs_info->thread_pool_size = new_pool_size; 15540d2450abSSergei Trofimovich 1555efe120a0SFrank Holton btrfs_info(fs_info, "resize thread pool %d -> %d", 15560d2450abSSergei Trofimovich old_pool_size, new_pool_size); 15570d2450abSSergei Trofimovich 15585cdc7ad3SQu Wenruo btrfs_workqueue_set_max(fs_info->workers, new_pool_size); 1559afe3d242SQu Wenruo btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size); 1560a8c93d4eSQu Wenruo btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size); 1561e66f0bb1SQu Wenruo btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size); 1562fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size); 1563fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size); 1564fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_write_workers, 1565fccb5d86SQu Wenruo new_pool_size); 1566fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size); 1567fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size); 15685b3bc44eSQu Wenruo btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size); 1569736cfa15SQu Wenruo btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size); 15700339ef2fSQu Wenruo btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers, 1571ff023aacSStefan Behrens new_pool_size); 15720d2450abSSergei Trofimovich } 15730d2450abSSergei Trofimovich 1574f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info) 1575dc81cdc5SMiao Xie { 1576dc81cdc5SMiao Xie set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1577f42a34b2SMiao Xie } 1578dc81cdc5SMiao Xie 1579f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info, 1580f42a34b2SMiao Xie unsigned long old_opts, int flags) 1581f42a34b2SMiao Xie { 1582dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1583dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1584dc81cdc5SMiao Xie (flags & MS_RDONLY))) { 1585dc81cdc5SMiao Xie /* wait for any defraggers to finish */ 1586dc81cdc5SMiao Xie wait_event(fs_info->transaction_wait, 1587dc81cdc5SMiao Xie (atomic_read(&fs_info->defrag_running) == 0)); 1588dc81cdc5SMiao Xie if (flags & MS_RDONLY) 1589dc81cdc5SMiao Xie sync_filesystem(fs_info->sb); 1590dc81cdc5SMiao Xie } 1591dc81cdc5SMiao Xie } 1592dc81cdc5SMiao Xie 1593dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info, 1594dc81cdc5SMiao Xie unsigned long old_opts) 1595dc81cdc5SMiao Xie { 1596dc81cdc5SMiao Xie /* 1597dc81cdc5SMiao Xie * We need cleanup all defragable inodes if the autodefragment is 1598dc81cdc5SMiao Xie * close or the fs is R/O. 1599dc81cdc5SMiao Xie */ 1600dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1601dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1602dc81cdc5SMiao Xie (fs_info->sb->s_flags & MS_RDONLY))) { 1603dc81cdc5SMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 1604dc81cdc5SMiao Xie } 1605dc81cdc5SMiao Xie 1606dc81cdc5SMiao Xie clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1607dc81cdc5SMiao Xie } 1608dc81cdc5SMiao Xie 1609c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1610c146afadSYan Zheng { 1611815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1612815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 161349b25e05SJeff Mahoney unsigned old_flags = sb->s_flags; 161449b25e05SJeff Mahoney unsigned long old_opts = fs_info->mount_opt; 161549b25e05SJeff Mahoney unsigned long old_compress_type = fs_info->compress_type; 161649b25e05SJeff Mahoney u64 old_max_inline = fs_info->max_inline; 161749b25e05SJeff Mahoney u64 old_alloc_start = fs_info->alloc_start; 161849b25e05SJeff Mahoney int old_thread_pool_size = fs_info->thread_pool_size; 161949b25e05SJeff Mahoney unsigned int old_metadata_ratio = fs_info->metadata_ratio; 1620c146afadSYan Zheng int ret; 1621c146afadSYan Zheng 162202b9984dSTheodore Ts'o sync_filesystem(sb); 1623f42a34b2SMiao Xie btrfs_remount_prepare(fs_info); 1624dc81cdc5SMiao Xie 1625f667aef6SQu Wenruo if (data) { 1626f667aef6SQu Wenruo struct security_mnt_opts new_sec_opts; 1627f667aef6SQu Wenruo 1628f667aef6SQu Wenruo security_init_mnt_opts(&new_sec_opts); 1629f667aef6SQu Wenruo ret = parse_security_options(data, &new_sec_opts); 1630f667aef6SQu Wenruo if (ret) 1631f667aef6SQu Wenruo goto restore; 1632f667aef6SQu Wenruo ret = setup_security_options(fs_info, sb, 1633f667aef6SQu Wenruo &new_sec_opts); 1634f667aef6SQu Wenruo if (ret) { 1635f667aef6SQu Wenruo security_free_mnt_opts(&new_sec_opts); 1636f667aef6SQu Wenruo goto restore; 1637f667aef6SQu Wenruo } 1638f667aef6SQu Wenruo } 1639f667aef6SQu Wenruo 1640b288052eSChris Mason ret = btrfs_parse_options(root, data); 164149b25e05SJeff Mahoney if (ret) { 164249b25e05SJeff Mahoney ret = -EINVAL; 164349b25e05SJeff Mahoney goto restore; 164449b25e05SJeff Mahoney } 1645b288052eSChris Mason 1646f42a34b2SMiao Xie btrfs_remount_begin(fs_info, old_opts, *flags); 16470d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 16480d2450abSSergei Trofimovich fs_info->thread_pool_size, old_thread_pool_size); 16490d2450abSSergei Trofimovich 1650c146afadSYan Zheng if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 1651dc81cdc5SMiao Xie goto out; 1652c146afadSYan Zheng 1653c146afadSYan Zheng if (*flags & MS_RDONLY) { 16548dabb742SStefan Behrens /* 16558dabb742SStefan Behrens * this also happens on 'umount -rf' or on shutdown, when 16568dabb742SStefan Behrens * the filesystem is busy. 16578dabb742SStefan Behrens */ 165821c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 1659361c093dSStefan Behrens 1660361c093dSStefan Behrens /* wait for the uuid_scan task to finish */ 1661361c093dSStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 1662361c093dSStefan Behrens /* avoid complains from lockdep et al. */ 1663361c093dSStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 1664361c093dSStefan Behrens 1665c146afadSYan Zheng sb->s_flags |= MS_RDONLY; 1666c146afadSYan Zheng 1667e44163e1SJeff Mahoney /* 1668e44163e1SJeff Mahoney * Setting MS_RDONLY will put the cleaner thread to 1669e44163e1SJeff Mahoney * sleep at the next loop if it's already active. 1670e44163e1SJeff Mahoney * If it's already asleep, we'll leave unused block 1671e44163e1SJeff Mahoney * groups on disk until we're mounted read-write again 1672e44163e1SJeff Mahoney * unless we clean them up here. 1673e44163e1SJeff Mahoney */ 1674e44163e1SJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1675e44163e1SJeff Mahoney 16768dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 16778dabb742SStefan Behrens btrfs_scrub_cancel(fs_info); 1678061594efSMiao Xie btrfs_pause_balance(fs_info); 16798dabb742SStefan Behrens 1680c146afadSYan Zheng ret = btrfs_commit_super(root); 168149b25e05SJeff Mahoney if (ret) 168249b25e05SJeff Mahoney goto restore; 1683c146afadSYan Zheng } else { 16846ef3de9cSDavid Sterba if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 16856ef3de9cSDavid Sterba btrfs_err(fs_info, 1686efe120a0SFrank Holton "Remounting read-write after error is not allowed"); 16876ef3de9cSDavid Sterba ret = -EINVAL; 16886ef3de9cSDavid Sterba goto restore; 16896ef3de9cSDavid Sterba } 16908a3db184SSergei Trofimovich if (fs_info->fs_devices->rw_devices == 0) { 169149b25e05SJeff Mahoney ret = -EACCES; 169249b25e05SJeff Mahoney goto restore; 16938a3db184SSergei Trofimovich } 16942b82032cSYan Zheng 1695292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 1696292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 1697292fd7fcSStefan Behrens !(*flags & MS_RDONLY)) { 1698efe120a0SFrank Holton btrfs_warn(fs_info, 1699efe120a0SFrank Holton "too many missing devices, writeable remount is not allowed"); 1700292fd7fcSStefan Behrens ret = -EACCES; 1701292fd7fcSStefan Behrens goto restore; 1702292fd7fcSStefan Behrens } 1703292fd7fcSStefan Behrens 17048a3db184SSergei Trofimovich if (btrfs_super_log_root(fs_info->super_copy) != 0) { 170549b25e05SJeff Mahoney ret = -EINVAL; 170649b25e05SJeff Mahoney goto restore; 17078a3db184SSergei Trofimovich } 1708c146afadSYan Zheng 1709815745cfSAl Viro ret = btrfs_cleanup_fs_roots(fs_info); 171049b25e05SJeff Mahoney if (ret) 171149b25e05SJeff Mahoney goto restore; 1712c146afadSYan Zheng 1713d68fc57bSYan, Zheng /* recover relocation */ 17145f316481SWang Shilong mutex_lock(&fs_info->cleaner_mutex); 1715d68fc57bSYan, Zheng ret = btrfs_recover_relocation(root); 17165f316481SWang Shilong mutex_unlock(&fs_info->cleaner_mutex); 171749b25e05SJeff Mahoney if (ret) 171849b25e05SJeff Mahoney goto restore; 1719c146afadSYan Zheng 17202b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 17212b6ba629SIlya Dryomov if (ret) 17222b6ba629SIlya Dryomov goto restore; 17232b6ba629SIlya Dryomov 17248dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 17258dabb742SStefan Behrens if (ret) { 1726efe120a0SFrank Holton btrfs_warn(fs_info, "failed to resume dev_replace"); 17278dabb742SStefan Behrens goto restore; 17288dabb742SStefan Behrens } 172994aebfb2SJosef Bacik 173094aebfb2SJosef Bacik if (!fs_info->uuid_root) { 1731efe120a0SFrank Holton btrfs_info(fs_info, "creating UUID tree"); 173294aebfb2SJosef Bacik ret = btrfs_create_uuid_tree(fs_info); 173394aebfb2SJosef Bacik if (ret) { 1734efe120a0SFrank Holton btrfs_warn(fs_info, "failed to create the UUID tree %d", ret); 173594aebfb2SJosef Bacik goto restore; 173694aebfb2SJosef Bacik } 173794aebfb2SJosef Bacik } 1738c146afadSYan Zheng sb->s_flags &= ~MS_RDONLY; 1739c146afadSYan Zheng } 1740dc81cdc5SMiao Xie out: 17412c6a92b0SJustin Maggard wake_up_process(fs_info->transaction_kthread); 1742dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 1743c146afadSYan Zheng return 0; 174449b25e05SJeff Mahoney 174549b25e05SJeff Mahoney restore: 174649b25e05SJeff Mahoney /* We've hit an error - don't reset MS_RDONLY */ 174749b25e05SJeff Mahoney if (sb->s_flags & MS_RDONLY) 174849b25e05SJeff Mahoney old_flags |= MS_RDONLY; 174949b25e05SJeff Mahoney sb->s_flags = old_flags; 175049b25e05SJeff Mahoney fs_info->mount_opt = old_opts; 175149b25e05SJeff Mahoney fs_info->compress_type = old_compress_type; 175249b25e05SJeff Mahoney fs_info->max_inline = old_max_inline; 1753c018daecSMiao Xie mutex_lock(&fs_info->chunk_mutex); 175449b25e05SJeff Mahoney fs_info->alloc_start = old_alloc_start; 1755c018daecSMiao Xie mutex_unlock(&fs_info->chunk_mutex); 17560d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 17570d2450abSSergei Trofimovich old_thread_pool_size, fs_info->thread_pool_size); 175849b25e05SJeff Mahoney fs_info->metadata_ratio = old_metadata_ratio; 1759dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 176049b25e05SJeff Mahoney return ret; 1761c146afadSYan Zheng } 1762c146afadSYan Zheng 1763bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 1764bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1, 1765bcd53741SArne Jansen const void *dev_info2) 1766bcd53741SArne Jansen { 1767bcd53741SArne Jansen if (((struct btrfs_device_info *)dev_info1)->max_avail > 1768bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1769bcd53741SArne Jansen return -1; 1770bcd53741SArne Jansen else if (((struct btrfs_device_info *)dev_info1)->max_avail < 1771bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1772bcd53741SArne Jansen return 1; 1773bcd53741SArne Jansen else 1774bcd53741SArne Jansen return 0; 1775bcd53741SArne Jansen } 1776bcd53741SArne Jansen 1777bcd53741SArne Jansen /* 1778bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 1779bcd53741SArne Jansen * is stored.(Descending Sort) 1780bcd53741SArne Jansen */ 1781bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 1782bcd53741SArne Jansen struct btrfs_device_info *devices, 1783bcd53741SArne Jansen size_t nr_devices) 1784bcd53741SArne Jansen { 1785bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 1786bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 1787bcd53741SArne Jansen } 1788bcd53741SArne Jansen 17896d07bcecSMiao Xie /* 17906d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 17916d07bcecSMiao Xie * file data. 17926d07bcecSMiao Xie */ 17936d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes) 17946d07bcecSMiao Xie { 17956d07bcecSMiao Xie struct btrfs_fs_info *fs_info = root->fs_info; 17966d07bcecSMiao Xie struct btrfs_device_info *devices_info; 17976d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 17986d07bcecSMiao Xie struct btrfs_device *device; 17996d07bcecSMiao Xie u64 skip_space; 18006d07bcecSMiao Xie u64 type; 18016d07bcecSMiao Xie u64 avail_space; 18026d07bcecSMiao Xie u64 used_space; 18036d07bcecSMiao Xie u64 min_stripe_size; 180439fb26c3SMiao Xie int min_stripes = 1, num_stripes = 1; 18056d07bcecSMiao Xie int i = 0, nr_devices; 18066d07bcecSMiao Xie int ret; 18076d07bcecSMiao Xie 18087e33fd99SJosef Bacik /* 18097e33fd99SJosef Bacik * We aren't under the device list lock, so this is racey-ish, but good 18107e33fd99SJosef Bacik * enough for our purposes. 18117e33fd99SJosef Bacik */ 1812b772a86eSLi Zefan nr_devices = fs_info->fs_devices->open_devices; 18137e33fd99SJosef Bacik if (!nr_devices) { 18147e33fd99SJosef Bacik smp_mb(); 18157e33fd99SJosef Bacik nr_devices = fs_info->fs_devices->open_devices; 18167e33fd99SJosef Bacik ASSERT(nr_devices); 18177e33fd99SJosef Bacik if (!nr_devices) { 18187e33fd99SJosef Bacik *free_bytes = 0; 18197e33fd99SJosef Bacik return 0; 18207e33fd99SJosef Bacik } 18217e33fd99SJosef Bacik } 18226d07bcecSMiao Xie 1823d9b0d9baSDulshani Gunawardhana devices_info = kmalloc_array(nr_devices, sizeof(*devices_info), 18246d07bcecSMiao Xie GFP_NOFS); 18256d07bcecSMiao Xie if (!devices_info) 18266d07bcecSMiao Xie return -ENOMEM; 18276d07bcecSMiao Xie 18286d07bcecSMiao Xie /* calc min stripe number for data space alloction */ 18296d07bcecSMiao Xie type = btrfs_get_alloc_profile(root, 1); 183039fb26c3SMiao Xie if (type & BTRFS_BLOCK_GROUP_RAID0) { 18316d07bcecSMiao Xie min_stripes = 2; 183239fb26c3SMiao Xie num_stripes = nr_devices; 183339fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID1) { 18346d07bcecSMiao Xie min_stripes = 2; 183539fb26c3SMiao Xie num_stripes = 2; 183639fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID10) { 18376d07bcecSMiao Xie min_stripes = 4; 183839fb26c3SMiao Xie num_stripes = 4; 183939fb26c3SMiao Xie } 18406d07bcecSMiao Xie 18416d07bcecSMiao Xie if (type & BTRFS_BLOCK_GROUP_DUP) 18426d07bcecSMiao Xie min_stripe_size = 2 * BTRFS_STRIPE_LEN; 18436d07bcecSMiao Xie else 18446d07bcecSMiao Xie min_stripe_size = BTRFS_STRIPE_LEN; 18456d07bcecSMiao Xie 18467e33fd99SJosef Bacik if (fs_info->alloc_start) 18477e33fd99SJosef Bacik mutex_lock(&fs_devices->device_list_mutex); 18487e33fd99SJosef Bacik rcu_read_lock(); 18497e33fd99SJosef Bacik list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 185063a212abSStefan Behrens if (!device->in_fs_metadata || !device->bdev || 185163a212abSStefan Behrens device->is_tgtdev_for_dev_replace) 18526d07bcecSMiao Xie continue; 18536d07bcecSMiao Xie 18547e33fd99SJosef Bacik if (i >= nr_devices) 18557e33fd99SJosef Bacik break; 18567e33fd99SJosef Bacik 18576d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 18586d07bcecSMiao Xie 18596d07bcecSMiao Xie /* align with stripe_len */ 1860f8c269d7SDavid Sterba avail_space = div_u64(avail_space, BTRFS_STRIPE_LEN); 18616d07bcecSMiao Xie avail_space *= BTRFS_STRIPE_LEN; 18626d07bcecSMiao Xie 18636d07bcecSMiao Xie /* 18646d07bcecSMiao Xie * In order to avoid overwritting the superblock on the drive, 18656d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 18666d07bcecSMiao Xie * allocation. 18676d07bcecSMiao Xie */ 1868*ee22184bSByongho Lee skip_space = SZ_1M; 18696d07bcecSMiao Xie 18706d07bcecSMiao Xie /* user can set the offset in fs_info->alloc_start. */ 18717e33fd99SJosef Bacik if (fs_info->alloc_start && 18727e33fd99SJosef Bacik fs_info->alloc_start + BTRFS_STRIPE_LEN <= 18737e33fd99SJosef Bacik device->total_bytes) { 18747e33fd99SJosef Bacik rcu_read_unlock(); 18756d07bcecSMiao Xie skip_space = max(fs_info->alloc_start, skip_space); 18766d07bcecSMiao Xie 18776d07bcecSMiao Xie /* 18787e33fd99SJosef Bacik * btrfs can not use the free space in 18797e33fd99SJosef Bacik * [0, skip_space - 1], we must subtract it from the 18807e33fd99SJosef Bacik * total. In order to implement it, we account the used 18817e33fd99SJosef Bacik * space in this range first. 18826d07bcecSMiao Xie */ 18837e33fd99SJosef Bacik ret = btrfs_account_dev_extents_size(device, 0, 18847e33fd99SJosef Bacik skip_space - 1, 18856d07bcecSMiao Xie &used_space); 18866d07bcecSMiao Xie if (ret) { 18876d07bcecSMiao Xie kfree(devices_info); 18887e33fd99SJosef Bacik mutex_unlock(&fs_devices->device_list_mutex); 18896d07bcecSMiao Xie return ret; 18906d07bcecSMiao Xie } 18916d07bcecSMiao Xie 18927e33fd99SJosef Bacik rcu_read_lock(); 18937e33fd99SJosef Bacik 18946d07bcecSMiao Xie /* calc the free space in [0, skip_space - 1] */ 18956d07bcecSMiao Xie skip_space -= used_space; 18967e33fd99SJosef Bacik } 18976d07bcecSMiao Xie 18986d07bcecSMiao Xie /* 18996d07bcecSMiao Xie * we can use the free space in [0, skip_space - 1], subtract 19006d07bcecSMiao Xie * it from the total. 19016d07bcecSMiao Xie */ 19026d07bcecSMiao Xie if (avail_space && avail_space >= skip_space) 19036d07bcecSMiao Xie avail_space -= skip_space; 19046d07bcecSMiao Xie else 19056d07bcecSMiao Xie avail_space = 0; 19066d07bcecSMiao Xie 19076d07bcecSMiao Xie if (avail_space < min_stripe_size) 19086d07bcecSMiao Xie continue; 19096d07bcecSMiao Xie 19106d07bcecSMiao Xie devices_info[i].dev = device; 19116d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 19126d07bcecSMiao Xie 19136d07bcecSMiao Xie i++; 19146d07bcecSMiao Xie } 19157e33fd99SJosef Bacik rcu_read_unlock(); 19167e33fd99SJosef Bacik if (fs_info->alloc_start) 19177e33fd99SJosef Bacik mutex_unlock(&fs_devices->device_list_mutex); 19186d07bcecSMiao Xie 19196d07bcecSMiao Xie nr_devices = i; 19206d07bcecSMiao Xie 19216d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 19226d07bcecSMiao Xie 19236d07bcecSMiao Xie i = nr_devices - 1; 19246d07bcecSMiao Xie avail_space = 0; 19256d07bcecSMiao Xie while (nr_devices >= min_stripes) { 192639fb26c3SMiao Xie if (num_stripes > nr_devices) 192739fb26c3SMiao Xie num_stripes = nr_devices; 192839fb26c3SMiao Xie 19296d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 19306d07bcecSMiao Xie int j; 19316d07bcecSMiao Xie u64 alloc_size; 19326d07bcecSMiao Xie 193339fb26c3SMiao Xie avail_space += devices_info[i].max_avail * num_stripes; 19346d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 193539fb26c3SMiao Xie for (j = i + 1 - num_stripes; j <= i; j++) 19366d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 19376d07bcecSMiao Xie } 19386d07bcecSMiao Xie i--; 19396d07bcecSMiao Xie nr_devices--; 19406d07bcecSMiao Xie } 19416d07bcecSMiao Xie 19426d07bcecSMiao Xie kfree(devices_info); 19436d07bcecSMiao Xie *free_bytes = avail_space; 19446d07bcecSMiao Xie return 0; 19456d07bcecSMiao Xie } 19466d07bcecSMiao Xie 1947ba7b6e62SDavid Sterba /* 1948ba7b6e62SDavid Sterba * Calculate numbers for 'df', pessimistic in case of mixed raid profiles. 1949ba7b6e62SDavid Sterba * 1950ba7b6e62SDavid Sterba * If there's a redundant raid level at DATA block groups, use the respective 1951ba7b6e62SDavid Sterba * multiplier to scale the sizes. 1952ba7b6e62SDavid Sterba * 1953ba7b6e62SDavid Sterba * Unused device space usage is based on simulating the chunk allocator 1954ba7b6e62SDavid Sterba * algorithm that respects the device sizes, order of allocations and the 1955ba7b6e62SDavid Sterba * 'alloc_start' value, this is a close approximation of the actual use but 1956ba7b6e62SDavid Sterba * there are other factors that may change the result (like a new metadata 1957ba7b6e62SDavid Sterba * chunk). 1958ba7b6e62SDavid Sterba * 1959ba7b6e62SDavid Sterba * FIXME: not accurate for mixed block groups, total and free/used are ok, 1960ba7b6e62SDavid Sterba * available appears slightly larger. 1961ba7b6e62SDavid Sterba */ 19628fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 19638fd17795SChris Mason { 1964815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 1965815745cfSAl Viro struct btrfs_super_block *disk_super = fs_info->super_copy; 1966815745cfSAl Viro struct list_head *head = &fs_info->space_info; 1967bd4d1088SJosef Bacik struct btrfs_space_info *found; 1968bd4d1088SJosef Bacik u64 total_used = 0; 19696d07bcecSMiao Xie u64 total_free_data = 0; 1970db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 1971815745cfSAl Viro __be32 *fsid = (__be32 *)fs_info->fsid; 1972ba7b6e62SDavid Sterba unsigned factor = 1; 1973ba7b6e62SDavid Sterba struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 19746d07bcecSMiao Xie int ret; 19758fd17795SChris Mason 197615484377SMiao Xie /* 197715484377SMiao Xie * holding chunk_muext to avoid allocating new chunks, holding 197815484377SMiao Xie * device_list_mutex to avoid the device being removed 197915484377SMiao Xie */ 1980bd4d1088SJosef Bacik rcu_read_lock(); 198189a55897SJosef Bacik list_for_each_entry_rcu(found, head, list) { 19826d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 1983ba7b6e62SDavid Sterba int i; 1984ba7b6e62SDavid Sterba 19856d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 19866d07bcecSMiao Xie total_free_data -= 19876d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 1988ba7b6e62SDavid Sterba 1989ba7b6e62SDavid Sterba for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { 1990ba7b6e62SDavid Sterba if (!list_empty(&found->block_groups[i])) { 1991ba7b6e62SDavid Sterba switch (i) { 1992ba7b6e62SDavid Sterba case BTRFS_RAID_DUP: 1993ba7b6e62SDavid Sterba case BTRFS_RAID_RAID1: 1994ba7b6e62SDavid Sterba case BTRFS_RAID_RAID10: 1995ba7b6e62SDavid Sterba factor = 2; 1996ba7b6e62SDavid Sterba } 1997ba7b6e62SDavid Sterba } 1998ba7b6e62SDavid Sterba } 19996d07bcecSMiao Xie } 20006d07bcecSMiao Xie 2001b742bb82SYan, Zheng total_used += found->disk_used; 200289a55897SJosef Bacik } 2003ba7b6e62SDavid Sterba 2004bd4d1088SJosef Bacik rcu_read_unlock(); 2005bd4d1088SJosef Bacik 2006ba7b6e62SDavid Sterba buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor); 2007ba7b6e62SDavid Sterba buf->f_blocks >>= bits; 2008ba7b6e62SDavid Sterba buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits); 2009ba7b6e62SDavid Sterba 2010ba7b6e62SDavid Sterba /* Account global block reserve as used, it's in logical size already */ 2011ba7b6e62SDavid Sterba spin_lock(&block_rsv->lock); 2012ba7b6e62SDavid Sterba buf->f_bfree -= block_rsv->size >> bits; 2013ba7b6e62SDavid Sterba spin_unlock(&block_rsv->lock); 2014ba7b6e62SDavid Sterba 20150d95c1beSDavid Sterba buf->f_bavail = div_u64(total_free_data, factor); 2016815745cfSAl Viro ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data); 20177e33fd99SJosef Bacik if (ret) 20186d07bcecSMiao Xie return ret; 2019ba7b6e62SDavid Sterba buf->f_bavail += div_u64(total_free_data, factor); 20206d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 2021d397712bSChris Mason 2022ba7b6e62SDavid Sterba buf->f_type = BTRFS_SUPER_MAGIC; 2023ba7b6e62SDavid Sterba buf->f_bsize = dentry->d_sb->s_blocksize; 2024ba7b6e62SDavid Sterba buf->f_namelen = BTRFS_NAME_LEN; 2025ba7b6e62SDavid Sterba 20269d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 20279d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 20289d03632eSDavid Woodhouse on a big-endian or little-endian host */ 20299d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 20309d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 203132d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 20322b0143b5SDavid Howells buf->f_fsid.val[0] ^= BTRFS_I(d_inode(dentry))->root->objectid >> 32; 20332b0143b5SDavid Howells buf->f_fsid.val[1] ^= BTRFS_I(d_inode(dentry))->root->objectid; 203432d48fa1SDavid Woodhouse 20358fd17795SChris Mason return 0; 20368fd17795SChris Mason } 2037b5133862SChris Mason 2038aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb) 2039aea52e19SAl Viro { 2040815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2041aea52e19SAl Viro kill_anon_super(sb); 2042aea52e19SAl Viro free_fs_info(fs_info); 2043aea52e19SAl Viro } 2044aea52e19SAl Viro 20452e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 20462e635a27SChris Mason .owner = THIS_MODULE, 20472e635a27SChris Mason .name = "btrfs", 2048061dbc6bSAl Viro .mount = btrfs_mount, 2049aea52e19SAl Viro .kill_sb = btrfs_kill_super, 2050f667aef6SQu Wenruo .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA, 20512e635a27SChris Mason }; 20527f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs"); 2053a9218f6bSChris Mason 2054d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file) 2055d8620958STom Van Braeckel { 2056d8620958STom Van Braeckel /* 2057d8620958STom Van Braeckel * The control file's private_data is used to hold the 2058d8620958STom Van Braeckel * transaction when it is started and is used to keep 2059d8620958STom Van Braeckel * track of whether a transaction is already in progress. 2060d8620958STom Van Braeckel */ 2061d8620958STom Van Braeckel file->private_data = NULL; 2062d8620958STom Van Braeckel return 0; 2063d8620958STom Van Braeckel } 2064d8620958STom Van Braeckel 2065d352ac68SChris Mason /* 2066d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 2067d352ac68SChris Mason */ 20688a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 20698a4b83ccSChris Mason unsigned long arg) 20708a4b83ccSChris Mason { 20718a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 20728a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 2073c071fcfdSChris Mason int ret = -ENOTTY; 20748a4b83ccSChris Mason 2075e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2076e441d54dSChris Mason return -EPERM; 2077e441d54dSChris Mason 2078dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 2079dae7b665SLi Zefan if (IS_ERR(vol)) 2080dae7b665SLi Zefan return PTR_ERR(vol); 2081c071fcfdSChris Mason 20828a4b83ccSChris Mason switch (cmd) { 20838a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 208497288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 20858a4b83ccSChris Mason &btrfs_fs_type, &fs_devices); 20868a4b83ccSChris Mason break; 208702db0844SJosef Bacik case BTRFS_IOC_DEVICES_READY: 208802db0844SJosef Bacik ret = btrfs_scan_one_device(vol->name, FMODE_READ, 208902db0844SJosef Bacik &btrfs_fs_type, &fs_devices); 209002db0844SJosef Bacik if (ret) 209102db0844SJosef Bacik break; 209202db0844SJosef Bacik ret = !(fs_devices->num_devices == fs_devices->total_devices); 209302db0844SJosef Bacik break; 20948a4b83ccSChris Mason } 2095dae7b665SLi Zefan 20968a4b83ccSChris Mason kfree(vol); 2097f819d837SLinda Knippers return ret; 20988a4b83ccSChris Mason } 20998a4b83ccSChris Mason 21000176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 2101ed0dab6bSYan { 2102354aa0fbSMiao Xie struct btrfs_trans_handle *trans; 2103354aa0fbSMiao Xie struct btrfs_root *root = btrfs_sb(sb)->tree_root; 2104354aa0fbSMiao Xie 2105d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 2106354aa0fbSMiao Xie if (IS_ERR(trans)) { 2107354aa0fbSMiao Xie /* no transaction, don't bother */ 2108354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 21090176260fSLinus Torvalds return 0; 2110354aa0fbSMiao Xie return PTR_ERR(trans); 2111354aa0fbSMiao Xie } 2112354aa0fbSMiao Xie return btrfs_commit_transaction(trans, root); 2113ed0dab6bSYan } 2114ed0dab6bSYan 21159c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 21169c5085c1SJosef Bacik { 21179c5085c1SJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 21189c5085c1SJosef Bacik struct btrfs_fs_devices *cur_devices; 21199c5085c1SJosef Bacik struct btrfs_device *dev, *first_dev = NULL; 21209c5085c1SJosef Bacik struct list_head *head; 21219c5085c1SJosef Bacik struct rcu_string *name; 21229c5085c1SJosef Bacik 21239c5085c1SJosef Bacik mutex_lock(&fs_info->fs_devices->device_list_mutex); 21249c5085c1SJosef Bacik cur_devices = fs_info->fs_devices; 21259c5085c1SJosef Bacik while (cur_devices) { 21269c5085c1SJosef Bacik head = &cur_devices->devices; 21279c5085c1SJosef Bacik list_for_each_entry(dev, head, dev_list) { 2128aa9ddcd4SJosef Bacik if (dev->missing) 2129aa9ddcd4SJosef Bacik continue; 21300aeb8a6eSAnand Jain if (!dev->name) 21310aeb8a6eSAnand Jain continue; 21329c5085c1SJosef Bacik if (!first_dev || dev->devid < first_dev->devid) 21339c5085c1SJosef Bacik first_dev = dev; 21349c5085c1SJosef Bacik } 21359c5085c1SJosef Bacik cur_devices = cur_devices->seed; 21369c5085c1SJosef Bacik } 21379c5085c1SJosef Bacik 21389c5085c1SJosef Bacik if (first_dev) { 21399c5085c1SJosef Bacik rcu_read_lock(); 21409c5085c1SJosef Bacik name = rcu_dereference(first_dev->name); 21419c5085c1SJosef Bacik seq_escape(m, name->str, " \t\n\\"); 21429c5085c1SJosef Bacik rcu_read_unlock(); 21439c5085c1SJosef Bacik } else { 21449c5085c1SJosef Bacik WARN_ON(1); 21459c5085c1SJosef Bacik } 21469c5085c1SJosef Bacik mutex_unlock(&fs_info->fs_devices->device_list_mutex); 21479c5085c1SJosef Bacik return 0; 21489c5085c1SJosef Bacik } 21499c5085c1SJosef Bacik 2150b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 215176dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 2152bd555975SAl Viro .evict_inode = btrfs_evict_inode, 2153e20d96d6SChris Mason .put_super = btrfs_put_super, 2154d5719762SChris Mason .sync_fs = btrfs_sync_fs, 2155a9572a15SEric Paris .show_options = btrfs_show_options, 21569c5085c1SJosef Bacik .show_devname = btrfs_show_devname, 21574730a4bcSChris Mason .write_inode = btrfs_write_inode, 21582c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 21592c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 21608fd17795SChris Mason .statfs = btrfs_statfs, 2161c146afadSYan Zheng .remount_fs = btrfs_remount, 21620176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 2163e20d96d6SChris Mason }; 2164a9218f6bSChris Mason 2165a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 2166d8620958STom Van Braeckel .open = btrfs_control_open, 2167a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 2168a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 2169a9218f6bSChris Mason .owner = THIS_MODULE, 21706038f373SArnd Bergmann .llseek = noop_llseek, 2171a9218f6bSChris Mason }; 2172a9218f6bSChris Mason 2173a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 2174578454ffSKay Sievers .minor = BTRFS_MINOR, 2175a9218f6bSChris Mason .name = "btrfs-control", 2176a9218f6bSChris Mason .fops = &btrfs_ctl_fops 2177a9218f6bSChris Mason }; 2178a9218f6bSChris Mason 2179578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 2180578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 2181578454ffSKay Sievers 2182a9218f6bSChris Mason static int btrfs_interface_init(void) 2183a9218f6bSChris Mason { 2184a9218f6bSChris Mason return misc_register(&btrfs_misc); 2185a9218f6bSChris Mason } 2186a9218f6bSChris Mason 2187b2950863SChristoph Hellwig static void btrfs_interface_exit(void) 2188a9218f6bSChris Mason { 2189f368ed60SGreg Kroah-Hartman misc_deregister(&btrfs_misc); 2190a9218f6bSChris Mason } 2191a9218f6bSChris Mason 219285965600SDavid Sterba static void btrfs_print_info(void) 219385965600SDavid Sterba { 219485965600SDavid Sterba printk(KERN_INFO "Btrfs loaded" 219585965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 219685965600SDavid Sterba ", debug=on" 219785965600SDavid Sterba #endif 219879556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT 219979556c3dSStefan Behrens ", assert=on" 220079556c3dSStefan Behrens #endif 220185965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 220285965600SDavid Sterba ", integrity-checker=on" 220385965600SDavid Sterba #endif 220485965600SDavid Sterba "\n"); 220585965600SDavid Sterba } 220685965600SDavid Sterba 2207dc11dd5dSJosef Bacik static int btrfs_run_sanity_tests(void) 2208dc11dd5dSJosef Bacik { 220906ea65a3SJosef Bacik int ret; 221006ea65a3SJosef Bacik 2211294e30feSJosef Bacik ret = btrfs_init_test_fs(); 221206ea65a3SJosef Bacik if (ret) 221306ea65a3SJosef Bacik return ret; 2214294e30feSJosef Bacik 2215294e30feSJosef Bacik ret = btrfs_test_free_space_cache(); 2216294e30feSJosef Bacik if (ret) 2217294e30feSJosef Bacik goto out; 2218294e30feSJosef Bacik ret = btrfs_test_extent_buffer_operations(); 2219294e30feSJosef Bacik if (ret) 2220294e30feSJosef Bacik goto out; 2221294e30feSJosef Bacik ret = btrfs_test_extent_io(); 2222aaedb55bSJosef Bacik if (ret) 2223aaedb55bSJosef Bacik goto out; 2224aaedb55bSJosef Bacik ret = btrfs_test_inodes(); 2225faa2dbf0SJosef Bacik if (ret) 2226faa2dbf0SJosef Bacik goto out; 2227faa2dbf0SJosef Bacik ret = btrfs_test_qgroups(); 2228294e30feSJosef Bacik out: 2229294e30feSJosef Bacik btrfs_destroy_test_fs(); 2230294e30feSJosef Bacik return ret; 2231dc11dd5dSJosef Bacik } 2232dc11dd5dSJosef Bacik 22332e635a27SChris Mason static int __init init_btrfs_fs(void) 22342e635a27SChris Mason { 22352c90e5d6SChris Mason int err; 223658176a96SJosef Bacik 223714a958e6SFilipe David Borba Manana err = btrfs_hash_init(); 223814a958e6SFilipe David Borba Manana if (err) 223914a958e6SFilipe David Borba Manana return err; 224014a958e6SFilipe David Borba Manana 224163541927SFilipe David Borba Manana btrfs_props_init(); 224263541927SFilipe David Borba Manana 224358176a96SJosef Bacik err = btrfs_init_sysfs(); 224458176a96SJosef Bacik if (err) 224514a958e6SFilipe David Borba Manana goto free_hash; 224658176a96SJosef Bacik 2247143bede5SJeff Mahoney btrfs_init_compress(); 2248d1310b2eSChris Mason 2249261507a0SLi Zefan err = btrfs_init_cachep(); 2250261507a0SLi Zefan if (err) 2251261507a0SLi Zefan goto free_compress; 2252261507a0SLi Zefan 2253d1310b2eSChris Mason err = extent_io_init(); 22542f4cbe64SWyatt Banks if (err) 22552f4cbe64SWyatt Banks goto free_cachep; 22562f4cbe64SWyatt Banks 2257d1310b2eSChris Mason err = extent_map_init(); 2258d1310b2eSChris Mason if (err) 2259d1310b2eSChris Mason goto free_extent_io; 2260d1310b2eSChris Mason 22616352b91dSMiao Xie err = ordered_data_init(); 22622f4cbe64SWyatt Banks if (err) 22632f4cbe64SWyatt Banks goto free_extent_map; 2264c8b97818SChris Mason 22656352b91dSMiao Xie err = btrfs_delayed_inode_init(); 22666352b91dSMiao Xie if (err) 22676352b91dSMiao Xie goto free_ordered_data; 22686352b91dSMiao Xie 22699247f317SMiao Xie err = btrfs_auto_defrag_init(); 227016cdcec7SMiao Xie if (err) 227116cdcec7SMiao Xie goto free_delayed_inode; 227216cdcec7SMiao Xie 227378a6184aSMiao Xie err = btrfs_delayed_ref_init(); 22749247f317SMiao Xie if (err) 22759247f317SMiao Xie goto free_auto_defrag; 22769247f317SMiao Xie 2277b9e9a6cbSWang Shilong err = btrfs_prelim_ref_init(); 2278b9e9a6cbSWang Shilong if (err) 2279af13b492SDavid Sterba goto free_delayed_ref; 2280b9e9a6cbSWang Shilong 228197eb6b69SDavid Sterba err = btrfs_end_io_wq_init(); 228278a6184aSMiao Xie if (err) 2283af13b492SDavid Sterba goto free_prelim_ref; 228478a6184aSMiao Xie 228597eb6b69SDavid Sterba err = btrfs_interface_init(); 228697eb6b69SDavid Sterba if (err) 228797eb6b69SDavid Sterba goto free_end_io_wq; 228897eb6b69SDavid Sterba 2289e565d4b9SJan Schmidt btrfs_init_lockdep(); 2290e565d4b9SJan Schmidt 229185965600SDavid Sterba btrfs_print_info(); 2292dc11dd5dSJosef Bacik 2293dc11dd5dSJosef Bacik err = btrfs_run_sanity_tests(); 2294dc11dd5dSJosef Bacik if (err) 2295dc11dd5dSJosef Bacik goto unregister_ioctl; 2296dc11dd5dSJosef Bacik 2297dc11dd5dSJosef Bacik err = register_filesystem(&btrfs_fs_type); 2298dc11dd5dSJosef Bacik if (err) 2299dc11dd5dSJosef Bacik goto unregister_ioctl; 230074255aa0SJosef Bacik 23012f4cbe64SWyatt Banks return 0; 23022f4cbe64SWyatt Banks 2303a9218f6bSChris Mason unregister_ioctl: 2304a9218f6bSChris Mason btrfs_interface_exit(); 230597eb6b69SDavid Sterba free_end_io_wq: 230697eb6b69SDavid Sterba btrfs_end_io_wq_exit(); 2307b9e9a6cbSWang Shilong free_prelim_ref: 2308b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 230978a6184aSMiao Xie free_delayed_ref: 231078a6184aSMiao Xie btrfs_delayed_ref_exit(); 23119247f317SMiao Xie free_auto_defrag: 23129247f317SMiao Xie btrfs_auto_defrag_exit(); 231316cdcec7SMiao Xie free_delayed_inode: 231416cdcec7SMiao Xie btrfs_delayed_inode_exit(); 23156352b91dSMiao Xie free_ordered_data: 23166352b91dSMiao Xie ordered_data_exit(); 23172f4cbe64SWyatt Banks free_extent_map: 23182f4cbe64SWyatt Banks extent_map_exit(); 2319d1310b2eSChris Mason free_extent_io: 2320d1310b2eSChris Mason extent_io_exit(); 23212f4cbe64SWyatt Banks free_cachep: 23222f4cbe64SWyatt Banks btrfs_destroy_cachep(); 2323261507a0SLi Zefan free_compress: 2324261507a0SLi Zefan btrfs_exit_compress(); 23252f4cbe64SWyatt Banks btrfs_exit_sysfs(); 232614a958e6SFilipe David Borba Manana free_hash: 232714a958e6SFilipe David Borba Manana btrfs_hash_exit(); 23282c90e5d6SChris Mason return err; 23292e635a27SChris Mason } 23302e635a27SChris Mason 23312e635a27SChris Mason static void __exit exit_btrfs_fs(void) 23322e635a27SChris Mason { 233339279cc3SChris Mason btrfs_destroy_cachep(); 233478a6184aSMiao Xie btrfs_delayed_ref_exit(); 23359247f317SMiao Xie btrfs_auto_defrag_exit(); 233616cdcec7SMiao Xie btrfs_delayed_inode_exit(); 2337b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 23386352b91dSMiao Xie ordered_data_exit(); 2339a52d9a80SChris Mason extent_map_exit(); 2340d1310b2eSChris Mason extent_io_exit(); 2341a9218f6bSChris Mason btrfs_interface_exit(); 23425ed5f588SJosef Bacik btrfs_end_io_wq_exit(); 23432e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 234458176a96SJosef Bacik btrfs_exit_sysfs(); 23458a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 2346261507a0SLi Zefan btrfs_exit_compress(); 234714a958e6SFilipe David Borba Manana btrfs_hash_exit(); 23482e635a27SChris Mason } 23492e635a27SChris Mason 235060efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs); 23512e635a27SChris Mason module_exit(exit_btrfs_fs) 23522e635a27SChris Mason 23532e635a27SChris Mason MODULE_LICENSE("GPL"); 2354