[PATCH v2] block: allocate aligned write buffer for 'truncate -m full'

Andrey Drobyshev posted 1 patch 3 days, 2 hours ago
block/file-posix.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
[PATCH v2] block: allocate aligned write buffer for 'truncate -m full'
Posted by Andrey Drobyshev 3 days, 2 hours ago
In case we're truncating an image opened with O_DIRECT, we might get
-EINVAL on write with unaligned buffer.  In particular, when running
iotests/298 with '-nocache' we get:

qemu-io: Failed to resize underlying file: Could not write zeros for
preallocation: Invalid argument

Let's just allocate the buffer using qemu_blockalign0() instead.

Signed-off-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
Reviewed-by: Denis V. Lunev <den@openvz.org>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
---

iotests/298 with '-nocache' is still failing, so sending this once more.

v1 -> v2: use QEMU_AUTO_VFREE (Vladimir)

v1: https://lore.kernel.org/qemu-devel/20231211105559.316897-1-andrey.drobyshev@virtuozzo.com/

 block/file-posix.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/block/file-posix.c b/block/file-posix.c
index 9aad156aad4..39d6a34456c 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -2366,7 +2366,7 @@ static int handle_aiocb_truncate(void *opaque)
     RawPosixAIOData *aiocb = opaque;
     int result = 0;
     int64_t current_length = 0;
-    char *buf = NULL;
+    QEMU_AUTO_VFREE char *buf = NULL;
     struct stat st;
     int fd = aiocb->aio_fildes;
     int64_t offset = aiocb->aio_offset;
@@ -2433,7 +2433,7 @@ static int handle_aiocb_truncate(void *opaque)
             goto out;
         }
 
-        buf = g_malloc0(65536);
+        buf = qemu_blockalign0(aiocb->bs, 65536);
 
         seek_result = lseek(fd, current_length, SEEK_SET);
         if (seek_result < 0) {
@@ -2492,7 +2492,6 @@ out:
         }
     }
 
-    g_free(buf);
     return result;
 }
 
-- 
2.47.1
Re: [PATCH v2] block: allocate aligned write buffer for 'truncate -m full'
Posted by Kevin Wolf 1 day, 5 hours ago
Am 23.09.2026 um 18:22 hat Andrey Drobyshev geschrieben:
> In case we're truncating an image opened with O_DIRECT, we might get
> -EINVAL on write with unaligned buffer.  In particular, when running
> iotests/298 with '-nocache' we get:
> 
> qemu-io: Failed to resize underlying file: Could not write zeros for
> preallocation: Invalid argument
> 
> Let's just allocate the buffer using qemu_blockalign0() instead.
> 
> Signed-off-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
> Reviewed-by: Denis V. Lunev <den@openvz.org>
> Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>

Thanks, applied to the block branch.

Kevin