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