xref: /linux/fs/btrfs/super.c (revision 90c711ab380d633bf85249bd5d819edb601feda7)
16cbd5570SChris Mason /*
26cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
194b82d6e4SYan #include <linux/blkdev.h>
202e635a27SChris Mason #include <linux/module.h>
21e20d96d6SChris Mason #include <linux/buffer_head.h>
222e635a27SChris Mason #include <linux/fs.h>
232e635a27SChris Mason #include <linux/pagemap.h>
242e635a27SChris Mason #include <linux/highmem.h>
252e635a27SChris Mason #include <linux/time.h>
262e635a27SChris Mason #include <linux/init.h>
27a9572a15SEric Paris #include <linux/seq_file.h>
282e635a27SChris Mason #include <linux/string.h>
292e635a27SChris Mason #include <linux/backing-dev.h>
304b82d6e4SYan #include <linux/mount.h>
31dee26a9fSChris Mason #include <linux/mpage.h>
3275dfe396SChris Mason #include <linux/swap.h>
3375dfe396SChris Mason #include <linux/writeback.h>
348fd17795SChris Mason #include <linux/statfs.h>
3508607c1bSChris Mason #include <linux/compat.h>
3695e05289SChris Mason #include <linux/parser.h>
37c59f8951SChris Mason #include <linux/ctype.h>
386da6abaeSChris Mason #include <linux/namei.h>
39a9218f6bSChris Mason #include <linux/miscdevice.h>
401bcbf313SQinghuang Feng #include <linux/magic.h>
415a0e3ad6STejun Heo #include <linux/slab.h>
4290a887c9SDan Magenheimer #include <linux/cleancache.h>
4322c44fe6SJosef Bacik #include <linux/ratelimit.h>
4455e301fdSFilipe Brandenburger #include <linux/btrfs.h>
4516cdcec7SMiao Xie #include "delayed-inode.h"
462e635a27SChris Mason #include "ctree.h"
47e20d96d6SChris Mason #include "disk-io.h"
48d5719762SChris Mason #include "transaction.h"
492c90e5d6SChris Mason #include "btrfs_inode.h"
503a686375SChris Mason #include "print-tree.h"
5114a958e6SFilipe David Borba Manana #include "hash.h"
5263541927SFilipe David Borba Manana #include "props.h"
535103e947SJosef Bacik #include "xattr.h"
548a4b83ccSChris Mason #include "volumes.h"
55be6e8dc0SBalaji Rao #include "export.h"
56c8b97818SChris Mason #include "compression.h"
579c5085c1SJosef Bacik #include "rcu-string.h"
588dabb742SStefan Behrens #include "dev-replace.h"
5974255aa0SJosef Bacik #include "free-space-cache.h"
60b9e9a6cbSWang Shilong #include "backref.h"
61dc11dd5dSJosef Bacik #include "tests/btrfs-tests.h"
622e635a27SChris Mason 
63d3982100SMark Fasheh #include "qgroup.h"
641abe9b8aSliubo #define CREATE_TRACE_POINTS
651abe9b8aSliubo #include <trace/events/btrfs.h>
661abe9b8aSliubo 
67b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
68830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type;
69e20d96d6SChris Mason 
700723a047SHarald Hoyer static int btrfs_remount(struct super_block *sb, int *flags, char *data);
710723a047SHarald Hoyer 
72e33e17eeSJeff Mahoney const char *btrfs_decode_error(int errno)
73acce952bSliubo {
7408748810SDavid Sterba 	char *errstr = "unknown";
75acce952bSliubo 
76acce952bSliubo 	switch (errno) {
77acce952bSliubo 	case -EIO:
78acce952bSliubo 		errstr = "IO failure";
79acce952bSliubo 		break;
80acce952bSliubo 	case -ENOMEM:
81acce952bSliubo 		errstr = "Out of memory";
82acce952bSliubo 		break;
83acce952bSliubo 	case -EROFS:
84acce952bSliubo 		errstr = "Readonly filesystem";
85acce952bSliubo 		break;
868c342930SJeff Mahoney 	case -EEXIST:
878c342930SJeff Mahoney 		errstr = "Object already exists";
888c342930SJeff Mahoney 		break;
8994ef7280SDavid Sterba 	case -ENOSPC:
9094ef7280SDavid Sterba 		errstr = "No space left";
9194ef7280SDavid Sterba 		break;
9294ef7280SDavid Sterba 	case -ENOENT:
9394ef7280SDavid Sterba 		errstr = "No such entry";
9494ef7280SDavid Sterba 		break;
95acce952bSliubo 	}
96acce952bSliubo 
97acce952bSliubo 	return errstr;
98acce952bSliubo }
99acce952bSliubo 
100acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */
101acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
102acce952bSliubo {
103acce952bSliubo 	struct super_block *sb = fs_info->sb;
104acce952bSliubo 
105acce952bSliubo 	if (sb->s_flags & MS_RDONLY)
106acce952bSliubo 		return;
107acce952bSliubo 
10887533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
109acce952bSliubo 		sb->s_flags |= MS_RDONLY;
110c2cf52ebSSimon Kirby 		btrfs_info(fs_info, "forced readonly");
1111acd6831SStefan Behrens 		/*
1121acd6831SStefan Behrens 		 * Note that a running device replace operation is not
1131acd6831SStefan Behrens 		 * canceled here although there is no way to update
1141acd6831SStefan Behrens 		 * the progress. It would add the risk of a deadlock,
11501327610SNicholas D Steeves 		 * therefore the canceling is omitted. The only penalty
1161acd6831SStefan Behrens 		 * is that some I/O remains active until the procedure
1171acd6831SStefan Behrens 		 * completes. The next time when the filesystem is
1181acd6831SStefan Behrens 		 * mounted writeable again, the device replace
1191acd6831SStefan Behrens 		 * operation continues.
1201acd6831SStefan Behrens 		 */
121acce952bSliubo 	}
122acce952bSliubo }
123acce952bSliubo 
124acce952bSliubo /*
12534d97007SAnand Jain  * __btrfs_handle_fs_error decodes expected errors from the caller and
126acce952bSliubo  * invokes the approciate error response.
127acce952bSliubo  */
128c0d19e2bSDavid Sterba __cold
12934d97007SAnand Jain void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
1304da35113SJeff Mahoney 		       unsigned int line, int errno, const char *fmt, ...)
131acce952bSliubo {
132acce952bSliubo 	struct super_block *sb = fs_info->sb;
13357d816a1SAnand Jain #ifdef CONFIG_PRINTK
134acce952bSliubo 	const char *errstr;
13557d816a1SAnand Jain #endif
136acce952bSliubo 
137acce952bSliubo 	/*
138acce952bSliubo 	 * Special case: if the error is EROFS, and we're already
139acce952bSliubo 	 * under MS_RDONLY, then it is safe here.
140acce952bSliubo 	 */
141acce952bSliubo 	if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
142acce952bSliubo   		return;
143acce952bSliubo 
14457d816a1SAnand Jain #ifdef CONFIG_PRINTK
14508748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
1464da35113SJeff Mahoney 	if (fmt) {
14737252a66SEric Sandeen 		struct va_format vaf;
14837252a66SEric Sandeen 		va_list args;
14937252a66SEric Sandeen 
15037252a66SEric Sandeen 		va_start(args, fmt);
15137252a66SEric Sandeen 		vaf.fmt = fmt;
15237252a66SEric Sandeen 		vaf.va = &args;
1534da35113SJeff Mahoney 
154efe120a0SFrank Holton 		printk(KERN_CRIT
155efe120a0SFrank Holton 			"BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n",
15608748810SDavid Sterba 			sb->s_id, function, line, errno, errstr, &vaf);
15737252a66SEric Sandeen 		va_end(args);
1584da35113SJeff Mahoney 	} else {
159efe120a0SFrank Holton 		printk(KERN_CRIT "BTRFS: error (device %s) in %s:%d: errno=%d %s\n",
16008748810SDavid Sterba 			sb->s_id, function, line, errno, errstr);
1614da35113SJeff Mahoney 	}
16257d816a1SAnand Jain #endif
163acce952bSliubo 
1640713d90cSAnand Jain 	/*
1650713d90cSAnand Jain 	 * Today we only save the error info to memory.  Long term we'll
1660713d90cSAnand Jain 	 * also send it down to the disk
1670713d90cSAnand Jain 	 */
1680713d90cSAnand Jain 	set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
1690713d90cSAnand Jain 
1704da35113SJeff Mahoney 	/* Don't go through full error handling during mount */
171cf79ffb5SJosef Bacik 	if (sb->s_flags & MS_BORN)
172acce952bSliubo 		btrfs_handle_error(fs_info);
173acce952bSliubo }
1744da35113SJeff Mahoney 
17557d816a1SAnand Jain #ifdef CONFIG_PRINTK
176533574c6SJoe Perches static const char * const logtypes[] = {
1774da35113SJeff Mahoney 	"emergency",
1784da35113SJeff Mahoney 	"alert",
1794da35113SJeff Mahoney 	"critical",
1804da35113SJeff Mahoney 	"error",
1814da35113SJeff Mahoney 	"warning",
1824da35113SJeff Mahoney 	"notice",
1834da35113SJeff Mahoney 	"info",
1844da35113SJeff Mahoney 	"debug",
1854da35113SJeff Mahoney };
1864da35113SJeff Mahoney 
187c2cf52ebSSimon Kirby void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
1884da35113SJeff Mahoney {
1894da35113SJeff Mahoney 	struct super_block *sb = fs_info->sb;
1904da35113SJeff Mahoney 	char lvl[4];
1914da35113SJeff Mahoney 	struct va_format vaf;
1924da35113SJeff Mahoney 	va_list args;
1934da35113SJeff Mahoney 	const char *type = logtypes[4];
194533574c6SJoe Perches 	int kern_level;
1954da35113SJeff Mahoney 
1964da35113SJeff Mahoney 	va_start(args, fmt);
1974da35113SJeff Mahoney 
198533574c6SJoe Perches 	kern_level = printk_get_level(fmt);
199533574c6SJoe Perches 	if (kern_level) {
200533574c6SJoe Perches 		size_t size = printk_skip_level(fmt) - fmt;
201533574c6SJoe Perches 		memcpy(lvl, fmt,  size);
202533574c6SJoe Perches 		lvl[size] = '\0';
203533574c6SJoe Perches 		fmt += size;
204533574c6SJoe Perches 		type = logtypes[kern_level - '0'];
2054da35113SJeff Mahoney 	} else
2064da35113SJeff Mahoney 		*lvl = '\0';
2074da35113SJeff Mahoney 
2084da35113SJeff Mahoney 	vaf.fmt = fmt;
2094da35113SJeff Mahoney 	vaf.va = &args;
210533574c6SJoe Perches 
211c2cf52ebSSimon Kirby 	printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf);
212533574c6SJoe Perches 
213533574c6SJoe Perches 	va_end(args);
2144da35113SJeff Mahoney }
215533574c6SJoe Perches #endif
216533574c6SJoe Perches 
2178c342930SJeff Mahoney /*
21849b25e05SJeff Mahoney  * We only mark the transaction aborted and then set the file system read-only.
21949b25e05SJeff Mahoney  * This will prevent new transactions from starting or trying to join this
22049b25e05SJeff Mahoney  * one.
22149b25e05SJeff Mahoney  *
22249b25e05SJeff Mahoney  * This means that error recovery at the call site is limited to freeing
22349b25e05SJeff Mahoney  * any local memory allocations and passing the error code up without
22449b25e05SJeff Mahoney  * further cleanup. The transaction should complete as it normally would
22549b25e05SJeff Mahoney  * in the call path but will return -EIO.
22649b25e05SJeff Mahoney  *
22749b25e05SJeff Mahoney  * We'll complete the cleanup in btrfs_end_transaction and
22849b25e05SJeff Mahoney  * btrfs_commit_transaction.
22949b25e05SJeff Mahoney  */
230c0d19e2bSDavid Sterba __cold
23149b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
23249b25e05SJeff Mahoney 			       struct btrfs_root *root, const char *function,
23349b25e05SJeff Mahoney 			       unsigned int line, int errno)
23449b25e05SJeff Mahoney {
23549b25e05SJeff Mahoney 	trans->aborted = errno;
23649b25e05SJeff Mahoney 	/* Nothing used. The other threads that have joined this
23749b25e05SJeff Mahoney 	 * transaction may be able to continue. */
23864c12921SJeff Mahoney 	if (!trans->dirty && list_empty(&trans->new_bgs)) {
23969ce977aSMiao Xie 		const char *errstr;
24069ce977aSMiao Xie 
24108748810SDavid Sterba 		errstr = btrfs_decode_error(errno);
242c2cf52ebSSimon Kirby 		btrfs_warn(root->fs_info,
243c2cf52ebSSimon Kirby 		           "%s:%d: Aborting unused transaction(%s).",
24469ce977aSMiao Xie 		           function, line, errstr);
24549b25e05SJeff Mahoney 		return;
24649b25e05SJeff Mahoney 	}
2478d25a086SMiao Xie 	ACCESS_ONCE(trans->transaction->aborted) = errno;
248501407aaSJosef Bacik 	/* Wake up anybody who may be waiting on this transaction */
249501407aaSJosef Bacik 	wake_up(&root->fs_info->transaction_wait);
250501407aaSJosef Bacik 	wake_up(&root->fs_info->transaction_blocked_wait);
25134d97007SAnand Jain 	__btrfs_handle_fs_error(root->fs_info, function, line, errno, NULL);
25249b25e05SJeff Mahoney }
25349b25e05SJeff Mahoney /*
2548c342930SJeff Mahoney  * __btrfs_panic decodes unexpected, fatal errors from the caller,
2558c342930SJeff Mahoney  * issues an alert, and either panics or BUGs, depending on mount options.
2568c342930SJeff Mahoney  */
257c0d19e2bSDavid Sterba __cold
2588c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
2598c342930SJeff Mahoney 		   unsigned int line, int errno, const char *fmt, ...)
2608c342930SJeff Mahoney {
2618c342930SJeff Mahoney 	char *s_id = "<unknown>";
2628c342930SJeff Mahoney 	const char *errstr;
2638c342930SJeff Mahoney 	struct va_format vaf = { .fmt = fmt };
2648c342930SJeff Mahoney 	va_list args;
2658c342930SJeff Mahoney 
2668c342930SJeff Mahoney 	if (fs_info)
2678c342930SJeff Mahoney 		s_id = fs_info->sb->s_id;
2688c342930SJeff Mahoney 
2698c342930SJeff Mahoney 	va_start(args, fmt);
2708c342930SJeff Mahoney 	vaf.va = &args;
2718c342930SJeff Mahoney 
27208748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
273aa43a17cSEric Sandeen 	if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR))
27408748810SDavid Sterba 		panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
27508748810SDavid Sterba 			s_id, function, line, &vaf, errno, errstr);
2768c342930SJeff Mahoney 
277efe120a0SFrank Holton 	btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
278efe120a0SFrank Holton 		   function, line, &vaf, errno, errstr);
2798c342930SJeff Mahoney 	va_end(args);
2808c342930SJeff Mahoney 	/* Caller calls BUG() */
2818c342930SJeff Mahoney }
282acce952bSliubo 
283e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
284e20d96d6SChris Mason {
2853abdbd78SDavid Sterba 	close_ctree(btrfs_sb(sb)->tree_root);
286e20d96d6SChris Mason }
2872e635a27SChris Mason 
28895e05289SChris Mason enum {
28973f73415SJosef Bacik 	Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
290287a0ab9SJosef Bacik 	Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
291287a0ab9SJosef Bacik 	Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
292261507a0SLi Zefan 	Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
293261507a0SLi Zefan 	Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
29470f6d82eSOmar Sandoval 	Opt_space_cache, Opt_space_cache_version, Opt_clear_cache,
29570f6d82eSOmar Sandoval 	Opt_user_subvol_rm_allowed, Opt_enospc_debug, Opt_subvolrootid,
29670f6d82eSOmar Sandoval 	Opt_defrag, Opt_inode_cache, Opt_no_space_cache, Opt_recovery,
29770f6d82eSOmar Sandoval 	Opt_skip_balance, Opt_check_integrity,
29870f6d82eSOmar Sandoval 	Opt_check_integrity_including_extent_data,
299f420ee1eSStefan Behrens 	Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree,
300e07a2adeSQu Wenruo 	Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard,
301a258af7aSQu Wenruo 	Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow,
3028dcddfa0SQu Wenruo 	Opt_datasum, Opt_treelog, Opt_noinode_cache, Opt_usebackuproot,
303fed8f166SQu Wenruo 	Opt_nologreplay, Opt_norecovery,
304d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
305d0bd4560SJosef Bacik 	Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
306d0bd4560SJosef Bacik #endif
3079555c6c1SIlya Dryomov 	Opt_err,
30895e05289SChris Mason };
30995e05289SChris Mason 
3104d4ab6d6SDavid Sterba static const match_table_t tokens = {
311dfe25020SChris Mason 	{Opt_degraded, "degraded"},
31295e05289SChris Mason 	{Opt_subvol, "subvol=%s"},
3131493381fSWang Shilong 	{Opt_subvolid, "subvolid=%s"},
31443e570b0SChristoph Hellwig 	{Opt_device, "device=%s"},
315b6cda9bcSChris Mason 	{Opt_nodatasum, "nodatasum"},
316d399167dSQu Wenruo 	{Opt_datasum, "datasum"},
317be20aa9dSChris Mason 	{Opt_nodatacow, "nodatacow"},
318a258af7aSQu Wenruo 	{Opt_datacow, "datacow"},
31921ad10cfSChris Mason 	{Opt_nobarrier, "nobarrier"},
320842bef58SQu Wenruo 	{Opt_barrier, "barrier"},
3216f568d35SChris Mason 	{Opt_max_inline, "max_inline=%s"},
3228f662a76SChris Mason 	{Opt_alloc_start, "alloc_start=%s"},
3234543df7eSChris Mason 	{Opt_thread_pool, "thread_pool=%d"},
324c8b97818SChris Mason 	{Opt_compress, "compress"},
325261507a0SLi Zefan 	{Opt_compress_type, "compress=%s"},
326a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
327261507a0SLi Zefan 	{Opt_compress_force_type, "compress-force=%s"},
328e18e4809SChris Mason 	{Opt_ssd, "ssd"},
329451d7585SChris Mason 	{Opt_ssd_spread, "ssd_spread"},
3303b30c22fSChris Mason 	{Opt_nossd, "nossd"},
331bd0330adSQu Wenruo 	{Opt_acl, "acl"},
33233268eafSJosef Bacik 	{Opt_noacl, "noacl"},
3333a5e1404SSage Weil 	{Opt_notreelog, "notreelog"},
334a88998f2SQu Wenruo 	{Opt_treelog, "treelog"},
33596da0919SQu Wenruo 	{Opt_nologreplay, "nologreplay"},
336fed8f166SQu Wenruo 	{Opt_norecovery, "norecovery"},
337dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
3382c9ee856SQu Wenruo 	{Opt_noflushoncommit, "noflushoncommit"},
33997e728d4SJosef Bacik 	{Opt_ratio, "metadata_ratio=%d"},
340e244a0aeSChristoph Hellwig 	{Opt_discard, "discard"},
341e07a2adeSQu Wenruo 	{Opt_nodiscard, "nodiscard"},
3420af3d00bSJosef Bacik 	{Opt_space_cache, "space_cache"},
34370f6d82eSOmar Sandoval 	{Opt_space_cache_version, "space_cache=%s"},
34488c2ba3bSJosef Bacik 	{Opt_clear_cache, "clear_cache"},
3454260f7c7SSage Weil 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
34691435650SChris Mason 	{Opt_enospc_debug, "enospc_debug"},
34753036293SQu Wenruo 	{Opt_noenospc_debug, "noenospc_debug"},
348e15d0542SXin Zhong 	{Opt_subvolrootid, "subvolrootid=%d"},
3494cb5300bSChris Mason 	{Opt_defrag, "autodefrag"},
350fc0ca9afSQu Wenruo 	{Opt_nodefrag, "noautodefrag"},
3514b9465cbSChris Mason 	{Opt_inode_cache, "inode_cache"},
3523818aea2SQu Wenruo 	{Opt_noinode_cache, "noinode_cache"},
3538965593eSDavid Sterba 	{Opt_no_space_cache, "nospace_cache"},
3548dcddfa0SQu Wenruo 	{Opt_recovery, "recovery"}, /* deprecated */
3558dcddfa0SQu Wenruo 	{Opt_usebackuproot, "usebackuproot"},
3569555c6c1SIlya Dryomov 	{Opt_skip_balance, "skip_balance"},
35721adbd5cSStefan Behrens 	{Opt_check_integrity, "check_int"},
35821adbd5cSStefan Behrens 	{Opt_check_integrity_including_extent_data, "check_int_data"},
35921adbd5cSStefan Behrens 	{Opt_check_integrity_print_mask, "check_int_print_mask=%d"},
360f420ee1eSStefan Behrens 	{Opt_rescan_uuid_tree, "rescan_uuid_tree"},
3618c342930SJeff Mahoney 	{Opt_fatal_errors, "fatal_errors=%s"},
3628b87dc17SDavid Sterba 	{Opt_commit_interval, "commit=%d"},
363d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
364d0bd4560SJosef Bacik 	{Opt_fragment_data, "fragment=data"},
365d0bd4560SJosef Bacik 	{Opt_fragment_metadata, "fragment=metadata"},
366d0bd4560SJosef Bacik 	{Opt_fragment_all, "fragment=all"},
367d0bd4560SJosef Bacik #endif
36833268eafSJosef Bacik 	{Opt_err, NULL},
36995e05289SChris Mason };
37095e05289SChris Mason 
371edf24abeSChristoph Hellwig /*
372edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
373edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
37449b25e05SJeff Mahoney  * XXX JDM: This needs to be cleaned up for remount.
375edf24abeSChristoph Hellwig  */
37696da0919SQu Wenruo int btrfs_parse_options(struct btrfs_root *root, char *options,
37796da0919SQu Wenruo 			unsigned long new_flags)
37895e05289SChris Mason {
379edf24abeSChristoph Hellwig 	struct btrfs_fs_info *info = root->fs_info;
38095e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
38173bc1876SJosef Bacik 	char *p, *num, *orig = NULL;
38273bc1876SJosef Bacik 	u64 cache_gen;
3834543df7eSChris Mason 	int intarg;
384a7a3f7caSSage Weil 	int ret = 0;
385261507a0SLi Zefan 	char *compress_type;
386261507a0SLi Zefan 	bool compress_force = false;
387b7c47bbbSTsutomu Itoh 	enum btrfs_compression_type saved_compress_type;
388b7c47bbbSTsutomu Itoh 	bool saved_compress_force;
389b7c47bbbSTsutomu Itoh 	int no_compress = 0;
390b6cda9bcSChris Mason 
3916c41761fSDavid Sterba 	cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
39270f6d82eSOmar Sandoval 	if (btrfs_fs_compat_ro(root->fs_info, FREE_SPACE_TREE))
39370f6d82eSOmar Sandoval 		btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
39470f6d82eSOmar Sandoval 	else if (cache_gen)
39573bc1876SJosef Bacik 		btrfs_set_opt(info->mount_opt, SPACE_CACHE);
39673bc1876SJosef Bacik 
39796da0919SQu Wenruo 	/*
39896da0919SQu Wenruo 	 * Even the options are empty, we still need to do extra check
39996da0919SQu Wenruo 	 * against new flags
40096da0919SQu Wenruo 	 */
40195e05289SChris Mason 	if (!options)
40296da0919SQu Wenruo 		goto check;
40395e05289SChris Mason 
404be20aa9dSChris Mason 	/*
405be20aa9dSChris Mason 	 * strsep changes the string, duplicate it because parse_options
406be20aa9dSChris Mason 	 * gets called twice
407be20aa9dSChris Mason 	 */
408be20aa9dSChris Mason 	options = kstrdup(options, GFP_NOFS);
409be20aa9dSChris Mason 	if (!options)
410be20aa9dSChris Mason 		return -ENOMEM;
411be20aa9dSChris Mason 
412da495eccSJosef Bacik 	orig = options;
413be20aa9dSChris Mason 
41495e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
41595e05289SChris Mason 		int token;
41695e05289SChris Mason 		if (!*p)
41795e05289SChris Mason 			continue;
41895e05289SChris Mason 
41995e05289SChris Mason 		token = match_token(p, tokens, args);
42095e05289SChris Mason 		switch (token) {
421dfe25020SChris Mason 		case Opt_degraded:
422efe120a0SFrank Holton 			btrfs_info(root->fs_info, "allowing degraded mounts");
423dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
424dfe25020SChris Mason 			break;
42595e05289SChris Mason 		case Opt_subvol:
42673f73415SJosef Bacik 		case Opt_subvolid:
427e15d0542SXin Zhong 		case Opt_subvolrootid:
42843e570b0SChristoph Hellwig 		case Opt_device:
429edf24abeSChristoph Hellwig 			/*
43043e570b0SChristoph Hellwig 			 * These are parsed by btrfs_parse_early_options
431edf24abeSChristoph Hellwig 			 * and can be happily ignored here.
432edf24abeSChristoph Hellwig 			 */
43395e05289SChris Mason 			break;
434b6cda9bcSChris Mason 		case Opt_nodatasum:
43507802534SQu Wenruo 			btrfs_set_and_info(root, NODATASUM,
43607802534SQu Wenruo 					   "setting nodatasum");
437be20aa9dSChris Mason 			break;
438d399167dSQu Wenruo 		case Opt_datasum:
43907802534SQu Wenruo 			if (btrfs_test_opt(root, NODATASUM)) {
440d399167dSQu Wenruo 				if (btrfs_test_opt(root, NODATACOW))
441d399167dSQu Wenruo 					btrfs_info(root->fs_info, "setting datasum, datacow enabled");
442d399167dSQu Wenruo 				else
443d399167dSQu Wenruo 					btrfs_info(root->fs_info, "setting datasum");
44407802534SQu Wenruo 			}
445d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATACOW);
446d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATASUM);
447d399167dSQu Wenruo 			break;
448be20aa9dSChris Mason 		case Opt_nodatacow:
44907802534SQu Wenruo 			if (!btrfs_test_opt(root, NODATACOW)) {
450bedb2ccaSAndrei Popa 				if (!btrfs_test_opt(root, COMPRESS) ||
451bedb2ccaSAndrei Popa 				    !btrfs_test_opt(root, FORCE_COMPRESS)) {
452efe120a0SFrank Holton 					btrfs_info(root->fs_info,
453efe120a0SFrank Holton 						   "setting nodatacow, compression disabled");
454bedb2ccaSAndrei Popa 				} else {
455efe120a0SFrank Holton 					btrfs_info(root->fs_info, "setting nodatacow");
456bedb2ccaSAndrei Popa 				}
45707802534SQu Wenruo 			}
458bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, COMPRESS);
459bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
460be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
461be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
462b6cda9bcSChris Mason 			break;
463a258af7aSQu Wenruo 		case Opt_datacow:
46407802534SQu Wenruo 			btrfs_clear_and_info(root, NODATACOW,
46507802534SQu Wenruo 					     "setting datacow");
466a258af7aSQu Wenruo 			break;
467a555f810SChris Mason 		case Opt_compress_force:
468261507a0SLi Zefan 		case Opt_compress_force_type:
469261507a0SLi Zefan 			compress_force = true;
4701c697d4aSEric Sandeen 			/* Fallthrough */
471261507a0SLi Zefan 		case Opt_compress:
472261507a0SLi Zefan 		case Opt_compress_type:
473b7c47bbbSTsutomu Itoh 			saved_compress_type = btrfs_test_opt(root, COMPRESS) ?
474b7c47bbbSTsutomu Itoh 				info->compress_type : BTRFS_COMPRESS_NONE;
475b7c47bbbSTsutomu Itoh 			saved_compress_force =
476b7c47bbbSTsutomu Itoh 				btrfs_test_opt(root, FORCE_COMPRESS);
477261507a0SLi Zefan 			if (token == Opt_compress ||
478261507a0SLi Zefan 			    token == Opt_compress_force ||
479261507a0SLi Zefan 			    strcmp(args[0].from, "zlib") == 0) {
480261507a0SLi Zefan 				compress_type = "zlib";
481261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
482063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
483bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
484bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
485b7c47bbbSTsutomu Itoh 				no_compress = 0;
486a6fa6faeSLi Zefan 			} else if (strcmp(args[0].from, "lzo") == 0) {
487a6fa6faeSLi Zefan 				compress_type = "lzo";
488a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
489063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
490bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
491bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
4922b0ce2c2SMitch Harder 				btrfs_set_fs_incompat(info, COMPRESS_LZO);
493b7c47bbbSTsutomu Itoh 				no_compress = 0;
494063849eaSArnd Hannemann 			} else if (strncmp(args[0].from, "no", 2) == 0) {
495063849eaSArnd Hannemann 				compress_type = "no";
496063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, COMPRESS);
497063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
498063849eaSArnd Hannemann 				compress_force = false;
499b7c47bbbSTsutomu Itoh 				no_compress++;
500261507a0SLi Zefan 			} else {
501261507a0SLi Zefan 				ret = -EINVAL;
502261507a0SLi Zefan 				goto out;
503261507a0SLi Zefan 			}
504261507a0SLi Zefan 
505261507a0SLi Zefan 			if (compress_force) {
506b7c47bbbSTsutomu Itoh 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
507143f3636SDavid Sterba 			} else {
5084027e0f4SWang Shilong 				/*
5094027e0f4SWang Shilong 				 * If we remount from compress-force=xxx to
5104027e0f4SWang Shilong 				 * compress=xxx, we need clear FORCE_COMPRESS
5114027e0f4SWang Shilong 				 * flag, otherwise, there is no way for users
5124027e0f4SWang Shilong 				 * to disable forcible compression separately.
5134027e0f4SWang Shilong 				 */
5144027e0f4SWang Shilong 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
515a7e252afSMiao Xie 			}
516b7c47bbbSTsutomu Itoh 			if ((btrfs_test_opt(root, COMPRESS) &&
517b7c47bbbSTsutomu Itoh 			     (info->compress_type != saved_compress_type ||
518b7c47bbbSTsutomu Itoh 			      compress_force != saved_compress_force)) ||
519b7c47bbbSTsutomu Itoh 			    (!btrfs_test_opt(root, COMPRESS) &&
520b7c47bbbSTsutomu Itoh 			     no_compress == 1)) {
521b7c47bbbSTsutomu Itoh 				btrfs_info(root->fs_info,
522b7c47bbbSTsutomu Itoh 					   "%s %s compression",
523b7c47bbbSTsutomu Itoh 					   (compress_force) ? "force" : "use",
524b7c47bbbSTsutomu Itoh 					   compress_type);
525b7c47bbbSTsutomu Itoh 			}
526b7c47bbbSTsutomu Itoh 			compress_force = false;
527a555f810SChris Mason 			break;
528e18e4809SChris Mason 		case Opt_ssd:
52907802534SQu Wenruo 			btrfs_set_and_info(root, SSD,
53007802534SQu Wenruo 					   "use ssd allocation scheme");
531e18e4809SChris Mason 			break;
532451d7585SChris Mason 		case Opt_ssd_spread:
53307802534SQu Wenruo 			btrfs_set_and_info(root, SSD_SPREAD,
53407802534SQu Wenruo 					   "use spread ssd allocation scheme");
5352aa06a35SEric Sandeen 			btrfs_set_opt(info->mount_opt, SSD);
536451d7585SChris Mason 			break;
5373b30c22fSChris Mason 		case Opt_nossd:
5382aa06a35SEric Sandeen 			btrfs_set_and_info(root, NOSSD,
53907802534SQu Wenruo 					     "not using ssd allocation scheme");
5403b30c22fSChris Mason 			btrfs_clear_opt(info->mount_opt, SSD);
5413b30c22fSChris Mason 			break;
542842bef58SQu Wenruo 		case Opt_barrier:
54307802534SQu Wenruo 			btrfs_clear_and_info(root, NOBARRIER,
54407802534SQu Wenruo 					     "turning on barriers");
545842bef58SQu Wenruo 			break;
54621ad10cfSChris Mason 		case Opt_nobarrier:
54707802534SQu Wenruo 			btrfs_set_and_info(root, NOBARRIER,
54807802534SQu Wenruo 					   "turning off barriers");
54921ad10cfSChris Mason 			break;
5504543df7eSChris Mason 		case Opt_thread_pool:
5512c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
5522c334e87SWang Shilong 			if (ret) {
5532c334e87SWang Shilong 				goto out;
5542c334e87SWang Shilong 			} else if (intarg > 0) {
5554543df7eSChris Mason 				info->thread_pool_size = intarg;
5562c334e87SWang Shilong 			} else {
5572c334e87SWang Shilong 				ret = -EINVAL;
5582c334e87SWang Shilong 				goto out;
5592c334e87SWang Shilong 			}
5604543df7eSChris Mason 			break;
5616f568d35SChris Mason 		case Opt_max_inline:
562edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
5636f568d35SChris Mason 			if (num) {
56491748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
5656f568d35SChris Mason 				kfree(num);
5666f568d35SChris Mason 
56715ada040SChris Mason 				if (info->max_inline) {
568feb5f965SMitch Harder 					info->max_inline = min_t(u64,
56915ada040SChris Mason 						info->max_inline,
57015ada040SChris Mason 						root->sectorsize);
57115ada040SChris Mason 				}
572efe120a0SFrank Holton 				btrfs_info(root->fs_info, "max_inline at %llu",
573c1c9ff7cSGeert Uytterhoeven 					info->max_inline);
5742c334e87SWang Shilong 			} else {
5752c334e87SWang Shilong 				ret = -ENOMEM;
5762c334e87SWang Shilong 				goto out;
5776f568d35SChris Mason 			}
5786f568d35SChris Mason 			break;
5798f662a76SChris Mason 		case Opt_alloc_start:
580edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
5818f662a76SChris Mason 			if (num) {
582c018daecSMiao Xie 				mutex_lock(&info->chunk_mutex);
58391748467SAkinobu Mita 				info->alloc_start = memparse(num, NULL);
584c018daecSMiao Xie 				mutex_unlock(&info->chunk_mutex);
5858f662a76SChris Mason 				kfree(num);
586efe120a0SFrank Holton 				btrfs_info(root->fs_info, "allocations start at %llu",
587c1c9ff7cSGeert Uytterhoeven 					info->alloc_start);
5882c334e87SWang Shilong 			} else {
5892c334e87SWang Shilong 				ret = -ENOMEM;
5902c334e87SWang Shilong 				goto out;
5918f662a76SChris Mason 			}
5928f662a76SChris Mason 			break;
593bd0330adSQu Wenruo 		case Opt_acl:
59445ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL
595bd0330adSQu Wenruo 			root->fs_info->sb->s_flags |= MS_POSIXACL;
596bd0330adSQu Wenruo 			break;
59745ff35d6SGuangliang Zhao #else
59845ff35d6SGuangliang Zhao 			btrfs_err(root->fs_info,
59945ff35d6SGuangliang Zhao 				"support for ACL not compiled in!");
60045ff35d6SGuangliang Zhao 			ret = -EINVAL;
60145ff35d6SGuangliang Zhao 			goto out;
60245ff35d6SGuangliang Zhao #endif
60333268eafSJosef Bacik 		case Opt_noacl:
60433268eafSJosef Bacik 			root->fs_info->sb->s_flags &= ~MS_POSIXACL;
60533268eafSJosef Bacik 			break;
6063a5e1404SSage Weil 		case Opt_notreelog:
60707802534SQu Wenruo 			btrfs_set_and_info(root, NOTREELOG,
60807802534SQu Wenruo 					   "disabling tree log");
6093a5e1404SSage Weil 			break;
610a88998f2SQu Wenruo 		case Opt_treelog:
61107802534SQu Wenruo 			btrfs_clear_and_info(root, NOTREELOG,
61207802534SQu Wenruo 					     "enabling tree log");
613a88998f2SQu Wenruo 			break;
614fed8f166SQu Wenruo 		case Opt_norecovery:
61596da0919SQu Wenruo 		case Opt_nologreplay:
61696da0919SQu Wenruo 			btrfs_set_and_info(root, NOLOGREPLAY,
61796da0919SQu Wenruo 					   "disabling log replay at mount time");
61896da0919SQu Wenruo 			break;
619dccae999SSage Weil 		case Opt_flushoncommit:
62007802534SQu Wenruo 			btrfs_set_and_info(root, FLUSHONCOMMIT,
62107802534SQu Wenruo 					   "turning on flush-on-commit");
622dccae999SSage Weil 			break;
6232c9ee856SQu Wenruo 		case Opt_noflushoncommit:
62407802534SQu Wenruo 			btrfs_clear_and_info(root, FLUSHONCOMMIT,
62507802534SQu Wenruo 					     "turning off flush-on-commit");
6262c9ee856SQu Wenruo 			break;
62797e728d4SJosef Bacik 		case Opt_ratio:
6282c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
6292c334e87SWang Shilong 			if (ret) {
6302c334e87SWang Shilong 				goto out;
6312c334e87SWang Shilong 			} else if (intarg >= 0) {
63297e728d4SJosef Bacik 				info->metadata_ratio = intarg;
633efe120a0SFrank Holton 				btrfs_info(root->fs_info, "metadata ratio %d",
63497e728d4SJosef Bacik 				       info->metadata_ratio);
6352c334e87SWang Shilong 			} else {
6362c334e87SWang Shilong 				ret = -EINVAL;
6372c334e87SWang Shilong 				goto out;
63897e728d4SJosef Bacik 			}
63997e728d4SJosef Bacik 			break;
640e244a0aeSChristoph Hellwig 		case Opt_discard:
64107802534SQu Wenruo 			btrfs_set_and_info(root, DISCARD,
64207802534SQu Wenruo 					   "turning on discard");
643e244a0aeSChristoph Hellwig 			break;
644e07a2adeSQu Wenruo 		case Opt_nodiscard:
64507802534SQu Wenruo 			btrfs_clear_and_info(root, DISCARD,
64607802534SQu Wenruo 					     "turning off discard");
647e07a2adeSQu Wenruo 			break;
6480af3d00bSJosef Bacik 		case Opt_space_cache:
64970f6d82eSOmar Sandoval 		case Opt_space_cache_version:
65070f6d82eSOmar Sandoval 			if (token == Opt_space_cache ||
65170f6d82eSOmar Sandoval 			    strcmp(args[0].from, "v1") == 0) {
65270f6d82eSOmar Sandoval 				btrfs_clear_opt(root->fs_info->mount_opt,
65370f6d82eSOmar Sandoval 						FREE_SPACE_TREE);
65407802534SQu Wenruo 				btrfs_set_and_info(root, SPACE_CACHE,
65507802534SQu Wenruo 						   "enabling disk space caching");
65670f6d82eSOmar Sandoval 			} else if (strcmp(args[0].from, "v2") == 0) {
65770f6d82eSOmar Sandoval 				btrfs_clear_opt(root->fs_info->mount_opt,
65870f6d82eSOmar Sandoval 						SPACE_CACHE);
65970f6d82eSOmar Sandoval 				btrfs_set_and_info(root, FREE_SPACE_TREE,
66070f6d82eSOmar Sandoval 						   "enabling free space tree");
66170f6d82eSOmar Sandoval 			} else {
66270f6d82eSOmar Sandoval 				ret = -EINVAL;
66370f6d82eSOmar Sandoval 				goto out;
66470f6d82eSOmar Sandoval 			}
6650de90876SJosef Bacik 			break;
666f420ee1eSStefan Behrens 		case Opt_rescan_uuid_tree:
667f420ee1eSStefan Behrens 			btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
668f420ee1eSStefan Behrens 			break;
66973bc1876SJosef Bacik 		case Opt_no_space_cache:
67070f6d82eSOmar Sandoval 			if (btrfs_test_opt(root, SPACE_CACHE)) {
67107802534SQu Wenruo 				btrfs_clear_and_info(root, SPACE_CACHE,
67207802534SQu Wenruo 						     "disabling disk space caching");
67370f6d82eSOmar Sandoval 			}
67470f6d82eSOmar Sandoval 			if (btrfs_test_opt(root, FREE_SPACE_TREE)) {
67570f6d82eSOmar Sandoval 				btrfs_clear_and_info(root, FREE_SPACE_TREE,
67670f6d82eSOmar Sandoval 						     "disabling free space tree");
67770f6d82eSOmar Sandoval 			}
67873bc1876SJosef Bacik 			break;
6794b9465cbSChris Mason 		case Opt_inode_cache:
6807e1876acSDavid Sterba 			btrfs_set_pending_and_info(info, INODE_MAP_CACHE,
68107802534SQu Wenruo 					   "enabling inode map caching");
6823818aea2SQu Wenruo 			break;
6833818aea2SQu Wenruo 		case Opt_noinode_cache:
6847e1876acSDavid Sterba 			btrfs_clear_pending_and_info(info, INODE_MAP_CACHE,
68507802534SQu Wenruo 					     "disabling inode map caching");
6864b9465cbSChris Mason 			break;
68788c2ba3bSJosef Bacik 		case Opt_clear_cache:
68807802534SQu Wenruo 			btrfs_set_and_info(root, CLEAR_CACHE,
68907802534SQu Wenruo 					   "force clearing of disk cache");
6900af3d00bSJosef Bacik 			break;
6914260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
6924260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
6934260f7c7SSage Weil 			break;
69491435650SChris Mason 		case Opt_enospc_debug:
69591435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
69691435650SChris Mason 			break;
69753036293SQu Wenruo 		case Opt_noenospc_debug:
69853036293SQu Wenruo 			btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
69953036293SQu Wenruo 			break;
7004cb5300bSChris Mason 		case Opt_defrag:
70107802534SQu Wenruo 			btrfs_set_and_info(root, AUTO_DEFRAG,
70207802534SQu Wenruo 					   "enabling auto defrag");
7034cb5300bSChris Mason 			break;
704fc0ca9afSQu Wenruo 		case Opt_nodefrag:
70507802534SQu Wenruo 			btrfs_clear_and_info(root, AUTO_DEFRAG,
70607802534SQu Wenruo 					     "disabling auto defrag");
707fc0ca9afSQu Wenruo 			break;
708af31f5e5SChris Mason 		case Opt_recovery:
7098dcddfa0SQu Wenruo 			btrfs_warn(root->fs_info,
7108dcddfa0SQu Wenruo 				   "'recovery' is deprecated, use 'usebackuproot' instead");
7118dcddfa0SQu Wenruo 		case Opt_usebackuproot:
7128dcddfa0SQu Wenruo 			btrfs_info(root->fs_info,
7138dcddfa0SQu Wenruo 				   "trying to use backup root at mount time");
7148dcddfa0SQu Wenruo 			btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
715af31f5e5SChris Mason 			break;
7169555c6c1SIlya Dryomov 		case Opt_skip_balance:
7179555c6c1SIlya Dryomov 			btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
7189555c6c1SIlya Dryomov 			break;
71921adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
72021adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
721efe120a0SFrank Holton 			btrfs_info(root->fs_info,
722efe120a0SFrank Holton 				   "enabling check integrity including extent data");
72321adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt,
72421adbd5cSStefan Behrens 				      CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
72521adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
72621adbd5cSStefan Behrens 			break;
72721adbd5cSStefan Behrens 		case Opt_check_integrity:
728efe120a0SFrank Holton 			btrfs_info(root->fs_info, "enabling check integrity");
72921adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
73021adbd5cSStefan Behrens 			break;
73121adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
7322c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
7332c334e87SWang Shilong 			if (ret) {
7342c334e87SWang Shilong 				goto out;
7352c334e87SWang Shilong 			} else if (intarg >= 0) {
73621adbd5cSStefan Behrens 				info->check_integrity_print_mask = intarg;
737efe120a0SFrank Holton 				btrfs_info(root->fs_info, "check_integrity_print_mask 0x%x",
73821adbd5cSStefan Behrens 				       info->check_integrity_print_mask);
7392c334e87SWang Shilong 			} else {
7402c334e87SWang Shilong 				ret = -EINVAL;
7412c334e87SWang Shilong 				goto out;
74221adbd5cSStefan Behrens 			}
74321adbd5cSStefan Behrens 			break;
74421adbd5cSStefan Behrens #else
74521adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
74621adbd5cSStefan Behrens 		case Opt_check_integrity:
74721adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
748efe120a0SFrank Holton 			btrfs_err(root->fs_info,
749efe120a0SFrank Holton 				"support for check_integrity* not compiled in!");
75021adbd5cSStefan Behrens 			ret = -EINVAL;
75121adbd5cSStefan Behrens 			goto out;
75221adbd5cSStefan Behrens #endif
7538c342930SJeff Mahoney 		case Opt_fatal_errors:
7548c342930SJeff Mahoney 			if (strcmp(args[0].from, "panic") == 0)
7558c342930SJeff Mahoney 				btrfs_set_opt(info->mount_opt,
7568c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
7578c342930SJeff Mahoney 			else if (strcmp(args[0].from, "bug") == 0)
7588c342930SJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
7598c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
7608c342930SJeff Mahoney 			else {
7618c342930SJeff Mahoney 				ret = -EINVAL;
7628c342930SJeff Mahoney 				goto out;
7638c342930SJeff Mahoney 			}
7648c342930SJeff Mahoney 			break;
7658b87dc17SDavid Sterba 		case Opt_commit_interval:
7668b87dc17SDavid Sterba 			intarg = 0;
7678b87dc17SDavid Sterba 			ret = match_int(&args[0], &intarg);
7688b87dc17SDavid Sterba 			if (ret < 0) {
769efe120a0SFrank Holton 				btrfs_err(root->fs_info, "invalid commit interval");
7708b87dc17SDavid Sterba 				ret = -EINVAL;
7718b87dc17SDavid Sterba 				goto out;
7728b87dc17SDavid Sterba 			}
7738b87dc17SDavid Sterba 			if (intarg > 0) {
7748b87dc17SDavid Sterba 				if (intarg > 300) {
775efe120a0SFrank Holton 					btrfs_warn(root->fs_info, "excessive commit interval %d",
7768b87dc17SDavid Sterba 							intarg);
7778b87dc17SDavid Sterba 				}
7788b87dc17SDavid Sterba 				info->commit_interval = intarg;
7798b87dc17SDavid Sterba 			} else {
780efe120a0SFrank Holton 				btrfs_info(root->fs_info, "using default commit interval %ds",
7818b87dc17SDavid Sterba 				    BTRFS_DEFAULT_COMMIT_INTERVAL);
7828b87dc17SDavid Sterba 				info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
7838b87dc17SDavid Sterba 			}
7848b87dc17SDavid Sterba 			break;
785d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
786d0bd4560SJosef Bacik 		case Opt_fragment_all:
787d0bd4560SJosef Bacik 			btrfs_info(root->fs_info, "fragmenting all space");
788d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
789d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
790d0bd4560SJosef Bacik 			break;
791d0bd4560SJosef Bacik 		case Opt_fragment_metadata:
792d0bd4560SJosef Bacik 			btrfs_info(root->fs_info, "fragmenting metadata");
793d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt,
794d0bd4560SJosef Bacik 				      FRAGMENT_METADATA);
795d0bd4560SJosef Bacik 			break;
796d0bd4560SJosef Bacik 		case Opt_fragment_data:
797d0bd4560SJosef Bacik 			btrfs_info(root->fs_info, "fragmenting data");
798d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
799d0bd4560SJosef Bacik 			break;
800d0bd4560SJosef Bacik #endif
801a7a3f7caSSage Weil 		case Opt_err:
802efe120a0SFrank Holton 			btrfs_info(root->fs_info, "unrecognized mount option '%s'", p);
803a7a3f7caSSage Weil 			ret = -EINVAL;
804a7a3f7caSSage Weil 			goto out;
80595e05289SChris Mason 		default:
806be20aa9dSChris Mason 			break;
80795e05289SChris Mason 		}
80895e05289SChris Mason 	}
80996da0919SQu Wenruo check:
81096da0919SQu Wenruo 	/*
81196da0919SQu Wenruo 	 * Extra check for current option against current flag
81296da0919SQu Wenruo 	 */
81396da0919SQu Wenruo 	if (btrfs_test_opt(root, NOLOGREPLAY) && !(new_flags & MS_RDONLY)) {
81496da0919SQu Wenruo 		btrfs_err(root->fs_info,
81596da0919SQu Wenruo 			  "nologreplay must be used with ro mount option");
81696da0919SQu Wenruo 		ret = -EINVAL;
81796da0919SQu Wenruo 	}
818a7a3f7caSSage Weil out:
81970f6d82eSOmar Sandoval 	if (btrfs_fs_compat_ro(root->fs_info, FREE_SPACE_TREE) &&
82070f6d82eSOmar Sandoval 	    !btrfs_test_opt(root, FREE_SPACE_TREE) &&
82170f6d82eSOmar Sandoval 	    !btrfs_test_opt(root, CLEAR_CACHE)) {
82270f6d82eSOmar Sandoval 		btrfs_err(root->fs_info, "cannot disable free space tree");
82370f6d82eSOmar Sandoval 		ret = -EINVAL;
82470f6d82eSOmar Sandoval 
82570f6d82eSOmar Sandoval 	}
82673bc1876SJosef Bacik 	if (!ret && btrfs_test_opt(root, SPACE_CACHE))
827efe120a0SFrank Holton 		btrfs_info(root->fs_info, "disk space caching is enabled");
82870f6d82eSOmar Sandoval 	if (!ret && btrfs_test_opt(root, FREE_SPACE_TREE))
82970f6d82eSOmar Sandoval 		btrfs_info(root->fs_info, "using free space tree");
830da495eccSJosef Bacik 	kfree(orig);
831a7a3f7caSSage Weil 	return ret;
832edf24abeSChristoph Hellwig }
833edf24abeSChristoph Hellwig 
834edf24abeSChristoph Hellwig /*
835edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
836edf24abeSChristoph Hellwig  *
837edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
838edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
839edf24abeSChristoph Hellwig  */
84097288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags,
84173f73415SJosef Bacik 		void *holder, char **subvol_name, u64 *subvol_objectid,
8425e2a4b25SDavid Sterba 		struct btrfs_fs_devices **fs_devices)
843edf24abeSChristoph Hellwig {
844edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
84583c8c9bdSJeff Liu 	char *device_name, *opts, *orig, *p;
8461493381fSWang Shilong 	char *num = NULL;
847edf24abeSChristoph Hellwig 	int error = 0;
848edf24abeSChristoph Hellwig 
849edf24abeSChristoph Hellwig 	if (!options)
850830c4adbSJosef Bacik 		return 0;
851edf24abeSChristoph Hellwig 
852edf24abeSChristoph Hellwig 	/*
853edf24abeSChristoph Hellwig 	 * strsep changes the string, duplicate it because parse_options
854edf24abeSChristoph Hellwig 	 * gets called twice
855edf24abeSChristoph Hellwig 	 */
856edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
857edf24abeSChristoph Hellwig 	if (!opts)
858edf24abeSChristoph Hellwig 		return -ENOMEM;
8593f3d0bc0STero Roponen 	orig = opts;
860edf24abeSChristoph Hellwig 
861edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
862edf24abeSChristoph Hellwig 		int token;
863edf24abeSChristoph Hellwig 		if (!*p)
864edf24abeSChristoph Hellwig 			continue;
865edf24abeSChristoph Hellwig 
866edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
867edf24abeSChristoph Hellwig 		switch (token) {
868edf24abeSChristoph Hellwig 		case Opt_subvol:
869a90e8b6fSIlya Dryomov 			kfree(*subvol_name);
870edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
8712c334e87SWang Shilong 			if (!*subvol_name) {
8722c334e87SWang Shilong 				error = -ENOMEM;
8732c334e87SWang Shilong 				goto out;
8742c334e87SWang Shilong 			}
875edf24abeSChristoph Hellwig 			break;
87673f73415SJosef Bacik 		case Opt_subvolid:
8771493381fSWang Shilong 			num = match_strdup(&args[0]);
8781493381fSWang Shilong 			if (num) {
8791493381fSWang Shilong 				*subvol_objectid = memparse(num, NULL);
8801493381fSWang Shilong 				kfree(num);
8814849f01dSJosef Bacik 				/* we want the original fs_tree */
8821493381fSWang Shilong 				if (!*subvol_objectid)
8834849f01dSJosef Bacik 					*subvol_objectid =
8844849f01dSJosef Bacik 						BTRFS_FS_TREE_OBJECTID;
8852c334e87SWang Shilong 			} else {
8862c334e87SWang Shilong 				error = -EINVAL;
8872c334e87SWang Shilong 				goto out;
8884849f01dSJosef Bacik 			}
88973f73415SJosef Bacik 			break;
890e15d0542SXin Zhong 		case Opt_subvolrootid:
8915e2a4b25SDavid Sterba 			printk(KERN_WARNING
892efe120a0SFrank Holton 				"BTRFS: 'subvolrootid' mount option is deprecated and has "
893efe120a0SFrank Holton 				"no effect\n");
894e15d0542SXin Zhong 			break;
89543e570b0SChristoph Hellwig 		case Opt_device:
89683c8c9bdSJeff Liu 			device_name = match_strdup(&args[0]);
89783c8c9bdSJeff Liu 			if (!device_name) {
89883c8c9bdSJeff Liu 				error = -ENOMEM;
89983c8c9bdSJeff Liu 				goto out;
90083c8c9bdSJeff Liu 			}
90183c8c9bdSJeff Liu 			error = btrfs_scan_one_device(device_name,
90243e570b0SChristoph Hellwig 					flags, holder, fs_devices);
90383c8c9bdSJeff Liu 			kfree(device_name);
90443e570b0SChristoph Hellwig 			if (error)
905830c4adbSJosef Bacik 				goto out;
90643e570b0SChristoph Hellwig 			break;
907edf24abeSChristoph Hellwig 		default:
908edf24abeSChristoph Hellwig 			break;
909edf24abeSChristoph Hellwig 		}
910edf24abeSChristoph Hellwig 	}
911edf24abeSChristoph Hellwig 
912edf24abeSChristoph Hellwig out:
913830c4adbSJosef Bacik 	kfree(orig);
914edf24abeSChristoph Hellwig 	return error;
91595e05289SChris Mason }
91695e05289SChris Mason 
91705dbe683SOmar Sandoval static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
91873f73415SJosef Bacik 					   u64 subvol_objectid)
91973f73415SJosef Bacik {
920815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
92105dbe683SOmar Sandoval 	struct btrfs_root *fs_root;
92205dbe683SOmar Sandoval 	struct btrfs_root_ref *root_ref;
92305dbe683SOmar Sandoval 	struct btrfs_inode_ref *inode_ref;
92405dbe683SOmar Sandoval 	struct btrfs_key key;
92505dbe683SOmar Sandoval 	struct btrfs_path *path = NULL;
92605dbe683SOmar Sandoval 	char *name = NULL, *ptr;
92705dbe683SOmar Sandoval 	u64 dirid;
92805dbe683SOmar Sandoval 	int len;
92905dbe683SOmar Sandoval 	int ret;
93005dbe683SOmar Sandoval 
93105dbe683SOmar Sandoval 	path = btrfs_alloc_path();
93205dbe683SOmar Sandoval 	if (!path) {
93305dbe683SOmar Sandoval 		ret = -ENOMEM;
93405dbe683SOmar Sandoval 		goto err;
93505dbe683SOmar Sandoval 	}
93605dbe683SOmar Sandoval 	path->leave_spinning = 1;
93705dbe683SOmar Sandoval 
93805dbe683SOmar Sandoval 	name = kmalloc(PATH_MAX, GFP_NOFS);
93905dbe683SOmar Sandoval 	if (!name) {
94005dbe683SOmar Sandoval 		ret = -ENOMEM;
94105dbe683SOmar Sandoval 		goto err;
94205dbe683SOmar Sandoval 	}
94305dbe683SOmar Sandoval 	ptr = name + PATH_MAX - 1;
94405dbe683SOmar Sandoval 	ptr[0] = '\0';
94505dbe683SOmar Sandoval 
94605dbe683SOmar Sandoval 	/*
94705dbe683SOmar Sandoval 	 * Walk up the subvolume trees in the tree of tree roots by root
94805dbe683SOmar Sandoval 	 * backrefs until we hit the top-level subvolume.
94905dbe683SOmar Sandoval 	 */
95005dbe683SOmar Sandoval 	while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
95105dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
95205dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_BACKREF_KEY;
95305dbe683SOmar Sandoval 		key.offset = (u64)-1;
95405dbe683SOmar Sandoval 
95505dbe683SOmar Sandoval 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
95605dbe683SOmar Sandoval 		if (ret < 0) {
95705dbe683SOmar Sandoval 			goto err;
95805dbe683SOmar Sandoval 		} else if (ret > 0) {
95905dbe683SOmar Sandoval 			ret = btrfs_previous_item(root, path, subvol_objectid,
96005dbe683SOmar Sandoval 						  BTRFS_ROOT_BACKREF_KEY);
96105dbe683SOmar Sandoval 			if (ret < 0) {
96205dbe683SOmar Sandoval 				goto err;
96305dbe683SOmar Sandoval 			} else if (ret > 0) {
96405dbe683SOmar Sandoval 				ret = -ENOENT;
96505dbe683SOmar Sandoval 				goto err;
96605dbe683SOmar Sandoval 			}
96705dbe683SOmar Sandoval 		}
96805dbe683SOmar Sandoval 
96905dbe683SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
97005dbe683SOmar Sandoval 		subvol_objectid = key.offset;
97105dbe683SOmar Sandoval 
97205dbe683SOmar Sandoval 		root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
97305dbe683SOmar Sandoval 					  struct btrfs_root_ref);
97405dbe683SOmar Sandoval 		len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
97505dbe683SOmar Sandoval 		ptr -= len + 1;
97605dbe683SOmar Sandoval 		if (ptr < name) {
97705dbe683SOmar Sandoval 			ret = -ENAMETOOLONG;
97805dbe683SOmar Sandoval 			goto err;
97905dbe683SOmar Sandoval 		}
98005dbe683SOmar Sandoval 		read_extent_buffer(path->nodes[0], ptr + 1,
98105dbe683SOmar Sandoval 				   (unsigned long)(root_ref + 1), len);
98205dbe683SOmar Sandoval 		ptr[0] = '/';
98305dbe683SOmar Sandoval 		dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
98405dbe683SOmar Sandoval 		btrfs_release_path(path);
98505dbe683SOmar Sandoval 
98605dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
98705dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_ITEM_KEY;
98805dbe683SOmar Sandoval 		key.offset = (u64)-1;
98905dbe683SOmar Sandoval 		fs_root = btrfs_read_fs_root_no_name(fs_info, &key);
99005dbe683SOmar Sandoval 		if (IS_ERR(fs_root)) {
99105dbe683SOmar Sandoval 			ret = PTR_ERR(fs_root);
99205dbe683SOmar Sandoval 			goto err;
99305dbe683SOmar Sandoval 		}
99405dbe683SOmar Sandoval 
99505dbe683SOmar Sandoval 		/*
99605dbe683SOmar Sandoval 		 * Walk up the filesystem tree by inode refs until we hit the
99705dbe683SOmar Sandoval 		 * root directory.
99805dbe683SOmar Sandoval 		 */
99905dbe683SOmar Sandoval 		while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
100005dbe683SOmar Sandoval 			key.objectid = dirid;
100105dbe683SOmar Sandoval 			key.type = BTRFS_INODE_REF_KEY;
100205dbe683SOmar Sandoval 			key.offset = (u64)-1;
100305dbe683SOmar Sandoval 
100405dbe683SOmar Sandoval 			ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
100505dbe683SOmar Sandoval 			if (ret < 0) {
100605dbe683SOmar Sandoval 				goto err;
100705dbe683SOmar Sandoval 			} else if (ret > 0) {
100805dbe683SOmar Sandoval 				ret = btrfs_previous_item(fs_root, path, dirid,
100905dbe683SOmar Sandoval 							  BTRFS_INODE_REF_KEY);
101005dbe683SOmar Sandoval 				if (ret < 0) {
101105dbe683SOmar Sandoval 					goto err;
101205dbe683SOmar Sandoval 				} else if (ret > 0) {
101305dbe683SOmar Sandoval 					ret = -ENOENT;
101405dbe683SOmar Sandoval 					goto err;
101505dbe683SOmar Sandoval 				}
101605dbe683SOmar Sandoval 			}
101705dbe683SOmar Sandoval 
101805dbe683SOmar Sandoval 			btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
101905dbe683SOmar Sandoval 			dirid = key.offset;
102005dbe683SOmar Sandoval 
102105dbe683SOmar Sandoval 			inode_ref = btrfs_item_ptr(path->nodes[0],
102205dbe683SOmar Sandoval 						   path->slots[0],
102305dbe683SOmar Sandoval 						   struct btrfs_inode_ref);
102405dbe683SOmar Sandoval 			len = btrfs_inode_ref_name_len(path->nodes[0],
102505dbe683SOmar Sandoval 						       inode_ref);
102605dbe683SOmar Sandoval 			ptr -= len + 1;
102705dbe683SOmar Sandoval 			if (ptr < name) {
102805dbe683SOmar Sandoval 				ret = -ENAMETOOLONG;
102905dbe683SOmar Sandoval 				goto err;
103005dbe683SOmar Sandoval 			}
103105dbe683SOmar Sandoval 			read_extent_buffer(path->nodes[0], ptr + 1,
103205dbe683SOmar Sandoval 					   (unsigned long)(inode_ref + 1), len);
103305dbe683SOmar Sandoval 			ptr[0] = '/';
103405dbe683SOmar Sandoval 			btrfs_release_path(path);
103505dbe683SOmar Sandoval 		}
103605dbe683SOmar Sandoval 	}
103705dbe683SOmar Sandoval 
103805dbe683SOmar Sandoval 	btrfs_free_path(path);
103905dbe683SOmar Sandoval 	if (ptr == name + PATH_MAX - 1) {
104005dbe683SOmar Sandoval 		name[0] = '/';
104105dbe683SOmar Sandoval 		name[1] = '\0';
104205dbe683SOmar Sandoval 	} else {
104305dbe683SOmar Sandoval 		memmove(name, ptr, name + PATH_MAX - ptr);
104405dbe683SOmar Sandoval 	}
104505dbe683SOmar Sandoval 	return name;
104605dbe683SOmar Sandoval 
104705dbe683SOmar Sandoval err:
104805dbe683SOmar Sandoval 	btrfs_free_path(path);
104905dbe683SOmar Sandoval 	kfree(name);
105005dbe683SOmar Sandoval 	return ERR_PTR(ret);
105105dbe683SOmar Sandoval }
105205dbe683SOmar Sandoval 
105305dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
105405dbe683SOmar Sandoval {
105505dbe683SOmar Sandoval 	struct btrfs_root *root = fs_info->tree_root;
105673f73415SJosef Bacik 	struct btrfs_dir_item *di;
105773f73415SJosef Bacik 	struct btrfs_path *path;
105873f73415SJosef Bacik 	struct btrfs_key location;
105973f73415SJosef Bacik 	u64 dir_id;
106073f73415SJosef Bacik 
106173f73415SJosef Bacik 	path = btrfs_alloc_path();
106273f73415SJosef Bacik 	if (!path)
106305dbe683SOmar Sandoval 		return -ENOMEM;
106473f73415SJosef Bacik 	path->leave_spinning = 1;
106573f73415SJosef Bacik 
106673f73415SJosef Bacik 	/*
106773f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
106873f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
106973f73415SJosef Bacik 	 * to mount.
107073f73415SJosef Bacik 	 */
1071815745cfSAl Viro 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
107273f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
1073b0839166SJulia Lawall 	if (IS_ERR(di)) {
1074b0839166SJulia Lawall 		btrfs_free_path(path);
107505dbe683SOmar Sandoval 		return PTR_ERR(di);
1076b0839166SJulia Lawall 	}
107773f73415SJosef Bacik 	if (!di) {
107873f73415SJosef Bacik 		/*
107973f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
108073f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
108105dbe683SOmar Sandoval 		 * mount the top-level subvolume.
108273f73415SJosef Bacik 		 */
108373f73415SJosef Bacik 		btrfs_free_path(path);
108405dbe683SOmar Sandoval 		*objectid = BTRFS_FS_TREE_OBJECTID;
108505dbe683SOmar Sandoval 		return 0;
108673f73415SJosef Bacik 	}
108773f73415SJosef Bacik 
108873f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
108973f73415SJosef Bacik 	btrfs_free_path(path);
109005dbe683SOmar Sandoval 	*objectid = location.objectid;
109105dbe683SOmar Sandoval 	return 0;
109273f73415SJosef Bacik }
109373f73415SJosef Bacik 
10948a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
10958a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
10968a4b83ccSChris Mason 			    void *data, int silent)
10972e635a27SChris Mason {
10982e635a27SChris Mason 	struct inode *inode;
1099815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
11005d4f98a2SYan Zheng 	struct btrfs_key key;
110139279cc3SChris Mason 	int err;
11022e635a27SChris Mason 
11032e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
11042e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
1105e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
1106af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
1107be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
11085103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
11092e635a27SChris Mason 	sb->s_time_gran = 1;
11100eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
111133268eafSJosef Bacik 	sb->s_flags |= MS_POSIXACL;
111249cf6f45SChris Ball #endif
11130c4d2d95SJosef Bacik 	sb->s_flags |= MS_I_VERSION;
1114da2f0f74SChris Mason 	sb->s_iflags |= SB_I_CGROUPWB;
1115ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
1116ad2b2c80SAl Viro 	if (err) {
1117efe120a0SFrank Holton 		printk(KERN_ERR "BTRFS: open_ctree failed\n");
1118ad2b2c80SAl Viro 		return err;
1119e20d96d6SChris Mason 	}
1120b888db2bSChris Mason 
11215d4f98a2SYan Zheng 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
11225d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
11235d4f98a2SYan Zheng 	key.offset = 0;
112498c7089cSAl Viro 	inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
11255d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
11265d4f98a2SYan Zheng 		err = PTR_ERR(inode);
112739279cc3SChris Mason 		goto fail_close;
112839279cc3SChris Mason 	}
11292e635a27SChris Mason 
113048fde701SAl Viro 	sb->s_root = d_make_root(inode);
113148fde701SAl Viro 	if (!sb->s_root) {
113239279cc3SChris Mason 		err = -ENOMEM;
113339279cc3SChris Mason 		goto fail_close;
11342e635a27SChris Mason 	}
113558176a96SJosef Bacik 
11366885f308SChris Mason 	save_mount_options(sb, data);
113790a887c9SDan Magenheimer 	cleancache_init_fs(sb);
113859553edfSAl Viro 	sb->s_flags |= MS_ACTIVE;
11392e635a27SChris Mason 	return 0;
11402e635a27SChris Mason 
114139279cc3SChris Mason fail_close:
1142815745cfSAl Viro 	close_ctree(fs_info->tree_root);
1143d5719762SChris Mason 	return err;
1144d5719762SChris Mason }
1145d5719762SChris Mason 
11466bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
1147d5719762SChris Mason {
1148d5719762SChris Mason 	struct btrfs_trans_handle *trans;
1149815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1150815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
1151df2ce34cSChris Mason 
11521abe9b8aSliubo 	trace_btrfs_sync_fs(wait);
11531abe9b8aSliubo 
1154d561c025SChris Mason 	if (!wait) {
1155815745cfSAl Viro 		filemap_flush(fs_info->btree_inode->i_mapping);
1156df2ce34cSChris Mason 		return 0;
1157d561c025SChris Mason 	}
1158771ed689SChris Mason 
1159578def7cSFilipe Manana 	btrfs_wait_ordered_roots(fs_info, -1, 0, (u64)-1);
1160771ed689SChris Mason 
1161d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
116260376ce4SJosef Bacik 	if (IS_ERR(trans)) {
1163354aa0fbSMiao Xie 		/* no transaction, don't bother */
11646b5fe46dSDavid Sterba 		if (PTR_ERR(trans) == -ENOENT) {
11656b5fe46dSDavid Sterba 			/*
11666b5fe46dSDavid Sterba 			 * Exit unless we have some pending changes
11676b5fe46dSDavid Sterba 			 * that need to go through commit
11686b5fe46dSDavid Sterba 			 */
11696b5fe46dSDavid Sterba 			if (fs_info->pending_changes == 0)
1170bd7de2c9SJosef Bacik 				return 0;
1171a53f4f8eSQu Wenruo 			/*
1172a53f4f8eSQu Wenruo 			 * A non-blocking test if the fs is frozen. We must not
1173a53f4f8eSQu Wenruo 			 * start a new transaction here otherwise a deadlock
1174a53f4f8eSQu Wenruo 			 * happens. The pending operations are delayed to the
1175a53f4f8eSQu Wenruo 			 * next commit after thawing.
1176a53f4f8eSQu Wenruo 			 */
1177a53f4f8eSQu Wenruo 			if (__sb_start_write(sb, SB_FREEZE_WRITE, false))
1178a53f4f8eSQu Wenruo 				__sb_end_write(sb, SB_FREEZE_WRITE);
1179a53f4f8eSQu Wenruo 			else
1180a53f4f8eSQu Wenruo 				return 0;
11816b5fe46dSDavid Sterba 			trans = btrfs_start_transaction(root, 0);
118260376ce4SJosef Bacik 		}
118398bd5c54SDavid Sterba 		if (IS_ERR(trans))
118498bd5c54SDavid Sterba 			return PTR_ERR(trans);
11856b5fe46dSDavid Sterba 	}
1186bd7de2c9SJosef Bacik 	return btrfs_commit_transaction(trans, root);
1187d5719762SChris Mason }
1188d5719762SChris Mason 
118934c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1190a9572a15SEric Paris {
1191815745cfSAl Viro 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
1192815745cfSAl Viro 	struct btrfs_root *root = info->tree_root;
1193200da64eSTsutomu Itoh 	char *compress_type;
1194a9572a15SEric Paris 
1195a9572a15SEric Paris 	if (btrfs_test_opt(root, DEGRADED))
1196a9572a15SEric Paris 		seq_puts(seq, ",degraded");
1197a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATASUM))
1198a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
1199a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATACOW))
1200a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
1201a9572a15SEric Paris 	if (btrfs_test_opt(root, NOBARRIER))
1202a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
120395ac567aSFilipe David Borba Manana 	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1204c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",max_inline=%llu", info->max_inline);
1205a9572a15SEric Paris 	if (info->alloc_start != 0)
1206c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",alloc_start=%llu", info->alloc_start);
1207a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
1208a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
1209a9572a15SEric Paris 		seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
1210200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, COMPRESS)) {
1211200da64eSTsutomu Itoh 		if (info->compress_type == BTRFS_COMPRESS_ZLIB)
1212200da64eSTsutomu Itoh 			compress_type = "zlib";
1213200da64eSTsutomu Itoh 		else
1214200da64eSTsutomu Itoh 			compress_type = "lzo";
1215200da64eSTsutomu Itoh 		if (btrfs_test_opt(root, FORCE_COMPRESS))
1216200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
1217200da64eSTsutomu Itoh 		else
1218200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
1219200da64eSTsutomu Itoh 	}
1220c289811cSChris Mason 	if (btrfs_test_opt(root, NOSSD))
1221c289811cSChris Mason 		seq_puts(seq, ",nossd");
1222451d7585SChris Mason 	if (btrfs_test_opt(root, SSD_SPREAD))
1223451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
1224451d7585SChris Mason 	else if (btrfs_test_opt(root, SSD))
1225a9572a15SEric Paris 		seq_puts(seq, ",ssd");
12263a5e1404SSage Weil 	if (btrfs_test_opt(root, NOTREELOG))
12276b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
122896da0919SQu Wenruo 	if (btrfs_test_opt(root, NOLOGREPLAY))
122996da0919SQu Wenruo 		seq_puts(seq, ",nologreplay");
1230dccae999SSage Weil 	if (btrfs_test_opt(root, FLUSHONCOMMIT))
12316b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
123220a5239aSMatthew Wilcox 	if (btrfs_test_opt(root, DISCARD))
123320a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
1234a9572a15SEric Paris 	if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
1235a9572a15SEric Paris 		seq_puts(seq, ",noacl");
1236200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, SPACE_CACHE))
1237200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
123870f6d82eSOmar Sandoval 	else if (btrfs_test_opt(root, FREE_SPACE_TREE))
123970f6d82eSOmar Sandoval 		seq_puts(seq, ",space_cache=v2");
124073bc1876SJosef Bacik 	else
12418965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
1242f420ee1eSStefan Behrens 	if (btrfs_test_opt(root, RESCAN_UUID_TREE))
1243f420ee1eSStefan Behrens 		seq_puts(seq, ",rescan_uuid_tree");
1244200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, CLEAR_CACHE))
1245200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
1246200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1247200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
12480942caa3SDavid Sterba 	if (btrfs_test_opt(root, ENOSPC_DEBUG))
12490942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
12500942caa3SDavid Sterba 	if (btrfs_test_opt(root, AUTO_DEFRAG))
12510942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
12520942caa3SDavid Sterba 	if (btrfs_test_opt(root, INODE_MAP_CACHE))
12530942caa3SDavid Sterba 		seq_puts(seq, ",inode_cache");
12549555c6c1SIlya Dryomov 	if (btrfs_test_opt(root, SKIP_BALANCE))
12559555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
12568507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
12578507d216SWang Shilong 	if (btrfs_test_opt(root, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
12588507d216SWang Shilong 		seq_puts(seq, ",check_int_data");
12598507d216SWang Shilong 	else if (btrfs_test_opt(root, CHECK_INTEGRITY))
12608507d216SWang Shilong 		seq_puts(seq, ",check_int");
12618507d216SWang Shilong 	if (info->check_integrity_print_mask)
12628507d216SWang Shilong 		seq_printf(seq, ",check_int_print_mask=%d",
12638507d216SWang Shilong 				info->check_integrity_print_mask);
12648507d216SWang Shilong #endif
12658507d216SWang Shilong 	if (info->metadata_ratio)
12668507d216SWang Shilong 		seq_printf(seq, ",metadata_ratio=%d",
12678507d216SWang Shilong 				info->metadata_ratio);
12688c342930SJeff Mahoney 	if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR))
12698c342930SJeff Mahoney 		seq_puts(seq, ",fatal_errors=panic");
12708b87dc17SDavid Sterba 	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
12718b87dc17SDavid Sterba 		seq_printf(seq, ",commit=%d", info->commit_interval);
1272d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
1273d0bd4560SJosef Bacik 	if (btrfs_test_opt(root, FRAGMENT_DATA))
1274d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=data");
1275d0bd4560SJosef Bacik 	if (btrfs_test_opt(root, FRAGMENT_METADATA))
1276d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=metadata");
1277d0bd4560SJosef Bacik #endif
1278c8d3fe02SOmar Sandoval 	seq_printf(seq, ",subvolid=%llu",
1279c8d3fe02SOmar Sandoval 		  BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1280c8d3fe02SOmar Sandoval 	seq_puts(seq, ",subvol=");
1281c8d3fe02SOmar Sandoval 	seq_dentry(seq, dentry, " \t\n\\");
1282a9572a15SEric Paris 	return 0;
1283a9572a15SEric Paris }
1284a9572a15SEric Paris 
1285a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
12862e635a27SChris Mason {
1287815745cfSAl Viro 	struct btrfs_fs_info *p = data;
1288815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(s);
12894b82d6e4SYan 
1290815745cfSAl Viro 	return fs_info->fs_devices == p->fs_devices;
12914b82d6e4SYan }
12924b82d6e4SYan 
1293450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
1294450ba0eaSJosef Bacik {
12956de1d09dSAl Viro 	int err = set_anon_super(s, data);
12966de1d09dSAl Viro 	if (!err)
1297450ba0eaSJosef Bacik 		s->s_fs_info = data;
12986de1d09dSAl Viro 	return err;
1299450ba0eaSJosef Bacik }
1300450ba0eaSJosef Bacik 
1301830c4adbSJosef Bacik /*
1302f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
1303f9d9ef62SDavid Sterba  */
1304f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
1305f9d9ef62SDavid Sterba {
1306f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1307f9d9ef62SDavid Sterba 		return 1;
1308f9d9ef62SDavid Sterba 	return 0;
1309f9d9ef62SDavid Sterba }
1310f9d9ef62SDavid Sterba 
1311f9d9ef62SDavid Sterba /*
1312e6e4dbe8SOmar Sandoval  * This will add subvolid=0 to the argument string while removing any subvol=
1313e6e4dbe8SOmar Sandoval  * and subvolid= arguments to make sure we get the top-level root for path
1314e6e4dbe8SOmar Sandoval  * walking to the subvol we want.
1315830c4adbSJosef Bacik  */
1316830c4adbSJosef Bacik static char *setup_root_args(char *args)
1317830c4adbSJosef Bacik {
1318e6e4dbe8SOmar Sandoval 	char *buf, *dst, *sep;
1319830c4adbSJosef Bacik 
1320e6e4dbe8SOmar Sandoval 	if (!args)
1321e6e4dbe8SOmar Sandoval 		return kstrdup("subvolid=0", GFP_NOFS);
1322830c4adbSJosef Bacik 
1323e6e4dbe8SOmar Sandoval 	/* The worst case is that we add ",subvolid=0" to the end. */
1324e6e4dbe8SOmar Sandoval 	buf = dst = kmalloc(strlen(args) + strlen(",subvolid=0") + 1, GFP_NOFS);
1325f60d16a8SJim Meyering 	if (!buf)
1326f60d16a8SJim Meyering 		return NULL;
1327830c4adbSJosef Bacik 
1328e6e4dbe8SOmar Sandoval 	while (1) {
1329e6e4dbe8SOmar Sandoval 		sep = strchrnul(args, ',');
1330e6e4dbe8SOmar Sandoval 		if (!strstarts(args, "subvol=") &&
1331e6e4dbe8SOmar Sandoval 		    !strstarts(args, "subvolid=")) {
1332e6e4dbe8SOmar Sandoval 			memcpy(dst, args, sep - args);
1333e6e4dbe8SOmar Sandoval 			dst += sep - args;
1334e6e4dbe8SOmar Sandoval 			*dst++ = ',';
1335830c4adbSJosef Bacik 		}
1336e6e4dbe8SOmar Sandoval 		if (*sep)
1337e6e4dbe8SOmar Sandoval 			args = sep + 1;
1338e6e4dbe8SOmar Sandoval 		else
1339e6e4dbe8SOmar Sandoval 			break;
1340e6e4dbe8SOmar Sandoval 	}
1341f60d16a8SJim Meyering 	strcpy(dst, "subvolid=0");
1342830c4adbSJosef Bacik 
1343f60d16a8SJim Meyering 	return buf;
1344830c4adbSJosef Bacik }
1345830c4adbSJosef Bacik 
1346bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1347bb289b7bSOmar Sandoval 				   int flags, const char *device_name,
1348bb289b7bSOmar Sandoval 				   char *data)
1349830c4adbSJosef Bacik {
1350830c4adbSJosef Bacik 	struct dentry *root;
1351fa330659SOmar Sandoval 	struct vfsmount *mnt = NULL;
1352830c4adbSJosef Bacik 	char *newargs;
1353fa330659SOmar Sandoval 	int ret;
1354830c4adbSJosef Bacik 
1355830c4adbSJosef Bacik 	newargs = setup_root_args(data);
1356fa330659SOmar Sandoval 	if (!newargs) {
1357fa330659SOmar Sandoval 		root = ERR_PTR(-ENOMEM);
1358fa330659SOmar Sandoval 		goto out;
1359fa330659SOmar Sandoval 	}
13600723a047SHarald Hoyer 
1361fa330659SOmar Sandoval 	mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, newargs);
1362fa330659SOmar Sandoval 	if (PTR_ERR_OR_ZERO(mnt) == -EBUSY) {
13630723a047SHarald Hoyer 		if (flags & MS_RDONLY) {
1364fa330659SOmar Sandoval 			mnt = vfs_kern_mount(&btrfs_fs_type, flags & ~MS_RDONLY,
1365fa330659SOmar Sandoval 					     device_name, newargs);
13660723a047SHarald Hoyer 		} else {
1367fa330659SOmar Sandoval 			mnt = vfs_kern_mount(&btrfs_fs_type, flags | MS_RDONLY,
1368fa330659SOmar Sandoval 					     device_name, newargs);
13690040e606SChristoph Jaeger 			if (IS_ERR(mnt)) {
1370fa330659SOmar Sandoval 				root = ERR_CAST(mnt);
1371fa330659SOmar Sandoval 				mnt = NULL;
1372fa330659SOmar Sandoval 				goto out;
13730040e606SChristoph Jaeger 			}
13740723a047SHarald Hoyer 
1375773cd04eSOmar Sandoval 			down_write(&mnt->mnt_sb->s_umount);
1376fa330659SOmar Sandoval 			ret = btrfs_remount(mnt->mnt_sb, &flags, NULL);
1377773cd04eSOmar Sandoval 			up_write(&mnt->mnt_sb->s_umount);
1378fa330659SOmar Sandoval 			if (ret < 0) {
1379fa330659SOmar Sandoval 				root = ERR_PTR(ret);
1380fa330659SOmar Sandoval 				goto out;
13810723a047SHarald Hoyer 			}
13820723a047SHarald Hoyer 		}
13830723a047SHarald Hoyer 	}
1384fa330659SOmar Sandoval 	if (IS_ERR(mnt)) {
1385fa330659SOmar Sandoval 		root = ERR_CAST(mnt);
1386fa330659SOmar Sandoval 		mnt = NULL;
1387fa330659SOmar Sandoval 		goto out;
1388fa330659SOmar Sandoval 	}
1389830c4adbSJosef Bacik 
139005dbe683SOmar Sandoval 	if (!subvol_name) {
139105dbe683SOmar Sandoval 		if (!subvol_objectid) {
139205dbe683SOmar Sandoval 			ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
139305dbe683SOmar Sandoval 							  &subvol_objectid);
139405dbe683SOmar Sandoval 			if (ret) {
139505dbe683SOmar Sandoval 				root = ERR_PTR(ret);
139605dbe683SOmar Sandoval 				goto out;
139705dbe683SOmar Sandoval 			}
139805dbe683SOmar Sandoval 		}
139905dbe683SOmar Sandoval 		subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb),
140005dbe683SOmar Sandoval 							    subvol_objectid);
140105dbe683SOmar Sandoval 		if (IS_ERR(subvol_name)) {
140205dbe683SOmar Sandoval 			root = ERR_CAST(subvol_name);
140305dbe683SOmar Sandoval 			subvol_name = NULL;
140405dbe683SOmar Sandoval 			goto out;
140505dbe683SOmar Sandoval 		}
140605dbe683SOmar Sandoval 
140705dbe683SOmar Sandoval 	}
140805dbe683SOmar Sandoval 
1409ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1410fa330659SOmar Sandoval 	/* mount_subtree() drops our reference on the vfsmount. */
1411fa330659SOmar Sandoval 	mnt = NULL;
1412830c4adbSJosef Bacik 
1413bb289b7bSOmar Sandoval 	if (!IS_ERR(root)) {
1414ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1415bb289b7bSOmar Sandoval 		struct inode *root_inode = d_inode(root);
1416bb289b7bSOmar Sandoval 		u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1417bb289b7bSOmar Sandoval 
1418bb289b7bSOmar Sandoval 		ret = 0;
1419bb289b7bSOmar Sandoval 		if (!is_subvolume_inode(root_inode)) {
1420bb289b7bSOmar Sandoval 			pr_err("BTRFS: '%s' is not a valid subvolume\n",
1421bb289b7bSOmar Sandoval 			       subvol_name);
1422bb289b7bSOmar Sandoval 			ret = -EINVAL;
1423bb289b7bSOmar Sandoval 		}
1424bb289b7bSOmar Sandoval 		if (subvol_objectid && root_objectid != subvol_objectid) {
142505dbe683SOmar Sandoval 			/*
142605dbe683SOmar Sandoval 			 * This will also catch a race condition where a
142705dbe683SOmar Sandoval 			 * subvolume which was passed by ID is renamed and
142805dbe683SOmar Sandoval 			 * another subvolume is renamed over the old location.
142905dbe683SOmar Sandoval 			 */
1430bb289b7bSOmar Sandoval 			pr_err("BTRFS: subvol '%s' does not match subvolid %llu\n",
1431bb289b7bSOmar Sandoval 			       subvol_name, subvol_objectid);
1432bb289b7bSOmar Sandoval 			ret = -EINVAL;
1433bb289b7bSOmar Sandoval 		}
1434bb289b7bSOmar Sandoval 		if (ret) {
1435ea441d11SAl Viro 			dput(root);
1436bb289b7bSOmar Sandoval 			root = ERR_PTR(ret);
1437ea441d11SAl Viro 			deactivate_locked_super(s);
1438bb289b7bSOmar Sandoval 		}
1439f9d9ef62SDavid Sterba 	}
1440f9d9ef62SDavid Sterba 
1441fa330659SOmar Sandoval out:
1442fa330659SOmar Sandoval 	mntput(mnt);
1443fa330659SOmar Sandoval 	kfree(newargs);
1444fa330659SOmar Sandoval 	kfree(subvol_name);
1445830c4adbSJosef Bacik 	return root;
1446830c4adbSJosef Bacik }
1447450ba0eaSJosef Bacik 
1448f667aef6SQu Wenruo static int parse_security_options(char *orig_opts,
1449f667aef6SQu Wenruo 				  struct security_mnt_opts *sec_opts)
1450f667aef6SQu Wenruo {
1451f667aef6SQu Wenruo 	char *secdata = NULL;
1452f667aef6SQu Wenruo 	int ret = 0;
1453f667aef6SQu Wenruo 
1454f667aef6SQu Wenruo 	secdata = alloc_secdata();
1455f667aef6SQu Wenruo 	if (!secdata)
1456f667aef6SQu Wenruo 		return -ENOMEM;
1457f667aef6SQu Wenruo 	ret = security_sb_copy_data(orig_opts, secdata);
1458f667aef6SQu Wenruo 	if (ret) {
1459f667aef6SQu Wenruo 		free_secdata(secdata);
1460f667aef6SQu Wenruo 		return ret;
1461f667aef6SQu Wenruo 	}
1462f667aef6SQu Wenruo 	ret = security_sb_parse_opts_str(secdata, sec_opts);
1463f667aef6SQu Wenruo 	free_secdata(secdata);
1464f667aef6SQu Wenruo 	return ret;
1465f667aef6SQu Wenruo }
1466f667aef6SQu Wenruo 
1467f667aef6SQu Wenruo static int setup_security_options(struct btrfs_fs_info *fs_info,
1468f667aef6SQu Wenruo 				  struct super_block *sb,
1469f667aef6SQu Wenruo 				  struct security_mnt_opts *sec_opts)
1470f667aef6SQu Wenruo {
1471f667aef6SQu Wenruo 	int ret = 0;
1472f667aef6SQu Wenruo 
1473f667aef6SQu Wenruo 	/*
1474f667aef6SQu Wenruo 	 * Call security_sb_set_mnt_opts() to check whether new sec_opts
1475f667aef6SQu Wenruo 	 * is valid.
1476f667aef6SQu Wenruo 	 */
1477f667aef6SQu Wenruo 	ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL);
1478f667aef6SQu Wenruo 	if (ret)
1479f667aef6SQu Wenruo 		return ret;
1480f667aef6SQu Wenruo 
1481a43bb39bSQu Wenruo #ifdef CONFIG_SECURITY
1482f667aef6SQu Wenruo 	if (!fs_info->security_opts.num_mnt_opts) {
1483f667aef6SQu Wenruo 		/* first time security setup, copy sec_opts to fs_info */
1484f667aef6SQu Wenruo 		memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts));
1485f667aef6SQu Wenruo 	} else {
1486f667aef6SQu Wenruo 		/*
1487180e4d47SLuis de Bethencourt 		 * Since SELinux (the only one supporting security_mnt_opts)
1488180e4d47SLuis de Bethencourt 		 * does NOT support changing context during remount/mount of
1489180e4d47SLuis de Bethencourt 		 * the same sb, this must be the same or part of the same
1490180e4d47SLuis de Bethencourt 		 * security options, just free it.
1491f667aef6SQu Wenruo 		 */
1492f667aef6SQu Wenruo 		security_free_mnt_opts(sec_opts);
1493f667aef6SQu Wenruo 	}
1494a43bb39bSQu Wenruo #endif
1495f667aef6SQu Wenruo 	return ret;
1496f667aef6SQu Wenruo }
1497f667aef6SQu Wenruo 
1498edf24abeSChristoph Hellwig /*
1499edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
1500edf24abeSChristoph Hellwig  *
1501edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
1502edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
1503edf24abeSChristoph Hellwig  */
1504061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1505306e16ceSDavid Sterba 		const char *device_name, void *data)
15064b82d6e4SYan {
15074b82d6e4SYan 	struct block_device *bdev = NULL;
15084b82d6e4SYan 	struct super_block *s;
15098a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
1510450ba0eaSJosef Bacik 	struct btrfs_fs_info *fs_info = NULL;
1511f667aef6SQu Wenruo 	struct security_mnt_opts new_sec_opts;
151297288f2cSChristoph Hellwig 	fmode_t mode = FMODE_READ;
151373f73415SJosef Bacik 	char *subvol_name = NULL;
151473f73415SJosef Bacik 	u64 subvol_objectid = 0;
15154b82d6e4SYan 	int error = 0;
15164b82d6e4SYan 
151797288f2cSChristoph Hellwig 	if (!(flags & MS_RDONLY))
151897288f2cSChristoph Hellwig 		mode |= FMODE_WRITE;
151997288f2cSChristoph Hellwig 
152097288f2cSChristoph Hellwig 	error = btrfs_parse_early_options(data, mode, fs_type,
152173f73415SJosef Bacik 					  &subvol_name, &subvol_objectid,
15225e2a4b25SDavid Sterba 					  &fs_devices);
1523f23c8af8SIlya Dryomov 	if (error) {
1524f23c8af8SIlya Dryomov 		kfree(subvol_name);
1525061dbc6bSAl Viro 		return ERR_PTR(error);
1526f23c8af8SIlya Dryomov 	}
1527edf24abeSChristoph Hellwig 
152805dbe683SOmar Sandoval 	if (subvol_name || subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
1529fa330659SOmar Sandoval 		/* mount_subvol() will free subvol_name. */
1530bb289b7bSOmar Sandoval 		return mount_subvol(subvol_name, subvol_objectid, flags,
1531bb289b7bSOmar Sandoval 				    device_name, data);
1532830c4adbSJosef Bacik 	}
1533830c4adbSJosef Bacik 
1534f667aef6SQu Wenruo 	security_init_mnt_opts(&new_sec_opts);
1535f667aef6SQu Wenruo 	if (data) {
1536f667aef6SQu Wenruo 		error = parse_security_options(data, &new_sec_opts);
15378a4b83ccSChris Mason 		if (error)
1538830c4adbSJosef Bacik 			return ERR_PTR(error);
1539f667aef6SQu Wenruo 	}
1540f667aef6SQu Wenruo 
1541f667aef6SQu Wenruo 	error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
1542f667aef6SQu Wenruo 	if (error)
1543f667aef6SQu Wenruo 		goto error_sec_opts;
15444b82d6e4SYan 
1545450ba0eaSJosef Bacik 	/*
1546450ba0eaSJosef Bacik 	 * Setup a dummy root and fs_info for test/set super.  This is because
1547450ba0eaSJosef Bacik 	 * we don't actually fill this stuff out until open_ctree, but we need
1548450ba0eaSJosef Bacik 	 * it for searching for existing supers, so this lets us do that and
1549450ba0eaSJosef Bacik 	 * then open_ctree will properly initialize everything later.
1550450ba0eaSJosef Bacik 	 */
1551450ba0eaSJosef Bacik 	fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
1552f667aef6SQu Wenruo 	if (!fs_info) {
1553f667aef6SQu Wenruo 		error = -ENOMEM;
1554f667aef6SQu Wenruo 		goto error_sec_opts;
1555f667aef6SQu Wenruo 	}
155604d21a24SIlya Dryomov 
1557450ba0eaSJosef Bacik 	fs_info->fs_devices = fs_devices;
1558450ba0eaSJosef Bacik 
15596c41761fSDavid Sterba 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
15606c41761fSDavid Sterba 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
1561f667aef6SQu Wenruo 	security_init_mnt_opts(&fs_info->security_opts);
15626c41761fSDavid Sterba 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
15636c41761fSDavid Sterba 		error = -ENOMEM;
156404d21a24SIlya Dryomov 		goto error_fs_info;
156504d21a24SIlya Dryomov 	}
156604d21a24SIlya Dryomov 
156704d21a24SIlya Dryomov 	error = btrfs_open_devices(fs_devices, mode, fs_type);
156804d21a24SIlya Dryomov 	if (error)
156904d21a24SIlya Dryomov 		goto error_fs_info;
157004d21a24SIlya Dryomov 
157104d21a24SIlya Dryomov 	if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
157204d21a24SIlya Dryomov 		error = -EACCES;
15736c41761fSDavid Sterba 		goto error_close_devices;
15746c41761fSDavid Sterba 	}
15756c41761fSDavid Sterba 
1576dfe25020SChris Mason 	bdev = fs_devices->latest_bdev;
15779249e17fSDavid Howells 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC,
15789249e17fSDavid Howells 		 fs_info);
1579830c4adbSJosef Bacik 	if (IS_ERR(s)) {
1580830c4adbSJosef Bacik 		error = PTR_ERR(s);
1581830c4adbSJosef Bacik 		goto error_close_devices;
1582830c4adbSJosef Bacik 	}
15834b82d6e4SYan 
15844b82d6e4SYan 	if (s->s_root) {
15852b82032cSYan Zheng 		btrfs_close_devices(fs_devices);
15866c41761fSDavid Sterba 		free_fs_info(fs_info);
158759553edfSAl Viro 		if ((flags ^ s->s_flags) & MS_RDONLY)
158859553edfSAl Viro 			error = -EBUSY;
15894b82d6e4SYan 	} else {
1590a1c6f057SDmitry Monakhov 		snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
1591815745cfSAl Viro 		btrfs_sb(s)->bdev_holder = fs_type;
15928a4b83ccSChris Mason 		error = btrfs_fill_super(s, fs_devices, data,
15938a4b83ccSChris Mason 					 flags & MS_SILENT ? 1 : 0);
15944b82d6e4SYan 	}
159505dbe683SOmar Sandoval 	if (error) {
1596e15d0542SXin Zhong 		deactivate_locked_super(s);
1597f667aef6SQu Wenruo 		goto error_sec_opts;
1598f667aef6SQu Wenruo 	}
1599f667aef6SQu Wenruo 
1600f667aef6SQu Wenruo 	fs_info = btrfs_sb(s);
1601f667aef6SQu Wenruo 	error = setup_security_options(fs_info, s, &new_sec_opts);
1602f667aef6SQu Wenruo 	if (error) {
1603f667aef6SQu Wenruo 		deactivate_locked_super(s);
1604f667aef6SQu Wenruo 		goto error_sec_opts;
1605f667aef6SQu Wenruo 	}
16064b82d6e4SYan 
160705dbe683SOmar Sandoval 	return dget(s->s_root);
16084b82d6e4SYan 
1609c146afadSYan Zheng error_close_devices:
16108a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
161104d21a24SIlya Dryomov error_fs_info:
16126c41761fSDavid Sterba 	free_fs_info(fs_info);
1613f667aef6SQu Wenruo error_sec_opts:
1614f667aef6SQu Wenruo 	security_free_mnt_opts(&new_sec_opts);
1615061dbc6bSAl Viro 	return ERR_PTR(error);
16164b82d6e4SYan }
16172e635a27SChris Mason 
16180d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
16190d2450abSSergei Trofimovich 				     int new_pool_size, int old_pool_size)
16200d2450abSSergei Trofimovich {
16210d2450abSSergei Trofimovich 	if (new_pool_size == old_pool_size)
16220d2450abSSergei Trofimovich 		return;
16230d2450abSSergei Trofimovich 
16240d2450abSSergei Trofimovich 	fs_info->thread_pool_size = new_pool_size;
16250d2450abSSergei Trofimovich 
1626efe120a0SFrank Holton 	btrfs_info(fs_info, "resize thread pool %d -> %d",
16270d2450abSSergei Trofimovich 	       old_pool_size, new_pool_size);
16280d2450abSSergei Trofimovich 
16295cdc7ad3SQu Wenruo 	btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1630afe3d242SQu Wenruo 	btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1631a8c93d4eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size);
1632e66f0bb1SQu Wenruo 	btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1633fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size);
1634fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size);
1635fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_write_workers,
1636fccb5d86SQu Wenruo 				new_pool_size);
1637fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1638fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
16395b3bc44eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
1640736cfa15SQu Wenruo 	btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size);
16410339ef2fSQu Wenruo 	btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers,
1642ff023aacSStefan Behrens 				new_pool_size);
16430d2450abSSergei Trofimovich }
16440d2450abSSergei Trofimovich 
1645f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
1646dc81cdc5SMiao Xie {
1647dc81cdc5SMiao Xie 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1648f42a34b2SMiao Xie }
1649dc81cdc5SMiao Xie 
1650f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1651f42a34b2SMiao Xie 				       unsigned long old_opts, int flags)
1652f42a34b2SMiao Xie {
1653dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1654dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1655dc81cdc5SMiao Xie 	     (flags & MS_RDONLY))) {
1656dc81cdc5SMiao Xie 		/* wait for any defraggers to finish */
1657dc81cdc5SMiao Xie 		wait_event(fs_info->transaction_wait,
1658dc81cdc5SMiao Xie 			   (atomic_read(&fs_info->defrag_running) == 0));
1659dc81cdc5SMiao Xie 		if (flags & MS_RDONLY)
1660dc81cdc5SMiao Xie 			sync_filesystem(fs_info->sb);
1661dc81cdc5SMiao Xie 	}
1662dc81cdc5SMiao Xie }
1663dc81cdc5SMiao Xie 
1664dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1665dc81cdc5SMiao Xie 					 unsigned long old_opts)
1666dc81cdc5SMiao Xie {
1667dc81cdc5SMiao Xie 	/*
1668180e4d47SLuis de Bethencourt 	 * We need to cleanup all defragable inodes if the autodefragment is
1669180e4d47SLuis de Bethencourt 	 * close or the filesystem is read only.
1670dc81cdc5SMiao Xie 	 */
1671dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1672dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1673dc81cdc5SMiao Xie 	     (fs_info->sb->s_flags & MS_RDONLY))) {
1674dc81cdc5SMiao Xie 		btrfs_cleanup_defrag_inodes(fs_info);
1675dc81cdc5SMiao Xie 	}
1676dc81cdc5SMiao Xie 
1677dc81cdc5SMiao Xie 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1678dc81cdc5SMiao Xie }
1679dc81cdc5SMiao Xie 
1680c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1681c146afadSYan Zheng {
1682815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1683815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
168449b25e05SJeff Mahoney 	unsigned old_flags = sb->s_flags;
168549b25e05SJeff Mahoney 	unsigned long old_opts = fs_info->mount_opt;
168649b25e05SJeff Mahoney 	unsigned long old_compress_type = fs_info->compress_type;
168749b25e05SJeff Mahoney 	u64 old_max_inline = fs_info->max_inline;
168849b25e05SJeff Mahoney 	u64 old_alloc_start = fs_info->alloc_start;
168949b25e05SJeff Mahoney 	int old_thread_pool_size = fs_info->thread_pool_size;
169049b25e05SJeff Mahoney 	unsigned int old_metadata_ratio = fs_info->metadata_ratio;
1691c146afadSYan Zheng 	int ret;
1692c146afadSYan Zheng 
169302b9984dSTheodore Ts'o 	sync_filesystem(sb);
1694f42a34b2SMiao Xie 	btrfs_remount_prepare(fs_info);
1695dc81cdc5SMiao Xie 
1696f667aef6SQu Wenruo 	if (data) {
1697f667aef6SQu Wenruo 		struct security_mnt_opts new_sec_opts;
1698f667aef6SQu Wenruo 
1699f667aef6SQu Wenruo 		security_init_mnt_opts(&new_sec_opts);
1700f667aef6SQu Wenruo 		ret = parse_security_options(data, &new_sec_opts);
1701f667aef6SQu Wenruo 		if (ret)
1702f667aef6SQu Wenruo 			goto restore;
1703f667aef6SQu Wenruo 		ret = setup_security_options(fs_info, sb,
1704f667aef6SQu Wenruo 					     &new_sec_opts);
1705f667aef6SQu Wenruo 		if (ret) {
1706f667aef6SQu Wenruo 			security_free_mnt_opts(&new_sec_opts);
1707f667aef6SQu Wenruo 			goto restore;
1708f667aef6SQu Wenruo 		}
1709f667aef6SQu Wenruo 	}
1710f667aef6SQu Wenruo 
171196da0919SQu Wenruo 	ret = btrfs_parse_options(root, data, *flags);
171249b25e05SJeff Mahoney 	if (ret) {
171349b25e05SJeff Mahoney 		ret = -EINVAL;
171449b25e05SJeff Mahoney 		goto restore;
171549b25e05SJeff Mahoney 	}
1716b288052eSChris Mason 
1717f42a34b2SMiao Xie 	btrfs_remount_begin(fs_info, old_opts, *flags);
17180d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
17190d2450abSSergei Trofimovich 		fs_info->thread_pool_size, old_thread_pool_size);
17200d2450abSSergei Trofimovich 
1721c146afadSYan Zheng 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
1722dc81cdc5SMiao Xie 		goto out;
1723c146afadSYan Zheng 
1724c146afadSYan Zheng 	if (*flags & MS_RDONLY) {
17258dabb742SStefan Behrens 		/*
17268dabb742SStefan Behrens 		 * this also happens on 'umount -rf' or on shutdown, when
17278dabb742SStefan Behrens 		 * the filesystem is busy.
17288dabb742SStefan Behrens 		 */
172921c7e756SMiao Xie 		cancel_work_sync(&fs_info->async_reclaim_work);
1730361c093dSStefan Behrens 
1731361c093dSStefan Behrens 		/* wait for the uuid_scan task to finish */
1732361c093dSStefan Behrens 		down(&fs_info->uuid_tree_rescan_sem);
1733361c093dSStefan Behrens 		/* avoid complains from lockdep et al. */
1734361c093dSStefan Behrens 		up(&fs_info->uuid_tree_rescan_sem);
1735361c093dSStefan Behrens 
1736c146afadSYan Zheng 		sb->s_flags |= MS_RDONLY;
1737c146afadSYan Zheng 
1738e44163e1SJeff Mahoney 		/*
1739e44163e1SJeff Mahoney 		 * Setting MS_RDONLY will put the cleaner thread to
1740e44163e1SJeff Mahoney 		 * sleep at the next loop if it's already active.
1741e44163e1SJeff Mahoney 		 * If it's already asleep, we'll leave unused block
1742e44163e1SJeff Mahoney 		 * groups on disk until we're mounted read-write again
1743e44163e1SJeff Mahoney 		 * unless we clean them up here.
1744e44163e1SJeff Mahoney 		 */
1745e44163e1SJeff Mahoney 		btrfs_delete_unused_bgs(fs_info);
1746e44163e1SJeff Mahoney 
17478dabb742SStefan Behrens 		btrfs_dev_replace_suspend_for_unmount(fs_info);
17488dabb742SStefan Behrens 		btrfs_scrub_cancel(fs_info);
1749061594efSMiao Xie 		btrfs_pause_balance(fs_info);
17508dabb742SStefan Behrens 
1751c146afadSYan Zheng 		ret = btrfs_commit_super(root);
175249b25e05SJeff Mahoney 		if (ret)
175349b25e05SJeff Mahoney 			goto restore;
1754c146afadSYan Zheng 	} else {
17556ef3de9cSDavid Sterba 		if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
17566ef3de9cSDavid Sterba 			btrfs_err(fs_info,
1757efe120a0SFrank Holton 				"Remounting read-write after error is not allowed");
17586ef3de9cSDavid Sterba 			ret = -EINVAL;
17596ef3de9cSDavid Sterba 			goto restore;
17606ef3de9cSDavid Sterba 		}
17618a3db184SSergei Trofimovich 		if (fs_info->fs_devices->rw_devices == 0) {
176249b25e05SJeff Mahoney 			ret = -EACCES;
176349b25e05SJeff Mahoney 			goto restore;
17648a3db184SSergei Trofimovich 		}
17652b82032cSYan Zheng 
1766292fd7fcSStefan Behrens 		if (fs_info->fs_devices->missing_devices >
1767292fd7fcSStefan Behrens 		     fs_info->num_tolerated_disk_barrier_failures &&
1768292fd7fcSStefan Behrens 		    !(*flags & MS_RDONLY)) {
1769efe120a0SFrank Holton 			btrfs_warn(fs_info,
1770efe120a0SFrank Holton 				"too many missing devices, writeable remount is not allowed");
1771292fd7fcSStefan Behrens 			ret = -EACCES;
1772292fd7fcSStefan Behrens 			goto restore;
1773292fd7fcSStefan Behrens 		}
1774292fd7fcSStefan Behrens 
17758a3db184SSergei Trofimovich 		if (btrfs_super_log_root(fs_info->super_copy) != 0) {
177649b25e05SJeff Mahoney 			ret = -EINVAL;
177749b25e05SJeff Mahoney 			goto restore;
17788a3db184SSergei Trofimovich 		}
1779c146afadSYan Zheng 
1780815745cfSAl Viro 		ret = btrfs_cleanup_fs_roots(fs_info);
178149b25e05SJeff Mahoney 		if (ret)
178249b25e05SJeff Mahoney 			goto restore;
1783c146afadSYan Zheng 
1784d68fc57bSYan, Zheng 		/* recover relocation */
17855f316481SWang Shilong 		mutex_lock(&fs_info->cleaner_mutex);
1786d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
17875f316481SWang Shilong 		mutex_unlock(&fs_info->cleaner_mutex);
178849b25e05SJeff Mahoney 		if (ret)
178949b25e05SJeff Mahoney 			goto restore;
1790c146afadSYan Zheng 
17912b6ba629SIlya Dryomov 		ret = btrfs_resume_balance_async(fs_info);
17922b6ba629SIlya Dryomov 		if (ret)
17932b6ba629SIlya Dryomov 			goto restore;
17942b6ba629SIlya Dryomov 
17958dabb742SStefan Behrens 		ret = btrfs_resume_dev_replace_async(fs_info);
17968dabb742SStefan Behrens 		if (ret) {
1797efe120a0SFrank Holton 			btrfs_warn(fs_info, "failed to resume dev_replace");
17988dabb742SStefan Behrens 			goto restore;
17998dabb742SStefan Behrens 		}
180094aebfb2SJosef Bacik 
180194aebfb2SJosef Bacik 		if (!fs_info->uuid_root) {
1802efe120a0SFrank Holton 			btrfs_info(fs_info, "creating UUID tree");
180394aebfb2SJosef Bacik 			ret = btrfs_create_uuid_tree(fs_info);
180494aebfb2SJosef Bacik 			if (ret) {
1805efe120a0SFrank Holton 				btrfs_warn(fs_info, "failed to create the UUID tree %d", ret);
180694aebfb2SJosef Bacik 				goto restore;
180794aebfb2SJosef Bacik 			}
180894aebfb2SJosef Bacik 		}
1809c146afadSYan Zheng 		sb->s_flags &= ~MS_RDONLY;
1810*90c711abSZygo Blaxell 
1811*90c711abSZygo Blaxell 		fs_info->open = 1;
1812c146afadSYan Zheng 	}
1813dc81cdc5SMiao Xie out:
18142c6a92b0SJustin Maggard 	wake_up_process(fs_info->transaction_kthread);
1815dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
1816c146afadSYan Zheng 	return 0;
181749b25e05SJeff Mahoney 
181849b25e05SJeff Mahoney restore:
181949b25e05SJeff Mahoney 	/* We've hit an error - don't reset MS_RDONLY */
182049b25e05SJeff Mahoney 	if (sb->s_flags & MS_RDONLY)
182149b25e05SJeff Mahoney 		old_flags |= MS_RDONLY;
182249b25e05SJeff Mahoney 	sb->s_flags = old_flags;
182349b25e05SJeff Mahoney 	fs_info->mount_opt = old_opts;
182449b25e05SJeff Mahoney 	fs_info->compress_type = old_compress_type;
182549b25e05SJeff Mahoney 	fs_info->max_inline = old_max_inline;
1826c018daecSMiao Xie 	mutex_lock(&fs_info->chunk_mutex);
182749b25e05SJeff Mahoney 	fs_info->alloc_start = old_alloc_start;
1828c018daecSMiao Xie 	mutex_unlock(&fs_info->chunk_mutex);
18290d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
18300d2450abSSergei Trofimovich 		old_thread_pool_size, fs_info->thread_pool_size);
183149b25e05SJeff Mahoney 	fs_info->metadata_ratio = old_metadata_ratio;
1832dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
183349b25e05SJeff Mahoney 	return ret;
1834c146afadSYan Zheng }
1835c146afadSYan Zheng 
1836bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
1837bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1,
1838bcd53741SArne Jansen 				       const void *dev_info2)
1839bcd53741SArne Jansen {
1840bcd53741SArne Jansen 	if (((struct btrfs_device_info *)dev_info1)->max_avail >
1841bcd53741SArne Jansen 	    ((struct btrfs_device_info *)dev_info2)->max_avail)
1842bcd53741SArne Jansen 		return -1;
1843bcd53741SArne Jansen 	else if (((struct btrfs_device_info *)dev_info1)->max_avail <
1844bcd53741SArne Jansen 		 ((struct btrfs_device_info *)dev_info2)->max_avail)
1845bcd53741SArne Jansen 		return 1;
1846bcd53741SArne Jansen 	else
1847bcd53741SArne Jansen 	return 0;
1848bcd53741SArne Jansen }
1849bcd53741SArne Jansen 
1850bcd53741SArne Jansen /*
1851bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
1852bcd53741SArne Jansen  * is stored.(Descending Sort)
1853bcd53741SArne Jansen  */
1854bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
1855bcd53741SArne Jansen 					struct btrfs_device_info *devices,
1856bcd53741SArne Jansen 					size_t nr_devices)
1857bcd53741SArne Jansen {
1858bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1859bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
1860bcd53741SArne Jansen }
1861bcd53741SArne Jansen 
18626d07bcecSMiao Xie /*
18636d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
18646d07bcecSMiao Xie  * file data.
18656d07bcecSMiao Xie  */
18666d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
18676d07bcecSMiao Xie {
18686d07bcecSMiao Xie 	struct btrfs_fs_info *fs_info = root->fs_info;
18696d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
18706d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
18716d07bcecSMiao Xie 	struct btrfs_device *device;
18726d07bcecSMiao Xie 	u64 skip_space;
18736d07bcecSMiao Xie 	u64 type;
18746d07bcecSMiao Xie 	u64 avail_space;
18756d07bcecSMiao Xie 	u64 used_space;
18766d07bcecSMiao Xie 	u64 min_stripe_size;
187739fb26c3SMiao Xie 	int min_stripes = 1, num_stripes = 1;
18786d07bcecSMiao Xie 	int i = 0, nr_devices;
18796d07bcecSMiao Xie 	int ret;
18806d07bcecSMiao Xie 
18817e33fd99SJosef Bacik 	/*
188201327610SNicholas D Steeves 	 * We aren't under the device list lock, so this is racy-ish, but good
18837e33fd99SJosef Bacik 	 * enough for our purposes.
18847e33fd99SJosef Bacik 	 */
1885b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
18867e33fd99SJosef Bacik 	if (!nr_devices) {
18877e33fd99SJosef Bacik 		smp_mb();
18887e33fd99SJosef Bacik 		nr_devices = fs_info->fs_devices->open_devices;
18897e33fd99SJosef Bacik 		ASSERT(nr_devices);
18907e33fd99SJosef Bacik 		if (!nr_devices) {
18917e33fd99SJosef Bacik 			*free_bytes = 0;
18927e33fd99SJosef Bacik 			return 0;
18937e33fd99SJosef Bacik 		}
18947e33fd99SJosef Bacik 	}
18956d07bcecSMiao Xie 
1896d9b0d9baSDulshani Gunawardhana 	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
18976d07bcecSMiao Xie 			       GFP_NOFS);
18986d07bcecSMiao Xie 	if (!devices_info)
18996d07bcecSMiao Xie 		return -ENOMEM;
19006d07bcecSMiao Xie 
190101327610SNicholas D Steeves 	/* calc min stripe number for data space allocation */
19026d07bcecSMiao Xie 	type = btrfs_get_alloc_profile(root, 1);
190339fb26c3SMiao Xie 	if (type & BTRFS_BLOCK_GROUP_RAID0) {
19046d07bcecSMiao Xie 		min_stripes = 2;
190539fb26c3SMiao Xie 		num_stripes = nr_devices;
190639fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID1) {
19076d07bcecSMiao Xie 		min_stripes = 2;
190839fb26c3SMiao Xie 		num_stripes = 2;
190939fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID10) {
19106d07bcecSMiao Xie 		min_stripes = 4;
191139fb26c3SMiao Xie 		num_stripes = 4;
191239fb26c3SMiao Xie 	}
19136d07bcecSMiao Xie 
19146d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_DUP)
19156d07bcecSMiao Xie 		min_stripe_size = 2 * BTRFS_STRIPE_LEN;
19166d07bcecSMiao Xie 	else
19176d07bcecSMiao Xie 		min_stripe_size = BTRFS_STRIPE_LEN;
19186d07bcecSMiao Xie 
19197e33fd99SJosef Bacik 	if (fs_info->alloc_start)
19207e33fd99SJosef Bacik 		mutex_lock(&fs_devices->device_list_mutex);
19217e33fd99SJosef Bacik 	rcu_read_lock();
19227e33fd99SJosef Bacik 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
192363a212abSStefan Behrens 		if (!device->in_fs_metadata || !device->bdev ||
192463a212abSStefan Behrens 		    device->is_tgtdev_for_dev_replace)
19256d07bcecSMiao Xie 			continue;
19266d07bcecSMiao Xie 
19277e33fd99SJosef Bacik 		if (i >= nr_devices)
19287e33fd99SJosef Bacik 			break;
19297e33fd99SJosef Bacik 
19306d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
19316d07bcecSMiao Xie 
19326d07bcecSMiao Xie 		/* align with stripe_len */
1933f8c269d7SDavid Sterba 		avail_space = div_u64(avail_space, BTRFS_STRIPE_LEN);
19346d07bcecSMiao Xie 		avail_space *= BTRFS_STRIPE_LEN;
19356d07bcecSMiao Xie 
19366d07bcecSMiao Xie 		/*
193701327610SNicholas D Steeves 		 * In order to avoid overwriting the superblock on the drive,
19386d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
19396d07bcecSMiao Xie 		 * allocation.
19406d07bcecSMiao Xie 		 */
1941ee22184bSByongho Lee 		skip_space = SZ_1M;
19426d07bcecSMiao Xie 
19436d07bcecSMiao Xie 		/* user can set the offset in fs_info->alloc_start. */
19447e33fd99SJosef Bacik 		if (fs_info->alloc_start &&
19457e33fd99SJosef Bacik 		    fs_info->alloc_start + BTRFS_STRIPE_LEN <=
19467e33fd99SJosef Bacik 		    device->total_bytes) {
19477e33fd99SJosef Bacik 			rcu_read_unlock();
19486d07bcecSMiao Xie 			skip_space = max(fs_info->alloc_start, skip_space);
19496d07bcecSMiao Xie 
19506d07bcecSMiao Xie 			/*
19517e33fd99SJosef Bacik 			 * btrfs can not use the free space in
19527e33fd99SJosef Bacik 			 * [0, skip_space - 1], we must subtract it from the
19537e33fd99SJosef Bacik 			 * total. In order to implement it, we account the used
19547e33fd99SJosef Bacik 			 * space in this range first.
19556d07bcecSMiao Xie 			 */
19567e33fd99SJosef Bacik 			ret = btrfs_account_dev_extents_size(device, 0,
19577e33fd99SJosef Bacik 							     skip_space - 1,
19586d07bcecSMiao Xie 							     &used_space);
19596d07bcecSMiao Xie 			if (ret) {
19606d07bcecSMiao Xie 				kfree(devices_info);
19617e33fd99SJosef Bacik 				mutex_unlock(&fs_devices->device_list_mutex);
19626d07bcecSMiao Xie 				return ret;
19636d07bcecSMiao Xie 			}
19646d07bcecSMiao Xie 
19657e33fd99SJosef Bacik 			rcu_read_lock();
19667e33fd99SJosef Bacik 
19676d07bcecSMiao Xie 			/* calc the free space in [0, skip_space - 1] */
19686d07bcecSMiao Xie 			skip_space -= used_space;
19697e33fd99SJosef Bacik 		}
19706d07bcecSMiao Xie 
19716d07bcecSMiao Xie 		/*
19726d07bcecSMiao Xie 		 * we can use the free space in [0, skip_space - 1], subtract
19736d07bcecSMiao Xie 		 * it from the total.
19746d07bcecSMiao Xie 		 */
19756d07bcecSMiao Xie 		if (avail_space && avail_space >= skip_space)
19766d07bcecSMiao Xie 			avail_space -= skip_space;
19776d07bcecSMiao Xie 		else
19786d07bcecSMiao Xie 			avail_space = 0;
19796d07bcecSMiao Xie 
19806d07bcecSMiao Xie 		if (avail_space < min_stripe_size)
19816d07bcecSMiao Xie 			continue;
19826d07bcecSMiao Xie 
19836d07bcecSMiao Xie 		devices_info[i].dev = device;
19846d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
19856d07bcecSMiao Xie 
19866d07bcecSMiao Xie 		i++;
19876d07bcecSMiao Xie 	}
19887e33fd99SJosef Bacik 	rcu_read_unlock();
19897e33fd99SJosef Bacik 	if (fs_info->alloc_start)
19907e33fd99SJosef Bacik 		mutex_unlock(&fs_devices->device_list_mutex);
19916d07bcecSMiao Xie 
19926d07bcecSMiao Xie 	nr_devices = i;
19936d07bcecSMiao Xie 
19946d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
19956d07bcecSMiao Xie 
19966d07bcecSMiao Xie 	i = nr_devices - 1;
19976d07bcecSMiao Xie 	avail_space = 0;
19986d07bcecSMiao Xie 	while (nr_devices >= min_stripes) {
199939fb26c3SMiao Xie 		if (num_stripes > nr_devices)
200039fb26c3SMiao Xie 			num_stripes = nr_devices;
200139fb26c3SMiao Xie 
20026d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
20036d07bcecSMiao Xie 			int j;
20046d07bcecSMiao Xie 			u64 alloc_size;
20056d07bcecSMiao Xie 
200639fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
20076d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
200839fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
20096d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
20106d07bcecSMiao Xie 		}
20116d07bcecSMiao Xie 		i--;
20126d07bcecSMiao Xie 		nr_devices--;
20136d07bcecSMiao Xie 	}
20146d07bcecSMiao Xie 
20156d07bcecSMiao Xie 	kfree(devices_info);
20166d07bcecSMiao Xie 	*free_bytes = avail_space;
20176d07bcecSMiao Xie 	return 0;
20186d07bcecSMiao Xie }
20196d07bcecSMiao Xie 
2020ba7b6e62SDavid Sterba /*
2021ba7b6e62SDavid Sterba  * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2022ba7b6e62SDavid Sterba  *
2023ba7b6e62SDavid Sterba  * If there's a redundant raid level at DATA block groups, use the respective
2024ba7b6e62SDavid Sterba  * multiplier to scale the sizes.
2025ba7b6e62SDavid Sterba  *
2026ba7b6e62SDavid Sterba  * Unused device space usage is based on simulating the chunk allocator
2027ba7b6e62SDavid Sterba  * algorithm that respects the device sizes, order of allocations and the
2028ba7b6e62SDavid Sterba  * 'alloc_start' value, this is a close approximation of the actual use but
2029ba7b6e62SDavid Sterba  * there are other factors that may change the result (like a new metadata
2030ba7b6e62SDavid Sterba  * chunk).
2031ba7b6e62SDavid Sterba  *
2032ca8a51b3SDavid Sterba  * If metadata is exhausted, f_bavail will be 0.
2033ca8a51b3SDavid Sterba  *
2034ba7b6e62SDavid Sterba  * FIXME: not accurate for mixed block groups, total and free/used are ok,
2035ba7b6e62SDavid Sterba  * available appears slightly larger.
2036ba7b6e62SDavid Sterba  */
20378fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
20388fd17795SChris Mason {
2039815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
2040815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
2041815745cfSAl Viro 	struct list_head *head = &fs_info->space_info;
2042bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
2043bd4d1088SJosef Bacik 	u64 total_used = 0;
20446d07bcecSMiao Xie 	u64 total_free_data = 0;
2045ca8a51b3SDavid Sterba 	u64 total_free_meta = 0;
2046db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
2047815745cfSAl Viro 	__be32 *fsid = (__be32 *)fs_info->fsid;
2048ba7b6e62SDavid Sterba 	unsigned factor = 1;
2049ba7b6e62SDavid Sterba 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
20506d07bcecSMiao Xie 	int ret;
2051ca8a51b3SDavid Sterba 	u64 thresh = 0;
2052ae02d1bdSLuis de Bethencourt 	int mixed = 0;
20538fd17795SChris Mason 
205415484377SMiao Xie 	/*
2055180e4d47SLuis de Bethencourt 	 * holding chunk_mutex to avoid allocating new chunks, holding
205615484377SMiao Xie 	 * device_list_mutex to avoid the device being removed
205715484377SMiao Xie 	 */
2058bd4d1088SJosef Bacik 	rcu_read_lock();
205989a55897SJosef Bacik 	list_for_each_entry_rcu(found, head, list) {
20606d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
2061ba7b6e62SDavid Sterba 			int i;
2062ba7b6e62SDavid Sterba 
20636d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
20646d07bcecSMiao Xie 			total_free_data -=
20656d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
2066ba7b6e62SDavid Sterba 
2067ba7b6e62SDavid Sterba 			for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
2068ba7b6e62SDavid Sterba 				if (!list_empty(&found->block_groups[i])) {
2069ba7b6e62SDavid Sterba 					switch (i) {
2070ba7b6e62SDavid Sterba 					case BTRFS_RAID_DUP:
2071ba7b6e62SDavid Sterba 					case BTRFS_RAID_RAID1:
2072ba7b6e62SDavid Sterba 					case BTRFS_RAID_RAID10:
2073ba7b6e62SDavid Sterba 						factor = 2;
2074ba7b6e62SDavid Sterba 					}
2075ba7b6e62SDavid Sterba 				}
2076ba7b6e62SDavid Sterba 			}
20776d07bcecSMiao Xie 		}
2078ae02d1bdSLuis de Bethencourt 
2079ae02d1bdSLuis de Bethencourt 		/*
2080ae02d1bdSLuis de Bethencourt 		 * Metadata in mixed block goup profiles are accounted in data
2081ae02d1bdSLuis de Bethencourt 		 */
2082ae02d1bdSLuis de Bethencourt 		if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
2083ae02d1bdSLuis de Bethencourt 			if (found->flags & BTRFS_BLOCK_GROUP_DATA)
2084ae02d1bdSLuis de Bethencourt 				mixed = 1;
2085ae02d1bdSLuis de Bethencourt 			else
2086ae02d1bdSLuis de Bethencourt 				total_free_meta += found->disk_total -
2087ae02d1bdSLuis de Bethencourt 					found->disk_used;
2088ae02d1bdSLuis de Bethencourt 		}
20896d07bcecSMiao Xie 
2090b742bb82SYan, Zheng 		total_used += found->disk_used;
209189a55897SJosef Bacik 	}
2092ba7b6e62SDavid Sterba 
2093bd4d1088SJosef Bacik 	rcu_read_unlock();
2094bd4d1088SJosef Bacik 
2095ba7b6e62SDavid Sterba 	buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
2096ba7b6e62SDavid Sterba 	buf->f_blocks >>= bits;
2097ba7b6e62SDavid Sterba 	buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
2098ba7b6e62SDavid Sterba 
2099ba7b6e62SDavid Sterba 	/* Account global block reserve as used, it's in logical size already */
2100ba7b6e62SDavid Sterba 	spin_lock(&block_rsv->lock);
210141b34accSLuis de Bethencourt 	/* Mixed block groups accounting is not byte-accurate, avoid overflow */
210241b34accSLuis de Bethencourt 	if (buf->f_bfree >= block_rsv->size >> bits)
2103ba7b6e62SDavid Sterba 		buf->f_bfree -= block_rsv->size >> bits;
210441b34accSLuis de Bethencourt 	else
210541b34accSLuis de Bethencourt 		buf->f_bfree = 0;
2106ba7b6e62SDavid Sterba 	spin_unlock(&block_rsv->lock);
2107ba7b6e62SDavid Sterba 
21080d95c1beSDavid Sterba 	buf->f_bavail = div_u64(total_free_data, factor);
2109815745cfSAl Viro 	ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data);
21107e33fd99SJosef Bacik 	if (ret)
21116d07bcecSMiao Xie 		return ret;
2112ba7b6e62SDavid Sterba 	buf->f_bavail += div_u64(total_free_data, factor);
21136d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
2114d397712bSChris Mason 
2115ca8a51b3SDavid Sterba 	/*
2116ca8a51b3SDavid Sterba 	 * We calculate the remaining metadata space minus global reserve. If
2117ca8a51b3SDavid Sterba 	 * this is (supposedly) smaller than zero, there's no space. But this
2118ca8a51b3SDavid Sterba 	 * does not hold in practice, the exhausted state happens where's still
2119ca8a51b3SDavid Sterba 	 * some positive delta. So we apply some guesswork and compare the
2120ca8a51b3SDavid Sterba 	 * delta to a 4M threshold.  (Practically observed delta was ~2M.)
2121ca8a51b3SDavid Sterba 	 *
2122ca8a51b3SDavid Sterba 	 * We probably cannot calculate the exact threshold value because this
2123ca8a51b3SDavid Sterba 	 * depends on the internal reservations requested by various
2124ca8a51b3SDavid Sterba 	 * operations, so some operations that consume a few metadata will
2125ca8a51b3SDavid Sterba 	 * succeed even if the Avail is zero. But this is better than the other
2126ca8a51b3SDavid Sterba 	 * way around.
2127ca8a51b3SDavid Sterba 	 */
2128ca8a51b3SDavid Sterba 	thresh = 4 * 1024 * 1024;
2129ca8a51b3SDavid Sterba 
2130ae02d1bdSLuis de Bethencourt 	if (!mixed && total_free_meta - thresh < block_rsv->size)
2131ca8a51b3SDavid Sterba 		buf->f_bavail = 0;
2132ca8a51b3SDavid Sterba 
2133ba7b6e62SDavid Sterba 	buf->f_type = BTRFS_SUPER_MAGIC;
2134ba7b6e62SDavid Sterba 	buf->f_bsize = dentry->d_sb->s_blocksize;
2135ba7b6e62SDavid Sterba 	buf->f_namelen = BTRFS_NAME_LEN;
2136ba7b6e62SDavid Sterba 
21379d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
21389d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
21399d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
21409d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
21419d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
214232d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
21432b0143b5SDavid Howells 	buf->f_fsid.val[0] ^= BTRFS_I(d_inode(dentry))->root->objectid >> 32;
21442b0143b5SDavid Howells 	buf->f_fsid.val[1] ^= BTRFS_I(d_inode(dentry))->root->objectid;
214532d48fa1SDavid Woodhouse 
21468fd17795SChris Mason 	return 0;
21478fd17795SChris Mason }
2148b5133862SChris Mason 
2149aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
2150aea52e19SAl Viro {
2151815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2152aea52e19SAl Viro 	kill_anon_super(sb);
2153aea52e19SAl Viro 	free_fs_info(fs_info);
2154aea52e19SAl Viro }
2155aea52e19SAl Viro 
21562e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
21572e635a27SChris Mason 	.owner		= THIS_MODULE,
21582e635a27SChris Mason 	.name		= "btrfs",
2159061dbc6bSAl Viro 	.mount		= btrfs_mount,
2160aea52e19SAl Viro 	.kill_sb	= btrfs_kill_super,
2161f667aef6SQu Wenruo 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
21622e635a27SChris Mason };
21637f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs");
2164a9218f6bSChris Mason 
2165d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file)
2166d8620958STom Van Braeckel {
2167d8620958STom Van Braeckel 	/*
2168d8620958STom Van Braeckel 	 * The control file's private_data is used to hold the
2169d8620958STom Van Braeckel 	 * transaction when it is started and is used to keep
2170d8620958STom Van Braeckel 	 * track of whether a transaction is already in progress.
2171d8620958STom Van Braeckel 	 */
2172d8620958STom Van Braeckel 	file->private_data = NULL;
2173d8620958STom Van Braeckel 	return 0;
2174d8620958STom Van Braeckel }
2175d8620958STom Van Braeckel 
2176d352ac68SChris Mason /*
2177d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
2178d352ac68SChris Mason  */
21798a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
21808a4b83ccSChris Mason 				unsigned long arg)
21818a4b83ccSChris Mason {
21828a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
21838a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
2184c071fcfdSChris Mason 	int ret = -ENOTTY;
21858a4b83ccSChris Mason 
2186e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2187e441d54dSChris Mason 		return -EPERM;
2188e441d54dSChris Mason 
2189dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
2190dae7b665SLi Zefan 	if (IS_ERR(vol))
2191dae7b665SLi Zefan 		return PTR_ERR(vol);
2192c071fcfdSChris Mason 
21938a4b83ccSChris Mason 	switch (cmd) {
21948a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
219597288f2cSChristoph Hellwig 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
21968a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
21978a4b83ccSChris Mason 		break;
219802db0844SJosef Bacik 	case BTRFS_IOC_DEVICES_READY:
219902db0844SJosef Bacik 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
220002db0844SJosef Bacik 					    &btrfs_fs_type, &fs_devices);
220102db0844SJosef Bacik 		if (ret)
220202db0844SJosef Bacik 			break;
220302db0844SJosef Bacik 		ret = !(fs_devices->num_devices == fs_devices->total_devices);
220402db0844SJosef Bacik 		break;
2205c5868f83SDavid Sterba 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2206d5131b65SDavid Sterba 		ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2207c5868f83SDavid Sterba 		break;
22088a4b83ccSChris Mason 	}
2209dae7b665SLi Zefan 
22108a4b83ccSChris Mason 	kfree(vol);
2211f819d837SLinda Knippers 	return ret;
22128a4b83ccSChris Mason }
22138a4b83ccSChris Mason 
22140176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
2215ed0dab6bSYan {
2216354aa0fbSMiao Xie 	struct btrfs_trans_handle *trans;
2217354aa0fbSMiao Xie 	struct btrfs_root *root = btrfs_sb(sb)->tree_root;
2218354aa0fbSMiao Xie 
2219d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
2220354aa0fbSMiao Xie 	if (IS_ERR(trans)) {
2221354aa0fbSMiao Xie 		/* no transaction, don't bother */
2222354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
22230176260fSLinus Torvalds 			return 0;
2224354aa0fbSMiao Xie 		return PTR_ERR(trans);
2225354aa0fbSMiao Xie 	}
2226354aa0fbSMiao Xie 	return btrfs_commit_transaction(trans, root);
2227ed0dab6bSYan }
2228ed0dab6bSYan 
22299c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
22309c5085c1SJosef Bacik {
22319c5085c1SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
22329c5085c1SJosef Bacik 	struct btrfs_fs_devices *cur_devices;
22339c5085c1SJosef Bacik 	struct btrfs_device *dev, *first_dev = NULL;
22349c5085c1SJosef Bacik 	struct list_head *head;
22359c5085c1SJosef Bacik 	struct rcu_string *name;
22369c5085c1SJosef Bacik 
22379c5085c1SJosef Bacik 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
22389c5085c1SJosef Bacik 	cur_devices = fs_info->fs_devices;
22399c5085c1SJosef Bacik 	while (cur_devices) {
22409c5085c1SJosef Bacik 		head = &cur_devices->devices;
22419c5085c1SJosef Bacik 		list_for_each_entry(dev, head, dev_list) {
2242aa9ddcd4SJosef Bacik 			if (dev->missing)
2243aa9ddcd4SJosef Bacik 				continue;
22440aeb8a6eSAnand Jain 			if (!dev->name)
22450aeb8a6eSAnand Jain 				continue;
22469c5085c1SJosef Bacik 			if (!first_dev || dev->devid < first_dev->devid)
22479c5085c1SJosef Bacik 				first_dev = dev;
22489c5085c1SJosef Bacik 		}
22499c5085c1SJosef Bacik 		cur_devices = cur_devices->seed;
22509c5085c1SJosef Bacik 	}
22519c5085c1SJosef Bacik 
22529c5085c1SJosef Bacik 	if (first_dev) {
22539c5085c1SJosef Bacik 		rcu_read_lock();
22549c5085c1SJosef Bacik 		name = rcu_dereference(first_dev->name);
22559c5085c1SJosef Bacik 		seq_escape(m, name->str, " \t\n\\");
22569c5085c1SJosef Bacik 		rcu_read_unlock();
22579c5085c1SJosef Bacik 	} else {
22589c5085c1SJosef Bacik 		WARN_ON(1);
22599c5085c1SJosef Bacik 	}
22609c5085c1SJosef Bacik 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
22619c5085c1SJosef Bacik 	return 0;
22629c5085c1SJosef Bacik }
22639c5085c1SJosef Bacik 
2264b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
226576dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
2266bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
2267e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
2268d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
2269a9572a15SEric Paris 	.show_options	= btrfs_show_options,
22709c5085c1SJosef Bacik 	.show_devname	= btrfs_show_devname,
22714730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
22722c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
22732c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
22748fd17795SChris Mason 	.statfs		= btrfs_statfs,
2275c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
22760176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
2277e20d96d6SChris Mason };
2278a9218f6bSChris Mason 
2279a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
2280d8620958STom Van Braeckel 	.open = btrfs_control_open,
2281a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
2282a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
2283a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
22846038f373SArnd Bergmann 	.llseek = noop_llseek,
2285a9218f6bSChris Mason };
2286a9218f6bSChris Mason 
2287a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
2288578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
2289a9218f6bSChris Mason 	.name		= "btrfs-control",
2290a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
2291a9218f6bSChris Mason };
2292a9218f6bSChris Mason 
2293578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2294578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
2295578454ffSKay Sievers 
2296a9218f6bSChris Mason static int btrfs_interface_init(void)
2297a9218f6bSChris Mason {
2298a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
2299a9218f6bSChris Mason }
2300a9218f6bSChris Mason 
2301b2950863SChristoph Hellwig static void btrfs_interface_exit(void)
2302a9218f6bSChris Mason {
2303f368ed60SGreg Kroah-Hartman 	misc_deregister(&btrfs_misc);
2304a9218f6bSChris Mason }
2305a9218f6bSChris Mason 
23068ae1af3cSAnand Jain static void btrfs_print_mod_info(void)
230785965600SDavid Sterba {
23085f9e1059SJeff Mahoney 	printk(KERN_INFO "Btrfs loaded, crc32c=%s"
230985965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
231085965600SDavid Sterba 			", debug=on"
231185965600SDavid Sterba #endif
231279556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT
231379556c3dSStefan Behrens 			", assert=on"
231479556c3dSStefan Behrens #endif
231585965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
231685965600SDavid Sterba 			", integrity-checker=on"
231785965600SDavid Sterba #endif
23185f9e1059SJeff Mahoney 			"\n",
23195f9e1059SJeff Mahoney 			btrfs_crc32c_impl());
232085965600SDavid Sterba }
232185965600SDavid Sterba 
2322dc11dd5dSJosef Bacik static int btrfs_run_sanity_tests(void)
2323dc11dd5dSJosef Bacik {
2324d94f43b4SFeifei Xu 	int ret, i;
2325b9ef22deSFeifei Xu 	u32 sectorsize, nodesize;
2326d94f43b4SFeifei Xu 	u32 test_sectorsize[] = {
2327d94f43b4SFeifei Xu 		PAGE_SIZE,
2328d94f43b4SFeifei Xu 	};
2329294e30feSJosef Bacik 	ret = btrfs_init_test_fs();
233006ea65a3SJosef Bacik 	if (ret)
233106ea65a3SJosef Bacik 		return ret;
2332d94f43b4SFeifei Xu 	for (i = 0; i < ARRAY_SIZE(test_sectorsize); i++) {
2333d94f43b4SFeifei Xu 		sectorsize = test_sectorsize[i];
2334d94f43b4SFeifei Xu 		for (nodesize = sectorsize;
2335d94f43b4SFeifei Xu 		     nodesize <= BTRFS_MAX_METADATA_BLOCKSIZE;
2336d94f43b4SFeifei Xu 		     nodesize <<= 1) {
2337d94f43b4SFeifei Xu 			pr_info("BTRFS: selftest: sectorsize: %u  nodesize: %u\n",
2338d94f43b4SFeifei Xu 				sectorsize, nodesize);
2339b9ef22deSFeifei Xu 			ret = btrfs_test_free_space_cache(sectorsize, nodesize);
2340294e30feSJosef Bacik 			if (ret)
2341294e30feSJosef Bacik 				goto out;
2342b9ef22deSFeifei Xu 			ret = btrfs_test_extent_buffer_operations(sectorsize,
2343b9ef22deSFeifei Xu 				nodesize);
2344294e30feSJosef Bacik 			if (ret)
2345294e30feSJosef Bacik 				goto out;
2346b9ef22deSFeifei Xu 			ret = btrfs_test_extent_io(sectorsize, nodesize);
2347aaedb55bSJosef Bacik 			if (ret)
2348aaedb55bSJosef Bacik 				goto out;
2349b9ef22deSFeifei Xu 			ret = btrfs_test_inodes(sectorsize, nodesize);
2350faa2dbf0SJosef Bacik 			if (ret)
2351faa2dbf0SJosef Bacik 				goto out;
2352b9ef22deSFeifei Xu 			ret = btrfs_test_qgroups(sectorsize, nodesize);
23537c55ee0cSOmar Sandoval 			if (ret)
23547c55ee0cSOmar Sandoval 				goto out;
2355b9ef22deSFeifei Xu 			ret = btrfs_test_free_space_tree(sectorsize, nodesize);
2356d94f43b4SFeifei Xu 			if (ret)
2357d94f43b4SFeifei Xu 				goto out;
2358d94f43b4SFeifei Xu 		}
2359d94f43b4SFeifei Xu 	}
2360294e30feSJosef Bacik out:
2361294e30feSJosef Bacik 	btrfs_destroy_test_fs();
2362294e30feSJosef Bacik 	return ret;
2363dc11dd5dSJosef Bacik }
2364dc11dd5dSJosef Bacik 
23652e635a27SChris Mason static int __init init_btrfs_fs(void)
23662e635a27SChris Mason {
23672c90e5d6SChris Mason 	int err;
236858176a96SJosef Bacik 
236914a958e6SFilipe David Borba Manana 	err = btrfs_hash_init();
237014a958e6SFilipe David Borba Manana 	if (err)
237114a958e6SFilipe David Borba Manana 		return err;
237214a958e6SFilipe David Borba Manana 
237363541927SFilipe David Borba Manana 	btrfs_props_init();
237463541927SFilipe David Borba Manana 
237558176a96SJosef Bacik 	err = btrfs_init_sysfs();
237658176a96SJosef Bacik 	if (err)
237714a958e6SFilipe David Borba Manana 		goto free_hash;
237858176a96SJosef Bacik 
2379143bede5SJeff Mahoney 	btrfs_init_compress();
2380d1310b2eSChris Mason 
2381261507a0SLi Zefan 	err = btrfs_init_cachep();
2382261507a0SLi Zefan 	if (err)
2383261507a0SLi Zefan 		goto free_compress;
2384261507a0SLi Zefan 
2385d1310b2eSChris Mason 	err = extent_io_init();
23862f4cbe64SWyatt Banks 	if (err)
23872f4cbe64SWyatt Banks 		goto free_cachep;
23882f4cbe64SWyatt Banks 
2389d1310b2eSChris Mason 	err = extent_map_init();
2390d1310b2eSChris Mason 	if (err)
2391d1310b2eSChris Mason 		goto free_extent_io;
2392d1310b2eSChris Mason 
23936352b91dSMiao Xie 	err = ordered_data_init();
23942f4cbe64SWyatt Banks 	if (err)
23952f4cbe64SWyatt Banks 		goto free_extent_map;
2396c8b97818SChris Mason 
23976352b91dSMiao Xie 	err = btrfs_delayed_inode_init();
23986352b91dSMiao Xie 	if (err)
23996352b91dSMiao Xie 		goto free_ordered_data;
24006352b91dSMiao Xie 
24019247f317SMiao Xie 	err = btrfs_auto_defrag_init();
240216cdcec7SMiao Xie 	if (err)
240316cdcec7SMiao Xie 		goto free_delayed_inode;
240416cdcec7SMiao Xie 
240578a6184aSMiao Xie 	err = btrfs_delayed_ref_init();
24069247f317SMiao Xie 	if (err)
24079247f317SMiao Xie 		goto free_auto_defrag;
24089247f317SMiao Xie 
2409b9e9a6cbSWang Shilong 	err = btrfs_prelim_ref_init();
2410b9e9a6cbSWang Shilong 	if (err)
2411af13b492SDavid Sterba 		goto free_delayed_ref;
2412b9e9a6cbSWang Shilong 
241397eb6b69SDavid Sterba 	err = btrfs_end_io_wq_init();
241478a6184aSMiao Xie 	if (err)
2415af13b492SDavid Sterba 		goto free_prelim_ref;
241678a6184aSMiao Xie 
241797eb6b69SDavid Sterba 	err = btrfs_interface_init();
241897eb6b69SDavid Sterba 	if (err)
241997eb6b69SDavid Sterba 		goto free_end_io_wq;
242097eb6b69SDavid Sterba 
2421e565d4b9SJan Schmidt 	btrfs_init_lockdep();
2422e565d4b9SJan Schmidt 
24238ae1af3cSAnand Jain 	btrfs_print_mod_info();
2424dc11dd5dSJosef Bacik 
2425dc11dd5dSJosef Bacik 	err = btrfs_run_sanity_tests();
2426dc11dd5dSJosef Bacik 	if (err)
2427dc11dd5dSJosef Bacik 		goto unregister_ioctl;
2428dc11dd5dSJosef Bacik 
2429dc11dd5dSJosef Bacik 	err = register_filesystem(&btrfs_fs_type);
2430dc11dd5dSJosef Bacik 	if (err)
2431dc11dd5dSJosef Bacik 		goto unregister_ioctl;
243274255aa0SJosef Bacik 
24332f4cbe64SWyatt Banks 	return 0;
24342f4cbe64SWyatt Banks 
2435a9218f6bSChris Mason unregister_ioctl:
2436a9218f6bSChris Mason 	btrfs_interface_exit();
243797eb6b69SDavid Sterba free_end_io_wq:
243897eb6b69SDavid Sterba 	btrfs_end_io_wq_exit();
2439b9e9a6cbSWang Shilong free_prelim_ref:
2440b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
244178a6184aSMiao Xie free_delayed_ref:
244278a6184aSMiao Xie 	btrfs_delayed_ref_exit();
24439247f317SMiao Xie free_auto_defrag:
24449247f317SMiao Xie 	btrfs_auto_defrag_exit();
244516cdcec7SMiao Xie free_delayed_inode:
244616cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
24476352b91dSMiao Xie free_ordered_data:
24486352b91dSMiao Xie 	ordered_data_exit();
24492f4cbe64SWyatt Banks free_extent_map:
24502f4cbe64SWyatt Banks 	extent_map_exit();
2451d1310b2eSChris Mason free_extent_io:
2452d1310b2eSChris Mason 	extent_io_exit();
24532f4cbe64SWyatt Banks free_cachep:
24542f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
2455261507a0SLi Zefan free_compress:
2456261507a0SLi Zefan 	btrfs_exit_compress();
24572f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
245814a958e6SFilipe David Borba Manana free_hash:
245914a958e6SFilipe David Borba Manana 	btrfs_hash_exit();
24602c90e5d6SChris Mason 	return err;
24612e635a27SChris Mason }
24622e635a27SChris Mason 
24632e635a27SChris Mason static void __exit exit_btrfs_fs(void)
24642e635a27SChris Mason {
246539279cc3SChris Mason 	btrfs_destroy_cachep();
246678a6184aSMiao Xie 	btrfs_delayed_ref_exit();
24679247f317SMiao Xie 	btrfs_auto_defrag_exit();
246816cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
2469b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
24706352b91dSMiao Xie 	ordered_data_exit();
2471a52d9a80SChris Mason 	extent_map_exit();
2472d1310b2eSChris Mason 	extent_io_exit();
2473a9218f6bSChris Mason 	btrfs_interface_exit();
24745ed5f588SJosef Bacik 	btrfs_end_io_wq_exit();
24752e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
247658176a96SJosef Bacik 	btrfs_exit_sysfs();
24778a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
2478261507a0SLi Zefan 	btrfs_exit_compress();
247914a958e6SFilipe David Borba Manana 	btrfs_hash_exit();
24802e635a27SChris Mason }
24812e635a27SChris Mason 
248260efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs);
24832e635a27SChris Mason module_exit(exit_btrfs_fs)
24842e635a27SChris Mason 
24852e635a27SChris Mason MODULE_LICENSE("GPL");
2486