summaryrefslogtreecommitdiff
path: root/nbd/client.c
diff options
context:
space:
mode:
authorEric Blake <eblake@redhat.com>2016-10-14 13:33:16 -0500
committerPaolo Bonzini <pbonzini@redhat.com>2016-11-02 09:28:56 +0100
commitb6f5d3b573fe43da1f9fa07b7454e4492f409411 (patch)
treebe19f967869577966cdbcfa26f1a21dfbfc5832a /nbd/client.c
parent8b34a9dbc3f2c0afe3450cb20b94cc30f450e77b (diff)
downloadqemu-b6f5d3b573fe43da1f9fa07b7454e4492f409411.tar.gz
nbd: Improve server handling of shutdown requests
NBD commit 6d34500b clarified how clients and servers are supposed to behave before closing a connection. It added NBD_REP_ERR_SHUTDOWN (for the server to announce it is about to go away during option haggling, so the client should quit sending NBD_OPT_* other than NBD_OPT_ABORT) and ESHUTDOWN (for the server to announce it is about to go away during transmission, so the client should quit sending NBD_CMD_* other than NBD_CMD_DISC). It also clarified that NBD_OPT_ABORT gets a reply, while NBD_CMD_DISC does not. This patch merely adds the missing reply to NBD_OPT_ABORT and teaches the client to recognize server errors. Actually teaching the server to send NBD_REP_ERR_SHUTDOWN or ESHUTDOWN would require knowing that the server has been requested to shut down soon (maybe we could do that by installing a SIGINT handler in qemu-nbd, which transitions from RUNNING to a new state that waits for the client to react, rather than just out-right quitting - but that's a bigger task for another day). Signed-off-by: Eric Blake <eblake@redhat.com> Message-Id: <1476469998-28592-15-git-send-email-eblake@redhat.com> [Move dummy ESHUTDOWN to include/qemu/osdep.h. - Paolo] Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'nbd/client.c')
-rw-r--r--nbd/client.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/nbd/client.c b/nbd/client.c
index 7bdce531cc..7db4301d29 100644
--- a/nbd/client.c
+++ b/nbd/client.c
@@ -40,6 +40,9 @@ static int nbd_errno_to_system_errno(int err)
case NBD_ENOSPC:
ret = ENOSPC;
break;
+ case NBD_ESHUTDOWN:
+ ret = ESHUTDOWN;
+ break;
default:
TRACE("Squashing unexpected error %d to EINVAL", err);
/* fallthrough */
@@ -239,11 +242,21 @@ static int nbd_handle_reply_err(QIOChannel *ioc, nbd_opt_reply *reply,
reply->option);
break;
+ case NBD_REP_ERR_PLATFORM:
+ error_setg(errp, "Server lacks support for option %" PRIx32,
+ reply->option);
+ break;
+
case NBD_REP_ERR_TLS_REQD:
error_setg(errp, "TLS negotiation required before option %" PRIx32,
reply->option);
break;
+ case NBD_REP_ERR_SHUTDOWN:
+ error_setg(errp, "Server shutting down before option %" PRIx32,
+ reply->option);
+ break;
+
default:
error_setg(errp, "Unknown error code when asking for option %" PRIx32,
reply->option);
@@ -785,6 +798,11 @@ ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *reply)
reply->error = nbd_errno_to_system_errno(reply->error);
+ if (reply->error == ESHUTDOWN) {
+ /* This works even on mingw which lacks a native ESHUTDOWN */
+ LOG("server shutting down");
+ return -EINVAL;
+ }
TRACE("Got reply: { magic = 0x%" PRIx32 ", .error = % " PRId32
", handle = %" PRIu64" }",
magic, reply->error, reply->handle);