xref: /linux/fs/btrfs/super.c (revision 3619c94f073e4e96bef4cc15e70adbc36f3cb203)
1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
26cbd5570SChris Mason /*
36cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
46cbd5570SChris Mason  */
56cbd5570SChris Mason 
64b82d6e4SYan #include <linux/blkdev.h>
72e635a27SChris Mason #include <linux/module.h>
82e635a27SChris Mason #include <linux/fs.h>
92e635a27SChris Mason #include <linux/pagemap.h>
102e635a27SChris Mason #include <linux/highmem.h>
112e635a27SChris Mason #include <linux/time.h>
122e635a27SChris Mason #include <linux/init.h>
13a9572a15SEric Paris #include <linux/seq_file.h>
142e635a27SChris Mason #include <linux/string.h>
152e635a27SChris Mason #include <linux/backing-dev.h>
164b82d6e4SYan #include <linux/mount.h>
1775dfe396SChris Mason #include <linux/writeback.h>
188fd17795SChris Mason #include <linux/statfs.h>
1908607c1bSChris Mason #include <linux/compat.h>
2095e05289SChris Mason #include <linux/parser.h>
21c59f8951SChris Mason #include <linux/ctype.h>
226da6abaeSChris Mason #include <linux/namei.h>
23a9218f6bSChris Mason #include <linux/miscdevice.h>
241bcbf313SQinghuang Feng #include <linux/magic.h>
255a0e3ad6STejun Heo #include <linux/slab.h>
2690a887c9SDan Magenheimer #include <linux/cleancache.h>
2722c44fe6SJosef Bacik #include <linux/ratelimit.h>
289678c543SNikolay Borisov #include <linux/crc32c.h>
2955e301fdSFilipe Brandenburger #include <linux/btrfs.h>
3016cdcec7SMiao Xie #include "delayed-inode.h"
312e635a27SChris Mason #include "ctree.h"
32e20d96d6SChris Mason #include "disk-io.h"
33d5719762SChris Mason #include "transaction.h"
342c90e5d6SChris Mason #include "btrfs_inode.h"
353a686375SChris Mason #include "print-tree.h"
3663541927SFilipe David Borba Manana #include "props.h"
375103e947SJosef Bacik #include "xattr.h"
388a4b83ccSChris Mason #include "volumes.h"
39be6e8dc0SBalaji Rao #include "export.h"
40c8b97818SChris Mason #include "compression.h"
419c5085c1SJosef Bacik #include "rcu-string.h"
428dabb742SStefan Behrens #include "dev-replace.h"
4374255aa0SJosef Bacik #include "free-space-cache.h"
44b9e9a6cbSWang Shilong #include "backref.h"
458719aaaeSJosef Bacik #include "space-info.h"
4689439109SDavid Sterba #include "sysfs.h"
47dc11dd5dSJosef Bacik #include "tests/btrfs-tests.h"
48aac0023cSJosef Bacik #include "block-group.h"
49b0643e59SDennis Zhou #include "discard.h"
502e635a27SChris Mason 
51d3982100SMark Fasheh #include "qgroup.h"
521abe9b8aSliubo #define CREATE_TRACE_POINTS
531abe9b8aSliubo #include <trace/events/btrfs.h>
541abe9b8aSliubo 
55b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
5672fa39f5SMisono, Tomohiro 
5772fa39f5SMisono, Tomohiro /*
5872fa39f5SMisono, Tomohiro  * Types for mounting the default subvolume and a subvolume explicitly
5972fa39f5SMisono, Tomohiro  * requested by subvol=/path. That way the callchain is straightforward and we
6072fa39f5SMisono, Tomohiro  * don't have to play tricks with the mount options and recursive calls to
6172fa39f5SMisono, Tomohiro  * btrfs_mount.
62312c89fbSMisono, Tomohiro  *
63312c89fbSMisono, Tomohiro  * The new btrfs_root_fs_type also servers as a tag for the bdev_holder.
6472fa39f5SMisono, Tomohiro  */
65830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type;
6672fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type;
67e20d96d6SChris Mason 
680723a047SHarald Hoyer static int btrfs_remount(struct super_block *sb, int *flags, char *data);
690723a047SHarald Hoyer 
704143cb8bSDavid Sterba const char * __attribute_const__ btrfs_decode_error(int errno)
71acce952bSliubo {
7208748810SDavid Sterba 	char *errstr = "unknown";
73acce952bSliubo 
74acce952bSliubo 	switch (errno) {
75acce952bSliubo 	case -EIO:
76acce952bSliubo 		errstr = "IO failure";
77acce952bSliubo 		break;
78acce952bSliubo 	case -ENOMEM:
79acce952bSliubo 		errstr = "Out of memory";
80acce952bSliubo 		break;
81acce952bSliubo 	case -EROFS:
82acce952bSliubo 		errstr = "Readonly filesystem";
83acce952bSliubo 		break;
848c342930SJeff Mahoney 	case -EEXIST:
858c342930SJeff Mahoney 		errstr = "Object already exists";
868c342930SJeff Mahoney 		break;
8794ef7280SDavid Sterba 	case -ENOSPC:
8894ef7280SDavid Sterba 		errstr = "No space left";
8994ef7280SDavid Sterba 		break;
9094ef7280SDavid Sterba 	case -ENOENT:
9194ef7280SDavid Sterba 		errstr = "No such entry";
9294ef7280SDavid Sterba 		break;
93acce952bSliubo 	}
94acce952bSliubo 
95acce952bSliubo 	return errstr;
96acce952bSliubo }
97acce952bSliubo 
98acce952bSliubo /*
9934d97007SAnand Jain  * __btrfs_handle_fs_error decodes expected errors from the caller and
10052042d8eSAndrea Gelmini  * invokes the appropriate error response.
101acce952bSliubo  */
102c0d19e2bSDavid Sterba __cold
10334d97007SAnand Jain void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
1044da35113SJeff Mahoney 		       unsigned int line, int errno, const char *fmt, ...)
105acce952bSliubo {
106acce952bSliubo 	struct super_block *sb = fs_info->sb;
10757d816a1SAnand Jain #ifdef CONFIG_PRINTK
108acce952bSliubo 	const char *errstr;
10957d816a1SAnand Jain #endif
110acce952bSliubo 
111acce952bSliubo 	/*
112acce952bSliubo 	 * Special case: if the error is EROFS, and we're already
1131751e8a6SLinus Torvalds 	 * under SB_RDONLY, then it is safe here.
114acce952bSliubo 	 */
115bc98a42cSDavid Howells 	if (errno == -EROFS && sb_rdonly(sb))
116acce952bSliubo   		return;
117acce952bSliubo 
11857d816a1SAnand Jain #ifdef CONFIG_PRINTK
11908748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
1204da35113SJeff Mahoney 	if (fmt) {
12137252a66SEric Sandeen 		struct va_format vaf;
12237252a66SEric Sandeen 		va_list args;
12337252a66SEric Sandeen 
12437252a66SEric Sandeen 		va_start(args, fmt);
12537252a66SEric Sandeen 		vaf.fmt = fmt;
12637252a66SEric Sandeen 		vaf.va = &args;
1274da35113SJeff Mahoney 
12862e85577SJeff Mahoney 		pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n",
12908748810SDavid Sterba 			sb->s_id, function, line, errno, errstr, &vaf);
13037252a66SEric Sandeen 		va_end(args);
1314da35113SJeff Mahoney 	} else {
13262e85577SJeff Mahoney 		pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s\n",
13308748810SDavid Sterba 			sb->s_id, function, line, errno, errstr);
1344da35113SJeff Mahoney 	}
13557d816a1SAnand Jain #endif
136acce952bSliubo 
1370713d90cSAnand Jain 	/*
1380713d90cSAnand Jain 	 * Today we only save the error info to memory.  Long term we'll
1390713d90cSAnand Jain 	 * also send it down to the disk
1400713d90cSAnand Jain 	 */
1410713d90cSAnand Jain 	set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
1420713d90cSAnand Jain 
1434da35113SJeff Mahoney 	/* Don't go through full error handling during mount */
144922ea899SAnand Jain 	if (!(sb->s_flags & SB_BORN))
145922ea899SAnand Jain 		return;
146922ea899SAnand Jain 
147922ea899SAnand Jain 	if (sb_rdonly(sb))
148922ea899SAnand Jain 		return;
149922ea899SAnand Jain 
150b0643e59SDennis Zhou 	btrfs_discard_stop(fs_info);
151b0643e59SDennis Zhou 
152922ea899SAnand Jain 	/* btrfs handle error by forcing the filesystem readonly */
153922ea899SAnand Jain 	sb->s_flags |= SB_RDONLY;
154922ea899SAnand Jain 	btrfs_info(fs_info, "forced readonly");
155922ea899SAnand Jain 	/*
156922ea899SAnand Jain 	 * Note that a running device replace operation is not canceled here
157922ea899SAnand Jain 	 * although there is no way to update the progress. It would add the
158922ea899SAnand Jain 	 * risk of a deadlock, therefore the canceling is omitted. The only
159922ea899SAnand Jain 	 * penalty is that some I/O remains active until the procedure
16052042d8eSAndrea Gelmini 	 * completes. The next time when the filesystem is mounted writable
161922ea899SAnand Jain 	 * again, the device replace operation continues.
162922ea899SAnand Jain 	 */
163acce952bSliubo }
1644da35113SJeff Mahoney 
16557d816a1SAnand Jain #ifdef CONFIG_PRINTK
166533574c6SJoe Perches static const char * const logtypes[] = {
1674da35113SJeff Mahoney 	"emergency",
1684da35113SJeff Mahoney 	"alert",
1694da35113SJeff Mahoney 	"critical",
1704da35113SJeff Mahoney 	"error",
1714da35113SJeff Mahoney 	"warning",
1724da35113SJeff Mahoney 	"notice",
1734da35113SJeff Mahoney 	"info",
1744da35113SJeff Mahoney 	"debug",
1754da35113SJeff Mahoney };
1764da35113SJeff Mahoney 
17735f4e5e6SNikolay Borisov 
17835f4e5e6SNikolay Borisov /*
17935f4e5e6SNikolay Borisov  * Use one ratelimit state per log level so that a flood of less important
18035f4e5e6SNikolay Borisov  * messages doesn't cause more important ones to be dropped.
18135f4e5e6SNikolay Borisov  */
18235f4e5e6SNikolay Borisov static struct ratelimit_state printk_limits[] = {
18335f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100),
18435f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100),
18535f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100),
18635f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100),
18735f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100),
18835f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100),
18935f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100),
19035f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100),
19135f4e5e6SNikolay Borisov };
19235f4e5e6SNikolay Borisov 
193b105e927SDavid Sterba void __cold btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
1944da35113SJeff Mahoney {
19540f7828bSPetr Mladek 	char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0";
1964da35113SJeff Mahoney 	struct va_format vaf;
1974da35113SJeff Mahoney 	va_list args;
198533574c6SJoe Perches 	int kern_level;
19940f7828bSPetr Mladek 	const char *type = logtypes[4];
20040f7828bSPetr Mladek 	struct ratelimit_state *ratelimit = &printk_limits[4];
2014da35113SJeff Mahoney 
2024da35113SJeff Mahoney 	va_start(args, fmt);
2034da35113SJeff Mahoney 
204262c5e86SPetr Mladek 	while ((kern_level = printk_get_level(fmt)) != 0) {
205533574c6SJoe Perches 		size_t size = printk_skip_level(fmt) - fmt;
206262c5e86SPetr Mladek 
207262c5e86SPetr Mladek 		if (kern_level >= '0' && kern_level <= '7') {
208533574c6SJoe Perches 			memcpy(lvl, fmt,  size);
209533574c6SJoe Perches 			lvl[size] = '\0';
210533574c6SJoe Perches 			type = logtypes[kern_level - '0'];
21135f4e5e6SNikolay Borisov 			ratelimit = &printk_limits[kern_level - '0'];
212262c5e86SPetr Mladek 		}
213262c5e86SPetr Mladek 		fmt += size;
214262c5e86SPetr Mladek 	}
215262c5e86SPetr Mladek 
2164da35113SJeff Mahoney 	vaf.fmt = fmt;
2174da35113SJeff Mahoney 	vaf.va = &args;
218533574c6SJoe Perches 
21935f4e5e6SNikolay Borisov 	if (__ratelimit(ratelimit))
2203993b112SColin Ian King 		printk("%sBTRFS %s (device %s): %pV\n", lvl, type,
2213993b112SColin Ian King 			fs_info ? fs_info->sb->s_id : "<unknown>", &vaf);
222533574c6SJoe Perches 
223533574c6SJoe Perches 	va_end(args);
2244da35113SJeff Mahoney }
225533574c6SJoe Perches #endif
226533574c6SJoe Perches 
2278c342930SJeff Mahoney /*
22849b25e05SJeff Mahoney  * We only mark the transaction aborted and then set the file system read-only.
22949b25e05SJeff Mahoney  * This will prevent new transactions from starting or trying to join this
23049b25e05SJeff Mahoney  * one.
23149b25e05SJeff Mahoney  *
23249b25e05SJeff Mahoney  * This means that error recovery at the call site is limited to freeing
23349b25e05SJeff Mahoney  * any local memory allocations and passing the error code up without
23449b25e05SJeff Mahoney  * further cleanup. The transaction should complete as it normally would
23549b25e05SJeff Mahoney  * in the call path but will return -EIO.
23649b25e05SJeff Mahoney  *
23749b25e05SJeff Mahoney  * We'll complete the cleanup in btrfs_end_transaction and
23849b25e05SJeff Mahoney  * btrfs_commit_transaction.
23949b25e05SJeff Mahoney  */
240c0d19e2bSDavid Sterba __cold
24149b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
24266642832SJeff Mahoney 			       const char *function,
24349b25e05SJeff Mahoney 			       unsigned int line, int errno)
24449b25e05SJeff Mahoney {
24566642832SJeff Mahoney 	struct btrfs_fs_info *fs_info = trans->fs_info;
24666642832SJeff Mahoney 
24749b25e05SJeff Mahoney 	trans->aborted = errno;
24849b25e05SJeff Mahoney 	/* Nothing used. The other threads that have joined this
24949b25e05SJeff Mahoney 	 * transaction may be able to continue. */
25064c12921SJeff Mahoney 	if (!trans->dirty && list_empty(&trans->new_bgs)) {
25169ce977aSMiao Xie 		const char *errstr;
25269ce977aSMiao Xie 
25308748810SDavid Sterba 		errstr = btrfs_decode_error(errno);
25466642832SJeff Mahoney 		btrfs_warn(fs_info,
255c2cf52ebSSimon Kirby 		           "%s:%d: Aborting unused transaction(%s).",
25669ce977aSMiao Xie 		           function, line, errstr);
25749b25e05SJeff Mahoney 		return;
25849b25e05SJeff Mahoney 	}
25920c7bcecSSeraphime Kirkovski 	WRITE_ONCE(trans->transaction->aborted, errno);
260501407aaSJosef Bacik 	/* Wake up anybody who may be waiting on this transaction */
26166642832SJeff Mahoney 	wake_up(&fs_info->transaction_wait);
26266642832SJeff Mahoney 	wake_up(&fs_info->transaction_blocked_wait);
26366642832SJeff Mahoney 	__btrfs_handle_fs_error(fs_info, function, line, errno, NULL);
26449b25e05SJeff Mahoney }
26549b25e05SJeff Mahoney /*
2668c342930SJeff Mahoney  * __btrfs_panic decodes unexpected, fatal errors from the caller,
2678c342930SJeff Mahoney  * issues an alert, and either panics or BUGs, depending on mount options.
2688c342930SJeff Mahoney  */
269c0d19e2bSDavid Sterba __cold
2708c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
2718c342930SJeff Mahoney 		   unsigned int line, int errno, const char *fmt, ...)
2728c342930SJeff Mahoney {
2738c342930SJeff Mahoney 	char *s_id = "<unknown>";
2748c342930SJeff Mahoney 	const char *errstr;
2758c342930SJeff Mahoney 	struct va_format vaf = { .fmt = fmt };
2768c342930SJeff Mahoney 	va_list args;
2778c342930SJeff Mahoney 
2788c342930SJeff Mahoney 	if (fs_info)
2798c342930SJeff Mahoney 		s_id = fs_info->sb->s_id;
2808c342930SJeff Mahoney 
2818c342930SJeff Mahoney 	va_start(args, fmt);
2828c342930SJeff Mahoney 	vaf.va = &args;
2838c342930SJeff Mahoney 
28408748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
285d8953d69SSatoru Takeuchi 	if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR)))
28608748810SDavid Sterba 		panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
28708748810SDavid Sterba 			s_id, function, line, &vaf, errno, errstr);
2888c342930SJeff Mahoney 
289efe120a0SFrank Holton 	btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
290efe120a0SFrank Holton 		   function, line, &vaf, errno, errstr);
2918c342930SJeff Mahoney 	va_end(args);
2928c342930SJeff Mahoney 	/* Caller calls BUG() */
2938c342930SJeff Mahoney }
294acce952bSliubo 
295e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
296e20d96d6SChris Mason {
2976bccf3abSJeff Mahoney 	close_ctree(btrfs_sb(sb));
298e20d96d6SChris Mason }
2992e635a27SChris Mason 
30095e05289SChris Mason enum {
301416a7202SDavid Sterba 	Opt_acl, Opt_noacl,
302416a7202SDavid Sterba 	Opt_clear_cache,
303416a7202SDavid Sterba 	Opt_commit_interval,
304416a7202SDavid Sterba 	Opt_compress,
305416a7202SDavid Sterba 	Opt_compress_force,
306416a7202SDavid Sterba 	Opt_compress_force_type,
307416a7202SDavid Sterba 	Opt_compress_type,
308416a7202SDavid Sterba 	Opt_degraded,
309416a7202SDavid Sterba 	Opt_device,
310416a7202SDavid Sterba 	Opt_fatal_errors,
311416a7202SDavid Sterba 	Opt_flushoncommit, Opt_noflushoncommit,
312416a7202SDavid Sterba 	Opt_inode_cache, Opt_noinode_cache,
313416a7202SDavid Sterba 	Opt_max_inline,
314416a7202SDavid Sterba 	Opt_barrier, Opt_nobarrier,
315416a7202SDavid Sterba 	Opt_datacow, Opt_nodatacow,
316416a7202SDavid Sterba 	Opt_datasum, Opt_nodatasum,
317416a7202SDavid Sterba 	Opt_defrag, Opt_nodefrag,
318416a7202SDavid Sterba 	Opt_discard, Opt_nodiscard,
319b0643e59SDennis Zhou 	Opt_discard_mode,
320416a7202SDavid Sterba 	Opt_nologreplay,
321416a7202SDavid Sterba 	Opt_norecovery,
322416a7202SDavid Sterba 	Opt_ratio,
323416a7202SDavid Sterba 	Opt_rescan_uuid_tree,
324416a7202SDavid Sterba 	Opt_skip_balance,
325416a7202SDavid Sterba 	Opt_space_cache, Opt_no_space_cache,
326416a7202SDavid Sterba 	Opt_space_cache_version,
327416a7202SDavid Sterba 	Opt_ssd, Opt_nossd,
328416a7202SDavid Sterba 	Opt_ssd_spread, Opt_nossd_spread,
329416a7202SDavid Sterba 	Opt_subvol,
33037becec9SOmar Sandoval 	Opt_subvol_empty,
331416a7202SDavid Sterba 	Opt_subvolid,
332416a7202SDavid Sterba 	Opt_thread_pool,
333416a7202SDavid Sterba 	Opt_treelog, Opt_notreelog,
334416a7202SDavid Sterba 	Opt_usebackuproot,
335416a7202SDavid Sterba 	Opt_user_subvol_rm_allowed,
336416a7202SDavid Sterba 
337416a7202SDavid Sterba 	/* Deprecated options */
338416a7202SDavid Sterba 	Opt_alloc_start,
339416a7202SDavid Sterba 	Opt_recovery,
340416a7202SDavid Sterba 	Opt_subvolrootid,
341416a7202SDavid Sterba 
342416a7202SDavid Sterba 	/* Debugging options */
343416a7202SDavid Sterba 	Opt_check_integrity,
34470f6d82eSOmar Sandoval 	Opt_check_integrity_including_extent_data,
345416a7202SDavid Sterba 	Opt_check_integrity_print_mask,
346416a7202SDavid Sterba 	Opt_enospc_debug, Opt_noenospc_debug,
347d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
348d0bd4560SJosef Bacik 	Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
349d0bd4560SJosef Bacik #endif
350fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
351fb592373SJosef Bacik 	Opt_ref_verify,
352fb592373SJosef Bacik #endif
3539555c6c1SIlya Dryomov 	Opt_err,
35495e05289SChris Mason };
35595e05289SChris Mason 
3564d4ab6d6SDavid Sterba static const match_table_t tokens = {
357416a7202SDavid Sterba 	{Opt_acl, "acl"},
358416a7202SDavid Sterba 	{Opt_noacl, "noacl"},
359416a7202SDavid Sterba 	{Opt_clear_cache, "clear_cache"},
360416a7202SDavid Sterba 	{Opt_commit_interval, "commit=%u"},
361c8b97818SChris Mason 	{Opt_compress, "compress"},
362261507a0SLi Zefan 	{Opt_compress_type, "compress=%s"},
363a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
364261507a0SLi Zefan 	{Opt_compress_force_type, "compress-force=%s"},
365416a7202SDavid Sterba 	{Opt_degraded, "degraded"},
366416a7202SDavid Sterba 	{Opt_device, "device=%s"},
367416a7202SDavid Sterba 	{Opt_fatal_errors, "fatal_errors=%s"},
368dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
3692c9ee856SQu Wenruo 	{Opt_noflushoncommit, "noflushoncommit"},
3704b9465cbSChris Mason 	{Opt_inode_cache, "inode_cache"},
3713818aea2SQu Wenruo 	{Opt_noinode_cache, "noinode_cache"},
372416a7202SDavid Sterba 	{Opt_max_inline, "max_inline=%s"},
373416a7202SDavid Sterba 	{Opt_barrier, "barrier"},
374416a7202SDavid Sterba 	{Opt_nobarrier, "nobarrier"},
375416a7202SDavid Sterba 	{Opt_datacow, "datacow"},
376416a7202SDavid Sterba 	{Opt_nodatacow, "nodatacow"},
377416a7202SDavid Sterba 	{Opt_datasum, "datasum"},
378416a7202SDavid Sterba 	{Opt_nodatasum, "nodatasum"},
379416a7202SDavid Sterba 	{Opt_defrag, "autodefrag"},
380416a7202SDavid Sterba 	{Opt_nodefrag, "noautodefrag"},
381416a7202SDavid Sterba 	{Opt_discard, "discard"},
382b0643e59SDennis Zhou 	{Opt_discard_mode, "discard=%s"},
383416a7202SDavid Sterba 	{Opt_nodiscard, "nodiscard"},
384416a7202SDavid Sterba 	{Opt_nologreplay, "nologreplay"},
385416a7202SDavid Sterba 	{Opt_norecovery, "norecovery"},
386416a7202SDavid Sterba 	{Opt_ratio, "metadata_ratio=%u"},
387416a7202SDavid Sterba 	{Opt_rescan_uuid_tree, "rescan_uuid_tree"},
3889555c6c1SIlya Dryomov 	{Opt_skip_balance, "skip_balance"},
389416a7202SDavid Sterba 	{Opt_space_cache, "space_cache"},
390416a7202SDavid Sterba 	{Opt_no_space_cache, "nospace_cache"},
391416a7202SDavid Sterba 	{Opt_space_cache_version, "space_cache=%s"},
392416a7202SDavid Sterba 	{Opt_ssd, "ssd"},
393416a7202SDavid Sterba 	{Opt_nossd, "nossd"},
394416a7202SDavid Sterba 	{Opt_ssd_spread, "ssd_spread"},
395416a7202SDavid Sterba 	{Opt_nossd_spread, "nossd_spread"},
396416a7202SDavid Sterba 	{Opt_subvol, "subvol=%s"},
39737becec9SOmar Sandoval 	{Opt_subvol_empty, "subvol="},
398416a7202SDavid Sterba 	{Opt_subvolid, "subvolid=%s"},
399416a7202SDavid Sterba 	{Opt_thread_pool, "thread_pool=%u"},
400416a7202SDavid Sterba 	{Opt_treelog, "treelog"},
401416a7202SDavid Sterba 	{Opt_notreelog, "notreelog"},
402416a7202SDavid Sterba 	{Opt_usebackuproot, "usebackuproot"},
403416a7202SDavid Sterba 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
404416a7202SDavid Sterba 
405416a7202SDavid Sterba 	/* Deprecated options */
406416a7202SDavid Sterba 	{Opt_alloc_start, "alloc_start=%s"},
407416a7202SDavid Sterba 	{Opt_recovery, "recovery"},
408416a7202SDavid Sterba 	{Opt_subvolrootid, "subvolrootid=%d"},
409416a7202SDavid Sterba 
410416a7202SDavid Sterba 	/* Debugging options */
41121adbd5cSStefan Behrens 	{Opt_check_integrity, "check_int"},
41221adbd5cSStefan Behrens 	{Opt_check_integrity_including_extent_data, "check_int_data"},
41302453bdeSAnand Jain 	{Opt_check_integrity_print_mask, "check_int_print_mask=%u"},
414416a7202SDavid Sterba 	{Opt_enospc_debug, "enospc_debug"},
415416a7202SDavid Sterba 	{Opt_noenospc_debug, "noenospc_debug"},
416d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
417d0bd4560SJosef Bacik 	{Opt_fragment_data, "fragment=data"},
418d0bd4560SJosef Bacik 	{Opt_fragment_metadata, "fragment=metadata"},
419d0bd4560SJosef Bacik 	{Opt_fragment_all, "fragment=all"},
420d0bd4560SJosef Bacik #endif
421fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
422fb592373SJosef Bacik 	{Opt_ref_verify, "ref_verify"},
423fb592373SJosef Bacik #endif
42433268eafSJosef Bacik 	{Opt_err, NULL},
42595e05289SChris Mason };
42695e05289SChris Mason 
427edf24abeSChristoph Hellwig /*
428edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
429edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
43049b25e05SJeff Mahoney  * XXX JDM: This needs to be cleaned up for remount.
431edf24abeSChristoph Hellwig  */
4322ff7e61eSJeff Mahoney int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
43396da0919SQu Wenruo 			unsigned long new_flags)
43495e05289SChris Mason {
43595e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
436e215772cSMisono, Tomohiro 	char *p, *num;
43773bc1876SJosef Bacik 	u64 cache_gen;
4384543df7eSChris Mason 	int intarg;
439a7a3f7caSSage Weil 	int ret = 0;
440261507a0SLi Zefan 	char *compress_type;
441261507a0SLi Zefan 	bool compress_force = false;
442b7c47bbbSTsutomu Itoh 	enum btrfs_compression_type saved_compress_type;
443b7c47bbbSTsutomu Itoh 	bool saved_compress_force;
444b7c47bbbSTsutomu Itoh 	int no_compress = 0;
445b6cda9bcSChris Mason 
4460b246afaSJeff Mahoney 	cache_gen = btrfs_super_cache_generation(info->super_copy);
4470b246afaSJeff Mahoney 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
44870f6d82eSOmar Sandoval 		btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
44970f6d82eSOmar Sandoval 	else if (cache_gen)
45073bc1876SJosef Bacik 		btrfs_set_opt(info->mount_opt, SPACE_CACHE);
45173bc1876SJosef Bacik 
45296da0919SQu Wenruo 	/*
45396da0919SQu Wenruo 	 * Even the options are empty, we still need to do extra check
45496da0919SQu Wenruo 	 * against new flags
45596da0919SQu Wenruo 	 */
45695e05289SChris Mason 	if (!options)
45796da0919SQu Wenruo 		goto check;
45895e05289SChris Mason 
45995e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
46095e05289SChris Mason 		int token;
46195e05289SChris Mason 		if (!*p)
46295e05289SChris Mason 			continue;
46395e05289SChris Mason 
46495e05289SChris Mason 		token = match_token(p, tokens, args);
46595e05289SChris Mason 		switch (token) {
466dfe25020SChris Mason 		case Opt_degraded:
4670b246afaSJeff Mahoney 			btrfs_info(info, "allowing degraded mounts");
468dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
469dfe25020SChris Mason 			break;
47095e05289SChris Mason 		case Opt_subvol:
47137becec9SOmar Sandoval 		case Opt_subvol_empty:
47273f73415SJosef Bacik 		case Opt_subvolid:
473e15d0542SXin Zhong 		case Opt_subvolrootid:
47443e570b0SChristoph Hellwig 		case Opt_device:
475edf24abeSChristoph Hellwig 			/*
476fa59f27cSAnand Jain 			 * These are parsed by btrfs_parse_subvol_options or
477fa59f27cSAnand Jain 			 * btrfs_parse_device_options and can be ignored here.
478edf24abeSChristoph Hellwig 			 */
47995e05289SChris Mason 			break;
480b6cda9bcSChris Mason 		case Opt_nodatasum:
4813cdde224SJeff Mahoney 			btrfs_set_and_info(info, NODATASUM,
48207802534SQu Wenruo 					   "setting nodatasum");
483be20aa9dSChris Mason 			break;
484d399167dSQu Wenruo 		case Opt_datasum:
4853cdde224SJeff Mahoney 			if (btrfs_test_opt(info, NODATASUM)) {
4863cdde224SJeff Mahoney 				if (btrfs_test_opt(info, NODATACOW))
4870b246afaSJeff Mahoney 					btrfs_info(info,
4885d163e0eSJeff Mahoney 						   "setting datasum, datacow enabled");
489d399167dSQu Wenruo 				else
4900b246afaSJeff Mahoney 					btrfs_info(info, "setting datasum");
49107802534SQu Wenruo 			}
492d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATACOW);
493d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATASUM);
494d399167dSQu Wenruo 			break;
495be20aa9dSChris Mason 		case Opt_nodatacow:
4963cdde224SJeff Mahoney 			if (!btrfs_test_opt(info, NODATACOW)) {
4973cdde224SJeff Mahoney 				if (!btrfs_test_opt(info, COMPRESS) ||
4983cdde224SJeff Mahoney 				    !btrfs_test_opt(info, FORCE_COMPRESS)) {
4990b246afaSJeff Mahoney 					btrfs_info(info,
500efe120a0SFrank Holton 						   "setting nodatacow, compression disabled");
501bedb2ccaSAndrei Popa 				} else {
5020b246afaSJeff Mahoney 					btrfs_info(info, "setting nodatacow");
503bedb2ccaSAndrei Popa 				}
50407802534SQu Wenruo 			}
505bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, COMPRESS);
506bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
507be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
508be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
509b6cda9bcSChris Mason 			break;
510a258af7aSQu Wenruo 		case Opt_datacow:
5113cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NODATACOW,
51207802534SQu Wenruo 					     "setting datacow");
513a258af7aSQu Wenruo 			break;
514a555f810SChris Mason 		case Opt_compress_force:
515261507a0SLi Zefan 		case Opt_compress_force_type:
516261507a0SLi Zefan 			compress_force = true;
5171c697d4aSEric Sandeen 			/* Fallthrough */
518261507a0SLi Zefan 		case Opt_compress:
519261507a0SLi Zefan 		case Opt_compress_type:
5203cdde224SJeff Mahoney 			saved_compress_type = btrfs_test_opt(info,
5213cdde224SJeff Mahoney 							     COMPRESS) ?
522b7c47bbbSTsutomu Itoh 				info->compress_type : BTRFS_COMPRESS_NONE;
523b7c47bbbSTsutomu Itoh 			saved_compress_force =
5243cdde224SJeff Mahoney 				btrfs_test_opt(info, FORCE_COMPRESS);
525261507a0SLi Zefan 			if (token == Opt_compress ||
526261507a0SLi Zefan 			    token == Opt_compress_force ||
527a7164fa4SDavid Sterba 			    strncmp(args[0].from, "zlib", 4) == 0) {
528261507a0SLi Zefan 				compress_type = "zlib";
529eae8d825SQu Wenruo 
530261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
531eae8d825SQu Wenruo 				info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
532eae8d825SQu Wenruo 				/*
533eae8d825SQu Wenruo 				 * args[0] contains uninitialized data since
534eae8d825SQu Wenruo 				 * for these tokens we don't expect any
535eae8d825SQu Wenruo 				 * parameter.
536eae8d825SQu Wenruo 				 */
537eae8d825SQu Wenruo 				if (token != Opt_compress &&
538eae8d825SQu Wenruo 				    token != Opt_compress_force)
539f51d2b59SDavid Sterba 					info->compress_level =
540d0ab62ceSDennis Zhou 					  btrfs_compress_str2level(
541d0ab62ceSDennis Zhou 							BTRFS_COMPRESS_ZLIB,
542d0ab62ceSDennis Zhou 							args[0].from + 4);
543063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
544bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
545bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
546b7c47bbbSTsutomu Itoh 				no_compress = 0;
547a7164fa4SDavid Sterba 			} else if (strncmp(args[0].from, "lzo", 3) == 0) {
548a6fa6faeSLi Zefan 				compress_type = "lzo";
549a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
550063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
551bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
552bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
5532b0ce2c2SMitch Harder 				btrfs_set_fs_incompat(info, COMPRESS_LZO);
554b7c47bbbSTsutomu Itoh 				no_compress = 0;
5553f93aef5SDennis Zhou 			} else if (strncmp(args[0].from, "zstd", 4) == 0) {
5565c1aab1dSNick Terrell 				compress_type = "zstd";
5575c1aab1dSNick Terrell 				info->compress_type = BTRFS_COMPRESS_ZSTD;
5583f93aef5SDennis Zhou 				info->compress_level =
5593f93aef5SDennis Zhou 					btrfs_compress_str2level(
5603f93aef5SDennis Zhou 							 BTRFS_COMPRESS_ZSTD,
5613f93aef5SDennis Zhou 							 args[0].from + 4);
5625c1aab1dSNick Terrell 				btrfs_set_opt(info->mount_opt, COMPRESS);
5635c1aab1dSNick Terrell 				btrfs_clear_opt(info->mount_opt, NODATACOW);
5645c1aab1dSNick Terrell 				btrfs_clear_opt(info->mount_opt, NODATASUM);
5655c1aab1dSNick Terrell 				btrfs_set_fs_incompat(info, COMPRESS_ZSTD);
5665c1aab1dSNick Terrell 				no_compress = 0;
567063849eaSArnd Hannemann 			} else if (strncmp(args[0].from, "no", 2) == 0) {
568063849eaSArnd Hannemann 				compress_type = "no";
569063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, COMPRESS);
570063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
571063849eaSArnd Hannemann 				compress_force = false;
572b7c47bbbSTsutomu Itoh 				no_compress++;
573261507a0SLi Zefan 			} else {
574261507a0SLi Zefan 				ret = -EINVAL;
575261507a0SLi Zefan 				goto out;
576261507a0SLi Zefan 			}
577261507a0SLi Zefan 
578261507a0SLi Zefan 			if (compress_force) {
579b7c47bbbSTsutomu Itoh 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
580143f3636SDavid Sterba 			} else {
5814027e0f4SWang Shilong 				/*
5824027e0f4SWang Shilong 				 * If we remount from compress-force=xxx to
5834027e0f4SWang Shilong 				 * compress=xxx, we need clear FORCE_COMPRESS
5844027e0f4SWang Shilong 				 * flag, otherwise, there is no way for users
5854027e0f4SWang Shilong 				 * to disable forcible compression separately.
5864027e0f4SWang Shilong 				 */
5874027e0f4SWang Shilong 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
588a7e252afSMiao Xie 			}
5893cdde224SJeff Mahoney 			if ((btrfs_test_opt(info, COMPRESS) &&
590b7c47bbbSTsutomu Itoh 			     (info->compress_type != saved_compress_type ||
591b7c47bbbSTsutomu Itoh 			      compress_force != saved_compress_force)) ||
5923cdde224SJeff Mahoney 			    (!btrfs_test_opt(info, COMPRESS) &&
593b7c47bbbSTsutomu Itoh 			     no_compress == 1)) {
594f51d2b59SDavid Sterba 				btrfs_info(info, "%s %s compression, level %d",
595b7c47bbbSTsutomu Itoh 					   (compress_force) ? "force" : "use",
596f51d2b59SDavid Sterba 					   compress_type, info->compress_level);
597b7c47bbbSTsutomu Itoh 			}
598b7c47bbbSTsutomu Itoh 			compress_force = false;
599a555f810SChris Mason 			break;
600e18e4809SChris Mason 		case Opt_ssd:
6013cdde224SJeff Mahoney 			btrfs_set_and_info(info, SSD,
602583b7231SHans van Kranenburg 					   "enabling ssd optimizations");
603951e7966SAdam Borowski 			btrfs_clear_opt(info->mount_opt, NOSSD);
604e18e4809SChris Mason 			break;
605451d7585SChris Mason 		case Opt_ssd_spread:
606583b7231SHans van Kranenburg 			btrfs_set_and_info(info, SSD,
607583b7231SHans van Kranenburg 					   "enabling ssd optimizations");
6083cdde224SJeff Mahoney 			btrfs_set_and_info(info, SSD_SPREAD,
609583b7231SHans van Kranenburg 					   "using spread ssd allocation scheme");
610951e7966SAdam Borowski 			btrfs_clear_opt(info->mount_opt, NOSSD);
611451d7585SChris Mason 			break;
6123b30c22fSChris Mason 		case Opt_nossd:
613583b7231SHans van Kranenburg 			btrfs_set_opt(info->mount_opt, NOSSD);
614583b7231SHans van Kranenburg 			btrfs_clear_and_info(info, SSD,
615583b7231SHans van Kranenburg 					     "not using ssd optimizations");
61662b8e077SHoward McLauchlan 			/* Fallthrough */
61762b8e077SHoward McLauchlan 		case Opt_nossd_spread:
618583b7231SHans van Kranenburg 			btrfs_clear_and_info(info, SSD_SPREAD,
619583b7231SHans van Kranenburg 					     "not using spread ssd allocation scheme");
6203b30c22fSChris Mason 			break;
621842bef58SQu Wenruo 		case Opt_barrier:
6223cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NOBARRIER,
62307802534SQu Wenruo 					     "turning on barriers");
624842bef58SQu Wenruo 			break;
62521ad10cfSChris Mason 		case Opt_nobarrier:
6263cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOBARRIER,
62707802534SQu Wenruo 					   "turning off barriers");
62821ad10cfSChris Mason 			break;
6294543df7eSChris Mason 		case Opt_thread_pool:
6302c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
6312c334e87SWang Shilong 			if (ret) {
6322c334e87SWang Shilong 				goto out;
633f7b885beSAnand Jain 			} else if (intarg == 0) {
6342c334e87SWang Shilong 				ret = -EINVAL;
6352c334e87SWang Shilong 				goto out;
6362c334e87SWang Shilong 			}
637f7b885beSAnand Jain 			info->thread_pool_size = intarg;
6384543df7eSChris Mason 			break;
6396f568d35SChris Mason 		case Opt_max_inline:
640edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
6416f568d35SChris Mason 			if (num) {
64291748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
6436f568d35SChris Mason 				kfree(num);
6446f568d35SChris Mason 
64515ada040SChris Mason 				if (info->max_inline) {
646feb5f965SMitch Harder 					info->max_inline = min_t(u64,
64715ada040SChris Mason 						info->max_inline,
6480b246afaSJeff Mahoney 						info->sectorsize);
64915ada040SChris Mason 				}
6500b246afaSJeff Mahoney 				btrfs_info(info, "max_inline at %llu",
651c1c9ff7cSGeert Uytterhoeven 					   info->max_inline);
6522c334e87SWang Shilong 			} else {
6532c334e87SWang Shilong 				ret = -ENOMEM;
6542c334e87SWang Shilong 				goto out;
6556f568d35SChris Mason 			}
6566f568d35SChris Mason 			break;
6578f662a76SChris Mason 		case Opt_alloc_start:
6580d0c71b3SDavid Sterba 			btrfs_info(info,
6590d0c71b3SDavid Sterba 				"option alloc_start is obsolete, ignored");
6608f662a76SChris Mason 			break;
661bd0330adSQu Wenruo 		case Opt_acl:
66245ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL
6631751e8a6SLinus Torvalds 			info->sb->s_flags |= SB_POSIXACL;
664bd0330adSQu Wenruo 			break;
66545ff35d6SGuangliang Zhao #else
6660b246afaSJeff Mahoney 			btrfs_err(info, "support for ACL not compiled in!");
66745ff35d6SGuangliang Zhao 			ret = -EINVAL;
66845ff35d6SGuangliang Zhao 			goto out;
66945ff35d6SGuangliang Zhao #endif
67033268eafSJosef Bacik 		case Opt_noacl:
6711751e8a6SLinus Torvalds 			info->sb->s_flags &= ~SB_POSIXACL;
67233268eafSJosef Bacik 			break;
6733a5e1404SSage Weil 		case Opt_notreelog:
6743cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOTREELOG,
67507802534SQu Wenruo 					   "disabling tree log");
6763a5e1404SSage Weil 			break;
677a88998f2SQu Wenruo 		case Opt_treelog:
6783cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NOTREELOG,
67907802534SQu Wenruo 					     "enabling tree log");
680a88998f2SQu Wenruo 			break;
681fed8f166SQu Wenruo 		case Opt_norecovery:
68296da0919SQu Wenruo 		case Opt_nologreplay:
6833cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOLOGREPLAY,
68496da0919SQu Wenruo 					   "disabling log replay at mount time");
68596da0919SQu Wenruo 			break;
686dccae999SSage Weil 		case Opt_flushoncommit:
6873cdde224SJeff Mahoney 			btrfs_set_and_info(info, FLUSHONCOMMIT,
68807802534SQu Wenruo 					   "turning on flush-on-commit");
689dccae999SSage Weil 			break;
6902c9ee856SQu Wenruo 		case Opt_noflushoncommit:
6913cdde224SJeff Mahoney 			btrfs_clear_and_info(info, FLUSHONCOMMIT,
69207802534SQu Wenruo 					     "turning off flush-on-commit");
6932c9ee856SQu Wenruo 			break;
69497e728d4SJosef Bacik 		case Opt_ratio:
6952c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
696764cb8b4SAnand Jain 			if (ret)
6972c334e87SWang Shilong 				goto out;
69897e728d4SJosef Bacik 			info->metadata_ratio = intarg;
699764cb8b4SAnand Jain 			btrfs_info(info, "metadata ratio %u",
70097e728d4SJosef Bacik 				   info->metadata_ratio);
70197e728d4SJosef Bacik 			break;
702e244a0aeSChristoph Hellwig 		case Opt_discard:
703b0643e59SDennis Zhou 		case Opt_discard_mode:
704b0643e59SDennis Zhou 			if (token == Opt_discard ||
705b0643e59SDennis Zhou 			    strcmp(args[0].from, "sync") == 0) {
706b0643e59SDennis Zhou 				btrfs_clear_opt(info->mount_opt, DISCARD_ASYNC);
70746b27f50SDennis Zhou 				btrfs_set_and_info(info, DISCARD_SYNC,
70846b27f50SDennis Zhou 						   "turning on sync discard");
709b0643e59SDennis Zhou 			} else if (strcmp(args[0].from, "async") == 0) {
710b0643e59SDennis Zhou 				btrfs_clear_opt(info->mount_opt, DISCARD_SYNC);
711b0643e59SDennis Zhou 				btrfs_set_and_info(info, DISCARD_ASYNC,
712b0643e59SDennis Zhou 						   "turning on async discard");
713b0643e59SDennis Zhou 			} else {
714b0643e59SDennis Zhou 				ret = -EINVAL;
715b0643e59SDennis Zhou 				goto out;
716b0643e59SDennis Zhou 			}
717e244a0aeSChristoph Hellwig 			break;
718e07a2adeSQu Wenruo 		case Opt_nodiscard:
71946b27f50SDennis Zhou 			btrfs_clear_and_info(info, DISCARD_SYNC,
72007802534SQu Wenruo 					     "turning off discard");
721b0643e59SDennis Zhou 			btrfs_clear_and_info(info, DISCARD_ASYNC,
722b0643e59SDennis Zhou 					     "turning off async discard");
723e07a2adeSQu Wenruo 			break;
7240af3d00bSJosef Bacik 		case Opt_space_cache:
72570f6d82eSOmar Sandoval 		case Opt_space_cache_version:
72670f6d82eSOmar Sandoval 			if (token == Opt_space_cache ||
72770f6d82eSOmar Sandoval 			    strcmp(args[0].from, "v1") == 0) {
7280b246afaSJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
72970f6d82eSOmar Sandoval 						FREE_SPACE_TREE);
7303cdde224SJeff Mahoney 				btrfs_set_and_info(info, SPACE_CACHE,
73107802534SQu Wenruo 					   "enabling disk space caching");
73270f6d82eSOmar Sandoval 			} else if (strcmp(args[0].from, "v2") == 0) {
7330b246afaSJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
73470f6d82eSOmar Sandoval 						SPACE_CACHE);
7350b246afaSJeff Mahoney 				btrfs_set_and_info(info, FREE_SPACE_TREE,
73670f6d82eSOmar Sandoval 						   "enabling free space tree");
73770f6d82eSOmar Sandoval 			} else {
73870f6d82eSOmar Sandoval 				ret = -EINVAL;
73970f6d82eSOmar Sandoval 				goto out;
74070f6d82eSOmar Sandoval 			}
7410de90876SJosef Bacik 			break;
742f420ee1eSStefan Behrens 		case Opt_rescan_uuid_tree:
743f420ee1eSStefan Behrens 			btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
744f420ee1eSStefan Behrens 			break;
74573bc1876SJosef Bacik 		case Opt_no_space_cache:
7463cdde224SJeff Mahoney 			if (btrfs_test_opt(info, SPACE_CACHE)) {
7470b246afaSJeff Mahoney 				btrfs_clear_and_info(info, SPACE_CACHE,
74807802534SQu Wenruo 					     "disabling disk space caching");
74970f6d82eSOmar Sandoval 			}
7503cdde224SJeff Mahoney 			if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
7510b246afaSJeff Mahoney 				btrfs_clear_and_info(info, FREE_SPACE_TREE,
75270f6d82eSOmar Sandoval 					     "disabling free space tree");
75370f6d82eSOmar Sandoval 			}
75473bc1876SJosef Bacik 			break;
7554b9465cbSChris Mason 		case Opt_inode_cache:
7567e1876acSDavid Sterba 			btrfs_set_pending_and_info(info, INODE_MAP_CACHE,
75707802534SQu Wenruo 					   "enabling inode map caching");
7583818aea2SQu Wenruo 			break;
7593818aea2SQu Wenruo 		case Opt_noinode_cache:
7607e1876acSDavid Sterba 			btrfs_clear_pending_and_info(info, INODE_MAP_CACHE,
76107802534SQu Wenruo 					     "disabling inode map caching");
7624b9465cbSChris Mason 			break;
76388c2ba3bSJosef Bacik 		case Opt_clear_cache:
7643cdde224SJeff Mahoney 			btrfs_set_and_info(info, CLEAR_CACHE,
76507802534SQu Wenruo 					   "force clearing of disk cache");
7660af3d00bSJosef Bacik 			break;
7674260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
7684260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
7694260f7c7SSage Weil 			break;
77091435650SChris Mason 		case Opt_enospc_debug:
77191435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
77291435650SChris Mason 			break;
77353036293SQu Wenruo 		case Opt_noenospc_debug:
77453036293SQu Wenruo 			btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
77553036293SQu Wenruo 			break;
7764cb5300bSChris Mason 		case Opt_defrag:
7773cdde224SJeff Mahoney 			btrfs_set_and_info(info, AUTO_DEFRAG,
77807802534SQu Wenruo 					   "enabling auto defrag");
7794cb5300bSChris Mason 			break;
780fc0ca9afSQu Wenruo 		case Opt_nodefrag:
7813cdde224SJeff Mahoney 			btrfs_clear_and_info(info, AUTO_DEFRAG,
78207802534SQu Wenruo 					     "disabling auto defrag");
783fc0ca9afSQu Wenruo 			break;
784af31f5e5SChris Mason 		case Opt_recovery:
7850b246afaSJeff Mahoney 			btrfs_warn(info,
7868dcddfa0SQu Wenruo 				   "'recovery' is deprecated, use 'usebackuproot' instead");
787acd43e3cSBart Van Assche 			/* fall through */
7888dcddfa0SQu Wenruo 		case Opt_usebackuproot:
7890b246afaSJeff Mahoney 			btrfs_info(info,
7908dcddfa0SQu Wenruo 				   "trying to use backup root at mount time");
7918dcddfa0SQu Wenruo 			btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
792af31f5e5SChris Mason 			break;
7939555c6c1SIlya Dryomov 		case Opt_skip_balance:
7949555c6c1SIlya Dryomov 			btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
7959555c6c1SIlya Dryomov 			break;
79621adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
79721adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
7980b246afaSJeff Mahoney 			btrfs_info(info,
799efe120a0SFrank Holton 				   "enabling check integrity including extent data");
80021adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt,
80121adbd5cSStefan Behrens 				      CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
80221adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
80321adbd5cSStefan Behrens 			break;
80421adbd5cSStefan Behrens 		case Opt_check_integrity:
8050b246afaSJeff Mahoney 			btrfs_info(info, "enabling check integrity");
80621adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
80721adbd5cSStefan Behrens 			break;
80821adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
8092c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
81002453bdeSAnand Jain 			if (ret)
8112c334e87SWang Shilong 				goto out;
81221adbd5cSStefan Behrens 			info->check_integrity_print_mask = intarg;
81302453bdeSAnand Jain 			btrfs_info(info, "check_integrity_print_mask 0x%x",
81421adbd5cSStefan Behrens 				   info->check_integrity_print_mask);
81521adbd5cSStefan Behrens 			break;
81621adbd5cSStefan Behrens #else
81721adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
81821adbd5cSStefan Behrens 		case Opt_check_integrity:
81921adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
8200b246afaSJeff Mahoney 			btrfs_err(info,
821efe120a0SFrank Holton 				  "support for check_integrity* not compiled in!");
82221adbd5cSStefan Behrens 			ret = -EINVAL;
82321adbd5cSStefan Behrens 			goto out;
82421adbd5cSStefan Behrens #endif
8258c342930SJeff Mahoney 		case Opt_fatal_errors:
8268c342930SJeff Mahoney 			if (strcmp(args[0].from, "panic") == 0)
8278c342930SJeff Mahoney 				btrfs_set_opt(info->mount_opt,
8288c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
8298c342930SJeff Mahoney 			else if (strcmp(args[0].from, "bug") == 0)
8308c342930SJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
8318c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
8328c342930SJeff Mahoney 			else {
8338c342930SJeff Mahoney 				ret = -EINVAL;
8348c342930SJeff Mahoney 				goto out;
8358c342930SJeff Mahoney 			}
8368c342930SJeff Mahoney 			break;
8378b87dc17SDavid Sterba 		case Opt_commit_interval:
8388b87dc17SDavid Sterba 			intarg = 0;
8398b87dc17SDavid Sterba 			ret = match_int(&args[0], &intarg);
840d3740608SAnand Jain 			if (ret)
8418b87dc17SDavid Sterba 				goto out;
842d3740608SAnand Jain 			if (intarg == 0) {
843d3740608SAnand Jain 				btrfs_info(info,
844d3740608SAnand Jain 					   "using default commit interval %us",
845d3740608SAnand Jain 					   BTRFS_DEFAULT_COMMIT_INTERVAL);
846d3740608SAnand Jain 				intarg = BTRFS_DEFAULT_COMMIT_INTERVAL;
847d3740608SAnand Jain 			} else if (intarg > 300) {
848d3740608SAnand Jain 				btrfs_warn(info, "excessive commit interval %d",
8498b87dc17SDavid Sterba 					   intarg);
8508b87dc17SDavid Sterba 			}
8518b87dc17SDavid Sterba 			info->commit_interval = intarg;
8528b87dc17SDavid Sterba 			break;
853d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
854d0bd4560SJosef Bacik 		case Opt_fragment_all:
8550b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting all space");
856d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
857d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
858d0bd4560SJosef Bacik 			break;
859d0bd4560SJosef Bacik 		case Opt_fragment_metadata:
8600b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting metadata");
861d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt,
862d0bd4560SJosef Bacik 				      FRAGMENT_METADATA);
863d0bd4560SJosef Bacik 			break;
864d0bd4560SJosef Bacik 		case Opt_fragment_data:
8650b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting data");
866d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
867d0bd4560SJosef Bacik 			break;
868d0bd4560SJosef Bacik #endif
869fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
870fb592373SJosef Bacik 		case Opt_ref_verify:
871fb592373SJosef Bacik 			btrfs_info(info, "doing ref verification");
872fb592373SJosef Bacik 			btrfs_set_opt(info->mount_opt, REF_VERIFY);
873fb592373SJosef Bacik 			break;
874fb592373SJosef Bacik #endif
875a7a3f7caSSage Weil 		case Opt_err:
8760b246afaSJeff Mahoney 			btrfs_info(info, "unrecognized mount option '%s'", p);
877a7a3f7caSSage Weil 			ret = -EINVAL;
878a7a3f7caSSage Weil 			goto out;
87995e05289SChris Mason 		default:
880be20aa9dSChris Mason 			break;
88195e05289SChris Mason 		}
88295e05289SChris Mason 	}
88396da0919SQu Wenruo check:
88496da0919SQu Wenruo 	/*
88596da0919SQu Wenruo 	 * Extra check for current option against current flag
88696da0919SQu Wenruo 	 */
8871751e8a6SLinus Torvalds 	if (btrfs_test_opt(info, NOLOGREPLAY) && !(new_flags & SB_RDONLY)) {
8880b246afaSJeff Mahoney 		btrfs_err(info,
88996da0919SQu Wenruo 			  "nologreplay must be used with ro mount option");
89096da0919SQu Wenruo 		ret = -EINVAL;
89196da0919SQu Wenruo 	}
892a7a3f7caSSage Weil out:
8930b246afaSJeff Mahoney 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
8943cdde224SJeff Mahoney 	    !btrfs_test_opt(info, FREE_SPACE_TREE) &&
8953cdde224SJeff Mahoney 	    !btrfs_test_opt(info, CLEAR_CACHE)) {
8960b246afaSJeff Mahoney 		btrfs_err(info, "cannot disable free space tree");
89770f6d82eSOmar Sandoval 		ret = -EINVAL;
89870f6d82eSOmar Sandoval 
89970f6d82eSOmar Sandoval 	}
9003cdde224SJeff Mahoney 	if (!ret && btrfs_test_opt(info, SPACE_CACHE))
9010b246afaSJeff Mahoney 		btrfs_info(info, "disk space caching is enabled");
9023cdde224SJeff Mahoney 	if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE))
9030b246afaSJeff Mahoney 		btrfs_info(info, "using free space tree");
904a7a3f7caSSage Weil 	return ret;
905edf24abeSChristoph Hellwig }
906edf24abeSChristoph Hellwig 
907edf24abeSChristoph Hellwig /*
908edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
909edf24abeSChristoph Hellwig  *
910edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
911edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
912edf24abeSChristoph Hellwig  */
913fa59f27cSAnand Jain static int btrfs_parse_device_options(const char *options, fmode_t flags,
914d64dcbd1SGu Jinxiang 				      void *holder)
915edf24abeSChristoph Hellwig {
916edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
91783c8c9bdSJeff Liu 	char *device_name, *opts, *orig, *p;
91836350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
919d7407606SMisono, Tomohiro 	int error = 0;
920d7407606SMisono, Tomohiro 
9215139cff5SDavid Sterba 	lockdep_assert_held(&uuid_mutex);
9225139cff5SDavid Sterba 
923d7407606SMisono, Tomohiro 	if (!options)
924d7407606SMisono, Tomohiro 		return 0;
925d7407606SMisono, Tomohiro 
926d7407606SMisono, Tomohiro 	/*
927d7407606SMisono, Tomohiro 	 * strsep changes the string, duplicate it because btrfs_parse_options
928d7407606SMisono, Tomohiro 	 * gets called later
929d7407606SMisono, Tomohiro 	 */
930d7407606SMisono, Tomohiro 	opts = kstrdup(options, GFP_KERNEL);
931d7407606SMisono, Tomohiro 	if (!opts)
932d7407606SMisono, Tomohiro 		return -ENOMEM;
933d7407606SMisono, Tomohiro 	orig = opts;
934d7407606SMisono, Tomohiro 
935d7407606SMisono, Tomohiro 	while ((p = strsep(&opts, ",")) != NULL) {
936d7407606SMisono, Tomohiro 		int token;
937d7407606SMisono, Tomohiro 
938d7407606SMisono, Tomohiro 		if (!*p)
939d7407606SMisono, Tomohiro 			continue;
940d7407606SMisono, Tomohiro 
941d7407606SMisono, Tomohiro 		token = match_token(p, tokens, args);
942d7407606SMisono, Tomohiro 		if (token == Opt_device) {
943d7407606SMisono, Tomohiro 			device_name = match_strdup(&args[0]);
944d7407606SMisono, Tomohiro 			if (!device_name) {
945d7407606SMisono, Tomohiro 				error = -ENOMEM;
946d7407606SMisono, Tomohiro 				goto out;
947d7407606SMisono, Tomohiro 			}
94836350e95SGu Jinxiang 			device = btrfs_scan_one_device(device_name, flags,
94936350e95SGu Jinxiang 					holder);
950d7407606SMisono, Tomohiro 			kfree(device_name);
95136350e95SGu Jinxiang 			if (IS_ERR(device)) {
95236350e95SGu Jinxiang 				error = PTR_ERR(device);
953d7407606SMisono, Tomohiro 				goto out;
954d7407606SMisono, Tomohiro 			}
955d7407606SMisono, Tomohiro 		}
95636350e95SGu Jinxiang 	}
957d7407606SMisono, Tomohiro 
958d7407606SMisono, Tomohiro out:
959d7407606SMisono, Tomohiro 	kfree(orig);
960d7407606SMisono, Tomohiro 	return error;
961d7407606SMisono, Tomohiro }
962d7407606SMisono, Tomohiro 
963d7407606SMisono, Tomohiro /*
964d7407606SMisono, Tomohiro  * Parse mount options that are related to subvolume id
965d7407606SMisono, Tomohiro  *
966d7407606SMisono, Tomohiro  * The value is later passed to mount_subvol()
967d7407606SMisono, Tomohiro  */
96893b9bcdfSGu Jinxiang static int btrfs_parse_subvol_options(const char *options, char **subvol_name,
96993b9bcdfSGu Jinxiang 		u64 *subvol_objectid)
970d7407606SMisono, Tomohiro {
971d7407606SMisono, Tomohiro 	substring_t args[MAX_OPT_ARGS];
972d7407606SMisono, Tomohiro 	char *opts, *orig, *p;
973edf24abeSChristoph Hellwig 	int error = 0;
974ccb0e7d1SAnand Jain 	u64 subvolid;
975edf24abeSChristoph Hellwig 
976edf24abeSChristoph Hellwig 	if (!options)
977830c4adbSJosef Bacik 		return 0;
978edf24abeSChristoph Hellwig 
979edf24abeSChristoph Hellwig 	/*
980d7407606SMisono, Tomohiro 	 * strsep changes the string, duplicate it because
981fa59f27cSAnand Jain 	 * btrfs_parse_device_options gets called later
982edf24abeSChristoph Hellwig 	 */
983edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
984edf24abeSChristoph Hellwig 	if (!opts)
985edf24abeSChristoph Hellwig 		return -ENOMEM;
9863f3d0bc0STero Roponen 	orig = opts;
987edf24abeSChristoph Hellwig 
988edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
989edf24abeSChristoph Hellwig 		int token;
990edf24abeSChristoph Hellwig 		if (!*p)
991edf24abeSChristoph Hellwig 			continue;
992edf24abeSChristoph Hellwig 
993edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
994edf24abeSChristoph Hellwig 		switch (token) {
995edf24abeSChristoph Hellwig 		case Opt_subvol:
996a90e8b6fSIlya Dryomov 			kfree(*subvol_name);
997edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
9982c334e87SWang Shilong 			if (!*subvol_name) {
9992c334e87SWang Shilong 				error = -ENOMEM;
10002c334e87SWang Shilong 				goto out;
10012c334e87SWang Shilong 			}
1002edf24abeSChristoph Hellwig 			break;
100373f73415SJosef Bacik 		case Opt_subvolid:
1004ccb0e7d1SAnand Jain 			error = match_u64(&args[0], &subvolid);
1005ccb0e7d1SAnand Jain 			if (error)
10062c334e87SWang Shilong 				goto out;
1007ccb0e7d1SAnand Jain 
1008ccb0e7d1SAnand Jain 			/* we want the original fs_tree */
1009ccb0e7d1SAnand Jain 			if (subvolid == 0)
1010ccb0e7d1SAnand Jain 				subvolid = BTRFS_FS_TREE_OBJECTID;
1011ccb0e7d1SAnand Jain 
1012ccb0e7d1SAnand Jain 			*subvol_objectid = subvolid;
101373f73415SJosef Bacik 			break;
1014e15d0542SXin Zhong 		case Opt_subvolrootid:
101562e85577SJeff Mahoney 			pr_warn("BTRFS: 'subvolrootid' mount option is deprecated and has no effect\n");
1016e15d0542SXin Zhong 			break;
1017edf24abeSChristoph Hellwig 		default:
1018edf24abeSChristoph Hellwig 			break;
1019edf24abeSChristoph Hellwig 		}
1020edf24abeSChristoph Hellwig 	}
1021edf24abeSChristoph Hellwig 
1022edf24abeSChristoph Hellwig out:
1023830c4adbSJosef Bacik 	kfree(orig);
1024edf24abeSChristoph Hellwig 	return error;
102595e05289SChris Mason }
102695e05289SChris Mason 
102705dbe683SOmar Sandoval static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
102873f73415SJosef Bacik 					   u64 subvol_objectid)
102973f73415SJosef Bacik {
1030815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
103105dbe683SOmar Sandoval 	struct btrfs_root *fs_root;
103205dbe683SOmar Sandoval 	struct btrfs_root_ref *root_ref;
103305dbe683SOmar Sandoval 	struct btrfs_inode_ref *inode_ref;
103405dbe683SOmar Sandoval 	struct btrfs_key key;
103505dbe683SOmar Sandoval 	struct btrfs_path *path = NULL;
103605dbe683SOmar Sandoval 	char *name = NULL, *ptr;
103705dbe683SOmar Sandoval 	u64 dirid;
103805dbe683SOmar Sandoval 	int len;
103905dbe683SOmar Sandoval 	int ret;
104005dbe683SOmar Sandoval 
104105dbe683SOmar Sandoval 	path = btrfs_alloc_path();
104205dbe683SOmar Sandoval 	if (!path) {
104305dbe683SOmar Sandoval 		ret = -ENOMEM;
104405dbe683SOmar Sandoval 		goto err;
104505dbe683SOmar Sandoval 	}
104605dbe683SOmar Sandoval 	path->leave_spinning = 1;
104705dbe683SOmar Sandoval 
10483ec83621SDavid Sterba 	name = kmalloc(PATH_MAX, GFP_KERNEL);
104905dbe683SOmar Sandoval 	if (!name) {
105005dbe683SOmar Sandoval 		ret = -ENOMEM;
105105dbe683SOmar Sandoval 		goto err;
105205dbe683SOmar Sandoval 	}
105305dbe683SOmar Sandoval 	ptr = name + PATH_MAX - 1;
105405dbe683SOmar Sandoval 	ptr[0] = '\0';
105505dbe683SOmar Sandoval 
105605dbe683SOmar Sandoval 	/*
105705dbe683SOmar Sandoval 	 * Walk up the subvolume trees in the tree of tree roots by root
105805dbe683SOmar Sandoval 	 * backrefs until we hit the top-level subvolume.
105905dbe683SOmar Sandoval 	 */
106005dbe683SOmar Sandoval 	while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
106105dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
106205dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_BACKREF_KEY;
106305dbe683SOmar Sandoval 		key.offset = (u64)-1;
106405dbe683SOmar Sandoval 
106505dbe683SOmar Sandoval 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
106605dbe683SOmar Sandoval 		if (ret < 0) {
106705dbe683SOmar Sandoval 			goto err;
106805dbe683SOmar Sandoval 		} else if (ret > 0) {
106905dbe683SOmar Sandoval 			ret = btrfs_previous_item(root, path, subvol_objectid,
107005dbe683SOmar Sandoval 						  BTRFS_ROOT_BACKREF_KEY);
107105dbe683SOmar Sandoval 			if (ret < 0) {
107205dbe683SOmar Sandoval 				goto err;
107305dbe683SOmar Sandoval 			} else if (ret > 0) {
107405dbe683SOmar Sandoval 				ret = -ENOENT;
107505dbe683SOmar Sandoval 				goto err;
107605dbe683SOmar Sandoval 			}
107705dbe683SOmar Sandoval 		}
107805dbe683SOmar Sandoval 
107905dbe683SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
108005dbe683SOmar Sandoval 		subvol_objectid = key.offset;
108105dbe683SOmar Sandoval 
108205dbe683SOmar Sandoval 		root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
108305dbe683SOmar Sandoval 					  struct btrfs_root_ref);
108405dbe683SOmar Sandoval 		len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
108505dbe683SOmar Sandoval 		ptr -= len + 1;
108605dbe683SOmar Sandoval 		if (ptr < name) {
108705dbe683SOmar Sandoval 			ret = -ENAMETOOLONG;
108805dbe683SOmar Sandoval 			goto err;
108905dbe683SOmar Sandoval 		}
109005dbe683SOmar Sandoval 		read_extent_buffer(path->nodes[0], ptr + 1,
109105dbe683SOmar Sandoval 				   (unsigned long)(root_ref + 1), len);
109205dbe683SOmar Sandoval 		ptr[0] = '/';
109305dbe683SOmar Sandoval 		dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
109405dbe683SOmar Sandoval 		btrfs_release_path(path);
109505dbe683SOmar Sandoval 
109605dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
109705dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_ITEM_KEY;
109805dbe683SOmar Sandoval 		key.offset = (u64)-1;
1099*3619c94fSJosef Bacik 		fs_root = btrfs_get_fs_root(fs_info, &key, true);
110005dbe683SOmar Sandoval 		if (IS_ERR(fs_root)) {
110105dbe683SOmar Sandoval 			ret = PTR_ERR(fs_root);
110205dbe683SOmar Sandoval 			goto err;
110305dbe683SOmar Sandoval 		}
110405dbe683SOmar Sandoval 
110505dbe683SOmar Sandoval 		/*
110605dbe683SOmar Sandoval 		 * Walk up the filesystem tree by inode refs until we hit the
110705dbe683SOmar Sandoval 		 * root directory.
110805dbe683SOmar Sandoval 		 */
110905dbe683SOmar Sandoval 		while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
111005dbe683SOmar Sandoval 			key.objectid = dirid;
111105dbe683SOmar Sandoval 			key.type = BTRFS_INODE_REF_KEY;
111205dbe683SOmar Sandoval 			key.offset = (u64)-1;
111305dbe683SOmar Sandoval 
111405dbe683SOmar Sandoval 			ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
111505dbe683SOmar Sandoval 			if (ret < 0) {
111605dbe683SOmar Sandoval 				goto err;
111705dbe683SOmar Sandoval 			} else if (ret > 0) {
111805dbe683SOmar Sandoval 				ret = btrfs_previous_item(fs_root, path, dirid,
111905dbe683SOmar Sandoval 							  BTRFS_INODE_REF_KEY);
112005dbe683SOmar Sandoval 				if (ret < 0) {
112105dbe683SOmar Sandoval 					goto err;
112205dbe683SOmar Sandoval 				} else if (ret > 0) {
112305dbe683SOmar Sandoval 					ret = -ENOENT;
112405dbe683SOmar Sandoval 					goto err;
112505dbe683SOmar Sandoval 				}
112605dbe683SOmar Sandoval 			}
112705dbe683SOmar Sandoval 
112805dbe683SOmar Sandoval 			btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
112905dbe683SOmar Sandoval 			dirid = key.offset;
113005dbe683SOmar Sandoval 
113105dbe683SOmar Sandoval 			inode_ref = btrfs_item_ptr(path->nodes[0],
113205dbe683SOmar Sandoval 						   path->slots[0],
113305dbe683SOmar Sandoval 						   struct btrfs_inode_ref);
113405dbe683SOmar Sandoval 			len = btrfs_inode_ref_name_len(path->nodes[0],
113505dbe683SOmar Sandoval 						       inode_ref);
113605dbe683SOmar Sandoval 			ptr -= len + 1;
113705dbe683SOmar Sandoval 			if (ptr < name) {
113805dbe683SOmar Sandoval 				ret = -ENAMETOOLONG;
113905dbe683SOmar Sandoval 				goto err;
114005dbe683SOmar Sandoval 			}
114105dbe683SOmar Sandoval 			read_extent_buffer(path->nodes[0], ptr + 1,
114205dbe683SOmar Sandoval 					   (unsigned long)(inode_ref + 1), len);
114305dbe683SOmar Sandoval 			ptr[0] = '/';
114405dbe683SOmar Sandoval 			btrfs_release_path(path);
114505dbe683SOmar Sandoval 		}
114605dbe683SOmar Sandoval 	}
114705dbe683SOmar Sandoval 
114805dbe683SOmar Sandoval 	btrfs_free_path(path);
114905dbe683SOmar Sandoval 	if (ptr == name + PATH_MAX - 1) {
115005dbe683SOmar Sandoval 		name[0] = '/';
115105dbe683SOmar Sandoval 		name[1] = '\0';
115205dbe683SOmar Sandoval 	} else {
115305dbe683SOmar Sandoval 		memmove(name, ptr, name + PATH_MAX - ptr);
115405dbe683SOmar Sandoval 	}
115505dbe683SOmar Sandoval 	return name;
115605dbe683SOmar Sandoval 
115705dbe683SOmar Sandoval err:
115805dbe683SOmar Sandoval 	btrfs_free_path(path);
115905dbe683SOmar Sandoval 	kfree(name);
116005dbe683SOmar Sandoval 	return ERR_PTR(ret);
116105dbe683SOmar Sandoval }
116205dbe683SOmar Sandoval 
116305dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
116405dbe683SOmar Sandoval {
116505dbe683SOmar Sandoval 	struct btrfs_root *root = fs_info->tree_root;
116673f73415SJosef Bacik 	struct btrfs_dir_item *di;
116773f73415SJosef Bacik 	struct btrfs_path *path;
116873f73415SJosef Bacik 	struct btrfs_key location;
116973f73415SJosef Bacik 	u64 dir_id;
117073f73415SJosef Bacik 
117173f73415SJosef Bacik 	path = btrfs_alloc_path();
117273f73415SJosef Bacik 	if (!path)
117305dbe683SOmar Sandoval 		return -ENOMEM;
117473f73415SJosef Bacik 	path->leave_spinning = 1;
117573f73415SJosef Bacik 
117673f73415SJosef Bacik 	/*
117773f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
117873f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
117973f73415SJosef Bacik 	 * to mount.
118073f73415SJosef Bacik 	 */
1181815745cfSAl Viro 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
118273f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
1183b0839166SJulia Lawall 	if (IS_ERR(di)) {
1184b0839166SJulia Lawall 		btrfs_free_path(path);
118505dbe683SOmar Sandoval 		return PTR_ERR(di);
1186b0839166SJulia Lawall 	}
118773f73415SJosef Bacik 	if (!di) {
118873f73415SJosef Bacik 		/*
118973f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
119073f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
119105dbe683SOmar Sandoval 		 * mount the top-level subvolume.
119273f73415SJosef Bacik 		 */
119373f73415SJosef Bacik 		btrfs_free_path(path);
119405dbe683SOmar Sandoval 		*objectid = BTRFS_FS_TREE_OBJECTID;
119505dbe683SOmar Sandoval 		return 0;
119673f73415SJosef Bacik 	}
119773f73415SJosef Bacik 
119873f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
119973f73415SJosef Bacik 	btrfs_free_path(path);
120005dbe683SOmar Sandoval 	*objectid = location.objectid;
120105dbe683SOmar Sandoval 	return 0;
120273f73415SJosef Bacik }
120373f73415SJosef Bacik 
12048a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
12058a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
120656e033a7SDavid Sterba 			    void *data)
12072e635a27SChris Mason {
12082e635a27SChris Mason 	struct inode *inode;
1209815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
12105d4f98a2SYan Zheng 	struct btrfs_key key;
121139279cc3SChris Mason 	int err;
12122e635a27SChris Mason 
12132e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
12142e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
1215e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
1216af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
1217be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
12185103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
12192e635a27SChris Mason 	sb->s_time_gran = 1;
12200eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
12211751e8a6SLinus Torvalds 	sb->s_flags |= SB_POSIXACL;
122249cf6f45SChris Ball #endif
1223357fdad0SMatthew Garrett 	sb->s_flags |= SB_I_VERSION;
1224da2f0f74SChris Mason 	sb->s_iflags |= SB_I_CGROUPWB;
12259e11ceeeSJan Kara 
12269e11ceeeSJan Kara 	err = super_setup_bdi(sb);
12279e11ceeeSJan Kara 	if (err) {
12289e11ceeeSJan Kara 		btrfs_err(fs_info, "super_setup_bdi failed");
12299e11ceeeSJan Kara 		return err;
12309e11ceeeSJan Kara 	}
12319e11ceeeSJan Kara 
1232ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
1233ad2b2c80SAl Viro 	if (err) {
1234ab8d0fc4SJeff Mahoney 		btrfs_err(fs_info, "open_ctree failed");
1235ad2b2c80SAl Viro 		return err;
1236e20d96d6SChris Mason 	}
1237b888db2bSChris Mason 
12385d4f98a2SYan Zheng 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
12395d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
12405d4f98a2SYan Zheng 	key.offset = 0;
12414c66e0d4SDavid Sterba 	inode = btrfs_iget(sb, &key, fs_info->fs_root);
12425d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
12435d4f98a2SYan Zheng 		err = PTR_ERR(inode);
124439279cc3SChris Mason 		goto fail_close;
124539279cc3SChris Mason 	}
12462e635a27SChris Mason 
124748fde701SAl Viro 	sb->s_root = d_make_root(inode);
124848fde701SAl Viro 	if (!sb->s_root) {
124939279cc3SChris Mason 		err = -ENOMEM;
125039279cc3SChris Mason 		goto fail_close;
12512e635a27SChris Mason 	}
125258176a96SJosef Bacik 
125390a887c9SDan Magenheimer 	cleancache_init_fs(sb);
12541751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
12552e635a27SChris Mason 	return 0;
12562e635a27SChris Mason 
125739279cc3SChris Mason fail_close:
12586bccf3abSJeff Mahoney 	close_ctree(fs_info);
1259d5719762SChris Mason 	return err;
1260d5719762SChris Mason }
1261d5719762SChris Mason 
12626bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
1263d5719762SChris Mason {
1264d5719762SChris Mason 	struct btrfs_trans_handle *trans;
1265815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1266815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
1267df2ce34cSChris Mason 
1268bc074524SJeff Mahoney 	trace_btrfs_sync_fs(fs_info, wait);
12691abe9b8aSliubo 
1270d561c025SChris Mason 	if (!wait) {
1271815745cfSAl Viro 		filemap_flush(fs_info->btree_inode->i_mapping);
1272df2ce34cSChris Mason 		return 0;
1273d561c025SChris Mason 	}
1274771ed689SChris Mason 
12756374e57aSChris Mason 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1276771ed689SChris Mason 
1277d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
127860376ce4SJosef Bacik 	if (IS_ERR(trans)) {
1279354aa0fbSMiao Xie 		/* no transaction, don't bother */
12806b5fe46dSDavid Sterba 		if (PTR_ERR(trans) == -ENOENT) {
12816b5fe46dSDavid Sterba 			/*
12826b5fe46dSDavid Sterba 			 * Exit unless we have some pending changes
12836b5fe46dSDavid Sterba 			 * that need to go through commit
12846b5fe46dSDavid Sterba 			 */
12856b5fe46dSDavid Sterba 			if (fs_info->pending_changes == 0)
1286bd7de2c9SJosef Bacik 				return 0;
1287a53f4f8eSQu Wenruo 			/*
1288a53f4f8eSQu Wenruo 			 * A non-blocking test if the fs is frozen. We must not
1289a53f4f8eSQu Wenruo 			 * start a new transaction here otherwise a deadlock
1290a53f4f8eSQu Wenruo 			 * happens. The pending operations are delayed to the
1291a53f4f8eSQu Wenruo 			 * next commit after thawing.
1292a53f4f8eSQu Wenruo 			 */
1293a7e3c5f2SRakesh Pandit 			if (sb_start_write_trylock(sb))
1294a7e3c5f2SRakesh Pandit 				sb_end_write(sb);
1295a53f4f8eSQu Wenruo 			else
1296a53f4f8eSQu Wenruo 				return 0;
12976b5fe46dSDavid Sterba 			trans = btrfs_start_transaction(root, 0);
129860376ce4SJosef Bacik 		}
129998bd5c54SDavid Sterba 		if (IS_ERR(trans))
130098bd5c54SDavid Sterba 			return PTR_ERR(trans);
13016b5fe46dSDavid Sterba 	}
13023a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
1303d5719762SChris Mason }
1304d5719762SChris Mason 
130534c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1306a9572a15SEric Paris {
1307815745cfSAl Viro 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
13080f628c63SDavid Sterba 	const char *compress_type;
1309a9572a15SEric Paris 
13103cdde224SJeff Mahoney 	if (btrfs_test_opt(info, DEGRADED))
1311a9572a15SEric Paris 		seq_puts(seq, ",degraded");
13123cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATASUM))
1313a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
13143cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATACOW))
1315a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
13163cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOBARRIER))
1317a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
131895ac567aSFilipe David Borba Manana 	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1319c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",max_inline=%llu", info->max_inline);
1320a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
1321a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
1322f7b885beSAnand Jain 		seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
13233cdde224SJeff Mahoney 	if (btrfs_test_opt(info, COMPRESS)) {
13240f628c63SDavid Sterba 		compress_type = btrfs_compress_type2str(info->compress_type);
13253cdde224SJeff Mahoney 		if (btrfs_test_opt(info, FORCE_COMPRESS))
1326200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
1327200da64eSTsutomu Itoh 		else
1328200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
1329f51d2b59SDavid Sterba 		if (info->compress_level)
1330fa4d885aSAdam Borowski 			seq_printf(seq, ":%d", info->compress_level);
1331200da64eSTsutomu Itoh 	}
13323cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOSSD))
1333c289811cSChris Mason 		seq_puts(seq, ",nossd");
13343cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SSD_SPREAD))
1335451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
13363cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, SSD))
1337a9572a15SEric Paris 		seq_puts(seq, ",ssd");
13383cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOTREELOG))
13396b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
13403cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOLOGREPLAY))
134196da0919SQu Wenruo 		seq_puts(seq, ",nologreplay");
13423cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FLUSHONCOMMIT))
13436b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
134446b27f50SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_SYNC))
134520a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
1346b0643e59SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_ASYNC))
1347b0643e59SDennis Zhou 		seq_puts(seq, ",discard=async");
13481751e8a6SLinus Torvalds 	if (!(info->sb->s_flags & SB_POSIXACL))
1349a9572a15SEric Paris 		seq_puts(seq, ",noacl");
13503cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SPACE_CACHE))
1351200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
13523cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, FREE_SPACE_TREE))
135370f6d82eSOmar Sandoval 		seq_puts(seq, ",space_cache=v2");
135473bc1876SJosef Bacik 	else
13558965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
13563cdde224SJeff Mahoney 	if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1357f420ee1eSStefan Behrens 		seq_puts(seq, ",rescan_uuid_tree");
13583cdde224SJeff Mahoney 	if (btrfs_test_opt(info, CLEAR_CACHE))
1359200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
13603cdde224SJeff Mahoney 	if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1361200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
13623cdde224SJeff Mahoney 	if (btrfs_test_opt(info, ENOSPC_DEBUG))
13630942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
13643cdde224SJeff Mahoney 	if (btrfs_test_opt(info, AUTO_DEFRAG))
13650942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
13663cdde224SJeff Mahoney 	if (btrfs_test_opt(info, INODE_MAP_CACHE))
13670942caa3SDavid Sterba 		seq_puts(seq, ",inode_cache");
13683cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SKIP_BALANCE))
13699555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
13708507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
13713cdde224SJeff Mahoney 	if (btrfs_test_opt(info, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
13728507d216SWang Shilong 		seq_puts(seq, ",check_int_data");
13733cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, CHECK_INTEGRITY))
13748507d216SWang Shilong 		seq_puts(seq, ",check_int");
13758507d216SWang Shilong 	if (info->check_integrity_print_mask)
13768507d216SWang Shilong 		seq_printf(seq, ",check_int_print_mask=%d",
13778507d216SWang Shilong 				info->check_integrity_print_mask);
13788507d216SWang Shilong #endif
13798507d216SWang Shilong 	if (info->metadata_ratio)
1380764cb8b4SAnand Jain 		seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
13813cdde224SJeff Mahoney 	if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
13828c342930SJeff Mahoney 		seq_puts(seq, ",fatal_errors=panic");
13838b87dc17SDavid Sterba 	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1384d3740608SAnand Jain 		seq_printf(seq, ",commit=%u", info->commit_interval);
1385d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
13863cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_DATA))
1387d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=data");
13883cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_METADATA))
1389d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=metadata");
1390d0bd4560SJosef Bacik #endif
1391fb592373SJosef Bacik 	if (btrfs_test_opt(info, REF_VERIFY))
1392fb592373SJosef Bacik 		seq_puts(seq, ",ref_verify");
1393c8d3fe02SOmar Sandoval 	seq_printf(seq, ",subvolid=%llu",
1394c8d3fe02SOmar Sandoval 		  BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1395c8d3fe02SOmar Sandoval 	seq_puts(seq, ",subvol=");
1396c8d3fe02SOmar Sandoval 	seq_dentry(seq, dentry, " \t\n\\");
1397a9572a15SEric Paris 	return 0;
1398a9572a15SEric Paris }
1399a9572a15SEric Paris 
1400a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
14012e635a27SChris Mason {
1402815745cfSAl Viro 	struct btrfs_fs_info *p = data;
1403815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(s);
14044b82d6e4SYan 
1405815745cfSAl Viro 	return fs_info->fs_devices == p->fs_devices;
14064b82d6e4SYan }
14074b82d6e4SYan 
1408450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
1409450ba0eaSJosef Bacik {
14106de1d09dSAl Viro 	int err = set_anon_super(s, data);
14116de1d09dSAl Viro 	if (!err)
1412450ba0eaSJosef Bacik 		s->s_fs_info = data;
14136de1d09dSAl Viro 	return err;
1414450ba0eaSJosef Bacik }
1415450ba0eaSJosef Bacik 
1416830c4adbSJosef Bacik /*
1417f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
1418f9d9ef62SDavid Sterba  */
1419f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
1420f9d9ef62SDavid Sterba {
1421f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1422f9d9ef62SDavid Sterba 		return 1;
1423f9d9ef62SDavid Sterba 	return 0;
1424f9d9ef62SDavid Sterba }
1425f9d9ef62SDavid Sterba 
1426bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1427ae0bc863SAnand Jain 				   struct vfsmount *mnt)
1428830c4adbSJosef Bacik {
1429830c4adbSJosef Bacik 	struct dentry *root;
1430fa330659SOmar Sandoval 	int ret;
1431830c4adbSJosef Bacik 
143205dbe683SOmar Sandoval 	if (!subvol_name) {
143305dbe683SOmar Sandoval 		if (!subvol_objectid) {
143405dbe683SOmar Sandoval 			ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
143505dbe683SOmar Sandoval 							  &subvol_objectid);
143605dbe683SOmar Sandoval 			if (ret) {
143705dbe683SOmar Sandoval 				root = ERR_PTR(ret);
143805dbe683SOmar Sandoval 				goto out;
143905dbe683SOmar Sandoval 			}
144005dbe683SOmar Sandoval 		}
144105dbe683SOmar Sandoval 		subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb),
144205dbe683SOmar Sandoval 							    subvol_objectid);
144305dbe683SOmar Sandoval 		if (IS_ERR(subvol_name)) {
144405dbe683SOmar Sandoval 			root = ERR_CAST(subvol_name);
144505dbe683SOmar Sandoval 			subvol_name = NULL;
144605dbe683SOmar Sandoval 			goto out;
144705dbe683SOmar Sandoval 		}
144805dbe683SOmar Sandoval 
144905dbe683SOmar Sandoval 	}
145005dbe683SOmar Sandoval 
1451ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1452fa330659SOmar Sandoval 	/* mount_subtree() drops our reference on the vfsmount. */
1453fa330659SOmar Sandoval 	mnt = NULL;
1454830c4adbSJosef Bacik 
1455bb289b7bSOmar Sandoval 	if (!IS_ERR(root)) {
1456ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1457ab8d0fc4SJeff Mahoney 		struct btrfs_fs_info *fs_info = btrfs_sb(s);
1458bb289b7bSOmar Sandoval 		struct inode *root_inode = d_inode(root);
1459bb289b7bSOmar Sandoval 		u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1460bb289b7bSOmar Sandoval 
1461bb289b7bSOmar Sandoval 		ret = 0;
1462bb289b7bSOmar Sandoval 		if (!is_subvolume_inode(root_inode)) {
1463ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info, "'%s' is not a valid subvolume",
1464bb289b7bSOmar Sandoval 			       subvol_name);
1465bb289b7bSOmar Sandoval 			ret = -EINVAL;
1466bb289b7bSOmar Sandoval 		}
1467bb289b7bSOmar Sandoval 		if (subvol_objectid && root_objectid != subvol_objectid) {
146805dbe683SOmar Sandoval 			/*
146905dbe683SOmar Sandoval 			 * This will also catch a race condition where a
147005dbe683SOmar Sandoval 			 * subvolume which was passed by ID is renamed and
147105dbe683SOmar Sandoval 			 * another subvolume is renamed over the old location.
147205dbe683SOmar Sandoval 			 */
1473ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info,
1474ab8d0fc4SJeff Mahoney 				  "subvol '%s' does not match subvolid %llu",
1475bb289b7bSOmar Sandoval 				  subvol_name, subvol_objectid);
1476bb289b7bSOmar Sandoval 			ret = -EINVAL;
1477bb289b7bSOmar Sandoval 		}
1478bb289b7bSOmar Sandoval 		if (ret) {
1479ea441d11SAl Viro 			dput(root);
1480bb289b7bSOmar Sandoval 			root = ERR_PTR(ret);
1481ea441d11SAl Viro 			deactivate_locked_super(s);
1482bb289b7bSOmar Sandoval 		}
1483f9d9ef62SDavid Sterba 	}
1484f9d9ef62SDavid Sterba 
1485fa330659SOmar Sandoval out:
1486fa330659SOmar Sandoval 	mntput(mnt);
1487fa330659SOmar Sandoval 	kfree(subvol_name);
1488830c4adbSJosef Bacik 	return root;
1489830c4adbSJosef Bacik }
1490450ba0eaSJosef Bacik 
1491312c89fbSMisono, Tomohiro /*
1492312c89fbSMisono, Tomohiro  * Find a superblock for the given device / mount point.
1493312c89fbSMisono, Tomohiro  *
1494312c89fbSMisono, Tomohiro  * Note: This is based on mount_bdev from fs/super.c with a few additions
1495312c89fbSMisono, Tomohiro  *       for multiple device setup.  Make sure to keep it in sync.
1496312c89fbSMisono, Tomohiro  */
149772fa39f5SMisono, Tomohiro static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
149872fa39f5SMisono, Tomohiro 		int flags, const char *device_name, void *data)
149972fa39f5SMisono, Tomohiro {
150072fa39f5SMisono, Tomohiro 	struct block_device *bdev = NULL;
150172fa39f5SMisono, Tomohiro 	struct super_block *s;
150236350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
150372fa39f5SMisono, Tomohiro 	struct btrfs_fs_devices *fs_devices = NULL;
150472fa39f5SMisono, Tomohiro 	struct btrfs_fs_info *fs_info = NULL;
1505204cc0ccSAl Viro 	void *new_sec_opts = NULL;
150672fa39f5SMisono, Tomohiro 	fmode_t mode = FMODE_READ;
150772fa39f5SMisono, Tomohiro 	int error = 0;
150872fa39f5SMisono, Tomohiro 
150972fa39f5SMisono, Tomohiro 	if (!(flags & SB_RDONLY))
151072fa39f5SMisono, Tomohiro 		mode |= FMODE_WRITE;
151172fa39f5SMisono, Tomohiro 
151272fa39f5SMisono, Tomohiro 	if (data) {
1513a65001e8SAl Viro 		error = security_sb_eat_lsm_opts(data, &new_sec_opts);
151472fa39f5SMisono, Tomohiro 		if (error)
151572fa39f5SMisono, Tomohiro 			return ERR_PTR(error);
151672fa39f5SMisono, Tomohiro 	}
151772fa39f5SMisono, Tomohiro 
151872fa39f5SMisono, Tomohiro 	/*
151972fa39f5SMisono, Tomohiro 	 * Setup a dummy root and fs_info for test/set super.  This is because
152072fa39f5SMisono, Tomohiro 	 * we don't actually fill this stuff out until open_ctree, but we need
152172fa39f5SMisono, Tomohiro 	 * it for searching for existing supers, so this lets us do that and
152272fa39f5SMisono, Tomohiro 	 * then open_ctree will properly initialize everything later.
152372fa39f5SMisono, Tomohiro 	 */
1524a8fd1f71SJeff Mahoney 	fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
152572fa39f5SMisono, Tomohiro 	if (!fs_info) {
152672fa39f5SMisono, Tomohiro 		error = -ENOMEM;
152772fa39f5SMisono, Tomohiro 		goto error_sec_opts;
152872fa39f5SMisono, Tomohiro 	}
152972fa39f5SMisono, Tomohiro 
153072fa39f5SMisono, Tomohiro 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
153172fa39f5SMisono, Tomohiro 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
153272fa39f5SMisono, Tomohiro 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
153372fa39f5SMisono, Tomohiro 		error = -ENOMEM;
153472fa39f5SMisono, Tomohiro 		goto error_fs_info;
153572fa39f5SMisono, Tomohiro 	}
153672fa39f5SMisono, Tomohiro 
1537f5194e34SDavid Sterba 	mutex_lock(&uuid_mutex);
1538fa59f27cSAnand Jain 	error = btrfs_parse_device_options(data, mode, fs_type);
153981ffd56bSDavid Sterba 	if (error) {
1540399f7f4cSDavid Sterba 		mutex_unlock(&uuid_mutex);
1541399f7f4cSDavid Sterba 		goto error_fs_info;
154281ffd56bSDavid Sterba 	}
1543399f7f4cSDavid Sterba 
154436350e95SGu Jinxiang 	device = btrfs_scan_one_device(device_name, mode, fs_type);
154536350e95SGu Jinxiang 	if (IS_ERR(device)) {
1546399f7f4cSDavid Sterba 		mutex_unlock(&uuid_mutex);
154736350e95SGu Jinxiang 		error = PTR_ERR(device);
1548399f7f4cSDavid Sterba 		goto error_fs_info;
154981ffd56bSDavid Sterba 	}
1550399f7f4cSDavid Sterba 
155136350e95SGu Jinxiang 	fs_devices = device->fs_devices;
1552399f7f4cSDavid Sterba 	fs_info->fs_devices = fs_devices;
1553399f7f4cSDavid Sterba 
155472fa39f5SMisono, Tomohiro 	error = btrfs_open_devices(fs_devices, mode, fs_type);
1555f5194e34SDavid Sterba 	mutex_unlock(&uuid_mutex);
155672fa39f5SMisono, Tomohiro 	if (error)
155772fa39f5SMisono, Tomohiro 		goto error_fs_info;
155872fa39f5SMisono, Tomohiro 
155972fa39f5SMisono, Tomohiro 	if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
156072fa39f5SMisono, Tomohiro 		error = -EACCES;
156172fa39f5SMisono, Tomohiro 		goto error_close_devices;
156272fa39f5SMisono, Tomohiro 	}
156372fa39f5SMisono, Tomohiro 
156472fa39f5SMisono, Tomohiro 	bdev = fs_devices->latest_bdev;
156572fa39f5SMisono, Tomohiro 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
156672fa39f5SMisono, Tomohiro 		 fs_info);
156772fa39f5SMisono, Tomohiro 	if (IS_ERR(s)) {
156872fa39f5SMisono, Tomohiro 		error = PTR_ERR(s);
156972fa39f5SMisono, Tomohiro 		goto error_close_devices;
157072fa39f5SMisono, Tomohiro 	}
157172fa39f5SMisono, Tomohiro 
157272fa39f5SMisono, Tomohiro 	if (s->s_root) {
157372fa39f5SMisono, Tomohiro 		btrfs_close_devices(fs_devices);
157472fa39f5SMisono, Tomohiro 		free_fs_info(fs_info);
157572fa39f5SMisono, Tomohiro 		if ((flags ^ s->s_flags) & SB_RDONLY)
157672fa39f5SMisono, Tomohiro 			error = -EBUSY;
157772fa39f5SMisono, Tomohiro 	} else {
157872fa39f5SMisono, Tomohiro 		snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
157972fa39f5SMisono, Tomohiro 		btrfs_sb(s)->bdev_holder = fs_type;
15809b4e675aSDavid Sterba 		if (!strstr(crc32c_impl(), "generic"))
15819b4e675aSDavid Sterba 			set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
158272fa39f5SMisono, Tomohiro 		error = btrfs_fill_super(s, fs_devices, data);
158372fa39f5SMisono, Tomohiro 	}
1584a65001e8SAl Viro 	if (!error)
1585204cc0ccSAl Viro 		error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL);
1586a65001e8SAl Viro 	security_free_mnt_opts(&new_sec_opts);
158772fa39f5SMisono, Tomohiro 	if (error) {
158872fa39f5SMisono, Tomohiro 		deactivate_locked_super(s);
1589a65001e8SAl Viro 		return ERR_PTR(error);
159072fa39f5SMisono, Tomohiro 	}
159172fa39f5SMisono, Tomohiro 
159272fa39f5SMisono, Tomohiro 	return dget(s->s_root);
159372fa39f5SMisono, Tomohiro 
159472fa39f5SMisono, Tomohiro error_close_devices:
159572fa39f5SMisono, Tomohiro 	btrfs_close_devices(fs_devices);
159672fa39f5SMisono, Tomohiro error_fs_info:
159772fa39f5SMisono, Tomohiro 	free_fs_info(fs_info);
159872fa39f5SMisono, Tomohiro error_sec_opts:
159972fa39f5SMisono, Tomohiro 	security_free_mnt_opts(&new_sec_opts);
160072fa39f5SMisono, Tomohiro 	return ERR_PTR(error);
160172fa39f5SMisono, Tomohiro }
1602312c89fbSMisono, Tomohiro 
1603edf24abeSChristoph Hellwig /*
1604312c89fbSMisono, Tomohiro  * Mount function which is called by VFS layer.
1605edf24abeSChristoph Hellwig  *
1606312c89fbSMisono, Tomohiro  * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1607312c89fbSMisono, Tomohiro  * which needs vfsmount* of device's root (/).  This means device's root has to
1608312c89fbSMisono, Tomohiro  * be mounted internally in any case.
1609312c89fbSMisono, Tomohiro  *
1610312c89fbSMisono, Tomohiro  * Operation flow:
1611312c89fbSMisono, Tomohiro  *   1. Parse subvol id related options for later use in mount_subvol().
1612312c89fbSMisono, Tomohiro  *
1613312c89fbSMisono, Tomohiro  *   2. Mount device's root (/) by calling vfs_kern_mount().
1614312c89fbSMisono, Tomohiro  *
1615312c89fbSMisono, Tomohiro  *      NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1616312c89fbSMisono, Tomohiro  *      first place. In order to avoid calling btrfs_mount() again, we use
1617312c89fbSMisono, Tomohiro  *      different file_system_type which is not registered to VFS by
1618312c89fbSMisono, Tomohiro  *      register_filesystem() (btrfs_root_fs_type). As a result,
1619312c89fbSMisono, Tomohiro  *      btrfs_mount_root() is called. The return value will be used by
1620312c89fbSMisono, Tomohiro  *      mount_subtree() in mount_subvol().
1621312c89fbSMisono, Tomohiro  *
1622312c89fbSMisono, Tomohiro  *   3. Call mount_subvol() to get the dentry of subvolume. Since there is
1623312c89fbSMisono, Tomohiro  *      "btrfs subvolume set-default", mount_subvol() is called always.
1624edf24abeSChristoph Hellwig  */
1625061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1626306e16ceSDavid Sterba 		const char *device_name, void *data)
16274b82d6e4SYan {
1628312c89fbSMisono, Tomohiro 	struct vfsmount *mnt_root;
1629312c89fbSMisono, Tomohiro 	struct dentry *root;
163073f73415SJosef Bacik 	char *subvol_name = NULL;
163173f73415SJosef Bacik 	u64 subvol_objectid = 0;
16324b82d6e4SYan 	int error = 0;
16334b82d6e4SYan 
163493b9bcdfSGu Jinxiang 	error = btrfs_parse_subvol_options(data, &subvol_name,
163593b9bcdfSGu Jinxiang 					&subvol_objectid);
1636f23c8af8SIlya Dryomov 	if (error) {
1637f23c8af8SIlya Dryomov 		kfree(subvol_name);
1638061dbc6bSAl Viro 		return ERR_PTR(error);
1639f23c8af8SIlya Dryomov 	}
1640edf24abeSChristoph Hellwig 
1641312c89fbSMisono, Tomohiro 	/* mount device's root (/) */
1642312c89fbSMisono, Tomohiro 	mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data);
1643312c89fbSMisono, Tomohiro 	if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) {
1644312c89fbSMisono, Tomohiro 		if (flags & SB_RDONLY) {
1645312c89fbSMisono, Tomohiro 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1646312c89fbSMisono, Tomohiro 				flags & ~SB_RDONLY, device_name, data);
16474b82d6e4SYan 		} else {
1648312c89fbSMisono, Tomohiro 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1649312c89fbSMisono, Tomohiro 				flags | SB_RDONLY, device_name, data);
1650312c89fbSMisono, Tomohiro 			if (IS_ERR(mnt_root)) {
1651312c89fbSMisono, Tomohiro 				root = ERR_CAST(mnt_root);
1652532b618bSEric W. Biederman 				kfree(subvol_name);
1653312c89fbSMisono, Tomohiro 				goto out;
1654f667aef6SQu Wenruo 			}
1655f667aef6SQu Wenruo 
1656312c89fbSMisono, Tomohiro 			down_write(&mnt_root->mnt_sb->s_umount);
1657312c89fbSMisono, Tomohiro 			error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL);
1658312c89fbSMisono, Tomohiro 			up_write(&mnt_root->mnt_sb->s_umount);
1659312c89fbSMisono, Tomohiro 			if (error < 0) {
1660312c89fbSMisono, Tomohiro 				root = ERR_PTR(error);
1661312c89fbSMisono, Tomohiro 				mntput(mnt_root);
1662532b618bSEric W. Biederman 				kfree(subvol_name);
1663312c89fbSMisono, Tomohiro 				goto out;
1664312c89fbSMisono, Tomohiro 			}
1665312c89fbSMisono, Tomohiro 		}
1666312c89fbSMisono, Tomohiro 	}
1667312c89fbSMisono, Tomohiro 	if (IS_ERR(mnt_root)) {
1668312c89fbSMisono, Tomohiro 		root = ERR_CAST(mnt_root);
1669532b618bSEric W. Biederman 		kfree(subvol_name);
1670312c89fbSMisono, Tomohiro 		goto out;
1671f667aef6SQu Wenruo 	}
16724b82d6e4SYan 
1673312c89fbSMisono, Tomohiro 	/* mount_subvol() will free subvol_name and mnt_root */
1674ae0bc863SAnand Jain 	root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
16754b82d6e4SYan 
1676312c89fbSMisono, Tomohiro out:
1677312c89fbSMisono, Tomohiro 	return root;
16784b82d6e4SYan }
16792e635a27SChris Mason 
16800d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1681f7b885beSAnand Jain 				     u32 new_pool_size, u32 old_pool_size)
16820d2450abSSergei Trofimovich {
16830d2450abSSergei Trofimovich 	if (new_pool_size == old_pool_size)
16840d2450abSSergei Trofimovich 		return;
16850d2450abSSergei Trofimovich 
16860d2450abSSergei Trofimovich 	fs_info->thread_pool_size = new_pool_size;
16870d2450abSSergei Trofimovich 
1688efe120a0SFrank Holton 	btrfs_info(fs_info, "resize thread pool %d -> %d",
16890d2450abSSergei Trofimovich 	       old_pool_size, new_pool_size);
16900d2450abSSergei Trofimovich 
16915cdc7ad3SQu Wenruo 	btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1692afe3d242SQu Wenruo 	btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1693e66f0bb1SQu Wenruo 	btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1694fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size);
1695fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size);
1696fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_write_workers,
1697fccb5d86SQu Wenruo 				new_pool_size);
1698fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1699fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
17005b3bc44eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
1701736cfa15SQu Wenruo 	btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size);
17020339ef2fSQu Wenruo 	btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers,
1703ff023aacSStefan Behrens 				new_pool_size);
17040d2450abSSergei Trofimovich }
17050d2450abSSergei Trofimovich 
1706f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
1707dc81cdc5SMiao Xie {
1708dc81cdc5SMiao Xie 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1709f42a34b2SMiao Xie }
1710dc81cdc5SMiao Xie 
1711f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1712f42a34b2SMiao Xie 				       unsigned long old_opts, int flags)
1713f42a34b2SMiao Xie {
1714dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1715dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
17161751e8a6SLinus Torvalds 	     (flags & SB_RDONLY))) {
1717dc81cdc5SMiao Xie 		/* wait for any defraggers to finish */
1718dc81cdc5SMiao Xie 		wait_event(fs_info->transaction_wait,
1719dc81cdc5SMiao Xie 			   (atomic_read(&fs_info->defrag_running) == 0));
17201751e8a6SLinus Torvalds 		if (flags & SB_RDONLY)
1721dc81cdc5SMiao Xie 			sync_filesystem(fs_info->sb);
1722dc81cdc5SMiao Xie 	}
1723dc81cdc5SMiao Xie }
1724dc81cdc5SMiao Xie 
1725dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1726dc81cdc5SMiao Xie 					 unsigned long old_opts)
1727dc81cdc5SMiao Xie {
1728dc81cdc5SMiao Xie 	/*
1729180e4d47SLuis de Bethencourt 	 * We need to cleanup all defragable inodes if the autodefragment is
1730180e4d47SLuis de Bethencourt 	 * close or the filesystem is read only.
1731dc81cdc5SMiao Xie 	 */
1732dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1733bc98a42cSDavid Howells 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1734dc81cdc5SMiao Xie 		btrfs_cleanup_defrag_inodes(fs_info);
1735dc81cdc5SMiao Xie 	}
1736dc81cdc5SMiao Xie 
1737b0643e59SDennis Zhou 	/* If we toggled discard async */
1738b0643e59SDennis Zhou 	if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1739b0643e59SDennis Zhou 	    btrfs_test_opt(fs_info, DISCARD_ASYNC))
1740b0643e59SDennis Zhou 		btrfs_discard_resume(fs_info);
1741b0643e59SDennis Zhou 	else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1742b0643e59SDennis Zhou 		 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1743b0643e59SDennis Zhou 		btrfs_discard_cleanup(fs_info);
1744b0643e59SDennis Zhou 
1745dc81cdc5SMiao Xie 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1746dc81cdc5SMiao Xie }
1747dc81cdc5SMiao Xie 
1748c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1749c146afadSYan Zheng {
1750815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1751815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
175249b25e05SJeff Mahoney 	unsigned old_flags = sb->s_flags;
175349b25e05SJeff Mahoney 	unsigned long old_opts = fs_info->mount_opt;
175449b25e05SJeff Mahoney 	unsigned long old_compress_type = fs_info->compress_type;
175549b25e05SJeff Mahoney 	u64 old_max_inline = fs_info->max_inline;
1756f7b885beSAnand Jain 	u32 old_thread_pool_size = fs_info->thread_pool_size;
1757d612ac59SAnand Jain 	u32 old_metadata_ratio = fs_info->metadata_ratio;
1758c146afadSYan Zheng 	int ret;
1759c146afadSYan Zheng 
176002b9984dSTheodore Ts'o 	sync_filesystem(sb);
1761f42a34b2SMiao Xie 	btrfs_remount_prepare(fs_info);
1762dc81cdc5SMiao Xie 
1763f667aef6SQu Wenruo 	if (data) {
1764204cc0ccSAl Viro 		void *new_sec_opts = NULL;
1765f667aef6SQu Wenruo 
1766a65001e8SAl Viro 		ret = security_sb_eat_lsm_opts(data, &new_sec_opts);
1767a65001e8SAl Viro 		if (!ret)
1768204cc0ccSAl Viro 			ret = security_sb_remount(sb, new_sec_opts);
1769a65001e8SAl Viro 		security_free_mnt_opts(&new_sec_opts);
1770f667aef6SQu Wenruo 		if (ret)
1771f667aef6SQu Wenruo 			goto restore;
1772f667aef6SQu Wenruo 	}
1773f667aef6SQu Wenruo 
17742ff7e61eSJeff Mahoney 	ret = btrfs_parse_options(fs_info, data, *flags);
1775891f41cbSChengguang Xu 	if (ret)
177649b25e05SJeff Mahoney 		goto restore;
1777b288052eSChris Mason 
1778f42a34b2SMiao Xie 	btrfs_remount_begin(fs_info, old_opts, *flags);
17790d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
17800d2450abSSergei Trofimovich 		fs_info->thread_pool_size, old_thread_pool_size);
17810d2450abSSergei Trofimovich 
17821751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
1783dc81cdc5SMiao Xie 		goto out;
1784c146afadSYan Zheng 
17851751e8a6SLinus Torvalds 	if (*flags & SB_RDONLY) {
17868dabb742SStefan Behrens 		/*
17878dabb742SStefan Behrens 		 * this also happens on 'umount -rf' or on shutdown, when
17888dabb742SStefan Behrens 		 * the filesystem is busy.
17898dabb742SStefan Behrens 		 */
179021c7e756SMiao Xie 		cancel_work_sync(&fs_info->async_reclaim_work);
1791361c093dSStefan Behrens 
1792b0643e59SDennis Zhou 		btrfs_discard_cleanup(fs_info);
1793b0643e59SDennis Zhou 
1794361c093dSStefan Behrens 		/* wait for the uuid_scan task to finish */
1795361c093dSStefan Behrens 		down(&fs_info->uuid_tree_rescan_sem);
1796361c093dSStefan Behrens 		/* avoid complains from lockdep et al. */
1797361c093dSStefan Behrens 		up(&fs_info->uuid_tree_rescan_sem);
1798361c093dSStefan Behrens 
17991751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
1800c146afadSYan Zheng 
1801e44163e1SJeff Mahoney 		/*
18021751e8a6SLinus Torvalds 		 * Setting SB_RDONLY will put the cleaner thread to
1803e44163e1SJeff Mahoney 		 * sleep at the next loop if it's already active.
1804e44163e1SJeff Mahoney 		 * If it's already asleep, we'll leave unused block
1805e44163e1SJeff Mahoney 		 * groups on disk until we're mounted read-write again
1806e44163e1SJeff Mahoney 		 * unless we clean them up here.
1807e44163e1SJeff Mahoney 		 */
1808e44163e1SJeff Mahoney 		btrfs_delete_unused_bgs(fs_info);
1809e44163e1SJeff Mahoney 
18108dabb742SStefan Behrens 		btrfs_dev_replace_suspend_for_unmount(fs_info);
18118dabb742SStefan Behrens 		btrfs_scrub_cancel(fs_info);
1812061594efSMiao Xie 		btrfs_pause_balance(fs_info);
18138dabb742SStefan Behrens 
18146bccf3abSJeff Mahoney 		ret = btrfs_commit_super(fs_info);
181549b25e05SJeff Mahoney 		if (ret)
181649b25e05SJeff Mahoney 			goto restore;
1817c146afadSYan Zheng 	} else {
18180b246afaSJeff Mahoney 		if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
18196ef3de9cSDavid Sterba 			btrfs_err(fs_info,
1820efe120a0SFrank Holton 				"Remounting read-write after error is not allowed");
18216ef3de9cSDavid Sterba 			ret = -EINVAL;
18226ef3de9cSDavid Sterba 			goto restore;
18236ef3de9cSDavid Sterba 		}
18248a3db184SSergei Trofimovich 		if (fs_info->fs_devices->rw_devices == 0) {
182549b25e05SJeff Mahoney 			ret = -EACCES;
182649b25e05SJeff Mahoney 			goto restore;
18278a3db184SSergei Trofimovich 		}
18282b82032cSYan Zheng 
18296528b99dSAnand Jain 		if (!btrfs_check_rw_degradable(fs_info, NULL)) {
1830efe120a0SFrank Holton 			btrfs_warn(fs_info,
183152042d8eSAndrea Gelmini 		"too many missing devices, writable remount is not allowed");
1832292fd7fcSStefan Behrens 			ret = -EACCES;
1833292fd7fcSStefan Behrens 			goto restore;
1834292fd7fcSStefan Behrens 		}
1835292fd7fcSStefan Behrens 
18368a3db184SSergei Trofimovich 		if (btrfs_super_log_root(fs_info->super_copy) != 0) {
183710a3a3edSDavid Sterba 			btrfs_warn(fs_info,
183810a3a3edSDavid Sterba 		"mount required to replay tree-log, cannot remount read-write");
183949b25e05SJeff Mahoney 			ret = -EINVAL;
184049b25e05SJeff Mahoney 			goto restore;
18418a3db184SSergei Trofimovich 		}
1842c146afadSYan Zheng 
1843815745cfSAl Viro 		ret = btrfs_cleanup_fs_roots(fs_info);
184449b25e05SJeff Mahoney 		if (ret)
184549b25e05SJeff Mahoney 			goto restore;
1846c146afadSYan Zheng 
1847d68fc57bSYan, Zheng 		/* recover relocation */
18485f316481SWang Shilong 		mutex_lock(&fs_info->cleaner_mutex);
1849d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
18505f316481SWang Shilong 		mutex_unlock(&fs_info->cleaner_mutex);
185149b25e05SJeff Mahoney 		if (ret)
185249b25e05SJeff Mahoney 			goto restore;
1853c146afadSYan Zheng 
18542b6ba629SIlya Dryomov 		ret = btrfs_resume_balance_async(fs_info);
18552b6ba629SIlya Dryomov 		if (ret)
18562b6ba629SIlya Dryomov 			goto restore;
18572b6ba629SIlya Dryomov 
18588dabb742SStefan Behrens 		ret = btrfs_resume_dev_replace_async(fs_info);
18598dabb742SStefan Behrens 		if (ret) {
1860efe120a0SFrank Holton 			btrfs_warn(fs_info, "failed to resume dev_replace");
18618dabb742SStefan Behrens 			goto restore;
18628dabb742SStefan Behrens 		}
186394aebfb2SJosef Bacik 
18646c6b5a39SAleksa Sarai 		btrfs_qgroup_rescan_resume(fs_info);
18656c6b5a39SAleksa Sarai 
186694aebfb2SJosef Bacik 		if (!fs_info->uuid_root) {
1867efe120a0SFrank Holton 			btrfs_info(fs_info, "creating UUID tree");
186894aebfb2SJosef Bacik 			ret = btrfs_create_uuid_tree(fs_info);
186994aebfb2SJosef Bacik 			if (ret) {
18705d163e0eSJeff Mahoney 				btrfs_warn(fs_info,
18715d163e0eSJeff Mahoney 					   "failed to create the UUID tree %d",
18725d163e0eSJeff Mahoney 					   ret);
187394aebfb2SJosef Bacik 				goto restore;
187494aebfb2SJosef Bacik 			}
187594aebfb2SJosef Bacik 		}
18761751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_RDONLY;
187790c711abSZygo Blaxell 
1878afcdd129SJosef Bacik 		set_bit(BTRFS_FS_OPEN, &fs_info->flags);
1879c146afadSYan Zheng 	}
1880dc81cdc5SMiao Xie out:
18812c6a92b0SJustin Maggard 	wake_up_process(fs_info->transaction_kthread);
1882dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
1883c146afadSYan Zheng 	return 0;
188449b25e05SJeff Mahoney 
188549b25e05SJeff Mahoney restore:
18861751e8a6SLinus Torvalds 	/* We've hit an error - don't reset SB_RDONLY */
1887bc98a42cSDavid Howells 	if (sb_rdonly(sb))
18881751e8a6SLinus Torvalds 		old_flags |= SB_RDONLY;
188949b25e05SJeff Mahoney 	sb->s_flags = old_flags;
189049b25e05SJeff Mahoney 	fs_info->mount_opt = old_opts;
189149b25e05SJeff Mahoney 	fs_info->compress_type = old_compress_type;
189249b25e05SJeff Mahoney 	fs_info->max_inline = old_max_inline;
18930d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
18940d2450abSSergei Trofimovich 		old_thread_pool_size, fs_info->thread_pool_size);
189549b25e05SJeff Mahoney 	fs_info->metadata_ratio = old_metadata_ratio;
1896dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
189749b25e05SJeff Mahoney 	return ret;
1898c146afadSYan Zheng }
1899c146afadSYan Zheng 
1900bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
19017e17916bSArnd Bergmann static inline int btrfs_cmp_device_free_bytes(const void *dev_info1,
1902bcd53741SArne Jansen 				       const void *dev_info2)
1903bcd53741SArne Jansen {
1904bcd53741SArne Jansen 	if (((struct btrfs_device_info *)dev_info1)->max_avail >
1905bcd53741SArne Jansen 	    ((struct btrfs_device_info *)dev_info2)->max_avail)
1906bcd53741SArne Jansen 		return -1;
1907bcd53741SArne Jansen 	else if (((struct btrfs_device_info *)dev_info1)->max_avail <
1908bcd53741SArne Jansen 		 ((struct btrfs_device_info *)dev_info2)->max_avail)
1909bcd53741SArne Jansen 		return 1;
1910bcd53741SArne Jansen 	else
1911bcd53741SArne Jansen 	return 0;
1912bcd53741SArne Jansen }
1913bcd53741SArne Jansen 
1914bcd53741SArne Jansen /*
1915bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
1916bcd53741SArne Jansen  * is stored.(Descending Sort)
1917bcd53741SArne Jansen  */
1918bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
1919bcd53741SArne Jansen 					struct btrfs_device_info *devices,
1920bcd53741SArne Jansen 					size_t nr_devices)
1921bcd53741SArne Jansen {
1922bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1923bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
1924bcd53741SArne Jansen }
1925bcd53741SArne Jansen 
19266d07bcecSMiao Xie /*
19276d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
19286d07bcecSMiao Xie  * file data.
19296d07bcecSMiao Xie  */
19307e17916bSArnd Bergmann static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
19316bccf3abSJeff Mahoney 					      u64 *free_bytes)
19326d07bcecSMiao Xie {
19336d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
19346d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
19356d07bcecSMiao Xie 	struct btrfs_device *device;
19366d07bcecSMiao Xie 	u64 type;
19376d07bcecSMiao Xie 	u64 avail_space;
19386d07bcecSMiao Xie 	u64 min_stripe_size;
1939559ca6eaSNikolay Borisov 	int num_stripes = 1;
19406d07bcecSMiao Xie 	int i = 0, nr_devices;
19414f080f57SDavid Sterba 	const struct btrfs_raid_attr *rattr;
19426d07bcecSMiao Xie 
19437e33fd99SJosef Bacik 	/*
194401327610SNicholas D Steeves 	 * We aren't under the device list lock, so this is racy-ish, but good
19457e33fd99SJosef Bacik 	 * enough for our purposes.
19467e33fd99SJosef Bacik 	 */
1947b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
19487e33fd99SJosef Bacik 	if (!nr_devices) {
19497e33fd99SJosef Bacik 		smp_mb();
19507e33fd99SJosef Bacik 		nr_devices = fs_info->fs_devices->open_devices;
19517e33fd99SJosef Bacik 		ASSERT(nr_devices);
19527e33fd99SJosef Bacik 		if (!nr_devices) {
19537e33fd99SJosef Bacik 			*free_bytes = 0;
19547e33fd99SJosef Bacik 			return 0;
19557e33fd99SJosef Bacik 		}
19567e33fd99SJosef Bacik 	}
19576d07bcecSMiao Xie 
1958d9b0d9baSDulshani Gunawardhana 	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
19596a44517dSDavid Sterba 			       GFP_KERNEL);
19606d07bcecSMiao Xie 	if (!devices_info)
19616d07bcecSMiao Xie 		return -ENOMEM;
19626d07bcecSMiao Xie 
196301327610SNicholas D Steeves 	/* calc min stripe number for data space allocation */
19641b86826dSJeff Mahoney 	type = btrfs_data_alloc_profile(fs_info);
19654f080f57SDavid Sterba 	rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
19664f080f57SDavid Sterba 
1967e1ea2beeSDavid Sterba 	if (type & BTRFS_BLOCK_GROUP_RAID0)
196839fb26c3SMiao Xie 		num_stripes = nr_devices;
1969e1ea2beeSDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1)
197039fb26c3SMiao Xie 		num_stripes = 2;
197147e6f742SDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1C3)
197247e6f742SDavid Sterba 		num_stripes = 3;
19738d6fac00SDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1C4)
19748d6fac00SDavid Sterba 		num_stripes = 4;
1975e1ea2beeSDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID10)
197639fb26c3SMiao Xie 		num_stripes = 4;
19776d07bcecSMiao Xie 
19784f080f57SDavid Sterba 	/* Adjust for more than 1 stripe per device */
19794f080f57SDavid Sterba 	min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
19806d07bcecSMiao Xie 
19817e33fd99SJosef Bacik 	rcu_read_lock();
19827e33fd99SJosef Bacik 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
1983e12c9621SAnand Jain 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1984e12c9621SAnand Jain 						&device->dev_state) ||
1985401e29c1SAnand Jain 		    !device->bdev ||
1986401e29c1SAnand Jain 		    test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
19876d07bcecSMiao Xie 			continue;
19886d07bcecSMiao Xie 
19897e33fd99SJosef Bacik 		if (i >= nr_devices)
19907e33fd99SJosef Bacik 			break;
19917e33fd99SJosef Bacik 
19926d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
19936d07bcecSMiao Xie 
19946d07bcecSMiao Xie 		/* align with stripe_len */
1995559ca6eaSNikolay Borisov 		avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
19966d07bcecSMiao Xie 
19976d07bcecSMiao Xie 		/*
199801327610SNicholas D Steeves 		 * In order to avoid overwriting the superblock on the drive,
19996d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
20006d07bcecSMiao Xie 		 * allocation.
2001559ca6eaSNikolay Borisov 		 *
2002559ca6eaSNikolay Borisov 		 * This ensures we have at least min_stripe_size free space
2003559ca6eaSNikolay Borisov 		 * after excluding 1MB.
20046d07bcecSMiao Xie 		 */
2005559ca6eaSNikolay Borisov 		if (avail_space <= SZ_1M + min_stripe_size)
20066d07bcecSMiao Xie 			continue;
20076d07bcecSMiao Xie 
2008559ca6eaSNikolay Borisov 		avail_space -= SZ_1M;
2009559ca6eaSNikolay Borisov 
20106d07bcecSMiao Xie 		devices_info[i].dev = device;
20116d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
20126d07bcecSMiao Xie 
20136d07bcecSMiao Xie 		i++;
20146d07bcecSMiao Xie 	}
20157e33fd99SJosef Bacik 	rcu_read_unlock();
20166d07bcecSMiao Xie 
20176d07bcecSMiao Xie 	nr_devices = i;
20186d07bcecSMiao Xie 
20196d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
20206d07bcecSMiao Xie 
20216d07bcecSMiao Xie 	i = nr_devices - 1;
20226d07bcecSMiao Xie 	avail_space = 0;
2023559ca6eaSNikolay Borisov 	while (nr_devices >= rattr->devs_min) {
2024559ca6eaSNikolay Borisov 		num_stripes = min(num_stripes, nr_devices);
202539fb26c3SMiao Xie 
20266d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
20276d07bcecSMiao Xie 			int j;
20286d07bcecSMiao Xie 			u64 alloc_size;
20296d07bcecSMiao Xie 
203039fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
20316d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
203239fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
20336d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
20346d07bcecSMiao Xie 		}
20356d07bcecSMiao Xie 		i--;
20366d07bcecSMiao Xie 		nr_devices--;
20376d07bcecSMiao Xie 	}
20386d07bcecSMiao Xie 
20396d07bcecSMiao Xie 	kfree(devices_info);
20406d07bcecSMiao Xie 	*free_bytes = avail_space;
20416d07bcecSMiao Xie 	return 0;
20426d07bcecSMiao Xie }
20436d07bcecSMiao Xie 
2044ba7b6e62SDavid Sterba /*
2045ba7b6e62SDavid Sterba  * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2046ba7b6e62SDavid Sterba  *
2047ba7b6e62SDavid Sterba  * If there's a redundant raid level at DATA block groups, use the respective
2048ba7b6e62SDavid Sterba  * multiplier to scale the sizes.
2049ba7b6e62SDavid Sterba  *
2050ba7b6e62SDavid Sterba  * Unused device space usage is based on simulating the chunk allocator
20510d0c71b3SDavid Sterba  * algorithm that respects the device sizes and order of allocations.  This is
20520d0c71b3SDavid Sterba  * a close approximation of the actual use but there are other factors that may
20530d0c71b3SDavid Sterba  * change the result (like a new metadata chunk).
2054ba7b6e62SDavid Sterba  *
2055ca8a51b3SDavid Sterba  * If metadata is exhausted, f_bavail will be 0.
2056ba7b6e62SDavid Sterba  */
20578fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
20588fd17795SChris Mason {
2059815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
2060815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
2061bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
2062bd4d1088SJosef Bacik 	u64 total_used = 0;
20636d07bcecSMiao Xie 	u64 total_free_data = 0;
2064ca8a51b3SDavid Sterba 	u64 total_free_meta = 0;
2065db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
2066de37aa51SNikolay Borisov 	__be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
2067ba7b6e62SDavid Sterba 	unsigned factor = 1;
2068ba7b6e62SDavid Sterba 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
20696d07bcecSMiao Xie 	int ret;
2070ca8a51b3SDavid Sterba 	u64 thresh = 0;
2071ae02d1bdSLuis de Bethencourt 	int mixed = 0;
20728fd17795SChris Mason 
2073bd4d1088SJosef Bacik 	rcu_read_lock();
2074f5389f33SJohannes Thumshirn 	list_for_each_entry_rcu(found, &fs_info->space_info, list) {
20756d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
2076ba7b6e62SDavid Sterba 			int i;
2077ba7b6e62SDavid Sterba 
20786d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
20796d07bcecSMiao Xie 			total_free_data -=
20806d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
2081ba7b6e62SDavid Sterba 
2082ba7b6e62SDavid Sterba 			for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
208346df06b8SDavid Sterba 				if (!list_empty(&found->block_groups[i]))
208446df06b8SDavid Sterba 					factor = btrfs_bg_type_to_factor(
208546df06b8SDavid Sterba 						btrfs_raid_array[i].bg_flag);
2086ba7b6e62SDavid Sterba 			}
20876d07bcecSMiao Xie 		}
2088ae02d1bdSLuis de Bethencourt 
2089ae02d1bdSLuis de Bethencourt 		/*
2090ae02d1bdSLuis de Bethencourt 		 * Metadata in mixed block goup profiles are accounted in data
2091ae02d1bdSLuis de Bethencourt 		 */
2092ae02d1bdSLuis de Bethencourt 		if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
2093ae02d1bdSLuis de Bethencourt 			if (found->flags & BTRFS_BLOCK_GROUP_DATA)
2094ae02d1bdSLuis de Bethencourt 				mixed = 1;
2095ae02d1bdSLuis de Bethencourt 			else
2096ae02d1bdSLuis de Bethencourt 				total_free_meta += found->disk_total -
2097ae02d1bdSLuis de Bethencourt 					found->disk_used;
2098ae02d1bdSLuis de Bethencourt 		}
20996d07bcecSMiao Xie 
2100b742bb82SYan, Zheng 		total_used += found->disk_used;
210189a55897SJosef Bacik 	}
2102ba7b6e62SDavid Sterba 
2103bd4d1088SJosef Bacik 	rcu_read_unlock();
2104bd4d1088SJosef Bacik 
2105ba7b6e62SDavid Sterba 	buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
2106ba7b6e62SDavid Sterba 	buf->f_blocks >>= bits;
2107ba7b6e62SDavid Sterba 	buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
2108ba7b6e62SDavid Sterba 
2109ba7b6e62SDavid Sterba 	/* Account global block reserve as used, it's in logical size already */
2110ba7b6e62SDavid Sterba 	spin_lock(&block_rsv->lock);
211141b34accSLuis de Bethencourt 	/* Mixed block groups accounting is not byte-accurate, avoid overflow */
211241b34accSLuis de Bethencourt 	if (buf->f_bfree >= block_rsv->size >> bits)
2113ba7b6e62SDavid Sterba 		buf->f_bfree -= block_rsv->size >> bits;
211441b34accSLuis de Bethencourt 	else
211541b34accSLuis de Bethencourt 		buf->f_bfree = 0;
2116ba7b6e62SDavid Sterba 	spin_unlock(&block_rsv->lock);
2117ba7b6e62SDavid Sterba 
21180d95c1beSDavid Sterba 	buf->f_bavail = div_u64(total_free_data, factor);
21196bccf3abSJeff Mahoney 	ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
21207e33fd99SJosef Bacik 	if (ret)
21216d07bcecSMiao Xie 		return ret;
2122ba7b6e62SDavid Sterba 	buf->f_bavail += div_u64(total_free_data, factor);
21236d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
2124d397712bSChris Mason 
2125ca8a51b3SDavid Sterba 	/*
2126ca8a51b3SDavid Sterba 	 * We calculate the remaining metadata space minus global reserve. If
2127ca8a51b3SDavid Sterba 	 * this is (supposedly) smaller than zero, there's no space. But this
2128ca8a51b3SDavid Sterba 	 * does not hold in practice, the exhausted state happens where's still
2129ca8a51b3SDavid Sterba 	 * some positive delta. So we apply some guesswork and compare the
2130ca8a51b3SDavid Sterba 	 * delta to a 4M threshold.  (Practically observed delta was ~2M.)
2131ca8a51b3SDavid Sterba 	 *
2132ca8a51b3SDavid Sterba 	 * We probably cannot calculate the exact threshold value because this
2133ca8a51b3SDavid Sterba 	 * depends on the internal reservations requested by various
2134ca8a51b3SDavid Sterba 	 * operations, so some operations that consume a few metadata will
2135ca8a51b3SDavid Sterba 	 * succeed even if the Avail is zero. But this is better than the other
2136ca8a51b3SDavid Sterba 	 * way around.
2137ca8a51b3SDavid Sterba 	 */
2138d4417e22SNikolay Borisov 	thresh = SZ_4M;
2139ca8a51b3SDavid Sterba 
2140d55966c4SJosef Bacik 	/*
2141d55966c4SJosef Bacik 	 * We only want to claim there's no available space if we can no longer
2142d55966c4SJosef Bacik 	 * allocate chunks for our metadata profile and our global reserve will
2143d55966c4SJosef Bacik 	 * not fit in the free metadata space.  If we aren't ->full then we
2144d55966c4SJosef Bacik 	 * still can allocate chunks and thus are fine using the currently
2145d55966c4SJosef Bacik 	 * calculated f_bavail.
2146d55966c4SJosef Bacik 	 */
2147d55966c4SJosef Bacik 	if (!mixed && block_rsv->space_info->full &&
2148d55966c4SJosef Bacik 	    total_free_meta - thresh < block_rsv->size)
2149ca8a51b3SDavid Sterba 		buf->f_bavail = 0;
2150ca8a51b3SDavid Sterba 
2151ba7b6e62SDavid Sterba 	buf->f_type = BTRFS_SUPER_MAGIC;
2152ba7b6e62SDavid Sterba 	buf->f_bsize = dentry->d_sb->s_blocksize;
2153ba7b6e62SDavid Sterba 	buf->f_namelen = BTRFS_NAME_LEN;
2154ba7b6e62SDavid Sterba 
21559d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
21569d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
21579d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
21589d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
21599d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
216032d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
21614fd786e6SMisono Tomohiro 	buf->f_fsid.val[0] ^=
21624fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32;
21634fd786e6SMisono Tomohiro 	buf->f_fsid.val[1] ^=
21644fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid;
216532d48fa1SDavid Woodhouse 
21668fd17795SChris Mason 	return 0;
21678fd17795SChris Mason }
2168b5133862SChris Mason 
2169aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
2170aea52e19SAl Viro {
2171815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2172aea52e19SAl Viro 	kill_anon_super(sb);
2173aea52e19SAl Viro 	free_fs_info(fs_info);
2174aea52e19SAl Viro }
2175aea52e19SAl Viro 
21762e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
21772e635a27SChris Mason 	.owner		= THIS_MODULE,
21782e635a27SChris Mason 	.name		= "btrfs",
2179061dbc6bSAl Viro 	.mount		= btrfs_mount,
2180aea52e19SAl Viro 	.kill_sb	= btrfs_kill_super,
2181f667aef6SQu Wenruo 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
21822e635a27SChris Mason };
218372fa39f5SMisono, Tomohiro 
218472fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type = {
218572fa39f5SMisono, Tomohiro 	.owner		= THIS_MODULE,
218672fa39f5SMisono, Tomohiro 	.name		= "btrfs",
218772fa39f5SMisono, Tomohiro 	.mount		= btrfs_mount_root,
218872fa39f5SMisono, Tomohiro 	.kill_sb	= btrfs_kill_super,
218972fa39f5SMisono, Tomohiro 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
219072fa39f5SMisono, Tomohiro };
219172fa39f5SMisono, Tomohiro 
21927f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs");
2193a9218f6bSChris Mason 
2194d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file)
2195d8620958STom Van Braeckel {
2196d8620958STom Van Braeckel 	/*
2197d8620958STom Van Braeckel 	 * The control file's private_data is used to hold the
2198d8620958STom Van Braeckel 	 * transaction when it is started and is used to keep
2199d8620958STom Van Braeckel 	 * track of whether a transaction is already in progress.
2200d8620958STom Van Braeckel 	 */
2201d8620958STom Van Braeckel 	file->private_data = NULL;
2202d8620958STom Van Braeckel 	return 0;
2203d8620958STom Van Braeckel }
2204d8620958STom Van Braeckel 
2205d352ac68SChris Mason /*
2206cfe953c8SSu Yue  * Used by /dev/btrfs-control for devices ioctls.
2207d352ac68SChris Mason  */
22088a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
22098a4b83ccSChris Mason 				unsigned long arg)
22108a4b83ccSChris Mason {
22118a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
221236350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
2213c071fcfdSChris Mason 	int ret = -ENOTTY;
22148a4b83ccSChris Mason 
2215e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2216e441d54dSChris Mason 		return -EPERM;
2217e441d54dSChris Mason 
2218dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
2219dae7b665SLi Zefan 	if (IS_ERR(vol))
2220dae7b665SLi Zefan 		return PTR_ERR(vol);
2221f505754fSFilipe Manana 	vol->name[BTRFS_PATH_NAME_MAX] = '\0';
2222c071fcfdSChris Mason 
22238a4b83ccSChris Mason 	switch (cmd) {
22248a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
2225899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
222636350e95SGu Jinxiang 		device = btrfs_scan_one_device(vol->name, FMODE_READ,
222736350e95SGu Jinxiang 					       &btrfs_root_fs_type);
222836350e95SGu Jinxiang 		ret = PTR_ERR_OR_ZERO(device);
2229899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
22308a4b83ccSChris Mason 		break;
2231228a73abSAnand Jain 	case BTRFS_IOC_FORGET_DEV:
2232228a73abSAnand Jain 		ret = btrfs_forget_devices(vol->name);
2233228a73abSAnand Jain 		break;
223402db0844SJosef Bacik 	case BTRFS_IOC_DEVICES_READY:
2235899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
223636350e95SGu Jinxiang 		device = btrfs_scan_one_device(vol->name, FMODE_READ,
223736350e95SGu Jinxiang 					       &btrfs_root_fs_type);
223836350e95SGu Jinxiang 		if (IS_ERR(device)) {
2239899f9307SDavid Sterba 			mutex_unlock(&uuid_mutex);
224036350e95SGu Jinxiang 			ret = PTR_ERR(device);
224102db0844SJosef Bacik 			break;
2242899f9307SDavid Sterba 		}
224336350e95SGu Jinxiang 		ret = !(device->fs_devices->num_devices ==
224436350e95SGu Jinxiang 			device->fs_devices->total_devices);
2245899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
224602db0844SJosef Bacik 		break;
2247c5868f83SDavid Sterba 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2248d5131b65SDavid Sterba 		ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2249c5868f83SDavid Sterba 		break;
22508a4b83ccSChris Mason 	}
2251dae7b665SLi Zefan 
22528a4b83ccSChris Mason 	kfree(vol);
2253f819d837SLinda Knippers 	return ret;
22548a4b83ccSChris Mason }
22558a4b83ccSChris Mason 
22560176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
2257ed0dab6bSYan {
2258354aa0fbSMiao Xie 	struct btrfs_trans_handle *trans;
22590b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
22600b246afaSJeff Mahoney 	struct btrfs_root *root = fs_info->tree_root;
2261354aa0fbSMiao Xie 
2262fac03c8dSDavid Sterba 	set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
22639e7cc91aSWang Xiaoguang 	/*
22649e7cc91aSWang Xiaoguang 	 * We don't need a barrier here, we'll wait for any transaction that
22659e7cc91aSWang Xiaoguang 	 * could be in progress on other threads (and do delayed iputs that
22669e7cc91aSWang Xiaoguang 	 * we want to avoid on a frozen filesystem), or do the commit
22679e7cc91aSWang Xiaoguang 	 * ourselves.
22689e7cc91aSWang Xiaoguang 	 */
2269d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
2270354aa0fbSMiao Xie 	if (IS_ERR(trans)) {
2271354aa0fbSMiao Xie 		/* no transaction, don't bother */
2272354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
22730176260fSLinus Torvalds 			return 0;
2274354aa0fbSMiao Xie 		return PTR_ERR(trans);
2275354aa0fbSMiao Xie 	}
22763a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
2277ed0dab6bSYan }
2278ed0dab6bSYan 
22799e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb)
22809e7cc91aSWang Xiaoguang {
2281fac03c8dSDavid Sterba 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2282fac03c8dSDavid Sterba 
2283fac03c8dSDavid Sterba 	clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
22849e7cc91aSWang Xiaoguang 	return 0;
22859e7cc91aSWang Xiaoguang }
22869e7cc91aSWang Xiaoguang 
22879c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
22889c5085c1SJosef Bacik {
22899c5085c1SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
22909c5085c1SJosef Bacik 	struct btrfs_fs_devices *cur_devices;
22919c5085c1SJosef Bacik 	struct btrfs_device *dev, *first_dev = NULL;
22929c5085c1SJosef Bacik 	struct list_head *head;
22939c5085c1SJosef Bacik 
229488c14590SDavid Sterba 	/*
229588c14590SDavid Sterba 	 * Lightweight locking of the devices. We should not need
229688c14590SDavid Sterba 	 * device_list_mutex here as we only read the device data and the list
229788c14590SDavid Sterba 	 * is protected by RCU.  Even if a device is deleted during the list
229888c14590SDavid Sterba 	 * traversals, we'll get valid data, the freeing callback will wait at
229952042d8eSAndrea Gelmini 	 * least until the rcu_read_unlock.
230088c14590SDavid Sterba 	 */
230188c14590SDavid Sterba 	rcu_read_lock();
23029c5085c1SJosef Bacik 	cur_devices = fs_info->fs_devices;
23039c5085c1SJosef Bacik 	while (cur_devices) {
23049c5085c1SJosef Bacik 		head = &cur_devices->devices;
230588c14590SDavid Sterba 		list_for_each_entry_rcu(dev, head, dev_list) {
2306e6e674bdSAnand Jain 			if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
2307aa9ddcd4SJosef Bacik 				continue;
23080aeb8a6eSAnand Jain 			if (!dev->name)
23090aeb8a6eSAnand Jain 				continue;
23109c5085c1SJosef Bacik 			if (!first_dev || dev->devid < first_dev->devid)
23119c5085c1SJosef Bacik 				first_dev = dev;
23129c5085c1SJosef Bacik 		}
23139c5085c1SJosef Bacik 		cur_devices = cur_devices->seed;
23149c5085c1SJosef Bacik 	}
23159c5085c1SJosef Bacik 
2316672d5990SMisono Tomohiro 	if (first_dev)
2317672d5990SMisono Tomohiro 		seq_escape(m, rcu_str_deref(first_dev->name), " \t\n\\");
2318672d5990SMisono Tomohiro 	else
23199c5085c1SJosef Bacik 		WARN_ON(1);
232088c14590SDavid Sterba 	rcu_read_unlock();
23219c5085c1SJosef Bacik 	return 0;
23229c5085c1SJosef Bacik }
23239c5085c1SJosef Bacik 
2324b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
232576dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
2326bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
2327e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
2328d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
2329a9572a15SEric Paris 	.show_options	= btrfs_show_options,
23309c5085c1SJosef Bacik 	.show_devname	= btrfs_show_devname,
23312c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
23322c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
233326602cabSAl Viro 	.free_inode	= btrfs_free_inode,
23348fd17795SChris Mason 	.statfs		= btrfs_statfs,
2335c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
23360176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
23379e7cc91aSWang Xiaoguang 	.unfreeze_fs	= btrfs_unfreeze,
2338e20d96d6SChris Mason };
2339a9218f6bSChris Mason 
2340a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
2341d8620958STom Van Braeckel 	.open = btrfs_control_open,
2342a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
23431832f2d8SArnd Bergmann 	.compat_ioctl = compat_ptr_ioctl,
2344a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
23456038f373SArnd Bergmann 	.llseek = noop_llseek,
2346a9218f6bSChris Mason };
2347a9218f6bSChris Mason 
2348a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
2349578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
2350a9218f6bSChris Mason 	.name		= "btrfs-control",
2351a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
2352a9218f6bSChris Mason };
2353a9218f6bSChris Mason 
2354578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2355578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
2356578454ffSKay Sievers 
2357f5c29bd9SLiu Bo static int __init btrfs_interface_init(void)
2358a9218f6bSChris Mason {
2359a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
2360a9218f6bSChris Mason }
2361a9218f6bSChris Mason 
2362e67c718bSDavid Sterba static __cold void btrfs_interface_exit(void)
2363a9218f6bSChris Mason {
2364f368ed60SGreg Kroah-Hartman 	misc_deregister(&btrfs_misc);
2365a9218f6bSChris Mason }
2366a9218f6bSChris Mason 
2367f5c29bd9SLiu Bo static void __init btrfs_print_mod_info(void)
236885965600SDavid Sterba {
2369edf57cbfSBart Van Assche 	static const char options[] = ""
237085965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
237185965600SDavid Sterba 			", debug=on"
237285965600SDavid Sterba #endif
237379556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT
237479556c3dSStefan Behrens 			", assert=on"
237579556c3dSStefan Behrens #endif
237685965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
237785965600SDavid Sterba 			", integrity-checker=on"
237885965600SDavid Sterba #endif
2379fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2380fb592373SJosef Bacik 			", ref-verify=on"
2381fb592373SJosef Bacik #endif
2382edf57cbfSBart Van Assche 			;
2383edf57cbfSBart Van Assche 	pr_info("Btrfs loaded, crc32c=%s%s\n", crc32c_impl(), options);
238485965600SDavid Sterba }
238585965600SDavid Sterba 
23862e635a27SChris Mason static int __init init_btrfs_fs(void)
23872e635a27SChris Mason {
23882c90e5d6SChris Mason 	int err;
238958176a96SJosef Bacik 
239063541927SFilipe David Borba Manana 	btrfs_props_init();
239163541927SFilipe David Borba Manana 
239258176a96SJosef Bacik 	err = btrfs_init_sysfs();
239358176a96SJosef Bacik 	if (err)
23949678c543SNikolay Borisov 		return err;
239558176a96SJosef Bacik 
2396143bede5SJeff Mahoney 	btrfs_init_compress();
2397d1310b2eSChris Mason 
2398261507a0SLi Zefan 	err = btrfs_init_cachep();
2399261507a0SLi Zefan 	if (err)
2400261507a0SLi Zefan 		goto free_compress;
2401261507a0SLi Zefan 
2402d1310b2eSChris Mason 	err = extent_io_init();
24032f4cbe64SWyatt Banks 	if (err)
24042f4cbe64SWyatt Banks 		goto free_cachep;
24052f4cbe64SWyatt Banks 
24066f0d04f8SJosef Bacik 	err = extent_state_cache_init();
2407d1310b2eSChris Mason 	if (err)
2408d1310b2eSChris Mason 		goto free_extent_io;
2409d1310b2eSChris Mason 
24106f0d04f8SJosef Bacik 	err = extent_map_init();
24116f0d04f8SJosef Bacik 	if (err)
24126f0d04f8SJosef Bacik 		goto free_extent_state_cache;
24136f0d04f8SJosef Bacik 
24146352b91dSMiao Xie 	err = ordered_data_init();
24152f4cbe64SWyatt Banks 	if (err)
24162f4cbe64SWyatt Banks 		goto free_extent_map;
2417c8b97818SChris Mason 
24186352b91dSMiao Xie 	err = btrfs_delayed_inode_init();
24196352b91dSMiao Xie 	if (err)
24206352b91dSMiao Xie 		goto free_ordered_data;
24216352b91dSMiao Xie 
24229247f317SMiao Xie 	err = btrfs_auto_defrag_init();
242316cdcec7SMiao Xie 	if (err)
242416cdcec7SMiao Xie 		goto free_delayed_inode;
242516cdcec7SMiao Xie 
242678a6184aSMiao Xie 	err = btrfs_delayed_ref_init();
24279247f317SMiao Xie 	if (err)
24289247f317SMiao Xie 		goto free_auto_defrag;
24299247f317SMiao Xie 
2430b9e9a6cbSWang Shilong 	err = btrfs_prelim_ref_init();
2431b9e9a6cbSWang Shilong 	if (err)
2432af13b492SDavid Sterba 		goto free_delayed_ref;
2433b9e9a6cbSWang Shilong 
243497eb6b69SDavid Sterba 	err = btrfs_end_io_wq_init();
243578a6184aSMiao Xie 	if (err)
2436af13b492SDavid Sterba 		goto free_prelim_ref;
243778a6184aSMiao Xie 
243897eb6b69SDavid Sterba 	err = btrfs_interface_init();
243997eb6b69SDavid Sterba 	if (err)
244097eb6b69SDavid Sterba 		goto free_end_io_wq;
244197eb6b69SDavid Sterba 
2442e565d4b9SJan Schmidt 	btrfs_init_lockdep();
2443e565d4b9SJan Schmidt 
24448ae1af3cSAnand Jain 	btrfs_print_mod_info();
2445dc11dd5dSJosef Bacik 
2446dc11dd5dSJosef Bacik 	err = btrfs_run_sanity_tests();
2447dc11dd5dSJosef Bacik 	if (err)
2448dc11dd5dSJosef Bacik 		goto unregister_ioctl;
2449dc11dd5dSJosef Bacik 
2450dc11dd5dSJosef Bacik 	err = register_filesystem(&btrfs_fs_type);
2451dc11dd5dSJosef Bacik 	if (err)
2452dc11dd5dSJosef Bacik 		goto unregister_ioctl;
245374255aa0SJosef Bacik 
24542f4cbe64SWyatt Banks 	return 0;
24552f4cbe64SWyatt Banks 
2456a9218f6bSChris Mason unregister_ioctl:
2457a9218f6bSChris Mason 	btrfs_interface_exit();
245897eb6b69SDavid Sterba free_end_io_wq:
245997eb6b69SDavid Sterba 	btrfs_end_io_wq_exit();
2460b9e9a6cbSWang Shilong free_prelim_ref:
2461b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
246278a6184aSMiao Xie free_delayed_ref:
246378a6184aSMiao Xie 	btrfs_delayed_ref_exit();
24649247f317SMiao Xie free_auto_defrag:
24659247f317SMiao Xie 	btrfs_auto_defrag_exit();
246616cdcec7SMiao Xie free_delayed_inode:
246716cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
24686352b91dSMiao Xie free_ordered_data:
24696352b91dSMiao Xie 	ordered_data_exit();
24702f4cbe64SWyatt Banks free_extent_map:
24712f4cbe64SWyatt Banks 	extent_map_exit();
24726f0d04f8SJosef Bacik free_extent_state_cache:
24736f0d04f8SJosef Bacik 	extent_state_cache_exit();
2474d1310b2eSChris Mason free_extent_io:
2475d1310b2eSChris Mason 	extent_io_exit();
24762f4cbe64SWyatt Banks free_cachep:
24772f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
2478261507a0SLi Zefan free_compress:
2479261507a0SLi Zefan 	btrfs_exit_compress();
24802f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
24819678c543SNikolay Borisov 
24822c90e5d6SChris Mason 	return err;
24832e635a27SChris Mason }
24842e635a27SChris Mason 
24852e635a27SChris Mason static void __exit exit_btrfs_fs(void)
24862e635a27SChris Mason {
248739279cc3SChris Mason 	btrfs_destroy_cachep();
248878a6184aSMiao Xie 	btrfs_delayed_ref_exit();
24899247f317SMiao Xie 	btrfs_auto_defrag_exit();
249016cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
2491b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
24926352b91dSMiao Xie 	ordered_data_exit();
2493a52d9a80SChris Mason 	extent_map_exit();
24946f0d04f8SJosef Bacik 	extent_state_cache_exit();
2495d1310b2eSChris Mason 	extent_io_exit();
2496a9218f6bSChris Mason 	btrfs_interface_exit();
24975ed5f588SJosef Bacik 	btrfs_end_io_wq_exit();
24982e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
249958176a96SJosef Bacik 	btrfs_exit_sysfs();
25008a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
2501261507a0SLi Zefan 	btrfs_exit_compress();
25022e635a27SChris Mason }
25032e635a27SChris Mason 
250460efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs);
25052e635a27SChris Mason module_exit(exit_btrfs_fs)
25062e635a27SChris Mason 
25072e635a27SChris Mason MODULE_LICENSE("GPL");
2508d5178578SJohannes Thumshirn MODULE_SOFTDEP("pre: crc32c");
25093951e7f0SJohannes Thumshirn MODULE_SOFTDEP("pre: xxhash64");
25103831bf00SJohannes Thumshirn MODULE_SOFTDEP("pre: sha256");
2511352ae07bSDavid Sterba MODULE_SOFTDEP("pre: blake2b-256");
2512