aboutsummaryrefslogtreecommitdiff
path: root/nbd/nbd-internal.h
diff options
context:
space:
mode:
authorVladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>2017-08-04 18:14:25 +0300
committerEric Blake <eblake@redhat.com>2017-08-30 13:00:38 -0500
commitab01df1fe21c013d1fd9e3f4c782430741c7dd33 (patch)
treeb65500fefd4e3f01cee811b6beaf87da40930b71 /nbd/nbd-internal.h
parenta0acf3a8f716946d7c425870bd05bc6dfe7667a2 (diff)
nbd/client: refactor nbd_read_eof
Refactor nbd_read_eof to return 1 on success, 0 on eof, when no data was read and <0 for other cases, because returned size of read data is not actually used. Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> Message-Id: <20170804151440.320927-3-vsementsov@virtuozzo.com> [eblake: tweak function comments, rebase to test 083 enhancements] Signed-off-by: Eric Blake <eblake@redhat.com>
Diffstat (limited to 'nbd/nbd-internal.h')
-rw-r--r--nbd/nbd-internal.h33
1 files changed, 24 insertions, 9 deletions
diff --git a/nbd/nbd-internal.h b/nbd/nbd-internal.h
index 396ddb4d3e..03549e3f39 100644
--- a/nbd/nbd-internal.h
+++ b/nbd/nbd-internal.h
@@ -77,21 +77,36 @@
#define NBD_ESHUTDOWN 108
/* nbd_read_eof
- * Tries to read @size bytes from @ioc. Returns number of bytes actually read.
- * May return a value >= 0 and < size only on EOF, i.e. when iteratively called
- * qio_channel_readv() returns 0. So, there is no need to call nbd_read_eof
- * iteratively.
+ * Tries to read @size bytes from @ioc.
+ * Returns 1 on success
+ * 0 on eof, when no data was read (errp is not set)
+ * negative errno on failure (errp is set)
*/
-static inline ssize_t nbd_read_eof(QIOChannel *ioc, void *buffer, size_t size,
- Error **errp)
+static inline int nbd_read_eof(QIOChannel *ioc, void *buffer, size_t size,
+ Error **errp)
{
struct iovec iov = { .iov_base = buffer, .iov_len = size };
+ ssize_t ret;
+
/* Sockets are kept in blocking mode in the negotiation phase. After
* that, a non-readable socket simply means that another thread stole
* our request/reply. Synchronization is done with recv_coroutine, so
* that this is coroutine-safe.
*/
- return nbd_rwv(ioc, &iov, 1, size, true, errp);
+
+ assert(size);
+
+ ret = nbd_rwv(ioc, &iov, 1, size, true, errp);
+ if (ret <= 0) {
+ return ret;
+ }
+
+ if (ret != size) {
+ error_setg(errp, "End of file");
+ return -EINVAL;
+ }
+
+ return 1;
}
/* nbd_read
@@ -100,9 +115,9 @@ static inline ssize_t nbd_read_eof(QIOChannel *ioc, void *buffer, size_t size,
static inline int nbd_read(QIOChannel *ioc, void *buffer, size_t size,
Error **errp)
{
- ssize_t ret = nbd_read_eof(ioc, buffer, size, errp);
+ int ret = nbd_read_eof(ioc, buffer, size, errp);
- if (ret >= 0 && ret != size) {
+ if (ret == 0) {
ret = -EINVAL;
error_setg(errp, "End of file");
}