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 "block/block_int.h"
18 #include "qapi/qmp/qbool.h"
19 #include "qapi/qmp/qdict.h"
20 #include "qapi/qmp/qerror.h"
21 #include "qapi/qmp/qint.h"
22 #include "qapi/qmp/qjson.h"
23 #include "qapi/qmp/qlist.h"
24 #include "qapi/qmp/qstring.h"
25 #include "qapi-event.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 */
42 typedef struct QuorumVoteItem
{
44 QLIST_ENTRY(QuorumVoteItem
) next
;
47 /* this structure is a vote version. A version is the set of votes sharing the
49 * The set of votes will be tracked with the items field and its cardinality is
52 typedef struct QuorumVoteVersion
{
53 QuorumVoteValue value
;
56 QLIST_HEAD(, QuorumVoteItem
) items
;
57 QLIST_ENTRY(QuorumVoteVersion
) next
;
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
);
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 int threshold
; /* if less than threshold children reads gave the
71 * same result a quorum error occurs.
73 bool is_blkverify
; /* true if the driver is in blkverify mode
74 * Writes are mirrored on two children devices.
75 * On reads the two children devices' contents are
76 * compared and if a difference is spotted its
77 * location is printed and the code aborts.
78 * It is useful to debug other block drivers by
79 * comparing them with a reference one.
81 bool rewrite_corrupted
;/* true if the driver must rewrite-on-read corrupted
82 * block if Quorum is reached.
85 QuorumReadPattern read_pattern
;
88 typedef struct QuorumAIOCB QuorumAIOCB
;
90 /* Quorum will create one instance of the following structure per operation it
91 * performs on its children.
92 * So for each read/write operation coming from the upper layer there will be
93 * $children_count QuorumChildRequest.
95 typedef struct QuorumChildRequest
{
101 } QuorumChildRequest
;
103 /* Quorum will use the following structure to track progress of each read/write
104 * operation received by the upper layer.
105 * This structure hold pointers to the QuorumChildRequest structures instances
106 * used to do operations on each children and track overall progress.
111 /* Request metadata */
115 QEMUIOVector
*qiov
; /* calling IOV */
117 QuorumChildRequest
*qcrs
; /* individual child requests */
118 int count
; /* number of completed AIOCB */
119 int success_count
; /* number of successfully completed AIOCB */
121 int rewrite_count
; /* number of replica to rewrite: count down to
122 * zero once writes are fired
129 int child_iter
; /* which child to read in fifo pattern */
132 static bool quorum_vote(QuorumAIOCB
*acb
);
134 static void quorum_aio_cancel(BlockAIOCB
*blockacb
)
136 QuorumAIOCB
*acb
= container_of(blockacb
, QuorumAIOCB
, common
);
137 BDRVQuorumState
*s
= acb
->common
.bs
->opaque
;
140 /* cancel all callbacks */
141 for (i
= 0; i
< s
->num_children
; i
++) {
142 if (acb
->qcrs
[i
].aiocb
) {
143 bdrv_aio_cancel_async(acb
->qcrs
[i
].aiocb
);
148 static AIOCBInfo quorum_aiocb_info
= {
149 .aiocb_size
= sizeof(QuorumAIOCB
),
150 .cancel_async
= quorum_aio_cancel
,
153 static void quorum_aio_finalize(QuorumAIOCB
*acb
)
161 acb
->common
.cb(acb
->common
.opaque
, ret
);
164 /* on the quorum case acb->child_iter == s->num_children - 1 */
165 for (i
= 0; i
<= acb
->child_iter
; i
++) {
166 qemu_vfree(acb
->qcrs
[i
].buf
);
167 qemu_iovec_destroy(&acb
->qcrs
[i
].qiov
);
175 static bool quorum_sha256_compare(QuorumVoteValue
*a
, QuorumVoteValue
*b
)
177 return !memcmp(a
->h
, b
->h
, HASH_LENGTH
);
180 static bool quorum_64bits_compare(QuorumVoteValue
*a
, QuorumVoteValue
*b
)
185 static QuorumAIOCB
*quorum_aio_get(BDRVQuorumState
*s
,
186 BlockDriverState
*bs
,
190 BlockCompletionFunc
*cb
,
193 QuorumAIOCB
*acb
= qemu_aio_get(&quorum_aiocb_info
, bs
, cb
, opaque
);
196 acb
->common
.bs
->opaque
= s
;
197 acb
->sector_num
= sector_num
;
198 acb
->nb_sectors
= nb_sectors
;
200 acb
->qcrs
= g_new0(QuorumChildRequest
, s
->num_children
);
202 acb
->success_count
= 0;
203 acb
->rewrite_count
= 0;
204 acb
->votes
.compare
= quorum_sha256_compare
;
205 QLIST_INIT(&acb
->votes
.vote_list
);
206 acb
->is_read
= false;
209 for (i
= 0; i
< s
->num_children
; i
++) {
210 acb
->qcrs
[i
].buf
= NULL
;
211 acb
->qcrs
[i
].ret
= 0;
212 acb
->qcrs
[i
].parent
= acb
;
218 static void quorum_report_bad(QuorumOpType type
, uint64_t sector_num
,
219 int nb_sectors
, char *node_name
, int ret
)
221 const char *msg
= NULL
;
223 msg
= strerror(-ret
);
226 qapi_event_send_quorum_report_bad(type
, !!msg
, msg
, node_name
,
227 sector_num
, nb_sectors
, &error_abort
);
230 static void quorum_report_failure(QuorumAIOCB
*acb
)
232 const char *reference
= bdrv_get_device_or_node_name(acb
->common
.bs
);
233 qapi_event_send_quorum_failure(reference
, acb
->sector_num
,
234 acb
->nb_sectors
, &error_abort
);
237 static int quorum_vote_error(QuorumAIOCB
*acb
);
239 static bool quorum_has_too_much_io_failed(QuorumAIOCB
*acb
)
241 BDRVQuorumState
*s
= acb
->common
.bs
->opaque
;
243 if (acb
->success_count
< s
->threshold
) {
244 acb
->vote_ret
= quorum_vote_error(acb
);
245 quorum_report_failure(acb
);
252 static void quorum_rewrite_aio_cb(void *opaque
, int ret
)
254 QuorumAIOCB
*acb
= opaque
;
256 /* one less rewrite to do */
257 acb
->rewrite_count
--;
259 /* wait until all rewrite callbacks have completed */
260 if (acb
->rewrite_count
) {
264 quorum_aio_finalize(acb
);
267 static BlockAIOCB
*read_fifo_child(QuorumAIOCB
*acb
);
269 static void quorum_copy_qiov(QEMUIOVector
*dest
, QEMUIOVector
*source
)
272 assert(dest
->niov
== source
->niov
);
273 assert(dest
->size
== source
->size
);
274 for (i
= 0; i
< source
->niov
; i
++) {
275 assert(dest
->iov
[i
].iov_len
== source
->iov
[i
].iov_len
);
276 memcpy(dest
->iov
[i
].iov_base
,
277 source
->iov
[i
].iov_base
,
278 source
->iov
[i
].iov_len
);
282 static void quorum_aio_cb(void *opaque
, int ret
)
284 QuorumChildRequest
*sacb
= opaque
;
285 QuorumAIOCB
*acb
= sacb
->parent
;
286 BDRVQuorumState
*s
= acb
->common
.bs
->opaque
;
288 bool rewrite
= false;
290 if (acb
->is_read
&& s
->read_pattern
== QUORUM_READ_PATTERN_FIFO
) {
291 /* We try to read next child in FIFO order if we fail to read */
292 if (ret
< 0 && (acb
->child_iter
+ 1) < s
->num_children
) {
294 read_fifo_child(acb
);
299 quorum_copy_qiov(acb
->qiov
, &acb
->qcrs
[acb
->child_iter
].qiov
);
302 quorum_aio_finalize(acb
);
306 type
= acb
->is_read
? QUORUM_OP_TYPE_READ
: QUORUM_OP_TYPE_WRITE
;
310 acb
->success_count
++;
312 quorum_report_bad(type
, acb
->sector_num
, acb
->nb_sectors
,
313 sacb
->aiocb
->bs
->node_name
, ret
);
315 assert(acb
->count
<= s
->num_children
);
316 assert(acb
->success_count
<= s
->num_children
);
317 if (acb
->count
< s
->num_children
) {
321 /* Do the vote on read */
323 rewrite
= quorum_vote(acb
);
325 quorum_has_too_much_io_failed(acb
);
328 /* if no rewrite is done the code will finish right away */
330 quorum_aio_finalize(acb
);
334 static void quorum_report_bad_versions(BDRVQuorumState
*s
,
336 QuorumVoteValue
*value
)
338 QuorumVoteVersion
*version
;
339 QuorumVoteItem
*item
;
341 QLIST_FOREACH(version
, &acb
->votes
.vote_list
, next
) {
342 if (acb
->votes
.compare(&version
->value
, value
)) {
345 QLIST_FOREACH(item
, &version
->items
, next
) {
346 quorum_report_bad(QUORUM_OP_TYPE_READ
, acb
->sector_num
,
348 s
->children
[item
->index
]->bs
->node_name
, 0);
353 static bool quorum_rewrite_bad_versions(BDRVQuorumState
*s
, QuorumAIOCB
*acb
,
354 QuorumVoteValue
*value
)
356 QuorumVoteVersion
*version
;
357 QuorumVoteItem
*item
;
360 /* first count the number of bad versions: done first to avoid concurrency
363 QLIST_FOREACH(version
, &acb
->votes
.vote_list
, next
) {
364 if (acb
->votes
.compare(&version
->value
, value
)) {
367 QLIST_FOREACH(item
, &version
->items
, next
) {
372 /* quorum_rewrite_aio_cb will count down this to zero */
373 acb
->rewrite_count
= count
;
375 /* now fire the correcting rewrites */
376 QLIST_FOREACH(version
, &acb
->votes
.vote_list
, next
) {
377 if (acb
->votes
.compare(&version
->value
, value
)) {
380 QLIST_FOREACH(item
, &version
->items
, next
) {
381 bdrv_aio_writev(s
->children
[item
->index
]->bs
, acb
->sector_num
,
382 acb
->qiov
, acb
->nb_sectors
, quorum_rewrite_aio_cb
,
387 /* return true if any rewrite is done else false */
391 static void quorum_count_vote(QuorumVotes
*votes
,
392 QuorumVoteValue
*value
,
395 QuorumVoteVersion
*v
= NULL
, *version
= NULL
;
396 QuorumVoteItem
*item
;
398 /* look if we have something with this hash */
399 QLIST_FOREACH(v
, &votes
->vote_list
, next
) {
400 if (votes
->compare(&v
->value
, value
)) {
406 /* It's a version not yet in the list add it */
408 version
= g_new0(QuorumVoteVersion
, 1);
409 QLIST_INIT(&version
->items
);
410 memcpy(&version
->value
, value
, sizeof(version
->value
));
411 version
->index
= index
;
412 version
->vote_count
= 0;
413 QLIST_INSERT_HEAD(&votes
->vote_list
, version
, next
);
416 version
->vote_count
++;
418 item
= g_new0(QuorumVoteItem
, 1);
420 QLIST_INSERT_HEAD(&version
->items
, item
, next
);
423 static void quorum_free_vote_list(QuorumVotes
*votes
)
425 QuorumVoteVersion
*version
, *next_version
;
426 QuorumVoteItem
*item
, *next_item
;
428 QLIST_FOREACH_SAFE(version
, &votes
->vote_list
, next
, next_version
) {
429 QLIST_REMOVE(version
, next
);
430 QLIST_FOREACH_SAFE(item
, &version
->items
, next
, next_item
) {
431 QLIST_REMOVE(item
, next
);
438 static int quorum_compute_hash(QuorumAIOCB
*acb
, int i
, QuorumVoteValue
*hash
)
440 QEMUIOVector
*qiov
= &acb
->qcrs
[i
].qiov
;
441 size_t len
= sizeof(hash
->h
);
442 uint8_t *data
= hash
->h
;
444 /* XXX - would be nice if we could pass in the Error **
445 * and propagate that back, but this quorum code is
446 * restricted to just errno values currently */
447 if (qcrypto_hash_bytesv(QCRYPTO_HASH_ALG_SHA256
,
448 qiov
->iov
, qiov
->niov
,
457 static QuorumVoteVersion
*quorum_get_vote_winner(QuorumVotes
*votes
)
460 QuorumVoteVersion
*candidate
, *winner
= NULL
;
462 QLIST_FOREACH(candidate
, &votes
->vote_list
, next
) {
463 if (candidate
->vote_count
> max
) {
464 max
= candidate
->vote_count
;
472 /* qemu_iovec_compare is handy for blkverify mode because it returns the first
473 * differing byte location. Yet it is handcoded to compare vectors one byte
474 * after another so it does not benefit from the libc SIMD optimizations.
475 * quorum_iovec_compare is written for speed and should be used in the non
476 * blkverify mode of quorum.
478 static bool quorum_iovec_compare(QEMUIOVector
*a
, QEMUIOVector
*b
)
483 assert(a
->niov
== b
->niov
);
484 for (i
= 0; i
< a
->niov
; i
++) {
485 assert(a
->iov
[i
].iov_len
== b
->iov
[i
].iov_len
);
486 result
= memcmp(a
->iov
[i
].iov_base
,
497 static void GCC_FMT_ATTR(2, 3) quorum_err(QuorumAIOCB
*acb
,
498 const char *fmt
, ...)
503 fprintf(stderr
, "quorum: sector_num=%" PRId64
" nb_sectors=%d ",
504 acb
->sector_num
, acb
->nb_sectors
);
505 vfprintf(stderr
, fmt
, ap
);
506 fprintf(stderr
, "\n");
511 static bool quorum_compare(QuorumAIOCB
*acb
,
515 BDRVQuorumState
*s
= acb
->common
.bs
->opaque
;
518 /* This driver will replace blkverify in this particular case */
519 if (s
->is_blkverify
) {
520 offset
= qemu_iovec_compare(a
, b
);
522 quorum_err(acb
, "contents mismatch in sector %" PRId64
,
524 (uint64_t)(offset
/ BDRV_SECTOR_SIZE
));
529 return quorum_iovec_compare(a
, b
);
532 /* Do a vote to get the error code */
533 static int quorum_vote_error(QuorumAIOCB
*acb
)
535 BDRVQuorumState
*s
= acb
->common
.bs
->opaque
;
536 QuorumVoteVersion
*winner
= NULL
;
537 QuorumVotes error_votes
;
538 QuorumVoteValue result_value
;
542 QLIST_INIT(&error_votes
.vote_list
);
543 error_votes
.compare
= quorum_64bits_compare
;
545 for (i
= 0; i
< s
->num_children
; i
++) {
546 ret
= acb
->qcrs
[i
].ret
;
549 result_value
.l
= ret
;
550 quorum_count_vote(&error_votes
, &result_value
, i
);
555 winner
= quorum_get_vote_winner(&error_votes
);
556 ret
= winner
->value
.l
;
559 quorum_free_vote_list(&error_votes
);
564 static bool quorum_vote(QuorumAIOCB
*acb
)
567 bool rewrite
= false;
569 QuorumVoteValue hash
;
570 BDRVQuorumState
*s
= acb
->common
.bs
->opaque
;
571 QuorumVoteVersion
*winner
;
573 if (quorum_has_too_much_io_failed(acb
)) {
577 /* get the index of the first successful read */
578 for (i
= 0; i
< s
->num_children
; i
++) {
579 if (!acb
->qcrs
[i
].ret
) {
584 assert(i
< s
->num_children
);
586 /* compare this read with all other successful reads stopping at quorum
589 for (j
= i
+ 1; j
< s
->num_children
; j
++) {
590 if (acb
->qcrs
[j
].ret
) {
593 quorum
= quorum_compare(acb
, &acb
->qcrs
[i
].qiov
, &acb
->qcrs
[j
].qiov
);
599 /* Every successful read agrees */
601 quorum_copy_qiov(acb
->qiov
, &acb
->qcrs
[i
].qiov
);
605 /* compute hashes for each successful read, also store indexes */
606 for (i
= 0; i
< s
->num_children
; i
++) {
607 if (acb
->qcrs
[i
].ret
) {
610 ret
= quorum_compute_hash(acb
, i
, &hash
);
611 /* if ever the hash computation failed */
616 quorum_count_vote(&acb
->votes
, &hash
, i
);
619 /* vote to select the most represented version */
620 winner
= quorum_get_vote_winner(&acb
->votes
);
622 /* if the winner count is smaller than threshold the read fails */
623 if (winner
->vote_count
< s
->threshold
) {
624 quorum_report_failure(acb
);
625 acb
->vote_ret
= -EIO
;
629 /* we have a winner: copy it */
630 quorum_copy_qiov(acb
->qiov
, &acb
->qcrs
[winner
->index
].qiov
);
632 /* some versions are bad print them */
633 quorum_report_bad_versions(s
, acb
, &winner
->value
);
635 /* corruption correction is enabled */
636 if (s
->rewrite_corrupted
) {
637 rewrite
= quorum_rewrite_bad_versions(s
, acb
, &winner
->value
);
642 quorum_free_vote_list(&acb
->votes
);
646 static BlockAIOCB
*read_quorum_children(QuorumAIOCB
*acb
)
648 BDRVQuorumState
*s
= acb
->common
.bs
->opaque
;
651 for (i
= 0; i
< s
->num_children
; i
++) {
652 acb
->qcrs
[i
].buf
= qemu_blockalign(s
->children
[i
]->bs
, acb
->qiov
->size
);
653 qemu_iovec_init(&acb
->qcrs
[i
].qiov
, acb
->qiov
->niov
);
654 qemu_iovec_clone(&acb
->qcrs
[i
].qiov
, acb
->qiov
, acb
->qcrs
[i
].buf
);
657 for (i
= 0; i
< s
->num_children
; i
++) {
658 acb
->qcrs
[i
].aiocb
= bdrv_aio_readv(s
->children
[i
]->bs
, acb
->sector_num
,
659 &acb
->qcrs
[i
].qiov
, acb
->nb_sectors
,
660 quorum_aio_cb
, &acb
->qcrs
[i
]);
666 static BlockAIOCB
*read_fifo_child(QuorumAIOCB
*acb
)
668 BDRVQuorumState
*s
= acb
->common
.bs
->opaque
;
670 acb
->qcrs
[acb
->child_iter
].buf
=
671 qemu_blockalign(s
->children
[acb
->child_iter
]->bs
, acb
->qiov
->size
);
672 qemu_iovec_init(&acb
->qcrs
[acb
->child_iter
].qiov
, acb
->qiov
->niov
);
673 qemu_iovec_clone(&acb
->qcrs
[acb
->child_iter
].qiov
, acb
->qiov
,
674 acb
->qcrs
[acb
->child_iter
].buf
);
675 acb
->qcrs
[acb
->child_iter
].aiocb
=
676 bdrv_aio_readv(s
->children
[acb
->child_iter
]->bs
, acb
->sector_num
,
677 &acb
->qcrs
[acb
->child_iter
].qiov
, acb
->nb_sectors
,
678 quorum_aio_cb
, &acb
->qcrs
[acb
->child_iter
]);
683 static BlockAIOCB
*quorum_aio_readv(BlockDriverState
*bs
,
687 BlockCompletionFunc
*cb
,
690 BDRVQuorumState
*s
= bs
->opaque
;
691 QuorumAIOCB
*acb
= quorum_aio_get(s
, bs
, qiov
, sector_num
,
692 nb_sectors
, cb
, opaque
);
695 if (s
->read_pattern
== QUORUM_READ_PATTERN_QUORUM
) {
696 acb
->child_iter
= s
->num_children
- 1;
697 return read_quorum_children(acb
);
701 return read_fifo_child(acb
);
704 static BlockAIOCB
*quorum_aio_writev(BlockDriverState
*bs
,
708 BlockCompletionFunc
*cb
,
711 BDRVQuorumState
*s
= bs
->opaque
;
712 QuorumAIOCB
*acb
= quorum_aio_get(s
, bs
, qiov
, sector_num
, nb_sectors
,
716 for (i
= 0; i
< s
->num_children
; i
++) {
717 acb
->qcrs
[i
].aiocb
= bdrv_aio_writev(s
->children
[i
]->bs
, sector_num
,
718 qiov
, nb_sectors
, &quorum_aio_cb
,
725 static int64_t quorum_getlength(BlockDriverState
*bs
)
727 BDRVQuorumState
*s
= bs
->opaque
;
731 /* check that all file have the same length */
732 result
= bdrv_getlength(s
->children
[0]->bs
);
736 for (i
= 1; i
< s
->num_children
; i
++) {
737 int64_t value
= bdrv_getlength(s
->children
[i
]->bs
);
741 if (value
!= result
) {
749 static void quorum_invalidate_cache(BlockDriverState
*bs
, Error
**errp
)
751 BDRVQuorumState
*s
= bs
->opaque
;
752 Error
*local_err
= NULL
;
755 for (i
= 0; i
< s
->num_children
; i
++) {
756 bdrv_invalidate_cache(s
->children
[i
]->bs
, &local_err
);
758 error_propagate(errp
, local_err
);
764 static coroutine_fn
int quorum_co_flush(BlockDriverState
*bs
)
766 BDRVQuorumState
*s
= bs
->opaque
;
767 QuorumVoteVersion
*winner
= NULL
;
768 QuorumVotes error_votes
;
769 QuorumVoteValue result_value
;
772 int success_count
= 0;
774 QLIST_INIT(&error_votes
.vote_list
);
775 error_votes
.compare
= quorum_64bits_compare
;
777 for (i
= 0; i
< s
->num_children
; i
++) {
778 result
= bdrv_co_flush(s
->children
[i
]->bs
);
780 quorum_report_bad(QUORUM_OP_TYPE_FLUSH
, 0,
781 bdrv_nb_sectors(s
->children
[i
]->bs
),
782 s
->children
[i
]->bs
->node_name
, result
);
783 result_value
.l
= result
;
784 quorum_count_vote(&error_votes
, &result_value
, i
);
790 if (success_count
>= s
->threshold
) {
793 winner
= quorum_get_vote_winner(&error_votes
);
794 result
= winner
->value
.l
;
796 quorum_free_vote_list(&error_votes
);
801 static bool quorum_recurse_is_first_non_filter(BlockDriverState
*bs
,
802 BlockDriverState
*candidate
)
804 BDRVQuorumState
*s
= bs
->opaque
;
807 for (i
= 0; i
< s
->num_children
; i
++) {
808 bool perm
= bdrv_recurse_is_first_non_filter(s
->children
[i
]->bs
,
818 static int quorum_valid_threshold(int threshold
, int num_children
, Error
**errp
)
822 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
823 "vote-threshold", "value >= 1");
827 if (threshold
> num_children
) {
828 error_setg(errp
, "threshold may not exceed children count");
835 static QemuOptsList quorum_runtime_opts
= {
837 .head
= QTAILQ_HEAD_INITIALIZER(quorum_runtime_opts
.head
),
840 .name
= QUORUM_OPT_VOTE_THRESHOLD
,
841 .type
= QEMU_OPT_NUMBER
,
842 .help
= "The number of vote needed for reaching quorum",
845 .name
= QUORUM_OPT_BLKVERIFY
,
846 .type
= QEMU_OPT_BOOL
,
847 .help
= "Trigger block verify mode if set",
850 .name
= QUORUM_OPT_REWRITE
,
851 .type
= QEMU_OPT_BOOL
,
852 .help
= "Rewrite corrupted block on read quorum",
855 .name
= QUORUM_OPT_READ_PATTERN
,
856 .type
= QEMU_OPT_STRING
,
857 .help
= "Allowed pattern: quorum, fifo. Quorum is default",
859 { /* end of list */ }
863 static int parse_read_pattern(const char *opt
)
868 /* Set quorum as default */
869 return QUORUM_READ_PATTERN_QUORUM
;
872 for (i
= 0; i
< QUORUM_READ_PATTERN__MAX
; i
++) {
873 if (!strcmp(opt
, QuorumReadPattern_lookup
[i
])) {
881 static int quorum_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
884 BDRVQuorumState
*s
= bs
->opaque
;
885 Error
*local_err
= NULL
;
886 QemuOpts
*opts
= NULL
;
891 qdict_flatten(options
);
893 /* count how many different children are present */
894 s
->num_children
= qdict_array_entries(options
, "children.");
895 if (s
->num_children
< 0) {
896 error_setg(&local_err
, "Option children is not a valid array");
900 if (s
->num_children
< 2) {
901 error_setg(&local_err
,
902 "Number of provided children must be greater than 1");
907 opts
= qemu_opts_create(&quorum_runtime_opts
, NULL
, 0, &error_abort
);
908 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
914 s
->threshold
= qemu_opt_get_number(opts
, QUORUM_OPT_VOTE_THRESHOLD
, 0);
915 /* and validate it against s->num_children */
916 ret
= quorum_valid_threshold(s
->threshold
, s
->num_children
, &local_err
);
921 ret
= parse_read_pattern(qemu_opt_get(opts
, QUORUM_OPT_READ_PATTERN
));
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
);
971 /* cleanup on error */
972 for (i
= 0; i
< s
->num_children
; i
++) {
976 bdrv_unref_child(bs
, s
->children
[i
]);
982 /* propagate error */
984 error_propagate(errp
, local_err
);
989 static void quorum_close(BlockDriverState
*bs
)
991 BDRVQuorumState
*s
= bs
->opaque
;
994 for (i
= 0; i
< s
->num_children
; i
++) {
995 bdrv_unref_child(bs
, s
->children
[i
]);
1001 static void quorum_detach_aio_context(BlockDriverState
*bs
)
1003 BDRVQuorumState
*s
= bs
->opaque
;
1006 for (i
= 0; i
< s
->num_children
; i
++) {
1007 bdrv_detach_aio_context(s
->children
[i
]->bs
);
1011 static void quorum_attach_aio_context(BlockDriverState
*bs
,
1012 AioContext
*new_context
)
1014 BDRVQuorumState
*s
= bs
->opaque
;
1017 for (i
= 0; i
< s
->num_children
; i
++) {
1018 bdrv_attach_aio_context(s
->children
[i
]->bs
, new_context
);
1022 static void quorum_refresh_filename(BlockDriverState
*bs
, QDict
*options
)
1024 BDRVQuorumState
*s
= bs
->opaque
;
1029 for (i
= 0; i
< s
->num_children
; i
++) {
1030 bdrv_refresh_filename(s
->children
[i
]->bs
);
1031 if (!s
->children
[i
]->bs
->full_open_options
) {
1036 children
= qlist_new();
1037 for (i
= 0; i
< s
->num_children
; i
++) {
1038 QINCREF(s
->children
[i
]->bs
->full_open_options
);
1039 qlist_append_obj(children
,
1040 QOBJECT(s
->children
[i
]->bs
->full_open_options
));
1044 qdict_put_obj(opts
, "driver", QOBJECT(qstring_from_str("quorum")));
1045 qdict_put_obj(opts
, QUORUM_OPT_VOTE_THRESHOLD
,
1046 QOBJECT(qint_from_int(s
->threshold
)));
1047 qdict_put_obj(opts
, QUORUM_OPT_BLKVERIFY
,
1048 QOBJECT(qbool_from_bool(s
->is_blkverify
)));
1049 qdict_put_obj(opts
, QUORUM_OPT_REWRITE
,
1050 QOBJECT(qbool_from_bool(s
->rewrite_corrupted
)));
1051 qdict_put_obj(opts
, "children", QOBJECT(children
));
1053 bs
->full_open_options
= opts
;
1056 static BlockDriver bdrv_quorum
= {
1057 .format_name
= "quorum",
1058 .protocol_name
= "quorum",
1060 .instance_size
= sizeof(BDRVQuorumState
),
1062 .bdrv_file_open
= quorum_open
,
1063 .bdrv_close
= quorum_close
,
1064 .bdrv_refresh_filename
= quorum_refresh_filename
,
1066 .bdrv_co_flush_to_disk
= quorum_co_flush
,
1068 .bdrv_getlength
= quorum_getlength
,
1070 .bdrv_aio_readv
= quorum_aio_readv
,
1071 .bdrv_aio_writev
= quorum_aio_writev
,
1072 .bdrv_invalidate_cache
= quorum_invalidate_cache
,
1074 .bdrv_detach_aio_context
= quorum_detach_aio_context
,
1075 .bdrv_attach_aio_context
= quorum_attach_aio_context
,
1078 .bdrv_recurse_is_first_non_filter
= quorum_recurse_is_first_non_filter
,
1081 static void bdrv_quorum_init(void)
1083 if (!qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA256
)) {
1084 /* SHA256 hash support is required for quorum device */
1087 bdrv_register(&bdrv_quorum
);
1090 block_init(bdrv_quorum_init
);