Skip to contentSkip to conversation

block: fix the use of protocols in backing files

PATCH In review Anthony Liguori 8 participants 13 messages Oct 27, 2010 – Nov 4, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: fix the use of protocols in backing files

block.c +10 -4
index 1a965b2..00b6f21 100644
@@ -603,10 +603,16 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
         BlockDriver *back_drv = NULL;
 
         bs->backing_hd = bdrv_new("");
-        path_combine(backing_filename, sizeof(backing_filename),
-                     filename, bs->backing_file);
-        if (bs->backing_format[0] != '\0')
-            back_drv = bdrv_find_format(bs->backing_format);
+        back_drv = bdrv_find_protocol(bs->backing_file);
+        if (!back_drv) {
+            path_combine(backing_filename, sizeof(backing_filename),
+                         filename, bs->backing_file);
+            if (bs->backing_format[0] != '\0')
+                back_drv = bdrv_find_format(bs->backing_format);
+        } else {
+            pstrcpy(backing_filename, sizeof(backing_filename),
+                    bs->backing_file);
+        }
 
         /* backing files always opened read-only */
         back_flags =
Signature · 1 line
1.7.0.4
#1 lore raw
Quoted 10 lines · Anthony Liguori
block.c +0 -0
index 1a965b2..00b6f21 100644
@@ -603,10 +603,16 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
         BlockDriver *back_drv = NULL;
 
         bs->backing_hd = bdrv_new("");

block.c int bdrv_open(BlockDriverState *bs, const char *filename, int flags, · line 611

Quoted 10 lines
block.c +6 -4
-        path_combine(backing_filename, sizeof(backing_filename),
-                     filename, bs->backing_file);
-        if (bs->backing_format[0] != '\0')
-            back_drv = bdrv_find_format(bs->backing_format);
+        back_drv = bdrv_find_protocol(bs->backing_file);
+        if (!back_drv) {
+            path_combine(backing_filename, sizeof(backing_filename),
+                         filename, bs->backing_file);
+            if (bs->backing_format[0] != '\0')
+                back_drv = bdrv_find_format(bs->backing_format);

Sigh..

Quoted 8 lines
block.c +4 -0
+        } else {
+            pstrcpy(backing_filename, sizeof(backing_filename),
+                    bs->backing_file);
+        }
 
         /* backing files always opened read-only */
         back_flags =
Signature · 1 line
mailto:[email protected]
#2 lore raw
Quoted 12 lines · malc
Quoted 9 lines · Anthony Liguori
block.c +0 -0
index 1a965b2..00b6f21 100644
@@ -603,10 +603,16 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
          BlockDriver *back_drv = NULL;
 
Quoted 14 lines

block.c int bdrv_open(BlockDriverState *bs, const char *filename, int flags, · line 616

Quoted 12 lines
block.c +6 -4
          bs->backing_hd = bdrv_new("");
-        path_combine(backing_filename, sizeof(backing_filename),
-                     filename, bs->backing_file);
-        if (bs->backing_format[0] != '\0')
-            back_drv = bdrv_find_format(bs->backing_format);
+        back_drv = bdrv_find_protocol(bs->backing_file);
+        if (!back_drv) {
+            path_combine(backing_filename, sizeof(backing_filename),
+                         filename, bs->backing_file);
+            if (bs->backing_format[0] != '\0')
+                back_drv = bdrv_find_format(bs->backing_format);
 

Sigh..

Always good to see such clear and constructive comments...

I'll respin.

Regards,

Anthony Liguori

Quoted 11 lines
Quoted 10 lines
block.c +4 -0
+        } else {
+            pstrcpy(backing_filename, sizeof(backing_filename),
+                    bs->backing_file);
+        }
 
          /* backing files always opened read-only */

back_flags =

#3 lore raw
In reply to Anthony Liguori
Quoted 26 lines · Anthony Liguori
block.c +10 -4
index 1a965b2..00b6f21 100644
@@ -603,10 +603,16 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
         BlockDriver *back_drv = NULL;
 
         bs->backing_hd = bdrv_new("");
-        path_combine(backing_filename, sizeof(backing_filename),
-                     filename, bs->backing_file);
-        if (bs->backing_format[0] != '\0')
-            back_drv = bdrv_find_format(bs->backing_format);
+        back_drv = bdrv_find_protocol(bs->backing_file);
+        if (!back_drv) {
+            path_combine(backing_filename, sizeof(backing_filename),
+                         filename, bs->backing_file);
+            if (bs->backing_format[0] != '\0')
+                back_drv = bdrv_find_format(bs->backing_format);
+        } else {
+            pstrcpy(backing_filename, sizeof(backing_filename),
+                    bs->backing_file);
+        }
 
         /* backing files always opened read-only */
Quoted 3 lines

        back_flags =
--
1.7.0.4

I think this makes sense.

Now it is possible to specify backing files that are relative to QEMU's current working directory using file:filename. I don't see harm in this.

Stefan

#4 lore raw

Am 28.10.2010 10:30, schrieb Stefan Hajnoczi:

Quoted 33 lines · Anthony Liguori
Quoted 28 lines
block.c +10 -5
index 1a965b2..00b6f21 100644
@@ -603,10 +603,16 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
        BlockDriver *back_drv = NULL;
 
        bs->backing_hd = bdrv_new("");
-        path_combine(backing_filename, sizeof(backing_filename),
-                     filename, bs->backing_file);
-        if (bs->backing_format[0] != '\0')
-            back_drv = bdrv_find_format(bs->backing_format);
+        back_drv = bdrv_find_protocol(bs->backing_file);
+        if (!back_drv) {
+            path_combine(backing_filename, sizeof(backing_filename),
+                         filename, bs->backing_file);
+            if (bs->backing_format[0] != '\0')
+                back_drv = bdrv_find_format(bs->backing_format);
+        } else {
+            pstrcpy(backing_filename, sizeof(backing_filename),
+                    bs->backing_file);
+        }
 
        /* backing files always opened read-only */
        back_flags =
--
Quoted 1 line

1.7.0.4

I think this makes sense.

Quoted 3 lines

Now it is possible to specify backing files that are relative to QEMU's current working directory using file:filename. I don't see harm in this.

It would be more consistent if it was relative to the image, but there's no meaningful way to do that for arbitrary protocols. It's definitely not worse than before the change.

Kevin

#5 lore raw
In reply to Stefan Hajnoczi
Quoted 33 lines · Stefan Hajnoczi
Quoted 26 lines · Anthony Liguori
block.c +10 -4
index 1a965b2..00b6f21 100644
@@ -603,10 +603,16 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
         BlockDriver *back_drv = NULL;
 
         bs->backing_hd = bdrv_new("");
-        path_combine(backing_filename, sizeof(backing_filename),
-                     filename, bs->backing_file);
-        if (bs->backing_format[0] != '\0')
-            back_drv = bdrv_find_format(bs->backing_format);
+        back_drv = bdrv_find_protocol(bs->backing_file);
+        if (!back_drv) {
+            path_combine(backing_filename, sizeof(backing_filename),
+                         filename, bs->backing_file);
+            if (bs->backing_format[0] != '\0')
+                back_drv = bdrv_find_format(bs->backing_format);
+        } else {
+            pstrcpy(backing_filename, sizeof(backing_filename),
+                    bs->backing_file);
+        }
 
         /* backing files always opened read-only */
Quoted 3 lines

        back_flags =
--
1.7.0.4

I think this makes sense.

Quoted 3 lines

Now it is possible to specify backing files that are relative to QEMU's current working directory using file:filename. I don't see harm in this.

Shouldn't a backing file be treated as relative to the image file pointing to it, rather than the QEMU working directory. eg so you can do

  # qemu-img create backing.img
  # qemu-img create -o backing_file=file:backing.img main.img

And have main.img be able to resolve backing.img in its same directory, no matter what directory QEMU itself is executing from

Regards,
Daniel

Signature · 4 lines
|: Red Hat, Engineering, London    -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org -o- http://virt-manager.org -o- http://deltacloud.org :|
|: http://autobuild.org        -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
#6 lore raw
Quoted 38 lines · Daniel P. Berrange
Quoted 33 lines · Stefan Hajnoczi
Quoted 26 lines · Anthony Liguori
block.c +10 -4
index 1a965b2..00b6f21 100644
@@ -603,10 +603,16 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
         BlockDriver *back_drv = NULL;
 
         bs->backing_hd = bdrv_new("");
-        path_combine(backing_filename, sizeof(backing_filename),
-                     filename, bs->backing_file);
-        if (bs->backing_format[0] != '\0')
-            back_drv = bdrv_find_format(bs->backing_format);
+        back_drv = bdrv_find_protocol(bs->backing_file);
+        if (!back_drv) {
+            path_combine(backing_filename, sizeof(backing_filename),
+                         filename, bs->backing_file);
+            if (bs->backing_format[0] != '\0')
+                back_drv = bdrv_find_format(bs->backing_format);
+        } else {
+            pstrcpy(backing_filename, sizeof(backing_filename),
+                    bs->backing_file);
+        }
 
         /* backing files always opened read-only */
Quoted 3 lines

        back_flags =
--
1.7.0.4

I think this makes sense.

Quoted 3 lines

Now it is possible to specify backing files that are relative to QEMU's current working directory using file:filename. I don't see harm in this.

Quoted 8 lines

Shouldn't a backing file be treated as relative to the image file pointing to it, rather than the QEMU working directory. eg so you can do

  # qemu-img create backing.img
  # qemu-img create -o backing_file=file:backing.img main.img

And have main.img be able to resolve backing.img in its same directory, no matter what directory QEMU itself is executing from

This works relative to main.img:

# qemu-img create -o backing_file=backing.img main.img

but this works relative to QEMU's cwd:

# qemu-img create -o backing_file=file:backing.img main.img

As Kevin mentioned, special-casing the file: protocol isn't ideal. We shouldn't make assumptions about specific protocols.

Stefan

#7 lore raw
In reply to Daniel P. Berrange

Am 28.10.2010 11:35, schrieb Daniel P. Berrange:

Quoted 38 lines · Stefan Hajnoczi
Quoted 33 lines · Anthony Liguori
Quoted 28 lines
block.c +10 -5
index 1a965b2..00b6f21 100644
@@ -603,10 +603,16 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
        BlockDriver *back_drv = NULL;
 
        bs->backing_hd = bdrv_new("");
-        path_combine(backing_filename, sizeof(backing_filename),
-                     filename, bs->backing_file);
-        if (bs->backing_format[0] != '\0')
-            back_drv = bdrv_find_format(bs->backing_format);
+        back_drv = bdrv_find_protocol(bs->backing_file);
+        if (!back_drv) {
+            path_combine(backing_filename, sizeof(backing_filename),
+                         filename, bs->backing_file);
+            if (bs->backing_format[0] != '\0')
+                back_drv = bdrv_find_format(bs->backing_format);
+        } else {
+            pstrcpy(backing_filename, sizeof(backing_filename),
+                    bs->backing_file);
+        }
 
        /* backing files always opened read-only */
        back_flags =
--
Quoted 1 line

1.7.0.4

I think this makes sense.

Quoted 3 lines

Now it is possible to specify backing files that are relative to QEMU's current working directory using file:filename. I don't see harm in this.

Quoted 8 lines

Shouldn't a backing file be treated as relative to the image file pointing to it, rather than the QEMU working directory. eg so you can do

  # qemu-img create backing.img
  # qemu-img create -o backing_file=file:backing.img main.img

And have main.img be able to resolve backing.img in its same directory, no matter what directory QEMU itself is executing from

The problem is that this wouldn't work in the general case. It's rather an exception that it makes sense for file: backing files with file:
images. Consider this:

  # qemu-img create -o backing_file=nbd:foo:1234 /tmp/main.img

Without this patch, you'll end up with /tmp/nbd:foo:1234, which is probably not what you wanted. With a patch that would work for file: you would get a hardly better path nbd:/tmp/foo:1234

Kevin

#8 lore raw
Quoted 52 lines · Kevin Wolf

Am 28.10.2010 11:35, schrieb Daniel P. Berrange:

Quoted 38 lines · Stefan Hajnoczi
Quoted 33 lines · Anthony Liguori
Quoted 28 lines
block.c +10 -5
index 1a965b2..00b6f21 100644
@@ -603,10 +603,16 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
        BlockDriver *back_drv = NULL;
 
        bs->backing_hd = bdrv_new("");
-        path_combine(backing_filename, sizeof(backing_filename),
-                     filename, bs->backing_file);
-        if (bs->backing_format[0] != '\0')
-            back_drv = bdrv_find_format(bs->backing_format);
+        back_drv = bdrv_find_protocol(bs->backing_file);
+        if (!back_drv) {
+            path_combine(backing_filename, sizeof(backing_filename),
+                         filename, bs->backing_file);
+            if (bs->backing_format[0] != '\0')
+                back_drv = bdrv_find_format(bs->backing_format);
+        } else {
+            pstrcpy(backing_filename, sizeof(backing_filename),
+                    bs->backing_file);
+        }
 
        /* backing files always opened read-only */
        back_flags =
--
Quoted 1 line

1.7.0.4

I think this makes sense.

Quoted 3 lines

Now it is possible to specify backing files that are relative to QEMU's current working directory using file:filename. I don't see harm in this.

Quoted 8 lines

Shouldn't a backing file be treated as relative to the image file pointing to it, rather than the QEMU working directory. eg so you can do

  # qemu-img create backing.img
  # qemu-img create -o backing_file=file:backing.img main.img

And have main.img be able to resolve backing.img in its same directory, no matter what directory QEMU itself is executing from

The problem is that this wouldn't work in the general case. It's rather an exception that it makes sense for file: backing files with file:
images. Consider this:

Quoted 5 lines
  # qemu-img create -o backing_file=nbd:foo:1234 /tmp/main.img

Without this patch, you'll end up with /tmp/nbd:foo:1234, which is probably not what you wanted. With a patch that would work for file: you would get a hardly better path nbd:/tmp/foo:1234

Since we know the protocol, there could be a per-protocol function used for resolving the backing store URI relative to the master URI. That would avoid needing to special case file: in the shared generic code.

Daniel

Signature · 4 lines
|: Red Hat, Engineering, London    -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org -o- http://virt-manager.org -o- http://deltacloud.org :|
|: http://autobuild.org        -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
#9 lore raw
Quoted 6 lines · Daniel P. Berrange
Quoted 6 lines

The problem is that this wouldn't work in the general case. It's rather an exception that it makes sense for file: backing files with file:
images. Consider this:

   # qemu-img create -o backing_file=nbd:foo:1234 /tmp/main.img
Quoted 8 lines
Quoted 4 lines

Without this patch, you'll end up with /tmp/nbd:foo:1234, which is probably not what you wanted. With a patch that would work for file: you would get a hardly better path nbd:/tmp/foo:1234

Since we know the protocol, there could be a per-protocol function used for resolving the backing store URI relative to the master URI. That would avoid needing to special case file: in the shared generic code.

Relative resolution of a backing files makes me very nervous. Any time a disk image can reasonably resolve to something other than what the user expected is potentially a very nasty security issue.

The less obvious the resolution, the worse the problem becomes. I think resolution based on current path is probably the most obvious implementation.

Regards,

Anthony Liguori

Quoted 2 lines

Daniel

#10 lore raw
In reply to Anthony Liguori

Am 27.10.2010 20:19, schrieb Anthony Liguori:

Quoted 7 lines
block.c +0 -0
index 1a965b2..00b6f21 100644

block.c int bdrv_open(BlockDriverState *bs, const char *filename, int flags, · line 607

Quoted 9 lines
block.c +2 -4
@@ -603,10 +603,16 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
         BlockDriver *back_drv = NULL;
 
         bs->backing_hd = bdrv_new("");
-        path_combine(backing_filename, sizeof(backing_filename),
-                     filename, bs->backing_file);
-        if (bs->backing_format[0] != '\0')
-            back_drv = bdrv_find_format(bs->backing_format);
+        back_drv = bdrv_find_protocol(bs->backing_file);
+        if (!back_drv) {

If no protocol is specified, bdrv_find_protocol doesn't return NULL but the file: driver.

This breaks all backing file related qemu-iotests cases because qcow2 backing files are opened as raw files now.

Kevin

#11 lore raw
Quoted 11 lines · Kevin Wolf

Am 27.10.2010 20:19, schrieb Anthony Liguori:

Quoted 9 lines
block.c +0 -0
index 1a965b2..00b6f21 100644
@@ -603,10 +603,16 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
          BlockDriver *back_drv = NULL;
 
Quoted 11 lines

block.c int bdrv_open(BlockDriverState *bs, const char *filename, int flags, · line 616

Quoted 8 lines
block.c +2 -4
          bs->backing_hd = bdrv_new("");
-        path_combine(backing_filename, sizeof(backing_filename),
-                     filename, bs->backing_file);
-        if (bs->backing_format[0] != '\0')
-            back_drv = bdrv_find_format(bs->backing_format);
+        back_drv = bdrv_find_protocol(bs->backing_file);
+        if (!back_drv) {
 

If no protocol is specified, bdrv_find_protocol doesn't return NULL but the file: driver.

An ugly way to handle this would be to do if (strstr(bs->backing_file, ":") == NULL) instead.

A deeper refactoring could return NULL in bdrv_find_protocol and fixup the callers to default to file: if none are specified.

What do you think?

Regards,

Anthony Liguori

Quoted 5 lines

This breaks all backing file related qemu-iotests cases because qcow2 backing files are opened as raw files now.

Kevin

#12 lore raw

Am 04.11.2010 14:14, schrieb Anthony Liguori:

Quoted 24 lines · Kevin Wolf
Quoted 11 lines

Am 27.10.2010 20:19, schrieb Anthony Liguori:

Quoted 9 lines
block.c +0 -0
index 1a965b2..00b6f21 100644
@@ -603,10 +603,16 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
          BlockDriver *back_drv = NULL;
 
Quoted 11 lines

block.c int bdrv_open(BlockDriverState *bs, const char *filename, int flags, · line 616

Quoted 8 lines
block.c +2 -4
          bs->backing_hd = bdrv_new("");
-        path_combine(backing_filename, sizeof(backing_filename),
-                     filename, bs->backing_file);
-        if (bs->backing_format[0] != '\0')
-            back_drv = bdrv_find_format(bs->backing_format);
+        back_drv = bdrv_find_protocol(bs->backing_file);
+        if (!back_drv) {
 

If no protocol is specified, bdrv_find_protocol doesn't return NULL but the file: driver.

Quoted 2 lines

An ugly way to handle this would be to do if (strstr(bs->backing_file, ":") == NULL) instead.

Ugly indeed.

Quoted 2 lines

A deeper refactoring could return NULL in bdrv_find_protocol and fixup the callers to default to file: if none are specified.

NULL is already used for errors, so we'd have to have something like

int bdrv_find_protocol(const char *filename, BlockDriver **drv)

in order to be able to distinguish "invalid protocol" from "no explicit protocol". You could rename this function and retain a bdrv_find_protocol with the old prototype as a wrapper that returns file instead of NULL (there are several callers that expect this behaviour, so probably it makes sense to have it in a central place).

Does that sound reasonable?

Kevin