[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 09/18] block/qcow2: Keep auto_backing_file if possible
From: |
Kevin Wolf |
Subject: |
[PULL 09/18] block/qcow2: Keep auto_backing_file if possible |
Date: |
Fri, 30 Sep 2022 18:52:13 +0200 |
From: Hanna Reitz <hreitz@redhat.com>
qcow2_do_open() is used by qcow2_co_invalidate_cache(), i.e. may be run
on an image that has been opened before. When reading the backing file
string from the image header, compare it against the existing
bs->backing_file, and update bs->auto_backing_file only if they differ.
auto_backing_file should ideally contain the filename the backing BDS
will actually have after opening, i.e. a post-bdrv_refresh_filename()
version of what is in the image header. So for example, if the image
header reports the following backing file string:
json:{"driver": "qcow2", "file": {
"driver": "file", "filename": "/tmp/backing.qcow2"
}}
Then auto_backing_file should contain simply "/tmp/backing.qcow2".
Because bdrv_refresh_filename() only works on existing BDSs, though, the
way how we get this auto_backing_file value is to have the format driver
set it to whatever is in the image header, and when the backing BDS is
opened based on that, we update it with the filename the backing BDS
actually got.
However, qcow2's qcow2_co_invalidate_cache() implementation breaks this
because it just resets auto_backing_file to whatever is in the image
file without opening a BDS based on it, so we never get
auto_backing_file back to the "refreshed" version, and in the example
above, it would stay "json:{...}".
Then, bs->backing->bs->filename will differ from bs->auto_backing_file,
making bdrv_backing_overridden(bs) return true, which will lead
bdrv_refresh_filename(bs) to generate a json:{} filename for bs, even
though that may not have been necessary. This is reported in the issue
linked below.
Therefore, skip updating auto_backing_file if nothing has changed in the
image header since we last read it.
Fixes: https://gitlab.com/qemu-project/qemu/-/issues/1117
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
Message-Id: <20220803144446.20723-2-hreitz@redhat.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
block/qcow2.c | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)
diff --git a/block/qcow2.c b/block/qcow2.c
index c8fc3a6160..6c8c8b2b5a 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -1697,16 +1697,27 @@ static int coroutine_fn qcow2_do_open(BlockDriverState
*bs, QDict *options,
ret = -EINVAL;
goto fail;
}
+
+ s->image_backing_file = g_malloc(len + 1);
ret = bdrv_pread(bs->file, header.backing_file_offset, len,
- bs->auto_backing_file, 0);
+ s->image_backing_file, 0);
if (ret < 0) {
error_setg_errno(errp, -ret, "Could not read backing file name");
goto fail;
}
- bs->auto_backing_file[len] = '\0';
- pstrcpy(bs->backing_file, sizeof(bs->backing_file),
- bs->auto_backing_file);
- s->image_backing_file = g_strdup(bs->auto_backing_file);
+ s->image_backing_file[len] = '\0';
+
+ /*
+ * Update only when something has changed. This function is called by
+ * qcow2_co_invalidate_cache(), and we do not want to reset
+ * auto_backing_file unless necessary.
+ */
+ if (!g_str_equal(s->image_backing_file, bs->backing_file)) {
+ pstrcpy(bs->backing_file, sizeof(bs->backing_file),
+ s->image_backing_file);
+ pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file),
+ s->image_backing_file);
+ }
}
/*
--
2.37.3
- [PULL 00/18] Block layer patches, Kevin Wolf, 2022/09/30
- [PULL 02/18] block/qcow2-bitmap: Add missing cast to silent GCC error, Kevin Wolf, 2022/09/30
- [PULL 01/18] qcow2: fix memory leak in qcow2_read_extensions, Kevin Wolf, 2022/09/30
- [PULL 06/18] block: use bdrv_is_sg() helper instead of raw bs->sg reading, Kevin Wolf, 2022/09/30
- [PULL 04/18] block: pass OnOffAuto instead of bool to block_acct_setup(), Kevin Wolf, 2022/09/30
- [PULL 03/18] qemu-img: Wean documentation and help output off '?' for help, Kevin Wolf, 2022/09/30
- [PULL 07/18] block: make serializing requests functions 'void', Kevin Wolf, 2022/09/30
- [PULL 05/18] block: add missed block_acct_setup with new block device init procedure, Kevin Wolf, 2022/09/30
- [PULL 08/18] gluster: stop using .bdrv_needs_filename, Kevin Wolf, 2022/09/30
- [PULL 14/18] piix_ide_reset: Use pci_set_* functions instead of direct access, Kevin Wolf, 2022/09/30
- [PULL 09/18] block/qcow2: Keep auto_backing_file if possible,
Kevin Wolf <=
- [PULL 12/18] block: move bdrv_qiov_is_aligned to file-posix, Kevin Wolf, 2022/09/30
- [PULL 10/18] block/qed: Keep auto_backing_file if possible, Kevin Wolf, 2022/09/30
- [PULL 16/18] hw/ide/core: Clear LBA and drive bits for EXECUTE DEVICE DIAGNOSTIC, Kevin Wolf, 2022/09/30
- [PULL 15/18] tests/qtest/ide-test.c: Create disk image for use as a secondary, Kevin Wolf, 2022/09/30
- [PULL 13/18] block: use the request length for iov alignment, Kevin Wolf, 2022/09/30
- [PULL 17/18] tests/qtest/ide-test: Verify that DIAGNOSTIC clears DEV to zero, Kevin Wolf, 2022/09/30
- [PULL 18/18] hw/ide/core.c: Implement ATA INITIALIZE_DEVICE_PARAMETERS command, Kevin Wolf, 2022/09/30
- [PULL 11/18] iotests/backing-file-invalidation: Add new test, Kevin Wolf, 2022/09/30