127cec15eSBenoît Canet /*
227cec15eSBenoît Canet * Quorum Block filter
327cec15eSBenoît Canet *
427cec15eSBenoît Canet * Copyright (C) 2012-2014 Nodalink, EURL.
527cec15eSBenoît Canet *
627cec15eSBenoît Canet * Author:
727cec15eSBenoît Canet * Benoît Canet <benoit.canet@irqsave.net>
827cec15eSBenoît Canet *
927cec15eSBenoît Canet * Based on the design and code of blkverify.c (Copyright (C) 2010 IBM, Corp)
1027cec15eSBenoît Canet * and blkmirror.c (Copyright (C) 2011 Red Hat, Inc).
1127cec15eSBenoît Canet *
1227cec15eSBenoît Canet * This work is licensed under the terms of the GNU GPL, version 2 or later.
1327cec15eSBenoît Canet * See the COPYING file in the top-level directory.
1427cec15eSBenoît Canet */
1527cec15eSBenoît Canet
1680c71a24SPeter Maydell #include "qemu/osdep.h"
1798292c61SWen Congyang #include "qemu/cutils.h"
180b8fa32fSMarkus Armbruster #include "qemu/module.h"
19922a01a0SMarkus Armbruster #include "qemu/option.h"
205df022cfSPeter Maydell #include "qemu/memalign.h"
2127cec15eSBenoît Canet #include "block/block_int.h"
22ef9bba14SAlberto Garcia #include "block/coroutines.h"
23609f45eaSMax Reitz #include "block/qdict.h"
24e688df6bSMarkus Armbruster #include "qapi/error.h"
259af23989SMarkus Armbruster #include "qapi/qapi-events-block.h"
26fafcfe22SMax Reitz #include "qapi/qmp/qdict.h"
27cc7a8ea7SMarkus Armbruster #include "qapi/qmp/qerror.h"
28fafcfe22SMax Reitz #include "qapi/qmp/qlist.h"
29fafcfe22SMax Reitz #include "qapi/qmp/qstring.h"
30488981a4SDaniel P. Berrange #include "crypto/hash.h"
3195c6bff3SBenoît Canet
3295c6bff3SBenoît Canet #define HASH_LENGTH 32
3395c6bff3SBenoît Canet
345eb9a3c7SLukas Straub #define INDEXSTR_LEN 32
355eb9a3c7SLukas Straub
36c88a1de5SBenoît Canet #define QUORUM_OPT_VOTE_THRESHOLD "vote-threshold"
37c88a1de5SBenoît Canet #define QUORUM_OPT_BLKVERIFY "blkverify"
38cf29a570SBenoît Canet #define QUORUM_OPT_REWRITE "rewrite-corrupted"
39a9db86b2SLiu Yuan #define QUORUM_OPT_READ_PATTERN "read-pattern"
40c88a1de5SBenoît Canet
4195c6bff3SBenoît Canet /* This union holds a vote hash value */
4295c6bff3SBenoît Canet typedef union QuorumVoteValue {
43488981a4SDaniel P. Berrange uint8_t h[HASH_LENGTH]; /* SHA-256 hash */
4495c6bff3SBenoît Canet int64_t l; /* simpler 64 bits hash */
4595c6bff3SBenoît Canet } QuorumVoteValue;
4695c6bff3SBenoît Canet
4795c6bff3SBenoît Canet /* A vote item */
4895c6bff3SBenoît Canet typedef struct QuorumVoteItem {
4995c6bff3SBenoît Canet int index;
5095c6bff3SBenoît Canet QLIST_ENTRY(QuorumVoteItem) next;
5195c6bff3SBenoît Canet } QuorumVoteItem;
5295c6bff3SBenoît Canet
5395c6bff3SBenoît Canet /* this structure is a vote version. A version is the set of votes sharing the
5495c6bff3SBenoît Canet * same vote value.
5595c6bff3SBenoît Canet * The set of votes will be tracked with the items field and its cardinality is
5695c6bff3SBenoît Canet * vote_count.
5795c6bff3SBenoît Canet */
5895c6bff3SBenoît Canet typedef struct QuorumVoteVersion {
5995c6bff3SBenoît Canet QuorumVoteValue value;
6095c6bff3SBenoît Canet int index;
6195c6bff3SBenoît Canet int vote_count;
6295c6bff3SBenoît Canet QLIST_HEAD(, QuorumVoteItem) items;
6395c6bff3SBenoît Canet QLIST_ENTRY(QuorumVoteVersion) next;
6495c6bff3SBenoît Canet } QuorumVoteVersion;
6595c6bff3SBenoît Canet
6695c6bff3SBenoît Canet /* this structure holds a group of vote versions together */
6795c6bff3SBenoît Canet typedef struct QuorumVotes {
6895c6bff3SBenoît Canet QLIST_HEAD(, QuorumVoteVersion) vote_list;
6995c6bff3SBenoît Canet bool (*compare)(QuorumVoteValue *a, QuorumVoteValue *b);
7095c6bff3SBenoît Canet } QuorumVotes;
7127cec15eSBenoît Canet
72cadebd7aSBenoît Canet /* the following structure holds the state of one quorum instance */
73cadebd7aSBenoît Canet typedef struct BDRVQuorumState {
740bd6e91aSKevin Wolf BdrvChild **children; /* children BlockDriverStates */
75cadebd7aSBenoît Canet int num_children; /* children count */
7698292c61SWen Congyang unsigned next_child_index; /* the index of the next child that should
7798292c61SWen Congyang * be added
7898292c61SWen Congyang */
79cadebd7aSBenoît Canet int threshold; /* if less than threshold children reads gave the
80cadebd7aSBenoît Canet * same result a quorum error occurs.
81cadebd7aSBenoît Canet */
82cadebd7aSBenoît Canet bool is_blkverify; /* true if the driver is in blkverify mode
83cadebd7aSBenoît Canet * Writes are mirrored on two children devices.
84cadebd7aSBenoît Canet * On reads the two children devices' contents are
85cadebd7aSBenoît Canet * compared and if a difference is spotted its
86cadebd7aSBenoît Canet * location is printed and the code aborts.
87cadebd7aSBenoît Canet * It is useful to debug other block drivers by
88cadebd7aSBenoît Canet * comparing them with a reference one.
89cadebd7aSBenoît Canet */
90cf29a570SBenoît Canet bool rewrite_corrupted;/* true if the driver must rewrite-on-read corrupted
91cf29a570SBenoît Canet * block if Quorum is reached.
92cf29a570SBenoît Canet */
93a9db86b2SLiu Yuan
94a9db86b2SLiu Yuan QuorumReadPattern read_pattern;
95cadebd7aSBenoît Canet } BDRVQuorumState;
96cadebd7aSBenoît Canet
9727cec15eSBenoît Canet typedef struct QuorumAIOCB QuorumAIOCB;
9827cec15eSBenoît Canet
9927cec15eSBenoît Canet /* Quorum will create one instance of the following structure per operation it
10027cec15eSBenoît Canet * performs on its children.
10127cec15eSBenoît Canet * So for each read/write operation coming from the upper layer there will be
10227cec15eSBenoît Canet * $children_count QuorumChildRequest.
10327cec15eSBenoît Canet */
10427cec15eSBenoît Canet typedef struct QuorumChildRequest {
105ce15dc08SKevin Wolf BlockDriverState *bs;
10627cec15eSBenoît Canet QEMUIOVector qiov;
10727cec15eSBenoît Canet uint8_t *buf;
10827cec15eSBenoît Canet int ret;
10927cec15eSBenoît Canet QuorumAIOCB *parent;
11027cec15eSBenoît Canet } QuorumChildRequest;
11127cec15eSBenoît Canet
11227cec15eSBenoît Canet /* Quorum will use the following structure to track progress of each read/write
11327cec15eSBenoît Canet * operation received by the upper layer.
11427cec15eSBenoît Canet * This structure hold pointers to the QuorumChildRequest structures instances
11527cec15eSBenoît Canet * used to do operations on each children and track overall progress.
11627cec15eSBenoît Canet */
11727cec15eSBenoît Canet struct QuorumAIOCB {
118ce15dc08SKevin Wolf BlockDriverState *bs;
119ce15dc08SKevin Wolf Coroutine *co;
12027cec15eSBenoît Canet
12127cec15eSBenoît Canet /* Request metadata */
1226847da38SKevin Wolf uint64_t offset;
1236847da38SKevin Wolf uint64_t bytes;
1241b1a920bSMax Reitz int flags;
12527cec15eSBenoît Canet
12627cec15eSBenoît Canet QEMUIOVector *qiov; /* calling IOV */
12727cec15eSBenoît Canet
12827cec15eSBenoît Canet QuorumChildRequest *qcrs; /* individual child requests */
12927cec15eSBenoît Canet int count; /* number of completed AIOCB */
13027cec15eSBenoît Canet int success_count; /* number of successfully completed AIOCB */
13127cec15eSBenoît Canet
132cf29a570SBenoît Canet int rewrite_count; /* number of replica to rewrite: count down to
133cf29a570SBenoît Canet * zero once writes are fired
134cf29a570SBenoît Canet */
135cf29a570SBenoît Canet
13695c6bff3SBenoît Canet QuorumVotes votes;
13795c6bff3SBenoît Canet
13827cec15eSBenoît Canet bool is_read;
13927cec15eSBenoît Canet int vote_ret;
14086ec252cSPaolo Bonzini int children_read; /* how many children have been read from */
14127cec15eSBenoît Canet };
142cadebd7aSBenoît Canet
143ce15dc08SKevin Wolf typedef struct QuorumCo {
144ce15dc08SKevin Wolf QuorumAIOCB *acb;
145ce15dc08SKevin Wolf int idx;
146ce15dc08SKevin Wolf } QuorumCo;
147ce15dc08SKevin Wolf
quorum_aio_finalize(QuorumAIOCB * acb)14813e7956eSBenoît Canet static void quorum_aio_finalize(QuorumAIOCB *acb)
14913e7956eSBenoît Canet {
15013e7956eSBenoît Canet g_free(acb->qcrs);
1510f31977dSKevin Wolf g_free(acb);
15213e7956eSBenoît Canet }
15313e7956eSBenoît Canet
quorum_sha256_compare(QuorumVoteValue * a,QuorumVoteValue * b)15495c6bff3SBenoît Canet static bool quorum_sha256_compare(QuorumVoteValue *a, QuorumVoteValue *b)
15595c6bff3SBenoît Canet {
15695c6bff3SBenoît Canet return !memcmp(a->h, b->h, HASH_LENGTH);
15795c6bff3SBenoît Canet }
15895c6bff3SBenoît Canet
quorum_64bits_compare(QuorumVoteValue * a,QuorumVoteValue * b)15995c6bff3SBenoît Canet static bool quorum_64bits_compare(QuorumVoteValue *a, QuorumVoteValue *b)
16095c6bff3SBenoît Canet {
16195c6bff3SBenoît Canet return a->l == b->l;
16295c6bff3SBenoît Canet }
16395c6bff3SBenoît Canet
quorum_aio_get(BlockDriverState * bs,QEMUIOVector * qiov,uint64_t offset,uint64_t bytes,int flags)1642987ae7dSPaolo Bonzini static QuorumAIOCB *coroutine_fn quorum_aio_get(BlockDriverState *bs,
16513e7956eSBenoît Canet QEMUIOVector *qiov,
1662987ae7dSPaolo Bonzini uint64_t offset, uint64_t bytes,
1671b1a920bSMax Reitz int flags)
16813e7956eSBenoît Canet {
16910c85519SKevin Wolf BDRVQuorumState *s = bs->opaque;
170ce15dc08SKevin Wolf QuorumAIOCB *acb = g_new(QuorumAIOCB, 1);
17113e7956eSBenoît Canet int i;
17213e7956eSBenoît Canet
1737c37f941SKevin Wolf *acb = (QuorumAIOCB) {
1747c37f941SKevin Wolf .co = qemu_coroutine_self(),
1757c37f941SKevin Wolf .bs = bs,
1767c37f941SKevin Wolf .offset = offset,
1777c37f941SKevin Wolf .bytes = bytes,
1781b1a920bSMax Reitz .flags = flags,
1797c37f941SKevin Wolf .qiov = qiov,
1807c37f941SKevin Wolf .votes.compare = quorum_sha256_compare,
1817c37f941SKevin Wolf .votes.vote_list = QLIST_HEAD_INITIALIZER(acb.votes.vote_list),
1827c37f941SKevin Wolf };
18313e7956eSBenoît Canet
1847c37f941SKevin Wolf acb->qcrs = g_new0(QuorumChildRequest, s->num_children);
18513e7956eSBenoît Canet for (i = 0; i < s->num_children; i++) {
18613e7956eSBenoît Canet acb->qcrs[i].buf = NULL;
18713e7956eSBenoît Canet acb->qcrs[i].ret = 0;
18813e7956eSBenoît Canet acb->qcrs[i].parent = acb;
18913e7956eSBenoît Canet }
19013e7956eSBenoît Canet
19113e7956eSBenoît Canet return acb;
19213e7956eSBenoît Canet }
19313e7956eSBenoît Canet
quorum_report_bad(QuorumOpType type,uint64_t offset,uint64_t bytes,char * node_name,int ret)1946847da38SKevin Wolf static void quorum_report_bad(QuorumOpType type, uint64_t offset,
1956847da38SKevin Wolf uint64_t bytes, char *node_name, int ret)
19695c6bff3SBenoît Canet {
197fe069d9dSWenchao Xia const char *msg = NULL;
1986847da38SKevin Wolf int64_t start_sector = offset / BDRV_SECTOR_SIZE;
1996847da38SKevin Wolf int64_t end_sector = DIV_ROUND_UP(offset + bytes, BDRV_SECTOR_SIZE);
2006847da38SKevin Wolf
2010c762736SBenoît Canet if (ret < 0) {
202fe069d9dSWenchao Xia msg = strerror(-ret);
2030c762736SBenoît Canet }
2040ae053b7SChanglong Xie
20554fde4ffSMarkus Armbruster qapi_event_send_quorum_report_bad(type, msg, node_name, start_sector,
2063ab72385SPeter Xu end_sector - start_sector);
20795c6bff3SBenoît Canet }
20895c6bff3SBenoît Canet
quorum_report_failure(QuorumAIOCB * acb)2094026f1c4SKevin Wolf static void GRAPH_RDLOCK quorum_report_failure(QuorumAIOCB *acb)
21095c6bff3SBenoît Canet {
211ce15dc08SKevin Wolf const char *reference = bdrv_get_device_or_node_name(acb->bs);
2126847da38SKevin Wolf int64_t start_sector = acb->offset / BDRV_SECTOR_SIZE;
2136847da38SKevin Wolf int64_t end_sector = DIV_ROUND_UP(acb->offset + acb->bytes,
2146847da38SKevin Wolf BDRV_SECTOR_SIZE);
2156847da38SKevin Wolf
2166847da38SKevin Wolf qapi_event_send_quorum_failure(reference, start_sector,
2173ab72385SPeter Xu end_sector - start_sector);
21895c6bff3SBenoît Canet }
21995c6bff3SBenoît Canet
22095c6bff3SBenoît Canet static int quorum_vote_error(QuorumAIOCB *acb);
22195c6bff3SBenoît Canet
quorum_has_too_much_io_failed(QuorumAIOCB * acb)2224026f1c4SKevin Wolf static bool GRAPH_RDLOCK quorum_has_too_much_io_failed(QuorumAIOCB *acb)
22395c6bff3SBenoît Canet {
224ce15dc08SKevin Wolf BDRVQuorumState *s = acb->bs->opaque;
22595c6bff3SBenoît Canet
22695c6bff3SBenoît Canet if (acb->success_count < s->threshold) {
22795c6bff3SBenoît Canet acb->vote_ret = quorum_vote_error(acb);
22895c6bff3SBenoît Canet quorum_report_failure(acb);
22995c6bff3SBenoît Canet return true;
23095c6bff3SBenoît Canet }
23195c6bff3SBenoît Canet
23295c6bff3SBenoît Canet return false;
23395c6bff3SBenoît Canet }
23495c6bff3SBenoît Canet
quorum_copy_qiov(QEMUIOVector * dest,QEMUIOVector * source)235a9db86b2SLiu Yuan static void quorum_copy_qiov(QEMUIOVector *dest, QEMUIOVector *source)
236a9db86b2SLiu Yuan {
237a9db86b2SLiu Yuan int i;
238a9db86b2SLiu Yuan assert(dest->niov == source->niov);
239a9db86b2SLiu Yuan assert(dest->size == source->size);
240a9db86b2SLiu Yuan for (i = 0; i < source->niov; i++) {
241a9db86b2SLiu Yuan assert(dest->iov[i].iov_len == source->iov[i].iov_len);
242a9db86b2SLiu Yuan memcpy(dest->iov[i].iov_base,
243a9db86b2SLiu Yuan source->iov[i].iov_base,
244a9db86b2SLiu Yuan source->iov[i].iov_len);
245a9db86b2SLiu Yuan }
246a9db86b2SLiu Yuan }
247a9db86b2SLiu Yuan
quorum_report_bad_acb(QuorumChildRequest * sacb,int ret)2481ba7e159SPaolo Bonzini static void quorum_report_bad_acb(QuorumChildRequest *sacb, int ret)
2491ba7e159SPaolo Bonzini {
2501ba7e159SPaolo Bonzini QuorumAIOCB *acb = sacb->parent;
2511ba7e159SPaolo Bonzini QuorumOpType type = acb->is_read ? QUORUM_OP_TYPE_READ : QUORUM_OP_TYPE_WRITE;
2526847da38SKevin Wolf quorum_report_bad(type, acb->offset, acb->bytes, sacb->bs->node_name, ret);
2531ba7e159SPaolo Bonzini }
2541ba7e159SPaolo Bonzini
quorum_report_bad_versions(BDRVQuorumState * s,QuorumAIOCB * acb,QuorumVoteValue * value)25595c6bff3SBenoît Canet static void quorum_report_bad_versions(BDRVQuorumState *s,
25695c6bff3SBenoît Canet QuorumAIOCB *acb,
25795c6bff3SBenoît Canet QuorumVoteValue *value)
25895c6bff3SBenoît Canet {
25995c6bff3SBenoît Canet QuorumVoteVersion *version;
26095c6bff3SBenoît Canet QuorumVoteItem *item;
26195c6bff3SBenoît Canet
26295c6bff3SBenoît Canet QLIST_FOREACH(version, &acb->votes.vote_list, next) {
26395c6bff3SBenoît Canet if (acb->votes.compare(&version->value, value)) {
26495c6bff3SBenoît Canet continue;
26595c6bff3SBenoît Canet }
26695c6bff3SBenoît Canet QLIST_FOREACH(item, &version->items, next) {
2676847da38SKevin Wolf quorum_report_bad(QUORUM_OP_TYPE_READ, acb->offset, acb->bytes,
2680ae053b7SChanglong Xie s->children[item->index]->bs->node_name, 0);
26995c6bff3SBenoît Canet }
27095c6bff3SBenoît Canet }
27195c6bff3SBenoît Canet }
27295c6bff3SBenoît Canet
273b9b10c35SKevin Wolf /*
274b9b10c35SKevin Wolf * This function can count as GRAPH_RDLOCK because read_quorum_children() holds
275b9b10c35SKevin Wolf * the graph lock and keeps it until this coroutine has terminated.
276b9b10c35SKevin Wolf */
quorum_rewrite_entry(void * opaque)277b9b10c35SKevin Wolf static void coroutine_fn GRAPH_RDLOCK quorum_rewrite_entry(void *opaque)
278dee66e28SKevin Wolf {
279dee66e28SKevin Wolf QuorumCo *co = opaque;
280dee66e28SKevin Wolf QuorumAIOCB *acb = co->acb;
281dee66e28SKevin Wolf BDRVQuorumState *s = acb->bs->opaque;
282dee66e28SKevin Wolf
283dee66e28SKevin Wolf /* Ignore any errors, it's just a correction attempt for already
2841b1a920bSMax Reitz * corrupted data.
2851b1a920bSMax Reitz * Mask out BDRV_REQ_WRITE_UNCHANGED because this overwrites the
2861b1a920bSMax Reitz * area with different data from the other children. */
2876847da38SKevin Wolf bdrv_co_pwritev(s->children[co->idx], acb->offset, acb->bytes,
2881b1a920bSMax Reitz acb->qiov, acb->flags & ~BDRV_REQ_WRITE_UNCHANGED);
289dee66e28SKevin Wolf
290dee66e28SKevin Wolf /* Wake up the caller after the last rewrite */
291dee66e28SKevin Wolf acb->rewrite_count--;
292dee66e28SKevin Wolf if (!acb->rewrite_count) {
293dee66e28SKevin Wolf qemu_coroutine_enter_if_inactive(acb->co);
294dee66e28SKevin Wolf }
295dee66e28SKevin Wolf }
296dee66e28SKevin Wolf
297b9b10c35SKevin Wolf static bool coroutine_fn GRAPH_RDLOCK
quorum_rewrite_bad_versions(QuorumAIOCB * acb,QuorumVoteValue * value)298b9b10c35SKevin Wolf quorum_rewrite_bad_versions(QuorumAIOCB *acb, QuorumVoteValue *value)
299cf29a570SBenoît Canet {
300cf29a570SBenoît Canet QuorumVoteVersion *version;
301cf29a570SBenoît Canet QuorumVoteItem *item;
302cf29a570SBenoît Canet int count = 0;
303cf29a570SBenoît Canet
304cf29a570SBenoît Canet /* first count the number of bad versions: done first to avoid concurrency
305cf29a570SBenoît Canet * issues.
306cf29a570SBenoît Canet */
307cf29a570SBenoît Canet QLIST_FOREACH(version, &acb->votes.vote_list, next) {
308cf29a570SBenoît Canet if (acb->votes.compare(&version->value, value)) {
309cf29a570SBenoît Canet continue;
310cf29a570SBenoît Canet }
311cf29a570SBenoît Canet QLIST_FOREACH(item, &version->items, next) {
312cf29a570SBenoît Canet count++;
313cf29a570SBenoît Canet }
314cf29a570SBenoît Canet }
315cf29a570SBenoît Canet
316dee66e28SKevin Wolf /* quorum_rewrite_entry will count down this to zero */
317cf29a570SBenoît Canet acb->rewrite_count = count;
318cf29a570SBenoît Canet
319cf29a570SBenoît Canet /* now fire the correcting rewrites */
320cf29a570SBenoît Canet QLIST_FOREACH(version, &acb->votes.vote_list, next) {
321cf29a570SBenoît Canet if (acb->votes.compare(&version->value, value)) {
322cf29a570SBenoît Canet continue;
323cf29a570SBenoît Canet }
324cf29a570SBenoît Canet QLIST_FOREACH(item, &version->items, next) {
325dee66e28SKevin Wolf Coroutine *co;
326dee66e28SKevin Wolf QuorumCo data = {
327dee66e28SKevin Wolf .acb = acb,
328dee66e28SKevin Wolf .idx = item->index,
329dee66e28SKevin Wolf };
330dee66e28SKevin Wolf
331dee66e28SKevin Wolf co = qemu_coroutine_create(quorum_rewrite_entry, &data);
332dee66e28SKevin Wolf qemu_coroutine_enter(co);
333cf29a570SBenoît Canet }
334cf29a570SBenoît Canet }
335cf29a570SBenoît Canet
336cf29a570SBenoît Canet /* return true if any rewrite is done else false */
337cf29a570SBenoît Canet return count;
338cf29a570SBenoît Canet }
339cf29a570SBenoît Canet
quorum_count_vote(QuorumVotes * votes,QuorumVoteValue * value,int index)34095c6bff3SBenoît Canet static void quorum_count_vote(QuorumVotes *votes,
34195c6bff3SBenoît Canet QuorumVoteValue *value,
34295c6bff3SBenoît Canet int index)
34395c6bff3SBenoît Canet {
34495c6bff3SBenoît Canet QuorumVoteVersion *v = NULL, *version = NULL;
34595c6bff3SBenoît Canet QuorumVoteItem *item;
34695c6bff3SBenoît Canet
34795c6bff3SBenoît Canet /* look if we have something with this hash */
34895c6bff3SBenoît Canet QLIST_FOREACH(v, &votes->vote_list, next) {
34995c6bff3SBenoît Canet if (votes->compare(&v->value, value)) {
35095c6bff3SBenoît Canet version = v;
35195c6bff3SBenoît Canet break;
35295c6bff3SBenoît Canet }
35395c6bff3SBenoît Canet }
35495c6bff3SBenoît Canet
35595c6bff3SBenoît Canet /* It's a version not yet in the list add it */
35695c6bff3SBenoît Canet if (!version) {
35795c6bff3SBenoît Canet version = g_new0(QuorumVoteVersion, 1);
35895c6bff3SBenoît Canet QLIST_INIT(&version->items);
35995c6bff3SBenoît Canet memcpy(&version->value, value, sizeof(version->value));
36095c6bff3SBenoît Canet version->index = index;
36195c6bff3SBenoît Canet version->vote_count = 0;
36295c6bff3SBenoît Canet QLIST_INSERT_HEAD(&votes->vote_list, version, next);
36395c6bff3SBenoît Canet }
36495c6bff3SBenoît Canet
36595c6bff3SBenoît Canet version->vote_count++;
36695c6bff3SBenoît Canet
36795c6bff3SBenoît Canet item = g_new0(QuorumVoteItem, 1);
36895c6bff3SBenoît Canet item->index = index;
36995c6bff3SBenoît Canet QLIST_INSERT_HEAD(&version->items, item, next);
37095c6bff3SBenoît Canet }
37195c6bff3SBenoît Canet
quorum_free_vote_list(QuorumVotes * votes)37295c6bff3SBenoît Canet static void quorum_free_vote_list(QuorumVotes *votes)
37395c6bff3SBenoît Canet {
37495c6bff3SBenoît Canet QuorumVoteVersion *version, *next_version;
37595c6bff3SBenoît Canet QuorumVoteItem *item, *next_item;
37695c6bff3SBenoît Canet
37795c6bff3SBenoît Canet QLIST_FOREACH_SAFE(version, &votes->vote_list, next, next_version) {
37895c6bff3SBenoît Canet QLIST_REMOVE(version, next);
37995c6bff3SBenoît Canet QLIST_FOREACH_SAFE(item, &version->items, next, next_item) {
38095c6bff3SBenoît Canet QLIST_REMOVE(item, next);
38195c6bff3SBenoît Canet g_free(item);
38295c6bff3SBenoît Canet }
38395c6bff3SBenoît Canet g_free(version);
38495c6bff3SBenoît Canet }
38595c6bff3SBenoît Canet }
38695c6bff3SBenoît Canet
quorum_compute_hash(QuorumAIOCB * acb,int i,QuorumVoteValue * hash)38795c6bff3SBenoît Canet static int quorum_compute_hash(QuorumAIOCB *acb, int i, QuorumVoteValue *hash)
38895c6bff3SBenoît Canet {
38995c6bff3SBenoît Canet QEMUIOVector *qiov = &acb->qcrs[i].qiov;
390488981a4SDaniel P. Berrange size_t len = sizeof(hash->h);
391488981a4SDaniel P. Berrange uint8_t *data = hash->h;
39295c6bff3SBenoît Canet
393488981a4SDaniel P. Berrange /* XXX - would be nice if we could pass in the Error **
394488981a4SDaniel P. Berrange * and propagate that back, but this quorum code is
395488981a4SDaniel P. Berrange * restricted to just errno values currently */
396*ef834aa2SMarkus Armbruster if (qcrypto_hash_bytesv(QCRYPTO_HASH_ALGO_SHA256,
397488981a4SDaniel P. Berrange qiov->iov, qiov->niov,
398488981a4SDaniel P. Berrange &data, &len,
399488981a4SDaniel P. Berrange NULL) < 0) {
400488981a4SDaniel P. Berrange return -EINVAL;
40195c6bff3SBenoît Canet }
40295c6bff3SBenoît Canet
403488981a4SDaniel P. Berrange return 0;
40495c6bff3SBenoît Canet }
40595c6bff3SBenoît Canet
quorum_get_vote_winner(QuorumVotes * votes)40695c6bff3SBenoît Canet static QuorumVoteVersion *quorum_get_vote_winner(QuorumVotes *votes)
40795c6bff3SBenoît Canet {
40895c6bff3SBenoît Canet int max = 0;
40995c6bff3SBenoît Canet QuorumVoteVersion *candidate, *winner = NULL;
41095c6bff3SBenoît Canet
41195c6bff3SBenoît Canet QLIST_FOREACH(candidate, &votes->vote_list, next) {
41295c6bff3SBenoît Canet if (candidate->vote_count > max) {
41395c6bff3SBenoît Canet max = candidate->vote_count;
41495c6bff3SBenoît Canet winner = candidate;
41595c6bff3SBenoît Canet }
41695c6bff3SBenoît Canet }
41795c6bff3SBenoît Canet
41895c6bff3SBenoît Canet return winner;
41995c6bff3SBenoît Canet }
42095c6bff3SBenoît Canet
42195c6bff3SBenoît Canet /* qemu_iovec_compare is handy for blkverify mode because it returns the first
42295c6bff3SBenoît Canet * differing byte location. Yet it is handcoded to compare vectors one byte
42395c6bff3SBenoît Canet * after another so it does not benefit from the libc SIMD optimizations.
42495c6bff3SBenoît Canet * quorum_iovec_compare is written for speed and should be used in the non
42595c6bff3SBenoît Canet * blkverify mode of quorum.
42695c6bff3SBenoît Canet */
quorum_iovec_compare(QEMUIOVector * a,QEMUIOVector * b)42795c6bff3SBenoît Canet static bool quorum_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
42895c6bff3SBenoît Canet {
42995c6bff3SBenoît Canet int i;
43095c6bff3SBenoît Canet int result;
43195c6bff3SBenoît Canet
43295c6bff3SBenoît Canet assert(a->niov == b->niov);
43395c6bff3SBenoît Canet for (i = 0; i < a->niov; i++) {
43495c6bff3SBenoît Canet assert(a->iov[i].iov_len == b->iov[i].iov_len);
43595c6bff3SBenoît Canet result = memcmp(a->iov[i].iov_base,
43695c6bff3SBenoît Canet b->iov[i].iov_base,
43795c6bff3SBenoît Canet a->iov[i].iov_len);
43895c6bff3SBenoît Canet if (result) {
43995c6bff3SBenoît Canet return false;
44095c6bff3SBenoît Canet }
44195c6bff3SBenoît Canet }
44295c6bff3SBenoît Canet
44395c6bff3SBenoît Canet return true;
44495c6bff3SBenoît Canet }
44595c6bff3SBenoît Canet
quorum_compare(QuorumAIOCB * acb,QEMUIOVector * a,QEMUIOVector * b)4466840e8d8SAlberto Garcia static bool quorum_compare(QuorumAIOCB *acb, QEMUIOVector *a, QEMUIOVector *b)
44795c6bff3SBenoît Canet {
448ce15dc08SKevin Wolf BDRVQuorumState *s = acb->bs->opaque;
44995c6bff3SBenoît Canet ssize_t offset;
45095c6bff3SBenoît Canet
45195c6bff3SBenoît Canet /* This driver will replace blkverify in this particular case */
45295c6bff3SBenoît Canet if (s->is_blkverify) {
45395c6bff3SBenoît Canet offset = qemu_iovec_compare(a, b);
45495c6bff3SBenoît Canet if (offset != -1) {
4556840e8d8SAlberto Garcia fprintf(stderr, "quorum: offset=%" PRIu64 " bytes=%" PRIu64
4566840e8d8SAlberto Garcia " contents mismatch at offset %" PRIu64 "\n",
4576840e8d8SAlberto Garcia acb->offset, acb->bytes, acb->offset + offset);
4586840e8d8SAlberto Garcia exit(1);
45995c6bff3SBenoît Canet }
46095c6bff3SBenoît Canet return true;
46195c6bff3SBenoît Canet }
46295c6bff3SBenoît Canet
46395c6bff3SBenoît Canet return quorum_iovec_compare(a, b);
46495c6bff3SBenoît Canet }
46595c6bff3SBenoît Canet
46695c6bff3SBenoît Canet /* Do a vote to get the error code */
quorum_vote_error(QuorumAIOCB * acb)46795c6bff3SBenoît Canet static int quorum_vote_error(QuorumAIOCB *acb)
46895c6bff3SBenoît Canet {
469ce15dc08SKevin Wolf BDRVQuorumState *s = acb->bs->opaque;
47095c6bff3SBenoît Canet QuorumVoteVersion *winner = NULL;
47195c6bff3SBenoît Canet QuorumVotes error_votes;
47295c6bff3SBenoît Canet QuorumVoteValue result_value;
47395c6bff3SBenoît Canet int i, ret = 0;
47495c6bff3SBenoît Canet bool error = false;
47595c6bff3SBenoît Canet
47695c6bff3SBenoît Canet QLIST_INIT(&error_votes.vote_list);
47795c6bff3SBenoît Canet error_votes.compare = quorum_64bits_compare;
47895c6bff3SBenoît Canet
47995c6bff3SBenoît Canet for (i = 0; i < s->num_children; i++) {
48095c6bff3SBenoît Canet ret = acb->qcrs[i].ret;
48195c6bff3SBenoît Canet if (ret) {
48295c6bff3SBenoît Canet error = true;
48395c6bff3SBenoît Canet result_value.l = ret;
48495c6bff3SBenoît Canet quorum_count_vote(&error_votes, &result_value, i);
48595c6bff3SBenoît Canet }
48695c6bff3SBenoît Canet }
48795c6bff3SBenoît Canet
48895c6bff3SBenoît Canet if (error) {
48995c6bff3SBenoît Canet winner = quorum_get_vote_winner(&error_votes);
49095c6bff3SBenoît Canet ret = winner->value.l;
49195c6bff3SBenoît Canet }
49295c6bff3SBenoît Canet
49395c6bff3SBenoît Canet quorum_free_vote_list(&error_votes);
49495c6bff3SBenoît Canet
49595c6bff3SBenoît Canet return ret;
49695c6bff3SBenoît Canet }
49795c6bff3SBenoît Canet
quorum_vote(QuorumAIOCB * acb)498b9b10c35SKevin Wolf static void coroutine_fn GRAPH_RDLOCK quorum_vote(QuorumAIOCB *acb)
49995c6bff3SBenoît Canet {
50095c6bff3SBenoît Canet bool quorum = true;
50195c6bff3SBenoît Canet int i, j, ret;
50295c6bff3SBenoît Canet QuorumVoteValue hash;
503ce15dc08SKevin Wolf BDRVQuorumState *s = acb->bs->opaque;
50495c6bff3SBenoît Canet QuorumVoteVersion *winner;
50595c6bff3SBenoît Canet
50695c6bff3SBenoît Canet if (quorum_has_too_much_io_failed(acb)) {
5077cd9b396SKevin Wolf return;
50895c6bff3SBenoît Canet }
50995c6bff3SBenoît Canet
51095c6bff3SBenoît Canet /* get the index of the first successful read */
51195c6bff3SBenoît Canet for (i = 0; i < s->num_children; i++) {
51295c6bff3SBenoît Canet if (!acb->qcrs[i].ret) {
51395c6bff3SBenoît Canet break;
51495c6bff3SBenoît Canet }
51595c6bff3SBenoît Canet }
51695c6bff3SBenoît Canet
51795c6bff3SBenoît Canet assert(i < s->num_children);
51895c6bff3SBenoît Canet
51995c6bff3SBenoît Canet /* compare this read with all other successful reads stopping at quorum
52095c6bff3SBenoît Canet * failure
52195c6bff3SBenoît Canet */
52295c6bff3SBenoît Canet for (j = i + 1; j < s->num_children; j++) {
52395c6bff3SBenoît Canet if (acb->qcrs[j].ret) {
52495c6bff3SBenoît Canet continue;
52595c6bff3SBenoît Canet }
52695c6bff3SBenoît Canet quorum = quorum_compare(acb, &acb->qcrs[i].qiov, &acb->qcrs[j].qiov);
52795c6bff3SBenoît Canet if (!quorum) {
52895c6bff3SBenoît Canet break;
52995c6bff3SBenoît Canet }
53095c6bff3SBenoît Canet }
53195c6bff3SBenoît Canet
53295c6bff3SBenoît Canet /* Every successful read agrees */
53395c6bff3SBenoît Canet if (quorum) {
53495c6bff3SBenoît Canet quorum_copy_qiov(acb->qiov, &acb->qcrs[i].qiov);
5357cd9b396SKevin Wolf return;
53695c6bff3SBenoît Canet }
53795c6bff3SBenoît Canet
53895c6bff3SBenoît Canet /* compute hashes for each successful read, also store indexes */
53995c6bff3SBenoît Canet for (i = 0; i < s->num_children; i++) {
54095c6bff3SBenoît Canet if (acb->qcrs[i].ret) {
54195c6bff3SBenoît Canet continue;
54295c6bff3SBenoît Canet }
54395c6bff3SBenoît Canet ret = quorum_compute_hash(acb, i, &hash);
54495c6bff3SBenoît Canet /* if ever the hash computation failed */
54595c6bff3SBenoît Canet if (ret < 0) {
54695c6bff3SBenoît Canet acb->vote_ret = ret;
54795c6bff3SBenoît Canet goto free_exit;
54895c6bff3SBenoît Canet }
54995c6bff3SBenoît Canet quorum_count_vote(&acb->votes, &hash, i);
55095c6bff3SBenoît Canet }
55195c6bff3SBenoît Canet
55295c6bff3SBenoît Canet /* vote to select the most represented version */
55395c6bff3SBenoît Canet winner = quorum_get_vote_winner(&acb->votes);
55495c6bff3SBenoît Canet
55595c6bff3SBenoît Canet /* if the winner count is smaller than threshold the read fails */
55695c6bff3SBenoît Canet if (winner->vote_count < s->threshold) {
55795c6bff3SBenoît Canet quorum_report_failure(acb);
55895c6bff3SBenoît Canet acb->vote_ret = -EIO;
55995c6bff3SBenoît Canet goto free_exit;
56095c6bff3SBenoît Canet }
56195c6bff3SBenoît Canet
56295c6bff3SBenoît Canet /* we have a winner: copy it */
56395c6bff3SBenoît Canet quorum_copy_qiov(acb->qiov, &acb->qcrs[winner->index].qiov);
56495c6bff3SBenoît Canet
56595c6bff3SBenoît Canet /* some versions are bad print them */
56695c6bff3SBenoît Canet quorum_report_bad_versions(s, acb, &winner->value);
56795c6bff3SBenoît Canet
568cf29a570SBenoît Canet /* corruption correction is enabled */
569cf29a570SBenoît Canet if (s->rewrite_corrupted) {
570dee66e28SKevin Wolf quorum_rewrite_bad_versions(acb, &winner->value);
571cf29a570SBenoît Canet }
572cf29a570SBenoît Canet
57395c6bff3SBenoît Canet free_exit:
57495c6bff3SBenoît Canet /* free lists */
57595c6bff3SBenoît Canet quorum_free_vote_list(&acb->votes);
57695c6bff3SBenoît Canet }
57795c6bff3SBenoît Canet
578b9b10c35SKevin Wolf /*
579b9b10c35SKevin Wolf * This function can count as GRAPH_RDLOCK because read_quorum_children() holds
580b9b10c35SKevin Wolf * the graph lock and keeps it until this coroutine has terminated.
581b9b10c35SKevin Wolf */
read_quorum_children_entry(void * opaque)582b9b10c35SKevin Wolf static void coroutine_fn GRAPH_RDLOCK read_quorum_children_entry(void *opaque)
583a9db86b2SLiu Yuan {
584ce15dc08SKevin Wolf QuorumCo *co = opaque;
585ce15dc08SKevin Wolf QuorumAIOCB *acb = co->acb;
586ce15dc08SKevin Wolf BDRVQuorumState *s = acb->bs->opaque;
587ce15dc08SKevin Wolf int i = co->idx;
5887cd9b396SKevin Wolf QuorumChildRequest *sacb = &acb->qcrs[i];
589ce15dc08SKevin Wolf
5907cd9b396SKevin Wolf sacb->bs = s->children[i]->bs;
5916847da38SKevin Wolf sacb->ret = bdrv_co_preadv(s->children[i], acb->offset, acb->bytes,
592ce15dc08SKevin Wolf &acb->qcrs[i].qiov, 0);
5937cd9b396SKevin Wolf
5947cd9b396SKevin Wolf if (sacb->ret == 0) {
5957cd9b396SKevin Wolf acb->success_count++;
5967cd9b396SKevin Wolf } else {
5977cd9b396SKevin Wolf quorum_report_bad_acb(sacb, sacb->ret);
5987cd9b396SKevin Wolf }
5997cd9b396SKevin Wolf
6007cd9b396SKevin Wolf acb->count++;
6017cd9b396SKevin Wolf assert(acb->count <= s->num_children);
6027cd9b396SKevin Wolf assert(acb->success_count <= s->num_children);
6037cd9b396SKevin Wolf
6047cd9b396SKevin Wolf /* Wake up the caller after the last read */
6057cd9b396SKevin Wolf if (acb->count == s->num_children) {
6067cd9b396SKevin Wolf qemu_coroutine_enter_if_inactive(acb->co);
6077cd9b396SKevin Wolf }
608ce15dc08SKevin Wolf }
609ce15dc08SKevin Wolf
read_quorum_children(QuorumAIOCB * acb)610b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK read_quorum_children(QuorumAIOCB *acb)
611ce15dc08SKevin Wolf {
612ce15dc08SKevin Wolf BDRVQuorumState *s = acb->bs->opaque;
6134a4ff4c5SLaurent Vivier int i;
614a9db86b2SLiu Yuan
61586ec252cSPaolo Bonzini acb->children_read = s->num_children;
616a9db86b2SLiu Yuan for (i = 0; i < s->num_children; i++) {
6170bd6e91aSKevin Wolf acb->qcrs[i].buf = qemu_blockalign(s->children[i]->bs, acb->qiov->size);
618a9db86b2SLiu Yuan qemu_iovec_init(&acb->qcrs[i].qiov, acb->qiov->niov);
619a9db86b2SLiu Yuan qemu_iovec_clone(&acb->qcrs[i].qiov, acb->qiov, acb->qcrs[i].buf);
620a9db86b2SLiu Yuan }
621a9db86b2SLiu Yuan
622a9db86b2SLiu Yuan for (i = 0; i < s->num_children; i++) {
623ce15dc08SKevin Wolf Coroutine *co;
624ce15dc08SKevin Wolf QuorumCo data = {
625ce15dc08SKevin Wolf .acb = acb,
626ce15dc08SKevin Wolf .idx = i,
627ce15dc08SKevin Wolf };
628ce15dc08SKevin Wolf
629ce15dc08SKevin Wolf co = qemu_coroutine_create(read_quorum_children_entry, &data);
630ce15dc08SKevin Wolf qemu_coroutine_enter(co);
631a9db86b2SLiu Yuan }
632a9db86b2SLiu Yuan
6337cd9b396SKevin Wolf while (acb->count < s->num_children) {
6347cd9b396SKevin Wolf qemu_coroutine_yield();
6357cd9b396SKevin Wolf }
6367cd9b396SKevin Wolf
6377cd9b396SKevin Wolf /* Do the vote on read */
6387cd9b396SKevin Wolf quorum_vote(acb);
6397cd9b396SKevin Wolf for (i = 0; i < s->num_children; i++) {
6407cd9b396SKevin Wolf qemu_vfree(acb->qcrs[i].buf);
6417cd9b396SKevin Wolf qemu_iovec_destroy(&acb->qcrs[i].qiov);
6427cd9b396SKevin Wolf }
6437cd9b396SKevin Wolf
6447cd9b396SKevin Wolf while (acb->rewrite_count) {
645ce15dc08SKevin Wolf qemu_coroutine_yield();
646a9db86b2SLiu Yuan }
647a9db86b2SLiu Yuan
6484a4ff4c5SLaurent Vivier return acb->vote_ret;
649ce15dc08SKevin Wolf }
650ce15dc08SKevin Wolf
read_fifo_child(QuorumAIOCB * acb)651b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK read_fifo_child(QuorumAIOCB *acb)
652a9db86b2SLiu Yuan {
653ce15dc08SKevin Wolf BDRVQuorumState *s = acb->bs->opaque;
654a7e15902SKevin Wolf int n, ret;
655a9db86b2SLiu Yuan
656a7e15902SKevin Wolf /* We try to read the next child in FIFO order if we failed to read */
657a7e15902SKevin Wolf do {
658a7e15902SKevin Wolf n = acb->children_read++;
659ce15dc08SKevin Wolf acb->qcrs[n].bs = s->children[n]->bs;
660a7e15902SKevin Wolf ret = bdrv_co_preadv(s->children[n], acb->offset, acb->bytes,
661a7e15902SKevin Wolf acb->qiov, 0);
662a7e15902SKevin Wolf if (ret < 0) {
663a7e15902SKevin Wolf quorum_report_bad_acb(&acb->qcrs[n], ret);
664a7e15902SKevin Wolf }
665a7e15902SKevin Wolf } while (ret < 0 && acb->children_read < s->num_children);
666a7e15902SKevin Wolf
667a7e15902SKevin Wolf /* FIXME: rewrite failed children if acb->children_read > 1? */
668a9db86b2SLiu Yuan
669ce15dc08SKevin Wolf return ret;
670a9db86b2SLiu Yuan }
671a9db86b2SLiu Yuan
672b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
quorum_co_preadv(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,BdrvRequestFlags flags)673b9b10c35SKevin Wolf quorum_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
674b9b10c35SKevin Wolf QEMUIOVector *qiov, BdrvRequestFlags flags)
6757db6982aSBenoît Canet {
6767db6982aSBenoît Canet BDRVQuorumState *s = bs->opaque;
6771b1a920bSMax Reitz QuorumAIOCB *acb = quorum_aio_get(bs, qiov, offset, bytes, flags);
678ce15dc08SKevin Wolf int ret;
679ce15dc08SKevin Wolf
6807db6982aSBenoît Canet acb->is_read = true;
68186ec252cSPaolo Bonzini acb->children_read = 0;
6827db6982aSBenoît Canet
683a9db86b2SLiu Yuan if (s->read_pattern == QUORUM_READ_PATTERN_QUORUM) {
684ce15dc08SKevin Wolf ret = read_quorum_children(acb);
685ce15dc08SKevin Wolf } else {
686ce15dc08SKevin Wolf ret = read_fifo_child(acb);
687ce15dc08SKevin Wolf }
6880f31977dSKevin Wolf quorum_aio_finalize(acb);
6890f31977dSKevin Wolf
690ce15dc08SKevin Wolf return ret;
6917db6982aSBenoît Canet }
6927db6982aSBenoît Canet
693abaf8b75SKevin Wolf /*
694abaf8b75SKevin Wolf * This function can count as GRAPH_RDLOCK because quorum_co_pwritev() holds the
695abaf8b75SKevin Wolf * graph lock and keeps it until this coroutine has terminated.
696abaf8b75SKevin Wolf */
write_quorum_entry(void * opaque)697abaf8b75SKevin Wolf static void coroutine_fn GRAPH_RDLOCK write_quorum_entry(void *opaque)
698ce15dc08SKevin Wolf {
699ce15dc08SKevin Wolf QuorumCo *co = opaque;
700ce15dc08SKevin Wolf QuorumAIOCB *acb = co->acb;
701ce15dc08SKevin Wolf BDRVQuorumState *s = acb->bs->opaque;
702ce15dc08SKevin Wolf int i = co->idx;
7037cd9b396SKevin Wolf QuorumChildRequest *sacb = &acb->qcrs[i];
704ce15dc08SKevin Wolf
7057cd9b396SKevin Wolf sacb->bs = s->children[i]->bs;
7065cddb2e9SAlberto Garcia if (acb->flags & BDRV_REQ_ZERO_WRITE) {
7075cddb2e9SAlberto Garcia sacb->ret = bdrv_co_pwrite_zeroes(s->children[i], acb->offset,
7085cddb2e9SAlberto Garcia acb->bytes, acb->flags);
7095cddb2e9SAlberto Garcia } else {
7106847da38SKevin Wolf sacb->ret = bdrv_co_pwritev(s->children[i], acb->offset, acb->bytes,
7111b1a920bSMax Reitz acb->qiov, acb->flags);
7125cddb2e9SAlberto Garcia }
7137cd9b396SKevin Wolf if (sacb->ret == 0) {
7147cd9b396SKevin Wolf acb->success_count++;
7157cd9b396SKevin Wolf } else {
7167cd9b396SKevin Wolf quorum_report_bad_acb(sacb, sacb->ret);
7177cd9b396SKevin Wolf }
7187cd9b396SKevin Wolf acb->count++;
7197cd9b396SKevin Wolf assert(acb->count <= s->num_children);
7207cd9b396SKevin Wolf assert(acb->success_count <= s->num_children);
7217cd9b396SKevin Wolf
7227cd9b396SKevin Wolf /* Wake up the caller after the last write */
7237cd9b396SKevin Wolf if (acb->count == s->num_children) {
7247cd9b396SKevin Wolf qemu_coroutine_enter_if_inactive(acb->co);
7257cd9b396SKevin Wolf }
7267db6982aSBenoît Canet }
7277db6982aSBenoît Canet
7287b1fb72eSKevin Wolf static int coroutine_fn GRAPH_RDLOCK
quorum_co_pwritev(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,BdrvRequestFlags flags)7297b1fb72eSKevin Wolf quorum_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
7307b1fb72eSKevin Wolf QEMUIOVector *qiov, BdrvRequestFlags flags)
73113e7956eSBenoît Canet {
73213e7956eSBenoît Canet BDRVQuorumState *s = bs->opaque;
7331b1a920bSMax Reitz QuorumAIOCB *acb = quorum_aio_get(bs, qiov, offset, bytes, flags);
734ce15dc08SKevin Wolf int i, ret;
73513e7956eSBenoît Canet
73613e7956eSBenoît Canet for (i = 0; i < s->num_children; i++) {
737ce15dc08SKevin Wolf Coroutine *co;
738ce15dc08SKevin Wolf QuorumCo data = {
739ce15dc08SKevin Wolf .acb = acb,
740ce15dc08SKevin Wolf .idx = i,
741ce15dc08SKevin Wolf };
742ce15dc08SKevin Wolf
743ce15dc08SKevin Wolf co = qemu_coroutine_create(write_quorum_entry, &data);
744ce15dc08SKevin Wolf qemu_coroutine_enter(co);
74513e7956eSBenoît Canet }
74613e7956eSBenoît Canet
7477cd9b396SKevin Wolf while (acb->count < s->num_children) {
748ce15dc08SKevin Wolf qemu_coroutine_yield();
749ce15dc08SKevin Wolf }
750ce15dc08SKevin Wolf
7517cd9b396SKevin Wolf quorum_has_too_much_io_failed(acb);
7527cd9b396SKevin Wolf
753ce15dc08SKevin Wolf ret = acb->vote_ret;
7540f31977dSKevin Wolf quorum_aio_finalize(acb);
755ce15dc08SKevin Wolf
756ce15dc08SKevin Wolf return ret;
75713e7956eSBenoît Canet }
75813e7956eSBenoît Canet
759abaf8b75SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
quorum_co_pwrite_zeroes(BlockDriverState * bs,int64_t offset,int64_t bytes,BdrvRequestFlags flags)760abaf8b75SKevin Wolf quorum_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes,
7612987ae7dSPaolo Bonzini BdrvRequestFlags flags)
7625cddb2e9SAlberto Garcia {
7635cddb2e9SAlberto Garcia return quorum_co_pwritev(bs, offset, bytes, NULL,
7645cddb2e9SAlberto Garcia flags | BDRV_REQ_ZERO_WRITE);
7655cddb2e9SAlberto Garcia }
7665cddb2e9SAlberto Garcia
7678ab8140aSKevin Wolf static int64_t coroutine_fn GRAPH_RDLOCK
quorum_co_getlength(BlockDriverState * bs)7688ab8140aSKevin Wolf quorum_co_getlength(BlockDriverState *bs)
769d55dee20SBenoît Canet {
770d55dee20SBenoît Canet BDRVQuorumState *s = bs->opaque;
771d55dee20SBenoît Canet int64_t result;
772d55dee20SBenoît Canet int i;
773d55dee20SBenoît Canet
774d55dee20SBenoît Canet /* check that all file have the same length */
775c86422c5SEmanuele Giuseppe Esposito result = bdrv_co_getlength(s->children[0]->bs);
776d55dee20SBenoît Canet if (result < 0) {
777d55dee20SBenoît Canet return result;
778d55dee20SBenoît Canet }
779d55dee20SBenoît Canet for (i = 1; i < s->num_children; i++) {
780c86422c5SEmanuele Giuseppe Esposito int64_t value = bdrv_co_getlength(s->children[i]->bs);
781d55dee20SBenoît Canet if (value < 0) {
782d55dee20SBenoît Canet return value;
783d55dee20SBenoît Canet }
784d55dee20SBenoît Canet if (value != result) {
785d55dee20SBenoît Canet return -EIO;
786d55dee20SBenoît Canet }
787d55dee20SBenoît Canet }
788d55dee20SBenoît Canet
789d55dee20SBenoît Canet return result;
790d55dee20SBenoît Canet }
791d55dee20SBenoît Canet
quorum_co_flush(BlockDriverState * bs)79288095349SEmanuele Giuseppe Esposito static coroutine_fn GRAPH_RDLOCK int quorum_co_flush(BlockDriverState *bs)
7931c508d17SBenoît Canet {
7941c508d17SBenoît Canet BDRVQuorumState *s = bs->opaque;
7951c508d17SBenoît Canet QuorumVoteVersion *winner = NULL;
7961c508d17SBenoît Canet QuorumVotes error_votes;
7971c508d17SBenoît Canet QuorumVoteValue result_value;
7981c508d17SBenoît Canet int i;
7991c508d17SBenoît Canet int result = 0;
800924e8a2bSChanglong Xie int success_count = 0;
8011c508d17SBenoît Canet
8021c508d17SBenoît Canet QLIST_INIT(&error_votes.vote_list);
8031c508d17SBenoît Canet error_votes.compare = quorum_64bits_compare;
8041c508d17SBenoît Canet
8051c508d17SBenoît Canet for (i = 0; i < s->num_children; i++) {
8060bd6e91aSKevin Wolf result = bdrv_co_flush(s->children[i]->bs);
807924e8a2bSChanglong Xie if (result) {
808795be062SAlberto Garcia quorum_report_bad(QUORUM_OP_TYPE_FLUSH, 0, 0,
809924e8a2bSChanglong Xie s->children[i]->bs->node_name, result);
8101c508d17SBenoît Canet result_value.l = result;
8111c508d17SBenoît Canet quorum_count_vote(&error_votes, &result_value, i);
812924e8a2bSChanglong Xie } else {
813924e8a2bSChanglong Xie success_count++;
814924e8a2bSChanglong Xie }
8151c508d17SBenoît Canet }
8161c508d17SBenoît Canet
817924e8a2bSChanglong Xie if (success_count >= s->threshold) {
818924e8a2bSChanglong Xie result = 0;
819924e8a2bSChanglong Xie } else {
8201c508d17SBenoît Canet winner = quorum_get_vote_winner(&error_votes);
8211c508d17SBenoît Canet result = winner->value.l;
822924e8a2bSChanglong Xie }
8231c508d17SBenoît Canet quorum_free_vote_list(&error_votes);
8241c508d17SBenoît Canet
8251c508d17SBenoît Canet return result;
8261c508d17SBenoît Canet }
8271c508d17SBenoît Canet
828533c6e4eSKevin Wolf static bool GRAPH_RDLOCK
quorum_recurse_can_replace(BlockDriverState * bs,BlockDriverState * to_replace)829533c6e4eSKevin Wolf quorum_recurse_can_replace(BlockDriverState *bs, BlockDriverState *to_replace)
830a3ed794bSMax Reitz {
831a3ed794bSMax Reitz BDRVQuorumState *s = bs->opaque;
832a3ed794bSMax Reitz int i;
833a3ed794bSMax Reitz
834a3ed794bSMax Reitz for (i = 0; i < s->num_children; i++) {
835a3ed794bSMax Reitz /*
836a3ed794bSMax Reitz * We have no idea whether our children show the same data as
837a3ed794bSMax Reitz * this node (@bs). It is actually highly likely that
838a3ed794bSMax Reitz * @to_replace does not, because replacing a broken child is
839a3ed794bSMax Reitz * one of the main use cases here.
840a3ed794bSMax Reitz *
841a3ed794bSMax Reitz * We do know that the new BDS will match @bs, so replacing
842a3ed794bSMax Reitz * any of our children by it will be safe. It cannot change
843a3ed794bSMax Reitz * the data this quorum node presents to its parents.
844a3ed794bSMax Reitz *
845a3ed794bSMax Reitz * However, replacing @to_replace by @bs in any of our
846a3ed794bSMax Reitz * children's chains may change visible data somewhere in
847a3ed794bSMax Reitz * there. We therefore cannot recurse down those chains with
848a3ed794bSMax Reitz * bdrv_recurse_can_replace().
849a3ed794bSMax Reitz * (More formally, bdrv_recurse_can_replace() requires that
850a3ed794bSMax Reitz * @to_replace will be replaced by something matching the @bs
851a3ed794bSMax Reitz * passed to it. We cannot guarantee that.)
852a3ed794bSMax Reitz *
853a3ed794bSMax Reitz * Thus, we can only check whether any of our immediate
854a3ed794bSMax Reitz * children matches @to_replace.
855a3ed794bSMax Reitz *
856a3ed794bSMax Reitz * (In the future, we might add a function to recurse down a
857a3ed794bSMax Reitz * chain that checks that nothing there cares about a change
858a3ed794bSMax Reitz * in data from the respective child in question. For
859a3ed794bSMax Reitz * example, most filters do not care when their child's data
860a3ed794bSMax Reitz * suddenly changes, as long as their parents do not care.)
861a3ed794bSMax Reitz */
862a3ed794bSMax Reitz if (s->children[i]->bs == to_replace) {
863a3ed794bSMax Reitz /*
864a3ed794bSMax Reitz * We now have to ensure that there is no other parent
865a3ed794bSMax Reitz * that cares about replacing this child by a node with
866a3ed794bSMax Reitz * potentially different data.
867a3ed794bSMax Reitz * We do so by checking whether there are any other parents
868a3ed794bSMax Reitz * at all, which is stricter than necessary, but also very
869a3ed794bSMax Reitz * simple. (We may decide to implement something more
870a3ed794bSMax Reitz * complex and permissive when there is an actual need for
871a3ed794bSMax Reitz * it.)
872a3ed794bSMax Reitz */
873a3ed794bSMax Reitz return QLIST_FIRST(&to_replace->parents) == s->children[i] &&
874a3ed794bSMax Reitz QLIST_NEXT(s->children[i], next_parent) == NULL;
875a3ed794bSMax Reitz }
876a3ed794bSMax Reitz }
877a3ed794bSMax Reitz
878a3ed794bSMax Reitz return false;
879a3ed794bSMax Reitz }
880a3ed794bSMax Reitz
quorum_valid_threshold(int threshold,int num_children,Error ** errp)881c88a1de5SBenoît Canet static int quorum_valid_threshold(int threshold, int num_children, Error **errp)
882c88a1de5SBenoît Canet {
883c88a1de5SBenoît Canet
884c88a1de5SBenoît Canet if (threshold < 1) {
885c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
8866cc0667dSMarkus Armbruster "vote-threshold", "a value >= 1");
887c88a1de5SBenoît Canet return -ERANGE;
888c88a1de5SBenoît Canet }
889c88a1de5SBenoît Canet
890c88a1de5SBenoît Canet if (threshold > num_children) {
891c88a1de5SBenoît Canet error_setg(errp, "threshold may not exceed children count");
892c88a1de5SBenoît Canet return -ERANGE;
893c88a1de5SBenoît Canet }
894c88a1de5SBenoît Canet
895c88a1de5SBenoît Canet return 0;
896c88a1de5SBenoît Canet }
897c88a1de5SBenoît Canet
898c88a1de5SBenoît Canet static QemuOptsList quorum_runtime_opts = {
899c88a1de5SBenoît Canet .name = "quorum",
900c88a1de5SBenoît Canet .head = QTAILQ_HEAD_INITIALIZER(quorum_runtime_opts.head),
901c88a1de5SBenoît Canet .desc = {
902c88a1de5SBenoît Canet {
903c88a1de5SBenoît Canet .name = QUORUM_OPT_VOTE_THRESHOLD,
904c88a1de5SBenoît Canet .type = QEMU_OPT_NUMBER,
905c88a1de5SBenoît Canet .help = "The number of vote needed for reaching quorum",
906c88a1de5SBenoît Canet },
907c88a1de5SBenoît Canet {
908c88a1de5SBenoît Canet .name = QUORUM_OPT_BLKVERIFY,
909c88a1de5SBenoît Canet .type = QEMU_OPT_BOOL,
910c88a1de5SBenoît Canet .help = "Trigger block verify mode if set",
911c88a1de5SBenoît Canet },
912cf29a570SBenoît Canet {
913cf29a570SBenoît Canet .name = QUORUM_OPT_REWRITE,
914cf29a570SBenoît Canet .type = QEMU_OPT_BOOL,
915cf29a570SBenoît Canet .help = "Rewrite corrupted block on read quorum",
916cf29a570SBenoît Canet },
917a9db86b2SLiu Yuan {
918a9db86b2SLiu Yuan .name = QUORUM_OPT_READ_PATTERN,
919a9db86b2SLiu Yuan .type = QEMU_OPT_STRING,
920a9db86b2SLiu Yuan .help = "Allowed pattern: quorum, fifo. Quorum is default",
921a9db86b2SLiu Yuan },
922c88a1de5SBenoît Canet { /* end of list */ }
923c88a1de5SBenoît Canet },
924c88a1de5SBenoît Canet };
925c88a1de5SBenoît Canet
quorum_refresh_flags(BlockDriverState * bs)9265cddb2e9SAlberto Garcia static void quorum_refresh_flags(BlockDriverState *bs)
9275cddb2e9SAlberto Garcia {
9285cddb2e9SAlberto Garcia BDRVQuorumState *s = bs->opaque;
9295cddb2e9SAlberto Garcia int i;
9305cddb2e9SAlberto Garcia
9315cddb2e9SAlberto Garcia bs->supported_zero_flags =
9325cddb2e9SAlberto Garcia BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK;
9335cddb2e9SAlberto Garcia
9345cddb2e9SAlberto Garcia for (i = 0; i < s->num_children; i++) {
9355cddb2e9SAlberto Garcia bs->supported_zero_flags &= s->children[i]->bs->supported_zero_flags;
9365cddb2e9SAlberto Garcia }
9375cddb2e9SAlberto Garcia
9385cddb2e9SAlberto Garcia bs->supported_zero_flags |= BDRV_REQ_WRITE_UNCHANGED;
9395cddb2e9SAlberto Garcia }
9405cddb2e9SAlberto Garcia
quorum_open(BlockDriverState * bs,QDict * options,int flags,Error ** errp)941c88a1de5SBenoît Canet static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
942c88a1de5SBenoît Canet Error **errp)
943c88a1de5SBenoît Canet {
944c88a1de5SBenoît Canet BDRVQuorumState *s = bs->opaque;
9458df3abfcSFam Zheng QemuOpts *opts = NULL;
9468d5fb199SMarc-André Lureau const char *pattern_str;
947c88a1de5SBenoît Canet bool *opened;
948c88a1de5SBenoît Canet int i;
949c88a1de5SBenoît Canet int ret = 0;
950c88a1de5SBenoît Canet
951c88a1de5SBenoît Canet qdict_flatten(options);
952c88a1de5SBenoît Canet
953ea6828d8SKevin Wolf /* count how many different children are present */
954ea6828d8SKevin Wolf s->num_children = qdict_array_entries(options, "children.");
955ea6828d8SKevin Wolf if (s->num_children < 0) {
956dcfe4805SMarkus Armbruster error_setg(errp, "Option children is not a valid array");
9578a87f3d7SMax Reitz ret = -EINVAL;
9588a87f3d7SMax Reitz goto exit;
9598a87f3d7SMax Reitz }
96098292c61SWen Congyang if (s->num_children < 1) {
961dcfe4805SMarkus Armbruster error_setg(errp, "Number of provided children must be 1 or more");
962c88a1de5SBenoît Canet ret = -EINVAL;
963c88a1de5SBenoît Canet goto exit;
964c88a1de5SBenoît Canet }
965c88a1de5SBenoît Canet
966c88a1de5SBenoît Canet opts = qemu_opts_create(&quorum_runtime_opts, NULL, 0, &error_abort);
967a5f9b9dfSMarkus Armbruster if (!qemu_opts_absorb_qdict(opts, options, errp)) {
968c88a1de5SBenoît Canet ret = -EINVAL;
969c88a1de5SBenoît Canet goto exit;
970c88a1de5SBenoît Canet }
971c88a1de5SBenoît Canet
972c88a1de5SBenoît Canet s->threshold = qemu_opt_get_number(opts, QUORUM_OPT_VOTE_THRESHOLD, 0);
973834cb2adSWen Congyang /* and validate it against s->num_children */
974dcfe4805SMarkus Armbruster ret = quorum_valid_threshold(s->threshold, s->num_children, errp);
975834cb2adSWen Congyang if (ret < 0) {
976834cb2adSWen Congyang goto exit;
977834cb2adSWen Congyang }
978834cb2adSWen Congyang
9798d5fb199SMarc-André Lureau pattern_str = qemu_opt_get(opts, QUORUM_OPT_READ_PATTERN);
9808d5fb199SMarc-André Lureau if (!pattern_str) {
9818d5fb199SMarc-André Lureau ret = QUORUM_READ_PATTERN_QUORUM;
9828d5fb199SMarc-André Lureau } else {
983f7abe0ecSMarc-André Lureau ret = qapi_enum_parse(&QuorumReadPattern_lookup, pattern_str,
9848d5fb199SMarc-André Lureau -EINVAL, NULL);
9858d5fb199SMarc-André Lureau }
986a9db86b2SLiu Yuan if (ret < 0) {
987dcfe4805SMarkus Armbruster error_setg(errp, "Please set read-pattern as fifo or quorum");
988a9db86b2SLiu Yuan goto exit;
989a9db86b2SLiu Yuan }
990a9db86b2SLiu Yuan s->read_pattern = ret;
991c88a1de5SBenoît Canet
992a9db86b2SLiu Yuan if (s->read_pattern == QUORUM_READ_PATTERN_QUORUM) {
99383aedca8SAlberto Garcia s->is_blkverify = qemu_opt_get_bool(opts, QUORUM_OPT_BLKVERIFY, false);
99483aedca8SAlberto Garcia if (s->is_blkverify && (s->num_children != 2 || s->threshold != 2)) {
995dcfe4805SMarkus Armbruster error_setg(errp, "blkverify=on can only be set if there are "
99683aedca8SAlberto Garcia "exactly two files and vote-threshold is 2");
99783aedca8SAlberto Garcia ret = -EINVAL;
99883aedca8SAlberto Garcia goto exit;
999c88a1de5SBenoît Canet }
1000c88a1de5SBenoît Canet
1001a9db86b2SLiu Yuan s->rewrite_corrupted = qemu_opt_get_bool(opts, QUORUM_OPT_REWRITE,
1002a9db86b2SLiu Yuan false);
1003cf29a570SBenoît Canet if (s->rewrite_corrupted && s->is_blkverify) {
1004dcfe4805SMarkus Armbruster error_setg(errp,
1005cf29a570SBenoît Canet "rewrite-corrupted=on cannot be used with blkverify=on");
1006cf29a570SBenoît Canet ret = -EINVAL;
1007cf29a570SBenoît Canet goto exit;
1008cf29a570SBenoît Canet }
1009a9db86b2SLiu Yuan }
1010cf29a570SBenoît Canet
10110bd6e91aSKevin Wolf /* allocate the children array */
10120bd6e91aSKevin Wolf s->children = g_new0(BdrvChild *, s->num_children);
1013c88a1de5SBenoît Canet opened = g_new0(bool, s->num_children);
1014c88a1de5SBenoît Canet
1015ea6828d8SKevin Wolf for (i = 0; i < s->num_children; i++) {
10165eb9a3c7SLukas Straub char indexstr[INDEXSTR_LEN];
10175eb9a3c7SLukas Straub ret = snprintf(indexstr, INDEXSTR_LEN, "children.%d", i);
10185eb9a3c7SLukas Straub assert(ret < INDEXSTR_LEN);
10198a87f3d7SMax Reitz
10200bd6e91aSKevin Wolf s->children[i] = bdrv_open_child(NULL, options, indexstr, bs,
102136ee58d1SMax Reitz &child_of_bds, BDRV_CHILD_DATA, false,
1022bc520249SVladimir Sementsov-Ogievskiy errp);
1023bc520249SVladimir Sementsov-Ogievskiy if (!s->children[i]) {
10240bd6e91aSKevin Wolf ret = -EINVAL;
1025c88a1de5SBenoît Canet goto close_exit;
1026c88a1de5SBenoît Canet }
1027ea6828d8SKevin Wolf
1028c88a1de5SBenoît Canet opened[i] = true;
1029c88a1de5SBenoît Canet }
103098292c61SWen Congyang s->next_child_index = s->num_children;
1031c88a1de5SBenoît Canet
10321b1a920bSMax Reitz bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED;
10335cddb2e9SAlberto Garcia quorum_refresh_flags(bs);
10341b1a920bSMax Reitz
1035c88a1de5SBenoît Canet g_free(opened);
1036c88a1de5SBenoît Canet goto exit;
1037c88a1de5SBenoît Canet
1038c88a1de5SBenoît Canet close_exit:
1039c88a1de5SBenoît Canet /* cleanup on error */
10406bc30f19SStefan Hajnoczi bdrv_graph_wrlock();
1041c88a1de5SBenoît Canet for (i = 0; i < s->num_children; i++) {
1042c88a1de5SBenoît Canet if (!opened[i]) {
1043c88a1de5SBenoît Canet continue;
1044c88a1de5SBenoît Canet }
10450bd6e91aSKevin Wolf bdrv_unref_child(bs, s->children[i]);
1046c88a1de5SBenoît Canet }
10476bc30f19SStefan Hajnoczi bdrv_graph_wrunlock();
10480bd6e91aSKevin Wolf g_free(s->children);
1049c88a1de5SBenoît Canet g_free(opened);
1050c88a1de5SBenoît Canet exit:
10518df3abfcSFam Zheng qemu_opts_del(opts);
1052c88a1de5SBenoît Canet return ret;
1053c88a1de5SBenoît Canet }
1054c88a1de5SBenoît Canet
quorum_close(BlockDriverState * bs)1055c88a1de5SBenoît Canet static void quorum_close(BlockDriverState *bs)
1056c88a1de5SBenoît Canet {
1057c88a1de5SBenoît Canet BDRVQuorumState *s = bs->opaque;
1058c88a1de5SBenoît Canet int i;
1059c88a1de5SBenoît Canet
10606bc30f19SStefan Hajnoczi bdrv_graph_wrlock();
1061c88a1de5SBenoît Canet for (i = 0; i < s->num_children; i++) {
10620bd6e91aSKevin Wolf bdrv_unref_child(bs, s->children[i]);
1063c88a1de5SBenoît Canet }
10646bc30f19SStefan Hajnoczi bdrv_graph_wrunlock();
1065c88a1de5SBenoît Canet
10660bd6e91aSKevin Wolf g_free(s->children);
1067c88a1de5SBenoît Canet }
1068c88a1de5SBenoît Canet
10699def6082SKevin Wolf static void GRAPH_WRLOCK
quorum_add_child(BlockDriverState * bs,BlockDriverState * child_bs,Error ** errp)10709def6082SKevin Wolf quorum_add_child(BlockDriverState *bs, BlockDriverState *child_bs, Error **errp)
107198292c61SWen Congyang {
107298292c61SWen Congyang BDRVQuorumState *s = bs->opaque;
107398292c61SWen Congyang BdrvChild *child;
10745eb9a3c7SLukas Straub char indexstr[INDEXSTR_LEN];
107598292c61SWen Congyang int ret;
107698292c61SWen Congyang
1077808b27d4SAlberto Garcia if (s->is_blkverify) {
1078808b27d4SAlberto Garcia error_setg(errp, "Cannot add a child to a quorum in blkverify mode");
1079808b27d4SAlberto Garcia return;
1080808b27d4SAlberto Garcia }
1081808b27d4SAlberto Garcia
108298292c61SWen Congyang assert(s->num_children <= INT_MAX / sizeof(BdrvChild *));
108398292c61SWen Congyang if (s->num_children == INT_MAX / sizeof(BdrvChild *) ||
108498292c61SWen Congyang s->next_child_index == UINT_MAX) {
108598292c61SWen Congyang error_setg(errp, "Too many children");
108698292c61SWen Congyang return;
108798292c61SWen Congyang }
108898292c61SWen Congyang
10895eb9a3c7SLukas Straub ret = snprintf(indexstr, INDEXSTR_LEN, "children.%u", s->next_child_index);
10905eb9a3c7SLukas Straub if (ret < 0 || ret >= INDEXSTR_LEN) {
109198292c61SWen Congyang error_setg(errp, "cannot generate child name");
109298292c61SWen Congyang return;
109398292c61SWen Congyang }
109498292c61SWen Congyang s->next_child_index++;
109598292c61SWen Congyang
109698292c61SWen Congyang /* We can safely add the child now */
109798292c61SWen Congyang bdrv_ref(child_bs);
10988b2ff529SKevin Wolf
109936ee58d1SMax Reitz child = bdrv_attach_child(bs, child_bs, indexstr, &child_of_bds,
110036ee58d1SMax Reitz BDRV_CHILD_DATA, errp);
11018b2ff529SKevin Wolf if (child == NULL) {
11028b2ff529SKevin Wolf s->next_child_index--;
11039def6082SKevin Wolf return;
11048b2ff529SKevin Wolf }
110598292c61SWen Congyang s->children = g_renew(BdrvChild *, s->children, s->num_children + 1);
110698292c61SWen Congyang s->children[s->num_children++] = child;
11075cddb2e9SAlberto Garcia quorum_refresh_flags(bs);
110898292c61SWen Congyang }
110998292c61SWen Congyang
11109def6082SKevin Wolf static void GRAPH_WRLOCK
quorum_del_child(BlockDriverState * bs,BdrvChild * child,Error ** errp)11119def6082SKevin Wolf quorum_del_child(BlockDriverState *bs, BdrvChild *child, Error **errp)
111298292c61SWen Congyang {
111398292c61SWen Congyang BDRVQuorumState *s = bs->opaque;
11145eb9a3c7SLukas Straub char indexstr[INDEXSTR_LEN];
111598292c61SWen Congyang int i;
111698292c61SWen Congyang
111798292c61SWen Congyang for (i = 0; i < s->num_children; i++) {
111898292c61SWen Congyang if (s->children[i] == child) {
111998292c61SWen Congyang break;
112098292c61SWen Congyang }
112198292c61SWen Congyang }
112298292c61SWen Congyang
112398292c61SWen Congyang /* we have checked it in bdrv_del_child() */
112498292c61SWen Congyang assert(i < s->num_children);
112598292c61SWen Congyang
112698292c61SWen Congyang if (s->num_children <= s->threshold) {
112798292c61SWen Congyang error_setg(errp,
112898292c61SWen Congyang "The number of children cannot be lower than the vote threshold %d",
112998292c61SWen Congyang s->threshold);
113098292c61SWen Congyang return;
113198292c61SWen Congyang }
113298292c61SWen Congyang
1133808b27d4SAlberto Garcia /* We know now that num_children > threshold, so blkverify must be false */
1134808b27d4SAlberto Garcia assert(!s->is_blkverify);
1135808b27d4SAlberto Garcia
11365eb9a3c7SLukas Straub snprintf(indexstr, INDEXSTR_LEN, "children.%u", s->next_child_index - 1);
11375eb9a3c7SLukas Straub if (!strncmp(child->name, indexstr, INDEXSTR_LEN)) {
11385eb9a3c7SLukas Straub s->next_child_index--;
11395eb9a3c7SLukas Straub }
11405eb9a3c7SLukas Straub
114198292c61SWen Congyang /* We can safely remove this child now */
114298292c61SWen Congyang memmove(&s->children[i], &s->children[i + 1],
114398292c61SWen Congyang (s->num_children - i - 1) * sizeof(BdrvChild *));
114498292c61SWen Congyang s->children = g_renew(BdrvChild *, s->children, --s->num_children);
11459def6082SKevin Wolf
114698292c61SWen Congyang bdrv_unref_child(bs, child);
114798292c61SWen Congyang
11485cddb2e9SAlberto Garcia quorum_refresh_flags(bs);
114998292c61SWen Congyang }
115098292c61SWen Congyang
quorum_gather_child_options(BlockDriverState * bs,QDict * target,bool backing_overridden)1151abc521a9SMax Reitz static void quorum_gather_child_options(BlockDriverState *bs, QDict *target,
1152abc521a9SMax Reitz bool backing_overridden)
1153abc521a9SMax Reitz {
1154abc521a9SMax Reitz BDRVQuorumState *s = bs->opaque;
1155abc521a9SMax Reitz QList *children_list;
1156abc521a9SMax Reitz int i;
1157abc521a9SMax Reitz
1158abc521a9SMax Reitz /*
1159abc521a9SMax Reitz * The generic implementation for gathering child options in
1160abc521a9SMax Reitz * bdrv_refresh_filename() would use the names of the children
1161abc521a9SMax Reitz * as specified for bdrv_open_child() or bdrv_attach_child(),
1162abc521a9SMax Reitz * which is "children.%u" with %u being a value
1163abc521a9SMax Reitz * (s->next_child_index) that is incremented each time a new child
1164abc521a9SMax Reitz * is added (and never decremented). Since children can be
1165abc521a9SMax Reitz * deleted at runtime, there may be gaps in that enumeration.
1166abc521a9SMax Reitz * When creating a new quorum BDS and specifying the children for
1167abc521a9SMax Reitz * it through runtime options, the enumeration used there may not
1168abc521a9SMax Reitz * have any gaps, though.
1169abc521a9SMax Reitz *
1170abc521a9SMax Reitz * Therefore, we have to create a new gap-less enumeration here
1171abc521a9SMax Reitz * (which we can achieve by simply putting all of the children's
1172abc521a9SMax Reitz * full_open_options into a QList).
1173abc521a9SMax Reitz *
1174abc521a9SMax Reitz * XXX: Note that there are issues with the current child option
1175abc521a9SMax Reitz * structure quorum uses (such as the fact that children do
1176abc521a9SMax Reitz * not really have unique permanent names). Therefore, this
1177abc521a9SMax Reitz * is going to have to change in the future and ideally we
1178abc521a9SMax Reitz * want quorum to be covered by the generic implementation.
1179abc521a9SMax Reitz */
1180abc521a9SMax Reitz
1181abc521a9SMax Reitz children_list = qlist_new();
1182abc521a9SMax Reitz qdict_put(target, "children", children_list);
1183abc521a9SMax Reitz
1184abc521a9SMax Reitz for (i = 0; i < s->num_children; i++) {
1185abc521a9SMax Reitz qlist_append(children_list,
1186abc521a9SMax Reitz qobject_ref(s->children[i]->bs->full_open_options));
1187abc521a9SMax Reitz }
1188abc521a9SMax Reitz }
1189abc521a9SMax Reitz
quorum_dirname(BlockDriverState * bs,Error ** errp)1190f3037bd2SMax Reitz static char *quorum_dirname(BlockDriverState *bs, Error **errp)
1191f3037bd2SMax Reitz {
1192f3037bd2SMax Reitz /* In general, there are multiple BDSs with different dirnames below this
1193f3037bd2SMax Reitz * one; so there is no unique dirname we could return (unless all are equal
1194f3037bd2SMax Reitz * by chance, or there is only one). Therefore, to be consistent, just
1195f3037bd2SMax Reitz * always return NULL. */
1196f3037bd2SMax Reitz error_setg(errp, "Cannot generate a base directory for quorum nodes");
1197f3037bd2SMax Reitz return NULL;
1198f3037bd2SMax Reitz }
1199f3037bd2SMax Reitz
quorum_child_perm(BlockDriverState * bs,BdrvChild * c,BdrvChildRole role,BlockReopenQueue * reopen_queue,uint64_t perm,uint64_t shared,uint64_t * nperm,uint64_t * nshared)120037a3791bSMax Reitz static void quorum_child_perm(BlockDriverState *bs, BdrvChild *c,
1201bf8e925eSMax Reitz BdrvChildRole role,
120237a3791bSMax Reitz BlockReopenQueue *reopen_queue,
120337a3791bSMax Reitz uint64_t perm, uint64_t shared,
120437a3791bSMax Reitz uint64_t *nperm, uint64_t *nshared)
120537a3791bSMax Reitz {
12069ca5b0e8SMax Reitz BDRVQuorumState *s = bs->opaque;
12079ca5b0e8SMax Reitz
120837a3791bSMax Reitz *nperm = perm & DEFAULT_PERM_PASSTHROUGH;
12099ca5b0e8SMax Reitz if (s->rewrite_corrupted) {
12109ca5b0e8SMax Reitz *nperm |= BLK_PERM_WRITE;
12119ca5b0e8SMax Reitz }
121237a3791bSMax Reitz
121337a3791bSMax Reitz /*
121437a3791bSMax Reitz * We cannot share RESIZE or WRITE, as this would make the
121537a3791bSMax Reitz * children differ from each other.
121637a3791bSMax Reitz */
121737a3791bSMax Reitz *nshared = (shared & (BLK_PERM_CONSISTENT_READ |
121837a3791bSMax Reitz BLK_PERM_WRITE_UNCHANGED))
121937a3791bSMax Reitz | DEFAULT_PERM_UNCHANGED;
122037a3791bSMax Reitz }
122137a3791bSMax Reitz
1222ef9bba14SAlberto Garcia /*
1223ef9bba14SAlberto Garcia * Each one of the children can report different status flags even
1224ef9bba14SAlberto Garcia * when they contain the same data, so what this function does is
1225ef9bba14SAlberto Garcia * return BDRV_BLOCK_ZERO if *all* children agree that a certain
1226ef9bba14SAlberto Garcia * region contains zeroes, and BDRV_BLOCK_DATA otherwise.
1227ef9bba14SAlberto Garcia */
12287ff9579eSKevin Wolf static int coroutine_fn GRAPH_RDLOCK
quorum_co_block_status(BlockDriverState * bs,bool want_zero,int64_t offset,int64_t count,int64_t * pnum,int64_t * map,BlockDriverState ** file)12297ff9579eSKevin Wolf quorum_co_block_status(BlockDriverState *bs, bool want_zero,
1230ef9bba14SAlberto Garcia int64_t offset, int64_t count,
12317ff9579eSKevin Wolf int64_t *pnum, int64_t *map, BlockDriverState **file)
1232ef9bba14SAlberto Garcia {
1233ef9bba14SAlberto Garcia BDRVQuorumState *s = bs->opaque;
1234ef9bba14SAlberto Garcia int i, ret;
1235ef9bba14SAlberto Garcia int64_t pnum_zero = count;
1236ef9bba14SAlberto Garcia int64_t pnum_data = 0;
1237ef9bba14SAlberto Garcia
1238ef9bba14SAlberto Garcia for (i = 0; i < s->num_children; i++) {
1239ef9bba14SAlberto Garcia int64_t bytes;
1240ef9bba14SAlberto Garcia ret = bdrv_co_common_block_status_above(s->children[i]->bs, NULL, false,
1241ef9bba14SAlberto Garcia want_zero, offset, count,
1242ef9bba14SAlberto Garcia &bytes, NULL, NULL, NULL);
1243ef9bba14SAlberto Garcia if (ret < 0) {
1244ef9bba14SAlberto Garcia quorum_report_bad(QUORUM_OP_TYPE_READ, offset, count,
1245ef9bba14SAlberto Garcia s->children[i]->bs->node_name, ret);
1246ef9bba14SAlberto Garcia pnum_data = count;
1247ef9bba14SAlberto Garcia break;
1248ef9bba14SAlberto Garcia }
1249ef9bba14SAlberto Garcia /*
1250ef9bba14SAlberto Garcia * Even if all children agree about whether there are zeroes
1251ef9bba14SAlberto Garcia * or not at @offset they might disagree on the size, so use
1252ef9bba14SAlberto Garcia * the smallest when reporting BDRV_BLOCK_ZERO and the largest
1253ef9bba14SAlberto Garcia * when reporting BDRV_BLOCK_DATA.
1254ef9bba14SAlberto Garcia */
1255ef9bba14SAlberto Garcia if (ret & BDRV_BLOCK_ZERO) {
1256ef9bba14SAlberto Garcia pnum_zero = MIN(pnum_zero, bytes);
1257ef9bba14SAlberto Garcia } else {
1258ef9bba14SAlberto Garcia pnum_data = MAX(pnum_data, bytes);
1259ef9bba14SAlberto Garcia }
1260ef9bba14SAlberto Garcia }
1261ef9bba14SAlberto Garcia
1262ef9bba14SAlberto Garcia if (pnum_data) {
1263ef9bba14SAlberto Garcia *pnum = pnum_data;
1264ef9bba14SAlberto Garcia return BDRV_BLOCK_DATA;
1265ef9bba14SAlberto Garcia } else {
1266ef9bba14SAlberto Garcia *pnum = pnum_zero;
1267ef9bba14SAlberto Garcia return BDRV_BLOCK_ZERO;
1268ef9bba14SAlberto Garcia }
1269ef9bba14SAlberto Garcia }
1270ef9bba14SAlberto Garcia
12712654267cSMax Reitz static const char *const quorum_strong_runtime_opts[] = {
12722654267cSMax Reitz QUORUM_OPT_VOTE_THRESHOLD,
12732654267cSMax Reitz QUORUM_OPT_BLKVERIFY,
12742654267cSMax Reitz QUORUM_OPT_REWRITE,
12752654267cSMax Reitz QUORUM_OPT_READ_PATTERN,
12762654267cSMax Reitz
12772654267cSMax Reitz NULL
12782654267cSMax Reitz };
12792654267cSMax Reitz
1280cadebd7aSBenoît Canet static BlockDriver bdrv_quorum = {
1281cadebd7aSBenoît Canet .format_name = "quorum",
1282cadebd7aSBenoît Canet
1283cadebd7aSBenoît Canet .instance_size = sizeof(BDRVQuorumState),
128413e7956eSBenoît Canet
128565d2c3e2SFabiano Rosas .bdrv_open = quorum_open,
1286c88a1de5SBenoît Canet .bdrv_close = quorum_close,
1287abc521a9SMax Reitz .bdrv_gather_child_options = quorum_gather_child_options,
1288f3037bd2SMax Reitz .bdrv_dirname = quorum_dirname,
1289ef9bba14SAlberto Garcia .bdrv_co_block_status = quorum_co_block_status,
1290c88a1de5SBenoît Canet
12915529b02dSLukas Straub .bdrv_co_flush = quorum_co_flush,
12921c508d17SBenoît Canet
1293c86422c5SEmanuele Giuseppe Esposito .bdrv_co_getlength = quorum_co_getlength,
1294d55dee20SBenoît Canet
12956847da38SKevin Wolf .bdrv_co_preadv = quorum_co_preadv,
12966847da38SKevin Wolf .bdrv_co_pwritev = quorum_co_pwritev,
12975cddb2e9SAlberto Garcia .bdrv_co_pwrite_zeroes = quorum_co_pwrite_zeroes,
129898a7a38fSBenoît Canet
129998292c61SWen Congyang .bdrv_add_child = quorum_add_child,
130098292c61SWen Congyang .bdrv_del_child = quorum_del_child,
130198292c61SWen Congyang
130237a3791bSMax Reitz .bdrv_child_perm = quorum_child_perm,
1303d7010dfbSKevin Wolf
1304a3ed794bSMax Reitz .bdrv_recurse_can_replace = quorum_recurse_can_replace,
13052654267cSMax Reitz
13062654267cSMax Reitz .strong_runtime_opts = quorum_strong_runtime_opts,
1307cadebd7aSBenoît Canet };
1308cadebd7aSBenoît Canet
bdrv_quorum_init(void)1309cadebd7aSBenoît Canet static void bdrv_quorum_init(void)
1310cadebd7aSBenoît Canet {
1311*ef834aa2SMarkus Armbruster if (!qcrypto_hash_supports(QCRYPTO_HASH_ALGO_SHA256)) {
1312e94867edSSascha Silbe /* SHA256 hash support is required for quorum device */
1313e94867edSSascha Silbe return;
1314e94867edSSascha Silbe }
1315cadebd7aSBenoît Canet bdrv_register(&bdrv_quorum);
1316cadebd7aSBenoît Canet }
1317cadebd7aSBenoît Canet
1318cadebd7aSBenoît Canet block_init(bdrv_quorum_init);
1319