From patchwork Fri Apr 22 23:40:50 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 8916641 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 DC6539F1C1 for ; Sat, 23 Apr 2016 00:06:47 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2906B2021B for ; Sat, 23 Apr 2016 00:06:47 +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 327932020F for ; Sat, 23 Apr 2016 00:06:46 +0000 (UTC) Received: from localhost ([::1]:42053 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1atl5x-0007dB-EF for patchwork-qemu-devel@patchwork.kernel.org; Fri, 22 Apr 2016 20:06:45 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45721) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1atkht-0004R5-KC for qemu-devel@nongnu.org; Fri, 22 Apr 2016 19:41:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1atkhs-0005jG-KV for qemu-devel@nongnu.org; Fri, 22 Apr 2016 19:41:53 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51514) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1atkhp-0005dg-Pa; Fri, 22 Apr 2016 19:41:49 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (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 69462C049D58; Fri, 22 Apr 2016 23:41:49 +0000 (UTC) Received: from red.redhat.com (ovpn-113-21.phx2.redhat.com [10.3.113.21]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u3MNfHXV028475; Fri, 22 Apr 2016 19:41:48 -0400 From: Eric Blake To: qemu-devel@nongnu.org Date: Fri, 22 Apr 2016 17:40:50 -0600 Message-Id: <1461368452-10389-43-git-send-email-eblake@redhat.com> In-Reply-To: <1461368452-10389-1-git-send-email-eblake@redhat.com> References: <1461368452-10389-1-git-send-email-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 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 v3 42/44] nbd: Implement NBD_CMD_WRITE_ZEROES on client 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 , alex@alex.org.uk, qemu-block@nongnu.org, Max Reitz 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 Upstream NBD protocol recently added the ability to efficiently write zeroes without having to send the zeroes over the wire, along with a flag to control whether the client wants a hole. The generic block code takes care of falling back to the obvious write of lots of zeroes if we return -ENOTSUP because the server does not have WRITE_ZEROES. Signed-off-by: Eric Blake Reviewed-by: Alex Bligh --- v3: rebase, tell block layer about our support --- block/nbd-client.h | 2 ++ block/nbd-client.c | 34 ++++++++++++++++++++++++++++++++++ block/nbd.c | 24 ++++++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/block/nbd-client.h b/block/nbd-client.h index 0867147..07630ab 100644 --- a/block/nbd-client.h +++ b/block/nbd-client.h @@ -46,6 +46,8 @@ void nbd_client_close(BlockDriverState *bs); int nbd_client_co_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors); int nbd_client_co_flush(BlockDriverState *bs); +int nbd_client_co_write_zeroes(BlockDriverState *bs, int64_t sector_num, + int nb_sectors, int *flags); int nbd_client_co_writev(BlockDriverState *bs, int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, int *flags); int nbd_client_co_readv(BlockDriverState *bs, int64_t sector_num, diff --git a/block/nbd-client.c b/block/nbd-client.c index f20219b..2b6ac27 100644 --- a/block/nbd-client.c +++ b/block/nbd-client.c @@ -291,6 +291,40 @@ int nbd_client_co_readv(BlockDriverState *bs, int64_t sector_num, return nbd_co_readv_1(bs, sector_num, nb_sectors, qiov, offset); } +int nbd_client_co_write_zeroes(BlockDriverState *bs, int64_t sector_num, + int nb_sectors, int *flags) +{ + ssize_t ret; + NbdClientSession *client = nbd_get_client_session(bs); + struct nbd_request request = { .type = NBD_CMD_WRITE_ZEROES }; + struct nbd_reply reply; + + if (!(client->info.flags & NBD_FLAG_SEND_WRITE_ZEROES)) { + return -ENOTSUP; + } + + if ((*flags & BDRV_REQ_FUA) && (client->info.flags & NBD_FLAG_SEND_FUA)) { + *flags &= ~BDRV_REQ_FUA; + request.flags |= NBD_CMD_FLAG_FUA; + } + if (!(*flags & BDRV_REQ_MAY_UNMAP)) { + request.flags |= NBD_CMD_FLAG_NO_HOLE; + } + + request.from = sector_num * 512; + request.len = nb_sectors * 512; + + nbd_coroutine_start(client, &request); + ret = nbd_co_send_request(bs, &request, NULL, 0); + if (ret < 0) { + reply.error = -ret; + } else { + nbd_co_receive_reply(client, &request, &reply, NULL, 0); + } + nbd_coroutine_end(client, &request); + return -reply.error; +} + int nbd_client_co_writev(BlockDriverState *bs, int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, int *flags) { diff --git a/block/nbd.c b/block/nbd.c index 34db83e..5172039 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -355,6 +355,26 @@ static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num, return nbd_client_co_readv(bs, sector_num, nb_sectors, qiov); } +static int nbd_co_write_zeroes(BlockDriverState *bs, int64_t sector_num, + int nb_sectors, BdrvRequestFlags orig_flags) +{ + int flags = orig_flags; + int ret; + + ret = nbd_client_co_write_zeroes(bs, sector_num, nb_sectors, &flags); + if (ret < 0) { + return ret; + } + + /* The flag wasn't sent to the server, so we need to emulate it with an + * explicit flush */ + if (flags & BDRV_REQ_FUA) { + ret = nbd_client_co_flush(bs); + } + + return ret; +} + static int nbd_co_writev_flags(BlockDriverState *bs, int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, int flags) { @@ -388,6 +408,7 @@ static int nbd_co_flush(BlockDriverState *bs) static void nbd_refresh_limits(BlockDriverState *bs, Error **errp) { bs->bl.max_discard = UINT32_MAX >> BDRV_SECTOR_BITS; + bs->bl.max_write_zeroes = UINT32_MAX >> BDRV_SECTOR_BITS; bs->bl.max_transfer_length = UINT32_MAX >> BDRV_SECTOR_BITS; } @@ -476,6 +497,7 @@ static BlockDriver bdrv_nbd = { .bdrv_parse_filename = nbd_parse_filename, .bdrv_file_open = nbd_open, .bdrv_co_readv = nbd_co_readv, + .bdrv_co_write_zeroes = nbd_co_write_zeroes, .bdrv_co_writev = nbd_co_writev, .bdrv_co_writev_flags = nbd_co_writev_flags, .supported_write_flags = BDRV_REQ_FUA, @@ -496,6 +518,7 @@ static BlockDriver bdrv_nbd_tcp = { .bdrv_parse_filename = nbd_parse_filename, .bdrv_file_open = nbd_open, .bdrv_co_readv = nbd_co_readv, + .bdrv_co_write_zeroes = nbd_co_write_zeroes, .bdrv_co_writev = nbd_co_writev, .bdrv_co_writev_flags = nbd_co_writev_flags, .supported_write_flags = BDRV_REQ_FUA, @@ -516,6 +539,7 @@ static BlockDriver bdrv_nbd_unix = { .bdrv_parse_filename = nbd_parse_filename, .bdrv_file_open = nbd_open, .bdrv_co_readv = nbd_co_readv, + .bdrv_co_write_zeroes = nbd_co_write_zeroes, .bdrv_co_writev = nbd_co_writev, .bdrv_co_writev_flags = nbd_co_writev_flags, .supported_write_flags = BDRV_REQ_FUA,