qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Xu Wang <cngesaint@gmail.com>
To: qemu-devel@nongnu.org
Cc: cngesaint@gmail.com, Xu Wang <cngesaint@outlook.com>
Subject: [Qemu-devel] [PATCH 1/2] Refine and export infinite loop checking in collect_image_info_list()
Date: Thu, 27 Jun 2013 03:38:19 -0400	[thread overview]
Message-ID: <1372318700-25103-2-git-send-email-cngesaint@gmail.com> (raw)
In-Reply-To: <1372318700-25103-1-git-send-email-cngesaint@gmail.com>

From: Xu Wang <cngesaint@outlook.com>

Signed-off-by: Xu Wang <cngesaint@outlook.com>
---
 qemu-img.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 89 insertions(+), 21 deletions(-)

diff --git a/qemu-img.c b/qemu-img.c
index 809b4f1..0bc265d 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -324,6 +324,86 @@ static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,
     return 0;
 }
 
+static gboolean str_equal_func(gconstpointer a, gconstpointer b)
+{
+    return strcmp(a, b) == 0;
+}
+
+/**
+ * Check backing file chain if there is a loop in it and build list of
+ * ImageInfo if needed.
+ *
+ * @filename: topmost image filename
+ * @fmt: topmost image format (may be NULL to autodetect)
+ * @head: head of ImageInfo list. If not need please set head to null.
+ * @chain: true  - enumerate entire backing file chain
+ *         false - only topmost image file
+ * @backing_file: if this value is set, filename will insert into hash
+ *                table directly. open and seek backing file start from it.
+ *
+ * If return true, stands for a backing file loop exists or some error
+ * happend. If return false, everything is ok.
+ */
+static bool backing_file_loop_check(const char *filename, const char *fmt,
+                             bool chain, const char *backing_file) {
+    GHashTable *filenames;
+    BlockDriverState *bs;
+    ImageInfo *info;
+    Error *err = NULL;
+
+    filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
+
+    /* If backing file exists, filename will insert into hash table and seek
+     * the whole backing file chain from @backing_file.
+     */
+    if (backing_file) {
+        g_hash_table_insert(filenames, (gpointer)filename, NULL);
+        filename = backing_file;
+    }
+
+    while (filename) {
+        if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
+            error_report("Backing file '%s' creates an infinite loop.",
+                         filename);
+            goto err;
+        }
+        g_hash_table_insert(filenames, (gpointer)filename, NULL);
+
+        bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_NO_BACKING,
+                           false, false);
+        if (!bs) {
+            goto err;
+        }
+
+        bdrv_query_image_info(bs, &info, &err);
+        if (error_is_set(&err)) {
+            error_report("%s", error_get_pretty(err));
+            error_free(err);
+            goto err;
+        }
+
+        bdrv_delete(bs);
+
+        filename = fmt = NULL;
+        if (chain) {
+            if (info->has_full_backing_filename) {
+                filename = info->full_backing_filename;
+            } else if (info->has_backing_filename) {
+                filename = info->backing_filename;
+            }
+            if (info->has_backing_filename_format) {
+                fmt = info->backing_filename_format;
+            }
+        }
+    }
+    g_hash_table_destroy(filenames);
+    return false;
+
+err:
+    g_hash_table_destroy(filenames);
+    return true;
+}
+
 static int img_create(int argc, char **argv)
 {
     int c;
@@ -1620,11 +1700,6 @@ static void dump_human_image_info_list(ImageInfoList *list)
     }
 }
 
-static gboolean str_equal_func(gconstpointer a, gconstpointer b)
-{
-    return strcmp(a, b) == 0;
-}
-
 /**
  * Open an image file chain and return an ImageInfoList
  *
@@ -1642,24 +1717,18 @@ static ImageInfoList *collect_image_info_list(const char *filename,
                                               bool chain)
 {
     ImageInfoList *head = NULL;
+    BlockDriverState *bs;
+    ImageInfoList *elem;
     ImageInfoList **last = &head;
-    GHashTable *filenames;
+    ImageInfo *info;
     Error *err = NULL;
-
-    filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
+        
+    /* check if loop exists and build ImageInfoList */
+    if (backing_file_loop_check(filename, fmt, chain, NULL)) {
+        goto err;
+    }
 
     while (filename) {
-        BlockDriverState *bs;
-        ImageInfo *info;
-        ImageInfoList *elem;
-
-        if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
-            error_report("Backing file '%s' creates an infinite loop.",
-                         filename);
-            goto err;
-        }
-        g_hash_table_insert(filenames, (gpointer)filename, NULL);
-
         bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_NO_BACKING,
                            false, false);
         if (!bs) {
@@ -1692,12 +1761,11 @@ static ImageInfoList *collect_image_info_list(const char *filename,
             }
         }
     }
-    g_hash_table_destroy(filenames);
+
     return head;
 
 err:
     qapi_free_ImageInfoList(head);
-    g_hash_table_destroy(filenames);
     return NULL;
 }
 
-- 
1.8.1.4

  reply	other threads:[~2013-06-27  7:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-27  7:38 [Qemu-devel] [PATCH 0/2] Add infinite loop checking in img_create() Xu Wang
2013-06-27  7:38 ` Xu Wang [this message]
2013-06-28 20:37   ` [Qemu-devel] [PATCH 1/2] Refine and export infinite loop checking in collect_image_info_list() Eric Blake
2013-07-04 13:00     ` Stefan Hajnoczi
2013-07-08  7:31     ` Xu Wang
2013-06-27  7:38 ` [Qemu-devel] [PATCH 2/2] Check infinite loop in img_create() Xu Wang
2013-06-29 14:32 ` [Qemu-devel] [PATCH 0/2] Add infinite loop checking " Andreas Färber
2013-07-04 13:01   ` 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=1372318700-25103-2-git-send-email-cngesaint@gmail.com \
    --to=cngesaint@gmail.com \
    --cc=cngesaint@outlook.com \
    --cc=qemu-devel@nongnu.org \
    /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).