Received: from eggs.gnu.org ([2001:4830:134:3::10]:44538) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WAiZ1-0000Z4-6r for qemu-devel@nongnu.org; Tue, 04 Feb 2014 11:11:48 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WAiXh-0005YH-Mf for qemu-devel@nongnu.org; Tue, 04 Feb 2014 11:09:31 -0500 Received: from mx1.redhat.com ([209.132.183.28]:52375) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WAiXh-0005Y9-D2 for qemu-devel@nongnu.org; Tue, 04 Feb 2014 11:08:09 -0500 Date: Tue, 4 Feb 2014 17:08:07 +0100 From: Kevin Wolf Message-ID: <20140204160807.GO3384@dhcp-200-207.str.redhat.com> References: <1391464280-25627-1-git-send-email-benoit.canet@irqsave.net> <1391464280-25627-13-git-send-email-benoit.canet@irqsave.net> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: <1391464280-25627-13-git-send-email-benoit.canet@irqsave.net> Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH V15 12/13] quorum: Add quorum_open() and quorum_close(). List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: =?iso-8859-1?Q?Beno=EEt?= Canet Cc: =?iso-8859-1?Q?Beno=EEt?= Canet , qemu-devel@nongnu.org, stefanha@redhat.com, mreitz@redhat.com Am 03.02.2014 um 22:51 hat Beno=EEt Canet geschrieben: > From: Beno=EEt Canet >=20 > Example of command line: > -drive if=3Dvirtio,file.driver=3Dquorum,\ > file.children.0.file.filename=3D1.raw,\ > file.children.0.node-name=3D1.raw,\ > file.children.0.driver=3Draw,\ > file.children.1.file.filename=3D2.raw,\ > file.children.1.node-name=3D2.raw,\ > file.children.1.driver=3Draw,\ > file.children.2.file.filename=3D3.raw,\ > file.children.2.node-name=3D3.raw,\ > file.children.2.driver=3Draw,\ > file.vote_threshold=3D2 >=20 > file.blkverify=3Don with file.vote_threshold=3D2 and two files can be p= assed to > emulated blkverify. >=20 > Signed-off-by: Benoit Canet > --- > block/quorum.c | 171 +++++++++++++++++++++++++++++++++++++++++++++++= ++++++++ > qapi-schema.json | 21 ++++++- > 2 files changed, 191 insertions(+), 1 deletion(-) >=20 > diff --git a/block/quorum.c b/block/quorum.c > index 1e683f8..d2bea29 100644 > --- a/block/quorum.c > +++ b/block/quorum.c > @@ -17,8 +17,12 @@ > #include > #include "block/block_int.h" > #include "qapi/qmp/qjson.h" > +#include "qapi/qmp/types.h" > +#include "qemu-common.h" > =20 > #define HASH_LENGTH 32 > +#define KEY_PREFIX "children." > +#define KEY_FILENAME_SUFFIX ".file.filename" > =20 > /* This union holds a vote hash value */ > typedef union QuorumVoteValue { > @@ -712,12 +716,179 @@ static bool quorum_recurse_is_first_non_filter(B= lockDriverState *bs, > return false; > } > =20 > +static int quorum_valid_threshold(int threshold, > + int total, > + Error **errp) > +{ > + > + if (threshold < 1) { > + error_set(errp, QERR_INVALID_PARAMETER_VALUE, > + "vote-threshold", "value >=3D 1"); > + return -ERANGE; > + } > + > + if (threshold > total) { > + error_setg(errp, "threshold may not exceed children count"); > + return -ERANGE; > + } > + > + return 0; > +} > + > +static int quorum_open(BlockDriverState *bs, > + QDict *options, > + int flags, > + Error **errp) > +{ > + BDRVQuorumState *s =3D bs->opaque; > + Error *local_err =3D NULL; > + bool *opened; > + QDict *sub =3D NULL; > + QList *list =3D NULL; > + const QListEntry *lentry; > + const QDictEntry *dentry; > + const char *value; > + char *next; > + int i; > + int ret =3D 0; > + unsigned long long threshold =3D 0; > + > + qdict_flatten(options); > + qdict_extract_subqdict(options, &sub, "children."); > + qdict_array_split(sub, &list); > + > + /* count how many different children are present and validate */ > + s->total =3D !qlist_size(list) ? qdict_size(sub) : qlist_size(list= ); Which case does qdict_size(sub) address? > + if (s->total < 2) { > + error_setg(&local_err, > + "Number of provided children must be greater than 1= "); > + ret =3D -EINVAL; > + goto exit; > + } > + > + ret =3D qdict_get_try_int(options, "vote-threshold", -1); > + /* from QMP */ > + if (ret !=3D -1) { > + qdict_del(options, "vote-threshold"); > + s->threshold =3D ret; > + /* from command line */ > + } else { > + /* retrieve the threshold option from the command line */ > + value =3D qdict_get_try_str(options, "vote_threshold"); > + if (!value) { > + error_setg(&local_err, > + "vote_threshold must be provided"); > + ret =3D -EINVAL; > + goto exit; > + } > + qdict_del(options, "vote_threshold"); > + > + ret =3D parse_uint(value, &threshold, &next, 10); > + > + /* no int found -> scan fail */ > + if (ret < 0) { > + error_setg(&local_err, > + "invalid vote_threshold specified"); > + ret =3D -EINVAL; > + goto exit; > + } > + s->threshold =3D threshold; > + } This part looks seriously wrong. I think you should consider using an QemuOpts like other drivers do (have a look at qcow2, for example), that should parse the integer for you. > + /* and validate it against s->total */ > + ret =3D quorum_valid_threshold(s->threshold, s->total, &local_err)= ; > + if (ret < 0) { > + goto exit; > + } > + > + /* is the driver in blkverify mode */ > + value =3D qdict_get_try_str(options, "blkverify"); > + if (value && !strcmp(value, "on") && > + s->total =3D=3D 2 && s->threshold =3D=3D 2) { > + s->is_blkverify =3D true; > + } else if (value && strcmp(value, "off")) { > + fprintf(stderr, "blkverify mode is set by setting blkverify=3D= on " > + "and using two files with vote_threshold=3D2\n= "); > + } > + qdict_del(options, "blkverify"); And the QemuOpts would also know how to parse a boolean. > + > + /* allocate the children BlockDriverState array */ > + s->bs =3D g_new0(BlockDriverState *, s->total); > + opened =3D g_new0(bool, s->total); > + > + /* Open by file name or options dict (command line or QMP) */ > + if (s->total =3D=3D qlist_size(list)) { > + for (i =3D 0, lentry =3D qlist_first(list); lentry; > + lentry =3D qlist_next(lentry), i++) { > + ret =3D bdrv_open(&s->bs[i], NULL, NULL, > + qobject_to_qdict(lentry->value), > + flags, NULL, &local_err); > + if (ret < 0) { > + goto close_exit; > + } > + opened[i] =3D true; > + } > + /* Open by QMP references */ > + } else { > + for (i =3D 0, dentry =3D qdict_first(sub); dentry; > + dentry =3D qdict_next(sub, dentry), i++) { > + ret =3D bdrv_open(&s->bs[i], NULL, > + qstring_get_str(qobject_to_qstring(dentry-= >value)), > + NULL, flags, NULL, &local_err); > + if (ret < 0) { > + goto close_exit; > + } > + opened[i] =3D true; > + } > + } What does the reference case look like? (Should work on the command line, too) I imagine something like this: -drive if=3Dvirtio,file.driver=3Dquorum,\ file.children.0=3Dimage0,\ file.children.1=3Dimage1,\ file.children.2=3Dimage2,\ file.vote_threshold=3D2 Doesn't qdict_array_split() split this into a list of strings? The whole thing with directly accessing QDict entries for references confuses me. > + g_free(opened); > + goto exit; > + > +close_exit: > + /* cleanup on error */ > + for (i =3D 0; i < s->total; i++) { > + if (!opened[i]) { > + continue; > + } > + bdrv_unref(s->bs[i]); > + } > + g_free(s->bs); > + g_free(opened); > +exit: > + /* propagate error */ > + if (error_is_set(&local_err)) { > + error_propagate(errp, local_err); > + } > + QDECREF(sub); > + QDECREF(list); > + return ret; > +} > + > +static void quorum_close(BlockDriverState *bs) > +{ > + BDRVQuorumState *s =3D bs->opaque; > + int i; > + > + for (i =3D 0; i < s->total; i++) { > + /* Ensure writes reach stable storage */ > + bdrv_flush(s->bs[i]); Not necessary, block.c code will flush for us. > + /* close manually because we'll free s->bs */ > + bdrv_unref(s->bs[i]); > + } > + > + g_free(s->bs); > +} Kevin