]>
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 | ||
787f3133 JD |
58 | /* rbd_aio_discard added in 0.1.2 */ |
59 | #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2) | |
60 | #define LIBRBD_SUPPORTS_DISCARD | |
61 | #else | |
62 | #undef LIBRBD_SUPPORTS_DISCARD | |
63 | #endif | |
64 | ||
f27aaf4b CB |
65 | #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER) |
66 | ||
ad32e9c0 JD |
67 | #define RBD_MAX_SNAPS 100 |
68 | ||
1d393bde | 69 | /* The LIBRBD_SUPPORTS_IOVEC is defined in librbd.h */ |
70 | #ifdef LIBRBD_SUPPORTS_IOVEC | |
71 | #define LIBRBD_USE_IOVEC 1 | |
72 | #else | |
73 | #define LIBRBD_USE_IOVEC 0 | |
74 | #endif | |
75 | ||
787f3133 JD |
76 | typedef enum { |
77 | RBD_AIO_READ, | |
78 | RBD_AIO_WRITE, | |
dc7588c1 JD |
79 | RBD_AIO_DISCARD, |
80 | RBD_AIO_FLUSH | |
787f3133 JD |
81 | } RBDAIOCmd; |
82 | ||
f27aaf4b | 83 | typedef struct RBDAIOCB { |
7c84b1b8 | 84 | BlockAIOCB common; |
08448d51 | 85 | int64_t ret; |
f27aaf4b CB |
86 | QEMUIOVector *qiov; |
87 | char *bounce; | |
787f3133 | 88 | RBDAIOCmd cmd; |
f27aaf4b CB |
89 | int error; |
90 | struct BDRVRBDState *s; | |
f27aaf4b CB |
91 | } RBDAIOCB; |
92 | ||
93 | typedef struct RADOSCB { | |
f27aaf4b CB |
94 | RBDAIOCB *acb; |
95 | struct BDRVRBDState *s; | |
ad32e9c0 | 96 | int64_t size; |
f27aaf4b | 97 | char *buf; |
08448d51 | 98 | int64_t ret; |
f27aaf4b CB |
99 | } RADOSCB; |
100 | ||
f27aaf4b | 101 | typedef struct BDRVRBDState { |
ad32e9c0 JD |
102 | rados_t cluster; |
103 | rados_ioctx_t io_ctx; | |
104 | rbd_image_t image; | |
80b61a27 | 105 | char *image_name; |
ad32e9c0 | 106 | char *snap; |
19ae9ae0 | 107 | char *namespace; |
d24f8023 | 108 | uint64_t image_size; |
f27aaf4b CB |
109 | } BDRVRBDState; |
110 | ||
aa045c2d KW |
111 | static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx, |
112 | BlockdevOptionsRbd *opts, bool cache, | |
113 | const char *keypairs, const char *secretid, | |
114 | Error **errp); | |
115 | ||
730b00bb | 116 | static char *qemu_rbd_next_tok(char *src, char delim, char **p) |
f27aaf4b | 117 | { |
f27aaf4b CB |
118 | char *end; |
119 | ||
120 | *p = NULL; | |
121 | ||
8efb339d | 122 | for (end = src; *end; ++end) { |
16a06b24 | 123 | if (*end == delim) { |
8efb339d MA |
124 | break; |
125 | } | |
126 | if (*end == '\\' && end[1] != '\0') { | |
127 | end++; | |
f27aaf4b CB |
128 | } |
129 | } | |
8efb339d MA |
130 | if (*end == delim) { |
131 | *p = end + 1; | |
132 | *end = '\0'; | |
133 | } | |
7830f909 | 134 | return src; |
f27aaf4b CB |
135 | } |
136 | ||
16a06b24 SW |
137 | static void qemu_rbd_unescape(char *src) |
138 | { | |
139 | char *p; | |
140 | ||
141 | for (p = src; *src; ++src, ++p) { | |
142 | if (*src == '\\' && src[1] != '\0') { | |
143 | src++; | |
144 | } | |
145 | *p = *src; | |
146 | } | |
147 | *p = '\0'; | |
148 | } | |
149 | ||
c7cacb3e JC |
150 | static void qemu_rbd_parse_filename(const char *filename, QDict *options, |
151 | Error **errp) | |
f27aaf4b CB |
152 | { |
153 | const char *start; | |
e98c6961 EB |
154 | char *p, *buf; |
155 | QList *keypairs = NULL; | |
19ae9ae0 | 156 | char *found_str, *image_name; |
f27aaf4b CB |
157 | |
158 | if (!strstart(filename, "rbd:", &start)) { | |
d61563b2 | 159 | error_setg(errp, "File name must start with 'rbd:'"); |
c7cacb3e | 160 | return; |
f27aaf4b CB |
161 | } |
162 | ||
7267c094 | 163 | buf = g_strdup(start); |
f27aaf4b CB |
164 | p = buf; |
165 | ||
730b00bb | 166 | found_str = qemu_rbd_next_tok(p, '/', &p); |
7830f909 | 167 | if (!p) { |
7830f909 | 168 | error_setg(errp, "Pool name is required"); |
f27aaf4b CB |
169 | goto done; |
170 | } | |
7830f909 | 171 | qemu_rbd_unescape(found_str); |
46f5ac20 | 172 | qdict_put_str(options, "pool", found_str); |
fab5cf59 JD |
173 | |
174 | if (strchr(p, '@')) { | |
19ae9ae0 | 175 | image_name = qemu_rbd_next_tok(p, '@', &p); |
7830f909 | 176 | |
730b00bb | 177 | found_str = qemu_rbd_next_tok(p, ':', &p); |
7830f909 | 178 | qemu_rbd_unescape(found_str); |
46f5ac20 | 179 | qdict_put_str(options, "snapshot", found_str); |
fab5cf59 | 180 | } else { |
19ae9ae0 FF |
181 | image_name = qemu_rbd_next_tok(p, ':', &p); |
182 | } | |
183 | /* Check for namespace in the image_name */ | |
184 | if (strchr(image_name, '/')) { | |
185 | found_str = qemu_rbd_next_tok(image_name, '/', &image_name); | |
7830f909 | 186 | qemu_rbd_unescape(found_str); |
19ae9ae0 FF |
187 | qdict_put_str(options, "namespace", found_str); |
188 | } else { | |
189 | qdict_put_str(options, "namespace", ""); | |
f27aaf4b | 190 | } |
19ae9ae0 FF |
191 | qemu_rbd_unescape(image_name); |
192 | qdict_put_str(options, "image", image_name); | |
7830f909 | 193 | if (!p) { |
f27aaf4b CB |
194 | goto done; |
195 | } | |
196 | ||
c7cacb3e JC |
197 | /* The following are essentially all key/value pairs, and we treat |
198 | * 'id' and 'conf' a bit special. Key/value pairs may be in any order. */ | |
199 | while (p) { | |
200 | char *name, *value; | |
730b00bb | 201 | name = qemu_rbd_next_tok(p, '=', &p); |
c7cacb3e JC |
202 | if (!p) { |
203 | error_setg(errp, "conf option %s has no value", name); | |
204 | break; | |
7c7e9df0 | 205 | } |
c7cacb3e JC |
206 | |
207 | qemu_rbd_unescape(name); | |
208 | ||
730b00bb | 209 | value = qemu_rbd_next_tok(p, ':', &p); |
c7cacb3e JC |
210 | qemu_rbd_unescape(value); |
211 | ||
212 | if (!strcmp(name, "conf")) { | |
46f5ac20 | 213 | qdict_put_str(options, "conf", value); |
c7cacb3e | 214 | } else if (!strcmp(name, "id")) { |
46f5ac20 | 215 | qdict_put_str(options, "user", value); |
c7cacb3e | 216 | } else { |
e98c6961 EB |
217 | /* |
218 | * We pass these internally to qemu_rbd_set_keypairs(), so | |
219 | * we can get away with the simpler list of [ "key1", | |
220 | * "value1", "key2", "value2" ] rather than a raw dict | |
221 | * { "key1": "value1", "key2": "value2" } where we can't | |
222 | * guarantee order, or even a more correct but complex | |
223 | * [ { "key1": "value1" }, { "key2": "value2" } ] | |
224 | */ | |
225 | if (!keypairs) { | |
226 | keypairs = qlist_new(); | |
c7cacb3e | 227 | } |
46f5ac20 EB |
228 | qlist_append_str(keypairs, name); |
229 | qlist_append_str(keypairs, value); | |
c7cacb3e | 230 | } |
7c7e9df0 | 231 | } |
c7cacb3e | 232 | |
e98c6961 EB |
233 | if (keypairs) { |
234 | qdict_put(options, "=keyvalue-pairs", | |
235 | qobject_to_json(QOBJECT(keypairs))); | |
c7cacb3e JC |
236 | } |
237 | ||
c7cacb3e | 238 | done: |
c7cacb3e | 239 | g_free(buf); |
cb3e7f08 | 240 | qobject_unref(keypairs); |
c7cacb3e | 241 | return; |
7c7e9df0 SW |
242 | } |
243 | ||
60390a21 | 244 | |
e8e16d4b EB |
245 | static void qemu_rbd_refresh_limits(BlockDriverState *bs, Error **errp) |
246 | { | |
247 | /* XXX Does RBD support AIO on less than 512-byte alignment? */ | |
248 | bs->bl.request_alignment = 512; | |
249 | } | |
250 | ||
251 | ||
d083f954 | 252 | static int qemu_rbd_set_auth(rados_t cluster, BlockdevOptionsRbd *opts, |
60390a21 DB |
253 | Error **errp) |
254 | { | |
d083f954 | 255 | char *key, *acr; |
a3699de4 MA |
256 | int r; |
257 | GString *accu; | |
258 | RbdAuthModeList *auth; | |
259 | ||
d083f954 MA |
260 | if (opts->key_secret) { |
261 | key = qcrypto_secret_lookup_as_base64(opts->key_secret, errp); | |
262 | if (!key) { | |
263 | return -EIO; | |
264 | } | |
265 | r = rados_conf_set(cluster, "key", key); | |
266 | g_free(key); | |
267 | if (r < 0) { | |
268 | error_setg_errno(errp, -r, "Could not set 'key'"); | |
269 | return r; | |
a3699de4 | 270 | } |
60390a21 DB |
271 | } |
272 | ||
a3699de4 MA |
273 | if (opts->has_auth_client_required) { |
274 | accu = g_string_new(""); | |
275 | for (auth = opts->auth_client_required; auth; auth = auth->next) { | |
276 | if (accu->str[0]) { | |
277 | g_string_append_c(accu, ';'); | |
278 | } | |
279 | g_string_append(accu, RbdAuthMode_str(auth->value)); | |
280 | } | |
281 | acr = g_string_free(accu, FALSE); | |
282 | r = rados_conf_set(cluster, "auth_client_required", acr); | |
283 | g_free(acr); | |
284 | if (r < 0) { | |
285 | error_setg_errno(errp, -r, | |
286 | "Could not set 'auth_client_required'"); | |
287 | return r; | |
288 | } | |
289 | } | |
60390a21 DB |
290 | |
291 | return 0; | |
292 | } | |
293 | ||
e98c6961 | 294 | static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json, |
c7cacb3e | 295 | Error **errp) |
fab5cf59 | 296 | { |
e98c6961 EB |
297 | QList *keypairs; |
298 | QString *name; | |
299 | QString *value; | |
300 | const char *key; | |
301 | size_t remaining; | |
fab5cf59 JD |
302 | int ret = 0; |
303 | ||
e98c6961 EB |
304 | if (!keypairs_json) { |
305 | return ret; | |
306 | } | |
7dc847eb HR |
307 | keypairs = qobject_to(QList, |
308 | qobject_from_json(keypairs_json, &error_abort)); | |
e98c6961 EB |
309 | remaining = qlist_size(keypairs) / 2; |
310 | assert(remaining); | |
311 | ||
312 | while (remaining--) { | |
7dc847eb HR |
313 | name = qobject_to(QString, qlist_pop(keypairs)); |
314 | value = qobject_to(QString, qlist_pop(keypairs)); | |
e98c6961 EB |
315 | assert(name && value); |
316 | key = qstring_get_str(name); | |
317 | ||
318 | ret = rados_conf_set(cluster, key, qstring_get_str(value)); | |
cb3e7f08 | 319 | qobject_unref(value); |
c7cacb3e | 320 | if (ret < 0) { |
e98c6961 | 321 | error_setg_errno(errp, -ret, "invalid conf option %s", key); |
cb3e7f08 | 322 | qobject_unref(name); |
c7cacb3e JC |
323 | ret = -EINVAL; |
324 | break; | |
fab5cf59 | 325 | } |
cb3e7f08 | 326 | qobject_unref(name); |
fab5cf59 JD |
327 | } |
328 | ||
cb3e7f08 | 329 | qobject_unref(keypairs); |
fab5cf59 JD |
330 | return ret; |
331 | } | |
332 | ||
1d393bde | 333 | static void qemu_rbd_memset(RADOSCB *rcb, int64_t offs) |
334 | { | |
335 | if (LIBRBD_USE_IOVEC) { | |
336 | RBDAIOCB *acb = rcb->acb; | |
337 | iov_memset(acb->qiov->iov, acb->qiov->niov, offs, 0, | |
338 | acb->qiov->size - offs); | |
339 | } else { | |
340 | memset(rcb->buf + offs, 0, rcb->size - offs); | |
341 | } | |
342 | } | |
343 | ||
0f9d252d JC |
344 | static QemuOptsList runtime_opts = { |
345 | .name = "rbd", | |
346 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
347 | .desc = { | |
348 | { | |
cbf036b4 | 349 | .name = "pool", |
0f9d252d | 350 | .type = QEMU_OPT_STRING, |
cbf036b4 | 351 | .help = "Rados pool name", |
0f9d252d | 352 | }, |
19ae9ae0 FF |
353 | { |
354 | .name = "namespace", | |
355 | .type = QEMU_OPT_STRING, | |
356 | .help = "Rados namespace name in the pool", | |
357 | }, | |
0f9d252d | 358 | { |
cbf036b4 | 359 | .name = "image", |
0f9d252d | 360 | .type = QEMU_OPT_STRING, |
cbf036b4 | 361 | .help = "Image name in the pool", |
0f9d252d JC |
362 | }, |
363 | { | |
364 | .name = "conf", | |
365 | .type = QEMU_OPT_STRING, | |
366 | .help = "Rados config file location", | |
367 | }, | |
0f9d252d JC |
368 | { |
369 | .name = "snapshot", | |
370 | .type = QEMU_OPT_STRING, | |
371 | .help = "Ceph snapshot name", | |
372 | }, | |
373 | { | |
374 | /* maps to 'id' in rados_create() */ | |
375 | .name = "user", | |
376 | .type = QEMU_OPT_STRING, | |
377 | .help = "Rados id name", | |
378 | }, | |
cbf036b4 | 379 | /* |
2836284d | 380 | * server.* extracted manually, see qemu_rbd_mon_host() |
cbf036b4 | 381 | */ |
0f9d252d JC |
382 | { /* end of list */ } |
383 | }, | |
384 | }; | |
385 | ||
d083f954 | 386 | /* FIXME Deprecate and remove keypairs or make it available in QMP. */ |
1bebea37 KW |
387 | static int qemu_rbd_do_create(BlockdevCreateOptions *options, |
388 | const char *keypairs, const char *password_secret, | |
389 | Error **errp) | |
f27aaf4b | 390 | { |
1bebea37 | 391 | BlockdevCreateOptionsRbd *opts = &options->u.rbd; |
ad32e9c0 JD |
392 | rados_t cluster; |
393 | rados_ioctx_t io_ctx; | |
1bebea37 KW |
394 | int obj_order = 0; |
395 | int ret; | |
396 | ||
397 | assert(options->driver == BLOCKDEV_DRIVER_RBD); | |
398 | if (opts->location->has_snapshot) { | |
399 | error_setg(errp, "Can't use snapshot name for image creation"); | |
400 | return -EINVAL; | |
401 | } | |
f27aaf4b | 402 | |
1bebea37 KW |
403 | if (opts->has_cluster_size) { |
404 | int64_t objsize = opts->cluster_size; | |
bd0cf596 CL |
405 | if ((objsize - 1) & objsize) { /* not a power of 2? */ |
406 | error_setg(errp, "obj size needs to be power of 2"); | |
1bebea37 | 407 | return -EINVAL; |
bd0cf596 CL |
408 | } |
409 | if (objsize < 4096) { | |
410 | error_setg(errp, "obj size too small"); | |
1bebea37 | 411 | return -EINVAL; |
f27aaf4b | 412 | } |
786a4ea8 | 413 | obj_order = ctz32(objsize); |
f27aaf4b CB |
414 | } |
415 | ||
aa045c2d KW |
416 | ret = qemu_rbd_connect(&cluster, &io_ctx, opts->location, false, keypairs, |
417 | password_secret, errp); | |
87cd3d20 | 418 | if (ret < 0) { |
1bebea37 | 419 | return ret; |
f27aaf4b CB |
420 | } |
421 | ||
1bebea37 | 422 | ret = rbd_create(io_ctx, opts->location->image, opts->size, &obj_order); |
87cd3d20 VU |
423 | if (ret < 0) { |
424 | error_setg_errno(errp, -ret, "error rbd create"); | |
aa045c2d | 425 | goto out; |
87cd3d20 | 426 | } |
f27aaf4b | 427 | |
1bebea37 | 428 | ret = 0; |
aa045c2d KW |
429 | out: |
430 | rados_ioctx_destroy(io_ctx); | |
e38f643a | 431 | rados_shutdown(cluster); |
1bebea37 KW |
432 | return ret; |
433 | } | |
434 | ||
435 | static int qemu_rbd_co_create(BlockdevCreateOptions *options, Error **errp) | |
436 | { | |
437 | return qemu_rbd_do_create(options, NULL, NULL, errp); | |
438 | } | |
439 | ||
440 | static int coroutine_fn qemu_rbd_co_create_opts(const char *filename, | |
441 | QemuOpts *opts, | |
442 | Error **errp) | |
443 | { | |
444 | BlockdevCreateOptions *create_options; | |
445 | BlockdevCreateOptionsRbd *rbd_opts; | |
446 | BlockdevOptionsRbd *loc; | |
447 | Error *local_err = NULL; | |
448 | const char *keypairs, *password_secret; | |
449 | QDict *options = NULL; | |
450 | int ret = 0; | |
451 | ||
452 | create_options = g_new0(BlockdevCreateOptions, 1); | |
453 | create_options->driver = BLOCKDEV_DRIVER_RBD; | |
454 | rbd_opts = &create_options->u.rbd; | |
455 | ||
456 | rbd_opts->location = g_new0(BlockdevOptionsRbd, 1); | |
457 | ||
458 | password_secret = qemu_opt_get(opts, "password-secret"); | |
459 | ||
460 | /* Read out options */ | |
461 | rbd_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), | |
462 | BDRV_SECTOR_SIZE); | |
463 | rbd_opts->cluster_size = qemu_opt_get_size_del(opts, | |
464 | BLOCK_OPT_CLUSTER_SIZE, 0); | |
465 | rbd_opts->has_cluster_size = (rbd_opts->cluster_size != 0); | |
466 | ||
467 | options = qdict_new(); | |
468 | qemu_rbd_parse_filename(filename, options, &local_err); | |
469 | if (local_err) { | |
470 | ret = -EINVAL; | |
471 | error_propagate(errp, local_err); | |
472 | goto exit; | |
473 | } | |
474 | ||
475 | /* | |
476 | * Caution: while qdict_get_try_str() is fine, getting non-string | |
477 | * types would require more care. When @options come from -blockdev | |
478 | * or blockdev_add, its members are typed according to the QAPI | |
479 | * schema, but when they come from -drive, they're all QString. | |
480 | */ | |
481 | loc = rbd_opts->location; | |
19ae9ae0 FF |
482 | loc->pool = g_strdup(qdict_get_try_str(options, "pool")); |
483 | loc->conf = g_strdup(qdict_get_try_str(options, "conf")); | |
484 | loc->has_conf = !!loc->conf; | |
485 | loc->user = g_strdup(qdict_get_try_str(options, "user")); | |
486 | loc->has_user = !!loc->user; | |
487 | loc->q_namespace = g_strdup(qdict_get_try_str(options, "namespace")); | |
488 | loc->image = g_strdup(qdict_get_try_str(options, "image")); | |
489 | keypairs = qdict_get_try_str(options, "=keyvalue-pairs"); | |
1bebea37 KW |
490 | |
491 | ret = qemu_rbd_do_create(create_options, keypairs, password_secret, errp); | |
492 | if (ret < 0) { | |
493 | goto exit; | |
494 | } | |
c7cacb3e JC |
495 | |
496 | exit: | |
cb3e7f08 | 497 | qobject_unref(options); |
1bebea37 | 498 | qapi_free_BlockdevCreateOptions(create_options); |
f27aaf4b CB |
499 | return ret; |
500 | } | |
501 | ||
502 | /* | |
e04fb07f SH |
503 | * This aio completion is being called from rbd_finish_bh() and runs in qemu |
504 | * BH context. | |
f27aaf4b | 505 | */ |
ad32e9c0 | 506 | static void qemu_rbd_complete_aio(RADOSCB *rcb) |
f27aaf4b CB |
507 | { |
508 | RBDAIOCB *acb = rcb->acb; | |
509 | int64_t r; | |
510 | ||
f27aaf4b CB |
511 | r = rcb->ret; |
512 | ||
dc7588c1 | 513 | if (acb->cmd != RBD_AIO_READ) { |
f27aaf4b CB |
514 | if (r < 0) { |
515 | acb->ret = r; | |
516 | acb->error = 1; | |
517 | } else if (!acb->error) { | |
ad32e9c0 | 518 | acb->ret = rcb->size; |
f27aaf4b CB |
519 | } |
520 | } else { | |
ad32e9c0 | 521 | if (r < 0) { |
1d393bde | 522 | qemu_rbd_memset(rcb, 0); |
f27aaf4b CB |
523 | acb->ret = r; |
524 | acb->error = 1; | |
ad32e9c0 | 525 | } else if (r < rcb->size) { |
1d393bde | 526 | qemu_rbd_memset(rcb, r); |
f27aaf4b | 527 | if (!acb->error) { |
ad32e9c0 | 528 | acb->ret = rcb->size; |
f27aaf4b CB |
529 | } |
530 | } else if (!acb->error) { | |
ad32e9c0 | 531 | acb->ret = r; |
f27aaf4b CB |
532 | } |
533 | } | |
f27aaf4b | 534 | |
e04fb07f | 535 | g_free(rcb); |
f27aaf4b | 536 | |
1d393bde | 537 | if (!LIBRBD_USE_IOVEC) { |
538 | if (acb->cmd == RBD_AIO_READ) { | |
539 | qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); | |
540 | } | |
541 | qemu_vfree(acb->bounce); | |
e04fb07f | 542 | } |
1d393bde | 543 | |
e04fb07f | 544 | acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret)); |
f27aaf4b | 545 | |
8007429a | 546 | qemu_aio_unref(acb); |
f27aaf4b CB |
547 | } |
548 | ||
4bfb2741 | 549 | static char *qemu_rbd_mon_host(BlockdevOptionsRbd *opts, Error **errp) |
0a55679b | 550 | { |
4bfb2741 | 551 | const char **vals; |
2836284d MA |
552 | const char *host, *port; |
553 | char *rados_str; | |
4bfb2741 KW |
554 | InetSocketAddressBaseList *p; |
555 | int i, cnt; | |
556 | ||
557 | if (!opts->has_server) { | |
558 | return NULL; | |
559 | } | |
560 | ||
561 | for (cnt = 0, p = opts->server; p; p = p->next) { | |
562 | cnt++; | |
563 | } | |
564 | ||
565 | vals = g_new(const char *, cnt + 1); | |
566 | ||
567 | for (i = 0, p = opts->server; p; p = p->next, i++) { | |
568 | host = p->value->host; | |
569 | port = p->value->port; | |
0a55679b | 570 | |
2836284d | 571 | if (strchr(host, ':')) { |
4bfb2741 | 572 | vals[i] = g_strdup_printf("[%s]:%s", host, port); |
0a55679b | 573 | } else { |
4bfb2741 | 574 | vals[i] = g_strdup_printf("%s:%s", host, port); |
0a55679b | 575 | } |
0a55679b | 576 | } |
2836284d | 577 | vals[i] = NULL; |
0a55679b | 578 | |
2836284d | 579 | rados_str = i ? g_strjoinv(";", (char **)vals) : NULL; |
2836284d | 580 | g_strfreev((char **)vals); |
0a55679b JC |
581 | return rados_str; |
582 | } | |
583 | ||
3d9136f9 | 584 | static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx, |
4bfb2741 | 585 | BlockdevOptionsRbd *opts, bool cache, |
4ff45049 KW |
586 | const char *keypairs, const char *secretid, |
587 | Error **errp) | |
f27aaf4b | 588 | { |
0a55679b | 589 | char *mon_host = NULL; |
3d9136f9 | 590 | Error *local_err = NULL; |
f27aaf4b CB |
591 | int r; |
592 | ||
d083f954 MA |
593 | if (secretid) { |
594 | if (opts->key_secret) { | |
595 | error_setg(errp, | |
596 | "Legacy 'password-secret' clashes with 'key-secret'"); | |
597 | return -EINVAL; | |
598 | } | |
599 | opts->key_secret = g_strdup(secretid); | |
600 | opts->has_key_secret = true; | |
601 | } | |
602 | ||
4bfb2741 | 603 | mon_host = qemu_rbd_mon_host(opts, &local_err); |
0a55679b JC |
604 | if (local_err) { |
605 | error_propagate(errp, local_err); | |
606 | r = -EINVAL; | |
607 | goto failed_opts; | |
608 | } | |
609 | ||
4bfb2741 | 610 | r = rados_create(cluster, opts->user); |
ad32e9c0 | 611 | if (r < 0) { |
87cd3d20 | 612 | error_setg_errno(errp, -r, "error initializing"); |
c3ca988d | 613 | goto failed_opts; |
f27aaf4b CB |
614 | } |
615 | ||
c7cacb3e | 616 | /* try default location when conf=NULL, but ignore failure */ |
4bfb2741 KW |
617 | r = rados_conf_read_file(*cluster, opts->conf); |
618 | if (opts->has_conf && r < 0) { | |
619 | error_setg_errno(errp, -r, "error reading conf file %s", opts->conf); | |
c7cacb3e | 620 | goto failed_shutdown; |
99a3c89d JD |
621 | } |
622 | ||
3d9136f9 | 623 | r = qemu_rbd_set_keypairs(*cluster, keypairs, errp); |
c7cacb3e JC |
624 | if (r < 0) { |
625 | goto failed_shutdown; | |
99a3c89d JD |
626 | } |
627 | ||
0a55679b | 628 | if (mon_host) { |
3d9136f9 | 629 | r = rados_conf_set(*cluster, "mon_host", mon_host); |
0a55679b JC |
630 | if (r < 0) { |
631 | goto failed_shutdown; | |
632 | } | |
633 | } | |
634 | ||
d083f954 MA |
635 | r = qemu_rbd_set_auth(*cluster, opts, errp); |
636 | if (r < 0) { | |
60390a21 DB |
637 | goto failed_shutdown; |
638 | } | |
639 | ||
b11f38fc JD |
640 | /* |
641 | * Fallback to more conservative semantics if setting cache | |
642 | * options fails. Ignore errors from setting rbd_cache because the | |
643 | * only possible error is that the option does not exist, and | |
644 | * librbd defaults to no caching. If write through caching cannot | |
645 | * be set up, fall back to no caching. | |
646 | */ | |
3d9136f9 KW |
647 | if (cache) { |
648 | rados_conf_set(*cluster, "rbd_cache", "true"); | |
b11f38fc | 649 | } else { |
3d9136f9 | 650 | rados_conf_set(*cluster, "rbd_cache", "false"); |
b11f38fc JD |
651 | } |
652 | ||
3d9136f9 | 653 | r = rados_connect(*cluster); |
ad32e9c0 | 654 | if (r < 0) { |
87cd3d20 | 655 | error_setg_errno(errp, -r, "error connecting"); |
eb93d5d9 | 656 | goto failed_shutdown; |
f27aaf4b CB |
657 | } |
658 | ||
4bfb2741 | 659 | r = rados_ioctx_create(*cluster, opts->pool, io_ctx); |
ad32e9c0 | 660 | if (r < 0) { |
4bfb2741 | 661 | error_setg_errno(errp, -r, "error opening pool %s", opts->pool); |
eb93d5d9 | 662 | goto failed_shutdown; |
f27aaf4b | 663 | } |
19ae9ae0 FF |
664 | /* |
665 | * Set the namespace after opening the io context on the pool, | |
666 | * if nspace == NULL or if nspace == "", it is just as we did nothing | |
667 | */ | |
668 | rados_ioctx_set_namespace(*io_ctx, opts->q_namespace); | |
f27aaf4b | 669 | |
3d9136f9 KW |
670 | return 0; |
671 | ||
672 | failed_shutdown: | |
673 | rados_shutdown(*cluster); | |
3d9136f9 | 674 | failed_opts: |
3d9136f9 KW |
675 | g_free(mon_host); |
676 | return r; | |
677 | } | |
678 | ||
f24b03b5 JC |
679 | static int qemu_rbd_convert_options(QDict *options, BlockdevOptionsRbd **opts, |
680 | Error **errp) | |
681 | { | |
682 | Visitor *v; | |
683 | Error *local_err = NULL; | |
684 | ||
685 | /* Convert the remaining options into a QAPI object */ | |
686 | v = qobject_input_visitor_new_flat_confused(options, errp); | |
687 | if (!v) { | |
688 | return -EINVAL; | |
689 | } | |
690 | ||
691 | visit_type_BlockdevOptionsRbd(v, NULL, opts, &local_err); | |
692 | visit_free(v); | |
693 | ||
694 | if (local_err) { | |
695 | error_propagate(errp, local_err); | |
696 | return -EINVAL; | |
697 | } | |
698 | ||
699 | return 0; | |
700 | } | |
701 | ||
084d1d13 JC |
702 | static int qemu_rbd_attempt_legacy_options(QDict *options, |
703 | BlockdevOptionsRbd **opts, | |
704 | char **keypairs) | |
705 | { | |
706 | char *filename; | |
707 | int r; | |
708 | ||
709 | filename = g_strdup(qdict_get_try_str(options, "filename")); | |
710 | if (!filename) { | |
711 | return -EINVAL; | |
712 | } | |
713 | qdict_del(options, "filename"); | |
714 | ||
715 | qemu_rbd_parse_filename(filename, options, NULL); | |
716 | ||
717 | /* keypairs freed by caller */ | |
718 | *keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs")); | |
719 | if (*keypairs) { | |
720 | qdict_del(options, "=keyvalue-pairs"); | |
721 | } | |
722 | ||
723 | r = qemu_rbd_convert_options(options, opts, NULL); | |
724 | ||
725 | g_free(filename); | |
726 | return r; | |
727 | } | |
728 | ||
3d9136f9 KW |
729 | static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags, |
730 | Error **errp) | |
731 | { | |
732 | BDRVRBDState *s = bs->opaque; | |
4bfb2741 | 733 | BlockdevOptionsRbd *opts = NULL; |
bfb15b4b | 734 | const QDictEntry *e; |
3d9136f9 | 735 | Error *local_err = NULL; |
4ff45049 | 736 | char *keypairs, *secretid; |
3d9136f9 KW |
737 | int r; |
738 | ||
4ff45049 KW |
739 | keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs")); |
740 | if (keypairs) { | |
741 | qdict_del(options, "=keyvalue-pairs"); | |
742 | } | |
743 | ||
744 | secretid = g_strdup(qdict_get_try_str(options, "password-secret")); | |
745 | if (secretid) { | |
746 | qdict_del(options, "password-secret"); | |
747 | } | |
748 | ||
f24b03b5 | 749 | r = qemu_rbd_convert_options(options, &opts, &local_err); |
4bfb2741 | 750 | if (local_err) { |
084d1d13 JC |
751 | /* If keypairs are present, that means some options are present in |
752 | * the modern option format. Don't attempt to parse legacy option | |
753 | * formats, as we won't support mixed usage. */ | |
754 | if (keypairs) { | |
755 | error_propagate(errp, local_err); | |
756 | goto out; | |
757 | } | |
758 | ||
759 | /* If the initial attempt to convert and process the options failed, | |
760 | * we may be attempting to open an image file that has the rbd options | |
761 | * specified in the older format consisting of all key/value pairs | |
762 | * encoded in the filename. Go ahead and attempt to parse the | |
763 | * filename, and see if we can pull out the required options. */ | |
764 | r = qemu_rbd_attempt_legacy_options(options, &opts, &keypairs); | |
765 | if (r < 0) { | |
766 | /* Propagate the original error, not the legacy parsing fallback | |
767 | * error, as the latter was just a best-effort attempt. */ | |
768 | error_propagate(errp, local_err); | |
769 | goto out; | |
770 | } | |
771 | /* Take care whenever deciding to actually deprecate; once this ability | |
772 | * is removed, we will not be able to open any images with legacy-styled | |
773 | * backing image strings. */ | |
5197f445 MA |
774 | warn_report("RBD options encoded in the filename as keyvalue pairs " |
775 | "is deprecated"); | |
4bfb2741 KW |
776 | } |
777 | ||
bfb15b4b JC |
778 | /* Remove the processed options from the QDict (the visitor processes |
779 | * _all_ options in the QDict) */ | |
780 | while ((e = qdict_first(options))) { | |
781 | qdict_del(options, e->key); | |
782 | } | |
783 | ||
d41a5588 KW |
784 | r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts, |
785 | !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp); | |
3d9136f9 | 786 | if (r < 0) { |
4ff45049 | 787 | goto out; |
3d9136f9 KW |
788 | } |
789 | ||
d41a5588 KW |
790 | s->snap = g_strdup(opts->snapshot); |
791 | s->image_name = g_strdup(opts->image); | |
792 | ||
e2b8247a | 793 | /* rbd_open is always r/w */ |
80b61a27 | 794 | r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap); |
f27aaf4b | 795 | if (r < 0) { |
80b61a27 JC |
796 | error_setg_errno(errp, -r, "error reading header from %s", |
797 | s->image_name); | |
eb93d5d9 | 798 | goto failed_open; |
f27aaf4b CB |
799 | } |
800 | ||
d24f8023 SG |
801 | r = rbd_get_size(s->image, &s->image_size); |
802 | if (r < 0) { | |
803 | error_setg_errno(errp, -r, "error getting image size from %s", | |
804 | s->image_name); | |
805 | rbd_close(s->image); | |
806 | goto failed_open; | |
807 | } | |
808 | ||
e2b8247a JC |
809 | /* If we are using an rbd snapshot, we must be r/o, otherwise |
810 | * leave as-is */ | |
811 | if (s->snap != NULL) { | |
eaa2410f KW |
812 | r = bdrv_apply_auto_read_only(bs, "rbd snapshots are read-only", errp); |
813 | if (r < 0) { | |
814 | rbd_close(s->image); | |
815 | goto failed_open; | |
e2b8247a JC |
816 | } |
817 | } | |
f27aaf4b | 818 | |
4ff45049 KW |
819 | r = 0; |
820 | goto out; | |
f27aaf4b | 821 | |
eb93d5d9 | 822 | failed_open: |
ad32e9c0 | 823 | rados_ioctx_destroy(s->io_ctx); |
eb93d5d9 | 824 | g_free(s->snap); |
80b61a27 | 825 | g_free(s->image_name); |
3d9136f9 | 826 | rados_shutdown(s->cluster); |
4ff45049 | 827 | out: |
4bfb2741 | 828 | qapi_free_BlockdevOptionsRbd(opts); |
4ff45049 KW |
829 | g_free(keypairs); |
830 | g_free(secretid); | |
f27aaf4b CB |
831 | return r; |
832 | } | |
833 | ||
56e7cf8d JC |
834 | |
835 | /* Since RBD is currently always opened R/W via the API, | |
836 | * we just need to check if we are using a snapshot or not, in | |
837 | * order to determine if we will allow it to be R/W */ | |
838 | static int qemu_rbd_reopen_prepare(BDRVReopenState *state, | |
839 | BlockReopenQueue *queue, Error **errp) | |
840 | { | |
841 | BDRVRBDState *s = state->bs->opaque; | |
842 | int ret = 0; | |
843 | ||
844 | if (s->snap && state->flags & BDRV_O_RDWR) { | |
845 | error_setg(errp, | |
846 | "Cannot change node '%s' to r/w when using RBD snapshot", | |
847 | bdrv_get_device_or_node_name(state->bs)); | |
848 | ret = -EINVAL; | |
849 | } | |
850 | ||
851 | return ret; | |
852 | } | |
853 | ||
ad32e9c0 | 854 | static void qemu_rbd_close(BlockDriverState *bs) |
f27aaf4b CB |
855 | { |
856 | BDRVRBDState *s = bs->opaque; | |
857 | ||
ad32e9c0 JD |
858 | rbd_close(s->image); |
859 | rados_ioctx_destroy(s->io_ctx); | |
7267c094 | 860 | g_free(s->snap); |
80b61a27 | 861 | g_free(s->image_name); |
ad32e9c0 | 862 | rados_shutdown(s->cluster); |
f27aaf4b CB |
863 | } |
864 | ||
d24f8023 SG |
865 | /* Resize the RBD image and update the 'image_size' with the current size */ |
866 | static int qemu_rbd_resize(BlockDriverState *bs, uint64_t size) | |
867 | { | |
868 | BDRVRBDState *s = bs->opaque; | |
869 | int r; | |
870 | ||
871 | r = rbd_resize(s->image, size); | |
872 | if (r < 0) { | |
873 | return r; | |
874 | } | |
875 | ||
876 | s->image_size = size; | |
877 | ||
878 | return 0; | |
879 | } | |
880 | ||
d7331bed | 881 | static const AIOCBInfo rbd_aiocb_info = { |
f27aaf4b | 882 | .aiocb_size = sizeof(RBDAIOCB), |
f27aaf4b CB |
883 | }; |
884 | ||
e04fb07f | 885 | static void rbd_finish_bh(void *opaque) |
f27aaf4b | 886 | { |
e04fb07f | 887 | RADOSCB *rcb = opaque; |
e04fb07f | 888 | qemu_rbd_complete_aio(rcb); |
ad32e9c0 JD |
889 | } |
890 | ||
891 | /* | |
892 | * This is the callback function for rbd_aio_read and _write | |
893 | * | |
894 | * Note: this function is being called from a non qemu thread so | |
895 | * we need to be careful about what we do here. Generally we only | |
e04fb07f SH |
896 | * schedule a BH, and do the rest of the io completion handling |
897 | * from rbd_finish_bh() which runs in a qemu context. | |
ad32e9c0 JD |
898 | */ |
899 | static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb) | |
900 | { | |
e04fb07f SH |
901 | RBDAIOCB *acb = rcb->acb; |
902 | ||
ad32e9c0 JD |
903 | rcb->ret = rbd_aio_get_return_value(c); |
904 | rbd_aio_release(c); | |
f27aaf4b | 905 | |
e4ec5ad4 PD |
906 | replay_bh_schedule_oneshot_event(bdrv_get_aio_context(acb->common.bs), |
907 | rbd_finish_bh, rcb); | |
f27aaf4b CB |
908 | } |
909 | ||
787f3133 JD |
910 | static int rbd_aio_discard_wrapper(rbd_image_t image, |
911 | uint64_t off, | |
912 | uint64_t len, | |
913 | rbd_completion_t comp) | |
914 | { | |
915 | #ifdef LIBRBD_SUPPORTS_DISCARD | |
916 | return rbd_aio_discard(image, off, len, comp); | |
917 | #else | |
918 | return -ENOTSUP; | |
919 | #endif | |
920 | } | |
921 | ||
dc7588c1 JD |
922 | static int rbd_aio_flush_wrapper(rbd_image_t image, |
923 | rbd_completion_t comp) | |
924 | { | |
925 | #ifdef LIBRBD_SUPPORTS_AIO_FLUSH | |
926 | return rbd_aio_flush(image, comp); | |
927 | #else | |
928 | return -ENOTSUP; | |
929 | #endif | |
930 | } | |
931 | ||
7c84b1b8 | 932 | static BlockAIOCB *rbd_start_aio(BlockDriverState *bs, |
7bbca9e2 | 933 | int64_t off, |
7c84b1b8 | 934 | QEMUIOVector *qiov, |
7bbca9e2 | 935 | int64_t size, |
097310b5 | 936 | BlockCompletionFunc *cb, |
7c84b1b8 MA |
937 | void *opaque, |
938 | RBDAIOCmd cmd) | |
f27aaf4b CB |
939 | { |
940 | RBDAIOCB *acb; | |
0f7a0237 | 941 | RADOSCB *rcb = NULL; |
ad32e9c0 | 942 | rbd_completion_t c; |
51a13528 | 943 | int r; |
f27aaf4b CB |
944 | |
945 | BDRVRBDState *s = bs->opaque; | |
946 | ||
d7331bed | 947 | acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque); |
787f3133 | 948 | acb->cmd = cmd; |
f27aaf4b | 949 | acb->qiov = qiov; |
7bbca9e2 | 950 | assert(!qiov || qiov->size == size); |
1d393bde | 951 | |
952 | rcb = g_new(RADOSCB, 1); | |
953 | ||
954 | if (!LIBRBD_USE_IOVEC) { | |
955 | if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) { | |
956 | acb->bounce = NULL; | |
957 | } else { | |
958 | acb->bounce = qemu_try_blockalign(bs, qiov->size); | |
959 | if (acb->bounce == NULL) { | |
960 | goto failed; | |
961 | } | |
0f7a0237 | 962 | } |
1d393bde | 963 | if (cmd == RBD_AIO_WRITE) { |
964 | qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); | |
965 | } | |
966 | rcb->buf = acb->bounce; | |
787f3133 | 967 | } |
1d393bde | 968 | |
f27aaf4b CB |
969 | acb->ret = 0; |
970 | acb->error = 0; | |
971 | acb->s = s; | |
f27aaf4b | 972 | |
ad32e9c0 | 973 | rcb->acb = acb; |
ad32e9c0 JD |
974 | rcb->s = acb->s; |
975 | rcb->size = size; | |
51a13528 JD |
976 | r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c); |
977 | if (r < 0) { | |
978 | goto failed; | |
979 | } | |
f27aaf4b | 980 | |
787f3133 | 981 | switch (cmd) { |
d24f8023 SG |
982 | case RBD_AIO_WRITE: { |
983 | /* | |
984 | * RBD APIs don't allow us to write more than actual size, so in order | |
985 | * to support growing images, we resize the image before write | |
986 | * operations that exceed the current size. | |
987 | */ | |
988 | if (off + size > s->image_size) { | |
989 | r = qemu_rbd_resize(bs, off + size); | |
990 | if (r < 0) { | |
991 | goto failed_completion; | |
992 | } | |
993 | } | |
1d393bde | 994 | #ifdef LIBRBD_SUPPORTS_IOVEC |
995 | r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c); | |
996 | #else | |
997 | r = rbd_aio_write(s->image, off, size, rcb->buf, c); | |
998 | #endif | |
787f3133 | 999 | break; |
d24f8023 | 1000 | } |
787f3133 | 1001 | case RBD_AIO_READ: |
1d393bde | 1002 | #ifdef LIBRBD_SUPPORTS_IOVEC |
1003 | r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c); | |
1004 | #else | |
1005 | r = rbd_aio_read(s->image, off, size, rcb->buf, c); | |
1006 | #endif | |
787f3133 JD |
1007 | break; |
1008 | case RBD_AIO_DISCARD: | |
1009 | r = rbd_aio_discard_wrapper(s->image, off, size, c); | |
1010 | break; | |
dc7588c1 JD |
1011 | case RBD_AIO_FLUSH: |
1012 | r = rbd_aio_flush_wrapper(s->image, c); | |
1013 | break; | |
787f3133 JD |
1014 | default: |
1015 | r = -EINVAL; | |
51a13528 JD |
1016 | } |
1017 | ||
1018 | if (r < 0) { | |
405a2764 | 1019 | goto failed_completion; |
f27aaf4b | 1020 | } |
f27aaf4b | 1021 | return &acb->common; |
51a13528 | 1022 | |
405a2764 KW |
1023 | failed_completion: |
1024 | rbd_aio_release(c); | |
51a13528 | 1025 | failed: |
7267c094 | 1026 | g_free(rcb); |
1d393bde | 1027 | if (!LIBRBD_USE_IOVEC) { |
1028 | qemu_vfree(acb->bounce); | |
1029 | } | |
1030 | ||
8007429a | 1031 | qemu_aio_unref(acb); |
51a13528 | 1032 | return NULL; |
f27aaf4b CB |
1033 | } |
1034 | ||
e8e16d4b EB |
1035 | static BlockAIOCB *qemu_rbd_aio_preadv(BlockDriverState *bs, |
1036 | uint64_t offset, uint64_t bytes, | |
1037 | QEMUIOVector *qiov, int flags, | |
1038 | BlockCompletionFunc *cb, | |
1039 | void *opaque) | |
f27aaf4b | 1040 | { |
e8e16d4b | 1041 | return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque, |
787f3133 | 1042 | RBD_AIO_READ); |
f27aaf4b CB |
1043 | } |
1044 | ||
e8e16d4b EB |
1045 | static BlockAIOCB *qemu_rbd_aio_pwritev(BlockDriverState *bs, |
1046 | uint64_t offset, uint64_t bytes, | |
1047 | QEMUIOVector *qiov, int flags, | |
1048 | BlockCompletionFunc *cb, | |
1049 | void *opaque) | |
f27aaf4b | 1050 | { |
e8e16d4b | 1051 | return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque, |
787f3133 | 1052 | RBD_AIO_WRITE); |
f27aaf4b CB |
1053 | } |
1054 | ||
dc7588c1 | 1055 | #ifdef LIBRBD_SUPPORTS_AIO_FLUSH |
7c84b1b8 | 1056 | static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs, |
097310b5 | 1057 | BlockCompletionFunc *cb, |
7c84b1b8 | 1058 | void *opaque) |
dc7588c1 JD |
1059 | { |
1060 | return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH); | |
1061 | } | |
1062 | ||
1063 | #else | |
1064 | ||
8b94ff85 | 1065 | static int qemu_rbd_co_flush(BlockDriverState *bs) |
7a3f5fe9 SW |
1066 | { |
1067 | #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1) | |
1068 | /* rbd_flush added in 0.1.1 */ | |
1069 | BDRVRBDState *s = bs->opaque; | |
1070 | return rbd_flush(s->image); | |
1071 | #else | |
1072 | return 0; | |
1073 | #endif | |
1074 | } | |
dc7588c1 | 1075 | #endif |
7a3f5fe9 | 1076 | |
ad32e9c0 | 1077 | static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi) |
f27aaf4b CB |
1078 | { |
1079 | BDRVRBDState *s = bs->opaque; | |
ad32e9c0 JD |
1080 | rbd_image_info_t info; |
1081 | int r; | |
1082 | ||
1083 | r = rbd_stat(s->image, &info, sizeof(info)); | |
1084 | if (r < 0) { | |
1085 | return r; | |
1086 | } | |
1087 | ||
1088 | bdi->cluster_size = info.obj_size; | |
f27aaf4b CB |
1089 | return 0; |
1090 | } | |
1091 | ||
ad32e9c0 | 1092 | static int64_t qemu_rbd_getlength(BlockDriverState *bs) |
f27aaf4b CB |
1093 | { |
1094 | BDRVRBDState *s = bs->opaque; | |
ad32e9c0 JD |
1095 | rbd_image_info_t info; |
1096 | int r; | |
f27aaf4b | 1097 | |
ad32e9c0 JD |
1098 | r = rbd_stat(s->image, &info, sizeof(info)); |
1099 | if (r < 0) { | |
1100 | return r; | |
1101 | } | |
1102 | ||
1103 | return info.size; | |
f27aaf4b CB |
1104 | } |
1105 | ||
061ca8a3 KW |
1106 | static int coroutine_fn qemu_rbd_co_truncate(BlockDriverState *bs, |
1107 | int64_t offset, | |
c80d8b06 | 1108 | bool exact, |
061ca8a3 KW |
1109 | PreallocMode prealloc, |
1110 | Error **errp) | |
30cdc48c | 1111 | { |
30cdc48c JD |
1112 | int r; |
1113 | ||
8243ccb7 HR |
1114 | if (prealloc != PREALLOC_MODE_OFF) { |
1115 | error_setg(errp, "Unsupported preallocation mode '%s'", | |
977c736f | 1116 | PreallocMode_str(prealloc)); |
8243ccb7 HR |
1117 | return -ENOTSUP; |
1118 | } | |
1119 | ||
d24f8023 | 1120 | r = qemu_rbd_resize(bs, offset); |
30cdc48c | 1121 | if (r < 0) { |
f59adb32 | 1122 | error_setg_errno(errp, -r, "Failed to resize file"); |
30cdc48c JD |
1123 | return r; |
1124 | } | |
1125 | ||
1126 | return 0; | |
1127 | } | |
1128 | ||
ad32e9c0 JD |
1129 | static int qemu_rbd_snap_create(BlockDriverState *bs, |
1130 | QEMUSnapshotInfo *sn_info) | |
f27aaf4b CB |
1131 | { |
1132 | BDRVRBDState *s = bs->opaque; | |
f27aaf4b | 1133 | int r; |
f27aaf4b CB |
1134 | |
1135 | if (sn_info->name[0] == '\0') { | |
1136 | return -EINVAL; /* we need a name for rbd snapshots */ | |
1137 | } | |
1138 | ||
1139 | /* | |
1140 | * rbd snapshots are using the name as the user controlled unique identifier | |
1141 | * we can't use the rbd snapid for that purpose, as it can't be set | |
1142 | */ | |
1143 | if (sn_info->id_str[0] != '\0' && | |
1144 | strcmp(sn_info->id_str, sn_info->name) != 0) { | |
1145 | return -EINVAL; | |
1146 | } | |
1147 | ||
1148 | if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) { | |
1149 | return -ERANGE; | |
1150 | } | |
1151 | ||
ad32e9c0 | 1152 | r = rbd_snap_create(s->image, sn_info->name); |
f27aaf4b | 1153 | if (r < 0) { |
ad32e9c0 | 1154 | error_report("failed to create snap: %s", strerror(-r)); |
f27aaf4b CB |
1155 | return r; |
1156 | } | |
1157 | ||
f27aaf4b CB |
1158 | return 0; |
1159 | } | |
1160 | ||
bd603247 | 1161 | static int qemu_rbd_snap_remove(BlockDriverState *bs, |
a89d89d3 WX |
1162 | const char *snapshot_id, |
1163 | const char *snapshot_name, | |
1164 | Error **errp) | |
bd603247 GF |
1165 | { |
1166 | BDRVRBDState *s = bs->opaque; | |
1167 | int r; | |
1168 | ||
a89d89d3 WX |
1169 | if (!snapshot_name) { |
1170 | error_setg(errp, "rbd need a valid snapshot name"); | |
1171 | return -EINVAL; | |
1172 | } | |
1173 | ||
1174 | /* If snapshot_id is specified, it must be equal to name, see | |
1175 | qemu_rbd_snap_list() */ | |
1176 | if (snapshot_id && strcmp(snapshot_id, snapshot_name)) { | |
1177 | error_setg(errp, | |
1178 | "rbd do not support snapshot id, it should be NULL or " | |
1179 | "equal to snapshot name"); | |
1180 | return -EINVAL; | |
1181 | } | |
1182 | ||
bd603247 | 1183 | r = rbd_snap_remove(s->image, snapshot_name); |
a89d89d3 WX |
1184 | if (r < 0) { |
1185 | error_setg_errno(errp, -r, "Failed to remove the snapshot"); | |
1186 | } | |
bd603247 GF |
1187 | return r; |
1188 | } | |
1189 | ||
1190 | static int qemu_rbd_snap_rollback(BlockDriverState *bs, | |
1191 | const char *snapshot_name) | |
1192 | { | |
1193 | BDRVRBDState *s = bs->opaque; | |
bd603247 | 1194 | |
9be38598 | 1195 | return rbd_snap_rollback(s->image, snapshot_name); |
bd603247 GF |
1196 | } |
1197 | ||
ad32e9c0 JD |
1198 | static int qemu_rbd_snap_list(BlockDriverState *bs, |
1199 | QEMUSnapshotInfo **psn_tab) | |
f27aaf4b CB |
1200 | { |
1201 | BDRVRBDState *s = bs->opaque; | |
f27aaf4b | 1202 | QEMUSnapshotInfo *sn_info, *sn_tab = NULL; |
ad32e9c0 JD |
1203 | int i, snap_count; |
1204 | rbd_snap_info_t *snaps; | |
1205 | int max_snaps = RBD_MAX_SNAPS; | |
f27aaf4b | 1206 | |
ad32e9c0 | 1207 | do { |
02c4f26b | 1208 | snaps = g_new(rbd_snap_info_t, max_snaps); |
ad32e9c0 | 1209 | snap_count = rbd_snap_list(s->image, snaps, &max_snaps); |
9e6337d0 | 1210 | if (snap_count <= 0) { |
7267c094 | 1211 | g_free(snaps); |
f27aaf4b | 1212 | } |
ad32e9c0 | 1213 | } while (snap_count == -ERANGE); |
f27aaf4b | 1214 | |
ad32e9c0 | 1215 | if (snap_count <= 0) { |
b9c53290 | 1216 | goto done; |
f27aaf4b CB |
1217 | } |
1218 | ||
5839e53b | 1219 | sn_tab = g_new0(QEMUSnapshotInfo, snap_count); |
f27aaf4b | 1220 | |
ad32e9c0 JD |
1221 | for (i = 0; i < snap_count; i++) { |
1222 | const char *snap_name = snaps[i].name; | |
f27aaf4b CB |
1223 | |
1224 | sn_info = sn_tab + i; | |
1225 | pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name); | |
1226 | pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name); | |
f27aaf4b | 1227 | |
ad32e9c0 | 1228 | sn_info->vm_state_size = snaps[i].size; |
f27aaf4b CB |
1229 | sn_info->date_sec = 0; |
1230 | sn_info->date_nsec = 0; | |
1231 | sn_info->vm_clock_nsec = 0; | |
1232 | } | |
ad32e9c0 | 1233 | rbd_snap_list_end(snaps); |
9e6337d0 | 1234 | g_free(snaps); |
ad32e9c0 | 1235 | |
b9c53290 | 1236 | done: |
f27aaf4b | 1237 | *psn_tab = sn_tab; |
f27aaf4b | 1238 | return snap_count; |
f27aaf4b CB |
1239 | } |
1240 | ||
787f3133 | 1241 | #ifdef LIBRBD_SUPPORTS_DISCARD |
4da444a0 EB |
1242 | static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs, |
1243 | int64_t offset, | |
f5a5ca79 | 1244 | int bytes, |
4da444a0 EB |
1245 | BlockCompletionFunc *cb, |
1246 | void *opaque) | |
787f3133 | 1247 | { |
f5a5ca79 | 1248 | return rbd_start_aio(bs, offset, NULL, bytes, cb, opaque, |
787f3133 JD |
1249 | RBD_AIO_DISCARD); |
1250 | } | |
1251 | #endif | |
1252 | ||
be217884 | 1253 | #ifdef LIBRBD_SUPPORTS_INVALIDATE |
2b148f39 PB |
1254 | static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs, |
1255 | Error **errp) | |
be217884 AC |
1256 | { |
1257 | BDRVRBDState *s = bs->opaque; | |
1258 | int r = rbd_invalidate_cache(s->image); | |
1259 | if (r < 0) { | |
1260 | error_setg_errno(errp, -r, "Failed to invalidate the cache"); | |
1261 | } | |
1262 | } | |
1263 | #endif | |
1264 | ||
bd0cf596 CL |
1265 | static QemuOptsList qemu_rbd_create_opts = { |
1266 | .name = "rbd-create-opts", | |
1267 | .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head), | |
1268 | .desc = { | |
1269 | { | |
1270 | .name = BLOCK_OPT_SIZE, | |
1271 | .type = QEMU_OPT_SIZE, | |
1272 | .help = "Virtual disk size" | |
1273 | }, | |
1274 | { | |
1275 | .name = BLOCK_OPT_CLUSTER_SIZE, | |
1276 | .type = QEMU_OPT_SIZE, | |
1277 | .help = "RBD object size" | |
1278 | }, | |
60390a21 DB |
1279 | { |
1280 | .name = "password-secret", | |
1281 | .type = QEMU_OPT_STRING, | |
1282 | .help = "ID of secret providing the password", | |
1283 | }, | |
bd0cf596 CL |
1284 | { /* end of list */ } |
1285 | } | |
f27aaf4b CB |
1286 | }; |
1287 | ||
2654267c HR |
1288 | static const char *const qemu_rbd_strong_runtime_opts[] = { |
1289 | "pool", | |
1290 | "image", | |
1291 | "conf", | |
1292 | "snapshot", | |
1293 | "user", | |
1294 | "server.", | |
1295 | "password-secret", | |
1296 | ||
1297 | NULL | |
1298 | }; | |
1299 | ||
f27aaf4b | 1300 | static BlockDriver bdrv_rbd = { |
c7cacb3e JC |
1301 | .format_name = "rbd", |
1302 | .instance_size = sizeof(BDRVRBDState), | |
1303 | .bdrv_parse_filename = qemu_rbd_parse_filename, | |
e8e16d4b | 1304 | .bdrv_refresh_limits = qemu_rbd_refresh_limits, |
c7cacb3e JC |
1305 | .bdrv_file_open = qemu_rbd_open, |
1306 | .bdrv_close = qemu_rbd_close, | |
56e7cf8d | 1307 | .bdrv_reopen_prepare = qemu_rbd_reopen_prepare, |
1bebea37 | 1308 | .bdrv_co_create = qemu_rbd_co_create, |
efc75e2a | 1309 | .bdrv_co_create_opts = qemu_rbd_co_create_opts, |
c7cacb3e | 1310 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
1dcaf527 | 1311 | .bdrv_has_zero_init_truncate = bdrv_has_zero_init_1, |
c7cacb3e JC |
1312 | .bdrv_get_info = qemu_rbd_getinfo, |
1313 | .create_opts = &qemu_rbd_create_opts, | |
1314 | .bdrv_getlength = qemu_rbd_getlength, | |
061ca8a3 | 1315 | .bdrv_co_truncate = qemu_rbd_co_truncate, |
c7cacb3e | 1316 | .protocol_name = "rbd", |
f27aaf4b | 1317 | |
e8e16d4b EB |
1318 | .bdrv_aio_preadv = qemu_rbd_aio_preadv, |
1319 | .bdrv_aio_pwritev = qemu_rbd_aio_pwritev, | |
dc7588c1 JD |
1320 | |
1321 | #ifdef LIBRBD_SUPPORTS_AIO_FLUSH | |
1322 | .bdrv_aio_flush = qemu_rbd_aio_flush, | |
1323 | #else | |
c68b89ac | 1324 | .bdrv_co_flush_to_disk = qemu_rbd_co_flush, |
dc7588c1 | 1325 | #endif |
f27aaf4b | 1326 | |
787f3133 | 1327 | #ifdef LIBRBD_SUPPORTS_DISCARD |
4da444a0 | 1328 | .bdrv_aio_pdiscard = qemu_rbd_aio_pdiscard, |
787f3133 JD |
1329 | #endif |
1330 | ||
c68b89ac | 1331 | .bdrv_snapshot_create = qemu_rbd_snap_create, |
bd603247 | 1332 | .bdrv_snapshot_delete = qemu_rbd_snap_remove, |
c68b89ac | 1333 | .bdrv_snapshot_list = qemu_rbd_snap_list, |
bd603247 | 1334 | .bdrv_snapshot_goto = qemu_rbd_snap_rollback, |
be217884 | 1335 | #ifdef LIBRBD_SUPPORTS_INVALIDATE |
2b148f39 | 1336 | .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache, |
be217884 | 1337 | #endif |
2654267c HR |
1338 | |
1339 | .strong_runtime_opts = qemu_rbd_strong_runtime_opts, | |
f27aaf4b CB |
1340 | }; |
1341 | ||
1342 | static void bdrv_rbd_init(void) | |
1343 | { | |
1344 | bdrv_register(&bdrv_rbd); | |
1345 | } | |
1346 | ||
1347 | block_init(bdrv_rbd_init); |