xref: /linux/fs/btrfs/super.c (revision e565d4b962948e70f37f417603caf131a773621b)
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>
43830c4adbSJosef Bacik #include <linux/mnt_namespace.h>
4422c44fe6SJosef Bacik #include <linux/ratelimit.h>
454b4e25f2SChris Mason #include "compat.h"
4616cdcec7SMiao Xie #include "delayed-inode.h"
472e635a27SChris Mason #include "ctree.h"
48e20d96d6SChris Mason #include "disk-io.h"
49d5719762SChris Mason #include "transaction.h"
502c90e5d6SChris Mason #include "btrfs_inode.h"
51c5739bbaSChris Mason #include "ioctl.h"
523a686375SChris Mason #include "print-tree.h"
535103e947SJosef Bacik #include "xattr.h"
548a4b83ccSChris Mason #include "volumes.h"
55b3c3da71SChris Mason #include "version.h"
56be6e8dc0SBalaji Rao #include "export.h"
57c8b97818SChris Mason #include "compression.h"
582e635a27SChris Mason 
591abe9b8aSliubo #define CREATE_TRACE_POINTS
601abe9b8aSliubo #include <trace/events/btrfs.h>
611abe9b8aSliubo 
62b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
63830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type;
64e20d96d6SChris Mason 
65acce952bSliubo static const char *btrfs_decode_error(struct btrfs_fs_info *fs_info, int errno,
66acce952bSliubo 				      char nbuf[16])
67acce952bSliubo {
68acce952bSliubo 	char *errstr = NULL;
69acce952bSliubo 
70acce952bSliubo 	switch (errno) {
71acce952bSliubo 	case -EIO:
72acce952bSliubo 		errstr = "IO failure";
73acce952bSliubo 		break;
74acce952bSliubo 	case -ENOMEM:
75acce952bSliubo 		errstr = "Out of memory";
76acce952bSliubo 		break;
77acce952bSliubo 	case -EROFS:
78acce952bSliubo 		errstr = "Readonly filesystem";
79acce952bSliubo 		break;
80acce952bSliubo 	default:
81acce952bSliubo 		if (nbuf) {
82acce952bSliubo 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
83acce952bSliubo 				errstr = nbuf;
84acce952bSliubo 		}
85acce952bSliubo 		break;
86acce952bSliubo 	}
87acce952bSliubo 
88acce952bSliubo 	return errstr;
89acce952bSliubo }
90acce952bSliubo 
91acce952bSliubo static void __save_error_info(struct btrfs_fs_info *fs_info)
92acce952bSliubo {
93acce952bSliubo 	/*
94acce952bSliubo 	 * today we only save the error info into ram.  Long term we'll
95acce952bSliubo 	 * also send it down to the disk
96acce952bSliubo 	 */
97acce952bSliubo 	fs_info->fs_state = BTRFS_SUPER_FLAG_ERROR;
98acce952bSliubo }
99acce952bSliubo 
100acce952bSliubo /* NOTE:
101acce952bSliubo  *	We move write_super stuff at umount in order to avoid deadlock
102acce952bSliubo  *	for umount hold all lock.
103acce952bSliubo  */
104acce952bSliubo static void save_error_info(struct btrfs_fs_info *fs_info)
105acce952bSliubo {
106acce952bSliubo 	__save_error_info(fs_info);
107acce952bSliubo }
108acce952bSliubo 
109acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */
110acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
111acce952bSliubo {
112acce952bSliubo 	struct super_block *sb = fs_info->sb;
113acce952bSliubo 
114acce952bSliubo 	if (sb->s_flags & MS_RDONLY)
115acce952bSliubo 		return;
116acce952bSliubo 
117acce952bSliubo 	if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
118acce952bSliubo 		sb->s_flags |= MS_RDONLY;
119acce952bSliubo 		printk(KERN_INFO "btrfs is forced readonly\n");
120acce952bSliubo 	}
121acce952bSliubo }
122acce952bSliubo 
123acce952bSliubo /*
124acce952bSliubo  * __btrfs_std_error decodes expected errors from the caller and
125acce952bSliubo  * invokes the approciate error response.
126acce952bSliubo  */
127acce952bSliubo void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
128acce952bSliubo 		     unsigned int line, int errno)
129acce952bSliubo {
130acce952bSliubo 	struct super_block *sb = fs_info->sb;
131acce952bSliubo 	char nbuf[16];
132acce952bSliubo 	const char *errstr;
133acce952bSliubo 
134acce952bSliubo 	/*
135acce952bSliubo 	 * Special case: if the error is EROFS, and we're already
136acce952bSliubo 	 * under MS_RDONLY, then it is safe here.
137acce952bSliubo 	 */
138acce952bSliubo 	if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
139acce952bSliubo 		return;
140acce952bSliubo 
141acce952bSliubo 	errstr = btrfs_decode_error(fs_info, errno, nbuf);
142acce952bSliubo 	printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: %s\n",
143acce952bSliubo 		sb->s_id, function, line, errstr);
144acce952bSliubo 	save_error_info(fs_info);
145acce952bSliubo 
146acce952bSliubo 	btrfs_handle_error(fs_info);
147acce952bSliubo }
148acce952bSliubo 
149e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
150e20d96d6SChris Mason {
151e20d96d6SChris Mason 	struct btrfs_root *root = btrfs_sb(sb);
152e20d96d6SChris Mason 	int ret;
153e20d96d6SChris Mason 
154e20d96d6SChris Mason 	ret = close_ctree(root);
155e20d96d6SChris Mason 	sb->s_fs_info = NULL;
156559af821SAndi Kleen 
157559af821SAndi Kleen 	(void)ret; /* FIXME: need to fix VFS to return error? */
158e20d96d6SChris Mason }
1592e635a27SChris Mason 
16095e05289SChris Mason enum {
16173f73415SJosef Bacik 	Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
162287a0ab9SJosef Bacik 	Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
163287a0ab9SJosef Bacik 	Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
164261507a0SLi Zefan 	Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
165261507a0SLi Zefan 	Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
16691435650SChris Mason 	Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed,
1679555c6c1SIlya Dryomov 	Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache,
1689555c6c1SIlya Dryomov 	Opt_no_space_cache, Opt_recovery, Opt_skip_balance,
16921adbd5cSStefan Behrens 	Opt_check_integrity, Opt_check_integrity_including_extent_data,
17021adbd5cSStefan Behrens 	Opt_check_integrity_print_mask,
1719555c6c1SIlya Dryomov 	Opt_err,
17295e05289SChris Mason };
17395e05289SChris Mason 
17495e05289SChris Mason static match_table_t tokens = {
175dfe25020SChris Mason 	{Opt_degraded, "degraded"},
17695e05289SChris Mason 	{Opt_subvol, "subvol=%s"},
17773f73415SJosef Bacik 	{Opt_subvolid, "subvolid=%d"},
17843e570b0SChristoph Hellwig 	{Opt_device, "device=%s"},
179b6cda9bcSChris Mason 	{Opt_nodatasum, "nodatasum"},
180be20aa9dSChris Mason 	{Opt_nodatacow, "nodatacow"},
18121ad10cfSChris Mason 	{Opt_nobarrier, "nobarrier"},
1826f568d35SChris Mason 	{Opt_max_inline, "max_inline=%s"},
1838f662a76SChris Mason 	{Opt_alloc_start, "alloc_start=%s"},
1844543df7eSChris Mason 	{Opt_thread_pool, "thread_pool=%d"},
185c8b97818SChris Mason 	{Opt_compress, "compress"},
186261507a0SLi Zefan 	{Opt_compress_type, "compress=%s"},
187a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
188261507a0SLi Zefan 	{Opt_compress_force_type, "compress-force=%s"},
189e18e4809SChris Mason 	{Opt_ssd, "ssd"},
190451d7585SChris Mason 	{Opt_ssd_spread, "ssd_spread"},
1913b30c22fSChris Mason 	{Opt_nossd, "nossd"},
19233268eafSJosef Bacik 	{Opt_noacl, "noacl"},
1933a5e1404SSage Weil 	{Opt_notreelog, "notreelog"},
194dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
19597e728d4SJosef Bacik 	{Opt_ratio, "metadata_ratio=%d"},
196e244a0aeSChristoph Hellwig 	{Opt_discard, "discard"},
1970af3d00bSJosef Bacik 	{Opt_space_cache, "space_cache"},
19888c2ba3bSJosef Bacik 	{Opt_clear_cache, "clear_cache"},
1994260f7c7SSage Weil 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
20091435650SChris Mason 	{Opt_enospc_debug, "enospc_debug"},
201e15d0542SXin Zhong 	{Opt_subvolrootid, "subvolrootid=%d"},
2024cb5300bSChris Mason 	{Opt_defrag, "autodefrag"},
2034b9465cbSChris Mason 	{Opt_inode_cache, "inode_cache"},
2048965593eSDavid Sterba 	{Opt_no_space_cache, "nospace_cache"},
205af31f5e5SChris Mason 	{Opt_recovery, "recovery"},
2069555c6c1SIlya Dryomov 	{Opt_skip_balance, "skip_balance"},
20721adbd5cSStefan Behrens 	{Opt_check_integrity, "check_int"},
20821adbd5cSStefan Behrens 	{Opt_check_integrity_including_extent_data, "check_int_data"},
20921adbd5cSStefan Behrens 	{Opt_check_integrity_print_mask, "check_int_print_mask=%d"},
21033268eafSJosef Bacik 	{Opt_err, NULL},
21195e05289SChris Mason };
21295e05289SChris Mason 
213edf24abeSChristoph Hellwig /*
214edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
215edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
216edf24abeSChristoph Hellwig  */
217edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options)
21895e05289SChris Mason {
219edf24abeSChristoph Hellwig 	struct btrfs_fs_info *info = root->fs_info;
22095e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
22173bc1876SJosef Bacik 	char *p, *num, *orig = NULL;
22273bc1876SJosef Bacik 	u64 cache_gen;
2234543df7eSChris Mason 	int intarg;
224a7a3f7caSSage Weil 	int ret = 0;
225261507a0SLi Zefan 	char *compress_type;
226261507a0SLi Zefan 	bool compress_force = false;
227b6cda9bcSChris Mason 
2286c41761fSDavid Sterba 	cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
22973bc1876SJosef Bacik 	if (cache_gen)
23073bc1876SJosef Bacik 		btrfs_set_opt(info->mount_opt, SPACE_CACHE);
23173bc1876SJosef Bacik 
23295e05289SChris Mason 	if (!options)
23373bc1876SJosef Bacik 		goto out;
23495e05289SChris Mason 
235be20aa9dSChris Mason 	/*
236be20aa9dSChris Mason 	 * strsep changes the string, duplicate it because parse_options
237be20aa9dSChris Mason 	 * gets called twice
238be20aa9dSChris Mason 	 */
239be20aa9dSChris Mason 	options = kstrdup(options, GFP_NOFS);
240be20aa9dSChris Mason 	if (!options)
241be20aa9dSChris Mason 		return -ENOMEM;
242be20aa9dSChris Mason 
243da495eccSJosef Bacik 	orig = options;
244be20aa9dSChris Mason 
24595e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
24695e05289SChris Mason 		int token;
24795e05289SChris Mason 		if (!*p)
24895e05289SChris Mason 			continue;
24995e05289SChris Mason 
25095e05289SChris Mason 		token = match_token(p, tokens, args);
25195e05289SChris Mason 		switch (token) {
252dfe25020SChris Mason 		case Opt_degraded:
253edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: allowing degraded mounts\n");
254dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
255dfe25020SChris Mason 			break;
25695e05289SChris Mason 		case Opt_subvol:
25773f73415SJosef Bacik 		case Opt_subvolid:
258e15d0542SXin Zhong 		case Opt_subvolrootid:
25943e570b0SChristoph Hellwig 		case Opt_device:
260edf24abeSChristoph Hellwig 			/*
26143e570b0SChristoph Hellwig 			 * These are parsed by btrfs_parse_early_options
262edf24abeSChristoph Hellwig 			 * and can be happily ignored here.
263edf24abeSChristoph Hellwig 			 */
26495e05289SChris Mason 			break;
265b6cda9bcSChris Mason 		case Opt_nodatasum:
266067c28adSChris Mason 			printk(KERN_INFO "btrfs: setting nodatasum\n");
267b6cda9bcSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
268be20aa9dSChris Mason 			break;
269be20aa9dSChris Mason 		case Opt_nodatacow:
270edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: setting nodatacow\n");
271be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
272be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
273b6cda9bcSChris Mason 			break;
274a555f810SChris Mason 		case Opt_compress_force:
275261507a0SLi Zefan 		case Opt_compress_force_type:
276261507a0SLi Zefan 			compress_force = true;
277261507a0SLi Zefan 		case Opt_compress:
278261507a0SLi Zefan 		case Opt_compress_type:
279261507a0SLi Zefan 			if (token == Opt_compress ||
280261507a0SLi Zefan 			    token == Opt_compress_force ||
281261507a0SLi Zefan 			    strcmp(args[0].from, "zlib") == 0) {
282261507a0SLi Zefan 				compress_type = "zlib";
283261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
284a6fa6faeSLi Zefan 			} else if (strcmp(args[0].from, "lzo") == 0) {
285a6fa6faeSLi Zefan 				compress_type = "lzo";
286a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
287261507a0SLi Zefan 			} else {
288261507a0SLi Zefan 				ret = -EINVAL;
289261507a0SLi Zefan 				goto out;
290261507a0SLi Zefan 			}
291261507a0SLi Zefan 
292a555f810SChris Mason 			btrfs_set_opt(info->mount_opt, COMPRESS);
293261507a0SLi Zefan 			if (compress_force) {
294261507a0SLi Zefan 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
295261507a0SLi Zefan 				pr_info("btrfs: force %s compression\n",
296261507a0SLi Zefan 					compress_type);
297261507a0SLi Zefan 			} else
298261507a0SLi Zefan 				pr_info("btrfs: use %s compression\n",
299261507a0SLi Zefan 					compress_type);
300a555f810SChris Mason 			break;
301e18e4809SChris Mason 		case Opt_ssd:
302edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
303e18e4809SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
304e18e4809SChris Mason 			break;
305451d7585SChris Mason 		case Opt_ssd_spread:
306451d7585SChris Mason 			printk(KERN_INFO "btrfs: use spread ssd "
307451d7585SChris Mason 			       "allocation scheme\n");
308451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
309451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD_SPREAD);
310451d7585SChris Mason 			break;
3113b30c22fSChris Mason 		case Opt_nossd:
312451d7585SChris Mason 			printk(KERN_INFO "btrfs: not using ssd allocation "
313451d7585SChris Mason 			       "scheme\n");
314c289811cSChris Mason 			btrfs_set_opt(info->mount_opt, NOSSD);
3153b30c22fSChris Mason 			btrfs_clear_opt(info->mount_opt, SSD);
316451d7585SChris Mason 			btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
3173b30c22fSChris Mason 			break;
31821ad10cfSChris Mason 		case Opt_nobarrier:
319edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: turning off barriers\n");
32021ad10cfSChris Mason 			btrfs_set_opt(info->mount_opt, NOBARRIER);
32121ad10cfSChris Mason 			break;
3224543df7eSChris Mason 		case Opt_thread_pool:
3234543df7eSChris Mason 			intarg = 0;
3244543df7eSChris Mason 			match_int(&args[0], &intarg);
3254543df7eSChris Mason 			if (intarg) {
3264543df7eSChris Mason 				info->thread_pool_size = intarg;
3274543df7eSChris Mason 				printk(KERN_INFO "btrfs: thread pool %d\n",
3284543df7eSChris Mason 				       info->thread_pool_size);
3294543df7eSChris Mason 			}
3304543df7eSChris Mason 			break;
3316f568d35SChris Mason 		case Opt_max_inline:
332edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
3336f568d35SChris Mason 			if (num) {
33491748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
3356f568d35SChris Mason 				kfree(num);
3366f568d35SChris Mason 
33715ada040SChris Mason 				if (info->max_inline) {
3386f568d35SChris Mason 					info->max_inline = max_t(u64,
33915ada040SChris Mason 						info->max_inline,
34015ada040SChris Mason 						root->sectorsize);
34115ada040SChris Mason 				}
342edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: max_inline at %llu\n",
34321380931SJoel Becker 					(unsigned long long)info->max_inline);
3446f568d35SChris Mason 			}
3456f568d35SChris Mason 			break;
3468f662a76SChris Mason 		case Opt_alloc_start:
347edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
3488f662a76SChris Mason 			if (num) {
34991748467SAkinobu Mita 				info->alloc_start = memparse(num, NULL);
3508f662a76SChris Mason 				kfree(num);
351edf24abeSChristoph Hellwig 				printk(KERN_INFO
352edf24abeSChristoph Hellwig 					"btrfs: allocations start at %llu\n",
35321380931SJoel Becker 					(unsigned long long)info->alloc_start);
3548f662a76SChris Mason 			}
3558f662a76SChris Mason 			break;
35633268eafSJosef Bacik 		case Opt_noacl:
35733268eafSJosef Bacik 			root->fs_info->sb->s_flags &= ~MS_POSIXACL;
35833268eafSJosef Bacik 			break;
3593a5e1404SSage Weil 		case Opt_notreelog:
3603a5e1404SSage Weil 			printk(KERN_INFO "btrfs: disabling tree log\n");
3613a5e1404SSage Weil 			btrfs_set_opt(info->mount_opt, NOTREELOG);
3623a5e1404SSage Weil 			break;
363dccae999SSage Weil 		case Opt_flushoncommit:
364dccae999SSage Weil 			printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
365dccae999SSage Weil 			btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
366dccae999SSage Weil 			break;
36797e728d4SJosef Bacik 		case Opt_ratio:
36897e728d4SJosef Bacik 			intarg = 0;
36997e728d4SJosef Bacik 			match_int(&args[0], &intarg);
37097e728d4SJosef Bacik 			if (intarg) {
37197e728d4SJosef Bacik 				info->metadata_ratio = intarg;
37297e728d4SJosef Bacik 				printk(KERN_INFO "btrfs: metadata ratio %d\n",
37397e728d4SJosef Bacik 				       info->metadata_ratio);
37497e728d4SJosef Bacik 			}
37597e728d4SJosef Bacik 			break;
376e244a0aeSChristoph Hellwig 		case Opt_discard:
377e244a0aeSChristoph Hellwig 			btrfs_set_opt(info->mount_opt, DISCARD);
378e244a0aeSChristoph Hellwig 			break;
3790af3d00bSJosef Bacik 		case Opt_space_cache:
3800af3d00bSJosef Bacik 			btrfs_set_opt(info->mount_opt, SPACE_CACHE);
3810de90876SJosef Bacik 			break;
38273bc1876SJosef Bacik 		case Opt_no_space_cache:
38373bc1876SJosef Bacik 			printk(KERN_INFO "btrfs: disabling disk space caching\n");
38473bc1876SJosef Bacik 			btrfs_clear_opt(info->mount_opt, SPACE_CACHE);
38573bc1876SJosef Bacik 			break;
3864b9465cbSChris Mason 		case Opt_inode_cache:
3874b9465cbSChris Mason 			printk(KERN_INFO "btrfs: enabling inode map caching\n");
3884b9465cbSChris Mason 			btrfs_set_opt(info->mount_opt, INODE_MAP_CACHE);
3894b9465cbSChris Mason 			break;
39088c2ba3bSJosef Bacik 		case Opt_clear_cache:
39188c2ba3bSJosef Bacik 			printk(KERN_INFO "btrfs: force clearing of disk cache\n");
39288c2ba3bSJosef Bacik 			btrfs_set_opt(info->mount_opt, CLEAR_CACHE);
3930af3d00bSJosef Bacik 			break;
3944260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
3954260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
3964260f7c7SSage Weil 			break;
39791435650SChris Mason 		case Opt_enospc_debug:
39891435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
39991435650SChris Mason 			break;
4004cb5300bSChris Mason 		case Opt_defrag:
4014cb5300bSChris Mason 			printk(KERN_INFO "btrfs: enabling auto defrag");
4024cb5300bSChris Mason 			btrfs_set_opt(info->mount_opt, AUTO_DEFRAG);
4034cb5300bSChris Mason 			break;
404af31f5e5SChris Mason 		case Opt_recovery:
405af31f5e5SChris Mason 			printk(KERN_INFO "btrfs: enabling auto recovery");
406af31f5e5SChris Mason 			btrfs_set_opt(info->mount_opt, RECOVERY);
407af31f5e5SChris Mason 			break;
4089555c6c1SIlya Dryomov 		case Opt_skip_balance:
4099555c6c1SIlya Dryomov 			btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
4109555c6c1SIlya Dryomov 			break;
41121adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
41221adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
41321adbd5cSStefan Behrens 			printk(KERN_INFO "btrfs: enabling check integrity"
41421adbd5cSStefan Behrens 			       " including extent data\n");
41521adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt,
41621adbd5cSStefan Behrens 				      CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
41721adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
41821adbd5cSStefan Behrens 			break;
41921adbd5cSStefan Behrens 		case Opt_check_integrity:
42021adbd5cSStefan Behrens 			printk(KERN_INFO "btrfs: enabling check integrity\n");
42121adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
42221adbd5cSStefan Behrens 			break;
42321adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
42421adbd5cSStefan Behrens 			intarg = 0;
42521adbd5cSStefan Behrens 			match_int(&args[0], &intarg);
42621adbd5cSStefan Behrens 			if (intarg) {
42721adbd5cSStefan Behrens 				info->check_integrity_print_mask = intarg;
42821adbd5cSStefan Behrens 				printk(KERN_INFO "btrfs:"
42921adbd5cSStefan Behrens 				       " check_integrity_print_mask 0x%x\n",
43021adbd5cSStefan Behrens 				       info->check_integrity_print_mask);
43121adbd5cSStefan Behrens 			}
43221adbd5cSStefan Behrens 			break;
43321adbd5cSStefan Behrens #else
43421adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
43521adbd5cSStefan Behrens 		case Opt_check_integrity:
43621adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
43721adbd5cSStefan Behrens 			printk(KERN_ERR "btrfs: support for check_integrity*"
43821adbd5cSStefan Behrens 			       " not compiled in!\n");
43921adbd5cSStefan Behrens 			ret = -EINVAL;
44021adbd5cSStefan Behrens 			goto out;
44121adbd5cSStefan Behrens #endif
442a7a3f7caSSage Weil 		case Opt_err:
443a7a3f7caSSage Weil 			printk(KERN_INFO "btrfs: unrecognized mount option "
444a7a3f7caSSage Weil 			       "'%s'\n", p);
445a7a3f7caSSage Weil 			ret = -EINVAL;
446a7a3f7caSSage Weil 			goto out;
44795e05289SChris Mason 		default:
448be20aa9dSChris Mason 			break;
44995e05289SChris Mason 		}
45095e05289SChris Mason 	}
451a7a3f7caSSage Weil out:
45273bc1876SJosef Bacik 	if (!ret && btrfs_test_opt(root, SPACE_CACHE))
45373bc1876SJosef Bacik 		printk(KERN_INFO "btrfs: disk space caching is enabled\n");
454da495eccSJosef Bacik 	kfree(orig);
455a7a3f7caSSage Weil 	return ret;
456edf24abeSChristoph Hellwig }
457edf24abeSChristoph Hellwig 
458edf24abeSChristoph Hellwig /*
459edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
460edf24abeSChristoph Hellwig  *
461edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
462edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
463edf24abeSChristoph Hellwig  */
46497288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags,
46573f73415SJosef Bacik 		void *holder, char **subvol_name, u64 *subvol_objectid,
466e15d0542SXin Zhong 		u64 *subvol_rootid, struct btrfs_fs_devices **fs_devices)
467edf24abeSChristoph Hellwig {
468edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
46983c8c9bdSJeff Liu 	char *device_name, *opts, *orig, *p;
470edf24abeSChristoph Hellwig 	int error = 0;
47173f73415SJosef Bacik 	int intarg;
472edf24abeSChristoph Hellwig 
473edf24abeSChristoph Hellwig 	if (!options)
474830c4adbSJosef Bacik 		return 0;
475edf24abeSChristoph Hellwig 
476edf24abeSChristoph Hellwig 	/*
477edf24abeSChristoph Hellwig 	 * strsep changes the string, duplicate it because parse_options
478edf24abeSChristoph Hellwig 	 * gets called twice
479edf24abeSChristoph Hellwig 	 */
480edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
481edf24abeSChristoph Hellwig 	if (!opts)
482edf24abeSChristoph Hellwig 		return -ENOMEM;
4833f3d0bc0STero Roponen 	orig = opts;
484edf24abeSChristoph Hellwig 
485edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
486edf24abeSChristoph Hellwig 		int token;
487edf24abeSChristoph Hellwig 		if (!*p)
488edf24abeSChristoph Hellwig 			continue;
489edf24abeSChristoph Hellwig 
490edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
491edf24abeSChristoph Hellwig 		switch (token) {
492edf24abeSChristoph Hellwig 		case Opt_subvol:
493a90e8b6fSIlya Dryomov 			kfree(*subvol_name);
494edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
495edf24abeSChristoph Hellwig 			break;
49673f73415SJosef Bacik 		case Opt_subvolid:
49773f73415SJosef Bacik 			intarg = 0;
4984849f01dSJosef Bacik 			error = match_int(&args[0], &intarg);
4994849f01dSJosef Bacik 			if (!error) {
5004849f01dSJosef Bacik 				/* we want the original fs_tree */
5014849f01dSJosef Bacik 				if (!intarg)
5024849f01dSJosef Bacik 					*subvol_objectid =
5034849f01dSJosef Bacik 						BTRFS_FS_TREE_OBJECTID;
5044849f01dSJosef Bacik 				else
50573f73415SJosef Bacik 					*subvol_objectid = intarg;
5064849f01dSJosef Bacik 			}
50773f73415SJosef Bacik 			break;
508e15d0542SXin Zhong 		case Opt_subvolrootid:
509e15d0542SXin Zhong 			intarg = 0;
510e15d0542SXin Zhong 			error = match_int(&args[0], &intarg);
511e15d0542SXin Zhong 			if (!error) {
512e15d0542SXin Zhong 				/* we want the original fs_tree */
513e15d0542SXin Zhong 				if (!intarg)
514e15d0542SXin Zhong 					*subvol_rootid =
515e15d0542SXin Zhong 						BTRFS_FS_TREE_OBJECTID;
516e15d0542SXin Zhong 				else
517e15d0542SXin Zhong 					*subvol_rootid = intarg;
518e15d0542SXin Zhong 			}
519e15d0542SXin Zhong 			break;
52043e570b0SChristoph Hellwig 		case Opt_device:
52183c8c9bdSJeff Liu 			device_name = match_strdup(&args[0]);
52283c8c9bdSJeff Liu 			if (!device_name) {
52383c8c9bdSJeff Liu 				error = -ENOMEM;
52483c8c9bdSJeff Liu 				goto out;
52583c8c9bdSJeff Liu 			}
52683c8c9bdSJeff Liu 			error = btrfs_scan_one_device(device_name,
52743e570b0SChristoph Hellwig 					flags, holder, fs_devices);
52883c8c9bdSJeff Liu 			kfree(device_name);
52943e570b0SChristoph Hellwig 			if (error)
530830c4adbSJosef Bacik 				goto out;
53143e570b0SChristoph Hellwig 			break;
532edf24abeSChristoph Hellwig 		default:
533edf24abeSChristoph Hellwig 			break;
534edf24abeSChristoph Hellwig 		}
535edf24abeSChristoph Hellwig 	}
536edf24abeSChristoph Hellwig 
537edf24abeSChristoph Hellwig out:
538830c4adbSJosef Bacik 	kfree(orig);
539edf24abeSChristoph Hellwig 	return error;
54095e05289SChris Mason }
54195e05289SChris Mason 
54273f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb,
54373f73415SJosef Bacik 				       u64 subvol_objectid)
54473f73415SJosef Bacik {
54573f73415SJosef Bacik 	struct btrfs_root *root = sb->s_fs_info;
54673f73415SJosef Bacik 	struct btrfs_root *new_root;
54773f73415SJosef Bacik 	struct btrfs_dir_item *di;
54873f73415SJosef Bacik 	struct btrfs_path *path;
54973f73415SJosef Bacik 	struct btrfs_key location;
55073f73415SJosef Bacik 	struct inode *inode;
55173f73415SJosef Bacik 	u64 dir_id;
55273f73415SJosef Bacik 	int new = 0;
55373f73415SJosef Bacik 
55473f73415SJosef Bacik 	/*
55573f73415SJosef Bacik 	 * We have a specific subvol we want to mount, just setup location and
55673f73415SJosef Bacik 	 * go look up the root.
55773f73415SJosef Bacik 	 */
55873f73415SJosef Bacik 	if (subvol_objectid) {
55973f73415SJosef Bacik 		location.objectid = subvol_objectid;
56073f73415SJosef Bacik 		location.type = BTRFS_ROOT_ITEM_KEY;
56173f73415SJosef Bacik 		location.offset = (u64)-1;
56273f73415SJosef Bacik 		goto find_root;
56373f73415SJosef Bacik 	}
56473f73415SJosef Bacik 
56573f73415SJosef Bacik 	path = btrfs_alloc_path();
56673f73415SJosef Bacik 	if (!path)
56773f73415SJosef Bacik 		return ERR_PTR(-ENOMEM);
56873f73415SJosef Bacik 	path->leave_spinning = 1;
56973f73415SJosef Bacik 
57073f73415SJosef Bacik 	/*
57173f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
57273f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
57373f73415SJosef Bacik 	 * to mount.
57473f73415SJosef Bacik 	 */
5756c41761fSDavid Sterba 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
57673f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
577b0839166SJulia Lawall 	if (IS_ERR(di)) {
578b0839166SJulia Lawall 		btrfs_free_path(path);
579fb4f6f91SDan Carpenter 		return ERR_CAST(di);
580b0839166SJulia Lawall 	}
58173f73415SJosef Bacik 	if (!di) {
58273f73415SJosef Bacik 		/*
58373f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
58473f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
58573f73415SJosef Bacik 		 * mount to root most subvolume.
58673f73415SJosef Bacik 		 */
58773f73415SJosef Bacik 		btrfs_free_path(path);
58873f73415SJosef Bacik 		dir_id = BTRFS_FIRST_FREE_OBJECTID;
58973f73415SJosef Bacik 		new_root = root->fs_info->fs_root;
59073f73415SJosef Bacik 		goto setup_root;
59173f73415SJosef Bacik 	}
59273f73415SJosef Bacik 
59373f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
59473f73415SJosef Bacik 	btrfs_free_path(path);
59573f73415SJosef Bacik 
59673f73415SJosef Bacik find_root:
59773f73415SJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
59873f73415SJosef Bacik 	if (IS_ERR(new_root))
599d0b678cbSJulia Lawall 		return ERR_CAST(new_root);
60073f73415SJosef Bacik 
60173f73415SJosef Bacik 	if (btrfs_root_refs(&new_root->root_item) == 0)
60273f73415SJosef Bacik 		return ERR_PTR(-ENOENT);
60373f73415SJosef Bacik 
60473f73415SJosef Bacik 	dir_id = btrfs_root_dirid(&new_root->root_item);
60573f73415SJosef Bacik setup_root:
60673f73415SJosef Bacik 	location.objectid = dir_id;
60773f73415SJosef Bacik 	location.type = BTRFS_INODE_ITEM_KEY;
60873f73415SJosef Bacik 	location.offset = 0;
60973f73415SJosef Bacik 
61073f73415SJosef Bacik 	inode = btrfs_iget(sb, &location, new_root, &new);
6114cbd1149SDan Carpenter 	if (IS_ERR(inode))
6124cbd1149SDan Carpenter 		return ERR_CAST(inode);
61373f73415SJosef Bacik 
61473f73415SJosef Bacik 	/*
61573f73415SJosef Bacik 	 * If we're just mounting the root most subvol put the inode and return
61673f73415SJosef Bacik 	 * a reference to the dentry.  We will have already gotten a reference
61773f73415SJosef Bacik 	 * to the inode in btrfs_fill_super so we're good to go.
61873f73415SJosef Bacik 	 */
61973f73415SJosef Bacik 	if (!new && sb->s_root->d_inode == inode) {
62073f73415SJosef Bacik 		iput(inode);
62173f73415SJosef Bacik 		return dget(sb->s_root);
62273f73415SJosef Bacik 	}
62373f73415SJosef Bacik 
624ba5b8958SJosef Bacik 	return d_obtain_alias(inode);
62573f73415SJosef Bacik }
62673f73415SJosef Bacik 
6278a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
6288a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
6298a4b83ccSChris Mason 			    void *data, int silent)
6302e635a27SChris Mason {
6312e635a27SChris Mason 	struct inode *inode;
632e20d96d6SChris Mason 	struct dentry *root_dentry;
6330f7d52f4SChris Mason 	struct btrfs_root *tree_root;
6345d4f98a2SYan Zheng 	struct btrfs_key key;
63539279cc3SChris Mason 	int err;
6362e635a27SChris Mason 
6372e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
6382e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
639e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
640af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
641be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
6425103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
6432e635a27SChris Mason 	sb->s_time_gran = 1;
6440eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
64533268eafSJosef Bacik 	sb->s_flags |= MS_POSIXACL;
64649cf6f45SChris Ball #endif
647e20d96d6SChris Mason 
648dfe25020SChris Mason 	tree_root = open_ctree(sb, fs_devices, (char *)data);
649d98237b3SChris Mason 
650e58ca020SYan 	if (IS_ERR(tree_root)) {
651e20d96d6SChris Mason 		printk("btrfs: open_ctree failed\n");
652e58ca020SYan 		return PTR_ERR(tree_root);
653e20d96d6SChris Mason 	}
6540f7d52f4SChris Mason 	sb->s_fs_info = tree_root;
655b888db2bSChris Mason 
6565d4f98a2SYan Zheng 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6575d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
6585d4f98a2SYan Zheng 	key.offset = 0;
65973f73415SJosef Bacik 	inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
6605d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
6615d4f98a2SYan Zheng 		err = PTR_ERR(inode);
66239279cc3SChris Mason 		goto fail_close;
66339279cc3SChris Mason 	}
6642e635a27SChris Mason 
665e20d96d6SChris Mason 	root_dentry = d_alloc_root(inode);
666e20d96d6SChris Mason 	if (!root_dentry) {
6672e635a27SChris Mason 		iput(inode);
66839279cc3SChris Mason 		err = -ENOMEM;
66939279cc3SChris Mason 		goto fail_close;
6702e635a27SChris Mason 	}
67158176a96SJosef Bacik 
672e20d96d6SChris Mason 	sb->s_root = root_dentry;
6736885f308SChris Mason 
6746885f308SChris Mason 	save_mount_options(sb, data);
67590a887c9SDan Magenheimer 	cleancache_init_fs(sb);
6762e635a27SChris Mason 	return 0;
6772e635a27SChris Mason 
67839279cc3SChris Mason fail_close:
67939279cc3SChris Mason 	close_ctree(tree_root);
680d5719762SChris Mason 	return err;
681d5719762SChris Mason }
682d5719762SChris Mason 
6836bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
684d5719762SChris Mason {
685d5719762SChris Mason 	struct btrfs_trans_handle *trans;
686dccae999SSage Weil 	struct btrfs_root *root = btrfs_sb(sb);
687d5719762SChris Mason 	int ret;
688df2ce34cSChris Mason 
6891abe9b8aSliubo 	trace_btrfs_sync_fs(wait);
6901abe9b8aSliubo 
691d561c025SChris Mason 	if (!wait) {
6927cfcc17eSChris Mason 		filemap_flush(root->fs_info->btree_inode->i_mapping);
693df2ce34cSChris Mason 		return 0;
694d561c025SChris Mason 	}
695771ed689SChris Mason 
69624bbcf04SYan, Zheng 	btrfs_start_delalloc_inodes(root, 0);
69724bbcf04SYan, Zheng 	btrfs_wait_ordered_extents(root, 0, 0);
698771ed689SChris Mason 
699a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
70098d5dc13STsutomu Itoh 	if (IS_ERR(trans))
70198d5dc13STsutomu Itoh 		return PTR_ERR(trans);
702d5719762SChris Mason 	ret = btrfs_commit_transaction(trans, root);
70354aa1f4dSChris Mason 	return ret;
704d5719762SChris Mason }
705d5719762SChris Mason 
706a9572a15SEric Paris static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
707a9572a15SEric Paris {
708a9572a15SEric Paris 	struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
709a9572a15SEric Paris 	struct btrfs_fs_info *info = root->fs_info;
710200da64eSTsutomu Itoh 	char *compress_type;
711a9572a15SEric Paris 
712a9572a15SEric Paris 	if (btrfs_test_opt(root, DEGRADED))
713a9572a15SEric Paris 		seq_puts(seq, ",degraded");
714a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATASUM))
715a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
716a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATACOW))
717a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
718a9572a15SEric Paris 	if (btrfs_test_opt(root, NOBARRIER))
719a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
720a9572a15SEric Paris 	if (info->max_inline != 8192 * 1024)
72121380931SJoel Becker 		seq_printf(seq, ",max_inline=%llu",
72221380931SJoel Becker 			   (unsigned long long)info->max_inline);
723a9572a15SEric Paris 	if (info->alloc_start != 0)
72421380931SJoel Becker 		seq_printf(seq, ",alloc_start=%llu",
72521380931SJoel Becker 			   (unsigned long long)info->alloc_start);
726a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
727a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
728a9572a15SEric Paris 		seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
729200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, COMPRESS)) {
730200da64eSTsutomu Itoh 		if (info->compress_type == BTRFS_COMPRESS_ZLIB)
731200da64eSTsutomu Itoh 			compress_type = "zlib";
732200da64eSTsutomu Itoh 		else
733200da64eSTsutomu Itoh 			compress_type = "lzo";
734200da64eSTsutomu Itoh 		if (btrfs_test_opt(root, FORCE_COMPRESS))
735200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
736200da64eSTsutomu Itoh 		else
737200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
738200da64eSTsutomu Itoh 	}
739c289811cSChris Mason 	if (btrfs_test_opt(root, NOSSD))
740c289811cSChris Mason 		seq_puts(seq, ",nossd");
741451d7585SChris Mason 	if (btrfs_test_opt(root, SSD_SPREAD))
742451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
743451d7585SChris Mason 	else if (btrfs_test_opt(root, SSD))
744a9572a15SEric Paris 		seq_puts(seq, ",ssd");
7453a5e1404SSage Weil 	if (btrfs_test_opt(root, NOTREELOG))
7466b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
747dccae999SSage Weil 	if (btrfs_test_opt(root, FLUSHONCOMMIT))
7486b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
74920a5239aSMatthew Wilcox 	if (btrfs_test_opt(root, DISCARD))
75020a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
751a9572a15SEric Paris 	if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
752a9572a15SEric Paris 		seq_puts(seq, ",noacl");
753200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, SPACE_CACHE))
754200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
75573bc1876SJosef Bacik 	else
7568965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
757200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, CLEAR_CACHE))
758200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
759200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
760200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
7610942caa3SDavid Sterba 	if (btrfs_test_opt(root, ENOSPC_DEBUG))
7620942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
7630942caa3SDavid Sterba 	if (btrfs_test_opt(root, AUTO_DEFRAG))
7640942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
7650942caa3SDavid Sterba 	if (btrfs_test_opt(root, INODE_MAP_CACHE))
7660942caa3SDavid Sterba 		seq_puts(seq, ",inode_cache");
7679555c6c1SIlya Dryomov 	if (btrfs_test_opt(root, SKIP_BALANCE))
7689555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
769a9572a15SEric Paris 	return 0;
770a9572a15SEric Paris }
771a9572a15SEric Paris 
772a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
7732e635a27SChris Mason {
774450ba0eaSJosef Bacik 	struct btrfs_root *test_root = data;
775a061fc8dSChris Mason 	struct btrfs_root *root = btrfs_sb(s);
7764b82d6e4SYan 
777619c8c76SIan Kent 	/*
778619c8c76SIan Kent 	 * If this super block is going away, return false as it
779619c8c76SIan Kent 	 * can't match as an existing super block.
780619c8c76SIan Kent 	 */
781619c8c76SIan Kent 	if (!atomic_read(&s->s_active))
782619c8c76SIan Kent 		return 0;
783450ba0eaSJosef Bacik 	return root->fs_info->fs_devices == test_root->fs_info->fs_devices;
7844b82d6e4SYan }
7854b82d6e4SYan 
786450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
787450ba0eaSJosef Bacik {
788450ba0eaSJosef Bacik 	s->s_fs_info = data;
789450ba0eaSJosef Bacik 
790450ba0eaSJosef Bacik 	return set_anon_super(s, data);
791450ba0eaSJosef Bacik }
792450ba0eaSJosef Bacik 
793830c4adbSJosef Bacik /*
794f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
795f9d9ef62SDavid Sterba  */
796f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
797f9d9ef62SDavid Sterba {
798f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
799f9d9ef62SDavid Sterba 		return 1;
800f9d9ef62SDavid Sterba 	return 0;
801f9d9ef62SDavid Sterba }
802f9d9ef62SDavid Sterba 
803f9d9ef62SDavid Sterba /*
804830c4adbSJosef Bacik  * This will strip out the subvol=%s argument for an argument string and add
805830c4adbSJosef Bacik  * subvolid=0 to make sure we get the actual tree root for path walking to the
806830c4adbSJosef Bacik  * subvol we want.
807830c4adbSJosef Bacik  */
808830c4adbSJosef Bacik static char *setup_root_args(char *args)
809830c4adbSJosef Bacik {
810830c4adbSJosef Bacik 	unsigned copied = 0;
811830c4adbSJosef Bacik 	unsigned len = strlen(args) + 2;
812830c4adbSJosef Bacik 	char *pos;
813830c4adbSJosef Bacik 	char *ret;
814830c4adbSJosef Bacik 
815830c4adbSJosef Bacik 	/*
816830c4adbSJosef Bacik 	 * We need the same args as before, but minus
817830c4adbSJosef Bacik 	 *
818830c4adbSJosef Bacik 	 * subvol=a
819830c4adbSJosef Bacik 	 *
820830c4adbSJosef Bacik 	 * and add
821830c4adbSJosef Bacik 	 *
822830c4adbSJosef Bacik 	 * subvolid=0
823830c4adbSJosef Bacik 	 *
824830c4adbSJosef Bacik 	 * which is a difference of 2 characters, so we allocate strlen(args) +
825830c4adbSJosef Bacik 	 * 2 characters.
826830c4adbSJosef Bacik 	 */
827830c4adbSJosef Bacik 	ret = kzalloc(len * sizeof(char), GFP_NOFS);
828830c4adbSJosef Bacik 	if (!ret)
829830c4adbSJosef Bacik 		return NULL;
830830c4adbSJosef Bacik 	pos = strstr(args, "subvol=");
831830c4adbSJosef Bacik 
832830c4adbSJosef Bacik 	/* This shouldn't happen, but just in case.. */
833830c4adbSJosef Bacik 	if (!pos) {
834830c4adbSJosef Bacik 		kfree(ret);
835830c4adbSJosef Bacik 		return NULL;
836830c4adbSJosef Bacik 	}
837830c4adbSJosef Bacik 
838830c4adbSJosef Bacik 	/*
839830c4adbSJosef Bacik 	 * The subvol=<> arg is not at the front of the string, copy everybody
840830c4adbSJosef Bacik 	 * up to that into ret.
841830c4adbSJosef Bacik 	 */
842830c4adbSJosef Bacik 	if (pos != args) {
843830c4adbSJosef Bacik 		*pos = '\0';
844830c4adbSJosef Bacik 		strcpy(ret, args);
845830c4adbSJosef Bacik 		copied += strlen(args);
846830c4adbSJosef Bacik 		pos++;
847830c4adbSJosef Bacik 	}
848830c4adbSJosef Bacik 
849830c4adbSJosef Bacik 	strncpy(ret + copied, "subvolid=0", len - copied);
850830c4adbSJosef Bacik 
851830c4adbSJosef Bacik 	/* Length of subvolid=0 */
852830c4adbSJosef Bacik 	copied += 10;
853830c4adbSJosef Bacik 
854830c4adbSJosef Bacik 	/*
855830c4adbSJosef Bacik 	 * If there is no , after the subvol= option then we know there's no
856830c4adbSJosef Bacik 	 * other options and we can just return.
857830c4adbSJosef Bacik 	 */
858830c4adbSJosef Bacik 	pos = strchr(pos, ',');
859830c4adbSJosef Bacik 	if (!pos)
860830c4adbSJosef Bacik 		return ret;
861830c4adbSJosef Bacik 
862830c4adbSJosef Bacik 	/* Copy the rest of the arguments into our buffer */
863830c4adbSJosef Bacik 	strncpy(ret + copied, pos, len - copied);
864830c4adbSJosef Bacik 	copied += strlen(pos);
865830c4adbSJosef Bacik 
866830c4adbSJosef Bacik 	return ret;
867830c4adbSJosef Bacik }
868830c4adbSJosef Bacik 
869830c4adbSJosef Bacik static struct dentry *mount_subvol(const char *subvol_name, int flags,
870830c4adbSJosef Bacik 				   const char *device_name, char *data)
871830c4adbSJosef Bacik {
872830c4adbSJosef Bacik 	struct dentry *root;
873830c4adbSJosef Bacik 	struct vfsmount *mnt;
874830c4adbSJosef Bacik 	char *newargs;
875830c4adbSJosef Bacik 
876830c4adbSJosef Bacik 	newargs = setup_root_args(data);
877830c4adbSJosef Bacik 	if (!newargs)
878830c4adbSJosef Bacik 		return ERR_PTR(-ENOMEM);
879830c4adbSJosef Bacik 	mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name,
880830c4adbSJosef Bacik 			     newargs);
881830c4adbSJosef Bacik 	kfree(newargs);
882830c4adbSJosef Bacik 	if (IS_ERR(mnt))
883830c4adbSJosef Bacik 		return ERR_CAST(mnt);
884830c4adbSJosef Bacik 
885ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
886830c4adbSJosef Bacik 
887ea441d11SAl Viro 	if (!IS_ERR(root) && !is_subvolume_inode(root->d_inode)) {
888ea441d11SAl Viro 		struct super_block *s = root->d_sb;
889ea441d11SAl Viro 		dput(root);
890ea441d11SAl Viro 		root = ERR_PTR(-EINVAL);
891ea441d11SAl Viro 		deactivate_locked_super(s);
892f9d9ef62SDavid Sterba 		printk(KERN_ERR "btrfs: '%s' is not a valid subvolume\n",
893f9d9ef62SDavid Sterba 				subvol_name);
894f9d9ef62SDavid Sterba 	}
895f9d9ef62SDavid Sterba 
896830c4adbSJosef Bacik 	return root;
897830c4adbSJosef Bacik }
898450ba0eaSJosef Bacik 
899edf24abeSChristoph Hellwig /*
900edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
901edf24abeSChristoph Hellwig  *
902edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
903edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
904edf24abeSChristoph Hellwig  */
905061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
906306e16ceSDavid Sterba 		const char *device_name, void *data)
9074b82d6e4SYan {
9084b82d6e4SYan 	struct block_device *bdev = NULL;
9094b82d6e4SYan 	struct super_block *s;
9104b82d6e4SYan 	struct dentry *root;
9118a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
912450ba0eaSJosef Bacik 	struct btrfs_fs_info *fs_info = NULL;
91397288f2cSChristoph Hellwig 	fmode_t mode = FMODE_READ;
91473f73415SJosef Bacik 	char *subvol_name = NULL;
91573f73415SJosef Bacik 	u64 subvol_objectid = 0;
916e15d0542SXin Zhong 	u64 subvol_rootid = 0;
9174b82d6e4SYan 	int error = 0;
9184b82d6e4SYan 
91997288f2cSChristoph Hellwig 	if (!(flags & MS_RDONLY))
92097288f2cSChristoph Hellwig 		mode |= FMODE_WRITE;
92197288f2cSChristoph Hellwig 
92297288f2cSChristoph Hellwig 	error = btrfs_parse_early_options(data, mode, fs_type,
92373f73415SJosef Bacik 					  &subvol_name, &subvol_objectid,
924e15d0542SXin Zhong 					  &subvol_rootid, &fs_devices);
925f23c8af8SIlya Dryomov 	if (error) {
926f23c8af8SIlya Dryomov 		kfree(subvol_name);
927061dbc6bSAl Viro 		return ERR_PTR(error);
928f23c8af8SIlya Dryomov 	}
929edf24abeSChristoph Hellwig 
930830c4adbSJosef Bacik 	if (subvol_name) {
931830c4adbSJosef Bacik 		root = mount_subvol(subvol_name, flags, device_name, data);
932830c4adbSJosef Bacik 		kfree(subvol_name);
933830c4adbSJosef Bacik 		return root;
934830c4adbSJosef Bacik 	}
935830c4adbSJosef Bacik 
936306e16ceSDavid Sterba 	error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
9378a4b83ccSChris Mason 	if (error)
938830c4adbSJosef Bacik 		return ERR_PTR(error);
9394b82d6e4SYan 
940450ba0eaSJosef Bacik 	/*
941450ba0eaSJosef Bacik 	 * Setup a dummy root and fs_info for test/set super.  This is because
942450ba0eaSJosef Bacik 	 * we don't actually fill this stuff out until open_ctree, but we need
943450ba0eaSJosef Bacik 	 * it for searching for existing supers, so this lets us do that and
944450ba0eaSJosef Bacik 	 * then open_ctree will properly initialize everything later.
945450ba0eaSJosef Bacik 	 */
946450ba0eaSJosef Bacik 	fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
94704d21a24SIlya Dryomov 	if (!fs_info)
94804d21a24SIlya Dryomov 		return ERR_PTR(-ENOMEM);
94904d21a24SIlya Dryomov 
95004d21a24SIlya Dryomov 	fs_info->tree_root = kzalloc(sizeof(struct btrfs_root), GFP_NOFS);
95104d21a24SIlya Dryomov 	if (!fs_info->tree_root) {
95245ea6095Sslyich@gmail.com 		error = -ENOMEM;
95304d21a24SIlya Dryomov 		goto error_fs_info;
95445ea6095Sslyich@gmail.com 	}
95504d21a24SIlya Dryomov 	fs_info->tree_root->fs_info = fs_info;
956450ba0eaSJosef Bacik 	fs_info->fs_devices = fs_devices;
957450ba0eaSJosef Bacik 
9586c41761fSDavid Sterba 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
9596c41761fSDavid Sterba 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
9606c41761fSDavid Sterba 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
9616c41761fSDavid Sterba 		error = -ENOMEM;
96204d21a24SIlya Dryomov 		goto error_fs_info;
96304d21a24SIlya Dryomov 	}
96404d21a24SIlya Dryomov 
96504d21a24SIlya Dryomov 	error = btrfs_open_devices(fs_devices, mode, fs_type);
96604d21a24SIlya Dryomov 	if (error)
96704d21a24SIlya Dryomov 		goto error_fs_info;
96804d21a24SIlya Dryomov 
96904d21a24SIlya Dryomov 	if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
97004d21a24SIlya Dryomov 		error = -EACCES;
9716c41761fSDavid Sterba 		goto error_close_devices;
9726c41761fSDavid Sterba 	}
9736c41761fSDavid Sterba 
974dfe25020SChris Mason 	bdev = fs_devices->latest_bdev;
97504d21a24SIlya Dryomov 	s = sget(fs_type, btrfs_test_super, btrfs_set_super,
97604d21a24SIlya Dryomov 		 fs_info->tree_root);
977830c4adbSJosef Bacik 	if (IS_ERR(s)) {
978830c4adbSJosef Bacik 		error = PTR_ERR(s);
979830c4adbSJosef Bacik 		goto error_close_devices;
980830c4adbSJosef Bacik 	}
9814b82d6e4SYan 
9824b82d6e4SYan 	if (s->s_root) {
9834b82d6e4SYan 		if ((flags ^ s->s_flags) & MS_RDONLY) {
9846f5bbff9SAl Viro 			deactivate_locked_super(s);
98504d21a24SIlya Dryomov 			error = -EBUSY;
98604d21a24SIlya Dryomov 			goto error_close_devices;
9874b82d6e4SYan 		}
9884b82d6e4SYan 
9892b82032cSYan Zheng 		btrfs_close_devices(fs_devices);
9906c41761fSDavid Sterba 		free_fs_info(fs_info);
9914b82d6e4SYan 	} else {
9924b82d6e4SYan 		char b[BDEVNAME_SIZE];
9934b82d6e4SYan 
9949e1f1de0SAl Viro 		s->s_flags = flags | MS_NOSEC;
9954b82d6e4SYan 		strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
9965f524444SIlya Dryomov 		btrfs_sb(s)->fs_info->bdev_holder = fs_type;
9978a4b83ccSChris Mason 		error = btrfs_fill_super(s, fs_devices, data,
9988a4b83ccSChris Mason 					 flags & MS_SILENT ? 1 : 0);
9994b82d6e4SYan 		if (error) {
10006f5bbff9SAl Viro 			deactivate_locked_super(s);
1001830c4adbSJosef Bacik 			return ERR_PTR(error);
10024b82d6e4SYan 		}
10034b82d6e4SYan 
10044b82d6e4SYan 		s->s_flags |= MS_ACTIVE;
10054b82d6e4SYan 	}
10064b82d6e4SYan 
1007e15d0542SXin Zhong 	root = get_default_root(s, subvol_objectid);
1008e15d0542SXin Zhong 	if (IS_ERR(root)) {
1009e15d0542SXin Zhong 		deactivate_locked_super(s);
1010830c4adbSJosef Bacik 		return root;
101176fcef19SDavid Woodhouse 	}
10124b82d6e4SYan 
1013061dbc6bSAl Viro 	return root;
10144b82d6e4SYan 
1015c146afadSYan Zheng error_close_devices:
10168a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
101704d21a24SIlya Dryomov error_fs_info:
10186c41761fSDavid Sterba 	free_fs_info(fs_info);
1019061dbc6bSAl Viro 	return ERR_PTR(error);
10204b82d6e4SYan }
10212e635a27SChris Mason 
1022c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1023c146afadSYan Zheng {
1024c146afadSYan Zheng 	struct btrfs_root *root = btrfs_sb(sb);
1025c146afadSYan Zheng 	int ret;
1026c146afadSYan Zheng 
1027b288052eSChris Mason 	ret = btrfs_parse_options(root, data);
1028b288052eSChris Mason 	if (ret)
1029b288052eSChris Mason 		return -EINVAL;
1030b288052eSChris Mason 
1031c146afadSYan Zheng 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
1032c146afadSYan Zheng 		return 0;
1033c146afadSYan Zheng 
1034c146afadSYan Zheng 	if (*flags & MS_RDONLY) {
1035c146afadSYan Zheng 		sb->s_flags |= MS_RDONLY;
1036c146afadSYan Zheng 
1037c146afadSYan Zheng 		ret =  btrfs_commit_super(root);
1038c146afadSYan Zheng 		WARN_ON(ret);
1039c146afadSYan Zheng 	} else {
10402b82032cSYan Zheng 		if (root->fs_info->fs_devices->rw_devices == 0)
10412b82032cSYan Zheng 			return -EACCES;
10422b82032cSYan Zheng 
10436c41761fSDavid Sterba 		if (btrfs_super_log_root(root->fs_info->super_copy) != 0)
1044c146afadSYan Zheng 			return -EINVAL;
1045c146afadSYan Zheng 
1046d68fc57bSYan, Zheng 		ret = btrfs_cleanup_fs_roots(root->fs_info);
1047c146afadSYan Zheng 		WARN_ON(ret);
1048c146afadSYan Zheng 
1049d68fc57bSYan, Zheng 		/* recover relocation */
1050d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
1051c146afadSYan Zheng 		WARN_ON(ret);
1052c146afadSYan Zheng 
1053c146afadSYan Zheng 		sb->s_flags &= ~MS_RDONLY;
1054c146afadSYan Zheng 	}
1055c146afadSYan Zheng 
1056c146afadSYan Zheng 	return 0;
1057c146afadSYan Zheng }
1058c146afadSYan Zheng 
1059bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
1060bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1,
1061bcd53741SArne Jansen 				       const void *dev_info2)
1062bcd53741SArne Jansen {
1063bcd53741SArne Jansen 	if (((struct btrfs_device_info *)dev_info1)->max_avail >
1064bcd53741SArne Jansen 	    ((struct btrfs_device_info *)dev_info2)->max_avail)
1065bcd53741SArne Jansen 		return -1;
1066bcd53741SArne Jansen 	else if (((struct btrfs_device_info *)dev_info1)->max_avail <
1067bcd53741SArne Jansen 		 ((struct btrfs_device_info *)dev_info2)->max_avail)
1068bcd53741SArne Jansen 		return 1;
1069bcd53741SArne Jansen 	else
1070bcd53741SArne Jansen 	return 0;
1071bcd53741SArne Jansen }
1072bcd53741SArne Jansen 
1073bcd53741SArne Jansen /*
1074bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
1075bcd53741SArne Jansen  * is stored.(Descending Sort)
1076bcd53741SArne Jansen  */
1077bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
1078bcd53741SArne Jansen 					struct btrfs_device_info *devices,
1079bcd53741SArne Jansen 					size_t nr_devices)
1080bcd53741SArne Jansen {
1081bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1082bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
1083bcd53741SArne Jansen }
1084bcd53741SArne Jansen 
10856d07bcecSMiao Xie /*
10866d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
10876d07bcecSMiao Xie  * file data.
10886d07bcecSMiao Xie  */
10896d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
10906d07bcecSMiao Xie {
10916d07bcecSMiao Xie 	struct btrfs_fs_info *fs_info = root->fs_info;
10926d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
10936d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
10946d07bcecSMiao Xie 	struct btrfs_device *device;
10956d07bcecSMiao Xie 	u64 skip_space;
10966d07bcecSMiao Xie 	u64 type;
10976d07bcecSMiao Xie 	u64 avail_space;
10986d07bcecSMiao Xie 	u64 used_space;
10996d07bcecSMiao Xie 	u64 min_stripe_size;
110039fb26c3SMiao Xie 	int min_stripes = 1, num_stripes = 1;
11016d07bcecSMiao Xie 	int i = 0, nr_devices;
11026d07bcecSMiao Xie 	int ret;
11036d07bcecSMiao Xie 
1104b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
11056d07bcecSMiao Xie 	BUG_ON(!nr_devices);
11066d07bcecSMiao Xie 
11076d07bcecSMiao Xie 	devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
11086d07bcecSMiao Xie 			       GFP_NOFS);
11096d07bcecSMiao Xie 	if (!devices_info)
11106d07bcecSMiao Xie 		return -ENOMEM;
11116d07bcecSMiao Xie 
11126d07bcecSMiao Xie 	/* calc min stripe number for data space alloction */
11136d07bcecSMiao Xie 	type = btrfs_get_alloc_profile(root, 1);
111439fb26c3SMiao Xie 	if (type & BTRFS_BLOCK_GROUP_RAID0) {
11156d07bcecSMiao Xie 		min_stripes = 2;
111639fb26c3SMiao Xie 		num_stripes = nr_devices;
111739fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID1) {
11186d07bcecSMiao Xie 		min_stripes = 2;
111939fb26c3SMiao Xie 		num_stripes = 2;
112039fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID10) {
11216d07bcecSMiao Xie 		min_stripes = 4;
112239fb26c3SMiao Xie 		num_stripes = 4;
112339fb26c3SMiao Xie 	}
11246d07bcecSMiao Xie 
11256d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_DUP)
11266d07bcecSMiao Xie 		min_stripe_size = 2 * BTRFS_STRIPE_LEN;
11276d07bcecSMiao Xie 	else
11286d07bcecSMiao Xie 		min_stripe_size = BTRFS_STRIPE_LEN;
11296d07bcecSMiao Xie 
1130b772a86eSLi Zefan 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
1131b772a86eSLi Zefan 		if (!device->in_fs_metadata || !device->bdev)
11326d07bcecSMiao Xie 			continue;
11336d07bcecSMiao Xie 
11346d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
11356d07bcecSMiao Xie 
11366d07bcecSMiao Xie 		/* align with stripe_len */
11376d07bcecSMiao Xie 		do_div(avail_space, BTRFS_STRIPE_LEN);
11386d07bcecSMiao Xie 		avail_space *= BTRFS_STRIPE_LEN;
11396d07bcecSMiao Xie 
11406d07bcecSMiao Xie 		/*
11416d07bcecSMiao Xie 		 * In order to avoid overwritting the superblock on the drive,
11426d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
11436d07bcecSMiao Xie 		 * allocation.
11446d07bcecSMiao Xie 		 */
11456d07bcecSMiao Xie 		skip_space = 1024 * 1024;
11466d07bcecSMiao Xie 
11476d07bcecSMiao Xie 		/* user can set the offset in fs_info->alloc_start. */
11486d07bcecSMiao Xie 		if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
11496d07bcecSMiao Xie 		    device->total_bytes)
11506d07bcecSMiao Xie 			skip_space = max(fs_info->alloc_start, skip_space);
11516d07bcecSMiao Xie 
11526d07bcecSMiao Xie 		/*
11536d07bcecSMiao Xie 		 * btrfs can not use the free space in [0, skip_space - 1],
11546d07bcecSMiao Xie 		 * we must subtract it from the total. In order to implement
11556d07bcecSMiao Xie 		 * it, we account the used space in this range first.
11566d07bcecSMiao Xie 		 */
11576d07bcecSMiao Xie 		ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
11586d07bcecSMiao Xie 						     &used_space);
11596d07bcecSMiao Xie 		if (ret) {
11606d07bcecSMiao Xie 			kfree(devices_info);
11616d07bcecSMiao Xie 			return ret;
11626d07bcecSMiao Xie 		}
11636d07bcecSMiao Xie 
11646d07bcecSMiao Xie 		/* calc the free space in [0, skip_space - 1] */
11656d07bcecSMiao Xie 		skip_space -= used_space;
11666d07bcecSMiao Xie 
11676d07bcecSMiao Xie 		/*
11686d07bcecSMiao Xie 		 * we can use the free space in [0, skip_space - 1], subtract
11696d07bcecSMiao Xie 		 * it from the total.
11706d07bcecSMiao Xie 		 */
11716d07bcecSMiao Xie 		if (avail_space && avail_space >= skip_space)
11726d07bcecSMiao Xie 			avail_space -= skip_space;
11736d07bcecSMiao Xie 		else
11746d07bcecSMiao Xie 			avail_space = 0;
11756d07bcecSMiao Xie 
11766d07bcecSMiao Xie 		if (avail_space < min_stripe_size)
11776d07bcecSMiao Xie 			continue;
11786d07bcecSMiao Xie 
11796d07bcecSMiao Xie 		devices_info[i].dev = device;
11806d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
11816d07bcecSMiao Xie 
11826d07bcecSMiao Xie 		i++;
11836d07bcecSMiao Xie 	}
11846d07bcecSMiao Xie 
11856d07bcecSMiao Xie 	nr_devices = i;
11866d07bcecSMiao Xie 
11876d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
11886d07bcecSMiao Xie 
11896d07bcecSMiao Xie 	i = nr_devices - 1;
11906d07bcecSMiao Xie 	avail_space = 0;
11916d07bcecSMiao Xie 	while (nr_devices >= min_stripes) {
119239fb26c3SMiao Xie 		if (num_stripes > nr_devices)
119339fb26c3SMiao Xie 			num_stripes = nr_devices;
119439fb26c3SMiao Xie 
11956d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
11966d07bcecSMiao Xie 			int j;
11976d07bcecSMiao Xie 			u64 alloc_size;
11986d07bcecSMiao Xie 
119939fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
12006d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
120139fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
12026d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
12036d07bcecSMiao Xie 		}
12046d07bcecSMiao Xie 		i--;
12056d07bcecSMiao Xie 		nr_devices--;
12066d07bcecSMiao Xie 	}
12076d07bcecSMiao Xie 
12086d07bcecSMiao Xie 	kfree(devices_info);
12096d07bcecSMiao Xie 	*free_bytes = avail_space;
12106d07bcecSMiao Xie 	return 0;
12116d07bcecSMiao Xie }
12126d07bcecSMiao Xie 
12138fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
12148fd17795SChris Mason {
12158fd17795SChris Mason 	struct btrfs_root *root = btrfs_sb(dentry->d_sb);
12166c41761fSDavid Sterba 	struct btrfs_super_block *disk_super = root->fs_info->super_copy;
1217bd4d1088SJosef Bacik 	struct list_head *head = &root->fs_info->space_info;
1218bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
1219bd4d1088SJosef Bacik 	u64 total_used = 0;
12206d07bcecSMiao Xie 	u64 total_free_data = 0;
1221db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
12229d03632eSDavid Woodhouse 	__be32 *fsid = (__be32 *)root->fs_info->fsid;
12236d07bcecSMiao Xie 	int ret;
12248fd17795SChris Mason 
12256d07bcecSMiao Xie 	/* holding chunk_muext to avoid allocating new chunks */
12266d07bcecSMiao Xie 	mutex_lock(&root->fs_info->chunk_mutex);
1227bd4d1088SJosef Bacik 	rcu_read_lock();
122889a55897SJosef Bacik 	list_for_each_entry_rcu(found, head, list) {
12296d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
12306d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
12316d07bcecSMiao Xie 			total_free_data -=
12326d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
12336d07bcecSMiao Xie 		}
12346d07bcecSMiao Xie 
1235b742bb82SYan, Zheng 		total_used += found->disk_used;
123689a55897SJosef Bacik 	}
1237bd4d1088SJosef Bacik 	rcu_read_unlock();
1238bd4d1088SJosef Bacik 
12398fd17795SChris Mason 	buf->f_namelen = BTRFS_NAME_LEN;
1240db94535dSChris Mason 	buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
1241bd4d1088SJosef Bacik 	buf->f_bfree = buf->f_blocks - (total_used >> bits);
12428fd17795SChris Mason 	buf->f_bsize = dentry->d_sb->s_blocksize;
12438fd17795SChris Mason 	buf->f_type = BTRFS_SUPER_MAGIC;
12446d07bcecSMiao Xie 	buf->f_bavail = total_free_data;
12456d07bcecSMiao Xie 	ret = btrfs_calc_avail_data_space(root, &total_free_data);
12466d07bcecSMiao Xie 	if (ret) {
12476d07bcecSMiao Xie 		mutex_unlock(&root->fs_info->chunk_mutex);
12486d07bcecSMiao Xie 		return ret;
12496d07bcecSMiao Xie 	}
12506d07bcecSMiao Xie 	buf->f_bavail += total_free_data;
12516d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
12526d07bcecSMiao Xie 	mutex_unlock(&root->fs_info->chunk_mutex);
1253d397712bSChris Mason 
12549d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
12559d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
12569d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
12579d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
12589d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
125932d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
126032d48fa1SDavid Woodhouse 	buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
126132d48fa1SDavid Woodhouse 	buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
126232d48fa1SDavid Woodhouse 
12638fd17795SChris Mason 	return 0;
12648fd17795SChris Mason }
1265b5133862SChris Mason 
12662e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
12672e635a27SChris Mason 	.owner		= THIS_MODULE,
12682e635a27SChris Mason 	.name		= "btrfs",
1269061dbc6bSAl Viro 	.mount		= btrfs_mount,
1270a061fc8dSChris Mason 	.kill_sb	= kill_anon_super,
12712e635a27SChris Mason 	.fs_flags	= FS_REQUIRES_DEV,
12722e635a27SChris Mason };
1273a9218f6bSChris Mason 
1274d352ac68SChris Mason /*
1275d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
1276d352ac68SChris Mason  */
12778a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
12788a4b83ccSChris Mason 				unsigned long arg)
12798a4b83ccSChris Mason {
12808a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
12818a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
1282c071fcfdSChris Mason 	int ret = -ENOTTY;
12838a4b83ccSChris Mason 
1284e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1285e441d54dSChris Mason 		return -EPERM;
1286e441d54dSChris Mason 
1287dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
1288dae7b665SLi Zefan 	if (IS_ERR(vol))
1289dae7b665SLi Zefan 		return PTR_ERR(vol);
1290c071fcfdSChris Mason 
12918a4b83ccSChris Mason 	switch (cmd) {
12928a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
129397288f2cSChristoph Hellwig 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
12948a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
12958a4b83ccSChris Mason 		break;
12968a4b83ccSChris Mason 	}
1297dae7b665SLi Zefan 
12988a4b83ccSChris Mason 	kfree(vol);
1299f819d837SLinda Knippers 	return ret;
13008a4b83ccSChris Mason }
13018a4b83ccSChris Mason 
13020176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
1303ed0dab6bSYan {
1304ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
1305a74a4b97SChris Mason 	mutex_lock(&root->fs_info->transaction_kthread_mutex);
1306a74a4b97SChris Mason 	mutex_lock(&root->fs_info->cleaner_mutex);
13070176260fSLinus Torvalds 	return 0;
1308ed0dab6bSYan }
1309ed0dab6bSYan 
13100176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb)
1311ed0dab6bSYan {
1312ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
1313a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->cleaner_mutex);
1314a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->transaction_kthread_mutex);
13150176260fSLinus Torvalds 	return 0;
1316ed0dab6bSYan }
13172e635a27SChris Mason 
131822c44fe6SJosef Bacik static void btrfs_fs_dirty_inode(struct inode *inode, int flags)
131922c44fe6SJosef Bacik {
132022c44fe6SJosef Bacik 	int ret;
132122c44fe6SJosef Bacik 
132222c44fe6SJosef Bacik 	ret = btrfs_dirty_inode(inode);
132322c44fe6SJosef Bacik 	if (ret)
132422c44fe6SJosef Bacik 		printk_ratelimited(KERN_ERR "btrfs: fail to dirty inode %Lu "
132522c44fe6SJosef Bacik 				   "error %d\n", btrfs_ino(inode), ret);
132622c44fe6SJosef Bacik }
132722c44fe6SJosef Bacik 
1328b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
132976dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
1330bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
1331e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
1332d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
1333a9572a15SEric Paris 	.show_options	= btrfs_show_options,
13344730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
133522c44fe6SJosef Bacik 	.dirty_inode	= btrfs_fs_dirty_inode,
13362c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
13372c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
13388fd17795SChris Mason 	.statfs		= btrfs_statfs,
1339c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
13400176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
13410176260fSLinus Torvalds 	.unfreeze_fs	= btrfs_unfreeze,
1342e20d96d6SChris Mason };
1343a9218f6bSChris Mason 
1344a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
1345a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
1346a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
1347a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
13486038f373SArnd Bergmann 	.llseek = noop_llseek,
1349a9218f6bSChris Mason };
1350a9218f6bSChris Mason 
1351a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
1352578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
1353a9218f6bSChris Mason 	.name		= "btrfs-control",
1354a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
1355a9218f6bSChris Mason };
1356a9218f6bSChris Mason 
1357578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
1358578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
1359578454ffSKay Sievers 
1360a9218f6bSChris Mason static int btrfs_interface_init(void)
1361a9218f6bSChris Mason {
1362a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
1363a9218f6bSChris Mason }
1364a9218f6bSChris Mason 
1365b2950863SChristoph Hellwig static void btrfs_interface_exit(void)
1366a9218f6bSChris Mason {
1367a9218f6bSChris Mason 	if (misc_deregister(&btrfs_misc) < 0)
1368d397712bSChris Mason 		printk(KERN_INFO "misc_deregister failed for control device");
1369a9218f6bSChris Mason }
1370a9218f6bSChris Mason 
13712e635a27SChris Mason static int __init init_btrfs_fs(void)
13722e635a27SChris Mason {
13732c90e5d6SChris Mason 	int err;
137458176a96SJosef Bacik 
137558176a96SJosef Bacik 	err = btrfs_init_sysfs();
137658176a96SJosef Bacik 	if (err)
137758176a96SJosef Bacik 		return err;
137858176a96SJosef Bacik 
1379261507a0SLi Zefan 	err = btrfs_init_compress();
13802c90e5d6SChris Mason 	if (err)
1381a74a4b97SChris Mason 		goto free_sysfs;
1382d1310b2eSChris Mason 
1383261507a0SLi Zefan 	err = btrfs_init_cachep();
1384261507a0SLi Zefan 	if (err)
1385261507a0SLi Zefan 		goto free_compress;
1386261507a0SLi Zefan 
1387d1310b2eSChris Mason 	err = extent_io_init();
13882f4cbe64SWyatt Banks 	if (err)
13892f4cbe64SWyatt Banks 		goto free_cachep;
13902f4cbe64SWyatt Banks 
1391d1310b2eSChris Mason 	err = extent_map_init();
1392d1310b2eSChris Mason 	if (err)
1393d1310b2eSChris Mason 		goto free_extent_io;
1394d1310b2eSChris Mason 
139516cdcec7SMiao Xie 	err = btrfs_delayed_inode_init();
13962f4cbe64SWyatt Banks 	if (err)
13972f4cbe64SWyatt Banks 		goto free_extent_map;
1398c8b97818SChris Mason 
139916cdcec7SMiao Xie 	err = btrfs_interface_init();
140016cdcec7SMiao Xie 	if (err)
140116cdcec7SMiao Xie 		goto free_delayed_inode;
140216cdcec7SMiao Xie 
1403a9218f6bSChris Mason 	err = register_filesystem(&btrfs_fs_type);
1404a9218f6bSChris Mason 	if (err)
1405a9218f6bSChris Mason 		goto unregister_ioctl;
1406b3c3da71SChris Mason 
1407*e565d4b9SJan Schmidt 	btrfs_init_lockdep();
1408*e565d4b9SJan Schmidt 
1409b3c3da71SChris Mason 	printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
14102f4cbe64SWyatt Banks 	return 0;
14112f4cbe64SWyatt Banks 
1412a9218f6bSChris Mason unregister_ioctl:
1413a9218f6bSChris Mason 	btrfs_interface_exit();
141416cdcec7SMiao Xie free_delayed_inode:
141516cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
14162f4cbe64SWyatt Banks free_extent_map:
14172f4cbe64SWyatt Banks 	extent_map_exit();
1418d1310b2eSChris Mason free_extent_io:
1419d1310b2eSChris Mason 	extent_io_exit();
14202f4cbe64SWyatt Banks free_cachep:
14212f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
1422261507a0SLi Zefan free_compress:
1423261507a0SLi Zefan 	btrfs_exit_compress();
1424a74a4b97SChris Mason free_sysfs:
14252f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
14262c90e5d6SChris Mason 	return err;
14272e635a27SChris Mason }
14282e635a27SChris Mason 
14292e635a27SChris Mason static void __exit exit_btrfs_fs(void)
14302e635a27SChris Mason {
143139279cc3SChris Mason 	btrfs_destroy_cachep();
143216cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
1433a52d9a80SChris Mason 	extent_map_exit();
1434d1310b2eSChris Mason 	extent_io_exit();
1435a9218f6bSChris Mason 	btrfs_interface_exit();
14362e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
143758176a96SJosef Bacik 	btrfs_exit_sysfs();
14388a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
1439261507a0SLi Zefan 	btrfs_exit_compress();
14402e635a27SChris Mason }
14412e635a27SChris Mason 
14422e635a27SChris Mason module_init(init_btrfs_fs)
14432e635a27SChris Mason module_exit(exit_btrfs_fs)
14442e635a27SChris Mason 
14452e635a27SChris Mason MODULE_LICENSE("GPL");
1446