cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
958e1b
From aa17f7f15bf2b0af9019a6935563d1f1a98ecc05 Mon Sep 17 00:00:00 2001
958e1b
From: Jeffrey Cody <jcody@redhat.com>
958e1b
Date: Tue, 16 Sep 2014 20:11:40 +0200
958e1b
Subject: [PATCH 02/20] vmdk: Allow vmdk_create to work with protocol
958e1b
958e1b
Message-id: <1c4328440599c6f754b823e280f9b8212ae9c62b.1410897407.git.jcody@redhat.com>
958e1b
Patchwork-id: 61206
958e1b
O-Subject: [PATCH qemu-kvm-rhel RHEL7.1 01/15] vmdk: Allow vmdk_create to work with protocol
958e1b
Bugzilla: 1098086
958e1b
RH-Acked-by: Fam Zheng <famz@redhat.com>
958e1b
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
958e1b
RH-Acked-by: Max Reitz <mreitz@redhat.com>
958e1b
958e1b
From: Fam Zheng <famz@redhat.com>
958e1b
958e1b
This improves vmdk_create to use bdrv_* functions to replace qemu_open
958e1b
and other fd functions. The error handling are improved as well. One
958e1b
difference is that bdrv_pwrite will round up buffer to sectors, so for
958e1b
description file, an extra bdrv_truncate is used in the end to drop
958e1b
inding zeros.
958e1b
958e1b
Notes:
958e1b
958e1b
 - A bonus bug fix is correct endian is used in initializing GD entries.
958e1b
958e1b
 - ROUND_UP and DIV_ROUND_UP are used where possible.
958e1b
958e1b
I tested that new code produces exactly the same file as previously.
958e1b
958e1b
Signed-off-by: Fam Zheng <famz@redhat.com>
958e1b
Tested-by: Peter Lieven <pl@kamp.de>
958e1b
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
958e1b
(cherry picked from commit 917703c179d4483d050fe112a2a81acec3d6ffd7)
958e1b
Signed-off-by: Jeff Cody <jcody@redhat.com>
958e1b
958e1b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
958e1b
---
958e1b
 block/vmdk.c |  164 +++++++++++++++++++++++++++++++++------------------------
958e1b
 1 files changed, 95 insertions(+), 69 deletions(-)
958e1b
958e1b
diff --git a/block/vmdk.c b/block/vmdk.c
958e1b
index 7532e11..4ff9531 100644
958e1b
--- a/block/vmdk.c
958e1b
+++ b/block/vmdk.c
958e1b
@@ -1464,23 +1464,33 @@ static int coroutine_fn vmdk_co_write_zeroes(BlockDriverState *bs,
958e1b
 }
958e1b
 
958e1b
 static int vmdk_create_extent(const char *filename, int64_t filesize,
958e1b
-                              bool flat, bool compress, bool zeroed_grain)
958e1b
+                              bool flat, bool compress, bool zeroed_grain,
958e1b
+                              Error **errp)
958e1b
 {
958e1b
     int ret, i;
958e1b
-    int fd = 0;
958e1b
+    BlockDriverState *bs = NULL;
958e1b
     VMDK4Header header;
958e1b
-    uint32_t tmp, magic, grains, gd_size, gt_size, gt_count;
958e1b
+    Error *local_err;
958e1b
+    uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
958e1b
+    uint32_t *gd_buf = NULL;
958e1b
+    int gd_buf_size;
958e1b
 
958e1b
-    fd = qemu_open(filename,
958e1b
-                   O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
958e1b
-                   0644);
958e1b
-    if (fd < 0) {
958e1b
-        return -errno;
958e1b
+    ret = bdrv_create_file(filename, NULL, &local_err);
958e1b
+    if (ret < 0) {
958e1b
+        error_propagate(errp, local_err);
958e1b
+        goto exit;
958e1b
     }
958e1b
+
958e1b
+    ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, &local_err);
958e1b
+    if (ret < 0) {
958e1b
+        error_propagate(errp, local_err);
958e1b
+        goto exit;
958e1b
+    }
958e1b
+
958e1b
     if (flat) {
958e1b
-        ret = ftruncate(fd, filesize);
958e1b
+        ret = bdrv_truncate(bs, filesize);
958e1b
         if (ret < 0) {
958e1b
-            ret = -errno;
958e1b
+            error_setg(errp, "Could not truncate file");
958e1b
         }
958e1b
         goto exit;
958e1b
     }
958e1b
@@ -1491,24 +1501,23 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
958e1b
                    | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
958e1b
                    | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
958e1b
     header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
958e1b
-    header.capacity = filesize / 512;
958e1b
+    header.capacity = filesize / BDRV_SECTOR_SIZE;
958e1b
     header.granularity = 128;
958e1b
-    header.num_gtes_per_gt = 512;
958e1b
+    header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
958e1b
 
958e1b
-    grains = (filesize / 512 + header.granularity - 1) / header.granularity;
958e1b
-    gt_size = ((header.num_gtes_per_gt * sizeof(uint32_t)) + 511) >> 9;
958e1b
-    gt_count =
958e1b
-        (grains + header.num_gtes_per_gt - 1) / header.num_gtes_per_gt;
958e1b
-    gd_size = (gt_count * sizeof(uint32_t) + 511) >> 9;
958e1b
+    grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity);
958e1b
+    gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
958e1b
+                           BDRV_SECTOR_SIZE);
958e1b
+    gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
958e1b
+    gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
958e1b
 
958e1b
     header.desc_offset = 1;
958e1b
     header.desc_size = 20;
958e1b
     header.rgd_offset = header.desc_offset + header.desc_size;
958e1b
-    header.gd_offset = header.rgd_offset + gd_size + (gt_size * gt_count);
958e1b
+    header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
958e1b
     header.grain_offset =
958e1b
-       ((header.gd_offset + gd_size + (gt_size * gt_count) +
958e1b
-         header.granularity - 1) / header.granularity) *
958e1b
-        header.granularity;
958e1b
+        ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
958e1b
+                 header.granularity);
958e1b
     /* swap endianness for all header fields */
958e1b
     header.version = cpu_to_le32(header.version);
958e1b
     header.flags = cpu_to_le32(header.flags);
958e1b
@@ -1528,48 +1537,55 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
958e1b
     header.check_bytes[3] = 0xa;
958e1b
 
958e1b
     /* write all the data */
958e1b
-    ret = qemu_write_full(fd, &magic, sizeof(magic));
958e1b
-    if (ret != sizeof(magic)) {
958e1b
-        ret = -errno;
958e1b
+    ret = bdrv_pwrite(bs, 0, &magic, sizeof(magic));
958e1b
+    if (ret < 0) {
958e1b
+        error_set(errp, QERR_IO_ERROR);
958e1b
         goto exit;
958e1b
     }
958e1b
-    ret = qemu_write_full(fd, &header, sizeof(header));
958e1b
-    if (ret != sizeof(header)) {
958e1b
-        ret = -errno;
958e1b
+    ret = bdrv_pwrite(bs, sizeof(magic), &header, sizeof(header));
958e1b
+    if (ret < 0) {
958e1b
+        error_set(errp, QERR_IO_ERROR);
958e1b
         goto exit;
958e1b
     }
958e1b
 
958e1b
-    ret = ftruncate(fd, le64_to_cpu(header.grain_offset) << 9);
958e1b
+    ret = bdrv_truncate(bs, le64_to_cpu(header.grain_offset) << 9);
958e1b
     if (ret < 0) {
958e1b
-        ret = -errno;
958e1b
+        error_setg(errp, "Could not truncate file");
958e1b
         goto exit;
958e1b
     }
958e1b
 
958e1b
     /* write grain directory */
958e1b
-    lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET);
958e1b
-    for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_size;
958e1b
+    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
958e1b
+    gd_buf = g_malloc0(gd_buf_size);
958e1b
+    for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
958e1b
          i < gt_count; i++, tmp += gt_size) {
958e1b
-        ret = qemu_write_full(fd, &tmp, sizeof(tmp));
958e1b
-        if (ret != sizeof(tmp)) {
958e1b
-            ret = -errno;
958e1b
-            goto exit;
958e1b
-        }
958e1b
+        gd_buf[i] = cpu_to_le32(tmp);
958e1b
+    }
958e1b
+    ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
958e1b
+                      gd_buf, gd_buf_size);
958e1b
+    if (ret < 0) {
958e1b
+        error_set(errp, QERR_IO_ERROR);
958e1b
+        goto exit;
958e1b
     }
958e1b
 
958e1b
     /* write backup grain directory */
958e1b
-    lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET);
958e1b
-    for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_size;
958e1b
+    for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
958e1b
          i < gt_count; i++, tmp += gt_size) {
958e1b
-        ret = qemu_write_full(fd, &tmp, sizeof(tmp));
958e1b
-        if (ret != sizeof(tmp)) {
958e1b
-            ret = -errno;
958e1b
-            goto exit;
958e1b
-        }
958e1b
+        gd_buf[i] = cpu_to_le32(tmp);
958e1b
+    }
958e1b
+    ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
958e1b
+                      gd_buf, gd_buf_size);
958e1b
+    if (ret < 0) {
958e1b
+        error_set(errp, QERR_IO_ERROR);
958e1b
+        goto exit;
958e1b
     }
958e1b
 
958e1b
     ret = 0;
958e1b
- exit:
958e1b
-    qemu_close(fd);
958e1b
+exit:
958e1b
+    if (bs) {
958e1b
+        bdrv_unref(bs);
958e1b
+    }
958e1b
+    g_free(gd_buf);
958e1b
     return ret;
958e1b
 }
958e1b
 
958e1b
@@ -1616,7 +1632,9 @@ static int filename_decompose(const char *filename, char *path, char *prefix,
958e1b
 static int vmdk_create(const char *filename, QEMUOptionParameter *options,
958e1b
                        Error **errp)
958e1b
 {
958e1b
-    int fd, idx = 0;
958e1b
+    int idx = 0;
958e1b
+    BlockDriverState *new_bs = NULL;
958e1b
+    Error *local_err;
958e1b
     char *desc = NULL;
958e1b
     int64_t total_size = 0, filesize;
958e1b
     const char *adapter_type = NULL;
958e1b
@@ -1633,6 +1651,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options,
958e1b
     uint32_t parent_cid = 0xffffffff;
958e1b
     uint32_t number_heads = 16;
958e1b
     bool zeroed_grain = false;
958e1b
+    uint32_t desc_offset = 0, desc_len;
958e1b
     const char desc_template[] =
958e1b
         "# Disk DescriptorFile\n"
958e1b
         "version=1\n"
958e1b
@@ -1766,7 +1785,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options,
958e1b
                 path, desc_filename);
958e1b
 
958e1b
         if (vmdk_create_extent(ext_filename, size,
958e1b
-                               flat, compress, zeroed_grain)) {
958e1b
+                               flat, compress, zeroed_grain, errp)) {
958e1b
             ret = -EINVAL;
958e1b
             goto exit;
958e1b
         }
958e1b
@@ -1774,7 +1793,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options,
958e1b
 
958e1b
         /* Format description line */
958e1b
         snprintf(desc_line, sizeof(desc_line),
958e1b
-                    desc_extent_line, size / 512, desc_filename);
958e1b
+                    desc_extent_line, size / BDRV_SECTOR_SIZE, desc_filename);
958e1b
         g_string_append(ext_desc_lines, desc_line);
958e1b
     }
958e1b
     /* generate descriptor file */
958e1b
@@ -1785,36 +1804,43 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options,
958e1b
                            parent_desc_line,
958e1b
                            ext_desc_lines->str,
958e1b
                            (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4),
958e1b
-                           total_size / (int64_t)(63 * number_heads * 512),
958e1b
+                           total_size /
958e1b
+                               (int64_t)(63 * number_heads * BDRV_SECTOR_SIZE),
958e1b
                            number_heads,
958e1b
                            adapter_type);
958e1b
-    if (split || flat) {
958e1b
-        fd = qemu_open(filename,
958e1b
-                       O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
958e1b
-                       0644);
958e1b
+    desc_len = strlen(desc);
958e1b
+    /* the descriptor offset = 0x200 */
958e1b
+    if (!split && !flat) {
958e1b
+        desc_offset = 0x200;
958e1b
     } else {
958e1b
-        fd = qemu_open(filename,
958e1b
-                       O_WRONLY | O_BINARY | O_LARGEFILE,
958e1b
-                       0644);
958e1b
+        ret = bdrv_create_file(filename, options, &local_err);
958e1b
+        if (ret < 0) {
958e1b
+            error_setg_errno(errp, -ret, "Could not create image file");
958e1b
+            goto exit;
958e1b
+        }
958e1b
     }
958e1b
-    if (fd < 0) {
958e1b
-        ret = -errno;
958e1b
+    ret = bdrv_file_open(&new_bs, filename, NULL, BDRV_O_RDWR, &local_err);
958e1b
+    if (ret < 0) {
958e1b
+        error_setg_errno(errp, -ret, "Could not write description");
958e1b
         goto exit;
958e1b
     }
958e1b
-    /* the descriptor offset = 0x200 */
958e1b
-    if (!split && !flat && 0x200 != lseek(fd, 0x200, SEEK_SET)) {
958e1b
-        ret = -errno;
958e1b
-        goto close_exit;
958e1b
+    ret = bdrv_pwrite(new_bs, desc_offset, desc, desc_len);
958e1b
+    if (ret < 0) {
958e1b
+        error_setg_errno(errp, -ret, "Could not write description");
958e1b
+        goto exit;
958e1b
     }
958e1b
-    ret = qemu_write_full(fd, desc, strlen(desc));
958e1b
-    if (ret != strlen(desc)) {
958e1b
-        ret = -errno;
958e1b
-        goto close_exit;
958e1b
+    /* bdrv_pwrite write padding zeros to align to sector, we don't need that
958e1b
+     * for description file */
958e1b
+    if (desc_offset == 0) {
958e1b
+        ret = bdrv_truncate(new_bs, desc_len);
958e1b
+        if (ret < 0) {
958e1b
+            error_setg(errp, "Could not truncate file");
958e1b
+        }
958e1b
     }
958e1b
-    ret = 0;
958e1b
-close_exit:
958e1b
-    qemu_close(fd);
958e1b
 exit:
958e1b
+    if (new_bs) {
958e1b
+        bdrv_unref(new_bs);
958e1b
+    }
958e1b
     g_free(desc);
958e1b
     g_string_free(ext_desc_lines, true);
958e1b
     return ret;
958e1b
-- 
958e1b
1.7.1
958e1b