]> Git Repo - qemu.git/blame - block/quorum.c
sheepdog: Assign bs to file in sd_co_get_block_status
[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 <[email protected]>
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
95c6bff3
BC
218static void quorum_report_bad(QuorumAIOCB *acb, char *node_name, int ret)
219{
fe069d9d 220 const char *msg = NULL;
0c762736 221 if (ret < 0) {
fe069d9d 222 msg = strerror(-ret);
0c762736 223 }
fe069d9d
WX
224 qapi_event_send_quorum_report_bad(!!msg, msg, node_name,
225 acb->sector_num, acb->nb_sectors, &error_abort);
95c6bff3
BC
226}
227
228static void quorum_report_failure(QuorumAIOCB *acb)
229{
9b2aa84f 230 const char *reference = bdrv_get_device_or_node_name(acb->common.bs);
fe069d9d
WX
231 qapi_event_send_quorum_failure(reference, acb->sector_num,
232 acb->nb_sectors, &error_abort);
95c6bff3
BC
233}
234
235static int quorum_vote_error(QuorumAIOCB *acb);
236
237static bool quorum_has_too_much_io_failed(QuorumAIOCB *acb)
238{
239 BDRVQuorumState *s = acb->common.bs->opaque;
240
241 if (acb->success_count < s->threshold) {
242 acb->vote_ret = quorum_vote_error(acb);
243 quorum_report_failure(acb);
244 return true;
245 }
246
247 return false;
248}
249
cf29a570
BC
250static void quorum_rewrite_aio_cb(void *opaque, int ret)
251{
252 QuorumAIOCB *acb = opaque;
253
254 /* one less rewrite to do */
255 acb->rewrite_count--;
256
257 /* wait until all rewrite callbacks have completed */
258 if (acb->rewrite_count) {
259 return;
260 }
261
262 quorum_aio_finalize(acb);
263}
264
7c84b1b8 265static BlockAIOCB *read_fifo_child(QuorumAIOCB *acb);
a9db86b2
LY
266
267static void quorum_copy_qiov(QEMUIOVector *dest, QEMUIOVector *source)
268{
269 int i;
270 assert(dest->niov == source->niov);
271 assert(dest->size == source->size);
272 for (i = 0; i < source->niov; i++) {
273 assert(dest->iov[i].iov_len == source->iov[i].iov_len);
274 memcpy(dest->iov[i].iov_base,
275 source->iov[i].iov_base,
276 source->iov[i].iov_len);
277 }
278}
279
13e7956e
BC
280static void quorum_aio_cb(void *opaque, int ret)
281{
282 QuorumChildRequest *sacb = opaque;
283 QuorumAIOCB *acb = sacb->parent;
284 BDRVQuorumState *s = acb->common.bs->opaque;
cf29a570 285 bool rewrite = false;
13e7956e 286
a9db86b2
LY
287 if (acb->is_read && s->read_pattern == QUORUM_READ_PATTERN_FIFO) {
288 /* We try to read next child in FIFO order if we fail to read */
289 if (ret < 0 && ++acb->child_iter < s->num_children) {
290 read_fifo_child(acb);
291 return;
292 }
293
294 if (ret == 0) {
295 quorum_copy_qiov(acb->qiov, &acb->qcrs[acb->child_iter].qiov);
296 }
297 acb->vote_ret = ret;
298 quorum_aio_finalize(acb);
299 return;
300 }
301
13e7956e
BC
302 sacb->ret = ret;
303 acb->count++;
304 if (ret == 0) {
305 acb->success_count++;
95c6bff3
BC
306 } else {
307 quorum_report_bad(acb, sacb->aiocb->bs->node_name, ret);
13e7956e
BC
308 }
309 assert(acb->count <= s->num_children);
310 assert(acb->success_count <= s->num_children);
311 if (acb->count < s->num_children) {
312 return;
313 }
314
95c6bff3
BC
315 /* Do the vote on read */
316 if (acb->is_read) {
cf29a570 317 rewrite = quorum_vote(acb);
95c6bff3
BC
318 } else {
319 quorum_has_too_much_io_failed(acb);
320 }
321
cf29a570
BC
322 /* if no rewrite is done the code will finish right away */
323 if (!rewrite) {
324 quorum_aio_finalize(acb);
325 }
13e7956e
BC
326}
327
95c6bff3
BC
328static void quorum_report_bad_versions(BDRVQuorumState *s,
329 QuorumAIOCB *acb,
330 QuorumVoteValue *value)
331{
332 QuorumVoteVersion *version;
333 QuorumVoteItem *item;
334
335 QLIST_FOREACH(version, &acb->votes.vote_list, next) {
336 if (acb->votes.compare(&version->value, value)) {
337 continue;
338 }
339 QLIST_FOREACH(item, &version->items, next) {
0bd6e91a 340 quorum_report_bad(acb, s->children[item->index]->bs->node_name, 0);
95c6bff3
BC
341 }
342 }
343}
344
cf29a570
BC
345static bool quorum_rewrite_bad_versions(BDRVQuorumState *s, QuorumAIOCB *acb,
346 QuorumVoteValue *value)
347{
348 QuorumVoteVersion *version;
349 QuorumVoteItem *item;
350 int count = 0;
351
352 /* first count the number of bad versions: done first to avoid concurrency
353 * issues.
354 */
355 QLIST_FOREACH(version, &acb->votes.vote_list, next) {
356 if (acb->votes.compare(&version->value, value)) {
357 continue;
358 }
359 QLIST_FOREACH(item, &version->items, next) {
360 count++;
361 }
362 }
363
364 /* quorum_rewrite_aio_cb will count down this to zero */
365 acb->rewrite_count = count;
366
367 /* now fire the correcting rewrites */
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) {
0bd6e91a
KW
373 bdrv_aio_writev(s->children[item->index]->bs, acb->sector_num,
374 acb->qiov, acb->nb_sectors, quorum_rewrite_aio_cb,
375 acb);
cf29a570
BC
376 }
377 }
378
379 /* return true if any rewrite is done else false */
380 return count;
381}
382
95c6bff3
BC
383static void quorum_count_vote(QuorumVotes *votes,
384 QuorumVoteValue *value,
385 int index)
386{
387 QuorumVoteVersion *v = NULL, *version = NULL;
388 QuorumVoteItem *item;
389
390 /* look if we have something with this hash */
391 QLIST_FOREACH(v, &votes->vote_list, next) {
392 if (votes->compare(&v->value, value)) {
393 version = v;
394 break;
395 }
396 }
397
398 /* It's a version not yet in the list add it */
399 if (!version) {
400 version = g_new0(QuorumVoteVersion, 1);
401 QLIST_INIT(&version->items);
402 memcpy(&version->value, value, sizeof(version->value));
403 version->index = index;
404 version->vote_count = 0;
405 QLIST_INSERT_HEAD(&votes->vote_list, version, next);
406 }
407
408 version->vote_count++;
409
410 item = g_new0(QuorumVoteItem, 1);
411 item->index = index;
412 QLIST_INSERT_HEAD(&version->items, item, next);
413}
414
415static void quorum_free_vote_list(QuorumVotes *votes)
416{
417 QuorumVoteVersion *version, *next_version;
418 QuorumVoteItem *item, *next_item;
419
420 QLIST_FOREACH_SAFE(version, &votes->vote_list, next, next_version) {
421 QLIST_REMOVE(version, next);
422 QLIST_FOREACH_SAFE(item, &version->items, next, next_item) {
423 QLIST_REMOVE(item, next);
424 g_free(item);
425 }
426 g_free(version);
427 }
428}
429
430static int quorum_compute_hash(QuorumAIOCB *acb, int i, QuorumVoteValue *hash)
431{
95c6bff3 432 QEMUIOVector *qiov = &acb->qcrs[i].qiov;
488981a4
DB
433 size_t len = sizeof(hash->h);
434 uint8_t *data = hash->h;
95c6bff3 435
488981a4
DB
436 /* XXX - would be nice if we could pass in the Error **
437 * and propagate that back, but this quorum code is
438 * restricted to just errno values currently */
439 if (qcrypto_hash_bytesv(QCRYPTO_HASH_ALG_SHA256,
440 qiov->iov, qiov->niov,
441 &data, &len,
442 NULL) < 0) {
443 return -EINVAL;
95c6bff3
BC
444 }
445
488981a4 446 return 0;
95c6bff3
BC
447}
448
449static QuorumVoteVersion *quorum_get_vote_winner(QuorumVotes *votes)
450{
451 int max = 0;
452 QuorumVoteVersion *candidate, *winner = NULL;
453
454 QLIST_FOREACH(candidate, &votes->vote_list, next) {
455 if (candidate->vote_count > max) {
456 max = candidate->vote_count;
457 winner = candidate;
458 }
459 }
460
461 return winner;
462}
463
464/* qemu_iovec_compare is handy for blkverify mode because it returns the first
465 * differing byte location. Yet it is handcoded to compare vectors one byte
466 * after another so it does not benefit from the libc SIMD optimizations.
467 * quorum_iovec_compare is written for speed and should be used in the non
468 * blkverify mode of quorum.
469 */
470static bool quorum_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
471{
472 int i;
473 int result;
474
475 assert(a->niov == b->niov);
476 for (i = 0; i < a->niov; i++) {
477 assert(a->iov[i].iov_len == b->iov[i].iov_len);
478 result = memcmp(a->iov[i].iov_base,
479 b->iov[i].iov_base,
480 a->iov[i].iov_len);
481 if (result) {
482 return false;
483 }
484 }
485
486 return true;
487}
488
489static void GCC_FMT_ATTR(2, 3) quorum_err(QuorumAIOCB *acb,
490 const char *fmt, ...)
491{
492 va_list ap;
493
494 va_start(ap, fmt);
495 fprintf(stderr, "quorum: sector_num=%" PRId64 " nb_sectors=%d ",
496 acb->sector_num, acb->nb_sectors);
497 vfprintf(stderr, fmt, ap);
498 fprintf(stderr, "\n");
499 va_end(ap);
500 exit(1);
501}
502
503static bool quorum_compare(QuorumAIOCB *acb,
504 QEMUIOVector *a,
505 QEMUIOVector *b)
506{
507 BDRVQuorumState *s = acb->common.bs->opaque;
508 ssize_t offset;
509
510 /* This driver will replace blkverify in this particular case */
511 if (s->is_blkverify) {
512 offset = qemu_iovec_compare(a, b);
513 if (offset != -1) {
514 quorum_err(acb, "contents mismatch in sector %" PRId64,
515 acb->sector_num +
516 (uint64_t)(offset / BDRV_SECTOR_SIZE));
517 }
518 return true;
519 }
520
521 return quorum_iovec_compare(a, b);
522}
523
524/* Do a vote to get the error code */
525static int quorum_vote_error(QuorumAIOCB *acb)
526{
527 BDRVQuorumState *s = acb->common.bs->opaque;
528 QuorumVoteVersion *winner = NULL;
529 QuorumVotes error_votes;
530 QuorumVoteValue result_value;
531 int i, ret = 0;
532 bool error = false;
533
534 QLIST_INIT(&error_votes.vote_list);
535 error_votes.compare = quorum_64bits_compare;
536
537 for (i = 0; i < s->num_children; i++) {
538 ret = acb->qcrs[i].ret;
539 if (ret) {
540 error = true;
541 result_value.l = ret;
542 quorum_count_vote(&error_votes, &result_value, i);
543 }
544 }
545
546 if (error) {
547 winner = quorum_get_vote_winner(&error_votes);
548 ret = winner->value.l;
549 }
550
551 quorum_free_vote_list(&error_votes);
552
553 return ret;
554}
555
cf29a570 556static bool quorum_vote(QuorumAIOCB *acb)
95c6bff3
BC
557{
558 bool quorum = true;
cf29a570 559 bool rewrite = false;
95c6bff3
BC
560 int i, j, ret;
561 QuorumVoteValue hash;
562 BDRVQuorumState *s = acb->common.bs->opaque;
563 QuorumVoteVersion *winner;
564
565 if (quorum_has_too_much_io_failed(acb)) {
cf29a570 566 return false;
95c6bff3
BC
567 }
568
569 /* get the index of the first successful read */
570 for (i = 0; i < s->num_children; i++) {
571 if (!acb->qcrs[i].ret) {
572 break;
573 }
574 }
575
576 assert(i < s->num_children);
577
578 /* compare this read with all other successful reads stopping at quorum
579 * failure
580 */
581 for (j = i + 1; j < s->num_children; j++) {
582 if (acb->qcrs[j].ret) {
583 continue;
584 }
585 quorum = quorum_compare(acb, &acb->qcrs[i].qiov, &acb->qcrs[j].qiov);
586 if (!quorum) {
587 break;
588 }
589 }
590
591 /* Every successful read agrees */
592 if (quorum) {
593 quorum_copy_qiov(acb->qiov, &acb->qcrs[i].qiov);
cf29a570 594 return false;
95c6bff3
BC
595 }
596
597 /* compute hashes for each successful read, also store indexes */
598 for (i = 0; i < s->num_children; i++) {
599 if (acb->qcrs[i].ret) {
600 continue;
601 }
602 ret = quorum_compute_hash(acb, i, &hash);
603 /* if ever the hash computation failed */
604 if (ret < 0) {
605 acb->vote_ret = ret;
606 goto free_exit;
607 }
608 quorum_count_vote(&acb->votes, &hash, i);
609 }
610
611 /* vote to select the most represented version */
612 winner = quorum_get_vote_winner(&acb->votes);
613
614 /* if the winner count is smaller than threshold the read fails */
615 if (winner->vote_count < s->threshold) {
616 quorum_report_failure(acb);
617 acb->vote_ret = -EIO;
618 goto free_exit;
619 }
620
621 /* we have a winner: copy it */
622 quorum_copy_qiov(acb->qiov, &acb->qcrs[winner->index].qiov);
623
624 /* some versions are bad print them */
625 quorum_report_bad_versions(s, acb, &winner->value);
626
cf29a570
BC
627 /* corruption correction is enabled */
628 if (s->rewrite_corrupted) {
629 rewrite = quorum_rewrite_bad_versions(s, acb, &winner->value);
630 }
631
95c6bff3
BC
632free_exit:
633 /* free lists */
634 quorum_free_vote_list(&acb->votes);
cf29a570 635 return rewrite;
95c6bff3
BC
636}
637
7c84b1b8 638static BlockAIOCB *read_quorum_children(QuorumAIOCB *acb)
7db6982a 639{
a9db86b2 640 BDRVQuorumState *s = acb->common.bs->opaque;
7db6982a
BC
641 int i;
642
7db6982a 643 for (i = 0; i < s->num_children; i++) {
0bd6e91a 644 acb->qcrs[i].buf = qemu_blockalign(s->children[i]->bs, acb->qiov->size);
a9db86b2
LY
645 qemu_iovec_init(&acb->qcrs[i].qiov, acb->qiov->niov);
646 qemu_iovec_clone(&acb->qcrs[i].qiov, acb->qiov, acb->qcrs[i].buf);
7db6982a
BC
647 }
648
649 for (i = 0; i < s->num_children; i++) {
0bd6e91a 650 bdrv_aio_readv(s->children[i]->bs, acb->sector_num, &acb->qcrs[i].qiov,
a9db86b2 651 acb->nb_sectors, quorum_aio_cb, &acb->qcrs[i]);
7db6982a
BC
652 }
653
654 return &acb->common;
655}
656
7c84b1b8 657static BlockAIOCB *read_fifo_child(QuorumAIOCB *acb)
a9db86b2
LY
658{
659 BDRVQuorumState *s = acb->common.bs->opaque;
660
0bd6e91a
KW
661 acb->qcrs[acb->child_iter].buf =
662 qemu_blockalign(s->children[acb->child_iter]->bs, acb->qiov->size);
a9db86b2
LY
663 qemu_iovec_init(&acb->qcrs[acb->child_iter].qiov, acb->qiov->niov);
664 qemu_iovec_clone(&acb->qcrs[acb->child_iter].qiov, acb->qiov,
665 acb->qcrs[acb->child_iter].buf);
0bd6e91a 666 bdrv_aio_readv(s->children[acb->child_iter]->bs, acb->sector_num,
a9db86b2
LY
667 &acb->qcrs[acb->child_iter].qiov, acb->nb_sectors,
668 quorum_aio_cb, &acb->qcrs[acb->child_iter]);
669
670 return &acb->common;
671}
672
7c84b1b8
MA
673static BlockAIOCB *quorum_aio_readv(BlockDriverState *bs,
674 int64_t sector_num,
675 QEMUIOVector *qiov,
676 int nb_sectors,
097310b5 677 BlockCompletionFunc *cb,
7c84b1b8 678 void *opaque)
a9db86b2
LY
679{
680 BDRVQuorumState *s = bs->opaque;
681 QuorumAIOCB *acb = quorum_aio_get(s, bs, qiov, sector_num,
682 nb_sectors, cb, opaque);
683 acb->is_read = true;
684
685 if (s->read_pattern == QUORUM_READ_PATTERN_QUORUM) {
686 acb->child_iter = s->num_children - 1;
687 return read_quorum_children(acb);
688 }
689
690 acb->child_iter = 0;
691 return read_fifo_child(acb);
692}
693
7c84b1b8
MA
694static BlockAIOCB *quorum_aio_writev(BlockDriverState *bs,
695 int64_t sector_num,
696 QEMUIOVector *qiov,
697 int nb_sectors,
097310b5 698 BlockCompletionFunc *cb,
7c84b1b8 699 void *opaque)
13e7956e
BC
700{
701 BDRVQuorumState *s = bs->opaque;
702 QuorumAIOCB *acb = quorum_aio_get(s, bs, qiov, sector_num, nb_sectors,
703 cb, opaque);
704 int i;
705
706 for (i = 0; i < s->num_children; i++) {
0bd6e91a
KW
707 acb->qcrs[i].aiocb = bdrv_aio_writev(s->children[i]->bs, sector_num,
708 qiov, nb_sectors, &quorum_aio_cb,
13e7956e
BC
709 &acb->qcrs[i]);
710 }
711
712 return &acb->common;
713}
714
d55dee20
BC
715static int64_t quorum_getlength(BlockDriverState *bs)
716{
717 BDRVQuorumState *s = bs->opaque;
718 int64_t result;
719 int i;
720
721 /* check that all file have the same length */
0bd6e91a 722 result = bdrv_getlength(s->children[0]->bs);
d55dee20
BC
723 if (result < 0) {
724 return result;
725 }
726 for (i = 1; i < s->num_children; i++) {
0bd6e91a 727 int64_t value = bdrv_getlength(s->children[i]->bs);
d55dee20
BC
728 if (value < 0) {
729 return value;
730 }
731 if (value != result) {
732 return -EIO;
733 }
734 }
735
736 return result;
737}
738
5a8a30db 739static void quorum_invalidate_cache(BlockDriverState *bs, Error **errp)
a28e4c40
BC
740{
741 BDRVQuorumState *s = bs->opaque;
5a8a30db 742 Error *local_err = NULL;
a28e4c40
BC
743 int i;
744
745 for (i = 0; i < s->num_children; i++) {
0bd6e91a 746 bdrv_invalidate_cache(s->children[i]->bs, &local_err);
5a8a30db
KW
747 if (local_err) {
748 error_propagate(errp, local_err);
749 return;
750 }
a28e4c40
BC
751 }
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;
762
763 QLIST_INIT(&error_votes.vote_list);
764 error_votes.compare = quorum_64bits_compare;
765
766 for (i = 0; i < s->num_children; i++) {
0bd6e91a 767 result = bdrv_co_flush(s->children[i]->bs);
1c508d17
BC
768 result_value.l = result;
769 quorum_count_vote(&error_votes, &result_value, i);
770 }
771
772 winner = quorum_get_vote_winner(&error_votes);
773 result = winner->value.l;
774
775 quorum_free_vote_list(&error_votes);
776
777 return result;
778}
779
98a7a38f
BC
780static bool quorum_recurse_is_first_non_filter(BlockDriverState *bs,
781 BlockDriverState *candidate)
782{
783 BDRVQuorumState *s = bs->opaque;
784 int i;
785
786 for (i = 0; i < s->num_children; i++) {
0bd6e91a 787 bool perm = bdrv_recurse_is_first_non_filter(s->children[i]->bs,
98a7a38f
BC
788 candidate);
789 if (perm) {
790 return true;
791 }
792 }
793
794 return false;
795}
796
c88a1de5
BC
797static int quorum_valid_threshold(int threshold, int num_children, Error **errp)
798{
799
800 if (threshold < 1) {
c6bd8c70
MA
801 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
802 "vote-threshold", "value >= 1");
c88a1de5
BC
803 return -ERANGE;
804 }
805
806 if (threshold > num_children) {
807 error_setg(errp, "threshold may not exceed children count");
808 return -ERANGE;
809 }
810
811 return 0;
812}
813
814static QemuOptsList quorum_runtime_opts = {
815 .name = "quorum",
816 .head = QTAILQ_HEAD_INITIALIZER(quorum_runtime_opts.head),
817 .desc = {
818 {
819 .name = QUORUM_OPT_VOTE_THRESHOLD,
820 .type = QEMU_OPT_NUMBER,
821 .help = "The number of vote needed for reaching quorum",
822 },
823 {
824 .name = QUORUM_OPT_BLKVERIFY,
825 .type = QEMU_OPT_BOOL,
826 .help = "Trigger block verify mode if set",
827 },
cf29a570
BC
828 {
829 .name = QUORUM_OPT_REWRITE,
830 .type = QEMU_OPT_BOOL,
831 .help = "Rewrite corrupted block on read quorum",
832 },
a9db86b2
LY
833 {
834 .name = QUORUM_OPT_READ_PATTERN,
835 .type = QEMU_OPT_STRING,
836 .help = "Allowed pattern: quorum, fifo. Quorum is default",
837 },
c88a1de5
BC
838 { /* end of list */ }
839 },
840};
841
a9db86b2
LY
842static int parse_read_pattern(const char *opt)
843{
844 int i;
845
846 if (!opt) {
847 /* Set quorum as default */
848 return QUORUM_READ_PATTERN_QUORUM;
849 }
850
7fb1cf16 851 for (i = 0; i < QUORUM_READ_PATTERN__MAX; i++) {
a9db86b2
LY
852 if (!strcmp(opt, QuorumReadPattern_lookup[i])) {
853 return i;
854 }
855 }
856
857 return -EINVAL;
858}
859
c88a1de5
BC
860static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
861 Error **errp)
862{
863 BDRVQuorumState *s = bs->opaque;
864 Error *local_err = NULL;
8df3abfc 865 QemuOpts *opts = NULL;
c88a1de5 866 bool *opened;
c88a1de5
BC
867 int i;
868 int ret = 0;
869
870 qdict_flatten(options);
c88a1de5 871
ea6828d8
KW
872 /* count how many different children are present */
873 s->num_children = qdict_array_entries(options, "children.");
874 if (s->num_children < 0) {
875 error_setg(&local_err, "Option children is not a valid array");
8a87f3d7
HR
876 ret = -EINVAL;
877 goto exit;
878 }
c88a1de5
BC
879 if (s->num_children < 2) {
880 error_setg(&local_err,
881 "Number of provided children must be greater than 1");
882 ret = -EINVAL;
883 goto exit;
884 }
885
886 opts = qemu_opts_create(&quorum_runtime_opts, NULL, 0, &error_abort);
887 qemu_opts_absorb_qdict(opts, options, &local_err);
0fb6395c 888 if (local_err) {
c88a1de5
BC
889 ret = -EINVAL;
890 goto exit;
891 }
892
893 s->threshold = qemu_opt_get_number(opts, QUORUM_OPT_VOTE_THRESHOLD, 0);
834cb2ad
WC
894 /* and validate it against s->num_children */
895 ret = quorum_valid_threshold(s->threshold, s->num_children, &local_err);
896 if (ret < 0) {
897 goto exit;
898 }
899
a9db86b2 900 ret = parse_read_pattern(qemu_opt_get(opts, QUORUM_OPT_READ_PATTERN));
c88a1de5 901 if (ret < 0) {
a9db86b2 902 error_setg(&local_err, "Please set read-pattern as fifo or quorum");
c88a1de5
BC
903 goto exit;
904 }
a9db86b2 905 s->read_pattern = ret;
c88a1de5 906
a9db86b2 907 if (s->read_pattern == QUORUM_READ_PATTERN_QUORUM) {
a9db86b2
LY
908 /* is the driver in blkverify mode */
909 if (qemu_opt_get_bool(opts, QUORUM_OPT_BLKVERIFY, false) &&
910 s->num_children == 2 && s->threshold == 2) {
911 s->is_blkverify = true;
912 } else if (qemu_opt_get_bool(opts, QUORUM_OPT_BLKVERIFY, false)) {
913 fprintf(stderr, "blkverify mode is set by setting blkverify=on "
914 "and using two files with vote_threshold=2\n");
915 }
916
917 s->rewrite_corrupted = qemu_opt_get_bool(opts, QUORUM_OPT_REWRITE,
918 false);
919 if (s->rewrite_corrupted && s->is_blkverify) {
920 error_setg(&local_err,
921 "rewrite-corrupted=on cannot be used with blkverify=on");
922 ret = -EINVAL;
923 goto exit;
924 }
cf29a570
BC
925 }
926
0bd6e91a
KW
927 /* allocate the children array */
928 s->children = g_new0(BdrvChild *, s->num_children);
c88a1de5
BC
929 opened = g_new0(bool, s->num_children);
930
ea6828d8
KW
931 for (i = 0; i < s->num_children; i++) {
932 char indexstr[32];
933 ret = snprintf(indexstr, 32, "children.%d", i);
934 assert(ret < 32);
8a87f3d7 935
0bd6e91a
KW
936 s->children[i] = bdrv_open_child(NULL, options, indexstr, bs,
937 &child_format, false, &local_err);
938 if (local_err) {
939 ret = -EINVAL;
8a87f3d7 940 goto close_exit;
c88a1de5 941 }
ea6828d8 942
8a87f3d7 943 opened[i] = true;
c88a1de5
BC
944 }
945
946 g_free(opened);
947 goto exit;
948
949close_exit:
950 /* cleanup on error */
951 for (i = 0; i < s->num_children; i++) {
952 if (!opened[i]) {
953 continue;
954 }
0bd6e91a 955 bdrv_unref_child(bs, s->children[i]);
c88a1de5 956 }
0bd6e91a 957 g_free(s->children);
c88a1de5
BC
958 g_free(opened);
959exit:
8df3abfc 960 qemu_opts_del(opts);
c88a1de5 961 /* propagate error */
0fb6395c 962 if (local_err) {
c88a1de5
BC
963 error_propagate(errp, local_err);
964 }
c88a1de5
BC
965 return ret;
966}
967
968static void quorum_close(BlockDriverState *bs)
969{
970 BDRVQuorumState *s = bs->opaque;
971 int i;
972
973 for (i = 0; i < s->num_children; i++) {
0bd6e91a 974 bdrv_unref_child(bs, s->children[i]);
c88a1de5
BC
975 }
976
0bd6e91a 977 g_free(s->children);
c88a1de5
BC
978}
979
e3625d3d
SH
980static void quorum_detach_aio_context(BlockDriverState *bs)
981{
982 BDRVQuorumState *s = bs->opaque;
983 int i;
984
985 for (i = 0; i < s->num_children; i++) {
0bd6e91a 986 bdrv_detach_aio_context(s->children[i]->bs);
e3625d3d
SH
987 }
988}
989
990static void quorum_attach_aio_context(BlockDriverState *bs,
991 AioContext *new_context)
992{
993 BDRVQuorumState *s = bs->opaque;
994 int i;
995
996 for (i = 0; i < s->num_children; i++) {
0bd6e91a 997 bdrv_attach_aio_context(s->children[i]->bs, new_context);
e3625d3d
SH
998 }
999}
1000
4cdd01d3 1001static void quorum_refresh_filename(BlockDriverState *bs, QDict *options)
fafcfe22
HR
1002{
1003 BDRVQuorumState *s = bs->opaque;
1004 QDict *opts;
1005 QList *children;
1006 int i;
1007
1008 for (i = 0; i < s->num_children; i++) {
0bd6e91a
KW
1009 bdrv_refresh_filename(s->children[i]->bs);
1010 if (!s->children[i]->bs->full_open_options) {
fafcfe22
HR
1011 return;
1012 }
1013 }
1014
1015 children = qlist_new();
1016 for (i = 0; i < s->num_children; i++) {
0bd6e91a
KW
1017 QINCREF(s->children[i]->bs->full_open_options);
1018 qlist_append_obj(children,
1019 QOBJECT(s->children[i]->bs->full_open_options));
fafcfe22
HR
1020 }
1021
1022 opts = qdict_new();
1023 qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("quorum")));
1024 qdict_put_obj(opts, QUORUM_OPT_VOTE_THRESHOLD,
1025 QOBJECT(qint_from_int(s->threshold)));
1026 qdict_put_obj(opts, QUORUM_OPT_BLKVERIFY,
fc48ffc3 1027 QOBJECT(qbool_from_bool(s->is_blkverify)));
fafcfe22 1028 qdict_put_obj(opts, QUORUM_OPT_REWRITE,
fc48ffc3 1029 QOBJECT(qbool_from_bool(s->rewrite_corrupted)));
fafcfe22
HR
1030 qdict_put_obj(opts, "children", QOBJECT(children));
1031
1032 bs->full_open_options = opts;
1033}
1034
cadebd7a 1035static BlockDriver bdrv_quorum = {
e3625d3d
SH
1036 .format_name = "quorum",
1037 .protocol_name = "quorum",
1038
1039 .instance_size = sizeof(BDRVQuorumState),
cadebd7a 1040
e3625d3d
SH
1041 .bdrv_file_open = quorum_open,
1042 .bdrv_close = quorum_close,
fafcfe22 1043 .bdrv_refresh_filename = quorum_refresh_filename,
13e7956e 1044
e3625d3d 1045 .bdrv_co_flush_to_disk = quorum_co_flush,
c88a1de5 1046
e3625d3d 1047 .bdrv_getlength = quorum_getlength,
1c508d17 1048
e3625d3d
SH
1049 .bdrv_aio_readv = quorum_aio_readv,
1050 .bdrv_aio_writev = quorum_aio_writev,
1051 .bdrv_invalidate_cache = quorum_invalidate_cache,
d55dee20 1052
e3625d3d
SH
1053 .bdrv_detach_aio_context = quorum_detach_aio_context,
1054 .bdrv_attach_aio_context = quorum_attach_aio_context,
98a7a38f 1055
e3625d3d
SH
1056 .is_filter = true,
1057 .bdrv_recurse_is_first_non_filter = quorum_recurse_is_first_non_filter,
cadebd7a
BC
1058};
1059
1060static void bdrv_quorum_init(void)
1061{
e94867ed
SS
1062 if (!qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA256)) {
1063 /* SHA256 hash support is required for quorum device */
1064 return;
1065 }
cadebd7a
BC
1066 bdrv_register(&bdrv_quorum);
1067}
1068
1069block_init(bdrv_quorum_init);
This page took 0.270393 seconds and 4 git commands to generate.