From nobody Sat Apr 27 19:09:56 2024 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 1486151545576330.90920713760124; Fri, 3 Feb 2017 11:52:25 -0800 (PST) Received: from localhost ([::1]:36614 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZjuA-0000ec-Vw for importer@patchew.org; Fri, 03 Feb 2017 14:52:23 -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) 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) 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) 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" When using file system that does not support fallocate() (e.g. NFS < 4.2), truncating the file only when preallocation=3DOFF 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=3Dfalloc mnt/test 1g Formatting 'mnt/test', fmt=3Draw size=3D1073741824 preallocation=3Dfalloc real 0m21.185s user 0m0.022s sys 0m0.574s $ time ./qemu-img-fix create -f raw -o preallocation=3Dfalloc mnt/test 1g Formatting 'mnt/test', fmt=3Draw size=3D1073741824 preallocation=3Dfalloc real 0m11.601s user 0m0.016s sys 0m0.525s $ time dd if=3D/dev/zero of=3Dmnt/test bs=3D1M count=3D1024 oflag=3Ddirect 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=3Dfalloc mnt/test= 8192 ... pread64(9, "\0", 1, 4095) =3D 1 pwrite64(9, "\0", 1, 4095) =3D 1 pread64(9, "\0", 1, 8191) =3D 1 pwrite64(9, "\0", 1, 8191) =3D 1 $ strace ./qemu-img-fix create -f raw -o preallocation=3Dfalloc mnt/test 81= 92 ... pwrite64(9, "\0", 1, 4095) =3D 1 pwrite64(9, "\0", 1, 8191) =3D 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 } =20 - if (ftruncate(fd, total_size) !=3D 0) { - result =3D -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) !=3D 0) { + result =3D -errno; + error_setg_errno(errp, -result, "Could not resize file"); + } break; default: result =3D -EINVAL; @@ -1644,7 +1642,6 @@ static int raw_create(const char *filename, QemuOpts = *opts, Error **errp) break; } =20 -out_close: if (qemu_close(fd) !=3D 0 && result =3D=3D 0) { result =3D -errno; error_setg_errno(errp, -result, "Could not close the new file"); --=20 2.9.3