From patchwork Fri Dec 23 15:26:19 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 133076 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 27240B71C5 for ; Sat, 24 Dec 2011 02:29:30 +1100 (EST) Received: from localhost ([::1]:56124 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Re73i-0003CC-Gp for incoming@patchwork.ozlabs.org; Fri, 23 Dec 2011 10:29:22 -0500 Received: from eggs.gnu.org ([140.186.70.92]:51209) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Re73c-0003Av-Cf for qemu-devel@nongnu.org; Fri, 23 Dec 2011 10:29:17 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Re73Y-0006j6-Aq for qemu-devel@nongnu.org; Fri, 23 Dec 2011 10:29:16 -0500 Received: from mail-gx0-f173.google.com ([209.85.161.173]:54035) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Re73Y-0006gC-8C for qemu-devel@nongnu.org; Fri, 23 Dec 2011 10:29:12 -0500 Received: by mail-gx0-f173.google.com with SMTP id k1so8050701ggn.4 for ; Fri, 23 Dec 2011 07:29:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=sender:from:to:subject:date:message-id:x-mailer:in-reply-to :references; bh=Bp8/BPR6oaA+HUPnv9It9gTzV8+gjMHarB8+DRg0JRg=; b=IVGu9/Wc8yf3GJQoxSxujlAjVg20kVUKgiX90rmuTMxA1MLm/jI/h3pnpxjzlFnyqL UswPksBmKLOOIhJT99pZWmCU+tEIW7RB9oeNQbCv9RHLqO2TJhM9JSKSUmcLz6fz+mVz AQLZH+6w0M0MCorqFYMeT7ArilEViA7tkhhm0= Received: by 10.50.222.233 with SMTP id qp9mr14329685igc.1.1324654151905; Fri, 23 Dec 2011 07:29:11 -0800 (PST) Received: from localhost.localdomain (93-34-178-147.ip50.fastwebnet.it. [93.34.178.147]) by mx.google.com with ESMTPS id aq5sm42055557igc.5.2011.12.23.07.29.06 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 23 Dec 2011 07:29:11 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 23 Dec 2011 16:26:19 +0100 Message-Id: <1324653990-20074-16-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.7.7.1 In-Reply-To: <1324653990-20074-1-git-send-email-pbonzini@redhat.com> References: <1324653990-20074-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 209.85.161.173 Subject: [Qemu-devel] [PATCH 15/26] qemu-nbd: introduce nbd_do_send_reply X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Group the sending of a reply and the associated data into a new function. Without corking, the caller would be forced to leave 12 free bytes at the beginning of the data pointer. Not too ugly, but still ugly. :) Using nbd_do_send_reply everywhere will help when the routine will set up the write handler that re-enters the send coroutine. Signed-off-by: Paolo Bonzini --- nbd.c | 46 ++++++++++++++++++++++++++++++++-------------- 1 files changed, 32 insertions(+), 14 deletions(-) diff --git a/nbd.c b/nbd.c index d383d85..025c5b0 100644 --- a/nbd.c +++ b/nbd.c @@ -583,6 +583,34 @@ static int nbd_send_reply(int csock, struct nbd_reply *reply) return 0; } +static int nbd_do_send_reply(int csock, struct nbd_reply *reply, + uint8_t *data, int len) +{ + int rc, ret; + + if (!len) { + rc = nbd_send_reply(csock, reply); + if (rc == -1) { + rc = -errno; + } + } else { + socket_set_cork(csock, 1); + rc = nbd_send_reply(csock, reply); + if (rc != -1) { + ret = write_sync(csock, data, len); + if (ret != len) { + errno = EIO; + rc = -1; + } + } + if (rc == -1) { + rc = -errno; + } + socket_set_cork(csock, 0); + } + return rc; +} + int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset, uint32_t nbdflags, uint8_t *data) @@ -637,18 +665,8 @@ int nbd_trip(BlockDriverState *bs, int csock, off_t size, } TRACE("Read %u byte(s)", request.len); - socket_set_cork(csock, 1); - if (nbd_send_reply(csock, &reply) == -1) + if (nbd_do_send_reply(csock, &reply, data, request.len) < 0) return -1; - - TRACE("Sending data to client"); - - if (write_sync(csock, data, request.len) != request.len) { - LOG("writing to socket failed"); - errno = EINVAL; - return -1; - } - socket_set_cork(csock, 0); break; case NBD_CMD_WRITE: TRACE("Request type is WRITE"); @@ -684,7 +702,7 @@ int nbd_trip(BlockDriverState *bs, int csock, off_t size, } } - if (nbd_send_reply(csock, &reply) == -1) + if (nbd_do_send_reply(csock, &reply, NULL, 0) < 0) return -1; break; case NBD_CMD_DISC: @@ -700,7 +718,7 @@ int nbd_trip(BlockDriverState *bs, int csock, off_t size, reply.error = -ret; } - if (nbd_send_reply(csock, &reply) == -1) + if (nbd_do_send_reply(csock, &reply, NULL, 0) < 0) return -1; break; case NBD_CMD_TRIM: @@ -711,7 +729,7 @@ int nbd_trip(BlockDriverState *bs, int csock, off_t size, LOG("discard failed"); reply.error = -ret; } - if (nbd_send_reply(csock, &reply) == -1) + if (nbd_do_send_reply(csock, &reply, NULL, 0) < 0) return -1; break; default: