[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH COLO v4 08/15] NBD client: implement block driver in
From: |
Wen Congyang |
Subject: |
[Qemu-devel] [PATCH COLO v4 08/15] NBD client: implement block driver interfaces to connect/disconnect NBD server |
Date: |
Fri, 8 May 2015 17:16:00 +0800 |
Signed-off-by: Wen Congyang <address@hidden>
Signed-off-by: zhanghailiang <address@hidden>
Signed-off-by: Gonglei <address@hidden>
---
block/nbd.c | 65 ++++++++++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 47 insertions(+), 18 deletions(-)
diff --git a/block/nbd.c b/block/nbd.c
index 2176186..0624232 100644
--- a/block/nbd.c
+++ b/block/nbd.c
@@ -44,6 +44,8 @@
typedef struct BDRVNBDState {
NbdClientSession client;
QemuOpts *socket_opts;
+ char *export;
+ bool connected;
} BDRVNBDState;
static int nbd_parse_uri(const char *filename, QDict *options)
@@ -254,34 +256,55 @@ static int nbd_establish_connection(BlockDriverState *bs,
Error **errp)
return sock;
}
-static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
- Error **errp)
+static void nbd_connect_server(BlockDriverState *bs, Error **errp)
{
BDRVNBDState *s = bs->opaque;
- char *export = NULL;
- int result, sock;
- Error *local_err = NULL;
-
- /* Pop the config into our state object. Exit if invalid. */
- nbd_config(s, options, &export, &local_err);
- if (local_err) {
- error_propagate(errp, local_err);
- return -EINVAL;
- }
+ int sock;
/* establish TCP connection, return error if it fails
* TODO: Configurable retry-until-timeout behaviour.
*/
sock = nbd_establish_connection(bs, errp);
if (sock < 0) {
- g_free(export);
- return sock;
+ return;
}
/* NBD handshake */
- result = nbd_client_init(bs, sock, export, errp);
- g_free(export);
- return result;
+ nbd_client_init(bs, sock, s->export, errp);
+
+ s->connected = true;
+}
+
+static void nbd_disconnect_server(BlockDriverState *bs)
+{
+ BDRVNBDState *s = bs->opaque;
+
+ if (s->connected) {
+ nbd_client_close(bs);
+ s->connected = false;
+ }
+}
+
+static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
+ Error **errp)
+{
+ BDRVNBDState *s = bs->opaque;
+ Error *local_err = NULL;
+
+ /* Pop the config into our state object. Exit if invalid. */
+ nbd_config(s, options, &s->export, &local_err);
+ if (local_err) {
+ error_propagate(errp, local_err);
+ return -EINVAL;
+ }
+
+ nbd_connect_server(bs, &local_err);
+ if (local_err) {
+ error_propagate(errp, local_err);
+ return -EINVAL;
+ }
+
+ return 0;
}
static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num,
@@ -318,7 +341,7 @@ static void nbd_close(BlockDriverState *bs)
BDRVNBDState *s = bs->opaque;
qemu_opts_del(s->socket_opts);
- nbd_client_close(bs);
+ nbd_disconnect_server(bs);
}
static int64_t nbd_getlength(BlockDriverState *bs)
@@ -400,6 +423,8 @@ static BlockDriver bdrv_nbd = {
.bdrv_detach_aio_context = nbd_detach_aio_context,
.bdrv_attach_aio_context = nbd_attach_aio_context,
.bdrv_refresh_filename = nbd_refresh_filename,
+ .bdrv_connect = nbd_connect_server,
+ .bdrv_disconnect = nbd_disconnect_server,
};
static BlockDriver bdrv_nbd_tcp = {
@@ -418,6 +443,8 @@ static BlockDriver bdrv_nbd_tcp = {
.bdrv_detach_aio_context = nbd_detach_aio_context,
.bdrv_attach_aio_context = nbd_attach_aio_context,
.bdrv_refresh_filename = nbd_refresh_filename,
+ .bdrv_connect = nbd_connect_server,
+ .bdrv_disconnect = nbd_disconnect_server,
};
static BlockDriver bdrv_nbd_unix = {
@@ -436,6 +463,8 @@ static BlockDriver bdrv_nbd_unix = {
.bdrv_detach_aio_context = nbd_detach_aio_context,
.bdrv_attach_aio_context = nbd_attach_aio_context,
.bdrv_refresh_filename = nbd_refresh_filename,
+ .bdrv_connect = nbd_connect_server,
+ .bdrv_disconnect = nbd_disconnect_server,
};
static void bdrv_nbd_init(void)
--
2.1.0
- Re: [Qemu-devel] [PATCH COLO v4 02/15] allow writing to the backing file, (continued)
[Qemu-devel] [PATCH COLO v4 06/15] Don't allow a disk use backing reference target, Wen Congyang, 2015/05/08
[Qemu-devel] [PATCH COLO v4 04/15] block: Parse "backing_reference" option to reference existing BDS, Wen Congyang, 2015/05/08
[Qemu-devel] [PATCH COLO v4 10/15] NBD client: connect to nbd server later, Wen Congyang, 2015/05/08
[Qemu-devel] [PATCH COLO v4 09/15] Introduce a new -drive option to control whether to connect to remote target, Wen Congyang, 2015/05/08
[Qemu-devel] [PATCH COLO v4 08/15] NBD client: implement block driver interfaces to connect/disconnect NBD server,
Wen Congyang <=
[Qemu-devel] [PATCH COLO v4 12/15] skip nbd_target when starting block replication, Wen Congyang, 2015/05/08
[Qemu-devel] [PATCH COLO v4 11/15] Add new block driver interfaces to control block replication, Wen Congyang, 2015/05/08
[Qemu-devel] [PATCH COLO v4 13/15] quorum: implement block driver interfaces for block replication, Wen Congyang, 2015/05/08
[Qemu-devel] [PATCH COLO v4 14/15] quorum: allow ignoring child errors, Wen Congyang, 2015/05/08
[Qemu-devel] [PATCH COLO v4 15/15] Implement new driver for block replication, Wen Congyang, 2015/05/08
Re: [Qemu-devel] [PATCH COLO v4 00/15] Block replication for continuous checkpoints, Dr. David Alan Gilbert, 2015/05/20