mirror of https://github.com/xemu-project/xemu.git
pflash: Only read non-zero parts of backend image
Currently we fill the VIRT_FLASH memory space with two 64MB NOR images when using persistent UEFI variables on virt board. Actually we only use a very small(non-zero) part of the memory while the rest significant large(zero) part of memory is wasted. So this patch checks the block status and only writes the non-zero part into memory. This requires pflash devices to use sparse files for backends. Signed-off-by: Xiang Zheng <zhengxiang9@huawei.com> [ kraxel: rebased to latest master ] Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> Message-Id: <20221220084246.1984871-1-kraxel@redhat.com> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Reviewed-by: Kevin Wolf <kwolf@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
parent
95988739c7
commit
a4b15a8b9e
|
@ -15,6 +15,40 @@
|
||||||
#include "qapi/error.h"
|
#include "qapi/error.h"
|
||||||
#include "qapi/qapi-types-block.h"
|
#include "qapi/qapi-types-block.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Read the non-zeroes parts of @blk into @buf
|
||||||
|
* Reading all of the @blk is expensive if the zeroes parts of @blk
|
||||||
|
* is large enough. Therefore check the block status and only write
|
||||||
|
* the non-zeroes block into @buf.
|
||||||
|
*
|
||||||
|
* Return 0 on success, non-zero on error.
|
||||||
|
*/
|
||||||
|
static int blk_pread_nonzeroes(BlockBackend *blk, hwaddr size, void *buf)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
int64_t bytes, offset = 0;
|
||||||
|
BlockDriverState *bs = blk_bs(blk);
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
bytes = MIN(size - offset, BDRV_REQUEST_MAX_SECTORS);
|
||||||
|
if (bytes <= 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
ret = bdrv_block_status(bs, offset, bytes, &bytes, NULL, NULL);
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
if (!(ret & BDRV_BLOCK_ZERO)) {
|
||||||
|
ret = bdrv_pread(bs->file, offset, bytes,
|
||||||
|
(uint8_t *) buf + offset, 0);
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
offset += bytes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Read the entire contents of @blk into @buf.
|
* Read the entire contents of @blk into @buf.
|
||||||
* @blk's contents must be @size bytes, and @size must be at most
|
* @blk's contents must be @size bytes, and @size must be at most
|
||||||
|
@ -54,7 +88,7 @@ bool blk_check_size_and_read_all(BlockBackend *blk, void *buf, hwaddr size,
|
||||||
* block device and read only on demand.
|
* block device and read only on demand.
|
||||||
*/
|
*/
|
||||||
assert(size <= BDRV_REQUEST_MAX_BYTES);
|
assert(size <= BDRV_REQUEST_MAX_BYTES);
|
||||||
ret = blk_pread(blk, 0, size, buf, 0);
|
ret = blk_pread_nonzeroes(blk, size, buf);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_setg_errno(errp, -ret, "can't read block backend");
|
error_setg_errno(errp, -ret, "can't read block backend");
|
||||||
return false;
|
return false;
|
||||||
|
|
Loading…
Reference in New Issue