diff mbox

block: Fix relative backing file path checking

Message ID 1386657774-9342-1-git-send-email-gesaint@linux.vnet.ibm.com
State New
Headers show

Commit Message

Xu Wang Dec. 10, 2013, 6:42 a.m. UTC
This patch is made for Bug #1257334 (diffuse handling of image
creation from another path). The cause of it is user could create
image even though the backing file doesn't exist. Becase backing
file checking in the bdrv_img_create() is from the user's current
path instead of relative path to the image to be created. This patch
makes qemu check backing file according to the relative path to the
image to be created. Hence if relative backing file path doesn't
exist, the backing file checking will fail now.

Test case:
  Reproduce process (from bug page):
    1. $mkdir a/
    2. $qemu-img create -f qcow2 a/blob.img 10G
    3. $qemu-img create -f qcow2 -b a/blob.img a/ovl.img
       Here the actual backing file of ovl.img is a/a/blob.img. But
       the backing file checking will check from the user's current
       path and find a/blob.img successfully. But the path saved in
       the ovl.img is a/a/blob.img. Bug occurred.

  After patched:
    The step 3 above an error message will be thrown because backing
    file checking started after got the full path of backing file
    intead of relative path.

Signed-off-by: Xu Wang <gesaint@linux.vnet.ibm.com>
---
 block.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

Comments

Eric Blake Dec. 12, 2013, 10:35 p.m. UTC | #1
On 12/09/2013 11:42 PM, Xu Wang wrote:
> This patch is made for Bug #1257334 (diffuse handling of image
> creation from another path). The cause of it is user could create
> image even though the backing file doesn't exist. Becase backing

s/Becase/, because/

> file checking in the bdrv_img_create() is from the user's current
> path instead of relative path to the image to be created. This patch
> makes qemu check backing file according to the relative path to the
> image to be created. Hence if relative backing file path doesn't
> exist, the backing file checking will fail now.
> 
> Test case:
>   Reproduce process (from bug page):
>     1. $mkdir a/
>     2. $qemu-img create -f qcow2 a/blob.img 10G
>     3. $qemu-img create -f qcow2 -b a/blob.img a/ovl.img
>        Here the actual backing file of ovl.img is a/a/blob.img. But
>        the backing file checking will check from the user's current
>        path and find a/blob.img successfully. But the path saved in
>        the ovl.img is a/a/blob.img. Bug occurred.
> 
>   After patched:
>     The step 3 above an error message will be thrown because backing
>     file checking started after got the full path of backing file
>     intead of relative path.

s/intead/instead/

> 
> Signed-off-by: Xu Wang <gesaint@linux.vnet.ibm.com>
> ---
>  block.c | 17 +++++++++++++++--
>  1 file changed, 15 insertions(+), 2 deletions(-)


> 
> diff --git a/block.c b/block.c
> index 13f001a..20d2b66 100644
> --- a/block.c
> +++ b/block.c
> @@ -4790,18 +4790,31 @@ void bdrv_img_create(const char *filename, const char *fmt,
>              uint64_t size;
>              char buf[32];
>              int back_flags;
> +            char backing_filename_full[PATH_MAX];

I hate code that uses PATH_MAX as an array size.  It is not portable to
systems like GNU Hurd.  But qemu doesn't compile on GNU Hurd, and you're
not the first user of this construct in the codebase (so the problem
already exists and should be independently cleaned up).  So I won't let
it hold up your patch.

> +                error_setg_errno(errp, -ret, "Backing file '%s'"
> +                                 "(actual path is '%s') error: %s",

Outputs "Backing file 'foo'(actual path...", which looks bad.  You need
a space in the error message either before or after the line break.

The rest of the patch looks okay.
diff mbox

Patch

diff --git a/block.c b/block.c
index 13f001a..20d2b66 100644
--- a/block.c
+++ b/block.c
@@ -4790,18 +4790,31 @@  void bdrv_img_create(const char *filename, const char *fmt,
             uint64_t size;
             char buf[32];
             int back_flags;
+            char backing_filename_full[PATH_MAX];
 
+            /* check if relative backing file path could be accessed */
+            if (backing_file->value.s[0] == '\0' ||
+                path_has_protocol(backing_file->value.s)) {
+                pstrcpy(backing_filename_full, sizeof(backing_filename_full),
+                        backing_file->value.s);
+            } else {
+                path_combine(backing_filename_full,
+                             sizeof(backing_filename_full),
+                             filename, backing_file->value.s);
+            }
             /* backing files always opened read-only */
             back_flags =
                 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
 
             bs = bdrv_new("");
 
-            ret = bdrv_open(bs, backing_file->value.s, NULL, back_flags,
+            ret = bdrv_open(bs, backing_filename_full, NULL, back_flags,
                             backing_drv, &local_err);
             if (ret < 0) {
-                error_setg_errno(errp, -ret, "Could not open '%s': %s",
+                error_setg_errno(errp, -ret, "Backing file '%s'"
+                                 "(actual path is '%s') error: %s",
                                  backing_file->value.s,
+                                 backing_filename_full,
                                  error_get_pretty(local_err));
                 error_free(local_err);
                 local_err = NULL;