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.c243
1 files changed, 185 insertions, 58 deletions
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 8a1ea6e64575..9ac612e6ca60 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -63,10 +63,10 @@ static void btrfs_put_super(struct super_block *sb)
63} 63}
64 64
65enum { 65enum {
66 Opt_degraded, Opt_subvol, Opt_device, Opt_nodatasum, Opt_nodatacow, 66 Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
67 Opt_max_extent, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, 67 Opt_nodatacow, Opt_max_extent, Opt_max_inline, Opt_alloc_start,
68 Opt_ssd, Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, 68 Opt_nobarrier, Opt_ssd, Opt_nossd, Opt_ssd_spread, Opt_thread_pool,
69 Opt_compress, Opt_compress_force, Opt_notreelog, Opt_ratio, 69 Opt_noacl, Opt_compress, Opt_compress_force, Opt_notreelog, Opt_ratio,
70 Opt_flushoncommit, 70 Opt_flushoncommit,
71 Opt_discard, Opt_err, 71 Opt_discard, Opt_err,
72}; 72};
@@ -74,6 +74,7 @@ enum {
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"},
@@ -95,31 +96,6 @@ static match_table_t tokens = {
95 {Opt_err, NULL}, 96 {Opt_err, NULL},
96}; 97};
97 98
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/* 99/*
124 * Regular mount options parser. Everything that is needed only when 100 * Regular mount options parser. Everything that is needed only when
125 * reading in a new superblock is parsed here. 101 * reading in a new superblock is parsed here.
@@ -128,7 +104,7 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
128{ 104{
129 struct btrfs_fs_info *info = root->fs_info; 105 struct btrfs_fs_info *info = root->fs_info;
130 substring_t args[MAX_OPT_ARGS]; 106 substring_t args[MAX_OPT_ARGS];
131 char *p, *num; 107 char *p, *num, *orig;
132 int intarg; 108 int intarg;
133 int ret = 0; 109 int ret = 0;
134 110
@@ -143,6 +119,7 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
143 if (!options) 119 if (!options)
144 return -ENOMEM; 120 return -ENOMEM;
145 121
122 orig = options;
146 123
147 while ((p = strsep(&options, ",")) != NULL) { 124 while ((p = strsep(&options, ",")) != NULL) {
148 int token; 125 int token;
@@ -156,6 +133,7 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
156 btrfs_set_opt(info->mount_opt, DEGRADED); 133 btrfs_set_opt(info->mount_opt, DEGRADED);
157 break; 134 break;
158 case Opt_subvol: 135 case Opt_subvol:
136 case Opt_subvolid:
159 case Opt_device: 137 case Opt_device:
160 /* 138 /*
161 * These are parsed by btrfs_parse_early_options 139 * These are parsed by btrfs_parse_early_options
@@ -213,7 +191,7 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
213 case Opt_max_extent: 191 case Opt_max_extent:
214 num = match_strdup(&args[0]); 192 num = match_strdup(&args[0]);
215 if (num) { 193 if (num) {
216 info->max_extent = btrfs_parse_size(num); 194 info->max_extent = memparse(num, NULL);
217 kfree(num); 195 kfree(num);
218 196
219 info->max_extent = max_t(u64, 197 info->max_extent = max_t(u64,
@@ -225,7 +203,7 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
225 case Opt_max_inline: 203 case Opt_max_inline:
226 num = match_strdup(&args[0]); 204 num = match_strdup(&args[0]);
227 if (num) { 205 if (num) {
228 info->max_inline = btrfs_parse_size(num); 206 info->max_inline = memparse(num, NULL);
229 kfree(num); 207 kfree(num);
230 208
231 if (info->max_inline) { 209 if (info->max_inline) {
@@ -240,7 +218,7 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
240 case Opt_alloc_start: 218 case Opt_alloc_start:
241 num = match_strdup(&args[0]); 219 num = match_strdup(&args[0]);
242 if (num) { 220 if (num) {
243 info->alloc_start = btrfs_parse_size(num); 221 info->alloc_start = memparse(num, NULL);
244 kfree(num); 222 kfree(num);
245 printk(KERN_INFO 223 printk(KERN_INFO
246 "btrfs: allocations start at %llu\n", 224 "btrfs: allocations start at %llu\n",
@@ -280,7 +258,7 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
280 } 258 }
281 } 259 }
282out: 260out:
283 kfree(options); 261 kfree(orig);
284 return ret; 262 return ret;
285} 263}
286 264
@@ -291,12 +269,13 @@ out:
291 * only when we need to allocate a new super block. 269 * only when we need to allocate a new super block.
292 */ 270 */
293static int btrfs_parse_early_options(const char *options, fmode_t flags, 271static int btrfs_parse_early_options(const char *options, fmode_t flags,
294 void *holder, char **subvol_name, 272 void *holder, char **subvol_name, u64 *subvol_objectid,
295 struct btrfs_fs_devices **fs_devices) 273 struct btrfs_fs_devices **fs_devices)
296{ 274{
297 substring_t args[MAX_OPT_ARGS]; 275 substring_t args[MAX_OPT_ARGS];
298 char *opts, *p; 276 char *opts, *p;
299 int error = 0; 277 int error = 0;
278 int intarg;
300 279
301 if (!options) 280 if (!options)
302 goto out; 281 goto out;
@@ -319,6 +298,18 @@ static int btrfs_parse_early_options(const char *options, fmode_t flags,
319 case Opt_subvol: 298 case Opt_subvol:
320 *subvol_name = match_strdup(&args[0]); 299 *subvol_name = match_strdup(&args[0]);
321 break; 300 break;
301 case Opt_subvolid:
302 intarg = 0;
303 error = match_int(&args[0], &intarg);
304 if (!error) {
305 /* we want the original fs_tree */
306 if (!intarg)
307 *subvol_objectid =
308 BTRFS_FS_TREE_OBJECTID;
309 else
310 *subvol_objectid = intarg;
311 }
312 break;
322 case Opt_device: 313 case Opt_device:
323 error = btrfs_scan_one_device(match_strdup(&args[0]), 314 error = btrfs_scan_one_device(match_strdup(&args[0]),
324 flags, holder, fs_devices); 315 flags, holder, fs_devices);
@@ -346,6 +337,110 @@ static int btrfs_parse_early_options(const char *options, fmode_t flags,
346 return error; 337 return error;
347} 338}
348 339
340static struct dentry *get_default_root(struct super_block *sb,
341 u64 subvol_objectid)
342{
343 struct btrfs_root *root = sb->s_fs_info;
344 struct btrfs_root *new_root;
345 struct btrfs_dir_item *di;
346 struct btrfs_path *path;
347 struct btrfs_key location;
348 struct inode *inode;
349 struct dentry *dentry;
350 u64 dir_id;
351 int new = 0;
352
353 /*
354 * We have a specific subvol we want to mount, just setup location and
355 * go look up the root.
356 */
357 if (subvol_objectid) {
358 location.objectid = subvol_objectid;
359 location.type = BTRFS_ROOT_ITEM_KEY;
360 location.offset = (u64)-1;
361 goto find_root;
362 }
363
364 path = btrfs_alloc_path();
365 if (!path)
366 return ERR_PTR(-ENOMEM);
367 path->leave_spinning = 1;
368
369 /*
370 * Find the "default" dir item which points to the root item that we
371 * will mount by default if we haven't been given a specific subvolume
372 * to mount.
373 */
374 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
375 di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
376 if (!di) {
377 /*
378 * Ok the default dir item isn't there. This is weird since
379 * it's always been there, but don't freak out, just try and
380 * mount to root most subvolume.
381 */
382 btrfs_free_path(path);
383 dir_id = BTRFS_FIRST_FREE_OBJECTID;
384 new_root = root->fs_info->fs_root;
385 goto setup_root;
386 }
387
388 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
389 btrfs_free_path(path);
390
391find_root:
392 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
393 if (IS_ERR(new_root))
394 return ERR_PTR(PTR_ERR(new_root));
395
396 if (btrfs_root_refs(&new_root->root_item) == 0)
397 return ERR_PTR(-ENOENT);
398
399 dir_id = btrfs_root_dirid(&new_root->root_item);
400setup_root:
401 location.objectid = dir_id;
402 location.type = BTRFS_INODE_ITEM_KEY;
403 location.offset = 0;
404
405 inode = btrfs_iget(sb, &location, new_root, &new);
406 if (!inode)
407 return ERR_PTR(-ENOMEM);
408
409 /*
410 * If we're just mounting the root most subvol put the inode and return
411 * a reference to the dentry. We will have already gotten a reference
412 * to the inode in btrfs_fill_super so we're good to go.
413 */
414 if (!new && sb->s_root->d_inode == inode) {
415 iput(inode);
416 return dget(sb->s_root);
417 }
418
419 if (new) {
420 const struct qstr name = { .name = "/", .len = 1 };
421
422 /*
423 * New inode, we need to make the dentry a sibling of s_root so
424 * everything gets cleaned up properly on unmount.
425 */
426 dentry = d_alloc(sb->s_root, &name);
427 if (!dentry) {
428 iput(inode);
429 return ERR_PTR(-ENOMEM);
430 }
431 d_splice_alias(inode, dentry);
432 } else {
433 /*
434 * We found the inode in cache, just find a dentry for it and
435 * put the reference to the inode we just got.
436 */
437 dentry = d_find_alias(inode);
438 iput(inode);
439 }
440
441 return dentry;
442}
443
349static int btrfs_fill_super(struct super_block *sb, 444static int btrfs_fill_super(struct super_block *sb,
350 struct btrfs_fs_devices *fs_devices, 445 struct btrfs_fs_devices *fs_devices,
351 void *data, int silent) 446 void *data, int silent)
@@ -379,7 +474,7 @@ static int btrfs_fill_super(struct super_block *sb,
379 key.objectid = BTRFS_FIRST_FREE_OBJECTID; 474 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
380 key.type = BTRFS_INODE_ITEM_KEY; 475 key.type = BTRFS_INODE_ITEM_KEY;
381 key.offset = 0; 476 key.offset = 0;
382 inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root); 477 inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
383 if (IS_ERR(inode)) { 478 if (IS_ERR(inode)) {
384 err = PTR_ERR(inode); 479 err = PTR_ERR(inode);
385 goto fail_close; 480 goto fail_close;
@@ -391,12 +486,6 @@ static int btrfs_fill_super(struct super_block *sb,
391 err = -ENOMEM; 486 err = -ENOMEM;
392 goto fail_close; 487 goto fail_close;
393 } 488 }
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 489
401 sb->s_root = root_dentry; 490 sb->s_root = root_dentry;
402 491
@@ -488,19 +577,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, 577static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
489 const char *dev_name, void *data, struct vfsmount *mnt) 578 const char *dev_name, void *data, struct vfsmount *mnt)
490{ 579{
491 char *subvol_name = NULL;
492 struct block_device *bdev = NULL; 580 struct block_device *bdev = NULL;
493 struct super_block *s; 581 struct super_block *s;
494 struct dentry *root; 582 struct dentry *root;
495 struct btrfs_fs_devices *fs_devices = NULL; 583 struct btrfs_fs_devices *fs_devices = NULL;
496 fmode_t mode = FMODE_READ; 584 fmode_t mode = FMODE_READ;
585 char *subvol_name = NULL;
586 u64 subvol_objectid = 0;
497 int error = 0; 587 int error = 0;
588 int found = 0;
498 589
499 if (!(flags & MS_RDONLY)) 590 if (!(flags & MS_RDONLY))
500 mode |= FMODE_WRITE; 591 mode |= FMODE_WRITE;
501 592
502 error = btrfs_parse_early_options(data, mode, fs_type, 593 error = btrfs_parse_early_options(data, mode, fs_type,
503 &subvol_name, &fs_devices); 594 &subvol_name, &subvol_objectid,
595 &fs_devices);
504 if (error) 596 if (error)
505 return error; 597 return error;
506 598
@@ -529,6 +621,7 @@ static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
529 goto error_close_devices; 621 goto error_close_devices;
530 } 622 }
531 623
624 found = 1;
532 btrfs_close_devices(fs_devices); 625 btrfs_close_devices(fs_devices);
533 } else { 626 } else {
534 char b[BDEVNAME_SIZE]; 627 char b[BDEVNAME_SIZE];
@@ -546,25 +639,35 @@ static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
546 s->s_flags |= MS_ACTIVE; 639 s->s_flags |= MS_ACTIVE;
547 } 640 }
548 641
549 if (!strcmp(subvol_name, ".")) 642 root = get_default_root(s, subvol_objectid);
550 root = dget(s->s_root); 643 if (IS_ERR(root)) {
551 else { 644 error = PTR_ERR(root);
552 mutex_lock(&s->s_root->d_inode->i_mutex); 645 deactivate_locked_super(s);
553 root = lookup_one_len(subvol_name, s->s_root, 646 goto error;
647 }
648 /* if they gave us a subvolume name bind mount into that */
649 if (strcmp(subvol_name, ".")) {
650 struct dentry *new_root;
651 mutex_lock(&root->d_inode->i_mutex);
652 new_root = lookup_one_len(subvol_name, root,
554 strlen(subvol_name)); 653 strlen(subvol_name));
555 mutex_unlock(&s->s_root->d_inode->i_mutex); 654 mutex_unlock(&root->d_inode->i_mutex);
556 655
557 if (IS_ERR(root)) { 656 if (IS_ERR(new_root)) {
558 deactivate_locked_super(s); 657 deactivate_locked_super(s);
559 error = PTR_ERR(root); 658 error = PTR_ERR(new_root);
560 goto error_free_subvol_name; 659 dput(root);
660 goto error_close_devices;
561 } 661 }
562 if (!root->d_inode) { 662 if (!new_root->d_inode) {
563 dput(root); 663 dput(root);
664 dput(new_root);
564 deactivate_locked_super(s); 665 deactivate_locked_super(s);
565 error = -ENXIO; 666 error = -ENXIO;
566 goto error_free_subvol_name; 667 goto error_close_devices;
567 } 668 }
669 dput(root);
670 root = new_root;
568 } 671 }
569 672
570 mnt->mnt_sb = s; 673 mnt->mnt_sb = s;
@@ -579,6 +682,7 @@ error_close_devices:
579 btrfs_close_devices(fs_devices); 682 btrfs_close_devices(fs_devices);
580error_free_subvol_name: 683error_free_subvol_name:
581 kfree(subvol_name); 684 kfree(subvol_name);
685error:
582 return error; 686 return error;
583} 687}
584 688
@@ -623,14 +727,37 @@ static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
623{ 727{
624 struct btrfs_root *root = btrfs_sb(dentry->d_sb); 728 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
625 struct btrfs_super_block *disk_super = &root->fs_info->super_copy; 729 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
730 struct list_head *head = &root->fs_info->space_info;
731 struct btrfs_space_info *found;
732 u64 total_used = 0;
733 u64 data_used = 0;
626 int bits = dentry->d_sb->s_blocksize_bits; 734 int bits = dentry->d_sb->s_blocksize_bits;
627 __be32 *fsid = (__be32 *)root->fs_info->fsid; 735 __be32 *fsid = (__be32 *)root->fs_info->fsid;
628 736
737 rcu_read_lock();
738 list_for_each_entry_rcu(found, head, list) {
739 if (found->flags & (BTRFS_BLOCK_GROUP_DUP|
740 BTRFS_BLOCK_GROUP_RAID10|
741 BTRFS_BLOCK_GROUP_RAID1)) {
742 total_used += found->bytes_used;
743 if (found->flags & BTRFS_BLOCK_GROUP_DATA)
744 data_used += found->bytes_used;
745 else
746 data_used += found->total_bytes;
747 }
748
749 total_used += found->bytes_used;
750 if (found->flags & BTRFS_BLOCK_GROUP_DATA)
751 data_used += found->bytes_used;
752 else
753 data_used += found->total_bytes;
754 }
755 rcu_read_unlock();
756
629 buf->f_namelen = BTRFS_NAME_LEN; 757 buf->f_namelen = BTRFS_NAME_LEN;
630 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits; 758 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
631 buf->f_bfree = buf->f_blocks - 759 buf->f_bfree = buf->f_blocks - (total_used >> bits);
632 (btrfs_super_bytes_used(disk_super) >> bits); 760 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; 761 buf->f_bsize = dentry->d_sb->s_blocksize;
635 buf->f_type = BTRFS_SUPER_MAGIC; 762 buf->f_type = BTRFS_SUPER_MAGIC;
636 763