16cbd5570SChris Mason /* 26cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 36cbd5570SChris Mason * 46cbd5570SChris Mason * This program is free software; you can redistribute it and/or 56cbd5570SChris Mason * modify it under the terms of the GNU General Public 66cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 76cbd5570SChris Mason * 86cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 96cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 106cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 116cbd5570SChris Mason * General Public License for more details. 126cbd5570SChris Mason * 136cbd5570SChris Mason * You should have received a copy of the GNU General Public 146cbd5570SChris Mason * License along with this program; if not, write to the 156cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 166cbd5570SChris Mason * Boston, MA 021110-1307, USA. 176cbd5570SChris Mason */ 186cbd5570SChris Mason 194b82d6e4SYan #include <linux/blkdev.h> 202e635a27SChris Mason #include <linux/module.h> 21e20d96d6SChris Mason #include <linux/buffer_head.h> 222e635a27SChris Mason #include <linux/fs.h> 232e635a27SChris Mason #include <linux/pagemap.h> 242e635a27SChris Mason #include <linux/highmem.h> 252e635a27SChris Mason #include <linux/time.h> 262e635a27SChris Mason #include <linux/init.h> 27a9572a15SEric Paris #include <linux/seq_file.h> 282e635a27SChris Mason #include <linux/string.h> 292e635a27SChris Mason #include <linux/backing-dev.h> 304b82d6e4SYan #include <linux/mount.h> 31dee26a9fSChris Mason #include <linux/mpage.h> 3275dfe396SChris Mason #include <linux/swap.h> 3375dfe396SChris Mason #include <linux/writeback.h> 348fd17795SChris Mason #include <linux/statfs.h> 3508607c1bSChris Mason #include <linux/compat.h> 3695e05289SChris Mason #include <linux/parser.h> 37c59f8951SChris Mason #include <linux/ctype.h> 386da6abaeSChris Mason #include <linux/namei.h> 39a9218f6bSChris Mason #include <linux/miscdevice.h> 401bcbf313SQinghuang Feng #include <linux/magic.h> 415a0e3ad6STejun Heo #include <linux/slab.h> 4290a887c9SDan Magenheimer #include <linux/cleancache.h> 4322c44fe6SJosef Bacik #include <linux/ratelimit.h> 4455e301fdSFilipe Brandenburger #include <linux/btrfs.h> 4516cdcec7SMiao Xie #include "delayed-inode.h" 462e635a27SChris Mason #include "ctree.h" 47e20d96d6SChris Mason #include "disk-io.h" 48d5719762SChris Mason #include "transaction.h" 492c90e5d6SChris Mason #include "btrfs_inode.h" 503a686375SChris Mason #include "print-tree.h" 5114a958e6SFilipe David Borba Manana #include "hash.h" 5263541927SFilipe David Borba Manana #include "props.h" 535103e947SJosef Bacik #include "xattr.h" 548a4b83ccSChris Mason #include "volumes.h" 55be6e8dc0SBalaji Rao #include "export.h" 56c8b97818SChris Mason #include "compression.h" 579c5085c1SJosef Bacik #include "rcu-string.h" 588dabb742SStefan Behrens #include "dev-replace.h" 5974255aa0SJosef Bacik #include "free-space-cache.h" 60b9e9a6cbSWang Shilong #include "backref.h" 61dc11dd5dSJosef Bacik #include "tests/btrfs-tests.h" 622e635a27SChris Mason 63d3982100SMark Fasheh #include "qgroup.h" 641abe9b8aSliubo #define CREATE_TRACE_POINTS 651abe9b8aSliubo #include <trace/events/btrfs.h> 661abe9b8aSliubo 67b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops; 68830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type; 69e20d96d6SChris Mason 700723a047SHarald Hoyer static int btrfs_remount(struct super_block *sb, int *flags, char *data); 710723a047SHarald Hoyer 72e33e17eeSJeff Mahoney const char *btrfs_decode_error(int errno) 73acce952bSliubo { 7408748810SDavid Sterba char *errstr = "unknown"; 75acce952bSliubo 76acce952bSliubo switch (errno) { 77acce952bSliubo case -EIO: 78acce952bSliubo errstr = "IO failure"; 79acce952bSliubo break; 80acce952bSliubo case -ENOMEM: 81acce952bSliubo errstr = "Out of memory"; 82acce952bSliubo break; 83acce952bSliubo case -EROFS: 84acce952bSliubo errstr = "Readonly filesystem"; 85acce952bSliubo break; 868c342930SJeff Mahoney case -EEXIST: 878c342930SJeff Mahoney errstr = "Object already exists"; 888c342930SJeff Mahoney break; 8994ef7280SDavid Sterba case -ENOSPC: 9094ef7280SDavid Sterba errstr = "No space left"; 9194ef7280SDavid Sterba break; 9294ef7280SDavid Sterba case -ENOENT: 9394ef7280SDavid Sterba errstr = "No such entry"; 9494ef7280SDavid Sterba break; 95acce952bSliubo } 96acce952bSliubo 97acce952bSliubo return errstr; 98acce952bSliubo } 99acce952bSliubo 100acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */ 101acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info) 102acce952bSliubo { 103acce952bSliubo struct super_block *sb = fs_info->sb; 104acce952bSliubo 105acce952bSliubo if (sb->s_flags & MS_RDONLY) 106acce952bSliubo return; 107acce952bSliubo 10887533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 109acce952bSliubo sb->s_flags |= MS_RDONLY; 110c2cf52ebSSimon Kirby btrfs_info(fs_info, "forced readonly"); 1111acd6831SStefan Behrens /* 1121acd6831SStefan Behrens * Note that a running device replace operation is not 1131acd6831SStefan Behrens * canceled here although there is no way to update 1141acd6831SStefan Behrens * the progress. It would add the risk of a deadlock, 11501327610SNicholas D Steeves * therefore the canceling is omitted. The only penalty 1161acd6831SStefan Behrens * is that some I/O remains active until the procedure 1171acd6831SStefan Behrens * completes. The next time when the filesystem is 1181acd6831SStefan Behrens * mounted writeable again, the device replace 1191acd6831SStefan Behrens * operation continues. 1201acd6831SStefan Behrens */ 121acce952bSliubo } 122acce952bSliubo } 123acce952bSliubo 124acce952bSliubo /* 12534d97007SAnand Jain * __btrfs_handle_fs_error decodes expected errors from the caller and 126acce952bSliubo * invokes the approciate error response. 127acce952bSliubo */ 128c0d19e2bSDavid Sterba __cold 12934d97007SAnand Jain void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function, 1304da35113SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 131acce952bSliubo { 132acce952bSliubo struct super_block *sb = fs_info->sb; 13357d816a1SAnand Jain #ifdef CONFIG_PRINTK 134acce952bSliubo const char *errstr; 13557d816a1SAnand Jain #endif 136acce952bSliubo 137acce952bSliubo /* 138acce952bSliubo * Special case: if the error is EROFS, and we're already 139acce952bSliubo * under MS_RDONLY, then it is safe here. 140acce952bSliubo */ 141acce952bSliubo if (errno == -EROFS && (sb->s_flags & MS_RDONLY)) 142acce952bSliubo return; 143acce952bSliubo 14457d816a1SAnand Jain #ifdef CONFIG_PRINTK 14508748810SDavid Sterba errstr = btrfs_decode_error(errno); 1464da35113SJeff Mahoney if (fmt) { 14737252a66SEric Sandeen struct va_format vaf; 14837252a66SEric Sandeen va_list args; 14937252a66SEric Sandeen 15037252a66SEric Sandeen va_start(args, fmt); 15137252a66SEric Sandeen vaf.fmt = fmt; 15237252a66SEric Sandeen vaf.va = &args; 1534da35113SJeff Mahoney 15462e85577SJeff Mahoney pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n", 15508748810SDavid Sterba sb->s_id, function, line, errno, errstr, &vaf); 15637252a66SEric Sandeen va_end(args); 1574da35113SJeff Mahoney } else { 15862e85577SJeff Mahoney pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s\n", 15908748810SDavid Sterba sb->s_id, function, line, errno, errstr); 1604da35113SJeff Mahoney } 16157d816a1SAnand Jain #endif 162acce952bSliubo 1630713d90cSAnand Jain /* 1640713d90cSAnand Jain * Today we only save the error info to memory. Long term we'll 1650713d90cSAnand Jain * also send it down to the disk 1660713d90cSAnand Jain */ 1670713d90cSAnand Jain set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 1680713d90cSAnand Jain 1694da35113SJeff Mahoney /* Don't go through full error handling during mount */ 170cf79ffb5SJosef Bacik if (sb->s_flags & MS_BORN) 171acce952bSliubo btrfs_handle_error(fs_info); 172acce952bSliubo } 1734da35113SJeff Mahoney 17457d816a1SAnand Jain #ifdef CONFIG_PRINTK 175533574c6SJoe Perches static const char * const logtypes[] = { 1764da35113SJeff Mahoney "emergency", 1774da35113SJeff Mahoney "alert", 1784da35113SJeff Mahoney "critical", 1794da35113SJeff Mahoney "error", 1804da35113SJeff Mahoney "warning", 1814da35113SJeff Mahoney "notice", 1824da35113SJeff Mahoney "info", 1834da35113SJeff Mahoney "debug", 1844da35113SJeff Mahoney }; 1854da35113SJeff Mahoney 18635f4e5e6SNikolay Borisov 18735f4e5e6SNikolay Borisov /* 18835f4e5e6SNikolay Borisov * Use one ratelimit state per log level so that a flood of less important 18935f4e5e6SNikolay Borisov * messages doesn't cause more important ones to be dropped. 19035f4e5e6SNikolay Borisov */ 19135f4e5e6SNikolay Borisov static struct ratelimit_state printk_limits[] = { 19235f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100), 19335f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100), 19435f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100), 19535f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100), 19635f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100), 19735f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100), 19835f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100), 19935f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100), 20035f4e5e6SNikolay Borisov }; 20135f4e5e6SNikolay Borisov 202c2cf52ebSSimon Kirby void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...) 2034da35113SJeff Mahoney { 2044da35113SJeff Mahoney struct super_block *sb = fs_info->sb; 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)) 230c2cf52ebSSimon Kirby printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf); 231533574c6SJoe Perches 232533574c6SJoe Perches va_end(args); 2334da35113SJeff Mahoney } 234533574c6SJoe Perches #endif 235533574c6SJoe Perches 2368c342930SJeff Mahoney /* 23749b25e05SJeff Mahoney * We only mark the transaction aborted and then set the file system read-only. 23849b25e05SJeff Mahoney * This will prevent new transactions from starting or trying to join this 23949b25e05SJeff Mahoney * one. 24049b25e05SJeff Mahoney * 24149b25e05SJeff Mahoney * This means that error recovery at the call site is limited to freeing 24249b25e05SJeff Mahoney * any local memory allocations and passing the error code up without 24349b25e05SJeff Mahoney * further cleanup. The transaction should complete as it normally would 24449b25e05SJeff Mahoney * in the call path but will return -EIO. 24549b25e05SJeff Mahoney * 24649b25e05SJeff Mahoney * We'll complete the cleanup in btrfs_end_transaction and 24749b25e05SJeff Mahoney * btrfs_commit_transaction. 24849b25e05SJeff Mahoney */ 249c0d19e2bSDavid Sterba __cold 25049b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans, 25166642832SJeff Mahoney const char *function, 25249b25e05SJeff Mahoney unsigned int line, int errno) 25349b25e05SJeff Mahoney { 25466642832SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 25566642832SJeff Mahoney 25649b25e05SJeff Mahoney trans->aborted = errno; 25749b25e05SJeff Mahoney /* Nothing used. The other threads that have joined this 25849b25e05SJeff Mahoney * transaction may be able to continue. */ 25964c12921SJeff Mahoney if (!trans->dirty && list_empty(&trans->new_bgs)) { 26069ce977aSMiao Xie const char *errstr; 26169ce977aSMiao Xie 26208748810SDavid Sterba errstr = btrfs_decode_error(errno); 26366642832SJeff Mahoney btrfs_warn(fs_info, 264c2cf52ebSSimon Kirby "%s:%d: Aborting unused transaction(%s).", 26569ce977aSMiao Xie function, line, errstr); 26649b25e05SJeff Mahoney return; 26749b25e05SJeff Mahoney } 26820c7bcecSSeraphime Kirkovski WRITE_ONCE(trans->transaction->aborted, errno); 269501407aaSJosef Bacik /* Wake up anybody who may be waiting on this transaction */ 27066642832SJeff Mahoney wake_up(&fs_info->transaction_wait); 27166642832SJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 27266642832SJeff Mahoney __btrfs_handle_fs_error(fs_info, function, line, errno, NULL); 27349b25e05SJeff Mahoney } 27449b25e05SJeff Mahoney /* 2758c342930SJeff Mahoney * __btrfs_panic decodes unexpected, fatal errors from the caller, 2768c342930SJeff Mahoney * issues an alert, and either panics or BUGs, depending on mount options. 2778c342930SJeff Mahoney */ 278c0d19e2bSDavid Sterba __cold 2798c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, 2808c342930SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 2818c342930SJeff Mahoney { 2828c342930SJeff Mahoney char *s_id = "<unknown>"; 2838c342930SJeff Mahoney const char *errstr; 2848c342930SJeff Mahoney struct va_format vaf = { .fmt = fmt }; 2858c342930SJeff Mahoney va_list args; 2868c342930SJeff Mahoney 2878c342930SJeff Mahoney if (fs_info) 2888c342930SJeff Mahoney s_id = fs_info->sb->s_id; 2898c342930SJeff Mahoney 2908c342930SJeff Mahoney va_start(args, fmt); 2918c342930SJeff Mahoney vaf.va = &args; 2928c342930SJeff Mahoney 29308748810SDavid Sterba errstr = btrfs_decode_error(errno); 294aa43a17cSEric Sandeen if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR)) 29508748810SDavid Sterba panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n", 29608748810SDavid Sterba s_id, function, line, &vaf, errno, errstr); 2978c342930SJeff Mahoney 298efe120a0SFrank Holton btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)", 299efe120a0SFrank Holton function, line, &vaf, errno, errstr); 3008c342930SJeff Mahoney va_end(args); 3018c342930SJeff Mahoney /* Caller calls BUG() */ 3028c342930SJeff Mahoney } 303acce952bSliubo 304e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 305e20d96d6SChris Mason { 3066bccf3abSJeff Mahoney close_ctree(btrfs_sb(sb)); 307e20d96d6SChris Mason } 3082e635a27SChris Mason 30995e05289SChris Mason enum { 31073f73415SJosef Bacik Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum, 311287a0ab9SJosef Bacik Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, 312287a0ab9SJosef Bacik Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress, 313261507a0SLi Zefan Opt_compress_type, Opt_compress_force, Opt_compress_force_type, 314261507a0SLi Zefan Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard, 31570f6d82eSOmar Sandoval Opt_space_cache, Opt_space_cache_version, Opt_clear_cache, 31670f6d82eSOmar Sandoval Opt_user_subvol_rm_allowed, Opt_enospc_debug, Opt_subvolrootid, 31770f6d82eSOmar Sandoval Opt_defrag, Opt_inode_cache, Opt_no_space_cache, Opt_recovery, 31870f6d82eSOmar Sandoval Opt_skip_balance, Opt_check_integrity, 31970f6d82eSOmar Sandoval Opt_check_integrity_including_extent_data, 320f420ee1eSStefan Behrens Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree, 321e07a2adeSQu Wenruo Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard, 322a258af7aSQu Wenruo Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow, 3238dcddfa0SQu Wenruo Opt_datasum, Opt_treelog, Opt_noinode_cache, Opt_usebackuproot, 324fed8f166SQu Wenruo Opt_nologreplay, Opt_norecovery, 325d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 326d0bd4560SJosef Bacik Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all, 327d0bd4560SJosef Bacik #endif 3289555c6c1SIlya Dryomov Opt_err, 32995e05289SChris Mason }; 33095e05289SChris Mason 3314d4ab6d6SDavid Sterba static const match_table_t tokens = { 332dfe25020SChris Mason {Opt_degraded, "degraded"}, 33395e05289SChris Mason {Opt_subvol, "subvol=%s"}, 3341493381fSWang Shilong {Opt_subvolid, "subvolid=%s"}, 33543e570b0SChristoph Hellwig {Opt_device, "device=%s"}, 336b6cda9bcSChris Mason {Opt_nodatasum, "nodatasum"}, 337d399167dSQu Wenruo {Opt_datasum, "datasum"}, 338be20aa9dSChris Mason {Opt_nodatacow, "nodatacow"}, 339a258af7aSQu Wenruo {Opt_datacow, "datacow"}, 34021ad10cfSChris Mason {Opt_nobarrier, "nobarrier"}, 341842bef58SQu Wenruo {Opt_barrier, "barrier"}, 3426f568d35SChris Mason {Opt_max_inline, "max_inline=%s"}, 3438f662a76SChris Mason {Opt_alloc_start, "alloc_start=%s"}, 3444543df7eSChris Mason {Opt_thread_pool, "thread_pool=%d"}, 345c8b97818SChris Mason {Opt_compress, "compress"}, 346261507a0SLi Zefan {Opt_compress_type, "compress=%s"}, 347a555f810SChris Mason {Opt_compress_force, "compress-force"}, 348261507a0SLi Zefan {Opt_compress_force_type, "compress-force=%s"}, 349e18e4809SChris Mason {Opt_ssd, "ssd"}, 350451d7585SChris Mason {Opt_ssd_spread, "ssd_spread"}, 3513b30c22fSChris Mason {Opt_nossd, "nossd"}, 352bd0330adSQu Wenruo {Opt_acl, "acl"}, 35333268eafSJosef Bacik {Opt_noacl, "noacl"}, 3543a5e1404SSage Weil {Opt_notreelog, "notreelog"}, 355a88998f2SQu Wenruo {Opt_treelog, "treelog"}, 35696da0919SQu Wenruo {Opt_nologreplay, "nologreplay"}, 357fed8f166SQu Wenruo {Opt_norecovery, "norecovery"}, 358dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 3592c9ee856SQu Wenruo {Opt_noflushoncommit, "noflushoncommit"}, 36097e728d4SJosef Bacik {Opt_ratio, "metadata_ratio=%d"}, 361e244a0aeSChristoph Hellwig {Opt_discard, "discard"}, 362e07a2adeSQu Wenruo {Opt_nodiscard, "nodiscard"}, 3630af3d00bSJosef Bacik {Opt_space_cache, "space_cache"}, 36470f6d82eSOmar Sandoval {Opt_space_cache_version, "space_cache=%s"}, 36588c2ba3bSJosef Bacik {Opt_clear_cache, "clear_cache"}, 3664260f7c7SSage Weil {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, 36791435650SChris Mason {Opt_enospc_debug, "enospc_debug"}, 36853036293SQu Wenruo {Opt_noenospc_debug, "noenospc_debug"}, 369e15d0542SXin Zhong {Opt_subvolrootid, "subvolrootid=%d"}, 3704cb5300bSChris Mason {Opt_defrag, "autodefrag"}, 371fc0ca9afSQu Wenruo {Opt_nodefrag, "noautodefrag"}, 3724b9465cbSChris Mason {Opt_inode_cache, "inode_cache"}, 3733818aea2SQu Wenruo {Opt_noinode_cache, "noinode_cache"}, 3748965593eSDavid Sterba {Opt_no_space_cache, "nospace_cache"}, 3758dcddfa0SQu Wenruo {Opt_recovery, "recovery"}, /* deprecated */ 3768dcddfa0SQu Wenruo {Opt_usebackuproot, "usebackuproot"}, 3779555c6c1SIlya Dryomov {Opt_skip_balance, "skip_balance"}, 37821adbd5cSStefan Behrens {Opt_check_integrity, "check_int"}, 37921adbd5cSStefan Behrens {Opt_check_integrity_including_extent_data, "check_int_data"}, 38021adbd5cSStefan Behrens {Opt_check_integrity_print_mask, "check_int_print_mask=%d"}, 381f420ee1eSStefan Behrens {Opt_rescan_uuid_tree, "rescan_uuid_tree"}, 3828c342930SJeff Mahoney {Opt_fatal_errors, "fatal_errors=%s"}, 3838b87dc17SDavid Sterba {Opt_commit_interval, "commit=%d"}, 384d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 385d0bd4560SJosef Bacik {Opt_fragment_data, "fragment=data"}, 386d0bd4560SJosef Bacik {Opt_fragment_metadata, "fragment=metadata"}, 387d0bd4560SJosef Bacik {Opt_fragment_all, "fragment=all"}, 388d0bd4560SJosef Bacik #endif 38933268eafSJosef Bacik {Opt_err, NULL}, 39095e05289SChris Mason }; 39195e05289SChris Mason 392edf24abeSChristoph Hellwig /* 393edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 394edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 39549b25e05SJeff Mahoney * XXX JDM: This needs to be cleaned up for remount. 396edf24abeSChristoph Hellwig */ 3972ff7e61eSJeff Mahoney int btrfs_parse_options(struct btrfs_fs_info *info, char *options, 39896da0919SQu Wenruo unsigned long new_flags) 39995e05289SChris Mason { 40095e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 40173bc1876SJosef Bacik char *p, *num, *orig = NULL; 40273bc1876SJosef Bacik u64 cache_gen; 4034543df7eSChris Mason int intarg; 404a7a3f7caSSage Weil int ret = 0; 405261507a0SLi Zefan char *compress_type; 406261507a0SLi Zefan bool compress_force = false; 407b7c47bbbSTsutomu Itoh enum btrfs_compression_type saved_compress_type; 408b7c47bbbSTsutomu Itoh bool saved_compress_force; 409b7c47bbbSTsutomu Itoh int no_compress = 0; 410b6cda9bcSChris Mason 4110b246afaSJeff Mahoney cache_gen = btrfs_super_cache_generation(info->super_copy); 4120b246afaSJeff Mahoney if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) 41370f6d82eSOmar Sandoval btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE); 41470f6d82eSOmar Sandoval else if (cache_gen) 41573bc1876SJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 41673bc1876SJosef Bacik 41796da0919SQu Wenruo /* 41896da0919SQu Wenruo * Even the options are empty, we still need to do extra check 41996da0919SQu Wenruo * against new flags 42096da0919SQu Wenruo */ 42195e05289SChris Mason if (!options) 42296da0919SQu Wenruo goto check; 42395e05289SChris Mason 424be20aa9dSChris Mason /* 425be20aa9dSChris Mason * strsep changes the string, duplicate it because parse_options 426be20aa9dSChris Mason * gets called twice 427be20aa9dSChris Mason */ 428be20aa9dSChris Mason options = kstrdup(options, GFP_NOFS); 429be20aa9dSChris Mason if (!options) 430be20aa9dSChris Mason return -ENOMEM; 431be20aa9dSChris Mason 432da495eccSJosef Bacik orig = options; 433be20aa9dSChris Mason 43495e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 43595e05289SChris Mason int token; 43695e05289SChris Mason if (!*p) 43795e05289SChris Mason continue; 43895e05289SChris Mason 43995e05289SChris Mason token = match_token(p, tokens, args); 44095e05289SChris Mason switch (token) { 441dfe25020SChris Mason case Opt_degraded: 4420b246afaSJeff Mahoney btrfs_info(info, "allowing degraded mounts"); 443dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 444dfe25020SChris Mason break; 44595e05289SChris Mason case Opt_subvol: 44673f73415SJosef Bacik case Opt_subvolid: 447e15d0542SXin Zhong case Opt_subvolrootid: 44843e570b0SChristoph Hellwig case Opt_device: 449edf24abeSChristoph Hellwig /* 45043e570b0SChristoph Hellwig * These are parsed by btrfs_parse_early_options 451edf24abeSChristoph Hellwig * and can be happily ignored here. 452edf24abeSChristoph Hellwig */ 45395e05289SChris Mason break; 454b6cda9bcSChris Mason case Opt_nodatasum: 4553cdde224SJeff Mahoney btrfs_set_and_info(info, NODATASUM, 45607802534SQu Wenruo "setting nodatasum"); 457be20aa9dSChris Mason break; 458d399167dSQu Wenruo case Opt_datasum: 4593cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATASUM)) { 4603cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATACOW)) 4610b246afaSJeff Mahoney btrfs_info(info, 4625d163e0eSJeff Mahoney "setting datasum, datacow enabled"); 463d399167dSQu Wenruo else 4640b246afaSJeff Mahoney btrfs_info(info, "setting datasum"); 46507802534SQu Wenruo } 466d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATACOW); 467d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATASUM); 468d399167dSQu Wenruo break; 469be20aa9dSChris Mason case Opt_nodatacow: 4703cdde224SJeff Mahoney if (!btrfs_test_opt(info, NODATACOW)) { 4713cdde224SJeff Mahoney if (!btrfs_test_opt(info, COMPRESS) || 4723cdde224SJeff Mahoney !btrfs_test_opt(info, FORCE_COMPRESS)) { 4730b246afaSJeff Mahoney btrfs_info(info, 474efe120a0SFrank Holton "setting nodatacow, compression disabled"); 475bedb2ccaSAndrei Popa } else { 4760b246afaSJeff Mahoney btrfs_info(info, "setting nodatacow"); 477bedb2ccaSAndrei Popa } 47807802534SQu Wenruo } 479bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, COMPRESS); 480bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 481be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 482be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 483b6cda9bcSChris Mason break; 484a258af7aSQu Wenruo case Opt_datacow: 4853cdde224SJeff Mahoney btrfs_clear_and_info(info, NODATACOW, 48607802534SQu Wenruo "setting datacow"); 487a258af7aSQu Wenruo break; 488a555f810SChris Mason case Opt_compress_force: 489261507a0SLi Zefan case Opt_compress_force_type: 490261507a0SLi Zefan compress_force = true; 4911c697d4aSEric Sandeen /* Fallthrough */ 492261507a0SLi Zefan case Opt_compress: 493261507a0SLi Zefan case Opt_compress_type: 4943cdde224SJeff Mahoney saved_compress_type = btrfs_test_opt(info, 4953cdde224SJeff Mahoney COMPRESS) ? 496b7c47bbbSTsutomu Itoh info->compress_type : BTRFS_COMPRESS_NONE; 497b7c47bbbSTsutomu Itoh saved_compress_force = 4983cdde224SJeff Mahoney btrfs_test_opt(info, FORCE_COMPRESS); 499261507a0SLi Zefan if (token == Opt_compress || 500261507a0SLi Zefan token == Opt_compress_force || 501261507a0SLi Zefan strcmp(args[0].from, "zlib") == 0) { 502261507a0SLi Zefan compress_type = "zlib"; 503261507a0SLi Zefan info->compress_type = BTRFS_COMPRESS_ZLIB; 504063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 505bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 506bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 507b7c47bbbSTsutomu Itoh no_compress = 0; 508a6fa6faeSLi Zefan } else if (strcmp(args[0].from, "lzo") == 0) { 509a6fa6faeSLi Zefan compress_type = "lzo"; 510a6fa6faeSLi Zefan info->compress_type = BTRFS_COMPRESS_LZO; 511063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 512bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 513bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 5142b0ce2c2SMitch Harder btrfs_set_fs_incompat(info, COMPRESS_LZO); 515b7c47bbbSTsutomu Itoh no_compress = 0; 516063849eaSArnd Hannemann } else if (strncmp(args[0].from, "no", 2) == 0) { 517063849eaSArnd Hannemann compress_type = "no"; 518063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, COMPRESS); 519063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 520063849eaSArnd Hannemann compress_force = false; 521b7c47bbbSTsutomu Itoh no_compress++; 522261507a0SLi Zefan } else { 523261507a0SLi Zefan ret = -EINVAL; 524261507a0SLi Zefan goto out; 525261507a0SLi Zefan } 526261507a0SLi Zefan 527261507a0SLi Zefan if (compress_force) { 528b7c47bbbSTsutomu Itoh btrfs_set_opt(info->mount_opt, FORCE_COMPRESS); 529143f3636SDavid Sterba } else { 5304027e0f4SWang Shilong /* 5314027e0f4SWang Shilong * If we remount from compress-force=xxx to 5324027e0f4SWang Shilong * compress=xxx, we need clear FORCE_COMPRESS 5334027e0f4SWang Shilong * flag, otherwise, there is no way for users 5344027e0f4SWang Shilong * to disable forcible compression separately. 5354027e0f4SWang Shilong */ 5364027e0f4SWang Shilong btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 537a7e252afSMiao Xie } 5383cdde224SJeff Mahoney if ((btrfs_test_opt(info, COMPRESS) && 539b7c47bbbSTsutomu Itoh (info->compress_type != saved_compress_type || 540b7c47bbbSTsutomu Itoh compress_force != saved_compress_force)) || 5413cdde224SJeff Mahoney (!btrfs_test_opt(info, COMPRESS) && 542b7c47bbbSTsutomu Itoh no_compress == 1)) { 5430b246afaSJeff Mahoney btrfs_info(info, "%s %s compression", 544b7c47bbbSTsutomu Itoh (compress_force) ? "force" : "use", 545b7c47bbbSTsutomu Itoh compress_type); 546b7c47bbbSTsutomu Itoh } 547b7c47bbbSTsutomu Itoh compress_force = false; 548a555f810SChris Mason break; 549e18e4809SChris Mason case Opt_ssd: 5503cdde224SJeff Mahoney btrfs_set_and_info(info, SSD, 55107802534SQu Wenruo "use ssd allocation scheme"); 552e18e4809SChris Mason break; 553451d7585SChris Mason case Opt_ssd_spread: 5543cdde224SJeff Mahoney btrfs_set_and_info(info, SSD_SPREAD, 55507802534SQu Wenruo "use spread ssd allocation scheme"); 5562aa06a35SEric Sandeen btrfs_set_opt(info->mount_opt, SSD); 557451d7585SChris Mason break; 5583b30c22fSChris Mason case Opt_nossd: 5593cdde224SJeff Mahoney btrfs_set_and_info(info, NOSSD, 56007802534SQu Wenruo "not using ssd allocation scheme"); 5613b30c22fSChris Mason btrfs_clear_opt(info->mount_opt, SSD); 5623b30c22fSChris Mason break; 563842bef58SQu Wenruo case Opt_barrier: 5643cdde224SJeff Mahoney btrfs_clear_and_info(info, NOBARRIER, 56507802534SQu Wenruo "turning on barriers"); 566842bef58SQu Wenruo break; 56721ad10cfSChris Mason case Opt_nobarrier: 5683cdde224SJeff Mahoney btrfs_set_and_info(info, NOBARRIER, 56907802534SQu Wenruo "turning off barriers"); 57021ad10cfSChris Mason break; 5714543df7eSChris Mason case Opt_thread_pool: 5722c334e87SWang Shilong ret = match_int(&args[0], &intarg); 5732c334e87SWang Shilong if (ret) { 5742c334e87SWang Shilong goto out; 5752c334e87SWang Shilong } else if (intarg > 0) { 5764543df7eSChris Mason info->thread_pool_size = intarg; 5772c334e87SWang Shilong } else { 5782c334e87SWang Shilong ret = -EINVAL; 5792c334e87SWang Shilong goto out; 5802c334e87SWang Shilong } 5814543df7eSChris Mason break; 5826f568d35SChris Mason case Opt_max_inline: 583edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5846f568d35SChris Mason if (num) { 58591748467SAkinobu Mita info->max_inline = memparse(num, NULL); 5866f568d35SChris Mason kfree(num); 5876f568d35SChris Mason 58815ada040SChris Mason if (info->max_inline) { 589feb5f965SMitch Harder info->max_inline = min_t(u64, 59015ada040SChris Mason info->max_inline, 5910b246afaSJeff Mahoney info->sectorsize); 59215ada040SChris Mason } 5930b246afaSJeff Mahoney btrfs_info(info, "max_inline at %llu", 594c1c9ff7cSGeert Uytterhoeven info->max_inline); 5952c334e87SWang Shilong } else { 5962c334e87SWang Shilong ret = -ENOMEM; 5972c334e87SWang Shilong goto out; 5986f568d35SChris Mason } 5996f568d35SChris Mason break; 6008f662a76SChris Mason case Opt_alloc_start: 601edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 6028f662a76SChris Mason if (num) { 603c018daecSMiao Xie mutex_lock(&info->chunk_mutex); 60491748467SAkinobu Mita info->alloc_start = memparse(num, NULL); 605c018daecSMiao Xie mutex_unlock(&info->chunk_mutex); 6068f662a76SChris Mason kfree(num); 6070b246afaSJeff Mahoney btrfs_info(info, "allocations start at %llu", 608c1c9ff7cSGeert Uytterhoeven info->alloc_start); 6092c334e87SWang Shilong } else { 6102c334e87SWang Shilong ret = -ENOMEM; 6112c334e87SWang Shilong goto out; 6128f662a76SChris Mason } 6138f662a76SChris Mason break; 614bd0330adSQu Wenruo case Opt_acl: 61545ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL 6160b246afaSJeff Mahoney info->sb->s_flags |= MS_POSIXACL; 617bd0330adSQu Wenruo break; 61845ff35d6SGuangliang Zhao #else 6190b246afaSJeff Mahoney btrfs_err(info, "support for ACL not compiled in!"); 62045ff35d6SGuangliang Zhao ret = -EINVAL; 62145ff35d6SGuangliang Zhao goto out; 62245ff35d6SGuangliang Zhao #endif 62333268eafSJosef Bacik case Opt_noacl: 6240b246afaSJeff Mahoney info->sb->s_flags &= ~MS_POSIXACL; 62533268eafSJosef Bacik break; 6263a5e1404SSage Weil case Opt_notreelog: 6273cdde224SJeff Mahoney btrfs_set_and_info(info, NOTREELOG, 62807802534SQu Wenruo "disabling tree log"); 6293a5e1404SSage Weil break; 630a88998f2SQu Wenruo case Opt_treelog: 6313cdde224SJeff Mahoney btrfs_clear_and_info(info, NOTREELOG, 63207802534SQu Wenruo "enabling tree log"); 633a88998f2SQu Wenruo break; 634fed8f166SQu Wenruo case Opt_norecovery: 63596da0919SQu Wenruo case Opt_nologreplay: 6363cdde224SJeff Mahoney btrfs_set_and_info(info, NOLOGREPLAY, 63796da0919SQu Wenruo "disabling log replay at mount time"); 63896da0919SQu Wenruo break; 639dccae999SSage Weil case Opt_flushoncommit: 6403cdde224SJeff Mahoney btrfs_set_and_info(info, FLUSHONCOMMIT, 64107802534SQu Wenruo "turning on flush-on-commit"); 642dccae999SSage Weil break; 6432c9ee856SQu Wenruo case Opt_noflushoncommit: 6443cdde224SJeff Mahoney btrfs_clear_and_info(info, FLUSHONCOMMIT, 64507802534SQu Wenruo "turning off flush-on-commit"); 6462c9ee856SQu Wenruo break; 64797e728d4SJosef Bacik case Opt_ratio: 6482c334e87SWang Shilong ret = match_int(&args[0], &intarg); 6492c334e87SWang Shilong if (ret) { 6502c334e87SWang Shilong goto out; 6512c334e87SWang Shilong } else if (intarg >= 0) { 65297e728d4SJosef Bacik info->metadata_ratio = intarg; 6530b246afaSJeff Mahoney btrfs_info(info, "metadata ratio %d", 65497e728d4SJosef Bacik info->metadata_ratio); 6552c334e87SWang Shilong } else { 6562c334e87SWang Shilong ret = -EINVAL; 6572c334e87SWang Shilong goto out; 65897e728d4SJosef Bacik } 65997e728d4SJosef Bacik break; 660e244a0aeSChristoph Hellwig case Opt_discard: 6613cdde224SJeff Mahoney btrfs_set_and_info(info, DISCARD, 66207802534SQu Wenruo "turning on discard"); 663e244a0aeSChristoph Hellwig break; 664e07a2adeSQu Wenruo case Opt_nodiscard: 6653cdde224SJeff Mahoney btrfs_clear_and_info(info, DISCARD, 66607802534SQu Wenruo "turning off discard"); 667e07a2adeSQu Wenruo break; 6680af3d00bSJosef Bacik case Opt_space_cache: 66970f6d82eSOmar Sandoval case Opt_space_cache_version: 67070f6d82eSOmar Sandoval if (token == Opt_space_cache || 67170f6d82eSOmar Sandoval strcmp(args[0].from, "v1") == 0) { 6720b246afaSJeff Mahoney btrfs_clear_opt(info->mount_opt, 67370f6d82eSOmar Sandoval FREE_SPACE_TREE); 6743cdde224SJeff Mahoney btrfs_set_and_info(info, SPACE_CACHE, 67507802534SQu Wenruo "enabling disk space caching"); 67670f6d82eSOmar Sandoval } else if (strcmp(args[0].from, "v2") == 0) { 6770b246afaSJeff Mahoney btrfs_clear_opt(info->mount_opt, 67870f6d82eSOmar Sandoval SPACE_CACHE); 6790b246afaSJeff Mahoney btrfs_set_and_info(info, FREE_SPACE_TREE, 68070f6d82eSOmar Sandoval "enabling free space tree"); 68170f6d82eSOmar Sandoval } else { 68270f6d82eSOmar Sandoval ret = -EINVAL; 68370f6d82eSOmar Sandoval goto out; 68470f6d82eSOmar Sandoval } 6850de90876SJosef Bacik break; 686f420ee1eSStefan Behrens case Opt_rescan_uuid_tree: 687f420ee1eSStefan Behrens btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE); 688f420ee1eSStefan Behrens break; 68973bc1876SJosef Bacik case Opt_no_space_cache: 6903cdde224SJeff Mahoney if (btrfs_test_opt(info, SPACE_CACHE)) { 6910b246afaSJeff Mahoney btrfs_clear_and_info(info, SPACE_CACHE, 69207802534SQu Wenruo "disabling disk space caching"); 69370f6d82eSOmar Sandoval } 6943cdde224SJeff Mahoney if (btrfs_test_opt(info, FREE_SPACE_TREE)) { 6950b246afaSJeff Mahoney btrfs_clear_and_info(info, FREE_SPACE_TREE, 69670f6d82eSOmar Sandoval "disabling free space tree"); 69770f6d82eSOmar Sandoval } 69873bc1876SJosef Bacik break; 6994b9465cbSChris Mason case Opt_inode_cache: 7007e1876acSDavid Sterba btrfs_set_pending_and_info(info, INODE_MAP_CACHE, 70107802534SQu Wenruo "enabling inode map caching"); 7023818aea2SQu Wenruo break; 7033818aea2SQu Wenruo case Opt_noinode_cache: 7047e1876acSDavid Sterba btrfs_clear_pending_and_info(info, INODE_MAP_CACHE, 70507802534SQu Wenruo "disabling inode map caching"); 7064b9465cbSChris Mason break; 70788c2ba3bSJosef Bacik case Opt_clear_cache: 7083cdde224SJeff Mahoney btrfs_set_and_info(info, CLEAR_CACHE, 70907802534SQu Wenruo "force clearing of disk cache"); 7100af3d00bSJosef Bacik break; 7114260f7c7SSage Weil case Opt_user_subvol_rm_allowed: 7124260f7c7SSage Weil btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 7134260f7c7SSage Weil break; 71491435650SChris Mason case Opt_enospc_debug: 71591435650SChris Mason btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 71691435650SChris Mason break; 71753036293SQu Wenruo case Opt_noenospc_debug: 71853036293SQu Wenruo btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG); 71953036293SQu Wenruo break; 7204cb5300bSChris Mason case Opt_defrag: 7213cdde224SJeff Mahoney btrfs_set_and_info(info, AUTO_DEFRAG, 72207802534SQu Wenruo "enabling auto defrag"); 7234cb5300bSChris Mason break; 724fc0ca9afSQu Wenruo case Opt_nodefrag: 7253cdde224SJeff Mahoney btrfs_clear_and_info(info, AUTO_DEFRAG, 72607802534SQu Wenruo "disabling auto defrag"); 727fc0ca9afSQu Wenruo break; 728af31f5e5SChris Mason case Opt_recovery: 7290b246afaSJeff Mahoney btrfs_warn(info, 7308dcddfa0SQu Wenruo "'recovery' is deprecated, use 'usebackuproot' instead"); 7318dcddfa0SQu Wenruo case Opt_usebackuproot: 7320b246afaSJeff Mahoney btrfs_info(info, 7338dcddfa0SQu Wenruo "trying to use backup root at mount time"); 7348dcddfa0SQu Wenruo btrfs_set_opt(info->mount_opt, USEBACKUPROOT); 735af31f5e5SChris Mason break; 7369555c6c1SIlya Dryomov case Opt_skip_balance: 7379555c6c1SIlya Dryomov btrfs_set_opt(info->mount_opt, SKIP_BALANCE); 7389555c6c1SIlya Dryomov break; 73921adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 74021adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 7410b246afaSJeff Mahoney btrfs_info(info, 742efe120a0SFrank Holton "enabling check integrity including extent data"); 74321adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, 74421adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA); 74521adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 74621adbd5cSStefan Behrens break; 74721adbd5cSStefan Behrens case Opt_check_integrity: 7480b246afaSJeff Mahoney btrfs_info(info, "enabling check integrity"); 74921adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 75021adbd5cSStefan Behrens break; 75121adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 7522c334e87SWang Shilong ret = match_int(&args[0], &intarg); 7532c334e87SWang Shilong if (ret) { 7542c334e87SWang Shilong goto out; 7552c334e87SWang Shilong } else if (intarg >= 0) { 75621adbd5cSStefan Behrens info->check_integrity_print_mask = intarg; 7570b246afaSJeff Mahoney btrfs_info(info, 7585d163e0eSJeff Mahoney "check_integrity_print_mask 0x%x", 75921adbd5cSStefan Behrens info->check_integrity_print_mask); 7602c334e87SWang Shilong } else { 7612c334e87SWang Shilong ret = -EINVAL; 7622c334e87SWang Shilong goto out; 76321adbd5cSStefan Behrens } 76421adbd5cSStefan Behrens break; 76521adbd5cSStefan Behrens #else 76621adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 76721adbd5cSStefan Behrens case Opt_check_integrity: 76821adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 7690b246afaSJeff Mahoney btrfs_err(info, 770efe120a0SFrank Holton "support for check_integrity* not compiled in!"); 77121adbd5cSStefan Behrens ret = -EINVAL; 77221adbd5cSStefan Behrens goto out; 77321adbd5cSStefan Behrens #endif 7748c342930SJeff Mahoney case Opt_fatal_errors: 7758c342930SJeff Mahoney if (strcmp(args[0].from, "panic") == 0) 7768c342930SJeff Mahoney btrfs_set_opt(info->mount_opt, 7778c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7788c342930SJeff Mahoney else if (strcmp(args[0].from, "bug") == 0) 7798c342930SJeff Mahoney btrfs_clear_opt(info->mount_opt, 7808c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7818c342930SJeff Mahoney else { 7828c342930SJeff Mahoney ret = -EINVAL; 7838c342930SJeff Mahoney goto out; 7848c342930SJeff Mahoney } 7858c342930SJeff Mahoney break; 7868b87dc17SDavid Sterba case Opt_commit_interval: 7878b87dc17SDavid Sterba intarg = 0; 7888b87dc17SDavid Sterba ret = match_int(&args[0], &intarg); 7898b87dc17SDavid Sterba if (ret < 0) { 7900b246afaSJeff Mahoney btrfs_err(info, "invalid commit interval"); 7918b87dc17SDavid Sterba ret = -EINVAL; 7928b87dc17SDavid Sterba goto out; 7938b87dc17SDavid Sterba } 7948b87dc17SDavid Sterba if (intarg > 0) { 7958b87dc17SDavid Sterba if (intarg > 300) { 7960b246afaSJeff Mahoney btrfs_warn(info, 7975d163e0eSJeff Mahoney "excessive commit interval %d", 7988b87dc17SDavid Sterba intarg); 7998b87dc17SDavid Sterba } 8008b87dc17SDavid Sterba info->commit_interval = intarg; 8018b87dc17SDavid Sterba } else { 8020b246afaSJeff Mahoney btrfs_info(info, 8035d163e0eSJeff Mahoney "using default commit interval %ds", 8048b87dc17SDavid Sterba BTRFS_DEFAULT_COMMIT_INTERVAL); 8058b87dc17SDavid Sterba info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 8068b87dc17SDavid Sterba } 8078b87dc17SDavid Sterba break; 808d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 809d0bd4560SJosef Bacik case Opt_fragment_all: 8100b246afaSJeff Mahoney btrfs_info(info, "fragmenting all space"); 811d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 812d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA); 813d0bd4560SJosef Bacik break; 814d0bd4560SJosef Bacik case Opt_fragment_metadata: 8150b246afaSJeff Mahoney btrfs_info(info, "fragmenting metadata"); 816d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, 817d0bd4560SJosef Bacik FRAGMENT_METADATA); 818d0bd4560SJosef Bacik break; 819d0bd4560SJosef Bacik case Opt_fragment_data: 8200b246afaSJeff Mahoney btrfs_info(info, "fragmenting data"); 821d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 822d0bd4560SJosef Bacik break; 823d0bd4560SJosef Bacik #endif 824a7a3f7caSSage Weil case Opt_err: 8250b246afaSJeff Mahoney btrfs_info(info, "unrecognized mount option '%s'", p); 826a7a3f7caSSage Weil ret = -EINVAL; 827a7a3f7caSSage Weil goto out; 82895e05289SChris Mason default: 829be20aa9dSChris Mason break; 83095e05289SChris Mason } 83195e05289SChris Mason } 83296da0919SQu Wenruo check: 83396da0919SQu Wenruo /* 83496da0919SQu Wenruo * Extra check for current option against current flag 83596da0919SQu Wenruo */ 8363cdde224SJeff Mahoney if (btrfs_test_opt(info, NOLOGREPLAY) && !(new_flags & MS_RDONLY)) { 8370b246afaSJeff Mahoney btrfs_err(info, 83896da0919SQu Wenruo "nologreplay must be used with ro mount option"); 83996da0919SQu Wenruo ret = -EINVAL; 84096da0919SQu Wenruo } 841a7a3f7caSSage Weil out: 8420b246afaSJeff Mahoney if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) && 8433cdde224SJeff Mahoney !btrfs_test_opt(info, FREE_SPACE_TREE) && 8443cdde224SJeff Mahoney !btrfs_test_opt(info, CLEAR_CACHE)) { 8450b246afaSJeff Mahoney btrfs_err(info, "cannot disable free space tree"); 84670f6d82eSOmar Sandoval ret = -EINVAL; 84770f6d82eSOmar Sandoval 84870f6d82eSOmar Sandoval } 8493cdde224SJeff Mahoney if (!ret && btrfs_test_opt(info, SPACE_CACHE)) 8500b246afaSJeff Mahoney btrfs_info(info, "disk space caching is enabled"); 8513cdde224SJeff Mahoney if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE)) 8520b246afaSJeff Mahoney btrfs_info(info, "using free space tree"); 853da495eccSJosef Bacik kfree(orig); 854a7a3f7caSSage Weil return ret; 855edf24abeSChristoph Hellwig } 856edf24abeSChristoph Hellwig 857edf24abeSChristoph Hellwig /* 858edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 859edf24abeSChristoph Hellwig * 860edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 861edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 862edf24abeSChristoph Hellwig */ 86397288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 86473f73415SJosef Bacik void *holder, char **subvol_name, u64 *subvol_objectid, 8655e2a4b25SDavid Sterba struct btrfs_fs_devices **fs_devices) 866edf24abeSChristoph Hellwig { 867edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 86883c8c9bdSJeff Liu char *device_name, *opts, *orig, *p; 8691493381fSWang Shilong char *num = NULL; 870edf24abeSChristoph Hellwig int error = 0; 871edf24abeSChristoph Hellwig 872edf24abeSChristoph Hellwig if (!options) 873830c4adbSJosef Bacik return 0; 874edf24abeSChristoph Hellwig 875edf24abeSChristoph Hellwig /* 876edf24abeSChristoph Hellwig * strsep changes the string, duplicate it because parse_options 877edf24abeSChristoph Hellwig * gets called twice 878edf24abeSChristoph Hellwig */ 879edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 880edf24abeSChristoph Hellwig if (!opts) 881edf24abeSChristoph Hellwig return -ENOMEM; 8823f3d0bc0STero Roponen orig = opts; 883edf24abeSChristoph Hellwig 884edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 885edf24abeSChristoph Hellwig int token; 886edf24abeSChristoph Hellwig if (!*p) 887edf24abeSChristoph Hellwig continue; 888edf24abeSChristoph Hellwig 889edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 890edf24abeSChristoph Hellwig switch (token) { 891edf24abeSChristoph Hellwig case Opt_subvol: 892a90e8b6fSIlya Dryomov kfree(*subvol_name); 893edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 8942c334e87SWang Shilong if (!*subvol_name) { 8952c334e87SWang Shilong error = -ENOMEM; 8962c334e87SWang Shilong goto out; 8972c334e87SWang Shilong } 898edf24abeSChristoph Hellwig break; 89973f73415SJosef Bacik case Opt_subvolid: 9001493381fSWang Shilong num = match_strdup(&args[0]); 9011493381fSWang Shilong if (num) { 9021493381fSWang Shilong *subvol_objectid = memparse(num, NULL); 9031493381fSWang Shilong kfree(num); 9044849f01dSJosef Bacik /* we want the original fs_tree */ 9051493381fSWang Shilong if (!*subvol_objectid) 9064849f01dSJosef Bacik *subvol_objectid = 9074849f01dSJosef Bacik BTRFS_FS_TREE_OBJECTID; 9082c334e87SWang Shilong } else { 9092c334e87SWang Shilong error = -EINVAL; 9102c334e87SWang Shilong goto out; 9114849f01dSJosef Bacik } 91273f73415SJosef Bacik break; 913e15d0542SXin Zhong case Opt_subvolrootid: 91462e85577SJeff Mahoney pr_warn("BTRFS: 'subvolrootid' mount option is deprecated and has no effect\n"); 915e15d0542SXin Zhong break; 91643e570b0SChristoph Hellwig case Opt_device: 91783c8c9bdSJeff Liu device_name = match_strdup(&args[0]); 91883c8c9bdSJeff Liu if (!device_name) { 91983c8c9bdSJeff Liu error = -ENOMEM; 92083c8c9bdSJeff Liu goto out; 92183c8c9bdSJeff Liu } 92283c8c9bdSJeff Liu error = btrfs_scan_one_device(device_name, 92343e570b0SChristoph Hellwig flags, holder, fs_devices); 92483c8c9bdSJeff Liu kfree(device_name); 92543e570b0SChristoph Hellwig if (error) 926830c4adbSJosef Bacik goto out; 92743e570b0SChristoph Hellwig break; 928edf24abeSChristoph Hellwig default: 929edf24abeSChristoph Hellwig break; 930edf24abeSChristoph Hellwig } 931edf24abeSChristoph Hellwig } 932edf24abeSChristoph Hellwig 933edf24abeSChristoph Hellwig out: 934830c4adbSJosef Bacik kfree(orig); 935edf24abeSChristoph Hellwig return error; 93695e05289SChris Mason } 93795e05289SChris Mason 93805dbe683SOmar Sandoval static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info, 93973f73415SJosef Bacik u64 subvol_objectid) 94073f73415SJosef Bacik { 941815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 94205dbe683SOmar Sandoval struct btrfs_root *fs_root; 94305dbe683SOmar Sandoval struct btrfs_root_ref *root_ref; 94405dbe683SOmar Sandoval struct btrfs_inode_ref *inode_ref; 94505dbe683SOmar Sandoval struct btrfs_key key; 94605dbe683SOmar Sandoval struct btrfs_path *path = NULL; 94705dbe683SOmar Sandoval char *name = NULL, *ptr; 94805dbe683SOmar Sandoval u64 dirid; 94905dbe683SOmar Sandoval int len; 95005dbe683SOmar Sandoval int ret; 95105dbe683SOmar Sandoval 95205dbe683SOmar Sandoval path = btrfs_alloc_path(); 95305dbe683SOmar Sandoval if (!path) { 95405dbe683SOmar Sandoval ret = -ENOMEM; 95505dbe683SOmar Sandoval goto err; 95605dbe683SOmar Sandoval } 95705dbe683SOmar Sandoval path->leave_spinning = 1; 95805dbe683SOmar Sandoval 95905dbe683SOmar Sandoval name = kmalloc(PATH_MAX, GFP_NOFS); 96005dbe683SOmar Sandoval if (!name) { 96105dbe683SOmar Sandoval ret = -ENOMEM; 96205dbe683SOmar Sandoval goto err; 96305dbe683SOmar Sandoval } 96405dbe683SOmar Sandoval ptr = name + PATH_MAX - 1; 96505dbe683SOmar Sandoval ptr[0] = '\0'; 96605dbe683SOmar Sandoval 96705dbe683SOmar Sandoval /* 96805dbe683SOmar Sandoval * Walk up the subvolume trees in the tree of tree roots by root 96905dbe683SOmar Sandoval * backrefs until we hit the top-level subvolume. 97005dbe683SOmar Sandoval */ 97105dbe683SOmar Sandoval while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) { 97205dbe683SOmar Sandoval key.objectid = subvol_objectid; 97305dbe683SOmar Sandoval key.type = BTRFS_ROOT_BACKREF_KEY; 97405dbe683SOmar Sandoval key.offset = (u64)-1; 97505dbe683SOmar Sandoval 97605dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 97705dbe683SOmar Sandoval if (ret < 0) { 97805dbe683SOmar Sandoval goto err; 97905dbe683SOmar Sandoval } else if (ret > 0) { 98005dbe683SOmar Sandoval ret = btrfs_previous_item(root, path, subvol_objectid, 98105dbe683SOmar Sandoval BTRFS_ROOT_BACKREF_KEY); 98205dbe683SOmar Sandoval if (ret < 0) { 98305dbe683SOmar Sandoval goto err; 98405dbe683SOmar Sandoval } else if (ret > 0) { 98505dbe683SOmar Sandoval ret = -ENOENT; 98605dbe683SOmar Sandoval goto err; 98705dbe683SOmar Sandoval } 98805dbe683SOmar Sandoval } 98905dbe683SOmar Sandoval 99005dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 99105dbe683SOmar Sandoval subvol_objectid = key.offset; 99205dbe683SOmar Sandoval 99305dbe683SOmar Sandoval root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], 99405dbe683SOmar Sandoval struct btrfs_root_ref); 99505dbe683SOmar Sandoval len = btrfs_root_ref_name_len(path->nodes[0], root_ref); 99605dbe683SOmar Sandoval ptr -= len + 1; 99705dbe683SOmar Sandoval if (ptr < name) { 99805dbe683SOmar Sandoval ret = -ENAMETOOLONG; 99905dbe683SOmar Sandoval goto err; 100005dbe683SOmar Sandoval } 100105dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 100205dbe683SOmar Sandoval (unsigned long)(root_ref + 1), len); 100305dbe683SOmar Sandoval ptr[0] = '/'; 100405dbe683SOmar Sandoval dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref); 100505dbe683SOmar Sandoval btrfs_release_path(path); 100605dbe683SOmar Sandoval 100705dbe683SOmar Sandoval key.objectid = subvol_objectid; 100805dbe683SOmar Sandoval key.type = BTRFS_ROOT_ITEM_KEY; 100905dbe683SOmar Sandoval key.offset = (u64)-1; 101005dbe683SOmar Sandoval fs_root = btrfs_read_fs_root_no_name(fs_info, &key); 101105dbe683SOmar Sandoval if (IS_ERR(fs_root)) { 101205dbe683SOmar Sandoval ret = PTR_ERR(fs_root); 101305dbe683SOmar Sandoval goto err; 101405dbe683SOmar Sandoval } 101505dbe683SOmar Sandoval 101605dbe683SOmar Sandoval /* 101705dbe683SOmar Sandoval * Walk up the filesystem tree by inode refs until we hit the 101805dbe683SOmar Sandoval * root directory. 101905dbe683SOmar Sandoval */ 102005dbe683SOmar Sandoval while (dirid != BTRFS_FIRST_FREE_OBJECTID) { 102105dbe683SOmar Sandoval key.objectid = dirid; 102205dbe683SOmar Sandoval key.type = BTRFS_INODE_REF_KEY; 102305dbe683SOmar Sandoval key.offset = (u64)-1; 102405dbe683SOmar Sandoval 102505dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); 102605dbe683SOmar Sandoval if (ret < 0) { 102705dbe683SOmar Sandoval goto err; 102805dbe683SOmar Sandoval } else if (ret > 0) { 102905dbe683SOmar Sandoval ret = btrfs_previous_item(fs_root, path, dirid, 103005dbe683SOmar Sandoval BTRFS_INODE_REF_KEY); 103105dbe683SOmar Sandoval if (ret < 0) { 103205dbe683SOmar Sandoval goto err; 103305dbe683SOmar Sandoval } else if (ret > 0) { 103405dbe683SOmar Sandoval ret = -ENOENT; 103505dbe683SOmar Sandoval goto err; 103605dbe683SOmar Sandoval } 103705dbe683SOmar Sandoval } 103805dbe683SOmar Sandoval 103905dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 104005dbe683SOmar Sandoval dirid = key.offset; 104105dbe683SOmar Sandoval 104205dbe683SOmar Sandoval inode_ref = btrfs_item_ptr(path->nodes[0], 104305dbe683SOmar Sandoval path->slots[0], 104405dbe683SOmar Sandoval struct btrfs_inode_ref); 104505dbe683SOmar Sandoval len = btrfs_inode_ref_name_len(path->nodes[0], 104605dbe683SOmar Sandoval inode_ref); 104705dbe683SOmar Sandoval ptr -= len + 1; 104805dbe683SOmar Sandoval if (ptr < name) { 104905dbe683SOmar Sandoval ret = -ENAMETOOLONG; 105005dbe683SOmar Sandoval goto err; 105105dbe683SOmar Sandoval } 105205dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 105305dbe683SOmar Sandoval (unsigned long)(inode_ref + 1), len); 105405dbe683SOmar Sandoval ptr[0] = '/'; 105505dbe683SOmar Sandoval btrfs_release_path(path); 105605dbe683SOmar Sandoval } 105705dbe683SOmar Sandoval } 105805dbe683SOmar Sandoval 105905dbe683SOmar Sandoval btrfs_free_path(path); 106005dbe683SOmar Sandoval if (ptr == name + PATH_MAX - 1) { 106105dbe683SOmar Sandoval name[0] = '/'; 106205dbe683SOmar Sandoval name[1] = '\0'; 106305dbe683SOmar Sandoval } else { 106405dbe683SOmar Sandoval memmove(name, ptr, name + PATH_MAX - ptr); 106505dbe683SOmar Sandoval } 106605dbe683SOmar Sandoval return name; 106705dbe683SOmar Sandoval 106805dbe683SOmar Sandoval err: 106905dbe683SOmar Sandoval btrfs_free_path(path); 107005dbe683SOmar Sandoval kfree(name); 107105dbe683SOmar Sandoval return ERR_PTR(ret); 107205dbe683SOmar Sandoval } 107305dbe683SOmar Sandoval 107405dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid) 107505dbe683SOmar Sandoval { 107605dbe683SOmar Sandoval struct btrfs_root *root = fs_info->tree_root; 107773f73415SJosef Bacik struct btrfs_dir_item *di; 107873f73415SJosef Bacik struct btrfs_path *path; 107973f73415SJosef Bacik struct btrfs_key location; 108073f73415SJosef Bacik u64 dir_id; 108173f73415SJosef Bacik 108273f73415SJosef Bacik path = btrfs_alloc_path(); 108373f73415SJosef Bacik if (!path) 108405dbe683SOmar Sandoval return -ENOMEM; 108573f73415SJosef Bacik path->leave_spinning = 1; 108673f73415SJosef Bacik 108773f73415SJosef Bacik /* 108873f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 108973f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 109073f73415SJosef Bacik * to mount. 109173f73415SJosef Bacik */ 1092815745cfSAl Viro dir_id = btrfs_super_root_dir(fs_info->super_copy); 109373f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 1094b0839166SJulia Lawall if (IS_ERR(di)) { 1095b0839166SJulia Lawall btrfs_free_path(path); 109605dbe683SOmar Sandoval return PTR_ERR(di); 1097b0839166SJulia Lawall } 109873f73415SJosef Bacik if (!di) { 109973f73415SJosef Bacik /* 110073f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 110173f73415SJosef Bacik * it's always been there, but don't freak out, just try and 110205dbe683SOmar Sandoval * mount the top-level subvolume. 110373f73415SJosef Bacik */ 110473f73415SJosef Bacik btrfs_free_path(path); 110505dbe683SOmar Sandoval *objectid = BTRFS_FS_TREE_OBJECTID; 110605dbe683SOmar Sandoval return 0; 110773f73415SJosef Bacik } 110873f73415SJosef Bacik 110973f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 111073f73415SJosef Bacik btrfs_free_path(path); 111105dbe683SOmar Sandoval *objectid = location.objectid; 111205dbe683SOmar Sandoval return 0; 111373f73415SJosef Bacik } 111473f73415SJosef Bacik 11158a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 11168a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 1117*56e033a7SDavid Sterba void *data) 11182e635a27SChris Mason { 11192e635a27SChris Mason struct inode *inode; 1120815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 11215d4f98a2SYan Zheng struct btrfs_key key; 112239279cc3SChris Mason int err; 11232e635a27SChris Mason 11242e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 11252e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 1126e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 1127af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 1128be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 11295103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 11302e635a27SChris Mason sb->s_time_gran = 1; 11310eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 113233268eafSJosef Bacik sb->s_flags |= MS_POSIXACL; 113349cf6f45SChris Ball #endif 11340c4d2d95SJosef Bacik sb->s_flags |= MS_I_VERSION; 1135da2f0f74SChris Mason sb->s_iflags |= SB_I_CGROUPWB; 1136ad2b2c80SAl Viro err = open_ctree(sb, fs_devices, (char *)data); 1137ad2b2c80SAl Viro if (err) { 1138ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "open_ctree failed"); 1139ad2b2c80SAl Viro return err; 1140e20d96d6SChris Mason } 1141b888db2bSChris Mason 11425d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 11435d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 11445d4f98a2SYan Zheng key.offset = 0; 114598c7089cSAl Viro inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL); 11465d4f98a2SYan Zheng if (IS_ERR(inode)) { 11475d4f98a2SYan Zheng err = PTR_ERR(inode); 114839279cc3SChris Mason goto fail_close; 114939279cc3SChris Mason } 11502e635a27SChris Mason 115148fde701SAl Viro sb->s_root = d_make_root(inode); 115248fde701SAl Viro if (!sb->s_root) { 115339279cc3SChris Mason err = -ENOMEM; 115439279cc3SChris Mason goto fail_close; 11552e635a27SChris Mason } 115658176a96SJosef Bacik 11576885f308SChris Mason save_mount_options(sb, data); 115890a887c9SDan Magenheimer cleancache_init_fs(sb); 115959553edfSAl Viro sb->s_flags |= MS_ACTIVE; 11602e635a27SChris Mason return 0; 11612e635a27SChris Mason 116239279cc3SChris Mason fail_close: 11636bccf3abSJeff Mahoney close_ctree(fs_info); 1164d5719762SChris Mason return err; 1165d5719762SChris Mason } 1166d5719762SChris Mason 11676bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 1168d5719762SChris Mason { 1169d5719762SChris Mason struct btrfs_trans_handle *trans; 1170815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1171815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 1172df2ce34cSChris Mason 1173bc074524SJeff Mahoney trace_btrfs_sync_fs(fs_info, wait); 11741abe9b8aSliubo 1175d561c025SChris Mason if (!wait) { 1176815745cfSAl Viro filemap_flush(fs_info->btree_inode->i_mapping); 1177df2ce34cSChris Mason return 0; 1178d561c025SChris Mason } 1179771ed689SChris Mason 1180578def7cSFilipe Manana btrfs_wait_ordered_roots(fs_info, -1, 0, (u64)-1); 1181771ed689SChris Mason 1182d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 118360376ce4SJosef Bacik if (IS_ERR(trans)) { 1184354aa0fbSMiao Xie /* no transaction, don't bother */ 11856b5fe46dSDavid Sterba if (PTR_ERR(trans) == -ENOENT) { 11866b5fe46dSDavid Sterba /* 11876b5fe46dSDavid Sterba * Exit unless we have some pending changes 11886b5fe46dSDavid Sterba * that need to go through commit 11896b5fe46dSDavid Sterba */ 11906b5fe46dSDavid Sterba if (fs_info->pending_changes == 0) 1191bd7de2c9SJosef Bacik return 0; 1192a53f4f8eSQu Wenruo /* 1193a53f4f8eSQu Wenruo * A non-blocking test if the fs is frozen. We must not 1194a53f4f8eSQu Wenruo * start a new transaction here otherwise a deadlock 1195a53f4f8eSQu Wenruo * happens. The pending operations are delayed to the 1196a53f4f8eSQu Wenruo * next commit after thawing. 1197a53f4f8eSQu Wenruo */ 1198a53f4f8eSQu Wenruo if (__sb_start_write(sb, SB_FREEZE_WRITE, false)) 1199a53f4f8eSQu Wenruo __sb_end_write(sb, SB_FREEZE_WRITE); 1200a53f4f8eSQu Wenruo else 1201a53f4f8eSQu Wenruo return 0; 12026b5fe46dSDavid Sterba trans = btrfs_start_transaction(root, 0); 120360376ce4SJosef Bacik } 120498bd5c54SDavid Sterba if (IS_ERR(trans)) 120598bd5c54SDavid Sterba return PTR_ERR(trans); 12066b5fe46dSDavid Sterba } 12073a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 1208d5719762SChris Mason } 1209d5719762SChris Mason 121034c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 1211a9572a15SEric Paris { 1212815745cfSAl Viro struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 1213200da64eSTsutomu Itoh char *compress_type; 1214a9572a15SEric Paris 12153cdde224SJeff Mahoney if (btrfs_test_opt(info, DEGRADED)) 1216a9572a15SEric Paris seq_puts(seq, ",degraded"); 12173cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATASUM)) 1218a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 12193cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATACOW)) 1220a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 12213cdde224SJeff Mahoney if (btrfs_test_opt(info, NOBARRIER)) 1222a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 122395ac567aSFilipe David Borba Manana if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE) 1224c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",max_inline=%llu", info->max_inline); 1225a9572a15SEric Paris if (info->alloc_start != 0) 1226c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",alloc_start=%llu", info->alloc_start); 1227a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 1228a9572a15SEric Paris num_online_cpus() + 2, 8)) 1229a9572a15SEric Paris seq_printf(seq, ",thread_pool=%d", info->thread_pool_size); 12303cdde224SJeff Mahoney if (btrfs_test_opt(info, COMPRESS)) { 1231200da64eSTsutomu Itoh if (info->compress_type == BTRFS_COMPRESS_ZLIB) 1232200da64eSTsutomu Itoh compress_type = "zlib"; 1233200da64eSTsutomu Itoh else 1234200da64eSTsutomu Itoh compress_type = "lzo"; 12353cdde224SJeff Mahoney if (btrfs_test_opt(info, FORCE_COMPRESS)) 1236200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 1237200da64eSTsutomu Itoh else 1238200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 1239200da64eSTsutomu Itoh } 12403cdde224SJeff Mahoney if (btrfs_test_opt(info, NOSSD)) 1241c289811cSChris Mason seq_puts(seq, ",nossd"); 12423cdde224SJeff Mahoney if (btrfs_test_opt(info, SSD_SPREAD)) 1243451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 12443cdde224SJeff Mahoney else if (btrfs_test_opt(info, SSD)) 1245a9572a15SEric Paris seq_puts(seq, ",ssd"); 12463cdde224SJeff Mahoney if (btrfs_test_opt(info, NOTREELOG)) 12476b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 12483cdde224SJeff Mahoney if (btrfs_test_opt(info, NOLOGREPLAY)) 124996da0919SQu Wenruo seq_puts(seq, ",nologreplay"); 12503cdde224SJeff Mahoney if (btrfs_test_opt(info, FLUSHONCOMMIT)) 12516b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 12523cdde224SJeff Mahoney if (btrfs_test_opt(info, DISCARD)) 125320a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 12540b246afaSJeff Mahoney if (!(info->sb->s_flags & MS_POSIXACL)) 1255a9572a15SEric Paris seq_puts(seq, ",noacl"); 12563cdde224SJeff Mahoney if (btrfs_test_opt(info, SPACE_CACHE)) 1257200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 12583cdde224SJeff Mahoney else if (btrfs_test_opt(info, FREE_SPACE_TREE)) 125970f6d82eSOmar Sandoval seq_puts(seq, ",space_cache=v2"); 126073bc1876SJosef Bacik else 12618965593eSDavid Sterba seq_puts(seq, ",nospace_cache"); 12623cdde224SJeff Mahoney if (btrfs_test_opt(info, RESCAN_UUID_TREE)) 1263f420ee1eSStefan Behrens seq_puts(seq, ",rescan_uuid_tree"); 12643cdde224SJeff Mahoney if (btrfs_test_opt(info, CLEAR_CACHE)) 1265200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 12663cdde224SJeff Mahoney if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED)) 1267200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 12683cdde224SJeff Mahoney if (btrfs_test_opt(info, ENOSPC_DEBUG)) 12690942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 12703cdde224SJeff Mahoney if (btrfs_test_opt(info, AUTO_DEFRAG)) 12710942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 12723cdde224SJeff Mahoney if (btrfs_test_opt(info, INODE_MAP_CACHE)) 12730942caa3SDavid Sterba seq_puts(seq, ",inode_cache"); 12743cdde224SJeff Mahoney if (btrfs_test_opt(info, SKIP_BALANCE)) 12759555c6c1SIlya Dryomov seq_puts(seq, ",skip_balance"); 12768507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 12773cdde224SJeff Mahoney if (btrfs_test_opt(info, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA)) 12788507d216SWang Shilong seq_puts(seq, ",check_int_data"); 12793cdde224SJeff Mahoney else if (btrfs_test_opt(info, CHECK_INTEGRITY)) 12808507d216SWang Shilong seq_puts(seq, ",check_int"); 12818507d216SWang Shilong if (info->check_integrity_print_mask) 12828507d216SWang Shilong seq_printf(seq, ",check_int_print_mask=%d", 12838507d216SWang Shilong info->check_integrity_print_mask); 12848507d216SWang Shilong #endif 12858507d216SWang Shilong if (info->metadata_ratio) 12868507d216SWang Shilong seq_printf(seq, ",metadata_ratio=%d", 12878507d216SWang Shilong info->metadata_ratio); 12883cdde224SJeff Mahoney if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR)) 12898c342930SJeff Mahoney seq_puts(seq, ",fatal_errors=panic"); 12908b87dc17SDavid Sterba if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL) 12918b87dc17SDavid Sterba seq_printf(seq, ",commit=%d", info->commit_interval); 1292d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 12933cdde224SJeff Mahoney if (btrfs_test_opt(info, FRAGMENT_DATA)) 1294d0bd4560SJosef Bacik seq_puts(seq, ",fragment=data"); 12953cdde224SJeff Mahoney if (btrfs_test_opt(info, FRAGMENT_METADATA)) 1296d0bd4560SJosef Bacik seq_puts(seq, ",fragment=metadata"); 1297d0bd4560SJosef Bacik #endif 1298c8d3fe02SOmar Sandoval seq_printf(seq, ",subvolid=%llu", 1299c8d3fe02SOmar Sandoval BTRFS_I(d_inode(dentry))->root->root_key.objectid); 1300c8d3fe02SOmar Sandoval seq_puts(seq, ",subvol="); 1301c8d3fe02SOmar Sandoval seq_dentry(seq, dentry, " \t\n\\"); 1302a9572a15SEric Paris return 0; 1303a9572a15SEric Paris } 1304a9572a15SEric Paris 1305a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 13062e635a27SChris Mason { 1307815745cfSAl Viro struct btrfs_fs_info *p = data; 1308815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(s); 13094b82d6e4SYan 1310815745cfSAl Viro return fs_info->fs_devices == p->fs_devices; 13114b82d6e4SYan } 13124b82d6e4SYan 1313450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 1314450ba0eaSJosef Bacik { 13156de1d09dSAl Viro int err = set_anon_super(s, data); 13166de1d09dSAl Viro if (!err) 1317450ba0eaSJosef Bacik s->s_fs_info = data; 13186de1d09dSAl Viro return err; 1319450ba0eaSJosef Bacik } 1320450ba0eaSJosef Bacik 1321830c4adbSJosef Bacik /* 1322f9d9ef62SDavid Sterba * subvolumes are identified by ino 256 1323f9d9ef62SDavid Sterba */ 1324f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode) 1325f9d9ef62SDavid Sterba { 1326f9d9ef62SDavid Sterba if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 1327f9d9ef62SDavid Sterba return 1; 1328f9d9ef62SDavid Sterba return 0; 1329f9d9ef62SDavid Sterba } 1330f9d9ef62SDavid Sterba 1331f9d9ef62SDavid Sterba /* 1332e6e4dbe8SOmar Sandoval * This will add subvolid=0 to the argument string while removing any subvol= 1333e6e4dbe8SOmar Sandoval * and subvolid= arguments to make sure we get the top-level root for path 1334e6e4dbe8SOmar Sandoval * walking to the subvol we want. 1335830c4adbSJosef Bacik */ 1336830c4adbSJosef Bacik static char *setup_root_args(char *args) 1337830c4adbSJosef Bacik { 1338e6e4dbe8SOmar Sandoval char *buf, *dst, *sep; 1339830c4adbSJosef Bacik 1340e6e4dbe8SOmar Sandoval if (!args) 1341e6e4dbe8SOmar Sandoval return kstrdup("subvolid=0", GFP_NOFS); 1342830c4adbSJosef Bacik 1343e6e4dbe8SOmar Sandoval /* The worst case is that we add ",subvolid=0" to the end. */ 1344e6e4dbe8SOmar Sandoval buf = dst = kmalloc(strlen(args) + strlen(",subvolid=0") + 1, GFP_NOFS); 1345f60d16a8SJim Meyering if (!buf) 1346f60d16a8SJim Meyering return NULL; 1347830c4adbSJosef Bacik 1348e6e4dbe8SOmar Sandoval while (1) { 1349e6e4dbe8SOmar Sandoval sep = strchrnul(args, ','); 1350e6e4dbe8SOmar Sandoval if (!strstarts(args, "subvol=") && 1351e6e4dbe8SOmar Sandoval !strstarts(args, "subvolid=")) { 1352e6e4dbe8SOmar Sandoval memcpy(dst, args, sep - args); 1353e6e4dbe8SOmar Sandoval dst += sep - args; 1354e6e4dbe8SOmar Sandoval *dst++ = ','; 1355830c4adbSJosef Bacik } 1356e6e4dbe8SOmar Sandoval if (*sep) 1357e6e4dbe8SOmar Sandoval args = sep + 1; 1358e6e4dbe8SOmar Sandoval else 1359e6e4dbe8SOmar Sandoval break; 1360e6e4dbe8SOmar Sandoval } 1361f60d16a8SJim Meyering strcpy(dst, "subvolid=0"); 1362830c4adbSJosef Bacik 1363f60d16a8SJim Meyering return buf; 1364830c4adbSJosef Bacik } 1365830c4adbSJosef Bacik 1366bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid, 1367bb289b7bSOmar Sandoval int flags, const char *device_name, 1368bb289b7bSOmar Sandoval char *data) 1369830c4adbSJosef Bacik { 1370830c4adbSJosef Bacik struct dentry *root; 1371fa330659SOmar Sandoval struct vfsmount *mnt = NULL; 1372830c4adbSJosef Bacik char *newargs; 1373fa330659SOmar Sandoval int ret; 1374830c4adbSJosef Bacik 1375830c4adbSJosef Bacik newargs = setup_root_args(data); 1376fa330659SOmar Sandoval if (!newargs) { 1377fa330659SOmar Sandoval root = ERR_PTR(-ENOMEM); 1378fa330659SOmar Sandoval goto out; 1379fa330659SOmar Sandoval } 13800723a047SHarald Hoyer 1381fa330659SOmar Sandoval mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, newargs); 1382fa330659SOmar Sandoval if (PTR_ERR_OR_ZERO(mnt) == -EBUSY) { 13830723a047SHarald Hoyer if (flags & MS_RDONLY) { 1384fa330659SOmar Sandoval mnt = vfs_kern_mount(&btrfs_fs_type, flags & ~MS_RDONLY, 1385fa330659SOmar Sandoval device_name, newargs); 13860723a047SHarald Hoyer } else { 1387fa330659SOmar Sandoval mnt = vfs_kern_mount(&btrfs_fs_type, flags | MS_RDONLY, 1388fa330659SOmar Sandoval device_name, newargs); 13890040e606SChristoph Jaeger if (IS_ERR(mnt)) { 1390fa330659SOmar Sandoval root = ERR_CAST(mnt); 1391fa330659SOmar Sandoval mnt = NULL; 1392fa330659SOmar Sandoval goto out; 13930040e606SChristoph Jaeger } 13940723a047SHarald Hoyer 1395773cd04eSOmar Sandoval down_write(&mnt->mnt_sb->s_umount); 1396fa330659SOmar Sandoval ret = btrfs_remount(mnt->mnt_sb, &flags, NULL); 1397773cd04eSOmar Sandoval up_write(&mnt->mnt_sb->s_umount); 1398fa330659SOmar Sandoval if (ret < 0) { 1399fa330659SOmar Sandoval root = ERR_PTR(ret); 1400fa330659SOmar Sandoval goto out; 14010723a047SHarald Hoyer } 14020723a047SHarald Hoyer } 14030723a047SHarald Hoyer } 1404fa330659SOmar Sandoval if (IS_ERR(mnt)) { 1405fa330659SOmar Sandoval root = ERR_CAST(mnt); 1406fa330659SOmar Sandoval mnt = NULL; 1407fa330659SOmar Sandoval goto out; 1408fa330659SOmar Sandoval } 1409830c4adbSJosef Bacik 141005dbe683SOmar Sandoval if (!subvol_name) { 141105dbe683SOmar Sandoval if (!subvol_objectid) { 141205dbe683SOmar Sandoval ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb), 141305dbe683SOmar Sandoval &subvol_objectid); 141405dbe683SOmar Sandoval if (ret) { 141505dbe683SOmar Sandoval root = ERR_PTR(ret); 141605dbe683SOmar Sandoval goto out; 141705dbe683SOmar Sandoval } 141805dbe683SOmar Sandoval } 141905dbe683SOmar Sandoval subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb), 142005dbe683SOmar Sandoval subvol_objectid); 142105dbe683SOmar Sandoval if (IS_ERR(subvol_name)) { 142205dbe683SOmar Sandoval root = ERR_CAST(subvol_name); 142305dbe683SOmar Sandoval subvol_name = NULL; 142405dbe683SOmar Sandoval goto out; 142505dbe683SOmar Sandoval } 142605dbe683SOmar Sandoval 142705dbe683SOmar Sandoval } 142805dbe683SOmar Sandoval 1429ea441d11SAl Viro root = mount_subtree(mnt, subvol_name); 1430fa330659SOmar Sandoval /* mount_subtree() drops our reference on the vfsmount. */ 1431fa330659SOmar Sandoval mnt = NULL; 1432830c4adbSJosef Bacik 1433bb289b7bSOmar Sandoval if (!IS_ERR(root)) { 1434ea441d11SAl Viro struct super_block *s = root->d_sb; 1435ab8d0fc4SJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(s); 1436bb289b7bSOmar Sandoval struct inode *root_inode = d_inode(root); 1437bb289b7bSOmar Sandoval u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid; 1438bb289b7bSOmar Sandoval 1439bb289b7bSOmar Sandoval ret = 0; 1440bb289b7bSOmar Sandoval if (!is_subvolume_inode(root_inode)) { 1441ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "'%s' is not a valid subvolume", 1442bb289b7bSOmar Sandoval subvol_name); 1443bb289b7bSOmar Sandoval ret = -EINVAL; 1444bb289b7bSOmar Sandoval } 1445bb289b7bSOmar Sandoval if (subvol_objectid && root_objectid != subvol_objectid) { 144605dbe683SOmar Sandoval /* 144705dbe683SOmar Sandoval * This will also catch a race condition where a 144805dbe683SOmar Sandoval * subvolume which was passed by ID is renamed and 144905dbe683SOmar Sandoval * another subvolume is renamed over the old location. 145005dbe683SOmar Sandoval */ 1451ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 1452ab8d0fc4SJeff Mahoney "subvol '%s' does not match subvolid %llu", 1453bb289b7bSOmar Sandoval subvol_name, subvol_objectid); 1454bb289b7bSOmar Sandoval ret = -EINVAL; 1455bb289b7bSOmar Sandoval } 1456bb289b7bSOmar Sandoval if (ret) { 1457ea441d11SAl Viro dput(root); 1458bb289b7bSOmar Sandoval root = ERR_PTR(ret); 1459ea441d11SAl Viro deactivate_locked_super(s); 1460bb289b7bSOmar Sandoval } 1461f9d9ef62SDavid Sterba } 1462f9d9ef62SDavid Sterba 1463fa330659SOmar Sandoval out: 1464fa330659SOmar Sandoval mntput(mnt); 1465fa330659SOmar Sandoval kfree(newargs); 1466fa330659SOmar Sandoval kfree(subvol_name); 1467830c4adbSJosef Bacik return root; 1468830c4adbSJosef Bacik } 1469450ba0eaSJosef Bacik 1470f667aef6SQu Wenruo static int parse_security_options(char *orig_opts, 1471f667aef6SQu Wenruo struct security_mnt_opts *sec_opts) 1472f667aef6SQu Wenruo { 1473f667aef6SQu Wenruo char *secdata = NULL; 1474f667aef6SQu Wenruo int ret = 0; 1475f667aef6SQu Wenruo 1476f667aef6SQu Wenruo secdata = alloc_secdata(); 1477f667aef6SQu Wenruo if (!secdata) 1478f667aef6SQu Wenruo return -ENOMEM; 1479f667aef6SQu Wenruo ret = security_sb_copy_data(orig_opts, secdata); 1480f667aef6SQu Wenruo if (ret) { 1481f667aef6SQu Wenruo free_secdata(secdata); 1482f667aef6SQu Wenruo return ret; 1483f667aef6SQu Wenruo } 1484f667aef6SQu Wenruo ret = security_sb_parse_opts_str(secdata, sec_opts); 1485f667aef6SQu Wenruo free_secdata(secdata); 1486f667aef6SQu Wenruo return ret; 1487f667aef6SQu Wenruo } 1488f667aef6SQu Wenruo 1489f667aef6SQu Wenruo static int setup_security_options(struct btrfs_fs_info *fs_info, 1490f667aef6SQu Wenruo struct super_block *sb, 1491f667aef6SQu Wenruo struct security_mnt_opts *sec_opts) 1492f667aef6SQu Wenruo { 1493f667aef6SQu Wenruo int ret = 0; 1494f667aef6SQu Wenruo 1495f667aef6SQu Wenruo /* 1496f667aef6SQu Wenruo * Call security_sb_set_mnt_opts() to check whether new sec_opts 1497f667aef6SQu Wenruo * is valid. 1498f667aef6SQu Wenruo */ 1499f667aef6SQu Wenruo ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL); 1500f667aef6SQu Wenruo if (ret) 1501f667aef6SQu Wenruo return ret; 1502f667aef6SQu Wenruo 1503a43bb39bSQu Wenruo #ifdef CONFIG_SECURITY 1504f667aef6SQu Wenruo if (!fs_info->security_opts.num_mnt_opts) { 1505f667aef6SQu Wenruo /* first time security setup, copy sec_opts to fs_info */ 1506f667aef6SQu Wenruo memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts)); 1507f667aef6SQu Wenruo } else { 1508f667aef6SQu Wenruo /* 1509180e4d47SLuis de Bethencourt * Since SELinux (the only one supporting security_mnt_opts) 1510180e4d47SLuis de Bethencourt * does NOT support changing context during remount/mount of 1511180e4d47SLuis de Bethencourt * the same sb, this must be the same or part of the same 1512180e4d47SLuis de Bethencourt * security options, just free it. 1513f667aef6SQu Wenruo */ 1514f667aef6SQu Wenruo security_free_mnt_opts(sec_opts); 1515f667aef6SQu Wenruo } 1516a43bb39bSQu Wenruo #endif 1517f667aef6SQu Wenruo return ret; 1518f667aef6SQu Wenruo } 1519f667aef6SQu Wenruo 1520edf24abeSChristoph Hellwig /* 1521edf24abeSChristoph Hellwig * Find a superblock for the given device / mount point. 1522edf24abeSChristoph Hellwig * 1523edf24abeSChristoph Hellwig * Note: This is based on get_sb_bdev from fs/super.c with a few additions 1524edf24abeSChristoph Hellwig * for multiple device setup. Make sure to keep it in sync. 1525edf24abeSChristoph Hellwig */ 1526061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1527306e16ceSDavid Sterba const char *device_name, void *data) 15284b82d6e4SYan { 15294b82d6e4SYan struct block_device *bdev = NULL; 15304b82d6e4SYan struct super_block *s; 15318a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices = NULL; 1532450ba0eaSJosef Bacik struct btrfs_fs_info *fs_info = NULL; 1533f667aef6SQu Wenruo struct security_mnt_opts new_sec_opts; 153497288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 153573f73415SJosef Bacik char *subvol_name = NULL; 153673f73415SJosef Bacik u64 subvol_objectid = 0; 15374b82d6e4SYan int error = 0; 15384b82d6e4SYan 153997288f2cSChristoph Hellwig if (!(flags & MS_RDONLY)) 154097288f2cSChristoph Hellwig mode |= FMODE_WRITE; 154197288f2cSChristoph Hellwig 154297288f2cSChristoph Hellwig error = btrfs_parse_early_options(data, mode, fs_type, 154373f73415SJosef Bacik &subvol_name, &subvol_objectid, 15445e2a4b25SDavid Sterba &fs_devices); 1545f23c8af8SIlya Dryomov if (error) { 1546f23c8af8SIlya Dryomov kfree(subvol_name); 1547061dbc6bSAl Viro return ERR_PTR(error); 1548f23c8af8SIlya Dryomov } 1549edf24abeSChristoph Hellwig 155005dbe683SOmar Sandoval if (subvol_name || subvol_objectid != BTRFS_FS_TREE_OBJECTID) { 1551fa330659SOmar Sandoval /* mount_subvol() will free subvol_name. */ 1552bb289b7bSOmar Sandoval return mount_subvol(subvol_name, subvol_objectid, flags, 1553bb289b7bSOmar Sandoval device_name, data); 1554830c4adbSJosef Bacik } 1555830c4adbSJosef Bacik 1556f667aef6SQu Wenruo security_init_mnt_opts(&new_sec_opts); 1557f667aef6SQu Wenruo if (data) { 1558f667aef6SQu Wenruo error = parse_security_options(data, &new_sec_opts); 15598a4b83ccSChris Mason if (error) 1560830c4adbSJosef Bacik return ERR_PTR(error); 1561f667aef6SQu Wenruo } 1562f667aef6SQu Wenruo 1563f667aef6SQu Wenruo error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices); 1564f667aef6SQu Wenruo if (error) 1565f667aef6SQu Wenruo goto error_sec_opts; 15664b82d6e4SYan 1567450ba0eaSJosef Bacik /* 1568450ba0eaSJosef Bacik * Setup a dummy root and fs_info for test/set super. This is because 1569450ba0eaSJosef Bacik * we don't actually fill this stuff out until open_ctree, but we need 1570450ba0eaSJosef Bacik * it for searching for existing supers, so this lets us do that and 1571450ba0eaSJosef Bacik * then open_ctree will properly initialize everything later. 1572450ba0eaSJosef Bacik */ 1573450ba0eaSJosef Bacik fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS); 1574f667aef6SQu Wenruo if (!fs_info) { 1575f667aef6SQu Wenruo error = -ENOMEM; 1576f667aef6SQu Wenruo goto error_sec_opts; 1577f667aef6SQu Wenruo } 157804d21a24SIlya Dryomov 1579450ba0eaSJosef Bacik fs_info->fs_devices = fs_devices; 1580450ba0eaSJosef Bacik 15816c41761fSDavid Sterba fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 15826c41761fSDavid Sterba fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 1583f667aef6SQu Wenruo security_init_mnt_opts(&fs_info->security_opts); 15846c41761fSDavid Sterba if (!fs_info->super_copy || !fs_info->super_for_commit) { 15856c41761fSDavid Sterba error = -ENOMEM; 158604d21a24SIlya Dryomov goto error_fs_info; 158704d21a24SIlya Dryomov } 158804d21a24SIlya Dryomov 158904d21a24SIlya Dryomov error = btrfs_open_devices(fs_devices, mode, fs_type); 159004d21a24SIlya Dryomov if (error) 159104d21a24SIlya Dryomov goto error_fs_info; 159204d21a24SIlya Dryomov 159304d21a24SIlya Dryomov if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) { 159404d21a24SIlya Dryomov error = -EACCES; 15956c41761fSDavid Sterba goto error_close_devices; 15966c41761fSDavid Sterba } 15976c41761fSDavid Sterba 1598dfe25020SChris Mason bdev = fs_devices->latest_bdev; 15999249e17fSDavid Howells s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC, 16009249e17fSDavid Howells fs_info); 1601830c4adbSJosef Bacik if (IS_ERR(s)) { 1602830c4adbSJosef Bacik error = PTR_ERR(s); 1603830c4adbSJosef Bacik goto error_close_devices; 1604830c4adbSJosef Bacik } 16054b82d6e4SYan 16064b82d6e4SYan if (s->s_root) { 16072b82032cSYan Zheng btrfs_close_devices(fs_devices); 16086c41761fSDavid Sterba free_fs_info(fs_info); 160959553edfSAl Viro if ((flags ^ s->s_flags) & MS_RDONLY) 161059553edfSAl Viro error = -EBUSY; 16114b82d6e4SYan } else { 1612a1c6f057SDmitry Monakhov snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev); 1613815745cfSAl Viro btrfs_sb(s)->bdev_holder = fs_type; 1614*56e033a7SDavid Sterba error = btrfs_fill_super(s, fs_devices, data); 16154b82d6e4SYan } 161605dbe683SOmar Sandoval if (error) { 1617e15d0542SXin Zhong deactivate_locked_super(s); 1618f667aef6SQu Wenruo goto error_sec_opts; 1619f667aef6SQu Wenruo } 1620f667aef6SQu Wenruo 1621f667aef6SQu Wenruo fs_info = btrfs_sb(s); 1622f667aef6SQu Wenruo error = setup_security_options(fs_info, s, &new_sec_opts); 1623f667aef6SQu Wenruo if (error) { 1624f667aef6SQu Wenruo deactivate_locked_super(s); 1625f667aef6SQu Wenruo goto error_sec_opts; 1626f667aef6SQu Wenruo } 16274b82d6e4SYan 162805dbe683SOmar Sandoval return dget(s->s_root); 16294b82d6e4SYan 1630c146afadSYan Zheng error_close_devices: 16318a4b83ccSChris Mason btrfs_close_devices(fs_devices); 163204d21a24SIlya Dryomov error_fs_info: 16336c41761fSDavid Sterba free_fs_info(fs_info); 1634f667aef6SQu Wenruo error_sec_opts: 1635f667aef6SQu Wenruo security_free_mnt_opts(&new_sec_opts); 1636061dbc6bSAl Viro return ERR_PTR(error); 16374b82d6e4SYan } 16382e635a27SChris Mason 16390d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 16400d2450abSSergei Trofimovich int new_pool_size, int old_pool_size) 16410d2450abSSergei Trofimovich { 16420d2450abSSergei Trofimovich if (new_pool_size == old_pool_size) 16430d2450abSSergei Trofimovich return; 16440d2450abSSergei Trofimovich 16450d2450abSSergei Trofimovich fs_info->thread_pool_size = new_pool_size; 16460d2450abSSergei Trofimovich 1647efe120a0SFrank Holton btrfs_info(fs_info, "resize thread pool %d -> %d", 16480d2450abSSergei Trofimovich old_pool_size, new_pool_size); 16490d2450abSSergei Trofimovich 16505cdc7ad3SQu Wenruo btrfs_workqueue_set_max(fs_info->workers, new_pool_size); 1651afe3d242SQu Wenruo btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size); 1652a8c93d4eSQu Wenruo btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size); 1653e66f0bb1SQu Wenruo btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size); 1654fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size); 1655fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size); 1656fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_write_workers, 1657fccb5d86SQu Wenruo new_pool_size); 1658fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size); 1659fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size); 16605b3bc44eSQu Wenruo btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size); 1661736cfa15SQu Wenruo btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size); 16620339ef2fSQu Wenruo btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers, 1663ff023aacSStefan Behrens new_pool_size); 16640d2450abSSergei Trofimovich } 16650d2450abSSergei Trofimovich 1666f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info) 1667dc81cdc5SMiao Xie { 1668dc81cdc5SMiao Xie set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1669f42a34b2SMiao Xie } 1670dc81cdc5SMiao Xie 1671f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info, 1672f42a34b2SMiao Xie unsigned long old_opts, int flags) 1673f42a34b2SMiao Xie { 1674dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1675dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1676dc81cdc5SMiao Xie (flags & MS_RDONLY))) { 1677dc81cdc5SMiao Xie /* wait for any defraggers to finish */ 1678dc81cdc5SMiao Xie wait_event(fs_info->transaction_wait, 1679dc81cdc5SMiao Xie (atomic_read(&fs_info->defrag_running) == 0)); 1680dc81cdc5SMiao Xie if (flags & MS_RDONLY) 1681dc81cdc5SMiao Xie sync_filesystem(fs_info->sb); 1682dc81cdc5SMiao Xie } 1683dc81cdc5SMiao Xie } 1684dc81cdc5SMiao Xie 1685dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info, 1686dc81cdc5SMiao Xie unsigned long old_opts) 1687dc81cdc5SMiao Xie { 1688dc81cdc5SMiao Xie /* 1689180e4d47SLuis de Bethencourt * We need to cleanup all defragable inodes if the autodefragment is 1690180e4d47SLuis de Bethencourt * close or the filesystem is read only. 1691dc81cdc5SMiao Xie */ 1692dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1693dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1694dc81cdc5SMiao Xie (fs_info->sb->s_flags & MS_RDONLY))) { 1695dc81cdc5SMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 1696dc81cdc5SMiao Xie } 1697dc81cdc5SMiao Xie 1698dc81cdc5SMiao Xie clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1699dc81cdc5SMiao Xie } 1700dc81cdc5SMiao Xie 1701c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1702c146afadSYan Zheng { 1703815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1704815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 170549b25e05SJeff Mahoney unsigned old_flags = sb->s_flags; 170649b25e05SJeff Mahoney unsigned long old_opts = fs_info->mount_opt; 170749b25e05SJeff Mahoney unsigned long old_compress_type = fs_info->compress_type; 170849b25e05SJeff Mahoney u64 old_max_inline = fs_info->max_inline; 170949b25e05SJeff Mahoney u64 old_alloc_start = fs_info->alloc_start; 171049b25e05SJeff Mahoney int old_thread_pool_size = fs_info->thread_pool_size; 171149b25e05SJeff Mahoney unsigned int old_metadata_ratio = fs_info->metadata_ratio; 1712c146afadSYan Zheng int ret; 1713c146afadSYan Zheng 171402b9984dSTheodore Ts'o sync_filesystem(sb); 1715f42a34b2SMiao Xie btrfs_remount_prepare(fs_info); 1716dc81cdc5SMiao Xie 1717f667aef6SQu Wenruo if (data) { 1718f667aef6SQu Wenruo struct security_mnt_opts new_sec_opts; 1719f667aef6SQu Wenruo 1720f667aef6SQu Wenruo security_init_mnt_opts(&new_sec_opts); 1721f667aef6SQu Wenruo ret = parse_security_options(data, &new_sec_opts); 1722f667aef6SQu Wenruo if (ret) 1723f667aef6SQu Wenruo goto restore; 1724f667aef6SQu Wenruo ret = setup_security_options(fs_info, sb, 1725f667aef6SQu Wenruo &new_sec_opts); 1726f667aef6SQu Wenruo if (ret) { 1727f667aef6SQu Wenruo security_free_mnt_opts(&new_sec_opts); 1728f667aef6SQu Wenruo goto restore; 1729f667aef6SQu Wenruo } 1730f667aef6SQu Wenruo } 1731f667aef6SQu Wenruo 17322ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, data, *flags); 173349b25e05SJeff Mahoney if (ret) { 173449b25e05SJeff Mahoney ret = -EINVAL; 173549b25e05SJeff Mahoney goto restore; 173649b25e05SJeff Mahoney } 1737b288052eSChris Mason 1738f42a34b2SMiao Xie btrfs_remount_begin(fs_info, old_opts, *flags); 17390d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 17400d2450abSSergei Trofimovich fs_info->thread_pool_size, old_thread_pool_size); 17410d2450abSSergei Trofimovich 1742c146afadSYan Zheng if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 1743dc81cdc5SMiao Xie goto out; 1744c146afadSYan Zheng 1745c146afadSYan Zheng if (*flags & MS_RDONLY) { 17468dabb742SStefan Behrens /* 17478dabb742SStefan Behrens * this also happens on 'umount -rf' or on shutdown, when 17488dabb742SStefan Behrens * the filesystem is busy. 17498dabb742SStefan Behrens */ 175021c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 1751361c093dSStefan Behrens 1752361c093dSStefan Behrens /* wait for the uuid_scan task to finish */ 1753361c093dSStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 1754361c093dSStefan Behrens /* avoid complains from lockdep et al. */ 1755361c093dSStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 1756361c093dSStefan Behrens 1757c146afadSYan Zheng sb->s_flags |= MS_RDONLY; 1758c146afadSYan Zheng 1759e44163e1SJeff Mahoney /* 1760e44163e1SJeff Mahoney * Setting MS_RDONLY will put the cleaner thread to 1761e44163e1SJeff Mahoney * sleep at the next loop if it's already active. 1762e44163e1SJeff Mahoney * If it's already asleep, we'll leave unused block 1763e44163e1SJeff Mahoney * groups on disk until we're mounted read-write again 1764e44163e1SJeff Mahoney * unless we clean them up here. 1765e44163e1SJeff Mahoney */ 1766e44163e1SJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1767e44163e1SJeff Mahoney 17688dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 17698dabb742SStefan Behrens btrfs_scrub_cancel(fs_info); 1770061594efSMiao Xie btrfs_pause_balance(fs_info); 17718dabb742SStefan Behrens 17726bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 177349b25e05SJeff Mahoney if (ret) 177449b25e05SJeff Mahoney goto restore; 1775c146afadSYan Zheng } else { 17760b246afaSJeff Mahoney if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 17776ef3de9cSDavid Sterba btrfs_err(fs_info, 1778efe120a0SFrank Holton "Remounting read-write after error is not allowed"); 17796ef3de9cSDavid Sterba ret = -EINVAL; 17806ef3de9cSDavid Sterba goto restore; 17816ef3de9cSDavid Sterba } 17828a3db184SSergei Trofimovich if (fs_info->fs_devices->rw_devices == 0) { 178349b25e05SJeff Mahoney ret = -EACCES; 178449b25e05SJeff Mahoney goto restore; 17858a3db184SSergei Trofimovich } 17862b82032cSYan Zheng 1787292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 1788292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 1789292fd7fcSStefan Behrens !(*flags & MS_RDONLY)) { 1790efe120a0SFrank Holton btrfs_warn(fs_info, 1791efe120a0SFrank Holton "too many missing devices, writeable remount is not allowed"); 1792292fd7fcSStefan Behrens ret = -EACCES; 1793292fd7fcSStefan Behrens goto restore; 1794292fd7fcSStefan Behrens } 1795292fd7fcSStefan Behrens 17968a3db184SSergei Trofimovich if (btrfs_super_log_root(fs_info->super_copy) != 0) { 179749b25e05SJeff Mahoney ret = -EINVAL; 179849b25e05SJeff Mahoney goto restore; 17998a3db184SSergei Trofimovich } 1800c146afadSYan Zheng 1801815745cfSAl Viro ret = btrfs_cleanup_fs_roots(fs_info); 180249b25e05SJeff Mahoney if (ret) 180349b25e05SJeff Mahoney goto restore; 1804c146afadSYan Zheng 1805d68fc57bSYan, Zheng /* recover relocation */ 18065f316481SWang Shilong mutex_lock(&fs_info->cleaner_mutex); 1807d68fc57bSYan, Zheng ret = btrfs_recover_relocation(root); 18085f316481SWang Shilong mutex_unlock(&fs_info->cleaner_mutex); 180949b25e05SJeff Mahoney if (ret) 181049b25e05SJeff Mahoney goto restore; 1811c146afadSYan Zheng 18122b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 18132b6ba629SIlya Dryomov if (ret) 18142b6ba629SIlya Dryomov goto restore; 18152b6ba629SIlya Dryomov 18168dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 18178dabb742SStefan Behrens if (ret) { 1818efe120a0SFrank Holton btrfs_warn(fs_info, "failed to resume dev_replace"); 18198dabb742SStefan Behrens goto restore; 18208dabb742SStefan Behrens } 182194aebfb2SJosef Bacik 182294aebfb2SJosef Bacik if (!fs_info->uuid_root) { 1823efe120a0SFrank Holton btrfs_info(fs_info, "creating UUID tree"); 182494aebfb2SJosef Bacik ret = btrfs_create_uuid_tree(fs_info); 182594aebfb2SJosef Bacik if (ret) { 18265d163e0eSJeff Mahoney btrfs_warn(fs_info, 18275d163e0eSJeff Mahoney "failed to create the UUID tree %d", 18285d163e0eSJeff Mahoney ret); 182994aebfb2SJosef Bacik goto restore; 183094aebfb2SJosef Bacik } 183194aebfb2SJosef Bacik } 1832c146afadSYan Zheng sb->s_flags &= ~MS_RDONLY; 183390c711abSZygo Blaxell 1834afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 1835c146afadSYan Zheng } 1836dc81cdc5SMiao Xie out: 18372c6a92b0SJustin Maggard wake_up_process(fs_info->transaction_kthread); 1838dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 1839c146afadSYan Zheng return 0; 184049b25e05SJeff Mahoney 184149b25e05SJeff Mahoney restore: 184249b25e05SJeff Mahoney /* We've hit an error - don't reset MS_RDONLY */ 184349b25e05SJeff Mahoney if (sb->s_flags & MS_RDONLY) 184449b25e05SJeff Mahoney old_flags |= MS_RDONLY; 184549b25e05SJeff Mahoney sb->s_flags = old_flags; 184649b25e05SJeff Mahoney fs_info->mount_opt = old_opts; 184749b25e05SJeff Mahoney fs_info->compress_type = old_compress_type; 184849b25e05SJeff Mahoney fs_info->max_inline = old_max_inline; 1849c018daecSMiao Xie mutex_lock(&fs_info->chunk_mutex); 185049b25e05SJeff Mahoney fs_info->alloc_start = old_alloc_start; 1851c018daecSMiao Xie mutex_unlock(&fs_info->chunk_mutex); 18520d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 18530d2450abSSergei Trofimovich old_thread_pool_size, fs_info->thread_pool_size); 185449b25e05SJeff Mahoney fs_info->metadata_ratio = old_metadata_ratio; 1855dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 185649b25e05SJeff Mahoney return ret; 1857c146afadSYan Zheng } 1858c146afadSYan Zheng 1859bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 1860bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1, 1861bcd53741SArne Jansen const void *dev_info2) 1862bcd53741SArne Jansen { 1863bcd53741SArne Jansen if (((struct btrfs_device_info *)dev_info1)->max_avail > 1864bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1865bcd53741SArne Jansen return -1; 1866bcd53741SArne Jansen else if (((struct btrfs_device_info *)dev_info1)->max_avail < 1867bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1868bcd53741SArne Jansen return 1; 1869bcd53741SArne Jansen else 1870bcd53741SArne Jansen return 0; 1871bcd53741SArne Jansen } 1872bcd53741SArne Jansen 1873bcd53741SArne Jansen /* 1874bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 1875bcd53741SArne Jansen * is stored.(Descending Sort) 1876bcd53741SArne Jansen */ 1877bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 1878bcd53741SArne Jansen struct btrfs_device_info *devices, 1879bcd53741SArne Jansen size_t nr_devices) 1880bcd53741SArne Jansen { 1881bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 1882bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 1883bcd53741SArne Jansen } 1884bcd53741SArne Jansen 18856d07bcecSMiao Xie /* 18866d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 18876d07bcecSMiao Xie * file data. 18886d07bcecSMiao Xie */ 18896bccf3abSJeff Mahoney static int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info, 18906bccf3abSJeff Mahoney u64 *free_bytes) 18916d07bcecSMiao Xie { 18926bccf3abSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 18936d07bcecSMiao Xie struct btrfs_device_info *devices_info; 18946d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 18956d07bcecSMiao Xie struct btrfs_device *device; 18966d07bcecSMiao Xie u64 skip_space; 18976d07bcecSMiao Xie u64 type; 18986d07bcecSMiao Xie u64 avail_space; 18996d07bcecSMiao Xie u64 used_space; 19006d07bcecSMiao Xie u64 min_stripe_size; 190139fb26c3SMiao Xie int min_stripes = 1, num_stripes = 1; 19026d07bcecSMiao Xie int i = 0, nr_devices; 19036d07bcecSMiao Xie int ret; 19046d07bcecSMiao Xie 19057e33fd99SJosef Bacik /* 190601327610SNicholas D Steeves * We aren't under the device list lock, so this is racy-ish, but good 19077e33fd99SJosef Bacik * enough for our purposes. 19087e33fd99SJosef Bacik */ 1909b772a86eSLi Zefan nr_devices = fs_info->fs_devices->open_devices; 19107e33fd99SJosef Bacik if (!nr_devices) { 19117e33fd99SJosef Bacik smp_mb(); 19127e33fd99SJosef Bacik nr_devices = fs_info->fs_devices->open_devices; 19137e33fd99SJosef Bacik ASSERT(nr_devices); 19147e33fd99SJosef Bacik if (!nr_devices) { 19157e33fd99SJosef Bacik *free_bytes = 0; 19167e33fd99SJosef Bacik return 0; 19177e33fd99SJosef Bacik } 19187e33fd99SJosef Bacik } 19196d07bcecSMiao Xie 1920d9b0d9baSDulshani Gunawardhana devices_info = kmalloc_array(nr_devices, sizeof(*devices_info), 19216d07bcecSMiao Xie GFP_NOFS); 19226d07bcecSMiao Xie if (!devices_info) 19236d07bcecSMiao Xie return -ENOMEM; 19246d07bcecSMiao Xie 192501327610SNicholas D Steeves /* calc min stripe number for data space allocation */ 19266d07bcecSMiao Xie type = btrfs_get_alloc_profile(root, 1); 192739fb26c3SMiao Xie if (type & BTRFS_BLOCK_GROUP_RAID0) { 19286d07bcecSMiao Xie min_stripes = 2; 192939fb26c3SMiao Xie num_stripes = nr_devices; 193039fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID1) { 19316d07bcecSMiao Xie min_stripes = 2; 193239fb26c3SMiao Xie num_stripes = 2; 193339fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID10) { 19346d07bcecSMiao Xie min_stripes = 4; 193539fb26c3SMiao Xie num_stripes = 4; 193639fb26c3SMiao Xie } 19376d07bcecSMiao Xie 19386d07bcecSMiao Xie if (type & BTRFS_BLOCK_GROUP_DUP) 19396d07bcecSMiao Xie min_stripe_size = 2 * BTRFS_STRIPE_LEN; 19406d07bcecSMiao Xie else 19416d07bcecSMiao Xie min_stripe_size = BTRFS_STRIPE_LEN; 19426d07bcecSMiao Xie 19437e33fd99SJosef Bacik if (fs_info->alloc_start) 19447e33fd99SJosef Bacik mutex_lock(&fs_devices->device_list_mutex); 19457e33fd99SJosef Bacik rcu_read_lock(); 19467e33fd99SJosef Bacik list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 194763a212abSStefan Behrens if (!device->in_fs_metadata || !device->bdev || 194863a212abSStefan Behrens device->is_tgtdev_for_dev_replace) 19496d07bcecSMiao Xie continue; 19506d07bcecSMiao Xie 19517e33fd99SJosef Bacik if (i >= nr_devices) 19527e33fd99SJosef Bacik break; 19537e33fd99SJosef Bacik 19546d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 19556d07bcecSMiao Xie 19566d07bcecSMiao Xie /* align with stripe_len */ 1957f8c269d7SDavid Sterba avail_space = div_u64(avail_space, BTRFS_STRIPE_LEN); 19586d07bcecSMiao Xie avail_space *= BTRFS_STRIPE_LEN; 19596d07bcecSMiao Xie 19606d07bcecSMiao Xie /* 196101327610SNicholas D Steeves * In order to avoid overwriting the superblock on the drive, 19626d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 19636d07bcecSMiao Xie * allocation. 19646d07bcecSMiao Xie */ 1965ee22184bSByongho Lee skip_space = SZ_1M; 19666d07bcecSMiao Xie 19676d07bcecSMiao Xie /* user can set the offset in fs_info->alloc_start. */ 19687e33fd99SJosef Bacik if (fs_info->alloc_start && 19697e33fd99SJosef Bacik fs_info->alloc_start + BTRFS_STRIPE_LEN <= 19707e33fd99SJosef Bacik device->total_bytes) { 19717e33fd99SJosef Bacik rcu_read_unlock(); 19726d07bcecSMiao Xie skip_space = max(fs_info->alloc_start, skip_space); 19736d07bcecSMiao Xie 19746d07bcecSMiao Xie /* 19757e33fd99SJosef Bacik * btrfs can not use the free space in 19767e33fd99SJosef Bacik * [0, skip_space - 1], we must subtract it from the 19777e33fd99SJosef Bacik * total. In order to implement it, we account the used 19787e33fd99SJosef Bacik * space in this range first. 19796d07bcecSMiao Xie */ 19807e33fd99SJosef Bacik ret = btrfs_account_dev_extents_size(device, 0, 19817e33fd99SJosef Bacik skip_space - 1, 19826d07bcecSMiao Xie &used_space); 19836d07bcecSMiao Xie if (ret) { 19846d07bcecSMiao Xie kfree(devices_info); 19857e33fd99SJosef Bacik mutex_unlock(&fs_devices->device_list_mutex); 19866d07bcecSMiao Xie return ret; 19876d07bcecSMiao Xie } 19886d07bcecSMiao Xie 19897e33fd99SJosef Bacik rcu_read_lock(); 19907e33fd99SJosef Bacik 19916d07bcecSMiao Xie /* calc the free space in [0, skip_space - 1] */ 19926d07bcecSMiao Xie skip_space -= used_space; 19937e33fd99SJosef Bacik } 19946d07bcecSMiao Xie 19956d07bcecSMiao Xie /* 19966d07bcecSMiao Xie * we can use the free space in [0, skip_space - 1], subtract 19976d07bcecSMiao Xie * it from the total. 19986d07bcecSMiao Xie */ 19996d07bcecSMiao Xie if (avail_space && avail_space >= skip_space) 20006d07bcecSMiao Xie avail_space -= skip_space; 20016d07bcecSMiao Xie else 20026d07bcecSMiao Xie avail_space = 0; 20036d07bcecSMiao Xie 20046d07bcecSMiao Xie if (avail_space < min_stripe_size) 20056d07bcecSMiao Xie continue; 20066d07bcecSMiao Xie 20076d07bcecSMiao Xie devices_info[i].dev = device; 20086d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 20096d07bcecSMiao Xie 20106d07bcecSMiao Xie i++; 20116d07bcecSMiao Xie } 20127e33fd99SJosef Bacik rcu_read_unlock(); 20137e33fd99SJosef Bacik if (fs_info->alloc_start) 20147e33fd99SJosef Bacik mutex_unlock(&fs_devices->device_list_mutex); 20156d07bcecSMiao Xie 20166d07bcecSMiao Xie nr_devices = i; 20176d07bcecSMiao Xie 20186d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 20196d07bcecSMiao Xie 20206d07bcecSMiao Xie i = nr_devices - 1; 20216d07bcecSMiao Xie avail_space = 0; 20226d07bcecSMiao Xie while (nr_devices >= min_stripes) { 202339fb26c3SMiao Xie if (num_stripes > nr_devices) 202439fb26c3SMiao Xie num_stripes = nr_devices; 202539fb26c3SMiao Xie 20266d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 20276d07bcecSMiao Xie int j; 20286d07bcecSMiao Xie u64 alloc_size; 20296d07bcecSMiao Xie 203039fb26c3SMiao Xie avail_space += devices_info[i].max_avail * num_stripes; 20316d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 203239fb26c3SMiao Xie for (j = i + 1 - num_stripes; j <= i; j++) 20336d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 20346d07bcecSMiao Xie } 20356d07bcecSMiao Xie i--; 20366d07bcecSMiao Xie nr_devices--; 20376d07bcecSMiao Xie } 20386d07bcecSMiao Xie 20396d07bcecSMiao Xie kfree(devices_info); 20406d07bcecSMiao Xie *free_bytes = avail_space; 20416d07bcecSMiao Xie return 0; 20426d07bcecSMiao Xie } 20436d07bcecSMiao Xie 2044ba7b6e62SDavid Sterba /* 2045ba7b6e62SDavid Sterba * Calculate numbers for 'df', pessimistic in case of mixed raid profiles. 2046ba7b6e62SDavid Sterba * 2047ba7b6e62SDavid Sterba * If there's a redundant raid level at DATA block groups, use the respective 2048ba7b6e62SDavid Sterba * multiplier to scale the sizes. 2049ba7b6e62SDavid Sterba * 2050ba7b6e62SDavid Sterba * Unused device space usage is based on simulating the chunk allocator 2051ba7b6e62SDavid Sterba * algorithm that respects the device sizes, order of allocations and the 2052ba7b6e62SDavid Sterba * 'alloc_start' value, this is a close approximation of the actual use but 2053ba7b6e62SDavid Sterba * there are other factors that may change the result (like a new metadata 2054ba7b6e62SDavid Sterba * chunk). 2055ba7b6e62SDavid Sterba * 2056ca8a51b3SDavid Sterba * If metadata is exhausted, f_bavail will be 0. 2057ba7b6e62SDavid Sterba */ 20588fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 20598fd17795SChris Mason { 2060815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 2061815745cfSAl Viro struct btrfs_super_block *disk_super = fs_info->super_copy; 2062815745cfSAl Viro struct list_head *head = &fs_info->space_info; 2063bd4d1088SJosef Bacik struct btrfs_space_info *found; 2064bd4d1088SJosef Bacik u64 total_used = 0; 20656d07bcecSMiao Xie u64 total_free_data = 0; 2066ca8a51b3SDavid Sterba u64 total_free_meta = 0; 2067db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 2068815745cfSAl Viro __be32 *fsid = (__be32 *)fs_info->fsid; 2069ba7b6e62SDavid Sterba unsigned factor = 1; 2070ba7b6e62SDavid Sterba struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 20716d07bcecSMiao Xie int ret; 2072ca8a51b3SDavid Sterba u64 thresh = 0; 2073ae02d1bdSLuis de Bethencourt int mixed = 0; 20748fd17795SChris Mason 2075bd4d1088SJosef Bacik rcu_read_lock(); 207689a55897SJosef Bacik list_for_each_entry_rcu(found, head, list) { 20776d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 2078ba7b6e62SDavid Sterba int i; 2079ba7b6e62SDavid Sterba 20806d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 20816d07bcecSMiao Xie total_free_data -= 20826d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 2083ba7b6e62SDavid Sterba 2084ba7b6e62SDavid Sterba for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { 2085ba7b6e62SDavid Sterba if (!list_empty(&found->block_groups[i])) { 2086ba7b6e62SDavid Sterba switch (i) { 2087ba7b6e62SDavid Sterba case BTRFS_RAID_DUP: 2088ba7b6e62SDavid Sterba case BTRFS_RAID_RAID1: 2089ba7b6e62SDavid Sterba case BTRFS_RAID_RAID10: 2090ba7b6e62SDavid Sterba factor = 2; 2091ba7b6e62SDavid Sterba } 2092ba7b6e62SDavid Sterba } 2093ba7b6e62SDavid Sterba } 20946d07bcecSMiao Xie } 2095ae02d1bdSLuis de Bethencourt 2096ae02d1bdSLuis de Bethencourt /* 2097ae02d1bdSLuis de Bethencourt * Metadata in mixed block goup profiles are accounted in data 2098ae02d1bdSLuis de Bethencourt */ 2099ae02d1bdSLuis de Bethencourt if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) { 2100ae02d1bdSLuis de Bethencourt if (found->flags & BTRFS_BLOCK_GROUP_DATA) 2101ae02d1bdSLuis de Bethencourt mixed = 1; 2102ae02d1bdSLuis de Bethencourt else 2103ae02d1bdSLuis de Bethencourt total_free_meta += found->disk_total - 2104ae02d1bdSLuis de Bethencourt found->disk_used; 2105ae02d1bdSLuis de Bethencourt } 21066d07bcecSMiao Xie 2107b742bb82SYan, Zheng total_used += found->disk_used; 210889a55897SJosef Bacik } 2109ba7b6e62SDavid Sterba 2110bd4d1088SJosef Bacik rcu_read_unlock(); 2111bd4d1088SJosef Bacik 2112ba7b6e62SDavid Sterba buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor); 2113ba7b6e62SDavid Sterba buf->f_blocks >>= bits; 2114ba7b6e62SDavid Sterba buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits); 2115ba7b6e62SDavid Sterba 2116ba7b6e62SDavid Sterba /* Account global block reserve as used, it's in logical size already */ 2117ba7b6e62SDavid Sterba spin_lock(&block_rsv->lock); 211841b34accSLuis de Bethencourt /* Mixed block groups accounting is not byte-accurate, avoid overflow */ 211941b34accSLuis de Bethencourt if (buf->f_bfree >= block_rsv->size >> bits) 2120ba7b6e62SDavid Sterba buf->f_bfree -= block_rsv->size >> bits; 212141b34accSLuis de Bethencourt else 212241b34accSLuis de Bethencourt buf->f_bfree = 0; 2123ba7b6e62SDavid Sterba spin_unlock(&block_rsv->lock); 2124ba7b6e62SDavid Sterba 21250d95c1beSDavid Sterba buf->f_bavail = div_u64(total_free_data, factor); 21266bccf3abSJeff Mahoney ret = btrfs_calc_avail_data_space(fs_info, &total_free_data); 21277e33fd99SJosef Bacik if (ret) 21286d07bcecSMiao Xie return ret; 2129ba7b6e62SDavid Sterba buf->f_bavail += div_u64(total_free_data, factor); 21306d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 2131d397712bSChris Mason 2132ca8a51b3SDavid Sterba /* 2133ca8a51b3SDavid Sterba * We calculate the remaining metadata space minus global reserve. If 2134ca8a51b3SDavid Sterba * this is (supposedly) smaller than zero, there's no space. But this 2135ca8a51b3SDavid Sterba * does not hold in practice, the exhausted state happens where's still 2136ca8a51b3SDavid Sterba * some positive delta. So we apply some guesswork and compare the 2137ca8a51b3SDavid Sterba * delta to a 4M threshold. (Practically observed delta was ~2M.) 2138ca8a51b3SDavid Sterba * 2139ca8a51b3SDavid Sterba * We probably cannot calculate the exact threshold value because this 2140ca8a51b3SDavid Sterba * depends on the internal reservations requested by various 2141ca8a51b3SDavid Sterba * operations, so some operations that consume a few metadata will 2142ca8a51b3SDavid Sterba * succeed even if the Avail is zero. But this is better than the other 2143ca8a51b3SDavid Sterba * way around. 2144ca8a51b3SDavid Sterba */ 2145ca8a51b3SDavid Sterba thresh = 4 * 1024 * 1024; 2146ca8a51b3SDavid Sterba 2147ae02d1bdSLuis de Bethencourt if (!mixed && total_free_meta - thresh < block_rsv->size) 2148ca8a51b3SDavid Sterba buf->f_bavail = 0; 2149ca8a51b3SDavid Sterba 2150ba7b6e62SDavid Sterba buf->f_type = BTRFS_SUPER_MAGIC; 2151ba7b6e62SDavid Sterba buf->f_bsize = dentry->d_sb->s_blocksize; 2152ba7b6e62SDavid Sterba buf->f_namelen = BTRFS_NAME_LEN; 2153ba7b6e62SDavid Sterba 21549d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 21559d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 21569d03632eSDavid Woodhouse on a big-endian or little-endian host */ 21579d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 21589d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 215932d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 21602b0143b5SDavid Howells buf->f_fsid.val[0] ^= BTRFS_I(d_inode(dentry))->root->objectid >> 32; 21612b0143b5SDavid Howells buf->f_fsid.val[1] ^= BTRFS_I(d_inode(dentry))->root->objectid; 216232d48fa1SDavid Woodhouse 21638fd17795SChris Mason return 0; 21648fd17795SChris Mason } 2165b5133862SChris Mason 2166aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb) 2167aea52e19SAl Viro { 2168815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2169aea52e19SAl Viro kill_anon_super(sb); 2170aea52e19SAl Viro free_fs_info(fs_info); 2171aea52e19SAl Viro } 2172aea52e19SAl Viro 21732e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 21742e635a27SChris Mason .owner = THIS_MODULE, 21752e635a27SChris Mason .name = "btrfs", 2176061dbc6bSAl Viro .mount = btrfs_mount, 2177aea52e19SAl Viro .kill_sb = btrfs_kill_super, 2178f667aef6SQu Wenruo .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA, 21792e635a27SChris Mason }; 21807f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs"); 2181a9218f6bSChris Mason 2182d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file) 2183d8620958STom Van Braeckel { 2184d8620958STom Van Braeckel /* 2185d8620958STom Van Braeckel * The control file's private_data is used to hold the 2186d8620958STom Van Braeckel * transaction when it is started and is used to keep 2187d8620958STom Van Braeckel * track of whether a transaction is already in progress. 2188d8620958STom Van Braeckel */ 2189d8620958STom Van Braeckel file->private_data = NULL; 2190d8620958STom Van Braeckel return 0; 2191d8620958STom Van Braeckel } 2192d8620958STom Van Braeckel 2193d352ac68SChris Mason /* 2194d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 2195d352ac68SChris Mason */ 21968a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 21978a4b83ccSChris Mason unsigned long arg) 21988a4b83ccSChris Mason { 21998a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 22008a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 2201c071fcfdSChris Mason int ret = -ENOTTY; 22028a4b83ccSChris Mason 2203e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2204e441d54dSChris Mason return -EPERM; 2205e441d54dSChris Mason 2206dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 2207dae7b665SLi Zefan if (IS_ERR(vol)) 2208dae7b665SLi Zefan return PTR_ERR(vol); 2209c071fcfdSChris Mason 22108a4b83ccSChris Mason switch (cmd) { 22118a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 221297288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 22138a4b83ccSChris Mason &btrfs_fs_type, &fs_devices); 22148a4b83ccSChris Mason break; 221502db0844SJosef Bacik case BTRFS_IOC_DEVICES_READY: 221602db0844SJosef Bacik ret = btrfs_scan_one_device(vol->name, FMODE_READ, 221702db0844SJosef Bacik &btrfs_fs_type, &fs_devices); 221802db0844SJosef Bacik if (ret) 221902db0844SJosef Bacik break; 222002db0844SJosef Bacik ret = !(fs_devices->num_devices == fs_devices->total_devices); 222102db0844SJosef Bacik break; 2222c5868f83SDavid Sterba case BTRFS_IOC_GET_SUPPORTED_FEATURES: 2223d5131b65SDavid Sterba ret = btrfs_ioctl_get_supported_features((void __user*)arg); 2224c5868f83SDavid Sterba break; 22258a4b83ccSChris Mason } 2226dae7b665SLi Zefan 22278a4b83ccSChris Mason kfree(vol); 2228f819d837SLinda Knippers return ret; 22298a4b83ccSChris Mason } 22308a4b83ccSChris Mason 22310176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 2232ed0dab6bSYan { 2233354aa0fbSMiao Xie struct btrfs_trans_handle *trans; 22340b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(sb); 22350b246afaSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 2236354aa0fbSMiao Xie 22370b246afaSJeff Mahoney fs_info->fs_frozen = 1; 22389e7cc91aSWang Xiaoguang /* 22399e7cc91aSWang Xiaoguang * We don't need a barrier here, we'll wait for any transaction that 22409e7cc91aSWang Xiaoguang * could be in progress on other threads (and do delayed iputs that 22419e7cc91aSWang Xiaoguang * we want to avoid on a frozen filesystem), or do the commit 22429e7cc91aSWang Xiaoguang * ourselves. 22439e7cc91aSWang Xiaoguang */ 2244d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 2245354aa0fbSMiao Xie if (IS_ERR(trans)) { 2246354aa0fbSMiao Xie /* no transaction, don't bother */ 2247354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 22480176260fSLinus Torvalds return 0; 2249354aa0fbSMiao Xie return PTR_ERR(trans); 2250354aa0fbSMiao Xie } 22513a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 2252ed0dab6bSYan } 2253ed0dab6bSYan 22549e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb) 22559e7cc91aSWang Xiaoguang { 22560b246afaSJeff Mahoney btrfs_sb(sb)->fs_frozen = 0; 22579e7cc91aSWang Xiaoguang return 0; 22589e7cc91aSWang Xiaoguang } 22599e7cc91aSWang Xiaoguang 22609c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 22619c5085c1SJosef Bacik { 22629c5085c1SJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 22639c5085c1SJosef Bacik struct btrfs_fs_devices *cur_devices; 22649c5085c1SJosef Bacik struct btrfs_device *dev, *first_dev = NULL; 22659c5085c1SJosef Bacik struct list_head *head; 22669c5085c1SJosef Bacik struct rcu_string *name; 22679c5085c1SJosef Bacik 22689c5085c1SJosef Bacik mutex_lock(&fs_info->fs_devices->device_list_mutex); 22699c5085c1SJosef Bacik cur_devices = fs_info->fs_devices; 22709c5085c1SJosef Bacik while (cur_devices) { 22719c5085c1SJosef Bacik head = &cur_devices->devices; 22729c5085c1SJosef Bacik list_for_each_entry(dev, head, dev_list) { 2273aa9ddcd4SJosef Bacik if (dev->missing) 2274aa9ddcd4SJosef Bacik continue; 22750aeb8a6eSAnand Jain if (!dev->name) 22760aeb8a6eSAnand Jain continue; 22779c5085c1SJosef Bacik if (!first_dev || dev->devid < first_dev->devid) 22789c5085c1SJosef Bacik first_dev = dev; 22799c5085c1SJosef Bacik } 22809c5085c1SJosef Bacik cur_devices = cur_devices->seed; 22819c5085c1SJosef Bacik } 22829c5085c1SJosef Bacik 22839c5085c1SJosef Bacik if (first_dev) { 22849c5085c1SJosef Bacik rcu_read_lock(); 22859c5085c1SJosef Bacik name = rcu_dereference(first_dev->name); 22869c5085c1SJosef Bacik seq_escape(m, name->str, " \t\n\\"); 22879c5085c1SJosef Bacik rcu_read_unlock(); 22889c5085c1SJosef Bacik } else { 22899c5085c1SJosef Bacik WARN_ON(1); 22909c5085c1SJosef Bacik } 22919c5085c1SJosef Bacik mutex_unlock(&fs_info->fs_devices->device_list_mutex); 22929c5085c1SJosef Bacik return 0; 22939c5085c1SJosef Bacik } 22949c5085c1SJosef Bacik 2295b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 229676dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 2297bd555975SAl Viro .evict_inode = btrfs_evict_inode, 2298e20d96d6SChris Mason .put_super = btrfs_put_super, 2299d5719762SChris Mason .sync_fs = btrfs_sync_fs, 2300a9572a15SEric Paris .show_options = btrfs_show_options, 23019c5085c1SJosef Bacik .show_devname = btrfs_show_devname, 23024730a4bcSChris Mason .write_inode = btrfs_write_inode, 23032c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 23042c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 23058fd17795SChris Mason .statfs = btrfs_statfs, 2306c146afadSYan Zheng .remount_fs = btrfs_remount, 23070176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 23089e7cc91aSWang Xiaoguang .unfreeze_fs = btrfs_unfreeze, 2309e20d96d6SChris Mason }; 2310a9218f6bSChris Mason 2311a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 2312d8620958STom Van Braeckel .open = btrfs_control_open, 2313a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 2314a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 2315a9218f6bSChris Mason .owner = THIS_MODULE, 23166038f373SArnd Bergmann .llseek = noop_llseek, 2317a9218f6bSChris Mason }; 2318a9218f6bSChris Mason 2319a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 2320578454ffSKay Sievers .minor = BTRFS_MINOR, 2321a9218f6bSChris Mason .name = "btrfs-control", 2322a9218f6bSChris Mason .fops = &btrfs_ctl_fops 2323a9218f6bSChris Mason }; 2324a9218f6bSChris Mason 2325578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 2326578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 2327578454ffSKay Sievers 2328a9218f6bSChris Mason static int btrfs_interface_init(void) 2329a9218f6bSChris Mason { 2330a9218f6bSChris Mason return misc_register(&btrfs_misc); 2331a9218f6bSChris Mason } 2332a9218f6bSChris Mason 2333b2950863SChristoph Hellwig static void btrfs_interface_exit(void) 2334a9218f6bSChris Mason { 2335f368ed60SGreg Kroah-Hartman misc_deregister(&btrfs_misc); 2336a9218f6bSChris Mason } 2337a9218f6bSChris Mason 23388ae1af3cSAnand Jain static void btrfs_print_mod_info(void) 233985965600SDavid Sterba { 234062e85577SJeff Mahoney pr_info("Btrfs loaded, crc32c=%s" 234185965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 234285965600SDavid Sterba ", debug=on" 234385965600SDavid Sterba #endif 234479556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT 234579556c3dSStefan Behrens ", assert=on" 234679556c3dSStefan Behrens #endif 234785965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 234885965600SDavid Sterba ", integrity-checker=on" 234985965600SDavid Sterba #endif 23505f9e1059SJeff Mahoney "\n", 23515f9e1059SJeff Mahoney btrfs_crc32c_impl()); 235285965600SDavid Sterba } 235385965600SDavid Sterba 23542e635a27SChris Mason static int __init init_btrfs_fs(void) 23552e635a27SChris Mason { 23562c90e5d6SChris Mason int err; 235758176a96SJosef Bacik 235814a958e6SFilipe David Borba Manana err = btrfs_hash_init(); 235914a958e6SFilipe David Borba Manana if (err) 236014a958e6SFilipe David Borba Manana return err; 236114a958e6SFilipe David Borba Manana 236263541927SFilipe David Borba Manana btrfs_props_init(); 236363541927SFilipe David Borba Manana 236458176a96SJosef Bacik err = btrfs_init_sysfs(); 236558176a96SJosef Bacik if (err) 236614a958e6SFilipe David Borba Manana goto free_hash; 236758176a96SJosef Bacik 2368143bede5SJeff Mahoney btrfs_init_compress(); 2369d1310b2eSChris Mason 2370261507a0SLi Zefan err = btrfs_init_cachep(); 2371261507a0SLi Zefan if (err) 2372261507a0SLi Zefan goto free_compress; 2373261507a0SLi Zefan 2374d1310b2eSChris Mason err = extent_io_init(); 23752f4cbe64SWyatt Banks if (err) 23762f4cbe64SWyatt Banks goto free_cachep; 23772f4cbe64SWyatt Banks 2378d1310b2eSChris Mason err = extent_map_init(); 2379d1310b2eSChris Mason if (err) 2380d1310b2eSChris Mason goto free_extent_io; 2381d1310b2eSChris Mason 23826352b91dSMiao Xie err = ordered_data_init(); 23832f4cbe64SWyatt Banks if (err) 23842f4cbe64SWyatt Banks goto free_extent_map; 2385c8b97818SChris Mason 23866352b91dSMiao Xie err = btrfs_delayed_inode_init(); 23876352b91dSMiao Xie if (err) 23886352b91dSMiao Xie goto free_ordered_data; 23896352b91dSMiao Xie 23909247f317SMiao Xie err = btrfs_auto_defrag_init(); 239116cdcec7SMiao Xie if (err) 239216cdcec7SMiao Xie goto free_delayed_inode; 239316cdcec7SMiao Xie 239478a6184aSMiao Xie err = btrfs_delayed_ref_init(); 23959247f317SMiao Xie if (err) 23969247f317SMiao Xie goto free_auto_defrag; 23979247f317SMiao Xie 2398b9e9a6cbSWang Shilong err = btrfs_prelim_ref_init(); 2399b9e9a6cbSWang Shilong if (err) 2400af13b492SDavid Sterba goto free_delayed_ref; 2401b9e9a6cbSWang Shilong 240297eb6b69SDavid Sterba err = btrfs_end_io_wq_init(); 240378a6184aSMiao Xie if (err) 2404af13b492SDavid Sterba goto free_prelim_ref; 240578a6184aSMiao Xie 240697eb6b69SDavid Sterba err = btrfs_interface_init(); 240797eb6b69SDavid Sterba if (err) 240897eb6b69SDavid Sterba goto free_end_io_wq; 240997eb6b69SDavid Sterba 2410e565d4b9SJan Schmidt btrfs_init_lockdep(); 2411e565d4b9SJan Schmidt 24128ae1af3cSAnand Jain btrfs_print_mod_info(); 2413dc11dd5dSJosef Bacik 2414dc11dd5dSJosef Bacik err = btrfs_run_sanity_tests(); 2415dc11dd5dSJosef Bacik if (err) 2416dc11dd5dSJosef Bacik goto unregister_ioctl; 2417dc11dd5dSJosef Bacik 2418dc11dd5dSJosef Bacik err = register_filesystem(&btrfs_fs_type); 2419dc11dd5dSJosef Bacik if (err) 2420dc11dd5dSJosef Bacik goto unregister_ioctl; 242174255aa0SJosef Bacik 24222f4cbe64SWyatt Banks return 0; 24232f4cbe64SWyatt Banks 2424a9218f6bSChris Mason unregister_ioctl: 2425a9218f6bSChris Mason btrfs_interface_exit(); 242697eb6b69SDavid Sterba free_end_io_wq: 242797eb6b69SDavid Sterba btrfs_end_io_wq_exit(); 2428b9e9a6cbSWang Shilong free_prelim_ref: 2429b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 243078a6184aSMiao Xie free_delayed_ref: 243178a6184aSMiao Xie btrfs_delayed_ref_exit(); 24329247f317SMiao Xie free_auto_defrag: 24339247f317SMiao Xie btrfs_auto_defrag_exit(); 243416cdcec7SMiao Xie free_delayed_inode: 243516cdcec7SMiao Xie btrfs_delayed_inode_exit(); 24366352b91dSMiao Xie free_ordered_data: 24376352b91dSMiao Xie ordered_data_exit(); 24382f4cbe64SWyatt Banks free_extent_map: 24392f4cbe64SWyatt Banks extent_map_exit(); 2440d1310b2eSChris Mason free_extent_io: 2441d1310b2eSChris Mason extent_io_exit(); 24422f4cbe64SWyatt Banks free_cachep: 24432f4cbe64SWyatt Banks btrfs_destroy_cachep(); 2444261507a0SLi Zefan free_compress: 2445261507a0SLi Zefan btrfs_exit_compress(); 24462f4cbe64SWyatt Banks btrfs_exit_sysfs(); 244714a958e6SFilipe David Borba Manana free_hash: 244814a958e6SFilipe David Borba Manana btrfs_hash_exit(); 24492c90e5d6SChris Mason return err; 24502e635a27SChris Mason } 24512e635a27SChris Mason 24522e635a27SChris Mason static void __exit exit_btrfs_fs(void) 24532e635a27SChris Mason { 245439279cc3SChris Mason btrfs_destroy_cachep(); 245578a6184aSMiao Xie btrfs_delayed_ref_exit(); 24569247f317SMiao Xie btrfs_auto_defrag_exit(); 245716cdcec7SMiao Xie btrfs_delayed_inode_exit(); 2458b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 24596352b91dSMiao Xie ordered_data_exit(); 2460a52d9a80SChris Mason extent_map_exit(); 2461d1310b2eSChris Mason extent_io_exit(); 2462a9218f6bSChris Mason btrfs_interface_exit(); 24635ed5f588SJosef Bacik btrfs_end_io_wq_exit(); 24642e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 246558176a96SJosef Bacik btrfs_exit_sysfs(); 24668a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 2467261507a0SLi Zefan btrfs_exit_compress(); 246814a958e6SFilipe David Borba Manana btrfs_hash_exit(); 24692e635a27SChris Mason } 24702e635a27SChris Mason 247160efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs); 24722e635a27SChris Mason module_exit(exit_btrfs_fs) 24732e635a27SChris Mason 24742e635a27SChris Mason MODULE_LICENSE("GPL"); 2475