From patchwork Fri Jan 27 23:45:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nir Soffer X-Patchwork-Id: 9542881 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 35FEB601D7 for ; Fri, 27 Jan 2017 23:46:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2033E26861 for ; Fri, 27 Jan 2017 23:46:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 112A627F99; Fri, 27 Jan 2017 23:46:36 +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 E75AC26861 for ; Fri, 27 Jan 2017 23:46:33 +0000 (UTC) Received: from localhost ([::1]:49349 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cXGDw-0004P3-97 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 27 Jan 2017 18:46:32 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56303) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cXGDg-0004Ol-6R for qemu-devel@nongnu.org; Fri, 27 Jan 2017 18:46:17 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cXGDd-0005nI-3F for qemu-devel@nongnu.org; Fri, 27 Jan 2017 18:46:16 -0500 Received: from mail-wm0-x242.google.com ([2a00:1450:400c:c09::242]:34450) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cXGDc-0005mw-Sn for qemu-devel@nongnu.org; Fri, 27 Jan 2017 18:46:13 -0500 Received: by mail-wm0-x242.google.com with SMTP id c85so61519680wmi.1 for ; Fri, 27 Jan 2017 15:46:11 -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; bh=pZFOjQA+kdw3NmFp9hnwF01Zr+KwSySU6opZNy81IKQ=; b=rULY/XF5pDwzrSzM+d435hDDESDqcVn2a4sZb0/FWxC/PjnxEOEXVK9j6v+YYltXNw CW9c8CUw7qzW65tMXleZap7jmckYvyxnsyOswrkIs5JOU94ffHw8gJRGW0RbcixH4f++ aCgIQxjWBtjKQWIwzwIhackuWrdJiDYwGjCQqAfIt5WsG1MjNHslwqZkmwvgNXeE13/Y 2pT+OBh8Y8kBtYtio31vUMbYuGq7r01sR3Rds2f72GGlcNH94QuyrsdaUS00axPCBKqv rKjBCEjfOE71ExcyDBH1HGrASEeCHC1cGOxbRKCk3VKGO3Z7leYSVBybBShZ9LGgO85O l0eg== 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; bh=pZFOjQA+kdw3NmFp9hnwF01Zr+KwSySU6opZNy81IKQ=; b=SW02N9MoAR75yDNMQKK50NB2bw0Se2LrC7r+rrISgaAA1OyGNi4IuCJmH2Ki9Ldxkv bRy4Z2hGLGbCeiQKm4C5G8OeaVsWQ1eIZY+o5GPkmzaw7NSggll6mQFSpZ5x8WxpiOWE uouHNopW0qb7oacYea2OhETiekmdHDToPSdqbEzXetxBEd5kyTIPt7ERQRfDQEX8m8BW HvJQIfoGtWsewt/7MSa9IeWTO+KUe7P54Dvk3QeylyJEcpsavLXjRWLyn7Q3l56m4oSV ZsBM8echtit++7XxzIPZaUMhYW8yC8AzUw+K7OLCNCdmZad1g3fUtObCiN4K5fSVVPNp Stiw== X-Gm-Message-State: AIkVDXKFPT2S7WtLbqLIjARInodeY3ufNpUw/IwOp77d3Cm4zpaPJffVeRWGoQDeDBePpA== X-Received: by 10.223.151.205 with SMTP id t13mr9465783wrb.9.1485560769913; Fri, 27 Jan 2017 15:46:09 -0800 (PST) Received: from lean.com (bzq-79-181-80-51.red.bezeqint.net. [79.181.80.51]) by smtp.gmail.com with ESMTPSA id j18sm10189429wrb.33.2017.01.27.15.46.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 27 Jan 2017 15:46:09 -0800 (PST) From: Nir Soffer To: qemu-devel@nongnu.org Date: Sat, 28 Jan 2017 01:45:54 +0200 Message-Id: <20170127234554.1196-1-nirsof@gmail.com> X-Mailer: git-send-email 2.9.3 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::242 Subject: [Qemu-devel] [PATCH] qemu-img: Do not truncate before 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: Kevin Wolf , Nir Soffer , Nir Soffer Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Nir Soffer When using file system that does not support fallocate(), posix_fallocate() fallback to emulation mode. In this mode, when preallocating blocks before file end, posix_preallocate is calling one pread() and one pwrite() per block. But when preallocation blocks after file end, it calls only one pwrite per block. Truncating the file only when preallocation=OFF speeds up creating raw file in this situation. Here are example run with without and with this change, tested on Fedora 25 VM, creating a raw image on NFS version 3 mount over 1G nic: $ time ./qemu-img create -f raw -o preallocation=falloc mnt/test 1g Formatting 'mnt/test', fmt=raw size=1073741824 preallocation=falloc real 0m17.083s user 0m0.020s sys 0m0.404s $ rm mnt/test $ time ./qemu-img create -f raw -o preallocation=falloc mnt/test 1g Formatting 'mnt/test', fmt=raw size=1073741824 preallocation=falloc real 0m12.372s user 0m0.020s sys 0m0.376s $ strace ./qemu-img-up create -f raw -o preallocation=falloc mnt/test 8192 ... pread64(9, "\0", 1, 4095) = 1 pwrite64(9, "\0", 1, 4095) = 1 pread64(9, "\0", 1, 8191) = 1 pwrite64(9, "\0", 1, 8191) = 1 $ strace ./qemu-img-fix create -f raw -o preallocation=falloc mnt/test 8192 ... pwrite64(9, "\0", 1, 4095) = 1 pwrite64(9, "\0", 1, 8191) = 1 Signed-off-by: Nir Soffer --- block/file-posix.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index 28b47d9..d7f6129 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1588,12 +1588,6 @@ static int raw_create(const char *filename, QemuOpts *opts, Error **errp) #endif } - if (ftruncate(fd, total_size) != 0) { - result = -errno; - error_setg_errno(errp, -result, "Could not resize file"); - goto out_close; - } - switch (prealloc) { #ifdef CONFIG_POSIX_FALLOCATE case PREALLOC_MODE_FALLOC: @@ -1633,6 +1627,10 @@ static int raw_create(const char *filename, QemuOpts *opts, Error **errp) break; } case PREALLOC_MODE_OFF: + if (ftruncate(fd, total_size) != 0) { + result = -errno; + error_setg_errno(errp, -result, "Could not resize file"); + } break; default: result = -EINVAL; @@ -1641,7 +1639,6 @@ 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");