]>
Commit | Line | Data |
---|---|---|
f27aaf4b CB |
1 | /* |
2 | * QEMU Block driver for RADOS (Ceph) | |
3 | * | |
ad32e9c0 JD |
4 | * Copyright (C) 2010-2011 Christian Brunner <chb@muc.de>, |
5 | * Josh Durgin <josh.durgin@dreamhost.com> | |
f27aaf4b CB |
6 | * |
7 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
8 | * the COPYING file in the top-level directory. | |
9 | * | |
6b620ca3 PB |
10 | * Contributions after 2012-01-13 are licensed under the terms of the |
11 | * GNU GPL, version 2 or (at your option) any later version. | |
f27aaf4b CB |
12 | */ |
13 | ||
80c71a24 | 14 | #include "qemu/osdep.h" |
ad32e9c0 | 15 | |
2836284d | 16 | #include <rbd/librbd.h> |
da34e65c | 17 | #include "qapi/error.h" |
1de7afc9 | 18 | #include "qemu/error-report.h" |
0b8fa32f | 19 | #include "qemu/module.h" |
922a01a0 | 20 | #include "qemu/option.h" |
737e150e | 21 | #include "block/block_int.h" |
609f45ea | 22 | #include "block/qdict.h" |
60390a21 | 23 | #include "crypto/secret.h" |
f348b6d1 | 24 | #include "qemu/cutils.h" |
e4ec5ad4 | 25 | #include "sysemu/replay.h" |
c7cacb3e | 26 | #include "qapi/qmp/qstring.h" |
452fcdbc | 27 | #include "qapi/qmp/qdict.h" |
e98c6961 | 28 | #include "qapi/qmp/qjson.h" |
47e6b297 | 29 | #include "qapi/qmp/qlist.h" |
4bfb2741 KW |
30 | #include "qapi/qobject-input-visitor.h" |
31 | #include "qapi/qapi-visit-block-core.h" | |
f27aaf4b | 32 | |
f27aaf4b CB |
33 | /* |
34 | * When specifying the image filename use: | |
35 | * | |
fab5cf59 | 36 | * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]] |
f27aaf4b | 37 | * |
9e1fbcde | 38 | * poolname must be the name of an existing rados pool. |
f27aaf4b | 39 | * |
9e1fbcde | 40 | * devicename is the name of the rbd image. |
f27aaf4b | 41 | * |
9e1fbcde SW |
42 | * Each option given is used to configure rados, and may be any valid |
43 | * Ceph option, "id", or "conf". | |
fab5cf59 | 44 | * |
9e1fbcde SW |
45 | * The "id" option indicates what user we should authenticate as to |
46 | * the Ceph cluster. If it is excluded we will use the Ceph default | |
47 | * (normally 'admin'). | |
f27aaf4b | 48 | * |
9e1fbcde SW |
49 | * The "conf" option specifies a Ceph configuration file to read. If |
50 | * it is not specified, we will read from the default Ceph locations | |
51 | * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration | |
52 | * file, specify conf=/dev/null. | |
f27aaf4b | 53 | * |
9e1fbcde SW |
54 | * Configuration values containing :, @, or = can be escaped with a |
55 | * leading "\". | |
f27aaf4b CB |
56 | */ |
57 | ||
58 | #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER) | |
59 | ||
ad32e9c0 JD |
60 | #define RBD_MAX_SNAPS 100 |
61 | ||
42e4ac9e OO |
62 | #define RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN 8 |
63 | ||
64 | static const char rbd_luks_header_verification[ | |
65 | RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN] = { | |
66 | 'L', 'U', 'K', 'S', 0xBA, 0xBE, 0, 1 | |
67 | }; | |
68 | ||
69 | static const char rbd_luks2_header_verification[ | |
70 | RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN] = { | |
71 | 'L', 'U', 'K', 'S', 0xBA, 0xBE, 0, 2 | |
72 | }; | |
73 | ||
787f3133 JD |
74 | typedef enum { |
75 | RBD_AIO_READ, | |
76 | RBD_AIO_WRITE, | |
dc7588c1 JD |
77 | RBD_AIO_DISCARD, |
78 | RBD_AIO_FLUSH | |
787f3133 JD |
79 | } RBDAIOCmd; |
80 | ||
f27aaf4b | 81 | typedef struct RBDAIOCB { |
7c84b1b8 | 82 | BlockAIOCB common; |
08448d51 | 83 | int64_t ret; |
f27aaf4b | 84 | QEMUIOVector *qiov; |
787f3133 | 85 | RBDAIOCmd cmd; |
f27aaf4b CB |
86 | int error; |
87 | struct BDRVRBDState *s; | |
f27aaf4b CB |
88 | } RBDAIOCB; |
89 | ||
90 | typedef struct RADOSCB { | |
f27aaf4b CB |
91 | RBDAIOCB *acb; |
92 | struct BDRVRBDState *s; | |
ad32e9c0 | 93 | int64_t size; |
08448d51 | 94 | int64_t ret; |
f27aaf4b CB |
95 | } RADOSCB; |
96 | ||
f27aaf4b | 97 | typedef struct BDRVRBDState { |
ad32e9c0 JD |
98 | rados_t cluster; |
99 | rados_ioctx_t io_ctx; | |
100 | rbd_image_t image; | |
80b61a27 | 101 | char *image_name; |
ad32e9c0 | 102 | char *snap; |
19ae9ae0 | 103 | char *namespace; |
d24f8023 | 104 | uint64_t image_size; |
f27aaf4b CB |
105 | } BDRVRBDState; |
106 | ||
aa045c2d KW |
107 | static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx, |
108 | BlockdevOptionsRbd *opts, bool cache, | |
109 | const char *keypairs, const char *secretid, | |
110 | Error **errp); | |
111 | ||
2b99cfce CK |
112 | static char *qemu_rbd_strchr(char *src, char delim) |
113 | { | |
114 | char *p; | |
115 | ||
116 | for (p = src; *p; ++p) { | |
117 | if (*p == delim) { | |
118 | return p; | |
119 | } | |
120 | if (*p == '\\' && p[1] != '\0') { | |
121 | ++p; | |
122 | } | |
123 | } | |
124 | ||
125 | return NULL; | |
126 | } | |
127 | ||
128 | ||
730b00bb | 129 | static char *qemu_rbd_next_tok(char *src, char delim, char **p) |
f27aaf4b | 130 | { |
f27aaf4b CB |
131 | char *end; |
132 | ||
133 | *p = NULL; | |
134 | ||
2b99cfce CK |
135 | end = qemu_rbd_strchr(src, delim); |
136 | if (end) { | |
8efb339d MA |
137 | *p = end + 1; |
138 | *end = '\0'; | |
139 | } | |
7830f909 | 140 | return src; |
f27aaf4b CB |
141 | } |
142 | ||
16a06b24 SW |
143 | static void qemu_rbd_unescape(char *src) |
144 | { | |
145 | char *p; | |
146 | ||
147 | for (p = src; *src; ++src, ++p) { | |
148 | if (*src == '\\' && src[1] != '\0') { | |
149 | src++; | |
150 | } | |
151 | *p = *src; | |
152 | } | |
153 | *p = '\0'; | |
154 | } | |
155 | ||
c7cacb3e JC |
156 | static void qemu_rbd_parse_filename(const char *filename, QDict *options, |
157 | Error **errp) | |
f27aaf4b CB |
158 | { |
159 | const char *start; | |
e98c6961 EB |
160 | char *p, *buf; |
161 | QList *keypairs = NULL; | |
19ae9ae0 | 162 | char *found_str, *image_name; |
f27aaf4b CB |
163 | |
164 | if (!strstart(filename, "rbd:", &start)) { | |
d61563b2 | 165 | error_setg(errp, "File name must start with 'rbd:'"); |
c7cacb3e | 166 | return; |
f27aaf4b CB |
167 | } |
168 | ||
7267c094 | 169 | buf = g_strdup(start); |
f27aaf4b CB |
170 | p = buf; |
171 | ||
730b00bb | 172 | found_str = qemu_rbd_next_tok(p, '/', &p); |
7830f909 | 173 | if (!p) { |
7830f909 | 174 | error_setg(errp, "Pool name is required"); |
f27aaf4b CB |
175 | goto done; |
176 | } | |
7830f909 | 177 | qemu_rbd_unescape(found_str); |
46f5ac20 | 178 | qdict_put_str(options, "pool", found_str); |
fab5cf59 | 179 | |
2b99cfce | 180 | if (qemu_rbd_strchr(p, '@')) { |
19ae9ae0 | 181 | image_name = qemu_rbd_next_tok(p, '@', &p); |
7830f909 | 182 | |
730b00bb | 183 | found_str = qemu_rbd_next_tok(p, ':', &p); |
7830f909 | 184 | qemu_rbd_unescape(found_str); |
46f5ac20 | 185 | qdict_put_str(options, "snapshot", found_str); |
fab5cf59 | 186 | } else { |
19ae9ae0 FF |
187 | image_name = qemu_rbd_next_tok(p, ':', &p); |
188 | } | |
189 | /* Check for namespace in the image_name */ | |
2b99cfce | 190 | if (qemu_rbd_strchr(image_name, '/')) { |
19ae9ae0 | 191 | found_str = qemu_rbd_next_tok(image_name, '/', &image_name); |
7830f909 | 192 | qemu_rbd_unescape(found_str); |
19ae9ae0 FF |
193 | qdict_put_str(options, "namespace", found_str); |
194 | } else { | |
195 | qdict_put_str(options, "namespace", ""); | |
f27aaf4b | 196 | } |
19ae9ae0 FF |
197 | qemu_rbd_unescape(image_name); |
198 | qdict_put_str(options, "image", image_name); | |
7830f909 | 199 | if (!p) { |
f27aaf4b CB |
200 | goto done; |
201 | } | |
202 | ||
c7cacb3e JC |
203 | /* The following are essentially all key/value pairs, and we treat |
204 | * 'id' and 'conf' a bit special. Key/value pairs may be in any order. */ | |
205 | while (p) { | |
206 | char *name, *value; | |
730b00bb | 207 | name = qemu_rbd_next_tok(p, '=', &p); |
c7cacb3e JC |
208 | if (!p) { |
209 | error_setg(errp, "conf option %s has no value", name); | |
210 | break; | |
7c7e9df0 | 211 | } |
c7cacb3e JC |
212 | |
213 | qemu_rbd_unescape(name); | |
214 | ||
730b00bb | 215 | value = qemu_rbd_next_tok(p, ':', &p); |
c7cacb3e JC |
216 | qemu_rbd_unescape(value); |
217 | ||
218 | if (!strcmp(name, "conf")) { | |
46f5ac20 | 219 | qdict_put_str(options, "conf", value); |
c7cacb3e | 220 | } else if (!strcmp(name, "id")) { |
46f5ac20 | 221 | qdict_put_str(options, "user", value); |
c7cacb3e | 222 | } else { |
e98c6961 EB |
223 | /* |
224 | * We pass these internally to qemu_rbd_set_keypairs(), so | |
225 | * we can get away with the simpler list of [ "key1", | |
226 | * "value1", "key2", "value2" ] rather than a raw dict | |
227 | * { "key1": "value1", "key2": "value2" } where we can't | |
228 | * guarantee order, or even a more correct but complex | |
229 | * [ { "key1": "value1" }, { "key2": "value2" } ] | |
230 | */ | |
231 | if (!keypairs) { | |
232 | keypairs = qlist_new(); | |
c7cacb3e | 233 | } |
46f5ac20 EB |
234 | qlist_append_str(keypairs, name); |
235 | qlist_append_str(keypairs, value); | |
c7cacb3e | 236 | } |
7c7e9df0 | 237 | } |
c7cacb3e | 238 | |
e98c6961 EB |
239 | if (keypairs) { |
240 | qdict_put(options, "=keyvalue-pairs", | |
eab3a467 | 241 | qstring_from_gstring(qobject_to_json(QOBJECT(keypairs)))); |
c7cacb3e JC |
242 | } |
243 | ||
c7cacb3e | 244 | done: |
c7cacb3e | 245 | g_free(buf); |
cb3e7f08 | 246 | qobject_unref(keypairs); |
c7cacb3e | 247 | return; |
7c7e9df0 SW |
248 | } |
249 | ||
60390a21 | 250 | |
e8e16d4b EB |
251 | static void qemu_rbd_refresh_limits(BlockDriverState *bs, Error **errp) |
252 | { | |
253 | /* XXX Does RBD support AIO on less than 512-byte alignment? */ | |
254 | bs->bl.request_alignment = 512; | |
255 | } | |
256 | ||
257 | ||
d083f954 | 258 | static int qemu_rbd_set_auth(rados_t cluster, BlockdevOptionsRbd *opts, |
60390a21 DB |
259 | Error **errp) |
260 | { | |
d083f954 | 261 | char *key, *acr; |
a3699de4 MA |
262 | int r; |
263 | GString *accu; | |
264 | RbdAuthModeList *auth; | |
265 | ||
d083f954 MA |
266 | if (opts->key_secret) { |
267 | key = qcrypto_secret_lookup_as_base64(opts->key_secret, errp); | |
268 | if (!key) { | |
269 | return -EIO; | |
270 | } | |
271 | r = rados_conf_set(cluster, "key", key); | |
272 | g_free(key); | |
273 | if (r < 0) { | |
274 | error_setg_errno(errp, -r, "Could not set 'key'"); | |
275 | return r; | |
a3699de4 | 276 | } |
60390a21 DB |
277 | } |
278 | ||
a3699de4 MA |
279 | if (opts->has_auth_client_required) { |
280 | accu = g_string_new(""); | |
281 | for (auth = opts->auth_client_required; auth; auth = auth->next) { | |
282 | if (accu->str[0]) { | |
283 | g_string_append_c(accu, ';'); | |
284 | } | |
285 | g_string_append(accu, RbdAuthMode_str(auth->value)); | |
286 | } | |
287 | acr = g_string_free(accu, FALSE); | |
288 | r = rados_conf_set(cluster, "auth_client_required", acr); | |
289 | g_free(acr); | |
290 | if (r < 0) { | |
291 | error_setg_errno(errp, -r, | |
292 | "Could not set 'auth_client_required'"); | |
293 | return r; | |
294 | } | |
295 | } | |
60390a21 DB |
296 | |
297 | return 0; | |
298 | } | |
299 | ||
e98c6961 | 300 | static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json, |
c7cacb3e | 301 | Error **errp) |
fab5cf59 | 302 | { |
e98c6961 EB |
303 | QList *keypairs; |
304 | QString *name; | |
305 | QString *value; | |
306 | const char *key; | |
307 | size_t remaining; | |
fab5cf59 JD |
308 | int ret = 0; |
309 | ||
e98c6961 EB |
310 | if (!keypairs_json) { |
311 | return ret; | |
312 | } | |
7dc847eb HR |
313 | keypairs = qobject_to(QList, |
314 | qobject_from_json(keypairs_json, &error_abort)); | |
e98c6961 EB |
315 | remaining = qlist_size(keypairs) / 2; |
316 | assert(remaining); | |
317 | ||
318 | while (remaining--) { | |
7dc847eb HR |
319 | name = qobject_to(QString, qlist_pop(keypairs)); |
320 | value = qobject_to(QString, qlist_pop(keypairs)); | |
e98c6961 EB |
321 | assert(name && value); |
322 | key = qstring_get_str(name); | |
323 | ||
324 | ret = rados_conf_set(cluster, key, qstring_get_str(value)); | |
cb3e7f08 | 325 | qobject_unref(value); |
c7cacb3e | 326 | if (ret < 0) { |
e98c6961 | 327 | error_setg_errno(errp, -ret, "invalid conf option %s", key); |
cb3e7f08 | 328 | qobject_unref(name); |
c7cacb3e JC |
329 | ret = -EINVAL; |
330 | break; | |
fab5cf59 | 331 | } |
cb3e7f08 | 332 | qobject_unref(name); |
fab5cf59 JD |
333 | } |
334 | ||
cb3e7f08 | 335 | qobject_unref(keypairs); |
fab5cf59 JD |
336 | return ret; |
337 | } | |
338 | ||
1d393bde | 339 | static void qemu_rbd_memset(RADOSCB *rcb, int64_t offs) |
340 | { | |
48672ac0 PL |
341 | RBDAIOCB *acb = rcb->acb; |
342 | iov_memset(acb->qiov->iov, acb->qiov->niov, offs, 0, | |
343 | acb->qiov->size - offs); | |
1d393bde | 344 | } |
345 | ||
42e4ac9e OO |
346 | #ifdef LIBRBD_SUPPORTS_ENCRYPTION |
347 | static int qemu_rbd_convert_luks_options( | |
348 | RbdEncryptionOptionsLUKSBase *luks_opts, | |
349 | char **passphrase, | |
350 | size_t *passphrase_len, | |
351 | Error **errp) | |
352 | { | |
353 | return qcrypto_secret_lookup(luks_opts->key_secret, (uint8_t **)passphrase, | |
354 | passphrase_len, errp); | |
355 | } | |
356 | ||
357 | static int qemu_rbd_convert_luks_create_options( | |
358 | RbdEncryptionCreateOptionsLUKSBase *luks_opts, | |
359 | rbd_encryption_algorithm_t *alg, | |
360 | char **passphrase, | |
361 | size_t *passphrase_len, | |
362 | Error **errp) | |
363 | { | |
364 | int r = 0; | |
365 | ||
366 | r = qemu_rbd_convert_luks_options( | |
367 | qapi_RbdEncryptionCreateOptionsLUKSBase_base(luks_opts), | |
368 | passphrase, passphrase_len, errp); | |
369 | if (r < 0) { | |
370 | return r; | |
371 | } | |
372 | ||
373 | if (luks_opts->has_cipher_alg) { | |
374 | switch (luks_opts->cipher_alg) { | |
375 | case QCRYPTO_CIPHER_ALG_AES_128: { | |
376 | *alg = RBD_ENCRYPTION_ALGORITHM_AES128; | |
377 | break; | |
378 | } | |
379 | case QCRYPTO_CIPHER_ALG_AES_256: { | |
380 | *alg = RBD_ENCRYPTION_ALGORITHM_AES256; | |
381 | break; | |
382 | } | |
383 | default: { | |
384 | r = -ENOTSUP; | |
385 | error_setg_errno(errp, -r, "unknown encryption algorithm: %u", | |
386 | luks_opts->cipher_alg); | |
387 | return r; | |
388 | } | |
389 | } | |
390 | } else { | |
391 | /* default alg */ | |
392 | *alg = RBD_ENCRYPTION_ALGORITHM_AES256; | |
393 | } | |
394 | ||
395 | return 0; | |
396 | } | |
397 | ||
398 | static int qemu_rbd_encryption_format(rbd_image_t image, | |
399 | RbdEncryptionCreateOptions *encrypt, | |
400 | Error **errp) | |
401 | { | |
402 | int r = 0; | |
403 | g_autofree char *passphrase = NULL; | |
404 | size_t passphrase_len; | |
405 | rbd_encryption_format_t format; | |
406 | rbd_encryption_options_t opts; | |
407 | rbd_encryption_luks1_format_options_t luks_opts; | |
408 | rbd_encryption_luks2_format_options_t luks2_opts; | |
409 | size_t opts_size; | |
410 | uint64_t raw_size, effective_size; | |
411 | ||
412 | r = rbd_get_size(image, &raw_size); | |
413 | if (r < 0) { | |
414 | error_setg_errno(errp, -r, "cannot get raw image size"); | |
415 | return r; | |
416 | } | |
417 | ||
418 | switch (encrypt->format) { | |
419 | case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS: { | |
420 | memset(&luks_opts, 0, sizeof(luks_opts)); | |
421 | format = RBD_ENCRYPTION_FORMAT_LUKS1; | |
422 | opts = &luks_opts; | |
423 | opts_size = sizeof(luks_opts); | |
424 | r = qemu_rbd_convert_luks_create_options( | |
425 | qapi_RbdEncryptionCreateOptionsLUKS_base(&encrypt->u.luks), | |
426 | &luks_opts.alg, &passphrase, &passphrase_len, errp); | |
427 | if (r < 0) { | |
428 | return r; | |
429 | } | |
430 | luks_opts.passphrase = passphrase; | |
431 | luks_opts.passphrase_size = passphrase_len; | |
432 | break; | |
433 | } | |
434 | case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2: { | |
435 | memset(&luks2_opts, 0, sizeof(luks2_opts)); | |
436 | format = RBD_ENCRYPTION_FORMAT_LUKS2; | |
437 | opts = &luks2_opts; | |
438 | opts_size = sizeof(luks2_opts); | |
439 | r = qemu_rbd_convert_luks_create_options( | |
440 | qapi_RbdEncryptionCreateOptionsLUKS2_base( | |
441 | &encrypt->u.luks2), | |
442 | &luks2_opts.alg, &passphrase, &passphrase_len, errp); | |
443 | if (r < 0) { | |
444 | return r; | |
445 | } | |
446 | luks2_opts.passphrase = passphrase; | |
447 | luks2_opts.passphrase_size = passphrase_len; | |
448 | break; | |
449 | } | |
450 | default: { | |
451 | r = -ENOTSUP; | |
452 | error_setg_errno( | |
453 | errp, -r, "unknown image encryption format: %u", | |
454 | encrypt->format); | |
455 | return r; | |
456 | } | |
457 | } | |
458 | ||
459 | r = rbd_encryption_format(image, format, opts, opts_size); | |
460 | if (r < 0) { | |
461 | error_setg_errno(errp, -r, "encryption format fail"); | |
462 | return r; | |
463 | } | |
464 | ||
465 | r = rbd_get_size(image, &effective_size); | |
466 | if (r < 0) { | |
467 | error_setg_errno(errp, -r, "cannot get effective image size"); | |
468 | return r; | |
469 | } | |
470 | ||
471 | r = rbd_resize(image, raw_size + (raw_size - effective_size)); | |
472 | if (r < 0) { | |
473 | error_setg_errno(errp, -r, "cannot resize image after format"); | |
474 | return r; | |
475 | } | |
476 | ||
477 | return 0; | |
478 | } | |
479 | ||
480 | static int qemu_rbd_encryption_load(rbd_image_t image, | |
481 | RbdEncryptionOptions *encrypt, | |
482 | Error **errp) | |
483 | { | |
484 | int r = 0; | |
485 | g_autofree char *passphrase = NULL; | |
486 | size_t passphrase_len; | |
487 | rbd_encryption_luks1_format_options_t luks_opts; | |
488 | rbd_encryption_luks2_format_options_t luks2_opts; | |
489 | rbd_encryption_format_t format; | |
490 | rbd_encryption_options_t opts; | |
491 | size_t opts_size; | |
492 | ||
493 | switch (encrypt->format) { | |
494 | case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS: { | |
495 | memset(&luks_opts, 0, sizeof(luks_opts)); | |
496 | format = RBD_ENCRYPTION_FORMAT_LUKS1; | |
497 | opts = &luks_opts; | |
498 | opts_size = sizeof(luks_opts); | |
499 | r = qemu_rbd_convert_luks_options( | |
500 | qapi_RbdEncryptionOptionsLUKS_base(&encrypt->u.luks), | |
501 | &passphrase, &passphrase_len, errp); | |
502 | if (r < 0) { | |
503 | return r; | |
504 | } | |
505 | luks_opts.passphrase = passphrase; | |
506 | luks_opts.passphrase_size = passphrase_len; | |
507 | break; | |
508 | } | |
509 | case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2: { | |
510 | memset(&luks2_opts, 0, sizeof(luks2_opts)); | |
511 | format = RBD_ENCRYPTION_FORMAT_LUKS2; | |
512 | opts = &luks2_opts; | |
513 | opts_size = sizeof(luks2_opts); | |
514 | r = qemu_rbd_convert_luks_options( | |
515 | qapi_RbdEncryptionOptionsLUKS2_base(&encrypt->u.luks2), | |
516 | &passphrase, &passphrase_len, errp); | |
517 | if (r < 0) { | |
518 | return r; | |
519 | } | |
520 | luks2_opts.passphrase = passphrase; | |
521 | luks2_opts.passphrase_size = passphrase_len; | |
522 | break; | |
523 | } | |
524 | default: { | |
525 | r = -ENOTSUP; | |
526 | error_setg_errno( | |
527 | errp, -r, "unknown image encryption format: %u", | |
528 | encrypt->format); | |
529 | return r; | |
530 | } | |
531 | } | |
532 | ||
533 | r = rbd_encryption_load(image, format, opts, opts_size); | |
534 | if (r < 0) { | |
535 | error_setg_errno(errp, -r, "encryption load fail"); | |
536 | return r; | |
537 | } | |
538 | ||
539 | return 0; | |
540 | } | |
541 | #endif | |
542 | ||
d083f954 | 543 | /* FIXME Deprecate and remove keypairs or make it available in QMP. */ |
1bebea37 KW |
544 | static int qemu_rbd_do_create(BlockdevCreateOptions *options, |
545 | const char *keypairs, const char *password_secret, | |
546 | Error **errp) | |
f27aaf4b | 547 | { |
1bebea37 | 548 | BlockdevCreateOptionsRbd *opts = &options->u.rbd; |
ad32e9c0 JD |
549 | rados_t cluster; |
550 | rados_ioctx_t io_ctx; | |
1bebea37 KW |
551 | int obj_order = 0; |
552 | int ret; | |
553 | ||
554 | assert(options->driver == BLOCKDEV_DRIVER_RBD); | |
555 | if (opts->location->has_snapshot) { | |
556 | error_setg(errp, "Can't use snapshot name for image creation"); | |
557 | return -EINVAL; | |
558 | } | |
f27aaf4b | 559 | |
42e4ac9e OO |
560 | #ifndef LIBRBD_SUPPORTS_ENCRYPTION |
561 | if (opts->has_encrypt) { | |
562 | error_setg(errp, "RBD library does not support image encryption"); | |
563 | return -ENOTSUP; | |
564 | } | |
565 | #endif | |
566 | ||
1bebea37 KW |
567 | if (opts->has_cluster_size) { |
568 | int64_t objsize = opts->cluster_size; | |
bd0cf596 CL |
569 | if ((objsize - 1) & objsize) { /* not a power of 2? */ |
570 | error_setg(errp, "obj size needs to be power of 2"); | |
1bebea37 | 571 | return -EINVAL; |
bd0cf596 CL |
572 | } |
573 | if (objsize < 4096) { | |
574 | error_setg(errp, "obj size too small"); | |
1bebea37 | 575 | return -EINVAL; |
f27aaf4b | 576 | } |
786a4ea8 | 577 | obj_order = ctz32(objsize); |
f27aaf4b CB |
578 | } |
579 | ||
aa045c2d KW |
580 | ret = qemu_rbd_connect(&cluster, &io_ctx, opts->location, false, keypairs, |
581 | password_secret, errp); | |
87cd3d20 | 582 | if (ret < 0) { |
1bebea37 | 583 | return ret; |
f27aaf4b CB |
584 | } |
585 | ||
1bebea37 | 586 | ret = rbd_create(io_ctx, opts->location->image, opts->size, &obj_order); |
87cd3d20 VU |
587 | if (ret < 0) { |
588 | error_setg_errno(errp, -ret, "error rbd create"); | |
aa045c2d | 589 | goto out; |
87cd3d20 | 590 | } |
f27aaf4b | 591 | |
42e4ac9e OO |
592 | #ifdef LIBRBD_SUPPORTS_ENCRYPTION |
593 | if (opts->has_encrypt) { | |
594 | rbd_image_t image; | |
595 | ||
596 | ret = rbd_open(io_ctx, opts->location->image, &image, NULL); | |
597 | if (ret < 0) { | |
598 | error_setg_errno(errp, -ret, | |
599 | "error opening image '%s' for encryption format", | |
600 | opts->location->image); | |
601 | goto out; | |
602 | } | |
603 | ||
604 | ret = qemu_rbd_encryption_format(image, opts->encrypt, errp); | |
605 | rbd_close(image); | |
606 | if (ret < 0) { | |
607 | /* encryption format fail, try removing the image */ | |
608 | rbd_remove(io_ctx, opts->location->image); | |
609 | goto out; | |
610 | } | |
611 | } | |
612 | #endif | |
613 | ||
1bebea37 | 614 | ret = 0; |
aa045c2d KW |
615 | out: |
616 | rados_ioctx_destroy(io_ctx); | |
e38f643a | 617 | rados_shutdown(cluster); |
1bebea37 KW |
618 | return ret; |
619 | } | |
620 | ||
621 | static int qemu_rbd_co_create(BlockdevCreateOptions *options, Error **errp) | |
622 | { | |
623 | return qemu_rbd_do_create(options, NULL, NULL, errp); | |
624 | } | |
625 | ||
42e4ac9e OO |
626 | static int qemu_rbd_extract_encryption_create_options( |
627 | QemuOpts *opts, | |
628 | RbdEncryptionCreateOptions **spec, | |
629 | Error **errp) | |
630 | { | |
631 | QDict *opts_qdict; | |
632 | QDict *encrypt_qdict; | |
633 | Visitor *v; | |
634 | int ret = 0; | |
635 | ||
636 | opts_qdict = qemu_opts_to_qdict(opts, NULL); | |
637 | qdict_extract_subqdict(opts_qdict, &encrypt_qdict, "encrypt."); | |
638 | qobject_unref(opts_qdict); | |
639 | if (!qdict_size(encrypt_qdict)) { | |
640 | *spec = NULL; | |
641 | goto exit; | |
642 | } | |
643 | ||
644 | /* Convert options into a QAPI object */ | |
645 | v = qobject_input_visitor_new_flat_confused(encrypt_qdict, errp); | |
646 | if (!v) { | |
647 | ret = -EINVAL; | |
648 | goto exit; | |
649 | } | |
650 | ||
651 | visit_type_RbdEncryptionCreateOptions(v, NULL, spec, errp); | |
652 | visit_free(v); | |
653 | if (!*spec) { | |
654 | ret = -EINVAL; | |
655 | goto exit; | |
656 | } | |
657 | ||
658 | exit: | |
659 | qobject_unref(encrypt_qdict); | |
660 | return ret; | |
661 | } | |
662 | ||
b92902df ML |
663 | static int coroutine_fn qemu_rbd_co_create_opts(BlockDriver *drv, |
664 | const char *filename, | |
1bebea37 KW |
665 | QemuOpts *opts, |
666 | Error **errp) | |
667 | { | |
668 | BlockdevCreateOptions *create_options; | |
669 | BlockdevCreateOptionsRbd *rbd_opts; | |
670 | BlockdevOptionsRbd *loc; | |
42e4ac9e | 671 | RbdEncryptionCreateOptions *encrypt = NULL; |
1bebea37 KW |
672 | Error *local_err = NULL; |
673 | const char *keypairs, *password_secret; | |
674 | QDict *options = NULL; | |
675 | int ret = 0; | |
676 | ||
677 | create_options = g_new0(BlockdevCreateOptions, 1); | |
678 | create_options->driver = BLOCKDEV_DRIVER_RBD; | |
679 | rbd_opts = &create_options->u.rbd; | |
680 | ||
681 | rbd_opts->location = g_new0(BlockdevOptionsRbd, 1); | |
682 | ||
683 | password_secret = qemu_opt_get(opts, "password-secret"); | |
684 | ||
685 | /* Read out options */ | |
686 | rbd_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), | |
687 | BDRV_SECTOR_SIZE); | |
688 | rbd_opts->cluster_size = qemu_opt_get_size_del(opts, | |
689 | BLOCK_OPT_CLUSTER_SIZE, 0); | |
690 | rbd_opts->has_cluster_size = (rbd_opts->cluster_size != 0); | |
691 | ||
692 | options = qdict_new(); | |
693 | qemu_rbd_parse_filename(filename, options, &local_err); | |
694 | if (local_err) { | |
695 | ret = -EINVAL; | |
696 | error_propagate(errp, local_err); | |
697 | goto exit; | |
698 | } | |
699 | ||
42e4ac9e OO |
700 | ret = qemu_rbd_extract_encryption_create_options(opts, &encrypt, errp); |
701 | if (ret < 0) { | |
702 | goto exit; | |
703 | } | |
704 | rbd_opts->encrypt = encrypt; | |
705 | rbd_opts->has_encrypt = !!encrypt; | |
706 | ||
1bebea37 KW |
707 | /* |
708 | * Caution: while qdict_get_try_str() is fine, getting non-string | |
709 | * types would require more care. When @options come from -blockdev | |
710 | * or blockdev_add, its members are typed according to the QAPI | |
711 | * schema, but when they come from -drive, they're all QString. | |
712 | */ | |
713 | loc = rbd_opts->location; | |
19ae9ae0 FF |
714 | loc->pool = g_strdup(qdict_get_try_str(options, "pool")); |
715 | loc->conf = g_strdup(qdict_get_try_str(options, "conf")); | |
716 | loc->has_conf = !!loc->conf; | |
717 | loc->user = g_strdup(qdict_get_try_str(options, "user")); | |
718 | loc->has_user = !!loc->user; | |
719 | loc->q_namespace = g_strdup(qdict_get_try_str(options, "namespace")); | |
b084b420 | 720 | loc->has_q_namespace = !!loc->q_namespace; |
19ae9ae0 FF |
721 | loc->image = g_strdup(qdict_get_try_str(options, "image")); |
722 | keypairs = qdict_get_try_str(options, "=keyvalue-pairs"); | |
1bebea37 KW |
723 | |
724 | ret = qemu_rbd_do_create(create_options, keypairs, password_secret, errp); | |
725 | if (ret < 0) { | |
726 | goto exit; | |
727 | } | |
c7cacb3e JC |
728 | |
729 | exit: | |
cb3e7f08 | 730 | qobject_unref(options); |
1bebea37 | 731 | qapi_free_BlockdevCreateOptions(create_options); |
f27aaf4b CB |
732 | return ret; |
733 | } | |
734 | ||
735 | /* | |
e04fb07f SH |
736 | * This aio completion is being called from rbd_finish_bh() and runs in qemu |
737 | * BH context. | |
f27aaf4b | 738 | */ |
ad32e9c0 | 739 | static void qemu_rbd_complete_aio(RADOSCB *rcb) |
f27aaf4b CB |
740 | { |
741 | RBDAIOCB *acb = rcb->acb; | |
742 | int64_t r; | |
743 | ||
f27aaf4b CB |
744 | r = rcb->ret; |
745 | ||
dc7588c1 | 746 | if (acb->cmd != RBD_AIO_READ) { |
f27aaf4b CB |
747 | if (r < 0) { |
748 | acb->ret = r; | |
749 | acb->error = 1; | |
750 | } else if (!acb->error) { | |
ad32e9c0 | 751 | acb->ret = rcb->size; |
f27aaf4b CB |
752 | } |
753 | } else { | |
ad32e9c0 | 754 | if (r < 0) { |
1d393bde | 755 | qemu_rbd_memset(rcb, 0); |
f27aaf4b CB |
756 | acb->ret = r; |
757 | acb->error = 1; | |
ad32e9c0 | 758 | } else if (r < rcb->size) { |
1d393bde | 759 | qemu_rbd_memset(rcb, r); |
f27aaf4b | 760 | if (!acb->error) { |
ad32e9c0 | 761 | acb->ret = rcb->size; |
f27aaf4b CB |
762 | } |
763 | } else if (!acb->error) { | |
ad32e9c0 | 764 | acb->ret = r; |
f27aaf4b CB |
765 | } |
766 | } | |
f27aaf4b | 767 | |
e04fb07f | 768 | g_free(rcb); |
f27aaf4b | 769 | |
e04fb07f | 770 | acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret)); |
f27aaf4b | 771 | |
8007429a | 772 | qemu_aio_unref(acb); |
f27aaf4b CB |
773 | } |
774 | ||
4bfb2741 | 775 | static char *qemu_rbd_mon_host(BlockdevOptionsRbd *opts, Error **errp) |
0a55679b | 776 | { |
4bfb2741 | 777 | const char **vals; |
2836284d MA |
778 | const char *host, *port; |
779 | char *rados_str; | |
4bfb2741 KW |
780 | InetSocketAddressBaseList *p; |
781 | int i, cnt; | |
782 | ||
783 | if (!opts->has_server) { | |
784 | return NULL; | |
785 | } | |
786 | ||
787 | for (cnt = 0, p = opts->server; p; p = p->next) { | |
788 | cnt++; | |
789 | } | |
790 | ||
791 | vals = g_new(const char *, cnt + 1); | |
792 | ||
793 | for (i = 0, p = opts->server; p; p = p->next, i++) { | |
794 | host = p->value->host; | |
795 | port = p->value->port; | |
0a55679b | 796 | |
2836284d | 797 | if (strchr(host, ':')) { |
4bfb2741 | 798 | vals[i] = g_strdup_printf("[%s]:%s", host, port); |
0a55679b | 799 | } else { |
4bfb2741 | 800 | vals[i] = g_strdup_printf("%s:%s", host, port); |
0a55679b | 801 | } |
0a55679b | 802 | } |
2836284d | 803 | vals[i] = NULL; |
0a55679b | 804 | |
2836284d | 805 | rados_str = i ? g_strjoinv(";", (char **)vals) : NULL; |
2836284d | 806 | g_strfreev((char **)vals); |
0a55679b JC |
807 | return rados_str; |
808 | } | |
809 | ||
3d9136f9 | 810 | static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx, |
4bfb2741 | 811 | BlockdevOptionsRbd *opts, bool cache, |
4ff45049 KW |
812 | const char *keypairs, const char *secretid, |
813 | Error **errp) | |
f27aaf4b | 814 | { |
0a55679b | 815 | char *mon_host = NULL; |
3d9136f9 | 816 | Error *local_err = NULL; |
f27aaf4b CB |
817 | int r; |
818 | ||
d083f954 MA |
819 | if (secretid) { |
820 | if (opts->key_secret) { | |
821 | error_setg(errp, | |
822 | "Legacy 'password-secret' clashes with 'key-secret'"); | |
823 | return -EINVAL; | |
824 | } | |
825 | opts->key_secret = g_strdup(secretid); | |
826 | opts->has_key_secret = true; | |
827 | } | |
828 | ||
4bfb2741 | 829 | mon_host = qemu_rbd_mon_host(opts, &local_err); |
0a55679b JC |
830 | if (local_err) { |
831 | error_propagate(errp, local_err); | |
832 | r = -EINVAL; | |
c1c1f6cf | 833 | goto out; |
0a55679b JC |
834 | } |
835 | ||
4bfb2741 | 836 | r = rados_create(cluster, opts->user); |
ad32e9c0 | 837 | if (r < 0) { |
87cd3d20 | 838 | error_setg_errno(errp, -r, "error initializing"); |
c1c1f6cf | 839 | goto out; |
f27aaf4b CB |
840 | } |
841 | ||
c7cacb3e | 842 | /* try default location when conf=NULL, but ignore failure */ |
4bfb2741 KW |
843 | r = rados_conf_read_file(*cluster, opts->conf); |
844 | if (opts->has_conf && r < 0) { | |
845 | error_setg_errno(errp, -r, "error reading conf file %s", opts->conf); | |
c7cacb3e | 846 | goto failed_shutdown; |
99a3c89d JD |
847 | } |
848 | ||
3d9136f9 | 849 | r = qemu_rbd_set_keypairs(*cluster, keypairs, errp); |
c7cacb3e JC |
850 | if (r < 0) { |
851 | goto failed_shutdown; | |
99a3c89d JD |
852 | } |
853 | ||
0a55679b | 854 | if (mon_host) { |
3d9136f9 | 855 | r = rados_conf_set(*cluster, "mon_host", mon_host); |
0a55679b JC |
856 | if (r < 0) { |
857 | goto failed_shutdown; | |
858 | } | |
859 | } | |
860 | ||
d083f954 MA |
861 | r = qemu_rbd_set_auth(*cluster, opts, errp); |
862 | if (r < 0) { | |
60390a21 DB |
863 | goto failed_shutdown; |
864 | } | |
865 | ||
b11f38fc JD |
866 | /* |
867 | * Fallback to more conservative semantics if setting cache | |
868 | * options fails. Ignore errors from setting rbd_cache because the | |
869 | * only possible error is that the option does not exist, and | |
870 | * librbd defaults to no caching. If write through caching cannot | |
871 | * be set up, fall back to no caching. | |
872 | */ | |
3d9136f9 KW |
873 | if (cache) { |
874 | rados_conf_set(*cluster, "rbd_cache", "true"); | |
b11f38fc | 875 | } else { |
3d9136f9 | 876 | rados_conf_set(*cluster, "rbd_cache", "false"); |
b11f38fc JD |
877 | } |
878 | ||
3d9136f9 | 879 | r = rados_connect(*cluster); |
ad32e9c0 | 880 | if (r < 0) { |
87cd3d20 | 881 | error_setg_errno(errp, -r, "error connecting"); |
eb93d5d9 | 882 | goto failed_shutdown; |
f27aaf4b CB |
883 | } |
884 | ||
4bfb2741 | 885 | r = rados_ioctx_create(*cluster, opts->pool, io_ctx); |
ad32e9c0 | 886 | if (r < 0) { |
4bfb2741 | 887 | error_setg_errno(errp, -r, "error opening pool %s", opts->pool); |
eb93d5d9 | 888 | goto failed_shutdown; |
f27aaf4b | 889 | } |
19ae9ae0 FF |
890 | /* |
891 | * Set the namespace after opening the io context on the pool, | |
892 | * if nspace == NULL or if nspace == "", it is just as we did nothing | |
893 | */ | |
894 | rados_ioctx_set_namespace(*io_ctx, opts->q_namespace); | |
f27aaf4b | 895 | |
c1c1f6cf SG |
896 | r = 0; |
897 | goto out; | |
3d9136f9 KW |
898 | |
899 | failed_shutdown: | |
900 | rados_shutdown(*cluster); | |
c1c1f6cf | 901 | out: |
3d9136f9 KW |
902 | g_free(mon_host); |
903 | return r; | |
904 | } | |
905 | ||
f24b03b5 JC |
906 | static int qemu_rbd_convert_options(QDict *options, BlockdevOptionsRbd **opts, |
907 | Error **errp) | |
908 | { | |
909 | Visitor *v; | |
f24b03b5 JC |
910 | |
911 | /* Convert the remaining options into a QAPI object */ | |
912 | v = qobject_input_visitor_new_flat_confused(options, errp); | |
913 | if (!v) { | |
914 | return -EINVAL; | |
915 | } | |
916 | ||
b11a093c | 917 | visit_type_BlockdevOptionsRbd(v, NULL, opts, errp); |
f24b03b5 | 918 | visit_free(v); |
b11a093c | 919 | if (!opts) { |
f24b03b5 JC |
920 | return -EINVAL; |
921 | } | |
922 | ||
923 | return 0; | |
924 | } | |
925 | ||
084d1d13 JC |
926 | static int qemu_rbd_attempt_legacy_options(QDict *options, |
927 | BlockdevOptionsRbd **opts, | |
928 | char **keypairs) | |
929 | { | |
930 | char *filename; | |
931 | int r; | |
932 | ||
933 | filename = g_strdup(qdict_get_try_str(options, "filename")); | |
934 | if (!filename) { | |
935 | return -EINVAL; | |
936 | } | |
937 | qdict_del(options, "filename"); | |
938 | ||
939 | qemu_rbd_parse_filename(filename, options, NULL); | |
940 | ||
941 | /* keypairs freed by caller */ | |
942 | *keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs")); | |
943 | if (*keypairs) { | |
944 | qdict_del(options, "=keyvalue-pairs"); | |
945 | } | |
946 | ||
947 | r = qemu_rbd_convert_options(options, opts, NULL); | |
948 | ||
949 | g_free(filename); | |
950 | return r; | |
951 | } | |
952 | ||
3d9136f9 KW |
953 | static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags, |
954 | Error **errp) | |
955 | { | |
956 | BDRVRBDState *s = bs->opaque; | |
4bfb2741 | 957 | BlockdevOptionsRbd *opts = NULL; |
bfb15b4b | 958 | const QDictEntry *e; |
3d9136f9 | 959 | Error *local_err = NULL; |
4ff45049 | 960 | char *keypairs, *secretid; |
3d9136f9 KW |
961 | int r; |
962 | ||
4ff45049 KW |
963 | keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs")); |
964 | if (keypairs) { | |
965 | qdict_del(options, "=keyvalue-pairs"); | |
966 | } | |
967 | ||
968 | secretid = g_strdup(qdict_get_try_str(options, "password-secret")); | |
969 | if (secretid) { | |
970 | qdict_del(options, "password-secret"); | |
971 | } | |
972 | ||
f24b03b5 | 973 | r = qemu_rbd_convert_options(options, &opts, &local_err); |
4bfb2741 | 974 | if (local_err) { |
084d1d13 JC |
975 | /* If keypairs are present, that means some options are present in |
976 | * the modern option format. Don't attempt to parse legacy option | |
977 | * formats, as we won't support mixed usage. */ | |
978 | if (keypairs) { | |
979 | error_propagate(errp, local_err); | |
980 | goto out; | |
981 | } | |
982 | ||
983 | /* If the initial attempt to convert and process the options failed, | |
984 | * we may be attempting to open an image file that has the rbd options | |
985 | * specified in the older format consisting of all key/value pairs | |
986 | * encoded in the filename. Go ahead and attempt to parse the | |
987 | * filename, and see if we can pull out the required options. */ | |
988 | r = qemu_rbd_attempt_legacy_options(options, &opts, &keypairs); | |
989 | if (r < 0) { | |
990 | /* Propagate the original error, not the legacy parsing fallback | |
991 | * error, as the latter was just a best-effort attempt. */ | |
992 | error_propagate(errp, local_err); | |
993 | goto out; | |
994 | } | |
995 | /* Take care whenever deciding to actually deprecate; once this ability | |
996 | * is removed, we will not be able to open any images with legacy-styled | |
997 | * backing image strings. */ | |
5197f445 MA |
998 | warn_report("RBD options encoded in the filename as keyvalue pairs " |
999 | "is deprecated"); | |
4bfb2741 KW |
1000 | } |
1001 | ||
bfb15b4b JC |
1002 | /* Remove the processed options from the QDict (the visitor processes |
1003 | * _all_ options in the QDict) */ | |
1004 | while ((e = qdict_first(options))) { | |
1005 | qdict_del(options, e->key); | |
1006 | } | |
1007 | ||
d41a5588 KW |
1008 | r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts, |
1009 | !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp); | |
3d9136f9 | 1010 | if (r < 0) { |
4ff45049 | 1011 | goto out; |
3d9136f9 KW |
1012 | } |
1013 | ||
d41a5588 KW |
1014 | s->snap = g_strdup(opts->snapshot); |
1015 | s->image_name = g_strdup(opts->image); | |
1016 | ||
e2b8247a | 1017 | /* rbd_open is always r/w */ |
80b61a27 | 1018 | r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap); |
f27aaf4b | 1019 | if (r < 0) { |
80b61a27 JC |
1020 | error_setg_errno(errp, -r, "error reading header from %s", |
1021 | s->image_name); | |
eb93d5d9 | 1022 | goto failed_open; |
f27aaf4b CB |
1023 | } |
1024 | ||
42e4ac9e OO |
1025 | if (opts->has_encrypt) { |
1026 | #ifdef LIBRBD_SUPPORTS_ENCRYPTION | |
1027 | r = qemu_rbd_encryption_load(s->image, opts->encrypt, errp); | |
1028 | if (r < 0) { | |
1029 | goto failed_post_open; | |
1030 | } | |
1031 | #else | |
1032 | r = -ENOTSUP; | |
1033 | error_setg(errp, "RBD library does not support image encryption"); | |
1034 | goto failed_post_open; | |
1035 | #endif | |
1036 | } | |
1037 | ||
d24f8023 SG |
1038 | r = rbd_get_size(s->image, &s->image_size); |
1039 | if (r < 0) { | |
1040 | error_setg_errno(errp, -r, "error getting image size from %s", | |
1041 | s->image_name); | |
42e4ac9e | 1042 | goto failed_post_open; |
d24f8023 SG |
1043 | } |
1044 | ||
e2b8247a JC |
1045 | /* If we are using an rbd snapshot, we must be r/o, otherwise |
1046 | * leave as-is */ | |
1047 | if (s->snap != NULL) { | |
eaa2410f KW |
1048 | r = bdrv_apply_auto_read_only(bs, "rbd snapshots are read-only", errp); |
1049 | if (r < 0) { | |
42e4ac9e | 1050 | goto failed_post_open; |
e2b8247a JC |
1051 | } |
1052 | } | |
f27aaf4b | 1053 | |
2f98910d EB |
1054 | /* When extending regular files, we get zeros from the OS */ |
1055 | bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE; | |
1056 | ||
4ff45049 KW |
1057 | r = 0; |
1058 | goto out; | |
f27aaf4b | 1059 | |
42e4ac9e OO |
1060 | failed_post_open: |
1061 | rbd_close(s->image); | |
eb93d5d9 | 1062 | failed_open: |
ad32e9c0 | 1063 | rados_ioctx_destroy(s->io_ctx); |
eb93d5d9 | 1064 | g_free(s->snap); |
80b61a27 | 1065 | g_free(s->image_name); |
3d9136f9 | 1066 | rados_shutdown(s->cluster); |
4ff45049 | 1067 | out: |
4bfb2741 | 1068 | qapi_free_BlockdevOptionsRbd(opts); |
4ff45049 KW |
1069 | g_free(keypairs); |
1070 | g_free(secretid); | |
f27aaf4b CB |
1071 | return r; |
1072 | } | |
1073 | ||
56e7cf8d JC |
1074 | |
1075 | /* Since RBD is currently always opened R/W via the API, | |
1076 | * we just need to check if we are using a snapshot or not, in | |
1077 | * order to determine if we will allow it to be R/W */ | |
1078 | static int qemu_rbd_reopen_prepare(BDRVReopenState *state, | |
1079 | BlockReopenQueue *queue, Error **errp) | |
1080 | { | |
1081 | BDRVRBDState *s = state->bs->opaque; | |
1082 | int ret = 0; | |
1083 | ||
1084 | if (s->snap && state->flags & BDRV_O_RDWR) { | |
1085 | error_setg(errp, | |
1086 | "Cannot change node '%s' to r/w when using RBD snapshot", | |
1087 | bdrv_get_device_or_node_name(state->bs)); | |
1088 | ret = -EINVAL; | |
1089 | } | |
1090 | ||
1091 | return ret; | |
1092 | } | |
1093 | ||
ad32e9c0 | 1094 | static void qemu_rbd_close(BlockDriverState *bs) |
f27aaf4b CB |
1095 | { |
1096 | BDRVRBDState *s = bs->opaque; | |
1097 | ||
ad32e9c0 JD |
1098 | rbd_close(s->image); |
1099 | rados_ioctx_destroy(s->io_ctx); | |
7267c094 | 1100 | g_free(s->snap); |
80b61a27 | 1101 | g_free(s->image_name); |
ad32e9c0 | 1102 | rados_shutdown(s->cluster); |
f27aaf4b CB |
1103 | } |
1104 | ||
d24f8023 SG |
1105 | /* Resize the RBD image and update the 'image_size' with the current size */ |
1106 | static int qemu_rbd_resize(BlockDriverState *bs, uint64_t size) | |
1107 | { | |
1108 | BDRVRBDState *s = bs->opaque; | |
1109 | int r; | |
1110 | ||
1111 | r = rbd_resize(s->image, size); | |
1112 | if (r < 0) { | |
1113 | return r; | |
1114 | } | |
1115 | ||
1116 | s->image_size = size; | |
1117 | ||
1118 | return 0; | |
1119 | } | |
1120 | ||
d7331bed | 1121 | static const AIOCBInfo rbd_aiocb_info = { |
f27aaf4b | 1122 | .aiocb_size = sizeof(RBDAIOCB), |
f27aaf4b CB |
1123 | }; |
1124 | ||
e04fb07f | 1125 | static void rbd_finish_bh(void *opaque) |
f27aaf4b | 1126 | { |
e04fb07f | 1127 | RADOSCB *rcb = opaque; |
e04fb07f | 1128 | qemu_rbd_complete_aio(rcb); |
ad32e9c0 JD |
1129 | } |
1130 | ||
1131 | /* | |
1132 | * This is the callback function for rbd_aio_read and _write | |
1133 | * | |
1134 | * Note: this function is being called from a non qemu thread so | |
1135 | * we need to be careful about what we do here. Generally we only | |
e04fb07f SH |
1136 | * schedule a BH, and do the rest of the io completion handling |
1137 | * from rbd_finish_bh() which runs in a qemu context. | |
ad32e9c0 JD |
1138 | */ |
1139 | static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb) | |
1140 | { | |
e04fb07f SH |
1141 | RBDAIOCB *acb = rcb->acb; |
1142 | ||
ad32e9c0 JD |
1143 | rcb->ret = rbd_aio_get_return_value(c); |
1144 | rbd_aio_release(c); | |
f27aaf4b | 1145 | |
e4ec5ad4 PD |
1146 | replay_bh_schedule_oneshot_event(bdrv_get_aio_context(acb->common.bs), |
1147 | rbd_finish_bh, rcb); | |
f27aaf4b CB |
1148 | } |
1149 | ||
7c84b1b8 | 1150 | static BlockAIOCB *rbd_start_aio(BlockDriverState *bs, |
7bbca9e2 | 1151 | int64_t off, |
7c84b1b8 | 1152 | QEMUIOVector *qiov, |
7bbca9e2 | 1153 | int64_t size, |
097310b5 | 1154 | BlockCompletionFunc *cb, |
7c84b1b8 MA |
1155 | void *opaque, |
1156 | RBDAIOCmd cmd) | |
f27aaf4b CB |
1157 | { |
1158 | RBDAIOCB *acb; | |
0f7a0237 | 1159 | RADOSCB *rcb = NULL; |
ad32e9c0 | 1160 | rbd_completion_t c; |
51a13528 | 1161 | int r; |
f27aaf4b CB |
1162 | |
1163 | BDRVRBDState *s = bs->opaque; | |
1164 | ||
d7331bed | 1165 | acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque); |
787f3133 | 1166 | acb->cmd = cmd; |
f27aaf4b | 1167 | acb->qiov = qiov; |
7bbca9e2 | 1168 | assert(!qiov || qiov->size == size); |
1d393bde | 1169 | |
1170 | rcb = g_new(RADOSCB, 1); | |
1171 | ||
f27aaf4b CB |
1172 | acb->ret = 0; |
1173 | acb->error = 0; | |
1174 | acb->s = s; | |
f27aaf4b | 1175 | |
ad32e9c0 | 1176 | rcb->acb = acb; |
ad32e9c0 JD |
1177 | rcb->s = acb->s; |
1178 | rcb->size = size; | |
51a13528 JD |
1179 | r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c); |
1180 | if (r < 0) { | |
1181 | goto failed; | |
1182 | } | |
f27aaf4b | 1183 | |
787f3133 | 1184 | switch (cmd) { |
48672ac0 | 1185 | case RBD_AIO_WRITE: |
d24f8023 SG |
1186 | /* |
1187 | * RBD APIs don't allow us to write more than actual size, so in order | |
1188 | * to support growing images, we resize the image before write | |
1189 | * operations that exceed the current size. | |
1190 | */ | |
1191 | if (off + size > s->image_size) { | |
1192 | r = qemu_rbd_resize(bs, off + size); | |
1193 | if (r < 0) { | |
1194 | goto failed_completion; | |
1195 | } | |
1196 | } | |
48672ac0 | 1197 | r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c); |
787f3133 JD |
1198 | break; |
1199 | case RBD_AIO_READ: | |
48672ac0 | 1200 | r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c); |
787f3133 JD |
1201 | break; |
1202 | case RBD_AIO_DISCARD: | |
48672ac0 | 1203 | r = rbd_aio_discard(s->image, off, size, c); |
787f3133 | 1204 | break; |
dc7588c1 | 1205 | case RBD_AIO_FLUSH: |
48672ac0 | 1206 | r = rbd_aio_flush(s->image, c); |
dc7588c1 | 1207 | break; |
787f3133 JD |
1208 | default: |
1209 | r = -EINVAL; | |
51a13528 JD |
1210 | } |
1211 | ||
1212 | if (r < 0) { | |
405a2764 | 1213 | goto failed_completion; |
f27aaf4b | 1214 | } |
f27aaf4b | 1215 | return &acb->common; |
51a13528 | 1216 | |
405a2764 KW |
1217 | failed_completion: |
1218 | rbd_aio_release(c); | |
51a13528 | 1219 | failed: |
7267c094 | 1220 | g_free(rcb); |
1d393bde | 1221 | |
8007429a | 1222 | qemu_aio_unref(acb); |
51a13528 | 1223 | return NULL; |
f27aaf4b CB |
1224 | } |
1225 | ||
e8e16d4b EB |
1226 | static BlockAIOCB *qemu_rbd_aio_preadv(BlockDriverState *bs, |
1227 | uint64_t offset, uint64_t bytes, | |
1228 | QEMUIOVector *qiov, int flags, | |
1229 | BlockCompletionFunc *cb, | |
1230 | void *opaque) | |
f27aaf4b | 1231 | { |
e8e16d4b | 1232 | return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque, |
787f3133 | 1233 | RBD_AIO_READ); |
f27aaf4b CB |
1234 | } |
1235 | ||
e8e16d4b EB |
1236 | static BlockAIOCB *qemu_rbd_aio_pwritev(BlockDriverState *bs, |
1237 | uint64_t offset, uint64_t bytes, | |
1238 | QEMUIOVector *qiov, int flags, | |
1239 | BlockCompletionFunc *cb, | |
1240 | void *opaque) | |
f27aaf4b | 1241 | { |
e8e16d4b | 1242 | return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque, |
787f3133 | 1243 | RBD_AIO_WRITE); |
f27aaf4b CB |
1244 | } |
1245 | ||
7c84b1b8 | 1246 | static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs, |
097310b5 | 1247 | BlockCompletionFunc *cb, |
7c84b1b8 | 1248 | void *opaque) |
dc7588c1 JD |
1249 | { |
1250 | return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH); | |
1251 | } | |
1252 | ||
ad32e9c0 | 1253 | static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi) |
f27aaf4b CB |
1254 | { |
1255 | BDRVRBDState *s = bs->opaque; | |
ad32e9c0 JD |
1256 | rbd_image_info_t info; |
1257 | int r; | |
1258 | ||
1259 | r = rbd_stat(s->image, &info, sizeof(info)); | |
1260 | if (r < 0) { | |
1261 | return r; | |
1262 | } | |
1263 | ||
1264 | bdi->cluster_size = info.obj_size; | |
f27aaf4b CB |
1265 | return 0; |
1266 | } | |
1267 | ||
42e4ac9e OO |
1268 | static ImageInfoSpecific *qemu_rbd_get_specific_info(BlockDriverState *bs, |
1269 | Error **errp) | |
1270 | { | |
1271 | BDRVRBDState *s = bs->opaque; | |
1272 | ImageInfoSpecific *spec_info; | |
1273 | char buf[RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN] = {0}; | |
1274 | int r; | |
1275 | ||
1276 | if (s->image_size >= RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN) { | |
1277 | r = rbd_read(s->image, 0, | |
1278 | RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN, buf); | |
1279 | if (r < 0) { | |
1280 | error_setg_errno(errp, -r, "cannot read image start for probe"); | |
1281 | return NULL; | |
1282 | } | |
1283 | } | |
1284 | ||
1285 | spec_info = g_new(ImageInfoSpecific, 1); | |
1286 | *spec_info = (ImageInfoSpecific){ | |
1287 | .type = IMAGE_INFO_SPECIFIC_KIND_RBD, | |
1288 | .u.rbd.data = g_new0(ImageInfoSpecificRbd, 1), | |
1289 | }; | |
1290 | ||
1291 | if (memcmp(buf, rbd_luks_header_verification, | |
1292 | RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN) == 0) { | |
1293 | spec_info->u.rbd.data->encryption_format = | |
1294 | RBD_IMAGE_ENCRYPTION_FORMAT_LUKS; | |
1295 | spec_info->u.rbd.data->has_encryption_format = true; | |
1296 | } else if (memcmp(buf, rbd_luks2_header_verification, | |
1297 | RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN) == 0) { | |
1298 | spec_info->u.rbd.data->encryption_format = | |
1299 | RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2; | |
1300 | spec_info->u.rbd.data->has_encryption_format = true; | |
1301 | } else { | |
1302 | spec_info->u.rbd.data->has_encryption_format = false; | |
1303 | } | |
1304 | ||
1305 | return spec_info; | |
1306 | } | |
1307 | ||
ad32e9c0 | 1308 | static int64_t qemu_rbd_getlength(BlockDriverState *bs) |
f27aaf4b CB |
1309 | { |
1310 | BDRVRBDState *s = bs->opaque; | |
ad32e9c0 JD |
1311 | rbd_image_info_t info; |
1312 | int r; | |
f27aaf4b | 1313 | |
ad32e9c0 JD |
1314 | r = rbd_stat(s->image, &info, sizeof(info)); |
1315 | if (r < 0) { | |
1316 | return r; | |
1317 | } | |
1318 | ||
1319 | return info.size; | |
f27aaf4b CB |
1320 | } |
1321 | ||
061ca8a3 KW |
1322 | static int coroutine_fn qemu_rbd_co_truncate(BlockDriverState *bs, |
1323 | int64_t offset, | |
c80d8b06 | 1324 | bool exact, |
061ca8a3 | 1325 | PreallocMode prealloc, |
92b92799 | 1326 | BdrvRequestFlags flags, |
061ca8a3 | 1327 | Error **errp) |
30cdc48c | 1328 | { |
30cdc48c JD |
1329 | int r; |
1330 | ||
8243ccb7 HR |
1331 | if (prealloc != PREALLOC_MODE_OFF) { |
1332 | error_setg(errp, "Unsupported preallocation mode '%s'", | |
977c736f | 1333 | PreallocMode_str(prealloc)); |
8243ccb7 HR |
1334 | return -ENOTSUP; |
1335 | } | |
1336 | ||
d24f8023 | 1337 | r = qemu_rbd_resize(bs, offset); |
30cdc48c | 1338 | if (r < 0) { |
f59adb32 | 1339 | error_setg_errno(errp, -r, "Failed to resize file"); |
30cdc48c JD |
1340 | return r; |
1341 | } | |
1342 | ||
1343 | return 0; | |
1344 | } | |
1345 | ||
ad32e9c0 JD |
1346 | static int qemu_rbd_snap_create(BlockDriverState *bs, |
1347 | QEMUSnapshotInfo *sn_info) | |
f27aaf4b CB |
1348 | { |
1349 | BDRVRBDState *s = bs->opaque; | |
f27aaf4b | 1350 | int r; |
f27aaf4b CB |
1351 | |
1352 | if (sn_info->name[0] == '\0') { | |
1353 | return -EINVAL; /* we need a name for rbd snapshots */ | |
1354 | } | |
1355 | ||
1356 | /* | |
1357 | * rbd snapshots are using the name as the user controlled unique identifier | |
1358 | * we can't use the rbd snapid for that purpose, as it can't be set | |
1359 | */ | |
1360 | if (sn_info->id_str[0] != '\0' && | |
1361 | strcmp(sn_info->id_str, sn_info->name) != 0) { | |
1362 | return -EINVAL; | |
1363 | } | |
1364 | ||
1365 | if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) { | |
1366 | return -ERANGE; | |
1367 | } | |
1368 | ||
ad32e9c0 | 1369 | r = rbd_snap_create(s->image, sn_info->name); |
f27aaf4b | 1370 | if (r < 0) { |
ad32e9c0 | 1371 | error_report("failed to create snap: %s", strerror(-r)); |
f27aaf4b CB |
1372 | return r; |
1373 | } | |
1374 | ||
f27aaf4b CB |
1375 | return 0; |
1376 | } | |
1377 | ||
bd603247 | 1378 | static int qemu_rbd_snap_remove(BlockDriverState *bs, |
a89d89d3 WX |
1379 | const char *snapshot_id, |
1380 | const char *snapshot_name, | |
1381 | Error **errp) | |
bd603247 GF |
1382 | { |
1383 | BDRVRBDState *s = bs->opaque; | |
1384 | int r; | |
1385 | ||
a89d89d3 WX |
1386 | if (!snapshot_name) { |
1387 | error_setg(errp, "rbd need a valid snapshot name"); | |
1388 | return -EINVAL; | |
1389 | } | |
1390 | ||
1391 | /* If snapshot_id is specified, it must be equal to name, see | |
1392 | qemu_rbd_snap_list() */ | |
1393 | if (snapshot_id && strcmp(snapshot_id, snapshot_name)) { | |
1394 | error_setg(errp, | |
1395 | "rbd do not support snapshot id, it should be NULL or " | |
1396 | "equal to snapshot name"); | |
1397 | return -EINVAL; | |
1398 | } | |
1399 | ||
bd603247 | 1400 | r = rbd_snap_remove(s->image, snapshot_name); |
a89d89d3 WX |
1401 | if (r < 0) { |
1402 | error_setg_errno(errp, -r, "Failed to remove the snapshot"); | |
1403 | } | |
bd603247 GF |
1404 | return r; |
1405 | } | |
1406 | ||
1407 | static int qemu_rbd_snap_rollback(BlockDriverState *bs, | |
1408 | const char *snapshot_name) | |
1409 | { | |
1410 | BDRVRBDState *s = bs->opaque; | |
bd603247 | 1411 | |
9be38598 | 1412 | return rbd_snap_rollback(s->image, snapshot_name); |
bd603247 GF |
1413 | } |
1414 | ||
ad32e9c0 JD |
1415 | static int qemu_rbd_snap_list(BlockDriverState *bs, |
1416 | QEMUSnapshotInfo **psn_tab) | |
f27aaf4b CB |
1417 | { |
1418 | BDRVRBDState *s = bs->opaque; | |
f27aaf4b | 1419 | QEMUSnapshotInfo *sn_info, *sn_tab = NULL; |
ad32e9c0 JD |
1420 | int i, snap_count; |
1421 | rbd_snap_info_t *snaps; | |
1422 | int max_snaps = RBD_MAX_SNAPS; | |
f27aaf4b | 1423 | |
ad32e9c0 | 1424 | do { |
02c4f26b | 1425 | snaps = g_new(rbd_snap_info_t, max_snaps); |
ad32e9c0 | 1426 | snap_count = rbd_snap_list(s->image, snaps, &max_snaps); |
9e6337d0 | 1427 | if (snap_count <= 0) { |
7267c094 | 1428 | g_free(snaps); |
f27aaf4b | 1429 | } |
ad32e9c0 | 1430 | } while (snap_count == -ERANGE); |
f27aaf4b | 1431 | |
ad32e9c0 | 1432 | if (snap_count <= 0) { |
b9c53290 | 1433 | goto done; |
f27aaf4b CB |
1434 | } |
1435 | ||
5839e53b | 1436 | sn_tab = g_new0(QEMUSnapshotInfo, snap_count); |
f27aaf4b | 1437 | |
ad32e9c0 JD |
1438 | for (i = 0; i < snap_count; i++) { |
1439 | const char *snap_name = snaps[i].name; | |
f27aaf4b CB |
1440 | |
1441 | sn_info = sn_tab + i; | |
1442 | pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name); | |
1443 | pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name); | |
f27aaf4b | 1444 | |
ad32e9c0 | 1445 | sn_info->vm_state_size = snaps[i].size; |
f27aaf4b CB |
1446 | sn_info->date_sec = 0; |
1447 | sn_info->date_nsec = 0; | |
1448 | sn_info->vm_clock_nsec = 0; | |
1449 | } | |
ad32e9c0 | 1450 | rbd_snap_list_end(snaps); |
9e6337d0 | 1451 | g_free(snaps); |
ad32e9c0 | 1452 | |
b9c53290 | 1453 | done: |
f27aaf4b | 1454 | *psn_tab = sn_tab; |
f27aaf4b | 1455 | return snap_count; |
f27aaf4b CB |
1456 | } |
1457 | ||
4da444a0 EB |
1458 | static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs, |
1459 | int64_t offset, | |
f5a5ca79 | 1460 | int bytes, |
4da444a0 EB |
1461 | BlockCompletionFunc *cb, |
1462 | void *opaque) | |
787f3133 | 1463 | { |
f5a5ca79 | 1464 | return rbd_start_aio(bs, offset, NULL, bytes, cb, opaque, |
787f3133 JD |
1465 | RBD_AIO_DISCARD); |
1466 | } | |
787f3133 | 1467 | |
2b148f39 PB |
1468 | static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs, |
1469 | Error **errp) | |
be217884 AC |
1470 | { |
1471 | BDRVRBDState *s = bs->opaque; | |
1472 | int r = rbd_invalidate_cache(s->image); | |
1473 | if (r < 0) { | |
1474 | error_setg_errno(errp, -r, "Failed to invalidate the cache"); | |
1475 | } | |
1476 | } | |
be217884 | 1477 | |
bd0cf596 CL |
1478 | static QemuOptsList qemu_rbd_create_opts = { |
1479 | .name = "rbd-create-opts", | |
1480 | .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head), | |
1481 | .desc = { | |
1482 | { | |
1483 | .name = BLOCK_OPT_SIZE, | |
1484 | .type = QEMU_OPT_SIZE, | |
1485 | .help = "Virtual disk size" | |
1486 | }, | |
1487 | { | |
1488 | .name = BLOCK_OPT_CLUSTER_SIZE, | |
1489 | .type = QEMU_OPT_SIZE, | |
1490 | .help = "RBD object size" | |
1491 | }, | |
60390a21 DB |
1492 | { |
1493 | .name = "password-secret", | |
1494 | .type = QEMU_OPT_STRING, | |
1495 | .help = "ID of secret providing the password", | |
1496 | }, | |
42e4ac9e OO |
1497 | { |
1498 | .name = "encrypt.format", | |
1499 | .type = QEMU_OPT_STRING, | |
1500 | .help = "Encrypt the image, format choices: 'luks', 'luks2'", | |
1501 | }, | |
1502 | { | |
1503 | .name = "encrypt.cipher-alg", | |
1504 | .type = QEMU_OPT_STRING, | |
1505 | .help = "Name of encryption cipher algorithm" | |
1506 | " (allowed values: aes-128, aes-256)", | |
1507 | }, | |
1508 | { | |
1509 | .name = "encrypt.key-secret", | |
1510 | .type = QEMU_OPT_STRING, | |
1511 | .help = "ID of secret providing LUKS passphrase", | |
1512 | }, | |
bd0cf596 CL |
1513 | { /* end of list */ } |
1514 | } | |
f27aaf4b CB |
1515 | }; |
1516 | ||
2654267c HR |
1517 | static const char *const qemu_rbd_strong_runtime_opts[] = { |
1518 | "pool", | |
7bae7c80 | 1519 | "namespace", |
2654267c HR |
1520 | "image", |
1521 | "conf", | |
1522 | "snapshot", | |
1523 | "user", | |
1524 | "server.", | |
1525 | "password-secret", | |
1526 | ||
1527 | NULL | |
1528 | }; | |
1529 | ||
f27aaf4b | 1530 | static BlockDriver bdrv_rbd = { |
c7cacb3e JC |
1531 | .format_name = "rbd", |
1532 | .instance_size = sizeof(BDRVRBDState), | |
1533 | .bdrv_parse_filename = qemu_rbd_parse_filename, | |
e8e16d4b | 1534 | .bdrv_refresh_limits = qemu_rbd_refresh_limits, |
c7cacb3e JC |
1535 | .bdrv_file_open = qemu_rbd_open, |
1536 | .bdrv_close = qemu_rbd_close, | |
56e7cf8d | 1537 | .bdrv_reopen_prepare = qemu_rbd_reopen_prepare, |
1bebea37 | 1538 | .bdrv_co_create = qemu_rbd_co_create, |
efc75e2a | 1539 | .bdrv_co_create_opts = qemu_rbd_co_create_opts, |
c7cacb3e JC |
1540 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
1541 | .bdrv_get_info = qemu_rbd_getinfo, | |
42e4ac9e | 1542 | .bdrv_get_specific_info = qemu_rbd_get_specific_info, |
c7cacb3e JC |
1543 | .create_opts = &qemu_rbd_create_opts, |
1544 | .bdrv_getlength = qemu_rbd_getlength, | |
061ca8a3 | 1545 | .bdrv_co_truncate = qemu_rbd_co_truncate, |
c7cacb3e | 1546 | .protocol_name = "rbd", |
f27aaf4b | 1547 | |
e8e16d4b EB |
1548 | .bdrv_aio_preadv = qemu_rbd_aio_preadv, |
1549 | .bdrv_aio_pwritev = qemu_rbd_aio_pwritev, | |
dc7588c1 | 1550 | |
dc7588c1 | 1551 | .bdrv_aio_flush = qemu_rbd_aio_flush, |
4da444a0 | 1552 | .bdrv_aio_pdiscard = qemu_rbd_aio_pdiscard, |
787f3133 | 1553 | |
c68b89ac | 1554 | .bdrv_snapshot_create = qemu_rbd_snap_create, |
bd603247 | 1555 | .bdrv_snapshot_delete = qemu_rbd_snap_remove, |
c68b89ac | 1556 | .bdrv_snapshot_list = qemu_rbd_snap_list, |
bd603247 | 1557 | .bdrv_snapshot_goto = qemu_rbd_snap_rollback, |
2b148f39 | 1558 | .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache, |
2654267c HR |
1559 | |
1560 | .strong_runtime_opts = qemu_rbd_strong_runtime_opts, | |
f27aaf4b CB |
1561 | }; |
1562 | ||
1563 | static void bdrv_rbd_init(void) | |
1564 | { | |
1565 | bdrv_register(&bdrv_rbd); | |
1566 | } | |
1567 | ||
1568 | block_init(bdrv_rbd_init); |