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/module.h"
19 #include "qemu/option.h"
20 #include "block/block_int.h"
21 #include "block/qdict.h"
22 #include "qapi/error.h"
23 #include "qapi/qapi-events-block.h"
24 #include "qapi/qmp/qdict.h"
25 #include "qapi/qmp/qerror.h"
26 #include "qapi/qmp/qlist.h"
27 #include "qapi/qmp/qstring.h"
28 #include "crypto/hash.h"
30 #define HASH_LENGTH 32
32 #define QUORUM_OPT_VOTE_THRESHOLD "vote-threshold"
33 #define QUORUM_OPT_BLKVERIFY "blkverify"
34 #define QUORUM_OPT_REWRITE "rewrite-corrupted"
35 #define QUORUM_OPT_READ_PATTERN "read-pattern"
37 /* This union holds a vote hash value */
38 typedef union QuorumVoteValue
{
39 uint8_t h
[HASH_LENGTH
]; /* SHA-256 hash */
40 int64_t l
; /* simpler 64 bits hash */
44 typedef struct QuorumVoteItem
{
46 QLIST_ENTRY(QuorumVoteItem
) next
;
49 /* this structure is a vote version. A version is the set of votes sharing the
51 * The set of votes will be tracked with the items field and its cardinality is
54 typedef struct QuorumVoteVersion
{
55 QuorumVoteValue value
;
58 QLIST_HEAD(, QuorumVoteItem
) items
;
59 QLIST_ENTRY(QuorumVoteVersion
) next
;
62 /* this structure holds a group of vote versions together */
63 typedef struct QuorumVotes
{
64 QLIST_HEAD(, QuorumVoteVersion
) vote_list
;
65 bool (*compare
)(QuorumVoteValue
*a
, QuorumVoteValue
*b
);
68 /* the following structure holds the state of one quorum instance */
69 typedef struct BDRVQuorumState
{
70 BdrvChild
**children
; /* children BlockDriverStates */
71 int num_children
; /* children count */
72 unsigned next_child_index
; /* the index of the next child that should
75 int threshold
; /* if less than threshold children reads gave the
76 * same result a quorum error occurs.
78 bool is_blkverify
; /* true if the driver is in blkverify mode
79 * Writes are mirrored on two children devices.
80 * On reads the two children devices' contents are
81 * compared and if a difference is spotted its
82 * location is printed and the code aborts.
83 * It is useful to debug other block drivers by
84 * comparing them with a reference one.
86 bool rewrite_corrupted
;/* true if the driver must rewrite-on-read corrupted
87 * block if Quorum is reached.
90 QuorumReadPattern read_pattern
;
93 typedef struct QuorumAIOCB QuorumAIOCB
;
95 /* Quorum will create one instance of the following structure per operation it
96 * performs on its children.
97 * So for each read/write operation coming from the upper layer there will be
98 * $children_count QuorumChildRequest.
100 typedef struct QuorumChildRequest
{
101 BlockDriverState
*bs
;
106 } QuorumChildRequest
;
108 /* Quorum will use the following structure to track progress of each read/write
109 * operation received by the upper layer.
110 * This structure hold pointers to the QuorumChildRequest structures instances
111 * used to do operations on each children and track overall progress.
114 BlockDriverState
*bs
;
117 /* Request metadata */
122 QEMUIOVector
*qiov
; /* calling IOV */
124 QuorumChildRequest
*qcrs
; /* individual child requests */
125 int count
; /* number of completed AIOCB */
126 int success_count
; /* number of successfully completed AIOCB */
128 int rewrite_count
; /* number of replica to rewrite: count down to
129 * zero once writes are fired
136 int children_read
; /* how many children have been read from */
139 typedef struct QuorumCo
{
144 static void quorum_aio_finalize(QuorumAIOCB
*acb
)
150 static bool quorum_sha256_compare(QuorumVoteValue
*a
, QuorumVoteValue
*b
)
152 return !memcmp(a
->h
, b
->h
, HASH_LENGTH
);
155 static bool quorum_64bits_compare(QuorumVoteValue
*a
, QuorumVoteValue
*b
)
160 static QuorumAIOCB
*quorum_aio_get(BlockDriverState
*bs
,
166 BDRVQuorumState
*s
= bs
->opaque
;
167 QuorumAIOCB
*acb
= g_new(QuorumAIOCB
, 1);
170 *acb
= (QuorumAIOCB
) {
171 .co
= qemu_coroutine_self(),
177 .votes
.compare
= quorum_sha256_compare
,
178 .votes
.vote_list
= QLIST_HEAD_INITIALIZER(acb
.votes
.vote_list
),
181 acb
->qcrs
= g_new0(QuorumChildRequest
, s
->num_children
);
182 for (i
= 0; i
< s
->num_children
; i
++) {
183 acb
->qcrs
[i
].buf
= NULL
;
184 acb
->qcrs
[i
].ret
= 0;
185 acb
->qcrs
[i
].parent
= acb
;
191 static void quorum_report_bad(QuorumOpType type
, uint64_t offset
,
192 uint64_t bytes
, char *node_name
, int ret
)
194 const char *msg
= NULL
;
195 int64_t start_sector
= offset
/ BDRV_SECTOR_SIZE
;
196 int64_t end_sector
= DIV_ROUND_UP(offset
+ bytes
, BDRV_SECTOR_SIZE
);
199 msg
= strerror(-ret
);
202 qapi_event_send_quorum_report_bad(type
, !!msg
, msg
, node_name
, start_sector
,
203 end_sector
- start_sector
);
206 static void quorum_report_failure(QuorumAIOCB
*acb
)
208 const char *reference
= bdrv_get_device_or_node_name(acb
->bs
);
209 int64_t start_sector
= acb
->offset
/ BDRV_SECTOR_SIZE
;
210 int64_t end_sector
= DIV_ROUND_UP(acb
->offset
+ acb
->bytes
,
213 qapi_event_send_quorum_failure(reference
, start_sector
,
214 end_sector
- start_sector
);
217 static int quorum_vote_error(QuorumAIOCB
*acb
);
219 static bool quorum_has_too_much_io_failed(QuorumAIOCB
*acb
)
221 BDRVQuorumState
*s
= acb
->bs
->opaque
;
223 if (acb
->success_count
< s
->threshold
) {
224 acb
->vote_ret
= quorum_vote_error(acb
);
225 quorum_report_failure(acb
);
232 static int read_fifo_child(QuorumAIOCB
*acb
);
234 static void quorum_copy_qiov(QEMUIOVector
*dest
, QEMUIOVector
*source
)
237 assert(dest
->niov
== source
->niov
);
238 assert(dest
->size
== source
->size
);
239 for (i
= 0; i
< source
->niov
; i
++) {
240 assert(dest
->iov
[i
].iov_len
== source
->iov
[i
].iov_len
);
241 memcpy(dest
->iov
[i
].iov_base
,
242 source
->iov
[i
].iov_base
,
243 source
->iov
[i
].iov_len
);
247 static void quorum_report_bad_acb(QuorumChildRequest
*sacb
, int ret
)
249 QuorumAIOCB
*acb
= sacb
->parent
;
250 QuorumOpType type
= acb
->is_read
? QUORUM_OP_TYPE_READ
: QUORUM_OP_TYPE_WRITE
;
251 quorum_report_bad(type
, acb
->offset
, acb
->bytes
, sacb
->bs
->node_name
, ret
);
254 static void quorum_report_bad_versions(BDRVQuorumState
*s
,
256 QuorumVoteValue
*value
)
258 QuorumVoteVersion
*version
;
259 QuorumVoteItem
*item
;
261 QLIST_FOREACH(version
, &acb
->votes
.vote_list
, next
) {
262 if (acb
->votes
.compare(&version
->value
, value
)) {
265 QLIST_FOREACH(item
, &version
->items
, next
) {
266 quorum_report_bad(QUORUM_OP_TYPE_READ
, acb
->offset
, acb
->bytes
,
267 s
->children
[item
->index
]->bs
->node_name
, 0);
272 static void quorum_rewrite_entry(void *opaque
)
274 QuorumCo
*co
= opaque
;
275 QuorumAIOCB
*acb
= co
->acb
;
276 BDRVQuorumState
*s
= acb
->bs
->opaque
;
278 /* Ignore any errors, it's just a correction attempt for already
280 * Mask out BDRV_REQ_WRITE_UNCHANGED because this overwrites the
281 * area with different data from the other children. */
282 bdrv_co_pwritev(s
->children
[co
->idx
], acb
->offset
, acb
->bytes
,
283 acb
->qiov
, acb
->flags
& ~BDRV_REQ_WRITE_UNCHANGED
);
285 /* Wake up the caller after the last rewrite */
286 acb
->rewrite_count
--;
287 if (!acb
->rewrite_count
) {
288 qemu_coroutine_enter_if_inactive(acb
->co
);
292 static bool quorum_rewrite_bad_versions(QuorumAIOCB
*acb
,
293 QuorumVoteValue
*value
)
295 QuorumVoteVersion
*version
;
296 QuorumVoteItem
*item
;
299 /* first count the number of bad versions: done first to avoid concurrency
302 QLIST_FOREACH(version
, &acb
->votes
.vote_list
, next
) {
303 if (acb
->votes
.compare(&version
->value
, value
)) {
306 QLIST_FOREACH(item
, &version
->items
, next
) {
311 /* quorum_rewrite_entry will count down this to zero */
312 acb
->rewrite_count
= count
;
314 /* now fire the correcting rewrites */
315 QLIST_FOREACH(version
, &acb
->votes
.vote_list
, next
) {
316 if (acb
->votes
.compare(&version
->value
, value
)) {
319 QLIST_FOREACH(item
, &version
->items
, next
) {
326 co
= qemu_coroutine_create(quorum_rewrite_entry
, &data
);
327 qemu_coroutine_enter(co
);
331 /* return true if any rewrite is done else false */
335 static void quorum_count_vote(QuorumVotes
*votes
,
336 QuorumVoteValue
*value
,
339 QuorumVoteVersion
*v
= NULL
, *version
= NULL
;
340 QuorumVoteItem
*item
;
342 /* look if we have something with this hash */
343 QLIST_FOREACH(v
, &votes
->vote_list
, next
) {
344 if (votes
->compare(&v
->value
, value
)) {
350 /* It's a version not yet in the list add it */
352 version
= g_new0(QuorumVoteVersion
, 1);
353 QLIST_INIT(&version
->items
);
354 memcpy(&version
->value
, value
, sizeof(version
->value
));
355 version
->index
= index
;
356 version
->vote_count
= 0;
357 QLIST_INSERT_HEAD(&votes
->vote_list
, version
, next
);
360 version
->vote_count
++;
362 item
= g_new0(QuorumVoteItem
, 1);
364 QLIST_INSERT_HEAD(&version
->items
, item
, next
);
367 static void quorum_free_vote_list(QuorumVotes
*votes
)
369 QuorumVoteVersion
*version
, *next_version
;
370 QuorumVoteItem
*item
, *next_item
;
372 QLIST_FOREACH_SAFE(version
, &votes
->vote_list
, next
, next_version
) {
373 QLIST_REMOVE(version
, next
);
374 QLIST_FOREACH_SAFE(item
, &version
->items
, next
, next_item
) {
375 QLIST_REMOVE(item
, next
);
382 static int quorum_compute_hash(QuorumAIOCB
*acb
, int i
, QuorumVoteValue
*hash
)
384 QEMUIOVector
*qiov
= &acb
->qcrs
[i
].qiov
;
385 size_t len
= sizeof(hash
->h
);
386 uint8_t *data
= hash
->h
;
388 /* XXX - would be nice if we could pass in the Error **
389 * and propagate that back, but this quorum code is
390 * restricted to just errno values currently */
391 if (qcrypto_hash_bytesv(QCRYPTO_HASH_ALG_SHA256
,
392 qiov
->iov
, qiov
->niov
,
401 static QuorumVoteVersion
*quorum_get_vote_winner(QuorumVotes
*votes
)
404 QuorumVoteVersion
*candidate
, *winner
= NULL
;
406 QLIST_FOREACH(candidate
, &votes
->vote_list
, next
) {
407 if (candidate
->vote_count
> max
) {
408 max
= candidate
->vote_count
;
416 /* qemu_iovec_compare is handy for blkverify mode because it returns the first
417 * differing byte location. Yet it is handcoded to compare vectors one byte
418 * after another so it does not benefit from the libc SIMD optimizations.
419 * quorum_iovec_compare is written for speed and should be used in the non
420 * blkverify mode of quorum.
422 static bool quorum_iovec_compare(QEMUIOVector
*a
, QEMUIOVector
*b
)
427 assert(a
->niov
== b
->niov
);
428 for (i
= 0; i
< a
->niov
; i
++) {
429 assert(a
->iov
[i
].iov_len
== b
->iov
[i
].iov_len
);
430 result
= memcmp(a
->iov
[i
].iov_base
,
441 static bool quorum_compare(QuorumAIOCB
*acb
, QEMUIOVector
*a
, QEMUIOVector
*b
)
443 BDRVQuorumState
*s
= acb
->bs
->opaque
;
446 /* This driver will replace blkverify in this particular case */
447 if (s
->is_blkverify
) {
448 offset
= qemu_iovec_compare(a
, b
);
450 fprintf(stderr
, "quorum: offset=%" PRIu64
" bytes=%" PRIu64
451 " contents mismatch at offset %" PRIu64
"\n",
452 acb
->offset
, acb
->bytes
, acb
->offset
+ offset
);
458 return quorum_iovec_compare(a
, b
);
461 /* Do a vote to get the error code */
462 static int quorum_vote_error(QuorumAIOCB
*acb
)
464 BDRVQuorumState
*s
= acb
->bs
->opaque
;
465 QuorumVoteVersion
*winner
= NULL
;
466 QuorumVotes error_votes
;
467 QuorumVoteValue result_value
;
471 QLIST_INIT(&error_votes
.vote_list
);
472 error_votes
.compare
= quorum_64bits_compare
;
474 for (i
= 0; i
< s
->num_children
; i
++) {
475 ret
= acb
->qcrs
[i
].ret
;
478 result_value
.l
= ret
;
479 quorum_count_vote(&error_votes
, &result_value
, i
);
484 winner
= quorum_get_vote_winner(&error_votes
);
485 ret
= winner
->value
.l
;
488 quorum_free_vote_list(&error_votes
);
493 static void quorum_vote(QuorumAIOCB
*acb
)
497 QuorumVoteValue hash
;
498 BDRVQuorumState
*s
= acb
->bs
->opaque
;
499 QuorumVoteVersion
*winner
;
501 if (quorum_has_too_much_io_failed(acb
)) {
505 /* get the index of the first successful read */
506 for (i
= 0; i
< s
->num_children
; i
++) {
507 if (!acb
->qcrs
[i
].ret
) {
512 assert(i
< s
->num_children
);
514 /* compare this read with all other successful reads stopping at quorum
517 for (j
= i
+ 1; j
< s
->num_children
; j
++) {
518 if (acb
->qcrs
[j
].ret
) {
521 quorum
= quorum_compare(acb
, &acb
->qcrs
[i
].qiov
, &acb
->qcrs
[j
].qiov
);
527 /* Every successful read agrees */
529 quorum_copy_qiov(acb
->qiov
, &acb
->qcrs
[i
].qiov
);
533 /* compute hashes for each successful read, also store indexes */
534 for (i
= 0; i
< s
->num_children
; i
++) {
535 if (acb
->qcrs
[i
].ret
) {
538 ret
= quorum_compute_hash(acb
, i
, &hash
);
539 /* if ever the hash computation failed */
544 quorum_count_vote(&acb
->votes
, &hash
, i
);
547 /* vote to select the most represented version */
548 winner
= quorum_get_vote_winner(&acb
->votes
);
550 /* if the winner count is smaller than threshold the read fails */
551 if (winner
->vote_count
< s
->threshold
) {
552 quorum_report_failure(acb
);
553 acb
->vote_ret
= -EIO
;
557 /* we have a winner: copy it */
558 quorum_copy_qiov(acb
->qiov
, &acb
->qcrs
[winner
->index
].qiov
);
560 /* some versions are bad print them */
561 quorum_report_bad_versions(s
, acb
, &winner
->value
);
563 /* corruption correction is enabled */
564 if (s
->rewrite_corrupted
) {
565 quorum_rewrite_bad_versions(acb
, &winner
->value
);
570 quorum_free_vote_list(&acb
->votes
);
573 static void read_quorum_children_entry(void *opaque
)
575 QuorumCo
*co
= opaque
;
576 QuorumAIOCB
*acb
= co
->acb
;
577 BDRVQuorumState
*s
= acb
->bs
->opaque
;
579 QuorumChildRequest
*sacb
= &acb
->qcrs
[i
];
581 sacb
->bs
= s
->children
[i
]->bs
;
582 sacb
->ret
= bdrv_co_preadv(s
->children
[i
], acb
->offset
, acb
->bytes
,
583 &acb
->qcrs
[i
].qiov
, 0);
585 if (sacb
->ret
== 0) {
586 acb
->success_count
++;
588 quorum_report_bad_acb(sacb
, sacb
->ret
);
592 assert(acb
->count
<= s
->num_children
);
593 assert(acb
->success_count
<= s
->num_children
);
595 /* Wake up the caller after the last read */
596 if (acb
->count
== s
->num_children
) {
597 qemu_coroutine_enter_if_inactive(acb
->co
);
601 static int read_quorum_children(QuorumAIOCB
*acb
)
603 BDRVQuorumState
*s
= acb
->bs
->opaque
;
606 acb
->children_read
= s
->num_children
;
607 for (i
= 0; i
< s
->num_children
; i
++) {
608 acb
->qcrs
[i
].buf
= qemu_blockalign(s
->children
[i
]->bs
, acb
->qiov
->size
);
609 qemu_iovec_init(&acb
->qcrs
[i
].qiov
, acb
->qiov
->niov
);
610 qemu_iovec_clone(&acb
->qcrs
[i
].qiov
, acb
->qiov
, acb
->qcrs
[i
].buf
);
613 for (i
= 0; i
< s
->num_children
; i
++) {
620 co
= qemu_coroutine_create(read_quorum_children_entry
, &data
);
621 qemu_coroutine_enter(co
);
624 while (acb
->count
< s
->num_children
) {
625 qemu_coroutine_yield();
628 /* Do the vote on read */
630 for (i
= 0; i
< s
->num_children
; i
++) {
631 qemu_vfree(acb
->qcrs
[i
].buf
);
632 qemu_iovec_destroy(&acb
->qcrs
[i
].qiov
);
635 while (acb
->rewrite_count
) {
636 qemu_coroutine_yield();
639 return acb
->vote_ret
;
642 static int read_fifo_child(QuorumAIOCB
*acb
)
644 BDRVQuorumState
*s
= acb
->bs
->opaque
;
647 /* We try to read the next child in FIFO order if we failed to read */
649 n
= acb
->children_read
++;
650 acb
->qcrs
[n
].bs
= s
->children
[n
]->bs
;
651 ret
= bdrv_co_preadv(s
->children
[n
], acb
->offset
, acb
->bytes
,
654 quorum_report_bad_acb(&acb
->qcrs
[n
], ret
);
656 } while (ret
< 0 && acb
->children_read
< s
->num_children
);
658 /* FIXME: rewrite failed children if acb->children_read > 1? */
663 static int quorum_co_preadv(BlockDriverState
*bs
, uint64_t offset
,
664 uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
666 BDRVQuorumState
*s
= bs
->opaque
;
667 QuorumAIOCB
*acb
= quorum_aio_get(bs
, qiov
, offset
, bytes
, flags
);
671 acb
->children_read
= 0;
673 if (s
->read_pattern
== QUORUM_READ_PATTERN_QUORUM
) {
674 ret
= read_quorum_children(acb
);
676 ret
= read_fifo_child(acb
);
678 quorum_aio_finalize(acb
);
683 static void write_quorum_entry(void *opaque
)
685 QuorumCo
*co
= opaque
;
686 QuorumAIOCB
*acb
= co
->acb
;
687 BDRVQuorumState
*s
= acb
->bs
->opaque
;
689 QuorumChildRequest
*sacb
= &acb
->qcrs
[i
];
691 sacb
->bs
= s
->children
[i
]->bs
;
692 sacb
->ret
= bdrv_co_pwritev(s
->children
[i
], acb
->offset
, acb
->bytes
,
693 acb
->qiov
, acb
->flags
);
694 if (sacb
->ret
== 0) {
695 acb
->success_count
++;
697 quorum_report_bad_acb(sacb
, sacb
->ret
);
700 assert(acb
->count
<= s
->num_children
);
701 assert(acb
->success_count
<= s
->num_children
);
703 /* Wake up the caller after the last write */
704 if (acb
->count
== s
->num_children
) {
705 qemu_coroutine_enter_if_inactive(acb
->co
);
709 static int quorum_co_pwritev(BlockDriverState
*bs
, uint64_t offset
,
710 uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
712 BDRVQuorumState
*s
= bs
->opaque
;
713 QuorumAIOCB
*acb
= quorum_aio_get(bs
, qiov
, offset
, bytes
, flags
);
716 for (i
= 0; i
< s
->num_children
; i
++) {
723 co
= qemu_coroutine_create(write_quorum_entry
, &data
);
724 qemu_coroutine_enter(co
);
727 while (acb
->count
< s
->num_children
) {
728 qemu_coroutine_yield();
731 quorum_has_too_much_io_failed(acb
);
734 quorum_aio_finalize(acb
);
739 static int64_t quorum_getlength(BlockDriverState
*bs
)
741 BDRVQuorumState
*s
= bs
->opaque
;
745 /* check that all file have the same length */
746 result
= bdrv_getlength(s
->children
[0]->bs
);
750 for (i
= 1; i
< s
->num_children
; i
++) {
751 int64_t value
= bdrv_getlength(s
->children
[i
]->bs
);
755 if (value
!= result
) {
763 static coroutine_fn
int quorum_co_flush(BlockDriverState
*bs
)
765 BDRVQuorumState
*s
= bs
->opaque
;
766 QuorumVoteVersion
*winner
= NULL
;
767 QuorumVotes error_votes
;
768 QuorumVoteValue result_value
;
771 int success_count
= 0;
773 QLIST_INIT(&error_votes
.vote_list
);
774 error_votes
.compare
= quorum_64bits_compare
;
776 for (i
= 0; i
< s
->num_children
; i
++) {
777 result
= bdrv_co_flush(s
->children
[i
]->bs
);
779 quorum_report_bad(QUORUM_OP_TYPE_FLUSH
, 0, 0,
780 s
->children
[i
]->bs
->node_name
, result
);
781 result_value
.l
= result
;
782 quorum_count_vote(&error_votes
, &result_value
, i
);
788 if (success_count
>= s
->threshold
) {
791 winner
= quorum_get_vote_winner(&error_votes
);
792 result
= winner
->value
.l
;
794 quorum_free_vote_list(&error_votes
);
799 static bool quorum_recurse_can_replace(BlockDriverState
*bs
,
800 BlockDriverState
*to_replace
)
802 BDRVQuorumState
*s
= bs
->opaque
;
805 for (i
= 0; i
< s
->num_children
; i
++) {
807 * We have no idea whether our children show the same data as
808 * this node (@bs). It is actually highly likely that
809 * @to_replace does not, because replacing a broken child is
810 * one of the main use cases here.
812 * We do know that the new BDS will match @bs, so replacing
813 * any of our children by it will be safe. It cannot change
814 * the data this quorum node presents to its parents.
816 * However, replacing @to_replace by @bs in any of our
817 * children's chains may change visible data somewhere in
818 * there. We therefore cannot recurse down those chains with
819 * bdrv_recurse_can_replace().
820 * (More formally, bdrv_recurse_can_replace() requires that
821 * @to_replace will be replaced by something matching the @bs
822 * passed to it. We cannot guarantee that.)
824 * Thus, we can only check whether any of our immediate
825 * children matches @to_replace.
827 * (In the future, we might add a function to recurse down a
828 * chain that checks that nothing there cares about a change
829 * in data from the respective child in question. For
830 * example, most filters do not care when their child's data
831 * suddenly changes, as long as their parents do not care.)
833 if (s
->children
[i
]->bs
== to_replace
) {
835 * We now have to ensure that there is no other parent
836 * that cares about replacing this child by a node with
837 * potentially different data.
838 * We do so by checking whether there are any other parents
839 * at all, which is stricter than necessary, but also very
840 * simple. (We may decide to implement something more
841 * complex and permissive when there is an actual need for
844 return QLIST_FIRST(&to_replace
->parents
) == s
->children
[i
] &&
845 QLIST_NEXT(s
->children
[i
], next_parent
) == NULL
;
852 static int quorum_valid_threshold(int threshold
, int num_children
, Error
**errp
)
856 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
857 "vote-threshold", "value >= 1");
861 if (threshold
> num_children
) {
862 error_setg(errp
, "threshold may not exceed children count");
869 static QemuOptsList quorum_runtime_opts
= {
871 .head
= QTAILQ_HEAD_INITIALIZER(quorum_runtime_opts
.head
),
874 .name
= QUORUM_OPT_VOTE_THRESHOLD
,
875 .type
= QEMU_OPT_NUMBER
,
876 .help
= "The number of vote needed for reaching quorum",
879 .name
= QUORUM_OPT_BLKVERIFY
,
880 .type
= QEMU_OPT_BOOL
,
881 .help
= "Trigger block verify mode if set",
884 .name
= QUORUM_OPT_REWRITE
,
885 .type
= QEMU_OPT_BOOL
,
886 .help
= "Rewrite corrupted block on read quorum",
889 .name
= QUORUM_OPT_READ_PATTERN
,
890 .type
= QEMU_OPT_STRING
,
891 .help
= "Allowed pattern: quorum, fifo. Quorum is default",
893 { /* end of list */ }
897 static int quorum_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
900 BDRVQuorumState
*s
= bs
->opaque
;
901 Error
*local_err
= NULL
;
902 QemuOpts
*opts
= NULL
;
903 const char *pattern_str
;
908 qdict_flatten(options
);
910 /* count how many different children are present */
911 s
->num_children
= qdict_array_entries(options
, "children.");
912 if (s
->num_children
< 0) {
913 error_setg(&local_err
, "Option children is not a valid array");
917 if (s
->num_children
< 1) {
918 error_setg(&local_err
,
919 "Number of provided children must be 1 or more");
924 opts
= qemu_opts_create(&quorum_runtime_opts
, NULL
, 0, &error_abort
);
925 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
931 s
->threshold
= qemu_opt_get_number(opts
, QUORUM_OPT_VOTE_THRESHOLD
, 0);
932 /* and validate it against s->num_children */
933 ret
= quorum_valid_threshold(s
->threshold
, s
->num_children
, &local_err
);
938 pattern_str
= qemu_opt_get(opts
, QUORUM_OPT_READ_PATTERN
);
940 ret
= QUORUM_READ_PATTERN_QUORUM
;
942 ret
= qapi_enum_parse(&QuorumReadPattern_lookup
, pattern_str
,
946 error_setg(&local_err
, "Please set read-pattern as fifo or quorum");
949 s
->read_pattern
= ret
;
951 if (s
->read_pattern
== QUORUM_READ_PATTERN_QUORUM
) {
952 s
->is_blkverify
= qemu_opt_get_bool(opts
, QUORUM_OPT_BLKVERIFY
, false);
953 if (s
->is_blkverify
&& (s
->num_children
!= 2 || s
->threshold
!= 2)) {
954 error_setg(&local_err
, "blkverify=on can only be set if there are "
955 "exactly two files and vote-threshold is 2");
960 s
->rewrite_corrupted
= qemu_opt_get_bool(opts
, QUORUM_OPT_REWRITE
,
962 if (s
->rewrite_corrupted
&& s
->is_blkverify
) {
963 error_setg(&local_err
,
964 "rewrite-corrupted=on cannot be used with blkverify=on");
970 /* allocate the children array */
971 s
->children
= g_new0(BdrvChild
*, s
->num_children
);
972 opened
= g_new0(bool, s
->num_children
);
974 for (i
= 0; i
< s
->num_children
; i
++) {
976 ret
= snprintf(indexstr
, 32, "children.%d", i
);
979 s
->children
[i
] = bdrv_open_child(NULL
, options
, indexstr
, bs
,
980 &child_format
, false, &local_err
);
988 s
->next_child_index
= s
->num_children
;
990 bs
->supported_write_flags
= BDRV_REQ_WRITE_UNCHANGED
;
996 /* cleanup on error */
997 for (i
= 0; i
< s
->num_children
; i
++) {
1001 bdrv_unref_child(bs
, s
->children
[i
]);
1003 g_free(s
->children
);
1006 qemu_opts_del(opts
);
1007 /* propagate error */
1008 error_propagate(errp
, local_err
);
1012 static void quorum_close(BlockDriverState
*bs
)
1014 BDRVQuorumState
*s
= bs
->opaque
;
1017 for (i
= 0; i
< s
->num_children
; i
++) {
1018 bdrv_unref_child(bs
, s
->children
[i
]);
1021 g_free(s
->children
);
1024 static void quorum_add_child(BlockDriverState
*bs
, BlockDriverState
*child_bs
,
1027 BDRVQuorumState
*s
= bs
->opaque
;
1032 if (s
->is_blkverify
) {
1033 error_setg(errp
, "Cannot add a child to a quorum in blkverify mode");
1037 assert(s
->num_children
<= INT_MAX
/ sizeof(BdrvChild
*));
1038 if (s
->num_children
== INT_MAX
/ sizeof(BdrvChild
*) ||
1039 s
->next_child_index
== UINT_MAX
) {
1040 error_setg(errp
, "Too many children");
1044 ret
= snprintf(indexstr
, 32, "children.%u", s
->next_child_index
);
1045 if (ret
< 0 || ret
>= 32) {
1046 error_setg(errp
, "cannot generate child name");
1049 s
->next_child_index
++;
1051 bdrv_drained_begin(bs
);
1053 /* We can safely add the child now */
1056 child
= bdrv_attach_child(bs
, child_bs
, indexstr
, &child_format
, errp
);
1057 if (child
== NULL
) {
1058 s
->next_child_index
--;
1061 s
->children
= g_renew(BdrvChild
*, s
->children
, s
->num_children
+ 1);
1062 s
->children
[s
->num_children
++] = child
;
1065 bdrv_drained_end(bs
);
1068 static void quorum_del_child(BlockDriverState
*bs
, BdrvChild
*child
,
1071 BDRVQuorumState
*s
= bs
->opaque
;
1074 for (i
= 0; i
< s
->num_children
; i
++) {
1075 if (s
->children
[i
] == child
) {
1080 /* we have checked it in bdrv_del_child() */
1081 assert(i
< s
->num_children
);
1083 if (s
->num_children
<= s
->threshold
) {
1085 "The number of children cannot be lower than the vote threshold %d",
1090 /* We know now that num_children > threshold, so blkverify must be false */
1091 assert(!s
->is_blkverify
);
1093 bdrv_drained_begin(bs
);
1095 /* We can safely remove this child now */
1096 memmove(&s
->children
[i
], &s
->children
[i
+ 1],
1097 (s
->num_children
- i
- 1) * sizeof(BdrvChild
*));
1098 s
->children
= g_renew(BdrvChild
*, s
->children
, --s
->num_children
);
1099 bdrv_unref_child(bs
, child
);
1101 bdrv_drained_end(bs
);
1104 static void quorum_gather_child_options(BlockDriverState
*bs
, QDict
*target
,
1105 bool backing_overridden
)
1107 BDRVQuorumState
*s
= bs
->opaque
;
1108 QList
*children_list
;
1112 * The generic implementation for gathering child options in
1113 * bdrv_refresh_filename() would use the names of the children
1114 * as specified for bdrv_open_child() or bdrv_attach_child(),
1115 * which is "children.%u" with %u being a value
1116 * (s->next_child_index) that is incremented each time a new child
1117 * is added (and never decremented). Since children can be
1118 * deleted at runtime, there may be gaps in that enumeration.
1119 * When creating a new quorum BDS and specifying the children for
1120 * it through runtime options, the enumeration used there may not
1121 * have any gaps, though.
1123 * Therefore, we have to create a new gap-less enumeration here
1124 * (which we can achieve by simply putting all of the children's
1125 * full_open_options into a QList).
1127 * XXX: Note that there are issues with the current child option
1128 * structure quorum uses (such as the fact that children do
1129 * not really have unique permanent names). Therefore, this
1130 * is going to have to change in the future and ideally we
1131 * want quorum to be covered by the generic implementation.
1134 children_list
= qlist_new();
1135 qdict_put(target
, "children", children_list
);
1137 for (i
= 0; i
< s
->num_children
; i
++) {
1138 qlist_append(children_list
,
1139 qobject_ref(s
->children
[i
]->bs
->full_open_options
));
1143 static char *quorum_dirname(BlockDriverState
*bs
, Error
**errp
)
1145 /* In general, there are multiple BDSs with different dirnames below this
1146 * one; so there is no unique dirname we could return (unless all are equal
1147 * by chance, or there is only one). Therefore, to be consistent, just
1148 * always return NULL. */
1149 error_setg(errp
, "Cannot generate a base directory for quorum nodes");
1153 static void quorum_child_perm(BlockDriverState
*bs
, BdrvChild
*c
,
1154 const BdrvChildRole
*role
,
1155 BlockReopenQueue
*reopen_queue
,
1156 uint64_t perm
, uint64_t shared
,
1157 uint64_t *nperm
, uint64_t *nshared
)
1159 *nperm
= perm
& DEFAULT_PERM_PASSTHROUGH
;
1162 * We cannot share RESIZE or WRITE, as this would make the
1163 * children differ from each other.
1165 *nshared
= (shared
& (BLK_PERM_CONSISTENT_READ
|
1166 BLK_PERM_WRITE_UNCHANGED
))
1167 | DEFAULT_PERM_UNCHANGED
;
1170 static const char *const quorum_strong_runtime_opts
[] = {
1171 QUORUM_OPT_VOTE_THRESHOLD
,
1172 QUORUM_OPT_BLKVERIFY
,
1174 QUORUM_OPT_READ_PATTERN
,
1179 static BlockDriver bdrv_quorum
= {
1180 .format_name
= "quorum",
1182 .instance_size
= sizeof(BDRVQuorumState
),
1184 .bdrv_open
= quorum_open
,
1185 .bdrv_close
= quorum_close
,
1186 .bdrv_gather_child_options
= quorum_gather_child_options
,
1187 .bdrv_dirname
= quorum_dirname
,
1189 .bdrv_co_flush_to_disk
= quorum_co_flush
,
1191 .bdrv_getlength
= quorum_getlength
,
1193 .bdrv_co_preadv
= quorum_co_preadv
,
1194 .bdrv_co_pwritev
= quorum_co_pwritev
,
1196 .bdrv_add_child
= quorum_add_child
,
1197 .bdrv_del_child
= quorum_del_child
,
1199 .bdrv_child_perm
= quorum_child_perm
,
1201 .bdrv_recurse_can_replace
= quorum_recurse_can_replace
,
1203 .strong_runtime_opts
= quorum_strong_runtime_opts
,
1206 static void bdrv_quorum_init(void)
1208 if (!qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA256
)) {
1209 /* SHA256 hash support is required for quorum device */
1212 bdrv_register(&bdrv_quorum
);
1215 block_init(bdrv_quorum_init
);