Btrfs: Seed device support
[linux-2.6-block.git] / fs / btrfs / ioctl.c
index 6002eb64daf938e7fcf1a84959cf485526fef69e..f43df72b0e177e46f9ed02fcf2659a1f0b91453b 100644 (file)
@@ -21,6 +21,7 @@
 #include <linux/buffer_head.h>
 #include <linux/file.h>
 #include <linux/fs.h>
+#include <linux/fsnotify.h>
 #include <linux/pagemap.h>
 #include <linux/highmem.h>
 #include <linux/time.h>
 #include <linux/string.h>
 #include <linux/smp_lock.h>
 #include <linux/backing-dev.h>
+#include <linux/mount.h>
 #include <linux/mpage.h>
+#include <linux/namei.h>
 #include <linux/swap.h>
 #include <linux/writeback.h>
 #include <linux/statfs.h>
 #include <linux/compat.h>
 #include <linux/bit_spinlock.h>
+#include <linux/security.h>
 #include <linux/version.h>
 #include <linux/xattr.h>
+#include <linux/vmalloc.h>
 #include "ctree.h"
 #include "disk-io.h"
 #include "transaction.h"
@@ -47,8 +52,9 @@
 
 
 
-static noinline int create_subvol(struct btrfs_root *root, char *name,
-                                 int namelen)
+static noinline int create_subvol(struct btrfs_root *root,
+                                 struct dentry *dentry,
+                                 char *name, int namelen)
 {
        struct btrfs_trans_handle *trans;
        struct btrfs_key key;
@@ -75,11 +81,12 @@ static noinline int create_subvol(struct btrfs_root *root, char *name,
        if (ret)
                goto fail;
 
-       leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
-                                     objectid, trans->transid, 0, 0,
-                                     0, 0);
-       if (IS_ERR(leaf))
-               return PTR_ERR(leaf);
+       leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
+                                     objectid, trans->transid, 0, 0, 0);
+       if (IS_ERR(leaf)) {
+               ret = PTR_ERR(leaf);
+               goto fail;
+       }
 
        btrfs_set_header_nritems(leaf, 0);
        btrfs_set_header_level(leaf, 0);
@@ -97,13 +104,15 @@ static noinline int create_subvol(struct btrfs_root *root, char *name,
        inode_item->generation = cpu_to_le64(1);
        inode_item->size = cpu_to_le64(3);
        inode_item->nlink = cpu_to_le32(1);
-       inode_item->nblocks = cpu_to_le64(1);
+       inode_item->nbytes = cpu_to_le64(root->leafsize);
        inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
 
        btrfs_set_root_bytenr(&root_item, leaf->start);
+       btrfs_set_root_generation(&root_item, trans->transid);
        btrfs_set_root_level(&root_item, 0);
        btrfs_set_root_refs(&root_item, 1);
        btrfs_set_root_used(&root_item, 0);
+       btrfs_set_root_last_snapshot(&root_item, 0);
 
        memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
        root_item.drop_level = 0;
@@ -129,13 +138,13 @@ static noinline int create_subvol(struct btrfs_root *root, char *name,
        dir = root->fs_info->sb->s_root->d_inode;
        ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
                                    name, namelen, dir->i_ino, &key,
-                                   BTRFS_FT_DIR);
+                                   BTRFS_FT_DIR, 0);
        if (ret)
                goto fail;
 
        ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
                             name, namelen, objectid,
-                            root->fs_info->sb->s_root->d_inode->i_ino);
+                            root->fs_info->sb->s_root->d_inode->i_ino, 0);
        if (ret)
                goto fail;
 
@@ -149,14 +158,11 @@ static noinline int create_subvol(struct btrfs_root *root, char *name,
        trans = btrfs_start_transaction(new_root, 1);
        BUG_ON(!trans);
 
-       ret = btrfs_create_subvol_root(new_root, trans, new_dirid,
+       ret = btrfs_create_subvol_root(new_root, dentry, trans, new_dirid,
                                       BTRFS_I(dir)->block_group);
        if (ret)
                goto fail;
 
-       /* Invalidate existing dcache entry for new subvolume. */
-       btrfs_invalidate_dcache_root(root, name, namelen);
-
 fail:
        nr = trans->blocks_used;
        err = btrfs_commit_transaction(trans, new_root);
@@ -164,7 +170,6 @@ fail:
                ret = err;
 fail_commit:
        btrfs_btree_balance_dirty(root, nr);
-       btrfs_throttle(root);
        return ret;
 }
 
@@ -206,15 +211,83 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
 
 fail_unlock:
        btrfs_btree_balance_dirty(root, nr);
-       btrfs_throttle(root);
        return ret;
 }
 
+/* copy of may_create in fs/namei.c() */
+static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
+{
+       if (child->d_inode)
+               return -EEXIST;
+       if (IS_DEADDIR(dir))
+               return -ENOENT;
+       return inode_permission(dir, MAY_WRITE | MAY_EXEC);
+}
+
+/*
+ * Create a new subvolume below @parent.  This is largely modeled after
+ * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
+ * inside this filesystem so it's quite a bit simpler.
+ */
+static noinline int btrfs_mksubvol(struct path *parent, char *name,
+                                  int mode, int namelen)
+{
+       struct dentry *dentry;
+       int error;
+
+       mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
+
+       dentry = lookup_one_len(name, parent->dentry, namelen);
+       error = PTR_ERR(dentry);
+       if (IS_ERR(dentry))
+               goto out_unlock;
+
+       error = -EEXIST;
+       if (dentry->d_inode)
+               goto out_dput;
+
+       if (!IS_POSIXACL(parent->dentry->d_inode))
+               mode &= ~current->fs->umask;
+       error = mnt_want_write(parent->mnt);
+       if (error)
+               goto out_dput;
+
+       error = btrfs_may_create(parent->dentry->d_inode, dentry);
+       if (error)
+               goto out_drop_write;
+
+       /*
+        * Actually perform the low-level subvolume creation after all
+        * this VFS fuzz.
+        *
+        * Eventually we want to pass in an inode under which we create this
+        * subvolume, but for now all are under the filesystem root.
+        *
+        * Also we should pass on the mode eventually to allow creating new
+        * subvolume with specific mode bits.
+        */
+       error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root, dentry,
+                             name, namelen);
+       if (error)
+               goto out_drop_write;
+
+       fsnotify_mkdir(parent->dentry->d_inode, dentry);
+out_drop_write:
+       mnt_drop_write(parent->mnt);
+out_dput:
+       dput(dentry);
+out_unlock:
+       mutex_unlock(&parent->dentry->d_inode->i_mutex);
+       return error;
+}
+
+
 int btrfs_defrag_file(struct file *file)
 {
        struct inode *inode = fdentry(file)->d_inode;
        struct btrfs_root *root = BTRFS_I(inode)->root;
        struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
+       struct btrfs_ordered_extent *ordered;
        struct page *page;
        unsigned long last_index;
        unsigned long ra_pages = root->fs_info->bdi.ra_pages;
@@ -236,6 +309,7 @@ int btrfs_defrag_file(struct file *file)
                                       min(last_index, i + ra_pages - 1));
                }
                total_read++;
+again:
                page = grab_cache_page(inode->i_mapping, i);
                if (!page)
                        goto out_unlock;
@@ -249,20 +323,30 @@ int btrfs_defrag_file(struct file *file)
                        }
                }
 
-#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
-               ClearPageDirty(page);
-#else
-               cancel_dirty_page(page, PAGE_CACHE_SIZE);
-#endif
                wait_on_page_writeback(page);
-               set_page_extent_mapped(page);
 
                page_start = (u64)page->index << PAGE_CACHE_SHIFT;
                page_end = page_start + PAGE_CACHE_SIZE - 1;
-
                lock_extent(io_tree, page_start, page_end, GFP_NOFS);
-               set_extent_delalloc(io_tree, page_start,
-                                   page_end, GFP_NOFS);
+
+               ordered = btrfs_lookup_ordered_extent(inode, page_start);
+               if (ordered) {
+                       unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
+                       unlock_page(page);
+                       page_cache_release(page);
+                       btrfs_start_ordered_extent(inode, ordered, 1);
+                       btrfs_put_ordered_extent(ordered);
+                       goto again;
+               }
+               set_page_extent_mapped(page);
+
+               /*
+                * this makes sure page_mkwrite is called on the
+                * page if it is dirtied again later
+                */
+               clear_page_dirty_for_io(page);
+
+               btrfs_set_extent_delalloc(inode, page_start, page_end);
 
                unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
                set_page_dirty(page);
@@ -294,6 +378,9 @@ static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
        int namelen;
        int mod = 0;
 
+       if (root->fs_info->sb->s_flags & MS_RDONLY)
+               return -EROFS;
+
        vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
 
        if (!vol_args)
@@ -303,14 +390,11 @@ static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
                ret = -EFAULT;
                goto out;
        }
+
+       vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
        namelen = strlen(vol_args->name);
-       if (namelen > BTRFS_VOL_NAME_MAX) {
-               ret = -EINVAL;
-               goto out;
-       }
 
-       mutex_lock(&root->fs_info->alloc_mutex);
-       mutex_lock(&root->fs_info->chunk_mutex);
+       mutex_lock(&root->fs_info->volume_mutex);
        sizestr = vol_args->name;
        devstr = strchr(sizestr, ':');
        if (devstr) {
@@ -321,7 +405,7 @@ static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
                devid = simple_strtoull(devstr, &end, 10);
                printk(KERN_INFO "resizing devid %llu\n", devid);
        }
-       device = btrfs_find_device(root, devid, NULL);
+       device = btrfs_find_device(root, devid, NULL, NULL);
        if (!device) {
                printk(KERN_INFO "resizer unable to find device %llu\n", devid);
                ret = -EINVAL;
@@ -380,16 +464,16 @@ static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
        }
 
 out_unlock:
-       mutex_lock(&root->fs_info->alloc_mutex);
-       mutex_lock(&root->fs_info->chunk_mutex);
+       mutex_unlock(&root->fs_info->volume_mutex);
 out:
        kfree(vol_args);
        return ret;
 }
 
-static noinline int btrfs_ioctl_snap_create(struct btrfs_root *root,
+static noinline int btrfs_ioctl_snap_create(struct file *file,
                                            void __user *arg)
 {
+       struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
        struct btrfs_ioctl_vol_args *vol_args;
        struct btrfs_dir_item *di;
        struct btrfs_path *path;
@@ -397,6 +481,9 @@ static noinline int btrfs_ioctl_snap_create(struct btrfs_root *root,
        int namelen;
        int ret;
 
+       if (root->fs_info->sb->s_flags & MS_RDONLY)
+               return -EROFS;
+
        vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
 
        if (!vol_args)
@@ -407,11 +494,8 @@ static noinline int btrfs_ioctl_snap_create(struct btrfs_root *root,
                goto out;
        }
 
+       vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
        namelen = strlen(vol_args->name);
-       if (namelen > BTRFS_VOL_NAME_MAX) {
-               ret = -EINVAL;
-               goto out;
-       }
        if (strchr(vol_args->name, '/')) {
                ret = -EINVAL;
                goto out;
@@ -439,12 +523,14 @@ static noinline int btrfs_ioctl_snap_create(struct btrfs_root *root,
                goto out;
        }
 
-       mutex_lock(&root->fs_info->drop_mutex);
-       if (root == root->fs_info->tree_root)
-               ret = create_subvol(root, vol_args->name, namelen);
-       else
+       if (root == root->fs_info->tree_root) {
+               ret = btrfs_mksubvol(&file->f_path, vol_args->name,
+                                    file->f_path.dentry->d_inode->i_mode,
+                                    namelen);
+       } else {
                ret = create_snapshot(root, vol_args->name, namelen);
-       mutex_unlock(&root->fs_info->drop_mutex);
+       }
+
 out:
        kfree(vol_args);
        return ret;
@@ -454,6 +540,11 @@ static int btrfs_ioctl_defrag(struct file *file)
 {
        struct inode *inode = fdentry(file)->d_inode;
        struct btrfs_root *root = BTRFS_I(inode)->root;
+       int ret;
+
+       ret = mnt_want_write(file->f_path.mnt);
+       if (ret)
+               return ret;
 
        switch (inode->i_mode & S_IFMT) {
        case S_IFDIR:
@@ -482,6 +573,7 @@ long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
                ret = -EFAULT;
                goto out;
        }
+       vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
        ret = btrfs_init_new_device(root, vol_args->name);
 
 out:
@@ -494,6 +586,9 @@ long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
        struct btrfs_ioctl_vol_args *vol_args;
        int ret;
 
+       if (root->fs_info->sb->s_flags & MS_RDONLY)
+               return -EROFS;
+
        vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
 
        if (!vol_args)
@@ -503,6 +598,7 @@ long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
                ret = -EFAULT;
                goto out;
        }
+       vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
        ret = btrfs_rm_device(root, vol_args->name);
 
 out:
@@ -510,56 +606,68 @@ out:
        return ret;
 }
 
-int dup_item_to_inode(struct btrfs_trans_handle *trans,
-                      struct btrfs_root *root,
-                      struct btrfs_path *path,
-                      struct extent_buffer *leaf,
-                      int slot,
-                      struct btrfs_key *key,
-                      u64 destino)
-{
-       char *dup;
-       int len = btrfs_item_size_nr(leaf, slot);
-       struct btrfs_key ckey = *key;
-       int ret = 0;
-
-       dup = kmalloc(len, GFP_NOFS);
-       if (!dup)
-               return -ENOMEM;
-
-       read_extent_buffer(leaf, dup, btrfs_item_ptr_offset(leaf, slot), len);
-       btrfs_release_path(root, path);
-
-       ckey.objectid = destino;
-       ret = btrfs_insert_item(trans, root, &ckey, dup, len);
-       kfree(dup);
-       return ret;
-}
-
-long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
+long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, u64 off,
+                      u64 olen, u64 destoff)
 {
        struct inode *inode = fdentry(file)->d_inode;
        struct btrfs_root *root = BTRFS_I(inode)->root;
        struct file *src_file;
        struct inode *src;
        struct btrfs_trans_handle *trans;
-       int ret;
-       u64 pos;
        struct btrfs_path *path;
-       struct btrfs_key key;
        struct extent_buffer *leaf;
+       char *buf;
+       struct btrfs_key key;
        u32 nritems;
        int slot;
+       int ret;
+       u64 len = olen;
+       u64 bs = root->fs_info->sb->s_blocksize;
+       u64 hint_byte;
 
-       src_file = fget(src_fd);
+       /*
+        * TODO:
+        * - split compressed inline extents.  annoying: we need to
+        *   decompress into destination's address_space (the file offset
+        *   may change, so source mapping won't do), then recompress (or
+        *   otherwise reinsert) a subrange.
+        * - allow ranges within the same file to be cloned (provided
+        *   they don't overlap)?
+        */
+
+       ret = mnt_want_write(file->f_path.mnt);
+       if (ret)
+               return ret;
+
+       src_file = fget(srcfd);
        if (!src_file)
                return -EBADF;
        src = src_file->f_dentry->d_inode;
 
+       ret = -EINVAL;
+       if (src == inode)
+               goto out_fput;
+
+       ret = -EISDIR;
+       if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
+               goto out_fput;
+
        ret = -EXDEV;
-       if (src->i_sb != inode->i_sb)
+       if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
                goto out_fput;
 
+       ret = -ENOMEM;
+       buf = vmalloc(btrfs_level_size(root, 0));
+       if (!buf)
+               goto out_fput;
+
+       path = btrfs_alloc_path();
+       if (!path) {
+               vfree(buf);
+               goto out_fput;
+       }
+       path->reada = 2;
+
        if (inode < src) {
                mutex_lock(&inode->i_mutex);
                mutex_lock(&src->i_mutex);
@@ -568,31 +676,49 @@ long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
                mutex_lock(&inode->i_mutex);
        }
 
-       ret = -ENOTEMPTY;
-       if (inode->i_size)
+       /* determine range to clone */
+       ret = -EINVAL;
+       if (off >= src->i_size || off + len > src->i_size)
+               goto out_unlock;
+       if (len == 0)
+               olen = len = src->i_size - off;
+       /* if we extend to eof, continue to block boundary */
+       if (off + len == src->i_size)
+               len = ((src->i_size + bs-1) & ~(bs-1))
+                       - off;
+
+       /* verify the end result is block aligned */
+       if ((off & (bs-1)) ||
+           ((off + len) & (bs-1)))
                goto out_unlock;
 
+       printk("final src extent is %llu~%llu\n", off, len);
+       printk("final dst extent is %llu~%llu\n", destoff, len);
+
        /* do any pending delalloc/csum calc on src, one way or
           another, and lock file content */
        while (1) {
-               filemap_write_and_wait(src->i_mapping);
-               lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
-               if (BTRFS_I(src)->delalloc_bytes == 0)
+               struct btrfs_ordered_extent *ordered;
+               lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
+               ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
+               if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
                        break;
-               unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
+               unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
+               if (ordered)
+                       btrfs_put_ordered_extent(ordered);
+               btrfs_wait_ordered_range(src, off, off+len);
        }
 
-       trans = btrfs_start_transaction(root, 0);
-       path = btrfs_alloc_path();
-       if (!path) {
-               ret = -ENOMEM;
-               goto out;
-       }
-       key.offset = 0;
-       key.type = BTRFS_EXTENT_DATA_KEY;
+       trans = btrfs_start_transaction(root, 1);
+       BUG_ON(!trans);
+
+       /* punch hole in destination first */
+       btrfs_drop_extents(trans, root, inode, off, off+len, 0, &hint_byte);
+
+       /* clone data */
        key.objectid = src->i_ino;
-       pos = 0;
-       path->reada = 2;
+       key.type = BTRFS_EXTENT_DATA_KEY;
+       key.offset = 0;
 
        while (1) {
                /*
@@ -603,90 +729,232 @@ long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
                if (ret < 0)
                        goto out;
 
-               if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
+               nritems = btrfs_header_nritems(path->nodes[0]);
+               if (path->slots[0] >= nritems) {
                        ret = btrfs_next_leaf(root, path);
                        if (ret < 0)
                                goto out;
                        if (ret > 0)
                                break;
+                       nritems = btrfs_header_nritems(path->nodes[0]);
                }
                leaf = path->nodes[0];
                slot = path->slots[0];
-               btrfs_item_key_to_cpu(leaf, &key, slot);
-               nritems = btrfs_header_nritems(leaf);
 
+               btrfs_item_key_to_cpu(leaf, &key, slot);
                if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
                    key.objectid != src->i_ino)
                        break;
 
                if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
                        struct btrfs_file_extent_item *extent;
-                       int found_type;
-                       pos = key.offset;
+                       int type;
+                       u32 size;
+                       struct btrfs_key new_key;
+                       u64 disko = 0, diskl = 0;
+                       u64 datao = 0, datal = 0;
+                       u8 comp;
+
+                       size = btrfs_item_size_nr(leaf, slot);
+                       read_extent_buffer(leaf, buf,
+                                          btrfs_item_ptr_offset(leaf, slot),
+                                          size);
+
                        extent = btrfs_item_ptr(leaf, slot,
                                                struct btrfs_file_extent_item);
-                       found_type = btrfs_file_extent_type(leaf, extent);
-                       if (found_type == BTRFS_FILE_EXTENT_REG) {
-                               u64 len = btrfs_file_extent_num_bytes(leaf,
-                                                                     extent);
-                               u64 ds = btrfs_file_extent_disk_bytenr(leaf,
-                                                                      extent);
-                               u64 dl = btrfs_file_extent_disk_num_bytes(leaf,
-                                                                extent);
-                               u64 off = btrfs_file_extent_offset(leaf,
-                                                                  extent);
-                               btrfs_insert_file_extent(trans, root,
-                                                        inode->i_ino, pos,
-                                                        ds, dl, len, off);
-                               /* ds == 0 means there's a hole */
-                               if (ds != 0) {
-                                       btrfs_inc_extent_ref(trans, root,
-                                                    ds, dl,
-                                                    root->root_key.objectid,
-                                                    trans->transid,
-                                                    inode->i_ino, pos);
+                       comp = btrfs_file_extent_compression(leaf, extent);
+                       type = btrfs_file_extent_type(leaf, extent);
+                       if (type == BTRFS_FILE_EXTENT_REG) {
+                               disko = btrfs_file_extent_disk_bytenr(leaf, extent);
+                               diskl = btrfs_file_extent_disk_num_bytes(leaf, extent);
+                               datao = btrfs_file_extent_offset(leaf, extent);
+                               datal = btrfs_file_extent_num_bytes(leaf, extent);
+                       } else if (type == BTRFS_FILE_EXTENT_INLINE) {
+                               /* take upper bound, may be compressed */
+                               datal = btrfs_file_extent_ram_bytes(leaf,
+                                                                   extent);
+                       }
+                       btrfs_release_path(root, path);
+
+                       if (key.offset + datal < off ||
+                           key.offset >= off+len)
+                               goto next;
+
+                       memcpy(&new_key, &key, sizeof(new_key));
+                       new_key.objectid = inode->i_ino;
+                       new_key.offset = key.offset + destoff - off;
+
+                       if (type == BTRFS_FILE_EXTENT_REG) {
+                               ret = btrfs_insert_empty_item(trans, root, path,
+                                                             &new_key, size);
+                               if (ret)
+                                       goto out;
+
+                               leaf = path->nodes[0];
+                               slot = path->slots[0];
+                               write_extent_buffer(leaf, buf,
+                                           btrfs_item_ptr_offset(leaf, slot),
+                                           size);
+
+                               extent = btrfs_item_ptr(leaf, slot,
+                                               struct btrfs_file_extent_item);
+                               printk("  orig disk %llu~%llu data %llu~%llu\n",
+                                      disko, diskl, datao, datal);
+
+                               if (off > key.offset) {
+                                       datao += off - key.offset;
+                                       datal -= off - key.offset;
+                               }
+                               if (key.offset + datao + datal + key.offset >
+                                   off + len)
+                                       datal = off + len - key.offset - datao;
+                               /* disko == 0 means it's a hole */
+                               if (!disko)
+                                       datao = 0;
+                               printk(" final disk %llu~%llu data %llu~%llu\n",
+                                      disko, diskl, datao, datal);
+
+                               btrfs_set_file_extent_offset(leaf, extent,
+                                                            datao);
+                               btrfs_set_file_extent_num_bytes(leaf, extent,
+                                                               datal);
+                               if (disko) {
+                                       inode_add_bytes(inode, datal);
+                                       ret = btrfs_inc_extent_ref(trans, root,
+                                                  disko, diskl, leaf->start,
+                                                  root->root_key.objectid,
+                                                  trans->transid,
+                                                  inode->i_ino);
+                                       BUG_ON(ret);
+                               }
+                       } else if (type == BTRFS_FILE_EXTENT_INLINE) {
+                               u64 skip = 0;
+                               u64 trim = 0;
+                               if (off > key.offset) {
+                                       skip = off - key.offset;
+                                       new_key.offset += skip;
+                               }
+                               if (key.offset + datal > off+len)
+                                       trim = key.offset + datal - (off+len);
+                               printk("len %lld skip %lld trim %lld\n",
+                                      datal, skip, trim);
+                               if (comp && (skip || trim)) {
+                                       printk("btrfs clone_range can't split compressed inline extents yet\n");
+                                       ret = -EINVAL;
+                                       goto out;
                                }
-                               pos = key.offset + len;
-                       } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
-                               ret = dup_item_to_inode(trans, root, path,
-                                                       leaf, slot, &key,
-                                                       inode->i_ino);
+                               size -= skip + trim;
+                               datal -= skip + trim;
+                               ret = btrfs_insert_empty_item(trans, root, path,
+                                                             &new_key, size);
                                if (ret)
                                        goto out;
-                               pos = key.offset + btrfs_item_size_nr(leaf,
-                                                                     slot);
+
+                               if (skip) {
+                                       u32 start = btrfs_file_extent_calc_inline_size(0);
+                                       memmove(buf+start, buf+start+skip,
+                                               datal);
+                               }
+
+                               leaf = path->nodes[0];
+                               slot = path->slots[0];
+                               write_extent_buffer(leaf, buf,
+                                           btrfs_item_ptr_offset(leaf, slot),
+                                           size);
+                               inode_add_bytes(inode, datal);
                        }
-               } else if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
-                       ret = dup_item_to_inode(trans, root, path, leaf,
-                                               slot, &key, inode->i_ino);
 
+                       btrfs_mark_buffer_dirty(leaf);
+               }
+
+               if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
+                       u32 size;
+                       struct btrfs_key new_key;
+                       u64 coverslen;
+                       int coff, clen;
+
+                       size = btrfs_item_size_nr(leaf, slot);
+                       coverslen = (size / BTRFS_CRC32_SIZE) <<
+                               root->fs_info->sb->s_blocksize_bits;
+                       printk("csums for %llu~%llu\n",
+                              key.offset, coverslen);
+                       if (key.offset + coverslen < off ||
+                           key.offset >= off+len)
+                               goto next;
+
+                       read_extent_buffer(leaf, buf,
+                                          btrfs_item_ptr_offset(leaf, slot),
+                                          size);
+                       btrfs_release_path(root, path);
+
+                       coff = 0;
+                       if (off > key.offset)
+                               coff = ((off - key.offset) >>
+                                       root->fs_info->sb->s_blocksize_bits) *
+                                       BTRFS_CRC32_SIZE;
+                       clen = size - coff;
+                       if (key.offset + coverslen > off+len)
+                               clen -= ((key.offset+coverslen-off-len) >>
+                                        root->fs_info->sb->s_blocksize_bits) *
+                                       BTRFS_CRC32_SIZE;
+                       printk(" will dup %d~%d of %d\n",
+                              coff, clen, size);
+
+                       memcpy(&new_key, &key, sizeof(new_key));
+                       new_key.objectid = inode->i_ino;
+                       new_key.offset = key.offset + destoff - off;
+
+                       ret = btrfs_insert_empty_item(trans, root, path,
+                                                     &new_key, clen);
                        if (ret)
                                goto out;
+
+                       leaf = path->nodes[0];
+                       slot = path->slots[0];
+                       write_extent_buffer(leaf, buf + coff,
+                                           btrfs_item_ptr_offset(leaf, slot),
+                                           clen);
+                       btrfs_mark_buffer_dirty(leaf);
                }
-               key.offset++;
+
+       next:
                btrfs_release_path(root, path);
+               key.offset++;
        }
-
        ret = 0;
 out:
-       btrfs_free_path(path);
-
-       inode->i_blocks = src->i_blocks;
-       i_size_write(inode, src->i_size);
-       btrfs_update_inode(trans, root, inode);
-
-       unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
-
+       btrfs_release_path(root, path);
+       if (ret == 0) {
+               inode->i_mtime = inode->i_ctime = CURRENT_TIME;
+               if (destoff + olen > inode->i_size)
+                       btrfs_i_size_write(inode, destoff + olen);
+               BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
+               ret = btrfs_update_inode(trans, root, inode);
+       }
        btrfs_end_transaction(trans, root);
-
+       unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
+       if (ret)
+               vmtruncate(inode, 0);
 out_unlock:
        mutex_unlock(&src->i_mutex);
        mutex_unlock(&inode->i_mutex);
+       vfree(buf);
+       btrfs_free_path(path);
 out_fput:
        fput(src_file);
        return ret;
 }
 
+long btrfs_ioctl_clone_range(struct file *file, unsigned long argptr)
+{
+       struct btrfs_ioctl_clone_range_args args;
+
+       if (copy_from_user(&args, (void *)argptr, sizeof(args)))
+               return -EFAULT;
+       return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
+                                args.src_length, args.dest_offset);
+}
+
 /*
  * there are many ways the trans_start and trans_end ioctls can lead
  * to deadlocks.  They should only be used by applications that
@@ -707,7 +975,16 @@ long btrfs_ioctl_trans_start(struct file *file)
                ret = -EINPROGRESS;
                goto out;
        }
-       trans = btrfs_start_transaction(root, 0);
+
+       ret = mnt_want_write(file->f_path.mnt);
+       if (ret)
+               goto out;
+
+       mutex_lock(&root->fs_info->trans_mutex);
+       root->fs_info->open_ioctl_trans++;
+       mutex_unlock(&root->fs_info->trans_mutex);
+
+       trans = btrfs_start_ioctl_transaction(root, 0);
        if (trans)
                file->private_data = trans;
        else
@@ -736,7 +1013,12 @@ long btrfs_ioctl_trans_end(struct file *file)
                goto out;
        }
        btrfs_end_transaction(trans, root);
-       file->private_data = 0;
+       file->private_data = NULL;
+
+       mutex_lock(&root->fs_info->trans_mutex);
+       root->fs_info->open_ioctl_trans--;
+       mutex_unlock(&root->fs_info->trans_mutex);
+
 out:
        return ret;
 }
@@ -748,7 +1030,7 @@ long btrfs_ioctl(struct file *file, unsigned int
 
        switch (cmd) {
        case BTRFS_IOC_SNAP_CREATE:
-               return btrfs_ioctl_snap_create(root, (void __user *)arg);
+               return btrfs_ioctl_snap_create(file, (void __user *)arg);
        case BTRFS_IOC_DEFRAG:
                return btrfs_ioctl_defrag(file);
        case BTRFS_IOC_RESIZE:
@@ -760,12 +1042,15 @@ long btrfs_ioctl(struct file *file, unsigned int
        case BTRFS_IOC_BALANCE:
                return btrfs_balance(root->fs_info->dev_root);
        case BTRFS_IOC_CLONE:
-               return btrfs_ioctl_clone(file, arg);
+               return btrfs_ioctl_clone(file, arg, 0, 0, 0);
+       case BTRFS_IOC_CLONE_RANGE:
+               return btrfs_ioctl_clone_range(file, arg);
        case BTRFS_IOC_TRANS_START:
                return btrfs_ioctl_trans_start(file);
        case BTRFS_IOC_TRANS_END:
                return btrfs_ioctl_trans_end(file);
        case BTRFS_IOC_SYNC:
+               btrfs_start_delalloc_inodes(root);
                btrfs_sync_fs(file->f_dentry->d_sb, 1);
                return 0;
        }