xref: /linux/fs/btrfs/super.c (revision 5565b8e0adcdd8ee32d02d82d98cdf6d966793c7)
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"
521abe9b8aSliubo #define CREATE_TRACE_POINTS
531abe9b8aSliubo #include <trace/events/btrfs.h>
541abe9b8aSliubo 
55b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
5672fa39f5SMisono, Tomohiro 
5772fa39f5SMisono, Tomohiro /*
5872fa39f5SMisono, Tomohiro  * Types for mounting the default subvolume and a subvolume explicitly
5972fa39f5SMisono, Tomohiro  * requested by subvol=/path. That way the callchain is straightforward and we
6072fa39f5SMisono, Tomohiro  * don't have to play tricks with the mount options and recursive calls to
6172fa39f5SMisono, Tomohiro  * btrfs_mount.
62312c89fbSMisono, Tomohiro  *
63312c89fbSMisono, Tomohiro  * The new btrfs_root_fs_type also servers as a tag for the bdev_holder.
6472fa39f5SMisono, Tomohiro  */
65830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type;
6672fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type;
67e20d96d6SChris Mason 
680723a047SHarald Hoyer static int btrfs_remount(struct super_block *sb, int *flags, char *data);
690723a047SHarald Hoyer 
70c067da87SSweet Tea Dorminy #ifdef CONFIG_PRINTK
71c067da87SSweet Tea Dorminy 
72c067da87SSweet Tea Dorminy #define STATE_STRING_PREFACE	": state "
73c067da87SSweet Tea Dorminy #define STATE_STRING_BUF_LEN	(sizeof(STATE_STRING_PREFACE) + BTRFS_FS_STATE_COUNT)
74c067da87SSweet Tea Dorminy 
75c067da87SSweet Tea Dorminy /*
76143823cfSDavid Sterba  * Characters to print to indicate error conditions or uncommon filesystem state.
77c067da87SSweet Tea Dorminy  * RO is not an error.
78c067da87SSweet Tea Dorminy  */
79c067da87SSweet Tea Dorminy static const char fs_state_chars[] = {
80c067da87SSweet Tea Dorminy 	[BTRFS_FS_STATE_ERROR]			= 'E',
81c067da87SSweet Tea Dorminy 	[BTRFS_FS_STATE_REMOUNTING]		= 'M',
82c067da87SSweet Tea Dorminy 	[BTRFS_FS_STATE_RO]			= 0,
83c067da87SSweet Tea Dorminy 	[BTRFS_FS_STATE_TRANS_ABORTED]		= 'A',
84c067da87SSweet Tea Dorminy 	[BTRFS_FS_STATE_DEV_REPLACING]		= 'R',
85c067da87SSweet Tea Dorminy 	[BTRFS_FS_STATE_DUMMY_FS_INFO]		= 0,
86c067da87SSweet Tea Dorminy 	[BTRFS_FS_STATE_NO_CSUMS]		= 'C',
87c067da87SSweet Tea Dorminy 	[BTRFS_FS_STATE_LOG_CLEANUP_ERROR]	= 'L',
88c067da87SSweet Tea Dorminy };
89c067da87SSweet Tea Dorminy 
90c067da87SSweet Tea Dorminy static void btrfs_state_to_string(const struct btrfs_fs_info *info, char *buf)
91c067da87SSweet Tea Dorminy {
92c067da87SSweet Tea Dorminy 	unsigned int bit;
93c067da87SSweet Tea Dorminy 	bool states_printed = false;
94c067da87SSweet Tea Dorminy 	unsigned long fs_state = READ_ONCE(info->fs_state);
95c067da87SSweet Tea Dorminy 	char *curr = buf;
96c067da87SSweet Tea Dorminy 
97c067da87SSweet Tea Dorminy 	memcpy(curr, STATE_STRING_PREFACE, sizeof(STATE_STRING_PREFACE));
98c067da87SSweet Tea Dorminy 	curr += sizeof(STATE_STRING_PREFACE) - 1;
99c067da87SSweet Tea Dorminy 
100c067da87SSweet Tea Dorminy 	for_each_set_bit(bit, &fs_state, sizeof(fs_state)) {
101c067da87SSweet Tea Dorminy 		WARN_ON_ONCE(bit >= BTRFS_FS_STATE_COUNT);
102c067da87SSweet Tea Dorminy 		if ((bit < BTRFS_FS_STATE_COUNT) && fs_state_chars[bit]) {
103c067da87SSweet Tea Dorminy 			*curr++ = fs_state_chars[bit];
104c067da87SSweet Tea Dorminy 			states_printed = true;
105c067da87SSweet Tea Dorminy 		}
106c067da87SSweet Tea Dorminy 	}
107c067da87SSweet Tea Dorminy 
108c067da87SSweet Tea Dorminy 	/* If no states were printed, reset the buffer */
109c067da87SSweet Tea Dorminy 	if (!states_printed)
110c067da87SSweet Tea Dorminy 		curr = buf;
111c067da87SSweet Tea Dorminy 
112c067da87SSweet Tea Dorminy 	*curr++ = 0;
113c067da87SSweet Tea Dorminy }
114c067da87SSweet Tea Dorminy #endif
115c067da87SSweet Tea Dorminy 
11659131393SJosef Bacik /*
11759131393SJosef Bacik  * Generally the error codes correspond to their respective errors, but there
11859131393SJosef Bacik  * are a few special cases.
11959131393SJosef Bacik  *
12059131393SJosef Bacik  * EUCLEAN: Any sort of corruption that we encounter.  The tree-checker for
12159131393SJosef Bacik  *          instance will return EUCLEAN if any of the blocks are corrupted in
12259131393SJosef Bacik  *          a way that is problematic.  We want to reserve EUCLEAN for these
12359131393SJosef Bacik  *          sort of corruptions.
12459131393SJosef Bacik  *
12559131393SJosef Bacik  * EROFS: If we check BTRFS_FS_STATE_ERROR and fail out with a return error, we
12659131393SJosef Bacik  *        need to use EROFS for this case.  We will have no idea of the
12759131393SJosef Bacik  *        original failure, that will have been reported at the time we tripped
12859131393SJosef Bacik  *        over the error.  Each subsequent error that doesn't have any context
12959131393SJosef Bacik  *        of the original error should use EROFS when handling BTRFS_FS_STATE_ERROR.
13059131393SJosef Bacik  */
1314143cb8bSDavid Sterba const char * __attribute_const__ btrfs_decode_error(int errno)
132acce952bSliubo {
13308748810SDavid Sterba 	char *errstr = "unknown";
134acce952bSliubo 
135acce952bSliubo 	switch (errno) {
136d54f8144SDavid Sterba 	case -ENOENT:		/* -2 */
137d54f8144SDavid Sterba 		errstr = "No such entry";
138d54f8144SDavid Sterba 		break;
139d54f8144SDavid Sterba 	case -EIO:		/* -5 */
140acce952bSliubo 		errstr = "IO failure";
141acce952bSliubo 		break;
142d54f8144SDavid Sterba 	case -ENOMEM:		/* -12*/
143acce952bSliubo 		errstr = "Out of memory";
144acce952bSliubo 		break;
145d54f8144SDavid Sterba 	case -EEXIST:		/* -17 */
1468c342930SJeff Mahoney 		errstr = "Object already exists";
1478c342930SJeff Mahoney 		break;
148d54f8144SDavid Sterba 	case -ENOSPC:		/* -28 */
14994ef7280SDavid Sterba 		errstr = "No space left";
15094ef7280SDavid Sterba 		break;
151d54f8144SDavid Sterba 	case -EROFS:		/* -30 */
152d54f8144SDavid Sterba 		errstr = "Readonly filesystem";
15394ef7280SDavid Sterba 		break;
154fb8521caSDavid Sterba 	case -EOPNOTSUPP:	/* -95 */
155fb8521caSDavid Sterba 		errstr = "Operation not supported";
156fb8521caSDavid Sterba 		break;
157fb8521caSDavid Sterba 	case -EUCLEAN:		/* -117 */
158fb8521caSDavid Sterba 		errstr = "Filesystem corrupted";
159fb8521caSDavid Sterba 		break;
160fb8521caSDavid Sterba 	case -EDQUOT:		/* -122 */
161fb8521caSDavid Sterba 		errstr = "Quota exceeded";
162fb8521caSDavid Sterba 		break;
163acce952bSliubo 	}
164acce952bSliubo 
165acce952bSliubo 	return errstr;
166acce952bSliubo }
167acce952bSliubo 
168acce952bSliubo /*
16934d97007SAnand Jain  * __btrfs_handle_fs_error decodes expected errors from the caller and
17052042d8eSAndrea Gelmini  * invokes the appropriate error response.
171acce952bSliubo  */
172c0d19e2bSDavid Sterba __cold
17334d97007SAnand Jain void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
1744da35113SJeff Mahoney 		       unsigned int line, int errno, const char *fmt, ...)
175acce952bSliubo {
176acce952bSliubo 	struct super_block *sb = fs_info->sb;
17757d816a1SAnand Jain #ifdef CONFIG_PRINTK
178c067da87SSweet Tea Dorminy 	char statestr[STATE_STRING_BUF_LEN];
179acce952bSliubo 	const char *errstr;
18057d816a1SAnand Jain #endif
181acce952bSliubo 
182acce952bSliubo 	/*
183acce952bSliubo 	 * Special case: if the error is EROFS, and we're already
1841751e8a6SLinus Torvalds 	 * under SB_RDONLY, then it is safe here.
185acce952bSliubo 	 */
186bc98a42cSDavid Howells 	if (errno == -EROFS && sb_rdonly(sb))
187acce952bSliubo   		return;
188acce952bSliubo 
18957d816a1SAnand Jain #ifdef CONFIG_PRINTK
19008748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
191c067da87SSweet Tea Dorminy 	btrfs_state_to_string(fs_info, statestr);
1924da35113SJeff Mahoney 	if (fmt) {
19337252a66SEric Sandeen 		struct va_format vaf;
19437252a66SEric Sandeen 		va_list args;
19537252a66SEric Sandeen 
19637252a66SEric Sandeen 		va_start(args, fmt);
19737252a66SEric Sandeen 		vaf.fmt = fmt;
19837252a66SEric Sandeen 		vaf.va = &args;
1994da35113SJeff Mahoney 
200c067da87SSweet Tea Dorminy 		pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s (%pV)\n",
201c067da87SSweet Tea Dorminy 			sb->s_id, statestr, function, line, errno, errstr, &vaf);
20237252a66SEric Sandeen 		va_end(args);
2034da35113SJeff Mahoney 	} else {
204c067da87SSweet Tea Dorminy 		pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s\n",
205c067da87SSweet Tea Dorminy 			sb->s_id, statestr, function, line, errno, errstr);
2064da35113SJeff Mahoney 	}
20757d816a1SAnand Jain #endif
208acce952bSliubo 
2090713d90cSAnand Jain 	/*
2100713d90cSAnand Jain 	 * Today we only save the error info to memory.  Long term we'll
2110713d90cSAnand Jain 	 * also send it down to the disk
2120713d90cSAnand Jain 	 */
2130713d90cSAnand Jain 	set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
2140713d90cSAnand Jain 
2154da35113SJeff Mahoney 	/* Don't go through full error handling during mount */
216922ea899SAnand Jain 	if (!(sb->s_flags & SB_BORN))
217922ea899SAnand Jain 		return;
218922ea899SAnand Jain 
219922ea899SAnand Jain 	if (sb_rdonly(sb))
220922ea899SAnand Jain 		return;
221922ea899SAnand Jain 
222b0643e59SDennis Zhou 	btrfs_discard_stop(fs_info);
223b0643e59SDennis Zhou 
224922ea899SAnand Jain 	/* btrfs handle error by forcing the filesystem readonly */
225a0a1db70SFilipe Manana 	btrfs_set_sb_rdonly(sb);
226922ea899SAnand Jain 	btrfs_info(fs_info, "forced readonly");
227922ea899SAnand Jain 	/*
228922ea899SAnand Jain 	 * Note that a running device replace operation is not canceled here
229922ea899SAnand Jain 	 * although there is no way to update the progress. It would add the
230922ea899SAnand Jain 	 * risk of a deadlock, therefore the canceling is omitted. The only
231922ea899SAnand Jain 	 * penalty is that some I/O remains active until the procedure
23252042d8eSAndrea Gelmini 	 * completes. The next time when the filesystem is mounted writable
233922ea899SAnand Jain 	 * again, the device replace operation continues.
234922ea899SAnand Jain 	 */
235acce952bSliubo }
2364da35113SJeff Mahoney 
23757d816a1SAnand Jain #ifdef CONFIG_PRINTK
238533574c6SJoe Perches static const char * const logtypes[] = {
2394da35113SJeff Mahoney 	"emergency",
2404da35113SJeff Mahoney 	"alert",
2414da35113SJeff Mahoney 	"critical",
2424da35113SJeff Mahoney 	"error",
2434da35113SJeff Mahoney 	"warning",
2444da35113SJeff Mahoney 	"notice",
2454da35113SJeff Mahoney 	"info",
2464da35113SJeff Mahoney 	"debug",
2474da35113SJeff Mahoney };
2484da35113SJeff Mahoney 
24935f4e5e6SNikolay Borisov 
25035f4e5e6SNikolay Borisov /*
25135f4e5e6SNikolay Borisov  * Use one ratelimit state per log level so that a flood of less important
25235f4e5e6SNikolay Borisov  * messages doesn't cause more important ones to be dropped.
25335f4e5e6SNikolay Borisov  */
25435f4e5e6SNikolay Borisov static struct ratelimit_state printk_limits[] = {
25535f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100),
25635f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100),
25735f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100),
25835f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100),
25935f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100),
26035f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100),
26135f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100),
26235f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100),
26335f4e5e6SNikolay Borisov };
26435f4e5e6SNikolay Borisov 
265b0a66a31SJonathan Lassoff void __cold _btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
2664da35113SJeff Mahoney {
26740f7828bSPetr Mladek 	char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0";
2684da35113SJeff Mahoney 	struct va_format vaf;
2694da35113SJeff Mahoney 	va_list args;
270533574c6SJoe Perches 	int kern_level;
27140f7828bSPetr Mladek 	const char *type = logtypes[4];
27240f7828bSPetr Mladek 	struct ratelimit_state *ratelimit = &printk_limits[4];
2734da35113SJeff Mahoney 
2744da35113SJeff Mahoney 	va_start(args, fmt);
2754da35113SJeff Mahoney 
276262c5e86SPetr Mladek 	while ((kern_level = printk_get_level(fmt)) != 0) {
277533574c6SJoe Perches 		size_t size = printk_skip_level(fmt) - fmt;
278262c5e86SPetr Mladek 
279262c5e86SPetr Mladek 		if (kern_level >= '0' && kern_level <= '7') {
280533574c6SJoe Perches 			memcpy(lvl, fmt,  size);
281533574c6SJoe Perches 			lvl[size] = '\0';
282533574c6SJoe Perches 			type = logtypes[kern_level - '0'];
28335f4e5e6SNikolay Borisov 			ratelimit = &printk_limits[kern_level - '0'];
284262c5e86SPetr Mladek 		}
285262c5e86SPetr Mladek 		fmt += size;
286262c5e86SPetr Mladek 	}
287262c5e86SPetr Mladek 
2884da35113SJeff Mahoney 	vaf.fmt = fmt;
2894da35113SJeff Mahoney 	vaf.va = &args;
290533574c6SJoe Perches 
291a0f6d924SDavid Sterba 	if (__ratelimit(ratelimit)) {
292c067da87SSweet Tea Dorminy 		if (fs_info) {
293c067da87SSweet Tea Dorminy 			char statestr[STATE_STRING_BUF_LEN];
294c067da87SSweet Tea Dorminy 
295c067da87SSweet Tea Dorminy 			btrfs_state_to_string(fs_info, statestr);
296b0a66a31SJonathan Lassoff 			_printk("%sBTRFS %s (device %s%s): %pV\n", lvl, type,
297c067da87SSweet Tea Dorminy 				fs_info->sb->s_id, statestr, &vaf);
298c067da87SSweet Tea Dorminy 		} else {
299b0a66a31SJonathan Lassoff 			_printk("%sBTRFS %s: %pV\n", lvl, type, &vaf);
300a0f6d924SDavid Sterba 		}
301c067da87SSweet Tea Dorminy 	}
302533574c6SJoe Perches 
303533574c6SJoe Perches 	va_end(args);
3044da35113SJeff Mahoney }
305533574c6SJoe Perches #endif
306533574c6SJoe Perches 
307e9306ad4SQu Wenruo #if BITS_PER_LONG == 32
308e9306ad4SQu Wenruo void __cold btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info)
309e9306ad4SQu Wenruo {
310e9306ad4SQu Wenruo 	if (!test_and_set_bit(BTRFS_FS_32BIT_WARN, &fs_info->flags)) {
311e9306ad4SQu Wenruo 		btrfs_warn(fs_info, "reaching 32bit limit for logical addresses");
312e9306ad4SQu Wenruo 		btrfs_warn(fs_info,
313e9306ad4SQu Wenruo "due to page cache limit on 32bit systems, btrfs can't access metadata at or beyond %lluT",
314e9306ad4SQu Wenruo 			   BTRFS_32BIT_MAX_FILE_SIZE >> 40);
315e9306ad4SQu Wenruo 		btrfs_warn(fs_info,
316e9306ad4SQu Wenruo 			   "please consider upgrading to 64bit kernel/hardware");
317e9306ad4SQu Wenruo 	}
318e9306ad4SQu Wenruo }
319e9306ad4SQu Wenruo 
320e9306ad4SQu Wenruo void __cold btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info)
321e9306ad4SQu Wenruo {
322e9306ad4SQu Wenruo 	if (!test_and_set_bit(BTRFS_FS_32BIT_ERROR, &fs_info->flags)) {
323e9306ad4SQu Wenruo 		btrfs_err(fs_info, "reached 32bit limit for logical addresses");
324e9306ad4SQu Wenruo 		btrfs_err(fs_info,
325e9306ad4SQu Wenruo "due to page cache limit on 32bit systems, metadata beyond %lluT can't be accessed",
326e9306ad4SQu Wenruo 			  BTRFS_32BIT_MAX_FILE_SIZE >> 40);
327e9306ad4SQu Wenruo 		btrfs_err(fs_info,
328e9306ad4SQu Wenruo 			   "please consider upgrading to 64bit kernel/hardware");
329e9306ad4SQu Wenruo 	}
330e9306ad4SQu Wenruo }
331e9306ad4SQu Wenruo #endif
332e9306ad4SQu Wenruo 
3338c342930SJeff Mahoney /*
33449b25e05SJeff Mahoney  * We only mark the transaction aborted and then set the file system read-only.
33549b25e05SJeff Mahoney  * This will prevent new transactions from starting or trying to join this
33649b25e05SJeff Mahoney  * one.
33749b25e05SJeff Mahoney  *
33849b25e05SJeff Mahoney  * This means that error recovery at the call site is limited to freeing
33949b25e05SJeff Mahoney  * any local memory allocations and passing the error code up without
34049b25e05SJeff Mahoney  * further cleanup. The transaction should complete as it normally would
34149b25e05SJeff Mahoney  * in the call path but will return -EIO.
34249b25e05SJeff Mahoney  *
34349b25e05SJeff Mahoney  * We'll complete the cleanup in btrfs_end_transaction and
34449b25e05SJeff Mahoney  * btrfs_commit_transaction.
34549b25e05SJeff Mahoney  */
346c0d19e2bSDavid Sterba __cold
34749b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
34866642832SJeff Mahoney 			       const char *function,
3498e327b9cSQu Wenruo 			       unsigned int line, int errno, bool first_hit)
35049b25e05SJeff Mahoney {
35166642832SJeff Mahoney 	struct btrfs_fs_info *fs_info = trans->fs_info;
35266642832SJeff Mahoney 
353bf31f87fSDavid Sterba 	WRITE_ONCE(trans->aborted, errno);
35420c7bcecSSeraphime Kirkovski 	WRITE_ONCE(trans->transaction->aborted, errno);
3558e327b9cSQu Wenruo 	if (first_hit && errno == -ENOSPC)
3568e327b9cSQu Wenruo 		btrfs_dump_space_info_for_trans_abort(fs_info);
357501407aaSJosef Bacik 	/* Wake up anybody who may be waiting on this transaction */
35866642832SJeff Mahoney 	wake_up(&fs_info->transaction_wait);
35966642832SJeff Mahoney 	wake_up(&fs_info->transaction_blocked_wait);
36066642832SJeff Mahoney 	__btrfs_handle_fs_error(fs_info, function, line, errno, NULL);
36149b25e05SJeff Mahoney }
36249b25e05SJeff Mahoney /*
3638c342930SJeff Mahoney  * __btrfs_panic decodes unexpected, fatal errors from the caller,
3648c342930SJeff Mahoney  * issues an alert, and either panics or BUGs, depending on mount options.
3658c342930SJeff Mahoney  */
366c0d19e2bSDavid Sterba __cold
3678c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
3688c342930SJeff Mahoney 		   unsigned int line, int errno, const char *fmt, ...)
3698c342930SJeff Mahoney {
3708c342930SJeff Mahoney 	char *s_id = "<unknown>";
3718c342930SJeff Mahoney 	const char *errstr;
3728c342930SJeff Mahoney 	struct va_format vaf = { .fmt = fmt };
3738c342930SJeff Mahoney 	va_list args;
3748c342930SJeff Mahoney 
3758c342930SJeff Mahoney 	if (fs_info)
3768c342930SJeff Mahoney 		s_id = fs_info->sb->s_id;
3778c342930SJeff Mahoney 
3788c342930SJeff Mahoney 	va_start(args, fmt);
3798c342930SJeff Mahoney 	vaf.va = &args;
3808c342930SJeff Mahoney 
38108748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
382d8953d69SSatoru Takeuchi 	if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR)))
38308748810SDavid Sterba 		panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
38408748810SDavid Sterba 			s_id, function, line, &vaf, errno, errstr);
3858c342930SJeff Mahoney 
386efe120a0SFrank Holton 	btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
387efe120a0SFrank Holton 		   function, line, &vaf, errno, errstr);
3888c342930SJeff Mahoney 	va_end(args);
3898c342930SJeff Mahoney 	/* Caller calls BUG() */
3908c342930SJeff Mahoney }
391acce952bSliubo 
392e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
393e20d96d6SChris Mason {
3946bccf3abSJeff Mahoney 	close_ctree(btrfs_sb(sb));
395e20d96d6SChris Mason }
3962e635a27SChris Mason 
39795e05289SChris Mason enum {
398416a7202SDavid Sterba 	Opt_acl, Opt_noacl,
399416a7202SDavid Sterba 	Opt_clear_cache,
400416a7202SDavid Sterba 	Opt_commit_interval,
401416a7202SDavid Sterba 	Opt_compress,
402416a7202SDavid Sterba 	Opt_compress_force,
403416a7202SDavid Sterba 	Opt_compress_force_type,
404416a7202SDavid Sterba 	Opt_compress_type,
405416a7202SDavid Sterba 	Opt_degraded,
406416a7202SDavid Sterba 	Opt_device,
407416a7202SDavid Sterba 	Opt_fatal_errors,
408416a7202SDavid Sterba 	Opt_flushoncommit, Opt_noflushoncommit,
409416a7202SDavid Sterba 	Opt_max_inline,
410416a7202SDavid Sterba 	Opt_barrier, Opt_nobarrier,
411416a7202SDavid Sterba 	Opt_datacow, Opt_nodatacow,
412416a7202SDavid Sterba 	Opt_datasum, Opt_nodatasum,
413416a7202SDavid Sterba 	Opt_defrag, Opt_nodefrag,
414416a7202SDavid Sterba 	Opt_discard, Opt_nodiscard,
415b0643e59SDennis Zhou 	Opt_discard_mode,
416416a7202SDavid Sterba 	Opt_norecovery,
417416a7202SDavid Sterba 	Opt_ratio,
418416a7202SDavid Sterba 	Opt_rescan_uuid_tree,
419416a7202SDavid Sterba 	Opt_skip_balance,
420416a7202SDavid Sterba 	Opt_space_cache, Opt_no_space_cache,
421416a7202SDavid Sterba 	Opt_space_cache_version,
422416a7202SDavid Sterba 	Opt_ssd, Opt_nossd,
423416a7202SDavid Sterba 	Opt_ssd_spread, Opt_nossd_spread,
424416a7202SDavid Sterba 	Opt_subvol,
42537becec9SOmar Sandoval 	Opt_subvol_empty,
426416a7202SDavid Sterba 	Opt_subvolid,
427416a7202SDavid Sterba 	Opt_thread_pool,
428416a7202SDavid Sterba 	Opt_treelog, Opt_notreelog,
429416a7202SDavid Sterba 	Opt_user_subvol_rm_allowed,
430416a7202SDavid Sterba 
43174ef0018SQu Wenruo 	/* Rescue options */
43274ef0018SQu Wenruo 	Opt_rescue,
43374ef0018SQu Wenruo 	Opt_usebackuproot,
43474ef0018SQu Wenruo 	Opt_nologreplay,
43542437a63SJosef Bacik 	Opt_ignorebadroots,
436882dbe0cSJosef Bacik 	Opt_ignoredatacsums,
4379037d3cbSJosef Bacik 	Opt_rescue_all,
43874ef0018SQu Wenruo 
439416a7202SDavid Sterba 	/* Deprecated options */
440416a7202SDavid Sterba 	Opt_recovery,
4415297199aSNikolay Borisov 	Opt_inode_cache, Opt_noinode_cache,
442416a7202SDavid Sterba 
443416a7202SDavid Sterba 	/* Debugging options */
444416a7202SDavid Sterba 	Opt_check_integrity,
44570f6d82eSOmar Sandoval 	Opt_check_integrity_including_extent_data,
446416a7202SDavid Sterba 	Opt_check_integrity_print_mask,
447416a7202SDavid Sterba 	Opt_enospc_debug, Opt_noenospc_debug,
448d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
449d0bd4560SJosef Bacik 	Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
450d0bd4560SJosef Bacik #endif
451fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
452fb592373SJosef Bacik 	Opt_ref_verify,
453fb592373SJosef Bacik #endif
4549555c6c1SIlya Dryomov 	Opt_err,
45595e05289SChris Mason };
45695e05289SChris Mason 
4574d4ab6d6SDavid Sterba static const match_table_t tokens = {
458416a7202SDavid Sterba 	{Opt_acl, "acl"},
459416a7202SDavid Sterba 	{Opt_noacl, "noacl"},
460416a7202SDavid Sterba 	{Opt_clear_cache, "clear_cache"},
461416a7202SDavid Sterba 	{Opt_commit_interval, "commit=%u"},
462c8b97818SChris Mason 	{Opt_compress, "compress"},
463261507a0SLi Zefan 	{Opt_compress_type, "compress=%s"},
464a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
465261507a0SLi Zefan 	{Opt_compress_force_type, "compress-force=%s"},
466416a7202SDavid Sterba 	{Opt_degraded, "degraded"},
467416a7202SDavid Sterba 	{Opt_device, "device=%s"},
468416a7202SDavid Sterba 	{Opt_fatal_errors, "fatal_errors=%s"},
469dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
4702c9ee856SQu Wenruo 	{Opt_noflushoncommit, "noflushoncommit"},
4714b9465cbSChris Mason 	{Opt_inode_cache, "inode_cache"},
4723818aea2SQu Wenruo 	{Opt_noinode_cache, "noinode_cache"},
473416a7202SDavid Sterba 	{Opt_max_inline, "max_inline=%s"},
474416a7202SDavid Sterba 	{Opt_barrier, "barrier"},
475416a7202SDavid Sterba 	{Opt_nobarrier, "nobarrier"},
476416a7202SDavid Sterba 	{Opt_datacow, "datacow"},
477416a7202SDavid Sterba 	{Opt_nodatacow, "nodatacow"},
478416a7202SDavid Sterba 	{Opt_datasum, "datasum"},
479416a7202SDavid Sterba 	{Opt_nodatasum, "nodatasum"},
480416a7202SDavid Sterba 	{Opt_defrag, "autodefrag"},
481416a7202SDavid Sterba 	{Opt_nodefrag, "noautodefrag"},
482416a7202SDavid Sterba 	{Opt_discard, "discard"},
483b0643e59SDennis Zhou 	{Opt_discard_mode, "discard=%s"},
484416a7202SDavid Sterba 	{Opt_nodiscard, "nodiscard"},
485416a7202SDavid Sterba 	{Opt_norecovery, "norecovery"},
486416a7202SDavid Sterba 	{Opt_ratio, "metadata_ratio=%u"},
487416a7202SDavid Sterba 	{Opt_rescan_uuid_tree, "rescan_uuid_tree"},
4889555c6c1SIlya Dryomov 	{Opt_skip_balance, "skip_balance"},
489416a7202SDavid Sterba 	{Opt_space_cache, "space_cache"},
490416a7202SDavid Sterba 	{Opt_no_space_cache, "nospace_cache"},
491416a7202SDavid Sterba 	{Opt_space_cache_version, "space_cache=%s"},
492416a7202SDavid Sterba 	{Opt_ssd, "ssd"},
493416a7202SDavid Sterba 	{Opt_nossd, "nossd"},
494416a7202SDavid Sterba 	{Opt_ssd_spread, "ssd_spread"},
495416a7202SDavid Sterba 	{Opt_nossd_spread, "nossd_spread"},
496416a7202SDavid Sterba 	{Opt_subvol, "subvol=%s"},
49737becec9SOmar Sandoval 	{Opt_subvol_empty, "subvol="},
498416a7202SDavid Sterba 	{Opt_subvolid, "subvolid=%s"},
499416a7202SDavid Sterba 	{Opt_thread_pool, "thread_pool=%u"},
500416a7202SDavid Sterba 	{Opt_treelog, "treelog"},
501416a7202SDavid Sterba 	{Opt_notreelog, "notreelog"},
502416a7202SDavid Sterba 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
503416a7202SDavid Sterba 
50474ef0018SQu Wenruo 	/* Rescue options */
50574ef0018SQu Wenruo 	{Opt_rescue, "rescue=%s"},
50674ef0018SQu Wenruo 	/* Deprecated, with alias rescue=nologreplay */
50774ef0018SQu Wenruo 	{Opt_nologreplay, "nologreplay"},
50874ef0018SQu Wenruo 	/* Deprecated, with alias rescue=usebackuproot */
50974ef0018SQu Wenruo 	{Opt_usebackuproot, "usebackuproot"},
51074ef0018SQu Wenruo 
511416a7202SDavid Sterba 	/* Deprecated options */
512416a7202SDavid Sterba 	{Opt_recovery, "recovery"},
513416a7202SDavid Sterba 
514416a7202SDavid Sterba 	/* Debugging options */
51521adbd5cSStefan Behrens 	{Opt_check_integrity, "check_int"},
51621adbd5cSStefan Behrens 	{Opt_check_integrity_including_extent_data, "check_int_data"},
51702453bdeSAnand Jain 	{Opt_check_integrity_print_mask, "check_int_print_mask=%u"},
518416a7202SDavid Sterba 	{Opt_enospc_debug, "enospc_debug"},
519416a7202SDavid Sterba 	{Opt_noenospc_debug, "noenospc_debug"},
520d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
521d0bd4560SJosef Bacik 	{Opt_fragment_data, "fragment=data"},
522d0bd4560SJosef Bacik 	{Opt_fragment_metadata, "fragment=metadata"},
523d0bd4560SJosef Bacik 	{Opt_fragment_all, "fragment=all"},
524d0bd4560SJosef Bacik #endif
525fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
526fb592373SJosef Bacik 	{Opt_ref_verify, "ref_verify"},
527fb592373SJosef Bacik #endif
52833268eafSJosef Bacik 	{Opt_err, NULL},
52995e05289SChris Mason };
53095e05289SChris Mason 
53174ef0018SQu Wenruo static const match_table_t rescue_tokens = {
53274ef0018SQu Wenruo 	{Opt_usebackuproot, "usebackuproot"},
53374ef0018SQu Wenruo 	{Opt_nologreplay, "nologreplay"},
53442437a63SJosef Bacik 	{Opt_ignorebadroots, "ignorebadroots"},
53542437a63SJosef Bacik 	{Opt_ignorebadroots, "ibadroots"},
536882dbe0cSJosef Bacik 	{Opt_ignoredatacsums, "ignoredatacsums"},
537882dbe0cSJosef Bacik 	{Opt_ignoredatacsums, "idatacsums"},
5389037d3cbSJosef Bacik 	{Opt_rescue_all, "all"},
53974ef0018SQu Wenruo 	{Opt_err, NULL},
54074ef0018SQu Wenruo };
54174ef0018SQu Wenruo 
542d70bf748SJosef Bacik static bool check_ro_option(struct btrfs_fs_info *fs_info, unsigned long opt,
543d70bf748SJosef Bacik 			    const char *opt_name)
544d70bf748SJosef Bacik {
545d70bf748SJosef Bacik 	if (fs_info->mount_opt & opt) {
546d70bf748SJosef Bacik 		btrfs_err(fs_info, "%s must be used with ro mount option",
547d70bf748SJosef Bacik 			  opt_name);
548d70bf748SJosef Bacik 		return true;
549d70bf748SJosef Bacik 	}
550d70bf748SJosef Bacik 	return false;
551d70bf748SJosef Bacik }
552d70bf748SJosef Bacik 
55374ef0018SQu Wenruo static int parse_rescue_options(struct btrfs_fs_info *info, const char *options)
55474ef0018SQu Wenruo {
55574ef0018SQu Wenruo 	char *opts;
55674ef0018SQu Wenruo 	char *orig;
55774ef0018SQu Wenruo 	char *p;
55874ef0018SQu Wenruo 	substring_t args[MAX_OPT_ARGS];
55974ef0018SQu Wenruo 	int ret = 0;
56074ef0018SQu Wenruo 
56174ef0018SQu Wenruo 	opts = kstrdup(options, GFP_KERNEL);
56274ef0018SQu Wenruo 	if (!opts)
56374ef0018SQu Wenruo 		return -ENOMEM;
56474ef0018SQu Wenruo 	orig = opts;
56574ef0018SQu Wenruo 
56674ef0018SQu Wenruo 	while ((p = strsep(&opts, ":")) != NULL) {
56774ef0018SQu Wenruo 		int token;
56874ef0018SQu Wenruo 
56974ef0018SQu Wenruo 		if (!*p)
57074ef0018SQu Wenruo 			continue;
57174ef0018SQu Wenruo 		token = match_token(p, rescue_tokens, args);
57274ef0018SQu Wenruo 		switch (token){
57374ef0018SQu Wenruo 		case Opt_usebackuproot:
57474ef0018SQu Wenruo 			btrfs_info(info,
57574ef0018SQu Wenruo 				   "trying to use backup root at mount time");
57674ef0018SQu Wenruo 			btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
57774ef0018SQu Wenruo 			break;
57874ef0018SQu Wenruo 		case Opt_nologreplay:
57974ef0018SQu Wenruo 			btrfs_set_and_info(info, NOLOGREPLAY,
58074ef0018SQu Wenruo 					   "disabling log replay at mount time");
58174ef0018SQu Wenruo 			break;
58242437a63SJosef Bacik 		case Opt_ignorebadroots:
58342437a63SJosef Bacik 			btrfs_set_and_info(info, IGNOREBADROOTS,
58442437a63SJosef Bacik 					   "ignoring bad roots");
58542437a63SJosef Bacik 			break;
586882dbe0cSJosef Bacik 		case Opt_ignoredatacsums:
587882dbe0cSJosef Bacik 			btrfs_set_and_info(info, IGNOREDATACSUMS,
588882dbe0cSJosef Bacik 					   "ignoring data csums");
589882dbe0cSJosef Bacik 			break;
5909037d3cbSJosef Bacik 		case Opt_rescue_all:
5919037d3cbSJosef Bacik 			btrfs_info(info, "enabling all of the rescue options");
5929037d3cbSJosef Bacik 			btrfs_set_and_info(info, IGNOREDATACSUMS,
5939037d3cbSJosef Bacik 					   "ignoring data csums");
5949037d3cbSJosef Bacik 			btrfs_set_and_info(info, IGNOREBADROOTS,
5959037d3cbSJosef Bacik 					   "ignoring bad roots");
5969037d3cbSJosef Bacik 			btrfs_set_and_info(info, NOLOGREPLAY,
5979037d3cbSJosef Bacik 					   "disabling log replay at mount time");
5989037d3cbSJosef Bacik 			break;
59974ef0018SQu Wenruo 		case Opt_err:
60074ef0018SQu Wenruo 			btrfs_info(info, "unrecognized rescue option '%s'", p);
60174ef0018SQu Wenruo 			ret = -EINVAL;
60274ef0018SQu Wenruo 			goto out;
60374ef0018SQu Wenruo 		default:
60474ef0018SQu Wenruo 			break;
60574ef0018SQu Wenruo 		}
60674ef0018SQu Wenruo 
60774ef0018SQu Wenruo 	}
60874ef0018SQu Wenruo out:
60974ef0018SQu Wenruo 	kfree(orig);
61074ef0018SQu Wenruo 	return ret;
61174ef0018SQu Wenruo }
61274ef0018SQu Wenruo 
613edf24abeSChristoph Hellwig /*
614edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
615edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
61649b25e05SJeff Mahoney  * XXX JDM: This needs to be cleaned up for remount.
617edf24abeSChristoph Hellwig  */
6182ff7e61eSJeff Mahoney int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
61996da0919SQu Wenruo 			unsigned long new_flags)
62095e05289SChris Mason {
62195e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
622e215772cSMisono, Tomohiro 	char *p, *num;
6234543df7eSChris Mason 	int intarg;
624a7a3f7caSSage Weil 	int ret = 0;
625261507a0SLi Zefan 	char *compress_type;
626261507a0SLi Zefan 	bool compress_force = false;
627b7c47bbbSTsutomu Itoh 	enum btrfs_compression_type saved_compress_type;
62827942c99SDavid Sterba 	int saved_compress_level;
629b7c47bbbSTsutomu Itoh 	bool saved_compress_force;
630b7c47bbbSTsutomu Itoh 	int no_compress = 0;
631dbecac26SMaciej S. Szmigiero 	const bool remounting = test_bit(BTRFS_FS_STATE_REMOUNTING, &info->fs_state);
632b6cda9bcSChris Mason 
6330b246afaSJeff Mahoney 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
63470f6d82eSOmar Sandoval 		btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
63594846229SBoris Burkov 	else if (btrfs_free_space_cache_v1_active(info)) {
6365d1ab66cSNaohiro Aota 		if (btrfs_is_zoned(info)) {
6375d1ab66cSNaohiro Aota 			btrfs_info(info,
6385d1ab66cSNaohiro Aota 			"zoned: clearing existing space cache");
6395d1ab66cSNaohiro Aota 			btrfs_set_super_cache_generation(info->super_copy, 0);
6405d1ab66cSNaohiro Aota 		} else {
64173bc1876SJosef Bacik 			btrfs_set_opt(info->mount_opt, SPACE_CACHE);
6425d1ab66cSNaohiro Aota 		}
6435d1ab66cSNaohiro Aota 	}
64473bc1876SJosef Bacik 
64596da0919SQu Wenruo 	/*
64696da0919SQu Wenruo 	 * Even the options are empty, we still need to do extra check
64796da0919SQu Wenruo 	 * against new flags
64896da0919SQu Wenruo 	 */
64995e05289SChris Mason 	if (!options)
65096da0919SQu Wenruo 		goto check;
65195e05289SChris Mason 
65295e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
65395e05289SChris Mason 		int token;
65495e05289SChris Mason 		if (!*p)
65595e05289SChris Mason 			continue;
65695e05289SChris Mason 
65795e05289SChris Mason 		token = match_token(p, tokens, args);
65895e05289SChris Mason 		switch (token) {
659dfe25020SChris Mason 		case Opt_degraded:
6600b246afaSJeff Mahoney 			btrfs_info(info, "allowing degraded mounts");
661dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
662dfe25020SChris Mason 			break;
66395e05289SChris Mason 		case Opt_subvol:
66437becec9SOmar Sandoval 		case Opt_subvol_empty:
66573f73415SJosef Bacik 		case Opt_subvolid:
66643e570b0SChristoph Hellwig 		case Opt_device:
667edf24abeSChristoph Hellwig 			/*
668fa59f27cSAnand Jain 			 * These are parsed by btrfs_parse_subvol_options or
669fa59f27cSAnand Jain 			 * btrfs_parse_device_options and can be ignored here.
670edf24abeSChristoph Hellwig 			 */
67195e05289SChris Mason 			break;
672b6cda9bcSChris Mason 		case Opt_nodatasum:
6733cdde224SJeff Mahoney 			btrfs_set_and_info(info, NODATASUM,
67407802534SQu Wenruo 					   "setting nodatasum");
675be20aa9dSChris Mason 			break;
676d399167dSQu Wenruo 		case Opt_datasum:
6773cdde224SJeff Mahoney 			if (btrfs_test_opt(info, NODATASUM)) {
6783cdde224SJeff Mahoney 				if (btrfs_test_opt(info, NODATACOW))
6790b246afaSJeff Mahoney 					btrfs_info(info,
6805d163e0eSJeff Mahoney 						   "setting datasum, datacow enabled");
681d399167dSQu Wenruo 				else
6820b246afaSJeff Mahoney 					btrfs_info(info, "setting datasum");
68307802534SQu Wenruo 			}
684d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATACOW);
685d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATASUM);
686d399167dSQu Wenruo 			break;
687be20aa9dSChris Mason 		case Opt_nodatacow:
6883cdde224SJeff Mahoney 			if (!btrfs_test_opt(info, NODATACOW)) {
6893cdde224SJeff Mahoney 				if (!btrfs_test_opt(info, COMPRESS) ||
6903cdde224SJeff Mahoney 				    !btrfs_test_opt(info, FORCE_COMPRESS)) {
6910b246afaSJeff Mahoney 					btrfs_info(info,
692efe120a0SFrank Holton 						   "setting nodatacow, compression disabled");
693bedb2ccaSAndrei Popa 				} else {
6940b246afaSJeff Mahoney 					btrfs_info(info, "setting nodatacow");
695bedb2ccaSAndrei Popa 				}
69607802534SQu Wenruo 			}
697bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, COMPRESS);
698bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
699be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
700be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
701b6cda9bcSChris Mason 			break;
702a258af7aSQu Wenruo 		case Opt_datacow:
7033cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NODATACOW,
70407802534SQu Wenruo 					     "setting datacow");
705a258af7aSQu Wenruo 			break;
706a555f810SChris Mason 		case Opt_compress_force:
707261507a0SLi Zefan 		case Opt_compress_force_type:
708261507a0SLi Zefan 			compress_force = true;
709c730ae0cSMarcos Paulo de Souza 			fallthrough;
710261507a0SLi Zefan 		case Opt_compress:
711261507a0SLi Zefan 		case Opt_compress_type:
7123cdde224SJeff Mahoney 			saved_compress_type = btrfs_test_opt(info,
7133cdde224SJeff Mahoney 							     COMPRESS) ?
714b7c47bbbSTsutomu Itoh 				info->compress_type : BTRFS_COMPRESS_NONE;
715b7c47bbbSTsutomu Itoh 			saved_compress_force =
7163cdde224SJeff Mahoney 				btrfs_test_opt(info, FORCE_COMPRESS);
71727942c99SDavid Sterba 			saved_compress_level = info->compress_level;
718261507a0SLi Zefan 			if (token == Opt_compress ||
719261507a0SLi Zefan 			    token == Opt_compress_force ||
720a7164fa4SDavid Sterba 			    strncmp(args[0].from, "zlib", 4) == 0) {
721261507a0SLi Zefan 				compress_type = "zlib";
722eae8d825SQu Wenruo 
723261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
724eae8d825SQu Wenruo 				info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
725eae8d825SQu Wenruo 				/*
726eae8d825SQu Wenruo 				 * args[0] contains uninitialized data since
727eae8d825SQu Wenruo 				 * for these tokens we don't expect any
728eae8d825SQu Wenruo 				 * parameter.
729eae8d825SQu Wenruo 				 */
730eae8d825SQu Wenruo 				if (token != Opt_compress &&
731eae8d825SQu Wenruo 				    token != Opt_compress_force)
732f51d2b59SDavid Sterba 					info->compress_level =
733d0ab62ceSDennis Zhou 					  btrfs_compress_str2level(
734d0ab62ceSDennis Zhou 							BTRFS_COMPRESS_ZLIB,
735d0ab62ceSDennis Zhou 							args[0].from + 4);
736063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
737bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
738bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
739b7c47bbbSTsutomu Itoh 				no_compress = 0;
740a7164fa4SDavid Sterba 			} else if (strncmp(args[0].from, "lzo", 3) == 0) {
741a6fa6faeSLi Zefan 				compress_type = "lzo";
742a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
743282dd7d7SMarcos Paulo de Souza 				info->compress_level = 0;
744063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
745bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
746bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
7472b0ce2c2SMitch Harder 				btrfs_set_fs_incompat(info, COMPRESS_LZO);
748b7c47bbbSTsutomu Itoh 				no_compress = 0;
7493f93aef5SDennis Zhou 			} else if (strncmp(args[0].from, "zstd", 4) == 0) {
7505c1aab1dSNick Terrell 				compress_type = "zstd";
7515c1aab1dSNick Terrell 				info->compress_type = BTRFS_COMPRESS_ZSTD;
7523f93aef5SDennis Zhou 				info->compress_level =
7533f93aef5SDennis Zhou 					btrfs_compress_str2level(
7543f93aef5SDennis Zhou 							 BTRFS_COMPRESS_ZSTD,
7553f93aef5SDennis Zhou 							 args[0].from + 4);
7565c1aab1dSNick Terrell 				btrfs_set_opt(info->mount_opt, COMPRESS);
7575c1aab1dSNick Terrell 				btrfs_clear_opt(info->mount_opt, NODATACOW);
7585c1aab1dSNick Terrell 				btrfs_clear_opt(info->mount_opt, NODATASUM);
7595c1aab1dSNick Terrell 				btrfs_set_fs_incompat(info, COMPRESS_ZSTD);
7605c1aab1dSNick Terrell 				no_compress = 0;
761063849eaSArnd Hannemann 			} else if (strncmp(args[0].from, "no", 2) == 0) {
762063849eaSArnd Hannemann 				compress_type = "no";
76327942c99SDavid Sterba 				info->compress_level = 0;
76427942c99SDavid Sterba 				info->compress_type = 0;
765063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, COMPRESS);
766063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
767063849eaSArnd Hannemann 				compress_force = false;
768b7c47bbbSTsutomu Itoh 				no_compress++;
769261507a0SLi Zefan 			} else {
770e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized compression value %s",
771e3a4167cSDavid Sterba 					  args[0].from);
772261507a0SLi Zefan 				ret = -EINVAL;
773261507a0SLi Zefan 				goto out;
774261507a0SLi Zefan 			}
775261507a0SLi Zefan 
776261507a0SLi Zefan 			if (compress_force) {
777b7c47bbbSTsutomu Itoh 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
778143f3636SDavid Sterba 			} else {
7794027e0f4SWang Shilong 				/*
7804027e0f4SWang Shilong 				 * If we remount from compress-force=xxx to
7814027e0f4SWang Shilong 				 * compress=xxx, we need clear FORCE_COMPRESS
7824027e0f4SWang Shilong 				 * flag, otherwise, there is no way for users
7834027e0f4SWang Shilong 				 * to disable forcible compression separately.
7844027e0f4SWang Shilong 				 */
7854027e0f4SWang Shilong 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
786a7e252afSMiao Xie 			}
78727942c99SDavid Sterba 			if (no_compress == 1) {
78827942c99SDavid Sterba 				btrfs_info(info, "use no compression");
78927942c99SDavid Sterba 			} else if ((info->compress_type != saved_compress_type) ||
79027942c99SDavid Sterba 				   (compress_force != saved_compress_force) ||
79127942c99SDavid Sterba 				   (info->compress_level != saved_compress_level)) {
792f51d2b59SDavid Sterba 				btrfs_info(info, "%s %s compression, level %d",
793b7c47bbbSTsutomu Itoh 					   (compress_force) ? "force" : "use",
794f51d2b59SDavid Sterba 					   compress_type, info->compress_level);
795b7c47bbbSTsutomu Itoh 			}
796b7c47bbbSTsutomu Itoh 			compress_force = false;
797a555f810SChris Mason 			break;
798e18e4809SChris Mason 		case Opt_ssd:
7993cdde224SJeff Mahoney 			btrfs_set_and_info(info, SSD,
800583b7231SHans van Kranenburg 					   "enabling ssd optimizations");
801951e7966SAdam Borowski 			btrfs_clear_opt(info->mount_opt, NOSSD);
802e18e4809SChris Mason 			break;
803451d7585SChris Mason 		case Opt_ssd_spread:
804583b7231SHans van Kranenburg 			btrfs_set_and_info(info, SSD,
805583b7231SHans van Kranenburg 					   "enabling ssd optimizations");
8063cdde224SJeff Mahoney 			btrfs_set_and_info(info, SSD_SPREAD,
807583b7231SHans van Kranenburg 					   "using spread ssd allocation scheme");
808951e7966SAdam Borowski 			btrfs_clear_opt(info->mount_opt, NOSSD);
809451d7585SChris Mason 			break;
8103b30c22fSChris Mason 		case Opt_nossd:
811583b7231SHans van Kranenburg 			btrfs_set_opt(info->mount_opt, NOSSD);
812583b7231SHans van Kranenburg 			btrfs_clear_and_info(info, SSD,
813583b7231SHans van Kranenburg 					     "not using ssd optimizations");
814c730ae0cSMarcos Paulo de Souza 			fallthrough;
81562b8e077SHoward McLauchlan 		case Opt_nossd_spread:
816583b7231SHans van Kranenburg 			btrfs_clear_and_info(info, SSD_SPREAD,
817583b7231SHans van Kranenburg 					     "not using spread ssd allocation scheme");
8183b30c22fSChris Mason 			break;
819842bef58SQu Wenruo 		case Opt_barrier:
8203cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NOBARRIER,
82107802534SQu Wenruo 					     "turning on barriers");
822842bef58SQu Wenruo 			break;
82321ad10cfSChris Mason 		case Opt_nobarrier:
8243cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOBARRIER,
82507802534SQu Wenruo 					   "turning off barriers");
82621ad10cfSChris Mason 			break;
8274543df7eSChris Mason 		case Opt_thread_pool:
8282c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
8292c334e87SWang Shilong 			if (ret) {
830e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized thread_pool value %s",
831e3a4167cSDavid Sterba 					  args[0].from);
8322c334e87SWang Shilong 				goto out;
833f7b885beSAnand Jain 			} else if (intarg == 0) {
834e3a4167cSDavid Sterba 				btrfs_err(info, "invalid value 0 for thread_pool");
8352c334e87SWang Shilong 				ret = -EINVAL;
8362c334e87SWang Shilong 				goto out;
8372c334e87SWang Shilong 			}
838f7b885beSAnand Jain 			info->thread_pool_size = intarg;
8394543df7eSChris Mason 			break;
8406f568d35SChris Mason 		case Opt_max_inline:
841edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
8426f568d35SChris Mason 			if (num) {
84391748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
8446f568d35SChris Mason 				kfree(num);
8456f568d35SChris Mason 
84615ada040SChris Mason 				if (info->max_inline) {
847feb5f965SMitch Harder 					info->max_inline = min_t(u64,
84815ada040SChris Mason 						info->max_inline,
8490b246afaSJeff Mahoney 						info->sectorsize);
85015ada040SChris Mason 				}
8510b246afaSJeff Mahoney 				btrfs_info(info, "max_inline at %llu",
852c1c9ff7cSGeert Uytterhoeven 					   info->max_inline);
8532c334e87SWang Shilong 			} else {
8542c334e87SWang Shilong 				ret = -ENOMEM;
8552c334e87SWang Shilong 				goto out;
8566f568d35SChris Mason 			}
8576f568d35SChris Mason 			break;
858bd0330adSQu Wenruo 		case Opt_acl:
85945ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL
8601751e8a6SLinus Torvalds 			info->sb->s_flags |= SB_POSIXACL;
861bd0330adSQu Wenruo 			break;
86245ff35d6SGuangliang Zhao #else
8630b246afaSJeff Mahoney 			btrfs_err(info, "support for ACL not compiled in!");
86445ff35d6SGuangliang Zhao 			ret = -EINVAL;
86545ff35d6SGuangliang Zhao 			goto out;
86645ff35d6SGuangliang Zhao #endif
86733268eafSJosef Bacik 		case Opt_noacl:
8681751e8a6SLinus Torvalds 			info->sb->s_flags &= ~SB_POSIXACL;
86933268eafSJosef Bacik 			break;
8703a5e1404SSage Weil 		case Opt_notreelog:
8713cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOTREELOG,
87207802534SQu Wenruo 					   "disabling tree log");
8733a5e1404SSage Weil 			break;
874a88998f2SQu Wenruo 		case Opt_treelog:
8753cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NOTREELOG,
87607802534SQu Wenruo 					     "enabling tree log");
877a88998f2SQu Wenruo 			break;
878fed8f166SQu Wenruo 		case Opt_norecovery:
87996da0919SQu Wenruo 		case Opt_nologreplay:
88074ef0018SQu Wenruo 			btrfs_warn(info,
88174ef0018SQu Wenruo 		"'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
8823cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOLOGREPLAY,
88396da0919SQu Wenruo 					   "disabling log replay at mount time");
88496da0919SQu Wenruo 			break;
885dccae999SSage Weil 		case Opt_flushoncommit:
8863cdde224SJeff Mahoney 			btrfs_set_and_info(info, FLUSHONCOMMIT,
88707802534SQu Wenruo 					   "turning on flush-on-commit");
888dccae999SSage Weil 			break;
8892c9ee856SQu Wenruo 		case Opt_noflushoncommit:
8903cdde224SJeff Mahoney 			btrfs_clear_and_info(info, FLUSHONCOMMIT,
89107802534SQu Wenruo 					     "turning off flush-on-commit");
8922c9ee856SQu Wenruo 			break;
89397e728d4SJosef Bacik 		case Opt_ratio:
8942c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
895e3a4167cSDavid Sterba 			if (ret) {
896e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized metadata_ratio value %s",
897e3a4167cSDavid Sterba 					  args[0].from);
8982c334e87SWang Shilong 				goto out;
899e3a4167cSDavid Sterba 			}
90097e728d4SJosef Bacik 			info->metadata_ratio = intarg;
901764cb8b4SAnand Jain 			btrfs_info(info, "metadata ratio %u",
90297e728d4SJosef Bacik 				   info->metadata_ratio);
90397e728d4SJosef Bacik 			break;
904e244a0aeSChristoph Hellwig 		case Opt_discard:
905b0643e59SDennis Zhou 		case Opt_discard_mode:
906b0643e59SDennis Zhou 			if (token == Opt_discard ||
907b0643e59SDennis Zhou 			    strcmp(args[0].from, "sync") == 0) {
908b0643e59SDennis Zhou 				btrfs_clear_opt(info->mount_opt, DISCARD_ASYNC);
90946b27f50SDennis Zhou 				btrfs_set_and_info(info, DISCARD_SYNC,
91046b27f50SDennis Zhou 						   "turning on sync discard");
911b0643e59SDennis Zhou 			} else if (strcmp(args[0].from, "async") == 0) {
912b0643e59SDennis Zhou 				btrfs_clear_opt(info->mount_opt, DISCARD_SYNC);
913b0643e59SDennis Zhou 				btrfs_set_and_info(info, DISCARD_ASYNC,
914b0643e59SDennis Zhou 						   "turning on async discard");
915b0643e59SDennis Zhou 			} else {
916e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized discard mode value %s",
917e3a4167cSDavid Sterba 					  args[0].from);
918b0643e59SDennis Zhou 				ret = -EINVAL;
919b0643e59SDennis Zhou 				goto out;
920b0643e59SDennis Zhou 			}
921e244a0aeSChristoph Hellwig 			break;
922e07a2adeSQu Wenruo 		case Opt_nodiscard:
92346b27f50SDennis Zhou 			btrfs_clear_and_info(info, DISCARD_SYNC,
92407802534SQu Wenruo 					     "turning off discard");
925b0643e59SDennis Zhou 			btrfs_clear_and_info(info, DISCARD_ASYNC,
926b0643e59SDennis Zhou 					     "turning off async discard");
927e07a2adeSQu Wenruo 			break;
9280af3d00bSJosef Bacik 		case Opt_space_cache:
92970f6d82eSOmar Sandoval 		case Opt_space_cache_version:
93063cd070dSJosef Bacik 			/*
93163cd070dSJosef Bacik 			 * We already set FREE_SPACE_TREE above because we have
93263cd070dSJosef Bacik 			 * compat_ro(FREE_SPACE_TREE) set, and we aren't going
93363cd070dSJosef Bacik 			 * to allow v1 to be set for extent tree v2, simply
93463cd070dSJosef Bacik 			 * ignore this setting if we're extent tree v2.
93563cd070dSJosef Bacik 			 */
93663cd070dSJosef Bacik 			if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
93763cd070dSJosef Bacik 				break;
93870f6d82eSOmar Sandoval 			if (token == Opt_space_cache ||
93970f6d82eSOmar Sandoval 			    strcmp(args[0].from, "v1") == 0) {
9400b246afaSJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
94170f6d82eSOmar Sandoval 						FREE_SPACE_TREE);
9423cdde224SJeff Mahoney 				btrfs_set_and_info(info, SPACE_CACHE,
94307802534SQu Wenruo 					   "enabling disk space caching");
94470f6d82eSOmar Sandoval 			} else if (strcmp(args[0].from, "v2") == 0) {
9450b246afaSJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
94670f6d82eSOmar Sandoval 						SPACE_CACHE);
9470b246afaSJeff Mahoney 				btrfs_set_and_info(info, FREE_SPACE_TREE,
94870f6d82eSOmar Sandoval 						   "enabling free space tree");
94970f6d82eSOmar Sandoval 			} else {
950e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized space_cache value %s",
951e3a4167cSDavid Sterba 					  args[0].from);
95270f6d82eSOmar Sandoval 				ret = -EINVAL;
95370f6d82eSOmar Sandoval 				goto out;
95470f6d82eSOmar Sandoval 			}
9550de90876SJosef Bacik 			break;
956f420ee1eSStefan Behrens 		case Opt_rescan_uuid_tree:
957f420ee1eSStefan Behrens 			btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
958f420ee1eSStefan Behrens 			break;
95973bc1876SJosef Bacik 		case Opt_no_space_cache:
96063cd070dSJosef Bacik 			/*
96163cd070dSJosef Bacik 			 * We cannot operate without the free space tree with
96263cd070dSJosef Bacik 			 * extent tree v2, ignore this option.
96363cd070dSJosef Bacik 			 */
96463cd070dSJosef Bacik 			if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
96563cd070dSJosef Bacik 				break;
9663cdde224SJeff Mahoney 			if (btrfs_test_opt(info, SPACE_CACHE)) {
9670b246afaSJeff Mahoney 				btrfs_clear_and_info(info, SPACE_CACHE,
96807802534SQu Wenruo 					     "disabling disk space caching");
96970f6d82eSOmar Sandoval 			}
9703cdde224SJeff Mahoney 			if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
9710b246afaSJeff Mahoney 				btrfs_clear_and_info(info, FREE_SPACE_TREE,
97270f6d82eSOmar Sandoval 					     "disabling free space tree");
97370f6d82eSOmar Sandoval 			}
97473bc1876SJosef Bacik 			break;
9754b9465cbSChris Mason 		case Opt_inode_cache:
9763818aea2SQu Wenruo 		case Opt_noinode_cache:
9775297199aSNikolay Borisov 			btrfs_warn(info,
9785297199aSNikolay Borisov 	"the 'inode_cache' option is deprecated and has no effect since 5.11");
9794b9465cbSChris Mason 			break;
98088c2ba3bSJosef Bacik 		case Opt_clear_cache:
98163cd070dSJosef Bacik 			/*
98263cd070dSJosef Bacik 			 * We cannot clear the free space tree with extent tree
98363cd070dSJosef Bacik 			 * v2, ignore this option.
98463cd070dSJosef Bacik 			 */
98563cd070dSJosef Bacik 			if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
98663cd070dSJosef Bacik 				break;
9873cdde224SJeff Mahoney 			btrfs_set_and_info(info, CLEAR_CACHE,
98807802534SQu Wenruo 					   "force clearing of disk cache");
9890af3d00bSJosef Bacik 			break;
9904260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
9914260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
9924260f7c7SSage Weil 			break;
99391435650SChris Mason 		case Opt_enospc_debug:
99491435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
99591435650SChris Mason 			break;
99653036293SQu Wenruo 		case Opt_noenospc_debug:
99753036293SQu Wenruo 			btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
99853036293SQu Wenruo 			break;
9994cb5300bSChris Mason 		case Opt_defrag:
10003cdde224SJeff Mahoney 			btrfs_set_and_info(info, AUTO_DEFRAG,
100107802534SQu Wenruo 					   "enabling auto defrag");
10024cb5300bSChris Mason 			break;
1003fc0ca9afSQu Wenruo 		case Opt_nodefrag:
10043cdde224SJeff Mahoney 			btrfs_clear_and_info(info, AUTO_DEFRAG,
100507802534SQu Wenruo 					     "disabling auto defrag");
1006fc0ca9afSQu Wenruo 			break;
1007af31f5e5SChris Mason 		case Opt_recovery:
10088dcddfa0SQu Wenruo 		case Opt_usebackuproot:
100974ef0018SQu Wenruo 			btrfs_warn(info,
101074ef0018SQu Wenruo 			"'%s' is deprecated, use 'rescue=usebackuproot' instead",
101174ef0018SQu Wenruo 				   token == Opt_recovery ? "recovery" :
101274ef0018SQu Wenruo 				   "usebackuproot");
10130b246afaSJeff Mahoney 			btrfs_info(info,
10148dcddfa0SQu Wenruo 				   "trying to use backup root at mount time");
10158dcddfa0SQu Wenruo 			btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
1016af31f5e5SChris Mason 			break;
10179555c6c1SIlya Dryomov 		case Opt_skip_balance:
10189555c6c1SIlya Dryomov 			btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
10199555c6c1SIlya Dryomov 			break;
102021adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
102121adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
10220b246afaSJeff Mahoney 			btrfs_info(info,
1023efe120a0SFrank Holton 				   "enabling check integrity including extent data");
1024cbeaae4fSDavid Sterba 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY_DATA);
102521adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
102621adbd5cSStefan Behrens 			break;
102721adbd5cSStefan Behrens 		case Opt_check_integrity:
10280b246afaSJeff Mahoney 			btrfs_info(info, "enabling check integrity");
102921adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
103021adbd5cSStefan Behrens 			break;
103121adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
10322c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
1033e3a4167cSDavid Sterba 			if (ret) {
1034e3a4167cSDavid Sterba 				btrfs_err(info,
1035e3a4167cSDavid Sterba 				"unrecognized check_integrity_print_mask value %s",
1036e3a4167cSDavid Sterba 					args[0].from);
10372c334e87SWang Shilong 				goto out;
1038e3a4167cSDavid Sterba 			}
103921adbd5cSStefan Behrens 			info->check_integrity_print_mask = intarg;
104002453bdeSAnand Jain 			btrfs_info(info, "check_integrity_print_mask 0x%x",
104121adbd5cSStefan Behrens 				   info->check_integrity_print_mask);
104221adbd5cSStefan Behrens 			break;
104321adbd5cSStefan Behrens #else
104421adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
104521adbd5cSStefan Behrens 		case Opt_check_integrity:
104621adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
10470b246afaSJeff Mahoney 			btrfs_err(info,
1048efe120a0SFrank Holton 				  "support for check_integrity* not compiled in!");
104921adbd5cSStefan Behrens 			ret = -EINVAL;
105021adbd5cSStefan Behrens 			goto out;
105121adbd5cSStefan Behrens #endif
10528c342930SJeff Mahoney 		case Opt_fatal_errors:
1053e3a4167cSDavid Sterba 			if (strcmp(args[0].from, "panic") == 0) {
10548c342930SJeff Mahoney 				btrfs_set_opt(info->mount_opt,
10558c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
1056e3a4167cSDavid Sterba 			} else if (strcmp(args[0].from, "bug") == 0) {
10578c342930SJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
10588c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
1059e3a4167cSDavid Sterba 			} else {
1060e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized fatal_errors value %s",
1061e3a4167cSDavid Sterba 					  args[0].from);
10628c342930SJeff Mahoney 				ret = -EINVAL;
10638c342930SJeff Mahoney 				goto out;
10648c342930SJeff Mahoney 			}
10658c342930SJeff Mahoney 			break;
10668b87dc17SDavid Sterba 		case Opt_commit_interval:
10678b87dc17SDavid Sterba 			intarg = 0;
10688b87dc17SDavid Sterba 			ret = match_int(&args[0], &intarg);
1069e3a4167cSDavid Sterba 			if (ret) {
1070e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized commit_interval value %s",
1071e3a4167cSDavid Sterba 					  args[0].from);
1072e3a4167cSDavid Sterba 				ret = -EINVAL;
10738b87dc17SDavid Sterba 				goto out;
1074e3a4167cSDavid Sterba 			}
1075d3740608SAnand Jain 			if (intarg == 0) {
1076d3740608SAnand Jain 				btrfs_info(info,
1077d3740608SAnand Jain 					   "using default commit interval %us",
1078d3740608SAnand Jain 					   BTRFS_DEFAULT_COMMIT_INTERVAL);
1079d3740608SAnand Jain 				intarg = BTRFS_DEFAULT_COMMIT_INTERVAL;
1080d3740608SAnand Jain 			} else if (intarg > 300) {
1081d3740608SAnand Jain 				btrfs_warn(info, "excessive commit interval %d",
10828b87dc17SDavid Sterba 					   intarg);
10838b87dc17SDavid Sterba 			}
10848b87dc17SDavid Sterba 			info->commit_interval = intarg;
10858b87dc17SDavid Sterba 			break;
108674ef0018SQu Wenruo 		case Opt_rescue:
108774ef0018SQu Wenruo 			ret = parse_rescue_options(info, args[0].from);
1088e3a4167cSDavid Sterba 			if (ret < 0) {
1089e3a4167cSDavid Sterba 				btrfs_err(info, "unrecognized rescue value %s",
1090e3a4167cSDavid Sterba 					  args[0].from);
109174ef0018SQu Wenruo 				goto out;
1092e3a4167cSDavid Sterba 			}
109374ef0018SQu Wenruo 			break;
1094d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
1095d0bd4560SJosef Bacik 		case Opt_fragment_all:
10960b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting all space");
1097d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
1098d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
1099d0bd4560SJosef Bacik 			break;
1100d0bd4560SJosef Bacik 		case Opt_fragment_metadata:
11010b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting metadata");
1102d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt,
1103d0bd4560SJosef Bacik 				      FRAGMENT_METADATA);
1104d0bd4560SJosef Bacik 			break;
1105d0bd4560SJosef Bacik 		case Opt_fragment_data:
11060b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting data");
1107d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
1108d0bd4560SJosef Bacik 			break;
1109d0bd4560SJosef Bacik #endif
1110fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
1111fb592373SJosef Bacik 		case Opt_ref_verify:
1112fb592373SJosef Bacik 			btrfs_info(info, "doing ref verification");
1113fb592373SJosef Bacik 			btrfs_set_opt(info->mount_opt, REF_VERIFY);
1114fb592373SJosef Bacik 			break;
1115fb592373SJosef Bacik #endif
1116a7a3f7caSSage Weil 		case Opt_err:
11177e8f19e5SDavid Sterba 			btrfs_err(info, "unrecognized mount option '%s'", p);
1118a7a3f7caSSage Weil 			ret = -EINVAL;
1119a7a3f7caSSage Weil 			goto out;
112095e05289SChris Mason 		default:
1121be20aa9dSChris Mason 			break;
112295e05289SChris Mason 		}
112395e05289SChris Mason 	}
112496da0919SQu Wenruo check:
1125d70bf748SJosef Bacik 	/* We're read-only, don't have to check. */
1126d70bf748SJosef Bacik 	if (new_flags & SB_RDONLY)
1127d70bf748SJosef Bacik 		goto out;
1128d70bf748SJosef Bacik 
112942437a63SJosef Bacik 	if (check_ro_option(info, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||
1130882dbe0cSJosef Bacik 	    check_ro_option(info, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
1131882dbe0cSJosef Bacik 	    check_ro_option(info, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums"))
113296da0919SQu Wenruo 		ret = -EINVAL;
1133a7a3f7caSSage Weil out:
11340b246afaSJeff Mahoney 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
11353cdde224SJeff Mahoney 	    !btrfs_test_opt(info, FREE_SPACE_TREE) &&
11363cdde224SJeff Mahoney 	    !btrfs_test_opt(info, CLEAR_CACHE)) {
11370b246afaSJeff Mahoney 		btrfs_err(info, "cannot disable free space tree");
113870f6d82eSOmar Sandoval 		ret = -EINVAL;
113970f6d82eSOmar Sandoval 
114070f6d82eSOmar Sandoval 	}
11415d1ab66cSNaohiro Aota 	if (!ret)
11425d1ab66cSNaohiro Aota 		ret = btrfs_check_mountopts_zoned(info);
1143dbecac26SMaciej S. Szmigiero 	if (!ret && !remounting) {
1144dbecac26SMaciej S. Szmigiero 		if (btrfs_test_opt(info, SPACE_CACHE))
11450b246afaSJeff Mahoney 			btrfs_info(info, "disk space caching is enabled");
1146dbecac26SMaciej S. Szmigiero 		if (btrfs_test_opt(info, FREE_SPACE_TREE))
11470b246afaSJeff Mahoney 			btrfs_info(info, "using free space tree");
1148dbecac26SMaciej S. Szmigiero 	}
1149a7a3f7caSSage Weil 	return ret;
1150edf24abeSChristoph Hellwig }
1151edf24abeSChristoph Hellwig 
1152edf24abeSChristoph Hellwig /*
1153edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
1154edf24abeSChristoph Hellwig  *
1155edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
1156edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
1157edf24abeSChristoph Hellwig  */
1158fa59f27cSAnand Jain static int btrfs_parse_device_options(const char *options, fmode_t flags,
1159d64dcbd1SGu Jinxiang 				      void *holder)
1160edf24abeSChristoph Hellwig {
1161edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
116283c8c9bdSJeff Liu 	char *device_name, *opts, *orig, *p;
116336350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
1164d7407606SMisono, Tomohiro 	int error = 0;
1165d7407606SMisono, Tomohiro 
11665139cff5SDavid Sterba 	lockdep_assert_held(&uuid_mutex);
11675139cff5SDavid Sterba 
1168d7407606SMisono, Tomohiro 	if (!options)
1169d7407606SMisono, Tomohiro 		return 0;
1170d7407606SMisono, Tomohiro 
1171d7407606SMisono, Tomohiro 	/*
1172d7407606SMisono, Tomohiro 	 * strsep changes the string, duplicate it because btrfs_parse_options
1173d7407606SMisono, Tomohiro 	 * gets called later
1174d7407606SMisono, Tomohiro 	 */
1175d7407606SMisono, Tomohiro 	opts = kstrdup(options, GFP_KERNEL);
1176d7407606SMisono, Tomohiro 	if (!opts)
1177d7407606SMisono, Tomohiro 		return -ENOMEM;
1178d7407606SMisono, Tomohiro 	orig = opts;
1179d7407606SMisono, Tomohiro 
1180d7407606SMisono, Tomohiro 	while ((p = strsep(&opts, ",")) != NULL) {
1181d7407606SMisono, Tomohiro 		int token;
1182d7407606SMisono, Tomohiro 
1183d7407606SMisono, Tomohiro 		if (!*p)
1184d7407606SMisono, Tomohiro 			continue;
1185d7407606SMisono, Tomohiro 
1186d7407606SMisono, Tomohiro 		token = match_token(p, tokens, args);
1187d7407606SMisono, Tomohiro 		if (token == Opt_device) {
1188d7407606SMisono, Tomohiro 			device_name = match_strdup(&args[0]);
1189d7407606SMisono, Tomohiro 			if (!device_name) {
1190d7407606SMisono, Tomohiro 				error = -ENOMEM;
1191d7407606SMisono, Tomohiro 				goto out;
1192d7407606SMisono, Tomohiro 			}
119336350e95SGu Jinxiang 			device = btrfs_scan_one_device(device_name, flags,
119436350e95SGu Jinxiang 					holder);
1195d7407606SMisono, Tomohiro 			kfree(device_name);
119636350e95SGu Jinxiang 			if (IS_ERR(device)) {
119736350e95SGu Jinxiang 				error = PTR_ERR(device);
1198d7407606SMisono, Tomohiro 				goto out;
1199d7407606SMisono, Tomohiro 			}
1200d7407606SMisono, Tomohiro 		}
120136350e95SGu Jinxiang 	}
1202d7407606SMisono, Tomohiro 
1203d7407606SMisono, Tomohiro out:
1204d7407606SMisono, Tomohiro 	kfree(orig);
1205d7407606SMisono, Tomohiro 	return error;
1206d7407606SMisono, Tomohiro }
1207d7407606SMisono, Tomohiro 
1208d7407606SMisono, Tomohiro /*
1209d7407606SMisono, Tomohiro  * Parse mount options that are related to subvolume id
1210d7407606SMisono, Tomohiro  *
1211d7407606SMisono, Tomohiro  * The value is later passed to mount_subvol()
1212d7407606SMisono, Tomohiro  */
121393b9bcdfSGu Jinxiang static int btrfs_parse_subvol_options(const char *options, char **subvol_name,
121493b9bcdfSGu Jinxiang 		u64 *subvol_objectid)
1215d7407606SMisono, Tomohiro {
1216d7407606SMisono, Tomohiro 	substring_t args[MAX_OPT_ARGS];
1217d7407606SMisono, Tomohiro 	char *opts, *orig, *p;
1218edf24abeSChristoph Hellwig 	int error = 0;
1219ccb0e7d1SAnand Jain 	u64 subvolid;
1220edf24abeSChristoph Hellwig 
1221edf24abeSChristoph Hellwig 	if (!options)
1222830c4adbSJosef Bacik 		return 0;
1223edf24abeSChristoph Hellwig 
1224edf24abeSChristoph Hellwig 	/*
1225d7407606SMisono, Tomohiro 	 * strsep changes the string, duplicate it because
1226fa59f27cSAnand Jain 	 * btrfs_parse_device_options gets called later
1227edf24abeSChristoph Hellwig 	 */
1228edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
1229edf24abeSChristoph Hellwig 	if (!opts)
1230edf24abeSChristoph Hellwig 		return -ENOMEM;
12313f3d0bc0STero Roponen 	orig = opts;
1232edf24abeSChristoph Hellwig 
1233edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
1234edf24abeSChristoph Hellwig 		int token;
1235edf24abeSChristoph Hellwig 		if (!*p)
1236edf24abeSChristoph Hellwig 			continue;
1237edf24abeSChristoph Hellwig 
1238edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
1239edf24abeSChristoph Hellwig 		switch (token) {
1240edf24abeSChristoph Hellwig 		case Opt_subvol:
1241a90e8b6fSIlya Dryomov 			kfree(*subvol_name);
1242edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
12432c334e87SWang Shilong 			if (!*subvol_name) {
12442c334e87SWang Shilong 				error = -ENOMEM;
12452c334e87SWang Shilong 				goto out;
12462c334e87SWang Shilong 			}
1247edf24abeSChristoph Hellwig 			break;
124873f73415SJosef Bacik 		case Opt_subvolid:
1249ccb0e7d1SAnand Jain 			error = match_u64(&args[0], &subvolid);
1250ccb0e7d1SAnand Jain 			if (error)
12512c334e87SWang Shilong 				goto out;
1252ccb0e7d1SAnand Jain 
1253ccb0e7d1SAnand Jain 			/* we want the original fs_tree */
1254ccb0e7d1SAnand Jain 			if (subvolid == 0)
1255ccb0e7d1SAnand Jain 				subvolid = BTRFS_FS_TREE_OBJECTID;
1256ccb0e7d1SAnand Jain 
1257ccb0e7d1SAnand Jain 			*subvol_objectid = subvolid;
125873f73415SJosef Bacik 			break;
1259edf24abeSChristoph Hellwig 		default:
1260edf24abeSChristoph Hellwig 			break;
1261edf24abeSChristoph Hellwig 		}
1262edf24abeSChristoph Hellwig 	}
1263edf24abeSChristoph Hellwig 
1264edf24abeSChristoph Hellwig out:
1265830c4adbSJosef Bacik 	kfree(orig);
1266edf24abeSChristoph Hellwig 	return error;
126795e05289SChris Mason }
126895e05289SChris Mason 
1269c0c907a4SMarcos Paulo de Souza char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
127073f73415SJosef Bacik 					  u64 subvol_objectid)
127173f73415SJosef Bacik {
1272815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
12735168489aSJosef Bacik 	struct btrfs_root *fs_root = NULL;
127405dbe683SOmar Sandoval 	struct btrfs_root_ref *root_ref;
127505dbe683SOmar Sandoval 	struct btrfs_inode_ref *inode_ref;
127605dbe683SOmar Sandoval 	struct btrfs_key key;
127705dbe683SOmar Sandoval 	struct btrfs_path *path = NULL;
127805dbe683SOmar Sandoval 	char *name = NULL, *ptr;
127905dbe683SOmar Sandoval 	u64 dirid;
128005dbe683SOmar Sandoval 	int len;
128105dbe683SOmar Sandoval 	int ret;
128205dbe683SOmar Sandoval 
128305dbe683SOmar Sandoval 	path = btrfs_alloc_path();
128405dbe683SOmar Sandoval 	if (!path) {
128505dbe683SOmar Sandoval 		ret = -ENOMEM;
128605dbe683SOmar Sandoval 		goto err;
128705dbe683SOmar Sandoval 	}
128805dbe683SOmar Sandoval 
12893ec83621SDavid Sterba 	name = kmalloc(PATH_MAX, GFP_KERNEL);
129005dbe683SOmar Sandoval 	if (!name) {
129105dbe683SOmar Sandoval 		ret = -ENOMEM;
129205dbe683SOmar Sandoval 		goto err;
129305dbe683SOmar Sandoval 	}
129405dbe683SOmar Sandoval 	ptr = name + PATH_MAX - 1;
129505dbe683SOmar Sandoval 	ptr[0] = '\0';
129605dbe683SOmar Sandoval 
129705dbe683SOmar Sandoval 	/*
129805dbe683SOmar Sandoval 	 * Walk up the subvolume trees in the tree of tree roots by root
129905dbe683SOmar Sandoval 	 * backrefs until we hit the top-level subvolume.
130005dbe683SOmar Sandoval 	 */
130105dbe683SOmar Sandoval 	while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
130205dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
130305dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_BACKREF_KEY;
130405dbe683SOmar Sandoval 		key.offset = (u64)-1;
130505dbe683SOmar Sandoval 
13060ff40a91SMarcos Paulo de Souza 		ret = btrfs_search_backwards(root, &key, path);
130705dbe683SOmar Sandoval 		if (ret < 0) {
130805dbe683SOmar Sandoval 			goto err;
130905dbe683SOmar Sandoval 		} else if (ret > 0) {
131005dbe683SOmar Sandoval 			ret = -ENOENT;
131105dbe683SOmar Sandoval 			goto err;
131205dbe683SOmar Sandoval 		}
131305dbe683SOmar Sandoval 
131405dbe683SOmar Sandoval 		subvol_objectid = key.offset;
131505dbe683SOmar Sandoval 
131605dbe683SOmar Sandoval 		root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
131705dbe683SOmar Sandoval 					  struct btrfs_root_ref);
131805dbe683SOmar Sandoval 		len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
131905dbe683SOmar Sandoval 		ptr -= len + 1;
132005dbe683SOmar Sandoval 		if (ptr < name) {
132105dbe683SOmar Sandoval 			ret = -ENAMETOOLONG;
132205dbe683SOmar Sandoval 			goto err;
132305dbe683SOmar Sandoval 		}
132405dbe683SOmar Sandoval 		read_extent_buffer(path->nodes[0], ptr + 1,
132505dbe683SOmar Sandoval 				   (unsigned long)(root_ref + 1), len);
132605dbe683SOmar Sandoval 		ptr[0] = '/';
132705dbe683SOmar Sandoval 		dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
132805dbe683SOmar Sandoval 		btrfs_release_path(path);
132905dbe683SOmar Sandoval 
133056e9357aSDavid Sterba 		fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true);
133105dbe683SOmar Sandoval 		if (IS_ERR(fs_root)) {
133205dbe683SOmar Sandoval 			ret = PTR_ERR(fs_root);
13335168489aSJosef Bacik 			fs_root = NULL;
13345168489aSJosef Bacik 			goto err;
13355168489aSJosef Bacik 		}
133605dbe683SOmar Sandoval 
133705dbe683SOmar Sandoval 		/*
133805dbe683SOmar Sandoval 		 * Walk up the filesystem tree by inode refs until we hit the
133905dbe683SOmar Sandoval 		 * root directory.
134005dbe683SOmar Sandoval 		 */
134105dbe683SOmar Sandoval 		while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
134205dbe683SOmar Sandoval 			key.objectid = dirid;
134305dbe683SOmar Sandoval 			key.type = BTRFS_INODE_REF_KEY;
134405dbe683SOmar Sandoval 			key.offset = (u64)-1;
134505dbe683SOmar Sandoval 
13460ff40a91SMarcos Paulo de Souza 			ret = btrfs_search_backwards(fs_root, &key, path);
134705dbe683SOmar Sandoval 			if (ret < 0) {
134805dbe683SOmar Sandoval 				goto err;
134905dbe683SOmar Sandoval 			} else if (ret > 0) {
135005dbe683SOmar Sandoval 				ret = -ENOENT;
135105dbe683SOmar Sandoval 				goto err;
135205dbe683SOmar Sandoval 			}
135305dbe683SOmar Sandoval 
135405dbe683SOmar Sandoval 			dirid = key.offset;
135505dbe683SOmar Sandoval 
135605dbe683SOmar Sandoval 			inode_ref = btrfs_item_ptr(path->nodes[0],
135705dbe683SOmar Sandoval 						   path->slots[0],
135805dbe683SOmar Sandoval 						   struct btrfs_inode_ref);
135905dbe683SOmar Sandoval 			len = btrfs_inode_ref_name_len(path->nodes[0],
136005dbe683SOmar Sandoval 						       inode_ref);
136105dbe683SOmar Sandoval 			ptr -= len + 1;
136205dbe683SOmar Sandoval 			if (ptr < name) {
136305dbe683SOmar Sandoval 				ret = -ENAMETOOLONG;
136405dbe683SOmar Sandoval 				goto err;
136505dbe683SOmar Sandoval 			}
136605dbe683SOmar Sandoval 			read_extent_buffer(path->nodes[0], ptr + 1,
136705dbe683SOmar Sandoval 					   (unsigned long)(inode_ref + 1), len);
136805dbe683SOmar Sandoval 			ptr[0] = '/';
136905dbe683SOmar Sandoval 			btrfs_release_path(path);
137005dbe683SOmar Sandoval 		}
137100246528SJosef Bacik 		btrfs_put_root(fs_root);
13725168489aSJosef Bacik 		fs_root = NULL;
137305dbe683SOmar Sandoval 	}
137405dbe683SOmar Sandoval 
137505dbe683SOmar Sandoval 	btrfs_free_path(path);
137605dbe683SOmar Sandoval 	if (ptr == name + PATH_MAX - 1) {
137705dbe683SOmar Sandoval 		name[0] = '/';
137805dbe683SOmar Sandoval 		name[1] = '\0';
137905dbe683SOmar Sandoval 	} else {
138005dbe683SOmar Sandoval 		memmove(name, ptr, name + PATH_MAX - ptr);
138105dbe683SOmar Sandoval 	}
138205dbe683SOmar Sandoval 	return name;
138305dbe683SOmar Sandoval 
138405dbe683SOmar Sandoval err:
138500246528SJosef Bacik 	btrfs_put_root(fs_root);
138605dbe683SOmar Sandoval 	btrfs_free_path(path);
138705dbe683SOmar Sandoval 	kfree(name);
138805dbe683SOmar Sandoval 	return ERR_PTR(ret);
138905dbe683SOmar Sandoval }
139005dbe683SOmar Sandoval 
139105dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
139205dbe683SOmar Sandoval {
139305dbe683SOmar Sandoval 	struct btrfs_root *root = fs_info->tree_root;
139473f73415SJosef Bacik 	struct btrfs_dir_item *di;
139573f73415SJosef Bacik 	struct btrfs_path *path;
139673f73415SJosef Bacik 	struct btrfs_key location;
139773f73415SJosef Bacik 	u64 dir_id;
139873f73415SJosef Bacik 
139973f73415SJosef Bacik 	path = btrfs_alloc_path();
140073f73415SJosef Bacik 	if (!path)
140105dbe683SOmar Sandoval 		return -ENOMEM;
140273f73415SJosef Bacik 
140373f73415SJosef Bacik 	/*
140473f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
140573f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
140673f73415SJosef Bacik 	 * to mount.
140773f73415SJosef Bacik 	 */
1408815745cfSAl Viro 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
140973f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
1410b0839166SJulia Lawall 	if (IS_ERR(di)) {
1411b0839166SJulia Lawall 		btrfs_free_path(path);
141205dbe683SOmar Sandoval 		return PTR_ERR(di);
1413b0839166SJulia Lawall 	}
141473f73415SJosef Bacik 	if (!di) {
141573f73415SJosef Bacik 		/*
141673f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
141773f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
141805dbe683SOmar Sandoval 		 * mount the top-level subvolume.
141973f73415SJosef Bacik 		 */
142073f73415SJosef Bacik 		btrfs_free_path(path);
142105dbe683SOmar Sandoval 		*objectid = BTRFS_FS_TREE_OBJECTID;
142205dbe683SOmar Sandoval 		return 0;
142373f73415SJosef Bacik 	}
142473f73415SJosef Bacik 
142573f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
142673f73415SJosef Bacik 	btrfs_free_path(path);
142705dbe683SOmar Sandoval 	*objectid = location.objectid;
142805dbe683SOmar Sandoval 	return 0;
142973f73415SJosef Bacik }
143073f73415SJosef Bacik 
14318a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
14328a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
143356e033a7SDavid Sterba 			    void *data)
14342e635a27SChris Mason {
14352e635a27SChris Mason 	struct inode *inode;
1436815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
143739279cc3SChris Mason 	int err;
14382e635a27SChris Mason 
14392e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
14402e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
1441e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
1442af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
1443be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
144414605409SBoris Burkov #ifdef CONFIG_FS_VERITY
144514605409SBoris Burkov 	sb->s_vop = &btrfs_verityops;
144614605409SBoris Burkov #endif
14475103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
14482e635a27SChris Mason 	sb->s_time_gran = 1;
14490eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
14501751e8a6SLinus Torvalds 	sb->s_flags |= SB_POSIXACL;
145149cf6f45SChris Ball #endif
1452357fdad0SMatthew Garrett 	sb->s_flags |= SB_I_VERSION;
1453da2f0f74SChris Mason 	sb->s_iflags |= SB_I_CGROUPWB;
14549e11ceeeSJan Kara 
14559e11ceeeSJan Kara 	err = super_setup_bdi(sb);
14569e11ceeeSJan Kara 	if (err) {
14579e11ceeeSJan Kara 		btrfs_err(fs_info, "super_setup_bdi failed");
14589e11ceeeSJan Kara 		return err;
14599e11ceeeSJan Kara 	}
14609e11ceeeSJan Kara 
1461ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
1462ad2b2c80SAl Viro 	if (err) {
1463ab8d0fc4SJeff Mahoney 		btrfs_err(fs_info, "open_ctree failed");
1464ad2b2c80SAl Viro 		return err;
1465e20d96d6SChris Mason 	}
1466b888db2bSChris Mason 
14670202e83fSDavid Sterba 	inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
14685d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
14695d4f98a2SYan Zheng 		err = PTR_ERR(inode);
147039279cc3SChris Mason 		goto fail_close;
147139279cc3SChris Mason 	}
14722e635a27SChris Mason 
147348fde701SAl Viro 	sb->s_root = d_make_root(inode);
147448fde701SAl Viro 	if (!sb->s_root) {
147539279cc3SChris Mason 		err = -ENOMEM;
147639279cc3SChris Mason 		goto fail_close;
14772e635a27SChris Mason 	}
147858176a96SJosef Bacik 
14791751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
14802e635a27SChris Mason 	return 0;
14812e635a27SChris Mason 
148239279cc3SChris Mason fail_close:
14836bccf3abSJeff Mahoney 	close_ctree(fs_info);
1484d5719762SChris Mason 	return err;
1485d5719762SChris Mason }
1486d5719762SChris Mason 
14876bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
1488d5719762SChris Mason {
1489d5719762SChris Mason 	struct btrfs_trans_handle *trans;
1490815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1491815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
1492df2ce34cSChris Mason 
1493bc074524SJeff Mahoney 	trace_btrfs_sync_fs(fs_info, wait);
14941abe9b8aSliubo 
1495d561c025SChris Mason 	if (!wait) {
1496815745cfSAl Viro 		filemap_flush(fs_info->btree_inode->i_mapping);
1497df2ce34cSChris Mason 		return 0;
1498d561c025SChris Mason 	}
1499771ed689SChris Mason 
15006374e57aSChris Mason 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1501771ed689SChris Mason 
1502d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
150360376ce4SJosef Bacik 	if (IS_ERR(trans)) {
1504354aa0fbSMiao Xie 		/* no transaction, don't bother */
15056b5fe46dSDavid Sterba 		if (PTR_ERR(trans) == -ENOENT) {
15066b5fe46dSDavid Sterba 			/*
15076b5fe46dSDavid Sterba 			 * Exit unless we have some pending changes
15086b5fe46dSDavid Sterba 			 * that need to go through commit
15096b5fe46dSDavid Sterba 			 */
15106b5fe46dSDavid Sterba 			if (fs_info->pending_changes == 0)
1511bd7de2c9SJosef Bacik 				return 0;
1512a53f4f8eSQu Wenruo 			/*
1513a53f4f8eSQu Wenruo 			 * A non-blocking test if the fs is frozen. We must not
1514a53f4f8eSQu Wenruo 			 * start a new transaction here otherwise a deadlock
1515a53f4f8eSQu Wenruo 			 * happens. The pending operations are delayed to the
1516a53f4f8eSQu Wenruo 			 * next commit after thawing.
1517a53f4f8eSQu Wenruo 			 */
1518a7e3c5f2SRakesh Pandit 			if (sb_start_write_trylock(sb))
1519a7e3c5f2SRakesh Pandit 				sb_end_write(sb);
1520a53f4f8eSQu Wenruo 			else
1521a53f4f8eSQu Wenruo 				return 0;
15226b5fe46dSDavid Sterba 			trans = btrfs_start_transaction(root, 0);
152360376ce4SJosef Bacik 		}
152498bd5c54SDavid Sterba 		if (IS_ERR(trans))
152598bd5c54SDavid Sterba 			return PTR_ERR(trans);
15266b5fe46dSDavid Sterba 	}
15273a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
1528d5719762SChris Mason }
1529d5719762SChris Mason 
1530ab0b4a3eSJosef Bacik static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed)
1531ab0b4a3eSJosef Bacik {
1532ab0b4a3eSJosef Bacik 	seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s);
1533ab0b4a3eSJosef Bacik 	*printed = true;
1534ab0b4a3eSJosef Bacik }
1535ab0b4a3eSJosef Bacik 
153634c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1537a9572a15SEric Paris {
1538815745cfSAl Viro 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
15390f628c63SDavid Sterba 	const char *compress_type;
15403ef3959bSJosef Bacik 	const char *subvol_name;
1541ab0b4a3eSJosef Bacik 	bool printed = false;
1542a9572a15SEric Paris 
15433cdde224SJeff Mahoney 	if (btrfs_test_opt(info, DEGRADED))
1544a9572a15SEric Paris 		seq_puts(seq, ",degraded");
15453cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATASUM))
1546a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
15473cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATACOW))
1548a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
15493cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOBARRIER))
1550a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
155195ac567aSFilipe David Borba Manana 	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1552c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",max_inline=%llu", info->max_inline);
1553a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
1554a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
1555f7b885beSAnand Jain 		seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
15563cdde224SJeff Mahoney 	if (btrfs_test_opt(info, COMPRESS)) {
15570f628c63SDavid Sterba 		compress_type = btrfs_compress_type2str(info->compress_type);
15583cdde224SJeff Mahoney 		if (btrfs_test_opt(info, FORCE_COMPRESS))
1559200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
1560200da64eSTsutomu Itoh 		else
1561200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
1562f51d2b59SDavid Sterba 		if (info->compress_level)
1563fa4d885aSAdam Borowski 			seq_printf(seq, ":%d", info->compress_level);
1564200da64eSTsutomu Itoh 	}
15653cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOSSD))
1566c289811cSChris Mason 		seq_puts(seq, ",nossd");
15673cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SSD_SPREAD))
1568451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
15693cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, SSD))
1570a9572a15SEric Paris 		seq_puts(seq, ",ssd");
15713cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOTREELOG))
15726b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
15733cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOLOGREPLAY))
1574ab0b4a3eSJosef Bacik 		print_rescue_option(seq, "nologreplay", &printed);
157568319c18SJosef Bacik 	if (btrfs_test_opt(info, USEBACKUPROOT))
157668319c18SJosef Bacik 		print_rescue_option(seq, "usebackuproot", &printed);
157742437a63SJosef Bacik 	if (btrfs_test_opt(info, IGNOREBADROOTS))
157842437a63SJosef Bacik 		print_rescue_option(seq, "ignorebadroots", &printed);
1579882dbe0cSJosef Bacik 	if (btrfs_test_opt(info, IGNOREDATACSUMS))
1580882dbe0cSJosef Bacik 		print_rescue_option(seq, "ignoredatacsums", &printed);
15813cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FLUSHONCOMMIT))
15826b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
158346b27f50SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_SYNC))
158420a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
1585b0643e59SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_ASYNC))
1586b0643e59SDennis Zhou 		seq_puts(seq, ",discard=async");
15871751e8a6SLinus Torvalds 	if (!(info->sb->s_flags & SB_POSIXACL))
1588a9572a15SEric Paris 		seq_puts(seq, ",noacl");
158904c41559SBoris Burkov 	if (btrfs_free_space_cache_v1_active(info))
1590200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
159104c41559SBoris Burkov 	else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
159270f6d82eSOmar Sandoval 		seq_puts(seq, ",space_cache=v2");
159373bc1876SJosef Bacik 	else
15948965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
15953cdde224SJeff Mahoney 	if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1596f420ee1eSStefan Behrens 		seq_puts(seq, ",rescan_uuid_tree");
15973cdde224SJeff Mahoney 	if (btrfs_test_opt(info, CLEAR_CACHE))
1598200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
15993cdde224SJeff Mahoney 	if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1600200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
16013cdde224SJeff Mahoney 	if (btrfs_test_opt(info, ENOSPC_DEBUG))
16020942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
16033cdde224SJeff Mahoney 	if (btrfs_test_opt(info, AUTO_DEFRAG))
16040942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
16053cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SKIP_BALANCE))
16069555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
16078507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1608cbeaae4fSDavid Sterba 	if (btrfs_test_opt(info, CHECK_INTEGRITY_DATA))
16098507d216SWang Shilong 		seq_puts(seq, ",check_int_data");
16103cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, CHECK_INTEGRITY))
16118507d216SWang Shilong 		seq_puts(seq, ",check_int");
16128507d216SWang Shilong 	if (info->check_integrity_print_mask)
16138507d216SWang Shilong 		seq_printf(seq, ",check_int_print_mask=%d",
16148507d216SWang Shilong 				info->check_integrity_print_mask);
16158507d216SWang Shilong #endif
16168507d216SWang Shilong 	if (info->metadata_ratio)
1617764cb8b4SAnand Jain 		seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
16183cdde224SJeff Mahoney 	if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
16198c342930SJeff Mahoney 		seq_puts(seq, ",fatal_errors=panic");
16208b87dc17SDavid Sterba 	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1621d3740608SAnand Jain 		seq_printf(seq, ",commit=%u", info->commit_interval);
1622d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
16233cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_DATA))
1624d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=data");
16253cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_METADATA))
1626d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=metadata");
1627d0bd4560SJosef Bacik #endif
1628fb592373SJosef Bacik 	if (btrfs_test_opt(info, REF_VERIFY))
1629fb592373SJosef Bacik 		seq_puts(seq, ",ref_verify");
1630c8d3fe02SOmar Sandoval 	seq_printf(seq, ",subvolid=%llu",
1631c8d3fe02SOmar Sandoval 		  BTRFS_I(d_inode(dentry))->root->root_key.objectid);
16323ef3959bSJosef Bacik 	subvol_name = btrfs_get_subvol_name_from_objectid(info,
16333ef3959bSJosef Bacik 			BTRFS_I(d_inode(dentry))->root->root_key.objectid);
16343ef3959bSJosef Bacik 	if (!IS_ERR(subvol_name)) {
1635c8d3fe02SOmar Sandoval 		seq_puts(seq, ",subvol=");
16363ef3959bSJosef Bacik 		seq_escape(seq, subvol_name, " \t\n\\");
16373ef3959bSJosef Bacik 		kfree(subvol_name);
16383ef3959bSJosef Bacik 	}
1639a9572a15SEric Paris 	return 0;
1640a9572a15SEric Paris }
1641a9572a15SEric Paris 
1642a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
16432e635a27SChris Mason {
1644815745cfSAl Viro 	struct btrfs_fs_info *p = data;
1645815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(s);
16464b82d6e4SYan 
1647815745cfSAl Viro 	return fs_info->fs_devices == p->fs_devices;
16484b82d6e4SYan }
16494b82d6e4SYan 
1650450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
1651450ba0eaSJosef Bacik {
16526de1d09dSAl Viro 	int err = set_anon_super(s, data);
16536de1d09dSAl Viro 	if (!err)
1654450ba0eaSJosef Bacik 		s->s_fs_info = data;
16556de1d09dSAl Viro 	return err;
1656450ba0eaSJosef Bacik }
1657450ba0eaSJosef Bacik 
1658830c4adbSJosef Bacik /*
1659f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
1660f9d9ef62SDavid Sterba  */
1661f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
1662f9d9ef62SDavid Sterba {
1663f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1664f9d9ef62SDavid Sterba 		return 1;
1665f9d9ef62SDavid Sterba 	return 0;
1666f9d9ef62SDavid Sterba }
1667f9d9ef62SDavid Sterba 
1668bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1669ae0bc863SAnand Jain 				   struct vfsmount *mnt)
1670830c4adbSJosef Bacik {
1671830c4adbSJosef Bacik 	struct dentry *root;
1672fa330659SOmar Sandoval 	int ret;
1673830c4adbSJosef Bacik 
167405dbe683SOmar Sandoval 	if (!subvol_name) {
167505dbe683SOmar Sandoval 		if (!subvol_objectid) {
167605dbe683SOmar Sandoval 			ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
167705dbe683SOmar Sandoval 							  &subvol_objectid);
167805dbe683SOmar Sandoval 			if (ret) {
167905dbe683SOmar Sandoval 				root = ERR_PTR(ret);
168005dbe683SOmar Sandoval 				goto out;
168105dbe683SOmar Sandoval 			}
168205dbe683SOmar Sandoval 		}
1683c0c907a4SMarcos Paulo de Souza 		subvol_name = btrfs_get_subvol_name_from_objectid(
1684c0c907a4SMarcos Paulo de Souza 					btrfs_sb(mnt->mnt_sb), subvol_objectid);
168505dbe683SOmar Sandoval 		if (IS_ERR(subvol_name)) {
168605dbe683SOmar Sandoval 			root = ERR_CAST(subvol_name);
168705dbe683SOmar Sandoval 			subvol_name = NULL;
168805dbe683SOmar Sandoval 			goto out;
168905dbe683SOmar Sandoval 		}
169005dbe683SOmar Sandoval 
169105dbe683SOmar Sandoval 	}
169205dbe683SOmar Sandoval 
1693ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1694fa330659SOmar Sandoval 	/* mount_subtree() drops our reference on the vfsmount. */
1695fa330659SOmar Sandoval 	mnt = NULL;
1696830c4adbSJosef Bacik 
1697bb289b7bSOmar Sandoval 	if (!IS_ERR(root)) {
1698ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1699ab8d0fc4SJeff Mahoney 		struct btrfs_fs_info *fs_info = btrfs_sb(s);
1700bb289b7bSOmar Sandoval 		struct inode *root_inode = d_inode(root);
1701bb289b7bSOmar Sandoval 		u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1702bb289b7bSOmar Sandoval 
1703bb289b7bSOmar Sandoval 		ret = 0;
1704bb289b7bSOmar Sandoval 		if (!is_subvolume_inode(root_inode)) {
1705ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info, "'%s' is not a valid subvolume",
1706bb289b7bSOmar Sandoval 			       subvol_name);
1707bb289b7bSOmar Sandoval 			ret = -EINVAL;
1708bb289b7bSOmar Sandoval 		}
1709bb289b7bSOmar Sandoval 		if (subvol_objectid && root_objectid != subvol_objectid) {
171005dbe683SOmar Sandoval 			/*
171105dbe683SOmar Sandoval 			 * This will also catch a race condition where a
171205dbe683SOmar Sandoval 			 * subvolume which was passed by ID is renamed and
171305dbe683SOmar Sandoval 			 * another subvolume is renamed over the old location.
171405dbe683SOmar Sandoval 			 */
1715ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info,
1716ab8d0fc4SJeff Mahoney 				  "subvol '%s' does not match subvolid %llu",
1717bb289b7bSOmar Sandoval 				  subvol_name, subvol_objectid);
1718bb289b7bSOmar Sandoval 			ret = -EINVAL;
1719bb289b7bSOmar Sandoval 		}
1720bb289b7bSOmar Sandoval 		if (ret) {
1721ea441d11SAl Viro 			dput(root);
1722bb289b7bSOmar Sandoval 			root = ERR_PTR(ret);
1723ea441d11SAl Viro 			deactivate_locked_super(s);
1724bb289b7bSOmar Sandoval 		}
1725f9d9ef62SDavid Sterba 	}
1726f9d9ef62SDavid Sterba 
1727fa330659SOmar Sandoval out:
1728fa330659SOmar Sandoval 	mntput(mnt);
1729fa330659SOmar Sandoval 	kfree(subvol_name);
1730830c4adbSJosef Bacik 	return root;
1731830c4adbSJosef Bacik }
1732450ba0eaSJosef Bacik 
1733312c89fbSMisono, Tomohiro /*
1734312c89fbSMisono, Tomohiro  * Find a superblock for the given device / mount point.
1735312c89fbSMisono, Tomohiro  *
1736312c89fbSMisono, Tomohiro  * Note: This is based on mount_bdev from fs/super.c with a few additions
1737312c89fbSMisono, Tomohiro  *       for multiple device setup.  Make sure to keep it in sync.
1738312c89fbSMisono, Tomohiro  */
173972fa39f5SMisono, Tomohiro static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
174072fa39f5SMisono, Tomohiro 		int flags, const char *device_name, void *data)
174172fa39f5SMisono, Tomohiro {
174272fa39f5SMisono, Tomohiro 	struct block_device *bdev = NULL;
174372fa39f5SMisono, Tomohiro 	struct super_block *s;
174436350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
174572fa39f5SMisono, Tomohiro 	struct btrfs_fs_devices *fs_devices = NULL;
174672fa39f5SMisono, Tomohiro 	struct btrfs_fs_info *fs_info = NULL;
1747204cc0ccSAl Viro 	void *new_sec_opts = NULL;
174872fa39f5SMisono, Tomohiro 	fmode_t mode = FMODE_READ;
174972fa39f5SMisono, Tomohiro 	int error = 0;
175072fa39f5SMisono, Tomohiro 
175172fa39f5SMisono, Tomohiro 	if (!(flags & SB_RDONLY))
175272fa39f5SMisono, Tomohiro 		mode |= FMODE_WRITE;
175372fa39f5SMisono, Tomohiro 
175472fa39f5SMisono, Tomohiro 	if (data) {
1755a65001e8SAl Viro 		error = security_sb_eat_lsm_opts(data, &new_sec_opts);
175672fa39f5SMisono, Tomohiro 		if (error)
175772fa39f5SMisono, Tomohiro 			return ERR_PTR(error);
175872fa39f5SMisono, Tomohiro 	}
175972fa39f5SMisono, Tomohiro 
176072fa39f5SMisono, Tomohiro 	/*
176172fa39f5SMisono, Tomohiro 	 * Setup a dummy root and fs_info for test/set super.  This is because
176272fa39f5SMisono, Tomohiro 	 * we don't actually fill this stuff out until open_ctree, but we need
17638260edbaSJosef Bacik 	 * then open_ctree will properly initialize the file system specific
17648260edbaSJosef Bacik 	 * settings later.  btrfs_init_fs_info initializes the static elements
17658260edbaSJosef Bacik 	 * of the fs_info (locks and such) to make cleanup easier if we find a
17668260edbaSJosef Bacik 	 * superblock with our given fs_devices later on at sget() time.
176772fa39f5SMisono, Tomohiro 	 */
1768a8fd1f71SJeff Mahoney 	fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
176972fa39f5SMisono, Tomohiro 	if (!fs_info) {
177072fa39f5SMisono, Tomohiro 		error = -ENOMEM;
177172fa39f5SMisono, Tomohiro 		goto error_sec_opts;
177272fa39f5SMisono, Tomohiro 	}
17738260edbaSJosef Bacik 	btrfs_init_fs_info(fs_info);
177472fa39f5SMisono, Tomohiro 
177572fa39f5SMisono, Tomohiro 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
177672fa39f5SMisono, Tomohiro 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
177772fa39f5SMisono, Tomohiro 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
177872fa39f5SMisono, Tomohiro 		error = -ENOMEM;
177972fa39f5SMisono, Tomohiro 		goto error_fs_info;
178072fa39f5SMisono, Tomohiro 	}
178172fa39f5SMisono, Tomohiro 
1782f5194e34SDavid Sterba 	mutex_lock(&uuid_mutex);
1783fa59f27cSAnand Jain 	error = btrfs_parse_device_options(data, mode, fs_type);
178481ffd56bSDavid Sterba 	if (error) {
1785399f7f4cSDavid Sterba 		mutex_unlock(&uuid_mutex);
1786399f7f4cSDavid Sterba 		goto error_fs_info;
178781ffd56bSDavid Sterba 	}
1788399f7f4cSDavid Sterba 
178936350e95SGu Jinxiang 	device = btrfs_scan_one_device(device_name, mode, fs_type);
179036350e95SGu Jinxiang 	if (IS_ERR(device)) {
1791399f7f4cSDavid Sterba 		mutex_unlock(&uuid_mutex);
179236350e95SGu Jinxiang 		error = PTR_ERR(device);
1793399f7f4cSDavid Sterba 		goto error_fs_info;
179481ffd56bSDavid Sterba 	}
1795399f7f4cSDavid Sterba 
179636350e95SGu Jinxiang 	fs_devices = device->fs_devices;
1797399f7f4cSDavid Sterba 	fs_info->fs_devices = fs_devices;
1798399f7f4cSDavid Sterba 
179972fa39f5SMisono, Tomohiro 	error = btrfs_open_devices(fs_devices, mode, fs_type);
1800f5194e34SDavid Sterba 	mutex_unlock(&uuid_mutex);
180172fa39f5SMisono, Tomohiro 	if (error)
180272fa39f5SMisono, Tomohiro 		goto error_fs_info;
180372fa39f5SMisono, Tomohiro 
180472fa39f5SMisono, Tomohiro 	if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
180572fa39f5SMisono, Tomohiro 		error = -EACCES;
180672fa39f5SMisono, Tomohiro 		goto error_close_devices;
180772fa39f5SMisono, Tomohiro 	}
180872fa39f5SMisono, Tomohiro 
1809d24fa5c1SAnand Jain 	bdev = fs_devices->latest_dev->bdev;
181072fa39f5SMisono, Tomohiro 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
181172fa39f5SMisono, Tomohiro 		 fs_info);
181272fa39f5SMisono, Tomohiro 	if (IS_ERR(s)) {
181372fa39f5SMisono, Tomohiro 		error = PTR_ERR(s);
181472fa39f5SMisono, Tomohiro 		goto error_close_devices;
181572fa39f5SMisono, Tomohiro 	}
181672fa39f5SMisono, Tomohiro 
181772fa39f5SMisono, Tomohiro 	if (s->s_root) {
181872fa39f5SMisono, Tomohiro 		btrfs_close_devices(fs_devices);
18190d4b0463SJosef Bacik 		btrfs_free_fs_info(fs_info);
182072fa39f5SMisono, Tomohiro 		if ((flags ^ s->s_flags) & SB_RDONLY)
182172fa39f5SMisono, Tomohiro 			error = -EBUSY;
182272fa39f5SMisono, Tomohiro 	} else {
182372fa39f5SMisono, Tomohiro 		snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
1824e33c267aSRoman Gushchin 		shrinker_debugfs_rename(&s->s_shrink, "sb-%s:%s", fs_type->name,
1825e33c267aSRoman Gushchin 					s->s_id);
182672fa39f5SMisono, Tomohiro 		btrfs_sb(s)->bdev_holder = fs_type;
18279b4e675aSDavid Sterba 		if (!strstr(crc32c_impl(), "generic"))
18289b4e675aSDavid Sterba 			set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
182972fa39f5SMisono, Tomohiro 		error = btrfs_fill_super(s, fs_devices, data);
183072fa39f5SMisono, Tomohiro 	}
1831a65001e8SAl Viro 	if (!error)
1832204cc0ccSAl Viro 		error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL);
1833a65001e8SAl Viro 	security_free_mnt_opts(&new_sec_opts);
183472fa39f5SMisono, Tomohiro 	if (error) {
183572fa39f5SMisono, Tomohiro 		deactivate_locked_super(s);
1836a65001e8SAl Viro 		return ERR_PTR(error);
183772fa39f5SMisono, Tomohiro 	}
183872fa39f5SMisono, Tomohiro 
183972fa39f5SMisono, Tomohiro 	return dget(s->s_root);
184072fa39f5SMisono, Tomohiro 
184172fa39f5SMisono, Tomohiro error_close_devices:
184272fa39f5SMisono, Tomohiro 	btrfs_close_devices(fs_devices);
184372fa39f5SMisono, Tomohiro error_fs_info:
18440d4b0463SJosef Bacik 	btrfs_free_fs_info(fs_info);
184572fa39f5SMisono, Tomohiro error_sec_opts:
184672fa39f5SMisono, Tomohiro 	security_free_mnt_opts(&new_sec_opts);
184772fa39f5SMisono, Tomohiro 	return ERR_PTR(error);
184872fa39f5SMisono, Tomohiro }
1849312c89fbSMisono, Tomohiro 
1850edf24abeSChristoph Hellwig /*
1851312c89fbSMisono, Tomohiro  * Mount function which is called by VFS layer.
1852edf24abeSChristoph Hellwig  *
1853312c89fbSMisono, Tomohiro  * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1854312c89fbSMisono, Tomohiro  * which needs vfsmount* of device's root (/).  This means device's root has to
1855312c89fbSMisono, Tomohiro  * be mounted internally in any case.
1856312c89fbSMisono, Tomohiro  *
1857312c89fbSMisono, Tomohiro  * Operation flow:
1858312c89fbSMisono, Tomohiro  *   1. Parse subvol id related options for later use in mount_subvol().
1859312c89fbSMisono, Tomohiro  *
1860312c89fbSMisono, Tomohiro  *   2. Mount device's root (/) by calling vfs_kern_mount().
1861312c89fbSMisono, Tomohiro  *
1862312c89fbSMisono, Tomohiro  *      NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1863312c89fbSMisono, Tomohiro  *      first place. In order to avoid calling btrfs_mount() again, we use
1864312c89fbSMisono, Tomohiro  *      different file_system_type which is not registered to VFS by
1865312c89fbSMisono, Tomohiro  *      register_filesystem() (btrfs_root_fs_type). As a result,
1866312c89fbSMisono, Tomohiro  *      btrfs_mount_root() is called. The return value will be used by
1867312c89fbSMisono, Tomohiro  *      mount_subtree() in mount_subvol().
1868312c89fbSMisono, Tomohiro  *
1869312c89fbSMisono, Tomohiro  *   3. Call mount_subvol() to get the dentry of subvolume. Since there is
1870312c89fbSMisono, Tomohiro  *      "btrfs subvolume set-default", mount_subvol() is called always.
1871edf24abeSChristoph Hellwig  */
1872061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1873306e16ceSDavid Sterba 		const char *device_name, void *data)
18744b82d6e4SYan {
1875312c89fbSMisono, Tomohiro 	struct vfsmount *mnt_root;
1876312c89fbSMisono, Tomohiro 	struct dentry *root;
187773f73415SJosef Bacik 	char *subvol_name = NULL;
187873f73415SJosef Bacik 	u64 subvol_objectid = 0;
18794b82d6e4SYan 	int error = 0;
18804b82d6e4SYan 
188193b9bcdfSGu Jinxiang 	error = btrfs_parse_subvol_options(data, &subvol_name,
188293b9bcdfSGu Jinxiang 					&subvol_objectid);
1883f23c8af8SIlya Dryomov 	if (error) {
1884f23c8af8SIlya Dryomov 		kfree(subvol_name);
1885061dbc6bSAl Viro 		return ERR_PTR(error);
1886f23c8af8SIlya Dryomov 	}
1887edf24abeSChristoph Hellwig 
1888312c89fbSMisono, Tomohiro 	/* mount device's root (/) */
1889312c89fbSMisono, Tomohiro 	mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data);
1890312c89fbSMisono, Tomohiro 	if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) {
1891312c89fbSMisono, Tomohiro 		if (flags & SB_RDONLY) {
1892312c89fbSMisono, Tomohiro 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1893312c89fbSMisono, Tomohiro 				flags & ~SB_RDONLY, device_name, data);
18944b82d6e4SYan 		} else {
1895312c89fbSMisono, Tomohiro 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1896312c89fbSMisono, Tomohiro 				flags | SB_RDONLY, device_name, data);
1897312c89fbSMisono, Tomohiro 			if (IS_ERR(mnt_root)) {
1898312c89fbSMisono, Tomohiro 				root = ERR_CAST(mnt_root);
1899532b618bSEric W. Biederman 				kfree(subvol_name);
1900312c89fbSMisono, Tomohiro 				goto out;
1901f667aef6SQu Wenruo 			}
1902f667aef6SQu Wenruo 
1903312c89fbSMisono, Tomohiro 			down_write(&mnt_root->mnt_sb->s_umount);
1904312c89fbSMisono, Tomohiro 			error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL);
1905312c89fbSMisono, Tomohiro 			up_write(&mnt_root->mnt_sb->s_umount);
1906312c89fbSMisono, Tomohiro 			if (error < 0) {
1907312c89fbSMisono, Tomohiro 				root = ERR_PTR(error);
1908312c89fbSMisono, Tomohiro 				mntput(mnt_root);
1909532b618bSEric W. Biederman 				kfree(subvol_name);
1910312c89fbSMisono, Tomohiro 				goto out;
1911312c89fbSMisono, Tomohiro 			}
1912312c89fbSMisono, Tomohiro 		}
1913312c89fbSMisono, Tomohiro 	}
1914312c89fbSMisono, Tomohiro 	if (IS_ERR(mnt_root)) {
1915312c89fbSMisono, Tomohiro 		root = ERR_CAST(mnt_root);
1916532b618bSEric W. Biederman 		kfree(subvol_name);
1917312c89fbSMisono, Tomohiro 		goto out;
1918f667aef6SQu Wenruo 	}
19194b82d6e4SYan 
1920312c89fbSMisono, Tomohiro 	/* mount_subvol() will free subvol_name and mnt_root */
1921ae0bc863SAnand Jain 	root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
19224b82d6e4SYan 
1923312c89fbSMisono, Tomohiro out:
1924312c89fbSMisono, Tomohiro 	return root;
19254b82d6e4SYan }
19262e635a27SChris Mason 
19270d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1928f7b885beSAnand Jain 				     u32 new_pool_size, u32 old_pool_size)
19290d2450abSSergei Trofimovich {
19300d2450abSSergei Trofimovich 	if (new_pool_size == old_pool_size)
19310d2450abSSergei Trofimovich 		return;
19320d2450abSSergei Trofimovich 
19330d2450abSSergei Trofimovich 	fs_info->thread_pool_size = new_pool_size;
19340d2450abSSergei Trofimovich 
1935efe120a0SFrank Holton 	btrfs_info(fs_info, "resize thread pool %d -> %d",
19360d2450abSSergei Trofimovich 	       old_pool_size, new_pool_size);
19370d2450abSSergei Trofimovich 
19385cdc7ad3SQu Wenruo 	btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1939a31b4a43SChristoph Hellwig 	btrfs_workqueue_set_max(fs_info->hipri_workers, new_pool_size);
1940afe3d242SQu Wenruo 	btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1941e66f0bb1SQu Wenruo 	btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1942fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1943fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
19445b3bc44eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
19450d2450abSSergei Trofimovich }
19460d2450abSSergei Trofimovich 
1947f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1948f42a34b2SMiao Xie 				       unsigned long old_opts, int flags)
1949f42a34b2SMiao Xie {
1950dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1951dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
19521751e8a6SLinus Torvalds 	     (flags & SB_RDONLY))) {
1953dc81cdc5SMiao Xie 		/* wait for any defraggers to finish */
1954dc81cdc5SMiao Xie 		wait_event(fs_info->transaction_wait,
1955dc81cdc5SMiao Xie 			   (atomic_read(&fs_info->defrag_running) == 0));
19561751e8a6SLinus Torvalds 		if (flags & SB_RDONLY)
1957dc81cdc5SMiao Xie 			sync_filesystem(fs_info->sb);
1958dc81cdc5SMiao Xie 	}
1959dc81cdc5SMiao Xie }
1960dc81cdc5SMiao Xie 
1961dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1962dc81cdc5SMiao Xie 					 unsigned long old_opts)
1963dc81cdc5SMiao Xie {
196494846229SBoris Burkov 	const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
196594846229SBoris Burkov 
1966dc81cdc5SMiao Xie 	/*
1967180e4d47SLuis de Bethencourt 	 * We need to cleanup all defragable inodes if the autodefragment is
1968180e4d47SLuis de Bethencourt 	 * close or the filesystem is read only.
1969dc81cdc5SMiao Xie 	 */
1970dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1971bc98a42cSDavid Howells 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1972dc81cdc5SMiao Xie 		btrfs_cleanup_defrag_inodes(fs_info);
1973dc81cdc5SMiao Xie 	}
1974dc81cdc5SMiao Xie 
1975b0643e59SDennis Zhou 	/* If we toggled discard async */
1976b0643e59SDennis Zhou 	if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1977b0643e59SDennis Zhou 	    btrfs_test_opt(fs_info, DISCARD_ASYNC))
1978b0643e59SDennis Zhou 		btrfs_discard_resume(fs_info);
1979b0643e59SDennis Zhou 	else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1980b0643e59SDennis Zhou 		 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1981b0643e59SDennis Zhou 		btrfs_discard_cleanup(fs_info);
198294846229SBoris Burkov 
198394846229SBoris Burkov 	/* If we toggled space cache */
198494846229SBoris Burkov 	if (cache_opt != btrfs_free_space_cache_v1_active(fs_info))
198594846229SBoris Burkov 		btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
1986dc81cdc5SMiao Xie }
1987dc81cdc5SMiao Xie 
1988c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1989c146afadSYan Zheng {
1990815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
199149b25e05SJeff Mahoney 	unsigned old_flags = sb->s_flags;
199249b25e05SJeff Mahoney 	unsigned long old_opts = fs_info->mount_opt;
199349b25e05SJeff Mahoney 	unsigned long old_compress_type = fs_info->compress_type;
199449b25e05SJeff Mahoney 	u64 old_max_inline = fs_info->max_inline;
1995f7b885beSAnand Jain 	u32 old_thread_pool_size = fs_info->thread_pool_size;
1996d612ac59SAnand Jain 	u32 old_metadata_ratio = fs_info->metadata_ratio;
1997c146afadSYan Zheng 	int ret;
1998c146afadSYan Zheng 
199902b9984dSTheodore Ts'o 	sync_filesystem(sb);
200088c4703fSJohannes Thumshirn 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
2001dc81cdc5SMiao Xie 
2002f667aef6SQu Wenruo 	if (data) {
2003204cc0ccSAl Viro 		void *new_sec_opts = NULL;
2004f667aef6SQu Wenruo 
2005a65001e8SAl Viro 		ret = security_sb_eat_lsm_opts(data, &new_sec_opts);
2006a65001e8SAl Viro 		if (!ret)
2007204cc0ccSAl Viro 			ret = security_sb_remount(sb, new_sec_opts);
2008a65001e8SAl Viro 		security_free_mnt_opts(&new_sec_opts);
2009f667aef6SQu Wenruo 		if (ret)
2010f667aef6SQu Wenruo 			goto restore;
2011f667aef6SQu Wenruo 	}
2012f667aef6SQu Wenruo 
20132ff7e61eSJeff Mahoney 	ret = btrfs_parse_options(fs_info, data, *flags);
2014891f41cbSChengguang Xu 	if (ret)
201549b25e05SJeff Mahoney 		goto restore;
2016b288052eSChris Mason 
2017d7f67ac9SQu Wenruo 	ret = btrfs_check_features(fs_info, sb);
2018d7f67ac9SQu Wenruo 	if (ret < 0)
20190591f040SQu Wenruo 		goto restore;
2020d7f67ac9SQu Wenruo 
2021f42a34b2SMiao Xie 	btrfs_remount_begin(fs_info, old_opts, *flags);
20220d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
20230d2450abSSergei Trofimovich 		fs_info->thread_pool_size, old_thread_pool_size);
20240d2450abSSergei Trofimovich 
2025c55a4319SBoris Burkov 	if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) !=
2026c55a4319SBoris Burkov 	    (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
20272838d255SBoris Burkov 	    (!sb_rdonly(sb) || (*flags & SB_RDONLY))) {
20282838d255SBoris Burkov 		btrfs_warn(fs_info,
20292838d255SBoris Burkov 		"remount supports changing free space tree only from ro to rw");
20302838d255SBoris Burkov 		/* Make sure free space cache options match the state on disk */
20312838d255SBoris Burkov 		if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
20322838d255SBoris Burkov 			btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
20332838d255SBoris Burkov 			btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
20342838d255SBoris Burkov 		}
20352838d255SBoris Burkov 		if (btrfs_free_space_cache_v1_active(fs_info)) {
20362838d255SBoris Burkov 			btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
20372838d255SBoris Burkov 			btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
20382838d255SBoris Burkov 		}
20392838d255SBoris Burkov 	}
20402838d255SBoris Burkov 
20411751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
2042dc81cdc5SMiao Xie 		goto out;
2043c146afadSYan Zheng 
20441751e8a6SLinus Torvalds 	if (*flags & SB_RDONLY) {
20458dabb742SStefan Behrens 		/*
20468dabb742SStefan Behrens 		 * this also happens on 'umount -rf' or on shutdown, when
20478dabb742SStefan Behrens 		 * the filesystem is busy.
20488dabb742SStefan Behrens 		 */
204921c7e756SMiao Xie 		cancel_work_sync(&fs_info->async_reclaim_work);
205057056740SJosef Bacik 		cancel_work_sync(&fs_info->async_data_reclaim_work);
2051361c093dSStefan Behrens 
2052b0643e59SDennis Zhou 		btrfs_discard_cleanup(fs_info);
2053b0643e59SDennis Zhou 
2054361c093dSStefan Behrens 		/* wait for the uuid_scan task to finish */
2055361c093dSStefan Behrens 		down(&fs_info->uuid_tree_rescan_sem);
2056361c093dSStefan Behrens 		/* avoid complains from lockdep et al. */
2057361c093dSStefan Behrens 		up(&fs_info->uuid_tree_rescan_sem);
2058361c093dSStefan Behrens 
2059a0a1db70SFilipe Manana 		btrfs_set_sb_rdonly(sb);
2060c146afadSYan Zheng 
2061e44163e1SJeff Mahoney 		/*
20621751e8a6SLinus Torvalds 		 * Setting SB_RDONLY will put the cleaner thread to
2063e44163e1SJeff Mahoney 		 * sleep at the next loop if it's already active.
2064e44163e1SJeff Mahoney 		 * If it's already asleep, we'll leave unused block
2065e44163e1SJeff Mahoney 		 * groups on disk until we're mounted read-write again
2066e44163e1SJeff Mahoney 		 * unless we clean them up here.
2067e44163e1SJeff Mahoney 		 */
2068e44163e1SJeff Mahoney 		btrfs_delete_unused_bgs(fs_info);
2069e44163e1SJeff Mahoney 
2070a0a1db70SFilipe Manana 		/*
2071a0a1db70SFilipe Manana 		 * The cleaner task could be already running before we set the
2072a0a1db70SFilipe Manana 		 * flag BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock).
2073a0a1db70SFilipe Manana 		 * We must make sure that after we finish the remount, i.e. after
2074a0a1db70SFilipe Manana 		 * we call btrfs_commit_super(), the cleaner can no longer start
2075a0a1db70SFilipe Manana 		 * a transaction - either because it was dropping a dead root,
2076a0a1db70SFilipe Manana 		 * running delayed iputs or deleting an unused block group (the
2077a0a1db70SFilipe Manana 		 * cleaner picked a block group from the list of unused block
2078a0a1db70SFilipe Manana 		 * groups before we were able to in the previous call to
2079a0a1db70SFilipe Manana 		 * btrfs_delete_unused_bgs()).
2080a0a1db70SFilipe Manana 		 */
2081a0a1db70SFilipe Manana 		wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING,
2082a0a1db70SFilipe Manana 			    TASK_UNINTERRUPTIBLE);
2083a0a1db70SFilipe Manana 
2084a8cc263eSFilipe Manana 		/*
2085a8cc263eSFilipe Manana 		 * We've set the superblock to RO mode, so we might have made
2086a8cc263eSFilipe Manana 		 * the cleaner task sleep without running all pending delayed
2087a8cc263eSFilipe Manana 		 * iputs. Go through all the delayed iputs here, so that if an
2088a8cc263eSFilipe Manana 		 * unmount happens without remounting RW we don't end up at
2089a8cc263eSFilipe Manana 		 * finishing close_ctree() with a non-empty list of delayed
2090a8cc263eSFilipe Manana 		 * iputs.
2091a8cc263eSFilipe Manana 		 */
2092a8cc263eSFilipe Manana 		btrfs_run_delayed_iputs(fs_info);
2093a8cc263eSFilipe Manana 
20948dabb742SStefan Behrens 		btrfs_dev_replace_suspend_for_unmount(fs_info);
20958dabb742SStefan Behrens 		btrfs_scrub_cancel(fs_info);
2096061594efSMiao Xie 		btrfs_pause_balance(fs_info);
20978dabb742SStefan Behrens 
2098cb13eea3SFilipe Manana 		/*
2099cb13eea3SFilipe Manana 		 * Pause the qgroup rescan worker if it is running. We don't want
2100cb13eea3SFilipe Manana 		 * it to be still running after we are in RO mode, as after that,
2101cb13eea3SFilipe Manana 		 * by the time we unmount, it might have left a transaction open,
2102cb13eea3SFilipe Manana 		 * so we would leak the transaction and/or crash.
2103cb13eea3SFilipe Manana 		 */
2104cb13eea3SFilipe Manana 		btrfs_qgroup_wait_for_completion(fs_info, false);
2105cb13eea3SFilipe Manana 
21066bccf3abSJeff Mahoney 		ret = btrfs_commit_super(fs_info);
210749b25e05SJeff Mahoney 		if (ret)
210849b25e05SJeff Mahoney 			goto restore;
2109c146afadSYan Zheng 	} else {
211084961539SJosef Bacik 		if (BTRFS_FS_ERROR(fs_info)) {
21116ef3de9cSDavid Sterba 			btrfs_err(fs_info,
2112efe120a0SFrank Holton 				"Remounting read-write after error is not allowed");
21136ef3de9cSDavid Sterba 			ret = -EINVAL;
21146ef3de9cSDavid Sterba 			goto restore;
21156ef3de9cSDavid Sterba 		}
21168a3db184SSergei Trofimovich 		if (fs_info->fs_devices->rw_devices == 0) {
211749b25e05SJeff Mahoney 			ret = -EACCES;
211849b25e05SJeff Mahoney 			goto restore;
21198a3db184SSergei Trofimovich 		}
21202b82032cSYan Zheng 
21216528b99dSAnand Jain 		if (!btrfs_check_rw_degradable(fs_info, NULL)) {
2122efe120a0SFrank Holton 			btrfs_warn(fs_info,
212352042d8eSAndrea Gelmini 		"too many missing devices, writable remount is not allowed");
2124292fd7fcSStefan Behrens 			ret = -EACCES;
2125292fd7fcSStefan Behrens 			goto restore;
2126292fd7fcSStefan Behrens 		}
2127292fd7fcSStefan Behrens 
21288a3db184SSergei Trofimovich 		if (btrfs_super_log_root(fs_info->super_copy) != 0) {
212910a3a3edSDavid Sterba 			btrfs_warn(fs_info,
213010a3a3edSDavid Sterba 		"mount required to replay tree-log, cannot remount read-write");
213149b25e05SJeff Mahoney 			ret = -EINVAL;
213249b25e05SJeff Mahoney 			goto restore;
21338a3db184SSergei Trofimovich 		}
2134c146afadSYan Zheng 
213544c0ca21SBoris Burkov 		/*
213644c0ca21SBoris Burkov 		 * NOTE: when remounting with a change that does writes, don't
213744c0ca21SBoris Burkov 		 * put it anywhere above this point, as we are not sure to be
213844c0ca21SBoris Burkov 		 * safe to write until we pass the above checks.
213944c0ca21SBoris Burkov 		 */
214044c0ca21SBoris Burkov 		ret = btrfs_start_pre_rw_mount(fs_info);
214149b25e05SJeff Mahoney 		if (ret)
214249b25e05SJeff Mahoney 			goto restore;
2143c146afadSYan Zheng 
2144a0a1db70SFilipe Manana 		btrfs_clear_sb_rdonly(sb);
214590c711abSZygo Blaxell 
2146afcdd129SJosef Bacik 		set_bit(BTRFS_FS_OPEN, &fs_info->flags);
2147c146afadSYan Zheng 	}
2148dc81cdc5SMiao Xie out:
2149faa00889SJosef Bacik 	/*
2150faa00889SJosef Bacik 	 * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS,
2151faa00889SJosef Bacik 	 * since the absence of the flag means it can be toggled off by remount.
2152faa00889SJosef Bacik 	 */
2153faa00889SJosef Bacik 	*flags |= SB_I_VERSION;
2154faa00889SJosef Bacik 
21552c6a92b0SJustin Maggard 	wake_up_process(fs_info->transaction_kthread);
2156dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
21578cd29088SBoris Burkov 	btrfs_clear_oneshot_options(fs_info);
215888c4703fSJohannes Thumshirn 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
215988c4703fSJohannes Thumshirn 
2160c146afadSYan Zheng 	return 0;
216149b25e05SJeff Mahoney 
216249b25e05SJeff Mahoney restore:
21631751e8a6SLinus Torvalds 	/* We've hit an error - don't reset SB_RDONLY */
2164bc98a42cSDavid Howells 	if (sb_rdonly(sb))
21651751e8a6SLinus Torvalds 		old_flags |= SB_RDONLY;
2166a0a1db70SFilipe Manana 	if (!(old_flags & SB_RDONLY))
2167a0a1db70SFilipe Manana 		clear_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state);
216849b25e05SJeff Mahoney 	sb->s_flags = old_flags;
216949b25e05SJeff Mahoney 	fs_info->mount_opt = old_opts;
217049b25e05SJeff Mahoney 	fs_info->compress_type = old_compress_type;
217149b25e05SJeff Mahoney 	fs_info->max_inline = old_max_inline;
21720d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
21730d2450abSSergei Trofimovich 		old_thread_pool_size, fs_info->thread_pool_size);
217449b25e05SJeff Mahoney 	fs_info->metadata_ratio = old_metadata_ratio;
2175dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
217688c4703fSJohannes Thumshirn 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
217788c4703fSJohannes Thumshirn 
217849b25e05SJeff Mahoney 	return ret;
2179c146afadSYan Zheng }
2180c146afadSYan Zheng 
2181bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
2182214cc184SDavid Sterba static int btrfs_cmp_device_free_bytes(const void *a, const void *b)
2183bcd53741SArne Jansen {
2184214cc184SDavid Sterba 	const struct btrfs_device_info *dev_info1 = a;
2185214cc184SDavid Sterba 	const struct btrfs_device_info *dev_info2 = b;
2186214cc184SDavid Sterba 
2187214cc184SDavid Sterba 	if (dev_info1->max_avail > dev_info2->max_avail)
2188bcd53741SArne Jansen 		return -1;
2189214cc184SDavid Sterba 	else if (dev_info1->max_avail < dev_info2->max_avail)
2190bcd53741SArne Jansen 		return 1;
2191bcd53741SArne Jansen 	return 0;
2192bcd53741SArne Jansen }
2193bcd53741SArne Jansen 
2194bcd53741SArne Jansen /*
2195bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
2196bcd53741SArne Jansen  * is stored.(Descending Sort)
2197bcd53741SArne Jansen  */
2198bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
2199bcd53741SArne Jansen 					struct btrfs_device_info *devices,
2200bcd53741SArne Jansen 					size_t nr_devices)
2201bcd53741SArne Jansen {
2202bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
2203bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
2204bcd53741SArne Jansen }
2205bcd53741SArne Jansen 
22066d07bcecSMiao Xie /*
22076d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
22086d07bcecSMiao Xie  * file data.
22096d07bcecSMiao Xie  */
22107e17916bSArnd Bergmann static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
22116bccf3abSJeff Mahoney 					      u64 *free_bytes)
22126d07bcecSMiao Xie {
22136d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
22146d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
22156d07bcecSMiao Xie 	struct btrfs_device *device;
22166d07bcecSMiao Xie 	u64 type;
22176d07bcecSMiao Xie 	u64 avail_space;
22186d07bcecSMiao Xie 	u64 min_stripe_size;
2219559ca6eaSNikolay Borisov 	int num_stripes = 1;
22206d07bcecSMiao Xie 	int i = 0, nr_devices;
22214f080f57SDavid Sterba 	const struct btrfs_raid_attr *rattr;
22226d07bcecSMiao Xie 
22237e33fd99SJosef Bacik 	/*
222401327610SNicholas D Steeves 	 * We aren't under the device list lock, so this is racy-ish, but good
22257e33fd99SJosef Bacik 	 * enough for our purposes.
22267e33fd99SJosef Bacik 	 */
2227b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
22287e33fd99SJosef Bacik 	if (!nr_devices) {
22297e33fd99SJosef Bacik 		smp_mb();
22307e33fd99SJosef Bacik 		nr_devices = fs_info->fs_devices->open_devices;
22317e33fd99SJosef Bacik 		ASSERT(nr_devices);
22327e33fd99SJosef Bacik 		if (!nr_devices) {
22337e33fd99SJosef Bacik 			*free_bytes = 0;
22347e33fd99SJosef Bacik 			return 0;
22357e33fd99SJosef Bacik 		}
22367e33fd99SJosef Bacik 	}
22376d07bcecSMiao Xie 
2238d9b0d9baSDulshani Gunawardhana 	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
22396a44517dSDavid Sterba 			       GFP_KERNEL);
22406d07bcecSMiao Xie 	if (!devices_info)
22416d07bcecSMiao Xie 		return -ENOMEM;
22426d07bcecSMiao Xie 
224301327610SNicholas D Steeves 	/* calc min stripe number for data space allocation */
22441b86826dSJeff Mahoney 	type = btrfs_data_alloc_profile(fs_info);
22454f080f57SDavid Sterba 	rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
22464f080f57SDavid Sterba 
2247e1ea2beeSDavid Sterba 	if (type & BTRFS_BLOCK_GROUP_RAID0)
224839fb26c3SMiao Xie 		num_stripes = nr_devices;
2249d09cb9e1SDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1_MASK)
2250d09cb9e1SDavid Sterba 		num_stripes = rattr->ncopies;
2251e1ea2beeSDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID10)
225239fb26c3SMiao Xie 		num_stripes = 4;
22536d07bcecSMiao Xie 
22544f080f57SDavid Sterba 	/* Adjust for more than 1 stripe per device */
22554f080f57SDavid Sterba 	min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
22566d07bcecSMiao Xie 
22577e33fd99SJosef Bacik 	rcu_read_lock();
22587e33fd99SJosef Bacik 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
2259e12c9621SAnand Jain 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2260e12c9621SAnand Jain 						&device->dev_state) ||
2261401e29c1SAnand Jain 		    !device->bdev ||
2262401e29c1SAnand Jain 		    test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
22636d07bcecSMiao Xie 			continue;
22646d07bcecSMiao Xie 
22657e33fd99SJosef Bacik 		if (i >= nr_devices)
22667e33fd99SJosef Bacik 			break;
22677e33fd99SJosef Bacik 
22686d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
22696d07bcecSMiao Xie 
22706d07bcecSMiao Xie 		/* align with stripe_len */
2271559ca6eaSNikolay Borisov 		avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
22726d07bcecSMiao Xie 
22736d07bcecSMiao Xie 		/*
227437f85ec3SQu Wenruo 		 * Ensure we have at least min_stripe_size on top of the
227537f85ec3SQu Wenruo 		 * reserved space on the device.
22766d07bcecSMiao Xie 		 */
227737f85ec3SQu Wenruo 		if (avail_space <= BTRFS_DEVICE_RANGE_RESERVED + min_stripe_size)
22786d07bcecSMiao Xie 			continue;
22796d07bcecSMiao Xie 
228037f85ec3SQu Wenruo 		avail_space -= BTRFS_DEVICE_RANGE_RESERVED;
2281559ca6eaSNikolay Borisov 
22826d07bcecSMiao Xie 		devices_info[i].dev = device;
22836d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
22846d07bcecSMiao Xie 
22856d07bcecSMiao Xie 		i++;
22866d07bcecSMiao Xie 	}
22877e33fd99SJosef Bacik 	rcu_read_unlock();
22886d07bcecSMiao Xie 
22896d07bcecSMiao Xie 	nr_devices = i;
22906d07bcecSMiao Xie 
22916d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
22926d07bcecSMiao Xie 
22936d07bcecSMiao Xie 	i = nr_devices - 1;
22946d07bcecSMiao Xie 	avail_space = 0;
2295559ca6eaSNikolay Borisov 	while (nr_devices >= rattr->devs_min) {
2296559ca6eaSNikolay Borisov 		num_stripes = min(num_stripes, nr_devices);
229739fb26c3SMiao Xie 
22986d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
22996d07bcecSMiao Xie 			int j;
23006d07bcecSMiao Xie 			u64 alloc_size;
23016d07bcecSMiao Xie 
230239fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
23036d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
230439fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
23056d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
23066d07bcecSMiao Xie 		}
23076d07bcecSMiao Xie 		i--;
23086d07bcecSMiao Xie 		nr_devices--;
23096d07bcecSMiao Xie 	}
23106d07bcecSMiao Xie 
23116d07bcecSMiao Xie 	kfree(devices_info);
23126d07bcecSMiao Xie 	*free_bytes = avail_space;
23136d07bcecSMiao Xie 	return 0;
23146d07bcecSMiao Xie }
23156d07bcecSMiao Xie 
2316ba7b6e62SDavid Sterba /*
2317ba7b6e62SDavid Sterba  * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2318ba7b6e62SDavid Sterba  *
2319ba7b6e62SDavid Sterba  * If there's a redundant raid level at DATA block groups, use the respective
2320ba7b6e62SDavid Sterba  * multiplier to scale the sizes.
2321ba7b6e62SDavid Sterba  *
2322ba7b6e62SDavid Sterba  * Unused device space usage is based on simulating the chunk allocator
23230d0c71b3SDavid Sterba  * algorithm that respects the device sizes and order of allocations.  This is
23240d0c71b3SDavid Sterba  * a close approximation of the actual use but there are other factors that may
23250d0c71b3SDavid Sterba  * change the result (like a new metadata chunk).
2326ba7b6e62SDavid Sterba  *
2327ca8a51b3SDavid Sterba  * If metadata is exhausted, f_bavail will be 0.
2328ba7b6e62SDavid Sterba  */
23298fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
23308fd17795SChris Mason {
2331815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
2332815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
2333bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
2334bd4d1088SJosef Bacik 	u64 total_used = 0;
23356d07bcecSMiao Xie 	u64 total_free_data = 0;
2336ca8a51b3SDavid Sterba 	u64 total_free_meta = 0;
2337265fdfa6SDavid Sterba 	u32 bits = fs_info->sectorsize_bits;
2338de37aa51SNikolay Borisov 	__be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
2339ba7b6e62SDavid Sterba 	unsigned factor = 1;
2340ba7b6e62SDavid Sterba 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
23416d07bcecSMiao Xie 	int ret;
2342ca8a51b3SDavid Sterba 	u64 thresh = 0;
2343ae02d1bdSLuis de Bethencourt 	int mixed = 0;
23448fd17795SChris Mason 
234572804905SJosef Bacik 	list_for_each_entry(found, &fs_info->space_info, list) {
23466d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
2347ba7b6e62SDavid Sterba 			int i;
2348ba7b6e62SDavid Sterba 
23496d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
23506d07bcecSMiao Xie 			total_free_data -=
23516d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
2352ba7b6e62SDavid Sterba 
2353ba7b6e62SDavid Sterba 			for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
235446df06b8SDavid Sterba 				if (!list_empty(&found->block_groups[i]))
235546df06b8SDavid Sterba 					factor = btrfs_bg_type_to_factor(
235646df06b8SDavid Sterba 						btrfs_raid_array[i].bg_flag);
2357ba7b6e62SDavid Sterba 			}
23586d07bcecSMiao Xie 		}
2359ae02d1bdSLuis de Bethencourt 
2360ae02d1bdSLuis de Bethencourt 		/*
2361ae02d1bdSLuis de Bethencourt 		 * Metadata in mixed block goup profiles are accounted in data
2362ae02d1bdSLuis de Bethencourt 		 */
2363ae02d1bdSLuis de Bethencourt 		if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
2364ae02d1bdSLuis de Bethencourt 			if (found->flags & BTRFS_BLOCK_GROUP_DATA)
2365ae02d1bdSLuis de Bethencourt 				mixed = 1;
2366ae02d1bdSLuis de Bethencourt 			else
2367ae02d1bdSLuis de Bethencourt 				total_free_meta += found->disk_total -
2368ae02d1bdSLuis de Bethencourt 					found->disk_used;
2369ae02d1bdSLuis de Bethencourt 		}
23706d07bcecSMiao Xie 
2371b742bb82SYan, Zheng 		total_used += found->disk_used;
237289a55897SJosef Bacik 	}
2373ba7b6e62SDavid Sterba 
2374ba7b6e62SDavid Sterba 	buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
2375ba7b6e62SDavid Sterba 	buf->f_blocks >>= bits;
2376ba7b6e62SDavid Sterba 	buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
2377ba7b6e62SDavid Sterba 
2378ba7b6e62SDavid Sterba 	/* Account global block reserve as used, it's in logical size already */
2379ba7b6e62SDavid Sterba 	spin_lock(&block_rsv->lock);
238041b34accSLuis de Bethencourt 	/* Mixed block groups accounting is not byte-accurate, avoid overflow */
238141b34accSLuis de Bethencourt 	if (buf->f_bfree >= block_rsv->size >> bits)
2382ba7b6e62SDavid Sterba 		buf->f_bfree -= block_rsv->size >> bits;
238341b34accSLuis de Bethencourt 	else
238441b34accSLuis de Bethencourt 		buf->f_bfree = 0;
2385ba7b6e62SDavid Sterba 	spin_unlock(&block_rsv->lock);
2386ba7b6e62SDavid Sterba 
23870d95c1beSDavid Sterba 	buf->f_bavail = div_u64(total_free_data, factor);
23886bccf3abSJeff Mahoney 	ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
23897e33fd99SJosef Bacik 	if (ret)
23906d07bcecSMiao Xie 		return ret;
2391ba7b6e62SDavid Sterba 	buf->f_bavail += div_u64(total_free_data, factor);
23926d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
2393d397712bSChris Mason 
2394ca8a51b3SDavid Sterba 	/*
2395ca8a51b3SDavid Sterba 	 * We calculate the remaining metadata space minus global reserve. If
2396ca8a51b3SDavid Sterba 	 * this is (supposedly) smaller than zero, there's no space. But this
2397ca8a51b3SDavid Sterba 	 * does not hold in practice, the exhausted state happens where's still
2398ca8a51b3SDavid Sterba 	 * some positive delta. So we apply some guesswork and compare the
2399ca8a51b3SDavid Sterba 	 * delta to a 4M threshold.  (Practically observed delta was ~2M.)
2400ca8a51b3SDavid Sterba 	 *
2401ca8a51b3SDavid Sterba 	 * We probably cannot calculate the exact threshold value because this
2402ca8a51b3SDavid Sterba 	 * depends on the internal reservations requested by various
2403ca8a51b3SDavid Sterba 	 * operations, so some operations that consume a few metadata will
2404ca8a51b3SDavid Sterba 	 * succeed even if the Avail is zero. But this is better than the other
2405ca8a51b3SDavid Sterba 	 * way around.
2406ca8a51b3SDavid Sterba 	 */
2407d4417e22SNikolay Borisov 	thresh = SZ_4M;
2408ca8a51b3SDavid Sterba 
2409d55966c4SJosef Bacik 	/*
2410d55966c4SJosef Bacik 	 * We only want to claim there's no available space if we can no longer
2411d55966c4SJosef Bacik 	 * allocate chunks for our metadata profile and our global reserve will
2412d55966c4SJosef Bacik 	 * not fit in the free metadata space.  If we aren't ->full then we
2413d55966c4SJosef Bacik 	 * still can allocate chunks and thus are fine using the currently
2414d55966c4SJosef Bacik 	 * calculated f_bavail.
2415d55966c4SJosef Bacik 	 */
2416d55966c4SJosef Bacik 	if (!mixed && block_rsv->space_info->full &&
2417d55966c4SJosef Bacik 	    total_free_meta - thresh < block_rsv->size)
2418ca8a51b3SDavid Sterba 		buf->f_bavail = 0;
2419ca8a51b3SDavid Sterba 
2420ba7b6e62SDavid Sterba 	buf->f_type = BTRFS_SUPER_MAGIC;
2421ba7b6e62SDavid Sterba 	buf->f_bsize = dentry->d_sb->s_blocksize;
2422ba7b6e62SDavid Sterba 	buf->f_namelen = BTRFS_NAME_LEN;
2423ba7b6e62SDavid Sterba 
24249d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
24259d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
24269d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
24279d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
24289d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
242932d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
24304fd786e6SMisono Tomohiro 	buf->f_fsid.val[0] ^=
24314fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32;
24324fd786e6SMisono Tomohiro 	buf->f_fsid.val[1] ^=
24334fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid;
243432d48fa1SDavid Woodhouse 
24358fd17795SChris Mason 	return 0;
24368fd17795SChris Mason }
2437b5133862SChris Mason 
2438aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
2439aea52e19SAl Viro {
2440815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2441aea52e19SAl Viro 	kill_anon_super(sb);
24420d4b0463SJosef Bacik 	btrfs_free_fs_info(fs_info);
2443aea52e19SAl Viro }
2444aea52e19SAl Viro 
24452e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
24462e635a27SChris Mason 	.owner		= THIS_MODULE,
24472e635a27SChris Mason 	.name		= "btrfs",
2448061dbc6bSAl Viro 	.mount		= btrfs_mount,
2449aea52e19SAl Viro 	.kill_sb	= btrfs_kill_super,
2450f667aef6SQu Wenruo 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
24512e635a27SChris Mason };
245272fa39f5SMisono, Tomohiro 
245372fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type = {
245472fa39f5SMisono, Tomohiro 	.owner		= THIS_MODULE,
245572fa39f5SMisono, Tomohiro 	.name		= "btrfs",
245672fa39f5SMisono, Tomohiro 	.mount		= btrfs_mount_root,
245772fa39f5SMisono, Tomohiro 	.kill_sb	= btrfs_kill_super,
24585b9b26f5SChristian Brauner 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP,
245972fa39f5SMisono, Tomohiro };
246072fa39f5SMisono, Tomohiro 
24617f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs");
2462a9218f6bSChris Mason 
2463d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file)
2464d8620958STom Van Braeckel {
2465d8620958STom Van Braeckel 	/*
2466d8620958STom Van Braeckel 	 * The control file's private_data is used to hold the
2467d8620958STom Van Braeckel 	 * transaction when it is started and is used to keep
2468d8620958STom Van Braeckel 	 * track of whether a transaction is already in progress.
2469d8620958STom Van Braeckel 	 */
2470d8620958STom Van Braeckel 	file->private_data = NULL;
2471d8620958STom Van Braeckel 	return 0;
2472d8620958STom Van Braeckel }
2473d8620958STom Van Braeckel 
2474d352ac68SChris Mason /*
2475cfe953c8SSu Yue  * Used by /dev/btrfs-control for devices ioctls.
2476d352ac68SChris Mason  */
24778a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
24788a4b83ccSChris Mason 				unsigned long arg)
24798a4b83ccSChris Mason {
24808a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
248136350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
248216cab91aSAnand Jain 	dev_t devt = 0;
2483c071fcfdSChris Mason 	int ret = -ENOTTY;
24848a4b83ccSChris Mason 
2485e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2486e441d54dSChris Mason 		return -EPERM;
2487e441d54dSChris Mason 
2488dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
2489dae7b665SLi Zefan 	if (IS_ERR(vol))
2490dae7b665SLi Zefan 		return PTR_ERR(vol);
2491f505754fSFilipe Manana 	vol->name[BTRFS_PATH_NAME_MAX] = '\0';
2492c071fcfdSChris Mason 
24938a4b83ccSChris Mason 	switch (cmd) {
24948a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
2495899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
249636350e95SGu Jinxiang 		device = btrfs_scan_one_device(vol->name, FMODE_READ,
249736350e95SGu Jinxiang 					       &btrfs_root_fs_type);
249836350e95SGu Jinxiang 		ret = PTR_ERR_OR_ZERO(device);
2499899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
25008a4b83ccSChris Mason 		break;
2501228a73abSAnand Jain 	case BTRFS_IOC_FORGET_DEV:
250216cab91aSAnand Jain 		if (vol->name[0] != 0) {
250316cab91aSAnand Jain 			ret = lookup_bdev(vol->name, &devt);
250416cab91aSAnand Jain 			if (ret)
250516cab91aSAnand Jain 				break;
250616cab91aSAnand Jain 		}
250716cab91aSAnand Jain 		ret = btrfs_forget_devices(devt);
2508228a73abSAnand Jain 		break;
250902db0844SJosef Bacik 	case BTRFS_IOC_DEVICES_READY:
2510899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
251136350e95SGu Jinxiang 		device = btrfs_scan_one_device(vol->name, FMODE_READ,
251236350e95SGu Jinxiang 					       &btrfs_root_fs_type);
251336350e95SGu Jinxiang 		if (IS_ERR(device)) {
2514899f9307SDavid Sterba 			mutex_unlock(&uuid_mutex);
251536350e95SGu Jinxiang 			ret = PTR_ERR(device);
251602db0844SJosef Bacik 			break;
2517899f9307SDavid Sterba 		}
251836350e95SGu Jinxiang 		ret = !(device->fs_devices->num_devices ==
251936350e95SGu Jinxiang 			device->fs_devices->total_devices);
2520899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
252102db0844SJosef Bacik 		break;
2522c5868f83SDavid Sterba 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2523d5131b65SDavid Sterba 		ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2524c5868f83SDavid Sterba 		break;
25258a4b83ccSChris Mason 	}
2526dae7b665SLi Zefan 
25278a4b83ccSChris Mason 	kfree(vol);
2528f819d837SLinda Knippers 	return ret;
25298a4b83ccSChris Mason }
25308a4b83ccSChris Mason 
25310176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
2532ed0dab6bSYan {
2533354aa0fbSMiao Xie 	struct btrfs_trans_handle *trans;
25340b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
25350b246afaSJeff Mahoney 	struct btrfs_root *root = fs_info->tree_root;
2536354aa0fbSMiao Xie 
2537fac03c8dSDavid Sterba 	set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
25389e7cc91aSWang Xiaoguang 	/*
25399e7cc91aSWang Xiaoguang 	 * We don't need a barrier here, we'll wait for any transaction that
25409e7cc91aSWang Xiaoguang 	 * could be in progress on other threads (and do delayed iputs that
25419e7cc91aSWang Xiaoguang 	 * we want to avoid on a frozen filesystem), or do the commit
25429e7cc91aSWang Xiaoguang 	 * ourselves.
25439e7cc91aSWang Xiaoguang 	 */
2544d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
2545354aa0fbSMiao Xie 	if (IS_ERR(trans)) {
2546354aa0fbSMiao Xie 		/* no transaction, don't bother */
2547354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
25480176260fSLinus Torvalds 			return 0;
2549354aa0fbSMiao Xie 		return PTR_ERR(trans);
2550354aa0fbSMiao Xie 	}
25513a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
2552ed0dab6bSYan }
2553ed0dab6bSYan 
2554a05d3c91SQu Wenruo static int check_dev_super(struct btrfs_device *dev)
2555a05d3c91SQu Wenruo {
2556a05d3c91SQu Wenruo 	struct btrfs_fs_info *fs_info = dev->fs_info;
2557a05d3c91SQu Wenruo 	struct btrfs_super_block *sb;
25583d17adeaSQu Wenruo 	u16 csum_type;
2559a05d3c91SQu Wenruo 	int ret = 0;
2560a05d3c91SQu Wenruo 
2561a05d3c91SQu Wenruo 	/* This should be called with fs still frozen. */
2562a05d3c91SQu Wenruo 	ASSERT(test_bit(BTRFS_FS_FROZEN, &fs_info->flags));
2563a05d3c91SQu Wenruo 
2564a05d3c91SQu Wenruo 	/* Missing dev, no need to check. */
2565a05d3c91SQu Wenruo 	if (!dev->bdev)
2566a05d3c91SQu Wenruo 		return 0;
2567a05d3c91SQu Wenruo 
2568a05d3c91SQu Wenruo 	/* Only need to check the primary super block. */
2569a05d3c91SQu Wenruo 	sb = btrfs_read_dev_one_super(dev->bdev, 0, true);
2570a05d3c91SQu Wenruo 	if (IS_ERR(sb))
2571a05d3c91SQu Wenruo 		return PTR_ERR(sb);
2572a05d3c91SQu Wenruo 
25733d17adeaSQu Wenruo 	/* Verify the checksum. */
25743d17adeaSQu Wenruo 	csum_type = btrfs_super_csum_type(sb);
25753d17adeaSQu Wenruo 	if (csum_type != btrfs_super_csum_type(fs_info->super_copy)) {
25763d17adeaSQu Wenruo 		btrfs_err(fs_info, "csum type changed, has %u expect %u",
25773d17adeaSQu Wenruo 			  csum_type, btrfs_super_csum_type(fs_info->super_copy));
25783d17adeaSQu Wenruo 		ret = -EUCLEAN;
25793d17adeaSQu Wenruo 		goto out;
25803d17adeaSQu Wenruo 	}
25813d17adeaSQu Wenruo 
25823d17adeaSQu Wenruo 	if (btrfs_check_super_csum(fs_info, sb)) {
25833d17adeaSQu Wenruo 		btrfs_err(fs_info, "csum for on-disk super block no longer matches");
25843d17adeaSQu Wenruo 		ret = -EUCLEAN;
25853d17adeaSQu Wenruo 		goto out;
25863d17adeaSQu Wenruo 	}
25873d17adeaSQu Wenruo 
2588a05d3c91SQu Wenruo 	/* Btrfs_validate_super() includes fsid check against super->fsid. */
2589a05d3c91SQu Wenruo 	ret = btrfs_validate_super(fs_info, sb, 0);
2590a05d3c91SQu Wenruo 	if (ret < 0)
2591a05d3c91SQu Wenruo 		goto out;
2592a05d3c91SQu Wenruo 
2593a05d3c91SQu Wenruo 	if (btrfs_super_generation(sb) != fs_info->last_trans_committed) {
2594a05d3c91SQu Wenruo 		btrfs_err(fs_info, "transid mismatch, has %llu expect %llu",
2595a05d3c91SQu Wenruo 			btrfs_super_generation(sb),
2596a05d3c91SQu Wenruo 			fs_info->last_trans_committed);
2597a05d3c91SQu Wenruo 		ret = -EUCLEAN;
2598a05d3c91SQu Wenruo 		goto out;
2599a05d3c91SQu Wenruo 	}
2600a05d3c91SQu Wenruo out:
2601a05d3c91SQu Wenruo 	btrfs_release_disk_super(sb);
2602a05d3c91SQu Wenruo 	return ret;
2603a05d3c91SQu Wenruo }
2604a05d3c91SQu Wenruo 
26059e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb)
26069e7cc91aSWang Xiaoguang {
2607fac03c8dSDavid Sterba 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2608a05d3c91SQu Wenruo 	struct btrfs_device *device;
2609a05d3c91SQu Wenruo 	int ret = 0;
2610fac03c8dSDavid Sterba 
2611a05d3c91SQu Wenruo 	/*
2612a05d3c91SQu Wenruo 	 * Make sure the fs is not changed by accident (like hibernation then
2613a05d3c91SQu Wenruo 	 * modified by other OS).
2614a05d3c91SQu Wenruo 	 * If we found anything wrong, we mark the fs error immediately.
2615a05d3c91SQu Wenruo 	 *
2616a05d3c91SQu Wenruo 	 * And since the fs is frozen, no one can modify the fs yet, thus
2617a05d3c91SQu Wenruo 	 * we don't need to hold device_list_mutex.
2618a05d3c91SQu Wenruo 	 */
2619a05d3c91SQu Wenruo 	list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
2620a05d3c91SQu Wenruo 		ret = check_dev_super(device);
2621a05d3c91SQu Wenruo 		if (ret < 0) {
2622a05d3c91SQu Wenruo 			btrfs_handle_fs_error(fs_info, ret,
2623a05d3c91SQu Wenruo 				"super block on devid %llu got modified unexpectedly",
2624a05d3c91SQu Wenruo 				device->devid);
2625a05d3c91SQu Wenruo 			break;
2626a05d3c91SQu Wenruo 		}
2627a05d3c91SQu Wenruo 	}
2628fac03c8dSDavid Sterba 	clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
2629a05d3c91SQu Wenruo 
2630a05d3c91SQu Wenruo 	/*
2631a05d3c91SQu Wenruo 	 * We still return 0, to allow VFS layer to unfreeze the fs even the
2632a05d3c91SQu Wenruo 	 * above checks failed. Since the fs is either fine or read-only, we're
2633a05d3c91SQu Wenruo 	 * safe to continue, without causing further damage.
2634a05d3c91SQu Wenruo 	 */
26359e7cc91aSWang Xiaoguang 	return 0;
26369e7cc91aSWang Xiaoguang }
26379e7cc91aSWang Xiaoguang 
26389c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
26399c5085c1SJosef Bacik {
26409c5085c1SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
26419c5085c1SJosef Bacik 
264288c14590SDavid Sterba 	/*
26436605fd2fSAnand Jain 	 * There should be always a valid pointer in latest_dev, it may be stale
26446605fd2fSAnand Jain 	 * for a short moment in case it's being deleted but still valid until
26456605fd2fSAnand Jain 	 * the end of RCU grace period.
264688c14590SDavid Sterba 	 */
264788c14590SDavid Sterba 	rcu_read_lock();
26486605fd2fSAnand Jain 	seq_escape(m, rcu_str_deref(fs_info->fs_devices->latest_dev->name), " \t\n\\");
264988c14590SDavid Sterba 	rcu_read_unlock();
26506605fd2fSAnand Jain 
26519c5085c1SJosef Bacik 	return 0;
26529c5085c1SJosef Bacik }
26539c5085c1SJosef Bacik 
2654b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
265576dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
2656bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
2657e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
2658d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
2659a9572a15SEric Paris 	.show_options	= btrfs_show_options,
26609c5085c1SJosef Bacik 	.show_devname	= btrfs_show_devname,
26612c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
26622c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
266326602cabSAl Viro 	.free_inode	= btrfs_free_inode,
26648fd17795SChris Mason 	.statfs		= btrfs_statfs,
2665c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
26660176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
26679e7cc91aSWang Xiaoguang 	.unfreeze_fs	= btrfs_unfreeze,
2668e20d96d6SChris Mason };
2669a9218f6bSChris Mason 
2670a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
2671d8620958STom Van Braeckel 	.open = btrfs_control_open,
2672a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
26731832f2d8SArnd Bergmann 	.compat_ioctl = compat_ptr_ioctl,
2674a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
26756038f373SArnd Bergmann 	.llseek = noop_llseek,
2676a9218f6bSChris Mason };
2677a9218f6bSChris Mason 
2678a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
2679578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
2680a9218f6bSChris Mason 	.name		= "btrfs-control",
2681a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
2682a9218f6bSChris Mason };
2683a9218f6bSChris Mason 
2684578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2685578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
2686578454ffSKay Sievers 
2687f5c29bd9SLiu Bo static int __init btrfs_interface_init(void)
2688a9218f6bSChris Mason {
2689a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
2690a9218f6bSChris Mason }
2691a9218f6bSChris Mason 
2692e67c718bSDavid Sterba static __cold void btrfs_interface_exit(void)
2693a9218f6bSChris Mason {
2694f368ed60SGreg Kroah-Hartman 	misc_deregister(&btrfs_misc);
2695a9218f6bSChris Mason }
2696a9218f6bSChris Mason 
2697*5565b8e0SQu Wenruo static int __init btrfs_print_mod_info(void)
269885965600SDavid Sterba {
2699edf57cbfSBart Van Assche 	static const char options[] = ""
270085965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
270185965600SDavid Sterba 			", debug=on"
270285965600SDavid Sterba #endif
270379556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT
270479556c3dSStefan Behrens 			", assert=on"
270579556c3dSStefan Behrens #endif
270685965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
270785965600SDavid Sterba 			", integrity-checker=on"
270885965600SDavid Sterba #endif
2709fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2710fb592373SJosef Bacik 			", ref-verify=on"
2711fb592373SJosef Bacik #endif
27125b316468SNaohiro Aota #ifdef CONFIG_BLK_DEV_ZONED
27135b316468SNaohiro Aota 			", zoned=yes"
27145b316468SNaohiro Aota #else
27155b316468SNaohiro Aota 			", zoned=no"
27165b316468SNaohiro Aota #endif
2717ea3dc7d2SDavid Sterba #ifdef CONFIG_FS_VERITY
2718ea3dc7d2SDavid Sterba 			", fsverity=yes"
2719ea3dc7d2SDavid Sterba #else
2720ea3dc7d2SDavid Sterba 			", fsverity=no"
2721ea3dc7d2SDavid Sterba #endif
2722edf57cbfSBart Van Assche 			;
2723edf57cbfSBart Van Assche 	pr_info("Btrfs loaded, crc32c=%s%s\n", crc32c_impl(), options);
2724*5565b8e0SQu Wenruo 	return 0;
2725*5565b8e0SQu Wenruo }
2726*5565b8e0SQu Wenruo 
2727*5565b8e0SQu Wenruo static int register_btrfs(void)
2728*5565b8e0SQu Wenruo {
2729*5565b8e0SQu Wenruo 	return register_filesystem(&btrfs_fs_type);
2730*5565b8e0SQu Wenruo }
2731*5565b8e0SQu Wenruo 
2732*5565b8e0SQu Wenruo static void unregister_btrfs(void)
2733*5565b8e0SQu Wenruo {
2734*5565b8e0SQu Wenruo 	unregister_filesystem(&btrfs_fs_type);
2735*5565b8e0SQu Wenruo }
2736*5565b8e0SQu Wenruo 
2737*5565b8e0SQu Wenruo /* Helper structure for long init/exit functions. */
2738*5565b8e0SQu Wenruo struct init_sequence {
2739*5565b8e0SQu Wenruo 	int (*init_func)(void);
2740*5565b8e0SQu Wenruo 	/* Can be NULL if the init_func doesn't need cleanup. */
2741*5565b8e0SQu Wenruo 	void (*exit_func)(void);
2742*5565b8e0SQu Wenruo };
2743*5565b8e0SQu Wenruo 
2744*5565b8e0SQu Wenruo static const struct init_sequence mod_init_seq[] = {
2745*5565b8e0SQu Wenruo 	{
2746*5565b8e0SQu Wenruo 		.init_func = btrfs_props_init,
2747*5565b8e0SQu Wenruo 		.exit_func = NULL,
2748*5565b8e0SQu Wenruo 	}, {
2749*5565b8e0SQu Wenruo 		.init_func = btrfs_init_sysfs,
2750*5565b8e0SQu Wenruo 		.exit_func = btrfs_exit_sysfs,
2751*5565b8e0SQu Wenruo 	}, {
2752*5565b8e0SQu Wenruo 		.init_func = btrfs_init_compress,
2753*5565b8e0SQu Wenruo 		.exit_func = btrfs_exit_compress,
2754*5565b8e0SQu Wenruo 	}, {
2755*5565b8e0SQu Wenruo 		.init_func = btrfs_init_cachep,
2756*5565b8e0SQu Wenruo 		.exit_func = btrfs_destroy_cachep,
2757*5565b8e0SQu Wenruo 	}, {
2758*5565b8e0SQu Wenruo 		.init_func = btrfs_transaction_init,
2759*5565b8e0SQu Wenruo 		.exit_func = btrfs_transaction_exit,
2760*5565b8e0SQu Wenruo 	}, {
2761*5565b8e0SQu Wenruo 		.init_func = btrfs_ctree_init,
2762*5565b8e0SQu Wenruo 		.exit_func = btrfs_ctree_exit,
2763*5565b8e0SQu Wenruo 	}, {
2764*5565b8e0SQu Wenruo 		.init_func = btrfs_free_space_init,
2765*5565b8e0SQu Wenruo 		.exit_func = btrfs_free_space_exit,
2766*5565b8e0SQu Wenruo 	}, {
2767*5565b8e0SQu Wenruo 		.init_func = extent_state_init_cachep,
2768*5565b8e0SQu Wenruo 		.exit_func = extent_state_free_cachep,
2769*5565b8e0SQu Wenruo 	}, {
2770*5565b8e0SQu Wenruo 		.init_func = extent_buffer_init_cachep,
2771*5565b8e0SQu Wenruo 		.exit_func = extent_buffer_free_cachep,
2772*5565b8e0SQu Wenruo 	}, {
2773*5565b8e0SQu Wenruo 		.init_func = btrfs_bioset_init,
2774*5565b8e0SQu Wenruo 		.exit_func = btrfs_bioset_exit,
2775*5565b8e0SQu Wenruo 	}, {
2776*5565b8e0SQu Wenruo 		.init_func = extent_map_init,
2777*5565b8e0SQu Wenruo 		.exit_func = extent_map_exit,
2778*5565b8e0SQu Wenruo 	}, {
2779*5565b8e0SQu Wenruo 		.init_func = ordered_data_init,
2780*5565b8e0SQu Wenruo 		.exit_func = ordered_data_exit,
2781*5565b8e0SQu Wenruo 	}, {
2782*5565b8e0SQu Wenruo 		.init_func = btrfs_delayed_inode_init,
2783*5565b8e0SQu Wenruo 		.exit_func = btrfs_delayed_inode_exit,
2784*5565b8e0SQu Wenruo 	}, {
2785*5565b8e0SQu Wenruo 		.init_func = btrfs_auto_defrag_init,
2786*5565b8e0SQu Wenruo 		.exit_func = btrfs_auto_defrag_exit,
2787*5565b8e0SQu Wenruo 	}, {
2788*5565b8e0SQu Wenruo 		.init_func = btrfs_delayed_ref_init,
2789*5565b8e0SQu Wenruo 		.exit_func = btrfs_delayed_ref_exit,
2790*5565b8e0SQu Wenruo 	}, {
2791*5565b8e0SQu Wenruo 		.init_func = btrfs_prelim_ref_init,
2792*5565b8e0SQu Wenruo 		.exit_func = btrfs_prelim_ref_exit,
2793*5565b8e0SQu Wenruo 	}, {
2794*5565b8e0SQu Wenruo 		.init_func = btrfs_interface_init,
2795*5565b8e0SQu Wenruo 		.exit_func = btrfs_interface_exit,
2796*5565b8e0SQu Wenruo 	}, {
2797*5565b8e0SQu Wenruo 		.init_func = btrfs_print_mod_info,
2798*5565b8e0SQu Wenruo 		.exit_func = NULL,
2799*5565b8e0SQu Wenruo 	}, {
2800*5565b8e0SQu Wenruo 		.init_func = btrfs_run_sanity_tests,
2801*5565b8e0SQu Wenruo 		.exit_func = NULL,
2802*5565b8e0SQu Wenruo 	}, {
2803*5565b8e0SQu Wenruo 		.init_func = register_btrfs,
2804*5565b8e0SQu Wenruo 		.exit_func = unregister_btrfs,
2805*5565b8e0SQu Wenruo 	}
2806*5565b8e0SQu Wenruo };
2807*5565b8e0SQu Wenruo 
2808*5565b8e0SQu Wenruo static bool mod_init_result[ARRAY_SIZE(mod_init_seq)];
2809*5565b8e0SQu Wenruo 
2810*5565b8e0SQu Wenruo static void __exit exit_btrfs_fs(void)
2811*5565b8e0SQu Wenruo {
2812*5565b8e0SQu Wenruo 	int i;
2813*5565b8e0SQu Wenruo 
2814*5565b8e0SQu Wenruo 	for (i = ARRAY_SIZE(mod_init_seq) - 1; i >= 0; i--) {
2815*5565b8e0SQu Wenruo 		if (!mod_init_result[i])
2816*5565b8e0SQu Wenruo 			continue;
2817*5565b8e0SQu Wenruo 		if (mod_init_seq[i].exit_func)
2818*5565b8e0SQu Wenruo 			mod_init_seq[i].exit_func();
2819*5565b8e0SQu Wenruo 		mod_init_result[i] = false;
2820*5565b8e0SQu Wenruo 	}
282185965600SDavid Sterba }
282285965600SDavid Sterba 
28232e635a27SChris Mason static int __init init_btrfs_fs(void)
28242e635a27SChris Mason {
2825*5565b8e0SQu Wenruo 	int ret;
2826*5565b8e0SQu Wenruo 	int i;
282758176a96SJosef Bacik 
2828*5565b8e0SQu Wenruo 	for (i = 0; i < ARRAY_SIZE(mod_init_seq); i++) {
2829*5565b8e0SQu Wenruo 		ASSERT(!mod_init_result[i]);
2830*5565b8e0SQu Wenruo 		ret = mod_init_seq[i].init_func();
2831*5565b8e0SQu Wenruo 		if (ret < 0)
2832*5565b8e0SQu Wenruo 			goto error;
2833*5565b8e0SQu Wenruo 		mod_init_result[i] = true;
2834*5565b8e0SQu Wenruo 	}
28352f4cbe64SWyatt Banks 	return 0;
28362f4cbe64SWyatt Banks 
2837*5565b8e0SQu Wenruo error:
2838*5565b8e0SQu Wenruo 	/*
2839*5565b8e0SQu Wenruo 	 * If we call exit_btrfs_fs() it would cause section mismatch.
2840*5565b8e0SQu Wenruo 	 * As init_btrfs_fs() belongs to .init.text, while exit_btrfs_fs()
2841*5565b8e0SQu Wenruo 	 * belongs to .exit.text.
2842*5565b8e0SQu Wenruo 	 */
2843*5565b8e0SQu Wenruo 	for (i = ARRAY_SIZE(mod_init_seq) - 1; i >= 0; i--) {
2844*5565b8e0SQu Wenruo 		if (!mod_init_result[i])
2845*5565b8e0SQu Wenruo 			continue;
2846*5565b8e0SQu Wenruo 		if (mod_init_seq[i].exit_func)
2847*5565b8e0SQu Wenruo 			mod_init_seq[i].exit_func();
2848*5565b8e0SQu Wenruo 		mod_init_result[i] = false;
28492e635a27SChris Mason 	}
2850*5565b8e0SQu Wenruo 	return ret;
28512e635a27SChris Mason }
28522e635a27SChris Mason 
285360efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs);
28542e635a27SChris Mason module_exit(exit_btrfs_fs)
28552e635a27SChris Mason 
28562e635a27SChris Mason MODULE_LICENSE("GPL");
2857d5178578SJohannes Thumshirn MODULE_SOFTDEP("pre: crc32c");
28583951e7f0SJohannes Thumshirn MODULE_SOFTDEP("pre: xxhash64");
28593831bf00SJohannes Thumshirn MODULE_SOFTDEP("pre: sha256");
2860352ae07bSDavid Sterba MODULE_SOFTDEP("pre: blake2b-256");
2861