colo: use local path for local headers
[qemu.git] / block / quorum.c
blobb6476c405a249dae8f1bc728346d1606c13fc651
1 /*
2 * Quorum Block filter
4 * Copyright (C) 2012-2014 Nodalink, EURL.
6 * Author:
7 * BenoƮt Canet <benoit.canet@irqsave.net>
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).
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.
16 #include "qemu/osdep.h"
17 #include "qemu/cutils.h"
18 #include "qemu/option.h"
19 #include "block/block_int.h"
20 #include "qapi/error.h"
21 #include "qapi/qapi-events-block.h"
22 #include "qapi/qmp/qdict.h"
23 #include "qapi/qmp/qerror.h"
24 #include "qapi/qmp/qlist.h"
25 #include "qapi/qmp/qstring.h"
26 #include "crypto/hash.h"
28 #define HASH_LENGTH 32
30 #define QUORUM_OPT_VOTE_THRESHOLD "vote-threshold"
31 #define QUORUM_OPT_BLKVERIFY "blkverify"
32 #define QUORUM_OPT_REWRITE "rewrite-corrupted"
33 #define QUORUM_OPT_READ_PATTERN "read-pattern"
35 /* This union holds a vote hash value */
36 typedef union QuorumVoteValue {
37 uint8_t h[HASH_LENGTH]; /* SHA-256 hash */
38 int64_t l; /* simpler 64 bits hash */
39 } QuorumVoteValue;
41 /* A vote item */
42 typedef struct QuorumVoteItem {
43 int index;
44 QLIST_ENTRY(QuorumVoteItem) next;
45 } QuorumVoteItem;
47 /* this structure is a vote version. A version is the set of votes sharing the
48 * same vote value.
49 * The set of votes will be tracked with the items field and its cardinality is
50 * vote_count.
52 typedef struct QuorumVoteVersion {
53 QuorumVoteValue value;
54 int index;
55 int vote_count;
56 QLIST_HEAD(, QuorumVoteItem) items;
57 QLIST_ENTRY(QuorumVoteVersion) next;
58 } QuorumVoteVersion;
60 /* this structure holds a group of vote versions together */
61 typedef struct QuorumVotes {
62 QLIST_HEAD(, QuorumVoteVersion) vote_list;
63 bool (*compare)(QuorumVoteValue *a, QuorumVoteValue *b);
64 } QuorumVotes;
66 /* the following structure holds the state of one quorum instance */
67 typedef struct BDRVQuorumState {
68 BdrvChild **children; /* children BlockDriverStates */
69 int num_children; /* children count */
70 unsigned next_child_index; /* the index of the next child that should
71 * be added
73 int threshold; /* if less than threshold children reads gave the
74 * same result a quorum error occurs.
76 bool is_blkverify; /* true if the driver is in blkverify mode
77 * Writes are mirrored on two children devices.
78 * On reads the two children devices' contents are
79 * compared and if a difference is spotted its
80 * location is printed and the code aborts.
81 * It is useful to debug other block drivers by
82 * comparing them with a reference one.
84 bool rewrite_corrupted;/* true if the driver must rewrite-on-read corrupted
85 * block if Quorum is reached.
88 QuorumReadPattern read_pattern;
89 } BDRVQuorumState;
91 typedef struct QuorumAIOCB QuorumAIOCB;
93 /* Quorum will create one instance of the following structure per operation it
94 * performs on its children.
95 * So for each read/write operation coming from the upper layer there will be
96 * $children_count QuorumChildRequest.
98 typedef struct QuorumChildRequest {
99 BlockDriverState *bs;
100 QEMUIOVector qiov;
101 uint8_t *buf;
102 int ret;
103 QuorumAIOCB *parent;
104 } QuorumChildRequest;
106 /* Quorum will use the following structure to track progress of each read/write
107 * operation received by the upper layer.
108 * This structure hold pointers to the QuorumChildRequest structures instances
109 * used to do operations on each children and track overall progress.
111 struct QuorumAIOCB {
112 BlockDriverState *bs;
113 Coroutine *co;
115 /* Request metadata */
116 uint64_t offset;
117 uint64_t bytes;
118 int flags;
120 QEMUIOVector *qiov; /* calling IOV */
122 QuorumChildRequest *qcrs; /* individual child requests */
123 int count; /* number of completed AIOCB */
124 int success_count; /* number of successfully completed AIOCB */
126 int rewrite_count; /* number of replica to rewrite: count down to
127 * zero once writes are fired
130 QuorumVotes votes;
132 bool is_read;
133 int vote_ret;
134 int children_read; /* how many children have been read from */
137 typedef struct QuorumCo {
138 QuorumAIOCB *acb;
139 int idx;
140 } QuorumCo;
142 static void quorum_aio_finalize(QuorumAIOCB *acb)
144 g_free(acb->qcrs);
145 g_free(acb);
148 static bool quorum_sha256_compare(QuorumVoteValue *a, QuorumVoteValue *b)
150 return !memcmp(a->h, b->h, HASH_LENGTH);
153 static bool quorum_64bits_compare(QuorumVoteValue *a, QuorumVoteValue *b)
155 return a->l == b->l;
158 static QuorumAIOCB *quorum_aio_get(BlockDriverState *bs,
159 QEMUIOVector *qiov,
160 uint64_t offset,
161 uint64_t bytes,
162 int flags)
164 BDRVQuorumState *s = bs->opaque;
165 QuorumAIOCB *acb = g_new(QuorumAIOCB, 1);
166 int i;
168 *acb = (QuorumAIOCB) {
169 .co = qemu_coroutine_self(),
170 .bs = bs,
171 .offset = offset,
172 .bytes = bytes,
173 .flags = flags,
174 .qiov = qiov,
175 .votes.compare = quorum_sha256_compare,
176 .votes.vote_list = QLIST_HEAD_INITIALIZER(acb.votes.vote_list),
179 acb->qcrs = g_new0(QuorumChildRequest, s->num_children);
180 for (i = 0; i < s->num_children; i++) {
181 acb->qcrs[i].buf = NULL;
182 acb->qcrs[i].ret = 0;
183 acb->qcrs[i].parent = acb;
186 return acb;
189 static void quorum_report_bad(QuorumOpType type, uint64_t offset,
190 uint64_t bytes, char *node_name, int ret)
192 const char *msg = NULL;
193 int64_t start_sector = offset / BDRV_SECTOR_SIZE;
194 int64_t end_sector = DIV_ROUND_UP(offset + bytes, BDRV_SECTOR_SIZE);
196 if (ret < 0) {
197 msg = strerror(-ret);
200 qapi_event_send_quorum_report_bad(type, !!msg, msg, node_name, start_sector,
201 end_sector - start_sector, &error_abort);
204 static void quorum_report_failure(QuorumAIOCB *acb)
206 const char *reference = bdrv_get_device_or_node_name(acb->bs);
207 int64_t start_sector = acb->offset / BDRV_SECTOR_SIZE;
208 int64_t end_sector = DIV_ROUND_UP(acb->offset + acb->bytes,
209 BDRV_SECTOR_SIZE);
211 qapi_event_send_quorum_failure(reference, start_sector,
212 end_sector - start_sector, &error_abort);
215 static int quorum_vote_error(QuorumAIOCB *acb);
217 static bool quorum_has_too_much_io_failed(QuorumAIOCB *acb)
219 BDRVQuorumState *s = acb->bs->opaque;
221 if (acb->success_count < s->threshold) {
222 acb->vote_ret = quorum_vote_error(acb);
223 quorum_report_failure(acb);
224 return true;
227 return false;
230 static int read_fifo_child(QuorumAIOCB *acb);
232 static void quorum_copy_qiov(QEMUIOVector *dest, QEMUIOVector *source)
234 int i;
235 assert(dest->niov == source->niov);
236 assert(dest->size == source->size);
237 for (i = 0; i < source->niov; i++) {
238 assert(dest->iov[i].iov_len == source->iov[i].iov_len);
239 memcpy(dest->iov[i].iov_base,
240 source->iov[i].iov_base,
241 source->iov[i].iov_len);
245 static void quorum_report_bad_acb(QuorumChildRequest *sacb, int ret)
247 QuorumAIOCB *acb = sacb->parent;
248 QuorumOpType type = acb->is_read ? QUORUM_OP_TYPE_READ : QUORUM_OP_TYPE_WRITE;
249 quorum_report_bad(type, acb->offset, acb->bytes, sacb->bs->node_name, ret);
252 static void quorum_report_bad_versions(BDRVQuorumState *s,
253 QuorumAIOCB *acb,
254 QuorumVoteValue *value)
256 QuorumVoteVersion *version;
257 QuorumVoteItem *item;
259 QLIST_FOREACH(version, &acb->votes.vote_list, next) {
260 if (acb->votes.compare(&version->value, value)) {
261 continue;
263 QLIST_FOREACH(item, &version->items, next) {
264 quorum_report_bad(QUORUM_OP_TYPE_READ, acb->offset, acb->bytes,
265 s->children[item->index]->bs->node_name, 0);
270 static void quorum_rewrite_entry(void *opaque)
272 QuorumCo *co = opaque;
273 QuorumAIOCB *acb = co->acb;
274 BDRVQuorumState *s = acb->bs->opaque;
276 /* Ignore any errors, it's just a correction attempt for already
277 * corrupted data.
278 * Mask out BDRV_REQ_WRITE_UNCHANGED because this overwrites the
279 * area with different data from the other children. */
280 bdrv_co_pwritev(s->children[co->idx], acb->offset, acb->bytes,
281 acb->qiov, acb->flags & ~BDRV_REQ_WRITE_UNCHANGED);
283 /* Wake up the caller after the last rewrite */
284 acb->rewrite_count--;
285 if (!acb->rewrite_count) {
286 qemu_coroutine_enter_if_inactive(acb->co);
290 static bool quorum_rewrite_bad_versions(QuorumAIOCB *acb,
291 QuorumVoteValue *value)
293 QuorumVoteVersion *version;
294 QuorumVoteItem *item;
295 int count = 0;
297 /* first count the number of bad versions: done first to avoid concurrency
298 * issues.
300 QLIST_FOREACH(version, &acb->votes.vote_list, next) {
301 if (acb->votes.compare(&version->value, value)) {
302 continue;
304 QLIST_FOREACH(item, &version->items, next) {
305 count++;
309 /* quorum_rewrite_entry will count down this to zero */
310 acb->rewrite_count = count;
312 /* now fire the correcting rewrites */
313 QLIST_FOREACH(version, &acb->votes.vote_list, next) {
314 if (acb->votes.compare(&version->value, value)) {
315 continue;
317 QLIST_FOREACH(item, &version->items, next) {
318 Coroutine *co;
319 QuorumCo data = {
320 .acb = acb,
321 .idx = item->index,
324 co = qemu_coroutine_create(quorum_rewrite_entry, &data);
325 qemu_coroutine_enter(co);
329 /* return true if any rewrite is done else false */
330 return count;
333 static void quorum_count_vote(QuorumVotes *votes,
334 QuorumVoteValue *value,
335 int index)
337 QuorumVoteVersion *v = NULL, *version = NULL;
338 QuorumVoteItem *item;
340 /* look if we have something with this hash */
341 QLIST_FOREACH(v, &votes->vote_list, next) {
342 if (votes->compare(&v->value, value)) {
343 version = v;
344 break;
348 /* It's a version not yet in the list add it */
349 if (!version) {
350 version = g_new0(QuorumVoteVersion, 1);
351 QLIST_INIT(&version->items);
352 memcpy(&version->value, value, sizeof(version->value));
353 version->index = index;
354 version->vote_count = 0;
355 QLIST_INSERT_HEAD(&votes->vote_list, version, next);
358 version->vote_count++;
360 item = g_new0(QuorumVoteItem, 1);
361 item->index = index;
362 QLIST_INSERT_HEAD(&version->items, item, next);
365 static void quorum_free_vote_list(QuorumVotes *votes)
367 QuorumVoteVersion *version, *next_version;
368 QuorumVoteItem *item, *next_item;
370 QLIST_FOREACH_SAFE(version, &votes->vote_list, next, next_version) {
371 QLIST_REMOVE(version, next);
372 QLIST_FOREACH_SAFE(item, &version->items, next, next_item) {
373 QLIST_REMOVE(item, next);
374 g_free(item);
376 g_free(version);
380 static int quorum_compute_hash(QuorumAIOCB *acb, int i, QuorumVoteValue *hash)
382 QEMUIOVector *qiov = &acb->qcrs[i].qiov;
383 size_t len = sizeof(hash->h);
384 uint8_t *data = hash->h;
386 /* XXX - would be nice if we could pass in the Error **
387 * and propagate that back, but this quorum code is
388 * restricted to just errno values currently */
389 if (qcrypto_hash_bytesv(QCRYPTO_HASH_ALG_SHA256,
390 qiov->iov, qiov->niov,
391 &data, &len,
392 NULL) < 0) {
393 return -EINVAL;
396 return 0;
399 static QuorumVoteVersion *quorum_get_vote_winner(QuorumVotes *votes)
401 int max = 0;
402 QuorumVoteVersion *candidate, *winner = NULL;
404 QLIST_FOREACH(candidate, &votes->vote_list, next) {
405 if (candidate->vote_count > max) {
406 max = candidate->vote_count;
407 winner = candidate;
411 return winner;
414 /* qemu_iovec_compare is handy for blkverify mode because it returns the first
415 * differing byte location. Yet it is handcoded to compare vectors one byte
416 * after another so it does not benefit from the libc SIMD optimizations.
417 * quorum_iovec_compare is written for speed and should be used in the non
418 * blkverify mode of quorum.
420 static bool quorum_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
422 int i;
423 int result;
425 assert(a->niov == b->niov);
426 for (i = 0; i < a->niov; i++) {
427 assert(a->iov[i].iov_len == b->iov[i].iov_len);
428 result = memcmp(a->iov[i].iov_base,
429 b->iov[i].iov_base,
430 a->iov[i].iov_len);
431 if (result) {
432 return false;
436 return true;
439 static void GCC_FMT_ATTR(2, 3) quorum_err(QuorumAIOCB *acb,
440 const char *fmt, ...)
442 va_list ap;
444 va_start(ap, fmt);
445 fprintf(stderr, "quorum: offset=%" PRIu64 " bytes=%" PRIu64 " ",
446 acb->offset, acb->bytes);
447 vfprintf(stderr, fmt, ap);
448 fprintf(stderr, "\n");
449 va_end(ap);
450 exit(1);
453 static bool quorum_compare(QuorumAIOCB *acb,
454 QEMUIOVector *a,
455 QEMUIOVector *b)
457 BDRVQuorumState *s = acb->bs->opaque;
458 ssize_t offset;
460 /* This driver will replace blkverify in this particular case */
461 if (s->is_blkverify) {
462 offset = qemu_iovec_compare(a, b);
463 if (offset != -1) {
464 quorum_err(acb, "contents mismatch at offset %" PRIu64,
465 acb->offset + offset);
467 return true;
470 return quorum_iovec_compare(a, b);
473 /* Do a vote to get the error code */
474 static int quorum_vote_error(QuorumAIOCB *acb)
476 BDRVQuorumState *s = acb->bs->opaque;
477 QuorumVoteVersion *winner = NULL;
478 QuorumVotes error_votes;
479 QuorumVoteValue result_value;
480 int i, ret = 0;
481 bool error = false;
483 QLIST_INIT(&error_votes.vote_list);
484 error_votes.compare = quorum_64bits_compare;
486 for (i = 0; i < s->num_children; i++) {
487 ret = acb->qcrs[i].ret;
488 if (ret) {
489 error = true;
490 result_value.l = ret;
491 quorum_count_vote(&error_votes, &result_value, i);
495 if (error) {
496 winner = quorum_get_vote_winner(&error_votes);
497 ret = winner->value.l;
500 quorum_free_vote_list(&error_votes);
502 return ret;
505 static void quorum_vote(QuorumAIOCB *acb)
507 bool quorum = true;
508 int i, j, ret;
509 QuorumVoteValue hash;
510 BDRVQuorumState *s = acb->bs->opaque;
511 QuorumVoteVersion *winner;
513 if (quorum_has_too_much_io_failed(acb)) {
514 return;
517 /* get the index of the first successful read */
518 for (i = 0; i < s->num_children; i++) {
519 if (!acb->qcrs[i].ret) {
520 break;
524 assert(i < s->num_children);
526 /* compare this read with all other successful reads stopping at quorum
527 * failure
529 for (j = i + 1; j < s->num_children; j++) {
530 if (acb->qcrs[j].ret) {
531 continue;
533 quorum = quorum_compare(acb, &acb->qcrs[i].qiov, &acb->qcrs[j].qiov);
534 if (!quorum) {
535 break;
539 /* Every successful read agrees */
540 if (quorum) {
541 quorum_copy_qiov(acb->qiov, &acb->qcrs[i].qiov);
542 return;
545 /* compute hashes for each successful read, also store indexes */
546 for (i = 0; i < s->num_children; i++) {
547 if (acb->qcrs[i].ret) {
548 continue;
550 ret = quorum_compute_hash(acb, i, &hash);
551 /* if ever the hash computation failed */
552 if (ret < 0) {
553 acb->vote_ret = ret;
554 goto free_exit;
556 quorum_count_vote(&acb->votes, &hash, i);
559 /* vote to select the most represented version */
560 winner = quorum_get_vote_winner(&acb->votes);
562 /* if the winner count is smaller than threshold the read fails */
563 if (winner->vote_count < s->threshold) {
564 quorum_report_failure(acb);
565 acb->vote_ret = -EIO;
566 goto free_exit;
569 /* we have a winner: copy it */
570 quorum_copy_qiov(acb->qiov, &acb->qcrs[winner->index].qiov);
572 /* some versions are bad print them */
573 quorum_report_bad_versions(s, acb, &winner->value);
575 /* corruption correction is enabled */
576 if (s->rewrite_corrupted) {
577 quorum_rewrite_bad_versions(acb, &winner->value);
580 free_exit:
581 /* free lists */
582 quorum_free_vote_list(&acb->votes);
585 static void read_quorum_children_entry(void *opaque)
587 QuorumCo *co = opaque;
588 QuorumAIOCB *acb = co->acb;
589 BDRVQuorumState *s = acb->bs->opaque;
590 int i = co->idx;
591 QuorumChildRequest *sacb = &acb->qcrs[i];
593 sacb->bs = s->children[i]->bs;
594 sacb->ret = bdrv_co_preadv(s->children[i], acb->offset, acb->bytes,
595 &acb->qcrs[i].qiov, 0);
597 if (sacb->ret == 0) {
598 acb->success_count++;
599 } else {
600 quorum_report_bad_acb(sacb, sacb->ret);
603 acb->count++;
604 assert(acb->count <= s->num_children);
605 assert(acb->success_count <= s->num_children);
607 /* Wake up the caller after the last read */
608 if (acb->count == s->num_children) {
609 qemu_coroutine_enter_if_inactive(acb->co);
613 static int read_quorum_children(QuorumAIOCB *acb)
615 BDRVQuorumState *s = acb->bs->opaque;
616 int i;
618 acb->children_read = s->num_children;
619 for (i = 0; i < s->num_children; i++) {
620 acb->qcrs[i].buf = qemu_blockalign(s->children[i]->bs, acb->qiov->size);
621 qemu_iovec_init(&acb->qcrs[i].qiov, acb->qiov->niov);
622 qemu_iovec_clone(&acb->qcrs[i].qiov, acb->qiov, acb->qcrs[i].buf);
625 for (i = 0; i < s->num_children; i++) {
626 Coroutine *co;
627 QuorumCo data = {
628 .acb = acb,
629 .idx = i,
632 co = qemu_coroutine_create(read_quorum_children_entry, &data);
633 qemu_coroutine_enter(co);
636 while (acb->count < s->num_children) {
637 qemu_coroutine_yield();
640 /* Do the vote on read */
641 quorum_vote(acb);
642 for (i = 0; i < s->num_children; i++) {
643 qemu_vfree(acb->qcrs[i].buf);
644 qemu_iovec_destroy(&acb->qcrs[i].qiov);
647 while (acb->rewrite_count) {
648 qemu_coroutine_yield();
651 return acb->vote_ret;
654 static int read_fifo_child(QuorumAIOCB *acb)
656 BDRVQuorumState *s = acb->bs->opaque;
657 int n, ret;
659 /* We try to read the next child in FIFO order if we failed to read */
660 do {
661 n = acb->children_read++;
662 acb->qcrs[n].bs = s->children[n]->bs;
663 ret = bdrv_co_preadv(s->children[n], acb->offset, acb->bytes,
664 acb->qiov, 0);
665 if (ret < 0) {
666 quorum_report_bad_acb(&acb->qcrs[n], ret);
668 } while (ret < 0 && acb->children_read < s->num_children);
670 /* FIXME: rewrite failed children if acb->children_read > 1? */
672 return ret;
675 static int quorum_co_preadv(BlockDriverState *bs, uint64_t offset,
676 uint64_t bytes, QEMUIOVector *qiov, int flags)
678 BDRVQuorumState *s = bs->opaque;
679 QuorumAIOCB *acb = quorum_aio_get(bs, qiov, offset, bytes, flags);
680 int ret;
682 acb->is_read = true;
683 acb->children_read = 0;
685 if (s->read_pattern == QUORUM_READ_PATTERN_QUORUM) {
686 ret = read_quorum_children(acb);
687 } else {
688 ret = read_fifo_child(acb);
690 quorum_aio_finalize(acb);
692 return ret;
695 static void write_quorum_entry(void *opaque)
697 QuorumCo *co = opaque;
698 QuorumAIOCB *acb = co->acb;
699 BDRVQuorumState *s = acb->bs->opaque;
700 int i = co->idx;
701 QuorumChildRequest *sacb = &acb->qcrs[i];
703 sacb->bs = s->children[i]->bs;
704 sacb->ret = bdrv_co_pwritev(s->children[i], acb->offset, acb->bytes,
705 acb->qiov, acb->flags);
706 if (sacb->ret == 0) {
707 acb->success_count++;
708 } else {
709 quorum_report_bad_acb(sacb, sacb->ret);
711 acb->count++;
712 assert(acb->count <= s->num_children);
713 assert(acb->success_count <= s->num_children);
715 /* Wake up the caller after the last write */
716 if (acb->count == s->num_children) {
717 qemu_coroutine_enter_if_inactive(acb->co);
721 static int quorum_co_pwritev(BlockDriverState *bs, uint64_t offset,
722 uint64_t bytes, QEMUIOVector *qiov, int flags)
724 BDRVQuorumState *s = bs->opaque;
725 QuorumAIOCB *acb = quorum_aio_get(bs, qiov, offset, bytes, flags);
726 int i, ret;
728 for (i = 0; i < s->num_children; i++) {
729 Coroutine *co;
730 QuorumCo data = {
731 .acb = acb,
732 .idx = i,
735 co = qemu_coroutine_create(write_quorum_entry, &data);
736 qemu_coroutine_enter(co);
739 while (acb->count < s->num_children) {
740 qemu_coroutine_yield();
743 quorum_has_too_much_io_failed(acb);
745 ret = acb->vote_ret;
746 quorum_aio_finalize(acb);
748 return ret;
751 static int64_t quorum_getlength(BlockDriverState *bs)
753 BDRVQuorumState *s = bs->opaque;
754 int64_t result;
755 int i;
757 /* check that all file have the same length */
758 result = bdrv_getlength(s->children[0]->bs);
759 if (result < 0) {
760 return result;
762 for (i = 1; i < s->num_children; i++) {
763 int64_t value = bdrv_getlength(s->children[i]->bs);
764 if (value < 0) {
765 return value;
767 if (value != result) {
768 return -EIO;
772 return result;
775 static coroutine_fn int quorum_co_flush(BlockDriverState *bs)
777 BDRVQuorumState *s = bs->opaque;
778 QuorumVoteVersion *winner = NULL;
779 QuorumVotes error_votes;
780 QuorumVoteValue result_value;
781 int i;
782 int result = 0;
783 int success_count = 0;
785 QLIST_INIT(&error_votes.vote_list);
786 error_votes.compare = quorum_64bits_compare;
788 for (i = 0; i < s->num_children; i++) {
789 result = bdrv_co_flush(s->children[i]->bs);
790 if (result) {
791 quorum_report_bad(QUORUM_OP_TYPE_FLUSH, 0, 0,
792 s->children[i]->bs->node_name, result);
793 result_value.l = result;
794 quorum_count_vote(&error_votes, &result_value, i);
795 } else {
796 success_count++;
800 if (success_count >= s->threshold) {
801 result = 0;
802 } else {
803 winner = quorum_get_vote_winner(&error_votes);
804 result = winner->value.l;
806 quorum_free_vote_list(&error_votes);
808 return result;
811 static bool quorum_recurse_is_first_non_filter(BlockDriverState *bs,
812 BlockDriverState *candidate)
814 BDRVQuorumState *s = bs->opaque;
815 int i;
817 for (i = 0; i < s->num_children; i++) {
818 bool perm = bdrv_recurse_is_first_non_filter(s->children[i]->bs,
819 candidate);
820 if (perm) {
821 return true;
825 return false;
828 static int quorum_valid_threshold(int threshold, int num_children, Error **errp)
831 if (threshold < 1) {
832 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
833 "vote-threshold", "value >= 1");
834 return -ERANGE;
837 if (threshold > num_children) {
838 error_setg(errp, "threshold may not exceed children count");
839 return -ERANGE;
842 return 0;
845 static QemuOptsList quorum_runtime_opts = {
846 .name = "quorum",
847 .head = QTAILQ_HEAD_INITIALIZER(quorum_runtime_opts.head),
848 .desc = {
850 .name = QUORUM_OPT_VOTE_THRESHOLD,
851 .type = QEMU_OPT_NUMBER,
852 .help = "The number of vote needed for reaching quorum",
855 .name = QUORUM_OPT_BLKVERIFY,
856 .type = QEMU_OPT_BOOL,
857 .help = "Trigger block verify mode if set",
860 .name = QUORUM_OPT_REWRITE,
861 .type = QEMU_OPT_BOOL,
862 .help = "Rewrite corrupted block on read quorum",
865 .name = QUORUM_OPT_READ_PATTERN,
866 .type = QEMU_OPT_STRING,
867 .help = "Allowed pattern: quorum, fifo. Quorum is default",
869 { /* end of list */ }
873 static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
874 Error **errp)
876 BDRVQuorumState *s = bs->opaque;
877 Error *local_err = NULL;
878 QemuOpts *opts = NULL;
879 const char *pattern_str;
880 bool *opened;
881 int i;
882 int ret = 0;
884 qdict_flatten(options);
886 /* count how many different children are present */
887 s->num_children = qdict_array_entries(options, "children.");
888 if (s->num_children < 0) {
889 error_setg(&local_err, "Option children is not a valid array");
890 ret = -EINVAL;
891 goto exit;
893 if (s->num_children < 1) {
894 error_setg(&local_err,
895 "Number of provided children must be 1 or more");
896 ret = -EINVAL;
897 goto exit;
900 opts = qemu_opts_create(&quorum_runtime_opts, NULL, 0, &error_abort);
901 qemu_opts_absorb_qdict(opts, options, &local_err);
902 if (local_err) {
903 ret = -EINVAL;
904 goto exit;
907 s->threshold = qemu_opt_get_number(opts, QUORUM_OPT_VOTE_THRESHOLD, 0);
908 /* and validate it against s->num_children */
909 ret = quorum_valid_threshold(s->threshold, s->num_children, &local_err);
910 if (ret < 0) {
911 goto exit;
914 pattern_str = qemu_opt_get(opts, QUORUM_OPT_READ_PATTERN);
915 if (!pattern_str) {
916 ret = QUORUM_READ_PATTERN_QUORUM;
917 } else {
918 ret = qapi_enum_parse(&QuorumReadPattern_lookup, pattern_str,
919 -EINVAL, NULL);
921 if (ret < 0) {
922 error_setg(&local_err, "Please set read-pattern as fifo or quorum");
923 goto exit;
925 s->read_pattern = ret;
927 if (s->read_pattern == QUORUM_READ_PATTERN_QUORUM) {
928 /* is the driver in blkverify mode */
929 if (qemu_opt_get_bool(opts, QUORUM_OPT_BLKVERIFY, false) &&
930 s->num_children == 2 && s->threshold == 2) {
931 s->is_blkverify = true;
932 } else if (qemu_opt_get_bool(opts, QUORUM_OPT_BLKVERIFY, false)) {
933 fprintf(stderr, "blkverify mode is set by setting blkverify=on "
934 "and using two files with vote_threshold=2\n");
937 s->rewrite_corrupted = qemu_opt_get_bool(opts, QUORUM_OPT_REWRITE,
938 false);
939 if (s->rewrite_corrupted && s->is_blkverify) {
940 error_setg(&local_err,
941 "rewrite-corrupted=on cannot be used with blkverify=on");
942 ret = -EINVAL;
943 goto exit;
947 /* allocate the children array */
948 s->children = g_new0(BdrvChild *, s->num_children);
949 opened = g_new0(bool, s->num_children);
951 for (i = 0; i < s->num_children; i++) {
952 char indexstr[32];
953 ret = snprintf(indexstr, 32, "children.%d", i);
954 assert(ret < 32);
956 s->children[i] = bdrv_open_child(NULL, options, indexstr, bs,
957 &child_format, false, &local_err);
958 if (local_err) {
959 ret = -EINVAL;
960 goto close_exit;
963 opened[i] = true;
965 s->next_child_index = s->num_children;
967 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED;
969 g_free(opened);
970 goto exit;
972 close_exit:
973 /* cleanup on error */
974 for (i = 0; i < s->num_children; i++) {
975 if (!opened[i]) {
976 continue;
978 bdrv_unref_child(bs, s->children[i]);
980 g_free(s->children);
981 g_free(opened);
982 exit:
983 qemu_opts_del(opts);
984 /* propagate error */
985 error_propagate(errp, local_err);
986 return ret;
989 static void quorum_close(BlockDriverState *bs)
991 BDRVQuorumState *s = bs->opaque;
992 int i;
994 for (i = 0; i < s->num_children; i++) {
995 bdrv_unref_child(bs, s->children[i]);
998 g_free(s->children);
1001 static void quorum_add_child(BlockDriverState *bs, BlockDriverState *child_bs,
1002 Error **errp)
1004 BDRVQuorumState *s = bs->opaque;
1005 BdrvChild *child;
1006 char indexstr[32];
1007 int ret;
1009 assert(s->num_children <= INT_MAX / sizeof(BdrvChild *));
1010 if (s->num_children == INT_MAX / sizeof(BdrvChild *) ||
1011 s->next_child_index == UINT_MAX) {
1012 error_setg(errp, "Too many children");
1013 return;
1016 ret = snprintf(indexstr, 32, "children.%u", s->next_child_index);
1017 if (ret < 0 || ret >= 32) {
1018 error_setg(errp, "cannot generate child name");
1019 return;
1021 s->next_child_index++;
1023 bdrv_drained_begin(bs);
1025 /* We can safely add the child now */
1026 bdrv_ref(child_bs);
1028 child = bdrv_attach_child(bs, child_bs, indexstr, &child_format, errp);
1029 if (child == NULL) {
1030 s->next_child_index--;
1031 bdrv_unref(child_bs);
1032 goto out;
1034 s->children = g_renew(BdrvChild *, s->children, s->num_children + 1);
1035 s->children[s->num_children++] = child;
1037 out:
1038 bdrv_drained_end(bs);
1041 static void quorum_del_child(BlockDriverState *bs, BdrvChild *child,
1042 Error **errp)
1044 BDRVQuorumState *s = bs->opaque;
1045 int i;
1047 for (i = 0; i < s->num_children; i++) {
1048 if (s->children[i] == child) {
1049 break;
1053 /* we have checked it in bdrv_del_child() */
1054 assert(i < s->num_children);
1056 if (s->num_children <= s->threshold) {
1057 error_setg(errp,
1058 "The number of children cannot be lower than the vote threshold %d",
1059 s->threshold);
1060 return;
1063 bdrv_drained_begin(bs);
1065 /* We can safely remove this child now */
1066 memmove(&s->children[i], &s->children[i + 1],
1067 (s->num_children - i - 1) * sizeof(BdrvChild *));
1068 s->children = g_renew(BdrvChild *, s->children, --s->num_children);
1069 bdrv_unref_child(bs, child);
1071 bdrv_drained_end(bs);
1074 static void quorum_refresh_filename(BlockDriverState *bs, QDict *options)
1076 BDRVQuorumState *s = bs->opaque;
1077 QDict *opts;
1078 QList *children;
1079 int i;
1081 for (i = 0; i < s->num_children; i++) {
1082 bdrv_refresh_filename(s->children[i]->bs);
1083 if (!s->children[i]->bs->full_open_options) {
1084 return;
1088 children = qlist_new();
1089 for (i = 0; i < s->num_children; i++) {
1090 qlist_append(children,
1091 qobject_ref(s->children[i]->bs->full_open_options));
1094 opts = qdict_new();
1095 qdict_put_str(opts, "driver", "quorum");
1096 qdict_put_int(opts, QUORUM_OPT_VOTE_THRESHOLD, s->threshold);
1097 qdict_put_bool(opts, QUORUM_OPT_BLKVERIFY, s->is_blkverify);
1098 qdict_put_bool(opts, QUORUM_OPT_REWRITE, s->rewrite_corrupted);
1099 qdict_put(opts, "children", children);
1101 bs->full_open_options = opts;
1104 static BlockDriver bdrv_quorum = {
1105 .format_name = "quorum",
1107 .instance_size = sizeof(BDRVQuorumState),
1109 .bdrv_open = quorum_open,
1110 .bdrv_close = quorum_close,
1111 .bdrv_refresh_filename = quorum_refresh_filename,
1113 .bdrv_co_flush_to_disk = quorum_co_flush,
1115 .bdrv_getlength = quorum_getlength,
1117 .bdrv_co_preadv = quorum_co_preadv,
1118 .bdrv_co_pwritev = quorum_co_pwritev,
1120 .bdrv_add_child = quorum_add_child,
1121 .bdrv_del_child = quorum_del_child,
1123 .bdrv_child_perm = bdrv_filter_default_perms,
1125 .is_filter = true,
1126 .bdrv_recurse_is_first_non_filter = quorum_recurse_is_first_non_filter,
1129 static void bdrv_quorum_init(void)
1131 if (!qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA256)) {
1132 /* SHA256 hash support is required for quorum device */
1133 return;
1135 bdrv_register(&bdrv_quorum);
1138 block_init(bdrv_quorum_init);