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 <gnutls/gnutls.h>
17 #include <gnutls/crypto.h>
18 #include "block/block_int.h"
19 #include "qapi/qmp/qbool.h"
20 #include "qapi/qmp/qdict.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"
27 #define HASH_LENGTH 32
29 #define QUORUM_OPT_VOTE_THRESHOLD "vote-threshold"
30 #define QUORUM_OPT_BLKVERIFY "blkverify"
31 #define QUORUM_OPT_REWRITE "rewrite-corrupted"
33 /* This union holds a vote hash value */
34 typedef union QuorumVoteValue
{
35 char h
[HASH_LENGTH
]; /* SHA-256 hash */
36 int64_t l
; /* simpler 64 bits hash */
40 typedef struct QuorumVoteItem
{
42 QLIST_ENTRY(QuorumVoteItem
) next
;
45 /* this structure is a vote version. A version is the set of votes sharing the
47 * The set of votes will be tracked with the items field and its cardinality is
50 typedef struct QuorumVoteVersion
{
51 QuorumVoteValue value
;
54 QLIST_HEAD(, QuorumVoteItem
) items
;
55 QLIST_ENTRY(QuorumVoteVersion
) next
;
58 /* this structure holds a group of vote versions together */
59 typedef struct QuorumVotes
{
60 QLIST_HEAD(, QuorumVoteVersion
) vote_list
;
61 bool (*compare
)(QuorumVoteValue
*a
, QuorumVoteValue
*b
);
64 /* the following structure holds the state of one quorum instance */
65 typedef struct BDRVQuorumState
{
66 BlockDriverState
**bs
; /* children BlockDriverStates */
67 int num_children
; /* children count */
68 int threshold
; /* if less than threshold children reads gave the
69 * same result a quorum error occurs.
71 bool is_blkverify
; /* true if the driver is in blkverify mode
72 * Writes are mirrored on two children devices.
73 * On reads the two children devices' contents are
74 * compared and if a difference is spotted its
75 * location is printed and the code aborts.
76 * It is useful to debug other block drivers by
77 * comparing them with a reference one.
79 bool rewrite_corrupted
;/* true if the driver must rewrite-on-read corrupted
80 * block if Quorum is reached.
84 typedef struct QuorumAIOCB QuorumAIOCB
;
86 /* Quorum will create one instance of the following structure per operation it
87 * performs on its children.
88 * So for each read/write operation coming from the upper layer there will be
89 * $children_count QuorumChildRequest.
91 typedef struct QuorumChildRequest
{
92 BlockDriverAIOCB
*aiocb
;
99 /* Quorum will use the following structure to track progress of each read/write
100 * operation received by the upper layer.
101 * This structure hold pointers to the QuorumChildRequest structures instances
102 * used to do operations on each children and track overall progress.
105 BlockDriverAIOCB common
;
107 /* Request metadata */
111 QEMUIOVector
*qiov
; /* calling IOV */
113 QuorumChildRequest
*qcrs
; /* individual child requests */
114 int count
; /* number of completed AIOCB */
115 int success_count
; /* number of successfully completed AIOCB */
117 int rewrite_count
; /* number of replica to rewrite: count down to
118 * zero once writes are fired
127 static bool quorum_vote(QuorumAIOCB
*acb
);
129 static void quorum_aio_cancel(BlockDriverAIOCB
*blockacb
)
131 QuorumAIOCB
*acb
= container_of(blockacb
, QuorumAIOCB
, common
);
132 BDRVQuorumState
*s
= acb
->common
.bs
->opaque
;
135 /* cancel all callbacks */
136 for (i
= 0; i
< s
->num_children
; i
++) {
137 bdrv_aio_cancel(acb
->qcrs
[i
].aiocb
);
141 qemu_aio_release(acb
);
144 static AIOCBInfo quorum_aiocb_info
= {
145 .aiocb_size
= sizeof(QuorumAIOCB
),
146 .cancel
= quorum_aio_cancel
,
149 static void quorum_aio_finalize(QuorumAIOCB
*acb
)
151 BDRVQuorumState
*s
= acb
->common
.bs
->opaque
;
158 acb
->common
.cb(acb
->common
.opaque
, ret
);
161 for (i
= 0; i
< s
->num_children
; i
++) {
162 qemu_vfree(acb
->qcrs
[i
].buf
);
163 qemu_iovec_destroy(&acb
->qcrs
[i
].qiov
);
168 qemu_aio_release(acb
);
171 static bool quorum_sha256_compare(QuorumVoteValue
*a
, QuorumVoteValue
*b
)
173 return !memcmp(a
->h
, b
->h
, HASH_LENGTH
);
176 static bool quorum_64bits_compare(QuorumVoteValue
*a
, QuorumVoteValue
*b
)
181 static QuorumAIOCB
*quorum_aio_get(BDRVQuorumState
*s
,
182 BlockDriverState
*bs
,
186 BlockDriverCompletionFunc
*cb
,
189 QuorumAIOCB
*acb
= qemu_aio_get(&quorum_aiocb_info
, bs
, cb
, opaque
);
192 acb
->common
.bs
->opaque
= s
;
193 acb
->sector_num
= sector_num
;
194 acb
->nb_sectors
= nb_sectors
;
196 acb
->qcrs
= g_new0(QuorumChildRequest
, s
->num_children
);
198 acb
->success_count
= 0;
199 acb
->rewrite_count
= 0;
200 acb
->votes
.compare
= quorum_sha256_compare
;
201 QLIST_INIT(&acb
->votes
.vote_list
);
202 acb
->is_read
= false;
205 for (i
= 0; i
< s
->num_children
; i
++) {
206 acb
->qcrs
[i
].buf
= NULL
;
207 acb
->qcrs
[i
].ret
= 0;
208 acb
->qcrs
[i
].parent
= acb
;
214 static void quorum_report_bad(QuorumAIOCB
*acb
, char *node_name
, int ret
)
216 const char *msg
= NULL
;
218 msg
= strerror(-ret
);
220 qapi_event_send_quorum_report_bad(!!msg
, msg
, node_name
,
221 acb
->sector_num
, acb
->nb_sectors
, &error_abort
);
224 static void quorum_report_failure(QuorumAIOCB
*acb
)
226 const char *reference
= acb
->common
.bs
->device_name
[0] ?
227 acb
->common
.bs
->device_name
:
228 acb
->common
.bs
->node_name
;
230 qapi_event_send_quorum_failure(reference
, acb
->sector_num
,
231 acb
->nb_sectors
, &error_abort
);
234 static int quorum_vote_error(QuorumAIOCB
*acb
);
236 static bool quorum_has_too_much_io_failed(QuorumAIOCB
*acb
)
238 BDRVQuorumState
*s
= acb
->common
.bs
->opaque
;
240 if (acb
->success_count
< s
->threshold
) {
241 acb
->vote_ret
= quorum_vote_error(acb
);
242 quorum_report_failure(acb
);
249 static void quorum_rewrite_aio_cb(void *opaque
, int ret
)
251 QuorumAIOCB
*acb
= opaque
;
253 /* one less rewrite to do */
254 acb
->rewrite_count
--;
256 /* wait until all rewrite callbacks have completed */
257 if (acb
->rewrite_count
) {
261 quorum_aio_finalize(acb
);
264 static void quorum_aio_cb(void *opaque
, int ret
)
266 QuorumChildRequest
*sacb
= opaque
;
267 QuorumAIOCB
*acb
= sacb
->parent
;
268 BDRVQuorumState
*s
= acb
->common
.bs
->opaque
;
269 bool rewrite
= false;
274 acb
->success_count
++;
276 quorum_report_bad(acb
, sacb
->aiocb
->bs
->node_name
, ret
);
278 assert(acb
->count
<= s
->num_children
);
279 assert(acb
->success_count
<= s
->num_children
);
280 if (acb
->count
< s
->num_children
) {
284 /* Do the vote on read */
286 rewrite
= quorum_vote(acb
);
288 quorum_has_too_much_io_failed(acb
);
291 /* if no rewrite is done the code will finish right away */
293 quorum_aio_finalize(acb
);
297 static void quorum_report_bad_versions(BDRVQuorumState
*s
,
299 QuorumVoteValue
*value
)
301 QuorumVoteVersion
*version
;
302 QuorumVoteItem
*item
;
304 QLIST_FOREACH(version
, &acb
->votes
.vote_list
, next
) {
305 if (acb
->votes
.compare(&version
->value
, value
)) {
308 QLIST_FOREACH(item
, &version
->items
, next
) {
309 quorum_report_bad(acb
, s
->bs
[item
->index
]->node_name
, 0);
314 static bool quorum_rewrite_bad_versions(BDRVQuorumState
*s
, QuorumAIOCB
*acb
,
315 QuorumVoteValue
*value
)
317 QuorumVoteVersion
*version
;
318 QuorumVoteItem
*item
;
321 /* first count the number of bad versions: done first to avoid concurrency
324 QLIST_FOREACH(version
, &acb
->votes
.vote_list
, next
) {
325 if (acb
->votes
.compare(&version
->value
, value
)) {
328 QLIST_FOREACH(item
, &version
->items
, next
) {
333 /* quorum_rewrite_aio_cb will count down this to zero */
334 acb
->rewrite_count
= count
;
336 /* now fire the correcting rewrites */
337 QLIST_FOREACH(version
, &acb
->votes
.vote_list
, next
) {
338 if (acb
->votes
.compare(&version
->value
, value
)) {
341 QLIST_FOREACH(item
, &version
->items
, next
) {
342 bdrv_aio_writev(s
->bs
[item
->index
], acb
->sector_num
, acb
->qiov
,
343 acb
->nb_sectors
, quorum_rewrite_aio_cb
, acb
);
347 /* return true if any rewrite is done else false */
351 static void quorum_copy_qiov(QEMUIOVector
*dest
, QEMUIOVector
*source
)
354 assert(dest
->niov
== source
->niov
);
355 assert(dest
->size
== source
->size
);
356 for (i
= 0; i
< source
->niov
; i
++) {
357 assert(dest
->iov
[i
].iov_len
== source
->iov
[i
].iov_len
);
358 memcpy(dest
->iov
[i
].iov_base
,
359 source
->iov
[i
].iov_base
,
360 source
->iov
[i
].iov_len
);
364 static void quorum_count_vote(QuorumVotes
*votes
,
365 QuorumVoteValue
*value
,
368 QuorumVoteVersion
*v
= NULL
, *version
= NULL
;
369 QuorumVoteItem
*item
;
371 /* look if we have something with this hash */
372 QLIST_FOREACH(v
, &votes
->vote_list
, next
) {
373 if (votes
->compare(&v
->value
, value
)) {
379 /* It's a version not yet in the list add it */
381 version
= g_new0(QuorumVoteVersion
, 1);
382 QLIST_INIT(&version
->items
);
383 memcpy(&version
->value
, value
, sizeof(version
->value
));
384 version
->index
= index
;
385 version
->vote_count
= 0;
386 QLIST_INSERT_HEAD(&votes
->vote_list
, version
, next
);
389 version
->vote_count
++;
391 item
= g_new0(QuorumVoteItem
, 1);
393 QLIST_INSERT_HEAD(&version
->items
, item
, next
);
396 static void quorum_free_vote_list(QuorumVotes
*votes
)
398 QuorumVoteVersion
*version
, *next_version
;
399 QuorumVoteItem
*item
, *next_item
;
401 QLIST_FOREACH_SAFE(version
, &votes
->vote_list
, next
, next_version
) {
402 QLIST_REMOVE(version
, next
);
403 QLIST_FOREACH_SAFE(item
, &version
->items
, next
, next_item
) {
404 QLIST_REMOVE(item
, next
);
411 static int quorum_compute_hash(QuorumAIOCB
*acb
, int i
, QuorumVoteValue
*hash
)
414 gnutls_hash_hd_t dig
;
415 QEMUIOVector
*qiov
= &acb
->qcrs
[i
].qiov
;
417 ret
= gnutls_hash_init(&dig
, GNUTLS_DIG_SHA256
);
423 for (j
= 0; j
< qiov
->niov
; j
++) {
424 ret
= gnutls_hash(dig
, qiov
->iov
[j
].iov_base
, qiov
->iov
[j
].iov_len
);
430 gnutls_hash_deinit(dig
, (void *) hash
);
434 static QuorumVoteVersion
*quorum_get_vote_winner(QuorumVotes
*votes
)
437 QuorumVoteVersion
*candidate
, *winner
= NULL
;
439 QLIST_FOREACH(candidate
, &votes
->vote_list
, next
) {
440 if (candidate
->vote_count
> max
) {
441 max
= candidate
->vote_count
;
449 /* qemu_iovec_compare is handy for blkverify mode because it returns the first
450 * differing byte location. Yet it is handcoded to compare vectors one byte
451 * after another so it does not benefit from the libc SIMD optimizations.
452 * quorum_iovec_compare is written for speed and should be used in the non
453 * blkverify mode of quorum.
455 static bool quorum_iovec_compare(QEMUIOVector
*a
, QEMUIOVector
*b
)
460 assert(a
->niov
== b
->niov
);
461 for (i
= 0; i
< a
->niov
; i
++) {
462 assert(a
->iov
[i
].iov_len
== b
->iov
[i
].iov_len
);
463 result
= memcmp(a
->iov
[i
].iov_base
,
474 static void GCC_FMT_ATTR(2, 3) quorum_err(QuorumAIOCB
*acb
,
475 const char *fmt
, ...)
480 fprintf(stderr
, "quorum: sector_num=%" PRId64
" nb_sectors=%d ",
481 acb
->sector_num
, acb
->nb_sectors
);
482 vfprintf(stderr
, fmt
, ap
);
483 fprintf(stderr
, "\n");
488 static bool quorum_compare(QuorumAIOCB
*acb
,
492 BDRVQuorumState
*s
= acb
->common
.bs
->opaque
;
495 /* This driver will replace blkverify in this particular case */
496 if (s
->is_blkverify
) {
497 offset
= qemu_iovec_compare(a
, b
);
499 quorum_err(acb
, "contents mismatch in sector %" PRId64
,
501 (uint64_t)(offset
/ BDRV_SECTOR_SIZE
));
506 return quorum_iovec_compare(a
, b
);
509 /* Do a vote to get the error code */
510 static int quorum_vote_error(QuorumAIOCB
*acb
)
512 BDRVQuorumState
*s
= acb
->common
.bs
->opaque
;
513 QuorumVoteVersion
*winner
= NULL
;
514 QuorumVotes error_votes
;
515 QuorumVoteValue result_value
;
519 QLIST_INIT(&error_votes
.vote_list
);
520 error_votes
.compare
= quorum_64bits_compare
;
522 for (i
= 0; i
< s
->num_children
; i
++) {
523 ret
= acb
->qcrs
[i
].ret
;
526 result_value
.l
= ret
;
527 quorum_count_vote(&error_votes
, &result_value
, i
);
532 winner
= quorum_get_vote_winner(&error_votes
);
533 ret
= winner
->value
.l
;
536 quorum_free_vote_list(&error_votes
);
541 static bool quorum_vote(QuorumAIOCB
*acb
)
544 bool rewrite
= false;
546 QuorumVoteValue hash
;
547 BDRVQuorumState
*s
= acb
->common
.bs
->opaque
;
548 QuorumVoteVersion
*winner
;
550 if (quorum_has_too_much_io_failed(acb
)) {
554 /* get the index of the first successful read */
555 for (i
= 0; i
< s
->num_children
; i
++) {
556 if (!acb
->qcrs
[i
].ret
) {
561 assert(i
< s
->num_children
);
563 /* compare this read with all other successful reads stopping at quorum
566 for (j
= i
+ 1; j
< s
->num_children
; j
++) {
567 if (acb
->qcrs
[j
].ret
) {
570 quorum
= quorum_compare(acb
, &acb
->qcrs
[i
].qiov
, &acb
->qcrs
[j
].qiov
);
576 /* Every successful read agrees */
578 quorum_copy_qiov(acb
->qiov
, &acb
->qcrs
[i
].qiov
);
582 /* compute hashes for each successful read, also store indexes */
583 for (i
= 0; i
< s
->num_children
; i
++) {
584 if (acb
->qcrs
[i
].ret
) {
587 ret
= quorum_compute_hash(acb
, i
, &hash
);
588 /* if ever the hash computation failed */
593 quorum_count_vote(&acb
->votes
, &hash
, i
);
596 /* vote to select the most represented version */
597 winner
= quorum_get_vote_winner(&acb
->votes
);
599 /* if the winner count is smaller than threshold the read fails */
600 if (winner
->vote_count
< s
->threshold
) {
601 quorum_report_failure(acb
);
602 acb
->vote_ret
= -EIO
;
606 /* we have a winner: copy it */
607 quorum_copy_qiov(acb
->qiov
, &acb
->qcrs
[winner
->index
].qiov
);
609 /* some versions are bad print them */
610 quorum_report_bad_versions(s
, acb
, &winner
->value
);
612 /* corruption correction is enabled */
613 if (s
->rewrite_corrupted
) {
614 rewrite
= quorum_rewrite_bad_versions(s
, acb
, &winner
->value
);
619 quorum_free_vote_list(&acb
->votes
);
623 static BlockDriverAIOCB
*quorum_aio_readv(BlockDriverState
*bs
,
627 BlockDriverCompletionFunc
*cb
,
630 BDRVQuorumState
*s
= bs
->opaque
;
631 QuorumAIOCB
*acb
= quorum_aio_get(s
, bs
, qiov
, sector_num
,
632 nb_sectors
, cb
, opaque
);
637 for (i
= 0; i
< s
->num_children
; i
++) {
638 acb
->qcrs
[i
].buf
= qemu_blockalign(s
->bs
[i
], qiov
->size
);
639 qemu_iovec_init(&acb
->qcrs
[i
].qiov
, qiov
->niov
);
640 qemu_iovec_clone(&acb
->qcrs
[i
].qiov
, qiov
, acb
->qcrs
[i
].buf
);
643 for (i
= 0; i
< s
->num_children
; i
++) {
644 bdrv_aio_readv(s
->bs
[i
], sector_num
, &acb
->qcrs
[i
].qiov
, nb_sectors
,
645 quorum_aio_cb
, &acb
->qcrs
[i
]);
651 static BlockDriverAIOCB
*quorum_aio_writev(BlockDriverState
*bs
,
655 BlockDriverCompletionFunc
*cb
,
658 BDRVQuorumState
*s
= bs
->opaque
;
659 QuorumAIOCB
*acb
= quorum_aio_get(s
, bs
, qiov
, sector_num
, nb_sectors
,
663 for (i
= 0; i
< s
->num_children
; i
++) {
664 acb
->qcrs
[i
].aiocb
= bdrv_aio_writev(s
->bs
[i
], sector_num
, qiov
,
665 nb_sectors
, &quorum_aio_cb
,
672 static int64_t quorum_getlength(BlockDriverState
*bs
)
674 BDRVQuorumState
*s
= bs
->opaque
;
678 /* check that all file have the same length */
679 result
= bdrv_getlength(s
->bs
[0]);
683 for (i
= 1; i
< s
->num_children
; i
++) {
684 int64_t value
= bdrv_getlength(s
->bs
[i
]);
688 if (value
!= result
) {
696 static void quorum_invalidate_cache(BlockDriverState
*bs
, Error
**errp
)
698 BDRVQuorumState
*s
= bs
->opaque
;
699 Error
*local_err
= NULL
;
702 for (i
= 0; i
< s
->num_children
; i
++) {
703 bdrv_invalidate_cache(s
->bs
[i
], &local_err
);
705 error_propagate(errp
, local_err
);
711 static coroutine_fn
int quorum_co_flush(BlockDriverState
*bs
)
713 BDRVQuorumState
*s
= bs
->opaque
;
714 QuorumVoteVersion
*winner
= NULL
;
715 QuorumVotes error_votes
;
716 QuorumVoteValue result_value
;
720 QLIST_INIT(&error_votes
.vote_list
);
721 error_votes
.compare
= quorum_64bits_compare
;
723 for (i
= 0; i
< s
->num_children
; i
++) {
724 result
= bdrv_co_flush(s
->bs
[i
]);
725 result_value
.l
= result
;
726 quorum_count_vote(&error_votes
, &result_value
, i
);
729 winner
= quorum_get_vote_winner(&error_votes
);
730 result
= winner
->value
.l
;
732 quorum_free_vote_list(&error_votes
);
737 static bool quorum_recurse_is_first_non_filter(BlockDriverState
*bs
,
738 BlockDriverState
*candidate
)
740 BDRVQuorumState
*s
= bs
->opaque
;
743 for (i
= 0; i
< s
->num_children
; i
++) {
744 bool perm
= bdrv_recurse_is_first_non_filter(s
->bs
[i
],
754 static int quorum_valid_threshold(int threshold
, int num_children
, Error
**errp
)
758 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
,
759 "vote-threshold", "value >= 1");
763 if (threshold
> num_children
) {
764 error_setg(errp
, "threshold may not exceed children count");
771 static QemuOptsList quorum_runtime_opts
= {
773 .head
= QTAILQ_HEAD_INITIALIZER(quorum_runtime_opts
.head
),
776 .name
= QUORUM_OPT_VOTE_THRESHOLD
,
777 .type
= QEMU_OPT_NUMBER
,
778 .help
= "The number of vote needed for reaching quorum",
781 .name
= QUORUM_OPT_BLKVERIFY
,
782 .type
= QEMU_OPT_BOOL
,
783 .help
= "Trigger block verify mode if set",
786 .name
= QUORUM_OPT_REWRITE
,
787 .type
= QEMU_OPT_BOOL
,
788 .help
= "Rewrite corrupted block on read quorum",
790 { /* end of list */ }
794 static int quorum_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
797 BDRVQuorumState
*s
= bs
->opaque
;
798 Error
*local_err
= NULL
;
803 const QListEntry
*lentry
;
807 qdict_flatten(options
);
808 qdict_extract_subqdict(options
, &sub
, "children.");
809 qdict_array_split(sub
, &list
);
811 if (qdict_size(sub
)) {
812 error_setg(&local_err
, "Invalid option children.%s",
813 qdict_first(sub
)->key
);
818 /* count how many different children are present */
819 s
->num_children
= qlist_size(list
);
820 if (s
->num_children
< 2) {
821 error_setg(&local_err
,
822 "Number of provided children must be greater than 1");
827 opts
= qemu_opts_create(&quorum_runtime_opts
, NULL
, 0, &error_abort
);
828 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
834 s
->threshold
= qemu_opt_get_number(opts
, QUORUM_OPT_VOTE_THRESHOLD
, 0);
836 /* and validate it against s->num_children */
837 ret
= quorum_valid_threshold(s
->threshold
, s
->num_children
, &local_err
);
842 /* is the driver in blkverify mode */
843 if (qemu_opt_get_bool(opts
, QUORUM_OPT_BLKVERIFY
, false) &&
844 s
->num_children
== 2 && s
->threshold
== 2) {
845 s
->is_blkverify
= true;
846 } else if (qemu_opt_get_bool(opts
, QUORUM_OPT_BLKVERIFY
, false)) {
847 fprintf(stderr
, "blkverify mode is set by setting blkverify=on "
848 "and using two files with vote_threshold=2\n");
851 s
->rewrite_corrupted
= qemu_opt_get_bool(opts
, QUORUM_OPT_REWRITE
, false);
852 if (s
->rewrite_corrupted
&& s
->is_blkverify
) {
853 error_setg(&local_err
,
854 "rewrite-corrupted=on cannot be used with blkverify=on");
859 /* allocate the children BlockDriverState array */
860 s
->bs
= g_new0(BlockDriverState
*, s
->num_children
);
861 opened
= g_new0(bool, s
->num_children
);
863 for (i
= 0, lentry
= qlist_first(list
); lentry
;
864 lentry
= qlist_next(lentry
), i
++) {
868 switch (qobject_type(lentry
->value
))
870 /* List of options */
872 d
= qobject_to_qdict(lentry
->value
);
874 ret
= bdrv_open(&s
->bs
[i
], NULL
, NULL
, d
, flags
, NULL
,
880 string
= qobject_to_qstring(lentry
->value
);
881 ret
= bdrv_open(&s
->bs
[i
], NULL
, qstring_get_str(string
), NULL
,
882 flags
, NULL
, &local_err
);
886 error_setg(&local_err
, "Specification of child block device %i "
901 /* cleanup on error */
902 for (i
= 0; i
< s
->num_children
; i
++) {
906 bdrv_unref(s
->bs
[i
]);
911 /* propagate error */
913 error_propagate(errp
, local_err
);
920 static void quorum_close(BlockDriverState
*bs
)
922 BDRVQuorumState
*s
= bs
->opaque
;
925 for (i
= 0; i
< s
->num_children
; i
++) {
926 bdrv_unref(s
->bs
[i
]);
932 static void quorum_detach_aio_context(BlockDriverState
*bs
)
934 BDRVQuorumState
*s
= bs
->opaque
;
937 for (i
= 0; i
< s
->num_children
; i
++) {
938 bdrv_detach_aio_context(s
->bs
[i
]);
942 static void quorum_attach_aio_context(BlockDriverState
*bs
,
943 AioContext
*new_context
)
945 BDRVQuorumState
*s
= bs
->opaque
;
948 for (i
= 0; i
< s
->num_children
; i
++) {
949 bdrv_attach_aio_context(s
->bs
[i
], new_context
);
953 static void quorum_refresh_filename(BlockDriverState
*bs
)
955 BDRVQuorumState
*s
= bs
->opaque
;
960 for (i
= 0; i
< s
->num_children
; i
++) {
961 bdrv_refresh_filename(s
->bs
[i
]);
962 if (!s
->bs
[i
]->full_open_options
) {
967 children
= qlist_new();
968 for (i
= 0; i
< s
->num_children
; i
++) {
969 QINCREF(s
->bs
[i
]->full_open_options
);
970 qlist_append_obj(children
, QOBJECT(s
->bs
[i
]->full_open_options
));
974 qdict_put_obj(opts
, "driver", QOBJECT(qstring_from_str("quorum")));
975 qdict_put_obj(opts
, QUORUM_OPT_VOTE_THRESHOLD
,
976 QOBJECT(qint_from_int(s
->threshold
)));
977 qdict_put_obj(opts
, QUORUM_OPT_BLKVERIFY
,
978 QOBJECT(qbool_from_int(s
->is_blkverify
)));
979 qdict_put_obj(opts
, QUORUM_OPT_REWRITE
,
980 QOBJECT(qbool_from_int(s
->rewrite_corrupted
)));
981 qdict_put_obj(opts
, "children", QOBJECT(children
));
983 bs
->full_open_options
= opts
;
986 static BlockDriver bdrv_quorum
= {
987 .format_name
= "quorum",
988 .protocol_name
= "quorum",
990 .instance_size
= sizeof(BDRVQuorumState
),
992 .bdrv_file_open
= quorum_open
,
993 .bdrv_close
= quorum_close
,
994 .bdrv_refresh_filename
= quorum_refresh_filename
,
996 .bdrv_co_flush_to_disk
= quorum_co_flush
,
998 .bdrv_getlength
= quorum_getlength
,
1000 .bdrv_aio_readv
= quorum_aio_readv
,
1001 .bdrv_aio_writev
= quorum_aio_writev
,
1002 .bdrv_invalidate_cache
= quorum_invalidate_cache
,
1004 .bdrv_detach_aio_context
= quorum_detach_aio_context
,
1005 .bdrv_attach_aio_context
= quorum_attach_aio_context
,
1008 .bdrv_recurse_is_first_non_filter
= quorum_recurse_is_first_non_filter
,
1011 static void bdrv_quorum_init(void)
1013 bdrv_register(&bdrv_quorum
);
1016 block_init(bdrv_quorum_init
);