#include "qemu/osdep.h"
+#include <rbd/librbd.h>
#include "qapi/error.h"
#include "qemu/error-report.h"
#include "block/block_int.h"
#include "crypto/secret.h"
#include "qemu/cutils.h"
#include "qapi/qmp/qstring.h"
-
-#include <rbd/librbd.h>
+#include "qapi/qmp/qjson.h"
/*
* When specifying the image filename use:
rados_t cluster;
rados_ioctx_t io_ctx;
rbd_image_t image;
- char *name;
+ char *image_name;
char *snap;
} BDRVRBDState;
Error **errp)
{
const char *start;
- char *p, *buf, *keypairs;
+ char *p, *buf;
+ QList *keypairs = NULL;
char *found_str;
- size_t max_keypair_size;
if (!strstart(filename, "rbd:", &start)) {
error_setg(errp, "File name must start with 'rbd:'");
return;
}
- max_keypair_size = strlen(start) + 1;
buf = g_strdup(start);
- keypairs = g_malloc0(max_keypair_size);
p = buf;
found_str = qemu_rbd_next_tok(p, '/', &p);
goto done;
}
qemu_rbd_unescape(found_str);
- qdict_put(options, "pool", qstring_from_str(found_str));
+ qdict_put_str(options, "pool", found_str);
if (strchr(p, '@')) {
found_str = qemu_rbd_next_tok(p, '@', &p);
qemu_rbd_unescape(found_str);
- qdict_put(options, "image", qstring_from_str(found_str));
+ qdict_put_str(options, "image", found_str);
found_str = qemu_rbd_next_tok(p, ':', &p);
qemu_rbd_unescape(found_str);
- qdict_put(options, "snapshot", qstring_from_str(found_str));
+ qdict_put_str(options, "snapshot", found_str);
} else {
found_str = qemu_rbd_next_tok(p, ':', &p);
qemu_rbd_unescape(found_str);
- qdict_put(options, "image", qstring_from_str(found_str));
+ qdict_put_str(options, "image", found_str);
}
if (!p) {
goto done;
qemu_rbd_unescape(value);
if (!strcmp(name, "conf")) {
- qdict_put(options, "conf", qstring_from_str(value));
+ qdict_put_str(options, "conf", value);
} else if (!strcmp(name, "id")) {
- qdict_put(options, "user" , qstring_from_str(value));
+ qdict_put_str(options, "user", value);
} else {
- /* FIXME: This is pretty ugly, and not the right way to do this.
- * These should be contained in a structure, and then
- * passed explicitly as individual key/value pairs to
- * rados. Consider this legacy code that needs to be
- * updated. */
- char *tmp = g_malloc0(max_keypair_size);
- /* only use a delimiter if it is not the first keypair found */
- /* These are sets of unknown key/value pairs we'll pass along
- * to ceph */
- if (keypairs[0]) {
- snprintf(tmp, max_keypair_size, ":%s=%s", name, value);
- pstrcat(keypairs, max_keypair_size, tmp);
- } else {
- snprintf(keypairs, max_keypair_size, "%s=%s", name, value);
+ /*
+ * We pass these internally to qemu_rbd_set_keypairs(), so
+ * we can get away with the simpler list of [ "key1",
+ * "value1", "key2", "value2" ] rather than a raw dict
+ * { "key1": "value1", "key2": "value2" } where we can't
+ * guarantee order, or even a more correct but complex
+ * [ { "key1": "value1" }, { "key2": "value2" } ]
+ */
+ if (!keypairs) {
+ keypairs = qlist_new();
}
- g_free(tmp);
+ qlist_append_str(keypairs, name);
+ qlist_append_str(keypairs, value);
}
}
- if (keypairs[0]) {
- qdict_put(options, "=keyvalue-pairs", qstring_from_str(keypairs));
+ if (keypairs) {
+ qdict_put(options, "=keyvalue-pairs",
+ qobject_to_json(QOBJECT(keypairs)));
}
-
done:
g_free(buf);
- g_free(keypairs);
+ QDECREF(keypairs);
return;
}
return 0;
}
-static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs,
+static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json,
Error **errp)
{
- char *p, *buf;
- char *name;
- char *value;
+ QList *keypairs;
+ QString *name;
+ QString *value;
+ const char *key;
+ size_t remaining;
int ret = 0;
- buf = g_strdup(keypairs);
- p = buf;
-
- while (p) {
- name = qemu_rbd_next_tok(p, '=', &p);
- if (!p) {
- error_setg(errp, "conf option %s has no value", name);
- ret = -EINVAL;
- break;
- }
-
- value = qemu_rbd_next_tok(p, ':', &p);
-
- ret = rados_conf_set(cluster, name, value);
+ if (!keypairs_json) {
+ return ret;
+ }
+ keypairs = qobject_to_qlist(qobject_from_json(keypairs_json,
+ &error_abort));
+ remaining = qlist_size(keypairs) / 2;
+ assert(remaining);
+
+ while (remaining--) {
+ name = qobject_to_qstring(qlist_pop(keypairs));
+ value = qobject_to_qstring(qlist_pop(keypairs));
+ assert(name && value);
+ key = qstring_get_str(name);
+
+ ret = rados_conf_set(cluster, key, qstring_get_str(value));
+ QDECREF(name);
+ QDECREF(value);
if (ret < 0) {
- error_setg_errno(errp, -ret, "invalid conf option %s", name);
+ error_setg_errno(errp, -ret, "invalid conf option %s", key);
ret = -EINVAL;
break;
}
}
- g_free(buf);
+ QDECREF(keypairs);
return ret;
}
.help = "Rados id name",
},
/*
- * server.* and auth-supported.* extracted manually, see
- * qemu_rbd_array_opts()
+ * server.* extracted manually, see qemu_rbd_mon_host()
*/
{
.name = "password-secret",
.type = QEMU_OPT_STRING,
.help = "Legacy rados key/value option parameters",
},
-
- /*
- * The remainder aren't option keys, but option sub-sub-keys,
- * so that qemu_rbd_array_opts() can abuse runtime_opts for
- * its own purposes
- * TODO clean this up
- */
- {
- .name = "host",
- .type = QEMU_OPT_STRING,
- },
- {
- .name = "port",
- .type = QEMU_OPT_STRING,
- },
{
- .name = "auth",
+ .name = "filename",
.type = QEMU_OPT_STRING,
- .help = "Supported authentication method, either cephx or none",
},
{ /* end of list */ }
},
int64_t bytes = 0;
int64_t objsize;
int obj_order = 0;
- const char *pool, *name, *conf, *clientname, *keypairs;
+ const char *pool, *image_name, *conf, *user, *keypairs;
const char *secretid;
rados_t cluster;
rados_ioctx_t io_ctx;
QDict *options = NULL;
- QemuOpts *rbd_opts = NULL;
int ret = 0;
secretid = qemu_opt_get(opts, "password-secret");
goto exit;
}
- rbd_opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
- qemu_opts_absorb_qdict(rbd_opts, options, &local_err);
- if (local_err) {
- error_propagate(errp, local_err);
- ret = -EINVAL;
- goto exit;
- }
-
- pool = qemu_opt_get(rbd_opts, "pool");
- conf = qemu_opt_get(rbd_opts, "conf");
- clientname = qemu_opt_get(rbd_opts, "user");
- name = qemu_opt_get(rbd_opts, "image");
- keypairs = qemu_opt_get(rbd_opts, "=keyvalue-pairs");
+ /*
+ * Caution: while qdict_get_try_str() is fine, getting non-string
+ * types would require more care. When @options come from -blockdev
+ * or blockdev_add, its members are typed according to the QAPI
+ * schema, but when they come from -drive, they're all QString.
+ */
+ pool = qdict_get_try_str(options, "pool");
+ conf = qdict_get_try_str(options, "conf");
+ user = qdict_get_try_str(options, "user");
+ image_name = qdict_get_try_str(options, "image");
+ keypairs = qdict_get_try_str(options, "=keyvalue-pairs");
- ret = rados_create(&cluster, clientname);
+ ret = rados_create(&cluster, user);
if (ret < 0) {
error_setg_errno(errp, -ret, "error initializing");
goto exit;
goto shutdown;
}
- ret = rbd_create(io_ctx, name, bytes, &obj_order);
+ ret = rbd_create(io_ctx, image_name, bytes, &obj_order);
if (ret < 0) {
error_setg_errno(errp, -ret, "error rbd create");
}
exit:
QDECREF(options);
- qemu_opts_del(rbd_opts);
return ret;
}
qemu_aio_unref(acb);
}
-#define RBD_MON_HOST 0
-#define RBD_AUTH_SUPPORTED 1
-
-static char *qemu_rbd_array_opts(QDict *options, const char *prefix, int type,
- Error **errp)
+static char *qemu_rbd_mon_host(QDict *options, Error **errp)
{
- int num_entries;
- QemuOpts *opts = NULL;
- QDict *sub_options;
- const char *host;
- const char *port;
- char *str;
- char *rados_str = NULL;
- Error *local_err = NULL;
+ const char **vals = g_new(const char *, qdict_size(options) + 1);
+ char keybuf[32];
+ const char *host, *port;
+ char *rados_str;
int i;
- assert(type == RBD_MON_HOST || type == RBD_AUTH_SUPPORTED);
-
- num_entries = qdict_array_entries(options, prefix);
-
- if (num_entries < 0) {
- error_setg(errp, "Parse error on RBD QDict array");
- return NULL;
- }
-
- for (i = 0; i < num_entries; i++) {
- char *strbuf = NULL;
- const char *value;
- char *rados_str_tmp;
-
- str = g_strdup_printf("%s%d.", prefix, i);
- qdict_extract_subqdict(options, &sub_options, str);
- g_free(str);
-
- opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
- qemu_opts_absorb_qdict(opts, sub_options, &local_err);
- QDECREF(sub_options);
- if (local_err) {
- error_propagate(errp, local_err);
- g_free(rados_str);
- rados_str = NULL;
- goto exit;
+ for (i = 0;; i++) {
+ sprintf(keybuf, "server.%d.host", i);
+ host = qdict_get_try_str(options, keybuf);
+ qdict_del(options, keybuf);
+ sprintf(keybuf, "server.%d.port", i);
+ port = qdict_get_try_str(options, keybuf);
+ qdict_del(options, keybuf);
+ if (!host && !port) {
+ break;
}
-
- if (type == RBD_MON_HOST) {
- host = qemu_opt_get(opts, "host");
- port = qemu_opt_get(opts, "port");
-
- value = host;
- if (port) {
- /* check for ipv6 */
- if (strchr(host, ':')) {
- strbuf = g_strdup_printf("[%s]:%s", host, port);
- } else {
- strbuf = g_strdup_printf("%s:%s", host, port);
- }
- value = strbuf;
- } else if (strchr(host, ':')) {
- strbuf = g_strdup_printf("[%s]", host);
- value = strbuf;
- }
- } else {
- value = qemu_opt_get(opts, "auth");
+ if (!host) {
+ error_setg(errp, "Parameter server.%d.host is missing", i);
+ rados_str = NULL;
+ goto out;
}
-
- /* each iteration in the for loop will build upon the string, and if
- * rados_str is NULL then it is our first pass */
- if (rados_str) {
- /* separate options with ';', as that is what rados_conf_set()
- * requires */
- rados_str_tmp = rados_str;
- rados_str = g_strdup_printf("%s;%s", rados_str_tmp, value);
- g_free(rados_str_tmp);
+ if (strchr(host, ':')) {
+ vals[i] = port ? g_strdup_printf("[%s]:%s", host, port)
+ : g_strdup_printf("[%s]", host);
} else {
- rados_str = g_strdup(value);
+ vals[i] = port ? g_strdup_printf("%s:%s", host, port)
+ : g_strdup(host);
}
-
- g_free(strbuf);
- qemu_opts_del(opts);
- opts = NULL;
}
+ vals[i] = NULL;
-exit:
- qemu_opts_del(opts);
+ rados_str = i ? g_strjoinv(";", (char **)vals) : NULL;
+out:
+ g_strfreev((char **)vals);
return rados_str;
}
Error **errp)
{
BDRVRBDState *s = bs->opaque;
- const char *pool, *snap, *conf, *clientname, *name, *keypairs;
- const char *secretid;
+ const char *pool, *snap, *conf, *user, *image_name, *keypairs;
+ const char *secretid, *filename;
QemuOpts *opts;
Error *local_err = NULL;
char *mon_host = NULL;
- char *auth_supported = NULL;
int r;
- opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
- qemu_opts_absorb_qdict(opts, options, &local_err);
- if (local_err) {
- error_propagate(errp, local_err);
- qemu_opts_del(opts);
- return -EINVAL;
+ /* If we are given a filename, parse the filename, with precedence given to
+ * filename encoded options */
+ filename = qdict_get_try_str(options, "filename");
+ if (filename) {
+ warn_report("'filename' option specified. "
+ "This is an unsupported option, and may be deprecated "
+ "in the future");
+ qemu_rbd_parse_filename(filename, options, &local_err);
+ if (local_err) {
+ r = -EINVAL;
+ error_propagate(errp, local_err);
+ goto exit;
+ }
}
- auth_supported = qemu_rbd_array_opts(options, "auth-supported.",
- RBD_AUTH_SUPPORTED, &local_err);
+ opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
+ qemu_opts_absorb_qdict(opts, options, &local_err);
if (local_err) {
error_propagate(errp, local_err);
r = -EINVAL;
goto failed_opts;
}
- mon_host = qemu_rbd_array_opts(options, "server.",
- RBD_MON_HOST, &local_err);
+ mon_host = qemu_rbd_mon_host(options, &local_err);
if (local_err) {
error_propagate(errp, local_err);
r = -EINVAL;
pool = qemu_opt_get(opts, "pool");
conf = qemu_opt_get(opts, "conf");
snap = qemu_opt_get(opts, "snapshot");
- clientname = qemu_opt_get(opts, "user");
- name = qemu_opt_get(opts, "image");
+ user = qemu_opt_get(opts, "user");
+ image_name = qemu_opt_get(opts, "image");
keypairs = qemu_opt_get(opts, "=keyvalue-pairs");
- if (!pool || !name) {
+ if (!pool || !image_name) {
error_setg(errp, "Parameters 'pool' and 'image' are required");
r = -EINVAL;
goto failed_opts;
}
- r = rados_create(&s->cluster, clientname);
+ r = rados_create(&s->cluster, user);
if (r < 0) {
error_setg_errno(errp, -r, "error initializing");
goto failed_opts;
}
s->snap = g_strdup(snap);
- s->name = g_strdup(name);
+ s->image_name = g_strdup(image_name);
/* try default location when conf=NULL, but ignore failure */
r = rados_conf_read_file(s->cluster, conf);
}
}
- if (auth_supported) {
- r = rados_conf_set(s->cluster, "auth_supported", auth_supported);
- if (r < 0) {
- goto failed_shutdown;
- }
- }
-
if (qemu_rbd_set_auth(s->cluster, secretid, errp) < 0) {
r = -EIO;
goto failed_shutdown;
goto failed_shutdown;
}
- r = rbd_open(s->io_ctx, s->name, &s->image, s->snap);
+ /* rbd_open is always r/w */
+ r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap);
if (r < 0) {
- error_setg_errno(errp, -r, "error reading header from %s", s->name);
+ error_setg_errno(errp, -r, "error reading header from %s",
+ s->image_name);
goto failed_open;
}
- bs->read_only = (s->snap != NULL);
+ /* If we are using an rbd snapshot, we must be r/o, otherwise
+ * leave as-is */
+ if (s->snap != NULL) {
+ if (!bdrv_is_read_only(bs)) {
+ error_report("Opening rbd snapshots without an explicit "
+ "read-only=on option is deprecated. Future versions "
+ "will refuse to open the image instead of "
+ "automatically marking the image read-only.");
+ r = bdrv_set_read_only(bs, true, &local_err);
+ if (r < 0) {
+ error_propagate(errp, local_err);
+ goto failed_open;
+ }
+ }
+ }
qemu_opts_del(opts);
return 0;
failed_shutdown:
rados_shutdown(s->cluster);
g_free(s->snap);
- g_free(s->name);
+ g_free(s->image_name);
failed_opts:
qemu_opts_del(opts);
g_free(mon_host);
- g_free(auth_supported);
+exit:
return r;
}
+
+/* Since RBD is currently always opened R/W via the API,
+ * we just need to check if we are using a snapshot or not, in
+ * order to determine if we will allow it to be R/W */
+static int qemu_rbd_reopen_prepare(BDRVReopenState *state,
+ BlockReopenQueue *queue, Error **errp)
+{
+ BDRVRBDState *s = state->bs->opaque;
+ int ret = 0;
+
+ if (s->snap && state->flags & BDRV_O_RDWR) {
+ error_setg(errp,
+ "Cannot change node '%s' to r/w when using RBD snapshot",
+ bdrv_get_device_or_node_name(state->bs));
+ ret = -EINVAL;
+ }
+
+ return ret;
+}
+
static void qemu_rbd_close(BlockDriverState *bs)
{
BDRVRBDState *s = bs->opaque;
rbd_close(s->image);
rados_ioctx_destroy(s->io_ctx);
g_free(s->snap);
- g_free(s->name);
+ g_free(s->image_name);
rados_shutdown(s->cluster);
}
return info.size;
}
-static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset)
+static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset,
+ PreallocMode prealloc, Error **errp)
{
BDRVRBDState *s = bs->opaque;
int r;
+ if (prealloc != PREALLOC_MODE_OFF) {
+ error_setg(errp, "Unsupported preallocation mode '%s'",
+ PreallocMode_str(prealloc));
+ return -ENOTSUP;
+ }
+
r = rbd_resize(s->image, offset);
if (r < 0) {
+ error_setg_errno(errp, -r, "Failed to resize file");
return r;
}
#ifdef LIBRBD_SUPPORTS_DISCARD
static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs,
int64_t offset,
- int count,
+ int bytes,
BlockCompletionFunc *cb,
void *opaque)
{
- return rbd_start_aio(bs, offset, NULL, count, cb, opaque,
+ return rbd_start_aio(bs, offset, NULL, bytes, cb, opaque,
RBD_AIO_DISCARD);
}
#endif
.bdrv_parse_filename = qemu_rbd_parse_filename,
.bdrv_file_open = qemu_rbd_open,
.bdrv_close = qemu_rbd_close,
+ .bdrv_reopen_prepare = qemu_rbd_reopen_prepare,
.bdrv_create = qemu_rbd_create,
.bdrv_has_zero_init = bdrv_has_zero_init_1,
.bdrv_get_info = qemu_rbd_getinfo,