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 100acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */ 101acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info) 102acce952bSliubo { 103acce952bSliubo struct super_block *sb = fs_info->sb; 104acce952bSliubo 105acce952bSliubo if (sb->s_flags & MS_RDONLY) 106acce952bSliubo return; 107acce952bSliubo 10887533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 109acce952bSliubo sb->s_flags |= MS_RDONLY; 110c2cf52ebSSimon Kirby btrfs_info(fs_info, "forced readonly"); 1111acd6831SStefan Behrens /* 1121acd6831SStefan Behrens * Note that a running device replace operation is not 1131acd6831SStefan Behrens * canceled here although there is no way to update 1141acd6831SStefan Behrens * the progress. It would add the risk of a deadlock, 11501327610SNicholas D Steeves * therefore the canceling is omitted. The only penalty 1161acd6831SStefan Behrens * is that some I/O remains active until the procedure 1171acd6831SStefan Behrens * completes. The next time when the filesystem is 1181acd6831SStefan Behrens * mounted writeable again, the device replace 1191acd6831SStefan Behrens * operation continues. 1201acd6831SStefan Behrens */ 121acce952bSliubo } 122acce952bSliubo } 123acce952bSliubo 124acce952bSliubo /* 12534d97007SAnand Jain * __btrfs_handle_fs_error decodes expected errors from the caller and 126acce952bSliubo * invokes the approciate error response. 127acce952bSliubo */ 128c0d19e2bSDavid Sterba __cold 12934d97007SAnand Jain void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function, 1304da35113SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 131acce952bSliubo { 132acce952bSliubo struct super_block *sb = fs_info->sb; 13357d816a1SAnand Jain #ifdef CONFIG_PRINTK 134acce952bSliubo const char *errstr; 13557d816a1SAnand Jain #endif 136acce952bSliubo 137acce952bSliubo /* 138acce952bSliubo * Special case: if the error is EROFS, and we're already 139acce952bSliubo * under MS_RDONLY, then it is safe here. 140acce952bSliubo */ 141acce952bSliubo if (errno == -EROFS && (sb->s_flags & MS_RDONLY)) 142acce952bSliubo return; 143acce952bSliubo 14457d816a1SAnand Jain #ifdef CONFIG_PRINTK 14508748810SDavid Sterba errstr = btrfs_decode_error(errno); 1464da35113SJeff Mahoney if (fmt) { 14737252a66SEric Sandeen struct va_format vaf; 14837252a66SEric Sandeen va_list args; 14937252a66SEric Sandeen 15037252a66SEric Sandeen va_start(args, fmt); 15137252a66SEric Sandeen vaf.fmt = fmt; 15237252a66SEric Sandeen vaf.va = &args; 1534da35113SJeff Mahoney 15462e85577SJeff Mahoney pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n", 15508748810SDavid Sterba sb->s_id, function, line, errno, errstr, &vaf); 15637252a66SEric Sandeen va_end(args); 1574da35113SJeff Mahoney } else { 15862e85577SJeff Mahoney pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s\n", 15908748810SDavid Sterba sb->s_id, function, line, errno, errstr); 1604da35113SJeff Mahoney } 16157d816a1SAnand Jain #endif 162acce952bSliubo 1630713d90cSAnand Jain /* 1640713d90cSAnand Jain * Today we only save the error info to memory. Long term we'll 1650713d90cSAnand Jain * also send it down to the disk 1660713d90cSAnand Jain */ 1670713d90cSAnand Jain set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 1680713d90cSAnand Jain 1694da35113SJeff Mahoney /* Don't go through full error handling during mount */ 170cf79ffb5SJosef Bacik if (sb->s_flags & MS_BORN) 171acce952bSliubo btrfs_handle_error(fs_info); 172acce952bSliubo } 1734da35113SJeff Mahoney 17457d816a1SAnand Jain #ifdef CONFIG_PRINTK 175533574c6SJoe Perches static const char * const logtypes[] = { 1764da35113SJeff Mahoney "emergency", 1774da35113SJeff Mahoney "alert", 1784da35113SJeff Mahoney "critical", 1794da35113SJeff Mahoney "error", 1804da35113SJeff Mahoney "warning", 1814da35113SJeff Mahoney "notice", 1824da35113SJeff Mahoney "info", 1834da35113SJeff Mahoney "debug", 1844da35113SJeff Mahoney }; 1854da35113SJeff Mahoney 18635f4e5e6SNikolay Borisov 18735f4e5e6SNikolay Borisov /* 18835f4e5e6SNikolay Borisov * Use one ratelimit state per log level so that a flood of less important 18935f4e5e6SNikolay Borisov * messages doesn't cause more important ones to be dropped. 19035f4e5e6SNikolay Borisov */ 19135f4e5e6SNikolay Borisov static struct ratelimit_state printk_limits[] = { 19235f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100), 19335f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100), 19435f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100), 19535f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100), 19635f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100), 19735f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100), 19835f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100), 19935f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100), 20035f4e5e6SNikolay Borisov }; 20135f4e5e6SNikolay Borisov 202c2cf52ebSSimon Kirby void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...) 2034da35113SJeff Mahoney { 2044da35113SJeff Mahoney struct super_block *sb = fs_info->sb; 2054da35113SJeff Mahoney char lvl[4]; 2064da35113SJeff Mahoney struct va_format vaf; 2074da35113SJeff Mahoney va_list args; 2084da35113SJeff Mahoney const char *type = logtypes[4]; 209533574c6SJoe Perches int kern_level; 21035f4e5e6SNikolay Borisov struct ratelimit_state *ratelimit; 2114da35113SJeff Mahoney 2124da35113SJeff Mahoney va_start(args, fmt); 2134da35113SJeff Mahoney 214533574c6SJoe Perches kern_level = printk_get_level(fmt); 215533574c6SJoe Perches if (kern_level) { 216533574c6SJoe Perches size_t size = printk_skip_level(fmt) - fmt; 217533574c6SJoe Perches memcpy(lvl, fmt, size); 218533574c6SJoe Perches lvl[size] = '\0'; 219533574c6SJoe Perches fmt += size; 220533574c6SJoe Perches type = logtypes[kern_level - '0']; 22135f4e5e6SNikolay Borisov ratelimit = &printk_limits[kern_level - '0']; 22235f4e5e6SNikolay Borisov } else { 2234da35113SJeff Mahoney *lvl = '\0'; 22435f4e5e6SNikolay Borisov /* Default to debug output */ 22535f4e5e6SNikolay Borisov ratelimit = &printk_limits[7]; 22635f4e5e6SNikolay Borisov } 2274da35113SJeff Mahoney 2284da35113SJeff Mahoney vaf.fmt = fmt; 2294da35113SJeff Mahoney vaf.va = &args; 230533574c6SJoe Perches 23135f4e5e6SNikolay Borisov if (__ratelimit(ratelimit)) 232c2cf52ebSSimon Kirby printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf); 233533574c6SJoe Perches 234533574c6SJoe Perches va_end(args); 2354da35113SJeff Mahoney } 236533574c6SJoe Perches #endif 237533574c6SJoe Perches 2388c342930SJeff Mahoney /* 23949b25e05SJeff Mahoney * We only mark the transaction aborted and then set the file system read-only. 24049b25e05SJeff Mahoney * This will prevent new transactions from starting or trying to join this 24149b25e05SJeff Mahoney * one. 24249b25e05SJeff Mahoney * 24349b25e05SJeff Mahoney * This means that error recovery at the call site is limited to freeing 24449b25e05SJeff Mahoney * any local memory allocations and passing the error code up without 24549b25e05SJeff Mahoney * further cleanup. The transaction should complete as it normally would 24649b25e05SJeff Mahoney * in the call path but will return -EIO. 24749b25e05SJeff Mahoney * 24849b25e05SJeff Mahoney * We'll complete the cleanup in btrfs_end_transaction and 24949b25e05SJeff Mahoney * btrfs_commit_transaction. 25049b25e05SJeff Mahoney */ 251c0d19e2bSDavid Sterba __cold 25249b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans, 25366642832SJeff Mahoney const char *function, 25449b25e05SJeff Mahoney unsigned int line, int errno) 25549b25e05SJeff Mahoney { 25666642832SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 25766642832SJeff Mahoney 25849b25e05SJeff Mahoney trans->aborted = errno; 25949b25e05SJeff Mahoney /* Nothing used. The other threads that have joined this 26049b25e05SJeff Mahoney * transaction may be able to continue. */ 26164c12921SJeff Mahoney if (!trans->dirty && list_empty(&trans->new_bgs)) { 26269ce977aSMiao Xie const char *errstr; 26369ce977aSMiao Xie 26408748810SDavid Sterba errstr = btrfs_decode_error(errno); 26566642832SJeff Mahoney btrfs_warn(fs_info, 266c2cf52ebSSimon Kirby "%s:%d: Aborting unused transaction(%s).", 26769ce977aSMiao Xie function, line, errstr); 26849b25e05SJeff Mahoney return; 26949b25e05SJeff Mahoney } 2708d25a086SMiao Xie ACCESS_ONCE(trans->transaction->aborted) = errno; 271501407aaSJosef Bacik /* Wake up anybody who may be waiting on this transaction */ 27266642832SJeff Mahoney wake_up(&fs_info->transaction_wait); 27366642832SJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 27466642832SJeff Mahoney __btrfs_handle_fs_error(fs_info, function, line, errno, NULL); 27549b25e05SJeff Mahoney } 27649b25e05SJeff Mahoney /* 2778c342930SJeff Mahoney * __btrfs_panic decodes unexpected, fatal errors from the caller, 2788c342930SJeff Mahoney * issues an alert, and either panics or BUGs, depending on mount options. 2798c342930SJeff Mahoney */ 280c0d19e2bSDavid Sterba __cold 2818c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, 2828c342930SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 2838c342930SJeff Mahoney { 2848c342930SJeff Mahoney char *s_id = "<unknown>"; 2858c342930SJeff Mahoney const char *errstr; 2868c342930SJeff Mahoney struct va_format vaf = { .fmt = fmt }; 2878c342930SJeff Mahoney va_list args; 2888c342930SJeff Mahoney 2898c342930SJeff Mahoney if (fs_info) 2908c342930SJeff Mahoney s_id = fs_info->sb->s_id; 2918c342930SJeff Mahoney 2928c342930SJeff Mahoney va_start(args, fmt); 2938c342930SJeff Mahoney vaf.va = &args; 2948c342930SJeff Mahoney 29508748810SDavid Sterba errstr = btrfs_decode_error(errno); 296aa43a17cSEric Sandeen if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR)) 29708748810SDavid Sterba panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n", 29808748810SDavid Sterba s_id, function, line, &vaf, errno, errstr); 2998c342930SJeff Mahoney 300efe120a0SFrank Holton btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)", 301efe120a0SFrank Holton function, line, &vaf, errno, errstr); 3028c342930SJeff Mahoney va_end(args); 3038c342930SJeff Mahoney /* Caller calls BUG() */ 3048c342930SJeff Mahoney } 305acce952bSliubo 306e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 307e20d96d6SChris Mason { 3086bccf3abSJeff Mahoney close_ctree(btrfs_sb(sb)); 309e20d96d6SChris Mason } 3102e635a27SChris Mason 31195e05289SChris Mason enum { 31273f73415SJosef Bacik Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum, 313287a0ab9SJosef Bacik Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, 314287a0ab9SJosef Bacik Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress, 315261507a0SLi Zefan Opt_compress_type, Opt_compress_force, Opt_compress_force_type, 316261507a0SLi Zefan Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard, 31770f6d82eSOmar Sandoval Opt_space_cache, Opt_space_cache_version, Opt_clear_cache, 31870f6d82eSOmar Sandoval Opt_user_subvol_rm_allowed, Opt_enospc_debug, Opt_subvolrootid, 31970f6d82eSOmar Sandoval Opt_defrag, Opt_inode_cache, Opt_no_space_cache, Opt_recovery, 32070f6d82eSOmar Sandoval Opt_skip_balance, Opt_check_integrity, 32170f6d82eSOmar Sandoval Opt_check_integrity_including_extent_data, 322f420ee1eSStefan Behrens Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree, 323e07a2adeSQu Wenruo Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard, 324a258af7aSQu Wenruo Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow, 3258dcddfa0SQu Wenruo Opt_datasum, Opt_treelog, Opt_noinode_cache, Opt_usebackuproot, 326fed8f166SQu Wenruo Opt_nologreplay, Opt_norecovery, 327d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 328d0bd4560SJosef Bacik Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all, 329d0bd4560SJosef Bacik #endif 3309555c6c1SIlya Dryomov Opt_err, 33195e05289SChris Mason }; 33295e05289SChris Mason 3334d4ab6d6SDavid Sterba static const match_table_t tokens = { 334dfe25020SChris Mason {Opt_degraded, "degraded"}, 33595e05289SChris Mason {Opt_subvol, "subvol=%s"}, 3361493381fSWang Shilong {Opt_subvolid, "subvolid=%s"}, 33743e570b0SChristoph Hellwig {Opt_device, "device=%s"}, 338b6cda9bcSChris Mason {Opt_nodatasum, "nodatasum"}, 339d399167dSQu Wenruo {Opt_datasum, "datasum"}, 340be20aa9dSChris Mason {Opt_nodatacow, "nodatacow"}, 341a258af7aSQu Wenruo {Opt_datacow, "datacow"}, 34221ad10cfSChris Mason {Opt_nobarrier, "nobarrier"}, 343842bef58SQu Wenruo {Opt_barrier, "barrier"}, 3446f568d35SChris Mason {Opt_max_inline, "max_inline=%s"}, 3458f662a76SChris Mason {Opt_alloc_start, "alloc_start=%s"}, 3464543df7eSChris Mason {Opt_thread_pool, "thread_pool=%d"}, 347c8b97818SChris Mason {Opt_compress, "compress"}, 348261507a0SLi Zefan {Opt_compress_type, "compress=%s"}, 349a555f810SChris Mason {Opt_compress_force, "compress-force"}, 350261507a0SLi Zefan {Opt_compress_force_type, "compress-force=%s"}, 351e18e4809SChris Mason {Opt_ssd, "ssd"}, 352451d7585SChris Mason {Opt_ssd_spread, "ssd_spread"}, 3533b30c22fSChris Mason {Opt_nossd, "nossd"}, 354bd0330adSQu Wenruo {Opt_acl, "acl"}, 35533268eafSJosef Bacik {Opt_noacl, "noacl"}, 3563a5e1404SSage Weil {Opt_notreelog, "notreelog"}, 357a88998f2SQu Wenruo {Opt_treelog, "treelog"}, 35896da0919SQu Wenruo {Opt_nologreplay, "nologreplay"}, 359fed8f166SQu Wenruo {Opt_norecovery, "norecovery"}, 360dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 3612c9ee856SQu Wenruo {Opt_noflushoncommit, "noflushoncommit"}, 36297e728d4SJosef Bacik {Opt_ratio, "metadata_ratio=%d"}, 363e244a0aeSChristoph Hellwig {Opt_discard, "discard"}, 364e07a2adeSQu Wenruo {Opt_nodiscard, "nodiscard"}, 3650af3d00bSJosef Bacik {Opt_space_cache, "space_cache"}, 36670f6d82eSOmar Sandoval {Opt_space_cache_version, "space_cache=%s"}, 36788c2ba3bSJosef Bacik {Opt_clear_cache, "clear_cache"}, 3684260f7c7SSage Weil {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, 36991435650SChris Mason {Opt_enospc_debug, "enospc_debug"}, 37053036293SQu Wenruo {Opt_noenospc_debug, "noenospc_debug"}, 371e15d0542SXin Zhong {Opt_subvolrootid, "subvolrootid=%d"}, 3724cb5300bSChris Mason {Opt_defrag, "autodefrag"}, 373fc0ca9afSQu Wenruo {Opt_nodefrag, "noautodefrag"}, 3744b9465cbSChris Mason {Opt_inode_cache, "inode_cache"}, 3753818aea2SQu Wenruo {Opt_noinode_cache, "noinode_cache"}, 3768965593eSDavid Sterba {Opt_no_space_cache, "nospace_cache"}, 3778dcddfa0SQu Wenruo {Opt_recovery, "recovery"}, /* deprecated */ 3788dcddfa0SQu Wenruo {Opt_usebackuproot, "usebackuproot"}, 3799555c6c1SIlya Dryomov {Opt_skip_balance, "skip_balance"}, 38021adbd5cSStefan Behrens {Opt_check_integrity, "check_int"}, 38121adbd5cSStefan Behrens {Opt_check_integrity_including_extent_data, "check_int_data"}, 38221adbd5cSStefan Behrens {Opt_check_integrity_print_mask, "check_int_print_mask=%d"}, 383f420ee1eSStefan Behrens {Opt_rescan_uuid_tree, "rescan_uuid_tree"}, 3848c342930SJeff Mahoney {Opt_fatal_errors, "fatal_errors=%s"}, 3858b87dc17SDavid Sterba {Opt_commit_interval, "commit=%d"}, 386d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 387d0bd4560SJosef Bacik {Opt_fragment_data, "fragment=data"}, 388d0bd4560SJosef Bacik {Opt_fragment_metadata, "fragment=metadata"}, 389d0bd4560SJosef Bacik {Opt_fragment_all, "fragment=all"}, 390d0bd4560SJosef Bacik #endif 39133268eafSJosef Bacik {Opt_err, NULL}, 39295e05289SChris Mason }; 39395e05289SChris Mason 394edf24abeSChristoph Hellwig /* 395edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 396edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 39749b25e05SJeff Mahoney * XXX JDM: This needs to be cleaned up for remount. 398edf24abeSChristoph Hellwig */ 399*2ff7e61eSJeff Mahoney int btrfs_parse_options(struct btrfs_fs_info *info, char *options, 40096da0919SQu Wenruo unsigned long new_flags) 40195e05289SChris Mason { 40295e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 40373bc1876SJosef Bacik char *p, *num, *orig = NULL; 40473bc1876SJosef Bacik u64 cache_gen; 4054543df7eSChris Mason int intarg; 406a7a3f7caSSage Weil int ret = 0; 407261507a0SLi Zefan char *compress_type; 408261507a0SLi Zefan bool compress_force = false; 409b7c47bbbSTsutomu Itoh enum btrfs_compression_type saved_compress_type; 410b7c47bbbSTsutomu Itoh bool saved_compress_force; 411b7c47bbbSTsutomu Itoh int no_compress = 0; 412b6cda9bcSChris Mason 4130b246afaSJeff Mahoney cache_gen = btrfs_super_cache_generation(info->super_copy); 4140b246afaSJeff Mahoney if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) 41570f6d82eSOmar Sandoval btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE); 41670f6d82eSOmar Sandoval else if (cache_gen) 41773bc1876SJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 41873bc1876SJosef Bacik 41996da0919SQu Wenruo /* 42096da0919SQu Wenruo * Even the options are empty, we still need to do extra check 42196da0919SQu Wenruo * against new flags 42296da0919SQu Wenruo */ 42395e05289SChris Mason if (!options) 42496da0919SQu Wenruo goto check; 42595e05289SChris Mason 426be20aa9dSChris Mason /* 427be20aa9dSChris Mason * strsep changes the string, duplicate it because parse_options 428be20aa9dSChris Mason * gets called twice 429be20aa9dSChris Mason */ 430be20aa9dSChris Mason options = kstrdup(options, GFP_NOFS); 431be20aa9dSChris Mason if (!options) 432be20aa9dSChris Mason return -ENOMEM; 433be20aa9dSChris Mason 434da495eccSJosef Bacik orig = options; 435be20aa9dSChris Mason 43695e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 43795e05289SChris Mason int token; 43895e05289SChris Mason if (!*p) 43995e05289SChris Mason continue; 44095e05289SChris Mason 44195e05289SChris Mason token = match_token(p, tokens, args); 44295e05289SChris Mason switch (token) { 443dfe25020SChris Mason case Opt_degraded: 4440b246afaSJeff Mahoney btrfs_info(info, "allowing degraded mounts"); 445dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 446dfe25020SChris Mason break; 44795e05289SChris Mason case Opt_subvol: 44873f73415SJosef Bacik case Opt_subvolid: 449e15d0542SXin Zhong case Opt_subvolrootid: 45043e570b0SChristoph Hellwig case Opt_device: 451edf24abeSChristoph Hellwig /* 45243e570b0SChristoph Hellwig * These are parsed by btrfs_parse_early_options 453edf24abeSChristoph Hellwig * and can be happily ignored here. 454edf24abeSChristoph Hellwig */ 45595e05289SChris Mason break; 456b6cda9bcSChris Mason case Opt_nodatasum: 4573cdde224SJeff Mahoney btrfs_set_and_info(info, NODATASUM, 45807802534SQu Wenruo "setting nodatasum"); 459be20aa9dSChris Mason break; 460d399167dSQu Wenruo case Opt_datasum: 4613cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATASUM)) { 4623cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATACOW)) 4630b246afaSJeff Mahoney btrfs_info(info, 4645d163e0eSJeff Mahoney "setting datasum, datacow enabled"); 465d399167dSQu Wenruo else 4660b246afaSJeff Mahoney btrfs_info(info, "setting datasum"); 46707802534SQu Wenruo } 468d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATACOW); 469d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATASUM); 470d399167dSQu Wenruo break; 471be20aa9dSChris Mason case Opt_nodatacow: 4723cdde224SJeff Mahoney if (!btrfs_test_opt(info, NODATACOW)) { 4733cdde224SJeff Mahoney if (!btrfs_test_opt(info, COMPRESS) || 4743cdde224SJeff Mahoney !btrfs_test_opt(info, FORCE_COMPRESS)) { 4750b246afaSJeff Mahoney btrfs_info(info, 476efe120a0SFrank Holton "setting nodatacow, compression disabled"); 477bedb2ccaSAndrei Popa } else { 4780b246afaSJeff Mahoney btrfs_info(info, "setting nodatacow"); 479bedb2ccaSAndrei Popa } 48007802534SQu Wenruo } 481bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, COMPRESS); 482bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 483be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 484be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 485b6cda9bcSChris Mason break; 486a258af7aSQu Wenruo case Opt_datacow: 4873cdde224SJeff Mahoney btrfs_clear_and_info(info, NODATACOW, 48807802534SQu Wenruo "setting datacow"); 489a258af7aSQu Wenruo break; 490a555f810SChris Mason case Opt_compress_force: 491261507a0SLi Zefan case Opt_compress_force_type: 492261507a0SLi Zefan compress_force = true; 4931c697d4aSEric Sandeen /* Fallthrough */ 494261507a0SLi Zefan case Opt_compress: 495261507a0SLi Zefan case Opt_compress_type: 4963cdde224SJeff Mahoney saved_compress_type = btrfs_test_opt(info, 4973cdde224SJeff Mahoney COMPRESS) ? 498b7c47bbbSTsutomu Itoh info->compress_type : BTRFS_COMPRESS_NONE; 499b7c47bbbSTsutomu Itoh saved_compress_force = 5003cdde224SJeff Mahoney btrfs_test_opt(info, FORCE_COMPRESS); 501261507a0SLi Zefan if (token == Opt_compress || 502261507a0SLi Zefan token == Opt_compress_force || 503261507a0SLi Zefan strcmp(args[0].from, "zlib") == 0) { 504261507a0SLi Zefan compress_type = "zlib"; 505261507a0SLi Zefan info->compress_type = BTRFS_COMPRESS_ZLIB; 506063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 507bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 508bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 509b7c47bbbSTsutomu Itoh no_compress = 0; 510a6fa6faeSLi Zefan } else if (strcmp(args[0].from, "lzo") == 0) { 511a6fa6faeSLi Zefan compress_type = "lzo"; 512a6fa6faeSLi Zefan info->compress_type = BTRFS_COMPRESS_LZO; 513063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 514bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 515bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 5162b0ce2c2SMitch Harder btrfs_set_fs_incompat(info, COMPRESS_LZO); 517b7c47bbbSTsutomu Itoh no_compress = 0; 518063849eaSArnd Hannemann } else if (strncmp(args[0].from, "no", 2) == 0) { 519063849eaSArnd Hannemann compress_type = "no"; 520063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, COMPRESS); 521063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 522063849eaSArnd Hannemann compress_force = false; 523b7c47bbbSTsutomu Itoh no_compress++; 524261507a0SLi Zefan } else { 525261507a0SLi Zefan ret = -EINVAL; 526261507a0SLi Zefan goto out; 527261507a0SLi Zefan } 528261507a0SLi Zefan 529261507a0SLi Zefan if (compress_force) { 530b7c47bbbSTsutomu Itoh btrfs_set_opt(info->mount_opt, FORCE_COMPRESS); 531143f3636SDavid Sterba } else { 5324027e0f4SWang Shilong /* 5334027e0f4SWang Shilong * If we remount from compress-force=xxx to 5344027e0f4SWang Shilong * compress=xxx, we need clear FORCE_COMPRESS 5354027e0f4SWang Shilong * flag, otherwise, there is no way for users 5364027e0f4SWang Shilong * to disable forcible compression separately. 5374027e0f4SWang Shilong */ 5384027e0f4SWang Shilong btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 539a7e252afSMiao Xie } 5403cdde224SJeff Mahoney if ((btrfs_test_opt(info, COMPRESS) && 541b7c47bbbSTsutomu Itoh (info->compress_type != saved_compress_type || 542b7c47bbbSTsutomu Itoh compress_force != saved_compress_force)) || 5433cdde224SJeff Mahoney (!btrfs_test_opt(info, COMPRESS) && 544b7c47bbbSTsutomu Itoh no_compress == 1)) { 5450b246afaSJeff Mahoney btrfs_info(info, "%s %s compression", 546b7c47bbbSTsutomu Itoh (compress_force) ? "force" : "use", 547b7c47bbbSTsutomu Itoh compress_type); 548b7c47bbbSTsutomu Itoh } 549b7c47bbbSTsutomu Itoh compress_force = false; 550a555f810SChris Mason break; 551e18e4809SChris Mason case Opt_ssd: 5523cdde224SJeff Mahoney btrfs_set_and_info(info, SSD, 55307802534SQu Wenruo "use ssd allocation scheme"); 554e18e4809SChris Mason break; 555451d7585SChris Mason case Opt_ssd_spread: 5563cdde224SJeff Mahoney btrfs_set_and_info(info, SSD_SPREAD, 55707802534SQu Wenruo "use spread ssd allocation scheme"); 5582aa06a35SEric Sandeen btrfs_set_opt(info->mount_opt, SSD); 559451d7585SChris Mason break; 5603b30c22fSChris Mason case Opt_nossd: 5613cdde224SJeff Mahoney btrfs_set_and_info(info, NOSSD, 56207802534SQu Wenruo "not using ssd allocation scheme"); 5633b30c22fSChris Mason btrfs_clear_opt(info->mount_opt, SSD); 5643b30c22fSChris Mason break; 565842bef58SQu Wenruo case Opt_barrier: 5663cdde224SJeff Mahoney btrfs_clear_and_info(info, NOBARRIER, 56707802534SQu Wenruo "turning on barriers"); 568842bef58SQu Wenruo break; 56921ad10cfSChris Mason case Opt_nobarrier: 5703cdde224SJeff Mahoney btrfs_set_and_info(info, NOBARRIER, 57107802534SQu Wenruo "turning off barriers"); 57221ad10cfSChris Mason break; 5734543df7eSChris Mason case Opt_thread_pool: 5742c334e87SWang Shilong ret = match_int(&args[0], &intarg); 5752c334e87SWang Shilong if (ret) { 5762c334e87SWang Shilong goto out; 5772c334e87SWang Shilong } else if (intarg > 0) { 5784543df7eSChris Mason info->thread_pool_size = intarg; 5792c334e87SWang Shilong } else { 5802c334e87SWang Shilong ret = -EINVAL; 5812c334e87SWang Shilong goto out; 5822c334e87SWang Shilong } 5834543df7eSChris Mason break; 5846f568d35SChris Mason case Opt_max_inline: 585edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5866f568d35SChris Mason if (num) { 58791748467SAkinobu Mita info->max_inline = memparse(num, NULL); 5886f568d35SChris Mason kfree(num); 5896f568d35SChris Mason 59015ada040SChris Mason if (info->max_inline) { 591feb5f965SMitch Harder info->max_inline = min_t(u64, 59215ada040SChris Mason info->max_inline, 5930b246afaSJeff Mahoney info->sectorsize); 59415ada040SChris Mason } 5950b246afaSJeff Mahoney btrfs_info(info, "max_inline at %llu", 596c1c9ff7cSGeert Uytterhoeven info->max_inline); 5972c334e87SWang Shilong } else { 5982c334e87SWang Shilong ret = -ENOMEM; 5992c334e87SWang Shilong goto out; 6006f568d35SChris Mason } 6016f568d35SChris Mason break; 6028f662a76SChris Mason case Opt_alloc_start: 603edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 6048f662a76SChris Mason if (num) { 605c018daecSMiao Xie mutex_lock(&info->chunk_mutex); 60691748467SAkinobu Mita info->alloc_start = memparse(num, NULL); 607c018daecSMiao Xie mutex_unlock(&info->chunk_mutex); 6088f662a76SChris Mason kfree(num); 6090b246afaSJeff Mahoney btrfs_info(info, "allocations start at %llu", 610c1c9ff7cSGeert Uytterhoeven info->alloc_start); 6112c334e87SWang Shilong } else { 6122c334e87SWang Shilong ret = -ENOMEM; 6132c334e87SWang Shilong goto out; 6148f662a76SChris Mason } 6158f662a76SChris Mason break; 616bd0330adSQu Wenruo case Opt_acl: 61745ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL 6180b246afaSJeff Mahoney info->sb->s_flags |= MS_POSIXACL; 619bd0330adSQu Wenruo break; 62045ff35d6SGuangliang Zhao #else 6210b246afaSJeff Mahoney btrfs_err(info, "support for ACL not compiled in!"); 62245ff35d6SGuangliang Zhao ret = -EINVAL; 62345ff35d6SGuangliang Zhao goto out; 62445ff35d6SGuangliang Zhao #endif 62533268eafSJosef Bacik case Opt_noacl: 6260b246afaSJeff Mahoney info->sb->s_flags &= ~MS_POSIXACL; 62733268eafSJosef Bacik break; 6283a5e1404SSage Weil case Opt_notreelog: 6293cdde224SJeff Mahoney btrfs_set_and_info(info, NOTREELOG, 63007802534SQu Wenruo "disabling tree log"); 6313a5e1404SSage Weil break; 632a88998f2SQu Wenruo case Opt_treelog: 6333cdde224SJeff Mahoney btrfs_clear_and_info(info, NOTREELOG, 63407802534SQu Wenruo "enabling tree log"); 635a88998f2SQu Wenruo break; 636fed8f166SQu Wenruo case Opt_norecovery: 63796da0919SQu Wenruo case Opt_nologreplay: 6383cdde224SJeff Mahoney btrfs_set_and_info(info, NOLOGREPLAY, 63996da0919SQu Wenruo "disabling log replay at mount time"); 64096da0919SQu Wenruo break; 641dccae999SSage Weil case Opt_flushoncommit: 6423cdde224SJeff Mahoney btrfs_set_and_info(info, FLUSHONCOMMIT, 64307802534SQu Wenruo "turning on flush-on-commit"); 644dccae999SSage Weil break; 6452c9ee856SQu Wenruo case Opt_noflushoncommit: 6463cdde224SJeff Mahoney btrfs_clear_and_info(info, FLUSHONCOMMIT, 64707802534SQu Wenruo "turning off flush-on-commit"); 6482c9ee856SQu Wenruo break; 64997e728d4SJosef Bacik case Opt_ratio: 6502c334e87SWang Shilong ret = match_int(&args[0], &intarg); 6512c334e87SWang Shilong if (ret) { 6522c334e87SWang Shilong goto out; 6532c334e87SWang Shilong } else if (intarg >= 0) { 65497e728d4SJosef Bacik info->metadata_ratio = intarg; 6550b246afaSJeff Mahoney btrfs_info(info, "metadata ratio %d", 65697e728d4SJosef Bacik info->metadata_ratio); 6572c334e87SWang Shilong } else { 6582c334e87SWang Shilong ret = -EINVAL; 6592c334e87SWang Shilong goto out; 66097e728d4SJosef Bacik } 66197e728d4SJosef Bacik break; 662e244a0aeSChristoph Hellwig case Opt_discard: 6633cdde224SJeff Mahoney btrfs_set_and_info(info, DISCARD, 66407802534SQu Wenruo "turning on discard"); 665e244a0aeSChristoph Hellwig break; 666e07a2adeSQu Wenruo case Opt_nodiscard: 6673cdde224SJeff Mahoney btrfs_clear_and_info(info, DISCARD, 66807802534SQu Wenruo "turning off discard"); 669e07a2adeSQu Wenruo break; 6700af3d00bSJosef Bacik case Opt_space_cache: 67170f6d82eSOmar Sandoval case Opt_space_cache_version: 67270f6d82eSOmar Sandoval if (token == Opt_space_cache || 67370f6d82eSOmar Sandoval strcmp(args[0].from, "v1") == 0) { 6740b246afaSJeff Mahoney btrfs_clear_opt(info->mount_opt, 67570f6d82eSOmar Sandoval FREE_SPACE_TREE); 6763cdde224SJeff Mahoney btrfs_set_and_info(info, SPACE_CACHE, 67707802534SQu Wenruo "enabling disk space caching"); 67870f6d82eSOmar Sandoval } else if (strcmp(args[0].from, "v2") == 0) { 6790b246afaSJeff Mahoney btrfs_clear_opt(info->mount_opt, 68070f6d82eSOmar Sandoval SPACE_CACHE); 6810b246afaSJeff Mahoney btrfs_set_and_info(info, FREE_SPACE_TREE, 68270f6d82eSOmar Sandoval "enabling free space tree"); 68370f6d82eSOmar Sandoval } else { 68470f6d82eSOmar Sandoval ret = -EINVAL; 68570f6d82eSOmar Sandoval goto out; 68670f6d82eSOmar Sandoval } 6870de90876SJosef Bacik break; 688f420ee1eSStefan Behrens case Opt_rescan_uuid_tree: 689f420ee1eSStefan Behrens btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE); 690f420ee1eSStefan Behrens break; 69173bc1876SJosef Bacik case Opt_no_space_cache: 6923cdde224SJeff Mahoney if (btrfs_test_opt(info, SPACE_CACHE)) { 6930b246afaSJeff Mahoney btrfs_clear_and_info(info, SPACE_CACHE, 69407802534SQu Wenruo "disabling disk space caching"); 69570f6d82eSOmar Sandoval } 6963cdde224SJeff Mahoney if (btrfs_test_opt(info, FREE_SPACE_TREE)) { 6970b246afaSJeff Mahoney btrfs_clear_and_info(info, FREE_SPACE_TREE, 69870f6d82eSOmar Sandoval "disabling free space tree"); 69970f6d82eSOmar Sandoval } 70073bc1876SJosef Bacik break; 7014b9465cbSChris Mason case Opt_inode_cache: 7027e1876acSDavid Sterba btrfs_set_pending_and_info(info, INODE_MAP_CACHE, 70307802534SQu Wenruo "enabling inode map caching"); 7043818aea2SQu Wenruo break; 7053818aea2SQu Wenruo case Opt_noinode_cache: 7067e1876acSDavid Sterba btrfs_clear_pending_and_info(info, INODE_MAP_CACHE, 70707802534SQu Wenruo "disabling inode map caching"); 7084b9465cbSChris Mason break; 70988c2ba3bSJosef Bacik case Opt_clear_cache: 7103cdde224SJeff Mahoney btrfs_set_and_info(info, CLEAR_CACHE, 71107802534SQu Wenruo "force clearing of disk cache"); 7120af3d00bSJosef Bacik break; 7134260f7c7SSage Weil case Opt_user_subvol_rm_allowed: 7144260f7c7SSage Weil btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 7154260f7c7SSage Weil break; 71691435650SChris Mason case Opt_enospc_debug: 71791435650SChris Mason btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 71891435650SChris Mason break; 71953036293SQu Wenruo case Opt_noenospc_debug: 72053036293SQu Wenruo btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG); 72153036293SQu Wenruo break; 7224cb5300bSChris Mason case Opt_defrag: 7233cdde224SJeff Mahoney btrfs_set_and_info(info, AUTO_DEFRAG, 72407802534SQu Wenruo "enabling auto defrag"); 7254cb5300bSChris Mason break; 726fc0ca9afSQu Wenruo case Opt_nodefrag: 7273cdde224SJeff Mahoney btrfs_clear_and_info(info, AUTO_DEFRAG, 72807802534SQu Wenruo "disabling auto defrag"); 729fc0ca9afSQu Wenruo break; 730af31f5e5SChris Mason case Opt_recovery: 7310b246afaSJeff Mahoney btrfs_warn(info, 7328dcddfa0SQu Wenruo "'recovery' is deprecated, use 'usebackuproot' instead"); 7338dcddfa0SQu Wenruo case Opt_usebackuproot: 7340b246afaSJeff Mahoney btrfs_info(info, 7358dcddfa0SQu Wenruo "trying to use backup root at mount time"); 7368dcddfa0SQu Wenruo btrfs_set_opt(info->mount_opt, USEBACKUPROOT); 737af31f5e5SChris Mason break; 7389555c6c1SIlya Dryomov case Opt_skip_balance: 7399555c6c1SIlya Dryomov btrfs_set_opt(info->mount_opt, SKIP_BALANCE); 7409555c6c1SIlya Dryomov break; 74121adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 74221adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 7430b246afaSJeff Mahoney btrfs_info(info, 744efe120a0SFrank Holton "enabling check integrity including extent data"); 74521adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, 74621adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA); 74721adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 74821adbd5cSStefan Behrens break; 74921adbd5cSStefan Behrens case Opt_check_integrity: 7500b246afaSJeff Mahoney btrfs_info(info, "enabling check integrity"); 75121adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 75221adbd5cSStefan Behrens break; 75321adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 7542c334e87SWang Shilong ret = match_int(&args[0], &intarg); 7552c334e87SWang Shilong if (ret) { 7562c334e87SWang Shilong goto out; 7572c334e87SWang Shilong } else if (intarg >= 0) { 75821adbd5cSStefan Behrens info->check_integrity_print_mask = intarg; 7590b246afaSJeff Mahoney btrfs_info(info, 7605d163e0eSJeff Mahoney "check_integrity_print_mask 0x%x", 76121adbd5cSStefan Behrens info->check_integrity_print_mask); 7622c334e87SWang Shilong } else { 7632c334e87SWang Shilong ret = -EINVAL; 7642c334e87SWang Shilong goto out; 76521adbd5cSStefan Behrens } 76621adbd5cSStefan Behrens break; 76721adbd5cSStefan Behrens #else 76821adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 76921adbd5cSStefan Behrens case Opt_check_integrity: 77021adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 7710b246afaSJeff Mahoney btrfs_err(info, 772efe120a0SFrank Holton "support for check_integrity* not compiled in!"); 77321adbd5cSStefan Behrens ret = -EINVAL; 77421adbd5cSStefan Behrens goto out; 77521adbd5cSStefan Behrens #endif 7768c342930SJeff Mahoney case Opt_fatal_errors: 7778c342930SJeff Mahoney if (strcmp(args[0].from, "panic") == 0) 7788c342930SJeff Mahoney btrfs_set_opt(info->mount_opt, 7798c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7808c342930SJeff Mahoney else if (strcmp(args[0].from, "bug") == 0) 7818c342930SJeff Mahoney btrfs_clear_opt(info->mount_opt, 7828c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7838c342930SJeff Mahoney else { 7848c342930SJeff Mahoney ret = -EINVAL; 7858c342930SJeff Mahoney goto out; 7868c342930SJeff Mahoney } 7878c342930SJeff Mahoney break; 7888b87dc17SDavid Sterba case Opt_commit_interval: 7898b87dc17SDavid Sterba intarg = 0; 7908b87dc17SDavid Sterba ret = match_int(&args[0], &intarg); 7918b87dc17SDavid Sterba if (ret < 0) { 7920b246afaSJeff Mahoney btrfs_err(info, "invalid commit interval"); 7938b87dc17SDavid Sterba ret = -EINVAL; 7948b87dc17SDavid Sterba goto out; 7958b87dc17SDavid Sterba } 7968b87dc17SDavid Sterba if (intarg > 0) { 7978b87dc17SDavid Sterba if (intarg > 300) { 7980b246afaSJeff Mahoney btrfs_warn(info, 7995d163e0eSJeff Mahoney "excessive commit interval %d", 8008b87dc17SDavid Sterba intarg); 8018b87dc17SDavid Sterba } 8028b87dc17SDavid Sterba info->commit_interval = intarg; 8038b87dc17SDavid Sterba } else { 8040b246afaSJeff Mahoney btrfs_info(info, 8055d163e0eSJeff Mahoney "using default commit interval %ds", 8068b87dc17SDavid Sterba BTRFS_DEFAULT_COMMIT_INTERVAL); 8078b87dc17SDavid Sterba info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 8088b87dc17SDavid Sterba } 8098b87dc17SDavid Sterba break; 810d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 811d0bd4560SJosef Bacik case Opt_fragment_all: 8120b246afaSJeff Mahoney btrfs_info(info, "fragmenting all space"); 813d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 814d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA); 815d0bd4560SJosef Bacik break; 816d0bd4560SJosef Bacik case Opt_fragment_metadata: 8170b246afaSJeff Mahoney btrfs_info(info, "fragmenting metadata"); 818d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, 819d0bd4560SJosef Bacik FRAGMENT_METADATA); 820d0bd4560SJosef Bacik break; 821d0bd4560SJosef Bacik case Opt_fragment_data: 8220b246afaSJeff Mahoney btrfs_info(info, "fragmenting data"); 823d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 824d0bd4560SJosef Bacik break; 825d0bd4560SJosef Bacik #endif 826a7a3f7caSSage Weil case Opt_err: 8270b246afaSJeff Mahoney btrfs_info(info, "unrecognized mount option '%s'", p); 828a7a3f7caSSage Weil ret = -EINVAL; 829a7a3f7caSSage Weil goto out; 83095e05289SChris Mason default: 831be20aa9dSChris Mason break; 83295e05289SChris Mason } 83395e05289SChris Mason } 83496da0919SQu Wenruo check: 83596da0919SQu Wenruo /* 83696da0919SQu Wenruo * Extra check for current option against current flag 83796da0919SQu Wenruo */ 8383cdde224SJeff Mahoney if (btrfs_test_opt(info, NOLOGREPLAY) && !(new_flags & MS_RDONLY)) { 8390b246afaSJeff Mahoney btrfs_err(info, 84096da0919SQu Wenruo "nologreplay must be used with ro mount option"); 84196da0919SQu Wenruo ret = -EINVAL; 84296da0919SQu Wenruo } 843a7a3f7caSSage Weil out: 8440b246afaSJeff Mahoney if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) && 8453cdde224SJeff Mahoney !btrfs_test_opt(info, FREE_SPACE_TREE) && 8463cdde224SJeff Mahoney !btrfs_test_opt(info, CLEAR_CACHE)) { 8470b246afaSJeff Mahoney btrfs_err(info, "cannot disable free space tree"); 84870f6d82eSOmar Sandoval ret = -EINVAL; 84970f6d82eSOmar Sandoval 85070f6d82eSOmar Sandoval } 8513cdde224SJeff Mahoney if (!ret && btrfs_test_opt(info, SPACE_CACHE)) 8520b246afaSJeff Mahoney btrfs_info(info, "disk space caching is enabled"); 8533cdde224SJeff Mahoney if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE)) 8540b246afaSJeff Mahoney btrfs_info(info, "using free space tree"); 855da495eccSJosef Bacik kfree(orig); 856a7a3f7caSSage Weil return ret; 857edf24abeSChristoph Hellwig } 858edf24abeSChristoph Hellwig 859edf24abeSChristoph Hellwig /* 860edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 861edf24abeSChristoph Hellwig * 862edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 863edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 864edf24abeSChristoph Hellwig */ 86597288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 86673f73415SJosef Bacik void *holder, char **subvol_name, u64 *subvol_objectid, 8675e2a4b25SDavid Sterba struct btrfs_fs_devices **fs_devices) 868edf24abeSChristoph Hellwig { 869edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 87083c8c9bdSJeff Liu char *device_name, *opts, *orig, *p; 8711493381fSWang Shilong char *num = NULL; 872edf24abeSChristoph Hellwig int error = 0; 873edf24abeSChristoph Hellwig 874edf24abeSChristoph Hellwig if (!options) 875830c4adbSJosef Bacik return 0; 876edf24abeSChristoph Hellwig 877edf24abeSChristoph Hellwig /* 878edf24abeSChristoph Hellwig * strsep changes the string, duplicate it because parse_options 879edf24abeSChristoph Hellwig * gets called twice 880edf24abeSChristoph Hellwig */ 881edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 882edf24abeSChristoph Hellwig if (!opts) 883edf24abeSChristoph Hellwig return -ENOMEM; 8843f3d0bc0STero Roponen orig = opts; 885edf24abeSChristoph Hellwig 886edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 887edf24abeSChristoph Hellwig int token; 888edf24abeSChristoph Hellwig if (!*p) 889edf24abeSChristoph Hellwig continue; 890edf24abeSChristoph Hellwig 891edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 892edf24abeSChristoph Hellwig switch (token) { 893edf24abeSChristoph Hellwig case Opt_subvol: 894a90e8b6fSIlya Dryomov kfree(*subvol_name); 895edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 8962c334e87SWang Shilong if (!*subvol_name) { 8972c334e87SWang Shilong error = -ENOMEM; 8982c334e87SWang Shilong goto out; 8992c334e87SWang Shilong } 900edf24abeSChristoph Hellwig break; 90173f73415SJosef Bacik case Opt_subvolid: 9021493381fSWang Shilong num = match_strdup(&args[0]); 9031493381fSWang Shilong if (num) { 9041493381fSWang Shilong *subvol_objectid = memparse(num, NULL); 9051493381fSWang Shilong kfree(num); 9064849f01dSJosef Bacik /* we want the original fs_tree */ 9071493381fSWang Shilong if (!*subvol_objectid) 9084849f01dSJosef Bacik *subvol_objectid = 9094849f01dSJosef Bacik BTRFS_FS_TREE_OBJECTID; 9102c334e87SWang Shilong } else { 9112c334e87SWang Shilong error = -EINVAL; 9122c334e87SWang Shilong goto out; 9134849f01dSJosef Bacik } 91473f73415SJosef Bacik break; 915e15d0542SXin Zhong case Opt_subvolrootid: 91662e85577SJeff Mahoney pr_warn("BTRFS: 'subvolrootid' mount option is deprecated and has no effect\n"); 917e15d0542SXin Zhong break; 91843e570b0SChristoph Hellwig case Opt_device: 91983c8c9bdSJeff Liu device_name = match_strdup(&args[0]); 92083c8c9bdSJeff Liu if (!device_name) { 92183c8c9bdSJeff Liu error = -ENOMEM; 92283c8c9bdSJeff Liu goto out; 92383c8c9bdSJeff Liu } 92483c8c9bdSJeff Liu error = btrfs_scan_one_device(device_name, 92543e570b0SChristoph Hellwig flags, holder, fs_devices); 92683c8c9bdSJeff Liu kfree(device_name); 92743e570b0SChristoph Hellwig if (error) 928830c4adbSJosef Bacik goto out; 92943e570b0SChristoph Hellwig break; 930edf24abeSChristoph Hellwig default: 931edf24abeSChristoph Hellwig break; 932edf24abeSChristoph Hellwig } 933edf24abeSChristoph Hellwig } 934edf24abeSChristoph Hellwig 935edf24abeSChristoph Hellwig out: 936830c4adbSJosef Bacik kfree(orig); 937edf24abeSChristoph Hellwig return error; 93895e05289SChris Mason } 93995e05289SChris Mason 94005dbe683SOmar Sandoval static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info, 94173f73415SJosef Bacik u64 subvol_objectid) 94273f73415SJosef Bacik { 943815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 94405dbe683SOmar Sandoval struct btrfs_root *fs_root; 94505dbe683SOmar Sandoval struct btrfs_root_ref *root_ref; 94605dbe683SOmar Sandoval struct btrfs_inode_ref *inode_ref; 94705dbe683SOmar Sandoval struct btrfs_key key; 94805dbe683SOmar Sandoval struct btrfs_path *path = NULL; 94905dbe683SOmar Sandoval char *name = NULL, *ptr; 95005dbe683SOmar Sandoval u64 dirid; 95105dbe683SOmar Sandoval int len; 95205dbe683SOmar Sandoval int ret; 95305dbe683SOmar Sandoval 95405dbe683SOmar Sandoval path = btrfs_alloc_path(); 95505dbe683SOmar Sandoval if (!path) { 95605dbe683SOmar Sandoval ret = -ENOMEM; 95705dbe683SOmar Sandoval goto err; 95805dbe683SOmar Sandoval } 95905dbe683SOmar Sandoval path->leave_spinning = 1; 96005dbe683SOmar Sandoval 96105dbe683SOmar Sandoval name = kmalloc(PATH_MAX, GFP_NOFS); 96205dbe683SOmar Sandoval if (!name) { 96305dbe683SOmar Sandoval ret = -ENOMEM; 96405dbe683SOmar Sandoval goto err; 96505dbe683SOmar Sandoval } 96605dbe683SOmar Sandoval ptr = name + PATH_MAX - 1; 96705dbe683SOmar Sandoval ptr[0] = '\0'; 96805dbe683SOmar Sandoval 96905dbe683SOmar Sandoval /* 97005dbe683SOmar Sandoval * Walk up the subvolume trees in the tree of tree roots by root 97105dbe683SOmar Sandoval * backrefs until we hit the top-level subvolume. 97205dbe683SOmar Sandoval */ 97305dbe683SOmar Sandoval while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) { 97405dbe683SOmar Sandoval key.objectid = subvol_objectid; 97505dbe683SOmar Sandoval key.type = BTRFS_ROOT_BACKREF_KEY; 97605dbe683SOmar Sandoval key.offset = (u64)-1; 97705dbe683SOmar Sandoval 97805dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 97905dbe683SOmar Sandoval if (ret < 0) { 98005dbe683SOmar Sandoval goto err; 98105dbe683SOmar Sandoval } else if (ret > 0) { 98205dbe683SOmar Sandoval ret = btrfs_previous_item(root, path, subvol_objectid, 98305dbe683SOmar Sandoval BTRFS_ROOT_BACKREF_KEY); 98405dbe683SOmar Sandoval if (ret < 0) { 98505dbe683SOmar Sandoval goto err; 98605dbe683SOmar Sandoval } else if (ret > 0) { 98705dbe683SOmar Sandoval ret = -ENOENT; 98805dbe683SOmar Sandoval goto err; 98905dbe683SOmar Sandoval } 99005dbe683SOmar Sandoval } 99105dbe683SOmar Sandoval 99205dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 99305dbe683SOmar Sandoval subvol_objectid = key.offset; 99405dbe683SOmar Sandoval 99505dbe683SOmar Sandoval root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], 99605dbe683SOmar Sandoval struct btrfs_root_ref); 99705dbe683SOmar Sandoval len = btrfs_root_ref_name_len(path->nodes[0], root_ref); 99805dbe683SOmar Sandoval ptr -= len + 1; 99905dbe683SOmar Sandoval if (ptr < name) { 100005dbe683SOmar Sandoval ret = -ENAMETOOLONG; 100105dbe683SOmar Sandoval goto err; 100205dbe683SOmar Sandoval } 100305dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 100405dbe683SOmar Sandoval (unsigned long)(root_ref + 1), len); 100505dbe683SOmar Sandoval ptr[0] = '/'; 100605dbe683SOmar Sandoval dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref); 100705dbe683SOmar Sandoval btrfs_release_path(path); 100805dbe683SOmar Sandoval 100905dbe683SOmar Sandoval key.objectid = subvol_objectid; 101005dbe683SOmar Sandoval key.type = BTRFS_ROOT_ITEM_KEY; 101105dbe683SOmar Sandoval key.offset = (u64)-1; 101205dbe683SOmar Sandoval fs_root = btrfs_read_fs_root_no_name(fs_info, &key); 101305dbe683SOmar Sandoval if (IS_ERR(fs_root)) { 101405dbe683SOmar Sandoval ret = PTR_ERR(fs_root); 101505dbe683SOmar Sandoval goto err; 101605dbe683SOmar Sandoval } 101705dbe683SOmar Sandoval 101805dbe683SOmar Sandoval /* 101905dbe683SOmar Sandoval * Walk up the filesystem tree by inode refs until we hit the 102005dbe683SOmar Sandoval * root directory. 102105dbe683SOmar Sandoval */ 102205dbe683SOmar Sandoval while (dirid != BTRFS_FIRST_FREE_OBJECTID) { 102305dbe683SOmar Sandoval key.objectid = dirid; 102405dbe683SOmar Sandoval key.type = BTRFS_INODE_REF_KEY; 102505dbe683SOmar Sandoval key.offset = (u64)-1; 102605dbe683SOmar Sandoval 102705dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); 102805dbe683SOmar Sandoval if (ret < 0) { 102905dbe683SOmar Sandoval goto err; 103005dbe683SOmar Sandoval } else if (ret > 0) { 103105dbe683SOmar Sandoval ret = btrfs_previous_item(fs_root, path, dirid, 103205dbe683SOmar Sandoval BTRFS_INODE_REF_KEY); 103305dbe683SOmar Sandoval if (ret < 0) { 103405dbe683SOmar Sandoval goto err; 103505dbe683SOmar Sandoval } else if (ret > 0) { 103605dbe683SOmar Sandoval ret = -ENOENT; 103705dbe683SOmar Sandoval goto err; 103805dbe683SOmar Sandoval } 103905dbe683SOmar Sandoval } 104005dbe683SOmar Sandoval 104105dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 104205dbe683SOmar Sandoval dirid = key.offset; 104305dbe683SOmar Sandoval 104405dbe683SOmar Sandoval inode_ref = btrfs_item_ptr(path->nodes[0], 104505dbe683SOmar Sandoval path->slots[0], 104605dbe683SOmar Sandoval struct btrfs_inode_ref); 104705dbe683SOmar Sandoval len = btrfs_inode_ref_name_len(path->nodes[0], 104805dbe683SOmar Sandoval inode_ref); 104905dbe683SOmar Sandoval ptr -= len + 1; 105005dbe683SOmar Sandoval if (ptr < name) { 105105dbe683SOmar Sandoval ret = -ENAMETOOLONG; 105205dbe683SOmar Sandoval goto err; 105305dbe683SOmar Sandoval } 105405dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 105505dbe683SOmar Sandoval (unsigned long)(inode_ref + 1), len); 105605dbe683SOmar Sandoval ptr[0] = '/'; 105705dbe683SOmar Sandoval btrfs_release_path(path); 105805dbe683SOmar Sandoval } 105905dbe683SOmar Sandoval } 106005dbe683SOmar Sandoval 106105dbe683SOmar Sandoval btrfs_free_path(path); 106205dbe683SOmar Sandoval if (ptr == name + PATH_MAX - 1) { 106305dbe683SOmar Sandoval name[0] = '/'; 106405dbe683SOmar Sandoval name[1] = '\0'; 106505dbe683SOmar Sandoval } else { 106605dbe683SOmar Sandoval memmove(name, ptr, name + PATH_MAX - ptr); 106705dbe683SOmar Sandoval } 106805dbe683SOmar Sandoval return name; 106905dbe683SOmar Sandoval 107005dbe683SOmar Sandoval err: 107105dbe683SOmar Sandoval btrfs_free_path(path); 107205dbe683SOmar Sandoval kfree(name); 107305dbe683SOmar Sandoval return ERR_PTR(ret); 107405dbe683SOmar Sandoval } 107505dbe683SOmar Sandoval 107605dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid) 107705dbe683SOmar Sandoval { 107805dbe683SOmar Sandoval struct btrfs_root *root = fs_info->tree_root; 107973f73415SJosef Bacik struct btrfs_dir_item *di; 108073f73415SJosef Bacik struct btrfs_path *path; 108173f73415SJosef Bacik struct btrfs_key location; 108273f73415SJosef Bacik u64 dir_id; 108373f73415SJosef Bacik 108473f73415SJosef Bacik path = btrfs_alloc_path(); 108573f73415SJosef Bacik if (!path) 108605dbe683SOmar Sandoval return -ENOMEM; 108773f73415SJosef Bacik path->leave_spinning = 1; 108873f73415SJosef Bacik 108973f73415SJosef Bacik /* 109073f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 109173f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 109273f73415SJosef Bacik * to mount. 109373f73415SJosef Bacik */ 1094815745cfSAl Viro dir_id = btrfs_super_root_dir(fs_info->super_copy); 109573f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 1096b0839166SJulia Lawall if (IS_ERR(di)) { 1097b0839166SJulia Lawall btrfs_free_path(path); 109805dbe683SOmar Sandoval return PTR_ERR(di); 1099b0839166SJulia Lawall } 110073f73415SJosef Bacik if (!di) { 110173f73415SJosef Bacik /* 110273f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 110373f73415SJosef Bacik * it's always been there, but don't freak out, just try and 110405dbe683SOmar Sandoval * mount the top-level subvolume. 110573f73415SJosef Bacik */ 110673f73415SJosef Bacik btrfs_free_path(path); 110705dbe683SOmar Sandoval *objectid = BTRFS_FS_TREE_OBJECTID; 110805dbe683SOmar Sandoval return 0; 110973f73415SJosef Bacik } 111073f73415SJosef Bacik 111173f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 111273f73415SJosef Bacik btrfs_free_path(path); 111305dbe683SOmar Sandoval *objectid = location.objectid; 111405dbe683SOmar Sandoval return 0; 111573f73415SJosef Bacik } 111673f73415SJosef Bacik 11178a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 11188a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 11198a4b83ccSChris Mason void *data, int silent) 11202e635a27SChris Mason { 11212e635a27SChris Mason struct inode *inode; 1122815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 11235d4f98a2SYan Zheng struct btrfs_key key; 112439279cc3SChris Mason int err; 11252e635a27SChris Mason 11262e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 11272e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 1128e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 1129af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 1130be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 11315103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 11322e635a27SChris Mason sb->s_time_gran = 1; 11330eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 113433268eafSJosef Bacik sb->s_flags |= MS_POSIXACL; 113549cf6f45SChris Ball #endif 11360c4d2d95SJosef Bacik sb->s_flags |= MS_I_VERSION; 1137da2f0f74SChris Mason sb->s_iflags |= SB_I_CGROUPWB; 1138ad2b2c80SAl Viro err = open_ctree(sb, fs_devices, (char *)data); 1139ad2b2c80SAl Viro if (err) { 1140ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "open_ctree failed"); 1141ad2b2c80SAl Viro return err; 1142e20d96d6SChris Mason } 1143b888db2bSChris Mason 11445d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 11455d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 11465d4f98a2SYan Zheng key.offset = 0; 114798c7089cSAl Viro inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL); 11485d4f98a2SYan Zheng if (IS_ERR(inode)) { 11495d4f98a2SYan Zheng err = PTR_ERR(inode); 115039279cc3SChris Mason goto fail_close; 115139279cc3SChris Mason } 11522e635a27SChris Mason 115348fde701SAl Viro sb->s_root = d_make_root(inode); 115448fde701SAl Viro if (!sb->s_root) { 115539279cc3SChris Mason err = -ENOMEM; 115639279cc3SChris Mason goto fail_close; 11572e635a27SChris Mason } 115858176a96SJosef Bacik 11596885f308SChris Mason save_mount_options(sb, data); 116090a887c9SDan Magenheimer cleancache_init_fs(sb); 116159553edfSAl Viro sb->s_flags |= MS_ACTIVE; 11622e635a27SChris Mason return 0; 11632e635a27SChris Mason 116439279cc3SChris Mason fail_close: 11656bccf3abSJeff Mahoney close_ctree(fs_info); 1166d5719762SChris Mason return err; 1167d5719762SChris Mason } 1168d5719762SChris Mason 11696bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 1170d5719762SChris Mason { 1171d5719762SChris Mason struct btrfs_trans_handle *trans; 1172815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1173815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 1174df2ce34cSChris Mason 1175bc074524SJeff Mahoney trace_btrfs_sync_fs(fs_info, wait); 11761abe9b8aSliubo 1177d561c025SChris Mason if (!wait) { 1178815745cfSAl Viro filemap_flush(fs_info->btree_inode->i_mapping); 1179df2ce34cSChris Mason return 0; 1180d561c025SChris Mason } 1181771ed689SChris Mason 1182578def7cSFilipe Manana btrfs_wait_ordered_roots(fs_info, -1, 0, (u64)-1); 1183771ed689SChris Mason 1184d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 118560376ce4SJosef Bacik if (IS_ERR(trans)) { 1186354aa0fbSMiao Xie /* no transaction, don't bother */ 11876b5fe46dSDavid Sterba if (PTR_ERR(trans) == -ENOENT) { 11886b5fe46dSDavid Sterba /* 11896b5fe46dSDavid Sterba * Exit unless we have some pending changes 11906b5fe46dSDavid Sterba * that need to go through commit 11916b5fe46dSDavid Sterba */ 11926b5fe46dSDavid Sterba if (fs_info->pending_changes == 0) 1193bd7de2c9SJosef Bacik return 0; 1194a53f4f8eSQu Wenruo /* 1195a53f4f8eSQu Wenruo * A non-blocking test if the fs is frozen. We must not 1196a53f4f8eSQu Wenruo * start a new transaction here otherwise a deadlock 1197a53f4f8eSQu Wenruo * happens. The pending operations are delayed to the 1198a53f4f8eSQu Wenruo * next commit after thawing. 1199a53f4f8eSQu Wenruo */ 1200a53f4f8eSQu Wenruo if (__sb_start_write(sb, SB_FREEZE_WRITE, false)) 1201a53f4f8eSQu Wenruo __sb_end_write(sb, SB_FREEZE_WRITE); 1202a53f4f8eSQu Wenruo else 1203a53f4f8eSQu Wenruo return 0; 12046b5fe46dSDavid Sterba trans = btrfs_start_transaction(root, 0); 120560376ce4SJosef Bacik } 120698bd5c54SDavid Sterba if (IS_ERR(trans)) 120798bd5c54SDavid Sterba return PTR_ERR(trans); 12086b5fe46dSDavid Sterba } 1209bd7de2c9SJosef Bacik return btrfs_commit_transaction(trans, root); 1210d5719762SChris Mason } 1211d5719762SChris Mason 121234c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 1213a9572a15SEric Paris { 1214815745cfSAl Viro struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 1215200da64eSTsutomu Itoh char *compress_type; 1216a9572a15SEric Paris 12173cdde224SJeff Mahoney if (btrfs_test_opt(info, DEGRADED)) 1218a9572a15SEric Paris seq_puts(seq, ",degraded"); 12193cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATASUM)) 1220a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 12213cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATACOW)) 1222a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 12233cdde224SJeff Mahoney if (btrfs_test_opt(info, NOBARRIER)) 1224a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 122595ac567aSFilipe David Borba Manana if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE) 1226c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",max_inline=%llu", info->max_inline); 1227a9572a15SEric Paris if (info->alloc_start != 0) 1228c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",alloc_start=%llu", info->alloc_start); 1229a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 1230a9572a15SEric Paris num_online_cpus() + 2, 8)) 1231a9572a15SEric Paris seq_printf(seq, ",thread_pool=%d", info->thread_pool_size); 12323cdde224SJeff Mahoney if (btrfs_test_opt(info, COMPRESS)) { 1233200da64eSTsutomu Itoh if (info->compress_type == BTRFS_COMPRESS_ZLIB) 1234200da64eSTsutomu Itoh compress_type = "zlib"; 1235200da64eSTsutomu Itoh else 1236200da64eSTsutomu Itoh compress_type = "lzo"; 12373cdde224SJeff Mahoney if (btrfs_test_opt(info, FORCE_COMPRESS)) 1238200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 1239200da64eSTsutomu Itoh else 1240200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 1241200da64eSTsutomu Itoh } 12423cdde224SJeff Mahoney if (btrfs_test_opt(info, NOSSD)) 1243c289811cSChris Mason seq_puts(seq, ",nossd"); 12443cdde224SJeff Mahoney if (btrfs_test_opt(info, SSD_SPREAD)) 1245451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 12463cdde224SJeff Mahoney else if (btrfs_test_opt(info, SSD)) 1247a9572a15SEric Paris seq_puts(seq, ",ssd"); 12483cdde224SJeff Mahoney if (btrfs_test_opt(info, NOTREELOG)) 12496b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 12503cdde224SJeff Mahoney if (btrfs_test_opt(info, NOLOGREPLAY)) 125196da0919SQu Wenruo seq_puts(seq, ",nologreplay"); 12523cdde224SJeff Mahoney if (btrfs_test_opt(info, FLUSHONCOMMIT)) 12536b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 12543cdde224SJeff Mahoney if (btrfs_test_opt(info, DISCARD)) 125520a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 12560b246afaSJeff Mahoney if (!(info->sb->s_flags & MS_POSIXACL)) 1257a9572a15SEric Paris seq_puts(seq, ",noacl"); 12583cdde224SJeff Mahoney if (btrfs_test_opt(info, SPACE_CACHE)) 1259200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 12603cdde224SJeff Mahoney else if (btrfs_test_opt(info, FREE_SPACE_TREE)) 126170f6d82eSOmar Sandoval seq_puts(seq, ",space_cache=v2"); 126273bc1876SJosef Bacik else 12638965593eSDavid Sterba seq_puts(seq, ",nospace_cache"); 12643cdde224SJeff Mahoney if (btrfs_test_opt(info, RESCAN_UUID_TREE)) 1265f420ee1eSStefan Behrens seq_puts(seq, ",rescan_uuid_tree"); 12663cdde224SJeff Mahoney if (btrfs_test_opt(info, CLEAR_CACHE)) 1267200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 12683cdde224SJeff Mahoney if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED)) 1269200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 12703cdde224SJeff Mahoney if (btrfs_test_opt(info, ENOSPC_DEBUG)) 12710942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 12723cdde224SJeff Mahoney if (btrfs_test_opt(info, AUTO_DEFRAG)) 12730942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 12743cdde224SJeff Mahoney if (btrfs_test_opt(info, INODE_MAP_CACHE)) 12750942caa3SDavid Sterba seq_puts(seq, ",inode_cache"); 12763cdde224SJeff Mahoney if (btrfs_test_opt(info, SKIP_BALANCE)) 12779555c6c1SIlya Dryomov seq_puts(seq, ",skip_balance"); 12788507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 12793cdde224SJeff Mahoney if (btrfs_test_opt(info, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA)) 12808507d216SWang Shilong seq_puts(seq, ",check_int_data"); 12813cdde224SJeff Mahoney else if (btrfs_test_opt(info, CHECK_INTEGRITY)) 12828507d216SWang Shilong seq_puts(seq, ",check_int"); 12838507d216SWang Shilong if (info->check_integrity_print_mask) 12848507d216SWang Shilong seq_printf(seq, ",check_int_print_mask=%d", 12858507d216SWang Shilong info->check_integrity_print_mask); 12868507d216SWang Shilong #endif 12878507d216SWang Shilong if (info->metadata_ratio) 12888507d216SWang Shilong seq_printf(seq, ",metadata_ratio=%d", 12898507d216SWang Shilong info->metadata_ratio); 12903cdde224SJeff Mahoney if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR)) 12918c342930SJeff Mahoney seq_puts(seq, ",fatal_errors=panic"); 12928b87dc17SDavid Sterba if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL) 12938b87dc17SDavid Sterba seq_printf(seq, ",commit=%d", info->commit_interval); 1294d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 12953cdde224SJeff Mahoney if (btrfs_test_opt(info, FRAGMENT_DATA)) 1296d0bd4560SJosef Bacik seq_puts(seq, ",fragment=data"); 12973cdde224SJeff Mahoney if (btrfs_test_opt(info, FRAGMENT_METADATA)) 1298d0bd4560SJosef Bacik seq_puts(seq, ",fragment=metadata"); 1299d0bd4560SJosef Bacik #endif 1300c8d3fe02SOmar Sandoval seq_printf(seq, ",subvolid=%llu", 1301c8d3fe02SOmar Sandoval BTRFS_I(d_inode(dentry))->root->root_key.objectid); 1302c8d3fe02SOmar Sandoval seq_puts(seq, ",subvol="); 1303c8d3fe02SOmar Sandoval seq_dentry(seq, dentry, " \t\n\\"); 1304a9572a15SEric Paris return 0; 1305a9572a15SEric Paris } 1306a9572a15SEric Paris 1307a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 13082e635a27SChris Mason { 1309815745cfSAl Viro struct btrfs_fs_info *p = data; 1310815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(s); 13114b82d6e4SYan 1312815745cfSAl Viro return fs_info->fs_devices == p->fs_devices; 13134b82d6e4SYan } 13144b82d6e4SYan 1315450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 1316450ba0eaSJosef Bacik { 13176de1d09dSAl Viro int err = set_anon_super(s, data); 13186de1d09dSAl Viro if (!err) 1319450ba0eaSJosef Bacik s->s_fs_info = data; 13206de1d09dSAl Viro return err; 1321450ba0eaSJosef Bacik } 1322450ba0eaSJosef Bacik 1323830c4adbSJosef Bacik /* 1324f9d9ef62SDavid Sterba * subvolumes are identified by ino 256 1325f9d9ef62SDavid Sterba */ 1326f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode) 1327f9d9ef62SDavid Sterba { 1328f9d9ef62SDavid Sterba if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 1329f9d9ef62SDavid Sterba return 1; 1330f9d9ef62SDavid Sterba return 0; 1331f9d9ef62SDavid Sterba } 1332f9d9ef62SDavid Sterba 1333f9d9ef62SDavid Sterba /* 1334e6e4dbe8SOmar Sandoval * This will add subvolid=0 to the argument string while removing any subvol= 1335e6e4dbe8SOmar Sandoval * and subvolid= arguments to make sure we get the top-level root for path 1336e6e4dbe8SOmar Sandoval * walking to the subvol we want. 1337830c4adbSJosef Bacik */ 1338830c4adbSJosef Bacik static char *setup_root_args(char *args) 1339830c4adbSJosef Bacik { 1340e6e4dbe8SOmar Sandoval char *buf, *dst, *sep; 1341830c4adbSJosef Bacik 1342e6e4dbe8SOmar Sandoval if (!args) 1343e6e4dbe8SOmar Sandoval return kstrdup("subvolid=0", GFP_NOFS); 1344830c4adbSJosef Bacik 1345e6e4dbe8SOmar Sandoval /* The worst case is that we add ",subvolid=0" to the end. */ 1346e6e4dbe8SOmar Sandoval buf = dst = kmalloc(strlen(args) + strlen(",subvolid=0") + 1, GFP_NOFS); 1347f60d16a8SJim Meyering if (!buf) 1348f60d16a8SJim Meyering return NULL; 1349830c4adbSJosef Bacik 1350e6e4dbe8SOmar Sandoval while (1) { 1351e6e4dbe8SOmar Sandoval sep = strchrnul(args, ','); 1352e6e4dbe8SOmar Sandoval if (!strstarts(args, "subvol=") && 1353e6e4dbe8SOmar Sandoval !strstarts(args, "subvolid=")) { 1354e6e4dbe8SOmar Sandoval memcpy(dst, args, sep - args); 1355e6e4dbe8SOmar Sandoval dst += sep - args; 1356e6e4dbe8SOmar Sandoval *dst++ = ','; 1357830c4adbSJosef Bacik } 1358e6e4dbe8SOmar Sandoval if (*sep) 1359e6e4dbe8SOmar Sandoval args = sep + 1; 1360e6e4dbe8SOmar Sandoval else 1361e6e4dbe8SOmar Sandoval break; 1362e6e4dbe8SOmar Sandoval } 1363f60d16a8SJim Meyering strcpy(dst, "subvolid=0"); 1364830c4adbSJosef Bacik 1365f60d16a8SJim Meyering return buf; 1366830c4adbSJosef Bacik } 1367830c4adbSJosef Bacik 1368bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid, 1369bb289b7bSOmar Sandoval int flags, const char *device_name, 1370bb289b7bSOmar Sandoval char *data) 1371830c4adbSJosef Bacik { 1372830c4adbSJosef Bacik struct dentry *root; 1373fa330659SOmar Sandoval struct vfsmount *mnt = NULL; 1374830c4adbSJosef Bacik char *newargs; 1375fa330659SOmar Sandoval int ret; 1376830c4adbSJosef Bacik 1377830c4adbSJosef Bacik newargs = setup_root_args(data); 1378fa330659SOmar Sandoval if (!newargs) { 1379fa330659SOmar Sandoval root = ERR_PTR(-ENOMEM); 1380fa330659SOmar Sandoval goto out; 1381fa330659SOmar Sandoval } 13820723a047SHarald Hoyer 1383fa330659SOmar Sandoval mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, newargs); 1384fa330659SOmar Sandoval if (PTR_ERR_OR_ZERO(mnt) == -EBUSY) { 13850723a047SHarald Hoyer if (flags & MS_RDONLY) { 1386fa330659SOmar Sandoval mnt = vfs_kern_mount(&btrfs_fs_type, flags & ~MS_RDONLY, 1387fa330659SOmar Sandoval device_name, newargs); 13880723a047SHarald Hoyer } else { 1389fa330659SOmar Sandoval mnt = vfs_kern_mount(&btrfs_fs_type, flags | MS_RDONLY, 1390fa330659SOmar Sandoval device_name, newargs); 13910040e606SChristoph Jaeger if (IS_ERR(mnt)) { 1392fa330659SOmar Sandoval root = ERR_CAST(mnt); 1393fa330659SOmar Sandoval mnt = NULL; 1394fa330659SOmar Sandoval goto out; 13950040e606SChristoph Jaeger } 13960723a047SHarald Hoyer 1397773cd04eSOmar Sandoval down_write(&mnt->mnt_sb->s_umount); 1398fa330659SOmar Sandoval ret = btrfs_remount(mnt->mnt_sb, &flags, NULL); 1399773cd04eSOmar Sandoval up_write(&mnt->mnt_sb->s_umount); 1400fa330659SOmar Sandoval if (ret < 0) { 1401fa330659SOmar Sandoval root = ERR_PTR(ret); 1402fa330659SOmar Sandoval goto out; 14030723a047SHarald Hoyer } 14040723a047SHarald Hoyer } 14050723a047SHarald Hoyer } 1406fa330659SOmar Sandoval if (IS_ERR(mnt)) { 1407fa330659SOmar Sandoval root = ERR_CAST(mnt); 1408fa330659SOmar Sandoval mnt = NULL; 1409fa330659SOmar Sandoval goto out; 1410fa330659SOmar Sandoval } 1411830c4adbSJosef Bacik 141205dbe683SOmar Sandoval if (!subvol_name) { 141305dbe683SOmar Sandoval if (!subvol_objectid) { 141405dbe683SOmar Sandoval ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb), 141505dbe683SOmar Sandoval &subvol_objectid); 141605dbe683SOmar Sandoval if (ret) { 141705dbe683SOmar Sandoval root = ERR_PTR(ret); 141805dbe683SOmar Sandoval goto out; 141905dbe683SOmar Sandoval } 142005dbe683SOmar Sandoval } 142105dbe683SOmar Sandoval subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb), 142205dbe683SOmar Sandoval subvol_objectid); 142305dbe683SOmar Sandoval if (IS_ERR(subvol_name)) { 142405dbe683SOmar Sandoval root = ERR_CAST(subvol_name); 142505dbe683SOmar Sandoval subvol_name = NULL; 142605dbe683SOmar Sandoval goto out; 142705dbe683SOmar Sandoval } 142805dbe683SOmar Sandoval 142905dbe683SOmar Sandoval } 143005dbe683SOmar Sandoval 1431ea441d11SAl Viro root = mount_subtree(mnt, subvol_name); 1432fa330659SOmar Sandoval /* mount_subtree() drops our reference on the vfsmount. */ 1433fa330659SOmar Sandoval mnt = NULL; 1434830c4adbSJosef Bacik 1435bb289b7bSOmar Sandoval if (!IS_ERR(root)) { 1436ea441d11SAl Viro struct super_block *s = root->d_sb; 1437ab8d0fc4SJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(s); 1438bb289b7bSOmar Sandoval struct inode *root_inode = d_inode(root); 1439bb289b7bSOmar Sandoval u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid; 1440bb289b7bSOmar Sandoval 1441bb289b7bSOmar Sandoval ret = 0; 1442bb289b7bSOmar Sandoval if (!is_subvolume_inode(root_inode)) { 1443ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "'%s' is not a valid subvolume", 1444bb289b7bSOmar Sandoval subvol_name); 1445bb289b7bSOmar Sandoval ret = -EINVAL; 1446bb289b7bSOmar Sandoval } 1447bb289b7bSOmar Sandoval if (subvol_objectid && root_objectid != subvol_objectid) { 144805dbe683SOmar Sandoval /* 144905dbe683SOmar Sandoval * This will also catch a race condition where a 145005dbe683SOmar Sandoval * subvolume which was passed by ID is renamed and 145105dbe683SOmar Sandoval * another subvolume is renamed over the old location. 145205dbe683SOmar Sandoval */ 1453ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 1454ab8d0fc4SJeff Mahoney "subvol '%s' does not match subvolid %llu", 1455bb289b7bSOmar Sandoval subvol_name, subvol_objectid); 1456bb289b7bSOmar Sandoval ret = -EINVAL; 1457bb289b7bSOmar Sandoval } 1458bb289b7bSOmar Sandoval if (ret) { 1459ea441d11SAl Viro dput(root); 1460bb289b7bSOmar Sandoval root = ERR_PTR(ret); 1461ea441d11SAl Viro deactivate_locked_super(s); 1462bb289b7bSOmar Sandoval } 1463f9d9ef62SDavid Sterba } 1464f9d9ef62SDavid Sterba 1465fa330659SOmar Sandoval out: 1466fa330659SOmar Sandoval mntput(mnt); 1467fa330659SOmar Sandoval kfree(newargs); 1468fa330659SOmar Sandoval kfree(subvol_name); 1469830c4adbSJosef Bacik return root; 1470830c4adbSJosef Bacik } 1471450ba0eaSJosef Bacik 1472f667aef6SQu Wenruo static int parse_security_options(char *orig_opts, 1473f667aef6SQu Wenruo struct security_mnt_opts *sec_opts) 1474f667aef6SQu Wenruo { 1475f667aef6SQu Wenruo char *secdata = NULL; 1476f667aef6SQu Wenruo int ret = 0; 1477f667aef6SQu Wenruo 1478f667aef6SQu Wenruo secdata = alloc_secdata(); 1479f667aef6SQu Wenruo if (!secdata) 1480f667aef6SQu Wenruo return -ENOMEM; 1481f667aef6SQu Wenruo ret = security_sb_copy_data(orig_opts, secdata); 1482f667aef6SQu Wenruo if (ret) { 1483f667aef6SQu Wenruo free_secdata(secdata); 1484f667aef6SQu Wenruo return ret; 1485f667aef6SQu Wenruo } 1486f667aef6SQu Wenruo ret = security_sb_parse_opts_str(secdata, sec_opts); 1487f667aef6SQu Wenruo free_secdata(secdata); 1488f667aef6SQu Wenruo return ret; 1489f667aef6SQu Wenruo } 1490f667aef6SQu Wenruo 1491f667aef6SQu Wenruo static int setup_security_options(struct btrfs_fs_info *fs_info, 1492f667aef6SQu Wenruo struct super_block *sb, 1493f667aef6SQu Wenruo struct security_mnt_opts *sec_opts) 1494f667aef6SQu Wenruo { 1495f667aef6SQu Wenruo int ret = 0; 1496f667aef6SQu Wenruo 1497f667aef6SQu Wenruo /* 1498f667aef6SQu Wenruo * Call security_sb_set_mnt_opts() to check whether new sec_opts 1499f667aef6SQu Wenruo * is valid. 1500f667aef6SQu Wenruo */ 1501f667aef6SQu Wenruo ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL); 1502f667aef6SQu Wenruo if (ret) 1503f667aef6SQu Wenruo return ret; 1504f667aef6SQu Wenruo 1505a43bb39bSQu Wenruo #ifdef CONFIG_SECURITY 1506f667aef6SQu Wenruo if (!fs_info->security_opts.num_mnt_opts) { 1507f667aef6SQu Wenruo /* first time security setup, copy sec_opts to fs_info */ 1508f667aef6SQu Wenruo memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts)); 1509f667aef6SQu Wenruo } else { 1510f667aef6SQu Wenruo /* 1511180e4d47SLuis de Bethencourt * Since SELinux (the only one supporting security_mnt_opts) 1512180e4d47SLuis de Bethencourt * does NOT support changing context during remount/mount of 1513180e4d47SLuis de Bethencourt * the same sb, this must be the same or part of the same 1514180e4d47SLuis de Bethencourt * security options, just free it. 1515f667aef6SQu Wenruo */ 1516f667aef6SQu Wenruo security_free_mnt_opts(sec_opts); 1517f667aef6SQu Wenruo } 1518a43bb39bSQu Wenruo #endif 1519f667aef6SQu Wenruo return ret; 1520f667aef6SQu Wenruo } 1521f667aef6SQu Wenruo 1522edf24abeSChristoph Hellwig /* 1523edf24abeSChristoph Hellwig * Find a superblock for the given device / mount point. 1524edf24abeSChristoph Hellwig * 1525edf24abeSChristoph Hellwig * Note: This is based on get_sb_bdev from fs/super.c with a few additions 1526edf24abeSChristoph Hellwig * for multiple device setup. Make sure to keep it in sync. 1527edf24abeSChristoph Hellwig */ 1528061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1529306e16ceSDavid Sterba const char *device_name, void *data) 15304b82d6e4SYan { 15314b82d6e4SYan struct block_device *bdev = NULL; 15324b82d6e4SYan struct super_block *s; 15338a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices = NULL; 1534450ba0eaSJosef Bacik struct btrfs_fs_info *fs_info = NULL; 1535f667aef6SQu Wenruo struct security_mnt_opts new_sec_opts; 153697288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 153773f73415SJosef Bacik char *subvol_name = NULL; 153873f73415SJosef Bacik u64 subvol_objectid = 0; 15394b82d6e4SYan int error = 0; 15404b82d6e4SYan 154197288f2cSChristoph Hellwig if (!(flags & MS_RDONLY)) 154297288f2cSChristoph Hellwig mode |= FMODE_WRITE; 154397288f2cSChristoph Hellwig 154497288f2cSChristoph Hellwig error = btrfs_parse_early_options(data, mode, fs_type, 154573f73415SJosef Bacik &subvol_name, &subvol_objectid, 15465e2a4b25SDavid Sterba &fs_devices); 1547f23c8af8SIlya Dryomov if (error) { 1548f23c8af8SIlya Dryomov kfree(subvol_name); 1549061dbc6bSAl Viro return ERR_PTR(error); 1550f23c8af8SIlya Dryomov } 1551edf24abeSChristoph Hellwig 155205dbe683SOmar Sandoval if (subvol_name || subvol_objectid != BTRFS_FS_TREE_OBJECTID) { 1553fa330659SOmar Sandoval /* mount_subvol() will free subvol_name. */ 1554bb289b7bSOmar Sandoval return mount_subvol(subvol_name, subvol_objectid, flags, 1555bb289b7bSOmar Sandoval device_name, data); 1556830c4adbSJosef Bacik } 1557830c4adbSJosef Bacik 1558f667aef6SQu Wenruo security_init_mnt_opts(&new_sec_opts); 1559f667aef6SQu Wenruo if (data) { 1560f667aef6SQu Wenruo error = parse_security_options(data, &new_sec_opts); 15618a4b83ccSChris Mason if (error) 1562830c4adbSJosef Bacik return ERR_PTR(error); 1563f667aef6SQu Wenruo } 1564f667aef6SQu Wenruo 1565f667aef6SQu Wenruo error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices); 1566f667aef6SQu Wenruo if (error) 1567f667aef6SQu Wenruo goto error_sec_opts; 15684b82d6e4SYan 1569450ba0eaSJosef Bacik /* 1570450ba0eaSJosef Bacik * Setup a dummy root and fs_info for test/set super. This is because 1571450ba0eaSJosef Bacik * we don't actually fill this stuff out until open_ctree, but we need 1572450ba0eaSJosef Bacik * it for searching for existing supers, so this lets us do that and 1573450ba0eaSJosef Bacik * then open_ctree will properly initialize everything later. 1574450ba0eaSJosef Bacik */ 1575450ba0eaSJosef Bacik fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS); 1576f667aef6SQu Wenruo if (!fs_info) { 1577f667aef6SQu Wenruo error = -ENOMEM; 1578f667aef6SQu Wenruo goto error_sec_opts; 1579f667aef6SQu Wenruo } 158004d21a24SIlya Dryomov 1581450ba0eaSJosef Bacik fs_info->fs_devices = fs_devices; 1582450ba0eaSJosef Bacik 15836c41761fSDavid Sterba fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 15846c41761fSDavid Sterba fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 1585f667aef6SQu Wenruo security_init_mnt_opts(&fs_info->security_opts); 15866c41761fSDavid Sterba if (!fs_info->super_copy || !fs_info->super_for_commit) { 15876c41761fSDavid Sterba error = -ENOMEM; 158804d21a24SIlya Dryomov goto error_fs_info; 158904d21a24SIlya Dryomov } 159004d21a24SIlya Dryomov 159104d21a24SIlya Dryomov error = btrfs_open_devices(fs_devices, mode, fs_type); 159204d21a24SIlya Dryomov if (error) 159304d21a24SIlya Dryomov goto error_fs_info; 159404d21a24SIlya Dryomov 159504d21a24SIlya Dryomov if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) { 159604d21a24SIlya Dryomov error = -EACCES; 15976c41761fSDavid Sterba goto error_close_devices; 15986c41761fSDavid Sterba } 15996c41761fSDavid Sterba 1600dfe25020SChris Mason bdev = fs_devices->latest_bdev; 16019249e17fSDavid Howells s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC, 16029249e17fSDavid Howells fs_info); 1603830c4adbSJosef Bacik if (IS_ERR(s)) { 1604830c4adbSJosef Bacik error = PTR_ERR(s); 1605830c4adbSJosef Bacik goto error_close_devices; 1606830c4adbSJosef Bacik } 16074b82d6e4SYan 16084b82d6e4SYan if (s->s_root) { 16092b82032cSYan Zheng btrfs_close_devices(fs_devices); 16106c41761fSDavid Sterba free_fs_info(fs_info); 161159553edfSAl Viro if ((flags ^ s->s_flags) & MS_RDONLY) 161259553edfSAl Viro error = -EBUSY; 16134b82d6e4SYan } else { 1614a1c6f057SDmitry Monakhov snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev); 1615815745cfSAl Viro btrfs_sb(s)->bdev_holder = fs_type; 16168a4b83ccSChris Mason error = btrfs_fill_super(s, fs_devices, data, 16178a4b83ccSChris Mason flags & MS_SILENT ? 1 : 0); 16184b82d6e4SYan } 161905dbe683SOmar Sandoval if (error) { 1620e15d0542SXin Zhong deactivate_locked_super(s); 1621f667aef6SQu Wenruo goto error_sec_opts; 1622f667aef6SQu Wenruo } 1623f667aef6SQu Wenruo 1624f667aef6SQu Wenruo fs_info = btrfs_sb(s); 1625f667aef6SQu Wenruo error = setup_security_options(fs_info, s, &new_sec_opts); 1626f667aef6SQu Wenruo if (error) { 1627f667aef6SQu Wenruo deactivate_locked_super(s); 1628f667aef6SQu Wenruo goto error_sec_opts; 1629f667aef6SQu Wenruo } 16304b82d6e4SYan 163105dbe683SOmar Sandoval return dget(s->s_root); 16324b82d6e4SYan 1633c146afadSYan Zheng error_close_devices: 16348a4b83ccSChris Mason btrfs_close_devices(fs_devices); 163504d21a24SIlya Dryomov error_fs_info: 16366c41761fSDavid Sterba free_fs_info(fs_info); 1637f667aef6SQu Wenruo error_sec_opts: 1638f667aef6SQu Wenruo security_free_mnt_opts(&new_sec_opts); 1639061dbc6bSAl Viro return ERR_PTR(error); 16404b82d6e4SYan } 16412e635a27SChris Mason 16420d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 16430d2450abSSergei Trofimovich int new_pool_size, int old_pool_size) 16440d2450abSSergei Trofimovich { 16450d2450abSSergei Trofimovich if (new_pool_size == old_pool_size) 16460d2450abSSergei Trofimovich return; 16470d2450abSSergei Trofimovich 16480d2450abSSergei Trofimovich fs_info->thread_pool_size = new_pool_size; 16490d2450abSSergei Trofimovich 1650efe120a0SFrank Holton btrfs_info(fs_info, "resize thread pool %d -> %d", 16510d2450abSSergei Trofimovich old_pool_size, new_pool_size); 16520d2450abSSergei Trofimovich 16535cdc7ad3SQu Wenruo btrfs_workqueue_set_max(fs_info->workers, new_pool_size); 1654afe3d242SQu Wenruo btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size); 1655a8c93d4eSQu Wenruo btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size); 1656e66f0bb1SQu Wenruo btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size); 1657fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size); 1658fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size); 1659fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_write_workers, 1660fccb5d86SQu Wenruo new_pool_size); 1661fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size); 1662fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size); 16635b3bc44eSQu Wenruo btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size); 1664736cfa15SQu Wenruo btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size); 16650339ef2fSQu Wenruo btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers, 1666ff023aacSStefan Behrens new_pool_size); 16670d2450abSSergei Trofimovich } 16680d2450abSSergei Trofimovich 1669f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info) 1670dc81cdc5SMiao Xie { 1671dc81cdc5SMiao Xie set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1672f42a34b2SMiao Xie } 1673dc81cdc5SMiao Xie 1674f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info, 1675f42a34b2SMiao Xie unsigned long old_opts, int flags) 1676f42a34b2SMiao Xie { 1677dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1678dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1679dc81cdc5SMiao Xie (flags & MS_RDONLY))) { 1680dc81cdc5SMiao Xie /* wait for any defraggers to finish */ 1681dc81cdc5SMiao Xie wait_event(fs_info->transaction_wait, 1682dc81cdc5SMiao Xie (atomic_read(&fs_info->defrag_running) == 0)); 1683dc81cdc5SMiao Xie if (flags & MS_RDONLY) 1684dc81cdc5SMiao Xie sync_filesystem(fs_info->sb); 1685dc81cdc5SMiao Xie } 1686dc81cdc5SMiao Xie } 1687dc81cdc5SMiao Xie 1688dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info, 1689dc81cdc5SMiao Xie unsigned long old_opts) 1690dc81cdc5SMiao Xie { 1691dc81cdc5SMiao Xie /* 1692180e4d47SLuis de Bethencourt * We need to cleanup all defragable inodes if the autodefragment is 1693180e4d47SLuis de Bethencourt * close or the filesystem is read only. 1694dc81cdc5SMiao Xie */ 1695dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1696dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1697dc81cdc5SMiao Xie (fs_info->sb->s_flags & MS_RDONLY))) { 1698dc81cdc5SMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 1699dc81cdc5SMiao Xie } 1700dc81cdc5SMiao Xie 1701dc81cdc5SMiao Xie clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1702dc81cdc5SMiao Xie } 1703dc81cdc5SMiao Xie 1704c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1705c146afadSYan Zheng { 1706815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1707815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 170849b25e05SJeff Mahoney unsigned old_flags = sb->s_flags; 170949b25e05SJeff Mahoney unsigned long old_opts = fs_info->mount_opt; 171049b25e05SJeff Mahoney unsigned long old_compress_type = fs_info->compress_type; 171149b25e05SJeff Mahoney u64 old_max_inline = fs_info->max_inline; 171249b25e05SJeff Mahoney u64 old_alloc_start = fs_info->alloc_start; 171349b25e05SJeff Mahoney int old_thread_pool_size = fs_info->thread_pool_size; 171449b25e05SJeff Mahoney unsigned int old_metadata_ratio = fs_info->metadata_ratio; 1715c146afadSYan Zheng int ret; 1716c146afadSYan Zheng 171702b9984dSTheodore Ts'o sync_filesystem(sb); 1718f42a34b2SMiao Xie btrfs_remount_prepare(fs_info); 1719dc81cdc5SMiao Xie 1720f667aef6SQu Wenruo if (data) { 1721f667aef6SQu Wenruo struct security_mnt_opts new_sec_opts; 1722f667aef6SQu Wenruo 1723f667aef6SQu Wenruo security_init_mnt_opts(&new_sec_opts); 1724f667aef6SQu Wenruo ret = parse_security_options(data, &new_sec_opts); 1725f667aef6SQu Wenruo if (ret) 1726f667aef6SQu Wenruo goto restore; 1727f667aef6SQu Wenruo ret = setup_security_options(fs_info, sb, 1728f667aef6SQu Wenruo &new_sec_opts); 1729f667aef6SQu Wenruo if (ret) { 1730f667aef6SQu Wenruo security_free_mnt_opts(&new_sec_opts); 1731f667aef6SQu Wenruo goto restore; 1732f667aef6SQu Wenruo } 1733f667aef6SQu Wenruo } 1734f667aef6SQu Wenruo 1735*2ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, data, *flags); 173649b25e05SJeff Mahoney if (ret) { 173749b25e05SJeff Mahoney ret = -EINVAL; 173849b25e05SJeff Mahoney goto restore; 173949b25e05SJeff Mahoney } 1740b288052eSChris Mason 1741f42a34b2SMiao Xie btrfs_remount_begin(fs_info, old_opts, *flags); 17420d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 17430d2450abSSergei Trofimovich fs_info->thread_pool_size, old_thread_pool_size); 17440d2450abSSergei Trofimovich 1745c146afadSYan Zheng if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 1746dc81cdc5SMiao Xie goto out; 1747c146afadSYan Zheng 1748c146afadSYan Zheng if (*flags & MS_RDONLY) { 17498dabb742SStefan Behrens /* 17508dabb742SStefan Behrens * this also happens on 'umount -rf' or on shutdown, when 17518dabb742SStefan Behrens * the filesystem is busy. 17528dabb742SStefan Behrens */ 175321c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 1754361c093dSStefan Behrens 1755361c093dSStefan Behrens /* wait for the uuid_scan task to finish */ 1756361c093dSStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 1757361c093dSStefan Behrens /* avoid complains from lockdep et al. */ 1758361c093dSStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 1759361c093dSStefan Behrens 1760c146afadSYan Zheng sb->s_flags |= MS_RDONLY; 1761c146afadSYan Zheng 1762e44163e1SJeff Mahoney /* 1763e44163e1SJeff Mahoney * Setting MS_RDONLY will put the cleaner thread to 1764e44163e1SJeff Mahoney * sleep at the next loop if it's already active. 1765e44163e1SJeff Mahoney * If it's already asleep, we'll leave unused block 1766e44163e1SJeff Mahoney * groups on disk until we're mounted read-write again 1767e44163e1SJeff Mahoney * unless we clean them up here. 1768e44163e1SJeff Mahoney */ 1769e44163e1SJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1770e44163e1SJeff Mahoney 17718dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 17728dabb742SStefan Behrens btrfs_scrub_cancel(fs_info); 1773061594efSMiao Xie btrfs_pause_balance(fs_info); 17748dabb742SStefan Behrens 17756bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 177649b25e05SJeff Mahoney if (ret) 177749b25e05SJeff Mahoney goto restore; 1778c146afadSYan Zheng } else { 17790b246afaSJeff Mahoney if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 17806ef3de9cSDavid Sterba btrfs_err(fs_info, 1781efe120a0SFrank Holton "Remounting read-write after error is not allowed"); 17826ef3de9cSDavid Sterba ret = -EINVAL; 17836ef3de9cSDavid Sterba goto restore; 17846ef3de9cSDavid Sterba } 17858a3db184SSergei Trofimovich if (fs_info->fs_devices->rw_devices == 0) { 178649b25e05SJeff Mahoney ret = -EACCES; 178749b25e05SJeff Mahoney goto restore; 17888a3db184SSergei Trofimovich } 17892b82032cSYan Zheng 1790292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 1791292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 1792292fd7fcSStefan Behrens !(*flags & MS_RDONLY)) { 1793efe120a0SFrank Holton btrfs_warn(fs_info, 1794efe120a0SFrank Holton "too many missing devices, writeable remount is not allowed"); 1795292fd7fcSStefan Behrens ret = -EACCES; 1796292fd7fcSStefan Behrens goto restore; 1797292fd7fcSStefan Behrens } 1798292fd7fcSStefan Behrens 17998a3db184SSergei Trofimovich if (btrfs_super_log_root(fs_info->super_copy) != 0) { 180049b25e05SJeff Mahoney ret = -EINVAL; 180149b25e05SJeff Mahoney goto restore; 18028a3db184SSergei Trofimovich } 1803c146afadSYan Zheng 1804815745cfSAl Viro ret = btrfs_cleanup_fs_roots(fs_info); 180549b25e05SJeff Mahoney if (ret) 180649b25e05SJeff Mahoney goto restore; 1807c146afadSYan Zheng 1808d68fc57bSYan, Zheng /* recover relocation */ 18095f316481SWang Shilong mutex_lock(&fs_info->cleaner_mutex); 1810d68fc57bSYan, Zheng ret = btrfs_recover_relocation(root); 18115f316481SWang Shilong mutex_unlock(&fs_info->cleaner_mutex); 181249b25e05SJeff Mahoney if (ret) 181349b25e05SJeff Mahoney goto restore; 1814c146afadSYan Zheng 18152b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 18162b6ba629SIlya Dryomov if (ret) 18172b6ba629SIlya Dryomov goto restore; 18182b6ba629SIlya Dryomov 18198dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 18208dabb742SStefan Behrens if (ret) { 1821efe120a0SFrank Holton btrfs_warn(fs_info, "failed to resume dev_replace"); 18228dabb742SStefan Behrens goto restore; 18238dabb742SStefan Behrens } 182494aebfb2SJosef Bacik 182594aebfb2SJosef Bacik if (!fs_info->uuid_root) { 1826efe120a0SFrank Holton btrfs_info(fs_info, "creating UUID tree"); 182794aebfb2SJosef Bacik ret = btrfs_create_uuid_tree(fs_info); 182894aebfb2SJosef Bacik if (ret) { 18295d163e0eSJeff Mahoney btrfs_warn(fs_info, 18305d163e0eSJeff Mahoney "failed to create the UUID tree %d", 18315d163e0eSJeff Mahoney ret); 183294aebfb2SJosef Bacik goto restore; 183394aebfb2SJosef Bacik } 183494aebfb2SJosef Bacik } 1835c146afadSYan Zheng sb->s_flags &= ~MS_RDONLY; 183690c711abSZygo Blaxell 1837afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 1838c146afadSYan Zheng } 1839dc81cdc5SMiao Xie out: 18402c6a92b0SJustin Maggard wake_up_process(fs_info->transaction_kthread); 1841dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 1842c146afadSYan Zheng return 0; 184349b25e05SJeff Mahoney 184449b25e05SJeff Mahoney restore: 184549b25e05SJeff Mahoney /* We've hit an error - don't reset MS_RDONLY */ 184649b25e05SJeff Mahoney if (sb->s_flags & MS_RDONLY) 184749b25e05SJeff Mahoney old_flags |= MS_RDONLY; 184849b25e05SJeff Mahoney sb->s_flags = old_flags; 184949b25e05SJeff Mahoney fs_info->mount_opt = old_opts; 185049b25e05SJeff Mahoney fs_info->compress_type = old_compress_type; 185149b25e05SJeff Mahoney fs_info->max_inline = old_max_inline; 1852c018daecSMiao Xie mutex_lock(&fs_info->chunk_mutex); 185349b25e05SJeff Mahoney fs_info->alloc_start = old_alloc_start; 1854c018daecSMiao Xie mutex_unlock(&fs_info->chunk_mutex); 18550d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 18560d2450abSSergei Trofimovich old_thread_pool_size, fs_info->thread_pool_size); 185749b25e05SJeff Mahoney fs_info->metadata_ratio = old_metadata_ratio; 1858dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 185949b25e05SJeff Mahoney return ret; 1860c146afadSYan Zheng } 1861c146afadSYan Zheng 1862bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 1863bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1, 1864bcd53741SArne Jansen const void *dev_info2) 1865bcd53741SArne Jansen { 1866bcd53741SArne Jansen if (((struct btrfs_device_info *)dev_info1)->max_avail > 1867bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1868bcd53741SArne Jansen return -1; 1869bcd53741SArne Jansen else if (((struct btrfs_device_info *)dev_info1)->max_avail < 1870bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1871bcd53741SArne Jansen return 1; 1872bcd53741SArne Jansen else 1873bcd53741SArne Jansen return 0; 1874bcd53741SArne Jansen } 1875bcd53741SArne Jansen 1876bcd53741SArne Jansen /* 1877bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 1878bcd53741SArne Jansen * is stored.(Descending Sort) 1879bcd53741SArne Jansen */ 1880bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 1881bcd53741SArne Jansen struct btrfs_device_info *devices, 1882bcd53741SArne Jansen size_t nr_devices) 1883bcd53741SArne Jansen { 1884bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 1885bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 1886bcd53741SArne Jansen } 1887bcd53741SArne Jansen 18886d07bcecSMiao Xie /* 18896d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 18906d07bcecSMiao Xie * file data. 18916d07bcecSMiao Xie */ 18926bccf3abSJeff Mahoney static int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info, 18936bccf3abSJeff Mahoney u64 *free_bytes) 18946d07bcecSMiao Xie { 18956bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 18966d07bcecSMiao Xie struct btrfs_device_info *devices_info; 18976d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 18986d07bcecSMiao Xie struct btrfs_device *device; 18996d07bcecSMiao Xie u64 skip_space; 19006d07bcecSMiao Xie u64 type; 19016d07bcecSMiao Xie u64 avail_space; 19026d07bcecSMiao Xie u64 used_space; 19036d07bcecSMiao Xie u64 min_stripe_size; 190439fb26c3SMiao Xie int min_stripes = 1, num_stripes = 1; 19056d07bcecSMiao Xie int i = 0, nr_devices; 19066d07bcecSMiao Xie int ret; 19076d07bcecSMiao Xie 19087e33fd99SJosef Bacik /* 190901327610SNicholas D Steeves * We aren't under the device list lock, so this is racy-ish, but good 19107e33fd99SJosef Bacik * enough for our purposes. 19117e33fd99SJosef Bacik */ 1912b772a86eSLi Zefan nr_devices = fs_info->fs_devices->open_devices; 19137e33fd99SJosef Bacik if (!nr_devices) { 19147e33fd99SJosef Bacik smp_mb(); 19157e33fd99SJosef Bacik nr_devices = fs_info->fs_devices->open_devices; 19167e33fd99SJosef Bacik ASSERT(nr_devices); 19177e33fd99SJosef Bacik if (!nr_devices) { 19187e33fd99SJosef Bacik *free_bytes = 0; 19197e33fd99SJosef Bacik return 0; 19207e33fd99SJosef Bacik } 19217e33fd99SJosef Bacik } 19226d07bcecSMiao Xie 1923d9b0d9baSDulshani Gunawardhana devices_info = kmalloc_array(nr_devices, sizeof(*devices_info), 19246d07bcecSMiao Xie GFP_NOFS); 19256d07bcecSMiao Xie if (!devices_info) 19266d07bcecSMiao Xie return -ENOMEM; 19276d07bcecSMiao Xie 192801327610SNicholas D Steeves /* calc min stripe number for data space allocation */ 19296d07bcecSMiao Xie type = btrfs_get_alloc_profile(root, 1); 193039fb26c3SMiao Xie if (type & BTRFS_BLOCK_GROUP_RAID0) { 19316d07bcecSMiao Xie min_stripes = 2; 193239fb26c3SMiao Xie num_stripes = nr_devices; 193339fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID1) { 19346d07bcecSMiao Xie min_stripes = 2; 193539fb26c3SMiao Xie num_stripes = 2; 193639fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID10) { 19376d07bcecSMiao Xie min_stripes = 4; 193839fb26c3SMiao Xie num_stripes = 4; 193939fb26c3SMiao Xie } 19406d07bcecSMiao Xie 19416d07bcecSMiao Xie if (type & BTRFS_BLOCK_GROUP_DUP) 19426d07bcecSMiao Xie min_stripe_size = 2 * BTRFS_STRIPE_LEN; 19436d07bcecSMiao Xie else 19446d07bcecSMiao Xie min_stripe_size = BTRFS_STRIPE_LEN; 19456d07bcecSMiao Xie 19467e33fd99SJosef Bacik if (fs_info->alloc_start) 19477e33fd99SJosef Bacik mutex_lock(&fs_devices->device_list_mutex); 19487e33fd99SJosef Bacik rcu_read_lock(); 19497e33fd99SJosef Bacik list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 195063a212abSStefan Behrens if (!device->in_fs_metadata || !device->bdev || 195163a212abSStefan Behrens device->is_tgtdev_for_dev_replace) 19526d07bcecSMiao Xie continue; 19536d07bcecSMiao Xie 19547e33fd99SJosef Bacik if (i >= nr_devices) 19557e33fd99SJosef Bacik break; 19567e33fd99SJosef Bacik 19576d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 19586d07bcecSMiao Xie 19596d07bcecSMiao Xie /* align with stripe_len */ 1960f8c269d7SDavid Sterba avail_space = div_u64(avail_space, BTRFS_STRIPE_LEN); 19616d07bcecSMiao Xie avail_space *= BTRFS_STRIPE_LEN; 19626d07bcecSMiao Xie 19636d07bcecSMiao Xie /* 196401327610SNicholas D Steeves * In order to avoid overwriting the superblock on the drive, 19656d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 19666d07bcecSMiao Xie * allocation. 19676d07bcecSMiao Xie */ 1968ee22184bSByongho Lee skip_space = SZ_1M; 19696d07bcecSMiao Xie 19706d07bcecSMiao Xie /* user can set the offset in fs_info->alloc_start. */ 19717e33fd99SJosef Bacik if (fs_info->alloc_start && 19727e33fd99SJosef Bacik fs_info->alloc_start + BTRFS_STRIPE_LEN <= 19737e33fd99SJosef Bacik device->total_bytes) { 19747e33fd99SJosef Bacik rcu_read_unlock(); 19756d07bcecSMiao Xie skip_space = max(fs_info->alloc_start, skip_space); 19766d07bcecSMiao Xie 19776d07bcecSMiao Xie /* 19787e33fd99SJosef Bacik * btrfs can not use the free space in 19797e33fd99SJosef Bacik * [0, skip_space - 1], we must subtract it from the 19807e33fd99SJosef Bacik * total. In order to implement it, we account the used 19817e33fd99SJosef Bacik * space in this range first. 19826d07bcecSMiao Xie */ 19837e33fd99SJosef Bacik ret = btrfs_account_dev_extents_size(device, 0, 19847e33fd99SJosef Bacik skip_space - 1, 19856d07bcecSMiao Xie &used_space); 19866d07bcecSMiao Xie if (ret) { 19876d07bcecSMiao Xie kfree(devices_info); 19887e33fd99SJosef Bacik mutex_unlock(&fs_devices->device_list_mutex); 19896d07bcecSMiao Xie return ret; 19906d07bcecSMiao Xie } 19916d07bcecSMiao Xie 19927e33fd99SJosef Bacik rcu_read_lock(); 19937e33fd99SJosef Bacik 19946d07bcecSMiao Xie /* calc the free space in [0, skip_space - 1] */ 19956d07bcecSMiao Xie skip_space -= used_space; 19967e33fd99SJosef Bacik } 19976d07bcecSMiao Xie 19986d07bcecSMiao Xie /* 19996d07bcecSMiao Xie * we can use the free space in [0, skip_space - 1], subtract 20006d07bcecSMiao Xie * it from the total. 20016d07bcecSMiao Xie */ 20026d07bcecSMiao Xie if (avail_space && avail_space >= skip_space) 20036d07bcecSMiao Xie avail_space -= skip_space; 20046d07bcecSMiao Xie else 20056d07bcecSMiao Xie avail_space = 0; 20066d07bcecSMiao Xie 20076d07bcecSMiao Xie if (avail_space < min_stripe_size) 20086d07bcecSMiao Xie continue; 20096d07bcecSMiao Xie 20106d07bcecSMiao Xie devices_info[i].dev = device; 20116d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 20126d07bcecSMiao Xie 20136d07bcecSMiao Xie i++; 20146d07bcecSMiao Xie } 20157e33fd99SJosef Bacik rcu_read_unlock(); 20167e33fd99SJosef Bacik if (fs_info->alloc_start) 20177e33fd99SJosef Bacik mutex_unlock(&fs_devices->device_list_mutex); 20186d07bcecSMiao Xie 20196d07bcecSMiao Xie nr_devices = i; 20206d07bcecSMiao Xie 20216d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 20226d07bcecSMiao Xie 20236d07bcecSMiao Xie i = nr_devices - 1; 20246d07bcecSMiao Xie avail_space = 0; 20256d07bcecSMiao Xie while (nr_devices >= min_stripes) { 202639fb26c3SMiao Xie if (num_stripes > nr_devices) 202739fb26c3SMiao Xie num_stripes = nr_devices; 202839fb26c3SMiao Xie 20296d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 20306d07bcecSMiao Xie int j; 20316d07bcecSMiao Xie u64 alloc_size; 20326d07bcecSMiao Xie 203339fb26c3SMiao Xie avail_space += devices_info[i].max_avail * num_stripes; 20346d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 203539fb26c3SMiao Xie for (j = i + 1 - num_stripes; j <= i; j++) 20366d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 20376d07bcecSMiao Xie } 20386d07bcecSMiao Xie i--; 20396d07bcecSMiao Xie nr_devices--; 20406d07bcecSMiao Xie } 20416d07bcecSMiao Xie 20426d07bcecSMiao Xie kfree(devices_info); 20436d07bcecSMiao Xie *free_bytes = avail_space; 20446d07bcecSMiao Xie return 0; 20456d07bcecSMiao Xie } 20466d07bcecSMiao Xie 2047ba7b6e62SDavid Sterba /* 2048ba7b6e62SDavid Sterba * Calculate numbers for 'df', pessimistic in case of mixed raid profiles. 2049ba7b6e62SDavid Sterba * 2050ba7b6e62SDavid Sterba * If there's a redundant raid level at DATA block groups, use the respective 2051ba7b6e62SDavid Sterba * multiplier to scale the sizes. 2052ba7b6e62SDavid Sterba * 2053ba7b6e62SDavid Sterba * Unused device space usage is based on simulating the chunk allocator 2054ba7b6e62SDavid Sterba * algorithm that respects the device sizes, order of allocations and the 2055ba7b6e62SDavid Sterba * 'alloc_start' value, this is a close approximation of the actual use but 2056ba7b6e62SDavid Sterba * there are other factors that may change the result (like a new metadata 2057ba7b6e62SDavid Sterba * chunk). 2058ba7b6e62SDavid Sterba * 2059ca8a51b3SDavid Sterba * If metadata is exhausted, f_bavail will be 0. 2060ba7b6e62SDavid Sterba */ 20618fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 20628fd17795SChris Mason { 2063815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 2064815745cfSAl Viro struct btrfs_super_block *disk_super = fs_info->super_copy; 2065815745cfSAl Viro struct list_head *head = &fs_info->space_info; 2066bd4d1088SJosef Bacik struct btrfs_space_info *found; 2067bd4d1088SJosef Bacik u64 total_used = 0; 20686d07bcecSMiao Xie u64 total_free_data = 0; 2069ca8a51b3SDavid Sterba u64 total_free_meta = 0; 2070db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 2071815745cfSAl Viro __be32 *fsid = (__be32 *)fs_info->fsid; 2072ba7b6e62SDavid Sterba unsigned factor = 1; 2073ba7b6e62SDavid Sterba struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 20746d07bcecSMiao Xie int ret; 2075ca8a51b3SDavid Sterba u64 thresh = 0; 2076ae02d1bdSLuis de Bethencourt int mixed = 0; 20778fd17795SChris Mason 2078bd4d1088SJosef Bacik rcu_read_lock(); 207989a55897SJosef Bacik list_for_each_entry_rcu(found, head, list) { 20806d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 2081ba7b6e62SDavid Sterba int i; 2082ba7b6e62SDavid Sterba 20836d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 20846d07bcecSMiao Xie total_free_data -= 20856d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 2086ba7b6e62SDavid Sterba 2087ba7b6e62SDavid Sterba for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { 2088ba7b6e62SDavid Sterba if (!list_empty(&found->block_groups[i])) { 2089ba7b6e62SDavid Sterba switch (i) { 2090ba7b6e62SDavid Sterba case BTRFS_RAID_DUP: 2091ba7b6e62SDavid Sterba case BTRFS_RAID_RAID1: 2092ba7b6e62SDavid Sterba case BTRFS_RAID_RAID10: 2093ba7b6e62SDavid Sterba factor = 2; 2094ba7b6e62SDavid Sterba } 2095ba7b6e62SDavid Sterba } 2096ba7b6e62SDavid Sterba } 20976d07bcecSMiao Xie } 2098ae02d1bdSLuis de Bethencourt 2099ae02d1bdSLuis de Bethencourt /* 2100ae02d1bdSLuis de Bethencourt * Metadata in mixed block goup profiles are accounted in data 2101ae02d1bdSLuis de Bethencourt */ 2102ae02d1bdSLuis de Bethencourt if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) { 2103ae02d1bdSLuis de Bethencourt if (found->flags & BTRFS_BLOCK_GROUP_DATA) 2104ae02d1bdSLuis de Bethencourt mixed = 1; 2105ae02d1bdSLuis de Bethencourt else 2106ae02d1bdSLuis de Bethencourt total_free_meta += found->disk_total - 2107ae02d1bdSLuis de Bethencourt found->disk_used; 2108ae02d1bdSLuis de Bethencourt } 21096d07bcecSMiao Xie 2110b742bb82SYan, Zheng total_used += found->disk_used; 211189a55897SJosef Bacik } 2112ba7b6e62SDavid Sterba 2113bd4d1088SJosef Bacik rcu_read_unlock(); 2114bd4d1088SJosef Bacik 2115ba7b6e62SDavid Sterba buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor); 2116ba7b6e62SDavid Sterba buf->f_blocks >>= bits; 2117ba7b6e62SDavid Sterba buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits); 2118ba7b6e62SDavid Sterba 2119ba7b6e62SDavid Sterba /* Account global block reserve as used, it's in logical size already */ 2120ba7b6e62SDavid Sterba spin_lock(&block_rsv->lock); 212141b34accSLuis de Bethencourt /* Mixed block groups accounting is not byte-accurate, avoid overflow */ 212241b34accSLuis de Bethencourt if (buf->f_bfree >= block_rsv->size >> bits) 2123ba7b6e62SDavid Sterba buf->f_bfree -= block_rsv->size >> bits; 212441b34accSLuis de Bethencourt else 212541b34accSLuis de Bethencourt buf->f_bfree = 0; 2126ba7b6e62SDavid Sterba spin_unlock(&block_rsv->lock); 2127ba7b6e62SDavid Sterba 21280d95c1beSDavid Sterba buf->f_bavail = div_u64(total_free_data, factor); 21296bccf3abSJeff Mahoney ret = btrfs_calc_avail_data_space(fs_info, &total_free_data); 21307e33fd99SJosef Bacik if (ret) 21316d07bcecSMiao Xie return ret; 2132ba7b6e62SDavid Sterba buf->f_bavail += div_u64(total_free_data, factor); 21336d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 2134d397712bSChris Mason 2135ca8a51b3SDavid Sterba /* 2136ca8a51b3SDavid Sterba * We calculate the remaining metadata space minus global reserve. If 2137ca8a51b3SDavid Sterba * this is (supposedly) smaller than zero, there's no space. But this 2138ca8a51b3SDavid Sterba * does not hold in practice, the exhausted state happens where's still 2139ca8a51b3SDavid Sterba * some positive delta. So we apply some guesswork and compare the 2140ca8a51b3SDavid Sterba * delta to a 4M threshold. (Practically observed delta was ~2M.) 2141ca8a51b3SDavid Sterba * 2142ca8a51b3SDavid Sterba * We probably cannot calculate the exact threshold value because this 2143ca8a51b3SDavid Sterba * depends on the internal reservations requested by various 2144ca8a51b3SDavid Sterba * operations, so some operations that consume a few metadata will 2145ca8a51b3SDavid Sterba * succeed even if the Avail is zero. But this is better than the other 2146ca8a51b3SDavid Sterba * way around. 2147ca8a51b3SDavid Sterba */ 2148ca8a51b3SDavid Sterba thresh = 4 * 1024 * 1024; 2149ca8a51b3SDavid Sterba 2150ae02d1bdSLuis de Bethencourt if (!mixed && total_free_meta - thresh < block_rsv->size) 2151ca8a51b3SDavid Sterba buf->f_bavail = 0; 2152ca8a51b3SDavid Sterba 2153ba7b6e62SDavid Sterba buf->f_type = BTRFS_SUPER_MAGIC; 2154ba7b6e62SDavid Sterba buf->f_bsize = dentry->d_sb->s_blocksize; 2155ba7b6e62SDavid Sterba buf->f_namelen = BTRFS_NAME_LEN; 2156ba7b6e62SDavid Sterba 21579d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 21589d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 21599d03632eSDavid Woodhouse on a big-endian or little-endian host */ 21609d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 21619d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 216232d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 21632b0143b5SDavid Howells buf->f_fsid.val[0] ^= BTRFS_I(d_inode(dentry))->root->objectid >> 32; 21642b0143b5SDavid Howells buf->f_fsid.val[1] ^= BTRFS_I(d_inode(dentry))->root->objectid; 216532d48fa1SDavid Woodhouse 21668fd17795SChris Mason return 0; 21678fd17795SChris Mason } 2168b5133862SChris Mason 2169aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb) 2170aea52e19SAl Viro { 2171815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2172aea52e19SAl Viro kill_anon_super(sb); 2173aea52e19SAl Viro free_fs_info(fs_info); 2174aea52e19SAl Viro } 2175aea52e19SAl Viro 21762e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 21772e635a27SChris Mason .owner = THIS_MODULE, 21782e635a27SChris Mason .name = "btrfs", 2179061dbc6bSAl Viro .mount = btrfs_mount, 2180aea52e19SAl Viro .kill_sb = btrfs_kill_super, 2181f667aef6SQu Wenruo .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA, 21822e635a27SChris Mason }; 21837f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs"); 2184a9218f6bSChris Mason 2185d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file) 2186d8620958STom Van Braeckel { 2187d8620958STom Van Braeckel /* 2188d8620958STom Van Braeckel * The control file's private_data is used to hold the 2189d8620958STom Van Braeckel * transaction when it is started and is used to keep 2190d8620958STom Van Braeckel * track of whether a transaction is already in progress. 2191d8620958STom Van Braeckel */ 2192d8620958STom Van Braeckel file->private_data = NULL; 2193d8620958STom Van Braeckel return 0; 2194d8620958STom Van Braeckel } 2195d8620958STom Van Braeckel 2196d352ac68SChris Mason /* 2197d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 2198d352ac68SChris Mason */ 21998a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 22008a4b83ccSChris Mason unsigned long arg) 22018a4b83ccSChris Mason { 22028a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 22038a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 2204c071fcfdSChris Mason int ret = -ENOTTY; 22058a4b83ccSChris Mason 2206e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2207e441d54dSChris Mason return -EPERM; 2208e441d54dSChris Mason 2209dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 2210dae7b665SLi Zefan if (IS_ERR(vol)) 2211dae7b665SLi Zefan return PTR_ERR(vol); 2212c071fcfdSChris Mason 22138a4b83ccSChris Mason switch (cmd) { 22148a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 221597288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 22168a4b83ccSChris Mason &btrfs_fs_type, &fs_devices); 22178a4b83ccSChris Mason break; 221802db0844SJosef Bacik case BTRFS_IOC_DEVICES_READY: 221902db0844SJosef Bacik ret = btrfs_scan_one_device(vol->name, FMODE_READ, 222002db0844SJosef Bacik &btrfs_fs_type, &fs_devices); 222102db0844SJosef Bacik if (ret) 222202db0844SJosef Bacik break; 222302db0844SJosef Bacik ret = !(fs_devices->num_devices == fs_devices->total_devices); 222402db0844SJosef Bacik break; 2225c5868f83SDavid Sterba case BTRFS_IOC_GET_SUPPORTED_FEATURES: 2226d5131b65SDavid Sterba ret = btrfs_ioctl_get_supported_features((void __user*)arg); 2227c5868f83SDavid Sterba break; 22288a4b83ccSChris Mason } 2229dae7b665SLi Zefan 22308a4b83ccSChris Mason kfree(vol); 2231f819d837SLinda Knippers return ret; 22328a4b83ccSChris Mason } 22338a4b83ccSChris Mason 22340176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 2235ed0dab6bSYan { 2236354aa0fbSMiao Xie struct btrfs_trans_handle *trans; 22370b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(sb); 22380b246afaSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 2239354aa0fbSMiao Xie 22400b246afaSJeff Mahoney fs_info->fs_frozen = 1; 22419e7cc91aSWang Xiaoguang /* 22429e7cc91aSWang Xiaoguang * We don't need a barrier here, we'll wait for any transaction that 22439e7cc91aSWang Xiaoguang * could be in progress on other threads (and do delayed iputs that 22449e7cc91aSWang Xiaoguang * we want to avoid on a frozen filesystem), or do the commit 22459e7cc91aSWang Xiaoguang * ourselves. 22469e7cc91aSWang Xiaoguang */ 2247d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 2248354aa0fbSMiao Xie if (IS_ERR(trans)) { 2249354aa0fbSMiao Xie /* no transaction, don't bother */ 2250354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 22510176260fSLinus Torvalds return 0; 2252354aa0fbSMiao Xie return PTR_ERR(trans); 2253354aa0fbSMiao Xie } 2254354aa0fbSMiao Xie return btrfs_commit_transaction(trans, root); 2255ed0dab6bSYan } 2256ed0dab6bSYan 22579e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb) 22589e7cc91aSWang Xiaoguang { 22590b246afaSJeff Mahoney btrfs_sb(sb)->fs_frozen = 0; 22609e7cc91aSWang Xiaoguang return 0; 22619e7cc91aSWang Xiaoguang } 22629e7cc91aSWang Xiaoguang 22639c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 22649c5085c1SJosef Bacik { 22659c5085c1SJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 22669c5085c1SJosef Bacik struct btrfs_fs_devices *cur_devices; 22679c5085c1SJosef Bacik struct btrfs_device *dev, *first_dev = NULL; 22689c5085c1SJosef Bacik struct list_head *head; 22699c5085c1SJosef Bacik struct rcu_string *name; 22709c5085c1SJosef Bacik 22719c5085c1SJosef Bacik mutex_lock(&fs_info->fs_devices->device_list_mutex); 22729c5085c1SJosef Bacik cur_devices = fs_info->fs_devices; 22739c5085c1SJosef Bacik while (cur_devices) { 22749c5085c1SJosef Bacik head = &cur_devices->devices; 22759c5085c1SJosef Bacik list_for_each_entry(dev, head, dev_list) { 2276aa9ddcd4SJosef Bacik if (dev->missing) 2277aa9ddcd4SJosef Bacik continue; 22780aeb8a6eSAnand Jain if (!dev->name) 22790aeb8a6eSAnand Jain continue; 22809c5085c1SJosef Bacik if (!first_dev || dev->devid < first_dev->devid) 22819c5085c1SJosef Bacik first_dev = dev; 22829c5085c1SJosef Bacik } 22839c5085c1SJosef Bacik cur_devices = cur_devices->seed; 22849c5085c1SJosef Bacik } 22859c5085c1SJosef Bacik 22869c5085c1SJosef Bacik if (first_dev) { 22879c5085c1SJosef Bacik rcu_read_lock(); 22889c5085c1SJosef Bacik name = rcu_dereference(first_dev->name); 22899c5085c1SJosef Bacik seq_escape(m, name->str, " \t\n\\"); 22909c5085c1SJosef Bacik rcu_read_unlock(); 22919c5085c1SJosef Bacik } else { 22929c5085c1SJosef Bacik WARN_ON(1); 22939c5085c1SJosef Bacik } 22949c5085c1SJosef Bacik mutex_unlock(&fs_info->fs_devices->device_list_mutex); 22959c5085c1SJosef Bacik return 0; 22969c5085c1SJosef Bacik } 22979c5085c1SJosef Bacik 2298b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 229976dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 2300bd555975SAl Viro .evict_inode = btrfs_evict_inode, 2301e20d96d6SChris Mason .put_super = btrfs_put_super, 2302d5719762SChris Mason .sync_fs = btrfs_sync_fs, 2303a9572a15SEric Paris .show_options = btrfs_show_options, 23049c5085c1SJosef Bacik .show_devname = btrfs_show_devname, 23054730a4bcSChris Mason .write_inode = btrfs_write_inode, 23062c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 23072c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 23088fd17795SChris Mason .statfs = btrfs_statfs, 2309c146afadSYan Zheng .remount_fs = btrfs_remount, 23100176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 23119e7cc91aSWang Xiaoguang .unfreeze_fs = btrfs_unfreeze, 2312e20d96d6SChris Mason }; 2313a9218f6bSChris Mason 2314a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 2315d8620958STom Van Braeckel .open = btrfs_control_open, 2316a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 2317a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 2318a9218f6bSChris Mason .owner = THIS_MODULE, 23196038f373SArnd Bergmann .llseek = noop_llseek, 2320a9218f6bSChris Mason }; 2321a9218f6bSChris Mason 2322a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 2323578454ffSKay Sievers .minor = BTRFS_MINOR, 2324a9218f6bSChris Mason .name = "btrfs-control", 2325a9218f6bSChris Mason .fops = &btrfs_ctl_fops 2326a9218f6bSChris Mason }; 2327a9218f6bSChris Mason 2328578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 2329578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 2330578454ffSKay Sievers 2331a9218f6bSChris Mason static int btrfs_interface_init(void) 2332a9218f6bSChris Mason { 2333a9218f6bSChris Mason return misc_register(&btrfs_misc); 2334a9218f6bSChris Mason } 2335a9218f6bSChris Mason 2336b2950863SChristoph Hellwig static void btrfs_interface_exit(void) 2337a9218f6bSChris Mason { 2338f368ed60SGreg Kroah-Hartman misc_deregister(&btrfs_misc); 2339a9218f6bSChris Mason } 2340a9218f6bSChris Mason 23418ae1af3cSAnand Jain static void btrfs_print_mod_info(void) 234285965600SDavid Sterba { 234362e85577SJeff Mahoney pr_info("Btrfs loaded, crc32c=%s" 234485965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 234585965600SDavid Sterba ", debug=on" 234685965600SDavid Sterba #endif 234779556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT 234879556c3dSStefan Behrens ", assert=on" 234979556c3dSStefan Behrens #endif 235085965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 235185965600SDavid Sterba ", integrity-checker=on" 235285965600SDavid Sterba #endif 23535f9e1059SJeff Mahoney "\n", 23545f9e1059SJeff Mahoney btrfs_crc32c_impl()); 235585965600SDavid Sterba } 235685965600SDavid Sterba 23572e635a27SChris Mason static int __init init_btrfs_fs(void) 23582e635a27SChris Mason { 23592c90e5d6SChris Mason int err; 236058176a96SJosef Bacik 236114a958e6SFilipe David Borba Manana err = btrfs_hash_init(); 236214a958e6SFilipe David Borba Manana if (err) 236314a958e6SFilipe David Borba Manana return err; 236414a958e6SFilipe David Borba Manana 236563541927SFilipe David Borba Manana btrfs_props_init(); 236663541927SFilipe David Borba Manana 236758176a96SJosef Bacik err = btrfs_init_sysfs(); 236858176a96SJosef Bacik if (err) 236914a958e6SFilipe David Borba Manana goto free_hash; 237058176a96SJosef Bacik 2371143bede5SJeff Mahoney btrfs_init_compress(); 2372d1310b2eSChris Mason 2373261507a0SLi Zefan err = btrfs_init_cachep(); 2374261507a0SLi Zefan if (err) 2375261507a0SLi Zefan goto free_compress; 2376261507a0SLi Zefan 2377d1310b2eSChris Mason err = extent_io_init(); 23782f4cbe64SWyatt Banks if (err) 23792f4cbe64SWyatt Banks goto free_cachep; 23802f4cbe64SWyatt Banks 2381d1310b2eSChris Mason err = extent_map_init(); 2382d1310b2eSChris Mason if (err) 2383d1310b2eSChris Mason goto free_extent_io; 2384d1310b2eSChris Mason 23856352b91dSMiao Xie err = ordered_data_init(); 23862f4cbe64SWyatt Banks if (err) 23872f4cbe64SWyatt Banks goto free_extent_map; 2388c8b97818SChris Mason 23896352b91dSMiao Xie err = btrfs_delayed_inode_init(); 23906352b91dSMiao Xie if (err) 23916352b91dSMiao Xie goto free_ordered_data; 23926352b91dSMiao Xie 23939247f317SMiao Xie err = btrfs_auto_defrag_init(); 239416cdcec7SMiao Xie if (err) 239516cdcec7SMiao Xie goto free_delayed_inode; 239616cdcec7SMiao Xie 239778a6184aSMiao Xie err = btrfs_delayed_ref_init(); 23989247f317SMiao Xie if (err) 23999247f317SMiao Xie goto free_auto_defrag; 24009247f317SMiao Xie 2401b9e9a6cbSWang Shilong err = btrfs_prelim_ref_init(); 2402b9e9a6cbSWang Shilong if (err) 2403af13b492SDavid Sterba goto free_delayed_ref; 2404b9e9a6cbSWang Shilong 240597eb6b69SDavid Sterba err = btrfs_end_io_wq_init(); 240678a6184aSMiao Xie if (err) 2407af13b492SDavid Sterba goto free_prelim_ref; 240878a6184aSMiao Xie 240997eb6b69SDavid Sterba err = btrfs_interface_init(); 241097eb6b69SDavid Sterba if (err) 241197eb6b69SDavid Sterba goto free_end_io_wq; 241297eb6b69SDavid Sterba 2413e565d4b9SJan Schmidt btrfs_init_lockdep(); 2414e565d4b9SJan Schmidt 24158ae1af3cSAnand Jain btrfs_print_mod_info(); 2416dc11dd5dSJosef Bacik 2417dc11dd5dSJosef Bacik err = btrfs_run_sanity_tests(); 2418dc11dd5dSJosef Bacik if (err) 2419dc11dd5dSJosef Bacik goto unregister_ioctl; 2420dc11dd5dSJosef Bacik 2421dc11dd5dSJosef Bacik err = register_filesystem(&btrfs_fs_type); 2422dc11dd5dSJosef Bacik if (err) 2423dc11dd5dSJosef Bacik goto unregister_ioctl; 242474255aa0SJosef Bacik 24252f4cbe64SWyatt Banks return 0; 24262f4cbe64SWyatt Banks 2427a9218f6bSChris Mason unregister_ioctl: 2428a9218f6bSChris Mason btrfs_interface_exit(); 242997eb6b69SDavid Sterba free_end_io_wq: 243097eb6b69SDavid Sterba btrfs_end_io_wq_exit(); 2431b9e9a6cbSWang Shilong free_prelim_ref: 2432b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 243378a6184aSMiao Xie free_delayed_ref: 243478a6184aSMiao Xie btrfs_delayed_ref_exit(); 24359247f317SMiao Xie free_auto_defrag: 24369247f317SMiao Xie btrfs_auto_defrag_exit(); 243716cdcec7SMiao Xie free_delayed_inode: 243816cdcec7SMiao Xie btrfs_delayed_inode_exit(); 24396352b91dSMiao Xie free_ordered_data: 24406352b91dSMiao Xie ordered_data_exit(); 24412f4cbe64SWyatt Banks free_extent_map: 24422f4cbe64SWyatt Banks extent_map_exit(); 2443d1310b2eSChris Mason free_extent_io: 2444d1310b2eSChris Mason extent_io_exit(); 24452f4cbe64SWyatt Banks free_cachep: 24462f4cbe64SWyatt Banks btrfs_destroy_cachep(); 2447261507a0SLi Zefan free_compress: 2448261507a0SLi Zefan btrfs_exit_compress(); 24492f4cbe64SWyatt Banks btrfs_exit_sysfs(); 245014a958e6SFilipe David Borba Manana free_hash: 245114a958e6SFilipe David Borba Manana btrfs_hash_exit(); 24522c90e5d6SChris Mason return err; 24532e635a27SChris Mason } 24542e635a27SChris Mason 24552e635a27SChris Mason static void __exit exit_btrfs_fs(void) 24562e635a27SChris Mason { 245739279cc3SChris Mason btrfs_destroy_cachep(); 245878a6184aSMiao Xie btrfs_delayed_ref_exit(); 24599247f317SMiao Xie btrfs_auto_defrag_exit(); 246016cdcec7SMiao Xie btrfs_delayed_inode_exit(); 2461b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 24626352b91dSMiao Xie ordered_data_exit(); 2463a52d9a80SChris Mason extent_map_exit(); 2464d1310b2eSChris Mason extent_io_exit(); 2465a9218f6bSChris Mason btrfs_interface_exit(); 24665ed5f588SJosef Bacik btrfs_end_io_wq_exit(); 24672e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 246858176a96SJosef Bacik btrfs_exit_sysfs(); 24698a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 2470261507a0SLi Zefan btrfs_exit_compress(); 247114a958e6SFilipe David Borba Manana btrfs_hash_exit(); 24722e635a27SChris Mason } 24732e635a27SChris Mason 247460efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs); 24752e635a27SChris Mason module_exit(exit_btrfs_fs) 24762e635a27SChris Mason 24772e635a27SChris Mason MODULE_LICENSE("GPL"); 2478