Skip to contentSkip to conversation

Add infinite loop check for backing file chain

PATCH Superseded Xu Wang 4 participants 18 messages Jul 8, 2013 – Jul 15, 2013

v2 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Xu Wang

author
#0 lore raw

v2 cover Add infinite loop check for backing file chain

Updates:
1. Changed infinite loop check in collect_image_info_list() from filename checking to inode checking.
2. Absolute or relative path is OK for filename path.
3. Hard and soft link are works well.
4. Added WIN32 platform support (shortcuts could be recognized correctly.)
5. Create a file which contains loop in backing file will failed.
6. Start a vm which boot block file contains loop in backing file chain will failed instead of no response and segment fault.

Xu Wang (5):
Refine and export infinite loop checking in collect_image_info_list() Add WIN32 platform support for backing_file_loop_check() Check infinite loop in bdrv_img_create()
Add backing file loop check in change_backing_file()
Add infinite loop check in drive_init()

 block.c               | 211 ++++++++++++++++++++++++++++++++++++++++++++++++--
 blockdev.c            |   5 ++
 include/block/block.h |   4 +
 qemu-img.c            |  30 +++----
 4 files changed, 224 insertions(+), 26 deletions(-)
Signature · 1 line
1.8.1.4
author
#1 lore raw

PATCH v2 1/5 Refine and export infinite loop checking in collect_image_info_list()

---
 block.c               | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++
 include/block/block.h |   4 ++
 qemu-img.c            |  30 +++++----------
 3 files changed, 114 insertions(+), 21 deletions(-)
block.c +101 -0
index b88ad2f..53b1a01 100644
@@ -4431,6 +4431,107 @@ bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie)
     bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns;
 }
 
+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, absolute or relative path is OK.
+ * @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 inode (-1 if it doesn't
+ *                exist) will insert into hash table directly. Loop check
+ *                starts from it. Absolute or relative path is OK for it.
+ * @backing_format: format of backing file
+ *
+ * If return true, stands for a backing file loop exists or some error
+ * happened. If return false, everything is ok.
+ */
+bool bdrv_backing_file_loop_check(const char *filename, const char *fmt,
+                                  bool chain, const char *backing_file,
+                                  const char *backing_format) {
+    GHashTable *inodes;
+    BlockDriverState *bs;
+    BlockDriver *drv;
+    struct stat sbuf;
+    long inode = 0;
+    int ret;
+    char fbuf[1024];
+
+    inodes = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
+
+    if (backing_file) {
+        /* Check if file exists. */
+        if (access(filename, F_OK)) {
+            inode = -1;
+        } else {
+            if (stat(filename, &sbuf) == -1) {
+                error_report("Get file %s stat failed.", filename);
+                goto err;
+            }
+            inode = (long)sbuf.st_ino;
+        }
+
+        filename = backing_file;
+        fmt = backing_format;
+        g_hash_table_insert(inodes, (gpointer)&inode, NULL);
+    }
+
+    while (filename && (filename[0] != '\0')) {
+        if (stat(filename, &sbuf) == -1) {
+                error_report("Get file %s stat failed.", filename);
+                goto err;
+        }
+        inode = (long)sbuf.st_ino;
+
+        if (g_hash_table_lookup_extended(inodes, &inode, NULL, NULL)) {
+            error_report("Backing file '%s' creates an infinite loop.",
+                         filename);
+            goto err;
+        }
+        g_hash_table_insert(inodes, (gpointer)&inode, NULL);
+
+        bs = bdrv_new("image");
+
+        if (fmt) {
+            drv = bdrv_find_format(fmt);
+            if (!drv) {
+                error_report("Unknown file format '%s'", fmt);
+                goto err;
+            }
+        } else {
+            drv = NULL;
+        }
+
+        ret = bdrv_open(bs, filename, NULL,
+                        BDRV_O_CACHE_WB | BDRV_O_NO_BACKING, drv);
+        if (ret < 0) {
+            error_report("Could not open '%s': %s", filename, strerror(-ret));
+            goto err;
+        }
+
+        filename = fmt = NULL;
+        if (chain) {
+            bdrv_get_backing_filename(bs, fbuf, sizeof(fbuf));
+            filename = fbuf;
+        }
+
+        bdrv_delete(bs);
+    }
+    g_hash_table_destroy(inodes);
+    return false;
+
+err:
+    g_hash_table_destroy(inodes);
+    return true;
+}
+
 void bdrv_img_create(const char *filename, const char *fmt,
                      const char *base_filename, const char *base_fmt,
                      char *options, uint64_t img_size, int flags,
include/block/block.h +4 -0
index 2307f67..6c631f2 100644
@@ -332,6 +332,10 @@ int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
 int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
                       int64_t pos, int size);
 
+bool bdrv_backing_file_loop_check(const char *filename, const char *fmt,
+                             bool chain, const char *backing_file,
+                             const char *backing_format);
+
 void bdrv_img_create(const char *filename, const char *fmt,
                      const char *base_filename, const char *base_fmt,
                      char *options, uint64_t img_size, int flags,
qemu-img.c +9 -21
index 809b4f1..48284c5 100644
@@ -1620,11 +1620,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 +1637,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 (bdrv_backing_file_loop_check(filename, fmt, chain, NULL, 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 +1681,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;
 }
 
Signature · 1 line
1.8.1.4
#2 lore raw

Re: [Qemu-devel] [PATCH V2 1/5] Refine and export infinite loop checking in collect_image_info_list()

Quoted 17 lines · Xu Wang
---
 block.c               | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++
 include/block/block.h |   4 ++
 qemu-img.c            |  30 +++++----------
 3 files changed, 114 insertions(+), 21 deletions(-)
block.c +2 -0
index b88ad2f..53b1a01 100644
@@ -4431,6 +4431,107 @@ bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie)
     bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns;
 }
 
+static gboolean str_equal_func(gconstpointer a, gconstpointer b)
+{

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4445

Quoted 10 lines
block.c +10 -0
+    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, absolute or relative path is OK.
+ * @fmt: topmost image format (may be NULL to autodetect)
+ * @head: head of ImageInfo list. If not need please set head to null.

This parameter is removed, you can also remove the comment.

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4447

Quoted 2 lines
block.c +2 -0
+ * @chain: true  - enumerate entire backing file chain
+ *         false - only topmost image file

Does it make sense when chain==false for loop check?

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4451

Quoted 4 lines
block.c +4 -0
+ * @backing_file: if this value is set, @filename inode (-1 if it doesn't
+ *                exist) will insert into hash table directly. Loop check
+ *                starts from it. Absolute or relative path is OK for it.
+ * @backing_format: format of backing file

Why these two parameters? The caller can still pass bs->backing_file to filename and bs->backing_fmt to fmt without them, to skip the topmost.

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4454

Quoted 3 lines
block.c +3 -0
+ *
+ * If return true, stands for a backing file loop exists or some error
+ * happened. If return false, everything is ok.

Please format like this:
+ * Returns: true for backing file loop, false for no loop.

Quoted 23 lines
block.c +23 -0
+ */
+bool bdrv_backing_file_loop_check(const char *filename, const char *fmt,
+                                  bool chain, const char *backing_file,
+                                  const char *backing_format) {
+    GHashTable *inodes;
+    BlockDriverState *bs;
+    BlockDriver *drv;
+    struct stat sbuf;
+    long inode = 0;
+    int ret;
+    char fbuf[1024];
+
+    inodes = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
+
+    if (backing_file) {
+        /* Check if file exists. */
+        if (access(filename, F_OK)) {
+            inode = -1;
+        } else {
+            if (stat(filename, &sbuf) == -1) {
+                error_report("Get file %s stat failed.", filename);
+                goto err;
+            }

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4487

Quoted 10 lines
block.c +10 -0
+            inode = (long)sbuf.st_ino;
+        }
+
+        filename = backing_file;
+        fmt = backing_format;
+        g_hash_table_insert(inodes, (gpointer)&inode, NULL);
+    }
+
+    while (filename && (filename[0] != '\0')) {
+        if (stat(filename, &sbuf) == -1) {

Does it mean stat() on backing_file twice if it's not NULL? As you assigned backing_file to filename above.

Quoted 9 lines
block.c +9 -0
+                error_report("Get file %s stat failed.", filename);
+                goto err;
+        }
+        inode = (long)sbuf.st_ino;
+
+        if (g_hash_table_lookup_extended(inodes, &inode, NULL, NULL)) {
+            error_report("Backing file '%s' creates an infinite loop.",
+                         filename);
+            goto err;

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4506

Quoted 10 lines
block.c +10 -0
+        }
+        g_hash_table_insert(inodes, (gpointer)&inode, NULL);
+
+        bs = bdrv_new("image");
+
+        if (fmt) {
+            drv = bdrv_find_format(fmt);
+            if (!drv) {
+                error_report("Unknown file format '%s'", fmt);
+                goto err;

bs is leaked.

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4514

Quoted 8 lines
block.c +8 -0
+            }
+        } else {
+            drv = NULL;
+        }
+
+        ret = bdrv_open(bs, filename, NULL,
+                        BDRV_O_CACHE_WB | BDRV_O_NO_BACKING, drv);
+        if (ret < 0) {

bs is leaked.

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4517

Quoted 3 lines
block.c +3 -0
+            error_report("Could not open '%s': %s", filename, strerror(-ret));
+            goto err;
+        }

These lines looks like a copy & paste from bdrv_new_open(). Could you just call it?

Quoted 25 lines
block.c +17 -0
+
+        filename = fmt = NULL;
+        if (chain) {
+            bdrv_get_backing_filename(bs, fbuf, sizeof(fbuf));
+            filename = fbuf;
+        }
+
+        bdrv_delete(bs);
+    }
+    g_hash_table_destroy(inodes);
+    return false;
+
+err:
+    g_hash_table_destroy(inodes);
+    return true;
+}
+
 void bdrv_img_create(const char *filename, const char *fmt,
                      const char *base_filename, const char *base_fmt,
                      char *options, uint64_t img_size, int flags,
include/block/block.h +0 -0
index 2307f67..6c631f2 100644

include/block/block.h int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, · line 337

Quoted 6 lines
include/block/block.h +3 -0
@@ -332,6 +332,10 @@ int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
 int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
                       int64_t pos, int size);
 
+bool bdrv_backing_file_loop_check(const char *filename, const char *fmt,
+                             bool chain, const char *backing_file,
+                             const char *backing_format);

Please align to parameter beginning:
bool chain, const char *backing_file, const char *backing_format);

Quoted 23 lines
include/block/block.h +1 -0
+
 void bdrv_img_create(const char *filename, const char *fmt,
                      const char *base_filename, const char *base_fmt,
                      char *options, uint64_t img_size, int flags,
qemu-img.c +0 -5
index 809b4f1..48284c5 100644
@@ -1620,11 +1620,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 +1637,18 @@ static ImageInfoList *collect_image_info_list(const char *filename,
                                               bool chain)
 {

qemu-img.c static ImageInfoList *collect_image_info_list(const char *filename, · line 1645

Quoted 10 lines
qemu-img.c +4 -3
     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);
+        

Please avoid trailing whitespaces. You could use scripts/checkpatch.pl to check your patch for coding style problems.

Quoted 38 lines
qemu-img.c +5 -13
+    /* check if loop exists and build ImageInfoList */
+    if (bdrv_backing_file_loop_check(filename, fmt, chain, NULL, 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 +1681,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;
 }
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Fam
#3 lore raw

Re: [Qemu-devel] [PATCH V2 1/5] Refine and export infinite loop checking in collect_image_info_list()

Quoted 14 lines · Fam Zheng
Quoted 14 lines
block.c +14 -0
+
+    if (backing_file) {
+        /* Check if file exists. */
+        if (access(filename, F_OK)) {
+            inode = -1;
+        } else {
+            if (stat(filename, &sbuf) == -1) {
+                error_report("Get file %s stat failed.", filename);
+                goto err;
+            }
+            inode = (long)sbuf.st_ino;
+        }
+
+        filename = backing_file;
Quoted 8 lines

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4487

Quoted 6 lines
block.c +6 -0
+        fmt = backing_format;
+        g_hash_table_insert(inodes, (gpointer)&inode, NULL);
+    }
+
+    while (filename && (filename[0] != '\0')) {
+        if (stat(filename, &sbuf) == -1) {

Does it mean stat() on backing_file twice if it's not NULL? As you assigned backing_file to filename above.

I was wrong, the first stat() is on filename. Please ignore this noise.

author
#4 lore raw

Re: [Qemu-devel] [PATCH V2 1/5] Refine and export infinite loop checking in collect_image_info_list()

In reply to Fam Zheng

2013/7/10 Fam Zheng <[email protected]>

Quoted 19 lines · Xu Wang
Quoted 3 lines
---
 block.c               | 101

++++++++++++++++++++++++++++++++++++++++++++++++++

block.c bdrv_acct_done(BlockDriverState *bs,

Quoted 9 lines
 include/block/block.h |   4 ++
 qemu-img.c            |  30 +++++----------
 3 files changed, 114 insertions(+), 21 deletions(-)
block.c +0 -0
index b88ad2f..53b1a01 100644
@@ -4431,6 +4431,107 @@ bdrv_acct_done(BlockDriverState *bs,

BlockAcctCookie *cookie)

block.c bdrv_acct_done(BlockDriverState *bs, · line 4431

Quoted 1 line
block.c +0 -0
     bs->total_time_ns[cookie->type] += get_clock() -

cookie->start_time_ns;

Quoted 2 lines
block.c +0 -0
 }
 
Quoted 14 lines

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4445

Quoted 12 lines
block.c +12 -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, absolute or relative path is OK.
+ * @fmt: topmost image format (may be NULL to autodetect)
+ * @head: head of ImageInfo list. If not need please set head to null.

This parameter is removed, you can also remove the comment.

Sorry. I'll update in new version.

Quoted 4 lines

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4447

Quoted 2 lines
block.c +2 -0
+ * @chain: true  - enumerate entire backing file chain
+ *         false - only topmost image file

Does it make sense when chain==false for loop check?

If chain==false, it means that backing file chain check will be ignored. It just
be used to be compatible with original function variables. However, the caller
calls bdrv_backing_file_loop_check() means that 'hey, you should check backing
files and tell me if there is a loop in it'. So I will remove it and do @chain logic
before call bdrv_backing_file_loop_check() in collect_image_info_list().

Quoted 10 lines

block.c bdrv_acct_done(BlockDriverState *bs, · line 4448

Quoted 1 line
block.c +1 -0
+ * @backing_file: if this value is set, @filename inode (-1 if it

doesn't

block.c bdrv_acct_done(BlockDriverState *bs, · line 4449

Quoted 1 line
block.c +1 -0
+ *                exist) will insert into hash table directly. Loop

check

block.c bdrv_acct_done(BlockDriverState *bs, · line 4450

Quoted 1 line
block.c +1 -0
+ *                starts from it. Absolute or relative path is OK for

it.

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4451

Quoted 1 line
block.c +1 -0
+ * @backing_format: format of backing file

Why these two parameters? The caller can still pass bs->backing_file to filename and bs->backing_fmt to fmt without them, to skip the topmost.

If @filename just a filename to be created, it will couldn't get the backing file name by calling bdrv_get_backing_file(). So caller could pass them by these
parameters to tell function 'The filename is pointed but it may just a filename, you
couldn't get backing file from it. But I'll give it by parameter'.

Quoted 6 lines

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4454

Quoted 3 lines
block.c +3 -0
+ *
+ * If return true, stands for a backing file loop exists or some error
+ * happened. If return false, everything is ok.

Please format like this:
+ * Returns: true for backing file loop, false for no loop.

OK, I'll update it in new version These are my first patches list for qemu. I will
do my best to learn rules about it as soon as possible. Thank you very much for the advice from all of yours:-)

Quoted 64 lines

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie)

Quoted 13 lines
block.c +13 -0
+ */
+bool bdrv_backing_file_loop_check(const char *filename, const char *fmt,
+                                  bool chain, const char *backing_file,
+                                  const char *backing_format) {
+    GHashTable *inodes;
+    BlockDriverState *bs;
+    BlockDriver *drv;
+    struct stat sbuf;
+    long inode = 0;
+    int ret;
+    char fbuf[1024];
+
+    inodes = g_hash_table_new_full(g_str_hash, str_equal_func, NULL,

NULL);

Quoted 10 lines
block.c +10 -0
+
+    if (backing_file) {
+        /* Check if file exists. */
+        if (access(filename, F_OK)) {
+            inode = -1;
+        } else {
+            if (stat(filename, &sbuf) == -1) {
+                error_report("Get file %s stat failed.", filename);
+                goto err;
+            }

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4487

Quoted 10 lines
block.c +10 -0
+            inode = (long)sbuf.st_ino;
+        }
+
+        filename = backing_file;
+        fmt = backing_format;
+        g_hash_table_insert(inodes, (gpointer)&inode, NULL);
+    }
+
+    while (filename && (filename[0] != '\0')) {
+        if (stat(filename, &sbuf) == -1) {

Does it mean stat() on backing_file twice if it's not NULL? As you assigned backing_file to filename above.

Quoted 9 lines
block.c +9 -0
+                error_report("Get file %s stat failed.", filename);
+                goto err;
+        }
+        inode = (long)sbuf.st_ino;
+
+        if (g_hash_table_lookup_extended(inodes, &inode, NULL, NULL)) {
+            error_report("Backing file '%s' creates an infinite loop.",
+                         filename);
+            goto err;

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4506

Quoted 10 lines
block.c +10 -0
+        }
+        g_hash_table_insert(inodes, (gpointer)&inode, NULL);
+
+        bs = bdrv_new("image");
+
+        if (fmt) {
+            drv = bdrv_find_format(fmt);
+            if (!drv) {
+                error_report("Unknown file format '%s'", fmt);
+                goto err;

bs is leaked.

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4514

Quoted 8 lines
block.c +8 -0
+            }
+        } else {
+            drv = NULL;
+        }
+
+        ret = bdrv_open(bs, filename, NULL,
+                        BDRV_O_CACHE_WB | BDRV_O_NO_BACKING, drv);
+        if (ret < 0) {
Quoted 2 lines

bs is leaked.

bdrv_delete will be added in err.

Quoted 7 lines

block.c bdrv_acct_done(BlockDriverState *bs, · line 4515

Quoted 1 line
block.c +1 -0
+            error_report("Could not open '%s': %s", filename,

strerror(-ret));

block.c bdrv_acct_done(BlockDriverState *bs, · line 4517

Quoted 2 lines
block.c +2 -0
+            goto err;
+        }

These lines looks like a copy & paste from bdrv_new_open(). Could you just call it?

bdrv_new_open() is implemented as a static function in qemu-img.c and qemu-img.c
includes block.c (). If bdrv_backing_file_loop_check() calls bdrv_new_open(),
It's really a bit strange. If it's needed to make bdrv_new_open() moved to block.c,
I'll do work for it.

Quoted 29 lines

include/block/block.h int bdrv_save_vmstate(BlockDriverState *bs, const

Quoted 25 lines
block.c +17 -0
+
+        filename = fmt = NULL;
+        if (chain) {
+            bdrv_get_backing_filename(bs, fbuf, sizeof(fbuf));
+            filename = fbuf;
+        }
+
+        bdrv_delete(bs);
+    }
+    g_hash_table_destroy(inodes);
+    return false;
+
+err:
+    g_hash_table_destroy(inodes);
+    return true;
+}
+
 void bdrv_img_create(const char *filename, const char *fmt,
                      const char *base_filename, const char *base_fmt,
                      char *options, uint64_t img_size, int flags,
include/block/block.h +0 -0
index 2307f67..6c631f2 100644
@@ -332,6 +332,10 @@ int bdrv_save_vmstate(BlockDriverState *bs, const

uint8_t *buf,

Quoted 3 lines
include/block/block.h +0 -0
 int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
                       int64_t pos, int size);
 
Quoted 7 lines

include/block/block.h int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, · line 337

Quoted 3 lines
include/block/block.h +3 -0
+bool bdrv_backing_file_loop_check(const char *filename, const char *fmt,
+                             bool chain, const char *backing_file,
+                             const char *backing_format);

Please align to parameter beginning:
bool chain, const char *backing_file, const char *backing_format);

Sorry for that.

Quoted 34 lines

qemu-img.c static void

Quoted 9 lines
include/block/block.h +1 -0
+
 void bdrv_img_create(const char *filename, const char *fmt,
                      const char *base_filename, const char *base_fmt,
                      char *options, uint64_t img_size, int flags,
qemu-img.c +0 -0
index 809b4f1..48284c5 100644
@@ -1620,11 +1620,6 @@ static void

dump_human_image_info_list(ImageInfoList *list)

qemu-img.c static ImageInfoList

Quoted 12 lines
qemu-img.c +0 -5
     }
 }
 
-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 +1637,18 @@ static ImageInfoList

*collect_image_info_list(const char *filename,

qemu-img.c static ImageInfoList *collect_image_info_list(const char *filename,

Quoted 11 lines
qemu-img.c +3 -3
                                               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,
Quoted 5 lines

NULL);

qemu-img.c static ImageInfoList · line 1645

Quoted 1 line
qemu-img.c +1 -0
+

Please avoid trailing whitespaces. You could use scripts/checkpatch.pl to check your patch for coding style problems.

OK, I'll do that.

Xu Wang

Quoted 46 lines

qemu-img.c static ImageInfoList · line 1647

Quoted 2 lines
qemu-img.c +2 -0
+    /* check if loop exists and build ImageInfoList */
+    if (bdrv_backing_file_loop_check(filename, fmt, chain, NULL, NULL))

{

qemu-img.c static ImageInfoList *collect_image_info_list(const char *filename,

Quoted 9 lines
qemu-img.c +2 -5
+        goto err;
+    }
 
     while (filename) {
-        BlockDriverState *bs;
-        ImageInfo *info;
-        ImageInfoList *elem;
-
-        if (g_hash_table_lookup_extended(filenames, filename, NULL,

NULL)) {

qemu-img.c static ImageInfoList *collect_image_info_list(const char *filename,

Quoted 7 lines
qemu-img.c +0 -6
-            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,

qemu-img.c static ImageInfoList

Quoted 3 lines
qemu-img.c +0 -0
                            false, false);
         if (!bs) {
@@ -1692,12 +1681,11 @@ static ImageInfoList

*collect_image_info_list(const char *filename,

Quoted 14 lines
qemu-img.c +1 -3
             }
         }
     }
-    g_hash_table_destroy(filenames);
+
     return head;
 
 err:
     qapi_free_ImageInfoList(head);
-    g_hash_table_destroy(filenames);
     return NULL;
 }
 
--
Quoted 3 lines

1.8.1.4

--
Fam

author
#5 lore raw

PATCH v2 2/5 Add WIN32 platform support for backing_file_loop_check()

In reply to Xu Wang
---
 block.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)
block.c +94 -0
index 53b1a01..8dc6ded 100644
@@ -4431,6 +4431,83 @@ bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie)
     bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns;
 }
 
+#ifdef _WIN32
+static int get_lnk_target_file(const char *lnk_file, char *filepath)
+{
+    unsigned int flag, offet;
+    unsigned int sflag;
+    char uch;
+    int i = 0;
+
+    FILE *fd = fopen(lnk_file, "rb");
+    if (!fd) {
+        error_report("Open file %s failed.", lnk_file);
+        return -1;
+    }
+    fread(&flag, 4, 1, fd);
+    if (flag != 0x4c) {
+        error_report("%s is not a lnk file.", lnk_file);
+        fclose(fd);
+        return -1;
+    }
+    fseek(fd, 0x14, SEEK_SET);
+    fread(&flag, 4, 1, fd);
+    fseek(fd, 0x4c, SEEK_SET);
+
+    if (flag & 0x01) {
+        fread(&sflag, 2, 1, fd);
+        fseek(fd, sflag, SEEK_CUR);
+    }
+
+    offset = ftell(fd);
+    fseek(fd, offset + 0x10, SEEK_SET);
+    fread(&flag, 4, 1, fd);
+    fseek(fd, flag + offset, SEEK_SET);
+
+    do {
+        fread(&uch, 1, 1, fd);
+        filepath[i++] = uch;
+    } while (uch != '\0');
+
+    fclose(fd);
+    return 0;
+}
+
+static long get_win_inode(const char *filename)
+{
+    char pbuf[MAX_PATH_LEN], *p;
+    long inode;
+    struct stat sbuf;
+    char path[MAX_PATH_LEN];
+
+    /* If filename contains .lnk, it's a shortcuts. Target file
+ *      * need to be parsed.
+ *           */
+    if (strstr(filename, ".lnk")) {
+        if (get_lnk_target_file(filename, path)) {
+            error_report("Parse .lnk file %s failed.", filename);
+            return -1;
+        }
+    } else {
+        memcpy(path, filename, sizeof(filename));
+    }
+
+    if (stat(path, &sbuf) == -1) {
+        error_report("get file %s stat error.", path);
+        return -1;
+    }
+    if (GetFullPathName(path, MAX_PATH_LEN, pbuf, &p) != 0) {
+        inode = 11003;
+        for (p = pbuf; *p != '\0'; p++) {
+            inode = inode * 31 + *(unsigned char *)p;
+        }
+        return (inode * 911) & 0x7FFF;
+    }
+
+    return -1;
+}
+#endif
+
 static gboolean str_equal_func(gconstpointer a, gconstpointer b)
 {
     return strcmp(a, b) == 0;
@@ -4475,7 +4552,15 @@ bool bdrv_backing_file_loop_check(const char *filename, const char *fmt,
                 error_report("Get file %s stat failed.", filename);
                 goto err;
             }
+            #ifdef _WIN32
+            inode = get_win_inode(filename);
+            if (inode == -1) {
+                error_report("Get file %s inode failed.", filename);
+                goto err;
+            }
+            #else
             inode = (long)sbuf.st_ino;
+            #endif
         }
 
         filename = backing_file;
@@ -4488,7 +4573,16 @@ bool bdrv_backing_file_loop_check(const char *filename, const char *fmt,
                 error_report("Get file %s stat failed.", filename);
                 goto err;
         }
+
+        #ifdef _WIN32
+        inode = get_win_inode(filename);
+        if (inode == -1) {
+            error_report("Get file %s inode failed.", filename);
+            goto err;
+        }
+        #else
         inode = (long)sbuf.st_ino;
+        #endif
 
         if (g_hash_table_lookup_extended(inodes, &inode, NULL, NULL)) {
             error_report("Backing file '%s' creates an infinite loop.",
Signature · 1 line
1.8.1.4
#6 lore raw

Re: [Qemu-devel] [PATCH V2 2/5] Add WIN32 platform support for backing_file_loop_check()

Quoted 40 lines · Xu Wang
---
 block.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)
block.c +27 -0
index 53b1a01..8dc6ded 100644
@@ -4431,6 +4431,83 @@ bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie)
     bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns;
 }
 
+#ifdef _WIN32
+static int get_lnk_target_file(const char *lnk_file, char *filepath)
+{
+    unsigned int flag, offet;
+    unsigned int sflag;
+    char uch;
+    int i = 0;
+
+    FILE *fd = fopen(lnk_file, "rb");
+    if (!fd) {
+        error_report("Open file %s failed.", lnk_file);
+        return -1;
+    }
+    fread(&flag, 4, 1, fd);
+    if (flag != 0x4c) {
+        error_report("%s is not a lnk file.", lnk_file);
+        fclose(fd);
+        return -1;
+    }
+    fseek(fd, 0x14, SEEK_SET);
+    fread(&flag, 4, 1, fd);
+    fseek(fd, 0x4c, SEEK_SET);
+
+    if (flag & 0x01) {
+        fread(&sflag, 2, 1, fd);
+        fseek(fd, sflag, SEEK_CUR);
+    }

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4470

Quoted 10 lines
block.c +10 -0
+
+    offset = ftell(fd);
+    fseek(fd, offset + 0x10, SEEK_SET);
+    fread(&flag, 4, 1, fd);
+    fseek(fd, flag + offset, SEEK_SET);
+
+    do {
+        fread(&uch, 1, 1, fd);
+        filepath[i++] = uch;
+    } while (uch != '\0');

Please limit the number of bytes to read to capacity of filepath, avoid the security hole.

Quoted 6 lines
block.c +6 -0
+
+    fclose(fd);
+    return 0;
+}
+
+static long get_win_inode(const char *filename)

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4486

Quoted 10 lines
block.c +10 -0
+{
+    char pbuf[MAX_PATH_LEN], *p;
+    long inode;
+    struct stat sbuf;
+    char path[MAX_PATH_LEN];
+
+    /* If filename contains .lnk, it's a shortcuts. Target file
+ *      * need to be parsed.
+ *           */
+    if (strstr(filename, ".lnk")) {

Please be more accurate with this checking, what if the filename happens to be "a.lnked.txt"?

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4495

Quoted 9 lines
block.c +9 -0
+        if (get_lnk_target_file(filename, path)) {
+            error_report("Parse .lnk file %s failed.", filename);
+            return -1;
+        }
+    } else {
+        memcpy(path, filename, sizeof(filename));
+    }
+
+    if (stat(path, &sbuf) == -1) {

What's this stat() call for?

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4499

Quoted 4 lines
block.c +4 -0
+        error_report("get file %s stat error.", path);
+        return -1;
+    }
+    if (GetFullPathName(path, MAX_PATH_LEN, pbuf, &p) != 0) {

How big is MAX_PATH_LEN?
MSDN: If the buffer is too small to contain the path, the return value is the size, in TCHARs, of the buffer that is required to hold the path and the terminating null character. Please try to handle this case. (And is pbuf NULL terminated in this case?)

Quoted 15 lines
block.c +11 -0
+        inode = 11003;
+        for (p = pbuf; *p != '\0'; p++) {
+            inode = inode * 31 + *(unsigned char *)p;
+        }
+        return (inode * 911) & 0x7FFF;
+    }
+
+    return -1;
+}
+#endif
+
 static gboolean str_equal_func(gconstpointer a, gconstpointer b)
 {
     return strcmp(a, b) == 0;

block.c bool bdrv_backing_file_loop_check(const char *filename, const char *fmt, · line 4561

Quoted 10 lines
block.c +7 -0
@@ -4475,7 +4552,15 @@ bool bdrv_backing_file_loop_check(const char *filename, const char *fmt,
                 error_report("Get file %s stat failed.", filename);
                 goto err;
             }
+            #ifdef _WIN32
+            inode = get_win_inode(filename);
+            if (inode == -1) {
+                error_report("Get file %s inode failed.", filename);
+                goto err;
+            }
+            #else

Move stat() call here? (seems not necessary for windows)

Quoted 8 lines
block.c +1 -0
             inode = (long)sbuf.st_ino;
+            #endif
         }
 
         filename = backing_file;
@@ -4488,7 +4573,16 @@ bool bdrv_backing_file_loop_check(const char *filename, const char *fmt,
                 error_report("Get file %s stat failed.", filename);
                 goto err;

block.c bool bdrv_backing_file_loop_check(const char *filename, const char *fmt, · line 4584

Quoted 10 lines
block.c +8 -0
         }
+
+        #ifdef _WIN32
+        inode = get_win_inode(filename);
+        if (inode == -1) {
+            error_report("Get file %s inode failed.", filename);
+            goto err;
+        }
+        #else
         inode = (long)sbuf.st_ino;

See above.

Quoted 8 lines
block.c +1 -0
+        #endif
 
         if (g_hash_table_lookup_extended(inodes, &inode, NULL, NULL)) {
             error_report("Backing file '%s' creates an infinite loop.",
Signature · 1 line
1.8.1.4
Signature · 1 line
Fam
author
#7 lore raw

Re: [Qemu-devel] [PATCH V2 2/5] Add WIN32 platform support for backing_file_loop_check()

2013/7/10 Fam Zheng <[email protected]>

Quoted 48 lines · Xu Wang
Quoted 3 lines
---
 block.c | 94

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

block.c bdrv_acct_done(BlockDriverState *bs,

Quoted 7 lines
 1 file changed, 94 insertions(+)
block.c +0 -0
index 53b1a01..8dc6ded 100644
@@ -4431,6 +4431,83 @@ bdrv_acct_done(BlockDriverState *bs,

BlockAcctCookie *cookie)

block.c bdrv_acct_done(BlockDriverState *bs, · line 4431

Quoted 1 line
block.c +0 -0
     bs->total_time_ns[cookie->type] += get_clock() -

cookie->start_time_ns;

Quoted 33 lines
block.c +31 -0
 }
 
+#ifdef _WIN32
+static int get_lnk_target_file(const char *lnk_file, char *filepath)
+{
+    unsigned int flag, offet;
+    unsigned int sflag;
+    char uch;
+    int i = 0;
+
+    FILE *fd = fopen(lnk_file, "rb");
+    if (!fd) {
+        error_report("Open file %s failed.", lnk_file);
+        return -1;
+    }
+    fread(&flag, 4, 1, fd);
+    if (flag != 0x4c) {
+        error_report("%s is not a lnk file.", lnk_file);
+        fclose(fd);
+        return -1;
+    }
+    fseek(fd, 0x14, SEEK_SET);
+    fread(&flag, 4, 1, fd);
+    fseek(fd, 0x4c, SEEK_SET);
+
+    if (flag & 0x01) {
+        fread(&sflag, 2, 1, fd);
+        fseek(fd, sflag, SEEK_CUR);
+    }
+
+    offset = ftell(fd);
+    fseek(fd, offset + 0x10, SEEK_SET);
+    fread(&flag, 4, 1, fd);
Quoted 9 lines

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4470

Quoted 6 lines
block.c +6 -0
+    fseek(fd, flag + offset, SEEK_SET);
+
+    do {
+        fread(&uch, 1, 1, fd);
+        filepath[i++] = uch;
+    } while (uch != '\0');

Please limit the number of bytes to read to capacity of filepath, avoid the security hole.

@i was limited in [0, MAX_PATH_LEN] in the new version.

Quoted 10 lines
Quoted 10 lines
block.c +10 -0
+
+    fclose(fd);
+    return 0;
+}
+
+static long get_win_inode(const char *filename)
+{
+    char pbuf[MAX_PATH_LEN], *p;
+    long inode;
+    struct stat sbuf;
Quoted 9 lines

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4486

Quoted 6 lines
block.c +6 -0
+    char path[MAX_PATH_LEN];
+
+    /* If filename contains .lnk, it's a shortcuts. Target file
+ *      * need to be parsed.
+ *           */
+    if (strstr(filename, ".lnk")) {

Please be more accurate with this checking, what if the filename happens to be "a.lnked.txt"?

Generally speaking filename has no more than one extenstion name on Windows (I just found some virus could break that rule). Maybe I have not enough experience
on WIN32 plateform, so I updated code will check whether the filename is end with
'.lnk'.

Quoted 11 lines

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4495

Quoted 9 lines
block.c +9 -0
+        if (get_lnk_target_file(filename, path)) {
+            error_report("Parse .lnk file %s failed.", filename);
+            return -1;
+        }
+    } else {
+        memcpy(path, filename, sizeof(filename));
+    }
+
+    if (stat(path, &sbuf) == -1) {

What's this stat() call for?

I read a lot code about get inode on the WIN32 plateform. All of them have stat calling.
The function of stat() here is checking file by calling stat() if there was something wrong
with this file, the following caculation is useless and return -1 directly.

Quoted 10 lines

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4499

Quoted 4 lines
block.c +4 -0
+        error_report("get file %s stat error.", path);
+        return -1;
+    }
+    if (GetFullPathName(path, MAX_PATH_LEN, pbuf, &p) != 0) {

How big is MAX_PATH_LEN?
MSDN: If the buffer is too small to contain the path, the return value is the size, in TCHARs, of the buffer that is required to hold the path and the terminating null character. Please try to handle this case. (And is pbuf NULL terminated in this case?)

This is really a hard desicion to set value of it because length of path on Windows
could be unlimited. So here I just set an value and want to get some tips from others.
Now it's set as 8192 but I missed it when I made this patch.

Quoted 26 lines

block.c bool bdrv_backing_file_loop_check(const char

Quoted 15 lines
block.c +11 -0
+        inode = 11003;
+        for (p = pbuf; *p != '\0'; p++) {
+            inode = inode * 31 + *(unsigned char *)p;
+        }
+        return (inode * 911) & 0x7FFF;
+    }
+
+    return -1;
+}
+#endif
+
 static gboolean str_equal_func(gconstpointer a, gconstpointer b)
 {
     return strcmp(a, b) == 0;
@@ -4475,7 +4552,15 @@ bool bdrv_backing_file_loop_check(const char

*filename, const char *fmt,

block.c bool bdrv_backing_file_loop_check(const char *filename, const char *fmt, · line 4561

Quoted 10 lines
block.c +7 -0
                 error_report("Get file %s stat failed.", filename);
                 goto err;
             }
+            #ifdef _WIN32
+            inode = get_win_inode(filename);
+            if (inode == -1) {
+                error_report("Get file %s inode failed.", filename);
+                goto err;
+            }
+            #else
Quoted 2 lines

Move stat() call here? (seems not necessary for windows)

I decided to move all stat() into get_inode (I changed its name from get_win_inode()). Logic is more simpler now.

Xu Wang

Quoted 32 lines

block.c bool bdrv_backing_file_loop_check(const char

Quoted 6 lines
block.c +1 -0
             inode = (long)sbuf.st_ino;
+            #endif
         }
 
         filename = backing_file;
@@ -4488,7 +4573,16 @@ bool bdrv_backing_file_loop_check(const char

*filename, const char *fmt,

block.c bool bdrv_backing_file_loop_check(const char *filename, const char *fmt, · line 4584

Quoted 12 lines
block.c +8 -0
                 error_report("Get file %s stat failed.", filename);
                 goto err;
         }
+
+        #ifdef _WIN32
+        inode = get_win_inode(filename);
+        if (inode == -1) {
+            error_report("Get file %s inode failed.", filename);
+            goto err;
+        }
+        #else
         inode = (long)sbuf.st_ino;

See above.

Quoted 8 lines
block.c +1 -1
+        #endif
 
         if (g_hash_table_lookup_extended(inodes, &inode, NULL, NULL)) {
             error_report("Backing file '%s' creates an infinite loop.",
--

1.8.1.4

--
Fam

#8 lore raw

Re: [Qemu-devel] [PATCH V2 2/5] Add WIN32 platform support for backing_file_loop_check()

Quoted 13 lines · Xu Wang

2013/7/10 Fam Zheng <[email protected]>

Quoted 11 lines · Xu Wang

block.c bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) · line 4499

Quoted 4 lines
block.c +4 -0
+        error_report("get file %s stat error.", path);
+        return -1;
+    }
+    if (GetFullPathName(path, MAX_PATH_LEN, pbuf, &p) != 0) {

How big is MAX_PATH_LEN?
MSDN: If the buffer is too small to contain the path, the return value is the size, in TCHARs, of the buffer that is required to hold the path and the terminating null character. Please try to handle this case. (And is pbuf NULL terminated in this case?)

Quoted 6 lines

This is really a hard desicion to set value of it because length of path on Windows
could be unlimited. So here I just set an value and want to get some tips from others.
Now it's set as 8192 but I missed it when I made this patch.

Yes, if you use heap allocated buffer, you can work with longer filename. If you use fixed length buffer, ideally you should detect whether return value is greater than your buffer size: in this case the pbuf content is invalid (or incomplete), so your hash computation below is inaccurate, consequentially false alarm.

Signature · 1 line
Fam
author
#9 lore raw

PATCH v2 3/5 Check infinite loop in bdrv_img_create()

In reply to Xu Wang
---
 block.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
block.c +5 -5
index 8dc6ded..6df25d9 100644
@@ -4688,15 +4688,15 @@ void bdrv_img_create(const char *filename, const char *fmt,
     }
 
     backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
+    backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
     if (backing_file && backing_file->value.s) {
-        if (!strcmp(filename, backing_file->value.s)) {
-            error_setg(errp, "Error: Trying to create an image with the "
-                             "same filename as the backing file");
+        if (bdrv_backing_file_loop_check(filename, fmt, true,
+                                         backing_file->value.s, 
+                                         backing_fmt->value.s)) {
+            /* There is loop exists in the backing file chain */
             goto out;
         }
     }
-
-    backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
     if (backing_fmt && backing_fmt->value.s) {
         backing_drv = bdrv_find_format(backing_fmt->value.s);
         if (!backing_drv) {
Signature · 1 line
1.8.1.4
#10 lore raw

Re: [Qemu-devel] [PATCH V2 3/5] Check infinite loop in bdrv_img_create()

Quoted 10 lines · Xu Wang
---
 block.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
block.c +0 -0
index 8dc6ded..6df25d9 100644

block.c void bdrv_img_create(const char *filename, const char *fmt, · line 4694

Quoted 10 lines
block.c +3 -3
@@ -4688,15 +4688,15 @@ void bdrv_img_create(const char *filename, const char *fmt,
     }
 
     backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
+    backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
     if (backing_file && backing_file->value.s) {
-        if (!strcmp(filename, backing_file->value.s)) {
-            error_setg(errp, "Error: Trying to create an image with the "
-                             "same filename as the backing file");
+        if (bdrv_backing_file_loop_check(filename, fmt, true,
+                                         backing_file->value.s, 

Trailing whitespace.

block.c void bdrv_img_create(const char *filename, const char *fmt, · line 4696

Quoted 2 lines
block.c +2 -0
+                                         backing_fmt->value.s)) {
+            /* There is loop exists in the backing file chain */

I see why you pass both filename and backing_file to in now, filename is not created yet so bdrv_get_backing_filename is not usable, but it needs to be checked.

Signature · 1 line
Fam
author
#11 lore raw

PATCH v2 4/5 Add backing file loop check in change_backing_file()

In reply to Xu Wang
---
 block.c | 6 ++++++
 1 file changed, 6 insertions(+)
block.c +6 -0
index 6df25d9..379b79b 100644
@@ -1971,6 +1971,12 @@ int bdrv_change_backing_file(BlockDriverState *bs,
         return -EINVAL;
     }
 
+    /* Check if loop exists in backing files chain after changed */
+    if (bdrv_backing_file_loop_check(bs->filename, NULL, true, backing_file,
+                                     backing_fmt)) {
+        return -EIO;
+    }
+
     if (drv->bdrv_change_backing_file != NULL) {
         ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
     } else {
Signature · 1 line
1.8.1.4
#12 lore raw

Re: [Qemu-devel] [PATCH V2 4/5] Add backing file loop check in change_backing_file()

Quoted 10 lines · Xu Wang
---
 block.c | 6 ++++++
 1 file changed, 6 insertions(+)
block.c +0 -0
index 6df25d9..379b79b 100644

block.c int bdrv_change_backing_file(BlockDriverState *bs, · line 1975

Quoted 5 lines
block.c +2 -0
@@ -1971,6 +1971,12 @@ int bdrv_change_backing_file(BlockDriverState *bs,
         return -EINVAL;
     }
 
+    /* Check if loop exists in backing files chain after changed */
+    if (bdrv_backing_file_loop_check(bs->filename, NULL, true, backing_file,

You can use bs->drv->format_name for fmt.

Quoted 11 lines
block.c +4 -0
+                                     backing_fmt)) {
+        return -EIO;
+    }
+
     if (drv->bdrv_change_backing_file != NULL) {
         ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
     } else {
Signature · 1 line
1.8.1.4
Signature · 1 line
Fam
author
#13 lore raw

PATCH v2 5/5 Add infinite loop check in drive_init()

In reply to Xu Wang
---
 blockdev.c | 5 +++++
 1 file changed, 5 insertions(+)
blockdev.c +5 -0
index 5975dde..0178764 100644
@@ -695,6 +695,11 @@ DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
         error_report("warning: disabling copy_on_read on readonly drive");
     }
 
+    /* Add backing file loop check */
+    if (bdrv_backing_file_loop_check(file, NULL, true, NULL, NULL)) {
+        goto err;
+    }
+
     ret = bdrv_open(dinfo->bdrv, file, bs_opts, bdrv_flags, drv);
     bs_opts = NULL;
 
Signature · 1 line
1.8.1.4
#14 lore raw

Re: [Qemu-devel] [PATCH V2 5/5] Add infinite loop check in drive_init()

Quoted 10 lines · Xu Wang
---
 blockdev.c | 5 +++++
 1 file changed, 5 insertions(+)
blockdev.c +0 -0
index 5975dde..0178764 100644

blockdev.c DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type) · line 699

Quoted 5 lines
blockdev.c +2 -0
@@ -695,6 +695,11 @@ DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
         error_report("warning: disabling copy_on_read on readonly drive");
     }
 
+    /* Add backing file loop check */
+    if (bdrv_backing_file_loop_check(file, NULL, true, NULL, NULL)) {

You can pass drv->format_name as second parameter.

Quoted 10 lines
blockdev.c +3 -0
+        goto err;
+    }
+
     ret = bdrv_open(dinfo->bdrv, file, bs_opts, bdrv_flags, drv);
     bs_opts = NULL;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Fam
#15 lore raw
In reply to Xu Wang
Quoted 17 lines · Xu Wang

Updates:
1. Changed infinite loop check in collect_image_info_list() from filename checking to inode checking.
2. Absolute or relative path is OK for filename path.
3. Hard and soft link are works well.
4. Added WIN32 platform support (shortcuts could be recognized correctly.)
5. Create a file which contains loop in backing file will failed.
6. Start a vm which boot block file contains loop in backing file chain will failed instead of no response and segment fault.

Xu Wang (5):
Refine and export infinite loop checking in collect_image_info_list() Add WIN32 platform support for backing_file_loop_check() Check infinite loop in bdrv_img_create()
Add backing file loop check in change_backing_file()
Add infinite loop check in drive_init()

Quoted 6 lines
 block.c               | 211 ++++++++++++++++++++++++++++++++++++++++++++++++--
 blockdev.c            |   5 ++
 include/block/block.h |   4 +
 qemu-img.c            |  30 +++----
 4 files changed, 224 insertions(+), 26 deletions(-)

Thanks for the series. I made a few comments on each patch. You hashed windows full path to get the "inode", but case sensitively, would that work with the same files of different case?

Please prefix your commit message with "block:" or "qemu-img:", e.g.:

block: refactor collect_image_info_list()
block: Add WIN32 platform support for backing_file_loop_check() etc..

and maybe also describe some of the long patches, explaining what is changed.

Also please consider to write some test case on your changes, as requested in the replies to previous version.

Signature · 1 line
Fam
#16 lore raw

Am 10.07.2013 um 13:13 hat Fam Zheng geschrieben:

Quoted 25 lines · Xu Wang
Quoted 17 lines

Updates:
1. Changed infinite loop check in collect_image_info_list() from filename checking to inode checking.
2. Absolute or relative path is OK for filename path.
3. Hard and soft link are works well.
4. Added WIN32 platform support (shortcuts could be recognized correctly.)
5. Create a file which contains loop in backing file will failed.
6. Start a vm which boot block file contains loop in backing file chain will failed instead of no response and segment fault.

Xu Wang (5):
Refine and export infinite loop checking in collect_image_info_list() Add WIN32 platform support for backing_file_loop_check() Check infinite loop in bdrv_img_create()
Add backing file loop check in change_backing_file()
Add infinite loop check in drive_init()

Quoted 6 lines
 block.c               | 211 ++++++++++++++++++++++++++++++++++++++++++++++++--
 blockdev.c            |   5 ++
 include/block/block.h |   4 +
 qemu-img.c            |  30 +++----
 4 files changed, 224 insertions(+), 26 deletions(-)
Quoted 3 lines

Thanks for the series. I made a few comments on each patch. You hashed windows full path to get the "inode", but case sensitively, would that work with the same files of different case?

I seem to remember that Windows can be made case-sensitive. In this case, we must not ignore case. Not detecting some loops is okay (but here we would in fact detect it in the second iteration), rejecting valid configurations is not.

Kevin

#17 lore raw
Quoted 8 lines · Kevin Wolf
Quoted 3 lines

Thanks for the series. I made a few comments on each patch. You hashed windows full path to get the "inode", but case sensitively, would that work with the same files of different case?

I seem to remember that Windows can be made case-sensitive. In this case, we must not ignore case. Not detecting some loops is okay (but here we would in fact detect it in the second iteration), rejecting valid configurations is not.

Indeed, Windows can be made case-sensitive. But you still want to hash case-insensitive, and just be careful to not report collisions until after checking inode equality. For that matter, stat() on windows is notoriously lame (such as returning 0 for st_ino on FAT, regardless of file). While I've compiled on windows, it has generally been on cygwin (where they go to great lengths to give a sane stat() in spite of FAT's shortcomings); you may want to get a review from someone more intimately acquainted with mingw (Paolo?), in case there is no way to make inode equality work, and where you may be better off with absolute path name comparison.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org