1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 26cbd5570SChris Mason /* 36cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 46cbd5570SChris Mason */ 56cbd5570SChris Mason 64b82d6e4SYan #include <linux/blkdev.h> 72e635a27SChris Mason #include <linux/module.h> 82e635a27SChris Mason #include <linux/fs.h> 92e635a27SChris Mason #include <linux/pagemap.h> 102e635a27SChris Mason #include <linux/highmem.h> 112e635a27SChris Mason #include <linux/time.h> 122e635a27SChris Mason #include <linux/init.h> 13a9572a15SEric Paris #include <linux/seq_file.h> 142e635a27SChris Mason #include <linux/string.h> 152e635a27SChris Mason #include <linux/backing-dev.h> 164b82d6e4SYan #include <linux/mount.h> 1775dfe396SChris Mason #include <linux/writeback.h> 188fd17795SChris Mason #include <linux/statfs.h> 1908607c1bSChris Mason #include <linux/compat.h> 2095e05289SChris Mason #include <linux/parser.h> 21c59f8951SChris Mason #include <linux/ctype.h> 226da6abaeSChris Mason #include <linux/namei.h> 23a9218f6bSChris Mason #include <linux/miscdevice.h> 241bcbf313SQinghuang Feng #include <linux/magic.h> 255a0e3ad6STejun Heo #include <linux/slab.h> 2690a887c9SDan Magenheimer #include <linux/cleancache.h> 2722c44fe6SJosef Bacik #include <linux/ratelimit.h> 289678c543SNikolay Borisov #include <linux/crc32c.h> 2955e301fdSFilipe Brandenburger #include <linux/btrfs.h> 3016cdcec7SMiao Xie #include "delayed-inode.h" 312e635a27SChris Mason #include "ctree.h" 32e20d96d6SChris Mason #include "disk-io.h" 33d5719762SChris Mason #include "transaction.h" 342c90e5d6SChris Mason #include "btrfs_inode.h" 353a686375SChris Mason #include "print-tree.h" 3663541927SFilipe David Borba Manana #include "props.h" 375103e947SJosef Bacik #include "xattr.h" 388a4b83ccSChris Mason #include "volumes.h" 39be6e8dc0SBalaji Rao #include "export.h" 40c8b97818SChris Mason #include "compression.h" 419c5085c1SJosef Bacik #include "rcu-string.h" 428dabb742SStefan Behrens #include "dev-replace.h" 4374255aa0SJosef Bacik #include "free-space-cache.h" 44b9e9a6cbSWang Shilong #include "backref.h" 458719aaaeSJosef Bacik #include "space-info.h" 4689439109SDavid Sterba #include "sysfs.h" 47b70f5097SNaohiro Aota #include "zoned.h" 48dc11dd5dSJosef Bacik #include "tests/btrfs-tests.h" 49aac0023cSJosef Bacik #include "block-group.h" 50b0643e59SDennis Zhou #include "discard.h" 51d3982100SMark Fasheh #include "qgroup.h" 521abe9b8aSliubo #define CREATE_TRACE_POINTS 531abe9b8aSliubo #include <trace/events/btrfs.h> 541abe9b8aSliubo 55b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops; 5672fa39f5SMisono, Tomohiro 5772fa39f5SMisono, Tomohiro /* 5872fa39f5SMisono, Tomohiro * Types for mounting the default subvolume and a subvolume explicitly 5972fa39f5SMisono, Tomohiro * requested by subvol=/path. That way the callchain is straightforward and we 6072fa39f5SMisono, Tomohiro * don't have to play tricks with the mount options and recursive calls to 6172fa39f5SMisono, Tomohiro * btrfs_mount. 62312c89fbSMisono, Tomohiro * 63312c89fbSMisono, Tomohiro * The new btrfs_root_fs_type also servers as a tag for the bdev_holder. 6472fa39f5SMisono, Tomohiro */ 65830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type; 6672fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type; 67e20d96d6SChris Mason 680723a047SHarald Hoyer static int btrfs_remount(struct super_block *sb, int *flags, char *data); 690723a047SHarald Hoyer 7059131393SJosef Bacik /* 7159131393SJosef Bacik * Generally the error codes correspond to their respective errors, but there 7259131393SJosef Bacik * are a few special cases. 7359131393SJosef Bacik * 7459131393SJosef Bacik * EUCLEAN: Any sort of corruption that we encounter. The tree-checker for 7559131393SJosef Bacik * instance will return EUCLEAN if any of the blocks are corrupted in 7659131393SJosef Bacik * a way that is problematic. We want to reserve EUCLEAN for these 7759131393SJosef Bacik * sort of corruptions. 7859131393SJosef Bacik * 7959131393SJosef Bacik * EROFS: If we check BTRFS_FS_STATE_ERROR and fail out with a return error, we 8059131393SJosef Bacik * need to use EROFS for this case. We will have no idea of the 8159131393SJosef Bacik * original failure, that will have been reported at the time we tripped 8259131393SJosef Bacik * over the error. Each subsequent error that doesn't have any context 8359131393SJosef Bacik * of the original error should use EROFS when handling BTRFS_FS_STATE_ERROR. 8459131393SJosef Bacik */ 854143cb8bSDavid Sterba const char * __attribute_const__ btrfs_decode_error(int errno) 86acce952bSliubo { 8708748810SDavid Sterba char *errstr = "unknown"; 88acce952bSliubo 89acce952bSliubo switch (errno) { 90d54f8144SDavid Sterba case -ENOENT: /* -2 */ 91d54f8144SDavid Sterba errstr = "No such entry"; 92d54f8144SDavid Sterba break; 93d54f8144SDavid Sterba case -EIO: /* -5 */ 94acce952bSliubo errstr = "IO failure"; 95acce952bSliubo break; 96d54f8144SDavid Sterba case -ENOMEM: /* -12*/ 97acce952bSliubo errstr = "Out of memory"; 98acce952bSliubo break; 99d54f8144SDavid Sterba case -EEXIST: /* -17 */ 1008c342930SJeff Mahoney errstr = "Object already exists"; 1018c342930SJeff Mahoney break; 102d54f8144SDavid Sterba case -ENOSPC: /* -28 */ 10394ef7280SDavid Sterba errstr = "No space left"; 10494ef7280SDavid Sterba break; 105d54f8144SDavid Sterba case -EROFS: /* -30 */ 106d54f8144SDavid Sterba errstr = "Readonly filesystem"; 10794ef7280SDavid Sterba break; 108fb8521caSDavid Sterba case -EOPNOTSUPP: /* -95 */ 109fb8521caSDavid Sterba errstr = "Operation not supported"; 110fb8521caSDavid Sterba break; 111fb8521caSDavid Sterba case -EUCLEAN: /* -117 */ 112fb8521caSDavid Sterba errstr = "Filesystem corrupted"; 113fb8521caSDavid Sterba break; 114fb8521caSDavid Sterba case -EDQUOT: /* -122 */ 115fb8521caSDavid Sterba errstr = "Quota exceeded"; 116fb8521caSDavid Sterba break; 117acce952bSliubo } 118acce952bSliubo 119acce952bSliubo return errstr; 120acce952bSliubo } 121acce952bSliubo 122acce952bSliubo /* 12334d97007SAnand Jain * __btrfs_handle_fs_error decodes expected errors from the caller and 12452042d8eSAndrea Gelmini * invokes the appropriate error response. 125acce952bSliubo */ 126c0d19e2bSDavid Sterba __cold 12734d97007SAnand Jain void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function, 1284da35113SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 129acce952bSliubo { 130acce952bSliubo struct super_block *sb = fs_info->sb; 13157d816a1SAnand Jain #ifdef CONFIG_PRINTK 132acce952bSliubo const char *errstr; 13357d816a1SAnand Jain #endif 134acce952bSliubo 135acce952bSliubo /* 136acce952bSliubo * Special case: if the error is EROFS, and we're already 1371751e8a6SLinus Torvalds * under SB_RDONLY, then it is safe here. 138acce952bSliubo */ 139bc98a42cSDavid Howells if (errno == -EROFS && sb_rdonly(sb)) 140acce952bSliubo return; 141acce952bSliubo 14257d816a1SAnand Jain #ifdef CONFIG_PRINTK 14308748810SDavid Sterba errstr = btrfs_decode_error(errno); 1444da35113SJeff Mahoney if (fmt) { 14537252a66SEric Sandeen struct va_format vaf; 14637252a66SEric Sandeen va_list args; 14737252a66SEric Sandeen 14837252a66SEric Sandeen va_start(args, fmt); 14937252a66SEric Sandeen vaf.fmt = fmt; 15037252a66SEric Sandeen vaf.va = &args; 1514da35113SJeff Mahoney 15262e85577SJeff Mahoney pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n", 15308748810SDavid Sterba sb->s_id, function, line, errno, errstr, &vaf); 15437252a66SEric Sandeen va_end(args); 1554da35113SJeff Mahoney } else { 15662e85577SJeff Mahoney pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s\n", 15708748810SDavid Sterba sb->s_id, function, line, errno, errstr); 1584da35113SJeff Mahoney } 15957d816a1SAnand Jain #endif 160acce952bSliubo 1610713d90cSAnand Jain /* 1620713d90cSAnand Jain * Today we only save the error info to memory. Long term we'll 1630713d90cSAnand Jain * also send it down to the disk 1640713d90cSAnand Jain */ 1650713d90cSAnand Jain set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 1660713d90cSAnand Jain 1674da35113SJeff Mahoney /* Don't go through full error handling during mount */ 168922ea899SAnand Jain if (!(sb->s_flags & SB_BORN)) 169922ea899SAnand Jain return; 170922ea899SAnand Jain 171922ea899SAnand Jain if (sb_rdonly(sb)) 172922ea899SAnand Jain return; 173922ea899SAnand Jain 174b0643e59SDennis Zhou btrfs_discard_stop(fs_info); 175b0643e59SDennis Zhou 176922ea899SAnand Jain /* btrfs handle error by forcing the filesystem readonly */ 177a0a1db70SFilipe Manana btrfs_set_sb_rdonly(sb); 178922ea899SAnand Jain btrfs_info(fs_info, "forced readonly"); 179922ea899SAnand Jain /* 180922ea899SAnand Jain * Note that a running device replace operation is not canceled here 181922ea899SAnand Jain * although there is no way to update the progress. It would add the 182922ea899SAnand Jain * risk of a deadlock, therefore the canceling is omitted. The only 183922ea899SAnand Jain * penalty is that some I/O remains active until the procedure 18452042d8eSAndrea Gelmini * completes. The next time when the filesystem is mounted writable 185922ea899SAnand Jain * again, the device replace operation continues. 186922ea899SAnand Jain */ 187acce952bSliubo } 1884da35113SJeff Mahoney 18957d816a1SAnand Jain #ifdef CONFIG_PRINTK 190533574c6SJoe Perches static const char * const logtypes[] = { 1914da35113SJeff Mahoney "emergency", 1924da35113SJeff Mahoney "alert", 1934da35113SJeff Mahoney "critical", 1944da35113SJeff Mahoney "error", 1954da35113SJeff Mahoney "warning", 1964da35113SJeff Mahoney "notice", 1974da35113SJeff Mahoney "info", 1984da35113SJeff Mahoney "debug", 1994da35113SJeff Mahoney }; 2004da35113SJeff Mahoney 20135f4e5e6SNikolay Borisov 20235f4e5e6SNikolay Borisov /* 20335f4e5e6SNikolay Borisov * Use one ratelimit state per log level so that a flood of less important 20435f4e5e6SNikolay Borisov * messages doesn't cause more important ones to be dropped. 20535f4e5e6SNikolay Borisov */ 20635f4e5e6SNikolay Borisov static struct ratelimit_state printk_limits[] = { 20735f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100), 20835f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100), 20935f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100), 21035f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100), 21135f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100), 21235f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100), 21335f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100), 21435f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100), 21535f4e5e6SNikolay Borisov }; 21635f4e5e6SNikolay Borisov 217b105e927SDavid Sterba void __cold btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...) 2184da35113SJeff Mahoney { 21940f7828bSPetr Mladek char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0"; 2204da35113SJeff Mahoney struct va_format vaf; 2214da35113SJeff Mahoney va_list args; 222533574c6SJoe Perches int kern_level; 22340f7828bSPetr Mladek const char *type = logtypes[4]; 22440f7828bSPetr Mladek struct ratelimit_state *ratelimit = &printk_limits[4]; 2254da35113SJeff Mahoney 2264da35113SJeff Mahoney va_start(args, fmt); 2274da35113SJeff Mahoney 228262c5e86SPetr Mladek while ((kern_level = printk_get_level(fmt)) != 0) { 229533574c6SJoe Perches size_t size = printk_skip_level(fmt) - fmt; 230262c5e86SPetr Mladek 231262c5e86SPetr Mladek if (kern_level >= '0' && kern_level <= '7') { 232533574c6SJoe Perches memcpy(lvl, fmt, size); 233533574c6SJoe Perches lvl[size] = '\0'; 234533574c6SJoe Perches type = logtypes[kern_level - '0']; 23535f4e5e6SNikolay Borisov ratelimit = &printk_limits[kern_level - '0']; 236262c5e86SPetr Mladek } 237262c5e86SPetr Mladek fmt += size; 238262c5e86SPetr Mladek } 239262c5e86SPetr Mladek 2404da35113SJeff Mahoney vaf.fmt = fmt; 2414da35113SJeff Mahoney vaf.va = &args; 242533574c6SJoe Perches 243a0f6d924SDavid Sterba if (__ratelimit(ratelimit)) { 244a0f6d924SDavid Sterba if (fs_info) 2453993b112SColin Ian King printk("%sBTRFS %s (device %s): %pV\n", lvl, type, 246a0f6d924SDavid Sterba fs_info->sb->s_id, &vaf); 247a0f6d924SDavid Sterba else 248a0f6d924SDavid Sterba printk("%sBTRFS %s: %pV\n", lvl, type, &vaf); 249a0f6d924SDavid Sterba } 250533574c6SJoe Perches 251533574c6SJoe Perches va_end(args); 2524da35113SJeff Mahoney } 253533574c6SJoe Perches #endif 254533574c6SJoe Perches 255e9306ad4SQu Wenruo #if BITS_PER_LONG == 32 256e9306ad4SQu Wenruo void __cold btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info) 257e9306ad4SQu Wenruo { 258e9306ad4SQu Wenruo if (!test_and_set_bit(BTRFS_FS_32BIT_WARN, &fs_info->flags)) { 259e9306ad4SQu Wenruo btrfs_warn(fs_info, "reaching 32bit limit for logical addresses"); 260e9306ad4SQu Wenruo btrfs_warn(fs_info, 261e9306ad4SQu Wenruo "due to page cache limit on 32bit systems, btrfs can't access metadata at or beyond %lluT", 262e9306ad4SQu Wenruo BTRFS_32BIT_MAX_FILE_SIZE >> 40); 263e9306ad4SQu Wenruo btrfs_warn(fs_info, 264e9306ad4SQu Wenruo "please consider upgrading to 64bit kernel/hardware"); 265e9306ad4SQu Wenruo } 266e9306ad4SQu Wenruo } 267e9306ad4SQu Wenruo 268e9306ad4SQu Wenruo void __cold btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info) 269e9306ad4SQu Wenruo { 270e9306ad4SQu Wenruo if (!test_and_set_bit(BTRFS_FS_32BIT_ERROR, &fs_info->flags)) { 271e9306ad4SQu Wenruo btrfs_err(fs_info, "reached 32bit limit for logical addresses"); 272e9306ad4SQu Wenruo btrfs_err(fs_info, 273e9306ad4SQu Wenruo "due to page cache limit on 32bit systems, metadata beyond %lluT can't be accessed", 274e9306ad4SQu Wenruo BTRFS_32BIT_MAX_FILE_SIZE >> 40); 275e9306ad4SQu Wenruo btrfs_err(fs_info, 276e9306ad4SQu Wenruo "please consider upgrading to 64bit kernel/hardware"); 277e9306ad4SQu Wenruo } 278e9306ad4SQu Wenruo } 279e9306ad4SQu Wenruo #endif 280e9306ad4SQu Wenruo 2818c342930SJeff Mahoney /* 28249b25e05SJeff Mahoney * We only mark the transaction aborted and then set the file system read-only. 28349b25e05SJeff Mahoney * This will prevent new transactions from starting or trying to join this 28449b25e05SJeff Mahoney * one. 28549b25e05SJeff Mahoney * 28649b25e05SJeff Mahoney * This means that error recovery at the call site is limited to freeing 28749b25e05SJeff Mahoney * any local memory allocations and passing the error code up without 28849b25e05SJeff Mahoney * further cleanup. The transaction should complete as it normally would 28949b25e05SJeff Mahoney * in the call path but will return -EIO. 29049b25e05SJeff Mahoney * 29149b25e05SJeff Mahoney * We'll complete the cleanup in btrfs_end_transaction and 29249b25e05SJeff Mahoney * btrfs_commit_transaction. 29349b25e05SJeff Mahoney */ 294c0d19e2bSDavid Sterba __cold 29549b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans, 29666642832SJeff Mahoney const char *function, 29749b25e05SJeff Mahoney unsigned int line, int errno) 29849b25e05SJeff Mahoney { 29966642832SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 30066642832SJeff Mahoney 301bf31f87fSDavid Sterba WRITE_ONCE(trans->aborted, errno); 30220c7bcecSSeraphime Kirkovski WRITE_ONCE(trans->transaction->aborted, errno); 303501407aaSJosef Bacik /* Wake up anybody who may be waiting on this transaction */ 30466642832SJeff Mahoney wake_up(&fs_info->transaction_wait); 30566642832SJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 30666642832SJeff Mahoney __btrfs_handle_fs_error(fs_info, function, line, errno, NULL); 30749b25e05SJeff Mahoney } 30849b25e05SJeff Mahoney /* 3098c342930SJeff Mahoney * __btrfs_panic decodes unexpected, fatal errors from the caller, 3108c342930SJeff Mahoney * issues an alert, and either panics or BUGs, depending on mount options. 3118c342930SJeff Mahoney */ 312c0d19e2bSDavid Sterba __cold 3138c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, 3148c342930SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 3158c342930SJeff Mahoney { 3168c342930SJeff Mahoney char *s_id = "<unknown>"; 3178c342930SJeff Mahoney const char *errstr; 3188c342930SJeff Mahoney struct va_format vaf = { .fmt = fmt }; 3198c342930SJeff Mahoney va_list args; 3208c342930SJeff Mahoney 3218c342930SJeff Mahoney if (fs_info) 3228c342930SJeff Mahoney s_id = fs_info->sb->s_id; 3238c342930SJeff Mahoney 3248c342930SJeff Mahoney va_start(args, fmt); 3258c342930SJeff Mahoney vaf.va = &args; 3268c342930SJeff Mahoney 32708748810SDavid Sterba errstr = btrfs_decode_error(errno); 328d8953d69SSatoru Takeuchi if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR))) 32908748810SDavid Sterba panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n", 33008748810SDavid Sterba s_id, function, line, &vaf, errno, errstr); 3318c342930SJeff Mahoney 332efe120a0SFrank Holton btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)", 333efe120a0SFrank Holton function, line, &vaf, errno, errstr); 3348c342930SJeff Mahoney va_end(args); 3358c342930SJeff Mahoney /* Caller calls BUG() */ 3368c342930SJeff Mahoney } 337acce952bSliubo 338e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 339e20d96d6SChris Mason { 3406bccf3abSJeff Mahoney close_ctree(btrfs_sb(sb)); 341e20d96d6SChris Mason } 3422e635a27SChris Mason 34395e05289SChris Mason enum { 344416a7202SDavid Sterba Opt_acl, Opt_noacl, 345416a7202SDavid Sterba Opt_clear_cache, 346416a7202SDavid Sterba Opt_commit_interval, 347416a7202SDavid Sterba Opt_compress, 348416a7202SDavid Sterba Opt_compress_force, 349416a7202SDavid Sterba Opt_compress_force_type, 350416a7202SDavid Sterba Opt_compress_type, 351416a7202SDavid Sterba Opt_degraded, 352416a7202SDavid Sterba Opt_device, 353416a7202SDavid Sterba Opt_fatal_errors, 354416a7202SDavid Sterba Opt_flushoncommit, Opt_noflushoncommit, 355416a7202SDavid Sterba Opt_max_inline, 356416a7202SDavid Sterba Opt_barrier, Opt_nobarrier, 357416a7202SDavid Sterba Opt_datacow, Opt_nodatacow, 358416a7202SDavid Sterba Opt_datasum, Opt_nodatasum, 359416a7202SDavid Sterba Opt_defrag, Opt_nodefrag, 360416a7202SDavid Sterba Opt_discard, Opt_nodiscard, 361b0643e59SDennis Zhou Opt_discard_mode, 362416a7202SDavid Sterba Opt_norecovery, 363416a7202SDavid Sterba Opt_ratio, 364416a7202SDavid Sterba Opt_rescan_uuid_tree, 365416a7202SDavid Sterba Opt_skip_balance, 366416a7202SDavid Sterba Opt_space_cache, Opt_no_space_cache, 367416a7202SDavid Sterba Opt_space_cache_version, 368416a7202SDavid Sterba Opt_ssd, Opt_nossd, 369416a7202SDavid Sterba Opt_ssd_spread, Opt_nossd_spread, 370416a7202SDavid Sterba Opt_subvol, 37137becec9SOmar Sandoval Opt_subvol_empty, 372416a7202SDavid Sterba Opt_subvolid, 373416a7202SDavid Sterba Opt_thread_pool, 374416a7202SDavid Sterba Opt_treelog, Opt_notreelog, 375416a7202SDavid Sterba Opt_user_subvol_rm_allowed, 376416a7202SDavid Sterba 37774ef0018SQu Wenruo /* Rescue options */ 37874ef0018SQu Wenruo Opt_rescue, 37974ef0018SQu Wenruo Opt_usebackuproot, 38074ef0018SQu Wenruo Opt_nologreplay, 38142437a63SJosef Bacik Opt_ignorebadroots, 382882dbe0cSJosef Bacik Opt_ignoredatacsums, 3839037d3cbSJosef Bacik Opt_rescue_all, 38474ef0018SQu Wenruo 385416a7202SDavid Sterba /* Deprecated options */ 386416a7202SDavid Sterba Opt_recovery, 3875297199aSNikolay Borisov Opt_inode_cache, Opt_noinode_cache, 388416a7202SDavid Sterba 389416a7202SDavid Sterba /* Debugging options */ 390416a7202SDavid Sterba Opt_check_integrity, 39170f6d82eSOmar Sandoval Opt_check_integrity_including_extent_data, 392416a7202SDavid Sterba Opt_check_integrity_print_mask, 393416a7202SDavid Sterba Opt_enospc_debug, Opt_noenospc_debug, 394d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 395d0bd4560SJosef Bacik Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all, 396d0bd4560SJosef Bacik #endif 397fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 398fb592373SJosef Bacik Opt_ref_verify, 399fb592373SJosef Bacik #endif 4009555c6c1SIlya Dryomov Opt_err, 40195e05289SChris Mason }; 40295e05289SChris Mason 4034d4ab6d6SDavid Sterba static const match_table_t tokens = { 404416a7202SDavid Sterba {Opt_acl, "acl"}, 405416a7202SDavid Sterba {Opt_noacl, "noacl"}, 406416a7202SDavid Sterba {Opt_clear_cache, "clear_cache"}, 407416a7202SDavid Sterba {Opt_commit_interval, "commit=%u"}, 408c8b97818SChris Mason {Opt_compress, "compress"}, 409261507a0SLi Zefan {Opt_compress_type, "compress=%s"}, 410a555f810SChris Mason {Opt_compress_force, "compress-force"}, 411261507a0SLi Zefan {Opt_compress_force_type, "compress-force=%s"}, 412416a7202SDavid Sterba {Opt_degraded, "degraded"}, 413416a7202SDavid Sterba {Opt_device, "device=%s"}, 414416a7202SDavid Sterba {Opt_fatal_errors, "fatal_errors=%s"}, 415dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 4162c9ee856SQu Wenruo {Opt_noflushoncommit, "noflushoncommit"}, 4174b9465cbSChris Mason {Opt_inode_cache, "inode_cache"}, 4183818aea2SQu Wenruo {Opt_noinode_cache, "noinode_cache"}, 419416a7202SDavid Sterba {Opt_max_inline, "max_inline=%s"}, 420416a7202SDavid Sterba {Opt_barrier, "barrier"}, 421416a7202SDavid Sterba {Opt_nobarrier, "nobarrier"}, 422416a7202SDavid Sterba {Opt_datacow, "datacow"}, 423416a7202SDavid Sterba {Opt_nodatacow, "nodatacow"}, 424416a7202SDavid Sterba {Opt_datasum, "datasum"}, 425416a7202SDavid Sterba {Opt_nodatasum, "nodatasum"}, 426416a7202SDavid Sterba {Opt_defrag, "autodefrag"}, 427416a7202SDavid Sterba {Opt_nodefrag, "noautodefrag"}, 428416a7202SDavid Sterba {Opt_discard, "discard"}, 429b0643e59SDennis Zhou {Opt_discard_mode, "discard=%s"}, 430416a7202SDavid Sterba {Opt_nodiscard, "nodiscard"}, 431416a7202SDavid Sterba {Opt_norecovery, "norecovery"}, 432416a7202SDavid Sterba {Opt_ratio, "metadata_ratio=%u"}, 433416a7202SDavid Sterba {Opt_rescan_uuid_tree, "rescan_uuid_tree"}, 4349555c6c1SIlya Dryomov {Opt_skip_balance, "skip_balance"}, 435416a7202SDavid Sterba {Opt_space_cache, "space_cache"}, 436416a7202SDavid Sterba {Opt_no_space_cache, "nospace_cache"}, 437416a7202SDavid Sterba {Opt_space_cache_version, "space_cache=%s"}, 438416a7202SDavid Sterba {Opt_ssd, "ssd"}, 439416a7202SDavid Sterba {Opt_nossd, "nossd"}, 440416a7202SDavid Sterba {Opt_ssd_spread, "ssd_spread"}, 441416a7202SDavid Sterba {Opt_nossd_spread, "nossd_spread"}, 442416a7202SDavid Sterba {Opt_subvol, "subvol=%s"}, 44337becec9SOmar Sandoval {Opt_subvol_empty, "subvol="}, 444416a7202SDavid Sterba {Opt_subvolid, "subvolid=%s"}, 445416a7202SDavid Sterba {Opt_thread_pool, "thread_pool=%u"}, 446416a7202SDavid Sterba {Opt_treelog, "treelog"}, 447416a7202SDavid Sterba {Opt_notreelog, "notreelog"}, 448416a7202SDavid Sterba {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, 449416a7202SDavid Sterba 45074ef0018SQu Wenruo /* Rescue options */ 45174ef0018SQu Wenruo {Opt_rescue, "rescue=%s"}, 45274ef0018SQu Wenruo /* Deprecated, with alias rescue=nologreplay */ 45374ef0018SQu Wenruo {Opt_nologreplay, "nologreplay"}, 45474ef0018SQu Wenruo /* Deprecated, with alias rescue=usebackuproot */ 45574ef0018SQu Wenruo {Opt_usebackuproot, "usebackuproot"}, 45674ef0018SQu Wenruo 457416a7202SDavid Sterba /* Deprecated options */ 458416a7202SDavid Sterba {Opt_recovery, "recovery"}, 459416a7202SDavid Sterba 460416a7202SDavid Sterba /* Debugging options */ 46121adbd5cSStefan Behrens {Opt_check_integrity, "check_int"}, 46221adbd5cSStefan Behrens {Opt_check_integrity_including_extent_data, "check_int_data"}, 46302453bdeSAnand Jain {Opt_check_integrity_print_mask, "check_int_print_mask=%u"}, 464416a7202SDavid Sterba {Opt_enospc_debug, "enospc_debug"}, 465416a7202SDavid Sterba {Opt_noenospc_debug, "noenospc_debug"}, 466d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 467d0bd4560SJosef Bacik {Opt_fragment_data, "fragment=data"}, 468d0bd4560SJosef Bacik {Opt_fragment_metadata, "fragment=metadata"}, 469d0bd4560SJosef Bacik {Opt_fragment_all, "fragment=all"}, 470d0bd4560SJosef Bacik #endif 471fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 472fb592373SJosef Bacik {Opt_ref_verify, "ref_verify"}, 473fb592373SJosef Bacik #endif 47433268eafSJosef Bacik {Opt_err, NULL}, 47595e05289SChris Mason }; 47695e05289SChris Mason 47774ef0018SQu Wenruo static const match_table_t rescue_tokens = { 47874ef0018SQu Wenruo {Opt_usebackuproot, "usebackuproot"}, 47974ef0018SQu Wenruo {Opt_nologreplay, "nologreplay"}, 48042437a63SJosef Bacik {Opt_ignorebadroots, "ignorebadroots"}, 48142437a63SJosef Bacik {Opt_ignorebadroots, "ibadroots"}, 482882dbe0cSJosef Bacik {Opt_ignoredatacsums, "ignoredatacsums"}, 483882dbe0cSJosef Bacik {Opt_ignoredatacsums, "idatacsums"}, 4849037d3cbSJosef Bacik {Opt_rescue_all, "all"}, 48574ef0018SQu Wenruo {Opt_err, NULL}, 48674ef0018SQu Wenruo }; 48774ef0018SQu Wenruo 488d70bf748SJosef Bacik static bool check_ro_option(struct btrfs_fs_info *fs_info, unsigned long opt, 489d70bf748SJosef Bacik const char *opt_name) 490d70bf748SJosef Bacik { 491d70bf748SJosef Bacik if (fs_info->mount_opt & opt) { 492d70bf748SJosef Bacik btrfs_err(fs_info, "%s must be used with ro mount option", 493d70bf748SJosef Bacik opt_name); 494d70bf748SJosef Bacik return true; 495d70bf748SJosef Bacik } 496d70bf748SJosef Bacik return false; 497d70bf748SJosef Bacik } 498d70bf748SJosef Bacik 49974ef0018SQu Wenruo static int parse_rescue_options(struct btrfs_fs_info *info, const char *options) 50074ef0018SQu Wenruo { 50174ef0018SQu Wenruo char *opts; 50274ef0018SQu Wenruo char *orig; 50374ef0018SQu Wenruo char *p; 50474ef0018SQu Wenruo substring_t args[MAX_OPT_ARGS]; 50574ef0018SQu Wenruo int ret = 0; 50674ef0018SQu Wenruo 50774ef0018SQu Wenruo opts = kstrdup(options, GFP_KERNEL); 50874ef0018SQu Wenruo if (!opts) 50974ef0018SQu Wenruo return -ENOMEM; 51074ef0018SQu Wenruo orig = opts; 51174ef0018SQu Wenruo 51274ef0018SQu Wenruo while ((p = strsep(&opts, ":")) != NULL) { 51374ef0018SQu Wenruo int token; 51474ef0018SQu Wenruo 51574ef0018SQu Wenruo if (!*p) 51674ef0018SQu Wenruo continue; 51774ef0018SQu Wenruo token = match_token(p, rescue_tokens, args); 51874ef0018SQu Wenruo switch (token){ 51974ef0018SQu Wenruo case Opt_usebackuproot: 52074ef0018SQu Wenruo btrfs_info(info, 52174ef0018SQu Wenruo "trying to use backup root at mount time"); 52274ef0018SQu Wenruo btrfs_set_opt(info->mount_opt, USEBACKUPROOT); 52374ef0018SQu Wenruo break; 52474ef0018SQu Wenruo case Opt_nologreplay: 52574ef0018SQu Wenruo btrfs_set_and_info(info, NOLOGREPLAY, 52674ef0018SQu Wenruo "disabling log replay at mount time"); 52774ef0018SQu Wenruo break; 52842437a63SJosef Bacik case Opt_ignorebadroots: 52942437a63SJosef Bacik btrfs_set_and_info(info, IGNOREBADROOTS, 53042437a63SJosef Bacik "ignoring bad roots"); 53142437a63SJosef Bacik break; 532882dbe0cSJosef Bacik case Opt_ignoredatacsums: 533882dbe0cSJosef Bacik btrfs_set_and_info(info, IGNOREDATACSUMS, 534882dbe0cSJosef Bacik "ignoring data csums"); 535882dbe0cSJosef Bacik break; 5369037d3cbSJosef Bacik case Opt_rescue_all: 5379037d3cbSJosef Bacik btrfs_info(info, "enabling all of the rescue options"); 5389037d3cbSJosef Bacik btrfs_set_and_info(info, IGNOREDATACSUMS, 5399037d3cbSJosef Bacik "ignoring data csums"); 5409037d3cbSJosef Bacik btrfs_set_and_info(info, IGNOREBADROOTS, 5419037d3cbSJosef Bacik "ignoring bad roots"); 5429037d3cbSJosef Bacik btrfs_set_and_info(info, NOLOGREPLAY, 5439037d3cbSJosef Bacik "disabling log replay at mount time"); 5449037d3cbSJosef Bacik break; 54574ef0018SQu Wenruo case Opt_err: 54674ef0018SQu Wenruo btrfs_info(info, "unrecognized rescue option '%s'", p); 54774ef0018SQu Wenruo ret = -EINVAL; 54874ef0018SQu Wenruo goto out; 54974ef0018SQu Wenruo default: 55074ef0018SQu Wenruo break; 55174ef0018SQu Wenruo } 55274ef0018SQu Wenruo 55374ef0018SQu Wenruo } 55474ef0018SQu Wenruo out: 55574ef0018SQu Wenruo kfree(orig); 55674ef0018SQu Wenruo return ret; 55774ef0018SQu Wenruo } 55874ef0018SQu Wenruo 559edf24abeSChristoph Hellwig /* 560edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 561edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 56249b25e05SJeff Mahoney * XXX JDM: This needs to be cleaned up for remount. 563edf24abeSChristoph Hellwig */ 5642ff7e61eSJeff Mahoney int btrfs_parse_options(struct btrfs_fs_info *info, char *options, 56596da0919SQu Wenruo unsigned long new_flags) 56695e05289SChris Mason { 56795e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 568e215772cSMisono, Tomohiro char *p, *num; 5694543df7eSChris Mason int intarg; 570a7a3f7caSSage Weil int ret = 0; 571261507a0SLi Zefan char *compress_type; 572261507a0SLi Zefan bool compress_force = false; 573b7c47bbbSTsutomu Itoh enum btrfs_compression_type saved_compress_type; 57427942c99SDavid Sterba int saved_compress_level; 575b7c47bbbSTsutomu Itoh bool saved_compress_force; 576b7c47bbbSTsutomu Itoh int no_compress = 0; 577b6cda9bcSChris Mason 5780b246afaSJeff Mahoney if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) 57970f6d82eSOmar Sandoval btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE); 58094846229SBoris Burkov else if (btrfs_free_space_cache_v1_active(info)) { 5815d1ab66cSNaohiro Aota if (btrfs_is_zoned(info)) { 5825d1ab66cSNaohiro Aota btrfs_info(info, 5835d1ab66cSNaohiro Aota "zoned: clearing existing space cache"); 5845d1ab66cSNaohiro Aota btrfs_set_super_cache_generation(info->super_copy, 0); 5855d1ab66cSNaohiro Aota } else { 58673bc1876SJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 5875d1ab66cSNaohiro Aota } 5885d1ab66cSNaohiro Aota } 58973bc1876SJosef Bacik 59096da0919SQu Wenruo /* 59196da0919SQu Wenruo * Even the options are empty, we still need to do extra check 59296da0919SQu Wenruo * against new flags 59396da0919SQu Wenruo */ 59495e05289SChris Mason if (!options) 59596da0919SQu Wenruo goto check; 59695e05289SChris Mason 59795e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 59895e05289SChris Mason int token; 59995e05289SChris Mason if (!*p) 60095e05289SChris Mason continue; 60195e05289SChris Mason 60295e05289SChris Mason token = match_token(p, tokens, args); 60395e05289SChris Mason switch (token) { 604dfe25020SChris Mason case Opt_degraded: 6050b246afaSJeff Mahoney btrfs_info(info, "allowing degraded mounts"); 606dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 607dfe25020SChris Mason break; 60895e05289SChris Mason case Opt_subvol: 60937becec9SOmar Sandoval case Opt_subvol_empty: 61073f73415SJosef Bacik case Opt_subvolid: 61143e570b0SChristoph Hellwig case Opt_device: 612edf24abeSChristoph Hellwig /* 613fa59f27cSAnand Jain * These are parsed by btrfs_parse_subvol_options or 614fa59f27cSAnand Jain * btrfs_parse_device_options and can be ignored here. 615edf24abeSChristoph Hellwig */ 61695e05289SChris Mason break; 617b6cda9bcSChris Mason case Opt_nodatasum: 6183cdde224SJeff Mahoney btrfs_set_and_info(info, NODATASUM, 61907802534SQu Wenruo "setting nodatasum"); 620be20aa9dSChris Mason break; 621d399167dSQu Wenruo case Opt_datasum: 6223cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATASUM)) { 6233cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATACOW)) 6240b246afaSJeff Mahoney btrfs_info(info, 6255d163e0eSJeff Mahoney "setting datasum, datacow enabled"); 626d399167dSQu Wenruo else 6270b246afaSJeff Mahoney btrfs_info(info, "setting datasum"); 62807802534SQu Wenruo } 629d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATACOW); 630d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATASUM); 631d399167dSQu Wenruo break; 632be20aa9dSChris Mason case Opt_nodatacow: 6333cdde224SJeff Mahoney if (!btrfs_test_opt(info, NODATACOW)) { 6343cdde224SJeff Mahoney if (!btrfs_test_opt(info, COMPRESS) || 6353cdde224SJeff Mahoney !btrfs_test_opt(info, FORCE_COMPRESS)) { 6360b246afaSJeff Mahoney btrfs_info(info, 637efe120a0SFrank Holton "setting nodatacow, compression disabled"); 638bedb2ccaSAndrei Popa } else { 6390b246afaSJeff Mahoney btrfs_info(info, "setting nodatacow"); 640bedb2ccaSAndrei Popa } 64107802534SQu Wenruo } 642bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, COMPRESS); 643bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 644be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 645be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 646b6cda9bcSChris Mason break; 647a258af7aSQu Wenruo case Opt_datacow: 6483cdde224SJeff Mahoney btrfs_clear_and_info(info, NODATACOW, 64907802534SQu Wenruo "setting datacow"); 650a258af7aSQu Wenruo break; 651a555f810SChris Mason case Opt_compress_force: 652261507a0SLi Zefan case Opt_compress_force_type: 653261507a0SLi Zefan compress_force = true; 654c730ae0cSMarcos Paulo de Souza fallthrough; 655261507a0SLi Zefan case Opt_compress: 656261507a0SLi Zefan case Opt_compress_type: 6573cdde224SJeff Mahoney saved_compress_type = btrfs_test_opt(info, 6583cdde224SJeff Mahoney COMPRESS) ? 659b7c47bbbSTsutomu Itoh info->compress_type : BTRFS_COMPRESS_NONE; 660b7c47bbbSTsutomu Itoh saved_compress_force = 6613cdde224SJeff Mahoney btrfs_test_opt(info, FORCE_COMPRESS); 66227942c99SDavid Sterba saved_compress_level = info->compress_level; 663261507a0SLi Zefan if (token == Opt_compress || 664261507a0SLi Zefan token == Opt_compress_force || 665a7164fa4SDavid Sterba strncmp(args[0].from, "zlib", 4) == 0) { 666261507a0SLi Zefan compress_type = "zlib"; 667eae8d825SQu Wenruo 668261507a0SLi Zefan info->compress_type = BTRFS_COMPRESS_ZLIB; 669eae8d825SQu Wenruo info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL; 670eae8d825SQu Wenruo /* 671eae8d825SQu Wenruo * args[0] contains uninitialized data since 672eae8d825SQu Wenruo * for these tokens we don't expect any 673eae8d825SQu Wenruo * parameter. 674eae8d825SQu Wenruo */ 675eae8d825SQu Wenruo if (token != Opt_compress && 676eae8d825SQu Wenruo token != Opt_compress_force) 677f51d2b59SDavid Sterba info->compress_level = 678d0ab62ceSDennis Zhou btrfs_compress_str2level( 679d0ab62ceSDennis Zhou BTRFS_COMPRESS_ZLIB, 680d0ab62ceSDennis Zhou args[0].from + 4); 681063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 682bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 683bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 684b7c47bbbSTsutomu Itoh no_compress = 0; 685a7164fa4SDavid Sterba } else if (strncmp(args[0].from, "lzo", 3) == 0) { 686a6fa6faeSLi Zefan compress_type = "lzo"; 687a6fa6faeSLi Zefan info->compress_type = BTRFS_COMPRESS_LZO; 688282dd7d7SMarcos Paulo de Souza info->compress_level = 0; 689063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 690bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 691bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 6922b0ce2c2SMitch Harder btrfs_set_fs_incompat(info, COMPRESS_LZO); 693b7c47bbbSTsutomu Itoh no_compress = 0; 6943f93aef5SDennis Zhou } else if (strncmp(args[0].from, "zstd", 4) == 0) { 6955c1aab1dSNick Terrell compress_type = "zstd"; 6965c1aab1dSNick Terrell info->compress_type = BTRFS_COMPRESS_ZSTD; 6973f93aef5SDennis Zhou info->compress_level = 6983f93aef5SDennis Zhou btrfs_compress_str2level( 6993f93aef5SDennis Zhou BTRFS_COMPRESS_ZSTD, 7003f93aef5SDennis Zhou args[0].from + 4); 7015c1aab1dSNick Terrell btrfs_set_opt(info->mount_opt, COMPRESS); 7025c1aab1dSNick Terrell btrfs_clear_opt(info->mount_opt, NODATACOW); 7035c1aab1dSNick Terrell btrfs_clear_opt(info->mount_opt, NODATASUM); 7045c1aab1dSNick Terrell btrfs_set_fs_incompat(info, COMPRESS_ZSTD); 7055c1aab1dSNick Terrell no_compress = 0; 706063849eaSArnd Hannemann } else if (strncmp(args[0].from, "no", 2) == 0) { 707063849eaSArnd Hannemann compress_type = "no"; 70827942c99SDavid Sterba info->compress_level = 0; 70927942c99SDavid Sterba info->compress_type = 0; 710063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, COMPRESS); 711063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 712063849eaSArnd Hannemann compress_force = false; 713b7c47bbbSTsutomu Itoh no_compress++; 714261507a0SLi Zefan } else { 715261507a0SLi Zefan ret = -EINVAL; 716261507a0SLi Zefan goto out; 717261507a0SLi Zefan } 718261507a0SLi Zefan 719261507a0SLi Zefan if (compress_force) { 720b7c47bbbSTsutomu Itoh btrfs_set_opt(info->mount_opt, FORCE_COMPRESS); 721143f3636SDavid Sterba } else { 7224027e0f4SWang Shilong /* 7234027e0f4SWang Shilong * If we remount from compress-force=xxx to 7244027e0f4SWang Shilong * compress=xxx, we need clear FORCE_COMPRESS 7254027e0f4SWang Shilong * flag, otherwise, there is no way for users 7264027e0f4SWang Shilong * to disable forcible compression separately. 7274027e0f4SWang Shilong */ 7284027e0f4SWang Shilong btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 729a7e252afSMiao Xie } 73027942c99SDavid Sterba if (no_compress == 1) { 73127942c99SDavid Sterba btrfs_info(info, "use no compression"); 73227942c99SDavid Sterba } else if ((info->compress_type != saved_compress_type) || 73327942c99SDavid Sterba (compress_force != saved_compress_force) || 73427942c99SDavid Sterba (info->compress_level != saved_compress_level)) { 735f51d2b59SDavid Sterba btrfs_info(info, "%s %s compression, level %d", 736b7c47bbbSTsutomu Itoh (compress_force) ? "force" : "use", 737f51d2b59SDavid Sterba compress_type, info->compress_level); 738b7c47bbbSTsutomu Itoh } 739b7c47bbbSTsutomu Itoh compress_force = false; 740a555f810SChris Mason break; 741e18e4809SChris Mason case Opt_ssd: 7423cdde224SJeff Mahoney btrfs_set_and_info(info, SSD, 743583b7231SHans van Kranenburg "enabling ssd optimizations"); 744951e7966SAdam Borowski btrfs_clear_opt(info->mount_opt, NOSSD); 745e18e4809SChris Mason break; 746451d7585SChris Mason case Opt_ssd_spread: 747583b7231SHans van Kranenburg btrfs_set_and_info(info, SSD, 748583b7231SHans van Kranenburg "enabling ssd optimizations"); 7493cdde224SJeff Mahoney btrfs_set_and_info(info, SSD_SPREAD, 750583b7231SHans van Kranenburg "using spread ssd allocation scheme"); 751951e7966SAdam Borowski btrfs_clear_opt(info->mount_opt, NOSSD); 752451d7585SChris Mason break; 7533b30c22fSChris Mason case Opt_nossd: 754583b7231SHans van Kranenburg btrfs_set_opt(info->mount_opt, NOSSD); 755583b7231SHans van Kranenburg btrfs_clear_and_info(info, SSD, 756583b7231SHans van Kranenburg "not using ssd optimizations"); 757c730ae0cSMarcos Paulo de Souza fallthrough; 75862b8e077SHoward McLauchlan case Opt_nossd_spread: 759583b7231SHans van Kranenburg btrfs_clear_and_info(info, SSD_SPREAD, 760583b7231SHans van Kranenburg "not using spread ssd allocation scheme"); 7613b30c22fSChris Mason break; 762842bef58SQu Wenruo case Opt_barrier: 7633cdde224SJeff Mahoney btrfs_clear_and_info(info, NOBARRIER, 76407802534SQu Wenruo "turning on barriers"); 765842bef58SQu Wenruo break; 76621ad10cfSChris Mason case Opt_nobarrier: 7673cdde224SJeff Mahoney btrfs_set_and_info(info, NOBARRIER, 76807802534SQu Wenruo "turning off barriers"); 76921ad10cfSChris Mason break; 7704543df7eSChris Mason case Opt_thread_pool: 7712c334e87SWang Shilong ret = match_int(&args[0], &intarg); 7722c334e87SWang Shilong if (ret) { 7732c334e87SWang Shilong goto out; 774f7b885beSAnand Jain } else if (intarg == 0) { 7752c334e87SWang Shilong ret = -EINVAL; 7762c334e87SWang Shilong goto out; 7772c334e87SWang Shilong } 778f7b885beSAnand Jain info->thread_pool_size = intarg; 7794543df7eSChris Mason break; 7806f568d35SChris Mason case Opt_max_inline: 781edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 7826f568d35SChris Mason if (num) { 78391748467SAkinobu Mita info->max_inline = memparse(num, NULL); 7846f568d35SChris Mason kfree(num); 7856f568d35SChris Mason 78615ada040SChris Mason if (info->max_inline) { 787feb5f965SMitch Harder info->max_inline = min_t(u64, 78815ada040SChris Mason info->max_inline, 7890b246afaSJeff Mahoney info->sectorsize); 79015ada040SChris Mason } 7910b246afaSJeff Mahoney btrfs_info(info, "max_inline at %llu", 792c1c9ff7cSGeert Uytterhoeven info->max_inline); 7932c334e87SWang Shilong } else { 7942c334e87SWang Shilong ret = -ENOMEM; 7952c334e87SWang Shilong goto out; 7966f568d35SChris Mason } 7976f568d35SChris Mason break; 798bd0330adSQu Wenruo case Opt_acl: 79945ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL 8001751e8a6SLinus Torvalds info->sb->s_flags |= SB_POSIXACL; 801bd0330adSQu Wenruo break; 80245ff35d6SGuangliang Zhao #else 8030b246afaSJeff Mahoney btrfs_err(info, "support for ACL not compiled in!"); 80445ff35d6SGuangliang Zhao ret = -EINVAL; 80545ff35d6SGuangliang Zhao goto out; 80645ff35d6SGuangliang Zhao #endif 80733268eafSJosef Bacik case Opt_noacl: 8081751e8a6SLinus Torvalds info->sb->s_flags &= ~SB_POSIXACL; 80933268eafSJosef Bacik break; 8103a5e1404SSage Weil case Opt_notreelog: 8113cdde224SJeff Mahoney btrfs_set_and_info(info, NOTREELOG, 81207802534SQu Wenruo "disabling tree log"); 8133a5e1404SSage Weil break; 814a88998f2SQu Wenruo case Opt_treelog: 8153cdde224SJeff Mahoney btrfs_clear_and_info(info, NOTREELOG, 81607802534SQu Wenruo "enabling tree log"); 817a88998f2SQu Wenruo break; 818fed8f166SQu Wenruo case Opt_norecovery: 81996da0919SQu Wenruo case Opt_nologreplay: 82074ef0018SQu Wenruo btrfs_warn(info, 82174ef0018SQu Wenruo "'nologreplay' is deprecated, use 'rescue=nologreplay' instead"); 8223cdde224SJeff Mahoney btrfs_set_and_info(info, NOLOGREPLAY, 82396da0919SQu Wenruo "disabling log replay at mount time"); 82496da0919SQu Wenruo break; 825dccae999SSage Weil case Opt_flushoncommit: 8263cdde224SJeff Mahoney btrfs_set_and_info(info, FLUSHONCOMMIT, 82707802534SQu Wenruo "turning on flush-on-commit"); 828dccae999SSage Weil break; 8292c9ee856SQu Wenruo case Opt_noflushoncommit: 8303cdde224SJeff Mahoney btrfs_clear_and_info(info, FLUSHONCOMMIT, 83107802534SQu Wenruo "turning off flush-on-commit"); 8322c9ee856SQu Wenruo break; 83397e728d4SJosef Bacik case Opt_ratio: 8342c334e87SWang Shilong ret = match_int(&args[0], &intarg); 835764cb8b4SAnand Jain if (ret) 8362c334e87SWang Shilong goto out; 83797e728d4SJosef Bacik info->metadata_ratio = intarg; 838764cb8b4SAnand Jain btrfs_info(info, "metadata ratio %u", 83997e728d4SJosef Bacik info->metadata_ratio); 84097e728d4SJosef Bacik break; 841e244a0aeSChristoph Hellwig case Opt_discard: 842b0643e59SDennis Zhou case Opt_discard_mode: 843b0643e59SDennis Zhou if (token == Opt_discard || 844b0643e59SDennis Zhou strcmp(args[0].from, "sync") == 0) { 845b0643e59SDennis Zhou btrfs_clear_opt(info->mount_opt, DISCARD_ASYNC); 84646b27f50SDennis Zhou btrfs_set_and_info(info, DISCARD_SYNC, 84746b27f50SDennis Zhou "turning on sync discard"); 848b0643e59SDennis Zhou } else if (strcmp(args[0].from, "async") == 0) { 849b0643e59SDennis Zhou btrfs_clear_opt(info->mount_opt, DISCARD_SYNC); 850b0643e59SDennis Zhou btrfs_set_and_info(info, DISCARD_ASYNC, 851b0643e59SDennis Zhou "turning on async discard"); 852b0643e59SDennis Zhou } else { 853b0643e59SDennis Zhou ret = -EINVAL; 854b0643e59SDennis Zhou goto out; 855b0643e59SDennis Zhou } 856e244a0aeSChristoph Hellwig break; 857e07a2adeSQu Wenruo case Opt_nodiscard: 85846b27f50SDennis Zhou btrfs_clear_and_info(info, DISCARD_SYNC, 85907802534SQu Wenruo "turning off discard"); 860b0643e59SDennis Zhou btrfs_clear_and_info(info, DISCARD_ASYNC, 861b0643e59SDennis Zhou "turning off async discard"); 862e07a2adeSQu Wenruo break; 8630af3d00bSJosef Bacik case Opt_space_cache: 86470f6d82eSOmar Sandoval case Opt_space_cache_version: 86570f6d82eSOmar Sandoval if (token == Opt_space_cache || 86670f6d82eSOmar Sandoval strcmp(args[0].from, "v1") == 0) { 8670b246afaSJeff Mahoney btrfs_clear_opt(info->mount_opt, 86870f6d82eSOmar Sandoval FREE_SPACE_TREE); 8693cdde224SJeff Mahoney btrfs_set_and_info(info, SPACE_CACHE, 87007802534SQu Wenruo "enabling disk space caching"); 87170f6d82eSOmar Sandoval } else if (strcmp(args[0].from, "v2") == 0) { 8720b246afaSJeff Mahoney btrfs_clear_opt(info->mount_opt, 87370f6d82eSOmar Sandoval SPACE_CACHE); 8740b246afaSJeff Mahoney btrfs_set_and_info(info, FREE_SPACE_TREE, 87570f6d82eSOmar Sandoval "enabling free space tree"); 87670f6d82eSOmar Sandoval } else { 87770f6d82eSOmar Sandoval ret = -EINVAL; 87870f6d82eSOmar Sandoval goto out; 87970f6d82eSOmar Sandoval } 8800de90876SJosef Bacik break; 881f420ee1eSStefan Behrens case Opt_rescan_uuid_tree: 882f420ee1eSStefan Behrens btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE); 883f420ee1eSStefan Behrens break; 88473bc1876SJosef Bacik case Opt_no_space_cache: 8853cdde224SJeff Mahoney if (btrfs_test_opt(info, SPACE_CACHE)) { 8860b246afaSJeff Mahoney btrfs_clear_and_info(info, SPACE_CACHE, 88707802534SQu Wenruo "disabling disk space caching"); 88870f6d82eSOmar Sandoval } 8893cdde224SJeff Mahoney if (btrfs_test_opt(info, FREE_SPACE_TREE)) { 8900b246afaSJeff Mahoney btrfs_clear_and_info(info, FREE_SPACE_TREE, 89170f6d82eSOmar Sandoval "disabling free space tree"); 89270f6d82eSOmar Sandoval } 89373bc1876SJosef Bacik break; 8944b9465cbSChris Mason case Opt_inode_cache: 8953818aea2SQu Wenruo case Opt_noinode_cache: 8965297199aSNikolay Borisov btrfs_warn(info, 8975297199aSNikolay Borisov "the 'inode_cache' option is deprecated and has no effect since 5.11"); 8984b9465cbSChris Mason break; 89988c2ba3bSJosef Bacik case Opt_clear_cache: 9003cdde224SJeff Mahoney btrfs_set_and_info(info, CLEAR_CACHE, 90107802534SQu Wenruo "force clearing of disk cache"); 9020af3d00bSJosef Bacik break; 9034260f7c7SSage Weil case Opt_user_subvol_rm_allowed: 9044260f7c7SSage Weil btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 9054260f7c7SSage Weil break; 90691435650SChris Mason case Opt_enospc_debug: 90791435650SChris Mason btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 90891435650SChris Mason break; 90953036293SQu Wenruo case Opt_noenospc_debug: 91053036293SQu Wenruo btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG); 91153036293SQu Wenruo break; 9124cb5300bSChris Mason case Opt_defrag: 9133cdde224SJeff Mahoney btrfs_set_and_info(info, AUTO_DEFRAG, 91407802534SQu Wenruo "enabling auto defrag"); 9154cb5300bSChris Mason break; 916fc0ca9afSQu Wenruo case Opt_nodefrag: 9173cdde224SJeff Mahoney btrfs_clear_and_info(info, AUTO_DEFRAG, 91807802534SQu Wenruo "disabling auto defrag"); 919fc0ca9afSQu Wenruo break; 920af31f5e5SChris Mason case Opt_recovery: 9218dcddfa0SQu Wenruo case Opt_usebackuproot: 92274ef0018SQu Wenruo btrfs_warn(info, 92374ef0018SQu Wenruo "'%s' is deprecated, use 'rescue=usebackuproot' instead", 92474ef0018SQu Wenruo token == Opt_recovery ? "recovery" : 92574ef0018SQu Wenruo "usebackuproot"); 9260b246afaSJeff Mahoney btrfs_info(info, 9278dcddfa0SQu Wenruo "trying to use backup root at mount time"); 9288dcddfa0SQu Wenruo btrfs_set_opt(info->mount_opt, USEBACKUPROOT); 929af31f5e5SChris Mason break; 9309555c6c1SIlya Dryomov case Opt_skip_balance: 9319555c6c1SIlya Dryomov btrfs_set_opt(info->mount_opt, SKIP_BALANCE); 9329555c6c1SIlya Dryomov break; 93321adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 93421adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 9350b246afaSJeff Mahoney btrfs_info(info, 936efe120a0SFrank Holton "enabling check integrity including extent data"); 937cbeaae4fSDavid Sterba btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY_DATA); 93821adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 93921adbd5cSStefan Behrens break; 94021adbd5cSStefan Behrens case Opt_check_integrity: 9410b246afaSJeff Mahoney btrfs_info(info, "enabling check integrity"); 94221adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 94321adbd5cSStefan Behrens break; 94421adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 9452c334e87SWang Shilong ret = match_int(&args[0], &intarg); 94602453bdeSAnand Jain if (ret) 9472c334e87SWang Shilong goto out; 94821adbd5cSStefan Behrens info->check_integrity_print_mask = intarg; 94902453bdeSAnand Jain btrfs_info(info, "check_integrity_print_mask 0x%x", 95021adbd5cSStefan Behrens info->check_integrity_print_mask); 95121adbd5cSStefan Behrens break; 95221adbd5cSStefan Behrens #else 95321adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 95421adbd5cSStefan Behrens case Opt_check_integrity: 95521adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 9560b246afaSJeff Mahoney btrfs_err(info, 957efe120a0SFrank Holton "support for check_integrity* not compiled in!"); 95821adbd5cSStefan Behrens ret = -EINVAL; 95921adbd5cSStefan Behrens goto out; 96021adbd5cSStefan Behrens #endif 9618c342930SJeff Mahoney case Opt_fatal_errors: 9628c342930SJeff Mahoney if (strcmp(args[0].from, "panic") == 0) 9638c342930SJeff Mahoney btrfs_set_opt(info->mount_opt, 9648c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 9658c342930SJeff Mahoney else if (strcmp(args[0].from, "bug") == 0) 9668c342930SJeff Mahoney btrfs_clear_opt(info->mount_opt, 9678c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 9688c342930SJeff Mahoney else { 9698c342930SJeff Mahoney ret = -EINVAL; 9708c342930SJeff Mahoney goto out; 9718c342930SJeff Mahoney } 9728c342930SJeff Mahoney break; 9738b87dc17SDavid Sterba case Opt_commit_interval: 9748b87dc17SDavid Sterba intarg = 0; 9758b87dc17SDavid Sterba ret = match_int(&args[0], &intarg); 976d3740608SAnand Jain if (ret) 9778b87dc17SDavid Sterba goto out; 978d3740608SAnand Jain if (intarg == 0) { 979d3740608SAnand Jain btrfs_info(info, 980d3740608SAnand Jain "using default commit interval %us", 981d3740608SAnand Jain BTRFS_DEFAULT_COMMIT_INTERVAL); 982d3740608SAnand Jain intarg = BTRFS_DEFAULT_COMMIT_INTERVAL; 983d3740608SAnand Jain } else if (intarg > 300) { 984d3740608SAnand Jain btrfs_warn(info, "excessive commit interval %d", 9858b87dc17SDavid Sterba intarg); 9868b87dc17SDavid Sterba } 9878b87dc17SDavid Sterba info->commit_interval = intarg; 9888b87dc17SDavid Sterba break; 98974ef0018SQu Wenruo case Opt_rescue: 99074ef0018SQu Wenruo ret = parse_rescue_options(info, args[0].from); 99174ef0018SQu Wenruo if (ret < 0) 99274ef0018SQu Wenruo goto out; 99374ef0018SQu Wenruo break; 994d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 995d0bd4560SJosef Bacik case Opt_fragment_all: 9960b246afaSJeff Mahoney btrfs_info(info, "fragmenting all space"); 997d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 998d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA); 999d0bd4560SJosef Bacik break; 1000d0bd4560SJosef Bacik case Opt_fragment_metadata: 10010b246afaSJeff Mahoney btrfs_info(info, "fragmenting metadata"); 1002d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, 1003d0bd4560SJosef Bacik FRAGMENT_METADATA); 1004d0bd4560SJosef Bacik break; 1005d0bd4560SJosef Bacik case Opt_fragment_data: 10060b246afaSJeff Mahoney btrfs_info(info, "fragmenting data"); 1007d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 1008d0bd4560SJosef Bacik break; 1009d0bd4560SJosef Bacik #endif 1010fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 1011fb592373SJosef Bacik case Opt_ref_verify: 1012fb592373SJosef Bacik btrfs_info(info, "doing ref verification"); 1013fb592373SJosef Bacik btrfs_set_opt(info->mount_opt, REF_VERIFY); 1014fb592373SJosef Bacik break; 1015fb592373SJosef Bacik #endif 1016a7a3f7caSSage Weil case Opt_err: 10177e8f19e5SDavid Sterba btrfs_err(info, "unrecognized mount option '%s'", p); 1018a7a3f7caSSage Weil ret = -EINVAL; 1019a7a3f7caSSage Weil goto out; 102095e05289SChris Mason default: 1021be20aa9dSChris Mason break; 102295e05289SChris Mason } 102395e05289SChris Mason } 102496da0919SQu Wenruo check: 1025d70bf748SJosef Bacik /* We're read-only, don't have to check. */ 1026d70bf748SJosef Bacik if (new_flags & SB_RDONLY) 1027d70bf748SJosef Bacik goto out; 1028d70bf748SJosef Bacik 102942437a63SJosef Bacik if (check_ro_option(info, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") || 1030882dbe0cSJosef Bacik check_ro_option(info, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") || 1031882dbe0cSJosef Bacik check_ro_option(info, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums")) 103296da0919SQu Wenruo ret = -EINVAL; 1033a7a3f7caSSage Weil out: 10340b246afaSJeff Mahoney if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) && 10353cdde224SJeff Mahoney !btrfs_test_opt(info, FREE_SPACE_TREE) && 10363cdde224SJeff Mahoney !btrfs_test_opt(info, CLEAR_CACHE)) { 10370b246afaSJeff Mahoney btrfs_err(info, "cannot disable free space tree"); 103870f6d82eSOmar Sandoval ret = -EINVAL; 103970f6d82eSOmar Sandoval 104070f6d82eSOmar Sandoval } 10415d1ab66cSNaohiro Aota if (!ret) 10425d1ab66cSNaohiro Aota ret = btrfs_check_mountopts_zoned(info); 10433cdde224SJeff Mahoney if (!ret && btrfs_test_opt(info, SPACE_CACHE)) 10440b246afaSJeff Mahoney btrfs_info(info, "disk space caching is enabled"); 10453cdde224SJeff Mahoney if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE)) 10460b246afaSJeff Mahoney btrfs_info(info, "using free space tree"); 1047a7a3f7caSSage Weil return ret; 1048edf24abeSChristoph Hellwig } 1049edf24abeSChristoph Hellwig 1050edf24abeSChristoph Hellwig /* 1051edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 1052edf24abeSChristoph Hellwig * 1053edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 1054edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 1055edf24abeSChristoph Hellwig */ 1056fa59f27cSAnand Jain static int btrfs_parse_device_options(const char *options, fmode_t flags, 1057d64dcbd1SGu Jinxiang void *holder) 1058edf24abeSChristoph Hellwig { 1059edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 106083c8c9bdSJeff Liu char *device_name, *opts, *orig, *p; 106136350e95SGu Jinxiang struct btrfs_device *device = NULL; 1062d7407606SMisono, Tomohiro int error = 0; 1063d7407606SMisono, Tomohiro 10645139cff5SDavid Sterba lockdep_assert_held(&uuid_mutex); 10655139cff5SDavid Sterba 1066d7407606SMisono, Tomohiro if (!options) 1067d7407606SMisono, Tomohiro return 0; 1068d7407606SMisono, Tomohiro 1069d7407606SMisono, Tomohiro /* 1070d7407606SMisono, Tomohiro * strsep changes the string, duplicate it because btrfs_parse_options 1071d7407606SMisono, Tomohiro * gets called later 1072d7407606SMisono, Tomohiro */ 1073d7407606SMisono, Tomohiro opts = kstrdup(options, GFP_KERNEL); 1074d7407606SMisono, Tomohiro if (!opts) 1075d7407606SMisono, Tomohiro return -ENOMEM; 1076d7407606SMisono, Tomohiro orig = opts; 1077d7407606SMisono, Tomohiro 1078d7407606SMisono, Tomohiro while ((p = strsep(&opts, ",")) != NULL) { 1079d7407606SMisono, Tomohiro int token; 1080d7407606SMisono, Tomohiro 1081d7407606SMisono, Tomohiro if (!*p) 1082d7407606SMisono, Tomohiro continue; 1083d7407606SMisono, Tomohiro 1084d7407606SMisono, Tomohiro token = match_token(p, tokens, args); 1085d7407606SMisono, Tomohiro if (token == Opt_device) { 1086d7407606SMisono, Tomohiro device_name = match_strdup(&args[0]); 1087d7407606SMisono, Tomohiro if (!device_name) { 1088d7407606SMisono, Tomohiro error = -ENOMEM; 1089d7407606SMisono, Tomohiro goto out; 1090d7407606SMisono, Tomohiro } 109136350e95SGu Jinxiang device = btrfs_scan_one_device(device_name, flags, 109236350e95SGu Jinxiang holder); 1093d7407606SMisono, Tomohiro kfree(device_name); 109436350e95SGu Jinxiang if (IS_ERR(device)) { 109536350e95SGu Jinxiang error = PTR_ERR(device); 1096d7407606SMisono, Tomohiro goto out; 1097d7407606SMisono, Tomohiro } 1098d7407606SMisono, Tomohiro } 109936350e95SGu Jinxiang } 1100d7407606SMisono, Tomohiro 1101d7407606SMisono, Tomohiro out: 1102d7407606SMisono, Tomohiro kfree(orig); 1103d7407606SMisono, Tomohiro return error; 1104d7407606SMisono, Tomohiro } 1105d7407606SMisono, Tomohiro 1106d7407606SMisono, Tomohiro /* 1107d7407606SMisono, Tomohiro * Parse mount options that are related to subvolume id 1108d7407606SMisono, Tomohiro * 1109d7407606SMisono, Tomohiro * The value is later passed to mount_subvol() 1110d7407606SMisono, Tomohiro */ 111193b9bcdfSGu Jinxiang static int btrfs_parse_subvol_options(const char *options, char **subvol_name, 111293b9bcdfSGu Jinxiang u64 *subvol_objectid) 1113d7407606SMisono, Tomohiro { 1114d7407606SMisono, Tomohiro substring_t args[MAX_OPT_ARGS]; 1115d7407606SMisono, Tomohiro char *opts, *orig, *p; 1116edf24abeSChristoph Hellwig int error = 0; 1117ccb0e7d1SAnand Jain u64 subvolid; 1118edf24abeSChristoph Hellwig 1119edf24abeSChristoph Hellwig if (!options) 1120830c4adbSJosef Bacik return 0; 1121edf24abeSChristoph Hellwig 1122edf24abeSChristoph Hellwig /* 1123d7407606SMisono, Tomohiro * strsep changes the string, duplicate it because 1124fa59f27cSAnand Jain * btrfs_parse_device_options gets called later 1125edf24abeSChristoph Hellwig */ 1126edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 1127edf24abeSChristoph Hellwig if (!opts) 1128edf24abeSChristoph Hellwig return -ENOMEM; 11293f3d0bc0STero Roponen orig = opts; 1130edf24abeSChristoph Hellwig 1131edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 1132edf24abeSChristoph Hellwig int token; 1133edf24abeSChristoph Hellwig if (!*p) 1134edf24abeSChristoph Hellwig continue; 1135edf24abeSChristoph Hellwig 1136edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 1137edf24abeSChristoph Hellwig switch (token) { 1138edf24abeSChristoph Hellwig case Opt_subvol: 1139a90e8b6fSIlya Dryomov kfree(*subvol_name); 1140edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 11412c334e87SWang Shilong if (!*subvol_name) { 11422c334e87SWang Shilong error = -ENOMEM; 11432c334e87SWang Shilong goto out; 11442c334e87SWang Shilong } 1145edf24abeSChristoph Hellwig break; 114673f73415SJosef Bacik case Opt_subvolid: 1147ccb0e7d1SAnand Jain error = match_u64(&args[0], &subvolid); 1148ccb0e7d1SAnand Jain if (error) 11492c334e87SWang Shilong goto out; 1150ccb0e7d1SAnand Jain 1151ccb0e7d1SAnand Jain /* we want the original fs_tree */ 1152ccb0e7d1SAnand Jain if (subvolid == 0) 1153ccb0e7d1SAnand Jain subvolid = BTRFS_FS_TREE_OBJECTID; 1154ccb0e7d1SAnand Jain 1155ccb0e7d1SAnand Jain *subvol_objectid = subvolid; 115673f73415SJosef Bacik break; 1157edf24abeSChristoph Hellwig default: 1158edf24abeSChristoph Hellwig break; 1159edf24abeSChristoph Hellwig } 1160edf24abeSChristoph Hellwig } 1161edf24abeSChristoph Hellwig 1162edf24abeSChristoph Hellwig out: 1163830c4adbSJosef Bacik kfree(orig); 1164edf24abeSChristoph Hellwig return error; 116595e05289SChris Mason } 116695e05289SChris Mason 1167c0c907a4SMarcos Paulo de Souza char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info, 116873f73415SJosef Bacik u64 subvol_objectid) 116973f73415SJosef Bacik { 1170815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 11715168489aSJosef Bacik struct btrfs_root *fs_root = NULL; 117205dbe683SOmar Sandoval struct btrfs_root_ref *root_ref; 117305dbe683SOmar Sandoval struct btrfs_inode_ref *inode_ref; 117405dbe683SOmar Sandoval struct btrfs_key key; 117505dbe683SOmar Sandoval struct btrfs_path *path = NULL; 117605dbe683SOmar Sandoval char *name = NULL, *ptr; 117705dbe683SOmar Sandoval u64 dirid; 117805dbe683SOmar Sandoval int len; 117905dbe683SOmar Sandoval int ret; 118005dbe683SOmar Sandoval 118105dbe683SOmar Sandoval path = btrfs_alloc_path(); 118205dbe683SOmar Sandoval if (!path) { 118305dbe683SOmar Sandoval ret = -ENOMEM; 118405dbe683SOmar Sandoval goto err; 118505dbe683SOmar Sandoval } 118605dbe683SOmar Sandoval 11873ec83621SDavid Sterba name = kmalloc(PATH_MAX, GFP_KERNEL); 118805dbe683SOmar Sandoval if (!name) { 118905dbe683SOmar Sandoval ret = -ENOMEM; 119005dbe683SOmar Sandoval goto err; 119105dbe683SOmar Sandoval } 119205dbe683SOmar Sandoval ptr = name + PATH_MAX - 1; 119305dbe683SOmar Sandoval ptr[0] = '\0'; 119405dbe683SOmar Sandoval 119505dbe683SOmar Sandoval /* 119605dbe683SOmar Sandoval * Walk up the subvolume trees in the tree of tree roots by root 119705dbe683SOmar Sandoval * backrefs until we hit the top-level subvolume. 119805dbe683SOmar Sandoval */ 119905dbe683SOmar Sandoval while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) { 120005dbe683SOmar Sandoval key.objectid = subvol_objectid; 120105dbe683SOmar Sandoval key.type = BTRFS_ROOT_BACKREF_KEY; 120205dbe683SOmar Sandoval key.offset = (u64)-1; 120305dbe683SOmar Sandoval 12040ff40a91SMarcos Paulo de Souza ret = btrfs_search_backwards(root, &key, path); 120505dbe683SOmar Sandoval if (ret < 0) { 120605dbe683SOmar Sandoval goto err; 120705dbe683SOmar Sandoval } else if (ret > 0) { 120805dbe683SOmar Sandoval ret = -ENOENT; 120905dbe683SOmar Sandoval goto err; 121005dbe683SOmar Sandoval } 121105dbe683SOmar Sandoval 121205dbe683SOmar Sandoval subvol_objectid = key.offset; 121305dbe683SOmar Sandoval 121405dbe683SOmar Sandoval root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], 121505dbe683SOmar Sandoval struct btrfs_root_ref); 121605dbe683SOmar Sandoval len = btrfs_root_ref_name_len(path->nodes[0], root_ref); 121705dbe683SOmar Sandoval ptr -= len + 1; 121805dbe683SOmar Sandoval if (ptr < name) { 121905dbe683SOmar Sandoval ret = -ENAMETOOLONG; 122005dbe683SOmar Sandoval goto err; 122105dbe683SOmar Sandoval } 122205dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 122305dbe683SOmar Sandoval (unsigned long)(root_ref + 1), len); 122405dbe683SOmar Sandoval ptr[0] = '/'; 122505dbe683SOmar Sandoval dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref); 122605dbe683SOmar Sandoval btrfs_release_path(path); 122705dbe683SOmar Sandoval 122856e9357aSDavid Sterba fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true); 122905dbe683SOmar Sandoval if (IS_ERR(fs_root)) { 123005dbe683SOmar Sandoval ret = PTR_ERR(fs_root); 12315168489aSJosef Bacik fs_root = NULL; 12325168489aSJosef Bacik goto err; 12335168489aSJosef Bacik } 123405dbe683SOmar Sandoval 123505dbe683SOmar Sandoval /* 123605dbe683SOmar Sandoval * Walk up the filesystem tree by inode refs until we hit the 123705dbe683SOmar Sandoval * root directory. 123805dbe683SOmar Sandoval */ 123905dbe683SOmar Sandoval while (dirid != BTRFS_FIRST_FREE_OBJECTID) { 124005dbe683SOmar Sandoval key.objectid = dirid; 124105dbe683SOmar Sandoval key.type = BTRFS_INODE_REF_KEY; 124205dbe683SOmar Sandoval key.offset = (u64)-1; 124305dbe683SOmar Sandoval 12440ff40a91SMarcos Paulo de Souza ret = btrfs_search_backwards(fs_root, &key, path); 124505dbe683SOmar Sandoval if (ret < 0) { 124605dbe683SOmar Sandoval goto err; 124705dbe683SOmar Sandoval } else if (ret > 0) { 124805dbe683SOmar Sandoval ret = -ENOENT; 124905dbe683SOmar Sandoval goto err; 125005dbe683SOmar Sandoval } 125105dbe683SOmar Sandoval 125205dbe683SOmar Sandoval dirid = key.offset; 125305dbe683SOmar Sandoval 125405dbe683SOmar Sandoval inode_ref = btrfs_item_ptr(path->nodes[0], 125505dbe683SOmar Sandoval path->slots[0], 125605dbe683SOmar Sandoval struct btrfs_inode_ref); 125705dbe683SOmar Sandoval len = btrfs_inode_ref_name_len(path->nodes[0], 125805dbe683SOmar Sandoval inode_ref); 125905dbe683SOmar Sandoval ptr -= len + 1; 126005dbe683SOmar Sandoval if (ptr < name) { 126105dbe683SOmar Sandoval ret = -ENAMETOOLONG; 126205dbe683SOmar Sandoval goto err; 126305dbe683SOmar Sandoval } 126405dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 126505dbe683SOmar Sandoval (unsigned long)(inode_ref + 1), len); 126605dbe683SOmar Sandoval ptr[0] = '/'; 126705dbe683SOmar Sandoval btrfs_release_path(path); 126805dbe683SOmar Sandoval } 126900246528SJosef Bacik btrfs_put_root(fs_root); 12705168489aSJosef Bacik fs_root = NULL; 127105dbe683SOmar Sandoval } 127205dbe683SOmar Sandoval 127305dbe683SOmar Sandoval btrfs_free_path(path); 127405dbe683SOmar Sandoval if (ptr == name + PATH_MAX - 1) { 127505dbe683SOmar Sandoval name[0] = '/'; 127605dbe683SOmar Sandoval name[1] = '\0'; 127705dbe683SOmar Sandoval } else { 127805dbe683SOmar Sandoval memmove(name, ptr, name + PATH_MAX - ptr); 127905dbe683SOmar Sandoval } 128005dbe683SOmar Sandoval return name; 128105dbe683SOmar Sandoval 128205dbe683SOmar Sandoval err: 128300246528SJosef Bacik btrfs_put_root(fs_root); 128405dbe683SOmar Sandoval btrfs_free_path(path); 128505dbe683SOmar Sandoval kfree(name); 128605dbe683SOmar Sandoval return ERR_PTR(ret); 128705dbe683SOmar Sandoval } 128805dbe683SOmar Sandoval 128905dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid) 129005dbe683SOmar Sandoval { 129105dbe683SOmar Sandoval struct btrfs_root *root = fs_info->tree_root; 129273f73415SJosef Bacik struct btrfs_dir_item *di; 129373f73415SJosef Bacik struct btrfs_path *path; 129473f73415SJosef Bacik struct btrfs_key location; 129573f73415SJosef Bacik u64 dir_id; 129673f73415SJosef Bacik 129773f73415SJosef Bacik path = btrfs_alloc_path(); 129873f73415SJosef Bacik if (!path) 129905dbe683SOmar Sandoval return -ENOMEM; 130073f73415SJosef Bacik 130173f73415SJosef Bacik /* 130273f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 130373f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 130473f73415SJosef Bacik * to mount. 130573f73415SJosef Bacik */ 1306815745cfSAl Viro dir_id = btrfs_super_root_dir(fs_info->super_copy); 130773f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 1308b0839166SJulia Lawall if (IS_ERR(di)) { 1309b0839166SJulia Lawall btrfs_free_path(path); 131005dbe683SOmar Sandoval return PTR_ERR(di); 1311b0839166SJulia Lawall } 131273f73415SJosef Bacik if (!di) { 131373f73415SJosef Bacik /* 131473f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 131573f73415SJosef Bacik * it's always been there, but don't freak out, just try and 131605dbe683SOmar Sandoval * mount the top-level subvolume. 131773f73415SJosef Bacik */ 131873f73415SJosef Bacik btrfs_free_path(path); 131905dbe683SOmar Sandoval *objectid = BTRFS_FS_TREE_OBJECTID; 132005dbe683SOmar Sandoval return 0; 132173f73415SJosef Bacik } 132273f73415SJosef Bacik 132373f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 132473f73415SJosef Bacik btrfs_free_path(path); 132505dbe683SOmar Sandoval *objectid = location.objectid; 132605dbe683SOmar Sandoval return 0; 132773f73415SJosef Bacik } 132873f73415SJosef Bacik 13298a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 13308a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 133156e033a7SDavid Sterba void *data) 13322e635a27SChris Mason { 13332e635a27SChris Mason struct inode *inode; 1334815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 133539279cc3SChris Mason int err; 13362e635a27SChris Mason 13372e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 13382e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 1339e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 1340af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 1341be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 134214605409SBoris Burkov #ifdef CONFIG_FS_VERITY 134314605409SBoris Burkov sb->s_vop = &btrfs_verityops; 134414605409SBoris Burkov #endif 13455103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 13462e635a27SChris Mason sb->s_time_gran = 1; 13470eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 13481751e8a6SLinus Torvalds sb->s_flags |= SB_POSIXACL; 134949cf6f45SChris Ball #endif 1350357fdad0SMatthew Garrett sb->s_flags |= SB_I_VERSION; 1351da2f0f74SChris Mason sb->s_iflags |= SB_I_CGROUPWB; 13529e11ceeeSJan Kara 13539e11ceeeSJan Kara err = super_setup_bdi(sb); 13549e11ceeeSJan Kara if (err) { 13559e11ceeeSJan Kara btrfs_err(fs_info, "super_setup_bdi failed"); 13569e11ceeeSJan Kara return err; 13579e11ceeeSJan Kara } 13589e11ceeeSJan Kara 1359ad2b2c80SAl Viro err = open_ctree(sb, fs_devices, (char *)data); 1360ad2b2c80SAl Viro if (err) { 1361ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "open_ctree failed"); 1362ad2b2c80SAl Viro return err; 1363e20d96d6SChris Mason } 1364b888db2bSChris Mason 13650202e83fSDavid Sterba inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root); 13665d4f98a2SYan Zheng if (IS_ERR(inode)) { 13675d4f98a2SYan Zheng err = PTR_ERR(inode); 136839279cc3SChris Mason goto fail_close; 136939279cc3SChris Mason } 13702e635a27SChris Mason 137148fde701SAl Viro sb->s_root = d_make_root(inode); 137248fde701SAl Viro if (!sb->s_root) { 137339279cc3SChris Mason err = -ENOMEM; 137439279cc3SChris Mason goto fail_close; 13752e635a27SChris Mason } 137658176a96SJosef Bacik 137790a887c9SDan Magenheimer cleancache_init_fs(sb); 13781751e8a6SLinus Torvalds sb->s_flags |= SB_ACTIVE; 13792e635a27SChris Mason return 0; 13802e635a27SChris Mason 138139279cc3SChris Mason fail_close: 13826bccf3abSJeff Mahoney close_ctree(fs_info); 1383d5719762SChris Mason return err; 1384d5719762SChris Mason } 1385d5719762SChris Mason 13866bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 1387d5719762SChris Mason { 1388d5719762SChris Mason struct btrfs_trans_handle *trans; 1389815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1390815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 1391df2ce34cSChris Mason 1392bc074524SJeff Mahoney trace_btrfs_sync_fs(fs_info, wait); 13931abe9b8aSliubo 1394d561c025SChris Mason if (!wait) { 1395815745cfSAl Viro filemap_flush(fs_info->btree_inode->i_mapping); 1396df2ce34cSChris Mason return 0; 1397d561c025SChris Mason } 1398771ed689SChris Mason 13996374e57aSChris Mason btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 1400771ed689SChris Mason 1401d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 140260376ce4SJosef Bacik if (IS_ERR(trans)) { 1403354aa0fbSMiao Xie /* no transaction, don't bother */ 14046b5fe46dSDavid Sterba if (PTR_ERR(trans) == -ENOENT) { 14056b5fe46dSDavid Sterba /* 14066b5fe46dSDavid Sterba * Exit unless we have some pending changes 14076b5fe46dSDavid Sterba * that need to go through commit 14086b5fe46dSDavid Sterba */ 14096b5fe46dSDavid Sterba if (fs_info->pending_changes == 0) 1410bd7de2c9SJosef Bacik return 0; 1411a53f4f8eSQu Wenruo /* 1412a53f4f8eSQu Wenruo * A non-blocking test if the fs is frozen. We must not 1413a53f4f8eSQu Wenruo * start a new transaction here otherwise a deadlock 1414a53f4f8eSQu Wenruo * happens. The pending operations are delayed to the 1415a53f4f8eSQu Wenruo * next commit after thawing. 1416a53f4f8eSQu Wenruo */ 1417a7e3c5f2SRakesh Pandit if (sb_start_write_trylock(sb)) 1418a7e3c5f2SRakesh Pandit sb_end_write(sb); 1419a53f4f8eSQu Wenruo else 1420a53f4f8eSQu Wenruo return 0; 14216b5fe46dSDavid Sterba trans = btrfs_start_transaction(root, 0); 142260376ce4SJosef Bacik } 142398bd5c54SDavid Sterba if (IS_ERR(trans)) 142498bd5c54SDavid Sterba return PTR_ERR(trans); 14256b5fe46dSDavid Sterba } 14263a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 1427d5719762SChris Mason } 1428d5719762SChris Mason 1429ab0b4a3eSJosef Bacik static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed) 1430ab0b4a3eSJosef Bacik { 1431ab0b4a3eSJosef Bacik seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s); 1432ab0b4a3eSJosef Bacik *printed = true; 1433ab0b4a3eSJosef Bacik } 1434ab0b4a3eSJosef Bacik 143534c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 1436a9572a15SEric Paris { 1437815745cfSAl Viro struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 14380f628c63SDavid Sterba const char *compress_type; 14393ef3959bSJosef Bacik const char *subvol_name; 1440ab0b4a3eSJosef Bacik bool printed = false; 1441a9572a15SEric Paris 14423cdde224SJeff Mahoney if (btrfs_test_opt(info, DEGRADED)) 1443a9572a15SEric Paris seq_puts(seq, ",degraded"); 14443cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATASUM)) 1445a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 14463cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATACOW)) 1447a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 14483cdde224SJeff Mahoney if (btrfs_test_opt(info, NOBARRIER)) 1449a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 145095ac567aSFilipe David Borba Manana if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE) 1451c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",max_inline=%llu", info->max_inline); 1452a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 1453a9572a15SEric Paris num_online_cpus() + 2, 8)) 1454f7b885beSAnand Jain seq_printf(seq, ",thread_pool=%u", info->thread_pool_size); 14553cdde224SJeff Mahoney if (btrfs_test_opt(info, COMPRESS)) { 14560f628c63SDavid Sterba compress_type = btrfs_compress_type2str(info->compress_type); 14573cdde224SJeff Mahoney if (btrfs_test_opt(info, FORCE_COMPRESS)) 1458200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 1459200da64eSTsutomu Itoh else 1460200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 1461f51d2b59SDavid Sterba if (info->compress_level) 1462fa4d885aSAdam Borowski seq_printf(seq, ":%d", info->compress_level); 1463200da64eSTsutomu Itoh } 14643cdde224SJeff Mahoney if (btrfs_test_opt(info, NOSSD)) 1465c289811cSChris Mason seq_puts(seq, ",nossd"); 14663cdde224SJeff Mahoney if (btrfs_test_opt(info, SSD_SPREAD)) 1467451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 14683cdde224SJeff Mahoney else if (btrfs_test_opt(info, SSD)) 1469a9572a15SEric Paris seq_puts(seq, ",ssd"); 14703cdde224SJeff Mahoney if (btrfs_test_opt(info, NOTREELOG)) 14716b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 14723cdde224SJeff Mahoney if (btrfs_test_opt(info, NOLOGREPLAY)) 1473ab0b4a3eSJosef Bacik print_rescue_option(seq, "nologreplay", &printed); 147468319c18SJosef Bacik if (btrfs_test_opt(info, USEBACKUPROOT)) 147568319c18SJosef Bacik print_rescue_option(seq, "usebackuproot", &printed); 147642437a63SJosef Bacik if (btrfs_test_opt(info, IGNOREBADROOTS)) 147742437a63SJosef Bacik print_rescue_option(seq, "ignorebadroots", &printed); 1478882dbe0cSJosef Bacik if (btrfs_test_opt(info, IGNOREDATACSUMS)) 1479882dbe0cSJosef Bacik print_rescue_option(seq, "ignoredatacsums", &printed); 14803cdde224SJeff Mahoney if (btrfs_test_opt(info, FLUSHONCOMMIT)) 14816b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 148246b27f50SDennis Zhou if (btrfs_test_opt(info, DISCARD_SYNC)) 148320a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 1484b0643e59SDennis Zhou if (btrfs_test_opt(info, DISCARD_ASYNC)) 1485b0643e59SDennis Zhou seq_puts(seq, ",discard=async"); 14861751e8a6SLinus Torvalds if (!(info->sb->s_flags & SB_POSIXACL)) 1487a9572a15SEric Paris seq_puts(seq, ",noacl"); 148804c41559SBoris Burkov if (btrfs_free_space_cache_v1_active(info)) 1489200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 149004c41559SBoris Burkov else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) 149170f6d82eSOmar Sandoval seq_puts(seq, ",space_cache=v2"); 149273bc1876SJosef Bacik else 14938965593eSDavid Sterba seq_puts(seq, ",nospace_cache"); 14943cdde224SJeff Mahoney if (btrfs_test_opt(info, RESCAN_UUID_TREE)) 1495f420ee1eSStefan Behrens seq_puts(seq, ",rescan_uuid_tree"); 14963cdde224SJeff Mahoney if (btrfs_test_opt(info, CLEAR_CACHE)) 1497200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 14983cdde224SJeff Mahoney if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED)) 1499200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 15003cdde224SJeff Mahoney if (btrfs_test_opt(info, ENOSPC_DEBUG)) 15010942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 15023cdde224SJeff Mahoney if (btrfs_test_opt(info, AUTO_DEFRAG)) 15030942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 15043cdde224SJeff Mahoney if (btrfs_test_opt(info, SKIP_BALANCE)) 15059555c6c1SIlya Dryomov seq_puts(seq, ",skip_balance"); 15068507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 1507cbeaae4fSDavid Sterba if (btrfs_test_opt(info, CHECK_INTEGRITY_DATA)) 15088507d216SWang Shilong seq_puts(seq, ",check_int_data"); 15093cdde224SJeff Mahoney else if (btrfs_test_opt(info, CHECK_INTEGRITY)) 15108507d216SWang Shilong seq_puts(seq, ",check_int"); 15118507d216SWang Shilong if (info->check_integrity_print_mask) 15128507d216SWang Shilong seq_printf(seq, ",check_int_print_mask=%d", 15138507d216SWang Shilong info->check_integrity_print_mask); 15148507d216SWang Shilong #endif 15158507d216SWang Shilong if (info->metadata_ratio) 1516764cb8b4SAnand Jain seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio); 15173cdde224SJeff Mahoney if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR)) 15188c342930SJeff Mahoney seq_puts(seq, ",fatal_errors=panic"); 15198b87dc17SDavid Sterba if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL) 1520d3740608SAnand Jain seq_printf(seq, ",commit=%u", info->commit_interval); 1521d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 15223cdde224SJeff Mahoney if (btrfs_test_opt(info, FRAGMENT_DATA)) 1523d0bd4560SJosef Bacik seq_puts(seq, ",fragment=data"); 15243cdde224SJeff Mahoney if (btrfs_test_opt(info, FRAGMENT_METADATA)) 1525d0bd4560SJosef Bacik seq_puts(seq, ",fragment=metadata"); 1526d0bd4560SJosef Bacik #endif 1527fb592373SJosef Bacik if (btrfs_test_opt(info, REF_VERIFY)) 1528fb592373SJosef Bacik seq_puts(seq, ",ref_verify"); 1529c8d3fe02SOmar Sandoval seq_printf(seq, ",subvolid=%llu", 1530c8d3fe02SOmar Sandoval BTRFS_I(d_inode(dentry))->root->root_key.objectid); 15313ef3959bSJosef Bacik subvol_name = btrfs_get_subvol_name_from_objectid(info, 15323ef3959bSJosef Bacik BTRFS_I(d_inode(dentry))->root->root_key.objectid); 15333ef3959bSJosef Bacik if (!IS_ERR(subvol_name)) { 1534c8d3fe02SOmar Sandoval seq_puts(seq, ",subvol="); 15353ef3959bSJosef Bacik seq_escape(seq, subvol_name, " \t\n\\"); 15363ef3959bSJosef Bacik kfree(subvol_name); 15373ef3959bSJosef Bacik } 1538a9572a15SEric Paris return 0; 1539a9572a15SEric Paris } 1540a9572a15SEric Paris 1541a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 15422e635a27SChris Mason { 1543815745cfSAl Viro struct btrfs_fs_info *p = data; 1544815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(s); 15454b82d6e4SYan 1546815745cfSAl Viro return fs_info->fs_devices == p->fs_devices; 15474b82d6e4SYan } 15484b82d6e4SYan 1549450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 1550450ba0eaSJosef Bacik { 15516de1d09dSAl Viro int err = set_anon_super(s, data); 15526de1d09dSAl Viro if (!err) 1553450ba0eaSJosef Bacik s->s_fs_info = data; 15546de1d09dSAl Viro return err; 1555450ba0eaSJosef Bacik } 1556450ba0eaSJosef Bacik 1557830c4adbSJosef Bacik /* 1558f9d9ef62SDavid Sterba * subvolumes are identified by ino 256 1559f9d9ef62SDavid Sterba */ 1560f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode) 1561f9d9ef62SDavid Sterba { 1562f9d9ef62SDavid Sterba if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 1563f9d9ef62SDavid Sterba return 1; 1564f9d9ef62SDavid Sterba return 0; 1565f9d9ef62SDavid Sterba } 1566f9d9ef62SDavid Sterba 1567bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid, 1568ae0bc863SAnand Jain struct vfsmount *mnt) 1569830c4adbSJosef Bacik { 1570830c4adbSJosef Bacik struct dentry *root; 1571fa330659SOmar Sandoval int ret; 1572830c4adbSJosef Bacik 157305dbe683SOmar Sandoval if (!subvol_name) { 157405dbe683SOmar Sandoval if (!subvol_objectid) { 157505dbe683SOmar Sandoval ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb), 157605dbe683SOmar Sandoval &subvol_objectid); 157705dbe683SOmar Sandoval if (ret) { 157805dbe683SOmar Sandoval root = ERR_PTR(ret); 157905dbe683SOmar Sandoval goto out; 158005dbe683SOmar Sandoval } 158105dbe683SOmar Sandoval } 1582c0c907a4SMarcos Paulo de Souza subvol_name = btrfs_get_subvol_name_from_objectid( 1583c0c907a4SMarcos Paulo de Souza btrfs_sb(mnt->mnt_sb), subvol_objectid); 158405dbe683SOmar Sandoval if (IS_ERR(subvol_name)) { 158505dbe683SOmar Sandoval root = ERR_CAST(subvol_name); 158605dbe683SOmar Sandoval subvol_name = NULL; 158705dbe683SOmar Sandoval goto out; 158805dbe683SOmar Sandoval } 158905dbe683SOmar Sandoval 159005dbe683SOmar Sandoval } 159105dbe683SOmar Sandoval 1592ea441d11SAl Viro root = mount_subtree(mnt, subvol_name); 1593fa330659SOmar Sandoval /* mount_subtree() drops our reference on the vfsmount. */ 1594fa330659SOmar Sandoval mnt = NULL; 1595830c4adbSJosef Bacik 1596bb289b7bSOmar Sandoval if (!IS_ERR(root)) { 1597ea441d11SAl Viro struct super_block *s = root->d_sb; 1598ab8d0fc4SJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(s); 1599bb289b7bSOmar Sandoval struct inode *root_inode = d_inode(root); 1600bb289b7bSOmar Sandoval u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid; 1601bb289b7bSOmar Sandoval 1602bb289b7bSOmar Sandoval ret = 0; 1603bb289b7bSOmar Sandoval if (!is_subvolume_inode(root_inode)) { 1604ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "'%s' is not a valid subvolume", 1605bb289b7bSOmar Sandoval subvol_name); 1606bb289b7bSOmar Sandoval ret = -EINVAL; 1607bb289b7bSOmar Sandoval } 1608bb289b7bSOmar Sandoval if (subvol_objectid && root_objectid != subvol_objectid) { 160905dbe683SOmar Sandoval /* 161005dbe683SOmar Sandoval * This will also catch a race condition where a 161105dbe683SOmar Sandoval * subvolume which was passed by ID is renamed and 161205dbe683SOmar Sandoval * another subvolume is renamed over the old location. 161305dbe683SOmar Sandoval */ 1614ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 1615ab8d0fc4SJeff Mahoney "subvol '%s' does not match subvolid %llu", 1616bb289b7bSOmar Sandoval subvol_name, subvol_objectid); 1617bb289b7bSOmar Sandoval ret = -EINVAL; 1618bb289b7bSOmar Sandoval } 1619bb289b7bSOmar Sandoval if (ret) { 1620ea441d11SAl Viro dput(root); 1621bb289b7bSOmar Sandoval root = ERR_PTR(ret); 1622ea441d11SAl Viro deactivate_locked_super(s); 1623bb289b7bSOmar Sandoval } 1624f9d9ef62SDavid Sterba } 1625f9d9ef62SDavid Sterba 1626fa330659SOmar Sandoval out: 1627fa330659SOmar Sandoval mntput(mnt); 1628fa330659SOmar Sandoval kfree(subvol_name); 1629830c4adbSJosef Bacik return root; 1630830c4adbSJosef Bacik } 1631450ba0eaSJosef Bacik 1632312c89fbSMisono, Tomohiro /* 1633312c89fbSMisono, Tomohiro * Find a superblock for the given device / mount point. 1634312c89fbSMisono, Tomohiro * 1635312c89fbSMisono, Tomohiro * Note: This is based on mount_bdev from fs/super.c with a few additions 1636312c89fbSMisono, Tomohiro * for multiple device setup. Make sure to keep it in sync. 1637312c89fbSMisono, Tomohiro */ 163872fa39f5SMisono, Tomohiro static struct dentry *btrfs_mount_root(struct file_system_type *fs_type, 163972fa39f5SMisono, Tomohiro int flags, const char *device_name, void *data) 164072fa39f5SMisono, Tomohiro { 164172fa39f5SMisono, Tomohiro struct block_device *bdev = NULL; 164272fa39f5SMisono, Tomohiro struct super_block *s; 164336350e95SGu Jinxiang struct btrfs_device *device = NULL; 164472fa39f5SMisono, Tomohiro struct btrfs_fs_devices *fs_devices = NULL; 164572fa39f5SMisono, Tomohiro struct btrfs_fs_info *fs_info = NULL; 1646204cc0ccSAl Viro void *new_sec_opts = NULL; 164772fa39f5SMisono, Tomohiro fmode_t mode = FMODE_READ; 164872fa39f5SMisono, Tomohiro int error = 0; 164972fa39f5SMisono, Tomohiro 165072fa39f5SMisono, Tomohiro if (!(flags & SB_RDONLY)) 165172fa39f5SMisono, Tomohiro mode |= FMODE_WRITE; 165272fa39f5SMisono, Tomohiro 165372fa39f5SMisono, Tomohiro if (data) { 1654a65001e8SAl Viro error = security_sb_eat_lsm_opts(data, &new_sec_opts); 165572fa39f5SMisono, Tomohiro if (error) 165672fa39f5SMisono, Tomohiro return ERR_PTR(error); 165772fa39f5SMisono, Tomohiro } 165872fa39f5SMisono, Tomohiro 165972fa39f5SMisono, Tomohiro /* 166072fa39f5SMisono, Tomohiro * Setup a dummy root and fs_info for test/set super. This is because 166172fa39f5SMisono, Tomohiro * we don't actually fill this stuff out until open_ctree, but we need 16628260edbaSJosef Bacik * then open_ctree will properly initialize the file system specific 16638260edbaSJosef Bacik * settings later. btrfs_init_fs_info initializes the static elements 16648260edbaSJosef Bacik * of the fs_info (locks and such) to make cleanup easier if we find a 16658260edbaSJosef Bacik * superblock with our given fs_devices later on at sget() time. 166672fa39f5SMisono, Tomohiro */ 1667a8fd1f71SJeff Mahoney fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL); 166872fa39f5SMisono, Tomohiro if (!fs_info) { 166972fa39f5SMisono, Tomohiro error = -ENOMEM; 167072fa39f5SMisono, Tomohiro goto error_sec_opts; 167172fa39f5SMisono, Tomohiro } 16728260edbaSJosef Bacik btrfs_init_fs_info(fs_info); 167372fa39f5SMisono, Tomohiro 167472fa39f5SMisono, Tomohiro fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL); 167572fa39f5SMisono, Tomohiro fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL); 167672fa39f5SMisono, Tomohiro if (!fs_info->super_copy || !fs_info->super_for_commit) { 167772fa39f5SMisono, Tomohiro error = -ENOMEM; 167872fa39f5SMisono, Tomohiro goto error_fs_info; 167972fa39f5SMisono, Tomohiro } 168072fa39f5SMisono, Tomohiro 1681f5194e34SDavid Sterba mutex_lock(&uuid_mutex); 1682fa59f27cSAnand Jain error = btrfs_parse_device_options(data, mode, fs_type); 168381ffd56bSDavid Sterba if (error) { 1684399f7f4cSDavid Sterba mutex_unlock(&uuid_mutex); 1685399f7f4cSDavid Sterba goto error_fs_info; 168681ffd56bSDavid Sterba } 1687399f7f4cSDavid Sterba 168836350e95SGu Jinxiang device = btrfs_scan_one_device(device_name, mode, fs_type); 168936350e95SGu Jinxiang if (IS_ERR(device)) { 1690399f7f4cSDavid Sterba mutex_unlock(&uuid_mutex); 169136350e95SGu Jinxiang error = PTR_ERR(device); 1692399f7f4cSDavid Sterba goto error_fs_info; 169381ffd56bSDavid Sterba } 1694399f7f4cSDavid Sterba 169536350e95SGu Jinxiang fs_devices = device->fs_devices; 1696399f7f4cSDavid Sterba fs_info->fs_devices = fs_devices; 1697399f7f4cSDavid Sterba 169872fa39f5SMisono, Tomohiro error = btrfs_open_devices(fs_devices, mode, fs_type); 1699f5194e34SDavid Sterba mutex_unlock(&uuid_mutex); 170072fa39f5SMisono, Tomohiro if (error) 170172fa39f5SMisono, Tomohiro goto error_fs_info; 170272fa39f5SMisono, Tomohiro 170372fa39f5SMisono, Tomohiro if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) { 170472fa39f5SMisono, Tomohiro error = -EACCES; 170572fa39f5SMisono, Tomohiro goto error_close_devices; 170672fa39f5SMisono, Tomohiro } 170772fa39f5SMisono, Tomohiro 1708d24fa5c1SAnand Jain bdev = fs_devices->latest_dev->bdev; 170972fa39f5SMisono, Tomohiro s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC, 171072fa39f5SMisono, Tomohiro fs_info); 171172fa39f5SMisono, Tomohiro if (IS_ERR(s)) { 171272fa39f5SMisono, Tomohiro error = PTR_ERR(s); 171372fa39f5SMisono, Tomohiro goto error_close_devices; 171472fa39f5SMisono, Tomohiro } 171572fa39f5SMisono, Tomohiro 171672fa39f5SMisono, Tomohiro if (s->s_root) { 171772fa39f5SMisono, Tomohiro btrfs_close_devices(fs_devices); 17180d4b0463SJosef Bacik btrfs_free_fs_info(fs_info); 171972fa39f5SMisono, Tomohiro if ((flags ^ s->s_flags) & SB_RDONLY) 172072fa39f5SMisono, Tomohiro error = -EBUSY; 172172fa39f5SMisono, Tomohiro } else { 172272fa39f5SMisono, Tomohiro snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev); 172372fa39f5SMisono, Tomohiro btrfs_sb(s)->bdev_holder = fs_type; 17249b4e675aSDavid Sterba if (!strstr(crc32c_impl(), "generic")) 17259b4e675aSDavid Sterba set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags); 172672fa39f5SMisono, Tomohiro error = btrfs_fill_super(s, fs_devices, data); 172772fa39f5SMisono, Tomohiro } 1728a65001e8SAl Viro if (!error) 1729204cc0ccSAl Viro error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL); 1730a65001e8SAl Viro security_free_mnt_opts(&new_sec_opts); 173172fa39f5SMisono, Tomohiro if (error) { 173272fa39f5SMisono, Tomohiro deactivate_locked_super(s); 1733a65001e8SAl Viro return ERR_PTR(error); 173472fa39f5SMisono, Tomohiro } 173572fa39f5SMisono, Tomohiro 173672fa39f5SMisono, Tomohiro return dget(s->s_root); 173772fa39f5SMisono, Tomohiro 173872fa39f5SMisono, Tomohiro error_close_devices: 173972fa39f5SMisono, Tomohiro btrfs_close_devices(fs_devices); 174072fa39f5SMisono, Tomohiro error_fs_info: 17410d4b0463SJosef Bacik btrfs_free_fs_info(fs_info); 174272fa39f5SMisono, Tomohiro error_sec_opts: 174372fa39f5SMisono, Tomohiro security_free_mnt_opts(&new_sec_opts); 174472fa39f5SMisono, Tomohiro return ERR_PTR(error); 174572fa39f5SMisono, Tomohiro } 1746312c89fbSMisono, Tomohiro 1747edf24abeSChristoph Hellwig /* 1748312c89fbSMisono, Tomohiro * Mount function which is called by VFS layer. 1749edf24abeSChristoph Hellwig * 1750312c89fbSMisono, Tomohiro * In order to allow mounting a subvolume directly, btrfs uses mount_subtree() 1751312c89fbSMisono, Tomohiro * which needs vfsmount* of device's root (/). This means device's root has to 1752312c89fbSMisono, Tomohiro * be mounted internally in any case. 1753312c89fbSMisono, Tomohiro * 1754312c89fbSMisono, Tomohiro * Operation flow: 1755312c89fbSMisono, Tomohiro * 1. Parse subvol id related options for later use in mount_subvol(). 1756312c89fbSMisono, Tomohiro * 1757312c89fbSMisono, Tomohiro * 2. Mount device's root (/) by calling vfs_kern_mount(). 1758312c89fbSMisono, Tomohiro * 1759312c89fbSMisono, Tomohiro * NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the 1760312c89fbSMisono, Tomohiro * first place. In order to avoid calling btrfs_mount() again, we use 1761312c89fbSMisono, Tomohiro * different file_system_type which is not registered to VFS by 1762312c89fbSMisono, Tomohiro * register_filesystem() (btrfs_root_fs_type). As a result, 1763312c89fbSMisono, Tomohiro * btrfs_mount_root() is called. The return value will be used by 1764312c89fbSMisono, Tomohiro * mount_subtree() in mount_subvol(). 1765312c89fbSMisono, Tomohiro * 1766312c89fbSMisono, Tomohiro * 3. Call mount_subvol() to get the dentry of subvolume. Since there is 1767312c89fbSMisono, Tomohiro * "btrfs subvolume set-default", mount_subvol() is called always. 1768edf24abeSChristoph Hellwig */ 1769061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1770306e16ceSDavid Sterba const char *device_name, void *data) 17714b82d6e4SYan { 1772312c89fbSMisono, Tomohiro struct vfsmount *mnt_root; 1773312c89fbSMisono, Tomohiro struct dentry *root; 177473f73415SJosef Bacik char *subvol_name = NULL; 177573f73415SJosef Bacik u64 subvol_objectid = 0; 17764b82d6e4SYan int error = 0; 17774b82d6e4SYan 177893b9bcdfSGu Jinxiang error = btrfs_parse_subvol_options(data, &subvol_name, 177993b9bcdfSGu Jinxiang &subvol_objectid); 1780f23c8af8SIlya Dryomov if (error) { 1781f23c8af8SIlya Dryomov kfree(subvol_name); 1782061dbc6bSAl Viro return ERR_PTR(error); 1783f23c8af8SIlya Dryomov } 1784edf24abeSChristoph Hellwig 1785312c89fbSMisono, Tomohiro /* mount device's root (/) */ 1786312c89fbSMisono, Tomohiro mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data); 1787312c89fbSMisono, Tomohiro if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) { 1788312c89fbSMisono, Tomohiro if (flags & SB_RDONLY) { 1789312c89fbSMisono, Tomohiro mnt_root = vfs_kern_mount(&btrfs_root_fs_type, 1790312c89fbSMisono, Tomohiro flags & ~SB_RDONLY, device_name, data); 17914b82d6e4SYan } else { 1792312c89fbSMisono, Tomohiro mnt_root = vfs_kern_mount(&btrfs_root_fs_type, 1793312c89fbSMisono, Tomohiro flags | SB_RDONLY, device_name, data); 1794312c89fbSMisono, Tomohiro if (IS_ERR(mnt_root)) { 1795312c89fbSMisono, Tomohiro root = ERR_CAST(mnt_root); 1796532b618bSEric W. Biederman kfree(subvol_name); 1797312c89fbSMisono, Tomohiro goto out; 1798f667aef6SQu Wenruo } 1799f667aef6SQu Wenruo 1800312c89fbSMisono, Tomohiro down_write(&mnt_root->mnt_sb->s_umount); 1801312c89fbSMisono, Tomohiro error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL); 1802312c89fbSMisono, Tomohiro up_write(&mnt_root->mnt_sb->s_umount); 1803312c89fbSMisono, Tomohiro if (error < 0) { 1804312c89fbSMisono, Tomohiro root = ERR_PTR(error); 1805312c89fbSMisono, Tomohiro mntput(mnt_root); 1806532b618bSEric W. Biederman kfree(subvol_name); 1807312c89fbSMisono, Tomohiro goto out; 1808312c89fbSMisono, Tomohiro } 1809312c89fbSMisono, Tomohiro } 1810312c89fbSMisono, Tomohiro } 1811312c89fbSMisono, Tomohiro if (IS_ERR(mnt_root)) { 1812312c89fbSMisono, Tomohiro root = ERR_CAST(mnt_root); 1813532b618bSEric W. Biederman kfree(subvol_name); 1814312c89fbSMisono, Tomohiro goto out; 1815f667aef6SQu Wenruo } 18164b82d6e4SYan 1817312c89fbSMisono, Tomohiro /* mount_subvol() will free subvol_name and mnt_root */ 1818ae0bc863SAnand Jain root = mount_subvol(subvol_name, subvol_objectid, mnt_root); 18194b82d6e4SYan 1820312c89fbSMisono, Tomohiro out: 1821312c89fbSMisono, Tomohiro return root; 18224b82d6e4SYan } 18232e635a27SChris Mason 18240d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 1825f7b885beSAnand Jain u32 new_pool_size, u32 old_pool_size) 18260d2450abSSergei Trofimovich { 18270d2450abSSergei Trofimovich if (new_pool_size == old_pool_size) 18280d2450abSSergei Trofimovich return; 18290d2450abSSergei Trofimovich 18300d2450abSSergei Trofimovich fs_info->thread_pool_size = new_pool_size; 18310d2450abSSergei Trofimovich 1832efe120a0SFrank Holton btrfs_info(fs_info, "resize thread pool %d -> %d", 18330d2450abSSergei Trofimovich old_pool_size, new_pool_size); 18340d2450abSSergei Trofimovich 18355cdc7ad3SQu Wenruo btrfs_workqueue_set_max(fs_info->workers, new_pool_size); 1836afe3d242SQu Wenruo btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size); 1837e66f0bb1SQu Wenruo btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size); 1838fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size); 1839fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size); 1840fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_write_workers, 1841fccb5d86SQu Wenruo new_pool_size); 1842fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size); 1843fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size); 18445b3bc44eSQu Wenruo btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size); 1845736cfa15SQu Wenruo btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size); 18460339ef2fSQu Wenruo btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers, 1847ff023aacSStefan Behrens new_pool_size); 18480d2450abSSergei Trofimovich } 18490d2450abSSergei Trofimovich 1850f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info, 1851f42a34b2SMiao Xie unsigned long old_opts, int flags) 1852f42a34b2SMiao Xie { 1853dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1854dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 18551751e8a6SLinus Torvalds (flags & SB_RDONLY))) { 1856dc81cdc5SMiao Xie /* wait for any defraggers to finish */ 1857dc81cdc5SMiao Xie wait_event(fs_info->transaction_wait, 1858dc81cdc5SMiao Xie (atomic_read(&fs_info->defrag_running) == 0)); 18591751e8a6SLinus Torvalds if (flags & SB_RDONLY) 1860dc81cdc5SMiao Xie sync_filesystem(fs_info->sb); 1861dc81cdc5SMiao Xie } 1862dc81cdc5SMiao Xie } 1863dc81cdc5SMiao Xie 1864dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info, 1865dc81cdc5SMiao Xie unsigned long old_opts) 1866dc81cdc5SMiao Xie { 186794846229SBoris Burkov const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE); 186894846229SBoris Burkov 1869dc81cdc5SMiao Xie /* 1870180e4d47SLuis de Bethencourt * We need to cleanup all defragable inodes if the autodefragment is 1871180e4d47SLuis de Bethencourt * close or the filesystem is read only. 1872dc81cdc5SMiao Xie */ 1873dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1874bc98a42cSDavid Howells (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) { 1875dc81cdc5SMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 1876dc81cdc5SMiao Xie } 1877dc81cdc5SMiao Xie 1878b0643e59SDennis Zhou /* If we toggled discard async */ 1879b0643e59SDennis Zhou if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) && 1880b0643e59SDennis Zhou btrfs_test_opt(fs_info, DISCARD_ASYNC)) 1881b0643e59SDennis Zhou btrfs_discard_resume(fs_info); 1882b0643e59SDennis Zhou else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) && 1883b0643e59SDennis Zhou !btrfs_test_opt(fs_info, DISCARD_ASYNC)) 1884b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 188594846229SBoris Burkov 188694846229SBoris Burkov /* If we toggled space cache */ 188794846229SBoris Burkov if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) 188894846229SBoris Burkov btrfs_set_free_space_cache_v1_active(fs_info, cache_opt); 1889dc81cdc5SMiao Xie } 1890dc81cdc5SMiao Xie 1891c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1892c146afadSYan Zheng { 1893815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 189449b25e05SJeff Mahoney unsigned old_flags = sb->s_flags; 189549b25e05SJeff Mahoney unsigned long old_opts = fs_info->mount_opt; 189649b25e05SJeff Mahoney unsigned long old_compress_type = fs_info->compress_type; 189749b25e05SJeff Mahoney u64 old_max_inline = fs_info->max_inline; 1898f7b885beSAnand Jain u32 old_thread_pool_size = fs_info->thread_pool_size; 1899d612ac59SAnand Jain u32 old_metadata_ratio = fs_info->metadata_ratio; 1900c146afadSYan Zheng int ret; 1901c146afadSYan Zheng 190202b9984dSTheodore Ts'o sync_filesystem(sb); 190388c4703fSJohannes Thumshirn set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1904dc81cdc5SMiao Xie 1905f667aef6SQu Wenruo if (data) { 1906204cc0ccSAl Viro void *new_sec_opts = NULL; 1907f667aef6SQu Wenruo 1908a65001e8SAl Viro ret = security_sb_eat_lsm_opts(data, &new_sec_opts); 1909a65001e8SAl Viro if (!ret) 1910204cc0ccSAl Viro ret = security_sb_remount(sb, new_sec_opts); 1911a65001e8SAl Viro security_free_mnt_opts(&new_sec_opts); 1912f667aef6SQu Wenruo if (ret) 1913f667aef6SQu Wenruo goto restore; 1914f667aef6SQu Wenruo } 1915f667aef6SQu Wenruo 19162ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, data, *flags); 1917891f41cbSChengguang Xu if (ret) 191849b25e05SJeff Mahoney goto restore; 1919b288052eSChris Mason 1920f42a34b2SMiao Xie btrfs_remount_begin(fs_info, old_opts, *flags); 19210d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 19220d2450abSSergei Trofimovich fs_info->thread_pool_size, old_thread_pool_size); 19230d2450abSSergei Trofimovich 1924c55a4319SBoris Burkov if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) != 1925c55a4319SBoris Burkov (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 19262838d255SBoris Burkov (!sb_rdonly(sb) || (*flags & SB_RDONLY))) { 19272838d255SBoris Burkov btrfs_warn(fs_info, 19282838d255SBoris Burkov "remount supports changing free space tree only from ro to rw"); 19292838d255SBoris Burkov /* Make sure free space cache options match the state on disk */ 19302838d255SBoris Burkov if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 19312838d255SBoris Burkov btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE); 19322838d255SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE); 19332838d255SBoris Burkov } 19342838d255SBoris Burkov if (btrfs_free_space_cache_v1_active(fs_info)) { 19352838d255SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE); 19362838d255SBoris Burkov btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE); 19372838d255SBoris Burkov } 19382838d255SBoris Burkov } 19392838d255SBoris Burkov 19401751e8a6SLinus Torvalds if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb)) 1941dc81cdc5SMiao Xie goto out; 1942c146afadSYan Zheng 19431751e8a6SLinus Torvalds if (*flags & SB_RDONLY) { 19448dabb742SStefan Behrens /* 19458dabb742SStefan Behrens * this also happens on 'umount -rf' or on shutdown, when 19468dabb742SStefan Behrens * the filesystem is busy. 19478dabb742SStefan Behrens */ 194821c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 194957056740SJosef Bacik cancel_work_sync(&fs_info->async_data_reclaim_work); 1950361c093dSStefan Behrens 1951b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 1952b0643e59SDennis Zhou 1953361c093dSStefan Behrens /* wait for the uuid_scan task to finish */ 1954361c093dSStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 1955361c093dSStefan Behrens /* avoid complains from lockdep et al. */ 1956361c093dSStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 1957361c093dSStefan Behrens 1958a0a1db70SFilipe Manana btrfs_set_sb_rdonly(sb); 1959c146afadSYan Zheng 1960e44163e1SJeff Mahoney /* 19611751e8a6SLinus Torvalds * Setting SB_RDONLY will put the cleaner thread to 1962e44163e1SJeff Mahoney * sleep at the next loop if it's already active. 1963e44163e1SJeff Mahoney * If it's already asleep, we'll leave unused block 1964e44163e1SJeff Mahoney * groups on disk until we're mounted read-write again 1965e44163e1SJeff Mahoney * unless we clean them up here. 1966e44163e1SJeff Mahoney */ 1967e44163e1SJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1968e44163e1SJeff Mahoney 1969a0a1db70SFilipe Manana /* 1970a0a1db70SFilipe Manana * The cleaner task could be already running before we set the 1971a0a1db70SFilipe Manana * flag BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock). 1972a0a1db70SFilipe Manana * We must make sure that after we finish the remount, i.e. after 1973a0a1db70SFilipe Manana * we call btrfs_commit_super(), the cleaner can no longer start 1974a0a1db70SFilipe Manana * a transaction - either because it was dropping a dead root, 1975a0a1db70SFilipe Manana * running delayed iputs or deleting an unused block group (the 1976a0a1db70SFilipe Manana * cleaner picked a block group from the list of unused block 1977a0a1db70SFilipe Manana * groups before we were able to in the previous call to 1978a0a1db70SFilipe Manana * btrfs_delete_unused_bgs()). 1979a0a1db70SFilipe Manana */ 1980a0a1db70SFilipe Manana wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING, 1981a0a1db70SFilipe Manana TASK_UNINTERRUPTIBLE); 1982a0a1db70SFilipe Manana 1983a8cc263eSFilipe Manana /* 1984a8cc263eSFilipe Manana * We've set the superblock to RO mode, so we might have made 1985a8cc263eSFilipe Manana * the cleaner task sleep without running all pending delayed 1986a8cc263eSFilipe Manana * iputs. Go through all the delayed iputs here, so that if an 1987a8cc263eSFilipe Manana * unmount happens without remounting RW we don't end up at 1988a8cc263eSFilipe Manana * finishing close_ctree() with a non-empty list of delayed 1989a8cc263eSFilipe Manana * iputs. 1990a8cc263eSFilipe Manana */ 1991a8cc263eSFilipe Manana btrfs_run_delayed_iputs(fs_info); 1992a8cc263eSFilipe Manana 19938dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 19948dabb742SStefan Behrens btrfs_scrub_cancel(fs_info); 1995061594efSMiao Xie btrfs_pause_balance(fs_info); 19968dabb742SStefan Behrens 1997cb13eea3SFilipe Manana /* 1998cb13eea3SFilipe Manana * Pause the qgroup rescan worker if it is running. We don't want 1999cb13eea3SFilipe Manana * it to be still running after we are in RO mode, as after that, 2000cb13eea3SFilipe Manana * by the time we unmount, it might have left a transaction open, 2001cb13eea3SFilipe Manana * so we would leak the transaction and/or crash. 2002cb13eea3SFilipe Manana */ 2003cb13eea3SFilipe Manana btrfs_qgroup_wait_for_completion(fs_info, false); 2004cb13eea3SFilipe Manana 20056bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 200649b25e05SJeff Mahoney if (ret) 200749b25e05SJeff Mahoney goto restore; 2008c146afadSYan Zheng } else { 20090b246afaSJeff Mahoney if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 20106ef3de9cSDavid Sterba btrfs_err(fs_info, 2011efe120a0SFrank Holton "Remounting read-write after error is not allowed"); 20126ef3de9cSDavid Sterba ret = -EINVAL; 20136ef3de9cSDavid Sterba goto restore; 20146ef3de9cSDavid Sterba } 20158a3db184SSergei Trofimovich if (fs_info->fs_devices->rw_devices == 0) { 201649b25e05SJeff Mahoney ret = -EACCES; 201749b25e05SJeff Mahoney goto restore; 20188a3db184SSergei Trofimovich } 20192b82032cSYan Zheng 20206528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) { 2021efe120a0SFrank Holton btrfs_warn(fs_info, 202252042d8eSAndrea Gelmini "too many missing devices, writable remount is not allowed"); 2023292fd7fcSStefan Behrens ret = -EACCES; 2024292fd7fcSStefan Behrens goto restore; 2025292fd7fcSStefan Behrens } 2026292fd7fcSStefan Behrens 20278a3db184SSergei Trofimovich if (btrfs_super_log_root(fs_info->super_copy) != 0) { 202810a3a3edSDavid Sterba btrfs_warn(fs_info, 202910a3a3edSDavid Sterba "mount required to replay tree-log, cannot remount read-write"); 203049b25e05SJeff Mahoney ret = -EINVAL; 203149b25e05SJeff Mahoney goto restore; 20328a3db184SSergei Trofimovich } 2033c146afadSYan Zheng 203444c0ca21SBoris Burkov /* 203544c0ca21SBoris Burkov * NOTE: when remounting with a change that does writes, don't 203644c0ca21SBoris Burkov * put it anywhere above this point, as we are not sure to be 203744c0ca21SBoris Burkov * safe to write until we pass the above checks. 203844c0ca21SBoris Burkov */ 203944c0ca21SBoris Burkov ret = btrfs_start_pre_rw_mount(fs_info); 204049b25e05SJeff Mahoney if (ret) 204149b25e05SJeff Mahoney goto restore; 2042c146afadSYan Zheng 2043a0a1db70SFilipe Manana btrfs_clear_sb_rdonly(sb); 204490c711abSZygo Blaxell 2045afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 2046c146afadSYan Zheng } 2047dc81cdc5SMiao Xie out: 2048faa00889SJosef Bacik /* 2049faa00889SJosef Bacik * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS, 2050faa00889SJosef Bacik * since the absence of the flag means it can be toggled off by remount. 2051faa00889SJosef Bacik */ 2052faa00889SJosef Bacik *flags |= SB_I_VERSION; 2053faa00889SJosef Bacik 20542c6a92b0SJustin Maggard wake_up_process(fs_info->transaction_kthread); 2055dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 20568cd29088SBoris Burkov btrfs_clear_oneshot_options(fs_info); 205788c4703fSJohannes Thumshirn clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 205888c4703fSJohannes Thumshirn 2059c146afadSYan Zheng return 0; 206049b25e05SJeff Mahoney 206149b25e05SJeff Mahoney restore: 20621751e8a6SLinus Torvalds /* We've hit an error - don't reset SB_RDONLY */ 2063bc98a42cSDavid Howells if (sb_rdonly(sb)) 20641751e8a6SLinus Torvalds old_flags |= SB_RDONLY; 2065a0a1db70SFilipe Manana if (!(old_flags & SB_RDONLY)) 2066a0a1db70SFilipe Manana clear_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state); 206749b25e05SJeff Mahoney sb->s_flags = old_flags; 206849b25e05SJeff Mahoney fs_info->mount_opt = old_opts; 206949b25e05SJeff Mahoney fs_info->compress_type = old_compress_type; 207049b25e05SJeff Mahoney fs_info->max_inline = old_max_inline; 20710d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 20720d2450abSSergei Trofimovich old_thread_pool_size, fs_info->thread_pool_size); 207349b25e05SJeff Mahoney fs_info->metadata_ratio = old_metadata_ratio; 2074dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 207588c4703fSJohannes Thumshirn clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 207688c4703fSJohannes Thumshirn 207749b25e05SJeff Mahoney return ret; 2078c146afadSYan Zheng } 2079c146afadSYan Zheng 2080bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 2081214cc184SDavid Sterba static int btrfs_cmp_device_free_bytes(const void *a, const void *b) 2082bcd53741SArne Jansen { 2083214cc184SDavid Sterba const struct btrfs_device_info *dev_info1 = a; 2084214cc184SDavid Sterba const struct btrfs_device_info *dev_info2 = b; 2085214cc184SDavid Sterba 2086214cc184SDavid Sterba if (dev_info1->max_avail > dev_info2->max_avail) 2087bcd53741SArne Jansen return -1; 2088214cc184SDavid Sterba else if (dev_info1->max_avail < dev_info2->max_avail) 2089bcd53741SArne Jansen return 1; 2090bcd53741SArne Jansen return 0; 2091bcd53741SArne Jansen } 2092bcd53741SArne Jansen 2093bcd53741SArne Jansen /* 2094bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 2095bcd53741SArne Jansen * is stored.(Descending Sort) 2096bcd53741SArne Jansen */ 2097bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 2098bcd53741SArne Jansen struct btrfs_device_info *devices, 2099bcd53741SArne Jansen size_t nr_devices) 2100bcd53741SArne Jansen { 2101bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 2102bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 2103bcd53741SArne Jansen } 2104bcd53741SArne Jansen 21056d07bcecSMiao Xie /* 21066d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 21076d07bcecSMiao Xie * file data. 21086d07bcecSMiao Xie */ 21097e17916bSArnd Bergmann static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info, 21106bccf3abSJeff Mahoney u64 *free_bytes) 21116d07bcecSMiao Xie { 21126d07bcecSMiao Xie struct btrfs_device_info *devices_info; 21136d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 21146d07bcecSMiao Xie struct btrfs_device *device; 21156d07bcecSMiao Xie u64 type; 21166d07bcecSMiao Xie u64 avail_space; 21176d07bcecSMiao Xie u64 min_stripe_size; 2118559ca6eaSNikolay Borisov int num_stripes = 1; 21196d07bcecSMiao Xie int i = 0, nr_devices; 21204f080f57SDavid Sterba const struct btrfs_raid_attr *rattr; 21216d07bcecSMiao Xie 21227e33fd99SJosef Bacik /* 212301327610SNicholas D Steeves * We aren't under the device list lock, so this is racy-ish, but good 21247e33fd99SJosef Bacik * enough for our purposes. 21257e33fd99SJosef Bacik */ 2126b772a86eSLi Zefan nr_devices = fs_info->fs_devices->open_devices; 21277e33fd99SJosef Bacik if (!nr_devices) { 21287e33fd99SJosef Bacik smp_mb(); 21297e33fd99SJosef Bacik nr_devices = fs_info->fs_devices->open_devices; 21307e33fd99SJosef Bacik ASSERT(nr_devices); 21317e33fd99SJosef Bacik if (!nr_devices) { 21327e33fd99SJosef Bacik *free_bytes = 0; 21337e33fd99SJosef Bacik return 0; 21347e33fd99SJosef Bacik } 21357e33fd99SJosef Bacik } 21366d07bcecSMiao Xie 2137d9b0d9baSDulshani Gunawardhana devices_info = kmalloc_array(nr_devices, sizeof(*devices_info), 21386a44517dSDavid Sterba GFP_KERNEL); 21396d07bcecSMiao Xie if (!devices_info) 21406d07bcecSMiao Xie return -ENOMEM; 21416d07bcecSMiao Xie 214201327610SNicholas D Steeves /* calc min stripe number for data space allocation */ 21431b86826dSJeff Mahoney type = btrfs_data_alloc_profile(fs_info); 21444f080f57SDavid Sterba rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)]; 21454f080f57SDavid Sterba 2146e1ea2beeSDavid Sterba if (type & BTRFS_BLOCK_GROUP_RAID0) 214739fb26c3SMiao Xie num_stripes = nr_devices; 2148e1ea2beeSDavid Sterba else if (type & BTRFS_BLOCK_GROUP_RAID1) 214939fb26c3SMiao Xie num_stripes = 2; 215047e6f742SDavid Sterba else if (type & BTRFS_BLOCK_GROUP_RAID1C3) 215147e6f742SDavid Sterba num_stripes = 3; 21528d6fac00SDavid Sterba else if (type & BTRFS_BLOCK_GROUP_RAID1C4) 21538d6fac00SDavid Sterba num_stripes = 4; 2154e1ea2beeSDavid Sterba else if (type & BTRFS_BLOCK_GROUP_RAID10) 215539fb26c3SMiao Xie num_stripes = 4; 21566d07bcecSMiao Xie 21574f080f57SDavid Sterba /* Adjust for more than 1 stripe per device */ 21584f080f57SDavid Sterba min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN; 21596d07bcecSMiao Xie 21607e33fd99SJosef Bacik rcu_read_lock(); 21617e33fd99SJosef Bacik list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 2162e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, 2163e12c9621SAnand Jain &device->dev_state) || 2164401e29c1SAnand Jain !device->bdev || 2165401e29c1SAnand Jain test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) 21666d07bcecSMiao Xie continue; 21676d07bcecSMiao Xie 21687e33fd99SJosef Bacik if (i >= nr_devices) 21697e33fd99SJosef Bacik break; 21707e33fd99SJosef Bacik 21716d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 21726d07bcecSMiao Xie 21736d07bcecSMiao Xie /* align with stripe_len */ 2174559ca6eaSNikolay Borisov avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN); 21756d07bcecSMiao Xie 21766d07bcecSMiao Xie /* 217701327610SNicholas D Steeves * In order to avoid overwriting the superblock on the drive, 21786d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 21796d07bcecSMiao Xie * allocation. 2180559ca6eaSNikolay Borisov * 2181559ca6eaSNikolay Borisov * This ensures we have at least min_stripe_size free space 2182559ca6eaSNikolay Borisov * after excluding 1MB. 21836d07bcecSMiao Xie */ 2184559ca6eaSNikolay Borisov if (avail_space <= SZ_1M + min_stripe_size) 21856d07bcecSMiao Xie continue; 21866d07bcecSMiao Xie 2187559ca6eaSNikolay Borisov avail_space -= SZ_1M; 2188559ca6eaSNikolay Borisov 21896d07bcecSMiao Xie devices_info[i].dev = device; 21906d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 21916d07bcecSMiao Xie 21926d07bcecSMiao Xie i++; 21936d07bcecSMiao Xie } 21947e33fd99SJosef Bacik rcu_read_unlock(); 21956d07bcecSMiao Xie 21966d07bcecSMiao Xie nr_devices = i; 21976d07bcecSMiao Xie 21986d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 21996d07bcecSMiao Xie 22006d07bcecSMiao Xie i = nr_devices - 1; 22016d07bcecSMiao Xie avail_space = 0; 2202559ca6eaSNikolay Borisov while (nr_devices >= rattr->devs_min) { 2203559ca6eaSNikolay Borisov num_stripes = min(num_stripes, nr_devices); 220439fb26c3SMiao Xie 22056d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 22066d07bcecSMiao Xie int j; 22076d07bcecSMiao Xie u64 alloc_size; 22086d07bcecSMiao Xie 220939fb26c3SMiao Xie avail_space += devices_info[i].max_avail * num_stripes; 22106d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 221139fb26c3SMiao Xie for (j = i + 1 - num_stripes; j <= i; j++) 22126d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 22136d07bcecSMiao Xie } 22146d07bcecSMiao Xie i--; 22156d07bcecSMiao Xie nr_devices--; 22166d07bcecSMiao Xie } 22176d07bcecSMiao Xie 22186d07bcecSMiao Xie kfree(devices_info); 22196d07bcecSMiao Xie *free_bytes = avail_space; 22206d07bcecSMiao Xie return 0; 22216d07bcecSMiao Xie } 22226d07bcecSMiao Xie 2223ba7b6e62SDavid Sterba /* 2224ba7b6e62SDavid Sterba * Calculate numbers for 'df', pessimistic in case of mixed raid profiles. 2225ba7b6e62SDavid Sterba * 2226ba7b6e62SDavid Sterba * If there's a redundant raid level at DATA block groups, use the respective 2227ba7b6e62SDavid Sterba * multiplier to scale the sizes. 2228ba7b6e62SDavid Sterba * 2229ba7b6e62SDavid Sterba * Unused device space usage is based on simulating the chunk allocator 22300d0c71b3SDavid Sterba * algorithm that respects the device sizes and order of allocations. This is 22310d0c71b3SDavid Sterba * a close approximation of the actual use but there are other factors that may 22320d0c71b3SDavid Sterba * change the result (like a new metadata chunk). 2233ba7b6e62SDavid Sterba * 2234ca8a51b3SDavid Sterba * If metadata is exhausted, f_bavail will be 0. 2235ba7b6e62SDavid Sterba */ 22368fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 22378fd17795SChris Mason { 2238815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 2239815745cfSAl Viro struct btrfs_super_block *disk_super = fs_info->super_copy; 2240bd4d1088SJosef Bacik struct btrfs_space_info *found; 2241bd4d1088SJosef Bacik u64 total_used = 0; 22426d07bcecSMiao Xie u64 total_free_data = 0; 2243ca8a51b3SDavid Sterba u64 total_free_meta = 0; 2244265fdfa6SDavid Sterba u32 bits = fs_info->sectorsize_bits; 2245de37aa51SNikolay Borisov __be32 *fsid = (__be32 *)fs_info->fs_devices->fsid; 2246ba7b6e62SDavid Sterba unsigned factor = 1; 2247ba7b6e62SDavid Sterba struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 22486d07bcecSMiao Xie int ret; 2249ca8a51b3SDavid Sterba u64 thresh = 0; 2250ae02d1bdSLuis de Bethencourt int mixed = 0; 22518fd17795SChris Mason 225272804905SJosef Bacik list_for_each_entry(found, &fs_info->space_info, list) { 22536d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 2254ba7b6e62SDavid Sterba int i; 2255ba7b6e62SDavid Sterba 22566d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 22576d07bcecSMiao Xie total_free_data -= 22586d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 2259ba7b6e62SDavid Sterba 2260ba7b6e62SDavid Sterba for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { 226146df06b8SDavid Sterba if (!list_empty(&found->block_groups[i])) 226246df06b8SDavid Sterba factor = btrfs_bg_type_to_factor( 226346df06b8SDavid Sterba btrfs_raid_array[i].bg_flag); 2264ba7b6e62SDavid Sterba } 22656d07bcecSMiao Xie } 2266ae02d1bdSLuis de Bethencourt 2267ae02d1bdSLuis de Bethencourt /* 2268ae02d1bdSLuis de Bethencourt * Metadata in mixed block goup profiles are accounted in data 2269ae02d1bdSLuis de Bethencourt */ 2270ae02d1bdSLuis de Bethencourt if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) { 2271ae02d1bdSLuis de Bethencourt if (found->flags & BTRFS_BLOCK_GROUP_DATA) 2272ae02d1bdSLuis de Bethencourt mixed = 1; 2273ae02d1bdSLuis de Bethencourt else 2274ae02d1bdSLuis de Bethencourt total_free_meta += found->disk_total - 2275ae02d1bdSLuis de Bethencourt found->disk_used; 2276ae02d1bdSLuis de Bethencourt } 22776d07bcecSMiao Xie 2278b742bb82SYan, Zheng total_used += found->disk_used; 227989a55897SJosef Bacik } 2280ba7b6e62SDavid Sterba 2281ba7b6e62SDavid Sterba buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor); 2282ba7b6e62SDavid Sterba buf->f_blocks >>= bits; 2283ba7b6e62SDavid Sterba buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits); 2284ba7b6e62SDavid Sterba 2285ba7b6e62SDavid Sterba /* Account global block reserve as used, it's in logical size already */ 2286ba7b6e62SDavid Sterba spin_lock(&block_rsv->lock); 228741b34accSLuis de Bethencourt /* Mixed block groups accounting is not byte-accurate, avoid overflow */ 228841b34accSLuis de Bethencourt if (buf->f_bfree >= block_rsv->size >> bits) 2289ba7b6e62SDavid Sterba buf->f_bfree -= block_rsv->size >> bits; 229041b34accSLuis de Bethencourt else 229141b34accSLuis de Bethencourt buf->f_bfree = 0; 2292ba7b6e62SDavid Sterba spin_unlock(&block_rsv->lock); 2293ba7b6e62SDavid Sterba 22940d95c1beSDavid Sterba buf->f_bavail = div_u64(total_free_data, factor); 22956bccf3abSJeff Mahoney ret = btrfs_calc_avail_data_space(fs_info, &total_free_data); 22967e33fd99SJosef Bacik if (ret) 22976d07bcecSMiao Xie return ret; 2298ba7b6e62SDavid Sterba buf->f_bavail += div_u64(total_free_data, factor); 22996d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 2300d397712bSChris Mason 2301ca8a51b3SDavid Sterba /* 2302ca8a51b3SDavid Sterba * We calculate the remaining metadata space minus global reserve. If 2303ca8a51b3SDavid Sterba * this is (supposedly) smaller than zero, there's no space. But this 2304ca8a51b3SDavid Sterba * does not hold in practice, the exhausted state happens where's still 2305ca8a51b3SDavid Sterba * some positive delta. So we apply some guesswork and compare the 2306ca8a51b3SDavid Sterba * delta to a 4M threshold. (Practically observed delta was ~2M.) 2307ca8a51b3SDavid Sterba * 2308ca8a51b3SDavid Sterba * We probably cannot calculate the exact threshold value because this 2309ca8a51b3SDavid Sterba * depends on the internal reservations requested by various 2310ca8a51b3SDavid Sterba * operations, so some operations that consume a few metadata will 2311ca8a51b3SDavid Sterba * succeed even if the Avail is zero. But this is better than the other 2312ca8a51b3SDavid Sterba * way around. 2313ca8a51b3SDavid Sterba */ 2314d4417e22SNikolay Borisov thresh = SZ_4M; 2315ca8a51b3SDavid Sterba 2316d55966c4SJosef Bacik /* 2317d55966c4SJosef Bacik * We only want to claim there's no available space if we can no longer 2318d55966c4SJosef Bacik * allocate chunks for our metadata profile and our global reserve will 2319d55966c4SJosef Bacik * not fit in the free metadata space. If we aren't ->full then we 2320d55966c4SJosef Bacik * still can allocate chunks and thus are fine using the currently 2321d55966c4SJosef Bacik * calculated f_bavail. 2322d55966c4SJosef Bacik */ 2323d55966c4SJosef Bacik if (!mixed && block_rsv->space_info->full && 2324d55966c4SJosef Bacik total_free_meta - thresh < block_rsv->size) 2325ca8a51b3SDavid Sterba buf->f_bavail = 0; 2326ca8a51b3SDavid Sterba 2327ba7b6e62SDavid Sterba buf->f_type = BTRFS_SUPER_MAGIC; 2328ba7b6e62SDavid Sterba buf->f_bsize = dentry->d_sb->s_blocksize; 2329ba7b6e62SDavid Sterba buf->f_namelen = BTRFS_NAME_LEN; 2330ba7b6e62SDavid Sterba 23319d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 23329d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 23339d03632eSDavid Woodhouse on a big-endian or little-endian host */ 23349d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 23359d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 233632d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 23374fd786e6SMisono Tomohiro buf->f_fsid.val[0] ^= 23384fd786e6SMisono Tomohiro BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32; 23394fd786e6SMisono Tomohiro buf->f_fsid.val[1] ^= 23404fd786e6SMisono Tomohiro BTRFS_I(d_inode(dentry))->root->root_key.objectid; 234132d48fa1SDavid Woodhouse 23428fd17795SChris Mason return 0; 23438fd17795SChris Mason } 2344b5133862SChris Mason 2345aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb) 2346aea52e19SAl Viro { 2347815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2348aea52e19SAl Viro kill_anon_super(sb); 23490d4b0463SJosef Bacik btrfs_free_fs_info(fs_info); 2350aea52e19SAl Viro } 2351aea52e19SAl Viro 23522e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 23532e635a27SChris Mason .owner = THIS_MODULE, 23542e635a27SChris Mason .name = "btrfs", 2355061dbc6bSAl Viro .mount = btrfs_mount, 2356aea52e19SAl Viro .kill_sb = btrfs_kill_super, 2357f667aef6SQu Wenruo .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA, 23582e635a27SChris Mason }; 235972fa39f5SMisono, Tomohiro 236072fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type = { 236172fa39f5SMisono, Tomohiro .owner = THIS_MODULE, 236272fa39f5SMisono, Tomohiro .name = "btrfs", 236372fa39f5SMisono, Tomohiro .mount = btrfs_mount_root, 236472fa39f5SMisono, Tomohiro .kill_sb = btrfs_kill_super, 23655b9b26f5SChristian Brauner .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP, 236672fa39f5SMisono, Tomohiro }; 236772fa39f5SMisono, Tomohiro 23687f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs"); 2369a9218f6bSChris Mason 2370d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file) 2371d8620958STom Van Braeckel { 2372d8620958STom Van Braeckel /* 2373d8620958STom Van Braeckel * The control file's private_data is used to hold the 2374d8620958STom Van Braeckel * transaction when it is started and is used to keep 2375d8620958STom Van Braeckel * track of whether a transaction is already in progress. 2376d8620958STom Van Braeckel */ 2377d8620958STom Van Braeckel file->private_data = NULL; 2378d8620958STom Van Braeckel return 0; 2379d8620958STom Van Braeckel } 2380d8620958STom Van Braeckel 2381d352ac68SChris Mason /* 2382cfe953c8SSu Yue * Used by /dev/btrfs-control for devices ioctls. 2383d352ac68SChris Mason */ 23848a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 23858a4b83ccSChris Mason unsigned long arg) 23868a4b83ccSChris Mason { 23878a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 238836350e95SGu Jinxiang struct btrfs_device *device = NULL; 2389c071fcfdSChris Mason int ret = -ENOTTY; 23908a4b83ccSChris Mason 2391e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2392e441d54dSChris Mason return -EPERM; 2393e441d54dSChris Mason 2394dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 2395dae7b665SLi Zefan if (IS_ERR(vol)) 2396dae7b665SLi Zefan return PTR_ERR(vol); 2397f505754fSFilipe Manana vol->name[BTRFS_PATH_NAME_MAX] = '\0'; 2398c071fcfdSChris Mason 23998a4b83ccSChris Mason switch (cmd) { 24008a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 2401899f9307SDavid Sterba mutex_lock(&uuid_mutex); 240236350e95SGu Jinxiang device = btrfs_scan_one_device(vol->name, FMODE_READ, 240336350e95SGu Jinxiang &btrfs_root_fs_type); 240436350e95SGu Jinxiang ret = PTR_ERR_OR_ZERO(device); 2405899f9307SDavid Sterba mutex_unlock(&uuid_mutex); 24068a4b83ccSChris Mason break; 2407228a73abSAnand Jain case BTRFS_IOC_FORGET_DEV: 2408228a73abSAnand Jain ret = btrfs_forget_devices(vol->name); 2409228a73abSAnand Jain break; 241002db0844SJosef Bacik case BTRFS_IOC_DEVICES_READY: 2411899f9307SDavid Sterba mutex_lock(&uuid_mutex); 241236350e95SGu Jinxiang device = btrfs_scan_one_device(vol->name, FMODE_READ, 241336350e95SGu Jinxiang &btrfs_root_fs_type); 241436350e95SGu Jinxiang if (IS_ERR(device)) { 2415899f9307SDavid Sterba mutex_unlock(&uuid_mutex); 241636350e95SGu Jinxiang ret = PTR_ERR(device); 241702db0844SJosef Bacik break; 2418899f9307SDavid Sterba } 241936350e95SGu Jinxiang ret = !(device->fs_devices->num_devices == 242036350e95SGu Jinxiang device->fs_devices->total_devices); 2421899f9307SDavid Sterba mutex_unlock(&uuid_mutex); 242202db0844SJosef Bacik break; 2423c5868f83SDavid Sterba case BTRFS_IOC_GET_SUPPORTED_FEATURES: 2424d5131b65SDavid Sterba ret = btrfs_ioctl_get_supported_features((void __user*)arg); 2425c5868f83SDavid Sterba break; 24268a4b83ccSChris Mason } 2427dae7b665SLi Zefan 24288a4b83ccSChris Mason kfree(vol); 2429f819d837SLinda Knippers return ret; 24308a4b83ccSChris Mason } 24318a4b83ccSChris Mason 24320176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 2433ed0dab6bSYan { 2434354aa0fbSMiao Xie struct btrfs_trans_handle *trans; 24350b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(sb); 24360b246afaSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 2437354aa0fbSMiao Xie 2438fac03c8dSDavid Sterba set_bit(BTRFS_FS_FROZEN, &fs_info->flags); 24399e7cc91aSWang Xiaoguang /* 24409e7cc91aSWang Xiaoguang * We don't need a barrier here, we'll wait for any transaction that 24419e7cc91aSWang Xiaoguang * could be in progress on other threads (and do delayed iputs that 24429e7cc91aSWang Xiaoguang * we want to avoid on a frozen filesystem), or do the commit 24439e7cc91aSWang Xiaoguang * ourselves. 24449e7cc91aSWang Xiaoguang */ 2445d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 2446354aa0fbSMiao Xie if (IS_ERR(trans)) { 2447354aa0fbSMiao Xie /* no transaction, don't bother */ 2448354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 24490176260fSLinus Torvalds return 0; 2450354aa0fbSMiao Xie return PTR_ERR(trans); 2451354aa0fbSMiao Xie } 24523a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 2453ed0dab6bSYan } 2454ed0dab6bSYan 24559e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb) 24569e7cc91aSWang Xiaoguang { 2457fac03c8dSDavid Sterba struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2458fac03c8dSDavid Sterba 2459fac03c8dSDavid Sterba clear_bit(BTRFS_FS_FROZEN, &fs_info->flags); 24609e7cc91aSWang Xiaoguang return 0; 24619e7cc91aSWang Xiaoguang } 24629e7cc91aSWang Xiaoguang 24639c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 24649c5085c1SJosef Bacik { 24659c5085c1SJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 24669c5085c1SJosef Bacik 246788c14590SDavid Sterba /* 2468*6605fd2fSAnand Jain * There should be always a valid pointer in latest_dev, it may be stale 2469*6605fd2fSAnand Jain * for a short moment in case it's being deleted but still valid until 2470*6605fd2fSAnand Jain * the end of RCU grace period. 247188c14590SDavid Sterba */ 247288c14590SDavid Sterba rcu_read_lock(); 2473*6605fd2fSAnand Jain seq_escape(m, rcu_str_deref(fs_info->fs_devices->latest_dev->name), " \t\n\\"); 247488c14590SDavid Sterba rcu_read_unlock(); 2475*6605fd2fSAnand Jain 24769c5085c1SJosef Bacik return 0; 24779c5085c1SJosef Bacik } 24789c5085c1SJosef Bacik 2479b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 248076dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 2481bd555975SAl Viro .evict_inode = btrfs_evict_inode, 2482e20d96d6SChris Mason .put_super = btrfs_put_super, 2483d5719762SChris Mason .sync_fs = btrfs_sync_fs, 2484a9572a15SEric Paris .show_options = btrfs_show_options, 24859c5085c1SJosef Bacik .show_devname = btrfs_show_devname, 24862c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 24872c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 248826602cabSAl Viro .free_inode = btrfs_free_inode, 24898fd17795SChris Mason .statfs = btrfs_statfs, 2490c146afadSYan Zheng .remount_fs = btrfs_remount, 24910176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 24929e7cc91aSWang Xiaoguang .unfreeze_fs = btrfs_unfreeze, 2493e20d96d6SChris Mason }; 2494a9218f6bSChris Mason 2495a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 2496d8620958STom Van Braeckel .open = btrfs_control_open, 2497a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 24981832f2d8SArnd Bergmann .compat_ioctl = compat_ptr_ioctl, 2499a9218f6bSChris Mason .owner = THIS_MODULE, 25006038f373SArnd Bergmann .llseek = noop_llseek, 2501a9218f6bSChris Mason }; 2502a9218f6bSChris Mason 2503a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 2504578454ffSKay Sievers .minor = BTRFS_MINOR, 2505a9218f6bSChris Mason .name = "btrfs-control", 2506a9218f6bSChris Mason .fops = &btrfs_ctl_fops 2507a9218f6bSChris Mason }; 2508a9218f6bSChris Mason 2509578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 2510578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 2511578454ffSKay Sievers 2512f5c29bd9SLiu Bo static int __init btrfs_interface_init(void) 2513a9218f6bSChris Mason { 2514a9218f6bSChris Mason return misc_register(&btrfs_misc); 2515a9218f6bSChris Mason } 2516a9218f6bSChris Mason 2517e67c718bSDavid Sterba static __cold void btrfs_interface_exit(void) 2518a9218f6bSChris Mason { 2519f368ed60SGreg Kroah-Hartman misc_deregister(&btrfs_misc); 2520a9218f6bSChris Mason } 2521a9218f6bSChris Mason 2522f5c29bd9SLiu Bo static void __init btrfs_print_mod_info(void) 252385965600SDavid Sterba { 2524edf57cbfSBart Van Assche static const char options[] = "" 252585965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 252685965600SDavid Sterba ", debug=on" 252785965600SDavid Sterba #endif 252879556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT 252979556c3dSStefan Behrens ", assert=on" 253079556c3dSStefan Behrens #endif 253185965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 253285965600SDavid Sterba ", integrity-checker=on" 253385965600SDavid Sterba #endif 2534fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 2535fb592373SJosef Bacik ", ref-verify=on" 2536fb592373SJosef Bacik #endif 25375b316468SNaohiro Aota #ifdef CONFIG_BLK_DEV_ZONED 25385b316468SNaohiro Aota ", zoned=yes" 25395b316468SNaohiro Aota #else 25405b316468SNaohiro Aota ", zoned=no" 25415b316468SNaohiro Aota #endif 2542ea3dc7d2SDavid Sterba #ifdef CONFIG_FS_VERITY 2543ea3dc7d2SDavid Sterba ", fsverity=yes" 2544ea3dc7d2SDavid Sterba #else 2545ea3dc7d2SDavid Sterba ", fsverity=no" 2546ea3dc7d2SDavid Sterba #endif 2547edf57cbfSBart Van Assche ; 2548edf57cbfSBart Van Assche pr_info("Btrfs loaded, crc32c=%s%s\n", crc32c_impl(), options); 254985965600SDavid Sterba } 255085965600SDavid Sterba 25512e635a27SChris Mason static int __init init_btrfs_fs(void) 25522e635a27SChris Mason { 25532c90e5d6SChris Mason int err; 255458176a96SJosef Bacik 255563541927SFilipe David Borba Manana btrfs_props_init(); 255663541927SFilipe David Borba Manana 255758176a96SJosef Bacik err = btrfs_init_sysfs(); 255858176a96SJosef Bacik if (err) 25599678c543SNikolay Borisov return err; 256058176a96SJosef Bacik 2561143bede5SJeff Mahoney btrfs_init_compress(); 2562d1310b2eSChris Mason 2563261507a0SLi Zefan err = btrfs_init_cachep(); 2564261507a0SLi Zefan if (err) 2565261507a0SLi Zefan goto free_compress; 2566261507a0SLi Zefan 2567d1310b2eSChris Mason err = extent_io_init(); 25682f4cbe64SWyatt Banks if (err) 25692f4cbe64SWyatt Banks goto free_cachep; 25702f4cbe64SWyatt Banks 25716f0d04f8SJosef Bacik err = extent_state_cache_init(); 2572d1310b2eSChris Mason if (err) 2573d1310b2eSChris Mason goto free_extent_io; 2574d1310b2eSChris Mason 25756f0d04f8SJosef Bacik err = extent_map_init(); 25766f0d04f8SJosef Bacik if (err) 25776f0d04f8SJosef Bacik goto free_extent_state_cache; 25786f0d04f8SJosef Bacik 25796352b91dSMiao Xie err = ordered_data_init(); 25802f4cbe64SWyatt Banks if (err) 25812f4cbe64SWyatt Banks goto free_extent_map; 2582c8b97818SChris Mason 25836352b91dSMiao Xie err = btrfs_delayed_inode_init(); 25846352b91dSMiao Xie if (err) 25856352b91dSMiao Xie goto free_ordered_data; 25866352b91dSMiao Xie 25879247f317SMiao Xie err = btrfs_auto_defrag_init(); 258816cdcec7SMiao Xie if (err) 258916cdcec7SMiao Xie goto free_delayed_inode; 259016cdcec7SMiao Xie 259178a6184aSMiao Xie err = btrfs_delayed_ref_init(); 25929247f317SMiao Xie if (err) 25939247f317SMiao Xie goto free_auto_defrag; 25949247f317SMiao Xie 2595b9e9a6cbSWang Shilong err = btrfs_prelim_ref_init(); 2596b9e9a6cbSWang Shilong if (err) 2597af13b492SDavid Sterba goto free_delayed_ref; 2598b9e9a6cbSWang Shilong 259997eb6b69SDavid Sterba err = btrfs_end_io_wq_init(); 260078a6184aSMiao Xie if (err) 2601af13b492SDavid Sterba goto free_prelim_ref; 260278a6184aSMiao Xie 260397eb6b69SDavid Sterba err = btrfs_interface_init(); 260497eb6b69SDavid Sterba if (err) 260597eb6b69SDavid Sterba goto free_end_io_wq; 260697eb6b69SDavid Sterba 26078ae1af3cSAnand Jain btrfs_print_mod_info(); 2608dc11dd5dSJosef Bacik 2609dc11dd5dSJosef Bacik err = btrfs_run_sanity_tests(); 2610dc11dd5dSJosef Bacik if (err) 2611dc11dd5dSJosef Bacik goto unregister_ioctl; 2612dc11dd5dSJosef Bacik 2613dc11dd5dSJosef Bacik err = register_filesystem(&btrfs_fs_type); 2614dc11dd5dSJosef Bacik if (err) 2615dc11dd5dSJosef Bacik goto unregister_ioctl; 261674255aa0SJosef Bacik 26172f4cbe64SWyatt Banks return 0; 26182f4cbe64SWyatt Banks 2619a9218f6bSChris Mason unregister_ioctl: 2620a9218f6bSChris Mason btrfs_interface_exit(); 262197eb6b69SDavid Sterba free_end_io_wq: 262297eb6b69SDavid Sterba btrfs_end_io_wq_exit(); 2623b9e9a6cbSWang Shilong free_prelim_ref: 2624b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 262578a6184aSMiao Xie free_delayed_ref: 262678a6184aSMiao Xie btrfs_delayed_ref_exit(); 26279247f317SMiao Xie free_auto_defrag: 26289247f317SMiao Xie btrfs_auto_defrag_exit(); 262916cdcec7SMiao Xie free_delayed_inode: 263016cdcec7SMiao Xie btrfs_delayed_inode_exit(); 26316352b91dSMiao Xie free_ordered_data: 26326352b91dSMiao Xie ordered_data_exit(); 26332f4cbe64SWyatt Banks free_extent_map: 26342f4cbe64SWyatt Banks extent_map_exit(); 26356f0d04f8SJosef Bacik free_extent_state_cache: 26366f0d04f8SJosef Bacik extent_state_cache_exit(); 2637d1310b2eSChris Mason free_extent_io: 2638d1310b2eSChris Mason extent_io_exit(); 26392f4cbe64SWyatt Banks free_cachep: 26402f4cbe64SWyatt Banks btrfs_destroy_cachep(); 2641261507a0SLi Zefan free_compress: 2642261507a0SLi Zefan btrfs_exit_compress(); 26432f4cbe64SWyatt Banks btrfs_exit_sysfs(); 26449678c543SNikolay Borisov 26452c90e5d6SChris Mason return err; 26462e635a27SChris Mason } 26472e635a27SChris Mason 26482e635a27SChris Mason static void __exit exit_btrfs_fs(void) 26492e635a27SChris Mason { 265039279cc3SChris Mason btrfs_destroy_cachep(); 265178a6184aSMiao Xie btrfs_delayed_ref_exit(); 26529247f317SMiao Xie btrfs_auto_defrag_exit(); 265316cdcec7SMiao Xie btrfs_delayed_inode_exit(); 2654b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 26556352b91dSMiao Xie ordered_data_exit(); 2656a52d9a80SChris Mason extent_map_exit(); 26576f0d04f8SJosef Bacik extent_state_cache_exit(); 2658d1310b2eSChris Mason extent_io_exit(); 2659a9218f6bSChris Mason btrfs_interface_exit(); 26605ed5f588SJosef Bacik btrfs_end_io_wq_exit(); 26612e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 266258176a96SJosef Bacik btrfs_exit_sysfs(); 26638a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 2664261507a0SLi Zefan btrfs_exit_compress(); 26652e635a27SChris Mason } 26662e635a27SChris Mason 266760efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs); 26682e635a27SChris Mason module_exit(exit_btrfs_fs) 26692e635a27SChris Mason 26702e635a27SChris Mason MODULE_LICENSE("GPL"); 2671d5178578SJohannes Thumshirn MODULE_SOFTDEP("pre: crc32c"); 26723951e7f0SJohannes Thumshirn MODULE_SOFTDEP("pre: xxhash64"); 26733831bf00SJohannes Thumshirn MODULE_SOFTDEP("pre: sha256"); 2674352ae07bSDavid Sterba MODULE_SOFTDEP("pre: blake2b-256"); 2675