aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs
diff options
context:
space:
mode:
authorChristoph Hellwig <hch@lst.de>2008-06-11 21:53:53 -0400
committerChris Mason <chris.mason@oracle.com>2008-09-25 11:04:03 -0400
commitf46b5a66b3316ef2f8febfe4c56e2d555e2c3979 (patch)
treedc3a5898d323539728576df50f8be621936d6f04 /fs/btrfs
parent9d2423c5c3fbb0f110ac0b6cdc5a8e4d64729483 (diff)
Btrfs: split out ioctl.c
Split the ioctl handling out of inode.c into a file of it's own. Also fix up checkpatch.pl warnings for the moved code. Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs')
-rw-r--r--fs/btrfs/Makefile2
-rw-r--r--fs/btrfs/ctree.h9
-rw-r--r--fs/btrfs/inode.c733
-rw-r--r--fs/btrfs/ioctl.c781
4 files changed, 796 insertions, 729 deletions
diff --git a/fs/btrfs/Makefile b/fs/btrfs/Makefile
index 9dcfc2fe3332..7ed6b39e42d2 100644
--- a/fs/btrfs/Makefile
+++ b/fs/btrfs/Makefile
@@ -6,7 +6,7 @@ btrfs-y := super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.o \
6 hash.o file-item.o inode-item.o inode-map.o disk-io.o \ 6 hash.o file-item.o inode-item.o inode-map.o disk-io.o \
7 transaction.o bit-radix.o inode.o file.o tree-defrag.o \ 7 transaction.o bit-radix.o inode.o file.o tree-defrag.o \
8 extent_map.o sysfs.o struct-funcs.o xattr.o ordered-data.o \ 8 extent_map.o sysfs.o struct-funcs.o xattr.o ordered-data.o \
9 extent_io.o volumes.o async-thread.o 9 extent_io.o volumes.o async-thread.o ioctl.o
10 10
11btrfs-$(CONFIG_FS_POSIX_ACL) += acl.o 11btrfs-$(CONFIG_FS_POSIX_ACL) += acl.o
12else 12else
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 7ae4666103c0..181c81d29897 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -1553,6 +1553,10 @@ int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
1553 struct btrfs_root *root, struct btrfs_path *path, 1553 struct btrfs_root *root, struct btrfs_path *path,
1554 u64 isize); 1554 u64 isize);
1555/* inode.c */ 1555/* inode.c */
1556int btrfs_create_subvol_root(struct btrfs_root *new_root,
1557 struct btrfs_trans_handle *trans, u64 new_dirid,
1558 struct btrfs_block_group_cache *block_group);
1559
1556void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name, 1560void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
1557 int namelen); 1561 int namelen);
1558 1562
@@ -1585,7 +1589,6 @@ void btrfs_destroy_inode(struct inode *inode);
1585int btrfs_init_cachep(void); 1589int btrfs_init_cachep(void);
1586void btrfs_destroy_cachep(void); 1590void btrfs_destroy_cachep(void);
1587long btrfs_ioctl_trans_end(struct file *file); 1591long btrfs_ioctl_trans_end(struct file *file);
1588long btrfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
1589struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid, 1592struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1590 struct btrfs_root *root); 1593 struct btrfs_root *root);
1591struct inode *btrfs_ilookup(struct super_block *s, u64 objectid, 1594struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
@@ -1598,6 +1601,10 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1598int btrfs_update_inode(struct btrfs_trans_handle *trans, 1601int btrfs_update_inode(struct btrfs_trans_handle *trans,
1599 struct btrfs_root *root, 1602 struct btrfs_root *root,
1600 struct inode *inode); 1603 struct inode *inode);
1604
1605/* ioctl.c */
1606long btrfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
1607
1601/* file.c */ 1608/* file.c */
1602int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end); 1609int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end);
1603int btrfs_check_file(struct btrfs_root *root, struct inode *inode); 1610int btrfs_check_file(struct btrfs_root *root, struct inode *inode);
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 7daef8d37006..0c79346fd2c9 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -2667,114 +2667,17 @@ void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
2667 } 2667 }
2668} 2668}
2669 2669
2670static int noinline create_subvol(struct btrfs_root *root, char *name, 2670int btrfs_create_subvol_root(struct btrfs_root *new_root,
2671 int namelen) 2671 struct btrfs_trans_handle *trans, u64 new_dirid,
2672 struct btrfs_block_group_cache *block_group)
2672{ 2673{
2673 struct btrfs_trans_handle *trans;
2674 struct btrfs_key key;
2675 struct btrfs_root_item root_item;
2676 struct btrfs_inode_item *inode_item;
2677 struct extent_buffer *leaf;
2678 struct btrfs_root *new_root = root;
2679 struct inode *inode; 2674 struct inode *inode;
2680 struct inode *dir;
2681 int ret; 2675 int ret;
2682 int err;
2683 u64 objectid;
2684 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2685 unsigned long nr = 1;
2686
2687 mutex_lock(&root->fs_info->fs_mutex);
2688 ret = btrfs_check_free_space(root, 1, 0);
2689 if (ret)
2690 goto fail_commit;
2691
2692 trans = btrfs_start_transaction(root, 1);
2693 BUG_ON(!trans);
2694
2695 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2696 0, &objectid);
2697 if (ret)
2698 goto fail;
2699
2700 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2701 objectid, trans->transid, 0, 0,
2702 0, 0);
2703 if (IS_ERR(leaf))
2704 return PTR_ERR(leaf);
2705
2706 btrfs_set_header_nritems(leaf, 0);
2707 btrfs_set_header_level(leaf, 0);
2708 btrfs_set_header_bytenr(leaf, leaf->start);
2709 btrfs_set_header_generation(leaf, trans->transid);
2710 btrfs_set_header_owner(leaf, objectid);
2711
2712 write_extent_buffer(leaf, root->fs_info->fsid,
2713 (unsigned long)btrfs_header_fsid(leaf),
2714 BTRFS_FSID_SIZE);
2715 btrfs_mark_buffer_dirty(leaf);
2716
2717 inode_item = &root_item.inode;
2718 memset(inode_item, 0, sizeof(*inode_item));
2719 inode_item->generation = cpu_to_le64(1);
2720 inode_item->size = cpu_to_le64(3);
2721 inode_item->nlink = cpu_to_le32(1);
2722 inode_item->nblocks = cpu_to_le64(1);
2723 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2724
2725 btrfs_set_root_bytenr(&root_item, leaf->start);
2726 btrfs_set_root_level(&root_item, 0);
2727 btrfs_set_root_refs(&root_item, 1);
2728 btrfs_set_root_used(&root_item, 0);
2729
2730 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2731 root_item.drop_level = 0;
2732
2733 free_extent_buffer(leaf);
2734 leaf = NULL;
2735
2736 btrfs_set_root_dirid(&root_item, new_dirid);
2737
2738 key.objectid = objectid;
2739 key.offset = 1;
2740 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2741 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2742 &root_item);
2743 if (ret)
2744 goto fail;
2745
2746 /*
2747 * insert the directory item
2748 */
2749 key.offset = (u64)-1;
2750 dir = root->fs_info->sb->s_root->d_inode;
2751 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2752 name, namelen, dir->i_ino, &key,
2753 BTRFS_FT_DIR);
2754 if (ret)
2755 goto fail;
2756
2757 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2758 name, namelen, objectid,
2759 root->fs_info->sb->s_root->d_inode->i_ino);
2760 if (ret)
2761 goto fail;
2762
2763 ret = btrfs_commit_transaction(trans, root);
2764 if (ret)
2765 goto fail_commit;
2766
2767 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2768 BUG_ON(!new_root);
2769
2770 trans = btrfs_start_transaction(new_root, 1);
2771 BUG_ON(!trans);
2772 2676
2773 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid, 2677 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2774 new_dirid, 2678 new_dirid, block_group, S_IFDIR | 0700);
2775 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2776 if (IS_ERR(inode)) 2679 if (IS_ERR(inode))
2777 goto fail; 2680 return PTR_ERR(inode);
2778 inode->i_op = &btrfs_dir_inode_operations; 2681 inode->i_op = &btrfs_dir_inode_operations;
2779 inode->i_fop = &btrfs_dir_file_operations; 2682 inode->i_fop = &btrfs_dir_file_operations;
2780 new_root->inode = inode; 2683 new_root->inode = inode;
@@ -2783,67 +2686,8 @@ static int noinline create_subvol(struct btrfs_root *root, char *name,
2783 new_dirid); 2686 new_dirid);
2784 inode->i_nlink = 1; 2687 inode->i_nlink = 1;
2785 inode->i_size = 0; 2688 inode->i_size = 0;
2786 ret = btrfs_update_inode(trans, new_root, inode);
2787 if (ret)
2788 goto fail;
2789
2790 /* Invalidate existing dcache entry for new subvolume. */
2791 btrfs_invalidate_dcache_root(root, name, namelen);
2792 2689
2793fail: 2690 return btrfs_update_inode(trans, new_root, inode);
2794 nr = trans->blocks_used;
2795 err = btrfs_commit_transaction(trans, new_root);
2796 if (err && !ret)
2797 ret = err;
2798fail_commit:
2799 mutex_unlock(&root->fs_info->fs_mutex);
2800 btrfs_btree_balance_dirty(root, nr);
2801 btrfs_throttle(root);
2802 return ret;
2803}
2804
2805static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2806{
2807 struct btrfs_pending_snapshot *pending_snapshot;
2808 struct btrfs_trans_handle *trans;
2809 int ret;
2810 int err;
2811 unsigned long nr = 0;
2812
2813 if (!root->ref_cows)
2814 return -EINVAL;
2815
2816 mutex_lock(&root->fs_info->fs_mutex);
2817 ret = btrfs_check_free_space(root, 1, 0);
2818 if (ret)
2819 goto fail_unlock;
2820
2821 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2822 if (!pending_snapshot) {
2823 ret = -ENOMEM;
2824 goto fail_unlock;
2825 }
2826 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
2827 if (!pending_snapshot->name) {
2828 ret = -ENOMEM;
2829 kfree(pending_snapshot);
2830 goto fail_unlock;
2831 }
2832 memcpy(pending_snapshot->name, name, namelen);
2833 pending_snapshot->name[namelen] = '\0';
2834 trans = btrfs_start_transaction(root, 1);
2835 BUG_ON(!trans);
2836 pending_snapshot->root = root;
2837 list_add(&pending_snapshot->list,
2838 &trans->transaction->pending_snapshots);
2839 ret = btrfs_update_inode(trans, root, root->inode);
2840 err = btrfs_commit_transaction(trans, root);
2841
2842fail_unlock:
2843 mutex_unlock(&root->fs_info->fs_mutex);
2844 btrfs_btree_balance_dirty(root, nr);
2845 btrfs_throttle(root);
2846 return ret;
2847} 2691}
2848 2692
2849unsigned long btrfs_force_ra(struct address_space *mapping, 2693unsigned long btrfs_force_ra(struct address_space *mapping,
@@ -2861,571 +2705,6 @@ unsigned long btrfs_force_ra(struct address_space *mapping,
2861#endif 2705#endif
2862} 2706}
2863 2707
2864int btrfs_defrag_file(struct file *file) {
2865 struct inode *inode = fdentry(file)->d_inode;
2866 struct btrfs_root *root = BTRFS_I(inode)->root;
2867 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2868 struct page *page;
2869 unsigned long last_index;
2870 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
2871 unsigned long total_read = 0;
2872 u64 page_start;
2873 u64 page_end;
2874 unsigned long i;
2875 int ret;
2876
2877 mutex_lock(&root->fs_info->fs_mutex);
2878 ret = btrfs_check_free_space(root, inode->i_size, 0);
2879 mutex_unlock(&root->fs_info->fs_mutex);
2880 if (ret)
2881 return -ENOSPC;
2882
2883 mutex_lock(&inode->i_mutex);
2884 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2885 for (i = 0; i <= last_index; i++) {
2886 if (total_read % ra_pages == 0) {
2887 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
2888 min(last_index, i + ra_pages - 1));
2889 }
2890 total_read++;
2891 page = grab_cache_page(inode->i_mapping, i);
2892 if (!page)
2893 goto out_unlock;
2894 if (!PageUptodate(page)) {
2895 btrfs_readpage(NULL, page);
2896 lock_page(page);
2897 if (!PageUptodate(page)) {
2898 unlock_page(page);
2899 page_cache_release(page);
2900 goto out_unlock;
2901 }
2902 }
2903
2904#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2905 ClearPageDirty(page);
2906#else
2907 cancel_dirty_page(page, PAGE_CACHE_SIZE);
2908#endif
2909 wait_on_page_writeback(page);
2910 set_page_extent_mapped(page);
2911
2912 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2913 page_end = page_start + PAGE_CACHE_SIZE - 1;
2914
2915 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2916 set_extent_delalloc(io_tree, page_start,
2917 page_end, GFP_NOFS);
2918
2919 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2920 set_page_dirty(page);
2921 unlock_page(page);
2922 page_cache_release(page);
2923 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2924 }
2925
2926out_unlock:
2927 mutex_unlock(&inode->i_mutex);
2928 return 0;
2929}
2930
2931static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2932{
2933 u64 new_size;
2934 u64 old_size;
2935 u64 devid = 1;
2936 struct btrfs_ioctl_vol_args *vol_args;
2937 struct btrfs_trans_handle *trans;
2938 struct btrfs_device *device = NULL;
2939 char *sizestr;
2940 char *devstr = NULL;
2941 int ret = 0;
2942 int namelen;
2943 int mod = 0;
2944
2945 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2946
2947 if (!vol_args)
2948 return -ENOMEM;
2949
2950 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2951 ret = -EFAULT;
2952 goto out;
2953 }
2954 namelen = strlen(vol_args->name);
2955 if (namelen > BTRFS_VOL_NAME_MAX) {
2956 ret = -EINVAL;
2957 goto out;
2958 }
2959
2960 mutex_lock(&root->fs_info->fs_mutex);
2961 sizestr = vol_args->name;
2962 devstr = strchr(sizestr, ':');
2963 if (devstr) {
2964 char *end;
2965 sizestr = devstr + 1;
2966 *devstr = '\0';
2967 devstr = vol_args->name;
2968 devid = simple_strtoull(devstr, &end, 10);
2969printk("resizing devid %Lu\n", devid);
2970 }
2971 device = btrfs_find_device(root, devid, NULL);
2972 if (!device) {
2973 printk("resizer unable to find device %Lu\n", devid);
2974 ret = -EINVAL;
2975 goto out_unlock;
2976 }
2977 if (!strcmp(sizestr, "max"))
2978 new_size = device->bdev->bd_inode->i_size;
2979 else {
2980 if (sizestr[0] == '-') {
2981 mod = -1;
2982 sizestr++;
2983 } else if (sizestr[0] == '+') {
2984 mod = 1;
2985 sizestr++;
2986 }
2987 new_size = btrfs_parse_size(sizestr);
2988 if (new_size == 0) {
2989 ret = -EINVAL;
2990 goto out_unlock;
2991 }
2992 }
2993
2994 old_size = device->total_bytes;
2995
2996 if (mod < 0) {
2997 if (new_size > old_size) {
2998 ret = -EINVAL;
2999 goto out_unlock;
3000 }
3001 new_size = old_size - new_size;
3002 } else if (mod > 0) {
3003 new_size = old_size + new_size;
3004 }
3005
3006 if (new_size < 256 * 1024 * 1024) {
3007 ret = -EINVAL;
3008 goto out_unlock;
3009 }
3010 if (new_size > device->bdev->bd_inode->i_size) {
3011 ret = -EFBIG;
3012 goto out_unlock;
3013 }
3014
3015 do_div(new_size, root->sectorsize);
3016 new_size *= root->sectorsize;
3017
3018printk("new size for %s is %llu\n", device->name, (unsigned long long)new_size);
3019
3020 if (new_size > old_size) {
3021 trans = btrfs_start_transaction(root, 1);
3022 ret = btrfs_grow_device(trans, device, new_size);
3023 btrfs_commit_transaction(trans, root);
3024 } else {
3025 ret = btrfs_shrink_device(device, new_size);
3026 }
3027
3028out_unlock:
3029 mutex_unlock(&root->fs_info->fs_mutex);
3030out:
3031 kfree(vol_args);
3032 return ret;
3033}
3034
3035static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
3036 void __user *arg)
3037{
3038 struct btrfs_ioctl_vol_args *vol_args;
3039 struct btrfs_dir_item *di;
3040 struct btrfs_path *path;
3041 u64 root_dirid;
3042 int namelen;
3043 int ret;
3044
3045 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
3046
3047 if (!vol_args)
3048 return -ENOMEM;
3049
3050 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
3051 ret = -EFAULT;
3052 goto out;
3053 }
3054
3055 namelen = strlen(vol_args->name);
3056 if (namelen > BTRFS_VOL_NAME_MAX) {
3057 ret = -EINVAL;
3058 goto out;
3059 }
3060 if (strchr(vol_args->name, '/')) {
3061 ret = -EINVAL;
3062 goto out;
3063 }
3064
3065 path = btrfs_alloc_path();
3066 if (!path) {
3067 ret = -ENOMEM;
3068 goto out;
3069 }
3070
3071 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
3072 mutex_lock(&root->fs_info->fs_mutex);
3073 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
3074 path, root_dirid,
3075 vol_args->name, namelen, 0);
3076 mutex_unlock(&root->fs_info->fs_mutex);
3077 btrfs_free_path(path);
3078
3079 if (di && !IS_ERR(di)) {
3080 ret = -EEXIST;
3081 goto out;
3082 }
3083
3084 if (IS_ERR(di)) {
3085 ret = PTR_ERR(di);
3086 goto out;
3087 }
3088
3089 if (root == root->fs_info->tree_root)
3090 ret = create_subvol(root, vol_args->name, namelen);
3091 else
3092 ret = create_snapshot(root, vol_args->name, namelen);
3093out:
3094 kfree(vol_args);
3095 return ret;
3096}
3097
3098static int btrfs_ioctl_defrag(struct file *file)
3099{
3100 struct inode *inode = fdentry(file)->d_inode;
3101 struct btrfs_root *root = BTRFS_I(inode)->root;
3102
3103 switch (inode->i_mode & S_IFMT) {
3104 case S_IFDIR:
3105 mutex_lock(&root->fs_info->fs_mutex);
3106 btrfs_defrag_root(root, 0);
3107 btrfs_defrag_root(root->fs_info->extent_root, 0);
3108 mutex_unlock(&root->fs_info->fs_mutex);
3109 break;
3110 case S_IFREG:
3111 btrfs_defrag_file(file);
3112 break;
3113 }
3114
3115 return 0;
3116}
3117
3118long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
3119{
3120 struct btrfs_ioctl_vol_args *vol_args;
3121 int ret;
3122
3123 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
3124
3125 if (!vol_args)
3126 return -ENOMEM;
3127
3128 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
3129 ret = -EFAULT;
3130 goto out;
3131 }
3132 ret = btrfs_init_new_device(root, vol_args->name);
3133
3134out:
3135 kfree(vol_args);
3136 return ret;
3137}
3138
3139long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
3140{
3141 struct btrfs_ioctl_vol_args *vol_args;
3142 int ret;
3143
3144 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
3145
3146 if (!vol_args)
3147 return -ENOMEM;
3148
3149 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
3150 ret = -EFAULT;
3151 goto out;
3152 }
3153 ret = btrfs_rm_device(root, vol_args->name);
3154
3155out:
3156 kfree(vol_args);
3157 return ret;
3158}
3159
3160int dup_item_to_inode(struct btrfs_trans_handle *trans,
3161 struct btrfs_root *root,
3162 struct btrfs_path *path,
3163 struct extent_buffer *leaf,
3164 int slot,
3165 struct btrfs_key *key,
3166 u64 destino)
3167{
3168 char *dup;
3169 int len = btrfs_item_size_nr(leaf, slot);
3170 struct btrfs_key ckey = *key;
3171 int ret = 0;
3172
3173 dup = kmalloc(len, GFP_NOFS);
3174 if (!dup)
3175 return -ENOMEM;
3176
3177 read_extent_buffer(leaf, dup, btrfs_item_ptr_offset(leaf, slot), len);
3178 btrfs_release_path(root, path);
3179
3180 ckey.objectid = destino;
3181 ret = btrfs_insert_item(trans, root, &ckey, dup, len);
3182 kfree(dup);
3183 return ret;
3184}
3185
3186long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
3187{
3188 struct inode *inode = fdentry(file)->d_inode;
3189 struct btrfs_root *root = BTRFS_I(inode)->root;
3190 struct file *src_file;
3191 struct inode *src;
3192 struct btrfs_trans_handle *trans;
3193 int ret;
3194 u64 pos;
3195 struct btrfs_path *path;
3196 struct btrfs_key key;
3197 struct extent_buffer *leaf;
3198 u32 nritems;
3199 int slot;
3200
3201 src_file = fget(src_fd);
3202 if (!src_file)
3203 return -EBADF;
3204 src = src_file->f_dentry->d_inode;
3205
3206 ret = -EXDEV;
3207 if (src->i_sb != inode->i_sb)
3208 goto out_fput;
3209
3210 if (inode < src) {
3211 mutex_lock(&inode->i_mutex);
3212 mutex_lock(&src->i_mutex);
3213 } else {
3214 mutex_lock(&src->i_mutex);
3215 mutex_lock(&inode->i_mutex);
3216 }
3217
3218 ret = -ENOTEMPTY;
3219 if (inode->i_size)
3220 goto out_unlock;
3221
3222 /* do any pending delalloc/csum calc on src, one way or
3223 another, and lock file content */
3224 while (1) {
3225 filemap_write_and_wait(src->i_mapping);
3226 lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
3227 if (BTRFS_I(src)->delalloc_bytes == 0)
3228 break;
3229 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
3230 }
3231
3232 mutex_lock(&root->fs_info->fs_mutex);
3233 trans = btrfs_start_transaction(root, 0);
3234 path = btrfs_alloc_path();
3235 if (!path) {
3236 ret = -ENOMEM;
3237 goto out;
3238 }
3239 key.offset = 0;
3240 key.type = BTRFS_EXTENT_DATA_KEY;
3241 key.objectid = src->i_ino;
3242 pos = 0;
3243 path->reada = 2;
3244
3245 while (1) {
3246 /*
3247 * note the key will change type as we walk through the
3248 * tree.
3249 */
3250 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
3251 if (ret < 0)
3252 goto out;
3253
3254 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3255 ret = btrfs_next_leaf(root, path);
3256 if (ret < 0)
3257 goto out;
3258 if (ret > 0)
3259 break;
3260 }
3261 leaf = path->nodes[0];
3262 slot = path->slots[0];
3263 btrfs_item_key_to_cpu(leaf, &key, slot);
3264 nritems = btrfs_header_nritems(leaf);
3265
3266 if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
3267 key.objectid != src->i_ino)
3268 break;
3269
3270 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
3271 struct btrfs_file_extent_item *extent;
3272 int found_type;
3273 pos = key.offset;
3274 extent = btrfs_item_ptr(leaf, slot,
3275 struct btrfs_file_extent_item);
3276 found_type = btrfs_file_extent_type(leaf, extent);
3277 if (found_type == BTRFS_FILE_EXTENT_REG) {
3278 u64 len = btrfs_file_extent_num_bytes(leaf,
3279 extent);
3280 u64 ds = btrfs_file_extent_disk_bytenr(leaf,
3281 extent);
3282 u64 dl = btrfs_file_extent_disk_num_bytes(leaf,
3283 extent);
3284 u64 off = btrfs_file_extent_offset(leaf,
3285 extent);
3286 btrfs_insert_file_extent(trans, root,
3287 inode->i_ino, pos,
3288 ds, dl, len, off);
3289 /* ds == 0 means there's a hole */
3290 if (ds != 0) {
3291 btrfs_inc_extent_ref(trans, root,
3292 ds, dl,
3293 root->root_key.objectid,
3294 trans->transid,
3295 inode->i_ino, pos);
3296 }
3297 pos = key.offset + len;
3298 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
3299 ret = dup_item_to_inode(trans, root, path,
3300 leaf, slot, &key,
3301 inode->i_ino);
3302 if (ret)
3303 goto out;
3304 pos = key.offset + btrfs_item_size_nr(leaf,
3305 slot);
3306 }
3307 } else if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
3308 ret = dup_item_to_inode(trans, root, path, leaf,
3309 slot, &key, inode->i_ino);
3310
3311 if (ret)
3312 goto out;
3313 }
3314 key.offset++;
3315 btrfs_release_path(root, path);
3316 }
3317
3318 ret = 0;
3319out:
3320 btrfs_free_path(path);
3321
3322 inode->i_blocks = src->i_blocks;
3323 i_size_write(inode, src->i_size);
3324 btrfs_update_inode(trans, root, inode);
3325
3326 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
3327
3328 btrfs_end_transaction(trans, root);
3329 mutex_unlock(&root->fs_info->fs_mutex);
3330
3331out_unlock:
3332 mutex_unlock(&src->i_mutex);
3333 mutex_unlock(&inode->i_mutex);
3334out_fput:
3335 fput(src_file);
3336 return ret;
3337}
3338
3339/*
3340 * there are many ways the trans_start and trans_end ioctls can lead
3341 * to deadlocks. They should only be used by applications that
3342 * basically own the machine, and have a very in depth understanding
3343 * of all the possible deadlocks and enospc problems.
3344 */
3345long btrfs_ioctl_trans_start(struct file *file)
3346{
3347 struct inode *inode = fdentry(file)->d_inode;
3348 struct btrfs_root *root = BTRFS_I(inode)->root;
3349 struct btrfs_trans_handle *trans;
3350 int ret = 0;
3351
3352 mutex_lock(&root->fs_info->fs_mutex);
3353 if (file->private_data) {
3354 ret = -EINPROGRESS;
3355 goto out;
3356 }
3357 trans = btrfs_start_transaction(root, 0);
3358 if (trans)
3359 file->private_data = trans;
3360 else
3361 ret = -ENOMEM;
3362 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
3363out:
3364 mutex_unlock(&root->fs_info->fs_mutex);
3365 return ret;
3366}
3367
3368/*
3369 * there are many ways the trans_start and trans_end ioctls can lead
3370 * to deadlocks. They should only be used by applications that
3371 * basically own the machine, and have a very in depth understanding
3372 * of all the possible deadlocks and enospc problems.
3373 */
3374long btrfs_ioctl_trans_end(struct file *file)
3375{
3376 struct inode *inode = fdentry(file)->d_inode;
3377 struct btrfs_root *root = BTRFS_I(inode)->root;
3378 struct btrfs_trans_handle *trans;
3379 int ret = 0;
3380
3381 mutex_lock(&root->fs_info->fs_mutex);
3382 trans = file->private_data;
3383 if (!trans) {
3384 ret = -EINVAL;
3385 goto out;
3386 }
3387 btrfs_end_transaction(trans, root);
3388 file->private_data = 0;
3389out:
3390 mutex_unlock(&root->fs_info->fs_mutex);
3391 return ret;
3392}
3393
3394long btrfs_ioctl(struct file *file, unsigned int
3395 cmd, unsigned long arg)
3396{
3397 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3398
3399 switch (cmd) {
3400 case BTRFS_IOC_SNAP_CREATE:
3401 return btrfs_ioctl_snap_create(root, (void __user *)arg);
3402 case BTRFS_IOC_DEFRAG:
3403 return btrfs_ioctl_defrag(file);
3404 case BTRFS_IOC_RESIZE:
3405 return btrfs_ioctl_resize(root, (void __user *)arg);
3406 case BTRFS_IOC_ADD_DEV:
3407 return btrfs_ioctl_add_dev(root, (void __user *)arg);
3408 case BTRFS_IOC_RM_DEV:
3409 return btrfs_ioctl_rm_dev(root, (void __user *)arg);
3410 case BTRFS_IOC_BALANCE:
3411 return btrfs_balance(root->fs_info->dev_root);
3412 case BTRFS_IOC_CLONE:
3413 return btrfs_ioctl_clone(file, arg);
3414 case BTRFS_IOC_TRANS_START:
3415 return btrfs_ioctl_trans_start(file);
3416 case BTRFS_IOC_TRANS_END:
3417 return btrfs_ioctl_trans_end(file);
3418 case BTRFS_IOC_SYNC:
3419 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3420 return 0;
3421 }
3422
3423 return -ENOTTY;
3424}
3425
3426/*
3427 * Called inside transaction, so use GFP_NOFS
3428 */
3429struct inode *btrfs_alloc_inode(struct super_block *sb) 2708struct inode *btrfs_alloc_inode(struct super_block *sb)
3430{ 2709{
3431 struct btrfs_inode *ei; 2710 struct btrfs_inode *ei;
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
new file mode 100644
index 000000000000..da8de6cfdb5a
--- /dev/null
+++ b/fs/btrfs/ioctl.c
@@ -0,0 +1,781 @@
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/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
29#include <linux/smp_lock.h>
30#include <linux/backing-dev.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/bit_spinlock.h>
37#include <linux/version.h>
38#include <linux/xattr.h>
39#include "ctree.h"
40#include "disk-io.h"
41#include "transaction.h"
42#include "btrfs_inode.h"
43#include "ioctl.h"
44#include "print-tree.h"
45#include "volumes.h"
46
47
48
49static noinline int create_subvol(struct btrfs_root *root, char *name,
50 int namelen)
51{
52 struct btrfs_trans_handle *trans;
53 struct btrfs_key key;
54 struct btrfs_root_item root_item;
55 struct btrfs_inode_item *inode_item;
56 struct extent_buffer *leaf;
57 struct btrfs_root *new_root = root;
58 struct inode *dir;
59 int ret;
60 int err;
61 u64 objectid;
62 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
63 unsigned long nr = 1;
64
65 mutex_lock(&root->fs_info->fs_mutex);
66 ret = btrfs_check_free_space(root, 1, 0);
67 if (ret)
68 goto fail_commit;
69
70 trans = btrfs_start_transaction(root, 1);
71 BUG_ON(!trans);
72
73 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
74 0, &objectid);
75 if (ret)
76 goto fail;
77
78 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
79 objectid, trans->transid, 0, 0,
80 0, 0);
81 if (IS_ERR(leaf))
82 return PTR_ERR(leaf);
83
84 btrfs_set_header_nritems(leaf, 0);
85 btrfs_set_header_level(leaf, 0);
86 btrfs_set_header_bytenr(leaf, leaf->start);
87 btrfs_set_header_generation(leaf, trans->transid);
88 btrfs_set_header_owner(leaf, objectid);
89
90 write_extent_buffer(leaf, root->fs_info->fsid,
91 (unsigned long)btrfs_header_fsid(leaf),
92 BTRFS_FSID_SIZE);
93 btrfs_mark_buffer_dirty(leaf);
94
95 inode_item = &root_item.inode;
96 memset(inode_item, 0, sizeof(*inode_item));
97 inode_item->generation = cpu_to_le64(1);
98 inode_item->size = cpu_to_le64(3);
99 inode_item->nlink = cpu_to_le32(1);
100 inode_item->nblocks = cpu_to_le64(1);
101 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
102
103 btrfs_set_root_bytenr(&root_item, leaf->start);
104 btrfs_set_root_level(&root_item, 0);
105 btrfs_set_root_refs(&root_item, 1);
106 btrfs_set_root_used(&root_item, 0);
107
108 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
109 root_item.drop_level = 0;
110
111 free_extent_buffer(leaf);
112 leaf = NULL;
113
114 btrfs_set_root_dirid(&root_item, new_dirid);
115
116 key.objectid = objectid;
117 key.offset = 1;
118 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
119 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
120 &root_item);
121 if (ret)
122 goto fail;
123
124 /*
125 * insert the directory item
126 */
127 key.offset = (u64)-1;
128 dir = root->fs_info->sb->s_root->d_inode;
129 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
130 name, namelen, dir->i_ino, &key,
131 BTRFS_FT_DIR);
132 if (ret)
133 goto fail;
134
135 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
136 name, namelen, objectid,
137 root->fs_info->sb->s_root->d_inode->i_ino);
138 if (ret)
139 goto fail;
140
141 ret = btrfs_commit_transaction(trans, root);
142 if (ret)
143 goto fail_commit;
144
145 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
146 BUG_ON(!new_root);
147
148 trans = btrfs_start_transaction(new_root, 1);
149 BUG_ON(!trans);
150
151 ret = btrfs_create_subvol_root(new_root, trans, new_dirid,
152 BTRFS_I(dir)->block_group);
153 if (ret)
154 goto fail;
155
156 /* Invalidate existing dcache entry for new subvolume. */
157 btrfs_invalidate_dcache_root(root, name, namelen);
158
159fail:
160 nr = trans->blocks_used;
161 err = btrfs_commit_transaction(trans, new_root);
162 if (err && !ret)
163 ret = err;
164fail_commit:
165 mutex_unlock(&root->fs_info->fs_mutex);
166 btrfs_btree_balance_dirty(root, nr);
167 btrfs_throttle(root);
168 return ret;
169}
170
171static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
172{
173 struct btrfs_pending_snapshot *pending_snapshot;
174 struct btrfs_trans_handle *trans;
175 int ret;
176 int err;
177 unsigned long nr = 0;
178
179 if (!root->ref_cows)
180 return -EINVAL;
181
182 mutex_lock(&root->fs_info->fs_mutex);
183 ret = btrfs_check_free_space(root, 1, 0);
184 if (ret)
185 goto fail_unlock;
186
187 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
188 if (!pending_snapshot) {
189 ret = -ENOMEM;
190 goto fail_unlock;
191 }
192 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
193 if (!pending_snapshot->name) {
194 ret = -ENOMEM;
195 kfree(pending_snapshot);
196 goto fail_unlock;
197 }
198 memcpy(pending_snapshot->name, name, namelen);
199 pending_snapshot->name[namelen] = '\0';
200 trans = btrfs_start_transaction(root, 1);
201 BUG_ON(!trans);
202 pending_snapshot->root = root;
203 list_add(&pending_snapshot->list,
204 &trans->transaction->pending_snapshots);
205 ret = btrfs_update_inode(trans, root, root->inode);
206 err = btrfs_commit_transaction(trans, root);
207
208fail_unlock:
209 mutex_unlock(&root->fs_info->fs_mutex);
210 btrfs_btree_balance_dirty(root, nr);
211 btrfs_throttle(root);
212 return ret;
213}
214
215int btrfs_defrag_file(struct file *file)
216{
217 struct inode *inode = fdentry(file)->d_inode;
218 struct btrfs_root *root = BTRFS_I(inode)->root;
219 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
220 struct page *page;
221 unsigned long last_index;
222 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
223 unsigned long total_read = 0;
224 u64 page_start;
225 u64 page_end;
226 unsigned long i;
227 int ret;
228
229 mutex_lock(&root->fs_info->fs_mutex);
230 ret = btrfs_check_free_space(root, inode->i_size, 0);
231 mutex_unlock(&root->fs_info->fs_mutex);
232 if (ret)
233 return -ENOSPC;
234
235 mutex_lock(&inode->i_mutex);
236 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
237 for (i = 0; i <= last_index; i++) {
238 if (total_read % ra_pages == 0) {
239 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
240 min(last_index, i + ra_pages - 1));
241 }
242 total_read++;
243 page = grab_cache_page(inode->i_mapping, i);
244 if (!page)
245 goto out_unlock;
246 if (!PageUptodate(page)) {
247 btrfs_readpage(NULL, page);
248 lock_page(page);
249 if (!PageUptodate(page)) {
250 unlock_page(page);
251 page_cache_release(page);
252 goto out_unlock;
253 }
254 }
255
256#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
257 ClearPageDirty(page);
258#else
259 cancel_dirty_page(page, PAGE_CACHE_SIZE);
260#endif
261 wait_on_page_writeback(page);
262 set_page_extent_mapped(page);
263
264 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
265 page_end = page_start + PAGE_CACHE_SIZE - 1;
266
267 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
268 set_extent_delalloc(io_tree, page_start,
269 page_end, GFP_NOFS);
270
271 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
272 set_page_dirty(page);
273 unlock_page(page);
274 page_cache_release(page);
275 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
276 }
277
278out_unlock:
279 mutex_unlock(&inode->i_mutex);
280 return 0;
281}
282
283/*
284 * Called inside transaction, so use GFP_NOFS
285 */
286
287static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
288{
289 u64 new_size;
290 u64 old_size;
291 u64 devid = 1;
292 struct btrfs_ioctl_vol_args *vol_args;
293 struct btrfs_trans_handle *trans;
294 struct btrfs_device *device = NULL;
295 char *sizestr;
296 char *devstr = NULL;
297 int ret = 0;
298 int namelen;
299 int mod = 0;
300
301 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
302
303 if (!vol_args)
304 return -ENOMEM;
305
306 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
307 ret = -EFAULT;
308 goto out;
309 }
310 namelen = strlen(vol_args->name);
311 if (namelen > BTRFS_VOL_NAME_MAX) {
312 ret = -EINVAL;
313 goto out;
314 }
315
316 mutex_lock(&root->fs_info->fs_mutex);
317 sizestr = vol_args->name;
318 devstr = strchr(sizestr, ':');
319 if (devstr) {
320 char *end;
321 sizestr = devstr + 1;
322 *devstr = '\0';
323 devstr = vol_args->name;
324 devid = simple_strtoull(devstr, &end, 10);
325 printk(KERN_INFO "resizing devid %llu\n", devid);
326 }
327 device = btrfs_find_device(root, devid, NULL);
328 if (!device) {
329 printk(KERN_INFO "resizer unable to find device %llu\n", devid);
330 ret = -EINVAL;
331 goto out_unlock;
332 }
333 if (!strcmp(sizestr, "max"))
334 new_size = device->bdev->bd_inode->i_size;
335 else {
336 if (sizestr[0] == '-') {
337 mod = -1;
338 sizestr++;
339 } else if (sizestr[0] == '+') {
340 mod = 1;
341 sizestr++;
342 }
343 new_size = btrfs_parse_size(sizestr);
344 if (new_size == 0) {
345 ret = -EINVAL;
346 goto out_unlock;
347 }
348 }
349
350 old_size = device->total_bytes;
351
352 if (mod < 0) {
353 if (new_size > old_size) {
354 ret = -EINVAL;
355 goto out_unlock;
356 }
357 new_size = old_size - new_size;
358 } else if (mod > 0) {
359 new_size = old_size + new_size;
360 }
361
362 if (new_size < 256 * 1024 * 1024) {
363 ret = -EINVAL;
364 goto out_unlock;
365 }
366 if (new_size > device->bdev->bd_inode->i_size) {
367 ret = -EFBIG;
368 goto out_unlock;
369 }
370
371 do_div(new_size, root->sectorsize);
372 new_size *= root->sectorsize;
373
374 printk(KERN_INFO "new size for %s is %llu\n",
375 device->name, (unsigned long long)new_size);
376
377 if (new_size > old_size) {
378 trans = btrfs_start_transaction(root, 1);
379 ret = btrfs_grow_device(trans, device, new_size);
380 btrfs_commit_transaction(trans, root);
381 } else {
382 ret = btrfs_shrink_device(device, new_size);
383 }
384
385out_unlock:
386 mutex_unlock(&root->fs_info->fs_mutex);
387out:
388 kfree(vol_args);
389 return ret;
390}
391
392static noinline int btrfs_ioctl_snap_create(struct btrfs_root *root,
393 void __user *arg)
394{
395 struct btrfs_ioctl_vol_args *vol_args;
396 struct btrfs_dir_item *di;
397 struct btrfs_path *path;
398 u64 root_dirid;
399 int namelen;
400 int ret;
401
402 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
403
404 if (!vol_args)
405 return -ENOMEM;
406
407 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
408 ret = -EFAULT;
409 goto out;
410 }
411
412 namelen = strlen(vol_args->name);
413 if (namelen > BTRFS_VOL_NAME_MAX) {
414 ret = -EINVAL;
415 goto out;
416 }
417 if (strchr(vol_args->name, '/')) {
418 ret = -EINVAL;
419 goto out;
420 }
421
422 path = btrfs_alloc_path();
423 if (!path) {
424 ret = -ENOMEM;
425 goto out;
426 }
427
428 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
429 mutex_lock(&root->fs_info->fs_mutex);
430 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
431 path, root_dirid,
432 vol_args->name, namelen, 0);
433 mutex_unlock(&root->fs_info->fs_mutex);
434 btrfs_free_path(path);
435
436 if (di && !IS_ERR(di)) {
437 ret = -EEXIST;
438 goto out;
439 }
440
441 if (IS_ERR(di)) {
442 ret = PTR_ERR(di);
443 goto out;
444 }
445
446 if (root == root->fs_info->tree_root)
447 ret = create_subvol(root, vol_args->name, namelen);
448 else
449 ret = create_snapshot(root, vol_args->name, namelen);
450out:
451 kfree(vol_args);
452 return ret;
453}
454
455static int btrfs_ioctl_defrag(struct file *file)
456{
457 struct inode *inode = fdentry(file)->d_inode;
458 struct btrfs_root *root = BTRFS_I(inode)->root;
459
460 switch (inode->i_mode & S_IFMT) {
461 case S_IFDIR:
462 mutex_lock(&root->fs_info->fs_mutex);
463 btrfs_defrag_root(root, 0);
464 btrfs_defrag_root(root->fs_info->extent_root, 0);
465 mutex_unlock(&root->fs_info->fs_mutex);
466 break;
467 case S_IFREG:
468 btrfs_defrag_file(file);
469 break;
470 }
471
472 return 0;
473}
474
475long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
476{
477 struct btrfs_ioctl_vol_args *vol_args;
478 int ret;
479
480 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
481
482 if (!vol_args)
483 return -ENOMEM;
484
485 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
486 ret = -EFAULT;
487 goto out;
488 }
489 ret = btrfs_init_new_device(root, vol_args->name);
490
491out:
492 kfree(vol_args);
493 return ret;
494}
495
496long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
497{
498 struct btrfs_ioctl_vol_args *vol_args;
499 int ret;
500
501 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
502
503 if (!vol_args)
504 return -ENOMEM;
505
506 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
507 ret = -EFAULT;
508 goto out;
509 }
510 ret = btrfs_rm_device(root, vol_args->name);
511
512out:
513 kfree(vol_args);
514 return ret;
515}
516
517int dup_item_to_inode(struct btrfs_trans_handle *trans,
518 struct btrfs_root *root,
519 struct btrfs_path *path,
520 struct extent_buffer *leaf,
521 int slot,
522 struct btrfs_key *key,
523 u64 destino)
524{
525 char *dup;
526 int len = btrfs_item_size_nr(leaf, slot);
527 struct btrfs_key ckey = *key;
528 int ret = 0;
529
530 dup = kmalloc(len, GFP_NOFS);
531 if (!dup)
532 return -ENOMEM;
533
534 read_extent_buffer(leaf, dup, btrfs_item_ptr_offset(leaf, slot), len);
535 btrfs_release_path(root, path);
536
537 ckey.objectid = destino;
538 ret = btrfs_insert_item(trans, root, &ckey, dup, len);
539 kfree(dup);
540 return ret;
541}
542
543long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
544{
545 struct inode *inode = fdentry(file)->d_inode;
546 struct btrfs_root *root = BTRFS_I(inode)->root;
547 struct file *src_file;
548 struct inode *src;
549 struct btrfs_trans_handle *trans;
550 int ret;
551 u64 pos;
552 struct btrfs_path *path;
553 struct btrfs_key key;
554 struct extent_buffer *leaf;
555 u32 nritems;
556 int slot;
557
558 src_file = fget(src_fd);
559 if (!src_file)
560 return -EBADF;
561 src = src_file->f_dentry->d_inode;
562
563 ret = -EXDEV;
564 if (src->i_sb != inode->i_sb)
565 goto out_fput;
566
567 if (inode < src) {
568 mutex_lock(&inode->i_mutex);
569 mutex_lock(&src->i_mutex);
570 } else {
571 mutex_lock(&src->i_mutex);
572 mutex_lock(&inode->i_mutex);
573 }
574
575 ret = -ENOTEMPTY;
576 if (inode->i_size)
577 goto out_unlock;
578
579 /* do any pending delalloc/csum calc on src, one way or
580 another, and lock file content */
581 while (1) {
582 filemap_write_and_wait(src->i_mapping);
583 lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
584 if (BTRFS_I(src)->delalloc_bytes == 0)
585 break;
586 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
587 }
588
589 mutex_lock(&root->fs_info->fs_mutex);
590 trans = btrfs_start_transaction(root, 0);
591 path = btrfs_alloc_path();
592 if (!path) {
593 ret = -ENOMEM;
594 goto out;
595 }
596 key.offset = 0;
597 key.type = BTRFS_EXTENT_DATA_KEY;
598 key.objectid = src->i_ino;
599 pos = 0;
600 path->reada = 2;
601
602 while (1) {
603 /*
604 * note the key will change type as we walk through the
605 * tree.
606 */
607 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
608 if (ret < 0)
609 goto out;
610
611 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
612 ret = btrfs_next_leaf(root, path);
613 if (ret < 0)
614 goto out;
615 if (ret > 0)
616 break;
617 }
618 leaf = path->nodes[0];
619 slot = path->slots[0];
620 btrfs_item_key_to_cpu(leaf, &key, slot);
621 nritems = btrfs_header_nritems(leaf);
622
623 if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
624 key.objectid != src->i_ino)
625 break;
626
627 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
628 struct btrfs_file_extent_item *extent;
629 int found_type;
630 pos = key.offset;
631 extent = btrfs_item_ptr(leaf, slot,
632 struct btrfs_file_extent_item);
633 found_type = btrfs_file_extent_type(leaf, extent);
634 if (found_type == BTRFS_FILE_EXTENT_REG) {
635 u64 len = btrfs_file_extent_num_bytes(leaf,
636 extent);
637 u64 ds = btrfs_file_extent_disk_bytenr(leaf,
638 extent);
639 u64 dl = btrfs_file_extent_disk_num_bytes(leaf,
640 extent);
641 u64 off = btrfs_file_extent_offset(leaf,
642 extent);
643 btrfs_insert_file_extent(trans, root,
644 inode->i_ino, pos,
645 ds, dl, len, off);
646 /* ds == 0 means there's a hole */
647 if (ds != 0) {
648 btrfs_inc_extent_ref(trans, root,
649 ds, dl,
650 root->root_key.objectid,
651 trans->transid,
652 inode->i_ino, pos);
653 }
654 pos = key.offset + len;
655 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
656 ret = dup_item_to_inode(trans, root, path,
657 leaf, slot, &key,
658 inode->i_ino);
659 if (ret)
660 goto out;
661 pos = key.offset + btrfs_item_size_nr(leaf,
662 slot);
663 }
664 } else if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
665 ret = dup_item_to_inode(trans, root, path, leaf,
666 slot, &key, inode->i_ino);
667
668 if (ret)
669 goto out;
670 }
671 key.offset++;
672 btrfs_release_path(root, path);
673 }
674
675 ret = 0;
676out:
677 btrfs_free_path(path);
678
679 inode->i_blocks = src->i_blocks;
680 i_size_write(inode, src->i_size);
681 btrfs_update_inode(trans, root, inode);
682
683 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
684
685 btrfs_end_transaction(trans, root);
686 mutex_unlock(&root->fs_info->fs_mutex);
687
688out_unlock:
689 mutex_unlock(&src->i_mutex);
690 mutex_unlock(&inode->i_mutex);
691out_fput:
692 fput(src_file);
693 return ret;
694}
695
696/*
697 * there are many ways the trans_start and trans_end ioctls can lead
698 * to deadlocks. They should only be used by applications that
699 * basically own the machine, and have a very in depth understanding
700 * of all the possible deadlocks and enospc problems.
701 */
702long btrfs_ioctl_trans_start(struct file *file)
703{
704 struct inode *inode = fdentry(file)->d_inode;
705 struct btrfs_root *root = BTRFS_I(inode)->root;
706 struct btrfs_trans_handle *trans;
707 int ret = 0;
708
709 mutex_lock(&root->fs_info->fs_mutex);
710 if (file->private_data) {
711 ret = -EINPROGRESS;
712 goto out;
713 }
714 trans = btrfs_start_transaction(root, 0);
715 if (trans)
716 file->private_data = trans;
717 else
718 ret = -ENOMEM;
719 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
720out:
721 mutex_unlock(&root->fs_info->fs_mutex);
722 return ret;
723}
724
725/*
726 * there are many ways the trans_start and trans_end ioctls can lead
727 * to deadlocks. They should only be used by applications that
728 * basically own the machine, and have a very in depth understanding
729 * of all the possible deadlocks and enospc problems.
730 */
731long btrfs_ioctl_trans_end(struct file *file)
732{
733 struct inode *inode = fdentry(file)->d_inode;
734 struct btrfs_root *root = BTRFS_I(inode)->root;
735 struct btrfs_trans_handle *trans;
736 int ret = 0;
737
738 mutex_lock(&root->fs_info->fs_mutex);
739 trans = file->private_data;
740 if (!trans) {
741 ret = -EINVAL;
742 goto out;
743 }
744 btrfs_end_transaction(trans, root);
745 file->private_data = 0;
746out:
747 mutex_unlock(&root->fs_info->fs_mutex);
748 return ret;
749}
750
751long btrfs_ioctl(struct file *file, unsigned int
752 cmd, unsigned long arg)
753{
754 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
755
756 switch (cmd) {
757 case BTRFS_IOC_SNAP_CREATE:
758 return btrfs_ioctl_snap_create(root, (void __user *)arg);
759 case BTRFS_IOC_DEFRAG:
760 return btrfs_ioctl_defrag(file);
761 case BTRFS_IOC_RESIZE:
762 return btrfs_ioctl_resize(root, (void __user *)arg);
763 case BTRFS_IOC_ADD_DEV:
764 return btrfs_ioctl_add_dev(root, (void __user *)arg);
765 case BTRFS_IOC_RM_DEV:
766 return btrfs_ioctl_rm_dev(root, (void __user *)arg);
767 case BTRFS_IOC_BALANCE:
768 return btrfs_balance(root->fs_info->dev_root);
769 case BTRFS_IOC_CLONE:
770 return btrfs_ioctl_clone(file, arg);
771 case BTRFS_IOC_TRANS_START:
772 return btrfs_ioctl_trans_start(file);
773 case BTRFS_IOC_TRANS_END:
774 return btrfs_ioctl_trans_end(file);
775 case BTRFS_IOC_SYNC:
776 btrfs_sync_fs(file->f_dentry->d_sb, 1);
777 return 0;
778 }
779
780 return -ENOTTY;
781}