file-posix: Fix alignment after reopen changing O_DIRECT

At the end of a reopen, we already call bdrv_refresh_limits(), which
should update bs->request_alignment according to the new file
descriptor. However, raw_probe_alignment() relies on s->needs_alignment
and just uses 1 if it isn't set. We neglected to update this field, so
starting with cache=writeback and then reopening with cache=none means
that we get an incorrect bs->request_alignment == 1 and unaligned
requests fail instead of being automatically aligned.

Fix this by recalculating s->needs_alignment in raw_refresh_limits()
before calling raw_probe_alignment().

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Message-Id: <20211104113109.56336-1-kwolf@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Message-Id: <20211115145409.176785-13-kwolf@redhat.com>
[hreitz: Fix iotest 142 for block sizes greater than 512 by operating on
         a file with a size of 1 MB]
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
Message-Id: <20211116101431.105252-1-hreitz@redhat.com>
This commit is contained in:
Kevin Wolf 2021-11-16 11:14:31 +01:00 committed by Hanna Reitz
parent c9d4e42a8f
commit 5dbd0ce115
3 changed files with 63 additions and 4 deletions

View file

@ -350,6 +350,35 @@ info block backing-file"
echo "$hmp_cmds" | run_qemu -drive "$files","$ids" | grep "Cache"
echo
echo "--- Alignment after changing O_DIRECT ---"
echo
# Directly test the protocol level: Can unaligned requests succeed even if
# O_DIRECT was only enabled through a reopen and vice versa?
# Ensure image size is a multiple of the sector size (required for O_DIRECT)
$QEMU_IMG create -f file "$TEST_IMG" 1M | _filter_img_create
# And write some data (not strictly necessary, but it feels better to actually
# have something to be read)
$QEMU_IO -f file -c 'write 0 4096' "$TEST_IMG" | _filter_qemu_io
$QEMU_IO --cache=writeback -f file $TEST_IMG <<EOF | _filter_qemu_io
read 42 42
reopen -o cache.direct=on
read 42 42
reopen -o cache.direct=off
read 42 42
EOF
$QEMU_IO --cache=none -f file $TEST_IMG <<EOF | _filter_qemu_io
read 42 42
reopen -o cache.direct=off
read 42 42
reopen -o cache.direct=on
read 42 42
EOF
# success, all done
echo "*** done"
rm -f $seq.full

View file

@ -747,4 +747,22 @@ cache.no-flush=on on backing-file
Cache mode: writeback
Cache mode: writeback, direct
Cache mode: writeback, ignore flushes
--- Alignment after changing O_DIRECT ---
Formatting 'TEST_DIR/t.IMGFMT', fmt=file size=1048576
wrote 4096/4096 bytes at offset 0
4 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
read 42/42 bytes at offset 42
42 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
read 42/42 bytes at offset 42
42 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
read 42/42 bytes at offset 42
42 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
read 42/42 bytes at offset 42
42 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
read 42/42 bytes at offset 42
42 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
read 42/42 bytes at offset 42
42 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
*** done