9ae3a8
From 9628ef39a4866417b7c2dc5af81ba12bfeb33630 Mon Sep 17 00:00:00 2001
9ae3a8
From: Jeffrey Cody <jcody@redhat.com>
9ae3a8
Date: Tue, 16 Sep 2014 20:11:51 +0200
9ae3a8
Subject: [PATCH 13/20] block: vdi - use block layer ops in vdi_create, instead of posix calls
9ae3a8
9ae3a8
Message-id: <f49d9ed2f027b0bee1a3fa4383cfa553d71e54bf.1410897407.git.jcody@redhat.com>
9ae3a8
Patchwork-id: 61217
9ae3a8
O-Subject: [PATCH qemu-kvm-rhel RHEL7.1 12/15] block: vdi - use block layer ops in vdi_create, instead of posix calls
9ae3a8
Bugzilla: 1098086
9ae3a8
RH-Acked-by: Fam Zheng <famz@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
RH-Acked-by: Max Reitz <mreitz@redhat.com>
9ae3a8
9ae3a8
Use the block layer to create, and write to, the image file in the
9ae3a8
VDI .bdrv_create() operation.
9ae3a8
9ae3a8
This has a couple of benefits: Images can now be created over protocols,
9ae3a8
and hacks such as NOCOW are not needed in the image format driver, and
9ae3a8
the underlying file protocol appropriate for the host OS can be relied
9ae3a8
upon.
9ae3a8
9ae3a8
Also some minor cleanup for error handling.
9ae3a8
9ae3a8
Reviewed-by: Max Reitz <mreitz@redhat.com>
9ae3a8
Signed-off-by: Jeff Cody <jcody@redhat.com>
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
(cherry picked from commit 70747862f129ea0af5e3910f204cc93174c549e4)
9ae3a8
9ae3a8
Conflicts:
9ae3a8
	block/vdi.c
9ae3a8
9ae3a8
RHEL7 notes: conflicts due to not having the 'nocow' commit, as
9ae3a8
             well as not using QemuOpts yet. Arguments for bdrv_open()
9ae3a8
             are different, and downstream does not have
9ae3a8
             BDRV_O_PROTOCOL.  In addition, bdrv_file_open() must be
9ae3a8
             used instead of just bdrv_open().
9ae3a8
9ae3a8
Signed-off-by: Jeff Cody <jcody@redhat.com>
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 block/vdi.c |   52 +++++++++++++++++++++++++++++-----------------------
9ae3a8
 1 files changed, 29 insertions(+), 23 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/vdi.c b/block/vdi.c
9ae3a8
index fb25424..1f4491f 100644
9ae3a8
--- a/block/vdi.c
9ae3a8
+++ b/block/vdi.c
9ae3a8
@@ -673,7 +673,6 @@ static int vdi_co_write(BlockDriverState *bs,
9ae3a8
 static int vdi_create(const char *filename, QEMUOptionParameter *options,
9ae3a8
                       Error **errp)
9ae3a8
 {
9ae3a8
-    int fd;
9ae3a8
     int result = 0;
9ae3a8
     uint64_t bytes = 0;
9ae3a8
     uint32_t blocks;
9ae3a8
@@ -682,6 +681,10 @@ static int vdi_create(const char *filename, QEMUOptionParameter *options,
9ae3a8
     VdiHeader header;
9ae3a8
     size_t i;
9ae3a8
     size_t bmap_size;
9ae3a8
+    int64_t offset = 0;
9ae3a8
+    Error *local_err = NULL;
9ae3a8
+    BlockDriverState *bs = NULL;
9ae3a8
+    uint32_t *bmap = NULL;
9ae3a8
 
9ae3a8
     logout("\n");
9ae3a8
 
9ae3a8
@@ -714,11 +717,14 @@ static int vdi_create(const char *filename, QEMUOptionParameter *options,
9ae3a8
         goto exit;
9ae3a8
     }
9ae3a8
 
9ae3a8
-    fd = qemu_open(filename,
9ae3a8
-                   O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
9ae3a8
-                   0644);
9ae3a8
-    if (fd < 0) {
9ae3a8
-        result = -errno;
9ae3a8
+    result = bdrv_create_file(filename, options, &local_err);
9ae3a8
+    if (result < 0) {
9ae3a8
+        error_propagate(errp, local_err);
9ae3a8
+        goto exit;
9ae3a8
+    }
9ae3a8
+    result = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, &local_err);
9ae3a8
+    if (result < 0) {
9ae3a8
+        error_propagate(errp, local_err);
9ae3a8
         goto exit;
9ae3a8
     }
9ae3a8
 
9ae3a8
@@ -751,13 +757,15 @@ static int vdi_create(const char *filename, QEMUOptionParameter *options,
9ae3a8
     vdi_header_print(&header);
9ae3a8
 #endif
9ae3a8
     vdi_header_to_le(&header);
9ae3a8
-    if (write(fd, &header, sizeof(header)) < 0) {
9ae3a8
-        result = -errno;
9ae3a8
-        goto close_and_exit;
9ae3a8
+    result = bdrv_pwrite_sync(bs, offset, &header, sizeof(header));
9ae3a8
+    if (result < 0) {
9ae3a8
+        error_setg(errp, "Error writing header to %s", filename);
9ae3a8
+        goto exit;
9ae3a8
     }
9ae3a8
+    offset += sizeof(header);
9ae3a8
 
9ae3a8
     if (bmap_size > 0) {
9ae3a8
-        uint32_t *bmap = g_malloc0(bmap_size);
9ae3a8
+        bmap = g_malloc0(bmap_size);
9ae3a8
         for (i = 0; i < blocks; i++) {
9ae3a8
             if (image_type == VDI_TYPE_STATIC) {
9ae3a8
                 bmap[i] = i;
9ae3a8
@@ -765,27 +773,25 @@ static int vdi_create(const char *filename, QEMUOptionParameter *options,
9ae3a8
                 bmap[i] = VDI_UNALLOCATED;
9ae3a8
             }
9ae3a8
         }
9ae3a8
-        if (write(fd, bmap, bmap_size) < 0) {
9ae3a8
-            result = -errno;
9ae3a8
-            g_free(bmap);
9ae3a8
-            goto close_and_exit;
9ae3a8
+        result = bdrv_pwrite_sync(bs, offset, bmap, bmap_size);
9ae3a8
+        if (result < 0) {
9ae3a8
+            error_setg(errp, "Error writing bmap to %s", filename);
9ae3a8
+            goto exit;
9ae3a8
         }
9ae3a8
-        g_free(bmap);
9ae3a8
+        offset += bmap_size;
9ae3a8
     }
9ae3a8
 
9ae3a8
     if (image_type == VDI_TYPE_STATIC) {
9ae3a8
-        if (ftruncate(fd, sizeof(header) + bmap_size + blocks * block_size)) {
9ae3a8
-            result = -errno;
9ae3a8
-            goto close_and_exit;
9ae3a8
+        result = bdrv_truncate(bs, offset + blocks * block_size);
9ae3a8
+        if (result < 0) {
9ae3a8
+            error_setg(errp, "Failed to statically allocate %s", filename);
9ae3a8
+            goto exit;
9ae3a8
         }
9ae3a8
     }
9ae3a8
 
9ae3a8
-close_and_exit:
9ae3a8
-    if ((close(fd) < 0) && !result) {
9ae3a8
-        result = -errno;
9ae3a8
-    }
9ae3a8
-
9ae3a8
 exit:
9ae3a8
+    bdrv_unref(bs);
9ae3a8
+    g_free(bmap);
9ae3a8
     return result;
9ae3a8
 }
9ae3a8
 
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8