blob: f33f30d36b842b6e51eb7c59b9f2d040ef88bc2c [file] [log] [blame]
Benoît Canet27cec152014-02-21 22:21:10 +01001/*
2 * Quorum Block filter
3 *
4 * Copyright (C) 2012-2014 Nodalink, EURL.
5 *
6 * Author:
7 * Benoît Canet <benoit.canet@irqsave.net>
8 *
9 * Based on the design and code of blkverify.c (Copyright (C) 2010 IBM, Corp)
10 * and blkmirror.c (Copyright (C) 2011 Red Hat, Inc).
11 *
12 * This work is licensed under the terms of the GNU GPL, version 2 or later.
13 * See the COPYING file in the top-level directory.
14 */
15
Peter Maydell80c71a22016-01-18 18:01:42 +000016#include "qemu/osdep.h"
Wen Congyang98292c62016-05-10 15:36:38 +080017#include "qemu/cutils.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020018#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010019#include "qemu/option.h"
Peter Maydell5df022c2022-02-26 18:07:23 +000020#include "qemu/memalign.h"
Benoît Canet27cec152014-02-21 22:21:10 +010021#include "block/block_int.h"
Alberto Garciaef9bba12020-11-13 17:52:31 +010022#include "block/coroutines.h"
Max Reitz609f45e2018-06-14 21:14:28 +020023#include "block/qdict.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010024#include "qapi/error.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010025#include "qapi/qapi-events-block.h"
Max Reitzfafcfe22014-07-18 20:25:00 +020026#include "qapi/qmp/qdict.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010027#include "qapi/qmp/qerror.h"
Max Reitzfafcfe22014-07-18 20:25:00 +020028#include "qapi/qmp/qlist.h"
29#include "qapi/qmp/qstring.h"
Daniel P. Berrange488981a42015-07-01 18:10:35 +010030#include "crypto/hash.h"
Benoît Canet95c6bff2014-02-21 22:21:15 +010031
32#define HASH_LENGTH 32
33
Lukas Straub5eb9a3c2020-08-04 12:46:42 +020034#define INDEXSTR_LEN 32
35
Benoît Canetc88a1de2014-02-21 22:21:20 +010036#define QUORUM_OPT_VOTE_THRESHOLD "vote-threshold"
37#define QUORUM_OPT_BLKVERIFY "blkverify"
Benoît Canetcf29a572014-06-11 15:24:10 +020038#define QUORUM_OPT_REWRITE "rewrite-corrupted"
Liu Yuana9db86b2014-08-18 17:41:05 +080039#define QUORUM_OPT_READ_PATTERN "read-pattern"
Benoît Canetc88a1de2014-02-21 22:21:20 +010040
Benoît Canet95c6bff2014-02-21 22:21:15 +010041/* This union holds a vote hash value */
42typedef union QuorumVoteValue {
Daniel P. Berrange488981a42015-07-01 18:10:35 +010043 uint8_t h[HASH_LENGTH]; /* SHA-256 hash */
Benoît Canet95c6bff2014-02-21 22:21:15 +010044 int64_t l; /* simpler 64 bits hash */
45} QuorumVoteValue;
46
47/* A vote item */
48typedef struct QuorumVoteItem {
49 int index;
50 QLIST_ENTRY(QuorumVoteItem) next;
51} QuorumVoteItem;
52
53/* this structure is a vote version. A version is the set of votes sharing the
54 * same vote value.
55 * The set of votes will be tracked with the items field and its cardinality is
56 * vote_count.
57 */
58typedef struct QuorumVoteVersion {
59 QuorumVoteValue value;
60 int index;
61 int vote_count;
62 QLIST_HEAD(, QuorumVoteItem) items;
63 QLIST_ENTRY(QuorumVoteVersion) next;
64} QuorumVoteVersion;
65
66/* this structure holds a group of vote versions together */
67typedef struct QuorumVotes {
68 QLIST_HEAD(, QuorumVoteVersion) vote_list;
69 bool (*compare)(QuorumVoteValue *a, QuorumVoteValue *b);
70} QuorumVotes;
Benoît Canet27cec152014-02-21 22:21:10 +010071
Benoît Canetcadebd72014-02-21 22:21:11 +010072/* the following structure holds the state of one quorum instance */
73typedef struct BDRVQuorumState {
Kevin Wolf0bd6e912015-06-16 11:29:22 +020074 BdrvChild **children; /* children BlockDriverStates */
Benoît Canetcadebd72014-02-21 22:21:11 +010075 int num_children; /* children count */
Wen Congyang98292c62016-05-10 15:36:38 +080076 unsigned next_child_index; /* the index of the next child that should
77 * be added
78 */
Benoît Canetcadebd72014-02-21 22:21:11 +010079 int threshold; /* if less than threshold children reads gave the
80 * same result a quorum error occurs.
81 */
82 bool is_blkverify; /* true if the driver is in blkverify mode
83 * Writes are mirrored on two children devices.
84 * On reads the two children devices' contents are
85 * compared and if a difference is spotted its
86 * location is printed and the code aborts.
87 * It is useful to debug other block drivers by
88 * comparing them with a reference one.
89 */
Benoît Canetcf29a572014-06-11 15:24:10 +020090 bool rewrite_corrupted;/* true if the driver must rewrite-on-read corrupted
91 * block if Quorum is reached.
92 */
Liu Yuana9db86b2014-08-18 17:41:05 +080093
94 QuorumReadPattern read_pattern;
Benoît Canetcadebd72014-02-21 22:21:11 +010095} BDRVQuorumState;
96
Benoît Canet27cec152014-02-21 22:21:10 +010097typedef struct QuorumAIOCB QuorumAIOCB;
98
99/* Quorum will create one instance of the following structure per operation it
100 * performs on its children.
101 * So for each read/write operation coming from the upper layer there will be
102 * $children_count QuorumChildRequest.
103 */
104typedef struct QuorumChildRequest {
Kevin Wolfce15dc02016-11-08 11:10:14 +0100105 BlockDriverState *bs;
Benoît Canet27cec152014-02-21 22:21:10 +0100106 QEMUIOVector qiov;
107 uint8_t *buf;
108 int ret;
109 QuorumAIOCB *parent;
110} QuorumChildRequest;
111
112/* Quorum will use the following structure to track progress of each read/write
113 * operation received by the upper layer.
114 * This structure hold pointers to the QuorumChildRequest structures instances
115 * used to do operations on each children and track overall progress.
116 */
117struct QuorumAIOCB {
Kevin Wolfce15dc02016-11-08 11:10:14 +0100118 BlockDriverState *bs;
119 Coroutine *co;
Benoît Canet27cec152014-02-21 22:21:10 +0100120
121 /* Request metadata */
Kevin Wolf6847da32016-11-10 17:22:07 +0100122 uint64_t offset;
123 uint64_t bytes;
Max Reitz1b1a9202018-04-21 15:29:25 +0200124 int flags;
Benoît Canet27cec152014-02-21 22:21:10 +0100125
126 QEMUIOVector *qiov; /* calling IOV */
127
128 QuorumChildRequest *qcrs; /* individual child requests */
129 int count; /* number of completed AIOCB */
130 int success_count; /* number of successfully completed AIOCB */
131
Benoît Canetcf29a572014-06-11 15:24:10 +0200132 int rewrite_count; /* number of replica to rewrite: count down to
133 * zero once writes are fired
134 */
135
Benoît Canet95c6bff2014-02-21 22:21:15 +0100136 QuorumVotes votes;
137
Benoît Canet27cec152014-02-21 22:21:10 +0100138 bool is_read;
139 int vote_ret;
Paolo Bonzini86ec2522016-10-05 18:35:26 +0200140 int children_read; /* how many children have been read from */
Benoît Canet27cec152014-02-21 22:21:10 +0100141};
Benoît Canetcadebd72014-02-21 22:21:11 +0100142
Kevin Wolfce15dc02016-11-08 11:10:14 +0100143typedef struct QuorumCo {
144 QuorumAIOCB *acb;
145 int idx;
146} QuorumCo;
147
Benoît Canet13e79562014-02-21 22:21:12 +0100148static void quorum_aio_finalize(QuorumAIOCB *acb)
149{
Benoît Canet13e79562014-02-21 22:21:12 +0100150 g_free(acb->qcrs);
Kevin Wolf0f319772016-11-10 14:24:27 +0100151 g_free(acb);
Benoît Canet13e79562014-02-21 22:21:12 +0100152}
153
Benoît Canet95c6bff2014-02-21 22:21:15 +0100154static bool quorum_sha256_compare(QuorumVoteValue *a, QuorumVoteValue *b)
155{
156 return !memcmp(a->h, b->h, HASH_LENGTH);
157}
158
159static bool quorum_64bits_compare(QuorumVoteValue *a, QuorumVoteValue *b)
160{
161 return a->l == b->l;
162}
163
Kevin Wolf10c85512016-11-07 18:00:29 +0100164static QuorumAIOCB *quorum_aio_get(BlockDriverState *bs,
Benoît Canet13e79562014-02-21 22:21:12 +0100165 QEMUIOVector *qiov,
Kevin Wolf6847da32016-11-10 17:22:07 +0100166 uint64_t offset,
Max Reitz1b1a9202018-04-21 15:29:25 +0200167 uint64_t bytes,
168 int flags)
Benoît Canet13e79562014-02-21 22:21:12 +0100169{
Kevin Wolf10c85512016-11-07 18:00:29 +0100170 BDRVQuorumState *s = bs->opaque;
Kevin Wolfce15dc02016-11-08 11:10:14 +0100171 QuorumAIOCB *acb = g_new(QuorumAIOCB, 1);
Benoît Canet13e79562014-02-21 22:21:12 +0100172 int i;
173
Kevin Wolf7c37f942016-11-22 12:49:49 +0100174 *acb = (QuorumAIOCB) {
175 .co = qemu_coroutine_self(),
176 .bs = bs,
177 .offset = offset,
178 .bytes = bytes,
Max Reitz1b1a9202018-04-21 15:29:25 +0200179 .flags = flags,
Kevin Wolf7c37f942016-11-22 12:49:49 +0100180 .qiov = qiov,
181 .votes.compare = quorum_sha256_compare,
182 .votes.vote_list = QLIST_HEAD_INITIALIZER(acb.votes.vote_list),
183 };
Benoît Canet13e79562014-02-21 22:21:12 +0100184
Kevin Wolf7c37f942016-11-22 12:49:49 +0100185 acb->qcrs = g_new0(QuorumChildRequest, s->num_children);
Benoît Canet13e79562014-02-21 22:21:12 +0100186 for (i = 0; i < s->num_children; i++) {
187 acb->qcrs[i].buf = NULL;
188 acb->qcrs[i].ret = 0;
189 acb->qcrs[i].parent = acb;
190 }
191
192 return acb;
193}
194
Kevin Wolf6847da32016-11-10 17:22:07 +0100195static void quorum_report_bad(QuorumOpType type, uint64_t offset,
196 uint64_t bytes, char *node_name, int ret)
Benoît Canet95c6bff2014-02-21 22:21:15 +0100197{
Wenchao Xiafe069d92014-06-18 08:43:53 +0200198 const char *msg = NULL;
Kevin Wolf6847da32016-11-10 17:22:07 +0100199 int64_t start_sector = offset / BDRV_SECTOR_SIZE;
200 int64_t end_sector = DIV_ROUND_UP(offset + bytes, BDRV_SECTOR_SIZE);
201
Benoît Canet0c762732014-02-22 18:43:41 +0100202 if (ret < 0) {
Wenchao Xiafe069d92014-06-18 08:43:53 +0200203 msg = strerror(-ret);
Benoît Canet0c762732014-02-22 18:43:41 +0100204 }
Changlong Xie0ae053b2016-02-26 09:39:01 +0800205
Kevin Wolf6847da32016-11-10 17:22:07 +0100206 qapi_event_send_quorum_report_bad(type, !!msg, msg, node_name, start_sector,
Peter Xu3ab72382018-08-15 21:37:37 +0800207 end_sector - start_sector);
Benoît Canet95c6bff2014-02-21 22:21:15 +0100208}
209
210static void quorum_report_failure(QuorumAIOCB *acb)
211{
Kevin Wolfce15dc02016-11-08 11:10:14 +0100212 const char *reference = bdrv_get_device_or_node_name(acb->bs);
Kevin Wolf6847da32016-11-10 17:22:07 +0100213 int64_t start_sector = acb->offset / BDRV_SECTOR_SIZE;
214 int64_t end_sector = DIV_ROUND_UP(acb->offset + acb->bytes,
215 BDRV_SECTOR_SIZE);
216
217 qapi_event_send_quorum_failure(reference, start_sector,
Peter Xu3ab72382018-08-15 21:37:37 +0800218 end_sector - start_sector);
Benoît Canet95c6bff2014-02-21 22:21:15 +0100219}
220
221static int quorum_vote_error(QuorumAIOCB *acb);
222
223static bool quorum_has_too_much_io_failed(QuorumAIOCB *acb)
224{
Kevin Wolfce15dc02016-11-08 11:10:14 +0100225 BDRVQuorumState *s = acb->bs->opaque;
Benoît Canet95c6bff2014-02-21 22:21:15 +0100226
227 if (acb->success_count < s->threshold) {
228 acb->vote_ret = quorum_vote_error(acb);
229 quorum_report_failure(acb);
230 return true;
231 }
232
233 return false;
234}
235
Kevin Wolfce15dc02016-11-08 11:10:14 +0100236static int read_fifo_child(QuorumAIOCB *acb);
Liu Yuana9db86b2014-08-18 17:41:05 +0800237
238static void quorum_copy_qiov(QEMUIOVector *dest, QEMUIOVector *source)
239{
240 int i;
241 assert(dest->niov == source->niov);
242 assert(dest->size == source->size);
243 for (i = 0; i < source->niov; i++) {
244 assert(dest->iov[i].iov_len == source->iov[i].iov_len);
245 memcpy(dest->iov[i].iov_base,
246 source->iov[i].iov_base,
247 source->iov[i].iov_len);
248 }
249}
250
Paolo Bonzini1ba7e152016-10-05 18:35:27 +0200251static void quorum_report_bad_acb(QuorumChildRequest *sacb, int ret)
252{
253 QuorumAIOCB *acb = sacb->parent;
254 QuorumOpType type = acb->is_read ? QUORUM_OP_TYPE_READ : QUORUM_OP_TYPE_WRITE;
Kevin Wolf6847da32016-11-10 17:22:07 +0100255 quorum_report_bad(type, acb->offset, acb->bytes, sacb->bs->node_name, ret);
Paolo Bonzini1ba7e152016-10-05 18:35:27 +0200256}
257
Benoît Canet95c6bff2014-02-21 22:21:15 +0100258static void quorum_report_bad_versions(BDRVQuorumState *s,
259 QuorumAIOCB *acb,
260 QuorumVoteValue *value)
261{
262 QuorumVoteVersion *version;
263 QuorumVoteItem *item;
264
265 QLIST_FOREACH(version, &acb->votes.vote_list, next) {
266 if (acb->votes.compare(&version->value, value)) {
267 continue;
268 }
269 QLIST_FOREACH(item, &version->items, next) {
Kevin Wolf6847da32016-11-10 17:22:07 +0100270 quorum_report_bad(QUORUM_OP_TYPE_READ, acb->offset, acb->bytes,
Changlong Xie0ae053b2016-02-26 09:39:01 +0800271 s->children[item->index]->bs->node_name, 0);
Benoît Canet95c6bff2014-02-21 22:21:15 +0100272 }
273 }
274}
275
Kevin Wolfdee66e22016-11-10 16:50:16 +0100276static void quorum_rewrite_entry(void *opaque)
277{
278 QuorumCo *co = opaque;
279 QuorumAIOCB *acb = co->acb;
280 BDRVQuorumState *s = acb->bs->opaque;
281
282 /* Ignore any errors, it's just a correction attempt for already
Max Reitz1b1a9202018-04-21 15:29:25 +0200283 * corrupted data.
284 * Mask out BDRV_REQ_WRITE_UNCHANGED because this overwrites the
285 * area with different data from the other children. */
Kevin Wolf6847da32016-11-10 17:22:07 +0100286 bdrv_co_pwritev(s->children[co->idx], acb->offset, acb->bytes,
Max Reitz1b1a9202018-04-21 15:29:25 +0200287 acb->qiov, acb->flags & ~BDRV_REQ_WRITE_UNCHANGED);
Kevin Wolfdee66e22016-11-10 16:50:16 +0100288
289 /* Wake up the caller after the last rewrite */
290 acb->rewrite_count--;
291 if (!acb->rewrite_count) {
292 qemu_coroutine_enter_if_inactive(acb->co);
293 }
294}
295
296static bool quorum_rewrite_bad_versions(QuorumAIOCB *acb,
Benoît Canetcf29a572014-06-11 15:24:10 +0200297 QuorumVoteValue *value)
298{
299 QuorumVoteVersion *version;
300 QuorumVoteItem *item;
301 int count = 0;
302
303 /* first count the number of bad versions: done first to avoid concurrency
304 * issues.
305 */
306 QLIST_FOREACH(version, &acb->votes.vote_list, next) {
307 if (acb->votes.compare(&version->value, value)) {
308 continue;
309 }
310 QLIST_FOREACH(item, &version->items, next) {
311 count++;
312 }
313 }
314
Kevin Wolfdee66e22016-11-10 16:50:16 +0100315 /* quorum_rewrite_entry will count down this to zero */
Benoît Canetcf29a572014-06-11 15:24:10 +0200316 acb->rewrite_count = count;
317
318 /* now fire the correcting rewrites */
319 QLIST_FOREACH(version, &acb->votes.vote_list, next) {
320 if (acb->votes.compare(&version->value, value)) {
321 continue;
322 }
323 QLIST_FOREACH(item, &version->items, next) {
Kevin Wolfdee66e22016-11-10 16:50:16 +0100324 Coroutine *co;
325 QuorumCo data = {
326 .acb = acb,
327 .idx = item->index,
328 };
329
330 co = qemu_coroutine_create(quorum_rewrite_entry, &data);
331 qemu_coroutine_enter(co);
Benoît Canetcf29a572014-06-11 15:24:10 +0200332 }
333 }
334
335 /* return true if any rewrite is done else false */
336 return count;
337}
338
Benoît Canet95c6bff2014-02-21 22:21:15 +0100339static void quorum_count_vote(QuorumVotes *votes,
340 QuorumVoteValue *value,
341 int index)
342{
343 QuorumVoteVersion *v = NULL, *version = NULL;
344 QuorumVoteItem *item;
345
346 /* look if we have something with this hash */
347 QLIST_FOREACH(v, &votes->vote_list, next) {
348 if (votes->compare(&v->value, value)) {
349 version = v;
350 break;
351 }
352 }
353
354 /* It's a version not yet in the list add it */
355 if (!version) {
356 version = g_new0(QuorumVoteVersion, 1);
357 QLIST_INIT(&version->items);
358 memcpy(&version->value, value, sizeof(version->value));
359 version->index = index;
360 version->vote_count = 0;
361 QLIST_INSERT_HEAD(&votes->vote_list, version, next);
362 }
363
364 version->vote_count++;
365
366 item = g_new0(QuorumVoteItem, 1);
367 item->index = index;
368 QLIST_INSERT_HEAD(&version->items, item, next);
369}
370
371static void quorum_free_vote_list(QuorumVotes *votes)
372{
373 QuorumVoteVersion *version, *next_version;
374 QuorumVoteItem *item, *next_item;
375
376 QLIST_FOREACH_SAFE(version, &votes->vote_list, next, next_version) {
377 QLIST_REMOVE(version, next);
378 QLIST_FOREACH_SAFE(item, &version->items, next, next_item) {
379 QLIST_REMOVE(item, next);
380 g_free(item);
381 }
382 g_free(version);
383 }
384}
385
386static int quorum_compute_hash(QuorumAIOCB *acb, int i, QuorumVoteValue *hash)
387{
Benoît Canet95c6bff2014-02-21 22:21:15 +0100388 QEMUIOVector *qiov = &acb->qcrs[i].qiov;
Daniel P. Berrange488981a42015-07-01 18:10:35 +0100389 size_t len = sizeof(hash->h);
390 uint8_t *data = hash->h;
Benoît Canet95c6bff2014-02-21 22:21:15 +0100391
Daniel P. Berrange488981a42015-07-01 18:10:35 +0100392 /* XXX - would be nice if we could pass in the Error **
393 * and propagate that back, but this quorum code is
394 * restricted to just errno values currently */
395 if (qcrypto_hash_bytesv(QCRYPTO_HASH_ALG_SHA256,
396 qiov->iov, qiov->niov,
397 &data, &len,
398 NULL) < 0) {
399 return -EINVAL;
Benoît Canet95c6bff2014-02-21 22:21:15 +0100400 }
401
Daniel P. Berrange488981a42015-07-01 18:10:35 +0100402 return 0;
Benoît Canet95c6bff2014-02-21 22:21:15 +0100403}
404
405static QuorumVoteVersion *quorum_get_vote_winner(QuorumVotes *votes)
406{
407 int max = 0;
408 QuorumVoteVersion *candidate, *winner = NULL;
409
410 QLIST_FOREACH(candidate, &votes->vote_list, next) {
411 if (candidate->vote_count > max) {
412 max = candidate->vote_count;
413 winner = candidate;
414 }
415 }
416
417 return winner;
418}
419
420/* qemu_iovec_compare is handy for blkverify mode because it returns the first
421 * differing byte location. Yet it is handcoded to compare vectors one byte
422 * after another so it does not benefit from the libc SIMD optimizations.
423 * quorum_iovec_compare is written for speed and should be used in the non
424 * blkverify mode of quorum.
425 */
426static bool quorum_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
427{
428 int i;
429 int result;
430
431 assert(a->niov == b->niov);
432 for (i = 0; i < a->niov; i++) {
433 assert(a->iov[i].iov_len == b->iov[i].iov_len);
434 result = memcmp(a->iov[i].iov_base,
435 b->iov[i].iov_base,
436 a->iov[i].iov_len);
437 if (result) {
438 return false;
439 }
440 }
441
442 return true;
443}
444
Alberto Garcia6840e8d2018-10-17 17:33:49 +0300445static bool quorum_compare(QuorumAIOCB *acb, QEMUIOVector *a, QEMUIOVector *b)
Benoît Canet95c6bff2014-02-21 22:21:15 +0100446{
Kevin Wolfce15dc02016-11-08 11:10:14 +0100447 BDRVQuorumState *s = acb->bs->opaque;
Benoît Canet95c6bff2014-02-21 22:21:15 +0100448 ssize_t offset;
449
450 /* This driver will replace blkverify in this particular case */
451 if (s->is_blkverify) {
452 offset = qemu_iovec_compare(a, b);
453 if (offset != -1) {
Alberto Garcia6840e8d2018-10-17 17:33:49 +0300454 fprintf(stderr, "quorum: offset=%" PRIu64 " bytes=%" PRIu64
455 " contents mismatch at offset %" PRIu64 "\n",
456 acb->offset, acb->bytes, acb->offset + offset);
457 exit(1);
Benoît Canet95c6bff2014-02-21 22:21:15 +0100458 }
459 return true;
460 }
461
462 return quorum_iovec_compare(a, b);
463}
464
465/* Do a vote to get the error code */
466static int quorum_vote_error(QuorumAIOCB *acb)
467{
Kevin Wolfce15dc02016-11-08 11:10:14 +0100468 BDRVQuorumState *s = acb->bs->opaque;
Benoît Canet95c6bff2014-02-21 22:21:15 +0100469 QuorumVoteVersion *winner = NULL;
470 QuorumVotes error_votes;
471 QuorumVoteValue result_value;
472 int i, ret = 0;
473 bool error = false;
474
475 QLIST_INIT(&error_votes.vote_list);
476 error_votes.compare = quorum_64bits_compare;
477
478 for (i = 0; i < s->num_children; i++) {
479 ret = acb->qcrs[i].ret;
480 if (ret) {
481 error = true;
482 result_value.l = ret;
483 quorum_count_vote(&error_votes, &result_value, i);
484 }
485 }
486
487 if (error) {
488 winner = quorum_get_vote_winner(&error_votes);
489 ret = winner->value.l;
490 }
491
492 quorum_free_vote_list(&error_votes);
493
494 return ret;
495}
496
Kevin Wolf7cd9b392016-11-10 16:13:15 +0100497static void quorum_vote(QuorumAIOCB *acb)
Benoît Canet95c6bff2014-02-21 22:21:15 +0100498{
499 bool quorum = true;
500 int i, j, ret;
501 QuorumVoteValue hash;
Kevin Wolfce15dc02016-11-08 11:10:14 +0100502 BDRVQuorumState *s = acb->bs->opaque;
Benoît Canet95c6bff2014-02-21 22:21:15 +0100503 QuorumVoteVersion *winner;
504
505 if (quorum_has_too_much_io_failed(acb)) {
Kevin Wolf7cd9b392016-11-10 16:13:15 +0100506 return;
Benoît Canet95c6bff2014-02-21 22:21:15 +0100507 }
508
509 /* get the index of the first successful read */
510 for (i = 0; i < s->num_children; i++) {
511 if (!acb->qcrs[i].ret) {
512 break;
513 }
514 }
515
516 assert(i < s->num_children);
517
518 /* compare this read with all other successful reads stopping at quorum
519 * failure
520 */
521 for (j = i + 1; j < s->num_children; j++) {
522 if (acb->qcrs[j].ret) {
523 continue;
524 }
525 quorum = quorum_compare(acb, &acb->qcrs[i].qiov, &acb->qcrs[j].qiov);
526 if (!quorum) {
527 break;
528 }
529 }
530
531 /* Every successful read agrees */
532 if (quorum) {
533 quorum_copy_qiov(acb->qiov, &acb->qcrs[i].qiov);
Kevin Wolf7cd9b392016-11-10 16:13:15 +0100534 return;
Benoît Canet95c6bff2014-02-21 22:21:15 +0100535 }
536
537 /* compute hashes for each successful read, also store indexes */
538 for (i = 0; i < s->num_children; i++) {
539 if (acb->qcrs[i].ret) {
540 continue;
541 }
542 ret = quorum_compute_hash(acb, i, &hash);
543 /* if ever the hash computation failed */
544 if (ret < 0) {
545 acb->vote_ret = ret;
546 goto free_exit;
547 }
548 quorum_count_vote(&acb->votes, &hash, i);
549 }
550
551 /* vote to select the most represented version */
552 winner = quorum_get_vote_winner(&acb->votes);
553
554 /* if the winner count is smaller than threshold the read fails */
555 if (winner->vote_count < s->threshold) {
556 quorum_report_failure(acb);
557 acb->vote_ret = -EIO;
558 goto free_exit;
559 }
560
561 /* we have a winner: copy it */
562 quorum_copy_qiov(acb->qiov, &acb->qcrs[winner->index].qiov);
563
564 /* some versions are bad print them */
565 quorum_report_bad_versions(s, acb, &winner->value);
566
Benoît Canetcf29a572014-06-11 15:24:10 +0200567 /* corruption correction is enabled */
568 if (s->rewrite_corrupted) {
Kevin Wolfdee66e22016-11-10 16:50:16 +0100569 quorum_rewrite_bad_versions(acb, &winner->value);
Benoît Canetcf29a572014-06-11 15:24:10 +0200570 }
571
Benoît Canet95c6bff2014-02-21 22:21:15 +0100572free_exit:
573 /* free lists */
574 quorum_free_vote_list(&acb->votes);
575}
576
Kevin Wolfce15dc02016-11-08 11:10:14 +0100577static void read_quorum_children_entry(void *opaque)
Liu Yuana9db86b2014-08-18 17:41:05 +0800578{
Kevin Wolfce15dc02016-11-08 11:10:14 +0100579 QuorumCo *co = opaque;
580 QuorumAIOCB *acb = co->acb;
581 BDRVQuorumState *s = acb->bs->opaque;
582 int i = co->idx;
Kevin Wolf7cd9b392016-11-10 16:13:15 +0100583 QuorumChildRequest *sacb = &acb->qcrs[i];
Kevin Wolfce15dc02016-11-08 11:10:14 +0100584
Kevin Wolf7cd9b392016-11-10 16:13:15 +0100585 sacb->bs = s->children[i]->bs;
Kevin Wolf6847da32016-11-10 17:22:07 +0100586 sacb->ret = bdrv_co_preadv(s->children[i], acb->offset, acb->bytes,
Kevin Wolf7cd9b392016-11-10 16:13:15 +0100587 &acb->qcrs[i].qiov, 0);
588
589 if (sacb->ret == 0) {
590 acb->success_count++;
591 } else {
592 quorum_report_bad_acb(sacb, sacb->ret);
593 }
594
595 acb->count++;
596 assert(acb->count <= s->num_children);
597 assert(acb->success_count <= s->num_children);
598
599 /* Wake up the caller after the last read */
600 if (acb->count == s->num_children) {
601 qemu_coroutine_enter_if_inactive(acb->co);
602 }
Kevin Wolfce15dc02016-11-08 11:10:14 +0100603}
604
605static int read_quorum_children(QuorumAIOCB *acb)
606{
607 BDRVQuorumState *s = acb->bs->opaque;
Laurent Vivier4a4ff4c2018-03-23 15:32:02 +0100608 int i;
Liu Yuana9db86b2014-08-18 17:41:05 +0800609
Paolo Bonzini86ec2522016-10-05 18:35:26 +0200610 acb->children_read = s->num_children;
Liu Yuana9db86b2014-08-18 17:41:05 +0800611 for (i = 0; i < s->num_children; i++) {
Kevin Wolf0bd6e912015-06-16 11:29:22 +0200612 acb->qcrs[i].buf = qemu_blockalign(s->children[i]->bs, acb->qiov->size);
Liu Yuana9db86b2014-08-18 17:41:05 +0800613 qemu_iovec_init(&acb->qcrs[i].qiov, acb->qiov->niov);
614 qemu_iovec_clone(&acb->qcrs[i].qiov, acb->qiov, acb->qcrs[i].buf);
615 }
616
617 for (i = 0; i < s->num_children; i++) {
Kevin Wolfce15dc02016-11-08 11:10:14 +0100618 Coroutine *co;
619 QuorumCo data = {
620 .acb = acb,
621 .idx = i,
622 };
623
624 co = qemu_coroutine_create(read_quorum_children_entry, &data);
625 qemu_coroutine_enter(co);
Liu Yuana9db86b2014-08-18 17:41:05 +0800626 }
627
Kevin Wolf7cd9b392016-11-10 16:13:15 +0100628 while (acb->count < s->num_children) {
629 qemu_coroutine_yield();
630 }
631
632 /* Do the vote on read */
633 quorum_vote(acb);
634 for (i = 0; i < s->num_children; i++) {
635 qemu_vfree(acb->qcrs[i].buf);
636 qemu_iovec_destroy(&acb->qcrs[i].qiov);
637 }
638
639 while (acb->rewrite_count) {
Kevin Wolfce15dc02016-11-08 11:10:14 +0100640 qemu_coroutine_yield();
641 }
642
Laurent Vivier4a4ff4c2018-03-23 15:32:02 +0100643 return acb->vote_ret;
Liu Yuana9db86b2014-08-18 17:41:05 +0800644}
645
Kevin Wolfce15dc02016-11-08 11:10:14 +0100646static int read_fifo_child(QuorumAIOCB *acb)
Liu Yuana9db86b2014-08-18 17:41:05 +0800647{
Kevin Wolfce15dc02016-11-08 11:10:14 +0100648 BDRVQuorumState *s = acb->bs->opaque;
Kevin Wolfa7e15902016-11-10 17:40:34 +0100649 int n, ret;
Liu Yuana9db86b2014-08-18 17:41:05 +0800650
Kevin Wolfa7e15902016-11-10 17:40:34 +0100651 /* We try to read the next child in FIFO order if we failed to read */
652 do {
653 n = acb->children_read++;
654 acb->qcrs[n].bs = s->children[n]->bs;
655 ret = bdrv_co_preadv(s->children[n], acb->offset, acb->bytes,
656 acb->qiov, 0);
657 if (ret < 0) {
658 quorum_report_bad_acb(&acb->qcrs[n], ret);
659 }
660 } while (ret < 0 && acb->children_read < s->num_children);
661
662 /* FIXME: rewrite failed children if acb->children_read > 1? */
Liu Yuana9db86b2014-08-18 17:41:05 +0800663
Kevin Wolfce15dc02016-11-08 11:10:14 +0100664 return ret;
Liu Yuana9db86b2014-08-18 17:41:05 +0800665}
666
Vladimir Sementsov-Ogievskiyf7ef38d2021-09-03 13:27:59 +0300667static int quorum_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
668 QEMUIOVector *qiov, BdrvRequestFlags flags)
Benoît Canet7db69822014-02-21 22:21:14 +0100669{
670 BDRVQuorumState *s = bs->opaque;
Max Reitz1b1a9202018-04-21 15:29:25 +0200671 QuorumAIOCB *acb = quorum_aio_get(bs, qiov, offset, bytes, flags);
Kevin Wolfce15dc02016-11-08 11:10:14 +0100672 int ret;
673
Benoît Canet7db69822014-02-21 22:21:14 +0100674 acb->is_read = true;
Paolo Bonzini86ec2522016-10-05 18:35:26 +0200675 acb->children_read = 0;
Benoît Canet7db69822014-02-21 22:21:14 +0100676
Liu Yuana9db86b2014-08-18 17:41:05 +0800677 if (s->read_pattern == QUORUM_READ_PATTERN_QUORUM) {
Kevin Wolfce15dc02016-11-08 11:10:14 +0100678 ret = read_quorum_children(acb);
679 } else {
680 ret = read_fifo_child(acb);
Benoît Canet7db69822014-02-21 22:21:14 +0100681 }
Kevin Wolf0f319772016-11-10 14:24:27 +0100682 quorum_aio_finalize(acb);
683
Kevin Wolfce15dc02016-11-08 11:10:14 +0100684 return ret;
Benoît Canet7db69822014-02-21 22:21:14 +0100685}
686
Kevin Wolfce15dc02016-11-08 11:10:14 +0100687static void write_quorum_entry(void *opaque)
688{
689 QuorumCo *co = opaque;
690 QuorumAIOCB *acb = co->acb;
691 BDRVQuorumState *s = acb->bs->opaque;
692 int i = co->idx;
Kevin Wolf7cd9b392016-11-10 16:13:15 +0100693 QuorumChildRequest *sacb = &acb->qcrs[i];
Kevin Wolfce15dc02016-11-08 11:10:14 +0100694
Kevin Wolf7cd9b392016-11-10 16:13:15 +0100695 sacb->bs = s->children[i]->bs;
Alberto Garcia5cddb2e2020-11-13 17:52:32 +0100696 if (acb->flags & BDRV_REQ_ZERO_WRITE) {
697 sacb->ret = bdrv_co_pwrite_zeroes(s->children[i], acb->offset,
698 acb->bytes, acb->flags);
699 } else {
700 sacb->ret = bdrv_co_pwritev(s->children[i], acb->offset, acb->bytes,
701 acb->qiov, acb->flags);
702 }
Kevin Wolf7cd9b392016-11-10 16:13:15 +0100703 if (sacb->ret == 0) {
704 acb->success_count++;
705 } else {
706 quorum_report_bad_acb(sacb, sacb->ret);
707 }
708 acb->count++;
709 assert(acb->count <= s->num_children);
710 assert(acb->success_count <= s->num_children);
711
712 /* Wake up the caller after the last write */
713 if (acb->count == s->num_children) {
714 qemu_coroutine_enter_if_inactive(acb->co);
715 }
Kevin Wolfce15dc02016-11-08 11:10:14 +0100716}
717
Vladimir Sementsov-Ogievskiye75abed2021-09-03 13:28:00 +0300718static int quorum_co_pwritev(BlockDriverState *bs, int64_t offset,
719 int64_t bytes, QEMUIOVector *qiov,
720 BdrvRequestFlags flags)
Benoît Canet13e79562014-02-21 22:21:12 +0100721{
722 BDRVQuorumState *s = bs->opaque;
Max Reitz1b1a9202018-04-21 15:29:25 +0200723 QuorumAIOCB *acb = quorum_aio_get(bs, qiov, offset, bytes, flags);
Kevin Wolfce15dc02016-11-08 11:10:14 +0100724 int i, ret;
Benoît Canet13e79562014-02-21 22:21:12 +0100725
726 for (i = 0; i < s->num_children; i++) {
Kevin Wolfce15dc02016-11-08 11:10:14 +0100727 Coroutine *co;
728 QuorumCo data = {
729 .acb = acb,
730 .idx = i,
731 };
732
733 co = qemu_coroutine_create(write_quorum_entry, &data);
734 qemu_coroutine_enter(co);
Benoît Canet13e79562014-02-21 22:21:12 +0100735 }
736
Kevin Wolf7cd9b392016-11-10 16:13:15 +0100737 while (acb->count < s->num_children) {
Kevin Wolfce15dc02016-11-08 11:10:14 +0100738 qemu_coroutine_yield();
739 }
740
Kevin Wolf7cd9b392016-11-10 16:13:15 +0100741 quorum_has_too_much_io_failed(acb);
742
Kevin Wolfce15dc02016-11-08 11:10:14 +0100743 ret = acb->vote_ret;
Kevin Wolf0f319772016-11-10 14:24:27 +0100744 quorum_aio_finalize(acb);
Kevin Wolfce15dc02016-11-08 11:10:14 +0100745
746 return ret;
Benoît Canet13e79562014-02-21 22:21:12 +0100747}
748
Alberto Garcia5cddb2e2020-11-13 17:52:32 +0100749static int quorum_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
Vladimir Sementsov-Ogievskiyf34b2bc2021-09-03 13:28:03 +0300750 int64_t bytes, BdrvRequestFlags flags)
Alberto Garcia5cddb2e2020-11-13 17:52:32 +0100751
752{
753 return quorum_co_pwritev(bs, offset, bytes, NULL,
754 flags | BDRV_REQ_ZERO_WRITE);
755}
756
Benoît Canetd55dee22014-02-21 22:21:16 +0100757static int64_t quorum_getlength(BlockDriverState *bs)
758{
759 BDRVQuorumState *s = bs->opaque;
760 int64_t result;
761 int i;
762
763 /* check that all file have the same length */
Kevin Wolf0bd6e912015-06-16 11:29:22 +0200764 result = bdrv_getlength(s->children[0]->bs);
Benoît Canetd55dee22014-02-21 22:21:16 +0100765 if (result < 0) {
766 return result;
767 }
768 for (i = 1; i < s->num_children; i++) {
Kevin Wolf0bd6e912015-06-16 11:29:22 +0200769 int64_t value = bdrv_getlength(s->children[i]->bs);
Benoît Canetd55dee22014-02-21 22:21:16 +0100770 if (value < 0) {
771 return value;
772 }
773 if (value != result) {
774 return -EIO;
775 }
776 }
777
778 return result;
779}
780
Benoît Canet1c508d12014-02-21 22:21:18 +0100781static coroutine_fn int quorum_co_flush(BlockDriverState *bs)
782{
783 BDRVQuorumState *s = bs->opaque;
784 QuorumVoteVersion *winner = NULL;
785 QuorumVotes error_votes;
786 QuorumVoteValue result_value;
787 int i;
788 int result = 0;
Changlong Xie924e8a22016-02-26 09:39:02 +0800789 int success_count = 0;
Benoît Canet1c508d12014-02-21 22:21:18 +0100790
791 QLIST_INIT(&error_votes.vote_list);
792 error_votes.compare = quorum_64bits_compare;
793
794 for (i = 0; i < s->num_children; i++) {
Kevin Wolf0bd6e912015-06-16 11:29:22 +0200795 result = bdrv_co_flush(s->children[i]->bs);
Changlong Xie924e8a22016-02-26 09:39:02 +0800796 if (result) {
Alberto Garcia795be062017-08-07 15:36:58 +0300797 quorum_report_bad(QUORUM_OP_TYPE_FLUSH, 0, 0,
Changlong Xie924e8a22016-02-26 09:39:02 +0800798 s->children[i]->bs->node_name, result);
799 result_value.l = result;
800 quorum_count_vote(&error_votes, &result_value, i);
801 } else {
802 success_count++;
803 }
Benoît Canet1c508d12014-02-21 22:21:18 +0100804 }
805
Changlong Xie924e8a22016-02-26 09:39:02 +0800806 if (success_count >= s->threshold) {
807 result = 0;
808 } else {
809 winner = quorum_get_vote_winner(&error_votes);
810 result = winner->value.l;
811 }
Benoît Canet1c508d12014-02-21 22:21:18 +0100812 quorum_free_vote_list(&error_votes);
813
814 return result;
815}
816
Max Reitza3ed7942020-02-18 11:34:43 +0100817static bool quorum_recurse_can_replace(BlockDriverState *bs,
818 BlockDriverState *to_replace)
819{
820 BDRVQuorumState *s = bs->opaque;
821 int i;
822
823 for (i = 0; i < s->num_children; i++) {
824 /*
825 * We have no idea whether our children show the same data as
826 * this node (@bs). It is actually highly likely that
827 * @to_replace does not, because replacing a broken child is
828 * one of the main use cases here.
829 *
830 * We do know that the new BDS will match @bs, so replacing
831 * any of our children by it will be safe. It cannot change
832 * the data this quorum node presents to its parents.
833 *
834 * However, replacing @to_replace by @bs in any of our
835 * children's chains may change visible data somewhere in
836 * there. We therefore cannot recurse down those chains with
837 * bdrv_recurse_can_replace().
838 * (More formally, bdrv_recurse_can_replace() requires that
839 * @to_replace will be replaced by something matching the @bs
840 * passed to it. We cannot guarantee that.)
841 *
842 * Thus, we can only check whether any of our immediate
843 * children matches @to_replace.
844 *
845 * (In the future, we might add a function to recurse down a
846 * chain that checks that nothing there cares about a change
847 * in data from the respective child in question. For
848 * example, most filters do not care when their child's data
849 * suddenly changes, as long as their parents do not care.)
850 */
851 if (s->children[i]->bs == to_replace) {
852 /*
853 * We now have to ensure that there is no other parent
854 * that cares about replacing this child by a node with
855 * potentially different data.
856 * We do so by checking whether there are any other parents
857 * at all, which is stricter than necessary, but also very
858 * simple. (We may decide to implement something more
859 * complex and permissive when there is an actual need for
860 * it.)
861 */
862 return QLIST_FIRST(&to_replace->parents) == s->children[i] &&
863 QLIST_NEXT(s->children[i], next_parent) == NULL;
864 }
865 }
866
867 return false;
868}
869
Benoît Canetc88a1de2014-02-21 22:21:20 +0100870static int quorum_valid_threshold(int threshold, int num_children, Error **errp)
871{
872
873 if (threshold < 1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100874 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Markus Armbruster6cc06672020-11-13 09:26:26 +0100875 "vote-threshold", "a value >= 1");
Benoît Canetc88a1de2014-02-21 22:21:20 +0100876 return -ERANGE;
877 }
878
879 if (threshold > num_children) {
880 error_setg(errp, "threshold may not exceed children count");
881 return -ERANGE;
882 }
883
884 return 0;
885}
886
887static QemuOptsList quorum_runtime_opts = {
888 .name = "quorum",
889 .head = QTAILQ_HEAD_INITIALIZER(quorum_runtime_opts.head),
890 .desc = {
891 {
892 .name = QUORUM_OPT_VOTE_THRESHOLD,
893 .type = QEMU_OPT_NUMBER,
894 .help = "The number of vote needed for reaching quorum",
895 },
896 {
897 .name = QUORUM_OPT_BLKVERIFY,
898 .type = QEMU_OPT_BOOL,
899 .help = "Trigger block verify mode if set",
900 },
Benoît Canetcf29a572014-06-11 15:24:10 +0200901 {
902 .name = QUORUM_OPT_REWRITE,
903 .type = QEMU_OPT_BOOL,
904 .help = "Rewrite corrupted block on read quorum",
905 },
Liu Yuana9db86b2014-08-18 17:41:05 +0800906 {
907 .name = QUORUM_OPT_READ_PATTERN,
908 .type = QEMU_OPT_STRING,
909 .help = "Allowed pattern: quorum, fifo. Quorum is default",
910 },
Benoît Canetc88a1de2014-02-21 22:21:20 +0100911 { /* end of list */ }
912 },
913};
914
Alberto Garcia5cddb2e2020-11-13 17:52:32 +0100915static void quorum_refresh_flags(BlockDriverState *bs)
916{
917 BDRVQuorumState *s = bs->opaque;
918 int i;
919
920 bs->supported_zero_flags =
921 BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK;
922
923 for (i = 0; i < s->num_children; i++) {
924 bs->supported_zero_flags &= s->children[i]->bs->supported_zero_flags;
925 }
926
927 bs->supported_zero_flags |= BDRV_REQ_WRITE_UNCHANGED;
928}
929
Benoît Canetc88a1de2014-02-21 22:21:20 +0100930static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
931 Error **errp)
932{
933 BDRVQuorumState *s = bs->opaque;
Fam Zheng8df3abf2014-08-28 13:56:12 +0800934 QemuOpts *opts = NULL;
Marc-André Lureau8d5fb192017-08-24 10:46:03 +0200935 const char *pattern_str;
Benoît Canetc88a1de2014-02-21 22:21:20 +0100936 bool *opened;
Benoît Canetc88a1de2014-02-21 22:21:20 +0100937 int i;
938 int ret = 0;
939
940 qdict_flatten(options);
Benoît Canetc88a1de2014-02-21 22:21:20 +0100941
Kevin Wolfea6828d2015-01-21 18:49:28 +0100942 /* count how many different children are present */
943 s->num_children = qdict_array_entries(options, "children.");
944 if (s->num_children < 0) {
Markus Armbrusterdcfe4802020-07-07 18:06:01 +0200945 error_setg(errp, "Option children is not a valid array");
Max Reitz8a87f3d2014-02-21 22:30:37 +0100946 ret = -EINVAL;
947 goto exit;
948 }
Wen Congyang98292c62016-05-10 15:36:38 +0800949 if (s->num_children < 1) {
Markus Armbrusterdcfe4802020-07-07 18:06:01 +0200950 error_setg(errp, "Number of provided children must be 1 or more");
Benoît Canetc88a1de2014-02-21 22:21:20 +0100951 ret = -EINVAL;
952 goto exit;
953 }
954
955 opts = qemu_opts_create(&quorum_runtime_opts, NULL, 0, &error_abort);
Markus Armbrustera5f9b9d2020-07-07 18:06:05 +0200956 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
Benoît Canetc88a1de2014-02-21 22:21:20 +0100957 ret = -EINVAL;
958 goto exit;
959 }
960
961 s->threshold = qemu_opt_get_number(opts, QUORUM_OPT_VOTE_THRESHOLD, 0);
Wen Congyang834cb2a2015-07-03 14:45:06 +0800962 /* and validate it against s->num_children */
Markus Armbrusterdcfe4802020-07-07 18:06:01 +0200963 ret = quorum_valid_threshold(s->threshold, s->num_children, errp);
Wen Congyang834cb2a2015-07-03 14:45:06 +0800964 if (ret < 0) {
965 goto exit;
966 }
967
Marc-André Lureau8d5fb192017-08-24 10:46:03 +0200968 pattern_str = qemu_opt_get(opts, QUORUM_OPT_READ_PATTERN);
969 if (!pattern_str) {
970 ret = QUORUM_READ_PATTERN_QUORUM;
971 } else {
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200972 ret = qapi_enum_parse(&QuorumReadPattern_lookup, pattern_str,
Marc-André Lureau8d5fb192017-08-24 10:46:03 +0200973 -EINVAL, NULL);
974 }
Benoît Canetc88a1de2014-02-21 22:21:20 +0100975 if (ret < 0) {
Markus Armbrusterdcfe4802020-07-07 18:06:01 +0200976 error_setg(errp, "Please set read-pattern as fifo or quorum");
Benoît Canetc88a1de2014-02-21 22:21:20 +0100977 goto exit;
978 }
Liu Yuana9db86b2014-08-18 17:41:05 +0800979 s->read_pattern = ret;
Benoît Canetc88a1de2014-02-21 22:21:20 +0100980
Liu Yuana9db86b2014-08-18 17:41:05 +0800981 if (s->read_pattern == QUORUM_READ_PATTERN_QUORUM) {
Alberto Garcia83aedca2018-10-17 17:33:50 +0300982 s->is_blkverify = qemu_opt_get_bool(opts, QUORUM_OPT_BLKVERIFY, false);
983 if (s->is_blkverify && (s->num_children != 2 || s->threshold != 2)) {
Markus Armbrusterdcfe4802020-07-07 18:06:01 +0200984 error_setg(errp, "blkverify=on can only be set if there are "
Alberto Garcia83aedca2018-10-17 17:33:50 +0300985 "exactly two files and vote-threshold is 2");
986 ret = -EINVAL;
987 goto exit;
Liu Yuana9db86b2014-08-18 17:41:05 +0800988 }
989
990 s->rewrite_corrupted = qemu_opt_get_bool(opts, QUORUM_OPT_REWRITE,
991 false);
992 if (s->rewrite_corrupted && s->is_blkverify) {
Markus Armbrusterdcfe4802020-07-07 18:06:01 +0200993 error_setg(errp,
Liu Yuana9db86b2014-08-18 17:41:05 +0800994 "rewrite-corrupted=on cannot be used with blkverify=on");
995 ret = -EINVAL;
996 goto exit;
997 }
Benoît Canetcf29a572014-06-11 15:24:10 +0200998 }
999
Kevin Wolf0bd6e912015-06-16 11:29:22 +02001000 /* allocate the children array */
1001 s->children = g_new0(BdrvChild *, s->num_children);
Benoît Canetc88a1de2014-02-21 22:21:20 +01001002 opened = g_new0(bool, s->num_children);
1003
Kevin Wolfea6828d2015-01-21 18:49:28 +01001004 for (i = 0; i < s->num_children; i++) {
Lukas Straub5eb9a3c2020-08-04 12:46:42 +02001005 char indexstr[INDEXSTR_LEN];
1006 ret = snprintf(indexstr, INDEXSTR_LEN, "children.%d", i);
1007 assert(ret < INDEXSTR_LEN);
Max Reitz8a87f3d2014-02-21 22:30:37 +01001008
Kevin Wolf0bd6e912015-06-16 11:29:22 +02001009 s->children[i] = bdrv_open_child(NULL, options, indexstr, bs,
Max Reitz36ee58d2020-05-13 13:05:31 +02001010 &child_of_bds, BDRV_CHILD_DATA, false,
Vladimir Sementsov-Ogievskiybc520242021-02-02 15:49:45 +03001011 errp);
1012 if (!s->children[i]) {
Kevin Wolf0bd6e912015-06-16 11:29:22 +02001013 ret = -EINVAL;
Max Reitz8a87f3d2014-02-21 22:30:37 +01001014 goto close_exit;
Benoît Canetc88a1de2014-02-21 22:21:20 +01001015 }
Kevin Wolfea6828d2015-01-21 18:49:28 +01001016
Max Reitz8a87f3d2014-02-21 22:30:37 +01001017 opened[i] = true;
Benoît Canetc88a1de2014-02-21 22:21:20 +01001018 }
Wen Congyang98292c62016-05-10 15:36:38 +08001019 s->next_child_index = s->num_children;
Benoît Canetc88a1de2014-02-21 22:21:20 +01001020
Max Reitz1b1a9202018-04-21 15:29:25 +02001021 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED;
Alberto Garcia5cddb2e2020-11-13 17:52:32 +01001022 quorum_refresh_flags(bs);
Max Reitz1b1a9202018-04-21 15:29:25 +02001023
Benoît Canetc88a1de2014-02-21 22:21:20 +01001024 g_free(opened);
1025 goto exit;
1026
1027close_exit:
1028 /* cleanup on error */
1029 for (i = 0; i < s->num_children; i++) {
1030 if (!opened[i]) {
1031 continue;
1032 }
Kevin Wolf0bd6e912015-06-16 11:29:22 +02001033 bdrv_unref_child(bs, s->children[i]);
Benoît Canetc88a1de2014-02-21 22:21:20 +01001034 }
Kevin Wolf0bd6e912015-06-16 11:29:22 +02001035 g_free(s->children);
Benoît Canetc88a1de2014-02-21 22:21:20 +01001036 g_free(opened);
1037exit:
Fam Zheng8df3abf2014-08-28 13:56:12 +08001038 qemu_opts_del(opts);
Benoît Canetc88a1de2014-02-21 22:21:20 +01001039 return ret;
1040}
1041
1042static void quorum_close(BlockDriverState *bs)
1043{
1044 BDRVQuorumState *s = bs->opaque;
1045 int i;
1046
1047 for (i = 0; i < s->num_children; i++) {
Kevin Wolf0bd6e912015-06-16 11:29:22 +02001048 bdrv_unref_child(bs, s->children[i]);
Benoît Canetc88a1de2014-02-21 22:21:20 +01001049 }
1050
Kevin Wolf0bd6e912015-06-16 11:29:22 +02001051 g_free(s->children);
Benoît Canetc88a1de2014-02-21 22:21:20 +01001052}
1053
Wen Congyang98292c62016-05-10 15:36:38 +08001054static void quorum_add_child(BlockDriverState *bs, BlockDriverState *child_bs,
1055 Error **errp)
1056{
1057 BDRVQuorumState *s = bs->opaque;
1058 BdrvChild *child;
Lukas Straub5eb9a3c2020-08-04 12:46:42 +02001059 char indexstr[INDEXSTR_LEN];
Wen Congyang98292c62016-05-10 15:36:38 +08001060 int ret;
1061
Alberto Garcia808b27d2018-10-18 11:59:03 +03001062 if (s->is_blkverify) {
1063 error_setg(errp, "Cannot add a child to a quorum in blkverify mode");
1064 return;
1065 }
1066
Wen Congyang98292c62016-05-10 15:36:38 +08001067 assert(s->num_children <= INT_MAX / sizeof(BdrvChild *));
1068 if (s->num_children == INT_MAX / sizeof(BdrvChild *) ||
1069 s->next_child_index == UINT_MAX) {
1070 error_setg(errp, "Too many children");
1071 return;
1072 }
1073
Lukas Straub5eb9a3c2020-08-04 12:46:42 +02001074 ret = snprintf(indexstr, INDEXSTR_LEN, "children.%u", s->next_child_index);
1075 if (ret < 0 || ret >= INDEXSTR_LEN) {
Wen Congyang98292c62016-05-10 15:36:38 +08001076 error_setg(errp, "cannot generate child name");
1077 return;
1078 }
1079 s->next_child_index++;
1080
1081 bdrv_drained_begin(bs);
1082
1083 /* We can safely add the child now */
1084 bdrv_ref(child_bs);
Kevin Wolf8b2ff522016-12-20 22:21:17 +01001085
Max Reitz36ee58d2020-05-13 13:05:31 +02001086 child = bdrv_attach_child(bs, child_bs, indexstr, &child_of_bds,
1087 BDRV_CHILD_DATA, errp);
Kevin Wolf8b2ff522016-12-20 22:21:17 +01001088 if (child == NULL) {
1089 s->next_child_index--;
Kevin Wolf8b2ff522016-12-20 22:21:17 +01001090 goto out;
1091 }
Wen Congyang98292c62016-05-10 15:36:38 +08001092 s->children = g_renew(BdrvChild *, s->children, s->num_children + 1);
1093 s->children[s->num_children++] = child;
Alberto Garcia5cddb2e2020-11-13 17:52:32 +01001094 quorum_refresh_flags(bs);
Wen Congyang98292c62016-05-10 15:36:38 +08001095
Kevin Wolf8b2ff522016-12-20 22:21:17 +01001096out:
Wen Congyang98292c62016-05-10 15:36:38 +08001097 bdrv_drained_end(bs);
1098}
1099
1100static void quorum_del_child(BlockDriverState *bs, BdrvChild *child,
1101 Error **errp)
1102{
1103 BDRVQuorumState *s = bs->opaque;
Lukas Straub5eb9a3c2020-08-04 12:46:42 +02001104 char indexstr[INDEXSTR_LEN];
Wen Congyang98292c62016-05-10 15:36:38 +08001105 int i;
1106
1107 for (i = 0; i < s->num_children; i++) {
1108 if (s->children[i] == child) {
1109 break;
1110 }
1111 }
1112
1113 /* we have checked it in bdrv_del_child() */
1114 assert(i < s->num_children);
1115
1116 if (s->num_children <= s->threshold) {
1117 error_setg(errp,
1118 "The number of children cannot be lower than the vote threshold %d",
1119 s->threshold);
1120 return;
1121 }
1122
Alberto Garcia808b27d2018-10-18 11:59:03 +03001123 /* We know now that num_children > threshold, so blkverify must be false */
1124 assert(!s->is_blkverify);
1125
Lukas Straub5eb9a3c2020-08-04 12:46:42 +02001126 snprintf(indexstr, INDEXSTR_LEN, "children.%u", s->next_child_index - 1);
1127 if (!strncmp(child->name, indexstr, INDEXSTR_LEN)) {
1128 s->next_child_index--;
1129 }
1130
Wen Congyang98292c62016-05-10 15:36:38 +08001131 bdrv_drained_begin(bs);
1132
1133 /* We can safely remove this child now */
1134 memmove(&s->children[i], &s->children[i + 1],
1135 (s->num_children - i - 1) * sizeof(BdrvChild *));
1136 s->children = g_renew(BdrvChild *, s->children, --s->num_children);
1137 bdrv_unref_child(bs, child);
1138
Alberto Garcia5cddb2e2020-11-13 17:52:32 +01001139 quorum_refresh_flags(bs);
Wen Congyang98292c62016-05-10 15:36:38 +08001140 bdrv_drained_end(bs);
1141}
1142
Max Reitzabc521a2019-02-01 20:29:26 +01001143static void quorum_gather_child_options(BlockDriverState *bs, QDict *target,
1144 bool backing_overridden)
1145{
1146 BDRVQuorumState *s = bs->opaque;
1147 QList *children_list;
1148 int i;
1149
1150 /*
1151 * The generic implementation for gathering child options in
1152 * bdrv_refresh_filename() would use the names of the children
1153 * as specified for bdrv_open_child() or bdrv_attach_child(),
1154 * which is "children.%u" with %u being a value
1155 * (s->next_child_index) that is incremented each time a new child
1156 * is added (and never decremented). Since children can be
1157 * deleted at runtime, there may be gaps in that enumeration.
1158 * When creating a new quorum BDS and specifying the children for
1159 * it through runtime options, the enumeration used there may not
1160 * have any gaps, though.
1161 *
1162 * Therefore, we have to create a new gap-less enumeration here
1163 * (which we can achieve by simply putting all of the children's
1164 * full_open_options into a QList).
1165 *
1166 * XXX: Note that there are issues with the current child option
1167 * structure quorum uses (such as the fact that children do
1168 * not really have unique permanent names). Therefore, this
1169 * is going to have to change in the future and ideally we
1170 * want quorum to be covered by the generic implementation.
1171 */
1172
1173 children_list = qlist_new();
1174 qdict_put(target, "children", children_list);
1175
1176 for (i = 0; i < s->num_children; i++) {
1177 qlist_append(children_list,
1178 qobject_ref(s->children[i]->bs->full_open_options));
1179 }
1180}
1181
Max Reitzf3037bd2019-02-01 20:29:20 +01001182static char *quorum_dirname(BlockDriverState *bs, Error **errp)
1183{
1184 /* In general, there are multiple BDSs with different dirnames below this
1185 * one; so there is no unique dirname we could return (unless all are equal
1186 * by chance, or there is only one). Therefore, to be consistent, just
1187 * always return NULL. */
1188 error_setg(errp, "Cannot generate a base directory for quorum nodes");
1189 return NULL;
1190}
1191
Max Reitz37a37912020-02-18 11:34:40 +01001192static void quorum_child_perm(BlockDriverState *bs, BdrvChild *c,
Max Reitzbf8e9252020-05-13 13:05:16 +02001193 BdrvChildRole role,
Max Reitz37a37912020-02-18 11:34:40 +01001194 BlockReopenQueue *reopen_queue,
1195 uint64_t perm, uint64_t shared,
1196 uint64_t *nperm, uint64_t *nshared)
1197{
Max Reitz9ca5b0e2020-11-13 22:17:16 +01001198 BDRVQuorumState *s = bs->opaque;
1199
Max Reitz37a37912020-02-18 11:34:40 +01001200 *nperm = perm & DEFAULT_PERM_PASSTHROUGH;
Max Reitz9ca5b0e2020-11-13 22:17:16 +01001201 if (s->rewrite_corrupted) {
1202 *nperm |= BLK_PERM_WRITE;
1203 }
Max Reitz37a37912020-02-18 11:34:40 +01001204
1205 /*
1206 * We cannot share RESIZE or WRITE, as this would make the
1207 * children differ from each other.
1208 */
1209 *nshared = (shared & (BLK_PERM_CONSISTENT_READ |
1210 BLK_PERM_WRITE_UNCHANGED))
1211 | DEFAULT_PERM_UNCHANGED;
1212}
1213
Alberto Garciaef9bba12020-11-13 17:52:31 +01001214/*
1215 * Each one of the children can report different status flags even
1216 * when they contain the same data, so what this function does is
1217 * return BDRV_BLOCK_ZERO if *all* children agree that a certain
1218 * region contains zeroes, and BDRV_BLOCK_DATA otherwise.
1219 */
1220static int coroutine_fn quorum_co_block_status(BlockDriverState *bs,
1221 bool want_zero,
1222 int64_t offset, int64_t count,
1223 int64_t *pnum, int64_t *map,
1224 BlockDriverState **file)
1225{
1226 BDRVQuorumState *s = bs->opaque;
1227 int i, ret;
1228 int64_t pnum_zero = count;
1229 int64_t pnum_data = 0;
1230
1231 for (i = 0; i < s->num_children; i++) {
1232 int64_t bytes;
1233 ret = bdrv_co_common_block_status_above(s->children[i]->bs, NULL, false,
1234 want_zero, offset, count,
1235 &bytes, NULL, NULL, NULL);
1236 if (ret < 0) {
1237 quorum_report_bad(QUORUM_OP_TYPE_READ, offset, count,
1238 s->children[i]->bs->node_name, ret);
1239 pnum_data = count;
1240 break;
1241 }
1242 /*
1243 * Even if all children agree about whether there are zeroes
1244 * or not at @offset they might disagree on the size, so use
1245 * the smallest when reporting BDRV_BLOCK_ZERO and the largest
1246 * when reporting BDRV_BLOCK_DATA.
1247 */
1248 if (ret & BDRV_BLOCK_ZERO) {
1249 pnum_zero = MIN(pnum_zero, bytes);
1250 } else {
1251 pnum_data = MAX(pnum_data, bytes);
1252 }
1253 }
1254
1255 if (pnum_data) {
1256 *pnum = pnum_data;
1257 return BDRV_BLOCK_DATA;
1258 } else {
1259 *pnum = pnum_zero;
1260 return BDRV_BLOCK_ZERO;
1261 }
1262}
1263
Max Reitz26542672019-02-01 20:29:25 +01001264static const char *const quorum_strong_runtime_opts[] = {
1265 QUORUM_OPT_VOTE_THRESHOLD,
1266 QUORUM_OPT_BLKVERIFY,
1267 QUORUM_OPT_REWRITE,
1268 QUORUM_OPT_READ_PATTERN,
1269
1270 NULL
1271};
1272
Benoît Canetcadebd72014-02-21 22:21:11 +01001273static BlockDriver bdrv_quorum = {
Stefan Hajnoczie3625d32014-05-08 16:34:46 +02001274 .format_name = "quorum",
Benoît Canetcadebd72014-02-21 22:21:11 +01001275
Stefan Hajnoczie3625d32014-05-08 16:34:46 +02001276 .instance_size = sizeof(BDRVQuorumState),
Benoît Canet13e79562014-02-21 22:21:12 +01001277
Fabiano Rosas65d2c3e2018-03-12 19:07:50 -03001278 .bdrv_open = quorum_open,
Stefan Hajnoczie3625d32014-05-08 16:34:46 +02001279 .bdrv_close = quorum_close,
Max Reitzabc521a2019-02-01 20:29:26 +01001280 .bdrv_gather_child_options = quorum_gather_child_options,
Max Reitzf3037bd2019-02-01 20:29:20 +01001281 .bdrv_dirname = quorum_dirname,
Alberto Garciaef9bba12020-11-13 17:52:31 +01001282 .bdrv_co_block_status = quorum_co_block_status,
Benoît Canetc88a1de2014-02-21 22:21:20 +01001283
Lukas Straub5529b022021-05-18 13:42:14 +02001284 .bdrv_co_flush = quorum_co_flush,
Benoît Canet1c508d12014-02-21 22:21:18 +01001285
Stefan Hajnoczie3625d32014-05-08 16:34:46 +02001286 .bdrv_getlength = quorum_getlength,
Benoît Canetd55dee22014-02-21 22:21:16 +01001287
Kevin Wolf6847da32016-11-10 17:22:07 +01001288 .bdrv_co_preadv = quorum_co_preadv,
1289 .bdrv_co_pwritev = quorum_co_pwritev,
Alberto Garcia5cddb2e2020-11-13 17:52:32 +01001290 .bdrv_co_pwrite_zeroes = quorum_co_pwrite_zeroes,
Benoît Canet98a7a382014-02-21 22:21:19 +01001291
Wen Congyang98292c62016-05-10 15:36:38 +08001292 .bdrv_add_child = quorum_add_child,
1293 .bdrv_del_child = quorum_del_child,
1294
Max Reitz37a37912020-02-18 11:34:40 +01001295 .bdrv_child_perm = quorum_child_perm,
Kevin Wolfd7010df2016-12-15 12:28:58 +01001296
Max Reitza3ed7942020-02-18 11:34:43 +01001297 .bdrv_recurse_can_replace = quorum_recurse_can_replace,
Max Reitz26542672019-02-01 20:29:25 +01001298
1299 .strong_runtime_opts = quorum_strong_runtime_opts,
Benoît Canetcadebd72014-02-21 22:21:11 +01001300};
1301
1302static void bdrv_quorum_init(void)
1303{
Sascha Silbee94867e2015-08-04 16:48:25 +02001304 if (!qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA256)) {
1305 /* SHA256 hash support is required for quorum device */
1306 return;
1307 }
Benoît Canetcadebd72014-02-21 22:21:11 +01001308 bdrv_register(&bdrv_quorum);
1309}
1310
1311block_init(bdrv_quorum_init);