xref: /linux/fs/btrfs/super.c (revision 63cd070decb15f540b15f2bc35ecad42dd9cf598)
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>
2622c44fe6SJosef Bacik #include <linux/ratelimit.h>
279678c543SNikolay Borisov #include <linux/crc32c.h>
2855e301fdSFilipe Brandenburger #include <linux/btrfs.h>
2916cdcec7SMiao Xie #include "delayed-inode.h"
302e635a27SChris Mason #include "ctree.h"
31e20d96d6SChris Mason #include "disk-io.h"
32d5719762SChris Mason #include "transaction.h"
332c90e5d6SChris Mason #include "btrfs_inode.h"
343a686375SChris Mason #include "print-tree.h"
3563541927SFilipe David Borba Manana #include "props.h"
365103e947SJosef Bacik #include "xattr.h"
378a4b83ccSChris Mason #include "volumes.h"
38be6e8dc0SBalaji Rao #include "export.h"
39c8b97818SChris Mason #include "compression.h"
409c5085c1SJosef Bacik #include "rcu-string.h"
418dabb742SStefan Behrens #include "dev-replace.h"
4274255aa0SJosef Bacik #include "free-space-cache.h"
43b9e9a6cbSWang Shilong #include "backref.h"
448719aaaeSJosef Bacik #include "space-info.h"
4589439109SDavid Sterba #include "sysfs.h"
46b70f5097SNaohiro Aota #include "zoned.h"
47dc11dd5dSJosef Bacik #include "tests/btrfs-tests.h"
48aac0023cSJosef Bacik #include "block-group.h"
49b0643e59SDennis Zhou #include "discard.h"
50d3982100SMark Fasheh #include "qgroup.h"
511abe9b8aSliubo #define CREATE_TRACE_POINTS
521abe9b8aSliubo #include <trace/events/btrfs.h>
531abe9b8aSliubo 
54b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
5572fa39f5SMisono, Tomohiro 
5672fa39f5SMisono, Tomohiro /*
5772fa39f5SMisono, Tomohiro  * Types for mounting the default subvolume and a subvolume explicitly
5872fa39f5SMisono, Tomohiro  * requested by subvol=/path. That way the callchain is straightforward and we
5972fa39f5SMisono, Tomohiro  * don't have to play tricks with the mount options and recursive calls to
6072fa39f5SMisono, Tomohiro  * btrfs_mount.
61312c89fbSMisono, Tomohiro  *
62312c89fbSMisono, Tomohiro  * The new btrfs_root_fs_type also servers as a tag for the bdev_holder.
6372fa39f5SMisono, Tomohiro  */
64830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type;
6572fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type;
66e20d96d6SChris Mason 
670723a047SHarald Hoyer static int btrfs_remount(struct super_block *sb, int *flags, char *data);
680723a047SHarald Hoyer 
6959131393SJosef Bacik /*
7059131393SJosef Bacik  * Generally the error codes correspond to their respective errors, but there
7159131393SJosef Bacik  * are a few special cases.
7259131393SJosef Bacik  *
7359131393SJosef Bacik  * EUCLEAN: Any sort of corruption that we encounter.  The tree-checker for
7459131393SJosef Bacik  *          instance will return EUCLEAN if any of the blocks are corrupted in
7559131393SJosef Bacik  *          a way that is problematic.  We want to reserve EUCLEAN for these
7659131393SJosef Bacik  *          sort of corruptions.
7759131393SJosef Bacik  *
7859131393SJosef Bacik  * EROFS: If we check BTRFS_FS_STATE_ERROR and fail out with a return error, we
7959131393SJosef Bacik  *        need to use EROFS for this case.  We will have no idea of the
8059131393SJosef Bacik  *        original failure, that will have been reported at the time we tripped
8159131393SJosef Bacik  *        over the error.  Each subsequent error that doesn't have any context
8259131393SJosef Bacik  *        of the original error should use EROFS when handling BTRFS_FS_STATE_ERROR.
8359131393SJosef Bacik  */
844143cb8bSDavid Sterba const char * __attribute_const__ btrfs_decode_error(int errno)
85acce952bSliubo {
8608748810SDavid Sterba 	char *errstr = "unknown";
87acce952bSliubo 
88acce952bSliubo 	switch (errno) {
89d54f8144SDavid Sterba 	case -ENOENT:		/* -2 */
90d54f8144SDavid Sterba 		errstr = "No such entry";
91d54f8144SDavid Sterba 		break;
92d54f8144SDavid Sterba 	case -EIO:		/* -5 */
93acce952bSliubo 		errstr = "IO failure";
94acce952bSliubo 		break;
95d54f8144SDavid Sterba 	case -ENOMEM:		/* -12*/
96acce952bSliubo 		errstr = "Out of memory";
97acce952bSliubo 		break;
98d54f8144SDavid Sterba 	case -EEXIST:		/* -17 */
998c342930SJeff Mahoney 		errstr = "Object already exists";
1008c342930SJeff Mahoney 		break;
101d54f8144SDavid Sterba 	case -ENOSPC:		/* -28 */
10294ef7280SDavid Sterba 		errstr = "No space left";
10394ef7280SDavid Sterba 		break;
104d54f8144SDavid Sterba 	case -EROFS:		/* -30 */
105d54f8144SDavid Sterba 		errstr = "Readonly filesystem";
10694ef7280SDavid Sterba 		break;
107fb8521caSDavid Sterba 	case -EOPNOTSUPP:	/* -95 */
108fb8521caSDavid Sterba 		errstr = "Operation not supported";
109fb8521caSDavid Sterba 		break;
110fb8521caSDavid Sterba 	case -EUCLEAN:		/* -117 */
111fb8521caSDavid Sterba 		errstr = "Filesystem corrupted";
112fb8521caSDavid Sterba 		break;
113fb8521caSDavid Sterba 	case -EDQUOT:		/* -122 */
114fb8521caSDavid Sterba 		errstr = "Quota exceeded";
115fb8521caSDavid Sterba 		break;
116acce952bSliubo 	}
117acce952bSliubo 
118acce952bSliubo 	return errstr;
119acce952bSliubo }
120acce952bSliubo 
121acce952bSliubo /*
12234d97007SAnand Jain  * __btrfs_handle_fs_error decodes expected errors from the caller and
12352042d8eSAndrea Gelmini  * invokes the appropriate error response.
124acce952bSliubo  */
125c0d19e2bSDavid Sterba __cold
12634d97007SAnand Jain void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
1274da35113SJeff Mahoney 		       unsigned int line, int errno, const char *fmt, ...)
128acce952bSliubo {
129acce952bSliubo 	struct super_block *sb = fs_info->sb;
13057d816a1SAnand Jain #ifdef CONFIG_PRINTK
131acce952bSliubo 	const char *errstr;
13257d816a1SAnand Jain #endif
133acce952bSliubo 
134acce952bSliubo 	/*
135acce952bSliubo 	 * Special case: if the error is EROFS, and we're already
1361751e8a6SLinus Torvalds 	 * under SB_RDONLY, then it is safe here.
137acce952bSliubo 	 */
138bc98a42cSDavid Howells 	if (errno == -EROFS && sb_rdonly(sb))
139acce952bSliubo   		return;
140acce952bSliubo 
14157d816a1SAnand Jain #ifdef CONFIG_PRINTK
14208748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
1434da35113SJeff Mahoney 	if (fmt) {
14437252a66SEric Sandeen 		struct va_format vaf;
14537252a66SEric Sandeen 		va_list args;
14637252a66SEric Sandeen 
14737252a66SEric Sandeen 		va_start(args, fmt);
14837252a66SEric Sandeen 		vaf.fmt = fmt;
14937252a66SEric Sandeen 		vaf.va = &args;
1504da35113SJeff Mahoney 
15162e85577SJeff Mahoney 		pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n",
15208748810SDavid Sterba 			sb->s_id, function, line, errno, errstr, &vaf);
15337252a66SEric Sandeen 		va_end(args);
1544da35113SJeff Mahoney 	} else {
15562e85577SJeff Mahoney 		pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s\n",
15608748810SDavid Sterba 			sb->s_id, function, line, errno, errstr);
1574da35113SJeff Mahoney 	}
15857d816a1SAnand Jain #endif
159acce952bSliubo 
1600713d90cSAnand Jain 	/*
1610713d90cSAnand Jain 	 * Today we only save the error info to memory.  Long term we'll
1620713d90cSAnand Jain 	 * also send it down to the disk
1630713d90cSAnand Jain 	 */
1640713d90cSAnand Jain 	set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
1650713d90cSAnand Jain 
1664da35113SJeff Mahoney 	/* Don't go through full error handling during mount */
167922ea899SAnand Jain 	if (!(sb->s_flags & SB_BORN))
168922ea899SAnand Jain 		return;
169922ea899SAnand Jain 
170922ea899SAnand Jain 	if (sb_rdonly(sb))
171922ea899SAnand Jain 		return;
172922ea899SAnand Jain 
173b0643e59SDennis Zhou 	btrfs_discard_stop(fs_info);
174b0643e59SDennis Zhou 
175922ea899SAnand Jain 	/* btrfs handle error by forcing the filesystem readonly */
176a0a1db70SFilipe Manana 	btrfs_set_sb_rdonly(sb);
177922ea899SAnand Jain 	btrfs_info(fs_info, "forced readonly");
178922ea899SAnand Jain 	/*
179922ea899SAnand Jain 	 * Note that a running device replace operation is not canceled here
180922ea899SAnand Jain 	 * although there is no way to update the progress. It would add the
181922ea899SAnand Jain 	 * risk of a deadlock, therefore the canceling is omitted. The only
182922ea899SAnand Jain 	 * penalty is that some I/O remains active until the procedure
18352042d8eSAndrea Gelmini 	 * completes. The next time when the filesystem is mounted writable
184922ea899SAnand Jain 	 * again, the device replace operation continues.
185922ea899SAnand Jain 	 */
186acce952bSliubo }
1874da35113SJeff Mahoney 
18857d816a1SAnand Jain #ifdef CONFIG_PRINTK
189533574c6SJoe Perches static const char * const logtypes[] = {
1904da35113SJeff Mahoney 	"emergency",
1914da35113SJeff Mahoney 	"alert",
1924da35113SJeff Mahoney 	"critical",
1934da35113SJeff Mahoney 	"error",
1944da35113SJeff Mahoney 	"warning",
1954da35113SJeff Mahoney 	"notice",
1964da35113SJeff Mahoney 	"info",
1974da35113SJeff Mahoney 	"debug",
1984da35113SJeff Mahoney };
1994da35113SJeff Mahoney 
20035f4e5e6SNikolay Borisov 
20135f4e5e6SNikolay Borisov /*
20235f4e5e6SNikolay Borisov  * Use one ratelimit state per log level so that a flood of less important
20335f4e5e6SNikolay Borisov  * messages doesn't cause more important ones to be dropped.
20435f4e5e6SNikolay Borisov  */
20535f4e5e6SNikolay Borisov static struct ratelimit_state printk_limits[] = {
20635f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100),
20735f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100),
20835f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100),
20935f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100),
21035f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100),
21135f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100),
21235f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100),
21335f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100),
21435f4e5e6SNikolay Borisov };
21535f4e5e6SNikolay Borisov 
216b105e927SDavid Sterba void __cold btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
2174da35113SJeff Mahoney {
21840f7828bSPetr Mladek 	char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0";
2194da35113SJeff Mahoney 	struct va_format vaf;
2204da35113SJeff Mahoney 	va_list args;
221533574c6SJoe Perches 	int kern_level;
22240f7828bSPetr Mladek 	const char *type = logtypes[4];
22340f7828bSPetr Mladek 	struct ratelimit_state *ratelimit = &printk_limits[4];
2244da35113SJeff Mahoney 
2254da35113SJeff Mahoney 	va_start(args, fmt);
2264da35113SJeff Mahoney 
227262c5e86SPetr Mladek 	while ((kern_level = printk_get_level(fmt)) != 0) {
228533574c6SJoe Perches 		size_t size = printk_skip_level(fmt) - fmt;
229262c5e86SPetr Mladek 
230262c5e86SPetr Mladek 		if (kern_level >= '0' && kern_level <= '7') {
231533574c6SJoe Perches 			memcpy(lvl, fmt,  size);
232533574c6SJoe Perches 			lvl[size] = '\0';
233533574c6SJoe Perches 			type = logtypes[kern_level - '0'];
23435f4e5e6SNikolay Borisov 			ratelimit = &printk_limits[kern_level - '0'];
235262c5e86SPetr Mladek 		}
236262c5e86SPetr Mladek 		fmt += size;
237262c5e86SPetr Mladek 	}
238262c5e86SPetr Mladek 
2394da35113SJeff Mahoney 	vaf.fmt = fmt;
2404da35113SJeff Mahoney 	vaf.va = &args;
241533574c6SJoe Perches 
242a0f6d924SDavid Sterba 	if (__ratelimit(ratelimit)) {
243a0f6d924SDavid Sterba 		if (fs_info)
2443993b112SColin Ian King 			printk("%sBTRFS %s (device %s): %pV\n", lvl, type,
245a0f6d924SDavid Sterba 				fs_info->sb->s_id, &vaf);
246a0f6d924SDavid Sterba 		else
247a0f6d924SDavid Sterba 			printk("%sBTRFS %s: %pV\n", lvl, type, &vaf);
248a0f6d924SDavid Sterba 	}
249533574c6SJoe Perches 
250533574c6SJoe Perches 	va_end(args);
2514da35113SJeff Mahoney }
252533574c6SJoe Perches #endif
253533574c6SJoe Perches 
254e9306ad4SQu Wenruo #if BITS_PER_LONG == 32
255e9306ad4SQu Wenruo void __cold btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info)
256e9306ad4SQu Wenruo {
257e9306ad4SQu Wenruo 	if (!test_and_set_bit(BTRFS_FS_32BIT_WARN, &fs_info->flags)) {
258e9306ad4SQu Wenruo 		btrfs_warn(fs_info, "reaching 32bit limit for logical addresses");
259e9306ad4SQu Wenruo 		btrfs_warn(fs_info,
260e9306ad4SQu Wenruo "due to page cache limit on 32bit systems, btrfs can't access metadata at or beyond %lluT",
261e9306ad4SQu Wenruo 			   BTRFS_32BIT_MAX_FILE_SIZE >> 40);
262e9306ad4SQu Wenruo 		btrfs_warn(fs_info,
263e9306ad4SQu Wenruo 			   "please consider upgrading to 64bit kernel/hardware");
264e9306ad4SQu Wenruo 	}
265e9306ad4SQu Wenruo }
266e9306ad4SQu Wenruo 
267e9306ad4SQu Wenruo void __cold btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info)
268e9306ad4SQu Wenruo {
269e9306ad4SQu Wenruo 	if (!test_and_set_bit(BTRFS_FS_32BIT_ERROR, &fs_info->flags)) {
270e9306ad4SQu Wenruo 		btrfs_err(fs_info, "reached 32bit limit for logical addresses");
271e9306ad4SQu Wenruo 		btrfs_err(fs_info,
272e9306ad4SQu Wenruo "due to page cache limit on 32bit systems, metadata beyond %lluT can't be accessed",
273e9306ad4SQu Wenruo 			  BTRFS_32BIT_MAX_FILE_SIZE >> 40);
274e9306ad4SQu Wenruo 		btrfs_err(fs_info,
275e9306ad4SQu Wenruo 			   "please consider upgrading to 64bit kernel/hardware");
276e9306ad4SQu Wenruo 	}
277e9306ad4SQu Wenruo }
278e9306ad4SQu Wenruo #endif
279e9306ad4SQu Wenruo 
2808c342930SJeff Mahoney /*
28149b25e05SJeff Mahoney  * We only mark the transaction aborted and then set the file system read-only.
28249b25e05SJeff Mahoney  * This will prevent new transactions from starting or trying to join this
28349b25e05SJeff Mahoney  * one.
28449b25e05SJeff Mahoney  *
28549b25e05SJeff Mahoney  * This means that error recovery at the call site is limited to freeing
28649b25e05SJeff Mahoney  * any local memory allocations and passing the error code up without
28749b25e05SJeff Mahoney  * further cleanup. The transaction should complete as it normally would
28849b25e05SJeff Mahoney  * in the call path but will return -EIO.
28949b25e05SJeff Mahoney  *
29049b25e05SJeff Mahoney  * We'll complete the cleanup in btrfs_end_transaction and
29149b25e05SJeff Mahoney  * btrfs_commit_transaction.
29249b25e05SJeff Mahoney  */
293c0d19e2bSDavid Sterba __cold
29449b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
29566642832SJeff Mahoney 			       const char *function,
29649b25e05SJeff Mahoney 			       unsigned int line, int errno)
29749b25e05SJeff Mahoney {
29866642832SJeff Mahoney 	struct btrfs_fs_info *fs_info = trans->fs_info;
29966642832SJeff Mahoney 
300bf31f87fSDavid Sterba 	WRITE_ONCE(trans->aborted, errno);
30120c7bcecSSeraphime Kirkovski 	WRITE_ONCE(trans->transaction->aborted, errno);
302501407aaSJosef Bacik 	/* Wake up anybody who may be waiting on this transaction */
30366642832SJeff Mahoney 	wake_up(&fs_info->transaction_wait);
30466642832SJeff Mahoney 	wake_up(&fs_info->transaction_blocked_wait);
30566642832SJeff Mahoney 	__btrfs_handle_fs_error(fs_info, function, line, errno, NULL);
30649b25e05SJeff Mahoney }
30749b25e05SJeff Mahoney /*
3088c342930SJeff Mahoney  * __btrfs_panic decodes unexpected, fatal errors from the caller,
3098c342930SJeff Mahoney  * issues an alert, and either panics or BUGs, depending on mount options.
3108c342930SJeff Mahoney  */
311c0d19e2bSDavid Sterba __cold
3128c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
3138c342930SJeff Mahoney 		   unsigned int line, int errno, const char *fmt, ...)
3148c342930SJeff Mahoney {
3158c342930SJeff Mahoney 	char *s_id = "<unknown>";
3168c342930SJeff Mahoney 	const char *errstr;
3178c342930SJeff Mahoney 	struct va_format vaf = { .fmt = fmt };
3188c342930SJeff Mahoney 	va_list args;
3198c342930SJeff Mahoney 
3208c342930SJeff Mahoney 	if (fs_info)
3218c342930SJeff Mahoney 		s_id = fs_info->sb->s_id;
3228c342930SJeff Mahoney 
3238c342930SJeff Mahoney 	va_start(args, fmt);
3248c342930SJeff Mahoney 	vaf.va = &args;
3258c342930SJeff Mahoney 
32608748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
327d8953d69SSatoru Takeuchi 	if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR)))
32808748810SDavid Sterba 		panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
32908748810SDavid Sterba 			s_id, function, line, &vaf, errno, errstr);
3308c342930SJeff Mahoney 
331efe120a0SFrank Holton 	btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
332efe120a0SFrank Holton 		   function, line, &vaf, errno, errstr);
3338c342930SJeff Mahoney 	va_end(args);
3348c342930SJeff Mahoney 	/* Caller calls BUG() */
3358c342930SJeff Mahoney }
336acce952bSliubo 
337e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
338e20d96d6SChris Mason {
3396bccf3abSJeff Mahoney 	close_ctree(btrfs_sb(sb));
340e20d96d6SChris Mason }
3412e635a27SChris Mason 
34295e05289SChris Mason enum {
343416a7202SDavid Sterba 	Opt_acl, Opt_noacl,
344416a7202SDavid Sterba 	Opt_clear_cache,
345416a7202SDavid Sterba 	Opt_commit_interval,
346416a7202SDavid Sterba 	Opt_compress,
347416a7202SDavid Sterba 	Opt_compress_force,
348416a7202SDavid Sterba 	Opt_compress_force_type,
349416a7202SDavid Sterba 	Opt_compress_type,
350416a7202SDavid Sterba 	Opt_degraded,
351416a7202SDavid Sterba 	Opt_device,
352416a7202SDavid Sterba 	Opt_fatal_errors,
353416a7202SDavid Sterba 	Opt_flushoncommit, Opt_noflushoncommit,
354416a7202SDavid Sterba 	Opt_max_inline,
355416a7202SDavid Sterba 	Opt_barrier, Opt_nobarrier,
356416a7202SDavid Sterba 	Opt_datacow, Opt_nodatacow,
357416a7202SDavid Sterba 	Opt_datasum, Opt_nodatasum,
358416a7202SDavid Sterba 	Opt_defrag, Opt_nodefrag,
359416a7202SDavid Sterba 	Opt_discard, Opt_nodiscard,
360b0643e59SDennis Zhou 	Opt_discard_mode,
361416a7202SDavid Sterba 	Opt_norecovery,
362416a7202SDavid Sterba 	Opt_ratio,
363416a7202SDavid Sterba 	Opt_rescan_uuid_tree,
364416a7202SDavid Sterba 	Opt_skip_balance,
365416a7202SDavid Sterba 	Opt_space_cache, Opt_no_space_cache,
366416a7202SDavid Sterba 	Opt_space_cache_version,
367416a7202SDavid Sterba 	Opt_ssd, Opt_nossd,
368416a7202SDavid Sterba 	Opt_ssd_spread, Opt_nossd_spread,
369416a7202SDavid Sterba 	Opt_subvol,
37037becec9SOmar Sandoval 	Opt_subvol_empty,
371416a7202SDavid Sterba 	Opt_subvolid,
372416a7202SDavid Sterba 	Opt_thread_pool,
373416a7202SDavid Sterba 	Opt_treelog, Opt_notreelog,
374416a7202SDavid Sterba 	Opt_user_subvol_rm_allowed,
375416a7202SDavid Sterba 
37674ef0018SQu Wenruo 	/* Rescue options */
37774ef0018SQu Wenruo 	Opt_rescue,
37874ef0018SQu Wenruo 	Opt_usebackuproot,
37974ef0018SQu Wenruo 	Opt_nologreplay,
38042437a63SJosef Bacik 	Opt_ignorebadroots,
381882dbe0cSJosef Bacik 	Opt_ignoredatacsums,
3829037d3cbSJosef Bacik 	Opt_rescue_all,
38374ef0018SQu Wenruo 
384416a7202SDavid Sterba 	/* Deprecated options */
385416a7202SDavid Sterba 	Opt_recovery,
3865297199aSNikolay Borisov 	Opt_inode_cache, Opt_noinode_cache,
387416a7202SDavid Sterba 
388416a7202SDavid Sterba 	/* Debugging options */
389416a7202SDavid Sterba 	Opt_check_integrity,
39070f6d82eSOmar Sandoval 	Opt_check_integrity_including_extent_data,
391416a7202SDavid Sterba 	Opt_check_integrity_print_mask,
392416a7202SDavid Sterba 	Opt_enospc_debug, Opt_noenospc_debug,
393d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
394d0bd4560SJosef Bacik 	Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
395d0bd4560SJosef Bacik #endif
396fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
397fb592373SJosef Bacik 	Opt_ref_verify,
398fb592373SJosef Bacik #endif
3999555c6c1SIlya Dryomov 	Opt_err,
40095e05289SChris Mason };
40195e05289SChris Mason 
4024d4ab6d6SDavid Sterba static const match_table_t tokens = {
403416a7202SDavid Sterba 	{Opt_acl, "acl"},
404416a7202SDavid Sterba 	{Opt_noacl, "noacl"},
405416a7202SDavid Sterba 	{Opt_clear_cache, "clear_cache"},
406416a7202SDavid Sterba 	{Opt_commit_interval, "commit=%u"},
407c8b97818SChris Mason 	{Opt_compress, "compress"},
408261507a0SLi Zefan 	{Opt_compress_type, "compress=%s"},
409a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
410261507a0SLi Zefan 	{Opt_compress_force_type, "compress-force=%s"},
411416a7202SDavid Sterba 	{Opt_degraded, "degraded"},
412416a7202SDavid Sterba 	{Opt_device, "device=%s"},
413416a7202SDavid Sterba 	{Opt_fatal_errors, "fatal_errors=%s"},
414dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
4152c9ee856SQu Wenruo 	{Opt_noflushoncommit, "noflushoncommit"},
4164b9465cbSChris Mason 	{Opt_inode_cache, "inode_cache"},
4173818aea2SQu Wenruo 	{Opt_noinode_cache, "noinode_cache"},
418416a7202SDavid Sterba 	{Opt_max_inline, "max_inline=%s"},
419416a7202SDavid Sterba 	{Opt_barrier, "barrier"},
420416a7202SDavid Sterba 	{Opt_nobarrier, "nobarrier"},
421416a7202SDavid Sterba 	{Opt_datacow, "datacow"},
422416a7202SDavid Sterba 	{Opt_nodatacow, "nodatacow"},
423416a7202SDavid Sterba 	{Opt_datasum, "datasum"},
424416a7202SDavid Sterba 	{Opt_nodatasum, "nodatasum"},
425416a7202SDavid Sterba 	{Opt_defrag, "autodefrag"},
426416a7202SDavid Sterba 	{Opt_nodefrag, "noautodefrag"},
427416a7202SDavid Sterba 	{Opt_discard, "discard"},
428b0643e59SDennis Zhou 	{Opt_discard_mode, "discard=%s"},
429416a7202SDavid Sterba 	{Opt_nodiscard, "nodiscard"},
430416a7202SDavid Sterba 	{Opt_norecovery, "norecovery"},
431416a7202SDavid Sterba 	{Opt_ratio, "metadata_ratio=%u"},
432416a7202SDavid Sterba 	{Opt_rescan_uuid_tree, "rescan_uuid_tree"},
4339555c6c1SIlya Dryomov 	{Opt_skip_balance, "skip_balance"},
434416a7202SDavid Sterba 	{Opt_space_cache, "space_cache"},
435416a7202SDavid Sterba 	{Opt_no_space_cache, "nospace_cache"},
436416a7202SDavid Sterba 	{Opt_space_cache_version, "space_cache=%s"},
437416a7202SDavid Sterba 	{Opt_ssd, "ssd"},
438416a7202SDavid Sterba 	{Opt_nossd, "nossd"},
439416a7202SDavid Sterba 	{Opt_ssd_spread, "ssd_spread"},
440416a7202SDavid Sterba 	{Opt_nossd_spread, "nossd_spread"},
441416a7202SDavid Sterba 	{Opt_subvol, "subvol=%s"},
44237becec9SOmar Sandoval 	{Opt_subvol_empty, "subvol="},
443416a7202SDavid Sterba 	{Opt_subvolid, "subvolid=%s"},
444416a7202SDavid Sterba 	{Opt_thread_pool, "thread_pool=%u"},
445416a7202SDavid Sterba 	{Opt_treelog, "treelog"},
446416a7202SDavid Sterba 	{Opt_notreelog, "notreelog"},
447416a7202SDavid Sterba 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
448416a7202SDavid Sterba 
44974ef0018SQu Wenruo 	/* Rescue options */
45074ef0018SQu Wenruo 	{Opt_rescue, "rescue=%s"},
45174ef0018SQu Wenruo 	/* Deprecated, with alias rescue=nologreplay */
45274ef0018SQu Wenruo 	{Opt_nologreplay, "nologreplay"},
45374ef0018SQu Wenruo 	/* Deprecated, with alias rescue=usebackuproot */
45474ef0018SQu Wenruo 	{Opt_usebackuproot, "usebackuproot"},
45574ef0018SQu Wenruo 
456416a7202SDavid Sterba 	/* Deprecated options */
457416a7202SDavid Sterba 	{Opt_recovery, "recovery"},
458416a7202SDavid Sterba 
459416a7202SDavid Sterba 	/* Debugging options */
46021adbd5cSStefan Behrens 	{Opt_check_integrity, "check_int"},
46121adbd5cSStefan Behrens 	{Opt_check_integrity_including_extent_data, "check_int_data"},
46202453bdeSAnand Jain 	{Opt_check_integrity_print_mask, "check_int_print_mask=%u"},
463416a7202SDavid Sterba 	{Opt_enospc_debug, "enospc_debug"},
464416a7202SDavid Sterba 	{Opt_noenospc_debug, "noenospc_debug"},
465d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
466d0bd4560SJosef Bacik 	{Opt_fragment_data, "fragment=data"},
467d0bd4560SJosef Bacik 	{Opt_fragment_metadata, "fragment=metadata"},
468d0bd4560SJosef Bacik 	{Opt_fragment_all, "fragment=all"},
469d0bd4560SJosef Bacik #endif
470fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
471fb592373SJosef Bacik 	{Opt_ref_verify, "ref_verify"},
472fb592373SJosef Bacik #endif
47333268eafSJosef Bacik 	{Opt_err, NULL},
47495e05289SChris Mason };
47595e05289SChris Mason 
47674ef0018SQu Wenruo static const match_table_t rescue_tokens = {
47774ef0018SQu Wenruo 	{Opt_usebackuproot, "usebackuproot"},
47874ef0018SQu Wenruo 	{Opt_nologreplay, "nologreplay"},
47942437a63SJosef Bacik 	{Opt_ignorebadroots, "ignorebadroots"},
48042437a63SJosef Bacik 	{Opt_ignorebadroots, "ibadroots"},
481882dbe0cSJosef Bacik 	{Opt_ignoredatacsums, "ignoredatacsums"},
482882dbe0cSJosef Bacik 	{Opt_ignoredatacsums, "idatacsums"},
4839037d3cbSJosef Bacik 	{Opt_rescue_all, "all"},
48474ef0018SQu Wenruo 	{Opt_err, NULL},
48574ef0018SQu Wenruo };
48674ef0018SQu Wenruo 
487d70bf748SJosef Bacik static bool check_ro_option(struct btrfs_fs_info *fs_info, unsigned long opt,
488d70bf748SJosef Bacik 			    const char *opt_name)
489d70bf748SJosef Bacik {
490d70bf748SJosef Bacik 	if (fs_info->mount_opt & opt) {
491d70bf748SJosef Bacik 		btrfs_err(fs_info, "%s must be used with ro mount option",
492d70bf748SJosef Bacik 			  opt_name);
493d70bf748SJosef Bacik 		return true;
494d70bf748SJosef Bacik 	}
495d70bf748SJosef Bacik 	return false;
496d70bf748SJosef Bacik }
497d70bf748SJosef Bacik 
49874ef0018SQu Wenruo static int parse_rescue_options(struct btrfs_fs_info *info, const char *options)
49974ef0018SQu Wenruo {
50074ef0018SQu Wenruo 	char *opts;
50174ef0018SQu Wenruo 	char *orig;
50274ef0018SQu Wenruo 	char *p;
50374ef0018SQu Wenruo 	substring_t args[MAX_OPT_ARGS];
50474ef0018SQu Wenruo 	int ret = 0;
50574ef0018SQu Wenruo 
50674ef0018SQu Wenruo 	opts = kstrdup(options, GFP_KERNEL);
50774ef0018SQu Wenruo 	if (!opts)
50874ef0018SQu Wenruo 		return -ENOMEM;
50974ef0018SQu Wenruo 	orig = opts;
51074ef0018SQu Wenruo 
51174ef0018SQu Wenruo 	while ((p = strsep(&opts, ":")) != NULL) {
51274ef0018SQu Wenruo 		int token;
51374ef0018SQu Wenruo 
51474ef0018SQu Wenruo 		if (!*p)
51574ef0018SQu Wenruo 			continue;
51674ef0018SQu Wenruo 		token = match_token(p, rescue_tokens, args);
51774ef0018SQu Wenruo 		switch (token){
51874ef0018SQu Wenruo 		case Opt_usebackuproot:
51974ef0018SQu Wenruo 			btrfs_info(info,
52074ef0018SQu Wenruo 				   "trying to use backup root at mount time");
52174ef0018SQu Wenruo 			btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
52274ef0018SQu Wenruo 			break;
52374ef0018SQu Wenruo 		case Opt_nologreplay:
52474ef0018SQu Wenruo 			btrfs_set_and_info(info, NOLOGREPLAY,
52574ef0018SQu Wenruo 					   "disabling log replay at mount time");
52674ef0018SQu Wenruo 			break;
52742437a63SJosef Bacik 		case Opt_ignorebadroots:
52842437a63SJosef Bacik 			btrfs_set_and_info(info, IGNOREBADROOTS,
52942437a63SJosef Bacik 					   "ignoring bad roots");
53042437a63SJosef Bacik 			break;
531882dbe0cSJosef Bacik 		case Opt_ignoredatacsums:
532882dbe0cSJosef Bacik 			btrfs_set_and_info(info, IGNOREDATACSUMS,
533882dbe0cSJosef Bacik 					   "ignoring data csums");
534882dbe0cSJosef Bacik 			break;
5359037d3cbSJosef Bacik 		case Opt_rescue_all:
5369037d3cbSJosef Bacik 			btrfs_info(info, "enabling all of the rescue options");
5379037d3cbSJosef Bacik 			btrfs_set_and_info(info, IGNOREDATACSUMS,
5389037d3cbSJosef Bacik 					   "ignoring data csums");
5399037d3cbSJosef Bacik 			btrfs_set_and_info(info, IGNOREBADROOTS,
5409037d3cbSJosef Bacik 					   "ignoring bad roots");
5419037d3cbSJosef Bacik 			btrfs_set_and_info(info, NOLOGREPLAY,
5429037d3cbSJosef Bacik 					   "disabling log replay at mount time");
5439037d3cbSJosef Bacik 			break;
54474ef0018SQu Wenruo 		case Opt_err:
54574ef0018SQu Wenruo 			btrfs_info(info, "unrecognized rescue option '%s'", p);
54674ef0018SQu Wenruo 			ret = -EINVAL;
54774ef0018SQu Wenruo 			goto out;
54874ef0018SQu Wenruo 		default:
54974ef0018SQu Wenruo 			break;
55074ef0018SQu Wenruo 		}
55174ef0018SQu Wenruo 
55274ef0018SQu Wenruo 	}
55374ef0018SQu Wenruo out:
55474ef0018SQu Wenruo 	kfree(orig);
55574ef0018SQu Wenruo 	return ret;
55674ef0018SQu Wenruo }
55774ef0018SQu Wenruo 
558edf24abeSChristoph Hellwig /*
559edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
560edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
56149b25e05SJeff Mahoney  * XXX JDM: This needs to be cleaned up for remount.
562edf24abeSChristoph Hellwig  */
5632ff7e61eSJeff Mahoney int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
56496da0919SQu Wenruo 			unsigned long new_flags)
56595e05289SChris Mason {
56695e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
567e215772cSMisono, Tomohiro 	char *p, *num;
5684543df7eSChris Mason 	int intarg;
569a7a3f7caSSage Weil 	int ret = 0;
570261507a0SLi Zefan 	char *compress_type;
571261507a0SLi Zefan 	bool compress_force = false;
572b7c47bbbSTsutomu Itoh 	enum btrfs_compression_type saved_compress_type;
57327942c99SDavid Sterba 	int saved_compress_level;
574b7c47bbbSTsutomu Itoh 	bool saved_compress_force;
575b7c47bbbSTsutomu Itoh 	int no_compress = 0;
576b6cda9bcSChris Mason 
5770b246afaSJeff Mahoney 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
57870f6d82eSOmar Sandoval 		btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
57994846229SBoris Burkov 	else if (btrfs_free_space_cache_v1_active(info)) {
5805d1ab66cSNaohiro Aota 		if (btrfs_is_zoned(info)) {
5815d1ab66cSNaohiro Aota 			btrfs_info(info,
5825d1ab66cSNaohiro Aota 			"zoned: clearing existing space cache");
5835d1ab66cSNaohiro Aota 			btrfs_set_super_cache_generation(info->super_copy, 0);
5845d1ab66cSNaohiro Aota 		} else {
58573bc1876SJosef Bacik 			btrfs_set_opt(info->mount_opt, SPACE_CACHE);
5865d1ab66cSNaohiro Aota 		}
5875d1ab66cSNaohiro Aota 	}
58873bc1876SJosef Bacik 
58996da0919SQu Wenruo 	/*
59096da0919SQu Wenruo 	 * Even the options are empty, we still need to do extra check
59196da0919SQu Wenruo 	 * against new flags
59296da0919SQu Wenruo 	 */
59395e05289SChris Mason 	if (!options)
59496da0919SQu Wenruo 		goto check;
59595e05289SChris Mason 
59695e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
59795e05289SChris Mason 		int token;
59895e05289SChris Mason 		if (!*p)
59995e05289SChris Mason 			continue;
60095e05289SChris Mason 
60195e05289SChris Mason 		token = match_token(p, tokens, args);
60295e05289SChris Mason 		switch (token) {
603dfe25020SChris Mason 		case Opt_degraded:
6040b246afaSJeff Mahoney 			btrfs_info(info, "allowing degraded mounts");
605dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
606dfe25020SChris Mason 			break;
60795e05289SChris Mason 		case Opt_subvol:
60837becec9SOmar Sandoval 		case Opt_subvol_empty:
60973f73415SJosef Bacik 		case Opt_subvolid:
61043e570b0SChristoph Hellwig 		case Opt_device:
611edf24abeSChristoph Hellwig 			/*
612fa59f27cSAnand Jain 			 * These are parsed by btrfs_parse_subvol_options or
613fa59f27cSAnand Jain 			 * btrfs_parse_device_options and can be ignored here.
614edf24abeSChristoph Hellwig 			 */
61595e05289SChris Mason 			break;
616b6cda9bcSChris Mason 		case Opt_nodatasum:
6173cdde224SJeff Mahoney 			btrfs_set_and_info(info, NODATASUM,
61807802534SQu Wenruo 					   "setting nodatasum");
619be20aa9dSChris Mason 			break;
620d399167dSQu Wenruo 		case Opt_datasum:
6213cdde224SJeff Mahoney 			if (btrfs_test_opt(info, NODATASUM)) {
6223cdde224SJeff Mahoney 				if (btrfs_test_opt(info, NODATACOW))
6230b246afaSJeff Mahoney 					btrfs_info(info,
6245d163e0eSJeff Mahoney 						   "setting datasum, datacow enabled");
625d399167dSQu Wenruo 				else
6260b246afaSJeff Mahoney 					btrfs_info(info, "setting datasum");
62707802534SQu Wenruo 			}
628d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATACOW);
629d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATASUM);
630d399167dSQu Wenruo 			break;
631be20aa9dSChris Mason 		case Opt_nodatacow:
6323cdde224SJeff Mahoney 			if (!btrfs_test_opt(info, NODATACOW)) {
6333cdde224SJeff Mahoney 				if (!btrfs_test_opt(info, COMPRESS) ||
6343cdde224SJeff Mahoney 				    !btrfs_test_opt(info, FORCE_COMPRESS)) {
6350b246afaSJeff Mahoney 					btrfs_info(info,
636efe120a0SFrank Holton 						   "setting nodatacow, compression disabled");
637bedb2ccaSAndrei Popa 				} else {
6380b246afaSJeff Mahoney 					btrfs_info(info, "setting nodatacow");
639bedb2ccaSAndrei Popa 				}
64007802534SQu Wenruo 			}
641bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, COMPRESS);
642bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
643be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
644be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
645b6cda9bcSChris Mason 			break;
646a258af7aSQu Wenruo 		case Opt_datacow:
6473cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NODATACOW,
64807802534SQu Wenruo 					     "setting datacow");
649a258af7aSQu Wenruo 			break;
650a555f810SChris Mason 		case Opt_compress_force:
651261507a0SLi Zefan 		case Opt_compress_force_type:
652261507a0SLi Zefan 			compress_force = true;
653c730ae0cSMarcos Paulo de Souza 			fallthrough;
654261507a0SLi Zefan 		case Opt_compress:
655261507a0SLi Zefan 		case Opt_compress_type:
6563cdde224SJeff Mahoney 			saved_compress_type = btrfs_test_opt(info,
6573cdde224SJeff Mahoney 							     COMPRESS) ?
658b7c47bbbSTsutomu Itoh 				info->compress_type : BTRFS_COMPRESS_NONE;
659b7c47bbbSTsutomu Itoh 			saved_compress_force =
6603cdde224SJeff Mahoney 				btrfs_test_opt(info, FORCE_COMPRESS);
66127942c99SDavid Sterba 			saved_compress_level = info->compress_level;
662261507a0SLi Zefan 			if (token == Opt_compress ||
663261507a0SLi Zefan 			    token == Opt_compress_force ||
664a7164fa4SDavid Sterba 			    strncmp(args[0].from, "zlib", 4) == 0) {
665261507a0SLi Zefan 				compress_type = "zlib";
666eae8d825SQu Wenruo 
667261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
668eae8d825SQu Wenruo 				info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
669eae8d825SQu Wenruo 				/*
670eae8d825SQu Wenruo 				 * args[0] contains uninitialized data since
671eae8d825SQu Wenruo 				 * for these tokens we don't expect any
672eae8d825SQu Wenruo 				 * parameter.
673eae8d825SQu Wenruo 				 */
674eae8d825SQu Wenruo 				if (token != Opt_compress &&
675eae8d825SQu Wenruo 				    token != Opt_compress_force)
676f51d2b59SDavid Sterba 					info->compress_level =
677d0ab62ceSDennis Zhou 					  btrfs_compress_str2level(
678d0ab62ceSDennis Zhou 							BTRFS_COMPRESS_ZLIB,
679d0ab62ceSDennis Zhou 							args[0].from + 4);
680063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
681bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
682bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
683b7c47bbbSTsutomu Itoh 				no_compress = 0;
684a7164fa4SDavid Sterba 			} else if (strncmp(args[0].from, "lzo", 3) == 0) {
685a6fa6faeSLi Zefan 				compress_type = "lzo";
686a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
687282dd7d7SMarcos Paulo de Souza 				info->compress_level = 0;
688063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
689bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
690bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
6912b0ce2c2SMitch Harder 				btrfs_set_fs_incompat(info, COMPRESS_LZO);
692b7c47bbbSTsutomu Itoh 				no_compress = 0;
6933f93aef5SDennis Zhou 			} else if (strncmp(args[0].from, "zstd", 4) == 0) {
6945c1aab1dSNick Terrell 				compress_type = "zstd";
6955c1aab1dSNick Terrell 				info->compress_type = BTRFS_COMPRESS_ZSTD;
6963f93aef5SDennis Zhou 				info->compress_level =
6973f93aef5SDennis Zhou 					btrfs_compress_str2level(
6983f93aef5SDennis Zhou 							 BTRFS_COMPRESS_ZSTD,
6993f93aef5SDennis Zhou 							 args[0].from + 4);
7005c1aab1dSNick Terrell 				btrfs_set_opt(info->mount_opt, COMPRESS);
7015c1aab1dSNick Terrell 				btrfs_clear_opt(info->mount_opt, NODATACOW);
7025c1aab1dSNick Terrell 				btrfs_clear_opt(info->mount_opt, NODATASUM);
7035c1aab1dSNick Terrell 				btrfs_set_fs_incompat(info, COMPRESS_ZSTD);
7045c1aab1dSNick Terrell 				no_compress = 0;
705063849eaSArnd Hannemann 			} else if (strncmp(args[0].from, "no", 2) == 0) {
706063849eaSArnd Hannemann 				compress_type = "no";
70727942c99SDavid Sterba 				info->compress_level = 0;
70827942c99SDavid Sterba 				info->compress_type = 0;
709063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, COMPRESS);
710063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
711063849eaSArnd Hannemann 				compress_force = false;
712b7c47bbbSTsutomu Itoh 				no_compress++;
713261507a0SLi Zefan 			} else {
714261507a0SLi Zefan 				ret = -EINVAL;
715261507a0SLi Zefan 				goto out;
716261507a0SLi Zefan 			}
717261507a0SLi Zefan 
718261507a0SLi Zefan 			if (compress_force) {
719b7c47bbbSTsutomu Itoh 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
720143f3636SDavid Sterba 			} else {
7214027e0f4SWang Shilong 				/*
7224027e0f4SWang Shilong 				 * If we remount from compress-force=xxx to
7234027e0f4SWang Shilong 				 * compress=xxx, we need clear FORCE_COMPRESS
7244027e0f4SWang Shilong 				 * flag, otherwise, there is no way for users
7254027e0f4SWang Shilong 				 * to disable forcible compression separately.
7264027e0f4SWang Shilong 				 */
7274027e0f4SWang Shilong 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
728a7e252afSMiao Xie 			}
72927942c99SDavid Sterba 			if (no_compress == 1) {
73027942c99SDavid Sterba 				btrfs_info(info, "use no compression");
73127942c99SDavid Sterba 			} else if ((info->compress_type != saved_compress_type) ||
73227942c99SDavid Sterba 				   (compress_force != saved_compress_force) ||
73327942c99SDavid Sterba 				   (info->compress_level != saved_compress_level)) {
734f51d2b59SDavid Sterba 				btrfs_info(info, "%s %s compression, level %d",
735b7c47bbbSTsutomu Itoh 					   (compress_force) ? "force" : "use",
736f51d2b59SDavid Sterba 					   compress_type, info->compress_level);
737b7c47bbbSTsutomu Itoh 			}
738b7c47bbbSTsutomu Itoh 			compress_force = false;
739a555f810SChris Mason 			break;
740e18e4809SChris Mason 		case Opt_ssd:
7413cdde224SJeff Mahoney 			btrfs_set_and_info(info, SSD,
742583b7231SHans van Kranenburg 					   "enabling ssd optimizations");
743951e7966SAdam Borowski 			btrfs_clear_opt(info->mount_opt, NOSSD);
744e18e4809SChris Mason 			break;
745451d7585SChris Mason 		case Opt_ssd_spread:
746583b7231SHans van Kranenburg 			btrfs_set_and_info(info, SSD,
747583b7231SHans van Kranenburg 					   "enabling ssd optimizations");
7483cdde224SJeff Mahoney 			btrfs_set_and_info(info, SSD_SPREAD,
749583b7231SHans van Kranenburg 					   "using spread ssd allocation scheme");
750951e7966SAdam Borowski 			btrfs_clear_opt(info->mount_opt, NOSSD);
751451d7585SChris Mason 			break;
7523b30c22fSChris Mason 		case Opt_nossd:
753583b7231SHans van Kranenburg 			btrfs_set_opt(info->mount_opt, NOSSD);
754583b7231SHans van Kranenburg 			btrfs_clear_and_info(info, SSD,
755583b7231SHans van Kranenburg 					     "not using ssd optimizations");
756c730ae0cSMarcos Paulo de Souza 			fallthrough;
75762b8e077SHoward McLauchlan 		case Opt_nossd_spread:
758583b7231SHans van Kranenburg 			btrfs_clear_and_info(info, SSD_SPREAD,
759583b7231SHans van Kranenburg 					     "not using spread ssd allocation scheme");
7603b30c22fSChris Mason 			break;
761842bef58SQu Wenruo 		case Opt_barrier:
7623cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NOBARRIER,
76307802534SQu Wenruo 					     "turning on barriers");
764842bef58SQu Wenruo 			break;
76521ad10cfSChris Mason 		case Opt_nobarrier:
7663cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOBARRIER,
76707802534SQu Wenruo 					   "turning off barriers");
76821ad10cfSChris Mason 			break;
7694543df7eSChris Mason 		case Opt_thread_pool:
7702c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
7712c334e87SWang Shilong 			if (ret) {
7722c334e87SWang Shilong 				goto out;
773f7b885beSAnand Jain 			} else if (intarg == 0) {
7742c334e87SWang Shilong 				ret = -EINVAL;
7752c334e87SWang Shilong 				goto out;
7762c334e87SWang Shilong 			}
777f7b885beSAnand Jain 			info->thread_pool_size = intarg;
7784543df7eSChris Mason 			break;
7796f568d35SChris Mason 		case Opt_max_inline:
780edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
7816f568d35SChris Mason 			if (num) {
78291748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
7836f568d35SChris Mason 				kfree(num);
7846f568d35SChris Mason 
78515ada040SChris Mason 				if (info->max_inline) {
786feb5f965SMitch Harder 					info->max_inline = min_t(u64,
78715ada040SChris Mason 						info->max_inline,
7880b246afaSJeff Mahoney 						info->sectorsize);
78915ada040SChris Mason 				}
7900b246afaSJeff Mahoney 				btrfs_info(info, "max_inline at %llu",
791c1c9ff7cSGeert Uytterhoeven 					   info->max_inline);
7922c334e87SWang Shilong 			} else {
7932c334e87SWang Shilong 				ret = -ENOMEM;
7942c334e87SWang Shilong 				goto out;
7956f568d35SChris Mason 			}
7966f568d35SChris Mason 			break;
797bd0330adSQu Wenruo 		case Opt_acl:
79845ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL
7991751e8a6SLinus Torvalds 			info->sb->s_flags |= SB_POSIXACL;
800bd0330adSQu Wenruo 			break;
80145ff35d6SGuangliang Zhao #else
8020b246afaSJeff Mahoney 			btrfs_err(info, "support for ACL not compiled in!");
80345ff35d6SGuangliang Zhao 			ret = -EINVAL;
80445ff35d6SGuangliang Zhao 			goto out;
80545ff35d6SGuangliang Zhao #endif
80633268eafSJosef Bacik 		case Opt_noacl:
8071751e8a6SLinus Torvalds 			info->sb->s_flags &= ~SB_POSIXACL;
80833268eafSJosef Bacik 			break;
8093a5e1404SSage Weil 		case Opt_notreelog:
8103cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOTREELOG,
81107802534SQu Wenruo 					   "disabling tree log");
8123a5e1404SSage Weil 			break;
813a88998f2SQu Wenruo 		case Opt_treelog:
8143cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NOTREELOG,
81507802534SQu Wenruo 					     "enabling tree log");
816a88998f2SQu Wenruo 			break;
817fed8f166SQu Wenruo 		case Opt_norecovery:
81896da0919SQu Wenruo 		case Opt_nologreplay:
81974ef0018SQu Wenruo 			btrfs_warn(info,
82074ef0018SQu Wenruo 		"'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
8213cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOLOGREPLAY,
82296da0919SQu Wenruo 					   "disabling log replay at mount time");
82396da0919SQu Wenruo 			break;
824dccae999SSage Weil 		case Opt_flushoncommit:
8253cdde224SJeff Mahoney 			btrfs_set_and_info(info, FLUSHONCOMMIT,
82607802534SQu Wenruo 					   "turning on flush-on-commit");
827dccae999SSage Weil 			break;
8282c9ee856SQu Wenruo 		case Opt_noflushoncommit:
8293cdde224SJeff Mahoney 			btrfs_clear_and_info(info, FLUSHONCOMMIT,
83007802534SQu Wenruo 					     "turning off flush-on-commit");
8312c9ee856SQu Wenruo 			break;
83297e728d4SJosef Bacik 		case Opt_ratio:
8332c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
834764cb8b4SAnand Jain 			if (ret)
8352c334e87SWang Shilong 				goto out;
83697e728d4SJosef Bacik 			info->metadata_ratio = intarg;
837764cb8b4SAnand Jain 			btrfs_info(info, "metadata ratio %u",
83897e728d4SJosef Bacik 				   info->metadata_ratio);
83997e728d4SJosef Bacik 			break;
840e244a0aeSChristoph Hellwig 		case Opt_discard:
841b0643e59SDennis Zhou 		case Opt_discard_mode:
842b0643e59SDennis Zhou 			if (token == Opt_discard ||
843b0643e59SDennis Zhou 			    strcmp(args[0].from, "sync") == 0) {
844b0643e59SDennis Zhou 				btrfs_clear_opt(info->mount_opt, DISCARD_ASYNC);
84546b27f50SDennis Zhou 				btrfs_set_and_info(info, DISCARD_SYNC,
84646b27f50SDennis Zhou 						   "turning on sync discard");
847b0643e59SDennis Zhou 			} else if (strcmp(args[0].from, "async") == 0) {
848b0643e59SDennis Zhou 				btrfs_clear_opt(info->mount_opt, DISCARD_SYNC);
849b0643e59SDennis Zhou 				btrfs_set_and_info(info, DISCARD_ASYNC,
850b0643e59SDennis Zhou 						   "turning on async discard");
851b0643e59SDennis Zhou 			} else {
852b0643e59SDennis Zhou 				ret = -EINVAL;
853b0643e59SDennis Zhou 				goto out;
854b0643e59SDennis Zhou 			}
855e244a0aeSChristoph Hellwig 			break;
856e07a2adeSQu Wenruo 		case Opt_nodiscard:
85746b27f50SDennis Zhou 			btrfs_clear_and_info(info, DISCARD_SYNC,
85807802534SQu Wenruo 					     "turning off discard");
859b0643e59SDennis Zhou 			btrfs_clear_and_info(info, DISCARD_ASYNC,
860b0643e59SDennis Zhou 					     "turning off async discard");
861e07a2adeSQu Wenruo 			break;
8620af3d00bSJosef Bacik 		case Opt_space_cache:
86370f6d82eSOmar Sandoval 		case Opt_space_cache_version:
864*63cd070dSJosef Bacik 			/*
865*63cd070dSJosef Bacik 			 * We already set FREE_SPACE_TREE above because we have
866*63cd070dSJosef Bacik 			 * compat_ro(FREE_SPACE_TREE) set, and we aren't going
867*63cd070dSJosef Bacik 			 * to allow v1 to be set for extent tree v2, simply
868*63cd070dSJosef Bacik 			 * ignore this setting if we're extent tree v2.
869*63cd070dSJosef Bacik 			 */
870*63cd070dSJosef Bacik 			if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
871*63cd070dSJosef Bacik 				break;
87270f6d82eSOmar Sandoval 			if (token == Opt_space_cache ||
87370f6d82eSOmar Sandoval 			    strcmp(args[0].from, "v1") == 0) {
8740b246afaSJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
87570f6d82eSOmar Sandoval 						FREE_SPACE_TREE);
8763cdde224SJeff Mahoney 				btrfs_set_and_info(info, SPACE_CACHE,
87707802534SQu Wenruo 					   "enabling disk space caching");
87870f6d82eSOmar Sandoval 			} else if (strcmp(args[0].from, "v2") == 0) {
8790b246afaSJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
88070f6d82eSOmar Sandoval 						SPACE_CACHE);
8810b246afaSJeff Mahoney 				btrfs_set_and_info(info, FREE_SPACE_TREE,
88270f6d82eSOmar Sandoval 						   "enabling free space tree");
88370f6d82eSOmar Sandoval 			} else {
88470f6d82eSOmar Sandoval 				ret = -EINVAL;
88570f6d82eSOmar Sandoval 				goto out;
88670f6d82eSOmar Sandoval 			}
8870de90876SJosef Bacik 			break;
888f420ee1eSStefan Behrens 		case Opt_rescan_uuid_tree:
889f420ee1eSStefan Behrens 			btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
890f420ee1eSStefan Behrens 			break;
89173bc1876SJosef Bacik 		case Opt_no_space_cache:
892*63cd070dSJosef Bacik 			/*
893*63cd070dSJosef Bacik 			 * We cannot operate without the free space tree with
894*63cd070dSJosef Bacik 			 * extent tree v2, ignore this option.
895*63cd070dSJosef Bacik 			 */
896*63cd070dSJosef Bacik 			if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
897*63cd070dSJosef Bacik 				break;
8983cdde224SJeff Mahoney 			if (btrfs_test_opt(info, SPACE_CACHE)) {
8990b246afaSJeff Mahoney 				btrfs_clear_and_info(info, SPACE_CACHE,
90007802534SQu Wenruo 					     "disabling disk space caching");
90170f6d82eSOmar Sandoval 			}
9023cdde224SJeff Mahoney 			if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
9030b246afaSJeff Mahoney 				btrfs_clear_and_info(info, FREE_SPACE_TREE,
90470f6d82eSOmar Sandoval 					     "disabling free space tree");
90570f6d82eSOmar Sandoval 			}
90673bc1876SJosef Bacik 			break;
9074b9465cbSChris Mason 		case Opt_inode_cache:
9083818aea2SQu Wenruo 		case Opt_noinode_cache:
9095297199aSNikolay Borisov 			btrfs_warn(info,
9105297199aSNikolay Borisov 	"the 'inode_cache' option is deprecated and has no effect since 5.11");
9114b9465cbSChris Mason 			break;
91288c2ba3bSJosef Bacik 		case Opt_clear_cache:
913*63cd070dSJosef Bacik 			/*
914*63cd070dSJosef Bacik 			 * We cannot clear the free space tree with extent tree
915*63cd070dSJosef Bacik 			 * v2, ignore this option.
916*63cd070dSJosef Bacik 			 */
917*63cd070dSJosef Bacik 			if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
918*63cd070dSJosef Bacik 				break;
9193cdde224SJeff Mahoney 			btrfs_set_and_info(info, CLEAR_CACHE,
92007802534SQu Wenruo 					   "force clearing of disk cache");
9210af3d00bSJosef Bacik 			break;
9224260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
9234260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
9244260f7c7SSage Weil 			break;
92591435650SChris Mason 		case Opt_enospc_debug:
92691435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
92791435650SChris Mason 			break;
92853036293SQu Wenruo 		case Opt_noenospc_debug:
92953036293SQu Wenruo 			btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
93053036293SQu Wenruo 			break;
9314cb5300bSChris Mason 		case Opt_defrag:
9323cdde224SJeff Mahoney 			btrfs_set_and_info(info, AUTO_DEFRAG,
93307802534SQu Wenruo 					   "enabling auto defrag");
9344cb5300bSChris Mason 			break;
935fc0ca9afSQu Wenruo 		case Opt_nodefrag:
9363cdde224SJeff Mahoney 			btrfs_clear_and_info(info, AUTO_DEFRAG,
93707802534SQu Wenruo 					     "disabling auto defrag");
938fc0ca9afSQu Wenruo 			break;
939af31f5e5SChris Mason 		case Opt_recovery:
9408dcddfa0SQu Wenruo 		case Opt_usebackuproot:
94174ef0018SQu Wenruo 			btrfs_warn(info,
94274ef0018SQu Wenruo 			"'%s' is deprecated, use 'rescue=usebackuproot' instead",
94374ef0018SQu Wenruo 				   token == Opt_recovery ? "recovery" :
94474ef0018SQu Wenruo 				   "usebackuproot");
9450b246afaSJeff Mahoney 			btrfs_info(info,
9468dcddfa0SQu Wenruo 				   "trying to use backup root at mount time");
9478dcddfa0SQu Wenruo 			btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
948af31f5e5SChris Mason 			break;
9499555c6c1SIlya Dryomov 		case Opt_skip_balance:
9509555c6c1SIlya Dryomov 			btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
9519555c6c1SIlya Dryomov 			break;
95221adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
95321adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
9540b246afaSJeff Mahoney 			btrfs_info(info,
955efe120a0SFrank Holton 				   "enabling check integrity including extent data");
956cbeaae4fSDavid Sterba 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY_DATA);
95721adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
95821adbd5cSStefan Behrens 			break;
95921adbd5cSStefan Behrens 		case Opt_check_integrity:
9600b246afaSJeff Mahoney 			btrfs_info(info, "enabling check integrity");
96121adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
96221adbd5cSStefan Behrens 			break;
96321adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
9642c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
96502453bdeSAnand Jain 			if (ret)
9662c334e87SWang Shilong 				goto out;
96721adbd5cSStefan Behrens 			info->check_integrity_print_mask = intarg;
96802453bdeSAnand Jain 			btrfs_info(info, "check_integrity_print_mask 0x%x",
96921adbd5cSStefan Behrens 				   info->check_integrity_print_mask);
97021adbd5cSStefan Behrens 			break;
97121adbd5cSStefan Behrens #else
97221adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
97321adbd5cSStefan Behrens 		case Opt_check_integrity:
97421adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
9750b246afaSJeff Mahoney 			btrfs_err(info,
976efe120a0SFrank Holton 				  "support for check_integrity* not compiled in!");
97721adbd5cSStefan Behrens 			ret = -EINVAL;
97821adbd5cSStefan Behrens 			goto out;
97921adbd5cSStefan Behrens #endif
9808c342930SJeff Mahoney 		case Opt_fatal_errors:
9818c342930SJeff Mahoney 			if (strcmp(args[0].from, "panic") == 0)
9828c342930SJeff Mahoney 				btrfs_set_opt(info->mount_opt,
9838c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
9848c342930SJeff Mahoney 			else if (strcmp(args[0].from, "bug") == 0)
9858c342930SJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
9868c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
9878c342930SJeff Mahoney 			else {
9888c342930SJeff Mahoney 				ret = -EINVAL;
9898c342930SJeff Mahoney 				goto out;
9908c342930SJeff Mahoney 			}
9918c342930SJeff Mahoney 			break;
9928b87dc17SDavid Sterba 		case Opt_commit_interval:
9938b87dc17SDavid Sterba 			intarg = 0;
9948b87dc17SDavid Sterba 			ret = match_int(&args[0], &intarg);
995d3740608SAnand Jain 			if (ret)
9968b87dc17SDavid Sterba 				goto out;
997d3740608SAnand Jain 			if (intarg == 0) {
998d3740608SAnand Jain 				btrfs_info(info,
999d3740608SAnand Jain 					   "using default commit interval %us",
1000d3740608SAnand Jain 					   BTRFS_DEFAULT_COMMIT_INTERVAL);
1001d3740608SAnand Jain 				intarg = BTRFS_DEFAULT_COMMIT_INTERVAL;
1002d3740608SAnand Jain 			} else if (intarg > 300) {
1003d3740608SAnand Jain 				btrfs_warn(info, "excessive commit interval %d",
10048b87dc17SDavid Sterba 					   intarg);
10058b87dc17SDavid Sterba 			}
10068b87dc17SDavid Sterba 			info->commit_interval = intarg;
10078b87dc17SDavid Sterba 			break;
100874ef0018SQu Wenruo 		case Opt_rescue:
100974ef0018SQu Wenruo 			ret = parse_rescue_options(info, args[0].from);
101074ef0018SQu Wenruo 			if (ret < 0)
101174ef0018SQu Wenruo 				goto out;
101274ef0018SQu Wenruo 			break;
1013d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
1014d0bd4560SJosef Bacik 		case Opt_fragment_all:
10150b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting all space");
1016d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
1017d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
1018d0bd4560SJosef Bacik 			break;
1019d0bd4560SJosef Bacik 		case Opt_fragment_metadata:
10200b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting metadata");
1021d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt,
1022d0bd4560SJosef Bacik 				      FRAGMENT_METADATA);
1023d0bd4560SJosef Bacik 			break;
1024d0bd4560SJosef Bacik 		case Opt_fragment_data:
10250b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting data");
1026d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
1027d0bd4560SJosef Bacik 			break;
1028d0bd4560SJosef Bacik #endif
1029fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
1030fb592373SJosef Bacik 		case Opt_ref_verify:
1031fb592373SJosef Bacik 			btrfs_info(info, "doing ref verification");
1032fb592373SJosef Bacik 			btrfs_set_opt(info->mount_opt, REF_VERIFY);
1033fb592373SJosef Bacik 			break;
1034fb592373SJosef Bacik #endif
1035a7a3f7caSSage Weil 		case Opt_err:
10367e8f19e5SDavid Sterba 			btrfs_err(info, "unrecognized mount option '%s'", p);
1037a7a3f7caSSage Weil 			ret = -EINVAL;
1038a7a3f7caSSage Weil 			goto out;
103995e05289SChris Mason 		default:
1040be20aa9dSChris Mason 			break;
104195e05289SChris Mason 		}
104295e05289SChris Mason 	}
104396da0919SQu Wenruo check:
1044d70bf748SJosef Bacik 	/* We're read-only, don't have to check. */
1045d70bf748SJosef Bacik 	if (new_flags & SB_RDONLY)
1046d70bf748SJosef Bacik 		goto out;
1047d70bf748SJosef Bacik 
104842437a63SJosef Bacik 	if (check_ro_option(info, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||
1049882dbe0cSJosef Bacik 	    check_ro_option(info, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
1050882dbe0cSJosef Bacik 	    check_ro_option(info, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums"))
105196da0919SQu Wenruo 		ret = -EINVAL;
1052a7a3f7caSSage Weil out:
10530b246afaSJeff Mahoney 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
10543cdde224SJeff Mahoney 	    !btrfs_test_opt(info, FREE_SPACE_TREE) &&
10553cdde224SJeff Mahoney 	    !btrfs_test_opt(info, CLEAR_CACHE)) {
10560b246afaSJeff Mahoney 		btrfs_err(info, "cannot disable free space tree");
105770f6d82eSOmar Sandoval 		ret = -EINVAL;
105870f6d82eSOmar Sandoval 
105970f6d82eSOmar Sandoval 	}
10605d1ab66cSNaohiro Aota 	if (!ret)
10615d1ab66cSNaohiro Aota 		ret = btrfs_check_mountopts_zoned(info);
10623cdde224SJeff Mahoney 	if (!ret && btrfs_test_opt(info, SPACE_CACHE))
10630b246afaSJeff Mahoney 		btrfs_info(info, "disk space caching is enabled");
10643cdde224SJeff Mahoney 	if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE))
10650b246afaSJeff Mahoney 		btrfs_info(info, "using free space tree");
1066a7a3f7caSSage Weil 	return ret;
1067edf24abeSChristoph Hellwig }
1068edf24abeSChristoph Hellwig 
1069edf24abeSChristoph Hellwig /*
1070edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
1071edf24abeSChristoph Hellwig  *
1072edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
1073edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
1074edf24abeSChristoph Hellwig  */
1075fa59f27cSAnand Jain static int btrfs_parse_device_options(const char *options, fmode_t flags,
1076d64dcbd1SGu Jinxiang 				      void *holder)
1077edf24abeSChristoph Hellwig {
1078edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
107983c8c9bdSJeff Liu 	char *device_name, *opts, *orig, *p;
108036350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
1081d7407606SMisono, Tomohiro 	int error = 0;
1082d7407606SMisono, Tomohiro 
10835139cff5SDavid Sterba 	lockdep_assert_held(&uuid_mutex);
10845139cff5SDavid Sterba 
1085d7407606SMisono, Tomohiro 	if (!options)
1086d7407606SMisono, Tomohiro 		return 0;
1087d7407606SMisono, Tomohiro 
1088d7407606SMisono, Tomohiro 	/*
1089d7407606SMisono, Tomohiro 	 * strsep changes the string, duplicate it because btrfs_parse_options
1090d7407606SMisono, Tomohiro 	 * gets called later
1091d7407606SMisono, Tomohiro 	 */
1092d7407606SMisono, Tomohiro 	opts = kstrdup(options, GFP_KERNEL);
1093d7407606SMisono, Tomohiro 	if (!opts)
1094d7407606SMisono, Tomohiro 		return -ENOMEM;
1095d7407606SMisono, Tomohiro 	orig = opts;
1096d7407606SMisono, Tomohiro 
1097d7407606SMisono, Tomohiro 	while ((p = strsep(&opts, ",")) != NULL) {
1098d7407606SMisono, Tomohiro 		int token;
1099d7407606SMisono, Tomohiro 
1100d7407606SMisono, Tomohiro 		if (!*p)
1101d7407606SMisono, Tomohiro 			continue;
1102d7407606SMisono, Tomohiro 
1103d7407606SMisono, Tomohiro 		token = match_token(p, tokens, args);
1104d7407606SMisono, Tomohiro 		if (token == Opt_device) {
1105d7407606SMisono, Tomohiro 			device_name = match_strdup(&args[0]);
1106d7407606SMisono, Tomohiro 			if (!device_name) {
1107d7407606SMisono, Tomohiro 				error = -ENOMEM;
1108d7407606SMisono, Tomohiro 				goto out;
1109d7407606SMisono, Tomohiro 			}
111036350e95SGu Jinxiang 			device = btrfs_scan_one_device(device_name, flags,
111136350e95SGu Jinxiang 					holder);
1112d7407606SMisono, Tomohiro 			kfree(device_name);
111336350e95SGu Jinxiang 			if (IS_ERR(device)) {
111436350e95SGu Jinxiang 				error = PTR_ERR(device);
1115d7407606SMisono, Tomohiro 				goto out;
1116d7407606SMisono, Tomohiro 			}
1117d7407606SMisono, Tomohiro 		}
111836350e95SGu Jinxiang 	}
1119d7407606SMisono, Tomohiro 
1120d7407606SMisono, Tomohiro out:
1121d7407606SMisono, Tomohiro 	kfree(orig);
1122d7407606SMisono, Tomohiro 	return error;
1123d7407606SMisono, Tomohiro }
1124d7407606SMisono, Tomohiro 
1125d7407606SMisono, Tomohiro /*
1126d7407606SMisono, Tomohiro  * Parse mount options that are related to subvolume id
1127d7407606SMisono, Tomohiro  *
1128d7407606SMisono, Tomohiro  * The value is later passed to mount_subvol()
1129d7407606SMisono, Tomohiro  */
113093b9bcdfSGu Jinxiang static int btrfs_parse_subvol_options(const char *options, char **subvol_name,
113193b9bcdfSGu Jinxiang 		u64 *subvol_objectid)
1132d7407606SMisono, Tomohiro {
1133d7407606SMisono, Tomohiro 	substring_t args[MAX_OPT_ARGS];
1134d7407606SMisono, Tomohiro 	char *opts, *orig, *p;
1135edf24abeSChristoph Hellwig 	int error = 0;
1136ccb0e7d1SAnand Jain 	u64 subvolid;
1137edf24abeSChristoph Hellwig 
1138edf24abeSChristoph Hellwig 	if (!options)
1139830c4adbSJosef Bacik 		return 0;
1140edf24abeSChristoph Hellwig 
1141edf24abeSChristoph Hellwig 	/*
1142d7407606SMisono, Tomohiro 	 * strsep changes the string, duplicate it because
1143fa59f27cSAnand Jain 	 * btrfs_parse_device_options gets called later
1144edf24abeSChristoph Hellwig 	 */
1145edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
1146edf24abeSChristoph Hellwig 	if (!opts)
1147edf24abeSChristoph Hellwig 		return -ENOMEM;
11483f3d0bc0STero Roponen 	orig = opts;
1149edf24abeSChristoph Hellwig 
1150edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
1151edf24abeSChristoph Hellwig 		int token;
1152edf24abeSChristoph Hellwig 		if (!*p)
1153edf24abeSChristoph Hellwig 			continue;
1154edf24abeSChristoph Hellwig 
1155edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
1156edf24abeSChristoph Hellwig 		switch (token) {
1157edf24abeSChristoph Hellwig 		case Opt_subvol:
1158a90e8b6fSIlya Dryomov 			kfree(*subvol_name);
1159edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
11602c334e87SWang Shilong 			if (!*subvol_name) {
11612c334e87SWang Shilong 				error = -ENOMEM;
11622c334e87SWang Shilong 				goto out;
11632c334e87SWang Shilong 			}
1164edf24abeSChristoph Hellwig 			break;
116573f73415SJosef Bacik 		case Opt_subvolid:
1166ccb0e7d1SAnand Jain 			error = match_u64(&args[0], &subvolid);
1167ccb0e7d1SAnand Jain 			if (error)
11682c334e87SWang Shilong 				goto out;
1169ccb0e7d1SAnand Jain 
1170ccb0e7d1SAnand Jain 			/* we want the original fs_tree */
1171ccb0e7d1SAnand Jain 			if (subvolid == 0)
1172ccb0e7d1SAnand Jain 				subvolid = BTRFS_FS_TREE_OBJECTID;
1173ccb0e7d1SAnand Jain 
1174ccb0e7d1SAnand Jain 			*subvol_objectid = subvolid;
117573f73415SJosef Bacik 			break;
1176edf24abeSChristoph Hellwig 		default:
1177edf24abeSChristoph Hellwig 			break;
1178edf24abeSChristoph Hellwig 		}
1179edf24abeSChristoph Hellwig 	}
1180edf24abeSChristoph Hellwig 
1181edf24abeSChristoph Hellwig out:
1182830c4adbSJosef Bacik 	kfree(orig);
1183edf24abeSChristoph Hellwig 	return error;
118495e05289SChris Mason }
118595e05289SChris Mason 
1186c0c907a4SMarcos Paulo de Souza char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
118773f73415SJosef Bacik 					  u64 subvol_objectid)
118873f73415SJosef Bacik {
1189815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
11905168489aSJosef Bacik 	struct btrfs_root *fs_root = NULL;
119105dbe683SOmar Sandoval 	struct btrfs_root_ref *root_ref;
119205dbe683SOmar Sandoval 	struct btrfs_inode_ref *inode_ref;
119305dbe683SOmar Sandoval 	struct btrfs_key key;
119405dbe683SOmar Sandoval 	struct btrfs_path *path = NULL;
119505dbe683SOmar Sandoval 	char *name = NULL, *ptr;
119605dbe683SOmar Sandoval 	u64 dirid;
119705dbe683SOmar Sandoval 	int len;
119805dbe683SOmar Sandoval 	int ret;
119905dbe683SOmar Sandoval 
120005dbe683SOmar Sandoval 	path = btrfs_alloc_path();
120105dbe683SOmar Sandoval 	if (!path) {
120205dbe683SOmar Sandoval 		ret = -ENOMEM;
120305dbe683SOmar Sandoval 		goto err;
120405dbe683SOmar Sandoval 	}
120505dbe683SOmar Sandoval 
12063ec83621SDavid Sterba 	name = kmalloc(PATH_MAX, GFP_KERNEL);
120705dbe683SOmar Sandoval 	if (!name) {
120805dbe683SOmar Sandoval 		ret = -ENOMEM;
120905dbe683SOmar Sandoval 		goto err;
121005dbe683SOmar Sandoval 	}
121105dbe683SOmar Sandoval 	ptr = name + PATH_MAX - 1;
121205dbe683SOmar Sandoval 	ptr[0] = '\0';
121305dbe683SOmar Sandoval 
121405dbe683SOmar Sandoval 	/*
121505dbe683SOmar Sandoval 	 * Walk up the subvolume trees in the tree of tree roots by root
121605dbe683SOmar Sandoval 	 * backrefs until we hit the top-level subvolume.
121705dbe683SOmar Sandoval 	 */
121805dbe683SOmar Sandoval 	while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
121905dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
122005dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_BACKREF_KEY;
122105dbe683SOmar Sandoval 		key.offset = (u64)-1;
122205dbe683SOmar Sandoval 
12230ff40a91SMarcos Paulo de Souza 		ret = btrfs_search_backwards(root, &key, path);
122405dbe683SOmar Sandoval 		if (ret < 0) {
122505dbe683SOmar Sandoval 			goto err;
122605dbe683SOmar Sandoval 		} else if (ret > 0) {
122705dbe683SOmar Sandoval 			ret = -ENOENT;
122805dbe683SOmar Sandoval 			goto err;
122905dbe683SOmar Sandoval 		}
123005dbe683SOmar Sandoval 
123105dbe683SOmar Sandoval 		subvol_objectid = key.offset;
123205dbe683SOmar Sandoval 
123305dbe683SOmar Sandoval 		root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
123405dbe683SOmar Sandoval 					  struct btrfs_root_ref);
123505dbe683SOmar Sandoval 		len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
123605dbe683SOmar Sandoval 		ptr -= len + 1;
123705dbe683SOmar Sandoval 		if (ptr < name) {
123805dbe683SOmar Sandoval 			ret = -ENAMETOOLONG;
123905dbe683SOmar Sandoval 			goto err;
124005dbe683SOmar Sandoval 		}
124105dbe683SOmar Sandoval 		read_extent_buffer(path->nodes[0], ptr + 1,
124205dbe683SOmar Sandoval 				   (unsigned long)(root_ref + 1), len);
124305dbe683SOmar Sandoval 		ptr[0] = '/';
124405dbe683SOmar Sandoval 		dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
124505dbe683SOmar Sandoval 		btrfs_release_path(path);
124605dbe683SOmar Sandoval 
124756e9357aSDavid Sterba 		fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true);
124805dbe683SOmar Sandoval 		if (IS_ERR(fs_root)) {
124905dbe683SOmar Sandoval 			ret = PTR_ERR(fs_root);
12505168489aSJosef Bacik 			fs_root = NULL;
12515168489aSJosef Bacik 			goto err;
12525168489aSJosef Bacik 		}
125305dbe683SOmar Sandoval 
125405dbe683SOmar Sandoval 		/*
125505dbe683SOmar Sandoval 		 * Walk up the filesystem tree by inode refs until we hit the
125605dbe683SOmar Sandoval 		 * root directory.
125705dbe683SOmar Sandoval 		 */
125805dbe683SOmar Sandoval 		while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
125905dbe683SOmar Sandoval 			key.objectid = dirid;
126005dbe683SOmar Sandoval 			key.type = BTRFS_INODE_REF_KEY;
126105dbe683SOmar Sandoval 			key.offset = (u64)-1;
126205dbe683SOmar Sandoval 
12630ff40a91SMarcos Paulo de Souza 			ret = btrfs_search_backwards(fs_root, &key, path);
126405dbe683SOmar Sandoval 			if (ret < 0) {
126505dbe683SOmar Sandoval 				goto err;
126605dbe683SOmar Sandoval 			} else if (ret > 0) {
126705dbe683SOmar Sandoval 				ret = -ENOENT;
126805dbe683SOmar Sandoval 				goto err;
126905dbe683SOmar Sandoval 			}
127005dbe683SOmar Sandoval 
127105dbe683SOmar Sandoval 			dirid = key.offset;
127205dbe683SOmar Sandoval 
127305dbe683SOmar Sandoval 			inode_ref = btrfs_item_ptr(path->nodes[0],
127405dbe683SOmar Sandoval 						   path->slots[0],
127505dbe683SOmar Sandoval 						   struct btrfs_inode_ref);
127605dbe683SOmar Sandoval 			len = btrfs_inode_ref_name_len(path->nodes[0],
127705dbe683SOmar Sandoval 						       inode_ref);
127805dbe683SOmar Sandoval 			ptr -= len + 1;
127905dbe683SOmar Sandoval 			if (ptr < name) {
128005dbe683SOmar Sandoval 				ret = -ENAMETOOLONG;
128105dbe683SOmar Sandoval 				goto err;
128205dbe683SOmar Sandoval 			}
128305dbe683SOmar Sandoval 			read_extent_buffer(path->nodes[0], ptr + 1,
128405dbe683SOmar Sandoval 					   (unsigned long)(inode_ref + 1), len);
128505dbe683SOmar Sandoval 			ptr[0] = '/';
128605dbe683SOmar Sandoval 			btrfs_release_path(path);
128705dbe683SOmar Sandoval 		}
128800246528SJosef Bacik 		btrfs_put_root(fs_root);
12895168489aSJosef Bacik 		fs_root = NULL;
129005dbe683SOmar Sandoval 	}
129105dbe683SOmar Sandoval 
129205dbe683SOmar Sandoval 	btrfs_free_path(path);
129305dbe683SOmar Sandoval 	if (ptr == name + PATH_MAX - 1) {
129405dbe683SOmar Sandoval 		name[0] = '/';
129505dbe683SOmar Sandoval 		name[1] = '\0';
129605dbe683SOmar Sandoval 	} else {
129705dbe683SOmar Sandoval 		memmove(name, ptr, name + PATH_MAX - ptr);
129805dbe683SOmar Sandoval 	}
129905dbe683SOmar Sandoval 	return name;
130005dbe683SOmar Sandoval 
130105dbe683SOmar Sandoval err:
130200246528SJosef Bacik 	btrfs_put_root(fs_root);
130305dbe683SOmar Sandoval 	btrfs_free_path(path);
130405dbe683SOmar Sandoval 	kfree(name);
130505dbe683SOmar Sandoval 	return ERR_PTR(ret);
130605dbe683SOmar Sandoval }
130705dbe683SOmar Sandoval 
130805dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
130905dbe683SOmar Sandoval {
131005dbe683SOmar Sandoval 	struct btrfs_root *root = fs_info->tree_root;
131173f73415SJosef Bacik 	struct btrfs_dir_item *di;
131273f73415SJosef Bacik 	struct btrfs_path *path;
131373f73415SJosef Bacik 	struct btrfs_key location;
131473f73415SJosef Bacik 	u64 dir_id;
131573f73415SJosef Bacik 
131673f73415SJosef Bacik 	path = btrfs_alloc_path();
131773f73415SJosef Bacik 	if (!path)
131805dbe683SOmar Sandoval 		return -ENOMEM;
131973f73415SJosef Bacik 
132073f73415SJosef Bacik 	/*
132173f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
132273f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
132373f73415SJosef Bacik 	 * to mount.
132473f73415SJosef Bacik 	 */
1325815745cfSAl Viro 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
132673f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
1327b0839166SJulia Lawall 	if (IS_ERR(di)) {
1328b0839166SJulia Lawall 		btrfs_free_path(path);
132905dbe683SOmar Sandoval 		return PTR_ERR(di);
1330b0839166SJulia Lawall 	}
133173f73415SJosef Bacik 	if (!di) {
133273f73415SJosef Bacik 		/*
133373f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
133473f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
133505dbe683SOmar Sandoval 		 * mount the top-level subvolume.
133673f73415SJosef Bacik 		 */
133773f73415SJosef Bacik 		btrfs_free_path(path);
133805dbe683SOmar Sandoval 		*objectid = BTRFS_FS_TREE_OBJECTID;
133905dbe683SOmar Sandoval 		return 0;
134073f73415SJosef Bacik 	}
134173f73415SJosef Bacik 
134273f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
134373f73415SJosef Bacik 	btrfs_free_path(path);
134405dbe683SOmar Sandoval 	*objectid = location.objectid;
134505dbe683SOmar Sandoval 	return 0;
134673f73415SJosef Bacik }
134773f73415SJosef Bacik 
13488a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
13498a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
135056e033a7SDavid Sterba 			    void *data)
13512e635a27SChris Mason {
13522e635a27SChris Mason 	struct inode *inode;
1353815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
135439279cc3SChris Mason 	int err;
13552e635a27SChris Mason 
13562e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
13572e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
1358e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
1359af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
1360be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
136114605409SBoris Burkov #ifdef CONFIG_FS_VERITY
136214605409SBoris Burkov 	sb->s_vop = &btrfs_verityops;
136314605409SBoris Burkov #endif
13645103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
13652e635a27SChris Mason 	sb->s_time_gran = 1;
13660eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
13671751e8a6SLinus Torvalds 	sb->s_flags |= SB_POSIXACL;
136849cf6f45SChris Ball #endif
1369357fdad0SMatthew Garrett 	sb->s_flags |= SB_I_VERSION;
1370da2f0f74SChris Mason 	sb->s_iflags |= SB_I_CGROUPWB;
13719e11ceeeSJan Kara 
13729e11ceeeSJan Kara 	err = super_setup_bdi(sb);
13739e11ceeeSJan Kara 	if (err) {
13749e11ceeeSJan Kara 		btrfs_err(fs_info, "super_setup_bdi failed");
13759e11ceeeSJan Kara 		return err;
13769e11ceeeSJan Kara 	}
13779e11ceeeSJan Kara 
1378ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
1379ad2b2c80SAl Viro 	if (err) {
1380ab8d0fc4SJeff Mahoney 		btrfs_err(fs_info, "open_ctree failed");
1381ad2b2c80SAl Viro 		return err;
1382e20d96d6SChris Mason 	}
1383b888db2bSChris Mason 
13840202e83fSDavid Sterba 	inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
13855d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
13865d4f98a2SYan Zheng 		err = PTR_ERR(inode);
138739279cc3SChris Mason 		goto fail_close;
138839279cc3SChris Mason 	}
13892e635a27SChris Mason 
139048fde701SAl Viro 	sb->s_root = d_make_root(inode);
139148fde701SAl Viro 	if (!sb->s_root) {
139239279cc3SChris Mason 		err = -ENOMEM;
139339279cc3SChris Mason 		goto fail_close;
13942e635a27SChris Mason 	}
139558176a96SJosef Bacik 
13961751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
13972e635a27SChris Mason 	return 0;
13982e635a27SChris Mason 
139939279cc3SChris Mason fail_close:
14006bccf3abSJeff Mahoney 	close_ctree(fs_info);
1401d5719762SChris Mason 	return err;
1402d5719762SChris Mason }
1403d5719762SChris Mason 
14046bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
1405d5719762SChris Mason {
1406d5719762SChris Mason 	struct btrfs_trans_handle *trans;
1407815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1408815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
1409df2ce34cSChris Mason 
1410bc074524SJeff Mahoney 	trace_btrfs_sync_fs(fs_info, wait);
14111abe9b8aSliubo 
1412d561c025SChris Mason 	if (!wait) {
1413815745cfSAl Viro 		filemap_flush(fs_info->btree_inode->i_mapping);
1414df2ce34cSChris Mason 		return 0;
1415d561c025SChris Mason 	}
1416771ed689SChris Mason 
14176374e57aSChris Mason 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1418771ed689SChris Mason 
1419d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
142060376ce4SJosef Bacik 	if (IS_ERR(trans)) {
1421354aa0fbSMiao Xie 		/* no transaction, don't bother */
14226b5fe46dSDavid Sterba 		if (PTR_ERR(trans) == -ENOENT) {
14236b5fe46dSDavid Sterba 			/*
14246b5fe46dSDavid Sterba 			 * Exit unless we have some pending changes
14256b5fe46dSDavid Sterba 			 * that need to go through commit
14266b5fe46dSDavid Sterba 			 */
14276b5fe46dSDavid Sterba 			if (fs_info->pending_changes == 0)
1428bd7de2c9SJosef Bacik 				return 0;
1429a53f4f8eSQu Wenruo 			/*
1430a53f4f8eSQu Wenruo 			 * A non-blocking test if the fs is frozen. We must not
1431a53f4f8eSQu Wenruo 			 * start a new transaction here otherwise a deadlock
1432a53f4f8eSQu Wenruo 			 * happens. The pending operations are delayed to the
1433a53f4f8eSQu Wenruo 			 * next commit after thawing.
1434a53f4f8eSQu Wenruo 			 */
1435a7e3c5f2SRakesh Pandit 			if (sb_start_write_trylock(sb))
1436a7e3c5f2SRakesh Pandit 				sb_end_write(sb);
1437a53f4f8eSQu Wenruo 			else
1438a53f4f8eSQu Wenruo 				return 0;
14396b5fe46dSDavid Sterba 			trans = btrfs_start_transaction(root, 0);
144060376ce4SJosef Bacik 		}
144198bd5c54SDavid Sterba 		if (IS_ERR(trans))
144298bd5c54SDavid Sterba 			return PTR_ERR(trans);
14436b5fe46dSDavid Sterba 	}
14443a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
1445d5719762SChris Mason }
1446d5719762SChris Mason 
1447ab0b4a3eSJosef Bacik static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed)
1448ab0b4a3eSJosef Bacik {
1449ab0b4a3eSJosef Bacik 	seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s);
1450ab0b4a3eSJosef Bacik 	*printed = true;
1451ab0b4a3eSJosef Bacik }
1452ab0b4a3eSJosef Bacik 
145334c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1454a9572a15SEric Paris {
1455815745cfSAl Viro 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
14560f628c63SDavid Sterba 	const char *compress_type;
14573ef3959bSJosef Bacik 	const char *subvol_name;
1458ab0b4a3eSJosef Bacik 	bool printed = false;
1459a9572a15SEric Paris 
14603cdde224SJeff Mahoney 	if (btrfs_test_opt(info, DEGRADED))
1461a9572a15SEric Paris 		seq_puts(seq, ",degraded");
14623cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATASUM))
1463a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
14643cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATACOW))
1465a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
14663cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOBARRIER))
1467a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
146895ac567aSFilipe David Borba Manana 	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1469c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",max_inline=%llu", info->max_inline);
1470a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
1471a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
1472f7b885beSAnand Jain 		seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
14733cdde224SJeff Mahoney 	if (btrfs_test_opt(info, COMPRESS)) {
14740f628c63SDavid Sterba 		compress_type = btrfs_compress_type2str(info->compress_type);
14753cdde224SJeff Mahoney 		if (btrfs_test_opt(info, FORCE_COMPRESS))
1476200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
1477200da64eSTsutomu Itoh 		else
1478200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
1479f51d2b59SDavid Sterba 		if (info->compress_level)
1480fa4d885aSAdam Borowski 			seq_printf(seq, ":%d", info->compress_level);
1481200da64eSTsutomu Itoh 	}
14823cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOSSD))
1483c289811cSChris Mason 		seq_puts(seq, ",nossd");
14843cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SSD_SPREAD))
1485451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
14863cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, SSD))
1487a9572a15SEric Paris 		seq_puts(seq, ",ssd");
14883cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOTREELOG))
14896b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
14903cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOLOGREPLAY))
1491ab0b4a3eSJosef Bacik 		print_rescue_option(seq, "nologreplay", &printed);
149268319c18SJosef Bacik 	if (btrfs_test_opt(info, USEBACKUPROOT))
149368319c18SJosef Bacik 		print_rescue_option(seq, "usebackuproot", &printed);
149442437a63SJosef Bacik 	if (btrfs_test_opt(info, IGNOREBADROOTS))
149542437a63SJosef Bacik 		print_rescue_option(seq, "ignorebadroots", &printed);
1496882dbe0cSJosef Bacik 	if (btrfs_test_opt(info, IGNOREDATACSUMS))
1497882dbe0cSJosef Bacik 		print_rescue_option(seq, "ignoredatacsums", &printed);
14983cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FLUSHONCOMMIT))
14996b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
150046b27f50SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_SYNC))
150120a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
1502b0643e59SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_ASYNC))
1503b0643e59SDennis Zhou 		seq_puts(seq, ",discard=async");
15041751e8a6SLinus Torvalds 	if (!(info->sb->s_flags & SB_POSIXACL))
1505a9572a15SEric Paris 		seq_puts(seq, ",noacl");
150604c41559SBoris Burkov 	if (btrfs_free_space_cache_v1_active(info))
1507200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
150804c41559SBoris Burkov 	else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
150970f6d82eSOmar Sandoval 		seq_puts(seq, ",space_cache=v2");
151073bc1876SJosef Bacik 	else
15118965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
15123cdde224SJeff Mahoney 	if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1513f420ee1eSStefan Behrens 		seq_puts(seq, ",rescan_uuid_tree");
15143cdde224SJeff Mahoney 	if (btrfs_test_opt(info, CLEAR_CACHE))
1515200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
15163cdde224SJeff Mahoney 	if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1517200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
15183cdde224SJeff Mahoney 	if (btrfs_test_opt(info, ENOSPC_DEBUG))
15190942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
15203cdde224SJeff Mahoney 	if (btrfs_test_opt(info, AUTO_DEFRAG))
15210942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
15223cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SKIP_BALANCE))
15239555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
15248507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1525cbeaae4fSDavid Sterba 	if (btrfs_test_opt(info, CHECK_INTEGRITY_DATA))
15268507d216SWang Shilong 		seq_puts(seq, ",check_int_data");
15273cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, CHECK_INTEGRITY))
15288507d216SWang Shilong 		seq_puts(seq, ",check_int");
15298507d216SWang Shilong 	if (info->check_integrity_print_mask)
15308507d216SWang Shilong 		seq_printf(seq, ",check_int_print_mask=%d",
15318507d216SWang Shilong 				info->check_integrity_print_mask);
15328507d216SWang Shilong #endif
15338507d216SWang Shilong 	if (info->metadata_ratio)
1534764cb8b4SAnand Jain 		seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
15353cdde224SJeff Mahoney 	if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
15368c342930SJeff Mahoney 		seq_puts(seq, ",fatal_errors=panic");
15378b87dc17SDavid Sterba 	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1538d3740608SAnand Jain 		seq_printf(seq, ",commit=%u", info->commit_interval);
1539d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
15403cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_DATA))
1541d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=data");
15423cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_METADATA))
1543d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=metadata");
1544d0bd4560SJosef Bacik #endif
1545fb592373SJosef Bacik 	if (btrfs_test_opt(info, REF_VERIFY))
1546fb592373SJosef Bacik 		seq_puts(seq, ",ref_verify");
1547c8d3fe02SOmar Sandoval 	seq_printf(seq, ",subvolid=%llu",
1548c8d3fe02SOmar Sandoval 		  BTRFS_I(d_inode(dentry))->root->root_key.objectid);
15493ef3959bSJosef Bacik 	subvol_name = btrfs_get_subvol_name_from_objectid(info,
15503ef3959bSJosef Bacik 			BTRFS_I(d_inode(dentry))->root->root_key.objectid);
15513ef3959bSJosef Bacik 	if (!IS_ERR(subvol_name)) {
1552c8d3fe02SOmar Sandoval 		seq_puts(seq, ",subvol=");
15533ef3959bSJosef Bacik 		seq_escape(seq, subvol_name, " \t\n\\");
15543ef3959bSJosef Bacik 		kfree(subvol_name);
15553ef3959bSJosef Bacik 	}
1556a9572a15SEric Paris 	return 0;
1557a9572a15SEric Paris }
1558a9572a15SEric Paris 
1559a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
15602e635a27SChris Mason {
1561815745cfSAl Viro 	struct btrfs_fs_info *p = data;
1562815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(s);
15634b82d6e4SYan 
1564815745cfSAl Viro 	return fs_info->fs_devices == p->fs_devices;
15654b82d6e4SYan }
15664b82d6e4SYan 
1567450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
1568450ba0eaSJosef Bacik {
15696de1d09dSAl Viro 	int err = set_anon_super(s, data);
15706de1d09dSAl Viro 	if (!err)
1571450ba0eaSJosef Bacik 		s->s_fs_info = data;
15726de1d09dSAl Viro 	return err;
1573450ba0eaSJosef Bacik }
1574450ba0eaSJosef Bacik 
1575830c4adbSJosef Bacik /*
1576f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
1577f9d9ef62SDavid Sterba  */
1578f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
1579f9d9ef62SDavid Sterba {
1580f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1581f9d9ef62SDavid Sterba 		return 1;
1582f9d9ef62SDavid Sterba 	return 0;
1583f9d9ef62SDavid Sterba }
1584f9d9ef62SDavid Sterba 
1585bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1586ae0bc863SAnand Jain 				   struct vfsmount *mnt)
1587830c4adbSJosef Bacik {
1588830c4adbSJosef Bacik 	struct dentry *root;
1589fa330659SOmar Sandoval 	int ret;
1590830c4adbSJosef Bacik 
159105dbe683SOmar Sandoval 	if (!subvol_name) {
159205dbe683SOmar Sandoval 		if (!subvol_objectid) {
159305dbe683SOmar Sandoval 			ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
159405dbe683SOmar Sandoval 							  &subvol_objectid);
159505dbe683SOmar Sandoval 			if (ret) {
159605dbe683SOmar Sandoval 				root = ERR_PTR(ret);
159705dbe683SOmar Sandoval 				goto out;
159805dbe683SOmar Sandoval 			}
159905dbe683SOmar Sandoval 		}
1600c0c907a4SMarcos Paulo de Souza 		subvol_name = btrfs_get_subvol_name_from_objectid(
1601c0c907a4SMarcos Paulo de Souza 					btrfs_sb(mnt->mnt_sb), subvol_objectid);
160205dbe683SOmar Sandoval 		if (IS_ERR(subvol_name)) {
160305dbe683SOmar Sandoval 			root = ERR_CAST(subvol_name);
160405dbe683SOmar Sandoval 			subvol_name = NULL;
160505dbe683SOmar Sandoval 			goto out;
160605dbe683SOmar Sandoval 		}
160705dbe683SOmar Sandoval 
160805dbe683SOmar Sandoval 	}
160905dbe683SOmar Sandoval 
1610ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1611fa330659SOmar Sandoval 	/* mount_subtree() drops our reference on the vfsmount. */
1612fa330659SOmar Sandoval 	mnt = NULL;
1613830c4adbSJosef Bacik 
1614bb289b7bSOmar Sandoval 	if (!IS_ERR(root)) {
1615ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1616ab8d0fc4SJeff Mahoney 		struct btrfs_fs_info *fs_info = btrfs_sb(s);
1617bb289b7bSOmar Sandoval 		struct inode *root_inode = d_inode(root);
1618bb289b7bSOmar Sandoval 		u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1619bb289b7bSOmar Sandoval 
1620bb289b7bSOmar Sandoval 		ret = 0;
1621bb289b7bSOmar Sandoval 		if (!is_subvolume_inode(root_inode)) {
1622ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info, "'%s' is not a valid subvolume",
1623bb289b7bSOmar Sandoval 			       subvol_name);
1624bb289b7bSOmar Sandoval 			ret = -EINVAL;
1625bb289b7bSOmar Sandoval 		}
1626bb289b7bSOmar Sandoval 		if (subvol_objectid && root_objectid != subvol_objectid) {
162705dbe683SOmar Sandoval 			/*
162805dbe683SOmar Sandoval 			 * This will also catch a race condition where a
162905dbe683SOmar Sandoval 			 * subvolume which was passed by ID is renamed and
163005dbe683SOmar Sandoval 			 * another subvolume is renamed over the old location.
163105dbe683SOmar Sandoval 			 */
1632ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info,
1633ab8d0fc4SJeff Mahoney 				  "subvol '%s' does not match subvolid %llu",
1634bb289b7bSOmar Sandoval 				  subvol_name, subvol_objectid);
1635bb289b7bSOmar Sandoval 			ret = -EINVAL;
1636bb289b7bSOmar Sandoval 		}
1637bb289b7bSOmar Sandoval 		if (ret) {
1638ea441d11SAl Viro 			dput(root);
1639bb289b7bSOmar Sandoval 			root = ERR_PTR(ret);
1640ea441d11SAl Viro 			deactivate_locked_super(s);
1641bb289b7bSOmar Sandoval 		}
1642f9d9ef62SDavid Sterba 	}
1643f9d9ef62SDavid Sterba 
1644fa330659SOmar Sandoval out:
1645fa330659SOmar Sandoval 	mntput(mnt);
1646fa330659SOmar Sandoval 	kfree(subvol_name);
1647830c4adbSJosef Bacik 	return root;
1648830c4adbSJosef Bacik }
1649450ba0eaSJosef Bacik 
1650312c89fbSMisono, Tomohiro /*
1651312c89fbSMisono, Tomohiro  * Find a superblock for the given device / mount point.
1652312c89fbSMisono, Tomohiro  *
1653312c89fbSMisono, Tomohiro  * Note: This is based on mount_bdev from fs/super.c with a few additions
1654312c89fbSMisono, Tomohiro  *       for multiple device setup.  Make sure to keep it in sync.
1655312c89fbSMisono, Tomohiro  */
165672fa39f5SMisono, Tomohiro static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
165772fa39f5SMisono, Tomohiro 		int flags, const char *device_name, void *data)
165872fa39f5SMisono, Tomohiro {
165972fa39f5SMisono, Tomohiro 	struct block_device *bdev = NULL;
166072fa39f5SMisono, Tomohiro 	struct super_block *s;
166136350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
166272fa39f5SMisono, Tomohiro 	struct btrfs_fs_devices *fs_devices = NULL;
166372fa39f5SMisono, Tomohiro 	struct btrfs_fs_info *fs_info = NULL;
1664204cc0ccSAl Viro 	void *new_sec_opts = NULL;
166572fa39f5SMisono, Tomohiro 	fmode_t mode = FMODE_READ;
166672fa39f5SMisono, Tomohiro 	int error = 0;
166772fa39f5SMisono, Tomohiro 
166872fa39f5SMisono, Tomohiro 	if (!(flags & SB_RDONLY))
166972fa39f5SMisono, Tomohiro 		mode |= FMODE_WRITE;
167072fa39f5SMisono, Tomohiro 
167172fa39f5SMisono, Tomohiro 	if (data) {
1672a65001e8SAl Viro 		error = security_sb_eat_lsm_opts(data, &new_sec_opts);
167372fa39f5SMisono, Tomohiro 		if (error)
167472fa39f5SMisono, Tomohiro 			return ERR_PTR(error);
167572fa39f5SMisono, Tomohiro 	}
167672fa39f5SMisono, Tomohiro 
167772fa39f5SMisono, Tomohiro 	/*
167872fa39f5SMisono, Tomohiro 	 * Setup a dummy root and fs_info for test/set super.  This is because
167972fa39f5SMisono, Tomohiro 	 * we don't actually fill this stuff out until open_ctree, but we need
16808260edbaSJosef Bacik 	 * then open_ctree will properly initialize the file system specific
16818260edbaSJosef Bacik 	 * settings later.  btrfs_init_fs_info initializes the static elements
16828260edbaSJosef Bacik 	 * of the fs_info (locks and such) to make cleanup easier if we find a
16838260edbaSJosef Bacik 	 * superblock with our given fs_devices later on at sget() time.
168472fa39f5SMisono, Tomohiro 	 */
1685a8fd1f71SJeff Mahoney 	fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
168672fa39f5SMisono, Tomohiro 	if (!fs_info) {
168772fa39f5SMisono, Tomohiro 		error = -ENOMEM;
168872fa39f5SMisono, Tomohiro 		goto error_sec_opts;
168972fa39f5SMisono, Tomohiro 	}
16908260edbaSJosef Bacik 	btrfs_init_fs_info(fs_info);
169172fa39f5SMisono, Tomohiro 
169272fa39f5SMisono, Tomohiro 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
169372fa39f5SMisono, Tomohiro 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
169472fa39f5SMisono, Tomohiro 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
169572fa39f5SMisono, Tomohiro 		error = -ENOMEM;
169672fa39f5SMisono, Tomohiro 		goto error_fs_info;
169772fa39f5SMisono, Tomohiro 	}
169872fa39f5SMisono, Tomohiro 
1699f5194e34SDavid Sterba 	mutex_lock(&uuid_mutex);
1700fa59f27cSAnand Jain 	error = btrfs_parse_device_options(data, mode, fs_type);
170181ffd56bSDavid Sterba 	if (error) {
1702399f7f4cSDavid Sterba 		mutex_unlock(&uuid_mutex);
1703399f7f4cSDavid Sterba 		goto error_fs_info;
170481ffd56bSDavid Sterba 	}
1705399f7f4cSDavid Sterba 
170636350e95SGu Jinxiang 	device = btrfs_scan_one_device(device_name, mode, fs_type);
170736350e95SGu Jinxiang 	if (IS_ERR(device)) {
1708399f7f4cSDavid Sterba 		mutex_unlock(&uuid_mutex);
170936350e95SGu Jinxiang 		error = PTR_ERR(device);
1710399f7f4cSDavid Sterba 		goto error_fs_info;
171181ffd56bSDavid Sterba 	}
1712399f7f4cSDavid Sterba 
171336350e95SGu Jinxiang 	fs_devices = device->fs_devices;
1714399f7f4cSDavid Sterba 	fs_info->fs_devices = fs_devices;
1715399f7f4cSDavid Sterba 
171672fa39f5SMisono, Tomohiro 	error = btrfs_open_devices(fs_devices, mode, fs_type);
1717f5194e34SDavid Sterba 	mutex_unlock(&uuid_mutex);
171872fa39f5SMisono, Tomohiro 	if (error)
171972fa39f5SMisono, Tomohiro 		goto error_fs_info;
172072fa39f5SMisono, Tomohiro 
172172fa39f5SMisono, Tomohiro 	if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
172272fa39f5SMisono, Tomohiro 		error = -EACCES;
172372fa39f5SMisono, Tomohiro 		goto error_close_devices;
172472fa39f5SMisono, Tomohiro 	}
172572fa39f5SMisono, Tomohiro 
1726d24fa5c1SAnand Jain 	bdev = fs_devices->latest_dev->bdev;
172772fa39f5SMisono, Tomohiro 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
172872fa39f5SMisono, Tomohiro 		 fs_info);
172972fa39f5SMisono, Tomohiro 	if (IS_ERR(s)) {
173072fa39f5SMisono, Tomohiro 		error = PTR_ERR(s);
173172fa39f5SMisono, Tomohiro 		goto error_close_devices;
173272fa39f5SMisono, Tomohiro 	}
173372fa39f5SMisono, Tomohiro 
173472fa39f5SMisono, Tomohiro 	if (s->s_root) {
173572fa39f5SMisono, Tomohiro 		btrfs_close_devices(fs_devices);
17360d4b0463SJosef Bacik 		btrfs_free_fs_info(fs_info);
173772fa39f5SMisono, Tomohiro 		if ((flags ^ s->s_flags) & SB_RDONLY)
173872fa39f5SMisono, Tomohiro 			error = -EBUSY;
173972fa39f5SMisono, Tomohiro 	} else {
174072fa39f5SMisono, Tomohiro 		snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
174172fa39f5SMisono, Tomohiro 		btrfs_sb(s)->bdev_holder = fs_type;
17429b4e675aSDavid Sterba 		if (!strstr(crc32c_impl(), "generic"))
17439b4e675aSDavid Sterba 			set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
174472fa39f5SMisono, Tomohiro 		error = btrfs_fill_super(s, fs_devices, data);
174572fa39f5SMisono, Tomohiro 	}
1746a65001e8SAl Viro 	if (!error)
1747204cc0ccSAl Viro 		error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL);
1748a65001e8SAl Viro 	security_free_mnt_opts(&new_sec_opts);
174972fa39f5SMisono, Tomohiro 	if (error) {
175072fa39f5SMisono, Tomohiro 		deactivate_locked_super(s);
1751a65001e8SAl Viro 		return ERR_PTR(error);
175272fa39f5SMisono, Tomohiro 	}
175372fa39f5SMisono, Tomohiro 
175472fa39f5SMisono, Tomohiro 	return dget(s->s_root);
175572fa39f5SMisono, Tomohiro 
175672fa39f5SMisono, Tomohiro error_close_devices:
175772fa39f5SMisono, Tomohiro 	btrfs_close_devices(fs_devices);
175872fa39f5SMisono, Tomohiro error_fs_info:
17590d4b0463SJosef Bacik 	btrfs_free_fs_info(fs_info);
176072fa39f5SMisono, Tomohiro error_sec_opts:
176172fa39f5SMisono, Tomohiro 	security_free_mnt_opts(&new_sec_opts);
176272fa39f5SMisono, Tomohiro 	return ERR_PTR(error);
176372fa39f5SMisono, Tomohiro }
1764312c89fbSMisono, Tomohiro 
1765edf24abeSChristoph Hellwig /*
1766312c89fbSMisono, Tomohiro  * Mount function which is called by VFS layer.
1767edf24abeSChristoph Hellwig  *
1768312c89fbSMisono, Tomohiro  * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1769312c89fbSMisono, Tomohiro  * which needs vfsmount* of device's root (/).  This means device's root has to
1770312c89fbSMisono, Tomohiro  * be mounted internally in any case.
1771312c89fbSMisono, Tomohiro  *
1772312c89fbSMisono, Tomohiro  * Operation flow:
1773312c89fbSMisono, Tomohiro  *   1. Parse subvol id related options for later use in mount_subvol().
1774312c89fbSMisono, Tomohiro  *
1775312c89fbSMisono, Tomohiro  *   2. Mount device's root (/) by calling vfs_kern_mount().
1776312c89fbSMisono, Tomohiro  *
1777312c89fbSMisono, Tomohiro  *      NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1778312c89fbSMisono, Tomohiro  *      first place. In order to avoid calling btrfs_mount() again, we use
1779312c89fbSMisono, Tomohiro  *      different file_system_type which is not registered to VFS by
1780312c89fbSMisono, Tomohiro  *      register_filesystem() (btrfs_root_fs_type). As a result,
1781312c89fbSMisono, Tomohiro  *      btrfs_mount_root() is called. The return value will be used by
1782312c89fbSMisono, Tomohiro  *      mount_subtree() in mount_subvol().
1783312c89fbSMisono, Tomohiro  *
1784312c89fbSMisono, Tomohiro  *   3. Call mount_subvol() to get the dentry of subvolume. Since there is
1785312c89fbSMisono, Tomohiro  *      "btrfs subvolume set-default", mount_subvol() is called always.
1786edf24abeSChristoph Hellwig  */
1787061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1788306e16ceSDavid Sterba 		const char *device_name, void *data)
17894b82d6e4SYan {
1790312c89fbSMisono, Tomohiro 	struct vfsmount *mnt_root;
1791312c89fbSMisono, Tomohiro 	struct dentry *root;
179273f73415SJosef Bacik 	char *subvol_name = NULL;
179373f73415SJosef Bacik 	u64 subvol_objectid = 0;
17944b82d6e4SYan 	int error = 0;
17954b82d6e4SYan 
179693b9bcdfSGu Jinxiang 	error = btrfs_parse_subvol_options(data, &subvol_name,
179793b9bcdfSGu Jinxiang 					&subvol_objectid);
1798f23c8af8SIlya Dryomov 	if (error) {
1799f23c8af8SIlya Dryomov 		kfree(subvol_name);
1800061dbc6bSAl Viro 		return ERR_PTR(error);
1801f23c8af8SIlya Dryomov 	}
1802edf24abeSChristoph Hellwig 
1803312c89fbSMisono, Tomohiro 	/* mount device's root (/) */
1804312c89fbSMisono, Tomohiro 	mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data);
1805312c89fbSMisono, Tomohiro 	if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) {
1806312c89fbSMisono, Tomohiro 		if (flags & SB_RDONLY) {
1807312c89fbSMisono, Tomohiro 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1808312c89fbSMisono, Tomohiro 				flags & ~SB_RDONLY, device_name, data);
18094b82d6e4SYan 		} else {
1810312c89fbSMisono, Tomohiro 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1811312c89fbSMisono, Tomohiro 				flags | SB_RDONLY, device_name, data);
1812312c89fbSMisono, Tomohiro 			if (IS_ERR(mnt_root)) {
1813312c89fbSMisono, Tomohiro 				root = ERR_CAST(mnt_root);
1814532b618bSEric W. Biederman 				kfree(subvol_name);
1815312c89fbSMisono, Tomohiro 				goto out;
1816f667aef6SQu Wenruo 			}
1817f667aef6SQu Wenruo 
1818312c89fbSMisono, Tomohiro 			down_write(&mnt_root->mnt_sb->s_umount);
1819312c89fbSMisono, Tomohiro 			error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL);
1820312c89fbSMisono, Tomohiro 			up_write(&mnt_root->mnt_sb->s_umount);
1821312c89fbSMisono, Tomohiro 			if (error < 0) {
1822312c89fbSMisono, Tomohiro 				root = ERR_PTR(error);
1823312c89fbSMisono, Tomohiro 				mntput(mnt_root);
1824532b618bSEric W. Biederman 				kfree(subvol_name);
1825312c89fbSMisono, Tomohiro 				goto out;
1826312c89fbSMisono, Tomohiro 			}
1827312c89fbSMisono, Tomohiro 		}
1828312c89fbSMisono, Tomohiro 	}
1829312c89fbSMisono, Tomohiro 	if (IS_ERR(mnt_root)) {
1830312c89fbSMisono, Tomohiro 		root = ERR_CAST(mnt_root);
1831532b618bSEric W. Biederman 		kfree(subvol_name);
1832312c89fbSMisono, Tomohiro 		goto out;
1833f667aef6SQu Wenruo 	}
18344b82d6e4SYan 
1835312c89fbSMisono, Tomohiro 	/* mount_subvol() will free subvol_name and mnt_root */
1836ae0bc863SAnand Jain 	root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
18374b82d6e4SYan 
1838312c89fbSMisono, Tomohiro out:
1839312c89fbSMisono, Tomohiro 	return root;
18404b82d6e4SYan }
18412e635a27SChris Mason 
18420d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1843f7b885beSAnand Jain 				     u32 new_pool_size, u32 old_pool_size)
18440d2450abSSergei Trofimovich {
18450d2450abSSergei Trofimovich 	if (new_pool_size == old_pool_size)
18460d2450abSSergei Trofimovich 		return;
18470d2450abSSergei Trofimovich 
18480d2450abSSergei Trofimovich 	fs_info->thread_pool_size = new_pool_size;
18490d2450abSSergei Trofimovich 
1850efe120a0SFrank Holton 	btrfs_info(fs_info, "resize thread pool %d -> %d",
18510d2450abSSergei Trofimovich 	       old_pool_size, new_pool_size);
18520d2450abSSergei Trofimovich 
18535cdc7ad3SQu Wenruo 	btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1854afe3d242SQu Wenruo 	btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1855e66f0bb1SQu Wenruo 	btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1856fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size);
1857fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size);
1858fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_write_workers,
1859fccb5d86SQu Wenruo 				new_pool_size);
1860fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1861fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
18625b3bc44eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
18630339ef2fSQu Wenruo 	btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers,
1864ff023aacSStefan Behrens 				new_pool_size);
18650d2450abSSergei Trofimovich }
18660d2450abSSergei Trofimovich 
1867f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1868f42a34b2SMiao Xie 				       unsigned long old_opts, int flags)
1869f42a34b2SMiao Xie {
1870dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1871dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
18721751e8a6SLinus Torvalds 	     (flags & SB_RDONLY))) {
1873dc81cdc5SMiao Xie 		/* wait for any defraggers to finish */
1874dc81cdc5SMiao Xie 		wait_event(fs_info->transaction_wait,
1875dc81cdc5SMiao Xie 			   (atomic_read(&fs_info->defrag_running) == 0));
18761751e8a6SLinus Torvalds 		if (flags & SB_RDONLY)
1877dc81cdc5SMiao Xie 			sync_filesystem(fs_info->sb);
1878dc81cdc5SMiao Xie 	}
1879dc81cdc5SMiao Xie }
1880dc81cdc5SMiao Xie 
1881dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1882dc81cdc5SMiao Xie 					 unsigned long old_opts)
1883dc81cdc5SMiao Xie {
188494846229SBoris Burkov 	const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
188594846229SBoris Burkov 
1886dc81cdc5SMiao Xie 	/*
1887180e4d47SLuis de Bethencourt 	 * We need to cleanup all defragable inodes if the autodefragment is
1888180e4d47SLuis de Bethencourt 	 * close or the filesystem is read only.
1889dc81cdc5SMiao Xie 	 */
1890dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1891bc98a42cSDavid Howells 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1892dc81cdc5SMiao Xie 		btrfs_cleanup_defrag_inodes(fs_info);
1893dc81cdc5SMiao Xie 	}
1894dc81cdc5SMiao Xie 
1895b0643e59SDennis Zhou 	/* If we toggled discard async */
1896b0643e59SDennis Zhou 	if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1897b0643e59SDennis Zhou 	    btrfs_test_opt(fs_info, DISCARD_ASYNC))
1898b0643e59SDennis Zhou 		btrfs_discard_resume(fs_info);
1899b0643e59SDennis Zhou 	else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1900b0643e59SDennis Zhou 		 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1901b0643e59SDennis Zhou 		btrfs_discard_cleanup(fs_info);
190294846229SBoris Burkov 
190394846229SBoris Burkov 	/* If we toggled space cache */
190494846229SBoris Burkov 	if (cache_opt != btrfs_free_space_cache_v1_active(fs_info))
190594846229SBoris Burkov 		btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
1906dc81cdc5SMiao Xie }
1907dc81cdc5SMiao Xie 
1908c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1909c146afadSYan Zheng {
1910815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
191149b25e05SJeff Mahoney 	unsigned old_flags = sb->s_flags;
191249b25e05SJeff Mahoney 	unsigned long old_opts = fs_info->mount_opt;
191349b25e05SJeff Mahoney 	unsigned long old_compress_type = fs_info->compress_type;
191449b25e05SJeff Mahoney 	u64 old_max_inline = fs_info->max_inline;
1915f7b885beSAnand Jain 	u32 old_thread_pool_size = fs_info->thread_pool_size;
1916d612ac59SAnand Jain 	u32 old_metadata_ratio = fs_info->metadata_ratio;
1917c146afadSYan Zheng 	int ret;
1918c146afadSYan Zheng 
191902b9984dSTheodore Ts'o 	sync_filesystem(sb);
192088c4703fSJohannes Thumshirn 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1921dc81cdc5SMiao Xie 
1922f667aef6SQu Wenruo 	if (data) {
1923204cc0ccSAl Viro 		void *new_sec_opts = NULL;
1924f667aef6SQu Wenruo 
1925a65001e8SAl Viro 		ret = security_sb_eat_lsm_opts(data, &new_sec_opts);
1926a65001e8SAl Viro 		if (!ret)
1927204cc0ccSAl Viro 			ret = security_sb_remount(sb, new_sec_opts);
1928a65001e8SAl Viro 		security_free_mnt_opts(&new_sec_opts);
1929f667aef6SQu Wenruo 		if (ret)
1930f667aef6SQu Wenruo 			goto restore;
1931f667aef6SQu Wenruo 	}
1932f667aef6SQu Wenruo 
19332ff7e61eSJeff Mahoney 	ret = btrfs_parse_options(fs_info, data, *flags);
1934891f41cbSChengguang Xu 	if (ret)
193549b25e05SJeff Mahoney 		goto restore;
1936b288052eSChris Mason 
1937f42a34b2SMiao Xie 	btrfs_remount_begin(fs_info, old_opts, *flags);
19380d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
19390d2450abSSergei Trofimovich 		fs_info->thread_pool_size, old_thread_pool_size);
19400d2450abSSergei Trofimovich 
1941c55a4319SBoris Burkov 	if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) !=
1942c55a4319SBoris Burkov 	    (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
19432838d255SBoris Burkov 	    (!sb_rdonly(sb) || (*flags & SB_RDONLY))) {
19442838d255SBoris Burkov 		btrfs_warn(fs_info,
19452838d255SBoris Burkov 		"remount supports changing free space tree only from ro to rw");
19462838d255SBoris Burkov 		/* Make sure free space cache options match the state on disk */
19472838d255SBoris Burkov 		if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
19482838d255SBoris Burkov 			btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
19492838d255SBoris Burkov 			btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
19502838d255SBoris Burkov 		}
19512838d255SBoris Burkov 		if (btrfs_free_space_cache_v1_active(fs_info)) {
19522838d255SBoris Burkov 			btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
19532838d255SBoris Burkov 			btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
19542838d255SBoris Burkov 		}
19552838d255SBoris Burkov 	}
19562838d255SBoris Burkov 
19571751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
1958dc81cdc5SMiao Xie 		goto out;
1959c146afadSYan Zheng 
19601751e8a6SLinus Torvalds 	if (*flags & SB_RDONLY) {
19618dabb742SStefan Behrens 		/*
19628dabb742SStefan Behrens 		 * this also happens on 'umount -rf' or on shutdown, when
19638dabb742SStefan Behrens 		 * the filesystem is busy.
19648dabb742SStefan Behrens 		 */
196521c7e756SMiao Xie 		cancel_work_sync(&fs_info->async_reclaim_work);
196657056740SJosef Bacik 		cancel_work_sync(&fs_info->async_data_reclaim_work);
1967361c093dSStefan Behrens 
1968b0643e59SDennis Zhou 		btrfs_discard_cleanup(fs_info);
1969b0643e59SDennis Zhou 
1970361c093dSStefan Behrens 		/* wait for the uuid_scan task to finish */
1971361c093dSStefan Behrens 		down(&fs_info->uuid_tree_rescan_sem);
1972361c093dSStefan Behrens 		/* avoid complains from lockdep et al. */
1973361c093dSStefan Behrens 		up(&fs_info->uuid_tree_rescan_sem);
1974361c093dSStefan Behrens 
1975a0a1db70SFilipe Manana 		btrfs_set_sb_rdonly(sb);
1976c146afadSYan Zheng 
1977e44163e1SJeff Mahoney 		/*
19781751e8a6SLinus Torvalds 		 * Setting SB_RDONLY will put the cleaner thread to
1979e44163e1SJeff Mahoney 		 * sleep at the next loop if it's already active.
1980e44163e1SJeff Mahoney 		 * If it's already asleep, we'll leave unused block
1981e44163e1SJeff Mahoney 		 * groups on disk until we're mounted read-write again
1982e44163e1SJeff Mahoney 		 * unless we clean them up here.
1983e44163e1SJeff Mahoney 		 */
1984e44163e1SJeff Mahoney 		btrfs_delete_unused_bgs(fs_info);
1985e44163e1SJeff Mahoney 
1986a0a1db70SFilipe Manana 		/*
1987a0a1db70SFilipe Manana 		 * The cleaner task could be already running before we set the
1988a0a1db70SFilipe Manana 		 * flag BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock).
1989a0a1db70SFilipe Manana 		 * We must make sure that after we finish the remount, i.e. after
1990a0a1db70SFilipe Manana 		 * we call btrfs_commit_super(), the cleaner can no longer start
1991a0a1db70SFilipe Manana 		 * a transaction - either because it was dropping a dead root,
1992a0a1db70SFilipe Manana 		 * running delayed iputs or deleting an unused block group (the
1993a0a1db70SFilipe Manana 		 * cleaner picked a block group from the list of unused block
1994a0a1db70SFilipe Manana 		 * groups before we were able to in the previous call to
1995a0a1db70SFilipe Manana 		 * btrfs_delete_unused_bgs()).
1996a0a1db70SFilipe Manana 		 */
1997a0a1db70SFilipe Manana 		wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING,
1998a0a1db70SFilipe Manana 			    TASK_UNINTERRUPTIBLE);
1999a0a1db70SFilipe Manana 
2000a8cc263eSFilipe Manana 		/*
2001a8cc263eSFilipe Manana 		 * We've set the superblock to RO mode, so we might have made
2002a8cc263eSFilipe Manana 		 * the cleaner task sleep without running all pending delayed
2003a8cc263eSFilipe Manana 		 * iputs. Go through all the delayed iputs here, so that if an
2004a8cc263eSFilipe Manana 		 * unmount happens without remounting RW we don't end up at
2005a8cc263eSFilipe Manana 		 * finishing close_ctree() with a non-empty list of delayed
2006a8cc263eSFilipe Manana 		 * iputs.
2007a8cc263eSFilipe Manana 		 */
2008a8cc263eSFilipe Manana 		btrfs_run_delayed_iputs(fs_info);
2009a8cc263eSFilipe Manana 
20108dabb742SStefan Behrens 		btrfs_dev_replace_suspend_for_unmount(fs_info);
20118dabb742SStefan Behrens 		btrfs_scrub_cancel(fs_info);
2012061594efSMiao Xie 		btrfs_pause_balance(fs_info);
20138dabb742SStefan Behrens 
2014cb13eea3SFilipe Manana 		/*
2015cb13eea3SFilipe Manana 		 * Pause the qgroup rescan worker if it is running. We don't want
2016cb13eea3SFilipe Manana 		 * it to be still running after we are in RO mode, as after that,
2017cb13eea3SFilipe Manana 		 * by the time we unmount, it might have left a transaction open,
2018cb13eea3SFilipe Manana 		 * so we would leak the transaction and/or crash.
2019cb13eea3SFilipe Manana 		 */
2020cb13eea3SFilipe Manana 		btrfs_qgroup_wait_for_completion(fs_info, false);
2021cb13eea3SFilipe Manana 
20226bccf3abSJeff Mahoney 		ret = btrfs_commit_super(fs_info);
202349b25e05SJeff Mahoney 		if (ret)
202449b25e05SJeff Mahoney 			goto restore;
2025c146afadSYan Zheng 	} else {
202684961539SJosef Bacik 		if (BTRFS_FS_ERROR(fs_info)) {
20276ef3de9cSDavid Sterba 			btrfs_err(fs_info,
2028efe120a0SFrank Holton 				"Remounting read-write after error is not allowed");
20296ef3de9cSDavid Sterba 			ret = -EINVAL;
20306ef3de9cSDavid Sterba 			goto restore;
20316ef3de9cSDavid Sterba 		}
20328a3db184SSergei Trofimovich 		if (fs_info->fs_devices->rw_devices == 0) {
203349b25e05SJeff Mahoney 			ret = -EACCES;
203449b25e05SJeff Mahoney 			goto restore;
20358a3db184SSergei Trofimovich 		}
20362b82032cSYan Zheng 
20376528b99dSAnand Jain 		if (!btrfs_check_rw_degradable(fs_info, NULL)) {
2038efe120a0SFrank Holton 			btrfs_warn(fs_info,
203952042d8eSAndrea Gelmini 		"too many missing devices, writable remount is not allowed");
2040292fd7fcSStefan Behrens 			ret = -EACCES;
2041292fd7fcSStefan Behrens 			goto restore;
2042292fd7fcSStefan Behrens 		}
2043292fd7fcSStefan Behrens 
20448a3db184SSergei Trofimovich 		if (btrfs_super_log_root(fs_info->super_copy) != 0) {
204510a3a3edSDavid Sterba 			btrfs_warn(fs_info,
204610a3a3edSDavid Sterba 		"mount required to replay tree-log, cannot remount read-write");
204749b25e05SJeff Mahoney 			ret = -EINVAL;
204849b25e05SJeff Mahoney 			goto restore;
20498a3db184SSergei Trofimovich 		}
2050c146afadSYan Zheng 
205144c0ca21SBoris Burkov 		/*
205244c0ca21SBoris Burkov 		 * NOTE: when remounting with a change that does writes, don't
205344c0ca21SBoris Burkov 		 * put it anywhere above this point, as we are not sure to be
205444c0ca21SBoris Burkov 		 * safe to write until we pass the above checks.
205544c0ca21SBoris Burkov 		 */
205644c0ca21SBoris Burkov 		ret = btrfs_start_pre_rw_mount(fs_info);
205749b25e05SJeff Mahoney 		if (ret)
205849b25e05SJeff Mahoney 			goto restore;
2059c146afadSYan Zheng 
2060a0a1db70SFilipe Manana 		btrfs_clear_sb_rdonly(sb);
206190c711abSZygo Blaxell 
2062afcdd129SJosef Bacik 		set_bit(BTRFS_FS_OPEN, &fs_info->flags);
2063c146afadSYan Zheng 	}
2064dc81cdc5SMiao Xie out:
2065faa00889SJosef Bacik 	/*
2066faa00889SJosef Bacik 	 * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS,
2067faa00889SJosef Bacik 	 * since the absence of the flag means it can be toggled off by remount.
2068faa00889SJosef Bacik 	 */
2069faa00889SJosef Bacik 	*flags |= SB_I_VERSION;
2070faa00889SJosef Bacik 
20712c6a92b0SJustin Maggard 	wake_up_process(fs_info->transaction_kthread);
2072dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
20738cd29088SBoris Burkov 	btrfs_clear_oneshot_options(fs_info);
207488c4703fSJohannes Thumshirn 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
207588c4703fSJohannes Thumshirn 
2076c146afadSYan Zheng 	return 0;
207749b25e05SJeff Mahoney 
207849b25e05SJeff Mahoney restore:
20791751e8a6SLinus Torvalds 	/* We've hit an error - don't reset SB_RDONLY */
2080bc98a42cSDavid Howells 	if (sb_rdonly(sb))
20811751e8a6SLinus Torvalds 		old_flags |= SB_RDONLY;
2082a0a1db70SFilipe Manana 	if (!(old_flags & SB_RDONLY))
2083a0a1db70SFilipe Manana 		clear_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state);
208449b25e05SJeff Mahoney 	sb->s_flags = old_flags;
208549b25e05SJeff Mahoney 	fs_info->mount_opt = old_opts;
208649b25e05SJeff Mahoney 	fs_info->compress_type = old_compress_type;
208749b25e05SJeff Mahoney 	fs_info->max_inline = old_max_inline;
20880d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
20890d2450abSSergei Trofimovich 		old_thread_pool_size, fs_info->thread_pool_size);
209049b25e05SJeff Mahoney 	fs_info->metadata_ratio = old_metadata_ratio;
2091dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
209288c4703fSJohannes Thumshirn 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
209388c4703fSJohannes Thumshirn 
209449b25e05SJeff Mahoney 	return ret;
2095c146afadSYan Zheng }
2096c146afadSYan Zheng 
2097bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
2098214cc184SDavid Sterba static int btrfs_cmp_device_free_bytes(const void *a, const void *b)
2099bcd53741SArne Jansen {
2100214cc184SDavid Sterba 	const struct btrfs_device_info *dev_info1 = a;
2101214cc184SDavid Sterba 	const struct btrfs_device_info *dev_info2 = b;
2102214cc184SDavid Sterba 
2103214cc184SDavid Sterba 	if (dev_info1->max_avail > dev_info2->max_avail)
2104bcd53741SArne Jansen 		return -1;
2105214cc184SDavid Sterba 	else if (dev_info1->max_avail < dev_info2->max_avail)
2106bcd53741SArne Jansen 		return 1;
2107bcd53741SArne Jansen 	return 0;
2108bcd53741SArne Jansen }
2109bcd53741SArne Jansen 
2110bcd53741SArne Jansen /*
2111bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
2112bcd53741SArne Jansen  * is stored.(Descending Sort)
2113bcd53741SArne Jansen  */
2114bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
2115bcd53741SArne Jansen 					struct btrfs_device_info *devices,
2116bcd53741SArne Jansen 					size_t nr_devices)
2117bcd53741SArne Jansen {
2118bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
2119bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
2120bcd53741SArne Jansen }
2121bcd53741SArne Jansen 
21226d07bcecSMiao Xie /*
21236d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
21246d07bcecSMiao Xie  * file data.
21256d07bcecSMiao Xie  */
21267e17916bSArnd Bergmann static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
21276bccf3abSJeff Mahoney 					      u64 *free_bytes)
21286d07bcecSMiao Xie {
21296d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
21306d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
21316d07bcecSMiao Xie 	struct btrfs_device *device;
21326d07bcecSMiao Xie 	u64 type;
21336d07bcecSMiao Xie 	u64 avail_space;
21346d07bcecSMiao Xie 	u64 min_stripe_size;
2135559ca6eaSNikolay Borisov 	int num_stripes = 1;
21366d07bcecSMiao Xie 	int i = 0, nr_devices;
21374f080f57SDavid Sterba 	const struct btrfs_raid_attr *rattr;
21386d07bcecSMiao Xie 
21397e33fd99SJosef Bacik 	/*
214001327610SNicholas D Steeves 	 * We aren't under the device list lock, so this is racy-ish, but good
21417e33fd99SJosef Bacik 	 * enough for our purposes.
21427e33fd99SJosef Bacik 	 */
2143b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
21447e33fd99SJosef Bacik 	if (!nr_devices) {
21457e33fd99SJosef Bacik 		smp_mb();
21467e33fd99SJosef Bacik 		nr_devices = fs_info->fs_devices->open_devices;
21477e33fd99SJosef Bacik 		ASSERT(nr_devices);
21487e33fd99SJosef Bacik 		if (!nr_devices) {
21497e33fd99SJosef Bacik 			*free_bytes = 0;
21507e33fd99SJosef Bacik 			return 0;
21517e33fd99SJosef Bacik 		}
21527e33fd99SJosef Bacik 	}
21536d07bcecSMiao Xie 
2154d9b0d9baSDulshani Gunawardhana 	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
21556a44517dSDavid Sterba 			       GFP_KERNEL);
21566d07bcecSMiao Xie 	if (!devices_info)
21576d07bcecSMiao Xie 		return -ENOMEM;
21586d07bcecSMiao Xie 
215901327610SNicholas D Steeves 	/* calc min stripe number for data space allocation */
21601b86826dSJeff Mahoney 	type = btrfs_data_alloc_profile(fs_info);
21614f080f57SDavid Sterba 	rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
21624f080f57SDavid Sterba 
2163e1ea2beeSDavid Sterba 	if (type & BTRFS_BLOCK_GROUP_RAID0)
216439fb26c3SMiao Xie 		num_stripes = nr_devices;
2165e1ea2beeSDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1)
216639fb26c3SMiao Xie 		num_stripes = 2;
216747e6f742SDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1C3)
216847e6f742SDavid Sterba 		num_stripes = 3;
21698d6fac00SDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1C4)
21708d6fac00SDavid Sterba 		num_stripes = 4;
2171e1ea2beeSDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID10)
217239fb26c3SMiao Xie 		num_stripes = 4;
21736d07bcecSMiao Xie 
21744f080f57SDavid Sterba 	/* Adjust for more than 1 stripe per device */
21754f080f57SDavid Sterba 	min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
21766d07bcecSMiao Xie 
21777e33fd99SJosef Bacik 	rcu_read_lock();
21787e33fd99SJosef Bacik 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
2179e12c9621SAnand Jain 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2180e12c9621SAnand Jain 						&device->dev_state) ||
2181401e29c1SAnand Jain 		    !device->bdev ||
2182401e29c1SAnand Jain 		    test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
21836d07bcecSMiao Xie 			continue;
21846d07bcecSMiao Xie 
21857e33fd99SJosef Bacik 		if (i >= nr_devices)
21867e33fd99SJosef Bacik 			break;
21877e33fd99SJosef Bacik 
21886d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
21896d07bcecSMiao Xie 
21906d07bcecSMiao Xie 		/* align with stripe_len */
2191559ca6eaSNikolay Borisov 		avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
21926d07bcecSMiao Xie 
21936d07bcecSMiao Xie 		/*
219401327610SNicholas D Steeves 		 * In order to avoid overwriting the superblock on the drive,
21956d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
21966d07bcecSMiao Xie 		 * allocation.
2197559ca6eaSNikolay Borisov 		 *
2198559ca6eaSNikolay Borisov 		 * This ensures we have at least min_stripe_size free space
2199559ca6eaSNikolay Borisov 		 * after excluding 1MB.
22006d07bcecSMiao Xie 		 */
2201559ca6eaSNikolay Borisov 		if (avail_space <= SZ_1M + min_stripe_size)
22026d07bcecSMiao Xie 			continue;
22036d07bcecSMiao Xie 
2204559ca6eaSNikolay Borisov 		avail_space -= SZ_1M;
2205559ca6eaSNikolay Borisov 
22066d07bcecSMiao Xie 		devices_info[i].dev = device;
22076d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
22086d07bcecSMiao Xie 
22096d07bcecSMiao Xie 		i++;
22106d07bcecSMiao Xie 	}
22117e33fd99SJosef Bacik 	rcu_read_unlock();
22126d07bcecSMiao Xie 
22136d07bcecSMiao Xie 	nr_devices = i;
22146d07bcecSMiao Xie 
22156d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
22166d07bcecSMiao Xie 
22176d07bcecSMiao Xie 	i = nr_devices - 1;
22186d07bcecSMiao Xie 	avail_space = 0;
2219559ca6eaSNikolay Borisov 	while (nr_devices >= rattr->devs_min) {
2220559ca6eaSNikolay Borisov 		num_stripes = min(num_stripes, nr_devices);
222139fb26c3SMiao Xie 
22226d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
22236d07bcecSMiao Xie 			int j;
22246d07bcecSMiao Xie 			u64 alloc_size;
22256d07bcecSMiao Xie 
222639fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
22276d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
222839fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
22296d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
22306d07bcecSMiao Xie 		}
22316d07bcecSMiao Xie 		i--;
22326d07bcecSMiao Xie 		nr_devices--;
22336d07bcecSMiao Xie 	}
22346d07bcecSMiao Xie 
22356d07bcecSMiao Xie 	kfree(devices_info);
22366d07bcecSMiao Xie 	*free_bytes = avail_space;
22376d07bcecSMiao Xie 	return 0;
22386d07bcecSMiao Xie }
22396d07bcecSMiao Xie 
2240ba7b6e62SDavid Sterba /*
2241ba7b6e62SDavid Sterba  * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2242ba7b6e62SDavid Sterba  *
2243ba7b6e62SDavid Sterba  * If there's a redundant raid level at DATA block groups, use the respective
2244ba7b6e62SDavid Sterba  * multiplier to scale the sizes.
2245ba7b6e62SDavid Sterba  *
2246ba7b6e62SDavid Sterba  * Unused device space usage is based on simulating the chunk allocator
22470d0c71b3SDavid Sterba  * algorithm that respects the device sizes and order of allocations.  This is
22480d0c71b3SDavid Sterba  * a close approximation of the actual use but there are other factors that may
22490d0c71b3SDavid Sterba  * change the result (like a new metadata chunk).
2250ba7b6e62SDavid Sterba  *
2251ca8a51b3SDavid Sterba  * If metadata is exhausted, f_bavail will be 0.
2252ba7b6e62SDavid Sterba  */
22538fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
22548fd17795SChris Mason {
2255815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
2256815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
2257bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
2258bd4d1088SJosef Bacik 	u64 total_used = 0;
22596d07bcecSMiao Xie 	u64 total_free_data = 0;
2260ca8a51b3SDavid Sterba 	u64 total_free_meta = 0;
2261265fdfa6SDavid Sterba 	u32 bits = fs_info->sectorsize_bits;
2262de37aa51SNikolay Borisov 	__be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
2263ba7b6e62SDavid Sterba 	unsigned factor = 1;
2264ba7b6e62SDavid Sterba 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
22656d07bcecSMiao Xie 	int ret;
2266ca8a51b3SDavid Sterba 	u64 thresh = 0;
2267ae02d1bdSLuis de Bethencourt 	int mixed = 0;
22688fd17795SChris Mason 
226972804905SJosef Bacik 	list_for_each_entry(found, &fs_info->space_info, list) {
22706d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
2271ba7b6e62SDavid Sterba 			int i;
2272ba7b6e62SDavid Sterba 
22736d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
22746d07bcecSMiao Xie 			total_free_data -=
22756d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
2276ba7b6e62SDavid Sterba 
2277ba7b6e62SDavid Sterba 			for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
227846df06b8SDavid Sterba 				if (!list_empty(&found->block_groups[i]))
227946df06b8SDavid Sterba 					factor = btrfs_bg_type_to_factor(
228046df06b8SDavid Sterba 						btrfs_raid_array[i].bg_flag);
2281ba7b6e62SDavid Sterba 			}
22826d07bcecSMiao Xie 		}
2283ae02d1bdSLuis de Bethencourt 
2284ae02d1bdSLuis de Bethencourt 		/*
2285ae02d1bdSLuis de Bethencourt 		 * Metadata in mixed block goup profiles are accounted in data
2286ae02d1bdSLuis de Bethencourt 		 */
2287ae02d1bdSLuis de Bethencourt 		if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
2288ae02d1bdSLuis de Bethencourt 			if (found->flags & BTRFS_BLOCK_GROUP_DATA)
2289ae02d1bdSLuis de Bethencourt 				mixed = 1;
2290ae02d1bdSLuis de Bethencourt 			else
2291ae02d1bdSLuis de Bethencourt 				total_free_meta += found->disk_total -
2292ae02d1bdSLuis de Bethencourt 					found->disk_used;
2293ae02d1bdSLuis de Bethencourt 		}
22946d07bcecSMiao Xie 
2295b742bb82SYan, Zheng 		total_used += found->disk_used;
229689a55897SJosef Bacik 	}
2297ba7b6e62SDavid Sterba 
2298ba7b6e62SDavid Sterba 	buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
2299ba7b6e62SDavid Sterba 	buf->f_blocks >>= bits;
2300ba7b6e62SDavid Sterba 	buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
2301ba7b6e62SDavid Sterba 
2302ba7b6e62SDavid Sterba 	/* Account global block reserve as used, it's in logical size already */
2303ba7b6e62SDavid Sterba 	spin_lock(&block_rsv->lock);
230441b34accSLuis de Bethencourt 	/* Mixed block groups accounting is not byte-accurate, avoid overflow */
230541b34accSLuis de Bethencourt 	if (buf->f_bfree >= block_rsv->size >> bits)
2306ba7b6e62SDavid Sterba 		buf->f_bfree -= block_rsv->size >> bits;
230741b34accSLuis de Bethencourt 	else
230841b34accSLuis de Bethencourt 		buf->f_bfree = 0;
2309ba7b6e62SDavid Sterba 	spin_unlock(&block_rsv->lock);
2310ba7b6e62SDavid Sterba 
23110d95c1beSDavid Sterba 	buf->f_bavail = div_u64(total_free_data, factor);
23126bccf3abSJeff Mahoney 	ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
23137e33fd99SJosef Bacik 	if (ret)
23146d07bcecSMiao Xie 		return ret;
2315ba7b6e62SDavid Sterba 	buf->f_bavail += div_u64(total_free_data, factor);
23166d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
2317d397712bSChris Mason 
2318ca8a51b3SDavid Sterba 	/*
2319ca8a51b3SDavid Sterba 	 * We calculate the remaining metadata space minus global reserve. If
2320ca8a51b3SDavid Sterba 	 * this is (supposedly) smaller than zero, there's no space. But this
2321ca8a51b3SDavid Sterba 	 * does not hold in practice, the exhausted state happens where's still
2322ca8a51b3SDavid Sterba 	 * some positive delta. So we apply some guesswork and compare the
2323ca8a51b3SDavid Sterba 	 * delta to a 4M threshold.  (Practically observed delta was ~2M.)
2324ca8a51b3SDavid Sterba 	 *
2325ca8a51b3SDavid Sterba 	 * We probably cannot calculate the exact threshold value because this
2326ca8a51b3SDavid Sterba 	 * depends on the internal reservations requested by various
2327ca8a51b3SDavid Sterba 	 * operations, so some operations that consume a few metadata will
2328ca8a51b3SDavid Sterba 	 * succeed even if the Avail is zero. But this is better than the other
2329ca8a51b3SDavid Sterba 	 * way around.
2330ca8a51b3SDavid Sterba 	 */
2331d4417e22SNikolay Borisov 	thresh = SZ_4M;
2332ca8a51b3SDavid Sterba 
2333d55966c4SJosef Bacik 	/*
2334d55966c4SJosef Bacik 	 * We only want to claim there's no available space if we can no longer
2335d55966c4SJosef Bacik 	 * allocate chunks for our metadata profile and our global reserve will
2336d55966c4SJosef Bacik 	 * not fit in the free metadata space.  If we aren't ->full then we
2337d55966c4SJosef Bacik 	 * still can allocate chunks and thus are fine using the currently
2338d55966c4SJosef Bacik 	 * calculated f_bavail.
2339d55966c4SJosef Bacik 	 */
2340d55966c4SJosef Bacik 	if (!mixed && block_rsv->space_info->full &&
2341d55966c4SJosef Bacik 	    total_free_meta - thresh < block_rsv->size)
2342ca8a51b3SDavid Sterba 		buf->f_bavail = 0;
2343ca8a51b3SDavid Sterba 
2344ba7b6e62SDavid Sterba 	buf->f_type = BTRFS_SUPER_MAGIC;
2345ba7b6e62SDavid Sterba 	buf->f_bsize = dentry->d_sb->s_blocksize;
2346ba7b6e62SDavid Sterba 	buf->f_namelen = BTRFS_NAME_LEN;
2347ba7b6e62SDavid Sterba 
23489d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
23499d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
23509d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
23519d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
23529d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
235332d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
23544fd786e6SMisono Tomohiro 	buf->f_fsid.val[0] ^=
23554fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32;
23564fd786e6SMisono Tomohiro 	buf->f_fsid.val[1] ^=
23574fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid;
235832d48fa1SDavid Woodhouse 
23598fd17795SChris Mason 	return 0;
23608fd17795SChris Mason }
2361b5133862SChris Mason 
2362aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
2363aea52e19SAl Viro {
2364815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2365aea52e19SAl Viro 	kill_anon_super(sb);
23660d4b0463SJosef Bacik 	btrfs_free_fs_info(fs_info);
2367aea52e19SAl Viro }
2368aea52e19SAl Viro 
23692e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
23702e635a27SChris Mason 	.owner		= THIS_MODULE,
23712e635a27SChris Mason 	.name		= "btrfs",
2372061dbc6bSAl Viro 	.mount		= btrfs_mount,
2373aea52e19SAl Viro 	.kill_sb	= btrfs_kill_super,
2374f667aef6SQu Wenruo 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
23752e635a27SChris Mason };
237672fa39f5SMisono, Tomohiro 
237772fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type = {
237872fa39f5SMisono, Tomohiro 	.owner		= THIS_MODULE,
237972fa39f5SMisono, Tomohiro 	.name		= "btrfs",
238072fa39f5SMisono, Tomohiro 	.mount		= btrfs_mount_root,
238172fa39f5SMisono, Tomohiro 	.kill_sb	= btrfs_kill_super,
23825b9b26f5SChristian Brauner 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP,
238372fa39f5SMisono, Tomohiro };
238472fa39f5SMisono, Tomohiro 
23857f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs");
2386a9218f6bSChris Mason 
2387d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file)
2388d8620958STom Van Braeckel {
2389d8620958STom Van Braeckel 	/*
2390d8620958STom Van Braeckel 	 * The control file's private_data is used to hold the
2391d8620958STom Van Braeckel 	 * transaction when it is started and is used to keep
2392d8620958STom Van Braeckel 	 * track of whether a transaction is already in progress.
2393d8620958STom Van Braeckel 	 */
2394d8620958STom Van Braeckel 	file->private_data = NULL;
2395d8620958STom Van Braeckel 	return 0;
2396d8620958STom Van Braeckel }
2397d8620958STom Van Braeckel 
2398d352ac68SChris Mason /*
2399cfe953c8SSu Yue  * Used by /dev/btrfs-control for devices ioctls.
2400d352ac68SChris Mason  */
24018a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
24028a4b83ccSChris Mason 				unsigned long arg)
24038a4b83ccSChris Mason {
24048a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
240536350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
240616cab91aSAnand Jain 	dev_t devt = 0;
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:
242616cab91aSAnand Jain 		if (vol->name[0] != 0) {
242716cab91aSAnand Jain 			ret = lookup_bdev(vol->name, &devt);
242816cab91aSAnand Jain 			if (ret)
242916cab91aSAnand Jain 				break;
243016cab91aSAnand Jain 		}
243116cab91aSAnand Jain 		ret = btrfs_forget_devices(devt);
2432228a73abSAnand Jain 		break;
243302db0844SJosef Bacik 	case BTRFS_IOC_DEVICES_READY:
2434899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
243536350e95SGu Jinxiang 		device = btrfs_scan_one_device(vol->name, FMODE_READ,
243636350e95SGu Jinxiang 					       &btrfs_root_fs_type);
243736350e95SGu Jinxiang 		if (IS_ERR(device)) {
2438899f9307SDavid Sterba 			mutex_unlock(&uuid_mutex);
243936350e95SGu Jinxiang 			ret = PTR_ERR(device);
244002db0844SJosef Bacik 			break;
2441899f9307SDavid Sterba 		}
244236350e95SGu Jinxiang 		ret = !(device->fs_devices->num_devices ==
244336350e95SGu Jinxiang 			device->fs_devices->total_devices);
2444899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
244502db0844SJosef Bacik 		break;
2446c5868f83SDavid Sterba 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2447d5131b65SDavid Sterba 		ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2448c5868f83SDavid Sterba 		break;
24498a4b83ccSChris Mason 	}
2450dae7b665SLi Zefan 
24518a4b83ccSChris Mason 	kfree(vol);
2452f819d837SLinda Knippers 	return ret;
24538a4b83ccSChris Mason }
24548a4b83ccSChris Mason 
24550176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
2456ed0dab6bSYan {
2457354aa0fbSMiao Xie 	struct btrfs_trans_handle *trans;
24580b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
24590b246afaSJeff Mahoney 	struct btrfs_root *root = fs_info->tree_root;
2460354aa0fbSMiao Xie 
2461fac03c8dSDavid Sterba 	set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
24629e7cc91aSWang Xiaoguang 	/*
24639e7cc91aSWang Xiaoguang 	 * We don't need a barrier here, we'll wait for any transaction that
24649e7cc91aSWang Xiaoguang 	 * could be in progress on other threads (and do delayed iputs that
24659e7cc91aSWang Xiaoguang 	 * we want to avoid on a frozen filesystem), or do the commit
24669e7cc91aSWang Xiaoguang 	 * ourselves.
24679e7cc91aSWang Xiaoguang 	 */
2468d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
2469354aa0fbSMiao Xie 	if (IS_ERR(trans)) {
2470354aa0fbSMiao Xie 		/* no transaction, don't bother */
2471354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
24720176260fSLinus Torvalds 			return 0;
2473354aa0fbSMiao Xie 		return PTR_ERR(trans);
2474354aa0fbSMiao Xie 	}
24753a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
2476ed0dab6bSYan }
2477ed0dab6bSYan 
24789e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb)
24799e7cc91aSWang Xiaoguang {
2480fac03c8dSDavid Sterba 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2481fac03c8dSDavid Sterba 
2482fac03c8dSDavid Sterba 	clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
24839e7cc91aSWang Xiaoguang 	return 0;
24849e7cc91aSWang Xiaoguang }
24859e7cc91aSWang Xiaoguang 
24869c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
24879c5085c1SJosef Bacik {
24889c5085c1SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
24899c5085c1SJosef Bacik 
249088c14590SDavid Sterba 	/*
24916605fd2fSAnand Jain 	 * There should be always a valid pointer in latest_dev, it may be stale
24926605fd2fSAnand Jain 	 * for a short moment in case it's being deleted but still valid until
24936605fd2fSAnand Jain 	 * the end of RCU grace period.
249488c14590SDavid Sterba 	 */
249588c14590SDavid Sterba 	rcu_read_lock();
24966605fd2fSAnand Jain 	seq_escape(m, rcu_str_deref(fs_info->fs_devices->latest_dev->name), " \t\n\\");
249788c14590SDavid Sterba 	rcu_read_unlock();
24986605fd2fSAnand Jain 
24999c5085c1SJosef Bacik 	return 0;
25009c5085c1SJosef Bacik }
25019c5085c1SJosef Bacik 
2502b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
250376dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
2504bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
2505e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
2506d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
2507a9572a15SEric Paris 	.show_options	= btrfs_show_options,
25089c5085c1SJosef Bacik 	.show_devname	= btrfs_show_devname,
25092c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
25102c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
251126602cabSAl Viro 	.free_inode	= btrfs_free_inode,
25128fd17795SChris Mason 	.statfs		= btrfs_statfs,
2513c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
25140176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
25159e7cc91aSWang Xiaoguang 	.unfreeze_fs	= btrfs_unfreeze,
2516e20d96d6SChris Mason };
2517a9218f6bSChris Mason 
2518a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
2519d8620958STom Van Braeckel 	.open = btrfs_control_open,
2520a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
25211832f2d8SArnd Bergmann 	.compat_ioctl = compat_ptr_ioctl,
2522a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
25236038f373SArnd Bergmann 	.llseek = noop_llseek,
2524a9218f6bSChris Mason };
2525a9218f6bSChris Mason 
2526a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
2527578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
2528a9218f6bSChris Mason 	.name		= "btrfs-control",
2529a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
2530a9218f6bSChris Mason };
2531a9218f6bSChris Mason 
2532578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2533578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
2534578454ffSKay Sievers 
2535f5c29bd9SLiu Bo static int __init btrfs_interface_init(void)
2536a9218f6bSChris Mason {
2537a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
2538a9218f6bSChris Mason }
2539a9218f6bSChris Mason 
2540e67c718bSDavid Sterba static __cold void btrfs_interface_exit(void)
2541a9218f6bSChris Mason {
2542f368ed60SGreg Kroah-Hartman 	misc_deregister(&btrfs_misc);
2543a9218f6bSChris Mason }
2544a9218f6bSChris Mason 
2545f5c29bd9SLiu Bo static void __init btrfs_print_mod_info(void)
254685965600SDavid Sterba {
2547edf57cbfSBart Van Assche 	static const char options[] = ""
254885965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
254985965600SDavid Sterba 			", debug=on"
255085965600SDavid Sterba #endif
255179556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT
255279556c3dSStefan Behrens 			", assert=on"
255379556c3dSStefan Behrens #endif
255485965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
255585965600SDavid Sterba 			", integrity-checker=on"
255685965600SDavid Sterba #endif
2557fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2558fb592373SJosef Bacik 			", ref-verify=on"
2559fb592373SJosef Bacik #endif
25605b316468SNaohiro Aota #ifdef CONFIG_BLK_DEV_ZONED
25615b316468SNaohiro Aota 			", zoned=yes"
25625b316468SNaohiro Aota #else
25635b316468SNaohiro Aota 			", zoned=no"
25645b316468SNaohiro Aota #endif
2565ea3dc7d2SDavid Sterba #ifdef CONFIG_FS_VERITY
2566ea3dc7d2SDavid Sterba 			", fsverity=yes"
2567ea3dc7d2SDavid Sterba #else
2568ea3dc7d2SDavid Sterba 			", fsverity=no"
2569ea3dc7d2SDavid Sterba #endif
2570edf57cbfSBart Van Assche 			;
2571edf57cbfSBart Van Assche 	pr_info("Btrfs loaded, crc32c=%s%s\n", crc32c_impl(), options);
257285965600SDavid Sterba }
257385965600SDavid Sterba 
25742e635a27SChris Mason static int __init init_btrfs_fs(void)
25752e635a27SChris Mason {
25762c90e5d6SChris Mason 	int err;
257758176a96SJosef Bacik 
257863541927SFilipe David Borba Manana 	btrfs_props_init();
257963541927SFilipe David Borba Manana 
258058176a96SJosef Bacik 	err = btrfs_init_sysfs();
258158176a96SJosef Bacik 	if (err)
25829678c543SNikolay Borisov 		return err;
258358176a96SJosef Bacik 
2584143bede5SJeff Mahoney 	btrfs_init_compress();
2585d1310b2eSChris Mason 
2586261507a0SLi Zefan 	err = btrfs_init_cachep();
2587261507a0SLi Zefan 	if (err)
2588261507a0SLi Zefan 		goto free_compress;
2589261507a0SLi Zefan 
2590d1310b2eSChris Mason 	err = extent_io_init();
25912f4cbe64SWyatt Banks 	if (err)
25922f4cbe64SWyatt Banks 		goto free_cachep;
25932f4cbe64SWyatt Banks 
25946f0d04f8SJosef Bacik 	err = extent_state_cache_init();
2595d1310b2eSChris Mason 	if (err)
2596d1310b2eSChris Mason 		goto free_extent_io;
2597d1310b2eSChris Mason 
25986f0d04f8SJosef Bacik 	err = extent_map_init();
25996f0d04f8SJosef Bacik 	if (err)
26006f0d04f8SJosef Bacik 		goto free_extent_state_cache;
26016f0d04f8SJosef Bacik 
26026352b91dSMiao Xie 	err = ordered_data_init();
26032f4cbe64SWyatt Banks 	if (err)
26042f4cbe64SWyatt Banks 		goto free_extent_map;
2605c8b97818SChris Mason 
26066352b91dSMiao Xie 	err = btrfs_delayed_inode_init();
26076352b91dSMiao Xie 	if (err)
26086352b91dSMiao Xie 		goto free_ordered_data;
26096352b91dSMiao Xie 
26109247f317SMiao Xie 	err = btrfs_auto_defrag_init();
261116cdcec7SMiao Xie 	if (err)
261216cdcec7SMiao Xie 		goto free_delayed_inode;
261316cdcec7SMiao Xie 
261478a6184aSMiao Xie 	err = btrfs_delayed_ref_init();
26159247f317SMiao Xie 	if (err)
26169247f317SMiao Xie 		goto free_auto_defrag;
26179247f317SMiao Xie 
2618b9e9a6cbSWang Shilong 	err = btrfs_prelim_ref_init();
2619b9e9a6cbSWang Shilong 	if (err)
2620af13b492SDavid Sterba 		goto free_delayed_ref;
2621b9e9a6cbSWang Shilong 
262297eb6b69SDavid Sterba 	err = btrfs_end_io_wq_init();
262378a6184aSMiao Xie 	if (err)
2624af13b492SDavid Sterba 		goto free_prelim_ref;
262578a6184aSMiao Xie 
262697eb6b69SDavid Sterba 	err = btrfs_interface_init();
262797eb6b69SDavid Sterba 	if (err)
262897eb6b69SDavid Sterba 		goto free_end_io_wq;
262997eb6b69SDavid Sterba 
26308ae1af3cSAnand Jain 	btrfs_print_mod_info();
2631dc11dd5dSJosef Bacik 
2632dc11dd5dSJosef Bacik 	err = btrfs_run_sanity_tests();
2633dc11dd5dSJosef Bacik 	if (err)
2634dc11dd5dSJosef Bacik 		goto unregister_ioctl;
2635dc11dd5dSJosef Bacik 
2636dc11dd5dSJosef Bacik 	err = register_filesystem(&btrfs_fs_type);
2637dc11dd5dSJosef Bacik 	if (err)
2638dc11dd5dSJosef Bacik 		goto unregister_ioctl;
263974255aa0SJosef Bacik 
26402f4cbe64SWyatt Banks 	return 0;
26412f4cbe64SWyatt Banks 
2642a9218f6bSChris Mason unregister_ioctl:
2643a9218f6bSChris Mason 	btrfs_interface_exit();
264497eb6b69SDavid Sterba free_end_io_wq:
264597eb6b69SDavid Sterba 	btrfs_end_io_wq_exit();
2646b9e9a6cbSWang Shilong free_prelim_ref:
2647b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
264878a6184aSMiao Xie free_delayed_ref:
264978a6184aSMiao Xie 	btrfs_delayed_ref_exit();
26509247f317SMiao Xie free_auto_defrag:
26519247f317SMiao Xie 	btrfs_auto_defrag_exit();
265216cdcec7SMiao Xie free_delayed_inode:
265316cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
26546352b91dSMiao Xie free_ordered_data:
26556352b91dSMiao Xie 	ordered_data_exit();
26562f4cbe64SWyatt Banks free_extent_map:
26572f4cbe64SWyatt Banks 	extent_map_exit();
26586f0d04f8SJosef Bacik free_extent_state_cache:
26596f0d04f8SJosef Bacik 	extent_state_cache_exit();
2660d1310b2eSChris Mason free_extent_io:
2661d1310b2eSChris Mason 	extent_io_exit();
26622f4cbe64SWyatt Banks free_cachep:
26632f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
2664261507a0SLi Zefan free_compress:
2665261507a0SLi Zefan 	btrfs_exit_compress();
26662f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
26679678c543SNikolay Borisov 
26682c90e5d6SChris Mason 	return err;
26692e635a27SChris Mason }
26702e635a27SChris Mason 
26712e635a27SChris Mason static void __exit exit_btrfs_fs(void)
26722e635a27SChris Mason {
267339279cc3SChris Mason 	btrfs_destroy_cachep();
267478a6184aSMiao Xie 	btrfs_delayed_ref_exit();
26759247f317SMiao Xie 	btrfs_auto_defrag_exit();
267616cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
2677b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
26786352b91dSMiao Xie 	ordered_data_exit();
2679a52d9a80SChris Mason 	extent_map_exit();
26806f0d04f8SJosef Bacik 	extent_state_cache_exit();
2681d1310b2eSChris Mason 	extent_io_exit();
2682a9218f6bSChris Mason 	btrfs_interface_exit();
26835ed5f588SJosef Bacik 	btrfs_end_io_wq_exit();
26842e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
268558176a96SJosef Bacik 	btrfs_exit_sysfs();
26868a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
2687261507a0SLi Zefan 	btrfs_exit_compress();
26882e635a27SChris Mason }
26892e635a27SChris Mason 
269060efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs);
26912e635a27SChris Mason module_exit(exit_btrfs_fs)
26922e635a27SChris Mason 
26932e635a27SChris Mason MODULE_LICENSE("GPL");
2694d5178578SJohannes Thumshirn MODULE_SOFTDEP("pre: crc32c");
26953951e7f0SJohannes Thumshirn MODULE_SOFTDEP("pre: xxhash64");
26963831bf00SJohannes Thumshirn MODULE_SOFTDEP("pre: sha256");
2697352ae07bSDavid Sterba MODULE_SOFTDEP("pre: blake2b-256");
2698