block: Image locking series
v10 cover block: Image locking series
v10: While we still don't have comprehensive propagation mechanism that will be provided by new op blocker system for "permissive modes", the locking enabled by default is regardlessly useful and long overdue. So I think we should merge this for 2.9 and build user options on top later when the op blocker API settles.
Address comments from Max and Eric:
- Use F_OFD_GETLK instead of opening r/w for ro images. [Max]
- Lock both bytes exclusively for non-shared write. [Max]
- Spell fixes. [Eric]
- Fix test matrix. [Max]
- Comment tweaks. [Max]
- Return code cleanups. [Max]
- Don't abuse "disable_lock" for migration. [Max]
- Use bs->exact_filename instead of bs->filename. [Max]
- Force protect qcow2 concurrent write.
- Fix indentation. [Max]
- Always use re-open for lockfd instead of dup. [Max]
- Fall through to "abort" where "prepare" failed. [Max]
- Fix option handling in raw_reopen_handle_lock. [Max]
- Use "error_abort" in commit and abort. [Max]
- Fix cleanup of raw_reopen_handle_lock() failure. [Max]
- Add a patch for qcow2 to mask BDRV_O_SHARE_RW if r/w.
- Rebase and fix new more cases that will be broken by "lock by default".
Fam Zheng (16):
osdep: Add qemu_lock_fd and qemu_unlock_fd
block: Define BDRV_O_SHARE_RW
qemu-io: Set "share-rw" flag together with read-only
qemu-img: Set "share-rw" flag in read-only commands
block: Set "share-rw" flag in drive-backup when sync=none
iotests: 055: Don't attach the drive to vm for drive-backup
iotests: 030: Read-only open image for getting map
iotests: 087: Don't attach test image twice
iotests: 085: Avoid image locking conflict
iotests: 091: Quit QEMU before checking image
iotests: 172: Use separate images for multiple devices
tests: Use null-co:// instead of /dev/null as the dummy image
tests: Disable image lock in test-replication
file-posix: Implement image locking
qcow2: Force "no other writer" lock on bs->file
tests: Add test-image-lock
block/file-posix.c | 667 ++++++++++++++++++++++++++++++++++++++++++++- block/qcow2.c | 11 + blockdev.c | 3 + include/block/block.h | 2 + include/qemu/osdep.h | 3 + qemu-img.c | 10 +- qemu-io.c | 2 + tests/Makefile.include | 2 + tests/drive_del-test.c | 2 +- tests/nvme-test.c | 2 +- tests/qemu-iotests/030 | 4 +- tests/qemu-iotests/055 | 32 ++- tests/qemu-iotests/085 | 32 ++- tests/qemu-iotests/085.out | 3 +- tests/qemu-iotests/087 | 6 +- tests/qemu-iotests/091 | 2 + tests/qemu-iotests/172 | 53 ++-- tests/qemu-iotests/172.out | 50 ++-- tests/test-image-lock.c | 200 ++++++++++++++ tests/test-replication.c | 9 +- tests/usb-hcd-uhci-test.c | 2 +- tests/usb-hcd-xhci-test.c | 2 +- tests/virtio-blk-test.c | 2 +- tests/virtio-scsi-test.c | 4 +- util/osdep.c | 48 ++++ 25 files changed, 1056 insertions(+), 97 deletions(-) create mode 100644 tests/test-image-lock.c
Signature · 1 line
2.9.3
PATCH v10 1/16 osdep: Add qemu_lock_fd and qemu_unlock_fd
They are wrappers of POSIX fcntl "file private locking", with a convenient "try lock" wrapper implemented with F_OFD_GETLK.
--- include/qemu/osdep.h | 3 +++ util/osdep.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+)
include/qemu/osdep.h +3 -0
@@ -297,6 +297,9 @@ int qemu_close(int fd);#ifndef _WIN32int qemu_dup (int fd);#endif +int qemu_lock_fd (int fd,int64_t start,int64_t len,bool exclusive); +int qemu_unlock_fd (int fd,int64_t start,int64_t len); +int qemu_lock_fd_test (int fd,int64_t start,int64_t len,bool exclusive);#if defined(__HAIKU__) && defined(__i386__)#define FMT_pid "%ld"
util/osdep.c +48 -0
54 lines not on this page · raw message
Signature · 1 line
2.9.3
PATCH v10 2/16 block: Define BDRV_O_SHARE_RW
In reply to Fam Zheng--- include/block/block.h | 2 ++ 1 file changed, 2 insertions(+)
include/block/block.h +2 -0
@@ -97,6 +97,8 @@ typedef struct HDGeometry {select an appropriate protocol driver, ignoring the format layer */ #define BDRV_O_NO_IO 0x10000/* don't initialize for I/O */ +#define BDRV_O_SHARE_RW 0x20000/* accept shared read-write from other users +of the image */ #define BDRV_O_CACHE_MASK (BDRV_O_NOCACHE | BDRV_O_NO_FLUSH)
Signature · 1 line
2.9.3
PATCH v10 3/16 qemu-io: Set "share-rw" flag together with read-only
In reply to Fam Zhengqemu-io is a low level tool to read or modify guest visible data, which implies the user knows very well what is being done. Allowing reading from a locked image is harmless in most cases, so do it.
--- qemu-io.c | 2 ++ 1 file changed, 2 insertions(+)
qemu-io.c +2 -0
@@ -585,6 +585,8 @@ int main(int argc, char **argv)/* open the device */ if (!readonly) { flags |=BDRV_O_RDWR ; + }else { + flags |=BDRV_O_SHARE_RW ; }if ((argc - optind) ==1 ) {
Signature · 1 line
2.9.3
PATCH v10 4/16 qemu-img: Set "share-rw" flag in read-only commands
In reply to Fam ZhengChecking the status of an image when it is being used by guest is usually useful, and there is no risk of corrupting data, so don't let the upcoming image locking feature limit this use case.
--- qemu-img.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
qemu-img.c +8 -2
@@ -705,6 +705,10 @@ static int img_check(int argc, char **argv)return 1 ; } +if (!(flags &BDRV_O_RDWR )) { + flags |=BDRV_O_SHARE_RW ; + } + blk =img_open (image_opts, filename, fmt, flags, writethrough, quiet);if (!blk) {return 1 ;
@@ -1238,6 +1242,7 @@ static int img_compare(int argc, char **argv) goto out3; } + flags |=BDRV_O_SHARE_RW ; blk1 =img_open (image_opts, filename1, fmt1, flags, writethrough, quiet);if (!blk1) { ret =2 ;
@@ -2286,7 +2291,8 @@ static ImageInfoList *collect_image_info_list(bool image_opts,g_hash_table_insert (filenames, (gpointer )filename,NULL ); blk =img_open (image_opts, filename, fmt, -BDRV_O_NO_BACKING |BDRV_O_NO_IO , false, false); +BDRV_O_NO_BACKING |BDRV_O_NO_IO |BDRV_O_SHARE_RW , + false, false);if (!blk) { goto err; }
@@ -2612,7 +2618,7 @@ static int img_map(int argc, char **argv)return 1 ; } - blk =img_open (image_opts, filename, fmt,0 , false, false); + blk =img_open (image_opts, filename, fmt,BDRV_O_SHARE_RW , false, false);if (!blk) {return 1 ; }
Signature · 1 line
2.9.3
PATCH v10 5/16 block: Set "share-rw" flag in drive-backup when sync=none
In reply to Fam ZhengIn this case we may open the source's backing image chain multiple times. Setting share flag means the new open won't try to acquire or check any lock, once we implement image locking.
--- blockdev.c | 3 +++ 1 file changed, 3 insertions(+)
blockdev.c +3 -0
@@ -3177,6 +3177,9 @@ static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *txn, }if (backup->sync ==MIRROR_SYNC_MODE_NONE ) { source = bs; +/* FIXME: block layer should really open target with BDRV_O_NO_BACKING +* and reuse source's backing chain, if they share one. */ + flags |=BDRV_O_SHARE_RW ; } size =bdrv_getlength (bs);
Signature · 1 line
2.9.3
PATCH v10 6/16 iotests: 055: Don't attach the drive to vm for drive-backup
In reply to Fam Zheng--- tests/qemu-iotests/055 | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-)
tests/qemu-iotests/055 +18 -14
@@ -455,17 +455,18 @@ class TestDriveCompression(iotests.QMPTestCase): except OSError: pass - def do_prepare_drives(self, fmt, args): + def do_prepare_drives(self, fmt, args, attach): self.vm = iotests.VM().add_drive(test_img) qemu_img('create', '-f', fmt, blockdev_target_img, str(TestDriveCompression.image_len), *args) - self.vm.add_drive(blockdev_target_img, format=fmt) + if attach: + self.vm.add_drive(blockdev_target_img, format=fmt) self.vm.launch() - def do_test_compress_complete(self, cmd, format, **args): - self.do_prepare_drives(format['type'], format['args']) + def do_test_compress_complete(self, cmd, format, attach, **args): + self.do_prepare_drives(format['type'], format['args'], attach) self.assert_no_active_block_jobs()
@@ -481,15 +482,16 @@ class TestDriveCompression(iotests.QMPTestCase): def test_complete_compress_drive_backup(self): for format in TestDriveCompression.fmt_supports_compression: - self.do_test_compress_complete('drive-backup', format, + self.do_test_compress_complete('drive-backup', format, attach=False, target=blockdev_target_img, mode='existing') def test_complete_compress_blockdev_backup(self): for format in TestDriveCompression.fmt_supports_compression: - self.do_test_compress_complete('blockdev-backup', format, target='drive1') + self.do_test_compress_complete('blockdev-backup', format, + attach=True, target='drive1') - def do_test_compress_cancel(self, cmd, format, **args): - self.do_prepare_drives(format['type'], format['args']) + def do_test_compress_cancel(self, cmd, format, attach, **args): + self.do_prepare_drives(format['type'], format['args'], attach) self.assert_no_active_block_jobs()
@@ -503,15 +505,16 @@ class TestDriveCompression(iotests.QMPTestCase): def test_compress_cancel_drive_backup(self): for format in TestDriveCompression.fmt_supports_compression: - self.do_test_compress_cancel('drive-backup', format, + self.do_test_compress_cancel('drive-backup', format, attach=False, target=blockdev_target_img, mode='existing') def test_compress_cancel_blockdev_backup(self): for format in TestDriveCompression.fmt_supports_compression: - self.do_test_compress_cancel('blockdev-backup', format, target='drive1') + self.do_test_compress_cancel('blockdev-backup', format, attach=True, + target='drive1') - def do_test_compress_pause(self, cmd, format, **args): - self.do_prepare_drives(format['type'], format['args']) + def do_test_compress_pause(self, cmd, format, attach, **args): + self.do_prepare_drives(format['type'], format['args'], attach) self.assert_no_active_block_jobs()
@@ -543,12 +546,13 @@ class TestDriveCompression(iotests.QMPTestCase): def test_compress_pause_drive_backup(self): for format in TestDriveCompression.fmt_supports_compression: - self.do_test_compress_pause('drive-backup', format, + self.do_test_compress_pause('drive-backup', format, attach=False, target=blockdev_target_img, mode='existing') def test_compress_pause_blockdev_backup(self): for format in TestDriveCompression.fmt_supports_compression: - self.do_test_compress_pause('blockdev-backup', format, target='drive1') + self.do_test_compress_pause('blockdev-backup', format, attach=True, + target='drive1') if __name__ == '__main__': iotests.main(supported_fmts=['raw', 'qcow2'])
Signature · 1 line
2.9.3
PATCH v10 7/16 iotests: 030: Read-only open image for getting map
In reply to Fam Zheng--- tests/qemu-iotests/030 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tests/qemu-iotests/030 +2 -2
@@ -114,7 +114,7 @@ class TestSingleDrive(iotests.QMPTestCase): self.assert_no_active_block_jobs() # The image map is empty before the operation - empty_map = qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img) + empty_map = qemu_io('-f', iotests.imgfmt, '-c', 'map', '-r', test_img) # This is a no-op: no data should ever be copied from the base image result = self.vm.qmp('block-stream', device='drive0', base=mid_img)
@@ -125,7 +125,7 @@ class TestSingleDrive(iotests.QMPTestCase): self.assert_no_active_block_jobs() self.vm.shutdown() - self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img), + self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', '-r', test_img), empty_map, 'image file map changed after a no-op') def test_stream_partial(self):
Signature · 1 line
2.9.3
PATCH v10 8/16 iotests: 087: Don't attach test image twice
In reply to Fam ZhengThe test scenario doesn't require the same image, instead it focuses on the duplicated node-name, so use null-co to avoid locking conflict.
--- tests/qemu-iotests/087 | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
tests/qemu-iotests/087 +2 -4
@@ -82,8 +82,7 @@ run_qemu -drive driver=$IMGFMT,id=disk,node-name=test-node,file="$TEST_IMG" <<EO "driver": "$IMGFMT", "node-name": "disk", "file": { - "driver": "file", - "filename": "$TEST_IMG" + "driver": "null-co" } } }
@@ -92,8 +91,7 @@ run_qemu -drive driver=$IMGFMT,id=disk,node-name=test-node,file="$TEST_IMG" <<EO "driver": "$IMGFMT", "node-name": "test-node", "file": { - "driver": "file", - "filename": "$TEST_IMG" + "driver": "null-co" } } }
Signature · 1 line
2.9.3
PATCH v10 9/16 iotests: 085: Avoid image locking conflict
In reply to Fam ZhengIn the case where we test the expected error when a blockdev-snapshot target already has a backing image, backing chain is opened multiple times. This will be a problem when we use image locking, so use a different backing file that is not already open.
--- tests/qemu-iotests/085 | 32 +++++++++++++++++++------------- tests/qemu-iotests/085.out | 3 ++- 2 files changed, 21 insertions(+), 14 deletions(-)
tests/qemu-iotests/085 +19 -13
@@ -87,24 +87,26 @@ function create_group_snapshot() } # ${1}: unique identifier for the snapshot filename -# ${2}: true: open backing images; false: don't open them (default) +# ${2}: extra_params to the blockdev-add command +# ${3}: filename +function do_blockdev_add() +{ + cmd="{ 'execute': 'blockdev-add', 'arguments': + { 'driver': 'qcow2', 'node-name': 'snap_${1}', ${2} + 'file': + { 'driver': 'file', 'filename': '${3}', + 'node-name': 'file_${1}' } } }" + _send_qemu_cmd $h "${cmd}" "return" +} + +# ${1}: unique identifier for the snapshot filename function add_snapshot_image() { - if [ "${2}" = "true" ]; then - extra_params="" - else - extra_params="'backing': '', " - fi base_image="${TEST_DIR}/$((${1}-1))-${snapshot_virt0}" snapshot_file="${TEST_DIR}/${1}-${snapshot_virt0}" _make_test_img -b "${base_image}" "$size" mv "${TEST_IMG}" "${snapshot_file}" - cmd="{ 'execute': 'blockdev-add', 'arguments': - { 'driver': 'qcow2', 'node-name': 'snap_${1}', ${extra_params} - 'file': - { 'driver': 'file', 'filename': '${snapshot_file}', - 'node-name': 'file_${1}' } } }" - _send_qemu_cmd $h "${cmd}" "return" + do_blockdev_add "$1" "'backing': '', " "${snapshot_file}" } # ${1}: unique identifier for the snapshot filename
@@ -222,7 +224,11 @@ echo === Invalid command - snapshot node has a backing image === echo SNAPSHOTS=$((${SNAPSHOTS}+1)) -add_snapshot_image ${SNAPSHOTS} true + +_make_test_img "$size" +mv "${TEST_IMG}" "${TEST_IMG}.base" +_make_test_img -b "${TEST_IMG}.base" "$size" +do_blockdev_add ${SNAPSHOTS} "" "${TEST_IMG}" blockdev_snapshot ${SNAPSHOTS} error echo
tests/qemu-iotests/085.out +2 -1
@@ -78,7 +78,8 @@ Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 backing_file=TEST_DIR/ === Invalid command - snapshot node has a backing image === -Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 backing_file=TEST_DIR/12-snapshot-v0.IMGFMT +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 backing_file=TEST_DIR/t.IMGFMT.base {"return": {}} {"error": {"class": "GenericError", "desc": "The snapshot already has a backing image"}}
Signature · 1 line
2.9.3
PATCH v10 10/16 iotests: 091: Quit QEMU before checking image
In reply to Fam Zheng--- tests/qemu-iotests/091 | 2 ++ 1 file changed, 2 insertions(+)
tests/qemu-iotests/091 +2 -0
@@ -95,7 +95,9 @@ echo "vm2: qemu process running successfully" echo "vm2: flush io, and quit" _send_qemu_cmd $h2 'qemu-io disk flush' "(qemu)" _send_qemu_cmd $h2 'quit' "" +_send_qemu_cmd $h1 'quit' "" +wait echo "Check image pattern" ${QEMU_IO} -c "read -P 0x22 0 4M" "${TEST_IMG}" | _filter_testdir | _filter_qemu_io
Signature · 1 line
2.9.3
PATCH v10 11/16 iotests: 172: Use separate images for multiple devices
In reply to Fam ZhengTo avoid image lock failures.
--- tests/qemu-iotests/172 | 53 ++++++++++++++++++++++++---------------------- tests/qemu-iotests/172.out | 50 ++++++++++++++++++++++--------------------- 2 files changed, 54 insertions(+), 49 deletions(-)
tests/qemu-iotests/172 +28 -25
@@ -86,6 +86,9 @@ size=720k _make_test_img $size +TEST_IMG="$TEST_IMG.2" _make_test_img $size +TEST_IMG="$TEST_IMG.3" _make_test_img $size + # Default drive semantics: # # By default you get a single empty floppy drive. You can override it with
@@ -105,7 +108,7 @@ echo === Using -fda/-fdb options === check_floppy_qtree -fda "$TEST_IMG" check_floppy_qtree -fdb "$TEST_IMG" -check_floppy_qtree -fda "$TEST_IMG" -fdb "$TEST_IMG" +check_floppy_qtree -fda "$TEST_IMG" -fdb "$TEST_IMG.2" echo
@@ -114,7 +117,7 @@ echo === Using -drive options === check_floppy_qtree -drive if=floppy,file="$TEST_IMG" check_floppy_qtree -drive if=floppy,file="$TEST_IMG",index=1 -check_floppy_qtree -drive if=floppy,file="$TEST_IMG" -drive if=floppy,file="$TEST_IMG",index=1 +check_floppy_qtree -drive if=floppy,file="$TEST_IMG" -drive if=floppy,file="$TEST_IMG.2",index=1 echo echo
@@ -122,7 +125,7 @@ echo === Using -drive if=none and -global === check_floppy_qtree -drive if=none,file="$TEST_IMG" -global isa-fdc.driveA=none0 check_floppy_qtree -drive if=none,file="$TEST_IMG" -global isa-fdc.driveB=none0 -check_floppy_qtree -drive if=none,file="$TEST_IMG" -drive if=none,file="$TEST_IMG" \ +check_floppy_qtree -drive if=none,file="$TEST_IMG" -drive if=none,file="$TEST_IMG.2" \ -global isa-fdc.driveA=none0 -global isa-fdc.driveB=none1 echo
@@ -131,7 +134,7 @@ echo === Using -drive if=none and -device === check_floppy_qtree -drive if=none,file="$TEST_IMG" -device floppy,drive=none0 check_floppy_qtree -drive if=none,file="$TEST_IMG" -device floppy,drive=none0,unit=1 -check_floppy_qtree -drive if=none,file="$TEST_IMG" -drive if=none,file="$TEST_IMG" \ +check_floppy_qtree -drive if=none,file="$TEST_IMG",file.disable-lock=on -drive if=none,file="$TEST_IMG.2" \ -device floppy,drive=none0 -device floppy,drive=none1,unit=1 echo
@@ -139,58 +142,58 @@ echo echo === Mixing -fdX and -global === # Working -check_floppy_qtree -fda "$TEST_IMG" -drive if=none,file="$TEST_IMG" -global isa-fdc.driveB=none0 -check_floppy_qtree -fdb "$TEST_IMG" -drive if=none,file="$TEST_IMG" -global isa-fdc.driveA=none0 +check_floppy_qtree -fda "$TEST_IMG" -drive if=none,file="$TEST_IMG.2" -global isa-fdc.driveB=none0 +check_floppy_qtree -fdb "$TEST_IMG" -drive if=none,file="$TEST_IMG.2" -global isa-fdc.driveA=none0 # Conflicting (-fdX wins) -check_floppy_qtree -fda "$TEST_IMG" -drive if=none,file="$TEST_IMG" -global isa-fdc.driveA=none0 -check_floppy_qtree -fdb "$TEST_IMG" -drive if=none,file="$TEST_IMG" -global isa-fdc.driveB=none0 +check_floppy_qtree -fda "$TEST_IMG" -drive if=none,file="$TEST_IMG.2" -global isa-fdc.driveA=none0 +check_floppy_qtree -fdb "$TEST_IMG" -drive if=none,file="$TEST_IMG.2" -global isa-fdc.driveB=none0 echo echo echo === Mixing -fdX and -device === # Working -check_floppy_qtree -fda "$TEST_IMG" -drive if=none,file="$TEST_IMG" -device floppy,drive=none0 -check_floppy_qtree -fda "$TEST_IMG" -drive if=none,file="$TEST_IMG" -device floppy,drive=none0,unit=1 +check_floppy_qtree -fda "$TEST_IMG" -drive if=none,file="$TEST_IMG.2" -device floppy,drive=none0 +check_floppy_qtree -fda "$TEST_IMG" -drive if=none,file="$TEST_IMG.2" -device floppy,drive=none0,unit=1 -check_floppy_qtree -fdb "$TEST_IMG" -drive if=none,file="$TEST_IMG" -device floppy,drive=none0 -check_floppy_qtree -fdb "$TEST_IMG" -drive if=none,file="$TEST_IMG" -device floppy,drive=none0,unit=0 +check_floppy_qtree -fdb "$TEST_IMG" -drive if=none,file="$TEST_IMG.2" -device floppy,drive=none0 +check_floppy_qtree -fdb "$TEST_IMG" -drive if=none,file="$TEST_IMG.2" -device floppy,drive=none0,unit=0 # Conflicting -check_floppy_qtree -fda "$TEST_IMG" -drive if=none,file="$TEST_IMG" -device floppy,drive=none0,unit=0 -check_floppy_qtree -fdb "$TEST_IMG" -drive if=none,file="$TEST_IMG" -device floppy,drive=none0,unit=1 +check_floppy_qtree -fda "$TEST_IMG" -drive if=none,file="$TEST_IMG.2" -device floppy,drive=none0,unit=0 +check_floppy_qtree -fdb "$TEST_IMG" -drive if=none,file="$TEST_IMG.2" -device floppy,drive=none0,unit=1 echo echo echo === Mixing -drive and -device === # Working -check_floppy_qtree -drive if=floppy,file="$TEST_IMG" -drive if=none,file="$TEST_IMG" -device floppy,drive=none0 -check_floppy_qtree -drive if=floppy,file="$TEST_IMG" -drive if=none,file="$TEST_IMG" -device floppy,drive=none0,unit=1 +check_floppy_qtree -drive if=floppy,file="$TEST_IMG" -drive if=none,file="$TEST_IMG.2" -device floppy,drive=none0 +check_floppy_qtree -drive if=floppy,file="$TEST_IMG" -drive if=none,file="$TEST_IMG.2" -device floppy,drive=none0,unit=1 # Conflicting -check_floppy_qtree -drive if=floppy,file="$TEST_IMG" -drive if=none,file="$TEST_IMG" -device floppy,drive=none0,unit=0 +check_floppy_qtree -drive if=floppy,file="$TEST_IMG" -drive if=none,file="$TEST_IMG.2" -device floppy,drive=none0,unit=0 echo echo echo === Mixing -global and -device === # Working -check_floppy_qtree -drive if=none,file="$TEST_IMG" -drive if=none,file="$TEST_IMG" \ +check_floppy_qtree -drive if=none,file="$TEST_IMG" -drive if=none,file="$TEST_IMG.2" \ -global isa-fdc.driveA=none0 -device floppy,drive=none1 -check_floppy_qtree -drive if=none,file="$TEST_IMG" -drive if=none,file="$TEST_IMG" \ +check_floppy_qtree -drive if=none,file="$TEST_IMG" -drive if=none,file="$TEST_IMG.2" \ -global isa-fdc.driveA=none0 -device floppy,drive=none1,unit=1 -check_floppy_qtree -drive if=none,file="$TEST_IMG" -drive if=none,file="$TEST_IMG" \ +check_floppy_qtree -drive if=none,file="$TEST_IMG" -drive if=none,file="$TEST_IMG.2" \ -global isa-fdc.driveB=none0 -device floppy,drive=none1 -check_floppy_qtree -drive if=none,file="$TEST_IMG" -drive if=none,file="$TEST_IMG" \ +check_floppy_qtree -drive if=none,file="$TEST_IMG" -drive if=none,file="$TEST_IMG.2" \ -global isa-fdc.driveB=none0 -device floppy,drive=none1,unit=0 # Conflicting -check_floppy_qtree -drive if=none,file="$TEST_IMG" -drive if=none,file="$TEST_IMG" \ +check_floppy_qtree -drive if=none,file="$TEST_IMG" -drive if=none,file="$TEST_IMG.2" \ -global isa-fdc.driveA=none0 -device floppy,drive=none1,unit=0 -check_floppy_qtree -drive if=none,file="$TEST_IMG" -drive if=none,file="$TEST_IMG" \ +check_floppy_qtree -drive if=none,file="$TEST_IMG" -drive if=none,file="$TEST_IMG.2" \ -global isa-fdc.driveB=none0 -device floppy,drive=none1,unit=1 echo
@@ -199,8 +202,8 @@ echo === Too many floppy drives === # Working check_floppy_qtree -drive if=floppy,file="$TEST_IMG" \ - -drive if=none,file="$TEST_IMG" \ - -drive if=none,file="$TEST_IMG" \ + -drive if=none,file="$TEST_IMG.2" \ + -drive if=none,file="$TEST_IMG.3" \ -global isa-fdc.driveB=none0 \ -device floppy,drive=none1
tests/qemu-iotests/172.out +26 -24
193 lines not on this page · raw message
Signature · 1 line
2.9.3
PATCH v10 12/16 tests: Use null-co:// instead of /dev/null as the dummy image
In reply to Fam Zheng--- tests/drive_del-test.c | 2 +- tests/nvme-test.c | 2 +- tests/usb-hcd-uhci-test.c | 2 +- tests/usb-hcd-xhci-test.c | 2 +- tests/virtio-blk-test.c | 2 +- tests/virtio-scsi-test.c | 4 ++-- 6 files changed, 7 insertions(+), 7 deletions(-)
tests/drive_del-test.c +1 -1
@@ -92,7 +92,7 @@ static void test_after_failed_device_add(void)static void test_drive_del_device_del (void ) {/* Start with a drive used by a device that unplugs instantaneously */ -qtest_start ("-drive if=none,id=drive0,file=/dev/null,format=raw" +qtest_start ("-drive if=none,id=drive0,file=null-co://,format=raw" " -device virtio-scsi-pci" " -device scsi-hd,drive=drive0,id=dev0" );
tests/nvme-test.c +1 -1
@@ -22,7 +22,7 @@ int main(int argc, char **argv)g_test_init (&argc, &argv,NULL );qtest_add_func ("/nvme/nop" , nop); -qtest_start ("-drive id=drv0,if=none,file=/dev/null,format=raw " +qtest_start ("-drive id=drv0,if=none,file=null-co://,format=raw " "-device nvme,drive=drv0,serial=foo" ); ret =g_test_run ();
tests/usb-hcd-uhci-test.c +1 -1
@@ -78,7 +78,7 @@ int main(int argc, char **argv) {const char *arch =qtest_get_arch ();const char *cmd ="-device piix3-usb-uhci,id=uhci,addr=1d.0" -" -drive id=drive0,if=none,file=/dev/null,format=raw" +" -drive id=drive0,if=none,file=null-co://,format=raw" " -device usb-tablet,bus=uhci.0,port=1" ;int ret;
tests/usb-hcd-xhci-test.c +1 -1
@@ -89,7 +89,7 @@ int main(int argc, char **argv)qtest_add_func ("/xhci/pci/hotplug/usb-uas" , test_usb_uas_hotplug);qtest_start ("-device nec-usb-xhci,id=xhci" -" -drive id=drive0,if=none,file=/dev/null,format=raw" ); +" -drive id=drive0,if=none,file=null-co://,format=raw" ); ret =g_test_run ();qtest_end ();
tests/virtio-blk-test.c +1 -1
@@ -63,7 +63,7 @@ static QOSState *pci_test_start(void)const char *arch =qtest_get_arch ();char *tmp_path;const char *cmd ="-drive if=none,id=drive0,file=%s,format=raw " -"-drive if=none,id=drive1,file=/dev/null,format=raw " +"-drive if=none,id=drive1,file=null-co://,format=raw " "-device virtio-blk-pci,id=drv0,drive=drive0," "addr=%x.%x" ;
tests/virtio-scsi-test.c +2 -2
@@ -35,7 +35,7 @@ typedef struct {static QOSState *qvirtio_scsi_start (const char *extra_opts) {const char *arch =qtest_get_arch (); -const char *cmd ="-drive id=drv0,if=none,file=/dev/null,format=raw " +const char *cmd ="-drive id=drv0,if=none,file=null-co://,format=raw " "-device virtio-scsi-pci,id=vs0 " "-device scsi-hd,bus=vs0.0,drive=drv0 %s" ;
@@ -195,7 +195,7 @@ static void hotplug(void)QDict *response;QOSState *qs; - qs =qvirtio_scsi_start ("-drive id=drv1,if=none,file=/dev/null,format=raw" ); + qs =qvirtio_scsi_start ("-drive id=drv1,if=none,file=null-co://,format=raw" ); response =qmp ("{\"execute\": \"device_add\"," " \"arguments\": {" " \"driver\": \"scsi-hd\","
Signature · 1 line
2.9.3
PATCH v10 13/16 tests: Disable image lock in test-replication
In reply to Fam ZhengThe COLO block replication architecture requires one disk to be shared between primary and secondary, in the test both processes use posix file protocol (instead of over NBD) so it is affected by image locking. Disable the lock.
--- tests/test-replication.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
tests/test-replication.c +7 -2
@@ -179,7 +179,8 @@ static BlockBackend *start_primary(void)char *cmdline; cmdline =g_strdup_printf ("driver=replication,mode=primary,node-name=xxx," -"file.driver=qcow2,file.file.filename=%s" +"file.driver=qcow2,file.file.filename=%s," +"file.file.disable-lock=on" , p_local_disk); opts =qemu_opts_parse_noisily (&qemu_drive_opts, cmdline, false);g_free (cmdline);
@@ -310,7 +311,9 @@ static BlockBackend *start_secondary(void)Error *local_err =NULL ;/* add s_local_disk and forge S_LOCAL_DISK_ID */ - cmdline =g_strdup_printf ("file.filename=%s,driver=qcow2" , s_local_disk); + cmdline =g_strdup_printf ("file.filename=%s,driver=qcow2," +"file.file.disable-lock=on" , + s_local_disk); opts =qemu_opts_parse_noisily (&qemu_drive_opts, cmdline, false);g_free (cmdline);
@@ -331,8 +334,10 @@ static BlockBackend *start_secondary(void)/* add S_(ACTIVE/HIDDEN)_DISK and forge S_ID */ cmdline =g_strdup_printf ("driver=replication,mode=secondary,top-id=%s," "file.driver=qcow2,file.file.filename=%s," +"file.file.disable-lock=on" ,"file.backing.driver=qcow2," "file.backing.file.filename=%s," +"file.backing.file.disable-lock=on" ,"file.backing.backing=%s" ,S_ID , s_active_disk, s_hidden_disk ,S_LOCAL_DISK_ID );
Signature · 1 line
2.9.3
PATCH v10 14/16 file-posix: Implement image locking
In reply to Fam ZhengThis implements open flag sensible image locking for local file and host device protocol.
virtlockd in libvirt locks the first byte, so we start looking at the file bytes from 1.
Quoting what was proposed by Kevin Wolf <[email protected]>, there are four locking modes by combining two bits (BDRV_O_RDWR and BDRV_O_SHARE_RW), and implemented by taking two locks:
Lock bytes:
* byte 1: I can't allow other processes to write to the image
* byte 2: I am writing to the image
Lock modes:
* shared writer (BDRV_O_RDWR | BDRV_O_SHARE_RW): Take shared lock on byte 2. Test whether byte 1 is locked using an exclusive lock, and fail if so.
* exclusive writer (BDRV_O_RDWR only): Take shared lock on byte 2. Test whether byte 1 is locked using an exclusive lock, and fail if so. Then take shared lock on byte 1. I suppose this is racy, but we can probably tolerate that.
* reader that can tolerate writers (BDRV_O_SHARE_RW only): Don't do anything
* reader that can't tolerate writers (neither bit is set): Take shared lock on byte 1. Test whether byte 2 is locked, and fail if so.
The complication is in the transactional reopen. To make the reopen logic managable, and allow better reuse, the code is internally organized with a table from old mode to the new one.
--- block/file-posix.c | 667 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 664 insertions(+), 3 deletions(-)
block/file-posix.c +664 -3
@@ -131,8 +131,39 @@ do { \#define MAX_BLOCKSIZE 4096 +/* Posix file locking bytes. Libvirt takes byte 0, so start from byte 1. */ +#define RAW_LOCK_BYTE_MIN 1 +#define RAW_LOCK_BYTE_NO_OTHER_WRITER 1 +#define RAW_LOCK_BYTE_WRITE 2 + +/* +** reader that can tolerate writers: Don't do anything +* +** reader that can't tolerate writers: Take shared lock on byte 1. Test +* byte 2 is unlocked. +* +** shared writer: Take shared lock on byte 2. Test byte 1 is unlocked. +* +** exclusive writer: Take exclusive locks on both bytes. +*/ + +typedef enum { +/* Read only and accept other writers. */ +RAW_L_READ_SHARE_RW , +/* Read only and try to forbid other writers. */ +RAW_L_READ , +/* Read write and accept other writers. */ +RAW_L_WRITE_SHARE_RW , +/* Read write and try to forbid other writers. */ +RAW_L_WRITE , +}BDRVRawLockMode ; + typedef struct BDRVRawState { int fd; + /* A dup of @fd to make manipulating lock easier, especially during reopen, + * where this will accept BDRVRawReopenState.lock_fd. */ + int lock_fd; + bool disable_lock; int type; int open_flags; size_t buf_align;
@@ -146,11 +177,15 @@ typedef struct BDRVRawState { bool use_linux_aio:1;bool has_fallocate;bool needs_alignment; +BDRVRawLockMode cur_lock_mode; } BDRVRawState;typedef struct BDRVRawReopenState {int fd; +/* A dup of @fd used for acquiring lock. */ +int lock_fd;int open_flags; +bool disable_lock; }BDRVRawReopenState ;static int fd_open (BlockDriverState *bs);
@@ -368,6 +403,58 @@ static void raw_parse_flags(int bdrv_flags, int *open_flags) } } +static int raw_lock_fd (int fd,BDRVRawLockMode mode,Error **errp) +{ +int ret; +assert (fd >=0 ); +switch (mode) { +case RAW_L_READ_SHARE_RW : + ret =qemu_unlock_fd (fd,RAW_LOCK_BYTE_MIN ,2 ); +if (ret) { +error_setg_errno (errp, -ret,"Failed to unlock fd" ); + goto fail; + } +break ; +case RAW_L_READ : + ret =qemu_lock_fd (fd,RAW_LOCK_BYTE_NO_OTHER_WRITER ,1 , false); +if (ret) { +error_setg_errno (errp, -ret,"Failed to lock share byte" ); + goto fail; + } + ret =qemu_lock_fd_test (fd,RAW_LOCK_BYTE_WRITE ,1 , true); +if (ret) { +error_setg_errno (errp, -ret,"Write byte lock is taken" ); + goto fail; + } +break ; +case RAW_L_WRITE_SHARE_RW : + ret =qemu_lock_fd (fd,RAW_LOCK_BYTE_WRITE ,1 , false); +if (ret) { +error_setg_errno (errp, -ret,"Failed to lock write byte" ); + goto fail; + } + ret =qemu_lock_fd_test (fd,RAW_LOCK_BYTE_NO_OTHER_WRITER ,1 , true); +if (ret) { +error_setg_errno (errp, -ret,"Share byte lock is taken" ); + goto fail; + } +break ; +case RAW_L_WRITE : + ret =qemu_lock_fd (fd,RAW_LOCK_BYTE_MIN ,2 , true); +if (ret) { +error_setg_errno (errp, -ret,"Failed to lock image" ); + goto fail; + } +break ; +default : +abort (); + } +return 0 ; +fail: +qemu_unlock_fd (fd,RAW_LOCK_BYTE_MIN ,2 ); +return ret; +} +static void raw_parse_filename (const char *filename,QDict *options,Error **errp) {
@@ -393,10 +480,88 @@ static QemuOptsList raw_runtime_opts = { .type = QEMU_OPT_STRING, .help = "host AIO implementation (threads, native)", }, + { + .name = "disable-lock", + .type = QEMU_OPT_BOOL, + .help = "don't lock the file", + }, {/* end of list */ } }, }; +static BDRVRawLockMode raw_get_lock_mode (int flags) +{ +switch (flags & (BDRV_O_RDWR |BDRV_O_SHARE_RW )) { +case BDRV_O_RDWR : +return RAW_L_WRITE ; +case BDRV_O_RDWR |BDRV_O_SHARE_RW : +return RAW_L_WRITE_SHARE_RW ; +case BDRV_O_SHARE_RW : +return RAW_L_READ_SHARE_RW ; +case 0 : +return RAW_L_READ ; +default : +abort (); + } +} + +static int raw_open_lockfd (const char *filename,int flags, +BDRVRawLockMode *lock_mode,Error **errp) +{ +int ret =-1 ; +const char *normalized_filename = filename; + + ret =raw_normalize_devicepath (&normalized_filename); +if (ret <0 ) { +error_setg_errno (errp, -ret,"Could not normalize device path" ); + }else { +assert (!(flags &O_CREAT )); + ret =qemu_open (normalized_filename, flags); +if (ret ==-1 ) { +error_setg_errno (errp, errno,"Could not open file: %s" , + normalized_filename); + ret = -errno; + } + } +return ret; +} + +static bool raw_lock_enabled (BlockDriverState *bs) +{ +BDRVRawState *s = bs->opaque; + +return !(s->disable_lock ||bdrv_get_flags (bs) &BDRV_O_INACTIVE ); +} + +static int raw_apply_image_lock (BlockDriverState *bs,int bdrv_flags, +Error **errp) +{ +int ret; +BDRVRawState *s = bs->opaque; +BDRVRawLockMode lock_mode; + +if (!raw_lock_enabled (bs)) { +return 0 ; + } +assert (s->cur_lock_mode ==RAW_L_READ_SHARE_RW ); + lock_mode =raw_get_lock_mode (bdrv_flags); + ret =raw_open_lockfd (bs->exact_filename, s->open_flags, &lock_mode, + errp); +if (ret <0 ) { +return ret; + } + s->lock_fd = ret; +if (lock_mode ==RAW_L_READ_SHARE_RW ) { +return 0 ; + } + ret =raw_lock_fd (s->lock_fd, lock_mode, errp); +if (ret) { +return ret; + } + s->cur_lock_mode = lock_mode; +return 0 ; +} +static int raw_open_common (BlockDriverState *bs,QDict *options,int bdrv_flags,int open_flags,Error **errp) {
@@ -440,6 +605,7 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,raw_parse_flags (bdrv_flags, &s->open_flags); s->fd =-1 ; + s->lock_fd =-1 ; fd =qemu_open (filename, s->open_flags,0644 );if (fd <0 ) { ret = -errno;
@@ -451,6 +617,13 @@ static int raw_open_common(BlockDriverState *bs, QDict *options, } s->fd = fd; + s->disable_lock =qemu_opt_get_bool (opts,"disable-lock" , false); + + ret =raw_apply_image_lock (bs, bdrv_flags, errp); +if (ret) { + goto fail; + } +#ifdef CONFIG_LINUX_AIO /* Currently Linux does AIO only for files opened with O_DIRECT */ if (s->use_linux_aio && !(s->open_flags &O_DIRECT )) {
@@ -538,6 +711,462 @@ static int raw_open(BlockDriverState *bs, QDict *options, int flags,return raw_open_common (bs, options, flags,0 , errp); } +typedef enum { +RAW_LT_PREPARE , +RAW_LT_COMMIT , +RAW_LT_ABORT +}RawLockTransOp ; + +typedef int (*RawReopenFunc )(RawLockTransOp op, +int old_lock_fd,int new_lock_fd, +BDRVRawLockMode old_lock, +BDRVRawLockMode new_lock, +Error **errp); + +static int raw_lt_nop (RawLockTransOp op, +int old_lock_fd,int new_lock_fd, +BDRVRawLockMode old_lock, +BDRVRawLockMode new_lock, +Error **errp) +{ +assert (old_lock == new_lock || new_lock ==RAW_L_READ_SHARE_RW ); +return 0 ; +} + +static int raw_lt_from_unlock (RawLockTransOp op, +int old_lock_fd,int new_lock_fd, +BDRVRawLockMode old_lock, +BDRVRawLockMode new_lock, +Error **errp) +{ +assert (old_lock != new_lock); +assert (old_lock ==RAW_L_READ_SHARE_RW ); +switch (op) { +case RAW_LT_PREPARE : +return raw_lock_fd (new_lock_fd, new_lock, errp); +case RAW_LT_COMMIT : +break ; +case RAW_LT_ABORT : +break ; + } + +return 0 ; +} + +static int raw_lt_read_to_write_share (RawLockTransOp op, +int old_lock_fd,int new_lock_fd, +BDRVRawLockMode old_lock, +BDRVRawLockMode new_lock, +Error **errp) +{ +int ret =0 ; + +assert (old_lock ==RAW_L_READ ); +assert (new_lock ==RAW_L_WRITE_SHARE_RW ); + +/* +* lock byte "no other writer" lock byte "write" +* old S 0 +* new 0 S +* +* (0 = unlocked; S = shared; X = exclusive.) +*/ +switch (op) { +case RAW_LT_PREPARE : + ret =qemu_lock_fd (new_lock_fd,RAW_LOCK_BYTE_WRITE ,1 , true); +if (ret) { +error_setg_errno (errp, -ret,"Failed to lock new fd (write byte)" ); +break ; + } + ret =qemu_lock_fd (new_lock_fd,RAW_LOCK_BYTE_NO_OTHER_WRITER ,1 , false); +if (ret) { +error_setg_errno (errp, -ret,"Failed to lock new fd (share byte)" ); +break ; + } + ret =qemu_unlock_fd (old_lock_fd,RAW_LOCK_BYTE_NO_OTHER_WRITER ,1 ); +if (ret) { +error_setg_errno (errp, -ret,"Failed to unlock old fd (share byte)" ); +break ; + } + ret =qemu_lock_fd (new_lock_fd,RAW_LOCK_BYTE_NO_OTHER_WRITER ,1 , true); +if (ret) { +error_setg_errno (errp, -ret,"Failed to upgrade new fd (share byte)" ); +break ; + } + ret =qemu_unlock_fd (new_lock_fd,RAW_LOCK_BYTE_NO_OTHER_WRITER ,1 ); +if (ret) { +/* This is very unlikely, but catch it anyway. */ +error_setg_errno (errp, -ret,"Failed to unlock new fd (share byte)" ); +break ; + } + ret =qemu_lock_fd (new_lock_fd,RAW_LOCK_BYTE_WRITE ,1 , false); +if (ret) { +error_setg_errno (errp, -ret,"Failed to downgrade new fd (write byte)" ); +break ; + } +break ; +case RAW_LT_COMMIT : +break ; +case RAW_LT_ABORT : + ret =qemu_lock_fd (old_lock_fd,RAW_LOCK_BYTE_NO_OTHER_WRITER ,1 , false); +if (ret) { +error_report ("Failed to restore lock on old fd (share byte)" ); + } +break ; + } +return ret; +} + +static int raw_lt_read_to_write (RawLockTransOp op, +int old_lock_fd,int new_lock_fd, +BDRVRawLockMode old_lock, +BDRVRawLockMode new_lock, +Error **errp) +{ +int ret =0 ; + +assert (old_lock ==RAW_L_READ ); +assert (new_lock ==RAW_L_WRITE ); +/* +* lock byte "no other writer" lock byte "write" +* old S 0 +* new X X +* +* (0 = unlocked; S = shared; X = exclusive.) +*/ +switch (op) { +case RAW_LT_PREPARE : + ret =qemu_lock_fd (new_lock_fd,RAW_LOCK_BYTE_WRITE ,1 , true); +if (ret) { +error_setg_errno (errp, -ret,"Failed to lock new fd (write byte)" ); +break ; + } + ret =qemu_lock_fd (new_lock_fd,RAW_LOCK_BYTE_NO_OTHER_WRITER ,1 , false); +if (ret) { +error_setg_errno (errp, -ret,"Failed to lock new fd (share byte)" ); +break ; + } + ret =qemu_unlock_fd (old_lock_fd,RAW_LOCK_BYTE_NO_OTHER_WRITER ,1 ); +if (ret) { +error_setg_errno (errp, -ret,"Failed to unlock old fd (share byte)" ); +break ; + } + ret =qemu_lock_fd (new_lock_fd,RAW_LOCK_BYTE_NO_OTHER_WRITER ,1 , true); +if (ret) { +error_setg_errno (errp, -ret,"Failed to upgrade new fd (share byte)" ); +break ; + } +break ; +case RAW_LT_COMMIT : +break ; +case RAW_LT_ABORT : + ret =qemu_lock_fd (new_lock_fd,RAW_LOCK_BYTE_NO_OTHER_WRITER ,1 , false); +if (ret) { +error_setg_errno (errp, -ret,"Failed to downgrade new fd (share byte)" ); +break ; + } + ret =qemu_lock_fd (old_lock_fd,RAW_LOCK_BYTE_NO_OTHER_WRITER ,1 , false); +if (ret) { +error_report ("Failed to restore lock on old fd (share byte)" ); + } +break ; + } +return ret; +} + +static int raw_lt_write_share_to_read (RawLockTransOp op, +int old_lock_fd,int new_lock_fd, +BDRVRawLockMode old_lock, +BDRVRawLockMode new_lock, +Error **errp) +{ +int ret =0 ; + +assert (old_lock ==RAW_L_WRITE_SHARE_RW ); +assert (new_lock ==RAW_L_READ ); +/* +* lock byte "no other writer" lock byte "write" +* old 0 S +* new S 0 +* +* (0 = unlocked; S = shared; X = exclusive.) +*/ +switch (op) { +case RAW_LT_PREPARE : +/* Make sure there are no other writers. */ + ret =qemu_lock_fd (old_lock_fd,RAW_LOCK_BYTE_WRITE ,1 , true); +if (ret) { +error_setg_errno (errp, -ret,"Failed to lock old fd (write byte)" ); +break ; + } + ret =qemu_lock_fd (new_lock_fd,RAW_LOCK_BYTE_NO_OTHER_WRITER ,1 , false); +if (ret) { +error_setg_errno (errp, -ret,"Failed to lock new fd (share byte)" ); +break ; + } +break ; +case RAW_LT_COMMIT : +break ; +case RAW_LT_ABORT : +break ; + } +return ret; +} + +static int raw_lt_write_share_to_write (RawLockTransOp op, +int old_lock_fd,int new_lock_fd, +BDRVRawLockMode old_lock, +BDRVRawLockMode new_lock, +Error **errp) +{ +int ret =0 ; + +assert (old_lock ==RAW_L_WRITE_SHARE_RW ); +assert (new_lock ==RAW_L_WRITE ); +/* +* lock byte "no other writer" lock byte "write" +* old 0 S +* new X X +* +* (0 = unlocked; S = shared; X = exclusive.) +*/ +switch (op) { +case RAW_LT_PREPARE : +/* Make sure there are no other writers. */ + ret =qemu_lock_fd (new_lock_fd,RAW_LOCK_BYTE_NO_OTHER_WRITER ,1 , true); +if (ret) { +error_setg_errno (errp, -ret,"Failed to lock new fd (share byte)" ); +break ; + } + ret =qemu_lock_fd (new_lock_fd,RAW_LOCK_BYTE_WRITE ,1 , false); +if (ret) { +error_setg_errno (errp, -ret,"Failed to lock new fd (write byte)" ); +break ; + } + ret =qemu_unlock_fd (old_lock_fd,RAW_LOCK_BYTE_WRITE ,1 ); +if (ret) { +error_setg_errno (errp, -ret,"Failed to unlock old fd (write byte)" ); +break ; + } + ret =qemu_lock_fd (new_lock_fd,RAW_LOCK_BYTE_WRITE ,1 , true); +if (ret) { +error_setg_errno (errp, -ret,"Failed to upgrade new fd (write byte)" ); +break ; + } +break ; +case RAW_LT_COMMIT : +break ; +case RAW_LT_ABORT : + ret =qemu_lock_fd (new_lock_fd,RAW_LOCK_BYTE_WRITE ,1 , false); +if (ret) { +error_setg_errno (errp, -ret,"Failed to downgrade new fd (write byte)" ); +break ; + } + ret =qemu_lock_fd (old_lock_fd,RAW_LOCK_BYTE_WRITE ,1 , false); +if (ret) { +error_setg_errno (errp, -ret,"Failed to restore old fd (write byte)" ); +break ; + } +break ; + } +return ret; +} + +static int raw_lt_write_to_read (RawLockTransOp op, +int old_lock_fd,int new_lock_fd, +BDRVRawLockMode old_lock, +BDRVRawLockMode new_lock, +Error **errp) +{ +int ret =0 ; + +assert (old_lock ==RAW_L_WRITE ); +assert (new_lock ==RAW_L_READ ); +/* +* lock byte "no other writer" lock byte "write" +* old X X +* new S 0 +* +* (0 = unlocked; S = shared; X = exclusive.) +*/ +switch (op) { +case RAW_LT_PREPARE : + ret =qemu_lock_fd (old_lock_fd,RAW_LOCK_BYTE_NO_OTHER_WRITER ,1 , false); +if (ret) { +error_setg_errno (errp, -ret,"Failed to downgrade old fd (share byte)" ); +break ; + } + ret =qemu_lock_fd (new_lock_fd,RAW_LOCK_BYTE_NO_OTHER_WRITER ,1 , false); +if (ret) { +error_setg_errno (errp, -ret,"Failed to lock new fd (share byte)" ); +break ; + } +break ; +case RAW_LT_COMMIT : +break ; +case RAW_LT_ABORT : + ret =qemu_lock_fd (old_lock_fd,RAW_LOCK_BYTE_NO_OTHER_WRITER ,1 , true); +if (ret) { +error_setg_errno (errp, -ret,"Failed to restore old fd (share byte)" ); + } +break ; + } +return ret; +} + +static int raw_lt_write_to_write_share (RawLockTransOp op, +int old_lock_fd,int new_lock_fd, +BDRVRawLockMode old_lock, +BDRVRawLockMode new_lock, +Error **errp) +{ +int ret =0 ; + +assert (old_lock ==RAW_L_WRITE ); +assert (new_lock ==RAW_L_WRITE_SHARE_RW ); +/* +* lock byte "no other writer" lock byte "write" +* old X X +* new 0 S +* +* (0 = unlocked; S = shared; X = exclusive.) +*/ +switch (op) { +case RAW_LT_PREPARE : +break ; +case RAW_LT_COMMIT : + ret =qemu_lock_fd (old_lock_fd,RAW_LOCK_BYTE_WRITE ,1 , false); +if (ret) { +error_report ("Failed to downgrade old fd (share byte)" ); +break ; + } + ret =qemu_lock_fd (new_lock_fd,RAW_LOCK_BYTE_WRITE ,1 , false); +if (ret) { +error_report ("Failed to unlock new fd (share byte)" ); +break ; + } +break ; +case RAW_LT_ABORT : +break ; + } +return ret; +} + +/** +* Transactionally moving between possible locking states is tricky and must be +* done carefully. That is mostly because downgrading an exclusive lock to +* shared or unlocked is not guaranteed to be revertible. As a result, in such +* cases we have to defer the downgrading to "commit", given that no revert will +* happen after that point, and that downgrading a lock should never fail. +* +* On the other hand, upgrading a lock (e.g. from unlocked or shared to +* exclusive lock) must happen in "prepare" because it may fail. +* +* Manage the operation matrix with this state transition table to make +* fulfilling above conditions easier. +*/ +static const struct RawReopenFuncRecord { +BDRVRawLockMode old_lock; +BDRVRawLockMode new_lock; +RawReopenFunc func; +bool need_lock_fd; +bool close_old_lock_fd; +} reopen_functions[] = { + + {RAW_L_READ_SHARE_RW ,RAW_L_READ_SHARE_RW , raw_lt_nop, false, false}, + {RAW_L_READ_SHARE_RW ,RAW_L_READ , raw_lt_from_unlock, true}, + {RAW_L_READ_SHARE_RW ,RAW_L_WRITE_SHARE_RW , raw_lt_from_unlock, true}, + {RAW_L_READ_SHARE_RW ,RAW_L_WRITE , raw_lt_from_unlock, true}, + + {RAW_L_READ ,RAW_L_READ_SHARE_RW , raw_lt_nop, false, true}, + {RAW_L_READ ,RAW_L_READ , raw_lt_nop, false, false}, + {RAW_L_READ ,RAW_L_WRITE_SHARE_RW , raw_lt_read_to_write_share, true}, + {RAW_L_READ ,RAW_L_WRITE , raw_lt_read_to_write, true}, + + {RAW_L_WRITE_SHARE_RW ,RAW_L_READ_SHARE_RW , raw_lt_nop, false, true}, + {RAW_L_WRITE_SHARE_RW ,RAW_L_READ , raw_lt_write_share_to_read, true}, + {RAW_L_WRITE_SHARE_RW ,RAW_L_WRITE_SHARE_RW , raw_lt_nop, false, false}, + {RAW_L_WRITE_SHARE_RW ,RAW_L_WRITE , raw_lt_write_share_to_write, true}, + + {RAW_L_WRITE ,RAW_L_READ_SHARE_RW , raw_lt_nop, false, true}, + {RAW_L_WRITE ,RAW_L_READ , raw_lt_write_to_read, true}, + {RAW_L_WRITE ,RAW_L_WRITE_SHARE_RW , raw_lt_write_to_write_share, true}, + {RAW_L_WRITE ,RAW_L_WRITE , raw_lt_nop, false, false}, +}; + +static int raw_reopen_handle_lock (BDRVReopenState *state, +RawLockTransOp op, +Error **errp) +{ +BDRVRawReopenState *rs = state->opaque; +BDRVRawState *s = state->bs->opaque; +BDRVRawLockMode old_lock, new_lock; +const struct RawReopenFuncRecord *rec; +int ret; + + old_lock = s->cur_lock_mode; + rs->disable_lock =qdict_get_try_bool (state->options,"disable-lock" , + false); +qdict_del (state->options,"disable-lock" ); + +if (rs->disable_lock) { + new_lock =RAW_L_READ_SHARE_RW ; + }else { + new_lock =raw_get_lock_mode (state->flags); + } + +for (rec = &reopen_functions[0 ]; + rec < &reopen_functions[ARRAY_SIZE (reopen_functions)]; + rec++) { +if (rec->old_lock == old_lock && rec->new_lock == new_lock) { +break ; + } + } +assert (rec != &reopen_functions[ARRAY_SIZE (reopen_functions)]); + +switch (op) { +case RAW_LT_PREPARE : +if (rec->need_lock_fd) { + ret =raw_open_lockfd (state->bs->exact_filename, + rs->open_flags, &new_lock, errp); +if (ret <0 ) { +return ret; + } + rs->lock_fd = ret; + }else { + rs->lock_fd =-1 ; + } + ret = rec->func (op, s->lock_fd, rs->lock_fd, old_lock, new_lock, errp); +if (!ret) { +return ret; + } +/* Only succeeded preparation will be reverted by block layer, we +* need to clean up this failure manually. */ + op =RAW_LT_ABORT ; +/* fall through */ +case RAW_LT_ABORT : + rec->func (op, s->lock_fd, rs->lock_fd, old_lock, new_lock, &error_abort); +if (rs->lock_fd >=0 ) { +qemu_close (rs->lock_fd); + rs->lock_fd =-1 ; + } +break ; +case RAW_LT_COMMIT : + rec->func (op, s->lock_fd, rs->lock_fd, old_lock, new_lock, &error_abort); +if ((rec->need_lock_fd || rec->close_old_lock_fd) && s->lock_fd >=0 ) { +qemu_close (s->lock_fd); + s->lock_fd =-1 ; + } +if (rec->need_lock_fd) { + s->lock_fd = rs->lock_fd; + } + s->cur_lock_mode = new_lock; + s->disable_lock = rs->disable_lock; +break ; + } +return 0 ; +} +static int raw_reopen_prepare (BDRVReopenState *state,BlockReopenQueue *queue,Error **errp) {
@@ -609,13 +1238,20 @@ static int raw_reopen_prepare(BDRVReopenState *state,if (rs->fd !=-1 ) {raw_probe_alignment (state->bs, rs->fd, &local_err);if (local_err) { -qemu_close (rs->fd); - rs->fd =-1 ;error_propagate (errp, local_err); ret = -EINVAL ; + goto fail; } } + ret =raw_reopen_handle_lock (state,RAW_LT_PREPARE , errp); +if (ret) { + goto fail; + } +return 0 ; +fail: +qemu_close (rs->fd); + rs->fd =-1 ;return ret; }
@@ -626,6 +1262,8 @@ static void raw_reopen_commit(BDRVReopenState *state) s->open_flags = rs->open_flags; +raw_reopen_handle_lock (state,RAW_LT_COMMIT , &error_abort); +qemu_close (s->fd); s->fd = rs->fd;
@@ -643,6 +1281,8 @@ static void raw_reopen_abort(BDRVReopenState *state)return ; } +raw_reopen_handle_lock (state,RAW_LT_ABORT , &error_abort); +if (rs->fd >=0 ) {qemu_close (rs->fd); rs->fd =-1 ;
@@ -1332,6 +1972,10 @@ static void raw_close(BlockDriverState *bs)qemu_close (s->fd); s->fd =-1 ; } +if (s->lock_fd >=0 ) { +qemu_close (s->lock_fd); + s->lock_fd =-1 ; + } }static int raw_truncate (BlockDriverState *bs,int64_t offset)
@@ -1832,6 +2476,22 @@ static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)return 0 ; } +static int raw_inactivate (BlockDriverState *bs) +{ +BDRVRawState *s = bs->opaque; +int r =0 ; + +if (s->cur_lock_mode !=RAW_L_READ_SHARE_RW ) { + r =raw_lock_fd (s->lock_fd,RAW_L_READ_SHARE_RW ,NULL ); + } +return r; +} + +static void raw_invalidate_cache (BlockDriverState *bs,Error **errp) +{ +raw_apply_image_lock (bs,bdrv_get_flags (bs), errp); +} +static QemuOptsList raw_create_opts = { .name ="raw-create-opts" , .head =QTAILQ_HEAD_INITIALIZER (raw_create_opts.head),
@@ -1885,7 +2545,8 @@ BlockDriver bdrv_file = { .bdrv_get_info = raw_get_info, .bdrv_get_allocated_file_size = raw_get_allocated_file_size, - + .bdrv_inactivate = raw_inactivate, + .bdrv_invalidate_cache = raw_invalidate_cache, .create_opts = &raw_create_opts, };
Signature · 1 line
2.9.3
Re: [Qemu-devel] [PATCH v10 14/16] file-posix: Implement image locking
Quoted 26 lines · Fam Zheng
This implements open flag sensible image locking for local file and host device protocol.
virtlockd in libvirt locks the first byte, so we start looking at the file bytes from 1.
Quoting what was proposed by Kevin Wolf <[email protected]>, there are four locking modes by combining two bits (
BDRV_O_RDWRandBDRV_O_SHARE_RW), and implemented by taking two locks:Lock bytes:
* byte 1: I can't allow other processes to write to the image
* byte 2: I am writing to the imageLock modes:
* shared writer (
BDRV_O_RDWR|BDRV_O_SHARE_RW): Take shared lock on byte 2. Test whether byte 1 is locked using an exclusive lock, and fail if so.* exclusive writer (
BDRV_O_RDWRonly): Take shared lock on byte 2. Test whether byte 1 is locked using an exclusive lock, and fail if so. Then take shared lock on byte 1. I suppose this is racy, but we can probably tolerate that.
Quoted 4 lines
* reader that can tolerate writers (
BDRV_O_SHARE_RWonly): Don't do anything* reader that can't tolerate writers (neither bit is set): Take shared lock on byte 1. Test whether byte 2 is locked, and fail if so.
Ahh, using two bytes is an interesting technique for mapping the four different access methods onto the more limit fcntl lock semantics. We might want to copy this approach in libvirt too....
block/file-posix.c do { \ · line 137
Quoted 4 lines
block/file-posix.c +4
-0+/* Posix file locking bytes. Libvirt takes byte 0, so start from byte 1. */ +#define RAW_LOCK_BYTE_MIN 1 +#define RAW_LOCK_BYTE_NO_OTHER_WRITER 1 +#define RAW_LOCK_BYTE_WRITE 2
...would you mind if QEMU started from say byte 10, leaving the first 10 reserved for libvirt uses. This lets libvirt have a continuous space for its own usage if we want to use more bytes
Regards,
Daniel
Signature · 3 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://entangle-photo.org -o- http://search.cpan.org/~danberr/ :|
Re: [Qemu-devel] [Qemu-block] [PATCH v10 14/16] file-posix: Implement image locking
Quoted 12 lines · Daniel P. Berrange
Quoted 10 lines · Fam Zheng
This implements open flag sensible image locking for local file and host device protocol.
virtlockd in libvirt locks the first byte, so we start looking at the file bytes from 1.
Quoting what was proposed by Kevin Wolf <[email protected]>, there are four locking modes by combining two bits (
BDRV_O_RDWRandBDRV_O_SHARE_RW), and implemented by taking two locks:
Quoted 8 lines
block/file-posix.c do { \ · line 137
Quoted 4 lines
block/file-posix.c +4
-0+/* Posix file locking bytes. Libvirt takes byte 0, so start from byte 1. */ +#define RAW_LOCK_BYTE_MIN 1 +#define RAW_LOCK_BYTE_NO_OTHER_WRITER 1 +#define RAW_LOCK_BYTE_WRITE 2...would you mind if QEMU started from say byte 10, leaving the first 10 reserved for libvirt uses. This lets libvirt have a continuous space for its own usage if we want to use more bytes
Thankfully, fcntl() locks can be taken beyond end-of-file, so I think we're okay making an arbitrarily larger range of bytes reserved for each process, even in the unlikely corner case of passing files smaller than 512 bytes.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [Qemu-block] [PATCH v10 14/16] file-posix: Implement image locking
Quoted 22 lines · Eric Blake
Quoted 12 lines · Daniel P. Berrange
Quoted 10 lines · Fam Zheng
This implements open flag sensible image locking for local file and host device protocol.
virtlockd in libvirt locks the first byte, so we start looking at the file bytes from 1.
Quoting what was proposed by Kevin Wolf <[email protected]>, there are four locking modes by combining two bits (
BDRV_O_RDWRandBDRV_O_SHARE_RW), and implemented by taking two locks:Quoted 8 lines
block/file-posix.c do { \ · line 137
Quoted 4 lines
block/file-posix.c +4
-0+/* Posix file locking bytes. Libvirt takes byte 0, so start from byte 1. */ +#define RAW_LOCK_BYTE_MIN 1 +#define RAW_LOCK_BYTE_NO_OTHER_WRITER 1 +#define RAW_LOCK_BYTE_WRITE 2...would you mind if QEMU started from say byte 10, leaving the first 10 reserved for libvirt uses. This lets libvirt have a continuous space for its own usage if we want to use more bytes
Quoted 4 lines
Thankfully,
fcntl()locks can be taken beyond end-of-file, so I think we're okay making an arbitrarily larger range of bytes reserved for each process, even in the unlikely corner case of passing files smaller than 512 bytes.
Not so unlikely. libguestfs actually detects and works around this case because qemu was (possibly still is) very broken when you pass small files.
https://github.com/libguestfs/libguestfs/blob/master/src/drives.c#L395-L441
Rich.
Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
Re: [Qemu-devel] [PATCH v10 14/16] file-posix: Implement image locking
In reply to Daniel P. BerrangeQuoted 36 lines · Daniel P. Berrange
Quoted 26 lines · Fam Zheng
This implements open flag sensible image locking for local file and host device protocol.
virtlockd in libvirt locks the first byte, so we start looking at the file bytes from 1.
Quoting what was proposed by Kevin Wolf <[email protected]>, there are four locking modes by combining two bits (
BDRV_O_RDWRandBDRV_O_SHARE_RW), and implemented by taking two locks:Lock bytes:
* byte 1: I can't allow other processes to write to the image
* byte 2: I am writing to the imageLock modes:
* shared writer (
BDRV_O_RDWR|BDRV_O_SHARE_RW): Take shared lock on byte 2. Test whether byte 1 is locked using an exclusive lock, and fail if so.* exclusive writer (
BDRV_O_RDWRonly): Take shared lock on byte 2. Test whether byte 1 is locked using an exclusive lock, and fail if so. Then take shared lock on byte 1. I suppose this is racy, but we can probably tolerate that.Quoted 4 lines
* reader that can tolerate writers (
BDRV_O_SHARE_RWonly): Don't do anything* reader that can't tolerate writers (neither bit is set): Take shared lock on byte 1. Test whether byte 2 is locked, and fail if so.
Ahh, using two bytes is an interesting technique for mapping the four different access methods onto the more limit fcntl lock semantics. We might want to copy this approach in libvirt too....
Quoted 8 lines
block/file-posix.c do { \ · line 137
Quoted 4 lines
block/file-posix.c +4
-0+/* Posix file locking bytes. Libvirt takes byte 0, so start from byte 1. */ +#define RAW_LOCK_BYTE_MIN 1 +#define RAW_LOCK_BYTE_NO_OTHER_WRITER 1 +#define RAW_LOCK_BYTE_WRITE 2...would you mind if QEMU started from say byte 10, leaving the first 10 reserved for libvirt uses. This lets libvirt have a continuous space for its own usage if we want to use more bytes
That's easy, will do it. (Actually the descriptions above are a bit stale because exclusive writers now take two bytes exclusively and the lock testings are done with F_OFD_GETLK so that RO readers can test against shared locks without acquiring a write lock, which requires O_RDWR).
Fam
PATCH v10 15/16 qcow2: Force "no other writer" lock on bs->file
In reply to Fam ZhengWriting to the same qcow2 file from two QEMU processes at the same time will never work correctly, so disable it even when the caller specifies BDRV_O_RDWR.
Other formats are less vulnerable because they don't have internal snapshots thus qemu-img is less often misused to create live snapshot.
--- block/qcow2.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
block/qcow2.c +11 -0
@@ -1177,6 +1177,17 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags, } } +if ((flags &BDRV_O_SHARE_RW ) && (flags &BDRV_O_RDWR )) { +/* Shared write is never a good idea for qcow2, override it. +* XXX: Use permission propagation and masking mechanism in op blockers +* API once it's there. */ + ret =bdrv_reopen (bs->file->bs, flags & ~BDRV_O_SHARE_RW , &local_err); +if (ret) { +error_propagate (errp, local_err); + goto fail; + } + } +#ifdef DEBUG_ALLOC {BdrvCheckResult result = {0 };
Signature · 1 line
2.9.3
PATCH v10 16/16 tests: Add test-image-lock
In reply to Fam Zheng--- tests/Makefile.include | 2 + tests/test-image-lock.c | 200 +++++++++++++++++++++++++++++++++++++++++++++++ tests/test-replication.c | 6 +- 3 files changed, 205 insertions(+), 3 deletions(-) create mode 100644 tests/test-image-lock.c
tests/Makefile.include +2 -0
@@ -55,6 +55,7 @@ check-unit-y += tests/test-hbitmap$(EXESUF) gcov-files-test-hbitmap-y = blockjob.c check-unit-y += tests/test-blockjob$(EXESUF) check-unit-y += tests/test-blockjob-txn$(EXESUF) +check-unit-y += tests/test-image-lock$(EXESUF) check-unit-y += tests/test-x86-cpuid$(EXESUF) # all code tested by test-x86-cpuid is inside topology.h gcov-files-test-x86-cpuid-y =
@@ -516,6 +517,7 @@ tests/test-aio$(EXESUF): tests/test-aio.o $(test-block-obj-y) tests/test-throttle$(EXESUF): tests/test-throttle.o $(test-block-obj-y) tests/test-blockjob$(EXESUF): tests/test-blockjob.o $(test-block-obj-y) $(test-util-obj-y) tests/test-blockjob-txn$(EXESUF): tests/test-blockjob-txn.o $(test-block-obj-y) $(test-util-obj-y) +tests/test-image-lock$(EXESUF): tests/test-image-lock.o $(test-block-obj-y) $(libqos-obj-y) tests/test-thread-pool$(EXESUF): tests/test-thread-pool.o $(test-block-obj-y) tests/test-iov$(EXESUF): tests/test-iov.o $(test-util-obj-y) tests/test-hbitmap$(EXESUF): tests/test-hbitmap.o $(test-util-obj-y)
tests/test-image-lock.c new file +200 -0
200 lines not on this page · raw message
tests/test-replication.c +3 -3
@@ -312,7 +312,7 @@ static BlockBackend *start_secondary(void)/* add s_local_disk and forge S_LOCAL_DISK_ID */ cmdline =g_strdup_printf ("file.filename=%s,driver=qcow2," -"file.file.disable-lock=on" , +"file.disable-lock=on" , s_local_disk); opts =qemu_opts_parse_noisily (&qemu_drive_opts, cmdline, false);g_free (cmdline);
@@ -334,10 +334,10 @@ static BlockBackend *start_secondary(void)/* add S_(ACTIVE/HIDDEN)_DISK and forge S_ID */ cmdline =g_strdup_printf ("driver=replication,mode=secondary,top-id=%s," "file.driver=qcow2,file.file.filename=%s," -"file.file.disable-lock=on" , +"file.file.disable-lock=on," "file.backing.driver=qcow2," "file.backing.file.filename=%s," -"file.backing.file.disable-lock=on" , +"file.backing.file.disable-lock=on," "file.backing.backing=%s" ,S_ID , s_active_disk, s_hidden_disk ,S_LOCAL_DISK_ID );
Signature · 1 line
2.9.3