Received: from eggs.gnu.org ([2001:4830:134:3::10]:35144) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XTWj7-0002yj-Ab for qemu-devel@nongnu.org; Mon, 15 Sep 2014 09:54:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XTWj2-0001Ed-76 for qemu-devel@nongnu.org; Mon, 15 Sep 2014 09:53:57 -0400 Received: from mx1.redhat.com ([209.132.183.28]:17541) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XTWj2-0001EO-00 for qemu-devel@nongnu.org; Mon, 15 Sep 2014 09:53:52 -0400 From: Markus Armbruster References: <1410620427-20089-1-git-send-email-armbru@redhat.com> <1410620427-20089-3-git-send-email-armbru@redhat.com> <20140915130216.GD22086@nodalink.com> Date: Mon, 15 Sep 2014 15:53:45 +0200 In-Reply-To: <20140915130216.GD22086@nodalink.com> (=?utf-8?Q?=22Beno?= =?utf-8?Q?=C3=AEt?= Canet"'s message of "Mon, 15 Sep 2014 13:02:16 +0000") Message-ID: <87mwa1c7me.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH v2 02/23] block: New BlockBackend List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: =?utf-8?Q?Beno=C3=AEt?= Canet Cc: kwolf@redhat.com, qemu-devel@nongnu.org, stefanha@redhat.com Beno=C3=AEt Canet writes: >> --- a/block.c >> +++ b/block.c >> @@ -2119,10 +2119,11 @@ static void bdrv_delete(BlockDriverState *bs) >>=20=20 >> bdrv_close(bs); >>=20=20 > > >> + drive_info_del(drive_get_by_blockdev(bs)); >> + >> /* remove from list, if necessary */ >> bdrv_make_anon(bs); >>=20=20 >> - drive_info_del(drive_get_by_blockdev(bs)); > > Do we really want this move ? Yes, we do. If bdrv_make_anon() runs before drive_info_del(), this conditional in drive_info_del() is always false: if (dinfo->bdrv->device_name[0]) { blk_unref(blk_by_name(dinfo->bdrv->device_name)); } I apologize for the hairiness. Things will become *way* simpler in PATCH 4. > Or is it an artefact of your work on the preparation series ? I added the line in "[PATCH 2/4] block: Keep DriveInfo alive until BlockDriverState dies" in the wrong place for this patch, so I have to move it here. When I noticed, the patch was already out. If I have to respin, I'll avoid the churn. >> g_free(bs); >> } >>=20=20 > >> + * Return the BlockBackend with name @name if it exists, else null. > >> + * @name must not be null. > The contract is that no one will call blk_by_name(NULL); > >> + */ >> +BlockBackend *blk_by_name(const char *name) >> +{ >> + BlockBackend *blk; > > Can we ? > > assert(name); Sure, why not. >> + >> + QTAILQ_FOREACH(blk, &blk_backends, link) { >> + if (!strcmp(name, blk->name)) { >> + return blk; >> + } >> + } >> + return NULL; >> +} [...] >> @@ -227,6 +228,15 @@ void drive_info_del(DriveInfo *dinfo) >> if (dinfo->opts) { >> qemu_opts_del(dinfo->opts); >> } >> + /* >> + * Hairy special case: if do_drive_del() has made dinfo->bdrv >> + * anonymous, it also unref'ed the associated BlockBackend. >> + */ > > Then you are filling the other case here. > > maybe > > /* > * Hairy special case: if do_drive_del() has made dinfo->bdrv > * anonymous, it also unref'ed the associated BlockBackend. > * Process the other case here. > */ > > would further explain you intents. Okay. >> + if (dinfo->bdrv->device_name[0]) { >> + blk_unref(blk_by_name(dinfo->bdrv->device_name)); >> + } >> + >> g_free(dinfo->id); >> QTAILQ_REMOVE(&drives, dinfo, next); >> g_free(dinfo->serial); >> @@ -307,6 +317,7 @@ static DriveInfo *blockdev_init(const char *file, QD= ict *bs_opts, >> int ro =3D 0; >> int bdrv_flags =3D 0; >> int on_read_error, on_write_error; >> + BlockBackend *blk; >> BlockDriverState *bs; >> DriveInfo *dinfo; >> ThrottleConfig cfg; >> @@ -463,9 +474,13 @@ static DriveInfo *blockdev_init(const char *file, Q= Dict *bs_opts, >> } > >> const char *fmt1 =3D NULL, *fmt2 =3D NULL, *cache, *filename1, *fil= ename2; >> + BlockBackend *blk1, *blk2; >> BlockDriverState *bs1, *bs2; >> int64_t total_sectors1, total_sectors2; >> uint8_t *buf1 =3D NULL, *buf2 =3D NULL; >> @@ -1011,18 +1022,20 @@ static int img_compare(int argc, char **argv) >> goto out3; >> } >>=20=20 >> + blk1 =3D blk_new("image 1", &error_abort); >> bs1 =3D bdrv_new_open("image 1", filename1, fmt1, flags, true, quie= t); >> if (!bs1) { >> error_report("Can't open file %s", filename1); >> ret =3D 2; >> - goto out3; >> + goto out2; > > Here we allocate blk1 and bs1 an if bs1 is null (error) we jump to > out2 which is: > >> out2: >> bdrv_unref(bs1); >> + blk_unref(blk1); > > It's a bit strange that we will bdrv_unref(NULL) in this case. > > This goto chain seems odd. You can solve the "on error, destroy the things already created" problem like this: Foo *foo; Bar *bar; Baz *baz; [...] foo =3D new_foo(); if (!foo) { goto out_foo; } bar =3D new_bar(); if (!bar) { goto out_bar; } baz =3D new_baz(); if (!baz) { goto out_baz; } [...] out_baz: del_baz(baz); out_bar: del_bar(bar); out_foo: del_foo(foo); Works, but keeping track of where exactly to go to is somewhat tedious and error prone. If the del_ functions do nothing for null arguments, the following also works: Foo *foo =3D NULL; Bar *bar =3D NULL; Baz *baz =3D NULL; [...] foo =3D new_foo(); if (!foo) { goto out; } bar =3D new_bar(); if (!bar) { goto out; } baz =3D new_baz(); if (!baz) { goto out; } [...] out: del_baz(baz); del_bar(bar); del_foo(foo); Simpler. This is the reason why free(NULL) does nothing. >> } >>=20=20 >> + blk2 =3D blk_new("image 2", &error_abort); >> bs2 =3D bdrv_new_open("image 2", filename2, fmt2, flags, true, quie= t); >> if (!bs2) { >> error_report("Can't open file %s", filename2); >> ret =3D 2; >> - goto out2; >> + goto out1; >> } >>=20=20 >> buf1 =3D qemu_blockalign(bs1, IO_BUF_SIZE); >> @@ -1183,11 +1196,14 @@ static int img_compare(int argc, char **argv) >> ret =3D 0; >>=20=20 >> out: >> - bdrv_unref(bs2); >> qemu_vfree(buf1); >> qemu_vfree(buf2); >> +out1: >> + bdrv_unref(bs2); >> + blk_unref(blk2); >> out2: >> bdrv_unref(bs1); >> + blk_unref(blk1); >> out3: >> qemu_progress_end(); >> return ret; >> @@ -1200,6 +1216,7 @@ static int img_convert(int argc, char **argv) >> int progress =3D 0, flags, src_flags; >> const char *fmt, *out_fmt, *cache, *src_cache, *out_baseimg, *out_f= ilename; >> BlockDriver *drv, *proto_drv; >> + BlockBackend **blk =3D NULL, *out_blk =3D NULL; > > Should blk be blks or blkes ? They are multiple. I follow the bs precedence (next line). In general, I feel using plural for array names doesn't work so well, because most uses tend to be subscripted, and there plural feels awkward. >> BlockDriverState **bs =3D NULL, *out_bs =3D NULL; >> int64_t total_sectors, nb_sectors, sector_num, bs_offset; >> int64_t *bs_sectors =3D NULL; [...]