diff mbox

[2/5] block: implement bdrv_find_backing_image in qcow2

Message ID 20111230100503.375631440@redhat.com
State New
Headers show

Commit Message

Marcelo Tosatti Dec. 30, 2011, 10:03 a.m. UTC
Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>

Comments

Stefan Hajnoczi Jan. 3, 2012, 1:44 p.m. UTC | #1
On Fri, Dec 30, 2011 at 10:03 AM, Marcelo Tosatti <mtosatti@redhat.com> wrote:
> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
>
> Index: stefanha/block/qcow2.c
> ===================================================================
> --- stefanha.orig/block/qcow2.c
> +++ stefanha/block/qcow2.c
> @@ -767,6 +767,20 @@ static int qcow2_change_backing_file(Blo
>     return qcow2_update_ext_header(bs, backing_file, backing_fmt);
>  }
>
> +static BlockDriverState *qcow2_find_backing_image(BlockDriverState *bs,
> +                                                  const char *id)
> +{
> +
> +    do {
> +        if (!strncmp(bs->backing_file, id, sizeof(bs->backing_file)))
> +            return bs->backing_hd;

Coding style uses {} always.

> +
> +        bs = bs->backing_hd;
> +    } while (bs);
> +
> +    return NULL;
> +}

The backing file may not be qcow2, so we cannot loop over
bs->backing_hd.  We need to recurse instead.

That said, any image format which uses bs->backing_file will use
bs->backing_hd.  For example, QED could use the exact same
.bdrv_find_backing_file() implementation.

Perhaps instead we need a generic implementation which does something like:

BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, const char *id)
{
    if (!bs->drv) {
        return NULL;
    }

    if (bs->backing_hd) {
        if (strcmp(id, bs->backing_file) == 0) {
            return bs->backing_hd;
        } else {
            return bdrv_find_backing_file(bs->backing_hd, id);
        }
    }

    if (bs->drv->bdrv_find_backing_file) {
        return bs->drv->bdrv_find_backing_file(bs, id);
    }
    return NULL;
}

Stefan
diff mbox

Patch

Index: stefanha/block/qcow2.c
===================================================================
--- stefanha.orig/block/qcow2.c
+++ stefanha/block/qcow2.c
@@ -767,6 +767,20 @@  static int qcow2_change_backing_file(Blo
     return qcow2_update_ext_header(bs, backing_file, backing_fmt);
 }
 
+static BlockDriverState *qcow2_find_backing_image(BlockDriverState *bs,
+                                                  const char *id)
+{
+
+    do {
+        if (!strncmp(bs->backing_file, id, sizeof(bs->backing_file)))
+            return bs->backing_hd;
+
+        bs = bs->backing_hd;
+    } while (bs);
+
+    return NULL;
+}
+
 static int preallocate(BlockDriverState *bs)
 {
     uint64_t nb_sectors;
@@ -1304,6 +1318,7 @@  static BlockDriver bdrv_qcow2 = {
     .bdrv_load_vmstate    = qcow2_load_vmstate,
 
     .bdrv_change_backing_file   = qcow2_change_backing_file,
+    .bdrv_find_backing_image    = qcow2_find_backing_image,
 
     .bdrv_invalidate_cache      = qcow2_invalidate_cache,