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
);
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
);
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 bool quorum_compare(QuorumAIOCB
*acb
, QEMUIOVector
*a
, QEMUIOVector
*b
)
442 BDRVQuorumState
*s
= acb
->bs
->opaque
;
445 /* This driver will replace blkverify in this particular case */
446 if (s
->is_blkverify
) {
447 offset
= qemu_iovec_compare(a
, b
);
449 fprintf(stderr
, "quorum: offset=%" PRIu64
" bytes=%" PRIu64
450 " contents mismatch at offset %" PRIu64
"\n",
451 acb
->offset
, acb
->bytes
, acb
->offset
+ offset
);
457 return quorum_iovec_compare(a
, b
);
460 /* Do a vote to get the error code */
461 static int quorum_vote_error(QuorumAIOCB
*acb
)
463 BDRVQuorumState
*s
= acb
->bs
->opaque
;
464 QuorumVoteVersion
*winner
= NULL
;
465 QuorumVotes error_votes
;
466 QuorumVoteValue result_value
;
470 QLIST_INIT(&error_votes
.vote_list
);
471 error_votes
.compare
= quorum_64bits_compare
;
473 for (i
= 0; i
< s
->num_children
; i
++) {
474 ret
= acb
->qcrs
[i
].ret
;
477 result_value
.l
= ret
;
478 quorum_count_vote(&error_votes
, &result_value
, i
);
483 winner
= quorum_get_vote_winner(&error_votes
);
484 ret
= winner
->value
.l
;
487 quorum_free_vote_list(&error_votes
);
492 static void quorum_vote(QuorumAIOCB
*acb
)
496 QuorumVoteValue hash
;
497 BDRVQuorumState
*s
= acb
->bs
->opaque
;
498 QuorumVoteVersion
*winner
;
500 if (quorum_has_too_much_io_failed(acb
)) {
504 /* get the index of the first successful read */
505 for (i
= 0; i
< s
->num_children
; i
++) {
506 if (!acb
->qcrs
[i
].ret
) {
511 assert(i
< s
->num_children
);
513 /* compare this read with all other successful reads stopping at quorum
516 for (j
= i
+ 1; j
< s
->num_children
; j
++) {
517 if (acb
->qcrs
[j
].ret
) {
520 quorum
= quorum_compare(acb
, &acb
->qcrs
[i
].qiov
, &acb
->qcrs
[j
].qiov
);
526 /* Every successful read agrees */
528 quorum_copy_qiov(acb
->qiov
, &acb
->qcrs
[i
].qiov
);
532 /* compute hashes for each successful read, also store indexes */
533 for (i
= 0; i
< s
->num_children
; i
++) {
534 if (acb
->qcrs
[i
].ret
) {
537 ret
= quorum_compute_hash(acb
, i
, &hash
);
538 /* if ever the hash computation failed */
543 quorum_count_vote(&acb
->votes
, &hash
, i
);
546 /* vote to select the most represented version */
547 winner
= quorum_get_vote_winner(&acb
->votes
);
549 /* if the winner count is smaller than threshold the read fails */
550 if (winner
->vote_count
< s
->threshold
) {
551 quorum_report_failure(acb
);
552 acb
->vote_ret
= -EIO
;
556 /* we have a winner: copy it */
557 quorum_copy_qiov(acb
->qiov
, &acb
->qcrs
[winner
->index
].qiov
);
559 /* some versions are bad print them */
560 quorum_report_bad_versions(s
, acb
, &winner
->value
);
562 /* corruption correction is enabled */
563 if (s
->rewrite_corrupted
) {
564 quorum_rewrite_bad_versions(acb
, &winner
->value
);
569 quorum_free_vote_list(&acb
->votes
);
572 static void read_quorum_children_entry(void *opaque
)
574 QuorumCo
*co
= opaque
;
575 QuorumAIOCB
*acb
= co
->acb
;
576 BDRVQuorumState
*s
= acb
->bs
->opaque
;
578 QuorumChildRequest
*sacb
= &acb
->qcrs
[i
];
580 sacb
->bs
= s
->children
[i
]->bs
;
581 sacb
->ret
= bdrv_co_preadv(s
->children
[i
], acb
->offset
, acb
->bytes
,
582 &acb
->qcrs
[i
].qiov
, 0);
584 if (sacb
->ret
== 0) {
585 acb
->success_count
++;
587 quorum_report_bad_acb(sacb
, sacb
->ret
);
591 assert(acb
->count
<= s
->num_children
);
592 assert(acb
->success_count
<= s
->num_children
);
594 /* Wake up the caller after the last read */
595 if (acb
->count
== s
->num_children
) {
596 qemu_coroutine_enter_if_inactive(acb
->co
);
600 static int read_quorum_children(QuorumAIOCB
*acb
)
602 BDRVQuorumState
*s
= acb
->bs
->opaque
;
605 acb
->children_read
= s
->num_children
;
606 for (i
= 0; i
< s
->num_children
; i
++) {
607 acb
->qcrs
[i
].buf
= qemu_blockalign(s
->children
[i
]->bs
, acb
->qiov
->size
);
608 qemu_iovec_init(&acb
->qcrs
[i
].qiov
, acb
->qiov
->niov
);
609 qemu_iovec_clone(&acb
->qcrs
[i
].qiov
, acb
->qiov
, acb
->qcrs
[i
].buf
);
612 for (i
= 0; i
< s
->num_children
; i
++) {
619 co
= qemu_coroutine_create(read_quorum_children_entry
, &data
);
620 qemu_coroutine_enter(co
);
623 while (acb
->count
< s
->num_children
) {
624 qemu_coroutine_yield();
627 /* Do the vote on read */
629 for (i
= 0; i
< s
->num_children
; i
++) {
630 qemu_vfree(acb
->qcrs
[i
].buf
);
631 qemu_iovec_destroy(&acb
->qcrs
[i
].qiov
);
634 while (acb
->rewrite_count
) {
635 qemu_coroutine_yield();
638 return acb
->vote_ret
;
641 static int read_fifo_child(QuorumAIOCB
*acb
)
643 BDRVQuorumState
*s
= acb
->bs
->opaque
;
646 /* We try to read the next child in FIFO order if we failed to read */
648 n
= acb
->children_read
++;
649 acb
->qcrs
[n
].bs
= s
->children
[n
]->bs
;
650 ret
= bdrv_co_preadv(s
->children
[n
], acb
->offset
, acb
->bytes
,
653 quorum_report_bad_acb(&acb
->qcrs
[n
], ret
);
655 } while (ret
< 0 && acb
->children_read
< s
->num_children
);
657 /* FIXME: rewrite failed children if acb->children_read > 1? */
662 static int quorum_co_preadv(BlockDriverState
*bs
, uint64_t offset
,
663 uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
665 BDRVQuorumState
*s
= bs
->opaque
;
666 QuorumAIOCB
*acb
= quorum_aio_get(bs
, qiov
, offset
, bytes
, flags
);
670 acb
->children_read
= 0;
672 if (s
->read_pattern
== QUORUM_READ_PATTERN_QUORUM
) {
673 ret
= read_quorum_children(acb
);
675 ret
= read_fifo_child(acb
);
677 quorum_aio_finalize(acb
);
682 static void write_quorum_entry(void *opaque
)
684 QuorumCo
*co
= opaque
;
685 QuorumAIOCB
*acb
= co
->acb
;
686 BDRVQuorumState
*s
= acb
->bs
->opaque
;
688 QuorumChildRequest
*sacb
= &acb
->qcrs
[i
];
690 sacb
->bs
= s
->children
[i
]->bs
;
691 sacb
->ret
= bdrv_co_pwritev(s
->children
[i
], acb
->offset
, acb
->bytes
,
692 acb
->qiov
, acb
->flags
);
693 if (sacb
->ret
== 0) {
694 acb
->success_count
++;
696 quorum_report_bad_acb(sacb
, sacb
->ret
);
699 assert(acb
->count
<= s
->num_children
);
700 assert(acb
->success_count
<= s
->num_children
);
702 /* Wake up the caller after the last write */
703 if (acb
->count
== s
->num_children
) {
704 qemu_coroutine_enter_if_inactive(acb
->co
);
708 static int quorum_co_pwritev(BlockDriverState
*bs
, uint64_t offset
,
709 uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
711 BDRVQuorumState
*s
= bs
->opaque
;
712 QuorumAIOCB
*acb
= quorum_aio_get(bs
, qiov
, offset
, bytes
, flags
);
715 for (i
= 0; i
< s
->num_children
; i
++) {
722 co
= qemu_coroutine_create(write_quorum_entry
, &data
);
723 qemu_coroutine_enter(co
);
726 while (acb
->count
< s
->num_children
) {
727 qemu_coroutine_yield();
730 quorum_has_too_much_io_failed(acb
);
733 quorum_aio_finalize(acb
);
738 static int64_t quorum_getlength(BlockDriverState
*bs
)
740 BDRVQuorumState
*s
= bs
->opaque
;
744 /* check that all file have the same length */
745 result
= bdrv_getlength(s
->children
[0]->bs
);
749 for (i
= 1; i
< s
->num_children
; i
++) {
750 int64_t value
= bdrv_getlength(s
->children
[i
]->bs
);
754 if (value
!= result
) {
762 static coroutine_fn
int quorum_co_flush(BlockDriverState
*bs
)
764 BDRVQuorumState
*s
= bs
->opaque
;
765 QuorumVoteVersion
*winner
= NULL
;
766 QuorumVotes error_votes
;
767 QuorumVoteValue result_value
;
770 int success_count
= 0;
772 QLIST_INIT(&error_votes
.vote_list
);
773 error_votes
.compare
= quorum_64bits_compare
;
775 for (i
= 0; i
< s
->num_children
; i
++) {
776 result
= bdrv_co_flush(s
->children
[i
]->bs
);
778 quorum_report_bad(QUORUM_OP_TYPE_FLUSH
, 0, 0,
779 s
->children
[i
]->bs
->node_name
, result
);
780 result_value
.l
= result
;
781 quorum_count_vote(&error_votes
, &result_value
, i
);
787 if (success_count
>= s
->threshold
) {
790 winner
= quorum_get_vote_winner(&error_votes
);
791 result
= winner
->value
.l
;
793 quorum_free_vote_list(&error_votes
);
798 static bool quorum_recurse_is_first_non_filter(BlockDriverState
*bs
,
799 BlockDriverState
*candidate
)
801 BDRVQuorumState
*s
= bs
->opaque
;
804 for (i
= 0; i
< s
->num_children
; i
++) {
805 bool perm
= bdrv_recurse_is_first_non_filter(s
->children
[i
]->bs
,
815 static int quorum_valid_threshold(int threshold
, int num_children
, Error
**errp
)
819 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
820 "vote-threshold", "value >= 1");
824 if (threshold
> num_children
) {
825 error_setg(errp
, "threshold may not exceed children count");
832 static QemuOptsList quorum_runtime_opts
= {
834 .head
= QTAILQ_HEAD_INITIALIZER(quorum_runtime_opts
.head
),
837 .name
= QUORUM_OPT_VOTE_THRESHOLD
,
838 .type
= QEMU_OPT_NUMBER
,
839 .help
= "The number of vote needed for reaching quorum",
842 .name
= QUORUM_OPT_BLKVERIFY
,
843 .type
= QEMU_OPT_BOOL
,
844 .help
= "Trigger block verify mode if set",
847 .name
= QUORUM_OPT_REWRITE
,
848 .type
= QEMU_OPT_BOOL
,
849 .help
= "Rewrite corrupted block on read quorum",
852 .name
= QUORUM_OPT_READ_PATTERN
,
853 .type
= QEMU_OPT_STRING
,
854 .help
= "Allowed pattern: quorum, fifo. Quorum is default",
856 { /* end of list */ }
860 static int quorum_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
863 BDRVQuorumState
*s
= bs
->opaque
;
864 Error
*local_err
= NULL
;
865 QemuOpts
*opts
= NULL
;
866 const char *pattern_str
;
871 qdict_flatten(options
);
873 /* count how many different children are present */
874 s
->num_children
= qdict_array_entries(options
, "children.");
875 if (s
->num_children
< 0) {
876 error_setg(&local_err
, "Option children is not a valid array");
880 if (s
->num_children
< 1) {
881 error_setg(&local_err
,
882 "Number of provided children must be 1 or more");
887 opts
= qemu_opts_create(&quorum_runtime_opts
, NULL
, 0, &error_abort
);
888 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
894 s
->threshold
= qemu_opt_get_number(opts
, QUORUM_OPT_VOTE_THRESHOLD
, 0);
895 /* and validate it against s->num_children */
896 ret
= quorum_valid_threshold(s
->threshold
, s
->num_children
, &local_err
);
901 pattern_str
= qemu_opt_get(opts
, QUORUM_OPT_READ_PATTERN
);
903 ret
= QUORUM_READ_PATTERN_QUORUM
;
905 ret
= qapi_enum_parse(&QuorumReadPattern_lookup
, pattern_str
,
909 error_setg(&local_err
, "Please set read-pattern as fifo or quorum");
912 s
->read_pattern
= ret
;
914 if (s
->read_pattern
== QUORUM_READ_PATTERN_QUORUM
) {
915 s
->is_blkverify
= qemu_opt_get_bool(opts
, QUORUM_OPT_BLKVERIFY
, false);
916 if (s
->is_blkverify
&& (s
->num_children
!= 2 || s
->threshold
!= 2)) {
917 error_setg(&local_err
, "blkverify=on can only be set if there are "
918 "exactly two files and vote-threshold is 2");
923 s
->rewrite_corrupted
= qemu_opt_get_bool(opts
, QUORUM_OPT_REWRITE
,
925 if (s
->rewrite_corrupted
&& s
->is_blkverify
) {
926 error_setg(&local_err
,
927 "rewrite-corrupted=on cannot be used with blkverify=on");
933 /* allocate the children array */
934 s
->children
= g_new0(BdrvChild
*, s
->num_children
);
935 opened
= g_new0(bool, s
->num_children
);
937 for (i
= 0; i
< s
->num_children
; i
++) {
939 ret
= snprintf(indexstr
, 32, "children.%d", i
);
942 s
->children
[i
] = bdrv_open_child(NULL
, options
, indexstr
, bs
,
943 &child_format
, false, &local_err
);
951 s
->next_child_index
= s
->num_children
;
953 bs
->supported_write_flags
= BDRV_REQ_WRITE_UNCHANGED
;
959 /* cleanup on error */
960 for (i
= 0; i
< s
->num_children
; i
++) {
964 bdrv_unref_child(bs
, s
->children
[i
]);
970 /* propagate error */
971 error_propagate(errp
, local_err
);
975 static void quorum_close(BlockDriverState
*bs
)
977 BDRVQuorumState
*s
= bs
->opaque
;
980 for (i
= 0; i
< s
->num_children
; i
++) {
981 bdrv_unref_child(bs
, s
->children
[i
]);
987 static void quorum_add_child(BlockDriverState
*bs
, BlockDriverState
*child_bs
,
990 BDRVQuorumState
*s
= bs
->opaque
;
995 if (s
->is_blkverify
) {
996 error_setg(errp
, "Cannot add a child to a quorum in blkverify mode");
1000 assert(s
->num_children
<= INT_MAX
/ sizeof(BdrvChild
*));
1001 if (s
->num_children
== INT_MAX
/ sizeof(BdrvChild
*) ||
1002 s
->next_child_index
== UINT_MAX
) {
1003 error_setg(errp
, "Too many children");
1007 ret
= snprintf(indexstr
, 32, "children.%u", s
->next_child_index
);
1008 if (ret
< 0 || ret
>= 32) {
1009 error_setg(errp
, "cannot generate child name");
1012 s
->next_child_index
++;
1014 bdrv_drained_begin(bs
);
1016 /* We can safely add the child now */
1019 child
= bdrv_attach_child(bs
, child_bs
, indexstr
, &child_format
, errp
);
1020 if (child
== NULL
) {
1021 s
->next_child_index
--;
1022 bdrv_unref(child_bs
);
1025 s
->children
= g_renew(BdrvChild
*, s
->children
, s
->num_children
+ 1);
1026 s
->children
[s
->num_children
++] = child
;
1029 bdrv_drained_end(bs
);
1032 static void quorum_del_child(BlockDriverState
*bs
, BdrvChild
*child
,
1035 BDRVQuorumState
*s
= bs
->opaque
;
1038 for (i
= 0; i
< s
->num_children
; i
++) {
1039 if (s
->children
[i
] == child
) {
1044 /* we have checked it in bdrv_del_child() */
1045 assert(i
< s
->num_children
);
1047 if (s
->num_children
<= s
->threshold
) {
1049 "The number of children cannot be lower than the vote threshold %d",
1054 /* We know now that num_children > threshold, so blkverify must be false */
1055 assert(!s
->is_blkverify
);
1057 bdrv_drained_begin(bs
);
1059 /* We can safely remove this child now */
1060 memmove(&s
->children
[i
], &s
->children
[i
+ 1],
1061 (s
->num_children
- i
- 1) * sizeof(BdrvChild
*));
1062 s
->children
= g_renew(BdrvChild
*, s
->children
, --s
->num_children
);
1063 bdrv_unref_child(bs
, child
);
1065 bdrv_drained_end(bs
);
1068 static void quorum_gather_child_options(BlockDriverState
*bs
, QDict
*target
,
1069 bool backing_overridden
)
1071 BDRVQuorumState
*s
= bs
->opaque
;
1072 QList
*children_list
;
1076 * The generic implementation for gathering child options in
1077 * bdrv_refresh_filename() would use the names of the children
1078 * as specified for bdrv_open_child() or bdrv_attach_child(),
1079 * which is "children.%u" with %u being a value
1080 * (s->next_child_index) that is incremented each time a new child
1081 * is added (and never decremented). Since children can be
1082 * deleted at runtime, there may be gaps in that enumeration.
1083 * When creating a new quorum BDS and specifying the children for
1084 * it through runtime options, the enumeration used there may not
1085 * have any gaps, though.
1087 * Therefore, we have to create a new gap-less enumeration here
1088 * (which we can achieve by simply putting all of the children's
1089 * full_open_options into a QList).
1091 * XXX: Note that there are issues with the current child option
1092 * structure quorum uses (such as the fact that children do
1093 * not really have unique permanent names). Therefore, this
1094 * is going to have to change in the future and ideally we
1095 * want quorum to be covered by the generic implementation.
1098 children_list
= qlist_new();
1099 qdict_put(target
, "children", children_list
);
1101 for (i
= 0; i
< s
->num_children
; i
++) {
1102 qlist_append(children_list
,
1103 qobject_ref(s
->children
[i
]->bs
->full_open_options
));
1107 static char *quorum_dirname(BlockDriverState
*bs
, Error
**errp
)
1109 /* In general, there are multiple BDSs with different dirnames below this
1110 * one; so there is no unique dirname we could return (unless all are equal
1111 * by chance, or there is only one). Therefore, to be consistent, just
1112 * always return NULL. */
1113 error_setg(errp
, "Cannot generate a base directory for quorum nodes");
1117 static const char *const quorum_strong_runtime_opts
[] = {
1118 QUORUM_OPT_VOTE_THRESHOLD
,
1119 QUORUM_OPT_BLKVERIFY
,
1121 QUORUM_OPT_READ_PATTERN
,
1126 static BlockDriver bdrv_quorum
= {
1127 .format_name
= "quorum",
1129 .instance_size
= sizeof(BDRVQuorumState
),
1131 .bdrv_open
= quorum_open
,
1132 .bdrv_close
= quorum_close
,
1133 .bdrv_gather_child_options
= quorum_gather_child_options
,
1134 .bdrv_dirname
= quorum_dirname
,
1136 .bdrv_co_flush_to_disk
= quorum_co_flush
,
1138 .bdrv_getlength
= quorum_getlength
,
1140 .bdrv_co_preadv
= quorum_co_preadv
,
1141 .bdrv_co_pwritev
= quorum_co_pwritev
,
1143 .bdrv_add_child
= quorum_add_child
,
1144 .bdrv_del_child
= quorum_del_child
,
1146 .bdrv_child_perm
= bdrv_filter_default_perms
,
1149 .bdrv_recurse_is_first_non_filter
= quorum_recurse_is_first_non_filter
,
1151 .strong_runtime_opts
= quorum_strong_runtime_opts
,
1154 static void bdrv_quorum_init(void)
1156 if (!qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA256
)) {
1157 /* SHA256 hash support is required for quorum device */
1160 bdrv_register(&bdrv_quorum
);
1163 block_init(bdrv_quorum_init
);