From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59781) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZjtoS-00010t-I6 for qemu-devel@nongnu.org; Wed, 07 Oct 2015 14:51:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZjtoO-0001bR-6w for qemu-devel@nongnu.org; Wed, 07 Oct 2015 14:51:40 -0400 References: <1442907862-21376-1-git-send-email-wency@cn.fujitsu.com> <1442907862-21376-3-git-send-email-wency@cn.fujitsu.com> From: Max Reitz Message-ID: <561569A9.50609@redhat.com> Date: Wed, 7 Oct 2015 20:51:21 +0200 MIME-Version: 1.0 In-Reply-To: <1442907862-21376-3-git-send-email-wency@cn.fujitsu.com> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="QknHJKqKLI8wv1TXLhF3UXeWRim4G5Tqm" Subject: Re: [Qemu-devel] [Qemu-block] [PATCH v5 2/4] quorum: implement bdrv_add_child() and bdrv_del_child() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Wen Congyang , qemu devel , Eric Blake , Markus Armbruster , Alberto Garcia , Stefan Hajnoczi Cc: Kevin Wolf , zhanghailiang , qemu block , Jiang Yunhong , Dong Eddie , "Dr. David Alan Gilbert" , Gonglei , Yang Hongyang This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --QknHJKqKLI8wv1TXLhF3UXeWRim4G5Tqm Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: quoted-printable On 22.09.2015 09:44, Wen Congyang wrote: > Signed-off-by: Wen Congyang > Signed-off-by: zhanghailiang > Signed-off-by: Gonglei > --- > block.c | 6 ++--- > block/quorum.c | 72 +++++++++++++++++++++++++++++++++++++++++++= ++++++-- > include/block/block.h | 3 +++ > 3 files changed, 76 insertions(+), 5 deletions(-) >=20 > diff --git a/block.c b/block.c > index 1b25e43..01f6d69 100644 > --- a/block.c > +++ b/block.c > @@ -1079,9 +1079,9 @@ static int bdrv_fill_options(QDict **options, con= st char **pfilename, > return 0; > } > =20 > -static BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs, > - BlockDriverState *child_bs, > - const BdrvChildRole *child_role) > +BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs, > + BlockDriverState *child_bs, > + const BdrvChildRole *child_role) > { > BdrvChild *child =3D g_new(BdrvChild, 1); > *child =3D (BdrvChild) { > diff --git a/block/quorum.c b/block/quorum.c > index 8fe53b4..111a57b 100644 > --- a/block/quorum.c > +++ b/block/quorum.c > @@ -66,6 +66,9 @@ typedef struct QuorumVotes { > typedef struct BDRVQuorumState { > BlockDriverState **bs; /* children BlockDriverStates */ > int num_children; /* children count */ > + int max_children; /* The maximum children count, we need to r= eallocate > + * bs if num_children grows larger than max= imum. > + */ > int threshold; /* if less than threshold children reads ga= ve the > * same result a quorum error occurs. > */ > @@ -874,9 +877,9 @@ static int quorum_open(BlockDriverState *bs, QDict = *options, int flags, > ret =3D -EINVAL; > goto exit; > } > - if (s->num_children < 2) { > + if (s->num_children < 1) { > error_setg(&local_err, > - "Number of provided children must be greater than 1= "); > + "Number of provided children must be 1 or more"); > ret =3D -EINVAL; > goto exit; > } > @@ -925,6 +928,7 @@ static int quorum_open(BlockDriverState *bs, QDict = *options, int flags, > /* allocate the children BlockDriverState array */ > s->bs =3D g_new0(BlockDriverState *, s->num_children); > opened =3D g_new0(bool, s->num_children); > + s->max_children =3D s->num_children; > =20 > for (i =3D 0; i < s->num_children; i++) { > char indexstr[32]; > @@ -995,6 +999,67 @@ static void quorum_attach_aio_context(BlockDriverS= tate *bs, > } > } > =20 > +static void quorum_add_child(BlockDriverState *bs, BlockDriverState *c= hild_bs, > + Error **errp) > +{ > + BDRVQuorumState *s =3D bs->opaque; > + > + bdrv_drain(bs); > + > + if (s->num_children =3D=3D s->max_children) { > + if (s->max_children >=3D INT_MAX) { Opposing Berto (:-)), I like >=3D even if the > part is actually impossible. I myself like to use constructs such as: for (i =3D 0; i < n; i++) { /* ... */ } if (i >=3D n) { /* ... */ } Even though @i can never exceed @n after the loop. This is because my way of thinking is "What if it could exceed @n? Then I'd like to take this branch as well." The same applies here. s->max_children can never exceed INT_MAX, but if it could, we'd want that to be an error, too. I do find myself in discussions about that from time to time, especially since others prefer (for the example above): for (i =3D 0; i < n; i++) { /* ... */ } assert(i <=3D n); if (i =3D=3D n) { /* ... */ } Or the like. > + error_setg(errp, "Too many children"); > + return; > + } > + > + s->bs =3D g_renew(BlockDriverState *, s->bs, s->max_children += 1); > + s->bs[s->num_children] =3D NULL; > + s->max_children++; > + } Just a suggestion, please feel free to ignore it completely: You can drop the s->max_children field and just always call g_renew() with s->num_children + 1 as the @count parameter. There shouldn't be any (visible) performance penalty, but it would simplify the code. > + > + bdrv_ref(child_bs); > + bdrv_attach_child(bs, child_bs, &child_format); > + s->bs[s->num_children++] =3D child_bs; > +} > + > +static void quorum_del_child(BlockDriverState *bs, BlockDriverState *c= hild_bs, > + Error **errp) > +{ > + BDRVQuorumState *s =3D bs->opaque; > + BdrvChild *child; > + int i; > + > + for (i =3D 0; i < s->num_children; i++) { > + if (s->bs[i] =3D=3D child_bs) { > + break; > + } > + } > + > + QLIST_FOREACH(child, &bs->children, next) { > + if (child->bs =3D=3D child_bs) { > + break; > + } > + } > + > + /* we have checked it in bdrv_del_child() */ > + assert(i < s->num_children && child); > + > + if (s->num_children <=3D s->threshold) { > + error_setg(errp, > + "The number of children cannot be lower than the vote thre= shold %d", > + s->threshold); > + return; > + } > + > + bdrv_drain(bs); > + /* We can safely remove this child now */ > + memmove(&s->bs[i], &s->bs[i + 1], > + (s->num_children - i - 1) * sizeof(void *)); I'd use a matching type here, i.e. sizeof(BlockDriverState *) before Kevin's bdrv_swap() series or sizeof(BdrvChild *) afterwards. > + s->num_children--; > + s->bs[s->num_children] =3D NULL; In case you do decide to drop s->max_children, you could replace this by a call to g_renew on s->bs (or s->children). In any case, as Berto said, this line is unnecessary. Max > + bdrv_unref_child(bs, child); > +} > + > static void quorum_refresh_filename(BlockDriverState *bs) > { > BDRVQuorumState *s =3D bs->opaque; > @@ -1049,6 +1114,9 @@ static BlockDriver bdrv_quorum =3D { > .bdrv_detach_aio_context =3D quorum_detach_aio_context,= > .bdrv_attach_aio_context =3D quorum_attach_aio_context,= > =20 > + .bdrv_add_child =3D quorum_add_child, > + .bdrv_del_child =3D quorum_del_child, > + > .is_filter =3D true, > .bdrv_recurse_is_first_non_filter =3D quorum_recurse_is_first_no= n_filter, > }; > diff --git a/include/block/block.h b/include/block/block.h > index 665c56f..bd97399 100644 > --- a/include/block/block.h > +++ b/include/block/block.h > @@ -514,6 +514,9 @@ void bdrv_disable_copy_on_read(BlockDriverState *bs= ); > void bdrv_ref(BlockDriverState *bs); > void bdrv_unref(BlockDriverState *bs); > void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child); > +BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs, > + BlockDriverState *child_bs, > + const BdrvChildRole *child_role); > =20 > bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **= errp); > void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason= ); >=20 --QknHJKqKLI8wv1TXLhF3UXeWRim4G5Tqm Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQEcBAEBCAAGBQJWFWmpAAoJEDuxQgLoOKytuVwH/0bmwc/pwFlMfJ8SgCCPewt/ nG565MRZH9ekXw9f0MpV4Jj5q1oivNaBEnth1+MPYsOXoQiupeZv1llWVrgv5wPu tHY7SD8BYnLznDbuoKNUw18a+Pbkf043W170VUW7DqxvbNPJ8HGboSghSqWyD1nH AqqyCDzfwkgnb1Z/PpmS6prMSPvgSimt8fT3Z23l8O86/enySzWWhkFen0z9luei ZDJZkrjPFe4szojHGc4KYjEKOZP87zoss4lUAZ+tMTLYm9H1cw5c4jILY3QoLa7r d366/XoQKLvZDJOW/+pvgVnRmlgD8jHrRY1DXmZaqoo7vECMwocXIiTa+pZ8NB0= =A562 -----END PGP SIGNATURE----- --QknHJKqKLI8wv1TXLhF3UXeWRim4G5Tqm--