aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/super.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/super.c')
-rw-r--r--fs/btrfs/super.c258
1 files changed, 179 insertions, 79 deletions
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 8a1ea6e64575..1776dbd8dc98 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -38,6 +38,7 @@
38#include <linux/namei.h> 38#include <linux/namei.h>
39#include <linux/miscdevice.h> 39#include <linux/miscdevice.h>
40#include <linux/magic.h> 40#include <linux/magic.h>
41#include <linux/slab.h>
41#include "compat.h" 42#include "compat.h"
42#include "ctree.h" 43#include "ctree.h"
43#include "disk-io.h" 44#include "disk-io.h"
@@ -63,22 +64,21 @@ static void btrfs_put_super(struct super_block *sb)
63} 64}
64 65
65enum { 66enum {
66 Opt_degraded, Opt_subvol, Opt_device, Opt_nodatasum, Opt_nodatacow, 67 Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
67 Opt_max_extent, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, 68 Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
68 Opt_ssd, Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, 69 Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
69 Opt_compress, Opt_compress_force, Opt_notreelog, Opt_ratio, 70 Opt_compress_force, Opt_notreelog, Opt_ratio, Opt_flushoncommit,
70 Opt_flushoncommit,
71 Opt_discard, Opt_err, 71 Opt_discard, Opt_err,
72}; 72};
73 73
74static match_table_t tokens = { 74static match_table_t tokens = {
75 {Opt_degraded, "degraded"}, 75 {Opt_degraded, "degraded"},
76 {Opt_subvol, "subvol=%s"}, 76 {Opt_subvol, "subvol=%s"},
77 {Opt_subvolid, "subvolid=%d"},
77 {Opt_device, "device=%s"}, 78 {Opt_device, "device=%s"},
78 {Opt_nodatasum, "nodatasum"}, 79 {Opt_nodatasum, "nodatasum"},
79 {Opt_nodatacow, "nodatacow"}, 80 {Opt_nodatacow, "nodatacow"},
80 {Opt_nobarrier, "nobarrier"}, 81 {Opt_nobarrier, "nobarrier"},
81 {Opt_max_extent, "max_extent=%s"},
82 {Opt_max_inline, "max_inline=%s"}, 82 {Opt_max_inline, "max_inline=%s"},
83 {Opt_alloc_start, "alloc_start=%s"}, 83 {Opt_alloc_start, "alloc_start=%s"},
84 {Opt_thread_pool, "thread_pool=%d"}, 84 {Opt_thread_pool, "thread_pool=%d"},
@@ -95,31 +95,6 @@ static match_table_t tokens = {
95 {Opt_err, NULL}, 95 {Opt_err, NULL},
96}; 96};
97 97
98u64 btrfs_parse_size(char *str)
99{
100 u64 res;
101 int mult = 1;
102 char *end;
103 char last;
104
105 res = simple_strtoul(str, &end, 10);
106
107 last = end[0];
108 if (isalpha(last)) {
109 last = tolower(last);
110 switch (last) {
111 case 'g':
112 mult *= 1024;
113 case 'm':
114 mult *= 1024;
115 case 'k':
116 mult *= 1024;
117 }
118 res = res * mult;
119 }
120 return res;
121}
122
123/* 98/*
124 * Regular mount options parser. Everything that is needed only when 99 * Regular mount options parser. Everything that is needed only when
125 * reading in a new superblock is parsed here. 100 * reading in a new superblock is parsed here.
@@ -128,7 +103,7 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
128{ 103{
129 struct btrfs_fs_info *info = root->fs_info; 104 struct btrfs_fs_info *info = root->fs_info;
130 substring_t args[MAX_OPT_ARGS]; 105 substring_t args[MAX_OPT_ARGS];
131 char *p, *num; 106 char *p, *num, *orig;
132 int intarg; 107 int intarg;
133 int ret = 0; 108 int ret = 0;
134 109
@@ -143,6 +118,7 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
143 if (!options) 118 if (!options)
144 return -ENOMEM; 119 return -ENOMEM;
145 120
121 orig = options;
146 122
147 while ((p = strsep(&options, ",")) != NULL) { 123 while ((p = strsep(&options, ",")) != NULL) {
148 int token; 124 int token;
@@ -156,6 +132,7 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
156 btrfs_set_opt(info->mount_opt, DEGRADED); 132 btrfs_set_opt(info->mount_opt, DEGRADED);
157 break; 133 break;
158 case Opt_subvol: 134 case Opt_subvol:
135 case Opt_subvolid:
159 case Opt_device: 136 case Opt_device:
160 /* 137 /*
161 * These are parsed by btrfs_parse_early_options 138 * These are parsed by btrfs_parse_early_options
@@ -210,22 +187,10 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
210 info->thread_pool_size); 187 info->thread_pool_size);
211 } 188 }
212 break; 189 break;
213 case Opt_max_extent:
214 num = match_strdup(&args[0]);
215 if (num) {
216 info->max_extent = btrfs_parse_size(num);
217 kfree(num);
218
219 info->max_extent = max_t(u64,
220 info->max_extent, root->sectorsize);
221 printk(KERN_INFO "btrfs: max_extent at %llu\n",
222 (unsigned long long)info->max_extent);
223 }
224 break;
225 case Opt_max_inline: 190 case Opt_max_inline:
226 num = match_strdup(&args[0]); 191 num = match_strdup(&args[0]);
227 if (num) { 192 if (num) {
228 info->max_inline = btrfs_parse_size(num); 193 info->max_inline = memparse(num, NULL);
229 kfree(num); 194 kfree(num);
230 195
231 if (info->max_inline) { 196 if (info->max_inline) {
@@ -240,7 +205,7 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
240 case Opt_alloc_start: 205 case Opt_alloc_start:
241 num = match_strdup(&args[0]); 206 num = match_strdup(&args[0]);
242 if (num) { 207 if (num) {
243 info->alloc_start = btrfs_parse_size(num); 208 info->alloc_start = memparse(num, NULL);
244 kfree(num); 209 kfree(num);
245 printk(KERN_INFO 210 printk(KERN_INFO
246 "btrfs: allocations start at %llu\n", 211 "btrfs: allocations start at %llu\n",
@@ -280,7 +245,7 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
280 } 245 }
281 } 246 }
282out: 247out:
283 kfree(options); 248 kfree(orig);
284 return ret; 249 return ret;
285} 250}
286 251
@@ -291,12 +256,13 @@ out:
291 * only when we need to allocate a new super block. 256 * only when we need to allocate a new super block.
292 */ 257 */
293static int btrfs_parse_early_options(const char *options, fmode_t flags, 258static int btrfs_parse_early_options(const char *options, fmode_t flags,
294 void *holder, char **subvol_name, 259 void *holder, char **subvol_name, u64 *subvol_objectid,
295 struct btrfs_fs_devices **fs_devices) 260 struct btrfs_fs_devices **fs_devices)
296{ 261{
297 substring_t args[MAX_OPT_ARGS]; 262 substring_t args[MAX_OPT_ARGS];
298 char *opts, *p; 263 char *opts, *p;
299 int error = 0; 264 int error = 0;
265 int intarg;
300 266
301 if (!options) 267 if (!options)
302 goto out; 268 goto out;
@@ -319,6 +285,18 @@ static int btrfs_parse_early_options(const char *options, fmode_t flags,
319 case Opt_subvol: 285 case Opt_subvol:
320 *subvol_name = match_strdup(&args[0]); 286 *subvol_name = match_strdup(&args[0]);
321 break; 287 break;
288 case Opt_subvolid:
289 intarg = 0;
290 error = match_int(&args[0], &intarg);
291 if (!error) {
292 /* we want the original fs_tree */
293 if (!intarg)
294 *subvol_objectid =
295 BTRFS_FS_TREE_OBJECTID;
296 else
297 *subvol_objectid = intarg;
298 }
299 break;
322 case Opt_device: 300 case Opt_device:
323 error = btrfs_scan_one_device(match_strdup(&args[0]), 301 error = btrfs_scan_one_device(match_strdup(&args[0]),
324 flags, holder, fs_devices); 302 flags, holder, fs_devices);
@@ -346,6 +324,112 @@ static int btrfs_parse_early_options(const char *options, fmode_t flags,
346 return error; 324 return error;
347} 325}
348 326
327static struct dentry *get_default_root(struct super_block *sb,
328 u64 subvol_objectid)
329{
330 struct btrfs_root *root = sb->s_fs_info;
331 struct btrfs_root *new_root;
332 struct btrfs_dir_item *di;
333 struct btrfs_path *path;
334 struct btrfs_key location;
335 struct inode *inode;
336 struct dentry *dentry;
337 u64 dir_id;
338 int new = 0;
339
340 /*
341 * We have a specific subvol we want to mount, just setup location and
342 * go look up the root.
343 */
344 if (subvol_objectid) {
345 location.objectid = subvol_objectid;
346 location.type = BTRFS_ROOT_ITEM_KEY;
347 location.offset = (u64)-1;
348 goto find_root;
349 }
350
351 path = btrfs_alloc_path();
352 if (!path)
353 return ERR_PTR(-ENOMEM);
354 path->leave_spinning = 1;
355
356 /*
357 * Find the "default" dir item which points to the root item that we
358 * will mount by default if we haven't been given a specific subvolume
359 * to mount.
360 */
361 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
362 di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
363 if (IS_ERR(di))
364 return ERR_CAST(di);
365 if (!di) {
366 /*
367 * Ok the default dir item isn't there. This is weird since
368 * it's always been there, but don't freak out, just try and
369 * mount to root most subvolume.
370 */
371 btrfs_free_path(path);
372 dir_id = BTRFS_FIRST_FREE_OBJECTID;
373 new_root = root->fs_info->fs_root;
374 goto setup_root;
375 }
376
377 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
378 btrfs_free_path(path);
379
380find_root:
381 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
382 if (IS_ERR(new_root))
383 return ERR_PTR(PTR_ERR(new_root));
384
385 if (btrfs_root_refs(&new_root->root_item) == 0)
386 return ERR_PTR(-ENOENT);
387
388 dir_id = btrfs_root_dirid(&new_root->root_item);
389setup_root:
390 location.objectid = dir_id;
391 location.type = BTRFS_INODE_ITEM_KEY;
392 location.offset = 0;
393
394 inode = btrfs_iget(sb, &location, new_root, &new);
395 if (IS_ERR(inode))
396 return ERR_CAST(inode);
397
398 /*
399 * If we're just mounting the root most subvol put the inode and return
400 * a reference to the dentry. We will have already gotten a reference
401 * to the inode in btrfs_fill_super so we're good to go.
402 */
403 if (!new && sb->s_root->d_inode == inode) {
404 iput(inode);
405 return dget(sb->s_root);
406 }
407
408 if (new) {
409 const struct qstr name = { .name = "/", .len = 1 };
410
411 /*
412 * New inode, we need to make the dentry a sibling of s_root so
413 * everything gets cleaned up properly on unmount.
414 */
415 dentry = d_alloc(sb->s_root, &name);
416 if (!dentry) {
417 iput(inode);
418 return ERR_PTR(-ENOMEM);
419 }
420 d_splice_alias(inode, dentry);
421 } else {
422 /*
423 * We found the inode in cache, just find a dentry for it and
424 * put the reference to the inode we just got.
425 */
426 dentry = d_find_alias(inode);
427 iput(inode);
428 }
429
430 return dentry;
431}
432
349static int btrfs_fill_super(struct super_block *sb, 433static int btrfs_fill_super(struct super_block *sb,
350 struct btrfs_fs_devices *fs_devices, 434 struct btrfs_fs_devices *fs_devices,
351 void *data, int silent) 435 void *data, int silent)
@@ -379,7 +463,7 @@ static int btrfs_fill_super(struct super_block *sb,
379 key.objectid = BTRFS_FIRST_FREE_OBJECTID; 463 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
380 key.type = BTRFS_INODE_ITEM_KEY; 464 key.type = BTRFS_INODE_ITEM_KEY;
381 key.offset = 0; 465 key.offset = 0;
382 inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root); 466 inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
383 if (IS_ERR(inode)) { 467 if (IS_ERR(inode)) {
384 err = PTR_ERR(inode); 468 err = PTR_ERR(inode);
385 goto fail_close; 469 goto fail_close;
@@ -391,12 +475,6 @@ static int btrfs_fill_super(struct super_block *sb,
391 err = -ENOMEM; 475 err = -ENOMEM;
392 goto fail_close; 476 goto fail_close;
393 } 477 }
394#if 0
395 /* this does the super kobj at the same time */
396 err = btrfs_sysfs_add_super(tree_root->fs_info);
397 if (err)
398 goto fail_close;
399#endif
400 478
401 sb->s_root = root_dentry; 479 sb->s_root = root_dentry;
402 480
@@ -422,7 +500,7 @@ int btrfs_sync_fs(struct super_block *sb, int wait)
422 btrfs_start_delalloc_inodes(root, 0); 500 btrfs_start_delalloc_inodes(root, 0);
423 btrfs_wait_ordered_extents(root, 0, 0); 501 btrfs_wait_ordered_extents(root, 0, 0);
424 502
425 trans = btrfs_start_transaction(root, 1); 503 trans = btrfs_start_transaction(root, 0);
426 ret = btrfs_commit_transaction(trans, root); 504 ret = btrfs_commit_transaction(trans, root);
427 return ret; 505 return ret;
428} 506}
@@ -440,9 +518,6 @@ static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
440 seq_puts(seq, ",nodatacow"); 518 seq_puts(seq, ",nodatacow");
441 if (btrfs_test_opt(root, NOBARRIER)) 519 if (btrfs_test_opt(root, NOBARRIER))
442 seq_puts(seq, ",nobarrier"); 520 seq_puts(seq, ",nobarrier");
443 if (info->max_extent != (u64)-1)
444 seq_printf(seq, ",max_extent=%llu",
445 (unsigned long long)info->max_extent);
446 if (info->max_inline != 8192 * 1024) 521 if (info->max_inline != 8192 * 1024)
447 seq_printf(seq, ",max_inline=%llu", 522 seq_printf(seq, ",max_inline=%llu",
448 (unsigned long long)info->max_inline); 523 (unsigned long long)info->max_inline);
@@ -488,19 +563,22 @@ static int btrfs_test_super(struct super_block *s, void *data)
488static int btrfs_get_sb(struct file_system_type *fs_type, int flags, 563static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
489 const char *dev_name, void *data, struct vfsmount *mnt) 564 const char *dev_name, void *data, struct vfsmount *mnt)
490{ 565{
491 char *subvol_name = NULL;
492 struct block_device *bdev = NULL; 566 struct block_device *bdev = NULL;
493 struct super_block *s; 567 struct super_block *s;
494 struct dentry *root; 568 struct dentry *root;
495 struct btrfs_fs_devices *fs_devices = NULL; 569 struct btrfs_fs_devices *fs_devices = NULL;
496 fmode_t mode = FMODE_READ; 570 fmode_t mode = FMODE_READ;
571 char *subvol_name = NULL;
572 u64 subvol_objectid = 0;
497 int error = 0; 573 int error = 0;
574 int found = 0;
498 575
499 if (!(flags & MS_RDONLY)) 576 if (!(flags & MS_RDONLY))
500 mode |= FMODE_WRITE; 577 mode |= FMODE_WRITE;
501 578
502 error = btrfs_parse_early_options(data, mode, fs_type, 579 error = btrfs_parse_early_options(data, mode, fs_type,
503 &subvol_name, &fs_devices); 580 &subvol_name, &subvol_objectid,
581 &fs_devices);
504 if (error) 582 if (error)
505 return error; 583 return error;
506 584
@@ -529,6 +607,7 @@ static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
529 goto error_close_devices; 607 goto error_close_devices;
530 } 608 }
531 609
610 found = 1;
532 btrfs_close_devices(fs_devices); 611 btrfs_close_devices(fs_devices);
533 } else { 612 } else {
534 char b[BDEVNAME_SIZE]; 613 char b[BDEVNAME_SIZE];
@@ -546,25 +625,35 @@ static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
546 s->s_flags |= MS_ACTIVE; 625 s->s_flags |= MS_ACTIVE;
547 } 626 }
548 627
549 if (!strcmp(subvol_name, ".")) 628 root = get_default_root(s, subvol_objectid);
550 root = dget(s->s_root); 629 if (IS_ERR(root)) {
551 else { 630 error = PTR_ERR(root);
552 mutex_lock(&s->s_root->d_inode->i_mutex); 631 deactivate_locked_super(s);
553 root = lookup_one_len(subvol_name, s->s_root, 632 goto error;
633 }
634 /* if they gave us a subvolume name bind mount into that */
635 if (strcmp(subvol_name, ".")) {
636 struct dentry *new_root;
637 mutex_lock(&root->d_inode->i_mutex);
638 new_root = lookup_one_len(subvol_name, root,
554 strlen(subvol_name)); 639 strlen(subvol_name));
555 mutex_unlock(&s->s_root->d_inode->i_mutex); 640 mutex_unlock(&root->d_inode->i_mutex);
556 641
557 if (IS_ERR(root)) { 642 if (IS_ERR(new_root)) {
558 deactivate_locked_super(s); 643 deactivate_locked_super(s);
559 error = PTR_ERR(root); 644 error = PTR_ERR(new_root);
560 goto error_free_subvol_name; 645 dput(root);
646 goto error_close_devices;
561 } 647 }
562 if (!root->d_inode) { 648 if (!new_root->d_inode) {
563 dput(root); 649 dput(root);
650 dput(new_root);
564 deactivate_locked_super(s); 651 deactivate_locked_super(s);
565 error = -ENXIO; 652 error = -ENXIO;
566 goto error_free_subvol_name; 653 goto error_close_devices;
567 } 654 }
655 dput(root);
656 root = new_root;
568 } 657 }
569 658
570 mnt->mnt_sb = s; 659 mnt->mnt_sb = s;
@@ -579,6 +668,7 @@ error_close_devices:
579 btrfs_close_devices(fs_devices); 668 btrfs_close_devices(fs_devices);
580error_free_subvol_name: 669error_free_subvol_name:
581 kfree(subvol_name); 670 kfree(subvol_name);
671error:
582 return error; 672 return error;
583} 673}
584 674
@@ -606,11 +696,11 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data)
606 if (btrfs_super_log_root(&root->fs_info->super_copy) != 0) 696 if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
607 return -EINVAL; 697 return -EINVAL;
608 698
609 /* recover relocation */ 699 ret = btrfs_cleanup_fs_roots(root->fs_info);
610 ret = btrfs_recover_relocation(root);
611 WARN_ON(ret); 700 WARN_ON(ret);
612 701
613 ret = btrfs_cleanup_fs_roots(root->fs_info); 702 /* recover relocation */
703 ret = btrfs_recover_relocation(root);
614 WARN_ON(ret); 704 WARN_ON(ret);
615 705
616 sb->s_flags &= ~MS_RDONLY; 706 sb->s_flags &= ~MS_RDONLY;
@@ -623,13 +713,20 @@ static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
623{ 713{
624 struct btrfs_root *root = btrfs_sb(dentry->d_sb); 714 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
625 struct btrfs_super_block *disk_super = &root->fs_info->super_copy; 715 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
716 struct list_head *head = &root->fs_info->space_info;
717 struct btrfs_space_info *found;
718 u64 total_used = 0;
626 int bits = dentry->d_sb->s_blocksize_bits; 719 int bits = dentry->d_sb->s_blocksize_bits;
627 __be32 *fsid = (__be32 *)root->fs_info->fsid; 720 __be32 *fsid = (__be32 *)root->fs_info->fsid;
628 721
722 rcu_read_lock();
723 list_for_each_entry_rcu(found, head, list)
724 total_used += found->disk_used;
725 rcu_read_unlock();
726
629 buf->f_namelen = BTRFS_NAME_LEN; 727 buf->f_namelen = BTRFS_NAME_LEN;
630 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits; 728 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
631 buf->f_bfree = buf->f_blocks - 729 buf->f_bfree = buf->f_blocks - (total_used >> bits);
632 (btrfs_super_bytes_used(disk_super) >> bits);
633 buf->f_bavail = buf->f_bfree; 730 buf->f_bavail = buf->f_bfree;
634 buf->f_bsize = dentry->d_sb->s_blocksize; 731 buf->f_bsize = dentry->d_sb->s_blocksize;
635 buf->f_type = BTRFS_SUPER_MAGIC; 732 buf->f_type = BTRFS_SUPER_MAGIC;
@@ -700,7 +797,7 @@ static int btrfs_unfreeze(struct super_block *sb)
700 797
701static const struct super_operations btrfs_super_ops = { 798static const struct super_operations btrfs_super_ops = {
702 .drop_inode = btrfs_drop_inode, 799 .drop_inode = btrfs_drop_inode,
703 .delete_inode = btrfs_delete_inode, 800 .evict_inode = btrfs_evict_inode,
704 .put_super = btrfs_put_super, 801 .put_super = btrfs_put_super,
705 .sync_fs = btrfs_sync_fs, 802 .sync_fs = btrfs_sync_fs,
706 .show_options = btrfs_show_options, 803 .show_options = btrfs_show_options,
@@ -721,11 +818,14 @@ static const struct file_operations btrfs_ctl_fops = {
721}; 818};
722 819
723static struct miscdevice btrfs_misc = { 820static struct miscdevice btrfs_misc = {
724 .minor = MISC_DYNAMIC_MINOR, 821 .minor = BTRFS_MINOR,
725 .name = "btrfs-control", 822 .name = "btrfs-control",
726 .fops = &btrfs_ctl_fops 823 .fops = &btrfs_ctl_fops
727}; 824};
728 825
826MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
827MODULE_ALIAS("devname:btrfs-control");
828
729static int btrfs_interface_init(void) 829static int btrfs_interface_init(void)
730{ 830{
731 return misc_register(&btrfs_misc); 831 return misc_register(&btrfs_misc);