xref: /openbmc/qemu/block/quorum.c (revision d6032e06)
1 /*
2  * Quorum Block filter
3  *
4  * Copyright (C) 2012-2014 Nodalink, EURL.
5  *
6  * Author:
7  *   Benoît Canet <benoit.canet@irqsave.net>
8  *
9  * Based on the design and code of blkverify.c (Copyright (C) 2010 IBM, Corp)
10  * and blkmirror.c (Copyright (C) 2011 Red Hat, Inc).
11  *
12  * This work is licensed under the terms of the GNU GPL, version 2 or later.
13  * See the COPYING file in the top-level directory.
14  */
15 
16 #include <gnutls/gnutls.h>
17 #include <gnutls/crypto.h>
18 #include "block/block_int.h"
19 #include "qapi/qmp/qjson.h"
20 
21 #define HASH_LENGTH 32
22 
23 #define QUORUM_OPT_VOTE_THRESHOLD "vote-threshold"
24 #define QUORUM_OPT_BLKVERIFY      "blkverify"
25 
26 /* This union holds a vote hash value */
27 typedef union QuorumVoteValue {
28     char h[HASH_LENGTH];       /* SHA-256 hash */
29     int64_t l;                 /* simpler 64 bits hash */
30 } QuorumVoteValue;
31 
32 /* A vote item */
33 typedef struct QuorumVoteItem {
34     int index;
35     QLIST_ENTRY(QuorumVoteItem) next;
36 } QuorumVoteItem;
37 
38 /* this structure is a vote version. A version is the set of votes sharing the
39  * same vote value.
40  * The set of votes will be tracked with the items field and its cardinality is
41  * vote_count.
42  */
43 typedef struct QuorumVoteVersion {
44     QuorumVoteValue value;
45     int index;
46     int vote_count;
47     QLIST_HEAD(, QuorumVoteItem) items;
48     QLIST_ENTRY(QuorumVoteVersion) next;
49 } QuorumVoteVersion;
50 
51 /* this structure holds a group of vote versions together */
52 typedef struct QuorumVotes {
53     QLIST_HEAD(, QuorumVoteVersion) vote_list;
54     bool (*compare)(QuorumVoteValue *a, QuorumVoteValue *b);
55 } QuorumVotes;
56 
57 /* the following structure holds the state of one quorum instance */
58 typedef struct BDRVQuorumState {
59     BlockDriverState **bs; /* children BlockDriverStates */
60     int num_children;      /* children count */
61     int threshold;         /* if less than threshold children reads gave the
62                             * same result a quorum error occurs.
63                             */
64     bool is_blkverify;     /* true if the driver is in blkverify mode
65                             * Writes are mirrored on two children devices.
66                             * On reads the two children devices' contents are
67                             * compared and if a difference is spotted its
68                             * location is printed and the code aborts.
69                             * It is useful to debug other block drivers by
70                             * comparing them with a reference one.
71                             */
72 } BDRVQuorumState;
73 
74 typedef struct QuorumAIOCB QuorumAIOCB;
75 
76 /* Quorum will create one instance of the following structure per operation it
77  * performs on its children.
78  * So for each read/write operation coming from the upper layer there will be
79  * $children_count QuorumChildRequest.
80  */
81 typedef struct QuorumChildRequest {
82     BlockDriverAIOCB *aiocb;
83     QEMUIOVector qiov;
84     uint8_t *buf;
85     int ret;
86     QuorumAIOCB *parent;
87 } QuorumChildRequest;
88 
89 /* Quorum will use the following structure to track progress of each read/write
90  * operation received by the upper layer.
91  * This structure hold pointers to the QuorumChildRequest structures instances
92  * used to do operations on each children and track overall progress.
93  */
94 struct QuorumAIOCB {
95     BlockDriverAIOCB common;
96 
97     /* Request metadata */
98     uint64_t sector_num;
99     int nb_sectors;
100 
101     QEMUIOVector *qiov;         /* calling IOV */
102 
103     QuorumChildRequest *qcrs;   /* individual child requests */
104     int count;                  /* number of completed AIOCB */
105     int success_count;          /* number of successfully completed AIOCB */
106 
107     QuorumVotes votes;
108 
109     bool is_read;
110     int vote_ret;
111 };
112 
113 static void quorum_vote(QuorumAIOCB *acb);
114 
115 static void quorum_aio_cancel(BlockDriverAIOCB *blockacb)
116 {
117     QuorumAIOCB *acb = container_of(blockacb, QuorumAIOCB, common);
118     BDRVQuorumState *s = acb->common.bs->opaque;
119     int i;
120 
121     /* cancel all callbacks */
122     for (i = 0; i < s->num_children; i++) {
123         bdrv_aio_cancel(acb->qcrs[i].aiocb);
124     }
125 
126     g_free(acb->qcrs);
127     qemu_aio_release(acb);
128 }
129 
130 static AIOCBInfo quorum_aiocb_info = {
131     .aiocb_size         = sizeof(QuorumAIOCB),
132     .cancel             = quorum_aio_cancel,
133 };
134 
135 static void quorum_aio_finalize(QuorumAIOCB *acb)
136 {
137     BDRVQuorumState *s = acb->common.bs->opaque;
138     int i, ret = 0;
139 
140     if (acb->vote_ret) {
141         ret = acb->vote_ret;
142     }
143 
144     acb->common.cb(acb->common.opaque, ret);
145 
146     if (acb->is_read) {
147         for (i = 0; i < s->num_children; i++) {
148             qemu_vfree(acb->qcrs[i].buf);
149             qemu_iovec_destroy(&acb->qcrs[i].qiov);
150         }
151     }
152 
153     g_free(acb->qcrs);
154     qemu_aio_release(acb);
155 }
156 
157 static bool quorum_sha256_compare(QuorumVoteValue *a, QuorumVoteValue *b)
158 {
159     return !memcmp(a->h, b->h, HASH_LENGTH);
160 }
161 
162 static bool quorum_64bits_compare(QuorumVoteValue *a, QuorumVoteValue *b)
163 {
164     return a->l == b->l;
165 }
166 
167 static QuorumAIOCB *quorum_aio_get(BDRVQuorumState *s,
168                                    BlockDriverState *bs,
169                                    QEMUIOVector *qiov,
170                                    uint64_t sector_num,
171                                    int nb_sectors,
172                                    BlockDriverCompletionFunc *cb,
173                                    void *opaque)
174 {
175     QuorumAIOCB *acb = qemu_aio_get(&quorum_aiocb_info, bs, cb, opaque);
176     int i;
177 
178     acb->common.bs->opaque = s;
179     acb->sector_num = sector_num;
180     acb->nb_sectors = nb_sectors;
181     acb->qiov = qiov;
182     acb->qcrs = g_new0(QuorumChildRequest, s->num_children);
183     acb->count = 0;
184     acb->success_count = 0;
185     acb->votes.compare = quorum_sha256_compare;
186     QLIST_INIT(&acb->votes.vote_list);
187     acb->is_read = false;
188     acb->vote_ret = 0;
189 
190     for (i = 0; i < s->num_children; i++) {
191         acb->qcrs[i].buf = NULL;
192         acb->qcrs[i].ret = 0;
193         acb->qcrs[i].parent = acb;
194     }
195 
196     return acb;
197 }
198 
199 static void quorum_report_bad(QuorumAIOCB *acb, char *node_name, int ret)
200 {
201     QObject *data;
202     assert(node_name);
203     data = qobject_from_jsonf("{ 'ret': %d"
204                               ", 'node-name': %s"
205                               ", 'sector-num': %" PRId64
206                               ", 'sectors-count': %d }",
207                               ret, node_name, acb->sector_num, acb->nb_sectors);
208     monitor_protocol_event(QEVENT_QUORUM_REPORT_BAD, data);
209     qobject_decref(data);
210 }
211 
212 static void quorum_report_failure(QuorumAIOCB *acb)
213 {
214     QObject *data;
215     const char *reference = acb->common.bs->device_name[0] ?
216                             acb->common.bs->device_name :
217                             acb->common.bs->node_name;
218     data = qobject_from_jsonf("{ 'reference': %s"
219                               ", 'sector-num': %" PRId64
220                               ", 'sectors-count': %d }",
221                               reference, acb->sector_num, acb->nb_sectors);
222     monitor_protocol_event(QEVENT_QUORUM_FAILURE, data);
223     qobject_decref(data);
224 }
225 
226 static int quorum_vote_error(QuorumAIOCB *acb);
227 
228 static bool quorum_has_too_much_io_failed(QuorumAIOCB *acb)
229 {
230     BDRVQuorumState *s = acb->common.bs->opaque;
231 
232     if (acb->success_count < s->threshold) {
233         acb->vote_ret = quorum_vote_error(acb);
234         quorum_report_failure(acb);
235         return true;
236     }
237 
238     return false;
239 }
240 
241 static void quorum_aio_cb(void *opaque, int ret)
242 {
243     QuorumChildRequest *sacb = opaque;
244     QuorumAIOCB *acb = sacb->parent;
245     BDRVQuorumState *s = acb->common.bs->opaque;
246 
247     sacb->ret = ret;
248     acb->count++;
249     if (ret == 0) {
250         acb->success_count++;
251     } else {
252         quorum_report_bad(acb, sacb->aiocb->bs->node_name, ret);
253     }
254     assert(acb->count <= s->num_children);
255     assert(acb->success_count <= s->num_children);
256     if (acb->count < s->num_children) {
257         return;
258     }
259 
260     /* Do the vote on read */
261     if (acb->is_read) {
262         quorum_vote(acb);
263     } else {
264         quorum_has_too_much_io_failed(acb);
265     }
266 
267     quorum_aio_finalize(acb);
268 }
269 
270 static void quorum_report_bad_versions(BDRVQuorumState *s,
271                                        QuorumAIOCB *acb,
272                                        QuorumVoteValue *value)
273 {
274     QuorumVoteVersion *version;
275     QuorumVoteItem *item;
276 
277     QLIST_FOREACH(version, &acb->votes.vote_list, next) {
278         if (acb->votes.compare(&version->value, value)) {
279             continue;
280         }
281         QLIST_FOREACH(item, &version->items, next) {
282             quorum_report_bad(acb, s->bs[item->index]->node_name, 0);
283         }
284     }
285 }
286 
287 static void quorum_copy_qiov(QEMUIOVector *dest, QEMUIOVector *source)
288 {
289     int i;
290     assert(dest->niov == source->niov);
291     assert(dest->size == source->size);
292     for (i = 0; i < source->niov; i++) {
293         assert(dest->iov[i].iov_len == source->iov[i].iov_len);
294         memcpy(dest->iov[i].iov_base,
295                source->iov[i].iov_base,
296                source->iov[i].iov_len);
297     }
298 }
299 
300 static void quorum_count_vote(QuorumVotes *votes,
301                               QuorumVoteValue *value,
302                               int index)
303 {
304     QuorumVoteVersion *v = NULL, *version = NULL;
305     QuorumVoteItem *item;
306 
307     /* look if we have something with this hash */
308     QLIST_FOREACH(v, &votes->vote_list, next) {
309         if (votes->compare(&v->value, value)) {
310             version = v;
311             break;
312         }
313     }
314 
315     /* It's a version not yet in the list add it */
316     if (!version) {
317         version = g_new0(QuorumVoteVersion, 1);
318         QLIST_INIT(&version->items);
319         memcpy(&version->value, value, sizeof(version->value));
320         version->index = index;
321         version->vote_count = 0;
322         QLIST_INSERT_HEAD(&votes->vote_list, version, next);
323     }
324 
325     version->vote_count++;
326 
327     item = g_new0(QuorumVoteItem, 1);
328     item->index = index;
329     QLIST_INSERT_HEAD(&version->items, item, next);
330 }
331 
332 static void quorum_free_vote_list(QuorumVotes *votes)
333 {
334     QuorumVoteVersion *version, *next_version;
335     QuorumVoteItem *item, *next_item;
336 
337     QLIST_FOREACH_SAFE(version, &votes->vote_list, next, next_version) {
338         QLIST_REMOVE(version, next);
339         QLIST_FOREACH_SAFE(item, &version->items, next, next_item) {
340             QLIST_REMOVE(item, next);
341             g_free(item);
342         }
343         g_free(version);
344     }
345 }
346 
347 static int quorum_compute_hash(QuorumAIOCB *acb, int i, QuorumVoteValue *hash)
348 {
349     int j, ret;
350     gnutls_hash_hd_t dig;
351     QEMUIOVector *qiov = &acb->qcrs[i].qiov;
352 
353     ret = gnutls_hash_init(&dig, GNUTLS_DIG_SHA256);
354 
355     if (ret < 0) {
356         return ret;
357     }
358 
359     for (j = 0; j < qiov->niov; j++) {
360         ret = gnutls_hash(dig, qiov->iov[j].iov_base, qiov->iov[j].iov_len);
361         if (ret < 0) {
362             break;
363         }
364     }
365 
366     gnutls_hash_deinit(dig, (void *) hash);
367     return ret;
368 }
369 
370 static QuorumVoteVersion *quorum_get_vote_winner(QuorumVotes *votes)
371 {
372     int max = 0;
373     QuorumVoteVersion *candidate, *winner = NULL;
374 
375     QLIST_FOREACH(candidate, &votes->vote_list, next) {
376         if (candidate->vote_count > max) {
377             max = candidate->vote_count;
378             winner = candidate;
379         }
380     }
381 
382     return winner;
383 }
384 
385 /* qemu_iovec_compare is handy for blkverify mode because it returns the first
386  * differing byte location. Yet it is handcoded to compare vectors one byte
387  * after another so it does not benefit from the libc SIMD optimizations.
388  * quorum_iovec_compare is written for speed and should be used in the non
389  * blkverify mode of quorum.
390  */
391 static bool quorum_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
392 {
393     int i;
394     int result;
395 
396     assert(a->niov == b->niov);
397     for (i = 0; i < a->niov; i++) {
398         assert(a->iov[i].iov_len == b->iov[i].iov_len);
399         result = memcmp(a->iov[i].iov_base,
400                         b->iov[i].iov_base,
401                         a->iov[i].iov_len);
402         if (result) {
403             return false;
404         }
405     }
406 
407     return true;
408 }
409 
410 static void GCC_FMT_ATTR(2, 3) quorum_err(QuorumAIOCB *acb,
411                                           const char *fmt, ...)
412 {
413     va_list ap;
414 
415     va_start(ap, fmt);
416     fprintf(stderr, "quorum: sector_num=%" PRId64 " nb_sectors=%d ",
417             acb->sector_num, acb->nb_sectors);
418     vfprintf(stderr, fmt, ap);
419     fprintf(stderr, "\n");
420     va_end(ap);
421     exit(1);
422 }
423 
424 static bool quorum_compare(QuorumAIOCB *acb,
425                            QEMUIOVector *a,
426                            QEMUIOVector *b)
427 {
428     BDRVQuorumState *s = acb->common.bs->opaque;
429     ssize_t offset;
430 
431     /* This driver will replace blkverify in this particular case */
432     if (s->is_blkverify) {
433         offset = qemu_iovec_compare(a, b);
434         if (offset != -1) {
435             quorum_err(acb, "contents mismatch in sector %" PRId64,
436                        acb->sector_num +
437                        (uint64_t)(offset / BDRV_SECTOR_SIZE));
438         }
439         return true;
440     }
441 
442     return quorum_iovec_compare(a, b);
443 }
444 
445 /* Do a vote to get the error code */
446 static int quorum_vote_error(QuorumAIOCB *acb)
447 {
448     BDRVQuorumState *s = acb->common.bs->opaque;
449     QuorumVoteVersion *winner = NULL;
450     QuorumVotes error_votes;
451     QuorumVoteValue result_value;
452     int i, ret = 0;
453     bool error = false;
454 
455     QLIST_INIT(&error_votes.vote_list);
456     error_votes.compare = quorum_64bits_compare;
457 
458     for (i = 0; i < s->num_children; i++) {
459         ret = acb->qcrs[i].ret;
460         if (ret) {
461             error = true;
462             result_value.l = ret;
463             quorum_count_vote(&error_votes, &result_value, i);
464         }
465     }
466 
467     if (error) {
468         winner = quorum_get_vote_winner(&error_votes);
469         ret = winner->value.l;
470     }
471 
472     quorum_free_vote_list(&error_votes);
473 
474     return ret;
475 }
476 
477 static void quorum_vote(QuorumAIOCB *acb)
478 {
479     bool quorum = true;
480     int i, j, ret;
481     QuorumVoteValue hash;
482     BDRVQuorumState *s = acb->common.bs->opaque;
483     QuorumVoteVersion *winner;
484 
485     if (quorum_has_too_much_io_failed(acb)) {
486         return;
487     }
488 
489     /* get the index of the first successful read */
490     for (i = 0; i < s->num_children; i++) {
491         if (!acb->qcrs[i].ret) {
492             break;
493         }
494     }
495 
496     assert(i < s->num_children);
497 
498     /* compare this read with all other successful reads stopping at quorum
499      * failure
500      */
501     for (j = i + 1; j < s->num_children; j++) {
502         if (acb->qcrs[j].ret) {
503             continue;
504         }
505         quorum = quorum_compare(acb, &acb->qcrs[i].qiov, &acb->qcrs[j].qiov);
506         if (!quorum) {
507             break;
508        }
509     }
510 
511     /* Every successful read agrees */
512     if (quorum) {
513         quorum_copy_qiov(acb->qiov, &acb->qcrs[i].qiov);
514         return;
515     }
516 
517     /* compute hashes for each successful read, also store indexes */
518     for (i = 0; i < s->num_children; i++) {
519         if (acb->qcrs[i].ret) {
520             continue;
521         }
522         ret = quorum_compute_hash(acb, i, &hash);
523         /* if ever the hash computation failed */
524         if (ret < 0) {
525             acb->vote_ret = ret;
526             goto free_exit;
527         }
528         quorum_count_vote(&acb->votes, &hash, i);
529     }
530 
531     /* vote to select the most represented version */
532     winner = quorum_get_vote_winner(&acb->votes);
533 
534     /* if the winner count is smaller than threshold the read fails */
535     if (winner->vote_count < s->threshold) {
536         quorum_report_failure(acb);
537         acb->vote_ret = -EIO;
538         goto free_exit;
539     }
540 
541     /* we have a winner: copy it */
542     quorum_copy_qiov(acb->qiov, &acb->qcrs[winner->index].qiov);
543 
544     /* some versions are bad print them */
545     quorum_report_bad_versions(s, acb, &winner->value);
546 
547 free_exit:
548     /* free lists */
549     quorum_free_vote_list(&acb->votes);
550 }
551 
552 static BlockDriverAIOCB *quorum_aio_readv(BlockDriverState *bs,
553                                          int64_t sector_num,
554                                          QEMUIOVector *qiov,
555                                          int nb_sectors,
556                                          BlockDriverCompletionFunc *cb,
557                                          void *opaque)
558 {
559     BDRVQuorumState *s = bs->opaque;
560     QuorumAIOCB *acb = quorum_aio_get(s, bs, qiov, sector_num,
561                                       nb_sectors, cb, opaque);
562     int i;
563 
564     acb->is_read = true;
565 
566     for (i = 0; i < s->num_children; i++) {
567         acb->qcrs[i].buf = qemu_blockalign(s->bs[i], qiov->size);
568         qemu_iovec_init(&acb->qcrs[i].qiov, qiov->niov);
569         qemu_iovec_clone(&acb->qcrs[i].qiov, qiov, acb->qcrs[i].buf);
570     }
571 
572     for (i = 0; i < s->num_children; i++) {
573         bdrv_aio_readv(s->bs[i], sector_num, &acb->qcrs[i].qiov, nb_sectors,
574                        quorum_aio_cb, &acb->qcrs[i]);
575     }
576 
577     return &acb->common;
578 }
579 
580 static BlockDriverAIOCB *quorum_aio_writev(BlockDriverState *bs,
581                                           int64_t sector_num,
582                                           QEMUIOVector *qiov,
583                                           int nb_sectors,
584                                           BlockDriverCompletionFunc *cb,
585                                           void *opaque)
586 {
587     BDRVQuorumState *s = bs->opaque;
588     QuorumAIOCB *acb = quorum_aio_get(s, bs, qiov, sector_num, nb_sectors,
589                                       cb, opaque);
590     int i;
591 
592     for (i = 0; i < s->num_children; i++) {
593         acb->qcrs[i].aiocb = bdrv_aio_writev(s->bs[i], sector_num, qiov,
594                                              nb_sectors, &quorum_aio_cb,
595                                              &acb->qcrs[i]);
596     }
597 
598     return &acb->common;
599 }
600 
601 static int64_t quorum_getlength(BlockDriverState *bs)
602 {
603     BDRVQuorumState *s = bs->opaque;
604     int64_t result;
605     int i;
606 
607     /* check that all file have the same length */
608     result = bdrv_getlength(s->bs[0]);
609     if (result < 0) {
610         return result;
611     }
612     for (i = 1; i < s->num_children; i++) {
613         int64_t value = bdrv_getlength(s->bs[i]);
614         if (value < 0) {
615             return value;
616         }
617         if (value != result) {
618             return -EIO;
619         }
620     }
621 
622     return result;
623 }
624 
625 static void quorum_invalidate_cache(BlockDriverState *bs)
626 {
627     BDRVQuorumState *s = bs->opaque;
628     int i;
629 
630     for (i = 0; i < s->num_children; i++) {
631         bdrv_invalidate_cache(s->bs[i]);
632     }
633 }
634 
635 static coroutine_fn int quorum_co_flush(BlockDriverState *bs)
636 {
637     BDRVQuorumState *s = bs->opaque;
638     QuorumVoteVersion *winner = NULL;
639     QuorumVotes error_votes;
640     QuorumVoteValue result_value;
641     int i;
642     int result = 0;
643 
644     QLIST_INIT(&error_votes.vote_list);
645     error_votes.compare = quorum_64bits_compare;
646 
647     for (i = 0; i < s->num_children; i++) {
648         result = bdrv_co_flush(s->bs[i]);
649         result_value.l = result;
650         quorum_count_vote(&error_votes, &result_value, i);
651     }
652 
653     winner = quorum_get_vote_winner(&error_votes);
654     result = winner->value.l;
655 
656     quorum_free_vote_list(&error_votes);
657 
658     return result;
659 }
660 
661 static bool quorum_recurse_is_first_non_filter(BlockDriverState *bs,
662                                                BlockDriverState *candidate)
663 {
664     BDRVQuorumState *s = bs->opaque;
665     int i;
666 
667     for (i = 0; i < s->num_children; i++) {
668         bool perm = bdrv_recurse_is_first_non_filter(s->bs[i],
669                                                      candidate);
670         if (perm) {
671             return true;
672         }
673     }
674 
675     return false;
676 }
677 
678 static int quorum_valid_threshold(int threshold, int num_children, Error **errp)
679 {
680 
681     if (threshold < 1) {
682         error_set(errp, QERR_INVALID_PARAMETER_VALUE,
683                   "vote-threshold", "value >= 1");
684         return -ERANGE;
685     }
686 
687     if (threshold > num_children) {
688         error_setg(errp, "threshold may not exceed children count");
689         return -ERANGE;
690     }
691 
692     return 0;
693 }
694 
695 static QemuOptsList quorum_runtime_opts = {
696     .name = "quorum",
697     .head = QTAILQ_HEAD_INITIALIZER(quorum_runtime_opts.head),
698     .desc = {
699         {
700             .name = QUORUM_OPT_VOTE_THRESHOLD,
701             .type = QEMU_OPT_NUMBER,
702             .help = "The number of vote needed for reaching quorum",
703         },
704         {
705             .name = QUORUM_OPT_BLKVERIFY,
706             .type = QEMU_OPT_BOOL,
707             .help = "Trigger block verify mode if set",
708         },
709         { /* end of list */ }
710     },
711 };
712 
713 static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
714                        Error **errp)
715 {
716     BDRVQuorumState *s = bs->opaque;
717     Error *local_err = NULL;
718     QemuOpts *opts;
719     bool *opened;
720     QDict *sub = NULL;
721     QList *list = NULL;
722     const QListEntry *lentry;
723     int i;
724     int ret = 0;
725 
726     qdict_flatten(options);
727     qdict_extract_subqdict(options, &sub, "children.");
728     qdict_array_split(sub, &list);
729 
730     if (qdict_size(sub)) {
731         error_setg(&local_err, "Invalid option children.%s",
732                    qdict_first(sub)->key);
733         ret = -EINVAL;
734         goto exit;
735     }
736 
737     /* count how many different children are present */
738     s->num_children = qlist_size(list);
739     if (s->num_children < 2) {
740         error_setg(&local_err,
741                    "Number of provided children must be greater than 1");
742         ret = -EINVAL;
743         goto exit;
744     }
745 
746     opts = qemu_opts_create(&quorum_runtime_opts, NULL, 0, &error_abort);
747     qemu_opts_absorb_qdict(opts, options, &local_err);
748     if (error_is_set(&local_err)) {
749         ret = -EINVAL;
750         goto exit;
751     }
752 
753     s->threshold = qemu_opt_get_number(opts, QUORUM_OPT_VOTE_THRESHOLD, 0);
754 
755     /* and validate it against s->num_children */
756     ret = quorum_valid_threshold(s->threshold, s->num_children, &local_err);
757     if (ret < 0) {
758         goto exit;
759     }
760 
761     /* is the driver in blkverify mode */
762     if (qemu_opt_get_bool(opts, QUORUM_OPT_BLKVERIFY, false) &&
763         s->num_children == 2 && s->threshold == 2) {
764         s->is_blkverify = true;
765     } else if (qemu_opt_get_bool(opts, QUORUM_OPT_BLKVERIFY, false)) {
766         fprintf(stderr, "blkverify mode is set by setting blkverify=on "
767                 "and using two files with vote_threshold=2\n");
768     }
769 
770     /* allocate the children BlockDriverState array */
771     s->bs = g_new0(BlockDriverState *, s->num_children);
772     opened = g_new0(bool, s->num_children);
773 
774     for (i = 0, lentry = qlist_first(list); lentry;
775          lentry = qlist_next(lentry), i++) {
776         QDict *d;
777         QString *string;
778 
779         switch (qobject_type(lentry->value))
780         {
781             /* List of options */
782             case QTYPE_QDICT:
783                 d = qobject_to_qdict(lentry->value);
784                 QINCREF(d);
785                 ret = bdrv_open(&s->bs[i], NULL, NULL, d, flags, NULL,
786                                 &local_err);
787                 break;
788 
789             /* QMP reference */
790             case QTYPE_QSTRING:
791                 string = qobject_to_qstring(lentry->value);
792                 ret = bdrv_open(&s->bs[i], NULL, qstring_get_str(string), NULL,
793                                 flags, NULL, &local_err);
794                 break;
795 
796             default:
797                 error_setg(&local_err, "Specification of child block device %i "
798                            "is invalid", i);
799                 ret = -EINVAL;
800         }
801 
802         if (ret < 0) {
803             goto close_exit;
804         }
805         opened[i] = true;
806     }
807 
808     g_free(opened);
809     goto exit;
810 
811 close_exit:
812     /* cleanup on error */
813     for (i = 0; i < s->num_children; i++) {
814         if (!opened[i]) {
815             continue;
816         }
817         bdrv_unref(s->bs[i]);
818     }
819     g_free(s->bs);
820     g_free(opened);
821 exit:
822     /* propagate error */
823     if (error_is_set(&local_err)) {
824         error_propagate(errp, local_err);
825     }
826     QDECREF(list);
827     QDECREF(sub);
828     return ret;
829 }
830 
831 static void quorum_close(BlockDriverState *bs)
832 {
833     BDRVQuorumState *s = bs->opaque;
834     int i;
835 
836     for (i = 0; i < s->num_children; i++) {
837         bdrv_unref(s->bs[i]);
838     }
839 
840     g_free(s->bs);
841 }
842 
843 static BlockDriver bdrv_quorum = {
844     .format_name        = "quorum",
845     .protocol_name      = "quorum",
846 
847     .instance_size      = sizeof(BDRVQuorumState),
848 
849     .bdrv_file_open     = quorum_open,
850     .bdrv_close         = quorum_close,
851 
852     .authorizations     = { true, true },
853 
854     .bdrv_co_flush_to_disk = quorum_co_flush,
855 
856     .bdrv_getlength     = quorum_getlength,
857 
858     .bdrv_aio_readv     = quorum_aio_readv,
859     .bdrv_aio_writev    = quorum_aio_writev,
860     .bdrv_invalidate_cache = quorum_invalidate_cache,
861 
862     .bdrv_recurse_is_first_non_filter = quorum_recurse_is_first_non_filter,
863 };
864 
865 static void bdrv_quorum_init(void)
866 {
867     bdrv_register(&bdrv_quorum);
868 }
869 
870 block_init(bdrv_quorum_init);
871