[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 75/85] block/file-posix: Extract raw_regular_truncate
From: |
Max Reitz |
Subject: |
[Qemu-devel] [PULL 75/85] block/file-posix: Extract raw_regular_truncate() |
Date: |
Tue, 11 Jul 2017 18:08:04 +0200 |
This functionality is part of raw_create() which we will be able to
reuse nicely in raw_truncate().
Signed-off-by: Max Reitz <address@hidden>
Reviewed-by: Stefan Hajnoczi <address@hidden>
Reviewed-by: Philippe Mathieu-Daudé <address@hidden>
Message-id: address@hidden
Signed-off-by: Max Reitz <address@hidden>
---
block/file-posix.c | 144 +++++++++++++++++++++++++++++------------------------
1 file changed, 78 insertions(+), 66 deletions(-)
diff --git a/block/file-posix.c b/block/file-posix.c
index 3388def..deefd6e 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -1624,6 +1624,81 @@ static void raw_close(BlockDriverState *bs)
}
}
+static int raw_regular_truncate(int fd, int64_t offset, PreallocMode prealloc,
+ Error **errp)
+{
+ int result = 0;
+ char *buf;
+
+ 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 if a
+ * block is allocated before allocating it, so don't do that here.
+ */
+ result = -posix_fallocate(fd, 0, offset);
+ if (result != 0) {
+ /* posix_fallocate() doesn't set errno. */
+ error_setg_errno(errp, -result,
+ "Could not preallocate data for the new file");
+ }
+ return result;
+#endif
+ case PREALLOC_MODE_FULL:
+ {
+ int64_t num = 0, left = offset;
+
+ /*
+ * 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) != 0) {
+ result = -errno;
+ error_setg_errno(errp, -result, "Could not resize file");
+ return result;
+ }
+
+ buf = g_malloc0(65536);
+
+ while (left > 0) {
+ num = MIN(left, 65536);
+ result = write(fd, buf, num);
+ if (result < 0) {
+ result = -errno;
+ error_setg_errno(errp, -result,
+ "Could not write to the new file");
+ break;
+ }
+ left -= result;
+ }
+ if (result >= 0) {
+ result = fsync(fd);
+ if (result < 0) {
+ result = -errno;
+ error_setg_errno(errp, -result,
+ "Could not flush new file to disk");
+ }
+ }
+ g_free(buf);
+ return result;
+ }
+ case PREALLOC_MODE_OFF:
+ if (ftruncate(fd, offset) != 0) {
+ result = -errno;
+ error_setg_errno(errp, -result, "Could not resize file");
+ }
+ return result;
+ default:
+ result = -ENOTSUP;
+ error_setg(errp, "Unsupported preallocation mode: %s",
+ PreallocMode_lookup[prealloc]);
+ return result;
+ }
+}
+
static int raw_truncate(BlockDriverState *bs, int64_t offset,
PreallocMode prealloc, Error **errp)
{
@@ -1892,72 +1967,9 @@ static int raw_create(const char *filename, QemuOpts
*opts, Error **errp)
#endif
}
- 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 if a
- * block is allocated before allocating it, so don't do that here.
- */
- result = -posix_fallocate(fd, 0, total_size);
- if (result != 0) {
- /* posix_fallocate() doesn't set errno. */
- error_setg_errno(errp, -result,
- "Could not preallocate data for the new file");
- }
- break;
-#endif
- case PREALLOC_MODE_FULL:
- {
- int64_t num = 0, left = total_size;
-
- /*
- * 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;
- }
-
- buf = g_malloc0(65536);
-
- while (left > 0) {
- num = MIN(left, 65536);
- result = write(fd, buf, num);
- if (result < 0) {
- result = -errno;
- error_setg_errno(errp, -result,
- "Could not write to the new file");
- break;
- }
- left -= result;
- }
- if (result >= 0) {
- result = fsync(fd);
- if (result < 0) {
- result = -errno;
- error_setg_errno(errp, -result,
- "Could not flush new file to disk");
- }
- }
- g_free(buf);
- 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 = -ENOTSUP;
- error_setg(errp, "Unsupported preallocation mode: %s",
- PreallocMode_lookup[prealloc]);
- break;
+ result = raw_regular_truncate(fd, total_size, prealloc, errp);
+ if (result < 0) {
+ goto out_close;
}
out_close:
--
2.9.4
- [Qemu-devel] [PULL 65/85] qcow2: extract image creation option parsing, (continued)
- [Qemu-devel] [PULL 65/85] qcow2: extract image creation option parsing, Max Reitz, 2017/07/11
- [Qemu-devel] [PULL 66/85] qcow2: add bdrv_measure() support, Max Reitz, 2017/07/11
- [Qemu-devel] [PULL 68/85] qemu-iotests: support per-format golden output files, Max Reitz, 2017/07/11
- [Qemu-devel] [PULL 67/85] qemu-img: add measure subcommand, Max Reitz, 2017/07/11
- [Qemu-devel] [PULL 57/85] iotests: chown LUKS device before qemu-io launches, Max Reitz, 2017/07/11
- [Qemu-devel] [PULL 70/85] block: Add PreallocMode to BD.bdrv_truncate(), Max Reitz, 2017/07/11
- [Qemu-devel] [PULL 71/85] block: Add PreallocMode to bdrv_truncate(), Max Reitz, 2017/07/11
- [Qemu-devel] [PULL 69/85] iotests: add test 178 for qemu-img measure, Max Reitz, 2017/07/11
- [Qemu-devel] [PULL 74/85] block/file-posix: Small fixes in raw_create(), Max Reitz, 2017/07/11
- [Qemu-devel] [PULL 72/85] block: Add PreallocMode to blk_truncate(), Max Reitz, 2017/07/11
- [Qemu-devel] [PULL 75/85] block/file-posix: Extract raw_regular_truncate(),
Max Reitz <=
- [Qemu-devel] [PULL 73/85] qemu-img: Expose PreallocMode for resizing, Max Reitz, 2017/07/11
- [Qemu-devel] [PULL 78/85] block/qcow2: Generalize preallocate(), Max Reitz, 2017/07/11
- [Qemu-devel] [PULL 77/85] block/file-posix: Preallocation for truncate, Max Reitz, 2017/07/11
- [Qemu-devel] [PULL 76/85] block/file-posix: Generalize raw_regular_truncate, Max Reitz, 2017/07/11
- [Qemu-devel] [PULL 79/85] block/qcow2: Lock s->lock in preallocate(), Max Reitz, 2017/07/11
- [Qemu-devel] [PULL 80/85] block/qcow2: Metadata preallocation for truncate, Max Reitz, 2017/07/11
- [Qemu-devel] [PULL 81/85] block/qcow2: Add qcow2_refcount_area(), Max Reitz, 2017/07/11
- [Qemu-devel] [PULL 82/85] block/qcow2: Rename "fail_block" to just "fail", Max Reitz, 2017/07/11
- [Qemu-devel] [PULL 83/85] block/qcow2: falloc/full preallocating growth, Max Reitz, 2017/07/11
- [Qemu-devel] [PULL 84/85] iotests: Add preallocated resize test for raw, Max Reitz, 2017/07/11