xref: /openbmc/linux/fs/btrfs/super.c (revision 8b87dc17)
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>
454b4e25f2SChris Mason #include "compat.h"
4616cdcec7SMiao Xie #include "delayed-inode.h"
472e635a27SChris Mason #include "ctree.h"
48e20d96d6SChris Mason #include "disk-io.h"
49d5719762SChris Mason #include "transaction.h"
502c90e5d6SChris Mason #include "btrfs_inode.h"
513a686375SChris Mason #include "print-tree.h"
525103e947SJosef Bacik #include "xattr.h"
538a4b83ccSChris Mason #include "volumes.h"
54be6e8dc0SBalaji Rao #include "export.h"
55c8b97818SChris Mason #include "compression.h"
569c5085c1SJosef Bacik #include "rcu-string.h"
578dabb742SStefan Behrens #include "dev-replace.h"
5874255aa0SJosef Bacik #include "free-space-cache.h"
592e635a27SChris Mason 
601abe9b8aSliubo #define CREATE_TRACE_POINTS
611abe9b8aSliubo #include <trace/events/btrfs.h>
621abe9b8aSliubo 
63b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
64830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type;
65e20d96d6SChris Mason 
6608748810SDavid Sterba static const char *btrfs_decode_error(int errno)
67acce952bSliubo {
6808748810SDavid Sterba 	char *errstr = "unknown";
69acce952bSliubo 
70acce952bSliubo 	switch (errno) {
71acce952bSliubo 	case -EIO:
72acce952bSliubo 		errstr = "IO failure";
73acce952bSliubo 		break;
74acce952bSliubo 	case -ENOMEM:
75acce952bSliubo 		errstr = "Out of memory";
76acce952bSliubo 		break;
77acce952bSliubo 	case -EROFS:
78acce952bSliubo 		errstr = "Readonly filesystem";
79acce952bSliubo 		break;
808c342930SJeff Mahoney 	case -EEXIST:
818c342930SJeff Mahoney 		errstr = "Object already exists";
828c342930SJeff Mahoney 		break;
8394ef7280SDavid Sterba 	case -ENOSPC:
8494ef7280SDavid Sterba 		errstr = "No space left";
8594ef7280SDavid Sterba 		break;
8694ef7280SDavid Sterba 	case -ENOENT:
8794ef7280SDavid Sterba 		errstr = "No such entry";
8894ef7280SDavid Sterba 		break;
89acce952bSliubo 	}
90acce952bSliubo 
91acce952bSliubo 	return errstr;
92acce952bSliubo }
93acce952bSliubo 
94bbece8a3SDavid Sterba static void save_error_info(struct btrfs_fs_info *fs_info)
95acce952bSliubo {
96acce952bSliubo 	/*
97acce952bSliubo 	 * today we only save the error info into ram.  Long term we'll
98acce952bSliubo 	 * also send it down to the disk
99acce952bSliubo 	 */
10087533c47SMiao Xie 	set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
101acce952bSliubo }
102acce952bSliubo 
103acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */
104acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
105acce952bSliubo {
106acce952bSliubo 	struct super_block *sb = fs_info->sb;
107acce952bSliubo 
108acce952bSliubo 	if (sb->s_flags & MS_RDONLY)
109acce952bSliubo 		return;
110acce952bSliubo 
11187533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
112acce952bSliubo 		sb->s_flags |= MS_RDONLY;
113c2cf52ebSSimon Kirby 		btrfs_info(fs_info, "forced readonly");
1141acd6831SStefan Behrens 		/*
1151acd6831SStefan Behrens 		 * Note that a running device replace operation is not
1161acd6831SStefan Behrens 		 * canceled here although there is no way to update
1171acd6831SStefan Behrens 		 * the progress. It would add the risk of a deadlock,
1181acd6831SStefan Behrens 		 * therefore the canceling is ommited. The only penalty
1191acd6831SStefan Behrens 		 * is that some I/O remains active until the procedure
1201acd6831SStefan Behrens 		 * completes. The next time when the filesystem is
1211acd6831SStefan Behrens 		 * mounted writeable again, the device replace
1221acd6831SStefan Behrens 		 * operation continues.
1231acd6831SStefan Behrens 		 */
124acce952bSliubo 	}
125acce952bSliubo }
126acce952bSliubo 
127533574c6SJoe Perches #ifdef CONFIG_PRINTK
128acce952bSliubo /*
129acce952bSliubo  * __btrfs_std_error decodes expected errors from the caller and
130acce952bSliubo  * invokes the approciate error response.
131acce952bSliubo  */
132acce952bSliubo void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
1334da35113SJeff Mahoney 		       unsigned int line, int errno, const char *fmt, ...)
134acce952bSliubo {
135acce952bSliubo 	struct super_block *sb = fs_info->sb;
136acce952bSliubo 	const char *errstr;
137acce952bSliubo 
138acce952bSliubo 	/*
139acce952bSliubo 	 * Special case: if the error is EROFS, and we're already
140acce952bSliubo 	 * under MS_RDONLY, then it is safe here.
141acce952bSliubo 	 */
142acce952bSliubo 	if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
143acce952bSliubo   		return;
144acce952bSliubo 
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 
15408748810SDavid Sterba 		printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: errno=%d %s (%pV)\n",
15508748810SDavid Sterba 			sb->s_id, function, line, errno, errstr, &vaf);
15637252a66SEric Sandeen 		va_end(args);
1574da35113SJeff Mahoney 	} else {
15808748810SDavid Sterba 		printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: errno=%d %s\n",
15908748810SDavid Sterba 			sb->s_id, function, line, errno, errstr);
1604da35113SJeff Mahoney 	}
161acce952bSliubo 
1624da35113SJeff Mahoney 	/* Don't go through full error handling during mount */
1634da35113SJeff Mahoney 	save_error_info(fs_info);
164cf79ffb5SJosef Bacik 	if (sb->s_flags & MS_BORN)
165acce952bSliubo 		btrfs_handle_error(fs_info);
166acce952bSliubo }
1674da35113SJeff Mahoney 
168533574c6SJoe Perches static const char * const logtypes[] = {
1694da35113SJeff Mahoney 	"emergency",
1704da35113SJeff Mahoney 	"alert",
1714da35113SJeff Mahoney 	"critical",
1724da35113SJeff Mahoney 	"error",
1734da35113SJeff Mahoney 	"warning",
1744da35113SJeff Mahoney 	"notice",
1754da35113SJeff Mahoney 	"info",
1764da35113SJeff Mahoney 	"debug",
1774da35113SJeff Mahoney };
1784da35113SJeff Mahoney 
179c2cf52ebSSimon Kirby void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
1804da35113SJeff Mahoney {
1814da35113SJeff Mahoney 	struct super_block *sb = fs_info->sb;
1824da35113SJeff Mahoney 	char lvl[4];
1834da35113SJeff Mahoney 	struct va_format vaf;
1844da35113SJeff Mahoney 	va_list args;
1854da35113SJeff Mahoney 	const char *type = logtypes[4];
186533574c6SJoe Perches 	int kern_level;
1874da35113SJeff Mahoney 
1884da35113SJeff Mahoney 	va_start(args, fmt);
1894da35113SJeff Mahoney 
190533574c6SJoe Perches 	kern_level = printk_get_level(fmt);
191533574c6SJoe Perches 	if (kern_level) {
192533574c6SJoe Perches 		size_t size = printk_skip_level(fmt) - fmt;
193533574c6SJoe Perches 		memcpy(lvl, fmt,  size);
194533574c6SJoe Perches 		lvl[size] = '\0';
195533574c6SJoe Perches 		fmt += size;
196533574c6SJoe Perches 		type = logtypes[kern_level - '0'];
1974da35113SJeff Mahoney 	} else
1984da35113SJeff Mahoney 		*lvl = '\0';
1994da35113SJeff Mahoney 
2004da35113SJeff Mahoney 	vaf.fmt = fmt;
2014da35113SJeff Mahoney 	vaf.va = &args;
202533574c6SJoe Perches 
203c2cf52ebSSimon Kirby 	printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf);
204533574c6SJoe Perches 
205533574c6SJoe Perches 	va_end(args);
2064da35113SJeff Mahoney }
207acce952bSliubo 
208533574c6SJoe Perches #else
209533574c6SJoe Perches 
210533574c6SJoe Perches void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
211533574c6SJoe Perches 		       unsigned int line, int errno, const char *fmt, ...)
212533574c6SJoe Perches {
213533574c6SJoe Perches 	struct super_block *sb = fs_info->sb;
214533574c6SJoe Perches 
215533574c6SJoe Perches 	/*
216533574c6SJoe Perches 	 * Special case: if the error is EROFS, and we're already
217533574c6SJoe Perches 	 * under MS_RDONLY, then it is safe here.
218533574c6SJoe Perches 	 */
219533574c6SJoe Perches 	if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
220533574c6SJoe Perches 		return;
221533574c6SJoe Perches 
222533574c6SJoe Perches 	/* Don't go through full error handling during mount */
223533574c6SJoe Perches 	if (sb->s_flags & MS_BORN) {
224533574c6SJoe Perches 		save_error_info(fs_info);
225533574c6SJoe Perches 		btrfs_handle_error(fs_info);
226533574c6SJoe Perches 	}
227533574c6SJoe Perches }
228533574c6SJoe Perches #endif
229533574c6SJoe Perches 
2308c342930SJeff Mahoney /*
23149b25e05SJeff Mahoney  * We only mark the transaction aborted and then set the file system read-only.
23249b25e05SJeff Mahoney  * This will prevent new transactions from starting or trying to join this
23349b25e05SJeff Mahoney  * one.
23449b25e05SJeff Mahoney  *
23549b25e05SJeff Mahoney  * This means that error recovery at the call site is limited to freeing
23649b25e05SJeff Mahoney  * any local memory allocations and passing the error code up without
23749b25e05SJeff Mahoney  * further cleanup. The transaction should complete as it normally would
23849b25e05SJeff Mahoney  * in the call path but will return -EIO.
23949b25e05SJeff Mahoney  *
24049b25e05SJeff Mahoney  * We'll complete the cleanup in btrfs_end_transaction and
24149b25e05SJeff Mahoney  * btrfs_commit_transaction.
24249b25e05SJeff Mahoney  */
24349b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
24449b25e05SJeff Mahoney 			       struct btrfs_root *root, const char *function,
24549b25e05SJeff Mahoney 			       unsigned int line, int errno)
24649b25e05SJeff Mahoney {
24708748810SDavid Sterba 	/*
24808748810SDavid Sterba 	 * Report first abort since mount
24908748810SDavid Sterba 	 */
25008748810SDavid Sterba 	if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED,
25108748810SDavid Sterba 				&root->fs_info->fs_state)) {
25208748810SDavid Sterba 		WARN(1, KERN_DEBUG "btrfs: Transaction aborted (error %d)\n",
25308748810SDavid Sterba 				errno);
25408748810SDavid Sterba 	}
25549b25e05SJeff Mahoney 	trans->aborted = errno;
25649b25e05SJeff Mahoney 	/* Nothing used. The other threads that have joined this
25749b25e05SJeff Mahoney 	 * transaction may be able to continue. */
25849b25e05SJeff Mahoney 	if (!trans->blocks_used) {
25969ce977aSMiao Xie 		const char *errstr;
26069ce977aSMiao Xie 
26108748810SDavid Sterba 		errstr = btrfs_decode_error(errno);
262c2cf52ebSSimon Kirby 		btrfs_warn(root->fs_info,
263c2cf52ebSSimon Kirby 		           "%s:%d: Aborting unused transaction(%s).",
26469ce977aSMiao Xie 		           function, line, errstr);
26549b25e05SJeff Mahoney 		return;
26649b25e05SJeff Mahoney 	}
2678d25a086SMiao Xie 	ACCESS_ONCE(trans->transaction->aborted) = errno;
268501407aaSJosef Bacik 	/* Wake up anybody who may be waiting on this transaction */
269501407aaSJosef Bacik 	wake_up(&root->fs_info->transaction_wait);
270501407aaSJosef Bacik 	wake_up(&root->fs_info->transaction_blocked_wait);
27149b25e05SJeff Mahoney 	__btrfs_std_error(root->fs_info, function, line, errno, NULL);
27249b25e05SJeff Mahoney }
27349b25e05SJeff Mahoney /*
2748c342930SJeff Mahoney  * __btrfs_panic decodes unexpected, fatal errors from the caller,
2758c342930SJeff Mahoney  * issues an alert, and either panics or BUGs, depending on mount options.
2768c342930SJeff Mahoney  */
2778c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
2788c342930SJeff Mahoney 		   unsigned int line, int errno, const char *fmt, ...)
2798c342930SJeff Mahoney {
2808c342930SJeff Mahoney 	char *s_id = "<unknown>";
2818c342930SJeff Mahoney 	const char *errstr;
2828c342930SJeff Mahoney 	struct va_format vaf = { .fmt = fmt };
2838c342930SJeff Mahoney 	va_list args;
2848c342930SJeff Mahoney 
2858c342930SJeff Mahoney 	if (fs_info)
2868c342930SJeff Mahoney 		s_id = fs_info->sb->s_id;
2878c342930SJeff Mahoney 
2888c342930SJeff Mahoney 	va_start(args, fmt);
2898c342930SJeff Mahoney 	vaf.va = &args;
2908c342930SJeff Mahoney 
29108748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
292aa43a17cSEric Sandeen 	if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR))
29308748810SDavid Sterba 		panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
29408748810SDavid Sterba 			s_id, function, line, &vaf, errno, errstr);
2958c342930SJeff Mahoney 
29608748810SDavid Sterba 	printk(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
29708748810SDavid Sterba 	       s_id, function, line, &vaf, errno, errstr);
2988c342930SJeff Mahoney 	va_end(args);
2998c342930SJeff Mahoney 	/* Caller calls BUG() */
3008c342930SJeff Mahoney }
301e20d96d6SChris Mason 
302e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
303e20d96d6SChris Mason {
304815745cfSAl Viro 	(void)close_ctree(btrfs_sb(sb)->tree_root);
305aea52e19SAl Viro 	/* FIXME: need to fix VFS to return error? */
306aea52e19SAl Viro 	/* AV: return it _where_?  ->put_super() can be triggered by any number
307aea52e19SAl Viro 	 * of async events, up to and including delivery of SIGKILL to the
308aea52e19SAl Viro 	 * last process that kept it busy.  Or segfault in the aforementioned
309aea52e19SAl Viro 	 * process...  Whom would you report that to?
310aea52e19SAl Viro 	 */
311e20d96d6SChris Mason }
3122e635a27SChris Mason 
31395e05289SChris Mason enum {
31473f73415SJosef Bacik 	Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
315287a0ab9SJosef Bacik 	Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
316287a0ab9SJosef Bacik 	Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
317261507a0SLi Zefan 	Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
318261507a0SLi Zefan 	Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
31991435650SChris Mason 	Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed,
3209555c6c1SIlya Dryomov 	Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache,
3219555c6c1SIlya Dryomov 	Opt_no_space_cache, Opt_recovery, Opt_skip_balance,
32221adbd5cSStefan Behrens 	Opt_check_integrity, Opt_check_integrity_including_extent_data,
3238c342930SJeff Mahoney 	Opt_check_integrity_print_mask, Opt_fatal_errors,
324*8b87dc17SDavid Sterba 	Opt_commit_interval,
3259555c6c1SIlya Dryomov 	Opt_err,
32695e05289SChris Mason };
32795e05289SChris Mason 
32895e05289SChris Mason static match_table_t tokens = {
329dfe25020SChris Mason 	{Opt_degraded, "degraded"},
33095e05289SChris Mason 	{Opt_subvol, "subvol=%s"},
3311493381fSWang Shilong 	{Opt_subvolid, "subvolid=%s"},
33243e570b0SChristoph Hellwig 	{Opt_device, "device=%s"},
333b6cda9bcSChris Mason 	{Opt_nodatasum, "nodatasum"},
334be20aa9dSChris Mason 	{Opt_nodatacow, "nodatacow"},
33521ad10cfSChris Mason 	{Opt_nobarrier, "nobarrier"},
3366f568d35SChris Mason 	{Opt_max_inline, "max_inline=%s"},
3378f662a76SChris Mason 	{Opt_alloc_start, "alloc_start=%s"},
3384543df7eSChris Mason 	{Opt_thread_pool, "thread_pool=%d"},
339c8b97818SChris Mason 	{Opt_compress, "compress"},
340261507a0SLi Zefan 	{Opt_compress_type, "compress=%s"},
341a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
342261507a0SLi Zefan 	{Opt_compress_force_type, "compress-force=%s"},
343e18e4809SChris Mason 	{Opt_ssd, "ssd"},
344451d7585SChris Mason 	{Opt_ssd_spread, "ssd_spread"},
3453b30c22fSChris Mason 	{Opt_nossd, "nossd"},
34633268eafSJosef Bacik 	{Opt_noacl, "noacl"},
3473a5e1404SSage Weil 	{Opt_notreelog, "notreelog"},
348dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
34997e728d4SJosef Bacik 	{Opt_ratio, "metadata_ratio=%d"},
350e244a0aeSChristoph Hellwig 	{Opt_discard, "discard"},
3510af3d00bSJosef Bacik 	{Opt_space_cache, "space_cache"},
35288c2ba3bSJosef Bacik 	{Opt_clear_cache, "clear_cache"},
3534260f7c7SSage Weil 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
35491435650SChris Mason 	{Opt_enospc_debug, "enospc_debug"},
355e15d0542SXin Zhong 	{Opt_subvolrootid, "subvolrootid=%d"},
3564cb5300bSChris Mason 	{Opt_defrag, "autodefrag"},
3574b9465cbSChris Mason 	{Opt_inode_cache, "inode_cache"},
3588965593eSDavid Sterba 	{Opt_no_space_cache, "nospace_cache"},
359af31f5e5SChris Mason 	{Opt_recovery, "recovery"},
3609555c6c1SIlya Dryomov 	{Opt_skip_balance, "skip_balance"},
36121adbd5cSStefan Behrens 	{Opt_check_integrity, "check_int"},
36221adbd5cSStefan Behrens 	{Opt_check_integrity_including_extent_data, "check_int_data"},
36321adbd5cSStefan Behrens 	{Opt_check_integrity_print_mask, "check_int_print_mask=%d"},
3648c342930SJeff Mahoney 	{Opt_fatal_errors, "fatal_errors=%s"},
365*8b87dc17SDavid Sterba 	{Opt_commit_interval, "commit=%d"},
36633268eafSJosef Bacik 	{Opt_err, NULL},
36795e05289SChris Mason };
36895e05289SChris Mason 
369edf24abeSChristoph Hellwig /*
370edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
371edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
37249b25e05SJeff Mahoney  * XXX JDM: This needs to be cleaned up for remount.
373edf24abeSChristoph Hellwig  */
374edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options)
37595e05289SChris Mason {
376edf24abeSChristoph Hellwig 	struct btrfs_fs_info *info = root->fs_info;
37795e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
37873bc1876SJosef Bacik 	char *p, *num, *orig = NULL;
37973bc1876SJosef Bacik 	u64 cache_gen;
3804543df7eSChris Mason 	int intarg;
381a7a3f7caSSage Weil 	int ret = 0;
382261507a0SLi Zefan 	char *compress_type;
383261507a0SLi Zefan 	bool compress_force = false;
384b6cda9bcSChris Mason 
3856c41761fSDavid Sterba 	cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
38673bc1876SJosef Bacik 	if (cache_gen)
38773bc1876SJosef Bacik 		btrfs_set_opt(info->mount_opt, SPACE_CACHE);
38873bc1876SJosef Bacik 
38995e05289SChris Mason 	if (!options)
39073bc1876SJosef Bacik 		goto out;
39195e05289SChris Mason 
392be20aa9dSChris Mason 	/*
393be20aa9dSChris Mason 	 * strsep changes the string, duplicate it because parse_options
394be20aa9dSChris Mason 	 * gets called twice
395be20aa9dSChris Mason 	 */
396be20aa9dSChris Mason 	options = kstrdup(options, GFP_NOFS);
397be20aa9dSChris Mason 	if (!options)
398be20aa9dSChris Mason 		return -ENOMEM;
399be20aa9dSChris Mason 
400da495eccSJosef Bacik 	orig = options;
401be20aa9dSChris Mason 
40295e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
40395e05289SChris Mason 		int token;
40495e05289SChris Mason 		if (!*p)
40595e05289SChris Mason 			continue;
40695e05289SChris Mason 
40795e05289SChris Mason 		token = match_token(p, tokens, args);
40895e05289SChris Mason 		switch (token) {
409dfe25020SChris Mason 		case Opt_degraded:
410edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: allowing degraded mounts\n");
411dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
412dfe25020SChris Mason 			break;
41395e05289SChris Mason 		case Opt_subvol:
41473f73415SJosef Bacik 		case Opt_subvolid:
415e15d0542SXin Zhong 		case Opt_subvolrootid:
41643e570b0SChristoph Hellwig 		case Opt_device:
417edf24abeSChristoph Hellwig 			/*
41843e570b0SChristoph Hellwig 			 * These are parsed by btrfs_parse_early_options
419edf24abeSChristoph Hellwig 			 * and can be happily ignored here.
420edf24abeSChristoph Hellwig 			 */
42195e05289SChris Mason 			break;
422b6cda9bcSChris Mason 		case Opt_nodatasum:
423067c28adSChris Mason 			printk(KERN_INFO "btrfs: setting nodatasum\n");
424b6cda9bcSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
425be20aa9dSChris Mason 			break;
426be20aa9dSChris Mason 		case Opt_nodatacow:
427bedb2ccaSAndrei Popa 			if (!btrfs_test_opt(root, COMPRESS) ||
428bedb2ccaSAndrei Popa 				!btrfs_test_opt(root, FORCE_COMPRESS)) {
429bedb2ccaSAndrei Popa 					printk(KERN_INFO "btrfs: setting nodatacow, compression disabled\n");
430bedb2ccaSAndrei Popa 			} else {
431edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: setting nodatacow\n");
432bedb2ccaSAndrei Popa 			}
433bedb2ccaSAndrei Popa 			info->compress_type = BTRFS_COMPRESS_NONE;
434bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, COMPRESS);
435bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
436be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
437be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
438b6cda9bcSChris Mason 			break;
439a555f810SChris Mason 		case Opt_compress_force:
440261507a0SLi Zefan 		case Opt_compress_force_type:
441261507a0SLi Zefan 			compress_force = true;
4421c697d4aSEric Sandeen 			/* Fallthrough */
443261507a0SLi Zefan 		case Opt_compress:
444261507a0SLi Zefan 		case Opt_compress_type:
445261507a0SLi Zefan 			if (token == Opt_compress ||
446261507a0SLi Zefan 			    token == Opt_compress_force ||
447261507a0SLi Zefan 			    strcmp(args[0].from, "zlib") == 0) {
448261507a0SLi Zefan 				compress_type = "zlib";
449261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
450063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
451bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
452bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
453a6fa6faeSLi Zefan 			} else if (strcmp(args[0].from, "lzo") == 0) {
454a6fa6faeSLi Zefan 				compress_type = "lzo";
455a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
456063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
457bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
458bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
4592b0ce2c2SMitch Harder 				btrfs_set_fs_incompat(info, COMPRESS_LZO);
460063849eaSArnd Hannemann 			} else if (strncmp(args[0].from, "no", 2) == 0) {
461063849eaSArnd Hannemann 				compress_type = "no";
462063849eaSArnd Hannemann 				info->compress_type = BTRFS_COMPRESS_NONE;
463063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, COMPRESS);
464063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
465063849eaSArnd Hannemann 				compress_force = false;
466261507a0SLi Zefan 			} else {
467261507a0SLi Zefan 				ret = -EINVAL;
468261507a0SLi Zefan 				goto out;
469261507a0SLi Zefan 			}
470261507a0SLi Zefan 
471261507a0SLi Zefan 			if (compress_force) {
472261507a0SLi Zefan 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
473261507a0SLi Zefan 				pr_info("btrfs: force %s compression\n",
474261507a0SLi Zefan 					compress_type);
475261507a0SLi Zefan 			} else
476261507a0SLi Zefan 				pr_info("btrfs: use %s compression\n",
477261507a0SLi Zefan 					compress_type);
478a555f810SChris Mason 			break;
479e18e4809SChris Mason 		case Opt_ssd:
480edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
481e18e4809SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
482e18e4809SChris Mason 			break;
483451d7585SChris Mason 		case Opt_ssd_spread:
484451d7585SChris Mason 			printk(KERN_INFO "btrfs: use spread ssd "
485451d7585SChris Mason 			       "allocation scheme\n");
486451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
487451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD_SPREAD);
488451d7585SChris Mason 			break;
4893b30c22fSChris Mason 		case Opt_nossd:
490451d7585SChris Mason 			printk(KERN_INFO "btrfs: not using ssd allocation "
491451d7585SChris Mason 			       "scheme\n");
492c289811cSChris Mason 			btrfs_set_opt(info->mount_opt, NOSSD);
4933b30c22fSChris Mason 			btrfs_clear_opt(info->mount_opt, SSD);
494451d7585SChris Mason 			btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
4953b30c22fSChris Mason 			break;
49621ad10cfSChris Mason 		case Opt_nobarrier:
497edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: turning off barriers\n");
49821ad10cfSChris Mason 			btrfs_set_opt(info->mount_opt, NOBARRIER);
49921ad10cfSChris Mason 			break;
5004543df7eSChris Mason 		case Opt_thread_pool:
5012c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
5022c334e87SWang Shilong 			if (ret) {
5032c334e87SWang Shilong 				goto out;
5042c334e87SWang Shilong 			} else if (intarg > 0) {
5054543df7eSChris Mason 				info->thread_pool_size = intarg;
5062c334e87SWang Shilong 			} else {
5072c334e87SWang Shilong 				ret = -EINVAL;
5082c334e87SWang Shilong 				goto out;
5092c334e87SWang Shilong 			}
5104543df7eSChris Mason 			break;
5116f568d35SChris Mason 		case Opt_max_inline:
512edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
5136f568d35SChris Mason 			if (num) {
51491748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
5156f568d35SChris Mason 				kfree(num);
5166f568d35SChris Mason 
51715ada040SChris Mason 				if (info->max_inline) {
5186f568d35SChris Mason 					info->max_inline = max_t(u64,
51915ada040SChris Mason 						info->max_inline,
52015ada040SChris Mason 						root->sectorsize);
52115ada040SChris Mason 				}
522edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: max_inline at %llu\n",
52321380931SJoel Becker 					(unsigned long long)info->max_inline);
5242c334e87SWang Shilong 			} else {
5252c334e87SWang Shilong 				ret = -ENOMEM;
5262c334e87SWang Shilong 				goto out;
5276f568d35SChris Mason 			}
5286f568d35SChris Mason 			break;
5298f662a76SChris Mason 		case Opt_alloc_start:
530edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
5318f662a76SChris Mason 			if (num) {
532c018daecSMiao Xie 				mutex_lock(&info->chunk_mutex);
53391748467SAkinobu Mita 				info->alloc_start = memparse(num, NULL);
534c018daecSMiao Xie 				mutex_unlock(&info->chunk_mutex);
5358f662a76SChris Mason 				kfree(num);
536edf24abeSChristoph Hellwig 				printk(KERN_INFO
537edf24abeSChristoph Hellwig 					"btrfs: allocations start at %llu\n",
53821380931SJoel Becker 					(unsigned long long)info->alloc_start);
5392c334e87SWang Shilong 			} else {
5402c334e87SWang Shilong 				ret = -ENOMEM;
5412c334e87SWang Shilong 				goto out;
5428f662a76SChris Mason 			}
5438f662a76SChris Mason 			break;
54433268eafSJosef Bacik 		case Opt_noacl:
54533268eafSJosef Bacik 			root->fs_info->sb->s_flags &= ~MS_POSIXACL;
54633268eafSJosef Bacik 			break;
5473a5e1404SSage Weil 		case Opt_notreelog:
5483a5e1404SSage Weil 			printk(KERN_INFO "btrfs: disabling tree log\n");
5493a5e1404SSage Weil 			btrfs_set_opt(info->mount_opt, NOTREELOG);
5503a5e1404SSage Weil 			break;
551dccae999SSage Weil 		case Opt_flushoncommit:
552dccae999SSage Weil 			printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
553dccae999SSage Weil 			btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
554dccae999SSage Weil 			break;
55597e728d4SJosef Bacik 		case Opt_ratio:
5562c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
5572c334e87SWang Shilong 			if (ret) {
5582c334e87SWang Shilong 				goto out;
5592c334e87SWang Shilong 			} else if (intarg >= 0) {
56097e728d4SJosef Bacik 				info->metadata_ratio = intarg;
56197e728d4SJosef Bacik 				printk(KERN_INFO "btrfs: metadata ratio %d\n",
56297e728d4SJosef Bacik 				       info->metadata_ratio);
5632c334e87SWang Shilong 			} else {
5642c334e87SWang Shilong 				ret = -EINVAL;
5652c334e87SWang Shilong 				goto out;
56697e728d4SJosef Bacik 			}
56797e728d4SJosef Bacik 			break;
568e244a0aeSChristoph Hellwig 		case Opt_discard:
569e244a0aeSChristoph Hellwig 			btrfs_set_opt(info->mount_opt, DISCARD);
570e244a0aeSChristoph Hellwig 			break;
5710af3d00bSJosef Bacik 		case Opt_space_cache:
5720af3d00bSJosef Bacik 			btrfs_set_opt(info->mount_opt, SPACE_CACHE);
5730de90876SJosef Bacik 			break;
57473bc1876SJosef Bacik 		case Opt_no_space_cache:
57573bc1876SJosef Bacik 			printk(KERN_INFO "btrfs: disabling disk space caching\n");
57673bc1876SJosef Bacik 			btrfs_clear_opt(info->mount_opt, SPACE_CACHE);
57773bc1876SJosef Bacik 			break;
5784b9465cbSChris Mason 		case Opt_inode_cache:
5794b9465cbSChris Mason 			printk(KERN_INFO "btrfs: enabling inode map caching\n");
5804b9465cbSChris Mason 			btrfs_set_opt(info->mount_opt, INODE_MAP_CACHE);
5814b9465cbSChris Mason 			break;
58288c2ba3bSJosef Bacik 		case Opt_clear_cache:
58388c2ba3bSJosef Bacik 			printk(KERN_INFO "btrfs: force clearing of disk cache\n");
58488c2ba3bSJosef Bacik 			btrfs_set_opt(info->mount_opt, CLEAR_CACHE);
5850af3d00bSJosef Bacik 			break;
5864260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
5874260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
5884260f7c7SSage Weil 			break;
58991435650SChris Mason 		case Opt_enospc_debug:
59091435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
59191435650SChris Mason 			break;
5924cb5300bSChris Mason 		case Opt_defrag:
59348940662SDaniel J Blueman 			printk(KERN_INFO "btrfs: enabling auto defrag\n");
5944cb5300bSChris Mason 			btrfs_set_opt(info->mount_opt, AUTO_DEFRAG);
5954cb5300bSChris Mason 			break;
596af31f5e5SChris Mason 		case Opt_recovery:
59748940662SDaniel J Blueman 			printk(KERN_INFO "btrfs: enabling auto recovery\n");
598af31f5e5SChris Mason 			btrfs_set_opt(info->mount_opt, RECOVERY);
599af31f5e5SChris Mason 			break;
6009555c6c1SIlya Dryomov 		case Opt_skip_balance:
6019555c6c1SIlya Dryomov 			btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
6029555c6c1SIlya Dryomov 			break;
60321adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
60421adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
60521adbd5cSStefan Behrens 			printk(KERN_INFO "btrfs: enabling check integrity"
60621adbd5cSStefan Behrens 			       " including extent data\n");
60721adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt,
60821adbd5cSStefan Behrens 				      CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
60921adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
61021adbd5cSStefan Behrens 			break;
61121adbd5cSStefan Behrens 		case Opt_check_integrity:
61221adbd5cSStefan Behrens 			printk(KERN_INFO "btrfs: enabling check integrity\n");
61321adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
61421adbd5cSStefan Behrens 			break;
61521adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
6162c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
6172c334e87SWang Shilong 			if (ret) {
6182c334e87SWang Shilong 				goto out;
6192c334e87SWang Shilong 			} else if (intarg >= 0) {
62021adbd5cSStefan Behrens 				info->check_integrity_print_mask = intarg;
62121adbd5cSStefan Behrens 				printk(KERN_INFO "btrfs:"
62221adbd5cSStefan Behrens 				       " check_integrity_print_mask 0x%x\n",
62321adbd5cSStefan Behrens 				       info->check_integrity_print_mask);
6242c334e87SWang Shilong 			} else {
6252c334e87SWang Shilong 				ret = -EINVAL;
6262c334e87SWang Shilong 				goto out;
62721adbd5cSStefan Behrens 			}
62821adbd5cSStefan Behrens 			break;
62921adbd5cSStefan Behrens #else
63021adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
63121adbd5cSStefan Behrens 		case Opt_check_integrity:
63221adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
63321adbd5cSStefan Behrens 			printk(KERN_ERR "btrfs: support for check_integrity*"
63421adbd5cSStefan Behrens 			       " not compiled in!\n");
63521adbd5cSStefan Behrens 			ret = -EINVAL;
63621adbd5cSStefan Behrens 			goto out;
63721adbd5cSStefan Behrens #endif
6388c342930SJeff Mahoney 		case Opt_fatal_errors:
6398c342930SJeff Mahoney 			if (strcmp(args[0].from, "panic") == 0)
6408c342930SJeff Mahoney 				btrfs_set_opt(info->mount_opt,
6418c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
6428c342930SJeff Mahoney 			else if (strcmp(args[0].from, "bug") == 0)
6438c342930SJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
6448c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
6458c342930SJeff Mahoney 			else {
6468c342930SJeff Mahoney 				ret = -EINVAL;
6478c342930SJeff Mahoney 				goto out;
6488c342930SJeff Mahoney 			}
6498c342930SJeff Mahoney 			break;
650*8b87dc17SDavid Sterba 		case Opt_commit_interval:
651*8b87dc17SDavid Sterba 			intarg = 0;
652*8b87dc17SDavid Sterba 			ret = match_int(&args[0], &intarg);
653*8b87dc17SDavid Sterba 			if (ret < 0) {
654*8b87dc17SDavid Sterba 				printk(KERN_ERR
655*8b87dc17SDavid Sterba 					"btrfs: invalid commit interval\n");
656*8b87dc17SDavid Sterba 				ret = -EINVAL;
657*8b87dc17SDavid Sterba 				goto out;
658*8b87dc17SDavid Sterba 			}
659*8b87dc17SDavid Sterba 			if (intarg > 0) {
660*8b87dc17SDavid Sterba 				if (intarg > 300) {
661*8b87dc17SDavid Sterba 					printk(KERN_WARNING
662*8b87dc17SDavid Sterba 					    "btrfs: excessive commit interval %d\n",
663*8b87dc17SDavid Sterba 							intarg);
664*8b87dc17SDavid Sterba 				}
665*8b87dc17SDavid Sterba 				info->commit_interval = intarg;
666*8b87dc17SDavid Sterba 			} else {
667*8b87dc17SDavid Sterba 				printk(KERN_INFO
668*8b87dc17SDavid Sterba 				    "btrfs: using default commit interval %ds\n",
669*8b87dc17SDavid Sterba 				    BTRFS_DEFAULT_COMMIT_INTERVAL);
670*8b87dc17SDavid Sterba 				info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
671*8b87dc17SDavid Sterba 			}
672*8b87dc17SDavid Sterba 			break;
673a7a3f7caSSage Weil 		case Opt_err:
674a7a3f7caSSage Weil 			printk(KERN_INFO "btrfs: unrecognized mount option "
675a7a3f7caSSage Weil 			       "'%s'\n", p);
676a7a3f7caSSage Weil 			ret = -EINVAL;
677a7a3f7caSSage Weil 			goto out;
67895e05289SChris Mason 		default:
679be20aa9dSChris Mason 			break;
68095e05289SChris Mason 		}
68195e05289SChris Mason 	}
682a7a3f7caSSage Weil out:
68373bc1876SJosef Bacik 	if (!ret && btrfs_test_opt(root, SPACE_CACHE))
68473bc1876SJosef Bacik 		printk(KERN_INFO "btrfs: disk space caching is enabled\n");
685da495eccSJosef Bacik 	kfree(orig);
686a7a3f7caSSage Weil 	return ret;
687edf24abeSChristoph Hellwig }
688edf24abeSChristoph Hellwig 
689edf24abeSChristoph Hellwig /*
690edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
691edf24abeSChristoph Hellwig  *
692edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
693edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
694edf24abeSChristoph Hellwig  */
69597288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags,
69673f73415SJosef Bacik 		void *holder, char **subvol_name, u64 *subvol_objectid,
6975e2a4b25SDavid Sterba 		struct btrfs_fs_devices **fs_devices)
698edf24abeSChristoph Hellwig {
699edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
70083c8c9bdSJeff Liu 	char *device_name, *opts, *orig, *p;
7011493381fSWang Shilong 	char *num = NULL;
702edf24abeSChristoph Hellwig 	int error = 0;
703edf24abeSChristoph Hellwig 
704edf24abeSChristoph Hellwig 	if (!options)
705830c4adbSJosef Bacik 		return 0;
706edf24abeSChristoph Hellwig 
707edf24abeSChristoph Hellwig 	/*
708edf24abeSChristoph Hellwig 	 * strsep changes the string, duplicate it because parse_options
709edf24abeSChristoph Hellwig 	 * gets called twice
710edf24abeSChristoph Hellwig 	 */
711edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
712edf24abeSChristoph Hellwig 	if (!opts)
713edf24abeSChristoph Hellwig 		return -ENOMEM;
7143f3d0bc0STero Roponen 	orig = opts;
715edf24abeSChristoph Hellwig 
716edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
717edf24abeSChristoph Hellwig 		int token;
718edf24abeSChristoph Hellwig 		if (!*p)
719edf24abeSChristoph Hellwig 			continue;
720edf24abeSChristoph Hellwig 
721edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
722edf24abeSChristoph Hellwig 		switch (token) {
723edf24abeSChristoph Hellwig 		case Opt_subvol:
724a90e8b6fSIlya Dryomov 			kfree(*subvol_name);
725edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
7262c334e87SWang Shilong 			if (!*subvol_name) {
7272c334e87SWang Shilong 				error = -ENOMEM;
7282c334e87SWang Shilong 				goto out;
7292c334e87SWang Shilong 			}
730edf24abeSChristoph Hellwig 			break;
73173f73415SJosef Bacik 		case Opt_subvolid:
7321493381fSWang Shilong 			num = match_strdup(&args[0]);
7331493381fSWang Shilong 			if (num) {
7341493381fSWang Shilong 				*subvol_objectid = memparse(num, NULL);
7351493381fSWang Shilong 				kfree(num);
7364849f01dSJosef Bacik 				/* we want the original fs_tree */
7371493381fSWang Shilong 				if (!*subvol_objectid)
7384849f01dSJosef Bacik 					*subvol_objectid =
7394849f01dSJosef Bacik 						BTRFS_FS_TREE_OBJECTID;
7402c334e87SWang Shilong 			} else {
7412c334e87SWang Shilong 				error = -EINVAL;
7422c334e87SWang Shilong 				goto out;
7434849f01dSJosef Bacik 			}
74473f73415SJosef Bacik 			break;
745e15d0542SXin Zhong 		case Opt_subvolrootid:
7465e2a4b25SDavid Sterba 			printk(KERN_WARNING
7475e2a4b25SDavid Sterba 				"btrfs: 'subvolrootid' mount option is deprecated and has no effect\n");
748e15d0542SXin Zhong 			break;
74943e570b0SChristoph Hellwig 		case Opt_device:
75083c8c9bdSJeff Liu 			device_name = match_strdup(&args[0]);
75183c8c9bdSJeff Liu 			if (!device_name) {
75283c8c9bdSJeff Liu 				error = -ENOMEM;
75383c8c9bdSJeff Liu 				goto out;
75483c8c9bdSJeff Liu 			}
75583c8c9bdSJeff Liu 			error = btrfs_scan_one_device(device_name,
75643e570b0SChristoph Hellwig 					flags, holder, fs_devices);
75783c8c9bdSJeff Liu 			kfree(device_name);
75843e570b0SChristoph Hellwig 			if (error)
759830c4adbSJosef Bacik 				goto out;
76043e570b0SChristoph Hellwig 			break;
761edf24abeSChristoph Hellwig 		default:
762edf24abeSChristoph Hellwig 			break;
763edf24abeSChristoph Hellwig 		}
764edf24abeSChristoph Hellwig 	}
765edf24abeSChristoph Hellwig 
766edf24abeSChristoph Hellwig out:
767830c4adbSJosef Bacik 	kfree(orig);
768edf24abeSChristoph Hellwig 	return error;
76995e05289SChris Mason }
77095e05289SChris Mason 
77173f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb,
77273f73415SJosef Bacik 				       u64 subvol_objectid)
77373f73415SJosef Bacik {
774815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
775815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
77673f73415SJosef Bacik 	struct btrfs_root *new_root;
77773f73415SJosef Bacik 	struct btrfs_dir_item *di;
77873f73415SJosef Bacik 	struct btrfs_path *path;
77973f73415SJosef Bacik 	struct btrfs_key location;
78073f73415SJosef Bacik 	struct inode *inode;
78173f73415SJosef Bacik 	u64 dir_id;
78273f73415SJosef Bacik 	int new = 0;
78373f73415SJosef Bacik 
78473f73415SJosef Bacik 	/*
78573f73415SJosef Bacik 	 * We have a specific subvol we want to mount, just setup location and
78673f73415SJosef Bacik 	 * go look up the root.
78773f73415SJosef Bacik 	 */
78873f73415SJosef Bacik 	if (subvol_objectid) {
78973f73415SJosef Bacik 		location.objectid = subvol_objectid;
79073f73415SJosef Bacik 		location.type = BTRFS_ROOT_ITEM_KEY;
79173f73415SJosef Bacik 		location.offset = (u64)-1;
79273f73415SJosef Bacik 		goto find_root;
79373f73415SJosef Bacik 	}
79473f73415SJosef Bacik 
79573f73415SJosef Bacik 	path = btrfs_alloc_path();
79673f73415SJosef Bacik 	if (!path)
79773f73415SJosef Bacik 		return ERR_PTR(-ENOMEM);
79873f73415SJosef Bacik 	path->leave_spinning = 1;
79973f73415SJosef Bacik 
80073f73415SJosef Bacik 	/*
80173f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
80273f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
80373f73415SJosef Bacik 	 * to mount.
80473f73415SJosef Bacik 	 */
805815745cfSAl Viro 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
80673f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
807b0839166SJulia Lawall 	if (IS_ERR(di)) {
808b0839166SJulia Lawall 		btrfs_free_path(path);
809fb4f6f91SDan Carpenter 		return ERR_CAST(di);
810b0839166SJulia Lawall 	}
81173f73415SJosef Bacik 	if (!di) {
81273f73415SJosef Bacik 		/*
81373f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
81473f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
81573f73415SJosef Bacik 		 * mount to root most subvolume.
81673f73415SJosef Bacik 		 */
81773f73415SJosef Bacik 		btrfs_free_path(path);
81873f73415SJosef Bacik 		dir_id = BTRFS_FIRST_FREE_OBJECTID;
819815745cfSAl Viro 		new_root = fs_info->fs_root;
82073f73415SJosef Bacik 		goto setup_root;
82173f73415SJosef Bacik 	}
82273f73415SJosef Bacik 
82373f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
82473f73415SJosef Bacik 	btrfs_free_path(path);
82573f73415SJosef Bacik 
82673f73415SJosef Bacik find_root:
827815745cfSAl Viro 	new_root = btrfs_read_fs_root_no_name(fs_info, &location);
82873f73415SJosef Bacik 	if (IS_ERR(new_root))
829d0b678cbSJulia Lawall 		return ERR_CAST(new_root);
83073f73415SJosef Bacik 
83173f73415SJosef Bacik 	dir_id = btrfs_root_dirid(&new_root->root_item);
83273f73415SJosef Bacik setup_root:
83373f73415SJosef Bacik 	location.objectid = dir_id;
83473f73415SJosef Bacik 	location.type = BTRFS_INODE_ITEM_KEY;
83573f73415SJosef Bacik 	location.offset = 0;
83673f73415SJosef Bacik 
83773f73415SJosef Bacik 	inode = btrfs_iget(sb, &location, new_root, &new);
8384cbd1149SDan Carpenter 	if (IS_ERR(inode))
8394cbd1149SDan Carpenter 		return ERR_CAST(inode);
84073f73415SJosef Bacik 
84173f73415SJosef Bacik 	/*
84273f73415SJosef Bacik 	 * If we're just mounting the root most subvol put the inode and return
84373f73415SJosef Bacik 	 * a reference to the dentry.  We will have already gotten a reference
84473f73415SJosef Bacik 	 * to the inode in btrfs_fill_super so we're good to go.
84573f73415SJosef Bacik 	 */
84673f73415SJosef Bacik 	if (!new && sb->s_root->d_inode == inode) {
84773f73415SJosef Bacik 		iput(inode);
84873f73415SJosef Bacik 		return dget(sb->s_root);
84973f73415SJosef Bacik 	}
85073f73415SJosef Bacik 
851ba5b8958SJosef Bacik 	return d_obtain_alias(inode);
85273f73415SJosef Bacik }
85373f73415SJosef Bacik 
8548a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
8558a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
8568a4b83ccSChris Mason 			    void *data, int silent)
8572e635a27SChris Mason {
8582e635a27SChris Mason 	struct inode *inode;
859815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
8605d4f98a2SYan Zheng 	struct btrfs_key key;
86139279cc3SChris Mason 	int err;
8622e635a27SChris Mason 
8632e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
8642e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
865e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
866af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
867be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
8685103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
8692e635a27SChris Mason 	sb->s_time_gran = 1;
8700eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
87133268eafSJosef Bacik 	sb->s_flags |= MS_POSIXACL;
87249cf6f45SChris Ball #endif
8730c4d2d95SJosef Bacik 	sb->s_flags |= MS_I_VERSION;
874ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
875ad2b2c80SAl Viro 	if (err) {
876e20d96d6SChris Mason 		printk("btrfs: open_ctree failed\n");
877ad2b2c80SAl Viro 		return err;
878e20d96d6SChris Mason 	}
879b888db2bSChris Mason 
8805d4f98a2SYan Zheng 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
8815d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
8825d4f98a2SYan Zheng 	key.offset = 0;
88398c7089cSAl Viro 	inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
8845d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
8855d4f98a2SYan Zheng 		err = PTR_ERR(inode);
88639279cc3SChris Mason 		goto fail_close;
88739279cc3SChris Mason 	}
8882e635a27SChris Mason 
88948fde701SAl Viro 	sb->s_root = d_make_root(inode);
89048fde701SAl Viro 	if (!sb->s_root) {
89139279cc3SChris Mason 		err = -ENOMEM;
89239279cc3SChris Mason 		goto fail_close;
8932e635a27SChris Mason 	}
89458176a96SJosef Bacik 
8956885f308SChris Mason 	save_mount_options(sb, data);
89690a887c9SDan Magenheimer 	cleancache_init_fs(sb);
89759553edfSAl Viro 	sb->s_flags |= MS_ACTIVE;
8982e635a27SChris Mason 	return 0;
8992e635a27SChris Mason 
90039279cc3SChris Mason fail_close:
901815745cfSAl Viro 	close_ctree(fs_info->tree_root);
902d5719762SChris Mason 	return err;
903d5719762SChris Mason }
904d5719762SChris Mason 
9056bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
906d5719762SChris Mason {
907d5719762SChris Mason 	struct btrfs_trans_handle *trans;
908815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
909815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
910df2ce34cSChris Mason 
9111abe9b8aSliubo 	trace_btrfs_sync_fs(wait);
9121abe9b8aSliubo 
913d561c025SChris Mason 	if (!wait) {
914815745cfSAl Viro 		filemap_flush(fs_info->btree_inode->i_mapping);
915df2ce34cSChris Mason 		return 0;
916d561c025SChris Mason 	}
917771ed689SChris Mason 
918c73e2936SJosef Bacik 	btrfs_wait_all_ordered_extents(fs_info, 1);
919771ed689SChris Mason 
920d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
92160376ce4SJosef Bacik 	if (IS_ERR(trans)) {
922354aa0fbSMiao Xie 		/* no transaction, don't bother */
923354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
924bd7de2c9SJosef Bacik 			return 0;
92598d5dc13STsutomu Itoh 		return PTR_ERR(trans);
92660376ce4SJosef Bacik 	}
927bd7de2c9SJosef Bacik 	return btrfs_commit_transaction(trans, root);
928d5719762SChris Mason }
929d5719762SChris Mason 
93034c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
931a9572a15SEric Paris {
932815745cfSAl Viro 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
933815745cfSAl Viro 	struct btrfs_root *root = info->tree_root;
934200da64eSTsutomu Itoh 	char *compress_type;
935a9572a15SEric Paris 
936a9572a15SEric Paris 	if (btrfs_test_opt(root, DEGRADED))
937a9572a15SEric Paris 		seq_puts(seq, ",degraded");
938a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATASUM))
939a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
940a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATACOW))
941a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
942a9572a15SEric Paris 	if (btrfs_test_opt(root, NOBARRIER))
943a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
944a9572a15SEric Paris 	if (info->max_inline != 8192 * 1024)
94521380931SJoel Becker 		seq_printf(seq, ",max_inline=%llu",
94621380931SJoel Becker 			   (unsigned long long)info->max_inline);
947a9572a15SEric Paris 	if (info->alloc_start != 0)
94821380931SJoel Becker 		seq_printf(seq, ",alloc_start=%llu",
94921380931SJoel Becker 			   (unsigned long long)info->alloc_start);
950a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
951a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
952a9572a15SEric Paris 		seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
953200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, COMPRESS)) {
954200da64eSTsutomu Itoh 		if (info->compress_type == BTRFS_COMPRESS_ZLIB)
955200da64eSTsutomu Itoh 			compress_type = "zlib";
956200da64eSTsutomu Itoh 		else
957200da64eSTsutomu Itoh 			compress_type = "lzo";
958200da64eSTsutomu Itoh 		if (btrfs_test_opt(root, FORCE_COMPRESS))
959200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
960200da64eSTsutomu Itoh 		else
961200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
962200da64eSTsutomu Itoh 	}
963c289811cSChris Mason 	if (btrfs_test_opt(root, NOSSD))
964c289811cSChris Mason 		seq_puts(seq, ",nossd");
965451d7585SChris Mason 	if (btrfs_test_opt(root, SSD_SPREAD))
966451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
967451d7585SChris Mason 	else if (btrfs_test_opt(root, SSD))
968a9572a15SEric Paris 		seq_puts(seq, ",ssd");
9693a5e1404SSage Weil 	if (btrfs_test_opt(root, NOTREELOG))
9706b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
971dccae999SSage Weil 	if (btrfs_test_opt(root, FLUSHONCOMMIT))
9726b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
97320a5239aSMatthew Wilcox 	if (btrfs_test_opt(root, DISCARD))
97420a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
975a9572a15SEric Paris 	if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
976a9572a15SEric Paris 		seq_puts(seq, ",noacl");
977200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, SPACE_CACHE))
978200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
97973bc1876SJosef Bacik 	else
9808965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
981200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, CLEAR_CACHE))
982200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
983200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
984200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
9850942caa3SDavid Sterba 	if (btrfs_test_opt(root, ENOSPC_DEBUG))
9860942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
9870942caa3SDavid Sterba 	if (btrfs_test_opt(root, AUTO_DEFRAG))
9880942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
9890942caa3SDavid Sterba 	if (btrfs_test_opt(root, INODE_MAP_CACHE))
9900942caa3SDavid Sterba 		seq_puts(seq, ",inode_cache");
9919555c6c1SIlya Dryomov 	if (btrfs_test_opt(root, SKIP_BALANCE))
9929555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
9938507d216SWang Shilong 	if (btrfs_test_opt(root, RECOVERY))
9948507d216SWang Shilong 		seq_puts(seq, ",recovery");
9958507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
9968507d216SWang Shilong 	if (btrfs_test_opt(root, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
9978507d216SWang Shilong 		seq_puts(seq, ",check_int_data");
9988507d216SWang Shilong 	else if (btrfs_test_opt(root, CHECK_INTEGRITY))
9998507d216SWang Shilong 		seq_puts(seq, ",check_int");
10008507d216SWang Shilong 	if (info->check_integrity_print_mask)
10018507d216SWang Shilong 		seq_printf(seq, ",check_int_print_mask=%d",
10028507d216SWang Shilong 				info->check_integrity_print_mask);
10038507d216SWang Shilong #endif
10048507d216SWang Shilong 	if (info->metadata_ratio)
10058507d216SWang Shilong 		seq_printf(seq, ",metadata_ratio=%d",
10068507d216SWang Shilong 				info->metadata_ratio);
10078c342930SJeff Mahoney 	if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR))
10088c342930SJeff Mahoney 		seq_puts(seq, ",fatal_errors=panic");
1009*8b87dc17SDavid Sterba 	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1010*8b87dc17SDavid Sterba 		seq_printf(seq, ",commit=%d", info->commit_interval);
1011a9572a15SEric Paris 	return 0;
1012a9572a15SEric Paris }
1013a9572a15SEric Paris 
1014a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
10152e635a27SChris Mason {
1016815745cfSAl Viro 	struct btrfs_fs_info *p = data;
1017815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(s);
10184b82d6e4SYan 
1019815745cfSAl Viro 	return fs_info->fs_devices == p->fs_devices;
10204b82d6e4SYan }
10214b82d6e4SYan 
1022450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
1023450ba0eaSJosef Bacik {
10246de1d09dSAl Viro 	int err = set_anon_super(s, data);
10256de1d09dSAl Viro 	if (!err)
1026450ba0eaSJosef Bacik 		s->s_fs_info = data;
10276de1d09dSAl Viro 	return err;
1028450ba0eaSJosef Bacik }
1029450ba0eaSJosef Bacik 
1030830c4adbSJosef Bacik /*
1031f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
1032f9d9ef62SDavid Sterba  */
1033f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
1034f9d9ef62SDavid Sterba {
1035f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1036f9d9ef62SDavid Sterba 		return 1;
1037f9d9ef62SDavid Sterba 	return 0;
1038f9d9ef62SDavid Sterba }
1039f9d9ef62SDavid Sterba 
1040f9d9ef62SDavid Sterba /*
1041830c4adbSJosef Bacik  * This will strip out the subvol=%s argument for an argument string and add
1042830c4adbSJosef Bacik  * subvolid=0 to make sure we get the actual tree root for path walking to the
1043830c4adbSJosef Bacik  * subvol we want.
1044830c4adbSJosef Bacik  */
1045830c4adbSJosef Bacik static char *setup_root_args(char *args)
1046830c4adbSJosef Bacik {
1047f60d16a8SJim Meyering 	unsigned len = strlen(args) + 2 + 1;
1048f60d16a8SJim Meyering 	char *src, *dst, *buf;
1049830c4adbSJosef Bacik 
1050830c4adbSJosef Bacik 	/*
1051f60d16a8SJim Meyering 	 * We need the same args as before, but with this substitution:
1052f60d16a8SJim Meyering 	 * s!subvol=[^,]+!subvolid=0!
1053830c4adbSJosef Bacik 	 *
1054f60d16a8SJim Meyering 	 * Since the replacement string is up to 2 bytes longer than the
1055f60d16a8SJim Meyering 	 * original, allocate strlen(args) + 2 + 1 bytes.
1056830c4adbSJosef Bacik 	 */
1057830c4adbSJosef Bacik 
1058f60d16a8SJim Meyering 	src = strstr(args, "subvol=");
1059830c4adbSJosef Bacik 	/* This shouldn't happen, but just in case.. */
1060f60d16a8SJim Meyering 	if (!src)
1061830c4adbSJosef Bacik 		return NULL;
1062f60d16a8SJim Meyering 
1063f60d16a8SJim Meyering 	buf = dst = kmalloc(len, GFP_NOFS);
1064f60d16a8SJim Meyering 	if (!buf)
1065f60d16a8SJim Meyering 		return NULL;
1066830c4adbSJosef Bacik 
1067830c4adbSJosef Bacik 	/*
1068f60d16a8SJim Meyering 	 * If the subvol= arg is not at the start of the string,
1069f60d16a8SJim Meyering 	 * copy whatever precedes it into buf.
1070830c4adbSJosef Bacik 	 */
1071f60d16a8SJim Meyering 	if (src != args) {
1072f60d16a8SJim Meyering 		*src++ = '\0';
1073f60d16a8SJim Meyering 		strcpy(buf, args);
1074f60d16a8SJim Meyering 		dst += strlen(args);
1075830c4adbSJosef Bacik 	}
1076830c4adbSJosef Bacik 
1077f60d16a8SJim Meyering 	strcpy(dst, "subvolid=0");
1078f60d16a8SJim Meyering 	dst += strlen("subvolid=0");
1079830c4adbSJosef Bacik 
1080830c4adbSJosef Bacik 	/*
1081f60d16a8SJim Meyering 	 * If there is a "," after the original subvol=... string,
1082f60d16a8SJim Meyering 	 * copy that suffix into our buffer.  Otherwise, we're done.
1083830c4adbSJosef Bacik 	 */
1084f60d16a8SJim Meyering 	src = strchr(src, ',');
1085f60d16a8SJim Meyering 	if (src)
1086f60d16a8SJim Meyering 		strcpy(dst, src);
1087830c4adbSJosef Bacik 
1088f60d16a8SJim Meyering 	return buf;
1089830c4adbSJosef Bacik }
1090830c4adbSJosef Bacik 
1091830c4adbSJosef Bacik static struct dentry *mount_subvol(const char *subvol_name, int flags,
1092830c4adbSJosef Bacik 				   const char *device_name, char *data)
1093830c4adbSJosef Bacik {
1094830c4adbSJosef Bacik 	struct dentry *root;
1095830c4adbSJosef Bacik 	struct vfsmount *mnt;
1096830c4adbSJosef Bacik 	char *newargs;
1097830c4adbSJosef Bacik 
1098830c4adbSJosef Bacik 	newargs = setup_root_args(data);
1099830c4adbSJosef Bacik 	if (!newargs)
1100830c4adbSJosef Bacik 		return ERR_PTR(-ENOMEM);
1101830c4adbSJosef Bacik 	mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name,
1102830c4adbSJosef Bacik 			     newargs);
1103830c4adbSJosef Bacik 	kfree(newargs);
1104830c4adbSJosef Bacik 	if (IS_ERR(mnt))
1105830c4adbSJosef Bacik 		return ERR_CAST(mnt);
1106830c4adbSJosef Bacik 
1107ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1108830c4adbSJosef Bacik 
1109ea441d11SAl Viro 	if (!IS_ERR(root) && !is_subvolume_inode(root->d_inode)) {
1110ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1111ea441d11SAl Viro 		dput(root);
1112ea441d11SAl Viro 		root = ERR_PTR(-EINVAL);
1113ea441d11SAl Viro 		deactivate_locked_super(s);
1114f9d9ef62SDavid Sterba 		printk(KERN_ERR "btrfs: '%s' is not a valid subvolume\n",
1115f9d9ef62SDavid Sterba 				subvol_name);
1116f9d9ef62SDavid Sterba 	}
1117f9d9ef62SDavid Sterba 
1118830c4adbSJosef Bacik 	return root;
1119830c4adbSJosef Bacik }
1120450ba0eaSJosef Bacik 
1121edf24abeSChristoph Hellwig /*
1122edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
1123edf24abeSChristoph Hellwig  *
1124edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
1125edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
1126edf24abeSChristoph Hellwig  */
1127061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1128306e16ceSDavid Sterba 		const char *device_name, void *data)
11294b82d6e4SYan {
11304b82d6e4SYan 	struct block_device *bdev = NULL;
11314b82d6e4SYan 	struct super_block *s;
11324b82d6e4SYan 	struct dentry *root;
11338a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
1134450ba0eaSJosef Bacik 	struct btrfs_fs_info *fs_info = NULL;
113597288f2cSChristoph Hellwig 	fmode_t mode = FMODE_READ;
113673f73415SJosef Bacik 	char *subvol_name = NULL;
113773f73415SJosef Bacik 	u64 subvol_objectid = 0;
11384b82d6e4SYan 	int error = 0;
11394b82d6e4SYan 
114097288f2cSChristoph Hellwig 	if (!(flags & MS_RDONLY))
114197288f2cSChristoph Hellwig 		mode |= FMODE_WRITE;
114297288f2cSChristoph Hellwig 
114397288f2cSChristoph Hellwig 	error = btrfs_parse_early_options(data, mode, fs_type,
114473f73415SJosef Bacik 					  &subvol_name, &subvol_objectid,
11455e2a4b25SDavid Sterba 					  &fs_devices);
1146f23c8af8SIlya Dryomov 	if (error) {
1147f23c8af8SIlya Dryomov 		kfree(subvol_name);
1148061dbc6bSAl Viro 		return ERR_PTR(error);
1149f23c8af8SIlya Dryomov 	}
1150edf24abeSChristoph Hellwig 
1151830c4adbSJosef Bacik 	if (subvol_name) {
1152830c4adbSJosef Bacik 		root = mount_subvol(subvol_name, flags, device_name, data);
1153830c4adbSJosef Bacik 		kfree(subvol_name);
1154830c4adbSJosef Bacik 		return root;
1155830c4adbSJosef Bacik 	}
1156830c4adbSJosef Bacik 
1157306e16ceSDavid Sterba 	error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
11588a4b83ccSChris Mason 	if (error)
1159830c4adbSJosef Bacik 		return ERR_PTR(error);
11604b82d6e4SYan 
1161450ba0eaSJosef Bacik 	/*
1162450ba0eaSJosef Bacik 	 * Setup a dummy root and fs_info for test/set super.  This is because
1163450ba0eaSJosef Bacik 	 * we don't actually fill this stuff out until open_ctree, but we need
1164450ba0eaSJosef Bacik 	 * it for searching for existing supers, so this lets us do that and
1165450ba0eaSJosef Bacik 	 * then open_ctree will properly initialize everything later.
1166450ba0eaSJosef Bacik 	 */
1167450ba0eaSJosef Bacik 	fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
116804d21a24SIlya Dryomov 	if (!fs_info)
116904d21a24SIlya Dryomov 		return ERR_PTR(-ENOMEM);
117004d21a24SIlya Dryomov 
1171450ba0eaSJosef Bacik 	fs_info->fs_devices = fs_devices;
1172450ba0eaSJosef Bacik 
11736c41761fSDavid Sterba 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
11746c41761fSDavid Sterba 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
11756c41761fSDavid Sterba 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
11766c41761fSDavid Sterba 		error = -ENOMEM;
117704d21a24SIlya Dryomov 		goto error_fs_info;
117804d21a24SIlya Dryomov 	}
117904d21a24SIlya Dryomov 
118004d21a24SIlya Dryomov 	error = btrfs_open_devices(fs_devices, mode, fs_type);
118104d21a24SIlya Dryomov 	if (error)
118204d21a24SIlya Dryomov 		goto error_fs_info;
118304d21a24SIlya Dryomov 
118404d21a24SIlya Dryomov 	if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
118504d21a24SIlya Dryomov 		error = -EACCES;
11866c41761fSDavid Sterba 		goto error_close_devices;
11876c41761fSDavid Sterba 	}
11886c41761fSDavid Sterba 
1189dfe25020SChris Mason 	bdev = fs_devices->latest_bdev;
11909249e17fSDavid Howells 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC,
11919249e17fSDavid Howells 		 fs_info);
1192830c4adbSJosef Bacik 	if (IS_ERR(s)) {
1193830c4adbSJosef Bacik 		error = PTR_ERR(s);
1194830c4adbSJosef Bacik 		goto error_close_devices;
1195830c4adbSJosef Bacik 	}
11964b82d6e4SYan 
11974b82d6e4SYan 	if (s->s_root) {
11982b82032cSYan Zheng 		btrfs_close_devices(fs_devices);
11996c41761fSDavid Sterba 		free_fs_info(fs_info);
120059553edfSAl Viro 		if ((flags ^ s->s_flags) & MS_RDONLY)
120159553edfSAl Viro 			error = -EBUSY;
12024b82d6e4SYan 	} else {
12034b82d6e4SYan 		char b[BDEVNAME_SIZE];
12044b82d6e4SYan 
12054b82d6e4SYan 		strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
1206815745cfSAl Viro 		btrfs_sb(s)->bdev_holder = fs_type;
12078a4b83ccSChris Mason 		error = btrfs_fill_super(s, fs_devices, data,
12088a4b83ccSChris Mason 					 flags & MS_SILENT ? 1 : 0);
12094b82d6e4SYan 	}
12104b82d6e4SYan 
121159553edfSAl Viro 	root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error);
121259553edfSAl Viro 	if (IS_ERR(root))
1213e15d0542SXin Zhong 		deactivate_locked_super(s);
12144b82d6e4SYan 
1215061dbc6bSAl Viro 	return root;
12164b82d6e4SYan 
1217c146afadSYan Zheng error_close_devices:
12188a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
121904d21a24SIlya Dryomov error_fs_info:
12206c41761fSDavid Sterba 	free_fs_info(fs_info);
1221061dbc6bSAl Viro 	return ERR_PTR(error);
12224b82d6e4SYan }
12232e635a27SChris Mason 
12240d2450abSSergei Trofimovich static void btrfs_set_max_workers(struct btrfs_workers *workers, int new_limit)
12250d2450abSSergei Trofimovich {
12260d2450abSSergei Trofimovich 	spin_lock_irq(&workers->lock);
12270d2450abSSergei Trofimovich 	workers->max_workers = new_limit;
12280d2450abSSergei Trofimovich 	spin_unlock_irq(&workers->lock);
12290d2450abSSergei Trofimovich }
12300d2450abSSergei Trofimovich 
12310d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
12320d2450abSSergei Trofimovich 				     int new_pool_size, int old_pool_size)
12330d2450abSSergei Trofimovich {
12340d2450abSSergei Trofimovich 	if (new_pool_size == old_pool_size)
12350d2450abSSergei Trofimovich 		return;
12360d2450abSSergei Trofimovich 
12370d2450abSSergei Trofimovich 	fs_info->thread_pool_size = new_pool_size;
12380d2450abSSergei Trofimovich 
12390d2450abSSergei Trofimovich 	printk(KERN_INFO "btrfs: resize thread pool %d -> %d\n",
12400d2450abSSergei Trofimovich 	       old_pool_size, new_pool_size);
12410d2450abSSergei Trofimovich 
12420d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->generic_worker, new_pool_size);
12430d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->workers, new_pool_size);
12440d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->delalloc_workers, new_pool_size);
12450d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->submit_workers, new_pool_size);
12460d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->caching_workers, new_pool_size);
12470d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->fixup_workers, new_pool_size);
12480d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->endio_workers, new_pool_size);
12490d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->endio_meta_workers, new_pool_size);
12500d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->endio_meta_write_workers, new_pool_size);
12510d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->endio_write_workers, new_pool_size);
12520d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->endio_freespace_worker, new_pool_size);
12530d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->delayed_workers, new_pool_size);
12540d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->readahead_workers, new_pool_size);
1255ff023aacSStefan Behrens 	btrfs_set_max_workers(&fs_info->scrub_wr_completion_workers,
1256ff023aacSStefan Behrens 			      new_pool_size);
12570d2450abSSergei Trofimovich }
12580d2450abSSergei Trofimovich 
1259f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
1260dc81cdc5SMiao Xie {
1261dc81cdc5SMiao Xie 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1262f42a34b2SMiao Xie }
1263dc81cdc5SMiao Xie 
1264f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1265f42a34b2SMiao Xie 				       unsigned long old_opts, int flags)
1266f42a34b2SMiao Xie {
1267dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1268dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1269dc81cdc5SMiao Xie 	     (flags & MS_RDONLY))) {
1270dc81cdc5SMiao Xie 		/* wait for any defraggers to finish */
1271dc81cdc5SMiao Xie 		wait_event(fs_info->transaction_wait,
1272dc81cdc5SMiao Xie 			   (atomic_read(&fs_info->defrag_running) == 0));
1273dc81cdc5SMiao Xie 		if (flags & MS_RDONLY)
1274dc81cdc5SMiao Xie 			sync_filesystem(fs_info->sb);
1275dc81cdc5SMiao Xie 	}
1276dc81cdc5SMiao Xie }
1277dc81cdc5SMiao Xie 
1278dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1279dc81cdc5SMiao Xie 					 unsigned long old_opts)
1280dc81cdc5SMiao Xie {
1281dc81cdc5SMiao Xie 	/*
1282dc81cdc5SMiao Xie 	 * We need cleanup all defragable inodes if the autodefragment is
1283dc81cdc5SMiao Xie 	 * close or the fs is R/O.
1284dc81cdc5SMiao Xie 	 */
1285dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1286dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1287dc81cdc5SMiao Xie 	     (fs_info->sb->s_flags & MS_RDONLY))) {
1288dc81cdc5SMiao Xie 		btrfs_cleanup_defrag_inodes(fs_info);
1289dc81cdc5SMiao Xie 	}
1290dc81cdc5SMiao Xie 
1291dc81cdc5SMiao Xie 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1292dc81cdc5SMiao Xie }
1293dc81cdc5SMiao Xie 
1294c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1295c146afadSYan Zheng {
1296815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1297815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
129849b25e05SJeff Mahoney 	unsigned old_flags = sb->s_flags;
129949b25e05SJeff Mahoney 	unsigned long old_opts = fs_info->mount_opt;
130049b25e05SJeff Mahoney 	unsigned long old_compress_type = fs_info->compress_type;
130149b25e05SJeff Mahoney 	u64 old_max_inline = fs_info->max_inline;
130249b25e05SJeff Mahoney 	u64 old_alloc_start = fs_info->alloc_start;
130349b25e05SJeff Mahoney 	int old_thread_pool_size = fs_info->thread_pool_size;
130449b25e05SJeff Mahoney 	unsigned int old_metadata_ratio = fs_info->metadata_ratio;
1305c146afadSYan Zheng 	int ret;
1306c146afadSYan Zheng 
1307f42a34b2SMiao Xie 	btrfs_remount_prepare(fs_info);
1308dc81cdc5SMiao Xie 
1309b288052eSChris Mason 	ret = btrfs_parse_options(root, data);
131049b25e05SJeff Mahoney 	if (ret) {
131149b25e05SJeff Mahoney 		ret = -EINVAL;
131249b25e05SJeff Mahoney 		goto restore;
131349b25e05SJeff Mahoney 	}
1314b288052eSChris Mason 
1315f42a34b2SMiao Xie 	btrfs_remount_begin(fs_info, old_opts, *flags);
13160d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
13170d2450abSSergei Trofimovich 		fs_info->thread_pool_size, old_thread_pool_size);
13180d2450abSSergei Trofimovich 
1319c146afadSYan Zheng 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
1320dc81cdc5SMiao Xie 		goto out;
1321c146afadSYan Zheng 
1322c146afadSYan Zheng 	if (*flags & MS_RDONLY) {
13238dabb742SStefan Behrens 		/*
13248dabb742SStefan Behrens 		 * this also happens on 'umount -rf' or on shutdown, when
13258dabb742SStefan Behrens 		 * the filesystem is busy.
13268dabb742SStefan Behrens 		 */
1327c146afadSYan Zheng 		sb->s_flags |= MS_RDONLY;
1328c146afadSYan Zheng 
13298dabb742SStefan Behrens 		btrfs_dev_replace_suspend_for_unmount(fs_info);
13308dabb742SStefan Behrens 		btrfs_scrub_cancel(fs_info);
1331061594efSMiao Xie 		btrfs_pause_balance(fs_info);
13328dabb742SStefan Behrens 
1333c146afadSYan Zheng 		ret = btrfs_commit_super(root);
133449b25e05SJeff Mahoney 		if (ret)
133549b25e05SJeff Mahoney 			goto restore;
1336c146afadSYan Zheng 	} else {
13378a3db184SSergei Trofimovich 		if (fs_info->fs_devices->rw_devices == 0) {
133849b25e05SJeff Mahoney 			ret = -EACCES;
133949b25e05SJeff Mahoney 			goto restore;
13408a3db184SSergei Trofimovich 		}
13412b82032cSYan Zheng 
1342292fd7fcSStefan Behrens 		if (fs_info->fs_devices->missing_devices >
1343292fd7fcSStefan Behrens 		     fs_info->num_tolerated_disk_barrier_failures &&
1344292fd7fcSStefan Behrens 		    !(*flags & MS_RDONLY)) {
1345292fd7fcSStefan Behrens 			printk(KERN_WARNING
1346292fd7fcSStefan Behrens 			       "Btrfs: too many missing devices, writeable remount is not allowed\n");
1347292fd7fcSStefan Behrens 			ret = -EACCES;
1348292fd7fcSStefan Behrens 			goto restore;
1349292fd7fcSStefan Behrens 		}
1350292fd7fcSStefan Behrens 
13518a3db184SSergei Trofimovich 		if (btrfs_super_log_root(fs_info->super_copy) != 0) {
135249b25e05SJeff Mahoney 			ret = -EINVAL;
135349b25e05SJeff Mahoney 			goto restore;
13548a3db184SSergei Trofimovich 		}
1355c146afadSYan Zheng 
1356815745cfSAl Viro 		ret = btrfs_cleanup_fs_roots(fs_info);
135749b25e05SJeff Mahoney 		if (ret)
135849b25e05SJeff Mahoney 			goto restore;
1359c146afadSYan Zheng 
1360d68fc57bSYan, Zheng 		/* recover relocation */
1361d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
136249b25e05SJeff Mahoney 		if (ret)
136349b25e05SJeff Mahoney 			goto restore;
1364c146afadSYan Zheng 
13652b6ba629SIlya Dryomov 		ret = btrfs_resume_balance_async(fs_info);
13662b6ba629SIlya Dryomov 		if (ret)
13672b6ba629SIlya Dryomov 			goto restore;
13682b6ba629SIlya Dryomov 
13698dabb742SStefan Behrens 		ret = btrfs_resume_dev_replace_async(fs_info);
13708dabb742SStefan Behrens 		if (ret) {
13718dabb742SStefan Behrens 			pr_warn("btrfs: failed to resume dev_replace\n");
13728dabb742SStefan Behrens 			goto restore;
13738dabb742SStefan Behrens 		}
1374c146afadSYan Zheng 		sb->s_flags &= ~MS_RDONLY;
1375c146afadSYan Zheng 	}
1376dc81cdc5SMiao Xie out:
1377dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
1378c146afadSYan Zheng 	return 0;
137949b25e05SJeff Mahoney 
138049b25e05SJeff Mahoney restore:
138149b25e05SJeff Mahoney 	/* We've hit an error - don't reset MS_RDONLY */
138249b25e05SJeff Mahoney 	if (sb->s_flags & MS_RDONLY)
138349b25e05SJeff Mahoney 		old_flags |= MS_RDONLY;
138449b25e05SJeff Mahoney 	sb->s_flags = old_flags;
138549b25e05SJeff Mahoney 	fs_info->mount_opt = old_opts;
138649b25e05SJeff Mahoney 	fs_info->compress_type = old_compress_type;
138749b25e05SJeff Mahoney 	fs_info->max_inline = old_max_inline;
1388c018daecSMiao Xie 	mutex_lock(&fs_info->chunk_mutex);
138949b25e05SJeff Mahoney 	fs_info->alloc_start = old_alloc_start;
1390c018daecSMiao Xie 	mutex_unlock(&fs_info->chunk_mutex);
13910d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
13920d2450abSSergei Trofimovich 		old_thread_pool_size, fs_info->thread_pool_size);
139349b25e05SJeff Mahoney 	fs_info->metadata_ratio = old_metadata_ratio;
1394dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
139549b25e05SJeff Mahoney 	return ret;
1396c146afadSYan Zheng }
1397c146afadSYan Zheng 
1398bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
1399bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1,
1400bcd53741SArne Jansen 				       const void *dev_info2)
1401bcd53741SArne Jansen {
1402bcd53741SArne Jansen 	if (((struct btrfs_device_info *)dev_info1)->max_avail >
1403bcd53741SArne Jansen 	    ((struct btrfs_device_info *)dev_info2)->max_avail)
1404bcd53741SArne Jansen 		return -1;
1405bcd53741SArne Jansen 	else if (((struct btrfs_device_info *)dev_info1)->max_avail <
1406bcd53741SArne Jansen 		 ((struct btrfs_device_info *)dev_info2)->max_avail)
1407bcd53741SArne Jansen 		return 1;
1408bcd53741SArne Jansen 	else
1409bcd53741SArne Jansen 	return 0;
1410bcd53741SArne Jansen }
1411bcd53741SArne Jansen 
1412bcd53741SArne Jansen /*
1413bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
1414bcd53741SArne Jansen  * is stored.(Descending Sort)
1415bcd53741SArne Jansen  */
1416bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
1417bcd53741SArne Jansen 					struct btrfs_device_info *devices,
1418bcd53741SArne Jansen 					size_t nr_devices)
1419bcd53741SArne Jansen {
1420bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1421bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
1422bcd53741SArne Jansen }
1423bcd53741SArne Jansen 
14246d07bcecSMiao Xie /*
14256d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
14266d07bcecSMiao Xie  * file data.
14276d07bcecSMiao Xie  */
14286d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
14296d07bcecSMiao Xie {
14306d07bcecSMiao Xie 	struct btrfs_fs_info *fs_info = root->fs_info;
14316d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
14326d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
14336d07bcecSMiao Xie 	struct btrfs_device *device;
14346d07bcecSMiao Xie 	u64 skip_space;
14356d07bcecSMiao Xie 	u64 type;
14366d07bcecSMiao Xie 	u64 avail_space;
14376d07bcecSMiao Xie 	u64 used_space;
14386d07bcecSMiao Xie 	u64 min_stripe_size;
143939fb26c3SMiao Xie 	int min_stripes = 1, num_stripes = 1;
14406d07bcecSMiao Xie 	int i = 0, nr_devices;
14416d07bcecSMiao Xie 	int ret;
14426d07bcecSMiao Xie 
1443b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
14446d07bcecSMiao Xie 	BUG_ON(!nr_devices);
14456d07bcecSMiao Xie 
14466d07bcecSMiao Xie 	devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
14476d07bcecSMiao Xie 			       GFP_NOFS);
14486d07bcecSMiao Xie 	if (!devices_info)
14496d07bcecSMiao Xie 		return -ENOMEM;
14506d07bcecSMiao Xie 
14516d07bcecSMiao Xie 	/* calc min stripe number for data space alloction */
14526d07bcecSMiao Xie 	type = btrfs_get_alloc_profile(root, 1);
145339fb26c3SMiao Xie 	if (type & BTRFS_BLOCK_GROUP_RAID0) {
14546d07bcecSMiao Xie 		min_stripes = 2;
145539fb26c3SMiao Xie 		num_stripes = nr_devices;
145639fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID1) {
14576d07bcecSMiao Xie 		min_stripes = 2;
145839fb26c3SMiao Xie 		num_stripes = 2;
145939fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID10) {
14606d07bcecSMiao Xie 		min_stripes = 4;
146139fb26c3SMiao Xie 		num_stripes = 4;
146239fb26c3SMiao Xie 	}
14636d07bcecSMiao Xie 
14646d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_DUP)
14656d07bcecSMiao Xie 		min_stripe_size = 2 * BTRFS_STRIPE_LEN;
14666d07bcecSMiao Xie 	else
14676d07bcecSMiao Xie 		min_stripe_size = BTRFS_STRIPE_LEN;
14686d07bcecSMiao Xie 
1469b772a86eSLi Zefan 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
147063a212abSStefan Behrens 		if (!device->in_fs_metadata || !device->bdev ||
147163a212abSStefan Behrens 		    device->is_tgtdev_for_dev_replace)
14726d07bcecSMiao Xie 			continue;
14736d07bcecSMiao Xie 
14746d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
14756d07bcecSMiao Xie 
14766d07bcecSMiao Xie 		/* align with stripe_len */
14776d07bcecSMiao Xie 		do_div(avail_space, BTRFS_STRIPE_LEN);
14786d07bcecSMiao Xie 		avail_space *= BTRFS_STRIPE_LEN;
14796d07bcecSMiao Xie 
14806d07bcecSMiao Xie 		/*
14816d07bcecSMiao Xie 		 * In order to avoid overwritting the superblock on the drive,
14826d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
14836d07bcecSMiao Xie 		 * allocation.
14846d07bcecSMiao Xie 		 */
14856d07bcecSMiao Xie 		skip_space = 1024 * 1024;
14866d07bcecSMiao Xie 
14876d07bcecSMiao Xie 		/* user can set the offset in fs_info->alloc_start. */
14886d07bcecSMiao Xie 		if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
14896d07bcecSMiao Xie 		    device->total_bytes)
14906d07bcecSMiao Xie 			skip_space = max(fs_info->alloc_start, skip_space);
14916d07bcecSMiao Xie 
14926d07bcecSMiao Xie 		/*
14936d07bcecSMiao Xie 		 * btrfs can not use the free space in [0, skip_space - 1],
14946d07bcecSMiao Xie 		 * we must subtract it from the total. In order to implement
14956d07bcecSMiao Xie 		 * it, we account the used space in this range first.
14966d07bcecSMiao Xie 		 */
14976d07bcecSMiao Xie 		ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
14986d07bcecSMiao Xie 						     &used_space);
14996d07bcecSMiao Xie 		if (ret) {
15006d07bcecSMiao Xie 			kfree(devices_info);
15016d07bcecSMiao Xie 			return ret;
15026d07bcecSMiao Xie 		}
15036d07bcecSMiao Xie 
15046d07bcecSMiao Xie 		/* calc the free space in [0, skip_space - 1] */
15056d07bcecSMiao Xie 		skip_space -= used_space;
15066d07bcecSMiao Xie 
15076d07bcecSMiao Xie 		/*
15086d07bcecSMiao Xie 		 * we can use the free space in [0, skip_space - 1], subtract
15096d07bcecSMiao Xie 		 * it from the total.
15106d07bcecSMiao Xie 		 */
15116d07bcecSMiao Xie 		if (avail_space && avail_space >= skip_space)
15126d07bcecSMiao Xie 			avail_space -= skip_space;
15136d07bcecSMiao Xie 		else
15146d07bcecSMiao Xie 			avail_space = 0;
15156d07bcecSMiao Xie 
15166d07bcecSMiao Xie 		if (avail_space < min_stripe_size)
15176d07bcecSMiao Xie 			continue;
15186d07bcecSMiao Xie 
15196d07bcecSMiao Xie 		devices_info[i].dev = device;
15206d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
15216d07bcecSMiao Xie 
15226d07bcecSMiao Xie 		i++;
15236d07bcecSMiao Xie 	}
15246d07bcecSMiao Xie 
15256d07bcecSMiao Xie 	nr_devices = i;
15266d07bcecSMiao Xie 
15276d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
15286d07bcecSMiao Xie 
15296d07bcecSMiao Xie 	i = nr_devices - 1;
15306d07bcecSMiao Xie 	avail_space = 0;
15316d07bcecSMiao Xie 	while (nr_devices >= min_stripes) {
153239fb26c3SMiao Xie 		if (num_stripes > nr_devices)
153339fb26c3SMiao Xie 			num_stripes = nr_devices;
153439fb26c3SMiao Xie 
15356d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
15366d07bcecSMiao Xie 			int j;
15376d07bcecSMiao Xie 			u64 alloc_size;
15386d07bcecSMiao Xie 
153939fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
15406d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
154139fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
15426d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
15436d07bcecSMiao Xie 		}
15446d07bcecSMiao Xie 		i--;
15456d07bcecSMiao Xie 		nr_devices--;
15466d07bcecSMiao Xie 	}
15476d07bcecSMiao Xie 
15486d07bcecSMiao Xie 	kfree(devices_info);
15496d07bcecSMiao Xie 	*free_bytes = avail_space;
15506d07bcecSMiao Xie 	return 0;
15516d07bcecSMiao Xie }
15526d07bcecSMiao Xie 
15538fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
15548fd17795SChris Mason {
1555815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
1556815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
1557815745cfSAl Viro 	struct list_head *head = &fs_info->space_info;
1558bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
1559bd4d1088SJosef Bacik 	u64 total_used = 0;
15606d07bcecSMiao Xie 	u64 total_free_data = 0;
1561db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
1562815745cfSAl Viro 	__be32 *fsid = (__be32 *)fs_info->fsid;
15636d07bcecSMiao Xie 	int ret;
15648fd17795SChris Mason 
15656d07bcecSMiao Xie 	/* holding chunk_muext to avoid allocating new chunks */
1566815745cfSAl Viro 	mutex_lock(&fs_info->chunk_mutex);
1567bd4d1088SJosef Bacik 	rcu_read_lock();
156889a55897SJosef Bacik 	list_for_each_entry_rcu(found, head, list) {
15696d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
15706d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
15716d07bcecSMiao Xie 			total_free_data -=
15726d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
15736d07bcecSMiao Xie 		}
15746d07bcecSMiao Xie 
1575b742bb82SYan, Zheng 		total_used += found->disk_used;
157689a55897SJosef Bacik 	}
1577bd4d1088SJosef Bacik 	rcu_read_unlock();
1578bd4d1088SJosef Bacik 
15798fd17795SChris Mason 	buf->f_namelen = BTRFS_NAME_LEN;
1580db94535dSChris Mason 	buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
1581bd4d1088SJosef Bacik 	buf->f_bfree = buf->f_blocks - (total_used >> bits);
15828fd17795SChris Mason 	buf->f_bsize = dentry->d_sb->s_blocksize;
15838fd17795SChris Mason 	buf->f_type = BTRFS_SUPER_MAGIC;
15846d07bcecSMiao Xie 	buf->f_bavail = total_free_data;
1585815745cfSAl Viro 	ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data);
15866d07bcecSMiao Xie 	if (ret) {
1587815745cfSAl Viro 		mutex_unlock(&fs_info->chunk_mutex);
15886d07bcecSMiao Xie 		return ret;
15896d07bcecSMiao Xie 	}
15906d07bcecSMiao Xie 	buf->f_bavail += total_free_data;
15916d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
1592815745cfSAl Viro 	mutex_unlock(&fs_info->chunk_mutex);
1593d397712bSChris Mason 
15949d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
15959d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
15969d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
15979d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
15989d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
159932d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
160032d48fa1SDavid Woodhouse 	buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
160132d48fa1SDavid Woodhouse 	buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
160232d48fa1SDavid Woodhouse 
16038fd17795SChris Mason 	return 0;
16048fd17795SChris Mason }
1605b5133862SChris Mason 
1606aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
1607aea52e19SAl Viro {
1608815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1609aea52e19SAl Viro 	kill_anon_super(sb);
1610aea52e19SAl Viro 	free_fs_info(fs_info);
1611aea52e19SAl Viro }
1612aea52e19SAl Viro 
16132e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
16142e635a27SChris Mason 	.owner		= THIS_MODULE,
16152e635a27SChris Mason 	.name		= "btrfs",
1616061dbc6bSAl Viro 	.mount		= btrfs_mount,
1617aea52e19SAl Viro 	.kill_sb	= btrfs_kill_super,
16182e635a27SChris Mason 	.fs_flags	= FS_REQUIRES_DEV,
16192e635a27SChris Mason };
16207f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs");
1621a9218f6bSChris Mason 
1622d352ac68SChris Mason /*
1623d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
1624d352ac68SChris Mason  */
16258a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
16268a4b83ccSChris Mason 				unsigned long arg)
16278a4b83ccSChris Mason {
16288a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
16298a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
1630c071fcfdSChris Mason 	int ret = -ENOTTY;
16318a4b83ccSChris Mason 
1632e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1633e441d54dSChris Mason 		return -EPERM;
1634e441d54dSChris Mason 
1635dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
1636dae7b665SLi Zefan 	if (IS_ERR(vol))
1637dae7b665SLi Zefan 		return PTR_ERR(vol);
1638c071fcfdSChris Mason 
16398a4b83ccSChris Mason 	switch (cmd) {
16408a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
164197288f2cSChristoph Hellwig 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
16428a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
16438a4b83ccSChris Mason 		break;
164402db0844SJosef Bacik 	case BTRFS_IOC_DEVICES_READY:
164502db0844SJosef Bacik 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
164602db0844SJosef Bacik 					    &btrfs_fs_type, &fs_devices);
164702db0844SJosef Bacik 		if (ret)
164802db0844SJosef Bacik 			break;
164902db0844SJosef Bacik 		ret = !(fs_devices->num_devices == fs_devices->total_devices);
165002db0844SJosef Bacik 		break;
16518a4b83ccSChris Mason 	}
1652dae7b665SLi Zefan 
16538a4b83ccSChris Mason 	kfree(vol);
1654f819d837SLinda Knippers 	return ret;
16558a4b83ccSChris Mason }
16568a4b83ccSChris Mason 
16570176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
1658ed0dab6bSYan {
1659354aa0fbSMiao Xie 	struct btrfs_trans_handle *trans;
1660354aa0fbSMiao Xie 	struct btrfs_root *root = btrfs_sb(sb)->tree_root;
1661354aa0fbSMiao Xie 
1662d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
1663354aa0fbSMiao Xie 	if (IS_ERR(trans)) {
1664354aa0fbSMiao Xie 		/* no transaction, don't bother */
1665354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
16660176260fSLinus Torvalds 			return 0;
1667354aa0fbSMiao Xie 		return PTR_ERR(trans);
1668354aa0fbSMiao Xie 	}
1669354aa0fbSMiao Xie 	return btrfs_commit_transaction(trans, root);
1670ed0dab6bSYan }
1671ed0dab6bSYan 
16720176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb)
1673ed0dab6bSYan {
16740176260fSLinus Torvalds 	return 0;
1675ed0dab6bSYan }
16762e635a27SChris Mason 
16779c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
16789c5085c1SJosef Bacik {
16799c5085c1SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
16809c5085c1SJosef Bacik 	struct btrfs_fs_devices *cur_devices;
16819c5085c1SJosef Bacik 	struct btrfs_device *dev, *first_dev = NULL;
16829c5085c1SJosef Bacik 	struct list_head *head;
16839c5085c1SJosef Bacik 	struct rcu_string *name;
16849c5085c1SJosef Bacik 
16859c5085c1SJosef Bacik 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
16869c5085c1SJosef Bacik 	cur_devices = fs_info->fs_devices;
16879c5085c1SJosef Bacik 	while (cur_devices) {
16889c5085c1SJosef Bacik 		head = &cur_devices->devices;
16899c5085c1SJosef Bacik 		list_for_each_entry(dev, head, dev_list) {
1690aa9ddcd4SJosef Bacik 			if (dev->missing)
1691aa9ddcd4SJosef Bacik 				continue;
16929c5085c1SJosef Bacik 			if (!first_dev || dev->devid < first_dev->devid)
16939c5085c1SJosef Bacik 				first_dev = dev;
16949c5085c1SJosef Bacik 		}
16959c5085c1SJosef Bacik 		cur_devices = cur_devices->seed;
16969c5085c1SJosef Bacik 	}
16979c5085c1SJosef Bacik 
16989c5085c1SJosef Bacik 	if (first_dev) {
16999c5085c1SJosef Bacik 		rcu_read_lock();
17009c5085c1SJosef Bacik 		name = rcu_dereference(first_dev->name);
17019c5085c1SJosef Bacik 		seq_escape(m, name->str, " \t\n\\");
17029c5085c1SJosef Bacik 		rcu_read_unlock();
17039c5085c1SJosef Bacik 	} else {
17049c5085c1SJosef Bacik 		WARN_ON(1);
17059c5085c1SJosef Bacik 	}
17069c5085c1SJosef Bacik 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
17079c5085c1SJosef Bacik 	return 0;
17089c5085c1SJosef Bacik }
17099c5085c1SJosef Bacik 
1710b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
171176dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
1712bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
1713e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
1714d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
1715a9572a15SEric Paris 	.show_options	= btrfs_show_options,
17169c5085c1SJosef Bacik 	.show_devname	= btrfs_show_devname,
17174730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
17182c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
17192c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
17208fd17795SChris Mason 	.statfs		= btrfs_statfs,
1721c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
17220176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
17230176260fSLinus Torvalds 	.unfreeze_fs	= btrfs_unfreeze,
1724e20d96d6SChris Mason };
1725a9218f6bSChris Mason 
1726a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
1727a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
1728a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
1729a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
17306038f373SArnd Bergmann 	.llseek = noop_llseek,
1731a9218f6bSChris Mason };
1732a9218f6bSChris Mason 
1733a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
1734578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
1735a9218f6bSChris Mason 	.name		= "btrfs-control",
1736a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
1737a9218f6bSChris Mason };
1738a9218f6bSChris Mason 
1739578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
1740578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
1741578454ffSKay Sievers 
1742a9218f6bSChris Mason static int btrfs_interface_init(void)
1743a9218f6bSChris Mason {
1744a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
1745a9218f6bSChris Mason }
1746a9218f6bSChris Mason 
1747b2950863SChristoph Hellwig static void btrfs_interface_exit(void)
1748a9218f6bSChris Mason {
1749a9218f6bSChris Mason 	if (misc_deregister(&btrfs_misc) < 0)
175048940662SDaniel J Blueman 		printk(KERN_INFO "btrfs: misc_deregister failed for control device\n");
1751a9218f6bSChris Mason }
1752a9218f6bSChris Mason 
175385965600SDavid Sterba static void btrfs_print_info(void)
175485965600SDavid Sterba {
175585965600SDavid Sterba 	printk(KERN_INFO "Btrfs loaded"
175685965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
175785965600SDavid Sterba 			", debug=on"
175885965600SDavid Sterba #endif
175985965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
176085965600SDavid Sterba 			", integrity-checker=on"
176185965600SDavid Sterba #endif
176285965600SDavid Sterba 			"\n");
176385965600SDavid Sterba }
176485965600SDavid Sterba 
17652e635a27SChris Mason static int __init init_btrfs_fs(void)
17662e635a27SChris Mason {
17672c90e5d6SChris Mason 	int err;
176858176a96SJosef Bacik 
176958176a96SJosef Bacik 	err = btrfs_init_sysfs();
177058176a96SJosef Bacik 	if (err)
177158176a96SJosef Bacik 		return err;
177258176a96SJosef Bacik 
1773143bede5SJeff Mahoney 	btrfs_init_compress();
1774d1310b2eSChris Mason 
1775261507a0SLi Zefan 	err = btrfs_init_cachep();
1776261507a0SLi Zefan 	if (err)
1777261507a0SLi Zefan 		goto free_compress;
1778261507a0SLi Zefan 
1779d1310b2eSChris Mason 	err = extent_io_init();
17802f4cbe64SWyatt Banks 	if (err)
17812f4cbe64SWyatt Banks 		goto free_cachep;
17822f4cbe64SWyatt Banks 
1783d1310b2eSChris Mason 	err = extent_map_init();
1784d1310b2eSChris Mason 	if (err)
1785d1310b2eSChris Mason 		goto free_extent_io;
1786d1310b2eSChris Mason 
17876352b91dSMiao Xie 	err = ordered_data_init();
17882f4cbe64SWyatt Banks 	if (err)
17892f4cbe64SWyatt Banks 		goto free_extent_map;
1790c8b97818SChris Mason 
17916352b91dSMiao Xie 	err = btrfs_delayed_inode_init();
17926352b91dSMiao Xie 	if (err)
17936352b91dSMiao Xie 		goto free_ordered_data;
17946352b91dSMiao Xie 
17959247f317SMiao Xie 	err = btrfs_auto_defrag_init();
179616cdcec7SMiao Xie 	if (err)
179716cdcec7SMiao Xie 		goto free_delayed_inode;
179816cdcec7SMiao Xie 
179978a6184aSMiao Xie 	err = btrfs_delayed_ref_init();
18009247f317SMiao Xie 	if (err)
18019247f317SMiao Xie 		goto free_auto_defrag;
18029247f317SMiao Xie 
180378a6184aSMiao Xie 	err = btrfs_interface_init();
180478a6184aSMiao Xie 	if (err)
180578a6184aSMiao Xie 		goto free_delayed_ref;
180678a6184aSMiao Xie 
1807a9218f6bSChris Mason 	err = register_filesystem(&btrfs_fs_type);
1808a9218f6bSChris Mason 	if (err)
1809a9218f6bSChris Mason 		goto unregister_ioctl;
1810b3c3da71SChris Mason 
1811e565d4b9SJan Schmidt 	btrfs_init_lockdep();
1812e565d4b9SJan Schmidt 
181385965600SDavid Sterba 	btrfs_print_info();
181474255aa0SJosef Bacik 	btrfs_test_free_space_cache();
181574255aa0SJosef Bacik 
18162f4cbe64SWyatt Banks 	return 0;
18172f4cbe64SWyatt Banks 
1818a9218f6bSChris Mason unregister_ioctl:
1819a9218f6bSChris Mason 	btrfs_interface_exit();
182078a6184aSMiao Xie free_delayed_ref:
182178a6184aSMiao Xie 	btrfs_delayed_ref_exit();
18229247f317SMiao Xie free_auto_defrag:
18239247f317SMiao Xie 	btrfs_auto_defrag_exit();
182416cdcec7SMiao Xie free_delayed_inode:
182516cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
18266352b91dSMiao Xie free_ordered_data:
18276352b91dSMiao Xie 	ordered_data_exit();
18282f4cbe64SWyatt Banks free_extent_map:
18292f4cbe64SWyatt Banks 	extent_map_exit();
1830d1310b2eSChris Mason free_extent_io:
1831d1310b2eSChris Mason 	extent_io_exit();
18322f4cbe64SWyatt Banks free_cachep:
18332f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
1834261507a0SLi Zefan free_compress:
1835261507a0SLi Zefan 	btrfs_exit_compress();
18362f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
18372c90e5d6SChris Mason 	return err;
18382e635a27SChris Mason }
18392e635a27SChris Mason 
18402e635a27SChris Mason static void __exit exit_btrfs_fs(void)
18412e635a27SChris Mason {
184239279cc3SChris Mason 	btrfs_destroy_cachep();
184378a6184aSMiao Xie 	btrfs_delayed_ref_exit();
18449247f317SMiao Xie 	btrfs_auto_defrag_exit();
184516cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
18466352b91dSMiao Xie 	ordered_data_exit();
1847a52d9a80SChris Mason 	extent_map_exit();
1848d1310b2eSChris Mason 	extent_io_exit();
1849a9218f6bSChris Mason 	btrfs_interface_exit();
18502e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
185158176a96SJosef Bacik 	btrfs_exit_sysfs();
18528a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
1853261507a0SLi Zefan 	btrfs_exit_compress();
18542e635a27SChris Mason }
18552e635a27SChris Mason 
18562e635a27SChris Mason module_init(init_btrfs_fs)
18572e635a27SChris Mason module_exit(exit_btrfs_fs)
18582e635a27SChris Mason 
18592e635a27SChris Mason MODULE_LICENSE("GPL");
1860