io: add new qio_channel_{readv, writev, read, write}_all functions
PATCH 1/1 io: add new qio_channel_{readv, writev, read, write}_all functions
These functions wait until they are able to read / write the full requested data buffer(s).
---
This patch combines these two previous proposals:
- https://lists.gnu.org/archive/html/qemu-devel/2017-08/msg01536.html
- https://lists.gnu.org/archive/html/qemu-devel/2017-08/msg04041.html
and switches the test suite over to use the new APIs so we get coverage by all the tests/test-io-channel-* test programs
include/io/channel.h | 90 +++++++++++++++++++++++++++++++++++++++ io/channel.c | 98 +++++++++++++++++++++++++++++++++++++++++++ tests/io-channel-helpers.c | 102 ++++----------------------------------------- 3 files changed, 197 insertions(+), 93 deletions(-)
include/io/channel.h +90 -0
@@ -269,6 +269,58 @@ ssize_t qio_channel_writev_full(QIOChannel *ioc, Error **errp);/** +* qio_channel_readv_all: +* @ioc: the channel object +* @iov: the array of memory regions to read data into +* @niov: the length of the @iov array +* @errp: pointer to a NULL-initialized error object +* +* Read data from the IO channel, storing it in the +* memory regions referenced by @iov. Each element +* in the @iov will be fully populated with data +* before the next one is used. The @niov parameter +* specifies the total number of elements in @iov. +* +* The function will wait for all requested data +* to be read, yielding from the current couroutine +* if required. +* +* If end-of-file occurrs before all requested data +* has been read, an error will be reported. +* +* Returns: 0 if all bytes were read, or -1 on error +*/ +int coroutine_fn qio_channel_readv_all(QIOChannel *ioc, +const struct iovec *iov, +size_t niov, +Error **errp); + + +/** +* qio_channel_writev_all: +* @ioc: the channel object +* @iov: the array of memory regions to write data from +* @niov: the length of the @iov array +* @errp: pointer to a NULL-initialized error object +* +* Write data to the IO channel, reading it from the +* memory regions referenced by @iov. Each element +* in the @iov will be fully sent, before the next +* one is used. The @niov parameter specifies the +* total number of elements in @iov. +* +* The function will wait for all requested data +* to be written, yielding from the current couroutine +* if required. +* +* Returns: 0 if all bytes were written, or -1 on error +*/ +int coroutine_fn qio_channel_writev_all(QIOChannel *ioc, +const struct iovec *iov, +size_t niov, +Error **erp); + +/** * qio_channel_readv: * @ioc: the channel object * @iov: the array of memory regions to read data into
@@ -331,6 +383,44 @@ ssize_t qio_channel_write(QIOChannel *ioc, Error **errp);/** +* qio_channel_read_all: +* @ioc: the channel object +* @buf: the memory region to read data into +* @buflen: the number of bytes to @buf +* @errp: pointer to a NULL-initialized error object +* +* Reads @buflen bytes into @buf, possibly blocking or (if the +* channel is non-blocking) yielding from the current coroutine +* multiple times until the entire content is read. If end-of-file +* occurs it will return an error rather than a short-read. Otherwise +* behaves as qio_channel_read(). +* +* Returns: 0 if all bytes were read, or -1 on error +*/ +int coroutine_fn qio_channel_read_all(QIOChannel *ioc, +char *buf, +size_t buflen, +Error **errp); +/** +* qio_channel_write_all: +* @ioc: the channel object +* @buf: the memory region to write data into +* @buflen: the number of bytes to @buf +* @errp: pointer to a NULL-initialized error object +* +* Writes @buflen bytes from @buf, possibly blocking or (if the +* channel is non-blocking) yielding from the current coroutine +* multiple times until the entire content is written. Otherwise +* behaves as qio_channel_write(). +* +* Returns: 0 if all bytes were written, or -1 on error +*/ +int coroutine_fn qio_channel_write_all(QIOChannel *ioc, +const char *buf, +size_t buflen, +Error **errp); + +/** * qio_channel_set_blocking: * @ioc: the channel object * @enabled: the blocking flag state
io/channel.c +98 -0
116 lines not on this page · raw message
tests/io-channel-helpers.c +9 -93
130 lines not on this page · raw message
Signature · 1 line
2.13.5
Quoted 3 lines · Daniel P. Berrange
These functions wait until they are able to read / write the full requested data buffer(s).
Hmm, sounds like the NBD code would benefit from using this in a followup patch.
Quoted 12 lines
Signed-off-by Daniel P. Berrange <[email protected]>---
This patch combines these two previous proposals:
- https://lists.gnu.org/archive/html/qemu-devel/2017-08/msg01536.html
- https://lists.gnu.org/archive/html/qemu-devel/2017-08/msg04041.htmland switches the test suite over to use the new APIs so we get coverage by all the tests/test-io-channel-* test programs
Quoted 15 lines
/**
+ *qio_channel_readv_all:
+ * @ioc: the channel object
+ * @iov: the array of memory regions to read data into
+ * @niov: the length of the @iov array
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Read data from the IO channel, storing it in the
+ * memory regions referenced by @iov. Each element
+ * in the @iov will be fully populated with data
+ * before the next one is used. The @niov parameter
+ * specifies the total number of elements in @iov.
+ *
+ * The function will wait for all requested data
+ * to be read, yielding from the current couroutine
s/couroutine/coroutine/
Quoted 3 lines
+ * if required.
+ *
+ * If end-of-file occurrs before all requested data
s/occurrs/occurs/
Quoted 5 lines
+ * has been read, an error will be reported.
+ *
+ * Returns: 0 if all bytes were read, or -1 on error
+ */
Quoted 15 lines
+/**
+ *qio_channel_writev_all:
+ * @ioc: the channel object
+ * @iov: the array of memory regions to write data from
+ * @niov: the length of the @iov array
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Write data to the IO channel, reading it from the
+ * memory regions referenced by @iov. Each element
+ * in the @iov will be fully sent, before the next
+ * one is used. The @niov parameter specifies the
+ * total number of elements in @iov.
+ *
+ * The function will wait for all requested data
+ * to be written, yielding from the current couroutine
s/couroutine/coroutine/
io/channel.c ssize_t qio_channel_writev_full(QIOChannel *ioc, · line 112
Quoted 13 lines
+++
b/io/channel.cio/channel.c +10
-0+int qio_channel_readv_all (QIOChannel *ioc, + while (nlocal_iov > 0) { +ssize_t len; + len =qio_channel_readv (ioc, local_iov, nlocal_iov, errp); +if (len ==QIO_CHANNEL_ERR_BLOCK ) { +qio_channel_wait (ioc,G_IO_IN ); +continue ; + }else if (len <0 ) { +error_setg_errno (errp,EIO , +"Channel was not able to read full iov" );
Should we report -len instead of EIO here?
io/channel.c ssize_t qio_channel_writev_full(QIOChannel *ioc, · line 153
Quoted 12 lines
io/channel.c +11
-0+int qio_channel_writev_all (QIOChannel *ioc, + while (nlocal_iov > 0) { +ssize_t len; + len =qio_channel_writev (ioc, local_iov, nlocal_iov, errp); +if (len ==QIO_CHANNEL_ERR_BLOCK ) { +qio_channel_wait (ioc,G_IO_OUT ); +continue ; + } +if (len <0 ) { +error_setg_errno (errp,EIO , +"Channel was not able to write full iov" );
and again
With the typos fixed, and either an explanation why EIO is better or else a fix to preserve errno,
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 81 lines · Eric Blake
Quoted 3 lines · Daniel P. Berrange
These functions wait until they are able to read / write the full requested data buffer(s).
Hmm, sounds like the NBD code would benefit from using this in a followup patch.
Quoted 12 lines
Signed-off-by Daniel P. Berrange <[email protected]>---
This patch combines these two previous proposals:
- https://lists.gnu.org/archive/html/qemu-devel/2017-08/msg01536.html
- https://lists.gnu.org/archive/html/qemu-devel/2017-08/msg04041.htmland switches the test suite over to use the new APIs so we get coverage by all the tests/test-io-channel-* test programs
Quoted 15 lines
/**
+ *qio_channel_readv_all:
+ * @ioc: the channel object
+ * @iov: the array of memory regions to read data into
+ * @niov: the length of the @iov array
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Read data from the IO channel, storing it in the
+ * memory regions referenced by @iov. Each element
+ * in the @iov will be fully populated with data
+ * before the next one is used. The @niov parameter
+ * specifies the total number of elements in @iov.
+ *
+ * The function will wait for all requested data
+ * to be read, yielding from the current couroutines/couroutine/coroutine/
Quoted 3 lines
+ * if required.
+ *
+ * If end-of-file occurrs before all requested datas/occurrs/occurs/
Quoted 5 lines
+ * has been read, an error will be reported.
+ *
+ * Returns: 0 if all bytes were read, or -1 on error
+ */Quoted 15 lines
+/**
+ *qio_channel_writev_all:
+ * @ioc: the channel object
+ * @iov: the array of memory regions to write data from
+ * @niov: the length of the @iov array
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Write data to the IO channel, reading it from the
+ * memory regions referenced by @iov. Each element
+ * in the @iov will be fully sent, before the next
+ * one is used. The @niov parameter specifies the
+ * total number of elements in @iov.
+ *
+ * The function will wait for all requested data
+ * to be written, yielding from the current couroutines/couroutine/coroutine/
io/channel.c ssize_t qio_channel_writev_full(QIOChannel *ioc, · line 112
Quoted 13 lines
+++
b/io/channel.cio/channel.c +10
-0+int qio_channel_readv_all (QIOChannel *ioc, + while (nlocal_iov > 0) { +ssize_t len; + len =qio_channel_readv (ioc, local_iov, nlocal_iov, errp); +if (len ==QIO_CHANNEL_ERR_BLOCK ) { +qio_channel_wait (ioc,G_IO_IN ); +continue ; + }else if (len <0 ) { +error_setg_errno (errp,EIO , +"Channel was not able to read full iov" );
Quoted 1 line
Should we report -len instead of EIO here?
No, QIO methods never return errno values, since channel implementations are not guaranteed to be calling commands that return errnos. eg the TLS wrapper calls gnutls which has no errno values reported.
If fact, though, we should just not call error_setg_errno() at all, since the qio_channel_readv method has already populated 'errp'.
Quoted 14 lines
io/channel.c ssize_t qio_channel_writev_full(QIOChannel *ioc, · line 153
Quoted 13 lines
io/channel.c +11
-0+int qio_channel_writev_all (QIOChannel *ioc, + while (nlocal_iov > 0) { +ssize_t len; + len =qio_channel_writev (ioc, local_iov, nlocal_iov, errp); +if (len ==QIO_CHANNEL_ERR_BLOCK ) { +qio_channel_wait (ioc,G_IO_OUT ); +continue ; + } +if (len <0 ) { +error_setg_errno (errp,EIO , +"Channel was not able to write full iov" );
Quoted 4 lines
and again
With the typos fixed, and either an explanation why EIO is better or else a fix to preserve errno,
Again, we should not call error_setg_errno at all, since errp is already populated.
Quoted 2 lines
Reviewed-by Eric Blake <[email protected]>
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|