xref: /openbmc/linux/fs/btrfs/super.c (revision 6d07bcec)
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>
424b4e25f2SChris Mason #include "compat.h"
432e635a27SChris Mason #include "ctree.h"
44e20d96d6SChris Mason #include "disk-io.h"
45d5719762SChris Mason #include "transaction.h"
462c90e5d6SChris Mason #include "btrfs_inode.h"
47c5739bbaSChris Mason #include "ioctl.h"
483a686375SChris Mason #include "print-tree.h"
495103e947SJosef Bacik #include "xattr.h"
508a4b83ccSChris Mason #include "volumes.h"
51b3c3da71SChris Mason #include "version.h"
52be6e8dc0SBalaji Rao #include "export.h"
53c8b97818SChris Mason #include "compression.h"
542e635a27SChris Mason 
55b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
56e20d96d6SChris Mason 
57e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
58e20d96d6SChris Mason {
59e20d96d6SChris Mason 	struct btrfs_root *root = btrfs_sb(sb);
60e20d96d6SChris Mason 	int ret;
61e20d96d6SChris Mason 
62e20d96d6SChris Mason 	ret = close_ctree(root);
63e20d96d6SChris Mason 	sb->s_fs_info = NULL;
64559af821SAndi Kleen 
65559af821SAndi Kleen 	(void)ret; /* FIXME: need to fix VFS to return error? */
66e20d96d6SChris Mason }
672e635a27SChris Mason 
6895e05289SChris Mason enum {
6973f73415SJosef Bacik 	Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
70287a0ab9SJosef Bacik 	Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
71287a0ab9SJosef Bacik 	Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
72261507a0SLi Zefan 	Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
73261507a0SLi Zefan 	Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
74261507a0SLi Zefan 	Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed, Opt_err,
7595e05289SChris Mason };
7695e05289SChris Mason 
7795e05289SChris Mason static match_table_t tokens = {
78dfe25020SChris Mason 	{Opt_degraded, "degraded"},
7995e05289SChris Mason 	{Opt_subvol, "subvol=%s"},
8073f73415SJosef Bacik 	{Opt_subvolid, "subvolid=%d"},
8143e570b0SChristoph Hellwig 	{Opt_device, "device=%s"},
82b6cda9bcSChris Mason 	{Opt_nodatasum, "nodatasum"},
83be20aa9dSChris Mason 	{Opt_nodatacow, "nodatacow"},
8421ad10cfSChris Mason 	{Opt_nobarrier, "nobarrier"},
856f568d35SChris Mason 	{Opt_max_inline, "max_inline=%s"},
868f662a76SChris Mason 	{Opt_alloc_start, "alloc_start=%s"},
874543df7eSChris Mason 	{Opt_thread_pool, "thread_pool=%d"},
88c8b97818SChris Mason 	{Opt_compress, "compress"},
89261507a0SLi Zefan 	{Opt_compress_type, "compress=%s"},
90a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
91261507a0SLi Zefan 	{Opt_compress_force_type, "compress-force=%s"},
92e18e4809SChris Mason 	{Opt_ssd, "ssd"},
93451d7585SChris Mason 	{Opt_ssd_spread, "ssd_spread"},
943b30c22fSChris Mason 	{Opt_nossd, "nossd"},
9533268eafSJosef Bacik 	{Opt_noacl, "noacl"},
963a5e1404SSage Weil 	{Opt_notreelog, "notreelog"},
97dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
9897e728d4SJosef Bacik 	{Opt_ratio, "metadata_ratio=%d"},
99e244a0aeSChristoph Hellwig 	{Opt_discard, "discard"},
1000af3d00bSJosef Bacik 	{Opt_space_cache, "space_cache"},
10188c2ba3bSJosef Bacik 	{Opt_clear_cache, "clear_cache"},
1024260f7c7SSage Weil 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
10333268eafSJosef Bacik 	{Opt_err, NULL},
10495e05289SChris Mason };
10595e05289SChris Mason 
106edf24abeSChristoph Hellwig /*
107edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
108edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
109edf24abeSChristoph Hellwig  */
110edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options)
11195e05289SChris Mason {
112edf24abeSChristoph Hellwig 	struct btrfs_fs_info *info = root->fs_info;
11395e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
114da495eccSJosef Bacik 	char *p, *num, *orig;
1154543df7eSChris Mason 	int intarg;
116a7a3f7caSSage Weil 	int ret = 0;
117261507a0SLi Zefan 	char *compress_type;
118261507a0SLi Zefan 	bool compress_force = false;
119b6cda9bcSChris Mason 
12095e05289SChris Mason 	if (!options)
121edf24abeSChristoph Hellwig 		return 0;
12295e05289SChris Mason 
123be20aa9dSChris Mason 	/*
124be20aa9dSChris Mason 	 * strsep changes the string, duplicate it because parse_options
125be20aa9dSChris Mason 	 * gets called twice
126be20aa9dSChris Mason 	 */
127be20aa9dSChris Mason 	options = kstrdup(options, GFP_NOFS);
128be20aa9dSChris Mason 	if (!options)
129be20aa9dSChris Mason 		return -ENOMEM;
130be20aa9dSChris Mason 
131da495eccSJosef Bacik 	orig = options;
132be20aa9dSChris Mason 
13395e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
13495e05289SChris Mason 		int token;
13595e05289SChris Mason 		if (!*p)
13695e05289SChris Mason 			continue;
13795e05289SChris Mason 
13895e05289SChris Mason 		token = match_token(p, tokens, args);
13995e05289SChris Mason 		switch (token) {
140dfe25020SChris Mason 		case Opt_degraded:
141edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: allowing degraded mounts\n");
142dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
143dfe25020SChris Mason 			break;
14495e05289SChris Mason 		case Opt_subvol:
14573f73415SJosef Bacik 		case Opt_subvolid:
14643e570b0SChristoph Hellwig 		case Opt_device:
147edf24abeSChristoph Hellwig 			/*
14843e570b0SChristoph Hellwig 			 * These are parsed by btrfs_parse_early_options
149edf24abeSChristoph Hellwig 			 * and can be happily ignored here.
150edf24abeSChristoph Hellwig 			 */
15195e05289SChris Mason 			break;
152b6cda9bcSChris Mason 		case Opt_nodatasum:
153067c28adSChris Mason 			printk(KERN_INFO "btrfs: setting nodatasum\n");
154b6cda9bcSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
155be20aa9dSChris Mason 			break;
156be20aa9dSChris Mason 		case Opt_nodatacow:
157edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: setting nodatacow\n");
158be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
159be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
160b6cda9bcSChris Mason 			break;
161a555f810SChris Mason 		case Opt_compress_force:
162261507a0SLi Zefan 		case Opt_compress_force_type:
163261507a0SLi Zefan 			compress_force = true;
164261507a0SLi Zefan 		case Opt_compress:
165261507a0SLi Zefan 		case Opt_compress_type:
166261507a0SLi Zefan 			if (token == Opt_compress ||
167261507a0SLi Zefan 			    token == Opt_compress_force ||
168261507a0SLi Zefan 			    strcmp(args[0].from, "zlib") == 0) {
169261507a0SLi Zefan 				compress_type = "zlib";
170261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
171a6fa6faeSLi Zefan 			} else if (strcmp(args[0].from, "lzo") == 0) {
172a6fa6faeSLi Zefan 				compress_type = "lzo";
173a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
174261507a0SLi Zefan 			} else {
175261507a0SLi Zefan 				ret = -EINVAL;
176261507a0SLi Zefan 				goto out;
177261507a0SLi Zefan 			}
178261507a0SLi Zefan 
179a555f810SChris Mason 			btrfs_set_opt(info->mount_opt, COMPRESS);
180261507a0SLi Zefan 			if (compress_force) {
181261507a0SLi Zefan 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
182261507a0SLi Zefan 				pr_info("btrfs: force %s compression\n",
183261507a0SLi Zefan 					compress_type);
184261507a0SLi Zefan 			} else
185261507a0SLi Zefan 				pr_info("btrfs: use %s compression\n",
186261507a0SLi Zefan 					compress_type);
187a555f810SChris Mason 			break;
188e18e4809SChris Mason 		case Opt_ssd:
189edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
190e18e4809SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
191e18e4809SChris Mason 			break;
192451d7585SChris Mason 		case Opt_ssd_spread:
193451d7585SChris Mason 			printk(KERN_INFO "btrfs: use spread ssd "
194451d7585SChris Mason 			       "allocation scheme\n");
195451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
196451d7585SChris Mason 			btrfs_set_opt(info->mount_opt, SSD_SPREAD);
197451d7585SChris Mason 			break;
1983b30c22fSChris Mason 		case Opt_nossd:
199451d7585SChris Mason 			printk(KERN_INFO "btrfs: not using ssd allocation "
200451d7585SChris Mason 			       "scheme\n");
201c289811cSChris Mason 			btrfs_set_opt(info->mount_opt, NOSSD);
2023b30c22fSChris Mason 			btrfs_clear_opt(info->mount_opt, SSD);
203451d7585SChris Mason 			btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
2043b30c22fSChris Mason 			break;
20521ad10cfSChris Mason 		case Opt_nobarrier:
206edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: turning off barriers\n");
20721ad10cfSChris Mason 			btrfs_set_opt(info->mount_opt, NOBARRIER);
20821ad10cfSChris Mason 			break;
2094543df7eSChris Mason 		case Opt_thread_pool:
2104543df7eSChris Mason 			intarg = 0;
2114543df7eSChris Mason 			match_int(&args[0], &intarg);
2124543df7eSChris Mason 			if (intarg) {
2134543df7eSChris Mason 				info->thread_pool_size = intarg;
2144543df7eSChris Mason 				printk(KERN_INFO "btrfs: thread pool %d\n",
2154543df7eSChris Mason 				       info->thread_pool_size);
2164543df7eSChris Mason 			}
2174543df7eSChris Mason 			break;
2186f568d35SChris Mason 		case Opt_max_inline:
219edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
2206f568d35SChris Mason 			if (num) {
22191748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
2226f568d35SChris Mason 				kfree(num);
2236f568d35SChris Mason 
22415ada040SChris Mason 				if (info->max_inline) {
2256f568d35SChris Mason 					info->max_inline = max_t(u64,
22615ada040SChris Mason 						info->max_inline,
22715ada040SChris Mason 						root->sectorsize);
22815ada040SChris Mason 				}
229edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: max_inline at %llu\n",
23021380931SJoel Becker 					(unsigned long long)info->max_inline);
2316f568d35SChris Mason 			}
2326f568d35SChris Mason 			break;
2338f662a76SChris Mason 		case Opt_alloc_start:
234edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
2358f662a76SChris Mason 			if (num) {
23691748467SAkinobu Mita 				info->alloc_start = memparse(num, NULL);
2378f662a76SChris Mason 				kfree(num);
238edf24abeSChristoph Hellwig 				printk(KERN_INFO
239edf24abeSChristoph Hellwig 					"btrfs: allocations start at %llu\n",
24021380931SJoel Becker 					(unsigned long long)info->alloc_start);
2418f662a76SChris Mason 			}
2428f662a76SChris Mason 			break;
24333268eafSJosef Bacik 		case Opt_noacl:
24433268eafSJosef Bacik 			root->fs_info->sb->s_flags &= ~MS_POSIXACL;
24533268eafSJosef Bacik 			break;
2463a5e1404SSage Weil 		case Opt_notreelog:
2473a5e1404SSage Weil 			printk(KERN_INFO "btrfs: disabling tree log\n");
2483a5e1404SSage Weil 			btrfs_set_opt(info->mount_opt, NOTREELOG);
2493a5e1404SSage Weil 			break;
250dccae999SSage Weil 		case Opt_flushoncommit:
251dccae999SSage Weil 			printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
252dccae999SSage Weil 			btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
253dccae999SSage Weil 			break;
25497e728d4SJosef Bacik 		case Opt_ratio:
25597e728d4SJosef Bacik 			intarg = 0;
25697e728d4SJosef Bacik 			match_int(&args[0], &intarg);
25797e728d4SJosef Bacik 			if (intarg) {
25897e728d4SJosef Bacik 				info->metadata_ratio = intarg;
25997e728d4SJosef Bacik 				printk(KERN_INFO "btrfs: metadata ratio %d\n",
26097e728d4SJosef Bacik 				       info->metadata_ratio);
26197e728d4SJosef Bacik 			}
26297e728d4SJosef Bacik 			break;
263e244a0aeSChristoph Hellwig 		case Opt_discard:
264e244a0aeSChristoph Hellwig 			btrfs_set_opt(info->mount_opt, DISCARD);
265e244a0aeSChristoph Hellwig 			break;
2660af3d00bSJosef Bacik 		case Opt_space_cache:
2670af3d00bSJosef Bacik 			printk(KERN_INFO "btrfs: enabling disk space caching\n");
2680af3d00bSJosef Bacik 			btrfs_set_opt(info->mount_opt, SPACE_CACHE);
2690de90876SJosef Bacik 			break;
27088c2ba3bSJosef Bacik 		case Opt_clear_cache:
27188c2ba3bSJosef Bacik 			printk(KERN_INFO "btrfs: force clearing of disk cache\n");
27288c2ba3bSJosef Bacik 			btrfs_set_opt(info->mount_opt, CLEAR_CACHE);
2730af3d00bSJosef Bacik 			break;
2744260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
2754260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
2764260f7c7SSage Weil 			break;
277a7a3f7caSSage Weil 		case Opt_err:
278a7a3f7caSSage Weil 			printk(KERN_INFO "btrfs: unrecognized mount option "
279a7a3f7caSSage Weil 			       "'%s'\n", p);
280a7a3f7caSSage Weil 			ret = -EINVAL;
281a7a3f7caSSage Weil 			goto out;
28295e05289SChris Mason 		default:
283be20aa9dSChris Mason 			break;
28495e05289SChris Mason 		}
28595e05289SChris Mason 	}
286a7a3f7caSSage Weil out:
287da495eccSJosef Bacik 	kfree(orig);
288a7a3f7caSSage Weil 	return ret;
289edf24abeSChristoph Hellwig }
290edf24abeSChristoph Hellwig 
291edf24abeSChristoph Hellwig /*
292edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
293edf24abeSChristoph Hellwig  *
294edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
295edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
296edf24abeSChristoph Hellwig  */
29797288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags,
29873f73415SJosef Bacik 		void *holder, char **subvol_name, u64 *subvol_objectid,
29943e570b0SChristoph Hellwig 		struct btrfs_fs_devices **fs_devices)
300edf24abeSChristoph Hellwig {
301edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
302edf24abeSChristoph Hellwig 	char *opts, *p;
303edf24abeSChristoph Hellwig 	int error = 0;
30473f73415SJosef Bacik 	int intarg;
305edf24abeSChristoph Hellwig 
306edf24abeSChristoph Hellwig 	if (!options)
307edf24abeSChristoph Hellwig 		goto out;
308edf24abeSChristoph Hellwig 
309edf24abeSChristoph Hellwig 	/*
310edf24abeSChristoph Hellwig 	 * strsep changes the string, duplicate it because parse_options
311edf24abeSChristoph Hellwig 	 * gets called twice
312edf24abeSChristoph Hellwig 	 */
313edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
314edf24abeSChristoph Hellwig 	if (!opts)
315edf24abeSChristoph Hellwig 		return -ENOMEM;
316edf24abeSChristoph Hellwig 
317edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
318edf24abeSChristoph Hellwig 		int token;
319edf24abeSChristoph Hellwig 		if (!*p)
320edf24abeSChristoph Hellwig 			continue;
321edf24abeSChristoph Hellwig 
322edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
323edf24abeSChristoph Hellwig 		switch (token) {
324edf24abeSChristoph Hellwig 		case Opt_subvol:
325edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
326edf24abeSChristoph Hellwig 			break;
32773f73415SJosef Bacik 		case Opt_subvolid:
32873f73415SJosef Bacik 			intarg = 0;
3294849f01dSJosef Bacik 			error = match_int(&args[0], &intarg);
3304849f01dSJosef Bacik 			if (!error) {
3314849f01dSJosef Bacik 				/* we want the original fs_tree */
3324849f01dSJosef Bacik 				if (!intarg)
3334849f01dSJosef Bacik 					*subvol_objectid =
3344849f01dSJosef Bacik 						BTRFS_FS_TREE_OBJECTID;
3354849f01dSJosef Bacik 				else
33673f73415SJosef Bacik 					*subvol_objectid = intarg;
3374849f01dSJosef Bacik 			}
33873f73415SJosef Bacik 			break;
33943e570b0SChristoph Hellwig 		case Opt_device:
34043e570b0SChristoph Hellwig 			error = btrfs_scan_one_device(match_strdup(&args[0]),
34143e570b0SChristoph Hellwig 					flags, holder, fs_devices);
34243e570b0SChristoph Hellwig 			if (error)
34343e570b0SChristoph Hellwig 				goto out_free_opts;
34443e570b0SChristoph Hellwig 			break;
345edf24abeSChristoph Hellwig 		default:
346edf24abeSChristoph Hellwig 			break;
347edf24abeSChristoph Hellwig 		}
348edf24abeSChristoph Hellwig 	}
349edf24abeSChristoph Hellwig 
35043e570b0SChristoph Hellwig  out_free_opts:
351edf24abeSChristoph Hellwig 	kfree(opts);
352edf24abeSChristoph Hellwig  out:
353edf24abeSChristoph Hellwig 	/*
354edf24abeSChristoph Hellwig 	 * If no subvolume name is specified we use the default one.  Allocate
3553de4586cSChris Mason 	 * a copy of the string "." here so that code later in the
356edf24abeSChristoph Hellwig 	 * mount path doesn't care if it's the default volume or another one.
357edf24abeSChristoph Hellwig 	 */
358edf24abeSChristoph Hellwig 	if (!*subvol_name) {
3593de4586cSChris Mason 		*subvol_name = kstrdup(".", GFP_KERNEL);
360edf24abeSChristoph Hellwig 		if (!*subvol_name)
361edf24abeSChristoph Hellwig 			return -ENOMEM;
362edf24abeSChristoph Hellwig 	}
363edf24abeSChristoph Hellwig 	return error;
36495e05289SChris Mason }
36595e05289SChris Mason 
36673f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb,
36773f73415SJosef Bacik 				       u64 subvol_objectid)
36873f73415SJosef Bacik {
36973f73415SJosef Bacik 	struct btrfs_root *root = sb->s_fs_info;
37073f73415SJosef Bacik 	struct btrfs_root *new_root;
37173f73415SJosef Bacik 	struct btrfs_dir_item *di;
37273f73415SJosef Bacik 	struct btrfs_path *path;
37373f73415SJosef Bacik 	struct btrfs_key location;
37473f73415SJosef Bacik 	struct inode *inode;
37573f73415SJosef Bacik 	struct dentry *dentry;
37673f73415SJosef Bacik 	u64 dir_id;
37773f73415SJosef Bacik 	int new = 0;
37873f73415SJosef Bacik 
37973f73415SJosef Bacik 	/*
38073f73415SJosef Bacik 	 * We have a specific subvol we want to mount, just setup location and
38173f73415SJosef Bacik 	 * go look up the root.
38273f73415SJosef Bacik 	 */
38373f73415SJosef Bacik 	if (subvol_objectid) {
38473f73415SJosef Bacik 		location.objectid = subvol_objectid;
38573f73415SJosef Bacik 		location.type = BTRFS_ROOT_ITEM_KEY;
38673f73415SJosef Bacik 		location.offset = (u64)-1;
38773f73415SJosef Bacik 		goto find_root;
38873f73415SJosef Bacik 	}
38973f73415SJosef Bacik 
39073f73415SJosef Bacik 	path = btrfs_alloc_path();
39173f73415SJosef Bacik 	if (!path)
39273f73415SJosef Bacik 		return ERR_PTR(-ENOMEM);
39373f73415SJosef Bacik 	path->leave_spinning = 1;
39473f73415SJosef Bacik 
39573f73415SJosef Bacik 	/*
39673f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
39773f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
39873f73415SJosef Bacik 	 * to mount.
39973f73415SJosef Bacik 	 */
40073f73415SJosef Bacik 	dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
40173f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
402fb4f6f91SDan Carpenter 	if (IS_ERR(di))
403fb4f6f91SDan Carpenter 		return ERR_CAST(di);
40473f73415SJosef Bacik 	if (!di) {
40573f73415SJosef Bacik 		/*
40673f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
40773f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
40873f73415SJosef Bacik 		 * mount to root most subvolume.
40973f73415SJosef Bacik 		 */
41073f73415SJosef Bacik 		btrfs_free_path(path);
41173f73415SJosef Bacik 		dir_id = BTRFS_FIRST_FREE_OBJECTID;
41273f73415SJosef Bacik 		new_root = root->fs_info->fs_root;
41373f73415SJosef Bacik 		goto setup_root;
41473f73415SJosef Bacik 	}
41573f73415SJosef Bacik 
41673f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
41773f73415SJosef Bacik 	btrfs_free_path(path);
41873f73415SJosef Bacik 
41973f73415SJosef Bacik find_root:
42073f73415SJosef Bacik 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
42173f73415SJosef Bacik 	if (IS_ERR(new_root))
422d0b678cbSJulia Lawall 		return ERR_CAST(new_root);
42373f73415SJosef Bacik 
42473f73415SJosef Bacik 	if (btrfs_root_refs(&new_root->root_item) == 0)
42573f73415SJosef Bacik 		return ERR_PTR(-ENOENT);
42673f73415SJosef Bacik 
42773f73415SJosef Bacik 	dir_id = btrfs_root_dirid(&new_root->root_item);
42873f73415SJosef Bacik setup_root:
42973f73415SJosef Bacik 	location.objectid = dir_id;
43073f73415SJosef Bacik 	location.type = BTRFS_INODE_ITEM_KEY;
43173f73415SJosef Bacik 	location.offset = 0;
43273f73415SJosef Bacik 
43373f73415SJosef Bacik 	inode = btrfs_iget(sb, &location, new_root, &new);
4344cbd1149SDan Carpenter 	if (IS_ERR(inode))
4354cbd1149SDan Carpenter 		return ERR_CAST(inode);
43673f73415SJosef Bacik 
43773f73415SJosef Bacik 	/*
43873f73415SJosef Bacik 	 * If we're just mounting the root most subvol put the inode and return
43973f73415SJosef Bacik 	 * a reference to the dentry.  We will have already gotten a reference
44073f73415SJosef Bacik 	 * to the inode in btrfs_fill_super so we're good to go.
44173f73415SJosef Bacik 	 */
44273f73415SJosef Bacik 	if (!new && sb->s_root->d_inode == inode) {
44373f73415SJosef Bacik 		iput(inode);
44473f73415SJosef Bacik 		return dget(sb->s_root);
44573f73415SJosef Bacik 	}
44673f73415SJosef Bacik 
44773f73415SJosef Bacik 	if (new) {
44873f73415SJosef Bacik 		const struct qstr name = { .name = "/", .len = 1 };
44973f73415SJosef Bacik 
45073f73415SJosef Bacik 		/*
45173f73415SJosef Bacik 		 * New inode, we need to make the dentry a sibling of s_root so
45273f73415SJosef Bacik 		 * everything gets cleaned up properly on unmount.
45373f73415SJosef Bacik 		 */
45473f73415SJosef Bacik 		dentry = d_alloc(sb->s_root, &name);
45573f73415SJosef Bacik 		if (!dentry) {
45673f73415SJosef Bacik 			iput(inode);
45773f73415SJosef Bacik 			return ERR_PTR(-ENOMEM);
45873f73415SJosef Bacik 		}
45973f73415SJosef Bacik 		d_splice_alias(inode, dentry);
46073f73415SJosef Bacik 	} else {
46173f73415SJosef Bacik 		/*
46273f73415SJosef Bacik 		 * We found the inode in cache, just find a dentry for it and
46373f73415SJosef Bacik 		 * put the reference to the inode we just got.
46473f73415SJosef Bacik 		 */
46573f73415SJosef Bacik 		dentry = d_find_alias(inode);
46673f73415SJosef Bacik 		iput(inode);
46773f73415SJosef Bacik 	}
46873f73415SJosef Bacik 
46973f73415SJosef Bacik 	return dentry;
47073f73415SJosef Bacik }
47173f73415SJosef Bacik 
4728a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
4738a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
4748a4b83ccSChris Mason 			    void *data, int silent)
4752e635a27SChris Mason {
4762e635a27SChris Mason 	struct inode *inode;
477e20d96d6SChris Mason 	struct dentry *root_dentry;
4780f7d52f4SChris Mason 	struct btrfs_root *tree_root;
4795d4f98a2SYan Zheng 	struct btrfs_key key;
48039279cc3SChris Mason 	int err;
4812e635a27SChris Mason 
4822e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
4832e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
484e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
485be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
4865103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
4872e635a27SChris Mason 	sb->s_time_gran = 1;
4880eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
48933268eafSJosef Bacik 	sb->s_flags |= MS_POSIXACL;
49049cf6f45SChris Ball #endif
491e20d96d6SChris Mason 
492dfe25020SChris Mason 	tree_root = open_ctree(sb, fs_devices, (char *)data);
493d98237b3SChris Mason 
494e58ca020SYan 	if (IS_ERR(tree_root)) {
495e20d96d6SChris Mason 		printk("btrfs: open_ctree failed\n");
496e58ca020SYan 		return PTR_ERR(tree_root);
497e20d96d6SChris Mason 	}
4980f7d52f4SChris Mason 	sb->s_fs_info = tree_root;
499b888db2bSChris Mason 
5005d4f98a2SYan Zheng 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
5015d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
5025d4f98a2SYan Zheng 	key.offset = 0;
50373f73415SJosef Bacik 	inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
5045d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
5055d4f98a2SYan Zheng 		err = PTR_ERR(inode);
50639279cc3SChris Mason 		goto fail_close;
50739279cc3SChris Mason 	}
5082e635a27SChris Mason 
509e20d96d6SChris Mason 	root_dentry = d_alloc_root(inode);
510e20d96d6SChris Mason 	if (!root_dentry) {
5112e635a27SChris Mason 		iput(inode);
51239279cc3SChris Mason 		err = -ENOMEM;
51339279cc3SChris Mason 		goto fail_close;
5142e635a27SChris Mason 	}
51558176a96SJosef Bacik 
516e20d96d6SChris Mason 	sb->s_root = root_dentry;
5176885f308SChris Mason 
5186885f308SChris Mason 	save_mount_options(sb, data);
5192e635a27SChris Mason 	return 0;
5202e635a27SChris Mason 
52139279cc3SChris Mason fail_close:
52239279cc3SChris Mason 	close_ctree(tree_root);
523d5719762SChris Mason 	return err;
524d5719762SChris Mason }
525d5719762SChris Mason 
5266bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
527d5719762SChris Mason {
528d5719762SChris Mason 	struct btrfs_trans_handle *trans;
529dccae999SSage Weil 	struct btrfs_root *root = btrfs_sb(sb);
530d5719762SChris Mason 	int ret;
531df2ce34cSChris Mason 
532d561c025SChris Mason 	if (!wait) {
5337cfcc17eSChris Mason 		filemap_flush(root->fs_info->btree_inode->i_mapping);
534df2ce34cSChris Mason 		return 0;
535d561c025SChris Mason 	}
536771ed689SChris Mason 
53724bbcf04SYan, Zheng 	btrfs_start_delalloc_inodes(root, 0);
53824bbcf04SYan, Zheng 	btrfs_wait_ordered_extents(root, 0, 0);
539771ed689SChris Mason 
540a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 0);
541d5719762SChris Mason 	ret = btrfs_commit_transaction(trans, root);
54254aa1f4dSChris Mason 	return ret;
543d5719762SChris Mason }
544d5719762SChris Mason 
545a9572a15SEric Paris static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
546a9572a15SEric Paris {
547a9572a15SEric Paris 	struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
548a9572a15SEric Paris 	struct btrfs_fs_info *info = root->fs_info;
549a9572a15SEric Paris 
550a9572a15SEric Paris 	if (btrfs_test_opt(root, DEGRADED))
551a9572a15SEric Paris 		seq_puts(seq, ",degraded");
552a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATASUM))
553a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
554a9572a15SEric Paris 	if (btrfs_test_opt(root, NODATACOW))
555a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
556a9572a15SEric Paris 	if (btrfs_test_opt(root, NOBARRIER))
557a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
558a9572a15SEric Paris 	if (info->max_inline != 8192 * 1024)
55921380931SJoel Becker 		seq_printf(seq, ",max_inline=%llu",
56021380931SJoel Becker 			   (unsigned long long)info->max_inline);
561a9572a15SEric Paris 	if (info->alloc_start != 0)
56221380931SJoel Becker 		seq_printf(seq, ",alloc_start=%llu",
56321380931SJoel Becker 			   (unsigned long long)info->alloc_start);
564a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
565a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
566a9572a15SEric Paris 		seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
567a9572a15SEric Paris 	if (btrfs_test_opt(root, COMPRESS))
568a9572a15SEric Paris 		seq_puts(seq, ",compress");
569c289811cSChris Mason 	if (btrfs_test_opt(root, NOSSD))
570c289811cSChris Mason 		seq_puts(seq, ",nossd");
571451d7585SChris Mason 	if (btrfs_test_opt(root, SSD_SPREAD))
572451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
573451d7585SChris Mason 	else if (btrfs_test_opt(root, SSD))
574a9572a15SEric Paris 		seq_puts(seq, ",ssd");
5753a5e1404SSage Weil 	if (btrfs_test_opt(root, NOTREELOG))
5766b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
577dccae999SSage Weil 	if (btrfs_test_opt(root, FLUSHONCOMMIT))
5786b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
57920a5239aSMatthew Wilcox 	if (btrfs_test_opt(root, DISCARD))
58020a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
581a9572a15SEric Paris 	if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
582a9572a15SEric Paris 		seq_puts(seq, ",noacl");
583a9572a15SEric Paris 	return 0;
584a9572a15SEric Paris }
585a9572a15SEric Paris 
586a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
5872e635a27SChris Mason {
588450ba0eaSJosef Bacik 	struct btrfs_root *test_root = data;
589a061fc8dSChris Mason 	struct btrfs_root *root = btrfs_sb(s);
5904b82d6e4SYan 
591619c8c76SIan Kent 	/*
592619c8c76SIan Kent 	 * If this super block is going away, return false as it
593619c8c76SIan Kent 	 * can't match as an existing super block.
594619c8c76SIan Kent 	 */
595619c8c76SIan Kent 	if (!atomic_read(&s->s_active))
596619c8c76SIan Kent 		return 0;
597450ba0eaSJosef Bacik 	return root->fs_info->fs_devices == test_root->fs_info->fs_devices;
5984b82d6e4SYan }
5994b82d6e4SYan 
600450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
601450ba0eaSJosef Bacik {
602450ba0eaSJosef Bacik 	s->s_fs_info = data;
603450ba0eaSJosef Bacik 
604450ba0eaSJosef Bacik 	return set_anon_super(s, data);
605450ba0eaSJosef Bacik }
606450ba0eaSJosef Bacik 
607450ba0eaSJosef Bacik 
608edf24abeSChristoph Hellwig /*
609edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
610edf24abeSChristoph Hellwig  *
611edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
612edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
613edf24abeSChristoph Hellwig  */
614edf24abeSChristoph Hellwig static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
615edf24abeSChristoph Hellwig 		const char *dev_name, void *data, struct vfsmount *mnt)
6164b82d6e4SYan {
6174b82d6e4SYan 	struct block_device *bdev = NULL;
6184b82d6e4SYan 	struct super_block *s;
6194b82d6e4SYan 	struct dentry *root;
6208a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
621450ba0eaSJosef Bacik 	struct btrfs_root *tree_root = NULL;
622450ba0eaSJosef Bacik 	struct btrfs_fs_info *fs_info = NULL;
62397288f2cSChristoph Hellwig 	fmode_t mode = FMODE_READ;
62473f73415SJosef Bacik 	char *subvol_name = NULL;
62573f73415SJosef Bacik 	u64 subvol_objectid = 0;
6264b82d6e4SYan 	int error = 0;
6274b82d6e4SYan 
62897288f2cSChristoph Hellwig 	if (!(flags & MS_RDONLY))
62997288f2cSChristoph Hellwig 		mode |= FMODE_WRITE;
63097288f2cSChristoph Hellwig 
63197288f2cSChristoph Hellwig 	error = btrfs_parse_early_options(data, mode, fs_type,
63273f73415SJosef Bacik 					  &subvol_name, &subvol_objectid,
63373f73415SJosef Bacik 					  &fs_devices);
634edf24abeSChristoph Hellwig 	if (error)
6351f483660SShen Feng 		return error;
636edf24abeSChristoph Hellwig 
63797288f2cSChristoph Hellwig 	error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices);
6388a4b83ccSChris Mason 	if (error)
639edf24abeSChristoph Hellwig 		goto error_free_subvol_name;
6404b82d6e4SYan 
64197288f2cSChristoph Hellwig 	error = btrfs_open_devices(fs_devices, mode, fs_type);
6428a4b83ccSChris Mason 	if (error)
643edf24abeSChristoph Hellwig 		goto error_free_subvol_name;
6448a4b83ccSChris Mason 
6452b82032cSYan Zheng 	if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
6462b82032cSYan Zheng 		error = -EACCES;
6472b82032cSYan Zheng 		goto error_close_devices;
6482b82032cSYan Zheng 	}
6492b82032cSYan Zheng 
650450ba0eaSJosef Bacik 	/*
651450ba0eaSJosef Bacik 	 * Setup a dummy root and fs_info for test/set super.  This is because
652450ba0eaSJosef Bacik 	 * we don't actually fill this stuff out until open_ctree, but we need
653450ba0eaSJosef Bacik 	 * it for searching for existing supers, so this lets us do that and
654450ba0eaSJosef Bacik 	 * then open_ctree will properly initialize everything later.
655450ba0eaSJosef Bacik 	 */
656450ba0eaSJosef Bacik 	fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
657450ba0eaSJosef Bacik 	tree_root = kzalloc(sizeof(struct btrfs_root), GFP_NOFS);
658450ba0eaSJosef Bacik 	if (!fs_info || !tree_root) {
659450ba0eaSJosef Bacik 		error = -ENOMEM;
660450ba0eaSJosef Bacik 		goto error_close_devices;
661450ba0eaSJosef Bacik 	}
662450ba0eaSJosef Bacik 	fs_info->tree_root = tree_root;
663450ba0eaSJosef Bacik 	fs_info->fs_devices = fs_devices;
664450ba0eaSJosef Bacik 	tree_root->fs_info = fs_info;
665450ba0eaSJosef Bacik 
666dfe25020SChris Mason 	bdev = fs_devices->latest_bdev;
667450ba0eaSJosef Bacik 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, tree_root);
6684b82d6e4SYan 	if (IS_ERR(s))
6694b82d6e4SYan 		goto error_s;
6704b82d6e4SYan 
6714b82d6e4SYan 	if (s->s_root) {
6724b82d6e4SYan 		if ((flags ^ s->s_flags) & MS_RDONLY) {
6736f5bbff9SAl Viro 			deactivate_locked_super(s);
6744b82d6e4SYan 			error = -EBUSY;
675c146afadSYan Zheng 			goto error_close_devices;
6764b82d6e4SYan 		}
6774b82d6e4SYan 
6782b82032cSYan Zheng 		btrfs_close_devices(fs_devices);
6794b82d6e4SYan 	} else {
6804b82d6e4SYan 		char b[BDEVNAME_SIZE];
6814b82d6e4SYan 
6824b82d6e4SYan 		s->s_flags = flags;
6834b82d6e4SYan 		strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
6848a4b83ccSChris Mason 		error = btrfs_fill_super(s, fs_devices, data,
6858a4b83ccSChris Mason 					 flags & MS_SILENT ? 1 : 0);
6864b82d6e4SYan 		if (error) {
6876f5bbff9SAl Viro 			deactivate_locked_super(s);
6881f483660SShen Feng 			goto error_free_subvol_name;
6894b82d6e4SYan 		}
6904b82d6e4SYan 
691788f20ebSChris Mason 		btrfs_sb(s)->fs_info->bdev_holder = fs_type;
6924b82d6e4SYan 		s->s_flags |= MS_ACTIVE;
6934b82d6e4SYan 	}
6944b82d6e4SYan 
69573f73415SJosef Bacik 	root = get_default_root(s, subvol_objectid);
6964b82d6e4SYan 	if (IS_ERR(root)) {
6974b82d6e4SYan 		error = PTR_ERR(root);
69873f73415SJosef Bacik 		deactivate_locked_super(s);
6990e78340fSJosef Bacik 		goto error_free_subvol_name;
7004b82d6e4SYan 	}
70173f73415SJosef Bacik 	/* if they gave us a subvolume name bind mount into that */
70273f73415SJosef Bacik 	if (strcmp(subvol_name, ".")) {
70373f73415SJosef Bacik 		struct dentry *new_root;
70473f73415SJosef Bacik 		mutex_lock(&root->d_inode->i_mutex);
70573f73415SJosef Bacik 		new_root = lookup_one_len(subvol_name, root,
70673f73415SJosef Bacik 				      strlen(subvol_name));
70773f73415SJosef Bacik 		mutex_unlock(&root->d_inode->i_mutex);
70873f73415SJosef Bacik 
70973f73415SJosef Bacik 		if (IS_ERR(new_root)) {
710f106e82cSLi Zefan 			dput(root);
71173f73415SJosef Bacik 			deactivate_locked_super(s);
71273f73415SJosef Bacik 			error = PTR_ERR(new_root);
7130e78340fSJosef Bacik 			goto error_free_subvol_name;
71473f73415SJosef Bacik 		}
71573f73415SJosef Bacik 		if (!new_root->d_inode) {
71673f73415SJosef Bacik 			dput(root);
71773f73415SJosef Bacik 			dput(new_root);
7186f5bbff9SAl Viro 			deactivate_locked_super(s);
7194b82d6e4SYan 			error = -ENXIO;
7200e78340fSJosef Bacik 			goto error_free_subvol_name;
7214b82d6e4SYan 		}
72273f73415SJosef Bacik 		dput(root);
72373f73415SJosef Bacik 		root = new_root;
72476fcef19SDavid Woodhouse 	}
7254b82d6e4SYan 
7264b82d6e4SYan 	mnt->mnt_sb = s;
7274b82d6e4SYan 	mnt->mnt_root = root;
728edf24abeSChristoph Hellwig 
729edf24abeSChristoph Hellwig 	kfree(subvol_name);
7304b82d6e4SYan 	return 0;
7314b82d6e4SYan 
7324b82d6e4SYan error_s:
7334b82d6e4SYan 	error = PTR_ERR(s);
734c146afadSYan Zheng error_close_devices:
7358a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
736450ba0eaSJosef Bacik 	kfree(fs_info);
737450ba0eaSJosef Bacik 	kfree(tree_root);
738edf24abeSChristoph Hellwig error_free_subvol_name:
739edf24abeSChristoph Hellwig 	kfree(subvol_name);
7404b82d6e4SYan 	return error;
7414b82d6e4SYan }
7422e635a27SChris Mason 
743c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
744c146afadSYan Zheng {
745c146afadSYan Zheng 	struct btrfs_root *root = btrfs_sb(sb);
746c146afadSYan Zheng 	int ret;
747c146afadSYan Zheng 
748b288052eSChris Mason 	ret = btrfs_parse_options(root, data);
749b288052eSChris Mason 	if (ret)
750b288052eSChris Mason 		return -EINVAL;
751b288052eSChris Mason 
752c146afadSYan Zheng 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
753c146afadSYan Zheng 		return 0;
754c146afadSYan Zheng 
755c146afadSYan Zheng 	if (*flags & MS_RDONLY) {
756c146afadSYan Zheng 		sb->s_flags |= MS_RDONLY;
757c146afadSYan Zheng 
758c146afadSYan Zheng 		ret =  btrfs_commit_super(root);
759c146afadSYan Zheng 		WARN_ON(ret);
760c146afadSYan Zheng 	} else {
7612b82032cSYan Zheng 		if (root->fs_info->fs_devices->rw_devices == 0)
7622b82032cSYan Zheng 			return -EACCES;
7632b82032cSYan Zheng 
764c146afadSYan Zheng 		if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
765c146afadSYan Zheng 			return -EINVAL;
766c146afadSYan Zheng 
767d68fc57bSYan, Zheng 		ret = btrfs_cleanup_fs_roots(root->fs_info);
768c146afadSYan Zheng 		WARN_ON(ret);
769c146afadSYan Zheng 
770d68fc57bSYan, Zheng 		/* recover relocation */
771d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
772c146afadSYan Zheng 		WARN_ON(ret);
773c146afadSYan Zheng 
774c146afadSYan Zheng 		sb->s_flags &= ~MS_RDONLY;
775c146afadSYan Zheng 	}
776c146afadSYan Zheng 
777c146afadSYan Zheng 	return 0;
778c146afadSYan Zheng }
779c146afadSYan Zheng 
780*6d07bcecSMiao Xie /*
781*6d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
782*6d07bcecSMiao Xie  * file data.
783*6d07bcecSMiao Xie  */
784*6d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
785*6d07bcecSMiao Xie {
786*6d07bcecSMiao Xie 	struct btrfs_fs_info *fs_info = root->fs_info;
787*6d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
788*6d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
789*6d07bcecSMiao Xie 	struct btrfs_device *device;
790*6d07bcecSMiao Xie 	u64 skip_space;
791*6d07bcecSMiao Xie 	u64 type;
792*6d07bcecSMiao Xie 	u64 avail_space;
793*6d07bcecSMiao Xie 	u64 used_space;
794*6d07bcecSMiao Xie 	u64 min_stripe_size;
795*6d07bcecSMiao Xie 	int min_stripes = 1;
796*6d07bcecSMiao Xie 	int i = 0, nr_devices;
797*6d07bcecSMiao Xie 	int ret;
798*6d07bcecSMiao Xie 
799*6d07bcecSMiao Xie 	nr_devices = fs_info->fs_devices->rw_devices;
800*6d07bcecSMiao Xie 	BUG_ON(!nr_devices);
801*6d07bcecSMiao Xie 
802*6d07bcecSMiao Xie 	devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
803*6d07bcecSMiao Xie 			       GFP_NOFS);
804*6d07bcecSMiao Xie 	if (!devices_info)
805*6d07bcecSMiao Xie 		return -ENOMEM;
806*6d07bcecSMiao Xie 
807*6d07bcecSMiao Xie 	/* calc min stripe number for data space alloction */
808*6d07bcecSMiao Xie 	type = btrfs_get_alloc_profile(root, 1);
809*6d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_RAID0)
810*6d07bcecSMiao Xie 		min_stripes = 2;
811*6d07bcecSMiao Xie 	else if (type & BTRFS_BLOCK_GROUP_RAID1)
812*6d07bcecSMiao Xie 		min_stripes = 2;
813*6d07bcecSMiao Xie 	else if (type & BTRFS_BLOCK_GROUP_RAID10)
814*6d07bcecSMiao Xie 		min_stripes = 4;
815*6d07bcecSMiao Xie 
816*6d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_DUP)
817*6d07bcecSMiao Xie 		min_stripe_size = 2 * BTRFS_STRIPE_LEN;
818*6d07bcecSMiao Xie 	else
819*6d07bcecSMiao Xie 		min_stripe_size = BTRFS_STRIPE_LEN;
820*6d07bcecSMiao Xie 
821*6d07bcecSMiao Xie 	list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
822*6d07bcecSMiao Xie 		if (!device->in_fs_metadata)
823*6d07bcecSMiao Xie 			continue;
824*6d07bcecSMiao Xie 
825*6d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
826*6d07bcecSMiao Xie 
827*6d07bcecSMiao Xie 		/* align with stripe_len */
828*6d07bcecSMiao Xie 		do_div(avail_space, BTRFS_STRIPE_LEN);
829*6d07bcecSMiao Xie 		avail_space *= BTRFS_STRIPE_LEN;
830*6d07bcecSMiao Xie 
831*6d07bcecSMiao Xie 		/*
832*6d07bcecSMiao Xie 		 * In order to avoid overwritting the superblock on the drive,
833*6d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
834*6d07bcecSMiao Xie 		 * allocation.
835*6d07bcecSMiao Xie 		 */
836*6d07bcecSMiao Xie 		skip_space = 1024 * 1024;
837*6d07bcecSMiao Xie 
838*6d07bcecSMiao Xie 		/* user can set the offset in fs_info->alloc_start. */
839*6d07bcecSMiao Xie 		if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
840*6d07bcecSMiao Xie 		    device->total_bytes)
841*6d07bcecSMiao Xie 			skip_space = max(fs_info->alloc_start, skip_space);
842*6d07bcecSMiao Xie 
843*6d07bcecSMiao Xie 		/*
844*6d07bcecSMiao Xie 		 * btrfs can not use the free space in [0, skip_space - 1],
845*6d07bcecSMiao Xie 		 * we must subtract it from the total. In order to implement
846*6d07bcecSMiao Xie 		 * it, we account the used space in this range first.
847*6d07bcecSMiao Xie 		 */
848*6d07bcecSMiao Xie 		ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
849*6d07bcecSMiao Xie 						     &used_space);
850*6d07bcecSMiao Xie 		if (ret) {
851*6d07bcecSMiao Xie 			kfree(devices_info);
852*6d07bcecSMiao Xie 			return ret;
853*6d07bcecSMiao Xie 		}
854*6d07bcecSMiao Xie 
855*6d07bcecSMiao Xie 		/* calc the free space in [0, skip_space - 1] */
856*6d07bcecSMiao Xie 		skip_space -= used_space;
857*6d07bcecSMiao Xie 
858*6d07bcecSMiao Xie 		/*
859*6d07bcecSMiao Xie 		 * we can use the free space in [0, skip_space - 1], subtract
860*6d07bcecSMiao Xie 		 * it from the total.
861*6d07bcecSMiao Xie 		 */
862*6d07bcecSMiao Xie 		if (avail_space && avail_space >= skip_space)
863*6d07bcecSMiao Xie 			avail_space -= skip_space;
864*6d07bcecSMiao Xie 		else
865*6d07bcecSMiao Xie 			avail_space = 0;
866*6d07bcecSMiao Xie 
867*6d07bcecSMiao Xie 		if (avail_space < min_stripe_size)
868*6d07bcecSMiao Xie 			continue;
869*6d07bcecSMiao Xie 
870*6d07bcecSMiao Xie 		devices_info[i].dev = device;
871*6d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
872*6d07bcecSMiao Xie 
873*6d07bcecSMiao Xie 		i++;
874*6d07bcecSMiao Xie 	}
875*6d07bcecSMiao Xie 
876*6d07bcecSMiao Xie 	nr_devices = i;
877*6d07bcecSMiao Xie 
878*6d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
879*6d07bcecSMiao Xie 
880*6d07bcecSMiao Xie 	i = nr_devices - 1;
881*6d07bcecSMiao Xie 	avail_space = 0;
882*6d07bcecSMiao Xie 	while (nr_devices >= min_stripes) {
883*6d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
884*6d07bcecSMiao Xie 			int j;
885*6d07bcecSMiao Xie 			u64 alloc_size;
886*6d07bcecSMiao Xie 
887*6d07bcecSMiao Xie 			avail_space += devices_info[i].max_avail * min_stripes;
888*6d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
889*6d07bcecSMiao Xie 			for (j = i + 1 - min_stripes; j <= i; j++)
890*6d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
891*6d07bcecSMiao Xie 		}
892*6d07bcecSMiao Xie 		i--;
893*6d07bcecSMiao Xie 		nr_devices--;
894*6d07bcecSMiao Xie 	}
895*6d07bcecSMiao Xie 
896*6d07bcecSMiao Xie 	kfree(devices_info);
897*6d07bcecSMiao Xie 	*free_bytes = avail_space;
898*6d07bcecSMiao Xie 	return 0;
899*6d07bcecSMiao Xie }
900*6d07bcecSMiao Xie 
9018fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
9028fd17795SChris Mason {
9038fd17795SChris Mason 	struct btrfs_root *root = btrfs_sb(dentry->d_sb);
9044b52dff6SChris Mason 	struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
905bd4d1088SJosef Bacik 	struct list_head *head = &root->fs_info->space_info;
906bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
907bd4d1088SJosef Bacik 	u64 total_used = 0;
908*6d07bcecSMiao Xie 	u64 total_free_data = 0;
909db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
9109d03632eSDavid Woodhouse 	__be32 *fsid = (__be32 *)root->fs_info->fsid;
911*6d07bcecSMiao Xie 	int ret;
9128fd17795SChris Mason 
913*6d07bcecSMiao Xie 	/* holding chunk_muext to avoid allocating new chunks */
914*6d07bcecSMiao Xie 	mutex_lock(&root->fs_info->chunk_mutex);
915bd4d1088SJosef Bacik 	rcu_read_lock();
91689a55897SJosef Bacik 	list_for_each_entry_rcu(found, head, list) {
917*6d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
918*6d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
919*6d07bcecSMiao Xie 			total_free_data -=
920*6d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
921*6d07bcecSMiao Xie 		}
922*6d07bcecSMiao Xie 
923b742bb82SYan, Zheng 		total_used += found->disk_used;
92489a55897SJosef Bacik 	}
925bd4d1088SJosef Bacik 	rcu_read_unlock();
926bd4d1088SJosef Bacik 
9278fd17795SChris Mason 	buf->f_namelen = BTRFS_NAME_LEN;
928db94535dSChris Mason 	buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
929bd4d1088SJosef Bacik 	buf->f_bfree = buf->f_blocks - (total_used >> bits);
9308fd17795SChris Mason 	buf->f_bsize = dentry->d_sb->s_blocksize;
9318fd17795SChris Mason 	buf->f_type = BTRFS_SUPER_MAGIC;
932*6d07bcecSMiao Xie 	buf->f_bavail = total_free_data;
933*6d07bcecSMiao Xie 	ret = btrfs_calc_avail_data_space(root, &total_free_data);
934*6d07bcecSMiao Xie 	if (ret) {
935*6d07bcecSMiao Xie 		mutex_unlock(&root->fs_info->chunk_mutex);
936*6d07bcecSMiao Xie 		return ret;
937*6d07bcecSMiao Xie 	}
938*6d07bcecSMiao Xie 	buf->f_bavail += total_free_data;
939*6d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
940*6d07bcecSMiao Xie 	mutex_unlock(&root->fs_info->chunk_mutex);
941d397712bSChris Mason 
9429d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
9439d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
9449d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
9459d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
9469d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
94732d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
94832d48fa1SDavid Woodhouse 	buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
94932d48fa1SDavid Woodhouse 	buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
95032d48fa1SDavid Woodhouse 
9518fd17795SChris Mason 	return 0;
9528fd17795SChris Mason }
953b5133862SChris Mason 
9542e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
9552e635a27SChris Mason 	.owner		= THIS_MODULE,
9562e635a27SChris Mason 	.name		= "btrfs",
9572e635a27SChris Mason 	.get_sb		= btrfs_get_sb,
958a061fc8dSChris Mason 	.kill_sb	= kill_anon_super,
9592e635a27SChris Mason 	.fs_flags	= FS_REQUIRES_DEV,
9602e635a27SChris Mason };
961a9218f6bSChris Mason 
962d352ac68SChris Mason /*
963d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
964d352ac68SChris Mason  */
9658a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
9668a4b83ccSChris Mason 				unsigned long arg)
9678a4b83ccSChris Mason {
9688a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
9698a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
970c071fcfdSChris Mason 	int ret = -ENOTTY;
9718a4b83ccSChris Mason 
972e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
973e441d54dSChris Mason 		return -EPERM;
974e441d54dSChris Mason 
975dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
976dae7b665SLi Zefan 	if (IS_ERR(vol))
977dae7b665SLi Zefan 		return PTR_ERR(vol);
978c071fcfdSChris Mason 
9798a4b83ccSChris Mason 	switch (cmd) {
9808a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
98197288f2cSChristoph Hellwig 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
9828a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
9838a4b83ccSChris Mason 		break;
9848a4b83ccSChris Mason 	}
985dae7b665SLi Zefan 
9868a4b83ccSChris Mason 	kfree(vol);
987f819d837SLinda Knippers 	return ret;
9888a4b83ccSChris Mason }
9898a4b83ccSChris Mason 
9900176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
991ed0dab6bSYan {
992ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
993a74a4b97SChris Mason 	mutex_lock(&root->fs_info->transaction_kthread_mutex);
994a74a4b97SChris Mason 	mutex_lock(&root->fs_info->cleaner_mutex);
9950176260fSLinus Torvalds 	return 0;
996ed0dab6bSYan }
997ed0dab6bSYan 
9980176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb)
999ed0dab6bSYan {
1000ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
1001a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->cleaner_mutex);
1002a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->transaction_kthread_mutex);
10030176260fSLinus Torvalds 	return 0;
1004ed0dab6bSYan }
10052e635a27SChris Mason 
1006b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
100776dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
1008bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
1009e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
1010d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
1011a9572a15SEric Paris 	.show_options	= btrfs_show_options,
10124730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
1013b5133862SChris Mason 	.dirty_inode	= btrfs_dirty_inode,
10142c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
10152c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
10168fd17795SChris Mason 	.statfs		= btrfs_statfs,
1017c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
10180176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
10190176260fSLinus Torvalds 	.unfreeze_fs	= btrfs_unfreeze,
1020e20d96d6SChris Mason };
1021a9218f6bSChris Mason 
1022a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
1023a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
1024a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
1025a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
1026a9218f6bSChris Mason };
1027a9218f6bSChris Mason 
1028a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
1029578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
1030a9218f6bSChris Mason 	.name		= "btrfs-control",
1031a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
1032a9218f6bSChris Mason };
1033a9218f6bSChris Mason 
1034578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
1035578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
1036578454ffSKay Sievers 
1037a9218f6bSChris Mason static int btrfs_interface_init(void)
1038a9218f6bSChris Mason {
1039a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
1040a9218f6bSChris Mason }
1041a9218f6bSChris Mason 
1042b2950863SChristoph Hellwig static void btrfs_interface_exit(void)
1043a9218f6bSChris Mason {
1044a9218f6bSChris Mason 	if (misc_deregister(&btrfs_misc) < 0)
1045d397712bSChris Mason 		printk(KERN_INFO "misc_deregister failed for control device");
1046a9218f6bSChris Mason }
1047a9218f6bSChris Mason 
10482e635a27SChris Mason static int __init init_btrfs_fs(void)
10492e635a27SChris Mason {
10502c90e5d6SChris Mason 	int err;
105158176a96SJosef Bacik 
105258176a96SJosef Bacik 	err = btrfs_init_sysfs();
105358176a96SJosef Bacik 	if (err)
105458176a96SJosef Bacik 		return err;
105558176a96SJosef Bacik 
1056261507a0SLi Zefan 	err = btrfs_init_compress();
10572c90e5d6SChris Mason 	if (err)
1058a74a4b97SChris Mason 		goto free_sysfs;
1059d1310b2eSChris Mason 
1060261507a0SLi Zefan 	err = btrfs_init_cachep();
1061261507a0SLi Zefan 	if (err)
1062261507a0SLi Zefan 		goto free_compress;
1063261507a0SLi Zefan 
1064d1310b2eSChris Mason 	err = extent_io_init();
10652f4cbe64SWyatt Banks 	if (err)
10662f4cbe64SWyatt Banks 		goto free_cachep;
10672f4cbe64SWyatt Banks 
1068d1310b2eSChris Mason 	err = extent_map_init();
1069d1310b2eSChris Mason 	if (err)
1070d1310b2eSChris Mason 		goto free_extent_io;
1071d1310b2eSChris Mason 
1072a9218f6bSChris Mason 	err = btrfs_interface_init();
10732f4cbe64SWyatt Banks 	if (err)
10742f4cbe64SWyatt Banks 		goto free_extent_map;
1075c8b97818SChris Mason 
1076a9218f6bSChris Mason 	err = register_filesystem(&btrfs_fs_type);
1077a9218f6bSChris Mason 	if (err)
1078a9218f6bSChris Mason 		goto unregister_ioctl;
1079b3c3da71SChris Mason 
1080b3c3da71SChris Mason 	printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
10812f4cbe64SWyatt Banks 	return 0;
10822f4cbe64SWyatt Banks 
1083a9218f6bSChris Mason unregister_ioctl:
1084a9218f6bSChris Mason 	btrfs_interface_exit();
10852f4cbe64SWyatt Banks free_extent_map:
10862f4cbe64SWyatt Banks 	extent_map_exit();
1087d1310b2eSChris Mason free_extent_io:
1088d1310b2eSChris Mason 	extent_io_exit();
10892f4cbe64SWyatt Banks free_cachep:
10902f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
1091261507a0SLi Zefan free_compress:
1092261507a0SLi Zefan 	btrfs_exit_compress();
1093a74a4b97SChris Mason free_sysfs:
10942f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
10952c90e5d6SChris Mason 	return err;
10962e635a27SChris Mason }
10972e635a27SChris Mason 
10982e635a27SChris Mason static void __exit exit_btrfs_fs(void)
10992e635a27SChris Mason {
110039279cc3SChris Mason 	btrfs_destroy_cachep();
1101a52d9a80SChris Mason 	extent_map_exit();
1102d1310b2eSChris Mason 	extent_io_exit();
1103a9218f6bSChris Mason 	btrfs_interface_exit();
11042e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
110558176a96SJosef Bacik 	btrfs_exit_sysfs();
11068a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
1107261507a0SLi Zefan 	btrfs_exit_compress();
11082e635a27SChris Mason }
11092e635a27SChris Mason 
11102e635a27SChris Mason module_init(init_btrfs_fs)
11112e635a27SChris Mason module_exit(exit_btrfs_fs)
11122e635a27SChris Mason 
11132e635a27SChris Mason MODULE_LICENSE("GPL");
1114