Received: from eggs.gnu.org ([2001:4830:134:3::10]:40294) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoZEd-0000WD-MB for qemu-devel@nongnu.org; Fri, 08 Apr 2016 12:26:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aoZEc-0005ed-LW for qemu-devel@nongnu.org; Fri, 08 Apr 2016 12:26:15 -0400 Date: Fri, 8 Apr 2016 18:26:07 +0200 From: Kevin Wolf Message-ID: <20160408162607.GI4700@noname.redhat.com> References: <1459965434-21531-1-git-send-email-mreitz@redhat.com> <1459965434-21531-3-git-send-email-mreitz@redhat.com> <20160407112947.GB4509@noname.redhat.com> <5707D301.90009@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="oLBj+sq0vYjzfsbl" Content-Disposition: inline In-Reply-To: <5707D301.90009@redhat.com> Subject: Re: [Qemu-devel] [PATCH v2 for-2.7 2/8] block: Let bdrv_open_inherit() return the snapshot List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Max Reitz Cc: qemu-block@nongnu.org, qemu-devel@nongnu.org, Alberto Garcia --oLBj+sq0vYjzfsbl Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Am 08.04.2016 um 17:49 hat Max Reitz geschrieben: > On 07.04.2016 13:29, Kevin Wolf wrote: > > Am 06.04.2016 um 19:57 hat Max Reitz geschrieben: > >> If bdrv_open_inherit() creates a snapshot BDS and *pbs is NULL, that > >> snapshot BDS should be returned instead of the BDS under it. > >> > >> To this end, bdrv_append_temp_snapshot() now returns the snapshot BDS > >> instead of just appending it on top of the snapshotted BDS. Also, it > >> calls bdrv_ref() before bdrv_append() (which bdrv_open_inherit() has to > >> undo if not returning the overlay). > >> > >> Signed-off-by: Max Reitz > >=20 > > This is a tricky patch, but after all it looks correct to me. I think we > > could improve a bit on the documentation, though: > >=20 > > 1. The commit message suggests that by returning the wrong BDS we may > > have an observable bug. It would be good to add details on why this > > used to be harmless (IIUC, all users of BDRV_O_SNAPSHOT go through > > blk_new_open(), and there first setting *pbs (which is blk->root->bs) > > and then doing bdrv_append() does the right thing) >=20 > To be honest, I'd rather not wrap my head around why it worked, but I'll > try to. I hope you just need to verify my theory stated above. ;-) > > 2. The refcounting stuff isn't obvious either: > >=20 > >> @@ -1481,12 +1482,16 @@ static int bdrv_append_temp_snapshot(BlockDriv= erState *bs, int flags, > >> goto out; > >> } > >> =20 > >> + bdrv_ref(bs_snapshot); > >> bdrv_append(bs_snapshot, bs); > >=20 > > This is because bdrv_append() drops the reference, but we want to return > > a strong reference. >=20 > Well, it's mostly because now we do return a (strong) reference. Before, > bs_snapshot simply was not returned at all. >=20 > I'll add a comment, though. Yes, that's the additional reference you need. I just kept all existing references in mind: We already get a strong reference from bdrv_new(). bdrv_append() takes ownership of a strong reference, though, and this is why we need a second one to return. > >> /* For snapshot=3Don, create a temporary qcow2 overlay. bs points= to the > >> * temporary snapshot afterwards. */ > >> if (snapshot_flags) { > >> - ret =3D bdrv_append_temp_snapshot(bs, snapshot_flags, snapsho= t_options, > >> - &local_err); > >> + BlockDriverState *snapshot_bs; > >> + snapshot_bs =3D bdrv_append_temp_snapshot(bs, snapshot_flags, > >> + snapshot_options, &lo= cal_err); > >> snapshot_options =3D NULL; > >> if (local_err) { > >> + ret =3D -EINVAL; > >> goto close_and_fail; > >> } > >> + if (!*pbs) { > >> + /* The reference is now held by the overlay BDS */ > >> + bdrv_unref(bs); > >=20 > > We still hold a strong reference to the newly created bs that we wanted > > to return, but now we're returning a different BDS, so we must drop the > > reference. (The overlay BDS doesn't hold "the" same reference as the > > comment suggests, but an additional one.) > >=20 > >> + bs =3D snapshot_bs; > >> + } else { > >> + /* It is still referenced in the same way that *pbs was r= eferenced, > >> + * however that may be */ > >> + bdrv_unref(snapshot_bs); > >=20 > > In this case we don't in fact return the reference for bs_snapshot, so > > drop it. > >=20 > > So I think what I would like here is comments that explain where the > > ownership of the individual strong references goes, not who else may or > > may not hold additional references to a BDS. >=20 > Well, the ownership goes away. ;-) Still good to know that this function owns the reference before it drops it. ;-) > I've fattened the comments so they explain exactly why the refcount is > decremented, respectively, and why this will most likely not result in > the deletion of the BDS. Sounds good. Kevin --oLBj+sq0vYjzfsbl Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.22 (GNU/Linux) iQIcBAEBAgAGBQJXB9ufAAoJEH8JsnLIjy/WZZMQAK6b6z0AqYHm/5rcpIly8ylN PdFIOr0I5Nl9lzNhI1g8oKsWS1Z27ssceITRFgy/dFbPajR3ovF1pBQUcDGHAqPC KCO+IjEPbaPlYJKp5kcUJU663x0njvvlbsn0Zzmz0NRMZyiAyNRPad2mVXRqOI8W Wxg0QZC7LNloOXhniWCgaqCBYQTUSNGED9yKAcIetnXexh/x3FluFLZJ4OH30VKN 5pMwsh6l0MDHEZHNoXBvRHMGBryduKz9zVd2ru+vVXePTsrZeYaQxzWisfm0oThY clUqKg6QzmivafFfZmjABxwljxIqEYK//JtoNmiWD9VcM//wOygnsKfERhd+n1RT XHoGglKl+xqFiPDqrEAwKGyDpL26HmO1LMiFyUvL2GQfZeQSQyLyTcNwVSHCm6Co 3Q4CaKVv6tvDw9C3v6+Qxcnf+AnDHlzuhEHt1pFwUOoRMZ/gb2YJLl0PrVTotLJ7 xpAKbpV1wFQIKc4QClRV0xQAIn66pBcyrR8Al92my/CCzwU3kLqQa0HTlaSo3PsD T4PMcMT7wx8DQ3aXQj4HPS0NoD9yEA2izvs6FErsNE1boZPFQCnfywwATdsMdOnk BwYYY9Us+bvVLmq+pTNUycPOnZRSZ3hALr49m1YtbF9LuXqD9G0GC5kCKJQlZKLU yfg+bAQ7uI7IPta5Hwzx =VZWW -----END PGP SIGNATURE----- --oLBj+sq0vYjzfsbl--