qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 06/19] qemu-img: Truncate before full preallocation
Date: Fri, 24 Feb 2017 19:16:57 +0100	[thread overview]
Message-ID: <1487960230-18054-7-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1487960230-18054-1-git-send-email-kwolf@redhat.com>

From: Nir Soffer <nirsof@gmail.com>

In a previous commit (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 <nirsof@gmail.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 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");
-- 
1.8.3.1

  parent reply	other threads:[~2017-02-24 18:17 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-24 18:16 [Qemu-devel] [PULL 00/19] Block layer patches Kevin Wolf
2017-02-24 18:16 ` [Qemu-devel] [PULL 01/19] qemu-iotests: Test 137 only supports 'file' protocol Kevin Wolf
2017-02-24 18:16 ` [Qemu-devel] [PULL 02/19] qemu-iotests: add ability to exclude certain protocols from tests Kevin Wolf
2017-02-24 18:16 ` [Qemu-devel] [PULL 03/19] qemu-iotests: redirect nbd server stdout to /dev/null Kevin Wolf
2017-02-24 18:16 ` [Qemu-devel] [PULL 04/19] qemu-img: Do not truncate before preallocation Kevin Wolf
2017-02-24 18:16 ` [Qemu-devel] [PULL 05/19] qemu-img: Add tests for raw image preallocation Kevin Wolf
2017-02-24 18:16 ` Kevin Wolf [this message]
2017-02-24 18:16 ` [Qemu-devel] [PULL 07/19] qemu-img: Improve documentation for PREALLOC_MODE_FALLOC Kevin Wolf
2017-02-24 18:16 ` [Qemu-devel] [PULL 08/19] iotests: Fix another race in 030 Kevin Wolf
2017-02-24 18:17 ` [Qemu-devel] [PULL 09/19] blockdev: Use BlockBackend to resize in qmp_block_resize() Kevin Wolf
2017-02-24 18:17 ` [Qemu-devel] [PULL 10/19] qcow2: Use BB for resizing in qcow2_amend_options() Kevin Wolf
2017-02-24 18:17 ` [Qemu-devel] [PULL 11/19] mirror: Resize active commit base in mirror_run() Kevin Wolf
2017-02-24 18:17 ` [Qemu-devel] [PULL 12/19] block: Pass BdrvChild to bdrv_truncate() Kevin Wolf
2017-02-24 18:17 ` [Qemu-devel] [PULL 13/19] block: Attach bs->file only during .bdrv_open() Kevin Wolf
2017-02-24 18:17 ` [Qemu-devel] [PULL 14/19] block: Factor out bdrv_open_child_bs() Kevin Wolf
2017-02-24 18:17 ` [Qemu-devel] [PULL 15/19] block: Use BlockBackend for image probing Kevin Wolf
2017-02-24 18:17 ` [Qemu-devel] [PULL 16/19] block: Factor out bdrv_open_driver() Kevin Wolf
2017-02-24 18:17 ` [Qemu-devel] [PULL 17/19] block: Add bdrv_new_open_driver() Kevin Wolf
2017-02-24 18:17 ` [Qemu-devel] [PULL 18/19] vvfat: Use opened node as backing file Kevin Wolf
2017-02-24 18:17 ` [Qemu-devel] [PULL 19/19] tests: Use opened block node for block job tests Kevin Wolf
2017-02-24 19:22 ` [Qemu-devel] [PULL 00/19] Block layer patches no-reply
2017-02-26 15:55 ` Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1487960230-18054-7-git-send-email-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).