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