xref: /linux/fs/btrfs/super.c (revision 39fb26c398ddf8d7794a85e896cfe1a42e55524b)
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,
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;
5910f7d52f4SChris Mason 	struct btrfs_root *tree_root;
5925d4f98a2SYan Zheng 	struct btrfs_key key;
59339279cc3SChris Mason 	int err;
5942e635a27SChris Mason 
5952e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
5962e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
597e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
598af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
599be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
6005103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
6012e635a27SChris Mason 	sb->s_time_gran = 1;
6020eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
60333268eafSJosef Bacik 	sb->s_flags |= MS_POSIXACL;
60449cf6f45SChris Ball #endif
605e20d96d6SChris Mason 
606dfe25020SChris Mason 	tree_root = open_ctree(sb, fs_devices, (char *)data);
607d98237b3SChris Mason 
608e58ca020SYan 	if (IS_ERR(tree_root)) {
609e20d96d6SChris Mason 		printk("btrfs: open_ctree failed\n");
610e58ca020SYan 		return PTR_ERR(tree_root);
611e20d96d6SChris Mason 	}
6120f7d52f4SChris Mason 	sb->s_fs_info = tree_root;
613b888db2bSChris Mason 
6145d4f98a2SYan Zheng 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6155d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
6165d4f98a2SYan Zheng 	key.offset = 0;
61773f73415SJosef Bacik 	inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
6185d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
6195d4f98a2SYan Zheng 		err = PTR_ERR(inode);
62039279cc3SChris Mason 		goto fail_close;
62139279cc3SChris Mason 	}
6222e635a27SChris Mason 
623e20d96d6SChris Mason 	root_dentry = d_alloc_root(inode);
624e20d96d6SChris Mason 	if (!root_dentry) {
6252e635a27SChris Mason 		iput(inode);
62639279cc3SChris Mason 		err = -ENOMEM;
62739279cc3SChris Mason 		goto fail_close;
6282e635a27SChris Mason 	}
62958176a96SJosef Bacik 
630e20d96d6SChris Mason 	sb->s_root = root_dentry;
6316885f308SChris Mason 
6326885f308SChris Mason 	save_mount_options(sb, data);
63390a887c9SDan Magenheimer 	cleancache_init_fs(sb);
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 
664a9572a15SEric Paris static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
665a9572a15SEric Paris {
666a9572a15SEric Paris 	struct btrfs_root *root = btrfs_sb(vfs->mnt_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 
733619c8c76SIan Kent 	/*
734619c8c76SIan Kent 	 * If this super block is going away, return false as it
735619c8c76SIan Kent 	 * can't match as an existing super block.
736619c8c76SIan Kent 	 */
737619c8c76SIan Kent 	if (!atomic_read(&s->s_active))
738619c8c76SIan Kent 		return 0;
739450ba0eaSJosef Bacik 	return root->fs_info->fs_devices == test_root->fs_info->fs_devices;
7404b82d6e4SYan }
7414b82d6e4SYan 
742450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
743450ba0eaSJosef Bacik {
744450ba0eaSJosef Bacik 	s->s_fs_info = data;
745450ba0eaSJosef Bacik 
746450ba0eaSJosef Bacik 	return set_anon_super(s, data);
747450ba0eaSJosef Bacik }
748450ba0eaSJosef Bacik 
749830c4adbSJosef Bacik /*
750f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
751f9d9ef62SDavid Sterba  */
752f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
753f9d9ef62SDavid Sterba {
754f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
755f9d9ef62SDavid Sterba 		return 1;
756f9d9ef62SDavid Sterba 	return 0;
757f9d9ef62SDavid Sterba }
758f9d9ef62SDavid Sterba 
759f9d9ef62SDavid Sterba /*
760830c4adbSJosef Bacik  * This will strip out the subvol=%s argument for an argument string and add
761830c4adbSJosef Bacik  * subvolid=0 to make sure we get the actual tree root for path walking to the
762830c4adbSJosef Bacik  * subvol we want.
763830c4adbSJosef Bacik  */
764830c4adbSJosef Bacik static char *setup_root_args(char *args)
765830c4adbSJosef Bacik {
766830c4adbSJosef Bacik 	unsigned copied = 0;
767830c4adbSJosef Bacik 	unsigned len = strlen(args) + 2;
768830c4adbSJosef Bacik 	char *pos;
769830c4adbSJosef Bacik 	char *ret;
770830c4adbSJosef Bacik 
771830c4adbSJosef Bacik 	/*
772830c4adbSJosef Bacik 	 * We need the same args as before, but minus
773830c4adbSJosef Bacik 	 *
774830c4adbSJosef Bacik 	 * subvol=a
775830c4adbSJosef Bacik 	 *
776830c4adbSJosef Bacik 	 * and add
777830c4adbSJosef Bacik 	 *
778830c4adbSJosef Bacik 	 * subvolid=0
779830c4adbSJosef Bacik 	 *
780830c4adbSJosef Bacik 	 * which is a difference of 2 characters, so we allocate strlen(args) +
781830c4adbSJosef Bacik 	 * 2 characters.
782830c4adbSJosef Bacik 	 */
783830c4adbSJosef Bacik 	ret = kzalloc(len * sizeof(char), GFP_NOFS);
784830c4adbSJosef Bacik 	if (!ret)
785830c4adbSJosef Bacik 		return NULL;
786830c4adbSJosef Bacik 	pos = strstr(args, "subvol=");
787830c4adbSJosef Bacik 
788830c4adbSJosef Bacik 	/* This shouldn't happen, but just in case.. */
789830c4adbSJosef Bacik 	if (!pos) {
790830c4adbSJosef Bacik 		kfree(ret);
791830c4adbSJosef Bacik 		return NULL;
792830c4adbSJosef Bacik 	}
793830c4adbSJosef Bacik 
794830c4adbSJosef Bacik 	/*
795830c4adbSJosef Bacik 	 * The subvol=<> arg is not at the front of the string, copy everybody
796830c4adbSJosef Bacik 	 * up to that into ret.
797830c4adbSJosef Bacik 	 */
798830c4adbSJosef Bacik 	if (pos != args) {
799830c4adbSJosef Bacik 		*pos = '\0';
800830c4adbSJosef Bacik 		strcpy(ret, args);
801830c4adbSJosef Bacik 		copied += strlen(args);
802830c4adbSJosef Bacik 		pos++;
803830c4adbSJosef Bacik 	}
804830c4adbSJosef Bacik 
805830c4adbSJosef Bacik 	strncpy(ret + copied, "subvolid=0", len - copied);
806830c4adbSJosef Bacik 
807830c4adbSJosef Bacik 	/* Length of subvolid=0 */
808830c4adbSJosef Bacik 	copied += 10;
809830c4adbSJosef Bacik 
810830c4adbSJosef Bacik 	/*
811830c4adbSJosef Bacik 	 * If there is no , after the subvol= option then we know there's no
812830c4adbSJosef Bacik 	 * other options and we can just return.
813830c4adbSJosef Bacik 	 */
814830c4adbSJosef Bacik 	pos = strchr(pos, ',');
815830c4adbSJosef Bacik 	if (!pos)
816830c4adbSJosef Bacik 		return ret;
817830c4adbSJosef Bacik 
818830c4adbSJosef Bacik 	/* Copy the rest of the arguments into our buffer */
819830c4adbSJosef Bacik 	strncpy(ret + copied, pos, len - copied);
820830c4adbSJosef Bacik 	copied += strlen(pos);
821830c4adbSJosef Bacik 
822830c4adbSJosef Bacik 	return ret;
823830c4adbSJosef Bacik }
824830c4adbSJosef Bacik 
825830c4adbSJosef Bacik static struct dentry *mount_subvol(const char *subvol_name, int flags,
826830c4adbSJosef Bacik 				   const char *device_name, char *data)
827830c4adbSJosef Bacik {
828830c4adbSJosef Bacik 	struct super_block *s;
829830c4adbSJosef Bacik 	struct dentry *root;
830830c4adbSJosef Bacik 	struct vfsmount *mnt;
831830c4adbSJosef Bacik 	struct mnt_namespace *ns_private;
832830c4adbSJosef Bacik 	char *newargs;
833830c4adbSJosef Bacik 	struct path path;
834830c4adbSJosef Bacik 	int error;
835830c4adbSJosef Bacik 
836830c4adbSJosef Bacik 	newargs = setup_root_args(data);
837830c4adbSJosef Bacik 	if (!newargs)
838830c4adbSJosef Bacik 		return ERR_PTR(-ENOMEM);
839830c4adbSJosef Bacik 	mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name,
840830c4adbSJosef Bacik 			     newargs);
841830c4adbSJosef Bacik 	kfree(newargs);
842830c4adbSJosef Bacik 	if (IS_ERR(mnt))
843830c4adbSJosef Bacik 		return ERR_CAST(mnt);
844830c4adbSJosef Bacik 
845830c4adbSJosef Bacik 	ns_private = create_mnt_ns(mnt);
846830c4adbSJosef Bacik 	if (IS_ERR(ns_private)) {
847830c4adbSJosef Bacik 		mntput(mnt);
848830c4adbSJosef Bacik 		return ERR_CAST(ns_private);
849830c4adbSJosef Bacik 	}
850830c4adbSJosef Bacik 
851830c4adbSJosef Bacik 	/*
852830c4adbSJosef Bacik 	 * This will trigger the automount of the subvol so we can just
853830c4adbSJosef Bacik 	 * drop the mnt we have here and return the dentry that we
854830c4adbSJosef Bacik 	 * found.
855830c4adbSJosef Bacik 	 */
856830c4adbSJosef Bacik 	error = vfs_path_lookup(mnt->mnt_root, mnt, subvol_name,
857830c4adbSJosef Bacik 				LOOKUP_FOLLOW, &path);
858830c4adbSJosef Bacik 	put_mnt_ns(ns_private);
859830c4adbSJosef Bacik 	if (error)
860830c4adbSJosef Bacik 		return ERR_PTR(error);
861830c4adbSJosef Bacik 
862f9d9ef62SDavid Sterba 	if (!is_subvolume_inode(path.dentry->d_inode)) {
863f9d9ef62SDavid Sterba 		path_put(&path);
864f9d9ef62SDavid Sterba 		mntput(mnt);
865f9d9ef62SDavid Sterba 		error = -EINVAL;
866f9d9ef62SDavid Sterba 		printk(KERN_ERR "btrfs: '%s' is not a valid subvolume\n",
867f9d9ef62SDavid Sterba 				subvol_name);
868f9d9ef62SDavid Sterba 		return ERR_PTR(-EINVAL);
869f9d9ef62SDavid Sterba 	}
870f9d9ef62SDavid Sterba 
871830c4adbSJosef Bacik 	/* Get a ref to the sb and the dentry we found and return it */
872830c4adbSJosef Bacik 	s = path.mnt->mnt_sb;
873830c4adbSJosef Bacik 	atomic_inc(&s->s_active);
874830c4adbSJosef Bacik 	root = dget(path.dentry);
875830c4adbSJosef Bacik 	path_put(&path);
876830c4adbSJosef Bacik 	down_write(&s->s_umount);
877830c4adbSJosef Bacik 
878830c4adbSJosef Bacik 	return root;
879830c4adbSJosef Bacik }
880450ba0eaSJosef Bacik 
881edf24abeSChristoph Hellwig /*
882edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
883edf24abeSChristoph Hellwig  *
884edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
885edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
886edf24abeSChristoph Hellwig  */
887061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
888306e16ceSDavid Sterba 		const char *device_name, void *data)
8894b82d6e4SYan {
8904b82d6e4SYan 	struct block_device *bdev = NULL;
8914b82d6e4SYan 	struct super_block *s;
8924b82d6e4SYan 	struct dentry *root;
8938a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
894450ba0eaSJosef Bacik 	struct btrfs_fs_info *fs_info = NULL;
89597288f2cSChristoph Hellwig 	fmode_t mode = FMODE_READ;
89673f73415SJosef Bacik 	char *subvol_name = NULL;
89773f73415SJosef Bacik 	u64 subvol_objectid = 0;
898e15d0542SXin Zhong 	u64 subvol_rootid = 0;
8994b82d6e4SYan 	int error = 0;
9004b82d6e4SYan 
90197288f2cSChristoph Hellwig 	if (!(flags & MS_RDONLY))
90297288f2cSChristoph Hellwig 		mode |= FMODE_WRITE;
90397288f2cSChristoph Hellwig 
90497288f2cSChristoph Hellwig 	error = btrfs_parse_early_options(data, mode, fs_type,
90573f73415SJosef Bacik 					  &subvol_name, &subvol_objectid,
906e15d0542SXin Zhong 					  &subvol_rootid, &fs_devices);
907f23c8af8SIlya Dryomov 	if (error) {
908f23c8af8SIlya Dryomov 		kfree(subvol_name);
909061dbc6bSAl Viro 		return ERR_PTR(error);
910f23c8af8SIlya Dryomov 	}
911edf24abeSChristoph Hellwig 
912830c4adbSJosef Bacik 	if (subvol_name) {
913830c4adbSJosef Bacik 		root = mount_subvol(subvol_name, flags, device_name, data);
914830c4adbSJosef Bacik 		kfree(subvol_name);
915830c4adbSJosef Bacik 		return root;
916830c4adbSJosef Bacik 	}
917830c4adbSJosef Bacik 
918306e16ceSDavid Sterba 	error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
9198a4b83ccSChris Mason 	if (error)
920830c4adbSJosef Bacik 		return ERR_PTR(error);
9214b82d6e4SYan 
922450ba0eaSJosef Bacik 	/*
923450ba0eaSJosef Bacik 	 * Setup a dummy root and fs_info for test/set super.  This is because
924450ba0eaSJosef Bacik 	 * we don't actually fill this stuff out until open_ctree, but we need
925450ba0eaSJosef Bacik 	 * it for searching for existing supers, so this lets us do that and
926450ba0eaSJosef Bacik 	 * then open_ctree will properly initialize everything later.
927450ba0eaSJosef Bacik 	 */
928450ba0eaSJosef Bacik 	fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
92904d21a24SIlya Dryomov 	if (!fs_info)
93004d21a24SIlya Dryomov 		return ERR_PTR(-ENOMEM);
93104d21a24SIlya Dryomov 
93204d21a24SIlya Dryomov 	fs_info->tree_root = kzalloc(sizeof(struct btrfs_root), GFP_NOFS);
93304d21a24SIlya Dryomov 	if (!fs_info->tree_root) {
93445ea6095Sslyich@gmail.com 		error = -ENOMEM;
93504d21a24SIlya Dryomov 		goto error_fs_info;
93645ea6095Sslyich@gmail.com 	}
93704d21a24SIlya Dryomov 	fs_info->tree_root->fs_info = fs_info;
938450ba0eaSJosef Bacik 	fs_info->fs_devices = fs_devices;
939450ba0eaSJosef Bacik 
9406c41761fSDavid Sterba 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
9416c41761fSDavid Sterba 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
9426c41761fSDavid Sterba 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
9436c41761fSDavid Sterba 		error = -ENOMEM;
94404d21a24SIlya Dryomov 		goto error_fs_info;
94504d21a24SIlya Dryomov 	}
94604d21a24SIlya Dryomov 
94704d21a24SIlya Dryomov 	error = btrfs_open_devices(fs_devices, mode, fs_type);
94804d21a24SIlya Dryomov 	if (error)
94904d21a24SIlya Dryomov 		goto error_fs_info;
95004d21a24SIlya Dryomov 
95104d21a24SIlya Dryomov 	if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
95204d21a24SIlya Dryomov 		error = -EACCES;
9536c41761fSDavid Sterba 		goto error_close_devices;
9546c41761fSDavid Sterba 	}
9556c41761fSDavid Sterba 
956dfe25020SChris Mason 	bdev = fs_devices->latest_bdev;
95704d21a24SIlya Dryomov 	s = sget(fs_type, btrfs_test_super, btrfs_set_super,
95804d21a24SIlya Dryomov 		 fs_info->tree_root);
959830c4adbSJosef Bacik 	if (IS_ERR(s)) {
960830c4adbSJosef Bacik 		error = PTR_ERR(s);
961830c4adbSJosef Bacik 		goto error_close_devices;
962830c4adbSJosef Bacik 	}
9634b82d6e4SYan 
9644b82d6e4SYan 	if (s->s_root) {
9654b82d6e4SYan 		if ((flags ^ s->s_flags) & MS_RDONLY) {
9666f5bbff9SAl Viro 			deactivate_locked_super(s);
96704d21a24SIlya Dryomov 			error = -EBUSY;
96804d21a24SIlya Dryomov 			goto error_close_devices;
9694b82d6e4SYan 		}
9704b82d6e4SYan 
9712b82032cSYan Zheng 		btrfs_close_devices(fs_devices);
9726c41761fSDavid Sterba 		free_fs_info(fs_info);
9734b82d6e4SYan 	} else {
9744b82d6e4SYan 		char b[BDEVNAME_SIZE];
9754b82d6e4SYan 
9769e1f1de0SAl Viro 		s->s_flags = flags | MS_NOSEC;
9774b82d6e4SYan 		strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
9785f524444SIlya Dryomov 		btrfs_sb(s)->fs_info->bdev_holder = fs_type;
9798a4b83ccSChris Mason 		error = btrfs_fill_super(s, fs_devices, data,
9808a4b83ccSChris Mason 					 flags & MS_SILENT ? 1 : 0);
9814b82d6e4SYan 		if (error) {
9826f5bbff9SAl Viro 			deactivate_locked_super(s);
983830c4adbSJosef Bacik 			return ERR_PTR(error);
9844b82d6e4SYan 		}
9854b82d6e4SYan 
9864b82d6e4SYan 		s->s_flags |= MS_ACTIVE;
9874b82d6e4SYan 	}
9884b82d6e4SYan 
989e15d0542SXin Zhong 	root = get_default_root(s, subvol_objectid);
990e15d0542SXin Zhong 	if (IS_ERR(root)) {
991e15d0542SXin Zhong 		deactivate_locked_super(s);
992830c4adbSJosef Bacik 		return root;
99376fcef19SDavid Woodhouse 	}
9944b82d6e4SYan 
995061dbc6bSAl Viro 	return root;
9964b82d6e4SYan 
997c146afadSYan Zheng error_close_devices:
9988a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
99904d21a24SIlya Dryomov error_fs_info:
10006c41761fSDavid Sterba 	free_fs_info(fs_info);
1001061dbc6bSAl Viro 	return ERR_PTR(error);
10024b82d6e4SYan }
10032e635a27SChris Mason 
1004c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1005c146afadSYan Zheng {
1006c146afadSYan Zheng 	struct btrfs_root *root = btrfs_sb(sb);
1007c146afadSYan Zheng 	int ret;
1008c146afadSYan Zheng 
1009b288052eSChris Mason 	ret = btrfs_parse_options(root, data);
1010b288052eSChris Mason 	if (ret)
1011b288052eSChris Mason 		return -EINVAL;
1012b288052eSChris Mason 
1013c146afadSYan Zheng 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
1014c146afadSYan Zheng 		return 0;
1015c146afadSYan Zheng 
1016c146afadSYan Zheng 	if (*flags & MS_RDONLY) {
1017c146afadSYan Zheng 		sb->s_flags |= MS_RDONLY;
1018c146afadSYan Zheng 
1019c146afadSYan Zheng 		ret =  btrfs_commit_super(root);
1020c146afadSYan Zheng 		WARN_ON(ret);
1021c146afadSYan Zheng 	} else {
10222b82032cSYan Zheng 		if (root->fs_info->fs_devices->rw_devices == 0)
10232b82032cSYan Zheng 			return -EACCES;
10242b82032cSYan Zheng 
10256c41761fSDavid Sterba 		if (btrfs_super_log_root(root->fs_info->super_copy) != 0)
1026c146afadSYan Zheng 			return -EINVAL;
1027c146afadSYan Zheng 
1028d68fc57bSYan, Zheng 		ret = btrfs_cleanup_fs_roots(root->fs_info);
1029c146afadSYan Zheng 		WARN_ON(ret);
1030c146afadSYan Zheng 
1031d68fc57bSYan, Zheng 		/* recover relocation */
1032d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
1033c146afadSYan Zheng 		WARN_ON(ret);
1034c146afadSYan Zheng 
1035c146afadSYan Zheng 		sb->s_flags &= ~MS_RDONLY;
1036c146afadSYan Zheng 	}
1037c146afadSYan Zheng 
1038c146afadSYan Zheng 	return 0;
1039c146afadSYan Zheng }
1040c146afadSYan Zheng 
1041bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
1042bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1,
1043bcd53741SArne Jansen 				       const void *dev_info2)
1044bcd53741SArne Jansen {
1045bcd53741SArne Jansen 	if (((struct btrfs_device_info *)dev_info1)->max_avail >
1046bcd53741SArne Jansen 	    ((struct btrfs_device_info *)dev_info2)->max_avail)
1047bcd53741SArne Jansen 		return -1;
1048bcd53741SArne Jansen 	else if (((struct btrfs_device_info *)dev_info1)->max_avail <
1049bcd53741SArne Jansen 		 ((struct btrfs_device_info *)dev_info2)->max_avail)
1050bcd53741SArne Jansen 		return 1;
1051bcd53741SArne Jansen 	else
1052bcd53741SArne Jansen 	return 0;
1053bcd53741SArne Jansen }
1054bcd53741SArne Jansen 
1055bcd53741SArne Jansen /*
1056bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
1057bcd53741SArne Jansen  * is stored.(Descending Sort)
1058bcd53741SArne Jansen  */
1059bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
1060bcd53741SArne Jansen 					struct btrfs_device_info *devices,
1061bcd53741SArne Jansen 					size_t nr_devices)
1062bcd53741SArne Jansen {
1063bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1064bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
1065bcd53741SArne Jansen }
1066bcd53741SArne Jansen 
10676d07bcecSMiao Xie /*
10686d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
10696d07bcecSMiao Xie  * file data.
10706d07bcecSMiao Xie  */
10716d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
10726d07bcecSMiao Xie {
10736d07bcecSMiao Xie 	struct btrfs_fs_info *fs_info = root->fs_info;
10746d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
10756d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
10766d07bcecSMiao Xie 	struct btrfs_device *device;
10776d07bcecSMiao Xie 	u64 skip_space;
10786d07bcecSMiao Xie 	u64 type;
10796d07bcecSMiao Xie 	u64 avail_space;
10806d07bcecSMiao Xie 	u64 used_space;
10816d07bcecSMiao Xie 	u64 min_stripe_size;
1082*39fb26c3SMiao Xie 	int min_stripes = 1, num_stripes = 1;
10836d07bcecSMiao Xie 	int i = 0, nr_devices;
10846d07bcecSMiao Xie 	int ret;
10856d07bcecSMiao Xie 
1086b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
10876d07bcecSMiao Xie 	BUG_ON(!nr_devices);
10886d07bcecSMiao Xie 
10896d07bcecSMiao Xie 	devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
10906d07bcecSMiao Xie 			       GFP_NOFS);
10916d07bcecSMiao Xie 	if (!devices_info)
10926d07bcecSMiao Xie 		return -ENOMEM;
10936d07bcecSMiao Xie 
10946d07bcecSMiao Xie 	/* calc min stripe number for data space alloction */
10956d07bcecSMiao Xie 	type = btrfs_get_alloc_profile(root, 1);
1096*39fb26c3SMiao Xie 	if (type & BTRFS_BLOCK_GROUP_RAID0) {
10976d07bcecSMiao Xie 		min_stripes = 2;
1098*39fb26c3SMiao Xie 		num_stripes = nr_devices;
1099*39fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID1) {
11006d07bcecSMiao Xie 		min_stripes = 2;
1101*39fb26c3SMiao Xie 		num_stripes = 2;
1102*39fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID10) {
11036d07bcecSMiao Xie 		min_stripes = 4;
1104*39fb26c3SMiao Xie 		num_stripes = 4;
1105*39fb26c3SMiao Xie 	}
11066d07bcecSMiao Xie 
11076d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_DUP)
11086d07bcecSMiao Xie 		min_stripe_size = 2 * BTRFS_STRIPE_LEN;
11096d07bcecSMiao Xie 	else
11106d07bcecSMiao Xie 		min_stripe_size = BTRFS_STRIPE_LEN;
11116d07bcecSMiao Xie 
1112b772a86eSLi Zefan 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
1113b772a86eSLi Zefan 		if (!device->in_fs_metadata || !device->bdev)
11146d07bcecSMiao Xie 			continue;
11156d07bcecSMiao Xie 
11166d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
11176d07bcecSMiao Xie 
11186d07bcecSMiao Xie 		/* align with stripe_len */
11196d07bcecSMiao Xie 		do_div(avail_space, BTRFS_STRIPE_LEN);
11206d07bcecSMiao Xie 		avail_space *= BTRFS_STRIPE_LEN;
11216d07bcecSMiao Xie 
11226d07bcecSMiao Xie 		/*
11236d07bcecSMiao Xie 		 * In order to avoid overwritting the superblock on the drive,
11246d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
11256d07bcecSMiao Xie 		 * allocation.
11266d07bcecSMiao Xie 		 */
11276d07bcecSMiao Xie 		skip_space = 1024 * 1024;
11286d07bcecSMiao Xie 
11296d07bcecSMiao Xie 		/* user can set the offset in fs_info->alloc_start. */
11306d07bcecSMiao Xie 		if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
11316d07bcecSMiao Xie 		    device->total_bytes)
11326d07bcecSMiao Xie 			skip_space = max(fs_info->alloc_start, skip_space);
11336d07bcecSMiao Xie 
11346d07bcecSMiao Xie 		/*
11356d07bcecSMiao Xie 		 * btrfs can not use the free space in [0, skip_space - 1],
11366d07bcecSMiao Xie 		 * we must subtract it from the total. In order to implement
11376d07bcecSMiao Xie 		 * it, we account the used space in this range first.
11386d07bcecSMiao Xie 		 */
11396d07bcecSMiao Xie 		ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
11406d07bcecSMiao Xie 						     &used_space);
11416d07bcecSMiao Xie 		if (ret) {
11426d07bcecSMiao Xie 			kfree(devices_info);
11436d07bcecSMiao Xie 			return ret;
11446d07bcecSMiao Xie 		}
11456d07bcecSMiao Xie 
11466d07bcecSMiao Xie 		/* calc the free space in [0, skip_space - 1] */
11476d07bcecSMiao Xie 		skip_space -= used_space;
11486d07bcecSMiao Xie 
11496d07bcecSMiao Xie 		/*
11506d07bcecSMiao Xie 		 * we can use the free space in [0, skip_space - 1], subtract
11516d07bcecSMiao Xie 		 * it from the total.
11526d07bcecSMiao Xie 		 */
11536d07bcecSMiao Xie 		if (avail_space && avail_space >= skip_space)
11546d07bcecSMiao Xie 			avail_space -= skip_space;
11556d07bcecSMiao Xie 		else
11566d07bcecSMiao Xie 			avail_space = 0;
11576d07bcecSMiao Xie 
11586d07bcecSMiao Xie 		if (avail_space < min_stripe_size)
11596d07bcecSMiao Xie 			continue;
11606d07bcecSMiao Xie 
11616d07bcecSMiao Xie 		devices_info[i].dev = device;
11626d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
11636d07bcecSMiao Xie 
11646d07bcecSMiao Xie 		i++;
11656d07bcecSMiao Xie 	}
11666d07bcecSMiao Xie 
11676d07bcecSMiao Xie 	nr_devices = i;
11686d07bcecSMiao Xie 
11696d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
11706d07bcecSMiao Xie 
11716d07bcecSMiao Xie 	i = nr_devices - 1;
11726d07bcecSMiao Xie 	avail_space = 0;
11736d07bcecSMiao Xie 	while (nr_devices >= min_stripes) {
1174*39fb26c3SMiao Xie 		if (num_stripes > nr_devices)
1175*39fb26c3SMiao Xie 			num_stripes = nr_devices;
1176*39fb26c3SMiao Xie 
11776d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
11786d07bcecSMiao Xie 			int j;
11796d07bcecSMiao Xie 			u64 alloc_size;
11806d07bcecSMiao Xie 
1181*39fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
11826d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
1183*39fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
11846d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
11856d07bcecSMiao Xie 		}
11866d07bcecSMiao Xie 		i--;
11876d07bcecSMiao Xie 		nr_devices--;
11886d07bcecSMiao Xie 	}
11896d07bcecSMiao Xie 
11906d07bcecSMiao Xie 	kfree(devices_info);
11916d07bcecSMiao Xie 	*free_bytes = avail_space;
11926d07bcecSMiao Xie 	return 0;
11936d07bcecSMiao Xie }
11946d07bcecSMiao Xie 
11958fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
11968fd17795SChris Mason {
11978fd17795SChris Mason 	struct btrfs_root *root = btrfs_sb(dentry->d_sb);
11986c41761fSDavid Sterba 	struct btrfs_super_block *disk_super = root->fs_info->super_copy;
1199bd4d1088SJosef Bacik 	struct list_head *head = &root->fs_info->space_info;
1200bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
1201bd4d1088SJosef Bacik 	u64 total_used = 0;
12026d07bcecSMiao Xie 	u64 total_free_data = 0;
1203db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
12049d03632eSDavid Woodhouse 	__be32 *fsid = (__be32 *)root->fs_info->fsid;
12056d07bcecSMiao Xie 	int ret;
12068fd17795SChris Mason 
12076d07bcecSMiao Xie 	/* holding chunk_muext to avoid allocating new chunks */
12086d07bcecSMiao Xie 	mutex_lock(&root->fs_info->chunk_mutex);
1209bd4d1088SJosef Bacik 	rcu_read_lock();
121089a55897SJosef Bacik 	list_for_each_entry_rcu(found, head, list) {
12116d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
12126d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
12136d07bcecSMiao Xie 			total_free_data -=
12146d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
12156d07bcecSMiao Xie 		}
12166d07bcecSMiao Xie 
1217b742bb82SYan, Zheng 		total_used += found->disk_used;
121889a55897SJosef Bacik 	}
1219bd4d1088SJosef Bacik 	rcu_read_unlock();
1220bd4d1088SJosef Bacik 
12218fd17795SChris Mason 	buf->f_namelen = BTRFS_NAME_LEN;
1222db94535dSChris Mason 	buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
1223bd4d1088SJosef Bacik 	buf->f_bfree = buf->f_blocks - (total_used >> bits);
12248fd17795SChris Mason 	buf->f_bsize = dentry->d_sb->s_blocksize;
12258fd17795SChris Mason 	buf->f_type = BTRFS_SUPER_MAGIC;
12266d07bcecSMiao Xie 	buf->f_bavail = total_free_data;
12276d07bcecSMiao Xie 	ret = btrfs_calc_avail_data_space(root, &total_free_data);
12286d07bcecSMiao Xie 	if (ret) {
12296d07bcecSMiao Xie 		mutex_unlock(&root->fs_info->chunk_mutex);
12306d07bcecSMiao Xie 		return ret;
12316d07bcecSMiao Xie 	}
12326d07bcecSMiao Xie 	buf->f_bavail += total_free_data;
12336d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
12346d07bcecSMiao Xie 	mutex_unlock(&root->fs_info->chunk_mutex);
1235d397712bSChris Mason 
12369d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
12379d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
12389d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
12399d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
12409d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
124132d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
124232d48fa1SDavid Woodhouse 	buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
124332d48fa1SDavid Woodhouse 	buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
124432d48fa1SDavid Woodhouse 
12458fd17795SChris Mason 	return 0;
12468fd17795SChris Mason }
1247b5133862SChris Mason 
12482e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
12492e635a27SChris Mason 	.owner		= THIS_MODULE,
12502e635a27SChris Mason 	.name		= "btrfs",
1251061dbc6bSAl Viro 	.mount		= btrfs_mount,
1252a061fc8dSChris Mason 	.kill_sb	= kill_anon_super,
12532e635a27SChris Mason 	.fs_flags	= FS_REQUIRES_DEV,
12542e635a27SChris Mason };
1255a9218f6bSChris Mason 
1256d352ac68SChris Mason /*
1257d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
1258d352ac68SChris Mason  */
12598a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
12608a4b83ccSChris Mason 				unsigned long arg)
12618a4b83ccSChris Mason {
12628a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
12638a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
1264c071fcfdSChris Mason 	int ret = -ENOTTY;
12658a4b83ccSChris Mason 
1266e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
1267e441d54dSChris Mason 		return -EPERM;
1268e441d54dSChris Mason 
1269dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
1270dae7b665SLi Zefan 	if (IS_ERR(vol))
1271dae7b665SLi Zefan 		return PTR_ERR(vol);
1272c071fcfdSChris Mason 
12738a4b83ccSChris Mason 	switch (cmd) {
12748a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
127597288f2cSChristoph Hellwig 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
12768a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
12778a4b83ccSChris Mason 		break;
12788a4b83ccSChris Mason 	}
1279dae7b665SLi Zefan 
12808a4b83ccSChris Mason 	kfree(vol);
1281f819d837SLinda Knippers 	return ret;
12828a4b83ccSChris Mason }
12838a4b83ccSChris Mason 
12840176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
1285ed0dab6bSYan {
1286ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
1287a74a4b97SChris Mason 	mutex_lock(&root->fs_info->transaction_kthread_mutex);
1288a74a4b97SChris Mason 	mutex_lock(&root->fs_info->cleaner_mutex);
12890176260fSLinus Torvalds 	return 0;
1290ed0dab6bSYan }
1291ed0dab6bSYan 
12920176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb)
1293ed0dab6bSYan {
1294ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
1295a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->cleaner_mutex);
1296a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->transaction_kthread_mutex);
12970176260fSLinus Torvalds 	return 0;
1298ed0dab6bSYan }
12992e635a27SChris Mason 
1300b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
130176dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
1302bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
1303e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
1304d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
1305a9572a15SEric Paris 	.show_options	= btrfs_show_options,
13064730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
1307b5133862SChris Mason 	.dirty_inode	= btrfs_dirty_inode,
13082c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
13092c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
13108fd17795SChris Mason 	.statfs		= btrfs_statfs,
1311c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
13120176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
13130176260fSLinus Torvalds 	.unfreeze_fs	= btrfs_unfreeze,
1314e20d96d6SChris Mason };
1315a9218f6bSChris Mason 
1316a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
1317a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
1318a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
1319a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
13206038f373SArnd Bergmann 	.llseek = noop_llseek,
1321a9218f6bSChris Mason };
1322a9218f6bSChris Mason 
1323a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
1324578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
1325a9218f6bSChris Mason 	.name		= "btrfs-control",
1326a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
1327a9218f6bSChris Mason };
1328a9218f6bSChris Mason 
1329578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
1330578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
1331578454ffSKay Sievers 
1332a9218f6bSChris Mason static int btrfs_interface_init(void)
1333a9218f6bSChris Mason {
1334a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
1335a9218f6bSChris Mason }
1336a9218f6bSChris Mason 
1337b2950863SChristoph Hellwig static void btrfs_interface_exit(void)
1338a9218f6bSChris Mason {
1339a9218f6bSChris Mason 	if (misc_deregister(&btrfs_misc) < 0)
1340d397712bSChris Mason 		printk(KERN_INFO "misc_deregister failed for control device");
1341a9218f6bSChris Mason }
1342a9218f6bSChris Mason 
13432e635a27SChris Mason static int __init init_btrfs_fs(void)
13442e635a27SChris Mason {
13452c90e5d6SChris Mason 	int err;
134658176a96SJosef Bacik 
134758176a96SJosef Bacik 	err = btrfs_init_sysfs();
134858176a96SJosef Bacik 	if (err)
134958176a96SJosef Bacik 		return err;
135058176a96SJosef Bacik 
1351261507a0SLi Zefan 	err = btrfs_init_compress();
13522c90e5d6SChris Mason 	if (err)
1353a74a4b97SChris Mason 		goto free_sysfs;
1354d1310b2eSChris Mason 
1355261507a0SLi Zefan 	err = btrfs_init_cachep();
1356261507a0SLi Zefan 	if (err)
1357261507a0SLi Zefan 		goto free_compress;
1358261507a0SLi Zefan 
1359d1310b2eSChris Mason 	err = extent_io_init();
13602f4cbe64SWyatt Banks 	if (err)
13612f4cbe64SWyatt Banks 		goto free_cachep;
13622f4cbe64SWyatt Banks 
1363d1310b2eSChris Mason 	err = extent_map_init();
1364d1310b2eSChris Mason 	if (err)
1365d1310b2eSChris Mason 		goto free_extent_io;
1366d1310b2eSChris Mason 
136716cdcec7SMiao Xie 	err = btrfs_delayed_inode_init();
13682f4cbe64SWyatt Banks 	if (err)
13692f4cbe64SWyatt Banks 		goto free_extent_map;
1370c8b97818SChris Mason 
137116cdcec7SMiao Xie 	err = btrfs_interface_init();
137216cdcec7SMiao Xie 	if (err)
137316cdcec7SMiao Xie 		goto free_delayed_inode;
137416cdcec7SMiao Xie 
1375a9218f6bSChris Mason 	err = register_filesystem(&btrfs_fs_type);
1376a9218f6bSChris Mason 	if (err)
1377a9218f6bSChris Mason 		goto unregister_ioctl;
1378b3c3da71SChris Mason 
1379b3c3da71SChris Mason 	printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
13802f4cbe64SWyatt Banks 	return 0;
13812f4cbe64SWyatt Banks 
1382a9218f6bSChris Mason unregister_ioctl:
1383a9218f6bSChris Mason 	btrfs_interface_exit();
138416cdcec7SMiao Xie free_delayed_inode:
138516cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
13862f4cbe64SWyatt Banks free_extent_map:
13872f4cbe64SWyatt Banks 	extent_map_exit();
1388d1310b2eSChris Mason free_extent_io:
1389d1310b2eSChris Mason 	extent_io_exit();
13902f4cbe64SWyatt Banks free_cachep:
13912f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
1392261507a0SLi Zefan free_compress:
1393261507a0SLi Zefan 	btrfs_exit_compress();
1394a74a4b97SChris Mason free_sysfs:
13952f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
13962c90e5d6SChris Mason 	return err;
13972e635a27SChris Mason }
13982e635a27SChris Mason 
13992e635a27SChris Mason static void __exit exit_btrfs_fs(void)
14002e635a27SChris Mason {
140139279cc3SChris Mason 	btrfs_destroy_cachep();
140216cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
1403a52d9a80SChris Mason 	extent_map_exit();
1404d1310b2eSChris Mason 	extent_io_exit();
1405a9218f6bSChris Mason 	btrfs_interface_exit();
14062e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
140758176a96SJosef Bacik 	btrfs_exit_sysfs();
14088a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
1409261507a0SLi Zefan 	btrfs_exit_compress();
14102e635a27SChris Mason }
14112e635a27SChris Mason 
14122e635a27SChris Mason module_init(init_btrfs_fs)
14132e635a27SChris Mason module_exit(exit_btrfs_fs)
14142e635a27SChris Mason 
14152e635a27SChris Mason MODULE_LICENSE("GPL");
1416