Btrfs: Fix file clone when source offset is not 0
authorLi Zefan <lizf@cn.fujitsu.com>
Wed, 26 Jan 2011 06:10:43 +0000 (14:10 +0800)
committerLi Zefan <lizf@cn.fujitsu.com>
Wed, 26 Jan 2011 17:11:18 +0000 (01:11 +0800)
Suppose:
- the source extent is: [0, 100]
- the src offset is 10
- the clone length is 90
- the dest offset is 0

This statement:

new_key.offset = key.offset + destoff - off

will produce such an extent for the dest file:

[ino, BTRFS_EXTENT_DATA_KEY, -10]

, which is obviously wrong.

Signed-off-by: Li Zefan <lizf@cn.fujitsu.com>
fs/btrfs/ioctl.c

index f87552a1d7ea0beeb540aa59ddac3754242304cc..1b61dab64062a848f5b995f9178b01496a5eb0dd 100644 (file)
@@ -1788,7 +1788,10 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
 
                        memcpy(&new_key, &key, sizeof(new_key));
                        new_key.objectid = inode->i_ino;
-                       new_key.offset = key.offset + destoff - off;
+                       if (off <= key.offset)
+                               new_key.offset = key.offset + destoff - off;
+                       else
+                               new_key.offset = destoff;
 
                        trans = btrfs_start_transaction(root, 1);
                        if (IS_ERR(trans)) {