xref: /linux/fs/btrfs/super.c (revision 3de4586c5278a28107030c336956381f69ff7a9d)
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>
272e635a27SChris Mason #include <linux/string.h>
282e635a27SChris Mason #include <linux/smp_lock.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>
402e635a27SChris Mason #include "ctree.h"
41e20d96d6SChris Mason #include "disk-io.h"
42d5719762SChris Mason #include "transaction.h"
432c90e5d6SChris Mason #include "btrfs_inode.h"
44c5739bbaSChris Mason #include "ioctl.h"
453a686375SChris Mason #include "print-tree.h"
465103e947SJosef Bacik #include "xattr.h"
478a4b83ccSChris Mason #include "volumes.h"
48b3c3da71SChris Mason #include "version.h"
49be6e8dc0SBalaji Rao #include "export.h"
50c8b97818SChris Mason #include "compression.h"
512e635a27SChris Mason 
525f39d397SChris Mason #define BTRFS_SUPER_MAGIC 0x9123683E
53e20d96d6SChris Mason 
54e20d96d6SChris Mason static struct super_operations btrfs_super_ops;
55e20d96d6SChris Mason 
56e20d96d6SChris Mason static void btrfs_put_super (struct super_block * sb)
57e20d96d6SChris Mason {
58e20d96d6SChris Mason 	struct btrfs_root *root = btrfs_sb(sb);
5958176a96SJosef Bacik 	struct btrfs_fs_info *fs = root->fs_info;
60e20d96d6SChris Mason 	int ret;
61e20d96d6SChris Mason 
62e20d96d6SChris Mason 	ret = close_ctree(root);
63e20d96d6SChris Mason 	if (ret) {
64e20d96d6SChris Mason 		printk("close ctree returns %d\n", ret);
65e20d96d6SChris Mason 	}
6658176a96SJosef Bacik 	btrfs_sysfs_del_super(fs);
67e20d96d6SChris Mason 	sb->s_fs_info = NULL;
68e20d96d6SChris Mason }
692e635a27SChris Mason 
7095e05289SChris Mason enum {
7143e570b0SChristoph Hellwig 	Opt_degraded, Opt_subvol, Opt_device, Opt_nodatasum, Opt_nodatacow,
72dfe25020SChris Mason 	Opt_max_extent, Opt_max_inline, Opt_alloc_start, Opt_nobarrier,
73c8b97818SChris Mason 	Opt_ssd, Opt_thread_pool, Opt_noacl,  Opt_compress, Opt_err,
7495e05289SChris Mason };
7595e05289SChris Mason 
7695e05289SChris Mason static match_table_t tokens = {
77dfe25020SChris Mason 	{Opt_degraded, "degraded"},
7895e05289SChris Mason 	{Opt_subvol, "subvol=%s"},
7943e570b0SChristoph Hellwig 	{Opt_device, "device=%s"},
80b6cda9bcSChris Mason 	{Opt_nodatasum, "nodatasum"},
81be20aa9dSChris Mason 	{Opt_nodatacow, "nodatacow"},
8221ad10cfSChris Mason 	{Opt_nobarrier, "nobarrier"},
83c59f8951SChris Mason 	{Opt_max_extent, "max_extent=%s"},
846f568d35SChris Mason 	{Opt_max_inline, "max_inline=%s"},
858f662a76SChris Mason 	{Opt_alloc_start, "alloc_start=%s"},
864543df7eSChris Mason 	{Opt_thread_pool, "thread_pool=%d"},
87c8b97818SChris Mason 	{Opt_compress, "compress"},
88e18e4809SChris Mason 	{Opt_ssd, "ssd"},
8933268eafSJosef Bacik 	{Opt_noacl, "noacl"},
9033268eafSJosef Bacik 	{Opt_err, NULL},
9195e05289SChris Mason };
9295e05289SChris Mason 
93edbd8d4eSChris Mason u64 btrfs_parse_size(char *str)
94c59f8951SChris Mason {
95edbd8d4eSChris Mason 	u64 res;
96c59f8951SChris Mason 	int mult = 1;
97c59f8951SChris Mason 	char *end;
98c59f8951SChris Mason 	char last;
99c59f8951SChris Mason 
100c59f8951SChris Mason 	res = simple_strtoul(str, &end, 10);
101c59f8951SChris Mason 
102c59f8951SChris Mason 	last = end[0];
103c59f8951SChris Mason 	if (isalpha(last)) {
104c59f8951SChris Mason 		last = tolower(last);
105c59f8951SChris Mason 		switch (last) {
106c59f8951SChris Mason 		case 'g':
107c59f8951SChris Mason 			mult *= 1024;
108c59f8951SChris Mason 		case 'm':
109c59f8951SChris Mason 			mult *= 1024;
110c59f8951SChris Mason 		case 'k':
111c59f8951SChris Mason 			mult *= 1024;
112c59f8951SChris Mason 		}
113c59f8951SChris Mason 		res = res * mult;
114c59f8951SChris Mason 	}
115c59f8951SChris Mason 	return res;
116c59f8951SChris Mason }
117c59f8951SChris Mason 
118edf24abeSChristoph Hellwig /*
119edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
120edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
121edf24abeSChristoph Hellwig  */
122edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options)
12395e05289SChris Mason {
124edf24abeSChristoph Hellwig 	struct btrfs_fs_info *info = root->fs_info;
12595e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
126edf24abeSChristoph Hellwig 	char *p, *num;
1274543df7eSChris Mason 	int intarg;
128b6cda9bcSChris Mason 
12995e05289SChris Mason 	if (!options)
130edf24abeSChristoph Hellwig 		return 0;
13195e05289SChris Mason 
132be20aa9dSChris Mason 	/*
133be20aa9dSChris Mason 	 * strsep changes the string, duplicate it because parse_options
134be20aa9dSChris Mason 	 * gets called twice
135be20aa9dSChris Mason 	 */
136be20aa9dSChris Mason 	options = kstrdup(options, GFP_NOFS);
137be20aa9dSChris Mason 	if (!options)
138be20aa9dSChris Mason 		return -ENOMEM;
139be20aa9dSChris Mason 
140be20aa9dSChris Mason 
14195e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
14295e05289SChris Mason 		int token;
14395e05289SChris Mason 		if (!*p)
14495e05289SChris Mason 			continue;
14595e05289SChris Mason 
14695e05289SChris Mason 		token = match_token(p, tokens, args);
14795e05289SChris Mason 		switch (token) {
148dfe25020SChris Mason 		case Opt_degraded:
149edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: allowing degraded mounts\n");
150dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
151dfe25020SChris Mason 			break;
15295e05289SChris Mason 		case Opt_subvol:
15343e570b0SChristoph Hellwig 		case Opt_device:
154edf24abeSChristoph Hellwig 			/*
15543e570b0SChristoph Hellwig 			 * These are parsed by btrfs_parse_early_options
156edf24abeSChristoph Hellwig 			 * and can be happily ignored here.
157edf24abeSChristoph Hellwig 			 */
15895e05289SChris Mason 			break;
159b6cda9bcSChris Mason 		case Opt_nodatasum:
160edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: setting nodatacsum\n");
161b6cda9bcSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
162be20aa9dSChris Mason 			break;
163be20aa9dSChris Mason 		case Opt_nodatacow:
164edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: setting nodatacow\n");
165be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
166be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
167b6cda9bcSChris Mason 			break;
168c8b97818SChris Mason 		case Opt_compress:
169c8b97818SChris Mason 			printk(KERN_INFO "btrfs: use compression\n");
170c8b97818SChris Mason 			btrfs_set_opt(info->mount_opt, COMPRESS);
171c8b97818SChris Mason 			break;
172e18e4809SChris Mason 		case Opt_ssd:
173edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
174e18e4809SChris Mason 			btrfs_set_opt(info->mount_opt, SSD);
175e18e4809SChris Mason 			break;
17621ad10cfSChris Mason 		case Opt_nobarrier:
177edf24abeSChristoph Hellwig 			printk(KERN_INFO "btrfs: turning off barriers\n");
17821ad10cfSChris Mason 			btrfs_set_opt(info->mount_opt, NOBARRIER);
17921ad10cfSChris Mason 			break;
1804543df7eSChris Mason 		case Opt_thread_pool:
1814543df7eSChris Mason 			intarg = 0;
1824543df7eSChris Mason 			match_int(&args[0], &intarg);
1834543df7eSChris Mason 			if (intarg) {
1844543df7eSChris Mason 				info->thread_pool_size = intarg;
1854543df7eSChris Mason 				printk(KERN_INFO "btrfs: thread pool %d\n",
1864543df7eSChris Mason 				       info->thread_pool_size);
1874543df7eSChris Mason 			}
1884543df7eSChris Mason 			break;
189c59f8951SChris Mason 		case Opt_max_extent:
190edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
191c59f8951SChris Mason 			if (num) {
192edf24abeSChristoph Hellwig 				info->max_extent = btrfs_parse_size(num);
193c59f8951SChris Mason 				kfree(num);
194c59f8951SChris Mason 
195c59f8951SChris Mason 				info->max_extent = max_t(u64,
196edf24abeSChristoph Hellwig 					info->max_extent, root->sectorsize);
197edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: max_extent at %llu\n",
198c59f8951SChris Mason 				       info->max_extent);
199c59f8951SChris Mason 			}
200c59f8951SChris Mason 			break;
2016f568d35SChris Mason 		case Opt_max_inline:
202edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
2036f568d35SChris Mason 			if (num) {
204edf24abeSChristoph Hellwig 				info->max_inline = btrfs_parse_size(num);
2056f568d35SChris Mason 				kfree(num);
2066f568d35SChris Mason 
20715ada040SChris Mason 				if (info->max_inline) {
2086f568d35SChris Mason 					info->max_inline = max_t(u64,
20915ada040SChris Mason 						info->max_inline,
21015ada040SChris Mason 						root->sectorsize);
21115ada040SChris Mason 				}
212edf24abeSChristoph Hellwig 				printk(KERN_INFO "btrfs: max_inline at %llu\n",
2136f568d35SChris Mason 					info->max_inline);
2146f568d35SChris Mason 			}
2156f568d35SChris Mason 			break;
2168f662a76SChris Mason 		case Opt_alloc_start:
217edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
2188f662a76SChris Mason 			if (num) {
219edf24abeSChristoph Hellwig 				info->alloc_start = btrfs_parse_size(num);
2208f662a76SChris Mason 				kfree(num);
221edf24abeSChristoph Hellwig 				printk(KERN_INFO
222edf24abeSChristoph Hellwig 					"btrfs: allocations start at %llu\n",
223edf24abeSChristoph Hellwig 					info->alloc_start);
2248f662a76SChris Mason 			}
2258f662a76SChris Mason 			break;
22633268eafSJosef Bacik 		case Opt_noacl:
22733268eafSJosef Bacik 			root->fs_info->sb->s_flags &= ~MS_POSIXACL;
22833268eafSJosef Bacik 			break;
22995e05289SChris Mason 		default:
230be20aa9dSChris Mason 			break;
23195e05289SChris Mason 		}
23295e05289SChris Mason 	}
233be20aa9dSChris Mason 	kfree(options);
234edf24abeSChristoph Hellwig 	return 0;
235edf24abeSChristoph Hellwig }
236edf24abeSChristoph Hellwig 
237edf24abeSChristoph Hellwig /*
238edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
239edf24abeSChristoph Hellwig  *
240edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
241edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
242edf24abeSChristoph Hellwig  */
24343e570b0SChristoph Hellwig static int btrfs_parse_early_options(const char *options, int flags,
24443e570b0SChristoph Hellwig 		void *holder, char **subvol_name,
24543e570b0SChristoph Hellwig 		struct btrfs_fs_devices **fs_devices)
246edf24abeSChristoph Hellwig {
247edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
248edf24abeSChristoph Hellwig 	char *opts, *p;
249edf24abeSChristoph Hellwig 	int error = 0;
250edf24abeSChristoph Hellwig 
251edf24abeSChristoph Hellwig 	if (!options)
252edf24abeSChristoph Hellwig 		goto out;
253edf24abeSChristoph Hellwig 
254edf24abeSChristoph Hellwig 	/*
255edf24abeSChristoph Hellwig 	 * strsep changes the string, duplicate it because parse_options
256edf24abeSChristoph Hellwig 	 * gets called twice
257edf24abeSChristoph Hellwig 	 */
258edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
259edf24abeSChristoph Hellwig 	if (!opts)
260edf24abeSChristoph Hellwig 		return -ENOMEM;
261edf24abeSChristoph Hellwig 
262edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
263edf24abeSChristoph Hellwig 		int token;
264edf24abeSChristoph Hellwig 		if (!*p)
265edf24abeSChristoph Hellwig 			continue;
266edf24abeSChristoph Hellwig 
267edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
268edf24abeSChristoph Hellwig 		switch (token) {
269edf24abeSChristoph Hellwig 		case Opt_subvol:
270edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
271edf24abeSChristoph Hellwig 			break;
27243e570b0SChristoph Hellwig 		case Opt_device:
27343e570b0SChristoph Hellwig 			error = btrfs_scan_one_device(match_strdup(&args[0]),
27443e570b0SChristoph Hellwig 					flags, holder, fs_devices);
27543e570b0SChristoph Hellwig 			if (error)
27643e570b0SChristoph Hellwig 				goto out_free_opts;
27743e570b0SChristoph Hellwig 			break;
278edf24abeSChristoph Hellwig 		default:
279edf24abeSChristoph Hellwig 			break;
280edf24abeSChristoph Hellwig 		}
281edf24abeSChristoph Hellwig 	}
282edf24abeSChristoph Hellwig 
28343e570b0SChristoph Hellwig  out_free_opts:
284edf24abeSChristoph Hellwig 	kfree(opts);
285edf24abeSChristoph Hellwig  out:
286edf24abeSChristoph Hellwig 	/*
287edf24abeSChristoph Hellwig 	 * If no subvolume name is specified we use the default one.  Allocate
288*3de4586cSChris Mason 	 * a copy of the string "." here so that code later in the
289edf24abeSChristoph Hellwig 	 * mount path doesn't care if it's the default volume or another one.
290edf24abeSChristoph Hellwig 	 */
291edf24abeSChristoph Hellwig 	if (!*subvol_name) {
292*3de4586cSChris Mason 		*subvol_name = kstrdup(".", GFP_KERNEL);
293edf24abeSChristoph Hellwig 		if (!*subvol_name)
294edf24abeSChristoph Hellwig 			return -ENOMEM;
295edf24abeSChristoph Hellwig 	}
296edf24abeSChristoph Hellwig 	return error;
29795e05289SChris Mason }
29895e05289SChris Mason 
2998a4b83ccSChris Mason static int btrfs_fill_super(struct super_block * sb,
3008a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
3018a4b83ccSChris Mason 			    void * data, int silent)
3022e635a27SChris Mason {
3032e635a27SChris Mason 	struct inode * inode;
304e20d96d6SChris Mason 	struct dentry * root_dentry;
305e20d96d6SChris Mason 	struct btrfs_super_block *disk_super;
3060f7d52f4SChris Mason 	struct btrfs_root *tree_root;
307d6e4a428SChris Mason 	struct btrfs_inode *bi;
30839279cc3SChris Mason 	int err;
3092e635a27SChris Mason 
3102e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
3112e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
312e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
313be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
3145103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
3152e635a27SChris Mason 	sb->s_time_gran = 1;
31633268eafSJosef Bacik 	sb->s_flags |= MS_POSIXACL;
317e20d96d6SChris Mason 
318dfe25020SChris Mason 	tree_root = open_ctree(sb, fs_devices, (char *)data);
319d98237b3SChris Mason 
320e58ca020SYan 	if (IS_ERR(tree_root)) {
321e20d96d6SChris Mason 		printk("btrfs: open_ctree failed\n");
322e58ca020SYan 		return PTR_ERR(tree_root);
323e20d96d6SChris Mason 	}
3240f7d52f4SChris Mason 	sb->s_fs_info = tree_root;
3255f39d397SChris Mason 	disk_super = &tree_root->fs_info->super_copy;
326*3de4586cSChris Mason 	inode = btrfs_iget_locked(sb, BTRFS_FIRST_FREE_OBJECTID,
327*3de4586cSChris Mason 				  tree_root->fs_info->fs_root);
328d6e4a428SChris Mason 	bi = BTRFS_I(inode);
329d6e4a428SChris Mason 	bi->location.objectid = inode->i_ino;
330d6e4a428SChris Mason 	bi->location.offset = 0;
331*3de4586cSChris Mason 	bi->root = tree_root->fs_info->fs_root;
332b888db2bSChris Mason 
333d6e4a428SChris Mason 	btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
334d6e4a428SChris Mason 
33539279cc3SChris Mason 	if (!inode) {
33639279cc3SChris Mason 		err = -ENOMEM;
33739279cc3SChris Mason 		goto fail_close;
33839279cc3SChris Mason 	}
339e20d96d6SChris Mason 	if (inode->i_state & I_NEW) {
340e20d96d6SChris Mason 		btrfs_read_locked_inode(inode);
341e20d96d6SChris Mason 		unlock_new_inode(inode);
342e20d96d6SChris Mason 	}
3432e635a27SChris Mason 
344e20d96d6SChris Mason 	root_dentry = d_alloc_root(inode);
345e20d96d6SChris Mason 	if (!root_dentry) {
3462e635a27SChris Mason 		iput(inode);
34739279cc3SChris Mason 		err = -ENOMEM;
34839279cc3SChris Mason 		goto fail_close;
3492e635a27SChris Mason 	}
35058176a96SJosef Bacik 
35158176a96SJosef Bacik 	/* this does the super kobj at the same time */
35258176a96SJosef Bacik 	err = btrfs_sysfs_add_super(tree_root->fs_info);
35358176a96SJosef Bacik 	if (err)
35458176a96SJosef Bacik 		goto fail_close;
35558176a96SJosef Bacik 
356e20d96d6SChris Mason 	sb->s_root = root_dentry;
3576885f308SChris Mason 
3586885f308SChris Mason 	save_mount_options(sb, data);
3592e635a27SChris Mason 	return 0;
3602e635a27SChris Mason 
36139279cc3SChris Mason fail_close:
36239279cc3SChris Mason 	close_ctree(tree_root);
363d5719762SChris Mason 	return err;
364d5719762SChris Mason }
365d5719762SChris Mason 
3666bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
367d5719762SChris Mason {
368d5719762SChris Mason 	struct btrfs_trans_handle *trans;
369d5719762SChris Mason 	struct btrfs_root *root;
370d5719762SChris Mason 	int ret;
371d98237b3SChris Mason 	root = btrfs_sb(sb);
372df2ce34cSChris Mason 
373c146afadSYan Zheng 	if (sb->s_flags & MS_RDONLY)
374c146afadSYan Zheng 		return 0;
375c146afadSYan Zheng 
376d5719762SChris Mason 	sb->s_dirt = 0;
377d561c025SChris Mason 	if (!wait) {
3787cfcc17eSChris Mason 		filemap_flush(root->fs_info->btree_inode->i_mapping);
379df2ce34cSChris Mason 		return 0;
380d561c025SChris Mason 	}
381771ed689SChris Mason 
382771ed689SChris Mason 	btrfs_start_delalloc_inodes(root);
383771ed689SChris Mason 	btrfs_wait_ordered_extents(root, 0);
384771ed689SChris Mason 
385e9d0b13bSChris Mason 	btrfs_clean_old_snapshots(root);
386d5719762SChris Mason 	trans = btrfs_start_transaction(root, 1);
387d5719762SChris Mason 	ret = btrfs_commit_transaction(trans, root);
388d5719762SChris Mason 	sb->s_dirt = 0;
38954aa1f4dSChris Mason 	return ret;
390d5719762SChris Mason }
391d5719762SChris Mason 
392d561c025SChris Mason static void btrfs_write_super(struct super_block *sb)
393d561c025SChris Mason {
39408607c1bSChris Mason 	sb->s_dirt = 0;
395d561c025SChris Mason }
396d561c025SChris Mason 
397a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
3982e635a27SChris Mason {
399a061fc8dSChris Mason 	struct btrfs_fs_devices *test_fs_devices = data;
400a061fc8dSChris Mason 	struct btrfs_root *root = btrfs_sb(s);
4014b82d6e4SYan 
402a061fc8dSChris Mason 	return root->fs_info->fs_devices == test_fs_devices;
4034b82d6e4SYan }
4044b82d6e4SYan 
405edf24abeSChristoph Hellwig /*
406edf24abeSChristoph Hellwig  * Find a superblock for the given device / mount point.
407edf24abeSChristoph Hellwig  *
408edf24abeSChristoph Hellwig  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
409edf24abeSChristoph Hellwig  *	  for multiple device setup.  Make sure to keep it in sync.
410edf24abeSChristoph Hellwig  */
411edf24abeSChristoph Hellwig static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
412edf24abeSChristoph Hellwig 		const char *dev_name, void *data, struct vfsmount *mnt)
4134b82d6e4SYan {
414edf24abeSChristoph Hellwig 	char *subvol_name = NULL;
4154b82d6e4SYan 	struct block_device *bdev = NULL;
4164b82d6e4SYan 	struct super_block *s;
4174b82d6e4SYan 	struct dentry *root;
4188a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices = NULL;
4194b82d6e4SYan 	int error = 0;
4204b82d6e4SYan 
42143e570b0SChristoph Hellwig 	error = btrfs_parse_early_options(data, flags, fs_type,
42243e570b0SChristoph Hellwig 					  &subvol_name, &fs_devices);
423edf24abeSChristoph Hellwig 	if (error)
424edf24abeSChristoph Hellwig 		goto error;
425edf24abeSChristoph Hellwig 
4268a4b83ccSChris Mason 	error = btrfs_scan_one_device(dev_name, flags, fs_type, &fs_devices);
4278a4b83ccSChris Mason 	if (error)
428edf24abeSChristoph Hellwig 		goto error_free_subvol_name;
4294b82d6e4SYan 
4308a4b83ccSChris Mason 	error = btrfs_open_devices(fs_devices, flags, fs_type);
4318a4b83ccSChris Mason 	if (error)
432edf24abeSChristoph Hellwig 		goto error_free_subvol_name;
4338a4b83ccSChris Mason 
4342b82032cSYan Zheng 	if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
4352b82032cSYan Zheng 		error = -EACCES;
4362b82032cSYan Zheng 		goto error_close_devices;
4372b82032cSYan Zheng 	}
4382b82032cSYan Zheng 
439dfe25020SChris Mason 	bdev = fs_devices->latest_bdev;
440a061fc8dSChris Mason 	s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices);
4414b82d6e4SYan 	if (IS_ERR(s))
4424b82d6e4SYan 		goto error_s;
4434b82d6e4SYan 
4444b82d6e4SYan 	if (s->s_root) {
4454b82d6e4SYan 		if ((flags ^ s->s_flags) & MS_RDONLY) {
4464b82d6e4SYan 			up_write(&s->s_umount);
4474b82d6e4SYan 			deactivate_super(s);
4484b82d6e4SYan 			error = -EBUSY;
449c146afadSYan Zheng 			goto error_close_devices;
4504b82d6e4SYan 		}
4514b82d6e4SYan 
4522b82032cSYan Zheng 		btrfs_close_devices(fs_devices);
4534b82d6e4SYan 	} else {
4544b82d6e4SYan 		char b[BDEVNAME_SIZE];
4554b82d6e4SYan 
4564b82d6e4SYan 		s->s_flags = flags;
4574b82d6e4SYan 		strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
4588a4b83ccSChris Mason 		error = btrfs_fill_super(s, fs_devices, data,
4598a4b83ccSChris Mason 					 flags & MS_SILENT ? 1 : 0);
4604b82d6e4SYan 		if (error) {
4614b82d6e4SYan 			up_write(&s->s_umount);
4624b82d6e4SYan 			deactivate_super(s);
4634b82d6e4SYan 			goto error;
4644b82d6e4SYan 		}
4654b82d6e4SYan 
466788f20ebSChris Mason 		btrfs_sb(s)->fs_info->bdev_holder = fs_type;
4674b82d6e4SYan 		s->s_flags |= MS_ACTIVE;
4684b82d6e4SYan 	}
4694b82d6e4SYan 
47076fcef19SDavid Woodhouse 	if (!strcmp(subvol_name, "."))
47176fcef19SDavid Woodhouse 		root = dget(s->s_root);
47276fcef19SDavid Woodhouse 	else {
473b48652c1SYan Zheng 		mutex_lock(&s->s_root->d_inode->i_mutex);
474edf24abeSChristoph Hellwig 		root = lookup_one_len(subvol_name, s->s_root, strlen(subvol_name));
475b48652c1SYan Zheng 		mutex_unlock(&s->s_root->d_inode->i_mutex);
4764b82d6e4SYan 		if (IS_ERR(root)) {
4774b82d6e4SYan 			up_write(&s->s_umount);
4784b82d6e4SYan 			deactivate_super(s);
4794b82d6e4SYan 			error = PTR_ERR(root);
4804b82d6e4SYan 			goto error;
4814b82d6e4SYan 		}
4824b82d6e4SYan 		if (!root->d_inode) {
4834b82d6e4SYan 			dput(root);
4844b82d6e4SYan 			up_write(&s->s_umount);
4854b82d6e4SYan 			deactivate_super(s);
4864b82d6e4SYan 			error = -ENXIO;
4874b82d6e4SYan 			goto error;
4884b82d6e4SYan 		}
48976fcef19SDavid Woodhouse 	}
4904b82d6e4SYan 
4914b82d6e4SYan 	mnt->mnt_sb = s;
4924b82d6e4SYan 	mnt->mnt_root = root;
493edf24abeSChristoph Hellwig 
494edf24abeSChristoph Hellwig 	kfree(subvol_name);
4954b82d6e4SYan 	return 0;
4964b82d6e4SYan 
4974b82d6e4SYan error_s:
4984b82d6e4SYan 	error = PTR_ERR(s);
499c146afadSYan Zheng error_close_devices:
5008a4b83ccSChris Mason 	btrfs_close_devices(fs_devices);
501edf24abeSChristoph Hellwig error_free_subvol_name:
502edf24abeSChristoph Hellwig 	kfree(subvol_name);
5034b82d6e4SYan error:
5044b82d6e4SYan 	return error;
5054b82d6e4SYan }
5062e635a27SChris Mason 
507c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
508c146afadSYan Zheng {
509c146afadSYan Zheng 	struct btrfs_root *root = btrfs_sb(sb);
510c146afadSYan Zheng 	int ret;
511c146afadSYan Zheng 
512c146afadSYan Zheng 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
513c146afadSYan Zheng 		return 0;
514c146afadSYan Zheng 
515c146afadSYan Zheng 	if (*flags & MS_RDONLY) {
516c146afadSYan Zheng 		sb->s_flags |= MS_RDONLY;
517c146afadSYan Zheng 
518c146afadSYan Zheng 		ret =  btrfs_commit_super(root);
519c146afadSYan Zheng 		WARN_ON(ret);
520c146afadSYan Zheng 	} else {
5212b82032cSYan Zheng 		if (root->fs_info->fs_devices->rw_devices == 0)
5222b82032cSYan Zheng 			return -EACCES;
5232b82032cSYan Zheng 
524c146afadSYan Zheng 		if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
525c146afadSYan Zheng 			return -EINVAL;
526c146afadSYan Zheng 
527c146afadSYan Zheng 		ret = btrfs_cleanup_reloc_trees(root);
528c146afadSYan Zheng 		WARN_ON(ret);
529c146afadSYan Zheng 
530c146afadSYan Zheng 		ret = btrfs_cleanup_fs_roots(root->fs_info);
531c146afadSYan Zheng 		WARN_ON(ret);
532c146afadSYan Zheng 
533c146afadSYan Zheng 		sb->s_flags &= ~MS_RDONLY;
534c146afadSYan Zheng 	}
535c146afadSYan Zheng 
536c146afadSYan Zheng 	return 0;
537c146afadSYan Zheng }
538c146afadSYan Zheng 
5398fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
5408fd17795SChris Mason {
5418fd17795SChris Mason 	struct btrfs_root *root = btrfs_sb(dentry->d_sb);
5424b52dff6SChris Mason 	struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
543db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
5449d03632eSDavid Woodhouse 	__be32 *fsid = (__be32 *)root->fs_info->fsid;
5458fd17795SChris Mason 
5468fd17795SChris Mason 	buf->f_namelen = BTRFS_NAME_LEN;
547db94535dSChris Mason 	buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
548db94535dSChris Mason 	buf->f_bfree = buf->f_blocks -
549db94535dSChris Mason 		(btrfs_super_bytes_used(disk_super) >> bits);
5508fd17795SChris Mason 	buf->f_bavail = buf->f_bfree;
5518fd17795SChris Mason 	buf->f_bsize = dentry->d_sb->s_blocksize;
5528fd17795SChris Mason 	buf->f_type = BTRFS_SUPER_MAGIC;
5539d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
5549d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
5559d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
5569d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
5579d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
55832d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
55932d48fa1SDavid Woodhouse 	buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
56032d48fa1SDavid Woodhouse 	buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
56132d48fa1SDavid Woodhouse 
5628fd17795SChris Mason 	return 0;
5638fd17795SChris Mason }
564b5133862SChris Mason 
5652e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
5662e635a27SChris Mason 	.owner		= THIS_MODULE,
5672e635a27SChris Mason 	.name		= "btrfs",
5682e635a27SChris Mason 	.get_sb		= btrfs_get_sb,
569a061fc8dSChris Mason 	.kill_sb	= kill_anon_super,
5702e635a27SChris Mason 	.fs_flags	= FS_REQUIRES_DEV,
5712e635a27SChris Mason };
572a9218f6bSChris Mason 
573d352ac68SChris Mason /*
574d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
575d352ac68SChris Mason  */
5768a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
5778a4b83ccSChris Mason 				unsigned long arg)
5788a4b83ccSChris Mason {
5798a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
5808a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
581f819d837SLinda Knippers 	int ret = 0;
5828a4b83ccSChris Mason 	int len;
5838a4b83ccSChris Mason 
5848a4b83ccSChris Mason 	vol = kmalloc(sizeof(*vol), GFP_KERNEL);
5858a4b83ccSChris Mason 	if (copy_from_user(vol, (void __user *)arg, sizeof(*vol))) {
5868a4b83ccSChris Mason 		ret = -EFAULT;
5878a4b83ccSChris Mason 		goto out;
5888a4b83ccSChris Mason 	}
5898a4b83ccSChris Mason 	len = strnlen(vol->name, BTRFS_PATH_NAME_MAX);
5908a4b83ccSChris Mason 	switch (cmd) {
5918a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
5928a4b83ccSChris Mason 		ret = btrfs_scan_one_device(vol->name, MS_RDONLY,
5938a4b83ccSChris Mason 					    &btrfs_fs_type, &fs_devices);
5948a4b83ccSChris Mason 		break;
5958a4b83ccSChris Mason 	}
5968a4b83ccSChris Mason out:
5978a4b83ccSChris Mason 	kfree(vol);
598f819d837SLinda Knippers 	return ret;
5998a4b83ccSChris Mason }
6008a4b83ccSChris Mason 
601ed0dab6bSYan static void btrfs_write_super_lockfs(struct super_block *sb)
602ed0dab6bSYan {
603ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
604a74a4b97SChris Mason 	mutex_lock(&root->fs_info->transaction_kthread_mutex);
605a74a4b97SChris Mason 	mutex_lock(&root->fs_info->cleaner_mutex);
606ed0dab6bSYan }
607ed0dab6bSYan 
608ed0dab6bSYan static void btrfs_unlockfs(struct super_block *sb)
609ed0dab6bSYan {
610ed0dab6bSYan 	struct btrfs_root *root = btrfs_sb(sb);
611a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->cleaner_mutex);
612a74a4b97SChris Mason 	mutex_unlock(&root->fs_info->transaction_kthread_mutex);
613ed0dab6bSYan }
6142e635a27SChris Mason 
615e20d96d6SChris Mason static struct super_operations btrfs_super_ops = {
616134e9731SChris Mason 	.delete_inode	= btrfs_delete_inode,
617e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
618d5719762SChris Mason 	.write_super	= btrfs_write_super,
619d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
6206885f308SChris Mason 	.show_options	= generic_show_options,
6214730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
622b5133862SChris Mason 	.dirty_inode	= btrfs_dirty_inode,
6232c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
6242c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
6258fd17795SChris Mason 	.statfs		= btrfs_statfs,
626c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
627ed0dab6bSYan 	.write_super_lockfs = btrfs_write_super_lockfs,
628ed0dab6bSYan 	.unlockfs	= btrfs_unlockfs,
629e20d96d6SChris Mason };
630a9218f6bSChris Mason 
631a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
632a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
633a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
634a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
635a9218f6bSChris Mason };
636a9218f6bSChris Mason 
637a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
638a9218f6bSChris Mason 	.minor		= MISC_DYNAMIC_MINOR,
639a9218f6bSChris Mason 	.name		= "btrfs-control",
640a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
641a9218f6bSChris Mason };
642a9218f6bSChris Mason 
643a9218f6bSChris Mason static int btrfs_interface_init(void)
644a9218f6bSChris Mason {
645a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
646a9218f6bSChris Mason }
647a9218f6bSChris Mason 
648a9218f6bSChris Mason void btrfs_interface_exit(void)
649a9218f6bSChris Mason {
650a9218f6bSChris Mason 	if (misc_deregister(&btrfs_misc) < 0)
651a9218f6bSChris Mason 		printk("misc_deregister failed for control device");
652a9218f6bSChris Mason }
653a9218f6bSChris Mason 
6542e635a27SChris Mason static int __init init_btrfs_fs(void)
6552e635a27SChris Mason {
6562c90e5d6SChris Mason 	int err;
65758176a96SJosef Bacik 
65858176a96SJosef Bacik 	err = btrfs_init_sysfs();
65958176a96SJosef Bacik 	if (err)
66058176a96SJosef Bacik 		return err;
66158176a96SJosef Bacik 
66239279cc3SChris Mason 	err = btrfs_init_cachep();
6632c90e5d6SChris Mason 	if (err)
664a74a4b97SChris Mason 		goto free_sysfs;
665d1310b2eSChris Mason 
666d1310b2eSChris Mason 	err = extent_io_init();
6672f4cbe64SWyatt Banks 	if (err)
6682f4cbe64SWyatt Banks 		goto free_cachep;
6692f4cbe64SWyatt Banks 
670d1310b2eSChris Mason 	err = extent_map_init();
671d1310b2eSChris Mason 	if (err)
672d1310b2eSChris Mason 		goto free_extent_io;
673d1310b2eSChris Mason 
674a9218f6bSChris Mason 	err = btrfs_interface_init();
6752f4cbe64SWyatt Banks 	if (err)
6762f4cbe64SWyatt Banks 		goto free_extent_map;
677c8b97818SChris Mason 
678a9218f6bSChris Mason 	err = register_filesystem(&btrfs_fs_type);
679a9218f6bSChris Mason 	if (err)
680a9218f6bSChris Mason 		goto unregister_ioctl;
681b3c3da71SChris Mason 
682b3c3da71SChris Mason 	printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
6832f4cbe64SWyatt Banks 	return 0;
6842f4cbe64SWyatt Banks 
685a9218f6bSChris Mason unregister_ioctl:
686a9218f6bSChris Mason 	btrfs_interface_exit();
6872f4cbe64SWyatt Banks free_extent_map:
6882f4cbe64SWyatt Banks 	extent_map_exit();
689d1310b2eSChris Mason free_extent_io:
690d1310b2eSChris Mason 	extent_io_exit();
6912f4cbe64SWyatt Banks free_cachep:
6922f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
693a74a4b97SChris Mason free_sysfs:
6942f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
6952c90e5d6SChris Mason 	return err;
6962e635a27SChris Mason }
6972e635a27SChris Mason 
6982e635a27SChris Mason static void __exit exit_btrfs_fs(void)
6992e635a27SChris Mason {
70039279cc3SChris Mason 	btrfs_destroy_cachep();
701a52d9a80SChris Mason 	extent_map_exit();
702d1310b2eSChris Mason 	extent_io_exit();
703a9218f6bSChris Mason 	btrfs_interface_exit();
7042e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
70558176a96SJosef Bacik 	btrfs_exit_sysfs();
7068a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
707c8b97818SChris Mason 	btrfs_zlib_exit();
7082e635a27SChris Mason }
7092e635a27SChris Mason 
7102e635a27SChris Mason module_init(init_btrfs_fs)
7112e635a27SChris Mason module_exit(exit_btrfs_fs)
7122e635a27SChris Mason 
7132e635a27SChris Mason MODULE_LICENSE("GPL");
714