xref: /linux/fs/btrfs/super.c (revision 5f524444c351e145a5f7e28253594688a421bfe8)
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>
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 {
150e20d96d6SChris Mason 	struct btrfs_root *root = btrfs_sb(sb);
151e20d96d6SChris Mason 	int ret;
152e20d96d6SChris Mason 
153e20d96d6SChris Mason 	ret = close_ctree(root);
154e20d96d6SChris Mason 	sb->s_fs_info = NULL;
155559af821SAndi Kleen 
156559af821SAndi Kleen 	(void)ret; /* FIXME: need to fix VFS to return error? */
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,
16773bc1876SJosef Bacik 	Opt_inode_cache, Opt_no_space_cache, 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"},
20073bc1876SJosef Bacik 	{Opt_no_space_cache, "no_space_cache"},
20133268eafSJosef Bacik 	{Opt_err, NULL},
20295e05289SChris Mason };
20395e05289SChris Mason 
204edf24abeSChristoph Hellwig /*
205edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
206edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
207edf24abeSChristoph Hellwig  */
208edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options)
20995e05289SChris Mason {
210edf24abeSChristoph Hellwig 	struct btrfs_fs_info *info = root->fs_info;
21195e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
21273bc1876SJosef Bacik 	char *p, *num, *orig = NULL;
21373bc1876SJosef Bacik 	u64 cache_gen;
2144543df7eSChris Mason 	int intarg;
215a7a3f7caSSage Weil 	int ret = 0;
216261507a0SLi Zefan 	char *compress_type;
217261507a0SLi Zefan 	bool compress_force = false;
218b6cda9bcSChris Mason 
21973bc1876SJosef Bacik 	cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
22073bc1876SJosef Bacik 	if (cache_gen)
22173bc1876SJosef Bacik 		btrfs_set_opt(info->mount_opt, SPACE_CACHE);
22273bc1876SJosef Bacik 
22395e05289SChris Mason 	if (!options)
22473bc1876SJosef Bacik 		goto out;
22595e05289SChris Mason 
226be20aa9dSChris Mason 	/*
227be20aa9dSChris Mason 	 * strsep changes the string, duplicate it because parse_options
228be20aa9dSChris Mason 	 * gets called twice
229be20aa9dSChris Mason 	 */
230be20aa9dSChris Mason 	options = kstrdup(options, GFP_NOFS);
231be20aa9dSChris Mason 	if (!options)
232be20aa9dSChris Mason 		return -ENOMEM;
233be20aa9dSChris Mason 
234da495eccSJosef Bacik 	orig = options;
235be20aa9dSChris Mason 
23695e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
23795e05289SChris Mason 		int token;
23895e05289SChris Mason 		if (!*p)
23995e05289SChris Mason 			continue;
24095e05289SChris Mason 
24195e05289SChris Mason 		token = match_token(p, tokens, args);
24295e05289SChris Mason 		switch (token) {
243dfe25020SChris Mason 		case Opt_degraded:
244edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: allowing degraded mounts\n");
245dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
246dfe25020SChris Mason 			break;
24795e05289SChris Mason 		case Opt_subvol:
24873f73415SJosef Bacik 		case Opt_subvolid:
249e15d0542SXin Zhong 		case Opt_subvolrootid:
25043e570b0SChristoph Hellwig 		case Opt_device:
251edf24abeSChristoph Hellwig 			/*
25243e570b0SChristoph Hellwig 			 * These are parsed by btrfs_parse_early_options
253edf24abeSChristoph Hellwig 			 * and can be happily ignored here.
254edf24abeSChristoph Hellwig 			 */
25595e05289SChris Mason 			break;
256b6cda9bcSChris Mason 		case Opt_nodatasum:
257067c28adSChris Mason 			printk(KERN_INFO "btrfs: setting nodatasum\n");
258b6cda9bcSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
259be20aa9dSChris Mason 			break;
260be20aa9dSChris Mason 		case Opt_nodatacow:
261edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: setting nodatacow\n");
262be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
263be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
264b6cda9bcSChris Mason 			break;
265a555f810SChris Mason 		case Opt_compress_force:
266261507a0SLi Zefan 		case Opt_compress_force_type:
267261507a0SLi Zefan 			compress_force = true;
268261507a0SLi Zefan 		case Opt_compress:
269261507a0SLi Zefan 		case Opt_compress_type:
270261507a0SLi Zefan 			if (token == Opt_compress ||
271261507a0SLi Zefan 			    token == Opt_compress_force ||
272261507a0SLi Zefan 			    strcmp(args[0].from, "zlib") == 0) {
273261507a0SLi Zefan 				compress_type = "zlib";
274261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
275a6fa6faeSLi Zefan 			} else if (strcmp(args[0].from, "lzo") == 0) {
276a6fa6faeSLi Zefan 				compress_type = "lzo";
277a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
278261507a0SLi Zefan 			} else {
279261507a0SLi Zefan 				ret = -EINVAL;
280261507a0SLi Zefan 				goto out;
281261507a0SLi Zefan 			}
282261507a0SLi Zefan 
283a555f810SChris Mason 			btrfs_set_opt(info->mount_opt, COMPRESS);
284261507a0SLi Zefan 			if (compress_force) {
285261507a0SLi Zefan 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
286261507a0SLi Zefan 				pr_info("btrfs: force %s compression\n",
287261507a0SLi Zefan 					compress_type);
288261507a0SLi Zefan 			} else
289261507a0SLi Zefan 				pr_info("btrfs: use %s compression\n",
290261507a0SLi Zefan 					compress_type);
291a555f810SChris Mason 			break;
292e18e4809SChris Mason 		case Opt_ssd:
293edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
294e18e4809SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
295e18e4809SChris Mason 			break;
296451d7585SChris Mason 		case Opt_ssd_spread:
297451d7585SChris Mason 			printk(KERN_INFO "btrfs: use spread ssd "
298451d7585SChris Mason 			       "allocation scheme\n");
299451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
300451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD_SPREAD);
301451d7585SChris Mason 			break;
3023b30c22fSChris Mason 		case Opt_nossd:
303451d7585SChris Mason 			printk(KERN_INFO "btrfs: not using ssd allocation "
304451d7585SChris Mason 			       "scheme\n");
305c289811cSChris Mason 			btrfs_set_opt(info->mount_opt, NOSSD);
3063b30c22fSChris Mason 			btrfs_clear_opt(info->mount_opt, SSD);
307451d7585SChris Mason 			btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
3083b30c22fSChris Mason 			break;
30921ad10cfSChris Mason 		case Opt_nobarrier:
310edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: turning off barriers\n");
31121ad10cfSChris Mason 			btrfs_set_opt(info->mount_opt, NOBARRIER);
31221ad10cfSChris Mason 			break;
3134543df7eSChris Mason 		case Opt_thread_pool:
3144543df7eSChris Mason 			intarg = 0;
3154543df7eSChris Mason 			match_int(&args[0], &intarg);
3164543df7eSChris Mason 			if (intarg) {
3174543df7eSChris Mason 				info->thread_pool_size = intarg;
3184543df7eSChris Mason 				printk(KERN_INFO "btrfs: thread pool %d\n",
3194543df7eSChris Mason 				       info->thread_pool_size);
3204543df7eSChris Mason 			}
3214543df7eSChris Mason 			break;
3226f568d35SChris Mason 		case Opt_max_inline:
323edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
3246f568d35SChris Mason 			if (num) {
32591748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
3266f568d35SChris Mason 				kfree(num);
3276f568d35SChris Mason 
32815ada040SChris Mason 				if (info->max_inline) {
3296f568d35SChris Mason 					info->max_inline = max_t(u64,
33015ada040SChris Mason 						info->max_inline,
33115ada040SChris Mason 						root->sectorsize);
33215ada040SChris Mason 				}
333edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: max_inline at %llu\n",
33421380931SJoel Becker 					(unsigned long long)info->max_inline);
3356f568d35SChris Mason 			}
3366f568d35SChris Mason 			break;
3378f662a76SChris Mason 		case Opt_alloc_start:
338edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
3398f662a76SChris Mason 			if (num) {
34091748467SAkinobu Mita 				info->alloc_start = memparse(num, NULL);
3418f662a76SChris Mason 				kfree(num);
342edf24abeSChristoph Hellwig 				printk(KERN_INFO
343edf24abeSChristoph Hellwig 					"btrfs: allocations start at %llu\n",
34421380931SJoel Becker 					(unsigned long long)info->alloc_start);
3458f662a76SChris Mason 			}
3468f662a76SChris Mason 			break;
34733268eafSJosef Bacik 		case Opt_noacl:
34833268eafSJosef Bacik 			root->fs_info->sb->s_flags &= ~MS_POSIXACL;
34933268eafSJosef Bacik 			break;
3503a5e1404SSage Weil 		case Opt_notreelog:
3513a5e1404SSage Weil 			printk(KERN_INFO "btrfs: disabling tree log\n");
3523a5e1404SSage Weil 			btrfs_set_opt(info->mount_opt, NOTREELOG);
3533a5e1404SSage Weil 			break;
354dccae999SSage Weil 		case Opt_flushoncommit:
355dccae999SSage Weil 			printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
356dccae999SSage Weil 			btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
357dccae999SSage Weil 			break;
35897e728d4SJosef Bacik 		case Opt_ratio:
35997e728d4SJosef Bacik 			intarg = 0;
36097e728d4SJosef Bacik 			match_int(&args[0], &intarg);
36197e728d4SJosef Bacik 			if (intarg) {
36297e728d4SJosef Bacik 				info->metadata_ratio = intarg;
36397e728d4SJosef Bacik 				printk(KERN_INFO "btrfs: metadata ratio %d\n",
36497e728d4SJosef Bacik 				       info->metadata_ratio);
36597e728d4SJosef Bacik 			}
36697e728d4SJosef Bacik 			break;
367e244a0aeSChristoph Hellwig 		case Opt_discard:
368e244a0aeSChristoph Hellwig 			btrfs_set_opt(info->mount_opt, DISCARD);
369e244a0aeSChristoph Hellwig 			break;
3700af3d00bSJosef Bacik 		case Opt_space_cache:
3710af3d00bSJosef Bacik 			btrfs_set_opt(info->mount_opt, SPACE_CACHE);
3720de90876SJosef Bacik 			break;
37373bc1876SJosef Bacik 		case Opt_no_space_cache:
37473bc1876SJosef Bacik 			printk(KERN_INFO "btrfs: disabling disk space caching\n");
37573bc1876SJosef Bacik 			btrfs_clear_opt(info->mount_opt, SPACE_CACHE);
37673bc1876SJosef Bacik 			break;
3774b9465cbSChris Mason 		case Opt_inode_cache:
3784b9465cbSChris Mason 			printk(KERN_INFO "btrfs: enabling inode map caching\n");
3794b9465cbSChris Mason 			btrfs_set_opt(info->mount_opt, INODE_MAP_CACHE);
3804b9465cbSChris Mason 			break;
38188c2ba3bSJosef Bacik 		case Opt_clear_cache:
38288c2ba3bSJosef Bacik 			printk(KERN_INFO "btrfs: force clearing of disk cache\n");
38388c2ba3bSJosef Bacik 			btrfs_set_opt(info->mount_opt, CLEAR_CACHE);
3840af3d00bSJosef Bacik 			break;
3854260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
3864260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
3874260f7c7SSage Weil 			break;
38891435650SChris Mason 		case Opt_enospc_debug:
38991435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
39091435650SChris Mason 			break;
3914cb5300bSChris Mason 		case Opt_defrag:
3924cb5300bSChris Mason 			printk(KERN_INFO "btrfs: enabling auto defrag");
3934cb5300bSChris Mason 			btrfs_set_opt(info->mount_opt, AUTO_DEFRAG);
3944cb5300bSChris Mason 			break;
395a7a3f7caSSage Weil 		case Opt_err:
396a7a3f7caSSage Weil 			printk(KERN_INFO "btrfs: unrecognized mount option "
397a7a3f7caSSage Weil 			       "'%s'\n", p);
398a7a3f7caSSage Weil 			ret = -EINVAL;
399a7a3f7caSSage Weil 			goto out;
40095e05289SChris Mason 		default:
401be20aa9dSChris Mason 			break;
40295e05289SChris Mason 		}
40395e05289SChris Mason 	}
404a7a3f7caSSage Weil out:
40573bc1876SJosef Bacik 	if (!ret && btrfs_test_opt(root, SPACE_CACHE))
40673bc1876SJosef Bacik 		printk(KERN_INFO "btrfs: disk space caching is enabled\n");
407da495eccSJosef Bacik 	kfree(orig);
408a7a3f7caSSage Weil 	return ret;
409edf24abeSChristoph Hellwig }
410edf24abeSChristoph Hellwig 
411edf24abeSChristoph Hellwig /*
412edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
413edf24abeSChristoph Hellwig  *
414edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
415edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
416edf24abeSChristoph Hellwig  */
41797288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags,
41873f73415SJosef Bacik 		void *holder, char **subvol_name, u64 *subvol_objectid,
419e15d0542SXin Zhong 		u64 *subvol_rootid, struct btrfs_fs_devices **fs_devices)
420edf24abeSChristoph Hellwig {
421edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
42283c8c9bdSJeff Liu 	char *device_name, *opts, *orig, *p;
423edf24abeSChristoph Hellwig 	int error = 0;
42473f73415SJosef Bacik 	int intarg;
425edf24abeSChristoph Hellwig 
426edf24abeSChristoph Hellwig 	if (!options)
427830c4adbSJosef Bacik 		return 0;
428edf24abeSChristoph Hellwig 
429edf24abeSChristoph Hellwig 	/*
430edf24abeSChristoph Hellwig 	 * strsep changes the string, duplicate it because parse_options
431edf24abeSChristoph Hellwig 	 * gets called twice
432edf24abeSChristoph Hellwig 	 */
433edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
434edf24abeSChristoph Hellwig 	if (!opts)
435edf24abeSChristoph Hellwig 		return -ENOMEM;
4363f3d0bc0STero Roponen 	orig = opts;
437edf24abeSChristoph Hellwig 
438edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
439edf24abeSChristoph Hellwig 		int token;
440edf24abeSChristoph Hellwig 		if (!*p)
441edf24abeSChristoph Hellwig 			continue;
442edf24abeSChristoph Hellwig 
443edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
444edf24abeSChristoph Hellwig 		switch (token) {
445edf24abeSChristoph Hellwig 		case Opt_subvol:
446edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
447edf24abeSChristoph Hellwig 			break;
44873f73415SJosef Bacik 		case Opt_subvolid:
44973f73415SJosef Bacik 			intarg = 0;
4504849f01dSJosef Bacik 			error = match_int(&args[0], &intarg);
4514849f01dSJosef Bacik 			if (!error) {
4524849f01dSJosef Bacik 				/* we want the original fs_tree */
4534849f01dSJosef Bacik 				if (!intarg)
4544849f01dSJosef Bacik 					*subvol_objectid =
4554849f01dSJosef Bacik 						BTRFS_FS_TREE_OBJECTID;
4564849f01dSJosef Bacik 				else
45773f73415SJosef Bacik 					*subvol_objectid = intarg;
4584849f01dSJosef Bacik 			}
45973f73415SJosef Bacik 			break;
460e15d0542SXin Zhong 		case Opt_subvolrootid:
461e15d0542SXin Zhong 			intarg = 0;
462e15d0542SXin Zhong 			error = match_int(&args[0], &intarg);
463e15d0542SXin Zhong 			if (!error) {
464e15d0542SXin Zhong 				/* we want the original fs_tree */
465e15d0542SXin Zhong 				if (!intarg)
466e15d0542SXin Zhong 					*subvol_rootid =
467e15d0542SXin Zhong 						BTRFS_FS_TREE_OBJECTID;
468e15d0542SXin Zhong 				else
469e15d0542SXin Zhong 					*subvol_rootid = intarg;
470e15d0542SXin Zhong 			}
471e15d0542SXin Zhong 			break;
47243e570b0SChristoph Hellwig 		case Opt_device:
47383c8c9bdSJeff Liu 			device_name = match_strdup(&args[0]);
47483c8c9bdSJeff Liu 			if (!device_name) {
47583c8c9bdSJeff Liu 				error = -ENOMEM;
47683c8c9bdSJeff Liu 				goto out;
47783c8c9bdSJeff Liu 			}
47883c8c9bdSJeff Liu 			error = btrfs_scan_one_device(device_name,
47943e570b0SChristoph Hellwig 					flags, holder, fs_devices);
48083c8c9bdSJeff Liu 			kfree(device_name);
48143e570b0SChristoph Hellwig 			if (error)
482830c4adbSJosef Bacik 				goto out;
48343e570b0SChristoph Hellwig 			break;
484edf24abeSChristoph Hellwig 		default:
485edf24abeSChristoph Hellwig 			break;
486edf24abeSChristoph Hellwig 		}
487edf24abeSChristoph Hellwig 	}
488edf24abeSChristoph Hellwig 
489edf24abeSChristoph Hellwig out:
490830c4adbSJosef Bacik 	kfree(orig);
491edf24abeSChristoph Hellwig 	return error;
49295e05289SChris Mason }
49395e05289SChris Mason 
49473f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb,
49573f73415SJosef Bacik 				       u64 subvol_objectid)
49673f73415SJosef Bacik {
49773f73415SJosef Bacik 	struct btrfs_root *root = sb->s_fs_info;
49873f73415SJosef Bacik 	struct btrfs_root *new_root;
49973f73415SJosef Bacik 	struct btrfs_dir_item *di;
50073f73415SJosef Bacik 	struct btrfs_path *path;
50173f73415SJosef Bacik 	struct btrfs_key location;
50273f73415SJosef Bacik 	struct inode *inode;
50373f73415SJosef Bacik 	u64 dir_id;
50473f73415SJosef Bacik 	int new = 0;
50573f73415SJosef Bacik 
50673f73415SJosef Bacik 	/*
50773f73415SJosef Bacik 	 * We have a specific subvol we want to mount, just setup location and
50873f73415SJosef Bacik 	 * go look up the root.
50973f73415SJosef Bacik 	 */
51073f73415SJosef Bacik 	if (subvol_objectid) {
51173f73415SJosef Bacik 		location.objectid = subvol_objectid;
51273f73415SJosef Bacik 		location.type = BTRFS_ROOT_ITEM_KEY;
51373f73415SJosef Bacik 		location.offset = (u64)-1;
51473f73415SJosef Bacik 		goto find_root;
51573f73415SJosef Bacik 	}
51673f73415SJosef Bacik 
51773f73415SJosef Bacik 	path = btrfs_alloc_path();
51873f73415SJosef Bacik 	if (!path)
51973f73415SJosef Bacik 		return ERR_PTR(-ENOMEM);
52073f73415SJosef Bacik 	path->leave_spinning = 1;
52173f73415SJosef Bacik 
52273f73415SJosef Bacik 	/*
52373f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
52473f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
52573f73415SJosef Bacik 	 * to mount.
52673f73415SJosef Bacik 	 */
52773f73415SJosef Bacik 	dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
52873f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
529b0839166SJulia Lawall 	if (IS_ERR(di)) {
530b0839166SJulia Lawall 		btrfs_free_path(path);
531fb4f6f91SDan Carpenter 		return ERR_CAST(di);
532b0839166SJulia Lawall 	}
53373f73415SJosef Bacik 	if (!di) {
53473f73415SJosef Bacik 		/*
53573f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
53673f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
53773f73415SJosef Bacik 		 * mount to root most subvolume.
53873f73415SJosef Bacik 		 */
53973f73415SJosef Bacik 		btrfs_free_path(path);
54073f73415SJosef Bacik 		dir_id = BTRFS_FIRST_FREE_OBJECTID;
54173f73415SJosef Bacik 		new_root = root->fs_info->fs_root;
54273f73415SJosef Bacik 		goto setup_root;
54373f73415SJosef Bacik 	}
54473f73415SJosef Bacik 
54573f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
54673f73415SJosef Bacik 	btrfs_free_path(path);
54773f73415SJosef Bacik 
54873f73415SJosef Bacik find_root:
54973f73415SJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
55073f73415SJosef Bacik 	if (IS_ERR(new_root))
551d0b678cbSJulia Lawall 		return ERR_CAST(new_root);
55273f73415SJosef Bacik 
55373f73415SJosef Bacik 	if (btrfs_root_refs(&new_root->root_item) == 0)
55473f73415SJosef Bacik 		return ERR_PTR(-ENOENT);
55573f73415SJosef Bacik 
55673f73415SJosef Bacik 	dir_id = btrfs_root_dirid(&new_root->root_item);
55773f73415SJosef Bacik setup_root:
55873f73415SJosef Bacik 	location.objectid = dir_id;
55973f73415SJosef Bacik 	location.type = BTRFS_INODE_ITEM_KEY;
56073f73415SJosef Bacik 	location.offset = 0;
56173f73415SJosef Bacik 
56273f73415SJosef Bacik 	inode = btrfs_iget(sb, &location, new_root, &new);
5634cbd1149SDan Carpenter 	if (IS_ERR(inode))
5644cbd1149SDan Carpenter 		return ERR_CAST(inode);
56573f73415SJosef Bacik 
56673f73415SJosef Bacik 	/*
56773f73415SJosef Bacik 	 * If we're just mounting the root most subvol put the inode and return
56873f73415SJosef Bacik 	 * a reference to the dentry.  We will have already gotten a reference
56973f73415SJosef Bacik 	 * to the inode in btrfs_fill_super so we're good to go.
57073f73415SJosef Bacik 	 */
57173f73415SJosef Bacik 	if (!new && sb->s_root->d_inode == inode) {
57273f73415SJosef Bacik 		iput(inode);
57373f73415SJosef Bacik 		return dget(sb->s_root);
57473f73415SJosef Bacik 	}
57573f73415SJosef Bacik 
576ba5b8958SJosef Bacik 	return d_obtain_alias(inode);
57773f73415SJosef Bacik }
57873f73415SJosef Bacik 
5798a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
5808a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
5818a4b83ccSChris Mason 			    void *data, int silent)
5822e635a27SChris Mason {
5832e635a27SChris Mason 	struct inode *inode;
584e20d96d6SChris Mason 	struct dentry *root_dentry;
5850f7d52f4SChris Mason 	struct btrfs_root *tree_root;
5865d4f98a2SYan Zheng 	struct btrfs_key key;
58739279cc3SChris Mason 	int err;
5882e635a27SChris Mason 
5892e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
5902e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
591e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
592af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
593be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
5945103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
5952e635a27SChris Mason 	sb->s_time_gran = 1;
5960eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
59733268eafSJosef Bacik 	sb->s_flags |= MS_POSIXACL;
59849cf6f45SChris Ball #endif
599e20d96d6SChris Mason 
600dfe25020SChris Mason 	tree_root = open_ctree(sb, fs_devices, (char *)data);
601d98237b3SChris Mason 
602e58ca020SYan 	if (IS_ERR(tree_root)) {
603e20d96d6SChris Mason 		printk("btrfs: open_ctree failed\n");
604e58ca020SYan 		return PTR_ERR(tree_root);
605e20d96d6SChris Mason 	}
6060f7d52f4SChris Mason 	sb->s_fs_info = tree_root;
607b888db2bSChris Mason 
6085d4f98a2SYan Zheng 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6095d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
6105d4f98a2SYan Zheng 	key.offset = 0;
61173f73415SJosef Bacik 	inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
6125d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
6135d4f98a2SYan Zheng 		err = PTR_ERR(inode);
61439279cc3SChris Mason 		goto fail_close;
61539279cc3SChris Mason 	}
6162e635a27SChris Mason 
617e20d96d6SChris Mason 	root_dentry = d_alloc_root(inode);
618e20d96d6SChris Mason 	if (!root_dentry) {
6192e635a27SChris Mason 		iput(inode);
62039279cc3SChris Mason 		err = -ENOMEM;
62139279cc3SChris Mason 		goto fail_close;
6222e635a27SChris Mason 	}
62358176a96SJosef Bacik 
624e20d96d6SChris Mason 	sb->s_root = root_dentry;
6256885f308SChris Mason 
6266885f308SChris Mason 	save_mount_options(sb, data);
62790a887c9SDan Magenheimer 	cleancache_init_fs(sb);
6282e635a27SChris Mason 	return 0;
6292e635a27SChris Mason 
63039279cc3SChris Mason fail_close:
63139279cc3SChris Mason 	close_ctree(tree_root);
632d5719762SChris Mason 	return err;
633d5719762SChris Mason }
634d5719762SChris Mason 
6356bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
636d5719762SChris Mason {
637d5719762SChris Mason 	struct btrfs_trans_handle *trans;
638dccae999SSage Weil 	struct btrfs_root *root = btrfs_sb(sb);
639d5719762SChris Mason 	int ret;
640df2ce34cSChris Mason 
6411abe9b8aSliubo 	trace_btrfs_sync_fs(wait);
6421abe9b8aSliubo 
643d561c025SChris Mason 	if (!wait) {
6447cfcc17eSChris Mason 		filemap_flush(root->fs_info->btree_inode->i_mapping);
645df2ce34cSChris Mason 		return 0;
646d561c025SChris Mason 	}
647771ed689SChris Mason 
64824bbcf04SYan, Zheng 	btrfs_start_delalloc_inodes(root, 0);
64924bbcf04SYan, Zheng 	btrfs_wait_ordered_extents(root, 0, 0);
650771ed689SChris Mason 
651a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
65298d5dc13STsutomu Itoh 	if (IS_ERR(trans))
65398d5dc13STsutomu Itoh 		return PTR_ERR(trans);
654d5719762SChris Mason 	ret = btrfs_commit_transaction(trans, root);
65554aa1f4dSChris Mason 	return ret;
656d5719762SChris Mason }
657d5719762SChris Mason 
658a9572a15SEric Paris static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
659a9572a15SEric Paris {
660a9572a15SEric Paris 	struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
661a9572a15SEric Paris 	struct btrfs_fs_info *info = root->fs_info;
662200da64eSTsutomu Itoh 	char *compress_type;
663a9572a15SEric Paris 
664a9572a15SEric Paris 	if (btrfs_test_opt(root, DEGRADED))
665a9572a15SEric Paris 		seq_puts(seq, ",degraded");
666a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATASUM))
667a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
668a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATACOW))
669a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
670a9572a15SEric Paris 	if (btrfs_test_opt(root, NOBARRIER))
671a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
672a9572a15SEric Paris 	if (info->max_inline != 8192 * 1024)
67321380931SJoel Becker 		seq_printf(seq, ",max_inline=%llu",
67421380931SJoel Becker 			   (unsigned long long)info->max_inline);
675a9572a15SEric Paris 	if (info->alloc_start != 0)
67621380931SJoel Becker 		seq_printf(seq, ",alloc_start=%llu",
67721380931SJoel Becker 			   (unsigned long long)info->alloc_start);
678a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
679a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
680a9572a15SEric Paris 		seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
681200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, COMPRESS)) {
682200da64eSTsutomu Itoh 		if (info->compress_type == BTRFS_COMPRESS_ZLIB)
683200da64eSTsutomu Itoh 			compress_type = "zlib";
684200da64eSTsutomu Itoh 		else
685200da64eSTsutomu Itoh 			compress_type = "lzo";
686200da64eSTsutomu Itoh 		if (btrfs_test_opt(root, FORCE_COMPRESS))
687200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
688200da64eSTsutomu Itoh 		else
689200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
690200da64eSTsutomu Itoh 	}
691c289811cSChris Mason 	if (btrfs_test_opt(root, NOSSD))
692c289811cSChris Mason 		seq_puts(seq, ",nossd");
693451d7585SChris Mason 	if (btrfs_test_opt(root, SSD_SPREAD))
694451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
695451d7585SChris Mason 	else if (btrfs_test_opt(root, SSD))
696a9572a15SEric Paris 		seq_puts(seq, ",ssd");
6973a5e1404SSage Weil 	if (btrfs_test_opt(root, NOTREELOG))
6986b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
699dccae999SSage Weil 	if (btrfs_test_opt(root, FLUSHONCOMMIT))
7006b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
70120a5239aSMatthew Wilcox 	if (btrfs_test_opt(root, DISCARD))
70220a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
703a9572a15SEric Paris 	if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
704a9572a15SEric Paris 		seq_puts(seq, ",noacl");
705200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, SPACE_CACHE))
706200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
70773bc1876SJosef Bacik 	else
70873bc1876SJosef Bacik 		seq_puts(seq, ",no_space_cache");
709200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, CLEAR_CACHE))
710200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
711200da64eSTsutomu Itoh 	if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
712200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
7130942caa3SDavid Sterba 	if (btrfs_test_opt(root, ENOSPC_DEBUG))
7140942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
7150942caa3SDavid Sterba 	if (btrfs_test_opt(root, AUTO_DEFRAG))
7160942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
7170942caa3SDavid Sterba 	if (btrfs_test_opt(root, INODE_MAP_CACHE))
7180942caa3SDavid Sterba 		seq_puts(seq, ",inode_cache");
719a9572a15SEric Paris 	return 0;
720a9572a15SEric Paris }
721a9572a15SEric Paris 
722a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
7232e635a27SChris Mason {
724450ba0eaSJosef Bacik 	struct btrfs_root *test_root = data;
725a061fc8dSChris Mason 	struct btrfs_root *root = btrfs_sb(s);
7264b82d6e4SYan 
727619c8c76SIan Kent 	/*
728619c8c76SIan Kent 	 * If this super block is going away, return false as it
729619c8c76SIan Kent 	 * can't match as an existing super block.
730619c8c76SIan Kent 	 */
731619c8c76SIan Kent 	if (!atomic_read(&s->s_active))
732619c8c76SIan Kent 		return 0;
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 {
738450ba0eaSJosef Bacik 	s->s_fs_info = data;
739450ba0eaSJosef Bacik 
740450ba0eaSJosef Bacik 	return set_anon_super(s, data);
741450ba0eaSJosef Bacik }
742450ba0eaSJosef Bacik 
743830c4adbSJosef Bacik /*
744830c4adbSJosef Bacik  * This will strip out the subvol=%s argument for an argument string and add
745830c4adbSJosef Bacik  * subvolid=0 to make sure we get the actual tree root for path walking to the
746830c4adbSJosef Bacik  * subvol we want.
747830c4adbSJosef Bacik  */
748830c4adbSJosef Bacik static char *setup_root_args(char *args)
749830c4adbSJosef Bacik {
750830c4adbSJosef Bacik 	unsigned copied = 0;
751830c4adbSJosef Bacik 	unsigned len = strlen(args) + 2;
752830c4adbSJosef Bacik 	char *pos;
753830c4adbSJosef Bacik 	char *ret;
754830c4adbSJosef Bacik 
755830c4adbSJosef Bacik 	/*
756830c4adbSJosef Bacik 	 * We need the same args as before, but minus
757830c4adbSJosef Bacik 	 *
758830c4adbSJosef Bacik 	 * subvol=a
759830c4adbSJosef Bacik 	 *
760830c4adbSJosef Bacik 	 * and add
761830c4adbSJosef Bacik 	 *
762830c4adbSJosef Bacik 	 * subvolid=0
763830c4adbSJosef Bacik 	 *
764830c4adbSJosef Bacik 	 * which is a difference of 2 characters, so we allocate strlen(args) +
765830c4adbSJosef Bacik 	 * 2 characters.
766830c4adbSJosef Bacik 	 */
767830c4adbSJosef Bacik 	ret = kzalloc(len * sizeof(char), GFP_NOFS);
768830c4adbSJosef Bacik 	if (!ret)
769830c4adbSJosef Bacik 		return NULL;
770830c4adbSJosef Bacik 	pos = strstr(args, "subvol=");
771830c4adbSJosef Bacik 
772830c4adbSJosef Bacik 	/* This shouldn't happen, but just in case.. */
773830c4adbSJosef Bacik 	if (!pos) {
774830c4adbSJosef Bacik 		kfree(ret);
775830c4adbSJosef Bacik 		return NULL;
776830c4adbSJosef Bacik 	}
777830c4adbSJosef Bacik 
778830c4adbSJosef Bacik 	/*
779830c4adbSJosef Bacik 	 * The subvol=<> arg is not at the front of the string, copy everybody
780830c4adbSJosef Bacik 	 * up to that into ret.
781830c4adbSJosef Bacik 	 */
782830c4adbSJosef Bacik 	if (pos != args) {
783830c4adbSJosef Bacik 		*pos = '\0';
784830c4adbSJosef Bacik 		strcpy(ret, args);
785830c4adbSJosef Bacik 		copied += strlen(args);
786830c4adbSJosef Bacik 		pos++;
787830c4adbSJosef Bacik 	}
788830c4adbSJosef Bacik 
789830c4adbSJosef Bacik 	strncpy(ret + copied, "subvolid=0", len - copied);
790830c4adbSJosef Bacik 
791830c4adbSJosef Bacik 	/* Length of subvolid=0 */
792830c4adbSJosef Bacik 	copied += 10;
793830c4adbSJosef Bacik 
794830c4adbSJosef Bacik 	/*
795830c4adbSJosef Bacik 	 * If there is no , after the subvol= option then we know there's no
796830c4adbSJosef Bacik 	 * other options and we can just return.
797830c4adbSJosef Bacik 	 */
798830c4adbSJosef Bacik 	pos = strchr(pos, ',');
799830c4adbSJosef Bacik 	if (!pos)
800830c4adbSJosef Bacik 		return ret;
801830c4adbSJosef Bacik 
802830c4adbSJosef Bacik 	/* Copy the rest of the arguments into our buffer */
803830c4adbSJosef Bacik 	strncpy(ret + copied, pos, len - copied);
804830c4adbSJosef Bacik 	copied += strlen(pos);
805830c4adbSJosef Bacik 
806830c4adbSJosef Bacik 	return ret;
807830c4adbSJosef Bacik }
808830c4adbSJosef Bacik 
809830c4adbSJosef Bacik static struct dentry *mount_subvol(const char *subvol_name, int flags,
810830c4adbSJosef Bacik 				   const char *device_name, char *data)
811830c4adbSJosef Bacik {
812830c4adbSJosef Bacik 	struct super_block *s;
813830c4adbSJosef Bacik 	struct dentry *root;
814830c4adbSJosef Bacik 	struct vfsmount *mnt;
815830c4adbSJosef Bacik 	struct mnt_namespace *ns_private;
816830c4adbSJosef Bacik 	char *newargs;
817830c4adbSJosef Bacik 	struct path path;
818830c4adbSJosef Bacik 	int error;
819830c4adbSJosef Bacik 
820830c4adbSJosef Bacik 	newargs = setup_root_args(data);
821830c4adbSJosef Bacik 	if (!newargs)
822830c4adbSJosef Bacik 		return ERR_PTR(-ENOMEM);
823830c4adbSJosef Bacik 	mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name,
824830c4adbSJosef Bacik 			     newargs);
825830c4adbSJosef Bacik 	kfree(newargs);
826830c4adbSJosef Bacik 	if (IS_ERR(mnt))
827830c4adbSJosef Bacik 		return ERR_CAST(mnt);
828830c4adbSJosef Bacik 
829830c4adbSJosef Bacik 	ns_private = create_mnt_ns(mnt);
830830c4adbSJosef Bacik 	if (IS_ERR(ns_private)) {
831830c4adbSJosef Bacik 		mntput(mnt);
832830c4adbSJosef Bacik 		return ERR_CAST(ns_private);
833830c4adbSJosef Bacik 	}
834830c4adbSJosef Bacik 
835830c4adbSJosef Bacik 	/*
836830c4adbSJosef Bacik 	 * This will trigger the automount of the subvol so we can just
837830c4adbSJosef Bacik 	 * drop the mnt we have here and return the dentry that we
838830c4adbSJosef Bacik 	 * found.
839830c4adbSJosef Bacik 	 */
840830c4adbSJosef Bacik 	error = vfs_path_lookup(mnt->mnt_root, mnt, subvol_name,
841830c4adbSJosef Bacik 				LOOKUP_FOLLOW, &path);
842830c4adbSJosef Bacik 	put_mnt_ns(ns_private);
843830c4adbSJosef Bacik 	if (error)
844830c4adbSJosef Bacik 		return ERR_PTR(error);
845830c4adbSJosef Bacik 
846830c4adbSJosef Bacik 	/* Get a ref to the sb and the dentry we found and return it */
847830c4adbSJosef Bacik 	s = path.mnt->mnt_sb;
848830c4adbSJosef Bacik 	atomic_inc(&s->s_active);
849830c4adbSJosef Bacik 	root = dget(path.dentry);
850830c4adbSJosef Bacik 	path_put(&path);
851830c4adbSJosef Bacik 	down_write(&s->s_umount);
852830c4adbSJosef Bacik 
853830c4adbSJosef Bacik 	return root;
854830c4adbSJosef Bacik }
855450ba0eaSJosef Bacik 
856edf24abeSChristoph Hellwig /*
857edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
858edf24abeSChristoph Hellwig  *
859edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
860edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
861edf24abeSChristoph Hellwig  */
862061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
863306e16ceSDavid Sterba 		const char *device_name, void *data)
8644b82d6e4SYan {
8654b82d6e4SYan 	struct block_device *bdev = NULL;
8664b82d6e4SYan 	struct super_block *s;
8674b82d6e4SYan 	struct dentry *root;
8688a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
869450ba0eaSJosef Bacik 	struct btrfs_root *tree_root = NULL;
870450ba0eaSJosef Bacik 	struct btrfs_fs_info *fs_info = NULL;
87197288f2cSChristoph Hellwig 	fmode_t mode = FMODE_READ;
87273f73415SJosef Bacik 	char *subvol_name = NULL;
87373f73415SJosef Bacik 	u64 subvol_objectid = 0;
874e15d0542SXin Zhong 	u64 subvol_rootid = 0;
8754b82d6e4SYan 	int error = 0;
8764b82d6e4SYan 
87797288f2cSChristoph Hellwig 	if (!(flags & MS_RDONLY))
87897288f2cSChristoph Hellwig 		mode |= FMODE_WRITE;
87997288f2cSChristoph Hellwig 
88097288f2cSChristoph Hellwig 	error = btrfs_parse_early_options(data, mode, fs_type,
88173f73415SJosef Bacik 					  &subvol_name, &subvol_objectid,
882e15d0542SXin Zhong 					  &subvol_rootid, &fs_devices);
883edf24abeSChristoph Hellwig 	if (error)
884061dbc6bSAl Viro 		return ERR_PTR(error);
885edf24abeSChristoph Hellwig 
886830c4adbSJosef Bacik 	if (subvol_name) {
887830c4adbSJosef Bacik 		root = mount_subvol(subvol_name, flags, device_name, data);
888830c4adbSJosef Bacik 		kfree(subvol_name);
889830c4adbSJosef Bacik 		return root;
890830c4adbSJosef Bacik 	}
891830c4adbSJosef Bacik 
892306e16ceSDavid Sterba 	error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
8938a4b83ccSChris Mason 	if (error)
894830c4adbSJosef Bacik 		return ERR_PTR(error);
8954b82d6e4SYan 
89697288f2cSChristoph Hellwig 	error = btrfs_open_devices(fs_devices, mode, fs_type);
8978a4b83ccSChris Mason 	if (error)
898830c4adbSJosef Bacik 		return ERR_PTR(error);
8998a4b83ccSChris Mason 
9002b82032cSYan Zheng 	if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
9012b82032cSYan Zheng 		error = -EACCES;
9022b82032cSYan Zheng 		goto error_close_devices;
9032b82032cSYan Zheng 	}
9042b82032cSYan Zheng 
905450ba0eaSJosef Bacik 	/*
906450ba0eaSJosef Bacik 	 * Setup a dummy root and fs_info for test/set super.  This is because
907450ba0eaSJosef Bacik 	 * we don't actually fill this stuff out until open_ctree, but we need
908450ba0eaSJosef Bacik 	 * it for searching for existing supers, so this lets us do that and
909450ba0eaSJosef Bacik 	 * then open_ctree will properly initialize everything later.
910450ba0eaSJosef Bacik 	 */
911450ba0eaSJosef Bacik 	fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
912450ba0eaSJosef Bacik 	tree_root = kzalloc(sizeof(struct btrfs_root), GFP_NOFS);
913450ba0eaSJosef Bacik 	if (!fs_info || !tree_root) {
914450ba0eaSJosef Bacik 		error = -ENOMEM;
915450ba0eaSJosef Bacik 		goto error_close_devices;
916450ba0eaSJosef Bacik 	}
917450ba0eaSJosef Bacik 	fs_info->tree_root = tree_root;
918450ba0eaSJosef Bacik 	fs_info->fs_devices = fs_devices;
919450ba0eaSJosef Bacik 	tree_root->fs_info = fs_info;
920450ba0eaSJosef Bacik 
921dfe25020SChris Mason 	bdev = fs_devices->latest_bdev;
922450ba0eaSJosef Bacik 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, tree_root);
923830c4adbSJosef Bacik 	if (IS_ERR(s)) {
924830c4adbSJosef Bacik 		error = PTR_ERR(s);
925830c4adbSJosef Bacik 		goto error_close_devices;
926830c4adbSJosef Bacik 	}
9274b82d6e4SYan 
9284b82d6e4SYan 	if (s->s_root) {
9294b82d6e4SYan 		if ((flags ^ s->s_flags) & MS_RDONLY) {
9306f5bbff9SAl Viro 			deactivate_locked_super(s);
931830c4adbSJosef Bacik 			return ERR_PTR(-EBUSY);
9324b82d6e4SYan 		}
9334b82d6e4SYan 
9342b82032cSYan Zheng 		btrfs_close_devices(fs_devices);
935bdc924bbSIan Kent 		kfree(fs_info);
936bdc924bbSIan Kent 		kfree(tree_root);
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));
942*5f524444SIlya 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 		if (error) {
9466f5bbff9SAl Viro 			deactivate_locked_super(s);
947830c4adbSJosef Bacik 			return ERR_PTR(error);
9484b82d6e4SYan 		}
9494b82d6e4SYan 
9504b82d6e4SYan 		s->s_flags |= MS_ACTIVE;
9514b82d6e4SYan 	}
9524b82d6e4SYan 
953e15d0542SXin Zhong 	root = get_default_root(s, subvol_objectid);
954e15d0542SXin Zhong 	if (IS_ERR(root)) {
955e15d0542SXin Zhong 		deactivate_locked_super(s);
956830c4adbSJosef Bacik 		return root;
95776fcef19SDavid Woodhouse 	}
9584b82d6e4SYan 
959061dbc6bSAl Viro 	return root;
9604b82d6e4SYan 
961c146afadSYan Zheng error_close_devices:
9628a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
963450ba0eaSJosef Bacik 	kfree(fs_info);
964450ba0eaSJosef Bacik 	kfree(tree_root);
965061dbc6bSAl Viro 	return ERR_PTR(error);
9664b82d6e4SYan }
9672e635a27SChris Mason 
968c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
969c146afadSYan Zheng {
970c146afadSYan Zheng 	struct btrfs_root *root = btrfs_sb(sb);
971c146afadSYan Zheng 	int ret;
972c146afadSYan Zheng 
973b288052eSChris Mason 	ret = btrfs_parse_options(root, data);
974b288052eSChris Mason 	if (ret)
975b288052eSChris Mason 		return -EINVAL;
976b288052eSChris Mason 
977c146afadSYan Zheng 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
978c146afadSYan Zheng 		return 0;
979c146afadSYan Zheng 
980c146afadSYan Zheng 	if (*flags & MS_RDONLY) {
981c146afadSYan Zheng 		sb->s_flags |= MS_RDONLY;
982c146afadSYan Zheng 
983c146afadSYan Zheng 		ret =  btrfs_commit_super(root);
984c146afadSYan Zheng 		WARN_ON(ret);
985c146afadSYan Zheng 	} else {
9862b82032cSYan Zheng 		if (root->fs_info->fs_devices->rw_devices == 0)
9872b82032cSYan Zheng 			return -EACCES;
9882b82032cSYan Zheng 
989c146afadSYan Zheng 		if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
990c146afadSYan Zheng 			return -EINVAL;
991c146afadSYan Zheng 
992d68fc57bSYan, Zheng 		ret = btrfs_cleanup_fs_roots(root->fs_info);
993c146afadSYan Zheng 		WARN_ON(ret);
994c146afadSYan Zheng 
995d68fc57bSYan, Zheng 		/* recover relocation */
996d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
997c146afadSYan Zheng 		WARN_ON(ret);
998c146afadSYan Zheng 
999c146afadSYan Zheng 		sb->s_flags &= ~MS_RDONLY;
1000c146afadSYan Zheng 	}
1001c146afadSYan Zheng 
1002c146afadSYan Zheng 	return 0;
1003c146afadSYan Zheng }
1004c146afadSYan Zheng 
1005bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
1006bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1,
1007bcd53741SArne Jansen 				       const void *dev_info2)
1008bcd53741SArne Jansen {
1009bcd53741SArne Jansen 	if (((struct btrfs_device_info *)dev_info1)->max_avail >
1010bcd53741SArne Jansen 	    ((struct btrfs_device_info *)dev_info2)->max_avail)
1011bcd53741SArne Jansen 		return -1;
1012bcd53741SArne Jansen 	else if (((struct btrfs_device_info *)dev_info1)->max_avail <
1013bcd53741SArne Jansen 		 ((struct btrfs_device_info *)dev_info2)->max_avail)
1014bcd53741SArne Jansen 		return 1;
1015bcd53741SArne Jansen 	else
1016bcd53741SArne Jansen 	return 0;
1017bcd53741SArne Jansen }
1018bcd53741SArne Jansen 
1019bcd53741SArne Jansen /*
1020bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
1021bcd53741SArne Jansen  * is stored.(Descending Sort)
1022bcd53741SArne Jansen  */
1023bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
1024bcd53741SArne Jansen 					struct btrfs_device_info *devices,
1025bcd53741SArne Jansen 					size_t nr_devices)
1026bcd53741SArne Jansen {
1027bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1028bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
1029bcd53741SArne Jansen }
1030bcd53741SArne Jansen 
10316d07bcecSMiao Xie /*
10326d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
10336d07bcecSMiao Xie  * file data.
10346d07bcecSMiao Xie  */
10356d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
10366d07bcecSMiao Xie {
10376d07bcecSMiao Xie 	struct btrfs_fs_info *fs_info = root->fs_info;
10386d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
10396d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
10406d07bcecSMiao Xie 	struct btrfs_device *device;
10416d07bcecSMiao Xie 	u64 skip_space;
10426d07bcecSMiao Xie 	u64 type;
10436d07bcecSMiao Xie 	u64 avail_space;
10446d07bcecSMiao Xie 	u64 used_space;
10456d07bcecSMiao Xie 	u64 min_stripe_size;
10466d07bcecSMiao Xie 	int min_stripes = 1;
10476d07bcecSMiao Xie 	int i = 0, nr_devices;
10486d07bcecSMiao Xie 	int ret;
10496d07bcecSMiao Xie 
10506d07bcecSMiao Xie 	nr_devices = fs_info->fs_devices->rw_devices;
10516d07bcecSMiao Xie 	BUG_ON(!nr_devices);
10526d07bcecSMiao Xie 
10536d07bcecSMiao Xie 	devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
10546d07bcecSMiao Xie 			       GFP_NOFS);
10556d07bcecSMiao Xie 	if (!devices_info)
10566d07bcecSMiao Xie 		return -ENOMEM;
10576d07bcecSMiao Xie 
10586d07bcecSMiao Xie 	/* calc min stripe number for data space alloction */
10596d07bcecSMiao Xie 	type = btrfs_get_alloc_profile(root, 1);
10606d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_RAID0)
10616d07bcecSMiao Xie 		min_stripes = 2;
10626d07bcecSMiao Xie 	else if (type & BTRFS_BLOCK_GROUP_RAID1)
10636d07bcecSMiao Xie 		min_stripes = 2;
10646d07bcecSMiao Xie 	else if (type & BTRFS_BLOCK_GROUP_RAID10)
10656d07bcecSMiao Xie 		min_stripes = 4;
10666d07bcecSMiao Xie 
10676d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_DUP)
10686d07bcecSMiao Xie 		min_stripe_size = 2 * BTRFS_STRIPE_LEN;
10696d07bcecSMiao Xie 	else
10706d07bcecSMiao Xie 		min_stripe_size = BTRFS_STRIPE_LEN;
10716d07bcecSMiao Xie 
10726d07bcecSMiao Xie 	list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
10736d07bcecSMiao Xie 		if (!device->in_fs_metadata)
10746d07bcecSMiao Xie 			continue;
10756d07bcecSMiao Xie 
10766d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
10776d07bcecSMiao Xie 
10786d07bcecSMiao Xie 		/* align with stripe_len */
10796d07bcecSMiao Xie 		do_div(avail_space, BTRFS_STRIPE_LEN);
10806d07bcecSMiao Xie 		avail_space *= BTRFS_STRIPE_LEN;
10816d07bcecSMiao Xie 
10826d07bcecSMiao Xie 		/*
10836d07bcecSMiao Xie 		 * In order to avoid overwritting the superblock on the drive,
10846d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
10856d07bcecSMiao Xie 		 * allocation.
10866d07bcecSMiao Xie 		 */
10876d07bcecSMiao Xie 		skip_space = 1024 * 1024;
10886d07bcecSMiao Xie 
10896d07bcecSMiao Xie 		/* user can set the offset in fs_info->alloc_start. */
10906d07bcecSMiao Xie 		if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
10916d07bcecSMiao Xie 		    device->total_bytes)
10926d07bcecSMiao Xie 			skip_space = max(fs_info->alloc_start, skip_space);
10936d07bcecSMiao Xie 
10946d07bcecSMiao Xie 		/*
10956d07bcecSMiao Xie 		 * btrfs can not use the free space in [0, skip_space - 1],
10966d07bcecSMiao Xie 		 * we must subtract it from the total. In order to implement
10976d07bcecSMiao Xie 		 * it, we account the used space in this range first.
10986d07bcecSMiao Xie 		 */
10996d07bcecSMiao Xie 		ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
11006d07bcecSMiao Xie 						     &used_space);
11016d07bcecSMiao Xie 		if (ret) {
11026d07bcecSMiao Xie 			kfree(devices_info);
11036d07bcecSMiao Xie 			return ret;
11046d07bcecSMiao Xie 		}
11056d07bcecSMiao Xie 
11066d07bcecSMiao Xie 		/* calc the free space in [0, skip_space - 1] */
11076d07bcecSMiao Xie 		skip_space -= used_space;
11086d07bcecSMiao Xie 
11096d07bcecSMiao Xie 		/*
11106d07bcecSMiao Xie 		 * we can use the free space in [0, skip_space - 1], subtract
11116d07bcecSMiao Xie 		 * it from the total.
11126d07bcecSMiao Xie 		 */
11136d07bcecSMiao Xie 		if (avail_space && avail_space >= skip_space)
11146d07bcecSMiao Xie 			avail_space -= skip_space;
11156d07bcecSMiao Xie 		else
11166d07bcecSMiao Xie 			avail_space = 0;
11176d07bcecSMiao Xie 
11186d07bcecSMiao Xie 		if (avail_space < min_stripe_size)
11196d07bcecSMiao Xie 			continue;
11206d07bcecSMiao Xie 
11216d07bcecSMiao Xie 		devices_info[i].dev = device;
11226d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
11236d07bcecSMiao Xie 
11246d07bcecSMiao Xie 		i++;
11256d07bcecSMiao Xie 	}
11266d07bcecSMiao Xie 
11276d07bcecSMiao Xie 	nr_devices = i;
11286d07bcecSMiao Xie 
11296d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
11306d07bcecSMiao Xie 
11316d07bcecSMiao Xie 	i = nr_devices - 1;
11326d07bcecSMiao Xie 	avail_space = 0;
11336d07bcecSMiao Xie 	while (nr_devices >= min_stripes) {
11346d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
11356d07bcecSMiao Xie 			int j;
11366d07bcecSMiao Xie 			u64 alloc_size;
11376d07bcecSMiao Xie 
11386d07bcecSMiao Xie 			avail_space += devices_info[i].max_avail * min_stripes;
11396d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
11406d07bcecSMiao Xie 			for (j = i + 1 - min_stripes; j <= i; j++)
11416d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
11426d07bcecSMiao Xie 		}
11436d07bcecSMiao Xie 		i--;
11446d07bcecSMiao Xie 		nr_devices--;
11456d07bcecSMiao Xie 	}
11466d07bcecSMiao Xie 
11476d07bcecSMiao Xie 	kfree(devices_info);
11486d07bcecSMiao Xie 	*free_bytes = avail_space;
11496d07bcecSMiao Xie 	return 0;
11506d07bcecSMiao Xie }
11516d07bcecSMiao Xie 
11528fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
11538fd17795SChris Mason {
11548fd17795SChris Mason 	struct btrfs_root *root = btrfs_sb(dentry->d_sb);
11554b52dff6SChris Mason 	struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1156bd4d1088SJosef Bacik 	struct list_head *head = &root->fs_info->space_info;
1157bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
1158bd4d1088SJosef Bacik 	u64 total_used = 0;
11596d07bcecSMiao Xie 	u64 total_free_data = 0;
1160db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
11619d03632eSDavid Woodhouse 	__be32 *fsid = (__be32 *)root->fs_info->fsid;
11626d07bcecSMiao Xie 	int ret;
11638fd17795SChris Mason 
11646d07bcecSMiao Xie 	/* holding chunk_muext to avoid allocating new chunks */
11656d07bcecSMiao Xie 	mutex_lock(&root->fs_info->chunk_mutex);
1166bd4d1088SJosef Bacik 	rcu_read_lock();
116789a55897SJosef Bacik 	list_for_each_entry_rcu(found, head, list) {
11686d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
11696d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
11706d07bcecSMiao Xie 			total_free_data -=
11716d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
11726d07bcecSMiao Xie 		}
11736d07bcecSMiao Xie 
1174b742bb82SYan, Zheng 		total_used += found->disk_used;
117589a55897SJosef Bacik 	}
1176bd4d1088SJosef Bacik 	rcu_read_unlock();
1177bd4d1088SJosef Bacik 
11788fd17795SChris Mason 	buf->f_namelen = BTRFS_NAME_LEN;
1179db94535dSChris Mason 	buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
1180bd4d1088SJosef Bacik 	buf->f_bfree = buf->f_blocks - (total_used >> bits);
11818fd17795SChris Mason 	buf->f_bsize = dentry->d_sb->s_blocksize;
11828fd17795SChris Mason 	buf->f_type = BTRFS_SUPER_MAGIC;
11836d07bcecSMiao Xie 	buf->f_bavail = total_free_data;
11846d07bcecSMiao Xie 	ret = btrfs_calc_avail_data_space(root, &total_free_data);
11856d07bcecSMiao Xie 	if (ret) {
11866d07bcecSMiao Xie 		mutex_unlock(&root->fs_info->chunk_mutex);
11876d07bcecSMiao Xie 		return ret;
11886d07bcecSMiao Xie 	}
11896d07bcecSMiao Xie 	buf->f_bavail += total_free_data;
11906d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
11916d07bcecSMiao Xie 	mutex_unlock(&root->fs_info->chunk_mutex);
1192d397712bSChris Mason 
11939d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
11949d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
11959d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
11969d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
11979d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
119832d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
119932d48fa1SDavid Woodhouse 	buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
120032d48fa1SDavid Woodhouse 	buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
120132d48fa1SDavid Woodhouse 
12028fd17795SChris Mason 	return 0;
12038fd17795SChris Mason }
1204b5133862SChris Mason 
12052e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
12062e635a27SChris Mason 	.owner		= THIS_MODULE,
12072e635a27SChris Mason 	.name		= "btrfs",
1208061dbc6bSAl Viro 	.mount		= btrfs_mount,
1209a061fc8dSChris Mason 	.kill_sb	= kill_anon_super,
12102e635a27SChris Mason 	.fs_flags	= FS_REQUIRES_DEV,
12112e635a27SChris Mason };
1212a9218f6bSChris Mason 
1213d352ac68SChris Mason /*
1214d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
1215d352ac68SChris Mason  */
12168a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
12178a4b83ccSChris Mason 				unsigned long arg)
12188a4b83ccSChris Mason {
12198a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
12208a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
1221c071fcfdSChris Mason 	int ret = -ENOTTY;
12228a4b83ccSChris Mason 
1223e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1224e441d54dSChris Mason 		return -EPERM;
1225e441d54dSChris Mason 
1226dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
1227dae7b665SLi Zefan 	if (IS_ERR(vol))
1228dae7b665SLi Zefan 		return PTR_ERR(vol);
1229c071fcfdSChris Mason 
12308a4b83ccSChris Mason 	switch (cmd) {
12318a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
123297288f2cSChristoph Hellwig 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
12338a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
12348a4b83ccSChris Mason 		break;
12358a4b83ccSChris Mason 	}
1236dae7b665SLi Zefan 
12378a4b83ccSChris Mason 	kfree(vol);
1238f819d837SLinda Knippers 	return ret;
12398a4b83ccSChris Mason }
12408a4b83ccSChris Mason 
12410176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
1242ed0dab6bSYan {
1243ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
1244a74a4b97SChris Mason 	mutex_lock(&root->fs_info->transaction_kthread_mutex);
1245a74a4b97SChris Mason 	mutex_lock(&root->fs_info->cleaner_mutex);
12460176260fSLinus Torvalds 	return 0;
1247ed0dab6bSYan }
1248ed0dab6bSYan 
12490176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb)
1250ed0dab6bSYan {
1251ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
1252a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->cleaner_mutex);
1253a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->transaction_kthread_mutex);
12540176260fSLinus Torvalds 	return 0;
1255ed0dab6bSYan }
12562e635a27SChris Mason 
1257b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
125876dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
1259bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
1260e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
1261d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
1262a9572a15SEric Paris 	.show_options	= btrfs_show_options,
12634730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
1264b5133862SChris Mason 	.dirty_inode	= btrfs_dirty_inode,
12652c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
12662c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
12678fd17795SChris Mason 	.statfs		= btrfs_statfs,
1268c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
12690176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
12700176260fSLinus Torvalds 	.unfreeze_fs	= btrfs_unfreeze,
1271e20d96d6SChris Mason };
1272a9218f6bSChris Mason 
1273a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
1274a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
1275a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
1276a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
12776038f373SArnd Bergmann 	.llseek = noop_llseek,
1278a9218f6bSChris Mason };
1279a9218f6bSChris Mason 
1280a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
1281578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
1282a9218f6bSChris Mason 	.name		= "btrfs-control",
1283a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
1284a9218f6bSChris Mason };
1285a9218f6bSChris Mason 
1286578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
1287578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
1288578454ffSKay Sievers 
1289a9218f6bSChris Mason static int btrfs_interface_init(void)
1290a9218f6bSChris Mason {
1291a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
1292a9218f6bSChris Mason }
1293a9218f6bSChris Mason 
1294b2950863SChristoph Hellwig static void btrfs_interface_exit(void)
1295a9218f6bSChris Mason {
1296a9218f6bSChris Mason 	if (misc_deregister(&btrfs_misc) < 0)
1297d397712bSChris Mason 		printk(KERN_INFO "misc_deregister failed for control device");
1298a9218f6bSChris Mason }
1299a9218f6bSChris Mason 
13002e635a27SChris Mason static int __init init_btrfs_fs(void)
13012e635a27SChris Mason {
13022c90e5d6SChris Mason 	int err;
130358176a96SJosef Bacik 
130458176a96SJosef Bacik 	err = btrfs_init_sysfs();
130558176a96SJosef Bacik 	if (err)
130658176a96SJosef Bacik 		return err;
130758176a96SJosef Bacik 
1308261507a0SLi Zefan 	err = btrfs_init_compress();
13092c90e5d6SChris Mason 	if (err)
1310a74a4b97SChris Mason 		goto free_sysfs;
1311d1310b2eSChris Mason 
1312261507a0SLi Zefan 	err = btrfs_init_cachep();
1313261507a0SLi Zefan 	if (err)
1314261507a0SLi Zefan 		goto free_compress;
1315261507a0SLi Zefan 
1316d1310b2eSChris Mason 	err = extent_io_init();
13172f4cbe64SWyatt Banks 	if (err)
13182f4cbe64SWyatt Banks 		goto free_cachep;
13192f4cbe64SWyatt Banks 
1320d1310b2eSChris Mason 	err = extent_map_init();
1321d1310b2eSChris Mason 	if (err)
1322d1310b2eSChris Mason 		goto free_extent_io;
1323d1310b2eSChris Mason 
132416cdcec7SMiao Xie 	err = btrfs_delayed_inode_init();
13252f4cbe64SWyatt Banks 	if (err)
13262f4cbe64SWyatt Banks 		goto free_extent_map;
1327c8b97818SChris Mason 
132816cdcec7SMiao Xie 	err = btrfs_interface_init();
132916cdcec7SMiao Xie 	if (err)
133016cdcec7SMiao Xie 		goto free_delayed_inode;
133116cdcec7SMiao Xie 
1332a9218f6bSChris Mason 	err = register_filesystem(&btrfs_fs_type);
1333a9218f6bSChris Mason 	if (err)
1334a9218f6bSChris Mason 		goto unregister_ioctl;
1335b3c3da71SChris Mason 
1336b3c3da71SChris Mason 	printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
13372f4cbe64SWyatt Banks 	return 0;
13382f4cbe64SWyatt Banks 
1339a9218f6bSChris Mason unregister_ioctl:
1340a9218f6bSChris Mason 	btrfs_interface_exit();
134116cdcec7SMiao Xie free_delayed_inode:
134216cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
13432f4cbe64SWyatt Banks free_extent_map:
13442f4cbe64SWyatt Banks 	extent_map_exit();
1345d1310b2eSChris Mason free_extent_io:
1346d1310b2eSChris Mason 	extent_io_exit();
13472f4cbe64SWyatt Banks free_cachep:
13482f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
1349261507a0SLi Zefan free_compress:
1350261507a0SLi Zefan 	btrfs_exit_compress();
1351a74a4b97SChris Mason free_sysfs:
13522f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
13532c90e5d6SChris Mason 	return err;
13542e635a27SChris Mason }
13552e635a27SChris Mason 
13562e635a27SChris Mason static void __exit exit_btrfs_fs(void)
13572e635a27SChris Mason {
135839279cc3SChris Mason 	btrfs_destroy_cachep();
135916cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
1360a52d9a80SChris Mason 	extent_map_exit();
1361d1310b2eSChris Mason 	extent_io_exit();
1362a9218f6bSChris Mason 	btrfs_interface_exit();
13632e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
136458176a96SJosef Bacik 	btrfs_exit_sysfs();
13658a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
1366261507a0SLi Zefan 	btrfs_exit_compress();
13672e635a27SChris Mason }
13682e635a27SChris Mason 
13692e635a27SChris Mason module_init(init_btrfs_fs)
13702e635a27SChris Mason module_exit(exit_btrfs_fs)
13712e635a27SChris Mason 
13722e635a27SChris Mason MODULE_LICENSE("GPL");
1373