qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Fam Zheng <famcool@gmail.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
	Stefan Hajnoczi <stefanha@gmail.com>,
	Christoph Hellwig <hch@lst.de>
Subject: [Qemu-devel] [PATCH 06/12] VMDK: vmdk_open for mono flat
Date: Sat, 4 Jun 2011 08:42:06 +0800	[thread overview]
Message-ID: <BANLkTik7oEAE2cXP+sWuFrQtKSPxWOV46Q@mail.gmail.com> (raw)

Vmdk_open for mono flat image.

Signed-off-by: Fam Zheng <famcool@gmail.com>
---
 block/vmdk.c |  134 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 128 insertions(+), 6 deletions(-)

diff --git a/block/vmdk.c b/block/vmdk.c
index b02a7b7..f1233cf 100644
--- a/block/vmdk.c
+++ b/block/vmdk.c
@@ -355,24 +355,110 @@ static int vmdk_parent_open(BlockDriverState *bs)
     char desc[DESC_SIZE];
     BDRVVmdkState *s = bs->opaque;

-    if (bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE) != DESC_SIZE)
+    if (bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE) != DESC_SIZE) {
         return -1;
+    }

     if ((p_name = strstr(desc,"parentFileNameHint")) != NULL) {
         char *end_name;

         p_name += sizeof("parentFileNameHint") + 1;
-        if ((end_name = strchr(p_name,'\"')) == NULL)
+        if ((end_name = strchr(p_name,'\"')) == NULL) {
             return -1;
-        if ((end_name - p_name) > sizeof (bs->backing_file) - 1)
+        }
+        if ((end_name - p_name) > sizeof (bs->backing_file) - 1) {
             return -1;
-
+        }
         pstrcpy(bs->backing_file, end_name - p_name + 1, p_name);
     }
-
     return 0;
 }

+/* find an option value out of descriptor file */
+static int vmdk_parse_description(const char *desc, const char *opt_name,
+        char *buf, int buf_size)
+{
+    char *opt_pos = strstr(desc, opt_name);
+    int r;
+    if (!opt_pos) {
+        return -1;
+    }
+    opt_pos += strlen(opt_name) + 2;
+    r = sscanf(opt_pos, "%[^\"]s", buf);
+    assert(r <= buf_size);
+    return r <= 0;
+}
+
+
+static int vmdk_parse_extents(const char *desc, VmdkExtent extents[],
+        const char *desc_file_path)
+{
+    int ret = 0;
+    int r;
+    char access[11];
+    char type[11];
+    char fname[512];
+    VmdkExtent *extent;
+    const char *p = desc;
+    int64_t sectors = 0;
+
+    while (*p) {
+        if (strncmp(p, "RW", strlen("RW")) == 0) {
+            /* parse extent line:
+             * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
+             * or
+             * RW [size in sectors] SPARSE "file-name.vmdk"
+             */
+
+            sscanf(p, "%10s %lld %10s \"%[^\"]512s\"",
+                    access, &sectors, type, fname);
+            if (!(strlen(access) && sectors && strlen(type) &&
strlen(fname))) {
+                goto cont;
+            }
+            if (strcmp(type, "FLAT") && strcmp(type, "SPARSE")) {
+                goto cont;
+            }
+            if (strcmp(access, "RW")) {
+                goto cont;
+            }
+            ret++;
+            if (!extents) {
+                goto cont;
+            }
+
+            /* save to extents array */
+            if (!strcmp(type, "FLAT")) {
+                /* FLAT extent */
+                char extent_path[1024];
+                path_combine(extent_path, sizeof(extent_path),
+                        desc_file_path, fname);
+                extent = &extents[ret - 1];
+                extent->flat = true;
+                extent->sectors = sectors;
+                extent->cluster_sectors = sectors;
+                extent->file = bdrv_new("");
+                if (!extent->file) {
+                    return -1;
+                }
+                r = bdrv_open(extent->file, extent_path,
+                        BDRV_O_RDWR | BDRV_O_NO_BACKING, NULL);
+                if (r) {
+                    return -1;
+                }
+            } else {
+                /* SPARSE extent, should not be here */
+                fprintf(stderr, "VMDK: Not supported extent type.\n");
+                return -1;
+            }
+        }
+cont:
+        /* move to next line */
+        while (*p && *p != '\n') p++;
+        p++;
+    }
+    return ret;
+}
+
 static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent)
 {
     int l1_size, i;
@@ -496,6 +582,42 @@ static int vmdk_open_vmdk4(BlockDriverState *bs, int flags)
     return -1;
 }

+static int vmdk_open_desc_file(BlockDriverState *bs, int flags)
+{
+    char buf[2048];
+    char ct[128];
+    VmdkExtent *extent;
+    BDRVVmdkState *s = bs->opaque;
+
+    if (bdrv_pread(bs->file, 0, buf, sizeof(buf)) == 0) {
+        goto fail;
+    }
+    if (0 != vmdk_parse_description(buf, "createType", ct, sizeof(ct))) {
+        goto fail;
+    }
+    if (0 != strcmp(ct, "monolithicFlat")) {
+        goto fail;
+    }
+    s->desc_offset = 0;
+    s->num_extents = vmdk_parse_extents(buf, NULL, NULL);
+    if  (!s->num_extents)
+        goto fail;
+    s->extents = qemu_mallocz(s->num_extents * sizeof(VmdkExtent));
+    extent = s->extents;
+    vmdk_parse_extents(buf, s->extents, bs->file->filename);
+    bs->total_sectors = extent->sectors;
+
+    // try to open parent images, if exist
+    if (vmdk_parent_open(bs) != 0) {
+        goto fail;
+    }
+    extent->parent_cid = vmdk_read_cid(bs, 1);
+    return 0;
+ fail:
+    qemu_free(s->extents);
+    return -1;
+}
+
 static int vmdk_open(BlockDriverState *bs, int flags)
 {
     uint32_t magic;
@@ -510,7 +632,7 @@ static int vmdk_open(BlockDriverState *bs, int flags)
     } else if (magic == VMDK4_MAGIC) {
         return vmdk_open_vmdk4(bs, flags);
     } else {
-        return -1;
+        return vmdk_open_desc_file(bs, flags);
     }
 }

             reply	other threads:[~2011-06-04  0:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-04  0:42 Fam Zheng [this message]
2011-06-18 16:42 ` [Qemu-devel] [PATCH 06/12] VMDK: vmdk_open for mono flat Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=BANLkTik7oEAE2cXP+sWuFrQtKSPxWOV46Q@mail.gmail.com \
    --to=famcool@gmail.com \
    --cc=hch@lst.de \
    --cc=kwolf@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).