From patchwork Fri Feb 17 00:51:26 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nir Soffer X-Patchwork-Id: 9578563 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 5527D60244 for ; Fri, 17 Feb 2017 00:52:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 44137285E4 for ; Fri, 17 Feb 2017 00:52:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3903B28683; Fri, 17 Feb 2017 00:52:22 +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.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID 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 CF6B6285E4 for ; Fri, 17 Feb 2017 00:52:21 +0000 (UTC) Received: from localhost ([::1]:51015 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ceWmb-0001ME-1B for patchwork-qemu-devel@patchwork.kernel.org; Thu, 16 Feb 2017 19:52:21 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60055) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ceWm7-0001C6-Gq for qemu-devel@nongnu.org; Thu, 16 Feb 2017 19:51:52 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ceWm6-0007qa-O2 for qemu-devel@nongnu.org; Thu, 16 Feb 2017 19:51:51 -0500 Received: from mail-wr0-x241.google.com ([2a00:1450:400c:c0c::241]:35436) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ceWm6-0007po-I0; Thu, 16 Feb 2017 19:51:50 -0500 Received: by mail-wr0-x241.google.com with SMTP id q39so3775042wrb.2; Thu, 16 Feb 2017 16:51:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=6A2JjvFrrdbYPT2T/is8MFxiKSez/7dvpR7ZphEgdSk=; b=c6UOoiitiq4a01KOSpCyGRhDd0mzN9OF3ymevkdTtVH3rmurkl4uGVxlVQnoiYPpiP fC37Pl1uyxSA6UsDd/v/gh0ah/Qsy2OAEcA2uJbTxeDEow3NxSNgDXWzq2Bw+0Nddv6X r0dBE1qRMQY/eNj16dYjhB0RAmaiVD2R4Io6pWp8786MdIVifTv+08bdyhOxCxnewCH1 VEVkfJLSNTlc27c8mJDD9i7qCBWoCTGsLfzNR+Vkq7VSgcMqBnG79V1zNIoV7eLdOMWZ Zgh+vjZ+OxqtbJJeZXhZT371jbH41bemfRZWvV3BhVRsuABSt6V0y5b5qISpJLOkI2JR 7TTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=6A2JjvFrrdbYPT2T/is8MFxiKSez/7dvpR7ZphEgdSk=; b=blWR+dmU4s3lxBsvmfXuv5QAyM9ZJzEG7XUY+3NKyWRC4YwA0Njhj7bEaujDk3Xceh AOPQg/75x7OS+sG8YtUmxSrDpJceqC55XsATVMSJqdzLIQhLv+zPy2LSw1uwjlYdL1Kc lA+3caqy4ucaHaxhjM+FhINAZ/esflPdjATJmwUhOqI56niNLeK+sWq21QdbVytGqfIy xb0VSR/p5Ta1JzvUg1LQNTtTHlAzWuwztGloBxx9W+MVpjy1V03xsS10mgA897RKG5b9 PFlat13Jf9VcqDv6mSQXw8oySOLoQxw3BewEK4T1pRdauEpdKj9TR89+1591v+w/KxGa uKOQ== X-Gm-Message-State: AMke39mbPy5sX4KRbn56D8e7csOV5q46KtHSfoKfddmURT3OOQpuOD+S25F8IhTxre5bHw== X-Received: by 10.223.177.18 with SMTP id l18mr4466792wra.96.1487292709477; Thu, 16 Feb 2017 16:51:49 -0800 (PST) Received: from lean.local (bzq-79-181-80-34.red.bezeqint.net. [79.181.80.34]) by smtp.gmail.com with ESMTPSA id u184sm123712wmb.29.2017.02.16.16.51.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 16 Feb 2017 16:51:48 -0800 (PST) From: Nir Soffer To: qemu-devel@nongnu.org Date: Fri, 17 Feb 2017 02:51:26 +0200 Message-Id: <20170217005127.21010-3-nirsof@gmail.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170217005127.21010-1-nirsof@gmail.com> References: <20170217005127.21010-1-nirsof@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c0c::241 Subject: [Qemu-devel] [PATCH 2/3] qemu-img: Truncate before full preallocation 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, Nir Soffer , qemu-block@nongnu.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP In commit 10ddfe7b6044 (qemu-img: Do not truncate before preallocation) we moved truncate to the PREALLOC_MODE_OFF branch to avoid slowdown in posix_fallocate(). However this change is not optimal when using PREALLOC_MODE_FULL, since knowing the final size from the beginning could allow the file system driver to do less allocations and possibly avoid fragmentation of the file. Now we truncate also before doing full preallocation. Signed-off-by: Nir Soffer --- block/file-posix.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/block/file-posix.c b/block/file-posix.c index 442f080..d24e34b 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1604,6 +1604,17 @@ static int raw_create(const char *filename, QemuOpts *opts, Error **errp) #endif case PREALLOC_MODE_FULL: { + /* + * 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) != 0) { + result = -errno; + error_setg_errno(errp, -result, "Could not resize file"); + goto out_close; + } + int64_t num = 0, left = total_size; buf = g_malloc0(65536); @@ -1642,6 +1653,7 @@ static int raw_create(const char *filename, QemuOpts *opts, Error **errp) break; } +out_close: if (qemu_close(fd) != 0 && result == 0) { result = -errno; error_setg_errno(errp, -result, "Could not close the new file");