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" 19922a01a0SMarkus Armbruster #include "qemu/option.h" 20737e150eSPaolo Bonzini #include "block/block_int.h" 2160390a21SDaniel P. Berrange #include "crypto/secret.h" 22f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 23c7cacb3eSJeff Cody #include "qapi/qmp/qstring.h" 24452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h" 25e98c6961SEric Blake #include "qapi/qmp/qjson.h" 2647e6b297SMarkus Armbruster #include "qapi/qmp/qlist.h" 274bfb2741SKevin Wolf #include "qapi/qobject-input-visitor.h" 284bfb2741SKevin Wolf #include "qapi/qapi-visit-block-core.h" 29f27aaf4bSChristian Brunner 30f27aaf4bSChristian Brunner /* 31f27aaf4bSChristian Brunner * When specifying the image filename use: 32f27aaf4bSChristian Brunner * 33fab5cf59SJosh Durgin * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]] 34f27aaf4bSChristian Brunner * 359e1fbcdeSSage Weil * poolname must be the name of an existing rados pool. 36f27aaf4bSChristian Brunner * 379e1fbcdeSSage Weil * devicename is the name of the rbd image. 38f27aaf4bSChristian Brunner * 399e1fbcdeSSage Weil * Each option given is used to configure rados, and may be any valid 409e1fbcdeSSage Weil * Ceph option, "id", or "conf". 41fab5cf59SJosh Durgin * 429e1fbcdeSSage Weil * The "id" option indicates what user we should authenticate as to 439e1fbcdeSSage Weil * the Ceph cluster. If it is excluded we will use the Ceph default 449e1fbcdeSSage Weil * (normally 'admin'). 45f27aaf4bSChristian Brunner * 469e1fbcdeSSage Weil * The "conf" option specifies a Ceph configuration file to read. If 479e1fbcdeSSage Weil * it is not specified, we will read from the default Ceph locations 489e1fbcdeSSage Weil * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration 499e1fbcdeSSage Weil * file, specify conf=/dev/null. 50f27aaf4bSChristian Brunner * 519e1fbcdeSSage Weil * Configuration values containing :, @, or = can be escaped with a 529e1fbcdeSSage Weil * leading "\". 53f27aaf4bSChristian Brunner */ 54f27aaf4bSChristian Brunner 55787f3133SJosh Durgin /* rbd_aio_discard added in 0.1.2 */ 56787f3133SJosh Durgin #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2) 57787f3133SJosh Durgin #define LIBRBD_SUPPORTS_DISCARD 58787f3133SJosh Durgin #else 59787f3133SJosh Durgin #undef LIBRBD_SUPPORTS_DISCARD 60787f3133SJosh Durgin #endif 61787f3133SJosh Durgin 62f27aaf4bSChristian Brunner #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER) 63f27aaf4bSChristian Brunner 64ad32e9c0SJosh Durgin #define RBD_MAX_SNAPS 100 65ad32e9c0SJosh Durgin 661d393bdeStianqing /* The LIBRBD_SUPPORTS_IOVEC is defined in librbd.h */ 671d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC 681d393bdeStianqing #define LIBRBD_USE_IOVEC 1 691d393bdeStianqing #else 701d393bdeStianqing #define LIBRBD_USE_IOVEC 0 711d393bdeStianqing #endif 721d393bdeStianqing 73787f3133SJosh Durgin typedef enum { 74787f3133SJosh Durgin RBD_AIO_READ, 75787f3133SJosh Durgin RBD_AIO_WRITE, 76dc7588c1SJosh Durgin RBD_AIO_DISCARD, 77dc7588c1SJosh Durgin RBD_AIO_FLUSH 78787f3133SJosh Durgin } RBDAIOCmd; 79787f3133SJosh Durgin 80f27aaf4bSChristian Brunner typedef struct RBDAIOCB { 817c84b1b8SMarkus Armbruster BlockAIOCB common; 8208448d51SStefan Priebe int64_t ret; 83f27aaf4bSChristian Brunner QEMUIOVector *qiov; 84f27aaf4bSChristian Brunner char *bounce; 85787f3133SJosh Durgin RBDAIOCmd cmd; 86f27aaf4bSChristian Brunner int error; 87f27aaf4bSChristian Brunner struct BDRVRBDState *s; 88f27aaf4bSChristian Brunner } RBDAIOCB; 89f27aaf4bSChristian Brunner 90f27aaf4bSChristian Brunner typedef struct RADOSCB { 91f27aaf4bSChristian Brunner RBDAIOCB *acb; 92f27aaf4bSChristian Brunner struct BDRVRBDState *s; 93ad32e9c0SJosh Durgin int64_t size; 94f27aaf4bSChristian Brunner char *buf; 9508448d51SStefan Priebe int64_t ret; 96f27aaf4bSChristian Brunner } RADOSCB; 97f27aaf4bSChristian Brunner 98f27aaf4bSChristian Brunner typedef struct BDRVRBDState { 99ad32e9c0SJosh Durgin rados_t cluster; 100ad32e9c0SJosh Durgin rados_ioctx_t io_ctx; 101ad32e9c0SJosh Durgin rbd_image_t image; 10280b61a27SJeff Cody char *image_name; 103ad32e9c0SJosh Durgin char *snap; 104f27aaf4bSChristian Brunner } BDRVRBDState; 105f27aaf4bSChristian Brunner 106aa045c2dSKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx, 107aa045c2dSKevin Wolf BlockdevOptionsRbd *opts, bool cache, 108aa045c2dSKevin Wolf const char *keypairs, const char *secretid, 109aa045c2dSKevin Wolf Error **errp); 110aa045c2dSKevin Wolf 111730b00bbSMarkus Armbruster static char *qemu_rbd_next_tok(char *src, char delim, char **p) 112f27aaf4bSChristian Brunner { 113f27aaf4bSChristian Brunner char *end; 114f27aaf4bSChristian Brunner 115f27aaf4bSChristian Brunner *p = NULL; 116f27aaf4bSChristian Brunner 11716a06b24SSage Weil for (end = src; *end; ++end) { 11816a06b24SSage Weil if (*end == delim) { 11916a06b24SSage Weil break; 12016a06b24SSage Weil } 12116a06b24SSage Weil if (*end == '\\' && end[1] != '\0') { 12216a06b24SSage Weil end++; 12316a06b24SSage Weil } 12416a06b24SSage Weil } 12516a06b24SSage Weil if (*end == delim) { 126f27aaf4bSChristian Brunner *p = end + 1; 127f27aaf4bSChristian Brunner *end = '\0'; 128f27aaf4bSChristian Brunner } 1297830f909SJeff Cody return src; 130f27aaf4bSChristian Brunner } 131f27aaf4bSChristian Brunner 13216a06b24SSage Weil static void qemu_rbd_unescape(char *src) 13316a06b24SSage Weil { 13416a06b24SSage Weil char *p; 13516a06b24SSage Weil 13616a06b24SSage Weil for (p = src; *src; ++src, ++p) { 13716a06b24SSage Weil if (*src == '\\' && src[1] != '\0') { 13816a06b24SSage Weil src++; 13916a06b24SSage Weil } 14016a06b24SSage Weil *p = *src; 14116a06b24SSage Weil } 14216a06b24SSage Weil *p = '\0'; 14316a06b24SSage Weil } 14416a06b24SSage Weil 145c7cacb3eSJeff Cody static void qemu_rbd_parse_filename(const char *filename, QDict *options, 146d61563b2SMarkus Armbruster Error **errp) 147f27aaf4bSChristian Brunner { 148f27aaf4bSChristian Brunner const char *start; 149e98c6961SEric Blake char *p, *buf; 150e98c6961SEric Blake QList *keypairs = NULL; 1517830f909SJeff Cody char *found_str; 152f27aaf4bSChristian Brunner 153f27aaf4bSChristian Brunner if (!strstart(filename, "rbd:", &start)) { 154d61563b2SMarkus Armbruster error_setg(errp, "File name must start with 'rbd:'"); 155c7cacb3eSJeff Cody return; 156f27aaf4bSChristian Brunner } 157f27aaf4bSChristian Brunner 1587267c094SAnthony Liguori buf = g_strdup(start); 159f27aaf4bSChristian Brunner p = buf; 160f27aaf4bSChristian Brunner 161730b00bbSMarkus Armbruster found_str = qemu_rbd_next_tok(p, '/', &p); 1627830f909SJeff Cody if (!p) { 1637830f909SJeff Cody error_setg(errp, "Pool name is required"); 1647830f909SJeff Cody goto done; 1657830f909SJeff Cody } 1667830f909SJeff Cody qemu_rbd_unescape(found_str); 16746f5ac20SEric Blake qdict_put_str(options, "pool", found_str); 168fab5cf59SJosh Durgin 169fab5cf59SJosh Durgin if (strchr(p, '@')) { 170730b00bbSMarkus Armbruster found_str = qemu_rbd_next_tok(p, '@', &p); 1717830f909SJeff Cody qemu_rbd_unescape(found_str); 17246f5ac20SEric Blake qdict_put_str(options, "image", found_str); 1737830f909SJeff Cody 174730b00bbSMarkus Armbruster found_str = qemu_rbd_next_tok(p, ':', &p); 1757830f909SJeff Cody qemu_rbd_unescape(found_str); 17646f5ac20SEric Blake qdict_put_str(options, "snapshot", found_str); 1777830f909SJeff Cody } else { 178730b00bbSMarkus Armbruster found_str = qemu_rbd_next_tok(p, ':', &p); 1797830f909SJeff Cody qemu_rbd_unescape(found_str); 18046f5ac20SEric Blake qdict_put_str(options, "image", found_str); 1817830f909SJeff Cody } 1827830f909SJeff Cody if (!p) { 183f27aaf4bSChristian Brunner goto done; 184f27aaf4bSChristian Brunner } 185f27aaf4bSChristian Brunner 186c7cacb3eSJeff Cody /* The following are essentially all key/value pairs, and we treat 187c7cacb3eSJeff Cody * 'id' and 'conf' a bit special. Key/value pairs may be in any order. */ 188c7cacb3eSJeff Cody while (p) { 189c7cacb3eSJeff Cody char *name, *value; 190730b00bbSMarkus Armbruster name = qemu_rbd_next_tok(p, '=', &p); 191c7cacb3eSJeff Cody if (!p) { 192c7cacb3eSJeff Cody error_setg(errp, "conf option %s has no value", name); 193c7cacb3eSJeff Cody break; 194c7cacb3eSJeff Cody } 195c7cacb3eSJeff Cody 196c7cacb3eSJeff Cody qemu_rbd_unescape(name); 197c7cacb3eSJeff Cody 198730b00bbSMarkus Armbruster value = qemu_rbd_next_tok(p, ':', &p); 199c7cacb3eSJeff Cody qemu_rbd_unescape(value); 200c7cacb3eSJeff Cody 201c7cacb3eSJeff Cody if (!strcmp(name, "conf")) { 20246f5ac20SEric Blake qdict_put_str(options, "conf", value); 203c7cacb3eSJeff Cody } else if (!strcmp(name, "id")) { 20446f5ac20SEric Blake qdict_put_str(options, "user", value); 205c7cacb3eSJeff Cody } else { 206e98c6961SEric Blake /* 207e98c6961SEric Blake * We pass these internally to qemu_rbd_set_keypairs(), so 208e98c6961SEric Blake * we can get away with the simpler list of [ "key1", 209e98c6961SEric Blake * "value1", "key2", "value2" ] rather than a raw dict 210e98c6961SEric Blake * { "key1": "value1", "key2": "value2" } where we can't 211e98c6961SEric Blake * guarantee order, or even a more correct but complex 212e98c6961SEric Blake * [ { "key1": "value1" }, { "key2": "value2" } ] 213e98c6961SEric Blake */ 214e98c6961SEric Blake if (!keypairs) { 215e98c6961SEric Blake keypairs = qlist_new(); 216c7cacb3eSJeff Cody } 21746f5ac20SEric Blake qlist_append_str(keypairs, name); 21846f5ac20SEric Blake qlist_append_str(keypairs, value); 219c7cacb3eSJeff Cody } 220c7cacb3eSJeff Cody } 221c7cacb3eSJeff Cody 222e98c6961SEric Blake if (keypairs) { 223e98c6961SEric Blake qdict_put(options, "=keyvalue-pairs", 224e98c6961SEric Blake qobject_to_json(QOBJECT(keypairs))); 225c7cacb3eSJeff Cody } 226c7cacb3eSJeff Cody 227f27aaf4bSChristian Brunner done: 2287267c094SAnthony Liguori g_free(buf); 229e98c6961SEric Blake QDECREF(keypairs); 230c7cacb3eSJeff Cody return; 2317c7e9df0SSage Weil } 2327c7e9df0SSage Weil 23360390a21SDaniel P. Berrange 23460390a21SDaniel P. Berrange static int qemu_rbd_set_auth(rados_t cluster, const char *secretid, 23560390a21SDaniel P. Berrange Error **errp) 23660390a21SDaniel P. Berrange { 23760390a21SDaniel P. Berrange if (secretid == 0) { 23860390a21SDaniel P. Berrange return 0; 23960390a21SDaniel P. Berrange } 24060390a21SDaniel P. Berrange 24160390a21SDaniel P. Berrange gchar *secret = qcrypto_secret_lookup_as_base64(secretid, 24260390a21SDaniel P. Berrange errp); 24360390a21SDaniel P. Berrange if (!secret) { 24460390a21SDaniel P. Berrange return -1; 24560390a21SDaniel P. Berrange } 24660390a21SDaniel P. Berrange 24760390a21SDaniel P. Berrange rados_conf_set(cluster, "key", secret); 24860390a21SDaniel P. Berrange g_free(secret); 24960390a21SDaniel P. Berrange 25060390a21SDaniel P. Berrange return 0; 25160390a21SDaniel P. Berrange } 25260390a21SDaniel P. Berrange 253e98c6961SEric Blake static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json, 254e34d8f29SJosh Durgin Error **errp) 255fab5cf59SJosh Durgin { 256e98c6961SEric Blake QList *keypairs; 257e98c6961SEric Blake QString *name; 258e98c6961SEric Blake QString *value; 259e98c6961SEric Blake const char *key; 260e98c6961SEric Blake size_t remaining; 261fab5cf59SJosh Durgin int ret = 0; 262fab5cf59SJosh Durgin 263e98c6961SEric Blake if (!keypairs_json) { 264e98c6961SEric Blake return ret; 265fab5cf59SJosh Durgin } 2667dc847ebSMax Reitz keypairs = qobject_to(QList, 2677dc847ebSMax Reitz qobject_from_json(keypairs_json, &error_abort)); 268e98c6961SEric Blake remaining = qlist_size(keypairs) / 2; 269e98c6961SEric Blake assert(remaining); 270fab5cf59SJosh Durgin 271e98c6961SEric Blake while (remaining--) { 2727dc847ebSMax Reitz name = qobject_to(QString, qlist_pop(keypairs)); 2737dc847ebSMax Reitz value = qobject_to(QString, qlist_pop(keypairs)); 274e98c6961SEric Blake assert(name && value); 275e98c6961SEric Blake key = qstring_get_str(name); 276fab5cf59SJosh Durgin 277e98c6961SEric Blake ret = rados_conf_set(cluster, key, qstring_get_str(value)); 278e98c6961SEric Blake QDECREF(value); 279fab5cf59SJosh Durgin if (ret < 0) { 280e98c6961SEric Blake error_setg_errno(errp, -ret, "invalid conf option %s", key); 28171c87815SKevin Wolf QDECREF(name); 282fab5cf59SJosh Durgin ret = -EINVAL; 283fab5cf59SJosh Durgin break; 284fab5cf59SJosh Durgin } 28571c87815SKevin Wolf QDECREF(name); 286fab5cf59SJosh Durgin } 287fab5cf59SJosh Durgin 288e98c6961SEric Blake QDECREF(keypairs); 289fab5cf59SJosh Durgin return ret; 290fab5cf59SJosh Durgin } 291fab5cf59SJosh Durgin 2921d393bdeStianqing static void qemu_rbd_memset(RADOSCB *rcb, int64_t offs) 2931d393bdeStianqing { 2941d393bdeStianqing if (LIBRBD_USE_IOVEC) { 2951d393bdeStianqing RBDAIOCB *acb = rcb->acb; 2961d393bdeStianqing iov_memset(acb->qiov->iov, acb->qiov->niov, offs, 0, 2971d393bdeStianqing acb->qiov->size - offs); 2981d393bdeStianqing } else { 2991d393bdeStianqing memset(rcb->buf + offs, 0, rcb->size - offs); 3001d393bdeStianqing } 3011d393bdeStianqing } 3021d393bdeStianqing 3030f9d252dSJeff Cody static QemuOptsList runtime_opts = { 3040f9d252dSJeff Cody .name = "rbd", 3050f9d252dSJeff Cody .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), 3060f9d252dSJeff Cody .desc = { 3070f9d252dSJeff Cody { 3080f9d252dSJeff Cody .name = "pool", 3090f9d252dSJeff Cody .type = QEMU_OPT_STRING, 3100f9d252dSJeff Cody .help = "Rados pool name", 3110f9d252dSJeff Cody }, 3120f9d252dSJeff Cody { 3130f9d252dSJeff Cody .name = "image", 3140f9d252dSJeff Cody .type = QEMU_OPT_STRING, 3150f9d252dSJeff Cody .help = "Image name in the pool", 3160f9d252dSJeff Cody }, 3170f9d252dSJeff Cody { 318cbf036b4SMarkus Armbruster .name = "conf", 319cbf036b4SMarkus Armbruster .type = QEMU_OPT_STRING, 320cbf036b4SMarkus Armbruster .help = "Rados config file location", 321cbf036b4SMarkus Armbruster }, 322cbf036b4SMarkus Armbruster { 3230f9d252dSJeff Cody .name = "snapshot", 3240f9d252dSJeff Cody .type = QEMU_OPT_STRING, 3250f9d252dSJeff Cody .help = "Ceph snapshot name", 3260f9d252dSJeff Cody }, 3270f9d252dSJeff Cody { 3280f9d252dSJeff Cody /* maps to 'id' in rados_create() */ 3290f9d252dSJeff Cody .name = "user", 3300f9d252dSJeff Cody .type = QEMU_OPT_STRING, 3310f9d252dSJeff Cody .help = "Rados id name", 3320f9d252dSJeff Cody }, 333cbf036b4SMarkus Armbruster /* 3342836284dSMarkus Armbruster * server.* extracted manually, see qemu_rbd_mon_host() 335cbf036b4SMarkus Armbruster */ 3360f9d252dSJeff Cody { /* end of list */ } 3370f9d252dSJeff Cody }, 3380f9d252dSJeff Cody }; 3390f9d252dSJeff Cody 3401bebea37SKevin Wolf /* FIXME Deprecate and remove keypairs or make it available in QMP. 3411bebea37SKevin Wolf * password_secret should eventually be configurable in opts->location. Support 3421bebea37SKevin Wolf * for it in .bdrv_open will make it work here as well. */ 3431bebea37SKevin Wolf static int qemu_rbd_do_create(BlockdevCreateOptions *options, 3441bebea37SKevin Wolf const char *keypairs, const char *password_secret, 3451bebea37SKevin Wolf Error **errp) 3461bebea37SKevin Wolf { 3471bebea37SKevin Wolf BlockdevCreateOptionsRbd *opts = &options->u.rbd; 3481bebea37SKevin Wolf rados_t cluster; 3491bebea37SKevin Wolf rados_ioctx_t io_ctx; 3501bebea37SKevin Wolf int obj_order = 0; 3511bebea37SKevin Wolf int ret; 3521bebea37SKevin Wolf 3531bebea37SKevin Wolf assert(options->driver == BLOCKDEV_DRIVER_RBD); 3541bebea37SKevin Wolf if (opts->location->has_snapshot) { 3551bebea37SKevin Wolf error_setg(errp, "Can't use snapshot name for image creation"); 3561bebea37SKevin Wolf return -EINVAL; 3571bebea37SKevin Wolf } 3581bebea37SKevin Wolf 3591bebea37SKevin Wolf if (opts->has_cluster_size) { 3601bebea37SKevin Wolf int64_t objsize = opts->cluster_size; 3611bebea37SKevin Wolf if ((objsize - 1) & objsize) { /* not a power of 2? */ 3621bebea37SKevin Wolf error_setg(errp, "obj size needs to be power of 2"); 3631bebea37SKevin Wolf return -EINVAL; 3641bebea37SKevin Wolf } 3651bebea37SKevin Wolf if (objsize < 4096) { 3661bebea37SKevin Wolf error_setg(errp, "obj size too small"); 3671bebea37SKevin Wolf return -EINVAL; 3681bebea37SKevin Wolf } 3691bebea37SKevin Wolf obj_order = ctz32(objsize); 3701bebea37SKevin Wolf } 3711bebea37SKevin Wolf 372aa045c2dSKevin Wolf ret = qemu_rbd_connect(&cluster, &io_ctx, opts->location, false, keypairs, 373aa045c2dSKevin Wolf password_secret, errp); 3741bebea37SKevin Wolf if (ret < 0) { 3751bebea37SKevin Wolf return ret; 3761bebea37SKevin Wolf } 3771bebea37SKevin Wolf 3781bebea37SKevin Wolf ret = rbd_create(io_ctx, opts->location->image, opts->size, &obj_order); 3791bebea37SKevin Wolf if (ret < 0) { 3801bebea37SKevin Wolf error_setg_errno(errp, -ret, "error rbd create"); 381aa045c2dSKevin Wolf goto out; 3821bebea37SKevin Wolf } 3831bebea37SKevin Wolf 3841bebea37SKevin Wolf ret = 0; 385aa045c2dSKevin Wolf out: 386aa045c2dSKevin Wolf rados_ioctx_destroy(io_ctx); 3871bebea37SKevin Wolf rados_shutdown(cluster); 3881bebea37SKevin Wolf return ret; 3891bebea37SKevin Wolf } 3901bebea37SKevin Wolf 3911bebea37SKevin Wolf static int qemu_rbd_co_create(BlockdevCreateOptions *options, Error **errp) 3921bebea37SKevin Wolf { 3931bebea37SKevin Wolf return qemu_rbd_do_create(options, NULL, NULL, errp); 3941bebea37SKevin Wolf } 3951bebea37SKevin Wolf 396efc75e2aSStefan Hajnoczi static int coroutine_fn qemu_rbd_co_create_opts(const char *filename, 397efc75e2aSStefan Hajnoczi QemuOpts *opts, 398efc75e2aSStefan Hajnoczi Error **errp) 399f27aaf4bSChristian Brunner { 4001bebea37SKevin Wolf BlockdevCreateOptions *create_options; 4011bebea37SKevin Wolf BlockdevCreateOptionsRbd *rbd_opts; 4021bebea37SKevin Wolf BlockdevOptionsRbd *loc; 403d61563b2SMarkus Armbruster Error *local_err = NULL; 4041bebea37SKevin Wolf const char *keypairs, *password_secret; 405c7cacb3eSJeff Cody QDict *options = NULL; 406c7cacb3eSJeff Cody int ret = 0; 407f27aaf4bSChristian Brunner 4081bebea37SKevin Wolf create_options = g_new0(BlockdevCreateOptions, 1); 4091bebea37SKevin Wolf create_options->driver = BLOCKDEV_DRIVER_RBD; 4101bebea37SKevin Wolf rbd_opts = &create_options->u.rbd; 4111bebea37SKevin Wolf 4121bebea37SKevin Wolf rbd_opts->location = g_new0(BlockdevOptionsRbd, 1); 4131bebea37SKevin Wolf 4141bebea37SKevin Wolf password_secret = qemu_opt_get(opts, "password-secret"); 41560390a21SDaniel P. Berrange 416f27aaf4bSChristian Brunner /* Read out options */ 4171bebea37SKevin Wolf rbd_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 418c2eb918eSHu Tao BDRV_SECTOR_SIZE); 4191bebea37SKevin Wolf rbd_opts->cluster_size = qemu_opt_get_size_del(opts, 4201bebea37SKevin Wolf BLOCK_OPT_CLUSTER_SIZE, 0); 4211bebea37SKevin Wolf rbd_opts->has_cluster_size = (rbd_opts->cluster_size != 0); 422f27aaf4bSChristian Brunner 423c7cacb3eSJeff Cody options = qdict_new(); 424c7cacb3eSJeff Cody qemu_rbd_parse_filename(filename, options, &local_err); 425c7cacb3eSJeff Cody if (local_err) { 426c7cacb3eSJeff Cody ret = -EINVAL; 427c7cacb3eSJeff Cody error_propagate(errp, local_err); 428c7cacb3eSJeff Cody goto exit; 429c7cacb3eSJeff Cody } 430c7cacb3eSJeff Cody 431129c7d1cSMarkus Armbruster /* 432129c7d1cSMarkus Armbruster * Caution: while qdict_get_try_str() is fine, getting non-string 433129c7d1cSMarkus Armbruster * types would require more care. When @options come from -blockdev 434129c7d1cSMarkus Armbruster * or blockdev_add, its members are typed according to the QAPI 435129c7d1cSMarkus Armbruster * schema, but when they come from -drive, they're all QString. 436129c7d1cSMarkus Armbruster */ 4371bebea37SKevin Wolf loc = rbd_opts->location; 4381bebea37SKevin Wolf loc->pool = g_strdup(qdict_get_try_str(options, "pool")); 4391bebea37SKevin Wolf loc->conf = g_strdup(qdict_get_try_str(options, "conf")); 4401bebea37SKevin Wolf loc->has_conf = !!loc->conf; 4411bebea37SKevin Wolf loc->user = g_strdup(qdict_get_try_str(options, "user")); 4421bebea37SKevin Wolf loc->has_user = !!loc->user; 4431bebea37SKevin Wolf loc->image = g_strdup(qdict_get_try_str(options, "image")); 44407846397SMarkus Armbruster keypairs = qdict_get_try_str(options, "=keyvalue-pairs"); 445c7cacb3eSJeff Cody 4461bebea37SKevin Wolf ret = qemu_rbd_do_create(create_options, keypairs, password_secret, errp); 44787cd3d20SVikhyat Umrao if (ret < 0) { 448c7cacb3eSJeff Cody goto exit; 449f27aaf4bSChristian Brunner } 450f27aaf4bSChristian Brunner 451c7cacb3eSJeff Cody exit: 452c7cacb3eSJeff Cody QDECREF(options); 4531bebea37SKevin Wolf qapi_free_BlockdevCreateOptions(create_options); 454f27aaf4bSChristian Brunner return ret; 455f27aaf4bSChristian Brunner } 456f27aaf4bSChristian Brunner 457f27aaf4bSChristian Brunner /* 458e04fb07fSStefan Hajnoczi * This aio completion is being called from rbd_finish_bh() and runs in qemu 459e04fb07fSStefan Hajnoczi * BH context. 460f27aaf4bSChristian Brunner */ 461ad32e9c0SJosh Durgin static void qemu_rbd_complete_aio(RADOSCB *rcb) 462f27aaf4bSChristian Brunner { 463f27aaf4bSChristian Brunner RBDAIOCB *acb = rcb->acb; 464f27aaf4bSChristian Brunner int64_t r; 465f27aaf4bSChristian Brunner 466f27aaf4bSChristian Brunner r = rcb->ret; 467f27aaf4bSChristian Brunner 468dc7588c1SJosh Durgin if (acb->cmd != RBD_AIO_READ) { 469f27aaf4bSChristian Brunner if (r < 0) { 470f27aaf4bSChristian Brunner acb->ret = r; 471f27aaf4bSChristian Brunner acb->error = 1; 472f27aaf4bSChristian Brunner } else if (!acb->error) { 473ad32e9c0SJosh Durgin acb->ret = rcb->size; 474f27aaf4bSChristian Brunner } 475f27aaf4bSChristian Brunner } else { 476ad32e9c0SJosh Durgin if (r < 0) { 4771d393bdeStianqing qemu_rbd_memset(rcb, 0); 478f27aaf4bSChristian Brunner acb->ret = r; 479f27aaf4bSChristian Brunner acb->error = 1; 480ad32e9c0SJosh Durgin } else if (r < rcb->size) { 4811d393bdeStianqing qemu_rbd_memset(rcb, r); 482f27aaf4bSChristian Brunner if (!acb->error) { 483ad32e9c0SJosh Durgin acb->ret = rcb->size; 484f27aaf4bSChristian Brunner } 485f27aaf4bSChristian Brunner } else if (!acb->error) { 486ad32e9c0SJosh Durgin acb->ret = r; 487f27aaf4bSChristian Brunner } 488f27aaf4bSChristian Brunner } 489e04fb07fSStefan Hajnoczi 4907267c094SAnthony Liguori g_free(rcb); 491e04fb07fSStefan Hajnoczi 4921d393bdeStianqing if (!LIBRBD_USE_IOVEC) { 493e04fb07fSStefan Hajnoczi if (acb->cmd == RBD_AIO_READ) { 494e04fb07fSStefan Hajnoczi qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); 495f27aaf4bSChristian Brunner } 496e04fb07fSStefan Hajnoczi qemu_vfree(acb->bounce); 4971d393bdeStianqing } 4981d393bdeStianqing 499e04fb07fSStefan Hajnoczi acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret)); 500f27aaf4bSChristian Brunner 5018007429aSFam Zheng qemu_aio_unref(acb); 502f27aaf4bSChristian Brunner } 503f27aaf4bSChristian Brunner 5044bfb2741SKevin Wolf static char *qemu_rbd_mon_host(BlockdevOptionsRbd *opts, Error **errp) 5050a55679bSJeff Cody { 5064bfb2741SKevin Wolf const char **vals; 5072836284dSMarkus Armbruster const char *host, *port; 5082836284dSMarkus Armbruster char *rados_str; 5094bfb2741SKevin Wolf InetSocketAddressBaseList *p; 5104bfb2741SKevin Wolf int i, cnt; 5110a55679bSJeff Cody 5124bfb2741SKevin Wolf if (!opts->has_server) { 5134bfb2741SKevin Wolf return NULL; 5140a55679bSJeff Cody } 5154bfb2741SKevin Wolf 5164bfb2741SKevin Wolf for (cnt = 0, p = opts->server; p; p = p->next) { 5174bfb2741SKevin Wolf cnt++; 5180a55679bSJeff Cody } 5190a55679bSJeff Cody 5204bfb2741SKevin Wolf vals = g_new(const char *, cnt + 1); 5214bfb2741SKevin Wolf 5224bfb2741SKevin Wolf for (i = 0, p = opts->server; p; p = p->next, i++) { 5234bfb2741SKevin Wolf host = p->value->host; 5244bfb2741SKevin Wolf port = p->value->port; 5254bfb2741SKevin Wolf 5260a55679bSJeff Cody if (strchr(host, ':')) { 5274bfb2741SKevin Wolf vals[i] = g_strdup_printf("[%s]:%s", host, port); 5280a55679bSJeff Cody } else { 5294bfb2741SKevin Wolf vals[i] = g_strdup_printf("%s:%s", host, port); 5300a55679bSJeff Cody } 5310a55679bSJeff Cody } 5322836284dSMarkus Armbruster vals[i] = NULL; 5330a55679bSJeff Cody 5342836284dSMarkus Armbruster rados_str = i ? g_strjoinv(";", (char **)vals) : NULL; 5352836284dSMarkus Armbruster g_strfreev((char **)vals); 5360a55679bSJeff Cody return rados_str; 5370a55679bSJeff Cody } 5380a55679bSJeff Cody 5393d9136f9SKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx, 5404bfb2741SKevin Wolf BlockdevOptionsRbd *opts, bool cache, 5414ff45049SKevin Wolf const char *keypairs, const char *secretid, 5424ff45049SKevin Wolf Error **errp) 543f27aaf4bSChristian Brunner { 5440a55679bSJeff Cody char *mon_host = NULL; 5453d9136f9SKevin Wolf Error *local_err = NULL; 546f27aaf4bSChristian Brunner int r; 547f27aaf4bSChristian Brunner 5484bfb2741SKevin Wolf mon_host = qemu_rbd_mon_host(opts, &local_err); 54984d18f06SMarkus Armbruster if (local_err) { 550d61563b2SMarkus Armbruster error_propagate(errp, local_err); 5512836284dSMarkus Armbruster r = -EINVAL; 5522836284dSMarkus Armbruster goto failed_opts; 553a9ccedc3SKevin Wolf } 554a9ccedc3SKevin Wolf 5554bfb2741SKevin Wolf r = rados_create(cluster, opts->user); 556ad32e9c0SJosh Durgin if (r < 0) { 55787cd3d20SVikhyat Umrao error_setg_errno(errp, -r, "error initializing"); 558c3ca988dSKevin Wolf goto failed_opts; 559f27aaf4bSChristian Brunner } 560f27aaf4bSChristian Brunner 561c7cacb3eSJeff Cody /* try default location when conf=NULL, but ignore failure */ 5624bfb2741SKevin Wolf r = rados_conf_read_file(*cluster, opts->conf); 5634bfb2741SKevin Wolf if (opts->has_conf && r < 0) { 5644bfb2741SKevin Wolf error_setg_errno(errp, -r, "error reading conf file %s", opts->conf); 565e34d8f29SJosh Durgin goto failed_shutdown; 566e34d8f29SJosh Durgin } 56799a3c89dSJosh Durgin 5683d9136f9SKevin Wolf r = qemu_rbd_set_keypairs(*cluster, keypairs, errp); 56999a3c89dSJosh Durgin if (r < 0) { 57099a3c89dSJosh Durgin goto failed_shutdown; 57199a3c89dSJosh Durgin } 57299a3c89dSJosh Durgin 5730a55679bSJeff Cody if (mon_host) { 5743d9136f9SKevin Wolf r = rados_conf_set(*cluster, "mon_host", mon_host); 5750a55679bSJeff Cody if (r < 0) { 5760a55679bSJeff Cody goto failed_shutdown; 5770a55679bSJeff Cody } 5780a55679bSJeff Cody } 5790a55679bSJeff Cody 5803d9136f9SKevin Wolf if (qemu_rbd_set_auth(*cluster, secretid, errp) < 0) { 58160390a21SDaniel P. Berrange r = -EIO; 58260390a21SDaniel P. Berrange goto failed_shutdown; 58360390a21SDaniel P. Berrange } 58460390a21SDaniel P. Berrange 585b11f38fcSJosh Durgin /* 586b11f38fcSJosh Durgin * Fallback to more conservative semantics if setting cache 587b11f38fcSJosh Durgin * options fails. Ignore errors from setting rbd_cache because the 588b11f38fcSJosh Durgin * only possible error is that the option does not exist, and 589b11f38fcSJosh Durgin * librbd defaults to no caching. If write through caching cannot 590b11f38fcSJosh Durgin * be set up, fall back to no caching. 591b11f38fcSJosh Durgin */ 5923d9136f9SKevin Wolf if (cache) { 5933d9136f9SKevin Wolf rados_conf_set(*cluster, "rbd_cache", "true"); 594b11f38fcSJosh Durgin } else { 5953d9136f9SKevin Wolf rados_conf_set(*cluster, "rbd_cache", "false"); 596b11f38fcSJosh Durgin } 597b11f38fcSJosh Durgin 5983d9136f9SKevin Wolf r = rados_connect(*cluster); 599ad32e9c0SJosh Durgin if (r < 0) { 60087cd3d20SVikhyat Umrao error_setg_errno(errp, -r, "error connecting"); 601eb93d5d9SSage Weil goto failed_shutdown; 602ad32e9c0SJosh Durgin } 603ad32e9c0SJosh Durgin 6044bfb2741SKevin Wolf r = rados_ioctx_create(*cluster, opts->pool, io_ctx); 605ad32e9c0SJosh Durgin if (r < 0) { 6064bfb2741SKevin Wolf error_setg_errno(errp, -r, "error opening pool %s", opts->pool); 607eb93d5d9SSage Weil goto failed_shutdown; 608ad32e9c0SJosh Durgin } 609ad32e9c0SJosh Durgin 6103d9136f9SKevin Wolf return 0; 6113d9136f9SKevin Wolf 6123d9136f9SKevin Wolf failed_shutdown: 6133d9136f9SKevin Wolf rados_shutdown(*cluster); 6143d9136f9SKevin Wolf failed_opts: 6153d9136f9SKevin Wolf g_free(mon_host); 6163d9136f9SKevin Wolf return r; 6173d9136f9SKevin Wolf } 6183d9136f9SKevin Wolf 6193d9136f9SKevin Wolf static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags, 6203d9136f9SKevin Wolf Error **errp) 6213d9136f9SKevin Wolf { 6223d9136f9SKevin Wolf BDRVRBDState *s = bs->opaque; 6234bfb2741SKevin Wolf BlockdevOptionsRbd *opts = NULL; 6244bfb2741SKevin Wolf Visitor *v; 6254bfb2741SKevin Wolf QObject *crumpled = NULL; 626*bfb15b4bSJeff Cody const QDictEntry *e; 6273d9136f9SKevin Wolf Error *local_err = NULL; 6283d9136f9SKevin Wolf const char *filename; 6294ff45049SKevin Wolf char *keypairs, *secretid; 6303d9136f9SKevin Wolf int r; 6313d9136f9SKevin Wolf 6323d9136f9SKevin Wolf /* If we are given a filename, parse the filename, with precedence given to 6333d9136f9SKevin Wolf * filename encoded options */ 6343d9136f9SKevin Wolf filename = qdict_get_try_str(options, "filename"); 6353d9136f9SKevin Wolf if (filename) { 6363d9136f9SKevin Wolf warn_report("'filename' option specified. " 6373d9136f9SKevin Wolf "This is an unsupported option, and may be deprecated " 6383d9136f9SKevin Wolf "in the future"); 6393d9136f9SKevin Wolf qemu_rbd_parse_filename(filename, options, &local_err); 6404ff45049SKevin Wolf qdict_del(options, "filename"); 6413d9136f9SKevin Wolf if (local_err) { 6423d9136f9SKevin Wolf error_propagate(errp, local_err); 6433d9136f9SKevin Wolf return -EINVAL; 6443d9136f9SKevin Wolf } 6453d9136f9SKevin Wolf } 6463d9136f9SKevin Wolf 6474ff45049SKevin Wolf keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs")); 6484ff45049SKevin Wolf if (keypairs) { 6494ff45049SKevin Wolf qdict_del(options, "=keyvalue-pairs"); 6504ff45049SKevin Wolf } 6514ff45049SKevin Wolf 6524ff45049SKevin Wolf secretid = g_strdup(qdict_get_try_str(options, "password-secret")); 6534ff45049SKevin Wolf if (secretid) { 6544ff45049SKevin Wolf qdict_del(options, "password-secret"); 6554ff45049SKevin Wolf } 6564ff45049SKevin Wolf 6574bfb2741SKevin Wolf /* Convert the remaining options into a QAPI object */ 6584bfb2741SKevin Wolf crumpled = qdict_crumple(options, errp); 6594bfb2741SKevin Wolf if (crumpled == NULL) { 6604bfb2741SKevin Wolf r = -EINVAL; 6614bfb2741SKevin Wolf goto out; 6624bfb2741SKevin Wolf } 6634bfb2741SKevin Wolf 6644bfb2741SKevin Wolf v = qobject_input_visitor_new_keyval(crumpled); 6654bfb2741SKevin Wolf visit_type_BlockdevOptionsRbd(v, NULL, &opts, &local_err); 6664bfb2741SKevin Wolf visit_free(v); 6674bfb2741SKevin Wolf qobject_decref(crumpled); 6684bfb2741SKevin Wolf 6694bfb2741SKevin Wolf if (local_err) { 6704bfb2741SKevin Wolf error_propagate(errp, local_err); 6714bfb2741SKevin Wolf r = -EINVAL; 6724bfb2741SKevin Wolf goto out; 6734bfb2741SKevin Wolf } 6744bfb2741SKevin Wolf 675*bfb15b4bSJeff Cody /* Remove the processed options from the QDict (the visitor processes 676*bfb15b4bSJeff Cody * _all_ options in the QDict) */ 677*bfb15b4bSJeff Cody while ((e = qdict_first(options))) { 678*bfb15b4bSJeff Cody qdict_del(options, e->key); 679*bfb15b4bSJeff Cody } 680*bfb15b4bSJeff Cody 681d41a5588SKevin Wolf r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts, 682d41a5588SKevin Wolf !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp); 6833d9136f9SKevin Wolf if (r < 0) { 6844ff45049SKevin Wolf goto out; 6853d9136f9SKevin Wolf } 6863d9136f9SKevin Wolf 687d41a5588SKevin Wolf s->snap = g_strdup(opts->snapshot); 688d41a5588SKevin Wolf s->image_name = g_strdup(opts->image); 689d41a5588SKevin Wolf 690e2b8247aSJeff Cody /* rbd_open is always r/w */ 69180b61a27SJeff Cody r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap); 692ad32e9c0SJosh Durgin if (r < 0) { 69380b61a27SJeff Cody error_setg_errno(errp, -r, "error reading header from %s", 69480b61a27SJeff Cody s->image_name); 695eb93d5d9SSage Weil goto failed_open; 696ad32e9c0SJosh Durgin } 697ad32e9c0SJosh Durgin 698e2b8247aSJeff Cody /* If we are using an rbd snapshot, we must be r/o, otherwise 699e2b8247aSJeff Cody * leave as-is */ 700e2b8247aSJeff Cody if (s->snap != NULL) { 701398e6ad0SKevin Wolf if (!bdrv_is_read_only(bs)) { 702398e6ad0SKevin Wolf error_report("Opening rbd snapshots without an explicit " 703398e6ad0SKevin Wolf "read-only=on option is deprecated. Future versions " 704398e6ad0SKevin Wolf "will refuse to open the image instead of " 705398e6ad0SKevin Wolf "automatically marking the image read-only."); 706e2b8247aSJeff Cody r = bdrv_set_read_only(bs, true, &local_err); 707e2b8247aSJeff Cody if (r < 0) { 708e2b8247aSJeff Cody error_propagate(errp, local_err); 709e2b8247aSJeff Cody goto failed_open; 710e2b8247aSJeff Cody } 711e2b8247aSJeff Cody } 712398e6ad0SKevin Wolf } 713f27aaf4bSChristian Brunner 7144ff45049SKevin Wolf r = 0; 7154ff45049SKevin Wolf goto out; 716f27aaf4bSChristian Brunner 717eb93d5d9SSage Weil failed_open: 718ad32e9c0SJosh Durgin rados_ioctx_destroy(s->io_ctx); 719eb93d5d9SSage Weil g_free(s->snap); 72080b61a27SJeff Cody g_free(s->image_name); 7213d9136f9SKevin Wolf rados_shutdown(s->cluster); 7224ff45049SKevin Wolf out: 7234bfb2741SKevin Wolf qapi_free_BlockdevOptionsRbd(opts); 7244ff45049SKevin Wolf g_free(keypairs); 7254ff45049SKevin Wolf g_free(secretid); 726f27aaf4bSChristian Brunner return r; 727f27aaf4bSChristian Brunner } 728f27aaf4bSChristian Brunner 72956e7cf8dSJeff Cody 73056e7cf8dSJeff Cody /* Since RBD is currently always opened R/W via the API, 73156e7cf8dSJeff Cody * we just need to check if we are using a snapshot or not, in 73256e7cf8dSJeff Cody * order to determine if we will allow it to be R/W */ 73356e7cf8dSJeff Cody static int qemu_rbd_reopen_prepare(BDRVReopenState *state, 73456e7cf8dSJeff Cody BlockReopenQueue *queue, Error **errp) 73556e7cf8dSJeff Cody { 73656e7cf8dSJeff Cody BDRVRBDState *s = state->bs->opaque; 73756e7cf8dSJeff Cody int ret = 0; 73856e7cf8dSJeff Cody 73956e7cf8dSJeff Cody if (s->snap && state->flags & BDRV_O_RDWR) { 74056e7cf8dSJeff Cody error_setg(errp, 74156e7cf8dSJeff Cody "Cannot change node '%s' to r/w when using RBD snapshot", 74256e7cf8dSJeff Cody bdrv_get_device_or_node_name(state->bs)); 74356e7cf8dSJeff Cody ret = -EINVAL; 74456e7cf8dSJeff Cody } 74556e7cf8dSJeff Cody 74656e7cf8dSJeff Cody return ret; 74756e7cf8dSJeff Cody } 74856e7cf8dSJeff Cody 749ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs) 750f27aaf4bSChristian Brunner { 751f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 752f27aaf4bSChristian Brunner 753ad32e9c0SJosh Durgin rbd_close(s->image); 754ad32e9c0SJosh Durgin rados_ioctx_destroy(s->io_ctx); 7557267c094SAnthony Liguori g_free(s->snap); 75680b61a27SJeff Cody g_free(s->image_name); 757ad32e9c0SJosh Durgin rados_shutdown(s->cluster); 758f27aaf4bSChristian Brunner } 759f27aaf4bSChristian Brunner 760d7331bedSStefan Hajnoczi static const AIOCBInfo rbd_aiocb_info = { 761f27aaf4bSChristian Brunner .aiocb_size = sizeof(RBDAIOCB), 762f27aaf4bSChristian Brunner }; 763f27aaf4bSChristian Brunner 764e04fb07fSStefan Hajnoczi static void rbd_finish_bh(void *opaque) 765f27aaf4bSChristian Brunner { 766e04fb07fSStefan Hajnoczi RADOSCB *rcb = opaque; 767e04fb07fSStefan Hajnoczi qemu_rbd_complete_aio(rcb); 768ad32e9c0SJosh Durgin } 769ad32e9c0SJosh Durgin 770ad32e9c0SJosh Durgin /* 771ad32e9c0SJosh Durgin * This is the callback function for rbd_aio_read and _write 772ad32e9c0SJosh Durgin * 773ad32e9c0SJosh Durgin * Note: this function is being called from a non qemu thread so 774ad32e9c0SJosh Durgin * we need to be careful about what we do here. Generally we only 775e04fb07fSStefan Hajnoczi * schedule a BH, and do the rest of the io completion handling 776e04fb07fSStefan Hajnoczi * from rbd_finish_bh() which runs in a qemu context. 777ad32e9c0SJosh Durgin */ 778ad32e9c0SJosh Durgin static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb) 779ad32e9c0SJosh Durgin { 780e04fb07fSStefan Hajnoczi RBDAIOCB *acb = rcb->acb; 781e04fb07fSStefan Hajnoczi 782ad32e9c0SJosh Durgin rcb->ret = rbd_aio_get_return_value(c); 783ad32e9c0SJosh Durgin rbd_aio_release(c); 784f27aaf4bSChristian Brunner 785fffb6e12SPaolo Bonzini aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs), 786ea800191SStefan Hajnoczi rbd_finish_bh, rcb); 787473c7f02SStefan Priebe } 788f27aaf4bSChristian Brunner 789787f3133SJosh Durgin static int rbd_aio_discard_wrapper(rbd_image_t image, 790787f3133SJosh Durgin uint64_t off, 791787f3133SJosh Durgin uint64_t len, 792787f3133SJosh Durgin rbd_completion_t comp) 793787f3133SJosh Durgin { 794787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 795787f3133SJosh Durgin return rbd_aio_discard(image, off, len, comp); 796787f3133SJosh Durgin #else 797787f3133SJosh Durgin return -ENOTSUP; 798787f3133SJosh Durgin #endif 799787f3133SJosh Durgin } 800787f3133SJosh Durgin 801dc7588c1SJosh Durgin static int rbd_aio_flush_wrapper(rbd_image_t image, 802dc7588c1SJosh Durgin rbd_completion_t comp) 803dc7588c1SJosh Durgin { 804dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH 805dc7588c1SJosh Durgin return rbd_aio_flush(image, comp); 806dc7588c1SJosh Durgin #else 807dc7588c1SJosh Durgin return -ENOTSUP; 808dc7588c1SJosh Durgin #endif 809dc7588c1SJosh Durgin } 810dc7588c1SJosh Durgin 8117c84b1b8SMarkus Armbruster static BlockAIOCB *rbd_start_aio(BlockDriverState *bs, 8127bbca9e2SEric Blake int64_t off, 813f27aaf4bSChristian Brunner QEMUIOVector *qiov, 8147bbca9e2SEric Blake int64_t size, 815097310b5SMarkus Armbruster BlockCompletionFunc *cb, 816787f3133SJosh Durgin void *opaque, 817787f3133SJosh Durgin RBDAIOCmd cmd) 818f27aaf4bSChristian Brunner { 819f27aaf4bSChristian Brunner RBDAIOCB *acb; 8200f7a0237SKevin Wolf RADOSCB *rcb = NULL; 821ad32e9c0SJosh Durgin rbd_completion_t c; 82251a13528SJosh Durgin int r; 823f27aaf4bSChristian Brunner 824f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 825f27aaf4bSChristian Brunner 826d7331bedSStefan Hajnoczi acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque); 827787f3133SJosh Durgin acb->cmd = cmd; 828f27aaf4bSChristian Brunner acb->qiov = qiov; 8297bbca9e2SEric Blake assert(!qiov || qiov->size == size); 8301d393bdeStianqing 8311d393bdeStianqing rcb = g_new(RADOSCB, 1); 8321d393bdeStianqing 8331d393bdeStianqing if (!LIBRBD_USE_IOVEC) { 834dc7588c1SJosh Durgin if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) { 835787f3133SJosh Durgin acb->bounce = NULL; 836787f3133SJosh Durgin } else { 8370f7a0237SKevin Wolf acb->bounce = qemu_try_blockalign(bs, qiov->size); 8380f7a0237SKevin Wolf if (acb->bounce == NULL) { 8390f7a0237SKevin Wolf goto failed; 8400f7a0237SKevin Wolf } 841787f3133SJosh Durgin } 8421d393bdeStianqing if (cmd == RBD_AIO_WRITE) { 8431d393bdeStianqing qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); 8441d393bdeStianqing } 8451d393bdeStianqing rcb->buf = acb->bounce; 8461d393bdeStianqing } 8471d393bdeStianqing 848f27aaf4bSChristian Brunner acb->ret = 0; 849f27aaf4bSChristian Brunner acb->error = 0; 850f27aaf4bSChristian Brunner acb->s = s; 851f27aaf4bSChristian Brunner 852f27aaf4bSChristian Brunner rcb->acb = acb; 853f27aaf4bSChristian Brunner rcb->s = acb->s; 854ad32e9c0SJosh Durgin rcb->size = size; 85551a13528SJosh Durgin r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c); 85651a13528SJosh Durgin if (r < 0) { 85751a13528SJosh Durgin goto failed; 85851a13528SJosh Durgin } 859f27aaf4bSChristian Brunner 860787f3133SJosh Durgin switch (cmd) { 861787f3133SJosh Durgin case RBD_AIO_WRITE: 8621d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC 8631d393bdeStianqing r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c); 8641d393bdeStianqing #else 8651d393bdeStianqing r = rbd_aio_write(s->image, off, size, rcb->buf, c); 8661d393bdeStianqing #endif 867787f3133SJosh Durgin break; 868787f3133SJosh Durgin case RBD_AIO_READ: 8691d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC 8701d393bdeStianqing r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c); 8711d393bdeStianqing #else 8721d393bdeStianqing r = rbd_aio_read(s->image, off, size, rcb->buf, c); 8731d393bdeStianqing #endif 874787f3133SJosh Durgin break; 875787f3133SJosh Durgin case RBD_AIO_DISCARD: 876787f3133SJosh Durgin r = rbd_aio_discard_wrapper(s->image, off, size, c); 877787f3133SJosh Durgin break; 878dc7588c1SJosh Durgin case RBD_AIO_FLUSH: 879dc7588c1SJosh Durgin r = rbd_aio_flush_wrapper(s->image, c); 880dc7588c1SJosh Durgin break; 881787f3133SJosh Durgin default: 882787f3133SJosh Durgin r = -EINVAL; 88351a13528SJosh Durgin } 88451a13528SJosh Durgin 88551a13528SJosh Durgin if (r < 0) { 886405a2764SKevin Wolf goto failed_completion; 887f27aaf4bSChristian Brunner } 888f27aaf4bSChristian Brunner return &acb->common; 88951a13528SJosh Durgin 890405a2764SKevin Wolf failed_completion: 891405a2764SKevin Wolf rbd_aio_release(c); 89251a13528SJosh Durgin failed: 8937267c094SAnthony Liguori g_free(rcb); 8941d393bdeStianqing if (!LIBRBD_USE_IOVEC) { 895405a2764SKevin Wolf qemu_vfree(acb->bounce); 8961d393bdeStianqing } 8971d393bdeStianqing 8988007429aSFam Zheng qemu_aio_unref(acb); 89951a13528SJosh Durgin return NULL; 900f27aaf4bSChristian Brunner } 901f27aaf4bSChristian Brunner 9027c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs, 903ad32e9c0SJosh Durgin int64_t sector_num, 904ad32e9c0SJosh Durgin QEMUIOVector *qiov, 905f27aaf4bSChristian Brunner int nb_sectors, 906097310b5SMarkus Armbruster BlockCompletionFunc *cb, 907f27aaf4bSChristian Brunner void *opaque) 908f27aaf4bSChristian Brunner { 9097bbca9e2SEric Blake return rbd_start_aio(bs, sector_num << BDRV_SECTOR_BITS, qiov, 910e948f663SPaolo Bonzini (int64_t) nb_sectors << BDRV_SECTOR_BITS, cb, opaque, 911787f3133SJosh Durgin RBD_AIO_READ); 912f27aaf4bSChristian Brunner } 913f27aaf4bSChristian Brunner 9147c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs, 915ad32e9c0SJosh Durgin int64_t sector_num, 916ad32e9c0SJosh Durgin QEMUIOVector *qiov, 917f27aaf4bSChristian Brunner int nb_sectors, 918097310b5SMarkus Armbruster BlockCompletionFunc *cb, 919f27aaf4bSChristian Brunner void *opaque) 920f27aaf4bSChristian Brunner { 9217bbca9e2SEric Blake return rbd_start_aio(bs, sector_num << BDRV_SECTOR_BITS, qiov, 922e948f663SPaolo Bonzini (int64_t) nb_sectors << BDRV_SECTOR_BITS, cb, opaque, 923787f3133SJosh Durgin RBD_AIO_WRITE); 924f27aaf4bSChristian Brunner } 925f27aaf4bSChristian Brunner 926dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH 9277c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs, 928097310b5SMarkus Armbruster BlockCompletionFunc *cb, 929dc7588c1SJosh Durgin void *opaque) 930dc7588c1SJosh Durgin { 931dc7588c1SJosh Durgin return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH); 932dc7588c1SJosh Durgin } 933dc7588c1SJosh Durgin 934dc7588c1SJosh Durgin #else 935dc7588c1SJosh Durgin 9368b94ff85SPaolo Bonzini static int qemu_rbd_co_flush(BlockDriverState *bs) 9377a3f5fe9SSage Weil { 9387a3f5fe9SSage Weil #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1) 9397a3f5fe9SSage Weil /* rbd_flush added in 0.1.1 */ 9407a3f5fe9SSage Weil BDRVRBDState *s = bs->opaque; 9417a3f5fe9SSage Weil return rbd_flush(s->image); 9427a3f5fe9SSage Weil #else 9437a3f5fe9SSage Weil return 0; 9447a3f5fe9SSage Weil #endif 9457a3f5fe9SSage Weil } 946dc7588c1SJosh Durgin #endif 9477a3f5fe9SSage Weil 948ad32e9c0SJosh Durgin static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi) 949f27aaf4bSChristian Brunner { 950f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 951ad32e9c0SJosh Durgin rbd_image_info_t info; 952ad32e9c0SJosh Durgin int r; 953ad32e9c0SJosh Durgin 954ad32e9c0SJosh Durgin r = rbd_stat(s->image, &info, sizeof(info)); 955ad32e9c0SJosh Durgin if (r < 0) { 956ad32e9c0SJosh Durgin return r; 957ad32e9c0SJosh Durgin } 958ad32e9c0SJosh Durgin 959ad32e9c0SJosh Durgin bdi->cluster_size = info.obj_size; 960f27aaf4bSChristian Brunner return 0; 961f27aaf4bSChristian Brunner } 962f27aaf4bSChristian Brunner 963ad32e9c0SJosh Durgin static int64_t qemu_rbd_getlength(BlockDriverState *bs) 964f27aaf4bSChristian Brunner { 965f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 966ad32e9c0SJosh Durgin rbd_image_info_t info; 967ad32e9c0SJosh Durgin int r; 968f27aaf4bSChristian Brunner 969ad32e9c0SJosh Durgin r = rbd_stat(s->image, &info, sizeof(info)); 970ad32e9c0SJosh Durgin if (r < 0) { 971ad32e9c0SJosh Durgin return r; 972f27aaf4bSChristian Brunner } 973f27aaf4bSChristian Brunner 974ad32e9c0SJosh Durgin return info.size; 975ad32e9c0SJosh Durgin } 976ad32e9c0SJosh Durgin 9778243ccb7SMax Reitz static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset, 9788243ccb7SMax Reitz PreallocMode prealloc, Error **errp) 97930cdc48cSJosh Durgin { 98030cdc48cSJosh Durgin BDRVRBDState *s = bs->opaque; 98130cdc48cSJosh Durgin int r; 98230cdc48cSJosh Durgin 9838243ccb7SMax Reitz if (prealloc != PREALLOC_MODE_OFF) { 9848243ccb7SMax Reitz error_setg(errp, "Unsupported preallocation mode '%s'", 985977c736fSMarkus Armbruster PreallocMode_str(prealloc)); 9868243ccb7SMax Reitz return -ENOTSUP; 9878243ccb7SMax Reitz } 9888243ccb7SMax Reitz 98930cdc48cSJosh Durgin r = rbd_resize(s->image, offset); 99030cdc48cSJosh Durgin if (r < 0) { 991f59adb32SMax Reitz error_setg_errno(errp, -r, "Failed to resize file"); 99230cdc48cSJosh Durgin return r; 99330cdc48cSJosh Durgin } 99430cdc48cSJosh Durgin 99530cdc48cSJosh Durgin return 0; 99630cdc48cSJosh Durgin } 99730cdc48cSJosh Durgin 998ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs, 999ad32e9c0SJosh Durgin QEMUSnapshotInfo *sn_info) 1000f27aaf4bSChristian Brunner { 1001f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 1002f27aaf4bSChristian Brunner int r; 1003f27aaf4bSChristian Brunner 1004f27aaf4bSChristian Brunner if (sn_info->name[0] == '\0') { 1005f27aaf4bSChristian Brunner return -EINVAL; /* we need a name for rbd snapshots */ 1006f27aaf4bSChristian Brunner } 1007f27aaf4bSChristian Brunner 1008f27aaf4bSChristian Brunner /* 1009f27aaf4bSChristian Brunner * rbd snapshots are using the name as the user controlled unique identifier 1010f27aaf4bSChristian Brunner * we can't use the rbd snapid for that purpose, as it can't be set 1011f27aaf4bSChristian Brunner */ 1012f27aaf4bSChristian Brunner if (sn_info->id_str[0] != '\0' && 1013f27aaf4bSChristian Brunner strcmp(sn_info->id_str, sn_info->name) != 0) { 1014f27aaf4bSChristian Brunner return -EINVAL; 1015f27aaf4bSChristian Brunner } 1016f27aaf4bSChristian Brunner 1017f27aaf4bSChristian Brunner if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) { 1018f27aaf4bSChristian Brunner return -ERANGE; 1019f27aaf4bSChristian Brunner } 1020f27aaf4bSChristian Brunner 1021ad32e9c0SJosh Durgin r = rbd_snap_create(s->image, sn_info->name); 1022f27aaf4bSChristian Brunner if (r < 0) { 1023ad32e9c0SJosh Durgin error_report("failed to create snap: %s", strerror(-r)); 1024f27aaf4bSChristian Brunner return r; 1025f27aaf4bSChristian Brunner } 1026f27aaf4bSChristian Brunner 1027f27aaf4bSChristian Brunner return 0; 1028f27aaf4bSChristian Brunner } 1029f27aaf4bSChristian Brunner 1030bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs, 1031a89d89d3SWenchao Xia const char *snapshot_id, 1032a89d89d3SWenchao Xia const char *snapshot_name, 1033a89d89d3SWenchao Xia Error **errp) 1034bd603247SGregory Farnum { 1035bd603247SGregory Farnum BDRVRBDState *s = bs->opaque; 1036bd603247SGregory Farnum int r; 1037bd603247SGregory Farnum 1038a89d89d3SWenchao Xia if (!snapshot_name) { 1039a89d89d3SWenchao Xia error_setg(errp, "rbd need a valid snapshot name"); 1040a89d89d3SWenchao Xia return -EINVAL; 1041a89d89d3SWenchao Xia } 1042a89d89d3SWenchao Xia 1043a89d89d3SWenchao Xia /* If snapshot_id is specified, it must be equal to name, see 1044a89d89d3SWenchao Xia qemu_rbd_snap_list() */ 1045a89d89d3SWenchao Xia if (snapshot_id && strcmp(snapshot_id, snapshot_name)) { 1046a89d89d3SWenchao Xia error_setg(errp, 1047a89d89d3SWenchao Xia "rbd do not support snapshot id, it should be NULL or " 1048a89d89d3SWenchao Xia "equal to snapshot name"); 1049a89d89d3SWenchao Xia return -EINVAL; 1050a89d89d3SWenchao Xia } 1051a89d89d3SWenchao Xia 1052bd603247SGregory Farnum r = rbd_snap_remove(s->image, snapshot_name); 1053a89d89d3SWenchao Xia if (r < 0) { 1054a89d89d3SWenchao Xia error_setg_errno(errp, -r, "Failed to remove the snapshot"); 1055a89d89d3SWenchao Xia } 1056bd603247SGregory Farnum return r; 1057bd603247SGregory Farnum } 1058bd603247SGregory Farnum 1059bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs, 1060bd603247SGregory Farnum const char *snapshot_name) 1061bd603247SGregory Farnum { 1062bd603247SGregory Farnum BDRVRBDState *s = bs->opaque; 1063bd603247SGregory Farnum 10649be38598SEduardo Habkost return rbd_snap_rollback(s->image, snapshot_name); 1065bd603247SGregory Farnum } 1066bd603247SGregory Farnum 1067ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs, 1068ad32e9c0SJosh Durgin QEMUSnapshotInfo **psn_tab) 1069f27aaf4bSChristian Brunner { 1070f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 1071f27aaf4bSChristian Brunner QEMUSnapshotInfo *sn_info, *sn_tab = NULL; 1072ad32e9c0SJosh Durgin int i, snap_count; 1073ad32e9c0SJosh Durgin rbd_snap_info_t *snaps; 1074ad32e9c0SJosh Durgin int max_snaps = RBD_MAX_SNAPS; 1075f27aaf4bSChristian Brunner 1076ad32e9c0SJosh Durgin do { 107702c4f26bSMarkus Armbruster snaps = g_new(rbd_snap_info_t, max_snaps); 1078ad32e9c0SJosh Durgin snap_count = rbd_snap_list(s->image, snaps, &max_snaps); 10799e6337d0SStefan Hajnoczi if (snap_count <= 0) { 10807267c094SAnthony Liguori g_free(snaps); 1081f27aaf4bSChristian Brunner } 1082ad32e9c0SJosh Durgin } while (snap_count == -ERANGE); 1083f27aaf4bSChristian Brunner 1084ad32e9c0SJosh Durgin if (snap_count <= 0) { 1085b9c53290SJosh Durgin goto done; 1086f27aaf4bSChristian Brunner } 1087f27aaf4bSChristian Brunner 10885839e53bSMarkus Armbruster sn_tab = g_new0(QEMUSnapshotInfo, snap_count); 1089f27aaf4bSChristian Brunner 1090ad32e9c0SJosh Durgin for (i = 0; i < snap_count; i++) { 1091ad32e9c0SJosh Durgin const char *snap_name = snaps[i].name; 1092f27aaf4bSChristian Brunner 1093f27aaf4bSChristian Brunner sn_info = sn_tab + i; 1094f27aaf4bSChristian Brunner pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name); 1095f27aaf4bSChristian Brunner pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name); 1096f27aaf4bSChristian Brunner 1097ad32e9c0SJosh Durgin sn_info->vm_state_size = snaps[i].size; 1098f27aaf4bSChristian Brunner sn_info->date_sec = 0; 1099f27aaf4bSChristian Brunner sn_info->date_nsec = 0; 1100f27aaf4bSChristian Brunner sn_info->vm_clock_nsec = 0; 1101f27aaf4bSChristian Brunner } 1102ad32e9c0SJosh Durgin rbd_snap_list_end(snaps); 11039e6337d0SStefan Hajnoczi g_free(snaps); 1104ad32e9c0SJosh Durgin 1105b9c53290SJosh Durgin done: 1106f27aaf4bSChristian Brunner *psn_tab = sn_tab; 1107f27aaf4bSChristian Brunner return snap_count; 1108f27aaf4bSChristian Brunner } 1109f27aaf4bSChristian Brunner 1110787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 11114da444a0SEric Blake static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs, 11124da444a0SEric Blake int64_t offset, 1113f5a5ca79SManos Pitsidianakis int bytes, 1114097310b5SMarkus Armbruster BlockCompletionFunc *cb, 1115787f3133SJosh Durgin void *opaque) 1116787f3133SJosh Durgin { 1117f5a5ca79SManos Pitsidianakis return rbd_start_aio(bs, offset, NULL, bytes, cb, opaque, 1118787f3133SJosh Durgin RBD_AIO_DISCARD); 1119787f3133SJosh Durgin } 1120787f3133SJosh Durgin #endif 1121787f3133SJosh Durgin 1122be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE 11232b148f39SPaolo Bonzini static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs, 1124be217884SAdam Crume Error **errp) 1125be217884SAdam Crume { 1126be217884SAdam Crume BDRVRBDState *s = bs->opaque; 1127be217884SAdam Crume int r = rbd_invalidate_cache(s->image); 1128be217884SAdam Crume if (r < 0) { 1129be217884SAdam Crume error_setg_errno(errp, -r, "Failed to invalidate the cache"); 1130be217884SAdam Crume } 1131be217884SAdam Crume } 1132be217884SAdam Crume #endif 1133be217884SAdam Crume 1134bd0cf596SChunyan Liu static QemuOptsList qemu_rbd_create_opts = { 1135bd0cf596SChunyan Liu .name = "rbd-create-opts", 1136bd0cf596SChunyan Liu .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head), 1137bd0cf596SChunyan Liu .desc = { 1138f27aaf4bSChristian Brunner { 1139f27aaf4bSChristian Brunner .name = BLOCK_OPT_SIZE, 1140bd0cf596SChunyan Liu .type = QEMU_OPT_SIZE, 1141f27aaf4bSChristian Brunner .help = "Virtual disk size" 1142f27aaf4bSChristian Brunner }, 1143f27aaf4bSChristian Brunner { 1144f27aaf4bSChristian Brunner .name = BLOCK_OPT_CLUSTER_SIZE, 1145bd0cf596SChunyan Liu .type = QEMU_OPT_SIZE, 1146f27aaf4bSChristian Brunner .help = "RBD object size" 1147f27aaf4bSChristian Brunner }, 114860390a21SDaniel P. Berrange { 114960390a21SDaniel P. Berrange .name = "password-secret", 115060390a21SDaniel P. Berrange .type = QEMU_OPT_STRING, 115160390a21SDaniel P. Berrange .help = "ID of secret providing the password", 115260390a21SDaniel P. Berrange }, 1153bd0cf596SChunyan Liu { /* end of list */ } 1154bd0cf596SChunyan Liu } 1155f27aaf4bSChristian Brunner }; 1156f27aaf4bSChristian Brunner 1157f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = { 1158f27aaf4bSChristian Brunner .format_name = "rbd", 1159f27aaf4bSChristian Brunner .instance_size = sizeof(BDRVRBDState), 1160c7cacb3eSJeff Cody .bdrv_parse_filename = qemu_rbd_parse_filename, 1161ad32e9c0SJosh Durgin .bdrv_file_open = qemu_rbd_open, 1162ad32e9c0SJosh Durgin .bdrv_close = qemu_rbd_close, 116356e7cf8dSJeff Cody .bdrv_reopen_prepare = qemu_rbd_reopen_prepare, 11641bebea37SKevin Wolf .bdrv_co_create = qemu_rbd_co_create, 1165efc75e2aSStefan Hajnoczi .bdrv_co_create_opts = qemu_rbd_co_create_opts, 11663ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1, 1167ad32e9c0SJosh Durgin .bdrv_get_info = qemu_rbd_getinfo, 1168bd0cf596SChunyan Liu .create_opts = &qemu_rbd_create_opts, 1169ad32e9c0SJosh Durgin .bdrv_getlength = qemu_rbd_getlength, 117030cdc48cSJosh Durgin .bdrv_truncate = qemu_rbd_truncate, 1171f27aaf4bSChristian Brunner .protocol_name = "rbd", 1172f27aaf4bSChristian Brunner 1173ad32e9c0SJosh Durgin .bdrv_aio_readv = qemu_rbd_aio_readv, 1174ad32e9c0SJosh Durgin .bdrv_aio_writev = qemu_rbd_aio_writev, 1175dc7588c1SJosh Durgin 1176dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH 1177dc7588c1SJosh Durgin .bdrv_aio_flush = qemu_rbd_aio_flush, 1178dc7588c1SJosh Durgin #else 1179c68b89acSKevin Wolf .bdrv_co_flush_to_disk = qemu_rbd_co_flush, 1180dc7588c1SJosh Durgin #endif 1181f27aaf4bSChristian Brunner 1182787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 11834da444a0SEric Blake .bdrv_aio_pdiscard = qemu_rbd_aio_pdiscard, 1184787f3133SJosh Durgin #endif 1185787f3133SJosh Durgin 1186ad32e9c0SJosh Durgin .bdrv_snapshot_create = qemu_rbd_snap_create, 1187bd603247SGregory Farnum .bdrv_snapshot_delete = qemu_rbd_snap_remove, 1188ad32e9c0SJosh Durgin .bdrv_snapshot_list = qemu_rbd_snap_list, 1189bd603247SGregory Farnum .bdrv_snapshot_goto = qemu_rbd_snap_rollback, 1190be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE 11912b148f39SPaolo Bonzini .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache, 1192be217884SAdam Crume #endif 1193f27aaf4bSChristian Brunner }; 1194f27aaf4bSChristian Brunner 1195f27aaf4bSChristian Brunner static void bdrv_rbd_init(void) 1196f27aaf4bSChristian Brunner { 1197f27aaf4bSChristian Brunner bdrv_register(&bdrv_rbd); 1198f27aaf4bSChristian Brunner } 1199f27aaf4bSChristian Brunner 1200f27aaf4bSChristian Brunner block_init(bdrv_rbd_init); 1201