xref: /openbmc/linux/fs/btrfs/super.c (revision d1310b2e0cd98eb1348553e69b73827b436dca7b)
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18 
19 #include <linux/blkdev.h>
20 #include <linux/module.h>
21 #include <linux/buffer_head.h>
22 #include <linux/fs.h>
23 #include <linux/pagemap.h>
24 #include <linux/highmem.h>
25 #include <linux/time.h>
26 #include <linux/init.h>
27 #include <linux/string.h>
28 #include <linux/smp_lock.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mount.h>
31 #include <linux/mpage.h>
32 #include <linux/swap.h>
33 #include <linux/writeback.h>
34 #include <linux/statfs.h>
35 #include <linux/compat.h>
36 #include <linux/parser.h>
37 #include <linux/ctype.h>
38 #include <linux/namei.h>
39 #include "ctree.h"
40 #include "disk-io.h"
41 #include "transaction.h"
42 #include "btrfs_inode.h"
43 #include "ioctl.h"
44 #include "print-tree.h"
45 #include "xattr.h"
46 
47 #define BTRFS_SUPER_MAGIC 0x9123683E
48 
49 static struct super_operations btrfs_super_ops;
50 
51 static void btrfs_put_super (struct super_block * sb)
52 {
53 	struct btrfs_root *root = btrfs_sb(sb);
54 	struct btrfs_fs_info *fs = root->fs_info;
55 	int ret;
56 
57 	ret = close_ctree(root);
58 	if (ret) {
59 		printk("close ctree returns %d\n", ret);
60 	}
61 	btrfs_sysfs_del_super(fs);
62 	sb->s_fs_info = NULL;
63 }
64 
65 enum {
66 	Opt_subvol, Opt_nodatasum, Opt_nodatacow, Opt_max_extent,
67 	Opt_alloc_start, Opt_nobarrier, Opt_ssd, Opt_err,
68 };
69 
70 static match_table_t tokens = {
71 	{Opt_subvol, "subvol=%s"},
72 	{Opt_nodatasum, "nodatasum"},
73 	{Opt_nodatacow, "nodatacow"},
74 	{Opt_nobarrier, "nobarrier"},
75 	{Opt_max_extent, "max_extent=%s"},
76 	{Opt_alloc_start, "alloc_start=%s"},
77 	{Opt_ssd, "ssd"},
78 	{Opt_err, NULL}
79 };
80 
81 u64 btrfs_parse_size(char *str)
82 {
83 	u64 res;
84 	int mult = 1;
85 	char *end;
86 	char last;
87 
88 	res = simple_strtoul(str, &end, 10);
89 
90 	last = end[0];
91 	if (isalpha(last)) {
92 		last = tolower(last);
93 		switch (last) {
94 		case 'g':
95 			mult *= 1024;
96 		case 'm':
97 			mult *= 1024;
98 		case 'k':
99 			mult *= 1024;
100 		}
101 		res = res * mult;
102 	}
103 	return res;
104 }
105 
106 static int parse_options (char * options,
107 			  struct btrfs_root *root,
108 			  char **subvol_name)
109 {
110 	char * p;
111 	struct btrfs_fs_info *info = NULL;
112 	substring_t args[MAX_OPT_ARGS];
113 
114 	if (!options)
115 		return 1;
116 
117 	/*
118 	 * strsep changes the string, duplicate it because parse_options
119 	 * gets called twice
120 	 */
121 	options = kstrdup(options, GFP_NOFS);
122 	if (!options)
123 		return -ENOMEM;
124 
125 	if (root)
126 		info = root->fs_info;
127 
128 	while ((p = strsep (&options, ",")) != NULL) {
129 		int token;
130 		if (!*p)
131 			continue;
132 
133 		token = match_token(p, tokens, args);
134 		switch (token) {
135 		case Opt_subvol:
136 			if (subvol_name) {
137 				*subvol_name = match_strdup(&args[0]);
138 			}
139 			break;
140 		case Opt_nodatasum:
141 			if (info) {
142 				printk("btrfs: setting nodatacsum\n");
143 				btrfs_set_opt(info->mount_opt, NODATASUM);
144 			}
145 			break;
146 		case Opt_nodatacow:
147 			if (info) {
148 				printk("btrfs: setting nodatacow\n");
149 				btrfs_set_opt(info->mount_opt, NODATACOW);
150 				btrfs_set_opt(info->mount_opt, NODATASUM);
151 			}
152 			break;
153 		case Opt_ssd:
154 			if (info) {
155 				printk("btrfs: use ssd allocation scheme\n");
156 				btrfs_set_opt(info->mount_opt, SSD);
157 			}
158 			break;
159 		case Opt_nobarrier:
160 			if (info) {
161 				printk("btrfs: turning off barriers\n");
162 				btrfs_set_opt(info->mount_opt, NOBARRIER);
163 			}
164 			break;
165 		case Opt_max_extent:
166 			if (info) {
167 				char *num = match_strdup(&args[0]);
168 				if (num) {
169 					info->max_extent =
170 						btrfs_parse_size(num);
171 					kfree(num);
172 
173 					info->max_extent = max_t(u64,
174 							 info->max_extent,
175 							 root->sectorsize);
176 					printk("btrfs: max_extent at %Lu\n",
177 					       info->max_extent);
178 				}
179 			}
180 			break;
181 		case Opt_alloc_start:
182 			if (info) {
183 				char *num = match_strdup(&args[0]);
184 				if (num) {
185 					info->alloc_start =
186 						btrfs_parse_size(num);
187 					kfree(num);
188 					printk("btrfs: allocations start at "
189 					       "%Lu\n", info->alloc_start);
190 				}
191 			}
192 			break;
193 		default:
194 			break;
195 		}
196 	}
197 	kfree(options);
198 	return 1;
199 }
200 
201 static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
202 {
203 	struct inode * inode;
204 	struct dentry * root_dentry;
205 	struct btrfs_super_block *disk_super;
206 	struct btrfs_root *tree_root;
207 	struct btrfs_inode *bi;
208 	int err;
209 
210 	sb->s_maxbytes = MAX_LFS_FILESIZE;
211 	sb->s_magic = BTRFS_SUPER_MAGIC;
212 	sb->s_op = &btrfs_super_ops;
213 	sb->s_xattr = btrfs_xattr_handlers;
214 	sb->s_time_gran = 1;
215 
216 	tree_root = open_ctree(sb);
217 
218 	if (!tree_root || IS_ERR(tree_root)) {
219 		printk("btrfs: open_ctree failed\n");
220 		return -EIO;
221 	}
222 	sb->s_fs_info = tree_root;
223 	disk_super = &tree_root->fs_info->super_copy;
224 	inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
225 				  tree_root);
226 	bi = BTRFS_I(inode);
227 	bi->location.objectid = inode->i_ino;
228 	bi->location.offset = 0;
229 	bi->root = tree_root;
230 
231 	btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
232 
233 	if (!inode) {
234 		err = -ENOMEM;
235 		goto fail_close;
236 	}
237 	if (inode->i_state & I_NEW) {
238 		btrfs_read_locked_inode(inode);
239 		unlock_new_inode(inode);
240 	}
241 
242 	root_dentry = d_alloc_root(inode);
243 	if (!root_dentry) {
244 		iput(inode);
245 		err = -ENOMEM;
246 		goto fail_close;
247 	}
248 
249 	parse_options((char *)data, tree_root, NULL);
250 
251 	/* this does the super kobj at the same time */
252 	err = btrfs_sysfs_add_super(tree_root->fs_info);
253 	if (err)
254 		goto fail_close;
255 
256 	sb->s_root = root_dentry;
257 	btrfs_transaction_queue_work(tree_root, HZ * 30);
258 	return 0;
259 
260 fail_close:
261 	close_ctree(tree_root);
262 	return err;
263 }
264 
265 static int btrfs_sync_fs(struct super_block *sb, int wait)
266 {
267 	struct btrfs_trans_handle *trans;
268 	struct btrfs_root *root;
269 	int ret;
270 	root = btrfs_sb(sb);
271 
272 	sb->s_dirt = 0;
273 	if (!wait) {
274 		filemap_flush(root->fs_info->btree_inode->i_mapping);
275 		return 0;
276 	}
277 	btrfs_clean_old_snapshots(root);
278 	mutex_lock(&root->fs_info->fs_mutex);
279 	btrfs_defrag_dirty_roots(root->fs_info);
280 	trans = btrfs_start_transaction(root, 1);
281 	ret = btrfs_commit_transaction(trans, root);
282 	sb->s_dirt = 0;
283 	mutex_unlock(&root->fs_info->fs_mutex);
284 	return ret;
285 }
286 
287 static void btrfs_write_super(struct super_block *sb)
288 {
289 	sb->s_dirt = 0;
290 }
291 
292 /*
293  * This is almost a copy of get_sb_bdev in fs/super.c.
294  * We need the local copy to allow direct mounting of
295  * subvolumes, but this could be easily integrated back
296  * into the generic version.  --hch
297  */
298 
299 /* start copy & paste */
300 static int set_bdev_super(struct super_block *s, void *data)
301 {
302 	s->s_bdev = data;
303 	s->s_dev = s->s_bdev->bd_dev;
304 	return 0;
305 }
306 
307 static int test_bdev_super(struct super_block *s, void *data)
308 {
309 	return (void *)s->s_bdev == data;
310 }
311 
312 int btrfs_get_sb_bdev(struct file_system_type *fs_type,
313 	int flags, const char *dev_name, void *data,
314 	int (*fill_super)(struct super_block *, void *, int),
315 	struct vfsmount *mnt, const char *subvol)
316 {
317 	struct block_device *bdev = NULL;
318 	struct super_block *s;
319 	struct dentry *root;
320 	int error = 0;
321 
322 	bdev = open_bdev_excl(dev_name, flags, fs_type);
323 	if (IS_ERR(bdev))
324 		return PTR_ERR(bdev);
325 
326 	/*
327 	 * once the super is inserted into the list by sget, s_umount
328 	 * will protect the lockfs code from trying to start a snapshot
329 	 * while we are mounting
330 	 */
331 	down(&bdev->bd_mount_sem);
332 	s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
333 	up(&bdev->bd_mount_sem);
334 	if (IS_ERR(s))
335 		goto error_s;
336 
337 	if (s->s_root) {
338 		if ((flags ^ s->s_flags) & MS_RDONLY) {
339 			up_write(&s->s_umount);
340 			deactivate_super(s);
341 			error = -EBUSY;
342 			goto error_bdev;
343 		}
344 
345 		close_bdev_excl(bdev);
346 	} else {
347 		char b[BDEVNAME_SIZE];
348 
349 		s->s_flags = flags;
350 		strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
351 		sb_set_blocksize(s, block_size(bdev));
352 		error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
353 		if (error) {
354 			up_write(&s->s_umount);
355 			deactivate_super(s);
356 			goto error;
357 		}
358 
359 		s->s_flags |= MS_ACTIVE;
360 	}
361 
362 	if (subvol) {
363 		root = lookup_one_len(subvol, s->s_root, strlen(subvol));
364 		if (IS_ERR(root)) {
365 			up_write(&s->s_umount);
366 			deactivate_super(s);
367 			error = PTR_ERR(root);
368 			goto error;
369 		}
370 		if (!root->d_inode) {
371 			dput(root);
372 			up_write(&s->s_umount);
373 			deactivate_super(s);
374 			error = -ENXIO;
375 			goto error;
376 		}
377 	} else {
378 		root = dget(s->s_root);
379 	}
380 
381 	mnt->mnt_sb = s;
382 	mnt->mnt_root = root;
383 	return 0;
384 
385 error_s:
386 	error = PTR_ERR(s);
387 error_bdev:
388 	close_bdev_excl(bdev);
389 error:
390 	return error;
391 }
392 /* end copy & paste */
393 
394 static int btrfs_get_sb(struct file_system_type *fs_type,
395 	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
396 {
397 	int ret;
398 	char *subvol_name = NULL;
399 
400 	parse_options((char *)data, NULL, &subvol_name);
401 	ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data,
402 			btrfs_fill_super, mnt,
403 			subvol_name ? subvol_name : "default");
404 	if (subvol_name)
405 		kfree(subvol_name);
406 	return ret;
407 }
408 
409 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
410 {
411 	struct btrfs_root *root = btrfs_sb(dentry->d_sb);
412 	struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
413 	int bits = dentry->d_sb->s_blocksize_bits;
414 
415 	buf->f_namelen = BTRFS_NAME_LEN;
416 	buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
417 	buf->f_bfree = buf->f_blocks -
418 		(btrfs_super_bytes_used(disk_super) >> bits);
419 	buf->f_bavail = buf->f_bfree;
420 	buf->f_bsize = dentry->d_sb->s_blocksize;
421 	buf->f_type = BTRFS_SUPER_MAGIC;
422 	return 0;
423 }
424 
425 static struct file_system_type btrfs_fs_type = {
426 	.owner		= THIS_MODULE,
427 	.name		= "btrfs",
428 	.get_sb		= btrfs_get_sb,
429 	.kill_sb	= kill_block_super,
430 	.fs_flags	= FS_REQUIRES_DEV,
431 };
432 static void btrfs_write_super_lockfs(struct super_block *sb)
433 {
434 	struct btrfs_root *root = btrfs_sb(sb);
435 	btrfs_transaction_flush_work(root);
436 }
437 
438 static void btrfs_unlockfs(struct super_block *sb)
439 {
440 	struct btrfs_root *root = btrfs_sb(sb);
441 	btrfs_transaction_queue_work(root, HZ * 30);
442 }
443 
444 static struct super_operations btrfs_super_ops = {
445 	.delete_inode	= btrfs_delete_inode,
446 	.put_inode	= btrfs_put_inode,
447 	.put_super	= btrfs_put_super,
448 	.read_inode	= btrfs_read_locked_inode,
449 	.write_super	= btrfs_write_super,
450 	.sync_fs	= btrfs_sync_fs,
451 	.write_inode	= btrfs_write_inode,
452 	.dirty_inode	= btrfs_dirty_inode,
453 	.alloc_inode	= btrfs_alloc_inode,
454 	.destroy_inode	= btrfs_destroy_inode,
455 	.statfs		= btrfs_statfs,
456 	.write_super_lockfs = btrfs_write_super_lockfs,
457 	.unlockfs	= btrfs_unlockfs,
458 };
459 static int __init init_btrfs_fs(void)
460 {
461 	int err;
462 
463 	err = btrfs_init_sysfs();
464 	if (err)
465 		return err;
466 
467 	btrfs_init_transaction_sys();
468 	err = btrfs_init_cachep();
469 	if (err)
470 		goto free_transaction_sys;
471 
472 	err = extent_io_init();
473 	if (err)
474 		goto free_cachep;
475 
476 	err = extent_map_init();
477 	if (err)
478 		goto free_extent_io;
479 
480 	err = register_filesystem(&btrfs_fs_type);
481 	if (err)
482 		goto free_extent_map;
483 	return 0;
484 
485 free_extent_map:
486 	extent_map_exit();
487 free_extent_io:
488 	extent_io_exit();
489 free_cachep:
490 	btrfs_destroy_cachep();
491 free_transaction_sys:
492 	btrfs_exit_transaction_sys();
493 	btrfs_exit_sysfs();
494 	return err;
495 }
496 
497 static void __exit exit_btrfs_fs(void)
498 {
499 	btrfs_exit_transaction_sys();
500 	btrfs_destroy_cachep();
501 	extent_map_exit();
502 	extent_io_exit();
503 	unregister_filesystem(&btrfs_fs_type);
504 	btrfs_exit_sysfs();
505 }
506 
507 module_init(init_btrfs_fs)
508 module_exit(exit_btrfs_fs)
509 
510 MODULE_LICENSE("GPL");
511