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 16da34e65cSMarkus Armbruster #include "qapi/error.h" 171de7afc9SPaolo Bonzini #include "qemu/error-report.h" 18737e150eSPaolo Bonzini #include "block/block_int.h" 1960390a21SDaniel P. Berrange #include "crypto/secret.h" 20f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 21f27aaf4bSChristian Brunner 22ad32e9c0SJosh Durgin #include <rbd/librbd.h> 23f27aaf4bSChristian Brunner 24f27aaf4bSChristian Brunner /* 25f27aaf4bSChristian Brunner * When specifying the image filename use: 26f27aaf4bSChristian Brunner * 27fab5cf59SJosh Durgin * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]] 28f27aaf4bSChristian Brunner * 299e1fbcdeSSage Weil * poolname must be the name of an existing rados pool. 30f27aaf4bSChristian Brunner * 319e1fbcdeSSage Weil * devicename is the name of the rbd image. 32f27aaf4bSChristian Brunner * 339e1fbcdeSSage Weil * Each option given is used to configure rados, and may be any valid 349e1fbcdeSSage Weil * Ceph option, "id", or "conf". 35fab5cf59SJosh Durgin * 369e1fbcdeSSage Weil * The "id" option indicates what user we should authenticate as to 379e1fbcdeSSage Weil * the Ceph cluster. If it is excluded we will use the Ceph default 389e1fbcdeSSage Weil * (normally 'admin'). 39f27aaf4bSChristian Brunner * 409e1fbcdeSSage Weil * The "conf" option specifies a Ceph configuration file to read. If 419e1fbcdeSSage Weil * it is not specified, we will read from the default Ceph locations 429e1fbcdeSSage Weil * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration 439e1fbcdeSSage Weil * file, specify conf=/dev/null. 44f27aaf4bSChristian Brunner * 459e1fbcdeSSage Weil * Configuration values containing :, @, or = can be escaped with a 469e1fbcdeSSage Weil * leading "\". 47f27aaf4bSChristian Brunner */ 48f27aaf4bSChristian Brunner 49787f3133SJosh Durgin /* rbd_aio_discard added in 0.1.2 */ 50787f3133SJosh Durgin #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2) 51787f3133SJosh Durgin #define LIBRBD_SUPPORTS_DISCARD 52787f3133SJosh Durgin #else 53787f3133SJosh Durgin #undef LIBRBD_SUPPORTS_DISCARD 54787f3133SJosh Durgin #endif 55787f3133SJosh Durgin 56f27aaf4bSChristian Brunner #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER) 57f27aaf4bSChristian Brunner 58ad32e9c0SJosh Durgin #define RBD_MAX_CONF_NAME_SIZE 128 59ad32e9c0SJosh Durgin #define RBD_MAX_CONF_VAL_SIZE 512 60ad32e9c0SJosh Durgin #define RBD_MAX_CONF_SIZE 1024 61ad32e9c0SJosh Durgin #define RBD_MAX_POOL_NAME_SIZE 128 62ad32e9c0SJosh Durgin #define RBD_MAX_SNAP_NAME_SIZE 128 63ad32e9c0SJosh Durgin #define RBD_MAX_SNAPS 100 64ad32e9c0SJosh Durgin 65787f3133SJosh Durgin typedef enum { 66787f3133SJosh Durgin RBD_AIO_READ, 67787f3133SJosh Durgin RBD_AIO_WRITE, 68dc7588c1SJosh Durgin RBD_AIO_DISCARD, 69dc7588c1SJosh Durgin RBD_AIO_FLUSH 70787f3133SJosh Durgin } RBDAIOCmd; 71787f3133SJosh Durgin 72f27aaf4bSChristian Brunner typedef struct RBDAIOCB { 737c84b1b8SMarkus Armbruster BlockAIOCB common; 74f27aaf4bSChristian Brunner QEMUBH *bh; 7508448d51SStefan Priebe int64_t ret; 76f27aaf4bSChristian Brunner QEMUIOVector *qiov; 77f27aaf4bSChristian Brunner char *bounce; 78787f3133SJosh Durgin RBDAIOCmd cmd; 79f27aaf4bSChristian Brunner int error; 80f27aaf4bSChristian Brunner struct BDRVRBDState *s; 81f27aaf4bSChristian Brunner } RBDAIOCB; 82f27aaf4bSChristian Brunner 83f27aaf4bSChristian Brunner typedef struct RADOSCB { 84f27aaf4bSChristian Brunner RBDAIOCB *acb; 85f27aaf4bSChristian Brunner struct BDRVRBDState *s; 86ad32e9c0SJosh Durgin int64_t size; 87f27aaf4bSChristian Brunner char *buf; 8808448d51SStefan Priebe int64_t ret; 89f27aaf4bSChristian Brunner } RADOSCB; 90f27aaf4bSChristian Brunner 91f27aaf4bSChristian Brunner typedef struct BDRVRBDState { 92ad32e9c0SJosh Durgin rados_t cluster; 93ad32e9c0SJosh Durgin rados_ioctx_t io_ctx; 94ad32e9c0SJosh Durgin rbd_image_t image; 95ad32e9c0SJosh Durgin char name[RBD_MAX_IMAGE_NAME_SIZE]; 96ad32e9c0SJosh Durgin char *snap; 97f27aaf4bSChristian Brunner } BDRVRBDState; 98f27aaf4bSChristian Brunner 99ad32e9c0SJosh Durgin static int qemu_rbd_next_tok(char *dst, int dst_len, 100f27aaf4bSChristian Brunner char *src, char delim, 101f27aaf4bSChristian Brunner const char *name, 102d61563b2SMarkus Armbruster char **p, Error **errp) 103f27aaf4bSChristian Brunner { 104f27aaf4bSChristian Brunner int l; 105f27aaf4bSChristian Brunner char *end; 106f27aaf4bSChristian Brunner 107f27aaf4bSChristian Brunner *p = NULL; 108f27aaf4bSChristian Brunner 109f27aaf4bSChristian Brunner if (delim != '\0') { 11016a06b24SSage Weil for (end = src; *end; ++end) { 11116a06b24SSage Weil if (*end == delim) { 11216a06b24SSage Weil break; 11316a06b24SSage Weil } 11416a06b24SSage Weil if (*end == '\\' && end[1] != '\0') { 11516a06b24SSage Weil end++; 11616a06b24SSage Weil } 11716a06b24SSage Weil } 11816a06b24SSage Weil if (*end == delim) { 119f27aaf4bSChristian Brunner *p = end + 1; 120f27aaf4bSChristian Brunner *end = '\0'; 121f27aaf4bSChristian Brunner } 122f27aaf4bSChristian Brunner } 123f27aaf4bSChristian Brunner l = strlen(src); 124f27aaf4bSChristian Brunner if (l >= dst_len) { 125d61563b2SMarkus Armbruster error_setg(errp, "%s too long", name); 126f27aaf4bSChristian Brunner return -EINVAL; 127f27aaf4bSChristian Brunner } else if (l == 0) { 128d61563b2SMarkus Armbruster error_setg(errp, "%s too short", name); 129f27aaf4bSChristian Brunner return -EINVAL; 130f27aaf4bSChristian Brunner } 131f27aaf4bSChristian Brunner 132f27aaf4bSChristian Brunner pstrcpy(dst, dst_len, src); 133f27aaf4bSChristian Brunner 134f27aaf4bSChristian Brunner return 0; 135f27aaf4bSChristian Brunner } 136f27aaf4bSChristian Brunner 13716a06b24SSage Weil static void qemu_rbd_unescape(char *src) 13816a06b24SSage Weil { 13916a06b24SSage Weil char *p; 14016a06b24SSage Weil 14116a06b24SSage Weil for (p = src; *src; ++src, ++p) { 14216a06b24SSage Weil if (*src == '\\' && src[1] != '\0') { 14316a06b24SSage Weil src++; 14416a06b24SSage Weil } 14516a06b24SSage Weil *p = *src; 14616a06b24SSage Weil } 14716a06b24SSage Weil *p = '\0'; 14816a06b24SSage Weil } 14916a06b24SSage Weil 150ad32e9c0SJosh Durgin static int qemu_rbd_parsename(const char *filename, 151f27aaf4bSChristian Brunner char *pool, int pool_len, 152f27aaf4bSChristian Brunner char *snap, int snap_len, 153fab5cf59SJosh Durgin char *name, int name_len, 154d61563b2SMarkus Armbruster char *conf, int conf_len, 155d61563b2SMarkus Armbruster Error **errp) 156f27aaf4bSChristian Brunner { 157f27aaf4bSChristian Brunner const char *start; 158f27aaf4bSChristian Brunner char *p, *buf; 159f27aaf4bSChristian Brunner int ret; 160f27aaf4bSChristian Brunner 161f27aaf4bSChristian Brunner if (!strstart(filename, "rbd:", &start)) { 162d61563b2SMarkus Armbruster error_setg(errp, "File name must start with 'rbd:'"); 163f27aaf4bSChristian Brunner return -EINVAL; 164f27aaf4bSChristian Brunner } 165f27aaf4bSChristian Brunner 1667267c094SAnthony Liguori buf = g_strdup(start); 167f27aaf4bSChristian Brunner p = buf; 168fab5cf59SJosh Durgin *snap = '\0'; 169fab5cf59SJosh Durgin *conf = '\0'; 170f27aaf4bSChristian Brunner 171d61563b2SMarkus Armbruster ret = qemu_rbd_next_tok(pool, pool_len, p, 172d61563b2SMarkus Armbruster '/', "pool name", &p, errp); 173f27aaf4bSChristian Brunner if (ret < 0 || !p) { 174f27aaf4bSChristian Brunner ret = -EINVAL; 175f27aaf4bSChristian Brunner goto done; 176f27aaf4bSChristian Brunner } 17716a06b24SSage Weil qemu_rbd_unescape(pool); 178fab5cf59SJosh Durgin 179fab5cf59SJosh Durgin if (strchr(p, '@')) { 180d61563b2SMarkus Armbruster ret = qemu_rbd_next_tok(name, name_len, p, 181d61563b2SMarkus Armbruster '@', "object name", &p, errp); 182f27aaf4bSChristian Brunner if (ret < 0) { 183f27aaf4bSChristian Brunner goto done; 184f27aaf4bSChristian Brunner } 185d61563b2SMarkus Armbruster ret = qemu_rbd_next_tok(snap, snap_len, p, 186d61563b2SMarkus Armbruster ':', "snap name", &p, errp); 18716a06b24SSage Weil qemu_rbd_unescape(snap); 188fab5cf59SJosh Durgin } else { 189d61563b2SMarkus Armbruster ret = qemu_rbd_next_tok(name, name_len, p, 190d61563b2SMarkus Armbruster ':', "object name", &p, errp); 191fab5cf59SJosh Durgin } 19216a06b24SSage Weil qemu_rbd_unescape(name); 193fab5cf59SJosh Durgin if (ret < 0 || !p) { 194f27aaf4bSChristian Brunner goto done; 195f27aaf4bSChristian Brunner } 196f27aaf4bSChristian Brunner 197d61563b2SMarkus Armbruster ret = qemu_rbd_next_tok(conf, conf_len, p, 198d61563b2SMarkus Armbruster '\0', "configuration", &p, errp); 199f27aaf4bSChristian Brunner 200f27aaf4bSChristian Brunner done: 2017267c094SAnthony Liguori g_free(buf); 202f27aaf4bSChristian Brunner return ret; 203f27aaf4bSChristian Brunner } 204f27aaf4bSChristian Brunner 2057c7e9df0SSage Weil static char *qemu_rbd_parse_clientname(const char *conf, char *clientname) 2067c7e9df0SSage Weil { 2077c7e9df0SSage Weil const char *p = conf; 2087c7e9df0SSage Weil 2097c7e9df0SSage Weil while (*p) { 2107c7e9df0SSage Weil int len; 2117c7e9df0SSage Weil const char *end = strchr(p, ':'); 2127c7e9df0SSage Weil 2137c7e9df0SSage Weil if (end) { 2147c7e9df0SSage Weil len = end - p; 2157c7e9df0SSage Weil } else { 2167c7e9df0SSage Weil len = strlen(p); 2177c7e9df0SSage Weil } 2187c7e9df0SSage Weil 2197c7e9df0SSage Weil if (strncmp(p, "id=", 3) == 0) { 2207c7e9df0SSage Weil len -= 3; 2217c7e9df0SSage Weil strncpy(clientname, p + 3, len); 2227c7e9df0SSage Weil clientname[len] = '\0'; 2237c7e9df0SSage Weil return clientname; 2247c7e9df0SSage Weil } 2257c7e9df0SSage Weil if (end == NULL) { 2267c7e9df0SSage Weil break; 2277c7e9df0SSage Weil } 2287c7e9df0SSage Weil p = end + 1; 2297c7e9df0SSage Weil } 2307c7e9df0SSage Weil return NULL; 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 25360390a21SDaniel P. Berrange 254e34d8f29SJosh Durgin static int qemu_rbd_set_conf(rados_t cluster, const char *conf, 255e34d8f29SJosh Durgin bool only_read_conf_file, 256e34d8f29SJosh Durgin Error **errp) 257fab5cf59SJosh Durgin { 258fab5cf59SJosh Durgin char *p, *buf; 259fab5cf59SJosh Durgin char name[RBD_MAX_CONF_NAME_SIZE]; 260fab5cf59SJosh Durgin char value[RBD_MAX_CONF_VAL_SIZE]; 261fab5cf59SJosh Durgin int ret = 0; 262fab5cf59SJosh Durgin 2637267c094SAnthony Liguori buf = g_strdup(conf); 264fab5cf59SJosh Durgin p = buf; 265fab5cf59SJosh Durgin 266fab5cf59SJosh Durgin while (p) { 267fab5cf59SJosh Durgin ret = qemu_rbd_next_tok(name, sizeof(name), p, 268d61563b2SMarkus Armbruster '=', "conf option name", &p, errp); 269fab5cf59SJosh Durgin if (ret < 0) { 270fab5cf59SJosh Durgin break; 271fab5cf59SJosh Durgin } 27216a06b24SSage Weil qemu_rbd_unescape(name); 273fab5cf59SJosh Durgin 274fab5cf59SJosh Durgin if (!p) { 275d61563b2SMarkus Armbruster error_setg(errp, "conf option %s has no value", name); 276fab5cf59SJosh Durgin ret = -EINVAL; 277fab5cf59SJosh Durgin break; 278fab5cf59SJosh Durgin } 279fab5cf59SJosh Durgin 280fab5cf59SJosh Durgin ret = qemu_rbd_next_tok(value, sizeof(value), p, 281d61563b2SMarkus Armbruster ':', "conf option value", &p, errp); 282fab5cf59SJosh Durgin if (ret < 0) { 283fab5cf59SJosh Durgin break; 284fab5cf59SJosh Durgin } 28516a06b24SSage Weil qemu_rbd_unescape(value); 286fab5cf59SJosh Durgin 2877c7e9df0SSage Weil if (strcmp(name, "conf") == 0) { 288e34d8f29SJosh Durgin /* read the conf file alone, so it doesn't override more 289e34d8f29SJosh Durgin specific settings for a particular device */ 290e34d8f29SJosh Durgin if (only_read_conf_file) { 2917c7e9df0SSage Weil ret = rados_conf_read_file(cluster, value); 2927c7e9df0SSage Weil if (ret < 0) { 29387cd3d20SVikhyat Umrao error_setg_errno(errp, -ret, "error reading conf file %s", 29487cd3d20SVikhyat Umrao value); 2957c7e9df0SSage Weil break; 2967c7e9df0SSage Weil } 297e34d8f29SJosh Durgin } 2987c7e9df0SSage Weil } else if (strcmp(name, "id") == 0) { 2997c7e9df0SSage Weil /* ignore, this is parsed by qemu_rbd_parse_clientname() */ 300e34d8f29SJosh Durgin } else if (!only_read_conf_file) { 301fab5cf59SJosh Durgin ret = rados_conf_set(cluster, name, value); 302fab5cf59SJosh Durgin if (ret < 0) { 30387cd3d20SVikhyat Umrao error_setg_errno(errp, -ret, "invalid conf option %s", name); 304fab5cf59SJosh Durgin ret = -EINVAL; 305fab5cf59SJosh Durgin break; 306fab5cf59SJosh Durgin } 307fab5cf59SJosh Durgin } 308fab5cf59SJosh Durgin } 309fab5cf59SJosh Durgin 3107267c094SAnthony Liguori g_free(buf); 311fab5cf59SJosh Durgin return ret; 312fab5cf59SJosh Durgin } 313fab5cf59SJosh Durgin 314bd0cf596SChunyan Liu static int qemu_rbd_create(const char *filename, QemuOpts *opts, Error **errp) 315f27aaf4bSChristian Brunner { 316d61563b2SMarkus Armbruster Error *local_err = NULL; 317f27aaf4bSChristian Brunner int64_t bytes = 0; 318f27aaf4bSChristian Brunner int64_t objsize; 319ad32e9c0SJosh Durgin int obj_order = 0; 320ad32e9c0SJosh Durgin char pool[RBD_MAX_POOL_NAME_SIZE]; 321ad32e9c0SJosh Durgin char name[RBD_MAX_IMAGE_NAME_SIZE]; 322ad32e9c0SJosh Durgin char snap_buf[RBD_MAX_SNAP_NAME_SIZE]; 323fab5cf59SJosh Durgin char conf[RBD_MAX_CONF_SIZE]; 3247c7e9df0SSage Weil char clientname_buf[RBD_MAX_CONF_SIZE]; 3257c7e9df0SSage Weil char *clientname; 32660390a21SDaniel P. Berrange const char *secretid; 327ad32e9c0SJosh Durgin rados_t cluster; 328ad32e9c0SJosh Durgin rados_ioctx_t io_ctx; 329f27aaf4bSChristian Brunner int ret; 330f27aaf4bSChristian Brunner 33160390a21SDaniel P. Berrange secretid = qemu_opt_get(opts, "password-secret"); 33260390a21SDaniel P. Berrange 333ad32e9c0SJosh Durgin if (qemu_rbd_parsename(filename, pool, sizeof(pool), 334f27aaf4bSChristian Brunner snap_buf, sizeof(snap_buf), 335fab5cf59SJosh Durgin name, sizeof(name), 336d61563b2SMarkus Armbruster conf, sizeof(conf), &local_err) < 0) { 337d61563b2SMarkus Armbruster error_propagate(errp, local_err); 338f27aaf4bSChristian Brunner return -EINVAL; 339f27aaf4bSChristian Brunner } 340f27aaf4bSChristian Brunner 341f27aaf4bSChristian Brunner /* Read out options */ 342c2eb918eSHu Tao bytes = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 343c2eb918eSHu Tao BDRV_SECTOR_SIZE); 344bd0cf596SChunyan Liu objsize = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE, 0); 345bd0cf596SChunyan Liu if (objsize) { 346f27aaf4bSChristian Brunner if ((objsize - 1) & objsize) { /* not a power of 2? */ 347d61563b2SMarkus Armbruster error_setg(errp, "obj size needs to be power of 2"); 348f27aaf4bSChristian Brunner return -EINVAL; 349f27aaf4bSChristian Brunner } 350f27aaf4bSChristian Brunner if (objsize < 4096) { 351d61563b2SMarkus Armbruster error_setg(errp, "obj size too small"); 352f27aaf4bSChristian Brunner return -EINVAL; 353f27aaf4bSChristian Brunner } 354786a4ea8SStefan Hajnoczi obj_order = ctz32(objsize); 355f27aaf4bSChristian Brunner } 356f27aaf4bSChristian Brunner 3577c7e9df0SSage Weil clientname = qemu_rbd_parse_clientname(conf, clientname_buf); 35887cd3d20SVikhyat Umrao ret = rados_create(&cluster, clientname); 35987cd3d20SVikhyat Umrao if (ret < 0) { 36087cd3d20SVikhyat Umrao error_setg_errno(errp, -ret, "error initializing"); 36187cd3d20SVikhyat Umrao return ret; 362f27aaf4bSChristian Brunner } 363f27aaf4bSChristian Brunner 364fab5cf59SJosh Durgin if (strstr(conf, "conf=") == NULL) { 365f9fe18ecSSage Weil /* try default location, but ignore failure */ 366f9fe18ecSSage Weil rados_conf_read_file(cluster, NULL); 367e34d8f29SJosh Durgin } else if (conf[0] != '\0' && 368e34d8f29SJosh Durgin qemu_rbd_set_conf(cluster, conf, true, &local_err) < 0) { 369e34d8f29SJosh Durgin rados_shutdown(cluster); 370e34d8f29SJosh Durgin error_propagate(errp, local_err); 371e34d8f29SJosh Durgin return -EIO; 372fab5cf59SJosh Durgin } 373fab5cf59SJosh Durgin 374fab5cf59SJosh Durgin if (conf[0] != '\0' && 375e34d8f29SJosh Durgin qemu_rbd_set_conf(cluster, conf, false, &local_err) < 0) { 376fab5cf59SJosh Durgin rados_shutdown(cluster); 377d61563b2SMarkus Armbruster error_propagate(errp, local_err); 378fab5cf59SJosh Durgin return -EIO; 379fab5cf59SJosh Durgin } 380ad32e9c0SJosh Durgin 38160390a21SDaniel P. Berrange if (qemu_rbd_set_auth(cluster, secretid, errp) < 0) { 38260390a21SDaniel P. Berrange rados_shutdown(cluster); 38360390a21SDaniel P. Berrange return -EIO; 38460390a21SDaniel P. Berrange } 38560390a21SDaniel P. Berrange 38687cd3d20SVikhyat Umrao ret = rados_connect(cluster); 38787cd3d20SVikhyat Umrao if (ret < 0) { 38887cd3d20SVikhyat Umrao error_setg_errno(errp, -ret, "error connecting"); 389ad32e9c0SJosh Durgin rados_shutdown(cluster); 39087cd3d20SVikhyat Umrao return ret; 391ad32e9c0SJosh Durgin } 392ad32e9c0SJosh Durgin 39387cd3d20SVikhyat Umrao ret = rados_ioctx_create(cluster, pool, &io_ctx); 39487cd3d20SVikhyat Umrao if (ret < 0) { 39587cd3d20SVikhyat Umrao error_setg_errno(errp, -ret, "error opening pool %s", pool); 396ad32e9c0SJosh Durgin rados_shutdown(cluster); 39787cd3d20SVikhyat Umrao return ret; 398f27aaf4bSChristian Brunner } 399f27aaf4bSChristian Brunner 400ad32e9c0SJosh Durgin ret = rbd_create(io_ctx, name, bytes, &obj_order); 401ad32e9c0SJosh Durgin rados_ioctx_destroy(io_ctx); 402ad32e9c0SJosh Durgin rados_shutdown(cluster); 40387cd3d20SVikhyat Umrao if (ret < 0) { 40487cd3d20SVikhyat Umrao error_setg_errno(errp, -ret, "error rbd create"); 40587cd3d20SVikhyat Umrao return ret; 40687cd3d20SVikhyat Umrao } 407f27aaf4bSChristian Brunner 408f27aaf4bSChristian Brunner return ret; 409f27aaf4bSChristian Brunner } 410f27aaf4bSChristian Brunner 411f27aaf4bSChristian Brunner /* 412e04fb07fSStefan Hajnoczi * This aio completion is being called from rbd_finish_bh() and runs in qemu 413e04fb07fSStefan Hajnoczi * BH context. 414f27aaf4bSChristian Brunner */ 415ad32e9c0SJosh Durgin static void qemu_rbd_complete_aio(RADOSCB *rcb) 416f27aaf4bSChristian Brunner { 417f27aaf4bSChristian Brunner RBDAIOCB *acb = rcb->acb; 418f27aaf4bSChristian Brunner int64_t r; 419f27aaf4bSChristian Brunner 420f27aaf4bSChristian Brunner r = rcb->ret; 421f27aaf4bSChristian Brunner 422dc7588c1SJosh Durgin if (acb->cmd != RBD_AIO_READ) { 423f27aaf4bSChristian Brunner if (r < 0) { 424f27aaf4bSChristian Brunner acb->ret = r; 425f27aaf4bSChristian Brunner acb->error = 1; 426f27aaf4bSChristian Brunner } else if (!acb->error) { 427ad32e9c0SJosh Durgin acb->ret = rcb->size; 428f27aaf4bSChristian Brunner } 429f27aaf4bSChristian Brunner } else { 430ad32e9c0SJosh Durgin if (r < 0) { 431ad32e9c0SJosh Durgin memset(rcb->buf, 0, rcb->size); 432f27aaf4bSChristian Brunner acb->ret = r; 433f27aaf4bSChristian Brunner acb->error = 1; 434ad32e9c0SJosh Durgin } else if (r < rcb->size) { 435ad32e9c0SJosh Durgin memset(rcb->buf + r, 0, rcb->size - r); 436f27aaf4bSChristian Brunner if (!acb->error) { 437ad32e9c0SJosh Durgin acb->ret = rcb->size; 438f27aaf4bSChristian Brunner } 439f27aaf4bSChristian Brunner } else if (!acb->error) { 440ad32e9c0SJosh Durgin acb->ret = r; 441f27aaf4bSChristian Brunner } 442f27aaf4bSChristian Brunner } 443e04fb07fSStefan Hajnoczi 4447267c094SAnthony Liguori g_free(rcb); 445e04fb07fSStefan Hajnoczi 446e04fb07fSStefan Hajnoczi if (acb->cmd == RBD_AIO_READ) { 447e04fb07fSStefan Hajnoczi qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); 448f27aaf4bSChristian Brunner } 449e04fb07fSStefan Hajnoczi qemu_vfree(acb->bounce); 450e04fb07fSStefan Hajnoczi acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret)); 451f27aaf4bSChristian Brunner 4528007429aSFam Zheng qemu_aio_unref(acb); 453f27aaf4bSChristian Brunner } 454f27aaf4bSChristian Brunner 455a9ccedc3SKevin Wolf /* TODO Convert to fine grained options */ 456a9ccedc3SKevin Wolf static QemuOptsList runtime_opts = { 457a9ccedc3SKevin Wolf .name = "rbd", 458a9ccedc3SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), 459a9ccedc3SKevin Wolf .desc = { 460a9ccedc3SKevin Wolf { 461a9ccedc3SKevin Wolf .name = "filename", 462a9ccedc3SKevin Wolf .type = QEMU_OPT_STRING, 463a9ccedc3SKevin Wolf .help = "Specification of the rbd image", 464a9ccedc3SKevin Wolf }, 46560390a21SDaniel P. Berrange { 46660390a21SDaniel P. Berrange .name = "password-secret", 46760390a21SDaniel P. Berrange .type = QEMU_OPT_STRING, 46860390a21SDaniel P. Berrange .help = "ID of secret providing the password", 46960390a21SDaniel P. Berrange }, 470a9ccedc3SKevin Wolf { /* end of list */ } 471a9ccedc3SKevin Wolf }, 472a9ccedc3SKevin Wolf }; 473a9ccedc3SKevin Wolf 474015a1036SMax Reitz static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags, 475015a1036SMax Reitz Error **errp) 476f27aaf4bSChristian Brunner { 477f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 478ad32e9c0SJosh Durgin char pool[RBD_MAX_POOL_NAME_SIZE]; 479ad32e9c0SJosh Durgin char snap_buf[RBD_MAX_SNAP_NAME_SIZE]; 480fab5cf59SJosh Durgin char conf[RBD_MAX_CONF_SIZE]; 4817c7e9df0SSage Weil char clientname_buf[RBD_MAX_CONF_SIZE]; 4827c7e9df0SSage Weil char *clientname; 48360390a21SDaniel P. Berrange const char *secretid; 484a9ccedc3SKevin Wolf QemuOpts *opts; 485a9ccedc3SKevin Wolf Error *local_err = NULL; 486a9ccedc3SKevin Wolf const char *filename; 487f27aaf4bSChristian Brunner int r; 488f27aaf4bSChristian Brunner 48987ea75d5SPeter Crosthwaite opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); 490a9ccedc3SKevin Wolf qemu_opts_absorb_qdict(opts, options, &local_err); 49184d18f06SMarkus Armbruster if (local_err) { 492d61563b2SMarkus Armbruster error_propagate(errp, local_err); 493a9ccedc3SKevin Wolf qemu_opts_del(opts); 494a9ccedc3SKevin Wolf return -EINVAL; 495a9ccedc3SKevin Wolf } 496a9ccedc3SKevin Wolf 497a9ccedc3SKevin Wolf filename = qemu_opt_get(opts, "filename"); 49860390a21SDaniel P. Berrange secretid = qemu_opt_get(opts, "password-secret"); 499a9ccedc3SKevin Wolf 500ad32e9c0SJosh Durgin if (qemu_rbd_parsename(filename, pool, sizeof(pool), 501f27aaf4bSChristian Brunner snap_buf, sizeof(snap_buf), 502fab5cf59SJosh Durgin s->name, sizeof(s->name), 503d61563b2SMarkus Armbruster conf, sizeof(conf), errp) < 0) { 504c3ca988dSKevin Wolf r = -EINVAL; 505c3ca988dSKevin Wolf goto failed_opts; 506f27aaf4bSChristian Brunner } 507f27aaf4bSChristian Brunner 5087c7e9df0SSage Weil clientname = qemu_rbd_parse_clientname(conf, clientname_buf); 5097c7e9df0SSage Weil r = rados_create(&s->cluster, clientname); 510ad32e9c0SJosh Durgin if (r < 0) { 51187cd3d20SVikhyat Umrao error_setg_errno(errp, -r, "error initializing"); 512c3ca988dSKevin Wolf goto failed_opts; 513f27aaf4bSChristian Brunner } 514f27aaf4bSChristian Brunner 515eb93d5d9SSage Weil s->snap = NULL; 516eb93d5d9SSage Weil if (snap_buf[0] != '\0') { 517eb93d5d9SSage Weil s->snap = g_strdup(snap_buf); 518eb93d5d9SSage Weil } 519eb93d5d9SSage Weil 52099a3c89dSJosh Durgin if (strstr(conf, "conf=") == NULL) { 52199a3c89dSJosh Durgin /* try default location, but ignore failure */ 52299a3c89dSJosh Durgin rados_conf_read_file(s->cluster, NULL); 523e34d8f29SJosh Durgin } else if (conf[0] != '\0') { 524e34d8f29SJosh Durgin r = qemu_rbd_set_conf(s->cluster, conf, true, errp); 525e34d8f29SJosh Durgin if (r < 0) { 526e34d8f29SJosh Durgin goto failed_shutdown; 527e34d8f29SJosh Durgin } 52899a3c89dSJosh Durgin } 52999a3c89dSJosh Durgin 53099a3c89dSJosh Durgin if (conf[0] != '\0') { 531e34d8f29SJosh Durgin r = qemu_rbd_set_conf(s->cluster, conf, false, errp); 53299a3c89dSJosh Durgin if (r < 0) { 53399a3c89dSJosh Durgin goto failed_shutdown; 53499a3c89dSJosh Durgin } 53599a3c89dSJosh Durgin } 53699a3c89dSJosh Durgin 53760390a21SDaniel P. Berrange if (qemu_rbd_set_auth(s->cluster, secretid, errp) < 0) { 53860390a21SDaniel P. Berrange r = -EIO; 53960390a21SDaniel P. Berrange goto failed_shutdown; 54060390a21SDaniel P. Berrange } 54160390a21SDaniel P. Berrange 542b11f38fcSJosh Durgin /* 543b11f38fcSJosh Durgin * Fallback to more conservative semantics if setting cache 544b11f38fcSJosh Durgin * options fails. Ignore errors from setting rbd_cache because the 545b11f38fcSJosh Durgin * only possible error is that the option does not exist, and 546b11f38fcSJosh Durgin * librbd defaults to no caching. If write through caching cannot 547b11f38fcSJosh Durgin * be set up, fall back to no caching. 548b11f38fcSJosh Durgin */ 549b11f38fcSJosh Durgin if (flags & BDRV_O_NOCACHE) { 550b11f38fcSJosh Durgin rados_conf_set(s->cluster, "rbd_cache", "false"); 551b11f38fcSJosh Durgin } else { 552b11f38fcSJosh Durgin rados_conf_set(s->cluster, "rbd_cache", "true"); 553b11f38fcSJosh Durgin } 554b11f38fcSJosh Durgin 555ad32e9c0SJosh Durgin r = rados_connect(s->cluster); 556ad32e9c0SJosh Durgin if (r < 0) { 55787cd3d20SVikhyat Umrao error_setg_errno(errp, -r, "error connecting"); 558eb93d5d9SSage Weil goto failed_shutdown; 559ad32e9c0SJosh Durgin } 560ad32e9c0SJosh Durgin 561ad32e9c0SJosh Durgin r = rados_ioctx_create(s->cluster, pool, &s->io_ctx); 562ad32e9c0SJosh Durgin if (r < 0) { 56387cd3d20SVikhyat Umrao error_setg_errno(errp, -r, "error opening pool %s", pool); 564eb93d5d9SSage Weil goto failed_shutdown; 565ad32e9c0SJosh Durgin } 566ad32e9c0SJosh Durgin 567ad32e9c0SJosh Durgin r = rbd_open(s->io_ctx, s->name, &s->image, s->snap); 568ad32e9c0SJosh Durgin if (r < 0) { 56987cd3d20SVikhyat Umrao error_setg_errno(errp, -r, "error reading header from %s", s->name); 570eb93d5d9SSage Weil goto failed_open; 571ad32e9c0SJosh Durgin } 572ad32e9c0SJosh Durgin 573ad32e9c0SJosh Durgin bs->read_only = (s->snap != NULL); 574f27aaf4bSChristian Brunner 575c3ca988dSKevin Wolf qemu_opts_del(opts); 576f27aaf4bSChristian Brunner return 0; 577f27aaf4bSChristian Brunner 578eb93d5d9SSage Weil failed_open: 579ad32e9c0SJosh Durgin rados_ioctx_destroy(s->io_ctx); 580eb93d5d9SSage Weil failed_shutdown: 581ad32e9c0SJosh Durgin rados_shutdown(s->cluster); 582eb93d5d9SSage Weil g_free(s->snap); 583c3ca988dSKevin Wolf failed_opts: 584c3ca988dSKevin Wolf qemu_opts_del(opts); 585f27aaf4bSChristian Brunner return r; 586f27aaf4bSChristian Brunner } 587f27aaf4bSChristian Brunner 588ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs) 589f27aaf4bSChristian Brunner { 590f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 591f27aaf4bSChristian Brunner 592ad32e9c0SJosh Durgin rbd_close(s->image); 593ad32e9c0SJosh Durgin rados_ioctx_destroy(s->io_ctx); 5947267c094SAnthony Liguori g_free(s->snap); 595ad32e9c0SJosh Durgin rados_shutdown(s->cluster); 596f27aaf4bSChristian Brunner } 597f27aaf4bSChristian Brunner 598d7331bedSStefan Hajnoczi static const AIOCBInfo rbd_aiocb_info = { 599f27aaf4bSChristian Brunner .aiocb_size = sizeof(RBDAIOCB), 600f27aaf4bSChristian Brunner }; 601f27aaf4bSChristian Brunner 602e04fb07fSStefan Hajnoczi static void rbd_finish_bh(void *opaque) 603f27aaf4bSChristian Brunner { 604e04fb07fSStefan Hajnoczi RADOSCB *rcb = opaque; 605e04fb07fSStefan Hajnoczi qemu_bh_delete(rcb->acb->bh); 606e04fb07fSStefan Hajnoczi qemu_rbd_complete_aio(rcb); 607ad32e9c0SJosh Durgin } 608ad32e9c0SJosh Durgin 609ad32e9c0SJosh Durgin /* 610ad32e9c0SJosh Durgin * This is the callback function for rbd_aio_read and _write 611ad32e9c0SJosh Durgin * 612ad32e9c0SJosh Durgin * Note: this function is being called from a non qemu thread so 613ad32e9c0SJosh Durgin * we need to be careful about what we do here. Generally we only 614e04fb07fSStefan Hajnoczi * schedule a BH, and do the rest of the io completion handling 615e04fb07fSStefan Hajnoczi * from rbd_finish_bh() which runs in a qemu context. 616ad32e9c0SJosh Durgin */ 617ad32e9c0SJosh Durgin static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb) 618ad32e9c0SJosh Durgin { 619e04fb07fSStefan Hajnoczi RBDAIOCB *acb = rcb->acb; 620e04fb07fSStefan Hajnoczi 621ad32e9c0SJosh Durgin rcb->ret = rbd_aio_get_return_value(c); 622ad32e9c0SJosh Durgin rbd_aio_release(c); 623f27aaf4bSChristian Brunner 624ea800191SStefan Hajnoczi acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs), 625ea800191SStefan Hajnoczi rbd_finish_bh, rcb); 626e04fb07fSStefan Hajnoczi qemu_bh_schedule(acb->bh); 627473c7f02SStefan Priebe } 628f27aaf4bSChristian Brunner 629787f3133SJosh Durgin static int rbd_aio_discard_wrapper(rbd_image_t image, 630787f3133SJosh Durgin uint64_t off, 631787f3133SJosh Durgin uint64_t len, 632787f3133SJosh Durgin rbd_completion_t comp) 633787f3133SJosh Durgin { 634787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 635787f3133SJosh Durgin return rbd_aio_discard(image, off, len, comp); 636787f3133SJosh Durgin #else 637787f3133SJosh Durgin return -ENOTSUP; 638787f3133SJosh Durgin #endif 639787f3133SJosh Durgin } 640787f3133SJosh Durgin 641dc7588c1SJosh Durgin static int rbd_aio_flush_wrapper(rbd_image_t image, 642dc7588c1SJosh Durgin rbd_completion_t comp) 643dc7588c1SJosh Durgin { 644dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH 645dc7588c1SJosh Durgin return rbd_aio_flush(image, comp); 646dc7588c1SJosh Durgin #else 647dc7588c1SJosh Durgin return -ENOTSUP; 648dc7588c1SJosh Durgin #endif 649dc7588c1SJosh Durgin } 650dc7588c1SJosh Durgin 6517c84b1b8SMarkus Armbruster static BlockAIOCB *rbd_start_aio(BlockDriverState *bs, 652f27aaf4bSChristian Brunner int64_t sector_num, 653f27aaf4bSChristian Brunner QEMUIOVector *qiov, 654f27aaf4bSChristian Brunner int nb_sectors, 655097310b5SMarkus Armbruster BlockCompletionFunc *cb, 656787f3133SJosh Durgin void *opaque, 657787f3133SJosh Durgin RBDAIOCmd cmd) 658f27aaf4bSChristian Brunner { 659f27aaf4bSChristian Brunner RBDAIOCB *acb; 6600f7a0237SKevin Wolf RADOSCB *rcb = NULL; 661ad32e9c0SJosh Durgin rbd_completion_t c; 662f27aaf4bSChristian Brunner int64_t off, size; 663f27aaf4bSChristian Brunner char *buf; 66451a13528SJosh Durgin int r; 665f27aaf4bSChristian Brunner 666f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 667f27aaf4bSChristian Brunner 668d7331bedSStefan Hajnoczi acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque); 669787f3133SJosh Durgin acb->cmd = cmd; 670f27aaf4bSChristian Brunner acb->qiov = qiov; 671dc7588c1SJosh Durgin if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) { 672787f3133SJosh Durgin acb->bounce = NULL; 673787f3133SJosh Durgin } else { 6740f7a0237SKevin Wolf acb->bounce = qemu_try_blockalign(bs, qiov->size); 6750f7a0237SKevin Wolf if (acb->bounce == NULL) { 6760f7a0237SKevin Wolf goto failed; 6770f7a0237SKevin Wolf } 678787f3133SJosh Durgin } 679f27aaf4bSChristian Brunner acb->ret = 0; 680f27aaf4bSChristian Brunner acb->error = 0; 681f27aaf4bSChristian Brunner acb->s = s; 682f27aaf4bSChristian Brunner acb->bh = NULL; 683f27aaf4bSChristian Brunner 684787f3133SJosh Durgin if (cmd == RBD_AIO_WRITE) { 685d5e6b161SMichael Tokarev qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); 686f27aaf4bSChristian Brunner } 687f27aaf4bSChristian Brunner 688f27aaf4bSChristian Brunner buf = acb->bounce; 689f27aaf4bSChristian Brunner 690f27aaf4bSChristian Brunner off = sector_num * BDRV_SECTOR_SIZE; 691f27aaf4bSChristian Brunner size = nb_sectors * BDRV_SECTOR_SIZE; 692f27aaf4bSChristian Brunner 6935839e53bSMarkus Armbruster rcb = g_new(RADOSCB, 1); 694f27aaf4bSChristian Brunner rcb->acb = acb; 695f27aaf4bSChristian Brunner rcb->buf = buf; 696f27aaf4bSChristian Brunner rcb->s = acb->s; 697ad32e9c0SJosh Durgin rcb->size = size; 69851a13528SJosh Durgin r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c); 69951a13528SJosh Durgin if (r < 0) { 70051a13528SJosh Durgin goto failed; 70151a13528SJosh Durgin } 702f27aaf4bSChristian Brunner 703787f3133SJosh Durgin switch (cmd) { 704787f3133SJosh Durgin case RBD_AIO_WRITE: 70551a13528SJosh Durgin r = rbd_aio_write(s->image, off, size, buf, c); 706787f3133SJosh Durgin break; 707787f3133SJosh Durgin case RBD_AIO_READ: 70851a13528SJosh Durgin r = rbd_aio_read(s->image, off, size, buf, c); 709787f3133SJosh Durgin break; 710787f3133SJosh Durgin case RBD_AIO_DISCARD: 711787f3133SJosh Durgin r = rbd_aio_discard_wrapper(s->image, off, size, c); 712787f3133SJosh Durgin break; 713dc7588c1SJosh Durgin case RBD_AIO_FLUSH: 714dc7588c1SJosh Durgin r = rbd_aio_flush_wrapper(s->image, c); 715dc7588c1SJosh Durgin break; 716787f3133SJosh Durgin default: 717787f3133SJosh Durgin r = -EINVAL; 71851a13528SJosh Durgin } 71951a13528SJosh Durgin 72051a13528SJosh Durgin if (r < 0) { 721405a2764SKevin Wolf goto failed_completion; 722f27aaf4bSChristian Brunner } 723f27aaf4bSChristian Brunner 724f27aaf4bSChristian Brunner return &acb->common; 72551a13528SJosh Durgin 726405a2764SKevin Wolf failed_completion: 727405a2764SKevin Wolf rbd_aio_release(c); 72851a13528SJosh Durgin failed: 7297267c094SAnthony Liguori g_free(rcb); 730405a2764SKevin Wolf qemu_vfree(acb->bounce); 7318007429aSFam Zheng qemu_aio_unref(acb); 73251a13528SJosh Durgin return NULL; 733f27aaf4bSChristian Brunner } 734f27aaf4bSChristian Brunner 7357c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs, 736ad32e9c0SJosh Durgin int64_t sector_num, 737ad32e9c0SJosh Durgin QEMUIOVector *qiov, 738f27aaf4bSChristian Brunner int nb_sectors, 739097310b5SMarkus Armbruster BlockCompletionFunc *cb, 740f27aaf4bSChristian Brunner void *opaque) 741f27aaf4bSChristian Brunner { 742787f3133SJosh Durgin return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque, 743787f3133SJosh Durgin RBD_AIO_READ); 744f27aaf4bSChristian Brunner } 745f27aaf4bSChristian Brunner 7467c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs, 747ad32e9c0SJosh Durgin int64_t sector_num, 748ad32e9c0SJosh Durgin QEMUIOVector *qiov, 749f27aaf4bSChristian Brunner int nb_sectors, 750097310b5SMarkus Armbruster BlockCompletionFunc *cb, 751f27aaf4bSChristian Brunner void *opaque) 752f27aaf4bSChristian Brunner { 753787f3133SJosh Durgin return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque, 754787f3133SJosh Durgin RBD_AIO_WRITE); 755f27aaf4bSChristian Brunner } 756f27aaf4bSChristian Brunner 757dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH 7587c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs, 759097310b5SMarkus Armbruster BlockCompletionFunc *cb, 760dc7588c1SJosh Durgin void *opaque) 761dc7588c1SJosh Durgin { 762dc7588c1SJosh Durgin return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH); 763dc7588c1SJosh Durgin } 764dc7588c1SJosh Durgin 765dc7588c1SJosh Durgin #else 766dc7588c1SJosh Durgin 7678b94ff85SPaolo Bonzini static int qemu_rbd_co_flush(BlockDriverState *bs) 7687a3f5fe9SSage Weil { 7697a3f5fe9SSage Weil #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1) 7707a3f5fe9SSage Weil /* rbd_flush added in 0.1.1 */ 7717a3f5fe9SSage Weil BDRVRBDState *s = bs->opaque; 7727a3f5fe9SSage Weil return rbd_flush(s->image); 7737a3f5fe9SSage Weil #else 7747a3f5fe9SSage Weil return 0; 7757a3f5fe9SSage Weil #endif 7767a3f5fe9SSage Weil } 777dc7588c1SJosh Durgin #endif 7787a3f5fe9SSage Weil 779ad32e9c0SJosh Durgin static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi) 780f27aaf4bSChristian Brunner { 781f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 782ad32e9c0SJosh Durgin rbd_image_info_t info; 783ad32e9c0SJosh Durgin int r; 784ad32e9c0SJosh Durgin 785ad32e9c0SJosh Durgin r = rbd_stat(s->image, &info, sizeof(info)); 786ad32e9c0SJosh Durgin if (r < 0) { 787ad32e9c0SJosh Durgin return r; 788ad32e9c0SJosh Durgin } 789ad32e9c0SJosh Durgin 790ad32e9c0SJosh Durgin bdi->cluster_size = info.obj_size; 791f27aaf4bSChristian Brunner return 0; 792f27aaf4bSChristian Brunner } 793f27aaf4bSChristian Brunner 794ad32e9c0SJosh Durgin static int64_t qemu_rbd_getlength(BlockDriverState *bs) 795f27aaf4bSChristian Brunner { 796f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 797ad32e9c0SJosh Durgin rbd_image_info_t info; 798ad32e9c0SJosh Durgin int r; 799f27aaf4bSChristian Brunner 800ad32e9c0SJosh Durgin r = rbd_stat(s->image, &info, sizeof(info)); 801ad32e9c0SJosh Durgin if (r < 0) { 802ad32e9c0SJosh Durgin return r; 803f27aaf4bSChristian Brunner } 804f27aaf4bSChristian Brunner 805ad32e9c0SJosh Durgin return info.size; 806ad32e9c0SJosh Durgin } 807ad32e9c0SJosh Durgin 80830cdc48cSJosh Durgin static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset) 80930cdc48cSJosh Durgin { 81030cdc48cSJosh Durgin BDRVRBDState *s = bs->opaque; 81130cdc48cSJosh Durgin int r; 81230cdc48cSJosh Durgin 81330cdc48cSJosh Durgin r = rbd_resize(s->image, offset); 81430cdc48cSJosh Durgin if (r < 0) { 81530cdc48cSJosh Durgin return r; 81630cdc48cSJosh Durgin } 81730cdc48cSJosh Durgin 81830cdc48cSJosh Durgin return 0; 81930cdc48cSJosh Durgin } 82030cdc48cSJosh Durgin 821ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs, 822ad32e9c0SJosh Durgin QEMUSnapshotInfo *sn_info) 823f27aaf4bSChristian Brunner { 824f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 825f27aaf4bSChristian Brunner int r; 826f27aaf4bSChristian Brunner 827f27aaf4bSChristian Brunner if (sn_info->name[0] == '\0') { 828f27aaf4bSChristian Brunner return -EINVAL; /* we need a name for rbd snapshots */ 829f27aaf4bSChristian Brunner } 830f27aaf4bSChristian Brunner 831f27aaf4bSChristian Brunner /* 832f27aaf4bSChristian Brunner * rbd snapshots are using the name as the user controlled unique identifier 833f27aaf4bSChristian Brunner * we can't use the rbd snapid for that purpose, as it can't be set 834f27aaf4bSChristian Brunner */ 835f27aaf4bSChristian Brunner if (sn_info->id_str[0] != '\0' && 836f27aaf4bSChristian Brunner strcmp(sn_info->id_str, sn_info->name) != 0) { 837f27aaf4bSChristian Brunner return -EINVAL; 838f27aaf4bSChristian Brunner } 839f27aaf4bSChristian Brunner 840f27aaf4bSChristian Brunner if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) { 841f27aaf4bSChristian Brunner return -ERANGE; 842f27aaf4bSChristian Brunner } 843f27aaf4bSChristian Brunner 844ad32e9c0SJosh Durgin r = rbd_snap_create(s->image, sn_info->name); 845f27aaf4bSChristian Brunner if (r < 0) { 846ad32e9c0SJosh Durgin error_report("failed to create snap: %s", strerror(-r)); 847f27aaf4bSChristian Brunner return r; 848f27aaf4bSChristian Brunner } 849f27aaf4bSChristian Brunner 850f27aaf4bSChristian Brunner return 0; 851f27aaf4bSChristian Brunner } 852f27aaf4bSChristian Brunner 853bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs, 854a89d89d3SWenchao Xia const char *snapshot_id, 855a89d89d3SWenchao Xia const char *snapshot_name, 856a89d89d3SWenchao Xia Error **errp) 857bd603247SGregory Farnum { 858bd603247SGregory Farnum BDRVRBDState *s = bs->opaque; 859bd603247SGregory Farnum int r; 860bd603247SGregory Farnum 861a89d89d3SWenchao Xia if (!snapshot_name) { 862a89d89d3SWenchao Xia error_setg(errp, "rbd need a valid snapshot name"); 863a89d89d3SWenchao Xia return -EINVAL; 864a89d89d3SWenchao Xia } 865a89d89d3SWenchao Xia 866a89d89d3SWenchao Xia /* If snapshot_id is specified, it must be equal to name, see 867a89d89d3SWenchao Xia qemu_rbd_snap_list() */ 868a89d89d3SWenchao Xia if (snapshot_id && strcmp(snapshot_id, snapshot_name)) { 869a89d89d3SWenchao Xia error_setg(errp, 870a89d89d3SWenchao Xia "rbd do not support snapshot id, it should be NULL or " 871a89d89d3SWenchao Xia "equal to snapshot name"); 872a89d89d3SWenchao Xia return -EINVAL; 873a89d89d3SWenchao Xia } 874a89d89d3SWenchao Xia 875bd603247SGregory Farnum r = rbd_snap_remove(s->image, snapshot_name); 876a89d89d3SWenchao Xia if (r < 0) { 877a89d89d3SWenchao Xia error_setg_errno(errp, -r, "Failed to remove the snapshot"); 878a89d89d3SWenchao Xia } 879bd603247SGregory Farnum return r; 880bd603247SGregory Farnum } 881bd603247SGregory Farnum 882bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs, 883bd603247SGregory Farnum const char *snapshot_name) 884bd603247SGregory Farnum { 885bd603247SGregory Farnum BDRVRBDState *s = bs->opaque; 886bd603247SGregory Farnum 887*9be38598SEduardo Habkost return rbd_snap_rollback(s->image, snapshot_name); 888bd603247SGregory Farnum } 889bd603247SGregory Farnum 890ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs, 891ad32e9c0SJosh Durgin QEMUSnapshotInfo **psn_tab) 892f27aaf4bSChristian Brunner { 893f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 894f27aaf4bSChristian Brunner QEMUSnapshotInfo *sn_info, *sn_tab = NULL; 895ad32e9c0SJosh Durgin int i, snap_count; 896ad32e9c0SJosh Durgin rbd_snap_info_t *snaps; 897ad32e9c0SJosh Durgin int max_snaps = RBD_MAX_SNAPS; 898f27aaf4bSChristian Brunner 899ad32e9c0SJosh Durgin do { 90002c4f26bSMarkus Armbruster snaps = g_new(rbd_snap_info_t, max_snaps); 901ad32e9c0SJosh Durgin snap_count = rbd_snap_list(s->image, snaps, &max_snaps); 9029e6337d0SStefan Hajnoczi if (snap_count <= 0) { 9037267c094SAnthony Liguori g_free(snaps); 904f27aaf4bSChristian Brunner } 905ad32e9c0SJosh Durgin } while (snap_count == -ERANGE); 906f27aaf4bSChristian Brunner 907ad32e9c0SJosh Durgin if (snap_count <= 0) { 908b9c53290SJosh Durgin goto done; 909f27aaf4bSChristian Brunner } 910f27aaf4bSChristian Brunner 9115839e53bSMarkus Armbruster sn_tab = g_new0(QEMUSnapshotInfo, snap_count); 912f27aaf4bSChristian Brunner 913ad32e9c0SJosh Durgin for (i = 0; i < snap_count; i++) { 914ad32e9c0SJosh Durgin const char *snap_name = snaps[i].name; 915f27aaf4bSChristian Brunner 916f27aaf4bSChristian Brunner sn_info = sn_tab + i; 917f27aaf4bSChristian Brunner pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name); 918f27aaf4bSChristian Brunner pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name); 919f27aaf4bSChristian Brunner 920ad32e9c0SJosh Durgin sn_info->vm_state_size = snaps[i].size; 921f27aaf4bSChristian Brunner sn_info->date_sec = 0; 922f27aaf4bSChristian Brunner sn_info->date_nsec = 0; 923f27aaf4bSChristian Brunner sn_info->vm_clock_nsec = 0; 924f27aaf4bSChristian Brunner } 925ad32e9c0SJosh Durgin rbd_snap_list_end(snaps); 9269e6337d0SStefan Hajnoczi g_free(snaps); 927ad32e9c0SJosh Durgin 928b9c53290SJosh Durgin done: 929f27aaf4bSChristian Brunner *psn_tab = sn_tab; 930f27aaf4bSChristian Brunner return snap_count; 931f27aaf4bSChristian Brunner } 932f27aaf4bSChristian Brunner 933787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 9347c84b1b8SMarkus Armbruster static BlockAIOCB* qemu_rbd_aio_discard(BlockDriverState *bs, 935787f3133SJosh Durgin int64_t sector_num, 936787f3133SJosh Durgin int nb_sectors, 937097310b5SMarkus Armbruster BlockCompletionFunc *cb, 938787f3133SJosh Durgin void *opaque) 939787f3133SJosh Durgin { 940787f3133SJosh Durgin return rbd_start_aio(bs, sector_num, NULL, nb_sectors, cb, opaque, 941787f3133SJosh Durgin RBD_AIO_DISCARD); 942787f3133SJosh Durgin } 943787f3133SJosh Durgin #endif 944787f3133SJosh Durgin 945be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE 946be217884SAdam Crume static void qemu_rbd_invalidate_cache(BlockDriverState *bs, 947be217884SAdam Crume Error **errp) 948be217884SAdam Crume { 949be217884SAdam Crume BDRVRBDState *s = bs->opaque; 950be217884SAdam Crume int r = rbd_invalidate_cache(s->image); 951be217884SAdam Crume if (r < 0) { 952be217884SAdam Crume error_setg_errno(errp, -r, "Failed to invalidate the cache"); 953be217884SAdam Crume } 954be217884SAdam Crume } 955be217884SAdam Crume #endif 956be217884SAdam Crume 957bd0cf596SChunyan Liu static QemuOptsList qemu_rbd_create_opts = { 958bd0cf596SChunyan Liu .name = "rbd-create-opts", 959bd0cf596SChunyan Liu .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head), 960bd0cf596SChunyan Liu .desc = { 961f27aaf4bSChristian Brunner { 962f27aaf4bSChristian Brunner .name = BLOCK_OPT_SIZE, 963bd0cf596SChunyan Liu .type = QEMU_OPT_SIZE, 964f27aaf4bSChristian Brunner .help = "Virtual disk size" 965f27aaf4bSChristian Brunner }, 966f27aaf4bSChristian Brunner { 967f27aaf4bSChristian Brunner .name = BLOCK_OPT_CLUSTER_SIZE, 968bd0cf596SChunyan Liu .type = QEMU_OPT_SIZE, 969f27aaf4bSChristian Brunner .help = "RBD object size" 970f27aaf4bSChristian Brunner }, 97160390a21SDaniel P. Berrange { 97260390a21SDaniel P. Berrange .name = "password-secret", 97360390a21SDaniel P. Berrange .type = QEMU_OPT_STRING, 97460390a21SDaniel P. Berrange .help = "ID of secret providing the password", 97560390a21SDaniel P. Berrange }, 976bd0cf596SChunyan Liu { /* end of list */ } 977bd0cf596SChunyan Liu } 978f27aaf4bSChristian Brunner }; 979f27aaf4bSChristian Brunner 980f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = { 981f27aaf4bSChristian Brunner .format_name = "rbd", 982f27aaf4bSChristian Brunner .instance_size = sizeof(BDRVRBDState), 983030be321SBenoît Canet .bdrv_needs_filename = true, 984ad32e9c0SJosh Durgin .bdrv_file_open = qemu_rbd_open, 985ad32e9c0SJosh Durgin .bdrv_close = qemu_rbd_close, 986c282e1fdSChunyan Liu .bdrv_create = qemu_rbd_create, 9873ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1, 988ad32e9c0SJosh Durgin .bdrv_get_info = qemu_rbd_getinfo, 989bd0cf596SChunyan Liu .create_opts = &qemu_rbd_create_opts, 990ad32e9c0SJosh Durgin .bdrv_getlength = qemu_rbd_getlength, 99130cdc48cSJosh Durgin .bdrv_truncate = qemu_rbd_truncate, 992f27aaf4bSChristian Brunner .protocol_name = "rbd", 993f27aaf4bSChristian Brunner 994ad32e9c0SJosh Durgin .bdrv_aio_readv = qemu_rbd_aio_readv, 995ad32e9c0SJosh Durgin .bdrv_aio_writev = qemu_rbd_aio_writev, 996dc7588c1SJosh Durgin 997dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH 998dc7588c1SJosh Durgin .bdrv_aio_flush = qemu_rbd_aio_flush, 999dc7588c1SJosh Durgin #else 1000c68b89acSKevin Wolf .bdrv_co_flush_to_disk = qemu_rbd_co_flush, 1001dc7588c1SJosh Durgin #endif 1002f27aaf4bSChristian Brunner 1003787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 1004787f3133SJosh Durgin .bdrv_aio_discard = qemu_rbd_aio_discard, 1005787f3133SJosh Durgin #endif 1006787f3133SJosh Durgin 1007ad32e9c0SJosh Durgin .bdrv_snapshot_create = qemu_rbd_snap_create, 1008bd603247SGregory Farnum .bdrv_snapshot_delete = qemu_rbd_snap_remove, 1009ad32e9c0SJosh Durgin .bdrv_snapshot_list = qemu_rbd_snap_list, 1010bd603247SGregory Farnum .bdrv_snapshot_goto = qemu_rbd_snap_rollback, 1011be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE 1012be217884SAdam Crume .bdrv_invalidate_cache = qemu_rbd_invalidate_cache, 1013be217884SAdam Crume #endif 1014f27aaf4bSChristian Brunner }; 1015f27aaf4bSChristian Brunner 1016f27aaf4bSChristian Brunner static void bdrv_rbd_init(void) 1017f27aaf4bSChristian Brunner { 1018f27aaf4bSChristian Brunner bdrv_register(&bdrv_rbd); 1019f27aaf4bSChristian Brunner } 1020f27aaf4bSChristian Brunner 1021f27aaf4bSChristian Brunner block_init(bdrv_rbd_init); 1022