aboutsummaryrefslogtreecommitdiff
path: root/include/block/nbd.h
diff options
context:
space:
mode:
authorVladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>2019-01-28 19:58:30 +0300
committerEric Blake <eblake@redhat.com>2019-02-04 15:11:27 -0600
commite6798f06a67a25def45a6636259de38cc38f1414 (patch)
treeaa6666d07f2722947fea9f44b19aa1ec9d4ca5fa /include/block/nbd.h
parent0ae2d54645eb2888af6dc7f701bc02ca18e4e656 (diff)
downloadqemu-e6798f06a67a25def45a6636259de38cc38f1414.zip
qemu-e6798f06a67a25def45a6636259de38cc38f1414.tar.gz
qemu-e6798f06a67a25def45a6636259de38cc38f1414.tar.bz2
nbd: generalize usage of nbd_read
We generally do very similar things around nbd_read: error_prepend specifying what we have tried to read, and be_to_cpu conversion of integers. So, it seems reasonable to move common things to helper functions, which: 1. simplify code a bit 2. generalize nbd_read error descriptions, all starting with "Failed to read" 3. make it more difficult to forget to convert things from BE Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> Reviewed-by: Eric Blake <eblake@redhat.com> Message-Id: <20190128165830.165170-1-vsementsov@virtuozzo.com> [eblake: rename macro to DEF_NBD_READ_N and formatting tweaks; checkpatch has false positive complaint] Signed-off-by: Eric Blake <eblake@redhat.com>
Diffstat (limited to 'include/block/nbd.h')
-rw-r--r--include/block/nbd.h32
1 files changed, 30 insertions, 2 deletions
diff --git a/include/block/nbd.h b/include/block/nbd.h
index 4faf394..96cfb1d 100644
--- a/include/block/nbd.h
+++ b/include/block/nbd.h
@@ -23,6 +23,7 @@
#include "qapi/qapi-types-block.h"
#include "io/channel-socket.h"
#include "crypto/tlscreds.h"
+#include "qapi/error.h"
/* Handshake phase structs - this struct is passed on the wire */
@@ -336,11 +337,38 @@ void nbd_server_start(SocketAddress *addr, const char *tls_creds,
* Reads @size bytes from @ioc. Returns 0 on success.
*/
static inline int nbd_read(QIOChannel *ioc, void *buffer, size_t size,
- Error **errp)
+ const char *desc, Error **errp)
{
- return qio_channel_read_all(ioc, buffer, size, errp) < 0 ? -EIO : 0;
+ int ret = qio_channel_read_all(ioc, buffer, size, errp) < 0 ? -EIO : 0;
+
+ if (ret < 0) {
+ if (desc) {
+ error_prepend(errp, "Failed to read %s: ", desc);
+ }
+ return -1;
+ }
+
+ return 0;
+}
+
+#define DEF_NBD_READ_N(bits) \
+static inline int nbd_read##bits(QIOChannel *ioc, \
+ uint##bits##_t *val, \
+ const char *desc, Error **errp) \
+{ \
+ if (nbd_read(ioc, val, sizeof(*val), desc, errp) < 0) { \
+ return -1; \
+ } \
+ *val = be##bits##_to_cpu(*val); \
+ return 0; \
}
+DEF_NBD_READ_N(16) /* Defines nbd_read16(). */
+DEF_NBD_READ_N(32) /* Defines nbd_read32(). */
+DEF_NBD_READ_N(64) /* Defines nbd_read64(). */
+
+#undef DEF_NBD_READ_N
+
static inline bool nbd_reply_is_simple(NBDReply *reply)
{
return reply->magic == NBD_SIMPLE_REPLY_MAGIC;