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; 10719ae9ae0SFlorian 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; 15619ae9ae0SFlorian 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, '@')) { 17519ae9ae0SFlorian 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 { 18119ae9ae0SFlorian Florensa image_name = qemu_rbd_next_tok(p, ':', &p); 1827830f909SJeff Cody } 18319ae9ae0SFlorian Florensa /* Check for namespace in the image_name */ 18419ae9ae0SFlorian Florensa if (strchr(image_name, '/')) { 18519ae9ae0SFlorian Florensa found_str = qemu_rbd_next_tok(image_name, '/', &image_name); 18619ae9ae0SFlorian Florensa qemu_rbd_unescape(found_str); 18719ae9ae0SFlorian Florensa qdict_put_str(options, "namespace", found_str); 18819ae9ae0SFlorian Florensa } else { 18919ae9ae0SFlorian Florensa qdict_put_str(options, "namespace", ""); 19019ae9ae0SFlorian Florensa } 19119ae9ae0SFlorian Florensa qemu_rbd_unescape(image_name); 19219ae9ae0SFlorian 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 344d083f954SMarkus Armbruster /* FIXME Deprecate and remove keypairs or make it available in QMP. */ 3451bebea37SKevin Wolf static int qemu_rbd_do_create(BlockdevCreateOptions *options, 3461bebea37SKevin Wolf const char *keypairs, const char *password_secret, 3471bebea37SKevin Wolf Error **errp) 3481bebea37SKevin Wolf { 3491bebea37SKevin Wolf BlockdevCreateOptionsRbd *opts = &options->u.rbd; 3501bebea37SKevin Wolf rados_t cluster; 3511bebea37SKevin Wolf rados_ioctx_t io_ctx; 3521bebea37SKevin Wolf int obj_order = 0; 3531bebea37SKevin Wolf int ret; 3541bebea37SKevin Wolf 3551bebea37SKevin Wolf assert(options->driver == BLOCKDEV_DRIVER_RBD); 3561bebea37SKevin Wolf if (opts->location->has_snapshot) { 3571bebea37SKevin Wolf error_setg(errp, "Can't use snapshot name for image creation"); 3581bebea37SKevin Wolf return -EINVAL; 3591bebea37SKevin Wolf } 3601bebea37SKevin Wolf 3611bebea37SKevin Wolf if (opts->has_cluster_size) { 3621bebea37SKevin Wolf int64_t objsize = opts->cluster_size; 3631bebea37SKevin Wolf if ((objsize - 1) & objsize) { /* not a power of 2? */ 3641bebea37SKevin Wolf error_setg(errp, "obj size needs to be power of 2"); 3651bebea37SKevin Wolf return -EINVAL; 3661bebea37SKevin Wolf } 3671bebea37SKevin Wolf if (objsize < 4096) { 3681bebea37SKevin Wolf error_setg(errp, "obj size too small"); 3691bebea37SKevin Wolf return -EINVAL; 3701bebea37SKevin Wolf } 3711bebea37SKevin Wolf obj_order = ctz32(objsize); 3721bebea37SKevin Wolf } 3731bebea37SKevin Wolf 374aa045c2dSKevin Wolf ret = qemu_rbd_connect(&cluster, &io_ctx, opts->location, false, keypairs, 375aa045c2dSKevin Wolf password_secret, errp); 3761bebea37SKevin Wolf if (ret < 0) { 3771bebea37SKevin Wolf return ret; 3781bebea37SKevin Wolf } 3791bebea37SKevin Wolf 3801bebea37SKevin Wolf ret = rbd_create(io_ctx, opts->location->image, opts->size, &obj_order); 3811bebea37SKevin Wolf if (ret < 0) { 3821bebea37SKevin Wolf error_setg_errno(errp, -ret, "error rbd create"); 383aa045c2dSKevin Wolf goto out; 3841bebea37SKevin Wolf } 3851bebea37SKevin Wolf 3861bebea37SKevin Wolf ret = 0; 387aa045c2dSKevin Wolf out: 388aa045c2dSKevin Wolf rados_ioctx_destroy(io_ctx); 3891bebea37SKevin Wolf rados_shutdown(cluster); 3901bebea37SKevin Wolf return ret; 3911bebea37SKevin Wolf } 3921bebea37SKevin Wolf 3931bebea37SKevin Wolf static int qemu_rbd_co_create(BlockdevCreateOptions *options, Error **errp) 3941bebea37SKevin Wolf { 3951bebea37SKevin Wolf return qemu_rbd_do_create(options, NULL, NULL, errp); 3961bebea37SKevin Wolf } 3971bebea37SKevin Wolf 398b92902dfSMaxim Levitsky static int coroutine_fn qemu_rbd_co_create_opts(BlockDriver *drv, 399b92902dfSMaxim Levitsky const char *filename, 400efc75e2aSStefan Hajnoczi QemuOpts *opts, 401efc75e2aSStefan Hajnoczi Error **errp) 402f27aaf4bSChristian Brunner { 4031bebea37SKevin Wolf BlockdevCreateOptions *create_options; 4041bebea37SKevin Wolf BlockdevCreateOptionsRbd *rbd_opts; 4051bebea37SKevin Wolf BlockdevOptionsRbd *loc; 406d61563b2SMarkus Armbruster Error *local_err = NULL; 4071bebea37SKevin Wolf const char *keypairs, *password_secret; 408c7cacb3eSJeff Cody QDict *options = NULL; 409c7cacb3eSJeff Cody int ret = 0; 410f27aaf4bSChristian Brunner 4111bebea37SKevin Wolf create_options = g_new0(BlockdevCreateOptions, 1); 4121bebea37SKevin Wolf create_options->driver = BLOCKDEV_DRIVER_RBD; 4131bebea37SKevin Wolf rbd_opts = &create_options->u.rbd; 4141bebea37SKevin Wolf 4151bebea37SKevin Wolf rbd_opts->location = g_new0(BlockdevOptionsRbd, 1); 4161bebea37SKevin Wolf 4171bebea37SKevin Wolf password_secret = qemu_opt_get(opts, "password-secret"); 41860390a21SDaniel P. Berrange 419f27aaf4bSChristian Brunner /* Read out options */ 4201bebea37SKevin Wolf rbd_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 421c2eb918eSHu Tao BDRV_SECTOR_SIZE); 4221bebea37SKevin Wolf rbd_opts->cluster_size = qemu_opt_get_size_del(opts, 4231bebea37SKevin Wolf BLOCK_OPT_CLUSTER_SIZE, 0); 4241bebea37SKevin Wolf rbd_opts->has_cluster_size = (rbd_opts->cluster_size != 0); 425f27aaf4bSChristian Brunner 426c7cacb3eSJeff Cody options = qdict_new(); 427c7cacb3eSJeff Cody qemu_rbd_parse_filename(filename, options, &local_err); 428c7cacb3eSJeff Cody if (local_err) { 429c7cacb3eSJeff Cody ret = -EINVAL; 430c7cacb3eSJeff Cody error_propagate(errp, local_err); 431c7cacb3eSJeff Cody goto exit; 432c7cacb3eSJeff Cody } 433c7cacb3eSJeff Cody 434129c7d1cSMarkus Armbruster /* 435129c7d1cSMarkus Armbruster * Caution: while qdict_get_try_str() is fine, getting non-string 436129c7d1cSMarkus Armbruster * types would require more care. When @options come from -blockdev 437129c7d1cSMarkus Armbruster * or blockdev_add, its members are typed according to the QAPI 438129c7d1cSMarkus Armbruster * schema, but when they come from -drive, they're all QString. 439129c7d1cSMarkus Armbruster */ 4401bebea37SKevin Wolf loc = rbd_opts->location; 4411bebea37SKevin Wolf loc->pool = g_strdup(qdict_get_try_str(options, "pool")); 4421bebea37SKevin Wolf loc->conf = g_strdup(qdict_get_try_str(options, "conf")); 4431bebea37SKevin Wolf loc->has_conf = !!loc->conf; 4441bebea37SKevin Wolf loc->user = g_strdup(qdict_get_try_str(options, "user")); 4451bebea37SKevin Wolf loc->has_user = !!loc->user; 44619ae9ae0SFlorian Florensa loc->q_namespace = g_strdup(qdict_get_try_str(options, "namespace")); 4471bebea37SKevin Wolf loc->image = g_strdup(qdict_get_try_str(options, "image")); 44807846397SMarkus Armbruster keypairs = qdict_get_try_str(options, "=keyvalue-pairs"); 449c7cacb3eSJeff Cody 4501bebea37SKevin Wolf ret = qemu_rbd_do_create(create_options, keypairs, password_secret, errp); 45187cd3d20SVikhyat Umrao if (ret < 0) { 452c7cacb3eSJeff Cody goto exit; 453f27aaf4bSChristian Brunner } 454f27aaf4bSChristian Brunner 455c7cacb3eSJeff Cody exit: 456cb3e7f08SMarc-André Lureau qobject_unref(options); 4571bebea37SKevin Wolf qapi_free_BlockdevCreateOptions(create_options); 458f27aaf4bSChristian Brunner return ret; 459f27aaf4bSChristian Brunner } 460f27aaf4bSChristian Brunner 461f27aaf4bSChristian Brunner /* 462e04fb07fSStefan Hajnoczi * This aio completion is being called from rbd_finish_bh() and runs in qemu 463e04fb07fSStefan Hajnoczi * BH context. 464f27aaf4bSChristian Brunner */ 465ad32e9c0SJosh Durgin static void qemu_rbd_complete_aio(RADOSCB *rcb) 466f27aaf4bSChristian Brunner { 467f27aaf4bSChristian Brunner RBDAIOCB *acb = rcb->acb; 468f27aaf4bSChristian Brunner int64_t r; 469f27aaf4bSChristian Brunner 470f27aaf4bSChristian Brunner r = rcb->ret; 471f27aaf4bSChristian Brunner 472dc7588c1SJosh Durgin if (acb->cmd != RBD_AIO_READ) { 473f27aaf4bSChristian Brunner if (r < 0) { 474f27aaf4bSChristian Brunner acb->ret = r; 475f27aaf4bSChristian Brunner acb->error = 1; 476f27aaf4bSChristian Brunner } else if (!acb->error) { 477ad32e9c0SJosh Durgin acb->ret = rcb->size; 478f27aaf4bSChristian Brunner } 479f27aaf4bSChristian Brunner } else { 480ad32e9c0SJosh Durgin if (r < 0) { 4811d393bdeStianqing qemu_rbd_memset(rcb, 0); 482f27aaf4bSChristian Brunner acb->ret = r; 483f27aaf4bSChristian Brunner acb->error = 1; 484ad32e9c0SJosh Durgin } else if (r < rcb->size) { 4851d393bdeStianqing qemu_rbd_memset(rcb, r); 486f27aaf4bSChristian Brunner if (!acb->error) { 487ad32e9c0SJosh Durgin acb->ret = rcb->size; 488f27aaf4bSChristian Brunner } 489f27aaf4bSChristian Brunner } else if (!acb->error) { 490ad32e9c0SJosh Durgin acb->ret = r; 491f27aaf4bSChristian Brunner } 492f27aaf4bSChristian Brunner } 493e04fb07fSStefan Hajnoczi 4947267c094SAnthony Liguori g_free(rcb); 495e04fb07fSStefan Hajnoczi 4961d393bdeStianqing if (!LIBRBD_USE_IOVEC) { 497e04fb07fSStefan Hajnoczi if (acb->cmd == RBD_AIO_READ) { 498e04fb07fSStefan Hajnoczi qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); 499f27aaf4bSChristian Brunner } 500e04fb07fSStefan Hajnoczi qemu_vfree(acb->bounce); 5011d393bdeStianqing } 5021d393bdeStianqing 503e04fb07fSStefan Hajnoczi acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret)); 504f27aaf4bSChristian Brunner 5058007429aSFam Zheng qemu_aio_unref(acb); 506f27aaf4bSChristian Brunner } 507f27aaf4bSChristian Brunner 5084bfb2741SKevin Wolf static char *qemu_rbd_mon_host(BlockdevOptionsRbd *opts, Error **errp) 5090a55679bSJeff Cody { 5104bfb2741SKevin Wolf const char **vals; 5112836284dSMarkus Armbruster const char *host, *port; 5122836284dSMarkus Armbruster char *rados_str; 5134bfb2741SKevin Wolf InetSocketAddressBaseList *p; 5144bfb2741SKevin Wolf int i, cnt; 5150a55679bSJeff Cody 5164bfb2741SKevin Wolf if (!opts->has_server) { 5174bfb2741SKevin Wolf return NULL; 5180a55679bSJeff Cody } 5194bfb2741SKevin Wolf 5204bfb2741SKevin Wolf for (cnt = 0, p = opts->server; p; p = p->next) { 5214bfb2741SKevin Wolf cnt++; 5220a55679bSJeff Cody } 5230a55679bSJeff Cody 5244bfb2741SKevin Wolf vals = g_new(const char *, cnt + 1); 5254bfb2741SKevin Wolf 5264bfb2741SKevin Wolf for (i = 0, p = opts->server; p; p = p->next, i++) { 5274bfb2741SKevin Wolf host = p->value->host; 5284bfb2741SKevin Wolf port = p->value->port; 5294bfb2741SKevin Wolf 5300a55679bSJeff Cody if (strchr(host, ':')) { 5314bfb2741SKevin Wolf vals[i] = g_strdup_printf("[%s]:%s", host, port); 5320a55679bSJeff Cody } else { 5334bfb2741SKevin Wolf vals[i] = g_strdup_printf("%s:%s", host, port); 5340a55679bSJeff Cody } 5350a55679bSJeff Cody } 5362836284dSMarkus Armbruster vals[i] = NULL; 5370a55679bSJeff Cody 5382836284dSMarkus Armbruster rados_str = i ? g_strjoinv(";", (char **)vals) : NULL; 5392836284dSMarkus Armbruster g_strfreev((char **)vals); 5400a55679bSJeff Cody return rados_str; 5410a55679bSJeff Cody } 5420a55679bSJeff Cody 5433d9136f9SKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx, 5444bfb2741SKevin Wolf BlockdevOptionsRbd *opts, bool cache, 5454ff45049SKevin Wolf const char *keypairs, const char *secretid, 5464ff45049SKevin Wolf Error **errp) 547f27aaf4bSChristian Brunner { 5480a55679bSJeff Cody char *mon_host = NULL; 5493d9136f9SKevin Wolf Error *local_err = NULL; 550f27aaf4bSChristian Brunner int r; 551f27aaf4bSChristian Brunner 552d083f954SMarkus Armbruster if (secretid) { 553d083f954SMarkus Armbruster if (opts->key_secret) { 554d083f954SMarkus Armbruster error_setg(errp, 555d083f954SMarkus Armbruster "Legacy 'password-secret' clashes with 'key-secret'"); 556d083f954SMarkus Armbruster return -EINVAL; 557d083f954SMarkus Armbruster } 558d083f954SMarkus Armbruster opts->key_secret = g_strdup(secretid); 559d083f954SMarkus Armbruster opts->has_key_secret = true; 560d083f954SMarkus Armbruster } 561d083f954SMarkus Armbruster 5624bfb2741SKevin Wolf mon_host = qemu_rbd_mon_host(opts, &local_err); 56384d18f06SMarkus Armbruster if (local_err) { 564d61563b2SMarkus Armbruster error_propagate(errp, local_err); 5652836284dSMarkus Armbruster r = -EINVAL; 5662836284dSMarkus Armbruster goto failed_opts; 567a9ccedc3SKevin Wolf } 568a9ccedc3SKevin Wolf 5694bfb2741SKevin Wolf r = rados_create(cluster, opts->user); 570ad32e9c0SJosh Durgin if (r < 0) { 57187cd3d20SVikhyat Umrao error_setg_errno(errp, -r, "error initializing"); 572c3ca988dSKevin Wolf goto failed_opts; 573f27aaf4bSChristian Brunner } 574f27aaf4bSChristian Brunner 575c7cacb3eSJeff Cody /* try default location when conf=NULL, but ignore failure */ 5764bfb2741SKevin Wolf r = rados_conf_read_file(*cluster, opts->conf); 5774bfb2741SKevin Wolf if (opts->has_conf && r < 0) { 5784bfb2741SKevin Wolf error_setg_errno(errp, -r, "error reading conf file %s", opts->conf); 579e34d8f29SJosh Durgin goto failed_shutdown; 580e34d8f29SJosh Durgin } 58199a3c89dSJosh Durgin 5823d9136f9SKevin Wolf r = qemu_rbd_set_keypairs(*cluster, keypairs, errp); 58399a3c89dSJosh Durgin if (r < 0) { 58499a3c89dSJosh Durgin goto failed_shutdown; 58599a3c89dSJosh Durgin } 58699a3c89dSJosh Durgin 5870a55679bSJeff Cody if (mon_host) { 5883d9136f9SKevin Wolf r = rados_conf_set(*cluster, "mon_host", mon_host); 5890a55679bSJeff Cody if (r < 0) { 5900a55679bSJeff Cody goto failed_shutdown; 5910a55679bSJeff Cody } 5920a55679bSJeff Cody } 5930a55679bSJeff Cody 594d083f954SMarkus Armbruster r = qemu_rbd_set_auth(*cluster, opts, errp); 595d083f954SMarkus Armbruster if (r < 0) { 59660390a21SDaniel P. Berrange goto failed_shutdown; 59760390a21SDaniel P. Berrange } 59860390a21SDaniel P. Berrange 599b11f38fcSJosh Durgin /* 600b11f38fcSJosh Durgin * Fallback to more conservative semantics if setting cache 601b11f38fcSJosh Durgin * options fails. Ignore errors from setting rbd_cache because the 602b11f38fcSJosh Durgin * only possible error is that the option does not exist, and 603b11f38fcSJosh Durgin * librbd defaults to no caching. If write through caching cannot 604b11f38fcSJosh Durgin * be set up, fall back to no caching. 605b11f38fcSJosh Durgin */ 6063d9136f9SKevin Wolf if (cache) { 6073d9136f9SKevin Wolf rados_conf_set(*cluster, "rbd_cache", "true"); 608b11f38fcSJosh Durgin } else { 6093d9136f9SKevin Wolf rados_conf_set(*cluster, "rbd_cache", "false"); 610b11f38fcSJosh Durgin } 611b11f38fcSJosh Durgin 6123d9136f9SKevin Wolf r = rados_connect(*cluster); 613ad32e9c0SJosh Durgin if (r < 0) { 61487cd3d20SVikhyat Umrao error_setg_errno(errp, -r, "error connecting"); 615eb93d5d9SSage Weil goto failed_shutdown; 616ad32e9c0SJosh Durgin } 617ad32e9c0SJosh Durgin 6184bfb2741SKevin Wolf r = rados_ioctx_create(*cluster, opts->pool, io_ctx); 619ad32e9c0SJosh Durgin if (r < 0) { 6204bfb2741SKevin Wolf error_setg_errno(errp, -r, "error opening pool %s", opts->pool); 621eb93d5d9SSage Weil goto failed_shutdown; 622ad32e9c0SJosh Durgin } 62319ae9ae0SFlorian Florensa /* 62419ae9ae0SFlorian Florensa * Set the namespace after opening the io context on the pool, 62519ae9ae0SFlorian Florensa * if nspace == NULL or if nspace == "", it is just as we did nothing 62619ae9ae0SFlorian Florensa */ 62719ae9ae0SFlorian Florensa rados_ioctx_set_namespace(*io_ctx, opts->q_namespace); 628ad32e9c0SJosh Durgin 6293d9136f9SKevin Wolf return 0; 6303d9136f9SKevin Wolf 6313d9136f9SKevin Wolf failed_shutdown: 6323d9136f9SKevin Wolf rados_shutdown(*cluster); 6333d9136f9SKevin Wolf failed_opts: 6343d9136f9SKevin Wolf g_free(mon_host); 6353d9136f9SKevin Wolf return r; 6363d9136f9SKevin Wolf } 6373d9136f9SKevin Wolf 638f24b03b5SJeff Cody static int qemu_rbd_convert_options(QDict *options, BlockdevOptionsRbd **opts, 639f24b03b5SJeff Cody Error **errp) 640f24b03b5SJeff Cody { 641f24b03b5SJeff Cody Visitor *v; 642f24b03b5SJeff Cody 643f24b03b5SJeff Cody /* Convert the remaining options into a QAPI object */ 644f24b03b5SJeff Cody v = qobject_input_visitor_new_flat_confused(options, errp); 645f24b03b5SJeff Cody if (!v) { 646f24b03b5SJeff Cody return -EINVAL; 647f24b03b5SJeff Cody } 648f24b03b5SJeff Cody 649b11a093cSMarkus Armbruster visit_type_BlockdevOptionsRbd(v, NULL, opts, errp); 650f24b03b5SJeff Cody visit_free(v); 651b11a093cSMarkus Armbruster if (!opts) { 652f24b03b5SJeff Cody return -EINVAL; 653f24b03b5SJeff Cody } 654f24b03b5SJeff Cody 655f24b03b5SJeff Cody return 0; 656f24b03b5SJeff Cody } 657f24b03b5SJeff Cody 658084d1d13SJeff Cody static int qemu_rbd_attempt_legacy_options(QDict *options, 659084d1d13SJeff Cody BlockdevOptionsRbd **opts, 660084d1d13SJeff Cody char **keypairs) 661084d1d13SJeff Cody { 662084d1d13SJeff Cody char *filename; 663084d1d13SJeff Cody int r; 664084d1d13SJeff Cody 665084d1d13SJeff Cody filename = g_strdup(qdict_get_try_str(options, "filename")); 666084d1d13SJeff Cody if (!filename) { 667084d1d13SJeff Cody return -EINVAL; 668084d1d13SJeff Cody } 669084d1d13SJeff Cody qdict_del(options, "filename"); 670084d1d13SJeff Cody 671084d1d13SJeff Cody qemu_rbd_parse_filename(filename, options, NULL); 672084d1d13SJeff Cody 673084d1d13SJeff Cody /* keypairs freed by caller */ 674084d1d13SJeff Cody *keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs")); 675084d1d13SJeff Cody if (*keypairs) { 676084d1d13SJeff Cody qdict_del(options, "=keyvalue-pairs"); 677084d1d13SJeff Cody } 678084d1d13SJeff Cody 679084d1d13SJeff Cody r = qemu_rbd_convert_options(options, opts, NULL); 680084d1d13SJeff Cody 681084d1d13SJeff Cody g_free(filename); 682084d1d13SJeff Cody return r; 683084d1d13SJeff Cody } 684084d1d13SJeff Cody 6853d9136f9SKevin Wolf static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags, 6863d9136f9SKevin Wolf Error **errp) 6873d9136f9SKevin Wolf { 6883d9136f9SKevin Wolf BDRVRBDState *s = bs->opaque; 6894bfb2741SKevin Wolf BlockdevOptionsRbd *opts = NULL; 690bfb15b4bSJeff Cody const QDictEntry *e; 6913d9136f9SKevin Wolf Error *local_err = NULL; 6924ff45049SKevin Wolf char *keypairs, *secretid; 6933d9136f9SKevin Wolf int r; 6943d9136f9SKevin Wolf 6954ff45049SKevin Wolf keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs")); 6964ff45049SKevin Wolf if (keypairs) { 6974ff45049SKevin Wolf qdict_del(options, "=keyvalue-pairs"); 6984ff45049SKevin Wolf } 6994ff45049SKevin Wolf 7004ff45049SKevin Wolf secretid = g_strdup(qdict_get_try_str(options, "password-secret")); 7014ff45049SKevin Wolf if (secretid) { 7024ff45049SKevin Wolf qdict_del(options, "password-secret"); 7034ff45049SKevin Wolf } 7044ff45049SKevin Wolf 705f24b03b5SJeff Cody r = qemu_rbd_convert_options(options, &opts, &local_err); 7064bfb2741SKevin Wolf if (local_err) { 707084d1d13SJeff Cody /* If keypairs are present, that means some options are present in 708084d1d13SJeff Cody * the modern option format. Don't attempt to parse legacy option 709084d1d13SJeff Cody * formats, as we won't support mixed usage. */ 710084d1d13SJeff Cody if (keypairs) { 7114bfb2741SKevin Wolf error_propagate(errp, local_err); 7124bfb2741SKevin Wolf goto out; 7134bfb2741SKevin Wolf } 7144bfb2741SKevin Wolf 715084d1d13SJeff Cody /* If the initial attempt to convert and process the options failed, 716084d1d13SJeff Cody * we may be attempting to open an image file that has the rbd options 717084d1d13SJeff Cody * specified in the older format consisting of all key/value pairs 718084d1d13SJeff Cody * encoded in the filename. Go ahead and attempt to parse the 719084d1d13SJeff Cody * filename, and see if we can pull out the required options. */ 720084d1d13SJeff Cody r = qemu_rbd_attempt_legacy_options(options, &opts, &keypairs); 721084d1d13SJeff Cody if (r < 0) { 722084d1d13SJeff Cody /* Propagate the original error, not the legacy parsing fallback 723084d1d13SJeff Cody * error, as the latter was just a best-effort attempt. */ 724084d1d13SJeff Cody error_propagate(errp, local_err); 725084d1d13SJeff Cody goto out; 726084d1d13SJeff Cody } 727084d1d13SJeff Cody /* Take care whenever deciding to actually deprecate; once this ability 728084d1d13SJeff Cody * is removed, we will not be able to open any images with legacy-styled 729084d1d13SJeff Cody * backing image strings. */ 7305197f445SMarkus Armbruster warn_report("RBD options encoded in the filename as keyvalue pairs " 731084d1d13SJeff Cody "is deprecated"); 732084d1d13SJeff Cody } 733084d1d13SJeff Cody 734bfb15b4bSJeff Cody /* Remove the processed options from the QDict (the visitor processes 735bfb15b4bSJeff Cody * _all_ options in the QDict) */ 736bfb15b4bSJeff Cody while ((e = qdict_first(options))) { 737bfb15b4bSJeff Cody qdict_del(options, e->key); 738bfb15b4bSJeff Cody } 739bfb15b4bSJeff Cody 740d41a5588SKevin Wolf r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts, 741d41a5588SKevin Wolf !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp); 7423d9136f9SKevin Wolf if (r < 0) { 7434ff45049SKevin Wolf goto out; 7443d9136f9SKevin Wolf } 7453d9136f9SKevin Wolf 746d41a5588SKevin Wolf s->snap = g_strdup(opts->snapshot); 747d41a5588SKevin Wolf s->image_name = g_strdup(opts->image); 748d41a5588SKevin Wolf 749e2b8247aSJeff Cody /* rbd_open is always r/w */ 75080b61a27SJeff Cody r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap); 751ad32e9c0SJosh Durgin if (r < 0) { 75280b61a27SJeff Cody error_setg_errno(errp, -r, "error reading header from %s", 75380b61a27SJeff Cody s->image_name); 754eb93d5d9SSage Weil goto failed_open; 755ad32e9c0SJosh Durgin } 756ad32e9c0SJosh Durgin 757d24f8023SStefano Garzarella r = rbd_get_size(s->image, &s->image_size); 758d24f8023SStefano Garzarella if (r < 0) { 759d24f8023SStefano Garzarella error_setg_errno(errp, -r, "error getting image size from %s", 760d24f8023SStefano Garzarella s->image_name); 761d24f8023SStefano Garzarella rbd_close(s->image); 762d24f8023SStefano Garzarella goto failed_open; 763d24f8023SStefano Garzarella } 764d24f8023SStefano Garzarella 765e2b8247aSJeff Cody /* If we are using an rbd snapshot, we must be r/o, otherwise 766e2b8247aSJeff Cody * leave as-is */ 767e2b8247aSJeff Cody if (s->snap != NULL) { 768eaa2410fSKevin Wolf r = bdrv_apply_auto_read_only(bs, "rbd snapshots are read-only", errp); 769e2b8247aSJeff Cody if (r < 0) { 770a51b9c48SKevin Wolf rbd_close(s->image); 771e2b8247aSJeff Cody goto failed_open; 772e2b8247aSJeff Cody } 773e2b8247aSJeff Cody } 774f27aaf4bSChristian Brunner 7752f98910dSEric Blake /* When extending regular files, we get zeros from the OS */ 7762f98910dSEric Blake bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE; 7772f98910dSEric Blake 7784ff45049SKevin Wolf r = 0; 7794ff45049SKevin Wolf goto out; 780f27aaf4bSChristian Brunner 781eb93d5d9SSage Weil failed_open: 782ad32e9c0SJosh Durgin rados_ioctx_destroy(s->io_ctx); 783eb93d5d9SSage Weil g_free(s->snap); 78480b61a27SJeff Cody g_free(s->image_name); 7853d9136f9SKevin Wolf rados_shutdown(s->cluster); 7864ff45049SKevin Wolf out: 7874bfb2741SKevin Wolf qapi_free_BlockdevOptionsRbd(opts); 7884ff45049SKevin Wolf g_free(keypairs); 7894ff45049SKevin Wolf g_free(secretid); 790f27aaf4bSChristian Brunner return r; 791f27aaf4bSChristian Brunner } 792f27aaf4bSChristian Brunner 79356e7cf8dSJeff Cody 79456e7cf8dSJeff Cody /* Since RBD is currently always opened R/W via the API, 79556e7cf8dSJeff Cody * we just need to check if we are using a snapshot or not, in 79656e7cf8dSJeff Cody * order to determine if we will allow it to be R/W */ 79756e7cf8dSJeff Cody static int qemu_rbd_reopen_prepare(BDRVReopenState *state, 79856e7cf8dSJeff Cody BlockReopenQueue *queue, Error **errp) 79956e7cf8dSJeff Cody { 80056e7cf8dSJeff Cody BDRVRBDState *s = state->bs->opaque; 80156e7cf8dSJeff Cody int ret = 0; 80256e7cf8dSJeff Cody 80356e7cf8dSJeff Cody if (s->snap && state->flags & BDRV_O_RDWR) { 80456e7cf8dSJeff Cody error_setg(errp, 80556e7cf8dSJeff Cody "Cannot change node '%s' to r/w when using RBD snapshot", 80656e7cf8dSJeff Cody bdrv_get_device_or_node_name(state->bs)); 80756e7cf8dSJeff Cody ret = -EINVAL; 80856e7cf8dSJeff Cody } 80956e7cf8dSJeff Cody 81056e7cf8dSJeff Cody return ret; 81156e7cf8dSJeff Cody } 81256e7cf8dSJeff Cody 813ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs) 814f27aaf4bSChristian Brunner { 815f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 816f27aaf4bSChristian Brunner 817ad32e9c0SJosh Durgin rbd_close(s->image); 818ad32e9c0SJosh Durgin rados_ioctx_destroy(s->io_ctx); 8197267c094SAnthony Liguori g_free(s->snap); 82080b61a27SJeff Cody g_free(s->image_name); 821ad32e9c0SJosh Durgin rados_shutdown(s->cluster); 822f27aaf4bSChristian Brunner } 823f27aaf4bSChristian Brunner 824d24f8023SStefano Garzarella /* Resize the RBD image and update the 'image_size' with the current size */ 825d24f8023SStefano Garzarella static int qemu_rbd_resize(BlockDriverState *bs, uint64_t size) 826d24f8023SStefano Garzarella { 827d24f8023SStefano Garzarella BDRVRBDState *s = bs->opaque; 828d24f8023SStefano Garzarella int r; 829d24f8023SStefano Garzarella 830d24f8023SStefano Garzarella r = rbd_resize(s->image, size); 831d24f8023SStefano Garzarella if (r < 0) { 832d24f8023SStefano Garzarella return r; 833d24f8023SStefano Garzarella } 834d24f8023SStefano Garzarella 835d24f8023SStefano Garzarella s->image_size = size; 836d24f8023SStefano Garzarella 837d24f8023SStefano Garzarella return 0; 838d24f8023SStefano Garzarella } 839d24f8023SStefano Garzarella 840d7331bedSStefan Hajnoczi static const AIOCBInfo rbd_aiocb_info = { 841f27aaf4bSChristian Brunner .aiocb_size = sizeof(RBDAIOCB), 842f27aaf4bSChristian Brunner }; 843f27aaf4bSChristian Brunner 844e04fb07fSStefan Hajnoczi static void rbd_finish_bh(void *opaque) 845f27aaf4bSChristian Brunner { 846e04fb07fSStefan Hajnoczi RADOSCB *rcb = opaque; 847e04fb07fSStefan Hajnoczi qemu_rbd_complete_aio(rcb); 848ad32e9c0SJosh Durgin } 849ad32e9c0SJosh Durgin 850ad32e9c0SJosh Durgin /* 851ad32e9c0SJosh Durgin * This is the callback function for rbd_aio_read and _write 852ad32e9c0SJosh Durgin * 853ad32e9c0SJosh Durgin * Note: this function is being called from a non qemu thread so 854ad32e9c0SJosh Durgin * we need to be careful about what we do here. Generally we only 855e04fb07fSStefan Hajnoczi * schedule a BH, and do the rest of the io completion handling 856e04fb07fSStefan Hajnoczi * from rbd_finish_bh() which runs in a qemu context. 857ad32e9c0SJosh Durgin */ 858ad32e9c0SJosh Durgin static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb) 859ad32e9c0SJosh Durgin { 860e04fb07fSStefan Hajnoczi RBDAIOCB *acb = rcb->acb; 861e04fb07fSStefan Hajnoczi 862ad32e9c0SJosh Durgin rcb->ret = rbd_aio_get_return_value(c); 863ad32e9c0SJosh Durgin rbd_aio_release(c); 864f27aaf4bSChristian Brunner 865e4ec5ad4SPavel Dovgalyuk replay_bh_schedule_oneshot_event(bdrv_get_aio_context(acb->common.bs), 866ea800191SStefan Hajnoczi rbd_finish_bh, rcb); 867473c7f02SStefan Priebe } 868f27aaf4bSChristian Brunner 869787f3133SJosh Durgin static int rbd_aio_discard_wrapper(rbd_image_t image, 870787f3133SJosh Durgin uint64_t off, 871787f3133SJosh Durgin uint64_t len, 872787f3133SJosh Durgin rbd_completion_t comp) 873787f3133SJosh Durgin { 874787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 875787f3133SJosh Durgin return rbd_aio_discard(image, off, len, comp); 876787f3133SJosh Durgin #else 877787f3133SJosh Durgin return -ENOTSUP; 878787f3133SJosh Durgin #endif 879787f3133SJosh Durgin } 880787f3133SJosh Durgin 881dc7588c1SJosh Durgin static int rbd_aio_flush_wrapper(rbd_image_t image, 882dc7588c1SJosh Durgin rbd_completion_t comp) 883dc7588c1SJosh Durgin { 884dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH 885dc7588c1SJosh Durgin return rbd_aio_flush(image, comp); 886dc7588c1SJosh Durgin #else 887dc7588c1SJosh Durgin return -ENOTSUP; 888dc7588c1SJosh Durgin #endif 889dc7588c1SJosh Durgin } 890dc7588c1SJosh Durgin 8917c84b1b8SMarkus Armbruster static BlockAIOCB *rbd_start_aio(BlockDriverState *bs, 8927bbca9e2SEric Blake int64_t off, 893f27aaf4bSChristian Brunner QEMUIOVector *qiov, 8947bbca9e2SEric Blake int64_t size, 895097310b5SMarkus Armbruster BlockCompletionFunc *cb, 896787f3133SJosh Durgin void *opaque, 897787f3133SJosh Durgin RBDAIOCmd cmd) 898f27aaf4bSChristian Brunner { 899f27aaf4bSChristian Brunner RBDAIOCB *acb; 9000f7a0237SKevin Wolf RADOSCB *rcb = NULL; 901ad32e9c0SJosh Durgin rbd_completion_t c; 90251a13528SJosh Durgin int r; 903f27aaf4bSChristian Brunner 904f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 905f27aaf4bSChristian Brunner 906d7331bedSStefan Hajnoczi acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque); 907787f3133SJosh Durgin acb->cmd = cmd; 908f27aaf4bSChristian Brunner acb->qiov = qiov; 9097bbca9e2SEric Blake assert(!qiov || qiov->size == size); 9101d393bdeStianqing 9111d393bdeStianqing rcb = g_new(RADOSCB, 1); 9121d393bdeStianqing 9131d393bdeStianqing if (!LIBRBD_USE_IOVEC) { 914dc7588c1SJosh Durgin if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) { 915787f3133SJosh Durgin acb->bounce = NULL; 916787f3133SJosh Durgin } else { 9170f7a0237SKevin Wolf acb->bounce = qemu_try_blockalign(bs, qiov->size); 9180f7a0237SKevin Wolf if (acb->bounce == NULL) { 9190f7a0237SKevin Wolf goto failed; 9200f7a0237SKevin Wolf } 921787f3133SJosh Durgin } 9221d393bdeStianqing if (cmd == RBD_AIO_WRITE) { 9231d393bdeStianqing qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); 9241d393bdeStianqing } 9251d393bdeStianqing rcb->buf = acb->bounce; 9261d393bdeStianqing } 9271d393bdeStianqing 928f27aaf4bSChristian Brunner acb->ret = 0; 929f27aaf4bSChristian Brunner acb->error = 0; 930f27aaf4bSChristian Brunner acb->s = s; 931f27aaf4bSChristian Brunner 932f27aaf4bSChristian Brunner rcb->acb = acb; 933f27aaf4bSChristian Brunner rcb->s = acb->s; 934ad32e9c0SJosh Durgin rcb->size = size; 93551a13528SJosh Durgin r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c); 93651a13528SJosh Durgin if (r < 0) { 93751a13528SJosh Durgin goto failed; 93851a13528SJosh Durgin } 939f27aaf4bSChristian Brunner 940787f3133SJosh Durgin switch (cmd) { 941d24f8023SStefano Garzarella case RBD_AIO_WRITE: { 942d24f8023SStefano Garzarella /* 943d24f8023SStefano Garzarella * RBD APIs don't allow us to write more than actual size, so in order 944d24f8023SStefano Garzarella * to support growing images, we resize the image before write 945d24f8023SStefano Garzarella * operations that exceed the current size. 946d24f8023SStefano Garzarella */ 947d24f8023SStefano Garzarella if (off + size > s->image_size) { 948d24f8023SStefano Garzarella r = qemu_rbd_resize(bs, off + size); 949d24f8023SStefano Garzarella if (r < 0) { 950d24f8023SStefano Garzarella goto failed_completion; 951d24f8023SStefano Garzarella } 952d24f8023SStefano Garzarella } 9531d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC 9541d393bdeStianqing r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c); 9551d393bdeStianqing #else 9561d393bdeStianqing r = rbd_aio_write(s->image, off, size, rcb->buf, c); 9571d393bdeStianqing #endif 958787f3133SJosh Durgin break; 959d24f8023SStefano Garzarella } 960787f3133SJosh Durgin case RBD_AIO_READ: 9611d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC 9621d393bdeStianqing r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c); 9631d393bdeStianqing #else 9641d393bdeStianqing r = rbd_aio_read(s->image, off, size, rcb->buf, c); 9651d393bdeStianqing #endif 966787f3133SJosh Durgin break; 967787f3133SJosh Durgin case RBD_AIO_DISCARD: 968787f3133SJosh Durgin r = rbd_aio_discard_wrapper(s->image, off, size, c); 969787f3133SJosh Durgin break; 970dc7588c1SJosh Durgin case RBD_AIO_FLUSH: 971dc7588c1SJosh Durgin r = rbd_aio_flush_wrapper(s->image, c); 972dc7588c1SJosh Durgin break; 973787f3133SJosh Durgin default: 974787f3133SJosh Durgin r = -EINVAL; 97551a13528SJosh Durgin } 97651a13528SJosh Durgin 97751a13528SJosh Durgin if (r < 0) { 978405a2764SKevin Wolf goto failed_completion; 979f27aaf4bSChristian Brunner } 980f27aaf4bSChristian Brunner return &acb->common; 98151a13528SJosh Durgin 982405a2764SKevin Wolf failed_completion: 983405a2764SKevin Wolf rbd_aio_release(c); 98451a13528SJosh Durgin failed: 9857267c094SAnthony Liguori g_free(rcb); 9861d393bdeStianqing if (!LIBRBD_USE_IOVEC) { 987405a2764SKevin Wolf qemu_vfree(acb->bounce); 9881d393bdeStianqing } 9891d393bdeStianqing 9908007429aSFam Zheng qemu_aio_unref(acb); 99151a13528SJosh Durgin return NULL; 992f27aaf4bSChristian Brunner } 993f27aaf4bSChristian Brunner 994e8e16d4bSEric Blake static BlockAIOCB *qemu_rbd_aio_preadv(BlockDriverState *bs, 995e8e16d4bSEric Blake uint64_t offset, uint64_t bytes, 996e8e16d4bSEric Blake QEMUIOVector *qiov, int flags, 997097310b5SMarkus Armbruster BlockCompletionFunc *cb, 998f27aaf4bSChristian Brunner void *opaque) 999f27aaf4bSChristian Brunner { 1000e8e16d4bSEric Blake return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque, 1001787f3133SJosh Durgin RBD_AIO_READ); 1002f27aaf4bSChristian Brunner } 1003f27aaf4bSChristian Brunner 1004e8e16d4bSEric Blake static BlockAIOCB *qemu_rbd_aio_pwritev(BlockDriverState *bs, 1005e8e16d4bSEric Blake uint64_t offset, uint64_t bytes, 1006e8e16d4bSEric Blake QEMUIOVector *qiov, int flags, 1007097310b5SMarkus Armbruster BlockCompletionFunc *cb, 1008f27aaf4bSChristian Brunner void *opaque) 1009f27aaf4bSChristian Brunner { 1010e8e16d4bSEric Blake return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque, 1011787f3133SJosh Durgin RBD_AIO_WRITE); 1012f27aaf4bSChristian Brunner } 1013f27aaf4bSChristian Brunner 1014dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH 10157c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs, 1016097310b5SMarkus Armbruster BlockCompletionFunc *cb, 1017dc7588c1SJosh Durgin void *opaque) 1018dc7588c1SJosh Durgin { 1019dc7588c1SJosh Durgin return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH); 1020dc7588c1SJosh Durgin } 1021dc7588c1SJosh Durgin 1022dc7588c1SJosh Durgin #else 1023dc7588c1SJosh Durgin 10248b94ff85SPaolo Bonzini static int qemu_rbd_co_flush(BlockDriverState *bs) 10257a3f5fe9SSage Weil { 10267a3f5fe9SSage Weil #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1) 10277a3f5fe9SSage Weil /* rbd_flush added in 0.1.1 */ 10287a3f5fe9SSage Weil BDRVRBDState *s = bs->opaque; 10297a3f5fe9SSage Weil return rbd_flush(s->image); 10307a3f5fe9SSage Weil #else 10317a3f5fe9SSage Weil return 0; 10327a3f5fe9SSage Weil #endif 10337a3f5fe9SSage Weil } 1034dc7588c1SJosh Durgin #endif 10357a3f5fe9SSage Weil 1036ad32e9c0SJosh Durgin static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi) 1037f27aaf4bSChristian Brunner { 1038f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 1039ad32e9c0SJosh Durgin rbd_image_info_t info; 1040ad32e9c0SJosh Durgin int r; 1041ad32e9c0SJosh Durgin 1042ad32e9c0SJosh Durgin r = rbd_stat(s->image, &info, sizeof(info)); 1043ad32e9c0SJosh Durgin if (r < 0) { 1044ad32e9c0SJosh Durgin return r; 1045ad32e9c0SJosh Durgin } 1046ad32e9c0SJosh Durgin 1047ad32e9c0SJosh Durgin bdi->cluster_size = info.obj_size; 1048f27aaf4bSChristian Brunner return 0; 1049f27aaf4bSChristian Brunner } 1050f27aaf4bSChristian Brunner 1051ad32e9c0SJosh Durgin static int64_t qemu_rbd_getlength(BlockDriverState *bs) 1052f27aaf4bSChristian Brunner { 1053f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 1054ad32e9c0SJosh Durgin rbd_image_info_t info; 1055ad32e9c0SJosh Durgin int r; 1056f27aaf4bSChristian Brunner 1057ad32e9c0SJosh Durgin r = rbd_stat(s->image, &info, sizeof(info)); 1058ad32e9c0SJosh Durgin if (r < 0) { 1059ad32e9c0SJosh Durgin return r; 1060f27aaf4bSChristian Brunner } 1061f27aaf4bSChristian Brunner 1062ad32e9c0SJosh Durgin return info.size; 1063ad32e9c0SJosh Durgin } 1064ad32e9c0SJosh Durgin 1065061ca8a3SKevin Wolf static int coroutine_fn qemu_rbd_co_truncate(BlockDriverState *bs, 1066061ca8a3SKevin Wolf int64_t offset, 1067c80d8b06SMax Reitz bool exact, 1068061ca8a3SKevin Wolf PreallocMode prealloc, 106992b92799SKevin Wolf BdrvRequestFlags flags, 1070061ca8a3SKevin Wolf Error **errp) 107130cdc48cSJosh Durgin { 107230cdc48cSJosh Durgin int r; 107330cdc48cSJosh Durgin 10748243ccb7SMax Reitz if (prealloc != PREALLOC_MODE_OFF) { 10758243ccb7SMax Reitz error_setg(errp, "Unsupported preallocation mode '%s'", 1076977c736fSMarkus Armbruster PreallocMode_str(prealloc)); 10778243ccb7SMax Reitz return -ENOTSUP; 10788243ccb7SMax Reitz } 10798243ccb7SMax Reitz 1080d24f8023SStefano Garzarella r = qemu_rbd_resize(bs, offset); 108130cdc48cSJosh Durgin if (r < 0) { 1082f59adb32SMax Reitz error_setg_errno(errp, -r, "Failed to resize file"); 108330cdc48cSJosh Durgin return r; 108430cdc48cSJosh Durgin } 108530cdc48cSJosh Durgin 108630cdc48cSJosh Durgin return 0; 108730cdc48cSJosh Durgin } 108830cdc48cSJosh Durgin 1089ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs, 1090ad32e9c0SJosh Durgin QEMUSnapshotInfo *sn_info) 1091f27aaf4bSChristian Brunner { 1092f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 1093f27aaf4bSChristian Brunner int r; 1094f27aaf4bSChristian Brunner 1095f27aaf4bSChristian Brunner if (sn_info->name[0] == '\0') { 1096f27aaf4bSChristian Brunner return -EINVAL; /* we need a name for rbd snapshots */ 1097f27aaf4bSChristian Brunner } 1098f27aaf4bSChristian Brunner 1099f27aaf4bSChristian Brunner /* 1100f27aaf4bSChristian Brunner * rbd snapshots are using the name as the user controlled unique identifier 1101f27aaf4bSChristian Brunner * we can't use the rbd snapid for that purpose, as it can't be set 1102f27aaf4bSChristian Brunner */ 1103f27aaf4bSChristian Brunner if (sn_info->id_str[0] != '\0' && 1104f27aaf4bSChristian Brunner strcmp(sn_info->id_str, sn_info->name) != 0) { 1105f27aaf4bSChristian Brunner return -EINVAL; 1106f27aaf4bSChristian Brunner } 1107f27aaf4bSChristian Brunner 1108f27aaf4bSChristian Brunner if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) { 1109f27aaf4bSChristian Brunner return -ERANGE; 1110f27aaf4bSChristian Brunner } 1111f27aaf4bSChristian Brunner 1112ad32e9c0SJosh Durgin r = rbd_snap_create(s->image, sn_info->name); 1113f27aaf4bSChristian Brunner if (r < 0) { 1114ad32e9c0SJosh Durgin error_report("failed to create snap: %s", strerror(-r)); 1115f27aaf4bSChristian Brunner return r; 1116f27aaf4bSChristian Brunner } 1117f27aaf4bSChristian Brunner 1118f27aaf4bSChristian Brunner return 0; 1119f27aaf4bSChristian Brunner } 1120f27aaf4bSChristian Brunner 1121bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs, 1122a89d89d3SWenchao Xia const char *snapshot_id, 1123a89d89d3SWenchao Xia const char *snapshot_name, 1124a89d89d3SWenchao Xia Error **errp) 1125bd603247SGregory Farnum { 1126bd603247SGregory Farnum BDRVRBDState *s = bs->opaque; 1127bd603247SGregory Farnum int r; 1128bd603247SGregory Farnum 1129a89d89d3SWenchao Xia if (!snapshot_name) { 1130a89d89d3SWenchao Xia error_setg(errp, "rbd need a valid snapshot name"); 1131a89d89d3SWenchao Xia return -EINVAL; 1132a89d89d3SWenchao Xia } 1133a89d89d3SWenchao Xia 1134a89d89d3SWenchao Xia /* If snapshot_id is specified, it must be equal to name, see 1135a89d89d3SWenchao Xia qemu_rbd_snap_list() */ 1136a89d89d3SWenchao Xia if (snapshot_id && strcmp(snapshot_id, snapshot_name)) { 1137a89d89d3SWenchao Xia error_setg(errp, 1138a89d89d3SWenchao Xia "rbd do not support snapshot id, it should be NULL or " 1139a89d89d3SWenchao Xia "equal to snapshot name"); 1140a89d89d3SWenchao Xia return -EINVAL; 1141a89d89d3SWenchao Xia } 1142a89d89d3SWenchao Xia 1143bd603247SGregory Farnum r = rbd_snap_remove(s->image, snapshot_name); 1144a89d89d3SWenchao Xia if (r < 0) { 1145a89d89d3SWenchao Xia error_setg_errno(errp, -r, "Failed to remove the snapshot"); 1146a89d89d3SWenchao Xia } 1147bd603247SGregory Farnum return r; 1148bd603247SGregory Farnum } 1149bd603247SGregory Farnum 1150bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs, 1151bd603247SGregory Farnum const char *snapshot_name) 1152bd603247SGregory Farnum { 1153bd603247SGregory Farnum BDRVRBDState *s = bs->opaque; 1154bd603247SGregory Farnum 11559be38598SEduardo Habkost return rbd_snap_rollback(s->image, snapshot_name); 1156bd603247SGregory Farnum } 1157bd603247SGregory Farnum 1158ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs, 1159ad32e9c0SJosh Durgin QEMUSnapshotInfo **psn_tab) 1160f27aaf4bSChristian Brunner { 1161f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 1162f27aaf4bSChristian Brunner QEMUSnapshotInfo *sn_info, *sn_tab = NULL; 1163ad32e9c0SJosh Durgin int i, snap_count; 1164ad32e9c0SJosh Durgin rbd_snap_info_t *snaps; 1165ad32e9c0SJosh Durgin int max_snaps = RBD_MAX_SNAPS; 1166f27aaf4bSChristian Brunner 1167ad32e9c0SJosh Durgin do { 116802c4f26bSMarkus Armbruster snaps = g_new(rbd_snap_info_t, max_snaps); 1169ad32e9c0SJosh Durgin snap_count = rbd_snap_list(s->image, snaps, &max_snaps); 11709e6337d0SStefan Hajnoczi if (snap_count <= 0) { 11717267c094SAnthony Liguori g_free(snaps); 1172f27aaf4bSChristian Brunner } 1173ad32e9c0SJosh Durgin } while (snap_count == -ERANGE); 1174f27aaf4bSChristian Brunner 1175ad32e9c0SJosh Durgin if (snap_count <= 0) { 1176b9c53290SJosh Durgin goto done; 1177f27aaf4bSChristian Brunner } 1178f27aaf4bSChristian Brunner 11795839e53bSMarkus Armbruster sn_tab = g_new0(QEMUSnapshotInfo, snap_count); 1180f27aaf4bSChristian Brunner 1181ad32e9c0SJosh Durgin for (i = 0; i < snap_count; i++) { 1182ad32e9c0SJosh Durgin const char *snap_name = snaps[i].name; 1183f27aaf4bSChristian Brunner 1184f27aaf4bSChristian Brunner sn_info = sn_tab + i; 1185f27aaf4bSChristian Brunner pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name); 1186f27aaf4bSChristian Brunner pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name); 1187f27aaf4bSChristian Brunner 1188ad32e9c0SJosh Durgin sn_info->vm_state_size = snaps[i].size; 1189f27aaf4bSChristian Brunner sn_info->date_sec = 0; 1190f27aaf4bSChristian Brunner sn_info->date_nsec = 0; 1191f27aaf4bSChristian Brunner sn_info->vm_clock_nsec = 0; 1192f27aaf4bSChristian Brunner } 1193ad32e9c0SJosh Durgin rbd_snap_list_end(snaps); 11949e6337d0SStefan Hajnoczi g_free(snaps); 1195ad32e9c0SJosh Durgin 1196b9c53290SJosh Durgin done: 1197f27aaf4bSChristian Brunner *psn_tab = sn_tab; 1198f27aaf4bSChristian Brunner return snap_count; 1199f27aaf4bSChristian Brunner } 1200f27aaf4bSChristian Brunner 1201787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 12024da444a0SEric Blake static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs, 12034da444a0SEric Blake int64_t offset, 1204f5a5ca79SManos Pitsidianakis int bytes, 1205097310b5SMarkus Armbruster BlockCompletionFunc *cb, 1206787f3133SJosh Durgin void *opaque) 1207787f3133SJosh Durgin { 1208f5a5ca79SManos Pitsidianakis return rbd_start_aio(bs, offset, NULL, bytes, cb, opaque, 1209787f3133SJosh Durgin RBD_AIO_DISCARD); 1210787f3133SJosh Durgin } 1211787f3133SJosh Durgin #endif 1212787f3133SJosh Durgin 1213be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE 12142b148f39SPaolo Bonzini static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs, 1215be217884SAdam Crume Error **errp) 1216be217884SAdam Crume { 1217be217884SAdam Crume BDRVRBDState *s = bs->opaque; 1218be217884SAdam Crume int r = rbd_invalidate_cache(s->image); 1219be217884SAdam Crume if (r < 0) { 1220be217884SAdam Crume error_setg_errno(errp, -r, "Failed to invalidate the cache"); 1221be217884SAdam Crume } 1222be217884SAdam Crume } 1223be217884SAdam Crume #endif 1224be217884SAdam Crume 1225bd0cf596SChunyan Liu static QemuOptsList qemu_rbd_create_opts = { 1226bd0cf596SChunyan Liu .name = "rbd-create-opts", 1227bd0cf596SChunyan Liu .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head), 1228bd0cf596SChunyan Liu .desc = { 1229f27aaf4bSChristian Brunner { 1230f27aaf4bSChristian Brunner .name = BLOCK_OPT_SIZE, 1231bd0cf596SChunyan Liu .type = QEMU_OPT_SIZE, 1232f27aaf4bSChristian Brunner .help = "Virtual disk size" 1233f27aaf4bSChristian Brunner }, 1234f27aaf4bSChristian Brunner { 1235f27aaf4bSChristian Brunner .name = BLOCK_OPT_CLUSTER_SIZE, 1236bd0cf596SChunyan Liu .type = QEMU_OPT_SIZE, 1237f27aaf4bSChristian Brunner .help = "RBD object size" 1238f27aaf4bSChristian Brunner }, 123960390a21SDaniel P. Berrange { 124060390a21SDaniel P. Berrange .name = "password-secret", 124160390a21SDaniel P. Berrange .type = QEMU_OPT_STRING, 124260390a21SDaniel P. Berrange .help = "ID of secret providing the password", 124360390a21SDaniel P. Berrange }, 1244bd0cf596SChunyan Liu { /* end of list */ } 1245bd0cf596SChunyan Liu } 1246f27aaf4bSChristian Brunner }; 1247f27aaf4bSChristian Brunner 12482654267cSMax Reitz static const char *const qemu_rbd_strong_runtime_opts[] = { 12492654267cSMax Reitz "pool", 1250*7bae7c80SStefano Garzarella "namespace", 12512654267cSMax Reitz "image", 12522654267cSMax Reitz "conf", 12532654267cSMax Reitz "snapshot", 12542654267cSMax Reitz "user", 12552654267cSMax Reitz "server.", 12562654267cSMax Reitz "password-secret", 12572654267cSMax Reitz 12582654267cSMax Reitz NULL 12592654267cSMax Reitz }; 12602654267cSMax Reitz 1261f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = { 1262f27aaf4bSChristian Brunner .format_name = "rbd", 1263f27aaf4bSChristian Brunner .instance_size = sizeof(BDRVRBDState), 1264c7cacb3eSJeff Cody .bdrv_parse_filename = qemu_rbd_parse_filename, 1265e8e16d4bSEric Blake .bdrv_refresh_limits = qemu_rbd_refresh_limits, 1266ad32e9c0SJosh Durgin .bdrv_file_open = qemu_rbd_open, 1267ad32e9c0SJosh Durgin .bdrv_close = qemu_rbd_close, 126856e7cf8dSJeff Cody .bdrv_reopen_prepare = qemu_rbd_reopen_prepare, 12691bebea37SKevin Wolf .bdrv_co_create = qemu_rbd_co_create, 1270efc75e2aSStefan Hajnoczi .bdrv_co_create_opts = qemu_rbd_co_create_opts, 12713ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1, 1272ad32e9c0SJosh Durgin .bdrv_get_info = qemu_rbd_getinfo, 1273bd0cf596SChunyan Liu .create_opts = &qemu_rbd_create_opts, 1274ad32e9c0SJosh Durgin .bdrv_getlength = qemu_rbd_getlength, 1275061ca8a3SKevin Wolf .bdrv_co_truncate = qemu_rbd_co_truncate, 1276f27aaf4bSChristian Brunner .protocol_name = "rbd", 1277f27aaf4bSChristian Brunner 1278e8e16d4bSEric Blake .bdrv_aio_preadv = qemu_rbd_aio_preadv, 1279e8e16d4bSEric Blake .bdrv_aio_pwritev = qemu_rbd_aio_pwritev, 1280dc7588c1SJosh Durgin 1281dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH 1282dc7588c1SJosh Durgin .bdrv_aio_flush = qemu_rbd_aio_flush, 1283dc7588c1SJosh Durgin #else 1284c68b89acSKevin Wolf .bdrv_co_flush_to_disk = qemu_rbd_co_flush, 1285dc7588c1SJosh Durgin #endif 1286f27aaf4bSChristian Brunner 1287787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 12884da444a0SEric Blake .bdrv_aio_pdiscard = qemu_rbd_aio_pdiscard, 1289787f3133SJosh Durgin #endif 1290787f3133SJosh Durgin 1291ad32e9c0SJosh Durgin .bdrv_snapshot_create = qemu_rbd_snap_create, 1292bd603247SGregory Farnum .bdrv_snapshot_delete = qemu_rbd_snap_remove, 1293ad32e9c0SJosh Durgin .bdrv_snapshot_list = qemu_rbd_snap_list, 1294bd603247SGregory Farnum .bdrv_snapshot_goto = qemu_rbd_snap_rollback, 1295be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE 12962b148f39SPaolo Bonzini .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache, 1297be217884SAdam Crume #endif 12982654267cSMax Reitz 12992654267cSMax Reitz .strong_runtime_opts = qemu_rbd_strong_runtime_opts, 1300f27aaf4bSChristian Brunner }; 1301f27aaf4bSChristian Brunner 1302f27aaf4bSChristian Brunner static void bdrv_rbd_init(void) 1303f27aaf4bSChristian Brunner { 1304f27aaf4bSChristian Brunner bdrv_register(&bdrv_rbd); 1305f27aaf4bSChristian Brunner } 1306f27aaf4bSChristian Brunner 1307f27aaf4bSChristian Brunner block_init(bdrv_rbd_init); 1308