Commit 971330f94f for qemu.org

commit 971330f94f948a3c90291d80df9560ed40dd00a6
Author: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
Date:   Wed Sep 23 19:22:24 2026 +0300

    block: allocate aligned write buffer for 'truncate -m full'

    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>
    Message-ID: <20260923162224.34946-1-andrey.drobyshev@virtuozzo.com>
    Reviewed-by: Kevin Wolf <kwolf@redhat.com>
    Signed-off-by: Kevin Wolf <kwolf@redhat.com>

diff --git a/block/file-posix.c b/block/file-posix.c
index 9aad156aad..39d6a34456 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;
 }