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.c259
1 files changed, 185 insertions, 74 deletions
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 8a1ea6e64575..1866dff0538e 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,110 @@ 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 (!di) {
364 /*
365 * Ok the default dir item isn't there. This is weird since
366 * it's always been there, but don't freak out, just try and
367 * mount to root most subvolume.
368 */
369 btrfs_free_path(path);
370 dir_id = BTRFS_FIRST_FREE_OBJECTID;
371 new_root = root->fs_info->fs_root;
372 goto setup_root;
373 }
374
375 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
376 btrfs_free_path(path);
377
378find_root:
379 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
380 if (IS_ERR(new_root))
381 return ERR_PTR(PTR_ERR(new_root));
382
383 if (btrfs_root_refs(&new_root->root_item) == 0)
384 return ERR_PTR(-ENOENT);
385
386 dir_id = btrfs_root_dirid(&new_root->root_item);
387setup_root:
388 location.objectid = dir_id;
389 location.type = BTRFS_INODE_ITEM_KEY;
390 location.offset = 0;
391
392 inode = btrfs_iget(sb, &location, new_root, &new);
393 if (!inode)
394 return ERR_PTR(-ENOMEM);
395
396 /*
397 * If we're just mounting the root most subvol put the inode and return
398 * a reference to the dentry. We will have already gotten a reference
399 * to the inode in btrfs_fill_super so we're good to go.
400 */
401 if (!new && sb->s_root->d_inode == inode) {
402 iput(inode);
403 return dget(sb->s_root);
404 }
405
406 if (new) {
407 const struct qstr name = { .name = "/", .len = 1 };
408
409 /*
410 * New inode, we need to make the dentry a sibling of s_root so
411 * everything gets cleaned up properly on unmount.
412 */
413 dentry = d_alloc(sb->s_root, &name);
414 if (!dentry) {
415 iput(inode);
416 return ERR_PTR(-ENOMEM);
417 }
418 d_splice_alias(inode, dentry);
419 } else {
420 /*
421 * We found the inode in cache, just find a dentry for it and
422 * put the reference to the inode we just got.
423 */
424 dentry = d_find_alias(inode);
425 iput(inode);
426 }
427
428 return dentry;
429}
430
349static int btrfs_fill_super(struct super_block *sb, 431static int btrfs_fill_super(struct super_block *sb,
350 struct btrfs_fs_devices *fs_devices, 432 struct btrfs_fs_devices *fs_devices,
351 void *data, int silent) 433 void *data, int silent)
@@ -379,7 +461,7 @@ static int btrfs_fill_super(struct super_block *sb,
379 key.objectid = BTRFS_FIRST_FREE_OBJECTID; 461 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
380 key.type = BTRFS_INODE_ITEM_KEY; 462 key.type = BTRFS_INODE_ITEM_KEY;
381 key.offset = 0; 463 key.offset = 0;
382 inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root); 464 inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
383 if (IS_ERR(inode)) { 465 if (IS_ERR(inode)) {
384 err = PTR_ERR(inode); 466 err = PTR_ERR(inode);
385 goto fail_close; 467 goto fail_close;
@@ -391,12 +473,6 @@ static int btrfs_fill_super(struct super_block *sb,
391 err = -ENOMEM; 473 err = -ENOMEM;
392 goto fail_close; 474 goto fail_close;
393 } 475 }
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 476
401 sb->s_root = root_dentry; 477 sb->s_root = root_dentry;
402 478
@@ -440,9 +516,6 @@ static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
440 seq_puts(seq, ",nodatacow"); 516 seq_puts(seq, ",nodatacow");
441 if (btrfs_test_opt(root, NOBARRIER)) 517 if (btrfs_test_opt(root, NOBARRIER))
442 seq_puts(seq, ",nobarrier"); 518 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) 519 if (info->max_inline != 8192 * 1024)
447 seq_printf(seq, ",max_inline=%llu", 520 seq_printf(seq, ",max_inline=%llu",
448 (unsigned long long)info->max_inline); 521 (unsigned long long)info->max_inline);
@@ -488,19 +561,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, 561static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
489 const char *dev_name, void *data, struct vfsmount *mnt) 562 const char *dev_name, void *data, struct vfsmount *mnt)
490{ 563{
491 char *subvol_name = NULL;
492 struct block_device *bdev = NULL; 564 struct block_device *bdev = NULL;
493 struct super_block *s; 565 struct super_block *s;
494 struct dentry *root; 566 struct dentry *root;
495 struct btrfs_fs_devices *fs_devices = NULL; 567 struct btrfs_fs_devices *fs_devices = NULL;
496 fmode_t mode = FMODE_READ; 568 fmode_t mode = FMODE_READ;
569 char *subvol_name = NULL;
570 u64 subvol_objectid = 0;
497 int error = 0; 571 int error = 0;
572 int found = 0;
498 573
499 if (!(flags & MS_RDONLY)) 574 if (!(flags & MS_RDONLY))
500 mode |= FMODE_WRITE; 575 mode |= FMODE_WRITE;
501 576
502 error = btrfs_parse_early_options(data, mode, fs_type, 577 error = btrfs_parse_early_options(data, mode, fs_type,
503 &subvol_name, &fs_devices); 578 &subvol_name, &subvol_objectid,
579 &fs_devices);
504 if (error) 580 if (error)
505 return error; 581 return error;
506 582
@@ -529,6 +605,7 @@ static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
529 goto error_close_devices; 605 goto error_close_devices;
530 } 606 }
531 607
608 found = 1;
532 btrfs_close_devices(fs_devices); 609 btrfs_close_devices(fs_devices);
533 } else { 610 } else {
534 char b[BDEVNAME_SIZE]; 611 char b[BDEVNAME_SIZE];
@@ -546,25 +623,35 @@ static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
546 s->s_flags |= MS_ACTIVE; 623 s->s_flags |= MS_ACTIVE;
547 } 624 }
548 625
549 if (!strcmp(subvol_name, ".")) 626 root = get_default_root(s, subvol_objectid);
550 root = dget(s->s_root); 627 if (IS_ERR(root)) {
551 else { 628 error = PTR_ERR(root);
552 mutex_lock(&s->s_root->d_inode->i_mutex); 629 deactivate_locked_super(s);
553 root = lookup_one_len(subvol_name, s->s_root, 630 goto error;
631 }
632 /* if they gave us a subvolume name bind mount into that */
633 if (strcmp(subvol_name, ".")) {
634 struct dentry *new_root;
635 mutex_lock(&root->d_inode->i_mutex);
636 new_root = lookup_one_len(subvol_name, root,
554 strlen(subvol_name)); 637 strlen(subvol_name));
555 mutex_unlock(&s->s_root->d_inode->i_mutex); 638 mutex_unlock(&root->d_inode->i_mutex);
556 639
557 if (IS_ERR(root)) { 640 if (IS_ERR(new_root)) {
558 deactivate_locked_super(s); 641 deactivate_locked_super(s);
559 error = PTR_ERR(root); 642 error = PTR_ERR(new_root);
560 goto error_free_subvol_name; 643 dput(root);
644 goto error_close_devices;
561 } 645 }
562 if (!root->d_inode) { 646 if (!new_root->d_inode) {
563 dput(root); 647 dput(root);
648 dput(new_root);
564 deactivate_locked_super(s); 649 deactivate_locked_super(s);
565 error = -ENXIO; 650 error = -ENXIO;
566 goto error_free_subvol_name; 651 goto error_close_devices;
567 } 652 }
653 dput(root);
654 root = new_root;
568 } 655 }
569 656
570 mnt->mnt_sb = s; 657 mnt->mnt_sb = s;
@@ -579,6 +666,7 @@ error_close_devices:
579 btrfs_close_devices(fs_devices); 666 btrfs_close_devices(fs_devices);
580error_free_subvol_name: 667error_free_subvol_name:
581 kfree(subvol_name); 668 kfree(subvol_name);
669error:
582 return error; 670 return error;
583} 671}
584 672
@@ -623,14 +711,37 @@ static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
623{ 711{
624 struct btrfs_root *root = btrfs_sb(dentry->d_sb); 712 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
625 struct btrfs_super_block *disk_super = &root->fs_info->super_copy; 713 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
714 struct list_head *head = &root->fs_info->space_info;
715 struct btrfs_space_info *found;
716 u64 total_used = 0;
717 u64 data_used = 0;
626 int bits = dentry->d_sb->s_blocksize_bits; 718 int bits = dentry->d_sb->s_blocksize_bits;
627 __be32 *fsid = (__be32 *)root->fs_info->fsid; 719 __be32 *fsid = (__be32 *)root->fs_info->fsid;
628 720
721 rcu_read_lock();
722 list_for_each_entry_rcu(found, head, list) {
723 if (found->flags & (BTRFS_BLOCK_GROUP_DUP|
724 BTRFS_BLOCK_GROUP_RAID10|
725 BTRFS_BLOCK_GROUP_RAID1)) {
726 total_used += found->bytes_used;
727 if (found->flags & BTRFS_BLOCK_GROUP_DATA)
728 data_used += found->bytes_used;
729 else
730 data_used += found->total_bytes;
731 }
732
733 total_used += found->bytes_used;
734 if (found->flags & BTRFS_BLOCK_GROUP_DATA)
735 data_used += found->bytes_used;
736 else
737 data_used += found->total_bytes;
738 }
739 rcu_read_unlock();
740
629 buf->f_namelen = BTRFS_NAME_LEN; 741 buf->f_namelen = BTRFS_NAME_LEN;
630 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits; 742 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
631 buf->f_bfree = buf->f_blocks - 743 buf->f_bfree = buf->f_blocks - (total_used >> bits);
632 (btrfs_super_bytes_used(disk_super) >> bits); 744 buf->f_bavail = buf->f_blocks - (data_used >> bits);
633 buf->f_bavail = buf->f_bfree;
634 buf->f_bsize = dentry->d_sb->s_blocksize; 745 buf->f_bsize = dentry->d_sb->s_blocksize;
635 buf->f_type = BTRFS_SUPER_MAGIC; 746 buf->f_type = BTRFS_SUPER_MAGIC;
636 747