blob: 9bd2bce71651b8612e37c5dc040da04f7ae1a520 [file] [log] [blame]
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001/*
2 * QEMU Block driver for RADOS (Ceph)
3 *
Josh Durginad32e9c2011-05-26 16:07:31 -07004 * Copyright (C) 2010-2011 Christian Brunner <chb@muc.de>,
5 * Josh Durgin <josh.durgin@dreamhost.com>
Christian Brunnerf27aaf42010-12-06 20:53:01 +01006 *
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 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010010 * Contributions after 2012-01-13 are licensed under the terms of the
11 * GNU GPL, version 2 or (at your option) any later version.
Christian Brunnerf27aaf42010-12-06 20:53:01 +010012 */
13
Peter Maydell80c71a22016-01-18 18:01:42 +000014#include "qemu/osdep.h"
Josh Durginad32e9c2011-05-26 16:07:31 -070015
Markus Armbruster28362842017-03-28 10:56:08 +020016#include <rbd/librbd.h>
Markus Armbrusterda34e652016-03-14 09:01:28 +010017#include "qapi/error.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010018#include "qemu/error-report.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020019#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010020#include "qemu/option.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010021#include "block/block_int.h"
Max Reitz609f45e2018-06-14 21:14:28 +020022#include "block/qdict.h"
Daniel P. Berrange60390a22016-01-21 14:19:19 +000023#include "crypto/secret.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020024#include "qemu/cutils.h"
Pavel Dovgalyuke4ec5ad2019-09-17 14:58:19 +030025#include "sysemu/replay.h"
Jeff Codyc7cacb32017-02-26 17:50:42 -050026#include "qapi/qmp/qstring.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010027#include "qapi/qmp/qdict.h"
Eric Blakee98c6962017-03-31 10:27:30 -050028#include "qapi/qmp/qjson.h"
Markus Armbruster47e6b292018-02-01 12:18:38 +010029#include "qapi/qmp/qlist.h"
Kevin Wolf4bfb2742018-02-15 20:58:24 +010030#include "qapi/qobject-input-visitor.h"
31#include "qapi/qapi-visit-block-core.h"
Christian Brunnerf27aaf42010-12-06 20:53:01 +010032
Christian Brunnerf27aaf42010-12-06 20:53:01 +010033/*
34 * When specifying the image filename use:
35 *
Josh Durginfab5cf52011-05-26 16:07:32 -070036 * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
Christian Brunnerf27aaf42010-12-06 20:53:01 +010037 *
Sage Weil9e1fbcd2011-09-15 14:11:10 -070038 * poolname must be the name of an existing rados pool.
Christian Brunnerf27aaf42010-12-06 20:53:01 +010039 *
Sage Weil9e1fbcd2011-09-15 14:11:10 -070040 * devicename is the name of the rbd image.
Christian Brunnerf27aaf42010-12-06 20:53:01 +010041 *
Sage Weil9e1fbcd2011-09-15 14:11:10 -070042 * Each option given is used to configure rados, and may be any valid
43 * Ceph option, "id", or "conf".
Josh Durginfab5cf52011-05-26 16:07:32 -070044 *
Sage Weil9e1fbcd2011-09-15 14:11:10 -070045 * The "id" option indicates what user we should authenticate as to
46 * the Ceph cluster. If it is excluded we will use the Ceph default
47 * (normally 'admin').
Christian Brunnerf27aaf42010-12-06 20:53:01 +010048 *
Sage Weil9e1fbcd2011-09-15 14:11:10 -070049 * The "conf" option specifies a Ceph configuration file to read. If
50 * it is not specified, we will read from the default Ceph locations
51 * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration
52 * file, specify conf=/dev/null.
Christian Brunnerf27aaf42010-12-06 20:53:01 +010053 *
Sage Weil9e1fbcd2011-09-15 14:11:10 -070054 * Configuration values containing :, @, or = can be escaped with a
55 * leading "\".
Christian Brunnerf27aaf42010-12-06 20:53:01 +010056 */
57
Josh Durgin787f3132012-04-30 23:16:45 -070058/* rbd_aio_discard added in 0.1.2 */
59#if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2)
60#define LIBRBD_SUPPORTS_DISCARD
61#else
62#undef LIBRBD_SUPPORTS_DISCARD
63#endif
64
Christian Brunnerf27aaf42010-12-06 20:53:01 +010065#define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
66
Josh Durginad32e9c2011-05-26 16:07:31 -070067#define RBD_MAX_SNAPS 100
68
tianqing1d393bd2017-02-21 14:50:03 +080069/* The LIBRBD_SUPPORTS_IOVEC is defined in librbd.h */
70#ifdef LIBRBD_SUPPORTS_IOVEC
71#define LIBRBD_USE_IOVEC 1
72#else
73#define LIBRBD_USE_IOVEC 0
74#endif
75
Josh Durgin787f3132012-04-30 23:16:45 -070076typedef enum {
77 RBD_AIO_READ,
78 RBD_AIO_WRITE,
Josh Durgindc7588c2013-03-29 13:03:23 -070079 RBD_AIO_DISCARD,
80 RBD_AIO_FLUSH
Josh Durgin787f3132012-04-30 23:16:45 -070081} RBDAIOCmd;
82
Christian Brunnerf27aaf42010-12-06 20:53:01 +010083typedef struct RBDAIOCB {
Markus Armbruster7c84b1b2014-10-07 13:59:14 +020084 BlockAIOCB common;
Stefan Priebe08448d52012-11-20 13:44:55 +010085 int64_t ret;
Christian Brunnerf27aaf42010-12-06 20:53:01 +010086 QEMUIOVector *qiov;
87 char *bounce;
Josh Durgin787f3132012-04-30 23:16:45 -070088 RBDAIOCmd cmd;
Christian Brunnerf27aaf42010-12-06 20:53:01 +010089 int error;
90 struct BDRVRBDState *s;
Christian Brunnerf27aaf42010-12-06 20:53:01 +010091} RBDAIOCB;
92
93typedef struct RADOSCB {
Christian Brunnerf27aaf42010-12-06 20:53:01 +010094 RBDAIOCB *acb;
95 struct BDRVRBDState *s;
Josh Durginad32e9c2011-05-26 16:07:31 -070096 int64_t size;
Christian Brunnerf27aaf42010-12-06 20:53:01 +010097 char *buf;
Stefan Priebe08448d52012-11-20 13:44:55 +010098 int64_t ret;
Christian Brunnerf27aaf42010-12-06 20:53:01 +010099} RADOSCB;
100
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100101typedef struct BDRVRBDState {
Josh Durginad32e9c2011-05-26 16:07:31 -0700102 rados_t cluster;
103 rados_ioctx_t io_ctx;
104 rbd_image_t image;
Jeff Cody80b61a22017-04-07 16:55:31 -0400105 char *image_name;
Josh Durginad32e9c2011-05-26 16:07:31 -0700106 char *snap;
Florian Florensa19ae9ae2020-01-10 12:15:13 +0100107 char *namespace;
Stefano Garzarellad24f8022019-05-09 16:59:27 +0200108 uint64_t image_size;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100109} BDRVRBDState;
110
Kevin Wolfaa045c22018-02-16 18:48:25 +0100111static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
112 BlockdevOptionsRbd *opts, bool cache,
113 const char *keypairs, const char *secretid,
114 Error **errp);
115
Markus Armbruster730b00b2017-03-28 10:56:01 +0200116static char *qemu_rbd_next_tok(char *src, char delim, char **p)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100117{
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100118 char *end;
119
120 *p = NULL;
121
Markus Armbruster8efb3392017-03-28 10:56:02 +0200122 for (end = src; *end; ++end) {
Sage Weil16a06b22011-09-19 13:35:26 -0700123 if (*end == delim) {
Markus Armbruster8efb3392017-03-28 10:56:02 +0200124 break;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100125 }
Markus Armbruster8efb3392017-03-28 10:56:02 +0200126 if (*end == '\\' && end[1] != '\0') {
127 end++;
128 }
129 }
130 if (*end == delim) {
131 *p = end + 1;
132 *end = '\0';
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100133 }
Jeff Cody7830f902017-02-24 10:30:33 -0500134 return src;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100135}
136
Sage Weil16a06b22011-09-19 13:35:26 -0700137static void qemu_rbd_unescape(char *src)
138{
139 char *p;
140
141 for (p = src; *src; ++src, ++p) {
142 if (*src == '\\' && src[1] != '\0') {
143 src++;
144 }
145 *p = *src;
146 }
147 *p = '\0';
148}
149
Jeff Codyc7cacb32017-02-26 17:50:42 -0500150static void qemu_rbd_parse_filename(const char *filename, QDict *options,
151 Error **errp)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100152{
153 const char *start;
Eric Blakee98c6962017-03-31 10:27:30 -0500154 char *p, *buf;
155 QList *keypairs = NULL;
Florian Florensa19ae9ae2020-01-10 12:15:13 +0100156 char *found_str, *image_name;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100157
158 if (!strstart(filename, "rbd:", &start)) {
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200159 error_setg(errp, "File name must start with 'rbd:'");
Jeff Codyc7cacb32017-02-26 17:50:42 -0500160 return;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100161 }
162
Anthony Liguori7267c092011-08-20 22:09:37 -0500163 buf = g_strdup(start);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100164 p = buf;
165
Markus Armbruster730b00b2017-03-28 10:56:01 +0200166 found_str = qemu_rbd_next_tok(p, '/', &p);
Jeff Cody7830f902017-02-24 10:30:33 -0500167 if (!p) {
Jeff Cody7830f902017-02-24 10:30:33 -0500168 error_setg(errp, "Pool name is required");
169 goto done;
170 }
171 qemu_rbd_unescape(found_str);
Eric Blake46f5ac22017-04-27 16:58:17 -0500172 qdict_put_str(options, "pool", found_str);
Josh Durginfab5cf52011-05-26 16:07:32 -0700173
174 if (strchr(p, '@')) {
Florian Florensa19ae9ae2020-01-10 12:15:13 +0100175 image_name = qemu_rbd_next_tok(p, '@', &p);
Jeff Cody7830f902017-02-24 10:30:33 -0500176
Markus Armbruster730b00b2017-03-28 10:56:01 +0200177 found_str = qemu_rbd_next_tok(p, ':', &p);
Jeff Cody7830f902017-02-24 10:30:33 -0500178 qemu_rbd_unescape(found_str);
Eric Blake46f5ac22017-04-27 16:58:17 -0500179 qdict_put_str(options, "snapshot", found_str);
Josh Durginfab5cf52011-05-26 16:07:32 -0700180 } else {
Florian Florensa19ae9ae2020-01-10 12:15:13 +0100181 image_name = qemu_rbd_next_tok(p, ':', &p);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100182 }
Florian Florensa19ae9ae2020-01-10 12:15:13 +0100183 /* Check for namespace in the image_name */
184 if (strchr(image_name, '/')) {
185 found_str = qemu_rbd_next_tok(image_name, '/', &image_name);
186 qemu_rbd_unescape(found_str);
187 qdict_put_str(options, "namespace", found_str);
188 } else {
189 qdict_put_str(options, "namespace", "");
190 }
191 qemu_rbd_unescape(image_name);
192 qdict_put_str(options, "image", image_name);
Jeff Cody7830f902017-02-24 10:30:33 -0500193 if (!p) {
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100194 goto done;
195 }
196
Jeff Codyc7cacb32017-02-26 17:50:42 -0500197 /* The following are essentially all key/value pairs, and we treat
198 * 'id' and 'conf' a bit special. Key/value pairs may be in any order. */
199 while (p) {
200 char *name, *value;
Markus Armbruster730b00b2017-03-28 10:56:01 +0200201 name = qemu_rbd_next_tok(p, '=', &p);
Jeff Codyc7cacb32017-02-26 17:50:42 -0500202 if (!p) {
203 error_setg(errp, "conf option %s has no value", name);
204 break;
205 }
206
207 qemu_rbd_unescape(name);
208
Markus Armbruster730b00b2017-03-28 10:56:01 +0200209 value = qemu_rbd_next_tok(p, ':', &p);
Jeff Codyc7cacb32017-02-26 17:50:42 -0500210 qemu_rbd_unescape(value);
211
212 if (!strcmp(name, "conf")) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500213 qdict_put_str(options, "conf", value);
Jeff Codyc7cacb32017-02-26 17:50:42 -0500214 } else if (!strcmp(name, "id")) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500215 qdict_put_str(options, "user", value);
Jeff Codyc7cacb32017-02-26 17:50:42 -0500216 } else {
Eric Blakee98c6962017-03-31 10:27:30 -0500217 /*
218 * We pass these internally to qemu_rbd_set_keypairs(), so
219 * we can get away with the simpler list of [ "key1",
220 * "value1", "key2", "value2" ] rather than a raw dict
221 * { "key1": "value1", "key2": "value2" } where we can't
222 * guarantee order, or even a more correct but complex
223 * [ { "key1": "value1" }, { "key2": "value2" } ]
224 */
225 if (!keypairs) {
226 keypairs = qlist_new();
Jeff Codyc7cacb32017-02-26 17:50:42 -0500227 }
Eric Blake46f5ac22017-04-27 16:58:17 -0500228 qlist_append_str(keypairs, name);
229 qlist_append_str(keypairs, value);
Jeff Codyc7cacb32017-02-26 17:50:42 -0500230 }
231 }
232
Eric Blakee98c6962017-03-31 10:27:30 -0500233 if (keypairs) {
234 qdict_put(options, "=keyvalue-pairs",
235 qobject_to_json(QOBJECT(keypairs)));
Jeff Codyc7cacb32017-02-26 17:50:42 -0500236 }
237
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100238done:
Anthony Liguori7267c092011-08-20 22:09:37 -0500239 g_free(buf);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200240 qobject_unref(keypairs);
Jeff Codyc7cacb32017-02-26 17:50:42 -0500241 return;
Sage Weil7c7e9df2011-09-07 09:28:04 -0700242}
243
Daniel P. Berrange60390a22016-01-21 14:19:19 +0000244
Eric Blakee8e16d42018-04-24 14:25:04 -0500245static void qemu_rbd_refresh_limits(BlockDriverState *bs, Error **errp)
246{
247 /* XXX Does RBD support AIO on less than 512-byte alignment? */
248 bs->bl.request_alignment = 512;
249}
250
251
Markus Armbrusterd083f952018-06-14 21:14:43 +0200252static int qemu_rbd_set_auth(rados_t cluster, BlockdevOptionsRbd *opts,
Daniel P. Berrange60390a22016-01-21 14:19:19 +0000253 Error **errp)
254{
Markus Armbrusterd083f952018-06-14 21:14:43 +0200255 char *key, *acr;
Markus Armbrustera3699de2018-06-14 21:14:42 +0200256 int r;
257 GString *accu;
258 RbdAuthModeList *auth;
259
Markus Armbrusterd083f952018-06-14 21:14:43 +0200260 if (opts->key_secret) {
261 key = qcrypto_secret_lookup_as_base64(opts->key_secret, errp);
262 if (!key) {
263 return -EIO;
Markus Armbrustera3699de2018-06-14 21:14:42 +0200264 }
Markus Armbrusterd083f952018-06-14 21:14:43 +0200265 r = rados_conf_set(cluster, "key", key);
266 g_free(key);
267 if (r < 0) {
268 error_setg_errno(errp, -r, "Could not set 'key'");
269 return r;
270 }
Daniel P. Berrange60390a22016-01-21 14:19:19 +0000271 }
272
Markus Armbrustera3699de2018-06-14 21:14:42 +0200273 if (opts->has_auth_client_required) {
274 accu = g_string_new("");
275 for (auth = opts->auth_client_required; auth; auth = auth->next) {
276 if (accu->str[0]) {
277 g_string_append_c(accu, ';');
278 }
279 g_string_append(accu, RbdAuthMode_str(auth->value));
280 }
281 acr = g_string_free(accu, FALSE);
282 r = rados_conf_set(cluster, "auth_client_required", acr);
283 g_free(acr);
284 if (r < 0) {
285 error_setg_errno(errp, -r,
286 "Could not set 'auth_client_required'");
287 return r;
288 }
Daniel P. Berrange60390a22016-01-21 14:19:19 +0000289 }
290
Daniel P. Berrange60390a22016-01-21 14:19:19 +0000291 return 0;
292}
293
Eric Blakee98c6962017-03-31 10:27:30 -0500294static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json,
Jeff Codyc7cacb32017-02-26 17:50:42 -0500295 Error **errp)
Josh Durginfab5cf52011-05-26 16:07:32 -0700296{
Eric Blakee98c6962017-03-31 10:27:30 -0500297 QList *keypairs;
298 QString *name;
299 QString *value;
300 const char *key;
301 size_t remaining;
Josh Durginfab5cf52011-05-26 16:07:32 -0700302 int ret = 0;
303
Eric Blakee98c6962017-03-31 10:27:30 -0500304 if (!keypairs_json) {
305 return ret;
306 }
Max Reitz7dc847e2018-02-24 16:40:29 +0100307 keypairs = qobject_to(QList,
308 qobject_from_json(keypairs_json, &error_abort));
Eric Blakee98c6962017-03-31 10:27:30 -0500309 remaining = qlist_size(keypairs) / 2;
310 assert(remaining);
Josh Durginfab5cf52011-05-26 16:07:32 -0700311
Eric Blakee98c6962017-03-31 10:27:30 -0500312 while (remaining--) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100313 name = qobject_to(QString, qlist_pop(keypairs));
314 value = qobject_to(QString, qlist_pop(keypairs));
Eric Blakee98c6962017-03-31 10:27:30 -0500315 assert(name && value);
316 key = qstring_get_str(name);
Josh Durginfab5cf52011-05-26 16:07:32 -0700317
Eric Blakee98c6962017-03-31 10:27:30 -0500318 ret = rados_conf_set(cluster, key, qstring_get_str(value));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200319 qobject_unref(value);
Jeff Codyc7cacb32017-02-26 17:50:42 -0500320 if (ret < 0) {
Eric Blakee98c6962017-03-31 10:27:30 -0500321 error_setg_errno(errp, -ret, "invalid conf option %s", key);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200322 qobject_unref(name);
Jeff Codyc7cacb32017-02-26 17:50:42 -0500323 ret = -EINVAL;
324 break;
Josh Durginfab5cf52011-05-26 16:07:32 -0700325 }
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200326 qobject_unref(name);
Josh Durginfab5cf52011-05-26 16:07:32 -0700327 }
328
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200329 qobject_unref(keypairs);
Josh Durginfab5cf52011-05-26 16:07:32 -0700330 return ret;
331}
332
tianqing1d393bd2017-02-21 14:50:03 +0800333static void qemu_rbd_memset(RADOSCB *rcb, int64_t offs)
334{
335 if (LIBRBD_USE_IOVEC) {
336 RBDAIOCB *acb = rcb->acb;
337 iov_memset(acb->qiov->iov, acb->qiov->niov, offs, 0,
338 acb->qiov->size - offs);
339 } else {
340 memset(rcb->buf + offs, 0, rcb->size - offs);
341 }
342}
343
Markus Armbrusterd083f952018-06-14 21:14:43 +0200344/* FIXME Deprecate and remove keypairs or make it available in QMP. */
Kevin Wolf1bebea32018-01-31 16:27:38 +0100345static int qemu_rbd_do_create(BlockdevCreateOptions *options,
346 const char *keypairs, const char *password_secret,
347 Error **errp)
348{
349 BlockdevCreateOptionsRbd *opts = &options->u.rbd;
350 rados_t cluster;
351 rados_ioctx_t io_ctx;
352 int obj_order = 0;
353 int ret;
354
355 assert(options->driver == BLOCKDEV_DRIVER_RBD);
356 if (opts->location->has_snapshot) {
357 error_setg(errp, "Can't use snapshot name for image creation");
358 return -EINVAL;
359 }
360
Kevin Wolf1bebea32018-01-31 16:27:38 +0100361 if (opts->has_cluster_size) {
362 int64_t objsize = opts->cluster_size;
363 if ((objsize - 1) & objsize) { /* not a power of 2? */
364 error_setg(errp, "obj size needs to be power of 2");
365 return -EINVAL;
366 }
367 if (objsize < 4096) {
368 error_setg(errp, "obj size too small");
369 return -EINVAL;
370 }
371 obj_order = ctz32(objsize);
372 }
373
Kevin Wolfaa045c22018-02-16 18:48:25 +0100374 ret = qemu_rbd_connect(&cluster, &io_ctx, opts->location, false, keypairs,
375 password_secret, errp);
Kevin Wolf1bebea32018-01-31 16:27:38 +0100376 if (ret < 0) {
Kevin Wolf1bebea32018-01-31 16:27:38 +0100377 return ret;
378 }
379
Kevin Wolf1bebea32018-01-31 16:27:38 +0100380 ret = rbd_create(io_ctx, opts->location->image, opts->size, &obj_order);
381 if (ret < 0) {
382 error_setg_errno(errp, -ret, "error rbd create");
Kevin Wolfaa045c22018-02-16 18:48:25 +0100383 goto out;
Kevin Wolf1bebea32018-01-31 16:27:38 +0100384 }
385
Kevin Wolf1bebea32018-01-31 16:27:38 +0100386 ret = 0;
Kevin Wolfaa045c22018-02-16 18:48:25 +0100387out:
388 rados_ioctx_destroy(io_ctx);
Kevin Wolf1bebea32018-01-31 16:27:38 +0100389 rados_shutdown(cluster);
390 return ret;
391}
392
393static int qemu_rbd_co_create(BlockdevCreateOptions *options, Error **errp)
394{
395 return qemu_rbd_do_create(options, NULL, NULL, errp);
396}
397
Maxim Levitskyb92902d2020-03-26 03:12:17 +0200398static int coroutine_fn qemu_rbd_co_create_opts(BlockDriver *drv,
399 const char *filename,
Stefan Hajnocziefc75e22018-01-18 13:43:45 +0100400 QemuOpts *opts,
401 Error **errp)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100402{
Kevin Wolf1bebea32018-01-31 16:27:38 +0100403 BlockdevCreateOptions *create_options;
404 BlockdevCreateOptionsRbd *rbd_opts;
405 BlockdevOptionsRbd *loc;
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200406 Error *local_err = NULL;
Kevin Wolf1bebea32018-01-31 16:27:38 +0100407 const char *keypairs, *password_secret;
Jeff Codyc7cacb32017-02-26 17:50:42 -0500408 QDict *options = NULL;
Jeff Codyc7cacb32017-02-26 17:50:42 -0500409 int ret = 0;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100410
Kevin Wolf1bebea32018-01-31 16:27:38 +0100411 create_options = g_new0(BlockdevCreateOptions, 1);
412 create_options->driver = BLOCKDEV_DRIVER_RBD;
413 rbd_opts = &create_options->u.rbd;
414
415 rbd_opts->location = g_new0(BlockdevOptionsRbd, 1);
416
417 password_secret = qemu_opt_get(opts, "password-secret");
Daniel P. Berrange60390a22016-01-21 14:19:19 +0000418
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100419 /* Read out options */
Kevin Wolf1bebea32018-01-31 16:27:38 +0100420 rbd_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
421 BDRV_SECTOR_SIZE);
422 rbd_opts->cluster_size = qemu_opt_get_size_del(opts,
423 BLOCK_OPT_CLUSTER_SIZE, 0);
424 rbd_opts->has_cluster_size = (rbd_opts->cluster_size != 0);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100425
Jeff Codyc7cacb32017-02-26 17:50:42 -0500426 options = qdict_new();
427 qemu_rbd_parse_filename(filename, options, &local_err);
428 if (local_err) {
429 ret = -EINVAL;
430 error_propagate(errp, local_err);
431 goto exit;
432 }
433
Markus Armbruster129c7d12017-03-30 19:43:12 +0200434 /*
435 * Caution: while qdict_get_try_str() is fine, getting non-string
436 * types would require more care. When @options come from -blockdev
437 * or blockdev_add, its members are typed according to the QAPI
438 * schema, but when they come from -drive, they're all QString.
439 */
Kevin Wolf1bebea32018-01-31 16:27:38 +0100440 loc = rbd_opts->location;
Florian Florensa19ae9ae2020-01-10 12:15:13 +0100441 loc->pool = g_strdup(qdict_get_try_str(options, "pool"));
442 loc->conf = g_strdup(qdict_get_try_str(options, "conf"));
443 loc->has_conf = !!loc->conf;
444 loc->user = g_strdup(qdict_get_try_str(options, "user"));
445 loc->has_user = !!loc->user;
446 loc->q_namespace = g_strdup(qdict_get_try_str(options, "namespace"));
447 loc->image = g_strdup(qdict_get_try_str(options, "image"));
448 keypairs = qdict_get_try_str(options, "=keyvalue-pairs");
Jeff Codyc7cacb32017-02-26 17:50:42 -0500449
Kevin Wolf1bebea32018-01-31 16:27:38 +0100450 ret = qemu_rbd_do_create(create_options, keypairs, password_secret, errp);
Vikhyat Umrao87cd3d22016-05-09 13:21:59 +0530451 if (ret < 0) {
Jeff Codyc7cacb32017-02-26 17:50:42 -0500452 goto exit;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100453 }
454
Jeff Codyc7cacb32017-02-26 17:50:42 -0500455exit:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200456 qobject_unref(options);
Kevin Wolf1bebea32018-01-31 16:27:38 +0100457 qapi_free_BlockdevCreateOptions(create_options);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100458 return ret;
459}
460
461/*
Stefan Hajnoczie04fb072013-12-05 16:38:33 +0100462 * This aio completion is being called from rbd_finish_bh() and runs in qemu
463 * BH context.
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100464 */
Josh Durginad32e9c2011-05-26 16:07:31 -0700465static void qemu_rbd_complete_aio(RADOSCB *rcb)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100466{
467 RBDAIOCB *acb = rcb->acb;
468 int64_t r;
469
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100470 r = rcb->ret;
471
Josh Durgindc7588c2013-03-29 13:03:23 -0700472 if (acb->cmd != RBD_AIO_READ) {
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100473 if (r < 0) {
474 acb->ret = r;
475 acb->error = 1;
476 } else if (!acb->error) {
Josh Durginad32e9c2011-05-26 16:07:31 -0700477 acb->ret = rcb->size;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100478 }
479 } else {
Josh Durginad32e9c2011-05-26 16:07:31 -0700480 if (r < 0) {
tianqing1d393bd2017-02-21 14:50:03 +0800481 qemu_rbd_memset(rcb, 0);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100482 acb->ret = r;
483 acb->error = 1;
Josh Durginad32e9c2011-05-26 16:07:31 -0700484 } else if (r < rcb->size) {
tianqing1d393bd2017-02-21 14:50:03 +0800485 qemu_rbd_memset(rcb, r);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100486 if (!acb->error) {
Josh Durginad32e9c2011-05-26 16:07:31 -0700487 acb->ret = rcb->size;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100488 }
489 } else if (!acb->error) {
Josh Durginad32e9c2011-05-26 16:07:31 -0700490 acb->ret = r;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100491 }
492 }
Stefan Hajnoczie04fb072013-12-05 16:38:33 +0100493
Anthony Liguori7267c092011-08-20 22:09:37 -0500494 g_free(rcb);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100495
tianqing1d393bd2017-02-21 14:50:03 +0800496 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);
Stefan Hajnoczie04fb072013-12-05 16:38:33 +0100501 }
tianqing1d393bd2017-02-21 14:50:03 +0800502
Stefan Hajnoczie04fb072013-12-05 16:38:33 +0100503 acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100504
Fam Zheng80074292014-09-11 13:41:28 +0800505 qemu_aio_unref(acb);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100506}
507
Kevin Wolf4bfb2742018-02-15 20:58:24 +0100508static char *qemu_rbd_mon_host(BlockdevOptionsRbd *opts, Error **errp)
Jeff Cody0a556792017-02-27 12:36:46 -0500509{
Kevin Wolf4bfb2742018-02-15 20:58:24 +0100510 const char **vals;
Markus Armbruster28362842017-03-28 10:56:08 +0200511 const char *host, *port;
512 char *rados_str;
Kevin Wolf4bfb2742018-02-15 20:58:24 +0100513 InetSocketAddressBaseList *p;
514 int i, cnt;
Jeff Cody0a556792017-02-27 12:36:46 -0500515
Kevin Wolf4bfb2742018-02-15 20:58:24 +0100516 if (!opts->has_server) {
517 return NULL;
518 }
519
520 for (cnt = 0, p = opts->server; p; p = p->next) {
521 cnt++;
522 }
523
524 vals = g_new(const char *, cnt + 1);
525
526 for (i = 0, p = opts->server; p; p = p->next, i++) {
527 host = p->value->host;
528 port = p->value->port;
Jeff Cody0a556792017-02-27 12:36:46 -0500529
Markus Armbruster28362842017-03-28 10:56:08 +0200530 if (strchr(host, ':')) {
Kevin Wolf4bfb2742018-02-15 20:58:24 +0100531 vals[i] = g_strdup_printf("[%s]:%s", host, port);
Jeff Cody0a556792017-02-27 12:36:46 -0500532 } else {
Kevin Wolf4bfb2742018-02-15 20:58:24 +0100533 vals[i] = g_strdup_printf("%s:%s", host, port);
Jeff Cody0a556792017-02-27 12:36:46 -0500534 }
Jeff Cody0a556792017-02-27 12:36:46 -0500535 }
Markus Armbruster28362842017-03-28 10:56:08 +0200536 vals[i] = NULL;
Jeff Cody0a556792017-02-27 12:36:46 -0500537
Markus Armbruster28362842017-03-28 10:56:08 +0200538 rados_str = i ? g_strjoinv(";", (char **)vals) : NULL;
Markus Armbruster28362842017-03-28 10:56:08 +0200539 g_strfreev((char **)vals);
Jeff Cody0a556792017-02-27 12:36:46 -0500540 return rados_str;
541}
542
Kevin Wolf3d9136f2018-02-15 19:13:47 +0100543static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
Kevin Wolf4bfb2742018-02-15 20:58:24 +0100544 BlockdevOptionsRbd *opts, bool cache,
Kevin Wolf4ff45042018-02-15 20:31:04 +0100545 const char *keypairs, const char *secretid,
546 Error **errp)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100547{
Jeff Cody0a556792017-02-27 12:36:46 -0500548 char *mon_host = NULL;
Kevin Wolf3d9136f2018-02-15 19:13:47 +0100549 Error *local_err = NULL;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100550 int r;
551
Markus Armbrusterd083f952018-06-14 21:14:43 +0200552 if (secretid) {
553 if (opts->key_secret) {
554 error_setg(errp,
555 "Legacy 'password-secret' clashes with 'key-secret'");
556 return -EINVAL;
557 }
558 opts->key_secret = g_strdup(secretid);
559 opts->has_key_secret = true;
560 }
561
Kevin Wolf4bfb2742018-02-15 20:58:24 +0100562 mon_host = qemu_rbd_mon_host(opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100563 if (local_err) {
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200564 error_propagate(errp, local_err);
Markus Armbruster28362842017-03-28 10:56:08 +0200565 r = -EINVAL;
566 goto failed_opts;
Kevin Wolfa9ccedc2013-04-12 18:05:35 +0200567 }
568
Kevin Wolf4bfb2742018-02-15 20:58:24 +0100569 r = rados_create(cluster, opts->user);
Josh Durginad32e9c2011-05-26 16:07:31 -0700570 if (r < 0) {
Vikhyat Umrao87cd3d22016-05-09 13:21:59 +0530571 error_setg_errno(errp, -r, "error initializing");
Kevin Wolfc3ca9882013-04-25 15:59:27 +0200572 goto failed_opts;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100573 }
574
Jeff Codyc7cacb32017-02-26 17:50:42 -0500575 /* try default location when conf=NULL, but ignore failure */
Kevin Wolf4bfb2742018-02-15 20:58:24 +0100576 r = rados_conf_read_file(*cluster, opts->conf);
577 if (opts->has_conf && r < 0) {
578 error_setg_errno(errp, -r, "error reading conf file %s", opts->conf);
Jeff Codyc7cacb32017-02-26 17:50:42 -0500579 goto failed_shutdown;
Josh Durgin99a3c892015-06-10 20:28:45 -0700580 }
581
Kevin Wolf3d9136f2018-02-15 19:13:47 +0100582 r = qemu_rbd_set_keypairs(*cluster, keypairs, errp);
Jeff Codyc7cacb32017-02-26 17:50:42 -0500583 if (r < 0) {
584 goto failed_shutdown;
Josh Durgin99a3c892015-06-10 20:28:45 -0700585 }
586
Jeff Cody0a556792017-02-27 12:36:46 -0500587 if (mon_host) {
Kevin Wolf3d9136f2018-02-15 19:13:47 +0100588 r = rados_conf_set(*cluster, "mon_host", mon_host);
Jeff Cody0a556792017-02-27 12:36:46 -0500589 if (r < 0) {
590 goto failed_shutdown;
591 }
592 }
593
Markus Armbrusterd083f952018-06-14 21:14:43 +0200594 r = qemu_rbd_set_auth(*cluster, opts, errp);
595 if (r < 0) {
Daniel P. Berrange60390a22016-01-21 14:19:19 +0000596 goto failed_shutdown;
597 }
598
Josh Durginb11f38f2012-05-17 13:42:29 -0700599 /*
600 * Fallback to more conservative semantics if setting cache
601 * options fails. Ignore errors from setting rbd_cache because the
602 * only possible error is that the option does not exist, and
603 * librbd defaults to no caching. If write through caching cannot
604 * be set up, fall back to no caching.
605 */
Kevin Wolf3d9136f2018-02-15 19:13:47 +0100606 if (cache) {
607 rados_conf_set(*cluster, "rbd_cache", "true");
Josh Durginb11f38f2012-05-17 13:42:29 -0700608 } else {
Kevin Wolf3d9136f2018-02-15 19:13:47 +0100609 rados_conf_set(*cluster, "rbd_cache", "false");
Josh Durginb11f38f2012-05-17 13:42:29 -0700610 }
611
Kevin Wolf3d9136f2018-02-15 19:13:47 +0100612 r = rados_connect(*cluster);
Josh Durginad32e9c2011-05-26 16:07:31 -0700613 if (r < 0) {
Vikhyat Umrao87cd3d22016-05-09 13:21:59 +0530614 error_setg_errno(errp, -r, "error connecting");
Sage Weileb93d5d2011-09-07 09:28:06 -0700615 goto failed_shutdown;
Josh Durginad32e9c2011-05-26 16:07:31 -0700616 }
617
Kevin Wolf4bfb2742018-02-15 20:58:24 +0100618 r = rados_ioctx_create(*cluster, opts->pool, io_ctx);
Josh Durginad32e9c2011-05-26 16:07:31 -0700619 if (r < 0) {
Kevin Wolf4bfb2742018-02-15 20:58:24 +0100620 error_setg_errno(errp, -r, "error opening pool %s", opts->pool);
Sage Weileb93d5d2011-09-07 09:28:06 -0700621 goto failed_shutdown;
Josh Durginad32e9c2011-05-26 16:07:31 -0700622 }
Florian Florensa19ae9ae2020-01-10 12:15:13 +0100623 /*
624 * Set the namespace after opening the io context on the pool,
625 * if nspace == NULL or if nspace == "", it is just as we did nothing
626 */
627 rados_ioctx_set_namespace(*io_ctx, opts->q_namespace);
Josh Durginad32e9c2011-05-26 16:07:31 -0700628
Kevin Wolf3d9136f2018-02-15 19:13:47 +0100629 return 0;
630
631failed_shutdown:
632 rados_shutdown(*cluster);
Kevin Wolf3d9136f2018-02-15 19:13:47 +0100633failed_opts:
Kevin Wolf3d9136f2018-02-15 19:13:47 +0100634 g_free(mon_host);
635 return r;
636}
637
Jeff Codyf24b03b2018-09-11 18:32:30 -0400638static int qemu_rbd_convert_options(QDict *options, BlockdevOptionsRbd **opts,
639 Error **errp)
640{
641 Visitor *v;
Jeff Codyf24b03b2018-09-11 18:32:30 -0400642
643 /* Convert the remaining options into a QAPI object */
644 v = qobject_input_visitor_new_flat_confused(options, errp);
645 if (!v) {
646 return -EINVAL;
647 }
648
Markus Armbrusterb11a0932020-07-07 18:06:07 +0200649 visit_type_BlockdevOptionsRbd(v, NULL, opts, errp);
Jeff Codyf24b03b2018-09-11 18:32:30 -0400650 visit_free(v);
Markus Armbrusterb11a0932020-07-07 18:06:07 +0200651 if (!opts) {
Jeff Codyf24b03b2018-09-11 18:32:30 -0400652 return -EINVAL;
653 }
654
655 return 0;
656}
657
Jeff Cody084d1d12018-09-11 18:32:31 -0400658static int qemu_rbd_attempt_legacy_options(QDict *options,
659 BlockdevOptionsRbd **opts,
660 char **keypairs)
661{
662 char *filename;
663 int r;
664
665 filename = g_strdup(qdict_get_try_str(options, "filename"));
666 if (!filename) {
667 return -EINVAL;
668 }
669 qdict_del(options, "filename");
670
671 qemu_rbd_parse_filename(filename, options, NULL);
672
673 /* keypairs freed by caller */
674 *keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
675 if (*keypairs) {
676 qdict_del(options, "=keyvalue-pairs");
677 }
678
679 r = qemu_rbd_convert_options(options, opts, NULL);
680
681 g_free(filename);
682 return r;
683}
684
Kevin Wolf3d9136f2018-02-15 19:13:47 +0100685static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
686 Error **errp)
687{
688 BDRVRBDState *s = bs->opaque;
Kevin Wolf4bfb2742018-02-15 20:58:24 +0100689 BlockdevOptionsRbd *opts = NULL;
Jeff Codybfb15b42018-04-04 11:40:45 -0400690 const QDictEntry *e;
Kevin Wolf3d9136f2018-02-15 19:13:47 +0100691 Error *local_err = NULL;
Kevin Wolf4ff45042018-02-15 20:31:04 +0100692 char *keypairs, *secretid;
Kevin Wolf3d9136f2018-02-15 19:13:47 +0100693 int r;
694
Kevin Wolf4ff45042018-02-15 20:31:04 +0100695 keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
696 if (keypairs) {
697 qdict_del(options, "=keyvalue-pairs");
698 }
699
700 secretid = g_strdup(qdict_get_try_str(options, "password-secret"));
701 if (secretid) {
702 qdict_del(options, "password-secret");
703 }
704
Jeff Codyf24b03b2018-09-11 18:32:30 -0400705 r = qemu_rbd_convert_options(options, &opts, &local_err);
Kevin Wolf4bfb2742018-02-15 20:58:24 +0100706 if (local_err) {
Jeff Cody084d1d12018-09-11 18:32:31 -0400707 /* If keypairs are present, that means some options are present in
708 * the modern option format. Don't attempt to parse legacy option
709 * formats, as we won't support mixed usage. */
710 if (keypairs) {
711 error_propagate(errp, local_err);
712 goto out;
713 }
714
715 /* If the initial attempt to convert and process the options failed,
716 * we may be attempting to open an image file that has the rbd options
717 * specified in the older format consisting of all key/value pairs
718 * encoded in the filename. Go ahead and attempt to parse the
719 * filename, and see if we can pull out the required options. */
720 r = qemu_rbd_attempt_legacy_options(options, &opts, &keypairs);
721 if (r < 0) {
722 /* Propagate the original error, not the legacy parsing fallback
723 * error, as the latter was just a best-effort attempt. */
724 error_propagate(errp, local_err);
725 goto out;
726 }
727 /* Take care whenever deciding to actually deprecate; once this ability
728 * is removed, we will not be able to open any images with legacy-styled
729 * backing image strings. */
Markus Armbruster5197f442018-10-17 10:26:27 +0200730 warn_report("RBD options encoded in the filename as keyvalue pairs "
731 "is deprecated");
Kevin Wolf4bfb2742018-02-15 20:58:24 +0100732 }
733
Jeff Codybfb15b42018-04-04 11:40:45 -0400734 /* Remove the processed options from the QDict (the visitor processes
735 * _all_ options in the QDict) */
736 while ((e = qdict_first(options))) {
737 qdict_del(options, e->key);
738 }
739
Kevin Wolfd41a5582018-02-16 18:54:52 +0100740 r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts,
741 !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp);
Kevin Wolf3d9136f2018-02-15 19:13:47 +0100742 if (r < 0) {
Kevin Wolf4ff45042018-02-15 20:31:04 +0100743 goto out;
Kevin Wolf3d9136f2018-02-15 19:13:47 +0100744 }
745
Kevin Wolfd41a5582018-02-16 18:54:52 +0100746 s->snap = g_strdup(opts->snapshot);
747 s->image_name = g_strdup(opts->image);
748
Jeff Codye2b82472017-04-07 16:55:26 -0400749 /* rbd_open is always r/w */
Jeff Cody80b61a22017-04-07 16:55:31 -0400750 r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap);
Josh Durginad32e9c2011-05-26 16:07:31 -0700751 if (r < 0) {
Jeff Cody80b61a22017-04-07 16:55:31 -0400752 error_setg_errno(errp, -r, "error reading header from %s",
753 s->image_name);
Sage Weileb93d5d2011-09-07 09:28:06 -0700754 goto failed_open;
Josh Durginad32e9c2011-05-26 16:07:31 -0700755 }
756
Stefano Garzarellad24f8022019-05-09 16:59:27 +0200757 r = rbd_get_size(s->image, &s->image_size);
758 if (r < 0) {
759 error_setg_errno(errp, -r, "error getting image size from %s",
760 s->image_name);
761 rbd_close(s->image);
762 goto failed_open;
763 }
764
Jeff Codye2b82472017-04-07 16:55:26 -0400765 /* If we are using an rbd snapshot, we must be r/o, otherwise
766 * leave as-is */
767 if (s->snap != NULL) {
Kevin Wolfeaa24102018-10-12 11:27:41 +0200768 r = bdrv_apply_auto_read_only(bs, "rbd snapshots are read-only", errp);
769 if (r < 0) {
770 rbd_close(s->image);
771 goto failed_open;
Jeff Codye2b82472017-04-07 16:55:26 -0400772 }
773 }
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100774
Eric Blake2f989102020-04-28 15:29:00 -0500775 /* When extending regular files, we get zeros from the OS */
776 bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE;
777
Kevin Wolf4ff45042018-02-15 20:31:04 +0100778 r = 0;
779 goto out;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100780
Sage Weileb93d5d2011-09-07 09:28:06 -0700781failed_open:
Josh Durginad32e9c2011-05-26 16:07:31 -0700782 rados_ioctx_destroy(s->io_ctx);
Sage Weileb93d5d2011-09-07 09:28:06 -0700783 g_free(s->snap);
Jeff Cody80b61a22017-04-07 16:55:31 -0400784 g_free(s->image_name);
Kevin Wolf3d9136f2018-02-15 19:13:47 +0100785 rados_shutdown(s->cluster);
Kevin Wolf4ff45042018-02-15 20:31:04 +0100786out:
Kevin Wolf4bfb2742018-02-15 20:58:24 +0100787 qapi_free_BlockdevOptionsRbd(opts);
Kevin Wolf4ff45042018-02-15 20:31:04 +0100788 g_free(keypairs);
789 g_free(secretid);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100790 return r;
791}
792
Jeff Cody56e7cf82017-04-07 16:55:32 -0400793
794/* Since RBD is currently always opened R/W via the API,
795 * we just need to check if we are using a snapshot or not, in
796 * order to determine if we will allow it to be R/W */
797static int qemu_rbd_reopen_prepare(BDRVReopenState *state,
798 BlockReopenQueue *queue, Error **errp)
799{
800 BDRVRBDState *s = state->bs->opaque;
801 int ret = 0;
802
803 if (s->snap && state->flags & BDRV_O_RDWR) {
804 error_setg(errp,
805 "Cannot change node '%s' to r/w when using RBD snapshot",
806 bdrv_get_device_or_node_name(state->bs));
807 ret = -EINVAL;
808 }
809
810 return ret;
811}
812
Josh Durginad32e9c2011-05-26 16:07:31 -0700813static void qemu_rbd_close(BlockDriverState *bs)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100814{
815 BDRVRBDState *s = bs->opaque;
816
Josh Durginad32e9c2011-05-26 16:07:31 -0700817 rbd_close(s->image);
818 rados_ioctx_destroy(s->io_ctx);
Anthony Liguori7267c092011-08-20 22:09:37 -0500819 g_free(s->snap);
Jeff Cody80b61a22017-04-07 16:55:31 -0400820 g_free(s->image_name);
Josh Durginad32e9c2011-05-26 16:07:31 -0700821 rados_shutdown(s->cluster);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100822}
823
Stefano Garzarellad24f8022019-05-09 16:59:27 +0200824/* Resize the RBD image and update the 'image_size' with the current size */
825static int qemu_rbd_resize(BlockDriverState *bs, uint64_t size)
826{
827 BDRVRBDState *s = bs->opaque;
828 int r;
829
830 r = rbd_resize(s->image, size);
831 if (r < 0) {
832 return r;
833 }
834
835 s->image_size = size;
836
837 return 0;
838}
839
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100840static const AIOCBInfo rbd_aiocb_info = {
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100841 .aiocb_size = sizeof(RBDAIOCB),
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100842};
843
Stefan Hajnoczie04fb072013-12-05 16:38:33 +0100844static void rbd_finish_bh(void *opaque)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100845{
Stefan Hajnoczie04fb072013-12-05 16:38:33 +0100846 RADOSCB *rcb = opaque;
Stefan Hajnoczie04fb072013-12-05 16:38:33 +0100847 qemu_rbd_complete_aio(rcb);
Josh Durginad32e9c2011-05-26 16:07:31 -0700848}
849
850/*
851 * This is the callback function for rbd_aio_read and _write
852 *
853 * Note: this function is being called from a non qemu thread so
854 * we need to be careful about what we do here. Generally we only
Stefan Hajnoczie04fb072013-12-05 16:38:33 +0100855 * schedule a BH, and do the rest of the io completion handling
856 * from rbd_finish_bh() which runs in a qemu context.
Josh Durginad32e9c2011-05-26 16:07:31 -0700857 */
858static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
859{
Stefan Hajnoczie04fb072013-12-05 16:38:33 +0100860 RBDAIOCB *acb = rcb->acb;
861
Josh Durginad32e9c2011-05-26 16:07:31 -0700862 rcb->ret = rbd_aio_get_return_value(c);
863 rbd_aio_release(c);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100864
Pavel Dovgalyuke4ec5ad2019-09-17 14:58:19 +0300865 replay_bh_schedule_oneshot_event(bdrv_get_aio_context(acb->common.bs),
866 rbd_finish_bh, rcb);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100867}
868
Josh Durgin787f3132012-04-30 23:16:45 -0700869static int rbd_aio_discard_wrapper(rbd_image_t image,
870 uint64_t off,
871 uint64_t len,
872 rbd_completion_t comp)
873{
874#ifdef LIBRBD_SUPPORTS_DISCARD
875 return rbd_aio_discard(image, off, len, comp);
876#else
877 return -ENOTSUP;
878#endif
879}
880
Josh Durgindc7588c2013-03-29 13:03:23 -0700881static int rbd_aio_flush_wrapper(rbd_image_t image,
882 rbd_completion_t comp)
883{
884#ifdef LIBRBD_SUPPORTS_AIO_FLUSH
885 return rbd_aio_flush(image, comp);
886#else
887 return -ENOTSUP;
888#endif
889}
890
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200891static BlockAIOCB *rbd_start_aio(BlockDriverState *bs,
Eric Blake7bbca9e2016-07-15 17:22:56 -0600892 int64_t off,
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200893 QEMUIOVector *qiov,
Eric Blake7bbca9e2016-07-15 17:22:56 -0600894 int64_t size,
Markus Armbruster097310b2014-10-07 13:59:15 +0200895 BlockCompletionFunc *cb,
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200896 void *opaque,
897 RBDAIOCmd cmd)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100898{
899 RBDAIOCB *acb;
Kevin Wolf0f7a0232014-05-21 18:11:48 +0200900 RADOSCB *rcb = NULL;
Josh Durginad32e9c2011-05-26 16:07:31 -0700901 rbd_completion_t c;
Josh Durgin51a13522011-05-26 16:07:33 -0700902 int r;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100903
904 BDRVRBDState *s = bs->opaque;
905
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100906 acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
Josh Durgin787f3132012-04-30 23:16:45 -0700907 acb->cmd = cmd;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100908 acb->qiov = qiov;
Eric Blake7bbca9e2016-07-15 17:22:56 -0600909 assert(!qiov || qiov->size == size);
tianqing1d393bd2017-02-21 14:50:03 +0800910
911 rcb = g_new(RADOSCB, 1);
912
913 if (!LIBRBD_USE_IOVEC) {
914 if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
915 acb->bounce = NULL;
916 } else {
917 acb->bounce = qemu_try_blockalign(bs, qiov->size);
918 if (acb->bounce == NULL) {
919 goto failed;
920 }
Kevin Wolf0f7a0232014-05-21 18:11:48 +0200921 }
tianqing1d393bd2017-02-21 14:50:03 +0800922 if (cmd == RBD_AIO_WRITE) {
923 qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
924 }
925 rcb->buf = acb->bounce;
Josh Durgin787f3132012-04-30 23:16:45 -0700926 }
tianqing1d393bd2017-02-21 14:50:03 +0800927
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100928 acb->ret = 0;
929 acb->error = 0;
930 acb->s = s;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100931
Josh Durginad32e9c2011-05-26 16:07:31 -0700932 rcb->acb = acb;
Josh Durginad32e9c2011-05-26 16:07:31 -0700933 rcb->s = acb->s;
934 rcb->size = size;
Josh Durgin51a13522011-05-26 16:07:33 -0700935 r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
936 if (r < 0) {
937 goto failed;
938 }
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100939
Josh Durgin787f3132012-04-30 23:16:45 -0700940 switch (cmd) {
Stefano Garzarellad24f8022019-05-09 16:59:27 +0200941 case RBD_AIO_WRITE: {
942 /*
943 * RBD APIs don't allow us to write more than actual size, so in order
944 * to support growing images, we resize the image before write
945 * operations that exceed the current size.
946 */
947 if (off + size > s->image_size) {
948 r = qemu_rbd_resize(bs, off + size);
949 if (r < 0) {
950 goto failed_completion;
951 }
952 }
tianqing1d393bd2017-02-21 14:50:03 +0800953#ifdef LIBRBD_SUPPORTS_IOVEC
954 r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c);
955#else
956 r = rbd_aio_write(s->image, off, size, rcb->buf, c);
957#endif
Josh Durgin787f3132012-04-30 23:16:45 -0700958 break;
Stefano Garzarellad24f8022019-05-09 16:59:27 +0200959 }
Josh Durgin787f3132012-04-30 23:16:45 -0700960 case RBD_AIO_READ:
tianqing1d393bd2017-02-21 14:50:03 +0800961#ifdef LIBRBD_SUPPORTS_IOVEC
962 r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c);
963#else
964 r = rbd_aio_read(s->image, off, size, rcb->buf, c);
965#endif
Josh Durgin787f3132012-04-30 23:16:45 -0700966 break;
967 case RBD_AIO_DISCARD:
968 r = rbd_aio_discard_wrapper(s->image, off, size, c);
969 break;
Josh Durgindc7588c2013-03-29 13:03:23 -0700970 case RBD_AIO_FLUSH:
971 r = rbd_aio_flush_wrapper(s->image, c);
972 break;
Josh Durgin787f3132012-04-30 23:16:45 -0700973 default:
974 r = -EINVAL;
Josh Durgin51a13522011-05-26 16:07:33 -0700975 }
976
977 if (r < 0) {
Kevin Wolf405a2762014-06-05 16:19:26 +0200978 goto failed_completion;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100979 }
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100980 return &acb->common;
Josh Durgin51a13522011-05-26 16:07:33 -0700981
Kevin Wolf405a2762014-06-05 16:19:26 +0200982failed_completion:
983 rbd_aio_release(c);
Josh Durgin51a13522011-05-26 16:07:33 -0700984failed:
Anthony Liguori7267c092011-08-20 22:09:37 -0500985 g_free(rcb);
tianqing1d393bd2017-02-21 14:50:03 +0800986 if (!LIBRBD_USE_IOVEC) {
987 qemu_vfree(acb->bounce);
988 }
989
Fam Zheng80074292014-09-11 13:41:28 +0800990 qemu_aio_unref(acb);
Josh Durgin51a13522011-05-26 16:07:33 -0700991 return NULL;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100992}
993
Eric Blakee8e16d42018-04-24 14:25:04 -0500994static BlockAIOCB *qemu_rbd_aio_preadv(BlockDriverState *bs,
995 uint64_t offset, uint64_t bytes,
996 QEMUIOVector *qiov, int flags,
Markus Armbruster097310b2014-10-07 13:59:15 +0200997 BlockCompletionFunc *cb,
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200998 void *opaque)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100999{
Eric Blakee8e16d42018-04-24 14:25:04 -05001000 return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque,
1001 RBD_AIO_READ);
1002}
1003
1004static BlockAIOCB *qemu_rbd_aio_pwritev(BlockDriverState *bs,
1005 uint64_t offset, uint64_t bytes,
1006 QEMUIOVector *qiov, int flags,
1007 BlockCompletionFunc *cb,
1008 void *opaque)
1009{
1010 return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque,
Josh Durgin787f3132012-04-30 23:16:45 -07001011 RBD_AIO_WRITE);
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001012}
1013
Josh Durgindc7588c2013-03-29 13:03:23 -07001014#ifdef LIBRBD_SUPPORTS_AIO_FLUSH
Markus Armbruster7c84b1b2014-10-07 13:59:14 +02001015static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02001016 BlockCompletionFunc *cb,
Markus Armbruster7c84b1b2014-10-07 13:59:14 +02001017 void *opaque)
Josh Durgindc7588c2013-03-29 13:03:23 -07001018{
1019 return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
1020}
1021
1022#else
1023
Paolo Bonzini8b94ff82011-10-20 13:16:24 +02001024static int qemu_rbd_co_flush(BlockDriverState *bs)
Sage Weil7a3f5fe2011-09-15 14:11:11 -07001025{
1026#if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
1027 /* rbd_flush added in 0.1.1 */
1028 BDRVRBDState *s = bs->opaque;
1029 return rbd_flush(s->image);
1030#else
1031 return 0;
1032#endif
1033}
Josh Durgindc7588c2013-03-29 13:03:23 -07001034#endif
Sage Weil7a3f5fe2011-09-15 14:11:11 -07001035
Josh Durginad32e9c2011-05-26 16:07:31 -07001036static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001037{
1038 BDRVRBDState *s = bs->opaque;
Josh Durginad32e9c2011-05-26 16:07:31 -07001039 rbd_image_info_t info;
1040 int r;
1041
1042 r = rbd_stat(s->image, &info, sizeof(info));
1043 if (r < 0) {
1044 return r;
1045 }
1046
1047 bdi->cluster_size = info.obj_size;
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001048 return 0;
1049}
1050
Josh Durginad32e9c2011-05-26 16:07:31 -07001051static int64_t qemu_rbd_getlength(BlockDriverState *bs)
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001052{
1053 BDRVRBDState *s = bs->opaque;
Josh Durginad32e9c2011-05-26 16:07:31 -07001054 rbd_image_info_t info;
1055 int r;
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001056
Josh Durginad32e9c2011-05-26 16:07:31 -07001057 r = rbd_stat(s->image, &info, sizeof(info));
1058 if (r < 0) {
1059 return r;
1060 }
1061
1062 return info.size;
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001063}
1064
Kevin Wolf061ca8a2018-06-21 17:54:35 +02001065static int coroutine_fn qemu_rbd_co_truncate(BlockDriverState *bs,
1066 int64_t offset,
Max Reitzc80d8b02019-09-18 11:51:40 +02001067 bool exact,
Kevin Wolf061ca8a2018-06-21 17:54:35 +02001068 PreallocMode prealloc,
Kevin Wolf92b92792020-04-24 14:54:39 +02001069 BdrvRequestFlags flags,
Kevin Wolf061ca8a2018-06-21 17:54:35 +02001070 Error **errp)
Josh Durgin30cdc482011-05-26 16:07:34 -07001071{
Josh Durgin30cdc482011-05-26 16:07:34 -07001072 int r;
1073
Max Reitz8243ccb2017-06-13 22:20:52 +02001074 if (prealloc != PREALLOC_MODE_OFF) {
1075 error_setg(errp, "Unsupported preallocation mode '%s'",
Markus Armbruster977c7362017-08-24 10:46:08 +02001076 PreallocMode_str(prealloc));
Max Reitz8243ccb2017-06-13 22:20:52 +02001077 return -ENOTSUP;
1078 }
1079
Stefano Garzarellad24f8022019-05-09 16:59:27 +02001080 r = qemu_rbd_resize(bs, offset);
Josh Durgin30cdc482011-05-26 16:07:34 -07001081 if (r < 0) {
Max Reitzf59adb32017-03-28 22:51:29 +02001082 error_setg_errno(errp, -r, "Failed to resize file");
Josh Durgin30cdc482011-05-26 16:07:34 -07001083 return r;
1084 }
1085
1086 return 0;
1087}
1088
Josh Durginad32e9c2011-05-26 16:07:31 -07001089static int qemu_rbd_snap_create(BlockDriverState *bs,
1090 QEMUSnapshotInfo *sn_info)
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001091{
1092 BDRVRBDState *s = bs->opaque;
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001093 int r;
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001094
1095 if (sn_info->name[0] == '\0') {
1096 return -EINVAL; /* we need a name for rbd snapshots */
1097 }
1098
1099 /*
1100 * rbd snapshots are using the name as the user controlled unique identifier
1101 * we can't use the rbd snapid for that purpose, as it can't be set
1102 */
1103 if (sn_info->id_str[0] != '\0' &&
1104 strcmp(sn_info->id_str, sn_info->name) != 0) {
1105 return -EINVAL;
1106 }
1107
1108 if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
1109 return -ERANGE;
1110 }
1111
Josh Durginad32e9c2011-05-26 16:07:31 -07001112 r = rbd_snap_create(s->image, sn_info->name);
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001113 if (r < 0) {
Josh Durginad32e9c2011-05-26 16:07:31 -07001114 error_report("failed to create snap: %s", strerror(-r));
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001115 return r;
1116 }
1117
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001118 return 0;
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001119}
1120
Gregory Farnumbd603242012-01-11 11:53:52 -08001121static int qemu_rbd_snap_remove(BlockDriverState *bs,
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08001122 const char *snapshot_id,
1123 const char *snapshot_name,
1124 Error **errp)
Gregory Farnumbd603242012-01-11 11:53:52 -08001125{
1126 BDRVRBDState *s = bs->opaque;
1127 int r;
1128
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08001129 if (!snapshot_name) {
1130 error_setg(errp, "rbd need a valid snapshot name");
1131 return -EINVAL;
1132 }
1133
1134 /* If snapshot_id is specified, it must be equal to name, see
1135 qemu_rbd_snap_list() */
1136 if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
1137 error_setg(errp,
1138 "rbd do not support snapshot id, it should be NULL or "
1139 "equal to snapshot name");
1140 return -EINVAL;
1141 }
1142
Gregory Farnumbd603242012-01-11 11:53:52 -08001143 r = rbd_snap_remove(s->image, snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08001144 if (r < 0) {
1145 error_setg_errno(errp, -r, "Failed to remove the snapshot");
1146 }
Gregory Farnumbd603242012-01-11 11:53:52 -08001147 return r;
1148}
1149
1150static int qemu_rbd_snap_rollback(BlockDriverState *bs,
1151 const char *snapshot_name)
1152{
1153 BDRVRBDState *s = bs->opaque;
Gregory Farnumbd603242012-01-11 11:53:52 -08001154
Eduardo Habkost9be38592016-06-13 18:57:58 -03001155 return rbd_snap_rollback(s->image, snapshot_name);
Gregory Farnumbd603242012-01-11 11:53:52 -08001156}
1157
Josh Durginad32e9c2011-05-26 16:07:31 -07001158static int qemu_rbd_snap_list(BlockDriverState *bs,
1159 QEMUSnapshotInfo **psn_tab)
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001160{
1161 BDRVRBDState *s = bs->opaque;
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001162 QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
Josh Durginad32e9c2011-05-26 16:07:31 -07001163 int i, snap_count;
1164 rbd_snap_info_t *snaps;
1165 int max_snaps = RBD_MAX_SNAPS;
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001166
Josh Durginad32e9c2011-05-26 16:07:31 -07001167 do {
Markus Armbruster02c4f262014-08-19 10:31:09 +02001168 snaps = g_new(rbd_snap_info_t, max_snaps);
Josh Durginad32e9c2011-05-26 16:07:31 -07001169 snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
Stefan Hajnoczi9e6337d2013-09-25 16:00:48 +02001170 if (snap_count <= 0) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001171 g_free(snaps);
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001172 }
Josh Durginad32e9c2011-05-26 16:07:31 -07001173 } while (snap_count == -ERANGE);
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001174
Josh Durginad32e9c2011-05-26 16:07:31 -07001175 if (snap_count <= 0) {
Josh Durginb9c53292011-12-06 17:05:10 -08001176 goto done;
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001177 }
1178
Markus Armbruster5839e532014-08-19 10:31:08 +02001179 sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001180
Josh Durginad32e9c2011-05-26 16:07:31 -07001181 for (i = 0; i < snap_count; i++) {
1182 const char *snap_name = snaps[i].name;
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001183
1184 sn_info = sn_tab + i;
1185 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
1186 pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001187
Josh Durginad32e9c2011-05-26 16:07:31 -07001188 sn_info->vm_state_size = snaps[i].size;
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001189 sn_info->date_sec = 0;
1190 sn_info->date_nsec = 0;
1191 sn_info->vm_clock_nsec = 0;
1192 }
Josh Durginad32e9c2011-05-26 16:07:31 -07001193 rbd_snap_list_end(snaps);
Stefan Hajnoczi9e6337d2013-09-25 16:00:48 +02001194 g_free(snaps);
Josh Durginad32e9c2011-05-26 16:07:31 -07001195
Josh Durginb9c53292011-12-06 17:05:10 -08001196 done:
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001197 *psn_tab = sn_tab;
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001198 return snap_count;
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001199}
1200
Josh Durgin787f3132012-04-30 23:16:45 -07001201#ifdef LIBRBD_SUPPORTS_DISCARD
Eric Blake4da444a2016-07-15 17:22:57 -06001202static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs,
1203 int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001204 int bytes,
Eric Blake4da444a2016-07-15 17:22:57 -06001205 BlockCompletionFunc *cb,
1206 void *opaque)
Josh Durgin787f3132012-04-30 23:16:45 -07001207{
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001208 return rbd_start_aio(bs, offset, NULL, bytes, cb, opaque,
Josh Durgin787f3132012-04-30 23:16:45 -07001209 RBD_AIO_DISCARD);
1210}
1211#endif
1212
Adam Crumebe217882014-10-09 11:44:32 -07001213#ifdef LIBRBD_SUPPORTS_INVALIDATE
Paolo Bonzini2b148f32018-03-01 17:36:18 +01001214static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs,
1215 Error **errp)
Adam Crumebe217882014-10-09 11:44:32 -07001216{
1217 BDRVRBDState *s = bs->opaque;
1218 int r = rbd_invalidate_cache(s->image);
1219 if (r < 0) {
1220 error_setg_errno(errp, -r, "Failed to invalidate the cache");
1221 }
1222}
1223#endif
1224
Chunyan Liubd0cf592014-06-05 17:21:04 +08001225static QemuOptsList qemu_rbd_create_opts = {
1226 .name = "rbd-create-opts",
1227 .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
1228 .desc = {
1229 {
1230 .name = BLOCK_OPT_SIZE,
1231 .type = QEMU_OPT_SIZE,
1232 .help = "Virtual disk size"
1233 },
1234 {
1235 .name = BLOCK_OPT_CLUSTER_SIZE,
1236 .type = QEMU_OPT_SIZE,
1237 .help = "RBD object size"
1238 },
Daniel P. Berrange60390a22016-01-21 14:19:19 +00001239 {
1240 .name = "password-secret",
1241 .type = QEMU_OPT_STRING,
1242 .help = "ID of secret providing the password",
1243 },
Chunyan Liubd0cf592014-06-05 17:21:04 +08001244 { /* end of list */ }
1245 }
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001246};
1247
Max Reitz26542672019-02-01 20:29:25 +01001248static const char *const qemu_rbd_strong_runtime_opts[] = {
1249 "pool",
Stefano Garzarella7bae7c82020-09-14 21:05:53 +02001250 "namespace",
Max Reitz26542672019-02-01 20:29:25 +01001251 "image",
1252 "conf",
1253 "snapshot",
1254 "user",
1255 "server.",
1256 "password-secret",
1257
1258 NULL
1259};
1260
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001261static BlockDriver bdrv_rbd = {
Jeff Codyc7cacb32017-02-26 17:50:42 -05001262 .format_name = "rbd",
1263 .instance_size = sizeof(BDRVRBDState),
1264 .bdrv_parse_filename = qemu_rbd_parse_filename,
Eric Blakee8e16d42018-04-24 14:25:04 -05001265 .bdrv_refresh_limits = qemu_rbd_refresh_limits,
Jeff Codyc7cacb32017-02-26 17:50:42 -05001266 .bdrv_file_open = qemu_rbd_open,
1267 .bdrv_close = qemu_rbd_close,
Jeff Cody56e7cf82017-04-07 16:55:32 -04001268 .bdrv_reopen_prepare = qemu_rbd_reopen_prepare,
Kevin Wolf1bebea32018-01-31 16:27:38 +01001269 .bdrv_co_create = qemu_rbd_co_create,
Stefan Hajnocziefc75e22018-01-18 13:43:45 +01001270 .bdrv_co_create_opts = qemu_rbd_co_create_opts,
Jeff Codyc7cacb32017-02-26 17:50:42 -05001271 .bdrv_has_zero_init = bdrv_has_zero_init_1,
1272 .bdrv_get_info = qemu_rbd_getinfo,
1273 .create_opts = &qemu_rbd_create_opts,
1274 .bdrv_getlength = qemu_rbd_getlength,
Kevin Wolf061ca8a2018-06-21 17:54:35 +02001275 .bdrv_co_truncate = qemu_rbd_co_truncate,
Jeff Codyc7cacb32017-02-26 17:50:42 -05001276 .protocol_name = "rbd",
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001277
Eric Blakee8e16d42018-04-24 14:25:04 -05001278 .bdrv_aio_preadv = qemu_rbd_aio_preadv,
1279 .bdrv_aio_pwritev = qemu_rbd_aio_pwritev,
Josh Durgindc7588c2013-03-29 13:03:23 -07001280
1281#ifdef LIBRBD_SUPPORTS_AIO_FLUSH
1282 .bdrv_aio_flush = qemu_rbd_aio_flush,
1283#else
Kevin Wolfc68b89a2011-11-10 17:25:44 +01001284 .bdrv_co_flush_to_disk = qemu_rbd_co_flush,
Josh Durgindc7588c2013-03-29 13:03:23 -07001285#endif
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001286
Josh Durgin787f3132012-04-30 23:16:45 -07001287#ifdef LIBRBD_SUPPORTS_DISCARD
Eric Blake4da444a2016-07-15 17:22:57 -06001288 .bdrv_aio_pdiscard = qemu_rbd_aio_pdiscard,
Josh Durgin787f3132012-04-30 23:16:45 -07001289#endif
1290
Kevin Wolfc68b89a2011-11-10 17:25:44 +01001291 .bdrv_snapshot_create = qemu_rbd_snap_create,
Gregory Farnumbd603242012-01-11 11:53:52 -08001292 .bdrv_snapshot_delete = qemu_rbd_snap_remove,
Kevin Wolfc68b89a2011-11-10 17:25:44 +01001293 .bdrv_snapshot_list = qemu_rbd_snap_list,
Gregory Farnumbd603242012-01-11 11:53:52 -08001294 .bdrv_snapshot_goto = qemu_rbd_snap_rollback,
Adam Crumebe217882014-10-09 11:44:32 -07001295#ifdef LIBRBD_SUPPORTS_INVALIDATE
Paolo Bonzini2b148f32018-03-01 17:36:18 +01001296 .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache,
Adam Crumebe217882014-10-09 11:44:32 -07001297#endif
Max Reitz26542672019-02-01 20:29:25 +01001298
1299 .strong_runtime_opts = qemu_rbd_strong_runtime_opts,
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001300};
1301
1302static void bdrv_rbd_init(void)
1303{
1304 bdrv_register(&bdrv_rbd);
1305}
1306
1307block_init(bdrv_rbd_init);