From patchwork Tue Feb 13 17:04:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 10217099 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 6F3BE601C2 for ; Tue, 13 Feb 2018 17:26:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 60D1D28EA0 for ; Tue, 13 Feb 2018 17:26:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5527428EB2; Tue, 13 Feb 2018 17:26:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 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.wl.linuxfoundation.org (Postfix) with ESMTPS id 0320828EA0 for ; Tue, 13 Feb 2018 17:26:11 +0000 (UTC) Received: from localhost ([::1]:56177 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eleLL-0003RK-3z for patchwork-qemu-devel@patchwork.kernel.org; Tue, 13 Feb 2018 12:26:11 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52215) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ele1g-0002qy-4s for qemu-devel@nongnu.org; Tue, 13 Feb 2018 12:05:53 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ele1d-0005Gj-Hj for qemu-devel@nongnu.org; Tue, 13 Feb 2018 12:05:52 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:41054 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ele1b-0005DZ-0m; Tue, 13 Feb 2018 12:05:47 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9DC0581638F0; Tue, 13 Feb 2018 17:05:46 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-94.ams2.redhat.com [10.36.117.94]) by smtp.corp.redhat.com (Postfix) with ESMTP id ED19C10073CD; Tue, 13 Feb 2018 17:05:45 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Tue, 13 Feb 2018 18:04:45 +0100 Message-Id: <20180213170529.10858-12-kwolf@redhat.com> In-Reply-To: <20180213170529.10858-1-kwolf@redhat.com> References: <20180213170529.10858-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 13 Feb 2018 17:05:46 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 13 Feb 2018 17:05:46 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'kwolf@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PULL 11/55] gluster: Query current size in do_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: kwolf@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Max Reitz Instead of expecting the current size to be 0, query it and allocate only the area [current_size, offset) if preallocation is requested. Signed-off-by: Max Reitz Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- block/gluster.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/block/gluster.c b/block/gluster.c index 8178541416..806b894bc8 100644 --- a/block/gluster.c +++ b/block/gluster.c @@ -968,10 +968,27 @@ static coroutine_fn int qemu_gluster_co_pwrite_zeroes(BlockDriverState *bs, static int qemu_gluster_do_truncate(struct glfs_fd *fd, int64_t offset, PreallocMode prealloc, Error **errp) { + int64_t current_length; + + current_length = glfs_lseek(fd, 0, SEEK_END); + if (current_length < 0) { + error_setg_errno(errp, errno, "Failed to determine current size"); + return -errno; + } + + if (current_length > offset && prealloc != PREALLOC_MODE_OFF) { + error_setg(errp, "Cannot use preallocation for shrinking files"); + return -ENOTSUP; + } + + if (current_length == offset) { + return 0; + } + switch (prealloc) { #ifdef CONFIG_GLUSTERFS_FALLOCATE case PREALLOC_MODE_FALLOC: - if (glfs_fallocate(fd, 0, 0, offset)) { + if (glfs_fallocate(fd, 0, current_length, offset - current_length)) { error_setg_errno(errp, errno, "Could not preallocate data"); return -errno; } @@ -983,7 +1000,7 @@ static int qemu_gluster_do_truncate(struct glfs_fd *fd, int64_t offset, error_setg_errno(errp, errno, "Could not resize file"); return -errno; } - if (glfs_zerofill(fd, 0, offset)) { + if (glfs_zerofill(fd, current_length, offset - current_length)) { error_setg_errno(errp, errno, "Could not zerofill the new area"); return -errno; }