xref: /linux/fs/btrfs/super.c (revision c7f13d428ea1bfe883f2741a9b5a5352d595eb09)
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"
51b8bea09aSQu Wenruo #include "raid56.h"
52*c7f13d42SJosef Bacik #include "fs.h"
531abe9b8aSliubo #define CREATE_TRACE_POINTS
541abe9b8aSliubo #include <trace/events/btrfs.h>
551abe9b8aSliubo 
56b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
5772fa39f5SMisono, Tomohiro 
5872fa39f5SMisono, Tomohiro /*
5972fa39f5SMisono, Tomohiro  * Types for mounting the default subvolume and a subvolume explicitly
6072fa39f5SMisono, Tomohiro  * requested by subvol=/path. That way the callchain is straightforward and we
6172fa39f5SMisono, Tomohiro  * don't have to play tricks with the mount options and recursive calls to
6272fa39f5SMisono, Tomohiro  * btrfs_mount.
63312c89fbSMisono, Tomohiro  *
64312c89fbSMisono, Tomohiro  * The new btrfs_root_fs_type also servers as a tag for the bdev_holder.
6572fa39f5SMisono, Tomohiro  */
66830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type;
6772fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type;
68e20d96d6SChris Mason 
690723a047SHarald Hoyer static int btrfs_remount(struct super_block *sb, int *flags, char *data);
700723a047SHarald Hoyer 
71c067da87SSweet Tea Dorminy #ifdef CONFIG_PRINTK
72c067da87SSweet Tea Dorminy 
73c067da87SSweet Tea Dorminy #define STATE_STRING_PREFACE	": state "
74c067da87SSweet Tea Dorminy #define STATE_STRING_BUF_LEN	(sizeof(STATE_STRING_PREFACE) + BTRFS_FS_STATE_COUNT)
75c067da87SSweet Tea Dorminy 
76c067da87SSweet Tea Dorminy /*
77143823cfSDavid Sterba  * Characters to print to indicate error conditions or uncommon filesystem state.
78c067da87SSweet Tea Dorminy  * RO is not an error.
79c067da87SSweet Tea Dorminy  */
80c067da87SSweet Tea Dorminy static const char fs_state_chars[] = {
81c067da87SSweet Tea Dorminy 	[BTRFS_FS_STATE_ERROR]			= 'E',
82c067da87SSweet Tea Dorminy 	[BTRFS_FS_STATE_REMOUNTING]		= 'M',
83c067da87SSweet Tea Dorminy 	[BTRFS_FS_STATE_RO]			= 0,
84c067da87SSweet Tea Dorminy 	[BTRFS_FS_STATE_TRANS_ABORTED]		= 'A',
85c067da87SSweet Tea Dorminy 	[BTRFS_FS_STATE_DEV_REPLACING]		= 'R',
86c067da87SSweet Tea Dorminy 	[BTRFS_FS_STATE_DUMMY_FS_INFO]		= 0,
87c067da87SSweet Tea Dorminy 	[BTRFS_FS_STATE_NO_CSUMS]		= 'C',
88c067da87SSweet Tea Dorminy 	[BTRFS_FS_STATE_LOG_CLEANUP_ERROR]	= 'L',
89c067da87SSweet Tea Dorminy };
90c067da87SSweet Tea Dorminy 
91c067da87SSweet Tea Dorminy static void btrfs_state_to_string(const struct btrfs_fs_info *info, char *buf)
92c067da87SSweet Tea Dorminy {
93c067da87SSweet Tea Dorminy 	unsigned int bit;
94c067da87SSweet Tea Dorminy 	bool states_printed = false;
95c067da87SSweet Tea Dorminy 	unsigned long fs_state = READ_ONCE(info->fs_state);
96c067da87SSweet Tea Dorminy 	char *curr = buf;
97c067da87SSweet Tea Dorminy 
98c067da87SSweet Tea Dorminy 	memcpy(curr, STATE_STRING_PREFACE, sizeof(STATE_STRING_PREFACE));
99c067da87SSweet Tea Dorminy 	curr += sizeof(STATE_STRING_PREFACE) - 1;
100c067da87SSweet Tea Dorminy 
101c067da87SSweet Tea Dorminy 	for_each_set_bit(bit, &fs_state, sizeof(fs_state)) {
102c067da87SSweet Tea Dorminy 		WARN_ON_ONCE(bit >= BTRFS_FS_STATE_COUNT);
103c067da87SSweet Tea Dorminy 		if ((bit < BTRFS_FS_STATE_COUNT) && fs_state_chars[bit]) {
104c067da87SSweet Tea Dorminy 			*curr++ = fs_state_chars[bit];
105c067da87SSweet Tea Dorminy 			states_printed = true;
106c067da87SSweet Tea Dorminy 		}
107c067da87SSweet Tea Dorminy 	}
108c067da87SSweet Tea Dorminy 
109c067da87SSweet Tea Dorminy 	/* If no states were printed, reset the buffer */
110c067da87SSweet Tea Dorminy 	if (!states_printed)
111c067da87SSweet Tea Dorminy 		curr = buf;
112c067da87SSweet Tea Dorminy 
113c067da87SSweet Tea Dorminy 	*curr++ = 0;
114c067da87SSweet Tea Dorminy }
115c067da87SSweet Tea Dorminy #endif
116c067da87SSweet Tea Dorminy 
11759131393SJosef Bacik /*
11859131393SJosef Bacik  * Generally the error codes correspond to their respective errors, but there
11959131393SJosef Bacik  * are a few special cases.
12059131393SJosef Bacik  *
12159131393SJosef Bacik  * EUCLEAN: Any sort of corruption that we encounter.  The tree-checker for
12259131393SJosef Bacik  *          instance will return EUCLEAN if any of the blocks are corrupted in
12359131393SJosef Bacik  *          a way that is problematic.  We want to reserve EUCLEAN for these
12459131393SJosef Bacik  *          sort of corruptions.
12559131393SJosef Bacik  *
12659131393SJosef Bacik  * EROFS: If we check BTRFS_FS_STATE_ERROR and fail out with a return error, we
12759131393SJosef Bacik  *        need to use EROFS for this case.  We will have no idea of the
12859131393SJosef Bacik  *        original failure, that will have been reported at the time we tripped
12959131393SJosef Bacik  *        over the error.  Each subsequent error that doesn't have any context
13059131393SJosef Bacik  *        of the original error should use EROFS when handling BTRFS_FS_STATE_ERROR.
13159131393SJosef Bacik  */
1324143cb8bSDavid Sterba const char * __attribute_const__ btrfs_decode_error(int errno)
133acce952bSliubo {
13408748810SDavid Sterba 	char *errstr = "unknown";
135acce952bSliubo 
136acce952bSliubo 	switch (errno) {
137d54f8144SDavid Sterba 	case -ENOENT:		/* -2 */
138d54f8144SDavid Sterba 		errstr = "No such entry";
139d54f8144SDavid Sterba 		break;
140d54f8144SDavid Sterba 	case -EIO:		/* -5 */
141acce952bSliubo 		errstr = "IO failure";
142acce952bSliubo 		break;
143d54f8144SDavid Sterba 	case -ENOMEM:		/* -12*/
144acce952bSliubo 		errstr = "Out of memory";
145acce952bSliubo 		break;
146d54f8144SDavid Sterba 	case -EEXIST:		/* -17 */
1478c342930SJeff Mahoney 		errstr = "Object already exists";
1488c342930SJeff Mahoney 		break;
149d54f8144SDavid Sterba 	case -ENOSPC:		/* -28 */
15094ef7280SDavid Sterba 		errstr = "No space left";
15194ef7280SDavid Sterba 		break;
152d54f8144SDavid Sterba 	case -EROFS:		/* -30 */
153d54f8144SDavid Sterba 		errstr = "Readonly filesystem";
15494ef7280SDavid Sterba 		break;
155fb8521caSDavid Sterba 	case -EOPNOTSUPP:	/* -95 */
156fb8521caSDavid Sterba 		errstr = "Operation not supported";
157fb8521caSDavid Sterba 		break;
158fb8521caSDavid Sterba 	case -EUCLEAN:		/* -117 */
159fb8521caSDavid Sterba 		errstr = "Filesystem corrupted";
160fb8521caSDavid Sterba 		break;
161fb8521caSDavid Sterba 	case -EDQUOT:		/* -122 */
162fb8521caSDavid Sterba 		errstr = "Quota exceeded";
163fb8521caSDavid Sterba 		break;
164acce952bSliubo 	}
165acce952bSliubo 
166acce952bSliubo 	return errstr;
167acce952bSliubo }
168acce952bSliubo 
169acce952bSliubo /*
17034d97007SAnand Jain  * __btrfs_handle_fs_error decodes expected errors from the caller and
17152042d8eSAndrea Gelmini  * invokes the appropriate error response.
172acce952bSliubo  */
173c0d19e2bSDavid Sterba __cold
17434d97007SAnand Jain void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
1754da35113SJeff Mahoney 		       unsigned int line, int errno, const char *fmt, ...)
176acce952bSliubo {
177acce952bSliubo 	struct super_block *sb = fs_info->sb;
17857d816a1SAnand Jain #ifdef CONFIG_PRINTK
179c067da87SSweet Tea Dorminy 	char statestr[STATE_STRING_BUF_LEN];
180acce952bSliubo 	const char *errstr;
18157d816a1SAnand Jain #endif
182acce952bSliubo 
183acce952bSliubo 	/*
184acce952bSliubo 	 * Special case: if the error is EROFS, and we're already
1851751e8a6SLinus Torvalds 	 * under SB_RDONLY, then it is safe here.
186acce952bSliubo 	 */
187bc98a42cSDavid Howells 	if (errno == -EROFS && sb_rdonly(sb))
188acce952bSliubo   		return;
189acce952bSliubo 
19057d816a1SAnand Jain #ifdef CONFIG_PRINTK
19108748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
192c067da87SSweet Tea Dorminy 	btrfs_state_to_string(fs_info, statestr);
1934da35113SJeff Mahoney 	if (fmt) {
19437252a66SEric Sandeen 		struct va_format vaf;
19537252a66SEric Sandeen 		va_list args;
19637252a66SEric Sandeen 
19737252a66SEric Sandeen 		va_start(args, fmt);
19837252a66SEric Sandeen 		vaf.fmt = fmt;
19937252a66SEric Sandeen 		vaf.va = &args;
2004da35113SJeff Mahoney 
201c067da87SSweet Tea Dorminy 		pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s (%pV)\n",
202c067da87SSweet Tea Dorminy 			sb->s_id, statestr, function, line, errno, errstr, &vaf);
20337252a66SEric Sandeen 		va_end(args);
2044da35113SJeff Mahoney 	} else {
205c067da87SSweet Tea Dorminy 		pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s\n",
206c067da87SSweet Tea Dorminy 			sb->s_id, statestr, function, line, errno, errstr);
2074da35113SJeff Mahoney 	}
20857d816a1SAnand Jain #endif
209acce952bSliubo 
2100713d90cSAnand Jain 	/*
2110713d90cSAnand Jain 	 * Today we only save the error info to memory.  Long term we'll
2120713d90cSAnand Jain 	 * also send it down to the disk
2130713d90cSAnand Jain 	 */
2140713d90cSAnand Jain 	set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
2150713d90cSAnand Jain 
2164da35113SJeff Mahoney 	/* Don't go through full error handling during mount */
217922ea899SAnand Jain 	if (!(sb->s_flags & SB_BORN))
218922ea899SAnand Jain 		return;
219922ea899SAnand Jain 
220922ea899SAnand Jain 	if (sb_rdonly(sb))
221922ea899SAnand Jain 		return;
222922ea899SAnand Jain 
223b0643e59SDennis Zhou 	btrfs_discard_stop(fs_info);
224b0643e59SDennis Zhou 
225922ea899SAnand Jain 	/* btrfs handle error by forcing the filesystem readonly */
226a0a1db70SFilipe Manana 	btrfs_set_sb_rdonly(sb);
227922ea899SAnand Jain 	btrfs_info(fs_info, "forced readonly");
228922ea899SAnand Jain 	/*
229922ea899SAnand Jain 	 * Note that a running device replace operation is not canceled here
230922ea899SAnand Jain 	 * although there is no way to update the progress. It would add the
231922ea899SAnand Jain 	 * risk of a deadlock, therefore the canceling is omitted. The only
232922ea899SAnand Jain 	 * penalty is that some I/O remains active until the procedure
23352042d8eSAndrea Gelmini 	 * completes. The next time when the filesystem is mounted writable
234922ea899SAnand Jain 	 * again, the device replace operation continues.
235922ea899SAnand Jain 	 */
236acce952bSliubo }
2374da35113SJeff Mahoney 
23857d816a1SAnand Jain #ifdef CONFIG_PRINTK
239533574c6SJoe Perches static const char * const logtypes[] = {
2404da35113SJeff Mahoney 	"emergency",
2414da35113SJeff Mahoney 	"alert",
2424da35113SJeff Mahoney 	"critical",
2434da35113SJeff Mahoney 	"error",
2444da35113SJeff Mahoney 	"warning",
2454da35113SJeff Mahoney 	"notice",
2464da35113SJeff Mahoney 	"info",
2474da35113SJeff Mahoney 	"debug",
2484da35113SJeff Mahoney };
2494da35113SJeff Mahoney 
25035f4e5e6SNikolay Borisov 
25135f4e5e6SNikolay Borisov /*
25235f4e5e6SNikolay Borisov  * Use one ratelimit state per log level so that a flood of less important
25335f4e5e6SNikolay Borisov  * messages doesn't cause more important ones to be dropped.
25435f4e5e6SNikolay Borisov  */
25535f4e5e6SNikolay Borisov static struct ratelimit_state printk_limits[] = {
25635f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100),
25735f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100),
25835f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100),
25935f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100),
26035f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100),
26135f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100),
26235f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100),
26335f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100),
26435f4e5e6SNikolay Borisov };
26535f4e5e6SNikolay Borisov 
266b0a66a31SJonathan Lassoff void __cold _btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
2674da35113SJeff Mahoney {
26840f7828bSPetr Mladek 	char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0";
2694da35113SJeff Mahoney 	struct va_format vaf;
2704da35113SJeff Mahoney 	va_list args;
271533574c6SJoe Perches 	int kern_level;
27240f7828bSPetr Mladek 	const char *type = logtypes[4];
27340f7828bSPetr Mladek 	struct ratelimit_state *ratelimit = &printk_limits[4];
2744da35113SJeff Mahoney 
2754da35113SJeff Mahoney 	va_start(args, fmt);
2764da35113SJeff Mahoney 
277262c5e86SPetr Mladek 	while ((kern_level = printk_get_level(fmt)) != 0) {
278533574c6SJoe Perches 		size_t size = printk_skip_level(fmt) - fmt;
279262c5e86SPetr Mladek 
280262c5e86SPetr Mladek 		if (kern_level >= '0' && kern_level <= '7') {
281533574c6SJoe Perches 			memcpy(lvl, fmt,  size);
282533574c6SJoe Perches 			lvl[size] = '\0';
283533574c6SJoe Perches 			type = logtypes[kern_level - '0'];
28435f4e5e6SNikolay Borisov 			ratelimit = &printk_limits[kern_level - '0'];
285262c5e86SPetr Mladek 		}
286262c5e86SPetr Mladek 		fmt += size;
287262c5e86SPetr Mladek 	}
288262c5e86SPetr Mladek 
2894da35113SJeff Mahoney 	vaf.fmt = fmt;
2904da35113SJeff Mahoney 	vaf.va = &args;
291533574c6SJoe Perches 
292a0f6d924SDavid Sterba 	if (__ratelimit(ratelimit)) {
293c067da87SSweet Tea Dorminy 		if (fs_info) {
294c067da87SSweet Tea Dorminy 			char statestr[STATE_STRING_BUF_LEN];
295c067da87SSweet Tea Dorminy 
296c067da87SSweet Tea Dorminy 			btrfs_state_to_string(fs_info, statestr);
297b0a66a31SJonathan Lassoff 			_printk("%sBTRFS %s (device %s%s): %pV\n", lvl, type,
298c067da87SSweet Tea Dorminy 				fs_info->sb->s_id, statestr, &vaf);
299c067da87SSweet Tea Dorminy 		} else {
300b0a66a31SJonathan Lassoff 			_printk("%sBTRFS %s: %pV\n", lvl, type, &vaf);
301a0f6d924SDavid Sterba 		}
302c067da87SSweet Tea Dorminy 	}
303533574c6SJoe Perches 
304533574c6SJoe Perches 	va_end(args);
3054da35113SJeff Mahoney }
306533574c6SJoe Perches #endif
307533574c6SJoe Perches 
308e9306ad4SQu Wenruo #if BITS_PER_LONG == 32
309e9306ad4SQu Wenruo void __cold btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info)
310e9306ad4SQu Wenruo {
311e9306ad4SQu Wenruo 	if (!test_and_set_bit(BTRFS_FS_32BIT_WARN, &fs_info->flags)) {
312e9306ad4SQu Wenruo 		btrfs_warn(fs_info, "reaching 32bit limit for logical addresses");
313e9306ad4SQu Wenruo 		btrfs_warn(fs_info,
314e9306ad4SQu Wenruo "due to page cache limit on 32bit systems, btrfs can't access metadata at or beyond %lluT",
315e9306ad4SQu Wenruo 			   BTRFS_32BIT_MAX_FILE_SIZE >> 40);
316e9306ad4SQu Wenruo 		btrfs_warn(fs_info,
317e9306ad4SQu Wenruo 			   "please consider upgrading to 64bit kernel/hardware");
318e9306ad4SQu Wenruo 	}
319e9306ad4SQu Wenruo }
320e9306ad4SQu Wenruo 
321e9306ad4SQu Wenruo void __cold btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info)
322e9306ad4SQu Wenruo {
323e9306ad4SQu Wenruo 	if (!test_and_set_bit(BTRFS_FS_32BIT_ERROR, &fs_info->flags)) {
324e9306ad4SQu Wenruo 		btrfs_err(fs_info, "reached 32bit limit for logical addresses");
325e9306ad4SQu Wenruo 		btrfs_err(fs_info,
326e9306ad4SQu Wenruo "due to page cache limit on 32bit systems, metadata beyond %lluT can't be accessed",
327e9306ad4SQu Wenruo 			  BTRFS_32BIT_MAX_FILE_SIZE >> 40);
328e9306ad4SQu Wenruo 		btrfs_err(fs_info,
329e9306ad4SQu Wenruo 			   "please consider upgrading to 64bit kernel/hardware");
330e9306ad4SQu Wenruo 	}
331e9306ad4SQu Wenruo }
332e9306ad4SQu Wenruo #endif
333e9306ad4SQu Wenruo 
3348c342930SJeff Mahoney /*
33549b25e05SJeff Mahoney  * We only mark the transaction aborted and then set the file system read-only.
33649b25e05SJeff Mahoney  * This will prevent new transactions from starting or trying to join this
33749b25e05SJeff Mahoney  * one.
33849b25e05SJeff Mahoney  *
33949b25e05SJeff Mahoney  * This means that error recovery at the call site is limited to freeing
34049b25e05SJeff Mahoney  * any local memory allocations and passing the error code up without
34149b25e05SJeff Mahoney  * further cleanup. The transaction should complete as it normally would
34249b25e05SJeff Mahoney  * in the call path but will return -EIO.
34349b25e05SJeff Mahoney  *
34449b25e05SJeff Mahoney  * We'll complete the cleanup in btrfs_end_transaction and
34549b25e05SJeff Mahoney  * btrfs_commit_transaction.
34649b25e05SJeff Mahoney  */
347c0d19e2bSDavid Sterba __cold
34849b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
34966642832SJeff Mahoney 			       const char *function,
3508e327b9cSQu Wenruo 			       unsigned int line, int errno, bool first_hit)
35149b25e05SJeff Mahoney {
35266642832SJeff Mahoney 	struct btrfs_fs_info *fs_info = trans->fs_info;
35366642832SJeff Mahoney 
354bf31f87fSDavid Sterba 	WRITE_ONCE(trans->aborted, errno);
35520c7bcecSSeraphime Kirkovski 	WRITE_ONCE(trans->transaction->aborted, errno);
3568e327b9cSQu Wenruo 	if (first_hit && errno == -ENOSPC)
3578e327b9cSQu Wenruo 		btrfs_dump_space_info_for_trans_abort(fs_info);
358501407aaSJosef Bacik 	/* Wake up anybody who may be waiting on this transaction */
35966642832SJeff Mahoney 	wake_up(&fs_info->transaction_wait);
36066642832SJeff Mahoney 	wake_up(&fs_info->transaction_blocked_wait);
36166642832SJeff Mahoney 	__btrfs_handle_fs_error(fs_info, function, line, errno, NULL);
36249b25e05SJeff Mahoney }
36349b25e05SJeff Mahoney /*
3648c342930SJeff Mahoney  * __btrfs_panic decodes unexpected, fatal errors from the caller,
3658c342930SJeff Mahoney  * issues an alert, and either panics or BUGs, depending on mount options.
3668c342930SJeff Mahoney  */
367c0d19e2bSDavid Sterba __cold
3688c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
3698c342930SJeff Mahoney 		   unsigned int line, int errno, const char *fmt, ...)
3708c342930SJeff Mahoney {
3718c342930SJeff Mahoney 	char *s_id = "<unknown>";
3728c342930SJeff Mahoney 	const char *errstr;
3738c342930SJeff Mahoney 	struct va_format vaf = { .fmt = fmt };
3748c342930SJeff Mahoney 	va_list args;
3758c342930SJeff Mahoney 
3768c342930SJeff Mahoney 	if (fs_info)
3778c342930SJeff Mahoney 		s_id = fs_info->sb->s_id;
3788c342930SJeff Mahoney 
3798c342930SJeff Mahoney 	va_start(args, fmt);
3808c342930SJeff Mahoney 	vaf.va = &args;
3818c342930SJeff Mahoney 
38208748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
383d8953d69SSatoru Takeuchi 	if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR)))
38408748810SDavid Sterba 		panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
38508748810SDavid Sterba 			s_id, function, line, &vaf, errno, errstr);
3868c342930SJeff Mahoney 
387efe120a0SFrank Holton 	btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
388efe120a0SFrank Holton 		   function, line, &vaf, errno, errstr);
3898c342930SJeff Mahoney 	va_end(args);
3908c342930SJeff Mahoney 	/* Caller calls BUG() */
3918c342930SJeff Mahoney }
392acce952bSliubo 
393e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
394e20d96d6SChris Mason {
3956bccf3abSJeff Mahoney 	close_ctree(btrfs_sb(sb));
396e20d96d6SChris Mason }
3972e635a27SChris Mason 
39895e05289SChris Mason enum {
399416a7202SDavid Sterba 	Opt_acl, Opt_noacl,
400416a7202SDavid Sterba 	Opt_clear_cache,
401416a7202SDavid Sterba 	Opt_commit_interval,
402416a7202SDavid Sterba 	Opt_compress,
403416a7202SDavid Sterba 	Opt_compress_force,
404416a7202SDavid Sterba 	Opt_compress_force_type,
405416a7202SDavid Sterba 	Opt_compress_type,
406416a7202SDavid Sterba 	Opt_degraded,
407416a7202SDavid Sterba 	Opt_device,
408416a7202SDavid Sterba 	Opt_fatal_errors,
409416a7202SDavid Sterba 	Opt_flushoncommit, Opt_noflushoncommit,
410416a7202SDavid Sterba 	Opt_max_inline,
411416a7202SDavid Sterba 	Opt_barrier, Opt_nobarrier,
412416a7202SDavid Sterba 	Opt_datacow, Opt_nodatacow,
413416a7202SDavid Sterba 	Opt_datasum, Opt_nodatasum,
414416a7202SDavid Sterba 	Opt_defrag, Opt_nodefrag,
415416a7202SDavid Sterba 	Opt_discard, Opt_nodiscard,
416b0643e59SDennis Zhou 	Opt_discard_mode,
417416a7202SDavid Sterba 	Opt_norecovery,
418416a7202SDavid Sterba 	Opt_ratio,
419416a7202SDavid Sterba 	Opt_rescan_uuid_tree,
420416a7202SDavid Sterba 	Opt_skip_balance,
421416a7202SDavid Sterba 	Opt_space_cache, Opt_no_space_cache,
422416a7202SDavid Sterba 	Opt_space_cache_version,
423416a7202SDavid Sterba 	Opt_ssd, Opt_nossd,
424416a7202SDavid Sterba 	Opt_ssd_spread, Opt_nossd_spread,
425416a7202SDavid Sterba 	Opt_subvol,
42637becec9SOmar Sandoval 	Opt_subvol_empty,
427416a7202SDavid Sterba 	Opt_subvolid,
428416a7202SDavid Sterba 	Opt_thread_pool,
429416a7202SDavid Sterba 	Opt_treelog, Opt_notreelog,
430416a7202SDavid Sterba 	Opt_user_subvol_rm_allowed,
431416a7202SDavid Sterba 
43274ef0018SQu Wenruo 	/* Rescue options */
43374ef0018SQu Wenruo 	Opt_rescue,
43474ef0018SQu Wenruo 	Opt_usebackuproot,
43574ef0018SQu Wenruo 	Opt_nologreplay,
43642437a63SJosef Bacik 	Opt_ignorebadroots,
437882dbe0cSJosef Bacik 	Opt_ignoredatacsums,
4389037d3cbSJosef Bacik 	Opt_rescue_all,
43974ef0018SQu Wenruo 
440416a7202SDavid Sterba 	/* Deprecated options */
441416a7202SDavid Sterba 	Opt_recovery,
4425297199aSNikolay Borisov 	Opt_inode_cache, Opt_noinode_cache,
443416a7202SDavid Sterba 
444416a7202SDavid Sterba 	/* Debugging options */
445416a7202SDavid Sterba 	Opt_check_integrity,
44670f6d82eSOmar Sandoval 	Opt_check_integrity_including_extent_data,
447416a7202SDavid Sterba 	Opt_check_integrity_print_mask,
448416a7202SDavid Sterba 	Opt_enospc_debug, Opt_noenospc_debug,
449d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
450d0bd4560SJosef Bacik 	Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
451d0bd4560SJosef Bacik #endif
452fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
453fb592373SJosef Bacik 	Opt_ref_verify,
454fb592373SJosef Bacik #endif
4559555c6c1SIlya Dryomov 	Opt_err,
45695e05289SChris Mason };
45795e05289SChris Mason 
4584d4ab6d6SDavid Sterba static const match_table_t tokens = {
459416a7202SDavid Sterba 	{Opt_acl, "acl"},
460416a7202SDavid Sterba 	{Opt_noacl, "noacl"},
461416a7202SDavid Sterba 	{Opt_clear_cache, "clear_cache"},
462416a7202SDavid Sterba 	{Opt_commit_interval, "commit=%u"},
463c8b97818SChris Mason 	{Opt_compress, "compress"},
464261507a0SLi Zefan 	{Opt_compress_type, "compress=%s"},
465a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
466261507a0SLi Zefan 	{Opt_compress_force_type, "compress-force=%s"},
467416a7202SDavid Sterba 	{Opt_degraded, "degraded"},
468416a7202SDavid Sterba 	{Opt_device, "device=%s"},
469416a7202SDavid Sterba 	{Opt_fatal_errors, "fatal_errors=%s"},
470dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
4712c9ee856SQu Wenruo 	{Opt_noflushoncommit, "noflushoncommit"},
4724b9465cbSChris Mason 	{Opt_inode_cache, "inode_cache"},
4733818aea2SQu Wenruo 	{Opt_noinode_cache, "noinode_cache"},
474416a7202SDavid Sterba 	{Opt_max_inline, "max_inline=%s"},
475416a7202SDavid Sterba 	{Opt_barrier, "barrier"},
476416a7202SDavid Sterba 	{Opt_nobarrier, "nobarrier"},
477416a7202SDavid Sterba 	{Opt_datacow, "datacow"},
478416a7202SDavid Sterba 	{Opt_nodatacow, "nodatacow"},
479416a7202SDavid Sterba 	{Opt_datasum, "datasum"},
480416a7202SDavid Sterba 	{Opt_nodatasum, "nodatasum"},
481416a7202SDavid Sterba 	{Opt_defrag, "autodefrag"},
482416a7202SDavid Sterba 	{Opt_nodefrag, "noautodefrag"},
483416a7202SDavid Sterba 	{Opt_discard, "discard"},
484b0643e59SDennis Zhou 	{Opt_discard_mode, "discard=%s"},
485416a7202SDavid Sterba 	{Opt_nodiscard, "nodiscard"},
486416a7202SDavid Sterba 	{Opt_norecovery, "norecovery"},
487416a7202SDavid Sterba 	{Opt_ratio, "metadata_ratio=%u"},
488416a7202SDavid Sterba 	{Opt_rescan_uuid_tree, "rescan_uuid_tree"},
4899555c6c1SIlya Dryomov 	{Opt_skip_balance, "skip_balance"},
490416a7202SDavid Sterba 	{Opt_space_cache, "space_cache"},
491416a7202SDavid Sterba 	{Opt_no_space_cache, "nospace_cache"},
492416a7202SDavid Sterba 	{Opt_space_cache_version, "space_cache=%s"},
493416a7202SDavid Sterba 	{Opt_ssd, "ssd"},
494416a7202SDavid Sterba 	{Opt_nossd, "nossd"},
495416a7202SDavid Sterba 	{Opt_ssd_spread, "ssd_spread"},
496416a7202SDavid Sterba 	{Opt_nossd_spread, "nossd_spread"},
497416a7202SDavid Sterba 	{Opt_subvol, "subvol=%s"},
49837becec9SOmar Sandoval 	{Opt_subvol_empty, "subvol="},
499416a7202SDavid Sterba 	{Opt_subvolid, "subvolid=%s"},
500416a7202SDavid Sterba 	{Opt_thread_pool, "thread_pool=%u"},
501416a7202SDavid Sterba 	{Opt_treelog, "treelog"},
502416a7202SDavid Sterba 	{Opt_notreelog, "notreelog"},
503416a7202SDavid Sterba 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
504416a7202SDavid Sterba 
50574ef0018SQu Wenruo 	/* Rescue options */
50674ef0018SQu Wenruo 	{Opt_rescue, "rescue=%s"},
50774ef0018SQu Wenruo 	/* Deprecated, with alias rescue=nologreplay */
50874ef0018SQu Wenruo 	{Opt_nologreplay, "nologreplay"},
50974ef0018SQu Wenruo 	/* Deprecated, with alias rescue=usebackuproot */
51074ef0018SQu Wenruo 	{Opt_usebackuproot, "usebackuproot"},
51174ef0018SQu Wenruo 
512416a7202SDavid Sterba 	/* Deprecated options */
513416a7202SDavid Sterba 	{Opt_recovery, "recovery"},
514416a7202SDavid Sterba 
515416a7202SDavid Sterba 	/* Debugging options */
51621adbd5cSStefan Behrens 	{Opt_check_integrity, "check_int"},
51721adbd5cSStefan Behrens 	{Opt_check_integrity_including_extent_data, "check_int_data"},
51802453bdeSAnand Jain 	{Opt_check_integrity_print_mask, "check_int_print_mask=%u"},
519416a7202SDavid Sterba 	{Opt_enospc_debug, "enospc_debug"},
520416a7202SDavid Sterba 	{Opt_noenospc_debug, "noenospc_debug"},
521d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
522d0bd4560SJosef Bacik 	{Opt_fragment_data, "fragment=data"},
523d0bd4560SJosef Bacik 	{Opt_fragment_metadata, "fragment=metadata"},
524d0bd4560SJosef Bacik 	{Opt_fragment_all, "fragment=all"},
525d0bd4560SJosef Bacik #endif
526fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
527fb592373SJosef Bacik 	{Opt_ref_verify, "ref_verify"},
528fb592373SJosef Bacik #endif
52933268eafSJosef Bacik 	{Opt_err, NULL},
53095e05289SChris Mason };
53195e05289SChris Mason 
53274ef0018SQu Wenruo static const match_table_t rescue_tokens = {
53374ef0018SQu Wenruo 	{Opt_usebackuproot, "usebackuproot"},
53474ef0018SQu Wenruo 	{Opt_nologreplay, "nologreplay"},
53542437a63SJosef Bacik 	{Opt_ignorebadroots, "ignorebadroots"},
53642437a63SJosef Bacik 	{Opt_ignorebadroots, "ibadroots"},
537882dbe0cSJosef Bacik 	{Opt_ignoredatacsums, "ignoredatacsums"},
538882dbe0cSJosef Bacik 	{Opt_ignoredatacsums, "idatacsums"},
5399037d3cbSJosef Bacik 	{Opt_rescue_all, "all"},
54074ef0018SQu Wenruo 	{Opt_err, NULL},
54174ef0018SQu Wenruo };
54274ef0018SQu Wenruo 
543d70bf748SJosef Bacik static bool check_ro_option(struct btrfs_fs_info *fs_info, unsigned long opt,
544d70bf748SJosef Bacik 			    const char *opt_name)
545d70bf748SJosef Bacik {
546d70bf748SJosef Bacik 	if (fs_info->mount_opt & opt) {
547d70bf748SJosef Bacik 		btrfs_err(fs_info, "%s must be used with ro mount option",
548d70bf748SJosef Bacik 			  opt_name);
549d70bf748SJosef Bacik 		return true;
550d70bf748SJosef Bacik 	}
551d70bf748SJosef Bacik 	return false;
552d70bf748SJosef Bacik }
553d70bf748SJosef Bacik 
55474ef0018SQu Wenruo static int parse_rescue_options(struct btrfs_fs_info *info, const char *options)
55574ef0018SQu Wenruo {
55674ef0018SQu Wenruo 	char *opts;
55774ef0018SQu Wenruo 	char *orig;
55874ef0018SQu Wenruo 	char *p;
55974ef0018SQu Wenruo 	substring_t args[MAX_OPT_ARGS];
56074ef0018SQu Wenruo 	int ret = 0;
56174ef0018SQu Wenruo 
56274ef0018SQu Wenruo 	opts = kstrdup(options, GFP_KERNEL);
56374ef0018SQu Wenruo 	if (!opts)
56474ef0018SQu Wenruo 		return -ENOMEM;
56574ef0018SQu Wenruo 	orig = opts;
56674ef0018SQu Wenruo 
56774ef0018SQu Wenruo 	while ((p = strsep(&opts, ":")) != NULL) {
56874ef0018SQu Wenruo 		int token;
56974ef0018SQu Wenruo 
57074ef0018SQu Wenruo 		if (!*p)
57174ef0018SQu Wenruo 			continue;
57274ef0018SQu Wenruo 		token = match_token(p, rescue_tokens, args);
57374ef0018SQu Wenruo 		switch (token){
57474ef0018SQu Wenruo 		case Opt_usebackuproot:
57574ef0018SQu Wenruo 			btrfs_info(info,
57674ef0018SQu Wenruo 				   "trying to use backup root at mount time");
57774ef0018SQu Wenruo 			btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
57874ef0018SQu Wenruo 			break;
57974ef0018SQu Wenruo 		case Opt_nologreplay:
58074ef0018SQu Wenruo 			btrfs_set_and_info(info, NOLOGREPLAY,
58174ef0018SQu Wenruo 					   "disabling log replay at mount time");
58274ef0018SQu Wenruo 			break;
58342437a63SJosef Bacik 		case Opt_ignorebadroots:
58442437a63SJosef Bacik 			btrfs_set_and_info(info, IGNOREBADROOTS,
58542437a63SJosef Bacik 					   "ignoring bad roots");
58642437a63SJosef Bacik 			break;
587882dbe0cSJosef Bacik 		case Opt_ignoredatacsums:
588882dbe0cSJosef Bacik 			btrfs_set_and_info(info, IGNOREDATACSUMS,
589882dbe0cSJosef Bacik 					   "ignoring data csums");
590882dbe0cSJosef Bacik 			break;
5919037d3cbSJosef Bacik 		case Opt_rescue_all:
5929037d3cbSJosef Bacik 			btrfs_info(info, "enabling all of the rescue options");
5939037d3cbSJosef Bacik 			btrfs_set_and_info(info, IGNOREDATACSUMS,
5949037d3cbSJosef Bacik 					   "ignoring data csums");
5959037d3cbSJosef Bacik 			btrfs_set_and_info(info, IGNOREBADROOTS,
5969037d3cbSJosef Bacik 					   "ignoring bad roots");
5979037d3cbSJosef Bacik 			btrfs_set_and_info(info, NOLOGREPLAY,
5989037d3cbSJosef Bacik 					   "disabling log replay at mount time");
5999037d3cbSJosef Bacik 			break;
60074ef0018SQu Wenruo 		case Opt_err:
60174ef0018SQu Wenruo 			btrfs_info(info, "unrecognized rescue option '%s'", p);
60274ef0018SQu Wenruo 			ret = -EINVAL;
60374ef0018SQu Wenruo 			goto out;
60474ef0018SQu Wenruo 		default:
60574ef0018SQu Wenruo 			break;
60674ef0018SQu Wenruo 		}
60774ef0018SQu Wenruo 
60874ef0018SQu Wenruo 	}
60974ef0018SQu Wenruo out:
61074ef0018SQu Wenruo 	kfree(orig);
61174ef0018SQu Wenruo 	return ret;
61274ef0018SQu Wenruo }
61374ef0018SQu Wenruo 
614edf24abeSChristoph Hellwig /*
615edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
616edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
61749b25e05SJeff Mahoney  * XXX JDM: This needs to be cleaned up for remount.
618edf24abeSChristoph Hellwig  */
6192ff7e61eSJeff Mahoney int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
62096da0919SQu Wenruo 			unsigned long new_flags)
62195e05289SChris Mason {
62295e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
623e215772cSMisono, Tomohiro 	char *p, *num;
6244543df7eSChris Mason 	int intarg;
625a7a3f7caSSage Weil 	int ret = 0;
626261507a0SLi Zefan 	char *compress_type;
627261507a0SLi Zefan 	bool compress_force = false;
628b7c47bbbSTsutomu Itoh 	enum btrfs_compression_type saved_compress_type;
62927942c99SDavid Sterba 	int saved_compress_level;
630b7c47bbbSTsutomu Itoh 	bool saved_compress_force;
631b7c47bbbSTsutomu Itoh 	int no_compress = 0;
632dbecac26SMaciej S. Szmigiero 	const bool remounting = test_bit(BTRFS_FS_STATE_REMOUNTING, &info->fs_state);
633b6cda9bcSChris Mason 
6340b246afaSJeff Mahoney 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
63570f6d82eSOmar Sandoval 		btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
63694846229SBoris Burkov 	else if (btrfs_free_space_cache_v1_active(info)) {
6375d1ab66cSNaohiro Aota 		if (btrfs_is_zoned(info)) {
6385d1ab66cSNaohiro Aota 			btrfs_info(info,
6395d1ab66cSNaohiro Aota 			"zoned: clearing existing space cache");
6405d1ab66cSNaohiro Aota 			btrfs_set_super_cache_generation(info->super_copy, 0);
6415d1ab66cSNaohiro Aota 		} else {
64273bc1876SJosef Bacik 			btrfs_set_opt(info->mount_opt, SPACE_CACHE);
6435d1ab66cSNaohiro Aota 		}
6445d1ab66cSNaohiro Aota 	}
64573bc1876SJosef Bacik 
64696da0919SQu Wenruo 	/*
64796da0919SQu Wenruo 	 * Even the options are empty, we still need to do extra check
64896da0919SQu Wenruo 	 * against new flags
64996da0919SQu Wenruo 	 */
65095e05289SChris Mason 	if (!options)
65196da0919SQu Wenruo 		goto check;
65295e05289SChris Mason 
65395e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
65495e05289SChris Mason 		int token;
65595e05289SChris Mason 		if (!*p)
65695e05289SChris Mason 			continue;
65795e05289SChris Mason 
65895e05289SChris Mason 		token = match_token(p, tokens, args);
65995e05289SChris Mason 		switch (token) {
660dfe25020SChris Mason 		case Opt_degraded:
6610b246afaSJeff Mahoney 			btrfs_info(info, "allowing degraded mounts");
662dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
663dfe25020SChris Mason 			break;
66495e05289SChris Mason 		case Opt_subvol:
66537becec9SOmar Sandoval 		case Opt_subvol_empty:
66673f73415SJosef Bacik 		case Opt_subvolid:
66743e570b0SChristoph Hellwig 		case Opt_device:
668edf24abeSChristoph Hellwig 			/*
669fa59f27cSAnand Jain 			 * These are parsed by btrfs_parse_subvol_options or
670fa59f27cSAnand Jain 			 * btrfs_parse_device_options and can be ignored here.
671edf24abeSChristoph Hellwig 			 */
67295e05289SChris Mason 			break;
673b6cda9bcSChris Mason 		case Opt_nodatasum:
6743cdde224SJeff Mahoney 			btrfs_set_and_info(info, NODATASUM,
67507802534SQu Wenruo 					   "setting nodatasum");
676be20aa9dSChris Mason 			break;
677d399167dSQu Wenruo 		case Opt_datasum:
6783cdde224SJeff Mahoney 			if (btrfs_test_opt(info, NODATASUM)) {
6793cdde224SJeff Mahoney 				if (btrfs_test_opt(info, NODATACOW))
6800b246afaSJeff Mahoney 					btrfs_info(info,
6815d163e0eSJeff Mahoney 						   "setting datasum, datacow enabled");
682d399167dSQu Wenruo 				else
6830b246afaSJeff Mahoney 					btrfs_info(info, "setting datasum");
68407802534SQu Wenruo 			}
685d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATACOW);
686d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATASUM);
687d399167dSQu Wenruo 			break;
688be20aa9dSChris Mason 		case Opt_nodatacow:
6893cdde224SJeff Mahoney 			if (!btrfs_test_opt(info, NODATACOW)) {
6903cdde224SJeff Mahoney 				if (!btrfs_test_opt(info, COMPRESS) ||
6913cdde224SJeff Mahoney 				    !btrfs_test_opt(info, FORCE_COMPRESS)) {
6920b246afaSJeff Mahoney 					btrfs_info(info,
693efe120a0SFrank Holton 						   "setting nodatacow, compression disabled");
694bedb2ccaSAndrei Popa 				} else {
6950b246afaSJeff Mahoney 					btrfs_info(info, "setting nodatacow");
696bedb2ccaSAndrei Popa 				}
69707802534SQu Wenruo 			}
698bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, COMPRESS);
699bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
700be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
701be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
702b6cda9bcSChris Mason 			break;
703a258af7aSQu Wenruo 		case Opt_datacow:
7043cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NODATACOW,
70507802534SQu Wenruo 					     "setting datacow");
706a258af7aSQu Wenruo 			break;
707a555f810SChris Mason 		case Opt_compress_force:
708261507a0SLi Zefan 		case Opt_compress_force_type:
709261507a0SLi Zefan 			compress_force = true;
710c730ae0cSMarcos Paulo de Souza 			fallthrough;
711261507a0SLi Zefan 		case Opt_compress:
712261507a0SLi Zefan 		case Opt_compress_type:
7133cdde224SJeff Mahoney 			saved_compress_type = btrfs_test_opt(info,
7143cdde224SJeff Mahoney 							     COMPRESS) ?
715b7c47bbbSTsutomu Itoh 				info->compress_type : BTRFS_COMPRESS_NONE;
716b7c47bbbSTsutomu Itoh 			saved_compress_force =
7173cdde224SJeff Mahoney 				btrfs_test_opt(info, FORCE_COMPRESS);
71827942c99SDavid Sterba 			saved_compress_level = info->compress_level;
719261507a0SLi Zefan 			if (token == Opt_compress ||
720261507a0SLi Zefan 			    token == Opt_compress_force ||
721a7164fa4SDavid Sterba 			    strncmp(args[0].from, "zlib", 4) == 0) {
722261507a0SLi Zefan 				compress_type = "zlib";
723eae8d825SQu Wenruo 
724261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
725eae8d825SQu Wenruo 				info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
726eae8d825SQu Wenruo 				/*
727eae8d825SQu Wenruo 				 * args[0] contains uninitialized data since
728eae8d825SQu Wenruo 				 * for these tokens we don't expect any
729eae8d825SQu Wenruo 				 * parameter.
730eae8d825SQu Wenruo 				 */
731eae8d825SQu Wenruo 				if (token != Opt_compress &&
732eae8d825SQu Wenruo 				    token != Opt_compress_force)
733f51d2b59SDavid Sterba 					info->compress_level =
734d0ab62ceSDennis Zhou 					  btrfs_compress_str2level(
735d0ab62ceSDennis Zhou 							BTRFS_COMPRESS_ZLIB,
736d0ab62ceSDennis Zhou 							args[0].from + 4);
737063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
738bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
739bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
740b7c47bbbSTsutomu Itoh 				no_compress = 0;
741a7164fa4SDavid Sterba 			} else if (strncmp(args[0].from, "lzo", 3) == 0) {
742a6fa6faeSLi Zefan 				compress_type = "lzo";
743a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
744282dd7d7SMarcos Paulo de Souza 				info->compress_level = 0;
745063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
746bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
747bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
7482b0ce2c2SMitch Harder 				btrfs_set_fs_incompat(info, COMPRESS_LZO);
749b7c47bbbSTsutomu Itoh 				no_compress = 0;
7503f93aef5SDennis Zhou 			} else if (strncmp(args[0].from, "zstd", 4) == 0) {
7515c1aab1dSNick Terrell 				compress_type = "zstd";
7525c1aab1dSNick Terrell 				info->compress_type = BTRFS_COMPRESS_ZSTD;
7533f93aef5SDennis Zhou 				info->compress_level =
7543f93aef5SDennis Zhou 					btrfs_compress_str2level(
7553f93aef5SDennis Zhou 							 BTRFS_COMPRESS_ZSTD,
7563f93aef5SDennis Zhou 							 args[0].from + 4);
7575c1aab1dSNick Terrell 				btrfs_set_opt(info->mount_opt, COMPRESS);
7585c1aab1dSNick Terrell 				btrfs_clear_opt(info->mount_opt, NODATACOW);
7595c1aab1dSNick Terrell 				btrfs_clear_opt(info->mount_opt, NODATASUM);
7605c1aab1dSNick Terrell 				btrfs_set_fs_incompat(info, COMPRESS_ZSTD);
7615c1aab1dSNick Terrell 				no_compress = 0;
762063849eaSArnd Hannemann 			} else if (strncmp(args[0].from, "no", 2) == 0) {
763063849eaSArnd Hannemann 				compress_type = "no";
76427942c99SDavid Sterba 				info->compress_level = 0;
76527942c99SDavid Sterba 				info->compress_type = 0;
766063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, COMPRESS);
767063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
768063849eaSArnd Hannemann 				compress_force = false;
769b7c47bbbSTsutomu Itoh 				no_compress++;
770261507a0SLi Zefan 			} else {
771e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized compression value %s",
772e3a4167cSDavid Sterba 					  args[0].from);
773261507a0SLi Zefan 				ret = -EINVAL;
774261507a0SLi Zefan 				goto out;
775261507a0SLi Zefan 			}
776261507a0SLi Zefan 
777261507a0SLi Zefan 			if (compress_force) {
778b7c47bbbSTsutomu Itoh 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
779143f3636SDavid Sterba 			} else {
7804027e0f4SWang Shilong 				/*
7814027e0f4SWang Shilong 				 * If we remount from compress-force=xxx to
7824027e0f4SWang Shilong 				 * compress=xxx, we need clear FORCE_COMPRESS
7834027e0f4SWang Shilong 				 * flag, otherwise, there is no way for users
7844027e0f4SWang Shilong 				 * to disable forcible compression separately.
7854027e0f4SWang Shilong 				 */
7864027e0f4SWang Shilong 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
787a7e252afSMiao Xie 			}
78827942c99SDavid Sterba 			if (no_compress == 1) {
78927942c99SDavid Sterba 				btrfs_info(info, "use no compression");
79027942c99SDavid Sterba 			} else if ((info->compress_type != saved_compress_type) ||
79127942c99SDavid Sterba 				   (compress_force != saved_compress_force) ||
79227942c99SDavid Sterba 				   (info->compress_level != saved_compress_level)) {
793f51d2b59SDavid Sterba 				btrfs_info(info, "%s %s compression, level %d",
794b7c47bbbSTsutomu Itoh 					   (compress_force) ? "force" : "use",
795f51d2b59SDavid Sterba 					   compress_type, info->compress_level);
796b7c47bbbSTsutomu Itoh 			}
797b7c47bbbSTsutomu Itoh 			compress_force = false;
798a555f810SChris Mason 			break;
799e18e4809SChris Mason 		case Opt_ssd:
8003cdde224SJeff Mahoney 			btrfs_set_and_info(info, SSD,
801583b7231SHans van Kranenburg 					   "enabling ssd optimizations");
802951e7966SAdam Borowski 			btrfs_clear_opt(info->mount_opt, NOSSD);
803e18e4809SChris Mason 			break;
804451d7585SChris Mason 		case Opt_ssd_spread:
805583b7231SHans van Kranenburg 			btrfs_set_and_info(info, SSD,
806583b7231SHans van Kranenburg 					   "enabling ssd optimizations");
8073cdde224SJeff Mahoney 			btrfs_set_and_info(info, SSD_SPREAD,
808583b7231SHans van Kranenburg 					   "using spread ssd allocation scheme");
809951e7966SAdam Borowski 			btrfs_clear_opt(info->mount_opt, NOSSD);
810451d7585SChris Mason 			break;
8113b30c22fSChris Mason 		case Opt_nossd:
812583b7231SHans van Kranenburg 			btrfs_set_opt(info->mount_opt, NOSSD);
813583b7231SHans van Kranenburg 			btrfs_clear_and_info(info, SSD,
814583b7231SHans van Kranenburg 					     "not using ssd optimizations");
815c730ae0cSMarcos Paulo de Souza 			fallthrough;
81662b8e077SHoward McLauchlan 		case Opt_nossd_spread:
817583b7231SHans van Kranenburg 			btrfs_clear_and_info(info, SSD_SPREAD,
818583b7231SHans van Kranenburg 					     "not using spread ssd allocation scheme");
8193b30c22fSChris Mason 			break;
820842bef58SQu Wenruo 		case Opt_barrier:
8213cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NOBARRIER,
82207802534SQu Wenruo 					     "turning on barriers");
823842bef58SQu Wenruo 			break;
82421ad10cfSChris Mason 		case Opt_nobarrier:
8253cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOBARRIER,
82607802534SQu Wenruo 					   "turning off barriers");
82721ad10cfSChris Mason 			break;
8284543df7eSChris Mason 		case Opt_thread_pool:
8292c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
8302c334e87SWang Shilong 			if (ret) {
831e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized thread_pool value %s",
832e3a4167cSDavid Sterba 					  args[0].from);
8332c334e87SWang Shilong 				goto out;
834f7b885beSAnand Jain 			} else if (intarg == 0) {
835e3a4167cSDavid Sterba 				btrfs_err(info, "invalid value 0 for thread_pool");
8362c334e87SWang Shilong 				ret = -EINVAL;
8372c334e87SWang Shilong 				goto out;
8382c334e87SWang Shilong 			}
839f7b885beSAnand Jain 			info->thread_pool_size = intarg;
8404543df7eSChris Mason 			break;
8416f568d35SChris Mason 		case Opt_max_inline:
842edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
8436f568d35SChris Mason 			if (num) {
84491748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
8456f568d35SChris Mason 				kfree(num);
8466f568d35SChris Mason 
84715ada040SChris Mason 				if (info->max_inline) {
848feb5f965SMitch Harder 					info->max_inline = min_t(u64,
84915ada040SChris Mason 						info->max_inline,
8500b246afaSJeff Mahoney 						info->sectorsize);
85115ada040SChris Mason 				}
8520b246afaSJeff Mahoney 				btrfs_info(info, "max_inline at %llu",
853c1c9ff7cSGeert Uytterhoeven 					   info->max_inline);
8542c334e87SWang Shilong 			} else {
8552c334e87SWang Shilong 				ret = -ENOMEM;
8562c334e87SWang Shilong 				goto out;
8576f568d35SChris Mason 			}
8586f568d35SChris Mason 			break;
859bd0330adSQu Wenruo 		case Opt_acl:
86045ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL
8611751e8a6SLinus Torvalds 			info->sb->s_flags |= SB_POSIXACL;
862bd0330adSQu Wenruo 			break;
86345ff35d6SGuangliang Zhao #else
8640b246afaSJeff Mahoney 			btrfs_err(info, "support for ACL not compiled in!");
86545ff35d6SGuangliang Zhao 			ret = -EINVAL;
86645ff35d6SGuangliang Zhao 			goto out;
86745ff35d6SGuangliang Zhao #endif
86833268eafSJosef Bacik 		case Opt_noacl:
8691751e8a6SLinus Torvalds 			info->sb->s_flags &= ~SB_POSIXACL;
87033268eafSJosef Bacik 			break;
8713a5e1404SSage Weil 		case Opt_notreelog:
8723cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOTREELOG,
87307802534SQu Wenruo 					   "disabling tree log");
8743a5e1404SSage Weil 			break;
875a88998f2SQu Wenruo 		case Opt_treelog:
8763cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NOTREELOG,
87707802534SQu Wenruo 					     "enabling tree log");
878a88998f2SQu Wenruo 			break;
879fed8f166SQu Wenruo 		case Opt_norecovery:
88096da0919SQu Wenruo 		case Opt_nologreplay:
88174ef0018SQu Wenruo 			btrfs_warn(info,
88274ef0018SQu Wenruo 		"'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
8833cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOLOGREPLAY,
88496da0919SQu Wenruo 					   "disabling log replay at mount time");
88596da0919SQu Wenruo 			break;
886dccae999SSage Weil 		case Opt_flushoncommit:
8873cdde224SJeff Mahoney 			btrfs_set_and_info(info, FLUSHONCOMMIT,
88807802534SQu Wenruo 					   "turning on flush-on-commit");
889dccae999SSage Weil 			break;
8902c9ee856SQu Wenruo 		case Opt_noflushoncommit:
8913cdde224SJeff Mahoney 			btrfs_clear_and_info(info, FLUSHONCOMMIT,
89207802534SQu Wenruo 					     "turning off flush-on-commit");
8932c9ee856SQu Wenruo 			break;
89497e728d4SJosef Bacik 		case Opt_ratio:
8952c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
896e3a4167cSDavid Sterba 			if (ret) {
897e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized metadata_ratio value %s",
898e3a4167cSDavid Sterba 					  args[0].from);
8992c334e87SWang Shilong 				goto out;
900e3a4167cSDavid Sterba 			}
90197e728d4SJosef Bacik 			info->metadata_ratio = intarg;
902764cb8b4SAnand Jain 			btrfs_info(info, "metadata ratio %u",
90397e728d4SJosef Bacik 				   info->metadata_ratio);
90497e728d4SJosef Bacik 			break;
905e244a0aeSChristoph Hellwig 		case Opt_discard:
906b0643e59SDennis Zhou 		case Opt_discard_mode:
907b0643e59SDennis Zhou 			if (token == Opt_discard ||
908b0643e59SDennis Zhou 			    strcmp(args[0].from, "sync") == 0) {
909b0643e59SDennis Zhou 				btrfs_clear_opt(info->mount_opt, DISCARD_ASYNC);
91046b27f50SDennis Zhou 				btrfs_set_and_info(info, DISCARD_SYNC,
91146b27f50SDennis Zhou 						   "turning on sync discard");
912b0643e59SDennis Zhou 			} else if (strcmp(args[0].from, "async") == 0) {
913b0643e59SDennis Zhou 				btrfs_clear_opt(info->mount_opt, DISCARD_SYNC);
914b0643e59SDennis Zhou 				btrfs_set_and_info(info, DISCARD_ASYNC,
915b0643e59SDennis Zhou 						   "turning on async discard");
916b0643e59SDennis Zhou 			} else {
917e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized discard mode value %s",
918e3a4167cSDavid Sterba 					  args[0].from);
919b0643e59SDennis Zhou 				ret = -EINVAL;
920b0643e59SDennis Zhou 				goto out;
921b0643e59SDennis Zhou 			}
92263a7cb13SDavid Sterba 			btrfs_clear_opt(info->mount_opt, NODISCARD);
923e244a0aeSChristoph Hellwig 			break;
924e07a2adeSQu Wenruo 		case Opt_nodiscard:
92546b27f50SDennis Zhou 			btrfs_clear_and_info(info, DISCARD_SYNC,
92607802534SQu Wenruo 					     "turning off discard");
927b0643e59SDennis Zhou 			btrfs_clear_and_info(info, DISCARD_ASYNC,
928b0643e59SDennis Zhou 					     "turning off async discard");
92963a7cb13SDavid Sterba 			btrfs_set_opt(info->mount_opt, NODISCARD);
930e07a2adeSQu Wenruo 			break;
9310af3d00bSJosef Bacik 		case Opt_space_cache:
93270f6d82eSOmar Sandoval 		case Opt_space_cache_version:
93363cd070dSJosef Bacik 			/*
93463cd070dSJosef Bacik 			 * We already set FREE_SPACE_TREE above because we have
93563cd070dSJosef Bacik 			 * compat_ro(FREE_SPACE_TREE) set, and we aren't going
93663cd070dSJosef Bacik 			 * to allow v1 to be set for extent tree v2, simply
93763cd070dSJosef Bacik 			 * ignore this setting if we're extent tree v2.
93863cd070dSJosef Bacik 			 */
93963cd070dSJosef Bacik 			if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
94063cd070dSJosef Bacik 				break;
94170f6d82eSOmar Sandoval 			if (token == Opt_space_cache ||
94270f6d82eSOmar Sandoval 			    strcmp(args[0].from, "v1") == 0) {
9430b246afaSJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
94470f6d82eSOmar Sandoval 						FREE_SPACE_TREE);
9453cdde224SJeff Mahoney 				btrfs_set_and_info(info, SPACE_CACHE,
94607802534SQu Wenruo 					   "enabling disk space caching");
94770f6d82eSOmar Sandoval 			} else if (strcmp(args[0].from, "v2") == 0) {
9480b246afaSJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
94970f6d82eSOmar Sandoval 						SPACE_CACHE);
9500b246afaSJeff Mahoney 				btrfs_set_and_info(info, FREE_SPACE_TREE,
95170f6d82eSOmar Sandoval 						   "enabling free space tree");
95270f6d82eSOmar Sandoval 			} else {
953e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized space_cache value %s",
954e3a4167cSDavid Sterba 					  args[0].from);
95570f6d82eSOmar Sandoval 				ret = -EINVAL;
95670f6d82eSOmar Sandoval 				goto out;
95770f6d82eSOmar Sandoval 			}
9580de90876SJosef Bacik 			break;
959f420ee1eSStefan Behrens 		case Opt_rescan_uuid_tree:
960f420ee1eSStefan Behrens 			btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
961f420ee1eSStefan Behrens 			break;
96273bc1876SJosef Bacik 		case Opt_no_space_cache:
96363cd070dSJosef Bacik 			/*
96463cd070dSJosef Bacik 			 * We cannot operate without the free space tree with
96563cd070dSJosef Bacik 			 * extent tree v2, ignore this option.
96663cd070dSJosef Bacik 			 */
96763cd070dSJosef Bacik 			if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
96863cd070dSJosef Bacik 				break;
9693cdde224SJeff Mahoney 			if (btrfs_test_opt(info, SPACE_CACHE)) {
9700b246afaSJeff Mahoney 				btrfs_clear_and_info(info, SPACE_CACHE,
97107802534SQu Wenruo 					     "disabling disk space caching");
97270f6d82eSOmar Sandoval 			}
9733cdde224SJeff Mahoney 			if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
9740b246afaSJeff Mahoney 				btrfs_clear_and_info(info, FREE_SPACE_TREE,
97570f6d82eSOmar Sandoval 					     "disabling free space tree");
97670f6d82eSOmar Sandoval 			}
97773bc1876SJosef Bacik 			break;
9784b9465cbSChris Mason 		case Opt_inode_cache:
9793818aea2SQu Wenruo 		case Opt_noinode_cache:
9805297199aSNikolay Borisov 			btrfs_warn(info,
9815297199aSNikolay Borisov 	"the 'inode_cache' option is deprecated and has no effect since 5.11");
9824b9465cbSChris Mason 			break;
98388c2ba3bSJosef Bacik 		case Opt_clear_cache:
98463cd070dSJosef Bacik 			/*
98563cd070dSJosef Bacik 			 * We cannot clear the free space tree with extent tree
98663cd070dSJosef Bacik 			 * v2, ignore this option.
98763cd070dSJosef Bacik 			 */
98863cd070dSJosef Bacik 			if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
98963cd070dSJosef Bacik 				break;
9903cdde224SJeff Mahoney 			btrfs_set_and_info(info, CLEAR_CACHE,
99107802534SQu Wenruo 					   "force clearing of disk cache");
9920af3d00bSJosef Bacik 			break;
9934260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
9944260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
9954260f7c7SSage Weil 			break;
99691435650SChris Mason 		case Opt_enospc_debug:
99791435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
99891435650SChris Mason 			break;
99953036293SQu Wenruo 		case Opt_noenospc_debug:
100053036293SQu Wenruo 			btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
100153036293SQu Wenruo 			break;
10024cb5300bSChris Mason 		case Opt_defrag:
10033cdde224SJeff Mahoney 			btrfs_set_and_info(info, AUTO_DEFRAG,
100407802534SQu Wenruo 					   "enabling auto defrag");
10054cb5300bSChris Mason 			break;
1006fc0ca9afSQu Wenruo 		case Opt_nodefrag:
10073cdde224SJeff Mahoney 			btrfs_clear_and_info(info, AUTO_DEFRAG,
100807802534SQu Wenruo 					     "disabling auto defrag");
1009fc0ca9afSQu Wenruo 			break;
1010af31f5e5SChris Mason 		case Opt_recovery:
10118dcddfa0SQu Wenruo 		case Opt_usebackuproot:
101274ef0018SQu Wenruo 			btrfs_warn(info,
101374ef0018SQu Wenruo 			"'%s' is deprecated, use 'rescue=usebackuproot' instead",
101474ef0018SQu Wenruo 				   token == Opt_recovery ? "recovery" :
101574ef0018SQu Wenruo 				   "usebackuproot");
10160b246afaSJeff Mahoney 			btrfs_info(info,
10178dcddfa0SQu Wenruo 				   "trying to use backup root at mount time");
10188dcddfa0SQu Wenruo 			btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
1019af31f5e5SChris Mason 			break;
10209555c6c1SIlya Dryomov 		case Opt_skip_balance:
10219555c6c1SIlya Dryomov 			btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
10229555c6c1SIlya Dryomov 			break;
102321adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
102421adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
10250b246afaSJeff Mahoney 			btrfs_info(info,
1026efe120a0SFrank Holton 				   "enabling check integrity including extent data");
1027cbeaae4fSDavid Sterba 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY_DATA);
102821adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
102921adbd5cSStefan Behrens 			break;
103021adbd5cSStefan Behrens 		case Opt_check_integrity:
10310b246afaSJeff Mahoney 			btrfs_info(info, "enabling check integrity");
103221adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
103321adbd5cSStefan Behrens 			break;
103421adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
10352c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
1036e3a4167cSDavid Sterba 			if (ret) {
1037e3a4167cSDavid Sterba 				btrfs_err(info,
1038e3a4167cSDavid Sterba 				"unrecognized check_integrity_print_mask value %s",
1039e3a4167cSDavid Sterba 					args[0].from);
10402c334e87SWang Shilong 				goto out;
1041e3a4167cSDavid Sterba 			}
104221adbd5cSStefan Behrens 			info->check_integrity_print_mask = intarg;
104302453bdeSAnand Jain 			btrfs_info(info, "check_integrity_print_mask 0x%x",
104421adbd5cSStefan Behrens 				   info->check_integrity_print_mask);
104521adbd5cSStefan Behrens 			break;
104621adbd5cSStefan Behrens #else
104721adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
104821adbd5cSStefan Behrens 		case Opt_check_integrity:
104921adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
10500b246afaSJeff Mahoney 			btrfs_err(info,
1051efe120a0SFrank Holton 				  "support for check_integrity* not compiled in!");
105221adbd5cSStefan Behrens 			ret = -EINVAL;
105321adbd5cSStefan Behrens 			goto out;
105421adbd5cSStefan Behrens #endif
10558c342930SJeff Mahoney 		case Opt_fatal_errors:
1056e3a4167cSDavid Sterba 			if (strcmp(args[0].from, "panic") == 0) {
10578c342930SJeff Mahoney 				btrfs_set_opt(info->mount_opt,
10588c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
1059e3a4167cSDavid Sterba 			} else if (strcmp(args[0].from, "bug") == 0) {
10608c342930SJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
10618c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
1062e3a4167cSDavid Sterba 			} else {
1063e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized fatal_errors value %s",
1064e3a4167cSDavid Sterba 					  args[0].from);
10658c342930SJeff Mahoney 				ret = -EINVAL;
10668c342930SJeff Mahoney 				goto out;
10678c342930SJeff Mahoney 			}
10688c342930SJeff Mahoney 			break;
10698b87dc17SDavid Sterba 		case Opt_commit_interval:
10708b87dc17SDavid Sterba 			intarg = 0;
10718b87dc17SDavid Sterba 			ret = match_int(&args[0], &intarg);
1072e3a4167cSDavid Sterba 			if (ret) {
1073e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized commit_interval value %s",
1074e3a4167cSDavid Sterba 					  args[0].from);
1075e3a4167cSDavid Sterba 				ret = -EINVAL;
10768b87dc17SDavid Sterba 				goto out;
1077e3a4167cSDavid Sterba 			}
1078d3740608SAnand Jain 			if (intarg == 0) {
1079d3740608SAnand Jain 				btrfs_info(info,
1080d3740608SAnand Jain 					   "using default commit interval %us",
1081d3740608SAnand Jain 					   BTRFS_DEFAULT_COMMIT_INTERVAL);
1082d3740608SAnand Jain 				intarg = BTRFS_DEFAULT_COMMIT_INTERVAL;
1083d3740608SAnand Jain 			} else if (intarg > 300) {
1084d3740608SAnand Jain 				btrfs_warn(info, "excessive commit interval %d",
10858b87dc17SDavid Sterba 					   intarg);
10868b87dc17SDavid Sterba 			}
10878b87dc17SDavid Sterba 			info->commit_interval = intarg;
10888b87dc17SDavid Sterba 			break;
108974ef0018SQu Wenruo 		case Opt_rescue:
109074ef0018SQu Wenruo 			ret = parse_rescue_options(info, args[0].from);
1091e3a4167cSDavid Sterba 			if (ret < 0) {
1092e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized rescue value %s",
1093e3a4167cSDavid Sterba 					  args[0].from);
109474ef0018SQu Wenruo 				goto out;
1095e3a4167cSDavid Sterba 			}
109674ef0018SQu Wenruo 			break;
1097d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
1098d0bd4560SJosef Bacik 		case Opt_fragment_all:
10990b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting all space");
1100d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
1101d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
1102d0bd4560SJosef Bacik 			break;
1103d0bd4560SJosef Bacik 		case Opt_fragment_metadata:
11040b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting metadata");
1105d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt,
1106d0bd4560SJosef Bacik 				      FRAGMENT_METADATA);
1107d0bd4560SJosef Bacik 			break;
1108d0bd4560SJosef Bacik 		case Opt_fragment_data:
11090b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting data");
1110d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
1111d0bd4560SJosef Bacik 			break;
1112d0bd4560SJosef Bacik #endif
1113fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
1114fb592373SJosef Bacik 		case Opt_ref_verify:
1115fb592373SJosef Bacik 			btrfs_info(info, "doing ref verification");
1116fb592373SJosef Bacik 			btrfs_set_opt(info->mount_opt, REF_VERIFY);
1117fb592373SJosef Bacik 			break;
1118fb592373SJosef Bacik #endif
1119a7a3f7caSSage Weil 		case Opt_err:
11207e8f19e5SDavid Sterba 			btrfs_err(info, "unrecognized mount option '%s'", p);
1121a7a3f7caSSage Weil 			ret = -EINVAL;
1122a7a3f7caSSage Weil 			goto out;
112395e05289SChris Mason 		default:
1124be20aa9dSChris Mason 			break;
112595e05289SChris Mason 		}
112695e05289SChris Mason 	}
112796da0919SQu Wenruo check:
1128d70bf748SJosef Bacik 	/* We're read-only, don't have to check. */
1129d70bf748SJosef Bacik 	if (new_flags & SB_RDONLY)
1130d70bf748SJosef Bacik 		goto out;
1131d70bf748SJosef Bacik 
113242437a63SJosef Bacik 	if (check_ro_option(info, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||
1133882dbe0cSJosef Bacik 	    check_ro_option(info, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
1134882dbe0cSJosef Bacik 	    check_ro_option(info, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums"))
113596da0919SQu Wenruo 		ret = -EINVAL;
1136a7a3f7caSSage Weil out:
11370b246afaSJeff Mahoney 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
11383cdde224SJeff Mahoney 	    !btrfs_test_opt(info, FREE_SPACE_TREE) &&
11393cdde224SJeff Mahoney 	    !btrfs_test_opt(info, CLEAR_CACHE)) {
11400b246afaSJeff Mahoney 		btrfs_err(info, "cannot disable free space tree");
114170f6d82eSOmar Sandoval 		ret = -EINVAL;
114270f6d82eSOmar Sandoval 
114370f6d82eSOmar Sandoval 	}
11445d1ab66cSNaohiro Aota 	if (!ret)
11455d1ab66cSNaohiro Aota 		ret = btrfs_check_mountopts_zoned(info);
1146dbecac26SMaciej S. Szmigiero 	if (!ret && !remounting) {
1147dbecac26SMaciej S. Szmigiero 		if (btrfs_test_opt(info, SPACE_CACHE))
11480b246afaSJeff Mahoney 			btrfs_info(info, "disk space caching is enabled");
1149dbecac26SMaciej S. Szmigiero 		if (btrfs_test_opt(info, FREE_SPACE_TREE))
11500b246afaSJeff Mahoney 			btrfs_info(info, "using free space tree");
1151dbecac26SMaciej S. Szmigiero 	}
1152a7a3f7caSSage Weil 	return ret;
1153edf24abeSChristoph Hellwig }
1154edf24abeSChristoph Hellwig 
1155edf24abeSChristoph Hellwig /*
1156edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
1157edf24abeSChristoph Hellwig  *
1158edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
1159edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
1160edf24abeSChristoph Hellwig  */
1161fa59f27cSAnand Jain static int btrfs_parse_device_options(const char *options, fmode_t flags,
1162d64dcbd1SGu Jinxiang 				      void *holder)
1163edf24abeSChristoph Hellwig {
1164edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
116583c8c9bdSJeff Liu 	char *device_name, *opts, *orig, *p;
116636350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
1167d7407606SMisono, Tomohiro 	int error = 0;
1168d7407606SMisono, Tomohiro 
11695139cff5SDavid Sterba 	lockdep_assert_held(&uuid_mutex);
11705139cff5SDavid Sterba 
1171d7407606SMisono, Tomohiro 	if (!options)
1172d7407606SMisono, Tomohiro 		return 0;
1173d7407606SMisono, Tomohiro 
1174d7407606SMisono, Tomohiro 	/*
1175d7407606SMisono, Tomohiro 	 * strsep changes the string, duplicate it because btrfs_parse_options
1176d7407606SMisono, Tomohiro 	 * gets called later
1177d7407606SMisono, Tomohiro 	 */
1178d7407606SMisono, Tomohiro 	opts = kstrdup(options, GFP_KERNEL);
1179d7407606SMisono, Tomohiro 	if (!opts)
1180d7407606SMisono, Tomohiro 		return -ENOMEM;
1181d7407606SMisono, Tomohiro 	orig = opts;
1182d7407606SMisono, Tomohiro 
1183d7407606SMisono, Tomohiro 	while ((p = strsep(&opts, ",")) != NULL) {
1184d7407606SMisono, Tomohiro 		int token;
1185d7407606SMisono, Tomohiro 
1186d7407606SMisono, Tomohiro 		if (!*p)
1187d7407606SMisono, Tomohiro 			continue;
1188d7407606SMisono, Tomohiro 
1189d7407606SMisono, Tomohiro 		token = match_token(p, tokens, args);
1190d7407606SMisono, Tomohiro 		if (token == Opt_device) {
1191d7407606SMisono, Tomohiro 			device_name = match_strdup(&args[0]);
1192d7407606SMisono, Tomohiro 			if (!device_name) {
1193d7407606SMisono, Tomohiro 				error = -ENOMEM;
1194d7407606SMisono, Tomohiro 				goto out;
1195d7407606SMisono, Tomohiro 			}
119636350e95SGu Jinxiang 			device = btrfs_scan_one_device(device_name, flags,
119736350e95SGu Jinxiang 					holder);
1198d7407606SMisono, Tomohiro 			kfree(device_name);
119936350e95SGu Jinxiang 			if (IS_ERR(device)) {
120036350e95SGu Jinxiang 				error = PTR_ERR(device);
1201d7407606SMisono, Tomohiro 				goto out;
1202d7407606SMisono, Tomohiro 			}
1203d7407606SMisono, Tomohiro 		}
120436350e95SGu Jinxiang 	}
1205d7407606SMisono, Tomohiro 
1206d7407606SMisono, Tomohiro out:
1207d7407606SMisono, Tomohiro 	kfree(orig);
1208d7407606SMisono, Tomohiro 	return error;
1209d7407606SMisono, Tomohiro }
1210d7407606SMisono, Tomohiro 
1211d7407606SMisono, Tomohiro /*
1212d7407606SMisono, Tomohiro  * Parse mount options that are related to subvolume id
1213d7407606SMisono, Tomohiro  *
1214d7407606SMisono, Tomohiro  * The value is later passed to mount_subvol()
1215d7407606SMisono, Tomohiro  */
121693b9bcdfSGu Jinxiang static int btrfs_parse_subvol_options(const char *options, char **subvol_name,
121793b9bcdfSGu Jinxiang 		u64 *subvol_objectid)
1218d7407606SMisono, Tomohiro {
1219d7407606SMisono, Tomohiro 	substring_t args[MAX_OPT_ARGS];
1220d7407606SMisono, Tomohiro 	char *opts, *orig, *p;
1221edf24abeSChristoph Hellwig 	int error = 0;
1222ccb0e7d1SAnand Jain 	u64 subvolid;
1223edf24abeSChristoph Hellwig 
1224edf24abeSChristoph Hellwig 	if (!options)
1225830c4adbSJosef Bacik 		return 0;
1226edf24abeSChristoph Hellwig 
1227edf24abeSChristoph Hellwig 	/*
1228d7407606SMisono, Tomohiro 	 * strsep changes the string, duplicate it because
1229fa59f27cSAnand Jain 	 * btrfs_parse_device_options gets called later
1230edf24abeSChristoph Hellwig 	 */
1231edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
1232edf24abeSChristoph Hellwig 	if (!opts)
1233edf24abeSChristoph Hellwig 		return -ENOMEM;
12343f3d0bc0STero Roponen 	orig = opts;
1235edf24abeSChristoph Hellwig 
1236edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
1237edf24abeSChristoph Hellwig 		int token;
1238edf24abeSChristoph Hellwig 		if (!*p)
1239edf24abeSChristoph Hellwig 			continue;
1240edf24abeSChristoph Hellwig 
1241edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
1242edf24abeSChristoph Hellwig 		switch (token) {
1243edf24abeSChristoph Hellwig 		case Opt_subvol:
1244a90e8b6fSIlya Dryomov 			kfree(*subvol_name);
1245edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
12462c334e87SWang Shilong 			if (!*subvol_name) {
12472c334e87SWang Shilong 				error = -ENOMEM;
12482c334e87SWang Shilong 				goto out;
12492c334e87SWang Shilong 			}
1250edf24abeSChristoph Hellwig 			break;
125173f73415SJosef Bacik 		case Opt_subvolid:
1252ccb0e7d1SAnand Jain 			error = match_u64(&args[0], &subvolid);
1253ccb0e7d1SAnand Jain 			if (error)
12542c334e87SWang Shilong 				goto out;
1255ccb0e7d1SAnand Jain 
1256ccb0e7d1SAnand Jain 			/* we want the original fs_tree */
1257ccb0e7d1SAnand Jain 			if (subvolid == 0)
1258ccb0e7d1SAnand Jain 				subvolid = BTRFS_FS_TREE_OBJECTID;
1259ccb0e7d1SAnand Jain 
1260ccb0e7d1SAnand Jain 			*subvol_objectid = subvolid;
126173f73415SJosef Bacik 			break;
1262edf24abeSChristoph Hellwig 		default:
1263edf24abeSChristoph Hellwig 			break;
1264edf24abeSChristoph Hellwig 		}
1265edf24abeSChristoph Hellwig 	}
1266edf24abeSChristoph Hellwig 
1267edf24abeSChristoph Hellwig out:
1268830c4adbSJosef Bacik 	kfree(orig);
1269edf24abeSChristoph Hellwig 	return error;
127095e05289SChris Mason }
127195e05289SChris Mason 
1272c0c907a4SMarcos Paulo de Souza char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
127373f73415SJosef Bacik 					  u64 subvol_objectid)
127473f73415SJosef Bacik {
1275815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
12765168489aSJosef Bacik 	struct btrfs_root *fs_root = NULL;
127705dbe683SOmar Sandoval 	struct btrfs_root_ref *root_ref;
127805dbe683SOmar Sandoval 	struct btrfs_inode_ref *inode_ref;
127905dbe683SOmar Sandoval 	struct btrfs_key key;
128005dbe683SOmar Sandoval 	struct btrfs_path *path = NULL;
128105dbe683SOmar Sandoval 	char *name = NULL, *ptr;
128205dbe683SOmar Sandoval 	u64 dirid;
128305dbe683SOmar Sandoval 	int len;
128405dbe683SOmar Sandoval 	int ret;
128505dbe683SOmar Sandoval 
128605dbe683SOmar Sandoval 	path = btrfs_alloc_path();
128705dbe683SOmar Sandoval 	if (!path) {
128805dbe683SOmar Sandoval 		ret = -ENOMEM;
128905dbe683SOmar Sandoval 		goto err;
129005dbe683SOmar Sandoval 	}
129105dbe683SOmar Sandoval 
12923ec83621SDavid Sterba 	name = kmalloc(PATH_MAX, GFP_KERNEL);
129305dbe683SOmar Sandoval 	if (!name) {
129405dbe683SOmar Sandoval 		ret = -ENOMEM;
129505dbe683SOmar Sandoval 		goto err;
129605dbe683SOmar Sandoval 	}
129705dbe683SOmar Sandoval 	ptr = name + PATH_MAX - 1;
129805dbe683SOmar Sandoval 	ptr[0] = '\0';
129905dbe683SOmar Sandoval 
130005dbe683SOmar Sandoval 	/*
130105dbe683SOmar Sandoval 	 * Walk up the subvolume trees in the tree of tree roots by root
130205dbe683SOmar Sandoval 	 * backrefs until we hit the top-level subvolume.
130305dbe683SOmar Sandoval 	 */
130405dbe683SOmar Sandoval 	while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
130505dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
130605dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_BACKREF_KEY;
130705dbe683SOmar Sandoval 		key.offset = (u64)-1;
130805dbe683SOmar Sandoval 
13090ff40a91SMarcos Paulo de Souza 		ret = btrfs_search_backwards(root, &key, path);
131005dbe683SOmar Sandoval 		if (ret < 0) {
131105dbe683SOmar Sandoval 			goto err;
131205dbe683SOmar Sandoval 		} else if (ret > 0) {
131305dbe683SOmar Sandoval 			ret = -ENOENT;
131405dbe683SOmar Sandoval 			goto err;
131505dbe683SOmar Sandoval 		}
131605dbe683SOmar Sandoval 
131705dbe683SOmar Sandoval 		subvol_objectid = key.offset;
131805dbe683SOmar Sandoval 
131905dbe683SOmar Sandoval 		root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
132005dbe683SOmar Sandoval 					  struct btrfs_root_ref);
132105dbe683SOmar Sandoval 		len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
132205dbe683SOmar Sandoval 		ptr -= len + 1;
132305dbe683SOmar Sandoval 		if (ptr < name) {
132405dbe683SOmar Sandoval 			ret = -ENAMETOOLONG;
132505dbe683SOmar Sandoval 			goto err;
132605dbe683SOmar Sandoval 		}
132705dbe683SOmar Sandoval 		read_extent_buffer(path->nodes[0], ptr + 1,
132805dbe683SOmar Sandoval 				   (unsigned long)(root_ref + 1), len);
132905dbe683SOmar Sandoval 		ptr[0] = '/';
133005dbe683SOmar Sandoval 		dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
133105dbe683SOmar Sandoval 		btrfs_release_path(path);
133205dbe683SOmar Sandoval 
133356e9357aSDavid Sterba 		fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true);
133405dbe683SOmar Sandoval 		if (IS_ERR(fs_root)) {
133505dbe683SOmar Sandoval 			ret = PTR_ERR(fs_root);
13365168489aSJosef Bacik 			fs_root = NULL;
13375168489aSJosef Bacik 			goto err;
13385168489aSJosef Bacik 		}
133905dbe683SOmar Sandoval 
134005dbe683SOmar Sandoval 		/*
134105dbe683SOmar Sandoval 		 * Walk up the filesystem tree by inode refs until we hit the
134205dbe683SOmar Sandoval 		 * root directory.
134305dbe683SOmar Sandoval 		 */
134405dbe683SOmar Sandoval 		while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
134505dbe683SOmar Sandoval 			key.objectid = dirid;
134605dbe683SOmar Sandoval 			key.type = BTRFS_INODE_REF_KEY;
134705dbe683SOmar Sandoval 			key.offset = (u64)-1;
134805dbe683SOmar Sandoval 
13490ff40a91SMarcos Paulo de Souza 			ret = btrfs_search_backwards(fs_root, &key, path);
135005dbe683SOmar Sandoval 			if (ret < 0) {
135105dbe683SOmar Sandoval 				goto err;
135205dbe683SOmar Sandoval 			} else if (ret > 0) {
135305dbe683SOmar Sandoval 				ret = -ENOENT;
135405dbe683SOmar Sandoval 				goto err;
135505dbe683SOmar Sandoval 			}
135605dbe683SOmar Sandoval 
135705dbe683SOmar Sandoval 			dirid = key.offset;
135805dbe683SOmar Sandoval 
135905dbe683SOmar Sandoval 			inode_ref = btrfs_item_ptr(path->nodes[0],
136005dbe683SOmar Sandoval 						   path->slots[0],
136105dbe683SOmar Sandoval 						   struct btrfs_inode_ref);
136205dbe683SOmar Sandoval 			len = btrfs_inode_ref_name_len(path->nodes[0],
136305dbe683SOmar Sandoval 						       inode_ref);
136405dbe683SOmar Sandoval 			ptr -= len + 1;
136505dbe683SOmar Sandoval 			if (ptr < name) {
136605dbe683SOmar Sandoval 				ret = -ENAMETOOLONG;
136705dbe683SOmar Sandoval 				goto err;
136805dbe683SOmar Sandoval 			}
136905dbe683SOmar Sandoval 			read_extent_buffer(path->nodes[0], ptr + 1,
137005dbe683SOmar Sandoval 					   (unsigned long)(inode_ref + 1), len);
137105dbe683SOmar Sandoval 			ptr[0] = '/';
137205dbe683SOmar Sandoval 			btrfs_release_path(path);
137305dbe683SOmar Sandoval 		}
137400246528SJosef Bacik 		btrfs_put_root(fs_root);
13755168489aSJosef Bacik 		fs_root = NULL;
137605dbe683SOmar Sandoval 	}
137705dbe683SOmar Sandoval 
137805dbe683SOmar Sandoval 	btrfs_free_path(path);
137905dbe683SOmar Sandoval 	if (ptr == name + PATH_MAX - 1) {
138005dbe683SOmar Sandoval 		name[0] = '/';
138105dbe683SOmar Sandoval 		name[1] = '\0';
138205dbe683SOmar Sandoval 	} else {
138305dbe683SOmar Sandoval 		memmove(name, ptr, name + PATH_MAX - ptr);
138405dbe683SOmar Sandoval 	}
138505dbe683SOmar Sandoval 	return name;
138605dbe683SOmar Sandoval 
138705dbe683SOmar Sandoval err:
138800246528SJosef Bacik 	btrfs_put_root(fs_root);
138905dbe683SOmar Sandoval 	btrfs_free_path(path);
139005dbe683SOmar Sandoval 	kfree(name);
139105dbe683SOmar Sandoval 	return ERR_PTR(ret);
139205dbe683SOmar Sandoval }
139305dbe683SOmar Sandoval 
139405dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
139505dbe683SOmar Sandoval {
139605dbe683SOmar Sandoval 	struct btrfs_root *root = fs_info->tree_root;
139773f73415SJosef Bacik 	struct btrfs_dir_item *di;
139873f73415SJosef Bacik 	struct btrfs_path *path;
139973f73415SJosef Bacik 	struct btrfs_key location;
140073f73415SJosef Bacik 	u64 dir_id;
140173f73415SJosef Bacik 
140273f73415SJosef Bacik 	path = btrfs_alloc_path();
140373f73415SJosef Bacik 	if (!path)
140405dbe683SOmar Sandoval 		return -ENOMEM;
140573f73415SJosef Bacik 
140673f73415SJosef Bacik 	/*
140773f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
140873f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
140973f73415SJosef Bacik 	 * to mount.
141073f73415SJosef Bacik 	 */
1411815745cfSAl Viro 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
141273f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
1413b0839166SJulia Lawall 	if (IS_ERR(di)) {
1414b0839166SJulia Lawall 		btrfs_free_path(path);
141505dbe683SOmar Sandoval 		return PTR_ERR(di);
1416b0839166SJulia Lawall 	}
141773f73415SJosef Bacik 	if (!di) {
141873f73415SJosef Bacik 		/*
141973f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
142073f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
142105dbe683SOmar Sandoval 		 * mount the top-level subvolume.
142273f73415SJosef Bacik 		 */
142373f73415SJosef Bacik 		btrfs_free_path(path);
142405dbe683SOmar Sandoval 		*objectid = BTRFS_FS_TREE_OBJECTID;
142505dbe683SOmar Sandoval 		return 0;
142673f73415SJosef Bacik 	}
142773f73415SJosef Bacik 
142873f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
142973f73415SJosef Bacik 	btrfs_free_path(path);
143005dbe683SOmar Sandoval 	*objectid = location.objectid;
143105dbe683SOmar Sandoval 	return 0;
143273f73415SJosef Bacik }
143373f73415SJosef Bacik 
14348a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
14358a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
143656e033a7SDavid Sterba 			    void *data)
14372e635a27SChris Mason {
14382e635a27SChris Mason 	struct inode *inode;
1439815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
144039279cc3SChris Mason 	int err;
14412e635a27SChris Mason 
14422e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
14432e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
1444e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
1445af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
1446be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
144714605409SBoris Burkov #ifdef CONFIG_FS_VERITY
144814605409SBoris Burkov 	sb->s_vop = &btrfs_verityops;
144914605409SBoris Burkov #endif
14505103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
14512e635a27SChris Mason 	sb->s_time_gran = 1;
14520eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
14531751e8a6SLinus Torvalds 	sb->s_flags |= SB_POSIXACL;
145449cf6f45SChris Ball #endif
1455357fdad0SMatthew Garrett 	sb->s_flags |= SB_I_VERSION;
1456da2f0f74SChris Mason 	sb->s_iflags |= SB_I_CGROUPWB;
14579e11ceeeSJan Kara 
14589e11ceeeSJan Kara 	err = super_setup_bdi(sb);
14599e11ceeeSJan Kara 	if (err) {
14609e11ceeeSJan Kara 		btrfs_err(fs_info, "super_setup_bdi failed");
14619e11ceeeSJan Kara 		return err;
14629e11ceeeSJan Kara 	}
14639e11ceeeSJan Kara 
1464ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
1465ad2b2c80SAl Viro 	if (err) {
1466ab8d0fc4SJeff Mahoney 		btrfs_err(fs_info, "open_ctree failed");
1467ad2b2c80SAl Viro 		return err;
1468e20d96d6SChris Mason 	}
1469b888db2bSChris Mason 
14700202e83fSDavid Sterba 	inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
14715d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
14725d4f98a2SYan Zheng 		err = PTR_ERR(inode);
147339279cc3SChris Mason 		goto fail_close;
147439279cc3SChris Mason 	}
14752e635a27SChris Mason 
147648fde701SAl Viro 	sb->s_root = d_make_root(inode);
147748fde701SAl Viro 	if (!sb->s_root) {
147839279cc3SChris Mason 		err = -ENOMEM;
147939279cc3SChris Mason 		goto fail_close;
14802e635a27SChris Mason 	}
148158176a96SJosef Bacik 
14821751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
14832e635a27SChris Mason 	return 0;
14842e635a27SChris Mason 
148539279cc3SChris Mason fail_close:
14866bccf3abSJeff Mahoney 	close_ctree(fs_info);
1487d5719762SChris Mason 	return err;
1488d5719762SChris Mason }
1489d5719762SChris Mason 
14906bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
1491d5719762SChris Mason {
1492d5719762SChris Mason 	struct btrfs_trans_handle *trans;
1493815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1494815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
1495df2ce34cSChris Mason 
1496bc074524SJeff Mahoney 	trace_btrfs_sync_fs(fs_info, wait);
14971abe9b8aSliubo 
1498d561c025SChris Mason 	if (!wait) {
1499815745cfSAl Viro 		filemap_flush(fs_info->btree_inode->i_mapping);
1500df2ce34cSChris Mason 		return 0;
1501d561c025SChris Mason 	}
1502771ed689SChris Mason 
15036374e57aSChris Mason 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1504771ed689SChris Mason 
1505d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
150660376ce4SJosef Bacik 	if (IS_ERR(trans)) {
1507354aa0fbSMiao Xie 		/* no transaction, don't bother */
15086b5fe46dSDavid Sterba 		if (PTR_ERR(trans) == -ENOENT) {
15096b5fe46dSDavid Sterba 			/*
15106b5fe46dSDavid Sterba 			 * Exit unless we have some pending changes
15116b5fe46dSDavid Sterba 			 * that need to go through commit
15126b5fe46dSDavid Sterba 			 */
15136b5fe46dSDavid Sterba 			if (fs_info->pending_changes == 0)
1514bd7de2c9SJosef Bacik 				return 0;
1515a53f4f8eSQu Wenruo 			/*
1516a53f4f8eSQu Wenruo 			 * A non-blocking test if the fs is frozen. We must not
1517a53f4f8eSQu Wenruo 			 * start a new transaction here otherwise a deadlock
1518a53f4f8eSQu Wenruo 			 * happens. The pending operations are delayed to the
1519a53f4f8eSQu Wenruo 			 * next commit after thawing.
1520a53f4f8eSQu Wenruo 			 */
1521a7e3c5f2SRakesh Pandit 			if (sb_start_write_trylock(sb))
1522a7e3c5f2SRakesh Pandit 				sb_end_write(sb);
1523a53f4f8eSQu Wenruo 			else
1524a53f4f8eSQu Wenruo 				return 0;
15256b5fe46dSDavid Sterba 			trans = btrfs_start_transaction(root, 0);
152660376ce4SJosef Bacik 		}
152798bd5c54SDavid Sterba 		if (IS_ERR(trans))
152898bd5c54SDavid Sterba 			return PTR_ERR(trans);
15296b5fe46dSDavid Sterba 	}
15303a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
1531d5719762SChris Mason }
1532d5719762SChris Mason 
1533ab0b4a3eSJosef Bacik static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed)
1534ab0b4a3eSJosef Bacik {
1535ab0b4a3eSJosef Bacik 	seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s);
1536ab0b4a3eSJosef Bacik 	*printed = true;
1537ab0b4a3eSJosef Bacik }
1538ab0b4a3eSJosef Bacik 
153934c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1540a9572a15SEric Paris {
1541815745cfSAl Viro 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
15420f628c63SDavid Sterba 	const char *compress_type;
15433ef3959bSJosef Bacik 	const char *subvol_name;
1544ab0b4a3eSJosef Bacik 	bool printed = false;
1545a9572a15SEric Paris 
15463cdde224SJeff Mahoney 	if (btrfs_test_opt(info, DEGRADED))
1547a9572a15SEric Paris 		seq_puts(seq, ",degraded");
15483cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATASUM))
1549a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
15503cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATACOW))
1551a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
15523cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOBARRIER))
1553a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
155495ac567aSFilipe David Borba Manana 	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1555c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",max_inline=%llu", info->max_inline);
1556a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
1557a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
1558f7b885beSAnand Jain 		seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
15593cdde224SJeff Mahoney 	if (btrfs_test_opt(info, COMPRESS)) {
15600f628c63SDavid Sterba 		compress_type = btrfs_compress_type2str(info->compress_type);
15613cdde224SJeff Mahoney 		if (btrfs_test_opt(info, FORCE_COMPRESS))
1562200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
1563200da64eSTsutomu Itoh 		else
1564200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
1565f51d2b59SDavid Sterba 		if (info->compress_level)
1566fa4d885aSAdam Borowski 			seq_printf(seq, ":%d", info->compress_level);
1567200da64eSTsutomu Itoh 	}
15683cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOSSD))
1569c289811cSChris Mason 		seq_puts(seq, ",nossd");
15703cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SSD_SPREAD))
1571451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
15723cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, SSD))
1573a9572a15SEric Paris 		seq_puts(seq, ",ssd");
15743cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOTREELOG))
15756b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
15763cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOLOGREPLAY))
1577ab0b4a3eSJosef Bacik 		print_rescue_option(seq, "nologreplay", &printed);
157868319c18SJosef Bacik 	if (btrfs_test_opt(info, USEBACKUPROOT))
157968319c18SJosef Bacik 		print_rescue_option(seq, "usebackuproot", &printed);
158042437a63SJosef Bacik 	if (btrfs_test_opt(info, IGNOREBADROOTS))
158142437a63SJosef Bacik 		print_rescue_option(seq, "ignorebadroots", &printed);
1582882dbe0cSJosef Bacik 	if (btrfs_test_opt(info, IGNOREDATACSUMS))
1583882dbe0cSJosef Bacik 		print_rescue_option(seq, "ignoredatacsums", &printed);
15843cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FLUSHONCOMMIT))
15856b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
158646b27f50SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_SYNC))
158720a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
1588b0643e59SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_ASYNC))
1589b0643e59SDennis Zhou 		seq_puts(seq, ",discard=async");
15901751e8a6SLinus Torvalds 	if (!(info->sb->s_flags & SB_POSIXACL))
1591a9572a15SEric Paris 		seq_puts(seq, ",noacl");
159204c41559SBoris Burkov 	if (btrfs_free_space_cache_v1_active(info))
1593200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
159404c41559SBoris Burkov 	else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
159570f6d82eSOmar Sandoval 		seq_puts(seq, ",space_cache=v2");
159673bc1876SJosef Bacik 	else
15978965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
15983cdde224SJeff Mahoney 	if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1599f420ee1eSStefan Behrens 		seq_puts(seq, ",rescan_uuid_tree");
16003cdde224SJeff Mahoney 	if (btrfs_test_opt(info, CLEAR_CACHE))
1601200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
16023cdde224SJeff Mahoney 	if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1603200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
16043cdde224SJeff Mahoney 	if (btrfs_test_opt(info, ENOSPC_DEBUG))
16050942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
16063cdde224SJeff Mahoney 	if (btrfs_test_opt(info, AUTO_DEFRAG))
16070942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
16083cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SKIP_BALANCE))
16099555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
16108507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1611cbeaae4fSDavid Sterba 	if (btrfs_test_opt(info, CHECK_INTEGRITY_DATA))
16128507d216SWang Shilong 		seq_puts(seq, ",check_int_data");
16133cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, CHECK_INTEGRITY))
16148507d216SWang Shilong 		seq_puts(seq, ",check_int");
16158507d216SWang Shilong 	if (info->check_integrity_print_mask)
16168507d216SWang Shilong 		seq_printf(seq, ",check_int_print_mask=%d",
16178507d216SWang Shilong 				info->check_integrity_print_mask);
16188507d216SWang Shilong #endif
16198507d216SWang Shilong 	if (info->metadata_ratio)
1620764cb8b4SAnand Jain 		seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
16213cdde224SJeff Mahoney 	if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
16228c342930SJeff Mahoney 		seq_puts(seq, ",fatal_errors=panic");
16238b87dc17SDavid Sterba 	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1624d3740608SAnand Jain 		seq_printf(seq, ",commit=%u", info->commit_interval);
1625d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
16263cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_DATA))
1627d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=data");
16283cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_METADATA))
1629d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=metadata");
1630d0bd4560SJosef Bacik #endif
1631fb592373SJosef Bacik 	if (btrfs_test_opt(info, REF_VERIFY))
1632fb592373SJosef Bacik 		seq_puts(seq, ",ref_verify");
1633c8d3fe02SOmar Sandoval 	seq_printf(seq, ",subvolid=%llu",
1634c8d3fe02SOmar Sandoval 		  BTRFS_I(d_inode(dentry))->root->root_key.objectid);
16353ef3959bSJosef Bacik 	subvol_name = btrfs_get_subvol_name_from_objectid(info,
16363ef3959bSJosef Bacik 			BTRFS_I(d_inode(dentry))->root->root_key.objectid);
16373ef3959bSJosef Bacik 	if (!IS_ERR(subvol_name)) {
1638c8d3fe02SOmar Sandoval 		seq_puts(seq, ",subvol=");
16393ef3959bSJosef Bacik 		seq_escape(seq, subvol_name, " \t\n\\");
16403ef3959bSJosef Bacik 		kfree(subvol_name);
16413ef3959bSJosef Bacik 	}
1642a9572a15SEric Paris 	return 0;
1643a9572a15SEric Paris }
1644a9572a15SEric Paris 
1645a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
16462e635a27SChris Mason {
1647815745cfSAl Viro 	struct btrfs_fs_info *p = data;
1648815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(s);
16494b82d6e4SYan 
1650815745cfSAl Viro 	return fs_info->fs_devices == p->fs_devices;
16514b82d6e4SYan }
16524b82d6e4SYan 
1653450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
1654450ba0eaSJosef Bacik {
16556de1d09dSAl Viro 	int err = set_anon_super(s, data);
16566de1d09dSAl Viro 	if (!err)
1657450ba0eaSJosef Bacik 		s->s_fs_info = data;
16586de1d09dSAl Viro 	return err;
1659450ba0eaSJosef Bacik }
1660450ba0eaSJosef Bacik 
1661830c4adbSJosef Bacik /*
1662f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
1663f9d9ef62SDavid Sterba  */
1664f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
1665f9d9ef62SDavid Sterba {
1666f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1667f9d9ef62SDavid Sterba 		return 1;
1668f9d9ef62SDavid Sterba 	return 0;
1669f9d9ef62SDavid Sterba }
1670f9d9ef62SDavid Sterba 
1671bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1672ae0bc863SAnand Jain 				   struct vfsmount *mnt)
1673830c4adbSJosef Bacik {
1674830c4adbSJosef Bacik 	struct dentry *root;
1675fa330659SOmar Sandoval 	int ret;
1676830c4adbSJosef Bacik 
167705dbe683SOmar Sandoval 	if (!subvol_name) {
167805dbe683SOmar Sandoval 		if (!subvol_objectid) {
167905dbe683SOmar Sandoval 			ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
168005dbe683SOmar Sandoval 							  &subvol_objectid);
168105dbe683SOmar Sandoval 			if (ret) {
168205dbe683SOmar Sandoval 				root = ERR_PTR(ret);
168305dbe683SOmar Sandoval 				goto out;
168405dbe683SOmar Sandoval 			}
168505dbe683SOmar Sandoval 		}
1686c0c907a4SMarcos Paulo de Souza 		subvol_name = btrfs_get_subvol_name_from_objectid(
1687c0c907a4SMarcos Paulo de Souza 					btrfs_sb(mnt->mnt_sb), subvol_objectid);
168805dbe683SOmar Sandoval 		if (IS_ERR(subvol_name)) {
168905dbe683SOmar Sandoval 			root = ERR_CAST(subvol_name);
169005dbe683SOmar Sandoval 			subvol_name = NULL;
169105dbe683SOmar Sandoval 			goto out;
169205dbe683SOmar Sandoval 		}
169305dbe683SOmar Sandoval 
169405dbe683SOmar Sandoval 	}
169505dbe683SOmar Sandoval 
1696ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1697fa330659SOmar Sandoval 	/* mount_subtree() drops our reference on the vfsmount. */
1698fa330659SOmar Sandoval 	mnt = NULL;
1699830c4adbSJosef Bacik 
1700bb289b7bSOmar Sandoval 	if (!IS_ERR(root)) {
1701ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1702ab8d0fc4SJeff Mahoney 		struct btrfs_fs_info *fs_info = btrfs_sb(s);
1703bb289b7bSOmar Sandoval 		struct inode *root_inode = d_inode(root);
1704bb289b7bSOmar Sandoval 		u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1705bb289b7bSOmar Sandoval 
1706bb289b7bSOmar Sandoval 		ret = 0;
1707bb289b7bSOmar Sandoval 		if (!is_subvolume_inode(root_inode)) {
1708ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info, "'%s' is not a valid subvolume",
1709bb289b7bSOmar Sandoval 			       subvol_name);
1710bb289b7bSOmar Sandoval 			ret = -EINVAL;
1711bb289b7bSOmar Sandoval 		}
1712bb289b7bSOmar Sandoval 		if (subvol_objectid && root_objectid != subvol_objectid) {
171305dbe683SOmar Sandoval 			/*
171405dbe683SOmar Sandoval 			 * This will also catch a race condition where a
171505dbe683SOmar Sandoval 			 * subvolume which was passed by ID is renamed and
171605dbe683SOmar Sandoval 			 * another subvolume is renamed over the old location.
171705dbe683SOmar Sandoval 			 */
1718ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info,
1719ab8d0fc4SJeff Mahoney 				  "subvol '%s' does not match subvolid %llu",
1720bb289b7bSOmar Sandoval 				  subvol_name, subvol_objectid);
1721bb289b7bSOmar Sandoval 			ret = -EINVAL;
1722bb289b7bSOmar Sandoval 		}
1723bb289b7bSOmar Sandoval 		if (ret) {
1724ea441d11SAl Viro 			dput(root);
1725bb289b7bSOmar Sandoval 			root = ERR_PTR(ret);
1726ea441d11SAl Viro 			deactivate_locked_super(s);
1727bb289b7bSOmar Sandoval 		}
1728f9d9ef62SDavid Sterba 	}
1729f9d9ef62SDavid Sterba 
1730fa330659SOmar Sandoval out:
1731fa330659SOmar Sandoval 	mntput(mnt);
1732fa330659SOmar Sandoval 	kfree(subvol_name);
1733830c4adbSJosef Bacik 	return root;
1734830c4adbSJosef Bacik }
1735450ba0eaSJosef Bacik 
1736312c89fbSMisono, Tomohiro /*
1737312c89fbSMisono, Tomohiro  * Find a superblock for the given device / mount point.
1738312c89fbSMisono, Tomohiro  *
1739312c89fbSMisono, Tomohiro  * Note: This is based on mount_bdev from fs/super.c with a few additions
1740312c89fbSMisono, Tomohiro  *       for multiple device setup.  Make sure to keep it in sync.
1741312c89fbSMisono, Tomohiro  */
174272fa39f5SMisono, Tomohiro static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
174372fa39f5SMisono, Tomohiro 		int flags, const char *device_name, void *data)
174472fa39f5SMisono, Tomohiro {
174572fa39f5SMisono, Tomohiro 	struct block_device *bdev = NULL;
174672fa39f5SMisono, Tomohiro 	struct super_block *s;
174736350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
174872fa39f5SMisono, Tomohiro 	struct btrfs_fs_devices *fs_devices = NULL;
174972fa39f5SMisono, Tomohiro 	struct btrfs_fs_info *fs_info = NULL;
1750204cc0ccSAl Viro 	void *new_sec_opts = NULL;
175172fa39f5SMisono, Tomohiro 	fmode_t mode = FMODE_READ;
175272fa39f5SMisono, Tomohiro 	int error = 0;
175372fa39f5SMisono, Tomohiro 
175472fa39f5SMisono, Tomohiro 	if (!(flags & SB_RDONLY))
175572fa39f5SMisono, Tomohiro 		mode |= FMODE_WRITE;
175672fa39f5SMisono, Tomohiro 
175772fa39f5SMisono, Tomohiro 	if (data) {
1758a65001e8SAl Viro 		error = security_sb_eat_lsm_opts(data, &new_sec_opts);
175972fa39f5SMisono, Tomohiro 		if (error)
176072fa39f5SMisono, Tomohiro 			return ERR_PTR(error);
176172fa39f5SMisono, Tomohiro 	}
176272fa39f5SMisono, Tomohiro 
176372fa39f5SMisono, Tomohiro 	/*
176472fa39f5SMisono, Tomohiro 	 * Setup a dummy root and fs_info for test/set super.  This is because
176572fa39f5SMisono, Tomohiro 	 * we don't actually fill this stuff out until open_ctree, but we need
17668260edbaSJosef Bacik 	 * then open_ctree will properly initialize the file system specific
17678260edbaSJosef Bacik 	 * settings later.  btrfs_init_fs_info initializes the static elements
17688260edbaSJosef Bacik 	 * of the fs_info (locks and such) to make cleanup easier if we find a
17698260edbaSJosef Bacik 	 * superblock with our given fs_devices later on at sget() time.
177072fa39f5SMisono, Tomohiro 	 */
1771a8fd1f71SJeff Mahoney 	fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
177272fa39f5SMisono, Tomohiro 	if (!fs_info) {
177372fa39f5SMisono, Tomohiro 		error = -ENOMEM;
177472fa39f5SMisono, Tomohiro 		goto error_sec_opts;
177572fa39f5SMisono, Tomohiro 	}
17768260edbaSJosef Bacik 	btrfs_init_fs_info(fs_info);
177772fa39f5SMisono, Tomohiro 
177872fa39f5SMisono, Tomohiro 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
177972fa39f5SMisono, Tomohiro 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
178072fa39f5SMisono, Tomohiro 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
178172fa39f5SMisono, Tomohiro 		error = -ENOMEM;
178272fa39f5SMisono, Tomohiro 		goto error_fs_info;
178372fa39f5SMisono, Tomohiro 	}
178472fa39f5SMisono, Tomohiro 
1785f5194e34SDavid Sterba 	mutex_lock(&uuid_mutex);
1786fa59f27cSAnand Jain 	error = btrfs_parse_device_options(data, mode, fs_type);
178781ffd56bSDavid Sterba 	if (error) {
1788399f7f4cSDavid Sterba 		mutex_unlock(&uuid_mutex);
1789399f7f4cSDavid Sterba 		goto error_fs_info;
179081ffd56bSDavid Sterba 	}
1791399f7f4cSDavid Sterba 
179236350e95SGu Jinxiang 	device = btrfs_scan_one_device(device_name, mode, fs_type);
179336350e95SGu Jinxiang 	if (IS_ERR(device)) {
1794399f7f4cSDavid Sterba 		mutex_unlock(&uuid_mutex);
179536350e95SGu Jinxiang 		error = PTR_ERR(device);
1796399f7f4cSDavid Sterba 		goto error_fs_info;
179781ffd56bSDavid Sterba 	}
1798399f7f4cSDavid Sterba 
179936350e95SGu Jinxiang 	fs_devices = device->fs_devices;
1800399f7f4cSDavid Sterba 	fs_info->fs_devices = fs_devices;
1801399f7f4cSDavid Sterba 
180272fa39f5SMisono, Tomohiro 	error = btrfs_open_devices(fs_devices, mode, fs_type);
1803f5194e34SDavid Sterba 	mutex_unlock(&uuid_mutex);
180472fa39f5SMisono, Tomohiro 	if (error)
180572fa39f5SMisono, Tomohiro 		goto error_fs_info;
180672fa39f5SMisono, Tomohiro 
180772fa39f5SMisono, Tomohiro 	if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
180872fa39f5SMisono, Tomohiro 		error = -EACCES;
180972fa39f5SMisono, Tomohiro 		goto error_close_devices;
181072fa39f5SMisono, Tomohiro 	}
181172fa39f5SMisono, Tomohiro 
1812d24fa5c1SAnand Jain 	bdev = fs_devices->latest_dev->bdev;
181372fa39f5SMisono, Tomohiro 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
181472fa39f5SMisono, Tomohiro 		 fs_info);
181572fa39f5SMisono, Tomohiro 	if (IS_ERR(s)) {
181672fa39f5SMisono, Tomohiro 		error = PTR_ERR(s);
181772fa39f5SMisono, Tomohiro 		goto error_close_devices;
181872fa39f5SMisono, Tomohiro 	}
181972fa39f5SMisono, Tomohiro 
182072fa39f5SMisono, Tomohiro 	if (s->s_root) {
182172fa39f5SMisono, Tomohiro 		btrfs_close_devices(fs_devices);
18220d4b0463SJosef Bacik 		btrfs_free_fs_info(fs_info);
182372fa39f5SMisono, Tomohiro 		if ((flags ^ s->s_flags) & SB_RDONLY)
182472fa39f5SMisono, Tomohiro 			error = -EBUSY;
182572fa39f5SMisono, Tomohiro 	} else {
182672fa39f5SMisono, Tomohiro 		snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
1827e33c267aSRoman Gushchin 		shrinker_debugfs_rename(&s->s_shrink, "sb-%s:%s", fs_type->name,
1828e33c267aSRoman Gushchin 					s->s_id);
182972fa39f5SMisono, Tomohiro 		btrfs_sb(s)->bdev_holder = fs_type;
18309b4e675aSDavid Sterba 		if (!strstr(crc32c_impl(), "generic"))
18319b4e675aSDavid Sterba 			set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
183272fa39f5SMisono, Tomohiro 		error = btrfs_fill_super(s, fs_devices, data);
183372fa39f5SMisono, Tomohiro 	}
1834a65001e8SAl Viro 	if (!error)
1835204cc0ccSAl Viro 		error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL);
1836a65001e8SAl Viro 	security_free_mnt_opts(&new_sec_opts);
183772fa39f5SMisono, Tomohiro 	if (error) {
183872fa39f5SMisono, Tomohiro 		deactivate_locked_super(s);
1839a65001e8SAl Viro 		return ERR_PTR(error);
184072fa39f5SMisono, Tomohiro 	}
184172fa39f5SMisono, Tomohiro 
184272fa39f5SMisono, Tomohiro 	return dget(s->s_root);
184372fa39f5SMisono, Tomohiro 
184472fa39f5SMisono, Tomohiro error_close_devices:
184572fa39f5SMisono, Tomohiro 	btrfs_close_devices(fs_devices);
184672fa39f5SMisono, Tomohiro error_fs_info:
18470d4b0463SJosef Bacik 	btrfs_free_fs_info(fs_info);
184872fa39f5SMisono, Tomohiro error_sec_opts:
184972fa39f5SMisono, Tomohiro 	security_free_mnt_opts(&new_sec_opts);
185072fa39f5SMisono, Tomohiro 	return ERR_PTR(error);
185172fa39f5SMisono, Tomohiro }
1852312c89fbSMisono, Tomohiro 
1853edf24abeSChristoph Hellwig /*
1854312c89fbSMisono, Tomohiro  * Mount function which is called by VFS layer.
1855edf24abeSChristoph Hellwig  *
1856312c89fbSMisono, Tomohiro  * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1857312c89fbSMisono, Tomohiro  * which needs vfsmount* of device's root (/).  This means device's root has to
1858312c89fbSMisono, Tomohiro  * be mounted internally in any case.
1859312c89fbSMisono, Tomohiro  *
1860312c89fbSMisono, Tomohiro  * Operation flow:
1861312c89fbSMisono, Tomohiro  *   1. Parse subvol id related options for later use in mount_subvol().
1862312c89fbSMisono, Tomohiro  *
1863312c89fbSMisono, Tomohiro  *   2. Mount device's root (/) by calling vfs_kern_mount().
1864312c89fbSMisono, Tomohiro  *
1865312c89fbSMisono, Tomohiro  *      NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1866312c89fbSMisono, Tomohiro  *      first place. In order to avoid calling btrfs_mount() again, we use
1867312c89fbSMisono, Tomohiro  *      different file_system_type which is not registered to VFS by
1868312c89fbSMisono, Tomohiro  *      register_filesystem() (btrfs_root_fs_type). As a result,
1869312c89fbSMisono, Tomohiro  *      btrfs_mount_root() is called. The return value will be used by
1870312c89fbSMisono, Tomohiro  *      mount_subtree() in mount_subvol().
1871312c89fbSMisono, Tomohiro  *
1872312c89fbSMisono, Tomohiro  *   3. Call mount_subvol() to get the dentry of subvolume. Since there is
1873312c89fbSMisono, Tomohiro  *      "btrfs subvolume set-default", mount_subvol() is called always.
1874edf24abeSChristoph Hellwig  */
1875061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1876306e16ceSDavid Sterba 		const char *device_name, void *data)
18774b82d6e4SYan {
1878312c89fbSMisono, Tomohiro 	struct vfsmount *mnt_root;
1879312c89fbSMisono, Tomohiro 	struct dentry *root;
188073f73415SJosef Bacik 	char *subvol_name = NULL;
188173f73415SJosef Bacik 	u64 subvol_objectid = 0;
18824b82d6e4SYan 	int error = 0;
18834b82d6e4SYan 
188493b9bcdfSGu Jinxiang 	error = btrfs_parse_subvol_options(data, &subvol_name,
188593b9bcdfSGu Jinxiang 					&subvol_objectid);
1886f23c8af8SIlya Dryomov 	if (error) {
1887f23c8af8SIlya Dryomov 		kfree(subvol_name);
1888061dbc6bSAl Viro 		return ERR_PTR(error);
1889f23c8af8SIlya Dryomov 	}
1890edf24abeSChristoph Hellwig 
1891312c89fbSMisono, Tomohiro 	/* mount device's root (/) */
1892312c89fbSMisono, Tomohiro 	mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data);
1893312c89fbSMisono, Tomohiro 	if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) {
1894312c89fbSMisono, Tomohiro 		if (flags & SB_RDONLY) {
1895312c89fbSMisono, Tomohiro 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1896312c89fbSMisono, Tomohiro 				flags & ~SB_RDONLY, device_name, data);
18974b82d6e4SYan 		} else {
1898312c89fbSMisono, Tomohiro 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1899312c89fbSMisono, Tomohiro 				flags | SB_RDONLY, device_name, data);
1900312c89fbSMisono, Tomohiro 			if (IS_ERR(mnt_root)) {
1901312c89fbSMisono, Tomohiro 				root = ERR_CAST(mnt_root);
1902532b618bSEric W. Biederman 				kfree(subvol_name);
1903312c89fbSMisono, Tomohiro 				goto out;
1904f667aef6SQu Wenruo 			}
1905f667aef6SQu Wenruo 
1906312c89fbSMisono, Tomohiro 			down_write(&mnt_root->mnt_sb->s_umount);
1907312c89fbSMisono, Tomohiro 			error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL);
1908312c89fbSMisono, Tomohiro 			up_write(&mnt_root->mnt_sb->s_umount);
1909312c89fbSMisono, Tomohiro 			if (error < 0) {
1910312c89fbSMisono, Tomohiro 				root = ERR_PTR(error);
1911312c89fbSMisono, Tomohiro 				mntput(mnt_root);
1912532b618bSEric W. Biederman 				kfree(subvol_name);
1913312c89fbSMisono, Tomohiro 				goto out;
1914312c89fbSMisono, Tomohiro 			}
1915312c89fbSMisono, Tomohiro 		}
1916312c89fbSMisono, Tomohiro 	}
1917312c89fbSMisono, Tomohiro 	if (IS_ERR(mnt_root)) {
1918312c89fbSMisono, Tomohiro 		root = ERR_CAST(mnt_root);
1919532b618bSEric W. Biederman 		kfree(subvol_name);
1920312c89fbSMisono, Tomohiro 		goto out;
1921f667aef6SQu Wenruo 	}
19224b82d6e4SYan 
1923312c89fbSMisono, Tomohiro 	/* mount_subvol() will free subvol_name and mnt_root */
1924ae0bc863SAnand Jain 	root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
19254b82d6e4SYan 
1926312c89fbSMisono, Tomohiro out:
1927312c89fbSMisono, Tomohiro 	return root;
19284b82d6e4SYan }
19292e635a27SChris Mason 
19300d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1931f7b885beSAnand Jain 				     u32 new_pool_size, u32 old_pool_size)
19320d2450abSSergei Trofimovich {
19330d2450abSSergei Trofimovich 	if (new_pool_size == old_pool_size)
19340d2450abSSergei Trofimovich 		return;
19350d2450abSSergei Trofimovich 
19360d2450abSSergei Trofimovich 	fs_info->thread_pool_size = new_pool_size;
19370d2450abSSergei Trofimovich 
1938efe120a0SFrank Holton 	btrfs_info(fs_info, "resize thread pool %d -> %d",
19390d2450abSSergei Trofimovich 	       old_pool_size, new_pool_size);
19400d2450abSSergei Trofimovich 
19415cdc7ad3SQu Wenruo 	btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1942a31b4a43SChristoph Hellwig 	btrfs_workqueue_set_max(fs_info->hipri_workers, new_pool_size);
1943afe3d242SQu Wenruo 	btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1944e66f0bb1SQu Wenruo 	btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1945fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1946fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
19475b3bc44eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
19480d2450abSSergei Trofimovich }
19490d2450abSSergei Trofimovich 
1950f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1951f42a34b2SMiao Xie 				       unsigned long old_opts, int flags)
1952f42a34b2SMiao Xie {
1953dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1954dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
19551751e8a6SLinus Torvalds 	     (flags & SB_RDONLY))) {
1956dc81cdc5SMiao Xie 		/* wait for any defraggers to finish */
1957dc81cdc5SMiao Xie 		wait_event(fs_info->transaction_wait,
1958dc81cdc5SMiao Xie 			   (atomic_read(&fs_info->defrag_running) == 0));
19591751e8a6SLinus Torvalds 		if (flags & SB_RDONLY)
1960dc81cdc5SMiao Xie 			sync_filesystem(fs_info->sb);
1961dc81cdc5SMiao Xie 	}
1962dc81cdc5SMiao Xie }
1963dc81cdc5SMiao Xie 
1964dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1965dc81cdc5SMiao Xie 					 unsigned long old_opts)
1966dc81cdc5SMiao Xie {
196794846229SBoris Burkov 	const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
196894846229SBoris Burkov 
1969dc81cdc5SMiao Xie 	/*
1970180e4d47SLuis de Bethencourt 	 * We need to cleanup all defragable inodes if the autodefragment is
1971180e4d47SLuis de Bethencourt 	 * close or the filesystem is read only.
1972dc81cdc5SMiao Xie 	 */
1973dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1974bc98a42cSDavid Howells 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1975dc81cdc5SMiao Xie 		btrfs_cleanup_defrag_inodes(fs_info);
1976dc81cdc5SMiao Xie 	}
1977dc81cdc5SMiao Xie 
1978b0643e59SDennis Zhou 	/* If we toggled discard async */
1979b0643e59SDennis Zhou 	if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1980b0643e59SDennis Zhou 	    btrfs_test_opt(fs_info, DISCARD_ASYNC))
1981b0643e59SDennis Zhou 		btrfs_discard_resume(fs_info);
1982b0643e59SDennis Zhou 	else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1983b0643e59SDennis Zhou 		 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1984b0643e59SDennis Zhou 		btrfs_discard_cleanup(fs_info);
198594846229SBoris Burkov 
198694846229SBoris Burkov 	/* If we toggled space cache */
198794846229SBoris Burkov 	if (cache_opt != btrfs_free_space_cache_v1_active(fs_info))
198894846229SBoris Burkov 		btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
1989dc81cdc5SMiao Xie }
1990dc81cdc5SMiao Xie 
1991c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1992c146afadSYan Zheng {
1993815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
199449b25e05SJeff Mahoney 	unsigned old_flags = sb->s_flags;
199549b25e05SJeff Mahoney 	unsigned long old_opts = fs_info->mount_opt;
199649b25e05SJeff Mahoney 	unsigned long old_compress_type = fs_info->compress_type;
199749b25e05SJeff Mahoney 	u64 old_max_inline = fs_info->max_inline;
1998f7b885beSAnand Jain 	u32 old_thread_pool_size = fs_info->thread_pool_size;
1999d612ac59SAnand Jain 	u32 old_metadata_ratio = fs_info->metadata_ratio;
2000c146afadSYan Zheng 	int ret;
2001c146afadSYan Zheng 
200202b9984dSTheodore Ts'o 	sync_filesystem(sb);
200388c4703fSJohannes Thumshirn 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
2004dc81cdc5SMiao Xie 
2005f667aef6SQu Wenruo 	if (data) {
2006204cc0ccSAl Viro 		void *new_sec_opts = NULL;
2007f667aef6SQu Wenruo 
2008a65001e8SAl Viro 		ret = security_sb_eat_lsm_opts(data, &new_sec_opts);
2009a65001e8SAl Viro 		if (!ret)
2010204cc0ccSAl Viro 			ret = security_sb_remount(sb, new_sec_opts);
2011a65001e8SAl Viro 		security_free_mnt_opts(&new_sec_opts);
2012f667aef6SQu Wenruo 		if (ret)
2013f667aef6SQu Wenruo 			goto restore;
2014f667aef6SQu Wenruo 	}
2015f667aef6SQu Wenruo 
20162ff7e61eSJeff Mahoney 	ret = btrfs_parse_options(fs_info, data, *flags);
2017891f41cbSChengguang Xu 	if (ret)
201849b25e05SJeff Mahoney 		goto restore;
2019b288052eSChris Mason 
2020d7f67ac9SQu Wenruo 	ret = btrfs_check_features(fs_info, sb);
2021d7f67ac9SQu Wenruo 	if (ret < 0)
20220591f040SQu Wenruo 		goto restore;
2023d7f67ac9SQu Wenruo 
2024f42a34b2SMiao Xie 	btrfs_remount_begin(fs_info, old_opts, *flags);
20250d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
20260d2450abSSergei Trofimovich 		fs_info->thread_pool_size, old_thread_pool_size);
20270d2450abSSergei Trofimovich 
2028c55a4319SBoris Burkov 	if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) !=
2029c55a4319SBoris Burkov 	    (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
20302838d255SBoris Burkov 	    (!sb_rdonly(sb) || (*flags & SB_RDONLY))) {
20312838d255SBoris Burkov 		btrfs_warn(fs_info,
20322838d255SBoris Burkov 		"remount supports changing free space tree only from ro to rw");
20332838d255SBoris Burkov 		/* Make sure free space cache options match the state on disk */
20342838d255SBoris Burkov 		if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
20352838d255SBoris Burkov 			btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
20362838d255SBoris Burkov 			btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
20372838d255SBoris Burkov 		}
20382838d255SBoris Burkov 		if (btrfs_free_space_cache_v1_active(fs_info)) {
20392838d255SBoris Burkov 			btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
20402838d255SBoris Burkov 			btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
20412838d255SBoris Burkov 		}
20422838d255SBoris Burkov 	}
20432838d255SBoris Burkov 
20441751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
2045dc81cdc5SMiao Xie 		goto out;
2046c146afadSYan Zheng 
20471751e8a6SLinus Torvalds 	if (*flags & SB_RDONLY) {
20488dabb742SStefan Behrens 		/*
20498dabb742SStefan Behrens 		 * this also happens on 'umount -rf' or on shutdown, when
20508dabb742SStefan Behrens 		 * the filesystem is busy.
20518dabb742SStefan Behrens 		 */
205221c7e756SMiao Xie 		cancel_work_sync(&fs_info->async_reclaim_work);
205357056740SJosef Bacik 		cancel_work_sync(&fs_info->async_data_reclaim_work);
2054361c093dSStefan Behrens 
2055b0643e59SDennis Zhou 		btrfs_discard_cleanup(fs_info);
2056b0643e59SDennis Zhou 
2057361c093dSStefan Behrens 		/* wait for the uuid_scan task to finish */
2058361c093dSStefan Behrens 		down(&fs_info->uuid_tree_rescan_sem);
2059361c093dSStefan Behrens 		/* avoid complains from lockdep et al. */
2060361c093dSStefan Behrens 		up(&fs_info->uuid_tree_rescan_sem);
2061361c093dSStefan Behrens 
2062a0a1db70SFilipe Manana 		btrfs_set_sb_rdonly(sb);
2063c146afadSYan Zheng 
2064e44163e1SJeff Mahoney 		/*
20651751e8a6SLinus Torvalds 		 * Setting SB_RDONLY will put the cleaner thread to
2066e44163e1SJeff Mahoney 		 * sleep at the next loop if it's already active.
2067e44163e1SJeff Mahoney 		 * If it's already asleep, we'll leave unused block
2068e44163e1SJeff Mahoney 		 * groups on disk until we're mounted read-write again
2069e44163e1SJeff Mahoney 		 * unless we clean them up here.
2070e44163e1SJeff Mahoney 		 */
2071e44163e1SJeff Mahoney 		btrfs_delete_unused_bgs(fs_info);
2072e44163e1SJeff Mahoney 
2073a0a1db70SFilipe Manana 		/*
2074a0a1db70SFilipe Manana 		 * The cleaner task could be already running before we set the
2075a0a1db70SFilipe Manana 		 * flag BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock).
2076a0a1db70SFilipe Manana 		 * We must make sure that after we finish the remount, i.e. after
2077a0a1db70SFilipe Manana 		 * we call btrfs_commit_super(), the cleaner can no longer start
2078a0a1db70SFilipe Manana 		 * a transaction - either because it was dropping a dead root,
2079a0a1db70SFilipe Manana 		 * running delayed iputs or deleting an unused block group (the
2080a0a1db70SFilipe Manana 		 * cleaner picked a block group from the list of unused block
2081a0a1db70SFilipe Manana 		 * groups before we were able to in the previous call to
2082a0a1db70SFilipe Manana 		 * btrfs_delete_unused_bgs()).
2083a0a1db70SFilipe Manana 		 */
2084a0a1db70SFilipe Manana 		wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING,
2085a0a1db70SFilipe Manana 			    TASK_UNINTERRUPTIBLE);
2086a0a1db70SFilipe Manana 
2087a8cc263eSFilipe Manana 		/*
2088a8cc263eSFilipe Manana 		 * We've set the superblock to RO mode, so we might have made
2089a8cc263eSFilipe Manana 		 * the cleaner task sleep without running all pending delayed
2090a8cc263eSFilipe Manana 		 * iputs. Go through all the delayed iputs here, so that if an
2091a8cc263eSFilipe Manana 		 * unmount happens without remounting RW we don't end up at
2092a8cc263eSFilipe Manana 		 * finishing close_ctree() with a non-empty list of delayed
2093a8cc263eSFilipe Manana 		 * iputs.
2094a8cc263eSFilipe Manana 		 */
2095a8cc263eSFilipe Manana 		btrfs_run_delayed_iputs(fs_info);
2096a8cc263eSFilipe Manana 
20978dabb742SStefan Behrens 		btrfs_dev_replace_suspend_for_unmount(fs_info);
20988dabb742SStefan Behrens 		btrfs_scrub_cancel(fs_info);
2099061594efSMiao Xie 		btrfs_pause_balance(fs_info);
21008dabb742SStefan Behrens 
2101cb13eea3SFilipe Manana 		/*
2102cb13eea3SFilipe Manana 		 * Pause the qgroup rescan worker if it is running. We don't want
2103cb13eea3SFilipe Manana 		 * it to be still running after we are in RO mode, as after that,
2104cb13eea3SFilipe Manana 		 * by the time we unmount, it might have left a transaction open,
2105cb13eea3SFilipe Manana 		 * so we would leak the transaction and/or crash.
2106cb13eea3SFilipe Manana 		 */
2107cb13eea3SFilipe Manana 		btrfs_qgroup_wait_for_completion(fs_info, false);
2108cb13eea3SFilipe Manana 
21096bccf3abSJeff Mahoney 		ret = btrfs_commit_super(fs_info);
211049b25e05SJeff Mahoney 		if (ret)
211149b25e05SJeff Mahoney 			goto restore;
2112c146afadSYan Zheng 	} else {
211384961539SJosef Bacik 		if (BTRFS_FS_ERROR(fs_info)) {
21146ef3de9cSDavid Sterba 			btrfs_err(fs_info,
2115efe120a0SFrank Holton 				"Remounting read-write after error is not allowed");
21166ef3de9cSDavid Sterba 			ret = -EINVAL;
21176ef3de9cSDavid Sterba 			goto restore;
21186ef3de9cSDavid Sterba 		}
21198a3db184SSergei Trofimovich 		if (fs_info->fs_devices->rw_devices == 0) {
212049b25e05SJeff Mahoney 			ret = -EACCES;
212149b25e05SJeff Mahoney 			goto restore;
21228a3db184SSergei Trofimovich 		}
21232b82032cSYan Zheng 
21246528b99dSAnand Jain 		if (!btrfs_check_rw_degradable(fs_info, NULL)) {
2125efe120a0SFrank Holton 			btrfs_warn(fs_info,
212652042d8eSAndrea Gelmini 		"too many missing devices, writable remount is not allowed");
2127292fd7fcSStefan Behrens 			ret = -EACCES;
2128292fd7fcSStefan Behrens 			goto restore;
2129292fd7fcSStefan Behrens 		}
2130292fd7fcSStefan Behrens 
21318a3db184SSergei Trofimovich 		if (btrfs_super_log_root(fs_info->super_copy) != 0) {
213210a3a3edSDavid Sterba 			btrfs_warn(fs_info,
213310a3a3edSDavid Sterba 		"mount required to replay tree-log, cannot remount read-write");
213449b25e05SJeff Mahoney 			ret = -EINVAL;
213549b25e05SJeff Mahoney 			goto restore;
21368a3db184SSergei Trofimovich 		}
2137c146afadSYan Zheng 
213844c0ca21SBoris Burkov 		/*
213944c0ca21SBoris Burkov 		 * NOTE: when remounting with a change that does writes, don't
214044c0ca21SBoris Burkov 		 * put it anywhere above this point, as we are not sure to be
214144c0ca21SBoris Burkov 		 * safe to write until we pass the above checks.
214244c0ca21SBoris Burkov 		 */
214344c0ca21SBoris Burkov 		ret = btrfs_start_pre_rw_mount(fs_info);
214449b25e05SJeff Mahoney 		if (ret)
214549b25e05SJeff Mahoney 			goto restore;
2146c146afadSYan Zheng 
2147a0a1db70SFilipe Manana 		btrfs_clear_sb_rdonly(sb);
214890c711abSZygo Blaxell 
2149afcdd129SJosef Bacik 		set_bit(BTRFS_FS_OPEN, &fs_info->flags);
2150c146afadSYan Zheng 	}
2151dc81cdc5SMiao Xie out:
2152faa00889SJosef Bacik 	/*
2153faa00889SJosef Bacik 	 * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS,
2154faa00889SJosef Bacik 	 * since the absence of the flag means it can be toggled off by remount.
2155faa00889SJosef Bacik 	 */
2156faa00889SJosef Bacik 	*flags |= SB_I_VERSION;
2157faa00889SJosef Bacik 
21582c6a92b0SJustin Maggard 	wake_up_process(fs_info->transaction_kthread);
2159dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
21608cd29088SBoris Burkov 	btrfs_clear_oneshot_options(fs_info);
216188c4703fSJohannes Thumshirn 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
216288c4703fSJohannes Thumshirn 
2163c146afadSYan Zheng 	return 0;
216449b25e05SJeff Mahoney 
216549b25e05SJeff Mahoney restore:
21661751e8a6SLinus Torvalds 	/* We've hit an error - don't reset SB_RDONLY */
2167bc98a42cSDavid Howells 	if (sb_rdonly(sb))
21681751e8a6SLinus Torvalds 		old_flags |= SB_RDONLY;
2169a0a1db70SFilipe Manana 	if (!(old_flags & SB_RDONLY))
2170a0a1db70SFilipe Manana 		clear_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state);
217149b25e05SJeff Mahoney 	sb->s_flags = old_flags;
217249b25e05SJeff Mahoney 	fs_info->mount_opt = old_opts;
217349b25e05SJeff Mahoney 	fs_info->compress_type = old_compress_type;
217449b25e05SJeff Mahoney 	fs_info->max_inline = old_max_inline;
21750d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
21760d2450abSSergei Trofimovich 		old_thread_pool_size, fs_info->thread_pool_size);
217749b25e05SJeff Mahoney 	fs_info->metadata_ratio = old_metadata_ratio;
2178dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
217988c4703fSJohannes Thumshirn 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
218088c4703fSJohannes Thumshirn 
218149b25e05SJeff Mahoney 	return ret;
2182c146afadSYan Zheng }
2183c146afadSYan Zheng 
2184bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
2185214cc184SDavid Sterba static int btrfs_cmp_device_free_bytes(const void *a, const void *b)
2186bcd53741SArne Jansen {
2187214cc184SDavid Sterba 	const struct btrfs_device_info *dev_info1 = a;
2188214cc184SDavid Sterba 	const struct btrfs_device_info *dev_info2 = b;
2189214cc184SDavid Sterba 
2190214cc184SDavid Sterba 	if (dev_info1->max_avail > dev_info2->max_avail)
2191bcd53741SArne Jansen 		return -1;
2192214cc184SDavid Sterba 	else if (dev_info1->max_avail < dev_info2->max_avail)
2193bcd53741SArne Jansen 		return 1;
2194bcd53741SArne Jansen 	return 0;
2195bcd53741SArne Jansen }
2196bcd53741SArne Jansen 
2197bcd53741SArne Jansen /*
2198bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
2199bcd53741SArne Jansen  * is stored.(Descending Sort)
2200bcd53741SArne Jansen  */
2201bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
2202bcd53741SArne Jansen 					struct btrfs_device_info *devices,
2203bcd53741SArne Jansen 					size_t nr_devices)
2204bcd53741SArne Jansen {
2205bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
2206bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
2207bcd53741SArne Jansen }
2208bcd53741SArne Jansen 
22096d07bcecSMiao Xie /*
22106d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
22116d07bcecSMiao Xie  * file data.
22126d07bcecSMiao Xie  */
22137e17916bSArnd Bergmann static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
22146bccf3abSJeff Mahoney 					      u64 *free_bytes)
22156d07bcecSMiao Xie {
22166d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
22176d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
22186d07bcecSMiao Xie 	struct btrfs_device *device;
22196d07bcecSMiao Xie 	u64 type;
22206d07bcecSMiao Xie 	u64 avail_space;
22216d07bcecSMiao Xie 	u64 min_stripe_size;
2222559ca6eaSNikolay Borisov 	int num_stripes = 1;
22236d07bcecSMiao Xie 	int i = 0, nr_devices;
22244f080f57SDavid Sterba 	const struct btrfs_raid_attr *rattr;
22256d07bcecSMiao Xie 
22267e33fd99SJosef Bacik 	/*
222701327610SNicholas D Steeves 	 * We aren't under the device list lock, so this is racy-ish, but good
22287e33fd99SJosef Bacik 	 * enough for our purposes.
22297e33fd99SJosef Bacik 	 */
2230b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
22317e33fd99SJosef Bacik 	if (!nr_devices) {
22327e33fd99SJosef Bacik 		smp_mb();
22337e33fd99SJosef Bacik 		nr_devices = fs_info->fs_devices->open_devices;
22347e33fd99SJosef Bacik 		ASSERT(nr_devices);
22357e33fd99SJosef Bacik 		if (!nr_devices) {
22367e33fd99SJosef Bacik 			*free_bytes = 0;
22377e33fd99SJosef Bacik 			return 0;
22387e33fd99SJosef Bacik 		}
22397e33fd99SJosef Bacik 	}
22406d07bcecSMiao Xie 
2241d9b0d9baSDulshani Gunawardhana 	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
22426a44517dSDavid Sterba 			       GFP_KERNEL);
22436d07bcecSMiao Xie 	if (!devices_info)
22446d07bcecSMiao Xie 		return -ENOMEM;
22456d07bcecSMiao Xie 
224601327610SNicholas D Steeves 	/* calc min stripe number for data space allocation */
22471b86826dSJeff Mahoney 	type = btrfs_data_alloc_profile(fs_info);
22484f080f57SDavid Sterba 	rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
22494f080f57SDavid Sterba 
2250e1ea2beeSDavid Sterba 	if (type & BTRFS_BLOCK_GROUP_RAID0)
225139fb26c3SMiao Xie 		num_stripes = nr_devices;
2252d09cb9e1SDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1_MASK)
2253d09cb9e1SDavid Sterba 		num_stripes = rattr->ncopies;
2254e1ea2beeSDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID10)
225539fb26c3SMiao Xie 		num_stripes = 4;
22566d07bcecSMiao Xie 
22574f080f57SDavid Sterba 	/* Adjust for more than 1 stripe per device */
22584f080f57SDavid Sterba 	min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
22596d07bcecSMiao Xie 
22607e33fd99SJosef Bacik 	rcu_read_lock();
22617e33fd99SJosef Bacik 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
2262e12c9621SAnand Jain 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2263e12c9621SAnand Jain 						&device->dev_state) ||
2264401e29c1SAnand Jain 		    !device->bdev ||
2265401e29c1SAnand Jain 		    test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
22666d07bcecSMiao Xie 			continue;
22676d07bcecSMiao Xie 
22687e33fd99SJosef Bacik 		if (i >= nr_devices)
22697e33fd99SJosef Bacik 			break;
22707e33fd99SJosef Bacik 
22716d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
22726d07bcecSMiao Xie 
22736d07bcecSMiao Xie 		/* align with stripe_len */
2274559ca6eaSNikolay Borisov 		avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
22756d07bcecSMiao Xie 
22766d07bcecSMiao Xie 		/*
227737f85ec3SQu Wenruo 		 * Ensure we have at least min_stripe_size on top of the
227837f85ec3SQu Wenruo 		 * reserved space on the device.
22796d07bcecSMiao Xie 		 */
228037f85ec3SQu Wenruo 		if (avail_space <= BTRFS_DEVICE_RANGE_RESERVED + min_stripe_size)
22816d07bcecSMiao Xie 			continue;
22826d07bcecSMiao Xie 
228337f85ec3SQu Wenruo 		avail_space -= BTRFS_DEVICE_RANGE_RESERVED;
2284559ca6eaSNikolay Borisov 
22856d07bcecSMiao Xie 		devices_info[i].dev = device;
22866d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
22876d07bcecSMiao Xie 
22886d07bcecSMiao Xie 		i++;
22896d07bcecSMiao Xie 	}
22907e33fd99SJosef Bacik 	rcu_read_unlock();
22916d07bcecSMiao Xie 
22926d07bcecSMiao Xie 	nr_devices = i;
22936d07bcecSMiao Xie 
22946d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
22956d07bcecSMiao Xie 
22966d07bcecSMiao Xie 	i = nr_devices - 1;
22976d07bcecSMiao Xie 	avail_space = 0;
2298559ca6eaSNikolay Borisov 	while (nr_devices >= rattr->devs_min) {
2299559ca6eaSNikolay Borisov 		num_stripes = min(num_stripes, nr_devices);
230039fb26c3SMiao Xie 
23016d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
23026d07bcecSMiao Xie 			int j;
23036d07bcecSMiao Xie 			u64 alloc_size;
23046d07bcecSMiao Xie 
230539fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
23066d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
230739fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
23086d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
23096d07bcecSMiao Xie 		}
23106d07bcecSMiao Xie 		i--;
23116d07bcecSMiao Xie 		nr_devices--;
23126d07bcecSMiao Xie 	}
23136d07bcecSMiao Xie 
23146d07bcecSMiao Xie 	kfree(devices_info);
23156d07bcecSMiao Xie 	*free_bytes = avail_space;
23166d07bcecSMiao Xie 	return 0;
23176d07bcecSMiao Xie }
23186d07bcecSMiao Xie 
2319ba7b6e62SDavid Sterba /*
2320ba7b6e62SDavid Sterba  * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2321ba7b6e62SDavid Sterba  *
2322ba7b6e62SDavid Sterba  * If there's a redundant raid level at DATA block groups, use the respective
2323ba7b6e62SDavid Sterba  * multiplier to scale the sizes.
2324ba7b6e62SDavid Sterba  *
2325ba7b6e62SDavid Sterba  * Unused device space usage is based on simulating the chunk allocator
23260d0c71b3SDavid Sterba  * algorithm that respects the device sizes and order of allocations.  This is
23270d0c71b3SDavid Sterba  * a close approximation of the actual use but there are other factors that may
23280d0c71b3SDavid Sterba  * change the result (like a new metadata chunk).
2329ba7b6e62SDavid Sterba  *
2330ca8a51b3SDavid Sterba  * If metadata is exhausted, f_bavail will be 0.
2331ba7b6e62SDavid Sterba  */
23328fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
23338fd17795SChris Mason {
2334815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
2335815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
2336bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
2337bd4d1088SJosef Bacik 	u64 total_used = 0;
23386d07bcecSMiao Xie 	u64 total_free_data = 0;
2339ca8a51b3SDavid Sterba 	u64 total_free_meta = 0;
2340265fdfa6SDavid Sterba 	u32 bits = fs_info->sectorsize_bits;
2341de37aa51SNikolay Borisov 	__be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
2342ba7b6e62SDavid Sterba 	unsigned factor = 1;
2343ba7b6e62SDavid Sterba 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
23446d07bcecSMiao Xie 	int ret;
2345ca8a51b3SDavid Sterba 	u64 thresh = 0;
2346ae02d1bdSLuis de Bethencourt 	int mixed = 0;
23478fd17795SChris Mason 
234872804905SJosef Bacik 	list_for_each_entry(found, &fs_info->space_info, list) {
23496d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
2350ba7b6e62SDavid Sterba 			int i;
2351ba7b6e62SDavid Sterba 
23526d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
23536d07bcecSMiao Xie 			total_free_data -=
23546d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
2355ba7b6e62SDavid Sterba 
2356ba7b6e62SDavid Sterba 			for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
235746df06b8SDavid Sterba 				if (!list_empty(&found->block_groups[i]))
235846df06b8SDavid Sterba 					factor = btrfs_bg_type_to_factor(
235946df06b8SDavid Sterba 						btrfs_raid_array[i].bg_flag);
2360ba7b6e62SDavid Sterba 			}
23616d07bcecSMiao Xie 		}
2362ae02d1bdSLuis de Bethencourt 
2363ae02d1bdSLuis de Bethencourt 		/*
2364ae02d1bdSLuis de Bethencourt 		 * Metadata in mixed block goup profiles are accounted in data
2365ae02d1bdSLuis de Bethencourt 		 */
2366ae02d1bdSLuis de Bethencourt 		if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
2367ae02d1bdSLuis de Bethencourt 			if (found->flags & BTRFS_BLOCK_GROUP_DATA)
2368ae02d1bdSLuis de Bethencourt 				mixed = 1;
2369ae02d1bdSLuis de Bethencourt 			else
2370ae02d1bdSLuis de Bethencourt 				total_free_meta += found->disk_total -
2371ae02d1bdSLuis de Bethencourt 					found->disk_used;
2372ae02d1bdSLuis de Bethencourt 		}
23736d07bcecSMiao Xie 
2374b742bb82SYan, Zheng 		total_used += found->disk_used;
237589a55897SJosef Bacik 	}
2376ba7b6e62SDavid Sterba 
2377ba7b6e62SDavid Sterba 	buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
2378ba7b6e62SDavid Sterba 	buf->f_blocks >>= bits;
2379ba7b6e62SDavid Sterba 	buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
2380ba7b6e62SDavid Sterba 
2381ba7b6e62SDavid Sterba 	/* Account global block reserve as used, it's in logical size already */
2382ba7b6e62SDavid Sterba 	spin_lock(&block_rsv->lock);
238341b34accSLuis de Bethencourt 	/* Mixed block groups accounting is not byte-accurate, avoid overflow */
238441b34accSLuis de Bethencourt 	if (buf->f_bfree >= block_rsv->size >> bits)
2385ba7b6e62SDavid Sterba 		buf->f_bfree -= block_rsv->size >> bits;
238641b34accSLuis de Bethencourt 	else
238741b34accSLuis de Bethencourt 		buf->f_bfree = 0;
2388ba7b6e62SDavid Sterba 	spin_unlock(&block_rsv->lock);
2389ba7b6e62SDavid Sterba 
23900d95c1beSDavid Sterba 	buf->f_bavail = div_u64(total_free_data, factor);
23916bccf3abSJeff Mahoney 	ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
23927e33fd99SJosef Bacik 	if (ret)
23936d07bcecSMiao Xie 		return ret;
2394ba7b6e62SDavid Sterba 	buf->f_bavail += div_u64(total_free_data, factor);
23956d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
2396d397712bSChris Mason 
2397ca8a51b3SDavid Sterba 	/*
2398ca8a51b3SDavid Sterba 	 * We calculate the remaining metadata space minus global reserve. If
2399ca8a51b3SDavid Sterba 	 * this is (supposedly) smaller than zero, there's no space. But this
2400ca8a51b3SDavid Sterba 	 * does not hold in practice, the exhausted state happens where's still
2401ca8a51b3SDavid Sterba 	 * some positive delta. So we apply some guesswork and compare the
2402ca8a51b3SDavid Sterba 	 * delta to a 4M threshold.  (Practically observed delta was ~2M.)
2403ca8a51b3SDavid Sterba 	 *
2404ca8a51b3SDavid Sterba 	 * We probably cannot calculate the exact threshold value because this
2405ca8a51b3SDavid Sterba 	 * depends on the internal reservations requested by various
2406ca8a51b3SDavid Sterba 	 * operations, so some operations that consume a few metadata will
2407ca8a51b3SDavid Sterba 	 * succeed even if the Avail is zero. But this is better than the other
2408ca8a51b3SDavid Sterba 	 * way around.
2409ca8a51b3SDavid Sterba 	 */
2410d4417e22SNikolay Borisov 	thresh = SZ_4M;
2411ca8a51b3SDavid Sterba 
2412d55966c4SJosef Bacik 	/*
2413d55966c4SJosef Bacik 	 * We only want to claim there's no available space if we can no longer
2414d55966c4SJosef Bacik 	 * allocate chunks for our metadata profile and our global reserve will
2415d55966c4SJosef Bacik 	 * not fit in the free metadata space.  If we aren't ->full then we
2416d55966c4SJosef Bacik 	 * still can allocate chunks and thus are fine using the currently
2417d55966c4SJosef Bacik 	 * calculated f_bavail.
2418d55966c4SJosef Bacik 	 */
2419d55966c4SJosef Bacik 	if (!mixed && block_rsv->space_info->full &&
2420d55966c4SJosef Bacik 	    total_free_meta - thresh < block_rsv->size)
2421ca8a51b3SDavid Sterba 		buf->f_bavail = 0;
2422ca8a51b3SDavid Sterba 
2423ba7b6e62SDavid Sterba 	buf->f_type = BTRFS_SUPER_MAGIC;
2424ba7b6e62SDavid Sterba 	buf->f_bsize = dentry->d_sb->s_blocksize;
2425ba7b6e62SDavid Sterba 	buf->f_namelen = BTRFS_NAME_LEN;
2426ba7b6e62SDavid Sterba 
24279d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
24289d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
24299d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
24309d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
24319d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
243232d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
24334fd786e6SMisono Tomohiro 	buf->f_fsid.val[0] ^=
24344fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32;
24354fd786e6SMisono Tomohiro 	buf->f_fsid.val[1] ^=
24364fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid;
243732d48fa1SDavid Woodhouse 
24388fd17795SChris Mason 	return 0;
24398fd17795SChris Mason }
2440b5133862SChris Mason 
2441aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
2442aea52e19SAl Viro {
2443815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2444aea52e19SAl Viro 	kill_anon_super(sb);
24450d4b0463SJosef Bacik 	btrfs_free_fs_info(fs_info);
2446aea52e19SAl Viro }
2447aea52e19SAl Viro 
24482e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
24492e635a27SChris Mason 	.owner		= THIS_MODULE,
24502e635a27SChris Mason 	.name		= "btrfs",
2451061dbc6bSAl Viro 	.mount		= btrfs_mount,
2452aea52e19SAl Viro 	.kill_sb	= btrfs_kill_super,
2453f667aef6SQu Wenruo 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
24542e635a27SChris Mason };
245572fa39f5SMisono, Tomohiro 
245672fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type = {
245772fa39f5SMisono, Tomohiro 	.owner		= THIS_MODULE,
245872fa39f5SMisono, Tomohiro 	.name		= "btrfs",
245972fa39f5SMisono, Tomohiro 	.mount		= btrfs_mount_root,
246072fa39f5SMisono, Tomohiro 	.kill_sb	= btrfs_kill_super,
24615b9b26f5SChristian Brauner 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP,
246272fa39f5SMisono, Tomohiro };
246372fa39f5SMisono, Tomohiro 
24647f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs");
2465a9218f6bSChris Mason 
2466d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file)
2467d8620958STom Van Braeckel {
2468d8620958STom Van Braeckel 	/*
2469d8620958STom Van Braeckel 	 * The control file's private_data is used to hold the
2470d8620958STom Van Braeckel 	 * transaction when it is started and is used to keep
2471d8620958STom Van Braeckel 	 * track of whether a transaction is already in progress.
2472d8620958STom Van Braeckel 	 */
2473d8620958STom Van Braeckel 	file->private_data = NULL;
2474d8620958STom Van Braeckel 	return 0;
2475d8620958STom Van Braeckel }
2476d8620958STom Van Braeckel 
2477d352ac68SChris Mason /*
2478cfe953c8SSu Yue  * Used by /dev/btrfs-control for devices ioctls.
2479d352ac68SChris Mason  */
24808a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
24818a4b83ccSChris Mason 				unsigned long arg)
24828a4b83ccSChris Mason {
24838a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
248436350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
248516cab91aSAnand Jain 	dev_t devt = 0;
2486c071fcfdSChris Mason 	int ret = -ENOTTY;
24878a4b83ccSChris Mason 
2488e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2489e441d54dSChris Mason 		return -EPERM;
2490e441d54dSChris Mason 
2491dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
2492dae7b665SLi Zefan 	if (IS_ERR(vol))
2493dae7b665SLi Zefan 		return PTR_ERR(vol);
2494f505754fSFilipe Manana 	vol->name[BTRFS_PATH_NAME_MAX] = '\0';
2495c071fcfdSChris Mason 
24968a4b83ccSChris Mason 	switch (cmd) {
24978a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
2498899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
249936350e95SGu Jinxiang 		device = btrfs_scan_one_device(vol->name, FMODE_READ,
250036350e95SGu Jinxiang 					       &btrfs_root_fs_type);
250136350e95SGu Jinxiang 		ret = PTR_ERR_OR_ZERO(device);
2502899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
25038a4b83ccSChris Mason 		break;
2504228a73abSAnand Jain 	case BTRFS_IOC_FORGET_DEV:
250516cab91aSAnand Jain 		if (vol->name[0] != 0) {
250616cab91aSAnand Jain 			ret = lookup_bdev(vol->name, &devt);
250716cab91aSAnand Jain 			if (ret)
250816cab91aSAnand Jain 				break;
250916cab91aSAnand Jain 		}
251016cab91aSAnand Jain 		ret = btrfs_forget_devices(devt);
2511228a73abSAnand Jain 		break;
251202db0844SJosef Bacik 	case BTRFS_IOC_DEVICES_READY:
2513899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
251436350e95SGu Jinxiang 		device = btrfs_scan_one_device(vol->name, FMODE_READ,
251536350e95SGu Jinxiang 					       &btrfs_root_fs_type);
251636350e95SGu Jinxiang 		if (IS_ERR(device)) {
2517899f9307SDavid Sterba 			mutex_unlock(&uuid_mutex);
251836350e95SGu Jinxiang 			ret = PTR_ERR(device);
251902db0844SJosef Bacik 			break;
2520899f9307SDavid Sterba 		}
252136350e95SGu Jinxiang 		ret = !(device->fs_devices->num_devices ==
252236350e95SGu Jinxiang 			device->fs_devices->total_devices);
2523899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
252402db0844SJosef Bacik 		break;
2525c5868f83SDavid Sterba 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2526d5131b65SDavid Sterba 		ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2527c5868f83SDavid Sterba 		break;
25288a4b83ccSChris Mason 	}
2529dae7b665SLi Zefan 
25308a4b83ccSChris Mason 	kfree(vol);
2531f819d837SLinda Knippers 	return ret;
25328a4b83ccSChris Mason }
25338a4b83ccSChris Mason 
25340176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
2535ed0dab6bSYan {
2536354aa0fbSMiao Xie 	struct btrfs_trans_handle *trans;
25370b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
25380b246afaSJeff Mahoney 	struct btrfs_root *root = fs_info->tree_root;
2539354aa0fbSMiao Xie 
2540fac03c8dSDavid Sterba 	set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
25419e7cc91aSWang Xiaoguang 	/*
25429e7cc91aSWang Xiaoguang 	 * We don't need a barrier here, we'll wait for any transaction that
25439e7cc91aSWang Xiaoguang 	 * could be in progress on other threads (and do delayed iputs that
25449e7cc91aSWang Xiaoguang 	 * we want to avoid on a frozen filesystem), or do the commit
25459e7cc91aSWang Xiaoguang 	 * ourselves.
25469e7cc91aSWang Xiaoguang 	 */
2547d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
2548354aa0fbSMiao Xie 	if (IS_ERR(trans)) {
2549354aa0fbSMiao Xie 		/* no transaction, don't bother */
2550354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
25510176260fSLinus Torvalds 			return 0;
2552354aa0fbSMiao Xie 		return PTR_ERR(trans);
2553354aa0fbSMiao Xie 	}
25543a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
2555ed0dab6bSYan }
2556ed0dab6bSYan 
2557a05d3c91SQu Wenruo static int check_dev_super(struct btrfs_device *dev)
2558a05d3c91SQu Wenruo {
2559a05d3c91SQu Wenruo 	struct btrfs_fs_info *fs_info = dev->fs_info;
2560a05d3c91SQu Wenruo 	struct btrfs_super_block *sb;
25613d17adeaSQu Wenruo 	u16 csum_type;
2562a05d3c91SQu Wenruo 	int ret = 0;
2563a05d3c91SQu Wenruo 
2564a05d3c91SQu Wenruo 	/* This should be called with fs still frozen. */
2565a05d3c91SQu Wenruo 	ASSERT(test_bit(BTRFS_FS_FROZEN, &fs_info->flags));
2566a05d3c91SQu Wenruo 
2567a05d3c91SQu Wenruo 	/* Missing dev, no need to check. */
2568a05d3c91SQu Wenruo 	if (!dev->bdev)
2569a05d3c91SQu Wenruo 		return 0;
2570a05d3c91SQu Wenruo 
2571a05d3c91SQu Wenruo 	/* Only need to check the primary super block. */
2572a05d3c91SQu Wenruo 	sb = btrfs_read_dev_one_super(dev->bdev, 0, true);
2573a05d3c91SQu Wenruo 	if (IS_ERR(sb))
2574a05d3c91SQu Wenruo 		return PTR_ERR(sb);
2575a05d3c91SQu Wenruo 
25763d17adeaSQu Wenruo 	/* Verify the checksum. */
25773d17adeaSQu Wenruo 	csum_type = btrfs_super_csum_type(sb);
25783d17adeaSQu Wenruo 	if (csum_type != btrfs_super_csum_type(fs_info->super_copy)) {
25793d17adeaSQu Wenruo 		btrfs_err(fs_info, "csum type changed, has %u expect %u",
25803d17adeaSQu Wenruo 			  csum_type, btrfs_super_csum_type(fs_info->super_copy));
25813d17adeaSQu Wenruo 		ret = -EUCLEAN;
25823d17adeaSQu Wenruo 		goto out;
25833d17adeaSQu Wenruo 	}
25843d17adeaSQu Wenruo 
25853d17adeaSQu Wenruo 	if (btrfs_check_super_csum(fs_info, sb)) {
25863d17adeaSQu Wenruo 		btrfs_err(fs_info, "csum for on-disk super block no longer matches");
25873d17adeaSQu Wenruo 		ret = -EUCLEAN;
25883d17adeaSQu Wenruo 		goto out;
25893d17adeaSQu Wenruo 	}
25903d17adeaSQu Wenruo 
2591a05d3c91SQu Wenruo 	/* Btrfs_validate_super() includes fsid check against super->fsid. */
2592a05d3c91SQu Wenruo 	ret = btrfs_validate_super(fs_info, sb, 0);
2593a05d3c91SQu Wenruo 	if (ret < 0)
2594a05d3c91SQu Wenruo 		goto out;
2595a05d3c91SQu Wenruo 
2596a05d3c91SQu Wenruo 	if (btrfs_super_generation(sb) != fs_info->last_trans_committed) {
2597a05d3c91SQu Wenruo 		btrfs_err(fs_info, "transid mismatch, has %llu expect %llu",
2598a05d3c91SQu Wenruo 			btrfs_super_generation(sb),
2599a05d3c91SQu Wenruo 			fs_info->last_trans_committed);
2600a05d3c91SQu Wenruo 		ret = -EUCLEAN;
2601a05d3c91SQu Wenruo 		goto out;
2602a05d3c91SQu Wenruo 	}
2603a05d3c91SQu Wenruo out:
2604a05d3c91SQu Wenruo 	btrfs_release_disk_super(sb);
2605a05d3c91SQu Wenruo 	return ret;
2606a05d3c91SQu Wenruo }
2607a05d3c91SQu Wenruo 
26089e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb)
26099e7cc91aSWang Xiaoguang {
2610fac03c8dSDavid Sterba 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2611a05d3c91SQu Wenruo 	struct btrfs_device *device;
2612a05d3c91SQu Wenruo 	int ret = 0;
2613fac03c8dSDavid Sterba 
2614a05d3c91SQu Wenruo 	/*
2615a05d3c91SQu Wenruo 	 * Make sure the fs is not changed by accident (like hibernation then
2616a05d3c91SQu Wenruo 	 * modified by other OS).
2617a05d3c91SQu Wenruo 	 * If we found anything wrong, we mark the fs error immediately.
2618a05d3c91SQu Wenruo 	 *
2619a05d3c91SQu Wenruo 	 * And since the fs is frozen, no one can modify the fs yet, thus
2620a05d3c91SQu Wenruo 	 * we don't need to hold device_list_mutex.
2621a05d3c91SQu Wenruo 	 */
2622a05d3c91SQu Wenruo 	list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
2623a05d3c91SQu Wenruo 		ret = check_dev_super(device);
2624a05d3c91SQu Wenruo 		if (ret < 0) {
2625a05d3c91SQu Wenruo 			btrfs_handle_fs_error(fs_info, ret,
2626a05d3c91SQu Wenruo 				"super block on devid %llu got modified unexpectedly",
2627a05d3c91SQu Wenruo 				device->devid);
2628a05d3c91SQu Wenruo 			break;
2629a05d3c91SQu Wenruo 		}
2630a05d3c91SQu Wenruo 	}
2631fac03c8dSDavid Sterba 	clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
2632a05d3c91SQu Wenruo 
2633a05d3c91SQu Wenruo 	/*
2634a05d3c91SQu Wenruo 	 * We still return 0, to allow VFS layer to unfreeze the fs even the
2635a05d3c91SQu Wenruo 	 * above checks failed. Since the fs is either fine or read-only, we're
2636a05d3c91SQu Wenruo 	 * safe to continue, without causing further damage.
2637a05d3c91SQu Wenruo 	 */
26389e7cc91aSWang Xiaoguang 	return 0;
26399e7cc91aSWang Xiaoguang }
26409e7cc91aSWang Xiaoguang 
26419c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
26429c5085c1SJosef Bacik {
26439c5085c1SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
26449c5085c1SJosef Bacik 
264588c14590SDavid Sterba 	/*
26466605fd2fSAnand Jain 	 * There should be always a valid pointer in latest_dev, it may be stale
26476605fd2fSAnand Jain 	 * for a short moment in case it's being deleted but still valid until
26486605fd2fSAnand Jain 	 * the end of RCU grace period.
264988c14590SDavid Sterba 	 */
265088c14590SDavid Sterba 	rcu_read_lock();
26516605fd2fSAnand Jain 	seq_escape(m, rcu_str_deref(fs_info->fs_devices->latest_dev->name), " \t\n\\");
265288c14590SDavid Sterba 	rcu_read_unlock();
26536605fd2fSAnand Jain 
26549c5085c1SJosef Bacik 	return 0;
26559c5085c1SJosef Bacik }
26569c5085c1SJosef Bacik 
2657b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
265876dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
2659bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
2660e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
2661d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
2662a9572a15SEric Paris 	.show_options	= btrfs_show_options,
26639c5085c1SJosef Bacik 	.show_devname	= btrfs_show_devname,
26642c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
26652c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
266626602cabSAl Viro 	.free_inode	= btrfs_free_inode,
26678fd17795SChris Mason 	.statfs		= btrfs_statfs,
2668c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
26690176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
26709e7cc91aSWang Xiaoguang 	.unfreeze_fs	= btrfs_unfreeze,
2671e20d96d6SChris Mason };
2672a9218f6bSChris Mason 
2673a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
2674d8620958STom Van Braeckel 	.open = btrfs_control_open,
2675a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
26761832f2d8SArnd Bergmann 	.compat_ioctl = compat_ptr_ioctl,
2677a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
26786038f373SArnd Bergmann 	.llseek = noop_llseek,
2679a9218f6bSChris Mason };
2680a9218f6bSChris Mason 
2681a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
2682578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
2683a9218f6bSChris Mason 	.name		= "btrfs-control",
2684a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
2685a9218f6bSChris Mason };
2686a9218f6bSChris Mason 
2687578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2688578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
2689578454ffSKay Sievers 
2690f5c29bd9SLiu Bo static int __init btrfs_interface_init(void)
2691a9218f6bSChris Mason {
2692a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
2693a9218f6bSChris Mason }
2694a9218f6bSChris Mason 
2695e67c718bSDavid Sterba static __cold void btrfs_interface_exit(void)
2696a9218f6bSChris Mason {
2697f368ed60SGreg Kroah-Hartman 	misc_deregister(&btrfs_misc);
2698a9218f6bSChris Mason }
2699a9218f6bSChris Mason 
27005565b8e0SQu Wenruo static int __init btrfs_print_mod_info(void)
270185965600SDavid Sterba {
2702edf57cbfSBart Van Assche 	static const char options[] = ""
270385965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
270485965600SDavid Sterba 			", debug=on"
270585965600SDavid Sterba #endif
270679556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT
270779556c3dSStefan Behrens 			", assert=on"
270879556c3dSStefan Behrens #endif
270985965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
271085965600SDavid Sterba 			", integrity-checker=on"
271185965600SDavid Sterba #endif
2712fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2713fb592373SJosef Bacik 			", ref-verify=on"
2714fb592373SJosef Bacik #endif
27155b316468SNaohiro Aota #ifdef CONFIG_BLK_DEV_ZONED
27165b316468SNaohiro Aota 			", zoned=yes"
27175b316468SNaohiro Aota #else
27185b316468SNaohiro Aota 			", zoned=no"
27195b316468SNaohiro Aota #endif
2720ea3dc7d2SDavid Sterba #ifdef CONFIG_FS_VERITY
2721ea3dc7d2SDavid Sterba 			", fsverity=yes"
2722ea3dc7d2SDavid Sterba #else
2723ea3dc7d2SDavid Sterba 			", fsverity=no"
2724ea3dc7d2SDavid Sterba #endif
2725edf57cbfSBart Van Assche 			;
2726edf57cbfSBart Van Assche 	pr_info("Btrfs loaded, crc32c=%s%s\n", crc32c_impl(), options);
27275565b8e0SQu Wenruo 	return 0;
27285565b8e0SQu Wenruo }
27295565b8e0SQu Wenruo 
27305565b8e0SQu Wenruo static int register_btrfs(void)
27315565b8e0SQu Wenruo {
27325565b8e0SQu Wenruo 	return register_filesystem(&btrfs_fs_type);
27335565b8e0SQu Wenruo }
27345565b8e0SQu Wenruo 
27355565b8e0SQu Wenruo static void unregister_btrfs(void)
27365565b8e0SQu Wenruo {
27375565b8e0SQu Wenruo 	unregister_filesystem(&btrfs_fs_type);
27385565b8e0SQu Wenruo }
27395565b8e0SQu Wenruo 
27405565b8e0SQu Wenruo /* Helper structure for long init/exit functions. */
27415565b8e0SQu Wenruo struct init_sequence {
27425565b8e0SQu Wenruo 	int (*init_func)(void);
27435565b8e0SQu Wenruo 	/* Can be NULL if the init_func doesn't need cleanup. */
27445565b8e0SQu Wenruo 	void (*exit_func)(void);
27455565b8e0SQu Wenruo };
27465565b8e0SQu Wenruo 
27475565b8e0SQu Wenruo static const struct init_sequence mod_init_seq[] = {
27485565b8e0SQu Wenruo 	{
27495565b8e0SQu Wenruo 		.init_func = btrfs_props_init,
27505565b8e0SQu Wenruo 		.exit_func = NULL,
27515565b8e0SQu Wenruo 	}, {
27525565b8e0SQu Wenruo 		.init_func = btrfs_init_sysfs,
27535565b8e0SQu Wenruo 		.exit_func = btrfs_exit_sysfs,
27545565b8e0SQu Wenruo 	}, {
27555565b8e0SQu Wenruo 		.init_func = btrfs_init_compress,
27565565b8e0SQu Wenruo 		.exit_func = btrfs_exit_compress,
27575565b8e0SQu Wenruo 	}, {
27585565b8e0SQu Wenruo 		.init_func = btrfs_init_cachep,
27595565b8e0SQu Wenruo 		.exit_func = btrfs_destroy_cachep,
27605565b8e0SQu Wenruo 	}, {
27615565b8e0SQu Wenruo 		.init_func = btrfs_transaction_init,
27625565b8e0SQu Wenruo 		.exit_func = btrfs_transaction_exit,
27635565b8e0SQu Wenruo 	}, {
27645565b8e0SQu Wenruo 		.init_func = btrfs_ctree_init,
27655565b8e0SQu Wenruo 		.exit_func = btrfs_ctree_exit,
27665565b8e0SQu Wenruo 	}, {
27675565b8e0SQu Wenruo 		.init_func = btrfs_free_space_init,
27685565b8e0SQu Wenruo 		.exit_func = btrfs_free_space_exit,
27695565b8e0SQu Wenruo 	}, {
27705565b8e0SQu Wenruo 		.init_func = extent_state_init_cachep,
27715565b8e0SQu Wenruo 		.exit_func = extent_state_free_cachep,
27725565b8e0SQu Wenruo 	}, {
27735565b8e0SQu Wenruo 		.init_func = extent_buffer_init_cachep,
27745565b8e0SQu Wenruo 		.exit_func = extent_buffer_free_cachep,
27755565b8e0SQu Wenruo 	}, {
27765565b8e0SQu Wenruo 		.init_func = btrfs_bioset_init,
27775565b8e0SQu Wenruo 		.exit_func = btrfs_bioset_exit,
27785565b8e0SQu Wenruo 	}, {
27795565b8e0SQu Wenruo 		.init_func = extent_map_init,
27805565b8e0SQu Wenruo 		.exit_func = extent_map_exit,
27815565b8e0SQu Wenruo 	}, {
27825565b8e0SQu Wenruo 		.init_func = ordered_data_init,
27835565b8e0SQu Wenruo 		.exit_func = ordered_data_exit,
27845565b8e0SQu Wenruo 	}, {
27855565b8e0SQu Wenruo 		.init_func = btrfs_delayed_inode_init,
27865565b8e0SQu Wenruo 		.exit_func = btrfs_delayed_inode_exit,
27875565b8e0SQu Wenruo 	}, {
27885565b8e0SQu Wenruo 		.init_func = btrfs_auto_defrag_init,
27895565b8e0SQu Wenruo 		.exit_func = btrfs_auto_defrag_exit,
27905565b8e0SQu Wenruo 	}, {
27915565b8e0SQu Wenruo 		.init_func = btrfs_delayed_ref_init,
27925565b8e0SQu Wenruo 		.exit_func = btrfs_delayed_ref_exit,
27935565b8e0SQu Wenruo 	}, {
27945565b8e0SQu Wenruo 		.init_func = btrfs_prelim_ref_init,
27955565b8e0SQu Wenruo 		.exit_func = btrfs_prelim_ref_exit,
27965565b8e0SQu Wenruo 	}, {
27975565b8e0SQu Wenruo 		.init_func = btrfs_interface_init,
27985565b8e0SQu Wenruo 		.exit_func = btrfs_interface_exit,
27995565b8e0SQu Wenruo 	}, {
28005565b8e0SQu Wenruo 		.init_func = btrfs_print_mod_info,
28015565b8e0SQu Wenruo 		.exit_func = NULL,
28025565b8e0SQu Wenruo 	}, {
28035565b8e0SQu Wenruo 		.init_func = btrfs_run_sanity_tests,
28045565b8e0SQu Wenruo 		.exit_func = NULL,
28055565b8e0SQu Wenruo 	}, {
28065565b8e0SQu Wenruo 		.init_func = register_btrfs,
28075565b8e0SQu Wenruo 		.exit_func = unregister_btrfs,
28085565b8e0SQu Wenruo 	}
28095565b8e0SQu Wenruo };
28105565b8e0SQu Wenruo 
28115565b8e0SQu Wenruo static bool mod_init_result[ARRAY_SIZE(mod_init_seq)];
28125565b8e0SQu Wenruo 
28135565b8e0SQu Wenruo static void __exit exit_btrfs_fs(void)
28145565b8e0SQu Wenruo {
28155565b8e0SQu Wenruo 	int i;
28165565b8e0SQu Wenruo 
28175565b8e0SQu Wenruo 	for (i = ARRAY_SIZE(mod_init_seq) - 1; i >= 0; i--) {
28185565b8e0SQu Wenruo 		if (!mod_init_result[i])
28195565b8e0SQu Wenruo 			continue;
28205565b8e0SQu Wenruo 		if (mod_init_seq[i].exit_func)
28215565b8e0SQu Wenruo 			mod_init_seq[i].exit_func();
28225565b8e0SQu Wenruo 		mod_init_result[i] = false;
28235565b8e0SQu Wenruo 	}
282485965600SDavid Sterba }
282585965600SDavid Sterba 
28262e635a27SChris Mason static int __init init_btrfs_fs(void)
28272e635a27SChris Mason {
28285565b8e0SQu Wenruo 	int ret;
28295565b8e0SQu Wenruo 	int i;
283058176a96SJosef Bacik 
28315565b8e0SQu Wenruo 	for (i = 0; i < ARRAY_SIZE(mod_init_seq); i++) {
28325565b8e0SQu Wenruo 		ASSERT(!mod_init_result[i]);
28335565b8e0SQu Wenruo 		ret = mod_init_seq[i].init_func();
28345565b8e0SQu Wenruo 		if (ret < 0)
28355565b8e0SQu Wenruo 			goto error;
28365565b8e0SQu Wenruo 		mod_init_result[i] = true;
28375565b8e0SQu Wenruo 	}
28382f4cbe64SWyatt Banks 	return 0;
28392f4cbe64SWyatt Banks 
28405565b8e0SQu Wenruo error:
28415565b8e0SQu Wenruo 	/*
28425565b8e0SQu Wenruo 	 * If we call exit_btrfs_fs() it would cause section mismatch.
28435565b8e0SQu Wenruo 	 * As init_btrfs_fs() belongs to .init.text, while exit_btrfs_fs()
28445565b8e0SQu Wenruo 	 * belongs to .exit.text.
28455565b8e0SQu Wenruo 	 */
28465565b8e0SQu Wenruo 	for (i = ARRAY_SIZE(mod_init_seq) - 1; i >= 0; i--) {
28475565b8e0SQu Wenruo 		if (!mod_init_result[i])
28485565b8e0SQu Wenruo 			continue;
28495565b8e0SQu Wenruo 		if (mod_init_seq[i].exit_func)
28505565b8e0SQu Wenruo 			mod_init_seq[i].exit_func();
28515565b8e0SQu Wenruo 		mod_init_result[i] = false;
28522e635a27SChris Mason 	}
28535565b8e0SQu Wenruo 	return ret;
28542e635a27SChris Mason }
28552e635a27SChris Mason 
285660efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs);
28572e635a27SChris Mason module_exit(exit_btrfs_fs)
28582e635a27SChris Mason 
28592e635a27SChris Mason MODULE_LICENSE("GPL");
2860d5178578SJohannes Thumshirn MODULE_SOFTDEP("pre: crc32c");
28613951e7f0SJohannes Thumshirn MODULE_SOFTDEP("pre: xxhash64");
28623831bf00SJohannes Thumshirn MODULE_SOFTDEP("pre: sha256");
2863352ae07bSDavid Sterba MODULE_SOFTDEP("pre: blake2b-256");
2864