1f27aaf4bSChristian Brunner /* 2f27aaf4bSChristian Brunner * QEMU Block driver for RADOS (Ceph) 3f27aaf4bSChristian Brunner * 4ad32e9c0SJosh Durgin * Copyright (C) 2010-2011 Christian Brunner <chb@muc.de>, 5ad32e9c0SJosh Durgin * Josh Durgin <josh.durgin@dreamhost.com> 6f27aaf4bSChristian Brunner * 7f27aaf4bSChristian Brunner * This work is licensed under the terms of the GNU GPL, version 2. See 8f27aaf4bSChristian Brunner * the COPYING file in the top-level directory. 9f27aaf4bSChristian Brunner * 106b620ca3SPaolo Bonzini * Contributions after 2012-01-13 are licensed under the terms of the 116b620ca3SPaolo Bonzini * GNU GPL, version 2 or (at your option) any later version. 12f27aaf4bSChristian Brunner */ 13f27aaf4bSChristian Brunner 1480c71a24SPeter Maydell #include "qemu/osdep.h" 15ad32e9c0SJosh Durgin 162836284dSMarkus Armbruster #include <rbd/librbd.h> 17da34e65cSMarkus Armbruster #include "qapi/error.h" 181de7afc9SPaolo Bonzini #include "qemu/error-report.h" 190b8fa32fSMarkus Armbruster #include "qemu/module.h" 20922a01a0SMarkus Armbruster #include "qemu/option.h" 21737e150eSPaolo Bonzini #include "block/block_int.h" 22609f45eaSMax Reitz #include "block/qdict.h" 2360390a21SDaniel P. Berrange #include "crypto/secret.h" 24f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 25e4ec5ad4SPavel Dovgalyuk #include "sysemu/replay.h" 26c7cacb3eSJeff Cody #include "qapi/qmp/qstring.h" 27452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h" 28e98c6961SEric Blake #include "qapi/qmp/qjson.h" 2947e6b297SMarkus Armbruster #include "qapi/qmp/qlist.h" 304bfb2741SKevin Wolf #include "qapi/qobject-input-visitor.h" 314bfb2741SKevin Wolf #include "qapi/qapi-visit-block-core.h" 32f27aaf4bSChristian Brunner 33f27aaf4bSChristian Brunner /* 34f27aaf4bSChristian Brunner * When specifying the image filename use: 35f27aaf4bSChristian Brunner * 36fab5cf59SJosh Durgin * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]] 37f27aaf4bSChristian Brunner * 389e1fbcdeSSage Weil * poolname must be the name of an existing rados pool. 39f27aaf4bSChristian Brunner * 409e1fbcdeSSage Weil * devicename is the name of the rbd image. 41f27aaf4bSChristian Brunner * 429e1fbcdeSSage Weil * Each option given is used to configure rados, and may be any valid 439e1fbcdeSSage Weil * Ceph option, "id", or "conf". 44fab5cf59SJosh Durgin * 459e1fbcdeSSage Weil * The "id" option indicates what user we should authenticate as to 469e1fbcdeSSage Weil * the Ceph cluster. If it is excluded we will use the Ceph default 479e1fbcdeSSage Weil * (normally 'admin'). 48f27aaf4bSChristian Brunner * 499e1fbcdeSSage Weil * The "conf" option specifies a Ceph configuration file to read. If 509e1fbcdeSSage Weil * it is not specified, we will read from the default Ceph locations 519e1fbcdeSSage Weil * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration 529e1fbcdeSSage Weil * file, specify conf=/dev/null. 53f27aaf4bSChristian Brunner * 549e1fbcdeSSage Weil * Configuration values containing :, @, or = can be escaped with a 559e1fbcdeSSage Weil * leading "\". 56f27aaf4bSChristian Brunner */ 57f27aaf4bSChristian Brunner 58787f3133SJosh Durgin /* rbd_aio_discard added in 0.1.2 */ 59787f3133SJosh Durgin #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2) 60787f3133SJosh Durgin #define LIBRBD_SUPPORTS_DISCARD 61787f3133SJosh Durgin #else 62787f3133SJosh Durgin #undef LIBRBD_SUPPORTS_DISCARD 63787f3133SJosh Durgin #endif 64787f3133SJosh Durgin 65f27aaf4bSChristian Brunner #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER) 66f27aaf4bSChristian Brunner 67ad32e9c0SJosh Durgin #define RBD_MAX_SNAPS 100 68ad32e9c0SJosh Durgin 691d393bdeStianqing /* The LIBRBD_SUPPORTS_IOVEC is defined in librbd.h */ 701d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC 711d393bdeStianqing #define LIBRBD_USE_IOVEC 1 721d393bdeStianqing #else 731d393bdeStianqing #define LIBRBD_USE_IOVEC 0 741d393bdeStianqing #endif 751d393bdeStianqing 76787f3133SJosh Durgin typedef enum { 77787f3133SJosh Durgin RBD_AIO_READ, 78787f3133SJosh Durgin RBD_AIO_WRITE, 79dc7588c1SJosh Durgin RBD_AIO_DISCARD, 80dc7588c1SJosh Durgin RBD_AIO_FLUSH 81787f3133SJosh Durgin } RBDAIOCmd; 82787f3133SJosh Durgin 83f27aaf4bSChristian Brunner typedef struct RBDAIOCB { 847c84b1b8SMarkus Armbruster BlockAIOCB common; 8508448d51SStefan Priebe int64_t ret; 86f27aaf4bSChristian Brunner QEMUIOVector *qiov; 87f27aaf4bSChristian Brunner char *bounce; 88787f3133SJosh Durgin RBDAIOCmd cmd; 89f27aaf4bSChristian Brunner int error; 90f27aaf4bSChristian Brunner struct BDRVRBDState *s; 91f27aaf4bSChristian Brunner } RBDAIOCB; 92f27aaf4bSChristian Brunner 93f27aaf4bSChristian Brunner typedef struct RADOSCB { 94f27aaf4bSChristian Brunner RBDAIOCB *acb; 95f27aaf4bSChristian Brunner struct BDRVRBDState *s; 96ad32e9c0SJosh Durgin int64_t size; 97f27aaf4bSChristian Brunner char *buf; 9808448d51SStefan Priebe int64_t ret; 99f27aaf4bSChristian Brunner } RADOSCB; 100f27aaf4bSChristian Brunner 101f27aaf4bSChristian Brunner typedef struct BDRVRBDState { 102ad32e9c0SJosh Durgin rados_t cluster; 103ad32e9c0SJosh Durgin rados_ioctx_t io_ctx; 104ad32e9c0SJosh Durgin rbd_image_t image; 10580b61a27SJeff Cody char *image_name; 106ad32e9c0SJosh Durgin char *snap; 107*19ae9ae0SFlorian Florensa char *namespace; 108d24f8023SStefano Garzarella uint64_t image_size; 109f27aaf4bSChristian Brunner } BDRVRBDState; 110f27aaf4bSChristian Brunner 111aa045c2dSKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx, 112aa045c2dSKevin Wolf BlockdevOptionsRbd *opts, bool cache, 113aa045c2dSKevin Wolf const char *keypairs, const char *secretid, 114aa045c2dSKevin Wolf Error **errp); 115aa045c2dSKevin Wolf 116730b00bbSMarkus Armbruster static char *qemu_rbd_next_tok(char *src, char delim, char **p) 117f27aaf4bSChristian Brunner { 118f27aaf4bSChristian Brunner char *end; 119f27aaf4bSChristian Brunner 120f27aaf4bSChristian Brunner *p = NULL; 121f27aaf4bSChristian Brunner 12216a06b24SSage Weil for (end = src; *end; ++end) { 12316a06b24SSage Weil if (*end == delim) { 12416a06b24SSage Weil break; 12516a06b24SSage Weil } 12616a06b24SSage Weil if (*end == '\\' && end[1] != '\0') { 12716a06b24SSage Weil end++; 12816a06b24SSage Weil } 12916a06b24SSage Weil } 13016a06b24SSage Weil if (*end == delim) { 131f27aaf4bSChristian Brunner *p = end + 1; 132f27aaf4bSChristian Brunner *end = '\0'; 133f27aaf4bSChristian Brunner } 1347830f909SJeff Cody return src; 135f27aaf4bSChristian Brunner } 136f27aaf4bSChristian Brunner 13716a06b24SSage Weil static void qemu_rbd_unescape(char *src) 13816a06b24SSage Weil { 13916a06b24SSage Weil char *p; 14016a06b24SSage Weil 14116a06b24SSage Weil for (p = src; *src; ++src, ++p) { 14216a06b24SSage Weil if (*src == '\\' && src[1] != '\0') { 14316a06b24SSage Weil src++; 14416a06b24SSage Weil } 14516a06b24SSage Weil *p = *src; 14616a06b24SSage Weil } 14716a06b24SSage Weil *p = '\0'; 14816a06b24SSage Weil } 14916a06b24SSage Weil 150c7cacb3eSJeff Cody static void qemu_rbd_parse_filename(const char *filename, QDict *options, 151d61563b2SMarkus Armbruster Error **errp) 152f27aaf4bSChristian Brunner { 153f27aaf4bSChristian Brunner const char *start; 154e98c6961SEric Blake char *p, *buf; 155e98c6961SEric Blake QList *keypairs = NULL; 156*19ae9ae0SFlorian Florensa char *found_str, *image_name; 157f27aaf4bSChristian Brunner 158f27aaf4bSChristian Brunner if (!strstart(filename, "rbd:", &start)) { 159d61563b2SMarkus Armbruster error_setg(errp, "File name must start with 'rbd:'"); 160c7cacb3eSJeff Cody return; 161f27aaf4bSChristian Brunner } 162f27aaf4bSChristian Brunner 1637267c094SAnthony Liguori buf = g_strdup(start); 164f27aaf4bSChristian Brunner p = buf; 165f27aaf4bSChristian Brunner 166730b00bbSMarkus Armbruster found_str = qemu_rbd_next_tok(p, '/', &p); 1677830f909SJeff Cody if (!p) { 1687830f909SJeff Cody error_setg(errp, "Pool name is required"); 1697830f909SJeff Cody goto done; 1707830f909SJeff Cody } 1717830f909SJeff Cody qemu_rbd_unescape(found_str); 17246f5ac20SEric Blake qdict_put_str(options, "pool", found_str); 173fab5cf59SJosh Durgin 174fab5cf59SJosh Durgin if (strchr(p, '@')) { 175*19ae9ae0SFlorian Florensa image_name = qemu_rbd_next_tok(p, '@', &p); 1767830f909SJeff Cody 177730b00bbSMarkus Armbruster found_str = qemu_rbd_next_tok(p, ':', &p); 1787830f909SJeff Cody qemu_rbd_unescape(found_str); 17946f5ac20SEric Blake qdict_put_str(options, "snapshot", found_str); 1807830f909SJeff Cody } else { 181*19ae9ae0SFlorian Florensa image_name = qemu_rbd_next_tok(p, ':', &p); 1827830f909SJeff Cody } 183*19ae9ae0SFlorian Florensa /* Check for namespace in the image_name */ 184*19ae9ae0SFlorian Florensa if (strchr(image_name, '/')) { 185*19ae9ae0SFlorian Florensa found_str = qemu_rbd_next_tok(image_name, '/', &image_name); 186*19ae9ae0SFlorian Florensa qemu_rbd_unescape(found_str); 187*19ae9ae0SFlorian Florensa qdict_put_str(options, "namespace", found_str); 188*19ae9ae0SFlorian Florensa } else { 189*19ae9ae0SFlorian Florensa qdict_put_str(options, "namespace", ""); 190*19ae9ae0SFlorian Florensa } 191*19ae9ae0SFlorian Florensa qemu_rbd_unescape(image_name); 192*19ae9ae0SFlorian Florensa qdict_put_str(options, "image", image_name); 1937830f909SJeff Cody if (!p) { 194f27aaf4bSChristian Brunner goto done; 195f27aaf4bSChristian Brunner } 196f27aaf4bSChristian Brunner 197c7cacb3eSJeff Cody /* The following are essentially all key/value pairs, and we treat 198c7cacb3eSJeff Cody * 'id' and 'conf' a bit special. Key/value pairs may be in any order. */ 199c7cacb3eSJeff Cody while (p) { 200c7cacb3eSJeff Cody char *name, *value; 201730b00bbSMarkus Armbruster name = qemu_rbd_next_tok(p, '=', &p); 202c7cacb3eSJeff Cody if (!p) { 203c7cacb3eSJeff Cody error_setg(errp, "conf option %s has no value", name); 204c7cacb3eSJeff Cody break; 205c7cacb3eSJeff Cody } 206c7cacb3eSJeff Cody 207c7cacb3eSJeff Cody qemu_rbd_unescape(name); 208c7cacb3eSJeff Cody 209730b00bbSMarkus Armbruster value = qemu_rbd_next_tok(p, ':', &p); 210c7cacb3eSJeff Cody qemu_rbd_unescape(value); 211c7cacb3eSJeff Cody 212c7cacb3eSJeff Cody if (!strcmp(name, "conf")) { 21346f5ac20SEric Blake qdict_put_str(options, "conf", value); 214c7cacb3eSJeff Cody } else if (!strcmp(name, "id")) { 21546f5ac20SEric Blake qdict_put_str(options, "user", value); 216c7cacb3eSJeff Cody } else { 217e98c6961SEric Blake /* 218e98c6961SEric Blake * We pass these internally to qemu_rbd_set_keypairs(), so 219e98c6961SEric Blake * we can get away with the simpler list of [ "key1", 220e98c6961SEric Blake * "value1", "key2", "value2" ] rather than a raw dict 221e98c6961SEric Blake * { "key1": "value1", "key2": "value2" } where we can't 222e98c6961SEric Blake * guarantee order, or even a more correct but complex 223e98c6961SEric Blake * [ { "key1": "value1" }, { "key2": "value2" } ] 224e98c6961SEric Blake */ 225e98c6961SEric Blake if (!keypairs) { 226e98c6961SEric Blake keypairs = qlist_new(); 227c7cacb3eSJeff Cody } 22846f5ac20SEric Blake qlist_append_str(keypairs, name); 22946f5ac20SEric Blake qlist_append_str(keypairs, value); 230c7cacb3eSJeff Cody } 231c7cacb3eSJeff Cody } 232c7cacb3eSJeff Cody 233e98c6961SEric Blake if (keypairs) { 234e98c6961SEric Blake qdict_put(options, "=keyvalue-pairs", 235e98c6961SEric Blake qobject_to_json(QOBJECT(keypairs))); 236c7cacb3eSJeff Cody } 237c7cacb3eSJeff Cody 238f27aaf4bSChristian Brunner done: 2397267c094SAnthony Liguori g_free(buf); 240cb3e7f08SMarc-André Lureau qobject_unref(keypairs); 241c7cacb3eSJeff Cody return; 2427c7e9df0SSage Weil } 2437c7e9df0SSage Weil 24460390a21SDaniel P. Berrange 245e8e16d4bSEric Blake static void qemu_rbd_refresh_limits(BlockDriverState *bs, Error **errp) 246e8e16d4bSEric Blake { 247e8e16d4bSEric Blake /* XXX Does RBD support AIO on less than 512-byte alignment? */ 248e8e16d4bSEric Blake bs->bl.request_alignment = 512; 249e8e16d4bSEric Blake } 250e8e16d4bSEric Blake 251e8e16d4bSEric Blake 252d083f954SMarkus Armbruster static int qemu_rbd_set_auth(rados_t cluster, BlockdevOptionsRbd *opts, 25360390a21SDaniel P. Berrange Error **errp) 25460390a21SDaniel P. Berrange { 255d083f954SMarkus Armbruster char *key, *acr; 256a3699de4SMarkus Armbruster int r; 257a3699de4SMarkus Armbruster GString *accu; 258a3699de4SMarkus Armbruster RbdAuthModeList *auth; 25960390a21SDaniel P. Berrange 260d083f954SMarkus Armbruster if (opts->key_secret) { 261d083f954SMarkus Armbruster key = qcrypto_secret_lookup_as_base64(opts->key_secret, errp); 262d083f954SMarkus Armbruster if (!key) { 263d083f954SMarkus Armbruster return -EIO; 26460390a21SDaniel P. Berrange } 265d083f954SMarkus Armbruster r = rados_conf_set(cluster, "key", key); 266d083f954SMarkus Armbruster g_free(key); 267d083f954SMarkus Armbruster if (r < 0) { 268d083f954SMarkus Armbruster error_setg_errno(errp, -r, "Could not set 'key'"); 269d083f954SMarkus Armbruster return r; 270d083f954SMarkus Armbruster } 271a3699de4SMarkus Armbruster } 272a3699de4SMarkus Armbruster 273a3699de4SMarkus Armbruster if (opts->has_auth_client_required) { 274a3699de4SMarkus Armbruster accu = g_string_new(""); 275a3699de4SMarkus Armbruster for (auth = opts->auth_client_required; auth; auth = auth->next) { 276a3699de4SMarkus Armbruster if (accu->str[0]) { 277a3699de4SMarkus Armbruster g_string_append_c(accu, ';'); 278a3699de4SMarkus Armbruster } 279a3699de4SMarkus Armbruster g_string_append(accu, RbdAuthMode_str(auth->value)); 280a3699de4SMarkus Armbruster } 281a3699de4SMarkus Armbruster acr = g_string_free(accu, FALSE); 282a3699de4SMarkus Armbruster r = rados_conf_set(cluster, "auth_client_required", acr); 283a3699de4SMarkus Armbruster g_free(acr); 284a3699de4SMarkus Armbruster if (r < 0) { 285a3699de4SMarkus Armbruster error_setg_errno(errp, -r, 286a3699de4SMarkus Armbruster "Could not set 'auth_client_required'"); 287a3699de4SMarkus Armbruster return r; 288a3699de4SMarkus Armbruster } 289a3699de4SMarkus Armbruster } 29060390a21SDaniel P. Berrange 29160390a21SDaniel P. Berrange return 0; 29260390a21SDaniel P. Berrange } 29360390a21SDaniel P. Berrange 294e98c6961SEric Blake static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json, 295e34d8f29SJosh Durgin Error **errp) 296fab5cf59SJosh Durgin { 297e98c6961SEric Blake QList *keypairs; 298e98c6961SEric Blake QString *name; 299e98c6961SEric Blake QString *value; 300e98c6961SEric Blake const char *key; 301e98c6961SEric Blake size_t remaining; 302fab5cf59SJosh Durgin int ret = 0; 303fab5cf59SJosh Durgin 304e98c6961SEric Blake if (!keypairs_json) { 305e98c6961SEric Blake return ret; 306fab5cf59SJosh Durgin } 3077dc847ebSMax Reitz keypairs = qobject_to(QList, 3087dc847ebSMax Reitz qobject_from_json(keypairs_json, &error_abort)); 309e98c6961SEric Blake remaining = qlist_size(keypairs) / 2; 310e98c6961SEric Blake assert(remaining); 311fab5cf59SJosh Durgin 312e98c6961SEric Blake while (remaining--) { 3137dc847ebSMax Reitz name = qobject_to(QString, qlist_pop(keypairs)); 3147dc847ebSMax Reitz value = qobject_to(QString, qlist_pop(keypairs)); 315e98c6961SEric Blake assert(name && value); 316e98c6961SEric Blake key = qstring_get_str(name); 317fab5cf59SJosh Durgin 318e98c6961SEric Blake ret = rados_conf_set(cluster, key, qstring_get_str(value)); 319cb3e7f08SMarc-André Lureau qobject_unref(value); 320fab5cf59SJosh Durgin if (ret < 0) { 321e98c6961SEric Blake error_setg_errno(errp, -ret, "invalid conf option %s", key); 322cb3e7f08SMarc-André Lureau qobject_unref(name); 323fab5cf59SJosh Durgin ret = -EINVAL; 324fab5cf59SJosh Durgin break; 325fab5cf59SJosh Durgin } 326cb3e7f08SMarc-André Lureau qobject_unref(name); 327fab5cf59SJosh Durgin } 328fab5cf59SJosh Durgin 329cb3e7f08SMarc-André Lureau qobject_unref(keypairs); 330fab5cf59SJosh Durgin return ret; 331fab5cf59SJosh Durgin } 332fab5cf59SJosh Durgin 3331d393bdeStianqing static void qemu_rbd_memset(RADOSCB *rcb, int64_t offs) 3341d393bdeStianqing { 3351d393bdeStianqing if (LIBRBD_USE_IOVEC) { 3361d393bdeStianqing RBDAIOCB *acb = rcb->acb; 3371d393bdeStianqing iov_memset(acb->qiov->iov, acb->qiov->niov, offs, 0, 3381d393bdeStianqing acb->qiov->size - offs); 3391d393bdeStianqing } else { 3401d393bdeStianqing memset(rcb->buf + offs, 0, rcb->size - offs); 3411d393bdeStianqing } 3421d393bdeStianqing } 3431d393bdeStianqing 3440f9d252dSJeff Cody static QemuOptsList runtime_opts = { 3450f9d252dSJeff Cody .name = "rbd", 3460f9d252dSJeff Cody .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), 3470f9d252dSJeff Cody .desc = { 3480f9d252dSJeff Cody { 3490f9d252dSJeff Cody .name = "pool", 3500f9d252dSJeff Cody .type = QEMU_OPT_STRING, 3510f9d252dSJeff Cody .help = "Rados pool name", 3520f9d252dSJeff Cody }, 3530f9d252dSJeff Cody { 354*19ae9ae0SFlorian Florensa .name = "namespace", 355*19ae9ae0SFlorian Florensa .type = QEMU_OPT_STRING, 356*19ae9ae0SFlorian Florensa .help = "Rados namespace name in the pool", 357*19ae9ae0SFlorian Florensa }, 358*19ae9ae0SFlorian Florensa { 3590f9d252dSJeff Cody .name = "image", 3600f9d252dSJeff Cody .type = QEMU_OPT_STRING, 3610f9d252dSJeff Cody .help = "Image name in the pool", 3620f9d252dSJeff Cody }, 3630f9d252dSJeff Cody { 364cbf036b4SMarkus Armbruster .name = "conf", 365cbf036b4SMarkus Armbruster .type = QEMU_OPT_STRING, 366cbf036b4SMarkus Armbruster .help = "Rados config file location", 367cbf036b4SMarkus Armbruster }, 368cbf036b4SMarkus Armbruster { 3690f9d252dSJeff Cody .name = "snapshot", 3700f9d252dSJeff Cody .type = QEMU_OPT_STRING, 3710f9d252dSJeff Cody .help = "Ceph snapshot name", 3720f9d252dSJeff Cody }, 3730f9d252dSJeff Cody { 3740f9d252dSJeff Cody /* maps to 'id' in rados_create() */ 3750f9d252dSJeff Cody .name = "user", 3760f9d252dSJeff Cody .type = QEMU_OPT_STRING, 3770f9d252dSJeff Cody .help = "Rados id name", 3780f9d252dSJeff Cody }, 379cbf036b4SMarkus Armbruster /* 3802836284dSMarkus Armbruster * server.* extracted manually, see qemu_rbd_mon_host() 381cbf036b4SMarkus Armbruster */ 3820f9d252dSJeff Cody { /* end of list */ } 3830f9d252dSJeff Cody }, 3840f9d252dSJeff Cody }; 3850f9d252dSJeff Cody 386d083f954SMarkus Armbruster /* FIXME Deprecate and remove keypairs or make it available in QMP. */ 3871bebea37SKevin Wolf static int qemu_rbd_do_create(BlockdevCreateOptions *options, 3881bebea37SKevin Wolf const char *keypairs, const char *password_secret, 3891bebea37SKevin Wolf Error **errp) 3901bebea37SKevin Wolf { 3911bebea37SKevin Wolf BlockdevCreateOptionsRbd *opts = &options->u.rbd; 3921bebea37SKevin Wolf rados_t cluster; 3931bebea37SKevin Wolf rados_ioctx_t io_ctx; 3941bebea37SKevin Wolf int obj_order = 0; 3951bebea37SKevin Wolf int ret; 3961bebea37SKevin Wolf 3971bebea37SKevin Wolf assert(options->driver == BLOCKDEV_DRIVER_RBD); 3981bebea37SKevin Wolf if (opts->location->has_snapshot) { 3991bebea37SKevin Wolf error_setg(errp, "Can't use snapshot name for image creation"); 4001bebea37SKevin Wolf return -EINVAL; 4011bebea37SKevin Wolf } 4021bebea37SKevin Wolf 4031bebea37SKevin Wolf if (opts->has_cluster_size) { 4041bebea37SKevin Wolf int64_t objsize = opts->cluster_size; 4051bebea37SKevin Wolf if ((objsize - 1) & objsize) { /* not a power of 2? */ 4061bebea37SKevin Wolf error_setg(errp, "obj size needs to be power of 2"); 4071bebea37SKevin Wolf return -EINVAL; 4081bebea37SKevin Wolf } 4091bebea37SKevin Wolf if (objsize < 4096) { 4101bebea37SKevin Wolf error_setg(errp, "obj size too small"); 4111bebea37SKevin Wolf return -EINVAL; 4121bebea37SKevin Wolf } 4131bebea37SKevin Wolf obj_order = ctz32(objsize); 4141bebea37SKevin Wolf } 4151bebea37SKevin Wolf 416aa045c2dSKevin Wolf ret = qemu_rbd_connect(&cluster, &io_ctx, opts->location, false, keypairs, 417aa045c2dSKevin Wolf password_secret, errp); 4181bebea37SKevin Wolf if (ret < 0) { 4191bebea37SKevin Wolf return ret; 4201bebea37SKevin Wolf } 4211bebea37SKevin Wolf 4221bebea37SKevin Wolf ret = rbd_create(io_ctx, opts->location->image, opts->size, &obj_order); 4231bebea37SKevin Wolf if (ret < 0) { 4241bebea37SKevin Wolf error_setg_errno(errp, -ret, "error rbd create"); 425aa045c2dSKevin Wolf goto out; 4261bebea37SKevin Wolf } 4271bebea37SKevin Wolf 4281bebea37SKevin Wolf ret = 0; 429aa045c2dSKevin Wolf out: 430aa045c2dSKevin Wolf rados_ioctx_destroy(io_ctx); 4311bebea37SKevin Wolf rados_shutdown(cluster); 4321bebea37SKevin Wolf return ret; 4331bebea37SKevin Wolf } 4341bebea37SKevin Wolf 4351bebea37SKevin Wolf static int qemu_rbd_co_create(BlockdevCreateOptions *options, Error **errp) 4361bebea37SKevin Wolf { 4371bebea37SKevin Wolf return qemu_rbd_do_create(options, NULL, NULL, errp); 4381bebea37SKevin Wolf } 4391bebea37SKevin Wolf 440efc75e2aSStefan Hajnoczi static int coroutine_fn qemu_rbd_co_create_opts(const char *filename, 441efc75e2aSStefan Hajnoczi QemuOpts *opts, 442efc75e2aSStefan Hajnoczi Error **errp) 443f27aaf4bSChristian Brunner { 4441bebea37SKevin Wolf BlockdevCreateOptions *create_options; 4451bebea37SKevin Wolf BlockdevCreateOptionsRbd *rbd_opts; 4461bebea37SKevin Wolf BlockdevOptionsRbd *loc; 447d61563b2SMarkus Armbruster Error *local_err = NULL; 4481bebea37SKevin Wolf const char *keypairs, *password_secret; 449c7cacb3eSJeff Cody QDict *options = NULL; 450c7cacb3eSJeff Cody int ret = 0; 451f27aaf4bSChristian Brunner 4521bebea37SKevin Wolf create_options = g_new0(BlockdevCreateOptions, 1); 4531bebea37SKevin Wolf create_options->driver = BLOCKDEV_DRIVER_RBD; 4541bebea37SKevin Wolf rbd_opts = &create_options->u.rbd; 4551bebea37SKevin Wolf 4561bebea37SKevin Wolf rbd_opts->location = g_new0(BlockdevOptionsRbd, 1); 4571bebea37SKevin Wolf 4581bebea37SKevin Wolf password_secret = qemu_opt_get(opts, "password-secret"); 45960390a21SDaniel P. Berrange 460f27aaf4bSChristian Brunner /* Read out options */ 4611bebea37SKevin Wolf rbd_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 462c2eb918eSHu Tao BDRV_SECTOR_SIZE); 4631bebea37SKevin Wolf rbd_opts->cluster_size = qemu_opt_get_size_del(opts, 4641bebea37SKevin Wolf BLOCK_OPT_CLUSTER_SIZE, 0); 4651bebea37SKevin Wolf rbd_opts->has_cluster_size = (rbd_opts->cluster_size != 0); 466f27aaf4bSChristian Brunner 467c7cacb3eSJeff Cody options = qdict_new(); 468c7cacb3eSJeff Cody qemu_rbd_parse_filename(filename, options, &local_err); 469c7cacb3eSJeff Cody if (local_err) { 470c7cacb3eSJeff Cody ret = -EINVAL; 471c7cacb3eSJeff Cody error_propagate(errp, local_err); 472c7cacb3eSJeff Cody goto exit; 473c7cacb3eSJeff Cody } 474c7cacb3eSJeff Cody 475129c7d1cSMarkus Armbruster /* 476129c7d1cSMarkus Armbruster * Caution: while qdict_get_try_str() is fine, getting non-string 477129c7d1cSMarkus Armbruster * types would require more care. When @options come from -blockdev 478129c7d1cSMarkus Armbruster * or blockdev_add, its members are typed according to the QAPI 479129c7d1cSMarkus Armbruster * schema, but when they come from -drive, they're all QString. 480129c7d1cSMarkus Armbruster */ 4811bebea37SKevin Wolf loc = rbd_opts->location; 4821bebea37SKevin Wolf loc->pool = g_strdup(qdict_get_try_str(options, "pool")); 4831bebea37SKevin Wolf loc->conf = g_strdup(qdict_get_try_str(options, "conf")); 4841bebea37SKevin Wolf loc->has_conf = !!loc->conf; 4851bebea37SKevin Wolf loc->user = g_strdup(qdict_get_try_str(options, "user")); 4861bebea37SKevin Wolf loc->has_user = !!loc->user; 487*19ae9ae0SFlorian Florensa loc->q_namespace = g_strdup(qdict_get_try_str(options, "namespace")); 4881bebea37SKevin Wolf loc->image = g_strdup(qdict_get_try_str(options, "image")); 48907846397SMarkus Armbruster keypairs = qdict_get_try_str(options, "=keyvalue-pairs"); 490c7cacb3eSJeff Cody 4911bebea37SKevin Wolf ret = qemu_rbd_do_create(create_options, keypairs, password_secret, errp); 49287cd3d20SVikhyat Umrao if (ret < 0) { 493c7cacb3eSJeff Cody goto exit; 494f27aaf4bSChristian Brunner } 495f27aaf4bSChristian Brunner 496c7cacb3eSJeff Cody exit: 497cb3e7f08SMarc-André Lureau qobject_unref(options); 4981bebea37SKevin Wolf qapi_free_BlockdevCreateOptions(create_options); 499f27aaf4bSChristian Brunner return ret; 500f27aaf4bSChristian Brunner } 501f27aaf4bSChristian Brunner 502f27aaf4bSChristian Brunner /* 503e04fb07fSStefan Hajnoczi * This aio completion is being called from rbd_finish_bh() and runs in qemu 504e04fb07fSStefan Hajnoczi * BH context. 505f27aaf4bSChristian Brunner */ 506ad32e9c0SJosh Durgin static void qemu_rbd_complete_aio(RADOSCB *rcb) 507f27aaf4bSChristian Brunner { 508f27aaf4bSChristian Brunner RBDAIOCB *acb = rcb->acb; 509f27aaf4bSChristian Brunner int64_t r; 510f27aaf4bSChristian Brunner 511f27aaf4bSChristian Brunner r = rcb->ret; 512f27aaf4bSChristian Brunner 513dc7588c1SJosh Durgin if (acb->cmd != RBD_AIO_READ) { 514f27aaf4bSChristian Brunner if (r < 0) { 515f27aaf4bSChristian Brunner acb->ret = r; 516f27aaf4bSChristian Brunner acb->error = 1; 517f27aaf4bSChristian Brunner } else if (!acb->error) { 518ad32e9c0SJosh Durgin acb->ret = rcb->size; 519f27aaf4bSChristian Brunner } 520f27aaf4bSChristian Brunner } else { 521ad32e9c0SJosh Durgin if (r < 0) { 5221d393bdeStianqing qemu_rbd_memset(rcb, 0); 523f27aaf4bSChristian Brunner acb->ret = r; 524f27aaf4bSChristian Brunner acb->error = 1; 525ad32e9c0SJosh Durgin } else if (r < rcb->size) { 5261d393bdeStianqing qemu_rbd_memset(rcb, r); 527f27aaf4bSChristian Brunner if (!acb->error) { 528ad32e9c0SJosh Durgin acb->ret = rcb->size; 529f27aaf4bSChristian Brunner } 530f27aaf4bSChristian Brunner } else if (!acb->error) { 531ad32e9c0SJosh Durgin acb->ret = r; 532f27aaf4bSChristian Brunner } 533f27aaf4bSChristian Brunner } 534e04fb07fSStefan Hajnoczi 5357267c094SAnthony Liguori g_free(rcb); 536e04fb07fSStefan Hajnoczi 5371d393bdeStianqing if (!LIBRBD_USE_IOVEC) { 538e04fb07fSStefan Hajnoczi if (acb->cmd == RBD_AIO_READ) { 539e04fb07fSStefan Hajnoczi qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); 540f27aaf4bSChristian Brunner } 541e04fb07fSStefan Hajnoczi qemu_vfree(acb->bounce); 5421d393bdeStianqing } 5431d393bdeStianqing 544e04fb07fSStefan Hajnoczi acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret)); 545f27aaf4bSChristian Brunner 5468007429aSFam Zheng qemu_aio_unref(acb); 547f27aaf4bSChristian Brunner } 548f27aaf4bSChristian Brunner 5494bfb2741SKevin Wolf static char *qemu_rbd_mon_host(BlockdevOptionsRbd *opts, Error **errp) 5500a55679bSJeff Cody { 5514bfb2741SKevin Wolf const char **vals; 5522836284dSMarkus Armbruster const char *host, *port; 5532836284dSMarkus Armbruster char *rados_str; 5544bfb2741SKevin Wolf InetSocketAddressBaseList *p; 5554bfb2741SKevin Wolf int i, cnt; 5560a55679bSJeff Cody 5574bfb2741SKevin Wolf if (!opts->has_server) { 5584bfb2741SKevin Wolf return NULL; 5590a55679bSJeff Cody } 5604bfb2741SKevin Wolf 5614bfb2741SKevin Wolf for (cnt = 0, p = opts->server; p; p = p->next) { 5624bfb2741SKevin Wolf cnt++; 5630a55679bSJeff Cody } 5640a55679bSJeff Cody 5654bfb2741SKevin Wolf vals = g_new(const char *, cnt + 1); 5664bfb2741SKevin Wolf 5674bfb2741SKevin Wolf for (i = 0, p = opts->server; p; p = p->next, i++) { 5684bfb2741SKevin Wolf host = p->value->host; 5694bfb2741SKevin Wolf port = p->value->port; 5704bfb2741SKevin Wolf 5710a55679bSJeff Cody if (strchr(host, ':')) { 5724bfb2741SKevin Wolf vals[i] = g_strdup_printf("[%s]:%s", host, port); 5730a55679bSJeff Cody } else { 5744bfb2741SKevin Wolf vals[i] = g_strdup_printf("%s:%s", host, port); 5750a55679bSJeff Cody } 5760a55679bSJeff Cody } 5772836284dSMarkus Armbruster vals[i] = NULL; 5780a55679bSJeff Cody 5792836284dSMarkus Armbruster rados_str = i ? g_strjoinv(";", (char **)vals) : NULL; 5802836284dSMarkus Armbruster g_strfreev((char **)vals); 5810a55679bSJeff Cody return rados_str; 5820a55679bSJeff Cody } 5830a55679bSJeff Cody 5843d9136f9SKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx, 5854bfb2741SKevin Wolf BlockdevOptionsRbd *opts, bool cache, 5864ff45049SKevin Wolf const char *keypairs, const char *secretid, 5874ff45049SKevin Wolf Error **errp) 588f27aaf4bSChristian Brunner { 5890a55679bSJeff Cody char *mon_host = NULL; 5903d9136f9SKevin Wolf Error *local_err = NULL; 591f27aaf4bSChristian Brunner int r; 592f27aaf4bSChristian Brunner 593d083f954SMarkus Armbruster if (secretid) { 594d083f954SMarkus Armbruster if (opts->key_secret) { 595d083f954SMarkus Armbruster error_setg(errp, 596d083f954SMarkus Armbruster "Legacy 'password-secret' clashes with 'key-secret'"); 597d083f954SMarkus Armbruster return -EINVAL; 598d083f954SMarkus Armbruster } 599d083f954SMarkus Armbruster opts->key_secret = g_strdup(secretid); 600d083f954SMarkus Armbruster opts->has_key_secret = true; 601d083f954SMarkus Armbruster } 602d083f954SMarkus Armbruster 6034bfb2741SKevin Wolf mon_host = qemu_rbd_mon_host(opts, &local_err); 60484d18f06SMarkus Armbruster if (local_err) { 605d61563b2SMarkus Armbruster error_propagate(errp, local_err); 6062836284dSMarkus Armbruster r = -EINVAL; 6072836284dSMarkus Armbruster goto failed_opts; 608a9ccedc3SKevin Wolf } 609a9ccedc3SKevin Wolf 6104bfb2741SKevin Wolf r = rados_create(cluster, opts->user); 611ad32e9c0SJosh Durgin if (r < 0) { 61287cd3d20SVikhyat Umrao error_setg_errno(errp, -r, "error initializing"); 613c3ca988dSKevin Wolf goto failed_opts; 614f27aaf4bSChristian Brunner } 615f27aaf4bSChristian Brunner 616c7cacb3eSJeff Cody /* try default location when conf=NULL, but ignore failure */ 6174bfb2741SKevin Wolf r = rados_conf_read_file(*cluster, opts->conf); 6184bfb2741SKevin Wolf if (opts->has_conf && r < 0) { 6194bfb2741SKevin Wolf error_setg_errno(errp, -r, "error reading conf file %s", opts->conf); 620e34d8f29SJosh Durgin goto failed_shutdown; 621e34d8f29SJosh Durgin } 62299a3c89dSJosh Durgin 6233d9136f9SKevin Wolf r = qemu_rbd_set_keypairs(*cluster, keypairs, errp); 62499a3c89dSJosh Durgin if (r < 0) { 62599a3c89dSJosh Durgin goto failed_shutdown; 62699a3c89dSJosh Durgin } 62799a3c89dSJosh Durgin 6280a55679bSJeff Cody if (mon_host) { 6293d9136f9SKevin Wolf r = rados_conf_set(*cluster, "mon_host", mon_host); 6300a55679bSJeff Cody if (r < 0) { 6310a55679bSJeff Cody goto failed_shutdown; 6320a55679bSJeff Cody } 6330a55679bSJeff Cody } 6340a55679bSJeff Cody 635d083f954SMarkus Armbruster r = qemu_rbd_set_auth(*cluster, opts, errp); 636d083f954SMarkus Armbruster if (r < 0) { 63760390a21SDaniel P. Berrange goto failed_shutdown; 63860390a21SDaniel P. Berrange } 63960390a21SDaniel P. Berrange 640b11f38fcSJosh Durgin /* 641b11f38fcSJosh Durgin * Fallback to more conservative semantics if setting cache 642b11f38fcSJosh Durgin * options fails. Ignore errors from setting rbd_cache because the 643b11f38fcSJosh Durgin * only possible error is that the option does not exist, and 644b11f38fcSJosh Durgin * librbd defaults to no caching. If write through caching cannot 645b11f38fcSJosh Durgin * be set up, fall back to no caching. 646b11f38fcSJosh Durgin */ 6473d9136f9SKevin Wolf if (cache) { 6483d9136f9SKevin Wolf rados_conf_set(*cluster, "rbd_cache", "true"); 649b11f38fcSJosh Durgin } else { 6503d9136f9SKevin Wolf rados_conf_set(*cluster, "rbd_cache", "false"); 651b11f38fcSJosh Durgin } 652b11f38fcSJosh Durgin 6533d9136f9SKevin Wolf r = rados_connect(*cluster); 654ad32e9c0SJosh Durgin if (r < 0) { 65587cd3d20SVikhyat Umrao error_setg_errno(errp, -r, "error connecting"); 656eb93d5d9SSage Weil goto failed_shutdown; 657ad32e9c0SJosh Durgin } 658ad32e9c0SJosh Durgin 6594bfb2741SKevin Wolf r = rados_ioctx_create(*cluster, opts->pool, io_ctx); 660ad32e9c0SJosh Durgin if (r < 0) { 6614bfb2741SKevin Wolf error_setg_errno(errp, -r, "error opening pool %s", opts->pool); 662eb93d5d9SSage Weil goto failed_shutdown; 663ad32e9c0SJosh Durgin } 664*19ae9ae0SFlorian Florensa /* 665*19ae9ae0SFlorian Florensa * Set the namespace after opening the io context on the pool, 666*19ae9ae0SFlorian Florensa * if nspace == NULL or if nspace == "", it is just as we did nothing 667*19ae9ae0SFlorian Florensa */ 668*19ae9ae0SFlorian Florensa rados_ioctx_set_namespace(*io_ctx, opts->q_namespace); 669ad32e9c0SJosh Durgin 6703d9136f9SKevin Wolf return 0; 6713d9136f9SKevin Wolf 6723d9136f9SKevin Wolf failed_shutdown: 6733d9136f9SKevin Wolf rados_shutdown(*cluster); 6743d9136f9SKevin Wolf failed_opts: 6753d9136f9SKevin Wolf g_free(mon_host); 6763d9136f9SKevin Wolf return r; 6773d9136f9SKevin Wolf } 6783d9136f9SKevin Wolf 679f24b03b5SJeff Cody static int qemu_rbd_convert_options(QDict *options, BlockdevOptionsRbd **opts, 680f24b03b5SJeff Cody Error **errp) 681f24b03b5SJeff Cody { 682f24b03b5SJeff Cody Visitor *v; 683f24b03b5SJeff Cody Error *local_err = NULL; 684f24b03b5SJeff Cody 685f24b03b5SJeff Cody /* Convert the remaining options into a QAPI object */ 686f24b03b5SJeff Cody v = qobject_input_visitor_new_flat_confused(options, errp); 687f24b03b5SJeff Cody if (!v) { 688f24b03b5SJeff Cody return -EINVAL; 689f24b03b5SJeff Cody } 690f24b03b5SJeff Cody 691f24b03b5SJeff Cody visit_type_BlockdevOptionsRbd(v, NULL, opts, &local_err); 692f24b03b5SJeff Cody visit_free(v); 693f24b03b5SJeff Cody 694f24b03b5SJeff Cody if (local_err) { 695f24b03b5SJeff Cody error_propagate(errp, local_err); 696f24b03b5SJeff Cody return -EINVAL; 697f24b03b5SJeff Cody } 698f24b03b5SJeff Cody 699f24b03b5SJeff Cody return 0; 700f24b03b5SJeff Cody } 701f24b03b5SJeff Cody 702084d1d13SJeff Cody static int qemu_rbd_attempt_legacy_options(QDict *options, 703084d1d13SJeff Cody BlockdevOptionsRbd **opts, 704084d1d13SJeff Cody char **keypairs) 705084d1d13SJeff Cody { 706084d1d13SJeff Cody char *filename; 707084d1d13SJeff Cody int r; 708084d1d13SJeff Cody 709084d1d13SJeff Cody filename = g_strdup(qdict_get_try_str(options, "filename")); 710084d1d13SJeff Cody if (!filename) { 711084d1d13SJeff Cody return -EINVAL; 712084d1d13SJeff Cody } 713084d1d13SJeff Cody qdict_del(options, "filename"); 714084d1d13SJeff Cody 715084d1d13SJeff Cody qemu_rbd_parse_filename(filename, options, NULL); 716084d1d13SJeff Cody 717084d1d13SJeff Cody /* keypairs freed by caller */ 718084d1d13SJeff Cody *keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs")); 719084d1d13SJeff Cody if (*keypairs) { 720084d1d13SJeff Cody qdict_del(options, "=keyvalue-pairs"); 721084d1d13SJeff Cody } 722084d1d13SJeff Cody 723084d1d13SJeff Cody r = qemu_rbd_convert_options(options, opts, NULL); 724084d1d13SJeff Cody 725084d1d13SJeff Cody g_free(filename); 726084d1d13SJeff Cody return r; 727084d1d13SJeff Cody } 728084d1d13SJeff Cody 7293d9136f9SKevin Wolf static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags, 7303d9136f9SKevin Wolf Error **errp) 7313d9136f9SKevin Wolf { 7323d9136f9SKevin Wolf BDRVRBDState *s = bs->opaque; 7334bfb2741SKevin Wolf BlockdevOptionsRbd *opts = NULL; 734bfb15b4bSJeff Cody const QDictEntry *e; 7353d9136f9SKevin Wolf Error *local_err = NULL; 7364ff45049SKevin Wolf char *keypairs, *secretid; 7373d9136f9SKevin Wolf int r; 7383d9136f9SKevin Wolf 7394ff45049SKevin Wolf keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs")); 7404ff45049SKevin Wolf if (keypairs) { 7414ff45049SKevin Wolf qdict_del(options, "=keyvalue-pairs"); 7424ff45049SKevin Wolf } 7434ff45049SKevin Wolf 7444ff45049SKevin Wolf secretid = g_strdup(qdict_get_try_str(options, "password-secret")); 7454ff45049SKevin Wolf if (secretid) { 7464ff45049SKevin Wolf qdict_del(options, "password-secret"); 7474ff45049SKevin Wolf } 7484ff45049SKevin Wolf 749f24b03b5SJeff Cody r = qemu_rbd_convert_options(options, &opts, &local_err); 7504bfb2741SKevin Wolf if (local_err) { 751084d1d13SJeff Cody /* If keypairs are present, that means some options are present in 752084d1d13SJeff Cody * the modern option format. Don't attempt to parse legacy option 753084d1d13SJeff Cody * formats, as we won't support mixed usage. */ 754084d1d13SJeff Cody if (keypairs) { 7554bfb2741SKevin Wolf error_propagate(errp, local_err); 7564bfb2741SKevin Wolf goto out; 7574bfb2741SKevin Wolf } 7584bfb2741SKevin Wolf 759084d1d13SJeff Cody /* If the initial attempt to convert and process the options failed, 760084d1d13SJeff Cody * we may be attempting to open an image file that has the rbd options 761084d1d13SJeff Cody * specified in the older format consisting of all key/value pairs 762084d1d13SJeff Cody * encoded in the filename. Go ahead and attempt to parse the 763084d1d13SJeff Cody * filename, and see if we can pull out the required options. */ 764084d1d13SJeff Cody r = qemu_rbd_attempt_legacy_options(options, &opts, &keypairs); 765084d1d13SJeff Cody if (r < 0) { 766084d1d13SJeff Cody /* Propagate the original error, not the legacy parsing fallback 767084d1d13SJeff Cody * error, as the latter was just a best-effort attempt. */ 768084d1d13SJeff Cody error_propagate(errp, local_err); 769084d1d13SJeff Cody goto out; 770084d1d13SJeff Cody } 771084d1d13SJeff Cody /* Take care whenever deciding to actually deprecate; once this ability 772084d1d13SJeff Cody * is removed, we will not be able to open any images with legacy-styled 773084d1d13SJeff Cody * backing image strings. */ 7745197f445SMarkus Armbruster warn_report("RBD options encoded in the filename as keyvalue pairs " 775084d1d13SJeff Cody "is deprecated"); 776084d1d13SJeff Cody } 777084d1d13SJeff Cody 778bfb15b4bSJeff Cody /* Remove the processed options from the QDict (the visitor processes 779bfb15b4bSJeff Cody * _all_ options in the QDict) */ 780bfb15b4bSJeff Cody while ((e = qdict_first(options))) { 781bfb15b4bSJeff Cody qdict_del(options, e->key); 782bfb15b4bSJeff Cody } 783bfb15b4bSJeff Cody 784d41a5588SKevin Wolf r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts, 785d41a5588SKevin Wolf !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp); 7863d9136f9SKevin Wolf if (r < 0) { 7874ff45049SKevin Wolf goto out; 7883d9136f9SKevin Wolf } 7893d9136f9SKevin Wolf 790d41a5588SKevin Wolf s->snap = g_strdup(opts->snapshot); 791d41a5588SKevin Wolf s->image_name = g_strdup(opts->image); 792d41a5588SKevin Wolf 793e2b8247aSJeff Cody /* rbd_open is always r/w */ 79480b61a27SJeff Cody r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap); 795ad32e9c0SJosh Durgin if (r < 0) { 79680b61a27SJeff Cody error_setg_errno(errp, -r, "error reading header from %s", 79780b61a27SJeff Cody s->image_name); 798eb93d5d9SSage Weil goto failed_open; 799ad32e9c0SJosh Durgin } 800ad32e9c0SJosh Durgin 801d24f8023SStefano Garzarella r = rbd_get_size(s->image, &s->image_size); 802d24f8023SStefano Garzarella if (r < 0) { 803d24f8023SStefano Garzarella error_setg_errno(errp, -r, "error getting image size from %s", 804d24f8023SStefano Garzarella s->image_name); 805d24f8023SStefano Garzarella rbd_close(s->image); 806d24f8023SStefano Garzarella goto failed_open; 807d24f8023SStefano Garzarella } 808d24f8023SStefano Garzarella 809e2b8247aSJeff Cody /* If we are using an rbd snapshot, we must be r/o, otherwise 810e2b8247aSJeff Cody * leave as-is */ 811e2b8247aSJeff Cody if (s->snap != NULL) { 812eaa2410fSKevin Wolf r = bdrv_apply_auto_read_only(bs, "rbd snapshots are read-only", errp); 813e2b8247aSJeff Cody if (r < 0) { 814a51b9c48SKevin Wolf rbd_close(s->image); 815e2b8247aSJeff Cody goto failed_open; 816e2b8247aSJeff Cody } 817e2b8247aSJeff Cody } 818f27aaf4bSChristian Brunner 8194ff45049SKevin Wolf r = 0; 8204ff45049SKevin Wolf goto out; 821f27aaf4bSChristian Brunner 822eb93d5d9SSage Weil failed_open: 823ad32e9c0SJosh Durgin rados_ioctx_destroy(s->io_ctx); 824eb93d5d9SSage Weil g_free(s->snap); 82580b61a27SJeff Cody g_free(s->image_name); 8263d9136f9SKevin Wolf rados_shutdown(s->cluster); 8274ff45049SKevin Wolf out: 8284bfb2741SKevin Wolf qapi_free_BlockdevOptionsRbd(opts); 8294ff45049SKevin Wolf g_free(keypairs); 8304ff45049SKevin Wolf g_free(secretid); 831f27aaf4bSChristian Brunner return r; 832f27aaf4bSChristian Brunner } 833f27aaf4bSChristian Brunner 83456e7cf8dSJeff Cody 83556e7cf8dSJeff Cody /* Since RBD is currently always opened R/W via the API, 83656e7cf8dSJeff Cody * we just need to check if we are using a snapshot or not, in 83756e7cf8dSJeff Cody * order to determine if we will allow it to be R/W */ 83856e7cf8dSJeff Cody static int qemu_rbd_reopen_prepare(BDRVReopenState *state, 83956e7cf8dSJeff Cody BlockReopenQueue *queue, Error **errp) 84056e7cf8dSJeff Cody { 84156e7cf8dSJeff Cody BDRVRBDState *s = state->bs->opaque; 84256e7cf8dSJeff Cody int ret = 0; 84356e7cf8dSJeff Cody 84456e7cf8dSJeff Cody if (s->snap && state->flags & BDRV_O_RDWR) { 84556e7cf8dSJeff Cody error_setg(errp, 84656e7cf8dSJeff Cody "Cannot change node '%s' to r/w when using RBD snapshot", 84756e7cf8dSJeff Cody bdrv_get_device_or_node_name(state->bs)); 84856e7cf8dSJeff Cody ret = -EINVAL; 84956e7cf8dSJeff Cody } 85056e7cf8dSJeff Cody 85156e7cf8dSJeff Cody return ret; 85256e7cf8dSJeff Cody } 85356e7cf8dSJeff Cody 854ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs) 855f27aaf4bSChristian Brunner { 856f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 857f27aaf4bSChristian Brunner 858ad32e9c0SJosh Durgin rbd_close(s->image); 859ad32e9c0SJosh Durgin rados_ioctx_destroy(s->io_ctx); 8607267c094SAnthony Liguori g_free(s->snap); 86180b61a27SJeff Cody g_free(s->image_name); 862ad32e9c0SJosh Durgin rados_shutdown(s->cluster); 863f27aaf4bSChristian Brunner } 864f27aaf4bSChristian Brunner 865d24f8023SStefano Garzarella /* Resize the RBD image and update the 'image_size' with the current size */ 866d24f8023SStefano Garzarella static int qemu_rbd_resize(BlockDriverState *bs, uint64_t size) 867d24f8023SStefano Garzarella { 868d24f8023SStefano Garzarella BDRVRBDState *s = bs->opaque; 869d24f8023SStefano Garzarella int r; 870d24f8023SStefano Garzarella 871d24f8023SStefano Garzarella r = rbd_resize(s->image, size); 872d24f8023SStefano Garzarella if (r < 0) { 873d24f8023SStefano Garzarella return r; 874d24f8023SStefano Garzarella } 875d24f8023SStefano Garzarella 876d24f8023SStefano Garzarella s->image_size = size; 877d24f8023SStefano Garzarella 878d24f8023SStefano Garzarella return 0; 879d24f8023SStefano Garzarella } 880d24f8023SStefano Garzarella 881d7331bedSStefan Hajnoczi static const AIOCBInfo rbd_aiocb_info = { 882f27aaf4bSChristian Brunner .aiocb_size = sizeof(RBDAIOCB), 883f27aaf4bSChristian Brunner }; 884f27aaf4bSChristian Brunner 885e04fb07fSStefan Hajnoczi static void rbd_finish_bh(void *opaque) 886f27aaf4bSChristian Brunner { 887e04fb07fSStefan Hajnoczi RADOSCB *rcb = opaque; 888e04fb07fSStefan Hajnoczi qemu_rbd_complete_aio(rcb); 889ad32e9c0SJosh Durgin } 890ad32e9c0SJosh Durgin 891ad32e9c0SJosh Durgin /* 892ad32e9c0SJosh Durgin * This is the callback function for rbd_aio_read and _write 893ad32e9c0SJosh Durgin * 894ad32e9c0SJosh Durgin * Note: this function is being called from a non qemu thread so 895ad32e9c0SJosh Durgin * we need to be careful about what we do here. Generally we only 896e04fb07fSStefan Hajnoczi * schedule a BH, and do the rest of the io completion handling 897e04fb07fSStefan Hajnoczi * from rbd_finish_bh() which runs in a qemu context. 898ad32e9c0SJosh Durgin */ 899ad32e9c0SJosh Durgin static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb) 900ad32e9c0SJosh Durgin { 901e04fb07fSStefan Hajnoczi RBDAIOCB *acb = rcb->acb; 902e04fb07fSStefan Hajnoczi 903ad32e9c0SJosh Durgin rcb->ret = rbd_aio_get_return_value(c); 904ad32e9c0SJosh Durgin rbd_aio_release(c); 905f27aaf4bSChristian Brunner 906e4ec5ad4SPavel Dovgalyuk replay_bh_schedule_oneshot_event(bdrv_get_aio_context(acb->common.bs), 907ea800191SStefan Hajnoczi rbd_finish_bh, rcb); 908473c7f02SStefan Priebe } 909f27aaf4bSChristian Brunner 910787f3133SJosh Durgin static int rbd_aio_discard_wrapper(rbd_image_t image, 911787f3133SJosh Durgin uint64_t off, 912787f3133SJosh Durgin uint64_t len, 913787f3133SJosh Durgin rbd_completion_t comp) 914787f3133SJosh Durgin { 915787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 916787f3133SJosh Durgin return rbd_aio_discard(image, off, len, comp); 917787f3133SJosh Durgin #else 918787f3133SJosh Durgin return -ENOTSUP; 919787f3133SJosh Durgin #endif 920787f3133SJosh Durgin } 921787f3133SJosh Durgin 922dc7588c1SJosh Durgin static int rbd_aio_flush_wrapper(rbd_image_t image, 923dc7588c1SJosh Durgin rbd_completion_t comp) 924dc7588c1SJosh Durgin { 925dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH 926dc7588c1SJosh Durgin return rbd_aio_flush(image, comp); 927dc7588c1SJosh Durgin #else 928dc7588c1SJosh Durgin return -ENOTSUP; 929dc7588c1SJosh Durgin #endif 930dc7588c1SJosh Durgin } 931dc7588c1SJosh Durgin 9327c84b1b8SMarkus Armbruster static BlockAIOCB *rbd_start_aio(BlockDriverState *bs, 9337bbca9e2SEric Blake int64_t off, 934f27aaf4bSChristian Brunner QEMUIOVector *qiov, 9357bbca9e2SEric Blake int64_t size, 936097310b5SMarkus Armbruster BlockCompletionFunc *cb, 937787f3133SJosh Durgin void *opaque, 938787f3133SJosh Durgin RBDAIOCmd cmd) 939f27aaf4bSChristian Brunner { 940f27aaf4bSChristian Brunner RBDAIOCB *acb; 9410f7a0237SKevin Wolf RADOSCB *rcb = NULL; 942ad32e9c0SJosh Durgin rbd_completion_t c; 94351a13528SJosh Durgin int r; 944f27aaf4bSChristian Brunner 945f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 946f27aaf4bSChristian Brunner 947d7331bedSStefan Hajnoczi acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque); 948787f3133SJosh Durgin acb->cmd = cmd; 949f27aaf4bSChristian Brunner acb->qiov = qiov; 9507bbca9e2SEric Blake assert(!qiov || qiov->size == size); 9511d393bdeStianqing 9521d393bdeStianqing rcb = g_new(RADOSCB, 1); 9531d393bdeStianqing 9541d393bdeStianqing if (!LIBRBD_USE_IOVEC) { 955dc7588c1SJosh Durgin if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) { 956787f3133SJosh Durgin acb->bounce = NULL; 957787f3133SJosh Durgin } else { 9580f7a0237SKevin Wolf acb->bounce = qemu_try_blockalign(bs, qiov->size); 9590f7a0237SKevin Wolf if (acb->bounce == NULL) { 9600f7a0237SKevin Wolf goto failed; 9610f7a0237SKevin Wolf } 962787f3133SJosh Durgin } 9631d393bdeStianqing if (cmd == RBD_AIO_WRITE) { 9641d393bdeStianqing qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); 9651d393bdeStianqing } 9661d393bdeStianqing rcb->buf = acb->bounce; 9671d393bdeStianqing } 9681d393bdeStianqing 969f27aaf4bSChristian Brunner acb->ret = 0; 970f27aaf4bSChristian Brunner acb->error = 0; 971f27aaf4bSChristian Brunner acb->s = s; 972f27aaf4bSChristian Brunner 973f27aaf4bSChristian Brunner rcb->acb = acb; 974f27aaf4bSChristian Brunner rcb->s = acb->s; 975ad32e9c0SJosh Durgin rcb->size = size; 97651a13528SJosh Durgin r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c); 97751a13528SJosh Durgin if (r < 0) { 97851a13528SJosh Durgin goto failed; 97951a13528SJosh Durgin } 980f27aaf4bSChristian Brunner 981787f3133SJosh Durgin switch (cmd) { 982d24f8023SStefano Garzarella case RBD_AIO_WRITE: { 983d24f8023SStefano Garzarella /* 984d24f8023SStefano Garzarella * RBD APIs don't allow us to write more than actual size, so in order 985d24f8023SStefano Garzarella * to support growing images, we resize the image before write 986d24f8023SStefano Garzarella * operations that exceed the current size. 987d24f8023SStefano Garzarella */ 988d24f8023SStefano Garzarella if (off + size > s->image_size) { 989d24f8023SStefano Garzarella r = qemu_rbd_resize(bs, off + size); 990d24f8023SStefano Garzarella if (r < 0) { 991d24f8023SStefano Garzarella goto failed_completion; 992d24f8023SStefano Garzarella } 993d24f8023SStefano Garzarella } 9941d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC 9951d393bdeStianqing r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c); 9961d393bdeStianqing #else 9971d393bdeStianqing r = rbd_aio_write(s->image, off, size, rcb->buf, c); 9981d393bdeStianqing #endif 999787f3133SJosh Durgin break; 1000d24f8023SStefano Garzarella } 1001787f3133SJosh Durgin case RBD_AIO_READ: 10021d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC 10031d393bdeStianqing r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c); 10041d393bdeStianqing #else 10051d393bdeStianqing r = rbd_aio_read(s->image, off, size, rcb->buf, c); 10061d393bdeStianqing #endif 1007787f3133SJosh Durgin break; 1008787f3133SJosh Durgin case RBD_AIO_DISCARD: 1009787f3133SJosh Durgin r = rbd_aio_discard_wrapper(s->image, off, size, c); 1010787f3133SJosh Durgin break; 1011dc7588c1SJosh Durgin case RBD_AIO_FLUSH: 1012dc7588c1SJosh Durgin r = rbd_aio_flush_wrapper(s->image, c); 1013dc7588c1SJosh Durgin break; 1014787f3133SJosh Durgin default: 1015787f3133SJosh Durgin r = -EINVAL; 101651a13528SJosh Durgin } 101751a13528SJosh Durgin 101851a13528SJosh Durgin if (r < 0) { 1019405a2764SKevin Wolf goto failed_completion; 1020f27aaf4bSChristian Brunner } 1021f27aaf4bSChristian Brunner return &acb->common; 102251a13528SJosh Durgin 1023405a2764SKevin Wolf failed_completion: 1024405a2764SKevin Wolf rbd_aio_release(c); 102551a13528SJosh Durgin failed: 10267267c094SAnthony Liguori g_free(rcb); 10271d393bdeStianqing if (!LIBRBD_USE_IOVEC) { 1028405a2764SKevin Wolf qemu_vfree(acb->bounce); 10291d393bdeStianqing } 10301d393bdeStianqing 10318007429aSFam Zheng qemu_aio_unref(acb); 103251a13528SJosh Durgin return NULL; 1033f27aaf4bSChristian Brunner } 1034f27aaf4bSChristian Brunner 1035e8e16d4bSEric Blake static BlockAIOCB *qemu_rbd_aio_preadv(BlockDriverState *bs, 1036e8e16d4bSEric Blake uint64_t offset, uint64_t bytes, 1037e8e16d4bSEric Blake QEMUIOVector *qiov, int flags, 1038097310b5SMarkus Armbruster BlockCompletionFunc *cb, 1039f27aaf4bSChristian Brunner void *opaque) 1040f27aaf4bSChristian Brunner { 1041e8e16d4bSEric Blake return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque, 1042787f3133SJosh Durgin RBD_AIO_READ); 1043f27aaf4bSChristian Brunner } 1044f27aaf4bSChristian Brunner 1045e8e16d4bSEric Blake static BlockAIOCB *qemu_rbd_aio_pwritev(BlockDriverState *bs, 1046e8e16d4bSEric Blake uint64_t offset, uint64_t bytes, 1047e8e16d4bSEric Blake QEMUIOVector *qiov, int flags, 1048097310b5SMarkus Armbruster BlockCompletionFunc *cb, 1049f27aaf4bSChristian Brunner void *opaque) 1050f27aaf4bSChristian Brunner { 1051e8e16d4bSEric Blake return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque, 1052787f3133SJosh Durgin RBD_AIO_WRITE); 1053f27aaf4bSChristian Brunner } 1054f27aaf4bSChristian Brunner 1055dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH 10567c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs, 1057097310b5SMarkus Armbruster BlockCompletionFunc *cb, 1058dc7588c1SJosh Durgin void *opaque) 1059dc7588c1SJosh Durgin { 1060dc7588c1SJosh Durgin return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH); 1061dc7588c1SJosh Durgin } 1062dc7588c1SJosh Durgin 1063dc7588c1SJosh Durgin #else 1064dc7588c1SJosh Durgin 10658b94ff85SPaolo Bonzini static int qemu_rbd_co_flush(BlockDriverState *bs) 10667a3f5fe9SSage Weil { 10677a3f5fe9SSage Weil #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1) 10687a3f5fe9SSage Weil /* rbd_flush added in 0.1.1 */ 10697a3f5fe9SSage Weil BDRVRBDState *s = bs->opaque; 10707a3f5fe9SSage Weil return rbd_flush(s->image); 10717a3f5fe9SSage Weil #else 10727a3f5fe9SSage Weil return 0; 10737a3f5fe9SSage Weil #endif 10747a3f5fe9SSage Weil } 1075dc7588c1SJosh Durgin #endif 10767a3f5fe9SSage Weil 1077ad32e9c0SJosh Durgin static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi) 1078f27aaf4bSChristian Brunner { 1079f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 1080ad32e9c0SJosh Durgin rbd_image_info_t info; 1081ad32e9c0SJosh Durgin int r; 1082ad32e9c0SJosh Durgin 1083ad32e9c0SJosh Durgin r = rbd_stat(s->image, &info, sizeof(info)); 1084ad32e9c0SJosh Durgin if (r < 0) { 1085ad32e9c0SJosh Durgin return r; 1086ad32e9c0SJosh Durgin } 1087ad32e9c0SJosh Durgin 1088ad32e9c0SJosh Durgin bdi->cluster_size = info.obj_size; 1089f27aaf4bSChristian Brunner return 0; 1090f27aaf4bSChristian Brunner } 1091f27aaf4bSChristian Brunner 1092ad32e9c0SJosh Durgin static int64_t qemu_rbd_getlength(BlockDriverState *bs) 1093f27aaf4bSChristian Brunner { 1094f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 1095ad32e9c0SJosh Durgin rbd_image_info_t info; 1096ad32e9c0SJosh Durgin int r; 1097f27aaf4bSChristian Brunner 1098ad32e9c0SJosh Durgin r = rbd_stat(s->image, &info, sizeof(info)); 1099ad32e9c0SJosh Durgin if (r < 0) { 1100ad32e9c0SJosh Durgin return r; 1101f27aaf4bSChristian Brunner } 1102f27aaf4bSChristian Brunner 1103ad32e9c0SJosh Durgin return info.size; 1104ad32e9c0SJosh Durgin } 1105ad32e9c0SJosh Durgin 1106061ca8a3SKevin Wolf static int coroutine_fn qemu_rbd_co_truncate(BlockDriverState *bs, 1107061ca8a3SKevin Wolf int64_t offset, 1108c80d8b06SMax Reitz bool exact, 1109061ca8a3SKevin Wolf PreallocMode prealloc, 1110061ca8a3SKevin Wolf Error **errp) 111130cdc48cSJosh Durgin { 111230cdc48cSJosh Durgin int r; 111330cdc48cSJosh Durgin 11148243ccb7SMax Reitz if (prealloc != PREALLOC_MODE_OFF) { 11158243ccb7SMax Reitz error_setg(errp, "Unsupported preallocation mode '%s'", 1116977c736fSMarkus Armbruster PreallocMode_str(prealloc)); 11178243ccb7SMax Reitz return -ENOTSUP; 11188243ccb7SMax Reitz } 11198243ccb7SMax Reitz 1120d24f8023SStefano Garzarella r = qemu_rbd_resize(bs, offset); 112130cdc48cSJosh Durgin if (r < 0) { 1122f59adb32SMax Reitz error_setg_errno(errp, -r, "Failed to resize file"); 112330cdc48cSJosh Durgin return r; 112430cdc48cSJosh Durgin } 112530cdc48cSJosh Durgin 112630cdc48cSJosh Durgin return 0; 112730cdc48cSJosh Durgin } 112830cdc48cSJosh Durgin 1129ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs, 1130ad32e9c0SJosh Durgin QEMUSnapshotInfo *sn_info) 1131f27aaf4bSChristian Brunner { 1132f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 1133f27aaf4bSChristian Brunner int r; 1134f27aaf4bSChristian Brunner 1135f27aaf4bSChristian Brunner if (sn_info->name[0] == '\0') { 1136f27aaf4bSChristian Brunner return -EINVAL; /* we need a name for rbd snapshots */ 1137f27aaf4bSChristian Brunner } 1138f27aaf4bSChristian Brunner 1139f27aaf4bSChristian Brunner /* 1140f27aaf4bSChristian Brunner * rbd snapshots are using the name as the user controlled unique identifier 1141f27aaf4bSChristian Brunner * we can't use the rbd snapid for that purpose, as it can't be set 1142f27aaf4bSChristian Brunner */ 1143f27aaf4bSChristian Brunner if (sn_info->id_str[0] != '\0' && 1144f27aaf4bSChristian Brunner strcmp(sn_info->id_str, sn_info->name) != 0) { 1145f27aaf4bSChristian Brunner return -EINVAL; 1146f27aaf4bSChristian Brunner } 1147f27aaf4bSChristian Brunner 1148f27aaf4bSChristian Brunner if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) { 1149f27aaf4bSChristian Brunner return -ERANGE; 1150f27aaf4bSChristian Brunner } 1151f27aaf4bSChristian Brunner 1152ad32e9c0SJosh Durgin r = rbd_snap_create(s->image, sn_info->name); 1153f27aaf4bSChristian Brunner if (r < 0) { 1154ad32e9c0SJosh Durgin error_report("failed to create snap: %s", strerror(-r)); 1155f27aaf4bSChristian Brunner return r; 1156f27aaf4bSChristian Brunner } 1157f27aaf4bSChristian Brunner 1158f27aaf4bSChristian Brunner return 0; 1159f27aaf4bSChristian Brunner } 1160f27aaf4bSChristian Brunner 1161bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs, 1162a89d89d3SWenchao Xia const char *snapshot_id, 1163a89d89d3SWenchao Xia const char *snapshot_name, 1164a89d89d3SWenchao Xia Error **errp) 1165bd603247SGregory Farnum { 1166bd603247SGregory Farnum BDRVRBDState *s = bs->opaque; 1167bd603247SGregory Farnum int r; 1168bd603247SGregory Farnum 1169a89d89d3SWenchao Xia if (!snapshot_name) { 1170a89d89d3SWenchao Xia error_setg(errp, "rbd need a valid snapshot name"); 1171a89d89d3SWenchao Xia return -EINVAL; 1172a89d89d3SWenchao Xia } 1173a89d89d3SWenchao Xia 1174a89d89d3SWenchao Xia /* If snapshot_id is specified, it must be equal to name, see 1175a89d89d3SWenchao Xia qemu_rbd_snap_list() */ 1176a89d89d3SWenchao Xia if (snapshot_id && strcmp(snapshot_id, snapshot_name)) { 1177a89d89d3SWenchao Xia error_setg(errp, 1178a89d89d3SWenchao Xia "rbd do not support snapshot id, it should be NULL or " 1179a89d89d3SWenchao Xia "equal to snapshot name"); 1180a89d89d3SWenchao Xia return -EINVAL; 1181a89d89d3SWenchao Xia } 1182a89d89d3SWenchao Xia 1183bd603247SGregory Farnum r = rbd_snap_remove(s->image, snapshot_name); 1184a89d89d3SWenchao Xia if (r < 0) { 1185a89d89d3SWenchao Xia error_setg_errno(errp, -r, "Failed to remove the snapshot"); 1186a89d89d3SWenchao Xia } 1187bd603247SGregory Farnum return r; 1188bd603247SGregory Farnum } 1189bd603247SGregory Farnum 1190bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs, 1191bd603247SGregory Farnum const char *snapshot_name) 1192bd603247SGregory Farnum { 1193bd603247SGregory Farnum BDRVRBDState *s = bs->opaque; 1194bd603247SGregory Farnum 11959be38598SEduardo Habkost return rbd_snap_rollback(s->image, snapshot_name); 1196bd603247SGregory Farnum } 1197bd603247SGregory Farnum 1198ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs, 1199ad32e9c0SJosh Durgin QEMUSnapshotInfo **psn_tab) 1200f27aaf4bSChristian Brunner { 1201f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 1202f27aaf4bSChristian Brunner QEMUSnapshotInfo *sn_info, *sn_tab = NULL; 1203ad32e9c0SJosh Durgin int i, snap_count; 1204ad32e9c0SJosh Durgin rbd_snap_info_t *snaps; 1205ad32e9c0SJosh Durgin int max_snaps = RBD_MAX_SNAPS; 1206f27aaf4bSChristian Brunner 1207ad32e9c0SJosh Durgin do { 120802c4f26bSMarkus Armbruster snaps = g_new(rbd_snap_info_t, max_snaps); 1209ad32e9c0SJosh Durgin snap_count = rbd_snap_list(s->image, snaps, &max_snaps); 12109e6337d0SStefan Hajnoczi if (snap_count <= 0) { 12117267c094SAnthony Liguori g_free(snaps); 1212f27aaf4bSChristian Brunner } 1213ad32e9c0SJosh Durgin } while (snap_count == -ERANGE); 1214f27aaf4bSChristian Brunner 1215ad32e9c0SJosh Durgin if (snap_count <= 0) { 1216b9c53290SJosh Durgin goto done; 1217f27aaf4bSChristian Brunner } 1218f27aaf4bSChristian Brunner 12195839e53bSMarkus Armbruster sn_tab = g_new0(QEMUSnapshotInfo, snap_count); 1220f27aaf4bSChristian Brunner 1221ad32e9c0SJosh Durgin for (i = 0; i < snap_count; i++) { 1222ad32e9c0SJosh Durgin const char *snap_name = snaps[i].name; 1223f27aaf4bSChristian Brunner 1224f27aaf4bSChristian Brunner sn_info = sn_tab + i; 1225f27aaf4bSChristian Brunner pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name); 1226f27aaf4bSChristian Brunner pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name); 1227f27aaf4bSChristian Brunner 1228ad32e9c0SJosh Durgin sn_info->vm_state_size = snaps[i].size; 1229f27aaf4bSChristian Brunner sn_info->date_sec = 0; 1230f27aaf4bSChristian Brunner sn_info->date_nsec = 0; 1231f27aaf4bSChristian Brunner sn_info->vm_clock_nsec = 0; 1232f27aaf4bSChristian Brunner } 1233ad32e9c0SJosh Durgin rbd_snap_list_end(snaps); 12349e6337d0SStefan Hajnoczi g_free(snaps); 1235ad32e9c0SJosh Durgin 1236b9c53290SJosh Durgin done: 1237f27aaf4bSChristian Brunner *psn_tab = sn_tab; 1238f27aaf4bSChristian Brunner return snap_count; 1239f27aaf4bSChristian Brunner } 1240f27aaf4bSChristian Brunner 1241787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 12424da444a0SEric Blake static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs, 12434da444a0SEric Blake int64_t offset, 1244f5a5ca79SManos Pitsidianakis int bytes, 1245097310b5SMarkus Armbruster BlockCompletionFunc *cb, 1246787f3133SJosh Durgin void *opaque) 1247787f3133SJosh Durgin { 1248f5a5ca79SManos Pitsidianakis return rbd_start_aio(bs, offset, NULL, bytes, cb, opaque, 1249787f3133SJosh Durgin RBD_AIO_DISCARD); 1250787f3133SJosh Durgin } 1251787f3133SJosh Durgin #endif 1252787f3133SJosh Durgin 1253be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE 12542b148f39SPaolo Bonzini static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs, 1255be217884SAdam Crume Error **errp) 1256be217884SAdam Crume { 1257be217884SAdam Crume BDRVRBDState *s = bs->opaque; 1258be217884SAdam Crume int r = rbd_invalidate_cache(s->image); 1259be217884SAdam Crume if (r < 0) { 1260be217884SAdam Crume error_setg_errno(errp, -r, "Failed to invalidate the cache"); 1261be217884SAdam Crume } 1262be217884SAdam Crume } 1263be217884SAdam Crume #endif 1264be217884SAdam Crume 1265bd0cf596SChunyan Liu static QemuOptsList qemu_rbd_create_opts = { 1266bd0cf596SChunyan Liu .name = "rbd-create-opts", 1267bd0cf596SChunyan Liu .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head), 1268bd0cf596SChunyan Liu .desc = { 1269f27aaf4bSChristian Brunner { 1270f27aaf4bSChristian Brunner .name = BLOCK_OPT_SIZE, 1271bd0cf596SChunyan Liu .type = QEMU_OPT_SIZE, 1272f27aaf4bSChristian Brunner .help = "Virtual disk size" 1273f27aaf4bSChristian Brunner }, 1274f27aaf4bSChristian Brunner { 1275f27aaf4bSChristian Brunner .name = BLOCK_OPT_CLUSTER_SIZE, 1276bd0cf596SChunyan Liu .type = QEMU_OPT_SIZE, 1277f27aaf4bSChristian Brunner .help = "RBD object size" 1278f27aaf4bSChristian Brunner }, 127960390a21SDaniel P. Berrange { 128060390a21SDaniel P. Berrange .name = "password-secret", 128160390a21SDaniel P. Berrange .type = QEMU_OPT_STRING, 128260390a21SDaniel P. Berrange .help = "ID of secret providing the password", 128360390a21SDaniel P. Berrange }, 1284bd0cf596SChunyan Liu { /* end of list */ } 1285bd0cf596SChunyan Liu } 1286f27aaf4bSChristian Brunner }; 1287f27aaf4bSChristian Brunner 12882654267cSMax Reitz static const char *const qemu_rbd_strong_runtime_opts[] = { 12892654267cSMax Reitz "pool", 12902654267cSMax Reitz "image", 12912654267cSMax Reitz "conf", 12922654267cSMax Reitz "snapshot", 12932654267cSMax Reitz "user", 12942654267cSMax Reitz "server.", 12952654267cSMax Reitz "password-secret", 12962654267cSMax Reitz 12972654267cSMax Reitz NULL 12982654267cSMax Reitz }; 12992654267cSMax Reitz 1300f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = { 1301f27aaf4bSChristian Brunner .format_name = "rbd", 1302f27aaf4bSChristian Brunner .instance_size = sizeof(BDRVRBDState), 1303c7cacb3eSJeff Cody .bdrv_parse_filename = qemu_rbd_parse_filename, 1304e8e16d4bSEric Blake .bdrv_refresh_limits = qemu_rbd_refresh_limits, 1305ad32e9c0SJosh Durgin .bdrv_file_open = qemu_rbd_open, 1306ad32e9c0SJosh Durgin .bdrv_close = qemu_rbd_close, 130756e7cf8dSJeff Cody .bdrv_reopen_prepare = qemu_rbd_reopen_prepare, 13081bebea37SKevin Wolf .bdrv_co_create = qemu_rbd_co_create, 1309efc75e2aSStefan Hajnoczi .bdrv_co_create_opts = qemu_rbd_co_create_opts, 13103ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1, 13111dcaf527SMax Reitz .bdrv_has_zero_init_truncate = bdrv_has_zero_init_1, 1312ad32e9c0SJosh Durgin .bdrv_get_info = qemu_rbd_getinfo, 1313bd0cf596SChunyan Liu .create_opts = &qemu_rbd_create_opts, 1314ad32e9c0SJosh Durgin .bdrv_getlength = qemu_rbd_getlength, 1315061ca8a3SKevin Wolf .bdrv_co_truncate = qemu_rbd_co_truncate, 1316f27aaf4bSChristian Brunner .protocol_name = "rbd", 1317f27aaf4bSChristian Brunner 1318e8e16d4bSEric Blake .bdrv_aio_preadv = qemu_rbd_aio_preadv, 1319e8e16d4bSEric Blake .bdrv_aio_pwritev = qemu_rbd_aio_pwritev, 1320dc7588c1SJosh Durgin 1321dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH 1322dc7588c1SJosh Durgin .bdrv_aio_flush = qemu_rbd_aio_flush, 1323dc7588c1SJosh Durgin #else 1324c68b89acSKevin Wolf .bdrv_co_flush_to_disk = qemu_rbd_co_flush, 1325dc7588c1SJosh Durgin #endif 1326f27aaf4bSChristian Brunner 1327787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 13284da444a0SEric Blake .bdrv_aio_pdiscard = qemu_rbd_aio_pdiscard, 1329787f3133SJosh Durgin #endif 1330787f3133SJosh Durgin 1331ad32e9c0SJosh Durgin .bdrv_snapshot_create = qemu_rbd_snap_create, 1332bd603247SGregory Farnum .bdrv_snapshot_delete = qemu_rbd_snap_remove, 1333ad32e9c0SJosh Durgin .bdrv_snapshot_list = qemu_rbd_snap_list, 1334bd603247SGregory Farnum .bdrv_snapshot_goto = qemu_rbd_snap_rollback, 1335be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE 13362b148f39SPaolo Bonzini .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache, 1337be217884SAdam Crume #endif 13382654267cSMax Reitz 13392654267cSMax Reitz .strong_runtime_opts = qemu_rbd_strong_runtime_opts, 1340f27aaf4bSChristian Brunner }; 1341f27aaf4bSChristian Brunner 1342f27aaf4bSChristian Brunner static void bdrv_rbd_init(void) 1343f27aaf4bSChristian Brunner { 1344f27aaf4bSChristian Brunner bdrv_register(&bdrv_rbd); 1345f27aaf4bSChristian Brunner } 1346f27aaf4bSChristian Brunner 1347f27aaf4bSChristian Brunner block_init(bdrv_rbd_init); 1348