mirror of https://gitee.com/openkylin/qemu.git
consolidate qemu_iovec_memset{,_skip}() into single function and use existing iov_memset()
This patch combines two functions into one, and replaces the implementation with already existing iov_memset() from iov.c. The new prototype of qemu_iovec_memset(): size_t qemu_iovec_memset(qiov, size_t offset, int fillc, size_t bytes) It is different from former qemu_iovec_memset_skip(), and I want to make other functions to be consistent with it too: first how much to skip, second what, and 3rd how many of it. It also returns actual number of bytes filled in, which may be less than the requested `bytes' if qiov is smaller than offset+bytes, in the same way iov_memset() does. While at it, use utility function iov_memset() from iov.h in posix-aio-compat.c, where qiov was used. Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
This commit is contained in:
parent
2278a69e70
commit
3d9b49254f
3
Makefile
3
Makefile
|
@ -154,7 +154,8 @@ qemu-img.o: qemu-img-cmds.h
|
||||||
qemu-img.o qemu-tool.o qemu-nbd.o qemu-io.o cmd.o qemu-ga.o: $(GENERATED_HEADERS)
|
qemu-img.o qemu-tool.o qemu-nbd.o qemu-io.o cmd.o qemu-ga.o: $(GENERATED_HEADERS)
|
||||||
|
|
||||||
tools-obj-y = $(oslib-obj-y) $(trace-obj-y) qemu-tool.o qemu-timer.o \
|
tools-obj-y = $(oslib-obj-y) $(trace-obj-y) qemu-tool.o qemu-timer.o \
|
||||||
qemu-timer-common.o main-loop.o notify.o iohandler.o cutils.o async.o
|
qemu-timer-common.o main-loop.o notify.o \
|
||||||
|
iohandler.o cutils.o iov.o async.o
|
||||||
tools-obj-$(CONFIG_POSIX) += compatfd.o
|
tools-obj-$(CONFIG_POSIX) += compatfd.o
|
||||||
|
|
||||||
qemu-img$(EXESUF): qemu-img.o $(tools-obj-y) $(block-obj-y)
|
qemu-img$(EXESUF): qemu-img.o $(tools-obj-y) $(block-obj-y)
|
||||||
|
|
|
@ -42,7 +42,7 @@ coroutine-obj-$(CONFIG_WIN32) += coroutine-win32.o
|
||||||
#######################################################################
|
#######################################################################
|
||||||
# block-obj-y is code used by both qemu system emulation and qemu-img
|
# block-obj-y is code used by both qemu system emulation and qemu-img
|
||||||
|
|
||||||
block-obj-y = cutils.o cache-utils.o qemu-option.o module.o async.o
|
block-obj-y = cutils.o iov.o cache-utils.o qemu-option.o module.o async.o
|
||||||
block-obj-y += nbd.o block.o aio.o aes.o qemu-config.o qemu-progress.o qemu-sockets.o
|
block-obj-y += nbd.o block.o aio.o aes.o qemu-config.o qemu-progress.o qemu-sockets.o
|
||||||
block-obj-y += $(coroutine-obj-y) $(qobject-obj-y) $(version-obj-y)
|
block-obj-y += $(coroutine-obj-y) $(qobject-obj-y) $(version-obj-y)
|
||||||
block-obj-$(CONFIG_POSIX) += posix-aio-compat.o
|
block-obj-$(CONFIG_POSIX) += posix-aio-compat.o
|
||||||
|
@ -198,7 +198,7 @@ common-obj-$(CONFIG_XEN_BACKEND) += xen_console.o xenfb.o xen_disk.o xen_nic.o
|
||||||
user-obj-y =
|
user-obj-y =
|
||||||
user-obj-y += envlist.o path.o
|
user-obj-y += envlist.o path.o
|
||||||
user-obj-y += tcg-runtime.o host-utils.o
|
user-obj-y += tcg-runtime.o host-utils.o
|
||||||
user-obj-y += cutils.o cache-utils.o
|
user-obj-y += cutils.o iov.o cache-utils.o
|
||||||
user-obj-y += module.o
|
user-obj-y += module.o
|
||||||
user-obj-y += qemu-user.o
|
user-obj-y += qemu-user.o
|
||||||
user-obj-y += $(trace-obj-y)
|
user-obj-y += $(trace-obj-y)
|
||||||
|
|
|
@ -510,7 +510,7 @@ int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
|
||||||
else
|
else
|
||||||
n1 = bs->total_sectors - sector_num;
|
n1 = bs->total_sectors - sector_num;
|
||||||
|
|
||||||
qemu_iovec_memset_skip(qiov, 0, 512 * (nb_sectors - n1), 512 * n1);
|
qemu_iovec_memset(qiov, 512 * n1, 0, 512 * (nb_sectors - n1));
|
||||||
|
|
||||||
return n1;
|
return n1;
|
||||||
}
|
}
|
||||||
|
@ -571,7 +571,7 @@ static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* Note: in this case, no need to wait */
|
/* Note: in this case, no need to wait */
|
||||||
qemu_iovec_memset(&hd_qiov, 0, 512 * cur_nr_sectors);
|
qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -580,7 +580,7 @@ static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num,
|
||||||
ret = -EIO;
|
ret = -EIO;
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
qemu_iovec_memset(&hd_qiov, 0, 512 * cur_nr_sectors);
|
qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case QCOW2_CLUSTER_COMPRESSED:
|
case QCOW2_CLUSTER_COMPRESSED:
|
||||||
|
|
|
@ -736,7 +736,7 @@ static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos,
|
||||||
/* Zero all sectors if reading beyond the end of the backing file */
|
/* Zero all sectors if reading beyond the end of the backing file */
|
||||||
if (pos >= backing_length ||
|
if (pos >= backing_length ||
|
||||||
pos + qiov->size > backing_length) {
|
pos + qiov->size > backing_length) {
|
||||||
qemu_iovec_memset(qiov, 0, qiov->size);
|
qemu_iovec_memset(qiov, 0, 0, qiov->size);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Complete now if there are no backing file sectors to read */
|
/* Complete now if there are no backing file sectors to read */
|
||||||
|
@ -1251,7 +1251,7 @@ static void qed_aio_read_data(void *opaque, int ret,
|
||||||
|
|
||||||
/* Handle zero cluster and backing file reads */
|
/* Handle zero cluster and backing file reads */
|
||||||
if (ret == QED_CLUSTER_ZERO) {
|
if (ret == QED_CLUSTER_ZERO) {
|
||||||
qemu_iovec_memset(&acb->cur_qiov, 0, acb->cur_qiov.size);
|
qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size);
|
||||||
qed_aio_next_io(acb, 0);
|
qed_aio_next_io(acb, 0);
|
||||||
return;
|
return;
|
||||||
} else if (ret != QED_CLUSTER_FOUND) {
|
} else if (ret != QED_CLUSTER_FOUND) {
|
||||||
|
|
44
cutils.c
44
cutils.c
|
@ -26,6 +26,7 @@
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
#include "qemu_socket.h"
|
#include "qemu_socket.h"
|
||||||
|
#include "iov.h"
|
||||||
|
|
||||||
void pstrcpy(char *buf, int buf_size, const char *str)
|
void pstrcpy(char *buf, int buf_size, const char *str)
|
||||||
{
|
{
|
||||||
|
@ -260,47 +261,10 @@ void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void qemu_iovec_memset(QEMUIOVector *qiov, int c, size_t count)
|
size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
|
||||||
|
int fillc, size_t bytes)
|
||||||
{
|
{
|
||||||
size_t n;
|
return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes);
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i = 0; i < qiov->niov && count; ++i) {
|
|
||||||
n = MIN(count, qiov->iov[i].iov_len);
|
|
||||||
memset(qiov->iov[i].iov_base, c, n);
|
|
||||||
count -= n;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void qemu_iovec_memset_skip(QEMUIOVector *qiov, int c, size_t count,
|
|
||||||
size_t skip)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
size_t done;
|
|
||||||
void *iov_base;
|
|
||||||
uint64_t iov_len;
|
|
||||||
|
|
||||||
done = 0;
|
|
||||||
for (i = 0; (i < qiov->niov) && (done != count); i++) {
|
|
||||||
if (skip >= qiov->iov[i].iov_len) {
|
|
||||||
/* Skip the whole iov */
|
|
||||||
skip -= qiov->iov[i].iov_len;
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
/* Skip only part (or nothing) of the iov */
|
|
||||||
iov_base = (uint8_t*) qiov->iov[i].iov_base + skip;
|
|
||||||
iov_len = qiov->iov[i].iov_len - skip;
|
|
||||||
skip = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (done + iov_len > count) {
|
|
||||||
memset(iov_base, c, count - done);
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
memset(iov_base, c, iov_len);
|
|
||||||
}
|
|
||||||
done += iov_len;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -63,8 +63,8 @@ static void qemu_laio_process_completion(struct qemu_laio_state *s,
|
||||||
} else if (ret >= 0) {
|
} else if (ret >= 0) {
|
||||||
/* Short reads mean EOF, pad with zeros. */
|
/* Short reads mean EOF, pad with zeros. */
|
||||||
if (laiocb->is_read) {
|
if (laiocb->is_read) {
|
||||||
qemu_iovec_memset_skip(laiocb->qiov, 0,
|
qemu_iovec_memset(laiocb->qiov, ret, 0,
|
||||||
laiocb->qiov->size - ret, ret);
|
laiocb->qiov->size - ret);
|
||||||
} else {
|
} else {
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "trace.h"
|
#include "trace.h"
|
||||||
#include "block_int.h"
|
#include "block_int.h"
|
||||||
|
#include "iov.h"
|
||||||
|
|
||||||
#include "block/raw-posix-aio.h"
|
#include "block/raw-posix-aio.h"
|
||||||
|
|
||||||
|
@ -351,11 +352,8 @@ static void *aio_thread(void *unused)
|
||||||
if (ret >= 0 && ret < aiocb->aio_nbytes && aiocb->common.bs->growable) {
|
if (ret >= 0 && ret < aiocb->aio_nbytes && aiocb->common.bs->growable) {
|
||||||
/* A short read means that we have reached EOF. Pad the buffer
|
/* A short read means that we have reached EOF. Pad the buffer
|
||||||
* with zeros for bytes after EOF. */
|
* with zeros for bytes after EOF. */
|
||||||
QEMUIOVector qiov;
|
iov_memset(aiocb->aio_iov, aiocb->aio_niov, ret,
|
||||||
|
0, aiocb->aio_nbytes - ret);
|
||||||
qemu_iovec_init_external(&qiov, aiocb->aio_iov,
|
|
||||||
aiocb->aio_niov);
|
|
||||||
qemu_iovec_memset_skip(&qiov, 0, aiocb->aio_nbytes - ret, ret);
|
|
||||||
|
|
||||||
ret = aiocb->aio_nbytes;
|
ret = aiocb->aio_nbytes;
|
||||||
}
|
}
|
||||||
|
|
|
@ -347,9 +347,8 @@ void qemu_iovec_destroy(QEMUIOVector *qiov);
|
||||||
void qemu_iovec_reset(QEMUIOVector *qiov);
|
void qemu_iovec_reset(QEMUIOVector *qiov);
|
||||||
void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf);
|
void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf);
|
||||||
void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count);
|
void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count);
|
||||||
void qemu_iovec_memset(QEMUIOVector *qiov, int c, size_t count);
|
size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
|
||||||
void qemu_iovec_memset_skip(QEMUIOVector *qiov, int c, size_t count,
|
int fillc, size_t bytes);
|
||||||
size_t skip);
|
|
||||||
|
|
||||||
bool buffer_is_zero(const void *buf, size_t len);
|
bool buffer_is_zero(const void *buf, size_t len);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue