summaryrefslogtreecommitdiff
path: root/block/file-posix.c
diff options
context:
space:
mode:
authorKevin Wolf <kwolf@redhat.com>2018-02-07 16:38:05 +0100
committerKevin Wolf <kwolf@redhat.com>2018-03-09 15:17:48 +0100
commit89b259eeaa0f4ad3fa89cbe500da9335536e20ee (patch)
tree01656995c7920de5b036a78703931d9928585185 /block/file-posix.c
parent4906da7e4de3720995b644f9903879cfd5a51a40 (diff)
downloadqemu-89b259eeaa0f4ad3fa89cbe500da9335536e20ee.tar.gz
file-posix: Fix no-op bdrv_truncate() with falloc preallocation
If bdrv_truncate() is called, but the requested size is the same as before, don't call posix_fallocate(), which returns -EINVAL for length zero and would therefore make bdrv_truncate() fail. The problem can be triggered by creating a zero-sized raw image with 'falloc' preallocation mode. Signed-off-by: Kevin Wolf <kwolf@redhat.com> Reviewed-by: Max Reitz <mreitz@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com>
Diffstat (limited to 'block/file-posix.c')
-rw-r--r--block/file-posix.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/block/file-posix.c b/block/file-posix.c
index fbc21a9921..d7fb772c14 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -1686,11 +1686,15 @@ static int raw_regular_truncate(int fd, int64_t offset, PreallocMode prealloc,
* file systems that do not support fallocate(), trying to check if a
* block is allocated before allocating it, so don't do that here.
*/
- result = -posix_fallocate(fd, current_length, offset - current_length);
- if (result != 0) {
- /* posix_fallocate() doesn't set errno. */
- error_setg_errno(errp, -result,
- "Could not preallocate new data");
+ if (offset != current_length) {
+ result = -posix_fallocate(fd, current_length, offset - current_length);
+ if (result != 0) {
+ /* posix_fallocate() doesn't set errno. */
+ error_setg_errno(errp, -result,
+ "Could not preallocate new data");
+ }
+ } else {
+ result = 0;
}
goto out;
#endif