From nobody Mon Apr 29 10:55:41 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.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 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1522204731732244.24362407515366; Tue, 27 Mar 2018 19:38:51 -0700 (PDT) Received: from localhost ([::1]:37107 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f10z9-0002SS-0j for importer@patchew.org; Tue, 27 Mar 2018 22:38:47 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49044) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f10yF-00027L-VE for qemu-devel@nongnu.org; Tue, 27 Mar 2018 22:37:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f10yF-0001BR-0c for qemu-devel@nongnu.org; Tue, 27 Mar 2018 22:37:51 -0400 Received: from mail139-154.mail.alibaba.com ([198.11.139.154]:37225) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1f10yA-00018z-FJ; Tue, 27 Mar 2018 22:37:46 -0400 Received: from localhost.localdomain(mailfrom:zhenwei.pi@youruncloud.com fp:112.95.153.98) by smtp.aliyun-inc.com(10.147.43.95); Wed, 28 Mar 2018 10:37:20 +0800 X-Alimail-AntiSpam: AC=CONTINUE; BC=0.06527799|-1; CH=green; FP=0|0|0|0|0|-1|-1|-1; HT=e01l07447; MF=zhenwei.pi@youruncloud.com; NM=1; PH=DS; RN=5; RT=5; SR=0; TI=SMTPD_---.BTQ5mMV_1522204639; From: "zhenwei.pi" To: kwolf@redhat.com, mreitz@redhat.com Date: Wed, 28 Mar 2018 10:37:17 +0800 Message-Id: <1522204637-29589-1-git-send-email-zhenwei.pi@youruncloud.com> X-Mailer: git-send-email 2.7.4 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 198.11.139.154 Subject: [Qemu-devel] [PATCH] file-posix: Support fallocate for block device 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: zhenwei.pi@youruncloud.com, qemu-devel@nongnu.org, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" since linux 4.9, block device supports fallocate. kernel issues block device zereout request and invalidates page cache. So ioctl(fd, FALLOC_FL_ZERO_RANGE...) is safer than ioctl(fd, BLKZEROOUT...). try to call do_fallocate, if failing, fallback. use new field "has_fallocate_zero_range" with default value as true. if do_fallocate returns -ENOTSUP, it will be set false. Signed-off-by: zhenwei.pi --- block/file-posix.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index d7fb772..842e940 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -159,8 +159,9 @@ typedef struct BDRVRawState { bool discard_zeroes:1; bool use_linux_aio:1; bool page_cache_inconsistent:1; - bool has_fallocate; - bool needs_alignment; + bool has_fallocate:1; + bool has_fallocate_zero_range:1; + bool needs_alignment:1; =20 PRManager *pr_mgr; } BDRVRawState; @@ -549,6 +550,7 @@ static int raw_open_common(BlockDriverState *bs, QDict = *options, =20 s->has_discard =3D true; s->has_write_zeroes =3D true; + s->has_fallocate_zero_range =3D true; if ((bs->open_flags & BDRV_O_NOCACHE) !=3D 0) { s->needs_alignment =3D true; } @@ -1365,10 +1367,6 @@ static ssize_t handle_aiocb_write_zeroes(RawPosixAIO= Data *aiocb) int64_t len; #endif =20 - if (aiocb->aio_type & QEMU_AIO_BLKDEV) { - return handle_aiocb_write_zeroes_block(aiocb); - } - #ifdef CONFIG_XFS if (s->is_xfs) { return xfs_write_zeroes(s, aiocb->aio_offset, aiocb->aio_nbytes); @@ -1376,16 +1374,25 @@ static ssize_t handle_aiocb_write_zeroes(RawPosixAI= OData *aiocb) #endif =20 #ifdef CONFIG_FALLOCATE_ZERO_RANGE - if (s->has_write_zeroes) { + /* since linux 4.9, block device supports fallocate. kernel issues + * block device zereout request and invalidates page cache. So + * ioctl(fd, FALLOC_FL_ZERO_RANGE...) is safer than ioctl(fd, + * BLKZEROOUT...). try to call do_fallocate, if failing, fallback. + */ + if (s->has_fallocate_zero_range) { int ret =3D do_fallocate(s->fd, FALLOC_FL_ZERO_RANGE, aiocb->aio_offset, aiocb->aio_nbytes); - if (ret =3D=3D 0 || ret !=3D -ENOTSUP) { + if (ret =3D=3D 0) { return ret; - } - s->has_write_zeroes =3D false; + } else if (ret =3D=3D -ENOTSUP) + s->has_fallocate_zero_range =3D false; } #endif =20 + if (aiocb->aio_type & QEMU_AIO_BLKDEV) { + return handle_aiocb_write_zeroes_block(aiocb); + } + #ifdef CONFIG_FALLOCATE_PUNCH_HOLE if (s->has_discard && s->has_fallocate) { int ret =3D do_fallocate(s->fd, --=20 2.7.4