sheepdog: reconnect server after connection failure
Subsystems (2)
cover sheepdog: reconnect server after connection failure
Currently, if a sheepdog server exits, all the connecting VMs need to be restarted. This series implements a feature to reconnect the server, and enables us to do online sheepdog upgrade and avoid restarting VMs when sheepdog servers crash unexpectedly.
MORITA Kazutaka (11):
ignore SIGPIPE in qemu-img and qemu-io
iov: handle eof in iov_send_recv
qemu-sockets: make wait_for_connect be invoked in qemu_aio_wait
sheepdog: make connect nonblocking
sheepdog: check return values of qemu_co_recv/send correctly
sheepdog: handle vdi objects in resend_aio_req
sheepdog: reload inode outside of resend_aioreq
coroutine: add co_aio_sleep_ns() to allow sleep in block drivers
sheepdog: try to reconnect to sheepdog after network error
sheepdog: make add_aio_request and send_aioreq void functions
sheepdog: cancel aio requests if possible
Makefile | 4 +- block/sheepdog.c | 314 ++++++++++++++++++++++++++++++++-------------- include/block/coroutine.h | 8 ++ qemu-coroutine-sleep.c | 47 +++++++ qemu-img.c | 4 + qemu-io.c | 4 + util/iov.c | 6 + util/qemu-sockets.c | 15 ++- 8 files changed, 303 insertions(+), 99 deletions(-)
Signature · 1 line
1.8.1.3.566.gaa39828
PATCH 1/11 ignore SIGPIPE in qemu-img and qemu-io
This prevents the tools from being stopped when they write data to a closed connection in the other side.
--- qemu-img.c | 4 ++++ qemu-io.c | 4 ++++ 2 files changed, 8 insertions(+)
qemu-img.c +4 -0
@@ -2319,6 +2319,10 @@ int main(int argc, char **argv)const img_cmd_t *cmd;const char *cmdname; +#ifdef CONFIG_POSIX +signal (SIGPIPE ,SIG_IGN ); +#endif +error_set_progname (argv[0 ]);qemu_init_main_loop ();
qemu-io.c +4 -0
@@ -335,6 +335,10 @@ int main(int argc, char **argv)int opt_index =0 ;int flags =BDRV_O_UNMAP ; +#ifdef CONFIG_POSIX +signal (SIGPIPE ,SIG_IGN ); +#endif + progname =basename (argv[0 ]);while ((c =getopt_long (argc, argv, sopt, lopt, &opt_index)) !=-1 ) {
Signature · 1 line
1.8.1.3.566.gaa39828
Re: [Qemu-devel] [PATCH for-1.6 01/11] ignore SIGPIPE in qemu-img and qemu-io
Il 23/07/2013 10:30, MORITA Kazutaka ha scritto:
Quoted 30 lines
This prevents the tools from being stopped when they write data to a closed connection in the other side.
Signed-off-by MORITA Kazutaka <[email protected]>--- qemu-img.c | 4 ++++ qemu-io.c | 4 ++++ 2 files changed, 8 insertions(+)qemu-img.c +4
-0@@ -2319,6 +2319,10 @@ int main(int argc, char **argv)const img_cmd_t *cmd;const char *cmdname; +#ifdef CONFIG_POSIX +signal (SIGPIPE ,SIG_IGN ); +#endif +error_set_progname (argv[0 ]);qemu_init_main_loop ();qemu-io.c +0
-0
qemu-io.c int main(int argc, char **argv) · line 344
Quoted 10 lines
qemu-io.c +4
-0@@ -335,6 +335,10 @@ int main(int argc, char **argv)int opt_index =0 ;int flags =BDRV_O_UNMAP ; +#ifdef CONFIG_POSIX +signal (SIGPIPE ,SIG_IGN ); +#endif + progname =basename (argv[0 ]);while ((c =getopt_long (argc, argv, sopt, lopt, &opt_index)) !=-1 ) {
and adding qemu-stable for this one.
Re: [Qemu-devel] [PATCH for-1.6 01/11] ignore SIGPIPE in qemu-img and qemu-io
Quoted 39 lines · Paolo Bonzini
Il 23/07/2013 10:30, MORITA Kazutaka ha scritto:
Quoted 38 lines
This prevents the tools from being stopped when they write data to a closed connection in the other side.
Signed-off-by MORITA Kazutaka <[email protected]>--- qemu-img.c | 4 ++++ qemu-io.c | 4 ++++ 2 files changed, 8 insertions(+)qemu-img.c +4
-0@@ -2319,6 +2319,10 @@ int main(int argc, char **argv)const img_cmd_t *cmd;const char *cmdname; +#ifdef CONFIG_POSIX +signal (SIGPIPE ,SIG_IGN ); +#endif +error_set_progname (argv[0 ]);qemu_init_main_loop ();qemu-io.c +4
-0@@ -335,6 +335,10 @@ int main(int argc, char **argv)int opt_index =0 ;int flags =BDRV_O_UNMAP ; +#ifdef CONFIG_POSIX +signal (SIGPIPE ,SIG_IGN ); +#endif + progname =basename (argv[0 ]);
Quoted 7 lines
qemu-io.c int main(int argc, char **argv) · line 344
Quoted 2 lines
qemu-io.c +0
-0while ((c =getopt_long (argc, argv, sopt, lopt, &opt_index)) !=-1 ) {Reviewed-by Paolo Bonzini <[email protected]>and adding qemu-stable for this one.
Nudge so this isn't forgotten about since it hasn't hit master yet.
Signature · 1 line
Doug Goldstein
Kevin Wolf
AppliedRe: [Qemu-devel] [PATCH for-1.6 01/11] ignore SIGPIPE in qemu-img and qemu-io
Am 03.08.2013 um 05:52 hat Doug Goldstein geschrieben:
Quoted 45 lines · Paolo Bonzini
Quoted 44 lines
Il 23/07/2013 10:30, MORITA Kazutaka ha scritto:
Quoted 30 lines
This prevents the tools from being stopped when they write data to a closed connection in the other side.
Signed-off-by MORITA Kazutaka <[email protected]>--- qemu-img.c | 4 ++++ qemu-io.c | 4 ++++ 2 files changed, 8 insertions(+)qemu-img.c +4
-0@@ -2319,6 +2319,10 @@ int main(int argc, char **argv)const img_cmd_t *cmd;const char *cmdname; +#ifdef CONFIG_POSIX +signal (SIGPIPE ,SIG_IGN ); +#endif +error_set_progname (argv[0 ]);qemu_init_main_loop ();qemu-io.c +0
-0qemu-io.c int main(int argc, char **argv) · line 344
Quoted 10 lines
qemu-io.c +4
-0@@ -335,6 +335,10 @@ int main(int argc, char **argv)int opt_index =0 ;int flags =BDRV_O_UNMAP ; +#ifdef CONFIG_POSIX +signal (SIGPIPE ,SIG_IGN ); +#endif + progname =basename (argv[0 ]);while ((c =getopt_long (argc, argv, sopt, lopt, &opt_index)) !=-1 ) {Reviewed-by Paolo Bonzini <[email protected]>
Quoted 4 lines
Quoted 2 lines
and adding qemu-stable for this one.
Nudge so this isn't forgotten about since it hasn't hit master yet.
Thanks, applied to the block branch.
Kevin
PATCH 2/11 iov: handle EOF in iov_send_recv
In reply to MORITA KazutakaWithout this patch, iov_send_recv() never returns when do_send_recv() returns zero.
--- util/iov.c | 6 ++++++ 1 file changed, 6 insertions(+)
util/iov.c +6 -0
@@ -202,6 +202,12 @@ ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,return -1 ; } +if (ret ==0 && !do_send) { +/* recv returns 0 when the peer has performed an orderly +* shutdown. */ +break ; + } +/* Prepare for the next iteration */ offset += ret; total += ret;
Signature · 1 line
1.8.1.3.566.gaa39828
Re: [Qemu-devel] [PATCH 02/11] iov: handle EOF in iov_send_recv
Il 23/07/2013 10:30, MORITA Kazutaka ha scritto:
Quoted 16 lines
Without this patch,
iov_send_recv()never returns whendo_send_recv()returns zero.Signed-off-by MORITA Kazutaka <[email protected]>--- util/iov.c | 6 ++++++ 1 file changed, 6 insertions(+)util/iov.c +0
-0@@ -202,6 +202,12 @@ ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,return -1 ; }
util/iov.c ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, · line 213
Quoted 10 lines
util/iov.c +6
-0+if (ret ==0 && !do_send) { +/* recv returns 0 when the peer has performed an orderly +* shutdown. */ +break ; + } +/* Prepare for the next iteration */ offset += ret; total += ret;
... and should also be in 1.5.2.
Paolo
Re: [Qemu-devel] [PATCH 02/11] iov: handle EOF in iov_send_recv
Quoted 30 lines · Paolo Bonzini
Il 23/07/2013 10:30, MORITA Kazutaka ha scritto:
Quoted 16 lines
Without this patch,
iov_send_recv()never returns whendo_send_recv()returns zero.Signed-off-by MORITA Kazutaka <[email protected]>--- util/iov.c | 6 ++++++ 1 file changed, 6 insertions(+)util/iov.c +0
-0@@ -202,6 +202,12 @@ ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,return -1 ; }util/iov.c ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, · line 213
Quoted 10 lines
util/iov.c +6
-0+if (ret ==0 && !do_send) { +/* recv returns 0 when the peer has performed an orderly +* shutdown. */ +break ; + } +/* Prepare for the next iteration */ offset += ret; total += ret;Reviewed-by Paolo Bonzini <[email protected]>
Quoted 4 lines
... and should also be in 1.5.2.
Paolo
Nudge so this doesn't get forgotten about. It hasn't hit master yet.
Signature · 1 line
Doug Goldstein
Kevin Wolf
AppliedRe: [Qemu-devel] [PATCH 02/11] iov: handle EOF in iov_send_recv
Am 03.08.2013 um 05:48 hat Doug Goldstein geschrieben:
Quoted 31 lines · Paolo Bonzini
Quoted 30 lines
Il 23/07/2013 10:30, MORITA Kazutaka ha scritto:
Quoted 16 lines
Without this patch,
iov_send_recv()never returns whendo_send_recv()returns zero.Signed-off-by MORITA Kazutaka <[email protected]>--- util/iov.c | 6 ++++++ 1 file changed, 6 insertions(+)util/iov.c +0
-0@@ -202,6 +202,12 @@ ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,return -1 ; }util/iov.c ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, · line 213
Quoted 10 lines
util/iov.c +6
-0+if (ret ==0 && !do_send) { +/* recv returns 0 when the peer has performed an orderly +* shutdown. */ +break ; + } +/* Prepare for the next iteration */ offset += ret; total += ret;Reviewed-by Paolo Bonzini <[email protected]>
Quoted 6 lines
Quoted 4 lines
... and should also be in 1.5.2.
Paolo
Nudge so this doesn't get forgotten about. It hasn't hit master yet.
Thanks, applied to the block branch.
Kevin
PATCH 3/11 qemu-sockets: make wait_for_connect be invoked in qemu_aio_wait
In reply to MORITA KazutakaThis allows us to use inet_nonblocking_connect() and unix_nonblocking_connect() in block drivers.
qemu-ga needs to link block-obj to resolve dependencies of qemu_aio_set_fd_handler().
--- Makefile | 4 ++-- util/qemu-sockets.c | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-)
Makefile +2 -2
@@ -197,7 +197,7 @@ fsdev/virtfs-proxy-helper$(EXESUF): LIBS += -lcap qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx $(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@," GEN $@") -qemu-ga$(EXESUF): LIBS = $(LIBS_QGA) +qemu-ga$(EXESUF): LIBS = $(LIBS_QGA) $(LIBS_TOOLS) qemu-ga$(EXESUF): QEMU_CFLAGS += -I qga/qapi-generated gen-out-type = $(subst .,-,$(suffix $@))
@@ -227,7 +227,7 @@ $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py) QGALIB_GEN=$(addprefix qga/qapi-generated/, qga-qapi-types.h qga-qapi-visit.h qga-qmp-commands.h) $(qga-obj-y) qemu-ga.o: $(QGALIB_GEN) -qemu-ga$(EXESUF): $(qga-obj-y) libqemuutil.a libqemustub.a +qemu-ga$(EXESUF): $(qga-obj-y) $(block-obj-y) libqemuutil.a libqemustub.a $(call LINK, $^) clean:
util/qemu-sockets.c +10 -5
@@ -218,6 +218,11 @@ typedef struct ConnectState {static int inet_connect_addr (struct addrinfo *addr,bool *in_progress,ConnectState *connect_state,Error **errp); +static int return_true (void *opaque) +{ +return 1 ; +} +static void wait_for_connect (void *opaque) {ConnectState *s = opaque;
@@ -225,7 +230,7 @@ static void wait_for_connect(void *opaque)socklen_t valsize =sizeof (val);bool in_progress; -qemu_set_fd_handler2 (s->fd,NULL ,NULL ,NULL ,NULL ); +qemu_aio_set_fd_handler (s->fd,NULL ,NULL ,NULL ,NULL ); do { rc = qemu_getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &val, &valsize);
@@ -288,8 +293,8 @@ static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,if (connect_state !=NULL &&QEMU_SOCKET_RC_INPROGRESS (rc)) { connect_state->fd = sock; -qemu_set_fd_handler2 (sock,NULL ,NULL , wait_for_connect, - connect_state); +qemu_aio_set_fd_handler (sock,NULL , wait_for_connect, return_true, + connect_state); *in_progress = true; }else if (rc <0 ) {error_set_errno (errp, errno,QERR_SOCKET_CONNECT_FAILED );
@@ -749,8 +754,8 @@ int unix_connect_opts(QemuOpts *opts, Error **errp,if (connect_state !=NULL &&QEMU_SOCKET_RC_INPROGRESS (rc)) { connect_state->fd = sock; -qemu_set_fd_handler2 (sock,NULL ,NULL , wait_for_connect, - connect_state); +qemu_aio_set_fd_handler (sock,NULL , wait_for_connect, return_true, + connect_state);return sock; }else if (rc >=0 ) {/* non blocking socket immediate success, call callback */
Signature · 1 line
1.8.1.3.566.gaa39828
Re: [Qemu-devel] [PATCH 03/11] qemu-sockets: make wait_for_connect be invoked in qemu_aio_wait
Il 23/07/2013 10:30, MORITA Kazutaka ha scritto:
Quoted 7 lines
This allows us to use
inet_nonblocking_connect()andunix_nonblocking_connect()in block drivers.qemu-ga needs to link block-obj to resolve dependencies of
qemu_aio_set_fd_handler().Signed-off-by MORITA Kazutaka <[email protected]>
I'm not sure this is safe. You could have e.g. migration start during qemu_aio_wait().
Paolo
Quoted 75 lines
--- Makefile | 4 ++-- util/qemu-sockets.c | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-)Makefile +2
-2@@ -197,7 +197,7 @@ fsdev/virtfs-proxy-helper$(EXESUF): LIBS += -lcap qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx $(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@," GEN $@") -qemu-ga$(EXESUF): LIBS = $(LIBS_QGA) +qemu-ga$(EXESUF): LIBS = $(LIBS_QGA) $(LIBS_TOOLS) qemu-ga$(EXESUF): QEMU_CFLAGS += -I qga/qapi-generated gen-out-type = $(subst .,-,$(suffix $@))@@ -227,7 +227,7 @@ $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py) QGALIB_GEN=$(addprefix qga/qapi-generated/, qga-qapi-types.h qga-qapi-visit.h qga-qmp-commands.h) $(qga-obj-y) qemu-ga.o: $(QGALIB_GEN) -qemu-ga$(EXESUF): $(qga-obj-y) libqemuutil.a libqemustub.a +qemu-ga$(EXESUF): $(qga-obj-y) $(block-obj-y) libqemuutil.a libqemustub.a $(call LINK, $^) clean:util/qemu-sockets.c +10
-5@@ -218,6 +218,11 @@ typedef struct ConnectState {static int inet_connect_addr (struct addrinfo *addr,bool *in_progress,ConnectState *connect_state,Error **errp); +static int return_true (void *opaque) +{ +return 1 ; +} +static void wait_for_connect (void *opaque) {ConnectState *s = opaque;@@ -225,7 +230,7 @@ static void wait_for_connect(void *opaque)socklen_t valsize =sizeof (val);bool in_progress; -qemu_set_fd_handler2 (s->fd,NULL ,NULL ,NULL ,NULL ); +qemu_aio_set_fd_handler (s->fd,NULL ,NULL ,NULL ,NULL ); do { rc = qemu_getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &val, &valsize);@@ -288,8 +293,8 @@ static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,if (connect_state !=NULL &&QEMU_SOCKET_RC_INPROGRESS (rc)) { connect_state->fd = sock; -qemu_set_fd_handler2 (sock,NULL ,NULL , wait_for_connect, - connect_state); +qemu_aio_set_fd_handler (sock,NULL , wait_for_connect, return_true, + connect_state); *in_progress = true; }else if (rc <0 ) {error_set_errno (errp, errno,QERR_SOCKET_CONNECT_FAILED );@@ -749,8 +754,8 @@ int unix_connect_opts(QemuOpts *opts, Error **errp,if (connect_state !=NULL &&QEMU_SOCKET_RC_INPROGRESS (rc)) { connect_state->fd = sock; -qemu_set_fd_handler2 (sock,NULL ,NULL , wait_for_connect, - connect_state); +qemu_aio_set_fd_handler (sock,NULL , wait_for_connect, return_true, + connect_state);return sock; }else if (rc >=0 ) {/* non blocking socket immediate success, call callback */
Re: [Qemu-devel] [sheepdog] [PATCH 03/11] qemu-sockets: make wait_for_connect be invoked in qemu_aio_wait
At Tue, 23 Jul 2013 13:36:08 +0200,
Quoted 5 lines · Paolo Bonzini
Il 23/07/2013 10:30, MORITA Kazutaka ha scritto:
Quoted 3 lines
This allows us to use
inet_nonblocking_connect()andunix_nonblocking_connect()in block drivers.
Quoted 7 lines
Quoted 4 lines
qemu-ga needs to link block-obj to resolve dependencies of
qemu_aio_set_fd_handler().Signed-off-by MORITA Kazutaka <[email protected]>I'm not sure this is safe. You could have e.g. migration start during
qemu_aio_wait().
I thought that it is safe. Qemu creates another thread for migration and it can be started at any time, either way. However, so as not to hurt the existing codes, it might be better to create another nonblocking connect for qemu_aio_wait().
I think of dropping this patch from this series and will leave it for another day. Usually, sheepdog users prepare a local sheepdog daemon to be connected to, and connect() is unlikely to sleep for a long time. Using a blocking connect wouldn't be a big problem.
Thanks,
Kazutaka
PATCH 4/11 sheepdog: make connect nonblocking
In reply to MORITA KazutakaThis uses nonblocking connect functions to connect to the sheepdog server. The connect operation is done in a coroutine function and it will be yielded until the created socked is ready for IO.
--- block/sheepdog.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 7 deletions(-)
block/sheepdog.c +63 -7
@@ -455,18 +455,51 @@ static SheepdogAIOCB *sd_aio_setup(BlockDriverState *bs, QEMUIOVector *qiov,return acb; } -static int connect_to_sdog (BDRVSheepdogState *s) -{ +typedef struct SheepdogConnectCo { +BDRVSheepdogState *bs; +Coroutine *co;int fd; +bool finished; +}SheepdogConnectCo ; + +static void sd_connect_completed (int fd,void *opaque) +{ +SheepdogConnectCo *scco = opaque; + +if (fd <0 ) { +int val, rc; +socklen_t valsize =sizeof (val); + +do { + rc =qemu_getsockopt (scco->fd,SOL_SOCKET ,SO_ERROR , &val, + &valsize); + }while (rc ==-1 &&socket_error () ==EINTR ); + + scco->fd = rc <0 ? -errno : -val; + } + + scco->finished = true; + +if (scco->co !=NULL ) { +qemu_coroutine_enter (scco->co,NULL ); + } +} + +static coroutine_fn void co_connect_to_sdog(void *opaque) +{ +SheepdogConnectCo *scco = opaque; +BDRVSheepdogState *s = scco->bs;Error *err =NULL ;if (s->is_unix) { - fd =unix_connect (s->host_spec, &err); + scco->fd =unix_nonblocking_connect (s->host_spec, sd_connect_completed, + opaque, &err); }else { - fd =inet_connect (s->host_spec, &err); + scco->fd =inet_nonblocking_connect (s->host_spec, sd_connect_completed, + opaque, &err);if (err ==NULL ) { -int ret =socket_set_nodelay (fd); +int ret =socket_set_nodelay (scco->fd);if (ret <0 ) {error_report ("%s" ,strerror (errno)); }
@@ -476,11 +509,34 @@ static int connect_to_sdog(BDRVSheepdogState *s)if (err !=NULL ) {qerror_report_err (err);error_free (err); + } + +if (!scco->finished) { +/* wait for connect to finish */ + scco->co =qemu_coroutine_self (); +qemu_coroutine_yield (); + } +} + +static int connect_to_sdog (BDRVSheepdogState *s) +{ +Coroutine *co; +SheepdogConnectCo scco = { + .bs = s, + .finished = false, + }; + +if (qemu_in_coroutine ()) { +co_connect_to_sdog (&scco); }else { -qemu_set_nonblock (fd); + co =qemu_coroutine_create (co_connect_to_sdog); +qemu_coroutine_enter (co, &scco); +while (!scco.finished) { +qemu_aio_wait (); + } } -return fd; +return scco.fd; } static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data,
Signature · 1 line
1.8.1.3.566.gaa39828
PATCH 5/11 sheepdog: check return values of qemu_co_recv/send correctly
In reply to MORITA Kazutakaqemu_co_recv/send return shorter length on error.
--- block/sheepdog.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
block/sheepdog.c +4 -4
@@ -727,7 +727,7 @@ static void coroutine_fn aio_read_response(void *opaque)/* read a header */ ret =qemu_co_recv (fd, &rsp,sizeof (rsp)); -if (ret <0 ) { +if (ret <sizeof (rsp)) {error_report ("failed to get the header, %s" ,strerror (errno)); goto out; }
@@ -778,7 +778,7 @@ static void coroutine_fn aio_read_response(void *opaque)case AIOCB_READ_UDATA : ret =qemu_co_recvv (fd, acb->qiov->iov, acb->qiov->niov, aio_req->iov_offset, rsp.data_length); -if (ret <0 ) { +if (ret < rsp.data_length) {error_report ("failed to get the data, %s" ,strerror (errno)); goto out; }
@@ -1131,7 +1131,7 @@ static int coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req,/* send a header */ ret =qemu_co_send (s->fd, &hdr,sizeof (hdr)); -if (ret <0 ) { +if (ret <sizeof (hdr)) {qemu_co_mutex_unlock (&s->lock);error_report ("failed to send a req, %s" ,strerror (errno));return -errno;
@@ -1139,7 +1139,7 @@ static int coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req,if (wlen) { ret =qemu_co_sendv (s->fd, iov, niov, aio_req->iov_offset, wlen); -if (ret <0 ) { +if (ret < wlen) {qemu_co_mutex_unlock (&s->lock);error_report ("failed to send a data, %s" ,strerror (errno));return -errno;
Signature · 1 line
1.8.1.3.566.gaa39828
PATCH 6/11 sheepdog: handle vdi objects in resend_aio_req
In reply to MORITA KazutakaThe current resend_aio_req() doesn't work when the request is against vdi objects. This fixes the problem.
--- block/sheepdog.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-)
block/sheepdog.c +16 -5
@@ -1265,11 +1265,15 @@ static int coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req)return ret; } - aio_req->oid =vid_to_data_oid (s->inode.vdi_id, -data_oid_to_idx (aio_req->oid)); +if (is_data_obj (aio_req->oid)) { + aio_req->oid =vid_to_data_oid (s->inode.vdi_id, +data_oid_to_idx (aio_req->oid)); + }else { + aio_req->oid =vid_to_vdi_oid (s->inode.vdi_id); + }/* check whether this request becomes a CoW one */ -if (acb->aiocb_type ==AIOCB_WRITE_UDATA ) { +if (acb->aiocb_type ==AIOCB_WRITE_UDATA &&is_data_obj (aio_req->oid)) {int idx =data_oid_to_idx (aio_req->oid);AIOReq *areq;
@@ -1297,8 +1301,15 @@ static int coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req) create = true; } out: -return add_aio_request (s, aio_req, acb->qiov->iov, acb->qiov->niov, - create, acb->aiocb_type); +if (is_data_obj (aio_req->oid)) { +return add_aio_request (s, aio_req, acb->qiov->iov, acb->qiov->niov, + create, acb->aiocb_type); + }else { +struct iovec iov; + iov.iov_base = &s->inode; + iov.iov_len =sizeof (s->inode); +return add_aio_request (s, aio_req, &iov,1 , false,AIOCB_WRITE_UDATA ); + } }/* TODO Convert to fine grained options */
Signature · 1 line
1.8.1.3.566.gaa39828
PATCH 7/11 sheepdog: reload inode outside of resend_aioreq
In reply to MORITA KazutakaThis prepares for using resend_aioreq() after reconnecting to the sheepdog server.
--- block/sheepdog.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-)
block/sheepdog.c +19 -14
@@ -222,6 +222,11 @@ static inline uint64_t data_oid_to_idx(uint64_t oid)return oid & (MAX_DATA_OBJS -1 ); } +static inline uint32_t oid_to_vid (uint64_t oid) +{ +return (oid & ~VDI_BIT ) >>VDI_SPACE_SHIFT ; +} +static inline uint64_t vid_to_vdi_oid (uint32_t vid) {return VDI_BIT | ((uint64_t )vid <<VDI_SPACE_SHIFT );
@@ -663,7 +668,7 @@ static int coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req, struct iovec *iov, int niov, bool create, enum AIOCBState aiocb_type); static int coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req); - +static int reload_inode (BDRVSheepdogState *s,uint32_t snapid,const char *tag);static AIOReq *find_pending_req (BDRVSheepdogState *s,uint64_t oid) {
@@ -811,6 +816,19 @@ static void coroutine_fn aio_read_response(void *opaque)case SD_RES_SUCCESS :break ;case SD_RES_READONLY : +if (s->inode.vdi_id ==oid_to_vid (aio_req->oid)) { + ret =reload_inode (s,0 ,"" ); +if (ret <0 ) { + goto out; + } + } + +if (is_data_obj (aio_req->oid)) { + aio_req->oid =vid_to_data_oid (s->inode.vdi_id, +data_oid_to_idx (aio_req->oid)); + }else { + aio_req->oid =vid_to_vdi_oid (s->inode.vdi_id); + } ret =resend_aioreq (s, aio_req);if (ret ==SD_RES_SUCCESS ) { goto out;
@@ -1258,19 +1276,6 @@ static int coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req) {SheepdogAIOCB *acb = aio_req->aiocb;bool create = false; -int ret; - - ret =reload_inode (s,0 ,"" ); -if (ret <0 ) { -return ret; - } - -if (is_data_obj (aio_req->oid)) { - aio_req->oid =vid_to_data_oid (s->inode.vdi_id, -data_oid_to_idx (aio_req->oid)); - }else { - aio_req->oid =vid_to_vdi_oid (s->inode.vdi_id); - }/* check whether this request becomes a CoW one */ if (acb->aiocb_type ==AIOCB_WRITE_UDATA &&is_data_obj (aio_req->oid)) {
Signature · 1 line
1.8.1.3.566.gaa39828
PATCH 8/11 coroutine: add co_aio_sleep_ns() to allow sleep in block drivers
In reply to MORITA KazutakaThis helper function behaves similarly to co_sleep_ns(), but the sleeping coroutine will be resumed when using qemu_aio_wait().
--- include/block/coroutine.h | 8 ++++++++ qemu-coroutine-sleep.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+)
include/block/coroutine.h +8 -0
@@ -210,6 +210,14 @@ void qemu_co_rwlock_unlock(CoRwlock *lock); void coroutine_fn co_sleep_ns(QEMUClock *clock, int64_t ns);/** +* Yield the coroutine for a given duration +* +* Behaves similarly to co_sleep_ns(), but the sleeping coroutine will be +* resumed when using qemu_aio_wait(). +*/ +void coroutine_fn co_aio_sleep_ns(int64_t ns); + +/** * Yield until a file descriptor becomes readable * * Note that this function clobbers the handlers for the file descriptor.
qemu-coroutine-sleep.c +47 -0
56 lines not on this page · raw message
Signature · 1 line
1.8.1.3.566.gaa39828
PATCH 9/11 sheepdog: try to reconnect to sheepdog after network error
In reply to MORITA KazutakaThis introduces a failed request queue and links all the inflight requests to the list after network error happens. After QEMU reconnects to the sheepdog server successfully, the sheepdog block driver will retry all the requests in the failed queue.
--- block/sheepdog.c | 72 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 15 deletions(-)
block/sheepdog.c +57 -15
@@ -318,8 +318,11 @@ typedef struct BDRVSheepdogState {Coroutine *co_recv;uint32_t aioreq_seq_num; + +/* Every aio request must be linked to either of these queues. */ QLIST_HEAD(inflight_aio_head, AIOReq) inflight_aio_head; QLIST_HEAD(pending_aio_head, AIOReq) pending_aio_head; + QLIST_HEAD(failed_aio_head, AIOReq) failed_aio_head; } BDRVSheepdogState;static const char *sd_strerror (int err)
@@ -669,6 +672,8 @@ static int coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req, enum AIOCBState aiocb_type); static int coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req);static int reload_inode (BDRVSheepdogState *s,uint32_t snapid,const char *tag); +static int get_sheep_fd (BDRVSheepdogState *s); +static void co_write_request (void *opaque);static AIOReq *find_pending_req (BDRVSheepdogState *s,uint64_t oid) {
@@ -710,6 +715,44 @@ static void coroutine_fn send_pending_req(BDRVSheepdogState *s, uint64_t oid) } } +static coroutine_fn void reconnect_to_sdog(void *opaque) +{ +BDRVSheepdogState *s = opaque; +AIOReq *aio_req, *next; + +qemu_aio_set_fd_handler (s->fd,NULL ,NULL ,NULL ,NULL ); +close (s->fd); + s->fd =-1 ; + +/* Wait for outstanding write requests to be completed. */ +while (s->co_send !=NULL ) { +co_write_request (opaque); + } + +/* Move all the inflight requests to the failed queue. */ + QLIST_FOREACH_SAFE(aio_req, &s->inflight_aio_head, aio_siblings, next) { +QLIST_REMOVE (aio_req, aio_siblings); +QLIST_INSERT_HEAD (&s->failed_aio_head, aio_req, aio_siblings); + } + +/* Try to reconnect the sheepdog server every one second. */ +while (s->fd <0 ) { + s->fd =get_sheep_fd (s); +if (s->fd <0 ) { +dprintf ("Wait for connection to be established\n" ); +co_aio_sleep_ns (1000000000ULL ); + } + }; + +/* Resend all the failed aio requests. */ +while (!QLIST_EMPTY (&s->failed_aio_head)) { + aio_req =QLIST_FIRST (&s->failed_aio_head); +QLIST_REMOVE (aio_req, aio_siblings); +QLIST_INSERT_HEAD (&s->inflight_aio_head, aio_req, aio_siblings); +resend_aioreq (s, aio_req); + } +} +/* * Receive responses of the I/O requests. *
@@ -726,15 +769,11 @@ static void coroutine_fn aio_read_response(void *opaque)SheepdogAIOCB *acb;uint64_t idx; -if (QLIST_EMPTY (&s->inflight_aio_head)) { - goto out; - } -/* read a header */ ret =qemu_co_recv (fd, &rsp,sizeof (rsp));if (ret <sizeof (rsp)) {error_report ("failed to get the header, %s" ,strerror (errno)); - goto out; + goto err; }/* find the right aio_req from the inflight aio list */
@@ -745,7 +784,7 @@ static void coroutine_fn aio_read_response(void *opaque) }if (!aio_req) {error_report ("cannot find aio_req %x" , rsp.id); - goto out; + goto err; } acb = aio_req->aiocb;
@@ -785,7 +824,7 @@ static void coroutine_fn aio_read_response(void *opaque) aio_req->iov_offset, rsp.data_length);if (ret < rsp.data_length) {error_report ("failed to get the data, %s" ,strerror (errno)); - goto out; + goto err; }break ;case AIOCB_FLUSH_CACHE :
@@ -819,10 +858,9 @@ static void coroutine_fn aio_read_response(void *opaque)if (s->inode.vdi_id ==oid_to_vid (aio_req->oid)) { ret =reload_inode (s,0 ,"" );if (ret <0 ) { - goto out; + goto err; } } -if (is_data_obj (aio_req->oid)) { aio_req->oid =vid_to_data_oid (s->inode.vdi_id,data_oid_to_idx (aio_req->oid));
@@ -850,6 +888,10 @@ static void coroutine_fn aio_read_response(void *opaque) } out: s->co_recv =NULL ; +return ; +err: + s->co_recv =NULL ; +reconnect_to_sdog (opaque); }static void co_read_response (void *opaque)
@@ -875,7 +917,8 @@ static int aio_flush_request(void *opaque)BDRVSheepdogState *s = opaque;return !QLIST_EMPTY (&s->inflight_aio_head) || - !QLIST_EMPTY (&s->pending_aio_head); + !QLIST_EMPTY (&s->pending_aio_head) || + !QLIST_EMPTY (&s->failed_aio_head); }/*
@@ -1150,23 +1193,21 @@ static int coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req,/* send a header */ ret =qemu_co_send (s->fd, &hdr,sizeof (hdr));if (ret <sizeof (hdr)) { -qemu_co_mutex_unlock (&s->lock);error_report ("failed to send a req, %s" ,strerror (errno)); -return -errno; + goto out; }if (wlen) { ret =qemu_co_sendv (s->fd, iov, niov, aio_req->iov_offset, wlen);if (ret < wlen) { -qemu_co_mutex_unlock (&s->lock);error_report ("failed to send a data, %s" ,strerror (errno)); -return -errno; } } - +out:socket_set_cork (s->fd,0 );qemu_aio_set_fd_handler (s->fd, co_read_response,NULL , aio_flush_request, s); + s->co_send =NULL ;qemu_co_mutex_unlock (&s->lock);return 0 ;
@@ -1356,6 +1397,7 @@ static int sd_open(BlockDriverState *bs, QDict *options, int flags)QLIST_INIT (&s->inflight_aio_head);QLIST_INIT (&s->pending_aio_head); +QLIST_INIT (&s->failed_aio_head); s->fd =-1 ;memset (vdi,0 ,sizeof (vdi));
Signature · 1 line
1.8.1.3.566.gaa39828
PATCH 10/11 sheepdog: make add_aio_request and send_aioreq void functions
In reply to MORITA KazutakaThese functions no longer return errors. We can make them void functions and simplify the codes.
--- block/sheepdog.c | 66 +++++++++++++++----------------------------------------- 1 file changed, 17 insertions(+), 49 deletions(-)
block/sheepdog.c +17 -49
@@ -667,10 +667,10 @@ static int do_req(int sockfd, SheepdogReq *hdr, void *data,return srco.ret; } -static int coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req, +static void coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req,struct iovec *iov,int niov,bool create,enum AIOCBState aiocb_type); -static int coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req); +static void coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req);static int reload_inode (BDRVSheepdogState *s,uint32_t snapid,const char *tag);static int get_sheep_fd (BDRVSheepdogState *s);static void co_write_request (void *opaque);
@@ -696,22 +696,14 @@ static void coroutine_fn send_pending_req(BDRVSheepdogState *s, uint64_t oid) {AIOReq *aio_req;SheepdogAIOCB *acb; -int ret;while ((aio_req =find_pending_req (s, oid)) !=NULL ) { acb = aio_req->aiocb;/* move aio_req from pending list to inflight one */ QLIST_REMOVE (aio_req, aio_siblings);QLIST_INSERT_HEAD (&s->inflight_aio_head, aio_req, aio_siblings); - ret =add_aio_request (s, aio_req, acb->qiov->iov, - acb->qiov->niov, false, acb->aiocb_type); -if (ret <0 ) { -error_report ("add_aio_request is failed" ); -free_aio_req (s, aio_req); -if (!acb->nr_pending) { -sd_finish_aiocb (acb); - } - } +add_aio_request (s, aio_req, acb->qiov->iov, acb->qiov->niov, false, + acb->aiocb_type); } }
@@ -867,11 +859,8 @@ static void coroutine_fn aio_read_response(void *opaque) }else { aio_req->oid =vid_to_vdi_oid (s->inode.vdi_id); } - ret =resend_aioreq (s, aio_req); -if (ret ==SD_RES_SUCCESS ) { - goto out; - } -/* fall through */ +resend_aioreq (s, aio_req); + goto out;default : acb->ret = -EIO ;error_report ("%s" ,sd_strerror (rsp.result));
@@ -1129,7 +1118,7 @@ out:return ret; } -static int coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req, +static void coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req,struct iovec *iov,int niov,bool create,enum AIOCBState aiocb_type) {
@@ -1209,8 +1198,6 @@ out: aio_flush_request, s); s->co_send =NULL ;qemu_co_mutex_unlock (&s->lock); - -return 0 ; } static int read_write_object(int fd, char *buf, uint64_t oid, int copies,
@@ -1313,7 +1300,7 @@ out:return ret; } -static int coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req) +static void coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req) {SheepdogAIOCB *acb = aio_req->aiocb;bool create = false;
@@ -1338,7 +1325,7 @@ static int coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req)dprintf ("simultaneous CoW to %" PRIx64"\n" , aio_req->oid);QLIST_REMOVE (aio_req, aio_siblings);QLIST_INSERT_HEAD (&s->pending_aio_head, aio_req, aio_siblings); -return SD_RES_SUCCESS ; +return ; } }
@@ -1348,13 +1335,13 @@ static int coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req) } out:if (is_data_obj (aio_req->oid)) { -return add_aio_request (s, aio_req, acb->qiov->iov, acb->qiov->niov, - create, acb->aiocb_type); +add_aio_request (s, aio_req, acb->qiov->iov, acb->qiov->niov, create, + acb->aiocb_type); }else {struct iovec iov; iov.iov_base = &s->inode; iov.iov_len =sizeof (s->inode); -return add_aio_request (s, aio_req, &iov,1 , false,AIOCB_WRITE_UDATA ); +add_aio_request (s, aio_req, &iov,1 , false,AIOCB_WRITE_UDATA ); } }
@@ -1744,7 +1731,6 @@ static int sd_truncate(BlockDriverState *bs, int64_t offset)*/ static void coroutine_fn sd_write_done(SheepdogAIOCB *acb) { -int ret;BDRVSheepdogState *s = acb->common.bs->opaque;struct iovec iov;AIOReq *aio_req;
@@ -1766,18 +1752,13 @@ static void coroutine_fn sd_write_done(SheepdogAIOCB *acb) aio_req =alloc_aio_req (s, acb,vid_to_vdi_oid (s->inode.vdi_id), data_len, offset,0 ,0 , offset);QLIST_INSERT_HEAD (&s->inflight_aio_head, aio_req, aio_siblings); - ret =add_aio_request (s, aio_req, &iov,1 , false,AIOCB_WRITE_UDATA ); -if (ret) { -free_aio_req (s, aio_req); - acb->ret = -EIO ; - goto out; - } +add_aio_request (s, aio_req, &iov,1 , false,AIOCB_WRITE_UDATA ); acb->aio_done_func = sd_finish_aiocb; acb->aiocb_type =AIOCB_WRITE_UDATA ;return ; } -out: +sd_finish_aiocb (acb); }
@@ -1984,14 +1965,8 @@ static int coroutine_fn sd_co_rw_vector(void *p) }QLIST_INSERT_HEAD (&s->inflight_aio_head, aio_req, aio_siblings); - ret =add_aio_request (s, aio_req, acb->qiov->iov, acb->qiov->niov, - create, acb->aiocb_type); -if (ret <0 ) { -error_report ("add_aio_request is failed" ); -free_aio_req (s, aio_req); - acb->ret = -EIO ; - goto out; - } +add_aio_request (s, aio_req, acb->qiov->iov, acb->qiov->niov, create, + acb->aiocb_type); done: offset =0 ; idx++;
@@ -2059,7 +2034,6 @@ static int coroutine_fn sd_co_flush_to_disk(BlockDriverState *bs)BDRVSheepdogState *s = bs->opaque;SheepdogAIOCB *acb;AIOReq *aio_req; -int ret;if (s->cache_flags !=SD_FLAG_CMD_CACHE ) {return 0 ;
@@ -2072,13 +2046,7 @@ static int coroutine_fn sd_co_flush_to_disk(BlockDriverState *bs) aio_req =alloc_aio_req (s, acb,vid_to_vdi_oid (s->inode.vdi_id),0 ,0 ,0 ,0 ,0 );QLIST_INSERT_HEAD (&s->inflight_aio_head, aio_req, aio_siblings); - ret =add_aio_request (s, aio_req,NULL ,0 , false, acb->aiocb_type); -if (ret <0 ) { -error_report ("add_aio_request is failed" ); -free_aio_req (s, aio_req); -qemu_aio_release (acb); -return ret; - } +add_aio_request (s, aio_req,NULL ,0 , false, acb->aiocb_type);qemu_coroutine_yield ();return acb->ret;
Signature · 1 line
1.8.1.3.566.gaa39828
PATCH 11/11 sheepdog: cancel aio requests if possible
In reply to MORITA KazutakaThis patch tries to cancel aio requests in pending queue and failed queue. When the sheepdog driver cannot cancel the requests, it waits for them to be completed.
--- block/sheepdog.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 11 deletions(-)
block/sheepdog.c +59 -11
@@ -294,7 +294,8 @@ struct SheepdogAIOCB {Coroutine *coroutine;void (*aio_done_func)(SheepdogAIOCB *); -bool canceled; +bool cancelable; +bool *finished;int nr_pending; };
@@ -411,6 +412,7 @@ static inline void free_aio_req(BDRVSheepdogState *s, AIOReq *aio_req) {SheepdogAIOCB *acb = aio_req->aiocb; + acb->cancelable = false;QLIST_REMOVE (aio_req, aio_siblings);g_free (aio_req);
@@ -419,23 +421,68 @@ static inline void free_aio_req(BDRVSheepdogState *s, AIOReq *aio_req) static void coroutine_fn sd_finish_aiocb(SheepdogAIOCB *acb) { -if (!acb->canceled) { -qemu_coroutine_enter (acb->coroutine,NULL ); +qemu_coroutine_enter (acb->coroutine,NULL ); +if (acb->finished) { + *acb->finished = true; }qemu_aio_release (acb); } +/* +* Check whether the specified acb can be canceled +* +* We can cancel aio when any request belonging to the acb is: +* - Not processed by the sheepdog server. +* - Not linked to the inflight queue. +*/ +static bool sd_acb_cancelable (const SheepdogAIOCB *acb) +{ +BDRVSheepdogState *s = acb->common.bs->opaque; +AIOReq *aioreq; + +if (!acb->cancelable) { +return false; + } + + QLIST_FOREACH(aioreq, &s->inflight_aio_head, aio_siblings) { +if (aioreq->aiocb == acb) { +return false; + } + } + +return false; +} +static void sd_aio_cancel (BlockDriverAIOCB *blockacb) {SheepdogAIOCB *acb = (SheepdogAIOCB *)blockacb; +BDRVSheepdogState *s = acb->common.bs->opaque; +AIOReq *aioreq, *next; +bool finished = false; + + acb->finished = &finished; +while (!finished) { +if (sd_acb_cancelable (acb)) { +/* Remove outstanding requests from pending and failed queues. */ +QLIST_FOREACH_SAFE (aioreq, &s->pending_aio_head, aio_siblings, + next) { +if (aioreq->aiocb == acb) { +free_aio_req (s, aioreq); + } + } +QLIST_FOREACH_SAFE (aioreq, &s->failed_aio_head, aio_siblings, + next) { +if (aioreq->aiocb == acb) { +free_aio_req (s, aioreq); + } + } -/* -* Sheepdog cannot cancel the requests which are already sent to -* the servers, so we just complete the request with -EIO here. -*/ - acb->ret = -EIO ; -qemu_coroutine_enter (acb->coroutine,NULL ); - acb->canceled = true; +assert (acb->nr_pending ==0 ); +sd_finish_aiocb (acb); +return ; + } +qemu_aio_wait (); + } }static const AIOCBInfo sd_aiocb_info = {
@@ -456,7 +503,8 @@ static SheepdogAIOCB *sd_aio_setup(BlockDriverState *bs, QEMUIOVector *qiov, acb->nb_sectors = nb_sectors; acb->aio_done_func =NULL ; - acb->canceled = false; + acb->cancelable = true; + acb->finished =NULL ; acb->coroutine =qemu_coroutine_self (); acb->ret =0 ; acb->nr_pending =0 ;
Signature · 1 line
1.8.1.3.566.gaa39828