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