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" 21e2c1c34fSMarkus Armbruster #include "block/block-io.h" 22737e150eSPaolo Bonzini #include "block/block_int.h" 23609f45eaSMax Reitz #include "block/qdict.h" 2460390a21SDaniel P. Berrange #include "crypto/secret.h" 25f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 26e4ec5ad4SPavel Dovgalyuk #include "sysemu/replay.h" 27c7cacb3eSJeff Cody #include "qapi/qmp/qstring.h" 28452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h" 29e98c6961SEric Blake #include "qapi/qmp/qjson.h" 3047e6b297SMarkus Armbruster #include "qapi/qmp/qlist.h" 314bfb2741SKevin Wolf #include "qapi/qobject-input-visitor.h" 324bfb2741SKevin Wolf #include "qapi/qapi-visit-block-core.h" 33f27aaf4bSChristian Brunner 34f27aaf4bSChristian Brunner /* 35f27aaf4bSChristian Brunner * When specifying the image filename use: 36f27aaf4bSChristian Brunner * 37fab5cf59SJosh Durgin * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]] 38f27aaf4bSChristian Brunner * 399e1fbcdeSSage Weil * poolname must be the name of an existing rados pool. 40f27aaf4bSChristian Brunner * 419e1fbcdeSSage Weil * devicename is the name of the rbd image. 42f27aaf4bSChristian Brunner * 439e1fbcdeSSage Weil * Each option given is used to configure rados, and may be any valid 449e1fbcdeSSage Weil * Ceph option, "id", or "conf". 45fab5cf59SJosh Durgin * 469e1fbcdeSSage Weil * The "id" option indicates what user we should authenticate as to 479e1fbcdeSSage Weil * the Ceph cluster. If it is excluded we will use the Ceph default 489e1fbcdeSSage Weil * (normally 'admin'). 49f27aaf4bSChristian Brunner * 509e1fbcdeSSage Weil * The "conf" option specifies a Ceph configuration file to read. If 519e1fbcdeSSage Weil * it is not specified, we will read from the default Ceph locations 529e1fbcdeSSage Weil * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration 539e1fbcdeSSage Weil * file, specify conf=/dev/null. 54f27aaf4bSChristian Brunner * 559e1fbcdeSSage Weil * Configuration values containing :, @, or = can be escaped with a 569e1fbcdeSSage Weil * leading "\". 57f27aaf4bSChristian Brunner */ 58f27aaf4bSChristian Brunner 59f27aaf4bSChristian Brunner #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER) 60f27aaf4bSChristian Brunner 61ad32e9c0SJosh Durgin #define RBD_MAX_SNAPS 100 62ad32e9c0SJosh Durgin 6342e4ac9eSOr Ozeri #define RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN 8 6442e4ac9eSOr Ozeri 6542e4ac9eSOr Ozeri static const char rbd_luks_header_verification[ 6642e4ac9eSOr Ozeri RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN] = { 6742e4ac9eSOr Ozeri 'L', 'U', 'K', 'S', 0xBA, 0xBE, 0, 1 6842e4ac9eSOr Ozeri }; 6942e4ac9eSOr Ozeri 7042e4ac9eSOr Ozeri static const char rbd_luks2_header_verification[ 7142e4ac9eSOr Ozeri RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN] = { 7242e4ac9eSOr Ozeri 'L', 'U', 'K', 'S', 0xBA, 0xBE, 0, 2 7342e4ac9eSOr Ozeri }; 7442e4ac9eSOr Ozeri 75787f3133SJosh Durgin typedef enum { 76787f3133SJosh Durgin RBD_AIO_READ, 77787f3133SJosh Durgin RBD_AIO_WRITE, 78dc7588c1SJosh Durgin RBD_AIO_DISCARD, 79c56ac27dSPeter Lieven RBD_AIO_FLUSH, 80c56ac27dSPeter Lieven RBD_AIO_WRITE_ZEROES 81787f3133SJosh Durgin } RBDAIOCmd; 82787f3133SJosh Durgin 83f27aaf4bSChristian Brunner typedef struct BDRVRBDState { 84ad32e9c0SJosh Durgin rados_t cluster; 85ad32e9c0SJosh Durgin rados_ioctx_t io_ctx; 86ad32e9c0SJosh Durgin rbd_image_t image; 8780b61a27SJeff Cody char *image_name; 88ad32e9c0SJosh Durgin char *snap; 8919ae9ae0SFlorian Florensa char *namespace; 90d24f8023SStefano Garzarella uint64_t image_size; 91832a93dcSPeter Lieven uint64_t object_size; 92f27aaf4bSChristian Brunner } BDRVRBDState; 93f27aaf4bSChristian Brunner 94c3e5fac5SPeter Lieven typedef struct RBDTask { 95c3e5fac5SPeter Lieven BlockDriverState *bs; 96c3e5fac5SPeter Lieven Coroutine *co; 97c3e5fac5SPeter Lieven bool complete; 98c3e5fac5SPeter Lieven int64_t ret; 99c3e5fac5SPeter Lieven } RBDTask; 100c3e5fac5SPeter Lieven 1010347a8fdSPeter Lieven typedef struct RBDDiffIterateReq { 1020347a8fdSPeter Lieven uint64_t offs; 1030347a8fdSPeter Lieven uint64_t bytes; 1040347a8fdSPeter Lieven bool exists; 1050347a8fdSPeter Lieven } RBDDiffIterateReq; 1060347a8fdSPeter Lieven 107aa045c2dSKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx, 108aa045c2dSKevin Wolf BlockdevOptionsRbd *opts, bool cache, 109aa045c2dSKevin Wolf const char *keypairs, const char *secretid, 110aa045c2dSKevin Wolf Error **errp); 111aa045c2dSKevin Wolf 1122b99cfceSConnor Kuehl static char *qemu_rbd_strchr(char *src, char delim) 1132b99cfceSConnor Kuehl { 1142b99cfceSConnor Kuehl char *p; 1152b99cfceSConnor Kuehl 1162b99cfceSConnor Kuehl for (p = src; *p; ++p) { 1172b99cfceSConnor Kuehl if (*p == delim) { 1182b99cfceSConnor Kuehl return p; 1192b99cfceSConnor Kuehl } 1202b99cfceSConnor Kuehl if (*p == '\\' && p[1] != '\0') { 1212b99cfceSConnor Kuehl ++p; 1222b99cfceSConnor Kuehl } 1232b99cfceSConnor Kuehl } 1242b99cfceSConnor Kuehl 1252b99cfceSConnor Kuehl return NULL; 1262b99cfceSConnor Kuehl } 1272b99cfceSConnor Kuehl 1282b99cfceSConnor Kuehl 129730b00bbSMarkus Armbruster static char *qemu_rbd_next_tok(char *src, char delim, char **p) 130f27aaf4bSChristian Brunner { 131f27aaf4bSChristian Brunner char *end; 132f27aaf4bSChristian Brunner 133f27aaf4bSChristian Brunner *p = NULL; 134f27aaf4bSChristian Brunner 1352b99cfceSConnor Kuehl end = qemu_rbd_strchr(src, delim); 1362b99cfceSConnor Kuehl if (end) { 137f27aaf4bSChristian Brunner *p = end + 1; 138f27aaf4bSChristian Brunner *end = '\0'; 139f27aaf4bSChristian Brunner } 1407830f909SJeff Cody return src; 141f27aaf4bSChristian Brunner } 142f27aaf4bSChristian Brunner 14316a06b24SSage Weil static void qemu_rbd_unescape(char *src) 14416a06b24SSage Weil { 14516a06b24SSage Weil char *p; 14616a06b24SSage Weil 14716a06b24SSage Weil for (p = src; *src; ++src, ++p) { 14816a06b24SSage Weil if (*src == '\\' && src[1] != '\0') { 14916a06b24SSage Weil src++; 15016a06b24SSage Weil } 15116a06b24SSage Weil *p = *src; 15216a06b24SSage Weil } 15316a06b24SSage Weil *p = '\0'; 15416a06b24SSage Weil } 15516a06b24SSage Weil 156c7cacb3eSJeff Cody static void qemu_rbd_parse_filename(const char *filename, QDict *options, 157d61563b2SMarkus Armbruster Error **errp) 158f27aaf4bSChristian Brunner { 159f27aaf4bSChristian Brunner const char *start; 160e98c6961SEric Blake char *p, *buf; 161e98c6961SEric Blake QList *keypairs = NULL; 16219ae9ae0SFlorian Florensa char *found_str, *image_name; 163f27aaf4bSChristian Brunner 164f27aaf4bSChristian Brunner if (!strstart(filename, "rbd:", &start)) { 165d61563b2SMarkus Armbruster error_setg(errp, "File name must start with 'rbd:'"); 166c7cacb3eSJeff Cody return; 167f27aaf4bSChristian Brunner } 168f27aaf4bSChristian Brunner 1697267c094SAnthony Liguori buf = g_strdup(start); 170f27aaf4bSChristian Brunner p = buf; 171f27aaf4bSChristian Brunner 172730b00bbSMarkus Armbruster found_str = qemu_rbd_next_tok(p, '/', &p); 1737830f909SJeff Cody if (!p) { 1747830f909SJeff Cody error_setg(errp, "Pool name is required"); 1757830f909SJeff Cody goto done; 1767830f909SJeff Cody } 1777830f909SJeff Cody qemu_rbd_unescape(found_str); 17846f5ac20SEric Blake qdict_put_str(options, "pool", found_str); 179fab5cf59SJosh Durgin 1802b99cfceSConnor Kuehl if (qemu_rbd_strchr(p, '@')) { 18119ae9ae0SFlorian Florensa image_name = qemu_rbd_next_tok(p, '@', &p); 1827830f909SJeff Cody 183730b00bbSMarkus Armbruster found_str = qemu_rbd_next_tok(p, ':', &p); 1847830f909SJeff Cody qemu_rbd_unescape(found_str); 18546f5ac20SEric Blake qdict_put_str(options, "snapshot", found_str); 1867830f909SJeff Cody } else { 18719ae9ae0SFlorian Florensa image_name = qemu_rbd_next_tok(p, ':', &p); 1887830f909SJeff Cody } 18919ae9ae0SFlorian Florensa /* Check for namespace in the image_name */ 1902b99cfceSConnor Kuehl if (qemu_rbd_strchr(image_name, '/')) { 19119ae9ae0SFlorian Florensa found_str = qemu_rbd_next_tok(image_name, '/', &image_name); 19219ae9ae0SFlorian Florensa qemu_rbd_unescape(found_str); 19319ae9ae0SFlorian Florensa qdict_put_str(options, "namespace", found_str); 19419ae9ae0SFlorian Florensa } else { 19519ae9ae0SFlorian Florensa qdict_put_str(options, "namespace", ""); 19619ae9ae0SFlorian Florensa } 19719ae9ae0SFlorian Florensa qemu_rbd_unescape(image_name); 19819ae9ae0SFlorian Florensa qdict_put_str(options, "image", image_name); 1997830f909SJeff Cody if (!p) { 200f27aaf4bSChristian Brunner goto done; 201f27aaf4bSChristian Brunner } 202f27aaf4bSChristian Brunner 203c7cacb3eSJeff Cody /* The following are essentially all key/value pairs, and we treat 204c7cacb3eSJeff Cody * 'id' and 'conf' a bit special. Key/value pairs may be in any order. */ 205c7cacb3eSJeff Cody while (p) { 206c7cacb3eSJeff Cody char *name, *value; 207730b00bbSMarkus Armbruster name = qemu_rbd_next_tok(p, '=', &p); 208c7cacb3eSJeff Cody if (!p) { 209c7cacb3eSJeff Cody error_setg(errp, "conf option %s has no value", name); 210c7cacb3eSJeff Cody break; 211c7cacb3eSJeff Cody } 212c7cacb3eSJeff Cody 213c7cacb3eSJeff Cody qemu_rbd_unescape(name); 214c7cacb3eSJeff Cody 215730b00bbSMarkus Armbruster value = qemu_rbd_next_tok(p, ':', &p); 216c7cacb3eSJeff Cody qemu_rbd_unescape(value); 217c7cacb3eSJeff Cody 218c7cacb3eSJeff Cody if (!strcmp(name, "conf")) { 21946f5ac20SEric Blake qdict_put_str(options, "conf", value); 220c7cacb3eSJeff Cody } else if (!strcmp(name, "id")) { 22146f5ac20SEric Blake qdict_put_str(options, "user", value); 222c7cacb3eSJeff Cody } else { 223e98c6961SEric Blake /* 224e98c6961SEric Blake * We pass these internally to qemu_rbd_set_keypairs(), so 225e98c6961SEric Blake * we can get away with the simpler list of [ "key1", 226e98c6961SEric Blake * "value1", "key2", "value2" ] rather than a raw dict 227e98c6961SEric Blake * { "key1": "value1", "key2": "value2" } where we can't 228e98c6961SEric Blake * guarantee order, or even a more correct but complex 229e98c6961SEric Blake * [ { "key1": "value1" }, { "key2": "value2" } ] 230e98c6961SEric Blake */ 231e98c6961SEric Blake if (!keypairs) { 232e98c6961SEric Blake keypairs = qlist_new(); 233c7cacb3eSJeff Cody } 23446f5ac20SEric Blake qlist_append_str(keypairs, name); 23546f5ac20SEric Blake qlist_append_str(keypairs, value); 236c7cacb3eSJeff Cody } 237c7cacb3eSJeff Cody } 238c7cacb3eSJeff Cody 239e98c6961SEric Blake if (keypairs) { 240e98c6961SEric Blake qdict_put(options, "=keyvalue-pairs", 241eab3a467SMarkus Armbruster qstring_from_gstring(qobject_to_json(QOBJECT(keypairs)))); 242c7cacb3eSJeff Cody } 243c7cacb3eSJeff Cody 244f27aaf4bSChristian Brunner done: 2457267c094SAnthony Liguori g_free(buf); 246cb3e7f08SMarc-André Lureau qobject_unref(keypairs); 247c7cacb3eSJeff Cody return; 2487c7e9df0SSage Weil } 2497c7e9df0SSage Weil 250d083f954SMarkus Armbruster static int qemu_rbd_set_auth(rados_t cluster, BlockdevOptionsRbd *opts, 25160390a21SDaniel P. Berrange Error **errp) 25260390a21SDaniel P. Berrange { 253d083f954SMarkus Armbruster char *key, *acr; 254a3699de4SMarkus Armbruster int r; 255a3699de4SMarkus Armbruster GString *accu; 256a3699de4SMarkus Armbruster RbdAuthModeList *auth; 25760390a21SDaniel P. Berrange 258d083f954SMarkus Armbruster if (opts->key_secret) { 259d083f954SMarkus Armbruster key = qcrypto_secret_lookup_as_base64(opts->key_secret, errp); 260d083f954SMarkus Armbruster if (!key) { 261d083f954SMarkus Armbruster return -EIO; 26260390a21SDaniel P. Berrange } 263d083f954SMarkus Armbruster r = rados_conf_set(cluster, "key", key); 264d083f954SMarkus Armbruster g_free(key); 265d083f954SMarkus Armbruster if (r < 0) { 266d083f954SMarkus Armbruster error_setg_errno(errp, -r, "Could not set 'key'"); 267d083f954SMarkus Armbruster return r; 268d083f954SMarkus Armbruster } 269a3699de4SMarkus Armbruster } 270a3699de4SMarkus Armbruster 271a3699de4SMarkus Armbruster if (opts->has_auth_client_required) { 272a3699de4SMarkus Armbruster accu = g_string_new(""); 273a3699de4SMarkus Armbruster for (auth = opts->auth_client_required; auth; auth = auth->next) { 274a3699de4SMarkus Armbruster if (accu->str[0]) { 275a3699de4SMarkus Armbruster g_string_append_c(accu, ';'); 276a3699de4SMarkus Armbruster } 277a3699de4SMarkus Armbruster g_string_append(accu, RbdAuthMode_str(auth->value)); 278a3699de4SMarkus Armbruster } 279a3699de4SMarkus Armbruster acr = g_string_free(accu, FALSE); 280a3699de4SMarkus Armbruster r = rados_conf_set(cluster, "auth_client_required", acr); 281a3699de4SMarkus Armbruster g_free(acr); 282a3699de4SMarkus Armbruster if (r < 0) { 283a3699de4SMarkus Armbruster error_setg_errno(errp, -r, 284a3699de4SMarkus Armbruster "Could not set 'auth_client_required'"); 285a3699de4SMarkus Armbruster return r; 286a3699de4SMarkus Armbruster } 287a3699de4SMarkus Armbruster } 28860390a21SDaniel P. Berrange 28960390a21SDaniel P. Berrange return 0; 29060390a21SDaniel P. Berrange } 29160390a21SDaniel P. Berrange 292e98c6961SEric Blake static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json, 293e34d8f29SJosh Durgin Error **errp) 294fab5cf59SJosh Durgin { 295e98c6961SEric Blake QList *keypairs; 296e98c6961SEric Blake QString *name; 297e98c6961SEric Blake QString *value; 298e98c6961SEric Blake const char *key; 299e98c6961SEric Blake size_t remaining; 300fab5cf59SJosh Durgin int ret = 0; 301fab5cf59SJosh Durgin 302e98c6961SEric Blake if (!keypairs_json) { 303e98c6961SEric Blake return ret; 304fab5cf59SJosh Durgin } 3057dc847ebSMax Reitz keypairs = qobject_to(QList, 3067dc847ebSMax Reitz qobject_from_json(keypairs_json, &error_abort)); 307e98c6961SEric Blake remaining = qlist_size(keypairs) / 2; 308e98c6961SEric Blake assert(remaining); 309fab5cf59SJosh Durgin 310e98c6961SEric Blake while (remaining--) { 3117dc847ebSMax Reitz name = qobject_to(QString, qlist_pop(keypairs)); 3127dc847ebSMax Reitz value = qobject_to(QString, qlist_pop(keypairs)); 313e98c6961SEric Blake assert(name && value); 314e98c6961SEric Blake key = qstring_get_str(name); 315fab5cf59SJosh Durgin 316e98c6961SEric Blake ret = rados_conf_set(cluster, key, qstring_get_str(value)); 317cb3e7f08SMarc-André Lureau qobject_unref(value); 318fab5cf59SJosh Durgin if (ret < 0) { 319e98c6961SEric Blake error_setg_errno(errp, -ret, "invalid conf option %s", key); 320cb3e7f08SMarc-André Lureau qobject_unref(name); 321fab5cf59SJosh Durgin ret = -EINVAL; 322fab5cf59SJosh Durgin break; 323fab5cf59SJosh Durgin } 324cb3e7f08SMarc-André Lureau qobject_unref(name); 325fab5cf59SJosh Durgin } 326fab5cf59SJosh Durgin 327cb3e7f08SMarc-André Lureau qobject_unref(keypairs); 328fab5cf59SJosh Durgin return ret; 329fab5cf59SJosh Durgin } 330fab5cf59SJosh Durgin 33142e4ac9eSOr Ozeri #ifdef LIBRBD_SUPPORTS_ENCRYPTION 33242e4ac9eSOr Ozeri static int qemu_rbd_convert_luks_options( 33342e4ac9eSOr Ozeri RbdEncryptionOptionsLUKSBase *luks_opts, 33442e4ac9eSOr Ozeri char **passphrase, 33542e4ac9eSOr Ozeri size_t *passphrase_len, 33642e4ac9eSOr Ozeri Error **errp) 33742e4ac9eSOr Ozeri { 33842e4ac9eSOr Ozeri return qcrypto_secret_lookup(luks_opts->key_secret, (uint8_t **)passphrase, 33942e4ac9eSOr Ozeri passphrase_len, errp); 34042e4ac9eSOr Ozeri } 34142e4ac9eSOr Ozeri 34242e4ac9eSOr Ozeri static int qemu_rbd_convert_luks_create_options( 34342e4ac9eSOr Ozeri RbdEncryptionCreateOptionsLUKSBase *luks_opts, 34442e4ac9eSOr Ozeri rbd_encryption_algorithm_t *alg, 34542e4ac9eSOr Ozeri char **passphrase, 34642e4ac9eSOr Ozeri size_t *passphrase_len, 34742e4ac9eSOr Ozeri Error **errp) 34842e4ac9eSOr Ozeri { 34942e4ac9eSOr Ozeri int r = 0; 35042e4ac9eSOr Ozeri 35142e4ac9eSOr Ozeri r = qemu_rbd_convert_luks_options( 35242e4ac9eSOr Ozeri qapi_RbdEncryptionCreateOptionsLUKSBase_base(luks_opts), 35342e4ac9eSOr Ozeri passphrase, passphrase_len, errp); 35442e4ac9eSOr Ozeri if (r < 0) { 35542e4ac9eSOr Ozeri return r; 35642e4ac9eSOr Ozeri } 35742e4ac9eSOr Ozeri 35842e4ac9eSOr Ozeri if (luks_opts->has_cipher_alg) { 35942e4ac9eSOr Ozeri switch (luks_opts->cipher_alg) { 36042e4ac9eSOr Ozeri case QCRYPTO_CIPHER_ALG_AES_128: { 36142e4ac9eSOr Ozeri *alg = RBD_ENCRYPTION_ALGORITHM_AES128; 36242e4ac9eSOr Ozeri break; 36342e4ac9eSOr Ozeri } 36442e4ac9eSOr Ozeri case QCRYPTO_CIPHER_ALG_AES_256: { 36542e4ac9eSOr Ozeri *alg = RBD_ENCRYPTION_ALGORITHM_AES256; 36642e4ac9eSOr Ozeri break; 36742e4ac9eSOr Ozeri } 36842e4ac9eSOr Ozeri default: { 36942e4ac9eSOr Ozeri r = -ENOTSUP; 37042e4ac9eSOr Ozeri error_setg_errno(errp, -r, "unknown encryption algorithm: %u", 37142e4ac9eSOr Ozeri luks_opts->cipher_alg); 37242e4ac9eSOr Ozeri return r; 37342e4ac9eSOr Ozeri } 37442e4ac9eSOr Ozeri } 37542e4ac9eSOr Ozeri } else { 37642e4ac9eSOr Ozeri /* default alg */ 37742e4ac9eSOr Ozeri *alg = RBD_ENCRYPTION_ALGORITHM_AES256; 37842e4ac9eSOr Ozeri } 37942e4ac9eSOr Ozeri 38042e4ac9eSOr Ozeri return 0; 38142e4ac9eSOr Ozeri } 38242e4ac9eSOr Ozeri 38342e4ac9eSOr Ozeri static int qemu_rbd_encryption_format(rbd_image_t image, 38442e4ac9eSOr Ozeri RbdEncryptionCreateOptions *encrypt, 38542e4ac9eSOr Ozeri Error **errp) 38642e4ac9eSOr Ozeri { 38742e4ac9eSOr Ozeri int r = 0; 38842e4ac9eSOr Ozeri g_autofree char *passphrase = NULL; 38942e4ac9eSOr Ozeri rbd_encryption_format_t format; 39042e4ac9eSOr Ozeri rbd_encryption_options_t opts; 39142e4ac9eSOr Ozeri rbd_encryption_luks1_format_options_t luks_opts; 39242e4ac9eSOr Ozeri rbd_encryption_luks2_format_options_t luks2_opts; 39342e4ac9eSOr Ozeri size_t opts_size; 39442e4ac9eSOr Ozeri uint64_t raw_size, effective_size; 39542e4ac9eSOr Ozeri 39642e4ac9eSOr Ozeri r = rbd_get_size(image, &raw_size); 39742e4ac9eSOr Ozeri if (r < 0) { 39842e4ac9eSOr Ozeri error_setg_errno(errp, -r, "cannot get raw image size"); 39942e4ac9eSOr Ozeri return r; 40042e4ac9eSOr Ozeri } 40142e4ac9eSOr Ozeri 40242e4ac9eSOr Ozeri switch (encrypt->format) { 40342e4ac9eSOr Ozeri case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS: { 40442e4ac9eSOr Ozeri memset(&luks_opts, 0, sizeof(luks_opts)); 40542e4ac9eSOr Ozeri format = RBD_ENCRYPTION_FORMAT_LUKS1; 40642e4ac9eSOr Ozeri opts = &luks_opts; 40742e4ac9eSOr Ozeri opts_size = sizeof(luks_opts); 40842e4ac9eSOr Ozeri r = qemu_rbd_convert_luks_create_options( 40942e4ac9eSOr Ozeri qapi_RbdEncryptionCreateOptionsLUKS_base(&encrypt->u.luks), 410a4ac51acSOr Ozeri &luks_opts.alg, &passphrase, &luks_opts.passphrase_size, 411a4ac51acSOr Ozeri errp); 41242e4ac9eSOr Ozeri if (r < 0) { 41342e4ac9eSOr Ozeri return r; 41442e4ac9eSOr Ozeri } 41542e4ac9eSOr Ozeri luks_opts.passphrase = passphrase; 41642e4ac9eSOr Ozeri break; 41742e4ac9eSOr Ozeri } 41842e4ac9eSOr Ozeri case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2: { 41942e4ac9eSOr Ozeri memset(&luks2_opts, 0, sizeof(luks2_opts)); 42042e4ac9eSOr Ozeri format = RBD_ENCRYPTION_FORMAT_LUKS2; 42142e4ac9eSOr Ozeri opts = &luks2_opts; 42242e4ac9eSOr Ozeri opts_size = sizeof(luks2_opts); 42342e4ac9eSOr Ozeri r = qemu_rbd_convert_luks_create_options( 42442e4ac9eSOr Ozeri qapi_RbdEncryptionCreateOptionsLUKS2_base( 42542e4ac9eSOr Ozeri &encrypt->u.luks2), 426a4ac51acSOr Ozeri &luks2_opts.alg, &passphrase, &luks2_opts.passphrase_size, 427a4ac51acSOr Ozeri errp); 42842e4ac9eSOr Ozeri if (r < 0) { 42942e4ac9eSOr Ozeri return r; 43042e4ac9eSOr Ozeri } 43142e4ac9eSOr Ozeri luks2_opts.passphrase = passphrase; 43242e4ac9eSOr Ozeri break; 43342e4ac9eSOr Ozeri } 43442e4ac9eSOr Ozeri default: { 43542e4ac9eSOr Ozeri r = -ENOTSUP; 43642e4ac9eSOr Ozeri error_setg_errno( 43742e4ac9eSOr Ozeri errp, -r, "unknown image encryption format: %u", 43842e4ac9eSOr Ozeri encrypt->format); 43942e4ac9eSOr Ozeri return r; 44042e4ac9eSOr Ozeri } 44142e4ac9eSOr Ozeri } 44242e4ac9eSOr Ozeri 44342e4ac9eSOr Ozeri r = rbd_encryption_format(image, format, opts, opts_size); 44442e4ac9eSOr Ozeri if (r < 0) { 44542e4ac9eSOr Ozeri error_setg_errno(errp, -r, "encryption format fail"); 44642e4ac9eSOr Ozeri return r; 44742e4ac9eSOr Ozeri } 44842e4ac9eSOr Ozeri 44942e4ac9eSOr Ozeri r = rbd_get_size(image, &effective_size); 45042e4ac9eSOr Ozeri if (r < 0) { 45142e4ac9eSOr Ozeri error_setg_errno(errp, -r, "cannot get effective image size"); 45242e4ac9eSOr Ozeri return r; 45342e4ac9eSOr Ozeri } 45442e4ac9eSOr Ozeri 45542e4ac9eSOr Ozeri r = rbd_resize(image, raw_size + (raw_size - effective_size)); 45642e4ac9eSOr Ozeri if (r < 0) { 45742e4ac9eSOr Ozeri error_setg_errno(errp, -r, "cannot resize image after format"); 45842e4ac9eSOr Ozeri return r; 45942e4ac9eSOr Ozeri } 46042e4ac9eSOr Ozeri 46142e4ac9eSOr Ozeri return 0; 46242e4ac9eSOr Ozeri } 46342e4ac9eSOr Ozeri 46442e4ac9eSOr Ozeri static int qemu_rbd_encryption_load(rbd_image_t image, 46542e4ac9eSOr Ozeri RbdEncryptionOptions *encrypt, 46642e4ac9eSOr Ozeri Error **errp) 46742e4ac9eSOr Ozeri { 46842e4ac9eSOr Ozeri int r = 0; 46942e4ac9eSOr Ozeri g_autofree char *passphrase = NULL; 47042e4ac9eSOr Ozeri rbd_encryption_luks1_format_options_t luks_opts; 47142e4ac9eSOr Ozeri rbd_encryption_luks2_format_options_t luks2_opts; 472*b8f218efSOr Ozeri #ifdef LIBRBD_SUPPORTS_ENCRYPTION_LOAD2 473*b8f218efSOr Ozeri rbd_encryption_luks_format_options_t luks_any_opts; 474*b8f218efSOr Ozeri #endif 47542e4ac9eSOr Ozeri rbd_encryption_format_t format; 47642e4ac9eSOr Ozeri rbd_encryption_options_t opts; 47742e4ac9eSOr Ozeri size_t opts_size; 47842e4ac9eSOr Ozeri 47942e4ac9eSOr Ozeri switch (encrypt->format) { 48042e4ac9eSOr Ozeri case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS: { 48142e4ac9eSOr Ozeri memset(&luks_opts, 0, sizeof(luks_opts)); 48242e4ac9eSOr Ozeri format = RBD_ENCRYPTION_FORMAT_LUKS1; 48342e4ac9eSOr Ozeri opts = &luks_opts; 48442e4ac9eSOr Ozeri opts_size = sizeof(luks_opts); 48542e4ac9eSOr Ozeri r = qemu_rbd_convert_luks_options( 48642e4ac9eSOr Ozeri qapi_RbdEncryptionOptionsLUKS_base(&encrypt->u.luks), 487a4ac51acSOr Ozeri &passphrase, &luks_opts.passphrase_size, errp); 48842e4ac9eSOr Ozeri if (r < 0) { 48942e4ac9eSOr Ozeri return r; 49042e4ac9eSOr Ozeri } 49142e4ac9eSOr Ozeri luks_opts.passphrase = passphrase; 49242e4ac9eSOr Ozeri break; 49342e4ac9eSOr Ozeri } 49442e4ac9eSOr Ozeri case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2: { 49542e4ac9eSOr Ozeri memset(&luks2_opts, 0, sizeof(luks2_opts)); 49642e4ac9eSOr Ozeri format = RBD_ENCRYPTION_FORMAT_LUKS2; 49742e4ac9eSOr Ozeri opts = &luks2_opts; 49842e4ac9eSOr Ozeri opts_size = sizeof(luks2_opts); 49942e4ac9eSOr Ozeri r = qemu_rbd_convert_luks_options( 50042e4ac9eSOr Ozeri qapi_RbdEncryptionOptionsLUKS2_base(&encrypt->u.luks2), 501a4ac51acSOr Ozeri &passphrase, &luks2_opts.passphrase_size, errp); 50242e4ac9eSOr Ozeri if (r < 0) { 50342e4ac9eSOr Ozeri return r; 50442e4ac9eSOr Ozeri } 50542e4ac9eSOr Ozeri luks2_opts.passphrase = passphrase; 50642e4ac9eSOr Ozeri break; 50742e4ac9eSOr Ozeri } 508*b8f218efSOr Ozeri #ifdef LIBRBD_SUPPORTS_ENCRYPTION_LOAD2 509*b8f218efSOr Ozeri case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS_ANY: { 510*b8f218efSOr Ozeri memset(&luks_any_opts, 0, sizeof(luks_any_opts)); 511*b8f218efSOr Ozeri format = RBD_ENCRYPTION_FORMAT_LUKS; 512*b8f218efSOr Ozeri opts = &luks_any_opts; 513*b8f218efSOr Ozeri opts_size = sizeof(luks_any_opts); 514*b8f218efSOr Ozeri r = qemu_rbd_convert_luks_options( 515*b8f218efSOr Ozeri qapi_RbdEncryptionOptionsLUKSAny_base(&encrypt->u.luks_any), 516*b8f218efSOr Ozeri &passphrase, &luks_any_opts.passphrase_size, errp); 517*b8f218efSOr Ozeri if (r < 0) { 518*b8f218efSOr Ozeri return r; 519*b8f218efSOr Ozeri } 520*b8f218efSOr Ozeri luks_any_opts.passphrase = passphrase; 521*b8f218efSOr Ozeri break; 522*b8f218efSOr Ozeri } 523*b8f218efSOr Ozeri #endif 52442e4ac9eSOr Ozeri default: { 52542e4ac9eSOr Ozeri r = -ENOTSUP; 52642e4ac9eSOr Ozeri error_setg_errno( 52742e4ac9eSOr Ozeri errp, -r, "unknown image encryption format: %u", 52842e4ac9eSOr Ozeri encrypt->format); 52942e4ac9eSOr Ozeri return r; 53042e4ac9eSOr Ozeri } 53142e4ac9eSOr Ozeri } 53242e4ac9eSOr Ozeri 53342e4ac9eSOr Ozeri r = rbd_encryption_load(image, format, opts, opts_size); 53442e4ac9eSOr Ozeri if (r < 0) { 53542e4ac9eSOr Ozeri error_setg_errno(errp, -r, "encryption load fail"); 53642e4ac9eSOr Ozeri return r; 53742e4ac9eSOr Ozeri } 53842e4ac9eSOr Ozeri 53942e4ac9eSOr Ozeri return 0; 54042e4ac9eSOr Ozeri } 54142e4ac9eSOr Ozeri #endif 54242e4ac9eSOr Ozeri 543d083f954SMarkus Armbruster /* FIXME Deprecate and remove keypairs or make it available in QMP. */ 5441bebea37SKevin Wolf static int qemu_rbd_do_create(BlockdevCreateOptions *options, 5451bebea37SKevin Wolf const char *keypairs, const char *password_secret, 5461bebea37SKevin Wolf Error **errp) 5471bebea37SKevin Wolf { 5481bebea37SKevin Wolf BlockdevCreateOptionsRbd *opts = &options->u.rbd; 5491bebea37SKevin Wolf rados_t cluster; 5501bebea37SKevin Wolf rados_ioctx_t io_ctx; 5511bebea37SKevin Wolf int obj_order = 0; 5521bebea37SKevin Wolf int ret; 5531bebea37SKevin Wolf 5541bebea37SKevin Wolf assert(options->driver == BLOCKDEV_DRIVER_RBD); 55554fde4ffSMarkus Armbruster if (opts->location->snapshot) { 5561bebea37SKevin Wolf error_setg(errp, "Can't use snapshot name for image creation"); 5571bebea37SKevin Wolf return -EINVAL; 5581bebea37SKevin Wolf } 5591bebea37SKevin Wolf 56042e4ac9eSOr Ozeri #ifndef LIBRBD_SUPPORTS_ENCRYPTION 56154fde4ffSMarkus Armbruster if (opts->encrypt) { 56242e4ac9eSOr Ozeri error_setg(errp, "RBD library does not support image encryption"); 56342e4ac9eSOr Ozeri return -ENOTSUP; 56442e4ac9eSOr Ozeri } 56542e4ac9eSOr Ozeri #endif 56642e4ac9eSOr Ozeri 5671bebea37SKevin Wolf if (opts->has_cluster_size) { 5681bebea37SKevin Wolf int64_t objsize = opts->cluster_size; 5691bebea37SKevin Wolf if ((objsize - 1) & objsize) { /* not a power of 2? */ 5701bebea37SKevin Wolf error_setg(errp, "obj size needs to be power of 2"); 5711bebea37SKevin Wolf return -EINVAL; 5721bebea37SKevin Wolf } 5731bebea37SKevin Wolf if (objsize < 4096) { 5741bebea37SKevin Wolf error_setg(errp, "obj size too small"); 5751bebea37SKevin Wolf return -EINVAL; 5761bebea37SKevin Wolf } 5771bebea37SKevin Wolf obj_order = ctz32(objsize); 5781bebea37SKevin Wolf } 5791bebea37SKevin Wolf 580aa045c2dSKevin Wolf ret = qemu_rbd_connect(&cluster, &io_ctx, opts->location, false, keypairs, 581aa045c2dSKevin Wolf password_secret, errp); 5821bebea37SKevin Wolf if (ret < 0) { 5831bebea37SKevin Wolf return ret; 5841bebea37SKevin Wolf } 5851bebea37SKevin Wolf 5861bebea37SKevin Wolf ret = rbd_create(io_ctx, opts->location->image, opts->size, &obj_order); 5871bebea37SKevin Wolf if (ret < 0) { 5881bebea37SKevin Wolf error_setg_errno(errp, -ret, "error rbd create"); 589aa045c2dSKevin Wolf goto out; 5901bebea37SKevin Wolf } 5911bebea37SKevin Wolf 59242e4ac9eSOr Ozeri #ifdef LIBRBD_SUPPORTS_ENCRYPTION 59354fde4ffSMarkus Armbruster if (opts->encrypt) { 59442e4ac9eSOr Ozeri rbd_image_t image; 59542e4ac9eSOr Ozeri 59642e4ac9eSOr Ozeri ret = rbd_open(io_ctx, opts->location->image, &image, NULL); 59742e4ac9eSOr Ozeri if (ret < 0) { 59842e4ac9eSOr Ozeri error_setg_errno(errp, -ret, 59942e4ac9eSOr Ozeri "error opening image '%s' for encryption format", 60042e4ac9eSOr Ozeri opts->location->image); 60142e4ac9eSOr Ozeri goto out; 60242e4ac9eSOr Ozeri } 60342e4ac9eSOr Ozeri 60442e4ac9eSOr Ozeri ret = qemu_rbd_encryption_format(image, opts->encrypt, errp); 60542e4ac9eSOr Ozeri rbd_close(image); 60642e4ac9eSOr Ozeri if (ret < 0) { 60742e4ac9eSOr Ozeri /* encryption format fail, try removing the image */ 60842e4ac9eSOr Ozeri rbd_remove(io_ctx, opts->location->image); 60942e4ac9eSOr Ozeri goto out; 61042e4ac9eSOr Ozeri } 61142e4ac9eSOr Ozeri } 61242e4ac9eSOr Ozeri #endif 61342e4ac9eSOr Ozeri 6141bebea37SKevin Wolf ret = 0; 615aa045c2dSKevin Wolf out: 616aa045c2dSKevin Wolf rados_ioctx_destroy(io_ctx); 6171bebea37SKevin Wolf rados_shutdown(cluster); 6181bebea37SKevin Wolf return ret; 6191bebea37SKevin Wolf } 6201bebea37SKevin Wolf 6211bebea37SKevin Wolf static int qemu_rbd_co_create(BlockdevCreateOptions *options, Error **errp) 6221bebea37SKevin Wolf { 6231bebea37SKevin Wolf return qemu_rbd_do_create(options, NULL, NULL, errp); 6241bebea37SKevin Wolf } 6251bebea37SKevin Wolf 62642e4ac9eSOr Ozeri static int qemu_rbd_extract_encryption_create_options( 62742e4ac9eSOr Ozeri QemuOpts *opts, 62842e4ac9eSOr Ozeri RbdEncryptionCreateOptions **spec, 62942e4ac9eSOr Ozeri Error **errp) 63042e4ac9eSOr Ozeri { 63142e4ac9eSOr Ozeri QDict *opts_qdict; 63242e4ac9eSOr Ozeri QDict *encrypt_qdict; 63342e4ac9eSOr Ozeri Visitor *v; 63442e4ac9eSOr Ozeri int ret = 0; 63542e4ac9eSOr Ozeri 63642e4ac9eSOr Ozeri opts_qdict = qemu_opts_to_qdict(opts, NULL); 63742e4ac9eSOr Ozeri qdict_extract_subqdict(opts_qdict, &encrypt_qdict, "encrypt."); 63842e4ac9eSOr Ozeri qobject_unref(opts_qdict); 63942e4ac9eSOr Ozeri if (!qdict_size(encrypt_qdict)) { 64042e4ac9eSOr Ozeri *spec = NULL; 64142e4ac9eSOr Ozeri goto exit; 64242e4ac9eSOr Ozeri } 64342e4ac9eSOr Ozeri 64442e4ac9eSOr Ozeri /* Convert options into a QAPI object */ 64542e4ac9eSOr Ozeri v = qobject_input_visitor_new_flat_confused(encrypt_qdict, errp); 64642e4ac9eSOr Ozeri if (!v) { 64742e4ac9eSOr Ozeri ret = -EINVAL; 64842e4ac9eSOr Ozeri goto exit; 64942e4ac9eSOr Ozeri } 65042e4ac9eSOr Ozeri 65142e4ac9eSOr Ozeri visit_type_RbdEncryptionCreateOptions(v, NULL, spec, errp); 65242e4ac9eSOr Ozeri visit_free(v); 65342e4ac9eSOr Ozeri if (!*spec) { 65442e4ac9eSOr Ozeri ret = -EINVAL; 65542e4ac9eSOr Ozeri goto exit; 65642e4ac9eSOr Ozeri } 65742e4ac9eSOr Ozeri 65842e4ac9eSOr Ozeri exit: 65942e4ac9eSOr Ozeri qobject_unref(encrypt_qdict); 66042e4ac9eSOr Ozeri return ret; 66142e4ac9eSOr Ozeri } 66242e4ac9eSOr Ozeri 663b92902dfSMaxim Levitsky static int coroutine_fn qemu_rbd_co_create_opts(BlockDriver *drv, 664b92902dfSMaxim Levitsky const char *filename, 665efc75e2aSStefan Hajnoczi QemuOpts *opts, 666efc75e2aSStefan Hajnoczi Error **errp) 667f27aaf4bSChristian Brunner { 6681bebea37SKevin Wolf BlockdevCreateOptions *create_options; 6691bebea37SKevin Wolf BlockdevCreateOptionsRbd *rbd_opts; 6701bebea37SKevin Wolf BlockdevOptionsRbd *loc; 67142e4ac9eSOr Ozeri RbdEncryptionCreateOptions *encrypt = NULL; 672d61563b2SMarkus Armbruster Error *local_err = NULL; 6731bebea37SKevin Wolf const char *keypairs, *password_secret; 674c7cacb3eSJeff Cody QDict *options = NULL; 675c7cacb3eSJeff Cody int ret = 0; 676f27aaf4bSChristian Brunner 6771bebea37SKevin Wolf create_options = g_new0(BlockdevCreateOptions, 1); 6781bebea37SKevin Wolf create_options->driver = BLOCKDEV_DRIVER_RBD; 6791bebea37SKevin Wolf rbd_opts = &create_options->u.rbd; 6801bebea37SKevin Wolf 6811bebea37SKevin Wolf rbd_opts->location = g_new0(BlockdevOptionsRbd, 1); 6821bebea37SKevin Wolf 6831bebea37SKevin Wolf password_secret = qemu_opt_get(opts, "password-secret"); 68460390a21SDaniel P. Berrange 685f27aaf4bSChristian Brunner /* Read out options */ 6861bebea37SKevin Wolf rbd_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 687c2eb918eSHu Tao BDRV_SECTOR_SIZE); 6881bebea37SKevin Wolf rbd_opts->cluster_size = qemu_opt_get_size_del(opts, 6891bebea37SKevin Wolf BLOCK_OPT_CLUSTER_SIZE, 0); 6901bebea37SKevin Wolf rbd_opts->has_cluster_size = (rbd_opts->cluster_size != 0); 691f27aaf4bSChristian Brunner 692c7cacb3eSJeff Cody options = qdict_new(); 693c7cacb3eSJeff Cody qemu_rbd_parse_filename(filename, options, &local_err); 694c7cacb3eSJeff Cody if (local_err) { 695c7cacb3eSJeff Cody ret = -EINVAL; 696c7cacb3eSJeff Cody error_propagate(errp, local_err); 697c7cacb3eSJeff Cody goto exit; 698c7cacb3eSJeff Cody } 699c7cacb3eSJeff Cody 70042e4ac9eSOr Ozeri ret = qemu_rbd_extract_encryption_create_options(opts, &encrypt, errp); 70142e4ac9eSOr Ozeri if (ret < 0) { 70242e4ac9eSOr Ozeri goto exit; 70342e4ac9eSOr Ozeri } 70442e4ac9eSOr Ozeri rbd_opts->encrypt = encrypt; 70542e4ac9eSOr Ozeri 706129c7d1cSMarkus Armbruster /* 707129c7d1cSMarkus Armbruster * Caution: while qdict_get_try_str() is fine, getting non-string 708129c7d1cSMarkus Armbruster * types would require more care. When @options come from -blockdev 709129c7d1cSMarkus Armbruster * or blockdev_add, its members are typed according to the QAPI 710129c7d1cSMarkus Armbruster * schema, but when they come from -drive, they're all QString. 711129c7d1cSMarkus Armbruster */ 7121bebea37SKevin Wolf loc = rbd_opts->location; 7131bebea37SKevin Wolf loc->pool = g_strdup(qdict_get_try_str(options, "pool")); 7141bebea37SKevin Wolf loc->conf = g_strdup(qdict_get_try_str(options, "conf")); 7151bebea37SKevin Wolf loc->user = g_strdup(qdict_get_try_str(options, "user")); 71619ae9ae0SFlorian Florensa loc->q_namespace = g_strdup(qdict_get_try_str(options, "namespace")); 7171bebea37SKevin Wolf loc->image = g_strdup(qdict_get_try_str(options, "image")); 71807846397SMarkus Armbruster keypairs = qdict_get_try_str(options, "=keyvalue-pairs"); 719c7cacb3eSJeff Cody 7201bebea37SKevin Wolf ret = qemu_rbd_do_create(create_options, keypairs, password_secret, errp); 72187cd3d20SVikhyat Umrao if (ret < 0) { 722c7cacb3eSJeff Cody goto exit; 723f27aaf4bSChristian Brunner } 724f27aaf4bSChristian Brunner 725c7cacb3eSJeff Cody exit: 726cb3e7f08SMarc-André Lureau qobject_unref(options); 7271bebea37SKevin Wolf qapi_free_BlockdevCreateOptions(create_options); 728f27aaf4bSChristian Brunner return ret; 729f27aaf4bSChristian Brunner } 730f27aaf4bSChristian Brunner 7314bfb2741SKevin Wolf static char *qemu_rbd_mon_host(BlockdevOptionsRbd *opts, Error **errp) 7320a55679bSJeff Cody { 7334bfb2741SKevin Wolf const char **vals; 7342836284dSMarkus Armbruster const char *host, *port; 7352836284dSMarkus Armbruster char *rados_str; 7364bfb2741SKevin Wolf InetSocketAddressBaseList *p; 7374bfb2741SKevin Wolf int i, cnt; 7380a55679bSJeff Cody 7394bfb2741SKevin Wolf if (!opts->has_server) { 7404bfb2741SKevin Wolf return NULL; 7410a55679bSJeff Cody } 7424bfb2741SKevin Wolf 7434bfb2741SKevin Wolf for (cnt = 0, p = opts->server; p; p = p->next) { 7444bfb2741SKevin Wolf cnt++; 7450a55679bSJeff Cody } 7460a55679bSJeff Cody 7474bfb2741SKevin Wolf vals = g_new(const char *, cnt + 1); 7484bfb2741SKevin Wolf 7494bfb2741SKevin Wolf for (i = 0, p = opts->server; p; p = p->next, i++) { 7504bfb2741SKevin Wolf host = p->value->host; 7514bfb2741SKevin Wolf port = p->value->port; 7524bfb2741SKevin Wolf 7530a55679bSJeff Cody if (strchr(host, ':')) { 7544bfb2741SKevin Wolf vals[i] = g_strdup_printf("[%s]:%s", host, port); 7550a55679bSJeff Cody } else { 7564bfb2741SKevin Wolf vals[i] = g_strdup_printf("%s:%s", host, port); 7570a55679bSJeff Cody } 7580a55679bSJeff Cody } 7592836284dSMarkus Armbruster vals[i] = NULL; 7600a55679bSJeff Cody 7612836284dSMarkus Armbruster rados_str = i ? g_strjoinv(";", (char **)vals) : NULL; 7622836284dSMarkus Armbruster g_strfreev((char **)vals); 7630a55679bSJeff Cody return rados_str; 7640a55679bSJeff Cody } 7650a55679bSJeff Cody 7663d9136f9SKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx, 7674bfb2741SKevin Wolf BlockdevOptionsRbd *opts, bool cache, 7684ff45049SKevin Wolf const char *keypairs, const char *secretid, 7694ff45049SKevin Wolf Error **errp) 770f27aaf4bSChristian Brunner { 7710a55679bSJeff Cody char *mon_host = NULL; 7723d9136f9SKevin Wolf Error *local_err = NULL; 773f27aaf4bSChristian Brunner int r; 774f27aaf4bSChristian Brunner 775d083f954SMarkus Armbruster if (secretid) { 776d083f954SMarkus Armbruster if (opts->key_secret) { 777d083f954SMarkus Armbruster error_setg(errp, 778d083f954SMarkus Armbruster "Legacy 'password-secret' clashes with 'key-secret'"); 779d083f954SMarkus Armbruster return -EINVAL; 780d083f954SMarkus Armbruster } 781d083f954SMarkus Armbruster opts->key_secret = g_strdup(secretid); 782d083f954SMarkus Armbruster } 783d083f954SMarkus Armbruster 7844bfb2741SKevin Wolf mon_host = qemu_rbd_mon_host(opts, &local_err); 78584d18f06SMarkus Armbruster if (local_err) { 786d61563b2SMarkus Armbruster error_propagate(errp, local_err); 7872836284dSMarkus Armbruster r = -EINVAL; 788c1c1f6cfSStefano Garzarella goto out; 789a9ccedc3SKevin Wolf } 790a9ccedc3SKevin Wolf 7914bfb2741SKevin Wolf r = rados_create(cluster, opts->user); 792ad32e9c0SJosh Durgin if (r < 0) { 79387cd3d20SVikhyat Umrao error_setg_errno(errp, -r, "error initializing"); 794c1c1f6cfSStefano Garzarella goto out; 795f27aaf4bSChristian Brunner } 796f27aaf4bSChristian Brunner 797c7cacb3eSJeff Cody /* try default location when conf=NULL, but ignore failure */ 7984bfb2741SKevin Wolf r = rados_conf_read_file(*cluster, opts->conf); 79954fde4ffSMarkus Armbruster if (opts->conf && r < 0) { 8004bfb2741SKevin Wolf error_setg_errno(errp, -r, "error reading conf file %s", opts->conf); 801e34d8f29SJosh Durgin goto failed_shutdown; 802e34d8f29SJosh Durgin } 80399a3c89dSJosh Durgin 8043d9136f9SKevin Wolf r = qemu_rbd_set_keypairs(*cluster, keypairs, errp); 80599a3c89dSJosh Durgin if (r < 0) { 80699a3c89dSJosh Durgin goto failed_shutdown; 80799a3c89dSJosh Durgin } 80899a3c89dSJosh Durgin 8090a55679bSJeff Cody if (mon_host) { 8103d9136f9SKevin Wolf r = rados_conf_set(*cluster, "mon_host", mon_host); 8110a55679bSJeff Cody if (r < 0) { 8120a55679bSJeff Cody goto failed_shutdown; 8130a55679bSJeff Cody } 8140a55679bSJeff Cody } 8150a55679bSJeff Cody 816d083f954SMarkus Armbruster r = qemu_rbd_set_auth(*cluster, opts, errp); 817d083f954SMarkus Armbruster if (r < 0) { 81860390a21SDaniel P. Berrange goto failed_shutdown; 81960390a21SDaniel P. Berrange } 82060390a21SDaniel P. Berrange 821b11f38fcSJosh Durgin /* 822b11f38fcSJosh Durgin * Fallback to more conservative semantics if setting cache 823b11f38fcSJosh Durgin * options fails. Ignore errors from setting rbd_cache because the 824b11f38fcSJosh Durgin * only possible error is that the option does not exist, and 825b11f38fcSJosh Durgin * librbd defaults to no caching. If write through caching cannot 826b11f38fcSJosh Durgin * be set up, fall back to no caching. 827b11f38fcSJosh Durgin */ 8283d9136f9SKevin Wolf if (cache) { 8293d9136f9SKevin Wolf rados_conf_set(*cluster, "rbd_cache", "true"); 830b11f38fcSJosh Durgin } else { 8313d9136f9SKevin Wolf rados_conf_set(*cluster, "rbd_cache", "false"); 832b11f38fcSJosh Durgin } 833b11f38fcSJosh Durgin 8343d9136f9SKevin Wolf r = rados_connect(*cluster); 835ad32e9c0SJosh Durgin if (r < 0) { 83687cd3d20SVikhyat Umrao error_setg_errno(errp, -r, "error connecting"); 837eb93d5d9SSage Weil goto failed_shutdown; 838ad32e9c0SJosh Durgin } 839ad32e9c0SJosh Durgin 8404bfb2741SKevin Wolf r = rados_ioctx_create(*cluster, opts->pool, io_ctx); 841ad32e9c0SJosh Durgin if (r < 0) { 8424bfb2741SKevin Wolf error_setg_errno(errp, -r, "error opening pool %s", opts->pool); 843eb93d5d9SSage Weil goto failed_shutdown; 844ad32e9c0SJosh Durgin } 84566dc5f96SStefano Garzarella 84666dc5f96SStefano Garzarella #ifdef HAVE_RBD_NAMESPACE_EXISTS 84754fde4ffSMarkus Armbruster if (opts->q_namespace && strlen(opts->q_namespace) > 0) { 84866dc5f96SStefano Garzarella bool exists; 84966dc5f96SStefano Garzarella 85066dc5f96SStefano Garzarella r = rbd_namespace_exists(*io_ctx, opts->q_namespace, &exists); 85166dc5f96SStefano Garzarella if (r < 0) { 85266dc5f96SStefano Garzarella error_setg_errno(errp, -r, "error checking namespace"); 85366dc5f96SStefano Garzarella goto failed_ioctx_destroy; 85466dc5f96SStefano Garzarella } 85566dc5f96SStefano Garzarella 85666dc5f96SStefano Garzarella if (!exists) { 85766dc5f96SStefano Garzarella error_setg(errp, "namespace '%s' does not exist", 85866dc5f96SStefano Garzarella opts->q_namespace); 85966dc5f96SStefano Garzarella r = -ENOENT; 86066dc5f96SStefano Garzarella goto failed_ioctx_destroy; 86166dc5f96SStefano Garzarella } 86266dc5f96SStefano Garzarella } 86366dc5f96SStefano Garzarella #endif 86466dc5f96SStefano Garzarella 86519ae9ae0SFlorian Florensa /* 86619ae9ae0SFlorian Florensa * Set the namespace after opening the io context on the pool, 86719ae9ae0SFlorian Florensa * if nspace == NULL or if nspace == "", it is just as we did nothing 86819ae9ae0SFlorian Florensa */ 86919ae9ae0SFlorian Florensa rados_ioctx_set_namespace(*io_ctx, opts->q_namespace); 870ad32e9c0SJosh Durgin 871c1c1f6cfSStefano Garzarella r = 0; 872c1c1f6cfSStefano Garzarella goto out; 8733d9136f9SKevin Wolf 87466dc5f96SStefano Garzarella #ifdef HAVE_RBD_NAMESPACE_EXISTS 87566dc5f96SStefano Garzarella failed_ioctx_destroy: 87666dc5f96SStefano Garzarella rados_ioctx_destroy(*io_ctx); 87766dc5f96SStefano Garzarella #endif 8783d9136f9SKevin Wolf failed_shutdown: 8793d9136f9SKevin Wolf rados_shutdown(*cluster); 880c1c1f6cfSStefano Garzarella out: 8813d9136f9SKevin Wolf g_free(mon_host); 8823d9136f9SKevin Wolf return r; 8833d9136f9SKevin Wolf } 8843d9136f9SKevin Wolf 885f24b03b5SJeff Cody static int qemu_rbd_convert_options(QDict *options, BlockdevOptionsRbd **opts, 886f24b03b5SJeff Cody Error **errp) 887f24b03b5SJeff Cody { 888f24b03b5SJeff Cody Visitor *v; 889f24b03b5SJeff Cody 890f24b03b5SJeff Cody /* Convert the remaining options into a QAPI object */ 891f24b03b5SJeff Cody v = qobject_input_visitor_new_flat_confused(options, errp); 892f24b03b5SJeff Cody if (!v) { 893f24b03b5SJeff Cody return -EINVAL; 894f24b03b5SJeff Cody } 895f24b03b5SJeff Cody 896b11a093cSMarkus Armbruster visit_type_BlockdevOptionsRbd(v, NULL, opts, errp); 897f24b03b5SJeff Cody visit_free(v); 898b11a093cSMarkus Armbruster if (!opts) { 899f24b03b5SJeff Cody return -EINVAL; 900f24b03b5SJeff Cody } 901f24b03b5SJeff Cody 902f24b03b5SJeff Cody return 0; 903f24b03b5SJeff Cody } 904f24b03b5SJeff Cody 905084d1d13SJeff Cody static int qemu_rbd_attempt_legacy_options(QDict *options, 906084d1d13SJeff Cody BlockdevOptionsRbd **opts, 907084d1d13SJeff Cody char **keypairs) 908084d1d13SJeff Cody { 909084d1d13SJeff Cody char *filename; 910084d1d13SJeff Cody int r; 911084d1d13SJeff Cody 912084d1d13SJeff Cody filename = g_strdup(qdict_get_try_str(options, "filename")); 913084d1d13SJeff Cody if (!filename) { 914084d1d13SJeff Cody return -EINVAL; 915084d1d13SJeff Cody } 916084d1d13SJeff Cody qdict_del(options, "filename"); 917084d1d13SJeff Cody 918084d1d13SJeff Cody qemu_rbd_parse_filename(filename, options, NULL); 919084d1d13SJeff Cody 920084d1d13SJeff Cody /* keypairs freed by caller */ 921084d1d13SJeff Cody *keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs")); 922084d1d13SJeff Cody if (*keypairs) { 923084d1d13SJeff Cody qdict_del(options, "=keyvalue-pairs"); 924084d1d13SJeff Cody } 925084d1d13SJeff Cody 926084d1d13SJeff Cody r = qemu_rbd_convert_options(options, opts, NULL); 927084d1d13SJeff Cody 928084d1d13SJeff Cody g_free(filename); 929084d1d13SJeff Cody return r; 930084d1d13SJeff Cody } 931084d1d13SJeff Cody 9323d9136f9SKevin Wolf static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags, 9333d9136f9SKevin Wolf Error **errp) 9343d9136f9SKevin Wolf { 9353d9136f9SKevin Wolf BDRVRBDState *s = bs->opaque; 9364bfb2741SKevin Wolf BlockdevOptionsRbd *opts = NULL; 937bfb15b4bSJeff Cody const QDictEntry *e; 9383d9136f9SKevin Wolf Error *local_err = NULL; 9394ff45049SKevin Wolf char *keypairs, *secretid; 940832a93dcSPeter Lieven rbd_image_info_t info; 9413d9136f9SKevin Wolf int r; 9423d9136f9SKevin Wolf 9434ff45049SKevin Wolf keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs")); 9444ff45049SKevin Wolf if (keypairs) { 9454ff45049SKevin Wolf qdict_del(options, "=keyvalue-pairs"); 9464ff45049SKevin Wolf } 9474ff45049SKevin Wolf 9484ff45049SKevin Wolf secretid = g_strdup(qdict_get_try_str(options, "password-secret")); 9494ff45049SKevin Wolf if (secretid) { 9504ff45049SKevin Wolf qdict_del(options, "password-secret"); 9514ff45049SKevin Wolf } 9524ff45049SKevin Wolf 953f24b03b5SJeff Cody r = qemu_rbd_convert_options(options, &opts, &local_err); 9544bfb2741SKevin Wolf if (local_err) { 955084d1d13SJeff Cody /* If keypairs are present, that means some options are present in 956084d1d13SJeff Cody * the modern option format. Don't attempt to parse legacy option 957084d1d13SJeff Cody * formats, as we won't support mixed usage. */ 958084d1d13SJeff Cody if (keypairs) { 9594bfb2741SKevin Wolf error_propagate(errp, local_err); 9604bfb2741SKevin Wolf goto out; 9614bfb2741SKevin Wolf } 9624bfb2741SKevin Wolf 963084d1d13SJeff Cody /* If the initial attempt to convert and process the options failed, 964084d1d13SJeff Cody * we may be attempting to open an image file that has the rbd options 965084d1d13SJeff Cody * specified in the older format consisting of all key/value pairs 966084d1d13SJeff Cody * encoded in the filename. Go ahead and attempt to parse the 967084d1d13SJeff Cody * filename, and see if we can pull out the required options. */ 968084d1d13SJeff Cody r = qemu_rbd_attempt_legacy_options(options, &opts, &keypairs); 969084d1d13SJeff Cody if (r < 0) { 970084d1d13SJeff Cody /* Propagate the original error, not the legacy parsing fallback 971084d1d13SJeff Cody * error, as the latter was just a best-effort attempt. */ 972084d1d13SJeff Cody error_propagate(errp, local_err); 973084d1d13SJeff Cody goto out; 974084d1d13SJeff Cody } 975084d1d13SJeff Cody /* Take care whenever deciding to actually deprecate; once this ability 976084d1d13SJeff Cody * is removed, we will not be able to open any images with legacy-styled 977084d1d13SJeff Cody * backing image strings. */ 9785197f445SMarkus Armbruster warn_report("RBD options encoded in the filename as keyvalue pairs " 979084d1d13SJeff Cody "is deprecated"); 980084d1d13SJeff Cody } 981084d1d13SJeff Cody 982bfb15b4bSJeff Cody /* Remove the processed options from the QDict (the visitor processes 983bfb15b4bSJeff Cody * _all_ options in the QDict) */ 984bfb15b4bSJeff Cody while ((e = qdict_first(options))) { 985bfb15b4bSJeff Cody qdict_del(options, e->key); 986bfb15b4bSJeff Cody } 987bfb15b4bSJeff Cody 988d41a5588SKevin Wolf r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts, 989d41a5588SKevin Wolf !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp); 9903d9136f9SKevin Wolf if (r < 0) { 9914ff45049SKevin Wolf goto out; 9923d9136f9SKevin Wolf } 9933d9136f9SKevin Wolf 994d41a5588SKevin Wolf s->snap = g_strdup(opts->snapshot); 995d41a5588SKevin Wolf s->image_name = g_strdup(opts->image); 996d41a5588SKevin Wolf 997e2b8247aSJeff Cody /* rbd_open is always r/w */ 99880b61a27SJeff Cody r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap); 999ad32e9c0SJosh Durgin if (r < 0) { 100080b61a27SJeff Cody error_setg_errno(errp, -r, "error reading header from %s", 100180b61a27SJeff Cody s->image_name); 1002eb93d5d9SSage Weil goto failed_open; 1003ad32e9c0SJosh Durgin } 1004ad32e9c0SJosh Durgin 100554fde4ffSMarkus Armbruster if (opts->encrypt) { 100642e4ac9eSOr Ozeri #ifdef LIBRBD_SUPPORTS_ENCRYPTION 100742e4ac9eSOr Ozeri r = qemu_rbd_encryption_load(s->image, opts->encrypt, errp); 100842e4ac9eSOr Ozeri if (r < 0) { 100942e4ac9eSOr Ozeri goto failed_post_open; 101042e4ac9eSOr Ozeri } 101142e4ac9eSOr Ozeri #else 101242e4ac9eSOr Ozeri r = -ENOTSUP; 101342e4ac9eSOr Ozeri error_setg(errp, "RBD library does not support image encryption"); 101442e4ac9eSOr Ozeri goto failed_post_open; 101542e4ac9eSOr Ozeri #endif 101642e4ac9eSOr Ozeri } 101742e4ac9eSOr Ozeri 1018832a93dcSPeter Lieven r = rbd_stat(s->image, &info, sizeof(info)); 1019d24f8023SStefano Garzarella if (r < 0) { 1020832a93dcSPeter Lieven error_setg_errno(errp, -r, "error getting image info from %s", 1021d24f8023SStefano Garzarella s->image_name); 102242e4ac9eSOr Ozeri goto failed_post_open; 1023d24f8023SStefano Garzarella } 1024832a93dcSPeter Lieven s->image_size = info.size; 1025832a93dcSPeter Lieven s->object_size = info.obj_size; 1026d24f8023SStefano Garzarella 1027e2b8247aSJeff Cody /* If we are using an rbd snapshot, we must be r/o, otherwise 1028e2b8247aSJeff Cody * leave as-is */ 1029e2b8247aSJeff Cody if (s->snap != NULL) { 1030eaa2410fSKevin Wolf r = bdrv_apply_auto_read_only(bs, "rbd snapshots are read-only", errp); 1031e2b8247aSJeff Cody if (r < 0) { 103242e4ac9eSOr Ozeri goto failed_post_open; 1033e2b8247aSJeff Cody } 1034e2b8247aSJeff Cody } 1035f27aaf4bSChristian Brunner 1036c56ac27dSPeter Lieven #ifdef LIBRBD_SUPPORTS_WRITE_ZEROES 1037c56ac27dSPeter Lieven bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK; 1038c56ac27dSPeter Lieven #endif 1039c56ac27dSPeter Lieven 10402f98910dSEric Blake /* When extending regular files, we get zeros from the OS */ 10412f98910dSEric Blake bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE; 10422f98910dSEric Blake 10434ff45049SKevin Wolf r = 0; 10444ff45049SKevin Wolf goto out; 1045f27aaf4bSChristian Brunner 104642e4ac9eSOr Ozeri failed_post_open: 104742e4ac9eSOr Ozeri rbd_close(s->image); 1048eb93d5d9SSage Weil failed_open: 1049ad32e9c0SJosh Durgin rados_ioctx_destroy(s->io_ctx); 1050eb93d5d9SSage Weil g_free(s->snap); 105180b61a27SJeff Cody g_free(s->image_name); 10523d9136f9SKevin Wolf rados_shutdown(s->cluster); 10534ff45049SKevin Wolf out: 10544bfb2741SKevin Wolf qapi_free_BlockdevOptionsRbd(opts); 10554ff45049SKevin Wolf g_free(keypairs); 10564ff45049SKevin Wolf g_free(secretid); 1057f27aaf4bSChristian Brunner return r; 1058f27aaf4bSChristian Brunner } 1059f27aaf4bSChristian Brunner 106056e7cf8dSJeff Cody 106156e7cf8dSJeff Cody /* Since RBD is currently always opened R/W via the API, 106256e7cf8dSJeff Cody * we just need to check if we are using a snapshot or not, in 106356e7cf8dSJeff Cody * order to determine if we will allow it to be R/W */ 106456e7cf8dSJeff Cody static int qemu_rbd_reopen_prepare(BDRVReopenState *state, 106556e7cf8dSJeff Cody BlockReopenQueue *queue, Error **errp) 106656e7cf8dSJeff Cody { 106756e7cf8dSJeff Cody BDRVRBDState *s = state->bs->opaque; 106856e7cf8dSJeff Cody int ret = 0; 106956e7cf8dSJeff Cody 107056e7cf8dSJeff Cody if (s->snap && state->flags & BDRV_O_RDWR) { 107156e7cf8dSJeff Cody error_setg(errp, 107256e7cf8dSJeff Cody "Cannot change node '%s' to r/w when using RBD snapshot", 107356e7cf8dSJeff Cody bdrv_get_device_or_node_name(state->bs)); 107456e7cf8dSJeff Cody ret = -EINVAL; 107556e7cf8dSJeff Cody } 107656e7cf8dSJeff Cody 107756e7cf8dSJeff Cody return ret; 107856e7cf8dSJeff Cody } 107956e7cf8dSJeff Cody 1080ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs) 1081f27aaf4bSChristian Brunner { 1082f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 1083f27aaf4bSChristian Brunner 1084ad32e9c0SJosh Durgin rbd_close(s->image); 1085ad32e9c0SJosh Durgin rados_ioctx_destroy(s->io_ctx); 10867267c094SAnthony Liguori g_free(s->snap); 108780b61a27SJeff Cody g_free(s->image_name); 1088ad32e9c0SJosh Durgin rados_shutdown(s->cluster); 1089f27aaf4bSChristian Brunner } 1090f27aaf4bSChristian Brunner 1091d24f8023SStefano Garzarella /* Resize the RBD image and update the 'image_size' with the current size */ 1092d24f8023SStefano Garzarella static int qemu_rbd_resize(BlockDriverState *bs, uint64_t size) 1093d24f8023SStefano Garzarella { 1094d24f8023SStefano Garzarella BDRVRBDState *s = bs->opaque; 1095d24f8023SStefano Garzarella int r; 1096d24f8023SStefano Garzarella 1097d24f8023SStefano Garzarella r = rbd_resize(s->image, size); 1098d24f8023SStefano Garzarella if (r < 0) { 1099d24f8023SStefano Garzarella return r; 1100d24f8023SStefano Garzarella } 1101d24f8023SStefano Garzarella 1102d24f8023SStefano Garzarella s->image_size = size; 1103d24f8023SStefano Garzarella 1104d24f8023SStefano Garzarella return 0; 1105d24f8023SStefano Garzarella } 1106d24f8023SStefano Garzarella 1107c3e5fac5SPeter Lieven static void qemu_rbd_finish_bh(void *opaque) 1108f27aaf4bSChristian Brunner { 1109c3e5fac5SPeter Lieven RBDTask *task = opaque; 111064cc845bSPeter Lieven task->complete = true; 1111c3e5fac5SPeter Lieven aio_co_wake(task->co); 1112ad32e9c0SJosh Durgin } 1113ad32e9c0SJosh Durgin 1114ad32e9c0SJosh Durgin /* 1115c3e5fac5SPeter Lieven * This is the completion callback function for all rbd aio calls 1116c3e5fac5SPeter Lieven * started from qemu_rbd_start_co(). 1117ad32e9c0SJosh Durgin * 1118ad32e9c0SJosh Durgin * Note: this function is being called from a non qemu thread so 1119ad32e9c0SJosh Durgin * we need to be careful about what we do here. Generally we only 1120e04fb07fSStefan Hajnoczi * schedule a BH, and do the rest of the io completion handling 1121c3e5fac5SPeter Lieven * from qemu_rbd_finish_bh() which runs in a qemu context. 1122ad32e9c0SJosh Durgin */ 1123c3e5fac5SPeter Lieven static void qemu_rbd_completion_cb(rbd_completion_t c, RBDTask *task) 1124ad32e9c0SJosh Durgin { 1125c3e5fac5SPeter Lieven task->ret = rbd_aio_get_return_value(c); 1126ad32e9c0SJosh Durgin rbd_aio_release(c); 1127c3e5fac5SPeter Lieven aio_bh_schedule_oneshot(bdrv_get_aio_context(task->bs), 1128c3e5fac5SPeter Lieven qemu_rbd_finish_bh, task); 1129473c7f02SStefan Priebe } 1130f27aaf4bSChristian Brunner 1131c3e5fac5SPeter Lieven static int coroutine_fn qemu_rbd_start_co(BlockDriverState *bs, 1132c3e5fac5SPeter Lieven uint64_t offset, 1133c3e5fac5SPeter Lieven uint64_t bytes, 1134f27aaf4bSChristian Brunner QEMUIOVector *qiov, 1135c3e5fac5SPeter Lieven int flags, 1136787f3133SJosh Durgin RBDAIOCmd cmd) 1137f27aaf4bSChristian Brunner { 1138c3e5fac5SPeter Lieven BDRVRBDState *s = bs->opaque; 1139c3e5fac5SPeter Lieven RBDTask task = { .bs = bs, .co = qemu_coroutine_self() }; 1140ad32e9c0SJosh Durgin rbd_completion_t c; 114151a13528SJosh Durgin int r; 1142f27aaf4bSChristian Brunner 1143c3e5fac5SPeter Lieven assert(!qiov || qiov->size == bytes); 1144f27aaf4bSChristian Brunner 1145cc5387a5SStefano Garzarella if (cmd == RBD_AIO_WRITE || cmd == RBD_AIO_WRITE_ZEROES) { 1146cc5387a5SStefano Garzarella /* 1147cc5387a5SStefano Garzarella * RBD APIs don't allow us to write more than actual size, so in order 1148cc5387a5SStefano Garzarella * to support growing images, we resize the image before write 1149cc5387a5SStefano Garzarella * operations that exceed the current size. 1150cc5387a5SStefano Garzarella */ 1151cc5387a5SStefano Garzarella if (offset + bytes > s->image_size) { 1152cc5387a5SStefano Garzarella int r = qemu_rbd_resize(bs, offset + bytes); 1153cc5387a5SStefano Garzarella if (r < 0) { 1154cc5387a5SStefano Garzarella return r; 1155cc5387a5SStefano Garzarella } 1156cc5387a5SStefano Garzarella } 1157cc5387a5SStefano Garzarella } 1158cc5387a5SStefano Garzarella 1159c3e5fac5SPeter Lieven r = rbd_aio_create_completion(&task, 1160c3e5fac5SPeter Lieven (rbd_callback_t) qemu_rbd_completion_cb, &c); 116151a13528SJosh Durgin if (r < 0) { 1162c3e5fac5SPeter Lieven return r; 116351a13528SJosh Durgin } 1164f27aaf4bSChristian Brunner 1165787f3133SJosh Durgin switch (cmd) { 1166787f3133SJosh Durgin case RBD_AIO_READ: 1167c3e5fac5SPeter Lieven r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, offset, c); 1168c3e5fac5SPeter Lieven break; 1169c3e5fac5SPeter Lieven case RBD_AIO_WRITE: 1170c3e5fac5SPeter Lieven r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, offset, c); 1171787f3133SJosh Durgin break; 1172787f3133SJosh Durgin case RBD_AIO_DISCARD: 1173c3e5fac5SPeter Lieven r = rbd_aio_discard(s->image, offset, bytes, c); 1174787f3133SJosh Durgin break; 1175dc7588c1SJosh Durgin case RBD_AIO_FLUSH: 117648672ac0SPeter Lieven r = rbd_aio_flush(s->image, c); 1177dc7588c1SJosh Durgin break; 1178c56ac27dSPeter Lieven #ifdef LIBRBD_SUPPORTS_WRITE_ZEROES 1179c56ac27dSPeter Lieven case RBD_AIO_WRITE_ZEROES: { 1180c56ac27dSPeter Lieven int zero_flags = 0; 1181c56ac27dSPeter Lieven #ifdef RBD_WRITE_ZEROES_FLAG_THICK_PROVISION 1182c56ac27dSPeter Lieven if (!(flags & BDRV_REQ_MAY_UNMAP)) { 1183c56ac27dSPeter Lieven zero_flags = RBD_WRITE_ZEROES_FLAG_THICK_PROVISION; 1184c56ac27dSPeter Lieven } 1185c56ac27dSPeter Lieven #endif 1186c56ac27dSPeter Lieven r = rbd_aio_write_zeroes(s->image, offset, bytes, c, zero_flags, 0); 1187c56ac27dSPeter Lieven break; 1188c56ac27dSPeter Lieven } 1189c56ac27dSPeter Lieven #endif 1190787f3133SJosh Durgin default: 1191787f3133SJosh Durgin r = -EINVAL; 119251a13528SJosh Durgin } 119351a13528SJosh Durgin 119451a13528SJosh Durgin if (r < 0) { 1195c3e5fac5SPeter Lieven error_report("rbd request failed early: cmd %d offset %" PRIu64 1196c3e5fac5SPeter Lieven " bytes %" PRIu64 " flags %d r %d (%s)", cmd, offset, 1197c3e5fac5SPeter Lieven bytes, flags, r, strerror(-r)); 1198405a2764SKevin Wolf rbd_aio_release(c); 1199c3e5fac5SPeter Lieven return r; 1200f27aaf4bSChristian Brunner } 1201f27aaf4bSChristian Brunner 1202c3e5fac5SPeter Lieven while (!task.complete) { 1203c3e5fac5SPeter Lieven qemu_coroutine_yield(); 1204f27aaf4bSChristian Brunner } 1205f27aaf4bSChristian Brunner 1206c3e5fac5SPeter Lieven if (task.ret < 0) { 1207c3e5fac5SPeter Lieven error_report("rbd request failed: cmd %d offset %" PRIu64 " bytes %" 1208c3e5fac5SPeter Lieven PRIu64 " flags %d task.ret %" PRIi64 " (%s)", cmd, offset, 1209c3e5fac5SPeter Lieven bytes, flags, task.ret, strerror(-task.ret)); 1210c3e5fac5SPeter Lieven return task.ret; 1211f27aaf4bSChristian Brunner } 1212f27aaf4bSChristian Brunner 1213c3e5fac5SPeter Lieven /* zero pad short reads */ 1214c3e5fac5SPeter Lieven if (cmd == RBD_AIO_READ && task.ret < qiov->size) { 1215c3e5fac5SPeter Lieven qemu_iovec_memset(qiov, task.ret, 0, qiov->size - task.ret); 1216c3e5fac5SPeter Lieven } 1217c3e5fac5SPeter Lieven 1218c3e5fac5SPeter Lieven return 0; 1219c3e5fac5SPeter Lieven } 1220c3e5fac5SPeter Lieven 1221c3e5fac5SPeter Lieven static int 1222f7ef38ddSVladimir Sementsov-Ogievskiy coroutine_fn qemu_rbd_co_preadv(BlockDriverState *bs, int64_t offset, 1223f7ef38ddSVladimir Sementsov-Ogievskiy int64_t bytes, QEMUIOVector *qiov, 1224f7ef38ddSVladimir Sementsov-Ogievskiy BdrvRequestFlags flags) 1225dc7588c1SJosh Durgin { 1226c3e5fac5SPeter Lieven return qemu_rbd_start_co(bs, offset, bytes, qiov, flags, RBD_AIO_READ); 1227c3e5fac5SPeter Lieven } 1228c3e5fac5SPeter Lieven 1229c3e5fac5SPeter Lieven static int 1230e75abedaSVladimir Sementsov-Ogievskiy coroutine_fn qemu_rbd_co_pwritev(BlockDriverState *bs, int64_t offset, 1231e75abedaSVladimir Sementsov-Ogievskiy int64_t bytes, QEMUIOVector *qiov, 1232e75abedaSVladimir Sementsov-Ogievskiy BdrvRequestFlags flags) 1233c3e5fac5SPeter Lieven { 1234c3e5fac5SPeter Lieven return qemu_rbd_start_co(bs, offset, bytes, qiov, flags, RBD_AIO_WRITE); 1235c3e5fac5SPeter Lieven } 1236c3e5fac5SPeter Lieven 1237c3e5fac5SPeter Lieven static int coroutine_fn qemu_rbd_co_flush(BlockDriverState *bs) 1238c3e5fac5SPeter Lieven { 1239c3e5fac5SPeter Lieven return qemu_rbd_start_co(bs, 0, 0, NULL, 0, RBD_AIO_FLUSH); 1240c3e5fac5SPeter Lieven } 1241c3e5fac5SPeter Lieven 1242c3e5fac5SPeter Lieven static int coroutine_fn qemu_rbd_co_pdiscard(BlockDriverState *bs, 12430c802287SVladimir Sementsov-Ogievskiy int64_t offset, int64_t bytes) 1244c3e5fac5SPeter Lieven { 12450c802287SVladimir Sementsov-Ogievskiy return qemu_rbd_start_co(bs, offset, bytes, NULL, 0, RBD_AIO_DISCARD); 1246dc7588c1SJosh Durgin } 1247dc7588c1SJosh Durgin 1248c56ac27dSPeter Lieven #ifdef LIBRBD_SUPPORTS_WRITE_ZEROES 1249c56ac27dSPeter Lieven static int 1250c56ac27dSPeter Lieven coroutine_fn qemu_rbd_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, 1251f34b2bcfSVladimir Sementsov-Ogievskiy int64_t bytes, BdrvRequestFlags flags) 1252c56ac27dSPeter Lieven { 1253f34b2bcfSVladimir Sementsov-Ogievskiy return qemu_rbd_start_co(bs, offset, bytes, NULL, flags, 1254c56ac27dSPeter Lieven RBD_AIO_WRITE_ZEROES); 1255c56ac27dSPeter Lieven } 1256c56ac27dSPeter Lieven #endif 1257c56ac27dSPeter Lieven 12583d47eb0aSEmanuele Giuseppe Esposito static int coroutine_fn 12593d47eb0aSEmanuele Giuseppe Esposito qemu_rbd_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 1260f27aaf4bSChristian Brunner { 1261f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 1262832a93dcSPeter Lieven bdi->cluster_size = s->object_size; 1263f27aaf4bSChristian Brunner return 0; 1264f27aaf4bSChristian Brunner } 1265f27aaf4bSChristian Brunner 126642e4ac9eSOr Ozeri static ImageInfoSpecific *qemu_rbd_get_specific_info(BlockDriverState *bs, 126742e4ac9eSOr Ozeri Error **errp) 126842e4ac9eSOr Ozeri { 126942e4ac9eSOr Ozeri BDRVRBDState *s = bs->opaque; 127042e4ac9eSOr Ozeri ImageInfoSpecific *spec_info; 127142e4ac9eSOr Ozeri char buf[RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN] = {0}; 127242e4ac9eSOr Ozeri int r; 127342e4ac9eSOr Ozeri 127442e4ac9eSOr Ozeri if (s->image_size >= RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN) { 127542e4ac9eSOr Ozeri r = rbd_read(s->image, 0, 127642e4ac9eSOr Ozeri RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN, buf); 127742e4ac9eSOr Ozeri if (r < 0) { 127842e4ac9eSOr Ozeri error_setg_errno(errp, -r, "cannot read image start for probe"); 127942e4ac9eSOr Ozeri return NULL; 128042e4ac9eSOr Ozeri } 128142e4ac9eSOr Ozeri } 128242e4ac9eSOr Ozeri 128342e4ac9eSOr Ozeri spec_info = g_new(ImageInfoSpecific, 1); 128442e4ac9eSOr Ozeri *spec_info = (ImageInfoSpecific){ 128542e4ac9eSOr Ozeri .type = IMAGE_INFO_SPECIFIC_KIND_RBD, 128642e4ac9eSOr Ozeri .u.rbd.data = g_new0(ImageInfoSpecificRbd, 1), 128742e4ac9eSOr Ozeri }; 128842e4ac9eSOr Ozeri 128942e4ac9eSOr Ozeri if (memcmp(buf, rbd_luks_header_verification, 129042e4ac9eSOr Ozeri RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN) == 0) { 129142e4ac9eSOr Ozeri spec_info->u.rbd.data->encryption_format = 129242e4ac9eSOr Ozeri RBD_IMAGE_ENCRYPTION_FORMAT_LUKS; 129342e4ac9eSOr Ozeri spec_info->u.rbd.data->has_encryption_format = true; 129442e4ac9eSOr Ozeri } else if (memcmp(buf, rbd_luks2_header_verification, 129542e4ac9eSOr Ozeri RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN) == 0) { 129642e4ac9eSOr Ozeri spec_info->u.rbd.data->encryption_format = 129742e4ac9eSOr Ozeri RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2; 129842e4ac9eSOr Ozeri spec_info->u.rbd.data->has_encryption_format = true; 129942e4ac9eSOr Ozeri } else { 130042e4ac9eSOr Ozeri spec_info->u.rbd.data->has_encryption_format = false; 130142e4ac9eSOr Ozeri } 130242e4ac9eSOr Ozeri 130342e4ac9eSOr Ozeri return spec_info; 130442e4ac9eSOr Ozeri } 130542e4ac9eSOr Ozeri 13060347a8fdSPeter Lieven /* 13070347a8fdSPeter Lieven * rbd_diff_iterate2 allows to interrupt the exection by returning a negative 13080347a8fdSPeter Lieven * value in the callback routine. Choose a value that does not conflict with 13090347a8fdSPeter Lieven * an existing exitcode and return it if we want to prematurely stop the 13100347a8fdSPeter Lieven * execution because we detected a change in the allocation status. 13110347a8fdSPeter Lieven */ 13120347a8fdSPeter Lieven #define QEMU_RBD_EXIT_DIFF_ITERATE2 -9000 13130347a8fdSPeter Lieven 13140347a8fdSPeter Lieven static int qemu_rbd_diff_iterate_cb(uint64_t offs, size_t len, 13150347a8fdSPeter Lieven int exists, void *opaque) 13160347a8fdSPeter Lieven { 13170347a8fdSPeter Lieven RBDDiffIterateReq *req = opaque; 13180347a8fdSPeter Lieven 13190347a8fdSPeter Lieven assert(req->offs + req->bytes <= offs); 13209e302f64SPeter Lieven 13219e302f64SPeter Lieven /* treat a hole like an unallocated area and bail out */ 13229e302f64SPeter Lieven if (!exists) { 13239e302f64SPeter Lieven return 0; 13249e302f64SPeter Lieven } 13250347a8fdSPeter Lieven 13260347a8fdSPeter Lieven if (!req->exists && offs > req->offs) { 13270347a8fdSPeter Lieven /* 13280347a8fdSPeter Lieven * we started in an unallocated area and hit the first allocated 13290347a8fdSPeter Lieven * block. req->bytes must be set to the length of the unallocated area 13300347a8fdSPeter Lieven * before the allocated area. stop further processing. 13310347a8fdSPeter Lieven */ 13320347a8fdSPeter Lieven req->bytes = offs - req->offs; 13330347a8fdSPeter Lieven return QEMU_RBD_EXIT_DIFF_ITERATE2; 13340347a8fdSPeter Lieven } 13350347a8fdSPeter Lieven 13360347a8fdSPeter Lieven if (req->exists && offs > req->offs + req->bytes) { 13370347a8fdSPeter Lieven /* 13380347a8fdSPeter Lieven * we started in an allocated area and jumped over an unallocated area, 13390347a8fdSPeter Lieven * req->bytes contains the length of the allocated area before the 13400347a8fdSPeter Lieven * unallocated area. stop further processing. 13410347a8fdSPeter Lieven */ 13420347a8fdSPeter Lieven return QEMU_RBD_EXIT_DIFF_ITERATE2; 13430347a8fdSPeter Lieven } 13440347a8fdSPeter Lieven 13450347a8fdSPeter Lieven req->bytes += len; 13460347a8fdSPeter Lieven req->exists = true; 13470347a8fdSPeter Lieven 13480347a8fdSPeter Lieven return 0; 13490347a8fdSPeter Lieven } 13500347a8fdSPeter Lieven 13510347a8fdSPeter Lieven static int coroutine_fn qemu_rbd_co_block_status(BlockDriverState *bs, 13520347a8fdSPeter Lieven bool want_zero, int64_t offset, 13530347a8fdSPeter Lieven int64_t bytes, int64_t *pnum, 13540347a8fdSPeter Lieven int64_t *map, 13550347a8fdSPeter Lieven BlockDriverState **file) 13560347a8fdSPeter Lieven { 13570347a8fdSPeter Lieven BDRVRBDState *s = bs->opaque; 13580347a8fdSPeter Lieven int status, r; 13590347a8fdSPeter Lieven RBDDiffIterateReq req = { .offs = offset }; 13600347a8fdSPeter Lieven uint64_t features, flags; 1361fc176116SPeter Lieven uint64_t head = 0; 13620347a8fdSPeter Lieven 13630347a8fdSPeter Lieven assert(offset + bytes <= s->image_size); 13640347a8fdSPeter Lieven 13650347a8fdSPeter Lieven /* default to all sectors allocated */ 13660347a8fdSPeter Lieven status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; 13670347a8fdSPeter Lieven *map = offset; 13680347a8fdSPeter Lieven *file = bs; 13690347a8fdSPeter Lieven *pnum = bytes; 13700347a8fdSPeter Lieven 13710347a8fdSPeter Lieven /* check if RBD image supports fast-diff */ 13720347a8fdSPeter Lieven r = rbd_get_features(s->image, &features); 13730347a8fdSPeter Lieven if (r < 0) { 13740347a8fdSPeter Lieven return status; 13750347a8fdSPeter Lieven } 13760347a8fdSPeter Lieven if (!(features & RBD_FEATURE_FAST_DIFF)) { 13770347a8fdSPeter Lieven return status; 13780347a8fdSPeter Lieven } 13790347a8fdSPeter Lieven 13800347a8fdSPeter Lieven /* check if RBD fast-diff result is valid */ 13810347a8fdSPeter Lieven r = rbd_get_flags(s->image, &flags); 13820347a8fdSPeter Lieven if (r < 0) { 13830347a8fdSPeter Lieven return status; 13840347a8fdSPeter Lieven } 13850347a8fdSPeter Lieven if (flags & RBD_FLAG_FAST_DIFF_INVALID) { 13860347a8fdSPeter Lieven return status; 13870347a8fdSPeter Lieven } 13880347a8fdSPeter Lieven 1389fc176116SPeter Lieven #if LIBRBD_VERSION_CODE < LIBRBD_VERSION(1, 17, 0) 1390fc176116SPeter Lieven /* 1391fc176116SPeter Lieven * librbd had a bug until early 2022 that affected all versions of ceph that 1392fc176116SPeter Lieven * supported fast-diff. This bug results in reporting of incorrect offsets 1393fc176116SPeter Lieven * if the offset parameter to rbd_diff_iterate2 is not object aligned. 1394fc176116SPeter Lieven * Work around this bug by rounding down the offset to object boundaries. 1395fc176116SPeter Lieven * This is OK because we call rbd_diff_iterate2 with whole_object = true. 1396fc176116SPeter Lieven * However, this workaround only works for non cloned images with default 1397fc176116SPeter Lieven * striping. 1398fc176116SPeter Lieven * 1399fc176116SPeter Lieven * See: https://tracker.ceph.com/issues/53784 1400fc176116SPeter Lieven */ 1401fc176116SPeter Lieven 1402fc176116SPeter Lieven /* check if RBD image has non-default striping enabled */ 1403fc176116SPeter Lieven if (features & RBD_FEATURE_STRIPINGV2) { 1404fc176116SPeter Lieven return status; 1405fc176116SPeter Lieven } 1406fc176116SPeter Lieven 1407fc176116SPeter Lieven #pragma GCC diagnostic push 1408fc176116SPeter Lieven #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 1409fc176116SPeter Lieven /* 1410fc176116SPeter Lieven * check if RBD image is a clone (= has a parent). 1411fc176116SPeter Lieven * 1412fc176116SPeter Lieven * rbd_get_parent_info is deprecated from Nautilus onwards, but the 1413fc176116SPeter Lieven * replacement rbd_get_parent is not present in Luminous and Mimic. 1414fc176116SPeter Lieven */ 1415fc176116SPeter Lieven if (rbd_get_parent_info(s->image, NULL, 0, NULL, 0, NULL, 0) != -ENOENT) { 1416fc176116SPeter Lieven return status; 1417fc176116SPeter Lieven } 1418fc176116SPeter Lieven #pragma GCC diagnostic pop 1419fc176116SPeter Lieven 1420fc176116SPeter Lieven head = req.offs & (s->object_size - 1); 1421fc176116SPeter Lieven req.offs -= head; 1422fc176116SPeter Lieven bytes += head; 1423fc176116SPeter Lieven #endif 1424fc176116SPeter Lieven 1425fc176116SPeter Lieven r = rbd_diff_iterate2(s->image, NULL, req.offs, bytes, true, true, 14260347a8fdSPeter Lieven qemu_rbd_diff_iterate_cb, &req); 14270347a8fdSPeter Lieven if (r < 0 && r != QEMU_RBD_EXIT_DIFF_ITERATE2) { 14280347a8fdSPeter Lieven return status; 14290347a8fdSPeter Lieven } 14300347a8fdSPeter Lieven assert(req.bytes <= bytes); 14310347a8fdSPeter Lieven if (!req.exists) { 14320347a8fdSPeter Lieven if (r == 0) { 14330347a8fdSPeter Lieven /* 14340347a8fdSPeter Lieven * rbd_diff_iterate2 does not invoke callbacks for unallocated 14350347a8fdSPeter Lieven * areas. This here catches the case where no callback was 14360347a8fdSPeter Lieven * invoked at all (req.bytes == 0). 14370347a8fdSPeter Lieven */ 14380347a8fdSPeter Lieven assert(req.bytes == 0); 14390347a8fdSPeter Lieven req.bytes = bytes; 14400347a8fdSPeter Lieven } 14410347a8fdSPeter Lieven status = BDRV_BLOCK_ZERO | BDRV_BLOCK_OFFSET_VALID; 14420347a8fdSPeter Lieven } 14430347a8fdSPeter Lieven 1444fc176116SPeter Lieven assert(req.bytes > head); 1445fc176116SPeter Lieven *pnum = req.bytes - head; 14460347a8fdSPeter Lieven return status; 14470347a8fdSPeter Lieven } 14480347a8fdSPeter Lieven 1449c86422c5SEmanuele Giuseppe Esposito static int64_t coroutine_fn qemu_rbd_co_getlength(BlockDriverState *bs) 1450f27aaf4bSChristian Brunner { 1451f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 1452ad32e9c0SJosh Durgin int r; 1453f27aaf4bSChristian Brunner 14546d921418SPeter Lieven r = rbd_get_size(s->image, &s->image_size); 1455ad32e9c0SJosh Durgin if (r < 0) { 1456ad32e9c0SJosh Durgin return r; 1457f27aaf4bSChristian Brunner } 1458f27aaf4bSChristian Brunner 14596d921418SPeter Lieven return s->image_size; 1460ad32e9c0SJosh Durgin } 1461ad32e9c0SJosh Durgin 1462061ca8a3SKevin Wolf static int coroutine_fn qemu_rbd_co_truncate(BlockDriverState *bs, 1463061ca8a3SKevin Wolf int64_t offset, 1464c80d8b06SMax Reitz bool exact, 1465061ca8a3SKevin Wolf PreallocMode prealloc, 146692b92799SKevin Wolf BdrvRequestFlags flags, 1467061ca8a3SKevin Wolf Error **errp) 146830cdc48cSJosh Durgin { 146930cdc48cSJosh Durgin int r; 147030cdc48cSJosh Durgin 14718243ccb7SMax Reitz if (prealloc != PREALLOC_MODE_OFF) { 14728243ccb7SMax Reitz error_setg(errp, "Unsupported preallocation mode '%s'", 1473977c736fSMarkus Armbruster PreallocMode_str(prealloc)); 14748243ccb7SMax Reitz return -ENOTSUP; 14758243ccb7SMax Reitz } 14768243ccb7SMax Reitz 1477d24f8023SStefano Garzarella r = qemu_rbd_resize(bs, offset); 147830cdc48cSJosh Durgin if (r < 0) { 1479f59adb32SMax Reitz error_setg_errno(errp, -r, "Failed to resize file"); 148030cdc48cSJosh Durgin return r; 148130cdc48cSJosh Durgin } 148230cdc48cSJosh Durgin 148330cdc48cSJosh Durgin return 0; 148430cdc48cSJosh Durgin } 148530cdc48cSJosh Durgin 1486ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs, 1487ad32e9c0SJosh Durgin QEMUSnapshotInfo *sn_info) 1488f27aaf4bSChristian Brunner { 1489f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 1490f27aaf4bSChristian Brunner int r; 1491f27aaf4bSChristian Brunner 1492f27aaf4bSChristian Brunner if (sn_info->name[0] == '\0') { 1493f27aaf4bSChristian Brunner return -EINVAL; /* we need a name for rbd snapshots */ 1494f27aaf4bSChristian Brunner } 1495f27aaf4bSChristian Brunner 1496f27aaf4bSChristian Brunner /* 1497f27aaf4bSChristian Brunner * rbd snapshots are using the name as the user controlled unique identifier 1498f27aaf4bSChristian Brunner * we can't use the rbd snapid for that purpose, as it can't be set 1499f27aaf4bSChristian Brunner */ 1500f27aaf4bSChristian Brunner if (sn_info->id_str[0] != '\0' && 1501f27aaf4bSChristian Brunner strcmp(sn_info->id_str, sn_info->name) != 0) { 1502f27aaf4bSChristian Brunner return -EINVAL; 1503f27aaf4bSChristian Brunner } 1504f27aaf4bSChristian Brunner 1505f27aaf4bSChristian Brunner if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) { 1506f27aaf4bSChristian Brunner return -ERANGE; 1507f27aaf4bSChristian Brunner } 1508f27aaf4bSChristian Brunner 1509ad32e9c0SJosh Durgin r = rbd_snap_create(s->image, sn_info->name); 1510f27aaf4bSChristian Brunner if (r < 0) { 1511ad32e9c0SJosh Durgin error_report("failed to create snap: %s", strerror(-r)); 1512f27aaf4bSChristian Brunner return r; 1513f27aaf4bSChristian Brunner } 1514f27aaf4bSChristian Brunner 1515f27aaf4bSChristian Brunner return 0; 1516f27aaf4bSChristian Brunner } 1517f27aaf4bSChristian Brunner 1518bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs, 1519a89d89d3SWenchao Xia const char *snapshot_id, 1520a89d89d3SWenchao Xia const char *snapshot_name, 1521a89d89d3SWenchao Xia Error **errp) 1522bd603247SGregory Farnum { 1523bd603247SGregory Farnum BDRVRBDState *s = bs->opaque; 1524bd603247SGregory Farnum int r; 1525bd603247SGregory Farnum 1526a89d89d3SWenchao Xia if (!snapshot_name) { 1527a89d89d3SWenchao Xia error_setg(errp, "rbd need a valid snapshot name"); 1528a89d89d3SWenchao Xia return -EINVAL; 1529a89d89d3SWenchao Xia } 1530a89d89d3SWenchao Xia 1531a89d89d3SWenchao Xia /* If snapshot_id is specified, it must be equal to name, see 1532a89d89d3SWenchao Xia qemu_rbd_snap_list() */ 1533a89d89d3SWenchao Xia if (snapshot_id && strcmp(snapshot_id, snapshot_name)) { 1534a89d89d3SWenchao Xia error_setg(errp, 1535a89d89d3SWenchao Xia "rbd do not support snapshot id, it should be NULL or " 1536a89d89d3SWenchao Xia "equal to snapshot name"); 1537a89d89d3SWenchao Xia return -EINVAL; 1538a89d89d3SWenchao Xia } 1539a89d89d3SWenchao Xia 1540bd603247SGregory Farnum r = rbd_snap_remove(s->image, snapshot_name); 1541a89d89d3SWenchao Xia if (r < 0) { 1542a89d89d3SWenchao Xia error_setg_errno(errp, -r, "Failed to remove the snapshot"); 1543a89d89d3SWenchao Xia } 1544bd603247SGregory Farnum return r; 1545bd603247SGregory Farnum } 1546bd603247SGregory Farnum 1547bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs, 1548bd603247SGregory Farnum const char *snapshot_name) 1549bd603247SGregory Farnum { 1550bd603247SGregory Farnum BDRVRBDState *s = bs->opaque; 1551bd603247SGregory Farnum 15529be38598SEduardo Habkost return rbd_snap_rollback(s->image, snapshot_name); 1553bd603247SGregory Farnum } 1554bd603247SGregory Farnum 1555ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs, 1556ad32e9c0SJosh Durgin QEMUSnapshotInfo **psn_tab) 1557f27aaf4bSChristian Brunner { 1558f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 1559f27aaf4bSChristian Brunner QEMUSnapshotInfo *sn_info, *sn_tab = NULL; 1560ad32e9c0SJosh Durgin int i, snap_count; 1561ad32e9c0SJosh Durgin rbd_snap_info_t *snaps; 1562ad32e9c0SJosh Durgin int max_snaps = RBD_MAX_SNAPS; 1563f27aaf4bSChristian Brunner 1564ad32e9c0SJosh Durgin do { 156502c4f26bSMarkus Armbruster snaps = g_new(rbd_snap_info_t, max_snaps); 1566ad32e9c0SJosh Durgin snap_count = rbd_snap_list(s->image, snaps, &max_snaps); 15679e6337d0SStefan Hajnoczi if (snap_count <= 0) { 15687267c094SAnthony Liguori g_free(snaps); 1569f27aaf4bSChristian Brunner } 1570ad32e9c0SJosh Durgin } while (snap_count == -ERANGE); 1571f27aaf4bSChristian Brunner 1572ad32e9c0SJosh Durgin if (snap_count <= 0) { 1573b9c53290SJosh Durgin goto done; 1574f27aaf4bSChristian Brunner } 1575f27aaf4bSChristian Brunner 15765839e53bSMarkus Armbruster sn_tab = g_new0(QEMUSnapshotInfo, snap_count); 1577f27aaf4bSChristian Brunner 1578ad32e9c0SJosh Durgin for (i = 0; i < snap_count; i++) { 1579ad32e9c0SJosh Durgin const char *snap_name = snaps[i].name; 1580f27aaf4bSChristian Brunner 1581f27aaf4bSChristian Brunner sn_info = sn_tab + i; 1582f27aaf4bSChristian Brunner pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name); 1583f27aaf4bSChristian Brunner pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name); 1584f27aaf4bSChristian Brunner 1585ad32e9c0SJosh Durgin sn_info->vm_state_size = snaps[i].size; 1586f27aaf4bSChristian Brunner sn_info->date_sec = 0; 1587f27aaf4bSChristian Brunner sn_info->date_nsec = 0; 1588f27aaf4bSChristian Brunner sn_info->vm_clock_nsec = 0; 1589f27aaf4bSChristian Brunner } 1590ad32e9c0SJosh Durgin rbd_snap_list_end(snaps); 15919e6337d0SStefan Hajnoczi g_free(snaps); 1592ad32e9c0SJosh Durgin 1593b9c53290SJosh Durgin done: 1594f27aaf4bSChristian Brunner *psn_tab = sn_tab; 1595f27aaf4bSChristian Brunner return snap_count; 1596f27aaf4bSChristian Brunner } 1597f27aaf4bSChristian Brunner 15982b148f39SPaolo Bonzini static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs, 1599be217884SAdam Crume Error **errp) 1600be217884SAdam Crume { 1601be217884SAdam Crume BDRVRBDState *s = bs->opaque; 1602be217884SAdam Crume int r = rbd_invalidate_cache(s->image); 1603be217884SAdam Crume if (r < 0) { 1604be217884SAdam Crume error_setg_errno(errp, -r, "Failed to invalidate the cache"); 1605be217884SAdam Crume } 1606be217884SAdam Crume } 1607be217884SAdam Crume 1608bd0cf596SChunyan Liu static QemuOptsList qemu_rbd_create_opts = { 1609bd0cf596SChunyan Liu .name = "rbd-create-opts", 1610bd0cf596SChunyan Liu .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head), 1611bd0cf596SChunyan Liu .desc = { 1612f27aaf4bSChristian Brunner { 1613f27aaf4bSChristian Brunner .name = BLOCK_OPT_SIZE, 1614bd0cf596SChunyan Liu .type = QEMU_OPT_SIZE, 1615f27aaf4bSChristian Brunner .help = "Virtual disk size" 1616f27aaf4bSChristian Brunner }, 1617f27aaf4bSChristian Brunner { 1618f27aaf4bSChristian Brunner .name = BLOCK_OPT_CLUSTER_SIZE, 1619bd0cf596SChunyan Liu .type = QEMU_OPT_SIZE, 1620f27aaf4bSChristian Brunner .help = "RBD object size" 1621f27aaf4bSChristian Brunner }, 162260390a21SDaniel P. Berrange { 162360390a21SDaniel P. Berrange .name = "password-secret", 162460390a21SDaniel P. Berrange .type = QEMU_OPT_STRING, 162560390a21SDaniel P. Berrange .help = "ID of secret providing the password", 162660390a21SDaniel P. Berrange }, 162742e4ac9eSOr Ozeri { 162842e4ac9eSOr Ozeri .name = "encrypt.format", 162942e4ac9eSOr Ozeri .type = QEMU_OPT_STRING, 163042e4ac9eSOr Ozeri .help = "Encrypt the image, format choices: 'luks', 'luks2'", 163142e4ac9eSOr Ozeri }, 163242e4ac9eSOr Ozeri { 163342e4ac9eSOr Ozeri .name = "encrypt.cipher-alg", 163442e4ac9eSOr Ozeri .type = QEMU_OPT_STRING, 163542e4ac9eSOr Ozeri .help = "Name of encryption cipher algorithm" 163642e4ac9eSOr Ozeri " (allowed values: aes-128, aes-256)", 163742e4ac9eSOr Ozeri }, 163842e4ac9eSOr Ozeri { 163942e4ac9eSOr Ozeri .name = "encrypt.key-secret", 164042e4ac9eSOr Ozeri .type = QEMU_OPT_STRING, 164142e4ac9eSOr Ozeri .help = "ID of secret providing LUKS passphrase", 164242e4ac9eSOr Ozeri }, 1643bd0cf596SChunyan Liu { /* end of list */ } 1644bd0cf596SChunyan Liu } 1645f27aaf4bSChristian Brunner }; 1646f27aaf4bSChristian Brunner 16472654267cSMax Reitz static const char *const qemu_rbd_strong_runtime_opts[] = { 16482654267cSMax Reitz "pool", 16497bae7c80SStefano Garzarella "namespace", 16502654267cSMax Reitz "image", 16512654267cSMax Reitz "conf", 16522654267cSMax Reitz "snapshot", 16532654267cSMax Reitz "user", 16542654267cSMax Reitz "server.", 16552654267cSMax Reitz "password-secret", 16562654267cSMax Reitz 16572654267cSMax Reitz NULL 16582654267cSMax Reitz }; 16592654267cSMax Reitz 1660f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = { 1661f27aaf4bSChristian Brunner .format_name = "rbd", 1662f27aaf4bSChristian Brunner .instance_size = sizeof(BDRVRBDState), 1663c7cacb3eSJeff Cody .bdrv_parse_filename = qemu_rbd_parse_filename, 1664ad32e9c0SJosh Durgin .bdrv_file_open = qemu_rbd_open, 1665ad32e9c0SJosh Durgin .bdrv_close = qemu_rbd_close, 166656e7cf8dSJeff Cody .bdrv_reopen_prepare = qemu_rbd_reopen_prepare, 16671bebea37SKevin Wolf .bdrv_co_create = qemu_rbd_co_create, 1668efc75e2aSStefan Hajnoczi .bdrv_co_create_opts = qemu_rbd_co_create_opts, 16693ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1, 16703d47eb0aSEmanuele Giuseppe Esposito .bdrv_co_get_info = qemu_rbd_co_get_info, 167142e4ac9eSOr Ozeri .bdrv_get_specific_info = qemu_rbd_get_specific_info, 1672bd0cf596SChunyan Liu .create_opts = &qemu_rbd_create_opts, 1673c86422c5SEmanuele Giuseppe Esposito .bdrv_co_getlength = qemu_rbd_co_getlength, 1674061ca8a3SKevin Wolf .bdrv_co_truncate = qemu_rbd_co_truncate, 1675f27aaf4bSChristian Brunner .protocol_name = "rbd", 1676f27aaf4bSChristian Brunner 1677c3e5fac5SPeter Lieven .bdrv_co_preadv = qemu_rbd_co_preadv, 1678c3e5fac5SPeter Lieven .bdrv_co_pwritev = qemu_rbd_co_pwritev, 1679c3e5fac5SPeter Lieven .bdrv_co_flush_to_disk = qemu_rbd_co_flush, 1680c3e5fac5SPeter Lieven .bdrv_co_pdiscard = qemu_rbd_co_pdiscard, 1681c56ac27dSPeter Lieven #ifdef LIBRBD_SUPPORTS_WRITE_ZEROES 1682c56ac27dSPeter Lieven .bdrv_co_pwrite_zeroes = qemu_rbd_co_pwrite_zeroes, 1683c56ac27dSPeter Lieven #endif 16840347a8fdSPeter Lieven .bdrv_co_block_status = qemu_rbd_co_block_status, 1685787f3133SJosh Durgin 1686ad32e9c0SJosh Durgin .bdrv_snapshot_create = qemu_rbd_snap_create, 1687bd603247SGregory Farnum .bdrv_snapshot_delete = qemu_rbd_snap_remove, 1688ad32e9c0SJosh Durgin .bdrv_snapshot_list = qemu_rbd_snap_list, 1689bd603247SGregory Farnum .bdrv_snapshot_goto = qemu_rbd_snap_rollback, 16902b148f39SPaolo Bonzini .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache, 16912654267cSMax Reitz 16922654267cSMax Reitz .strong_runtime_opts = qemu_rbd_strong_runtime_opts, 1693f27aaf4bSChristian Brunner }; 1694f27aaf4bSChristian Brunner 1695f27aaf4bSChristian Brunner static void bdrv_rbd_init(void) 1696f27aaf4bSChristian Brunner { 1697f27aaf4bSChristian Brunner bdrv_register(&bdrv_rbd); 1698f27aaf4bSChristian Brunner } 1699f27aaf4bSChristian Brunner 1700f27aaf4bSChristian Brunner block_init(bdrv_rbd_init); 1701