diff mbox

[2/2] block: Inactivate all children

Message ID 1461030177-29144-3-git-send-email-famz@redhat.com
State New
Headers show

Commit Message

Fam Zheng April 19, 2016, 1:42 a.m. UTC
Currently we only inactivate the top BDS. Actually bdrv_inactivate
should be the opposite of bdrv_invalidate_cache.

Recurse into the whole subtree instead.

Signed-off-by: Fam Zheng <famz@redhat.com>
---
 block.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Comments

Kevin Wolf May 4, 2016, 10:12 a.m. UTC | #1
Am 19.04.2016 um 03:42 hat Fam Zheng geschrieben:
> Currently we only inactivate the top BDS. Actually bdrv_inactivate
> should be the opposite of bdrv_invalidate_cache.
> 
> Recurse into the whole subtree instead.
> 
> Signed-off-by: Fam Zheng <famz@redhat.com>

Did you actually test this?

I would expect that bs->drv->bdrv_inactivate() fails now (as in
assertion failure) if it has anything to flush to the image because
bs->file has already be inactivated before. I think children need to be
inactived after their parents.

Nodes with multiple parents could actually become even more
interesting...

Kevin

>  block.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/block.c b/block.c
> index fa8b38f..9a84ed1 100644
> --- a/block.c
> +++ b/block.c
> @@ -3260,8 +3260,16 @@ void bdrv_invalidate_cache_all(Error **errp)
>  
>  static int bdrv_inactivate(BlockDriverState *bs)
>  {
> +    BdrvChild *child;
>      int ret;
>  
> +    QLIST_FOREACH(child, &bs->children, next) {
> +        ret = bdrv_inactivate(child->bs);
> +        if (ret < 0) {
> +            return ret;
> +        }
> +    }
> +
>      if (bs->drv->bdrv_inactivate) {
>          ret = bs->drv->bdrv_inactivate(bs);
>          if (ret < 0) {
> -- 
> 2.8.0
>
Fam Zheng May 5, 2016, 12:32 a.m. UTC | #2
On Wed, 05/04 12:12, Kevin Wolf wrote:
> Am 19.04.2016 um 03:42 hat Fam Zheng geschrieben:
> > Currently we only inactivate the top BDS. Actually bdrv_inactivate
> > should be the opposite of bdrv_invalidate_cache.
> > 
> > Recurse into the whole subtree instead.
> > 
> > Signed-off-by: Fam Zheng <famz@redhat.com>
> 
> Did you actually test this?
> 
> I would expect that bs->drv->bdrv_inactivate() fails now (as in
> assertion failure) if it has anything to flush to the image because
> bs->file has already be inactivated before. I think children need to be
> inactived after their parents.

OK, my test apparently failed to trigger that bdrv_pwritv() path. Good catch!

> 
> Nodes with multiple parents could actually become even more
> interesting...

I'll make it two passes recursion: one for calling drv->bdrv_inactivate and the
other for setting BDRV_O_INACTIVATE.

Fam
Kevin Wolf May 6, 2016, 7:49 a.m. UTC | #3
Am 05.05.2016 um 02:32 hat Fam Zheng geschrieben:
> On Wed, 05/04 12:12, Kevin Wolf wrote:
> > Am 19.04.2016 um 03:42 hat Fam Zheng geschrieben:
> > > Currently we only inactivate the top BDS. Actually bdrv_inactivate
> > > should be the opposite of bdrv_invalidate_cache.
> > > 
> > > Recurse into the whole subtree instead.
> > > 
> > > Signed-off-by: Fam Zheng <famz@redhat.com>
> > 
> > Did you actually test this?
> > 
> > I would expect that bs->drv->bdrv_inactivate() fails now (as in
> > assertion failure) if it has anything to flush to the image because
> > bs->file has already be inactivated before. I think children need to be
> > inactived after their parents.
> 
> OK, my test apparently failed to trigger that bdrv_pwritv() path. Good catch!
> 
> > 
> > Nodes with multiple parents could actually become even more
> > interesting...
> 
> I'll make it two passes recursion: one for calling drv->bdrv_inactivate and the
> other for setting BDRV_O_INACTIVATE.

Though that would assume that the .bdrv_inactivate() implementation of
drivers doesn't already bring the BDS into a state where further writes
aren't possible. I'm not sure if that's a good assumption to make, even
though it's currently true for qcow2.

For example, imagine we went forward with format-based image locking.
The first .bdrv_inactivate() would then already release the lock, we
can't continue writing after that.

Maybe we need something like an "active reference counter", and we
decrement that for all children and only call their .bdrv_inactivate()
when it arrives at 0.

Kevin
Fam Zheng May 10, 2016, 3:23 a.m. UTC | #4
On Fri, 05/06 09:49, Kevin Wolf wrote:
> Am 05.05.2016 um 02:32 hat Fam Zheng geschrieben:
> > On Wed, 05/04 12:12, Kevin Wolf wrote:
> > > Am 19.04.2016 um 03:42 hat Fam Zheng geschrieben:
> > > > Currently we only inactivate the top BDS. Actually bdrv_inactivate
> > > > should be the opposite of bdrv_invalidate_cache.
> > > > 
> > > > Recurse into the whole subtree instead.
> > > > 
> > > > Signed-off-by: Fam Zheng <famz@redhat.com>
> > > 
> > > Did you actually test this?
> > > 
> > > I would expect that bs->drv->bdrv_inactivate() fails now (as in
> > > assertion failure) if it has anything to flush to the image because
> > > bs->file has already be inactivated before. I think children need to be
> > > inactived after their parents.
> > 
> > OK, my test apparently failed to trigger that bdrv_pwritv() path. Good catch!
> > 
> > > 
> > > Nodes with multiple parents could actually become even more
> > > interesting...
> > 
> > I'll make it two passes recursion: one for calling drv->bdrv_inactivate and the
> > other for setting BDRV_O_INACTIVATE.
> 
> Though that would assume that the .bdrv_inactivate() implementation of
> drivers doesn't already bring the BDS into a state where further writes
> aren't possible. I'm not sure if that's a good assumption to make, even
> though it's currently true for qcow2.
> 
> For example, imagine we went forward with format-based image locking.
> The first .bdrv_inactivate() would then already release the lock, we
> can't continue writing after that.

we only need to make sure all cache of all images is flushed when
bdrv_inactivate_all() returns, and similarly, that the cache of one image is
flushed when .bdrv_inactivate() returns.  The releasing of the lock is an
explicit callback and should be place in bdrv_inactivate() right above setting
of BDRV_O_INACTIVATE.  This is the case in my image locking series.

> 
> Maybe we need something like an "active reference counter", and we
> decrement that for all children and only call their .bdrv_inactivate()
> when it arrives at 0.

That should work, but the effect of the counters are local to one invocation of
bdrv_inactivate_all(), and is not really necessary if we do as above.

Fam
Kevin Wolf May 10, 2016, 8:33 a.m. UTC | #5
Am 10.05.2016 um 05:23 hat Fam Zheng geschrieben:
> On Fri, 05/06 09:49, Kevin Wolf wrote:
> > Am 05.05.2016 um 02:32 hat Fam Zheng geschrieben:
> > > On Wed, 05/04 12:12, Kevin Wolf wrote:
> > > > Am 19.04.2016 um 03:42 hat Fam Zheng geschrieben:
> > > > > Currently we only inactivate the top BDS. Actually bdrv_inactivate
> > > > > should be the opposite of bdrv_invalidate_cache.
> > > > > 
> > > > > Recurse into the whole subtree instead.
> > > > > 
> > > > > Signed-off-by: Fam Zheng <famz@redhat.com>
> > > > 
> > > > Did you actually test this?
> > > > 
> > > > I would expect that bs->drv->bdrv_inactivate() fails now (as in
> > > > assertion failure) if it has anything to flush to the image because
> > > > bs->file has already be inactivated before. I think children need to be
> > > > inactived after their parents.
> > > 
> > > OK, my test apparently failed to trigger that bdrv_pwritv() path. Good catch!
> > > 
> > > > 
> > > > Nodes with multiple parents could actually become even more
> > > > interesting...
> > > 
> > > I'll make it two passes recursion: one for calling drv->bdrv_inactivate and the
> > > other for setting BDRV_O_INACTIVATE.
> > 
> > Though that would assume that the .bdrv_inactivate() implementation of
> > drivers doesn't already bring the BDS into a state where further writes
> > aren't possible. I'm not sure if that's a good assumption to make, even
> > though it's currently true for qcow2.
> > 
> > For example, imagine we went forward with format-based image locking.
> > The first .bdrv_inactivate() would then already release the lock, we
> > can't continue writing after that.
> 
> we only need to make sure all cache of all images is flushed when
> bdrv_inactivate_all() returns, and similarly, that the cache of one image is
> flushed when .bdrv_inactivate() returns.  The releasing of the lock is an
> explicit callback and should be place in bdrv_inactivate() right above setting
> of BDRV_O_INACTIVATE.  This is the case in my image locking series.

Fair enough. My series didn't have a separate callback, but with yours
that should be working.

So is the semantics of .bdrv_inactivate() basically "bdrv_flush, and I
really mean it"?

> > Maybe we need something like an "active reference counter", and we
> > decrement that for all children and only call their .bdrv_inactivate()
> > when it arrives at 0.
> 
> That should work, but the effect of the counters are local to one invocation of
> bdrv_inactivate_all(), and is not really necessary if we do as above.

Agreed.

Kevin
Fam Zheng May 11, 2016, 1:51 a.m. UTC | #6
On Tue, 05/10 10:33, Kevin Wolf wrote:
> 
> Fair enough. My series didn't have a separate callback, but with yours
> that should be working.
> 
> So is the semantics of .bdrv_inactivate() basically "bdrv_flush, and I
> really mean it"?

Yes.

> 
> > > Maybe we need something like an "active reference counter", and we
> > > decrement that for all children and only call their .bdrv_inactivate()
> > > when it arrives at 0.
> > 
> > That should work, but the effect of the counters are local to one invocation of
> > bdrv_inactivate_all(), and is not really necessary if we do as above.
> 
> Agreed.

Working on another version now.

Fam
diff mbox

Patch

diff --git a/block.c b/block.c
index fa8b38f..9a84ed1 100644
--- a/block.c
+++ b/block.c
@@ -3260,8 +3260,16 @@  void bdrv_invalidate_cache_all(Error **errp)
 
 static int bdrv_inactivate(BlockDriverState *bs)
 {
+    BdrvChild *child;
     int ret;
 
+    QLIST_FOREACH(child, &bs->children, next) {
+        ret = bdrv_inactivate(child->bs);
+        if (ret < 0) {
+            return ret;
+        }
+    }
+
     if (bs->drv->bdrv_inactivate) {
         ret = bs->drv->bdrv_inactivate(bs);
         if (ret < 0) {