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; 107d24f8023SStefano Garzarella uint64_t image_size; 108f27aaf4bSChristian Brunner } BDRVRBDState; 109f27aaf4bSChristian Brunner 110aa045c2dSKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx, 111aa045c2dSKevin Wolf BlockdevOptionsRbd *opts, bool cache, 112aa045c2dSKevin Wolf const char *keypairs, const char *secretid, 113aa045c2dSKevin Wolf Error **errp); 114aa045c2dSKevin Wolf 115730b00bbSMarkus Armbruster static char *qemu_rbd_next_tok(char *src, char delim, char **p) 116f27aaf4bSChristian Brunner { 117f27aaf4bSChristian Brunner char *end; 118f27aaf4bSChristian Brunner 119f27aaf4bSChristian Brunner *p = NULL; 120f27aaf4bSChristian Brunner 12116a06b24SSage Weil for (end = src; *end; ++end) { 12216a06b24SSage Weil if (*end == delim) { 12316a06b24SSage Weil break; 12416a06b24SSage Weil } 12516a06b24SSage Weil if (*end == '\\' && end[1] != '\0') { 12616a06b24SSage Weil end++; 12716a06b24SSage Weil } 12816a06b24SSage Weil } 12916a06b24SSage Weil if (*end == delim) { 130f27aaf4bSChristian Brunner *p = end + 1; 131f27aaf4bSChristian Brunner *end = '\0'; 132f27aaf4bSChristian Brunner } 1337830f909SJeff Cody return src; 134f27aaf4bSChristian Brunner } 135f27aaf4bSChristian Brunner 13616a06b24SSage Weil static void qemu_rbd_unescape(char *src) 13716a06b24SSage Weil { 13816a06b24SSage Weil char *p; 13916a06b24SSage Weil 14016a06b24SSage Weil for (p = src; *src; ++src, ++p) { 14116a06b24SSage Weil if (*src == '\\' && src[1] != '\0') { 14216a06b24SSage Weil src++; 14316a06b24SSage Weil } 14416a06b24SSage Weil *p = *src; 14516a06b24SSage Weil } 14616a06b24SSage Weil *p = '\0'; 14716a06b24SSage Weil } 14816a06b24SSage Weil 149c7cacb3eSJeff Cody static void qemu_rbd_parse_filename(const char *filename, QDict *options, 150d61563b2SMarkus Armbruster Error **errp) 151f27aaf4bSChristian Brunner { 152f27aaf4bSChristian Brunner const char *start; 153e98c6961SEric Blake char *p, *buf; 154e98c6961SEric Blake QList *keypairs = NULL; 1557830f909SJeff Cody char *found_str; 156f27aaf4bSChristian Brunner 157f27aaf4bSChristian Brunner if (!strstart(filename, "rbd:", &start)) { 158d61563b2SMarkus Armbruster error_setg(errp, "File name must start with 'rbd:'"); 159c7cacb3eSJeff Cody return; 160f27aaf4bSChristian Brunner } 161f27aaf4bSChristian Brunner 1627267c094SAnthony Liguori buf = g_strdup(start); 163f27aaf4bSChristian Brunner p = buf; 164f27aaf4bSChristian Brunner 165730b00bbSMarkus Armbruster found_str = qemu_rbd_next_tok(p, '/', &p); 1667830f909SJeff Cody if (!p) { 1677830f909SJeff Cody error_setg(errp, "Pool name is required"); 1687830f909SJeff Cody goto done; 1697830f909SJeff Cody } 1707830f909SJeff Cody qemu_rbd_unescape(found_str); 17146f5ac20SEric Blake qdict_put_str(options, "pool", found_str); 172fab5cf59SJosh Durgin 173fab5cf59SJosh Durgin if (strchr(p, '@')) { 174730b00bbSMarkus Armbruster found_str = qemu_rbd_next_tok(p, '@', &p); 1757830f909SJeff Cody qemu_rbd_unescape(found_str); 17646f5ac20SEric Blake qdict_put_str(options, "image", found_str); 1777830f909SJeff Cody 178730b00bbSMarkus Armbruster found_str = qemu_rbd_next_tok(p, ':', &p); 1797830f909SJeff Cody qemu_rbd_unescape(found_str); 18046f5ac20SEric Blake qdict_put_str(options, "snapshot", found_str); 1817830f909SJeff Cody } else { 182730b00bbSMarkus Armbruster found_str = qemu_rbd_next_tok(p, ':', &p); 1837830f909SJeff Cody qemu_rbd_unescape(found_str); 18446f5ac20SEric Blake qdict_put_str(options, "image", found_str); 1857830f909SJeff Cody } 1867830f909SJeff Cody if (!p) { 187f27aaf4bSChristian Brunner goto done; 188f27aaf4bSChristian Brunner } 189f27aaf4bSChristian Brunner 190c7cacb3eSJeff Cody /* The following are essentially all key/value pairs, and we treat 191c7cacb3eSJeff Cody * 'id' and 'conf' a bit special. Key/value pairs may be in any order. */ 192c7cacb3eSJeff Cody while (p) { 193c7cacb3eSJeff Cody char *name, *value; 194730b00bbSMarkus Armbruster name = qemu_rbd_next_tok(p, '=', &p); 195c7cacb3eSJeff Cody if (!p) { 196c7cacb3eSJeff Cody error_setg(errp, "conf option %s has no value", name); 197c7cacb3eSJeff Cody break; 198c7cacb3eSJeff Cody } 199c7cacb3eSJeff Cody 200c7cacb3eSJeff Cody qemu_rbd_unescape(name); 201c7cacb3eSJeff Cody 202730b00bbSMarkus Armbruster value = qemu_rbd_next_tok(p, ':', &p); 203c7cacb3eSJeff Cody qemu_rbd_unescape(value); 204c7cacb3eSJeff Cody 205c7cacb3eSJeff Cody if (!strcmp(name, "conf")) { 20646f5ac20SEric Blake qdict_put_str(options, "conf", value); 207c7cacb3eSJeff Cody } else if (!strcmp(name, "id")) { 20846f5ac20SEric Blake qdict_put_str(options, "user", value); 209c7cacb3eSJeff Cody } else { 210e98c6961SEric Blake /* 211e98c6961SEric Blake * We pass these internally to qemu_rbd_set_keypairs(), so 212e98c6961SEric Blake * we can get away with the simpler list of [ "key1", 213e98c6961SEric Blake * "value1", "key2", "value2" ] rather than a raw dict 214e98c6961SEric Blake * { "key1": "value1", "key2": "value2" } where we can't 215e98c6961SEric Blake * guarantee order, or even a more correct but complex 216e98c6961SEric Blake * [ { "key1": "value1" }, { "key2": "value2" } ] 217e98c6961SEric Blake */ 218e98c6961SEric Blake if (!keypairs) { 219e98c6961SEric Blake keypairs = qlist_new(); 220c7cacb3eSJeff Cody } 22146f5ac20SEric Blake qlist_append_str(keypairs, name); 22246f5ac20SEric Blake qlist_append_str(keypairs, value); 223c7cacb3eSJeff Cody } 224c7cacb3eSJeff Cody } 225c7cacb3eSJeff Cody 226e98c6961SEric Blake if (keypairs) { 227e98c6961SEric Blake qdict_put(options, "=keyvalue-pairs", 228e98c6961SEric Blake qobject_to_json(QOBJECT(keypairs))); 229c7cacb3eSJeff Cody } 230c7cacb3eSJeff Cody 231f27aaf4bSChristian Brunner done: 2327267c094SAnthony Liguori g_free(buf); 233cb3e7f08SMarc-André Lureau qobject_unref(keypairs); 234c7cacb3eSJeff Cody return; 2357c7e9df0SSage Weil } 2367c7e9df0SSage Weil 23760390a21SDaniel P. Berrange 238e8e16d4bSEric Blake static void qemu_rbd_refresh_limits(BlockDriverState *bs, Error **errp) 239e8e16d4bSEric Blake { 240e8e16d4bSEric Blake /* XXX Does RBD support AIO on less than 512-byte alignment? */ 241e8e16d4bSEric Blake bs->bl.request_alignment = 512; 242e8e16d4bSEric Blake } 243e8e16d4bSEric Blake 244e8e16d4bSEric Blake 245d083f954SMarkus Armbruster static int qemu_rbd_set_auth(rados_t cluster, BlockdevOptionsRbd *opts, 24660390a21SDaniel P. Berrange Error **errp) 24760390a21SDaniel P. Berrange { 248d083f954SMarkus Armbruster char *key, *acr; 249a3699de4SMarkus Armbruster int r; 250a3699de4SMarkus Armbruster GString *accu; 251a3699de4SMarkus Armbruster RbdAuthModeList *auth; 25260390a21SDaniel P. Berrange 253d083f954SMarkus Armbruster if (opts->key_secret) { 254d083f954SMarkus Armbruster key = qcrypto_secret_lookup_as_base64(opts->key_secret, errp); 255d083f954SMarkus Armbruster if (!key) { 256d083f954SMarkus Armbruster return -EIO; 25760390a21SDaniel P. Berrange } 258d083f954SMarkus Armbruster r = rados_conf_set(cluster, "key", key); 259d083f954SMarkus Armbruster g_free(key); 260d083f954SMarkus Armbruster if (r < 0) { 261d083f954SMarkus Armbruster error_setg_errno(errp, -r, "Could not set 'key'"); 262d083f954SMarkus Armbruster return r; 263d083f954SMarkus Armbruster } 264a3699de4SMarkus Armbruster } 265a3699de4SMarkus Armbruster 266a3699de4SMarkus Armbruster if (opts->has_auth_client_required) { 267a3699de4SMarkus Armbruster accu = g_string_new(""); 268a3699de4SMarkus Armbruster for (auth = opts->auth_client_required; auth; auth = auth->next) { 269a3699de4SMarkus Armbruster if (accu->str[0]) { 270a3699de4SMarkus Armbruster g_string_append_c(accu, ';'); 271a3699de4SMarkus Armbruster } 272a3699de4SMarkus Armbruster g_string_append(accu, RbdAuthMode_str(auth->value)); 273a3699de4SMarkus Armbruster } 274a3699de4SMarkus Armbruster acr = g_string_free(accu, FALSE); 275a3699de4SMarkus Armbruster r = rados_conf_set(cluster, "auth_client_required", acr); 276a3699de4SMarkus Armbruster g_free(acr); 277a3699de4SMarkus Armbruster if (r < 0) { 278a3699de4SMarkus Armbruster error_setg_errno(errp, -r, 279a3699de4SMarkus Armbruster "Could not set 'auth_client_required'"); 280a3699de4SMarkus Armbruster return r; 281a3699de4SMarkus Armbruster } 282a3699de4SMarkus Armbruster } 28360390a21SDaniel P. Berrange 28460390a21SDaniel P. Berrange return 0; 28560390a21SDaniel P. Berrange } 28660390a21SDaniel P. Berrange 287e98c6961SEric Blake static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json, 288e34d8f29SJosh Durgin Error **errp) 289fab5cf59SJosh Durgin { 290e98c6961SEric Blake QList *keypairs; 291e98c6961SEric Blake QString *name; 292e98c6961SEric Blake QString *value; 293e98c6961SEric Blake const char *key; 294e98c6961SEric Blake size_t remaining; 295fab5cf59SJosh Durgin int ret = 0; 296fab5cf59SJosh Durgin 297e98c6961SEric Blake if (!keypairs_json) { 298e98c6961SEric Blake return ret; 299fab5cf59SJosh Durgin } 3007dc847ebSMax Reitz keypairs = qobject_to(QList, 3017dc847ebSMax Reitz qobject_from_json(keypairs_json, &error_abort)); 302e98c6961SEric Blake remaining = qlist_size(keypairs) / 2; 303e98c6961SEric Blake assert(remaining); 304fab5cf59SJosh Durgin 305e98c6961SEric Blake while (remaining--) { 3067dc847ebSMax Reitz name = qobject_to(QString, qlist_pop(keypairs)); 3077dc847ebSMax Reitz value = qobject_to(QString, qlist_pop(keypairs)); 308e98c6961SEric Blake assert(name && value); 309e98c6961SEric Blake key = qstring_get_str(name); 310fab5cf59SJosh Durgin 311e98c6961SEric Blake ret = rados_conf_set(cluster, key, qstring_get_str(value)); 312cb3e7f08SMarc-André Lureau qobject_unref(value); 313fab5cf59SJosh Durgin if (ret < 0) { 314e98c6961SEric Blake error_setg_errno(errp, -ret, "invalid conf option %s", key); 315cb3e7f08SMarc-André Lureau qobject_unref(name); 316fab5cf59SJosh Durgin ret = -EINVAL; 317fab5cf59SJosh Durgin break; 318fab5cf59SJosh Durgin } 319cb3e7f08SMarc-André Lureau qobject_unref(name); 320fab5cf59SJosh Durgin } 321fab5cf59SJosh Durgin 322cb3e7f08SMarc-André Lureau qobject_unref(keypairs); 323fab5cf59SJosh Durgin return ret; 324fab5cf59SJosh Durgin } 325fab5cf59SJosh Durgin 3261d393bdeStianqing static void qemu_rbd_memset(RADOSCB *rcb, int64_t offs) 3271d393bdeStianqing { 3281d393bdeStianqing if (LIBRBD_USE_IOVEC) { 3291d393bdeStianqing RBDAIOCB *acb = rcb->acb; 3301d393bdeStianqing iov_memset(acb->qiov->iov, acb->qiov->niov, offs, 0, 3311d393bdeStianqing acb->qiov->size - offs); 3321d393bdeStianqing } else { 3331d393bdeStianqing memset(rcb->buf + offs, 0, rcb->size - offs); 3341d393bdeStianqing } 3351d393bdeStianqing } 3361d393bdeStianqing 3370f9d252dSJeff Cody static QemuOptsList runtime_opts = { 3380f9d252dSJeff Cody .name = "rbd", 3390f9d252dSJeff Cody .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), 3400f9d252dSJeff Cody .desc = { 3410f9d252dSJeff Cody { 3420f9d252dSJeff Cody .name = "pool", 3430f9d252dSJeff Cody .type = QEMU_OPT_STRING, 3440f9d252dSJeff Cody .help = "Rados pool name", 3450f9d252dSJeff Cody }, 3460f9d252dSJeff Cody { 3470f9d252dSJeff Cody .name = "image", 3480f9d252dSJeff Cody .type = QEMU_OPT_STRING, 3490f9d252dSJeff Cody .help = "Image name in the pool", 3500f9d252dSJeff Cody }, 3510f9d252dSJeff Cody { 352cbf036b4SMarkus Armbruster .name = "conf", 353cbf036b4SMarkus Armbruster .type = QEMU_OPT_STRING, 354cbf036b4SMarkus Armbruster .help = "Rados config file location", 355cbf036b4SMarkus Armbruster }, 356cbf036b4SMarkus Armbruster { 3570f9d252dSJeff Cody .name = "snapshot", 3580f9d252dSJeff Cody .type = QEMU_OPT_STRING, 3590f9d252dSJeff Cody .help = "Ceph snapshot name", 3600f9d252dSJeff Cody }, 3610f9d252dSJeff Cody { 3620f9d252dSJeff Cody /* maps to 'id' in rados_create() */ 3630f9d252dSJeff Cody .name = "user", 3640f9d252dSJeff Cody .type = QEMU_OPT_STRING, 3650f9d252dSJeff Cody .help = "Rados id name", 3660f9d252dSJeff Cody }, 367cbf036b4SMarkus Armbruster /* 3682836284dSMarkus Armbruster * server.* extracted manually, see qemu_rbd_mon_host() 369cbf036b4SMarkus Armbruster */ 3700f9d252dSJeff Cody { /* end of list */ } 3710f9d252dSJeff Cody }, 3720f9d252dSJeff Cody }; 3730f9d252dSJeff Cody 374d083f954SMarkus Armbruster /* FIXME Deprecate and remove keypairs or make it available in QMP. */ 3751bebea37SKevin Wolf static int qemu_rbd_do_create(BlockdevCreateOptions *options, 3761bebea37SKevin Wolf const char *keypairs, const char *password_secret, 3771bebea37SKevin Wolf Error **errp) 3781bebea37SKevin Wolf { 3791bebea37SKevin Wolf BlockdevCreateOptionsRbd *opts = &options->u.rbd; 3801bebea37SKevin Wolf rados_t cluster; 3811bebea37SKevin Wolf rados_ioctx_t io_ctx; 3821bebea37SKevin Wolf int obj_order = 0; 3831bebea37SKevin Wolf int ret; 3841bebea37SKevin Wolf 3851bebea37SKevin Wolf assert(options->driver == BLOCKDEV_DRIVER_RBD); 3861bebea37SKevin Wolf if (opts->location->has_snapshot) { 3871bebea37SKevin Wolf error_setg(errp, "Can't use snapshot name for image creation"); 3881bebea37SKevin Wolf return -EINVAL; 3891bebea37SKevin Wolf } 3901bebea37SKevin Wolf 3911bebea37SKevin Wolf if (opts->has_cluster_size) { 3921bebea37SKevin Wolf int64_t objsize = opts->cluster_size; 3931bebea37SKevin Wolf if ((objsize - 1) & objsize) { /* not a power of 2? */ 3941bebea37SKevin Wolf error_setg(errp, "obj size needs to be power of 2"); 3951bebea37SKevin Wolf return -EINVAL; 3961bebea37SKevin Wolf } 3971bebea37SKevin Wolf if (objsize < 4096) { 3981bebea37SKevin Wolf error_setg(errp, "obj size too small"); 3991bebea37SKevin Wolf return -EINVAL; 4001bebea37SKevin Wolf } 4011bebea37SKevin Wolf obj_order = ctz32(objsize); 4021bebea37SKevin Wolf } 4031bebea37SKevin Wolf 404aa045c2dSKevin Wolf ret = qemu_rbd_connect(&cluster, &io_ctx, opts->location, false, keypairs, 405aa045c2dSKevin Wolf password_secret, errp); 4061bebea37SKevin Wolf if (ret < 0) { 4071bebea37SKevin Wolf return ret; 4081bebea37SKevin Wolf } 4091bebea37SKevin Wolf 4101bebea37SKevin Wolf ret = rbd_create(io_ctx, opts->location->image, opts->size, &obj_order); 4111bebea37SKevin Wolf if (ret < 0) { 4121bebea37SKevin Wolf error_setg_errno(errp, -ret, "error rbd create"); 413aa045c2dSKevin Wolf goto out; 4141bebea37SKevin Wolf } 4151bebea37SKevin Wolf 4161bebea37SKevin Wolf ret = 0; 417aa045c2dSKevin Wolf out: 418aa045c2dSKevin Wolf rados_ioctx_destroy(io_ctx); 4191bebea37SKevin Wolf rados_shutdown(cluster); 4201bebea37SKevin Wolf return ret; 4211bebea37SKevin Wolf } 4221bebea37SKevin Wolf 4231bebea37SKevin Wolf static int qemu_rbd_co_create(BlockdevCreateOptions *options, Error **errp) 4241bebea37SKevin Wolf { 4251bebea37SKevin Wolf return qemu_rbd_do_create(options, NULL, NULL, errp); 4261bebea37SKevin Wolf } 4271bebea37SKevin Wolf 428efc75e2aSStefan Hajnoczi static int coroutine_fn qemu_rbd_co_create_opts(const char *filename, 429efc75e2aSStefan Hajnoczi QemuOpts *opts, 430efc75e2aSStefan Hajnoczi Error **errp) 431f27aaf4bSChristian Brunner { 4321bebea37SKevin Wolf BlockdevCreateOptions *create_options; 4331bebea37SKevin Wolf BlockdevCreateOptionsRbd *rbd_opts; 4341bebea37SKevin Wolf BlockdevOptionsRbd *loc; 435d61563b2SMarkus Armbruster Error *local_err = NULL; 4361bebea37SKevin Wolf const char *keypairs, *password_secret; 437c7cacb3eSJeff Cody QDict *options = NULL; 438c7cacb3eSJeff Cody int ret = 0; 439f27aaf4bSChristian Brunner 4401bebea37SKevin Wolf create_options = g_new0(BlockdevCreateOptions, 1); 4411bebea37SKevin Wolf create_options->driver = BLOCKDEV_DRIVER_RBD; 4421bebea37SKevin Wolf rbd_opts = &create_options->u.rbd; 4431bebea37SKevin Wolf 4441bebea37SKevin Wolf rbd_opts->location = g_new0(BlockdevOptionsRbd, 1); 4451bebea37SKevin Wolf 4461bebea37SKevin Wolf password_secret = qemu_opt_get(opts, "password-secret"); 44760390a21SDaniel P. Berrange 448f27aaf4bSChristian Brunner /* Read out options */ 4491bebea37SKevin Wolf rbd_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 450c2eb918eSHu Tao BDRV_SECTOR_SIZE); 4511bebea37SKevin Wolf rbd_opts->cluster_size = qemu_opt_get_size_del(opts, 4521bebea37SKevin Wolf BLOCK_OPT_CLUSTER_SIZE, 0); 4531bebea37SKevin Wolf rbd_opts->has_cluster_size = (rbd_opts->cluster_size != 0); 454f27aaf4bSChristian Brunner 455c7cacb3eSJeff Cody options = qdict_new(); 456c7cacb3eSJeff Cody qemu_rbd_parse_filename(filename, options, &local_err); 457c7cacb3eSJeff Cody if (local_err) { 458c7cacb3eSJeff Cody ret = -EINVAL; 459c7cacb3eSJeff Cody error_propagate(errp, local_err); 460c7cacb3eSJeff Cody goto exit; 461c7cacb3eSJeff Cody } 462c7cacb3eSJeff Cody 463129c7d1cSMarkus Armbruster /* 464129c7d1cSMarkus Armbruster * Caution: while qdict_get_try_str() is fine, getting non-string 465129c7d1cSMarkus Armbruster * types would require more care. When @options come from -blockdev 466129c7d1cSMarkus Armbruster * or blockdev_add, its members are typed according to the QAPI 467129c7d1cSMarkus Armbruster * schema, but when they come from -drive, they're all QString. 468129c7d1cSMarkus Armbruster */ 4691bebea37SKevin Wolf loc = rbd_opts->location; 4701bebea37SKevin Wolf loc->pool = g_strdup(qdict_get_try_str(options, "pool")); 4711bebea37SKevin Wolf loc->conf = g_strdup(qdict_get_try_str(options, "conf")); 4721bebea37SKevin Wolf loc->has_conf = !!loc->conf; 4731bebea37SKevin Wolf loc->user = g_strdup(qdict_get_try_str(options, "user")); 4741bebea37SKevin Wolf loc->has_user = !!loc->user; 4751bebea37SKevin Wolf loc->image = g_strdup(qdict_get_try_str(options, "image")); 47607846397SMarkus Armbruster keypairs = qdict_get_try_str(options, "=keyvalue-pairs"); 477c7cacb3eSJeff Cody 4781bebea37SKevin Wolf ret = qemu_rbd_do_create(create_options, keypairs, password_secret, errp); 47987cd3d20SVikhyat Umrao if (ret < 0) { 480c7cacb3eSJeff Cody goto exit; 481f27aaf4bSChristian Brunner } 482f27aaf4bSChristian Brunner 483c7cacb3eSJeff Cody exit: 484cb3e7f08SMarc-André Lureau qobject_unref(options); 4851bebea37SKevin Wolf qapi_free_BlockdevCreateOptions(create_options); 486f27aaf4bSChristian Brunner return ret; 487f27aaf4bSChristian Brunner } 488f27aaf4bSChristian Brunner 489f27aaf4bSChristian Brunner /* 490e04fb07fSStefan Hajnoczi * This aio completion is being called from rbd_finish_bh() and runs in qemu 491e04fb07fSStefan Hajnoczi * BH context. 492f27aaf4bSChristian Brunner */ 493ad32e9c0SJosh Durgin static void qemu_rbd_complete_aio(RADOSCB *rcb) 494f27aaf4bSChristian Brunner { 495f27aaf4bSChristian Brunner RBDAIOCB *acb = rcb->acb; 496f27aaf4bSChristian Brunner int64_t r; 497f27aaf4bSChristian Brunner 498f27aaf4bSChristian Brunner r = rcb->ret; 499f27aaf4bSChristian Brunner 500dc7588c1SJosh Durgin if (acb->cmd != RBD_AIO_READ) { 501f27aaf4bSChristian Brunner if (r < 0) { 502f27aaf4bSChristian Brunner acb->ret = r; 503f27aaf4bSChristian Brunner acb->error = 1; 504f27aaf4bSChristian Brunner } else if (!acb->error) { 505ad32e9c0SJosh Durgin acb->ret = rcb->size; 506f27aaf4bSChristian Brunner } 507f27aaf4bSChristian Brunner } else { 508ad32e9c0SJosh Durgin if (r < 0) { 5091d393bdeStianqing qemu_rbd_memset(rcb, 0); 510f27aaf4bSChristian Brunner acb->ret = r; 511f27aaf4bSChristian Brunner acb->error = 1; 512ad32e9c0SJosh Durgin } else if (r < rcb->size) { 5131d393bdeStianqing qemu_rbd_memset(rcb, r); 514f27aaf4bSChristian Brunner if (!acb->error) { 515ad32e9c0SJosh Durgin acb->ret = rcb->size; 516f27aaf4bSChristian Brunner } 517f27aaf4bSChristian Brunner } else if (!acb->error) { 518ad32e9c0SJosh Durgin acb->ret = r; 519f27aaf4bSChristian Brunner } 520f27aaf4bSChristian Brunner } 521e04fb07fSStefan Hajnoczi 5227267c094SAnthony Liguori g_free(rcb); 523e04fb07fSStefan Hajnoczi 5241d393bdeStianqing if (!LIBRBD_USE_IOVEC) { 525e04fb07fSStefan Hajnoczi if (acb->cmd == RBD_AIO_READ) { 526e04fb07fSStefan Hajnoczi qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); 527f27aaf4bSChristian Brunner } 528e04fb07fSStefan Hajnoczi qemu_vfree(acb->bounce); 5291d393bdeStianqing } 5301d393bdeStianqing 531e04fb07fSStefan Hajnoczi acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret)); 532f27aaf4bSChristian Brunner 5338007429aSFam Zheng qemu_aio_unref(acb); 534f27aaf4bSChristian Brunner } 535f27aaf4bSChristian Brunner 5364bfb2741SKevin Wolf static char *qemu_rbd_mon_host(BlockdevOptionsRbd *opts, Error **errp) 5370a55679bSJeff Cody { 5384bfb2741SKevin Wolf const char **vals; 5392836284dSMarkus Armbruster const char *host, *port; 5402836284dSMarkus Armbruster char *rados_str; 5414bfb2741SKevin Wolf InetSocketAddressBaseList *p; 5424bfb2741SKevin Wolf int i, cnt; 5430a55679bSJeff Cody 5444bfb2741SKevin Wolf if (!opts->has_server) { 5454bfb2741SKevin Wolf return NULL; 5460a55679bSJeff Cody } 5474bfb2741SKevin Wolf 5484bfb2741SKevin Wolf for (cnt = 0, p = opts->server; p; p = p->next) { 5494bfb2741SKevin Wolf cnt++; 5500a55679bSJeff Cody } 5510a55679bSJeff Cody 5524bfb2741SKevin Wolf vals = g_new(const char *, cnt + 1); 5534bfb2741SKevin Wolf 5544bfb2741SKevin Wolf for (i = 0, p = opts->server; p; p = p->next, i++) { 5554bfb2741SKevin Wolf host = p->value->host; 5564bfb2741SKevin Wolf port = p->value->port; 5574bfb2741SKevin Wolf 5580a55679bSJeff Cody if (strchr(host, ':')) { 5594bfb2741SKevin Wolf vals[i] = g_strdup_printf("[%s]:%s", host, port); 5600a55679bSJeff Cody } else { 5614bfb2741SKevin Wolf vals[i] = g_strdup_printf("%s:%s", host, port); 5620a55679bSJeff Cody } 5630a55679bSJeff Cody } 5642836284dSMarkus Armbruster vals[i] = NULL; 5650a55679bSJeff Cody 5662836284dSMarkus Armbruster rados_str = i ? g_strjoinv(";", (char **)vals) : NULL; 5672836284dSMarkus Armbruster g_strfreev((char **)vals); 5680a55679bSJeff Cody return rados_str; 5690a55679bSJeff Cody } 5700a55679bSJeff Cody 5713d9136f9SKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx, 5724bfb2741SKevin Wolf BlockdevOptionsRbd *opts, bool cache, 5734ff45049SKevin Wolf const char *keypairs, const char *secretid, 5744ff45049SKevin Wolf Error **errp) 575f27aaf4bSChristian Brunner { 5760a55679bSJeff Cody char *mon_host = NULL; 5773d9136f9SKevin Wolf Error *local_err = NULL; 578f27aaf4bSChristian Brunner int r; 579f27aaf4bSChristian Brunner 580d083f954SMarkus Armbruster if (secretid) { 581d083f954SMarkus Armbruster if (opts->key_secret) { 582d083f954SMarkus Armbruster error_setg(errp, 583d083f954SMarkus Armbruster "Legacy 'password-secret' clashes with 'key-secret'"); 584d083f954SMarkus Armbruster return -EINVAL; 585d083f954SMarkus Armbruster } 586d083f954SMarkus Armbruster opts->key_secret = g_strdup(secretid); 587d083f954SMarkus Armbruster opts->has_key_secret = true; 588d083f954SMarkus Armbruster } 589d083f954SMarkus Armbruster 5904bfb2741SKevin Wolf mon_host = qemu_rbd_mon_host(opts, &local_err); 59184d18f06SMarkus Armbruster if (local_err) { 592d61563b2SMarkus Armbruster error_propagate(errp, local_err); 5932836284dSMarkus Armbruster r = -EINVAL; 5942836284dSMarkus Armbruster goto failed_opts; 595a9ccedc3SKevin Wolf } 596a9ccedc3SKevin Wolf 5974bfb2741SKevin Wolf r = rados_create(cluster, opts->user); 598ad32e9c0SJosh Durgin if (r < 0) { 59987cd3d20SVikhyat Umrao error_setg_errno(errp, -r, "error initializing"); 600c3ca988dSKevin Wolf goto failed_opts; 601f27aaf4bSChristian Brunner } 602f27aaf4bSChristian Brunner 603c7cacb3eSJeff Cody /* try default location when conf=NULL, but ignore failure */ 6044bfb2741SKevin Wolf r = rados_conf_read_file(*cluster, opts->conf); 6054bfb2741SKevin Wolf if (opts->has_conf && r < 0) { 6064bfb2741SKevin Wolf error_setg_errno(errp, -r, "error reading conf file %s", opts->conf); 607e34d8f29SJosh Durgin goto failed_shutdown; 608e34d8f29SJosh Durgin } 60999a3c89dSJosh Durgin 6103d9136f9SKevin Wolf r = qemu_rbd_set_keypairs(*cluster, keypairs, errp); 61199a3c89dSJosh Durgin if (r < 0) { 61299a3c89dSJosh Durgin goto failed_shutdown; 61399a3c89dSJosh Durgin } 61499a3c89dSJosh Durgin 6150a55679bSJeff Cody if (mon_host) { 6163d9136f9SKevin Wolf r = rados_conf_set(*cluster, "mon_host", mon_host); 6170a55679bSJeff Cody if (r < 0) { 6180a55679bSJeff Cody goto failed_shutdown; 6190a55679bSJeff Cody } 6200a55679bSJeff Cody } 6210a55679bSJeff Cody 622d083f954SMarkus Armbruster r = qemu_rbd_set_auth(*cluster, opts, errp); 623d083f954SMarkus Armbruster if (r < 0) { 62460390a21SDaniel P. Berrange goto failed_shutdown; 62560390a21SDaniel P. Berrange } 62660390a21SDaniel P. Berrange 627b11f38fcSJosh Durgin /* 628b11f38fcSJosh Durgin * Fallback to more conservative semantics if setting cache 629b11f38fcSJosh Durgin * options fails. Ignore errors from setting rbd_cache because the 630b11f38fcSJosh Durgin * only possible error is that the option does not exist, and 631b11f38fcSJosh Durgin * librbd defaults to no caching. If write through caching cannot 632b11f38fcSJosh Durgin * be set up, fall back to no caching. 633b11f38fcSJosh Durgin */ 6343d9136f9SKevin Wolf if (cache) { 6353d9136f9SKevin Wolf rados_conf_set(*cluster, "rbd_cache", "true"); 636b11f38fcSJosh Durgin } else { 6373d9136f9SKevin Wolf rados_conf_set(*cluster, "rbd_cache", "false"); 638b11f38fcSJosh Durgin } 639b11f38fcSJosh Durgin 6403d9136f9SKevin Wolf r = rados_connect(*cluster); 641ad32e9c0SJosh Durgin if (r < 0) { 64287cd3d20SVikhyat Umrao error_setg_errno(errp, -r, "error connecting"); 643eb93d5d9SSage Weil goto failed_shutdown; 644ad32e9c0SJosh Durgin } 645ad32e9c0SJosh Durgin 6464bfb2741SKevin Wolf r = rados_ioctx_create(*cluster, opts->pool, io_ctx); 647ad32e9c0SJosh Durgin if (r < 0) { 6484bfb2741SKevin Wolf error_setg_errno(errp, -r, "error opening pool %s", opts->pool); 649eb93d5d9SSage Weil goto failed_shutdown; 650ad32e9c0SJosh Durgin } 651ad32e9c0SJosh Durgin 6523d9136f9SKevin Wolf return 0; 6533d9136f9SKevin Wolf 6543d9136f9SKevin Wolf failed_shutdown: 6553d9136f9SKevin Wolf rados_shutdown(*cluster); 6563d9136f9SKevin Wolf failed_opts: 6573d9136f9SKevin Wolf g_free(mon_host); 6583d9136f9SKevin Wolf return r; 6593d9136f9SKevin Wolf } 6603d9136f9SKevin Wolf 661f24b03b5SJeff Cody static int qemu_rbd_convert_options(QDict *options, BlockdevOptionsRbd **opts, 662f24b03b5SJeff Cody Error **errp) 663f24b03b5SJeff Cody { 664f24b03b5SJeff Cody Visitor *v; 665f24b03b5SJeff Cody Error *local_err = NULL; 666f24b03b5SJeff Cody 667f24b03b5SJeff Cody /* Convert the remaining options into a QAPI object */ 668f24b03b5SJeff Cody v = qobject_input_visitor_new_flat_confused(options, errp); 669f24b03b5SJeff Cody if (!v) { 670f24b03b5SJeff Cody return -EINVAL; 671f24b03b5SJeff Cody } 672f24b03b5SJeff Cody 673f24b03b5SJeff Cody visit_type_BlockdevOptionsRbd(v, NULL, opts, &local_err); 674f24b03b5SJeff Cody visit_free(v); 675f24b03b5SJeff Cody 676f24b03b5SJeff Cody if (local_err) { 677f24b03b5SJeff Cody error_propagate(errp, local_err); 678f24b03b5SJeff Cody return -EINVAL; 679f24b03b5SJeff Cody } 680f24b03b5SJeff Cody 681f24b03b5SJeff Cody return 0; 682f24b03b5SJeff Cody } 683f24b03b5SJeff Cody 684084d1d13SJeff Cody static int qemu_rbd_attempt_legacy_options(QDict *options, 685084d1d13SJeff Cody BlockdevOptionsRbd **opts, 686084d1d13SJeff Cody char **keypairs) 687084d1d13SJeff Cody { 688084d1d13SJeff Cody char *filename; 689084d1d13SJeff Cody int r; 690084d1d13SJeff Cody 691084d1d13SJeff Cody filename = g_strdup(qdict_get_try_str(options, "filename")); 692084d1d13SJeff Cody if (!filename) { 693084d1d13SJeff Cody return -EINVAL; 694084d1d13SJeff Cody } 695084d1d13SJeff Cody qdict_del(options, "filename"); 696084d1d13SJeff Cody 697084d1d13SJeff Cody qemu_rbd_parse_filename(filename, options, NULL); 698084d1d13SJeff Cody 699084d1d13SJeff Cody /* keypairs freed by caller */ 700084d1d13SJeff Cody *keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs")); 701084d1d13SJeff Cody if (*keypairs) { 702084d1d13SJeff Cody qdict_del(options, "=keyvalue-pairs"); 703084d1d13SJeff Cody } 704084d1d13SJeff Cody 705084d1d13SJeff Cody r = qemu_rbd_convert_options(options, opts, NULL); 706084d1d13SJeff Cody 707084d1d13SJeff Cody g_free(filename); 708084d1d13SJeff Cody return r; 709084d1d13SJeff Cody } 710084d1d13SJeff Cody 7113d9136f9SKevin Wolf static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags, 7123d9136f9SKevin Wolf Error **errp) 7133d9136f9SKevin Wolf { 7143d9136f9SKevin Wolf BDRVRBDState *s = bs->opaque; 7154bfb2741SKevin Wolf BlockdevOptionsRbd *opts = NULL; 716bfb15b4bSJeff Cody const QDictEntry *e; 7173d9136f9SKevin Wolf Error *local_err = NULL; 7184ff45049SKevin Wolf char *keypairs, *secretid; 7193d9136f9SKevin Wolf int r; 7203d9136f9SKevin Wolf 7214ff45049SKevin Wolf keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs")); 7224ff45049SKevin Wolf if (keypairs) { 7234ff45049SKevin Wolf qdict_del(options, "=keyvalue-pairs"); 7244ff45049SKevin Wolf } 7254ff45049SKevin Wolf 7264ff45049SKevin Wolf secretid = g_strdup(qdict_get_try_str(options, "password-secret")); 7274ff45049SKevin Wolf if (secretid) { 7284ff45049SKevin Wolf qdict_del(options, "password-secret"); 7294ff45049SKevin Wolf } 7304ff45049SKevin Wolf 731f24b03b5SJeff Cody r = qemu_rbd_convert_options(options, &opts, &local_err); 7324bfb2741SKevin Wolf if (local_err) { 733084d1d13SJeff Cody /* If keypairs are present, that means some options are present in 734084d1d13SJeff Cody * the modern option format. Don't attempt to parse legacy option 735084d1d13SJeff Cody * formats, as we won't support mixed usage. */ 736084d1d13SJeff Cody if (keypairs) { 7374bfb2741SKevin Wolf error_propagate(errp, local_err); 7384bfb2741SKevin Wolf goto out; 7394bfb2741SKevin Wolf } 7404bfb2741SKevin Wolf 741084d1d13SJeff Cody /* If the initial attempt to convert and process the options failed, 742084d1d13SJeff Cody * we may be attempting to open an image file that has the rbd options 743084d1d13SJeff Cody * specified in the older format consisting of all key/value pairs 744084d1d13SJeff Cody * encoded in the filename. Go ahead and attempt to parse the 745084d1d13SJeff Cody * filename, and see if we can pull out the required options. */ 746084d1d13SJeff Cody r = qemu_rbd_attempt_legacy_options(options, &opts, &keypairs); 747084d1d13SJeff Cody if (r < 0) { 748084d1d13SJeff Cody /* Propagate the original error, not the legacy parsing fallback 749084d1d13SJeff Cody * error, as the latter was just a best-effort attempt. */ 750084d1d13SJeff Cody error_propagate(errp, local_err); 751084d1d13SJeff Cody goto out; 752084d1d13SJeff Cody } 753084d1d13SJeff Cody /* Take care whenever deciding to actually deprecate; once this ability 754084d1d13SJeff Cody * is removed, we will not be able to open any images with legacy-styled 755084d1d13SJeff Cody * backing image strings. */ 7565197f445SMarkus Armbruster warn_report("RBD options encoded in the filename as keyvalue pairs " 757084d1d13SJeff Cody "is deprecated"); 758084d1d13SJeff Cody } 759084d1d13SJeff Cody 760bfb15b4bSJeff Cody /* Remove the processed options from the QDict (the visitor processes 761bfb15b4bSJeff Cody * _all_ options in the QDict) */ 762bfb15b4bSJeff Cody while ((e = qdict_first(options))) { 763bfb15b4bSJeff Cody qdict_del(options, e->key); 764bfb15b4bSJeff Cody } 765bfb15b4bSJeff Cody 766d41a5588SKevin Wolf r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts, 767d41a5588SKevin Wolf !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp); 7683d9136f9SKevin Wolf if (r < 0) { 7694ff45049SKevin Wolf goto out; 7703d9136f9SKevin Wolf } 7713d9136f9SKevin Wolf 772d41a5588SKevin Wolf s->snap = g_strdup(opts->snapshot); 773d41a5588SKevin Wolf s->image_name = g_strdup(opts->image); 774d41a5588SKevin Wolf 775e2b8247aSJeff Cody /* rbd_open is always r/w */ 77680b61a27SJeff Cody r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap); 777ad32e9c0SJosh Durgin if (r < 0) { 77880b61a27SJeff Cody error_setg_errno(errp, -r, "error reading header from %s", 77980b61a27SJeff Cody s->image_name); 780eb93d5d9SSage Weil goto failed_open; 781ad32e9c0SJosh Durgin } 782ad32e9c0SJosh Durgin 783d24f8023SStefano Garzarella r = rbd_get_size(s->image, &s->image_size); 784d24f8023SStefano Garzarella if (r < 0) { 785d24f8023SStefano Garzarella error_setg_errno(errp, -r, "error getting image size from %s", 786d24f8023SStefano Garzarella s->image_name); 787d24f8023SStefano Garzarella rbd_close(s->image); 788d24f8023SStefano Garzarella goto failed_open; 789d24f8023SStefano Garzarella } 790d24f8023SStefano Garzarella 791e2b8247aSJeff Cody /* If we are using an rbd snapshot, we must be r/o, otherwise 792e2b8247aSJeff Cody * leave as-is */ 793e2b8247aSJeff Cody if (s->snap != NULL) { 794eaa2410fSKevin Wolf r = bdrv_apply_auto_read_only(bs, "rbd snapshots are read-only", errp); 795e2b8247aSJeff Cody if (r < 0) { 796a51b9c48SKevin Wolf rbd_close(s->image); 797e2b8247aSJeff Cody goto failed_open; 798e2b8247aSJeff Cody } 799e2b8247aSJeff Cody } 800f27aaf4bSChristian Brunner 8014ff45049SKevin Wolf r = 0; 8024ff45049SKevin Wolf goto out; 803f27aaf4bSChristian Brunner 804eb93d5d9SSage Weil failed_open: 805ad32e9c0SJosh Durgin rados_ioctx_destroy(s->io_ctx); 806eb93d5d9SSage Weil g_free(s->snap); 80780b61a27SJeff Cody g_free(s->image_name); 8083d9136f9SKevin Wolf rados_shutdown(s->cluster); 8094ff45049SKevin Wolf out: 8104bfb2741SKevin Wolf qapi_free_BlockdevOptionsRbd(opts); 8114ff45049SKevin Wolf g_free(keypairs); 8124ff45049SKevin Wolf g_free(secretid); 813f27aaf4bSChristian Brunner return r; 814f27aaf4bSChristian Brunner } 815f27aaf4bSChristian Brunner 81656e7cf8dSJeff Cody 81756e7cf8dSJeff Cody /* Since RBD is currently always opened R/W via the API, 81856e7cf8dSJeff Cody * we just need to check if we are using a snapshot or not, in 81956e7cf8dSJeff Cody * order to determine if we will allow it to be R/W */ 82056e7cf8dSJeff Cody static int qemu_rbd_reopen_prepare(BDRVReopenState *state, 82156e7cf8dSJeff Cody BlockReopenQueue *queue, Error **errp) 82256e7cf8dSJeff Cody { 82356e7cf8dSJeff Cody BDRVRBDState *s = state->bs->opaque; 82456e7cf8dSJeff Cody int ret = 0; 82556e7cf8dSJeff Cody 82656e7cf8dSJeff Cody if (s->snap && state->flags & BDRV_O_RDWR) { 82756e7cf8dSJeff Cody error_setg(errp, 82856e7cf8dSJeff Cody "Cannot change node '%s' to r/w when using RBD snapshot", 82956e7cf8dSJeff Cody bdrv_get_device_or_node_name(state->bs)); 83056e7cf8dSJeff Cody ret = -EINVAL; 83156e7cf8dSJeff Cody } 83256e7cf8dSJeff Cody 83356e7cf8dSJeff Cody return ret; 83456e7cf8dSJeff Cody } 83556e7cf8dSJeff Cody 836ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs) 837f27aaf4bSChristian Brunner { 838f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 839f27aaf4bSChristian Brunner 840ad32e9c0SJosh Durgin rbd_close(s->image); 841ad32e9c0SJosh Durgin rados_ioctx_destroy(s->io_ctx); 8427267c094SAnthony Liguori g_free(s->snap); 84380b61a27SJeff Cody g_free(s->image_name); 844ad32e9c0SJosh Durgin rados_shutdown(s->cluster); 845f27aaf4bSChristian Brunner } 846f27aaf4bSChristian Brunner 847d24f8023SStefano Garzarella /* Resize the RBD image and update the 'image_size' with the current size */ 848d24f8023SStefano Garzarella static int qemu_rbd_resize(BlockDriverState *bs, uint64_t size) 849d24f8023SStefano Garzarella { 850d24f8023SStefano Garzarella BDRVRBDState *s = bs->opaque; 851d24f8023SStefano Garzarella int r; 852d24f8023SStefano Garzarella 853d24f8023SStefano Garzarella r = rbd_resize(s->image, size); 854d24f8023SStefano Garzarella if (r < 0) { 855d24f8023SStefano Garzarella return r; 856d24f8023SStefano Garzarella } 857d24f8023SStefano Garzarella 858d24f8023SStefano Garzarella s->image_size = size; 859d24f8023SStefano Garzarella 860d24f8023SStefano Garzarella return 0; 861d24f8023SStefano Garzarella } 862d24f8023SStefano Garzarella 863d7331bedSStefan Hajnoczi static const AIOCBInfo rbd_aiocb_info = { 864f27aaf4bSChristian Brunner .aiocb_size = sizeof(RBDAIOCB), 865f27aaf4bSChristian Brunner }; 866f27aaf4bSChristian Brunner 867e04fb07fSStefan Hajnoczi static void rbd_finish_bh(void *opaque) 868f27aaf4bSChristian Brunner { 869e04fb07fSStefan Hajnoczi RADOSCB *rcb = opaque; 870e04fb07fSStefan Hajnoczi qemu_rbd_complete_aio(rcb); 871ad32e9c0SJosh Durgin } 872ad32e9c0SJosh Durgin 873ad32e9c0SJosh Durgin /* 874ad32e9c0SJosh Durgin * This is the callback function for rbd_aio_read and _write 875ad32e9c0SJosh Durgin * 876ad32e9c0SJosh Durgin * Note: this function is being called from a non qemu thread so 877ad32e9c0SJosh Durgin * we need to be careful about what we do here. Generally we only 878e04fb07fSStefan Hajnoczi * schedule a BH, and do the rest of the io completion handling 879e04fb07fSStefan Hajnoczi * from rbd_finish_bh() which runs in a qemu context. 880ad32e9c0SJosh Durgin */ 881ad32e9c0SJosh Durgin static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb) 882ad32e9c0SJosh Durgin { 883e04fb07fSStefan Hajnoczi RBDAIOCB *acb = rcb->acb; 884e04fb07fSStefan Hajnoczi 885ad32e9c0SJosh Durgin rcb->ret = rbd_aio_get_return_value(c); 886ad32e9c0SJosh Durgin rbd_aio_release(c); 887f27aaf4bSChristian Brunner 888e4ec5ad4SPavel Dovgalyuk replay_bh_schedule_oneshot_event(bdrv_get_aio_context(acb->common.bs), 889ea800191SStefan Hajnoczi rbd_finish_bh, rcb); 890473c7f02SStefan Priebe } 891f27aaf4bSChristian Brunner 892787f3133SJosh Durgin static int rbd_aio_discard_wrapper(rbd_image_t image, 893787f3133SJosh Durgin uint64_t off, 894787f3133SJosh Durgin uint64_t len, 895787f3133SJosh Durgin rbd_completion_t comp) 896787f3133SJosh Durgin { 897787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 898787f3133SJosh Durgin return rbd_aio_discard(image, off, len, comp); 899787f3133SJosh Durgin #else 900787f3133SJosh Durgin return -ENOTSUP; 901787f3133SJosh Durgin #endif 902787f3133SJosh Durgin } 903787f3133SJosh Durgin 904dc7588c1SJosh Durgin static int rbd_aio_flush_wrapper(rbd_image_t image, 905dc7588c1SJosh Durgin rbd_completion_t comp) 906dc7588c1SJosh Durgin { 907dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH 908dc7588c1SJosh Durgin return rbd_aio_flush(image, comp); 909dc7588c1SJosh Durgin #else 910dc7588c1SJosh Durgin return -ENOTSUP; 911dc7588c1SJosh Durgin #endif 912dc7588c1SJosh Durgin } 913dc7588c1SJosh Durgin 9147c84b1b8SMarkus Armbruster static BlockAIOCB *rbd_start_aio(BlockDriverState *bs, 9157bbca9e2SEric Blake int64_t off, 916f27aaf4bSChristian Brunner QEMUIOVector *qiov, 9177bbca9e2SEric Blake int64_t size, 918097310b5SMarkus Armbruster BlockCompletionFunc *cb, 919787f3133SJosh Durgin void *opaque, 920787f3133SJosh Durgin RBDAIOCmd cmd) 921f27aaf4bSChristian Brunner { 922f27aaf4bSChristian Brunner RBDAIOCB *acb; 9230f7a0237SKevin Wolf RADOSCB *rcb = NULL; 924ad32e9c0SJosh Durgin rbd_completion_t c; 92551a13528SJosh Durgin int r; 926f27aaf4bSChristian Brunner 927f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 928f27aaf4bSChristian Brunner 929d7331bedSStefan Hajnoczi acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque); 930787f3133SJosh Durgin acb->cmd = cmd; 931f27aaf4bSChristian Brunner acb->qiov = qiov; 9327bbca9e2SEric Blake assert(!qiov || qiov->size == size); 9331d393bdeStianqing 9341d393bdeStianqing rcb = g_new(RADOSCB, 1); 9351d393bdeStianqing 9361d393bdeStianqing if (!LIBRBD_USE_IOVEC) { 937dc7588c1SJosh Durgin if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) { 938787f3133SJosh Durgin acb->bounce = NULL; 939787f3133SJosh Durgin } else { 9400f7a0237SKevin Wolf acb->bounce = qemu_try_blockalign(bs, qiov->size); 9410f7a0237SKevin Wolf if (acb->bounce == NULL) { 9420f7a0237SKevin Wolf goto failed; 9430f7a0237SKevin Wolf } 944787f3133SJosh Durgin } 9451d393bdeStianqing if (cmd == RBD_AIO_WRITE) { 9461d393bdeStianqing qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); 9471d393bdeStianqing } 9481d393bdeStianqing rcb->buf = acb->bounce; 9491d393bdeStianqing } 9501d393bdeStianqing 951f27aaf4bSChristian Brunner acb->ret = 0; 952f27aaf4bSChristian Brunner acb->error = 0; 953f27aaf4bSChristian Brunner acb->s = s; 954f27aaf4bSChristian Brunner 955f27aaf4bSChristian Brunner rcb->acb = acb; 956f27aaf4bSChristian Brunner rcb->s = acb->s; 957ad32e9c0SJosh Durgin rcb->size = size; 95851a13528SJosh Durgin r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c); 95951a13528SJosh Durgin if (r < 0) { 96051a13528SJosh Durgin goto failed; 96151a13528SJosh Durgin } 962f27aaf4bSChristian Brunner 963787f3133SJosh Durgin switch (cmd) { 964d24f8023SStefano Garzarella case RBD_AIO_WRITE: { 965d24f8023SStefano Garzarella /* 966d24f8023SStefano Garzarella * RBD APIs don't allow us to write more than actual size, so in order 967d24f8023SStefano Garzarella * to support growing images, we resize the image before write 968d24f8023SStefano Garzarella * operations that exceed the current size. 969d24f8023SStefano Garzarella */ 970d24f8023SStefano Garzarella if (off + size > s->image_size) { 971d24f8023SStefano Garzarella r = qemu_rbd_resize(bs, off + size); 972d24f8023SStefano Garzarella if (r < 0) { 973d24f8023SStefano Garzarella goto failed_completion; 974d24f8023SStefano Garzarella } 975d24f8023SStefano Garzarella } 9761d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC 9771d393bdeStianqing r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c); 9781d393bdeStianqing #else 9791d393bdeStianqing r = rbd_aio_write(s->image, off, size, rcb->buf, c); 9801d393bdeStianqing #endif 981787f3133SJosh Durgin break; 982d24f8023SStefano Garzarella } 983787f3133SJosh Durgin case RBD_AIO_READ: 9841d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC 9851d393bdeStianqing r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c); 9861d393bdeStianqing #else 9871d393bdeStianqing r = rbd_aio_read(s->image, off, size, rcb->buf, c); 9881d393bdeStianqing #endif 989787f3133SJosh Durgin break; 990787f3133SJosh Durgin case RBD_AIO_DISCARD: 991787f3133SJosh Durgin r = rbd_aio_discard_wrapper(s->image, off, size, c); 992787f3133SJosh Durgin break; 993dc7588c1SJosh Durgin case RBD_AIO_FLUSH: 994dc7588c1SJosh Durgin r = rbd_aio_flush_wrapper(s->image, c); 995dc7588c1SJosh Durgin break; 996787f3133SJosh Durgin default: 997787f3133SJosh Durgin r = -EINVAL; 99851a13528SJosh Durgin } 99951a13528SJosh Durgin 100051a13528SJosh Durgin if (r < 0) { 1001405a2764SKevin Wolf goto failed_completion; 1002f27aaf4bSChristian Brunner } 1003f27aaf4bSChristian Brunner return &acb->common; 100451a13528SJosh Durgin 1005405a2764SKevin Wolf failed_completion: 1006405a2764SKevin Wolf rbd_aio_release(c); 100751a13528SJosh Durgin failed: 10087267c094SAnthony Liguori g_free(rcb); 10091d393bdeStianqing if (!LIBRBD_USE_IOVEC) { 1010405a2764SKevin Wolf qemu_vfree(acb->bounce); 10111d393bdeStianqing } 10121d393bdeStianqing 10138007429aSFam Zheng qemu_aio_unref(acb); 101451a13528SJosh Durgin return NULL; 1015f27aaf4bSChristian Brunner } 1016f27aaf4bSChristian Brunner 1017e8e16d4bSEric Blake static BlockAIOCB *qemu_rbd_aio_preadv(BlockDriverState *bs, 1018e8e16d4bSEric Blake uint64_t offset, uint64_t bytes, 1019e8e16d4bSEric Blake QEMUIOVector *qiov, int flags, 1020097310b5SMarkus Armbruster BlockCompletionFunc *cb, 1021f27aaf4bSChristian Brunner void *opaque) 1022f27aaf4bSChristian Brunner { 1023e8e16d4bSEric Blake return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque, 1024787f3133SJosh Durgin RBD_AIO_READ); 1025f27aaf4bSChristian Brunner } 1026f27aaf4bSChristian Brunner 1027e8e16d4bSEric Blake static BlockAIOCB *qemu_rbd_aio_pwritev(BlockDriverState *bs, 1028e8e16d4bSEric Blake uint64_t offset, uint64_t bytes, 1029e8e16d4bSEric Blake QEMUIOVector *qiov, int flags, 1030097310b5SMarkus Armbruster BlockCompletionFunc *cb, 1031f27aaf4bSChristian Brunner void *opaque) 1032f27aaf4bSChristian Brunner { 1033e8e16d4bSEric Blake return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque, 1034787f3133SJosh Durgin RBD_AIO_WRITE); 1035f27aaf4bSChristian Brunner } 1036f27aaf4bSChristian Brunner 1037dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH 10387c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs, 1039097310b5SMarkus Armbruster BlockCompletionFunc *cb, 1040dc7588c1SJosh Durgin void *opaque) 1041dc7588c1SJosh Durgin { 1042dc7588c1SJosh Durgin return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH); 1043dc7588c1SJosh Durgin } 1044dc7588c1SJosh Durgin 1045dc7588c1SJosh Durgin #else 1046dc7588c1SJosh Durgin 10478b94ff85SPaolo Bonzini static int qemu_rbd_co_flush(BlockDriverState *bs) 10487a3f5fe9SSage Weil { 10497a3f5fe9SSage Weil #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1) 10507a3f5fe9SSage Weil /* rbd_flush added in 0.1.1 */ 10517a3f5fe9SSage Weil BDRVRBDState *s = bs->opaque; 10527a3f5fe9SSage Weil return rbd_flush(s->image); 10537a3f5fe9SSage Weil #else 10547a3f5fe9SSage Weil return 0; 10557a3f5fe9SSage Weil #endif 10567a3f5fe9SSage Weil } 1057dc7588c1SJosh Durgin #endif 10587a3f5fe9SSage Weil 1059ad32e9c0SJosh Durgin static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi) 1060f27aaf4bSChristian Brunner { 1061f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 1062ad32e9c0SJosh Durgin rbd_image_info_t info; 1063ad32e9c0SJosh Durgin int r; 1064ad32e9c0SJosh Durgin 1065ad32e9c0SJosh Durgin r = rbd_stat(s->image, &info, sizeof(info)); 1066ad32e9c0SJosh Durgin if (r < 0) { 1067ad32e9c0SJosh Durgin return r; 1068ad32e9c0SJosh Durgin } 1069ad32e9c0SJosh Durgin 1070ad32e9c0SJosh Durgin bdi->cluster_size = info.obj_size; 1071f27aaf4bSChristian Brunner return 0; 1072f27aaf4bSChristian Brunner } 1073f27aaf4bSChristian Brunner 1074ad32e9c0SJosh Durgin static int64_t qemu_rbd_getlength(BlockDriverState *bs) 1075f27aaf4bSChristian Brunner { 1076f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 1077ad32e9c0SJosh Durgin rbd_image_info_t info; 1078ad32e9c0SJosh Durgin int r; 1079f27aaf4bSChristian Brunner 1080ad32e9c0SJosh Durgin r = rbd_stat(s->image, &info, sizeof(info)); 1081ad32e9c0SJosh Durgin if (r < 0) { 1082ad32e9c0SJosh Durgin return r; 1083f27aaf4bSChristian Brunner } 1084f27aaf4bSChristian Brunner 1085ad32e9c0SJosh Durgin return info.size; 1086ad32e9c0SJosh Durgin } 1087ad32e9c0SJosh Durgin 1088061ca8a3SKevin Wolf static int coroutine_fn qemu_rbd_co_truncate(BlockDriverState *bs, 1089061ca8a3SKevin Wolf int64_t offset, 1090*c80d8b06SMax Reitz bool exact, 1091061ca8a3SKevin Wolf PreallocMode prealloc, 1092061ca8a3SKevin Wolf Error **errp) 109330cdc48cSJosh Durgin { 109430cdc48cSJosh Durgin int r; 109530cdc48cSJosh Durgin 10968243ccb7SMax Reitz if (prealloc != PREALLOC_MODE_OFF) { 10978243ccb7SMax Reitz error_setg(errp, "Unsupported preallocation mode '%s'", 1098977c736fSMarkus Armbruster PreallocMode_str(prealloc)); 10998243ccb7SMax Reitz return -ENOTSUP; 11008243ccb7SMax Reitz } 11018243ccb7SMax Reitz 1102d24f8023SStefano Garzarella r = qemu_rbd_resize(bs, offset); 110330cdc48cSJosh Durgin if (r < 0) { 1104f59adb32SMax Reitz error_setg_errno(errp, -r, "Failed to resize file"); 110530cdc48cSJosh Durgin return r; 110630cdc48cSJosh Durgin } 110730cdc48cSJosh Durgin 110830cdc48cSJosh Durgin return 0; 110930cdc48cSJosh Durgin } 111030cdc48cSJosh Durgin 1111ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs, 1112ad32e9c0SJosh Durgin QEMUSnapshotInfo *sn_info) 1113f27aaf4bSChristian Brunner { 1114f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 1115f27aaf4bSChristian Brunner int r; 1116f27aaf4bSChristian Brunner 1117f27aaf4bSChristian Brunner if (sn_info->name[0] == '\0') { 1118f27aaf4bSChristian Brunner return -EINVAL; /* we need a name for rbd snapshots */ 1119f27aaf4bSChristian Brunner } 1120f27aaf4bSChristian Brunner 1121f27aaf4bSChristian Brunner /* 1122f27aaf4bSChristian Brunner * rbd snapshots are using the name as the user controlled unique identifier 1123f27aaf4bSChristian Brunner * we can't use the rbd snapid for that purpose, as it can't be set 1124f27aaf4bSChristian Brunner */ 1125f27aaf4bSChristian Brunner if (sn_info->id_str[0] != '\0' && 1126f27aaf4bSChristian Brunner strcmp(sn_info->id_str, sn_info->name) != 0) { 1127f27aaf4bSChristian Brunner return -EINVAL; 1128f27aaf4bSChristian Brunner } 1129f27aaf4bSChristian Brunner 1130f27aaf4bSChristian Brunner if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) { 1131f27aaf4bSChristian Brunner return -ERANGE; 1132f27aaf4bSChristian Brunner } 1133f27aaf4bSChristian Brunner 1134ad32e9c0SJosh Durgin r = rbd_snap_create(s->image, sn_info->name); 1135f27aaf4bSChristian Brunner if (r < 0) { 1136ad32e9c0SJosh Durgin error_report("failed to create snap: %s", strerror(-r)); 1137f27aaf4bSChristian Brunner return r; 1138f27aaf4bSChristian Brunner } 1139f27aaf4bSChristian Brunner 1140f27aaf4bSChristian Brunner return 0; 1141f27aaf4bSChristian Brunner } 1142f27aaf4bSChristian Brunner 1143bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs, 1144a89d89d3SWenchao Xia const char *snapshot_id, 1145a89d89d3SWenchao Xia const char *snapshot_name, 1146a89d89d3SWenchao Xia Error **errp) 1147bd603247SGregory Farnum { 1148bd603247SGregory Farnum BDRVRBDState *s = bs->opaque; 1149bd603247SGregory Farnum int r; 1150bd603247SGregory Farnum 1151a89d89d3SWenchao Xia if (!snapshot_name) { 1152a89d89d3SWenchao Xia error_setg(errp, "rbd need a valid snapshot name"); 1153a89d89d3SWenchao Xia return -EINVAL; 1154a89d89d3SWenchao Xia } 1155a89d89d3SWenchao Xia 1156a89d89d3SWenchao Xia /* If snapshot_id is specified, it must be equal to name, see 1157a89d89d3SWenchao Xia qemu_rbd_snap_list() */ 1158a89d89d3SWenchao Xia if (snapshot_id && strcmp(snapshot_id, snapshot_name)) { 1159a89d89d3SWenchao Xia error_setg(errp, 1160a89d89d3SWenchao Xia "rbd do not support snapshot id, it should be NULL or " 1161a89d89d3SWenchao Xia "equal to snapshot name"); 1162a89d89d3SWenchao Xia return -EINVAL; 1163a89d89d3SWenchao Xia } 1164a89d89d3SWenchao Xia 1165bd603247SGregory Farnum r = rbd_snap_remove(s->image, snapshot_name); 1166a89d89d3SWenchao Xia if (r < 0) { 1167a89d89d3SWenchao Xia error_setg_errno(errp, -r, "Failed to remove the snapshot"); 1168a89d89d3SWenchao Xia } 1169bd603247SGregory Farnum return r; 1170bd603247SGregory Farnum } 1171bd603247SGregory Farnum 1172bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs, 1173bd603247SGregory Farnum const char *snapshot_name) 1174bd603247SGregory Farnum { 1175bd603247SGregory Farnum BDRVRBDState *s = bs->opaque; 1176bd603247SGregory Farnum 11779be38598SEduardo Habkost return rbd_snap_rollback(s->image, snapshot_name); 1178bd603247SGregory Farnum } 1179bd603247SGregory Farnum 1180ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs, 1181ad32e9c0SJosh Durgin QEMUSnapshotInfo **psn_tab) 1182f27aaf4bSChristian Brunner { 1183f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 1184f27aaf4bSChristian Brunner QEMUSnapshotInfo *sn_info, *sn_tab = NULL; 1185ad32e9c0SJosh Durgin int i, snap_count; 1186ad32e9c0SJosh Durgin rbd_snap_info_t *snaps; 1187ad32e9c0SJosh Durgin int max_snaps = RBD_MAX_SNAPS; 1188f27aaf4bSChristian Brunner 1189ad32e9c0SJosh Durgin do { 119002c4f26bSMarkus Armbruster snaps = g_new(rbd_snap_info_t, max_snaps); 1191ad32e9c0SJosh Durgin snap_count = rbd_snap_list(s->image, snaps, &max_snaps); 11929e6337d0SStefan Hajnoczi if (snap_count <= 0) { 11937267c094SAnthony Liguori g_free(snaps); 1194f27aaf4bSChristian Brunner } 1195ad32e9c0SJosh Durgin } while (snap_count == -ERANGE); 1196f27aaf4bSChristian Brunner 1197ad32e9c0SJosh Durgin if (snap_count <= 0) { 1198b9c53290SJosh Durgin goto done; 1199f27aaf4bSChristian Brunner } 1200f27aaf4bSChristian Brunner 12015839e53bSMarkus Armbruster sn_tab = g_new0(QEMUSnapshotInfo, snap_count); 1202f27aaf4bSChristian Brunner 1203ad32e9c0SJosh Durgin for (i = 0; i < snap_count; i++) { 1204ad32e9c0SJosh Durgin const char *snap_name = snaps[i].name; 1205f27aaf4bSChristian Brunner 1206f27aaf4bSChristian Brunner sn_info = sn_tab + i; 1207f27aaf4bSChristian Brunner pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name); 1208f27aaf4bSChristian Brunner pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name); 1209f27aaf4bSChristian Brunner 1210ad32e9c0SJosh Durgin sn_info->vm_state_size = snaps[i].size; 1211f27aaf4bSChristian Brunner sn_info->date_sec = 0; 1212f27aaf4bSChristian Brunner sn_info->date_nsec = 0; 1213f27aaf4bSChristian Brunner sn_info->vm_clock_nsec = 0; 1214f27aaf4bSChristian Brunner } 1215ad32e9c0SJosh Durgin rbd_snap_list_end(snaps); 12169e6337d0SStefan Hajnoczi g_free(snaps); 1217ad32e9c0SJosh Durgin 1218b9c53290SJosh Durgin done: 1219f27aaf4bSChristian Brunner *psn_tab = sn_tab; 1220f27aaf4bSChristian Brunner return snap_count; 1221f27aaf4bSChristian Brunner } 1222f27aaf4bSChristian Brunner 1223787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 12244da444a0SEric Blake static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs, 12254da444a0SEric Blake int64_t offset, 1226f5a5ca79SManos Pitsidianakis int bytes, 1227097310b5SMarkus Armbruster BlockCompletionFunc *cb, 1228787f3133SJosh Durgin void *opaque) 1229787f3133SJosh Durgin { 1230f5a5ca79SManos Pitsidianakis return rbd_start_aio(bs, offset, NULL, bytes, cb, opaque, 1231787f3133SJosh Durgin RBD_AIO_DISCARD); 1232787f3133SJosh Durgin } 1233787f3133SJosh Durgin #endif 1234787f3133SJosh Durgin 1235be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE 12362b148f39SPaolo Bonzini static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs, 1237be217884SAdam Crume Error **errp) 1238be217884SAdam Crume { 1239be217884SAdam Crume BDRVRBDState *s = bs->opaque; 1240be217884SAdam Crume int r = rbd_invalidate_cache(s->image); 1241be217884SAdam Crume if (r < 0) { 1242be217884SAdam Crume error_setg_errno(errp, -r, "Failed to invalidate the cache"); 1243be217884SAdam Crume } 1244be217884SAdam Crume } 1245be217884SAdam Crume #endif 1246be217884SAdam Crume 1247bd0cf596SChunyan Liu static QemuOptsList qemu_rbd_create_opts = { 1248bd0cf596SChunyan Liu .name = "rbd-create-opts", 1249bd0cf596SChunyan Liu .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head), 1250bd0cf596SChunyan Liu .desc = { 1251f27aaf4bSChristian Brunner { 1252f27aaf4bSChristian Brunner .name = BLOCK_OPT_SIZE, 1253bd0cf596SChunyan Liu .type = QEMU_OPT_SIZE, 1254f27aaf4bSChristian Brunner .help = "Virtual disk size" 1255f27aaf4bSChristian Brunner }, 1256f27aaf4bSChristian Brunner { 1257f27aaf4bSChristian Brunner .name = BLOCK_OPT_CLUSTER_SIZE, 1258bd0cf596SChunyan Liu .type = QEMU_OPT_SIZE, 1259f27aaf4bSChristian Brunner .help = "RBD object size" 1260f27aaf4bSChristian Brunner }, 126160390a21SDaniel P. Berrange { 126260390a21SDaniel P. Berrange .name = "password-secret", 126360390a21SDaniel P. Berrange .type = QEMU_OPT_STRING, 126460390a21SDaniel P. Berrange .help = "ID of secret providing the password", 126560390a21SDaniel P. Berrange }, 1266bd0cf596SChunyan Liu { /* end of list */ } 1267bd0cf596SChunyan Liu } 1268f27aaf4bSChristian Brunner }; 1269f27aaf4bSChristian Brunner 12702654267cSMax Reitz static const char *const qemu_rbd_strong_runtime_opts[] = { 12712654267cSMax Reitz "pool", 12722654267cSMax Reitz "image", 12732654267cSMax Reitz "conf", 12742654267cSMax Reitz "snapshot", 12752654267cSMax Reitz "user", 12762654267cSMax Reitz "server.", 12772654267cSMax Reitz "password-secret", 12782654267cSMax Reitz 12792654267cSMax Reitz NULL 12802654267cSMax Reitz }; 12812654267cSMax Reitz 1282f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = { 1283f27aaf4bSChristian Brunner .format_name = "rbd", 1284f27aaf4bSChristian Brunner .instance_size = sizeof(BDRVRBDState), 1285c7cacb3eSJeff Cody .bdrv_parse_filename = qemu_rbd_parse_filename, 1286e8e16d4bSEric Blake .bdrv_refresh_limits = qemu_rbd_refresh_limits, 1287ad32e9c0SJosh Durgin .bdrv_file_open = qemu_rbd_open, 1288ad32e9c0SJosh Durgin .bdrv_close = qemu_rbd_close, 128956e7cf8dSJeff Cody .bdrv_reopen_prepare = qemu_rbd_reopen_prepare, 12901bebea37SKevin Wolf .bdrv_co_create = qemu_rbd_co_create, 1291efc75e2aSStefan Hajnoczi .bdrv_co_create_opts = qemu_rbd_co_create_opts, 12923ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1, 12931dcaf527SMax Reitz .bdrv_has_zero_init_truncate = bdrv_has_zero_init_1, 1294ad32e9c0SJosh Durgin .bdrv_get_info = qemu_rbd_getinfo, 1295bd0cf596SChunyan Liu .create_opts = &qemu_rbd_create_opts, 1296ad32e9c0SJosh Durgin .bdrv_getlength = qemu_rbd_getlength, 1297061ca8a3SKevin Wolf .bdrv_co_truncate = qemu_rbd_co_truncate, 1298f27aaf4bSChristian Brunner .protocol_name = "rbd", 1299f27aaf4bSChristian Brunner 1300e8e16d4bSEric Blake .bdrv_aio_preadv = qemu_rbd_aio_preadv, 1301e8e16d4bSEric Blake .bdrv_aio_pwritev = qemu_rbd_aio_pwritev, 1302dc7588c1SJosh Durgin 1303dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH 1304dc7588c1SJosh Durgin .bdrv_aio_flush = qemu_rbd_aio_flush, 1305dc7588c1SJosh Durgin #else 1306c68b89acSKevin Wolf .bdrv_co_flush_to_disk = qemu_rbd_co_flush, 1307dc7588c1SJosh Durgin #endif 1308f27aaf4bSChristian Brunner 1309787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 13104da444a0SEric Blake .bdrv_aio_pdiscard = qemu_rbd_aio_pdiscard, 1311787f3133SJosh Durgin #endif 1312787f3133SJosh Durgin 1313ad32e9c0SJosh Durgin .bdrv_snapshot_create = qemu_rbd_snap_create, 1314bd603247SGregory Farnum .bdrv_snapshot_delete = qemu_rbd_snap_remove, 1315ad32e9c0SJosh Durgin .bdrv_snapshot_list = qemu_rbd_snap_list, 1316bd603247SGregory Farnum .bdrv_snapshot_goto = qemu_rbd_snap_rollback, 1317be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE 13182b148f39SPaolo Bonzini .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache, 1319be217884SAdam Crume #endif 13202654267cSMax Reitz 13212654267cSMax Reitz .strong_runtime_opts = qemu_rbd_strong_runtime_opts, 1322f27aaf4bSChristian Brunner }; 1323f27aaf4bSChristian Brunner 1324f27aaf4bSChristian Brunner static void bdrv_rbd_init(void) 1325f27aaf4bSChristian Brunner { 1326f27aaf4bSChristian Brunner bdrv_register(&bdrv_rbd); 1327f27aaf4bSChristian Brunner } 1328f27aaf4bSChristian Brunner 1329f27aaf4bSChristian Brunner block_init(bdrv_rbd_init); 1330