xref: /openbmc/linux/fs/btrfs/super.c (revision 16cab91a)
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:
86470f6d82eSOmar Sandoval 			if (token == Opt_space_cache ||
86570f6d82eSOmar Sandoval 			    strcmp(args[0].from, "v1") == 0) {
8660b246afaSJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
86770f6d82eSOmar Sandoval 						FREE_SPACE_TREE);
8683cdde224SJeff Mahoney 				btrfs_set_and_info(info, SPACE_CACHE,
86907802534SQu Wenruo 					   "enabling disk space caching");
87070f6d82eSOmar Sandoval 			} else if (strcmp(args[0].from, "v2") == 0) {
8710b246afaSJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
87270f6d82eSOmar Sandoval 						SPACE_CACHE);
8730b246afaSJeff Mahoney 				btrfs_set_and_info(info, FREE_SPACE_TREE,
87470f6d82eSOmar Sandoval 						   "enabling free space tree");
87570f6d82eSOmar Sandoval 			} else {
87670f6d82eSOmar Sandoval 				ret = -EINVAL;
87770f6d82eSOmar Sandoval 				goto out;
87870f6d82eSOmar Sandoval 			}
8790de90876SJosef Bacik 			break;
880f420ee1eSStefan Behrens 		case Opt_rescan_uuid_tree:
881f420ee1eSStefan Behrens 			btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
882f420ee1eSStefan Behrens 			break;
88373bc1876SJosef Bacik 		case Opt_no_space_cache:
8843cdde224SJeff Mahoney 			if (btrfs_test_opt(info, SPACE_CACHE)) {
8850b246afaSJeff Mahoney 				btrfs_clear_and_info(info, SPACE_CACHE,
88607802534SQu Wenruo 					     "disabling disk space caching");
88770f6d82eSOmar Sandoval 			}
8883cdde224SJeff Mahoney 			if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
8890b246afaSJeff Mahoney 				btrfs_clear_and_info(info, FREE_SPACE_TREE,
89070f6d82eSOmar Sandoval 					     "disabling free space tree");
89170f6d82eSOmar Sandoval 			}
89273bc1876SJosef Bacik 			break;
8934b9465cbSChris Mason 		case Opt_inode_cache:
8943818aea2SQu Wenruo 		case Opt_noinode_cache:
8955297199aSNikolay Borisov 			btrfs_warn(info,
8965297199aSNikolay Borisov 	"the 'inode_cache' option is deprecated and has no effect since 5.11");
8974b9465cbSChris Mason 			break;
89888c2ba3bSJosef Bacik 		case Opt_clear_cache:
8993cdde224SJeff Mahoney 			btrfs_set_and_info(info, CLEAR_CACHE,
90007802534SQu Wenruo 					   "force clearing of disk cache");
9010af3d00bSJosef Bacik 			break;
9024260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
9034260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
9044260f7c7SSage Weil 			break;
90591435650SChris Mason 		case Opt_enospc_debug:
90691435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
90791435650SChris Mason 			break;
90853036293SQu Wenruo 		case Opt_noenospc_debug:
90953036293SQu Wenruo 			btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
91053036293SQu Wenruo 			break;
9114cb5300bSChris Mason 		case Opt_defrag:
9123cdde224SJeff Mahoney 			btrfs_set_and_info(info, AUTO_DEFRAG,
91307802534SQu Wenruo 					   "enabling auto defrag");
9144cb5300bSChris Mason 			break;
915fc0ca9afSQu Wenruo 		case Opt_nodefrag:
9163cdde224SJeff Mahoney 			btrfs_clear_and_info(info, AUTO_DEFRAG,
91707802534SQu Wenruo 					     "disabling auto defrag");
918fc0ca9afSQu Wenruo 			break;
919af31f5e5SChris Mason 		case Opt_recovery:
9208dcddfa0SQu Wenruo 		case Opt_usebackuproot:
92174ef0018SQu Wenruo 			btrfs_warn(info,
92274ef0018SQu Wenruo 			"'%s' is deprecated, use 'rescue=usebackuproot' instead",
92374ef0018SQu Wenruo 				   token == Opt_recovery ? "recovery" :
92474ef0018SQu Wenruo 				   "usebackuproot");
9250b246afaSJeff Mahoney 			btrfs_info(info,
9268dcddfa0SQu Wenruo 				   "trying to use backup root at mount time");
9278dcddfa0SQu Wenruo 			btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
928af31f5e5SChris Mason 			break;
9299555c6c1SIlya Dryomov 		case Opt_skip_balance:
9309555c6c1SIlya Dryomov 			btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
9319555c6c1SIlya Dryomov 			break;
93221adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
93321adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
9340b246afaSJeff Mahoney 			btrfs_info(info,
935efe120a0SFrank Holton 				   "enabling check integrity including extent data");
936cbeaae4fSDavid Sterba 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY_DATA);
93721adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
93821adbd5cSStefan Behrens 			break;
93921adbd5cSStefan Behrens 		case Opt_check_integrity:
9400b246afaSJeff Mahoney 			btrfs_info(info, "enabling check integrity");
94121adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
94221adbd5cSStefan Behrens 			break;
94321adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
9442c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
94502453bdeSAnand Jain 			if (ret)
9462c334e87SWang Shilong 				goto out;
94721adbd5cSStefan Behrens 			info->check_integrity_print_mask = intarg;
94802453bdeSAnand Jain 			btrfs_info(info, "check_integrity_print_mask 0x%x",
94921adbd5cSStefan Behrens 				   info->check_integrity_print_mask);
95021adbd5cSStefan Behrens 			break;
95121adbd5cSStefan Behrens #else
95221adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
95321adbd5cSStefan Behrens 		case Opt_check_integrity:
95421adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
9550b246afaSJeff Mahoney 			btrfs_err(info,
956efe120a0SFrank Holton 				  "support for check_integrity* not compiled in!");
95721adbd5cSStefan Behrens 			ret = -EINVAL;
95821adbd5cSStefan Behrens 			goto out;
95921adbd5cSStefan Behrens #endif
9608c342930SJeff Mahoney 		case Opt_fatal_errors:
9618c342930SJeff Mahoney 			if (strcmp(args[0].from, "panic") == 0)
9628c342930SJeff Mahoney 				btrfs_set_opt(info->mount_opt,
9638c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
9648c342930SJeff Mahoney 			else if (strcmp(args[0].from, "bug") == 0)
9658c342930SJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
9668c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
9678c342930SJeff Mahoney 			else {
9688c342930SJeff Mahoney 				ret = -EINVAL;
9698c342930SJeff Mahoney 				goto out;
9708c342930SJeff Mahoney 			}
9718c342930SJeff Mahoney 			break;
9728b87dc17SDavid Sterba 		case Opt_commit_interval:
9738b87dc17SDavid Sterba 			intarg = 0;
9748b87dc17SDavid Sterba 			ret = match_int(&args[0], &intarg);
975d3740608SAnand Jain 			if (ret)
9768b87dc17SDavid Sterba 				goto out;
977d3740608SAnand Jain 			if (intarg == 0) {
978d3740608SAnand Jain 				btrfs_info(info,
979d3740608SAnand Jain 					   "using default commit interval %us",
980d3740608SAnand Jain 					   BTRFS_DEFAULT_COMMIT_INTERVAL);
981d3740608SAnand Jain 				intarg = BTRFS_DEFAULT_COMMIT_INTERVAL;
982d3740608SAnand Jain 			} else if (intarg > 300) {
983d3740608SAnand Jain 				btrfs_warn(info, "excessive commit interval %d",
9848b87dc17SDavid Sterba 					   intarg);
9858b87dc17SDavid Sterba 			}
9868b87dc17SDavid Sterba 			info->commit_interval = intarg;
9878b87dc17SDavid Sterba 			break;
98874ef0018SQu Wenruo 		case Opt_rescue:
98974ef0018SQu Wenruo 			ret = parse_rescue_options(info, args[0].from);
99074ef0018SQu Wenruo 			if (ret < 0)
99174ef0018SQu Wenruo 				goto out;
99274ef0018SQu Wenruo 			break;
993d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
994d0bd4560SJosef Bacik 		case Opt_fragment_all:
9950b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting all space");
996d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
997d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
998d0bd4560SJosef Bacik 			break;
999d0bd4560SJosef Bacik 		case Opt_fragment_metadata:
10000b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting metadata");
1001d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt,
1002d0bd4560SJosef Bacik 				      FRAGMENT_METADATA);
1003d0bd4560SJosef Bacik 			break;
1004d0bd4560SJosef Bacik 		case Opt_fragment_data:
10050b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting data");
1006d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
1007d0bd4560SJosef Bacik 			break;
1008d0bd4560SJosef Bacik #endif
1009fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
1010fb592373SJosef Bacik 		case Opt_ref_verify:
1011fb592373SJosef Bacik 			btrfs_info(info, "doing ref verification");
1012fb592373SJosef Bacik 			btrfs_set_opt(info->mount_opt, REF_VERIFY);
1013fb592373SJosef Bacik 			break;
1014fb592373SJosef Bacik #endif
1015a7a3f7caSSage Weil 		case Opt_err:
10167e8f19e5SDavid Sterba 			btrfs_err(info, "unrecognized mount option '%s'", p);
1017a7a3f7caSSage Weil 			ret = -EINVAL;
1018a7a3f7caSSage Weil 			goto out;
101995e05289SChris Mason 		default:
1020be20aa9dSChris Mason 			break;
102195e05289SChris Mason 		}
102295e05289SChris Mason 	}
102396da0919SQu Wenruo check:
1024d70bf748SJosef Bacik 	/* We're read-only, don't have to check. */
1025d70bf748SJosef Bacik 	if (new_flags & SB_RDONLY)
1026d70bf748SJosef Bacik 		goto out;
1027d70bf748SJosef Bacik 
102842437a63SJosef Bacik 	if (check_ro_option(info, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||
1029882dbe0cSJosef Bacik 	    check_ro_option(info, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
1030882dbe0cSJosef Bacik 	    check_ro_option(info, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums"))
103196da0919SQu Wenruo 		ret = -EINVAL;
1032a7a3f7caSSage Weil out:
10330b246afaSJeff Mahoney 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
10343cdde224SJeff Mahoney 	    !btrfs_test_opt(info, FREE_SPACE_TREE) &&
10353cdde224SJeff Mahoney 	    !btrfs_test_opt(info, CLEAR_CACHE)) {
10360b246afaSJeff Mahoney 		btrfs_err(info, "cannot disable free space tree");
103770f6d82eSOmar Sandoval 		ret = -EINVAL;
103870f6d82eSOmar Sandoval 
103970f6d82eSOmar Sandoval 	}
10405d1ab66cSNaohiro Aota 	if (!ret)
10415d1ab66cSNaohiro Aota 		ret = btrfs_check_mountopts_zoned(info);
10423cdde224SJeff Mahoney 	if (!ret && btrfs_test_opt(info, SPACE_CACHE))
10430b246afaSJeff Mahoney 		btrfs_info(info, "disk space caching is enabled");
10443cdde224SJeff Mahoney 	if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE))
10450b246afaSJeff Mahoney 		btrfs_info(info, "using free space tree");
1046a7a3f7caSSage Weil 	return ret;
1047edf24abeSChristoph Hellwig }
1048edf24abeSChristoph Hellwig 
1049edf24abeSChristoph Hellwig /*
1050edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
1051edf24abeSChristoph Hellwig  *
1052edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
1053edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
1054edf24abeSChristoph Hellwig  */
1055fa59f27cSAnand Jain static int btrfs_parse_device_options(const char *options, fmode_t flags,
1056d64dcbd1SGu Jinxiang 				      void *holder)
1057edf24abeSChristoph Hellwig {
1058edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
105983c8c9bdSJeff Liu 	char *device_name, *opts, *orig, *p;
106036350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
1061d7407606SMisono, Tomohiro 	int error = 0;
1062d7407606SMisono, Tomohiro 
10635139cff5SDavid Sterba 	lockdep_assert_held(&uuid_mutex);
10645139cff5SDavid Sterba 
1065d7407606SMisono, Tomohiro 	if (!options)
1066d7407606SMisono, Tomohiro 		return 0;
1067d7407606SMisono, Tomohiro 
1068d7407606SMisono, Tomohiro 	/*
1069d7407606SMisono, Tomohiro 	 * strsep changes the string, duplicate it because btrfs_parse_options
1070d7407606SMisono, Tomohiro 	 * gets called later
1071d7407606SMisono, Tomohiro 	 */
1072d7407606SMisono, Tomohiro 	opts = kstrdup(options, GFP_KERNEL);
1073d7407606SMisono, Tomohiro 	if (!opts)
1074d7407606SMisono, Tomohiro 		return -ENOMEM;
1075d7407606SMisono, Tomohiro 	orig = opts;
1076d7407606SMisono, Tomohiro 
1077d7407606SMisono, Tomohiro 	while ((p = strsep(&opts, ",")) != NULL) {
1078d7407606SMisono, Tomohiro 		int token;
1079d7407606SMisono, Tomohiro 
1080d7407606SMisono, Tomohiro 		if (!*p)
1081d7407606SMisono, Tomohiro 			continue;
1082d7407606SMisono, Tomohiro 
1083d7407606SMisono, Tomohiro 		token = match_token(p, tokens, args);
1084d7407606SMisono, Tomohiro 		if (token == Opt_device) {
1085d7407606SMisono, Tomohiro 			device_name = match_strdup(&args[0]);
1086d7407606SMisono, Tomohiro 			if (!device_name) {
1087d7407606SMisono, Tomohiro 				error = -ENOMEM;
1088d7407606SMisono, Tomohiro 				goto out;
1089d7407606SMisono, Tomohiro 			}
109036350e95SGu Jinxiang 			device = btrfs_scan_one_device(device_name, flags,
109136350e95SGu Jinxiang 					holder);
1092d7407606SMisono, Tomohiro 			kfree(device_name);
109336350e95SGu Jinxiang 			if (IS_ERR(device)) {
109436350e95SGu Jinxiang 				error = PTR_ERR(device);
1095d7407606SMisono, Tomohiro 				goto out;
1096d7407606SMisono, Tomohiro 			}
1097d7407606SMisono, Tomohiro 		}
109836350e95SGu Jinxiang 	}
1099d7407606SMisono, Tomohiro 
1100d7407606SMisono, Tomohiro out:
1101d7407606SMisono, Tomohiro 	kfree(orig);
1102d7407606SMisono, Tomohiro 	return error;
1103d7407606SMisono, Tomohiro }
1104d7407606SMisono, Tomohiro 
1105d7407606SMisono, Tomohiro /*
1106d7407606SMisono, Tomohiro  * Parse mount options that are related to subvolume id
1107d7407606SMisono, Tomohiro  *
1108d7407606SMisono, Tomohiro  * The value is later passed to mount_subvol()
1109d7407606SMisono, Tomohiro  */
111093b9bcdfSGu Jinxiang static int btrfs_parse_subvol_options(const char *options, char **subvol_name,
111193b9bcdfSGu Jinxiang 		u64 *subvol_objectid)
1112d7407606SMisono, Tomohiro {
1113d7407606SMisono, Tomohiro 	substring_t args[MAX_OPT_ARGS];
1114d7407606SMisono, Tomohiro 	char *opts, *orig, *p;
1115edf24abeSChristoph Hellwig 	int error = 0;
1116ccb0e7d1SAnand Jain 	u64 subvolid;
1117edf24abeSChristoph Hellwig 
1118edf24abeSChristoph Hellwig 	if (!options)
1119830c4adbSJosef Bacik 		return 0;
1120edf24abeSChristoph Hellwig 
1121edf24abeSChristoph Hellwig 	/*
1122d7407606SMisono, Tomohiro 	 * strsep changes the string, duplicate it because
1123fa59f27cSAnand Jain 	 * btrfs_parse_device_options gets called later
1124edf24abeSChristoph Hellwig 	 */
1125edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
1126edf24abeSChristoph Hellwig 	if (!opts)
1127edf24abeSChristoph Hellwig 		return -ENOMEM;
11283f3d0bc0STero Roponen 	orig = opts;
1129edf24abeSChristoph Hellwig 
1130edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
1131edf24abeSChristoph Hellwig 		int token;
1132edf24abeSChristoph Hellwig 		if (!*p)
1133edf24abeSChristoph Hellwig 			continue;
1134edf24abeSChristoph Hellwig 
1135edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
1136edf24abeSChristoph Hellwig 		switch (token) {
1137edf24abeSChristoph Hellwig 		case Opt_subvol:
1138a90e8b6fSIlya Dryomov 			kfree(*subvol_name);
1139edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
11402c334e87SWang Shilong 			if (!*subvol_name) {
11412c334e87SWang Shilong 				error = -ENOMEM;
11422c334e87SWang Shilong 				goto out;
11432c334e87SWang Shilong 			}
1144edf24abeSChristoph Hellwig 			break;
114573f73415SJosef Bacik 		case Opt_subvolid:
1146ccb0e7d1SAnand Jain 			error = match_u64(&args[0], &subvolid);
1147ccb0e7d1SAnand Jain 			if (error)
11482c334e87SWang Shilong 				goto out;
1149ccb0e7d1SAnand Jain 
1150ccb0e7d1SAnand Jain 			/* we want the original fs_tree */
1151ccb0e7d1SAnand Jain 			if (subvolid == 0)
1152ccb0e7d1SAnand Jain 				subvolid = BTRFS_FS_TREE_OBJECTID;
1153ccb0e7d1SAnand Jain 
1154ccb0e7d1SAnand Jain 			*subvol_objectid = subvolid;
115573f73415SJosef Bacik 			break;
1156edf24abeSChristoph Hellwig 		default:
1157edf24abeSChristoph Hellwig 			break;
1158edf24abeSChristoph Hellwig 		}
1159edf24abeSChristoph Hellwig 	}
1160edf24abeSChristoph Hellwig 
1161edf24abeSChristoph Hellwig out:
1162830c4adbSJosef Bacik 	kfree(orig);
1163edf24abeSChristoph Hellwig 	return error;
116495e05289SChris Mason }
116595e05289SChris Mason 
1166c0c907a4SMarcos Paulo de Souza char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
116773f73415SJosef Bacik 					  u64 subvol_objectid)
116873f73415SJosef Bacik {
1169815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
11705168489aSJosef Bacik 	struct btrfs_root *fs_root = NULL;
117105dbe683SOmar Sandoval 	struct btrfs_root_ref *root_ref;
117205dbe683SOmar Sandoval 	struct btrfs_inode_ref *inode_ref;
117305dbe683SOmar Sandoval 	struct btrfs_key key;
117405dbe683SOmar Sandoval 	struct btrfs_path *path = NULL;
117505dbe683SOmar Sandoval 	char *name = NULL, *ptr;
117605dbe683SOmar Sandoval 	u64 dirid;
117705dbe683SOmar Sandoval 	int len;
117805dbe683SOmar Sandoval 	int ret;
117905dbe683SOmar Sandoval 
118005dbe683SOmar Sandoval 	path = btrfs_alloc_path();
118105dbe683SOmar Sandoval 	if (!path) {
118205dbe683SOmar Sandoval 		ret = -ENOMEM;
118305dbe683SOmar Sandoval 		goto err;
118405dbe683SOmar Sandoval 	}
118505dbe683SOmar Sandoval 
11863ec83621SDavid Sterba 	name = kmalloc(PATH_MAX, GFP_KERNEL);
118705dbe683SOmar Sandoval 	if (!name) {
118805dbe683SOmar Sandoval 		ret = -ENOMEM;
118905dbe683SOmar Sandoval 		goto err;
119005dbe683SOmar Sandoval 	}
119105dbe683SOmar Sandoval 	ptr = name + PATH_MAX - 1;
119205dbe683SOmar Sandoval 	ptr[0] = '\0';
119305dbe683SOmar Sandoval 
119405dbe683SOmar Sandoval 	/*
119505dbe683SOmar Sandoval 	 * Walk up the subvolume trees in the tree of tree roots by root
119605dbe683SOmar Sandoval 	 * backrefs until we hit the top-level subvolume.
119705dbe683SOmar Sandoval 	 */
119805dbe683SOmar Sandoval 	while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
119905dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
120005dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_BACKREF_KEY;
120105dbe683SOmar Sandoval 		key.offset = (u64)-1;
120205dbe683SOmar Sandoval 
12030ff40a91SMarcos Paulo de Souza 		ret = btrfs_search_backwards(root, &key, path);
120405dbe683SOmar Sandoval 		if (ret < 0) {
120505dbe683SOmar Sandoval 			goto err;
120605dbe683SOmar Sandoval 		} else if (ret > 0) {
120705dbe683SOmar Sandoval 			ret = -ENOENT;
120805dbe683SOmar Sandoval 			goto err;
120905dbe683SOmar Sandoval 		}
121005dbe683SOmar Sandoval 
121105dbe683SOmar Sandoval 		subvol_objectid = key.offset;
121205dbe683SOmar Sandoval 
121305dbe683SOmar Sandoval 		root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
121405dbe683SOmar Sandoval 					  struct btrfs_root_ref);
121505dbe683SOmar Sandoval 		len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
121605dbe683SOmar Sandoval 		ptr -= len + 1;
121705dbe683SOmar Sandoval 		if (ptr < name) {
121805dbe683SOmar Sandoval 			ret = -ENAMETOOLONG;
121905dbe683SOmar Sandoval 			goto err;
122005dbe683SOmar Sandoval 		}
122105dbe683SOmar Sandoval 		read_extent_buffer(path->nodes[0], ptr + 1,
122205dbe683SOmar Sandoval 				   (unsigned long)(root_ref + 1), len);
122305dbe683SOmar Sandoval 		ptr[0] = '/';
122405dbe683SOmar Sandoval 		dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
122505dbe683SOmar Sandoval 		btrfs_release_path(path);
122605dbe683SOmar Sandoval 
122756e9357aSDavid Sterba 		fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true);
122805dbe683SOmar Sandoval 		if (IS_ERR(fs_root)) {
122905dbe683SOmar Sandoval 			ret = PTR_ERR(fs_root);
12305168489aSJosef Bacik 			fs_root = NULL;
12315168489aSJosef Bacik 			goto err;
12325168489aSJosef Bacik 		}
123305dbe683SOmar Sandoval 
123405dbe683SOmar Sandoval 		/*
123505dbe683SOmar Sandoval 		 * Walk up the filesystem tree by inode refs until we hit the
123605dbe683SOmar Sandoval 		 * root directory.
123705dbe683SOmar Sandoval 		 */
123805dbe683SOmar Sandoval 		while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
123905dbe683SOmar Sandoval 			key.objectid = dirid;
124005dbe683SOmar Sandoval 			key.type = BTRFS_INODE_REF_KEY;
124105dbe683SOmar Sandoval 			key.offset = (u64)-1;
124205dbe683SOmar Sandoval 
12430ff40a91SMarcos Paulo de Souza 			ret = btrfs_search_backwards(fs_root, &key, path);
124405dbe683SOmar Sandoval 			if (ret < 0) {
124505dbe683SOmar Sandoval 				goto err;
124605dbe683SOmar Sandoval 			} else if (ret > 0) {
124705dbe683SOmar Sandoval 				ret = -ENOENT;
124805dbe683SOmar Sandoval 				goto err;
124905dbe683SOmar Sandoval 			}
125005dbe683SOmar Sandoval 
125105dbe683SOmar Sandoval 			dirid = key.offset;
125205dbe683SOmar Sandoval 
125305dbe683SOmar Sandoval 			inode_ref = btrfs_item_ptr(path->nodes[0],
125405dbe683SOmar Sandoval 						   path->slots[0],
125505dbe683SOmar Sandoval 						   struct btrfs_inode_ref);
125605dbe683SOmar Sandoval 			len = btrfs_inode_ref_name_len(path->nodes[0],
125705dbe683SOmar Sandoval 						       inode_ref);
125805dbe683SOmar Sandoval 			ptr -= len + 1;
125905dbe683SOmar Sandoval 			if (ptr < name) {
126005dbe683SOmar Sandoval 				ret = -ENAMETOOLONG;
126105dbe683SOmar Sandoval 				goto err;
126205dbe683SOmar Sandoval 			}
126305dbe683SOmar Sandoval 			read_extent_buffer(path->nodes[0], ptr + 1,
126405dbe683SOmar Sandoval 					   (unsigned long)(inode_ref + 1), len);
126505dbe683SOmar Sandoval 			ptr[0] = '/';
126605dbe683SOmar Sandoval 			btrfs_release_path(path);
126705dbe683SOmar Sandoval 		}
126800246528SJosef Bacik 		btrfs_put_root(fs_root);
12695168489aSJosef Bacik 		fs_root = NULL;
127005dbe683SOmar Sandoval 	}
127105dbe683SOmar Sandoval 
127205dbe683SOmar Sandoval 	btrfs_free_path(path);
127305dbe683SOmar Sandoval 	if (ptr == name + PATH_MAX - 1) {
127405dbe683SOmar Sandoval 		name[0] = '/';
127505dbe683SOmar Sandoval 		name[1] = '\0';
127605dbe683SOmar Sandoval 	} else {
127705dbe683SOmar Sandoval 		memmove(name, ptr, name + PATH_MAX - ptr);
127805dbe683SOmar Sandoval 	}
127905dbe683SOmar Sandoval 	return name;
128005dbe683SOmar Sandoval 
128105dbe683SOmar Sandoval err:
128200246528SJosef Bacik 	btrfs_put_root(fs_root);
128305dbe683SOmar Sandoval 	btrfs_free_path(path);
128405dbe683SOmar Sandoval 	kfree(name);
128505dbe683SOmar Sandoval 	return ERR_PTR(ret);
128605dbe683SOmar Sandoval }
128705dbe683SOmar Sandoval 
128805dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
128905dbe683SOmar Sandoval {
129005dbe683SOmar Sandoval 	struct btrfs_root *root = fs_info->tree_root;
129173f73415SJosef Bacik 	struct btrfs_dir_item *di;
129273f73415SJosef Bacik 	struct btrfs_path *path;
129373f73415SJosef Bacik 	struct btrfs_key location;
129473f73415SJosef Bacik 	u64 dir_id;
129573f73415SJosef Bacik 
129673f73415SJosef Bacik 	path = btrfs_alloc_path();
129773f73415SJosef Bacik 	if (!path)
129805dbe683SOmar Sandoval 		return -ENOMEM;
129973f73415SJosef Bacik 
130073f73415SJosef Bacik 	/*
130173f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
130273f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
130373f73415SJosef Bacik 	 * to mount.
130473f73415SJosef Bacik 	 */
1305815745cfSAl Viro 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
130673f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
1307b0839166SJulia Lawall 	if (IS_ERR(di)) {
1308b0839166SJulia Lawall 		btrfs_free_path(path);
130905dbe683SOmar Sandoval 		return PTR_ERR(di);
1310b0839166SJulia Lawall 	}
131173f73415SJosef Bacik 	if (!di) {
131273f73415SJosef Bacik 		/*
131373f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
131473f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
131505dbe683SOmar Sandoval 		 * mount the top-level subvolume.
131673f73415SJosef Bacik 		 */
131773f73415SJosef Bacik 		btrfs_free_path(path);
131805dbe683SOmar Sandoval 		*objectid = BTRFS_FS_TREE_OBJECTID;
131905dbe683SOmar Sandoval 		return 0;
132073f73415SJosef Bacik 	}
132173f73415SJosef Bacik 
132273f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
132373f73415SJosef Bacik 	btrfs_free_path(path);
132405dbe683SOmar Sandoval 	*objectid = location.objectid;
132505dbe683SOmar Sandoval 	return 0;
132673f73415SJosef Bacik }
132773f73415SJosef Bacik 
13288a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
13298a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
133056e033a7SDavid Sterba 			    void *data)
13312e635a27SChris Mason {
13322e635a27SChris Mason 	struct inode *inode;
1333815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
133439279cc3SChris Mason 	int err;
13352e635a27SChris Mason 
13362e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
13372e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
1338e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
1339af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
1340be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
134114605409SBoris Burkov #ifdef CONFIG_FS_VERITY
134214605409SBoris Burkov 	sb->s_vop = &btrfs_verityops;
134314605409SBoris Burkov #endif
13445103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
13452e635a27SChris Mason 	sb->s_time_gran = 1;
13460eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
13471751e8a6SLinus Torvalds 	sb->s_flags |= SB_POSIXACL;
134849cf6f45SChris Ball #endif
1349357fdad0SMatthew Garrett 	sb->s_flags |= SB_I_VERSION;
1350da2f0f74SChris Mason 	sb->s_iflags |= SB_I_CGROUPWB;
13519e11ceeeSJan Kara 
13529e11ceeeSJan Kara 	err = super_setup_bdi(sb);
13539e11ceeeSJan Kara 	if (err) {
13549e11ceeeSJan Kara 		btrfs_err(fs_info, "super_setup_bdi failed");
13559e11ceeeSJan Kara 		return err;
13569e11ceeeSJan Kara 	}
13579e11ceeeSJan Kara 
1358ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
1359ad2b2c80SAl Viro 	if (err) {
1360ab8d0fc4SJeff Mahoney 		btrfs_err(fs_info, "open_ctree failed");
1361ad2b2c80SAl Viro 		return err;
1362e20d96d6SChris Mason 	}
1363b888db2bSChris Mason 
13640202e83fSDavid Sterba 	inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
13655d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
13665d4f98a2SYan Zheng 		err = PTR_ERR(inode);
136739279cc3SChris Mason 		goto fail_close;
136839279cc3SChris Mason 	}
13692e635a27SChris Mason 
137048fde701SAl Viro 	sb->s_root = d_make_root(inode);
137148fde701SAl Viro 	if (!sb->s_root) {
137239279cc3SChris Mason 		err = -ENOMEM;
137339279cc3SChris Mason 		goto fail_close;
13742e635a27SChris Mason 	}
137558176a96SJosef Bacik 
13761751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
13772e635a27SChris Mason 	return 0;
13782e635a27SChris Mason 
137939279cc3SChris Mason fail_close:
13806bccf3abSJeff Mahoney 	close_ctree(fs_info);
1381d5719762SChris Mason 	return err;
1382d5719762SChris Mason }
1383d5719762SChris Mason 
13846bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
1385d5719762SChris Mason {
1386d5719762SChris Mason 	struct btrfs_trans_handle *trans;
1387815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1388815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
1389df2ce34cSChris Mason 
1390bc074524SJeff Mahoney 	trace_btrfs_sync_fs(fs_info, wait);
13911abe9b8aSliubo 
1392d561c025SChris Mason 	if (!wait) {
1393815745cfSAl Viro 		filemap_flush(fs_info->btree_inode->i_mapping);
1394df2ce34cSChris Mason 		return 0;
1395d561c025SChris Mason 	}
1396771ed689SChris Mason 
13976374e57aSChris Mason 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1398771ed689SChris Mason 
1399d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
140060376ce4SJosef Bacik 	if (IS_ERR(trans)) {
1401354aa0fbSMiao Xie 		/* no transaction, don't bother */
14026b5fe46dSDavid Sterba 		if (PTR_ERR(trans) == -ENOENT) {
14036b5fe46dSDavid Sterba 			/*
14046b5fe46dSDavid Sterba 			 * Exit unless we have some pending changes
14056b5fe46dSDavid Sterba 			 * that need to go through commit
14066b5fe46dSDavid Sterba 			 */
14076b5fe46dSDavid Sterba 			if (fs_info->pending_changes == 0)
1408bd7de2c9SJosef Bacik 				return 0;
1409a53f4f8eSQu Wenruo 			/*
1410a53f4f8eSQu Wenruo 			 * A non-blocking test if the fs is frozen. We must not
1411a53f4f8eSQu Wenruo 			 * start a new transaction here otherwise a deadlock
1412a53f4f8eSQu Wenruo 			 * happens. The pending operations are delayed to the
1413a53f4f8eSQu Wenruo 			 * next commit after thawing.
1414a53f4f8eSQu Wenruo 			 */
1415a7e3c5f2SRakesh Pandit 			if (sb_start_write_trylock(sb))
1416a7e3c5f2SRakesh Pandit 				sb_end_write(sb);
1417a53f4f8eSQu Wenruo 			else
1418a53f4f8eSQu Wenruo 				return 0;
14196b5fe46dSDavid Sterba 			trans = btrfs_start_transaction(root, 0);
142060376ce4SJosef Bacik 		}
142198bd5c54SDavid Sterba 		if (IS_ERR(trans))
142298bd5c54SDavid Sterba 			return PTR_ERR(trans);
14236b5fe46dSDavid Sterba 	}
14243a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
1425d5719762SChris Mason }
1426d5719762SChris Mason 
1427ab0b4a3eSJosef Bacik static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed)
1428ab0b4a3eSJosef Bacik {
1429ab0b4a3eSJosef Bacik 	seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s);
1430ab0b4a3eSJosef Bacik 	*printed = true;
1431ab0b4a3eSJosef Bacik }
1432ab0b4a3eSJosef Bacik 
143334c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1434a9572a15SEric Paris {
1435815745cfSAl Viro 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
14360f628c63SDavid Sterba 	const char *compress_type;
14373ef3959bSJosef Bacik 	const char *subvol_name;
1438ab0b4a3eSJosef Bacik 	bool printed = false;
1439a9572a15SEric Paris 
14403cdde224SJeff Mahoney 	if (btrfs_test_opt(info, DEGRADED))
1441a9572a15SEric Paris 		seq_puts(seq, ",degraded");
14423cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATASUM))
1443a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
14443cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATACOW))
1445a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
14463cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOBARRIER))
1447a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
144895ac567aSFilipe David Borba Manana 	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1449c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",max_inline=%llu", info->max_inline);
1450a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
1451a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
1452f7b885beSAnand Jain 		seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
14533cdde224SJeff Mahoney 	if (btrfs_test_opt(info, COMPRESS)) {
14540f628c63SDavid Sterba 		compress_type = btrfs_compress_type2str(info->compress_type);
14553cdde224SJeff Mahoney 		if (btrfs_test_opt(info, FORCE_COMPRESS))
1456200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
1457200da64eSTsutomu Itoh 		else
1458200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
1459f51d2b59SDavid Sterba 		if (info->compress_level)
1460fa4d885aSAdam Borowski 			seq_printf(seq, ":%d", info->compress_level);
1461200da64eSTsutomu Itoh 	}
14623cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOSSD))
1463c289811cSChris Mason 		seq_puts(seq, ",nossd");
14643cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SSD_SPREAD))
1465451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
14663cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, SSD))
1467a9572a15SEric Paris 		seq_puts(seq, ",ssd");
14683cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOTREELOG))
14696b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
14703cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOLOGREPLAY))
1471ab0b4a3eSJosef Bacik 		print_rescue_option(seq, "nologreplay", &printed);
147268319c18SJosef Bacik 	if (btrfs_test_opt(info, USEBACKUPROOT))
147368319c18SJosef Bacik 		print_rescue_option(seq, "usebackuproot", &printed);
147442437a63SJosef Bacik 	if (btrfs_test_opt(info, IGNOREBADROOTS))
147542437a63SJosef Bacik 		print_rescue_option(seq, "ignorebadroots", &printed);
1476882dbe0cSJosef Bacik 	if (btrfs_test_opt(info, IGNOREDATACSUMS))
1477882dbe0cSJosef Bacik 		print_rescue_option(seq, "ignoredatacsums", &printed);
14783cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FLUSHONCOMMIT))
14796b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
148046b27f50SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_SYNC))
148120a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
1482b0643e59SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_ASYNC))
1483b0643e59SDennis Zhou 		seq_puts(seq, ",discard=async");
14841751e8a6SLinus Torvalds 	if (!(info->sb->s_flags & SB_POSIXACL))
1485a9572a15SEric Paris 		seq_puts(seq, ",noacl");
148604c41559SBoris Burkov 	if (btrfs_free_space_cache_v1_active(info))
1487200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
148804c41559SBoris Burkov 	else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
148970f6d82eSOmar Sandoval 		seq_puts(seq, ",space_cache=v2");
149073bc1876SJosef Bacik 	else
14918965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
14923cdde224SJeff Mahoney 	if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1493f420ee1eSStefan Behrens 		seq_puts(seq, ",rescan_uuid_tree");
14943cdde224SJeff Mahoney 	if (btrfs_test_opt(info, CLEAR_CACHE))
1495200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
14963cdde224SJeff Mahoney 	if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1497200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
14983cdde224SJeff Mahoney 	if (btrfs_test_opt(info, ENOSPC_DEBUG))
14990942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
15003cdde224SJeff Mahoney 	if (btrfs_test_opt(info, AUTO_DEFRAG))
15010942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
15023cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SKIP_BALANCE))
15039555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
15048507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1505cbeaae4fSDavid Sterba 	if (btrfs_test_opt(info, CHECK_INTEGRITY_DATA))
15068507d216SWang Shilong 		seq_puts(seq, ",check_int_data");
15073cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, CHECK_INTEGRITY))
15088507d216SWang Shilong 		seq_puts(seq, ",check_int");
15098507d216SWang Shilong 	if (info->check_integrity_print_mask)
15108507d216SWang Shilong 		seq_printf(seq, ",check_int_print_mask=%d",
15118507d216SWang Shilong 				info->check_integrity_print_mask);
15128507d216SWang Shilong #endif
15138507d216SWang Shilong 	if (info->metadata_ratio)
1514764cb8b4SAnand Jain 		seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
15153cdde224SJeff Mahoney 	if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
15168c342930SJeff Mahoney 		seq_puts(seq, ",fatal_errors=panic");
15178b87dc17SDavid Sterba 	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1518d3740608SAnand Jain 		seq_printf(seq, ",commit=%u", info->commit_interval);
1519d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
15203cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_DATA))
1521d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=data");
15223cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_METADATA))
1523d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=metadata");
1524d0bd4560SJosef Bacik #endif
1525fb592373SJosef Bacik 	if (btrfs_test_opt(info, REF_VERIFY))
1526fb592373SJosef Bacik 		seq_puts(seq, ",ref_verify");
1527c8d3fe02SOmar Sandoval 	seq_printf(seq, ",subvolid=%llu",
1528c8d3fe02SOmar Sandoval 		  BTRFS_I(d_inode(dentry))->root->root_key.objectid);
15293ef3959bSJosef Bacik 	subvol_name = btrfs_get_subvol_name_from_objectid(info,
15303ef3959bSJosef Bacik 			BTRFS_I(d_inode(dentry))->root->root_key.objectid);
15313ef3959bSJosef Bacik 	if (!IS_ERR(subvol_name)) {
1532c8d3fe02SOmar Sandoval 		seq_puts(seq, ",subvol=");
15333ef3959bSJosef Bacik 		seq_escape(seq, subvol_name, " \t\n\\");
15343ef3959bSJosef Bacik 		kfree(subvol_name);
15353ef3959bSJosef Bacik 	}
1536a9572a15SEric Paris 	return 0;
1537a9572a15SEric Paris }
1538a9572a15SEric Paris 
1539a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
15402e635a27SChris Mason {
1541815745cfSAl Viro 	struct btrfs_fs_info *p = data;
1542815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(s);
15434b82d6e4SYan 
1544815745cfSAl Viro 	return fs_info->fs_devices == p->fs_devices;
15454b82d6e4SYan }
15464b82d6e4SYan 
1547450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
1548450ba0eaSJosef Bacik {
15496de1d09dSAl Viro 	int err = set_anon_super(s, data);
15506de1d09dSAl Viro 	if (!err)
1551450ba0eaSJosef Bacik 		s->s_fs_info = data;
15526de1d09dSAl Viro 	return err;
1553450ba0eaSJosef Bacik }
1554450ba0eaSJosef Bacik 
1555830c4adbSJosef Bacik /*
1556f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
1557f9d9ef62SDavid Sterba  */
1558f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
1559f9d9ef62SDavid Sterba {
1560f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1561f9d9ef62SDavid Sterba 		return 1;
1562f9d9ef62SDavid Sterba 	return 0;
1563f9d9ef62SDavid Sterba }
1564f9d9ef62SDavid Sterba 
1565bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1566ae0bc863SAnand Jain 				   struct vfsmount *mnt)
1567830c4adbSJosef Bacik {
1568830c4adbSJosef Bacik 	struct dentry *root;
1569fa330659SOmar Sandoval 	int ret;
1570830c4adbSJosef Bacik 
157105dbe683SOmar Sandoval 	if (!subvol_name) {
157205dbe683SOmar Sandoval 		if (!subvol_objectid) {
157305dbe683SOmar Sandoval 			ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
157405dbe683SOmar Sandoval 							  &subvol_objectid);
157505dbe683SOmar Sandoval 			if (ret) {
157605dbe683SOmar Sandoval 				root = ERR_PTR(ret);
157705dbe683SOmar Sandoval 				goto out;
157805dbe683SOmar Sandoval 			}
157905dbe683SOmar Sandoval 		}
1580c0c907a4SMarcos Paulo de Souza 		subvol_name = btrfs_get_subvol_name_from_objectid(
1581c0c907a4SMarcos Paulo de Souza 					btrfs_sb(mnt->mnt_sb), subvol_objectid);
158205dbe683SOmar Sandoval 		if (IS_ERR(subvol_name)) {
158305dbe683SOmar Sandoval 			root = ERR_CAST(subvol_name);
158405dbe683SOmar Sandoval 			subvol_name = NULL;
158505dbe683SOmar Sandoval 			goto out;
158605dbe683SOmar Sandoval 		}
158705dbe683SOmar Sandoval 
158805dbe683SOmar Sandoval 	}
158905dbe683SOmar Sandoval 
1590ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1591fa330659SOmar Sandoval 	/* mount_subtree() drops our reference on the vfsmount. */
1592fa330659SOmar Sandoval 	mnt = NULL;
1593830c4adbSJosef Bacik 
1594bb289b7bSOmar Sandoval 	if (!IS_ERR(root)) {
1595ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1596ab8d0fc4SJeff Mahoney 		struct btrfs_fs_info *fs_info = btrfs_sb(s);
1597bb289b7bSOmar Sandoval 		struct inode *root_inode = d_inode(root);
1598bb289b7bSOmar Sandoval 		u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1599bb289b7bSOmar Sandoval 
1600bb289b7bSOmar Sandoval 		ret = 0;
1601bb289b7bSOmar Sandoval 		if (!is_subvolume_inode(root_inode)) {
1602ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info, "'%s' is not a valid subvolume",
1603bb289b7bSOmar Sandoval 			       subvol_name);
1604bb289b7bSOmar Sandoval 			ret = -EINVAL;
1605bb289b7bSOmar Sandoval 		}
1606bb289b7bSOmar Sandoval 		if (subvol_objectid && root_objectid != subvol_objectid) {
160705dbe683SOmar Sandoval 			/*
160805dbe683SOmar Sandoval 			 * This will also catch a race condition where a
160905dbe683SOmar Sandoval 			 * subvolume which was passed by ID is renamed and
161005dbe683SOmar Sandoval 			 * another subvolume is renamed over the old location.
161105dbe683SOmar Sandoval 			 */
1612ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info,
1613ab8d0fc4SJeff Mahoney 				  "subvol '%s' does not match subvolid %llu",
1614bb289b7bSOmar Sandoval 				  subvol_name, subvol_objectid);
1615bb289b7bSOmar Sandoval 			ret = -EINVAL;
1616bb289b7bSOmar Sandoval 		}
1617bb289b7bSOmar Sandoval 		if (ret) {
1618ea441d11SAl Viro 			dput(root);
1619bb289b7bSOmar Sandoval 			root = ERR_PTR(ret);
1620ea441d11SAl Viro 			deactivate_locked_super(s);
1621bb289b7bSOmar Sandoval 		}
1622f9d9ef62SDavid Sterba 	}
1623f9d9ef62SDavid Sterba 
1624fa330659SOmar Sandoval out:
1625fa330659SOmar Sandoval 	mntput(mnt);
1626fa330659SOmar Sandoval 	kfree(subvol_name);
1627830c4adbSJosef Bacik 	return root;
1628830c4adbSJosef Bacik }
1629450ba0eaSJosef Bacik 
1630312c89fbSMisono, Tomohiro /*
1631312c89fbSMisono, Tomohiro  * Find a superblock for the given device / mount point.
1632312c89fbSMisono, Tomohiro  *
1633312c89fbSMisono, Tomohiro  * Note: This is based on mount_bdev from fs/super.c with a few additions
1634312c89fbSMisono, Tomohiro  *       for multiple device setup.  Make sure to keep it in sync.
1635312c89fbSMisono, Tomohiro  */
163672fa39f5SMisono, Tomohiro static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
163772fa39f5SMisono, Tomohiro 		int flags, const char *device_name, void *data)
163872fa39f5SMisono, Tomohiro {
163972fa39f5SMisono, Tomohiro 	struct block_device *bdev = NULL;
164072fa39f5SMisono, Tomohiro 	struct super_block *s;
164136350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
164272fa39f5SMisono, Tomohiro 	struct btrfs_fs_devices *fs_devices = NULL;
164372fa39f5SMisono, Tomohiro 	struct btrfs_fs_info *fs_info = NULL;
1644204cc0ccSAl Viro 	void *new_sec_opts = NULL;
164572fa39f5SMisono, Tomohiro 	fmode_t mode = FMODE_READ;
164672fa39f5SMisono, Tomohiro 	int error = 0;
164772fa39f5SMisono, Tomohiro 
164872fa39f5SMisono, Tomohiro 	if (!(flags & SB_RDONLY))
164972fa39f5SMisono, Tomohiro 		mode |= FMODE_WRITE;
165072fa39f5SMisono, Tomohiro 
165172fa39f5SMisono, Tomohiro 	if (data) {
1652a65001e8SAl Viro 		error = security_sb_eat_lsm_opts(data, &new_sec_opts);
165372fa39f5SMisono, Tomohiro 		if (error)
165472fa39f5SMisono, Tomohiro 			return ERR_PTR(error);
165572fa39f5SMisono, Tomohiro 	}
165672fa39f5SMisono, Tomohiro 
165772fa39f5SMisono, Tomohiro 	/*
165872fa39f5SMisono, Tomohiro 	 * Setup a dummy root and fs_info for test/set super.  This is because
165972fa39f5SMisono, Tomohiro 	 * we don't actually fill this stuff out until open_ctree, but we need
16608260edbaSJosef Bacik 	 * then open_ctree will properly initialize the file system specific
16618260edbaSJosef Bacik 	 * settings later.  btrfs_init_fs_info initializes the static elements
16628260edbaSJosef Bacik 	 * of the fs_info (locks and such) to make cleanup easier if we find a
16638260edbaSJosef Bacik 	 * superblock with our given fs_devices later on at sget() time.
166472fa39f5SMisono, Tomohiro 	 */
1665a8fd1f71SJeff Mahoney 	fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
166672fa39f5SMisono, Tomohiro 	if (!fs_info) {
166772fa39f5SMisono, Tomohiro 		error = -ENOMEM;
166872fa39f5SMisono, Tomohiro 		goto error_sec_opts;
166972fa39f5SMisono, Tomohiro 	}
16708260edbaSJosef Bacik 	btrfs_init_fs_info(fs_info);
167172fa39f5SMisono, Tomohiro 
167272fa39f5SMisono, Tomohiro 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
167372fa39f5SMisono, Tomohiro 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
167472fa39f5SMisono, Tomohiro 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
167572fa39f5SMisono, Tomohiro 		error = -ENOMEM;
167672fa39f5SMisono, Tomohiro 		goto error_fs_info;
167772fa39f5SMisono, Tomohiro 	}
167872fa39f5SMisono, Tomohiro 
1679f5194e34SDavid Sterba 	mutex_lock(&uuid_mutex);
1680fa59f27cSAnand Jain 	error = btrfs_parse_device_options(data, mode, fs_type);
168181ffd56bSDavid Sterba 	if (error) {
1682399f7f4cSDavid Sterba 		mutex_unlock(&uuid_mutex);
1683399f7f4cSDavid Sterba 		goto error_fs_info;
168481ffd56bSDavid Sterba 	}
1685399f7f4cSDavid Sterba 
168636350e95SGu Jinxiang 	device = btrfs_scan_one_device(device_name, mode, fs_type);
168736350e95SGu Jinxiang 	if (IS_ERR(device)) {
1688399f7f4cSDavid Sterba 		mutex_unlock(&uuid_mutex);
168936350e95SGu Jinxiang 		error = PTR_ERR(device);
1690399f7f4cSDavid Sterba 		goto error_fs_info;
169181ffd56bSDavid Sterba 	}
1692399f7f4cSDavid Sterba 
169336350e95SGu Jinxiang 	fs_devices = device->fs_devices;
1694399f7f4cSDavid Sterba 	fs_info->fs_devices = fs_devices;
1695399f7f4cSDavid Sterba 
169672fa39f5SMisono, Tomohiro 	error = btrfs_open_devices(fs_devices, mode, fs_type);
1697f5194e34SDavid Sterba 	mutex_unlock(&uuid_mutex);
169872fa39f5SMisono, Tomohiro 	if (error)
169972fa39f5SMisono, Tomohiro 		goto error_fs_info;
170072fa39f5SMisono, Tomohiro 
170172fa39f5SMisono, Tomohiro 	if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
170272fa39f5SMisono, Tomohiro 		error = -EACCES;
170372fa39f5SMisono, Tomohiro 		goto error_close_devices;
170472fa39f5SMisono, Tomohiro 	}
170572fa39f5SMisono, Tomohiro 
1706d24fa5c1SAnand Jain 	bdev = fs_devices->latest_dev->bdev;
170772fa39f5SMisono, Tomohiro 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
170872fa39f5SMisono, Tomohiro 		 fs_info);
170972fa39f5SMisono, Tomohiro 	if (IS_ERR(s)) {
171072fa39f5SMisono, Tomohiro 		error = PTR_ERR(s);
171172fa39f5SMisono, Tomohiro 		goto error_close_devices;
171272fa39f5SMisono, Tomohiro 	}
171372fa39f5SMisono, Tomohiro 
171472fa39f5SMisono, Tomohiro 	if (s->s_root) {
171572fa39f5SMisono, Tomohiro 		btrfs_close_devices(fs_devices);
17160d4b0463SJosef Bacik 		btrfs_free_fs_info(fs_info);
171772fa39f5SMisono, Tomohiro 		if ((flags ^ s->s_flags) & SB_RDONLY)
171872fa39f5SMisono, Tomohiro 			error = -EBUSY;
171972fa39f5SMisono, Tomohiro 	} else {
172072fa39f5SMisono, Tomohiro 		snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
172172fa39f5SMisono, Tomohiro 		btrfs_sb(s)->bdev_holder = fs_type;
17229b4e675aSDavid Sterba 		if (!strstr(crc32c_impl(), "generic"))
17239b4e675aSDavid Sterba 			set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
172472fa39f5SMisono, Tomohiro 		error = btrfs_fill_super(s, fs_devices, data);
172572fa39f5SMisono, Tomohiro 	}
1726a65001e8SAl Viro 	if (!error)
1727204cc0ccSAl Viro 		error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL);
1728a65001e8SAl Viro 	security_free_mnt_opts(&new_sec_opts);
172972fa39f5SMisono, Tomohiro 	if (error) {
173072fa39f5SMisono, Tomohiro 		deactivate_locked_super(s);
1731a65001e8SAl Viro 		return ERR_PTR(error);
173272fa39f5SMisono, Tomohiro 	}
173372fa39f5SMisono, Tomohiro 
173472fa39f5SMisono, Tomohiro 	return dget(s->s_root);
173572fa39f5SMisono, Tomohiro 
173672fa39f5SMisono, Tomohiro error_close_devices:
173772fa39f5SMisono, Tomohiro 	btrfs_close_devices(fs_devices);
173872fa39f5SMisono, Tomohiro error_fs_info:
17390d4b0463SJosef Bacik 	btrfs_free_fs_info(fs_info);
174072fa39f5SMisono, Tomohiro error_sec_opts:
174172fa39f5SMisono, Tomohiro 	security_free_mnt_opts(&new_sec_opts);
174272fa39f5SMisono, Tomohiro 	return ERR_PTR(error);
174372fa39f5SMisono, Tomohiro }
1744312c89fbSMisono, Tomohiro 
1745edf24abeSChristoph Hellwig /*
1746312c89fbSMisono, Tomohiro  * Mount function which is called by VFS layer.
1747edf24abeSChristoph Hellwig  *
1748312c89fbSMisono, Tomohiro  * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1749312c89fbSMisono, Tomohiro  * which needs vfsmount* of device's root (/).  This means device's root has to
1750312c89fbSMisono, Tomohiro  * be mounted internally in any case.
1751312c89fbSMisono, Tomohiro  *
1752312c89fbSMisono, Tomohiro  * Operation flow:
1753312c89fbSMisono, Tomohiro  *   1. Parse subvol id related options for later use in mount_subvol().
1754312c89fbSMisono, Tomohiro  *
1755312c89fbSMisono, Tomohiro  *   2. Mount device's root (/) by calling vfs_kern_mount().
1756312c89fbSMisono, Tomohiro  *
1757312c89fbSMisono, Tomohiro  *      NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1758312c89fbSMisono, Tomohiro  *      first place. In order to avoid calling btrfs_mount() again, we use
1759312c89fbSMisono, Tomohiro  *      different file_system_type which is not registered to VFS by
1760312c89fbSMisono, Tomohiro  *      register_filesystem() (btrfs_root_fs_type). As a result,
1761312c89fbSMisono, Tomohiro  *      btrfs_mount_root() is called. The return value will be used by
1762312c89fbSMisono, Tomohiro  *      mount_subtree() in mount_subvol().
1763312c89fbSMisono, Tomohiro  *
1764312c89fbSMisono, Tomohiro  *   3. Call mount_subvol() to get the dentry of subvolume. Since there is
1765312c89fbSMisono, Tomohiro  *      "btrfs subvolume set-default", mount_subvol() is called always.
1766edf24abeSChristoph Hellwig  */
1767061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1768306e16ceSDavid Sterba 		const char *device_name, void *data)
17694b82d6e4SYan {
1770312c89fbSMisono, Tomohiro 	struct vfsmount *mnt_root;
1771312c89fbSMisono, Tomohiro 	struct dentry *root;
177273f73415SJosef Bacik 	char *subvol_name = NULL;
177373f73415SJosef Bacik 	u64 subvol_objectid = 0;
17744b82d6e4SYan 	int error = 0;
17754b82d6e4SYan 
177693b9bcdfSGu Jinxiang 	error = btrfs_parse_subvol_options(data, &subvol_name,
177793b9bcdfSGu Jinxiang 					&subvol_objectid);
1778f23c8af8SIlya Dryomov 	if (error) {
1779f23c8af8SIlya Dryomov 		kfree(subvol_name);
1780061dbc6bSAl Viro 		return ERR_PTR(error);
1781f23c8af8SIlya Dryomov 	}
1782edf24abeSChristoph Hellwig 
1783312c89fbSMisono, Tomohiro 	/* mount device's root (/) */
1784312c89fbSMisono, Tomohiro 	mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data);
1785312c89fbSMisono, Tomohiro 	if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) {
1786312c89fbSMisono, Tomohiro 		if (flags & SB_RDONLY) {
1787312c89fbSMisono, Tomohiro 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1788312c89fbSMisono, Tomohiro 				flags & ~SB_RDONLY, device_name, data);
17894b82d6e4SYan 		} else {
1790312c89fbSMisono, Tomohiro 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1791312c89fbSMisono, Tomohiro 				flags | SB_RDONLY, device_name, data);
1792312c89fbSMisono, Tomohiro 			if (IS_ERR(mnt_root)) {
1793312c89fbSMisono, Tomohiro 				root = ERR_CAST(mnt_root);
1794532b618bSEric W. Biederman 				kfree(subvol_name);
1795312c89fbSMisono, Tomohiro 				goto out;
1796f667aef6SQu Wenruo 			}
1797f667aef6SQu Wenruo 
1798312c89fbSMisono, Tomohiro 			down_write(&mnt_root->mnt_sb->s_umount);
1799312c89fbSMisono, Tomohiro 			error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL);
1800312c89fbSMisono, Tomohiro 			up_write(&mnt_root->mnt_sb->s_umount);
1801312c89fbSMisono, Tomohiro 			if (error < 0) {
1802312c89fbSMisono, Tomohiro 				root = ERR_PTR(error);
1803312c89fbSMisono, Tomohiro 				mntput(mnt_root);
1804532b618bSEric W. Biederman 				kfree(subvol_name);
1805312c89fbSMisono, Tomohiro 				goto out;
1806312c89fbSMisono, Tomohiro 			}
1807312c89fbSMisono, Tomohiro 		}
1808312c89fbSMisono, Tomohiro 	}
1809312c89fbSMisono, Tomohiro 	if (IS_ERR(mnt_root)) {
1810312c89fbSMisono, Tomohiro 		root = ERR_CAST(mnt_root);
1811532b618bSEric W. Biederman 		kfree(subvol_name);
1812312c89fbSMisono, Tomohiro 		goto out;
1813f667aef6SQu Wenruo 	}
18144b82d6e4SYan 
1815312c89fbSMisono, Tomohiro 	/* mount_subvol() will free subvol_name and mnt_root */
1816ae0bc863SAnand Jain 	root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
18174b82d6e4SYan 
1818312c89fbSMisono, Tomohiro out:
1819312c89fbSMisono, Tomohiro 	return root;
18204b82d6e4SYan }
18212e635a27SChris Mason 
18220d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1823f7b885beSAnand Jain 				     u32 new_pool_size, u32 old_pool_size)
18240d2450abSSergei Trofimovich {
18250d2450abSSergei Trofimovich 	if (new_pool_size == old_pool_size)
18260d2450abSSergei Trofimovich 		return;
18270d2450abSSergei Trofimovich 
18280d2450abSSergei Trofimovich 	fs_info->thread_pool_size = new_pool_size;
18290d2450abSSergei Trofimovich 
1830efe120a0SFrank Holton 	btrfs_info(fs_info, "resize thread pool %d -> %d",
18310d2450abSSergei Trofimovich 	       old_pool_size, new_pool_size);
18320d2450abSSergei Trofimovich 
18335cdc7ad3SQu Wenruo 	btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1834afe3d242SQu Wenruo 	btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1835e66f0bb1SQu Wenruo 	btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1836fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size);
1837fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size);
1838fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_write_workers,
1839fccb5d86SQu Wenruo 				new_pool_size);
1840fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1841fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
18425b3bc44eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
18430339ef2fSQu Wenruo 	btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers,
1844ff023aacSStefan Behrens 				new_pool_size);
18450d2450abSSergei Trofimovich }
18460d2450abSSergei Trofimovich 
1847f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1848f42a34b2SMiao Xie 				       unsigned long old_opts, int flags)
1849f42a34b2SMiao Xie {
1850dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1851dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
18521751e8a6SLinus Torvalds 	     (flags & SB_RDONLY))) {
1853dc81cdc5SMiao Xie 		/* wait for any defraggers to finish */
1854dc81cdc5SMiao Xie 		wait_event(fs_info->transaction_wait,
1855dc81cdc5SMiao Xie 			   (atomic_read(&fs_info->defrag_running) == 0));
18561751e8a6SLinus Torvalds 		if (flags & SB_RDONLY)
1857dc81cdc5SMiao Xie 			sync_filesystem(fs_info->sb);
1858dc81cdc5SMiao Xie 	}
1859dc81cdc5SMiao Xie }
1860dc81cdc5SMiao Xie 
1861dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1862dc81cdc5SMiao Xie 					 unsigned long old_opts)
1863dc81cdc5SMiao Xie {
186494846229SBoris Burkov 	const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
186594846229SBoris Burkov 
1866dc81cdc5SMiao Xie 	/*
1867180e4d47SLuis de Bethencourt 	 * We need to cleanup all defragable inodes if the autodefragment is
1868180e4d47SLuis de Bethencourt 	 * close or the filesystem is read only.
1869dc81cdc5SMiao Xie 	 */
1870dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1871bc98a42cSDavid Howells 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1872dc81cdc5SMiao Xie 		btrfs_cleanup_defrag_inodes(fs_info);
1873dc81cdc5SMiao Xie 	}
1874dc81cdc5SMiao Xie 
1875b0643e59SDennis Zhou 	/* If we toggled discard async */
1876b0643e59SDennis Zhou 	if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1877b0643e59SDennis Zhou 	    btrfs_test_opt(fs_info, DISCARD_ASYNC))
1878b0643e59SDennis Zhou 		btrfs_discard_resume(fs_info);
1879b0643e59SDennis Zhou 	else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1880b0643e59SDennis Zhou 		 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1881b0643e59SDennis Zhou 		btrfs_discard_cleanup(fs_info);
188294846229SBoris Burkov 
188394846229SBoris Burkov 	/* If we toggled space cache */
188494846229SBoris Burkov 	if (cache_opt != btrfs_free_space_cache_v1_active(fs_info))
188594846229SBoris Burkov 		btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
1886dc81cdc5SMiao Xie }
1887dc81cdc5SMiao Xie 
1888c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1889c146afadSYan Zheng {
1890815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
189149b25e05SJeff Mahoney 	unsigned old_flags = sb->s_flags;
189249b25e05SJeff Mahoney 	unsigned long old_opts = fs_info->mount_opt;
189349b25e05SJeff Mahoney 	unsigned long old_compress_type = fs_info->compress_type;
189449b25e05SJeff Mahoney 	u64 old_max_inline = fs_info->max_inline;
1895f7b885beSAnand Jain 	u32 old_thread_pool_size = fs_info->thread_pool_size;
1896d612ac59SAnand Jain 	u32 old_metadata_ratio = fs_info->metadata_ratio;
1897c146afadSYan Zheng 	int ret;
1898c146afadSYan Zheng 
189902b9984dSTheodore Ts'o 	sync_filesystem(sb);
190088c4703fSJohannes Thumshirn 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1901dc81cdc5SMiao Xie 
1902f667aef6SQu Wenruo 	if (data) {
1903204cc0ccSAl Viro 		void *new_sec_opts = NULL;
1904f667aef6SQu Wenruo 
1905a65001e8SAl Viro 		ret = security_sb_eat_lsm_opts(data, &new_sec_opts);
1906a65001e8SAl Viro 		if (!ret)
1907204cc0ccSAl Viro 			ret = security_sb_remount(sb, new_sec_opts);
1908a65001e8SAl Viro 		security_free_mnt_opts(&new_sec_opts);
1909f667aef6SQu Wenruo 		if (ret)
1910f667aef6SQu Wenruo 			goto restore;
1911f667aef6SQu Wenruo 	}
1912f667aef6SQu Wenruo 
19132ff7e61eSJeff Mahoney 	ret = btrfs_parse_options(fs_info, data, *flags);
1914891f41cbSChengguang Xu 	if (ret)
191549b25e05SJeff Mahoney 		goto restore;
1916b288052eSChris Mason 
1917f42a34b2SMiao Xie 	btrfs_remount_begin(fs_info, old_opts, *flags);
19180d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
19190d2450abSSergei Trofimovich 		fs_info->thread_pool_size, old_thread_pool_size);
19200d2450abSSergei Trofimovich 
1921c55a4319SBoris Burkov 	if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) !=
1922c55a4319SBoris Burkov 	    (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
19232838d255SBoris Burkov 	    (!sb_rdonly(sb) || (*flags & SB_RDONLY))) {
19242838d255SBoris Burkov 		btrfs_warn(fs_info,
19252838d255SBoris Burkov 		"remount supports changing free space tree only from ro to rw");
19262838d255SBoris Burkov 		/* Make sure free space cache options match the state on disk */
19272838d255SBoris Burkov 		if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
19282838d255SBoris Burkov 			btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
19292838d255SBoris Burkov 			btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
19302838d255SBoris Burkov 		}
19312838d255SBoris Burkov 		if (btrfs_free_space_cache_v1_active(fs_info)) {
19322838d255SBoris Burkov 			btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
19332838d255SBoris Burkov 			btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
19342838d255SBoris Burkov 		}
19352838d255SBoris Burkov 	}
19362838d255SBoris Burkov 
19371751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
1938dc81cdc5SMiao Xie 		goto out;
1939c146afadSYan Zheng 
19401751e8a6SLinus Torvalds 	if (*flags & SB_RDONLY) {
19418dabb742SStefan Behrens 		/*
19428dabb742SStefan Behrens 		 * this also happens on 'umount -rf' or on shutdown, when
19438dabb742SStefan Behrens 		 * the filesystem is busy.
19448dabb742SStefan Behrens 		 */
194521c7e756SMiao Xie 		cancel_work_sync(&fs_info->async_reclaim_work);
194657056740SJosef Bacik 		cancel_work_sync(&fs_info->async_data_reclaim_work);
1947361c093dSStefan Behrens 
1948b0643e59SDennis Zhou 		btrfs_discard_cleanup(fs_info);
1949b0643e59SDennis Zhou 
1950361c093dSStefan Behrens 		/* wait for the uuid_scan task to finish */
1951361c093dSStefan Behrens 		down(&fs_info->uuid_tree_rescan_sem);
1952361c093dSStefan Behrens 		/* avoid complains from lockdep et al. */
1953361c093dSStefan Behrens 		up(&fs_info->uuid_tree_rescan_sem);
1954361c093dSStefan Behrens 
1955a0a1db70SFilipe Manana 		btrfs_set_sb_rdonly(sb);
1956c146afadSYan Zheng 
1957e44163e1SJeff Mahoney 		/*
19581751e8a6SLinus Torvalds 		 * Setting SB_RDONLY will put the cleaner thread to
1959e44163e1SJeff Mahoney 		 * sleep at the next loop if it's already active.
1960e44163e1SJeff Mahoney 		 * If it's already asleep, we'll leave unused block
1961e44163e1SJeff Mahoney 		 * groups on disk until we're mounted read-write again
1962e44163e1SJeff Mahoney 		 * unless we clean them up here.
1963e44163e1SJeff Mahoney 		 */
1964e44163e1SJeff Mahoney 		btrfs_delete_unused_bgs(fs_info);
1965e44163e1SJeff Mahoney 
1966a0a1db70SFilipe Manana 		/*
1967a0a1db70SFilipe Manana 		 * The cleaner task could be already running before we set the
1968a0a1db70SFilipe Manana 		 * flag BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock).
1969a0a1db70SFilipe Manana 		 * We must make sure that after we finish the remount, i.e. after
1970a0a1db70SFilipe Manana 		 * we call btrfs_commit_super(), the cleaner can no longer start
1971a0a1db70SFilipe Manana 		 * a transaction - either because it was dropping a dead root,
1972a0a1db70SFilipe Manana 		 * running delayed iputs or deleting an unused block group (the
1973a0a1db70SFilipe Manana 		 * cleaner picked a block group from the list of unused block
1974a0a1db70SFilipe Manana 		 * groups before we were able to in the previous call to
1975a0a1db70SFilipe Manana 		 * btrfs_delete_unused_bgs()).
1976a0a1db70SFilipe Manana 		 */
1977a0a1db70SFilipe Manana 		wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING,
1978a0a1db70SFilipe Manana 			    TASK_UNINTERRUPTIBLE);
1979a0a1db70SFilipe Manana 
1980a8cc263eSFilipe Manana 		/*
1981a8cc263eSFilipe Manana 		 * We've set the superblock to RO mode, so we might have made
1982a8cc263eSFilipe Manana 		 * the cleaner task sleep without running all pending delayed
1983a8cc263eSFilipe Manana 		 * iputs. Go through all the delayed iputs here, so that if an
1984a8cc263eSFilipe Manana 		 * unmount happens without remounting RW we don't end up at
1985a8cc263eSFilipe Manana 		 * finishing close_ctree() with a non-empty list of delayed
1986a8cc263eSFilipe Manana 		 * iputs.
1987a8cc263eSFilipe Manana 		 */
1988a8cc263eSFilipe Manana 		btrfs_run_delayed_iputs(fs_info);
1989a8cc263eSFilipe Manana 
19908dabb742SStefan Behrens 		btrfs_dev_replace_suspend_for_unmount(fs_info);
19918dabb742SStefan Behrens 		btrfs_scrub_cancel(fs_info);
1992061594efSMiao Xie 		btrfs_pause_balance(fs_info);
19938dabb742SStefan Behrens 
1994cb13eea3SFilipe Manana 		/*
1995cb13eea3SFilipe Manana 		 * Pause the qgroup rescan worker if it is running. We don't want
1996cb13eea3SFilipe Manana 		 * it to be still running after we are in RO mode, as after that,
1997cb13eea3SFilipe Manana 		 * by the time we unmount, it might have left a transaction open,
1998cb13eea3SFilipe Manana 		 * so we would leak the transaction and/or crash.
1999cb13eea3SFilipe Manana 		 */
2000cb13eea3SFilipe Manana 		btrfs_qgroup_wait_for_completion(fs_info, false);
2001cb13eea3SFilipe Manana 
20026bccf3abSJeff Mahoney 		ret = btrfs_commit_super(fs_info);
200349b25e05SJeff Mahoney 		if (ret)
200449b25e05SJeff Mahoney 			goto restore;
2005c146afadSYan Zheng 	} else {
200684961539SJosef Bacik 		if (BTRFS_FS_ERROR(fs_info)) {
20076ef3de9cSDavid Sterba 			btrfs_err(fs_info,
2008efe120a0SFrank Holton 				"Remounting read-write after error is not allowed");
20096ef3de9cSDavid Sterba 			ret = -EINVAL;
20106ef3de9cSDavid Sterba 			goto restore;
20116ef3de9cSDavid Sterba 		}
20128a3db184SSergei Trofimovich 		if (fs_info->fs_devices->rw_devices == 0) {
201349b25e05SJeff Mahoney 			ret = -EACCES;
201449b25e05SJeff Mahoney 			goto restore;
20158a3db184SSergei Trofimovich 		}
20162b82032cSYan Zheng 
20176528b99dSAnand Jain 		if (!btrfs_check_rw_degradable(fs_info, NULL)) {
2018efe120a0SFrank Holton 			btrfs_warn(fs_info,
201952042d8eSAndrea Gelmini 		"too many missing devices, writable remount is not allowed");
2020292fd7fcSStefan Behrens 			ret = -EACCES;
2021292fd7fcSStefan Behrens 			goto restore;
2022292fd7fcSStefan Behrens 		}
2023292fd7fcSStefan Behrens 
20248a3db184SSergei Trofimovich 		if (btrfs_super_log_root(fs_info->super_copy) != 0) {
202510a3a3edSDavid Sterba 			btrfs_warn(fs_info,
202610a3a3edSDavid Sterba 		"mount required to replay tree-log, cannot remount read-write");
202749b25e05SJeff Mahoney 			ret = -EINVAL;
202849b25e05SJeff Mahoney 			goto restore;
20298a3db184SSergei Trofimovich 		}
2030c146afadSYan Zheng 
203144c0ca21SBoris Burkov 		/*
203244c0ca21SBoris Burkov 		 * NOTE: when remounting with a change that does writes, don't
203344c0ca21SBoris Burkov 		 * put it anywhere above this point, as we are not sure to be
203444c0ca21SBoris Burkov 		 * safe to write until we pass the above checks.
203544c0ca21SBoris Burkov 		 */
203644c0ca21SBoris Burkov 		ret = btrfs_start_pre_rw_mount(fs_info);
203749b25e05SJeff Mahoney 		if (ret)
203849b25e05SJeff Mahoney 			goto restore;
2039c146afadSYan Zheng 
2040a0a1db70SFilipe Manana 		btrfs_clear_sb_rdonly(sb);
204190c711abSZygo Blaxell 
2042afcdd129SJosef Bacik 		set_bit(BTRFS_FS_OPEN, &fs_info->flags);
2043c146afadSYan Zheng 	}
2044dc81cdc5SMiao Xie out:
2045faa00889SJosef Bacik 	/*
2046faa00889SJosef Bacik 	 * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS,
2047faa00889SJosef Bacik 	 * since the absence of the flag means it can be toggled off by remount.
2048faa00889SJosef Bacik 	 */
2049faa00889SJosef Bacik 	*flags |= SB_I_VERSION;
2050faa00889SJosef Bacik 
20512c6a92b0SJustin Maggard 	wake_up_process(fs_info->transaction_kthread);
2052dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
20538cd29088SBoris Burkov 	btrfs_clear_oneshot_options(fs_info);
205488c4703fSJohannes Thumshirn 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
205588c4703fSJohannes Thumshirn 
2056c146afadSYan Zheng 	return 0;
205749b25e05SJeff Mahoney 
205849b25e05SJeff Mahoney restore:
20591751e8a6SLinus Torvalds 	/* We've hit an error - don't reset SB_RDONLY */
2060bc98a42cSDavid Howells 	if (sb_rdonly(sb))
20611751e8a6SLinus Torvalds 		old_flags |= SB_RDONLY;
2062a0a1db70SFilipe Manana 	if (!(old_flags & SB_RDONLY))
2063a0a1db70SFilipe Manana 		clear_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state);
206449b25e05SJeff Mahoney 	sb->s_flags = old_flags;
206549b25e05SJeff Mahoney 	fs_info->mount_opt = old_opts;
206649b25e05SJeff Mahoney 	fs_info->compress_type = old_compress_type;
206749b25e05SJeff Mahoney 	fs_info->max_inline = old_max_inline;
20680d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
20690d2450abSSergei Trofimovich 		old_thread_pool_size, fs_info->thread_pool_size);
207049b25e05SJeff Mahoney 	fs_info->metadata_ratio = old_metadata_ratio;
2071dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
207288c4703fSJohannes Thumshirn 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
207388c4703fSJohannes Thumshirn 
207449b25e05SJeff Mahoney 	return ret;
2075c146afadSYan Zheng }
2076c146afadSYan Zheng 
2077bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
2078214cc184SDavid Sterba static int btrfs_cmp_device_free_bytes(const void *a, const void *b)
2079bcd53741SArne Jansen {
2080214cc184SDavid Sterba 	const struct btrfs_device_info *dev_info1 = a;
2081214cc184SDavid Sterba 	const struct btrfs_device_info *dev_info2 = b;
2082214cc184SDavid Sterba 
2083214cc184SDavid Sterba 	if (dev_info1->max_avail > dev_info2->max_avail)
2084bcd53741SArne Jansen 		return -1;
2085214cc184SDavid Sterba 	else if (dev_info1->max_avail < dev_info2->max_avail)
2086bcd53741SArne Jansen 		return 1;
2087bcd53741SArne Jansen 	return 0;
2088bcd53741SArne Jansen }
2089bcd53741SArne Jansen 
2090bcd53741SArne Jansen /*
2091bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
2092bcd53741SArne Jansen  * is stored.(Descending Sort)
2093bcd53741SArne Jansen  */
2094bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
2095bcd53741SArne Jansen 					struct btrfs_device_info *devices,
2096bcd53741SArne Jansen 					size_t nr_devices)
2097bcd53741SArne Jansen {
2098bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
2099bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
2100bcd53741SArne Jansen }
2101bcd53741SArne Jansen 
21026d07bcecSMiao Xie /*
21036d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
21046d07bcecSMiao Xie  * file data.
21056d07bcecSMiao Xie  */
21067e17916bSArnd Bergmann static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
21076bccf3abSJeff Mahoney 					      u64 *free_bytes)
21086d07bcecSMiao Xie {
21096d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
21106d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
21116d07bcecSMiao Xie 	struct btrfs_device *device;
21126d07bcecSMiao Xie 	u64 type;
21136d07bcecSMiao Xie 	u64 avail_space;
21146d07bcecSMiao Xie 	u64 min_stripe_size;
2115559ca6eaSNikolay Borisov 	int num_stripes = 1;
21166d07bcecSMiao Xie 	int i = 0, nr_devices;
21174f080f57SDavid Sterba 	const struct btrfs_raid_attr *rattr;
21186d07bcecSMiao Xie 
21197e33fd99SJosef Bacik 	/*
212001327610SNicholas D Steeves 	 * We aren't under the device list lock, so this is racy-ish, but good
21217e33fd99SJosef Bacik 	 * enough for our purposes.
21227e33fd99SJosef Bacik 	 */
2123b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
21247e33fd99SJosef Bacik 	if (!nr_devices) {
21257e33fd99SJosef Bacik 		smp_mb();
21267e33fd99SJosef Bacik 		nr_devices = fs_info->fs_devices->open_devices;
21277e33fd99SJosef Bacik 		ASSERT(nr_devices);
21287e33fd99SJosef Bacik 		if (!nr_devices) {
21297e33fd99SJosef Bacik 			*free_bytes = 0;
21307e33fd99SJosef Bacik 			return 0;
21317e33fd99SJosef Bacik 		}
21327e33fd99SJosef Bacik 	}
21336d07bcecSMiao Xie 
2134d9b0d9baSDulshani Gunawardhana 	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
21356a44517dSDavid Sterba 			       GFP_KERNEL);
21366d07bcecSMiao Xie 	if (!devices_info)
21376d07bcecSMiao Xie 		return -ENOMEM;
21386d07bcecSMiao Xie 
213901327610SNicholas D Steeves 	/* calc min stripe number for data space allocation */
21401b86826dSJeff Mahoney 	type = btrfs_data_alloc_profile(fs_info);
21414f080f57SDavid Sterba 	rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
21424f080f57SDavid Sterba 
2143e1ea2beeSDavid Sterba 	if (type & BTRFS_BLOCK_GROUP_RAID0)
214439fb26c3SMiao Xie 		num_stripes = nr_devices;
2145e1ea2beeSDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1)
214639fb26c3SMiao Xie 		num_stripes = 2;
214747e6f742SDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1C3)
214847e6f742SDavid Sterba 		num_stripes = 3;
21498d6fac00SDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1C4)
21508d6fac00SDavid Sterba 		num_stripes = 4;
2151e1ea2beeSDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID10)
215239fb26c3SMiao Xie 		num_stripes = 4;
21536d07bcecSMiao Xie 
21544f080f57SDavid Sterba 	/* Adjust for more than 1 stripe per device */
21554f080f57SDavid Sterba 	min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
21566d07bcecSMiao Xie 
21577e33fd99SJosef Bacik 	rcu_read_lock();
21587e33fd99SJosef Bacik 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
2159e12c9621SAnand Jain 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2160e12c9621SAnand Jain 						&device->dev_state) ||
2161401e29c1SAnand Jain 		    !device->bdev ||
2162401e29c1SAnand Jain 		    test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
21636d07bcecSMiao Xie 			continue;
21646d07bcecSMiao Xie 
21657e33fd99SJosef Bacik 		if (i >= nr_devices)
21667e33fd99SJosef Bacik 			break;
21677e33fd99SJosef Bacik 
21686d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
21696d07bcecSMiao Xie 
21706d07bcecSMiao Xie 		/* align with stripe_len */
2171559ca6eaSNikolay Borisov 		avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
21726d07bcecSMiao Xie 
21736d07bcecSMiao Xie 		/*
217401327610SNicholas D Steeves 		 * In order to avoid overwriting the superblock on the drive,
21756d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
21766d07bcecSMiao Xie 		 * allocation.
2177559ca6eaSNikolay Borisov 		 *
2178559ca6eaSNikolay Borisov 		 * This ensures we have at least min_stripe_size free space
2179559ca6eaSNikolay Borisov 		 * after excluding 1MB.
21806d07bcecSMiao Xie 		 */
2181559ca6eaSNikolay Borisov 		if (avail_space <= SZ_1M + min_stripe_size)
21826d07bcecSMiao Xie 			continue;
21836d07bcecSMiao Xie 
2184559ca6eaSNikolay Borisov 		avail_space -= SZ_1M;
2185559ca6eaSNikolay Borisov 
21866d07bcecSMiao Xie 		devices_info[i].dev = device;
21876d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
21886d07bcecSMiao Xie 
21896d07bcecSMiao Xie 		i++;
21906d07bcecSMiao Xie 	}
21917e33fd99SJosef Bacik 	rcu_read_unlock();
21926d07bcecSMiao Xie 
21936d07bcecSMiao Xie 	nr_devices = i;
21946d07bcecSMiao Xie 
21956d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
21966d07bcecSMiao Xie 
21976d07bcecSMiao Xie 	i = nr_devices - 1;
21986d07bcecSMiao Xie 	avail_space = 0;
2199559ca6eaSNikolay Borisov 	while (nr_devices >= rattr->devs_min) {
2200559ca6eaSNikolay Borisov 		num_stripes = min(num_stripes, nr_devices);
220139fb26c3SMiao Xie 
22026d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
22036d07bcecSMiao Xie 			int j;
22046d07bcecSMiao Xie 			u64 alloc_size;
22056d07bcecSMiao Xie 
220639fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
22076d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
220839fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
22096d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
22106d07bcecSMiao Xie 		}
22116d07bcecSMiao Xie 		i--;
22126d07bcecSMiao Xie 		nr_devices--;
22136d07bcecSMiao Xie 	}
22146d07bcecSMiao Xie 
22156d07bcecSMiao Xie 	kfree(devices_info);
22166d07bcecSMiao Xie 	*free_bytes = avail_space;
22176d07bcecSMiao Xie 	return 0;
22186d07bcecSMiao Xie }
22196d07bcecSMiao Xie 
2220ba7b6e62SDavid Sterba /*
2221ba7b6e62SDavid Sterba  * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2222ba7b6e62SDavid Sterba  *
2223ba7b6e62SDavid Sterba  * If there's a redundant raid level at DATA block groups, use the respective
2224ba7b6e62SDavid Sterba  * multiplier to scale the sizes.
2225ba7b6e62SDavid Sterba  *
2226ba7b6e62SDavid Sterba  * Unused device space usage is based on simulating the chunk allocator
22270d0c71b3SDavid Sterba  * algorithm that respects the device sizes and order of allocations.  This is
22280d0c71b3SDavid Sterba  * a close approximation of the actual use but there are other factors that may
22290d0c71b3SDavid Sterba  * change the result (like a new metadata chunk).
2230ba7b6e62SDavid Sterba  *
2231ca8a51b3SDavid Sterba  * If metadata is exhausted, f_bavail will be 0.
2232ba7b6e62SDavid Sterba  */
22338fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
22348fd17795SChris Mason {
2235815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
2236815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
2237bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
2238bd4d1088SJosef Bacik 	u64 total_used = 0;
22396d07bcecSMiao Xie 	u64 total_free_data = 0;
2240ca8a51b3SDavid Sterba 	u64 total_free_meta = 0;
2241265fdfa6SDavid Sterba 	u32 bits = fs_info->sectorsize_bits;
2242de37aa51SNikolay Borisov 	__be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
2243ba7b6e62SDavid Sterba 	unsigned factor = 1;
2244ba7b6e62SDavid Sterba 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
22456d07bcecSMiao Xie 	int ret;
2246ca8a51b3SDavid Sterba 	u64 thresh = 0;
2247ae02d1bdSLuis de Bethencourt 	int mixed = 0;
22488fd17795SChris Mason 
224972804905SJosef Bacik 	list_for_each_entry(found, &fs_info->space_info, list) {
22506d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
2251ba7b6e62SDavid Sterba 			int i;
2252ba7b6e62SDavid Sterba 
22536d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
22546d07bcecSMiao Xie 			total_free_data -=
22556d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
2256ba7b6e62SDavid Sterba 
2257ba7b6e62SDavid Sterba 			for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
225846df06b8SDavid Sterba 				if (!list_empty(&found->block_groups[i]))
225946df06b8SDavid Sterba 					factor = btrfs_bg_type_to_factor(
226046df06b8SDavid Sterba 						btrfs_raid_array[i].bg_flag);
2261ba7b6e62SDavid Sterba 			}
22626d07bcecSMiao Xie 		}
2263ae02d1bdSLuis de Bethencourt 
2264ae02d1bdSLuis de Bethencourt 		/*
2265ae02d1bdSLuis de Bethencourt 		 * Metadata in mixed block goup profiles are accounted in data
2266ae02d1bdSLuis de Bethencourt 		 */
2267ae02d1bdSLuis de Bethencourt 		if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
2268ae02d1bdSLuis de Bethencourt 			if (found->flags & BTRFS_BLOCK_GROUP_DATA)
2269ae02d1bdSLuis de Bethencourt 				mixed = 1;
2270ae02d1bdSLuis de Bethencourt 			else
2271ae02d1bdSLuis de Bethencourt 				total_free_meta += found->disk_total -
2272ae02d1bdSLuis de Bethencourt 					found->disk_used;
2273ae02d1bdSLuis de Bethencourt 		}
22746d07bcecSMiao Xie 
2275b742bb82SYan, Zheng 		total_used += found->disk_used;
227689a55897SJosef Bacik 	}
2277ba7b6e62SDavid Sterba 
2278ba7b6e62SDavid Sterba 	buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
2279ba7b6e62SDavid Sterba 	buf->f_blocks >>= bits;
2280ba7b6e62SDavid Sterba 	buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
2281ba7b6e62SDavid Sterba 
2282ba7b6e62SDavid Sterba 	/* Account global block reserve as used, it's in logical size already */
2283ba7b6e62SDavid Sterba 	spin_lock(&block_rsv->lock);
228441b34accSLuis de Bethencourt 	/* Mixed block groups accounting is not byte-accurate, avoid overflow */
228541b34accSLuis de Bethencourt 	if (buf->f_bfree >= block_rsv->size >> bits)
2286ba7b6e62SDavid Sterba 		buf->f_bfree -= block_rsv->size >> bits;
228741b34accSLuis de Bethencourt 	else
228841b34accSLuis de Bethencourt 		buf->f_bfree = 0;
2289ba7b6e62SDavid Sterba 	spin_unlock(&block_rsv->lock);
2290ba7b6e62SDavid Sterba 
22910d95c1beSDavid Sterba 	buf->f_bavail = div_u64(total_free_data, factor);
22926bccf3abSJeff Mahoney 	ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
22937e33fd99SJosef Bacik 	if (ret)
22946d07bcecSMiao Xie 		return ret;
2295ba7b6e62SDavid Sterba 	buf->f_bavail += div_u64(total_free_data, factor);
22966d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
2297d397712bSChris Mason 
2298ca8a51b3SDavid Sterba 	/*
2299ca8a51b3SDavid Sterba 	 * We calculate the remaining metadata space minus global reserve. If
2300ca8a51b3SDavid Sterba 	 * this is (supposedly) smaller than zero, there's no space. But this
2301ca8a51b3SDavid Sterba 	 * does not hold in practice, the exhausted state happens where's still
2302ca8a51b3SDavid Sterba 	 * some positive delta. So we apply some guesswork and compare the
2303ca8a51b3SDavid Sterba 	 * delta to a 4M threshold.  (Practically observed delta was ~2M.)
2304ca8a51b3SDavid Sterba 	 *
2305ca8a51b3SDavid Sterba 	 * We probably cannot calculate the exact threshold value because this
2306ca8a51b3SDavid Sterba 	 * depends on the internal reservations requested by various
2307ca8a51b3SDavid Sterba 	 * operations, so some operations that consume a few metadata will
2308ca8a51b3SDavid Sterba 	 * succeed even if the Avail is zero. But this is better than the other
2309ca8a51b3SDavid Sterba 	 * way around.
2310ca8a51b3SDavid Sterba 	 */
2311d4417e22SNikolay Borisov 	thresh = SZ_4M;
2312ca8a51b3SDavid Sterba 
2313d55966c4SJosef Bacik 	/*
2314d55966c4SJosef Bacik 	 * We only want to claim there's no available space if we can no longer
2315d55966c4SJosef Bacik 	 * allocate chunks for our metadata profile and our global reserve will
2316d55966c4SJosef Bacik 	 * not fit in the free metadata space.  If we aren't ->full then we
2317d55966c4SJosef Bacik 	 * still can allocate chunks and thus are fine using the currently
2318d55966c4SJosef Bacik 	 * calculated f_bavail.
2319d55966c4SJosef Bacik 	 */
2320d55966c4SJosef Bacik 	if (!mixed && block_rsv->space_info->full &&
2321d55966c4SJosef Bacik 	    total_free_meta - thresh < block_rsv->size)
2322ca8a51b3SDavid Sterba 		buf->f_bavail = 0;
2323ca8a51b3SDavid Sterba 
2324ba7b6e62SDavid Sterba 	buf->f_type = BTRFS_SUPER_MAGIC;
2325ba7b6e62SDavid Sterba 	buf->f_bsize = dentry->d_sb->s_blocksize;
2326ba7b6e62SDavid Sterba 	buf->f_namelen = BTRFS_NAME_LEN;
2327ba7b6e62SDavid Sterba 
23289d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
23299d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
23309d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
23319d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
23329d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
233332d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
23344fd786e6SMisono Tomohiro 	buf->f_fsid.val[0] ^=
23354fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32;
23364fd786e6SMisono Tomohiro 	buf->f_fsid.val[1] ^=
23374fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid;
233832d48fa1SDavid Woodhouse 
23398fd17795SChris Mason 	return 0;
23408fd17795SChris Mason }
2341b5133862SChris Mason 
2342aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
2343aea52e19SAl Viro {
2344815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2345aea52e19SAl Viro 	kill_anon_super(sb);
23460d4b0463SJosef Bacik 	btrfs_free_fs_info(fs_info);
2347aea52e19SAl Viro }
2348aea52e19SAl Viro 
23492e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
23502e635a27SChris Mason 	.owner		= THIS_MODULE,
23512e635a27SChris Mason 	.name		= "btrfs",
2352061dbc6bSAl Viro 	.mount		= btrfs_mount,
2353aea52e19SAl Viro 	.kill_sb	= btrfs_kill_super,
2354f667aef6SQu Wenruo 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
23552e635a27SChris Mason };
235672fa39f5SMisono, Tomohiro 
235772fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type = {
235872fa39f5SMisono, Tomohiro 	.owner		= THIS_MODULE,
235972fa39f5SMisono, Tomohiro 	.name		= "btrfs",
236072fa39f5SMisono, Tomohiro 	.mount		= btrfs_mount_root,
236172fa39f5SMisono, Tomohiro 	.kill_sb	= btrfs_kill_super,
23625b9b26f5SChristian Brauner 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP,
236372fa39f5SMisono, Tomohiro };
236472fa39f5SMisono, Tomohiro 
23657f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs");
2366a9218f6bSChris Mason 
2367d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file)
2368d8620958STom Van Braeckel {
2369d8620958STom Van Braeckel 	/*
2370d8620958STom Van Braeckel 	 * The control file's private_data is used to hold the
2371d8620958STom Van Braeckel 	 * transaction when it is started and is used to keep
2372d8620958STom Van Braeckel 	 * track of whether a transaction is already in progress.
2373d8620958STom Van Braeckel 	 */
2374d8620958STom Van Braeckel 	file->private_data = NULL;
2375d8620958STom Van Braeckel 	return 0;
2376d8620958STom Van Braeckel }
2377d8620958STom Van Braeckel 
2378d352ac68SChris Mason /*
2379cfe953c8SSu Yue  * Used by /dev/btrfs-control for devices ioctls.
2380d352ac68SChris Mason  */
23818a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
23828a4b83ccSChris Mason 				unsigned long arg)
23838a4b83ccSChris Mason {
23848a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
238536350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
2386*16cab91aSAnand Jain 	dev_t devt = 0;
2387c071fcfdSChris Mason 	int ret = -ENOTTY;
23888a4b83ccSChris Mason 
2389e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2390e441d54dSChris Mason 		return -EPERM;
2391e441d54dSChris Mason 
2392dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
2393dae7b665SLi Zefan 	if (IS_ERR(vol))
2394dae7b665SLi Zefan 		return PTR_ERR(vol);
2395f505754fSFilipe Manana 	vol->name[BTRFS_PATH_NAME_MAX] = '\0';
2396c071fcfdSChris Mason 
23978a4b83ccSChris Mason 	switch (cmd) {
23988a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
2399899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
240036350e95SGu Jinxiang 		device = btrfs_scan_one_device(vol->name, FMODE_READ,
240136350e95SGu Jinxiang 					       &btrfs_root_fs_type);
240236350e95SGu Jinxiang 		ret = PTR_ERR_OR_ZERO(device);
2403899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
24048a4b83ccSChris Mason 		break;
2405228a73abSAnand Jain 	case BTRFS_IOC_FORGET_DEV:
2406*16cab91aSAnand Jain 		if (vol->name[0] != 0) {
2407*16cab91aSAnand Jain 			ret = lookup_bdev(vol->name, &devt);
2408*16cab91aSAnand Jain 			if (ret)
2409*16cab91aSAnand Jain 				break;
2410*16cab91aSAnand Jain 		}
2411*16cab91aSAnand Jain 		ret = btrfs_forget_devices(devt);
2412228a73abSAnand Jain 		break;
241302db0844SJosef Bacik 	case BTRFS_IOC_DEVICES_READY:
2414899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
241536350e95SGu Jinxiang 		device = btrfs_scan_one_device(vol->name, FMODE_READ,
241636350e95SGu Jinxiang 					       &btrfs_root_fs_type);
241736350e95SGu Jinxiang 		if (IS_ERR(device)) {
2418899f9307SDavid Sterba 			mutex_unlock(&uuid_mutex);
241936350e95SGu Jinxiang 			ret = PTR_ERR(device);
242002db0844SJosef Bacik 			break;
2421899f9307SDavid Sterba 		}
242236350e95SGu Jinxiang 		ret = !(device->fs_devices->num_devices ==
242336350e95SGu Jinxiang 			device->fs_devices->total_devices);
2424899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
242502db0844SJosef Bacik 		break;
2426c5868f83SDavid Sterba 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2427d5131b65SDavid Sterba 		ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2428c5868f83SDavid Sterba 		break;
24298a4b83ccSChris Mason 	}
2430dae7b665SLi Zefan 
24318a4b83ccSChris Mason 	kfree(vol);
2432f819d837SLinda Knippers 	return ret;
24338a4b83ccSChris Mason }
24348a4b83ccSChris Mason 
24350176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
2436ed0dab6bSYan {
2437354aa0fbSMiao Xie 	struct btrfs_trans_handle *trans;
24380b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
24390b246afaSJeff Mahoney 	struct btrfs_root *root = fs_info->tree_root;
2440354aa0fbSMiao Xie 
2441fac03c8dSDavid Sterba 	set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
24429e7cc91aSWang Xiaoguang 	/*
24439e7cc91aSWang Xiaoguang 	 * We don't need a barrier here, we'll wait for any transaction that
24449e7cc91aSWang Xiaoguang 	 * could be in progress on other threads (and do delayed iputs that
24459e7cc91aSWang Xiaoguang 	 * we want to avoid on a frozen filesystem), or do the commit
24469e7cc91aSWang Xiaoguang 	 * ourselves.
24479e7cc91aSWang Xiaoguang 	 */
2448d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
2449354aa0fbSMiao Xie 	if (IS_ERR(trans)) {
2450354aa0fbSMiao Xie 		/* no transaction, don't bother */
2451354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
24520176260fSLinus Torvalds 			return 0;
2453354aa0fbSMiao Xie 		return PTR_ERR(trans);
2454354aa0fbSMiao Xie 	}
24553a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
2456ed0dab6bSYan }
2457ed0dab6bSYan 
24589e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb)
24599e7cc91aSWang Xiaoguang {
2460fac03c8dSDavid Sterba 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2461fac03c8dSDavid Sterba 
2462fac03c8dSDavid Sterba 	clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
24639e7cc91aSWang Xiaoguang 	return 0;
24649e7cc91aSWang Xiaoguang }
24659e7cc91aSWang Xiaoguang 
24669c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
24679c5085c1SJosef Bacik {
24689c5085c1SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
24699c5085c1SJosef Bacik 
247088c14590SDavid Sterba 	/*
24716605fd2fSAnand Jain 	 * There should be always a valid pointer in latest_dev, it may be stale
24726605fd2fSAnand Jain 	 * for a short moment in case it's being deleted but still valid until
24736605fd2fSAnand Jain 	 * the end of RCU grace period.
247488c14590SDavid Sterba 	 */
247588c14590SDavid Sterba 	rcu_read_lock();
24766605fd2fSAnand Jain 	seq_escape(m, rcu_str_deref(fs_info->fs_devices->latest_dev->name), " \t\n\\");
247788c14590SDavid Sterba 	rcu_read_unlock();
24786605fd2fSAnand Jain 
24799c5085c1SJosef Bacik 	return 0;
24809c5085c1SJosef Bacik }
24819c5085c1SJosef Bacik 
2482b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
248376dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
2484bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
2485e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
2486d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
2487a9572a15SEric Paris 	.show_options	= btrfs_show_options,
24889c5085c1SJosef Bacik 	.show_devname	= btrfs_show_devname,
24892c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
24902c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
249126602cabSAl Viro 	.free_inode	= btrfs_free_inode,
24928fd17795SChris Mason 	.statfs		= btrfs_statfs,
2493c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
24940176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
24959e7cc91aSWang Xiaoguang 	.unfreeze_fs	= btrfs_unfreeze,
2496e20d96d6SChris Mason };
2497a9218f6bSChris Mason 
2498a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
2499d8620958STom Van Braeckel 	.open = btrfs_control_open,
2500a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
25011832f2d8SArnd Bergmann 	.compat_ioctl = compat_ptr_ioctl,
2502a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
25036038f373SArnd Bergmann 	.llseek = noop_llseek,
2504a9218f6bSChris Mason };
2505a9218f6bSChris Mason 
2506a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
2507578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
2508a9218f6bSChris Mason 	.name		= "btrfs-control",
2509a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
2510a9218f6bSChris Mason };
2511a9218f6bSChris Mason 
2512578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2513578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
2514578454ffSKay Sievers 
2515f5c29bd9SLiu Bo static int __init btrfs_interface_init(void)
2516a9218f6bSChris Mason {
2517a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
2518a9218f6bSChris Mason }
2519a9218f6bSChris Mason 
2520e67c718bSDavid Sterba static __cold void btrfs_interface_exit(void)
2521a9218f6bSChris Mason {
2522f368ed60SGreg Kroah-Hartman 	misc_deregister(&btrfs_misc);
2523a9218f6bSChris Mason }
2524a9218f6bSChris Mason 
2525f5c29bd9SLiu Bo static void __init btrfs_print_mod_info(void)
252685965600SDavid Sterba {
2527edf57cbfSBart Van Assche 	static const char options[] = ""
252885965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
252985965600SDavid Sterba 			", debug=on"
253085965600SDavid Sterba #endif
253179556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT
253279556c3dSStefan Behrens 			", assert=on"
253379556c3dSStefan Behrens #endif
253485965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
253585965600SDavid Sterba 			", integrity-checker=on"
253685965600SDavid Sterba #endif
2537fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2538fb592373SJosef Bacik 			", ref-verify=on"
2539fb592373SJosef Bacik #endif
25405b316468SNaohiro Aota #ifdef CONFIG_BLK_DEV_ZONED
25415b316468SNaohiro Aota 			", zoned=yes"
25425b316468SNaohiro Aota #else
25435b316468SNaohiro Aota 			", zoned=no"
25445b316468SNaohiro Aota #endif
2545ea3dc7d2SDavid Sterba #ifdef CONFIG_FS_VERITY
2546ea3dc7d2SDavid Sterba 			", fsverity=yes"
2547ea3dc7d2SDavid Sterba #else
2548ea3dc7d2SDavid Sterba 			", fsverity=no"
2549ea3dc7d2SDavid Sterba #endif
2550edf57cbfSBart Van Assche 			;
2551edf57cbfSBart Van Assche 	pr_info("Btrfs loaded, crc32c=%s%s\n", crc32c_impl(), options);
255285965600SDavid Sterba }
255385965600SDavid Sterba 
25542e635a27SChris Mason static int __init init_btrfs_fs(void)
25552e635a27SChris Mason {
25562c90e5d6SChris Mason 	int err;
255758176a96SJosef Bacik 
255863541927SFilipe David Borba Manana 	btrfs_props_init();
255963541927SFilipe David Borba Manana 
256058176a96SJosef Bacik 	err = btrfs_init_sysfs();
256158176a96SJosef Bacik 	if (err)
25629678c543SNikolay Borisov 		return err;
256358176a96SJosef Bacik 
2564143bede5SJeff Mahoney 	btrfs_init_compress();
2565d1310b2eSChris Mason 
2566261507a0SLi Zefan 	err = btrfs_init_cachep();
2567261507a0SLi Zefan 	if (err)
2568261507a0SLi Zefan 		goto free_compress;
2569261507a0SLi Zefan 
2570d1310b2eSChris Mason 	err = extent_io_init();
25712f4cbe64SWyatt Banks 	if (err)
25722f4cbe64SWyatt Banks 		goto free_cachep;
25732f4cbe64SWyatt Banks 
25746f0d04f8SJosef Bacik 	err = extent_state_cache_init();
2575d1310b2eSChris Mason 	if (err)
2576d1310b2eSChris Mason 		goto free_extent_io;
2577d1310b2eSChris Mason 
25786f0d04f8SJosef Bacik 	err = extent_map_init();
25796f0d04f8SJosef Bacik 	if (err)
25806f0d04f8SJosef Bacik 		goto free_extent_state_cache;
25816f0d04f8SJosef Bacik 
25826352b91dSMiao Xie 	err = ordered_data_init();
25832f4cbe64SWyatt Banks 	if (err)
25842f4cbe64SWyatt Banks 		goto free_extent_map;
2585c8b97818SChris Mason 
25866352b91dSMiao Xie 	err = btrfs_delayed_inode_init();
25876352b91dSMiao Xie 	if (err)
25886352b91dSMiao Xie 		goto free_ordered_data;
25896352b91dSMiao Xie 
25909247f317SMiao Xie 	err = btrfs_auto_defrag_init();
259116cdcec7SMiao Xie 	if (err)
259216cdcec7SMiao Xie 		goto free_delayed_inode;
259316cdcec7SMiao Xie 
259478a6184aSMiao Xie 	err = btrfs_delayed_ref_init();
25959247f317SMiao Xie 	if (err)
25969247f317SMiao Xie 		goto free_auto_defrag;
25979247f317SMiao Xie 
2598b9e9a6cbSWang Shilong 	err = btrfs_prelim_ref_init();
2599b9e9a6cbSWang Shilong 	if (err)
2600af13b492SDavid Sterba 		goto free_delayed_ref;
2601b9e9a6cbSWang Shilong 
260297eb6b69SDavid Sterba 	err = btrfs_end_io_wq_init();
260378a6184aSMiao Xie 	if (err)
2604af13b492SDavid Sterba 		goto free_prelim_ref;
260578a6184aSMiao Xie 
260697eb6b69SDavid Sterba 	err = btrfs_interface_init();
260797eb6b69SDavid Sterba 	if (err)
260897eb6b69SDavid Sterba 		goto free_end_io_wq;
260997eb6b69SDavid Sterba 
26108ae1af3cSAnand Jain 	btrfs_print_mod_info();
2611dc11dd5dSJosef Bacik 
2612dc11dd5dSJosef Bacik 	err = btrfs_run_sanity_tests();
2613dc11dd5dSJosef Bacik 	if (err)
2614dc11dd5dSJosef Bacik 		goto unregister_ioctl;
2615dc11dd5dSJosef Bacik 
2616dc11dd5dSJosef Bacik 	err = register_filesystem(&btrfs_fs_type);
2617dc11dd5dSJosef Bacik 	if (err)
2618dc11dd5dSJosef Bacik 		goto unregister_ioctl;
261974255aa0SJosef Bacik 
26202f4cbe64SWyatt Banks 	return 0;
26212f4cbe64SWyatt Banks 
2622a9218f6bSChris Mason unregister_ioctl:
2623a9218f6bSChris Mason 	btrfs_interface_exit();
262497eb6b69SDavid Sterba free_end_io_wq:
262597eb6b69SDavid Sterba 	btrfs_end_io_wq_exit();
2626b9e9a6cbSWang Shilong free_prelim_ref:
2627b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
262878a6184aSMiao Xie free_delayed_ref:
262978a6184aSMiao Xie 	btrfs_delayed_ref_exit();
26309247f317SMiao Xie free_auto_defrag:
26319247f317SMiao Xie 	btrfs_auto_defrag_exit();
263216cdcec7SMiao Xie free_delayed_inode:
263316cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
26346352b91dSMiao Xie free_ordered_data:
26356352b91dSMiao Xie 	ordered_data_exit();
26362f4cbe64SWyatt Banks free_extent_map:
26372f4cbe64SWyatt Banks 	extent_map_exit();
26386f0d04f8SJosef Bacik free_extent_state_cache:
26396f0d04f8SJosef Bacik 	extent_state_cache_exit();
2640d1310b2eSChris Mason free_extent_io:
2641d1310b2eSChris Mason 	extent_io_exit();
26422f4cbe64SWyatt Banks free_cachep:
26432f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
2644261507a0SLi Zefan free_compress:
2645261507a0SLi Zefan 	btrfs_exit_compress();
26462f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
26479678c543SNikolay Borisov 
26482c90e5d6SChris Mason 	return err;
26492e635a27SChris Mason }
26502e635a27SChris Mason 
26512e635a27SChris Mason static void __exit exit_btrfs_fs(void)
26522e635a27SChris Mason {
265339279cc3SChris Mason 	btrfs_destroy_cachep();
265478a6184aSMiao Xie 	btrfs_delayed_ref_exit();
26559247f317SMiao Xie 	btrfs_auto_defrag_exit();
265616cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
2657b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
26586352b91dSMiao Xie 	ordered_data_exit();
2659a52d9a80SChris Mason 	extent_map_exit();
26606f0d04f8SJosef Bacik 	extent_state_cache_exit();
2661d1310b2eSChris Mason 	extent_io_exit();
2662a9218f6bSChris Mason 	btrfs_interface_exit();
26635ed5f588SJosef Bacik 	btrfs_end_io_wq_exit();
26642e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
266558176a96SJosef Bacik 	btrfs_exit_sysfs();
26668a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
2667261507a0SLi Zefan 	btrfs_exit_compress();
26682e635a27SChris Mason }
26692e635a27SChris Mason 
267060efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs);
26712e635a27SChris Mason module_exit(exit_btrfs_fs)
26722e635a27SChris Mason 
26732e635a27SChris Mason MODULE_LICENSE("GPL");
2674d5178578SJohannes Thumshirn MODULE_SOFTDEP("pre: crc32c");
26753951e7f0SJohannes Thumshirn MODULE_SOFTDEP("pre: xxhash64");
26763831bf00SJohannes Thumshirn MODULE_SOFTDEP("pre: sha256");
2677352ae07bSDavid Sterba MODULE_SOFTDEP("pre: blake2b-256");
2678