Benoît Canet | 27cec15 | 2014-02-21 22:21:10 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Quorum Block filter |
| 3 | * |
| 4 | * Copyright (C) 2012-2014 Nodalink, EURL. |
| 5 | * |
| 6 | * Author: |
| 7 | * Benoît Canet <benoit.canet@irqsave.net> |
| 8 | * |
| 9 | * Based on the design and code of blkverify.c (Copyright (C) 2010 IBM, Corp) |
| 10 | * and blkmirror.c (Copyright (C) 2011 Red Hat, Inc). |
| 11 | * |
| 12 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 13 | * See the COPYING file in the top-level directory. |
| 14 | */ |
| 15 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 16 | #include "qemu/osdep.h" |
Wen Congyang | 98292c6 | 2016-05-10 15:36:38 +0800 | [diff] [blame] | 17 | #include "qemu/cutils.h" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 18 | #include "qemu/option.h" |
Benoît Canet | 27cec15 | 2014-02-21 22:21:10 +0100 | [diff] [blame] | 19 | #include "block/block_int.h" |
Max Reitz | 609f45e | 2018-06-14 21:14:28 +0200 | [diff] [blame] | 20 | #include "block/qdict.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 21 | #include "qapi/error.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 22 | #include "qapi/qapi-events-block.h" |
Max Reitz | fafcfe2 | 2014-07-18 20:25:00 +0200 | [diff] [blame] | 23 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | cc7a8ea | 2015-03-17 17:22:46 +0100 | [diff] [blame] | 24 | #include "qapi/qmp/qerror.h" |
Max Reitz | fafcfe2 | 2014-07-18 20:25:00 +0200 | [diff] [blame] | 25 | #include "qapi/qmp/qlist.h" |
| 26 | #include "qapi/qmp/qstring.h" |
Daniel P. Berrange | 488981a4 | 2015-07-01 18:10:35 +0100 | [diff] [blame] | 27 | #include "crypto/hash.h" |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 28 | |
| 29 | #define HASH_LENGTH 32 |
| 30 | |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 31 | #define QUORUM_OPT_VOTE_THRESHOLD "vote-threshold" |
| 32 | #define QUORUM_OPT_BLKVERIFY "blkverify" |
Benoît Canet | cf29a57 | 2014-06-11 15:24:10 +0200 | [diff] [blame] | 33 | #define QUORUM_OPT_REWRITE "rewrite-corrupted" |
Liu Yuan | a9db86b | 2014-08-18 17:41:05 +0800 | [diff] [blame] | 34 | #define QUORUM_OPT_READ_PATTERN "read-pattern" |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 35 | |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 36 | /* This union holds a vote hash value */ |
| 37 | typedef union QuorumVoteValue { |
Daniel P. Berrange | 488981a4 | 2015-07-01 18:10:35 +0100 | [diff] [blame] | 38 | uint8_t h[HASH_LENGTH]; /* SHA-256 hash */ |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 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; |
Benoît Canet | 27cec15 | 2014-02-21 22:21:10 +0100 | [diff] [blame] | 66 | |
Benoît Canet | cadebd7 | 2014-02-21 22:21:11 +0100 | [diff] [blame] | 67 | /* the following structure holds the state of one quorum instance */ |
| 68 | typedef struct BDRVQuorumState { |
Kevin Wolf | 0bd6e91 | 2015-06-16 11:29:22 +0200 | [diff] [blame] | 69 | BdrvChild **children; /* children BlockDriverStates */ |
Benoît Canet | cadebd7 | 2014-02-21 22:21:11 +0100 | [diff] [blame] | 70 | int num_children; /* children count */ |
Wen Congyang | 98292c6 | 2016-05-10 15:36:38 +0800 | [diff] [blame] | 71 | unsigned next_child_index; /* the index of the next child that should |
| 72 | * be added |
| 73 | */ |
Benoît Canet | cadebd7 | 2014-02-21 22:21:11 +0100 | [diff] [blame] | 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 | */ |
Benoît Canet | cf29a57 | 2014-06-11 15:24:10 +0200 | [diff] [blame] | 85 | bool rewrite_corrupted;/* true if the driver must rewrite-on-read corrupted |
| 86 | * block if Quorum is reached. |
| 87 | */ |
Liu Yuan | a9db86b | 2014-08-18 17:41:05 +0800 | [diff] [blame] | 88 | |
| 89 | QuorumReadPattern read_pattern; |
Benoît Canet | cadebd7 | 2014-02-21 22:21:11 +0100 | [diff] [blame] | 90 | } BDRVQuorumState; |
| 91 | |
Benoît Canet | 27cec15 | 2014-02-21 22:21:10 +0100 | [diff] [blame] | 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 { |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 100 | BlockDriverState *bs; |
Benoît Canet | 27cec15 | 2014-02-21 22:21:10 +0100 | [diff] [blame] | 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 { |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 113 | BlockDriverState *bs; |
| 114 | Coroutine *co; |
Benoît Canet | 27cec15 | 2014-02-21 22:21:10 +0100 | [diff] [blame] | 115 | |
| 116 | /* Request metadata */ |
Kevin Wolf | 6847da3 | 2016-11-10 17:22:07 +0100 | [diff] [blame] | 117 | uint64_t offset; |
| 118 | uint64_t bytes; |
Max Reitz | 1b1a920 | 2018-04-21 15:29:25 +0200 | [diff] [blame] | 119 | int flags; |
Benoît Canet | 27cec15 | 2014-02-21 22:21:10 +0100 | [diff] [blame] | 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 | |
Benoît Canet | cf29a57 | 2014-06-11 15:24:10 +0200 | [diff] [blame] | 127 | int rewrite_count; /* number of replica to rewrite: count down to |
| 128 | * zero once writes are fired |
| 129 | */ |
| 130 | |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 131 | QuorumVotes votes; |
| 132 | |
Benoît Canet | 27cec15 | 2014-02-21 22:21:10 +0100 | [diff] [blame] | 133 | bool is_read; |
| 134 | int vote_ret; |
Paolo Bonzini | 86ec252 | 2016-10-05 18:35:26 +0200 | [diff] [blame] | 135 | int children_read; /* how many children have been read from */ |
Benoît Canet | 27cec15 | 2014-02-21 22:21:10 +0100 | [diff] [blame] | 136 | }; |
Benoît Canet | cadebd7 | 2014-02-21 22:21:11 +0100 | [diff] [blame] | 137 | |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 138 | typedef struct QuorumCo { |
| 139 | QuorumAIOCB *acb; |
| 140 | int idx; |
| 141 | } QuorumCo; |
| 142 | |
Benoît Canet | 13e7956 | 2014-02-21 22:21:12 +0100 | [diff] [blame] | 143 | static void quorum_aio_finalize(QuorumAIOCB *acb) |
| 144 | { |
Benoît Canet | 13e7956 | 2014-02-21 22:21:12 +0100 | [diff] [blame] | 145 | g_free(acb->qcrs); |
Kevin Wolf | 0f31977 | 2016-11-10 14:24:27 +0100 | [diff] [blame] | 146 | g_free(acb); |
Benoît Canet | 13e7956 | 2014-02-21 22:21:12 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 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 | |
Kevin Wolf | 10c8551 | 2016-11-07 18:00:29 +0100 | [diff] [blame] | 159 | static QuorumAIOCB *quorum_aio_get(BlockDriverState *bs, |
Benoît Canet | 13e7956 | 2014-02-21 22:21:12 +0100 | [diff] [blame] | 160 | QEMUIOVector *qiov, |
Kevin Wolf | 6847da3 | 2016-11-10 17:22:07 +0100 | [diff] [blame] | 161 | uint64_t offset, |
Max Reitz | 1b1a920 | 2018-04-21 15:29:25 +0200 | [diff] [blame] | 162 | uint64_t bytes, |
| 163 | int flags) |
Benoît Canet | 13e7956 | 2014-02-21 22:21:12 +0100 | [diff] [blame] | 164 | { |
Kevin Wolf | 10c8551 | 2016-11-07 18:00:29 +0100 | [diff] [blame] | 165 | BDRVQuorumState *s = bs->opaque; |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 166 | QuorumAIOCB *acb = g_new(QuorumAIOCB, 1); |
Benoît Canet | 13e7956 | 2014-02-21 22:21:12 +0100 | [diff] [blame] | 167 | int i; |
| 168 | |
Kevin Wolf | 7c37f94 | 2016-11-22 12:49:49 +0100 | [diff] [blame] | 169 | *acb = (QuorumAIOCB) { |
| 170 | .co = qemu_coroutine_self(), |
| 171 | .bs = bs, |
| 172 | .offset = offset, |
| 173 | .bytes = bytes, |
Max Reitz | 1b1a920 | 2018-04-21 15:29:25 +0200 | [diff] [blame] | 174 | .flags = flags, |
Kevin Wolf | 7c37f94 | 2016-11-22 12:49:49 +0100 | [diff] [blame] | 175 | .qiov = qiov, |
| 176 | .votes.compare = quorum_sha256_compare, |
| 177 | .votes.vote_list = QLIST_HEAD_INITIALIZER(acb.votes.vote_list), |
| 178 | }; |
Benoît Canet | 13e7956 | 2014-02-21 22:21:12 +0100 | [diff] [blame] | 179 | |
Kevin Wolf | 7c37f94 | 2016-11-22 12:49:49 +0100 | [diff] [blame] | 180 | acb->qcrs = g_new0(QuorumChildRequest, s->num_children); |
Benoît Canet | 13e7956 | 2014-02-21 22:21:12 +0100 | [diff] [blame] | 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 | |
Kevin Wolf | 6847da3 | 2016-11-10 17:22:07 +0100 | [diff] [blame] | 190 | static void quorum_report_bad(QuorumOpType type, uint64_t offset, |
| 191 | uint64_t bytes, char *node_name, int ret) |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 192 | { |
Wenchao Xia | fe069d9 | 2014-06-18 08:43:53 +0200 | [diff] [blame] | 193 | const char *msg = NULL; |
Kevin Wolf | 6847da3 | 2016-11-10 17:22:07 +0100 | [diff] [blame] | 194 | int64_t start_sector = offset / BDRV_SECTOR_SIZE; |
| 195 | int64_t end_sector = DIV_ROUND_UP(offset + bytes, BDRV_SECTOR_SIZE); |
| 196 | |
Benoît Canet | 0c76273 | 2014-02-22 18:43:41 +0100 | [diff] [blame] | 197 | if (ret < 0) { |
Wenchao Xia | fe069d9 | 2014-06-18 08:43:53 +0200 | [diff] [blame] | 198 | msg = strerror(-ret); |
Benoît Canet | 0c76273 | 2014-02-22 18:43:41 +0100 | [diff] [blame] | 199 | } |
Changlong Xie | 0ae053b | 2016-02-26 09:39:01 +0800 | [diff] [blame] | 200 | |
Kevin Wolf | 6847da3 | 2016-11-10 17:22:07 +0100 | [diff] [blame] | 201 | qapi_event_send_quorum_report_bad(type, !!msg, msg, node_name, start_sector, |
Peter Xu | 3ab7238 | 2018-08-15 21:37:37 +0800 | [diff] [blame] | 202 | end_sector - start_sector); |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static void quorum_report_failure(QuorumAIOCB *acb) |
| 206 | { |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 207 | const char *reference = bdrv_get_device_or_node_name(acb->bs); |
Kevin Wolf | 6847da3 | 2016-11-10 17:22:07 +0100 | [diff] [blame] | 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, |
Peter Xu | 3ab7238 | 2018-08-15 21:37:37 +0800 | [diff] [blame] | 213 | end_sector - start_sector); |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static int quorum_vote_error(QuorumAIOCB *acb); |
| 217 | |
| 218 | static bool quorum_has_too_much_io_failed(QuorumAIOCB *acb) |
| 219 | { |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 220 | BDRVQuorumState *s = acb->bs->opaque; |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 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 | |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 231 | static int read_fifo_child(QuorumAIOCB *acb); |
Liu Yuan | a9db86b | 2014-08-18 17:41:05 +0800 | [diff] [blame] | 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 | |
Paolo Bonzini | 1ba7e15 | 2016-10-05 18:35:27 +0200 | [diff] [blame] | 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; |
Kevin Wolf | 6847da3 | 2016-11-10 17:22:07 +0100 | [diff] [blame] | 250 | quorum_report_bad(type, acb->offset, acb->bytes, sacb->bs->node_name, ret); |
Paolo Bonzini | 1ba7e15 | 2016-10-05 18:35:27 +0200 | [diff] [blame] | 251 | } |
| 252 | |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 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) { |
Kevin Wolf | 6847da3 | 2016-11-10 17:22:07 +0100 | [diff] [blame] | 265 | quorum_report_bad(QUORUM_OP_TYPE_READ, acb->offset, acb->bytes, |
Changlong Xie | 0ae053b | 2016-02-26 09:39:01 +0800 | [diff] [blame] | 266 | s->children[item->index]->bs->node_name, 0); |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
Kevin Wolf | dee66e2 | 2016-11-10 16:50:16 +0100 | [diff] [blame] | 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 |
Max Reitz | 1b1a920 | 2018-04-21 15:29:25 +0200 | [diff] [blame] | 278 | * corrupted data. |
| 279 | * Mask out BDRV_REQ_WRITE_UNCHANGED because this overwrites the |
| 280 | * area with different data from the other children. */ |
Kevin Wolf | 6847da3 | 2016-11-10 17:22:07 +0100 | [diff] [blame] | 281 | bdrv_co_pwritev(s->children[co->idx], acb->offset, acb->bytes, |
Max Reitz | 1b1a920 | 2018-04-21 15:29:25 +0200 | [diff] [blame] | 282 | acb->qiov, acb->flags & ~BDRV_REQ_WRITE_UNCHANGED); |
Kevin Wolf | dee66e2 | 2016-11-10 16:50:16 +0100 | [diff] [blame] | 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, |
Benoît Canet | cf29a57 | 2014-06-11 15:24:10 +0200 | [diff] [blame] | 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 | |
Kevin Wolf | dee66e2 | 2016-11-10 16:50:16 +0100 | [diff] [blame] | 310 | /* quorum_rewrite_entry will count down this to zero */ |
Benoît Canet | cf29a57 | 2014-06-11 15:24:10 +0200 | [diff] [blame] | 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) { |
Kevin Wolf | dee66e2 | 2016-11-10 16:50:16 +0100 | [diff] [blame] | 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); |
Benoît Canet | cf29a57 | 2014-06-11 15:24:10 +0200 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
| 330 | /* return true if any rewrite is done else false */ |
| 331 | return count; |
| 332 | } |
| 333 | |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 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 | { |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 383 | QEMUIOVector *qiov = &acb->qcrs[i].qiov; |
Daniel P. Berrange | 488981a4 | 2015-07-01 18:10:35 +0100 | [diff] [blame] | 384 | size_t len = sizeof(hash->h); |
| 385 | uint8_t *data = hash->h; |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 386 | |
Daniel P. Berrange | 488981a4 | 2015-07-01 18:10:35 +0100 | [diff] [blame] | 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; |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 395 | } |
| 396 | |
Daniel P. Berrange | 488981a4 | 2015-07-01 18:10:35 +0100 | [diff] [blame] | 397 | return 0; |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 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 | |
| 440 | static void GCC_FMT_ATTR(2, 3) quorum_err(QuorumAIOCB *acb, |
| 441 | const char *fmt, ...) |
| 442 | { |
| 443 | va_list ap; |
| 444 | |
| 445 | va_start(ap, fmt); |
Kevin Wolf | 6847da3 | 2016-11-10 17:22:07 +0100 | [diff] [blame] | 446 | fprintf(stderr, "quorum: offset=%" PRIu64 " bytes=%" PRIu64 " ", |
| 447 | acb->offset, acb->bytes); |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 448 | vfprintf(stderr, fmt, ap); |
| 449 | fprintf(stderr, "\n"); |
| 450 | va_end(ap); |
| 451 | exit(1); |
| 452 | } |
| 453 | |
| 454 | static bool quorum_compare(QuorumAIOCB *acb, |
| 455 | QEMUIOVector *a, |
| 456 | QEMUIOVector *b) |
| 457 | { |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 458 | BDRVQuorumState *s = acb->bs->opaque; |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 459 | ssize_t offset; |
| 460 | |
| 461 | /* This driver will replace blkverify in this particular case */ |
| 462 | if (s->is_blkverify) { |
| 463 | offset = qemu_iovec_compare(a, b); |
| 464 | if (offset != -1) { |
Kevin Wolf | 6847da3 | 2016-11-10 17:22:07 +0100 | [diff] [blame] | 465 | quorum_err(acb, "contents mismatch at offset %" PRIu64, |
| 466 | acb->offset + offset); |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 467 | } |
| 468 | return true; |
| 469 | } |
| 470 | |
| 471 | return quorum_iovec_compare(a, b); |
| 472 | } |
| 473 | |
| 474 | /* Do a vote to get the error code */ |
| 475 | static int quorum_vote_error(QuorumAIOCB *acb) |
| 476 | { |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 477 | BDRVQuorumState *s = acb->bs->opaque; |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 478 | QuorumVoteVersion *winner = NULL; |
| 479 | QuorumVotes error_votes; |
| 480 | QuorumVoteValue result_value; |
| 481 | int i, ret = 0; |
| 482 | bool error = false; |
| 483 | |
| 484 | QLIST_INIT(&error_votes.vote_list); |
| 485 | error_votes.compare = quorum_64bits_compare; |
| 486 | |
| 487 | for (i = 0; i < s->num_children; i++) { |
| 488 | ret = acb->qcrs[i].ret; |
| 489 | if (ret) { |
| 490 | error = true; |
| 491 | result_value.l = ret; |
| 492 | quorum_count_vote(&error_votes, &result_value, i); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | if (error) { |
| 497 | winner = quorum_get_vote_winner(&error_votes); |
| 498 | ret = winner->value.l; |
| 499 | } |
| 500 | |
| 501 | quorum_free_vote_list(&error_votes); |
| 502 | |
| 503 | return ret; |
| 504 | } |
| 505 | |
Kevin Wolf | 7cd9b39 | 2016-11-10 16:13:15 +0100 | [diff] [blame] | 506 | static void quorum_vote(QuorumAIOCB *acb) |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 507 | { |
| 508 | bool quorum = true; |
| 509 | int i, j, ret; |
| 510 | QuorumVoteValue hash; |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 511 | BDRVQuorumState *s = acb->bs->opaque; |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 512 | QuorumVoteVersion *winner; |
| 513 | |
| 514 | if (quorum_has_too_much_io_failed(acb)) { |
Kevin Wolf | 7cd9b39 | 2016-11-10 16:13:15 +0100 | [diff] [blame] | 515 | return; |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* get the index of the first successful read */ |
| 519 | for (i = 0; i < s->num_children; i++) { |
| 520 | if (!acb->qcrs[i].ret) { |
| 521 | break; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | assert(i < s->num_children); |
| 526 | |
| 527 | /* compare this read with all other successful reads stopping at quorum |
| 528 | * failure |
| 529 | */ |
| 530 | for (j = i + 1; j < s->num_children; j++) { |
| 531 | if (acb->qcrs[j].ret) { |
| 532 | continue; |
| 533 | } |
| 534 | quorum = quorum_compare(acb, &acb->qcrs[i].qiov, &acb->qcrs[j].qiov); |
| 535 | if (!quorum) { |
| 536 | break; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | /* Every successful read agrees */ |
| 541 | if (quorum) { |
| 542 | quorum_copy_qiov(acb->qiov, &acb->qcrs[i].qiov); |
Kevin Wolf | 7cd9b39 | 2016-11-10 16:13:15 +0100 | [diff] [blame] | 543 | return; |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /* compute hashes for each successful read, also store indexes */ |
| 547 | for (i = 0; i < s->num_children; i++) { |
| 548 | if (acb->qcrs[i].ret) { |
| 549 | continue; |
| 550 | } |
| 551 | ret = quorum_compute_hash(acb, i, &hash); |
| 552 | /* if ever the hash computation failed */ |
| 553 | if (ret < 0) { |
| 554 | acb->vote_ret = ret; |
| 555 | goto free_exit; |
| 556 | } |
| 557 | quorum_count_vote(&acb->votes, &hash, i); |
| 558 | } |
| 559 | |
| 560 | /* vote to select the most represented version */ |
| 561 | winner = quorum_get_vote_winner(&acb->votes); |
| 562 | |
| 563 | /* if the winner count is smaller than threshold the read fails */ |
| 564 | if (winner->vote_count < s->threshold) { |
| 565 | quorum_report_failure(acb); |
| 566 | acb->vote_ret = -EIO; |
| 567 | goto free_exit; |
| 568 | } |
| 569 | |
| 570 | /* we have a winner: copy it */ |
| 571 | quorum_copy_qiov(acb->qiov, &acb->qcrs[winner->index].qiov); |
| 572 | |
| 573 | /* some versions are bad print them */ |
| 574 | quorum_report_bad_versions(s, acb, &winner->value); |
| 575 | |
Benoît Canet | cf29a57 | 2014-06-11 15:24:10 +0200 | [diff] [blame] | 576 | /* corruption correction is enabled */ |
| 577 | if (s->rewrite_corrupted) { |
Kevin Wolf | dee66e2 | 2016-11-10 16:50:16 +0100 | [diff] [blame] | 578 | quorum_rewrite_bad_versions(acb, &winner->value); |
Benoît Canet | cf29a57 | 2014-06-11 15:24:10 +0200 | [diff] [blame] | 579 | } |
| 580 | |
Benoît Canet | 95c6bff | 2014-02-21 22:21:15 +0100 | [diff] [blame] | 581 | free_exit: |
| 582 | /* free lists */ |
| 583 | quorum_free_vote_list(&acb->votes); |
| 584 | } |
| 585 | |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 586 | static void read_quorum_children_entry(void *opaque) |
Liu Yuan | a9db86b | 2014-08-18 17:41:05 +0800 | [diff] [blame] | 587 | { |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 588 | QuorumCo *co = opaque; |
| 589 | QuorumAIOCB *acb = co->acb; |
| 590 | BDRVQuorumState *s = acb->bs->opaque; |
| 591 | int i = co->idx; |
Kevin Wolf | 7cd9b39 | 2016-11-10 16:13:15 +0100 | [diff] [blame] | 592 | QuorumChildRequest *sacb = &acb->qcrs[i]; |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 593 | |
Kevin Wolf | 7cd9b39 | 2016-11-10 16:13:15 +0100 | [diff] [blame] | 594 | sacb->bs = s->children[i]->bs; |
Kevin Wolf | 6847da3 | 2016-11-10 17:22:07 +0100 | [diff] [blame] | 595 | sacb->ret = bdrv_co_preadv(s->children[i], acb->offset, acb->bytes, |
Kevin Wolf | 7cd9b39 | 2016-11-10 16:13:15 +0100 | [diff] [blame] | 596 | &acb->qcrs[i].qiov, 0); |
| 597 | |
| 598 | if (sacb->ret == 0) { |
| 599 | acb->success_count++; |
| 600 | } else { |
| 601 | quorum_report_bad_acb(sacb, sacb->ret); |
| 602 | } |
| 603 | |
| 604 | acb->count++; |
| 605 | assert(acb->count <= s->num_children); |
| 606 | assert(acb->success_count <= s->num_children); |
| 607 | |
| 608 | /* Wake up the caller after the last read */ |
| 609 | if (acb->count == s->num_children) { |
| 610 | qemu_coroutine_enter_if_inactive(acb->co); |
| 611 | } |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | static int read_quorum_children(QuorumAIOCB *acb) |
| 615 | { |
| 616 | BDRVQuorumState *s = acb->bs->opaque; |
Laurent Vivier | 4a4ff4c | 2018-03-23 15:32:02 +0100 | [diff] [blame] | 617 | int i; |
Liu Yuan | a9db86b | 2014-08-18 17:41:05 +0800 | [diff] [blame] | 618 | |
Paolo Bonzini | 86ec252 | 2016-10-05 18:35:26 +0200 | [diff] [blame] | 619 | acb->children_read = s->num_children; |
Liu Yuan | a9db86b | 2014-08-18 17:41:05 +0800 | [diff] [blame] | 620 | for (i = 0; i < s->num_children; i++) { |
Kevin Wolf | 0bd6e91 | 2015-06-16 11:29:22 +0200 | [diff] [blame] | 621 | acb->qcrs[i].buf = qemu_blockalign(s->children[i]->bs, acb->qiov->size); |
Liu Yuan | a9db86b | 2014-08-18 17:41:05 +0800 | [diff] [blame] | 622 | qemu_iovec_init(&acb->qcrs[i].qiov, acb->qiov->niov); |
| 623 | qemu_iovec_clone(&acb->qcrs[i].qiov, acb->qiov, acb->qcrs[i].buf); |
| 624 | } |
| 625 | |
| 626 | for (i = 0; i < s->num_children; i++) { |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 627 | Coroutine *co; |
| 628 | QuorumCo data = { |
| 629 | .acb = acb, |
| 630 | .idx = i, |
| 631 | }; |
| 632 | |
| 633 | co = qemu_coroutine_create(read_quorum_children_entry, &data); |
| 634 | qemu_coroutine_enter(co); |
Liu Yuan | a9db86b | 2014-08-18 17:41:05 +0800 | [diff] [blame] | 635 | } |
| 636 | |
Kevin Wolf | 7cd9b39 | 2016-11-10 16:13:15 +0100 | [diff] [blame] | 637 | while (acb->count < s->num_children) { |
| 638 | qemu_coroutine_yield(); |
| 639 | } |
| 640 | |
| 641 | /* Do the vote on read */ |
| 642 | quorum_vote(acb); |
| 643 | for (i = 0; i < s->num_children; i++) { |
| 644 | qemu_vfree(acb->qcrs[i].buf); |
| 645 | qemu_iovec_destroy(&acb->qcrs[i].qiov); |
| 646 | } |
| 647 | |
| 648 | while (acb->rewrite_count) { |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 649 | qemu_coroutine_yield(); |
| 650 | } |
| 651 | |
Laurent Vivier | 4a4ff4c | 2018-03-23 15:32:02 +0100 | [diff] [blame] | 652 | return acb->vote_ret; |
Liu Yuan | a9db86b | 2014-08-18 17:41:05 +0800 | [diff] [blame] | 653 | } |
| 654 | |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 655 | static int read_fifo_child(QuorumAIOCB *acb) |
Liu Yuan | a9db86b | 2014-08-18 17:41:05 +0800 | [diff] [blame] | 656 | { |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 657 | BDRVQuorumState *s = acb->bs->opaque; |
Kevin Wolf | a7e1590 | 2016-11-10 17:40:34 +0100 | [diff] [blame] | 658 | int n, ret; |
Liu Yuan | a9db86b | 2014-08-18 17:41:05 +0800 | [diff] [blame] | 659 | |
Kevin Wolf | a7e1590 | 2016-11-10 17:40:34 +0100 | [diff] [blame] | 660 | /* We try to read the next child in FIFO order if we failed to read */ |
| 661 | do { |
| 662 | n = acb->children_read++; |
| 663 | acb->qcrs[n].bs = s->children[n]->bs; |
| 664 | ret = bdrv_co_preadv(s->children[n], acb->offset, acb->bytes, |
| 665 | acb->qiov, 0); |
| 666 | if (ret < 0) { |
| 667 | quorum_report_bad_acb(&acb->qcrs[n], ret); |
| 668 | } |
| 669 | } while (ret < 0 && acb->children_read < s->num_children); |
| 670 | |
| 671 | /* FIXME: rewrite failed children if acb->children_read > 1? */ |
Liu Yuan | a9db86b | 2014-08-18 17:41:05 +0800 | [diff] [blame] | 672 | |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 673 | return ret; |
Liu Yuan | a9db86b | 2014-08-18 17:41:05 +0800 | [diff] [blame] | 674 | } |
| 675 | |
Kevin Wolf | 6847da3 | 2016-11-10 17:22:07 +0100 | [diff] [blame] | 676 | static int quorum_co_preadv(BlockDriverState *bs, uint64_t offset, |
| 677 | uint64_t bytes, QEMUIOVector *qiov, int flags) |
Benoît Canet | 7db6982 | 2014-02-21 22:21:14 +0100 | [diff] [blame] | 678 | { |
| 679 | BDRVQuorumState *s = bs->opaque; |
Max Reitz | 1b1a920 | 2018-04-21 15:29:25 +0200 | [diff] [blame] | 680 | QuorumAIOCB *acb = quorum_aio_get(bs, qiov, offset, bytes, flags); |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 681 | int ret; |
| 682 | |
Benoît Canet | 7db6982 | 2014-02-21 22:21:14 +0100 | [diff] [blame] | 683 | acb->is_read = true; |
Paolo Bonzini | 86ec252 | 2016-10-05 18:35:26 +0200 | [diff] [blame] | 684 | acb->children_read = 0; |
Benoît Canet | 7db6982 | 2014-02-21 22:21:14 +0100 | [diff] [blame] | 685 | |
Liu Yuan | a9db86b | 2014-08-18 17:41:05 +0800 | [diff] [blame] | 686 | if (s->read_pattern == QUORUM_READ_PATTERN_QUORUM) { |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 687 | ret = read_quorum_children(acb); |
| 688 | } else { |
| 689 | ret = read_fifo_child(acb); |
Benoît Canet | 7db6982 | 2014-02-21 22:21:14 +0100 | [diff] [blame] | 690 | } |
Kevin Wolf | 0f31977 | 2016-11-10 14:24:27 +0100 | [diff] [blame] | 691 | quorum_aio_finalize(acb); |
| 692 | |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 693 | return ret; |
Benoît Canet | 7db6982 | 2014-02-21 22:21:14 +0100 | [diff] [blame] | 694 | } |
| 695 | |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 696 | static void write_quorum_entry(void *opaque) |
| 697 | { |
| 698 | QuorumCo *co = opaque; |
| 699 | QuorumAIOCB *acb = co->acb; |
| 700 | BDRVQuorumState *s = acb->bs->opaque; |
| 701 | int i = co->idx; |
Kevin Wolf | 7cd9b39 | 2016-11-10 16:13:15 +0100 | [diff] [blame] | 702 | QuorumChildRequest *sacb = &acb->qcrs[i]; |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 703 | |
Kevin Wolf | 7cd9b39 | 2016-11-10 16:13:15 +0100 | [diff] [blame] | 704 | sacb->bs = s->children[i]->bs; |
Kevin Wolf | 6847da3 | 2016-11-10 17:22:07 +0100 | [diff] [blame] | 705 | sacb->ret = bdrv_co_pwritev(s->children[i], acb->offset, acb->bytes, |
Max Reitz | 1b1a920 | 2018-04-21 15:29:25 +0200 | [diff] [blame] | 706 | acb->qiov, acb->flags); |
Kevin Wolf | 7cd9b39 | 2016-11-10 16:13:15 +0100 | [diff] [blame] | 707 | if (sacb->ret == 0) { |
| 708 | acb->success_count++; |
| 709 | } else { |
| 710 | quorum_report_bad_acb(sacb, sacb->ret); |
| 711 | } |
| 712 | acb->count++; |
| 713 | assert(acb->count <= s->num_children); |
| 714 | assert(acb->success_count <= s->num_children); |
| 715 | |
| 716 | /* Wake up the caller after the last write */ |
| 717 | if (acb->count == s->num_children) { |
| 718 | qemu_coroutine_enter_if_inactive(acb->co); |
| 719 | } |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 720 | } |
| 721 | |
Kevin Wolf | 6847da3 | 2016-11-10 17:22:07 +0100 | [diff] [blame] | 722 | static int quorum_co_pwritev(BlockDriverState *bs, uint64_t offset, |
| 723 | uint64_t bytes, QEMUIOVector *qiov, int flags) |
Benoît Canet | 13e7956 | 2014-02-21 22:21:12 +0100 | [diff] [blame] | 724 | { |
| 725 | BDRVQuorumState *s = bs->opaque; |
Max Reitz | 1b1a920 | 2018-04-21 15:29:25 +0200 | [diff] [blame] | 726 | QuorumAIOCB *acb = quorum_aio_get(bs, qiov, offset, bytes, flags); |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 727 | int i, ret; |
Benoît Canet | 13e7956 | 2014-02-21 22:21:12 +0100 | [diff] [blame] | 728 | |
| 729 | for (i = 0; i < s->num_children; i++) { |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 730 | Coroutine *co; |
| 731 | QuorumCo data = { |
| 732 | .acb = acb, |
| 733 | .idx = i, |
| 734 | }; |
| 735 | |
| 736 | co = qemu_coroutine_create(write_quorum_entry, &data); |
| 737 | qemu_coroutine_enter(co); |
Benoît Canet | 13e7956 | 2014-02-21 22:21:12 +0100 | [diff] [blame] | 738 | } |
| 739 | |
Kevin Wolf | 7cd9b39 | 2016-11-10 16:13:15 +0100 | [diff] [blame] | 740 | while (acb->count < s->num_children) { |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 741 | qemu_coroutine_yield(); |
| 742 | } |
| 743 | |
Kevin Wolf | 7cd9b39 | 2016-11-10 16:13:15 +0100 | [diff] [blame] | 744 | quorum_has_too_much_io_failed(acb); |
| 745 | |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 746 | ret = acb->vote_ret; |
Kevin Wolf | 0f31977 | 2016-11-10 14:24:27 +0100 | [diff] [blame] | 747 | quorum_aio_finalize(acb); |
Kevin Wolf | ce15dc0 | 2016-11-08 11:10:14 +0100 | [diff] [blame] | 748 | |
| 749 | return ret; |
Benoît Canet | 13e7956 | 2014-02-21 22:21:12 +0100 | [diff] [blame] | 750 | } |
| 751 | |
Benoît Canet | d55dee2 | 2014-02-21 22:21:16 +0100 | [diff] [blame] | 752 | static int64_t quorum_getlength(BlockDriverState *bs) |
| 753 | { |
| 754 | BDRVQuorumState *s = bs->opaque; |
| 755 | int64_t result; |
| 756 | int i; |
| 757 | |
| 758 | /* check that all file have the same length */ |
Kevin Wolf | 0bd6e91 | 2015-06-16 11:29:22 +0200 | [diff] [blame] | 759 | result = bdrv_getlength(s->children[0]->bs); |
Benoît Canet | d55dee2 | 2014-02-21 22:21:16 +0100 | [diff] [blame] | 760 | if (result < 0) { |
| 761 | return result; |
| 762 | } |
| 763 | for (i = 1; i < s->num_children; i++) { |
Kevin Wolf | 0bd6e91 | 2015-06-16 11:29:22 +0200 | [diff] [blame] | 764 | int64_t value = bdrv_getlength(s->children[i]->bs); |
Benoît Canet | d55dee2 | 2014-02-21 22:21:16 +0100 | [diff] [blame] | 765 | if (value < 0) { |
| 766 | return value; |
| 767 | } |
| 768 | if (value != result) { |
| 769 | return -EIO; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | return result; |
| 774 | } |
| 775 | |
Benoît Canet | 1c508d1 | 2014-02-21 22:21:18 +0100 | [diff] [blame] | 776 | static coroutine_fn int quorum_co_flush(BlockDriverState *bs) |
| 777 | { |
| 778 | BDRVQuorumState *s = bs->opaque; |
| 779 | QuorumVoteVersion *winner = NULL; |
| 780 | QuorumVotes error_votes; |
| 781 | QuorumVoteValue result_value; |
| 782 | int i; |
| 783 | int result = 0; |
Changlong Xie | 924e8a2 | 2016-02-26 09:39:02 +0800 | [diff] [blame] | 784 | int success_count = 0; |
Benoît Canet | 1c508d1 | 2014-02-21 22:21:18 +0100 | [diff] [blame] | 785 | |
| 786 | QLIST_INIT(&error_votes.vote_list); |
| 787 | error_votes.compare = quorum_64bits_compare; |
| 788 | |
| 789 | for (i = 0; i < s->num_children; i++) { |
Kevin Wolf | 0bd6e91 | 2015-06-16 11:29:22 +0200 | [diff] [blame] | 790 | result = bdrv_co_flush(s->children[i]->bs); |
Changlong Xie | 924e8a2 | 2016-02-26 09:39:02 +0800 | [diff] [blame] | 791 | if (result) { |
Alberto Garcia | 795be06 | 2017-08-07 15:36:58 +0300 | [diff] [blame] | 792 | quorum_report_bad(QUORUM_OP_TYPE_FLUSH, 0, 0, |
Changlong Xie | 924e8a2 | 2016-02-26 09:39:02 +0800 | [diff] [blame] | 793 | s->children[i]->bs->node_name, result); |
| 794 | result_value.l = result; |
| 795 | quorum_count_vote(&error_votes, &result_value, i); |
| 796 | } else { |
| 797 | success_count++; |
| 798 | } |
Benoît Canet | 1c508d1 | 2014-02-21 22:21:18 +0100 | [diff] [blame] | 799 | } |
| 800 | |
Changlong Xie | 924e8a2 | 2016-02-26 09:39:02 +0800 | [diff] [blame] | 801 | if (success_count >= s->threshold) { |
| 802 | result = 0; |
| 803 | } else { |
| 804 | winner = quorum_get_vote_winner(&error_votes); |
| 805 | result = winner->value.l; |
| 806 | } |
Benoît Canet | 1c508d1 | 2014-02-21 22:21:18 +0100 | [diff] [blame] | 807 | quorum_free_vote_list(&error_votes); |
| 808 | |
| 809 | return result; |
| 810 | } |
| 811 | |
Benoît Canet | 98a7a38 | 2014-02-21 22:21:19 +0100 | [diff] [blame] | 812 | static bool quorum_recurse_is_first_non_filter(BlockDriverState *bs, |
| 813 | BlockDriverState *candidate) |
| 814 | { |
| 815 | BDRVQuorumState *s = bs->opaque; |
| 816 | int i; |
| 817 | |
| 818 | for (i = 0; i < s->num_children; i++) { |
Kevin Wolf | 0bd6e91 | 2015-06-16 11:29:22 +0200 | [diff] [blame] | 819 | bool perm = bdrv_recurse_is_first_non_filter(s->children[i]->bs, |
Benoît Canet | 98a7a38 | 2014-02-21 22:21:19 +0100 | [diff] [blame] | 820 | candidate); |
| 821 | if (perm) { |
| 822 | return true; |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | return false; |
| 827 | } |
| 828 | |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 829 | static int quorum_valid_threshold(int threshold, int num_children, Error **errp) |
| 830 | { |
| 831 | |
| 832 | if (threshold < 1) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 833 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 834 | "vote-threshold", "value >= 1"); |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 835 | return -ERANGE; |
| 836 | } |
| 837 | |
| 838 | if (threshold > num_children) { |
| 839 | error_setg(errp, "threshold may not exceed children count"); |
| 840 | return -ERANGE; |
| 841 | } |
| 842 | |
| 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | static QemuOptsList quorum_runtime_opts = { |
| 847 | .name = "quorum", |
| 848 | .head = QTAILQ_HEAD_INITIALIZER(quorum_runtime_opts.head), |
| 849 | .desc = { |
| 850 | { |
| 851 | .name = QUORUM_OPT_VOTE_THRESHOLD, |
| 852 | .type = QEMU_OPT_NUMBER, |
| 853 | .help = "The number of vote needed for reaching quorum", |
| 854 | }, |
| 855 | { |
| 856 | .name = QUORUM_OPT_BLKVERIFY, |
| 857 | .type = QEMU_OPT_BOOL, |
| 858 | .help = "Trigger block verify mode if set", |
| 859 | }, |
Benoît Canet | cf29a57 | 2014-06-11 15:24:10 +0200 | [diff] [blame] | 860 | { |
| 861 | .name = QUORUM_OPT_REWRITE, |
| 862 | .type = QEMU_OPT_BOOL, |
| 863 | .help = "Rewrite corrupted block on read quorum", |
| 864 | }, |
Liu Yuan | a9db86b | 2014-08-18 17:41:05 +0800 | [diff] [blame] | 865 | { |
| 866 | .name = QUORUM_OPT_READ_PATTERN, |
| 867 | .type = QEMU_OPT_STRING, |
| 868 | .help = "Allowed pattern: quorum, fifo. Quorum is default", |
| 869 | }, |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 870 | { /* end of list */ } |
| 871 | }, |
| 872 | }; |
| 873 | |
| 874 | static int quorum_open(BlockDriverState *bs, QDict *options, int flags, |
| 875 | Error **errp) |
| 876 | { |
| 877 | BDRVQuorumState *s = bs->opaque; |
| 878 | Error *local_err = NULL; |
Fam Zheng | 8df3abf | 2014-08-28 13:56:12 +0800 | [diff] [blame] | 879 | QemuOpts *opts = NULL; |
Marc-André Lureau | 8d5fb19 | 2017-08-24 10:46:03 +0200 | [diff] [blame] | 880 | const char *pattern_str; |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 881 | bool *opened; |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 882 | int i; |
| 883 | int ret = 0; |
| 884 | |
| 885 | qdict_flatten(options); |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 886 | |
Kevin Wolf | ea6828d | 2015-01-21 18:49:28 +0100 | [diff] [blame] | 887 | /* count how many different children are present */ |
| 888 | s->num_children = qdict_array_entries(options, "children."); |
| 889 | if (s->num_children < 0) { |
| 890 | error_setg(&local_err, "Option children is not a valid array"); |
Max Reitz | 8a87f3d | 2014-02-21 22:30:37 +0100 | [diff] [blame] | 891 | ret = -EINVAL; |
| 892 | goto exit; |
| 893 | } |
Wen Congyang | 98292c6 | 2016-05-10 15:36:38 +0800 | [diff] [blame] | 894 | if (s->num_children < 1) { |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 895 | error_setg(&local_err, |
Wen Congyang | 98292c6 | 2016-05-10 15:36:38 +0800 | [diff] [blame] | 896 | "Number of provided children must be 1 or more"); |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 897 | ret = -EINVAL; |
| 898 | goto exit; |
| 899 | } |
| 900 | |
| 901 | opts = qemu_opts_create(&quorum_runtime_opts, NULL, 0, &error_abort); |
| 902 | qemu_opts_absorb_qdict(opts, options, &local_err); |
Markus Armbruster | 0fb6395 | 2014-04-25 16:50:31 +0200 | [diff] [blame] | 903 | if (local_err) { |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 904 | ret = -EINVAL; |
| 905 | goto exit; |
| 906 | } |
| 907 | |
| 908 | s->threshold = qemu_opt_get_number(opts, QUORUM_OPT_VOTE_THRESHOLD, 0); |
Wen Congyang | 834cb2a | 2015-07-03 14:45:06 +0800 | [diff] [blame] | 909 | /* and validate it against s->num_children */ |
| 910 | ret = quorum_valid_threshold(s->threshold, s->num_children, &local_err); |
| 911 | if (ret < 0) { |
| 912 | goto exit; |
| 913 | } |
| 914 | |
Marc-André Lureau | 8d5fb19 | 2017-08-24 10:46:03 +0200 | [diff] [blame] | 915 | pattern_str = qemu_opt_get(opts, QUORUM_OPT_READ_PATTERN); |
| 916 | if (!pattern_str) { |
| 917 | ret = QUORUM_READ_PATTERN_QUORUM; |
| 918 | } else { |
Marc-André Lureau | f7abe0e | 2017-08-24 10:46:10 +0200 | [diff] [blame] | 919 | ret = qapi_enum_parse(&QuorumReadPattern_lookup, pattern_str, |
Marc-André Lureau | 8d5fb19 | 2017-08-24 10:46:03 +0200 | [diff] [blame] | 920 | -EINVAL, NULL); |
| 921 | } |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 922 | if (ret < 0) { |
Liu Yuan | a9db86b | 2014-08-18 17:41:05 +0800 | [diff] [blame] | 923 | error_setg(&local_err, "Please set read-pattern as fifo or quorum"); |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 924 | goto exit; |
| 925 | } |
Liu Yuan | a9db86b | 2014-08-18 17:41:05 +0800 | [diff] [blame] | 926 | s->read_pattern = ret; |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 927 | |
Liu Yuan | a9db86b | 2014-08-18 17:41:05 +0800 | [diff] [blame] | 928 | if (s->read_pattern == QUORUM_READ_PATTERN_QUORUM) { |
Liu Yuan | a9db86b | 2014-08-18 17:41:05 +0800 | [diff] [blame] | 929 | /* is the driver in blkverify mode */ |
| 930 | if (qemu_opt_get_bool(opts, QUORUM_OPT_BLKVERIFY, false) && |
| 931 | s->num_children == 2 && s->threshold == 2) { |
| 932 | s->is_blkverify = true; |
| 933 | } else if (qemu_opt_get_bool(opts, QUORUM_OPT_BLKVERIFY, false)) { |
| 934 | fprintf(stderr, "blkverify mode is set by setting blkverify=on " |
| 935 | "and using two files with vote_threshold=2\n"); |
| 936 | } |
| 937 | |
| 938 | s->rewrite_corrupted = qemu_opt_get_bool(opts, QUORUM_OPT_REWRITE, |
| 939 | false); |
| 940 | if (s->rewrite_corrupted && s->is_blkverify) { |
| 941 | error_setg(&local_err, |
| 942 | "rewrite-corrupted=on cannot be used with blkverify=on"); |
| 943 | ret = -EINVAL; |
| 944 | goto exit; |
| 945 | } |
Benoît Canet | cf29a57 | 2014-06-11 15:24:10 +0200 | [diff] [blame] | 946 | } |
| 947 | |
Kevin Wolf | 0bd6e91 | 2015-06-16 11:29:22 +0200 | [diff] [blame] | 948 | /* allocate the children array */ |
| 949 | s->children = g_new0(BdrvChild *, s->num_children); |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 950 | opened = g_new0(bool, s->num_children); |
| 951 | |
Kevin Wolf | ea6828d | 2015-01-21 18:49:28 +0100 | [diff] [blame] | 952 | for (i = 0; i < s->num_children; i++) { |
| 953 | char indexstr[32]; |
| 954 | ret = snprintf(indexstr, 32, "children.%d", i); |
| 955 | assert(ret < 32); |
Max Reitz | 8a87f3d | 2014-02-21 22:30:37 +0100 | [diff] [blame] | 956 | |
Kevin Wolf | 0bd6e91 | 2015-06-16 11:29:22 +0200 | [diff] [blame] | 957 | s->children[i] = bdrv_open_child(NULL, options, indexstr, bs, |
| 958 | &child_format, false, &local_err); |
| 959 | if (local_err) { |
| 960 | ret = -EINVAL; |
Max Reitz | 8a87f3d | 2014-02-21 22:30:37 +0100 | [diff] [blame] | 961 | goto close_exit; |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 962 | } |
Kevin Wolf | ea6828d | 2015-01-21 18:49:28 +0100 | [diff] [blame] | 963 | |
Max Reitz | 8a87f3d | 2014-02-21 22:30:37 +0100 | [diff] [blame] | 964 | opened[i] = true; |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 965 | } |
Wen Congyang | 98292c6 | 2016-05-10 15:36:38 +0800 | [diff] [blame] | 966 | s->next_child_index = s->num_children; |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 967 | |
Max Reitz | 1b1a920 | 2018-04-21 15:29:25 +0200 | [diff] [blame] | 968 | bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED; |
| 969 | |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 970 | g_free(opened); |
| 971 | goto exit; |
| 972 | |
| 973 | close_exit: |
| 974 | /* cleanup on error */ |
| 975 | for (i = 0; i < s->num_children; i++) { |
| 976 | if (!opened[i]) { |
| 977 | continue; |
| 978 | } |
Kevin Wolf | 0bd6e91 | 2015-06-16 11:29:22 +0200 | [diff] [blame] | 979 | bdrv_unref_child(bs, s->children[i]); |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 980 | } |
Kevin Wolf | 0bd6e91 | 2015-06-16 11:29:22 +0200 | [diff] [blame] | 981 | g_free(s->children); |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 982 | g_free(opened); |
| 983 | exit: |
Fam Zheng | 8df3abf | 2014-08-28 13:56:12 +0800 | [diff] [blame] | 984 | qemu_opts_del(opts); |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 985 | /* propagate error */ |
Eduardo Habkost | 621ff94 | 2016-06-13 18:57:56 -0300 | [diff] [blame] | 986 | error_propagate(errp, local_err); |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 987 | return ret; |
| 988 | } |
| 989 | |
| 990 | static void quorum_close(BlockDriverState *bs) |
| 991 | { |
| 992 | BDRVQuorumState *s = bs->opaque; |
| 993 | int i; |
| 994 | |
| 995 | for (i = 0; i < s->num_children; i++) { |
Kevin Wolf | 0bd6e91 | 2015-06-16 11:29:22 +0200 | [diff] [blame] | 996 | bdrv_unref_child(bs, s->children[i]); |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 997 | } |
| 998 | |
Kevin Wolf | 0bd6e91 | 2015-06-16 11:29:22 +0200 | [diff] [blame] | 999 | g_free(s->children); |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 1000 | } |
| 1001 | |
Wen Congyang | 98292c6 | 2016-05-10 15:36:38 +0800 | [diff] [blame] | 1002 | static void quorum_add_child(BlockDriverState *bs, BlockDriverState *child_bs, |
| 1003 | Error **errp) |
| 1004 | { |
| 1005 | BDRVQuorumState *s = bs->opaque; |
| 1006 | BdrvChild *child; |
| 1007 | char indexstr[32]; |
| 1008 | int ret; |
| 1009 | |
| 1010 | assert(s->num_children <= INT_MAX / sizeof(BdrvChild *)); |
| 1011 | if (s->num_children == INT_MAX / sizeof(BdrvChild *) || |
| 1012 | s->next_child_index == UINT_MAX) { |
| 1013 | error_setg(errp, "Too many children"); |
| 1014 | return; |
| 1015 | } |
| 1016 | |
| 1017 | ret = snprintf(indexstr, 32, "children.%u", s->next_child_index); |
| 1018 | if (ret < 0 || ret >= 32) { |
| 1019 | error_setg(errp, "cannot generate child name"); |
| 1020 | return; |
| 1021 | } |
| 1022 | s->next_child_index++; |
| 1023 | |
| 1024 | bdrv_drained_begin(bs); |
| 1025 | |
| 1026 | /* We can safely add the child now */ |
| 1027 | bdrv_ref(child_bs); |
Kevin Wolf | 8b2ff52 | 2016-12-20 22:21:17 +0100 | [diff] [blame] | 1028 | |
| 1029 | child = bdrv_attach_child(bs, child_bs, indexstr, &child_format, errp); |
| 1030 | if (child == NULL) { |
| 1031 | s->next_child_index--; |
| 1032 | bdrv_unref(child_bs); |
| 1033 | goto out; |
| 1034 | } |
Wen Congyang | 98292c6 | 2016-05-10 15:36:38 +0800 | [diff] [blame] | 1035 | s->children = g_renew(BdrvChild *, s->children, s->num_children + 1); |
| 1036 | s->children[s->num_children++] = child; |
| 1037 | |
Kevin Wolf | 8b2ff52 | 2016-12-20 22:21:17 +0100 | [diff] [blame] | 1038 | out: |
Wen Congyang | 98292c6 | 2016-05-10 15:36:38 +0800 | [diff] [blame] | 1039 | bdrv_drained_end(bs); |
| 1040 | } |
| 1041 | |
| 1042 | static void quorum_del_child(BlockDriverState *bs, BdrvChild *child, |
| 1043 | Error **errp) |
| 1044 | { |
| 1045 | BDRVQuorumState *s = bs->opaque; |
| 1046 | int i; |
| 1047 | |
| 1048 | for (i = 0; i < s->num_children; i++) { |
| 1049 | if (s->children[i] == child) { |
| 1050 | break; |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | /* we have checked it in bdrv_del_child() */ |
| 1055 | assert(i < s->num_children); |
| 1056 | |
| 1057 | if (s->num_children <= s->threshold) { |
| 1058 | error_setg(errp, |
| 1059 | "The number of children cannot be lower than the vote threshold %d", |
| 1060 | s->threshold); |
| 1061 | return; |
| 1062 | } |
| 1063 | |
| 1064 | bdrv_drained_begin(bs); |
| 1065 | |
| 1066 | /* We can safely remove this child now */ |
| 1067 | memmove(&s->children[i], &s->children[i + 1], |
| 1068 | (s->num_children - i - 1) * sizeof(BdrvChild *)); |
| 1069 | s->children = g_renew(BdrvChild *, s->children, --s->num_children); |
| 1070 | bdrv_unref_child(bs, child); |
| 1071 | |
| 1072 | bdrv_drained_end(bs); |
| 1073 | } |
| 1074 | |
Kevin Wolf | 4cdd01d | 2015-04-27 13:50:54 +0200 | [diff] [blame] | 1075 | static void quorum_refresh_filename(BlockDriverState *bs, QDict *options) |
Max Reitz | fafcfe2 | 2014-07-18 20:25:00 +0200 | [diff] [blame] | 1076 | { |
| 1077 | BDRVQuorumState *s = bs->opaque; |
| 1078 | QDict *opts; |
| 1079 | QList *children; |
| 1080 | int i; |
| 1081 | |
| 1082 | for (i = 0; i < s->num_children; i++) { |
Kevin Wolf | 0bd6e91 | 2015-06-16 11:29:22 +0200 | [diff] [blame] | 1083 | bdrv_refresh_filename(s->children[i]->bs); |
| 1084 | if (!s->children[i]->bs->full_open_options) { |
Max Reitz | fafcfe2 | 2014-07-18 20:25:00 +0200 | [diff] [blame] | 1085 | return; |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | children = qlist_new(); |
| 1090 | for (i = 0; i < s->num_children; i++) { |
Marc-André Lureau | f5a74a5 | 2018-04-19 17:01:44 +0200 | [diff] [blame] | 1091 | qlist_append(children, |
| 1092 | qobject_ref(s->children[i]->bs->full_open_options)); |
Max Reitz | fafcfe2 | 2014-07-18 20:25:00 +0200 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | opts = qdict_new(); |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 1096 | qdict_put_str(opts, "driver", "quorum"); |
| 1097 | qdict_put_int(opts, QUORUM_OPT_VOTE_THRESHOLD, s->threshold); |
| 1098 | qdict_put_bool(opts, QUORUM_OPT_BLKVERIFY, s->is_blkverify); |
| 1099 | qdict_put_bool(opts, QUORUM_OPT_REWRITE, s->rewrite_corrupted); |
Eric Blake | de6e795 | 2017-04-27 16:58:15 -0500 | [diff] [blame] | 1100 | qdict_put(opts, "children", children); |
Max Reitz | fafcfe2 | 2014-07-18 20:25:00 +0200 | [diff] [blame] | 1101 | |
| 1102 | bs->full_open_options = opts; |
| 1103 | } |
| 1104 | |
Benoît Canet | cadebd7 | 2014-02-21 22:21:11 +0100 | [diff] [blame] | 1105 | static BlockDriver bdrv_quorum = { |
Stefan Hajnoczi | e3625d3 | 2014-05-08 16:34:46 +0200 | [diff] [blame] | 1106 | .format_name = "quorum", |
Benoît Canet | cadebd7 | 2014-02-21 22:21:11 +0100 | [diff] [blame] | 1107 | |
Stefan Hajnoczi | e3625d3 | 2014-05-08 16:34:46 +0200 | [diff] [blame] | 1108 | .instance_size = sizeof(BDRVQuorumState), |
Benoît Canet | 13e7956 | 2014-02-21 22:21:12 +0100 | [diff] [blame] | 1109 | |
Fabiano Rosas | 65d2c3e | 2018-03-12 19:07:50 -0300 | [diff] [blame] | 1110 | .bdrv_open = quorum_open, |
Stefan Hajnoczi | e3625d3 | 2014-05-08 16:34:46 +0200 | [diff] [blame] | 1111 | .bdrv_close = quorum_close, |
Max Reitz | fafcfe2 | 2014-07-18 20:25:00 +0200 | [diff] [blame] | 1112 | .bdrv_refresh_filename = quorum_refresh_filename, |
Benoît Canet | c88a1de | 2014-02-21 22:21:20 +0100 | [diff] [blame] | 1113 | |
Stefan Hajnoczi | e3625d3 | 2014-05-08 16:34:46 +0200 | [diff] [blame] | 1114 | .bdrv_co_flush_to_disk = quorum_co_flush, |
Benoît Canet | 1c508d1 | 2014-02-21 22:21:18 +0100 | [diff] [blame] | 1115 | |
Stefan Hajnoczi | e3625d3 | 2014-05-08 16:34:46 +0200 | [diff] [blame] | 1116 | .bdrv_getlength = quorum_getlength, |
Benoît Canet | d55dee2 | 2014-02-21 22:21:16 +0100 | [diff] [blame] | 1117 | |
Kevin Wolf | 6847da3 | 2016-11-10 17:22:07 +0100 | [diff] [blame] | 1118 | .bdrv_co_preadv = quorum_co_preadv, |
| 1119 | .bdrv_co_pwritev = quorum_co_pwritev, |
Benoît Canet | 98a7a38 | 2014-02-21 22:21:19 +0100 | [diff] [blame] | 1120 | |
Wen Congyang | 98292c6 | 2016-05-10 15:36:38 +0800 | [diff] [blame] | 1121 | .bdrv_add_child = quorum_add_child, |
| 1122 | .bdrv_del_child = quorum_del_child, |
| 1123 | |
Kevin Wolf | d7010df | 2016-12-15 12:28:58 +0100 | [diff] [blame] | 1124 | .bdrv_child_perm = bdrv_filter_default_perms, |
| 1125 | |
Stefan Hajnoczi | e3625d3 | 2014-05-08 16:34:46 +0200 | [diff] [blame] | 1126 | .is_filter = true, |
| 1127 | .bdrv_recurse_is_first_non_filter = quorum_recurse_is_first_non_filter, |
Benoît Canet | cadebd7 | 2014-02-21 22:21:11 +0100 | [diff] [blame] | 1128 | }; |
| 1129 | |
| 1130 | static void bdrv_quorum_init(void) |
| 1131 | { |
Sascha Silbe | e94867e | 2015-08-04 16:48:25 +0200 | [diff] [blame] | 1132 | if (!qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA256)) { |
| 1133 | /* SHA256 hash support is required for quorum device */ |
| 1134 | return; |
| 1135 | } |
Benoît Canet | cadebd7 | 2014-02-21 22:21:11 +0100 | [diff] [blame] | 1136 | bdrv_register(&bdrv_quorum); |
| 1137 | } |
| 1138 | |
| 1139 | block_init(bdrv_quorum_init); |