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 120405dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 120505dbe683SOmar Sandoval if (ret < 0) { 120605dbe683SOmar Sandoval goto err; 120705dbe683SOmar Sandoval } else if (ret > 0) { 120805dbe683SOmar Sandoval ret = btrfs_previous_item(root, path, subvol_objectid, 120905dbe683SOmar Sandoval BTRFS_ROOT_BACKREF_KEY); 121005dbe683SOmar Sandoval if (ret < 0) { 121105dbe683SOmar Sandoval goto err; 121205dbe683SOmar Sandoval } else if (ret > 0) { 121305dbe683SOmar Sandoval ret = -ENOENT; 121405dbe683SOmar Sandoval goto err; 121505dbe683SOmar Sandoval } 121605dbe683SOmar Sandoval } 121705dbe683SOmar Sandoval 121805dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 121905dbe683SOmar Sandoval subvol_objectid = key.offset; 122005dbe683SOmar Sandoval 122105dbe683SOmar Sandoval root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], 122205dbe683SOmar Sandoval struct btrfs_root_ref); 122305dbe683SOmar Sandoval len = btrfs_root_ref_name_len(path->nodes[0], root_ref); 122405dbe683SOmar Sandoval ptr -= len + 1; 122505dbe683SOmar Sandoval if (ptr < name) { 122605dbe683SOmar Sandoval ret = -ENAMETOOLONG; 122705dbe683SOmar Sandoval goto err; 122805dbe683SOmar Sandoval } 122905dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 123005dbe683SOmar Sandoval (unsigned long)(root_ref + 1), len); 123105dbe683SOmar Sandoval ptr[0] = '/'; 123205dbe683SOmar Sandoval dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref); 123305dbe683SOmar Sandoval btrfs_release_path(path); 123405dbe683SOmar Sandoval 123556e9357aSDavid Sterba fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true); 123605dbe683SOmar Sandoval if (IS_ERR(fs_root)) { 123705dbe683SOmar Sandoval ret = PTR_ERR(fs_root); 12385168489aSJosef Bacik fs_root = NULL; 12395168489aSJosef Bacik goto err; 12405168489aSJosef Bacik } 124105dbe683SOmar Sandoval 124205dbe683SOmar Sandoval /* 124305dbe683SOmar Sandoval * Walk up the filesystem tree by inode refs until we hit the 124405dbe683SOmar Sandoval * root directory. 124505dbe683SOmar Sandoval */ 124605dbe683SOmar Sandoval while (dirid != BTRFS_FIRST_FREE_OBJECTID) { 124705dbe683SOmar Sandoval key.objectid = dirid; 124805dbe683SOmar Sandoval key.type = BTRFS_INODE_REF_KEY; 124905dbe683SOmar Sandoval key.offset = (u64)-1; 125005dbe683SOmar Sandoval 125105dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); 125205dbe683SOmar Sandoval if (ret < 0) { 125305dbe683SOmar Sandoval goto err; 125405dbe683SOmar Sandoval } else if (ret > 0) { 125505dbe683SOmar Sandoval ret = btrfs_previous_item(fs_root, path, dirid, 125605dbe683SOmar Sandoval BTRFS_INODE_REF_KEY); 125705dbe683SOmar Sandoval if (ret < 0) { 125805dbe683SOmar Sandoval goto err; 125905dbe683SOmar Sandoval } else if (ret > 0) { 126005dbe683SOmar Sandoval ret = -ENOENT; 126105dbe683SOmar Sandoval goto err; 126205dbe683SOmar Sandoval } 126305dbe683SOmar Sandoval } 126405dbe683SOmar Sandoval 126505dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 126605dbe683SOmar Sandoval dirid = key.offset; 126705dbe683SOmar Sandoval 126805dbe683SOmar Sandoval inode_ref = btrfs_item_ptr(path->nodes[0], 126905dbe683SOmar Sandoval path->slots[0], 127005dbe683SOmar Sandoval struct btrfs_inode_ref); 127105dbe683SOmar Sandoval len = btrfs_inode_ref_name_len(path->nodes[0], 127205dbe683SOmar Sandoval inode_ref); 127305dbe683SOmar Sandoval ptr -= len + 1; 127405dbe683SOmar Sandoval if (ptr < name) { 127505dbe683SOmar Sandoval ret = -ENAMETOOLONG; 127605dbe683SOmar Sandoval goto err; 127705dbe683SOmar Sandoval } 127805dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 127905dbe683SOmar Sandoval (unsigned long)(inode_ref + 1), len); 128005dbe683SOmar Sandoval ptr[0] = '/'; 128105dbe683SOmar Sandoval btrfs_release_path(path); 128205dbe683SOmar Sandoval } 128300246528SJosef Bacik btrfs_put_root(fs_root); 12845168489aSJosef Bacik fs_root = NULL; 128505dbe683SOmar Sandoval } 128605dbe683SOmar Sandoval 128705dbe683SOmar Sandoval btrfs_free_path(path); 128805dbe683SOmar Sandoval if (ptr == name + PATH_MAX - 1) { 128905dbe683SOmar Sandoval name[0] = '/'; 129005dbe683SOmar Sandoval name[1] = '\0'; 129105dbe683SOmar Sandoval } else { 129205dbe683SOmar Sandoval memmove(name, ptr, name + PATH_MAX - ptr); 129305dbe683SOmar Sandoval } 129405dbe683SOmar Sandoval return name; 129505dbe683SOmar Sandoval 129605dbe683SOmar Sandoval err: 129700246528SJosef Bacik btrfs_put_root(fs_root); 129805dbe683SOmar Sandoval btrfs_free_path(path); 129905dbe683SOmar Sandoval kfree(name); 130005dbe683SOmar Sandoval return ERR_PTR(ret); 130105dbe683SOmar Sandoval } 130205dbe683SOmar Sandoval 130305dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid) 130405dbe683SOmar Sandoval { 130505dbe683SOmar Sandoval struct btrfs_root *root = fs_info->tree_root; 130673f73415SJosef Bacik struct btrfs_dir_item *di; 130773f73415SJosef Bacik struct btrfs_path *path; 130873f73415SJosef Bacik struct btrfs_key location; 130973f73415SJosef Bacik u64 dir_id; 131073f73415SJosef Bacik 131173f73415SJosef Bacik path = btrfs_alloc_path(); 131273f73415SJosef Bacik if (!path) 131305dbe683SOmar Sandoval return -ENOMEM; 131473f73415SJosef Bacik 131573f73415SJosef Bacik /* 131673f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 131773f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 131873f73415SJosef Bacik * to mount. 131973f73415SJosef Bacik */ 1320815745cfSAl Viro dir_id = btrfs_super_root_dir(fs_info->super_copy); 132173f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 1322b0839166SJulia Lawall if (IS_ERR(di)) { 1323b0839166SJulia Lawall btrfs_free_path(path); 132405dbe683SOmar Sandoval return PTR_ERR(di); 1325b0839166SJulia Lawall } 132673f73415SJosef Bacik if (!di) { 132773f73415SJosef Bacik /* 132873f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 132973f73415SJosef Bacik * it's always been there, but don't freak out, just try and 133005dbe683SOmar Sandoval * mount the top-level subvolume. 133173f73415SJosef Bacik */ 133273f73415SJosef Bacik btrfs_free_path(path); 133305dbe683SOmar Sandoval *objectid = BTRFS_FS_TREE_OBJECTID; 133405dbe683SOmar Sandoval return 0; 133573f73415SJosef Bacik } 133673f73415SJosef Bacik 133773f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 133873f73415SJosef Bacik btrfs_free_path(path); 133905dbe683SOmar Sandoval *objectid = location.objectid; 134005dbe683SOmar Sandoval return 0; 134173f73415SJosef Bacik } 134273f73415SJosef Bacik 13438a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 13448a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 134556e033a7SDavid Sterba void *data) 13462e635a27SChris Mason { 13472e635a27SChris Mason struct inode *inode; 1348815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 134939279cc3SChris Mason int err; 13502e635a27SChris Mason 13512e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 13522e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 1353e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 1354af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 1355be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 13565103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 13572e635a27SChris Mason sb->s_time_gran = 1; 13580eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 13591751e8a6SLinus Torvalds sb->s_flags |= SB_POSIXACL; 136049cf6f45SChris Ball #endif 1361357fdad0SMatthew Garrett sb->s_flags |= SB_I_VERSION; 1362da2f0f74SChris Mason sb->s_iflags |= SB_I_CGROUPWB; 13639e11ceeeSJan Kara 13649e11ceeeSJan Kara err = super_setup_bdi(sb); 13659e11ceeeSJan Kara if (err) { 13669e11ceeeSJan Kara btrfs_err(fs_info, "super_setup_bdi failed"); 13679e11ceeeSJan Kara return err; 13689e11ceeeSJan Kara } 13699e11ceeeSJan Kara 1370ad2b2c80SAl Viro err = open_ctree(sb, fs_devices, (char *)data); 1371ad2b2c80SAl Viro if (err) { 1372ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "open_ctree failed"); 1373ad2b2c80SAl Viro return err; 1374e20d96d6SChris Mason } 1375b888db2bSChris Mason 13760202e83fSDavid Sterba inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root); 13775d4f98a2SYan Zheng if (IS_ERR(inode)) { 13785d4f98a2SYan Zheng err = PTR_ERR(inode); 137939279cc3SChris Mason goto fail_close; 138039279cc3SChris Mason } 13812e635a27SChris Mason 138248fde701SAl Viro sb->s_root = d_make_root(inode); 138348fde701SAl Viro if (!sb->s_root) { 138439279cc3SChris Mason err = -ENOMEM; 138539279cc3SChris Mason goto fail_close; 13862e635a27SChris Mason } 138758176a96SJosef Bacik 138890a887c9SDan Magenheimer cleancache_init_fs(sb); 13891751e8a6SLinus Torvalds sb->s_flags |= SB_ACTIVE; 13902e635a27SChris Mason return 0; 13912e635a27SChris Mason 139239279cc3SChris Mason fail_close: 13936bccf3abSJeff Mahoney close_ctree(fs_info); 1394d5719762SChris Mason return err; 1395d5719762SChris Mason } 1396d5719762SChris Mason 13976bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 1398d5719762SChris Mason { 1399d5719762SChris Mason struct btrfs_trans_handle *trans; 1400815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1401815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 1402df2ce34cSChris Mason 1403bc074524SJeff Mahoney trace_btrfs_sync_fs(fs_info, wait); 14041abe9b8aSliubo 1405d561c025SChris Mason if (!wait) { 1406815745cfSAl Viro filemap_flush(fs_info->btree_inode->i_mapping); 1407df2ce34cSChris Mason return 0; 1408d561c025SChris Mason } 1409771ed689SChris Mason 14106374e57aSChris Mason btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 1411771ed689SChris Mason 1412d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 141360376ce4SJosef Bacik if (IS_ERR(trans)) { 1414354aa0fbSMiao Xie /* no transaction, don't bother */ 14156b5fe46dSDavid Sterba if (PTR_ERR(trans) == -ENOENT) { 14166b5fe46dSDavid Sterba /* 14176b5fe46dSDavid Sterba * Exit unless we have some pending changes 14186b5fe46dSDavid Sterba * that need to go through commit 14196b5fe46dSDavid Sterba */ 14206b5fe46dSDavid Sterba if (fs_info->pending_changes == 0) 1421bd7de2c9SJosef Bacik return 0; 1422a53f4f8eSQu Wenruo /* 1423a53f4f8eSQu Wenruo * A non-blocking test if the fs is frozen. We must not 1424a53f4f8eSQu Wenruo * start a new transaction here otherwise a deadlock 1425a53f4f8eSQu Wenruo * happens. The pending operations are delayed to the 1426a53f4f8eSQu Wenruo * next commit after thawing. 1427a53f4f8eSQu Wenruo */ 1428a7e3c5f2SRakesh Pandit if (sb_start_write_trylock(sb)) 1429a7e3c5f2SRakesh Pandit sb_end_write(sb); 1430a53f4f8eSQu Wenruo else 1431a53f4f8eSQu Wenruo return 0; 14326b5fe46dSDavid Sterba trans = btrfs_start_transaction(root, 0); 143360376ce4SJosef Bacik } 143498bd5c54SDavid Sterba if (IS_ERR(trans)) 143598bd5c54SDavid Sterba return PTR_ERR(trans); 14366b5fe46dSDavid Sterba } 14373a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 1438d5719762SChris Mason } 1439d5719762SChris Mason 1440ab0b4a3eSJosef Bacik static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed) 1441ab0b4a3eSJosef Bacik { 1442ab0b4a3eSJosef Bacik seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s); 1443ab0b4a3eSJosef Bacik *printed = true; 1444ab0b4a3eSJosef Bacik } 1445ab0b4a3eSJosef Bacik 144634c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 1447a9572a15SEric Paris { 1448815745cfSAl Viro struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 14490f628c63SDavid Sterba const char *compress_type; 14503ef3959bSJosef Bacik const char *subvol_name; 1451ab0b4a3eSJosef Bacik bool printed = false; 1452a9572a15SEric Paris 14533cdde224SJeff Mahoney if (btrfs_test_opt(info, DEGRADED)) 1454a9572a15SEric Paris seq_puts(seq, ",degraded"); 14553cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATASUM)) 1456a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 14573cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATACOW)) 1458a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 14593cdde224SJeff Mahoney if (btrfs_test_opt(info, NOBARRIER)) 1460a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 146195ac567aSFilipe David Borba Manana if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE) 1462c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",max_inline=%llu", info->max_inline); 1463a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 1464a9572a15SEric Paris num_online_cpus() + 2, 8)) 1465f7b885beSAnand Jain seq_printf(seq, ",thread_pool=%u", info->thread_pool_size); 14663cdde224SJeff Mahoney if (btrfs_test_opt(info, COMPRESS)) { 14670f628c63SDavid Sterba compress_type = btrfs_compress_type2str(info->compress_type); 14683cdde224SJeff Mahoney if (btrfs_test_opt(info, FORCE_COMPRESS)) 1469200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 1470200da64eSTsutomu Itoh else 1471200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 1472f51d2b59SDavid Sterba if (info->compress_level) 1473fa4d885aSAdam Borowski seq_printf(seq, ":%d", info->compress_level); 1474200da64eSTsutomu Itoh } 14753cdde224SJeff Mahoney if (btrfs_test_opt(info, NOSSD)) 1476c289811cSChris Mason seq_puts(seq, ",nossd"); 14773cdde224SJeff Mahoney if (btrfs_test_opt(info, SSD_SPREAD)) 1478451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 14793cdde224SJeff Mahoney else if (btrfs_test_opt(info, SSD)) 1480a9572a15SEric Paris seq_puts(seq, ",ssd"); 14813cdde224SJeff Mahoney if (btrfs_test_opt(info, NOTREELOG)) 14826b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 14833cdde224SJeff Mahoney if (btrfs_test_opt(info, NOLOGREPLAY)) 1484ab0b4a3eSJosef Bacik print_rescue_option(seq, "nologreplay", &printed); 148568319c18SJosef Bacik if (btrfs_test_opt(info, USEBACKUPROOT)) 148668319c18SJosef Bacik print_rescue_option(seq, "usebackuproot", &printed); 148742437a63SJosef Bacik if (btrfs_test_opt(info, IGNOREBADROOTS)) 148842437a63SJosef Bacik print_rescue_option(seq, "ignorebadroots", &printed); 1489882dbe0cSJosef Bacik if (btrfs_test_opt(info, IGNOREDATACSUMS)) 1490882dbe0cSJosef Bacik print_rescue_option(seq, "ignoredatacsums", &printed); 14913cdde224SJeff Mahoney if (btrfs_test_opt(info, FLUSHONCOMMIT)) 14926b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 149346b27f50SDennis Zhou if (btrfs_test_opt(info, DISCARD_SYNC)) 149420a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 1495b0643e59SDennis Zhou if (btrfs_test_opt(info, DISCARD_ASYNC)) 1496b0643e59SDennis Zhou seq_puts(seq, ",discard=async"); 14971751e8a6SLinus Torvalds if (!(info->sb->s_flags & SB_POSIXACL)) 1498a9572a15SEric Paris seq_puts(seq, ",noacl"); 149904c41559SBoris Burkov if (btrfs_free_space_cache_v1_active(info)) 1500200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 150104c41559SBoris Burkov else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) 150270f6d82eSOmar Sandoval seq_puts(seq, ",space_cache=v2"); 150373bc1876SJosef Bacik else 15048965593eSDavid Sterba seq_puts(seq, ",nospace_cache"); 15053cdde224SJeff Mahoney if (btrfs_test_opt(info, RESCAN_UUID_TREE)) 1506f420ee1eSStefan Behrens seq_puts(seq, ",rescan_uuid_tree"); 15073cdde224SJeff Mahoney if (btrfs_test_opt(info, CLEAR_CACHE)) 1508200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 15093cdde224SJeff Mahoney if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED)) 1510200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 15113cdde224SJeff Mahoney if (btrfs_test_opt(info, ENOSPC_DEBUG)) 15120942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 15133cdde224SJeff Mahoney if (btrfs_test_opt(info, AUTO_DEFRAG)) 15140942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 15153cdde224SJeff Mahoney if (btrfs_test_opt(info, SKIP_BALANCE)) 15169555c6c1SIlya Dryomov seq_puts(seq, ",skip_balance"); 15178507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 1518cbeaae4fSDavid Sterba if (btrfs_test_opt(info, CHECK_INTEGRITY_DATA)) 15198507d216SWang Shilong seq_puts(seq, ",check_int_data"); 15203cdde224SJeff Mahoney else if (btrfs_test_opt(info, CHECK_INTEGRITY)) 15218507d216SWang Shilong seq_puts(seq, ",check_int"); 15228507d216SWang Shilong if (info->check_integrity_print_mask) 15238507d216SWang Shilong seq_printf(seq, ",check_int_print_mask=%d", 15248507d216SWang Shilong info->check_integrity_print_mask); 15258507d216SWang Shilong #endif 15268507d216SWang Shilong if (info->metadata_ratio) 1527764cb8b4SAnand Jain seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio); 15283cdde224SJeff Mahoney if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR)) 15298c342930SJeff Mahoney seq_puts(seq, ",fatal_errors=panic"); 15308b87dc17SDavid Sterba if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL) 1531d3740608SAnand Jain seq_printf(seq, ",commit=%u", info->commit_interval); 1532d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 15333cdde224SJeff Mahoney if (btrfs_test_opt(info, FRAGMENT_DATA)) 1534d0bd4560SJosef Bacik seq_puts(seq, ",fragment=data"); 15353cdde224SJeff Mahoney if (btrfs_test_opt(info, FRAGMENT_METADATA)) 1536d0bd4560SJosef Bacik seq_puts(seq, ",fragment=metadata"); 1537d0bd4560SJosef Bacik #endif 1538fb592373SJosef Bacik if (btrfs_test_opt(info, REF_VERIFY)) 1539fb592373SJosef Bacik seq_puts(seq, ",ref_verify"); 1540c8d3fe02SOmar Sandoval seq_printf(seq, ",subvolid=%llu", 1541c8d3fe02SOmar Sandoval BTRFS_I(d_inode(dentry))->root->root_key.objectid); 15423ef3959bSJosef Bacik subvol_name = btrfs_get_subvol_name_from_objectid(info, 15433ef3959bSJosef Bacik BTRFS_I(d_inode(dentry))->root->root_key.objectid); 15443ef3959bSJosef Bacik if (!IS_ERR(subvol_name)) { 1545c8d3fe02SOmar Sandoval seq_puts(seq, ",subvol="); 15463ef3959bSJosef Bacik seq_escape(seq, subvol_name, " \t\n\\"); 15473ef3959bSJosef Bacik kfree(subvol_name); 15483ef3959bSJosef Bacik } 1549a9572a15SEric Paris return 0; 1550a9572a15SEric Paris } 1551a9572a15SEric Paris 1552a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 15532e635a27SChris Mason { 1554815745cfSAl Viro struct btrfs_fs_info *p = data; 1555815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(s); 15564b82d6e4SYan 1557815745cfSAl Viro return fs_info->fs_devices == p->fs_devices; 15584b82d6e4SYan } 15594b82d6e4SYan 1560450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 1561450ba0eaSJosef Bacik { 15626de1d09dSAl Viro int err = set_anon_super(s, data); 15636de1d09dSAl Viro if (!err) 1564450ba0eaSJosef Bacik s->s_fs_info = data; 15656de1d09dSAl Viro return err; 1566450ba0eaSJosef Bacik } 1567450ba0eaSJosef Bacik 1568830c4adbSJosef Bacik /* 1569f9d9ef62SDavid Sterba * subvolumes are identified by ino 256 1570f9d9ef62SDavid Sterba */ 1571f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode) 1572f9d9ef62SDavid Sterba { 1573f9d9ef62SDavid Sterba if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 1574f9d9ef62SDavid Sterba return 1; 1575f9d9ef62SDavid Sterba return 0; 1576f9d9ef62SDavid Sterba } 1577f9d9ef62SDavid Sterba 1578bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid, 1579ae0bc863SAnand Jain struct vfsmount *mnt) 1580830c4adbSJosef Bacik { 1581830c4adbSJosef Bacik struct dentry *root; 1582fa330659SOmar Sandoval int ret; 1583830c4adbSJosef Bacik 158405dbe683SOmar Sandoval if (!subvol_name) { 158505dbe683SOmar Sandoval if (!subvol_objectid) { 158605dbe683SOmar Sandoval ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb), 158705dbe683SOmar Sandoval &subvol_objectid); 158805dbe683SOmar Sandoval if (ret) { 158905dbe683SOmar Sandoval root = ERR_PTR(ret); 159005dbe683SOmar Sandoval goto out; 159105dbe683SOmar Sandoval } 159205dbe683SOmar Sandoval } 1593c0c907a4SMarcos Paulo de Souza subvol_name = btrfs_get_subvol_name_from_objectid( 1594c0c907a4SMarcos Paulo de Souza btrfs_sb(mnt->mnt_sb), subvol_objectid); 159505dbe683SOmar Sandoval if (IS_ERR(subvol_name)) { 159605dbe683SOmar Sandoval root = ERR_CAST(subvol_name); 159705dbe683SOmar Sandoval subvol_name = NULL; 159805dbe683SOmar Sandoval goto out; 159905dbe683SOmar Sandoval } 160005dbe683SOmar Sandoval 160105dbe683SOmar Sandoval } 160205dbe683SOmar Sandoval 1603ea441d11SAl Viro root = mount_subtree(mnt, subvol_name); 1604fa330659SOmar Sandoval /* mount_subtree() drops our reference on the vfsmount. */ 1605fa330659SOmar Sandoval mnt = NULL; 1606830c4adbSJosef Bacik 1607bb289b7bSOmar Sandoval if (!IS_ERR(root)) { 1608ea441d11SAl Viro struct super_block *s = root->d_sb; 1609ab8d0fc4SJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(s); 1610bb289b7bSOmar Sandoval struct inode *root_inode = d_inode(root); 1611bb289b7bSOmar Sandoval u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid; 1612bb289b7bSOmar Sandoval 1613bb289b7bSOmar Sandoval ret = 0; 1614bb289b7bSOmar Sandoval if (!is_subvolume_inode(root_inode)) { 1615ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "'%s' is not a valid subvolume", 1616bb289b7bSOmar Sandoval subvol_name); 1617bb289b7bSOmar Sandoval ret = -EINVAL; 1618bb289b7bSOmar Sandoval } 1619bb289b7bSOmar Sandoval if (subvol_objectid && root_objectid != subvol_objectid) { 162005dbe683SOmar Sandoval /* 162105dbe683SOmar Sandoval * This will also catch a race condition where a 162205dbe683SOmar Sandoval * subvolume which was passed by ID is renamed and 162305dbe683SOmar Sandoval * another subvolume is renamed over the old location. 162405dbe683SOmar Sandoval */ 1625ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 1626ab8d0fc4SJeff Mahoney "subvol '%s' does not match subvolid %llu", 1627bb289b7bSOmar Sandoval subvol_name, subvol_objectid); 1628bb289b7bSOmar Sandoval ret = -EINVAL; 1629bb289b7bSOmar Sandoval } 1630bb289b7bSOmar Sandoval if (ret) { 1631ea441d11SAl Viro dput(root); 1632bb289b7bSOmar Sandoval root = ERR_PTR(ret); 1633ea441d11SAl Viro deactivate_locked_super(s); 1634bb289b7bSOmar Sandoval } 1635f9d9ef62SDavid Sterba } 1636f9d9ef62SDavid Sterba 1637fa330659SOmar Sandoval out: 1638fa330659SOmar Sandoval mntput(mnt); 1639fa330659SOmar Sandoval kfree(subvol_name); 1640830c4adbSJosef Bacik return root; 1641830c4adbSJosef Bacik } 1642450ba0eaSJosef Bacik 1643312c89fbSMisono, Tomohiro /* 1644312c89fbSMisono, Tomohiro * Find a superblock for the given device / mount point. 1645312c89fbSMisono, Tomohiro * 1646312c89fbSMisono, Tomohiro * Note: This is based on mount_bdev from fs/super.c with a few additions 1647312c89fbSMisono, Tomohiro * for multiple device setup. Make sure to keep it in sync. 1648312c89fbSMisono, Tomohiro */ 164972fa39f5SMisono, Tomohiro static struct dentry *btrfs_mount_root(struct file_system_type *fs_type, 165072fa39f5SMisono, Tomohiro int flags, const char *device_name, void *data) 165172fa39f5SMisono, Tomohiro { 165272fa39f5SMisono, Tomohiro struct block_device *bdev = NULL; 165372fa39f5SMisono, Tomohiro struct super_block *s; 165436350e95SGu Jinxiang struct btrfs_device *device = NULL; 165572fa39f5SMisono, Tomohiro struct btrfs_fs_devices *fs_devices = NULL; 165672fa39f5SMisono, Tomohiro struct btrfs_fs_info *fs_info = NULL; 1657204cc0ccSAl Viro void *new_sec_opts = NULL; 165872fa39f5SMisono, Tomohiro fmode_t mode = FMODE_READ; 165972fa39f5SMisono, Tomohiro int error = 0; 166072fa39f5SMisono, Tomohiro 166172fa39f5SMisono, Tomohiro if (!(flags & SB_RDONLY)) 166272fa39f5SMisono, Tomohiro mode |= FMODE_WRITE; 166372fa39f5SMisono, Tomohiro 166472fa39f5SMisono, Tomohiro if (data) { 1665a65001e8SAl Viro error = security_sb_eat_lsm_opts(data, &new_sec_opts); 166672fa39f5SMisono, Tomohiro if (error) 166772fa39f5SMisono, Tomohiro return ERR_PTR(error); 166872fa39f5SMisono, Tomohiro } 166972fa39f5SMisono, Tomohiro 167072fa39f5SMisono, Tomohiro /* 167172fa39f5SMisono, Tomohiro * Setup a dummy root and fs_info for test/set super. This is because 167272fa39f5SMisono, Tomohiro * we don't actually fill this stuff out until open_ctree, but we need 16738260edbaSJosef Bacik * then open_ctree will properly initialize the file system specific 16748260edbaSJosef Bacik * settings later. btrfs_init_fs_info initializes the static elements 16758260edbaSJosef Bacik * of the fs_info (locks and such) to make cleanup easier if we find a 16768260edbaSJosef Bacik * superblock with our given fs_devices later on at sget() time. 167772fa39f5SMisono, Tomohiro */ 1678a8fd1f71SJeff Mahoney fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL); 167972fa39f5SMisono, Tomohiro if (!fs_info) { 168072fa39f5SMisono, Tomohiro error = -ENOMEM; 168172fa39f5SMisono, Tomohiro goto error_sec_opts; 168272fa39f5SMisono, Tomohiro } 16838260edbaSJosef Bacik btrfs_init_fs_info(fs_info); 168472fa39f5SMisono, Tomohiro 168572fa39f5SMisono, Tomohiro fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL); 168672fa39f5SMisono, Tomohiro fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL); 168772fa39f5SMisono, Tomohiro if (!fs_info->super_copy || !fs_info->super_for_commit) { 168872fa39f5SMisono, Tomohiro error = -ENOMEM; 168972fa39f5SMisono, Tomohiro goto error_fs_info; 169072fa39f5SMisono, Tomohiro } 169172fa39f5SMisono, Tomohiro 1692f5194e34SDavid Sterba mutex_lock(&uuid_mutex); 1693fa59f27cSAnand Jain error = btrfs_parse_device_options(data, mode, fs_type); 169481ffd56bSDavid Sterba if (error) { 1695399f7f4cSDavid Sterba mutex_unlock(&uuid_mutex); 1696399f7f4cSDavid Sterba goto error_fs_info; 169781ffd56bSDavid Sterba } 1698399f7f4cSDavid Sterba 169936350e95SGu Jinxiang device = btrfs_scan_one_device(device_name, mode, fs_type); 170036350e95SGu Jinxiang if (IS_ERR(device)) { 1701399f7f4cSDavid Sterba mutex_unlock(&uuid_mutex); 170236350e95SGu Jinxiang error = PTR_ERR(device); 1703399f7f4cSDavid Sterba goto error_fs_info; 170481ffd56bSDavid Sterba } 1705399f7f4cSDavid Sterba 170636350e95SGu Jinxiang fs_devices = device->fs_devices; 1707399f7f4cSDavid Sterba fs_info->fs_devices = fs_devices; 1708399f7f4cSDavid Sterba 170972fa39f5SMisono, Tomohiro error = btrfs_open_devices(fs_devices, mode, fs_type); 1710f5194e34SDavid Sterba mutex_unlock(&uuid_mutex); 171172fa39f5SMisono, Tomohiro if (error) 171272fa39f5SMisono, Tomohiro goto error_fs_info; 171372fa39f5SMisono, Tomohiro 171472fa39f5SMisono, Tomohiro if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) { 171572fa39f5SMisono, Tomohiro error = -EACCES; 171672fa39f5SMisono, Tomohiro goto error_close_devices; 171772fa39f5SMisono, Tomohiro } 171872fa39f5SMisono, Tomohiro 171972fa39f5SMisono, Tomohiro bdev = fs_devices->latest_bdev; 172072fa39f5SMisono, Tomohiro s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC, 172172fa39f5SMisono, Tomohiro fs_info); 172272fa39f5SMisono, Tomohiro if (IS_ERR(s)) { 172372fa39f5SMisono, Tomohiro error = PTR_ERR(s); 172472fa39f5SMisono, Tomohiro goto error_close_devices; 172572fa39f5SMisono, Tomohiro } 172672fa39f5SMisono, Tomohiro 172772fa39f5SMisono, Tomohiro if (s->s_root) { 172872fa39f5SMisono, Tomohiro btrfs_close_devices(fs_devices); 17290d4b0463SJosef Bacik btrfs_free_fs_info(fs_info); 173072fa39f5SMisono, Tomohiro if ((flags ^ s->s_flags) & SB_RDONLY) 173172fa39f5SMisono, Tomohiro error = -EBUSY; 173272fa39f5SMisono, Tomohiro } else { 173372fa39f5SMisono, Tomohiro snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev); 173472fa39f5SMisono, Tomohiro btrfs_sb(s)->bdev_holder = fs_type; 17359b4e675aSDavid Sterba if (!strstr(crc32c_impl(), "generic")) 17369b4e675aSDavid Sterba set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags); 173772fa39f5SMisono, Tomohiro error = btrfs_fill_super(s, fs_devices, data); 173872fa39f5SMisono, Tomohiro } 1739a65001e8SAl Viro if (!error) 1740204cc0ccSAl Viro error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL); 1741a65001e8SAl Viro security_free_mnt_opts(&new_sec_opts); 174272fa39f5SMisono, Tomohiro if (error) { 174372fa39f5SMisono, Tomohiro deactivate_locked_super(s); 1744a65001e8SAl Viro return ERR_PTR(error); 174572fa39f5SMisono, Tomohiro } 174672fa39f5SMisono, Tomohiro 174772fa39f5SMisono, Tomohiro return dget(s->s_root); 174872fa39f5SMisono, Tomohiro 174972fa39f5SMisono, Tomohiro error_close_devices: 175072fa39f5SMisono, Tomohiro btrfs_close_devices(fs_devices); 175172fa39f5SMisono, Tomohiro error_fs_info: 17520d4b0463SJosef Bacik btrfs_free_fs_info(fs_info); 175372fa39f5SMisono, Tomohiro error_sec_opts: 175472fa39f5SMisono, Tomohiro security_free_mnt_opts(&new_sec_opts); 175572fa39f5SMisono, Tomohiro return ERR_PTR(error); 175672fa39f5SMisono, Tomohiro } 1757312c89fbSMisono, Tomohiro 1758edf24abeSChristoph Hellwig /* 1759312c89fbSMisono, Tomohiro * Mount function which is called by VFS layer. 1760edf24abeSChristoph Hellwig * 1761312c89fbSMisono, Tomohiro * In order to allow mounting a subvolume directly, btrfs uses mount_subtree() 1762312c89fbSMisono, Tomohiro * which needs vfsmount* of device's root (/). This means device's root has to 1763312c89fbSMisono, Tomohiro * be mounted internally in any case. 1764312c89fbSMisono, Tomohiro * 1765312c89fbSMisono, Tomohiro * Operation flow: 1766312c89fbSMisono, Tomohiro * 1. Parse subvol id related options for later use in mount_subvol(). 1767312c89fbSMisono, Tomohiro * 1768312c89fbSMisono, Tomohiro * 2. Mount device's root (/) by calling vfs_kern_mount(). 1769312c89fbSMisono, Tomohiro * 1770312c89fbSMisono, Tomohiro * NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the 1771312c89fbSMisono, Tomohiro * first place. In order to avoid calling btrfs_mount() again, we use 1772312c89fbSMisono, Tomohiro * different file_system_type which is not registered to VFS by 1773312c89fbSMisono, Tomohiro * register_filesystem() (btrfs_root_fs_type). As a result, 1774312c89fbSMisono, Tomohiro * btrfs_mount_root() is called. The return value will be used by 1775312c89fbSMisono, Tomohiro * mount_subtree() in mount_subvol(). 1776312c89fbSMisono, Tomohiro * 1777312c89fbSMisono, Tomohiro * 3. Call mount_subvol() to get the dentry of subvolume. Since there is 1778312c89fbSMisono, Tomohiro * "btrfs subvolume set-default", mount_subvol() is called always. 1779edf24abeSChristoph Hellwig */ 1780061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1781306e16ceSDavid Sterba const char *device_name, void *data) 17824b82d6e4SYan { 1783312c89fbSMisono, Tomohiro struct vfsmount *mnt_root; 1784312c89fbSMisono, Tomohiro struct dentry *root; 178573f73415SJosef Bacik char *subvol_name = NULL; 178673f73415SJosef Bacik u64 subvol_objectid = 0; 17874b82d6e4SYan int error = 0; 17884b82d6e4SYan 178993b9bcdfSGu Jinxiang error = btrfs_parse_subvol_options(data, &subvol_name, 179093b9bcdfSGu Jinxiang &subvol_objectid); 1791f23c8af8SIlya Dryomov if (error) { 1792f23c8af8SIlya Dryomov kfree(subvol_name); 1793061dbc6bSAl Viro return ERR_PTR(error); 1794f23c8af8SIlya Dryomov } 1795edf24abeSChristoph Hellwig 1796312c89fbSMisono, Tomohiro /* mount device's root (/) */ 1797312c89fbSMisono, Tomohiro mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data); 1798312c89fbSMisono, Tomohiro if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) { 1799312c89fbSMisono, Tomohiro if (flags & SB_RDONLY) { 1800312c89fbSMisono, Tomohiro mnt_root = vfs_kern_mount(&btrfs_root_fs_type, 1801312c89fbSMisono, Tomohiro flags & ~SB_RDONLY, device_name, data); 18024b82d6e4SYan } else { 1803312c89fbSMisono, Tomohiro mnt_root = vfs_kern_mount(&btrfs_root_fs_type, 1804312c89fbSMisono, Tomohiro flags | SB_RDONLY, device_name, data); 1805312c89fbSMisono, Tomohiro if (IS_ERR(mnt_root)) { 1806312c89fbSMisono, Tomohiro root = ERR_CAST(mnt_root); 1807532b618bSEric W. Biederman kfree(subvol_name); 1808312c89fbSMisono, Tomohiro goto out; 1809f667aef6SQu Wenruo } 1810f667aef6SQu Wenruo 1811312c89fbSMisono, Tomohiro down_write(&mnt_root->mnt_sb->s_umount); 1812312c89fbSMisono, Tomohiro error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL); 1813312c89fbSMisono, Tomohiro up_write(&mnt_root->mnt_sb->s_umount); 1814312c89fbSMisono, Tomohiro if (error < 0) { 1815312c89fbSMisono, Tomohiro root = ERR_PTR(error); 1816312c89fbSMisono, Tomohiro mntput(mnt_root); 1817532b618bSEric W. Biederman kfree(subvol_name); 1818312c89fbSMisono, Tomohiro goto out; 1819312c89fbSMisono, Tomohiro } 1820312c89fbSMisono, Tomohiro } 1821312c89fbSMisono, Tomohiro } 1822312c89fbSMisono, Tomohiro if (IS_ERR(mnt_root)) { 1823312c89fbSMisono, Tomohiro root = ERR_CAST(mnt_root); 1824532b618bSEric W. Biederman kfree(subvol_name); 1825312c89fbSMisono, Tomohiro goto out; 1826f667aef6SQu Wenruo } 18274b82d6e4SYan 1828312c89fbSMisono, Tomohiro /* mount_subvol() will free subvol_name and mnt_root */ 1829ae0bc863SAnand Jain root = mount_subvol(subvol_name, subvol_objectid, mnt_root); 18304b82d6e4SYan 1831312c89fbSMisono, Tomohiro out: 1832312c89fbSMisono, Tomohiro return root; 18334b82d6e4SYan } 18342e635a27SChris Mason 18350d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 1836f7b885beSAnand Jain u32 new_pool_size, u32 old_pool_size) 18370d2450abSSergei Trofimovich { 18380d2450abSSergei Trofimovich if (new_pool_size == old_pool_size) 18390d2450abSSergei Trofimovich return; 18400d2450abSSergei Trofimovich 18410d2450abSSergei Trofimovich fs_info->thread_pool_size = new_pool_size; 18420d2450abSSergei Trofimovich 1843efe120a0SFrank Holton btrfs_info(fs_info, "resize thread pool %d -> %d", 18440d2450abSSergei Trofimovich old_pool_size, new_pool_size); 18450d2450abSSergei Trofimovich 18465cdc7ad3SQu Wenruo btrfs_workqueue_set_max(fs_info->workers, new_pool_size); 1847afe3d242SQu Wenruo btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size); 1848e66f0bb1SQu Wenruo btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size); 1849fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size); 1850fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size); 1851fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_write_workers, 1852fccb5d86SQu Wenruo new_pool_size); 1853fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size); 1854fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size); 18555b3bc44eSQu Wenruo btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size); 1856736cfa15SQu Wenruo btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size); 18570339ef2fSQu Wenruo btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers, 1858ff023aacSStefan Behrens new_pool_size); 18590d2450abSSergei Trofimovich } 18600d2450abSSergei Trofimovich 1861f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info, 1862f42a34b2SMiao Xie unsigned long old_opts, int flags) 1863f42a34b2SMiao Xie { 1864dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1865dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 18661751e8a6SLinus Torvalds (flags & SB_RDONLY))) { 1867dc81cdc5SMiao Xie /* wait for any defraggers to finish */ 1868dc81cdc5SMiao Xie wait_event(fs_info->transaction_wait, 1869dc81cdc5SMiao Xie (atomic_read(&fs_info->defrag_running) == 0)); 18701751e8a6SLinus Torvalds if (flags & SB_RDONLY) 1871dc81cdc5SMiao Xie sync_filesystem(fs_info->sb); 1872dc81cdc5SMiao Xie } 1873dc81cdc5SMiao Xie } 1874dc81cdc5SMiao Xie 1875dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info, 1876dc81cdc5SMiao Xie unsigned long old_opts) 1877dc81cdc5SMiao Xie { 187894846229SBoris Burkov const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE); 187994846229SBoris Burkov 1880dc81cdc5SMiao Xie /* 1881180e4d47SLuis de Bethencourt * We need to cleanup all defragable inodes if the autodefragment is 1882180e4d47SLuis de Bethencourt * close or the filesystem is read only. 1883dc81cdc5SMiao Xie */ 1884dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1885bc98a42cSDavid Howells (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) { 1886dc81cdc5SMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 1887dc81cdc5SMiao Xie } 1888dc81cdc5SMiao Xie 1889b0643e59SDennis Zhou /* If we toggled discard async */ 1890b0643e59SDennis Zhou if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) && 1891b0643e59SDennis Zhou btrfs_test_opt(fs_info, DISCARD_ASYNC)) 1892b0643e59SDennis Zhou btrfs_discard_resume(fs_info); 1893b0643e59SDennis Zhou else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) && 1894b0643e59SDennis Zhou !btrfs_test_opt(fs_info, DISCARD_ASYNC)) 1895b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 189694846229SBoris Burkov 189794846229SBoris Burkov /* If we toggled space cache */ 189894846229SBoris Burkov if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) 189994846229SBoris Burkov btrfs_set_free_space_cache_v1_active(fs_info, cache_opt); 1900dc81cdc5SMiao Xie } 1901dc81cdc5SMiao Xie 1902c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1903c146afadSYan Zheng { 1904815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 190549b25e05SJeff Mahoney unsigned old_flags = sb->s_flags; 190649b25e05SJeff Mahoney unsigned long old_opts = fs_info->mount_opt; 190749b25e05SJeff Mahoney unsigned long old_compress_type = fs_info->compress_type; 190849b25e05SJeff Mahoney u64 old_max_inline = fs_info->max_inline; 1909f7b885beSAnand Jain u32 old_thread_pool_size = fs_info->thread_pool_size; 1910d612ac59SAnand Jain u32 old_metadata_ratio = fs_info->metadata_ratio; 1911c146afadSYan Zheng int ret; 1912c146afadSYan Zheng 191302b9984dSTheodore Ts'o sync_filesystem(sb); 191488c4703fSJohannes Thumshirn set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1915dc81cdc5SMiao Xie 1916f667aef6SQu Wenruo if (data) { 1917204cc0ccSAl Viro void *new_sec_opts = NULL; 1918f667aef6SQu Wenruo 1919a65001e8SAl Viro ret = security_sb_eat_lsm_opts(data, &new_sec_opts); 1920a65001e8SAl Viro if (!ret) 1921204cc0ccSAl Viro ret = security_sb_remount(sb, new_sec_opts); 1922a65001e8SAl Viro security_free_mnt_opts(&new_sec_opts); 1923f667aef6SQu Wenruo if (ret) 1924f667aef6SQu Wenruo goto restore; 1925f667aef6SQu Wenruo } 1926f667aef6SQu Wenruo 19272ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, data, *flags); 1928891f41cbSChengguang Xu if (ret) 192949b25e05SJeff Mahoney goto restore; 1930b288052eSChris Mason 1931f42a34b2SMiao Xie btrfs_remount_begin(fs_info, old_opts, *flags); 19320d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 19330d2450abSSergei Trofimovich fs_info->thread_pool_size, old_thread_pool_size); 19340d2450abSSergei Trofimovich 1935c55a4319SBoris Burkov if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) != 1936c55a4319SBoris Burkov (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 19372838d255SBoris Burkov (!sb_rdonly(sb) || (*flags & SB_RDONLY))) { 19382838d255SBoris Burkov btrfs_warn(fs_info, 19392838d255SBoris Burkov "remount supports changing free space tree only from ro to rw"); 19402838d255SBoris Burkov /* Make sure free space cache options match the state on disk */ 19412838d255SBoris Burkov if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 19422838d255SBoris Burkov btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE); 19432838d255SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE); 19442838d255SBoris Burkov } 19452838d255SBoris Burkov if (btrfs_free_space_cache_v1_active(fs_info)) { 19462838d255SBoris Burkov btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE); 19472838d255SBoris Burkov btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE); 19482838d255SBoris Burkov } 19492838d255SBoris Burkov } 19502838d255SBoris Burkov 19511751e8a6SLinus Torvalds if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb)) 1952dc81cdc5SMiao Xie goto out; 1953c146afadSYan Zheng 19541751e8a6SLinus Torvalds if (*flags & SB_RDONLY) { 19558dabb742SStefan Behrens /* 19568dabb742SStefan Behrens * this also happens on 'umount -rf' or on shutdown, when 19578dabb742SStefan Behrens * the filesystem is busy. 19588dabb742SStefan Behrens */ 195921c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 196057056740SJosef Bacik cancel_work_sync(&fs_info->async_data_reclaim_work); 1961361c093dSStefan Behrens 1962b0643e59SDennis Zhou btrfs_discard_cleanup(fs_info); 1963b0643e59SDennis Zhou 1964361c093dSStefan Behrens /* wait for the uuid_scan task to finish */ 1965361c093dSStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 1966361c093dSStefan Behrens /* avoid complains from lockdep et al. */ 1967361c093dSStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 1968361c093dSStefan Behrens 1969a0a1db70SFilipe Manana btrfs_set_sb_rdonly(sb); 1970c146afadSYan Zheng 1971e44163e1SJeff Mahoney /* 19721751e8a6SLinus Torvalds * Setting SB_RDONLY will put the cleaner thread to 1973e44163e1SJeff Mahoney * sleep at the next loop if it's already active. 1974e44163e1SJeff Mahoney * If it's already asleep, we'll leave unused block 1975e44163e1SJeff Mahoney * groups on disk until we're mounted read-write again 1976e44163e1SJeff Mahoney * unless we clean them up here. 1977e44163e1SJeff Mahoney */ 1978e44163e1SJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1979e44163e1SJeff Mahoney 1980a0a1db70SFilipe Manana /* 1981a0a1db70SFilipe Manana * The cleaner task could be already running before we set the 1982a0a1db70SFilipe Manana * flag BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock). 1983a0a1db70SFilipe Manana * We must make sure that after we finish the remount, i.e. after 1984a0a1db70SFilipe Manana * we call btrfs_commit_super(), the cleaner can no longer start 1985a0a1db70SFilipe Manana * a transaction - either because it was dropping a dead root, 1986a0a1db70SFilipe Manana * running delayed iputs or deleting an unused block group (the 1987a0a1db70SFilipe Manana * cleaner picked a block group from the list of unused block 1988a0a1db70SFilipe Manana * groups before we were able to in the previous call to 1989a0a1db70SFilipe Manana * btrfs_delete_unused_bgs()). 1990a0a1db70SFilipe Manana */ 1991a0a1db70SFilipe Manana wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING, 1992a0a1db70SFilipe Manana TASK_UNINTERRUPTIBLE); 1993a0a1db70SFilipe Manana 1994a8cc263eSFilipe Manana /* 1995a8cc263eSFilipe Manana * We've set the superblock to RO mode, so we might have made 1996a8cc263eSFilipe Manana * the cleaner task sleep without running all pending delayed 1997a8cc263eSFilipe Manana * iputs. Go through all the delayed iputs here, so that if an 1998a8cc263eSFilipe Manana * unmount happens without remounting RW we don't end up at 1999a8cc263eSFilipe Manana * finishing close_ctree() with a non-empty list of delayed 2000a8cc263eSFilipe Manana * iputs. 2001a8cc263eSFilipe Manana */ 2002a8cc263eSFilipe Manana btrfs_run_delayed_iputs(fs_info); 2003a8cc263eSFilipe Manana 20048dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 20058dabb742SStefan Behrens btrfs_scrub_cancel(fs_info); 2006061594efSMiao Xie btrfs_pause_balance(fs_info); 20078dabb742SStefan Behrens 2008cb13eea3SFilipe Manana /* 2009cb13eea3SFilipe Manana * Pause the qgroup rescan worker if it is running. We don't want 2010cb13eea3SFilipe Manana * it to be still running after we are in RO mode, as after that, 2011cb13eea3SFilipe Manana * by the time we unmount, it might have left a transaction open, 2012cb13eea3SFilipe Manana * so we would leak the transaction and/or crash. 2013cb13eea3SFilipe Manana */ 2014cb13eea3SFilipe Manana btrfs_qgroup_wait_for_completion(fs_info, false); 2015cb13eea3SFilipe Manana 20166bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 201749b25e05SJeff Mahoney if (ret) 201849b25e05SJeff Mahoney goto restore; 2019c146afadSYan Zheng } else { 20200b246afaSJeff Mahoney if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 20216ef3de9cSDavid Sterba btrfs_err(fs_info, 2022efe120a0SFrank Holton "Remounting read-write after error is not allowed"); 20236ef3de9cSDavid Sterba ret = -EINVAL; 20246ef3de9cSDavid Sterba goto restore; 20256ef3de9cSDavid Sterba } 20268a3db184SSergei Trofimovich if (fs_info->fs_devices->rw_devices == 0) { 202749b25e05SJeff Mahoney ret = -EACCES; 202849b25e05SJeff Mahoney goto restore; 20298a3db184SSergei Trofimovich } 20302b82032cSYan Zheng 20316528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) { 2032efe120a0SFrank Holton btrfs_warn(fs_info, 203352042d8eSAndrea Gelmini "too many missing devices, writable remount is not allowed"); 2034292fd7fcSStefan Behrens ret = -EACCES; 2035292fd7fcSStefan Behrens goto restore; 2036292fd7fcSStefan Behrens } 2037292fd7fcSStefan Behrens 20388a3db184SSergei Trofimovich if (btrfs_super_log_root(fs_info->super_copy) != 0) { 203910a3a3edSDavid Sterba btrfs_warn(fs_info, 204010a3a3edSDavid Sterba "mount required to replay tree-log, cannot remount read-write"); 204149b25e05SJeff Mahoney ret = -EINVAL; 204249b25e05SJeff Mahoney goto restore; 20438a3db184SSergei Trofimovich } 20440bb3eb3eSQu Wenruo if (fs_info->sectorsize < PAGE_SIZE) { 20450bb3eb3eSQu Wenruo btrfs_warn(fs_info, 20460bb3eb3eSQu Wenruo "read-write mount is not yet allowed for sectorsize %u page size %lu", 20470bb3eb3eSQu Wenruo fs_info->sectorsize, PAGE_SIZE); 20480bb3eb3eSQu Wenruo ret = -EINVAL; 20490bb3eb3eSQu Wenruo goto restore; 20500bb3eb3eSQu Wenruo } 2051c146afadSYan Zheng 205244c0ca21SBoris Burkov /* 205344c0ca21SBoris Burkov * NOTE: when remounting with a change that does writes, don't 205444c0ca21SBoris Burkov * put it anywhere above this point, as we are not sure to be 205544c0ca21SBoris Burkov * safe to write until we pass the above checks. 205644c0ca21SBoris Burkov */ 205744c0ca21SBoris Burkov ret = btrfs_start_pre_rw_mount(fs_info); 205849b25e05SJeff Mahoney if (ret) 205949b25e05SJeff Mahoney goto restore; 2060c146afadSYan Zheng 2061a0a1db70SFilipe Manana btrfs_clear_sb_rdonly(sb); 206290c711abSZygo Blaxell 2063afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 2064c146afadSYan Zheng } 2065dc81cdc5SMiao Xie out: 2066faa00889SJosef Bacik /* 2067faa00889SJosef Bacik * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS, 2068faa00889SJosef Bacik * since the absence of the flag means it can be toggled off by remount. 2069faa00889SJosef Bacik */ 2070faa00889SJosef Bacik *flags |= SB_I_VERSION; 2071faa00889SJosef Bacik 20722c6a92b0SJustin Maggard wake_up_process(fs_info->transaction_kthread); 2073dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 20748cd29088SBoris Burkov btrfs_clear_oneshot_options(fs_info); 207588c4703fSJohannes Thumshirn clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 207688c4703fSJohannes Thumshirn 2077c146afadSYan Zheng return 0; 207849b25e05SJeff Mahoney 207949b25e05SJeff Mahoney restore: 20801751e8a6SLinus Torvalds /* We've hit an error - don't reset SB_RDONLY */ 2081bc98a42cSDavid Howells if (sb_rdonly(sb)) 20821751e8a6SLinus Torvalds old_flags |= SB_RDONLY; 2083a0a1db70SFilipe Manana if (!(old_flags & SB_RDONLY)) 2084a0a1db70SFilipe Manana clear_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state); 208549b25e05SJeff Mahoney sb->s_flags = old_flags; 208649b25e05SJeff Mahoney fs_info->mount_opt = old_opts; 208749b25e05SJeff Mahoney fs_info->compress_type = old_compress_type; 208849b25e05SJeff Mahoney fs_info->max_inline = old_max_inline; 20890d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 20900d2450abSSergei Trofimovich old_thread_pool_size, fs_info->thread_pool_size); 209149b25e05SJeff Mahoney fs_info->metadata_ratio = old_metadata_ratio; 2092dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 209388c4703fSJohannes Thumshirn clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 209488c4703fSJohannes Thumshirn 209549b25e05SJeff Mahoney return ret; 2096c146afadSYan Zheng } 2097c146afadSYan Zheng 2098bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 2099*214cc184SDavid Sterba static int btrfs_cmp_device_free_bytes(const void *a, const void *b) 2100bcd53741SArne Jansen { 2101*214cc184SDavid Sterba const struct btrfs_device_info *dev_info1 = a; 2102*214cc184SDavid Sterba const struct btrfs_device_info *dev_info2 = b; 2103*214cc184SDavid Sterba 2104*214cc184SDavid Sterba if (dev_info1->max_avail > dev_info2->max_avail) 2105bcd53741SArne Jansen return -1; 2106*214cc184SDavid Sterba else if (dev_info1->max_avail < dev_info2->max_avail) 2107bcd53741SArne Jansen return 1; 2108bcd53741SArne Jansen return 0; 2109bcd53741SArne Jansen } 2110bcd53741SArne Jansen 2111bcd53741SArne Jansen /* 2112bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 2113bcd53741SArne Jansen * is stored.(Descending Sort) 2114bcd53741SArne Jansen */ 2115bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 2116bcd53741SArne Jansen struct btrfs_device_info *devices, 2117bcd53741SArne Jansen size_t nr_devices) 2118bcd53741SArne Jansen { 2119bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 2120bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 2121bcd53741SArne Jansen } 2122bcd53741SArne Jansen 21236d07bcecSMiao Xie /* 21246d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 21256d07bcecSMiao Xie * file data. 21266d07bcecSMiao Xie */ 21277e17916bSArnd Bergmann static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info, 21286bccf3abSJeff Mahoney u64 *free_bytes) 21296d07bcecSMiao Xie { 21306d07bcecSMiao Xie struct btrfs_device_info *devices_info; 21316d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 21326d07bcecSMiao Xie struct btrfs_device *device; 21336d07bcecSMiao Xie u64 type; 21346d07bcecSMiao Xie u64 avail_space; 21356d07bcecSMiao Xie u64 min_stripe_size; 2136559ca6eaSNikolay Borisov int num_stripes = 1; 21376d07bcecSMiao Xie int i = 0, nr_devices; 21384f080f57SDavid Sterba const struct btrfs_raid_attr *rattr; 21396d07bcecSMiao Xie 21407e33fd99SJosef Bacik /* 214101327610SNicholas D Steeves * We aren't under the device list lock, so this is racy-ish, but good 21427e33fd99SJosef Bacik * enough for our purposes. 21437e33fd99SJosef Bacik */ 2144b772a86eSLi Zefan nr_devices = fs_info->fs_devices->open_devices; 21457e33fd99SJosef Bacik if (!nr_devices) { 21467e33fd99SJosef Bacik smp_mb(); 21477e33fd99SJosef Bacik nr_devices = fs_info->fs_devices->open_devices; 21487e33fd99SJosef Bacik ASSERT(nr_devices); 21497e33fd99SJosef Bacik if (!nr_devices) { 21507e33fd99SJosef Bacik *free_bytes = 0; 21517e33fd99SJosef Bacik return 0; 21527e33fd99SJosef Bacik } 21537e33fd99SJosef Bacik } 21546d07bcecSMiao Xie 2155d9b0d9baSDulshani Gunawardhana devices_info = kmalloc_array(nr_devices, sizeof(*devices_info), 21566a44517dSDavid Sterba GFP_KERNEL); 21576d07bcecSMiao Xie if (!devices_info) 21586d07bcecSMiao Xie return -ENOMEM; 21596d07bcecSMiao Xie 216001327610SNicholas D Steeves /* calc min stripe number for data space allocation */ 21611b86826dSJeff Mahoney type = btrfs_data_alloc_profile(fs_info); 21624f080f57SDavid Sterba rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)]; 21634f080f57SDavid Sterba 2164e1ea2beeSDavid Sterba if (type & BTRFS_BLOCK_GROUP_RAID0) 216539fb26c3SMiao Xie num_stripes = nr_devices; 2166e1ea2beeSDavid Sterba else if (type & BTRFS_BLOCK_GROUP_RAID1) 216739fb26c3SMiao Xie num_stripes = 2; 216847e6f742SDavid Sterba else if (type & BTRFS_BLOCK_GROUP_RAID1C3) 216947e6f742SDavid Sterba num_stripes = 3; 21708d6fac00SDavid Sterba else if (type & BTRFS_BLOCK_GROUP_RAID1C4) 21718d6fac00SDavid Sterba num_stripes = 4; 2172e1ea2beeSDavid Sterba else if (type & BTRFS_BLOCK_GROUP_RAID10) 217339fb26c3SMiao Xie num_stripes = 4; 21746d07bcecSMiao Xie 21754f080f57SDavid Sterba /* Adjust for more than 1 stripe per device */ 21764f080f57SDavid Sterba min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN; 21776d07bcecSMiao Xie 21787e33fd99SJosef Bacik rcu_read_lock(); 21797e33fd99SJosef Bacik list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 2180e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, 2181e12c9621SAnand Jain &device->dev_state) || 2182401e29c1SAnand Jain !device->bdev || 2183401e29c1SAnand Jain test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) 21846d07bcecSMiao Xie continue; 21856d07bcecSMiao Xie 21867e33fd99SJosef Bacik if (i >= nr_devices) 21877e33fd99SJosef Bacik break; 21887e33fd99SJosef Bacik 21896d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 21906d07bcecSMiao Xie 21916d07bcecSMiao Xie /* align with stripe_len */ 2192559ca6eaSNikolay Borisov avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN); 21936d07bcecSMiao Xie 21946d07bcecSMiao Xie /* 219501327610SNicholas D Steeves * In order to avoid overwriting the superblock on the drive, 21966d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 21976d07bcecSMiao Xie * allocation. 2198559ca6eaSNikolay Borisov * 2199559ca6eaSNikolay Borisov * This ensures we have at least min_stripe_size free space 2200559ca6eaSNikolay Borisov * after excluding 1MB. 22016d07bcecSMiao Xie */ 2202559ca6eaSNikolay Borisov if (avail_space <= SZ_1M + min_stripe_size) 22036d07bcecSMiao Xie continue; 22046d07bcecSMiao Xie 2205559ca6eaSNikolay Borisov avail_space -= SZ_1M; 2206559ca6eaSNikolay Borisov 22076d07bcecSMiao Xie devices_info[i].dev = device; 22086d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 22096d07bcecSMiao Xie 22106d07bcecSMiao Xie i++; 22116d07bcecSMiao Xie } 22127e33fd99SJosef Bacik rcu_read_unlock(); 22136d07bcecSMiao Xie 22146d07bcecSMiao Xie nr_devices = i; 22156d07bcecSMiao Xie 22166d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 22176d07bcecSMiao Xie 22186d07bcecSMiao Xie i = nr_devices - 1; 22196d07bcecSMiao Xie avail_space = 0; 2220559ca6eaSNikolay Borisov while (nr_devices >= rattr->devs_min) { 2221559ca6eaSNikolay Borisov num_stripes = min(num_stripes, nr_devices); 222239fb26c3SMiao Xie 22236d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 22246d07bcecSMiao Xie int j; 22256d07bcecSMiao Xie u64 alloc_size; 22266d07bcecSMiao Xie 222739fb26c3SMiao Xie avail_space += devices_info[i].max_avail * num_stripes; 22286d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 222939fb26c3SMiao Xie for (j = i + 1 - num_stripes; j <= i; j++) 22306d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 22316d07bcecSMiao Xie } 22326d07bcecSMiao Xie i--; 22336d07bcecSMiao Xie nr_devices--; 22346d07bcecSMiao Xie } 22356d07bcecSMiao Xie 22366d07bcecSMiao Xie kfree(devices_info); 22376d07bcecSMiao Xie *free_bytes = avail_space; 22386d07bcecSMiao Xie return 0; 22396d07bcecSMiao Xie } 22406d07bcecSMiao Xie 2241ba7b6e62SDavid Sterba /* 2242ba7b6e62SDavid Sterba * Calculate numbers for 'df', pessimistic in case of mixed raid profiles. 2243ba7b6e62SDavid Sterba * 2244ba7b6e62SDavid Sterba * If there's a redundant raid level at DATA block groups, use the respective 2245ba7b6e62SDavid Sterba * multiplier to scale the sizes. 2246ba7b6e62SDavid Sterba * 2247ba7b6e62SDavid Sterba * Unused device space usage is based on simulating the chunk allocator 22480d0c71b3SDavid Sterba * algorithm that respects the device sizes and order of allocations. This is 22490d0c71b3SDavid Sterba * a close approximation of the actual use but there are other factors that may 22500d0c71b3SDavid Sterba * change the result (like a new metadata chunk). 2251ba7b6e62SDavid Sterba * 2252ca8a51b3SDavid Sterba * If metadata is exhausted, f_bavail will be 0. 2253ba7b6e62SDavid Sterba */ 22548fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 22558fd17795SChris Mason { 2256815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 2257815745cfSAl Viro struct btrfs_super_block *disk_super = fs_info->super_copy; 2258bd4d1088SJosef Bacik struct btrfs_space_info *found; 2259bd4d1088SJosef Bacik u64 total_used = 0; 22606d07bcecSMiao Xie u64 total_free_data = 0; 2261ca8a51b3SDavid Sterba u64 total_free_meta = 0; 2262265fdfa6SDavid Sterba u32 bits = fs_info->sectorsize_bits; 2263de37aa51SNikolay Borisov __be32 *fsid = (__be32 *)fs_info->fs_devices->fsid; 2264ba7b6e62SDavid Sterba unsigned factor = 1; 2265ba7b6e62SDavid Sterba struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 22666d07bcecSMiao Xie int ret; 2267ca8a51b3SDavid Sterba u64 thresh = 0; 2268ae02d1bdSLuis de Bethencourt int mixed = 0; 22698fd17795SChris Mason 227072804905SJosef Bacik list_for_each_entry(found, &fs_info->space_info, list) { 22716d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 2272ba7b6e62SDavid Sterba int i; 2273ba7b6e62SDavid Sterba 22746d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 22756d07bcecSMiao Xie total_free_data -= 22766d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 2277ba7b6e62SDavid Sterba 2278ba7b6e62SDavid Sterba for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { 227946df06b8SDavid Sterba if (!list_empty(&found->block_groups[i])) 228046df06b8SDavid Sterba factor = btrfs_bg_type_to_factor( 228146df06b8SDavid Sterba btrfs_raid_array[i].bg_flag); 2282ba7b6e62SDavid Sterba } 22836d07bcecSMiao Xie } 2284ae02d1bdSLuis de Bethencourt 2285ae02d1bdSLuis de Bethencourt /* 2286ae02d1bdSLuis de Bethencourt * Metadata in mixed block goup profiles are accounted in data 2287ae02d1bdSLuis de Bethencourt */ 2288ae02d1bdSLuis de Bethencourt if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) { 2289ae02d1bdSLuis de Bethencourt if (found->flags & BTRFS_BLOCK_GROUP_DATA) 2290ae02d1bdSLuis de Bethencourt mixed = 1; 2291ae02d1bdSLuis de Bethencourt else 2292ae02d1bdSLuis de Bethencourt total_free_meta += found->disk_total - 2293ae02d1bdSLuis de Bethencourt found->disk_used; 2294ae02d1bdSLuis de Bethencourt } 22956d07bcecSMiao Xie 2296b742bb82SYan, Zheng total_used += found->disk_used; 229789a55897SJosef Bacik } 2298ba7b6e62SDavid Sterba 2299ba7b6e62SDavid Sterba buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor); 2300ba7b6e62SDavid Sterba buf->f_blocks >>= bits; 2301ba7b6e62SDavid Sterba buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits); 2302ba7b6e62SDavid Sterba 2303ba7b6e62SDavid Sterba /* Account global block reserve as used, it's in logical size already */ 2304ba7b6e62SDavid Sterba spin_lock(&block_rsv->lock); 230541b34accSLuis de Bethencourt /* Mixed block groups accounting is not byte-accurate, avoid overflow */ 230641b34accSLuis de Bethencourt if (buf->f_bfree >= block_rsv->size >> bits) 2307ba7b6e62SDavid Sterba buf->f_bfree -= block_rsv->size >> bits; 230841b34accSLuis de Bethencourt else 230941b34accSLuis de Bethencourt buf->f_bfree = 0; 2310ba7b6e62SDavid Sterba spin_unlock(&block_rsv->lock); 2311ba7b6e62SDavid Sterba 23120d95c1beSDavid Sterba buf->f_bavail = div_u64(total_free_data, factor); 23136bccf3abSJeff Mahoney ret = btrfs_calc_avail_data_space(fs_info, &total_free_data); 23147e33fd99SJosef Bacik if (ret) 23156d07bcecSMiao Xie return ret; 2316ba7b6e62SDavid Sterba buf->f_bavail += div_u64(total_free_data, factor); 23176d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 2318d397712bSChris Mason 2319ca8a51b3SDavid Sterba /* 2320ca8a51b3SDavid Sterba * We calculate the remaining metadata space minus global reserve. If 2321ca8a51b3SDavid Sterba * this is (supposedly) smaller than zero, there's no space. But this 2322ca8a51b3SDavid Sterba * does not hold in practice, the exhausted state happens where's still 2323ca8a51b3SDavid Sterba * some positive delta. So we apply some guesswork and compare the 2324ca8a51b3SDavid Sterba * delta to a 4M threshold. (Practically observed delta was ~2M.) 2325ca8a51b3SDavid Sterba * 2326ca8a51b3SDavid Sterba * We probably cannot calculate the exact threshold value because this 2327ca8a51b3SDavid Sterba * depends on the internal reservations requested by various 2328ca8a51b3SDavid Sterba * operations, so some operations that consume a few metadata will 2329ca8a51b3SDavid Sterba * succeed even if the Avail is zero. But this is better than the other 2330ca8a51b3SDavid Sterba * way around. 2331ca8a51b3SDavid Sterba */ 2332d4417e22SNikolay Borisov thresh = SZ_4M; 2333ca8a51b3SDavid Sterba 2334d55966c4SJosef Bacik /* 2335d55966c4SJosef Bacik * We only want to claim there's no available space if we can no longer 2336d55966c4SJosef Bacik * allocate chunks for our metadata profile and our global reserve will 2337d55966c4SJosef Bacik * not fit in the free metadata space. If we aren't ->full then we 2338d55966c4SJosef Bacik * still can allocate chunks and thus are fine using the currently 2339d55966c4SJosef Bacik * calculated f_bavail. 2340d55966c4SJosef Bacik */ 2341d55966c4SJosef Bacik if (!mixed && block_rsv->space_info->full && 2342d55966c4SJosef Bacik total_free_meta - thresh < block_rsv->size) 2343ca8a51b3SDavid Sterba buf->f_bavail = 0; 2344ca8a51b3SDavid Sterba 2345ba7b6e62SDavid Sterba buf->f_type = BTRFS_SUPER_MAGIC; 2346ba7b6e62SDavid Sterba buf->f_bsize = dentry->d_sb->s_blocksize; 2347ba7b6e62SDavid Sterba buf->f_namelen = BTRFS_NAME_LEN; 2348ba7b6e62SDavid Sterba 23499d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 23509d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 23519d03632eSDavid Woodhouse on a big-endian or little-endian host */ 23529d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 23539d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 235432d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 23554fd786e6SMisono Tomohiro buf->f_fsid.val[0] ^= 23564fd786e6SMisono Tomohiro BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32; 23574fd786e6SMisono Tomohiro buf->f_fsid.val[1] ^= 23584fd786e6SMisono Tomohiro BTRFS_I(d_inode(dentry))->root->root_key.objectid; 235932d48fa1SDavid Woodhouse 23608fd17795SChris Mason return 0; 23618fd17795SChris Mason } 2362b5133862SChris Mason 2363aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb) 2364aea52e19SAl Viro { 2365815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2366aea52e19SAl Viro kill_anon_super(sb); 23670d4b0463SJosef Bacik btrfs_free_fs_info(fs_info); 2368aea52e19SAl Viro } 2369aea52e19SAl Viro 23702e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 23712e635a27SChris Mason .owner = THIS_MODULE, 23722e635a27SChris Mason .name = "btrfs", 2373061dbc6bSAl Viro .mount = btrfs_mount, 2374aea52e19SAl Viro .kill_sb = btrfs_kill_super, 2375f667aef6SQu Wenruo .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA, 23762e635a27SChris Mason }; 237772fa39f5SMisono, Tomohiro 237872fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type = { 237972fa39f5SMisono, Tomohiro .owner = THIS_MODULE, 238072fa39f5SMisono, Tomohiro .name = "btrfs", 238172fa39f5SMisono, Tomohiro .mount = btrfs_mount_root, 238272fa39f5SMisono, Tomohiro .kill_sb = btrfs_kill_super, 238372fa39f5SMisono, Tomohiro .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA, 238472fa39f5SMisono, Tomohiro }; 238572fa39f5SMisono, Tomohiro 23867f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs"); 2387a9218f6bSChris Mason 2388d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file) 2389d8620958STom Van Braeckel { 2390d8620958STom Van Braeckel /* 2391d8620958STom Van Braeckel * The control file's private_data is used to hold the 2392d8620958STom Van Braeckel * transaction when it is started and is used to keep 2393d8620958STom Van Braeckel * track of whether a transaction is already in progress. 2394d8620958STom Van Braeckel */ 2395d8620958STom Van Braeckel file->private_data = NULL; 2396d8620958STom Van Braeckel return 0; 2397d8620958STom Van Braeckel } 2398d8620958STom Van Braeckel 2399d352ac68SChris Mason /* 2400cfe953c8SSu Yue * Used by /dev/btrfs-control for devices ioctls. 2401d352ac68SChris Mason */ 24028a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 24038a4b83ccSChris Mason unsigned long arg) 24048a4b83ccSChris Mason { 24058a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 240636350e95SGu Jinxiang struct btrfs_device *device = NULL; 2407c071fcfdSChris Mason int ret = -ENOTTY; 24088a4b83ccSChris Mason 2409e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2410e441d54dSChris Mason return -EPERM; 2411e441d54dSChris Mason 2412dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 2413dae7b665SLi Zefan if (IS_ERR(vol)) 2414dae7b665SLi Zefan return PTR_ERR(vol); 2415f505754fSFilipe Manana vol->name[BTRFS_PATH_NAME_MAX] = '\0'; 2416c071fcfdSChris Mason 24178a4b83ccSChris Mason switch (cmd) { 24188a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 2419899f9307SDavid Sterba mutex_lock(&uuid_mutex); 242036350e95SGu Jinxiang device = btrfs_scan_one_device(vol->name, FMODE_READ, 242136350e95SGu Jinxiang &btrfs_root_fs_type); 242236350e95SGu Jinxiang ret = PTR_ERR_OR_ZERO(device); 2423899f9307SDavid Sterba mutex_unlock(&uuid_mutex); 24248a4b83ccSChris Mason break; 2425228a73abSAnand Jain case BTRFS_IOC_FORGET_DEV: 2426228a73abSAnand Jain ret = btrfs_forget_devices(vol->name); 2427228a73abSAnand Jain break; 242802db0844SJosef Bacik case BTRFS_IOC_DEVICES_READY: 2429899f9307SDavid Sterba mutex_lock(&uuid_mutex); 243036350e95SGu Jinxiang device = btrfs_scan_one_device(vol->name, FMODE_READ, 243136350e95SGu Jinxiang &btrfs_root_fs_type); 243236350e95SGu Jinxiang if (IS_ERR(device)) { 2433899f9307SDavid Sterba mutex_unlock(&uuid_mutex); 243436350e95SGu Jinxiang ret = PTR_ERR(device); 243502db0844SJosef Bacik break; 2436899f9307SDavid Sterba } 243736350e95SGu Jinxiang ret = !(device->fs_devices->num_devices == 243836350e95SGu Jinxiang device->fs_devices->total_devices); 2439899f9307SDavid Sterba mutex_unlock(&uuid_mutex); 244002db0844SJosef Bacik break; 2441c5868f83SDavid Sterba case BTRFS_IOC_GET_SUPPORTED_FEATURES: 2442d5131b65SDavid Sterba ret = btrfs_ioctl_get_supported_features((void __user*)arg); 2443c5868f83SDavid Sterba break; 24448a4b83ccSChris Mason } 2445dae7b665SLi Zefan 24468a4b83ccSChris Mason kfree(vol); 2447f819d837SLinda Knippers return ret; 24488a4b83ccSChris Mason } 24498a4b83ccSChris Mason 24500176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 2451ed0dab6bSYan { 2452354aa0fbSMiao Xie struct btrfs_trans_handle *trans; 24530b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(sb); 24540b246afaSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 2455354aa0fbSMiao Xie 2456fac03c8dSDavid Sterba set_bit(BTRFS_FS_FROZEN, &fs_info->flags); 24579e7cc91aSWang Xiaoguang /* 24589e7cc91aSWang Xiaoguang * We don't need a barrier here, we'll wait for any transaction that 24599e7cc91aSWang Xiaoguang * could be in progress on other threads (and do delayed iputs that 24609e7cc91aSWang Xiaoguang * we want to avoid on a frozen filesystem), or do the commit 24619e7cc91aSWang Xiaoguang * ourselves. 24629e7cc91aSWang Xiaoguang */ 2463d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 2464354aa0fbSMiao Xie if (IS_ERR(trans)) { 2465354aa0fbSMiao Xie /* no transaction, don't bother */ 2466354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 24670176260fSLinus Torvalds return 0; 2468354aa0fbSMiao Xie return PTR_ERR(trans); 2469354aa0fbSMiao Xie } 24703a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 2471ed0dab6bSYan } 2472ed0dab6bSYan 24739e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb) 24749e7cc91aSWang Xiaoguang { 2475fac03c8dSDavid Sterba struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2476fac03c8dSDavid Sterba 2477fac03c8dSDavid Sterba clear_bit(BTRFS_FS_FROZEN, &fs_info->flags); 24789e7cc91aSWang Xiaoguang return 0; 24799e7cc91aSWang Xiaoguang } 24809e7cc91aSWang Xiaoguang 24819c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 24829c5085c1SJosef Bacik { 24839c5085c1SJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 24849c5085c1SJosef Bacik struct btrfs_device *dev, *first_dev = NULL; 24859c5085c1SJosef Bacik 248688c14590SDavid Sterba /* 248788c14590SDavid Sterba * Lightweight locking of the devices. We should not need 248888c14590SDavid Sterba * device_list_mutex here as we only read the device data and the list 248988c14590SDavid Sterba * is protected by RCU. Even if a device is deleted during the list 249088c14590SDavid Sterba * traversals, we'll get valid data, the freeing callback will wait at 249152042d8eSAndrea Gelmini * least until the rcu_read_unlock. 249288c14590SDavid Sterba */ 249388c14590SDavid Sterba rcu_read_lock(); 24944faf55b0SAnand Jain list_for_each_entry_rcu(dev, &fs_info->fs_devices->devices, dev_list) { 2495e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 2496aa9ddcd4SJosef Bacik continue; 24970aeb8a6eSAnand Jain if (!dev->name) 24980aeb8a6eSAnand Jain continue; 24999c5085c1SJosef Bacik if (!first_dev || dev->devid < first_dev->devid) 25009c5085c1SJosef Bacik first_dev = dev; 25019c5085c1SJosef Bacik } 25029c5085c1SJosef Bacik 2503672d5990SMisono Tomohiro if (first_dev) 2504672d5990SMisono Tomohiro seq_escape(m, rcu_str_deref(first_dev->name), " \t\n\\"); 2505672d5990SMisono Tomohiro else 25069c5085c1SJosef Bacik WARN_ON(1); 250788c14590SDavid Sterba rcu_read_unlock(); 25089c5085c1SJosef Bacik return 0; 25099c5085c1SJosef Bacik } 25109c5085c1SJosef Bacik 2511b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 251276dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 2513bd555975SAl Viro .evict_inode = btrfs_evict_inode, 2514e20d96d6SChris Mason .put_super = btrfs_put_super, 2515d5719762SChris Mason .sync_fs = btrfs_sync_fs, 2516a9572a15SEric Paris .show_options = btrfs_show_options, 25179c5085c1SJosef Bacik .show_devname = btrfs_show_devname, 25182c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 25192c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 252026602cabSAl Viro .free_inode = btrfs_free_inode, 25218fd17795SChris Mason .statfs = btrfs_statfs, 2522c146afadSYan Zheng .remount_fs = btrfs_remount, 25230176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 25249e7cc91aSWang Xiaoguang .unfreeze_fs = btrfs_unfreeze, 2525e20d96d6SChris Mason }; 2526a9218f6bSChris Mason 2527a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 2528d8620958STom Van Braeckel .open = btrfs_control_open, 2529a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 25301832f2d8SArnd Bergmann .compat_ioctl = compat_ptr_ioctl, 2531a9218f6bSChris Mason .owner = THIS_MODULE, 25326038f373SArnd Bergmann .llseek = noop_llseek, 2533a9218f6bSChris Mason }; 2534a9218f6bSChris Mason 2535a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 2536578454ffSKay Sievers .minor = BTRFS_MINOR, 2537a9218f6bSChris Mason .name = "btrfs-control", 2538a9218f6bSChris Mason .fops = &btrfs_ctl_fops 2539a9218f6bSChris Mason }; 2540a9218f6bSChris Mason 2541578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 2542578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 2543578454ffSKay Sievers 2544f5c29bd9SLiu Bo static int __init btrfs_interface_init(void) 2545a9218f6bSChris Mason { 2546a9218f6bSChris Mason return misc_register(&btrfs_misc); 2547a9218f6bSChris Mason } 2548a9218f6bSChris Mason 2549e67c718bSDavid Sterba static __cold void btrfs_interface_exit(void) 2550a9218f6bSChris Mason { 2551f368ed60SGreg Kroah-Hartman misc_deregister(&btrfs_misc); 2552a9218f6bSChris Mason } 2553a9218f6bSChris Mason 2554f5c29bd9SLiu Bo static void __init btrfs_print_mod_info(void) 255585965600SDavid Sterba { 2556edf57cbfSBart Van Assche static const char options[] = "" 255785965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 255885965600SDavid Sterba ", debug=on" 255985965600SDavid Sterba #endif 256079556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT 256179556c3dSStefan Behrens ", assert=on" 256279556c3dSStefan Behrens #endif 256385965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 256485965600SDavid Sterba ", integrity-checker=on" 256585965600SDavid Sterba #endif 2566fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 2567fb592373SJosef Bacik ", ref-verify=on" 2568fb592373SJosef Bacik #endif 25695b316468SNaohiro Aota #ifdef CONFIG_BLK_DEV_ZONED 25705b316468SNaohiro Aota ", zoned=yes" 25715b316468SNaohiro Aota #else 25725b316468SNaohiro Aota ", zoned=no" 25735b316468SNaohiro Aota #endif 2574edf57cbfSBart Van Assche ; 2575edf57cbfSBart Van Assche pr_info("Btrfs loaded, crc32c=%s%s\n", crc32c_impl(), options); 257685965600SDavid Sterba } 257785965600SDavid Sterba 25782e635a27SChris Mason static int __init init_btrfs_fs(void) 25792e635a27SChris Mason { 25802c90e5d6SChris Mason int err; 258158176a96SJosef Bacik 258263541927SFilipe David Borba Manana btrfs_props_init(); 258363541927SFilipe David Borba Manana 258458176a96SJosef Bacik err = btrfs_init_sysfs(); 258558176a96SJosef Bacik if (err) 25869678c543SNikolay Borisov return err; 258758176a96SJosef Bacik 2588143bede5SJeff Mahoney btrfs_init_compress(); 2589d1310b2eSChris Mason 2590261507a0SLi Zefan err = btrfs_init_cachep(); 2591261507a0SLi Zefan if (err) 2592261507a0SLi Zefan goto free_compress; 2593261507a0SLi Zefan 2594d1310b2eSChris Mason err = extent_io_init(); 25952f4cbe64SWyatt Banks if (err) 25962f4cbe64SWyatt Banks goto free_cachep; 25972f4cbe64SWyatt Banks 25986f0d04f8SJosef Bacik err = extent_state_cache_init(); 2599d1310b2eSChris Mason if (err) 2600d1310b2eSChris Mason goto free_extent_io; 2601d1310b2eSChris Mason 26026f0d04f8SJosef Bacik err = extent_map_init(); 26036f0d04f8SJosef Bacik if (err) 26046f0d04f8SJosef Bacik goto free_extent_state_cache; 26056f0d04f8SJosef Bacik 26066352b91dSMiao Xie err = ordered_data_init(); 26072f4cbe64SWyatt Banks if (err) 26082f4cbe64SWyatt Banks goto free_extent_map; 2609c8b97818SChris Mason 26106352b91dSMiao Xie err = btrfs_delayed_inode_init(); 26116352b91dSMiao Xie if (err) 26126352b91dSMiao Xie goto free_ordered_data; 26136352b91dSMiao Xie 26149247f317SMiao Xie err = btrfs_auto_defrag_init(); 261516cdcec7SMiao Xie if (err) 261616cdcec7SMiao Xie goto free_delayed_inode; 261716cdcec7SMiao Xie 261878a6184aSMiao Xie err = btrfs_delayed_ref_init(); 26199247f317SMiao Xie if (err) 26209247f317SMiao Xie goto free_auto_defrag; 26219247f317SMiao Xie 2622b9e9a6cbSWang Shilong err = btrfs_prelim_ref_init(); 2623b9e9a6cbSWang Shilong if (err) 2624af13b492SDavid Sterba goto free_delayed_ref; 2625b9e9a6cbSWang Shilong 262697eb6b69SDavid Sterba err = btrfs_end_io_wq_init(); 262778a6184aSMiao Xie if (err) 2628af13b492SDavid Sterba goto free_prelim_ref; 262978a6184aSMiao Xie 263097eb6b69SDavid Sterba err = btrfs_interface_init(); 263197eb6b69SDavid Sterba if (err) 263297eb6b69SDavid Sterba goto free_end_io_wq; 263397eb6b69SDavid Sterba 26348ae1af3cSAnand Jain btrfs_print_mod_info(); 2635dc11dd5dSJosef Bacik 2636dc11dd5dSJosef Bacik err = btrfs_run_sanity_tests(); 2637dc11dd5dSJosef Bacik if (err) 2638dc11dd5dSJosef Bacik goto unregister_ioctl; 2639dc11dd5dSJosef Bacik 2640dc11dd5dSJosef Bacik err = register_filesystem(&btrfs_fs_type); 2641dc11dd5dSJosef Bacik if (err) 2642dc11dd5dSJosef Bacik goto unregister_ioctl; 264374255aa0SJosef Bacik 26442f4cbe64SWyatt Banks return 0; 26452f4cbe64SWyatt Banks 2646a9218f6bSChris Mason unregister_ioctl: 2647a9218f6bSChris Mason btrfs_interface_exit(); 264897eb6b69SDavid Sterba free_end_io_wq: 264997eb6b69SDavid Sterba btrfs_end_io_wq_exit(); 2650b9e9a6cbSWang Shilong free_prelim_ref: 2651b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 265278a6184aSMiao Xie free_delayed_ref: 265378a6184aSMiao Xie btrfs_delayed_ref_exit(); 26549247f317SMiao Xie free_auto_defrag: 26559247f317SMiao Xie btrfs_auto_defrag_exit(); 265616cdcec7SMiao Xie free_delayed_inode: 265716cdcec7SMiao Xie btrfs_delayed_inode_exit(); 26586352b91dSMiao Xie free_ordered_data: 26596352b91dSMiao Xie ordered_data_exit(); 26602f4cbe64SWyatt Banks free_extent_map: 26612f4cbe64SWyatt Banks extent_map_exit(); 26626f0d04f8SJosef Bacik free_extent_state_cache: 26636f0d04f8SJosef Bacik extent_state_cache_exit(); 2664d1310b2eSChris Mason free_extent_io: 2665d1310b2eSChris Mason extent_io_exit(); 26662f4cbe64SWyatt Banks free_cachep: 26672f4cbe64SWyatt Banks btrfs_destroy_cachep(); 2668261507a0SLi Zefan free_compress: 2669261507a0SLi Zefan btrfs_exit_compress(); 26702f4cbe64SWyatt Banks btrfs_exit_sysfs(); 26719678c543SNikolay Borisov 26722c90e5d6SChris Mason return err; 26732e635a27SChris Mason } 26742e635a27SChris Mason 26752e635a27SChris Mason static void __exit exit_btrfs_fs(void) 26762e635a27SChris Mason { 267739279cc3SChris Mason btrfs_destroy_cachep(); 267878a6184aSMiao Xie btrfs_delayed_ref_exit(); 26799247f317SMiao Xie btrfs_auto_defrag_exit(); 268016cdcec7SMiao Xie btrfs_delayed_inode_exit(); 2681b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 26826352b91dSMiao Xie ordered_data_exit(); 2683a52d9a80SChris Mason extent_map_exit(); 26846f0d04f8SJosef Bacik extent_state_cache_exit(); 2685d1310b2eSChris Mason extent_io_exit(); 2686a9218f6bSChris Mason btrfs_interface_exit(); 26875ed5f588SJosef Bacik btrfs_end_io_wq_exit(); 26882e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 268958176a96SJosef Bacik btrfs_exit_sysfs(); 26908a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 2691261507a0SLi Zefan btrfs_exit_compress(); 26922e635a27SChris Mason } 26932e635a27SChris Mason 269460efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs); 26952e635a27SChris Mason module_exit(exit_btrfs_fs) 26962e635a27SChris Mason 26972e635a27SChris Mason MODULE_LICENSE("GPL"); 2698d5178578SJohannes Thumshirn MODULE_SOFTDEP("pre: crc32c"); 26993951e7f0SJohannes Thumshirn MODULE_SOFTDEP("pre: xxhash64"); 27003831bf00SJohannes Thumshirn MODULE_SOFTDEP("pre: sha256"); 2701352ae07bSDavid Sterba MODULE_SOFTDEP("pre: blake2b-256"); 2702