xref: /linux/fs/btrfs/super.c (revision c0c907a47dccf2cf26251a8fb4a8e7a3bf79ce84)
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 
247bf31f87fSDavid Sterba 	WRITE_ONCE(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 
1027*c0c907a4SMarcos Paulo de Souza char *btrfs_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;
10315168489aSJosef Bacik 	struct btrfs_root *fs_root = NULL;
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;
10993619c94fSJosef 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);
11025168489aSJosef Bacik 			fs_root = NULL;
11035168489aSJosef Bacik 			goto err;
11045168489aSJosef Bacik 		}
110505dbe683SOmar Sandoval 
110605dbe683SOmar Sandoval 		/*
110705dbe683SOmar Sandoval 		 * Walk up the filesystem tree by inode refs until we hit the
110805dbe683SOmar Sandoval 		 * root directory.
110905dbe683SOmar Sandoval 		 */
111005dbe683SOmar Sandoval 		while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
111105dbe683SOmar Sandoval 			key.objectid = dirid;
111205dbe683SOmar Sandoval 			key.type = BTRFS_INODE_REF_KEY;
111305dbe683SOmar Sandoval 			key.offset = (u64)-1;
111405dbe683SOmar Sandoval 
111505dbe683SOmar Sandoval 			ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
111605dbe683SOmar Sandoval 			if (ret < 0) {
111705dbe683SOmar Sandoval 				goto err;
111805dbe683SOmar Sandoval 			} else if (ret > 0) {
111905dbe683SOmar Sandoval 				ret = btrfs_previous_item(fs_root, path, dirid,
112005dbe683SOmar Sandoval 							  BTRFS_INODE_REF_KEY);
112105dbe683SOmar Sandoval 				if (ret < 0) {
112205dbe683SOmar Sandoval 					goto err;
112305dbe683SOmar Sandoval 				} else if (ret > 0) {
112405dbe683SOmar Sandoval 					ret = -ENOENT;
112505dbe683SOmar Sandoval 					goto err;
112605dbe683SOmar Sandoval 				}
112705dbe683SOmar Sandoval 			}
112805dbe683SOmar Sandoval 
112905dbe683SOmar Sandoval 			btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
113005dbe683SOmar Sandoval 			dirid = key.offset;
113105dbe683SOmar Sandoval 
113205dbe683SOmar Sandoval 			inode_ref = btrfs_item_ptr(path->nodes[0],
113305dbe683SOmar Sandoval 						   path->slots[0],
113405dbe683SOmar Sandoval 						   struct btrfs_inode_ref);
113505dbe683SOmar Sandoval 			len = btrfs_inode_ref_name_len(path->nodes[0],
113605dbe683SOmar Sandoval 						       inode_ref);
113705dbe683SOmar Sandoval 			ptr -= len + 1;
113805dbe683SOmar Sandoval 			if (ptr < name) {
113905dbe683SOmar Sandoval 				ret = -ENAMETOOLONG;
114005dbe683SOmar Sandoval 				goto err;
114105dbe683SOmar Sandoval 			}
114205dbe683SOmar Sandoval 			read_extent_buffer(path->nodes[0], ptr + 1,
114305dbe683SOmar Sandoval 					   (unsigned long)(inode_ref + 1), len);
114405dbe683SOmar Sandoval 			ptr[0] = '/';
114505dbe683SOmar Sandoval 			btrfs_release_path(path);
114605dbe683SOmar Sandoval 		}
114700246528SJosef Bacik 		btrfs_put_root(fs_root);
11485168489aSJosef Bacik 		fs_root = NULL;
114905dbe683SOmar Sandoval 	}
115005dbe683SOmar Sandoval 
115105dbe683SOmar Sandoval 	btrfs_free_path(path);
115205dbe683SOmar Sandoval 	if (ptr == name + PATH_MAX - 1) {
115305dbe683SOmar Sandoval 		name[0] = '/';
115405dbe683SOmar Sandoval 		name[1] = '\0';
115505dbe683SOmar Sandoval 	} else {
115605dbe683SOmar Sandoval 		memmove(name, ptr, name + PATH_MAX - ptr);
115705dbe683SOmar Sandoval 	}
115805dbe683SOmar Sandoval 	return name;
115905dbe683SOmar Sandoval 
116005dbe683SOmar Sandoval err:
116100246528SJosef Bacik 	btrfs_put_root(fs_root);
116205dbe683SOmar Sandoval 	btrfs_free_path(path);
116305dbe683SOmar Sandoval 	kfree(name);
116405dbe683SOmar Sandoval 	return ERR_PTR(ret);
116505dbe683SOmar Sandoval }
116605dbe683SOmar Sandoval 
116705dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
116805dbe683SOmar Sandoval {
116905dbe683SOmar Sandoval 	struct btrfs_root *root = fs_info->tree_root;
117073f73415SJosef Bacik 	struct btrfs_dir_item *di;
117173f73415SJosef Bacik 	struct btrfs_path *path;
117273f73415SJosef Bacik 	struct btrfs_key location;
117373f73415SJosef Bacik 	u64 dir_id;
117473f73415SJosef Bacik 
117573f73415SJosef Bacik 	path = btrfs_alloc_path();
117673f73415SJosef Bacik 	if (!path)
117705dbe683SOmar Sandoval 		return -ENOMEM;
117873f73415SJosef Bacik 	path->leave_spinning = 1;
117973f73415SJosef Bacik 
118073f73415SJosef Bacik 	/*
118173f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
118273f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
118373f73415SJosef Bacik 	 * to mount.
118473f73415SJosef Bacik 	 */
1185815745cfSAl Viro 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
118673f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
1187b0839166SJulia Lawall 	if (IS_ERR(di)) {
1188b0839166SJulia Lawall 		btrfs_free_path(path);
118905dbe683SOmar Sandoval 		return PTR_ERR(di);
1190b0839166SJulia Lawall 	}
119173f73415SJosef Bacik 	if (!di) {
119273f73415SJosef Bacik 		/*
119373f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
119473f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
119505dbe683SOmar Sandoval 		 * mount the top-level subvolume.
119673f73415SJosef Bacik 		 */
119773f73415SJosef Bacik 		btrfs_free_path(path);
119805dbe683SOmar Sandoval 		*objectid = BTRFS_FS_TREE_OBJECTID;
119905dbe683SOmar Sandoval 		return 0;
120073f73415SJosef Bacik 	}
120173f73415SJosef Bacik 
120273f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
120373f73415SJosef Bacik 	btrfs_free_path(path);
120405dbe683SOmar Sandoval 	*objectid = location.objectid;
120505dbe683SOmar Sandoval 	return 0;
120673f73415SJosef Bacik }
120773f73415SJosef Bacik 
12088a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
12098a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
121056e033a7SDavid Sterba 			    void *data)
12112e635a27SChris Mason {
12122e635a27SChris Mason 	struct inode *inode;
1213815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
12145d4f98a2SYan Zheng 	struct btrfs_key key;
121539279cc3SChris Mason 	int err;
12162e635a27SChris Mason 
12172e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
12182e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
1219e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
1220af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
1221be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
12225103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
12232e635a27SChris Mason 	sb->s_time_gran = 1;
12240eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
12251751e8a6SLinus Torvalds 	sb->s_flags |= SB_POSIXACL;
122649cf6f45SChris Ball #endif
1227357fdad0SMatthew Garrett 	sb->s_flags |= SB_I_VERSION;
1228da2f0f74SChris Mason 	sb->s_iflags |= SB_I_CGROUPWB;
12299e11ceeeSJan Kara 
12309e11ceeeSJan Kara 	err = super_setup_bdi(sb);
12319e11ceeeSJan Kara 	if (err) {
12329e11ceeeSJan Kara 		btrfs_err(fs_info, "super_setup_bdi failed");
12339e11ceeeSJan Kara 		return err;
12349e11ceeeSJan Kara 	}
12359e11ceeeSJan Kara 
1236ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
1237ad2b2c80SAl Viro 	if (err) {
1238ab8d0fc4SJeff Mahoney 		btrfs_err(fs_info, "open_ctree failed");
1239ad2b2c80SAl Viro 		return err;
1240e20d96d6SChris Mason 	}
1241b888db2bSChris Mason 
12425d4f98a2SYan Zheng 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
12435d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
12445d4f98a2SYan Zheng 	key.offset = 0;
12454c66e0d4SDavid Sterba 	inode = btrfs_iget(sb, &key, fs_info->fs_root);
12465d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
12475d4f98a2SYan Zheng 		err = PTR_ERR(inode);
124839279cc3SChris Mason 		goto fail_close;
124939279cc3SChris Mason 	}
12502e635a27SChris Mason 
125148fde701SAl Viro 	sb->s_root = d_make_root(inode);
125248fde701SAl Viro 	if (!sb->s_root) {
125339279cc3SChris Mason 		err = -ENOMEM;
125439279cc3SChris Mason 		goto fail_close;
12552e635a27SChris Mason 	}
125658176a96SJosef Bacik 
125790a887c9SDan Magenheimer 	cleancache_init_fs(sb);
12581751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
12592e635a27SChris Mason 	return 0;
12602e635a27SChris Mason 
126139279cc3SChris Mason fail_close:
12626bccf3abSJeff Mahoney 	close_ctree(fs_info);
1263d5719762SChris Mason 	return err;
1264d5719762SChris Mason }
1265d5719762SChris Mason 
12666bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
1267d5719762SChris Mason {
1268d5719762SChris Mason 	struct btrfs_trans_handle *trans;
1269815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1270815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
1271df2ce34cSChris Mason 
1272bc074524SJeff Mahoney 	trace_btrfs_sync_fs(fs_info, wait);
12731abe9b8aSliubo 
1274d561c025SChris Mason 	if (!wait) {
1275815745cfSAl Viro 		filemap_flush(fs_info->btree_inode->i_mapping);
1276df2ce34cSChris Mason 		return 0;
1277d561c025SChris Mason 	}
1278771ed689SChris Mason 
12796374e57aSChris Mason 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1280771ed689SChris Mason 
1281d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
128260376ce4SJosef Bacik 	if (IS_ERR(trans)) {
1283354aa0fbSMiao Xie 		/* no transaction, don't bother */
12846b5fe46dSDavid Sterba 		if (PTR_ERR(trans) == -ENOENT) {
12856b5fe46dSDavid Sterba 			/*
12866b5fe46dSDavid Sterba 			 * Exit unless we have some pending changes
12876b5fe46dSDavid Sterba 			 * that need to go through commit
12886b5fe46dSDavid Sterba 			 */
12896b5fe46dSDavid Sterba 			if (fs_info->pending_changes == 0)
1290bd7de2c9SJosef Bacik 				return 0;
1291a53f4f8eSQu Wenruo 			/*
1292a53f4f8eSQu Wenruo 			 * A non-blocking test if the fs is frozen. We must not
1293a53f4f8eSQu Wenruo 			 * start a new transaction here otherwise a deadlock
1294a53f4f8eSQu Wenruo 			 * happens. The pending operations are delayed to the
1295a53f4f8eSQu Wenruo 			 * next commit after thawing.
1296a53f4f8eSQu Wenruo 			 */
1297a7e3c5f2SRakesh Pandit 			if (sb_start_write_trylock(sb))
1298a7e3c5f2SRakesh Pandit 				sb_end_write(sb);
1299a53f4f8eSQu Wenruo 			else
1300a53f4f8eSQu Wenruo 				return 0;
13016b5fe46dSDavid Sterba 			trans = btrfs_start_transaction(root, 0);
130260376ce4SJosef Bacik 		}
130398bd5c54SDavid Sterba 		if (IS_ERR(trans))
130498bd5c54SDavid Sterba 			return PTR_ERR(trans);
13056b5fe46dSDavid Sterba 	}
13063a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
1307d5719762SChris Mason }
1308d5719762SChris Mason 
130934c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1310a9572a15SEric Paris {
1311815745cfSAl Viro 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
13120f628c63SDavid Sterba 	const char *compress_type;
1313a9572a15SEric Paris 
13143cdde224SJeff Mahoney 	if (btrfs_test_opt(info, DEGRADED))
1315a9572a15SEric Paris 		seq_puts(seq, ",degraded");
13163cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATASUM))
1317a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
13183cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATACOW))
1319a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
13203cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOBARRIER))
1321a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
132295ac567aSFilipe David Borba Manana 	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1323c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",max_inline=%llu", info->max_inline);
1324a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
1325a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
1326f7b885beSAnand Jain 		seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
13273cdde224SJeff Mahoney 	if (btrfs_test_opt(info, COMPRESS)) {
13280f628c63SDavid Sterba 		compress_type = btrfs_compress_type2str(info->compress_type);
13293cdde224SJeff Mahoney 		if (btrfs_test_opt(info, FORCE_COMPRESS))
1330200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
1331200da64eSTsutomu Itoh 		else
1332200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
1333f51d2b59SDavid Sterba 		if (info->compress_level)
1334fa4d885aSAdam Borowski 			seq_printf(seq, ":%d", info->compress_level);
1335200da64eSTsutomu Itoh 	}
13363cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOSSD))
1337c289811cSChris Mason 		seq_puts(seq, ",nossd");
13383cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SSD_SPREAD))
1339451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
13403cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, SSD))
1341a9572a15SEric Paris 		seq_puts(seq, ",ssd");
13423cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOTREELOG))
13436b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
13443cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOLOGREPLAY))
134596da0919SQu Wenruo 		seq_puts(seq, ",nologreplay");
13463cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FLUSHONCOMMIT))
13476b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
134846b27f50SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_SYNC))
134920a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
1350b0643e59SDennis Zhou 	if (btrfs_test_opt(info, DISCARD_ASYNC))
1351b0643e59SDennis Zhou 		seq_puts(seq, ",discard=async");
13521751e8a6SLinus Torvalds 	if (!(info->sb->s_flags & SB_POSIXACL))
1353a9572a15SEric Paris 		seq_puts(seq, ",noacl");
13543cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SPACE_CACHE))
1355200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
13563cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, FREE_SPACE_TREE))
135770f6d82eSOmar Sandoval 		seq_puts(seq, ",space_cache=v2");
135873bc1876SJosef Bacik 	else
13598965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
13603cdde224SJeff Mahoney 	if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1361f420ee1eSStefan Behrens 		seq_puts(seq, ",rescan_uuid_tree");
13623cdde224SJeff Mahoney 	if (btrfs_test_opt(info, CLEAR_CACHE))
1363200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
13643cdde224SJeff Mahoney 	if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1365200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
13663cdde224SJeff Mahoney 	if (btrfs_test_opt(info, ENOSPC_DEBUG))
13670942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
13683cdde224SJeff Mahoney 	if (btrfs_test_opt(info, AUTO_DEFRAG))
13690942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
13703cdde224SJeff Mahoney 	if (btrfs_test_opt(info, INODE_MAP_CACHE))
13710942caa3SDavid Sterba 		seq_puts(seq, ",inode_cache");
13723cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SKIP_BALANCE))
13739555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
13748507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
13753cdde224SJeff Mahoney 	if (btrfs_test_opt(info, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
13768507d216SWang Shilong 		seq_puts(seq, ",check_int_data");
13773cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, CHECK_INTEGRITY))
13788507d216SWang Shilong 		seq_puts(seq, ",check_int");
13798507d216SWang Shilong 	if (info->check_integrity_print_mask)
13808507d216SWang Shilong 		seq_printf(seq, ",check_int_print_mask=%d",
13818507d216SWang Shilong 				info->check_integrity_print_mask);
13828507d216SWang Shilong #endif
13838507d216SWang Shilong 	if (info->metadata_ratio)
1384764cb8b4SAnand Jain 		seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
13853cdde224SJeff Mahoney 	if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
13868c342930SJeff Mahoney 		seq_puts(seq, ",fatal_errors=panic");
13878b87dc17SDavid Sterba 	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1388d3740608SAnand Jain 		seq_printf(seq, ",commit=%u", info->commit_interval);
1389d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
13903cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_DATA))
1391d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=data");
13923cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_METADATA))
1393d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=metadata");
1394d0bd4560SJosef Bacik #endif
1395fb592373SJosef Bacik 	if (btrfs_test_opt(info, REF_VERIFY))
1396fb592373SJosef Bacik 		seq_puts(seq, ",ref_verify");
1397c8d3fe02SOmar Sandoval 	seq_printf(seq, ",subvolid=%llu",
1398c8d3fe02SOmar Sandoval 		  BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1399c8d3fe02SOmar Sandoval 	seq_puts(seq, ",subvol=");
1400c8d3fe02SOmar Sandoval 	seq_dentry(seq, dentry, " \t\n\\");
1401a9572a15SEric Paris 	return 0;
1402a9572a15SEric Paris }
1403a9572a15SEric Paris 
1404a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
14052e635a27SChris Mason {
1406815745cfSAl Viro 	struct btrfs_fs_info *p = data;
1407815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(s);
14084b82d6e4SYan 
1409815745cfSAl Viro 	return fs_info->fs_devices == p->fs_devices;
14104b82d6e4SYan }
14114b82d6e4SYan 
1412450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
1413450ba0eaSJosef Bacik {
14146de1d09dSAl Viro 	int err = set_anon_super(s, data);
14156de1d09dSAl Viro 	if (!err)
1416450ba0eaSJosef Bacik 		s->s_fs_info = data;
14176de1d09dSAl Viro 	return err;
1418450ba0eaSJosef Bacik }
1419450ba0eaSJosef Bacik 
1420830c4adbSJosef Bacik /*
1421f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
1422f9d9ef62SDavid Sterba  */
1423f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
1424f9d9ef62SDavid Sterba {
1425f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1426f9d9ef62SDavid Sterba 		return 1;
1427f9d9ef62SDavid Sterba 	return 0;
1428f9d9ef62SDavid Sterba }
1429f9d9ef62SDavid Sterba 
1430bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1431ae0bc863SAnand Jain 				   struct vfsmount *mnt)
1432830c4adbSJosef Bacik {
1433830c4adbSJosef Bacik 	struct dentry *root;
1434fa330659SOmar Sandoval 	int ret;
1435830c4adbSJosef Bacik 
143605dbe683SOmar Sandoval 	if (!subvol_name) {
143705dbe683SOmar Sandoval 		if (!subvol_objectid) {
143805dbe683SOmar Sandoval 			ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
143905dbe683SOmar Sandoval 							  &subvol_objectid);
144005dbe683SOmar Sandoval 			if (ret) {
144105dbe683SOmar Sandoval 				root = ERR_PTR(ret);
144205dbe683SOmar Sandoval 				goto out;
144305dbe683SOmar Sandoval 			}
144405dbe683SOmar Sandoval 		}
1445*c0c907a4SMarcos Paulo de Souza 		subvol_name = btrfs_get_subvol_name_from_objectid(
1446*c0c907a4SMarcos Paulo de Souza 					btrfs_sb(mnt->mnt_sb), subvol_objectid);
144705dbe683SOmar Sandoval 		if (IS_ERR(subvol_name)) {
144805dbe683SOmar Sandoval 			root = ERR_CAST(subvol_name);
144905dbe683SOmar Sandoval 			subvol_name = NULL;
145005dbe683SOmar Sandoval 			goto out;
145105dbe683SOmar Sandoval 		}
145205dbe683SOmar Sandoval 
145305dbe683SOmar Sandoval 	}
145405dbe683SOmar Sandoval 
1455ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1456fa330659SOmar Sandoval 	/* mount_subtree() drops our reference on the vfsmount. */
1457fa330659SOmar Sandoval 	mnt = NULL;
1458830c4adbSJosef Bacik 
1459bb289b7bSOmar Sandoval 	if (!IS_ERR(root)) {
1460ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1461ab8d0fc4SJeff Mahoney 		struct btrfs_fs_info *fs_info = btrfs_sb(s);
1462bb289b7bSOmar Sandoval 		struct inode *root_inode = d_inode(root);
1463bb289b7bSOmar Sandoval 		u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1464bb289b7bSOmar Sandoval 
1465bb289b7bSOmar Sandoval 		ret = 0;
1466bb289b7bSOmar Sandoval 		if (!is_subvolume_inode(root_inode)) {
1467ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info, "'%s' is not a valid subvolume",
1468bb289b7bSOmar Sandoval 			       subvol_name);
1469bb289b7bSOmar Sandoval 			ret = -EINVAL;
1470bb289b7bSOmar Sandoval 		}
1471bb289b7bSOmar Sandoval 		if (subvol_objectid && root_objectid != subvol_objectid) {
147205dbe683SOmar Sandoval 			/*
147305dbe683SOmar Sandoval 			 * This will also catch a race condition where a
147405dbe683SOmar Sandoval 			 * subvolume which was passed by ID is renamed and
147505dbe683SOmar Sandoval 			 * another subvolume is renamed over the old location.
147605dbe683SOmar Sandoval 			 */
1477ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info,
1478ab8d0fc4SJeff Mahoney 				  "subvol '%s' does not match subvolid %llu",
1479bb289b7bSOmar Sandoval 				  subvol_name, subvol_objectid);
1480bb289b7bSOmar Sandoval 			ret = -EINVAL;
1481bb289b7bSOmar Sandoval 		}
1482bb289b7bSOmar Sandoval 		if (ret) {
1483ea441d11SAl Viro 			dput(root);
1484bb289b7bSOmar Sandoval 			root = ERR_PTR(ret);
1485ea441d11SAl Viro 			deactivate_locked_super(s);
1486bb289b7bSOmar Sandoval 		}
1487f9d9ef62SDavid Sterba 	}
1488f9d9ef62SDavid Sterba 
1489fa330659SOmar Sandoval out:
1490fa330659SOmar Sandoval 	mntput(mnt);
1491fa330659SOmar Sandoval 	kfree(subvol_name);
1492830c4adbSJosef Bacik 	return root;
1493830c4adbSJosef Bacik }
1494450ba0eaSJosef Bacik 
1495312c89fbSMisono, Tomohiro /*
1496312c89fbSMisono, Tomohiro  * Find a superblock for the given device / mount point.
1497312c89fbSMisono, Tomohiro  *
1498312c89fbSMisono, Tomohiro  * Note: This is based on mount_bdev from fs/super.c with a few additions
1499312c89fbSMisono, Tomohiro  *       for multiple device setup.  Make sure to keep it in sync.
1500312c89fbSMisono, Tomohiro  */
150172fa39f5SMisono, Tomohiro static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
150272fa39f5SMisono, Tomohiro 		int flags, const char *device_name, void *data)
150372fa39f5SMisono, Tomohiro {
150472fa39f5SMisono, Tomohiro 	struct block_device *bdev = NULL;
150572fa39f5SMisono, Tomohiro 	struct super_block *s;
150636350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
150772fa39f5SMisono, Tomohiro 	struct btrfs_fs_devices *fs_devices = NULL;
150872fa39f5SMisono, Tomohiro 	struct btrfs_fs_info *fs_info = NULL;
1509204cc0ccSAl Viro 	void *new_sec_opts = NULL;
151072fa39f5SMisono, Tomohiro 	fmode_t mode = FMODE_READ;
151172fa39f5SMisono, Tomohiro 	int error = 0;
151272fa39f5SMisono, Tomohiro 
151372fa39f5SMisono, Tomohiro 	if (!(flags & SB_RDONLY))
151472fa39f5SMisono, Tomohiro 		mode |= FMODE_WRITE;
151572fa39f5SMisono, Tomohiro 
151672fa39f5SMisono, Tomohiro 	if (data) {
1517a65001e8SAl Viro 		error = security_sb_eat_lsm_opts(data, &new_sec_opts);
151872fa39f5SMisono, Tomohiro 		if (error)
151972fa39f5SMisono, Tomohiro 			return ERR_PTR(error);
152072fa39f5SMisono, Tomohiro 	}
152172fa39f5SMisono, Tomohiro 
152272fa39f5SMisono, Tomohiro 	/*
152372fa39f5SMisono, Tomohiro 	 * Setup a dummy root and fs_info for test/set super.  This is because
152472fa39f5SMisono, Tomohiro 	 * we don't actually fill this stuff out until open_ctree, but we need
15258260edbaSJosef Bacik 	 * then open_ctree will properly initialize the file system specific
15268260edbaSJosef Bacik 	 * settings later.  btrfs_init_fs_info initializes the static elements
15278260edbaSJosef Bacik 	 * of the fs_info (locks and such) to make cleanup easier if we find a
15288260edbaSJosef Bacik 	 * superblock with our given fs_devices later on at sget() time.
152972fa39f5SMisono, Tomohiro 	 */
1530a8fd1f71SJeff Mahoney 	fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
153172fa39f5SMisono, Tomohiro 	if (!fs_info) {
153272fa39f5SMisono, Tomohiro 		error = -ENOMEM;
153372fa39f5SMisono, Tomohiro 		goto error_sec_opts;
153472fa39f5SMisono, Tomohiro 	}
15358260edbaSJosef Bacik 	btrfs_init_fs_info(fs_info);
153672fa39f5SMisono, Tomohiro 
153772fa39f5SMisono, Tomohiro 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
153872fa39f5SMisono, Tomohiro 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
153972fa39f5SMisono, Tomohiro 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
154072fa39f5SMisono, Tomohiro 		error = -ENOMEM;
154172fa39f5SMisono, Tomohiro 		goto error_fs_info;
154272fa39f5SMisono, Tomohiro 	}
154372fa39f5SMisono, Tomohiro 
1544f5194e34SDavid Sterba 	mutex_lock(&uuid_mutex);
1545fa59f27cSAnand Jain 	error = btrfs_parse_device_options(data, mode, fs_type);
154681ffd56bSDavid Sterba 	if (error) {
1547399f7f4cSDavid Sterba 		mutex_unlock(&uuid_mutex);
1548399f7f4cSDavid Sterba 		goto error_fs_info;
154981ffd56bSDavid Sterba 	}
1550399f7f4cSDavid Sterba 
155136350e95SGu Jinxiang 	device = btrfs_scan_one_device(device_name, mode, fs_type);
155236350e95SGu Jinxiang 	if (IS_ERR(device)) {
1553399f7f4cSDavid Sterba 		mutex_unlock(&uuid_mutex);
155436350e95SGu Jinxiang 		error = PTR_ERR(device);
1555399f7f4cSDavid Sterba 		goto error_fs_info;
155681ffd56bSDavid Sterba 	}
1557399f7f4cSDavid Sterba 
155836350e95SGu Jinxiang 	fs_devices = device->fs_devices;
1559399f7f4cSDavid Sterba 	fs_info->fs_devices = fs_devices;
1560399f7f4cSDavid Sterba 
156172fa39f5SMisono, Tomohiro 	error = btrfs_open_devices(fs_devices, mode, fs_type);
1562f5194e34SDavid Sterba 	mutex_unlock(&uuid_mutex);
156372fa39f5SMisono, Tomohiro 	if (error)
156472fa39f5SMisono, Tomohiro 		goto error_fs_info;
156572fa39f5SMisono, Tomohiro 
156672fa39f5SMisono, Tomohiro 	if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
156772fa39f5SMisono, Tomohiro 		error = -EACCES;
156872fa39f5SMisono, Tomohiro 		goto error_close_devices;
156972fa39f5SMisono, Tomohiro 	}
157072fa39f5SMisono, Tomohiro 
157172fa39f5SMisono, Tomohiro 	bdev = fs_devices->latest_bdev;
157272fa39f5SMisono, Tomohiro 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
157372fa39f5SMisono, Tomohiro 		 fs_info);
157472fa39f5SMisono, Tomohiro 	if (IS_ERR(s)) {
157572fa39f5SMisono, Tomohiro 		error = PTR_ERR(s);
157672fa39f5SMisono, Tomohiro 		goto error_close_devices;
157772fa39f5SMisono, Tomohiro 	}
157872fa39f5SMisono, Tomohiro 
157972fa39f5SMisono, Tomohiro 	if (s->s_root) {
158072fa39f5SMisono, Tomohiro 		btrfs_close_devices(fs_devices);
15810d4b0463SJosef Bacik 		btrfs_free_fs_info(fs_info);
158272fa39f5SMisono, Tomohiro 		if ((flags ^ s->s_flags) & SB_RDONLY)
158372fa39f5SMisono, Tomohiro 			error = -EBUSY;
158472fa39f5SMisono, Tomohiro 	} else {
158572fa39f5SMisono, Tomohiro 		snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
158672fa39f5SMisono, Tomohiro 		btrfs_sb(s)->bdev_holder = fs_type;
15879b4e675aSDavid Sterba 		if (!strstr(crc32c_impl(), "generic"))
15889b4e675aSDavid Sterba 			set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
158972fa39f5SMisono, Tomohiro 		error = btrfs_fill_super(s, fs_devices, data);
159072fa39f5SMisono, Tomohiro 	}
1591a65001e8SAl Viro 	if (!error)
1592204cc0ccSAl Viro 		error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL);
1593a65001e8SAl Viro 	security_free_mnt_opts(&new_sec_opts);
159472fa39f5SMisono, Tomohiro 	if (error) {
159572fa39f5SMisono, Tomohiro 		deactivate_locked_super(s);
1596a65001e8SAl Viro 		return ERR_PTR(error);
159772fa39f5SMisono, Tomohiro 	}
159872fa39f5SMisono, Tomohiro 
159972fa39f5SMisono, Tomohiro 	return dget(s->s_root);
160072fa39f5SMisono, Tomohiro 
160172fa39f5SMisono, Tomohiro error_close_devices:
160272fa39f5SMisono, Tomohiro 	btrfs_close_devices(fs_devices);
160372fa39f5SMisono, Tomohiro error_fs_info:
16040d4b0463SJosef Bacik 	btrfs_free_fs_info(fs_info);
160572fa39f5SMisono, Tomohiro error_sec_opts:
160672fa39f5SMisono, Tomohiro 	security_free_mnt_opts(&new_sec_opts);
160772fa39f5SMisono, Tomohiro 	return ERR_PTR(error);
160872fa39f5SMisono, Tomohiro }
1609312c89fbSMisono, Tomohiro 
1610edf24abeSChristoph Hellwig /*
1611312c89fbSMisono, Tomohiro  * Mount function which is called by VFS layer.
1612edf24abeSChristoph Hellwig  *
1613312c89fbSMisono, Tomohiro  * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1614312c89fbSMisono, Tomohiro  * which needs vfsmount* of device's root (/).  This means device's root has to
1615312c89fbSMisono, Tomohiro  * be mounted internally in any case.
1616312c89fbSMisono, Tomohiro  *
1617312c89fbSMisono, Tomohiro  * Operation flow:
1618312c89fbSMisono, Tomohiro  *   1. Parse subvol id related options for later use in mount_subvol().
1619312c89fbSMisono, Tomohiro  *
1620312c89fbSMisono, Tomohiro  *   2. Mount device's root (/) by calling vfs_kern_mount().
1621312c89fbSMisono, Tomohiro  *
1622312c89fbSMisono, Tomohiro  *      NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1623312c89fbSMisono, Tomohiro  *      first place. In order to avoid calling btrfs_mount() again, we use
1624312c89fbSMisono, Tomohiro  *      different file_system_type which is not registered to VFS by
1625312c89fbSMisono, Tomohiro  *      register_filesystem() (btrfs_root_fs_type). As a result,
1626312c89fbSMisono, Tomohiro  *      btrfs_mount_root() is called. The return value will be used by
1627312c89fbSMisono, Tomohiro  *      mount_subtree() in mount_subvol().
1628312c89fbSMisono, Tomohiro  *
1629312c89fbSMisono, Tomohiro  *   3. Call mount_subvol() to get the dentry of subvolume. Since there is
1630312c89fbSMisono, Tomohiro  *      "btrfs subvolume set-default", mount_subvol() is called always.
1631edf24abeSChristoph Hellwig  */
1632061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1633306e16ceSDavid Sterba 		const char *device_name, void *data)
16344b82d6e4SYan {
1635312c89fbSMisono, Tomohiro 	struct vfsmount *mnt_root;
1636312c89fbSMisono, Tomohiro 	struct dentry *root;
163773f73415SJosef Bacik 	char *subvol_name = NULL;
163873f73415SJosef Bacik 	u64 subvol_objectid = 0;
16394b82d6e4SYan 	int error = 0;
16404b82d6e4SYan 
164193b9bcdfSGu Jinxiang 	error = btrfs_parse_subvol_options(data, &subvol_name,
164293b9bcdfSGu Jinxiang 					&subvol_objectid);
1643f23c8af8SIlya Dryomov 	if (error) {
1644f23c8af8SIlya Dryomov 		kfree(subvol_name);
1645061dbc6bSAl Viro 		return ERR_PTR(error);
1646f23c8af8SIlya Dryomov 	}
1647edf24abeSChristoph Hellwig 
1648312c89fbSMisono, Tomohiro 	/* mount device's root (/) */
1649312c89fbSMisono, Tomohiro 	mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data);
1650312c89fbSMisono, Tomohiro 	if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) {
1651312c89fbSMisono, Tomohiro 		if (flags & SB_RDONLY) {
1652312c89fbSMisono, Tomohiro 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1653312c89fbSMisono, Tomohiro 				flags & ~SB_RDONLY, device_name, data);
16544b82d6e4SYan 		} else {
1655312c89fbSMisono, Tomohiro 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1656312c89fbSMisono, Tomohiro 				flags | SB_RDONLY, device_name, data);
1657312c89fbSMisono, Tomohiro 			if (IS_ERR(mnt_root)) {
1658312c89fbSMisono, Tomohiro 				root = ERR_CAST(mnt_root);
1659532b618bSEric W. Biederman 				kfree(subvol_name);
1660312c89fbSMisono, Tomohiro 				goto out;
1661f667aef6SQu Wenruo 			}
1662f667aef6SQu Wenruo 
1663312c89fbSMisono, Tomohiro 			down_write(&mnt_root->mnt_sb->s_umount);
1664312c89fbSMisono, Tomohiro 			error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL);
1665312c89fbSMisono, Tomohiro 			up_write(&mnt_root->mnt_sb->s_umount);
1666312c89fbSMisono, Tomohiro 			if (error < 0) {
1667312c89fbSMisono, Tomohiro 				root = ERR_PTR(error);
1668312c89fbSMisono, Tomohiro 				mntput(mnt_root);
1669532b618bSEric W. Biederman 				kfree(subvol_name);
1670312c89fbSMisono, Tomohiro 				goto out;
1671312c89fbSMisono, Tomohiro 			}
1672312c89fbSMisono, Tomohiro 		}
1673312c89fbSMisono, Tomohiro 	}
1674312c89fbSMisono, Tomohiro 	if (IS_ERR(mnt_root)) {
1675312c89fbSMisono, Tomohiro 		root = ERR_CAST(mnt_root);
1676532b618bSEric W. Biederman 		kfree(subvol_name);
1677312c89fbSMisono, Tomohiro 		goto out;
1678f667aef6SQu Wenruo 	}
16794b82d6e4SYan 
1680312c89fbSMisono, Tomohiro 	/* mount_subvol() will free subvol_name and mnt_root */
1681ae0bc863SAnand Jain 	root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
16824b82d6e4SYan 
1683312c89fbSMisono, Tomohiro out:
1684312c89fbSMisono, Tomohiro 	return root;
16854b82d6e4SYan }
16862e635a27SChris Mason 
16870d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1688f7b885beSAnand Jain 				     u32 new_pool_size, u32 old_pool_size)
16890d2450abSSergei Trofimovich {
16900d2450abSSergei Trofimovich 	if (new_pool_size == old_pool_size)
16910d2450abSSergei Trofimovich 		return;
16920d2450abSSergei Trofimovich 
16930d2450abSSergei Trofimovich 	fs_info->thread_pool_size = new_pool_size;
16940d2450abSSergei Trofimovich 
1695efe120a0SFrank Holton 	btrfs_info(fs_info, "resize thread pool %d -> %d",
16960d2450abSSergei Trofimovich 	       old_pool_size, new_pool_size);
16970d2450abSSergei Trofimovich 
16985cdc7ad3SQu Wenruo 	btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1699afe3d242SQu Wenruo 	btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1700e66f0bb1SQu Wenruo 	btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1701fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size);
1702fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size);
1703fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_write_workers,
1704fccb5d86SQu Wenruo 				new_pool_size);
1705fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1706fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
17075b3bc44eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
1708736cfa15SQu Wenruo 	btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size);
17090339ef2fSQu Wenruo 	btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers,
1710ff023aacSStefan Behrens 				new_pool_size);
17110d2450abSSergei Trofimovich }
17120d2450abSSergei Trofimovich 
1713f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
1714dc81cdc5SMiao Xie {
1715dc81cdc5SMiao Xie 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1716f42a34b2SMiao Xie }
1717dc81cdc5SMiao Xie 
1718f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1719f42a34b2SMiao Xie 				       unsigned long old_opts, int flags)
1720f42a34b2SMiao Xie {
1721dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1722dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
17231751e8a6SLinus Torvalds 	     (flags & SB_RDONLY))) {
1724dc81cdc5SMiao Xie 		/* wait for any defraggers to finish */
1725dc81cdc5SMiao Xie 		wait_event(fs_info->transaction_wait,
1726dc81cdc5SMiao Xie 			   (atomic_read(&fs_info->defrag_running) == 0));
17271751e8a6SLinus Torvalds 		if (flags & SB_RDONLY)
1728dc81cdc5SMiao Xie 			sync_filesystem(fs_info->sb);
1729dc81cdc5SMiao Xie 	}
1730dc81cdc5SMiao Xie }
1731dc81cdc5SMiao Xie 
1732dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1733dc81cdc5SMiao Xie 					 unsigned long old_opts)
1734dc81cdc5SMiao Xie {
1735dc81cdc5SMiao Xie 	/*
1736180e4d47SLuis de Bethencourt 	 * We need to cleanup all defragable inodes if the autodefragment is
1737180e4d47SLuis de Bethencourt 	 * close or the filesystem is read only.
1738dc81cdc5SMiao Xie 	 */
1739dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1740bc98a42cSDavid Howells 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1741dc81cdc5SMiao Xie 		btrfs_cleanup_defrag_inodes(fs_info);
1742dc81cdc5SMiao Xie 	}
1743dc81cdc5SMiao Xie 
1744b0643e59SDennis Zhou 	/* If we toggled discard async */
1745b0643e59SDennis Zhou 	if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1746b0643e59SDennis Zhou 	    btrfs_test_opt(fs_info, DISCARD_ASYNC))
1747b0643e59SDennis Zhou 		btrfs_discard_resume(fs_info);
1748b0643e59SDennis Zhou 	else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1749b0643e59SDennis Zhou 		 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1750b0643e59SDennis Zhou 		btrfs_discard_cleanup(fs_info);
1751b0643e59SDennis Zhou 
1752dc81cdc5SMiao Xie 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1753dc81cdc5SMiao Xie }
1754dc81cdc5SMiao Xie 
1755c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1756c146afadSYan Zheng {
1757815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1758815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
175949b25e05SJeff Mahoney 	unsigned old_flags = sb->s_flags;
176049b25e05SJeff Mahoney 	unsigned long old_opts = fs_info->mount_opt;
176149b25e05SJeff Mahoney 	unsigned long old_compress_type = fs_info->compress_type;
176249b25e05SJeff Mahoney 	u64 old_max_inline = fs_info->max_inline;
1763f7b885beSAnand Jain 	u32 old_thread_pool_size = fs_info->thread_pool_size;
1764d612ac59SAnand Jain 	u32 old_metadata_ratio = fs_info->metadata_ratio;
1765c146afadSYan Zheng 	int ret;
1766c146afadSYan Zheng 
176702b9984dSTheodore Ts'o 	sync_filesystem(sb);
1768f42a34b2SMiao Xie 	btrfs_remount_prepare(fs_info);
1769dc81cdc5SMiao Xie 
1770f667aef6SQu Wenruo 	if (data) {
1771204cc0ccSAl Viro 		void *new_sec_opts = NULL;
1772f667aef6SQu Wenruo 
1773a65001e8SAl Viro 		ret = security_sb_eat_lsm_opts(data, &new_sec_opts);
1774a65001e8SAl Viro 		if (!ret)
1775204cc0ccSAl Viro 			ret = security_sb_remount(sb, new_sec_opts);
1776a65001e8SAl Viro 		security_free_mnt_opts(&new_sec_opts);
1777f667aef6SQu Wenruo 		if (ret)
1778f667aef6SQu Wenruo 			goto restore;
1779f667aef6SQu Wenruo 	}
1780f667aef6SQu Wenruo 
17812ff7e61eSJeff Mahoney 	ret = btrfs_parse_options(fs_info, data, *flags);
1782891f41cbSChengguang Xu 	if (ret)
178349b25e05SJeff Mahoney 		goto restore;
1784b288052eSChris Mason 
1785f42a34b2SMiao Xie 	btrfs_remount_begin(fs_info, old_opts, *flags);
17860d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
17870d2450abSSergei Trofimovich 		fs_info->thread_pool_size, old_thread_pool_size);
17880d2450abSSergei Trofimovich 
17891751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
1790dc81cdc5SMiao Xie 		goto out;
1791c146afadSYan Zheng 
17921751e8a6SLinus Torvalds 	if (*flags & SB_RDONLY) {
17938dabb742SStefan Behrens 		/*
17948dabb742SStefan Behrens 		 * this also happens on 'umount -rf' or on shutdown, when
17958dabb742SStefan Behrens 		 * the filesystem is busy.
17968dabb742SStefan Behrens 		 */
179721c7e756SMiao Xie 		cancel_work_sync(&fs_info->async_reclaim_work);
1798361c093dSStefan Behrens 
1799b0643e59SDennis Zhou 		btrfs_discard_cleanup(fs_info);
1800b0643e59SDennis Zhou 
1801361c093dSStefan Behrens 		/* wait for the uuid_scan task to finish */
1802361c093dSStefan Behrens 		down(&fs_info->uuid_tree_rescan_sem);
1803361c093dSStefan Behrens 		/* avoid complains from lockdep et al. */
1804361c093dSStefan Behrens 		up(&fs_info->uuid_tree_rescan_sem);
1805361c093dSStefan Behrens 
18061751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
1807c146afadSYan Zheng 
1808e44163e1SJeff Mahoney 		/*
18091751e8a6SLinus Torvalds 		 * Setting SB_RDONLY will put the cleaner thread to
1810e44163e1SJeff Mahoney 		 * sleep at the next loop if it's already active.
1811e44163e1SJeff Mahoney 		 * If it's already asleep, we'll leave unused block
1812e44163e1SJeff Mahoney 		 * groups on disk until we're mounted read-write again
1813e44163e1SJeff Mahoney 		 * unless we clean them up here.
1814e44163e1SJeff Mahoney 		 */
1815e44163e1SJeff Mahoney 		btrfs_delete_unused_bgs(fs_info);
1816e44163e1SJeff Mahoney 
18178dabb742SStefan Behrens 		btrfs_dev_replace_suspend_for_unmount(fs_info);
18188dabb742SStefan Behrens 		btrfs_scrub_cancel(fs_info);
1819061594efSMiao Xie 		btrfs_pause_balance(fs_info);
18208dabb742SStefan Behrens 
18216bccf3abSJeff Mahoney 		ret = btrfs_commit_super(fs_info);
182249b25e05SJeff Mahoney 		if (ret)
182349b25e05SJeff Mahoney 			goto restore;
1824c146afadSYan Zheng 	} else {
18250b246afaSJeff Mahoney 		if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
18266ef3de9cSDavid Sterba 			btrfs_err(fs_info,
1827efe120a0SFrank Holton 				"Remounting read-write after error is not allowed");
18286ef3de9cSDavid Sterba 			ret = -EINVAL;
18296ef3de9cSDavid Sterba 			goto restore;
18306ef3de9cSDavid Sterba 		}
18318a3db184SSergei Trofimovich 		if (fs_info->fs_devices->rw_devices == 0) {
183249b25e05SJeff Mahoney 			ret = -EACCES;
183349b25e05SJeff Mahoney 			goto restore;
18348a3db184SSergei Trofimovich 		}
18352b82032cSYan Zheng 
18366528b99dSAnand Jain 		if (!btrfs_check_rw_degradable(fs_info, NULL)) {
1837efe120a0SFrank Holton 			btrfs_warn(fs_info,
183852042d8eSAndrea Gelmini 		"too many missing devices, writable remount is not allowed");
1839292fd7fcSStefan Behrens 			ret = -EACCES;
1840292fd7fcSStefan Behrens 			goto restore;
1841292fd7fcSStefan Behrens 		}
1842292fd7fcSStefan Behrens 
18438a3db184SSergei Trofimovich 		if (btrfs_super_log_root(fs_info->super_copy) != 0) {
184410a3a3edSDavid Sterba 			btrfs_warn(fs_info,
184510a3a3edSDavid Sterba 		"mount required to replay tree-log, cannot remount read-write");
184649b25e05SJeff Mahoney 			ret = -EINVAL;
184749b25e05SJeff Mahoney 			goto restore;
18488a3db184SSergei Trofimovich 		}
1849c146afadSYan Zheng 
1850815745cfSAl Viro 		ret = btrfs_cleanup_fs_roots(fs_info);
185149b25e05SJeff Mahoney 		if (ret)
185249b25e05SJeff Mahoney 			goto restore;
1853c146afadSYan Zheng 
1854d68fc57bSYan, Zheng 		/* recover relocation */
18555f316481SWang Shilong 		mutex_lock(&fs_info->cleaner_mutex);
1856d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
18575f316481SWang Shilong 		mutex_unlock(&fs_info->cleaner_mutex);
185849b25e05SJeff Mahoney 		if (ret)
185949b25e05SJeff Mahoney 			goto restore;
1860c146afadSYan Zheng 
18612b6ba629SIlya Dryomov 		ret = btrfs_resume_balance_async(fs_info);
18622b6ba629SIlya Dryomov 		if (ret)
18632b6ba629SIlya Dryomov 			goto restore;
18642b6ba629SIlya Dryomov 
18658dabb742SStefan Behrens 		ret = btrfs_resume_dev_replace_async(fs_info);
18668dabb742SStefan Behrens 		if (ret) {
1867efe120a0SFrank Holton 			btrfs_warn(fs_info, "failed to resume dev_replace");
18688dabb742SStefan Behrens 			goto restore;
18698dabb742SStefan Behrens 		}
187094aebfb2SJosef Bacik 
18716c6b5a39SAleksa Sarai 		btrfs_qgroup_rescan_resume(fs_info);
18726c6b5a39SAleksa Sarai 
187394aebfb2SJosef Bacik 		if (!fs_info->uuid_root) {
1874efe120a0SFrank Holton 			btrfs_info(fs_info, "creating UUID tree");
187594aebfb2SJosef Bacik 			ret = btrfs_create_uuid_tree(fs_info);
187694aebfb2SJosef Bacik 			if (ret) {
18775d163e0eSJeff Mahoney 				btrfs_warn(fs_info,
18785d163e0eSJeff Mahoney 					   "failed to create the UUID tree %d",
18795d163e0eSJeff Mahoney 					   ret);
188094aebfb2SJosef Bacik 				goto restore;
188194aebfb2SJosef Bacik 			}
188294aebfb2SJosef Bacik 		}
18831751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_RDONLY;
188490c711abSZygo Blaxell 
1885afcdd129SJosef Bacik 		set_bit(BTRFS_FS_OPEN, &fs_info->flags);
1886c146afadSYan Zheng 	}
1887dc81cdc5SMiao Xie out:
18882c6a92b0SJustin Maggard 	wake_up_process(fs_info->transaction_kthread);
1889dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
1890c146afadSYan Zheng 	return 0;
189149b25e05SJeff Mahoney 
189249b25e05SJeff Mahoney restore:
18931751e8a6SLinus Torvalds 	/* We've hit an error - don't reset SB_RDONLY */
1894bc98a42cSDavid Howells 	if (sb_rdonly(sb))
18951751e8a6SLinus Torvalds 		old_flags |= SB_RDONLY;
189649b25e05SJeff Mahoney 	sb->s_flags = old_flags;
189749b25e05SJeff Mahoney 	fs_info->mount_opt = old_opts;
189849b25e05SJeff Mahoney 	fs_info->compress_type = old_compress_type;
189949b25e05SJeff Mahoney 	fs_info->max_inline = old_max_inline;
19000d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
19010d2450abSSergei Trofimovich 		old_thread_pool_size, fs_info->thread_pool_size);
190249b25e05SJeff Mahoney 	fs_info->metadata_ratio = old_metadata_ratio;
1903dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
190449b25e05SJeff Mahoney 	return ret;
1905c146afadSYan Zheng }
1906c146afadSYan Zheng 
1907bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
19087e17916bSArnd Bergmann static inline int btrfs_cmp_device_free_bytes(const void *dev_info1,
1909bcd53741SArne Jansen 				       const void *dev_info2)
1910bcd53741SArne Jansen {
1911bcd53741SArne Jansen 	if (((struct btrfs_device_info *)dev_info1)->max_avail >
1912bcd53741SArne Jansen 	    ((struct btrfs_device_info *)dev_info2)->max_avail)
1913bcd53741SArne Jansen 		return -1;
1914bcd53741SArne Jansen 	else if (((struct btrfs_device_info *)dev_info1)->max_avail <
1915bcd53741SArne Jansen 		 ((struct btrfs_device_info *)dev_info2)->max_avail)
1916bcd53741SArne Jansen 		return 1;
1917bcd53741SArne Jansen 	else
1918bcd53741SArne Jansen 	return 0;
1919bcd53741SArne Jansen }
1920bcd53741SArne Jansen 
1921bcd53741SArne Jansen /*
1922bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
1923bcd53741SArne Jansen  * is stored.(Descending Sort)
1924bcd53741SArne Jansen  */
1925bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
1926bcd53741SArne Jansen 					struct btrfs_device_info *devices,
1927bcd53741SArne Jansen 					size_t nr_devices)
1928bcd53741SArne Jansen {
1929bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1930bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
1931bcd53741SArne Jansen }
1932bcd53741SArne Jansen 
19336d07bcecSMiao Xie /*
19346d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
19356d07bcecSMiao Xie  * file data.
19366d07bcecSMiao Xie  */
19377e17916bSArnd Bergmann static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
19386bccf3abSJeff Mahoney 					      u64 *free_bytes)
19396d07bcecSMiao Xie {
19406d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
19416d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
19426d07bcecSMiao Xie 	struct btrfs_device *device;
19436d07bcecSMiao Xie 	u64 type;
19446d07bcecSMiao Xie 	u64 avail_space;
19456d07bcecSMiao Xie 	u64 min_stripe_size;
1946559ca6eaSNikolay Borisov 	int num_stripes = 1;
19476d07bcecSMiao Xie 	int i = 0, nr_devices;
19484f080f57SDavid Sterba 	const struct btrfs_raid_attr *rattr;
19496d07bcecSMiao Xie 
19507e33fd99SJosef Bacik 	/*
195101327610SNicholas D Steeves 	 * We aren't under the device list lock, so this is racy-ish, but good
19527e33fd99SJosef Bacik 	 * enough for our purposes.
19537e33fd99SJosef Bacik 	 */
1954b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
19557e33fd99SJosef Bacik 	if (!nr_devices) {
19567e33fd99SJosef Bacik 		smp_mb();
19577e33fd99SJosef Bacik 		nr_devices = fs_info->fs_devices->open_devices;
19587e33fd99SJosef Bacik 		ASSERT(nr_devices);
19597e33fd99SJosef Bacik 		if (!nr_devices) {
19607e33fd99SJosef Bacik 			*free_bytes = 0;
19617e33fd99SJosef Bacik 			return 0;
19627e33fd99SJosef Bacik 		}
19637e33fd99SJosef Bacik 	}
19646d07bcecSMiao Xie 
1965d9b0d9baSDulshani Gunawardhana 	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
19666a44517dSDavid Sterba 			       GFP_KERNEL);
19676d07bcecSMiao Xie 	if (!devices_info)
19686d07bcecSMiao Xie 		return -ENOMEM;
19696d07bcecSMiao Xie 
197001327610SNicholas D Steeves 	/* calc min stripe number for data space allocation */
19711b86826dSJeff Mahoney 	type = btrfs_data_alloc_profile(fs_info);
19724f080f57SDavid Sterba 	rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
19734f080f57SDavid Sterba 
1974e1ea2beeSDavid Sterba 	if (type & BTRFS_BLOCK_GROUP_RAID0)
197539fb26c3SMiao Xie 		num_stripes = nr_devices;
1976e1ea2beeSDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1)
197739fb26c3SMiao Xie 		num_stripes = 2;
197847e6f742SDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1C3)
197947e6f742SDavid Sterba 		num_stripes = 3;
19808d6fac00SDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID1C4)
19818d6fac00SDavid Sterba 		num_stripes = 4;
1982e1ea2beeSDavid Sterba 	else if (type & BTRFS_BLOCK_GROUP_RAID10)
198339fb26c3SMiao Xie 		num_stripes = 4;
19846d07bcecSMiao Xie 
19854f080f57SDavid Sterba 	/* Adjust for more than 1 stripe per device */
19864f080f57SDavid Sterba 	min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
19876d07bcecSMiao Xie 
19887e33fd99SJosef Bacik 	rcu_read_lock();
19897e33fd99SJosef Bacik 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
1990e12c9621SAnand Jain 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1991e12c9621SAnand Jain 						&device->dev_state) ||
1992401e29c1SAnand Jain 		    !device->bdev ||
1993401e29c1SAnand Jain 		    test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
19946d07bcecSMiao Xie 			continue;
19956d07bcecSMiao Xie 
19967e33fd99SJosef Bacik 		if (i >= nr_devices)
19977e33fd99SJosef Bacik 			break;
19987e33fd99SJosef Bacik 
19996d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
20006d07bcecSMiao Xie 
20016d07bcecSMiao Xie 		/* align with stripe_len */
2002559ca6eaSNikolay Borisov 		avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
20036d07bcecSMiao Xie 
20046d07bcecSMiao Xie 		/*
200501327610SNicholas D Steeves 		 * In order to avoid overwriting the superblock on the drive,
20066d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
20076d07bcecSMiao Xie 		 * allocation.
2008559ca6eaSNikolay Borisov 		 *
2009559ca6eaSNikolay Borisov 		 * This ensures we have at least min_stripe_size free space
2010559ca6eaSNikolay Borisov 		 * after excluding 1MB.
20116d07bcecSMiao Xie 		 */
2012559ca6eaSNikolay Borisov 		if (avail_space <= SZ_1M + min_stripe_size)
20136d07bcecSMiao Xie 			continue;
20146d07bcecSMiao Xie 
2015559ca6eaSNikolay Borisov 		avail_space -= SZ_1M;
2016559ca6eaSNikolay Borisov 
20176d07bcecSMiao Xie 		devices_info[i].dev = device;
20186d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
20196d07bcecSMiao Xie 
20206d07bcecSMiao Xie 		i++;
20216d07bcecSMiao Xie 	}
20227e33fd99SJosef Bacik 	rcu_read_unlock();
20236d07bcecSMiao Xie 
20246d07bcecSMiao Xie 	nr_devices = i;
20256d07bcecSMiao Xie 
20266d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
20276d07bcecSMiao Xie 
20286d07bcecSMiao Xie 	i = nr_devices - 1;
20296d07bcecSMiao Xie 	avail_space = 0;
2030559ca6eaSNikolay Borisov 	while (nr_devices >= rattr->devs_min) {
2031559ca6eaSNikolay Borisov 		num_stripes = min(num_stripes, nr_devices);
203239fb26c3SMiao Xie 
20336d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
20346d07bcecSMiao Xie 			int j;
20356d07bcecSMiao Xie 			u64 alloc_size;
20366d07bcecSMiao Xie 
203739fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
20386d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
203939fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
20406d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
20416d07bcecSMiao Xie 		}
20426d07bcecSMiao Xie 		i--;
20436d07bcecSMiao Xie 		nr_devices--;
20446d07bcecSMiao Xie 	}
20456d07bcecSMiao Xie 
20466d07bcecSMiao Xie 	kfree(devices_info);
20476d07bcecSMiao Xie 	*free_bytes = avail_space;
20486d07bcecSMiao Xie 	return 0;
20496d07bcecSMiao Xie }
20506d07bcecSMiao Xie 
2051ba7b6e62SDavid Sterba /*
2052ba7b6e62SDavid Sterba  * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2053ba7b6e62SDavid Sterba  *
2054ba7b6e62SDavid Sterba  * If there's a redundant raid level at DATA block groups, use the respective
2055ba7b6e62SDavid Sterba  * multiplier to scale the sizes.
2056ba7b6e62SDavid Sterba  *
2057ba7b6e62SDavid Sterba  * Unused device space usage is based on simulating the chunk allocator
20580d0c71b3SDavid Sterba  * algorithm that respects the device sizes and order of allocations.  This is
20590d0c71b3SDavid Sterba  * a close approximation of the actual use but there are other factors that may
20600d0c71b3SDavid Sterba  * change the result (like a new metadata chunk).
2061ba7b6e62SDavid Sterba  *
2062ca8a51b3SDavid Sterba  * If metadata is exhausted, f_bavail will be 0.
2063ba7b6e62SDavid Sterba  */
20648fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
20658fd17795SChris Mason {
2066815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
2067815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
2068bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
2069bd4d1088SJosef Bacik 	u64 total_used = 0;
20706d07bcecSMiao Xie 	u64 total_free_data = 0;
2071ca8a51b3SDavid Sterba 	u64 total_free_meta = 0;
2072db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
2073de37aa51SNikolay Borisov 	__be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
2074ba7b6e62SDavid Sterba 	unsigned factor = 1;
2075ba7b6e62SDavid Sterba 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
20766d07bcecSMiao Xie 	int ret;
2077ca8a51b3SDavid Sterba 	u64 thresh = 0;
2078ae02d1bdSLuis de Bethencourt 	int mixed = 0;
20798fd17795SChris Mason 
2080bd4d1088SJosef Bacik 	rcu_read_lock();
2081f5389f33SJohannes Thumshirn 	list_for_each_entry_rcu(found, &fs_info->space_info, list) {
20826d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
2083ba7b6e62SDavid Sterba 			int i;
2084ba7b6e62SDavid Sterba 
20856d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
20866d07bcecSMiao Xie 			total_free_data -=
20876d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
2088ba7b6e62SDavid Sterba 
2089ba7b6e62SDavid Sterba 			for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
209046df06b8SDavid Sterba 				if (!list_empty(&found->block_groups[i]))
209146df06b8SDavid Sterba 					factor = btrfs_bg_type_to_factor(
209246df06b8SDavid Sterba 						btrfs_raid_array[i].bg_flag);
2093ba7b6e62SDavid Sterba 			}
20946d07bcecSMiao Xie 		}
2095ae02d1bdSLuis de Bethencourt 
2096ae02d1bdSLuis de Bethencourt 		/*
2097ae02d1bdSLuis de Bethencourt 		 * Metadata in mixed block goup profiles are accounted in data
2098ae02d1bdSLuis de Bethencourt 		 */
2099ae02d1bdSLuis de Bethencourt 		if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
2100ae02d1bdSLuis de Bethencourt 			if (found->flags & BTRFS_BLOCK_GROUP_DATA)
2101ae02d1bdSLuis de Bethencourt 				mixed = 1;
2102ae02d1bdSLuis de Bethencourt 			else
2103ae02d1bdSLuis de Bethencourt 				total_free_meta += found->disk_total -
2104ae02d1bdSLuis de Bethencourt 					found->disk_used;
2105ae02d1bdSLuis de Bethencourt 		}
21066d07bcecSMiao Xie 
2107b742bb82SYan, Zheng 		total_used += found->disk_used;
210889a55897SJosef Bacik 	}
2109ba7b6e62SDavid Sterba 
2110bd4d1088SJosef Bacik 	rcu_read_unlock();
2111bd4d1088SJosef Bacik 
2112ba7b6e62SDavid Sterba 	buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
2113ba7b6e62SDavid Sterba 	buf->f_blocks >>= bits;
2114ba7b6e62SDavid Sterba 	buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
2115ba7b6e62SDavid Sterba 
2116ba7b6e62SDavid Sterba 	/* Account global block reserve as used, it's in logical size already */
2117ba7b6e62SDavid Sterba 	spin_lock(&block_rsv->lock);
211841b34accSLuis de Bethencourt 	/* Mixed block groups accounting is not byte-accurate, avoid overflow */
211941b34accSLuis de Bethencourt 	if (buf->f_bfree >= block_rsv->size >> bits)
2120ba7b6e62SDavid Sterba 		buf->f_bfree -= block_rsv->size >> bits;
212141b34accSLuis de Bethencourt 	else
212241b34accSLuis de Bethencourt 		buf->f_bfree = 0;
2123ba7b6e62SDavid Sterba 	spin_unlock(&block_rsv->lock);
2124ba7b6e62SDavid Sterba 
21250d95c1beSDavid Sterba 	buf->f_bavail = div_u64(total_free_data, factor);
21266bccf3abSJeff Mahoney 	ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
21277e33fd99SJosef Bacik 	if (ret)
21286d07bcecSMiao Xie 		return ret;
2129ba7b6e62SDavid Sterba 	buf->f_bavail += div_u64(total_free_data, factor);
21306d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
2131d397712bSChris Mason 
2132ca8a51b3SDavid Sterba 	/*
2133ca8a51b3SDavid Sterba 	 * We calculate the remaining metadata space minus global reserve. If
2134ca8a51b3SDavid Sterba 	 * this is (supposedly) smaller than zero, there's no space. But this
2135ca8a51b3SDavid Sterba 	 * does not hold in practice, the exhausted state happens where's still
2136ca8a51b3SDavid Sterba 	 * some positive delta. So we apply some guesswork and compare the
2137ca8a51b3SDavid Sterba 	 * delta to a 4M threshold.  (Practically observed delta was ~2M.)
2138ca8a51b3SDavid Sterba 	 *
2139ca8a51b3SDavid Sterba 	 * We probably cannot calculate the exact threshold value because this
2140ca8a51b3SDavid Sterba 	 * depends on the internal reservations requested by various
2141ca8a51b3SDavid Sterba 	 * operations, so some operations that consume a few metadata will
2142ca8a51b3SDavid Sterba 	 * succeed even if the Avail is zero. But this is better than the other
2143ca8a51b3SDavid Sterba 	 * way around.
2144ca8a51b3SDavid Sterba 	 */
2145d4417e22SNikolay Borisov 	thresh = SZ_4M;
2146ca8a51b3SDavid Sterba 
2147d55966c4SJosef Bacik 	/*
2148d55966c4SJosef Bacik 	 * We only want to claim there's no available space if we can no longer
2149d55966c4SJosef Bacik 	 * allocate chunks for our metadata profile and our global reserve will
2150d55966c4SJosef Bacik 	 * not fit in the free metadata space.  If we aren't ->full then we
2151d55966c4SJosef Bacik 	 * still can allocate chunks and thus are fine using the currently
2152d55966c4SJosef Bacik 	 * calculated f_bavail.
2153d55966c4SJosef Bacik 	 */
2154d55966c4SJosef Bacik 	if (!mixed && block_rsv->space_info->full &&
2155d55966c4SJosef Bacik 	    total_free_meta - thresh < block_rsv->size)
2156ca8a51b3SDavid Sterba 		buf->f_bavail = 0;
2157ca8a51b3SDavid Sterba 
2158ba7b6e62SDavid Sterba 	buf->f_type = BTRFS_SUPER_MAGIC;
2159ba7b6e62SDavid Sterba 	buf->f_bsize = dentry->d_sb->s_blocksize;
2160ba7b6e62SDavid Sterba 	buf->f_namelen = BTRFS_NAME_LEN;
2161ba7b6e62SDavid Sterba 
21629d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
21639d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
21649d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
21659d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
21669d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
216732d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
21684fd786e6SMisono Tomohiro 	buf->f_fsid.val[0] ^=
21694fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32;
21704fd786e6SMisono Tomohiro 	buf->f_fsid.val[1] ^=
21714fd786e6SMisono Tomohiro 		BTRFS_I(d_inode(dentry))->root->root_key.objectid;
217232d48fa1SDavid Woodhouse 
21738fd17795SChris Mason 	return 0;
21748fd17795SChris Mason }
2175b5133862SChris Mason 
2176aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
2177aea52e19SAl Viro {
2178815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2179aea52e19SAl Viro 	kill_anon_super(sb);
21800d4b0463SJosef Bacik 	btrfs_free_fs_info(fs_info);
2181aea52e19SAl Viro }
2182aea52e19SAl Viro 
21832e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
21842e635a27SChris Mason 	.owner		= THIS_MODULE,
21852e635a27SChris Mason 	.name		= "btrfs",
2186061dbc6bSAl Viro 	.mount		= btrfs_mount,
2187aea52e19SAl Viro 	.kill_sb	= btrfs_kill_super,
2188f667aef6SQu Wenruo 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
21892e635a27SChris Mason };
219072fa39f5SMisono, Tomohiro 
219172fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type = {
219272fa39f5SMisono, Tomohiro 	.owner		= THIS_MODULE,
219372fa39f5SMisono, Tomohiro 	.name		= "btrfs",
219472fa39f5SMisono, Tomohiro 	.mount		= btrfs_mount_root,
219572fa39f5SMisono, Tomohiro 	.kill_sb	= btrfs_kill_super,
219672fa39f5SMisono, Tomohiro 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
219772fa39f5SMisono, Tomohiro };
219872fa39f5SMisono, Tomohiro 
21997f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs");
2200a9218f6bSChris Mason 
2201d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file)
2202d8620958STom Van Braeckel {
2203d8620958STom Van Braeckel 	/*
2204d8620958STom Van Braeckel 	 * The control file's private_data is used to hold the
2205d8620958STom Van Braeckel 	 * transaction when it is started and is used to keep
2206d8620958STom Van Braeckel 	 * track of whether a transaction is already in progress.
2207d8620958STom Van Braeckel 	 */
2208d8620958STom Van Braeckel 	file->private_data = NULL;
2209d8620958STom Van Braeckel 	return 0;
2210d8620958STom Van Braeckel }
2211d8620958STom Van Braeckel 
2212d352ac68SChris Mason /*
2213cfe953c8SSu Yue  * Used by /dev/btrfs-control for devices ioctls.
2214d352ac68SChris Mason  */
22158a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
22168a4b83ccSChris Mason 				unsigned long arg)
22178a4b83ccSChris Mason {
22188a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
221936350e95SGu Jinxiang 	struct btrfs_device *device = NULL;
2220c071fcfdSChris Mason 	int ret = -ENOTTY;
22218a4b83ccSChris Mason 
2222e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2223e441d54dSChris Mason 		return -EPERM;
2224e441d54dSChris Mason 
2225dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
2226dae7b665SLi Zefan 	if (IS_ERR(vol))
2227dae7b665SLi Zefan 		return PTR_ERR(vol);
2228f505754fSFilipe Manana 	vol->name[BTRFS_PATH_NAME_MAX] = '\0';
2229c071fcfdSChris Mason 
22308a4b83ccSChris Mason 	switch (cmd) {
22318a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
2232899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
223336350e95SGu Jinxiang 		device = btrfs_scan_one_device(vol->name, FMODE_READ,
223436350e95SGu Jinxiang 					       &btrfs_root_fs_type);
223536350e95SGu Jinxiang 		ret = PTR_ERR_OR_ZERO(device);
2236899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
22378a4b83ccSChris Mason 		break;
2238228a73abSAnand Jain 	case BTRFS_IOC_FORGET_DEV:
2239228a73abSAnand Jain 		ret = btrfs_forget_devices(vol->name);
2240228a73abSAnand Jain 		break;
224102db0844SJosef Bacik 	case BTRFS_IOC_DEVICES_READY:
2242899f9307SDavid Sterba 		mutex_lock(&uuid_mutex);
224336350e95SGu Jinxiang 		device = btrfs_scan_one_device(vol->name, FMODE_READ,
224436350e95SGu Jinxiang 					       &btrfs_root_fs_type);
224536350e95SGu Jinxiang 		if (IS_ERR(device)) {
2246899f9307SDavid Sterba 			mutex_unlock(&uuid_mutex);
224736350e95SGu Jinxiang 			ret = PTR_ERR(device);
224802db0844SJosef Bacik 			break;
2249899f9307SDavid Sterba 		}
225036350e95SGu Jinxiang 		ret = !(device->fs_devices->num_devices ==
225136350e95SGu Jinxiang 			device->fs_devices->total_devices);
2252899f9307SDavid Sterba 		mutex_unlock(&uuid_mutex);
225302db0844SJosef Bacik 		break;
2254c5868f83SDavid Sterba 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2255d5131b65SDavid Sterba 		ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2256c5868f83SDavid Sterba 		break;
22578a4b83ccSChris Mason 	}
2258dae7b665SLi Zefan 
22598a4b83ccSChris Mason 	kfree(vol);
2260f819d837SLinda Knippers 	return ret;
22618a4b83ccSChris Mason }
22628a4b83ccSChris Mason 
22630176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
2264ed0dab6bSYan {
2265354aa0fbSMiao Xie 	struct btrfs_trans_handle *trans;
22660b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
22670b246afaSJeff Mahoney 	struct btrfs_root *root = fs_info->tree_root;
2268354aa0fbSMiao Xie 
2269fac03c8dSDavid Sterba 	set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
22709e7cc91aSWang Xiaoguang 	/*
22719e7cc91aSWang Xiaoguang 	 * We don't need a barrier here, we'll wait for any transaction that
22729e7cc91aSWang Xiaoguang 	 * could be in progress on other threads (and do delayed iputs that
22739e7cc91aSWang Xiaoguang 	 * we want to avoid on a frozen filesystem), or do the commit
22749e7cc91aSWang Xiaoguang 	 * ourselves.
22759e7cc91aSWang Xiaoguang 	 */
2276d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
2277354aa0fbSMiao Xie 	if (IS_ERR(trans)) {
2278354aa0fbSMiao Xie 		/* no transaction, don't bother */
2279354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
22800176260fSLinus Torvalds 			return 0;
2281354aa0fbSMiao Xie 		return PTR_ERR(trans);
2282354aa0fbSMiao Xie 	}
22833a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
2284ed0dab6bSYan }
2285ed0dab6bSYan 
22869e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb)
22879e7cc91aSWang Xiaoguang {
2288fac03c8dSDavid Sterba 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2289fac03c8dSDavid Sterba 
2290fac03c8dSDavid Sterba 	clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
22919e7cc91aSWang Xiaoguang 	return 0;
22929e7cc91aSWang Xiaoguang }
22939e7cc91aSWang Xiaoguang 
22949c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
22959c5085c1SJosef Bacik {
22969c5085c1SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
22979c5085c1SJosef Bacik 	struct btrfs_fs_devices *cur_devices;
22989c5085c1SJosef Bacik 	struct btrfs_device *dev, *first_dev = NULL;
22999c5085c1SJosef Bacik 	struct list_head *head;
23009c5085c1SJosef Bacik 
230188c14590SDavid Sterba 	/*
230288c14590SDavid Sterba 	 * Lightweight locking of the devices. We should not need
230388c14590SDavid Sterba 	 * device_list_mutex here as we only read the device data and the list
230488c14590SDavid Sterba 	 * is protected by RCU.  Even if a device is deleted during the list
230588c14590SDavid Sterba 	 * traversals, we'll get valid data, the freeing callback will wait at
230652042d8eSAndrea Gelmini 	 * least until the rcu_read_unlock.
230788c14590SDavid Sterba 	 */
230888c14590SDavid Sterba 	rcu_read_lock();
23099c5085c1SJosef Bacik 	cur_devices = fs_info->fs_devices;
23109c5085c1SJosef Bacik 	while (cur_devices) {
23119c5085c1SJosef Bacik 		head = &cur_devices->devices;
231288c14590SDavid Sterba 		list_for_each_entry_rcu(dev, head, dev_list) {
2313e6e674bdSAnand Jain 			if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
2314aa9ddcd4SJosef Bacik 				continue;
23150aeb8a6eSAnand Jain 			if (!dev->name)
23160aeb8a6eSAnand Jain 				continue;
23179c5085c1SJosef Bacik 			if (!first_dev || dev->devid < first_dev->devid)
23189c5085c1SJosef Bacik 				first_dev = dev;
23199c5085c1SJosef Bacik 		}
23209c5085c1SJosef Bacik 		cur_devices = cur_devices->seed;
23219c5085c1SJosef Bacik 	}
23229c5085c1SJosef Bacik 
2323672d5990SMisono Tomohiro 	if (first_dev)
2324672d5990SMisono Tomohiro 		seq_escape(m, rcu_str_deref(first_dev->name), " \t\n\\");
2325672d5990SMisono Tomohiro 	else
23269c5085c1SJosef Bacik 		WARN_ON(1);
232788c14590SDavid Sterba 	rcu_read_unlock();
23289c5085c1SJosef Bacik 	return 0;
23299c5085c1SJosef Bacik }
23309c5085c1SJosef Bacik 
2331b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
233276dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
2333bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
2334e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
2335d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
2336a9572a15SEric Paris 	.show_options	= btrfs_show_options,
23379c5085c1SJosef Bacik 	.show_devname	= btrfs_show_devname,
23382c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
23392c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
234026602cabSAl Viro 	.free_inode	= btrfs_free_inode,
23418fd17795SChris Mason 	.statfs		= btrfs_statfs,
2342c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
23430176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
23449e7cc91aSWang Xiaoguang 	.unfreeze_fs	= btrfs_unfreeze,
2345e20d96d6SChris Mason };
2346a9218f6bSChris Mason 
2347a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
2348d8620958STom Van Braeckel 	.open = btrfs_control_open,
2349a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
23501832f2d8SArnd Bergmann 	.compat_ioctl = compat_ptr_ioctl,
2351a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
23526038f373SArnd Bergmann 	.llseek = noop_llseek,
2353a9218f6bSChris Mason };
2354a9218f6bSChris Mason 
2355a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
2356578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
2357a9218f6bSChris Mason 	.name		= "btrfs-control",
2358a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
2359a9218f6bSChris Mason };
2360a9218f6bSChris Mason 
2361578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2362578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
2363578454ffSKay Sievers 
2364f5c29bd9SLiu Bo static int __init btrfs_interface_init(void)
2365a9218f6bSChris Mason {
2366a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
2367a9218f6bSChris Mason }
2368a9218f6bSChris Mason 
2369e67c718bSDavid Sterba static __cold void btrfs_interface_exit(void)
2370a9218f6bSChris Mason {
2371f368ed60SGreg Kroah-Hartman 	misc_deregister(&btrfs_misc);
2372a9218f6bSChris Mason }
2373a9218f6bSChris Mason 
2374f5c29bd9SLiu Bo static void __init btrfs_print_mod_info(void)
237585965600SDavid Sterba {
2376edf57cbfSBart Van Assche 	static const char options[] = ""
237785965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
237885965600SDavid Sterba 			", debug=on"
237985965600SDavid Sterba #endif
238079556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT
238179556c3dSStefan Behrens 			", assert=on"
238279556c3dSStefan Behrens #endif
238385965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
238485965600SDavid Sterba 			", integrity-checker=on"
238585965600SDavid Sterba #endif
2386fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2387fb592373SJosef Bacik 			", ref-verify=on"
2388fb592373SJosef Bacik #endif
2389edf57cbfSBart Van Assche 			;
2390edf57cbfSBart Van Assche 	pr_info("Btrfs loaded, crc32c=%s%s\n", crc32c_impl(), options);
239185965600SDavid Sterba }
239285965600SDavid Sterba 
23932e635a27SChris Mason static int __init init_btrfs_fs(void)
23942e635a27SChris Mason {
23952c90e5d6SChris Mason 	int err;
239658176a96SJosef Bacik 
239763541927SFilipe David Borba Manana 	btrfs_props_init();
239863541927SFilipe David Borba Manana 
239958176a96SJosef Bacik 	err = btrfs_init_sysfs();
240058176a96SJosef Bacik 	if (err)
24019678c543SNikolay Borisov 		return err;
240258176a96SJosef Bacik 
2403143bede5SJeff Mahoney 	btrfs_init_compress();
2404d1310b2eSChris Mason 
2405261507a0SLi Zefan 	err = btrfs_init_cachep();
2406261507a0SLi Zefan 	if (err)
2407261507a0SLi Zefan 		goto free_compress;
2408261507a0SLi Zefan 
2409d1310b2eSChris Mason 	err = extent_io_init();
24102f4cbe64SWyatt Banks 	if (err)
24112f4cbe64SWyatt Banks 		goto free_cachep;
24122f4cbe64SWyatt Banks 
24136f0d04f8SJosef Bacik 	err = extent_state_cache_init();
2414d1310b2eSChris Mason 	if (err)
2415d1310b2eSChris Mason 		goto free_extent_io;
2416d1310b2eSChris Mason 
24176f0d04f8SJosef Bacik 	err = extent_map_init();
24186f0d04f8SJosef Bacik 	if (err)
24196f0d04f8SJosef Bacik 		goto free_extent_state_cache;
24206f0d04f8SJosef Bacik 
24216352b91dSMiao Xie 	err = ordered_data_init();
24222f4cbe64SWyatt Banks 	if (err)
24232f4cbe64SWyatt Banks 		goto free_extent_map;
2424c8b97818SChris Mason 
24256352b91dSMiao Xie 	err = btrfs_delayed_inode_init();
24266352b91dSMiao Xie 	if (err)
24276352b91dSMiao Xie 		goto free_ordered_data;
24286352b91dSMiao Xie 
24299247f317SMiao Xie 	err = btrfs_auto_defrag_init();
243016cdcec7SMiao Xie 	if (err)
243116cdcec7SMiao Xie 		goto free_delayed_inode;
243216cdcec7SMiao Xie 
243378a6184aSMiao Xie 	err = btrfs_delayed_ref_init();
24349247f317SMiao Xie 	if (err)
24359247f317SMiao Xie 		goto free_auto_defrag;
24369247f317SMiao Xie 
2437b9e9a6cbSWang Shilong 	err = btrfs_prelim_ref_init();
2438b9e9a6cbSWang Shilong 	if (err)
2439af13b492SDavid Sterba 		goto free_delayed_ref;
2440b9e9a6cbSWang Shilong 
244197eb6b69SDavid Sterba 	err = btrfs_end_io_wq_init();
244278a6184aSMiao Xie 	if (err)
2443af13b492SDavid Sterba 		goto free_prelim_ref;
244478a6184aSMiao Xie 
244597eb6b69SDavid Sterba 	err = btrfs_interface_init();
244697eb6b69SDavid Sterba 	if (err)
244797eb6b69SDavid Sterba 		goto free_end_io_wq;
244897eb6b69SDavid Sterba 
2449e565d4b9SJan Schmidt 	btrfs_init_lockdep();
2450e565d4b9SJan Schmidt 
24518ae1af3cSAnand Jain 	btrfs_print_mod_info();
2452dc11dd5dSJosef Bacik 
2453dc11dd5dSJosef Bacik 	err = btrfs_run_sanity_tests();
2454dc11dd5dSJosef Bacik 	if (err)
2455dc11dd5dSJosef Bacik 		goto unregister_ioctl;
2456dc11dd5dSJosef Bacik 
2457dc11dd5dSJosef Bacik 	err = register_filesystem(&btrfs_fs_type);
2458dc11dd5dSJosef Bacik 	if (err)
2459dc11dd5dSJosef Bacik 		goto unregister_ioctl;
246074255aa0SJosef Bacik 
24612f4cbe64SWyatt Banks 	return 0;
24622f4cbe64SWyatt Banks 
2463a9218f6bSChris Mason unregister_ioctl:
2464a9218f6bSChris Mason 	btrfs_interface_exit();
246597eb6b69SDavid Sterba free_end_io_wq:
246697eb6b69SDavid Sterba 	btrfs_end_io_wq_exit();
2467b9e9a6cbSWang Shilong free_prelim_ref:
2468b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
246978a6184aSMiao Xie free_delayed_ref:
247078a6184aSMiao Xie 	btrfs_delayed_ref_exit();
24719247f317SMiao Xie free_auto_defrag:
24729247f317SMiao Xie 	btrfs_auto_defrag_exit();
247316cdcec7SMiao Xie free_delayed_inode:
247416cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
24756352b91dSMiao Xie free_ordered_data:
24766352b91dSMiao Xie 	ordered_data_exit();
24772f4cbe64SWyatt Banks free_extent_map:
24782f4cbe64SWyatt Banks 	extent_map_exit();
24796f0d04f8SJosef Bacik free_extent_state_cache:
24806f0d04f8SJosef Bacik 	extent_state_cache_exit();
2481d1310b2eSChris Mason free_extent_io:
2482d1310b2eSChris Mason 	extent_io_exit();
24832f4cbe64SWyatt Banks free_cachep:
24842f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
2485261507a0SLi Zefan free_compress:
2486261507a0SLi Zefan 	btrfs_exit_compress();
24872f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
24889678c543SNikolay Borisov 
24892c90e5d6SChris Mason 	return err;
24902e635a27SChris Mason }
24912e635a27SChris Mason 
24922e635a27SChris Mason static void __exit exit_btrfs_fs(void)
24932e635a27SChris Mason {
249439279cc3SChris Mason 	btrfs_destroy_cachep();
249578a6184aSMiao Xie 	btrfs_delayed_ref_exit();
24969247f317SMiao Xie 	btrfs_auto_defrag_exit();
249716cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
2498b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
24996352b91dSMiao Xie 	ordered_data_exit();
2500a52d9a80SChris Mason 	extent_map_exit();
25016f0d04f8SJosef Bacik 	extent_state_cache_exit();
2502d1310b2eSChris Mason 	extent_io_exit();
2503a9218f6bSChris Mason 	btrfs_interface_exit();
25045ed5f588SJosef Bacik 	btrfs_end_io_wq_exit();
25052e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
250658176a96SJosef Bacik 	btrfs_exit_sysfs();
25078a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
2508261507a0SLi Zefan 	btrfs_exit_compress();
25092e635a27SChris Mason }
25102e635a27SChris Mason 
251160efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs);
25122e635a27SChris Mason module_exit(exit_btrfs_fs)
25132e635a27SChris Mason 
25142e635a27SChris Mason MODULE_LICENSE("GPL");
2515d5178578SJohannes Thumshirn MODULE_SOFTDEP("pre: crc32c");
25163951e7f0SJohannes Thumshirn MODULE_SOFTDEP("pre: xxhash64");
25173831bf00SJohannes Thumshirn MODULE_SOFTDEP("pre: sha256");
2518352ae07bSDavid Sterba MODULE_SOFTDEP("pre: blake2b-256");
2519