From nobody Mon Feb 9 11:33:04 2026 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 1497385567021387.3797837051295; Tue, 13 Jun 2017 13:26:07 -0700 (PDT) Received: from localhost ([::1]:45055 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dKsO5-0001tl-Kj for importer@patchew.org; Tue, 13 Jun 2017 16:26:05 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38065) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dKsJx-0006Q9-4X for qemu-devel@nongnu.org; Tue, 13 Jun 2017 16:21:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dKsJv-0001bW-Ux for qemu-devel@nongnu.org; Tue, 13 Jun 2017 16:21:49 -0400 Received: from mx1.redhat.com ([209.132.183.28]:41818) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dKsJq-0001Z9-33; Tue, 13 Jun 2017 16:21:42 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 26A444ACA3; Tue, 13 Jun 2017 20:21:41 +0000 (UTC) Received: from localhost (ovpn-204-68.brq.redhat.com [10.40.204.68]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9A7AE8BA4D; Tue, 13 Jun 2017 20:21:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 26A444ACA3 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=mreitz@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 26A444ACA3 From: Max Reitz To: qemu-block@nongnu.org Date: Tue, 13 Jun 2017 22:20:58 +0200 Message-Id: <20170613202107.10125-8-mreitz@redhat.com> In-Reply-To: <20170613202107.10125-1-mreitz@redhat.com> References: <20170613202107.10125-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Tue, 13 Jun 2017 20:21:41 +0000 (UTC) Content-Transfer-Encoding: quoted-printable 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 v4 07/16] block/file-posix: Generalize 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, Stefan Hajnoczi , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Currently, raw_regular_truncate() is intended for setting the size of a newly created file. However, we also want to use it for truncating an existing file in which case only the newly added space (when growing) should be preallocated. This also means that if resizing failed, we should try to restore the original file size. This is important when using preallocation. Signed-off-by: Max Reitz Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Stefan Hajnoczi --- block/file-posix.c | 61 ++++++++++++++++++++++++++++++++++++++++++++------= ---- 1 file changed, 50 insertions(+), 11 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index bd02bfb..dc3db13 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1624,11 +1624,31 @@ static void raw_close(BlockDriverState *bs) } } =20 +/** + * Truncates the given regular file @fd to @offset and, when growing, fill= s the + * new space according to @prealloc. + * + * Returns: 0 on success, -errno on failure. + */ static int raw_regular_truncate(int fd, int64_t offset, PreallocMode preal= loc, Error **errp) { int result =3D 0; - char *buf; + int64_t current_length =3D 0; + char *buf =3D NULL; + struct stat st; + + if (fstat(fd, &st) < 0) { + result =3D -errno; + error_setg_errno(errp, -result, "Could not stat file"); + return result; + } + + current_length =3D st.st_size; + if (current_length > offset && prealloc !=3D PREALLOC_MODE_OFF) { + error_setg(errp, "Cannot use preallocation for shrinking files"); + return -ENOTSUP; + } =20 switch (prealloc) { #ifdef CONFIG_POSIX_FALLOCATE @@ -1638,17 +1658,17 @@ static int raw_regular_truncate(int fd, int64_t off= set, PreallocMode prealloc, * 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); + result =3D -posix_fallocate(fd, current_length, offset - current_l= ength); if (result !=3D 0) { /* posix_fallocate() doesn't set errno. */ error_setg_errno(errp, -result, - "Could not preallocate data for the new file"= ); + "Could not preallocate new data"); } - return result; + goto out; #endif case PREALLOC_MODE_FULL: { - int64_t num =3D 0, left =3D offset; + int64_t num =3D 0, left =3D offset - current_length; =20 /* * Knowing the final size from the beginning could allow the file @@ -1658,19 +1678,27 @@ static int raw_regular_truncate(int fd, int64_t off= set, PreallocMode prealloc, if (ftruncate(fd, offset) !=3D 0) { result =3D -errno; error_setg_errno(errp, -result, "Could not resize file"); - return result; + goto out; } =20 buf =3D g_malloc0(65536); =20 + result =3D lseek(fd, current_length, SEEK_SET); + if (result < 0) { + result =3D -errno; + error_setg_errno(errp, -result, + "Failed to seek to the old end of file"); + goto out; + } + 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; + "Could not write zeros for preallocation"= ); + goto out; } left -=3D result; } @@ -1679,11 +1707,11 @@ static int raw_regular_truncate(int fd, int64_t off= set, PreallocMode prealloc, if (result < 0) { result =3D -errno; error_setg_errno(errp, -result, - "Could not flush new file to disk"); + "Could not flush file to disk"); + goto out; } } - g_free(buf); - return result; + goto out; } case PREALLOC_MODE_OFF: if (ftruncate(fd, offset) !=3D 0) { @@ -1697,6 +1725,17 @@ static int raw_regular_truncate(int fd, int64_t offs= et, PreallocMode prealloc, PreallocMode_lookup[prealloc]); return result; } + +out: + if (result < 0) { + if (ftruncate(fd, current_length) < 0) { + error_report("Failed to restore old file length: %s", + strerror(errno)); + } + } + + g_free(buf); + return result; } =20 static int raw_truncate(BlockDriverState *bs, int64_t offset, --=20 2.9.4