From patchwork Fri Feb 3 19:50:37 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nir Soffer X-Patchwork-Id: 723903 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3vFSFK0zmYz9s7D for ; Sat, 4 Feb 2017 06:52:20 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="CS8XZV1T"; dkim-atps=neutral Received: from localhost ([::1]:36614 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZjtw-0000ec-RP for incoming@patchwork.ozlabs.org; Fri, 03 Feb 2017 14:52:08 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41694) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZjt0-0008Jl-Ko for qemu-devel@nongnu.org; Fri, 03 Feb 2017 14:51:11 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cZjsx-0004T1-JU for qemu-devel@nongnu.org; Fri, 03 Feb 2017 14:51:10 -0500 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]:34886) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cZjsx-0004SO-Cl; Fri, 03 Feb 2017 14:51:07 -0500 Received: by mail-wm0-x241.google.com with SMTP id u63so6583872wmu.2; Fri, 03 Feb 2017 11:51:07 -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=839aKANennZMSTsDRjyBiyqn8N4R1Tc/BPaZDZ6/Fqk=; b=CS8XZV1TMw0rQ7H67rlVStlQi34MDB5Qobc28XqDECYDpi2lflnloIM351fmybuV/A VtfCwavCaqhh2z6YHj2JlHHtnGaq6DBlpg5e+O2rdA0NR8Qd4yJ303mCYjQVF5+zWbry LaLLHitR42PXF9BGWvpFotpZH7qJtAKTTxasP3uNV4P4mn8j0IbWgUKtSK3JeLVSKG/R oGEDHL7mgAsJSRPcxnw7c3xTQS2oDQhhdDuJu7ovNeHScLanKGJiF7eBDw7JMT9+SqbU ovE5Ptkrq/bDbimv+/iDUPr3pbdfDZLLPPMVWkcT7pv5cUvuBmQPV/s7ZIaxe1Ov1Yrj 0WgQ== 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=839aKANennZMSTsDRjyBiyqn8N4R1Tc/BPaZDZ6/Fqk=; b=qjjVCZWmeGAK8s3YSZzJLVHuREA7/E77hBaY0HsJiIx9YB0rEl6jpc7nWLmmMRlTHa V/VerV+9GU/Va6of54qeMq6mLwLmJowiwWb4hYk9XhjzXkVlQ6AVvytTvlK37uteamQ6 43I9l5s0XX7gysUrKkIHyx+iH/zoh2r6sGuzsYLrgRJyICtiO4R0S1ZeAsMocvBB21Pf TKXWcH4t/Z2cJm6fRx77uOWFaHaxg9/VgRBaqMRUCUdlJsVOVyYwRXal0NuxZ1plR6cm sXog2ti6+H9WOlF+Ga6iOUyaXNCGza1W9sPosyB3H6pu2TuiKZaaNLfD0as+aCkakaRQ WTBA== X-Gm-Message-State: AMke39ltsT9W7p9Zl41nRIIXpc+SDAlHTqiEuDqcaV4AJvDPq4Q3qyWHJhAucwAIZJgfxQ== X-Received: by 10.28.199.206 with SMTP id x197mr2727172wmf.5.1486151466079; Fri, 03 Feb 2017 11:51:06 -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 a13sm4546879wma.13.2017.02.03.11.51.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 03 Feb 2017 11:51:05 -0800 (PST) From: Nir Soffer To: qemu-devel@nongnu.org Date: Fri, 3 Feb 2017 21:50:37 +0200 Message-Id: <20170203195037.4238-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::241 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: kwolf@redhat.com, Nir Soffer , qemu-block@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" When using file system that does not support fallocate() (e.g. NFS < 4.2), truncating the file only when preallocation=OFF speeds up creating raw file. Here is example run, tested on Fedora 24 machine, creating raw file on NFS version 3 server. $ time ./qemu-img-master create -f raw -o preallocation=falloc mnt/test 1g Formatting 'mnt/test', fmt=raw size=1073741824 preallocation=falloc real 0m21.185s user 0m0.022s sys 0m0.574s $ time ./qemu-img-fix create -f raw -o preallocation=falloc mnt/test 1g Formatting 'mnt/test', fmt=raw size=1073741824 preallocation=falloc real 0m11.601s user 0m0.016s sys 0m0.525s $ time dd if=/dev/zero of=mnt/test bs=1M count=1024 oflag=direct 1024+0 records in 1024+0 records out 1073741824 bytes (1.1 GB, 1.0 GiB) copied, 15.6627 s, 68.6 MB/s real 0m16.104s user 0m0.009s sys 0m0.220s Running with strace we can see that without this change we do one pread() and one pwrite() for each block. With this change, we do only one pwrite() per block. $ strace ./qemu-img-master 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 This happens because posix_fallocate is checking if each block is allocated before writing a byte to the block, and when truncating the file before preallocation, all blocks are unallocated. Signed-off-by: Nir Soffer --- I sent this a week ago: http://lists.nongnu.org/archive/html/qemu-devel/2017-01/msg06123.html Sending again with improved commit message. 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 2134e0e..442f080 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1591,12 +1591,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: @@ -1636,6 +1630,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; @@ -1644,7 +1642,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");