From patchwork Fri Nov 29 16:45:24 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 295449 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 3121C2C00E4 for ; Sat, 30 Nov 2013 03:49:06 +1100 (EST) Received: from localhost ([::1]:48275 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VmRFX-0007Cc-6y for incoming@patchwork.ozlabs.org; Fri, 29 Nov 2013 11:49:03 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53826) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VmRDD-0003mX-1d for qemu-devel@nongnu.org; Fri, 29 Nov 2013 11:46:43 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VmRD7-0002oc-Tj for qemu-devel@nongnu.org; Fri, 29 Nov 2013 11:46:38 -0500 Received: from mx1.redhat.com ([209.132.183.28]:16155) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VmRD7-0002oX-Kb for qemu-devel@nongnu.org; Fri, 29 Nov 2013 11:46:33 -0500 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id rATGkW0f015378 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 29 Nov 2013 11:46:32 -0500 Received: from dhcp-200-207.str.redhat.com (ovpn-116-75.ams2.redhat.com [10.36.116.75]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id rATGkEqZ019151; Fri, 29 Nov 2013 11:46:30 -0500 From: Kevin Wolf To: anthony@codemonkey.ws Date: Fri, 29 Nov 2013 17:45:24 +0100 Message-Id: <1385743555-27888-10-git-send-email-kwolf@redhat.com> In-Reply-To: <1385743555-27888-1-git-send-email-kwolf@redhat.com> References: <1385743555-27888-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [Qemu-devel] [PULL 09/41] block: honour BlockLimits in bdrv_co_do_write_zeroes 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 From: Peter Lieven Reviewed-by: Eric Blake Signed-off-by: Peter Lieven Signed-off-by: Stefan Hajnoczi --- block.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/block.c b/block.c index 3759582..b4194da 100644 --- a/block.c +++ b/block.c @@ -2711,32 +2711,65 @@ int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs, BDRV_REQ_COPY_ON_READ); } +/* if no limit is specified in the BlockLimits use a default + * of 32768 512-byte sectors (16 MiB) per request. + */ +#define MAX_WRITE_ZEROES_DEFAULT 32768 + static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, int64_t sector_num, int nb_sectors, BdrvRequestFlags flags) { BlockDriver *drv = bs->drv; QEMUIOVector qiov; - struct iovec iov; - int ret; + struct iovec iov = {0}; + int ret = 0; - /* TODO Emulate only part of misaligned requests instead of letting block - * drivers return -ENOTSUP and emulate everything */ + int max_write_zeroes = bs->bl.max_write_zeroes ? + bs->bl.max_write_zeroes : MAX_WRITE_ZEROES_DEFAULT; - /* First try the efficient write zeroes operation */ - if (drv->bdrv_co_write_zeroes) { - ret = drv->bdrv_co_write_zeroes(bs, sector_num, nb_sectors, flags); - if (ret != -ENOTSUP) { - return ret; + while (nb_sectors > 0 && !ret) { + int num = nb_sectors; + + /* align request */ + if (bs->bl.write_zeroes_alignment && + num >= bs->bl.write_zeroes_alignment && + sector_num % bs->bl.write_zeroes_alignment) { + if (num > bs->bl.write_zeroes_alignment) { + num = bs->bl.write_zeroes_alignment; + } + num -= sector_num % bs->bl.write_zeroes_alignment; } - } - /* Fall back to bounce buffer if write zeroes is unsupported */ - iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; - iov.iov_base = qemu_blockalign(bs, iov.iov_len); - memset(iov.iov_base, 0, iov.iov_len); - qemu_iovec_init_external(&qiov, &iov, 1); + /* limit request size */ + if (num > max_write_zeroes) { + num = max_write_zeroes; + } + + ret = -ENOTSUP; + /* First try the efficient write zeroes operation */ + if (drv->bdrv_co_write_zeroes) { + ret = drv->bdrv_co_write_zeroes(bs, sector_num, num, flags); + } + + if (ret == -ENOTSUP) { + /* Fall back to bounce buffer if write zeroes is unsupported */ + iov.iov_len = num * BDRV_SECTOR_SIZE; + if (iov.iov_base == NULL) { + /* allocate bounce buffer only once and ensure that it + * is big enough for this and all future requests. + */ + size_t bufsize = num <= nb_sectors ? num : max_write_zeroes; + iov.iov_base = qemu_blockalign(bs, bufsize * BDRV_SECTOR_SIZE); + memset(iov.iov_base, 0, bufsize * BDRV_SECTOR_SIZE); + } + qemu_iovec_init_external(&qiov, &iov, 1); - ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, &qiov); + ret = drv->bdrv_co_writev(bs, sector_num, num, &qiov); + } + + sector_num += num; + nb_sectors -= num; + } qemu_vfree(iov.iov_base); return ret;