This is unlikely to come up now, but is a necessary prerequisite for
reconnection
behaviour.
Signed-off-by: Nick Thomas <[email protected]>
---
block/nbd.c | 13 +++++++++++--
1 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/block/nbd.c b/block/nbd.c
index 2bce47b..9536408 100644
--- a/block/nbd.c
+++ b/block/nbd.c
@@ -71,6 +71,11 @@ typedef struct BDRVNBDState {
char *host_spec;
} BDRVNBDState;
+static bool nbd_is_connected(BDRVNBDState *s)
+{
+ return s->sock >= 0;
+}
+
static int nbd_config(BDRVNBDState *s, const char *filename, int flags)
{
char *file;
@@ -309,6 +314,7 @@ static void nbd_teardown_connection(BlockDriverState *bs)
qemu_aio_set_fd_handler(s->sock, NULL, NULL, NULL, NULL);
closesocket(s->sock);
+ s->sock = -1;
}
static int nbd_open(BlockDriverState *bs, const char* filename, int flags)
@@ -316,6 +322,8 @@ static int nbd_open(BlockDriverState *bs, const char* filename, int flags)
BDRVNBDState *s = bs->opaque;
int result;
+ s->sock = -1;
+
qemu_co_mutex_init(&s->send_mutex);
qemu_co_mutex_init(&s->free_sema);
@@ -431,7 +439,7 @@ static int nbd_co_flush(BlockDriverState *bs)
struct nbd_reply reply;
ssize_t ret;
- if (!(s->nbdflags & NBD_FLAG_SEND_FLUSH)) {
+ if (!nbd_is_connected(s) || !(s->nbdflags & NBD_FLAG_SEND_FLUSH)) {
return 0;
}
@@ -462,7 +470,7 @@ static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num,
struct nbd_reply reply;
ssize_t ret;
- if (!(s->nbdflags & NBD_FLAG_SEND_TRIM)) {
+ if (!nbd_is_connected(s) || !(s->nbdflags & NBD_FLAG_SEND_TRIM)) {
return 0;
}
request.type = NBD_CMD_TRIM;
@@ -515,3 +523,4 @@ static void bdrv_nbd_init(void)
}
block_init(bdrv_nbd_init);
+