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