From patchwork Fri Apr 8 22:05:56 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 8786551 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 946129F659 for ; Fri, 8 Apr 2016 22:13:10 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D51802021A for ; Fri, 8 Apr 2016 22:13:09 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id ECAA820218 for ; Fri, 8 Apr 2016 22:13:08 +0000 (UTC) Received: from localhost ([::1]:58575 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoeeK-0007QG-6L for patchwork-qemu-devel@patchwork.kernel.org; Fri, 08 Apr 2016 18:13:08 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56413) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoeXu-00027W-6r for qemu-devel@nongnu.org; Fri, 08 Apr 2016 18:06:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aoeXr-0007YC-Ts for qemu-devel@nongnu.org; Fri, 08 Apr 2016 18:06:30 -0400 Received: from mx1.redhat.com ([209.132.183.28]:60618) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoeXp-0007WX-5s; Fri, 08 Apr 2016 18:06:25 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D6D703E5; Fri, 8 Apr 2016 22:06:24 +0000 (UTC) Received: from red.redhat.com (ovpn-113-199.phx2.redhat.com [10.3.113.199]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u38M67UM029137; Fri, 8 Apr 2016 18:06:24 -0400 From: Eric Blake To: qemu-devel@nongnu.org Date: Fri, 8 Apr 2016 16:05:56 -0600 Message-Id: <1460153158-21612-17-git-send-email-eblake@redhat.com> In-Reply-To: <1460153158-21612-1-git-send-email-eblake@redhat.com> References: <1460153158-21612-1-git-send-email-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 16/18] nbd: Support NBD_CMD_CLOSE X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Paolo Bonzini , "open list:Block layer core" , alex@alex.org.uk Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP NBD_CMD_DISC is annoying: the server is not required to reply, so the client has no choice but to disconnect once it has sent the message; but depending on timing, the server can see the disconnect prior to reading the request, and treat things as an abrupt exit rather than a clean shutdown (which may affect whether the server properly fsync()s data to disk, and so on). The new NBD_CMD_CLOSE adds another round of handshake, where the client waits for the server's action before closing, to make sure both parties know that it was a clean close rather than an accidental early disconnect. In nbd-client.c, nbd_client_close() is called after we have already exited the normal coroutine context used by all the other transmission phase handlers, so the code is a bit more complex to build up a coroutine just for the purpose of waiting for the server's response. Signed-off-by: Eric Blake --- include/block/nbd.h | 4 +++- block/nbd-client.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- nbd/server.c | 19 +++++++++++++++++-- 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/include/block/nbd.h b/include/block/nbd.h index d261dbc..4c57754 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -70,6 +70,7 @@ typedef struct nbd_reply nbd_reply; #define NBD_FLAG_SEND_FUA (1 << 3) /* Send FUA (Force Unit Access) */ #define NBD_FLAG_ROTATIONAL (1 << 4) /* Use elevator algorithm - rotational media */ #define NBD_FLAG_SEND_TRIM (1 << 5) /* Send TRIM (discard) */ +#define NBD_FLAG_SEND_CLOSE (1 << 8) /* Send CLOSE */ /* New-style handshake (global) flags, sent from server to client, and control what will happen during handshake phase. */ @@ -99,7 +100,8 @@ enum { NBD_CMD_WRITE = 1, NBD_CMD_DISC = 2, NBD_CMD_FLUSH = 3, - NBD_CMD_TRIM = 4 + NBD_CMD_TRIM = 4, + NBD_CMD_CLOSE = 7, }; #define NBD_DEFAULT_PORT 10809 diff --git a/block/nbd-client.c b/block/nbd-client.c index 285025d..f013084 100644 --- a/block/nbd-client.c +++ b/block/nbd-client.c @@ -374,6 +374,29 @@ void nbd_client_attach_aio_context(BlockDriverState *bs, false, nbd_reply_ready, NULL, bs); } +typedef struct NbdCloseCo { + BlockDriverState *bs; + nbd_request request; + nbd_reply reply; + bool done; +} NbdCloseCo; + +static void coroutine_fn nbd_client_close_co(void *opaque) +{ + NbdCloseCo *closeco = opaque; + NbdClientSession *client = nbd_get_client_session(closeco->bs); + ssize_t ret; + + nbd_coroutine_start(client, &closeco->request); + ret = nbd_co_send_request(closeco->bs, &closeco->request, NULL, 0); + if (ret >= 0) { + nbd_co_receive_reply(client, &closeco->request, &closeco->reply, + NULL, 0); + } + nbd_coroutine_end(client, &closeco->request); + closeco->done = true; +} + void nbd_client_close(BlockDriverState *bs) { NbdClientSession *client = nbd_get_client_session(bs); @@ -383,8 +406,28 @@ void nbd_client_close(BlockDriverState *bs) return; } - nbd_send_request(client->ioc, &request); + if (client->nbdflags & NBD_FLAG_SEND_CLOSE) { + /* Newer server, wants us to wait for reply before we close */ + Coroutine *co; + NbdCloseCo closeco = { + .bs = bs, + .request = { .type = NBD_CMD_CLOSE }, + }; + AioContext *aio_context; + g_assert(!qemu_in_coroutine()); + aio_context = bdrv_get_aio_context(bs); + co = qemu_coroutine_create(nbd_client_close_co); + qemu_coroutine_enter(co, &closeco); + while (!closeco.done) { + aio_poll(aio_context, true); + } + } else { + /* Older server, send request, but no reply will come */ + nbd_send_request(client->ioc, &request); + } + + /* Regardless of any received errors, the connection is done. */ nbd_teardown_connection(bs); } diff --git a/nbd/server.c b/nbd/server.c index e68e83c..2a6eaf2 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -624,7 +624,8 @@ static coroutine_fn int nbd_negotiate(NBDClientNewData *data) char buf[8 + 8 + 8 + 128]; int rc; const uint16_t myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM | - NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA); + NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA | + NBD_FLAG_SEND_CLOSE); bool oldStyle; size_t len; @@ -1244,7 +1245,21 @@ static void nbd_trip(void *opaque) break; case NBD_CMD_DISC: TRACE("Request type is DISCONNECT"); - errno = 0; + goto out; + case NBD_CMD_CLOSE: + TRACE("Request type is CLOSE"); + if (request.flags || request.from || request.len) { + LOG("bad parameters, skipping flush"); + reply.error = EINVAL; + } else { + ret = blk_co_flush(exp->blk); + if (ret < 0) { + LOG("flush failed"); + reply.error = -ret; + } + } + /* Attempt to send reply, but even if it fails, we are done */ + nbd_co_send_reply(req, &reply, 0); goto out; case NBD_CMD_FLUSH: TRACE("Request type is FLUSH");