aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/ioctl.c
diff options
context:
space:
mode:
authorAndrea Bastoni <bastoni@cs.unc.edu>2010-05-30 19:16:45 -0400
committerAndrea Bastoni <bastoni@cs.unc.edu>2010-05-30 19:16:45 -0400
commitada47b5fe13d89735805b566185f4885f5a3f750 (patch)
tree644b88f8a71896307d71438e9b3af49126ffb22b /fs/btrfs/ioctl.c
parent43e98717ad40a4ae64545b5ba047c7b86aa44f4f (diff)
parent3280f21d43ee541f97f8cda5792150d2dbec20d5 (diff)
Merge branch 'wip-2.6.34' into old-private-masterarchived-private-master
Diffstat (limited to 'fs/btrfs/ioctl.c')
-rw-r--r--fs/btrfs/ioctl.c749
1 files changed, 718 insertions, 31 deletions
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index cdbb054102b9..97a97839a867 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -39,6 +39,7 @@
39#include <linux/security.h> 39#include <linux/security.h>
40#include <linux/xattr.h> 40#include <linux/xattr.h>
41#include <linux/vmalloc.h> 41#include <linux/vmalloc.h>
42#include <linux/slab.h>
42#include "compat.h" 43#include "compat.h"
43#include "ctree.h" 44#include "ctree.h"
44#include "disk-io.h" 45#include "disk-io.h"
@@ -237,7 +238,6 @@ static noinline int create_subvol(struct btrfs_root *root,
237 u64 objectid; 238 u64 objectid;
238 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; 239 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
239 u64 index = 0; 240 u64 index = 0;
240 unsigned long nr = 1;
241 241
242 /* 242 /*
243 * 1 - inode item 243 * 1 - inode item
@@ -290,7 +290,7 @@ static noinline int create_subvol(struct btrfs_root *root,
290 btrfs_set_root_generation(&root_item, trans->transid); 290 btrfs_set_root_generation(&root_item, trans->transid);
291 btrfs_set_root_level(&root_item, 0); 291 btrfs_set_root_level(&root_item, 0);
292 btrfs_set_root_refs(&root_item, 1); 292 btrfs_set_root_refs(&root_item, 1);
293 btrfs_set_root_used(&root_item, 0); 293 btrfs_set_root_used(&root_item, leaf->len);
294 btrfs_set_root_last_snapshot(&root_item, 0); 294 btrfs_set_root_last_snapshot(&root_item, 0);
295 295
296 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); 296 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
@@ -342,24 +342,21 @@ static noinline int create_subvol(struct btrfs_root *root,
342 342
343 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry)); 343 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
344fail: 344fail:
345 nr = trans->blocks_used;
346 err = btrfs_commit_transaction(trans, root); 345 err = btrfs_commit_transaction(trans, root);
347 if (err && !ret) 346 if (err && !ret)
348 ret = err; 347 ret = err;
349 348
350 btrfs_unreserve_metadata_space(root, 6); 349 btrfs_unreserve_metadata_space(root, 6);
351 btrfs_btree_balance_dirty(root, nr);
352 return ret; 350 return ret;
353} 351}
354 352
355static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, 353static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
356 char *name, int namelen) 354 char *name, int namelen)
357{ 355{
356 struct inode *inode;
358 struct btrfs_pending_snapshot *pending_snapshot; 357 struct btrfs_pending_snapshot *pending_snapshot;
359 struct btrfs_trans_handle *trans; 358 struct btrfs_trans_handle *trans;
360 int ret = 0; 359 int ret;
361 int err;
362 unsigned long nr = 0;
363 360
364 if (!root->ref_cows) 361 if (!root->ref_cows)
365 return -EINVAL; 362 return -EINVAL;
@@ -372,20 +369,20 @@ static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
372 */ 369 */
373 ret = btrfs_reserve_metadata_space(root, 6); 370 ret = btrfs_reserve_metadata_space(root, 6);
374 if (ret) 371 if (ret)
375 goto fail_unlock; 372 goto fail;
376 373
377 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); 374 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
378 if (!pending_snapshot) { 375 if (!pending_snapshot) {
379 ret = -ENOMEM; 376 ret = -ENOMEM;
380 btrfs_unreserve_metadata_space(root, 6); 377 btrfs_unreserve_metadata_space(root, 6);
381 goto fail_unlock; 378 goto fail;
382 } 379 }
383 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS); 380 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
384 if (!pending_snapshot->name) { 381 if (!pending_snapshot->name) {
385 ret = -ENOMEM; 382 ret = -ENOMEM;
386 kfree(pending_snapshot); 383 kfree(pending_snapshot);
387 btrfs_unreserve_metadata_space(root, 6); 384 btrfs_unreserve_metadata_space(root, 6);
388 goto fail_unlock; 385 goto fail;
389 } 386 }
390 memcpy(pending_snapshot->name, name, namelen); 387 memcpy(pending_snapshot->name, name, namelen);
391 pending_snapshot->name[namelen] = '\0'; 388 pending_snapshot->name[namelen] = '\0';
@@ -395,10 +392,19 @@ static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
395 pending_snapshot->root = root; 392 pending_snapshot->root = root;
396 list_add(&pending_snapshot->list, 393 list_add(&pending_snapshot->list,
397 &trans->transaction->pending_snapshots); 394 &trans->transaction->pending_snapshots);
398 err = btrfs_commit_transaction(trans, root); 395 ret = btrfs_commit_transaction(trans, root);
396 BUG_ON(ret);
397 btrfs_unreserve_metadata_space(root, 6);
399 398
400fail_unlock: 399 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
401 btrfs_btree_balance_dirty(root, nr); 400 if (IS_ERR(inode)) {
401 ret = PTR_ERR(inode);
402 goto fail;
403 }
404 BUG_ON(!inode);
405 d_instantiate(dentry, inode);
406 ret = 0;
407fail:
402 return ret; 408 return ret;
403} 409}
404 410
@@ -469,7 +475,79 @@ out_unlock:
469 return error; 475 return error;
470} 476}
471 477
472static int btrfs_defrag_file(struct file *file) 478static int should_defrag_range(struct inode *inode, u64 start, u64 len,
479 int thresh, u64 *last_len, u64 *skip,
480 u64 *defrag_end)
481{
482 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
483 struct extent_map *em = NULL;
484 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
485 int ret = 1;
486
487
488 if (thresh == 0)
489 thresh = 256 * 1024;
490
491 /*
492 * make sure that once we start defragging and extent, we keep on
493 * defragging it
494 */
495 if (start < *defrag_end)
496 return 1;
497
498 *skip = 0;
499
500 /*
501 * hopefully we have this extent in the tree already, try without
502 * the full extent lock
503 */
504 read_lock(&em_tree->lock);
505 em = lookup_extent_mapping(em_tree, start, len);
506 read_unlock(&em_tree->lock);
507
508 if (!em) {
509 /* get the big lock and read metadata off disk */
510 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
511 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
512 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
513
514 if (IS_ERR(em))
515 return 0;
516 }
517
518 /* this will cover holes, and inline extents */
519 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
520 ret = 0;
521
522 /*
523 * we hit a real extent, if it is big don't bother defragging it again
524 */
525 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
526 ret = 0;
527
528 /*
529 * last_len ends up being a counter of how many bytes we've defragged.
530 * every time we choose not to defrag an extent, we reset *last_len
531 * so that the next tiny extent will force a defrag.
532 *
533 * The end result of this is that tiny extents before a single big
534 * extent will force at least part of that big extent to be defragged.
535 */
536 if (ret) {
537 *last_len += len;
538 *defrag_end = extent_map_end(em);
539 } else {
540 *last_len = 0;
541 *skip = extent_map_end(em);
542 *defrag_end = 0;
543 }
544
545 free_extent_map(em);
546 return ret;
547}
548
549static int btrfs_defrag_file(struct file *file,
550 struct btrfs_ioctl_defrag_range_args *range)
473{ 551{
474 struct inode *inode = fdentry(file)->d_inode; 552 struct inode *inode = fdentry(file)->d_inode;
475 struct btrfs_root *root = BTRFS_I(inode)->root; 553 struct btrfs_root *root = BTRFS_I(inode)->root;
@@ -481,37 +559,96 @@ static int btrfs_defrag_file(struct file *file)
481 unsigned long total_read = 0; 559 unsigned long total_read = 0;
482 u64 page_start; 560 u64 page_start;
483 u64 page_end; 561 u64 page_end;
562 u64 last_len = 0;
563 u64 skip = 0;
564 u64 defrag_end = 0;
484 unsigned long i; 565 unsigned long i;
485 int ret; 566 int ret;
486 567
487 ret = btrfs_check_data_free_space(root, inode, inode->i_size); 568 if (inode->i_size == 0)
488 if (ret) 569 return 0;
489 return -ENOSPC; 570
571 if (range->start + range->len > range->start) {
572 last_index = min_t(u64, inode->i_size - 1,
573 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
574 } else {
575 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
576 }
577
578 i = range->start >> PAGE_CACHE_SHIFT;
579 while (i <= last_index) {
580 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
581 PAGE_CACHE_SIZE,
582 range->extent_thresh,
583 &last_len, &skip,
584 &defrag_end)) {
585 unsigned long next;
586 /*
587 * the should_defrag function tells us how much to skip
588 * bump our counter by the suggested amount
589 */
590 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
591 i = max(i + 1, next);
592 continue;
593 }
490 594
491 mutex_lock(&inode->i_mutex);
492 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
493 for (i = 0; i <= last_index; i++) {
494 if (total_read % ra_pages == 0) { 595 if (total_read % ra_pages == 0) {
495 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i, 596 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
496 min(last_index, i + ra_pages - 1)); 597 min(last_index, i + ra_pages - 1));
497 } 598 }
498 total_read++; 599 total_read++;
600 mutex_lock(&inode->i_mutex);
601 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
602 BTRFS_I(inode)->force_compress = 1;
603
604 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
605 if (ret) {
606 ret = -ENOSPC;
607 break;
608 }
609
610 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
611 if (ret) {
612 btrfs_free_reserved_data_space(root, inode,
613 PAGE_CACHE_SIZE);
614 ret = -ENOSPC;
615 break;
616 }
499again: 617again:
618 if (inode->i_size == 0 ||
619 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
620 ret = 0;
621 goto err_reservations;
622 }
623
500 page = grab_cache_page(inode->i_mapping, i); 624 page = grab_cache_page(inode->i_mapping, i);
501 if (!page) 625 if (!page)
502 goto out_unlock; 626 goto err_reservations;
627
503 if (!PageUptodate(page)) { 628 if (!PageUptodate(page)) {
504 btrfs_readpage(NULL, page); 629 btrfs_readpage(NULL, page);
505 lock_page(page); 630 lock_page(page);
506 if (!PageUptodate(page)) { 631 if (!PageUptodate(page)) {
507 unlock_page(page); 632 unlock_page(page);
508 page_cache_release(page); 633 page_cache_release(page);
509 goto out_unlock; 634 goto err_reservations;
510 } 635 }
511 } 636 }
512 637
638 if (page->mapping != inode->i_mapping) {
639 unlock_page(page);
640 page_cache_release(page);
641 goto again;
642 }
643
513 wait_on_page_writeback(page); 644 wait_on_page_writeback(page);
514 645
646 if (PageDirty(page)) {
647 btrfs_free_reserved_data_space(root, inode,
648 PAGE_CACHE_SIZE);
649 goto loop_unlock;
650 }
651
515 page_start = (u64)page->index << PAGE_CACHE_SHIFT; 652 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
516 page_end = page_start + PAGE_CACHE_SIZE - 1; 653 page_end = page_start + PAGE_CACHE_SIZE - 1;
517 lock_extent(io_tree, page_start, page_end, GFP_NOFS); 654 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
@@ -532,18 +669,54 @@ again:
532 * page if it is dirtied again later 669 * page if it is dirtied again later
533 */ 670 */
534 clear_page_dirty_for_io(page); 671 clear_page_dirty_for_io(page);
672 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
673 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
674 EXTENT_DO_ACCOUNTING, GFP_NOFS);
535 675
536 btrfs_set_extent_delalloc(inode, page_start, page_end); 676 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
677 ClearPageChecked(page);
537 set_page_dirty(page); 678 set_page_dirty(page);
538 unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 679 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
680
681loop_unlock:
539 unlock_page(page); 682 unlock_page(page);
540 page_cache_release(page); 683 page_cache_release(page);
684 mutex_unlock(&inode->i_mutex);
685
686 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
541 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1); 687 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
688 i++;
689 }
690
691 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
692 filemap_flush(inode->i_mapping);
693
694 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
695 /* the filemap_flush will queue IO into the worker threads, but
696 * we have to make sure the IO is actually started and that
697 * ordered extents get created before we return
698 */
699 atomic_inc(&root->fs_info->async_submit_draining);
700 while (atomic_read(&root->fs_info->nr_async_submits) ||
701 atomic_read(&root->fs_info->async_delalloc_pages)) {
702 wait_event(root->fs_info->async_submit_wait,
703 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
704 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
705 }
706 atomic_dec(&root->fs_info->async_submit_draining);
707
708 mutex_lock(&inode->i_mutex);
709 BTRFS_I(inode)->force_compress = 0;
710 mutex_unlock(&inode->i_mutex);
542 } 711 }
543 712
544out_unlock:
545 mutex_unlock(&inode->i_mutex);
546 return 0; 713 return 0;
714
715err_reservations:
716 mutex_unlock(&inode->i_mutex);
717 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
718 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
719 return ret;
547} 720}
548 721
549static noinline int btrfs_ioctl_resize(struct btrfs_root *root, 722static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
@@ -603,7 +776,7 @@ static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
603 mod = 1; 776 mod = 1;
604 sizestr++; 777 sizestr++;
605 } 778 }
606 new_size = btrfs_parse_size(sizestr); 779 new_size = memparse(sizestr, NULL);
607 if (new_size == 0) { 780 if (new_size == 0) {
608 ret = -EINVAL; 781 ret = -EINVAL;
609 goto out_unlock; 782 goto out_unlock;
@@ -738,6 +911,330 @@ out:
738 return ret; 911 return ret;
739} 912}
740 913
914static noinline int key_in_sk(struct btrfs_key *key,
915 struct btrfs_ioctl_search_key *sk)
916{
917 struct btrfs_key test;
918 int ret;
919
920 test.objectid = sk->min_objectid;
921 test.type = sk->min_type;
922 test.offset = sk->min_offset;
923
924 ret = btrfs_comp_cpu_keys(key, &test);
925 if (ret < 0)
926 return 0;
927
928 test.objectid = sk->max_objectid;
929 test.type = sk->max_type;
930 test.offset = sk->max_offset;
931
932 ret = btrfs_comp_cpu_keys(key, &test);
933 if (ret > 0)
934 return 0;
935 return 1;
936}
937
938static noinline int copy_to_sk(struct btrfs_root *root,
939 struct btrfs_path *path,
940 struct btrfs_key *key,
941 struct btrfs_ioctl_search_key *sk,
942 char *buf,
943 unsigned long *sk_offset,
944 int *num_found)
945{
946 u64 found_transid;
947 struct extent_buffer *leaf;
948 struct btrfs_ioctl_search_header sh;
949 unsigned long item_off;
950 unsigned long item_len;
951 int nritems;
952 int i;
953 int slot;
954 int found = 0;
955 int ret = 0;
956
957 leaf = path->nodes[0];
958 slot = path->slots[0];
959 nritems = btrfs_header_nritems(leaf);
960
961 if (btrfs_header_generation(leaf) > sk->max_transid) {
962 i = nritems;
963 goto advance_key;
964 }
965 found_transid = btrfs_header_generation(leaf);
966
967 for (i = slot; i < nritems; i++) {
968 item_off = btrfs_item_ptr_offset(leaf, i);
969 item_len = btrfs_item_size_nr(leaf, i);
970
971 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
972 item_len = 0;
973
974 if (sizeof(sh) + item_len + *sk_offset >
975 BTRFS_SEARCH_ARGS_BUFSIZE) {
976 ret = 1;
977 goto overflow;
978 }
979
980 btrfs_item_key_to_cpu(leaf, key, i);
981 if (!key_in_sk(key, sk))
982 continue;
983
984 sh.objectid = key->objectid;
985 sh.offset = key->offset;
986 sh.type = key->type;
987 sh.len = item_len;
988 sh.transid = found_transid;
989
990 /* copy search result header */
991 memcpy(buf + *sk_offset, &sh, sizeof(sh));
992 *sk_offset += sizeof(sh);
993
994 if (item_len) {
995 char *p = buf + *sk_offset;
996 /* copy the item */
997 read_extent_buffer(leaf, p,
998 item_off, item_len);
999 *sk_offset += item_len;
1000 }
1001 found++;
1002
1003 if (*num_found >= sk->nr_items)
1004 break;
1005 }
1006advance_key:
1007 ret = 0;
1008 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1009 key->offset++;
1010 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1011 key->offset = 0;
1012 key->type++;
1013 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1014 key->offset = 0;
1015 key->type = 0;
1016 key->objectid++;
1017 } else
1018 ret = 1;
1019overflow:
1020 *num_found += found;
1021 return ret;
1022}
1023
1024static noinline int search_ioctl(struct inode *inode,
1025 struct btrfs_ioctl_search_args *args)
1026{
1027 struct btrfs_root *root;
1028 struct btrfs_key key;
1029 struct btrfs_key max_key;
1030 struct btrfs_path *path;
1031 struct btrfs_ioctl_search_key *sk = &args->key;
1032 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1033 int ret;
1034 int num_found = 0;
1035 unsigned long sk_offset = 0;
1036
1037 path = btrfs_alloc_path();
1038 if (!path)
1039 return -ENOMEM;
1040
1041 if (sk->tree_id == 0) {
1042 /* search the root of the inode that was passed */
1043 root = BTRFS_I(inode)->root;
1044 } else {
1045 key.objectid = sk->tree_id;
1046 key.type = BTRFS_ROOT_ITEM_KEY;
1047 key.offset = (u64)-1;
1048 root = btrfs_read_fs_root_no_name(info, &key);
1049 if (IS_ERR(root)) {
1050 printk(KERN_ERR "could not find root %llu\n",
1051 sk->tree_id);
1052 btrfs_free_path(path);
1053 return -ENOENT;
1054 }
1055 }
1056
1057 key.objectid = sk->min_objectid;
1058 key.type = sk->min_type;
1059 key.offset = sk->min_offset;
1060
1061 max_key.objectid = sk->max_objectid;
1062 max_key.type = sk->max_type;
1063 max_key.offset = sk->max_offset;
1064
1065 path->keep_locks = 1;
1066
1067 while(1) {
1068 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1069 sk->min_transid);
1070 if (ret != 0) {
1071 if (ret > 0)
1072 ret = 0;
1073 goto err;
1074 }
1075 ret = copy_to_sk(root, path, &key, sk, args->buf,
1076 &sk_offset, &num_found);
1077 btrfs_release_path(root, path);
1078 if (ret || num_found >= sk->nr_items)
1079 break;
1080
1081 }
1082 ret = 0;
1083err:
1084 sk->nr_items = num_found;
1085 btrfs_free_path(path);
1086 return ret;
1087}
1088
1089static noinline int btrfs_ioctl_tree_search(struct file *file,
1090 void __user *argp)
1091{
1092 struct btrfs_ioctl_search_args *args;
1093 struct inode *inode;
1094 int ret;
1095
1096 if (!capable(CAP_SYS_ADMIN))
1097 return -EPERM;
1098
1099 args = kmalloc(sizeof(*args), GFP_KERNEL);
1100 if (!args)
1101 return -ENOMEM;
1102
1103 if (copy_from_user(args, argp, sizeof(*args))) {
1104 kfree(args);
1105 return -EFAULT;
1106 }
1107 inode = fdentry(file)->d_inode;
1108 ret = search_ioctl(inode, args);
1109 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1110 ret = -EFAULT;
1111 kfree(args);
1112 return ret;
1113}
1114
1115/*
1116 * Search INODE_REFs to identify path name of 'dirid' directory
1117 * in a 'tree_id' tree. and sets path name to 'name'.
1118 */
1119static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1120 u64 tree_id, u64 dirid, char *name)
1121{
1122 struct btrfs_root *root;
1123 struct btrfs_key key;
1124 char *ptr;
1125 int ret = -1;
1126 int slot;
1127 int len;
1128 int total_len = 0;
1129 struct btrfs_inode_ref *iref;
1130 struct extent_buffer *l;
1131 struct btrfs_path *path;
1132
1133 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1134 name[0]='\0';
1135 return 0;
1136 }
1137
1138 path = btrfs_alloc_path();
1139 if (!path)
1140 return -ENOMEM;
1141
1142 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
1143
1144 key.objectid = tree_id;
1145 key.type = BTRFS_ROOT_ITEM_KEY;
1146 key.offset = (u64)-1;
1147 root = btrfs_read_fs_root_no_name(info, &key);
1148 if (IS_ERR(root)) {
1149 printk(KERN_ERR "could not find root %llu\n", tree_id);
1150 ret = -ENOENT;
1151 goto out;
1152 }
1153
1154 key.objectid = dirid;
1155 key.type = BTRFS_INODE_REF_KEY;
1156 key.offset = (u64)-1;
1157
1158 while(1) {
1159 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1160 if (ret < 0)
1161 goto out;
1162
1163 l = path->nodes[0];
1164 slot = path->slots[0];
1165 if (ret > 0 && slot > 0)
1166 slot--;
1167 btrfs_item_key_to_cpu(l, &key, slot);
1168
1169 if (ret > 0 && (key.objectid != dirid ||
1170 key.type != BTRFS_INODE_REF_KEY)) {
1171 ret = -ENOENT;
1172 goto out;
1173 }
1174
1175 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1176 len = btrfs_inode_ref_name_len(l, iref);
1177 ptr -= len + 1;
1178 total_len += len + 1;
1179 if (ptr < name)
1180 goto out;
1181
1182 *(ptr + len) = '/';
1183 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1184
1185 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1186 break;
1187
1188 btrfs_release_path(root, path);
1189 key.objectid = key.offset;
1190 key.offset = (u64)-1;
1191 dirid = key.objectid;
1192
1193 }
1194 if (ptr < name)
1195 goto out;
1196 memcpy(name, ptr, total_len);
1197 name[total_len]='\0';
1198 ret = 0;
1199out:
1200 btrfs_free_path(path);
1201 return ret;
1202}
1203
1204static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1205 void __user *argp)
1206{
1207 struct btrfs_ioctl_ino_lookup_args *args;
1208 struct inode *inode;
1209 int ret;
1210
1211 if (!capable(CAP_SYS_ADMIN))
1212 return -EPERM;
1213
1214 args = kmalloc(sizeof(*args), GFP_KERNEL);
1215 if (!args)
1216 return -ENOMEM;
1217
1218 if (copy_from_user(args, argp, sizeof(*args))) {
1219 kfree(args);
1220 return -EFAULT;
1221 }
1222 inode = fdentry(file)->d_inode;
1223
1224 if (args->treeid == 0)
1225 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1226
1227 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1228 args->treeid, args->objectid,
1229 args->name);
1230
1231 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1232 ret = -EFAULT;
1233
1234 kfree(args);
1235 return ret;
1236}
1237
741static noinline int btrfs_ioctl_snap_destroy(struct file *file, 1238static noinline int btrfs_ioctl_snap_destroy(struct file *file,
742 void __user *arg) 1239 void __user *arg)
743{ 1240{
@@ -844,10 +1341,11 @@ out:
844 return err; 1341 return err;
845} 1342}
846 1343
847static int btrfs_ioctl_defrag(struct file *file) 1344static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
848{ 1345{
849 struct inode *inode = fdentry(file)->d_inode; 1346 struct inode *inode = fdentry(file)->d_inode;
850 struct btrfs_root *root = BTRFS_I(inode)->root; 1347 struct btrfs_root *root = BTRFS_I(inode)->root;
1348 struct btrfs_ioctl_defrag_range_args *range;
851 int ret; 1349 int ret;
852 1350
853 ret = mnt_want_write(file->f_path.mnt); 1351 ret = mnt_want_write(file->f_path.mnt);
@@ -868,7 +1366,31 @@ static int btrfs_ioctl_defrag(struct file *file)
868 ret = -EINVAL; 1366 ret = -EINVAL;
869 goto out; 1367 goto out;
870 } 1368 }
871 btrfs_defrag_file(file); 1369
1370 range = kzalloc(sizeof(*range), GFP_KERNEL);
1371 if (!range) {
1372 ret = -ENOMEM;
1373 goto out;
1374 }
1375
1376 if (argp) {
1377 if (copy_from_user(range, argp,
1378 sizeof(*range))) {
1379 ret = -EFAULT;
1380 kfree(range);
1381 goto out;
1382 }
1383 /* compression requires us to start the IO */
1384 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1385 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1386 range->extent_thresh = (u32)-1;
1387 }
1388 } else {
1389 /* the rest are all set to zero by kzalloc */
1390 range->len = (u64)-1;
1391 }
1392 btrfs_defrag_file(file, range);
1393 kfree(range);
872 break; 1394 break;
873 } 1395 }
874out: 1396out:
@@ -959,12 +1481,17 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
959 ret = -EBADF; 1481 ret = -EBADF;
960 goto out_drop_write; 1482 goto out_drop_write;
961 } 1483 }
1484
962 src = src_file->f_dentry->d_inode; 1485 src = src_file->f_dentry->d_inode;
963 1486
964 ret = -EINVAL; 1487 ret = -EINVAL;
965 if (src == inode) 1488 if (src == inode)
966 goto out_fput; 1489 goto out_fput;
967 1490
1491 /* the src must be open for reading */
1492 if (!(src_file->f_mode & FMODE_READ))
1493 goto out_fput;
1494
968 ret = -EISDIR; 1495 ret = -EISDIR;
969 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) 1496 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
970 goto out_fput; 1497 goto out_fput;
@@ -1027,8 +1554,7 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1027 BUG_ON(!trans); 1554 BUG_ON(!trans);
1028 1555
1029 /* punch hole in destination first */ 1556 /* punch hole in destination first */
1030 btrfs_drop_extents(trans, root, inode, off, off + len, 1557 btrfs_drop_extents(trans, inode, off, off + len, &hint_byte, 1);
1031 off + len, 0, &hint_byte, 1);
1032 1558
1033 /* clone data */ 1559 /* clone data */
1034 key.objectid = src->i_ino; 1560 key.objectid = src->i_ino;
@@ -1270,6 +1796,157 @@ out:
1270 return ret; 1796 return ret;
1271} 1797}
1272 1798
1799static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
1800{
1801 struct inode *inode = fdentry(file)->d_inode;
1802 struct btrfs_root *root = BTRFS_I(inode)->root;
1803 struct btrfs_root *new_root;
1804 struct btrfs_dir_item *di;
1805 struct btrfs_trans_handle *trans;
1806 struct btrfs_path *path;
1807 struct btrfs_key location;
1808 struct btrfs_disk_key disk_key;
1809 struct btrfs_super_block *disk_super;
1810 u64 features;
1811 u64 objectid = 0;
1812 u64 dir_id;
1813
1814 if (!capable(CAP_SYS_ADMIN))
1815 return -EPERM;
1816
1817 if (copy_from_user(&objectid, argp, sizeof(objectid)))
1818 return -EFAULT;
1819
1820 if (!objectid)
1821 objectid = root->root_key.objectid;
1822
1823 location.objectid = objectid;
1824 location.type = BTRFS_ROOT_ITEM_KEY;
1825 location.offset = (u64)-1;
1826
1827 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
1828 if (IS_ERR(new_root))
1829 return PTR_ERR(new_root);
1830
1831 if (btrfs_root_refs(&new_root->root_item) == 0)
1832 return -ENOENT;
1833
1834 path = btrfs_alloc_path();
1835 if (!path)
1836 return -ENOMEM;
1837 path->leave_spinning = 1;
1838
1839 trans = btrfs_start_transaction(root, 1);
1840 if (!trans) {
1841 btrfs_free_path(path);
1842 return -ENOMEM;
1843 }
1844
1845 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
1846 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
1847 dir_id, "default", 7, 1);
1848 if (!di) {
1849 btrfs_free_path(path);
1850 btrfs_end_transaction(trans, root);
1851 printk(KERN_ERR "Umm, you don't have the default dir item, "
1852 "this isn't going to work\n");
1853 return -ENOENT;
1854 }
1855
1856 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
1857 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
1858 btrfs_mark_buffer_dirty(path->nodes[0]);
1859 btrfs_free_path(path);
1860
1861 disk_super = &root->fs_info->super_copy;
1862 features = btrfs_super_incompat_flags(disk_super);
1863 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
1864 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
1865 btrfs_set_super_incompat_flags(disk_super, features);
1866 }
1867 btrfs_end_transaction(trans, root);
1868
1869 return 0;
1870}
1871
1872long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
1873{
1874 struct btrfs_ioctl_space_args space_args;
1875 struct btrfs_ioctl_space_info space;
1876 struct btrfs_ioctl_space_info *dest;
1877 struct btrfs_ioctl_space_info *dest_orig;
1878 struct btrfs_ioctl_space_info *user_dest;
1879 struct btrfs_space_info *info;
1880 int alloc_size;
1881 int ret = 0;
1882 int slot_count = 0;
1883
1884 if (copy_from_user(&space_args,
1885 (struct btrfs_ioctl_space_args __user *)arg,
1886 sizeof(space_args)))
1887 return -EFAULT;
1888
1889 /* first we count slots */
1890 rcu_read_lock();
1891 list_for_each_entry_rcu(info, &root->fs_info->space_info, list)
1892 slot_count++;
1893 rcu_read_unlock();
1894
1895 /* space_slots == 0 means they are asking for a count */
1896 if (space_args.space_slots == 0) {
1897 space_args.total_spaces = slot_count;
1898 goto out;
1899 }
1900 alloc_size = sizeof(*dest) * slot_count;
1901 /* we generally have at most 6 or so space infos, one for each raid
1902 * level. So, a whole page should be more than enough for everyone
1903 */
1904 if (alloc_size > PAGE_CACHE_SIZE)
1905 return -ENOMEM;
1906
1907 space_args.total_spaces = 0;
1908 dest = kmalloc(alloc_size, GFP_NOFS);
1909 if (!dest)
1910 return -ENOMEM;
1911 dest_orig = dest;
1912
1913 /* now we have a buffer to copy into */
1914 rcu_read_lock();
1915 list_for_each_entry_rcu(info, &root->fs_info->space_info, list) {
1916 /* make sure we don't copy more than we allocated
1917 * in our buffer
1918 */
1919 if (slot_count == 0)
1920 break;
1921 slot_count--;
1922
1923 /* make sure userland has enough room in their buffer */
1924 if (space_args.total_spaces >= space_args.space_slots)
1925 break;
1926
1927 space.flags = info->flags;
1928 space.total_bytes = info->total_bytes;
1929 space.used_bytes = info->bytes_used;
1930 memcpy(dest, &space, sizeof(space));
1931 dest++;
1932 space_args.total_spaces++;
1933 }
1934 rcu_read_unlock();
1935
1936 user_dest = (struct btrfs_ioctl_space_info *)
1937 (arg + sizeof(struct btrfs_ioctl_space_args));
1938
1939 if (copy_to_user(user_dest, dest_orig, alloc_size))
1940 ret = -EFAULT;
1941
1942 kfree(dest_orig);
1943out:
1944 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
1945 ret = -EFAULT;
1946
1947 return ret;
1948}
1949
1273/* 1950/*
1274 * there are many ways the trans_start and trans_end ioctls can lead 1951 * there are many ways the trans_start and trans_end ioctls can lead
1275 * to deadlocks. They should only be used by applications that 1952 * to deadlocks. They should only be used by applications that
@@ -1316,8 +1993,12 @@ long btrfs_ioctl(struct file *file, unsigned int
1316 return btrfs_ioctl_snap_create(file, argp, 1); 1993 return btrfs_ioctl_snap_create(file, argp, 1);
1317 case BTRFS_IOC_SNAP_DESTROY: 1994 case BTRFS_IOC_SNAP_DESTROY:
1318 return btrfs_ioctl_snap_destroy(file, argp); 1995 return btrfs_ioctl_snap_destroy(file, argp);
1996 case BTRFS_IOC_DEFAULT_SUBVOL:
1997 return btrfs_ioctl_default_subvol(file, argp);
1319 case BTRFS_IOC_DEFRAG: 1998 case BTRFS_IOC_DEFRAG:
1320 return btrfs_ioctl_defrag(file); 1999 return btrfs_ioctl_defrag(file, NULL);
2000 case BTRFS_IOC_DEFRAG_RANGE:
2001 return btrfs_ioctl_defrag(file, argp);
1321 case BTRFS_IOC_RESIZE: 2002 case BTRFS_IOC_RESIZE:
1322 return btrfs_ioctl_resize(root, argp); 2003 return btrfs_ioctl_resize(root, argp);
1323 case BTRFS_IOC_ADD_DEV: 2004 case BTRFS_IOC_ADD_DEV:
@@ -1334,6 +2015,12 @@ long btrfs_ioctl(struct file *file, unsigned int
1334 return btrfs_ioctl_trans_start(file); 2015 return btrfs_ioctl_trans_start(file);
1335 case BTRFS_IOC_TRANS_END: 2016 case BTRFS_IOC_TRANS_END:
1336 return btrfs_ioctl_trans_end(file); 2017 return btrfs_ioctl_trans_end(file);
2018 case BTRFS_IOC_TREE_SEARCH:
2019 return btrfs_ioctl_tree_search(file, argp);
2020 case BTRFS_IOC_INO_LOOKUP:
2021 return btrfs_ioctl_ino_lookup(file, argp);
2022 case BTRFS_IOC_SPACE_INFO:
2023 return btrfs_ioctl_space_info(root, argp);
1337 case BTRFS_IOC_SYNC: 2024 case BTRFS_IOC_SYNC:
1338 btrfs_sync_fs(file->f_dentry->d_sb, 1); 2025 btrfs_sync_fs(file->f_dentry->d_sb, 1);
1339 return 0; 2026 return 0;