]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/btrfs/super.c
Btrfs: Add new ioctl to add devices
[mirror_ubuntu-artful-kernel.git] / fs / btrfs / super.c
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 <linux/miscdevice.h>
40 #include "ctree.h"
41 #include "disk-io.h"
42 #include "transaction.h"
43 #include "btrfs_inode.h"
44 #include "ioctl.h"
45 #include "print-tree.h"
46 #include "xattr.h"
47 #include "volumes.h"
48
49 #define BTRFS_SUPER_MAGIC 0x9123683E
50
51 static struct super_operations btrfs_super_ops;
52
53 static void btrfs_put_super (struct super_block * sb)
54 {
55 struct btrfs_root *root = btrfs_sb(sb);
56 struct btrfs_fs_info *fs = root->fs_info;
57 int ret;
58
59 ret = close_ctree(root);
60 if (ret) {
61 printk("close ctree returns %d\n", ret);
62 }
63 btrfs_sysfs_del_super(fs);
64 sb->s_fs_info = NULL;
65 }
66
67 enum {
68 Opt_subvol, Opt_nodatasum, Opt_nodatacow, Opt_max_extent,
69 Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, Opt_err,
70 };
71
72 static match_table_t tokens = {
73 {Opt_subvol, "subvol=%s"},
74 {Opt_nodatasum, "nodatasum"},
75 {Opt_nodatacow, "nodatacow"},
76 {Opt_nobarrier, "nobarrier"},
77 {Opt_max_extent, "max_extent=%s"},
78 {Opt_max_inline, "max_inline=%s"},
79 {Opt_alloc_start, "alloc_start=%s"},
80 {Opt_ssd, "ssd"},
81 {Opt_err, NULL}
82 };
83
84 u64 btrfs_parse_size(char *str)
85 {
86 u64 res;
87 int mult = 1;
88 char *end;
89 char last;
90
91 res = simple_strtoul(str, &end, 10);
92
93 last = end[0];
94 if (isalpha(last)) {
95 last = tolower(last);
96 switch (last) {
97 case 'g':
98 mult *= 1024;
99 case 'm':
100 mult *= 1024;
101 case 'k':
102 mult *= 1024;
103 }
104 res = res * mult;
105 }
106 return res;
107 }
108
109 static int parse_options (char * options,
110 struct btrfs_root *root,
111 char **subvol_name)
112 {
113 char * p;
114 struct btrfs_fs_info *info = NULL;
115 substring_t args[MAX_OPT_ARGS];
116
117 if (!options)
118 return 1;
119
120 /*
121 * strsep changes the string, duplicate it because parse_options
122 * gets called twice
123 */
124 options = kstrdup(options, GFP_NOFS);
125 if (!options)
126 return -ENOMEM;
127
128 if (root)
129 info = root->fs_info;
130
131 while ((p = strsep (&options, ",")) != NULL) {
132 int token;
133 if (!*p)
134 continue;
135
136 token = match_token(p, tokens, args);
137 switch (token) {
138 case Opt_subvol:
139 if (subvol_name) {
140 *subvol_name = match_strdup(&args[0]);
141 }
142 break;
143 case Opt_nodatasum:
144 if (info) {
145 printk("btrfs: setting nodatacsum\n");
146 btrfs_set_opt(info->mount_opt, NODATASUM);
147 }
148 break;
149 case Opt_nodatacow:
150 if (info) {
151 printk("btrfs: setting nodatacow\n");
152 btrfs_set_opt(info->mount_opt, NODATACOW);
153 btrfs_set_opt(info->mount_opt, NODATASUM);
154 }
155 break;
156 case Opt_ssd:
157 if (info) {
158 printk("btrfs: use ssd allocation scheme\n");
159 btrfs_set_opt(info->mount_opt, SSD);
160 }
161 break;
162 case Opt_nobarrier:
163 if (info) {
164 printk("btrfs: turning off barriers\n");
165 btrfs_set_opt(info->mount_opt, NOBARRIER);
166 }
167 break;
168 case Opt_max_extent:
169 if (info) {
170 char *num = match_strdup(&args[0]);
171 if (num) {
172 info->max_extent =
173 btrfs_parse_size(num);
174 kfree(num);
175
176 info->max_extent = max_t(u64,
177 info->max_extent,
178 root->sectorsize);
179 printk("btrfs: max_extent at %Lu\n",
180 info->max_extent);
181 }
182 }
183 break;
184 case Opt_max_inline:
185 if (info) {
186 char *num = match_strdup(&args[0]);
187 if (num) {
188 info->max_inline =
189 btrfs_parse_size(num);
190 kfree(num);
191
192 info->max_inline = max_t(u64,
193 info->max_inline,
194 root->sectorsize);
195 printk("btrfs: max_inline at %Lu\n",
196 info->max_inline);
197 }
198 }
199 break;
200 case Opt_alloc_start:
201 if (info) {
202 char *num = match_strdup(&args[0]);
203 if (num) {
204 info->alloc_start =
205 btrfs_parse_size(num);
206 kfree(num);
207 printk("btrfs: allocations start at "
208 "%Lu\n", info->alloc_start);
209 }
210 }
211 break;
212 default:
213 break;
214 }
215 }
216 kfree(options);
217 return 1;
218 }
219
220 static int btrfs_fill_super(struct super_block * sb,
221 struct btrfs_fs_devices *fs_devices,
222 void * data, int silent)
223 {
224 struct inode * inode;
225 struct dentry * root_dentry;
226 struct btrfs_super_block *disk_super;
227 struct btrfs_root *tree_root;
228 struct btrfs_inode *bi;
229 int err;
230
231 sb->s_maxbytes = MAX_LFS_FILESIZE;
232 sb->s_magic = BTRFS_SUPER_MAGIC;
233 sb->s_op = &btrfs_super_ops;
234 sb->s_xattr = btrfs_xattr_handlers;
235 sb->s_time_gran = 1;
236
237 tree_root = open_ctree(sb, fs_devices);
238
239 if (IS_ERR(tree_root)) {
240 printk("btrfs: open_ctree failed\n");
241 return PTR_ERR(tree_root);
242 }
243 sb->s_fs_info = tree_root;
244 disk_super = &tree_root->fs_info->super_copy;
245 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
246 tree_root);
247 bi = BTRFS_I(inode);
248 bi->location.objectid = inode->i_ino;
249 bi->location.offset = 0;
250 bi->root = tree_root;
251
252 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
253
254 if (!inode) {
255 err = -ENOMEM;
256 goto fail_close;
257 }
258 if (inode->i_state & I_NEW) {
259 btrfs_read_locked_inode(inode);
260 unlock_new_inode(inode);
261 }
262
263 root_dentry = d_alloc_root(inode);
264 if (!root_dentry) {
265 iput(inode);
266 err = -ENOMEM;
267 goto fail_close;
268 }
269
270 parse_options((char *)data, tree_root, NULL);
271
272 /* this does the super kobj at the same time */
273 err = btrfs_sysfs_add_super(tree_root->fs_info);
274 if (err)
275 goto fail_close;
276
277 sb->s_root = root_dentry;
278 btrfs_transaction_queue_work(tree_root, HZ * 30);
279
280 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
281 save_mount_options(sb, data);
282 #endif
283
284 return 0;
285
286 fail_close:
287 close_ctree(tree_root);
288 return err;
289 }
290
291 static int btrfs_sync_fs(struct super_block *sb, int wait)
292 {
293 struct btrfs_trans_handle *trans;
294 struct btrfs_root *root;
295 int ret;
296 root = btrfs_sb(sb);
297
298 sb->s_dirt = 0;
299 if (!wait) {
300 filemap_flush(root->fs_info->btree_inode->i_mapping);
301 return 0;
302 }
303 btrfs_clean_old_snapshots(root);
304 mutex_lock(&root->fs_info->fs_mutex);
305 btrfs_defrag_dirty_roots(root->fs_info);
306 trans = btrfs_start_transaction(root, 1);
307 ret = btrfs_commit_transaction(trans, root);
308 sb->s_dirt = 0;
309 mutex_unlock(&root->fs_info->fs_mutex);
310 return ret;
311 }
312
313 static void btrfs_write_super(struct super_block *sb)
314 {
315 sb->s_dirt = 0;
316 }
317
318 /*
319 * This is almost a copy of get_sb_bdev in fs/super.c.
320 * We need the local copy to allow direct mounting of
321 * subvolumes, but this could be easily integrated back
322 * into the generic version. --hch
323 */
324
325 /* start copy & paste */
326 static int set_bdev_super(struct super_block *s, void *data)
327 {
328 s->s_bdev = data;
329 s->s_dev = s->s_bdev->bd_dev;
330 return 0;
331 }
332
333 static int test_bdev_super(struct super_block *s, void *data)
334 {
335 return (void *)s->s_bdev == data;
336 }
337
338 int btrfs_get_sb_bdev(struct file_system_type *fs_type,
339 int flags, const char *dev_name, void *data,
340 struct vfsmount *mnt, const char *subvol)
341 {
342 struct block_device *bdev = NULL;
343 struct super_block *s;
344 struct dentry *root;
345 struct btrfs_fs_devices *fs_devices = NULL;
346 int error = 0;
347
348 error = btrfs_scan_one_device(dev_name, flags, fs_type, &fs_devices);
349 if (error)
350 return error;
351
352 error = btrfs_open_devices(fs_devices, flags, fs_type);
353 if (error)
354 return error;
355
356 bdev = fs_devices->lowest_bdev;
357 /*
358 * once the super is inserted into the list by sget, s_umount
359 * will protect the lockfs code from trying to start a snapshot
360 * while we are mounting
361 */
362 down(&bdev->bd_mount_sem);
363 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
364 up(&bdev->bd_mount_sem);
365 if (IS_ERR(s))
366 goto error_s;
367
368 if (s->s_root) {
369 if ((flags ^ s->s_flags) & MS_RDONLY) {
370 up_write(&s->s_umount);
371 deactivate_super(s);
372 error = -EBUSY;
373 goto error_bdev;
374 }
375
376 close_bdev_excl(bdev);
377 } else {
378 char b[BDEVNAME_SIZE];
379
380 s->s_flags = flags;
381 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
382 sb_set_blocksize(s, block_size(bdev));
383 error = btrfs_fill_super(s, fs_devices, data,
384 flags & MS_SILENT ? 1 : 0);
385 if (error) {
386 up_write(&s->s_umount);
387 deactivate_super(s);
388 goto error;
389 }
390
391 btrfs_sb(s)->fs_info->bdev_holder = fs_type;
392 s->s_flags |= MS_ACTIVE;
393 }
394
395 if (subvol) {
396 root = lookup_one_len(subvol, s->s_root, strlen(subvol));
397 if (IS_ERR(root)) {
398 up_write(&s->s_umount);
399 deactivate_super(s);
400 error = PTR_ERR(root);
401 goto error;
402 }
403 if (!root->d_inode) {
404 dput(root);
405 up_write(&s->s_umount);
406 deactivate_super(s);
407 error = -ENXIO;
408 goto error;
409 }
410 } else {
411 root = dget(s->s_root);
412 }
413
414 mnt->mnt_sb = s;
415 mnt->mnt_root = root;
416 return 0;
417
418 error_s:
419 error = PTR_ERR(s);
420 error_bdev:
421 btrfs_close_devices(fs_devices);
422 error:
423 return error;
424 }
425 /* end copy & paste */
426
427 static int btrfs_get_sb(struct file_system_type *fs_type,
428 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
429 {
430 int ret;
431 char *subvol_name = NULL;
432
433 parse_options((char *)data, NULL, &subvol_name);
434 ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data, mnt,
435 subvol_name ? subvol_name : "default");
436 if (subvol_name)
437 kfree(subvol_name);
438 return ret;
439 }
440
441 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
442 {
443 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
444 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
445 int bits = dentry->d_sb->s_blocksize_bits;
446
447 buf->f_namelen = BTRFS_NAME_LEN;
448 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
449 buf->f_bfree = buf->f_blocks -
450 (btrfs_super_bytes_used(disk_super) >> bits);
451 buf->f_bavail = buf->f_bfree;
452 buf->f_bsize = dentry->d_sb->s_blocksize;
453 buf->f_type = BTRFS_SUPER_MAGIC;
454 return 0;
455 }
456
457 static struct file_system_type btrfs_fs_type = {
458 .owner = THIS_MODULE,
459 .name = "btrfs",
460 .get_sb = btrfs_get_sb,
461 .kill_sb = kill_block_super,
462 .fs_flags = FS_REQUIRES_DEV,
463 };
464
465 static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
466 unsigned long arg)
467 {
468 struct btrfs_ioctl_vol_args *vol;
469 struct btrfs_fs_devices *fs_devices;
470 int ret;
471 int len;
472
473 vol = kmalloc(sizeof(*vol), GFP_KERNEL);
474 if (copy_from_user(vol, (void __user *)arg, sizeof(*vol))) {
475 ret = -EFAULT;
476 goto out;
477 }
478 len = strnlen(vol->name, BTRFS_PATH_NAME_MAX);
479 switch (cmd) {
480 case BTRFS_IOC_SCAN_DEV:
481 ret = btrfs_scan_one_device(vol->name, MS_RDONLY,
482 &btrfs_fs_type, &fs_devices);
483 break;
484 }
485 out:
486 kfree(vol);
487 return 0;
488 }
489
490 static void btrfs_write_super_lockfs(struct super_block *sb)
491 {
492 struct btrfs_root *root = btrfs_sb(sb);
493 btrfs_transaction_flush_work(root);
494 }
495
496 static void btrfs_unlockfs(struct super_block *sb)
497 {
498 struct btrfs_root *root = btrfs_sb(sb);
499 btrfs_transaction_queue_work(root, HZ * 30);
500 }
501
502 static struct super_operations btrfs_super_ops = {
503 .delete_inode = btrfs_delete_inode,
504 .put_inode = btrfs_put_inode,
505 .put_super = btrfs_put_super,
506 .write_super = btrfs_write_super,
507 .sync_fs = btrfs_sync_fs,
508 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
509 .read_inode = btrfs_read_locked_inode,
510 #else
511 .show_options = generic_show_options,
512 #endif
513 .write_inode = btrfs_write_inode,
514 .dirty_inode = btrfs_dirty_inode,
515 .alloc_inode = btrfs_alloc_inode,
516 .destroy_inode = btrfs_destroy_inode,
517 .statfs = btrfs_statfs,
518 .write_super_lockfs = btrfs_write_super_lockfs,
519 .unlockfs = btrfs_unlockfs,
520 };
521
522 static const struct file_operations btrfs_ctl_fops = {
523 .unlocked_ioctl = btrfs_control_ioctl,
524 .compat_ioctl = btrfs_control_ioctl,
525 .owner = THIS_MODULE,
526 };
527
528 static struct miscdevice btrfs_misc = {
529 .minor = MISC_DYNAMIC_MINOR,
530 .name = "btrfs-control",
531 .fops = &btrfs_ctl_fops
532 };
533
534 static int btrfs_interface_init(void)
535 {
536 return misc_register(&btrfs_misc);
537 }
538
539 void btrfs_interface_exit(void)
540 {
541 if (misc_deregister(&btrfs_misc) < 0)
542 printk("misc_deregister failed for control device");
543 }
544
545 static int __init init_btrfs_fs(void)
546 {
547 int err;
548
549 err = btrfs_init_sysfs();
550 if (err)
551 return err;
552
553 btrfs_init_transaction_sys();
554 err = btrfs_init_cachep();
555 if (err)
556 goto free_transaction_sys;
557
558 err = extent_io_init();
559 if (err)
560 goto free_cachep;
561
562 err = extent_map_init();
563 if (err)
564 goto free_extent_io;
565
566 err = btrfs_interface_init();
567 if (err)
568 goto free_extent_map;
569 err = register_filesystem(&btrfs_fs_type);
570 if (err)
571 goto unregister_ioctl;
572 return 0;
573
574 unregister_ioctl:
575 btrfs_interface_exit();
576 free_extent_map:
577 extent_map_exit();
578 free_extent_io:
579 extent_io_exit();
580 free_cachep:
581 btrfs_destroy_cachep();
582 free_transaction_sys:
583 btrfs_exit_transaction_sys();
584 btrfs_exit_sysfs();
585 return err;
586 }
587
588 static void __exit exit_btrfs_fs(void)
589 {
590 btrfs_exit_transaction_sys();
591 btrfs_destroy_cachep();
592 extent_map_exit();
593 extent_io_exit();
594 btrfs_interface_exit();
595 unregister_filesystem(&btrfs_fs_type);
596 btrfs_exit_sysfs();
597 btrfs_cleanup_fs_uuids();
598 }
599
600 module_init(init_btrfs_fs)
601 module_exit(exit_btrfs_fs)
602
603 MODULE_LICENSE("GPL");