xref: /linux/fs/btrfs/super.c (revision d9b0d9ba04cf99abff9125b688c03e154598a644)
16cbd5570SChris Mason /*
26cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
194b82d6e4SYan #include <linux/blkdev.h>
202e635a27SChris Mason #include <linux/module.h>
21e20d96d6SChris Mason #include <linux/buffer_head.h>
222e635a27SChris Mason #include <linux/fs.h>
232e635a27SChris Mason #include <linux/pagemap.h>
242e635a27SChris Mason #include <linux/highmem.h>
252e635a27SChris Mason #include <linux/time.h>
262e635a27SChris Mason #include <linux/init.h>
27a9572a15SEric Paris #include <linux/seq_file.h>
282e635a27SChris Mason #include <linux/string.h>
292e635a27SChris Mason #include <linux/backing-dev.h>
304b82d6e4SYan #include <linux/mount.h>
31dee26a9fSChris Mason #include <linux/mpage.h>
3275dfe396SChris Mason #include <linux/swap.h>
3375dfe396SChris Mason #include <linux/writeback.h>
348fd17795SChris Mason #include <linux/statfs.h>
3508607c1bSChris Mason #include <linux/compat.h>
3695e05289SChris Mason #include <linux/parser.h>
37c59f8951SChris Mason #include <linux/ctype.h>
386da6abaeSChris Mason #include <linux/namei.h>
39a9218f6bSChris Mason #include <linux/miscdevice.h>
401bcbf313SQinghuang Feng #include <linux/magic.h>
415a0e3ad6STejun Heo #include <linux/slab.h>
4290a887c9SDan Magenheimer #include <linux/cleancache.h>
4322c44fe6SJosef Bacik #include <linux/ratelimit.h>
4455e301fdSFilipe Brandenburger #include <linux/btrfs.h>
4516cdcec7SMiao Xie #include "delayed-inode.h"
462e635a27SChris Mason #include "ctree.h"
47e20d96d6SChris Mason #include "disk-io.h"
48d5719762SChris Mason #include "transaction.h"
492c90e5d6SChris Mason #include "btrfs_inode.h"
503a686375SChris Mason #include "print-tree.h"
515103e947SJosef Bacik #include "xattr.h"
528a4b83ccSChris Mason #include "volumes.h"
53be6e8dc0SBalaji Rao #include "export.h"
54c8b97818SChris Mason #include "compression.h"
559c5085c1SJosef Bacik #include "rcu-string.h"
568dabb742SStefan Behrens #include "dev-replace.h"
5774255aa0SJosef Bacik #include "free-space-cache.h"
58b9e9a6cbSWang Shilong #include "backref.h"
59dc11dd5dSJosef Bacik #include "tests/btrfs-tests.h"
602e635a27SChris Mason 
611abe9b8aSliubo #define CREATE_TRACE_POINTS
621abe9b8aSliubo #include <trace/events/btrfs.h>
631abe9b8aSliubo 
64b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
65830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type;
66e20d96d6SChris Mason 
6708748810SDavid Sterba static const char *btrfs_decode_error(int errno)
68acce952bSliubo {
6908748810SDavid Sterba 	char *errstr = "unknown";
70acce952bSliubo 
71acce952bSliubo 	switch (errno) {
72acce952bSliubo 	case -EIO:
73acce952bSliubo 		errstr = "IO failure";
74acce952bSliubo 		break;
75acce952bSliubo 	case -ENOMEM:
76acce952bSliubo 		errstr = "Out of memory";
77acce952bSliubo 		break;
78acce952bSliubo 	case -EROFS:
79acce952bSliubo 		errstr = "Readonly filesystem";
80acce952bSliubo 		break;
818c342930SJeff Mahoney 	case -EEXIST:
828c342930SJeff Mahoney 		errstr = "Object already exists";
838c342930SJeff Mahoney 		break;
8494ef7280SDavid Sterba 	case -ENOSPC:
8594ef7280SDavid Sterba 		errstr = "No space left";
8694ef7280SDavid Sterba 		break;
8794ef7280SDavid Sterba 	case -ENOENT:
8894ef7280SDavid Sterba 		errstr = "No such entry";
8994ef7280SDavid Sterba 		break;
90acce952bSliubo 	}
91acce952bSliubo 
92acce952bSliubo 	return errstr;
93acce952bSliubo }
94acce952bSliubo 
95bbece8a3SDavid Sterba static void save_error_info(struct btrfs_fs_info *fs_info)
96acce952bSliubo {
97acce952bSliubo 	/*
98acce952bSliubo 	 * today we only save the error info into ram.  Long term we'll
99acce952bSliubo 	 * also send it down to the disk
100acce952bSliubo 	 */
10187533c47SMiao Xie 	set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
102acce952bSliubo }
103acce952bSliubo 
104acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */
105acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
106acce952bSliubo {
107acce952bSliubo 	struct super_block *sb = fs_info->sb;
108acce952bSliubo 
109acce952bSliubo 	if (sb->s_flags & MS_RDONLY)
110acce952bSliubo 		return;
111acce952bSliubo 
11287533c47SMiao Xie 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
113acce952bSliubo 		sb->s_flags |= MS_RDONLY;
114c2cf52ebSSimon Kirby 		btrfs_info(fs_info, "forced readonly");
1151acd6831SStefan Behrens 		/*
1161acd6831SStefan Behrens 		 * Note that a running device replace operation is not
1171acd6831SStefan Behrens 		 * canceled here although there is no way to update
1181acd6831SStefan Behrens 		 * the progress. It would add the risk of a deadlock,
1191acd6831SStefan Behrens 		 * therefore the canceling is ommited. The only penalty
1201acd6831SStefan Behrens 		 * is that some I/O remains active until the procedure
1211acd6831SStefan Behrens 		 * completes. The next time when the filesystem is
1221acd6831SStefan Behrens 		 * mounted writeable again, the device replace
1231acd6831SStefan Behrens 		 * operation continues.
1241acd6831SStefan Behrens 		 */
125acce952bSliubo 	}
126acce952bSliubo }
127acce952bSliubo 
128533574c6SJoe Perches #ifdef CONFIG_PRINTK
129acce952bSliubo /*
130acce952bSliubo  * __btrfs_std_error decodes expected errors from the caller and
131acce952bSliubo  * invokes the approciate error response.
132acce952bSliubo  */
133acce952bSliubo void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
1344da35113SJeff Mahoney 		       unsigned int line, int errno, const char *fmt, ...)
135acce952bSliubo {
136acce952bSliubo 	struct super_block *sb = fs_info->sb;
137acce952bSliubo 	const char *errstr;
138acce952bSliubo 
139acce952bSliubo 	/*
140acce952bSliubo 	 * Special case: if the error is EROFS, and we're already
141acce952bSliubo 	 * under MS_RDONLY, then it is safe here.
142acce952bSliubo 	 */
143acce952bSliubo 	if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
144acce952bSliubo   		return;
145acce952bSliubo 
14608748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
1474da35113SJeff Mahoney 	if (fmt) {
14837252a66SEric Sandeen 		struct va_format vaf;
14937252a66SEric Sandeen 		va_list args;
15037252a66SEric Sandeen 
15137252a66SEric Sandeen 		va_start(args, fmt);
15237252a66SEric Sandeen 		vaf.fmt = fmt;
15337252a66SEric Sandeen 		vaf.va = &args;
1544da35113SJeff Mahoney 
15508748810SDavid Sterba 		printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: errno=%d %s (%pV)\n",
15608748810SDavid Sterba 			sb->s_id, function, line, errno, errstr, &vaf);
15737252a66SEric Sandeen 		va_end(args);
1584da35113SJeff Mahoney 	} else {
15908748810SDavid Sterba 		printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: errno=%d %s\n",
16008748810SDavid Sterba 			sb->s_id, function, line, errno, errstr);
1614da35113SJeff Mahoney 	}
162acce952bSliubo 
1634da35113SJeff Mahoney 	/* Don't go through full error handling during mount */
1644da35113SJeff Mahoney 	save_error_info(fs_info);
165cf79ffb5SJosef Bacik 	if (sb->s_flags & MS_BORN)
166acce952bSliubo 		btrfs_handle_error(fs_info);
167acce952bSliubo }
1684da35113SJeff Mahoney 
169533574c6SJoe Perches static const char * const logtypes[] = {
1704da35113SJeff Mahoney 	"emergency",
1714da35113SJeff Mahoney 	"alert",
1724da35113SJeff Mahoney 	"critical",
1734da35113SJeff Mahoney 	"error",
1744da35113SJeff Mahoney 	"warning",
1754da35113SJeff Mahoney 	"notice",
1764da35113SJeff Mahoney 	"info",
1774da35113SJeff Mahoney 	"debug",
1784da35113SJeff Mahoney };
1794da35113SJeff Mahoney 
180c2cf52ebSSimon Kirby void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
1814da35113SJeff Mahoney {
1824da35113SJeff Mahoney 	struct super_block *sb = fs_info->sb;
1834da35113SJeff Mahoney 	char lvl[4];
1844da35113SJeff Mahoney 	struct va_format vaf;
1854da35113SJeff Mahoney 	va_list args;
1864da35113SJeff Mahoney 	const char *type = logtypes[4];
187533574c6SJoe Perches 	int kern_level;
1884da35113SJeff Mahoney 
1894da35113SJeff Mahoney 	va_start(args, fmt);
1904da35113SJeff Mahoney 
191533574c6SJoe Perches 	kern_level = printk_get_level(fmt);
192533574c6SJoe Perches 	if (kern_level) {
193533574c6SJoe Perches 		size_t size = printk_skip_level(fmt) - fmt;
194533574c6SJoe Perches 		memcpy(lvl, fmt,  size);
195533574c6SJoe Perches 		lvl[size] = '\0';
196533574c6SJoe Perches 		fmt += size;
197533574c6SJoe Perches 		type = logtypes[kern_level - '0'];
1984da35113SJeff Mahoney 	} else
1994da35113SJeff Mahoney 		*lvl = '\0';
2004da35113SJeff Mahoney 
2014da35113SJeff Mahoney 	vaf.fmt = fmt;
2024da35113SJeff Mahoney 	vaf.va = &args;
203533574c6SJoe Perches 
204c2cf52ebSSimon Kirby 	printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf);
205533574c6SJoe Perches 
206533574c6SJoe Perches 	va_end(args);
2074da35113SJeff Mahoney }
208acce952bSliubo 
209533574c6SJoe Perches #else
210533574c6SJoe Perches 
211533574c6SJoe Perches void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
212533574c6SJoe Perches 		       unsigned int line, int errno, const char *fmt, ...)
213533574c6SJoe Perches {
214533574c6SJoe Perches 	struct super_block *sb = fs_info->sb;
215533574c6SJoe Perches 
216533574c6SJoe Perches 	/*
217533574c6SJoe Perches 	 * Special case: if the error is EROFS, and we're already
218533574c6SJoe Perches 	 * under MS_RDONLY, then it is safe here.
219533574c6SJoe Perches 	 */
220533574c6SJoe Perches 	if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
221533574c6SJoe Perches 		return;
222533574c6SJoe Perches 
223533574c6SJoe Perches 	/* Don't go through full error handling during mount */
224533574c6SJoe Perches 	if (sb->s_flags & MS_BORN) {
225533574c6SJoe Perches 		save_error_info(fs_info);
226533574c6SJoe Perches 		btrfs_handle_error(fs_info);
227533574c6SJoe Perches 	}
228533574c6SJoe Perches }
229533574c6SJoe Perches #endif
230533574c6SJoe Perches 
2318c342930SJeff Mahoney /*
23249b25e05SJeff Mahoney  * We only mark the transaction aborted and then set the file system read-only.
23349b25e05SJeff Mahoney  * This will prevent new transactions from starting or trying to join this
23449b25e05SJeff Mahoney  * one.
23549b25e05SJeff Mahoney  *
23649b25e05SJeff Mahoney  * This means that error recovery at the call site is limited to freeing
23749b25e05SJeff Mahoney  * any local memory allocations and passing the error code up without
23849b25e05SJeff Mahoney  * further cleanup. The transaction should complete as it normally would
23949b25e05SJeff Mahoney  * in the call path but will return -EIO.
24049b25e05SJeff Mahoney  *
24149b25e05SJeff Mahoney  * We'll complete the cleanup in btrfs_end_transaction and
24249b25e05SJeff Mahoney  * btrfs_commit_transaction.
24349b25e05SJeff Mahoney  */
24449b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
24549b25e05SJeff Mahoney 			       struct btrfs_root *root, const char *function,
24649b25e05SJeff Mahoney 			       unsigned int line, int errno)
24749b25e05SJeff Mahoney {
24808748810SDavid Sterba 	/*
24908748810SDavid Sterba 	 * Report first abort since mount
25008748810SDavid Sterba 	 */
25108748810SDavid Sterba 	if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED,
25208748810SDavid Sterba 				&root->fs_info->fs_state)) {
25308748810SDavid Sterba 		WARN(1, KERN_DEBUG "btrfs: Transaction aborted (error %d)\n",
25408748810SDavid Sterba 				errno);
25508748810SDavid Sterba 	}
25649b25e05SJeff Mahoney 	trans->aborted = errno;
25749b25e05SJeff Mahoney 	/* Nothing used. The other threads that have joined this
25849b25e05SJeff Mahoney 	 * transaction may be able to continue. */
25949b25e05SJeff Mahoney 	if (!trans->blocks_used) {
26069ce977aSMiao Xie 		const char *errstr;
26169ce977aSMiao Xie 
26208748810SDavid Sterba 		errstr = btrfs_decode_error(errno);
263c2cf52ebSSimon Kirby 		btrfs_warn(root->fs_info,
264c2cf52ebSSimon Kirby 		           "%s:%d: Aborting unused transaction(%s).",
26569ce977aSMiao Xie 		           function, line, errstr);
26649b25e05SJeff Mahoney 		return;
26749b25e05SJeff Mahoney 	}
2688d25a086SMiao Xie 	ACCESS_ONCE(trans->transaction->aborted) = errno;
269501407aaSJosef Bacik 	/* Wake up anybody who may be waiting on this transaction */
270501407aaSJosef Bacik 	wake_up(&root->fs_info->transaction_wait);
271501407aaSJosef Bacik 	wake_up(&root->fs_info->transaction_blocked_wait);
27249b25e05SJeff Mahoney 	__btrfs_std_error(root->fs_info, function, line, errno, NULL);
27349b25e05SJeff Mahoney }
27449b25e05SJeff Mahoney /*
2758c342930SJeff Mahoney  * __btrfs_panic decodes unexpected, fatal errors from the caller,
2768c342930SJeff Mahoney  * issues an alert, and either panics or BUGs, depending on mount options.
2778c342930SJeff Mahoney  */
2788c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
2798c342930SJeff Mahoney 		   unsigned int line, int errno, const char *fmt, ...)
2808c342930SJeff Mahoney {
2818c342930SJeff Mahoney 	char *s_id = "<unknown>";
2828c342930SJeff Mahoney 	const char *errstr;
2838c342930SJeff Mahoney 	struct va_format vaf = { .fmt = fmt };
2848c342930SJeff Mahoney 	va_list args;
2858c342930SJeff Mahoney 
2868c342930SJeff Mahoney 	if (fs_info)
2878c342930SJeff Mahoney 		s_id = fs_info->sb->s_id;
2888c342930SJeff Mahoney 
2898c342930SJeff Mahoney 	va_start(args, fmt);
2908c342930SJeff Mahoney 	vaf.va = &args;
2918c342930SJeff Mahoney 
29208748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
293aa43a17cSEric Sandeen 	if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR))
29408748810SDavid Sterba 		panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
29508748810SDavid Sterba 			s_id, function, line, &vaf, errno, errstr);
2968c342930SJeff Mahoney 
29708748810SDavid Sterba 	printk(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
29808748810SDavid Sterba 	       s_id, function, line, &vaf, errno, errstr);
2998c342930SJeff Mahoney 	va_end(args);
3008c342930SJeff Mahoney 	/* Caller calls BUG() */
3018c342930SJeff Mahoney }
302e20d96d6SChris Mason 
303e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
304e20d96d6SChris Mason {
305815745cfSAl Viro 	(void)close_ctree(btrfs_sb(sb)->tree_root);
306aea52e19SAl Viro 	/* FIXME: need to fix VFS to return error? */
307aea52e19SAl Viro 	/* AV: return it _where_?  ->put_super() can be triggered by any number
308aea52e19SAl Viro 	 * of async events, up to and including delivery of SIGKILL to the
309aea52e19SAl Viro 	 * last process that kept it busy.  Or segfault in the aforementioned
310aea52e19SAl Viro 	 * process...  Whom would you report that to?
311aea52e19SAl Viro 	 */
312e20d96d6SChris Mason }
3132e635a27SChris Mason 
31495e05289SChris Mason enum {
31573f73415SJosef Bacik 	Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
316287a0ab9SJosef Bacik 	Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
317287a0ab9SJosef Bacik 	Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
318261507a0SLi Zefan 	Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
319261507a0SLi Zefan 	Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
32091435650SChris Mason 	Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed,
3219555c6c1SIlya Dryomov 	Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache,
3229555c6c1SIlya Dryomov 	Opt_no_space_cache, Opt_recovery, Opt_skip_balance,
32321adbd5cSStefan Behrens 	Opt_check_integrity, Opt_check_integrity_including_extent_data,
324f420ee1eSStefan Behrens 	Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree,
3258b87dc17SDavid Sterba 	Opt_commit_interval,
3269555c6c1SIlya Dryomov 	Opt_err,
32795e05289SChris Mason };
32895e05289SChris Mason 
32995e05289SChris Mason static match_table_t tokens = {
330dfe25020SChris Mason 	{Opt_degraded, "degraded"},
33195e05289SChris Mason 	{Opt_subvol, "subvol=%s"},
3321493381fSWang Shilong 	{Opt_subvolid, "subvolid=%s"},
33343e570b0SChristoph Hellwig 	{Opt_device, "device=%s"},
334b6cda9bcSChris Mason 	{Opt_nodatasum, "nodatasum"},
335be20aa9dSChris Mason 	{Opt_nodatacow, "nodatacow"},
33621ad10cfSChris Mason 	{Opt_nobarrier, "nobarrier"},
3376f568d35SChris Mason 	{Opt_max_inline, "max_inline=%s"},
3388f662a76SChris Mason 	{Opt_alloc_start, "alloc_start=%s"},
3394543df7eSChris Mason 	{Opt_thread_pool, "thread_pool=%d"},
340c8b97818SChris Mason 	{Opt_compress, "compress"},
341261507a0SLi Zefan 	{Opt_compress_type, "compress=%s"},
342a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
343261507a0SLi Zefan 	{Opt_compress_force_type, "compress-force=%s"},
344e18e4809SChris Mason 	{Opt_ssd, "ssd"},
345451d7585SChris Mason 	{Opt_ssd_spread, "ssd_spread"},
3463b30c22fSChris Mason 	{Opt_nossd, "nossd"},
34733268eafSJosef Bacik 	{Opt_noacl, "noacl"},
3483a5e1404SSage Weil 	{Opt_notreelog, "notreelog"},
349dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
35097e728d4SJosef Bacik 	{Opt_ratio, "metadata_ratio=%d"},
351e244a0aeSChristoph Hellwig 	{Opt_discard, "discard"},
3520af3d00bSJosef Bacik 	{Opt_space_cache, "space_cache"},
35388c2ba3bSJosef Bacik 	{Opt_clear_cache, "clear_cache"},
3544260f7c7SSage Weil 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
35591435650SChris Mason 	{Opt_enospc_debug, "enospc_debug"},
356e15d0542SXin Zhong 	{Opt_subvolrootid, "subvolrootid=%d"},
3574cb5300bSChris Mason 	{Opt_defrag, "autodefrag"},
3584b9465cbSChris Mason 	{Opt_inode_cache, "inode_cache"},
3598965593eSDavid Sterba 	{Opt_no_space_cache, "nospace_cache"},
360af31f5e5SChris Mason 	{Opt_recovery, "recovery"},
3619555c6c1SIlya Dryomov 	{Opt_skip_balance, "skip_balance"},
36221adbd5cSStefan Behrens 	{Opt_check_integrity, "check_int"},
36321adbd5cSStefan Behrens 	{Opt_check_integrity_including_extent_data, "check_int_data"},
36421adbd5cSStefan Behrens 	{Opt_check_integrity_print_mask, "check_int_print_mask=%d"},
365f420ee1eSStefan Behrens 	{Opt_rescan_uuid_tree, "rescan_uuid_tree"},
3668c342930SJeff Mahoney 	{Opt_fatal_errors, "fatal_errors=%s"},
3678b87dc17SDavid Sterba 	{Opt_commit_interval, "commit=%d"},
36833268eafSJosef Bacik 	{Opt_err, NULL},
36995e05289SChris Mason };
37095e05289SChris Mason 
371edf24abeSChristoph Hellwig /*
372edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
373edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
37449b25e05SJeff Mahoney  * XXX JDM: This needs to be cleaned up for remount.
375edf24abeSChristoph Hellwig  */
376edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options)
37795e05289SChris Mason {
378edf24abeSChristoph Hellwig 	struct btrfs_fs_info *info = root->fs_info;
37995e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
38073bc1876SJosef Bacik 	char *p, *num, *orig = NULL;
38173bc1876SJosef Bacik 	u64 cache_gen;
3824543df7eSChris Mason 	int intarg;
383a7a3f7caSSage Weil 	int ret = 0;
384261507a0SLi Zefan 	char *compress_type;
385261507a0SLi Zefan 	bool compress_force = false;
386b6cda9bcSChris Mason 
3876c41761fSDavid Sterba 	cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
38873bc1876SJosef Bacik 	if (cache_gen)
38973bc1876SJosef Bacik 		btrfs_set_opt(info->mount_opt, SPACE_CACHE);
39073bc1876SJosef Bacik 
39195e05289SChris Mason 	if (!options)
39273bc1876SJosef Bacik 		goto out;
39395e05289SChris Mason 
394be20aa9dSChris Mason 	/*
395be20aa9dSChris Mason 	 * strsep changes the string, duplicate it because parse_options
396be20aa9dSChris Mason 	 * gets called twice
397be20aa9dSChris Mason 	 */
398be20aa9dSChris Mason 	options = kstrdup(options, GFP_NOFS);
399be20aa9dSChris Mason 	if (!options)
400be20aa9dSChris Mason 		return -ENOMEM;
401be20aa9dSChris Mason 
402da495eccSJosef Bacik 	orig = options;
403be20aa9dSChris Mason 
40495e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
40595e05289SChris Mason 		int token;
40695e05289SChris Mason 		if (!*p)
40795e05289SChris Mason 			continue;
40895e05289SChris Mason 
40995e05289SChris Mason 		token = match_token(p, tokens, args);
41095e05289SChris Mason 		switch (token) {
411dfe25020SChris Mason 		case Opt_degraded:
412edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: allowing degraded mounts\n");
413dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
414dfe25020SChris Mason 			break;
41595e05289SChris Mason 		case Opt_subvol:
41673f73415SJosef Bacik 		case Opt_subvolid:
417e15d0542SXin Zhong 		case Opt_subvolrootid:
41843e570b0SChristoph Hellwig 		case Opt_device:
419edf24abeSChristoph Hellwig 			/*
42043e570b0SChristoph Hellwig 			 * These are parsed by btrfs_parse_early_options
421edf24abeSChristoph Hellwig 			 * and can be happily ignored here.
422edf24abeSChristoph Hellwig 			 */
42395e05289SChris Mason 			break;
424b6cda9bcSChris Mason 		case Opt_nodatasum:
425067c28adSChris Mason 			printk(KERN_INFO "btrfs: setting nodatasum\n");
426b6cda9bcSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
427be20aa9dSChris Mason 			break;
428be20aa9dSChris Mason 		case Opt_nodatacow:
429bedb2ccaSAndrei Popa 			if (!btrfs_test_opt(root, COMPRESS) ||
430bedb2ccaSAndrei Popa 				!btrfs_test_opt(root, FORCE_COMPRESS)) {
431bedb2ccaSAndrei Popa 					printk(KERN_INFO "btrfs: setting nodatacow, compression disabled\n");
432bedb2ccaSAndrei Popa 			} else {
433edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: setting nodatacow\n");
434bedb2ccaSAndrei Popa 			}
435bedb2ccaSAndrei Popa 			info->compress_type = BTRFS_COMPRESS_NONE;
436bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, COMPRESS);
437bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
438be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
439be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
440b6cda9bcSChris Mason 			break;
441a555f810SChris Mason 		case Opt_compress_force:
442261507a0SLi Zefan 		case Opt_compress_force_type:
443261507a0SLi Zefan 			compress_force = true;
4441c697d4aSEric Sandeen 			/* Fallthrough */
445261507a0SLi Zefan 		case Opt_compress:
446261507a0SLi Zefan 		case Opt_compress_type:
447261507a0SLi Zefan 			if (token == Opt_compress ||
448261507a0SLi Zefan 			    token == Opt_compress_force ||
449261507a0SLi Zefan 			    strcmp(args[0].from, "zlib") == 0) {
450261507a0SLi Zefan 				compress_type = "zlib";
451261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
452063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
453bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
454bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
455a6fa6faeSLi Zefan 			} else if (strcmp(args[0].from, "lzo") == 0) {
456a6fa6faeSLi Zefan 				compress_type = "lzo";
457a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
458063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
459bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
460bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
4612b0ce2c2SMitch Harder 				btrfs_set_fs_incompat(info, COMPRESS_LZO);
462063849eaSArnd Hannemann 			} else if (strncmp(args[0].from, "no", 2) == 0) {
463063849eaSArnd Hannemann 				compress_type = "no";
464063849eaSArnd Hannemann 				info->compress_type = BTRFS_COMPRESS_NONE;
465063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, COMPRESS);
466063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
467063849eaSArnd Hannemann 				compress_force = false;
468261507a0SLi Zefan 			} else {
469261507a0SLi Zefan 				ret = -EINVAL;
470261507a0SLi Zefan 				goto out;
471261507a0SLi Zefan 			}
472261507a0SLi Zefan 
473261507a0SLi Zefan 			if (compress_force) {
474261507a0SLi Zefan 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
475261507a0SLi Zefan 				pr_info("btrfs: force %s compression\n",
476261507a0SLi Zefan 					compress_type);
477261507a0SLi Zefan 			} else
478261507a0SLi Zefan 				pr_info("btrfs: use %s compression\n",
479261507a0SLi Zefan 					compress_type);
480a555f810SChris Mason 			break;
481e18e4809SChris Mason 		case Opt_ssd:
482edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
483e18e4809SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
484e18e4809SChris Mason 			break;
485451d7585SChris Mason 		case Opt_ssd_spread:
486451d7585SChris Mason 			printk(KERN_INFO "btrfs: use spread ssd "
487451d7585SChris Mason 			       "allocation scheme\n");
488451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
489451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD_SPREAD);
490451d7585SChris Mason 			break;
4913b30c22fSChris Mason 		case Opt_nossd:
492451d7585SChris Mason 			printk(KERN_INFO "btrfs: not using ssd allocation "
493451d7585SChris Mason 			       "scheme\n");
494c289811cSChris Mason 			btrfs_set_opt(info->mount_opt, NOSSD);
4953b30c22fSChris Mason 			btrfs_clear_opt(info->mount_opt, SSD);
496451d7585SChris Mason 			btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
4973b30c22fSChris Mason 			break;
49821ad10cfSChris Mason 		case Opt_nobarrier:
499edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: turning off barriers\n");
50021ad10cfSChris Mason 			btrfs_set_opt(info->mount_opt, NOBARRIER);
50121ad10cfSChris Mason 			break;
5024543df7eSChris Mason 		case Opt_thread_pool:
5032c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
5042c334e87SWang Shilong 			if (ret) {
5052c334e87SWang Shilong 				goto out;
5062c334e87SWang Shilong 			} else if (intarg > 0) {
5074543df7eSChris Mason 				info->thread_pool_size = intarg;
5082c334e87SWang Shilong 			} else {
5092c334e87SWang Shilong 				ret = -EINVAL;
5102c334e87SWang Shilong 				goto out;
5112c334e87SWang Shilong 			}
5124543df7eSChris Mason 			break;
5136f568d35SChris Mason 		case Opt_max_inline:
514edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
5156f568d35SChris Mason 			if (num) {
51691748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
5176f568d35SChris Mason 				kfree(num);
5186f568d35SChris Mason 
51915ada040SChris Mason 				if (info->max_inline) {
5206f568d35SChris Mason 					info->max_inline = max_t(u64,
52115ada040SChris Mason 						info->max_inline,
52215ada040SChris Mason 						root->sectorsize);
52315ada040SChris Mason 				}
524edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: max_inline at %llu\n",
525c1c9ff7cSGeert Uytterhoeven 					info->max_inline);
5262c334e87SWang Shilong 			} else {
5272c334e87SWang Shilong 				ret = -ENOMEM;
5282c334e87SWang Shilong 				goto out;
5296f568d35SChris Mason 			}
5306f568d35SChris Mason 			break;
5318f662a76SChris Mason 		case Opt_alloc_start:
532edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
5338f662a76SChris Mason 			if (num) {
534c018daecSMiao Xie 				mutex_lock(&info->chunk_mutex);
53591748467SAkinobu Mita 				info->alloc_start = memparse(num, NULL);
536c018daecSMiao Xie 				mutex_unlock(&info->chunk_mutex);
5378f662a76SChris Mason 				kfree(num);
538edf24abeSChristoph Hellwig 				printk(KERN_INFO
539edf24abeSChristoph Hellwig 					"btrfs: allocations start at %llu\n",
540c1c9ff7cSGeert Uytterhoeven 					info->alloc_start);
5412c334e87SWang Shilong 			} else {
5422c334e87SWang Shilong 				ret = -ENOMEM;
5432c334e87SWang Shilong 				goto out;
5448f662a76SChris Mason 			}
5458f662a76SChris Mason 			break;
54633268eafSJosef Bacik 		case Opt_noacl:
54733268eafSJosef Bacik 			root->fs_info->sb->s_flags &= ~MS_POSIXACL;
54833268eafSJosef Bacik 			break;
5493a5e1404SSage Weil 		case Opt_notreelog:
5503a5e1404SSage Weil 			printk(KERN_INFO "btrfs: disabling tree log\n");
5513a5e1404SSage Weil 			btrfs_set_opt(info->mount_opt, NOTREELOG);
5523a5e1404SSage Weil 			break;
553dccae999SSage Weil 		case Opt_flushoncommit:
554dccae999SSage Weil 			printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
555dccae999SSage Weil 			btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
556dccae999SSage Weil 			break;
55797e728d4SJosef Bacik 		case Opt_ratio:
5582c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
5592c334e87SWang Shilong 			if (ret) {
5602c334e87SWang Shilong 				goto out;
5612c334e87SWang Shilong 			} else if (intarg >= 0) {
56297e728d4SJosef Bacik 				info->metadata_ratio = intarg;
56397e728d4SJosef Bacik 				printk(KERN_INFO "btrfs: metadata ratio %d\n",
56497e728d4SJosef Bacik 				       info->metadata_ratio);
5652c334e87SWang Shilong 			} else {
5662c334e87SWang Shilong 				ret = -EINVAL;
5672c334e87SWang Shilong 				goto out;
56897e728d4SJosef Bacik 			}
56997e728d4SJosef Bacik 			break;
570e244a0aeSChristoph Hellwig 		case Opt_discard:
571e244a0aeSChristoph Hellwig 			btrfs_set_opt(info->mount_opt, DISCARD);
572e244a0aeSChristoph Hellwig 			break;
5730af3d00bSJosef Bacik 		case Opt_space_cache:
5740af3d00bSJosef Bacik 			btrfs_set_opt(info->mount_opt, SPACE_CACHE);
5750de90876SJosef Bacik 			break;
576f420ee1eSStefan Behrens 		case Opt_rescan_uuid_tree:
577f420ee1eSStefan Behrens 			btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
578f420ee1eSStefan Behrens 			break;
57973bc1876SJosef Bacik 		case Opt_no_space_cache:
58073bc1876SJosef Bacik 			printk(KERN_INFO "btrfs: disabling disk space caching\n");
58173bc1876SJosef Bacik 			btrfs_clear_opt(info->mount_opt, SPACE_CACHE);
58273bc1876SJosef Bacik 			break;
5834b9465cbSChris Mason 		case Opt_inode_cache:
5844b9465cbSChris Mason 			printk(KERN_INFO "btrfs: enabling inode map caching\n");
5854b9465cbSChris Mason 			btrfs_set_opt(info->mount_opt, INODE_MAP_CACHE);
5864b9465cbSChris Mason 			break;
58788c2ba3bSJosef Bacik 		case Opt_clear_cache:
58888c2ba3bSJosef Bacik 			printk(KERN_INFO "btrfs: force clearing of disk cache\n");
58988c2ba3bSJosef Bacik 			btrfs_set_opt(info->mount_opt, CLEAR_CACHE);
5900af3d00bSJosef Bacik 			break;
5914260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
5924260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
5934260f7c7SSage Weil 			break;
59491435650SChris Mason 		case Opt_enospc_debug:
59591435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
59691435650SChris Mason 			break;
5974cb5300bSChris Mason 		case Opt_defrag:
59848940662SDaniel J Blueman 			printk(KERN_INFO "btrfs: enabling auto defrag\n");
5994cb5300bSChris Mason 			btrfs_set_opt(info->mount_opt, AUTO_DEFRAG);
6004cb5300bSChris Mason 			break;
601af31f5e5SChris Mason 		case Opt_recovery:
60248940662SDaniel J Blueman 			printk(KERN_INFO "btrfs: enabling auto recovery\n");
603af31f5e5SChris Mason 			btrfs_set_opt(info->mount_opt, RECOVERY);
604af31f5e5SChris Mason 			break;
6059555c6c1SIlya Dryomov 		case Opt_skip_balance:
6069555c6c1SIlya Dryomov 			btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
6079555c6c1SIlya Dryomov 			break;
60821adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
60921adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
61021adbd5cSStefan Behrens 			printk(KERN_INFO "btrfs: enabling check integrity"
61121adbd5cSStefan Behrens 			       " including extent data\n");
61221adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt,
61321adbd5cSStefan Behrens 				      CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
61421adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
61521adbd5cSStefan Behrens 			break;
61621adbd5cSStefan Behrens 		case Opt_check_integrity:
61721adbd5cSStefan Behrens 			printk(KERN_INFO "btrfs: enabling check integrity\n");
61821adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
61921adbd5cSStefan Behrens 			break;
62021adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
6212c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
6222c334e87SWang Shilong 			if (ret) {
6232c334e87SWang Shilong 				goto out;
6242c334e87SWang Shilong 			} else if (intarg >= 0) {
62521adbd5cSStefan Behrens 				info->check_integrity_print_mask = intarg;
62621adbd5cSStefan Behrens 				printk(KERN_INFO "btrfs:"
62721adbd5cSStefan Behrens 				       " check_integrity_print_mask 0x%x\n",
62821adbd5cSStefan Behrens 				       info->check_integrity_print_mask);
6292c334e87SWang Shilong 			} else {
6302c334e87SWang Shilong 				ret = -EINVAL;
6312c334e87SWang Shilong 				goto out;
63221adbd5cSStefan Behrens 			}
63321adbd5cSStefan Behrens 			break;
63421adbd5cSStefan Behrens #else
63521adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
63621adbd5cSStefan Behrens 		case Opt_check_integrity:
63721adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
63821adbd5cSStefan Behrens 			printk(KERN_ERR "btrfs: support for check_integrity*"
63921adbd5cSStefan Behrens 			       " not compiled in!\n");
64021adbd5cSStefan Behrens 			ret = -EINVAL;
64121adbd5cSStefan Behrens 			goto out;
64221adbd5cSStefan Behrens #endif
6438c342930SJeff Mahoney 		case Opt_fatal_errors:
6448c342930SJeff Mahoney 			if (strcmp(args[0].from, "panic") == 0)
6458c342930SJeff Mahoney 				btrfs_set_opt(info->mount_opt,
6468c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
6478c342930SJeff Mahoney 			else if (strcmp(args[0].from, "bug") == 0)
6488c342930SJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
6498c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
6508c342930SJeff Mahoney 			else {
6518c342930SJeff Mahoney 				ret = -EINVAL;
6528c342930SJeff Mahoney 				goto out;
6538c342930SJeff Mahoney 			}
6548c342930SJeff Mahoney 			break;
6558b87dc17SDavid Sterba 		case Opt_commit_interval:
6568b87dc17SDavid Sterba 			intarg = 0;
6578b87dc17SDavid Sterba 			ret = match_int(&args[0], &intarg);
6588b87dc17SDavid Sterba 			if (ret < 0) {
6598b87dc17SDavid Sterba 				printk(KERN_ERR
6608b87dc17SDavid Sterba 					"btrfs: invalid commit interval\n");
6618b87dc17SDavid Sterba 				ret = -EINVAL;
6628b87dc17SDavid Sterba 				goto out;
6638b87dc17SDavid Sterba 			}
6648b87dc17SDavid Sterba 			if (intarg > 0) {
6658b87dc17SDavid Sterba 				if (intarg > 300) {
6668b87dc17SDavid Sterba 					printk(KERN_WARNING
6678b87dc17SDavid Sterba 					    "btrfs: excessive commit interval %d\n",
6688b87dc17SDavid Sterba 							intarg);
6698b87dc17SDavid Sterba 				}
6708b87dc17SDavid Sterba 				info->commit_interval = intarg;
6718b87dc17SDavid Sterba 			} else {
6728b87dc17SDavid Sterba 				printk(KERN_INFO
6738b87dc17SDavid Sterba 				    "btrfs: using default commit interval %ds\n",
6748b87dc17SDavid Sterba 				    BTRFS_DEFAULT_COMMIT_INTERVAL);
6758b87dc17SDavid Sterba 				info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
6768b87dc17SDavid Sterba 			}
6778b87dc17SDavid Sterba 			break;
678a7a3f7caSSage Weil 		case Opt_err:
679a7a3f7caSSage Weil 			printk(KERN_INFO "btrfs: unrecognized mount option "
680a7a3f7caSSage Weil 			       "'%s'\n", p);
681a7a3f7caSSage Weil 			ret = -EINVAL;
682a7a3f7caSSage Weil 			goto out;
68395e05289SChris Mason 		default:
684be20aa9dSChris Mason 			break;
68595e05289SChris Mason 		}
68695e05289SChris Mason 	}
687a7a3f7caSSage Weil out:
68873bc1876SJosef Bacik 	if (!ret && btrfs_test_opt(root, SPACE_CACHE))
68973bc1876SJosef Bacik 		printk(KERN_INFO "btrfs: disk space caching is enabled\n");
690da495eccSJosef Bacik 	kfree(orig);
691a7a3f7caSSage Weil 	return ret;
692edf24abeSChristoph Hellwig }
693edf24abeSChristoph Hellwig 
694edf24abeSChristoph Hellwig /*
695edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
696edf24abeSChristoph Hellwig  *
697edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
698edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
699edf24abeSChristoph Hellwig  */
70097288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags,
70173f73415SJosef Bacik 		void *holder, char **subvol_name, u64 *subvol_objectid,
7025e2a4b25SDavid Sterba 		struct btrfs_fs_devices **fs_devices)
703edf24abeSChristoph Hellwig {
704edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
70583c8c9bdSJeff Liu 	char *device_name, *opts, *orig, *p;
7061493381fSWang Shilong 	char *num = NULL;
707edf24abeSChristoph Hellwig 	int error = 0;
708edf24abeSChristoph Hellwig 
709edf24abeSChristoph Hellwig 	if (!options)
710830c4adbSJosef Bacik 		return 0;
711edf24abeSChristoph Hellwig 
712edf24abeSChristoph Hellwig 	/*
713edf24abeSChristoph Hellwig 	 * strsep changes the string, duplicate it because parse_options
714edf24abeSChristoph Hellwig 	 * gets called twice
715edf24abeSChristoph Hellwig 	 */
716edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
717edf24abeSChristoph Hellwig 	if (!opts)
718edf24abeSChristoph Hellwig 		return -ENOMEM;
7193f3d0bc0STero Roponen 	orig = opts;
720edf24abeSChristoph Hellwig 
721edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
722edf24abeSChristoph Hellwig 		int token;
723edf24abeSChristoph Hellwig 		if (!*p)
724edf24abeSChristoph Hellwig 			continue;
725edf24abeSChristoph Hellwig 
726edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
727edf24abeSChristoph Hellwig 		switch (token) {
728edf24abeSChristoph Hellwig 		case Opt_subvol:
729a90e8b6fSIlya Dryomov 			kfree(*subvol_name);
730edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
7312c334e87SWang Shilong 			if (!*subvol_name) {
7322c334e87SWang Shilong 				error = -ENOMEM;
7332c334e87SWang Shilong 				goto out;
7342c334e87SWang Shilong 			}
735edf24abeSChristoph Hellwig 			break;
73673f73415SJosef Bacik 		case Opt_subvolid:
7371493381fSWang Shilong 			num = match_strdup(&args[0]);
7381493381fSWang Shilong 			if (num) {
7391493381fSWang Shilong 				*subvol_objectid = memparse(num, NULL);
7401493381fSWang Shilong 				kfree(num);
7414849f01dSJosef Bacik 				/* we want the original fs_tree */
7421493381fSWang Shilong 				if (!*subvol_objectid)
7434849f01dSJosef Bacik 					*subvol_objectid =
7444849f01dSJosef Bacik 						BTRFS_FS_TREE_OBJECTID;
7452c334e87SWang Shilong 			} else {
7462c334e87SWang Shilong 				error = -EINVAL;
7472c334e87SWang Shilong 				goto out;
7484849f01dSJosef Bacik 			}
74973f73415SJosef Bacik 			break;
750e15d0542SXin Zhong 		case Opt_subvolrootid:
7515e2a4b25SDavid Sterba 			printk(KERN_WARNING
7525e2a4b25SDavid Sterba 				"btrfs: 'subvolrootid' mount option is deprecated and has no effect\n");
753e15d0542SXin Zhong 			break;
75443e570b0SChristoph Hellwig 		case Opt_device:
75583c8c9bdSJeff Liu 			device_name = match_strdup(&args[0]);
75683c8c9bdSJeff Liu 			if (!device_name) {
75783c8c9bdSJeff Liu 				error = -ENOMEM;
75883c8c9bdSJeff Liu 				goto out;
75983c8c9bdSJeff Liu 			}
76083c8c9bdSJeff Liu 			error = btrfs_scan_one_device(device_name,
76143e570b0SChristoph Hellwig 					flags, holder, fs_devices);
76283c8c9bdSJeff Liu 			kfree(device_name);
76343e570b0SChristoph Hellwig 			if (error)
764830c4adbSJosef Bacik 				goto out;
76543e570b0SChristoph Hellwig 			break;
766edf24abeSChristoph Hellwig 		default:
767edf24abeSChristoph Hellwig 			break;
768edf24abeSChristoph Hellwig 		}
769edf24abeSChristoph Hellwig 	}
770edf24abeSChristoph Hellwig 
771edf24abeSChristoph Hellwig out:
772830c4adbSJosef Bacik 	kfree(orig);
773edf24abeSChristoph Hellwig 	return error;
77495e05289SChris Mason }
77595e05289SChris Mason 
77673f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb,
77773f73415SJosef Bacik 				       u64 subvol_objectid)
77873f73415SJosef Bacik {
779815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
780815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
78173f73415SJosef Bacik 	struct btrfs_root *new_root;
78273f73415SJosef Bacik 	struct btrfs_dir_item *di;
78373f73415SJosef Bacik 	struct btrfs_path *path;
78473f73415SJosef Bacik 	struct btrfs_key location;
78573f73415SJosef Bacik 	struct inode *inode;
78673f73415SJosef Bacik 	u64 dir_id;
78773f73415SJosef Bacik 	int new = 0;
78873f73415SJosef Bacik 
78973f73415SJosef Bacik 	/*
79073f73415SJosef Bacik 	 * We have a specific subvol we want to mount, just setup location and
79173f73415SJosef Bacik 	 * go look up the root.
79273f73415SJosef Bacik 	 */
79373f73415SJosef Bacik 	if (subvol_objectid) {
79473f73415SJosef Bacik 		location.objectid = subvol_objectid;
79573f73415SJosef Bacik 		location.type = BTRFS_ROOT_ITEM_KEY;
79673f73415SJosef Bacik 		location.offset = (u64)-1;
79773f73415SJosef Bacik 		goto find_root;
79873f73415SJosef Bacik 	}
79973f73415SJosef Bacik 
80073f73415SJosef Bacik 	path = btrfs_alloc_path();
80173f73415SJosef Bacik 	if (!path)
80273f73415SJosef Bacik 		return ERR_PTR(-ENOMEM);
80373f73415SJosef Bacik 	path->leave_spinning = 1;
80473f73415SJosef Bacik 
80573f73415SJosef Bacik 	/*
80673f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
80773f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
80873f73415SJosef Bacik 	 * to mount.
80973f73415SJosef Bacik 	 */
810815745cfSAl Viro 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
81173f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
812b0839166SJulia Lawall 	if (IS_ERR(di)) {
813b0839166SJulia Lawall 		btrfs_free_path(path);
814fb4f6f91SDan Carpenter 		return ERR_CAST(di);
815b0839166SJulia Lawall 	}
81673f73415SJosef Bacik 	if (!di) {
81773f73415SJosef Bacik 		/*
81873f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
81973f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
82073f73415SJosef Bacik 		 * mount to root most subvolume.
82173f73415SJosef Bacik 		 */
82273f73415SJosef Bacik 		btrfs_free_path(path);
82373f73415SJosef Bacik 		dir_id = BTRFS_FIRST_FREE_OBJECTID;
824815745cfSAl Viro 		new_root = fs_info->fs_root;
82573f73415SJosef Bacik 		goto setup_root;
82673f73415SJosef Bacik 	}
82773f73415SJosef Bacik 
82873f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
82973f73415SJosef Bacik 	btrfs_free_path(path);
83073f73415SJosef Bacik 
83173f73415SJosef Bacik find_root:
832815745cfSAl Viro 	new_root = btrfs_read_fs_root_no_name(fs_info, &location);
83373f73415SJosef Bacik 	if (IS_ERR(new_root))
834d0b678cbSJulia Lawall 		return ERR_CAST(new_root);
83573f73415SJosef Bacik 
83673f73415SJosef Bacik 	dir_id = btrfs_root_dirid(&new_root->root_item);
83773f73415SJosef Bacik setup_root:
83873f73415SJosef Bacik 	location.objectid = dir_id;
83973f73415SJosef Bacik 	location.type = BTRFS_INODE_ITEM_KEY;
84073f73415SJosef Bacik 	location.offset = 0;
84173f73415SJosef Bacik 
84273f73415SJosef Bacik 	inode = btrfs_iget(sb, &location, new_root, &new);
8434cbd1149SDan Carpenter 	if (IS_ERR(inode))
8444cbd1149SDan Carpenter 		return ERR_CAST(inode);
84573f73415SJosef Bacik 
84673f73415SJosef Bacik 	/*
84773f73415SJosef Bacik 	 * If we're just mounting the root most subvol put the inode and return
84873f73415SJosef Bacik 	 * a reference to the dentry.  We will have already gotten a reference
84973f73415SJosef Bacik 	 * to the inode in btrfs_fill_super so we're good to go.
85073f73415SJosef Bacik 	 */
85173f73415SJosef Bacik 	if (!new && sb->s_root->d_inode == inode) {
85273f73415SJosef Bacik 		iput(inode);
85373f73415SJosef Bacik 		return dget(sb->s_root);
85473f73415SJosef Bacik 	}
85573f73415SJosef Bacik 
856ba5b8958SJosef Bacik 	return d_obtain_alias(inode);
85773f73415SJosef Bacik }
85873f73415SJosef Bacik 
8598a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
8608a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
8618a4b83ccSChris Mason 			    void *data, int silent)
8622e635a27SChris Mason {
8632e635a27SChris Mason 	struct inode *inode;
864815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
8655d4f98a2SYan Zheng 	struct btrfs_key key;
86639279cc3SChris Mason 	int err;
8672e635a27SChris Mason 
8682e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
8692e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
870e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
871af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
872be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
8735103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
8742e635a27SChris Mason 	sb->s_time_gran = 1;
8750eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
87633268eafSJosef Bacik 	sb->s_flags |= MS_POSIXACL;
87749cf6f45SChris Ball #endif
8780c4d2d95SJosef Bacik 	sb->s_flags |= MS_I_VERSION;
879ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
880ad2b2c80SAl Viro 	if (err) {
881e20d96d6SChris Mason 		printk("btrfs: open_ctree failed\n");
882ad2b2c80SAl Viro 		return err;
883e20d96d6SChris Mason 	}
884b888db2bSChris Mason 
8855d4f98a2SYan Zheng 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
8865d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
8875d4f98a2SYan Zheng 	key.offset = 0;
88898c7089cSAl Viro 	inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
8895d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
8905d4f98a2SYan Zheng 		err = PTR_ERR(inode);
89139279cc3SChris Mason 		goto fail_close;
89239279cc3SChris Mason 	}
8932e635a27SChris Mason 
89448fde701SAl Viro 	sb->s_root = d_make_root(inode);
89548fde701SAl Viro 	if (!sb->s_root) {
89639279cc3SChris Mason 		err = -ENOMEM;
89739279cc3SChris Mason 		goto fail_close;
8982e635a27SChris Mason 	}
89958176a96SJosef Bacik 
9006885f308SChris Mason 	save_mount_options(sb, data);
90190a887c9SDan Magenheimer 	cleancache_init_fs(sb);
90259553edfSAl Viro 	sb->s_flags |= MS_ACTIVE;
9032e635a27SChris Mason 	return 0;
9042e635a27SChris Mason 
90539279cc3SChris Mason fail_close:
906815745cfSAl Viro 	close_ctree(fs_info->tree_root);
907d5719762SChris Mason 	return err;
908d5719762SChris Mason }
909d5719762SChris Mason 
9106bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
911d5719762SChris Mason {
912d5719762SChris Mason 	struct btrfs_trans_handle *trans;
913815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
914815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
915df2ce34cSChris Mason 
9161abe9b8aSliubo 	trace_btrfs_sync_fs(wait);
9171abe9b8aSliubo 
918d561c025SChris Mason 	if (!wait) {
919815745cfSAl Viro 		filemap_flush(fs_info->btree_inode->i_mapping);
920df2ce34cSChris Mason 		return 0;
921d561c025SChris Mason 	}
922771ed689SChris Mason 
923f0de181cSJosef Bacik 	btrfs_wait_all_ordered_extents(fs_info);
924771ed689SChris Mason 
925d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
92660376ce4SJosef Bacik 	if (IS_ERR(trans)) {
927354aa0fbSMiao Xie 		/* no transaction, don't bother */
928354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
929bd7de2c9SJosef Bacik 			return 0;
93098d5dc13STsutomu Itoh 		return PTR_ERR(trans);
93160376ce4SJosef Bacik 	}
932bd7de2c9SJosef Bacik 	return btrfs_commit_transaction(trans, root);
933d5719762SChris Mason }
934d5719762SChris Mason 
93534c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
936a9572a15SEric Paris {
937815745cfSAl Viro 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
938815745cfSAl Viro 	struct btrfs_root *root = info->tree_root;
939200da64eSTsutomu Itoh 	char *compress_type;
940a9572a15SEric Paris 
941a9572a15SEric Paris 	if (btrfs_test_opt(root, DEGRADED))
942a9572a15SEric Paris 		seq_puts(seq, ",degraded");
943a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATASUM))
944a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
945a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATACOW))
946a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
947a9572a15SEric Paris 	if (btrfs_test_opt(root, NOBARRIER))
948a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
949a9572a15SEric Paris 	if (info->max_inline != 8192 * 1024)
950c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",max_inline=%llu", info->max_inline);
951a9572a15SEric Paris 	if (info->alloc_start != 0)
952c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",alloc_start=%llu", info->alloc_start);
953a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
954a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
955a9572a15SEric Paris 		seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
956200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, COMPRESS)) {
957200da64eSTsutomu Itoh 		if (info->compress_type == BTRFS_COMPRESS_ZLIB)
958200da64eSTsutomu Itoh 			compress_type = "zlib";
959200da64eSTsutomu Itoh 		else
960200da64eSTsutomu Itoh 			compress_type = "lzo";
961200da64eSTsutomu Itoh 		if (btrfs_test_opt(root, FORCE_COMPRESS))
962200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
963200da64eSTsutomu Itoh 		else
964200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
965200da64eSTsutomu Itoh 	}
966c289811cSChris Mason 	if (btrfs_test_opt(root, NOSSD))
967c289811cSChris Mason 		seq_puts(seq, ",nossd");
968451d7585SChris Mason 	if (btrfs_test_opt(root, SSD_SPREAD))
969451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
970451d7585SChris Mason 	else if (btrfs_test_opt(root, SSD))
971a9572a15SEric Paris 		seq_puts(seq, ",ssd");
9723a5e1404SSage Weil 	if (btrfs_test_opt(root, NOTREELOG))
9736b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
974dccae999SSage Weil 	if (btrfs_test_opt(root, FLUSHONCOMMIT))
9756b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
97620a5239aSMatthew Wilcox 	if (btrfs_test_opt(root, DISCARD))
97720a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
978a9572a15SEric Paris 	if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
979a9572a15SEric Paris 		seq_puts(seq, ",noacl");
980200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, SPACE_CACHE))
981200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
98273bc1876SJosef Bacik 	else
9838965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
984f420ee1eSStefan Behrens 	if (btrfs_test_opt(root, RESCAN_UUID_TREE))
985f420ee1eSStefan Behrens 		seq_puts(seq, ",rescan_uuid_tree");
986200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, CLEAR_CACHE))
987200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
988200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
989200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
9900942caa3SDavid Sterba 	if (btrfs_test_opt(root, ENOSPC_DEBUG))
9910942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
9920942caa3SDavid Sterba 	if (btrfs_test_opt(root, AUTO_DEFRAG))
9930942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
9940942caa3SDavid Sterba 	if (btrfs_test_opt(root, INODE_MAP_CACHE))
9950942caa3SDavid Sterba 		seq_puts(seq, ",inode_cache");
9969555c6c1SIlya Dryomov 	if (btrfs_test_opt(root, SKIP_BALANCE))
9979555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
9988507d216SWang Shilong 	if (btrfs_test_opt(root, RECOVERY))
9998507d216SWang Shilong 		seq_puts(seq, ",recovery");
10008507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
10018507d216SWang Shilong 	if (btrfs_test_opt(root, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
10028507d216SWang Shilong 		seq_puts(seq, ",check_int_data");
10038507d216SWang Shilong 	else if (btrfs_test_opt(root, CHECK_INTEGRITY))
10048507d216SWang Shilong 		seq_puts(seq, ",check_int");
10058507d216SWang Shilong 	if (info->check_integrity_print_mask)
10068507d216SWang Shilong 		seq_printf(seq, ",check_int_print_mask=%d",
10078507d216SWang Shilong 				info->check_integrity_print_mask);
10088507d216SWang Shilong #endif
10098507d216SWang Shilong 	if (info->metadata_ratio)
10108507d216SWang Shilong 		seq_printf(seq, ",metadata_ratio=%d",
10118507d216SWang Shilong 				info->metadata_ratio);
10128c342930SJeff Mahoney 	if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR))
10138c342930SJeff Mahoney 		seq_puts(seq, ",fatal_errors=panic");
10148b87dc17SDavid Sterba 	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
10158b87dc17SDavid Sterba 		seq_printf(seq, ",commit=%d", info->commit_interval);
1016a9572a15SEric Paris 	return 0;
1017a9572a15SEric Paris }
1018a9572a15SEric Paris 
1019a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
10202e635a27SChris Mason {
1021815745cfSAl Viro 	struct btrfs_fs_info *p = data;
1022815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(s);
10234b82d6e4SYan 
1024815745cfSAl Viro 	return fs_info->fs_devices == p->fs_devices;
10254b82d6e4SYan }
10264b82d6e4SYan 
1027450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
1028450ba0eaSJosef Bacik {
10296de1d09dSAl Viro 	int err = set_anon_super(s, data);
10306de1d09dSAl Viro 	if (!err)
1031450ba0eaSJosef Bacik 		s->s_fs_info = data;
10326de1d09dSAl Viro 	return err;
1033450ba0eaSJosef Bacik }
1034450ba0eaSJosef Bacik 
1035830c4adbSJosef Bacik /*
1036f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
1037f9d9ef62SDavid Sterba  */
1038f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
1039f9d9ef62SDavid Sterba {
1040f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1041f9d9ef62SDavid Sterba 		return 1;
1042f9d9ef62SDavid Sterba 	return 0;
1043f9d9ef62SDavid Sterba }
1044f9d9ef62SDavid Sterba 
1045f9d9ef62SDavid Sterba /*
1046830c4adbSJosef Bacik  * This will strip out the subvol=%s argument for an argument string and add
1047830c4adbSJosef Bacik  * subvolid=0 to make sure we get the actual tree root for path walking to the
1048830c4adbSJosef Bacik  * subvol we want.
1049830c4adbSJosef Bacik  */
1050830c4adbSJosef Bacik static char *setup_root_args(char *args)
1051830c4adbSJosef Bacik {
1052f60d16a8SJim Meyering 	unsigned len = strlen(args) + 2 + 1;
1053f60d16a8SJim Meyering 	char *src, *dst, *buf;
1054830c4adbSJosef Bacik 
1055830c4adbSJosef Bacik 	/*
1056f60d16a8SJim Meyering 	 * We need the same args as before, but with this substitution:
1057f60d16a8SJim Meyering 	 * s!subvol=[^,]+!subvolid=0!
1058830c4adbSJosef Bacik 	 *
1059f60d16a8SJim Meyering 	 * Since the replacement string is up to 2 bytes longer than the
1060f60d16a8SJim Meyering 	 * original, allocate strlen(args) + 2 + 1 bytes.
1061830c4adbSJosef Bacik 	 */
1062830c4adbSJosef Bacik 
1063f60d16a8SJim Meyering 	src = strstr(args, "subvol=");
1064830c4adbSJosef Bacik 	/* This shouldn't happen, but just in case.. */
1065f60d16a8SJim Meyering 	if (!src)
1066830c4adbSJosef Bacik 		return NULL;
1067f60d16a8SJim Meyering 
1068f60d16a8SJim Meyering 	buf = dst = kmalloc(len, GFP_NOFS);
1069f60d16a8SJim Meyering 	if (!buf)
1070f60d16a8SJim Meyering 		return NULL;
1071830c4adbSJosef Bacik 
1072830c4adbSJosef Bacik 	/*
1073f60d16a8SJim Meyering 	 * If the subvol= arg is not at the start of the string,
1074f60d16a8SJim Meyering 	 * copy whatever precedes it into buf.
1075830c4adbSJosef Bacik 	 */
1076f60d16a8SJim Meyering 	if (src != args) {
1077f60d16a8SJim Meyering 		*src++ = '\0';
1078f60d16a8SJim Meyering 		strcpy(buf, args);
1079f60d16a8SJim Meyering 		dst += strlen(args);
1080830c4adbSJosef Bacik 	}
1081830c4adbSJosef Bacik 
1082f60d16a8SJim Meyering 	strcpy(dst, "subvolid=0");
1083f60d16a8SJim Meyering 	dst += strlen("subvolid=0");
1084830c4adbSJosef Bacik 
1085830c4adbSJosef Bacik 	/*
1086f60d16a8SJim Meyering 	 * If there is a "," after the original subvol=... string,
1087f60d16a8SJim Meyering 	 * copy that suffix into our buffer.  Otherwise, we're done.
1088830c4adbSJosef Bacik 	 */
1089f60d16a8SJim Meyering 	src = strchr(src, ',');
1090f60d16a8SJim Meyering 	if (src)
1091f60d16a8SJim Meyering 		strcpy(dst, src);
1092830c4adbSJosef Bacik 
1093f60d16a8SJim Meyering 	return buf;
1094830c4adbSJosef Bacik }
1095830c4adbSJosef Bacik 
1096830c4adbSJosef Bacik static struct dentry *mount_subvol(const char *subvol_name, int flags,
1097830c4adbSJosef Bacik 				   const char *device_name, char *data)
1098830c4adbSJosef Bacik {
1099830c4adbSJosef Bacik 	struct dentry *root;
1100830c4adbSJosef Bacik 	struct vfsmount *mnt;
1101830c4adbSJosef Bacik 	char *newargs;
1102830c4adbSJosef Bacik 
1103830c4adbSJosef Bacik 	newargs = setup_root_args(data);
1104830c4adbSJosef Bacik 	if (!newargs)
1105830c4adbSJosef Bacik 		return ERR_PTR(-ENOMEM);
1106830c4adbSJosef Bacik 	mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name,
1107830c4adbSJosef Bacik 			     newargs);
1108830c4adbSJosef Bacik 	kfree(newargs);
1109830c4adbSJosef Bacik 	if (IS_ERR(mnt))
1110830c4adbSJosef Bacik 		return ERR_CAST(mnt);
1111830c4adbSJosef Bacik 
1112ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1113830c4adbSJosef Bacik 
1114ea441d11SAl Viro 	if (!IS_ERR(root) && !is_subvolume_inode(root->d_inode)) {
1115ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1116ea441d11SAl Viro 		dput(root);
1117ea441d11SAl Viro 		root = ERR_PTR(-EINVAL);
1118ea441d11SAl Viro 		deactivate_locked_super(s);
1119f9d9ef62SDavid Sterba 		printk(KERN_ERR "btrfs: '%s' is not a valid subvolume\n",
1120f9d9ef62SDavid Sterba 				subvol_name);
1121f9d9ef62SDavid Sterba 	}
1122f9d9ef62SDavid Sterba 
1123830c4adbSJosef Bacik 	return root;
1124830c4adbSJosef Bacik }
1125450ba0eaSJosef Bacik 
1126edf24abeSChristoph Hellwig /*
1127edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
1128edf24abeSChristoph Hellwig  *
1129edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
1130edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
1131edf24abeSChristoph Hellwig  */
1132061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1133306e16ceSDavid Sterba 		const char *device_name, void *data)
11344b82d6e4SYan {
11354b82d6e4SYan 	struct block_device *bdev = NULL;
11364b82d6e4SYan 	struct super_block *s;
11374b82d6e4SYan 	struct dentry *root;
11388a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
1139450ba0eaSJosef Bacik 	struct btrfs_fs_info *fs_info = NULL;
114097288f2cSChristoph Hellwig 	fmode_t mode = FMODE_READ;
114173f73415SJosef Bacik 	char *subvol_name = NULL;
114273f73415SJosef Bacik 	u64 subvol_objectid = 0;
11434b82d6e4SYan 	int error = 0;
11444b82d6e4SYan 
114597288f2cSChristoph Hellwig 	if (!(flags & MS_RDONLY))
114697288f2cSChristoph Hellwig 		mode |= FMODE_WRITE;
114797288f2cSChristoph Hellwig 
114897288f2cSChristoph Hellwig 	error = btrfs_parse_early_options(data, mode, fs_type,
114973f73415SJosef Bacik 					  &subvol_name, &subvol_objectid,
11505e2a4b25SDavid Sterba 					  &fs_devices);
1151f23c8af8SIlya Dryomov 	if (error) {
1152f23c8af8SIlya Dryomov 		kfree(subvol_name);
1153061dbc6bSAl Viro 		return ERR_PTR(error);
1154f23c8af8SIlya Dryomov 	}
1155edf24abeSChristoph Hellwig 
1156830c4adbSJosef Bacik 	if (subvol_name) {
1157830c4adbSJosef Bacik 		root = mount_subvol(subvol_name, flags, device_name, data);
1158830c4adbSJosef Bacik 		kfree(subvol_name);
1159830c4adbSJosef Bacik 		return root;
1160830c4adbSJosef Bacik 	}
1161830c4adbSJosef Bacik 
1162306e16ceSDavid Sterba 	error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
11638a4b83ccSChris Mason 	if (error)
1164830c4adbSJosef Bacik 		return ERR_PTR(error);
11654b82d6e4SYan 
1166450ba0eaSJosef Bacik 	/*
1167450ba0eaSJosef Bacik 	 * Setup a dummy root and fs_info for test/set super.  This is because
1168450ba0eaSJosef Bacik 	 * we don't actually fill this stuff out until open_ctree, but we need
1169450ba0eaSJosef Bacik 	 * it for searching for existing supers, so this lets us do that and
1170450ba0eaSJosef Bacik 	 * then open_ctree will properly initialize everything later.
1171450ba0eaSJosef Bacik 	 */
1172450ba0eaSJosef Bacik 	fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
117304d21a24SIlya Dryomov 	if (!fs_info)
117404d21a24SIlya Dryomov 		return ERR_PTR(-ENOMEM);
117504d21a24SIlya Dryomov 
1176450ba0eaSJosef Bacik 	fs_info->fs_devices = fs_devices;
1177450ba0eaSJosef Bacik 
11786c41761fSDavid Sterba 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
11796c41761fSDavid Sterba 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
11806c41761fSDavid Sterba 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
11816c41761fSDavid Sterba 		error = -ENOMEM;
118204d21a24SIlya Dryomov 		goto error_fs_info;
118304d21a24SIlya Dryomov 	}
118404d21a24SIlya Dryomov 
118504d21a24SIlya Dryomov 	error = btrfs_open_devices(fs_devices, mode, fs_type);
118604d21a24SIlya Dryomov 	if (error)
118704d21a24SIlya Dryomov 		goto error_fs_info;
118804d21a24SIlya Dryomov 
118904d21a24SIlya Dryomov 	if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
119004d21a24SIlya Dryomov 		error = -EACCES;
11916c41761fSDavid Sterba 		goto error_close_devices;
11926c41761fSDavid Sterba 	}
11936c41761fSDavid Sterba 
1194dfe25020SChris Mason 	bdev = fs_devices->latest_bdev;
11959249e17fSDavid Howells 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC,
11969249e17fSDavid Howells 		 fs_info);
1197830c4adbSJosef Bacik 	if (IS_ERR(s)) {
1198830c4adbSJosef Bacik 		error = PTR_ERR(s);
1199830c4adbSJosef Bacik 		goto error_close_devices;
1200830c4adbSJosef Bacik 	}
12014b82d6e4SYan 
12024b82d6e4SYan 	if (s->s_root) {
12032b82032cSYan Zheng 		btrfs_close_devices(fs_devices);
12046c41761fSDavid Sterba 		free_fs_info(fs_info);
120559553edfSAl Viro 		if ((flags ^ s->s_flags) & MS_RDONLY)
120659553edfSAl Viro 			error = -EBUSY;
12074b82d6e4SYan 	} else {
12084b82d6e4SYan 		char b[BDEVNAME_SIZE];
12094b82d6e4SYan 
12104b82d6e4SYan 		strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
1211815745cfSAl Viro 		btrfs_sb(s)->bdev_holder = fs_type;
12128a4b83ccSChris Mason 		error = btrfs_fill_super(s, fs_devices, data,
12138a4b83ccSChris Mason 					 flags & MS_SILENT ? 1 : 0);
12144b82d6e4SYan 	}
12154b82d6e4SYan 
121659553edfSAl Viro 	root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error);
121759553edfSAl Viro 	if (IS_ERR(root))
1218e15d0542SXin Zhong 		deactivate_locked_super(s);
12194b82d6e4SYan 
1220061dbc6bSAl Viro 	return root;
12214b82d6e4SYan 
1222c146afadSYan Zheng error_close_devices:
12238a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
122404d21a24SIlya Dryomov error_fs_info:
12256c41761fSDavid Sterba 	free_fs_info(fs_info);
1226061dbc6bSAl Viro 	return ERR_PTR(error);
12274b82d6e4SYan }
12282e635a27SChris Mason 
12290d2450abSSergei Trofimovich static void btrfs_set_max_workers(struct btrfs_workers *workers, int new_limit)
12300d2450abSSergei Trofimovich {
12310d2450abSSergei Trofimovich 	spin_lock_irq(&workers->lock);
12320d2450abSSergei Trofimovich 	workers->max_workers = new_limit;
12330d2450abSSergei Trofimovich 	spin_unlock_irq(&workers->lock);
12340d2450abSSergei Trofimovich }
12350d2450abSSergei Trofimovich 
12360d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
12370d2450abSSergei Trofimovich 				     int new_pool_size, int old_pool_size)
12380d2450abSSergei Trofimovich {
12390d2450abSSergei Trofimovich 	if (new_pool_size == old_pool_size)
12400d2450abSSergei Trofimovich 		return;
12410d2450abSSergei Trofimovich 
12420d2450abSSergei Trofimovich 	fs_info->thread_pool_size = new_pool_size;
12430d2450abSSergei Trofimovich 
12440d2450abSSergei Trofimovich 	printk(KERN_INFO "btrfs: resize thread pool %d -> %d\n",
12450d2450abSSergei Trofimovich 	       old_pool_size, new_pool_size);
12460d2450abSSergei Trofimovich 
12470d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->generic_worker, new_pool_size);
12480d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->workers, new_pool_size);
12490d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->delalloc_workers, new_pool_size);
12500d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->submit_workers, new_pool_size);
12510d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->caching_workers, new_pool_size);
12520d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->fixup_workers, new_pool_size);
12530d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->endio_workers, new_pool_size);
12540d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->endio_meta_workers, new_pool_size);
12550d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->endio_meta_write_workers, new_pool_size);
12560d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->endio_write_workers, new_pool_size);
12570d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->endio_freespace_worker, new_pool_size);
12580d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->delayed_workers, new_pool_size);
12590d2450abSSergei Trofimovich 	btrfs_set_max_workers(&fs_info->readahead_workers, new_pool_size);
1260ff023aacSStefan Behrens 	btrfs_set_max_workers(&fs_info->scrub_wr_completion_workers,
1261ff023aacSStefan Behrens 			      new_pool_size);
12620d2450abSSergei Trofimovich }
12630d2450abSSergei Trofimovich 
1264f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
1265dc81cdc5SMiao Xie {
1266dc81cdc5SMiao Xie 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1267f42a34b2SMiao Xie }
1268dc81cdc5SMiao Xie 
1269f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1270f42a34b2SMiao Xie 				       unsigned long old_opts, int flags)
1271f42a34b2SMiao Xie {
1272dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1273dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1274dc81cdc5SMiao Xie 	     (flags & MS_RDONLY))) {
1275dc81cdc5SMiao Xie 		/* wait for any defraggers to finish */
1276dc81cdc5SMiao Xie 		wait_event(fs_info->transaction_wait,
1277dc81cdc5SMiao Xie 			   (atomic_read(&fs_info->defrag_running) == 0));
1278dc81cdc5SMiao Xie 		if (flags & MS_RDONLY)
1279dc81cdc5SMiao Xie 			sync_filesystem(fs_info->sb);
1280dc81cdc5SMiao Xie 	}
1281dc81cdc5SMiao Xie }
1282dc81cdc5SMiao Xie 
1283dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1284dc81cdc5SMiao Xie 					 unsigned long old_opts)
1285dc81cdc5SMiao Xie {
1286dc81cdc5SMiao Xie 	/*
1287dc81cdc5SMiao Xie 	 * We need cleanup all defragable inodes if the autodefragment is
1288dc81cdc5SMiao Xie 	 * close or the fs is R/O.
1289dc81cdc5SMiao Xie 	 */
1290dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1291dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1292dc81cdc5SMiao Xie 	     (fs_info->sb->s_flags & MS_RDONLY))) {
1293dc81cdc5SMiao Xie 		btrfs_cleanup_defrag_inodes(fs_info);
1294dc81cdc5SMiao Xie 	}
1295dc81cdc5SMiao Xie 
1296dc81cdc5SMiao Xie 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1297dc81cdc5SMiao Xie }
1298dc81cdc5SMiao Xie 
1299c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1300c146afadSYan Zheng {
1301815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1302815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
130349b25e05SJeff Mahoney 	unsigned old_flags = sb->s_flags;
130449b25e05SJeff Mahoney 	unsigned long old_opts = fs_info->mount_opt;
130549b25e05SJeff Mahoney 	unsigned long old_compress_type = fs_info->compress_type;
130649b25e05SJeff Mahoney 	u64 old_max_inline = fs_info->max_inline;
130749b25e05SJeff Mahoney 	u64 old_alloc_start = fs_info->alloc_start;
130849b25e05SJeff Mahoney 	int old_thread_pool_size = fs_info->thread_pool_size;
130949b25e05SJeff Mahoney 	unsigned int old_metadata_ratio = fs_info->metadata_ratio;
1310c146afadSYan Zheng 	int ret;
1311c146afadSYan Zheng 
1312f42a34b2SMiao Xie 	btrfs_remount_prepare(fs_info);
1313dc81cdc5SMiao Xie 
1314b288052eSChris Mason 	ret = btrfs_parse_options(root, data);
131549b25e05SJeff Mahoney 	if (ret) {
131649b25e05SJeff Mahoney 		ret = -EINVAL;
131749b25e05SJeff Mahoney 		goto restore;
131849b25e05SJeff Mahoney 	}
1319b288052eSChris Mason 
1320f42a34b2SMiao Xie 	btrfs_remount_begin(fs_info, old_opts, *flags);
13210d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
13220d2450abSSergei Trofimovich 		fs_info->thread_pool_size, old_thread_pool_size);
13230d2450abSSergei Trofimovich 
1324c146afadSYan Zheng 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
1325dc81cdc5SMiao Xie 		goto out;
1326c146afadSYan Zheng 
1327c146afadSYan Zheng 	if (*flags & MS_RDONLY) {
13288dabb742SStefan Behrens 		/*
13298dabb742SStefan Behrens 		 * this also happens on 'umount -rf' or on shutdown, when
13308dabb742SStefan Behrens 		 * the filesystem is busy.
13318dabb742SStefan Behrens 		 */
1332361c093dSStefan Behrens 
1333361c093dSStefan Behrens 		/* wait for the uuid_scan task to finish */
1334361c093dSStefan Behrens 		down(&fs_info->uuid_tree_rescan_sem);
1335361c093dSStefan Behrens 		/* avoid complains from lockdep et al. */
1336361c093dSStefan Behrens 		up(&fs_info->uuid_tree_rescan_sem);
1337361c093dSStefan Behrens 
1338c146afadSYan Zheng 		sb->s_flags |= MS_RDONLY;
1339c146afadSYan Zheng 
13408dabb742SStefan Behrens 		btrfs_dev_replace_suspend_for_unmount(fs_info);
13418dabb742SStefan Behrens 		btrfs_scrub_cancel(fs_info);
1342061594efSMiao Xie 		btrfs_pause_balance(fs_info);
13438dabb742SStefan Behrens 
1344c146afadSYan Zheng 		ret = btrfs_commit_super(root);
134549b25e05SJeff Mahoney 		if (ret)
134649b25e05SJeff Mahoney 			goto restore;
1347c146afadSYan Zheng 	} else {
13486ef3de9cSDavid Sterba 		if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
13496ef3de9cSDavid Sterba 			btrfs_err(fs_info,
13506ef3de9cSDavid Sterba 				"Remounting read-write after error is not allowed\n");
13516ef3de9cSDavid Sterba 			ret = -EINVAL;
13526ef3de9cSDavid Sterba 			goto restore;
13536ef3de9cSDavid Sterba 		}
13548a3db184SSergei Trofimovich 		if (fs_info->fs_devices->rw_devices == 0) {
135549b25e05SJeff Mahoney 			ret = -EACCES;
135649b25e05SJeff Mahoney 			goto restore;
13578a3db184SSergei Trofimovich 		}
13582b82032cSYan Zheng 
1359292fd7fcSStefan Behrens 		if (fs_info->fs_devices->missing_devices >
1360292fd7fcSStefan Behrens 		     fs_info->num_tolerated_disk_barrier_failures &&
1361292fd7fcSStefan Behrens 		    !(*flags & MS_RDONLY)) {
1362292fd7fcSStefan Behrens 			printk(KERN_WARNING
1363292fd7fcSStefan Behrens 			       "Btrfs: too many missing devices, writeable remount is not allowed\n");
1364292fd7fcSStefan Behrens 			ret = -EACCES;
1365292fd7fcSStefan Behrens 			goto restore;
1366292fd7fcSStefan Behrens 		}
1367292fd7fcSStefan Behrens 
13688a3db184SSergei Trofimovich 		if (btrfs_super_log_root(fs_info->super_copy) != 0) {
136949b25e05SJeff Mahoney 			ret = -EINVAL;
137049b25e05SJeff Mahoney 			goto restore;
13718a3db184SSergei Trofimovich 		}
1372c146afadSYan Zheng 
1373815745cfSAl Viro 		ret = btrfs_cleanup_fs_roots(fs_info);
137449b25e05SJeff Mahoney 		if (ret)
137549b25e05SJeff Mahoney 			goto restore;
1376c146afadSYan Zheng 
1377d68fc57bSYan, Zheng 		/* recover relocation */
1378d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
137949b25e05SJeff Mahoney 		if (ret)
138049b25e05SJeff Mahoney 			goto restore;
1381c146afadSYan Zheng 
13822b6ba629SIlya Dryomov 		ret = btrfs_resume_balance_async(fs_info);
13832b6ba629SIlya Dryomov 		if (ret)
13842b6ba629SIlya Dryomov 			goto restore;
13852b6ba629SIlya Dryomov 
13868dabb742SStefan Behrens 		ret = btrfs_resume_dev_replace_async(fs_info);
13878dabb742SStefan Behrens 		if (ret) {
13888dabb742SStefan Behrens 			pr_warn("btrfs: failed to resume dev_replace\n");
13898dabb742SStefan Behrens 			goto restore;
13908dabb742SStefan Behrens 		}
139194aebfb2SJosef Bacik 
139294aebfb2SJosef Bacik 		if (!fs_info->uuid_root) {
139394aebfb2SJosef Bacik 			pr_info("btrfs: creating UUID tree\n");
139494aebfb2SJosef Bacik 			ret = btrfs_create_uuid_tree(fs_info);
139594aebfb2SJosef Bacik 			if (ret) {
139694aebfb2SJosef Bacik 				pr_warn("btrfs: failed to create the uuid tree"
139794aebfb2SJosef Bacik 					"%d\n", ret);
139894aebfb2SJosef Bacik 				goto restore;
139994aebfb2SJosef Bacik 			}
140094aebfb2SJosef Bacik 		}
1401c146afadSYan Zheng 		sb->s_flags &= ~MS_RDONLY;
1402c146afadSYan Zheng 	}
1403dc81cdc5SMiao Xie out:
1404dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
1405c146afadSYan Zheng 	return 0;
140649b25e05SJeff Mahoney 
140749b25e05SJeff Mahoney restore:
140849b25e05SJeff Mahoney 	/* We've hit an error - don't reset MS_RDONLY */
140949b25e05SJeff Mahoney 	if (sb->s_flags & MS_RDONLY)
141049b25e05SJeff Mahoney 		old_flags |= MS_RDONLY;
141149b25e05SJeff Mahoney 	sb->s_flags = old_flags;
141249b25e05SJeff Mahoney 	fs_info->mount_opt = old_opts;
141349b25e05SJeff Mahoney 	fs_info->compress_type = old_compress_type;
141449b25e05SJeff Mahoney 	fs_info->max_inline = old_max_inline;
1415c018daecSMiao Xie 	mutex_lock(&fs_info->chunk_mutex);
141649b25e05SJeff Mahoney 	fs_info->alloc_start = old_alloc_start;
1417c018daecSMiao Xie 	mutex_unlock(&fs_info->chunk_mutex);
14180d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
14190d2450abSSergei Trofimovich 		old_thread_pool_size, fs_info->thread_pool_size);
142049b25e05SJeff Mahoney 	fs_info->metadata_ratio = old_metadata_ratio;
1421dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
142249b25e05SJeff Mahoney 	return ret;
1423c146afadSYan Zheng }
1424c146afadSYan Zheng 
1425bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
1426bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1,
1427bcd53741SArne Jansen 				       const void *dev_info2)
1428bcd53741SArne Jansen {
1429bcd53741SArne Jansen 	if (((struct btrfs_device_info *)dev_info1)->max_avail >
1430bcd53741SArne Jansen 	    ((struct btrfs_device_info *)dev_info2)->max_avail)
1431bcd53741SArne Jansen 		return -1;
1432bcd53741SArne Jansen 	else if (((struct btrfs_device_info *)dev_info1)->max_avail <
1433bcd53741SArne Jansen 		 ((struct btrfs_device_info *)dev_info2)->max_avail)
1434bcd53741SArne Jansen 		return 1;
1435bcd53741SArne Jansen 	else
1436bcd53741SArne Jansen 	return 0;
1437bcd53741SArne Jansen }
1438bcd53741SArne Jansen 
1439bcd53741SArne Jansen /*
1440bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
1441bcd53741SArne Jansen  * is stored.(Descending Sort)
1442bcd53741SArne Jansen  */
1443bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
1444bcd53741SArne Jansen 					struct btrfs_device_info *devices,
1445bcd53741SArne Jansen 					size_t nr_devices)
1446bcd53741SArne Jansen {
1447bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1448bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
1449bcd53741SArne Jansen }
1450bcd53741SArne Jansen 
14516d07bcecSMiao Xie /*
14526d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
14536d07bcecSMiao Xie  * file data.
14546d07bcecSMiao Xie  */
14556d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
14566d07bcecSMiao Xie {
14576d07bcecSMiao Xie 	struct btrfs_fs_info *fs_info = root->fs_info;
14586d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
14596d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
14606d07bcecSMiao Xie 	struct btrfs_device *device;
14616d07bcecSMiao Xie 	u64 skip_space;
14626d07bcecSMiao Xie 	u64 type;
14636d07bcecSMiao Xie 	u64 avail_space;
14646d07bcecSMiao Xie 	u64 used_space;
14656d07bcecSMiao Xie 	u64 min_stripe_size;
146639fb26c3SMiao Xie 	int min_stripes = 1, num_stripes = 1;
14676d07bcecSMiao Xie 	int i = 0, nr_devices;
14686d07bcecSMiao Xie 	int ret;
14696d07bcecSMiao Xie 
1470b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
14716d07bcecSMiao Xie 	BUG_ON(!nr_devices);
14726d07bcecSMiao Xie 
1473*d9b0d9baSDulshani Gunawardhana 	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
14746d07bcecSMiao Xie 			       GFP_NOFS);
14756d07bcecSMiao Xie 	if (!devices_info)
14766d07bcecSMiao Xie 		return -ENOMEM;
14776d07bcecSMiao Xie 
14786d07bcecSMiao Xie 	/* calc min stripe number for data space alloction */
14796d07bcecSMiao Xie 	type = btrfs_get_alloc_profile(root, 1);
148039fb26c3SMiao Xie 	if (type & BTRFS_BLOCK_GROUP_RAID0) {
14816d07bcecSMiao Xie 		min_stripes = 2;
148239fb26c3SMiao Xie 		num_stripes = nr_devices;
148339fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID1) {
14846d07bcecSMiao Xie 		min_stripes = 2;
148539fb26c3SMiao Xie 		num_stripes = 2;
148639fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID10) {
14876d07bcecSMiao Xie 		min_stripes = 4;
148839fb26c3SMiao Xie 		num_stripes = 4;
148939fb26c3SMiao Xie 	}
14906d07bcecSMiao Xie 
14916d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_DUP)
14926d07bcecSMiao Xie 		min_stripe_size = 2 * BTRFS_STRIPE_LEN;
14936d07bcecSMiao Xie 	else
14946d07bcecSMiao Xie 		min_stripe_size = BTRFS_STRIPE_LEN;
14956d07bcecSMiao Xie 
1496b772a86eSLi Zefan 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
149763a212abSStefan Behrens 		if (!device->in_fs_metadata || !device->bdev ||
149863a212abSStefan Behrens 		    device->is_tgtdev_for_dev_replace)
14996d07bcecSMiao Xie 			continue;
15006d07bcecSMiao Xie 
15016d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
15026d07bcecSMiao Xie 
15036d07bcecSMiao Xie 		/* align with stripe_len */
15046d07bcecSMiao Xie 		do_div(avail_space, BTRFS_STRIPE_LEN);
15056d07bcecSMiao Xie 		avail_space *= BTRFS_STRIPE_LEN;
15066d07bcecSMiao Xie 
15076d07bcecSMiao Xie 		/*
15086d07bcecSMiao Xie 		 * In order to avoid overwritting the superblock on the drive,
15096d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
15106d07bcecSMiao Xie 		 * allocation.
15116d07bcecSMiao Xie 		 */
15126d07bcecSMiao Xie 		skip_space = 1024 * 1024;
15136d07bcecSMiao Xie 
15146d07bcecSMiao Xie 		/* user can set the offset in fs_info->alloc_start. */
15156d07bcecSMiao Xie 		if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
15166d07bcecSMiao Xie 		    device->total_bytes)
15176d07bcecSMiao Xie 			skip_space = max(fs_info->alloc_start, skip_space);
15186d07bcecSMiao Xie 
15196d07bcecSMiao Xie 		/*
15206d07bcecSMiao Xie 		 * btrfs can not use the free space in [0, skip_space - 1],
15216d07bcecSMiao Xie 		 * we must subtract it from the total. In order to implement
15226d07bcecSMiao Xie 		 * it, we account the used space in this range first.
15236d07bcecSMiao Xie 		 */
15246d07bcecSMiao Xie 		ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
15256d07bcecSMiao Xie 						     &used_space);
15266d07bcecSMiao Xie 		if (ret) {
15276d07bcecSMiao Xie 			kfree(devices_info);
15286d07bcecSMiao Xie 			return ret;
15296d07bcecSMiao Xie 		}
15306d07bcecSMiao Xie 
15316d07bcecSMiao Xie 		/* calc the free space in [0, skip_space - 1] */
15326d07bcecSMiao Xie 		skip_space -= used_space;
15336d07bcecSMiao Xie 
15346d07bcecSMiao Xie 		/*
15356d07bcecSMiao Xie 		 * we can use the free space in [0, skip_space - 1], subtract
15366d07bcecSMiao Xie 		 * it from the total.
15376d07bcecSMiao Xie 		 */
15386d07bcecSMiao Xie 		if (avail_space && avail_space >= skip_space)
15396d07bcecSMiao Xie 			avail_space -= skip_space;
15406d07bcecSMiao Xie 		else
15416d07bcecSMiao Xie 			avail_space = 0;
15426d07bcecSMiao Xie 
15436d07bcecSMiao Xie 		if (avail_space < min_stripe_size)
15446d07bcecSMiao Xie 			continue;
15456d07bcecSMiao Xie 
15466d07bcecSMiao Xie 		devices_info[i].dev = device;
15476d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
15486d07bcecSMiao Xie 
15496d07bcecSMiao Xie 		i++;
15506d07bcecSMiao Xie 	}
15516d07bcecSMiao Xie 
15526d07bcecSMiao Xie 	nr_devices = i;
15536d07bcecSMiao Xie 
15546d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
15556d07bcecSMiao Xie 
15566d07bcecSMiao Xie 	i = nr_devices - 1;
15576d07bcecSMiao Xie 	avail_space = 0;
15586d07bcecSMiao Xie 	while (nr_devices >= min_stripes) {
155939fb26c3SMiao Xie 		if (num_stripes > nr_devices)
156039fb26c3SMiao Xie 			num_stripes = nr_devices;
156139fb26c3SMiao Xie 
15626d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
15636d07bcecSMiao Xie 			int j;
15646d07bcecSMiao Xie 			u64 alloc_size;
15656d07bcecSMiao Xie 
156639fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
15676d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
156839fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
15696d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
15706d07bcecSMiao Xie 		}
15716d07bcecSMiao Xie 		i--;
15726d07bcecSMiao Xie 		nr_devices--;
15736d07bcecSMiao Xie 	}
15746d07bcecSMiao Xie 
15756d07bcecSMiao Xie 	kfree(devices_info);
15766d07bcecSMiao Xie 	*free_bytes = avail_space;
15776d07bcecSMiao Xie 	return 0;
15786d07bcecSMiao Xie }
15796d07bcecSMiao Xie 
15808fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
15818fd17795SChris Mason {
1582815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
1583815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
1584815745cfSAl Viro 	struct list_head *head = &fs_info->space_info;
1585bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
1586bd4d1088SJosef Bacik 	u64 total_used = 0;
15876d07bcecSMiao Xie 	u64 total_free_data = 0;
1588db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
1589815745cfSAl Viro 	__be32 *fsid = (__be32 *)fs_info->fsid;
15906d07bcecSMiao Xie 	int ret;
15918fd17795SChris Mason 
15926d07bcecSMiao Xie 	/* holding chunk_muext to avoid allocating new chunks */
1593815745cfSAl Viro 	mutex_lock(&fs_info->chunk_mutex);
1594bd4d1088SJosef Bacik 	rcu_read_lock();
159589a55897SJosef Bacik 	list_for_each_entry_rcu(found, head, list) {
15966d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
15976d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
15986d07bcecSMiao Xie 			total_free_data -=
15996d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
16006d07bcecSMiao Xie 		}
16016d07bcecSMiao Xie 
1602b742bb82SYan, Zheng 		total_used += found->disk_used;
160389a55897SJosef Bacik 	}
1604bd4d1088SJosef Bacik 	rcu_read_unlock();
1605bd4d1088SJosef Bacik 
16068fd17795SChris Mason 	buf->f_namelen = BTRFS_NAME_LEN;
1607db94535dSChris Mason 	buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
1608bd4d1088SJosef Bacik 	buf->f_bfree = buf->f_blocks - (total_used >> bits);
16098fd17795SChris Mason 	buf->f_bsize = dentry->d_sb->s_blocksize;
16108fd17795SChris Mason 	buf->f_type = BTRFS_SUPER_MAGIC;
16116d07bcecSMiao Xie 	buf->f_bavail = total_free_data;
1612815745cfSAl Viro 	ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data);
16136d07bcecSMiao Xie 	if (ret) {
1614815745cfSAl Viro 		mutex_unlock(&fs_info->chunk_mutex);
16156d07bcecSMiao Xie 		return ret;
16166d07bcecSMiao Xie 	}
16176d07bcecSMiao Xie 	buf->f_bavail += total_free_data;
16186d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
1619815745cfSAl Viro 	mutex_unlock(&fs_info->chunk_mutex);
1620d397712bSChris Mason 
16219d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
16229d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
16239d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
16249d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
16259d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
162632d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
162732d48fa1SDavid Woodhouse 	buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
162832d48fa1SDavid Woodhouse 	buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
162932d48fa1SDavid Woodhouse 
16308fd17795SChris Mason 	return 0;
16318fd17795SChris Mason }
1632b5133862SChris Mason 
1633aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
1634aea52e19SAl Viro {
1635815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1636aea52e19SAl Viro 	kill_anon_super(sb);
1637aea52e19SAl Viro 	free_fs_info(fs_info);
1638aea52e19SAl Viro }
1639aea52e19SAl Viro 
16402e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
16412e635a27SChris Mason 	.owner		= THIS_MODULE,
16422e635a27SChris Mason 	.name		= "btrfs",
1643061dbc6bSAl Viro 	.mount		= btrfs_mount,
1644aea52e19SAl Viro 	.kill_sb	= btrfs_kill_super,
16452e635a27SChris Mason 	.fs_flags	= FS_REQUIRES_DEV,
16462e635a27SChris Mason };
16477f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs");
1648a9218f6bSChris Mason 
1649d352ac68SChris Mason /*
1650d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
1651d352ac68SChris Mason  */
16528a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
16538a4b83ccSChris Mason 				unsigned long arg)
16548a4b83ccSChris Mason {
16558a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
16568a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
1657c071fcfdSChris Mason 	int ret = -ENOTTY;
16588a4b83ccSChris Mason 
1659e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1660e441d54dSChris Mason 		return -EPERM;
1661e441d54dSChris Mason 
1662dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
1663dae7b665SLi Zefan 	if (IS_ERR(vol))
1664dae7b665SLi Zefan 		return PTR_ERR(vol);
1665c071fcfdSChris Mason 
16668a4b83ccSChris Mason 	switch (cmd) {
16678a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
166897288f2cSChristoph Hellwig 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
16698a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
16708a4b83ccSChris Mason 		break;
167102db0844SJosef Bacik 	case BTRFS_IOC_DEVICES_READY:
167202db0844SJosef Bacik 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
167302db0844SJosef Bacik 					    &btrfs_fs_type, &fs_devices);
167402db0844SJosef Bacik 		if (ret)
167502db0844SJosef Bacik 			break;
167602db0844SJosef Bacik 		ret = !(fs_devices->num_devices == fs_devices->total_devices);
167702db0844SJosef Bacik 		break;
16788a4b83ccSChris Mason 	}
1679dae7b665SLi Zefan 
16808a4b83ccSChris Mason 	kfree(vol);
1681f819d837SLinda Knippers 	return ret;
16828a4b83ccSChris Mason }
16838a4b83ccSChris Mason 
16840176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
1685ed0dab6bSYan {
1686354aa0fbSMiao Xie 	struct btrfs_trans_handle *trans;
1687354aa0fbSMiao Xie 	struct btrfs_root *root = btrfs_sb(sb)->tree_root;
1688354aa0fbSMiao Xie 
1689d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
1690354aa0fbSMiao Xie 	if (IS_ERR(trans)) {
1691354aa0fbSMiao Xie 		/* no transaction, don't bother */
1692354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
16930176260fSLinus Torvalds 			return 0;
1694354aa0fbSMiao Xie 		return PTR_ERR(trans);
1695354aa0fbSMiao Xie 	}
1696354aa0fbSMiao Xie 	return btrfs_commit_transaction(trans, root);
1697ed0dab6bSYan }
1698ed0dab6bSYan 
16990176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb)
1700ed0dab6bSYan {
17010176260fSLinus Torvalds 	return 0;
1702ed0dab6bSYan }
17032e635a27SChris Mason 
17049c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
17059c5085c1SJosef Bacik {
17069c5085c1SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
17079c5085c1SJosef Bacik 	struct btrfs_fs_devices *cur_devices;
17089c5085c1SJosef Bacik 	struct btrfs_device *dev, *first_dev = NULL;
17099c5085c1SJosef Bacik 	struct list_head *head;
17109c5085c1SJosef Bacik 	struct rcu_string *name;
17119c5085c1SJosef Bacik 
17129c5085c1SJosef Bacik 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
17139c5085c1SJosef Bacik 	cur_devices = fs_info->fs_devices;
17149c5085c1SJosef Bacik 	while (cur_devices) {
17159c5085c1SJosef Bacik 		head = &cur_devices->devices;
17169c5085c1SJosef Bacik 		list_for_each_entry(dev, head, dev_list) {
1717aa9ddcd4SJosef Bacik 			if (dev->missing)
1718aa9ddcd4SJosef Bacik 				continue;
17199c5085c1SJosef Bacik 			if (!first_dev || dev->devid < first_dev->devid)
17209c5085c1SJosef Bacik 				first_dev = dev;
17219c5085c1SJosef Bacik 		}
17229c5085c1SJosef Bacik 		cur_devices = cur_devices->seed;
17239c5085c1SJosef Bacik 	}
17249c5085c1SJosef Bacik 
17259c5085c1SJosef Bacik 	if (first_dev) {
17269c5085c1SJosef Bacik 		rcu_read_lock();
17279c5085c1SJosef Bacik 		name = rcu_dereference(first_dev->name);
17289c5085c1SJosef Bacik 		seq_escape(m, name->str, " \t\n\\");
17299c5085c1SJosef Bacik 		rcu_read_unlock();
17309c5085c1SJosef Bacik 	} else {
17319c5085c1SJosef Bacik 		WARN_ON(1);
17329c5085c1SJosef Bacik 	}
17339c5085c1SJosef Bacik 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
17349c5085c1SJosef Bacik 	return 0;
17359c5085c1SJosef Bacik }
17369c5085c1SJosef Bacik 
1737b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
173876dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
1739bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
1740e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
1741d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
1742a9572a15SEric Paris 	.show_options	= btrfs_show_options,
17439c5085c1SJosef Bacik 	.show_devname	= btrfs_show_devname,
17444730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
17452c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
17462c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
17478fd17795SChris Mason 	.statfs		= btrfs_statfs,
1748c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
17490176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
17500176260fSLinus Torvalds 	.unfreeze_fs	= btrfs_unfreeze,
1751e20d96d6SChris Mason };
1752a9218f6bSChris Mason 
1753a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
1754a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
1755a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
1756a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
17576038f373SArnd Bergmann 	.llseek = noop_llseek,
1758a9218f6bSChris Mason };
1759a9218f6bSChris Mason 
1760a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
1761578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
1762a9218f6bSChris Mason 	.name		= "btrfs-control",
1763a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
1764a9218f6bSChris Mason };
1765a9218f6bSChris Mason 
1766578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
1767578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
1768578454ffSKay Sievers 
1769a9218f6bSChris Mason static int btrfs_interface_init(void)
1770a9218f6bSChris Mason {
1771a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
1772a9218f6bSChris Mason }
1773a9218f6bSChris Mason 
1774b2950863SChristoph Hellwig static void btrfs_interface_exit(void)
1775a9218f6bSChris Mason {
1776a9218f6bSChris Mason 	if (misc_deregister(&btrfs_misc) < 0)
177748940662SDaniel J Blueman 		printk(KERN_INFO "btrfs: misc_deregister failed for control device\n");
1778a9218f6bSChris Mason }
1779a9218f6bSChris Mason 
178085965600SDavid Sterba static void btrfs_print_info(void)
178185965600SDavid Sterba {
178285965600SDavid Sterba 	printk(KERN_INFO "Btrfs loaded"
178385965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
178485965600SDavid Sterba 			", debug=on"
178585965600SDavid Sterba #endif
178679556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT
178779556c3dSStefan Behrens 			", assert=on"
178879556c3dSStefan Behrens #endif
178985965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
179085965600SDavid Sterba 			", integrity-checker=on"
179185965600SDavid Sterba #endif
179285965600SDavid Sterba 			"\n");
179385965600SDavid Sterba }
179485965600SDavid Sterba 
1795dc11dd5dSJosef Bacik static int btrfs_run_sanity_tests(void)
1796dc11dd5dSJosef Bacik {
179706ea65a3SJosef Bacik 	int ret;
179806ea65a3SJosef Bacik 
1799294e30feSJosef Bacik 	ret = btrfs_init_test_fs();
180006ea65a3SJosef Bacik 	if (ret)
180106ea65a3SJosef Bacik 		return ret;
1802294e30feSJosef Bacik 
1803294e30feSJosef Bacik 	ret = btrfs_test_free_space_cache();
1804294e30feSJosef Bacik 	if (ret)
1805294e30feSJosef Bacik 		goto out;
1806294e30feSJosef Bacik 	ret = btrfs_test_extent_buffer_operations();
1807294e30feSJosef Bacik 	if (ret)
1808294e30feSJosef Bacik 		goto out;
1809294e30feSJosef Bacik 	ret = btrfs_test_extent_io();
1810aaedb55bSJosef Bacik 	if (ret)
1811aaedb55bSJosef Bacik 		goto out;
1812aaedb55bSJosef Bacik 	ret = btrfs_test_inodes();
1813294e30feSJosef Bacik out:
1814294e30feSJosef Bacik 	btrfs_destroy_test_fs();
1815294e30feSJosef Bacik 	return ret;
1816dc11dd5dSJosef Bacik }
1817dc11dd5dSJosef Bacik 
18182e635a27SChris Mason static int __init init_btrfs_fs(void)
18192e635a27SChris Mason {
18202c90e5d6SChris Mason 	int err;
182158176a96SJosef Bacik 
182258176a96SJosef Bacik 	err = btrfs_init_sysfs();
182358176a96SJosef Bacik 	if (err)
182458176a96SJosef Bacik 		return err;
182558176a96SJosef Bacik 
1826143bede5SJeff Mahoney 	btrfs_init_compress();
1827d1310b2eSChris Mason 
1828261507a0SLi Zefan 	err = btrfs_init_cachep();
1829261507a0SLi Zefan 	if (err)
1830261507a0SLi Zefan 		goto free_compress;
1831261507a0SLi Zefan 
1832d1310b2eSChris Mason 	err = extent_io_init();
18332f4cbe64SWyatt Banks 	if (err)
18342f4cbe64SWyatt Banks 		goto free_cachep;
18352f4cbe64SWyatt Banks 
1836d1310b2eSChris Mason 	err = extent_map_init();
1837d1310b2eSChris Mason 	if (err)
1838d1310b2eSChris Mason 		goto free_extent_io;
1839d1310b2eSChris Mason 
18406352b91dSMiao Xie 	err = ordered_data_init();
18412f4cbe64SWyatt Banks 	if (err)
18422f4cbe64SWyatt Banks 		goto free_extent_map;
1843c8b97818SChris Mason 
18446352b91dSMiao Xie 	err = btrfs_delayed_inode_init();
18456352b91dSMiao Xie 	if (err)
18466352b91dSMiao Xie 		goto free_ordered_data;
18476352b91dSMiao Xie 
18489247f317SMiao Xie 	err = btrfs_auto_defrag_init();
184916cdcec7SMiao Xie 	if (err)
185016cdcec7SMiao Xie 		goto free_delayed_inode;
185116cdcec7SMiao Xie 
185278a6184aSMiao Xie 	err = btrfs_delayed_ref_init();
18539247f317SMiao Xie 	if (err)
18549247f317SMiao Xie 		goto free_auto_defrag;
18559247f317SMiao Xie 
1856b9e9a6cbSWang Shilong 	err = btrfs_prelim_ref_init();
1857b9e9a6cbSWang Shilong 	if (err)
1858b9e9a6cbSWang Shilong 		goto free_prelim_ref;
1859b9e9a6cbSWang Shilong 
186078a6184aSMiao Xie 	err = btrfs_interface_init();
186178a6184aSMiao Xie 	if (err)
186278a6184aSMiao Xie 		goto free_delayed_ref;
186378a6184aSMiao Xie 
1864e565d4b9SJan Schmidt 	btrfs_init_lockdep();
1865e565d4b9SJan Schmidt 
186685965600SDavid Sterba 	btrfs_print_info();
1867dc11dd5dSJosef Bacik 
1868dc11dd5dSJosef Bacik 	err = btrfs_run_sanity_tests();
1869dc11dd5dSJosef Bacik 	if (err)
1870dc11dd5dSJosef Bacik 		goto unregister_ioctl;
1871dc11dd5dSJosef Bacik 
1872dc11dd5dSJosef Bacik 	err = register_filesystem(&btrfs_fs_type);
1873dc11dd5dSJosef Bacik 	if (err)
1874dc11dd5dSJosef Bacik 		goto unregister_ioctl;
187574255aa0SJosef Bacik 
18762f4cbe64SWyatt Banks 	return 0;
18772f4cbe64SWyatt Banks 
1878a9218f6bSChris Mason unregister_ioctl:
1879a9218f6bSChris Mason 	btrfs_interface_exit();
1880b9e9a6cbSWang Shilong free_prelim_ref:
1881b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
188278a6184aSMiao Xie free_delayed_ref:
188378a6184aSMiao Xie 	btrfs_delayed_ref_exit();
18849247f317SMiao Xie free_auto_defrag:
18859247f317SMiao Xie 	btrfs_auto_defrag_exit();
188616cdcec7SMiao Xie free_delayed_inode:
188716cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
18886352b91dSMiao Xie free_ordered_data:
18896352b91dSMiao Xie 	ordered_data_exit();
18902f4cbe64SWyatt Banks free_extent_map:
18912f4cbe64SWyatt Banks 	extent_map_exit();
1892d1310b2eSChris Mason free_extent_io:
1893d1310b2eSChris Mason 	extent_io_exit();
18942f4cbe64SWyatt Banks free_cachep:
18952f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
1896261507a0SLi Zefan free_compress:
1897261507a0SLi Zefan 	btrfs_exit_compress();
18982f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
18992c90e5d6SChris Mason 	return err;
19002e635a27SChris Mason }
19012e635a27SChris Mason 
19022e635a27SChris Mason static void __exit exit_btrfs_fs(void)
19032e635a27SChris Mason {
190439279cc3SChris Mason 	btrfs_destroy_cachep();
190578a6184aSMiao Xie 	btrfs_delayed_ref_exit();
19069247f317SMiao Xie 	btrfs_auto_defrag_exit();
190716cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
1908b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
19096352b91dSMiao Xie 	ordered_data_exit();
1910a52d9a80SChris Mason 	extent_map_exit();
1911d1310b2eSChris Mason 	extent_io_exit();
1912a9218f6bSChris Mason 	btrfs_interface_exit();
19132e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
191458176a96SJosef Bacik 	btrfs_exit_sysfs();
19158a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
1916261507a0SLi Zefan 	btrfs_exit_compress();
19172e635a27SChris Mason }
19182e635a27SChris Mason 
19192e635a27SChris Mason module_init(init_btrfs_fs)
19202e635a27SChris Mason module_exit(exit_btrfs_fs)
19212e635a27SChris Mason 
19222e635a27SChris Mason MODULE_LICENSE("GPL");
1923