Browse Source

block/file-posix: Unaligned O_DIRECT block-status

Currently, qemu crashes whenever someone queries the block status of an
unaligned image tail of an O_DIRECT image:
$ echo > foo
$ qemu-img map --image-opts driver=file,filename=foo,cache.direct=on
Offset          Length          Mapped to       File
qemu-img: block/io.c:2093: bdrv_co_block_status: Assertion `*pnum &&
QEMU_IS_ALIGNED(*pnum, align) && align > offset - aligned_offset'
failed.

This is because bdrv_co_block_status() checks that the result returned
by the driver's implementation is aligned to the request_alignment, but
file-posix can fail to do so, which is actually mentioned in a comment
there: "[...] possibly including a partial sector at EOF".

Fix this by rounding up those partial sectors.

There are two possible alternative fixes:
(1) We could refuse to open unaligned image files with O_DIRECT
    altogether.  That sounds reasonable until you realize that qcow2
    does necessarily not fill up its metadata clusters, and that nobody
    runs qemu-img create with O_DIRECT.  Therefore, unpreallocated qcow2
    files usually have an unaligned image tail.

(2) bdrv_co_block_status() could ignore unaligned tails.  It actually
    throws away everything past the EOF already, so that sounds
    reasonable.
    Unfortunately, the block layer knows file lengths only with a
    granularity of BDRV_SECTOR_SIZE, so bdrv_co_block_status() usually
    would have to guess whether its file length information is inexact
    or whether the driver is broken.

Fixing what raw_co_block_status() returns is the safest thing to do.

There seems to be no other block driver that sets request_alignment and
does not make sure that it always returns aligned values.

Cc: qemu-stable@nongnu.org
Signed-off-by: Max Reitz <mreitz@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
master
Max Reitz 1 month ago
parent
commit
9c3db310ff
1 changed files with 16 additions and 0 deletions
  1. 16
    0
      block/file-posix.c

+ 16
- 0
block/file-posix.c View File

@@ -2488,6 +2488,8 @@ static int coroutine_fn raw_co_block_status(BlockDriverState *bs,
2488 2488
     off_t data = 0, hole = 0;
2489 2489
     int ret;
2490 2490
 
2491
+    assert(QEMU_IS_ALIGNED(offset | bytes, bs->bl.request_alignment));
2492
+
2491 2493
     ret = fd_open(bs);
2492 2494
     if (ret < 0) {
2493 2495
         return ret;
@@ -2513,6 +2515,20 @@ static int coroutine_fn raw_co_block_status(BlockDriverState *bs,
2513 2515
         /* On a data extent, compute bytes to the end of the extent,
2514 2516
          * possibly including a partial sector at EOF. */
2515 2517
         *pnum = MIN(bytes, hole - offset);
2518
+
2519
+        /*
2520
+         * We are not allowed to return partial sectors, though, so
2521
+         * round up if necessary.
2522
+         */
2523
+        if (!QEMU_IS_ALIGNED(*pnum, bs->bl.request_alignment)) {
2524
+            int64_t file_length = raw_getlength(bs);
2525
+            if (file_length > 0) {
2526
+                /* Ignore errors, this is just a safeguard */
2527
+                assert(hole == file_length);
2528
+            }
2529
+            *pnum = ROUND_UP(*pnum, bs->bl.request_alignment);
2530
+        }
2531
+
2516 2532
         ret = BDRV_BLOCK_DATA;
2517 2533
     } else {
2518 2534
         /* On a hole, compute bytes to the beginning of the next extent.  */

Loading…
Cancel
Save