From nobody Thu Nov 6 16:02:45 2025 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.zoho.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 1489441554407800.3365724878714; Mon, 13 Mar 2017 14:45:54 -0700 (PDT) Received: from localhost ([::1]:54553 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cnXmq-0002d8-PM for importer@patchew.org; Mon, 13 Mar 2017 17:45:52 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43758) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cnXiA-0007Wu-CA for qemu-devel@nongnu.org; Mon, 13 Mar 2017 17:41:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cnXi9-0001tH-80 for qemu-devel@nongnu.org; Mon, 13 Mar 2017 17:41:02 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51582) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cnXi4-0001pa-L4; Mon, 13 Mar 2017 17:40:56 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (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 C073A61B86; Mon, 13 Mar 2017 21:40:56 +0000 (UTC) Received: from localhost (ovpn-204-63.brq.redhat.com [10.40.204.63]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v2DLesYV026113 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Mon, 13 Mar 2017 17:40:56 -0400 From: Max Reitz To: qemu-block@nongnu.org Date: Mon, 13 Mar 2017 22:40:35 +0100 Message-Id: <20170313214045.26857-4-mreitz@redhat.com> In-Reply-To: <20170313214001.26339-1-mreitz@redhat.com> References: <20170313214001.26339-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Mon, 13 Mar 2017 21:40:56 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH for-2.10 06/16] block/file-posix: Extract raw_regular_truncate() 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 , qemu-devel@nongnu.org, Max Reitz 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" This functionality is part of raw_create() which we will be able to reuse nicely in raw_truncate(). Signed-off-by: Max Reitz Reviewed-by: Stefan Hajnoczi --- block/file-posix.c | 144 +++++++++++++++++++++++++++++--------------------= ---- 1 file changed, 78 insertions(+), 66 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index b02d8fc37b..35a9e43f3e 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1384,6 +1384,81 @@ static void raw_close(BlockDriverState *bs) } } =20 +static int raw_regular_truncate(int fd, int64_t offset, PreallocMode preal= loc, + Error **errp) +{ + int result =3D 0; + char *buf; + + switch (prealloc) { +#ifdef CONFIG_POSIX_FALLOCATE + case PREALLOC_MODE_FALLOC: + /* + * Truncating before posix_fallocate() makes it about twice slower= on + * file systems that do not support fallocate(), trying to check i= f a + * block is allocated before allocating it, so don't do that here. + */ + result =3D -posix_fallocate(fd, 0, offset); + if (result !=3D 0) { + /* posix_fallocate() doesn't set errno. */ + error_setg_errno(errp, -result, + "Could not preallocate data for the new file"= ); + } + return result; +#endif + case PREALLOC_MODE_FULL: + { + int64_t num =3D 0, left =3D offset; + + /* + * Knowing the final size from the beginning could allow the file + * system driver to do less allocations and possibly avoid + * fragmentation of the file. + */ + if (ftruncate(fd, offset) !=3D 0) { + result =3D -errno; + error_setg_errno(errp, -result, "Could not resize file"); + return result; + } + + buf =3D g_malloc0(65536); + + while (left > 0) { + num =3D MIN(left, 65536); + result =3D write(fd, buf, num); + if (result < 0) { + result =3D -errno; + error_setg_errno(errp, -result, + "Could not write to the new file"); + break; + } + left -=3D result; + } + if (result >=3D 0) { + result =3D fsync(fd); + if (result < 0) { + result =3D -errno; + error_setg_errno(errp, -result, + "Could not flush new file to disk"); + } + } + g_free(buf); + return result; + } + case PREALLOC_MODE_OFF: + if (ftruncate(fd, offset) !=3D 0) { + result =3D -errno; + error_setg_errno(errp, -result, "Could not resize file"); + } + return result; + default: + result =3D -ENOTSUP; + error_setg(errp, "Unsupported preallocation mode: %s", + PreallocMode_lookup[prealloc]); + return result; + } +} + static int raw_truncate(BlockDriverState *bs, int64_t offset, PreallocMode prealloc, Error **errp) { @@ -1652,72 +1727,9 @@ static int raw_create(const char *filename, QemuOpts= *opts, Error **errp) #endif } =20 - switch (prealloc) { -#ifdef CONFIG_POSIX_FALLOCATE - case PREALLOC_MODE_FALLOC: - /* - * Truncating before posix_fallocate() makes it about twice slower= on - * file systems that do not support fallocate(), trying to check i= f a - * block is allocated before allocating it, so don't do that here. - */ - result =3D -posix_fallocate(fd, 0, total_size); - if (result !=3D 0) { - /* posix_fallocate() doesn't set errno. */ - error_setg_errno(errp, -result, - "Could not preallocate data for the new file"= ); - } - break; -#endif - case PREALLOC_MODE_FULL: - { - int64_t num =3D 0, left =3D total_size; - - /* - * Knowing the final size from the beginning could allow the file - * system driver to do less allocations and possibly avoid - * fragmentation of the file. - */ - if (ftruncate(fd, total_size) !=3D 0) { - result =3D -errno; - error_setg_errno(errp, -result, "Could not resize file"); - goto out_close; - } - - buf =3D g_malloc0(65536); - - while (left > 0) { - num =3D MIN(left, 65536); - result =3D write(fd, buf, num); - if (result < 0) { - result =3D -errno; - error_setg_errno(errp, -result, - "Could not write to the new file"); - break; - } - left -=3D result; - } - if (result >=3D 0) { - result =3D fsync(fd); - if (result < 0) { - result =3D -errno; - error_setg_errno(errp, -result, - "Could not flush new file to disk"); - } - } - g_free(buf); - break; - } - case PREALLOC_MODE_OFF: - if (ftruncate(fd, total_size) !=3D 0) { - result =3D -errno; - error_setg_errno(errp, -result, "Could not resize file"); - } - break; - default: - result =3D -ENOTSUP; - error_setg(errp, "Unsupported preallocation mode: %s", - PreallocMode_lookup[prealloc]); - break; + result =3D raw_regular_truncate(fd, total_size, prealloc, errp); + if (result < 0) { + goto out_close; } =20 out_close: --=20 2.12.0