]> git.proxmox.com Git - mirror_qemu.git/blame - block/quorum.c
virtio: decrement vq->inuse in virtqueue_discard()
[mirror_qemu.git] / block / quorum.c
CommitLineData
27cec15e
BC
1/*
2 * Quorum Block filter
3 *
4 * Copyright (C) 2012-2014 Nodalink, EURL.
5 *
6 * Author:
7 * Benoît Canet <benoit.canet@irqsave.net>
8 *
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).
11 *
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.
14 */
15
80c71a24 16#include "qemu/osdep.h"
98292c61 17#include "qemu/cutils.h"
27cec15e 18#include "block/block_int.h"
fafcfe22
HR
19#include "qapi/qmp/qbool.h"
20#include "qapi/qmp/qdict.h"
cc7a8ea7 21#include "qapi/qmp/qerror.h"
fafcfe22 22#include "qapi/qmp/qint.h"
95c6bff3 23#include "qapi/qmp/qjson.h"
fafcfe22
HR
24#include "qapi/qmp/qlist.h"
25#include "qapi/qmp/qstring.h"
fe069d9d 26#include "qapi-event.h"
488981a4 27#include "crypto/hash.h"
95c6bff3
BC
28
29#define HASH_LENGTH 32
30
c88a1de5
BC
31#define QUORUM_OPT_VOTE_THRESHOLD "vote-threshold"
32#define QUORUM_OPT_BLKVERIFY "blkverify"
cf29a570 33#define QUORUM_OPT_REWRITE "rewrite-corrupted"
a9db86b2 34#define QUORUM_OPT_READ_PATTERN "read-pattern"
c88a1de5 35
95c6bff3
BC
36/* This union holds a vote hash value */
37typedef union QuorumVoteValue {
488981a4 38 uint8_t h[HASH_LENGTH]; /* SHA-256 hash */
95c6bff3
BC
39 int64_t l; /* simpler 64 bits hash */
40} QuorumVoteValue;
41
42/* A vote item */
43typedef struct QuorumVoteItem {
44 int index;
45 QLIST_ENTRY(QuorumVoteItem) next;
46} QuorumVoteItem;
47
48/* this structure is a vote version. A version is the set of votes sharing the
49 * same vote value.
50 * The set of votes will be tracked with the items field and its cardinality is
51 * vote_count.
52 */
53typedef struct QuorumVoteVersion {
54 QuorumVoteValue value;
55 int index;
56 int vote_count;
57 QLIST_HEAD(, QuorumVoteItem) items;
58 QLIST_ENTRY(QuorumVoteVersion) next;
59} QuorumVoteVersion;
60
61/* this structure holds a group of vote versions together */
62typedef struct QuorumVotes {
63 QLIST_HEAD(, QuorumVoteVersion) vote_list;
64 bool (*compare)(QuorumVoteValue *a, QuorumVoteValue *b);
65} QuorumVotes;
27cec15e 66
cadebd7a
BC
67/* the following structure holds the state of one quorum instance */
68typedef struct BDRVQuorumState {
0bd6e91a 69 BdrvChild **children; /* children BlockDriverStates */
cadebd7a 70 int num_children; /* children count */
98292c61
WC
71 unsigned next_child_index; /* the index of the next child that should
72 * be added
73 */
cadebd7a
BC
74 int threshold; /* if less than threshold children reads gave the
75 * same result a quorum error occurs.
76 */
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.
84 */
cf29a570
BC
85 bool rewrite_corrupted;/* true if the driver must rewrite-on-read corrupted
86 * block if Quorum is reached.
87 */
a9db86b2
LY
88
89 QuorumReadPattern read_pattern;
cadebd7a
BC
90} BDRVQuorumState;
91
27cec15e
BC
92typedef struct QuorumAIOCB QuorumAIOCB;
93
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.
98 */
99typedef struct QuorumChildRequest {
7c84b1b8 100 BlockAIOCB *aiocb;
27cec15e
BC
101 QEMUIOVector qiov;
102 uint8_t *buf;
103 int ret;
104 QuorumAIOCB *parent;
105} QuorumChildRequest;
106
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.
111 */
112struct QuorumAIOCB {
7c84b1b8 113 BlockAIOCB common;
27cec15e
BC
114
115 /* Request metadata */
116 uint64_t sector_num;
117 int nb_sectors;
118
119 QEMUIOVector *qiov; /* calling IOV */
120
121 QuorumChildRequest *qcrs; /* individual child requests */
122 int count; /* number of completed AIOCB */
123 int success_count; /* number of successfully completed AIOCB */
124
cf29a570
BC
125 int rewrite_count; /* number of replica to rewrite: count down to
126 * zero once writes are fired
127 */
128
95c6bff3
BC
129 QuorumVotes votes;
130
27cec15e
BC
131 bool is_read;
132 int vote_ret;
a9db86b2 133 int child_iter; /* which child to read in fifo pattern */
27cec15e 134};
cadebd7a 135
cf29a570 136static bool quorum_vote(QuorumAIOCB *acb);
95c6bff3 137
7c84b1b8 138static void quorum_aio_cancel(BlockAIOCB *blockacb)
13e7956e
BC
139{
140 QuorumAIOCB *acb = container_of(blockacb, QuorumAIOCB, common);
141 BDRVQuorumState *s = acb->common.bs->opaque;
142 int i;
143
144 /* cancel all callbacks */
145 for (i = 0; i < s->num_children; i++) {
997dd8df 146 if (acb->qcrs[i].aiocb) {
7940e505 147 bdrv_aio_cancel_async(acb->qcrs[i].aiocb);
997dd8df 148 }
13e7956e 149 }
13e7956e
BC
150}
151
152static AIOCBInfo quorum_aiocb_info = {
153 .aiocb_size = sizeof(QuorumAIOCB),
7940e505 154 .cancel_async = quorum_aio_cancel,
13e7956e
BC
155};
156
157static void quorum_aio_finalize(QuorumAIOCB *acb)
158{
7db6982a 159 int i, ret = 0;
13e7956e 160
95c6bff3
BC
161 if (acb->vote_ret) {
162 ret = acb->vote_ret;
163 }
164
13e7956e
BC
165 acb->common.cb(acb->common.opaque, ret);
166
7db6982a 167 if (acb->is_read) {
a9db86b2
LY
168 /* on the quorum case acb->child_iter == s->num_children - 1 */
169 for (i = 0; i <= acb->child_iter; i++) {
7db6982a
BC
170 qemu_vfree(acb->qcrs[i].buf);
171 qemu_iovec_destroy(&acb->qcrs[i].qiov);
172 }
173 }
174
13e7956e 175 g_free(acb->qcrs);
8007429a 176 qemu_aio_unref(acb);
13e7956e
BC
177}
178
95c6bff3
BC
179static bool quorum_sha256_compare(QuorumVoteValue *a, QuorumVoteValue *b)
180{
181 return !memcmp(a->h, b->h, HASH_LENGTH);
182}
183
184static bool quorum_64bits_compare(QuorumVoteValue *a, QuorumVoteValue *b)
185{
186 return a->l == b->l;
187}
188
13e7956e
BC
189static QuorumAIOCB *quorum_aio_get(BDRVQuorumState *s,
190 BlockDriverState *bs,
191 QEMUIOVector *qiov,
192 uint64_t sector_num,
193 int nb_sectors,
097310b5 194 BlockCompletionFunc *cb,
13e7956e
BC
195 void *opaque)
196{
197 QuorumAIOCB *acb = qemu_aio_get(&quorum_aiocb_info, bs, cb, opaque);
198 int i;
199
200 acb->common.bs->opaque = s;
201 acb->sector_num = sector_num;
202 acb->nb_sectors = nb_sectors;
203 acb->qiov = qiov;
204 acb->qcrs = g_new0(QuorumChildRequest, s->num_children);
205 acb->count = 0;
206 acb->success_count = 0;
cf29a570 207 acb->rewrite_count = 0;
95c6bff3
BC
208 acb->votes.compare = quorum_sha256_compare;
209 QLIST_INIT(&acb->votes.vote_list);
13e7956e
BC
210 acb->is_read = false;
211 acb->vote_ret = 0;
212
213 for (i = 0; i < s->num_children; i++) {
214 acb->qcrs[i].buf = NULL;
215 acb->qcrs[i].ret = 0;
216 acb->qcrs[i].parent = acb;
217 }
218
219 return acb;
220}
221
0ae053b7
CX
222static void quorum_report_bad(QuorumOpType type, uint64_t sector_num,
223 int nb_sectors, char *node_name, int ret)
95c6bff3 224{
fe069d9d 225 const char *msg = NULL;
0c762736 226 if (ret < 0) {
fe069d9d 227 msg = strerror(-ret);
0c762736 228 }
0ae053b7
CX
229
230 qapi_event_send_quorum_report_bad(type, !!msg, msg, node_name,
231 sector_num, nb_sectors, &error_abort);
95c6bff3
BC
232}
233
234static void quorum_report_failure(QuorumAIOCB *acb)
235{
9b2aa84f 236 const char *reference = bdrv_get_device_or_node_name(acb->common.bs);
fe069d9d
WX
237 qapi_event_send_quorum_failure(reference, acb->sector_num,
238 acb->nb_sectors, &error_abort);
95c6bff3
BC
239}
240
241static int quorum_vote_error(QuorumAIOCB *acb);
242
243static bool quorum_has_too_much_io_failed(QuorumAIOCB *acb)
244{
245 BDRVQuorumState *s = acb->common.bs->opaque;
246
247 if (acb->success_count < s->threshold) {
248 acb->vote_ret = quorum_vote_error(acb);
249 quorum_report_failure(acb);
250 return true;
251 }
252
253 return false;
254}
255
cf29a570
BC
256static void quorum_rewrite_aio_cb(void *opaque, int ret)
257{
258 QuorumAIOCB *acb = opaque;
259
260 /* one less rewrite to do */
261 acb->rewrite_count--;
262
263 /* wait until all rewrite callbacks have completed */
264 if (acb->rewrite_count) {
265 return;
266 }
267
268 quorum_aio_finalize(acb);
269}
270
7c84b1b8 271static BlockAIOCB *read_fifo_child(QuorumAIOCB *acb);
a9db86b2
LY
272
273static void quorum_copy_qiov(QEMUIOVector *dest, QEMUIOVector *source)
274{
275 int i;
276 assert(dest->niov == source->niov);
277 assert(dest->size == source->size);
278 for (i = 0; i < source->niov; i++) {
279 assert(dest->iov[i].iov_len == source->iov[i].iov_len);
280 memcpy(dest->iov[i].iov_base,
281 source->iov[i].iov_base,
282 source->iov[i].iov_len);
283 }
284}
285
13e7956e
BC
286static void quorum_aio_cb(void *opaque, int ret)
287{
288 QuorumChildRequest *sacb = opaque;
289 QuorumAIOCB *acb = sacb->parent;
290 BDRVQuorumState *s = acb->common.bs->opaque;
cf29a570 291 bool rewrite = false;
13e7956e 292
6049490d
AG
293 if (ret == 0) {
294 acb->success_count++;
295 } else {
296 QuorumOpType type;
297 type = acb->is_read ? QUORUM_OP_TYPE_READ : QUORUM_OP_TYPE_WRITE;
298 quorum_report_bad(type, acb->sector_num, acb->nb_sectors,
299 sacb->aiocb->bs->node_name, ret);
300 }
301
a9db86b2
LY
302 if (acb->is_read && s->read_pattern == QUORUM_READ_PATTERN_FIFO) {
303 /* We try to read next child in FIFO order if we fail to read */
f38738e2
CX
304 if (ret < 0 && (acb->child_iter + 1) < s->num_children) {
305 acb->child_iter++;
a9db86b2
LY
306 read_fifo_child(acb);
307 return;
308 }
309
310 if (ret == 0) {
311 quorum_copy_qiov(acb->qiov, &acb->qcrs[acb->child_iter].qiov);
312 }
313 acb->vote_ret = ret;
314 quorum_aio_finalize(acb);
315 return;
316 }
317
13e7956e
BC
318 sacb->ret = ret;
319 acb->count++;
13e7956e
BC
320 assert(acb->count <= s->num_children);
321 assert(acb->success_count <= s->num_children);
322 if (acb->count < s->num_children) {
323 return;
324 }
325
95c6bff3
BC
326 /* Do the vote on read */
327 if (acb->is_read) {
cf29a570 328 rewrite = quorum_vote(acb);
95c6bff3
BC
329 } else {
330 quorum_has_too_much_io_failed(acb);
331 }
332
cf29a570
BC
333 /* if no rewrite is done the code will finish right away */
334 if (!rewrite) {
335 quorum_aio_finalize(acb);
336 }
13e7956e
BC
337}
338
95c6bff3
BC
339static void quorum_report_bad_versions(BDRVQuorumState *s,
340 QuorumAIOCB *acb,
341 QuorumVoteValue *value)
342{
343 QuorumVoteVersion *version;
344 QuorumVoteItem *item;
345
346 QLIST_FOREACH(version, &acb->votes.vote_list, next) {
347 if (acb->votes.compare(&version->value, value)) {
348 continue;
349 }
350 QLIST_FOREACH(item, &version->items, next) {
0ae053b7
CX
351 quorum_report_bad(QUORUM_OP_TYPE_READ, acb->sector_num,
352 acb->nb_sectors,
353 s->children[item->index]->bs->node_name, 0);
95c6bff3
BC
354 }
355 }
356}
357
cf29a570
BC
358static bool quorum_rewrite_bad_versions(BDRVQuorumState *s, QuorumAIOCB *acb,
359 QuorumVoteValue *value)
360{
361 QuorumVoteVersion *version;
362 QuorumVoteItem *item;
363 int count = 0;
364
365 /* first count the number of bad versions: done first to avoid concurrency
366 * issues.
367 */
368 QLIST_FOREACH(version, &acb->votes.vote_list, next) {
369 if (acb->votes.compare(&version->value, value)) {
370 continue;
371 }
372 QLIST_FOREACH(item, &version->items, next) {
373 count++;
374 }
375 }
376
377 /* quorum_rewrite_aio_cb will count down this to zero */
378 acb->rewrite_count = count;
379
380 /* now fire the correcting rewrites */
381 QLIST_FOREACH(version, &acb->votes.vote_list, next) {
382 if (acb->votes.compare(&version->value, value)) {
383 continue;
384 }
385 QLIST_FOREACH(item, &version->items, next) {
0d1049c7 386 bdrv_aio_writev(s->children[item->index], acb->sector_num,
0bd6e91a
KW
387 acb->qiov, acb->nb_sectors, quorum_rewrite_aio_cb,
388 acb);
cf29a570
BC
389 }
390 }
391
392 /* return true if any rewrite is done else false */
393 return count;
394}
395
95c6bff3
BC
396static void quorum_count_vote(QuorumVotes *votes,
397 QuorumVoteValue *value,
398 int index)
399{
400 QuorumVoteVersion *v = NULL, *version = NULL;
401 QuorumVoteItem *item;
402
403 /* look if we have something with this hash */
404 QLIST_FOREACH(v, &votes->vote_list, next) {
405 if (votes->compare(&v->value, value)) {
406 version = v;
407 break;
408 }
409 }
410
411 /* It's a version not yet in the list add it */
412 if (!version) {
413 version = g_new0(QuorumVoteVersion, 1);
414 QLIST_INIT(&version->items);
415 memcpy(&version->value, value, sizeof(version->value));
416 version->index = index;
417 version->vote_count = 0;
418 QLIST_INSERT_HEAD(&votes->vote_list, version, next);
419 }
420
421 version->vote_count++;
422
423 item = g_new0(QuorumVoteItem, 1);
424 item->index = index;
425 QLIST_INSERT_HEAD(&version->items, item, next);
426}
427
428static void quorum_free_vote_list(QuorumVotes *votes)
429{
430 QuorumVoteVersion *version, *next_version;
431 QuorumVoteItem *item, *next_item;
432
433 QLIST_FOREACH_SAFE(version, &votes->vote_list, next, next_version) {
434 QLIST_REMOVE(version, next);
435 QLIST_FOREACH_SAFE(item, &version->items, next, next_item) {
436 QLIST_REMOVE(item, next);
437 g_free(item);
438 }
439 g_free(version);
440 }
441}
442
443static int quorum_compute_hash(QuorumAIOCB *acb, int i, QuorumVoteValue *hash)
444{
95c6bff3 445 QEMUIOVector *qiov = &acb->qcrs[i].qiov;
488981a4
DB
446 size_t len = sizeof(hash->h);
447 uint8_t *data = hash->h;
95c6bff3 448
488981a4
DB
449 /* XXX - would be nice if we could pass in the Error **
450 * and propagate that back, but this quorum code is
451 * restricted to just errno values currently */
452 if (qcrypto_hash_bytesv(QCRYPTO_HASH_ALG_SHA256,
453 qiov->iov, qiov->niov,
454 &data, &len,
455 NULL) < 0) {
456 return -EINVAL;
95c6bff3
BC
457 }
458
488981a4 459 return 0;
95c6bff3
BC
460}
461
462static QuorumVoteVersion *quorum_get_vote_winner(QuorumVotes *votes)
463{
464 int max = 0;
465 QuorumVoteVersion *candidate, *winner = NULL;
466
467 QLIST_FOREACH(candidate, &votes->vote_list, next) {
468 if (candidate->vote_count > max) {
469 max = candidate->vote_count;
470 winner = candidate;
471 }
472 }
473
474 return winner;
475}
476
477/* qemu_iovec_compare is handy for blkverify mode because it returns the first
478 * differing byte location. Yet it is handcoded to compare vectors one byte
479 * after another so it does not benefit from the libc SIMD optimizations.
480 * quorum_iovec_compare is written for speed and should be used in the non
481 * blkverify mode of quorum.
482 */
483static bool quorum_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
484{
485 int i;
486 int result;
487
488 assert(a->niov == b->niov);
489 for (i = 0; i < a->niov; i++) {
490 assert(a->iov[i].iov_len == b->iov[i].iov_len);
491 result = memcmp(a->iov[i].iov_base,
492 b->iov[i].iov_base,
493 a->iov[i].iov_len);
494 if (result) {
495 return false;
496 }
497 }
498
499 return true;
500}
501
502static void GCC_FMT_ATTR(2, 3) quorum_err(QuorumAIOCB *acb,
503 const char *fmt, ...)
504{
505 va_list ap;
506
507 va_start(ap, fmt);
508 fprintf(stderr, "quorum: sector_num=%" PRId64 " nb_sectors=%d ",
509 acb->sector_num, acb->nb_sectors);
510 vfprintf(stderr, fmt, ap);
511 fprintf(stderr, "\n");
512 va_end(ap);
513 exit(1);
514}
515
516static bool quorum_compare(QuorumAIOCB *acb,
517 QEMUIOVector *a,
518 QEMUIOVector *b)
519{
520 BDRVQuorumState *s = acb->common.bs->opaque;
521 ssize_t offset;
522
523 /* This driver will replace blkverify in this particular case */
524 if (s->is_blkverify) {
525 offset = qemu_iovec_compare(a, b);
526 if (offset != -1) {
527 quorum_err(acb, "contents mismatch in sector %" PRId64,
528 acb->sector_num +
529 (uint64_t)(offset / BDRV_SECTOR_SIZE));
530 }
531 return true;
532 }
533
534 return quorum_iovec_compare(a, b);
535}
536
537/* Do a vote to get the error code */
538static int quorum_vote_error(QuorumAIOCB *acb)
539{
540 BDRVQuorumState *s = acb->common.bs->opaque;
541 QuorumVoteVersion *winner = NULL;
542 QuorumVotes error_votes;
543 QuorumVoteValue result_value;
544 int i, ret = 0;
545 bool error = false;
546
547 QLIST_INIT(&error_votes.vote_list);
548 error_votes.compare = quorum_64bits_compare;
549
550 for (i = 0; i < s->num_children; i++) {
551 ret = acb->qcrs[i].ret;
552 if (ret) {
553 error = true;
554 result_value.l = ret;
555 quorum_count_vote(&error_votes, &result_value, i);
556 }
557 }
558
559 if (error) {
560 winner = quorum_get_vote_winner(&error_votes);
561 ret = winner->value.l;
562 }
563
564 quorum_free_vote_list(&error_votes);
565
566 return ret;
567}
568
cf29a570 569static bool quorum_vote(QuorumAIOCB *acb)
95c6bff3
BC
570{
571 bool quorum = true;
cf29a570 572 bool rewrite = false;
95c6bff3
BC
573 int i, j, ret;
574 QuorumVoteValue hash;
575 BDRVQuorumState *s = acb->common.bs->opaque;
576 QuorumVoteVersion *winner;
577
578 if (quorum_has_too_much_io_failed(acb)) {
cf29a570 579 return false;
95c6bff3
BC
580 }
581
582 /* get the index of the first successful read */
583 for (i = 0; i < s->num_children; i++) {
584 if (!acb->qcrs[i].ret) {
585 break;
586 }
587 }
588
589 assert(i < s->num_children);
590
591 /* compare this read with all other successful reads stopping at quorum
592 * failure
593 */
594 for (j = i + 1; j < s->num_children; j++) {
595 if (acb->qcrs[j].ret) {
596 continue;
597 }
598 quorum = quorum_compare(acb, &acb->qcrs[i].qiov, &acb->qcrs[j].qiov);
599 if (!quorum) {
600 break;
601 }
602 }
603
604 /* Every successful read agrees */
605 if (quorum) {
606 quorum_copy_qiov(acb->qiov, &acb->qcrs[i].qiov);
cf29a570 607 return false;
95c6bff3
BC
608 }
609
610 /* compute hashes for each successful read, also store indexes */
611 for (i = 0; i < s->num_children; i++) {
612 if (acb->qcrs[i].ret) {
613 continue;
614 }
615 ret = quorum_compute_hash(acb, i, &hash);
616 /* if ever the hash computation failed */
617 if (ret < 0) {
618 acb->vote_ret = ret;
619 goto free_exit;
620 }
621 quorum_count_vote(&acb->votes, &hash, i);
622 }
623
624 /* vote to select the most represented version */
625 winner = quorum_get_vote_winner(&acb->votes);
626
627 /* if the winner count is smaller than threshold the read fails */
628 if (winner->vote_count < s->threshold) {
629 quorum_report_failure(acb);
630 acb->vote_ret = -EIO;
631 goto free_exit;
632 }
633
634 /* we have a winner: copy it */
635 quorum_copy_qiov(acb->qiov, &acb->qcrs[winner->index].qiov);
636
637 /* some versions are bad print them */
638 quorum_report_bad_versions(s, acb, &winner->value);
639
cf29a570
BC
640 /* corruption correction is enabled */
641 if (s->rewrite_corrupted) {
642 rewrite = quorum_rewrite_bad_versions(s, acb, &winner->value);
643 }
644
95c6bff3
BC
645free_exit:
646 /* free lists */
647 quorum_free_vote_list(&acb->votes);
cf29a570 648 return rewrite;
95c6bff3
BC
649}
650
7c84b1b8 651static BlockAIOCB *read_quorum_children(QuorumAIOCB *acb)
7db6982a 652{
a9db86b2 653 BDRVQuorumState *s = acb->common.bs->opaque;
7db6982a
BC
654 int i;
655
7db6982a 656 for (i = 0; i < s->num_children; i++) {
0bd6e91a 657 acb->qcrs[i].buf = qemu_blockalign(s->children[i]->bs, acb->qiov->size);
a9db86b2
LY
658 qemu_iovec_init(&acb->qcrs[i].qiov, acb->qiov->niov);
659 qemu_iovec_clone(&acb->qcrs[i].qiov, acb->qiov, acb->qcrs[i].buf);
7db6982a
BC
660 }
661
662 for (i = 0; i < s->num_children; i++) {
ebb7af21 663 acb->qcrs[i].aiocb = bdrv_aio_readv(s->children[i], acb->sector_num,
b9c600d2
AG
664 &acb->qcrs[i].qiov, acb->nb_sectors,
665 quorum_aio_cb, &acb->qcrs[i]);
7db6982a
BC
666 }
667
668 return &acb->common;
669}
670
7c84b1b8 671static BlockAIOCB *read_fifo_child(QuorumAIOCB *acb)
a9db86b2
LY
672{
673 BDRVQuorumState *s = acb->common.bs->opaque;
674
0bd6e91a
KW
675 acb->qcrs[acb->child_iter].buf =
676 qemu_blockalign(s->children[acb->child_iter]->bs, acb->qiov->size);
a9db86b2
LY
677 qemu_iovec_init(&acb->qcrs[acb->child_iter].qiov, acb->qiov->niov);
678 qemu_iovec_clone(&acb->qcrs[acb->child_iter].qiov, acb->qiov,
679 acb->qcrs[acb->child_iter].buf);
b9c600d2 680 acb->qcrs[acb->child_iter].aiocb =
ebb7af21 681 bdrv_aio_readv(s->children[acb->child_iter], acb->sector_num,
b9c600d2
AG
682 &acb->qcrs[acb->child_iter].qiov, acb->nb_sectors,
683 quorum_aio_cb, &acb->qcrs[acb->child_iter]);
a9db86b2
LY
684
685 return &acb->common;
686}
687
7c84b1b8
MA
688static BlockAIOCB *quorum_aio_readv(BlockDriverState *bs,
689 int64_t sector_num,
690 QEMUIOVector *qiov,
691 int nb_sectors,
097310b5 692 BlockCompletionFunc *cb,
7c84b1b8 693 void *opaque)
a9db86b2
LY
694{
695 BDRVQuorumState *s = bs->opaque;
696 QuorumAIOCB *acb = quorum_aio_get(s, bs, qiov, sector_num,
697 nb_sectors, cb, opaque);
698 acb->is_read = true;
699
700 if (s->read_pattern == QUORUM_READ_PATTERN_QUORUM) {
701 acb->child_iter = s->num_children - 1;
702 return read_quorum_children(acb);
703 }
704
705 acb->child_iter = 0;
706 return read_fifo_child(acb);
707}
708
7c84b1b8
MA
709static BlockAIOCB *quorum_aio_writev(BlockDriverState *bs,
710 int64_t sector_num,
711 QEMUIOVector *qiov,
712 int nb_sectors,
097310b5 713 BlockCompletionFunc *cb,
7c84b1b8 714 void *opaque)
13e7956e
BC
715{
716 BDRVQuorumState *s = bs->opaque;
717 QuorumAIOCB *acb = quorum_aio_get(s, bs, qiov, sector_num, nb_sectors,
718 cb, opaque);
719 int i;
720
721 for (i = 0; i < s->num_children; i++) {
0d1049c7 722 acb->qcrs[i].aiocb = bdrv_aio_writev(s->children[i], sector_num,
0bd6e91a 723 qiov, nb_sectors, &quorum_aio_cb,
13e7956e
BC
724 &acb->qcrs[i]);
725 }
726
727 return &acb->common;
728}
729
d55dee20
BC
730static int64_t quorum_getlength(BlockDriverState *bs)
731{
732 BDRVQuorumState *s = bs->opaque;
733 int64_t result;
734 int i;
735
736 /* check that all file have the same length */
0bd6e91a 737 result = bdrv_getlength(s->children[0]->bs);
d55dee20
BC
738 if (result < 0) {
739 return result;
740 }
741 for (i = 1; i < s->num_children; i++) {
0bd6e91a 742 int64_t value = bdrv_getlength(s->children[i]->bs);
d55dee20
BC
743 if (value < 0) {
744 return value;
745 }
746 if (value != result) {
747 return -EIO;
748 }
749 }
750
751 return result;
752}
753
1c508d17
BC
754static coroutine_fn int quorum_co_flush(BlockDriverState *bs)
755{
756 BDRVQuorumState *s = bs->opaque;
757 QuorumVoteVersion *winner = NULL;
758 QuorumVotes error_votes;
759 QuorumVoteValue result_value;
760 int i;
761 int result = 0;
924e8a2b 762 int success_count = 0;
1c508d17
BC
763
764 QLIST_INIT(&error_votes.vote_list);
765 error_votes.compare = quorum_64bits_compare;
766
767 for (i = 0; i < s->num_children; i++) {
0bd6e91a 768 result = bdrv_co_flush(s->children[i]->bs);
924e8a2b
CX
769 if (result) {
770 quorum_report_bad(QUORUM_OP_TYPE_FLUSH, 0,
771 bdrv_nb_sectors(s->children[i]->bs),
772 s->children[i]->bs->node_name, result);
773 result_value.l = result;
774 quorum_count_vote(&error_votes, &result_value, i);
775 } else {
776 success_count++;
777 }
1c508d17
BC
778 }
779
924e8a2b
CX
780 if (success_count >= s->threshold) {
781 result = 0;
782 } else {
783 winner = quorum_get_vote_winner(&error_votes);
784 result = winner->value.l;
785 }
1c508d17
BC
786 quorum_free_vote_list(&error_votes);
787
788 return result;
789}
790
98a7a38f
BC
791static bool quorum_recurse_is_first_non_filter(BlockDriverState *bs,
792 BlockDriverState *candidate)
793{
794 BDRVQuorumState *s = bs->opaque;
795 int i;
796
797 for (i = 0; i < s->num_children; i++) {
0bd6e91a 798 bool perm = bdrv_recurse_is_first_non_filter(s->children[i]->bs,
98a7a38f
BC
799 candidate);
800 if (perm) {
801 return true;
802 }
803 }
804
805 return false;
806}
807
c88a1de5
BC
808static int quorum_valid_threshold(int threshold, int num_children, Error **errp)
809{
810
811 if (threshold < 1) {
c6bd8c70
MA
812 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
813 "vote-threshold", "value >= 1");
c88a1de5
BC
814 return -ERANGE;
815 }
816
817 if (threshold > num_children) {
818 error_setg(errp, "threshold may not exceed children count");
819 return -ERANGE;
820 }
821
822 return 0;
823}
824
825static QemuOptsList quorum_runtime_opts = {
826 .name = "quorum",
827 .head = QTAILQ_HEAD_INITIALIZER(quorum_runtime_opts.head),
828 .desc = {
829 {
830 .name = QUORUM_OPT_VOTE_THRESHOLD,
831 .type = QEMU_OPT_NUMBER,
832 .help = "The number of vote needed for reaching quorum",
833 },
834 {
835 .name = QUORUM_OPT_BLKVERIFY,
836 .type = QEMU_OPT_BOOL,
837 .help = "Trigger block verify mode if set",
838 },
cf29a570
BC
839 {
840 .name = QUORUM_OPT_REWRITE,
841 .type = QEMU_OPT_BOOL,
842 .help = "Rewrite corrupted block on read quorum",
843 },
a9db86b2
LY
844 {
845 .name = QUORUM_OPT_READ_PATTERN,
846 .type = QEMU_OPT_STRING,
847 .help = "Allowed pattern: quorum, fifo. Quorum is default",
848 },
c88a1de5
BC
849 { /* end of list */ }
850 },
851};
852
a9db86b2
LY
853static int parse_read_pattern(const char *opt)
854{
855 int i;
856
857 if (!opt) {
858 /* Set quorum as default */
859 return QUORUM_READ_PATTERN_QUORUM;
860 }
861
7fb1cf16 862 for (i = 0; i < QUORUM_READ_PATTERN__MAX; i++) {
a9db86b2
LY
863 if (!strcmp(opt, QuorumReadPattern_lookup[i])) {
864 return i;
865 }
866 }
867
868 return -EINVAL;
869}
870
c88a1de5
BC
871static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
872 Error **errp)
873{
874 BDRVQuorumState *s = bs->opaque;
875 Error *local_err = NULL;
8df3abfc 876 QemuOpts *opts = NULL;
c88a1de5 877 bool *opened;
c88a1de5
BC
878 int i;
879 int ret = 0;
880
881 qdict_flatten(options);
c88a1de5 882
ea6828d8
KW
883 /* count how many different children are present */
884 s->num_children = qdict_array_entries(options, "children.");
885 if (s->num_children < 0) {
886 error_setg(&local_err, "Option children is not a valid array");
8a87f3d7
HR
887 ret = -EINVAL;
888 goto exit;
889 }
98292c61 890 if (s->num_children < 1) {
c88a1de5 891 error_setg(&local_err,
98292c61 892 "Number of provided children must be 1 or more");
c88a1de5
BC
893 ret = -EINVAL;
894 goto exit;
895 }
896
897 opts = qemu_opts_create(&quorum_runtime_opts, NULL, 0, &error_abort);
898 qemu_opts_absorb_qdict(opts, options, &local_err);
0fb6395c 899 if (local_err) {
c88a1de5
BC
900 ret = -EINVAL;
901 goto exit;
902 }
903
904 s->threshold = qemu_opt_get_number(opts, QUORUM_OPT_VOTE_THRESHOLD, 0);
834cb2ad
WC
905 /* and validate it against s->num_children */
906 ret = quorum_valid_threshold(s->threshold, s->num_children, &local_err);
907 if (ret < 0) {
908 goto exit;
909 }
910
a9db86b2 911 ret = parse_read_pattern(qemu_opt_get(opts, QUORUM_OPT_READ_PATTERN));
c88a1de5 912 if (ret < 0) {
a9db86b2 913 error_setg(&local_err, "Please set read-pattern as fifo or quorum");
c88a1de5
BC
914 goto exit;
915 }
a9db86b2 916 s->read_pattern = ret;
c88a1de5 917
a9db86b2 918 if (s->read_pattern == QUORUM_READ_PATTERN_QUORUM) {
a9db86b2
LY
919 /* is the driver in blkverify mode */
920 if (qemu_opt_get_bool(opts, QUORUM_OPT_BLKVERIFY, false) &&
921 s->num_children == 2 && s->threshold == 2) {
922 s->is_blkverify = true;
923 } else if (qemu_opt_get_bool(opts, QUORUM_OPT_BLKVERIFY, false)) {
924 fprintf(stderr, "blkverify mode is set by setting blkverify=on "
925 "and using two files with vote_threshold=2\n");
926 }
927
928 s->rewrite_corrupted = qemu_opt_get_bool(opts, QUORUM_OPT_REWRITE,
929 false);
930 if (s->rewrite_corrupted && s->is_blkverify) {
931 error_setg(&local_err,
932 "rewrite-corrupted=on cannot be used with blkverify=on");
933 ret = -EINVAL;
934 goto exit;
935 }
cf29a570
BC
936 }
937
0bd6e91a
KW
938 /* allocate the children array */
939 s->children = g_new0(BdrvChild *, s->num_children);
c88a1de5
BC
940 opened = g_new0(bool, s->num_children);
941
ea6828d8
KW
942 for (i = 0; i < s->num_children; i++) {
943 char indexstr[32];
944 ret = snprintf(indexstr, 32, "children.%d", i);
945 assert(ret < 32);
8a87f3d7 946
0bd6e91a
KW
947 s->children[i] = bdrv_open_child(NULL, options, indexstr, bs,
948 &child_format, false, &local_err);
949 if (local_err) {
950 ret = -EINVAL;
8a87f3d7 951 goto close_exit;
c88a1de5 952 }
ea6828d8 953
8a87f3d7 954 opened[i] = true;
c88a1de5 955 }
98292c61 956 s->next_child_index = s->num_children;
c88a1de5
BC
957
958 g_free(opened);
959 goto exit;
960
961close_exit:
962 /* cleanup on error */
963 for (i = 0; i < s->num_children; i++) {
964 if (!opened[i]) {
965 continue;
966 }
0bd6e91a 967 bdrv_unref_child(bs, s->children[i]);
c88a1de5 968 }
0bd6e91a 969 g_free(s->children);
c88a1de5
BC
970 g_free(opened);
971exit:
8df3abfc 972 qemu_opts_del(opts);
c88a1de5 973 /* propagate error */
621ff94d 974 error_propagate(errp, local_err);
c88a1de5
BC
975 return ret;
976}
977
978static void quorum_close(BlockDriverState *bs)
979{
980 BDRVQuorumState *s = bs->opaque;
981 int i;
982
983 for (i = 0; i < s->num_children; i++) {
0bd6e91a 984 bdrv_unref_child(bs, s->children[i]);
c88a1de5
BC
985 }
986
0bd6e91a 987 g_free(s->children);
c88a1de5
BC
988}
989
98292c61
WC
990static void quorum_add_child(BlockDriverState *bs, BlockDriverState *child_bs,
991 Error **errp)
992{
993 BDRVQuorumState *s = bs->opaque;
994 BdrvChild *child;
995 char indexstr[32];
996 int ret;
997
998 assert(s->num_children <= INT_MAX / sizeof(BdrvChild *));
999 if (s->num_children == INT_MAX / sizeof(BdrvChild *) ||
1000 s->next_child_index == UINT_MAX) {
1001 error_setg(errp, "Too many children");
1002 return;
1003 }
1004
1005 ret = snprintf(indexstr, 32, "children.%u", s->next_child_index);
1006 if (ret < 0 || ret >= 32) {
1007 error_setg(errp, "cannot generate child name");
1008 return;
1009 }
1010 s->next_child_index++;
1011
1012 bdrv_drained_begin(bs);
1013
1014 /* We can safely add the child now */
1015 bdrv_ref(child_bs);
1016 child = bdrv_attach_child(bs, child_bs, indexstr, &child_format);
1017 s->children = g_renew(BdrvChild *, s->children, s->num_children + 1);
1018 s->children[s->num_children++] = child;
1019
1020 bdrv_drained_end(bs);
1021}
1022
1023static void quorum_del_child(BlockDriverState *bs, BdrvChild *child,
1024 Error **errp)
1025{
1026 BDRVQuorumState *s = bs->opaque;
1027 int i;
1028
1029 for (i = 0; i < s->num_children; i++) {
1030 if (s->children[i] == child) {
1031 break;
1032 }
1033 }
1034
1035 /* we have checked it in bdrv_del_child() */
1036 assert(i < s->num_children);
1037
1038 if (s->num_children <= s->threshold) {
1039 error_setg(errp,
1040 "The number of children cannot be lower than the vote threshold %d",
1041 s->threshold);
1042 return;
1043 }
1044
1045 bdrv_drained_begin(bs);
1046
1047 /* We can safely remove this child now */
1048 memmove(&s->children[i], &s->children[i + 1],
1049 (s->num_children - i - 1) * sizeof(BdrvChild *));
1050 s->children = g_renew(BdrvChild *, s->children, --s->num_children);
1051 bdrv_unref_child(bs, child);
1052
1053 bdrv_drained_end(bs);
1054}
1055
4cdd01d3 1056static void quorum_refresh_filename(BlockDriverState *bs, QDict *options)
fafcfe22
HR
1057{
1058 BDRVQuorumState *s = bs->opaque;
1059 QDict *opts;
1060 QList *children;
1061 int i;
1062
1063 for (i = 0; i < s->num_children; i++) {
0bd6e91a
KW
1064 bdrv_refresh_filename(s->children[i]->bs);
1065 if (!s->children[i]->bs->full_open_options) {
fafcfe22
HR
1066 return;
1067 }
1068 }
1069
1070 children = qlist_new();
1071 for (i = 0; i < s->num_children; i++) {
0bd6e91a
KW
1072 QINCREF(s->children[i]->bs->full_open_options);
1073 qlist_append_obj(children,
1074 QOBJECT(s->children[i]->bs->full_open_options));
fafcfe22
HR
1075 }
1076
1077 opts = qdict_new();
1078 qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("quorum")));
1079 qdict_put_obj(opts, QUORUM_OPT_VOTE_THRESHOLD,
1080 QOBJECT(qint_from_int(s->threshold)));
1081 qdict_put_obj(opts, QUORUM_OPT_BLKVERIFY,
fc48ffc3 1082 QOBJECT(qbool_from_bool(s->is_blkverify)));
fafcfe22 1083 qdict_put_obj(opts, QUORUM_OPT_REWRITE,
fc48ffc3 1084 QOBJECT(qbool_from_bool(s->rewrite_corrupted)));
fafcfe22
HR
1085 qdict_put_obj(opts, "children", QOBJECT(children));
1086
1087 bs->full_open_options = opts;
1088}
1089
cadebd7a 1090static BlockDriver bdrv_quorum = {
e3625d3d
SH
1091 .format_name = "quorum",
1092 .protocol_name = "quorum",
1093
1094 .instance_size = sizeof(BDRVQuorumState),
cadebd7a 1095
e3625d3d
SH
1096 .bdrv_file_open = quorum_open,
1097 .bdrv_close = quorum_close,
fafcfe22 1098 .bdrv_refresh_filename = quorum_refresh_filename,
13e7956e 1099
e3625d3d 1100 .bdrv_co_flush_to_disk = quorum_co_flush,
c88a1de5 1101
e3625d3d 1102 .bdrv_getlength = quorum_getlength,
1c508d17 1103
e3625d3d
SH
1104 .bdrv_aio_readv = quorum_aio_readv,
1105 .bdrv_aio_writev = quorum_aio_writev,
d55dee20 1106
98292c61
WC
1107 .bdrv_add_child = quorum_add_child,
1108 .bdrv_del_child = quorum_del_child,
1109
e3625d3d
SH
1110 .is_filter = true,
1111 .bdrv_recurse_is_first_non_filter = quorum_recurse_is_first_non_filter,
cadebd7a
BC
1112};
1113
1114static void bdrv_quorum_init(void)
1115{
e94867ed
SS
1116 if (!qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA256)) {
1117 /* SHA256 hash support is required for quorum device */
1118 return;
1119 }
cadebd7a
BC
1120 bdrv_register(&bdrv_quorum);
1121}
1122
1123block_init(bdrv_quorum_init);