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