Btrfs: fix a crash of clone with inline extents's split
PATCH 1/1 Btrfs: fix a crash of clone with inline extents's split
xfstests's btrfs/035 triggers a BUG_ON, which we use to detect the split of inline extents in __btrfs_drop_extents().
For inline extents, we cannot duplicate another EXTENT_DATA item, because it breaks the rule of inline extents, that is, 'start offset' needs to be 0.
We have set limitations for the source inode's compressed inline extents, because it needs to decompress and recompress. Now the destination inode's inline extents also need similar limitations.
With this, xfstests btrfs/035 doesn't run into panic.
--- fs/btrfs/file.c | 15 ++++++++++++--- fs/btrfs/ioctl.c | 10 ++++++---- 2 files changed, 18 insertions(+), 7 deletions(-)
fs/btrfs/file.c +12 -3
@@ -798,7 +798,10 @@ next_slot:*/ if (start > key.offset && end < extent_end) {BUG_ON (del_nr >0 ); -BUG_ON (extent_type ==BTRFS_FILE_EXTENT_INLINE ); +if (extent_type ==BTRFS_FILE_EXTENT_INLINE ) { + ret = -EINVAL ; +break ; + }memcpy (&new_key, &key,sizeof (new_key)); new_key.offset = start;
@@ -841,7 +844,10 @@ next_slot:* | -------- extent -------- | */ if (start <= key.offset && end < extent_end) { -BUG_ON (extent_type ==BTRFS_FILE_EXTENT_INLINE ); +if (extent_type ==BTRFS_FILE_EXTENT_INLINE ) { + ret = -EINVAL ; +break ; + }memcpy (&new_key, &key,sizeof (new_key)); new_key.offset = end;
@@ -864,7 +870,10 @@ next_slot:*/ if (start > key.offset && end >= extent_end) {BUG_ON (del_nr >0 ); -BUG_ON (extent_type ==BTRFS_FILE_EXTENT_INLINE ); +if (extent_type ==BTRFS_FILE_EXTENT_INLINE ) { + ret = -EINVAL ; +break ; + }btrfs_set_file_extent_num_bytes (leaf, fi, start - key.offset);
fs/btrfs/ioctl.c +6 -4
@@ -3090,8 +3090,9 @@ process_slot: new_key.offset + datal, 1);if (ret) { -btrfs_abort_transaction (trans, root, - ret); +if (ret != -EINVAL ) +btrfs_abort_transaction (trans, + root, ret);btrfs_end_transaction (trans, root); goto out; }
@@ -3175,8 +3176,9 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,* 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)? +* +* - split destination inode's inline extents. The inline extents can +* be either compressed or non-compressed. */ /* the destination must be opened for writing */
Signature · 1 line
1.8.2.1
Quoted 9 lines · Liu Bo
xfstests's btrfs/035 triggers a
BUG_ON, which we use to detect the split of inline extents in__btrfs_drop_extents().For inline extents, we cannot duplicate another
EXTENT_DATAitem, because it breaks the rule of inline extents, that is, 'start offset' needs to be 0.We have set limitations for the source inode's compressed inline extents, because it needs to decompress and recompress. Now the destination inode's inline extents also need similar limitations.
The limitation (by lack of implementation, not by design) of compressed inline extents is there, but it's impossible to reach. The inline extents are never longer than the 'inline limit' (the ~3916 size), so the comment is more a note to the future.
You're adding another limitation to avoid a crash, but I don't agree that EINVAL is right here, due to the fact that it's lack of implementation, not a real error.
There are enough EINVAL's that verify correcntess of the input parameters and it's not always clear which one fails. The EOPNOTSUPP errocode is close to the true reason of the failure, but it could be misinterpreted as if the whole clone operation is not supported, so it's not all correct but IMO better than EINVAL.
The most common case of 'cp --reflink' is not affected by this.
Quoted 14 lines
With this, xfstests btrfs/035 doesn't run into panic.
Signed-off-by Liu Bo <[email protected]>--- fs/btrfs/file.c | 15 ++++++++++++--- fs/btrfs/ioctl.c | 10 ++++++---- 2 files changed, 18 insertions(+), 7 deletions(-)fs/btrfs/ioctl.c +0
-0
fs/btrfs/ioctl.c process_slot: · line 3095
Quoted 8 lines
fs/btrfs/ioctl.c +3
-2@@ -3090,8 +3090,9 @@ process_slot: new_key.offset + datal, 1);if (ret) { -btrfs_abort_transaction (trans, root, - ret); +if (ret != -EINVAL ) +btrfs_abort_transaction (trans, + root, ret);
The error comes from __btrfs_drop_extents and all callers would need to be updated (or at least reviewed) with the 'ret != ...' check as well, because it changes the semantics. And I'm not sure if to the right direction.
fs/btrfs/ioctl.c static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, · line 3180
Quoted 10 lines
fs/btrfs/ioctl.c +0
-2btrfs_end_transaction (trans, root); goto out; }@@ -3175,8 +3176,9 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,* 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)?
True, but unrelated.
Quoted 3 lines
fs/btrfs/ioctl.c +3
-0+* +* - split destination inode's inline extents. The inline extents can +* be either compressed or non-compressed.
Quoted 20 lines · David Sterba
Quoted 9 lines · Liu Bo
xfstests's btrfs/035 triggers a
BUG_ON, which we use to detect the split of inline extents in__btrfs_drop_extents().For inline extents, we cannot duplicate another
EXTENT_DATAitem, because it breaks the rule of inline extents, that is, 'start offset' needs to be 0.We have set limitations for the source inode's compressed inline extents, because it needs to decompress and recompress. Now the destination inode's inline extents also need similar limitations.
The limitation (by lack of implementation, not by design) of compressed inline extents is there, but it's impossible to reach. The inline extents are never longer than the 'inline limit' (the ~3916 size), so the comment is more a note to the future.
You're adding another limitation to avoid a crash, but I don't agree that EINVAL is right here, due to the fact that it's lack of implementation, not a real error.
Quoted 5 lines
There are enough EINVAL's that verify correcntess of the input parameters and it's not always clear which one fails. The EOPNOTSUPP errocode is close to the true reason of the failure, but it could be misinterpreted as if the whole clone operation is not supported, so it's not all correct but IMO better than EINVAL.
Yep, I was hesitating on these two errors while making the patch, but I prefer EINVAL rather than EOPNOTSUPP because of the reason you've stated.
I think it'd be good to add one more btrfs_printk message to clarify what's happening here, agree?
Quoted 22 lines
The most common case of 'cp --reflink' is not affected by this.
Quoted 19 lines
With this, xfstests btrfs/035 doesn't run into panic.
Signed-off-by Liu Bo <[email protected]>--- fs/btrfs/file.c | 15 ++++++++++++--- fs/btrfs/ioctl.c | 10 ++++++---- 2 files changed, 18 insertions(+), 7 deletions(-)fs/btrfs/ioctl.c +0
-2@@ -3090,8 +3090,9 @@ process_slot: new_key.offset + datal, 1);if (ret) { -btrfs_abort_transaction (trans, root, - ret);
Quoted 8 lines
fs/btrfs/ioctl.c process_slot: · line 3095
Quoted 3 lines
fs/btrfs/ioctl.c +3
-0+if (ret != -EINVAL ) +btrfs_abort_transaction (trans, + root, ret);The error comes from
__btrfs_drop_extentsand all callers would need to be updated (or at least reviewed) with the 'ret != ...' check as well, because it changes the semantics. And I'm not sure if to the right direction.
Good point, Dave, actually I missed this part before, just checked for callers of __btrfs_drop_extents() and btrfs_drop_extents(), luckily EINVAL is not a special one at these places, the error is just returned to upper callers.
Quoted 9 lines
Quoted 8 lines
fs/btrfs/ioctl.c +0
-0btrfs_end_transaction (trans, root); goto out; }@@ -3175,8 +3176,9 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,* decompress into destination's address_space (the file offset * may change, so source mapping won't do), then recompress (or * otherwise reinsert) a subrange.
Quoted 4 lines
fs/btrfs/ioctl.c static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, · line 3179
Quoted 2 lines
fs/btrfs/ioctl.c +0
-2-* - allow ranges within the same file to be cloned (provided -* they don't overlap)? True, but unrelated.
yep, that's right, will clean it up.
Thanks for the comments!
-liubo
Quoted 7 lines · Liu Bo
Quoted 5 lines · David Sterba
There are enough EINVAL's that verify correcntess of the input parameters and it's not always clear which one fails. The EOPNOTSUPP errocode is close to the true reason of the failure, but it could be misinterpreted as if the whole clone operation is not supported, so it's not all correct but IMO better than EINVAL.
Quoted 5 lines
Yep, I was hesitating on these two errors while making the patch, but I prefer EINVAL rather than EOPNOTSUPP because of the reason you've stated.
I think it'd be good to add one more
btrfs_printkmessage to clarify what's happening here, agree?
I don't think a printk is the right thing here, this means that if an error happens somebody has to look into the log what happened and act accordingly.
The EOPNOTSUPP errorcode would allow an application to do a fallback action, ie. copy the data instead of cloning. The same as if the clone ioctl would not exist at all.
EINVAL says "you didn't give me valid arguments to work with".
PATCH 1/1 Btrfs: fix a crash of clone with inline extents's split
In reply to Liu BoThanks for the BUG_ON() fix here.
Strangely, I'm now seeing EIO returned for reads following the second clone-range.
Please see the subsequent xfstests patch.
Cheers, David
PATCH 1/1 btrfs/035: update clone test to expect EOPNOTSUPP
With kernel commit 00fdf13a2e9f313a044288aa59d3b8ec29ff904a, the first clone-range overwrite attempt now fails with EOPNOTSUPP.
FIXME: The second clone-range causes EIO on subsequent read attempts.
--- tests/btrfs/035 | 10 ++++++++++ tests/btrfs/035.out | 5 +++++ 2 files changed, 15 insertions(+)
tests/btrfs/035 +10 -0
@@ -57,21 +57,31 @@ src_str="aaaaaaaaaa" echo -n "$src_str" > $SCRATCH_MNT/src $CLONER_PROG $SCRATCH_MNT/src $SCRATCH_MNT/src.clone1 +cat $SCRATCH_MNT/src.clone1 +echo src_str="bbbbbbbbbbcccccccccc" echo -n "$src_str" > $SCRATCH_MNT/src $CLONER_PROG $SCRATCH_MNT/src $SCRATCH_MNT/src.clone2 +cat $SCRATCH_MNT/src.clone2 +echo +# Prior to kernel commit 00fdf13a2e9f313a044288aa59d3b8ec29ff904a, this clone +# resulted in a BUG_ON in __btrfs_drop_extents(). The kernel now returns EINVAL +# up to userspace. snap_src_sz=`ls -lah $SCRATCH_MNT/src.clone1 | awk '{print $5}'` echo "attempting ioctl (src.clone1 src)" $CLONER_PROG -s 0 -d 0 -l ${snap_src_sz} \ $SCRATCH_MNT/src.clone1 $SCRATCH_MNT/src +cat $SCRATCH_MNT/src +echo snap_src_sz=`ls -lah $SCRATCH_MNT/src.clone2 | awk '{print $5}'` echo "attempting ioctl (src.clone2 src)" $CLONER_PROG -s 0 -d 0 -l ${snap_src_sz} \ $SCRATCH_MNT/src.clone2 $SCRATCH_MNT/src +cat $SCRATCH_MNT/src status=0 ; exit
tests/btrfs/035.out +5 -0
@@ -1,3 +1,8 @@ QA output created by 035 +aaaaaaaaaa +bbbbbbbbbbcccccccccc attempting ioctl (src.clone1 src) +clone failed: Operation not supported +bbbbbbbbbbcccccccccc attempting ioctl (src.clone2 src) +bbbbbbbbbbcccccccccc
Signature · 1 line
1.8.4.5