Patchwork [4/7] block: close unused image files at the end of streaming

login
register
mail settings
Submitter Paolo Bonzini
Date April 5, 2012, 3:42 p.m.
Message ID <1333640581-25649-5-git-send-email-pbonzini@redhat.com>
Download mbox | patch
Permalink /patch/151017/
State New
Headers show

Comments

Paolo Bonzini - April 5, 2012, 3:42 p.m.
From: Marcelo Tosatti <mtosatti@redhat.com>

Close the now unused images that were part of the previous backing file
chain and adjust ->backing_hd properly.

Note that this only works with relative paths.
Given the images:

 /tmp/a/base.raw
 /tmp/a/snap1.qcow2
 /tmp/b/snap2.qcow2

chained as:

 base(bak:"") <- snap1(bak:"base.raw") <- snap2(bak:"../a/snap1.qcow2")

merging snap1 and snap2 we will obtain:

 base(bak:"") <- snap2(bak:"base.raw")

However this should be maintained by the user/admin: one can also
screw up relative paths using qemu-img manually.

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Federico Simoncelli <fsimonce@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 block/stream.c |   23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
Federico Simoncelli - April 5, 2012, 3:51 p.m.
----- Original Message -----
> From: "Paolo Bonzini" <pbonzini@redhat.com>
> To: qemu-devel@nongnu.org
> Cc: "Marcelo Tosatti" <mtosatti@redhat.com>, "Federico Simoncelli" <fsimonce@redhat.com>
> Sent: Thursday, April 5, 2012 5:42:58 PM
> Subject: [PATCH 4/7] block: close unused image files at the end of streaming
> 
> From: Marcelo Tosatti <mtosatti@redhat.com>
> 
> Close the now unused images that were part of the previous backing
> file
> chain and adjust ->backing_hd properly.
> 
> Note that this only works with relative paths.

s/relative/absolute/

> Given the images:
> 
>  /tmp/a/base.raw
>  /tmp/a/snap1.qcow2
>  /tmp/b/snap2.qcow2
> 
> chained as:
> 
>  base(bak:"") <- snap1(bak:"base.raw") <-
>  snap2(bak:"../a/snap1.qcow2")
> 
> merging snap1 and snap2 we will obtain:
> 
>  base(bak:"") <- snap2(bak:"base.raw")
> 
> However this should be maintained by the user/admin: one can also
> screw up relative paths using qemu-img manually.

The patch is fine but I disagree with this comment. The user/admin didn't make
any mistake and he shouldn't be in charge of additional maintenance (which is
also tricky since the VM is running).

Patch

diff --git a/block/stream.c b/block/stream.c
index 61ff7a2..54f2b39 100644
--- a/block/stream.c
+++ b/block/stream.c
@@ -76,6 +76,28 @@  static int coroutine_fn stream_populate(BlockDriverState *bs,
     return bdrv_co_copy_on_readv(bs, sector_num, nb_sectors, &qiov);
 }
 
+static void close_unused_images(BlockDriverState *top, BlockDriverState *base,
+                                const char *base_id)
+{
+    BlockDriverState *intermediate;
+    intermediate = top->backing_hd;
+
+    while (intermediate) {
+        BlockDriverState *unused;
+
+        /* reached base */
+        if (intermediate == base) {
+            break;
+        }
+
+        unused = intermediate;
+        intermediate = intermediate->backing_hd;
+        unused->backing_hd = NULL;
+        bdrv_delete(unused);
+    }
+    top->backing_hd = base;
+}
+
 /*
  * Given an image chain: [BASE] -> [INTER1] -> [INTER2] -> [TOP]
  *
@@ -223,6 +245,7 @@  retry:
             base_id = s->backing_file_id;
         }
         ret = bdrv_change_backing_file(bs, base_id, NULL);
+        close_unused_images(bs, base, base_id);
     }
 
     qemu_vfree(buf);