Btrfs: Update clone file ioctl
This patch updates the file clone ioctl for the tree locking and new data ordered code. --- Signed-off-by: Chris Mason <chris.mason@oracle.com>
This commit is contained in:
parent
b48652c101
commit
ae01a0abf3
1 changed files with 71 additions and 81 deletions
152
fs/btrfs/ioctl.c
152
fs/btrfs/ioctl.c
|
@ -517,32 +517,6 @@ out:
|
||||||
return ret;
|
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 src_fd)
|
||||||
{
|
{
|
||||||
struct inode *inode = fdentry(file)->d_inode;
|
struct inode *inode = fdentry(file)->d_inode;
|
||||||
|
@ -550,23 +524,42 @@ long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
|
||||||
struct file *src_file;
|
struct file *src_file;
|
||||||
struct inode *src;
|
struct inode *src;
|
||||||
struct btrfs_trans_handle *trans;
|
struct btrfs_trans_handle *trans;
|
||||||
int ret;
|
struct btrfs_ordered_extent *ordered;
|
||||||
u64 pos;
|
|
||||||
struct btrfs_path *path;
|
struct btrfs_path *path;
|
||||||
struct btrfs_key key;
|
|
||||||
struct extent_buffer *leaf;
|
struct extent_buffer *leaf;
|
||||||
|
char *buf;
|
||||||
|
struct btrfs_key key;
|
||||||
|
struct btrfs_key new_key;
|
||||||
|
u32 size;
|
||||||
u32 nritems;
|
u32 nritems;
|
||||||
int slot;
|
int slot;
|
||||||
|
int ret;
|
||||||
|
|
||||||
src_file = fget(src_fd);
|
src_file = fget(src_fd);
|
||||||
if (!src_file)
|
if (!src_file)
|
||||||
return -EBADF;
|
return -EBADF;
|
||||||
src = src_file->f_dentry->d_inode;
|
src = src_file->f_dentry->d_inode;
|
||||||
|
|
||||||
ret = -EXDEV;
|
ret = -EISDIR;
|
||||||
if (src->i_sb != inode->i_sb)
|
if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
|
||||||
goto out_fput;
|
goto out_fput;
|
||||||
|
|
||||||
|
ret = -EXDEV;
|
||||||
|
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) {
|
if (inode < src) {
|
||||||
mutex_lock(&inode->i_mutex);
|
mutex_lock(&inode->i_mutex);
|
||||||
mutex_lock(&src->i_mutex);
|
mutex_lock(&src->i_mutex);
|
||||||
|
@ -582,24 +575,22 @@ long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
|
||||||
/* do any pending delalloc/csum calc on src, one way or
|
/* do any pending delalloc/csum calc on src, one way or
|
||||||
another, and lock file content */
|
another, and lock file content */
|
||||||
while (1) {
|
while (1) {
|
||||||
filemap_write_and_wait(src->i_mapping);
|
|
||||||
lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
|
lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
|
||||||
if (BTRFS_I(src)->delalloc_bytes == 0)
|
ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
|
||||||
|
if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
|
||||||
break;
|
break;
|
||||||
unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
|
unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
|
||||||
|
if (ordered)
|
||||||
|
btrfs_put_ordered_extent(ordered);
|
||||||
|
btrfs_wait_ordered_range(src, 0, (u64)-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
trans = btrfs_start_transaction(root, 0);
|
trans = btrfs_start_transaction(root, 1);
|
||||||
path = btrfs_alloc_path();
|
BUG_ON(!trans);
|
||||||
if (!path) {
|
|
||||||
ret = -ENOMEM;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
key.offset = 0;
|
|
||||||
key.type = BTRFS_EXTENT_DATA_KEY;
|
|
||||||
key.objectid = src->i_ino;
|
key.objectid = src->i_ino;
|
||||||
pos = 0;
|
key.type = BTRFS_EXTENT_DATA_KEY;
|
||||||
path->reada = 2;
|
key.offset = 0;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
/*
|
/*
|
||||||
|
@ -610,18 +601,19 @@ long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
goto out;
|
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);
|
ret = btrfs_next_leaf(root, path);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
goto out;
|
goto out;
|
||||||
if (ret > 0)
|
if (ret > 0)
|
||||||
break;
|
break;
|
||||||
|
nritems = btrfs_header_nritems(path->nodes[0]);
|
||||||
}
|
}
|
||||||
leaf = path->nodes[0];
|
leaf = path->nodes[0];
|
||||||
slot = path->slots[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 ||
|
if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
|
||||||
key.objectid != src->i_ino)
|
key.objectid != src->i_ino)
|
||||||
break;
|
break;
|
||||||
|
@ -629,66 +621,64 @@ long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
|
||||||
if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
|
if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
|
||||||
struct btrfs_file_extent_item *extent;
|
struct btrfs_file_extent_item *extent;
|
||||||
int found_type;
|
int found_type;
|
||||||
pos = key.offset;
|
|
||||||
extent = btrfs_item_ptr(leaf, slot,
|
extent = btrfs_item_ptr(leaf, slot,
|
||||||
struct btrfs_file_extent_item);
|
struct btrfs_file_extent_item);
|
||||||
found_type = btrfs_file_extent_type(leaf, extent);
|
found_type = btrfs_file_extent_type(leaf, extent);
|
||||||
if (found_type == BTRFS_FILE_EXTENT_REG) {
|
if (found_type == BTRFS_FILE_EXTENT_REG) {
|
||||||
u64 len = btrfs_file_extent_num_bytes(leaf,
|
|
||||||
extent);
|
|
||||||
u64 ds = btrfs_file_extent_disk_bytenr(leaf,
|
u64 ds = btrfs_file_extent_disk_bytenr(leaf,
|
||||||
extent);
|
extent);
|
||||||
u64 dl = btrfs_file_extent_disk_num_bytes(leaf,
|
u64 dl = btrfs_file_extent_disk_num_bytes(leaf,
|
||||||
extent);
|
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 */
|
/* ds == 0 means there's a hole */
|
||||||
if (ds != 0) {
|
if (ds != 0) {
|
||||||
btrfs_inc_extent_ref(trans, root,
|
ret = btrfs_inc_extent_ref(trans, root,
|
||||||
ds, dl,
|
ds, dl,
|
||||||
root->root_key.objectid,
|
root->root_key.objectid,
|
||||||
trans->transid,
|
trans->transid,
|
||||||
inode->i_ino, pos);
|
inode->i_ino, key.offset);
|
||||||
|
if (ret)
|
||||||
|
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);
|
|
||||||
if (ret)
|
|
||||||
goto out;
|
|
||||||
pos = key.offset + btrfs_item_size_nr(leaf,
|
|
||||||
slot);
|
|
||||||
}
|
}
|
||||||
} else if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
|
}
|
||||||
ret = dup_item_to_inode(trans, root, path, leaf,
|
|
||||||
slot, &key, inode->i_ino);
|
|
||||||
|
|
||||||
if (ret)
|
if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY ||
|
||||||
goto out;
|
btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
|
||||||
|
size = btrfs_item_size_nr(leaf, slot);
|
||||||
|
read_extent_buffer(leaf, buf,
|
||||||
|
btrfs_item_ptr_offset(leaf, slot),
|
||||||
|
size);
|
||||||
|
btrfs_release_path(root, path);
|
||||||
|
memcpy(&new_key, &key, sizeof(new_key));
|
||||||
|
new_key.objectid = inode->i_ino;
|
||||||
|
ret = btrfs_insert_item(trans, root, &new_key,
|
||||||
|
buf, size);
|
||||||
|
BUG_ON(ret);
|
||||||
|
} else {
|
||||||
|
btrfs_release_path(root, path);
|
||||||
}
|
}
|
||||||
key.offset++;
|
key.offset++;
|
||||||
btrfs_release_path(root, path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
out:
|
out:
|
||||||
btrfs_free_path(path);
|
btrfs_release_path(root, path);
|
||||||
|
if (ret == 0) {
|
||||||
inode->i_blocks = src->i_blocks;
|
inode->i_mtime = inode->i_ctime = CURRENT_TIME;
|
||||||
i_size_write(inode, src->i_size);
|
inode->i_blocks = src->i_blocks;
|
||||||
btrfs_update_inode(trans, root, inode);
|
btrfs_i_size_write(inode, src->i_size);
|
||||||
|
BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
|
||||||
unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
|
ret = btrfs_update_inode(trans, root, inode);
|
||||||
|
}
|
||||||
btrfs_end_transaction(trans, root);
|
btrfs_end_transaction(trans, root);
|
||||||
|
unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
|
||||||
|
if (ret)
|
||||||
|
vmtruncate(inode, 0);
|
||||||
out_unlock:
|
out_unlock:
|
||||||
mutex_unlock(&src->i_mutex);
|
mutex_unlock(&src->i_mutex);
|
||||||
mutex_unlock(&inode->i_mutex);
|
mutex_unlock(&inode->i_mutex);
|
||||||
|
vfree(buf);
|
||||||
|
btrfs_free_path(path);
|
||||||
out_fput:
|
out_fput:
|
||||||
fput(src_file);
|
fput(src_file);
|
||||||
return ret;
|
return ret;
|
||||||
|
|
Loading…
Add table
Reference in a new issue