From nobody Fri May 17 21:17:11 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1598882571; cv=none; d=zohomail.com; s=zohoarc; b=OdP/MIMkThd1ka5U7kfc75YT/7lxeMrPBcgwAd4lCh2BFS2MjzAvTgx5pYnfiDG8M7/cStKxSP8oxtSx8Ff0HagX2Wo//EyhNmcuwmeoJdobOHklR99BCbiAEDfe9KiA4sReKAnOuqpfdIiQ+is85V30VCL/NWiBakG1JcGf2G0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598882571; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Hps0WnAMGV8M6tEIQQs0UxGyHLY9Kbi1+pTVoL39GFw=; b=m043JMB4DA8BObpP20ukTY5URdtObSHyfzfghbRcYN/ZfcHKTtaPgyFSQjkpXD6UkWEwy0FTSNn5nqg/9JuRQsqpFfSLUfpT+2rbXEAwf360fwu0qJZpgI3EvwaeatXB8Bo4Ay5YQFRBzWhtwebLZgmo9M4M4XYlpebdN/QMEZc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598882571496967.8150752279031; Mon, 31 Aug 2020 07:02:51 -0700 (PDT) Received: from localhost ([::1]:48918 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kCkOY-0002Zt-2X for importer@patchew.org; Mon, 31 Aug 2020 10:02:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58638) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kCkNQ-0001YW-Me; Mon, 31 Aug 2020 10:01:40 -0400 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]:33315) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kCkNN-0005Re-5y; Mon, 31 Aug 2020 10:01:40 -0400 Received: by mail-wm1-x342.google.com with SMTP id e11so84919wme.0; Mon, 31 Aug 2020 07:01:35 -0700 (PDT) Received: from localhost.localdomain (109-186-204-55.bb.netvision.net.il. [109.186.204.55]) by smtp.gmail.com with ESMTPSA id q192sm12413320wme.13.2020.08.31.07.01.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Aug 2020 07:01:32 -0700 (PDT) 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 :mime-version:content-transfer-encoding; bh=Hps0WnAMGV8M6tEIQQs0UxGyHLY9Kbi1+pTVoL39GFw=; b=tZE69HrtZHhZJop3qKm6VkCuCFG3EgzbVV3F5YFXFTgD4+uvT6b6kkENwqeeXiZs+i sz+lLwXl1R7WWBH8we70b3RYVMFHmvFn2U7NwTwMFmZyhjzL2jUZTYVc7IOMMmoL9iBz YT9A8QPlfVK7IDHVSHozeogKMsBlxdxGXM7NcG/WnVdrj4dNAAQcEnP1oy4f4+ehI10t qhf1ppGOfugeodmWeC3OisB53nx6vYihSiK+F6P8FmH/b1AkuU8/GJQDHBO13HDoBy03 hGz9p7NO0L3+VrLIoFgOalfCxpsmGGWLBs2uF5REyj6z5jgwyzv5Mz53sZY4WiBR63C9 jUXw== 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:mime-version:content-transfer-encoding; bh=Hps0WnAMGV8M6tEIQQs0UxGyHLY9Kbi1+pTVoL39GFw=; b=UE8LCgHxKKXVTxbyZPoIdYFbKo6Jex8KzttgjkGp0+m2AYlgR5Ejq2D8S5x4VfxOYG Fwb4AR5Dl4USmpwng+3wmn4BwtWS6mOTdidQIBwseicu2DQGrurS0R8AEnBdPnuFrz/X zn450lf91PWrLH5rDWLIyUxWKwi1JyDv0W0W3ujYScdKQPvsb666ky+OB2lt98yQ+0Em qKo70Tz8MVVVx2A2QQPsfHxcGIuVW9bhRuUFu9yHf5EAKgTQrg8E3CCbupAxqQ5Ah350 97IwQse++tUEt6psKJ20J5u0wtQmqabqUgOu0+TFDQiLiJijp1bKJ6HHk6jlUezoH21O yEAA== X-Gm-Message-State: AOAM533hmDNDmf+u8bmZN+IIw/oxtEbEAgQyis37n1wZnThEcWIOX8nc 36Ny9Sff+9ghHzTQO3tJUfC6ZcK2qAyjxEzW X-Google-Smtp-Source: ABdhPJyfqFcdV1tEWieRwTbPJ6mR4u7sPtO6Zx27N+gM6P1Ao1HVZGQ9FH6/wc0JWcp+g/QgC5rZDg== X-Received: by 2002:a7b:c5d0:: with SMTP id n16mr1457264wmk.7.1598882493577; Mon, 31 Aug 2020 07:01:33 -0700 (PDT) From: Nir Soffer X-Google-Original-From: Nir Soffer To: qemu-devel@nongnu.org Subject: [PATCH 1/2] block: file-posix: Extract preallocate helpers Date: Mon, 31 Aug 2020 17:01:26 +0300 Message-Id: <20200831140127.657134-2-nsoffer@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200831140127.657134-1-nsoffer@redhat.com> References: <20200831140127.657134-1-nsoffer@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::342; envelope-from=nirsof@gmail.com; helo=mail-wm1-x342.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-block@nongnu.org, Markus Armbruster , Max Reitz , Nir Soffer Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" handle_aiocb_truncate() was too big and complex, implementing 3 different preallocation modes. In a future patch I want to introduce a fallback from "falloc" to "full"; it will be too messy and error prone with the current code. Extract a helper for each of the preallocation modes (falloc, full, off) and leave only the common preparation and cleanup code in handle_aiocb_truncate(). Signed-off-by: Nir Soffer Reviewed-by: Alberto Garcia --- block/file-posix.c | 206 ++++++++++++++++++++++++++------------------- 1 file changed, 120 insertions(+), 86 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index 9a00d4190a..341ffb1cb4 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1832,12 +1832,128 @@ static int allocate_first_block(int fd, size_t max= _size) return ret; } =20 +static int preallocate_falloc(int fd, int64_t current_length, int64_t offs= et, + Error **errp) +{ +#ifdef CONFIG_POSIX_FALLOCATE + int result; + + if (offset =3D=3D current_length) + return 0; + + /* + * Truncating before posix_fallocate() makes it about twice slower on + * file systems that do not support fallocate(), trying to check if a + * block is allocated before allocating it, so don't do that here. + */ + + result =3D -posix_fallocate(fd, current_length, + offset - current_length); + if (result !=3D 0) { + /* posix_fallocate() doesn't set errno. */ + error_setg_errno(errp, -result, + "Could not preallocate new data"); + return result; + } + + if (current_length =3D=3D 0) { + /* + * posix_fallocate() uses fallocate() if the filesystem supports + * it, or fallback to manually writing zeroes. If fallocate() + * was used, unaligned reads from the fallocated area in + * raw_probe_alignment() will succeed, hence we need to allocate + * the first block. + * + * Optimize future alignment probing; ignore failures. + */ + allocate_first_block(fd, offset); + } + + return 0; +#else + return -ENOTSUP; +#endif +} + +static int preallocate_full(int fd, int64_t current_length, int64_t offset, + Error **errp) +{ + int64_t num =3D 0, left =3D offset - current_length; + off_t seek_result; + int result; + char *buf =3D NULL; + + /* + * 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, offset) !=3D 0) { + result =3D -errno; + error_setg_errno(errp, -result, "Could not resize file"); + goto out; + } + + buf =3D g_malloc0(65536); + + seek_result =3D lseek(fd, current_length, SEEK_SET); + if (seek_result < 0) { + result =3D -errno; + error_setg_errno(errp, -result, + "Failed to seek to the old end of file"); + goto out; + } + + while (left > 0) { + num =3D MIN(left, 65536); + result =3D write(fd, buf, num); + if (result < 0) { + if (errno =3D=3D EINTR) { + continue; + } + result =3D -errno; + error_setg_errno(errp, -result, + "Could not write zeros for preallocation"); + goto out; + } + left -=3D result; + } + + result =3D fsync(fd); + if (result < 0) { + result =3D -errno; + error_setg_errno(errp, -result, "Could not flush file to disk"); + goto out; + } + +out: + g_free(buf); + + return result; +} + +static int preallocate_off(int fd, int64_t current_length, int64_t offset, + Error **errp) +{ + if (ftruncate(fd, offset) !=3D 0) { + int result =3D -errno; + error_setg_errno(errp, -result, "Could not resize file"); + return result; + } + + if (current_length =3D=3D 0 && offset > current_length) { + /* Optimize future alignment probing; ignore failures. */ + allocate_first_block(fd, offset); + } + + return 0; +} + static int handle_aiocb_truncate(void *opaque) { RawPosixAIOData *aiocb =3D opaque; int result =3D 0; int64_t current_length =3D 0; - char *buf =3D NULL; struct stat st; int fd =3D aiocb->aio_fildes; int64_t offset =3D aiocb->aio_offset; @@ -1859,95 +1975,14 @@ static int handle_aiocb_truncate(void *opaque) switch (prealloc) { #ifdef CONFIG_POSIX_FALLOCATE case PREALLOC_MODE_FALLOC: - /* - * Truncating before posix_fallocate() makes it about twice slower= on - * file systems that do not support fallocate(), trying to check i= f a - * block is allocated before allocating it, so don't do that here. - */ - if (offset !=3D current_length) { - result =3D -posix_fallocate(fd, current_length, - offset - current_length); - if (result !=3D 0) { - /* posix_fallocate() doesn't set errno. */ - error_setg_errno(errp, -result, - "Could not preallocate new data"); - } else if (current_length =3D=3D 0) { - /* - * posix_fallocate() uses fallocate() if the filesystem - * supports it, or fallback to manually writing zeroes. If - * fallocate() was used, unaligned reads from the fallocat= ed - * area in raw_probe_alignment() will succeed, hence we ne= ed to - * allocate the first block. - * - * Optimize future alignment probing; ignore failures. - */ - allocate_first_block(fd, offset); - } - } else { - result =3D 0; - } + result =3D preallocate_falloc(fd, current_length, offset, errp); goto out; #endif case PREALLOC_MODE_FULL: - { - int64_t num =3D 0, left =3D offset - current_length; - off_t seek_result; - - /* - * 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, offset) !=3D 0) { - result =3D -errno; - error_setg_errno(errp, -result, "Could not resize file"); - goto out; - } - - buf =3D g_malloc0(65536); - - seek_result =3D lseek(fd, current_length, SEEK_SET); - if (seek_result < 0) { - result =3D -errno; - error_setg_errno(errp, -result, - "Failed to seek to the old end of file"); - goto out; - } - - while (left > 0) { - num =3D MIN(left, 65536); - result =3D write(fd, buf, num); - if (result < 0) { - if (errno =3D=3D EINTR) { - continue; - } - result =3D -errno; - error_setg_errno(errp, -result, - "Could not write zeros for preallocation"= ); - goto out; - } - left -=3D result; - } - if (result >=3D 0) { - result =3D fsync(fd); - if (result < 0) { - result =3D -errno; - error_setg_errno(errp, -result, - "Could not flush file to disk"); - goto out; - } - } + result =3D preallocate_full(fd, current_length, offset, errp); goto out; - } case PREALLOC_MODE_OFF: - if (ftruncate(fd, offset) !=3D 0) { - result =3D -errno; - error_setg_errno(errp, -result, "Could not resize file"); - } else if (current_length =3D=3D 0 && offset > current_length) { - /* Optimize future alignment probing; ignore failures. */ - allocate_first_block(fd, offset); - } - return result; + return preallocate_off(fd, current_length, offset, errp); default: result =3D -ENOTSUP; error_setg(errp, "Unsupported preallocation mode: %s", @@ -1963,7 +1998,6 @@ out: } } =20 - g_free(buf); return result; } =20 --=20 2.26.2 From nobody Fri May 17 21:17:11 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1598882661; cv=none; d=zohomail.com; s=zohoarc; b=LbPJv5tF34mlQnUBiKBR5WP0Lc7y5MrVCi2TUZnivCMP5lFWICuaPCmRqXMm9VFdx7cBgc23QKuGzTCMuSIJo2eNDvIeKOPfrkKbnanQSd1/dcwpo8nP06EWDtgMHQLLjHUFHBb2EWnVgPbyYPW8MK4g2iLzq9aOemLYIr353ew= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598882661; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=CxVi3AwzX6wIEGts+Qx2coeo7yCmSP7/IF+d/L8k3BM=; b=RXqHSh1sRm2x06heBE0XSgwEZyivLzRR8J5Omc1++cfJPZCtkk9DHQeffwKT/4Us3VFx+lhsObNuXDJ8G+stb4W/dT2S1LCIDsUFgyqzWhr+4kExzl1swcsud8Zp1Xxo0VWpX7rgF+TJzBw2Ww109PnJoT+RYL9FY4yC8cqNGPU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598882661269595.5382259683042; Mon, 31 Aug 2020 07:04:21 -0700 (PDT) Received: from localhost ([::1]:52262 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kCkPz-0003yz-U5 for importer@patchew.org; Mon, 31 Aug 2020 10:04:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58646) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kCkNR-0001Z0-2U; Mon, 31 Aug 2020 10:01:41 -0400 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]:35789) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kCkNO-0005Rq-LS; Mon, 31 Aug 2020 10:01:40 -0400 Received: by mail-wm1-x344.google.com with SMTP id o21so5427720wmc.0; Mon, 31 Aug 2020 07:01:37 -0700 (PDT) Received: from localhost.localdomain (109-186-204-55.bb.netvision.net.il. [109.186.204.55]) by smtp.gmail.com with ESMTPSA id q192sm12413320wme.13.2020.08.31.07.01.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Aug 2020 07:01:35 -0700 (PDT) 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 :mime-version:content-transfer-encoding; bh=CxVi3AwzX6wIEGts+Qx2coeo7yCmSP7/IF+d/L8k3BM=; b=c7rPsJjWpEeXVO4uVAIk79AqHacG3klAN6NI5+BuOI/rxSL4zkvTh3a51wgdLzeAZR OGLEv7Vb3tawX9GfwkXS502hXz6rAS35VQXDFyBTWr75gElHlLKTX58F3+cxXRgZg0h0 +zIkirU/TqyZmfVtREHc7DWwatAnF4j/MW/uDFlVL+DYum1BPomL5cg13MtbM9BhFfz0 qA6qCs6h8lwtyagxZbwAwA3yl8OTLT4EaP7/J9924eCnbSoDJMpmnPMG5Q+LW2uyRrn7 D07gRQ0AMqW2yhAmnwYQKU4K9Bib3wPt0W8QPoINSKQd5RG2HxFZ1SzTGfxVjGUyUJpc BavA== 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:mime-version:content-transfer-encoding; bh=CxVi3AwzX6wIEGts+Qx2coeo7yCmSP7/IF+d/L8k3BM=; b=Pr5nVDFgjZ+SwGQtpVrl6plcDRFv7kZ4bZLdwFBHPnKxrKgfnmt/BBxozAIVnC7fQF CKYqa80sv0C6rU6VwIS9VxlxLewiYIFM2M5VqNhTcvS266ATq84jrMJRU2yi0816qhNE XUYesQAZ1j4u9rrPM8yGKk7Kx2XK35i4oYcNm2u+p625+fY8CqCgDIh+5dPfRndV7Bg3 T3V6uNrPHFzLWqQ/A/qw2/yGKckA6aF9St1pWNb2Sqp7g/MBenDwnhMs5Q5AJ5MoEroH XHGPRxeiKZ6JNceDm6TugLvgnRsyfLS16L/Bnu4GwRFvdELSoFPTGwTw3iqRuR09k/5S 62Tw== X-Gm-Message-State: AOAM533Kia34u8mIa9cD4Wg7i238pctJSBHYyMy5pvSUA6O0/iLcoWbn KFRneTYvsbQdSEIGpthdzEsbrv288qG866mO X-Google-Smtp-Source: ABdhPJxkaHCLMG1nHD1HOldcLB3hqbSOgp1OTw5zi8G4B6rAV/Bhxd2IPjhUcQXLxgg/ptlYCE+kXA== X-Received: by 2002:a7b:c055:: with SMTP id u21mr1423972wmc.64.1598882495596; Mon, 31 Aug 2020 07:01:35 -0700 (PDT) From: Nir Soffer X-Google-Original-From: Nir Soffer To: qemu-devel@nongnu.org Subject: [PATCH 2/2] block: file-posix: Replace posix_fallocate with fallocate Date: Mon, 31 Aug 2020 17:01:27 +0300 Message-Id: <20200831140127.657134-3-nsoffer@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200831140127.657134-1-nsoffer@redhat.com> References: <20200831140127.657134-1-nsoffer@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::344; envelope-from=nirsof@gmail.com; helo=mail-wm1-x344.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-block@nongnu.org, Markus Armbruster , Max Reitz , Nir Soffer Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) If fallocate() is not supported, posix_fallocate() falls back to inefficient allocation, writing one byte for every 4k bytes[1]. This is very slow compared with writing zeros. In oVirt we measured ~400% improvement in allocation time when replacing posix_fallocate() with manually writing zeroes[2]. We also know that posix_fallocated() does not work well when using OFD locks[3]. We don't know the reason yet for this issue yet. Change preallocate_falloc() to use fallocate() instead of posix_falloate(), and fall back to full preallocation if not supported. Here are quick test results with this change. Before (qemu-img-5.1.0-2.fc32.x86_64): $ time qemu-img create -f raw -o preallocation=3Dfalloc /tmp/nfs3/test.raw = 6g Formatting '/tmp/nfs3/test.raw', fmt=3Draw size=3D6442450944 preallocation= =3Dfalloc real 0m42.100s user 0m0.602s sys 0m4.137s NFS stats: calls =C2=A0 =C2=A0 =C2=A0retrans =C2=A0 =C2=A0authrefrsh write 1571583 =C2=A0 =C2=A00 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A01572205 1571= 321 After: $ time ./qemu-img create -f raw -o preallocation=3Dfalloc /tmp/nfs3/test.ra= w 6g Formatting '/tmp/nfs3/test.raw', fmt=3Draw size=3D6442450944 preallocation= =3Dfalloc real 0m15.551s user 0m0.070s sys 0m2.623s NFS stats: calls =C2=A0 =C2=A0 =C2=A0retrans =C2=A0 =C2=A0authrefrsh write 24620 =C2=A0 =C2=A0 =C2=A00 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A024624 = 24567 =C2=A0 [1] https://code.woboq.org/userspace/glibc/sysdeps/posix/posix_fallocate.c.= html#96 [2] https://bugzilla.redhat.com/1850267#c25 [3] https://bugzilla.redhat.com/1851097 Signed-off-by: Nir Soffer Reviewed-by: Alberto Garcia --- block/file-posix.c | 32 +++++++++----------------- docs/system/qemu-block-drivers.rst.inc | 11 +++++---- docs/tools/qemu-img.rst | 11 +++++---- qapi/block-core.json | 4 ++-- 4 files changed, 25 insertions(+), 33 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index 341ffb1cb4..eac3c0b412 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1835,36 +1835,24 @@ static int allocate_first_block(int fd, size_t max_= size) static int preallocate_falloc(int fd, int64_t current_length, int64_t offs= et, Error **errp) { -#ifdef CONFIG_POSIX_FALLOCATE +#ifdef CONFIG_FALLOCATE int result; =20 if (offset =3D=3D current_length) return 0; =20 - /* - * Truncating before posix_fallocate() makes it about twice slower on - * file systems that do not support fallocate(), trying to check if a - * block is allocated before allocating it, so don't do that here. - */ - - result =3D -posix_fallocate(fd, current_length, - offset - current_length); + result =3D do_fallocate(fd, 0, current_length, offset - current_length= ); if (result !=3D 0) { - /* posix_fallocate() doesn't set errno. */ - error_setg_errno(errp, -result, - "Could not preallocate new data"); + error_setg_errno(errp, -result, "Could not preallocate new data"); return result; } =20 if (current_length =3D=3D 0) { /* - * posix_fallocate() uses fallocate() if the filesystem supports - * it, or fallback to manually writing zeroes. If fallocate() - * was used, unaligned reads from the fallocated area in - * raw_probe_alignment() will succeed, hence we need to allocate - * the first block. + * Unaligned reads from the fallocated area in raw_probe_alignment= () + * will succeed, hence we need to allocate the first block. * - * Optimize future alignment probing; ignore failures. + * Optimizes future alignment probing; ignore failures. */ allocate_first_block(fd, offset); } @@ -1973,10 +1961,12 @@ static int handle_aiocb_truncate(void *opaque) } =20 switch (prealloc) { -#ifdef CONFIG_POSIX_FALLOCATE +#ifdef CONFIG_FALLOCATE case PREALLOC_MODE_FALLOC: result =3D preallocate_falloc(fd, current_length, offset, errp); - goto out; + if (result !=3D -ENOTSUP) + goto out; + /* If fallocate() is not supported, fallback to full preallocation= . */ #endif case PREALLOC_MODE_FULL: result =3D preallocate_full(fd, current_length, offset, errp); @@ -3080,7 +3070,7 @@ static QemuOptsList raw_create_opts =3D { .name =3D BLOCK_OPT_PREALLOC, .type =3D QEMU_OPT_STRING, .help =3D "Preallocation mode (allowed values: off" -#ifdef CONFIG_POSIX_FALLOCATE +#ifdef CONFIG_FALLOCATE ", falloc" #endif ", full)" diff --git a/docs/system/qemu-block-drivers.rst.inc b/docs/system/qemu-bloc= k-drivers.rst.inc index b052a6d14e..8e4acf397e 100644 --- a/docs/system/qemu-block-drivers.rst.inc +++ b/docs/system/qemu-block-drivers.rst.inc @@ -25,11 +25,12 @@ This section describes each format and the options that= are supported for it. .. program:: raw .. option:: preallocation =20 - Preallocation mode (allowed values: ``off``, ``falloc``, - ``full``). ``falloc`` mode preallocates space for image by - calling ``posix_fallocate()``. ``full`` mode preallocates space - for image by writing data to underlying storage. This data may or - may not be zero, depending on the storage location. + Preallocation mode (allowed values: ``off``, ``falloc``, ``full``). + ``falloc`` mode preallocates space for image by calling + ``fallocate()``, and falling back to ``full` mode if not supported. + ``full`` mode preallocates space for image by writing data to + underlying storage. This data may or may not be zero, depending on + the storage location. =20 .. program:: image-formats .. option:: qcow2 diff --git a/docs/tools/qemu-img.rst b/docs/tools/qemu-img.rst index c35bd64822..a2089bd1b7 100644 --- a/docs/tools/qemu-img.rst +++ b/docs/tools/qemu-img.rst @@ -750,11 +750,12 @@ Supported image file formats: Supported options: =20 ``preallocation`` - Preallocation mode (allowed values: ``off``, ``falloc``, - ``full``). ``falloc`` mode preallocates space for image by - calling ``posix_fallocate()``. ``full`` mode preallocates space - for image by writing data to underlying storage. This data may or - may not be zero, depending on the storage location. + Preallocation mode (allowed values: ``off``, ``falloc``, ``full``). + ``falloc`` mode preallocates space for image by calling + ``fallocate()``, and falling back to ``full` mode if not supported. + ``full`` mode preallocates space for image by writing data to + underlying storage. This data may or may not be zero, depending on + the storage location. =20 ``qcow2`` =20 diff --git a/qapi/block-core.json b/qapi/block-core.json index db08c58d78..681d79ec63 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -5021,8 +5021,8 @@ # # @off: no preallocation # @metadata: preallocate only for metadata -# @falloc: like @full preallocation but allocate disk space by -# posix_fallocate() rather than writing data. +# @falloc: try to allocate disk space by fallocate(), and fallback to +# @full preallocation if not supported. # @full: preallocate all data by writing it to the device to ensure # disk space is really available. This data may or may not be # zero, depending on the image format and storage. --=20 2.26.2