cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
218e99
From 330cc46279f2fcd6f2d41d4e8beda8038c7a2fd9 Mon Sep 17 00:00:00 2001
218e99
From: Max Reitz <mreitz@redhat.com>
218e99
Date: Thu, 7 Nov 2013 13:18:21 +0100
218e99
Subject: [PATCH 87/87] vmdk: Implment bdrv_get_specific_info
218e99
218e99
RH-Author: Max Reitz <mreitz@redhat.com>
218e99
Message-id: <1383756824-6921-22-git-send-email-mreitz@redhat.com>
218e99
Patchwork-id: 55576
218e99
O-Subject: [RHEL-7.0 qemu-kvm PATCH v2 21/21] vmdk: Implment bdrv_get_specific_info
218e99
Bugzilla: 980771
218e99
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
218e99
RH-Acked-by: Fam Zheng <famz@redhat.com>
218e99
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
218e99
218e99
From: Fam Zheng <famz@redhat.com>
218e99
218e99
BZ: 980771
218e99
218e99
Implement .bdrv_get_specific_info to return the extent information.
218e99
218e99
Signed-off-by: Fam Zheng <famz@redhat.com>
218e99
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
218e99
(cherry picked from commit f4c129a38a5430b7342a7a23f53a22831154612f)
218e99
218e99
Signed-off-by: Max Reitz <mreitz@redhat.com>
218e99
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
---
218e99
 block/vmdk.c               |   68 +++++++++++++++++++++++++++++++++++++++++++-
218e99
 qapi-schema.json           |   24 +++++++++++++++-
218e99
 tests/qemu-iotests/059     |    2 +-
218e99
 tests/qemu-iotests/059.out |    5 +--
218e99
 4 files changed, 93 insertions(+), 6 deletions(-)
218e99
218e99
diff --git a/block/vmdk.c b/block/vmdk.c
218e99
index 8bef9f2..a3267fe 100644
218e99
--- a/block/vmdk.c
218e99
+++ b/block/vmdk.c
218e99
@@ -106,6 +106,7 @@ typedef struct VmdkExtent {
218e99
     uint32_t l2_cache_counts[L2_CACHE_SIZE];
218e99
 
218e99
     int64_t cluster_sectors;
218e99
+    char *type;
218e99
 } VmdkExtent;
218e99
 
218e99
 typedef struct BDRVVmdkState {
218e99
@@ -113,11 +114,13 @@ typedef struct BDRVVmdkState {
218e99
     uint64_t desc_offset;
218e99
     bool cid_updated;
218e99
     bool cid_checked;
218e99
+    uint32_t cid;
218e99
     uint32_t parent_cid;
218e99
     int num_extents;
218e99
     /* Extent array with num_extents entries, ascend ordered by address */
218e99
     VmdkExtent *extents;
218e99
     Error *migration_blocker;
218e99
+    char *create_type;
218e99
 } BDRVVmdkState;
218e99
 
218e99
 typedef struct VmdkMetaData {
218e99
@@ -214,6 +217,7 @@ static void vmdk_free_extents(BlockDriverState *bs)
218e99
         g_free(e->l1_table);
218e99
         g_free(e->l2_cache);
218e99
         g_free(e->l1_backup_table);
218e99
+        g_free(e->type);
218e99
         if (e->file != bs->file) {
218e99
             bdrv_delete(e->file);
218e99
         }
218e99
@@ -534,6 +538,7 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
218e99
     uint32_t l1_size, l1_entry_sectors;
218e99
     VMDK4Header header;
218e99
     VmdkExtent *extent;
218e99
+    BDRVVmdkState *s = bs->opaque;
218e99
     int64_t l1_backup_offset = 0;
218e99
 
218e99
     ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
218e99
@@ -549,6 +554,10 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
218e99
         }
218e99
     }
218e99
 
218e99
+    if (!s->create_type) {
218e99
+        s->create_type = g_strdup("monolithicSparse");
218e99
+    }
218e99
+
218e99
     if (le64_to_cpu(header.gd_offset) == VMDK4_GD_AT_END) {
218e99
         /*
218e99
          * The footer takes precedence over the header, so read it in. The
218e99
@@ -709,6 +718,8 @@ static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
218e99
     int64_t flat_offset;
218e99
     char extent_path[PATH_MAX];
218e99
     BlockDriverState *extent_file;
218e99
+    BDRVVmdkState *s = bs->opaque;
218e99
+    VmdkExtent *extent;
218e99
 
218e99
     while (*p) {
218e99
         /* parse extent line:
218e99
@@ -749,7 +760,6 @@ static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
218e99
         /* save to extents array */
218e99
         if (!strcmp(type, "FLAT") || !strcmp(type, "VMFS")) {
218e99
             /* FLAT extent */
218e99
-            VmdkExtent *extent;
218e99
 
218e99
             ret = vmdk_add_extent(bs, extent_file, true, sectors,
218e99
                             0, 0, 0, 0, 0, &extent, errp);
218e99
@@ -764,10 +774,12 @@ static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
218e99
                 bdrv_delete(extent_file);
218e99
                 return ret;
218e99
             }
218e99
+            extent = &s->extents[s->num_extents - 1];
218e99
         } else {
218e99
             error_setg(errp, "Unsupported extent type '%s'", type);
218e99
             return -ENOTSUP;
218e99
         }
218e99
+        extent->type = g_strdup(type);
218e99
 next_line:
218e99
         /* move to next line */
218e99
         while (*p && *p != '\n') {
218e99
@@ -812,6 +824,7 @@ static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
218e99
         ret = -ENOTSUP;
218e99
         goto exit;
218e99
     }
218e99
+    s->create_type = g_strdup(ct);
218e99
     s->desc_offset = 0;
218e99
     ret = vmdk_parse_extents(buf, bs, bs->file->filename, errp);
218e99
 exit:
218e99
@@ -838,6 +851,7 @@ static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
218e99
     if (ret) {
218e99
         goto fail;
218e99
     }
218e99
+    s->cid = vmdk_read_cid(bs, 0);
218e99
     s->parent_cid = vmdk_read_cid(bs, 1);
218e99
     qemu_co_mutex_init(&s->lock);
218e99
 
218e99
@@ -850,6 +864,8 @@ static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
218e99
     return 0;
218e99
 
218e99
 fail:
218e99
+    g_free(s->create_type);
218e99
+    s->create_type = NULL;
218e99
     vmdk_free_extents(bs);
218e99
     return ret;
218e99
 }
218e99
@@ -1761,6 +1777,7 @@ static void vmdk_close(BlockDriverState *bs)
218e99
     BDRVVmdkState *s = bs->opaque;
218e99
 
218e99
     vmdk_free_extents(bs);
218e99
+    g_free(s->create_type);
218e99
 
218e99
     migrate_del_blocker(s->migration_blocker);
218e99
     error_free(s->migration_blocker);
218e99
@@ -1822,6 +1839,54 @@ static int vmdk_has_zero_init(BlockDriverState *bs)
218e99
     return 1;
218e99
 }
218e99
 
218e99
+static ImageInfo *vmdk_get_extent_info(VmdkExtent *extent)
218e99
+{
218e99
+    ImageInfo *info = g_new0(ImageInfo, 1);
218e99
+
218e99
+    *info = (ImageInfo){
218e99
+        .filename         = g_strdup(extent->file->filename),
218e99
+        .format           = g_strdup(extent->type),
218e99
+        .virtual_size     = extent->sectors * BDRV_SECTOR_SIZE,
218e99
+        .compressed       = extent->compressed,
218e99
+        .has_compressed   = extent->compressed,
218e99
+        .cluster_size     = extent->cluster_sectors * BDRV_SECTOR_SIZE,
218e99
+        .has_cluster_size = !extent->flat,
218e99
+    };
218e99
+
218e99
+    return info;
218e99
+}
218e99
+
218e99
+static ImageInfoSpecific *vmdk_get_specific_info(BlockDriverState *bs)
218e99
+{
218e99
+    int i;
218e99
+    BDRVVmdkState *s = bs->opaque;
218e99
+    ImageInfoSpecific *spec_info = g_new0(ImageInfoSpecific, 1);
218e99
+    ImageInfoList **next;
218e99
+
218e99
+    *spec_info = (ImageInfoSpecific){
218e99
+        .kind = IMAGE_INFO_SPECIFIC_KIND_VMDK,
218e99
+        {
218e99
+            .vmdk = g_new0(ImageInfoSpecificVmdk, 1),
218e99
+        },
218e99
+    };
218e99
+
218e99
+    *spec_info->vmdk = (ImageInfoSpecificVmdk) {
218e99
+        .create_type = g_strdup(s->create_type),
218e99
+        .cid = s->cid,
218e99
+        .parent_cid = s->parent_cid,
218e99
+    };
218e99
+
218e99
+    next = &spec_info->vmdk->extents;
218e99
+    for (i = 0; i < s->num_extents; i++) {
218e99
+        *next = g_new0(ImageInfoList, 1);
218e99
+        (*next)->value = vmdk_get_extent_info(&s->extents[i]);
218e99
+        (*next)->next = NULL;
218e99
+        next = &(*next)->next;
218e99
+    }
218e99
+
218e99
+    return spec_info;
218e99
+}
218e99
+
218e99
 static QEMUOptionParameter vmdk_create_options[] = {
218e99
     {
218e99
         .name = BLOCK_OPT_SIZE,
218e99
@@ -1874,6 +1939,7 @@ static BlockDriver bdrv_vmdk = {
218e99
     .bdrv_co_get_block_status     = vmdk_co_get_block_status,
218e99
     .bdrv_get_allocated_file_size = vmdk_get_allocated_file_size,
218e99
     .bdrv_has_zero_init           = vmdk_has_zero_init,
218e99
+    .bdrv_get_specific_info       = vmdk_get_specific_info,
218e99
 
218e99
     .create_options               = vmdk_create_options,
218e99
 };
218e99
diff --git a/qapi-schema.json b/qapi-schema.json
218e99
index 99b05b7..64696a9 100644
218e99
--- a/qapi-schema.json
218e99
+++ b/qapi-schema.json
218e99
@@ -225,6 +225,27 @@
218e99
   } }
218e99
 
218e99
 ##
218e99
+# @ImageInfoSpecificVmdk:
218e99
+#
218e99
+# @create_type: The create type of VMDK image
218e99
+#
218e99
+# @cid: Content id of image
218e99
+#
218e99
+# @parent-cid: Parent VMDK image's cid
218e99
+#
218e99
+# @extents: List of extent files
218e99
+#
218e99
+# Since: 1.7
218e99
+##
218e99
+{ 'type': 'ImageInfoSpecificVmdk',
218e99
+  'data': {
218e99
+      'create-type': 'str',
218e99
+      'cid': 'int',
218e99
+      'parent-cid': 'int',
218e99
+      'extents': ['ImageInfo']
218e99
+  } }
218e99
+
218e99
+##
218e99
 # @ImageInfoSpecific:
218e99
 #
218e99
 # A discriminated record of image format specific information structures.
218e99
@@ -234,7 +255,8 @@
218e99
 
218e99
 { 'union': 'ImageInfoSpecific',
218e99
   'data': {
218e99
-      'qcow2': 'ImageInfoSpecificQCow2'
218e99
+      'qcow2': 'ImageInfoSpecificQCow2',
218e99
+      'vmdk': 'ImageInfoSpecificVmdk'
218e99
   } }
218e99
 
218e99
 ##
218e99
diff --git a/tests/qemu-iotests/059 b/tests/qemu-iotests/059
218e99
index 26d4538..36103e1 100755
218e99
--- a/tests/qemu-iotests/059
218e99
+++ b/tests/qemu-iotests/059
218e99
@@ -69,7 +69,7 @@ poke_file "$TEST_IMG" "$grain_table_size_offset" "\x01\x00\x00\x00"
218e99
 echo "=== Testing monolithicFlat creation and opening ==="
218e99
 echo
218e99
 IMGOPTS="subformat=monolithicFlat" _make_test_img 2G
218e99
-$QEMU_IMG info $TEST_IMG | _filter_testdir
218e99
+_img_info
218e99
 
218e99
 echo
218e99
 echo "=== Testing monolithicFlat with zeroed_grain ==="
218e99
diff --git a/tests/qemu-iotests/059.out b/tests/qemu-iotests/059.out
218e99
index 2b29ca9..5829794 100644
218e99
--- a/tests/qemu-iotests/059.out
218e99
+++ b/tests/qemu-iotests/059.out
218e99
@@ -18,10 +18,9 @@ no file open, try 'help open'
218e99
 === Testing monolithicFlat creation and opening ===
218e99
 
218e99
 Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=2147483648
218e99
-image: TEST_DIR/t.vmdk
218e99
-file format: vmdk
218e99
+image: TEST_DIR/t.IMGFMT
218e99
+file format: IMGFMT
218e99
 virtual size: 2.0G (2147483648 bytes)
218e99
-disk size: 4.0K
218e99
 
218e99
 === Testing monolithicFlat with zeroed_grain ===
218e99
 qemu-img: TEST_DIR/t.IMGFMT: Flat image can't enable zeroed grain
218e99
-- 
218e99
1.7.1
218e99