aboutsummaryrefslogtreecommitdiff
path: root/block/file-posix.c
diff options
context:
space:
mode:
authorNir Soffer <nirsof@gmail.com>2017-02-17 02:51:26 +0200
committerKevin Wolf <kwolf@redhat.com>2017-02-24 16:09:23 +0100
commit5a1dad9d5a5777c4883de78d9f35bd8141175798 (patch)
treea11964dfe24dbc4c7ef5fdc6c34abc4de325b187 /block/file-posix.c
parent6f993f3fca4763a6b494ff587d4400b6e8a5ef31 (diff)
qemu-img: Truncate before full preallocation
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>
Diffstat (limited to 'block/file-posix.c')
-rw-r--r--block/file-posix.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/block/file-posix.c b/block/file-posix.c
index 442f080eaa..d24e34be36 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");