xref: /linux/fs/btrfs/super.c (revision 59553edf110e5576d91be9dd5bd53d110e0d0290)
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>
444b4e25f2SChris Mason #include "compat.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"
50c5739bbaSChris Mason #include "ioctl.h"
513a686375SChris Mason #include "print-tree.h"
525103e947SJosef Bacik #include "xattr.h"
538a4b83ccSChris Mason #include "volumes.h"
54b3c3da71SChris Mason #include "version.h"
55be6e8dc0SBalaji Rao #include "export.h"
56c8b97818SChris Mason #include "compression.h"
572e635a27SChris Mason 
581abe9b8aSliubo #define CREATE_TRACE_POINTS
591abe9b8aSliubo #include <trace/events/btrfs.h>
601abe9b8aSliubo 
61b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
62830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type;
63e20d96d6SChris Mason 
64acce952bSliubo static const char *btrfs_decode_error(struct btrfs_fs_info *fs_info, int errno,
65acce952bSliubo 				      char nbuf[16])
66acce952bSliubo {
67acce952bSliubo 	char *errstr = NULL;
68acce952bSliubo 
69acce952bSliubo 	switch (errno) {
70acce952bSliubo 	case -EIO:
71acce952bSliubo 		errstr = "IO failure";
72acce952bSliubo 		break;
73acce952bSliubo 	case -ENOMEM:
74acce952bSliubo 		errstr = "Out of memory";
75acce952bSliubo 		break;
76acce952bSliubo 	case -EROFS:
77acce952bSliubo 		errstr = "Readonly filesystem";
78acce952bSliubo 		break;
79acce952bSliubo 	default:
80acce952bSliubo 		if (nbuf) {
81acce952bSliubo 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
82acce952bSliubo 				errstr = nbuf;
83acce952bSliubo 		}
84acce952bSliubo 		break;
85acce952bSliubo 	}
86acce952bSliubo 
87acce952bSliubo 	return errstr;
88acce952bSliubo }
89acce952bSliubo 
90acce952bSliubo static void __save_error_info(struct btrfs_fs_info *fs_info)
91acce952bSliubo {
92acce952bSliubo 	/*
93acce952bSliubo 	 * today we only save the error info into ram.  Long term we'll
94acce952bSliubo 	 * also send it down to the disk
95acce952bSliubo 	 */
96acce952bSliubo 	fs_info->fs_state = BTRFS_SUPER_FLAG_ERROR;
97acce952bSliubo }
98acce952bSliubo 
99acce952bSliubo /* NOTE:
100acce952bSliubo  *	We move write_super stuff at umount in order to avoid deadlock
101acce952bSliubo  *	for umount hold all lock.
102acce952bSliubo  */
103acce952bSliubo static void save_error_info(struct btrfs_fs_info *fs_info)
104acce952bSliubo {
105acce952bSliubo 	__save_error_info(fs_info);
106acce952bSliubo }
107acce952bSliubo 
108acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */
109acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
110acce952bSliubo {
111acce952bSliubo 	struct super_block *sb = fs_info->sb;
112acce952bSliubo 
113acce952bSliubo 	if (sb->s_flags & MS_RDONLY)
114acce952bSliubo 		return;
115acce952bSliubo 
116acce952bSliubo 	if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
117acce952bSliubo 		sb->s_flags |= MS_RDONLY;
118acce952bSliubo 		printk(KERN_INFO "btrfs is forced readonly\n");
119acce952bSliubo 	}
120acce952bSliubo }
121acce952bSliubo 
122acce952bSliubo /*
123acce952bSliubo  * __btrfs_std_error decodes expected errors from the caller and
124acce952bSliubo  * invokes the approciate error response.
125acce952bSliubo  */
126acce952bSliubo void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
127acce952bSliubo 		     unsigned int line, int errno)
128acce952bSliubo {
129acce952bSliubo 	struct super_block *sb = fs_info->sb;
130acce952bSliubo 	char nbuf[16];
131acce952bSliubo 	const char *errstr;
132acce952bSliubo 
133acce952bSliubo 	/*
134acce952bSliubo 	 * Special case: if the error is EROFS, and we're already
135acce952bSliubo 	 * under MS_RDONLY, then it is safe here.
136acce952bSliubo 	 */
137acce952bSliubo 	if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
138acce952bSliubo 		return;
139acce952bSliubo 
140acce952bSliubo 	errstr = btrfs_decode_error(fs_info, errno, nbuf);
141acce952bSliubo 	printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: %s\n",
142acce952bSliubo 		sb->s_id, function, line, errstr);
143acce952bSliubo 	save_error_info(fs_info);
144acce952bSliubo 
145acce952bSliubo 	btrfs_handle_error(fs_info);
146acce952bSliubo }
147acce952bSliubo 
148e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
149e20d96d6SChris Mason {
150aea52e19SAl Viro 	(void)close_ctree(btrfs_sb(sb));
151aea52e19SAl Viro 	/* FIXME: need to fix VFS to return error? */
152aea52e19SAl Viro 	/* AV: return it _where_?  ->put_super() can be triggered by any number
153aea52e19SAl Viro 	 * of async events, up to and including delivery of SIGKILL to the
154aea52e19SAl Viro 	 * last process that kept it busy.  Or segfault in the aforementioned
155aea52e19SAl Viro 	 * process...  Whom would you report that to?
156aea52e19SAl Viro 	 */
157e20d96d6SChris Mason }
1582e635a27SChris Mason 
15995e05289SChris Mason enum {
16073f73415SJosef Bacik 	Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
161287a0ab9SJosef Bacik 	Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
162287a0ab9SJosef Bacik 	Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
163261507a0SLi Zefan 	Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
164261507a0SLi Zefan 	Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
16591435650SChris Mason 	Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed,
1664b9465cbSChris Mason 	Opt_enospc_debug, Opt_subvolrootid, Opt_defrag,
167af31f5e5SChris Mason 	Opt_inode_cache, Opt_no_space_cache, Opt_recovery, Opt_err,
16895e05289SChris Mason };
16995e05289SChris Mason 
17095e05289SChris Mason static match_table_t tokens = {
171dfe25020SChris Mason 	{Opt_degraded, "degraded"},
17295e05289SChris Mason 	{Opt_subvol, "subvol=%s"},
17373f73415SJosef Bacik 	{Opt_subvolid, "subvolid=%d"},
17443e570b0SChristoph Hellwig 	{Opt_device, "device=%s"},
175b6cda9bcSChris Mason 	{Opt_nodatasum, "nodatasum"},
176be20aa9dSChris Mason 	{Opt_nodatacow, "nodatacow"},
17721ad10cfSChris Mason 	{Opt_nobarrier, "nobarrier"},
1786f568d35SChris Mason 	{Opt_max_inline, "max_inline=%s"},
1798f662a76SChris Mason 	{Opt_alloc_start, "alloc_start=%s"},
1804543df7eSChris Mason 	{Opt_thread_pool, "thread_pool=%d"},
181c8b97818SChris Mason 	{Opt_compress, "compress"},
182261507a0SLi Zefan 	{Opt_compress_type, "compress=%s"},
183a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
184261507a0SLi Zefan 	{Opt_compress_force_type, "compress-force=%s"},
185e18e4809SChris Mason 	{Opt_ssd, "ssd"},
186451d7585SChris Mason 	{Opt_ssd_spread, "ssd_spread"},
1873b30c22fSChris Mason 	{Opt_nossd, "nossd"},
18833268eafSJosef Bacik 	{Opt_noacl, "noacl"},
1893a5e1404SSage Weil 	{Opt_notreelog, "notreelog"},
190dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
19197e728d4SJosef Bacik 	{Opt_ratio, "metadata_ratio=%d"},
192e244a0aeSChristoph Hellwig 	{Opt_discard, "discard"},
1930af3d00bSJosef Bacik 	{Opt_space_cache, "space_cache"},
19488c2ba3bSJosef Bacik 	{Opt_clear_cache, "clear_cache"},
1954260f7c7SSage Weil 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
19691435650SChris Mason 	{Opt_enospc_debug, "enospc_debug"},
197e15d0542SXin Zhong 	{Opt_subvolrootid, "subvolrootid=%d"},
1984cb5300bSChris Mason 	{Opt_defrag, "autodefrag"},
1994b9465cbSChris Mason 	{Opt_inode_cache, "inode_cache"},
2008965593eSDavid Sterba 	{Opt_no_space_cache, "nospace_cache"},
201af31f5e5SChris Mason 	{Opt_recovery, "recovery"},
20233268eafSJosef Bacik 	{Opt_err, NULL},
20395e05289SChris Mason };
20495e05289SChris Mason 
205edf24abeSChristoph Hellwig /*
206edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
207edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
208edf24abeSChristoph Hellwig  */
209edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options)
21095e05289SChris Mason {
211edf24abeSChristoph Hellwig 	struct btrfs_fs_info *info = root->fs_info;
21295e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
21373bc1876SJosef Bacik 	char *p, *num, *orig = NULL;
21473bc1876SJosef Bacik 	u64 cache_gen;
2154543df7eSChris Mason 	int intarg;
216a7a3f7caSSage Weil 	int ret = 0;
217261507a0SLi Zefan 	char *compress_type;
218261507a0SLi Zefan 	bool compress_force = false;
219b6cda9bcSChris Mason 
2206c41761fSDavid Sterba 	cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
22173bc1876SJosef Bacik 	if (cache_gen)
22273bc1876SJosef Bacik 		btrfs_set_opt(info->mount_opt, SPACE_CACHE);
22373bc1876SJosef Bacik 
22495e05289SChris Mason 	if (!options)
22573bc1876SJosef Bacik 		goto out;
22695e05289SChris Mason 
227be20aa9dSChris Mason 	/*
228be20aa9dSChris Mason 	 * strsep changes the string, duplicate it because parse_options
229be20aa9dSChris Mason 	 * gets called twice
230be20aa9dSChris Mason 	 */
231be20aa9dSChris Mason 	options = kstrdup(options, GFP_NOFS);
232be20aa9dSChris Mason 	if (!options)
233be20aa9dSChris Mason 		return -ENOMEM;
234be20aa9dSChris Mason 
235da495eccSJosef Bacik 	orig = options;
236be20aa9dSChris Mason 
23795e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
23895e05289SChris Mason 		int token;
23995e05289SChris Mason 		if (!*p)
24095e05289SChris Mason 			continue;
24195e05289SChris Mason 
24295e05289SChris Mason 		token = match_token(p, tokens, args);
24395e05289SChris Mason 		switch (token) {
244dfe25020SChris Mason 		case Opt_degraded:
245edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: allowing degraded mounts\n");
246dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
247dfe25020SChris Mason 			break;
24895e05289SChris Mason 		case Opt_subvol:
24973f73415SJosef Bacik 		case Opt_subvolid:
250e15d0542SXin Zhong 		case Opt_subvolrootid:
25143e570b0SChristoph Hellwig 		case Opt_device:
252edf24abeSChristoph Hellwig 			/*
25343e570b0SChristoph Hellwig 			 * These are parsed by btrfs_parse_early_options
254edf24abeSChristoph Hellwig 			 * and can be happily ignored here.
255edf24abeSChristoph Hellwig 			 */
25695e05289SChris Mason 			break;
257b6cda9bcSChris Mason 		case Opt_nodatasum:
258067c28adSChris Mason 			printk(KERN_INFO "btrfs: setting nodatasum\n");
259b6cda9bcSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
260be20aa9dSChris Mason 			break;
261be20aa9dSChris Mason 		case Opt_nodatacow:
262edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: setting nodatacow\n");
263be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
264be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
265b6cda9bcSChris Mason 			break;
266a555f810SChris Mason 		case Opt_compress_force:
267261507a0SLi Zefan 		case Opt_compress_force_type:
268261507a0SLi Zefan 			compress_force = true;
269261507a0SLi Zefan 		case Opt_compress:
270261507a0SLi Zefan 		case Opt_compress_type:
271261507a0SLi Zefan 			if (token == Opt_compress ||
272261507a0SLi Zefan 			    token == Opt_compress_force ||
273261507a0SLi Zefan 			    strcmp(args[0].from, "zlib") == 0) {
274261507a0SLi Zefan 				compress_type = "zlib";
275261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
276a6fa6faeSLi Zefan 			} else if (strcmp(args[0].from, "lzo") == 0) {
277a6fa6faeSLi Zefan 				compress_type = "lzo";
278a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
279261507a0SLi Zefan 			} else {
280261507a0SLi Zefan 				ret = -EINVAL;
281261507a0SLi Zefan 				goto out;
282261507a0SLi Zefan 			}
283261507a0SLi Zefan 
284a555f810SChris Mason 			btrfs_set_opt(info->mount_opt, COMPRESS);
285261507a0SLi Zefan 			if (compress_force) {
286261507a0SLi Zefan 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
287261507a0SLi Zefan 				pr_info("btrfs: force %s compression\n",
288261507a0SLi Zefan 					compress_type);
289261507a0SLi Zefan 			} else
290261507a0SLi Zefan 				pr_info("btrfs: use %s compression\n",
291261507a0SLi Zefan 					compress_type);
292a555f810SChris Mason 			break;
293e18e4809SChris Mason 		case Opt_ssd:
294edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
295e18e4809SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
296e18e4809SChris Mason 			break;
297451d7585SChris Mason 		case Opt_ssd_spread:
298451d7585SChris Mason 			printk(KERN_INFO "btrfs: use spread ssd "
299451d7585SChris Mason 			       "allocation scheme\n");
300451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
301451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD_SPREAD);
302451d7585SChris Mason 			break;
3033b30c22fSChris Mason 		case Opt_nossd:
304451d7585SChris Mason 			printk(KERN_INFO "btrfs: not using ssd allocation "
305451d7585SChris Mason 			       "scheme\n");
306c289811cSChris Mason 			btrfs_set_opt(info->mount_opt, NOSSD);
3073b30c22fSChris Mason 			btrfs_clear_opt(info->mount_opt, SSD);
308451d7585SChris Mason 			btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
3093b30c22fSChris Mason 			break;
31021ad10cfSChris Mason 		case Opt_nobarrier:
311edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: turning off barriers\n");
31221ad10cfSChris Mason 			btrfs_set_opt(info->mount_opt, NOBARRIER);
31321ad10cfSChris Mason 			break;
3144543df7eSChris Mason 		case Opt_thread_pool:
3154543df7eSChris Mason 			intarg = 0;
3164543df7eSChris Mason 			match_int(&args[0], &intarg);
3174543df7eSChris Mason 			if (intarg) {
3184543df7eSChris Mason 				info->thread_pool_size = intarg;
3194543df7eSChris Mason 				printk(KERN_INFO "btrfs: thread pool %d\n",
3204543df7eSChris Mason 				       info->thread_pool_size);
3214543df7eSChris Mason 			}
3224543df7eSChris Mason 			break;
3236f568d35SChris Mason 		case Opt_max_inline:
324edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
3256f568d35SChris Mason 			if (num) {
32691748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
3276f568d35SChris Mason 				kfree(num);
3286f568d35SChris Mason 
32915ada040SChris Mason 				if (info->max_inline) {
3306f568d35SChris Mason 					info->max_inline = max_t(u64,
33115ada040SChris Mason 						info->max_inline,
33215ada040SChris Mason 						root->sectorsize);
33315ada040SChris Mason 				}
334edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: max_inline at %llu\n",
33521380931SJoel Becker 					(unsigned long long)info->max_inline);
3366f568d35SChris Mason 			}
3376f568d35SChris Mason 			break;
3388f662a76SChris Mason 		case Opt_alloc_start:
339edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
3408f662a76SChris Mason 			if (num) {
34191748467SAkinobu Mita 				info->alloc_start = memparse(num, NULL);
3428f662a76SChris Mason 				kfree(num);
343edf24abeSChristoph Hellwig 				printk(KERN_INFO
344edf24abeSChristoph Hellwig 					"btrfs: allocations start at %llu\n",
34521380931SJoel Becker 					(unsigned long long)info->alloc_start);
3468f662a76SChris Mason 			}
3478f662a76SChris Mason 			break;
34833268eafSJosef Bacik 		case Opt_noacl:
34933268eafSJosef Bacik 			root->fs_info->sb->s_flags &= ~MS_POSIXACL;
35033268eafSJosef Bacik 			break;
3513a5e1404SSage Weil 		case Opt_notreelog:
3523a5e1404SSage Weil 			printk(KERN_INFO "btrfs: disabling tree log\n");
3533a5e1404SSage Weil 			btrfs_set_opt(info->mount_opt, NOTREELOG);
3543a5e1404SSage Weil 			break;
355dccae999SSage Weil 		case Opt_flushoncommit:
356dccae999SSage Weil 			printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
357dccae999SSage Weil 			btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
358dccae999SSage Weil 			break;
35997e728d4SJosef Bacik 		case Opt_ratio:
36097e728d4SJosef Bacik 			intarg = 0;
36197e728d4SJosef Bacik 			match_int(&args[0], &intarg);
36297e728d4SJosef Bacik 			if (intarg) {
36397e728d4SJosef Bacik 				info->metadata_ratio = intarg;
36497e728d4SJosef Bacik 				printk(KERN_INFO "btrfs: metadata ratio %d\n",
36597e728d4SJosef Bacik 				       info->metadata_ratio);
36697e728d4SJosef Bacik 			}
36797e728d4SJosef Bacik 			break;
368e244a0aeSChristoph Hellwig 		case Opt_discard:
369e244a0aeSChristoph Hellwig 			btrfs_set_opt(info->mount_opt, DISCARD);
370e244a0aeSChristoph Hellwig 			break;
3710af3d00bSJosef Bacik 		case Opt_space_cache:
3720af3d00bSJosef Bacik 			btrfs_set_opt(info->mount_opt, SPACE_CACHE);
3730de90876SJosef Bacik 			break;
37473bc1876SJosef Bacik 		case Opt_no_space_cache:
37573bc1876SJosef Bacik 			printk(KERN_INFO "btrfs: disabling disk space caching\n");
37673bc1876SJosef Bacik 			btrfs_clear_opt(info->mount_opt, SPACE_CACHE);
37773bc1876SJosef Bacik 			break;
3784b9465cbSChris Mason 		case Opt_inode_cache:
3794b9465cbSChris Mason 			printk(KERN_INFO "btrfs: enabling inode map caching\n");
3804b9465cbSChris Mason 			btrfs_set_opt(info->mount_opt, INODE_MAP_CACHE);
3814b9465cbSChris Mason 			break;
38288c2ba3bSJosef Bacik 		case Opt_clear_cache:
38388c2ba3bSJosef Bacik 			printk(KERN_INFO "btrfs: force clearing of disk cache\n");
38488c2ba3bSJosef Bacik 			btrfs_set_opt(info->mount_opt, CLEAR_CACHE);
3850af3d00bSJosef Bacik 			break;
3864260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
3874260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
3884260f7c7SSage Weil 			break;
38991435650SChris Mason 		case Opt_enospc_debug:
39091435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
39191435650SChris Mason 			break;
3924cb5300bSChris Mason 		case Opt_defrag:
3934cb5300bSChris Mason 			printk(KERN_INFO "btrfs: enabling auto defrag");
3944cb5300bSChris Mason 			btrfs_set_opt(info->mount_opt, AUTO_DEFRAG);
3954cb5300bSChris Mason 			break;
396af31f5e5SChris Mason 		case Opt_recovery:
397af31f5e5SChris Mason 			printk(KERN_INFO "btrfs: enabling auto recovery");
398af31f5e5SChris Mason 			btrfs_set_opt(info->mount_opt, RECOVERY);
399af31f5e5SChris Mason 			break;
400a7a3f7caSSage Weil 		case Opt_err:
401a7a3f7caSSage Weil 			printk(KERN_INFO "btrfs: unrecognized mount option "
402a7a3f7caSSage Weil 			       "'%s'\n", p);
403a7a3f7caSSage Weil 			ret = -EINVAL;
404a7a3f7caSSage Weil 			goto out;
40595e05289SChris Mason 		default:
406be20aa9dSChris Mason 			break;
40795e05289SChris Mason 		}
40895e05289SChris Mason 	}
409a7a3f7caSSage Weil out:
41073bc1876SJosef Bacik 	if (!ret && btrfs_test_opt(root, SPACE_CACHE))
41173bc1876SJosef Bacik 		printk(KERN_INFO "btrfs: disk space caching is enabled\n");
412da495eccSJosef Bacik 	kfree(orig);
413a7a3f7caSSage Weil 	return ret;
414edf24abeSChristoph Hellwig }
415edf24abeSChristoph Hellwig 
416edf24abeSChristoph Hellwig /*
417edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
418edf24abeSChristoph Hellwig  *
419edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
420edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
421edf24abeSChristoph Hellwig  */
42297288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags,
42373f73415SJosef Bacik 		void *holder, char **subvol_name, u64 *subvol_objectid,
424e15d0542SXin Zhong 		u64 *subvol_rootid, struct btrfs_fs_devices **fs_devices)
425edf24abeSChristoph Hellwig {
426edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
42783c8c9bdSJeff Liu 	char *device_name, *opts, *orig, *p;
428edf24abeSChristoph Hellwig 	int error = 0;
42973f73415SJosef Bacik 	int intarg;
430edf24abeSChristoph Hellwig 
431edf24abeSChristoph Hellwig 	if (!options)
432830c4adbSJosef Bacik 		return 0;
433edf24abeSChristoph Hellwig 
434edf24abeSChristoph Hellwig 	/*
435edf24abeSChristoph Hellwig 	 * strsep changes the string, duplicate it because parse_options
436edf24abeSChristoph Hellwig 	 * gets called twice
437edf24abeSChristoph Hellwig 	 */
438edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
439edf24abeSChristoph Hellwig 	if (!opts)
440edf24abeSChristoph Hellwig 		return -ENOMEM;
4413f3d0bc0STero Roponen 	orig = opts;
442edf24abeSChristoph Hellwig 
443edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
444edf24abeSChristoph Hellwig 		int token;
445edf24abeSChristoph Hellwig 		if (!*p)
446edf24abeSChristoph Hellwig 			continue;
447edf24abeSChristoph Hellwig 
448edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
449edf24abeSChristoph Hellwig 		switch (token) {
450edf24abeSChristoph Hellwig 		case Opt_subvol:
451a90e8b6fSIlya Dryomov 			kfree(*subvol_name);
452edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
453edf24abeSChristoph Hellwig 			break;
45473f73415SJosef Bacik 		case Opt_subvolid:
45573f73415SJosef Bacik 			intarg = 0;
4564849f01dSJosef Bacik 			error = match_int(&args[0], &intarg);
4574849f01dSJosef Bacik 			if (!error) {
4584849f01dSJosef Bacik 				/* we want the original fs_tree */
4594849f01dSJosef Bacik 				if (!intarg)
4604849f01dSJosef Bacik 					*subvol_objectid =
4614849f01dSJosef Bacik 						BTRFS_FS_TREE_OBJECTID;
4624849f01dSJosef Bacik 				else
46373f73415SJosef Bacik 					*subvol_objectid = intarg;
4644849f01dSJosef Bacik 			}
46573f73415SJosef Bacik 			break;
466e15d0542SXin Zhong 		case Opt_subvolrootid:
467e15d0542SXin Zhong 			intarg = 0;
468e15d0542SXin Zhong 			error = match_int(&args[0], &intarg);
469e15d0542SXin Zhong 			if (!error) {
470e15d0542SXin Zhong 				/* we want the original fs_tree */
471e15d0542SXin Zhong 				if (!intarg)
472e15d0542SXin Zhong 					*subvol_rootid =
473e15d0542SXin Zhong 						BTRFS_FS_TREE_OBJECTID;
474e15d0542SXin Zhong 				else
475e15d0542SXin Zhong 					*subvol_rootid = intarg;
476e15d0542SXin Zhong 			}
477e15d0542SXin Zhong 			break;
47843e570b0SChristoph Hellwig 		case Opt_device:
47983c8c9bdSJeff Liu 			device_name = match_strdup(&args[0]);
48083c8c9bdSJeff Liu 			if (!device_name) {
48183c8c9bdSJeff Liu 				error = -ENOMEM;
48283c8c9bdSJeff Liu 				goto out;
48383c8c9bdSJeff Liu 			}
48483c8c9bdSJeff Liu 			error = btrfs_scan_one_device(device_name,
48543e570b0SChristoph Hellwig 					flags, holder, fs_devices);
48683c8c9bdSJeff Liu 			kfree(device_name);
48743e570b0SChristoph Hellwig 			if (error)
488830c4adbSJosef Bacik 				goto out;
48943e570b0SChristoph Hellwig 			break;
490edf24abeSChristoph Hellwig 		default:
491edf24abeSChristoph Hellwig 			break;
492edf24abeSChristoph Hellwig 		}
493edf24abeSChristoph Hellwig 	}
494edf24abeSChristoph Hellwig 
495edf24abeSChristoph Hellwig out:
496830c4adbSJosef Bacik 	kfree(orig);
497edf24abeSChristoph Hellwig 	return error;
49895e05289SChris Mason }
49995e05289SChris Mason 
50073f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb,
50173f73415SJosef Bacik 				       u64 subvol_objectid)
50273f73415SJosef Bacik {
50373f73415SJosef Bacik 	struct btrfs_root *root = sb->s_fs_info;
50473f73415SJosef Bacik 	struct btrfs_root *new_root;
50573f73415SJosef Bacik 	struct btrfs_dir_item *di;
50673f73415SJosef Bacik 	struct btrfs_path *path;
50773f73415SJosef Bacik 	struct btrfs_key location;
50873f73415SJosef Bacik 	struct inode *inode;
50973f73415SJosef Bacik 	u64 dir_id;
51073f73415SJosef Bacik 	int new = 0;
51173f73415SJosef Bacik 
51273f73415SJosef Bacik 	/*
51373f73415SJosef Bacik 	 * We have a specific subvol we want to mount, just setup location and
51473f73415SJosef Bacik 	 * go look up the root.
51573f73415SJosef Bacik 	 */
51673f73415SJosef Bacik 	if (subvol_objectid) {
51773f73415SJosef Bacik 		location.objectid = subvol_objectid;
51873f73415SJosef Bacik 		location.type = BTRFS_ROOT_ITEM_KEY;
51973f73415SJosef Bacik 		location.offset = (u64)-1;
52073f73415SJosef Bacik 		goto find_root;
52173f73415SJosef Bacik 	}
52273f73415SJosef Bacik 
52373f73415SJosef Bacik 	path = btrfs_alloc_path();
52473f73415SJosef Bacik 	if (!path)
52573f73415SJosef Bacik 		return ERR_PTR(-ENOMEM);
52673f73415SJosef Bacik 	path->leave_spinning = 1;
52773f73415SJosef Bacik 
52873f73415SJosef Bacik 	/*
52973f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
53073f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
53173f73415SJosef Bacik 	 * to mount.
53273f73415SJosef Bacik 	 */
5336c41761fSDavid Sterba 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
53473f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
535b0839166SJulia Lawall 	if (IS_ERR(di)) {
536b0839166SJulia Lawall 		btrfs_free_path(path);
537fb4f6f91SDan Carpenter 		return ERR_CAST(di);
538b0839166SJulia Lawall 	}
53973f73415SJosef Bacik 	if (!di) {
54073f73415SJosef Bacik 		/*
54173f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
54273f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
54373f73415SJosef Bacik 		 * mount to root most subvolume.
54473f73415SJosef Bacik 		 */
54573f73415SJosef Bacik 		btrfs_free_path(path);
54673f73415SJosef Bacik 		dir_id = BTRFS_FIRST_FREE_OBJECTID;
54773f73415SJosef Bacik 		new_root = root->fs_info->fs_root;
54873f73415SJosef Bacik 		goto setup_root;
54973f73415SJosef Bacik 	}
55073f73415SJosef Bacik 
55173f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
55273f73415SJosef Bacik 	btrfs_free_path(path);
55373f73415SJosef Bacik 
55473f73415SJosef Bacik find_root:
55573f73415SJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
55673f73415SJosef Bacik 	if (IS_ERR(new_root))
557d0b678cbSJulia Lawall 		return ERR_CAST(new_root);
55873f73415SJosef Bacik 
55973f73415SJosef Bacik 	if (btrfs_root_refs(&new_root->root_item) == 0)
56073f73415SJosef Bacik 		return ERR_PTR(-ENOENT);
56173f73415SJosef Bacik 
56273f73415SJosef Bacik 	dir_id = btrfs_root_dirid(&new_root->root_item);
56373f73415SJosef Bacik setup_root:
56473f73415SJosef Bacik 	location.objectid = dir_id;
56573f73415SJosef Bacik 	location.type = BTRFS_INODE_ITEM_KEY;
56673f73415SJosef Bacik 	location.offset = 0;
56773f73415SJosef Bacik 
56873f73415SJosef Bacik 	inode = btrfs_iget(sb, &location, new_root, &new);
5694cbd1149SDan Carpenter 	if (IS_ERR(inode))
5704cbd1149SDan Carpenter 		return ERR_CAST(inode);
57173f73415SJosef Bacik 
57273f73415SJosef Bacik 	/*
57373f73415SJosef Bacik 	 * If we're just mounting the root most subvol put the inode and return
57473f73415SJosef Bacik 	 * a reference to the dentry.  We will have already gotten a reference
57573f73415SJosef Bacik 	 * to the inode in btrfs_fill_super so we're good to go.
57673f73415SJosef Bacik 	 */
57773f73415SJosef Bacik 	if (!new && sb->s_root->d_inode == inode) {
57873f73415SJosef Bacik 		iput(inode);
57973f73415SJosef Bacik 		return dget(sb->s_root);
58073f73415SJosef Bacik 	}
58173f73415SJosef Bacik 
582ba5b8958SJosef Bacik 	return d_obtain_alias(inode);
58373f73415SJosef Bacik }
58473f73415SJosef Bacik 
5858a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
5868a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
5878a4b83ccSChris Mason 			    void *data, int silent)
5882e635a27SChris Mason {
5892e635a27SChris Mason 	struct inode *inode;
590e20d96d6SChris Mason 	struct dentry *root_dentry;
59129db78aaSAl Viro 	struct btrfs_root *tree_root = sb->s_fs_info;
59229db78aaSAl Viro 	struct btrfs_fs_info *fs_info = tree_root->fs_info;
5935d4f98a2SYan Zheng 	struct btrfs_key key;
59439279cc3SChris Mason 	int err;
5952e635a27SChris Mason 
5962e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
5972e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
598e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
599af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
600be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
6015103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
6022e635a27SChris Mason 	sb->s_time_gran = 1;
6030eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
60433268eafSJosef Bacik 	sb->s_flags |= MS_POSIXACL;
60549cf6f45SChris Ball #endif
606e20d96d6SChris Mason 
607ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
608ad2b2c80SAl Viro 	if (err) {
609e20d96d6SChris Mason 		printk("btrfs: open_ctree failed\n");
610ad2b2c80SAl Viro 		return err;
611e20d96d6SChris Mason 	}
612b888db2bSChris Mason 
6135d4f98a2SYan Zheng 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6145d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
6155d4f98a2SYan Zheng 	key.offset = 0;
61698c7089cSAl Viro 	inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
6175d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
6185d4f98a2SYan Zheng 		err = PTR_ERR(inode);
61939279cc3SChris Mason 		goto fail_close;
62039279cc3SChris Mason 	}
6212e635a27SChris Mason 
622e20d96d6SChris Mason 	root_dentry = d_alloc_root(inode);
623e20d96d6SChris Mason 	if (!root_dentry) {
6242e635a27SChris Mason 		iput(inode);
62539279cc3SChris Mason 		err = -ENOMEM;
62639279cc3SChris Mason 		goto fail_close;
6272e635a27SChris Mason 	}
62858176a96SJosef Bacik 
629e20d96d6SChris Mason 	sb->s_root = root_dentry;
6306885f308SChris Mason 
6316885f308SChris Mason 	save_mount_options(sb, data);
63290a887c9SDan Magenheimer 	cleancache_init_fs(sb);
633*59553edfSAl Viro 	sb->s_flags |= MS_ACTIVE;
6342e635a27SChris Mason 	return 0;
6352e635a27SChris Mason 
63639279cc3SChris Mason fail_close:
63739279cc3SChris Mason 	close_ctree(tree_root);
638d5719762SChris Mason 	return err;
639d5719762SChris Mason }
640d5719762SChris Mason 
6416bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
642d5719762SChris Mason {
643d5719762SChris Mason 	struct btrfs_trans_handle *trans;
644dccae999SSage Weil 	struct btrfs_root *root = btrfs_sb(sb);
645d5719762SChris Mason 	int ret;
646df2ce34cSChris Mason 
6471abe9b8aSliubo 	trace_btrfs_sync_fs(wait);
6481abe9b8aSliubo 
649d561c025SChris Mason 	if (!wait) {
6507cfcc17eSChris Mason 		filemap_flush(root->fs_info->btree_inode->i_mapping);
651df2ce34cSChris Mason 		return 0;
652d561c025SChris Mason 	}
653771ed689SChris Mason 
65424bbcf04SYan, Zheng 	btrfs_start_delalloc_inodes(root, 0);
65524bbcf04SYan, Zheng 	btrfs_wait_ordered_extents(root, 0, 0);
656771ed689SChris Mason 
657a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
65898d5dc13STsutomu Itoh 	if (IS_ERR(trans))
65998d5dc13STsutomu Itoh 		return PTR_ERR(trans);
660d5719762SChris Mason 	ret = btrfs_commit_transaction(trans, root);
66154aa1f4dSChris Mason 	return ret;
662d5719762SChris Mason }
663d5719762SChris Mason 
66434c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
665a9572a15SEric Paris {
66634c80b1dSAl Viro 	struct btrfs_root *root = btrfs_sb(dentry->d_sb);
667a9572a15SEric Paris 	struct btrfs_fs_info *info = root->fs_info;
668200da64eSTsutomu Itoh 	char *compress_type;
669a9572a15SEric Paris 
670a9572a15SEric Paris 	if (btrfs_test_opt(root, DEGRADED))
671a9572a15SEric Paris 		seq_puts(seq, ",degraded");
672a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATASUM))
673a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
674a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATACOW))
675a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
676a9572a15SEric Paris 	if (btrfs_test_opt(root, NOBARRIER))
677a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
678a9572a15SEric Paris 	if (info->max_inline != 8192 * 1024)
67921380931SJoel Becker 		seq_printf(seq, ",max_inline=%llu",
68021380931SJoel Becker 			   (unsigned long long)info->max_inline);
681a9572a15SEric Paris 	if (info->alloc_start != 0)
68221380931SJoel Becker 		seq_printf(seq, ",alloc_start=%llu",
68321380931SJoel Becker 			   (unsigned long long)info->alloc_start);
684a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
685a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
686a9572a15SEric Paris 		seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
687200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, COMPRESS)) {
688200da64eSTsutomu Itoh 		if (info->compress_type == BTRFS_COMPRESS_ZLIB)
689200da64eSTsutomu Itoh 			compress_type = "zlib";
690200da64eSTsutomu Itoh 		else
691200da64eSTsutomu Itoh 			compress_type = "lzo";
692200da64eSTsutomu Itoh 		if (btrfs_test_opt(root, FORCE_COMPRESS))
693200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
694200da64eSTsutomu Itoh 		else
695200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
696200da64eSTsutomu Itoh 	}
697c289811cSChris Mason 	if (btrfs_test_opt(root, NOSSD))
698c289811cSChris Mason 		seq_puts(seq, ",nossd");
699451d7585SChris Mason 	if (btrfs_test_opt(root, SSD_SPREAD))
700451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
701451d7585SChris Mason 	else if (btrfs_test_opt(root, SSD))
702a9572a15SEric Paris 		seq_puts(seq, ",ssd");
7033a5e1404SSage Weil 	if (btrfs_test_opt(root, NOTREELOG))
7046b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
705dccae999SSage Weil 	if (btrfs_test_opt(root, FLUSHONCOMMIT))
7066b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
70720a5239aSMatthew Wilcox 	if (btrfs_test_opt(root, DISCARD))
70820a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
709a9572a15SEric Paris 	if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
710a9572a15SEric Paris 		seq_puts(seq, ",noacl");
711200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, SPACE_CACHE))
712200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
71373bc1876SJosef Bacik 	else
7148965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
715200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, CLEAR_CACHE))
716200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
717200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
718200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
7190942caa3SDavid Sterba 	if (btrfs_test_opt(root, ENOSPC_DEBUG))
7200942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
7210942caa3SDavid Sterba 	if (btrfs_test_opt(root, AUTO_DEFRAG))
7220942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
7230942caa3SDavid Sterba 	if (btrfs_test_opt(root, INODE_MAP_CACHE))
7240942caa3SDavid Sterba 		seq_puts(seq, ",inode_cache");
725a9572a15SEric Paris 	return 0;
726a9572a15SEric Paris }
727a9572a15SEric Paris 
728a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
7292e635a27SChris Mason {
730450ba0eaSJosef Bacik 	struct btrfs_root *test_root = data;
731a061fc8dSChris Mason 	struct btrfs_root *root = btrfs_sb(s);
7324b82d6e4SYan 
733450ba0eaSJosef Bacik 	return root->fs_info->fs_devices == test_root->fs_info->fs_devices;
7344b82d6e4SYan }
7354b82d6e4SYan 
736450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
737450ba0eaSJosef Bacik {
7386de1d09dSAl Viro 	int err = set_anon_super(s, data);
7396de1d09dSAl Viro 	if (!err)
740450ba0eaSJosef Bacik 		s->s_fs_info = data;
7416de1d09dSAl Viro 	return err;
742450ba0eaSJosef Bacik }
743450ba0eaSJosef Bacik 
744830c4adbSJosef Bacik /*
745f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
746f9d9ef62SDavid Sterba  */
747f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
748f9d9ef62SDavid Sterba {
749f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
750f9d9ef62SDavid Sterba 		return 1;
751f9d9ef62SDavid Sterba 	return 0;
752f9d9ef62SDavid Sterba }
753f9d9ef62SDavid Sterba 
754f9d9ef62SDavid Sterba /*
755830c4adbSJosef Bacik  * This will strip out the subvol=%s argument for an argument string and add
756830c4adbSJosef Bacik  * subvolid=0 to make sure we get the actual tree root for path walking to the
757830c4adbSJosef Bacik  * subvol we want.
758830c4adbSJosef Bacik  */
759830c4adbSJosef Bacik static char *setup_root_args(char *args)
760830c4adbSJosef Bacik {
761830c4adbSJosef Bacik 	unsigned copied = 0;
762830c4adbSJosef Bacik 	unsigned len = strlen(args) + 2;
763830c4adbSJosef Bacik 	char *pos;
764830c4adbSJosef Bacik 	char *ret;
765830c4adbSJosef Bacik 
766830c4adbSJosef Bacik 	/*
767830c4adbSJosef Bacik 	 * We need the same args as before, but minus
768830c4adbSJosef Bacik 	 *
769830c4adbSJosef Bacik 	 * subvol=a
770830c4adbSJosef Bacik 	 *
771830c4adbSJosef Bacik 	 * and add
772830c4adbSJosef Bacik 	 *
773830c4adbSJosef Bacik 	 * subvolid=0
774830c4adbSJosef Bacik 	 *
775830c4adbSJosef Bacik 	 * which is a difference of 2 characters, so we allocate strlen(args) +
776830c4adbSJosef Bacik 	 * 2 characters.
777830c4adbSJosef Bacik 	 */
778830c4adbSJosef Bacik 	ret = kzalloc(len * sizeof(char), GFP_NOFS);
779830c4adbSJosef Bacik 	if (!ret)
780830c4adbSJosef Bacik 		return NULL;
781830c4adbSJosef Bacik 	pos = strstr(args, "subvol=");
782830c4adbSJosef Bacik 
783830c4adbSJosef Bacik 	/* This shouldn't happen, but just in case.. */
784830c4adbSJosef Bacik 	if (!pos) {
785830c4adbSJosef Bacik 		kfree(ret);
786830c4adbSJosef Bacik 		return NULL;
787830c4adbSJosef Bacik 	}
788830c4adbSJosef Bacik 
789830c4adbSJosef Bacik 	/*
790830c4adbSJosef Bacik 	 * The subvol=<> arg is not at the front of the string, copy everybody
791830c4adbSJosef Bacik 	 * up to that into ret.
792830c4adbSJosef Bacik 	 */
793830c4adbSJosef Bacik 	if (pos != args) {
794830c4adbSJosef Bacik 		*pos = '\0';
795830c4adbSJosef Bacik 		strcpy(ret, args);
796830c4adbSJosef Bacik 		copied += strlen(args);
797830c4adbSJosef Bacik 		pos++;
798830c4adbSJosef Bacik 	}
799830c4adbSJosef Bacik 
800830c4adbSJosef Bacik 	strncpy(ret + copied, "subvolid=0", len - copied);
801830c4adbSJosef Bacik 
802830c4adbSJosef Bacik 	/* Length of subvolid=0 */
803830c4adbSJosef Bacik 	copied += 10;
804830c4adbSJosef Bacik 
805830c4adbSJosef Bacik 	/*
806830c4adbSJosef Bacik 	 * If there is no , after the subvol= option then we know there's no
807830c4adbSJosef Bacik 	 * other options and we can just return.
808830c4adbSJosef Bacik 	 */
809830c4adbSJosef Bacik 	pos = strchr(pos, ',');
810830c4adbSJosef Bacik 	if (!pos)
811830c4adbSJosef Bacik 		return ret;
812830c4adbSJosef Bacik 
813830c4adbSJosef Bacik 	/* Copy the rest of the arguments into our buffer */
814830c4adbSJosef Bacik 	strncpy(ret + copied, pos, len - copied);
815830c4adbSJosef Bacik 	copied += strlen(pos);
816830c4adbSJosef Bacik 
817830c4adbSJosef Bacik 	return ret;
818830c4adbSJosef Bacik }
819830c4adbSJosef Bacik 
820830c4adbSJosef Bacik static struct dentry *mount_subvol(const char *subvol_name, int flags,
821830c4adbSJosef Bacik 				   const char *device_name, char *data)
822830c4adbSJosef Bacik {
823830c4adbSJosef Bacik 	struct dentry *root;
824830c4adbSJosef Bacik 	struct vfsmount *mnt;
825830c4adbSJosef Bacik 	char *newargs;
826830c4adbSJosef Bacik 
827830c4adbSJosef Bacik 	newargs = setup_root_args(data);
828830c4adbSJosef Bacik 	if (!newargs)
829830c4adbSJosef Bacik 		return ERR_PTR(-ENOMEM);
830830c4adbSJosef Bacik 	mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name,
831830c4adbSJosef Bacik 			     newargs);
832830c4adbSJosef Bacik 	kfree(newargs);
833830c4adbSJosef Bacik 	if (IS_ERR(mnt))
834830c4adbSJosef Bacik 		return ERR_CAST(mnt);
835830c4adbSJosef Bacik 
836ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
837830c4adbSJosef Bacik 
838ea441d11SAl Viro 	if (!IS_ERR(root) && !is_subvolume_inode(root->d_inode)) {
839ea441d11SAl Viro 		struct super_block *s = root->d_sb;
840ea441d11SAl Viro 		dput(root);
841ea441d11SAl Viro 		root = ERR_PTR(-EINVAL);
842ea441d11SAl Viro 		deactivate_locked_super(s);
843f9d9ef62SDavid Sterba 		printk(KERN_ERR "btrfs: '%s' is not a valid subvolume\n",
844f9d9ef62SDavid Sterba 				subvol_name);
845f9d9ef62SDavid Sterba 	}
846f9d9ef62SDavid Sterba 
847830c4adbSJosef Bacik 	return root;
848830c4adbSJosef Bacik }
849450ba0eaSJosef Bacik 
850edf24abeSChristoph Hellwig /*
851edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
852edf24abeSChristoph Hellwig  *
853edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
854edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
855edf24abeSChristoph Hellwig  */
856061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
857306e16ceSDavid Sterba 		const char *device_name, void *data)
8584b82d6e4SYan {
8594b82d6e4SYan 	struct block_device *bdev = NULL;
8604b82d6e4SYan 	struct super_block *s;
8614b82d6e4SYan 	struct dentry *root;
8628a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
863450ba0eaSJosef Bacik 	struct btrfs_fs_info *fs_info = NULL;
86497288f2cSChristoph Hellwig 	fmode_t mode = FMODE_READ;
86573f73415SJosef Bacik 	char *subvol_name = NULL;
86673f73415SJosef Bacik 	u64 subvol_objectid = 0;
867e15d0542SXin Zhong 	u64 subvol_rootid = 0;
8684b82d6e4SYan 	int error = 0;
8694b82d6e4SYan 
87097288f2cSChristoph Hellwig 	if (!(flags & MS_RDONLY))
87197288f2cSChristoph Hellwig 		mode |= FMODE_WRITE;
87297288f2cSChristoph Hellwig 
87397288f2cSChristoph Hellwig 	error = btrfs_parse_early_options(data, mode, fs_type,
87473f73415SJosef Bacik 					  &subvol_name, &subvol_objectid,
875e15d0542SXin Zhong 					  &subvol_rootid, &fs_devices);
876f23c8af8SIlya Dryomov 	if (error) {
877f23c8af8SIlya Dryomov 		kfree(subvol_name);
878061dbc6bSAl Viro 		return ERR_PTR(error);
879f23c8af8SIlya Dryomov 	}
880edf24abeSChristoph Hellwig 
881830c4adbSJosef Bacik 	if (subvol_name) {
882830c4adbSJosef Bacik 		root = mount_subvol(subvol_name, flags, device_name, data);
883830c4adbSJosef Bacik 		kfree(subvol_name);
884830c4adbSJosef Bacik 		return root;
885830c4adbSJosef Bacik 	}
886830c4adbSJosef Bacik 
887306e16ceSDavid Sterba 	error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
8888a4b83ccSChris Mason 	if (error)
889830c4adbSJosef Bacik 		return ERR_PTR(error);
8904b82d6e4SYan 
891450ba0eaSJosef Bacik 	/*
892450ba0eaSJosef Bacik 	 * Setup a dummy root and fs_info for test/set super.  This is because
893450ba0eaSJosef Bacik 	 * we don't actually fill this stuff out until open_ctree, but we need
894450ba0eaSJosef Bacik 	 * it for searching for existing supers, so this lets us do that and
895450ba0eaSJosef Bacik 	 * then open_ctree will properly initialize everything later.
896450ba0eaSJosef Bacik 	 */
897450ba0eaSJosef Bacik 	fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
89804d21a24SIlya Dryomov 	if (!fs_info)
89904d21a24SIlya Dryomov 		return ERR_PTR(-ENOMEM);
90004d21a24SIlya Dryomov 
9016f07e42eSAl Viro 	fs_info->tree_root = btrfs_alloc_root(fs_info);
90204d21a24SIlya Dryomov 	if (!fs_info->tree_root) {
90345ea6095Sslyich@gmail.com 		error = -ENOMEM;
90404d21a24SIlya Dryomov 		goto error_fs_info;
90545ea6095Sslyich@gmail.com 	}
906450ba0eaSJosef Bacik 	fs_info->fs_devices = fs_devices;
907450ba0eaSJosef Bacik 
9086c41761fSDavid Sterba 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
9096c41761fSDavid Sterba 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
9106c41761fSDavid Sterba 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
9116c41761fSDavid Sterba 		error = -ENOMEM;
91204d21a24SIlya Dryomov 		goto error_fs_info;
91304d21a24SIlya Dryomov 	}
91404d21a24SIlya Dryomov 
91504d21a24SIlya Dryomov 	error = btrfs_open_devices(fs_devices, mode, fs_type);
91604d21a24SIlya Dryomov 	if (error)
91704d21a24SIlya Dryomov 		goto error_fs_info;
91804d21a24SIlya Dryomov 
91904d21a24SIlya Dryomov 	if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
92004d21a24SIlya Dryomov 		error = -EACCES;
9216c41761fSDavid Sterba 		goto error_close_devices;
9226c41761fSDavid Sterba 	}
9236c41761fSDavid Sterba 
924dfe25020SChris Mason 	bdev = fs_devices->latest_bdev;
92504d21a24SIlya Dryomov 	s = sget(fs_type, btrfs_test_super, btrfs_set_super,
92604d21a24SIlya Dryomov 		 fs_info->tree_root);
927830c4adbSJosef Bacik 	if (IS_ERR(s)) {
928830c4adbSJosef Bacik 		error = PTR_ERR(s);
929830c4adbSJosef Bacik 		goto error_close_devices;
930830c4adbSJosef Bacik 	}
9314b82d6e4SYan 
9324b82d6e4SYan 	if (s->s_root) {
9332b82032cSYan Zheng 		btrfs_close_devices(fs_devices);
9346c41761fSDavid Sterba 		free_fs_info(fs_info);
935*59553edfSAl Viro 		if ((flags ^ s->s_flags) & MS_RDONLY)
936*59553edfSAl Viro 			error = -EBUSY;
9374b82d6e4SYan 	} else {
9384b82d6e4SYan 		char b[BDEVNAME_SIZE];
9394b82d6e4SYan 
9409e1f1de0SAl Viro 		s->s_flags = flags | MS_NOSEC;
9414b82d6e4SYan 		strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
9425f524444SIlya Dryomov 		btrfs_sb(s)->fs_info->bdev_holder = fs_type;
9438a4b83ccSChris Mason 		error = btrfs_fill_super(s, fs_devices, data,
9448a4b83ccSChris Mason 					 flags & MS_SILENT ? 1 : 0);
9454b82d6e4SYan 	}
9464b82d6e4SYan 
947*59553edfSAl Viro 	root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error);
948*59553edfSAl Viro 	if (IS_ERR(root))
949e15d0542SXin Zhong 		deactivate_locked_super(s);
9504b82d6e4SYan 
951061dbc6bSAl Viro 	return root;
9524b82d6e4SYan 
953c146afadSYan Zheng error_close_devices:
9548a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
95504d21a24SIlya Dryomov error_fs_info:
9566c41761fSDavid Sterba 	free_fs_info(fs_info);
957061dbc6bSAl Viro 	return ERR_PTR(error);
9584b82d6e4SYan }
9592e635a27SChris Mason 
960c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
961c146afadSYan Zheng {
962c146afadSYan Zheng 	struct btrfs_root *root = btrfs_sb(sb);
963c146afadSYan Zheng 	int ret;
964c146afadSYan Zheng 
965b288052eSChris Mason 	ret = btrfs_parse_options(root, data);
966b288052eSChris Mason 	if (ret)
967b288052eSChris Mason 		return -EINVAL;
968b288052eSChris Mason 
969c146afadSYan Zheng 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
970c146afadSYan Zheng 		return 0;
971c146afadSYan Zheng 
972c146afadSYan Zheng 	if (*flags & MS_RDONLY) {
973c146afadSYan Zheng 		sb->s_flags |= MS_RDONLY;
974c146afadSYan Zheng 
975c146afadSYan Zheng 		ret =  btrfs_commit_super(root);
976c146afadSYan Zheng 		WARN_ON(ret);
977c146afadSYan Zheng 	} else {
9782b82032cSYan Zheng 		if (root->fs_info->fs_devices->rw_devices == 0)
9792b82032cSYan Zheng 			return -EACCES;
9802b82032cSYan Zheng 
9816c41761fSDavid Sterba 		if (btrfs_super_log_root(root->fs_info->super_copy) != 0)
982c146afadSYan Zheng 			return -EINVAL;
983c146afadSYan Zheng 
984d68fc57bSYan, Zheng 		ret = btrfs_cleanup_fs_roots(root->fs_info);
985c146afadSYan Zheng 		WARN_ON(ret);
986c146afadSYan Zheng 
987d68fc57bSYan, Zheng 		/* recover relocation */
988d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
989c146afadSYan Zheng 		WARN_ON(ret);
990c146afadSYan Zheng 
991c146afadSYan Zheng 		sb->s_flags &= ~MS_RDONLY;
992c146afadSYan Zheng 	}
993c146afadSYan Zheng 
994c146afadSYan Zheng 	return 0;
995c146afadSYan Zheng }
996c146afadSYan Zheng 
997bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
998bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1,
999bcd53741SArne Jansen 				       const void *dev_info2)
1000bcd53741SArne Jansen {
1001bcd53741SArne Jansen 	if (((struct btrfs_device_info *)dev_info1)->max_avail >
1002bcd53741SArne Jansen 	    ((struct btrfs_device_info *)dev_info2)->max_avail)
1003bcd53741SArne Jansen 		return -1;
1004bcd53741SArne Jansen 	else if (((struct btrfs_device_info *)dev_info1)->max_avail <
1005bcd53741SArne Jansen 		 ((struct btrfs_device_info *)dev_info2)->max_avail)
1006bcd53741SArne Jansen 		return 1;
1007bcd53741SArne Jansen 	else
1008bcd53741SArne Jansen 	return 0;
1009bcd53741SArne Jansen }
1010bcd53741SArne Jansen 
1011bcd53741SArne Jansen /*
1012bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
1013bcd53741SArne Jansen  * is stored.(Descending Sort)
1014bcd53741SArne Jansen  */
1015bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
1016bcd53741SArne Jansen 					struct btrfs_device_info *devices,
1017bcd53741SArne Jansen 					size_t nr_devices)
1018bcd53741SArne Jansen {
1019bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1020bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
1021bcd53741SArne Jansen }
1022bcd53741SArne Jansen 
10236d07bcecSMiao Xie /*
10246d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
10256d07bcecSMiao Xie  * file data.
10266d07bcecSMiao Xie  */
10276d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
10286d07bcecSMiao Xie {
10296d07bcecSMiao Xie 	struct btrfs_fs_info *fs_info = root->fs_info;
10306d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
10316d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
10326d07bcecSMiao Xie 	struct btrfs_device *device;
10336d07bcecSMiao Xie 	u64 skip_space;
10346d07bcecSMiao Xie 	u64 type;
10356d07bcecSMiao Xie 	u64 avail_space;
10366d07bcecSMiao Xie 	u64 used_space;
10376d07bcecSMiao Xie 	u64 min_stripe_size;
103839fb26c3SMiao Xie 	int min_stripes = 1, num_stripes = 1;
10396d07bcecSMiao Xie 	int i = 0, nr_devices;
10406d07bcecSMiao Xie 	int ret;
10416d07bcecSMiao Xie 
1042b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
10436d07bcecSMiao Xie 	BUG_ON(!nr_devices);
10446d07bcecSMiao Xie 
10456d07bcecSMiao Xie 	devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
10466d07bcecSMiao Xie 			       GFP_NOFS);
10476d07bcecSMiao Xie 	if (!devices_info)
10486d07bcecSMiao Xie 		return -ENOMEM;
10496d07bcecSMiao Xie 
10506d07bcecSMiao Xie 	/* calc min stripe number for data space alloction */
10516d07bcecSMiao Xie 	type = btrfs_get_alloc_profile(root, 1);
105239fb26c3SMiao Xie 	if (type & BTRFS_BLOCK_GROUP_RAID0) {
10536d07bcecSMiao Xie 		min_stripes = 2;
105439fb26c3SMiao Xie 		num_stripes = nr_devices;
105539fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID1) {
10566d07bcecSMiao Xie 		min_stripes = 2;
105739fb26c3SMiao Xie 		num_stripes = 2;
105839fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID10) {
10596d07bcecSMiao Xie 		min_stripes = 4;
106039fb26c3SMiao Xie 		num_stripes = 4;
106139fb26c3SMiao Xie 	}
10626d07bcecSMiao Xie 
10636d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_DUP)
10646d07bcecSMiao Xie 		min_stripe_size = 2 * BTRFS_STRIPE_LEN;
10656d07bcecSMiao Xie 	else
10666d07bcecSMiao Xie 		min_stripe_size = BTRFS_STRIPE_LEN;
10676d07bcecSMiao Xie 
1068b772a86eSLi Zefan 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
1069b772a86eSLi Zefan 		if (!device->in_fs_metadata || !device->bdev)
10706d07bcecSMiao Xie 			continue;
10716d07bcecSMiao Xie 
10726d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
10736d07bcecSMiao Xie 
10746d07bcecSMiao Xie 		/* align with stripe_len */
10756d07bcecSMiao Xie 		do_div(avail_space, BTRFS_STRIPE_LEN);
10766d07bcecSMiao Xie 		avail_space *= BTRFS_STRIPE_LEN;
10776d07bcecSMiao Xie 
10786d07bcecSMiao Xie 		/*
10796d07bcecSMiao Xie 		 * In order to avoid overwritting the superblock on the drive,
10806d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
10816d07bcecSMiao Xie 		 * allocation.
10826d07bcecSMiao Xie 		 */
10836d07bcecSMiao Xie 		skip_space = 1024 * 1024;
10846d07bcecSMiao Xie 
10856d07bcecSMiao Xie 		/* user can set the offset in fs_info->alloc_start. */
10866d07bcecSMiao Xie 		if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
10876d07bcecSMiao Xie 		    device->total_bytes)
10886d07bcecSMiao Xie 			skip_space = max(fs_info->alloc_start, skip_space);
10896d07bcecSMiao Xie 
10906d07bcecSMiao Xie 		/*
10916d07bcecSMiao Xie 		 * btrfs can not use the free space in [0, skip_space - 1],
10926d07bcecSMiao Xie 		 * we must subtract it from the total. In order to implement
10936d07bcecSMiao Xie 		 * it, we account the used space in this range first.
10946d07bcecSMiao Xie 		 */
10956d07bcecSMiao Xie 		ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
10966d07bcecSMiao Xie 						     &used_space);
10976d07bcecSMiao Xie 		if (ret) {
10986d07bcecSMiao Xie 			kfree(devices_info);
10996d07bcecSMiao Xie 			return ret;
11006d07bcecSMiao Xie 		}
11016d07bcecSMiao Xie 
11026d07bcecSMiao Xie 		/* calc the free space in [0, skip_space - 1] */
11036d07bcecSMiao Xie 		skip_space -= used_space;
11046d07bcecSMiao Xie 
11056d07bcecSMiao Xie 		/*
11066d07bcecSMiao Xie 		 * we can use the free space in [0, skip_space - 1], subtract
11076d07bcecSMiao Xie 		 * it from the total.
11086d07bcecSMiao Xie 		 */
11096d07bcecSMiao Xie 		if (avail_space && avail_space >= skip_space)
11106d07bcecSMiao Xie 			avail_space -= skip_space;
11116d07bcecSMiao Xie 		else
11126d07bcecSMiao Xie 			avail_space = 0;
11136d07bcecSMiao Xie 
11146d07bcecSMiao Xie 		if (avail_space < min_stripe_size)
11156d07bcecSMiao Xie 			continue;
11166d07bcecSMiao Xie 
11176d07bcecSMiao Xie 		devices_info[i].dev = device;
11186d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
11196d07bcecSMiao Xie 
11206d07bcecSMiao Xie 		i++;
11216d07bcecSMiao Xie 	}
11226d07bcecSMiao Xie 
11236d07bcecSMiao Xie 	nr_devices = i;
11246d07bcecSMiao Xie 
11256d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
11266d07bcecSMiao Xie 
11276d07bcecSMiao Xie 	i = nr_devices - 1;
11286d07bcecSMiao Xie 	avail_space = 0;
11296d07bcecSMiao Xie 	while (nr_devices >= min_stripes) {
113039fb26c3SMiao Xie 		if (num_stripes > nr_devices)
113139fb26c3SMiao Xie 			num_stripes = nr_devices;
113239fb26c3SMiao Xie 
11336d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
11346d07bcecSMiao Xie 			int j;
11356d07bcecSMiao Xie 			u64 alloc_size;
11366d07bcecSMiao Xie 
113739fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
11386d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
113939fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
11406d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
11416d07bcecSMiao Xie 		}
11426d07bcecSMiao Xie 		i--;
11436d07bcecSMiao Xie 		nr_devices--;
11446d07bcecSMiao Xie 	}
11456d07bcecSMiao Xie 
11466d07bcecSMiao Xie 	kfree(devices_info);
11476d07bcecSMiao Xie 	*free_bytes = avail_space;
11486d07bcecSMiao Xie 	return 0;
11496d07bcecSMiao Xie }
11506d07bcecSMiao Xie 
11518fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
11528fd17795SChris Mason {
11538fd17795SChris Mason 	struct btrfs_root *root = btrfs_sb(dentry->d_sb);
11546c41761fSDavid Sterba 	struct btrfs_super_block *disk_super = root->fs_info->super_copy;
1155bd4d1088SJosef Bacik 	struct list_head *head = &root->fs_info->space_info;
1156bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
1157bd4d1088SJosef Bacik 	u64 total_used = 0;
11586d07bcecSMiao Xie 	u64 total_free_data = 0;
1159db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
11609d03632eSDavid Woodhouse 	__be32 *fsid = (__be32 *)root->fs_info->fsid;
11616d07bcecSMiao Xie 	int ret;
11628fd17795SChris Mason 
11636d07bcecSMiao Xie 	/* holding chunk_muext to avoid allocating new chunks */
11646d07bcecSMiao Xie 	mutex_lock(&root->fs_info->chunk_mutex);
1165bd4d1088SJosef Bacik 	rcu_read_lock();
116689a55897SJosef Bacik 	list_for_each_entry_rcu(found, head, list) {
11676d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
11686d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
11696d07bcecSMiao Xie 			total_free_data -=
11706d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
11716d07bcecSMiao Xie 		}
11726d07bcecSMiao Xie 
1173b742bb82SYan, Zheng 		total_used += found->disk_used;
117489a55897SJosef Bacik 	}
1175bd4d1088SJosef Bacik 	rcu_read_unlock();
1176bd4d1088SJosef Bacik 
11778fd17795SChris Mason 	buf->f_namelen = BTRFS_NAME_LEN;
1178db94535dSChris Mason 	buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
1179bd4d1088SJosef Bacik 	buf->f_bfree = buf->f_blocks - (total_used >> bits);
11808fd17795SChris Mason 	buf->f_bsize = dentry->d_sb->s_blocksize;
11818fd17795SChris Mason 	buf->f_type = BTRFS_SUPER_MAGIC;
11826d07bcecSMiao Xie 	buf->f_bavail = total_free_data;
11836d07bcecSMiao Xie 	ret = btrfs_calc_avail_data_space(root, &total_free_data);
11846d07bcecSMiao Xie 	if (ret) {
11856d07bcecSMiao Xie 		mutex_unlock(&root->fs_info->chunk_mutex);
11866d07bcecSMiao Xie 		return ret;
11876d07bcecSMiao Xie 	}
11886d07bcecSMiao Xie 	buf->f_bavail += total_free_data;
11896d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
11906d07bcecSMiao Xie 	mutex_unlock(&root->fs_info->chunk_mutex);
1191d397712bSChris Mason 
11929d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
11939d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
11949d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
11959d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
11969d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
119732d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
119832d48fa1SDavid Woodhouse 	buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
119932d48fa1SDavid Woodhouse 	buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
120032d48fa1SDavid Woodhouse 
12018fd17795SChris Mason 	return 0;
12028fd17795SChris Mason }
1203b5133862SChris Mason 
1204aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
1205aea52e19SAl Viro {
1206d22ca7deSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb)->fs_info;
1207aea52e19SAl Viro 	kill_anon_super(sb);
1208aea52e19SAl Viro 	free_fs_info(fs_info);
1209aea52e19SAl Viro }
1210aea52e19SAl Viro 
12112e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
12122e635a27SChris Mason 	.owner		= THIS_MODULE,
12132e635a27SChris Mason 	.name		= "btrfs",
1214061dbc6bSAl Viro 	.mount		= btrfs_mount,
1215aea52e19SAl Viro 	.kill_sb	= btrfs_kill_super,
12162e635a27SChris Mason 	.fs_flags	= FS_REQUIRES_DEV,
12172e635a27SChris Mason };
1218a9218f6bSChris Mason 
1219d352ac68SChris Mason /*
1220d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
1221d352ac68SChris Mason  */
12228a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
12238a4b83ccSChris Mason 				unsigned long arg)
12248a4b83ccSChris Mason {
12258a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
12268a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
1227c071fcfdSChris Mason 	int ret = -ENOTTY;
12288a4b83ccSChris Mason 
1229e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1230e441d54dSChris Mason 		return -EPERM;
1231e441d54dSChris Mason 
1232dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
1233dae7b665SLi Zefan 	if (IS_ERR(vol))
1234dae7b665SLi Zefan 		return PTR_ERR(vol);
1235c071fcfdSChris Mason 
12368a4b83ccSChris Mason 	switch (cmd) {
12378a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
123897288f2cSChristoph Hellwig 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
12398a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
12408a4b83ccSChris Mason 		break;
12418a4b83ccSChris Mason 	}
1242dae7b665SLi Zefan 
12438a4b83ccSChris Mason 	kfree(vol);
1244f819d837SLinda Knippers 	return ret;
12458a4b83ccSChris Mason }
12468a4b83ccSChris Mason 
12470176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
1248ed0dab6bSYan {
1249ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
1250a74a4b97SChris Mason 	mutex_lock(&root->fs_info->transaction_kthread_mutex);
1251a74a4b97SChris Mason 	mutex_lock(&root->fs_info->cleaner_mutex);
12520176260fSLinus Torvalds 	return 0;
1253ed0dab6bSYan }
1254ed0dab6bSYan 
12550176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb)
1256ed0dab6bSYan {
1257ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
1258a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->cleaner_mutex);
1259a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->transaction_kthread_mutex);
12600176260fSLinus Torvalds 	return 0;
1261ed0dab6bSYan }
12622e635a27SChris Mason 
126322c44fe6SJosef Bacik static void btrfs_fs_dirty_inode(struct inode *inode, int flags)
126422c44fe6SJosef Bacik {
126522c44fe6SJosef Bacik 	int ret;
126622c44fe6SJosef Bacik 
126722c44fe6SJosef Bacik 	ret = btrfs_dirty_inode(inode);
126822c44fe6SJosef Bacik 	if (ret)
126922c44fe6SJosef Bacik 		printk_ratelimited(KERN_ERR "btrfs: fail to dirty inode %Lu "
127022c44fe6SJosef Bacik 				   "error %d\n", btrfs_ino(inode), ret);
127122c44fe6SJosef Bacik }
127222c44fe6SJosef Bacik 
1273b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
127476dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
1275bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
1276e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
1277d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
1278a9572a15SEric Paris 	.show_options	= btrfs_show_options,
12794730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
128022c44fe6SJosef Bacik 	.dirty_inode	= btrfs_fs_dirty_inode,
12812c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
12822c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
12838fd17795SChris Mason 	.statfs		= btrfs_statfs,
1284c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
12850176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
12860176260fSLinus Torvalds 	.unfreeze_fs	= btrfs_unfreeze,
1287e20d96d6SChris Mason };
1288a9218f6bSChris Mason 
1289a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
1290a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
1291a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
1292a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
12936038f373SArnd Bergmann 	.llseek = noop_llseek,
1294a9218f6bSChris Mason };
1295a9218f6bSChris Mason 
1296a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
1297578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
1298a9218f6bSChris Mason 	.name		= "btrfs-control",
1299a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
1300a9218f6bSChris Mason };
1301a9218f6bSChris Mason 
1302578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
1303578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
1304578454ffSKay Sievers 
1305a9218f6bSChris Mason static int btrfs_interface_init(void)
1306a9218f6bSChris Mason {
1307a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
1308a9218f6bSChris Mason }
1309a9218f6bSChris Mason 
1310b2950863SChristoph Hellwig static void btrfs_interface_exit(void)
1311a9218f6bSChris Mason {
1312a9218f6bSChris Mason 	if (misc_deregister(&btrfs_misc) < 0)
1313d397712bSChris Mason 		printk(KERN_INFO "misc_deregister failed for control device");
1314a9218f6bSChris Mason }
1315a9218f6bSChris Mason 
13162e635a27SChris Mason static int __init init_btrfs_fs(void)
13172e635a27SChris Mason {
13182c90e5d6SChris Mason 	int err;
131958176a96SJosef Bacik 
132058176a96SJosef Bacik 	err = btrfs_init_sysfs();
132158176a96SJosef Bacik 	if (err)
132258176a96SJosef Bacik 		return err;
132358176a96SJosef Bacik 
1324261507a0SLi Zefan 	err = btrfs_init_compress();
13252c90e5d6SChris Mason 	if (err)
1326a74a4b97SChris Mason 		goto free_sysfs;
1327d1310b2eSChris Mason 
1328261507a0SLi Zefan 	err = btrfs_init_cachep();
1329261507a0SLi Zefan 	if (err)
1330261507a0SLi Zefan 		goto free_compress;
1331261507a0SLi Zefan 
1332d1310b2eSChris Mason 	err = extent_io_init();
13332f4cbe64SWyatt Banks 	if (err)
13342f4cbe64SWyatt Banks 		goto free_cachep;
13352f4cbe64SWyatt Banks 
1336d1310b2eSChris Mason 	err = extent_map_init();
1337d1310b2eSChris Mason 	if (err)
1338d1310b2eSChris Mason 		goto free_extent_io;
1339d1310b2eSChris Mason 
134016cdcec7SMiao Xie 	err = btrfs_delayed_inode_init();
13412f4cbe64SWyatt Banks 	if (err)
13422f4cbe64SWyatt Banks 		goto free_extent_map;
1343c8b97818SChris Mason 
134416cdcec7SMiao Xie 	err = btrfs_interface_init();
134516cdcec7SMiao Xie 	if (err)
134616cdcec7SMiao Xie 		goto free_delayed_inode;
134716cdcec7SMiao Xie 
1348a9218f6bSChris Mason 	err = register_filesystem(&btrfs_fs_type);
1349a9218f6bSChris Mason 	if (err)
1350a9218f6bSChris Mason 		goto unregister_ioctl;
1351b3c3da71SChris Mason 
1352b3c3da71SChris Mason 	printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
13532f4cbe64SWyatt Banks 	return 0;
13542f4cbe64SWyatt Banks 
1355a9218f6bSChris Mason unregister_ioctl:
1356a9218f6bSChris Mason 	btrfs_interface_exit();
135716cdcec7SMiao Xie free_delayed_inode:
135816cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
13592f4cbe64SWyatt Banks free_extent_map:
13602f4cbe64SWyatt Banks 	extent_map_exit();
1361d1310b2eSChris Mason free_extent_io:
1362d1310b2eSChris Mason 	extent_io_exit();
13632f4cbe64SWyatt Banks free_cachep:
13642f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
1365261507a0SLi Zefan free_compress:
1366261507a0SLi Zefan 	btrfs_exit_compress();
1367a74a4b97SChris Mason free_sysfs:
13682f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
13692c90e5d6SChris Mason 	return err;
13702e635a27SChris Mason }
13712e635a27SChris Mason 
13722e635a27SChris Mason static void __exit exit_btrfs_fs(void)
13732e635a27SChris Mason {
137439279cc3SChris Mason 	btrfs_destroy_cachep();
137516cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
1376a52d9a80SChris Mason 	extent_map_exit();
1377d1310b2eSChris Mason 	extent_io_exit();
1378a9218f6bSChris Mason 	btrfs_interface_exit();
13792e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
138058176a96SJosef Bacik 	btrfs_exit_sysfs();
13818a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
1382261507a0SLi Zefan 	btrfs_exit_compress();
13832e635a27SChris Mason }
13842e635a27SChris Mason 
13852e635a27SChris Mason module_init(init_btrfs_fs)
13862e635a27SChris Mason module_exit(exit_btrfs_fs)
13872e635a27SChris Mason 
13882e635a27SChris Mason MODULE_LICENSE("GPL");
1389