From nobody Thu Dec 18 13:18:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1551109385128758.1504405542063; Mon, 25 Feb 2019 07:43:05 -0800 (PST) Received: from localhost ([127.0.0.1]:39340 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyIPF-0000p3-45 for importer@patchew.org; Mon, 25 Feb 2019 10:43:01 -0500 Received: from eggs.gnu.org ([209.51.188.92]:37391) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyI4u-0000P4-Nw for qemu-devel@nongnu.org; Mon, 25 Feb 2019 10:22:01 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gyI4m-0000zU-CC for qemu-devel@nongnu.org; Mon, 25 Feb 2019 10:21:55 -0500 Received: from mx1.redhat.com ([209.132.183.28]:43932) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gyI4c-0000kC-UW; Mon, 25 Feb 2019 10:21:43 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9A71A86678; Mon, 25 Feb 2019 15:21:30 +0000 (UTC) Received: from linux.fritz.box.com (ovpn-117-243.ams2.redhat.com [10.36.117.243]) by smtp.corp.redhat.com (Postfix) with ESMTP id 746755C221; Mon, 25 Feb 2019 15:21:28 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Mon, 25 Feb 2019 16:19:57 +0100 Message-Id: <20190225152053.15976-16-kwolf@redhat.com> In-Reply-To: <20190225152053.15976-1-kwolf@redhat.com> References: <20190225152053.15976-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Mon, 25 Feb 2019 15:21:30 +0000 (UTC) Content-Transfer-Encoding: quoted-printable 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] [PULL 15/71] nbd: Use low-level QIOChannel API in nbd_read_eof() 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: kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Instead of using the convenience wrapper qio_channel_read_all_eof(), use the lower level QIOChannel API. This means duplicating some code, but we'll need this because this coroutine yield is special: We want it to be interruptible so that nbd_client_attach_aio_context() can correctly reenter the coroutine. This moves the bdrv_dec/inc_in_flight() pair into nbd_read_eof(), so that connection_co will always sit in this exact qio_channel_yield() call when bdrv_drain() returns. Signed-off-by: Kevin Wolf Reviewed-by: Eric Blake --- include/block/nbd.h | 4 ++-- block/nbd-client.c | 8 +------- nbd/client.c | 46 ++++++++++++++++++++++++++++++++++++--------- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/include/block/nbd.h b/include/block/nbd.h index cad975e00c..c6ef1ef42e 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -300,8 +300,8 @@ int nbd_receive_export_list(QIOChannel *ioc, QCryptoTLS= Creds *tlscreds, int nbd_init(int fd, QIOChannelSocket *sioc, NBDExportInfo *info, Error **errp); int nbd_send_request(QIOChannel *ioc, NBDRequest *request); -int coroutine_fn nbd_receive_reply(QIOChannel *ioc, NBDReply *reply, - Error **errp); +int coroutine_fn nbd_receive_reply(BlockDriverState *bs, QIOChannel *ioc, + NBDReply *reply, Error **errp); int nbd_client(int fd); int nbd_disconnect(int fd); int nbd_errno_to_system_errno(int err); diff --git a/block/nbd-client.c b/block/nbd-client.c index 5ce4aa9520..60f38f0320 100644 --- a/block/nbd-client.c +++ b/block/nbd-client.c @@ -84,15 +84,9 @@ static coroutine_fn void nbd_connection_entry(void *opaq= ue) * * Therefore we keep an additional in_flight reference all the tim= e and * only drop it temporarily here. - * - * FIXME This is not safe because the QIOChannel could wake up the - * coroutine for a second time; it is not prepared for coroutine - * resumption from external code. */ - bdrv_dec_in_flight(s->bs); assert(s->reply.handle =3D=3D 0); - ret =3D nbd_receive_reply(s->ioc, &s->reply, &local_err); - bdrv_inc_in_flight(s->bs); + ret =3D nbd_receive_reply(s->bs, s->ioc, &s->reply, &local_err); =20 if (local_err) { trace_nbd_read_reply_entry_fail(ret, error_get_pretty(local_er= r)); diff --git a/nbd/client.c b/nbd/client.c index 28d174c0f3..de7da48246 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -1394,30 +1394,58 @@ static int nbd_receive_structured_reply_chunk(QIOCh= annel *ioc, * negative errno on failure (errp is set) */ static inline int coroutine_fn -nbd_read_eof(QIOChannel *ioc, void *buffer, size_t size, Error **errp) +nbd_read_eof(BlockDriverState *bs, QIOChannel *ioc, void *buffer, size_t s= ize, + Error **errp) { - int ret; + bool partial =3D false; =20 assert(size); - ret =3D qio_channel_read_all_eof(ioc, buffer, size, errp); - if (ret < 0) { - ret =3D -EIO; + while (size > 0) { + struct iovec iov =3D { .iov_base =3D buffer, .iov_len =3D size }; + ssize_t len; + + len =3D qio_channel_readv(ioc, &iov, 1, errp); + if (len =3D=3D QIO_CHANNEL_ERR_BLOCK) { + bdrv_dec_in_flight(bs); + qio_channel_yield(ioc, G_IO_IN); + bdrv_inc_in_flight(bs); + continue; + } else if (len < 0) { + return -EIO; + } else if (len =3D=3D 0) { + if (partial) { + error_setg(errp, + "Unexpected end-of-file before all bytes were r= ead"); + return -EIO; + } else { + return 0; + } + } + + partial =3D true; + size -=3D len; + buffer =3D (uint8_t*) buffer + len; } - return ret; + return 1; } =20 /* nbd_receive_reply + * + * Decreases bs->in_flight while waiting for a new reply. This yield is wh= ere + * we wait indefinitely and the coroutine must be able to be safely reente= red + * for nbd_client_attach_aio_context(). + * * Returns 1 on success * 0 on eof, when no data was read (errp is not set) * negative errno on failure (errp is set) */ -int coroutine_fn nbd_receive_reply(QIOChannel *ioc, NBDReply *reply, - Error **errp) +int coroutine_fn nbd_receive_reply(BlockDriverState *bs, QIOChannel *ioc, + NBDReply *reply, Error **errp) { int ret; const char *type; =20 - ret =3D nbd_read_eof(ioc, &reply->magic, sizeof(reply->magic), errp); + ret =3D nbd_read_eof(bs, ioc, &reply->magic, sizeof(reply->magic), err= p); if (ret <=3D 0) { return ret; } --=20 2.20.1