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" 6400142756SJeff Mahoney #include "backref.h" 651abe9b8aSliubo #define CREATE_TRACE_POINTS 661abe9b8aSliubo #include <trace/events/btrfs.h> 671abe9b8aSliubo 68b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops; 69830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type; 70e20d96d6SChris Mason 710723a047SHarald Hoyer static int btrfs_remount(struct super_block *sb, int *flags, char *data); 720723a047SHarald Hoyer 73e33e17eeSJeff Mahoney const char *btrfs_decode_error(int errno) 74acce952bSliubo { 7508748810SDavid Sterba char *errstr = "unknown"; 76acce952bSliubo 77acce952bSliubo switch (errno) { 78acce952bSliubo case -EIO: 79acce952bSliubo errstr = "IO failure"; 80acce952bSliubo break; 81acce952bSliubo case -ENOMEM: 82acce952bSliubo errstr = "Out of memory"; 83acce952bSliubo break; 84acce952bSliubo case -EROFS: 85acce952bSliubo errstr = "Readonly filesystem"; 86acce952bSliubo break; 878c342930SJeff Mahoney case -EEXIST: 888c342930SJeff Mahoney errstr = "Object already exists"; 898c342930SJeff Mahoney break; 9094ef7280SDavid Sterba case -ENOSPC: 9194ef7280SDavid Sterba errstr = "No space left"; 9294ef7280SDavid Sterba break; 9394ef7280SDavid Sterba case -ENOENT: 9494ef7280SDavid Sterba errstr = "No such entry"; 9594ef7280SDavid Sterba break; 96acce952bSliubo } 97acce952bSliubo 98acce952bSliubo return errstr; 99acce952bSliubo } 100acce952bSliubo 101acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */ 102acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info) 103acce952bSliubo { 104acce952bSliubo struct super_block *sb = fs_info->sb; 105acce952bSliubo 106bc98a42cSDavid Howells if (sb_rdonly(sb)) 107acce952bSliubo return; 108acce952bSliubo 10987533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 110acce952bSliubo sb->s_flags |= MS_RDONLY; 111c2cf52ebSSimon Kirby btrfs_info(fs_info, "forced readonly"); 1121acd6831SStefan Behrens /* 1131acd6831SStefan Behrens * Note that a running device replace operation is not 1141acd6831SStefan Behrens * canceled here although there is no way to update 1151acd6831SStefan Behrens * the progress. It would add the risk of a deadlock, 11601327610SNicholas D Steeves * therefore the canceling is omitted. The only penalty 1171acd6831SStefan Behrens * is that some I/O remains active until the procedure 1181acd6831SStefan Behrens * completes. The next time when the filesystem is 1191acd6831SStefan Behrens * mounted writeable again, the device replace 1201acd6831SStefan Behrens * operation continues. 1211acd6831SStefan Behrens */ 122acce952bSliubo } 123acce952bSliubo } 124acce952bSliubo 125acce952bSliubo /* 12634d97007SAnand Jain * __btrfs_handle_fs_error decodes expected errors from the caller and 127acce952bSliubo * invokes the approciate error response. 128acce952bSliubo */ 129c0d19e2bSDavid Sterba __cold 13034d97007SAnand Jain void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function, 1314da35113SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 132acce952bSliubo { 133acce952bSliubo struct super_block *sb = fs_info->sb; 13457d816a1SAnand Jain #ifdef CONFIG_PRINTK 135acce952bSliubo const char *errstr; 13657d816a1SAnand Jain #endif 137acce952bSliubo 138acce952bSliubo /* 139acce952bSliubo * Special case: if the error is EROFS, and we're already 140acce952bSliubo * under MS_RDONLY, then it is safe here. 141acce952bSliubo */ 142bc98a42cSDavid Howells if (errno == -EROFS && sb_rdonly(sb)) 143acce952bSliubo return; 144acce952bSliubo 14557d816a1SAnand Jain #ifdef CONFIG_PRINTK 14608748810SDavid Sterba errstr = btrfs_decode_error(errno); 1474da35113SJeff Mahoney if (fmt) { 14837252a66SEric Sandeen struct va_format vaf; 14937252a66SEric Sandeen va_list args; 15037252a66SEric Sandeen 15137252a66SEric Sandeen va_start(args, fmt); 15237252a66SEric Sandeen vaf.fmt = fmt; 15337252a66SEric Sandeen vaf.va = &args; 1544da35113SJeff Mahoney 15562e85577SJeff Mahoney pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n", 15608748810SDavid Sterba sb->s_id, function, line, errno, errstr, &vaf); 15737252a66SEric Sandeen va_end(args); 1584da35113SJeff Mahoney } else { 15962e85577SJeff Mahoney pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s\n", 16008748810SDavid Sterba sb->s_id, function, line, errno, errstr); 1614da35113SJeff Mahoney } 16257d816a1SAnand Jain #endif 163acce952bSliubo 1640713d90cSAnand Jain /* 1650713d90cSAnand Jain * Today we only save the error info to memory. Long term we'll 1660713d90cSAnand Jain * also send it down to the disk 1670713d90cSAnand Jain */ 1680713d90cSAnand Jain set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 1690713d90cSAnand Jain 1704da35113SJeff Mahoney /* Don't go through full error handling during mount */ 171cf79ffb5SJosef Bacik if (sb->s_flags & MS_BORN) 172acce952bSliubo btrfs_handle_error(fs_info); 173acce952bSliubo } 1744da35113SJeff Mahoney 17557d816a1SAnand Jain #ifdef CONFIG_PRINTK 176533574c6SJoe Perches static const char * const logtypes[] = { 1774da35113SJeff Mahoney "emergency", 1784da35113SJeff Mahoney "alert", 1794da35113SJeff Mahoney "critical", 1804da35113SJeff Mahoney "error", 1814da35113SJeff Mahoney "warning", 1824da35113SJeff Mahoney "notice", 1834da35113SJeff Mahoney "info", 1844da35113SJeff Mahoney "debug", 1854da35113SJeff Mahoney }; 1864da35113SJeff Mahoney 18735f4e5e6SNikolay Borisov 18835f4e5e6SNikolay Borisov /* 18935f4e5e6SNikolay Borisov * Use one ratelimit state per log level so that a flood of less important 19035f4e5e6SNikolay Borisov * messages doesn't cause more important ones to be dropped. 19135f4e5e6SNikolay Borisov */ 19235f4e5e6SNikolay Borisov static struct ratelimit_state printk_limits[] = { 19335f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100), 19435f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100), 19535f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100), 19635f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100), 19735f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100), 19835f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100), 19935f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100), 20035f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100), 20135f4e5e6SNikolay Borisov }; 20235f4e5e6SNikolay Borisov 203c2cf52ebSSimon Kirby void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...) 2044da35113SJeff Mahoney { 20540f7828bSPetr Mladek char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0"; 2064da35113SJeff Mahoney struct va_format vaf; 2074da35113SJeff Mahoney va_list args; 208533574c6SJoe Perches int kern_level; 20940f7828bSPetr Mladek const char *type = logtypes[4]; 21040f7828bSPetr Mladek struct ratelimit_state *ratelimit = &printk_limits[4]; 2114da35113SJeff Mahoney 2124da35113SJeff Mahoney va_start(args, fmt); 2134da35113SJeff Mahoney 214262c5e86SPetr Mladek while ((kern_level = printk_get_level(fmt)) != 0) { 215533574c6SJoe Perches size_t size = printk_skip_level(fmt) - fmt; 216262c5e86SPetr Mladek 217262c5e86SPetr Mladek if (kern_level >= '0' && kern_level <= '7') { 218533574c6SJoe Perches memcpy(lvl, fmt, size); 219533574c6SJoe Perches lvl[size] = '\0'; 220533574c6SJoe Perches type = logtypes[kern_level - '0']; 22135f4e5e6SNikolay Borisov ratelimit = &printk_limits[kern_level - '0']; 222262c5e86SPetr Mladek } 223262c5e86SPetr Mladek fmt += size; 224262c5e86SPetr Mladek } 225262c5e86SPetr Mladek 2264da35113SJeff Mahoney vaf.fmt = fmt; 2274da35113SJeff Mahoney vaf.va = &args; 228533574c6SJoe Perches 22935f4e5e6SNikolay Borisov if (__ratelimit(ratelimit)) 2303993b112SColin Ian King printk("%sBTRFS %s (device %s): %pV\n", lvl, type, 2313993b112SColin Ian King fs_info ? fs_info->sb->s_id : "<unknown>", &vaf); 232533574c6SJoe Perches 233533574c6SJoe Perches va_end(args); 2344da35113SJeff Mahoney } 235533574c6SJoe Perches #endif 236533574c6SJoe Perches 2378c342930SJeff Mahoney /* 23849b25e05SJeff Mahoney * We only mark the transaction aborted and then set the file system read-only. 23949b25e05SJeff Mahoney * This will prevent new transactions from starting or trying to join this 24049b25e05SJeff Mahoney * one. 24149b25e05SJeff Mahoney * 24249b25e05SJeff Mahoney * This means that error recovery at the call site is limited to freeing 24349b25e05SJeff Mahoney * any local memory allocations and passing the error code up without 24449b25e05SJeff Mahoney * further cleanup. The transaction should complete as it normally would 24549b25e05SJeff Mahoney * in the call path but will return -EIO. 24649b25e05SJeff Mahoney * 24749b25e05SJeff Mahoney * We'll complete the cleanup in btrfs_end_transaction and 24849b25e05SJeff Mahoney * btrfs_commit_transaction. 24949b25e05SJeff Mahoney */ 250c0d19e2bSDavid Sterba __cold 25149b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans, 25266642832SJeff Mahoney const char *function, 25349b25e05SJeff Mahoney unsigned int line, int errno) 25449b25e05SJeff Mahoney { 25566642832SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 25666642832SJeff Mahoney 25749b25e05SJeff Mahoney trans->aborted = errno; 25849b25e05SJeff Mahoney /* Nothing used. The other threads that have joined this 25949b25e05SJeff Mahoney * transaction may be able to continue. */ 26064c12921SJeff Mahoney if (!trans->dirty && list_empty(&trans->new_bgs)) { 26169ce977aSMiao Xie const char *errstr; 26269ce977aSMiao Xie 26308748810SDavid Sterba errstr = btrfs_decode_error(errno); 26466642832SJeff Mahoney btrfs_warn(fs_info, 265c2cf52ebSSimon Kirby "%s:%d: Aborting unused transaction(%s).", 26669ce977aSMiao Xie function, line, errstr); 26749b25e05SJeff Mahoney return; 26849b25e05SJeff Mahoney } 26920c7bcecSSeraphime Kirkovski WRITE_ONCE(trans->transaction->aborted, errno); 270501407aaSJosef Bacik /* Wake up anybody who may be waiting on this transaction */ 27166642832SJeff Mahoney wake_up(&fs_info->transaction_wait); 27266642832SJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 27366642832SJeff Mahoney __btrfs_handle_fs_error(fs_info, function, line, errno, NULL); 27449b25e05SJeff Mahoney } 27549b25e05SJeff Mahoney /* 2768c342930SJeff Mahoney * __btrfs_panic decodes unexpected, fatal errors from the caller, 2778c342930SJeff Mahoney * issues an alert, and either panics or BUGs, depending on mount options. 2788c342930SJeff Mahoney */ 279c0d19e2bSDavid Sterba __cold 2808c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, 2818c342930SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 2828c342930SJeff Mahoney { 2838c342930SJeff Mahoney char *s_id = "<unknown>"; 2848c342930SJeff Mahoney const char *errstr; 2858c342930SJeff Mahoney struct va_format vaf = { .fmt = fmt }; 2868c342930SJeff Mahoney va_list args; 2878c342930SJeff Mahoney 2888c342930SJeff Mahoney if (fs_info) 2898c342930SJeff Mahoney s_id = fs_info->sb->s_id; 2908c342930SJeff Mahoney 2918c342930SJeff Mahoney va_start(args, fmt); 2928c342930SJeff Mahoney vaf.va = &args; 2938c342930SJeff Mahoney 29408748810SDavid Sterba errstr = btrfs_decode_error(errno); 295d8953d69SSatoru Takeuchi if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR))) 29608748810SDavid Sterba panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n", 29708748810SDavid Sterba s_id, function, line, &vaf, errno, errstr); 2988c342930SJeff Mahoney 299efe120a0SFrank Holton btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)", 300efe120a0SFrank Holton function, line, &vaf, errno, errstr); 3018c342930SJeff Mahoney va_end(args); 3028c342930SJeff Mahoney /* Caller calls BUG() */ 3038c342930SJeff Mahoney } 304acce952bSliubo 305e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 306e20d96d6SChris Mason { 3076bccf3abSJeff Mahoney close_ctree(btrfs_sb(sb)); 308e20d96d6SChris Mason } 3092e635a27SChris Mason 31095e05289SChris Mason enum { 31173f73415SJosef Bacik Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum, 312287a0ab9SJosef Bacik Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, 313287a0ab9SJosef Bacik Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress, 314261507a0SLi Zefan Opt_compress_type, Opt_compress_force, Opt_compress_force_type, 315261507a0SLi Zefan Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard, 31670f6d82eSOmar Sandoval Opt_space_cache, Opt_space_cache_version, Opt_clear_cache, 31770f6d82eSOmar Sandoval Opt_user_subvol_rm_allowed, Opt_enospc_debug, Opt_subvolrootid, 31870f6d82eSOmar Sandoval Opt_defrag, Opt_inode_cache, Opt_no_space_cache, Opt_recovery, 31970f6d82eSOmar Sandoval Opt_skip_balance, Opt_check_integrity, 32070f6d82eSOmar Sandoval Opt_check_integrity_including_extent_data, 321f420ee1eSStefan Behrens Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree, 322e07a2adeSQu Wenruo Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard, 323a258af7aSQu Wenruo Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow, 3248dcddfa0SQu Wenruo Opt_datasum, Opt_treelog, Opt_noinode_cache, Opt_usebackuproot, 325fed8f166SQu Wenruo Opt_nologreplay, Opt_norecovery, 326d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 327d0bd4560SJosef Bacik Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all, 328d0bd4560SJosef Bacik #endif 3299555c6c1SIlya Dryomov Opt_err, 33095e05289SChris Mason }; 33195e05289SChris Mason 3324d4ab6d6SDavid Sterba static const match_table_t tokens = { 333dfe25020SChris Mason {Opt_degraded, "degraded"}, 33495e05289SChris Mason {Opt_subvol, "subvol=%s"}, 3351493381fSWang Shilong {Opt_subvolid, "subvolid=%s"}, 33643e570b0SChristoph Hellwig {Opt_device, "device=%s"}, 337b6cda9bcSChris Mason {Opt_nodatasum, "nodatasum"}, 338d399167dSQu Wenruo {Opt_datasum, "datasum"}, 339be20aa9dSChris Mason {Opt_nodatacow, "nodatacow"}, 340a258af7aSQu Wenruo {Opt_datacow, "datacow"}, 34121ad10cfSChris Mason {Opt_nobarrier, "nobarrier"}, 342842bef58SQu Wenruo {Opt_barrier, "barrier"}, 3436f568d35SChris Mason {Opt_max_inline, "max_inline=%s"}, 3448f662a76SChris Mason {Opt_alloc_start, "alloc_start=%s"}, 3454543df7eSChris Mason {Opt_thread_pool, "thread_pool=%d"}, 346c8b97818SChris Mason {Opt_compress, "compress"}, 347261507a0SLi Zefan {Opt_compress_type, "compress=%s"}, 348a555f810SChris Mason {Opt_compress_force, "compress-force"}, 349261507a0SLi Zefan {Opt_compress_force_type, "compress-force=%s"}, 350e18e4809SChris Mason {Opt_ssd, "ssd"}, 351451d7585SChris Mason {Opt_ssd_spread, "ssd_spread"}, 3523b30c22fSChris Mason {Opt_nossd, "nossd"}, 353bd0330adSQu Wenruo {Opt_acl, "acl"}, 35433268eafSJosef Bacik {Opt_noacl, "noacl"}, 3553a5e1404SSage Weil {Opt_notreelog, "notreelog"}, 356a88998f2SQu Wenruo {Opt_treelog, "treelog"}, 35796da0919SQu Wenruo {Opt_nologreplay, "nologreplay"}, 358fed8f166SQu Wenruo {Opt_norecovery, "norecovery"}, 359dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 3602c9ee856SQu Wenruo {Opt_noflushoncommit, "noflushoncommit"}, 36197e728d4SJosef Bacik {Opt_ratio, "metadata_ratio=%d"}, 362e244a0aeSChristoph Hellwig {Opt_discard, "discard"}, 363e07a2adeSQu Wenruo {Opt_nodiscard, "nodiscard"}, 3640af3d00bSJosef Bacik {Opt_space_cache, "space_cache"}, 36570f6d82eSOmar Sandoval {Opt_space_cache_version, "space_cache=%s"}, 36688c2ba3bSJosef Bacik {Opt_clear_cache, "clear_cache"}, 3674260f7c7SSage Weil {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, 36891435650SChris Mason {Opt_enospc_debug, "enospc_debug"}, 36953036293SQu Wenruo {Opt_noenospc_debug, "noenospc_debug"}, 370e15d0542SXin Zhong {Opt_subvolrootid, "subvolrootid=%d"}, 3714cb5300bSChris Mason {Opt_defrag, "autodefrag"}, 372fc0ca9afSQu Wenruo {Opt_nodefrag, "noautodefrag"}, 3734b9465cbSChris Mason {Opt_inode_cache, "inode_cache"}, 3743818aea2SQu Wenruo {Opt_noinode_cache, "noinode_cache"}, 3758965593eSDavid Sterba {Opt_no_space_cache, "nospace_cache"}, 3768dcddfa0SQu Wenruo {Opt_recovery, "recovery"}, /* deprecated */ 3778dcddfa0SQu Wenruo {Opt_usebackuproot, "usebackuproot"}, 3789555c6c1SIlya Dryomov {Opt_skip_balance, "skip_balance"}, 37921adbd5cSStefan Behrens {Opt_check_integrity, "check_int"}, 38021adbd5cSStefan Behrens {Opt_check_integrity_including_extent_data, "check_int_data"}, 38121adbd5cSStefan Behrens {Opt_check_integrity_print_mask, "check_int_print_mask=%d"}, 382f420ee1eSStefan Behrens {Opt_rescan_uuid_tree, "rescan_uuid_tree"}, 3838c342930SJeff Mahoney {Opt_fatal_errors, "fatal_errors=%s"}, 3848b87dc17SDavid Sterba {Opt_commit_interval, "commit=%d"}, 385d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 386d0bd4560SJosef Bacik {Opt_fragment_data, "fragment=data"}, 387d0bd4560SJosef Bacik {Opt_fragment_metadata, "fragment=metadata"}, 388d0bd4560SJosef Bacik {Opt_fragment_all, "fragment=all"}, 389d0bd4560SJosef Bacik #endif 39033268eafSJosef Bacik {Opt_err, NULL}, 39195e05289SChris Mason }; 39295e05289SChris Mason 393edf24abeSChristoph Hellwig /* 394edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 395edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 39649b25e05SJeff Mahoney * XXX JDM: This needs to be cleaned up for remount. 397edf24abeSChristoph Hellwig */ 3982ff7e61eSJeff Mahoney int btrfs_parse_options(struct btrfs_fs_info *info, char *options, 39996da0919SQu Wenruo unsigned long new_flags) 40095e05289SChris Mason { 40195e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 40273bc1876SJosef Bacik char *p, *num, *orig = NULL; 40373bc1876SJosef Bacik u64 cache_gen; 4044543df7eSChris Mason int intarg; 405a7a3f7caSSage Weil int ret = 0; 406261507a0SLi Zefan char *compress_type; 407261507a0SLi Zefan bool compress_force = false; 408b7c47bbbSTsutomu Itoh enum btrfs_compression_type saved_compress_type; 409b7c47bbbSTsutomu Itoh bool saved_compress_force; 410b7c47bbbSTsutomu Itoh int no_compress = 0; 411b6cda9bcSChris Mason 4120b246afaSJeff Mahoney cache_gen = btrfs_super_cache_generation(info->super_copy); 4130b246afaSJeff Mahoney if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) 41470f6d82eSOmar Sandoval btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE); 41570f6d82eSOmar Sandoval else if (cache_gen) 41673bc1876SJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 41773bc1876SJosef Bacik 41896da0919SQu Wenruo /* 41996da0919SQu Wenruo * Even the options are empty, we still need to do extra check 42096da0919SQu Wenruo * against new flags 42196da0919SQu Wenruo */ 42295e05289SChris Mason if (!options) 42396da0919SQu Wenruo goto check; 42495e05289SChris Mason 425be20aa9dSChris Mason /* 426be20aa9dSChris Mason * strsep changes the string, duplicate it because parse_options 427be20aa9dSChris Mason * gets called twice 428be20aa9dSChris Mason */ 4293ec83621SDavid Sterba options = kstrdup(options, GFP_KERNEL); 430be20aa9dSChris Mason if (!options) 431be20aa9dSChris Mason return -ENOMEM; 432be20aa9dSChris Mason 433da495eccSJosef Bacik orig = options; 434be20aa9dSChris Mason 43595e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 43695e05289SChris Mason int token; 43795e05289SChris Mason if (!*p) 43895e05289SChris Mason continue; 43995e05289SChris Mason 44095e05289SChris Mason token = match_token(p, tokens, args); 44195e05289SChris Mason switch (token) { 442dfe25020SChris Mason case Opt_degraded: 4430b246afaSJeff Mahoney btrfs_info(info, "allowing degraded mounts"); 444dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 445dfe25020SChris Mason break; 44695e05289SChris Mason case Opt_subvol: 44773f73415SJosef Bacik case Opt_subvolid: 448e15d0542SXin Zhong case Opt_subvolrootid: 44943e570b0SChristoph Hellwig case Opt_device: 450edf24abeSChristoph Hellwig /* 45143e570b0SChristoph Hellwig * These are parsed by btrfs_parse_early_options 452edf24abeSChristoph Hellwig * and can be happily ignored here. 453edf24abeSChristoph Hellwig */ 45495e05289SChris Mason break; 455b6cda9bcSChris Mason case Opt_nodatasum: 4563cdde224SJeff Mahoney btrfs_set_and_info(info, NODATASUM, 45707802534SQu Wenruo "setting nodatasum"); 458be20aa9dSChris Mason break; 459d399167dSQu Wenruo case Opt_datasum: 4603cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATASUM)) { 4613cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATACOW)) 4620b246afaSJeff Mahoney btrfs_info(info, 4635d163e0eSJeff Mahoney "setting datasum, datacow enabled"); 464d399167dSQu Wenruo else 4650b246afaSJeff Mahoney btrfs_info(info, "setting datasum"); 46607802534SQu Wenruo } 467d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATACOW); 468d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATASUM); 469d399167dSQu Wenruo break; 470be20aa9dSChris Mason case Opt_nodatacow: 4713cdde224SJeff Mahoney if (!btrfs_test_opt(info, NODATACOW)) { 4723cdde224SJeff Mahoney if (!btrfs_test_opt(info, COMPRESS) || 4733cdde224SJeff Mahoney !btrfs_test_opt(info, FORCE_COMPRESS)) { 4740b246afaSJeff Mahoney btrfs_info(info, 475efe120a0SFrank Holton "setting nodatacow, compression disabled"); 476bedb2ccaSAndrei Popa } else { 4770b246afaSJeff Mahoney btrfs_info(info, "setting nodatacow"); 478bedb2ccaSAndrei Popa } 47907802534SQu Wenruo } 480bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, COMPRESS); 481bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 482be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 483be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 484b6cda9bcSChris Mason break; 485a258af7aSQu Wenruo case Opt_datacow: 4863cdde224SJeff Mahoney btrfs_clear_and_info(info, NODATACOW, 48707802534SQu Wenruo "setting datacow"); 488a258af7aSQu Wenruo break; 489a555f810SChris Mason case Opt_compress_force: 490261507a0SLi Zefan case Opt_compress_force_type: 491261507a0SLi Zefan compress_force = true; 4921c697d4aSEric Sandeen /* Fallthrough */ 493261507a0SLi Zefan case Opt_compress: 494261507a0SLi Zefan case Opt_compress_type: 4953cdde224SJeff Mahoney saved_compress_type = btrfs_test_opt(info, 4963cdde224SJeff Mahoney COMPRESS) ? 497b7c47bbbSTsutomu Itoh info->compress_type : BTRFS_COMPRESS_NONE; 498b7c47bbbSTsutomu Itoh saved_compress_force = 4993cdde224SJeff Mahoney btrfs_test_opt(info, FORCE_COMPRESS); 500261507a0SLi Zefan if (token == Opt_compress || 501261507a0SLi Zefan token == Opt_compress_force || 502a7164fa4SDavid Sterba strncmp(args[0].from, "zlib", 4) == 0) { 503261507a0SLi Zefan compress_type = "zlib"; 504261507a0SLi Zefan info->compress_type = BTRFS_COMPRESS_ZLIB; 505063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 506bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 507bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 508b7c47bbbSTsutomu Itoh no_compress = 0; 509a7164fa4SDavid Sterba } else if (strncmp(args[0].from, "lzo", 3) == 0) { 510a6fa6faeSLi Zefan compress_type = "lzo"; 511a6fa6faeSLi Zefan info->compress_type = BTRFS_COMPRESS_LZO; 512063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 513bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 514bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 5152b0ce2c2SMitch Harder btrfs_set_fs_incompat(info, COMPRESS_LZO); 516b7c47bbbSTsutomu Itoh no_compress = 0; 5175c1aab1dSNick Terrell } else if (strcmp(args[0].from, "zstd") == 0) { 5185c1aab1dSNick Terrell compress_type = "zstd"; 5195c1aab1dSNick Terrell info->compress_type = BTRFS_COMPRESS_ZSTD; 5205c1aab1dSNick Terrell btrfs_set_opt(info->mount_opt, COMPRESS); 5215c1aab1dSNick Terrell btrfs_clear_opt(info->mount_opt, NODATACOW); 5225c1aab1dSNick Terrell btrfs_clear_opt(info->mount_opt, NODATASUM); 5235c1aab1dSNick Terrell btrfs_set_fs_incompat(info, COMPRESS_ZSTD); 5245c1aab1dSNick Terrell no_compress = 0; 525063849eaSArnd Hannemann } else if (strncmp(args[0].from, "no", 2) == 0) { 526063849eaSArnd Hannemann compress_type = "no"; 527063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, COMPRESS); 528063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 529063849eaSArnd Hannemann compress_force = false; 530b7c47bbbSTsutomu Itoh no_compress++; 531261507a0SLi Zefan } else { 532261507a0SLi Zefan ret = -EINVAL; 533261507a0SLi Zefan goto out; 534261507a0SLi Zefan } 535261507a0SLi Zefan 536261507a0SLi Zefan if (compress_force) { 537b7c47bbbSTsutomu Itoh btrfs_set_opt(info->mount_opt, FORCE_COMPRESS); 538143f3636SDavid Sterba } else { 5394027e0f4SWang Shilong /* 5404027e0f4SWang Shilong * If we remount from compress-force=xxx to 5414027e0f4SWang Shilong * compress=xxx, we need clear FORCE_COMPRESS 5424027e0f4SWang Shilong * flag, otherwise, there is no way for users 5434027e0f4SWang Shilong * to disable forcible compression separately. 5444027e0f4SWang Shilong */ 5454027e0f4SWang Shilong btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 546a7e252afSMiao Xie } 5473cdde224SJeff Mahoney if ((btrfs_test_opt(info, COMPRESS) && 548b7c47bbbSTsutomu Itoh (info->compress_type != saved_compress_type || 549b7c47bbbSTsutomu Itoh compress_force != saved_compress_force)) || 5503cdde224SJeff Mahoney (!btrfs_test_opt(info, COMPRESS) && 551b7c47bbbSTsutomu Itoh no_compress == 1)) { 5520b246afaSJeff Mahoney btrfs_info(info, "%s %s compression", 553b7c47bbbSTsutomu Itoh (compress_force) ? "force" : "use", 554b7c47bbbSTsutomu Itoh compress_type); 555b7c47bbbSTsutomu Itoh } 556b7c47bbbSTsutomu Itoh compress_force = false; 557a555f810SChris Mason break; 558e18e4809SChris Mason case Opt_ssd: 5593cdde224SJeff Mahoney btrfs_set_and_info(info, SSD, 560583b7231SHans van Kranenburg "enabling ssd optimizations"); 561951e7966SAdam Borowski btrfs_clear_opt(info->mount_opt, NOSSD); 562e18e4809SChris Mason break; 563451d7585SChris Mason case Opt_ssd_spread: 564583b7231SHans van Kranenburg btrfs_set_and_info(info, SSD, 565583b7231SHans van Kranenburg "enabling ssd optimizations"); 5663cdde224SJeff Mahoney btrfs_set_and_info(info, SSD_SPREAD, 567583b7231SHans van Kranenburg "using spread ssd allocation scheme"); 568951e7966SAdam Borowski btrfs_clear_opt(info->mount_opt, NOSSD); 569451d7585SChris Mason break; 5703b30c22fSChris Mason case Opt_nossd: 571583b7231SHans van Kranenburg btrfs_set_opt(info->mount_opt, NOSSD); 572583b7231SHans van Kranenburg btrfs_clear_and_info(info, SSD, 573583b7231SHans van Kranenburg "not using ssd optimizations"); 574583b7231SHans van Kranenburg btrfs_clear_and_info(info, SSD_SPREAD, 575583b7231SHans van Kranenburg "not using spread ssd allocation scheme"); 5763b30c22fSChris Mason break; 577842bef58SQu Wenruo case Opt_barrier: 5783cdde224SJeff Mahoney btrfs_clear_and_info(info, NOBARRIER, 57907802534SQu Wenruo "turning on barriers"); 580842bef58SQu Wenruo break; 58121ad10cfSChris Mason case Opt_nobarrier: 5823cdde224SJeff Mahoney btrfs_set_and_info(info, NOBARRIER, 58307802534SQu Wenruo "turning off barriers"); 58421ad10cfSChris Mason break; 5854543df7eSChris Mason case Opt_thread_pool: 5862c334e87SWang Shilong ret = match_int(&args[0], &intarg); 5872c334e87SWang Shilong if (ret) { 5882c334e87SWang Shilong goto out; 5892c334e87SWang Shilong } else if (intarg > 0) { 5904543df7eSChris Mason info->thread_pool_size = intarg; 5912c334e87SWang Shilong } else { 5922c334e87SWang Shilong ret = -EINVAL; 5932c334e87SWang Shilong goto out; 5942c334e87SWang Shilong } 5954543df7eSChris Mason break; 5966f568d35SChris Mason case Opt_max_inline: 597edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5986f568d35SChris Mason if (num) { 59991748467SAkinobu Mita info->max_inline = memparse(num, NULL); 6006f568d35SChris Mason kfree(num); 6016f568d35SChris Mason 60215ada040SChris Mason if (info->max_inline) { 603feb5f965SMitch Harder info->max_inline = min_t(u64, 60415ada040SChris Mason info->max_inline, 6050b246afaSJeff Mahoney info->sectorsize); 60615ada040SChris Mason } 6070b246afaSJeff Mahoney btrfs_info(info, "max_inline at %llu", 608c1c9ff7cSGeert Uytterhoeven info->max_inline); 6092c334e87SWang Shilong } else { 6102c334e87SWang Shilong ret = -ENOMEM; 6112c334e87SWang Shilong goto out; 6126f568d35SChris Mason } 6136f568d35SChris Mason break; 6148f662a76SChris Mason case Opt_alloc_start: 6150d0c71b3SDavid Sterba btrfs_info(info, 6160d0c71b3SDavid Sterba "option alloc_start is obsolete, ignored"); 6178f662a76SChris Mason break; 618bd0330adSQu Wenruo case Opt_acl: 61945ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL 6200b246afaSJeff Mahoney info->sb->s_flags |= MS_POSIXACL; 621bd0330adSQu Wenruo break; 62245ff35d6SGuangliang Zhao #else 6230b246afaSJeff Mahoney btrfs_err(info, "support for ACL not compiled in!"); 62445ff35d6SGuangliang Zhao ret = -EINVAL; 62545ff35d6SGuangliang Zhao goto out; 62645ff35d6SGuangliang Zhao #endif 62733268eafSJosef Bacik case Opt_noacl: 6280b246afaSJeff Mahoney info->sb->s_flags &= ~MS_POSIXACL; 62933268eafSJosef Bacik break; 6303a5e1404SSage Weil case Opt_notreelog: 6313cdde224SJeff Mahoney btrfs_set_and_info(info, NOTREELOG, 63207802534SQu Wenruo "disabling tree log"); 6333a5e1404SSage Weil break; 634a88998f2SQu Wenruo case Opt_treelog: 6353cdde224SJeff Mahoney btrfs_clear_and_info(info, NOTREELOG, 63607802534SQu Wenruo "enabling tree log"); 637a88998f2SQu Wenruo break; 638fed8f166SQu Wenruo case Opt_norecovery: 63996da0919SQu Wenruo case Opt_nologreplay: 6403cdde224SJeff Mahoney btrfs_set_and_info(info, NOLOGREPLAY, 64196da0919SQu Wenruo "disabling log replay at mount time"); 64296da0919SQu Wenruo break; 643dccae999SSage Weil case Opt_flushoncommit: 6443cdde224SJeff Mahoney btrfs_set_and_info(info, FLUSHONCOMMIT, 64507802534SQu Wenruo "turning on flush-on-commit"); 646dccae999SSage Weil break; 6472c9ee856SQu Wenruo case Opt_noflushoncommit: 6483cdde224SJeff Mahoney btrfs_clear_and_info(info, FLUSHONCOMMIT, 64907802534SQu Wenruo "turning off flush-on-commit"); 6502c9ee856SQu Wenruo break; 65197e728d4SJosef Bacik case Opt_ratio: 6522c334e87SWang Shilong ret = match_int(&args[0], &intarg); 6532c334e87SWang Shilong if (ret) { 6542c334e87SWang Shilong goto out; 6552c334e87SWang Shilong } else if (intarg >= 0) { 65697e728d4SJosef Bacik info->metadata_ratio = intarg; 6570b246afaSJeff Mahoney btrfs_info(info, "metadata ratio %d", 65897e728d4SJosef Bacik info->metadata_ratio); 6592c334e87SWang Shilong } else { 6602c334e87SWang Shilong ret = -EINVAL; 6612c334e87SWang Shilong goto out; 66297e728d4SJosef Bacik } 66397e728d4SJosef Bacik break; 664e244a0aeSChristoph Hellwig case Opt_discard: 6653cdde224SJeff Mahoney btrfs_set_and_info(info, DISCARD, 66607802534SQu Wenruo "turning on discard"); 667e244a0aeSChristoph Hellwig break; 668e07a2adeSQu Wenruo case Opt_nodiscard: 6693cdde224SJeff Mahoney btrfs_clear_and_info(info, DISCARD, 67007802534SQu Wenruo "turning off discard"); 671e07a2adeSQu Wenruo break; 6720af3d00bSJosef Bacik case Opt_space_cache: 67370f6d82eSOmar Sandoval case Opt_space_cache_version: 67470f6d82eSOmar Sandoval if (token == Opt_space_cache || 67570f6d82eSOmar Sandoval strcmp(args[0].from, "v1") == 0) { 6760b246afaSJeff Mahoney btrfs_clear_opt(info->mount_opt, 67770f6d82eSOmar Sandoval FREE_SPACE_TREE); 6783cdde224SJeff Mahoney btrfs_set_and_info(info, SPACE_CACHE, 67907802534SQu Wenruo "enabling disk space caching"); 68070f6d82eSOmar Sandoval } else if (strcmp(args[0].from, "v2") == 0) { 6810b246afaSJeff Mahoney btrfs_clear_opt(info->mount_opt, 68270f6d82eSOmar Sandoval SPACE_CACHE); 6830b246afaSJeff Mahoney btrfs_set_and_info(info, FREE_SPACE_TREE, 68470f6d82eSOmar Sandoval "enabling free space tree"); 68570f6d82eSOmar Sandoval } else { 68670f6d82eSOmar Sandoval ret = -EINVAL; 68770f6d82eSOmar Sandoval goto out; 68870f6d82eSOmar Sandoval } 6890de90876SJosef Bacik break; 690f420ee1eSStefan Behrens case Opt_rescan_uuid_tree: 691f420ee1eSStefan Behrens btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE); 692f420ee1eSStefan Behrens break; 69373bc1876SJosef Bacik case Opt_no_space_cache: 6943cdde224SJeff Mahoney if (btrfs_test_opt(info, SPACE_CACHE)) { 6950b246afaSJeff Mahoney btrfs_clear_and_info(info, SPACE_CACHE, 69607802534SQu Wenruo "disabling disk space caching"); 69770f6d82eSOmar Sandoval } 6983cdde224SJeff Mahoney if (btrfs_test_opt(info, FREE_SPACE_TREE)) { 6990b246afaSJeff Mahoney btrfs_clear_and_info(info, FREE_SPACE_TREE, 70070f6d82eSOmar Sandoval "disabling free space tree"); 70170f6d82eSOmar Sandoval } 70273bc1876SJosef Bacik break; 7034b9465cbSChris Mason case Opt_inode_cache: 7047e1876acSDavid Sterba btrfs_set_pending_and_info(info, INODE_MAP_CACHE, 70507802534SQu Wenruo "enabling inode map caching"); 7063818aea2SQu Wenruo break; 7073818aea2SQu Wenruo case Opt_noinode_cache: 7087e1876acSDavid Sterba btrfs_clear_pending_and_info(info, INODE_MAP_CACHE, 70907802534SQu Wenruo "disabling inode map caching"); 7104b9465cbSChris Mason break; 71188c2ba3bSJosef Bacik case Opt_clear_cache: 7123cdde224SJeff Mahoney btrfs_set_and_info(info, CLEAR_CACHE, 71307802534SQu Wenruo "force clearing of disk cache"); 7140af3d00bSJosef Bacik break; 7154260f7c7SSage Weil case Opt_user_subvol_rm_allowed: 7164260f7c7SSage Weil btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 7174260f7c7SSage Weil break; 71891435650SChris Mason case Opt_enospc_debug: 71991435650SChris Mason btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 72091435650SChris Mason break; 72153036293SQu Wenruo case Opt_noenospc_debug: 72253036293SQu Wenruo btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG); 72353036293SQu Wenruo break; 7244cb5300bSChris Mason case Opt_defrag: 7253cdde224SJeff Mahoney btrfs_set_and_info(info, AUTO_DEFRAG, 72607802534SQu Wenruo "enabling auto defrag"); 7274cb5300bSChris Mason break; 728fc0ca9afSQu Wenruo case Opt_nodefrag: 7293cdde224SJeff Mahoney btrfs_clear_and_info(info, AUTO_DEFRAG, 73007802534SQu Wenruo "disabling auto defrag"); 731fc0ca9afSQu Wenruo break; 732af31f5e5SChris Mason case Opt_recovery: 7330b246afaSJeff Mahoney btrfs_warn(info, 7348dcddfa0SQu Wenruo "'recovery' is deprecated, use 'usebackuproot' instead"); 7358dcddfa0SQu Wenruo case Opt_usebackuproot: 7360b246afaSJeff Mahoney btrfs_info(info, 7378dcddfa0SQu Wenruo "trying to use backup root at mount time"); 7388dcddfa0SQu Wenruo btrfs_set_opt(info->mount_opt, USEBACKUPROOT); 739af31f5e5SChris Mason break; 7409555c6c1SIlya Dryomov case Opt_skip_balance: 7419555c6c1SIlya Dryomov btrfs_set_opt(info->mount_opt, SKIP_BALANCE); 7429555c6c1SIlya Dryomov break; 74321adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 74421adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 7450b246afaSJeff Mahoney btrfs_info(info, 746efe120a0SFrank Holton "enabling check integrity including extent data"); 74721adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, 74821adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA); 74921adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 75021adbd5cSStefan Behrens break; 75121adbd5cSStefan Behrens case Opt_check_integrity: 7520b246afaSJeff Mahoney btrfs_info(info, "enabling check integrity"); 75321adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 75421adbd5cSStefan Behrens break; 75521adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 7562c334e87SWang Shilong ret = match_int(&args[0], &intarg); 7572c334e87SWang Shilong if (ret) { 7582c334e87SWang Shilong goto out; 7592c334e87SWang Shilong } else if (intarg >= 0) { 76021adbd5cSStefan Behrens info->check_integrity_print_mask = intarg; 7610b246afaSJeff Mahoney btrfs_info(info, 7625d163e0eSJeff Mahoney "check_integrity_print_mask 0x%x", 76321adbd5cSStefan Behrens info->check_integrity_print_mask); 7642c334e87SWang Shilong } else { 7652c334e87SWang Shilong ret = -EINVAL; 7662c334e87SWang Shilong goto out; 76721adbd5cSStefan Behrens } 76821adbd5cSStefan Behrens break; 76921adbd5cSStefan Behrens #else 77021adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 77121adbd5cSStefan Behrens case Opt_check_integrity: 77221adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 7730b246afaSJeff Mahoney btrfs_err(info, 774efe120a0SFrank Holton "support for check_integrity* not compiled in!"); 77521adbd5cSStefan Behrens ret = -EINVAL; 77621adbd5cSStefan Behrens goto out; 77721adbd5cSStefan Behrens #endif 7788c342930SJeff Mahoney case Opt_fatal_errors: 7798c342930SJeff Mahoney if (strcmp(args[0].from, "panic") == 0) 7808c342930SJeff Mahoney btrfs_set_opt(info->mount_opt, 7818c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7828c342930SJeff Mahoney else if (strcmp(args[0].from, "bug") == 0) 7838c342930SJeff Mahoney btrfs_clear_opt(info->mount_opt, 7848c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7858c342930SJeff Mahoney else { 7868c342930SJeff Mahoney ret = -EINVAL; 7878c342930SJeff Mahoney goto out; 7888c342930SJeff Mahoney } 7898c342930SJeff Mahoney break; 7908b87dc17SDavid Sterba case Opt_commit_interval: 7918b87dc17SDavid Sterba intarg = 0; 7928b87dc17SDavid Sterba ret = match_int(&args[0], &intarg); 7938b87dc17SDavid Sterba if (ret < 0) { 7940b246afaSJeff Mahoney btrfs_err(info, "invalid commit interval"); 7958b87dc17SDavid Sterba ret = -EINVAL; 7968b87dc17SDavid Sterba goto out; 7978b87dc17SDavid Sterba } 7988b87dc17SDavid Sterba if (intarg > 0) { 7998b87dc17SDavid Sterba if (intarg > 300) { 8000b246afaSJeff Mahoney btrfs_warn(info, 8015d163e0eSJeff Mahoney "excessive commit interval %d", 8028b87dc17SDavid Sterba intarg); 8038b87dc17SDavid Sterba } 8048b87dc17SDavid Sterba info->commit_interval = intarg; 8058b87dc17SDavid Sterba } else { 8060b246afaSJeff Mahoney btrfs_info(info, 8075d163e0eSJeff Mahoney "using default commit interval %ds", 8088b87dc17SDavid Sterba BTRFS_DEFAULT_COMMIT_INTERVAL); 8098b87dc17SDavid Sterba info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 8108b87dc17SDavid Sterba } 8118b87dc17SDavid Sterba break; 812d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 813d0bd4560SJosef Bacik case Opt_fragment_all: 8140b246afaSJeff Mahoney btrfs_info(info, "fragmenting all space"); 815d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 816d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA); 817d0bd4560SJosef Bacik break; 818d0bd4560SJosef Bacik case Opt_fragment_metadata: 8190b246afaSJeff Mahoney btrfs_info(info, "fragmenting metadata"); 820d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, 821d0bd4560SJosef Bacik FRAGMENT_METADATA); 822d0bd4560SJosef Bacik break; 823d0bd4560SJosef Bacik case Opt_fragment_data: 8240b246afaSJeff Mahoney btrfs_info(info, "fragmenting data"); 825d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 826d0bd4560SJosef Bacik break; 827d0bd4560SJosef Bacik #endif 828a7a3f7caSSage Weil case Opt_err: 8290b246afaSJeff Mahoney btrfs_info(info, "unrecognized mount option '%s'", p); 830a7a3f7caSSage Weil ret = -EINVAL; 831a7a3f7caSSage Weil goto out; 83295e05289SChris Mason default: 833be20aa9dSChris Mason break; 83495e05289SChris Mason } 83595e05289SChris Mason } 83696da0919SQu Wenruo check: 83796da0919SQu Wenruo /* 83896da0919SQu Wenruo * Extra check for current option against current flag 83996da0919SQu Wenruo */ 8403cdde224SJeff Mahoney if (btrfs_test_opt(info, NOLOGREPLAY) && !(new_flags & MS_RDONLY)) { 8410b246afaSJeff Mahoney btrfs_err(info, 84296da0919SQu Wenruo "nologreplay must be used with ro mount option"); 84396da0919SQu Wenruo ret = -EINVAL; 84496da0919SQu Wenruo } 845a7a3f7caSSage Weil out: 8460b246afaSJeff Mahoney if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) && 8473cdde224SJeff Mahoney !btrfs_test_opt(info, FREE_SPACE_TREE) && 8483cdde224SJeff Mahoney !btrfs_test_opt(info, CLEAR_CACHE)) { 8490b246afaSJeff Mahoney btrfs_err(info, "cannot disable free space tree"); 85070f6d82eSOmar Sandoval ret = -EINVAL; 85170f6d82eSOmar Sandoval 85270f6d82eSOmar Sandoval } 8533cdde224SJeff Mahoney if (!ret && btrfs_test_opt(info, SPACE_CACHE)) 8540b246afaSJeff Mahoney btrfs_info(info, "disk space caching is enabled"); 8553cdde224SJeff Mahoney if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE)) 8560b246afaSJeff Mahoney btrfs_info(info, "using free space tree"); 857da495eccSJosef Bacik kfree(orig); 858a7a3f7caSSage Weil return ret; 859edf24abeSChristoph Hellwig } 860edf24abeSChristoph Hellwig 861edf24abeSChristoph Hellwig /* 862edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 863edf24abeSChristoph Hellwig * 864edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 865edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 866edf24abeSChristoph Hellwig */ 86797288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 86873f73415SJosef Bacik void *holder, char **subvol_name, u64 *subvol_objectid, 8695e2a4b25SDavid Sterba struct btrfs_fs_devices **fs_devices) 870edf24abeSChristoph Hellwig { 871edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 87283c8c9bdSJeff Liu char *device_name, *opts, *orig, *p; 8731493381fSWang Shilong char *num = NULL; 874edf24abeSChristoph Hellwig int error = 0; 875edf24abeSChristoph Hellwig 876edf24abeSChristoph Hellwig if (!options) 877830c4adbSJosef Bacik return 0; 878edf24abeSChristoph Hellwig 879edf24abeSChristoph Hellwig /* 880edf24abeSChristoph Hellwig * strsep changes the string, duplicate it because parse_options 881edf24abeSChristoph Hellwig * gets called twice 882edf24abeSChristoph Hellwig */ 883edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 884edf24abeSChristoph Hellwig if (!opts) 885edf24abeSChristoph Hellwig return -ENOMEM; 8863f3d0bc0STero Roponen orig = opts; 887edf24abeSChristoph Hellwig 888edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 889edf24abeSChristoph Hellwig int token; 890edf24abeSChristoph Hellwig if (!*p) 891edf24abeSChristoph Hellwig continue; 892edf24abeSChristoph Hellwig 893edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 894edf24abeSChristoph Hellwig switch (token) { 895edf24abeSChristoph Hellwig case Opt_subvol: 896a90e8b6fSIlya Dryomov kfree(*subvol_name); 897edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 8982c334e87SWang Shilong if (!*subvol_name) { 8992c334e87SWang Shilong error = -ENOMEM; 9002c334e87SWang Shilong goto out; 9012c334e87SWang Shilong } 902edf24abeSChristoph Hellwig break; 90373f73415SJosef Bacik case Opt_subvolid: 9041493381fSWang Shilong num = match_strdup(&args[0]); 9051493381fSWang Shilong if (num) { 9061493381fSWang Shilong *subvol_objectid = memparse(num, NULL); 9071493381fSWang Shilong kfree(num); 9084849f01dSJosef Bacik /* we want the original fs_tree */ 9091493381fSWang Shilong if (!*subvol_objectid) 9104849f01dSJosef Bacik *subvol_objectid = 9114849f01dSJosef Bacik BTRFS_FS_TREE_OBJECTID; 9122c334e87SWang Shilong } else { 9132c334e87SWang Shilong error = -EINVAL; 9142c334e87SWang Shilong goto out; 9154849f01dSJosef Bacik } 91673f73415SJosef Bacik break; 917e15d0542SXin Zhong case Opt_subvolrootid: 91862e85577SJeff Mahoney pr_warn("BTRFS: 'subvolrootid' mount option is deprecated and has no effect\n"); 919e15d0542SXin Zhong break; 92043e570b0SChristoph Hellwig case Opt_device: 92183c8c9bdSJeff Liu device_name = match_strdup(&args[0]); 92283c8c9bdSJeff Liu if (!device_name) { 92383c8c9bdSJeff Liu error = -ENOMEM; 92483c8c9bdSJeff Liu goto out; 92583c8c9bdSJeff Liu } 92683c8c9bdSJeff Liu error = btrfs_scan_one_device(device_name, 92743e570b0SChristoph Hellwig flags, holder, fs_devices); 92883c8c9bdSJeff Liu kfree(device_name); 92943e570b0SChristoph Hellwig if (error) 930830c4adbSJosef Bacik goto out; 93143e570b0SChristoph Hellwig break; 932edf24abeSChristoph Hellwig default: 933edf24abeSChristoph Hellwig break; 934edf24abeSChristoph Hellwig } 935edf24abeSChristoph Hellwig } 936edf24abeSChristoph Hellwig 937edf24abeSChristoph Hellwig out: 938830c4adbSJosef Bacik kfree(orig); 939edf24abeSChristoph Hellwig return error; 94095e05289SChris Mason } 94195e05289SChris Mason 94205dbe683SOmar Sandoval static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info, 94373f73415SJosef Bacik u64 subvol_objectid) 94473f73415SJosef Bacik { 945815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 94605dbe683SOmar Sandoval struct btrfs_root *fs_root; 94705dbe683SOmar Sandoval struct btrfs_root_ref *root_ref; 94805dbe683SOmar Sandoval struct btrfs_inode_ref *inode_ref; 94905dbe683SOmar Sandoval struct btrfs_key key; 95005dbe683SOmar Sandoval struct btrfs_path *path = NULL; 95105dbe683SOmar Sandoval char *name = NULL, *ptr; 95205dbe683SOmar Sandoval u64 dirid; 95305dbe683SOmar Sandoval int len; 95405dbe683SOmar Sandoval int ret; 95505dbe683SOmar Sandoval 95605dbe683SOmar Sandoval path = btrfs_alloc_path(); 95705dbe683SOmar Sandoval if (!path) { 95805dbe683SOmar Sandoval ret = -ENOMEM; 95905dbe683SOmar Sandoval goto err; 96005dbe683SOmar Sandoval } 96105dbe683SOmar Sandoval path->leave_spinning = 1; 96205dbe683SOmar Sandoval 9633ec83621SDavid Sterba name = kmalloc(PATH_MAX, GFP_KERNEL); 96405dbe683SOmar Sandoval if (!name) { 96505dbe683SOmar Sandoval ret = -ENOMEM; 96605dbe683SOmar Sandoval goto err; 96705dbe683SOmar Sandoval } 96805dbe683SOmar Sandoval ptr = name + PATH_MAX - 1; 96905dbe683SOmar Sandoval ptr[0] = '\0'; 97005dbe683SOmar Sandoval 97105dbe683SOmar Sandoval /* 97205dbe683SOmar Sandoval * Walk up the subvolume trees in the tree of tree roots by root 97305dbe683SOmar Sandoval * backrefs until we hit the top-level subvolume. 97405dbe683SOmar Sandoval */ 97505dbe683SOmar Sandoval while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) { 97605dbe683SOmar Sandoval key.objectid = subvol_objectid; 97705dbe683SOmar Sandoval key.type = BTRFS_ROOT_BACKREF_KEY; 97805dbe683SOmar Sandoval key.offset = (u64)-1; 97905dbe683SOmar Sandoval 98005dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 98105dbe683SOmar Sandoval if (ret < 0) { 98205dbe683SOmar Sandoval goto err; 98305dbe683SOmar Sandoval } else if (ret > 0) { 98405dbe683SOmar Sandoval ret = btrfs_previous_item(root, path, subvol_objectid, 98505dbe683SOmar Sandoval BTRFS_ROOT_BACKREF_KEY); 98605dbe683SOmar Sandoval if (ret < 0) { 98705dbe683SOmar Sandoval goto err; 98805dbe683SOmar Sandoval } else if (ret > 0) { 98905dbe683SOmar Sandoval ret = -ENOENT; 99005dbe683SOmar Sandoval goto err; 99105dbe683SOmar Sandoval } 99205dbe683SOmar Sandoval } 99305dbe683SOmar Sandoval 99405dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 99505dbe683SOmar Sandoval subvol_objectid = key.offset; 99605dbe683SOmar Sandoval 99705dbe683SOmar Sandoval root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], 99805dbe683SOmar Sandoval struct btrfs_root_ref); 99905dbe683SOmar Sandoval len = btrfs_root_ref_name_len(path->nodes[0], root_ref); 100005dbe683SOmar Sandoval ptr -= len + 1; 100105dbe683SOmar Sandoval if (ptr < name) { 100205dbe683SOmar Sandoval ret = -ENAMETOOLONG; 100305dbe683SOmar Sandoval goto err; 100405dbe683SOmar Sandoval } 100505dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 100605dbe683SOmar Sandoval (unsigned long)(root_ref + 1), len); 100705dbe683SOmar Sandoval ptr[0] = '/'; 100805dbe683SOmar Sandoval dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref); 100905dbe683SOmar Sandoval btrfs_release_path(path); 101005dbe683SOmar Sandoval 101105dbe683SOmar Sandoval key.objectid = subvol_objectid; 101205dbe683SOmar Sandoval key.type = BTRFS_ROOT_ITEM_KEY; 101305dbe683SOmar Sandoval key.offset = (u64)-1; 101405dbe683SOmar Sandoval fs_root = btrfs_read_fs_root_no_name(fs_info, &key); 101505dbe683SOmar Sandoval if (IS_ERR(fs_root)) { 101605dbe683SOmar Sandoval ret = PTR_ERR(fs_root); 101705dbe683SOmar Sandoval goto err; 101805dbe683SOmar Sandoval } 101905dbe683SOmar Sandoval 102005dbe683SOmar Sandoval /* 102105dbe683SOmar Sandoval * Walk up the filesystem tree by inode refs until we hit the 102205dbe683SOmar Sandoval * root directory. 102305dbe683SOmar Sandoval */ 102405dbe683SOmar Sandoval while (dirid != BTRFS_FIRST_FREE_OBJECTID) { 102505dbe683SOmar Sandoval key.objectid = dirid; 102605dbe683SOmar Sandoval key.type = BTRFS_INODE_REF_KEY; 102705dbe683SOmar Sandoval key.offset = (u64)-1; 102805dbe683SOmar Sandoval 102905dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); 103005dbe683SOmar Sandoval if (ret < 0) { 103105dbe683SOmar Sandoval goto err; 103205dbe683SOmar Sandoval } else if (ret > 0) { 103305dbe683SOmar Sandoval ret = btrfs_previous_item(fs_root, path, dirid, 103405dbe683SOmar Sandoval BTRFS_INODE_REF_KEY); 103505dbe683SOmar Sandoval if (ret < 0) { 103605dbe683SOmar Sandoval goto err; 103705dbe683SOmar Sandoval } else if (ret > 0) { 103805dbe683SOmar Sandoval ret = -ENOENT; 103905dbe683SOmar Sandoval goto err; 104005dbe683SOmar Sandoval } 104105dbe683SOmar Sandoval } 104205dbe683SOmar Sandoval 104305dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 104405dbe683SOmar Sandoval dirid = key.offset; 104505dbe683SOmar Sandoval 104605dbe683SOmar Sandoval inode_ref = btrfs_item_ptr(path->nodes[0], 104705dbe683SOmar Sandoval path->slots[0], 104805dbe683SOmar Sandoval struct btrfs_inode_ref); 104905dbe683SOmar Sandoval len = btrfs_inode_ref_name_len(path->nodes[0], 105005dbe683SOmar Sandoval inode_ref); 105105dbe683SOmar Sandoval ptr -= len + 1; 105205dbe683SOmar Sandoval if (ptr < name) { 105305dbe683SOmar Sandoval ret = -ENAMETOOLONG; 105405dbe683SOmar Sandoval goto err; 105505dbe683SOmar Sandoval } 105605dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 105705dbe683SOmar Sandoval (unsigned long)(inode_ref + 1), len); 105805dbe683SOmar Sandoval ptr[0] = '/'; 105905dbe683SOmar Sandoval btrfs_release_path(path); 106005dbe683SOmar Sandoval } 106105dbe683SOmar Sandoval } 106205dbe683SOmar Sandoval 106305dbe683SOmar Sandoval btrfs_free_path(path); 106405dbe683SOmar Sandoval if (ptr == name + PATH_MAX - 1) { 106505dbe683SOmar Sandoval name[0] = '/'; 106605dbe683SOmar Sandoval name[1] = '\0'; 106705dbe683SOmar Sandoval } else { 106805dbe683SOmar Sandoval memmove(name, ptr, name + PATH_MAX - ptr); 106905dbe683SOmar Sandoval } 107005dbe683SOmar Sandoval return name; 107105dbe683SOmar Sandoval 107205dbe683SOmar Sandoval err: 107305dbe683SOmar Sandoval btrfs_free_path(path); 107405dbe683SOmar Sandoval kfree(name); 107505dbe683SOmar Sandoval return ERR_PTR(ret); 107605dbe683SOmar Sandoval } 107705dbe683SOmar Sandoval 107805dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid) 107905dbe683SOmar Sandoval { 108005dbe683SOmar Sandoval struct btrfs_root *root = fs_info->tree_root; 108173f73415SJosef Bacik struct btrfs_dir_item *di; 108273f73415SJosef Bacik struct btrfs_path *path; 108373f73415SJosef Bacik struct btrfs_key location; 108473f73415SJosef Bacik u64 dir_id; 108573f73415SJosef Bacik 108673f73415SJosef Bacik path = btrfs_alloc_path(); 108773f73415SJosef Bacik if (!path) 108805dbe683SOmar Sandoval return -ENOMEM; 108973f73415SJosef Bacik path->leave_spinning = 1; 109073f73415SJosef Bacik 109173f73415SJosef Bacik /* 109273f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 109373f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 109473f73415SJosef Bacik * to mount. 109573f73415SJosef Bacik */ 1096815745cfSAl Viro dir_id = btrfs_super_root_dir(fs_info->super_copy); 109773f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 1098b0839166SJulia Lawall if (IS_ERR(di)) { 1099b0839166SJulia Lawall btrfs_free_path(path); 110005dbe683SOmar Sandoval return PTR_ERR(di); 1101b0839166SJulia Lawall } 110273f73415SJosef Bacik if (!di) { 110373f73415SJosef Bacik /* 110473f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 110573f73415SJosef Bacik * it's always been there, but don't freak out, just try and 110605dbe683SOmar Sandoval * mount the top-level subvolume. 110773f73415SJosef Bacik */ 110873f73415SJosef Bacik btrfs_free_path(path); 110905dbe683SOmar Sandoval *objectid = BTRFS_FS_TREE_OBJECTID; 111005dbe683SOmar Sandoval return 0; 111173f73415SJosef Bacik } 111273f73415SJosef Bacik 111373f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 111473f73415SJosef Bacik btrfs_free_path(path); 111505dbe683SOmar Sandoval *objectid = location.objectid; 111605dbe683SOmar Sandoval return 0; 111773f73415SJosef Bacik } 111873f73415SJosef Bacik 11198a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 11208a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 112156e033a7SDavid Sterba void *data) 11222e635a27SChris Mason { 11232e635a27SChris Mason struct inode *inode; 1124815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 11255d4f98a2SYan Zheng struct btrfs_key key; 112639279cc3SChris Mason int err; 11272e635a27SChris Mason 11282e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 11292e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 1130e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 1131af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 1132be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 11335103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 11342e635a27SChris Mason sb->s_time_gran = 1; 11350eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 113633268eafSJosef Bacik sb->s_flags |= MS_POSIXACL; 113749cf6f45SChris Ball #endif 1138357fdad0SMatthew Garrett sb->s_flags |= SB_I_VERSION; 1139da2f0f74SChris Mason sb->s_iflags |= SB_I_CGROUPWB; 11409e11ceeeSJan Kara 11419e11ceeeSJan Kara err = super_setup_bdi(sb); 11429e11ceeeSJan Kara if (err) { 11439e11ceeeSJan Kara btrfs_err(fs_info, "super_setup_bdi failed"); 11449e11ceeeSJan Kara return err; 11459e11ceeeSJan Kara } 11469e11ceeeSJan Kara 1147ad2b2c80SAl Viro err = open_ctree(sb, fs_devices, (char *)data); 1148ad2b2c80SAl Viro if (err) { 1149ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "open_ctree failed"); 1150ad2b2c80SAl Viro return err; 1151e20d96d6SChris Mason } 1152b888db2bSChris Mason 11535d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 11545d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 11555d4f98a2SYan Zheng key.offset = 0; 115698c7089cSAl Viro inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL); 11575d4f98a2SYan Zheng if (IS_ERR(inode)) { 11585d4f98a2SYan Zheng err = PTR_ERR(inode); 115939279cc3SChris Mason goto fail_close; 116039279cc3SChris Mason } 11612e635a27SChris Mason 116248fde701SAl Viro sb->s_root = d_make_root(inode); 116348fde701SAl Viro if (!sb->s_root) { 116439279cc3SChris Mason err = -ENOMEM; 116539279cc3SChris Mason goto fail_close; 11662e635a27SChris Mason } 116758176a96SJosef Bacik 116890a887c9SDan Magenheimer cleancache_init_fs(sb); 116959553edfSAl Viro sb->s_flags |= MS_ACTIVE; 11702e635a27SChris Mason return 0; 11712e635a27SChris Mason 117239279cc3SChris Mason fail_close: 11736bccf3abSJeff Mahoney close_ctree(fs_info); 1174d5719762SChris Mason return err; 1175d5719762SChris Mason } 1176d5719762SChris Mason 11776bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 1178d5719762SChris Mason { 1179d5719762SChris Mason struct btrfs_trans_handle *trans; 1180815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1181815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 1182df2ce34cSChris Mason 1183bc074524SJeff Mahoney trace_btrfs_sync_fs(fs_info, wait); 11841abe9b8aSliubo 1185d561c025SChris Mason if (!wait) { 1186815745cfSAl Viro filemap_flush(fs_info->btree_inode->i_mapping); 1187df2ce34cSChris Mason return 0; 1188d561c025SChris Mason } 1189771ed689SChris Mason 11906374e57aSChris Mason btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 1191771ed689SChris Mason 1192d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 119360376ce4SJosef Bacik if (IS_ERR(trans)) { 1194354aa0fbSMiao Xie /* no transaction, don't bother */ 11956b5fe46dSDavid Sterba if (PTR_ERR(trans) == -ENOENT) { 11966b5fe46dSDavid Sterba /* 11976b5fe46dSDavid Sterba * Exit unless we have some pending changes 11986b5fe46dSDavid Sterba * that need to go through commit 11996b5fe46dSDavid Sterba */ 12006b5fe46dSDavid Sterba if (fs_info->pending_changes == 0) 1201bd7de2c9SJosef Bacik return 0; 1202a53f4f8eSQu Wenruo /* 1203a53f4f8eSQu Wenruo * A non-blocking test if the fs is frozen. We must not 1204a53f4f8eSQu Wenruo * start a new transaction here otherwise a deadlock 1205a53f4f8eSQu Wenruo * happens. The pending operations are delayed to the 1206a53f4f8eSQu Wenruo * next commit after thawing. 1207a53f4f8eSQu Wenruo */ 1208*a7e3c5f2SRakesh Pandit if (sb_start_write_trylock(sb)) 1209*a7e3c5f2SRakesh Pandit sb_end_write(sb); 1210a53f4f8eSQu Wenruo else 1211a53f4f8eSQu Wenruo return 0; 12126b5fe46dSDavid Sterba trans = btrfs_start_transaction(root, 0); 121360376ce4SJosef Bacik } 121498bd5c54SDavid Sterba if (IS_ERR(trans)) 121598bd5c54SDavid Sterba return PTR_ERR(trans); 12166b5fe46dSDavid Sterba } 12173a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 1218d5719762SChris Mason } 1219d5719762SChris Mason 122034c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 1221a9572a15SEric Paris { 1222815745cfSAl Viro struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 1223200da64eSTsutomu Itoh char *compress_type; 1224a9572a15SEric Paris 12253cdde224SJeff Mahoney if (btrfs_test_opt(info, DEGRADED)) 1226a9572a15SEric Paris seq_puts(seq, ",degraded"); 12273cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATASUM)) 1228a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 12293cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATACOW)) 1230a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 12313cdde224SJeff Mahoney if (btrfs_test_opt(info, NOBARRIER)) 1232a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 123395ac567aSFilipe David Borba Manana if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE) 1234c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",max_inline=%llu", info->max_inline); 1235a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 1236a9572a15SEric Paris num_online_cpus() + 2, 8)) 1237a9572a15SEric Paris seq_printf(seq, ",thread_pool=%d", info->thread_pool_size); 12383cdde224SJeff Mahoney if (btrfs_test_opt(info, COMPRESS)) { 1239200da64eSTsutomu Itoh if (info->compress_type == BTRFS_COMPRESS_ZLIB) 1240200da64eSTsutomu Itoh compress_type = "zlib"; 12415c1aab1dSNick Terrell else if (info->compress_type == BTRFS_COMPRESS_LZO) 1242200da64eSTsutomu Itoh compress_type = "lzo"; 12435c1aab1dSNick Terrell else 12445c1aab1dSNick Terrell compress_type = "zstd"; 12453cdde224SJeff Mahoney if (btrfs_test_opt(info, FORCE_COMPRESS)) 1246200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 1247200da64eSTsutomu Itoh else 1248200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 1249200da64eSTsutomu Itoh } 12503cdde224SJeff Mahoney if (btrfs_test_opt(info, NOSSD)) 1251c289811cSChris Mason seq_puts(seq, ",nossd"); 12523cdde224SJeff Mahoney if (btrfs_test_opt(info, SSD_SPREAD)) 1253451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 12543cdde224SJeff Mahoney else if (btrfs_test_opt(info, SSD)) 1255a9572a15SEric Paris seq_puts(seq, ",ssd"); 12563cdde224SJeff Mahoney if (btrfs_test_opt(info, NOTREELOG)) 12576b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 12583cdde224SJeff Mahoney if (btrfs_test_opt(info, NOLOGREPLAY)) 125996da0919SQu Wenruo seq_puts(seq, ",nologreplay"); 12603cdde224SJeff Mahoney if (btrfs_test_opt(info, FLUSHONCOMMIT)) 12616b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 12623cdde224SJeff Mahoney if (btrfs_test_opt(info, DISCARD)) 126320a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 12640b246afaSJeff Mahoney if (!(info->sb->s_flags & MS_POSIXACL)) 1265a9572a15SEric Paris seq_puts(seq, ",noacl"); 12663cdde224SJeff Mahoney if (btrfs_test_opt(info, SPACE_CACHE)) 1267200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 12683cdde224SJeff Mahoney else if (btrfs_test_opt(info, FREE_SPACE_TREE)) 126970f6d82eSOmar Sandoval seq_puts(seq, ",space_cache=v2"); 127073bc1876SJosef Bacik else 12718965593eSDavid Sterba seq_puts(seq, ",nospace_cache"); 12723cdde224SJeff Mahoney if (btrfs_test_opt(info, RESCAN_UUID_TREE)) 1273f420ee1eSStefan Behrens seq_puts(seq, ",rescan_uuid_tree"); 12743cdde224SJeff Mahoney if (btrfs_test_opt(info, CLEAR_CACHE)) 1275200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 12763cdde224SJeff Mahoney if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED)) 1277200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 12783cdde224SJeff Mahoney if (btrfs_test_opt(info, ENOSPC_DEBUG)) 12790942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 12803cdde224SJeff Mahoney if (btrfs_test_opt(info, AUTO_DEFRAG)) 12810942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 12823cdde224SJeff Mahoney if (btrfs_test_opt(info, INODE_MAP_CACHE)) 12830942caa3SDavid Sterba seq_puts(seq, ",inode_cache"); 12843cdde224SJeff Mahoney if (btrfs_test_opt(info, SKIP_BALANCE)) 12859555c6c1SIlya Dryomov seq_puts(seq, ",skip_balance"); 12868507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 12873cdde224SJeff Mahoney if (btrfs_test_opt(info, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA)) 12888507d216SWang Shilong seq_puts(seq, ",check_int_data"); 12893cdde224SJeff Mahoney else if (btrfs_test_opt(info, CHECK_INTEGRITY)) 12908507d216SWang Shilong seq_puts(seq, ",check_int"); 12918507d216SWang Shilong if (info->check_integrity_print_mask) 12928507d216SWang Shilong seq_printf(seq, ",check_int_print_mask=%d", 12938507d216SWang Shilong info->check_integrity_print_mask); 12948507d216SWang Shilong #endif 12958507d216SWang Shilong if (info->metadata_ratio) 12968507d216SWang Shilong seq_printf(seq, ",metadata_ratio=%d", 12978507d216SWang Shilong info->metadata_ratio); 12983cdde224SJeff Mahoney if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR)) 12998c342930SJeff Mahoney seq_puts(seq, ",fatal_errors=panic"); 13008b87dc17SDavid Sterba if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL) 13018b87dc17SDavid Sterba seq_printf(seq, ",commit=%d", info->commit_interval); 1302d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 13033cdde224SJeff Mahoney if (btrfs_test_opt(info, FRAGMENT_DATA)) 1304d0bd4560SJosef Bacik seq_puts(seq, ",fragment=data"); 13053cdde224SJeff Mahoney if (btrfs_test_opt(info, FRAGMENT_METADATA)) 1306d0bd4560SJosef Bacik seq_puts(seq, ",fragment=metadata"); 1307d0bd4560SJosef Bacik #endif 1308c8d3fe02SOmar Sandoval seq_printf(seq, ",subvolid=%llu", 1309c8d3fe02SOmar Sandoval BTRFS_I(d_inode(dentry))->root->root_key.objectid); 1310c8d3fe02SOmar Sandoval seq_puts(seq, ",subvol="); 1311c8d3fe02SOmar Sandoval seq_dentry(seq, dentry, " \t\n\\"); 1312a9572a15SEric Paris return 0; 1313a9572a15SEric Paris } 1314a9572a15SEric Paris 1315a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 13162e635a27SChris Mason { 1317815745cfSAl Viro struct btrfs_fs_info *p = data; 1318815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(s); 13194b82d6e4SYan 1320815745cfSAl Viro return fs_info->fs_devices == p->fs_devices; 13214b82d6e4SYan } 13224b82d6e4SYan 1323450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 1324450ba0eaSJosef Bacik { 13256de1d09dSAl Viro int err = set_anon_super(s, data); 13266de1d09dSAl Viro if (!err) 1327450ba0eaSJosef Bacik s->s_fs_info = data; 13286de1d09dSAl Viro return err; 1329450ba0eaSJosef Bacik } 1330450ba0eaSJosef Bacik 1331830c4adbSJosef Bacik /* 1332f9d9ef62SDavid Sterba * subvolumes are identified by ino 256 1333f9d9ef62SDavid Sterba */ 1334f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode) 1335f9d9ef62SDavid Sterba { 1336f9d9ef62SDavid Sterba if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 1337f9d9ef62SDavid Sterba return 1; 1338f9d9ef62SDavid Sterba return 0; 1339f9d9ef62SDavid Sterba } 1340f9d9ef62SDavid Sterba 1341f9d9ef62SDavid Sterba /* 1342e6e4dbe8SOmar Sandoval * This will add subvolid=0 to the argument string while removing any subvol= 1343e6e4dbe8SOmar Sandoval * and subvolid= arguments to make sure we get the top-level root for path 1344e6e4dbe8SOmar Sandoval * walking to the subvol we want. 1345830c4adbSJosef Bacik */ 1346830c4adbSJosef Bacik static char *setup_root_args(char *args) 1347830c4adbSJosef Bacik { 1348e6e4dbe8SOmar Sandoval char *buf, *dst, *sep; 1349830c4adbSJosef Bacik 1350e6e4dbe8SOmar Sandoval if (!args) 13513ec83621SDavid Sterba return kstrdup("subvolid=0", GFP_KERNEL); 1352830c4adbSJosef Bacik 1353e6e4dbe8SOmar Sandoval /* The worst case is that we add ",subvolid=0" to the end. */ 13543ec83621SDavid Sterba buf = dst = kmalloc(strlen(args) + strlen(",subvolid=0") + 1, 13553ec83621SDavid Sterba GFP_KERNEL); 1356f60d16a8SJim Meyering if (!buf) 1357f60d16a8SJim Meyering return NULL; 1358830c4adbSJosef Bacik 1359e6e4dbe8SOmar Sandoval while (1) { 1360e6e4dbe8SOmar Sandoval sep = strchrnul(args, ','); 1361e6e4dbe8SOmar Sandoval if (!strstarts(args, "subvol=") && 1362e6e4dbe8SOmar Sandoval !strstarts(args, "subvolid=")) { 1363e6e4dbe8SOmar Sandoval memcpy(dst, args, sep - args); 1364e6e4dbe8SOmar Sandoval dst += sep - args; 1365e6e4dbe8SOmar Sandoval *dst++ = ','; 1366830c4adbSJosef Bacik } 1367e6e4dbe8SOmar Sandoval if (*sep) 1368e6e4dbe8SOmar Sandoval args = sep + 1; 1369e6e4dbe8SOmar Sandoval else 1370e6e4dbe8SOmar Sandoval break; 1371e6e4dbe8SOmar Sandoval } 1372f60d16a8SJim Meyering strcpy(dst, "subvolid=0"); 1373830c4adbSJosef Bacik 1374f60d16a8SJim Meyering return buf; 1375830c4adbSJosef Bacik } 1376830c4adbSJosef Bacik 1377bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid, 1378bb289b7bSOmar Sandoval int flags, const char *device_name, 1379bb289b7bSOmar Sandoval char *data) 1380830c4adbSJosef Bacik { 1381830c4adbSJosef Bacik struct dentry *root; 1382fa330659SOmar Sandoval struct vfsmount *mnt = NULL; 1383830c4adbSJosef Bacik char *newargs; 1384fa330659SOmar Sandoval int ret; 1385830c4adbSJosef Bacik 1386830c4adbSJosef Bacik newargs = setup_root_args(data); 1387fa330659SOmar Sandoval if (!newargs) { 1388fa330659SOmar Sandoval root = ERR_PTR(-ENOMEM); 1389fa330659SOmar Sandoval goto out; 1390fa330659SOmar Sandoval } 13910723a047SHarald Hoyer 1392fa330659SOmar Sandoval mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, newargs); 1393fa330659SOmar Sandoval if (PTR_ERR_OR_ZERO(mnt) == -EBUSY) { 13940723a047SHarald Hoyer if (flags & MS_RDONLY) { 1395fa330659SOmar Sandoval mnt = vfs_kern_mount(&btrfs_fs_type, flags & ~MS_RDONLY, 1396fa330659SOmar Sandoval device_name, newargs); 13970723a047SHarald Hoyer } else { 1398fa330659SOmar Sandoval mnt = vfs_kern_mount(&btrfs_fs_type, flags | MS_RDONLY, 1399fa330659SOmar Sandoval device_name, newargs); 14000040e606SChristoph Jaeger if (IS_ERR(mnt)) { 1401fa330659SOmar Sandoval root = ERR_CAST(mnt); 1402fa330659SOmar Sandoval mnt = NULL; 1403fa330659SOmar Sandoval goto out; 14040040e606SChristoph Jaeger } 14050723a047SHarald Hoyer 1406773cd04eSOmar Sandoval down_write(&mnt->mnt_sb->s_umount); 1407fa330659SOmar Sandoval ret = btrfs_remount(mnt->mnt_sb, &flags, NULL); 1408773cd04eSOmar Sandoval up_write(&mnt->mnt_sb->s_umount); 1409fa330659SOmar Sandoval if (ret < 0) { 1410fa330659SOmar Sandoval root = ERR_PTR(ret); 1411fa330659SOmar Sandoval goto out; 14120723a047SHarald Hoyer } 14130723a047SHarald Hoyer } 14140723a047SHarald Hoyer } 1415fa330659SOmar Sandoval if (IS_ERR(mnt)) { 1416fa330659SOmar Sandoval root = ERR_CAST(mnt); 1417fa330659SOmar Sandoval mnt = NULL; 1418fa330659SOmar Sandoval goto out; 1419fa330659SOmar Sandoval } 1420830c4adbSJosef Bacik 142105dbe683SOmar Sandoval if (!subvol_name) { 142205dbe683SOmar Sandoval if (!subvol_objectid) { 142305dbe683SOmar Sandoval ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb), 142405dbe683SOmar Sandoval &subvol_objectid); 142505dbe683SOmar Sandoval if (ret) { 142605dbe683SOmar Sandoval root = ERR_PTR(ret); 142705dbe683SOmar Sandoval goto out; 142805dbe683SOmar Sandoval } 142905dbe683SOmar Sandoval } 143005dbe683SOmar Sandoval subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb), 143105dbe683SOmar Sandoval subvol_objectid); 143205dbe683SOmar Sandoval if (IS_ERR(subvol_name)) { 143305dbe683SOmar Sandoval root = ERR_CAST(subvol_name); 143405dbe683SOmar Sandoval subvol_name = NULL; 143505dbe683SOmar Sandoval goto out; 143605dbe683SOmar Sandoval } 143705dbe683SOmar Sandoval 143805dbe683SOmar Sandoval } 143905dbe683SOmar Sandoval 1440ea441d11SAl Viro root = mount_subtree(mnt, subvol_name); 1441fa330659SOmar Sandoval /* mount_subtree() drops our reference on the vfsmount. */ 1442fa330659SOmar Sandoval mnt = NULL; 1443830c4adbSJosef Bacik 1444bb289b7bSOmar Sandoval if (!IS_ERR(root)) { 1445ea441d11SAl Viro struct super_block *s = root->d_sb; 1446ab8d0fc4SJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(s); 1447bb289b7bSOmar Sandoval struct inode *root_inode = d_inode(root); 1448bb289b7bSOmar Sandoval u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid; 1449bb289b7bSOmar Sandoval 1450bb289b7bSOmar Sandoval ret = 0; 1451bb289b7bSOmar Sandoval if (!is_subvolume_inode(root_inode)) { 1452ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "'%s' is not a valid subvolume", 1453bb289b7bSOmar Sandoval subvol_name); 1454bb289b7bSOmar Sandoval ret = -EINVAL; 1455bb289b7bSOmar Sandoval } 1456bb289b7bSOmar Sandoval if (subvol_objectid && root_objectid != subvol_objectid) { 145705dbe683SOmar Sandoval /* 145805dbe683SOmar Sandoval * This will also catch a race condition where a 145905dbe683SOmar Sandoval * subvolume which was passed by ID is renamed and 146005dbe683SOmar Sandoval * another subvolume is renamed over the old location. 146105dbe683SOmar Sandoval */ 1462ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 1463ab8d0fc4SJeff Mahoney "subvol '%s' does not match subvolid %llu", 1464bb289b7bSOmar Sandoval subvol_name, subvol_objectid); 1465bb289b7bSOmar Sandoval ret = -EINVAL; 1466bb289b7bSOmar Sandoval } 1467bb289b7bSOmar Sandoval if (ret) { 1468ea441d11SAl Viro dput(root); 1469bb289b7bSOmar Sandoval root = ERR_PTR(ret); 1470ea441d11SAl Viro deactivate_locked_super(s); 1471bb289b7bSOmar Sandoval } 1472f9d9ef62SDavid Sterba } 1473f9d9ef62SDavid Sterba 1474fa330659SOmar Sandoval out: 1475fa330659SOmar Sandoval mntput(mnt); 1476fa330659SOmar Sandoval kfree(newargs); 1477fa330659SOmar Sandoval kfree(subvol_name); 1478830c4adbSJosef Bacik return root; 1479830c4adbSJosef Bacik } 1480450ba0eaSJosef Bacik 1481f667aef6SQu Wenruo static int parse_security_options(char *orig_opts, 1482f667aef6SQu Wenruo struct security_mnt_opts *sec_opts) 1483f667aef6SQu Wenruo { 1484f667aef6SQu Wenruo char *secdata = NULL; 1485f667aef6SQu Wenruo int ret = 0; 1486f667aef6SQu Wenruo 1487f667aef6SQu Wenruo secdata = alloc_secdata(); 1488f667aef6SQu Wenruo if (!secdata) 1489f667aef6SQu Wenruo return -ENOMEM; 1490f667aef6SQu Wenruo ret = security_sb_copy_data(orig_opts, secdata); 1491f667aef6SQu Wenruo if (ret) { 1492f667aef6SQu Wenruo free_secdata(secdata); 1493f667aef6SQu Wenruo return ret; 1494f667aef6SQu Wenruo } 1495f667aef6SQu Wenruo ret = security_sb_parse_opts_str(secdata, sec_opts); 1496f667aef6SQu Wenruo free_secdata(secdata); 1497f667aef6SQu Wenruo return ret; 1498f667aef6SQu Wenruo } 1499f667aef6SQu Wenruo 1500f667aef6SQu Wenruo static int setup_security_options(struct btrfs_fs_info *fs_info, 1501f667aef6SQu Wenruo struct super_block *sb, 1502f667aef6SQu Wenruo struct security_mnt_opts *sec_opts) 1503f667aef6SQu Wenruo { 1504f667aef6SQu Wenruo int ret = 0; 1505f667aef6SQu Wenruo 1506f667aef6SQu Wenruo /* 1507f667aef6SQu Wenruo * Call security_sb_set_mnt_opts() to check whether new sec_opts 1508f667aef6SQu Wenruo * is valid. 1509f667aef6SQu Wenruo */ 1510f667aef6SQu Wenruo ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL); 1511f667aef6SQu Wenruo if (ret) 1512f667aef6SQu Wenruo return ret; 1513f667aef6SQu Wenruo 1514a43bb39bSQu Wenruo #ifdef CONFIG_SECURITY 1515f667aef6SQu Wenruo if (!fs_info->security_opts.num_mnt_opts) { 1516f667aef6SQu Wenruo /* first time security setup, copy sec_opts to fs_info */ 1517f667aef6SQu Wenruo memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts)); 1518f667aef6SQu Wenruo } else { 1519f667aef6SQu Wenruo /* 1520180e4d47SLuis de Bethencourt * Since SELinux (the only one supporting security_mnt_opts) 1521180e4d47SLuis de Bethencourt * does NOT support changing context during remount/mount of 1522180e4d47SLuis de Bethencourt * the same sb, this must be the same or part of the same 1523180e4d47SLuis de Bethencourt * security options, just free it. 1524f667aef6SQu Wenruo */ 1525f667aef6SQu Wenruo security_free_mnt_opts(sec_opts); 1526f667aef6SQu Wenruo } 1527a43bb39bSQu Wenruo #endif 1528f667aef6SQu Wenruo return ret; 1529f667aef6SQu Wenruo } 1530f667aef6SQu Wenruo 1531edf24abeSChristoph Hellwig /* 1532edf24abeSChristoph Hellwig * Find a superblock for the given device / mount point. 1533edf24abeSChristoph Hellwig * 1534edf24abeSChristoph Hellwig * Note: This is based on get_sb_bdev from fs/super.c with a few additions 1535edf24abeSChristoph Hellwig * for multiple device setup. Make sure to keep it in sync. 1536edf24abeSChristoph Hellwig */ 1537061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1538306e16ceSDavid Sterba const char *device_name, void *data) 15394b82d6e4SYan { 15404b82d6e4SYan struct block_device *bdev = NULL; 15414b82d6e4SYan struct super_block *s; 15428a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices = NULL; 1543450ba0eaSJosef Bacik struct btrfs_fs_info *fs_info = NULL; 1544f667aef6SQu Wenruo struct security_mnt_opts new_sec_opts; 154597288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 154673f73415SJosef Bacik char *subvol_name = NULL; 154773f73415SJosef Bacik u64 subvol_objectid = 0; 15484b82d6e4SYan int error = 0; 15494b82d6e4SYan 155097288f2cSChristoph Hellwig if (!(flags & MS_RDONLY)) 155197288f2cSChristoph Hellwig mode |= FMODE_WRITE; 155297288f2cSChristoph Hellwig 155397288f2cSChristoph Hellwig error = btrfs_parse_early_options(data, mode, fs_type, 155473f73415SJosef Bacik &subvol_name, &subvol_objectid, 15555e2a4b25SDavid Sterba &fs_devices); 1556f23c8af8SIlya Dryomov if (error) { 1557f23c8af8SIlya Dryomov kfree(subvol_name); 1558061dbc6bSAl Viro return ERR_PTR(error); 1559f23c8af8SIlya Dryomov } 1560edf24abeSChristoph Hellwig 156105dbe683SOmar Sandoval if (subvol_name || subvol_objectid != BTRFS_FS_TREE_OBJECTID) { 1562fa330659SOmar Sandoval /* mount_subvol() will free subvol_name. */ 1563bb289b7bSOmar Sandoval return mount_subvol(subvol_name, subvol_objectid, flags, 1564bb289b7bSOmar Sandoval device_name, data); 1565830c4adbSJosef Bacik } 1566830c4adbSJosef Bacik 1567f667aef6SQu Wenruo security_init_mnt_opts(&new_sec_opts); 1568f667aef6SQu Wenruo if (data) { 1569f667aef6SQu Wenruo error = parse_security_options(data, &new_sec_opts); 15708a4b83ccSChris Mason if (error) 1571830c4adbSJosef Bacik return ERR_PTR(error); 1572f667aef6SQu Wenruo } 1573f667aef6SQu Wenruo 1574f667aef6SQu Wenruo error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices); 1575f667aef6SQu Wenruo if (error) 1576f667aef6SQu Wenruo goto error_sec_opts; 15774b82d6e4SYan 1578450ba0eaSJosef Bacik /* 1579450ba0eaSJosef Bacik * Setup a dummy root and fs_info for test/set super. This is because 1580450ba0eaSJosef Bacik * we don't actually fill this stuff out until open_ctree, but we need 1581450ba0eaSJosef Bacik * it for searching for existing supers, so this lets us do that and 1582450ba0eaSJosef Bacik * then open_ctree will properly initialize everything later. 1583450ba0eaSJosef Bacik */ 15843ec83621SDavid Sterba fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL); 1585f667aef6SQu Wenruo if (!fs_info) { 1586f667aef6SQu Wenruo error = -ENOMEM; 1587f667aef6SQu Wenruo goto error_sec_opts; 1588f667aef6SQu Wenruo } 158904d21a24SIlya Dryomov 1590450ba0eaSJosef Bacik fs_info->fs_devices = fs_devices; 1591450ba0eaSJosef Bacik 15923ec83621SDavid Sterba fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL); 15933ec83621SDavid Sterba fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL); 1594f667aef6SQu Wenruo security_init_mnt_opts(&fs_info->security_opts); 15956c41761fSDavid Sterba if (!fs_info->super_copy || !fs_info->super_for_commit) { 15966c41761fSDavid Sterba error = -ENOMEM; 159704d21a24SIlya Dryomov goto error_fs_info; 159804d21a24SIlya Dryomov } 159904d21a24SIlya Dryomov 160004d21a24SIlya Dryomov error = btrfs_open_devices(fs_devices, mode, fs_type); 160104d21a24SIlya Dryomov if (error) 160204d21a24SIlya Dryomov goto error_fs_info; 160304d21a24SIlya Dryomov 160404d21a24SIlya Dryomov if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) { 160504d21a24SIlya Dryomov error = -EACCES; 16066c41761fSDavid Sterba goto error_close_devices; 16076c41761fSDavid Sterba } 16086c41761fSDavid Sterba 1609dfe25020SChris Mason bdev = fs_devices->latest_bdev; 16109249e17fSDavid Howells s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC, 16119249e17fSDavid Howells fs_info); 1612830c4adbSJosef Bacik if (IS_ERR(s)) { 1613830c4adbSJosef Bacik error = PTR_ERR(s); 1614830c4adbSJosef Bacik goto error_close_devices; 1615830c4adbSJosef Bacik } 16164b82d6e4SYan 16174b82d6e4SYan if (s->s_root) { 16182b82032cSYan Zheng btrfs_close_devices(fs_devices); 16196c41761fSDavid Sterba free_fs_info(fs_info); 162059553edfSAl Viro if ((flags ^ s->s_flags) & MS_RDONLY) 162159553edfSAl Viro error = -EBUSY; 16224b82d6e4SYan } else { 1623a1c6f057SDmitry Monakhov snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev); 1624815745cfSAl Viro btrfs_sb(s)->bdev_holder = fs_type; 162556e033a7SDavid Sterba error = btrfs_fill_super(s, fs_devices, data); 16264b82d6e4SYan } 162705dbe683SOmar Sandoval if (error) { 1628e15d0542SXin Zhong deactivate_locked_super(s); 1629f667aef6SQu Wenruo goto error_sec_opts; 1630f667aef6SQu Wenruo } 1631f667aef6SQu Wenruo 1632f667aef6SQu Wenruo fs_info = btrfs_sb(s); 1633f667aef6SQu Wenruo error = setup_security_options(fs_info, s, &new_sec_opts); 1634f667aef6SQu Wenruo if (error) { 1635f667aef6SQu Wenruo deactivate_locked_super(s); 1636f667aef6SQu Wenruo goto error_sec_opts; 1637f667aef6SQu Wenruo } 16384b82d6e4SYan 163905dbe683SOmar Sandoval return dget(s->s_root); 16404b82d6e4SYan 1641c146afadSYan Zheng error_close_devices: 16428a4b83ccSChris Mason btrfs_close_devices(fs_devices); 164304d21a24SIlya Dryomov error_fs_info: 16446c41761fSDavid Sterba free_fs_info(fs_info); 1645f667aef6SQu Wenruo error_sec_opts: 1646f667aef6SQu Wenruo security_free_mnt_opts(&new_sec_opts); 1647061dbc6bSAl Viro return ERR_PTR(error); 16484b82d6e4SYan } 16492e635a27SChris Mason 16500d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 16510d2450abSSergei Trofimovich int new_pool_size, int old_pool_size) 16520d2450abSSergei Trofimovich { 16530d2450abSSergei Trofimovich if (new_pool_size == old_pool_size) 16540d2450abSSergei Trofimovich return; 16550d2450abSSergei Trofimovich 16560d2450abSSergei Trofimovich fs_info->thread_pool_size = new_pool_size; 16570d2450abSSergei Trofimovich 1658efe120a0SFrank Holton btrfs_info(fs_info, "resize thread pool %d -> %d", 16590d2450abSSergei Trofimovich old_pool_size, new_pool_size); 16600d2450abSSergei Trofimovich 16615cdc7ad3SQu Wenruo btrfs_workqueue_set_max(fs_info->workers, new_pool_size); 1662afe3d242SQu Wenruo btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size); 1663a8c93d4eSQu Wenruo btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size); 1664e66f0bb1SQu Wenruo btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size); 1665fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size); 1666fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size); 1667fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_write_workers, 1668fccb5d86SQu Wenruo new_pool_size); 1669fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size); 1670fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size); 16715b3bc44eSQu Wenruo btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size); 1672736cfa15SQu Wenruo btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size); 16730339ef2fSQu Wenruo btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers, 1674ff023aacSStefan Behrens new_pool_size); 16750d2450abSSergei Trofimovich } 16760d2450abSSergei Trofimovich 1677f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info) 1678dc81cdc5SMiao Xie { 1679dc81cdc5SMiao Xie set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1680f42a34b2SMiao Xie } 1681dc81cdc5SMiao Xie 1682f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info, 1683f42a34b2SMiao Xie unsigned long old_opts, int flags) 1684f42a34b2SMiao Xie { 1685dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1686dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1687dc81cdc5SMiao Xie (flags & MS_RDONLY))) { 1688dc81cdc5SMiao Xie /* wait for any defraggers to finish */ 1689dc81cdc5SMiao Xie wait_event(fs_info->transaction_wait, 1690dc81cdc5SMiao Xie (atomic_read(&fs_info->defrag_running) == 0)); 1691dc81cdc5SMiao Xie if (flags & MS_RDONLY) 1692dc81cdc5SMiao Xie sync_filesystem(fs_info->sb); 1693dc81cdc5SMiao Xie } 1694dc81cdc5SMiao Xie } 1695dc81cdc5SMiao Xie 1696dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info, 1697dc81cdc5SMiao Xie unsigned long old_opts) 1698dc81cdc5SMiao Xie { 1699dc81cdc5SMiao Xie /* 1700180e4d47SLuis de Bethencourt * We need to cleanup all defragable inodes if the autodefragment is 1701180e4d47SLuis de Bethencourt * close or the filesystem is read only. 1702dc81cdc5SMiao Xie */ 1703dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1704bc98a42cSDavid Howells (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) { 1705dc81cdc5SMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 1706dc81cdc5SMiao Xie } 1707dc81cdc5SMiao Xie 1708dc81cdc5SMiao Xie clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1709dc81cdc5SMiao Xie } 1710dc81cdc5SMiao Xie 1711c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1712c146afadSYan Zheng { 1713815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1714815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 171549b25e05SJeff Mahoney unsigned old_flags = sb->s_flags; 171649b25e05SJeff Mahoney unsigned long old_opts = fs_info->mount_opt; 171749b25e05SJeff Mahoney unsigned long old_compress_type = fs_info->compress_type; 171849b25e05SJeff Mahoney u64 old_max_inline = fs_info->max_inline; 171949b25e05SJeff Mahoney int old_thread_pool_size = fs_info->thread_pool_size; 172049b25e05SJeff Mahoney unsigned int old_metadata_ratio = fs_info->metadata_ratio; 1721c146afadSYan Zheng int ret; 1722c146afadSYan Zheng 172302b9984dSTheodore Ts'o sync_filesystem(sb); 1724f42a34b2SMiao Xie btrfs_remount_prepare(fs_info); 1725dc81cdc5SMiao Xie 1726f667aef6SQu Wenruo if (data) { 1727f667aef6SQu Wenruo struct security_mnt_opts new_sec_opts; 1728f667aef6SQu Wenruo 1729f667aef6SQu Wenruo security_init_mnt_opts(&new_sec_opts); 1730f667aef6SQu Wenruo ret = parse_security_options(data, &new_sec_opts); 1731f667aef6SQu Wenruo if (ret) 1732f667aef6SQu Wenruo goto restore; 1733f667aef6SQu Wenruo ret = setup_security_options(fs_info, sb, 1734f667aef6SQu Wenruo &new_sec_opts); 1735f667aef6SQu Wenruo if (ret) { 1736f667aef6SQu Wenruo security_free_mnt_opts(&new_sec_opts); 1737f667aef6SQu Wenruo goto restore; 1738f667aef6SQu Wenruo } 1739f667aef6SQu Wenruo } 1740f667aef6SQu Wenruo 17412ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, data, *flags); 174249b25e05SJeff Mahoney if (ret) { 174349b25e05SJeff Mahoney ret = -EINVAL; 174449b25e05SJeff Mahoney goto restore; 174549b25e05SJeff Mahoney } 1746b288052eSChris Mason 1747f42a34b2SMiao Xie btrfs_remount_begin(fs_info, old_opts, *flags); 17480d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 17490d2450abSSergei Trofimovich fs_info->thread_pool_size, old_thread_pool_size); 17500d2450abSSergei Trofimovich 1751bc98a42cSDavid Howells if ((bool)(*flags & MS_RDONLY) == sb_rdonly(sb)) 1752dc81cdc5SMiao Xie goto out; 1753c146afadSYan Zheng 1754c146afadSYan Zheng if (*flags & MS_RDONLY) { 17558dabb742SStefan Behrens /* 17568dabb742SStefan Behrens * this also happens on 'umount -rf' or on shutdown, when 17578dabb742SStefan Behrens * the filesystem is busy. 17588dabb742SStefan Behrens */ 175921c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 1760361c093dSStefan Behrens 1761361c093dSStefan Behrens /* wait for the uuid_scan task to finish */ 1762361c093dSStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 1763361c093dSStefan Behrens /* avoid complains from lockdep et al. */ 1764361c093dSStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 1765361c093dSStefan Behrens 1766c146afadSYan Zheng sb->s_flags |= MS_RDONLY; 1767c146afadSYan Zheng 1768e44163e1SJeff Mahoney /* 1769e44163e1SJeff Mahoney * Setting MS_RDONLY will put the cleaner thread to 1770e44163e1SJeff Mahoney * sleep at the next loop if it's already active. 1771e44163e1SJeff Mahoney * If it's already asleep, we'll leave unused block 1772e44163e1SJeff Mahoney * groups on disk until we're mounted read-write again 1773e44163e1SJeff Mahoney * unless we clean them up here. 1774e44163e1SJeff Mahoney */ 1775e44163e1SJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1776e44163e1SJeff Mahoney 17778dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 17788dabb742SStefan Behrens btrfs_scrub_cancel(fs_info); 1779061594efSMiao Xie btrfs_pause_balance(fs_info); 17808dabb742SStefan Behrens 17816bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 178249b25e05SJeff Mahoney if (ret) 178349b25e05SJeff Mahoney goto restore; 1784c146afadSYan Zheng } else { 17850b246afaSJeff Mahoney if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 17866ef3de9cSDavid Sterba btrfs_err(fs_info, 1787efe120a0SFrank Holton "Remounting read-write after error is not allowed"); 17886ef3de9cSDavid Sterba ret = -EINVAL; 17896ef3de9cSDavid Sterba goto restore; 17906ef3de9cSDavid Sterba } 17918a3db184SSergei Trofimovich if (fs_info->fs_devices->rw_devices == 0) { 179249b25e05SJeff Mahoney ret = -EACCES; 179349b25e05SJeff Mahoney goto restore; 17948a3db184SSergei Trofimovich } 17952b82032cSYan Zheng 1796b382cfe8SQu Wenruo if (!btrfs_check_rw_degradable(fs_info)) { 1797efe120a0SFrank Holton btrfs_warn(fs_info, 1798efe120a0SFrank Holton "too many missing devices, writeable remount is not allowed"); 1799292fd7fcSStefan Behrens ret = -EACCES; 1800292fd7fcSStefan Behrens goto restore; 1801292fd7fcSStefan Behrens } 1802292fd7fcSStefan Behrens 18038a3db184SSergei Trofimovich if (btrfs_super_log_root(fs_info->super_copy) != 0) { 180449b25e05SJeff Mahoney ret = -EINVAL; 180549b25e05SJeff Mahoney goto restore; 18068a3db184SSergei Trofimovich } 1807c146afadSYan Zheng 1808815745cfSAl Viro ret = btrfs_cleanup_fs_roots(fs_info); 180949b25e05SJeff Mahoney if (ret) 181049b25e05SJeff Mahoney goto restore; 1811c146afadSYan Zheng 1812d68fc57bSYan, Zheng /* recover relocation */ 18135f316481SWang Shilong mutex_lock(&fs_info->cleaner_mutex); 1814d68fc57bSYan, Zheng ret = btrfs_recover_relocation(root); 18155f316481SWang Shilong mutex_unlock(&fs_info->cleaner_mutex); 181649b25e05SJeff Mahoney if (ret) 181749b25e05SJeff Mahoney goto restore; 1818c146afadSYan Zheng 18192b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 18202b6ba629SIlya Dryomov if (ret) 18212b6ba629SIlya Dryomov goto restore; 18222b6ba629SIlya Dryomov 18238dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 18248dabb742SStefan Behrens if (ret) { 1825efe120a0SFrank Holton btrfs_warn(fs_info, "failed to resume dev_replace"); 18268dabb742SStefan Behrens goto restore; 18278dabb742SStefan Behrens } 182894aebfb2SJosef Bacik 18296c6b5a39SAleksa Sarai btrfs_qgroup_rescan_resume(fs_info); 18306c6b5a39SAleksa Sarai 183194aebfb2SJosef Bacik if (!fs_info->uuid_root) { 1832efe120a0SFrank Holton btrfs_info(fs_info, "creating UUID tree"); 183394aebfb2SJosef Bacik ret = btrfs_create_uuid_tree(fs_info); 183494aebfb2SJosef Bacik if (ret) { 18355d163e0eSJeff Mahoney btrfs_warn(fs_info, 18365d163e0eSJeff Mahoney "failed to create the UUID tree %d", 18375d163e0eSJeff Mahoney ret); 183894aebfb2SJosef Bacik goto restore; 183994aebfb2SJosef Bacik } 184094aebfb2SJosef Bacik } 1841c146afadSYan Zheng sb->s_flags &= ~MS_RDONLY; 184290c711abSZygo Blaxell 1843afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 1844c146afadSYan Zheng } 1845dc81cdc5SMiao Xie out: 18462c6a92b0SJustin Maggard wake_up_process(fs_info->transaction_kthread); 1847dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 1848c146afadSYan Zheng return 0; 184949b25e05SJeff Mahoney 185049b25e05SJeff Mahoney restore: 185149b25e05SJeff Mahoney /* We've hit an error - don't reset MS_RDONLY */ 1852bc98a42cSDavid Howells if (sb_rdonly(sb)) 185349b25e05SJeff Mahoney old_flags |= MS_RDONLY; 185449b25e05SJeff Mahoney sb->s_flags = old_flags; 185549b25e05SJeff Mahoney fs_info->mount_opt = old_opts; 185649b25e05SJeff Mahoney fs_info->compress_type = old_compress_type; 185749b25e05SJeff Mahoney fs_info->max_inline = old_max_inline; 18580d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 18590d2450abSSergei Trofimovich old_thread_pool_size, fs_info->thread_pool_size); 186049b25e05SJeff Mahoney fs_info->metadata_ratio = old_metadata_ratio; 1861dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 186249b25e05SJeff Mahoney return ret; 1863c146afadSYan Zheng } 1864c146afadSYan Zheng 1865bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 1866bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1, 1867bcd53741SArne Jansen const void *dev_info2) 1868bcd53741SArne Jansen { 1869bcd53741SArne Jansen 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 if (((struct btrfs_device_info *)dev_info1)->max_avail < 1873bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1874bcd53741SArne Jansen return 1; 1875bcd53741SArne Jansen else 1876bcd53741SArne Jansen return 0; 1877bcd53741SArne Jansen } 1878bcd53741SArne Jansen 1879bcd53741SArne Jansen /* 1880bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 1881bcd53741SArne Jansen * is stored.(Descending Sort) 1882bcd53741SArne Jansen */ 1883bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 1884bcd53741SArne Jansen struct btrfs_device_info *devices, 1885bcd53741SArne Jansen size_t nr_devices) 1886bcd53741SArne Jansen { 1887bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 1888bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 1889bcd53741SArne Jansen } 1890bcd53741SArne Jansen 18916d07bcecSMiao Xie /* 18926d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 18936d07bcecSMiao Xie * file data. 18946d07bcecSMiao Xie */ 18956bccf3abSJeff Mahoney static int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info, 18966bccf3abSJeff Mahoney u64 *free_bytes) 18976d07bcecSMiao Xie { 18986d07bcecSMiao Xie struct btrfs_device_info *devices_info; 18996d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 19006d07bcecSMiao Xie struct btrfs_device *device; 19016d07bcecSMiao Xie u64 skip_space; 19026d07bcecSMiao Xie u64 type; 19036d07bcecSMiao Xie u64 avail_space; 19046d07bcecSMiao Xie u64 min_stripe_size; 190539fb26c3SMiao Xie int min_stripes = 1, num_stripes = 1; 19066d07bcecSMiao Xie int i = 0, nr_devices; 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), 19246a44517dSDavid Sterba GFP_KERNEL); 19256d07bcecSMiao Xie if (!devices_info) 19266d07bcecSMiao Xie return -ENOMEM; 19276d07bcecSMiao Xie 192801327610SNicholas D Steeves /* calc min stripe number for data space allocation */ 19291b86826dSJeff Mahoney type = btrfs_data_alloc_profile(fs_info); 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 rcu_read_lock(); 19477e33fd99SJosef Bacik list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 194863a212abSStefan Behrens if (!device->in_fs_metadata || !device->bdev || 194963a212abSStefan Behrens device->is_tgtdev_for_dev_replace) 19506d07bcecSMiao Xie continue; 19516d07bcecSMiao Xie 19527e33fd99SJosef Bacik if (i >= nr_devices) 19537e33fd99SJosef Bacik break; 19547e33fd99SJosef Bacik 19556d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 19566d07bcecSMiao Xie 19576d07bcecSMiao Xie /* align with stripe_len */ 1958f8c269d7SDavid Sterba avail_space = div_u64(avail_space, BTRFS_STRIPE_LEN); 19596d07bcecSMiao Xie avail_space *= BTRFS_STRIPE_LEN; 19606d07bcecSMiao Xie 19616d07bcecSMiao Xie /* 196201327610SNicholas D Steeves * In order to avoid overwriting the superblock on the drive, 19636d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 19646d07bcecSMiao Xie * allocation. 19656d07bcecSMiao Xie */ 1966ee22184bSByongho Lee skip_space = SZ_1M; 19676d07bcecSMiao Xie 19686d07bcecSMiao Xie /* 19696d07bcecSMiao Xie * we can use the free space in [0, skip_space - 1], subtract 19706d07bcecSMiao Xie * it from the total. 19716d07bcecSMiao Xie */ 19726d07bcecSMiao Xie if (avail_space && avail_space >= skip_space) 19736d07bcecSMiao Xie avail_space -= skip_space; 19746d07bcecSMiao Xie else 19756d07bcecSMiao Xie avail_space = 0; 19766d07bcecSMiao Xie 19776d07bcecSMiao Xie if (avail_space < min_stripe_size) 19786d07bcecSMiao Xie continue; 19796d07bcecSMiao Xie 19806d07bcecSMiao Xie devices_info[i].dev = device; 19816d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 19826d07bcecSMiao Xie 19836d07bcecSMiao Xie i++; 19846d07bcecSMiao Xie } 19857e33fd99SJosef Bacik rcu_read_unlock(); 19866d07bcecSMiao Xie 19876d07bcecSMiao Xie nr_devices = i; 19886d07bcecSMiao Xie 19896d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 19906d07bcecSMiao Xie 19916d07bcecSMiao Xie i = nr_devices - 1; 19926d07bcecSMiao Xie avail_space = 0; 19936d07bcecSMiao Xie while (nr_devices >= min_stripes) { 199439fb26c3SMiao Xie if (num_stripes > nr_devices) 199539fb26c3SMiao Xie num_stripes = nr_devices; 199639fb26c3SMiao Xie 19976d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 19986d07bcecSMiao Xie int j; 19996d07bcecSMiao Xie u64 alloc_size; 20006d07bcecSMiao Xie 200139fb26c3SMiao Xie avail_space += devices_info[i].max_avail * num_stripes; 20026d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 200339fb26c3SMiao Xie for (j = i + 1 - num_stripes; j <= i; j++) 20046d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 20056d07bcecSMiao Xie } 20066d07bcecSMiao Xie i--; 20076d07bcecSMiao Xie nr_devices--; 20086d07bcecSMiao Xie } 20096d07bcecSMiao Xie 20106d07bcecSMiao Xie kfree(devices_info); 20116d07bcecSMiao Xie *free_bytes = avail_space; 20126d07bcecSMiao Xie return 0; 20136d07bcecSMiao Xie } 20146d07bcecSMiao Xie 2015ba7b6e62SDavid Sterba /* 2016ba7b6e62SDavid Sterba * Calculate numbers for 'df', pessimistic in case of mixed raid profiles. 2017ba7b6e62SDavid Sterba * 2018ba7b6e62SDavid Sterba * If there's a redundant raid level at DATA block groups, use the respective 2019ba7b6e62SDavid Sterba * multiplier to scale the sizes. 2020ba7b6e62SDavid Sterba * 2021ba7b6e62SDavid Sterba * Unused device space usage is based on simulating the chunk allocator 20220d0c71b3SDavid Sterba * algorithm that respects the device sizes and order of allocations. This is 20230d0c71b3SDavid Sterba * a close approximation of the actual use but there are other factors that may 20240d0c71b3SDavid Sterba * change the result (like a new metadata chunk). 2025ba7b6e62SDavid Sterba * 2026ca8a51b3SDavid Sterba * If metadata is exhausted, f_bavail will be 0. 2027ba7b6e62SDavid Sterba */ 20288fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 20298fd17795SChris Mason { 2030815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 2031815745cfSAl Viro struct btrfs_super_block *disk_super = fs_info->super_copy; 2032815745cfSAl Viro struct list_head *head = &fs_info->space_info; 2033bd4d1088SJosef Bacik struct btrfs_space_info *found; 2034bd4d1088SJosef Bacik u64 total_used = 0; 20356d07bcecSMiao Xie u64 total_free_data = 0; 2036ca8a51b3SDavid Sterba u64 total_free_meta = 0; 2037db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 2038815745cfSAl Viro __be32 *fsid = (__be32 *)fs_info->fsid; 2039ba7b6e62SDavid Sterba unsigned factor = 1; 2040ba7b6e62SDavid Sterba struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 20416d07bcecSMiao Xie int ret; 2042ca8a51b3SDavid Sterba u64 thresh = 0; 2043ae02d1bdSLuis de Bethencourt int mixed = 0; 20448fd17795SChris Mason 2045bd4d1088SJosef Bacik rcu_read_lock(); 204689a55897SJosef Bacik list_for_each_entry_rcu(found, head, list) { 20476d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 2048ba7b6e62SDavid Sterba int i; 2049ba7b6e62SDavid Sterba 20506d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 20516d07bcecSMiao Xie total_free_data -= 20526d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 2053ba7b6e62SDavid Sterba 2054ba7b6e62SDavid Sterba for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { 2055ba7b6e62SDavid Sterba if (!list_empty(&found->block_groups[i])) { 2056ba7b6e62SDavid Sterba switch (i) { 2057ba7b6e62SDavid Sterba case BTRFS_RAID_DUP: 2058ba7b6e62SDavid Sterba case BTRFS_RAID_RAID1: 2059ba7b6e62SDavid Sterba case BTRFS_RAID_RAID10: 2060ba7b6e62SDavid Sterba factor = 2; 2061ba7b6e62SDavid Sterba } 2062ba7b6e62SDavid Sterba } 2063ba7b6e62SDavid Sterba } 20646d07bcecSMiao Xie } 2065ae02d1bdSLuis de Bethencourt 2066ae02d1bdSLuis de Bethencourt /* 2067ae02d1bdSLuis de Bethencourt * Metadata in mixed block goup profiles are accounted in data 2068ae02d1bdSLuis de Bethencourt */ 2069ae02d1bdSLuis de Bethencourt if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) { 2070ae02d1bdSLuis de Bethencourt if (found->flags & BTRFS_BLOCK_GROUP_DATA) 2071ae02d1bdSLuis de Bethencourt mixed = 1; 2072ae02d1bdSLuis de Bethencourt else 2073ae02d1bdSLuis de Bethencourt total_free_meta += found->disk_total - 2074ae02d1bdSLuis de Bethencourt found->disk_used; 2075ae02d1bdSLuis de Bethencourt } 20766d07bcecSMiao Xie 2077b742bb82SYan, Zheng total_used += found->disk_used; 207889a55897SJosef Bacik } 2079ba7b6e62SDavid Sterba 2080bd4d1088SJosef Bacik rcu_read_unlock(); 2081bd4d1088SJosef Bacik 2082ba7b6e62SDavid Sterba buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor); 2083ba7b6e62SDavid Sterba buf->f_blocks >>= bits; 2084ba7b6e62SDavid Sterba buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits); 2085ba7b6e62SDavid Sterba 2086ba7b6e62SDavid Sterba /* Account global block reserve as used, it's in logical size already */ 2087ba7b6e62SDavid Sterba spin_lock(&block_rsv->lock); 208841b34accSLuis de Bethencourt /* Mixed block groups accounting is not byte-accurate, avoid overflow */ 208941b34accSLuis de Bethencourt if (buf->f_bfree >= block_rsv->size >> bits) 2090ba7b6e62SDavid Sterba buf->f_bfree -= block_rsv->size >> bits; 209141b34accSLuis de Bethencourt else 209241b34accSLuis de Bethencourt buf->f_bfree = 0; 2093ba7b6e62SDavid Sterba spin_unlock(&block_rsv->lock); 2094ba7b6e62SDavid Sterba 20950d95c1beSDavid Sterba buf->f_bavail = div_u64(total_free_data, factor); 20966bccf3abSJeff Mahoney ret = btrfs_calc_avail_data_space(fs_info, &total_free_data); 20977e33fd99SJosef Bacik if (ret) 20986d07bcecSMiao Xie return ret; 2099ba7b6e62SDavid Sterba buf->f_bavail += div_u64(total_free_data, factor); 21006d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 2101d397712bSChris Mason 2102ca8a51b3SDavid Sterba /* 2103ca8a51b3SDavid Sterba * We calculate the remaining metadata space minus global reserve. If 2104ca8a51b3SDavid Sterba * this is (supposedly) smaller than zero, there's no space. But this 2105ca8a51b3SDavid Sterba * does not hold in practice, the exhausted state happens where's still 2106ca8a51b3SDavid Sterba * some positive delta. So we apply some guesswork and compare the 2107ca8a51b3SDavid Sterba * delta to a 4M threshold. (Practically observed delta was ~2M.) 2108ca8a51b3SDavid Sterba * 2109ca8a51b3SDavid Sterba * We probably cannot calculate the exact threshold value because this 2110ca8a51b3SDavid Sterba * depends on the internal reservations requested by various 2111ca8a51b3SDavid Sterba * operations, so some operations that consume a few metadata will 2112ca8a51b3SDavid Sterba * succeed even if the Avail is zero. But this is better than the other 2113ca8a51b3SDavid Sterba * way around. 2114ca8a51b3SDavid Sterba */ 2115ca8a51b3SDavid Sterba thresh = 4 * 1024 * 1024; 2116ca8a51b3SDavid Sterba 2117ae02d1bdSLuis de Bethencourt if (!mixed && total_free_meta - thresh < block_rsv->size) 2118ca8a51b3SDavid Sterba buf->f_bavail = 0; 2119ca8a51b3SDavid Sterba 2120ba7b6e62SDavid Sterba buf->f_type = BTRFS_SUPER_MAGIC; 2121ba7b6e62SDavid Sterba buf->f_bsize = dentry->d_sb->s_blocksize; 2122ba7b6e62SDavid Sterba buf->f_namelen = BTRFS_NAME_LEN; 2123ba7b6e62SDavid Sterba 21249d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 21259d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 21269d03632eSDavid Woodhouse on a big-endian or little-endian host */ 21279d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 21289d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 212932d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 21302b0143b5SDavid Howells buf->f_fsid.val[0] ^= BTRFS_I(d_inode(dentry))->root->objectid >> 32; 21312b0143b5SDavid Howells buf->f_fsid.val[1] ^= BTRFS_I(d_inode(dentry))->root->objectid; 213232d48fa1SDavid Woodhouse 21338fd17795SChris Mason return 0; 21348fd17795SChris Mason } 2135b5133862SChris Mason 2136aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb) 2137aea52e19SAl Viro { 2138815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2139aea52e19SAl Viro kill_anon_super(sb); 2140aea52e19SAl Viro free_fs_info(fs_info); 2141aea52e19SAl Viro } 2142aea52e19SAl Viro 21432e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 21442e635a27SChris Mason .owner = THIS_MODULE, 21452e635a27SChris Mason .name = "btrfs", 2146061dbc6bSAl Viro .mount = btrfs_mount, 2147aea52e19SAl Viro .kill_sb = btrfs_kill_super, 2148f667aef6SQu Wenruo .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA, 21492e635a27SChris Mason }; 21507f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs"); 2151a9218f6bSChris Mason 2152d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file) 2153d8620958STom Van Braeckel { 2154d8620958STom Van Braeckel /* 2155d8620958STom Van Braeckel * The control file's private_data is used to hold the 2156d8620958STom Van Braeckel * transaction when it is started and is used to keep 2157d8620958STom Van Braeckel * track of whether a transaction is already in progress. 2158d8620958STom Van Braeckel */ 2159d8620958STom Van Braeckel file->private_data = NULL; 2160d8620958STom Van Braeckel return 0; 2161d8620958STom Van Braeckel } 2162d8620958STom Van Braeckel 2163d352ac68SChris Mason /* 2164d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 2165d352ac68SChris Mason */ 21668a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 21678a4b83ccSChris Mason unsigned long arg) 21688a4b83ccSChris Mason { 21698a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 21708a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 2171c071fcfdSChris Mason int ret = -ENOTTY; 21728a4b83ccSChris Mason 2173e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2174e441d54dSChris Mason return -EPERM; 2175e441d54dSChris Mason 2176dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 2177dae7b665SLi Zefan if (IS_ERR(vol)) 2178dae7b665SLi Zefan return PTR_ERR(vol); 2179c071fcfdSChris Mason 21808a4b83ccSChris Mason switch (cmd) { 21818a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 218297288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 21838a4b83ccSChris Mason &btrfs_fs_type, &fs_devices); 21848a4b83ccSChris Mason break; 218502db0844SJosef Bacik case BTRFS_IOC_DEVICES_READY: 218602db0844SJosef Bacik ret = btrfs_scan_one_device(vol->name, FMODE_READ, 218702db0844SJosef Bacik &btrfs_fs_type, &fs_devices); 218802db0844SJosef Bacik if (ret) 218902db0844SJosef Bacik break; 219002db0844SJosef Bacik ret = !(fs_devices->num_devices == fs_devices->total_devices); 219102db0844SJosef Bacik break; 2192c5868f83SDavid Sterba case BTRFS_IOC_GET_SUPPORTED_FEATURES: 2193d5131b65SDavid Sterba ret = btrfs_ioctl_get_supported_features((void __user*)arg); 2194c5868f83SDavid Sterba break; 21958a4b83ccSChris Mason } 2196dae7b665SLi Zefan 21978a4b83ccSChris Mason kfree(vol); 2198f819d837SLinda Knippers return ret; 21998a4b83ccSChris Mason } 22008a4b83ccSChris Mason 22010176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 2202ed0dab6bSYan { 2203354aa0fbSMiao Xie struct btrfs_trans_handle *trans; 22040b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(sb); 22050b246afaSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 2206354aa0fbSMiao Xie 2207fac03c8dSDavid Sterba set_bit(BTRFS_FS_FROZEN, &fs_info->flags); 22089e7cc91aSWang Xiaoguang /* 22099e7cc91aSWang Xiaoguang * We don't need a barrier here, we'll wait for any transaction that 22109e7cc91aSWang Xiaoguang * could be in progress on other threads (and do delayed iputs that 22119e7cc91aSWang Xiaoguang * we want to avoid on a frozen filesystem), or do the commit 22129e7cc91aSWang Xiaoguang * ourselves. 22139e7cc91aSWang Xiaoguang */ 2214d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 2215354aa0fbSMiao Xie if (IS_ERR(trans)) { 2216354aa0fbSMiao Xie /* no transaction, don't bother */ 2217354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 22180176260fSLinus Torvalds return 0; 2219354aa0fbSMiao Xie return PTR_ERR(trans); 2220354aa0fbSMiao Xie } 22213a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 2222ed0dab6bSYan } 2223ed0dab6bSYan 22249e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb) 22259e7cc91aSWang Xiaoguang { 2226fac03c8dSDavid Sterba struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2227fac03c8dSDavid Sterba 2228fac03c8dSDavid Sterba clear_bit(BTRFS_FS_FROZEN, &fs_info->flags); 22299e7cc91aSWang Xiaoguang return 0; 22309e7cc91aSWang Xiaoguang } 22319e7cc91aSWang Xiaoguang 22329c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 22339c5085c1SJosef Bacik { 22349c5085c1SJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 22359c5085c1SJosef Bacik struct btrfs_fs_devices *cur_devices; 22369c5085c1SJosef Bacik struct btrfs_device *dev, *first_dev = NULL; 22379c5085c1SJosef Bacik struct list_head *head; 22389c5085c1SJosef Bacik struct rcu_string *name; 22399c5085c1SJosef Bacik 22409c5085c1SJosef Bacik mutex_lock(&fs_info->fs_devices->device_list_mutex); 22419c5085c1SJosef Bacik cur_devices = fs_info->fs_devices; 22429c5085c1SJosef Bacik while (cur_devices) { 22439c5085c1SJosef Bacik head = &cur_devices->devices; 22449c5085c1SJosef Bacik list_for_each_entry(dev, head, dev_list) { 2245aa9ddcd4SJosef Bacik if (dev->missing) 2246aa9ddcd4SJosef Bacik continue; 22470aeb8a6eSAnand Jain if (!dev->name) 22480aeb8a6eSAnand Jain continue; 22499c5085c1SJosef Bacik if (!first_dev || dev->devid < first_dev->devid) 22509c5085c1SJosef Bacik first_dev = dev; 22519c5085c1SJosef Bacik } 22529c5085c1SJosef Bacik cur_devices = cur_devices->seed; 22539c5085c1SJosef Bacik } 22549c5085c1SJosef Bacik 22559c5085c1SJosef Bacik if (first_dev) { 22569c5085c1SJosef Bacik rcu_read_lock(); 22579c5085c1SJosef Bacik name = rcu_dereference(first_dev->name); 22589c5085c1SJosef Bacik seq_escape(m, name->str, " \t\n\\"); 22599c5085c1SJosef Bacik rcu_read_unlock(); 22609c5085c1SJosef Bacik } else { 22619c5085c1SJosef Bacik WARN_ON(1); 22629c5085c1SJosef Bacik } 22639c5085c1SJosef Bacik mutex_unlock(&fs_info->fs_devices->device_list_mutex); 22649c5085c1SJosef Bacik return 0; 22659c5085c1SJosef Bacik } 22669c5085c1SJosef Bacik 2267b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 226876dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 2269bd555975SAl Viro .evict_inode = btrfs_evict_inode, 2270e20d96d6SChris Mason .put_super = btrfs_put_super, 2271d5719762SChris Mason .sync_fs = btrfs_sync_fs, 2272a9572a15SEric Paris .show_options = btrfs_show_options, 22739c5085c1SJosef Bacik .show_devname = btrfs_show_devname, 22744730a4bcSChris Mason .write_inode = btrfs_write_inode, 22752c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 22762c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 22778fd17795SChris Mason .statfs = btrfs_statfs, 2278c146afadSYan Zheng .remount_fs = btrfs_remount, 22790176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 22809e7cc91aSWang Xiaoguang .unfreeze_fs = btrfs_unfreeze, 2281e20d96d6SChris Mason }; 2282a9218f6bSChris Mason 2283a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 2284d8620958STom Van Braeckel .open = btrfs_control_open, 2285a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 2286a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 2287a9218f6bSChris Mason .owner = THIS_MODULE, 22886038f373SArnd Bergmann .llseek = noop_llseek, 2289a9218f6bSChris Mason }; 2290a9218f6bSChris Mason 2291a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 2292578454ffSKay Sievers .minor = BTRFS_MINOR, 2293a9218f6bSChris Mason .name = "btrfs-control", 2294a9218f6bSChris Mason .fops = &btrfs_ctl_fops 2295a9218f6bSChris Mason }; 2296a9218f6bSChris Mason 2297578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 2298578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 2299578454ffSKay Sievers 2300a9218f6bSChris Mason static int btrfs_interface_init(void) 2301a9218f6bSChris Mason { 2302a9218f6bSChris Mason return misc_register(&btrfs_misc); 2303a9218f6bSChris Mason } 2304a9218f6bSChris Mason 2305b2950863SChristoph Hellwig static void btrfs_interface_exit(void) 2306a9218f6bSChris Mason { 2307f368ed60SGreg Kroah-Hartman misc_deregister(&btrfs_misc); 2308a9218f6bSChris Mason } 2309a9218f6bSChris Mason 23108ae1af3cSAnand Jain static void btrfs_print_mod_info(void) 231185965600SDavid Sterba { 231262e85577SJeff Mahoney pr_info("Btrfs loaded, crc32c=%s" 231385965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 231485965600SDavid Sterba ", debug=on" 231585965600SDavid Sterba #endif 231679556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT 231779556c3dSStefan Behrens ", assert=on" 231879556c3dSStefan Behrens #endif 231985965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 232085965600SDavid Sterba ", integrity-checker=on" 232185965600SDavid Sterba #endif 23225f9e1059SJeff Mahoney "\n", 23235f9e1059SJeff Mahoney btrfs_crc32c_impl()); 232485965600SDavid Sterba } 232585965600SDavid Sterba 23262e635a27SChris Mason static int __init init_btrfs_fs(void) 23272e635a27SChris Mason { 23282c90e5d6SChris Mason int err; 232958176a96SJosef Bacik 233014a958e6SFilipe David Borba Manana err = btrfs_hash_init(); 233114a958e6SFilipe David Borba Manana if (err) 233214a958e6SFilipe David Borba Manana return err; 233314a958e6SFilipe David Borba Manana 233463541927SFilipe David Borba Manana btrfs_props_init(); 233563541927SFilipe David Borba Manana 233658176a96SJosef Bacik err = btrfs_init_sysfs(); 233758176a96SJosef Bacik if (err) 233814a958e6SFilipe David Borba Manana goto free_hash; 233958176a96SJosef Bacik 2340143bede5SJeff Mahoney btrfs_init_compress(); 2341d1310b2eSChris Mason 2342261507a0SLi Zefan err = btrfs_init_cachep(); 2343261507a0SLi Zefan if (err) 2344261507a0SLi Zefan goto free_compress; 2345261507a0SLi Zefan 2346d1310b2eSChris Mason err = extent_io_init(); 23472f4cbe64SWyatt Banks if (err) 23482f4cbe64SWyatt Banks goto free_cachep; 23492f4cbe64SWyatt Banks 2350d1310b2eSChris Mason err = extent_map_init(); 2351d1310b2eSChris Mason if (err) 2352d1310b2eSChris Mason goto free_extent_io; 2353d1310b2eSChris Mason 23546352b91dSMiao Xie err = ordered_data_init(); 23552f4cbe64SWyatt Banks if (err) 23562f4cbe64SWyatt Banks goto free_extent_map; 2357c8b97818SChris Mason 23586352b91dSMiao Xie err = btrfs_delayed_inode_init(); 23596352b91dSMiao Xie if (err) 23606352b91dSMiao Xie goto free_ordered_data; 23616352b91dSMiao Xie 23629247f317SMiao Xie err = btrfs_auto_defrag_init(); 236316cdcec7SMiao Xie if (err) 236416cdcec7SMiao Xie goto free_delayed_inode; 236516cdcec7SMiao Xie 236678a6184aSMiao Xie err = btrfs_delayed_ref_init(); 23679247f317SMiao Xie if (err) 23689247f317SMiao Xie goto free_auto_defrag; 23699247f317SMiao Xie 2370b9e9a6cbSWang Shilong err = btrfs_prelim_ref_init(); 2371b9e9a6cbSWang Shilong if (err) 2372af13b492SDavid Sterba goto free_delayed_ref; 2373b9e9a6cbSWang Shilong 237497eb6b69SDavid Sterba err = btrfs_end_io_wq_init(); 237578a6184aSMiao Xie if (err) 2376af13b492SDavid Sterba goto free_prelim_ref; 237778a6184aSMiao Xie 237897eb6b69SDavid Sterba err = btrfs_interface_init(); 237997eb6b69SDavid Sterba if (err) 238097eb6b69SDavid Sterba goto free_end_io_wq; 238197eb6b69SDavid Sterba 2382e565d4b9SJan Schmidt btrfs_init_lockdep(); 2383e565d4b9SJan Schmidt 23848ae1af3cSAnand Jain btrfs_print_mod_info(); 2385dc11dd5dSJosef Bacik 2386dc11dd5dSJosef Bacik err = btrfs_run_sanity_tests(); 2387dc11dd5dSJosef Bacik if (err) 2388dc11dd5dSJosef Bacik goto unregister_ioctl; 2389dc11dd5dSJosef Bacik 2390dc11dd5dSJosef Bacik err = register_filesystem(&btrfs_fs_type); 2391dc11dd5dSJosef Bacik if (err) 2392dc11dd5dSJosef Bacik goto unregister_ioctl; 239374255aa0SJosef Bacik 23942f4cbe64SWyatt Banks return 0; 23952f4cbe64SWyatt Banks 2396a9218f6bSChris Mason unregister_ioctl: 2397a9218f6bSChris Mason btrfs_interface_exit(); 239897eb6b69SDavid Sterba free_end_io_wq: 239997eb6b69SDavid Sterba btrfs_end_io_wq_exit(); 2400b9e9a6cbSWang Shilong free_prelim_ref: 2401b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 240278a6184aSMiao Xie free_delayed_ref: 240378a6184aSMiao Xie btrfs_delayed_ref_exit(); 24049247f317SMiao Xie free_auto_defrag: 24059247f317SMiao Xie btrfs_auto_defrag_exit(); 240616cdcec7SMiao Xie free_delayed_inode: 240716cdcec7SMiao Xie btrfs_delayed_inode_exit(); 24086352b91dSMiao Xie free_ordered_data: 24096352b91dSMiao Xie ordered_data_exit(); 24102f4cbe64SWyatt Banks free_extent_map: 24112f4cbe64SWyatt Banks extent_map_exit(); 2412d1310b2eSChris Mason free_extent_io: 2413d1310b2eSChris Mason extent_io_exit(); 24142f4cbe64SWyatt Banks free_cachep: 24152f4cbe64SWyatt Banks btrfs_destroy_cachep(); 2416261507a0SLi Zefan free_compress: 2417261507a0SLi Zefan btrfs_exit_compress(); 24182f4cbe64SWyatt Banks btrfs_exit_sysfs(); 241914a958e6SFilipe David Borba Manana free_hash: 242014a958e6SFilipe David Borba Manana btrfs_hash_exit(); 24212c90e5d6SChris Mason return err; 24222e635a27SChris Mason } 24232e635a27SChris Mason 24242e635a27SChris Mason static void __exit exit_btrfs_fs(void) 24252e635a27SChris Mason { 242639279cc3SChris Mason btrfs_destroy_cachep(); 242778a6184aSMiao Xie btrfs_delayed_ref_exit(); 24289247f317SMiao Xie btrfs_auto_defrag_exit(); 242916cdcec7SMiao Xie btrfs_delayed_inode_exit(); 2430b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 24316352b91dSMiao Xie ordered_data_exit(); 2432a52d9a80SChris Mason extent_map_exit(); 2433d1310b2eSChris Mason extent_io_exit(); 2434a9218f6bSChris Mason btrfs_interface_exit(); 24355ed5f588SJosef Bacik btrfs_end_io_wq_exit(); 24362e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 243758176a96SJosef Bacik btrfs_exit_sysfs(); 24388a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 2439261507a0SLi Zefan btrfs_exit_compress(); 244014a958e6SFilipe David Borba Manana btrfs_hash_exit(); 24412e635a27SChris Mason } 24422e635a27SChris Mason 244360efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs); 24442e635a27SChris Mason module_exit(exit_btrfs_fs) 24452e635a27SChris Mason 24462e635a27SChris Mason MODULE_LICENSE("GPL"); 2447