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