4 * Copyright (C) 2012-2014 Nodalink, EURL.
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 "block/qdict.h"
21 #include "qapi/error.h"
22 #include "qapi/qapi-events-block.h"
23 #include "qapi/qmp/qdict.h"
24 #include "qapi/qmp/qerror.h"
25 #include "qapi/qmp/qlist.h"
26 #include "qapi/qmp/qstring.h"
27 #include "crypto/hash.h"
29 #define HASH_LENGTH 32
31 #define QUORUM_OPT_VOTE_THRESHOLD "vote-threshold"
32 #define QUORUM_OPT_BLKVERIFY "blkverify"
33 #define QUORUM_OPT_REWRITE "rewrite-corrupted"
34 #define QUORUM_OPT_READ_PATTERN "read-pattern"
36 /* This union holds a vote hash value */
37 typedef union QuorumVoteValue
{
38 uint8_t h
[HASH_LENGTH
]; /* SHA-256 hash */
39 int64_t l
; /* simpler 64 bits hash */
43 typedef struct QuorumVoteItem
{
45 QLIST_ENTRY(QuorumVoteItem
) next
;
48 /* this structure is a vote version. A version is the set of votes sharing the
50 * The set of votes will be tracked with the items field and its cardinality is
53 typedef struct QuorumVoteVersion
{
54 QuorumVoteValue value
;
57 QLIST_HEAD(, QuorumVoteItem
) items
;
58 QLIST_ENTRY(QuorumVoteVersion
) next
;
61 /* this structure holds a group of vote versions together */
62 typedef struct QuorumVotes
{
63 QLIST_HEAD(, QuorumVoteVersion
) vote_list
;
64 bool (*compare
)(QuorumVoteValue
*a
, QuorumVoteValue
*b
);
67 /* the following structure holds the state of one quorum instance */
68 typedef struct BDRVQuorumState
{
69 BdrvChild
**children
; /* children BlockDriverStates */
70 int num_children
; /* children count */
71 unsigned next_child_index
; /* the index of the next child that should
74 int threshold
; /* if less than threshold children reads gave the
75 * same result a quorum error occurs.
77 bool is_blkverify
; /* true if the driver is in blkverify mode
78 * Writes are mirrored on two children devices.
79 * On reads the two children devices' contents are
80 * compared and if a difference is spotted its
81 * location is printed and the code aborts.
82 * It is useful to debug other block drivers by
83 * comparing them with a reference one.
85 bool rewrite_corrupted
;/* true if the driver must rewrite-on-read corrupted
86 * block if Quorum is reached.
89 QuorumReadPattern read_pattern
;
92 typedef struct QuorumAIOCB QuorumAIOCB
;
94 /* Quorum will create one instance of the following structure per operation it
95 * performs on its children.
96 * So for each read/write operation coming from the upper layer there will be
97 * $children_count QuorumChildRequest.
99 typedef struct QuorumChildRequest
{
100 BlockDriverState
*bs
;
105 } QuorumChildRequest
;
107 /* Quorum will use the following structure to track progress of each read/write
108 * operation received by the upper layer.
109 * This structure hold pointers to the QuorumChildRequest structures instances
110 * used to do operations on each children and track overall progress.
113 BlockDriverState
*bs
;
116 /* Request metadata */
121 QEMUIOVector
*qiov
; /* calling IOV */
123 QuorumChildRequest
*qcrs
; /* individual child requests */
124 int count
; /* number of completed AIOCB */
125 int success_count
; /* number of successfully completed AIOCB */
127 int rewrite_count
; /* number of replica to rewrite: count down to
128 * zero once writes are fired
135 int children_read
; /* how many children have been read from */
138 typedef struct QuorumCo
{
143 static void quorum_aio_finalize(QuorumAIOCB
*acb
)
149 static bool quorum_sha256_compare(QuorumVoteValue
*a
, QuorumVoteValue
*b
)
151 return !memcmp(a
->h
, b
->h
, HASH_LENGTH
);
154 static bool quorum_64bits_compare(QuorumVoteValue
*a
, QuorumVoteValue
*b
)
159 static QuorumAIOCB
*quorum_aio_get(BlockDriverState
*bs
,
165 BDRVQuorumState
*s
= bs
->opaque
;
166 QuorumAIOCB
*acb
= g_new(QuorumAIOCB
, 1);
169 *acb
= (QuorumAIOCB
) {
170 .co
= qemu_coroutine_self(),
176 .votes
.compare
= quorum_sha256_compare
,
177 .votes
.vote_list
= QLIST_HEAD_INITIALIZER(acb
.votes
.vote_list
),
180 acb
->qcrs
= g_new0(QuorumChildRequest
, s
->num_children
);
181 for (i
= 0; i
< s
->num_children
; i
++) {
182 acb
->qcrs
[i
].buf
= NULL
;
183 acb
->qcrs
[i
].ret
= 0;
184 acb
->qcrs
[i
].parent
= acb
;
190 static void quorum_report_bad(QuorumOpType type
, uint64_t offset
,
191 uint64_t bytes
, char *node_name
, int ret
)
193 const char *msg
= NULL
;
194 int64_t start_sector
= offset
/ BDRV_SECTOR_SIZE
;
195 int64_t end_sector
= DIV_ROUND_UP(offset
+ bytes
, BDRV_SECTOR_SIZE
);
198 msg
= strerror(-ret
);
201 qapi_event_send_quorum_report_bad(type
, !!msg
, msg
, node_name
, start_sector
,
202 end_sector
- start_sector
, &error_abort
);
205 static void quorum_report_failure(QuorumAIOCB
*acb
)
207 const char *reference
= bdrv_get_device_or_node_name(acb
->bs
);
208 int64_t start_sector
= acb
->offset
/ BDRV_SECTOR_SIZE
;
209 int64_t end_sector
= DIV_ROUND_UP(acb
->offset
+ acb
->bytes
,
212 qapi_event_send_quorum_failure(reference
, start_sector
,
213 end_sector
- start_sector
, &error_abort
);
216 static int quorum_vote_error(QuorumAIOCB
*acb
);
218 static bool quorum_has_too_much_io_failed(QuorumAIOCB
*acb
)
220 BDRVQuorumState
*s
= acb
->bs
->opaque
;
222 if (acb
->success_count
< s
->threshold
) {
223 acb
->vote_ret
= quorum_vote_error(acb
);
224 quorum_report_failure(acb
);
231 static int read_fifo_child(QuorumAIOCB
*acb
);
233 static void quorum_copy_qiov(QEMUIOVector
*dest
, QEMUIOVector
*source
)
236 assert(dest
->niov
== source
->niov
);
237 assert(dest
->size
== source
->size
);
238 for (i
= 0; i
< source
->niov
; i
++) {
239 assert(dest
->iov
[i
].iov_len
== source
->iov
[i
].iov_len
);
240 memcpy(dest
->iov
[i
].iov_base
,
241 source
->iov
[i
].iov_base
,
242 source
->iov
[i
].iov_len
);
246 static void quorum_report_bad_acb(QuorumChildRequest
*sacb
, int ret
)
248 QuorumAIOCB
*acb
= sacb
->parent
;
249 QuorumOpType type
= acb
->is_read
? QUORUM_OP_TYPE_READ
: QUORUM_OP_TYPE_WRITE
;
250 quorum_report_bad(type
, acb
->offset
, acb
->bytes
, sacb
->bs
->node_name
, ret
);
253 static void quorum_report_bad_versions(BDRVQuorumState
*s
,
255 QuorumVoteValue
*value
)
257 QuorumVoteVersion
*version
;
258 QuorumVoteItem
*item
;
260 QLIST_FOREACH(version
, &acb
->votes
.vote_list
, next
) {
261 if (acb
->votes
.compare(&version
->value
, value
)) {
264 QLIST_FOREACH(item
, &version
->items
, next
) {
265 quorum_report_bad(QUORUM_OP_TYPE_READ
, acb
->offset
, acb
->bytes
,
266 s
->children
[item
->index
]->bs
->node_name
, 0);
271 static void quorum_rewrite_entry(void *opaque
)
273 QuorumCo
*co
= opaque
;
274 QuorumAIOCB
*acb
= co
->acb
;
275 BDRVQuorumState
*s
= acb
->bs
->opaque
;
277 /* Ignore any errors, it's just a correction attempt for already
279 * Mask out BDRV_REQ_WRITE_UNCHANGED because this overwrites the
280 * area with different data from the other children. */
281 bdrv_co_pwritev(s
->children
[co
->idx
], acb
->offset
, acb
->bytes
,
282 acb
->qiov
, acb
->flags
& ~BDRV_REQ_WRITE_UNCHANGED
);
284 /* Wake up the caller after the last rewrite */
285 acb
->rewrite_count
--;
286 if (!acb
->rewrite_count
) {
287 qemu_coroutine_enter_if_inactive(acb
->co
);
291 static bool quorum_rewrite_bad_versions(QuorumAIOCB
*acb
,
292 QuorumVoteValue
*value
)
294 QuorumVoteVersion
*version
;
295 QuorumVoteItem
*item
;
298 /* first count the number of bad versions: done first to avoid concurrency
301 QLIST_FOREACH(version
, &acb
->votes
.vote_list
, next
) {
302 if (acb
->votes
.compare(&version
->value
, value
)) {
305 QLIST_FOREACH(item
, &version
->items
, next
) {
310 /* quorum_rewrite_entry will count down this to zero */
311 acb
->rewrite_count
= count
;
313 /* now fire the correcting rewrites */
314 QLIST_FOREACH(version
, &acb
->votes
.vote_list
, next
) {
315 if (acb
->votes
.compare(&version
->value
, value
)) {
318 QLIST_FOREACH(item
, &version
->items
, next
) {
325 co
= qemu_coroutine_create(quorum_rewrite_entry
, &data
);
326 qemu_coroutine_enter(co
);
330 /* return true if any rewrite is done else false */
334 static void quorum_count_vote(QuorumVotes
*votes
,
335 QuorumVoteValue
*value
,
338 QuorumVoteVersion
*v
= NULL
, *version
= NULL
;
339 QuorumVoteItem
*item
;
341 /* look if we have something with this hash */
342 QLIST_FOREACH(v
, &votes
->vote_list
, next
) {
343 if (votes
->compare(&v
->value
, value
)) {
349 /* It's a version not yet in the list add it */
351 version
= g_new0(QuorumVoteVersion
, 1);
352 QLIST_INIT(&version
->items
);
353 memcpy(&version
->value
, value
, sizeof(version
->value
));
354 version
->index
= index
;
355 version
->vote_count
= 0;
356 QLIST_INSERT_HEAD(&votes
->vote_list
, version
, next
);
359 version
->vote_count
++;
361 item
= g_new0(QuorumVoteItem
, 1);
363 QLIST_INSERT_HEAD(&version
->items
, item
, next
);
366 static void quorum_free_vote_list(QuorumVotes
*votes
)
368 QuorumVoteVersion
*version
, *next_version
;
369 QuorumVoteItem
*item
, *next_item
;
371 QLIST_FOREACH_SAFE(version
, &votes
->vote_list
, next
, next_version
) {
372 QLIST_REMOVE(version
, next
);
373 QLIST_FOREACH_SAFE(item
, &version
->items
, next
, next_item
) {
374 QLIST_REMOVE(item
, next
);
381 static int quorum_compute_hash(QuorumAIOCB
*acb
, int i
, QuorumVoteValue
*hash
)
383 QEMUIOVector
*qiov
= &acb
->qcrs
[i
].qiov
;
384 size_t len
= sizeof(hash
->h
);
385 uint8_t *data
= hash
->h
;
387 /* XXX - would be nice if we could pass in the Error **
388 * and propagate that back, but this quorum code is
389 * restricted to just errno values currently */
390 if (qcrypto_hash_bytesv(QCRYPTO_HASH_ALG_SHA256
,
391 qiov
->iov
, qiov
->niov
,
400 static QuorumVoteVersion
*quorum_get_vote_winner(QuorumVotes
*votes
)
403 QuorumVoteVersion
*candidate
, *winner
= NULL
;
405 QLIST_FOREACH(candidate
, &votes
->vote_list
, next
) {
406 if (candidate
->vote_count
> max
) {
407 max
= candidate
->vote_count
;
415 /* qemu_iovec_compare is handy for blkverify mode because it returns the first
416 * differing byte location. Yet it is handcoded to compare vectors one byte
417 * after another so it does not benefit from the libc SIMD optimizations.
418 * quorum_iovec_compare is written for speed and should be used in the non
419 * blkverify mode of quorum.
421 static bool quorum_iovec_compare(QEMUIOVector
*a
, QEMUIOVector
*b
)
426 assert(a
->niov
== b
->niov
);
427 for (i
= 0; i
< a
->niov
; i
++) {
428 assert(a
->iov
[i
].iov_len
== b
->iov
[i
].iov_len
);
429 result
= memcmp(a
->iov
[i
].iov_base
,
440 static void GCC_FMT_ATTR(2, 3) quorum_err(QuorumAIOCB
*acb
,
441 const char *fmt
, ...)
446 fprintf(stderr
, "quorum: offset=%" PRIu64
" bytes=%" PRIu64
" ",
447 acb
->offset
, acb
->bytes
);
448 vfprintf(stderr
, fmt
, ap
);
449 fprintf(stderr
, "\n");
454 static bool quorum_compare(QuorumAIOCB
*acb
,
458 BDRVQuorumState
*s
= acb
->bs
->opaque
;
461 /* This driver will replace blkverify in this particular case */
462 if (s
->is_blkverify
) {
463 offset
= qemu_iovec_compare(a
, b
);
465 quorum_err(acb
, "contents mismatch at offset %" PRIu64
,
466 acb
->offset
+ offset
);
471 return quorum_iovec_compare(a
, b
);
474 /* Do a vote to get the error code */
475 static int quorum_vote_error(QuorumAIOCB
*acb
)
477 BDRVQuorumState
*s
= acb
->bs
->opaque
;
478 QuorumVoteVersion
*winner
= NULL
;
479 QuorumVotes error_votes
;
480 QuorumVoteValue result_value
;
484 QLIST_INIT(&error_votes
.vote_list
);
485 error_votes
.compare
= quorum_64bits_compare
;
487 for (i
= 0; i
< s
->num_children
; i
++) {
488 ret
= acb
->qcrs
[i
].ret
;
491 result_value
.l
= ret
;
492 quorum_count_vote(&error_votes
, &result_value
, i
);
497 winner
= quorum_get_vote_winner(&error_votes
);
498 ret
= winner
->value
.l
;
501 quorum_free_vote_list(&error_votes
);
506 static void quorum_vote(QuorumAIOCB
*acb
)
510 QuorumVoteValue hash
;
511 BDRVQuorumState
*s
= acb
->bs
->opaque
;
512 QuorumVoteVersion
*winner
;
514 if (quorum_has_too_much_io_failed(acb
)) {
518 /* get the index of the first successful read */
519 for (i
= 0; i
< s
->num_children
; i
++) {
520 if (!acb
->qcrs
[i
].ret
) {
525 assert(i
< s
->num_children
);
527 /* compare this read with all other successful reads stopping at quorum
530 for (j
= i
+ 1; j
< s
->num_children
; j
++) {
531 if (acb
->qcrs
[j
].ret
) {
534 quorum
= quorum_compare(acb
, &acb
->qcrs
[i
].qiov
, &acb
->qcrs
[j
].qiov
);
540 /* Every successful read agrees */
542 quorum_copy_qiov(acb
->qiov
, &acb
->qcrs
[i
].qiov
);
546 /* compute hashes for each successful read, also store indexes */
547 for (i
= 0; i
< s
->num_children
; i
++) {
548 if (acb
->qcrs
[i
].ret
) {
551 ret
= quorum_compute_hash(acb
, i
, &hash
);
552 /* if ever the hash computation failed */
557 quorum_count_vote(&acb
->votes
, &hash
, i
);
560 /* vote to select the most represented version */
561 winner
= quorum_get_vote_winner(&acb
->votes
);
563 /* if the winner count is smaller than threshold the read fails */
564 if (winner
->vote_count
< s
->threshold
) {
565 quorum_report_failure(acb
);
566 acb
->vote_ret
= -EIO
;
570 /* we have a winner: copy it */
571 quorum_copy_qiov(acb
->qiov
, &acb
->qcrs
[winner
->index
].qiov
);
573 /* some versions are bad print them */
574 quorum_report_bad_versions(s
, acb
, &winner
->value
);
576 /* corruption correction is enabled */
577 if (s
->rewrite_corrupted
) {
578 quorum_rewrite_bad_versions(acb
, &winner
->value
);
583 quorum_free_vote_list(&acb
->votes
);
586 static void read_quorum_children_entry(void *opaque
)
588 QuorumCo
*co
= opaque
;
589 QuorumAIOCB
*acb
= co
->acb
;
590 BDRVQuorumState
*s
= acb
->bs
->opaque
;
592 QuorumChildRequest
*sacb
= &acb
->qcrs
[i
];
594 sacb
->bs
= s
->children
[i
]->bs
;
595 sacb
->ret
= bdrv_co_preadv(s
->children
[i
], acb
->offset
, acb
->bytes
,
596 &acb
->qcrs
[i
].qiov
, 0);
598 if (sacb
->ret
== 0) {
599 acb
->success_count
++;
601 quorum_report_bad_acb(sacb
, sacb
->ret
);
605 assert(acb
->count
<= s
->num_children
);
606 assert(acb
->success_count
<= s
->num_children
);
608 /* Wake up the caller after the last read */
609 if (acb
->count
== s
->num_children
) {
610 qemu_coroutine_enter_if_inactive(acb
->co
);
614 static int read_quorum_children(QuorumAIOCB
*acb
)
616 BDRVQuorumState
*s
= acb
->bs
->opaque
;
619 acb
->children_read
= s
->num_children
;
620 for (i
= 0; i
< s
->num_children
; i
++) {
621 acb
->qcrs
[i
].buf
= qemu_blockalign(s
->children
[i
]->bs
, acb
->qiov
->size
);
622 qemu_iovec_init(&acb
->qcrs
[i
].qiov
, acb
->qiov
->niov
);
623 qemu_iovec_clone(&acb
->qcrs
[i
].qiov
, acb
->qiov
, acb
->qcrs
[i
].buf
);
626 for (i
= 0; i
< s
->num_children
; i
++) {
633 co
= qemu_coroutine_create(read_quorum_children_entry
, &data
);
634 qemu_coroutine_enter(co
);
637 while (acb
->count
< s
->num_children
) {
638 qemu_coroutine_yield();
641 /* Do the vote on read */
643 for (i
= 0; i
< s
->num_children
; i
++) {
644 qemu_vfree(acb
->qcrs
[i
].buf
);
645 qemu_iovec_destroy(&acb
->qcrs
[i
].qiov
);
648 while (acb
->rewrite_count
) {
649 qemu_coroutine_yield();
652 return acb
->vote_ret
;
655 static int read_fifo_child(QuorumAIOCB
*acb
)
657 BDRVQuorumState
*s
= acb
->bs
->opaque
;
660 /* We try to read the next child in FIFO order if we failed to read */
662 n
= acb
->children_read
++;
663 acb
->qcrs
[n
].bs
= s
->children
[n
]->bs
;
664 ret
= bdrv_co_preadv(s
->children
[n
], acb
->offset
, acb
->bytes
,
667 quorum_report_bad_acb(&acb
->qcrs
[n
], ret
);
669 } while (ret
< 0 && acb
->children_read
< s
->num_children
);
671 /* FIXME: rewrite failed children if acb->children_read > 1? */
676 static int quorum_co_preadv(BlockDriverState
*bs
, uint64_t offset
,
677 uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
679 BDRVQuorumState
*s
= bs
->opaque
;
680 QuorumAIOCB
*acb
= quorum_aio_get(bs
, qiov
, offset
, bytes
, flags
);
684 acb
->children_read
= 0;
686 if (s
->read_pattern
== QUORUM_READ_PATTERN_QUORUM
) {
687 ret
= read_quorum_children(acb
);
689 ret
= read_fifo_child(acb
);
691 quorum_aio_finalize(acb
);
696 static void write_quorum_entry(void *opaque
)
698 QuorumCo
*co
= opaque
;
699 QuorumAIOCB
*acb
= co
->acb
;
700 BDRVQuorumState
*s
= acb
->bs
->opaque
;
702 QuorumChildRequest
*sacb
= &acb
->qcrs
[i
];
704 sacb
->bs
= s
->children
[i
]->bs
;
705 sacb
->ret
= bdrv_co_pwritev(s
->children
[i
], acb
->offset
, acb
->bytes
,
706 acb
->qiov
, acb
->flags
);
707 if (sacb
->ret
== 0) {
708 acb
->success_count
++;
710 quorum_report_bad_acb(sacb
, sacb
->ret
);
713 assert(acb
->count
<= s
->num_children
);
714 assert(acb
->success_count
<= s
->num_children
);
716 /* Wake up the caller after the last write */
717 if (acb
->count
== s
->num_children
) {
718 qemu_coroutine_enter_if_inactive(acb
->co
);
722 static int quorum_co_pwritev(BlockDriverState
*bs
, uint64_t offset
,
723 uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
725 BDRVQuorumState
*s
= bs
->opaque
;
726 QuorumAIOCB
*acb
= quorum_aio_get(bs
, qiov
, offset
, bytes
, flags
);
729 for (i
= 0; i
< s
->num_children
; i
++) {
736 co
= qemu_coroutine_create(write_quorum_entry
, &data
);
737 qemu_coroutine_enter(co
);
740 while (acb
->count
< s
->num_children
) {
741 qemu_coroutine_yield();
744 quorum_has_too_much_io_failed(acb
);
747 quorum_aio_finalize(acb
);
752 static int64_t quorum_getlength(BlockDriverState
*bs
)
754 BDRVQuorumState
*s
= bs
->opaque
;
758 /* check that all file have the same length */
759 result
= bdrv_getlength(s
->children
[0]->bs
);
763 for (i
= 1; i
< s
->num_children
; i
++) {
764 int64_t value
= bdrv_getlength(s
->children
[i
]->bs
);
768 if (value
!= result
) {
776 static coroutine_fn
int quorum_co_flush(BlockDriverState
*bs
)
778 BDRVQuorumState
*s
= bs
->opaque
;
779 QuorumVoteVersion
*winner
= NULL
;
780 QuorumVotes error_votes
;
781 QuorumVoteValue result_value
;
784 int success_count
= 0;
786 QLIST_INIT(&error_votes
.vote_list
);
787 error_votes
.compare
= quorum_64bits_compare
;
789 for (i
= 0; i
< s
->num_children
; i
++) {
790 result
= bdrv_co_flush(s
->children
[i
]->bs
);
792 quorum_report_bad(QUORUM_OP_TYPE_FLUSH
, 0, 0,
793 s
->children
[i
]->bs
->node_name
, result
);
794 result_value
.l
= result
;
795 quorum_count_vote(&error_votes
, &result_value
, i
);
801 if (success_count
>= s
->threshold
) {
804 winner
= quorum_get_vote_winner(&error_votes
);
805 result
= winner
->value
.l
;
807 quorum_free_vote_list(&error_votes
);
812 static bool quorum_recurse_is_first_non_filter(BlockDriverState
*bs
,
813 BlockDriverState
*candidate
)
815 BDRVQuorumState
*s
= bs
->opaque
;
818 for (i
= 0; i
< s
->num_children
; i
++) {
819 bool perm
= bdrv_recurse_is_first_non_filter(s
->children
[i
]->bs
,
829 static int quorum_valid_threshold(int threshold
, int num_children
, Error
**errp
)
833 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
834 "vote-threshold", "value >= 1");
838 if (threshold
> num_children
) {
839 error_setg(errp
, "threshold may not exceed children count");
846 static QemuOptsList quorum_runtime_opts
= {
848 .head
= QTAILQ_HEAD_INITIALIZER(quorum_runtime_opts
.head
),
851 .name
= QUORUM_OPT_VOTE_THRESHOLD
,
852 .type
= QEMU_OPT_NUMBER
,
853 .help
= "The number of vote needed for reaching quorum",
856 .name
= QUORUM_OPT_BLKVERIFY
,
857 .type
= QEMU_OPT_BOOL
,
858 .help
= "Trigger block verify mode if set",
861 .name
= QUORUM_OPT_REWRITE
,
862 .type
= QEMU_OPT_BOOL
,
863 .help
= "Rewrite corrupted block on read quorum",
866 .name
= QUORUM_OPT_READ_PATTERN
,
867 .type
= QEMU_OPT_STRING
,
868 .help
= "Allowed pattern: quorum, fifo. Quorum is default",
870 { /* end of list */ }
874 static int quorum_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
877 BDRVQuorumState
*s
= bs
->opaque
;
878 Error
*local_err
= NULL
;
879 QemuOpts
*opts
= NULL
;
880 const char *pattern_str
;
885 qdict_flatten(options
);
887 /* count how many different children are present */
888 s
->num_children
= qdict_array_entries(options
, "children.");
889 if (s
->num_children
< 0) {
890 error_setg(&local_err
, "Option children is not a valid array");
894 if (s
->num_children
< 1) {
895 error_setg(&local_err
,
896 "Number of provided children must be 1 or more");
901 opts
= qemu_opts_create(&quorum_runtime_opts
, NULL
, 0, &error_abort
);
902 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
908 s
->threshold
= qemu_opt_get_number(opts
, QUORUM_OPT_VOTE_THRESHOLD
, 0);
909 /* and validate it against s->num_children */
910 ret
= quorum_valid_threshold(s
->threshold
, s
->num_children
, &local_err
);
915 pattern_str
= qemu_opt_get(opts
, QUORUM_OPT_READ_PATTERN
);
917 ret
= QUORUM_READ_PATTERN_QUORUM
;
919 ret
= qapi_enum_parse(&QuorumReadPattern_lookup
, pattern_str
,
923 error_setg(&local_err
, "Please set read-pattern as fifo or quorum");
926 s
->read_pattern
= ret
;
928 if (s
->read_pattern
== QUORUM_READ_PATTERN_QUORUM
) {
929 /* is the driver in blkverify mode */
930 if (qemu_opt_get_bool(opts
, QUORUM_OPT_BLKVERIFY
, false) &&
931 s
->num_children
== 2 && s
->threshold
== 2) {
932 s
->is_blkverify
= true;
933 } else if (qemu_opt_get_bool(opts
, QUORUM_OPT_BLKVERIFY
, false)) {
934 fprintf(stderr
, "blkverify mode is set by setting blkverify=on "
935 "and using two files with vote_threshold=2\n");
938 s
->rewrite_corrupted
= qemu_opt_get_bool(opts
, QUORUM_OPT_REWRITE
,
940 if (s
->rewrite_corrupted
&& s
->is_blkverify
) {
941 error_setg(&local_err
,
942 "rewrite-corrupted=on cannot be used with blkverify=on");
948 /* allocate the children array */
949 s
->children
= g_new0(BdrvChild
*, s
->num_children
);
950 opened
= g_new0(bool, s
->num_children
);
952 for (i
= 0; i
< s
->num_children
; i
++) {
954 ret
= snprintf(indexstr
, 32, "children.%d", i
);
957 s
->children
[i
] = bdrv_open_child(NULL
, options
, indexstr
, bs
,
958 &child_format
, false, &local_err
);
966 s
->next_child_index
= s
->num_children
;
968 bs
->supported_write_flags
= BDRV_REQ_WRITE_UNCHANGED
;
974 /* cleanup on error */
975 for (i
= 0; i
< s
->num_children
; i
++) {
979 bdrv_unref_child(bs
, s
->children
[i
]);
985 /* propagate error */
986 error_propagate(errp
, local_err
);
990 static void quorum_close(BlockDriverState
*bs
)
992 BDRVQuorumState
*s
= bs
->opaque
;
995 for (i
= 0; i
< s
->num_children
; i
++) {
996 bdrv_unref_child(bs
, s
->children
[i
]);
1002 static void quorum_add_child(BlockDriverState
*bs
, BlockDriverState
*child_bs
,
1005 BDRVQuorumState
*s
= bs
->opaque
;
1010 assert(s
->num_children
<= INT_MAX
/ sizeof(BdrvChild
*));
1011 if (s
->num_children
== INT_MAX
/ sizeof(BdrvChild
*) ||
1012 s
->next_child_index
== UINT_MAX
) {
1013 error_setg(errp
, "Too many children");
1017 ret
= snprintf(indexstr
, 32, "children.%u", s
->next_child_index
);
1018 if (ret
< 0 || ret
>= 32) {
1019 error_setg(errp
, "cannot generate child name");
1022 s
->next_child_index
++;
1024 bdrv_drained_begin(bs
);
1026 /* We can safely add the child now */
1029 child
= bdrv_attach_child(bs
, child_bs
, indexstr
, &child_format
, errp
);
1030 if (child
== NULL
) {
1031 s
->next_child_index
--;
1032 bdrv_unref(child_bs
);
1035 s
->children
= g_renew(BdrvChild
*, s
->children
, s
->num_children
+ 1);
1036 s
->children
[s
->num_children
++] = child
;
1039 bdrv_drained_end(bs
);
1042 static void quorum_del_child(BlockDriverState
*bs
, BdrvChild
*child
,
1045 BDRVQuorumState
*s
= bs
->opaque
;
1048 for (i
= 0; i
< s
->num_children
; i
++) {
1049 if (s
->children
[i
] == child
) {
1054 /* we have checked it in bdrv_del_child() */
1055 assert(i
< s
->num_children
);
1057 if (s
->num_children
<= s
->threshold
) {
1059 "The number of children cannot be lower than the vote threshold %d",
1064 bdrv_drained_begin(bs
);
1066 /* We can safely remove this child now */
1067 memmove(&s
->children
[i
], &s
->children
[i
+ 1],
1068 (s
->num_children
- i
- 1) * sizeof(BdrvChild
*));
1069 s
->children
= g_renew(BdrvChild
*, s
->children
, --s
->num_children
);
1070 bdrv_unref_child(bs
, child
);
1072 bdrv_drained_end(bs
);
1075 static void quorum_refresh_filename(BlockDriverState
*bs
, QDict
*options
)
1077 BDRVQuorumState
*s
= bs
->opaque
;
1082 for (i
= 0; i
< s
->num_children
; i
++) {
1083 bdrv_refresh_filename(s
->children
[i
]->bs
);
1084 if (!s
->children
[i
]->bs
->full_open_options
) {
1089 children
= qlist_new();
1090 for (i
= 0; i
< s
->num_children
; i
++) {
1091 qlist_append(children
,
1092 qobject_ref(s
->children
[i
]->bs
->full_open_options
));
1096 qdict_put_str(opts
, "driver", "quorum");
1097 qdict_put_int(opts
, QUORUM_OPT_VOTE_THRESHOLD
, s
->threshold
);
1098 qdict_put_bool(opts
, QUORUM_OPT_BLKVERIFY
, s
->is_blkverify
);
1099 qdict_put_bool(opts
, QUORUM_OPT_REWRITE
, s
->rewrite_corrupted
);
1100 qdict_put(opts
, "children", children
);
1102 bs
->full_open_options
= opts
;
1105 static BlockDriver bdrv_quorum
= {
1106 .format_name
= "quorum",
1108 .instance_size
= sizeof(BDRVQuorumState
),
1110 .bdrv_open
= quorum_open
,
1111 .bdrv_close
= quorum_close
,
1112 .bdrv_refresh_filename
= quorum_refresh_filename
,
1114 .bdrv_co_flush_to_disk
= quorum_co_flush
,
1116 .bdrv_getlength
= quorum_getlength
,
1118 .bdrv_co_preadv
= quorum_co_preadv
,
1119 .bdrv_co_pwritev
= quorum_co_pwritev
,
1121 .bdrv_add_child
= quorum_add_child
,
1122 .bdrv_del_child
= quorum_del_child
,
1124 .bdrv_child_perm
= bdrv_filter_default_perms
,
1127 .bdrv_recurse_is_first_non_filter
= quorum_recurse_is_first_non_filter
,
1130 static void bdrv_quorum_init(void)
1132 if (!qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA256
)) {
1133 /* SHA256 hash support is required for quorum device */
1136 bdrv_register(&bdrv_quorum
);
1139 block_init(bdrv_quorum_init
);