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