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 16f27aaf4bSChristian Brunner #include "qemu-common.h" 171de7afc9SPaolo Bonzini #include "qemu/error-report.h" 18737e150eSPaolo Bonzini #include "block/block_int.h" 19*60390a21SDaniel P. Berrange #include "crypto/secret.h" 20f27aaf4bSChristian Brunner 21ad32e9c0SJosh Durgin #include <rbd/librbd.h> 22f27aaf4bSChristian Brunner 23f27aaf4bSChristian Brunner /* 24f27aaf4bSChristian Brunner * When specifying the image filename use: 25f27aaf4bSChristian Brunner * 26fab5cf59SJosh Durgin * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]] 27f27aaf4bSChristian Brunner * 289e1fbcdeSSage Weil * poolname must be the name of an existing rados pool. 29f27aaf4bSChristian Brunner * 309e1fbcdeSSage Weil * devicename is the name of the rbd image. 31f27aaf4bSChristian Brunner * 329e1fbcdeSSage Weil * Each option given is used to configure rados, and may be any valid 339e1fbcdeSSage Weil * Ceph option, "id", or "conf". 34fab5cf59SJosh Durgin * 359e1fbcdeSSage Weil * The "id" option indicates what user we should authenticate as to 369e1fbcdeSSage Weil * the Ceph cluster. If it is excluded we will use the Ceph default 379e1fbcdeSSage Weil * (normally 'admin'). 38f27aaf4bSChristian Brunner * 399e1fbcdeSSage Weil * The "conf" option specifies a Ceph configuration file to read. If 409e1fbcdeSSage Weil * it is not specified, we will read from the default Ceph locations 419e1fbcdeSSage Weil * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration 429e1fbcdeSSage Weil * file, specify conf=/dev/null. 43f27aaf4bSChristian Brunner * 449e1fbcdeSSage Weil * Configuration values containing :, @, or = can be escaped with a 459e1fbcdeSSage Weil * leading "\". 46f27aaf4bSChristian Brunner */ 47f27aaf4bSChristian Brunner 48787f3133SJosh Durgin /* rbd_aio_discard added in 0.1.2 */ 49787f3133SJosh Durgin #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2) 50787f3133SJosh Durgin #define LIBRBD_SUPPORTS_DISCARD 51787f3133SJosh Durgin #else 52787f3133SJosh Durgin #undef LIBRBD_SUPPORTS_DISCARD 53787f3133SJosh Durgin #endif 54787f3133SJosh Durgin 55f27aaf4bSChristian Brunner #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER) 56f27aaf4bSChristian Brunner 57ad32e9c0SJosh Durgin #define RBD_MAX_CONF_NAME_SIZE 128 58ad32e9c0SJosh Durgin #define RBD_MAX_CONF_VAL_SIZE 512 59ad32e9c0SJosh Durgin #define RBD_MAX_CONF_SIZE 1024 60ad32e9c0SJosh Durgin #define RBD_MAX_POOL_NAME_SIZE 128 61ad32e9c0SJosh Durgin #define RBD_MAX_SNAP_NAME_SIZE 128 62ad32e9c0SJosh Durgin #define RBD_MAX_SNAPS 100 63ad32e9c0SJosh Durgin 64787f3133SJosh Durgin typedef enum { 65787f3133SJosh Durgin RBD_AIO_READ, 66787f3133SJosh Durgin RBD_AIO_WRITE, 67dc7588c1SJosh Durgin RBD_AIO_DISCARD, 68dc7588c1SJosh Durgin RBD_AIO_FLUSH 69787f3133SJosh Durgin } RBDAIOCmd; 70787f3133SJosh Durgin 71f27aaf4bSChristian Brunner typedef struct RBDAIOCB { 727c84b1b8SMarkus Armbruster BlockAIOCB common; 73f27aaf4bSChristian Brunner QEMUBH *bh; 7408448d51SStefan Priebe int64_t ret; 75f27aaf4bSChristian Brunner QEMUIOVector *qiov; 76f27aaf4bSChristian Brunner char *bounce; 77787f3133SJosh Durgin RBDAIOCmd cmd; 78f27aaf4bSChristian Brunner int error; 79f27aaf4bSChristian Brunner struct BDRVRBDState *s; 80f27aaf4bSChristian Brunner } RBDAIOCB; 81f27aaf4bSChristian Brunner 82f27aaf4bSChristian Brunner typedef struct RADOSCB { 83f27aaf4bSChristian Brunner RBDAIOCB *acb; 84f27aaf4bSChristian Brunner struct BDRVRBDState *s; 85ad32e9c0SJosh Durgin int64_t size; 86f27aaf4bSChristian Brunner char *buf; 8708448d51SStefan Priebe int64_t ret; 88f27aaf4bSChristian Brunner } RADOSCB; 89f27aaf4bSChristian Brunner 90f27aaf4bSChristian Brunner typedef struct BDRVRBDState { 91ad32e9c0SJosh Durgin rados_t cluster; 92ad32e9c0SJosh Durgin rados_ioctx_t io_ctx; 93ad32e9c0SJosh Durgin rbd_image_t image; 94ad32e9c0SJosh Durgin char name[RBD_MAX_IMAGE_NAME_SIZE]; 95ad32e9c0SJosh Durgin char *snap; 96f27aaf4bSChristian Brunner } BDRVRBDState; 97f27aaf4bSChristian Brunner 98ad32e9c0SJosh Durgin static int qemu_rbd_next_tok(char *dst, int dst_len, 99f27aaf4bSChristian Brunner char *src, char delim, 100f27aaf4bSChristian Brunner const char *name, 101d61563b2SMarkus Armbruster char **p, Error **errp) 102f27aaf4bSChristian Brunner { 103f27aaf4bSChristian Brunner int l; 104f27aaf4bSChristian Brunner char *end; 105f27aaf4bSChristian Brunner 106f27aaf4bSChristian Brunner *p = NULL; 107f27aaf4bSChristian Brunner 108f27aaf4bSChristian Brunner if (delim != '\0') { 10916a06b24SSage Weil for (end = src; *end; ++end) { 11016a06b24SSage Weil if (*end == delim) { 11116a06b24SSage Weil break; 11216a06b24SSage Weil } 11316a06b24SSage Weil if (*end == '\\' && end[1] != '\0') { 11416a06b24SSage Weil end++; 11516a06b24SSage Weil } 11616a06b24SSage Weil } 11716a06b24SSage Weil if (*end == delim) { 118f27aaf4bSChristian Brunner *p = end + 1; 119f27aaf4bSChristian Brunner *end = '\0'; 120f27aaf4bSChristian Brunner } 121f27aaf4bSChristian Brunner } 122f27aaf4bSChristian Brunner l = strlen(src); 123f27aaf4bSChristian Brunner if (l >= dst_len) { 124d61563b2SMarkus Armbruster error_setg(errp, "%s too long", name); 125f27aaf4bSChristian Brunner return -EINVAL; 126f27aaf4bSChristian Brunner } else if (l == 0) { 127d61563b2SMarkus Armbruster error_setg(errp, "%s too short", name); 128f27aaf4bSChristian Brunner return -EINVAL; 129f27aaf4bSChristian Brunner } 130f27aaf4bSChristian Brunner 131f27aaf4bSChristian Brunner pstrcpy(dst, dst_len, src); 132f27aaf4bSChristian Brunner 133f27aaf4bSChristian Brunner return 0; 134f27aaf4bSChristian Brunner } 135f27aaf4bSChristian Brunner 13616a06b24SSage Weil static void qemu_rbd_unescape(char *src) 13716a06b24SSage Weil { 13816a06b24SSage Weil char *p; 13916a06b24SSage Weil 14016a06b24SSage Weil for (p = src; *src; ++src, ++p) { 14116a06b24SSage Weil if (*src == '\\' && src[1] != '\0') { 14216a06b24SSage Weil src++; 14316a06b24SSage Weil } 14416a06b24SSage Weil *p = *src; 14516a06b24SSage Weil } 14616a06b24SSage Weil *p = '\0'; 14716a06b24SSage Weil } 14816a06b24SSage Weil 149ad32e9c0SJosh Durgin static int qemu_rbd_parsename(const char *filename, 150f27aaf4bSChristian Brunner char *pool, int pool_len, 151f27aaf4bSChristian Brunner char *snap, int snap_len, 152fab5cf59SJosh Durgin char *name, int name_len, 153d61563b2SMarkus Armbruster char *conf, int conf_len, 154d61563b2SMarkus Armbruster Error **errp) 155f27aaf4bSChristian Brunner { 156f27aaf4bSChristian Brunner const char *start; 157f27aaf4bSChristian Brunner char *p, *buf; 158f27aaf4bSChristian Brunner int ret; 159f27aaf4bSChristian Brunner 160f27aaf4bSChristian Brunner if (!strstart(filename, "rbd:", &start)) { 161d61563b2SMarkus Armbruster error_setg(errp, "File name must start with 'rbd:'"); 162f27aaf4bSChristian Brunner return -EINVAL; 163f27aaf4bSChristian Brunner } 164f27aaf4bSChristian Brunner 1657267c094SAnthony Liguori buf = g_strdup(start); 166f27aaf4bSChristian Brunner p = buf; 167fab5cf59SJosh Durgin *snap = '\0'; 168fab5cf59SJosh Durgin *conf = '\0'; 169f27aaf4bSChristian Brunner 170d61563b2SMarkus Armbruster ret = qemu_rbd_next_tok(pool, pool_len, p, 171d61563b2SMarkus Armbruster '/', "pool name", &p, errp); 172f27aaf4bSChristian Brunner if (ret < 0 || !p) { 173f27aaf4bSChristian Brunner ret = -EINVAL; 174f27aaf4bSChristian Brunner goto done; 175f27aaf4bSChristian Brunner } 17616a06b24SSage Weil qemu_rbd_unescape(pool); 177fab5cf59SJosh Durgin 178fab5cf59SJosh Durgin if (strchr(p, '@')) { 179d61563b2SMarkus Armbruster ret = qemu_rbd_next_tok(name, name_len, p, 180d61563b2SMarkus Armbruster '@', "object name", &p, errp); 181f27aaf4bSChristian Brunner if (ret < 0) { 182f27aaf4bSChristian Brunner goto done; 183f27aaf4bSChristian Brunner } 184d61563b2SMarkus Armbruster ret = qemu_rbd_next_tok(snap, snap_len, p, 185d61563b2SMarkus Armbruster ':', "snap name", &p, errp); 18616a06b24SSage Weil qemu_rbd_unescape(snap); 187fab5cf59SJosh Durgin } else { 188d61563b2SMarkus Armbruster ret = qemu_rbd_next_tok(name, name_len, p, 189d61563b2SMarkus Armbruster ':', "object name", &p, errp); 190fab5cf59SJosh Durgin } 19116a06b24SSage Weil qemu_rbd_unescape(name); 192fab5cf59SJosh Durgin if (ret < 0 || !p) { 193f27aaf4bSChristian Brunner goto done; 194f27aaf4bSChristian Brunner } 195f27aaf4bSChristian Brunner 196d61563b2SMarkus Armbruster ret = qemu_rbd_next_tok(conf, conf_len, p, 197d61563b2SMarkus Armbruster '\0', "configuration", &p, errp); 198f27aaf4bSChristian Brunner 199f27aaf4bSChristian Brunner done: 2007267c094SAnthony Liguori g_free(buf); 201f27aaf4bSChristian Brunner return ret; 202f27aaf4bSChristian Brunner } 203f27aaf4bSChristian Brunner 2047c7e9df0SSage Weil static char *qemu_rbd_parse_clientname(const char *conf, char *clientname) 2057c7e9df0SSage Weil { 2067c7e9df0SSage Weil const char *p = conf; 2077c7e9df0SSage Weil 2087c7e9df0SSage Weil while (*p) { 2097c7e9df0SSage Weil int len; 2107c7e9df0SSage Weil const char *end = strchr(p, ':'); 2117c7e9df0SSage Weil 2127c7e9df0SSage Weil if (end) { 2137c7e9df0SSage Weil len = end - p; 2147c7e9df0SSage Weil } else { 2157c7e9df0SSage Weil len = strlen(p); 2167c7e9df0SSage Weil } 2177c7e9df0SSage Weil 2187c7e9df0SSage Weil if (strncmp(p, "id=", 3) == 0) { 2197c7e9df0SSage Weil len -= 3; 2207c7e9df0SSage Weil strncpy(clientname, p + 3, len); 2217c7e9df0SSage Weil clientname[len] = '\0'; 2227c7e9df0SSage Weil return clientname; 2237c7e9df0SSage Weil } 2247c7e9df0SSage Weil if (end == NULL) { 2257c7e9df0SSage Weil break; 2267c7e9df0SSage Weil } 2277c7e9df0SSage Weil p = end + 1; 2287c7e9df0SSage Weil } 2297c7e9df0SSage Weil return NULL; 2307c7e9df0SSage Weil } 2317c7e9df0SSage Weil 232*60390a21SDaniel P. Berrange 233*60390a21SDaniel P. Berrange static int qemu_rbd_set_auth(rados_t cluster, const char *secretid, 234*60390a21SDaniel P. Berrange Error **errp) 235*60390a21SDaniel P. Berrange { 236*60390a21SDaniel P. Berrange if (secretid == 0) { 237*60390a21SDaniel P. Berrange return 0; 238*60390a21SDaniel P. Berrange } 239*60390a21SDaniel P. Berrange 240*60390a21SDaniel P. Berrange gchar *secret = qcrypto_secret_lookup_as_base64(secretid, 241*60390a21SDaniel P. Berrange errp); 242*60390a21SDaniel P. Berrange if (!secret) { 243*60390a21SDaniel P. Berrange return -1; 244*60390a21SDaniel P. Berrange } 245*60390a21SDaniel P. Berrange 246*60390a21SDaniel P. Berrange rados_conf_set(cluster, "key", secret); 247*60390a21SDaniel P. Berrange g_free(secret); 248*60390a21SDaniel P. Berrange 249*60390a21SDaniel P. Berrange return 0; 250*60390a21SDaniel P. Berrange } 251*60390a21SDaniel P. Berrange 252*60390a21SDaniel P. Berrange 253e34d8f29SJosh Durgin static int qemu_rbd_set_conf(rados_t cluster, const char *conf, 254e34d8f29SJosh Durgin bool only_read_conf_file, 255e34d8f29SJosh Durgin Error **errp) 256fab5cf59SJosh Durgin { 257fab5cf59SJosh Durgin char *p, *buf; 258fab5cf59SJosh Durgin char name[RBD_MAX_CONF_NAME_SIZE]; 259fab5cf59SJosh Durgin char value[RBD_MAX_CONF_VAL_SIZE]; 260fab5cf59SJosh Durgin int ret = 0; 261fab5cf59SJosh Durgin 2627267c094SAnthony Liguori buf = g_strdup(conf); 263fab5cf59SJosh Durgin p = buf; 264fab5cf59SJosh Durgin 265fab5cf59SJosh Durgin while (p) { 266fab5cf59SJosh Durgin ret = qemu_rbd_next_tok(name, sizeof(name), p, 267d61563b2SMarkus Armbruster '=', "conf option name", &p, errp); 268fab5cf59SJosh Durgin if (ret < 0) { 269fab5cf59SJosh Durgin break; 270fab5cf59SJosh Durgin } 27116a06b24SSage Weil qemu_rbd_unescape(name); 272fab5cf59SJosh Durgin 273fab5cf59SJosh Durgin if (!p) { 274d61563b2SMarkus Armbruster error_setg(errp, "conf option %s has no value", name); 275fab5cf59SJosh Durgin ret = -EINVAL; 276fab5cf59SJosh Durgin break; 277fab5cf59SJosh Durgin } 278fab5cf59SJosh Durgin 279fab5cf59SJosh Durgin ret = qemu_rbd_next_tok(value, sizeof(value), p, 280d61563b2SMarkus Armbruster ':', "conf option value", &p, errp); 281fab5cf59SJosh Durgin if (ret < 0) { 282fab5cf59SJosh Durgin break; 283fab5cf59SJosh Durgin } 28416a06b24SSage Weil qemu_rbd_unescape(value); 285fab5cf59SJosh Durgin 2867c7e9df0SSage Weil if (strcmp(name, "conf") == 0) { 287e34d8f29SJosh Durgin /* read the conf file alone, so it doesn't override more 288e34d8f29SJosh Durgin specific settings for a particular device */ 289e34d8f29SJosh Durgin if (only_read_conf_file) { 2907c7e9df0SSage Weil ret = rados_conf_read_file(cluster, value); 2917c7e9df0SSage Weil if (ret < 0) { 292d61563b2SMarkus Armbruster error_setg(errp, "error reading conf file %s", value); 2937c7e9df0SSage Weil break; 2947c7e9df0SSage Weil } 295e34d8f29SJosh Durgin } 2967c7e9df0SSage Weil } else if (strcmp(name, "id") == 0) { 2977c7e9df0SSage Weil /* ignore, this is parsed by qemu_rbd_parse_clientname() */ 298e34d8f29SJosh Durgin } else if (!only_read_conf_file) { 299fab5cf59SJosh Durgin ret = rados_conf_set(cluster, name, value); 300fab5cf59SJosh Durgin if (ret < 0) { 301d61563b2SMarkus Armbruster error_setg(errp, "invalid conf option %s", name); 302fab5cf59SJosh Durgin ret = -EINVAL; 303fab5cf59SJosh Durgin break; 304fab5cf59SJosh Durgin } 305fab5cf59SJosh Durgin } 306fab5cf59SJosh Durgin } 307fab5cf59SJosh Durgin 3087267c094SAnthony Liguori g_free(buf); 309fab5cf59SJosh Durgin return ret; 310fab5cf59SJosh Durgin } 311fab5cf59SJosh Durgin 312bd0cf596SChunyan Liu static int qemu_rbd_create(const char *filename, QemuOpts *opts, Error **errp) 313f27aaf4bSChristian Brunner { 314d61563b2SMarkus Armbruster Error *local_err = NULL; 315f27aaf4bSChristian Brunner int64_t bytes = 0; 316f27aaf4bSChristian Brunner int64_t objsize; 317ad32e9c0SJosh Durgin int obj_order = 0; 318ad32e9c0SJosh Durgin char pool[RBD_MAX_POOL_NAME_SIZE]; 319ad32e9c0SJosh Durgin char name[RBD_MAX_IMAGE_NAME_SIZE]; 320ad32e9c0SJosh Durgin char snap_buf[RBD_MAX_SNAP_NAME_SIZE]; 321fab5cf59SJosh Durgin char conf[RBD_MAX_CONF_SIZE]; 3227c7e9df0SSage Weil char clientname_buf[RBD_MAX_CONF_SIZE]; 3237c7e9df0SSage Weil char *clientname; 324*60390a21SDaniel P. Berrange const char *secretid; 325ad32e9c0SJosh Durgin rados_t cluster; 326ad32e9c0SJosh Durgin rados_ioctx_t io_ctx; 327f27aaf4bSChristian Brunner int ret; 328f27aaf4bSChristian Brunner 329*60390a21SDaniel P. Berrange secretid = qemu_opt_get(opts, "password-secret"); 330*60390a21SDaniel P. Berrange 331ad32e9c0SJosh Durgin if (qemu_rbd_parsename(filename, pool, sizeof(pool), 332f27aaf4bSChristian Brunner snap_buf, sizeof(snap_buf), 333fab5cf59SJosh Durgin name, sizeof(name), 334d61563b2SMarkus Armbruster conf, sizeof(conf), &local_err) < 0) { 335d61563b2SMarkus Armbruster error_propagate(errp, local_err); 336f27aaf4bSChristian Brunner return -EINVAL; 337f27aaf4bSChristian Brunner } 338f27aaf4bSChristian Brunner 339f27aaf4bSChristian Brunner /* Read out options */ 340c2eb918eSHu Tao bytes = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 341c2eb918eSHu Tao BDRV_SECTOR_SIZE); 342bd0cf596SChunyan Liu objsize = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE, 0); 343bd0cf596SChunyan Liu if (objsize) { 344f27aaf4bSChristian Brunner if ((objsize - 1) & objsize) { /* not a power of 2? */ 345d61563b2SMarkus Armbruster error_setg(errp, "obj size needs to be power of 2"); 346f27aaf4bSChristian Brunner return -EINVAL; 347f27aaf4bSChristian Brunner } 348f27aaf4bSChristian Brunner if (objsize < 4096) { 349d61563b2SMarkus Armbruster error_setg(errp, "obj size too small"); 350f27aaf4bSChristian Brunner return -EINVAL; 351f27aaf4bSChristian Brunner } 352786a4ea8SStefan Hajnoczi obj_order = ctz32(objsize); 353f27aaf4bSChristian Brunner } 354f27aaf4bSChristian Brunner 3557c7e9df0SSage Weil clientname = qemu_rbd_parse_clientname(conf, clientname_buf); 3567c7e9df0SSage Weil if (rados_create(&cluster, clientname) < 0) { 357d61563b2SMarkus Armbruster error_setg(errp, "error initializing"); 358f27aaf4bSChristian Brunner return -EIO; 359f27aaf4bSChristian Brunner } 360f27aaf4bSChristian Brunner 361fab5cf59SJosh Durgin if (strstr(conf, "conf=") == NULL) { 362f9fe18ecSSage Weil /* try default location, but ignore failure */ 363f9fe18ecSSage Weil rados_conf_read_file(cluster, NULL); 364e34d8f29SJosh Durgin } else if (conf[0] != '\0' && 365e34d8f29SJosh Durgin qemu_rbd_set_conf(cluster, conf, true, &local_err) < 0) { 366e34d8f29SJosh Durgin rados_shutdown(cluster); 367e34d8f29SJosh Durgin error_propagate(errp, local_err); 368e34d8f29SJosh Durgin return -EIO; 369fab5cf59SJosh Durgin } 370fab5cf59SJosh Durgin 371fab5cf59SJosh Durgin if (conf[0] != '\0' && 372e34d8f29SJosh Durgin qemu_rbd_set_conf(cluster, conf, false, &local_err) < 0) { 373fab5cf59SJosh Durgin rados_shutdown(cluster); 374d61563b2SMarkus Armbruster error_propagate(errp, local_err); 375fab5cf59SJosh Durgin return -EIO; 376fab5cf59SJosh Durgin } 377ad32e9c0SJosh Durgin 378*60390a21SDaniel P. Berrange if (qemu_rbd_set_auth(cluster, secretid, errp) < 0) { 379*60390a21SDaniel P. Berrange rados_shutdown(cluster); 380*60390a21SDaniel P. Berrange return -EIO; 381*60390a21SDaniel P. Berrange } 382*60390a21SDaniel P. Berrange 383ad32e9c0SJosh Durgin if (rados_connect(cluster) < 0) { 384d61563b2SMarkus Armbruster error_setg(errp, "error connecting"); 385ad32e9c0SJosh Durgin rados_shutdown(cluster); 386ad32e9c0SJosh Durgin return -EIO; 387ad32e9c0SJosh Durgin } 388ad32e9c0SJosh Durgin 389ad32e9c0SJosh Durgin if (rados_ioctx_create(cluster, pool, &io_ctx) < 0) { 390d61563b2SMarkus Armbruster error_setg(errp, "error opening pool %s", pool); 391ad32e9c0SJosh Durgin rados_shutdown(cluster); 392f27aaf4bSChristian Brunner return -EIO; 393f27aaf4bSChristian Brunner } 394f27aaf4bSChristian Brunner 395ad32e9c0SJosh Durgin ret = rbd_create(io_ctx, name, bytes, &obj_order); 396ad32e9c0SJosh Durgin rados_ioctx_destroy(io_ctx); 397ad32e9c0SJosh Durgin rados_shutdown(cluster); 398f27aaf4bSChristian Brunner 399f27aaf4bSChristian Brunner return ret; 400f27aaf4bSChristian Brunner } 401f27aaf4bSChristian Brunner 402f27aaf4bSChristian Brunner /* 403e04fb07fSStefan Hajnoczi * This aio completion is being called from rbd_finish_bh() and runs in qemu 404e04fb07fSStefan Hajnoczi * BH context. 405f27aaf4bSChristian Brunner */ 406ad32e9c0SJosh Durgin static void qemu_rbd_complete_aio(RADOSCB *rcb) 407f27aaf4bSChristian Brunner { 408f27aaf4bSChristian Brunner RBDAIOCB *acb = rcb->acb; 409f27aaf4bSChristian Brunner int64_t r; 410f27aaf4bSChristian Brunner 411f27aaf4bSChristian Brunner r = rcb->ret; 412f27aaf4bSChristian Brunner 413dc7588c1SJosh Durgin if (acb->cmd != RBD_AIO_READ) { 414f27aaf4bSChristian Brunner if (r < 0) { 415f27aaf4bSChristian Brunner acb->ret = r; 416f27aaf4bSChristian Brunner acb->error = 1; 417f27aaf4bSChristian Brunner } else if (!acb->error) { 418ad32e9c0SJosh Durgin acb->ret = rcb->size; 419f27aaf4bSChristian Brunner } 420f27aaf4bSChristian Brunner } else { 421ad32e9c0SJosh Durgin if (r < 0) { 422ad32e9c0SJosh Durgin memset(rcb->buf, 0, rcb->size); 423f27aaf4bSChristian Brunner acb->ret = r; 424f27aaf4bSChristian Brunner acb->error = 1; 425ad32e9c0SJosh Durgin } else if (r < rcb->size) { 426ad32e9c0SJosh Durgin memset(rcb->buf + r, 0, rcb->size - r); 427f27aaf4bSChristian Brunner if (!acb->error) { 428ad32e9c0SJosh Durgin acb->ret = rcb->size; 429f27aaf4bSChristian Brunner } 430f27aaf4bSChristian Brunner } else if (!acb->error) { 431ad32e9c0SJosh Durgin acb->ret = r; 432f27aaf4bSChristian Brunner } 433f27aaf4bSChristian Brunner } 434e04fb07fSStefan Hajnoczi 4357267c094SAnthony Liguori g_free(rcb); 436e04fb07fSStefan Hajnoczi 437e04fb07fSStefan Hajnoczi if (acb->cmd == RBD_AIO_READ) { 438e04fb07fSStefan Hajnoczi qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); 439f27aaf4bSChristian Brunner } 440e04fb07fSStefan Hajnoczi qemu_vfree(acb->bounce); 441e04fb07fSStefan Hajnoczi acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret)); 442f27aaf4bSChristian Brunner 4438007429aSFam Zheng qemu_aio_unref(acb); 444f27aaf4bSChristian Brunner } 445f27aaf4bSChristian Brunner 446a9ccedc3SKevin Wolf /* TODO Convert to fine grained options */ 447a9ccedc3SKevin Wolf static QemuOptsList runtime_opts = { 448a9ccedc3SKevin Wolf .name = "rbd", 449a9ccedc3SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), 450a9ccedc3SKevin Wolf .desc = { 451a9ccedc3SKevin Wolf { 452a9ccedc3SKevin Wolf .name = "filename", 453a9ccedc3SKevin Wolf .type = QEMU_OPT_STRING, 454a9ccedc3SKevin Wolf .help = "Specification of the rbd image", 455a9ccedc3SKevin Wolf }, 456*60390a21SDaniel P. Berrange { 457*60390a21SDaniel P. Berrange .name = "password-secret", 458*60390a21SDaniel P. Berrange .type = QEMU_OPT_STRING, 459*60390a21SDaniel P. Berrange .help = "ID of secret providing the password", 460*60390a21SDaniel P. Berrange }, 461a9ccedc3SKevin Wolf { /* end of list */ } 462a9ccedc3SKevin Wolf }, 463a9ccedc3SKevin Wolf }; 464a9ccedc3SKevin Wolf 465015a1036SMax Reitz static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags, 466015a1036SMax Reitz Error **errp) 467f27aaf4bSChristian Brunner { 468f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 469ad32e9c0SJosh Durgin char pool[RBD_MAX_POOL_NAME_SIZE]; 470ad32e9c0SJosh Durgin char snap_buf[RBD_MAX_SNAP_NAME_SIZE]; 471fab5cf59SJosh Durgin char conf[RBD_MAX_CONF_SIZE]; 4727c7e9df0SSage Weil char clientname_buf[RBD_MAX_CONF_SIZE]; 4737c7e9df0SSage Weil char *clientname; 474*60390a21SDaniel P. Berrange const char *secretid; 475a9ccedc3SKevin Wolf QemuOpts *opts; 476a9ccedc3SKevin Wolf Error *local_err = NULL; 477a9ccedc3SKevin Wolf const char *filename; 478f27aaf4bSChristian Brunner int r; 479f27aaf4bSChristian Brunner 48087ea75d5SPeter Crosthwaite opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); 481a9ccedc3SKevin Wolf qemu_opts_absorb_qdict(opts, options, &local_err); 48284d18f06SMarkus Armbruster if (local_err) { 483d61563b2SMarkus Armbruster error_propagate(errp, local_err); 484a9ccedc3SKevin Wolf qemu_opts_del(opts); 485a9ccedc3SKevin Wolf return -EINVAL; 486a9ccedc3SKevin Wolf } 487a9ccedc3SKevin Wolf 488a9ccedc3SKevin Wolf filename = qemu_opt_get(opts, "filename"); 489*60390a21SDaniel P. Berrange secretid = qemu_opt_get(opts, "password-secret"); 490a9ccedc3SKevin Wolf 491ad32e9c0SJosh Durgin if (qemu_rbd_parsename(filename, pool, sizeof(pool), 492f27aaf4bSChristian Brunner snap_buf, sizeof(snap_buf), 493fab5cf59SJosh Durgin s->name, sizeof(s->name), 494d61563b2SMarkus Armbruster conf, sizeof(conf), errp) < 0) { 495c3ca988dSKevin Wolf r = -EINVAL; 496c3ca988dSKevin Wolf goto failed_opts; 497f27aaf4bSChristian Brunner } 498f27aaf4bSChristian Brunner 4997c7e9df0SSage Weil clientname = qemu_rbd_parse_clientname(conf, clientname_buf); 5007c7e9df0SSage Weil r = rados_create(&s->cluster, clientname); 501ad32e9c0SJosh Durgin if (r < 0) { 5029281dbe6SGonglei error_setg(errp, "error initializing"); 503c3ca988dSKevin Wolf goto failed_opts; 504f27aaf4bSChristian Brunner } 505f27aaf4bSChristian Brunner 506eb93d5d9SSage Weil s->snap = NULL; 507eb93d5d9SSage Weil if (snap_buf[0] != '\0') { 508eb93d5d9SSage Weil s->snap = g_strdup(snap_buf); 509eb93d5d9SSage Weil } 510eb93d5d9SSage Weil 51199a3c89dSJosh Durgin if (strstr(conf, "conf=") == NULL) { 51299a3c89dSJosh Durgin /* try default location, but ignore failure */ 51399a3c89dSJosh Durgin rados_conf_read_file(s->cluster, NULL); 514e34d8f29SJosh Durgin } else if (conf[0] != '\0') { 515e34d8f29SJosh Durgin r = qemu_rbd_set_conf(s->cluster, conf, true, errp); 516e34d8f29SJosh Durgin if (r < 0) { 517e34d8f29SJosh Durgin goto failed_shutdown; 518e34d8f29SJosh Durgin } 51999a3c89dSJosh Durgin } 52099a3c89dSJosh Durgin 52199a3c89dSJosh Durgin if (conf[0] != '\0') { 522e34d8f29SJosh Durgin r = qemu_rbd_set_conf(s->cluster, conf, false, errp); 52399a3c89dSJosh Durgin if (r < 0) { 52499a3c89dSJosh Durgin goto failed_shutdown; 52599a3c89dSJosh Durgin } 52699a3c89dSJosh Durgin } 52799a3c89dSJosh Durgin 528*60390a21SDaniel P. Berrange if (qemu_rbd_set_auth(s->cluster, secretid, errp) < 0) { 529*60390a21SDaniel P. Berrange r = -EIO; 530*60390a21SDaniel P. Berrange goto failed_shutdown; 531*60390a21SDaniel P. Berrange } 532*60390a21SDaniel P. Berrange 533b11f38fcSJosh Durgin /* 534b11f38fcSJosh Durgin * Fallback to more conservative semantics if setting cache 535b11f38fcSJosh Durgin * options fails. Ignore errors from setting rbd_cache because the 536b11f38fcSJosh Durgin * only possible error is that the option does not exist, and 537b11f38fcSJosh Durgin * librbd defaults to no caching. If write through caching cannot 538b11f38fcSJosh Durgin * be set up, fall back to no caching. 539b11f38fcSJosh Durgin */ 540b11f38fcSJosh Durgin if (flags & BDRV_O_NOCACHE) { 541b11f38fcSJosh Durgin rados_conf_set(s->cluster, "rbd_cache", "false"); 542b11f38fcSJosh Durgin } else { 543b11f38fcSJosh Durgin rados_conf_set(s->cluster, "rbd_cache", "true"); 544b11f38fcSJosh Durgin } 545b11f38fcSJosh Durgin 546ad32e9c0SJosh Durgin r = rados_connect(s->cluster); 547ad32e9c0SJosh Durgin if (r < 0) { 5489281dbe6SGonglei error_setg(errp, "error connecting"); 549eb93d5d9SSage Weil goto failed_shutdown; 550ad32e9c0SJosh Durgin } 551ad32e9c0SJosh Durgin 552ad32e9c0SJosh Durgin r = rados_ioctx_create(s->cluster, pool, &s->io_ctx); 553ad32e9c0SJosh Durgin if (r < 0) { 5549281dbe6SGonglei error_setg(errp, "error opening pool %s", pool); 555eb93d5d9SSage Weil goto failed_shutdown; 556ad32e9c0SJosh Durgin } 557ad32e9c0SJosh Durgin 558ad32e9c0SJosh Durgin r = rbd_open(s->io_ctx, s->name, &s->image, s->snap); 559ad32e9c0SJosh Durgin if (r < 0) { 5609281dbe6SGonglei error_setg(errp, "error reading header from %s", s->name); 561eb93d5d9SSage Weil goto failed_open; 562ad32e9c0SJosh Durgin } 563ad32e9c0SJosh Durgin 564ad32e9c0SJosh Durgin bs->read_only = (s->snap != NULL); 565f27aaf4bSChristian Brunner 566c3ca988dSKevin Wolf qemu_opts_del(opts); 567f27aaf4bSChristian Brunner return 0; 568f27aaf4bSChristian Brunner 569eb93d5d9SSage Weil failed_open: 570ad32e9c0SJosh Durgin rados_ioctx_destroy(s->io_ctx); 571eb93d5d9SSage Weil failed_shutdown: 572ad32e9c0SJosh Durgin rados_shutdown(s->cluster); 573eb93d5d9SSage Weil g_free(s->snap); 574c3ca988dSKevin Wolf failed_opts: 575c3ca988dSKevin Wolf qemu_opts_del(opts); 576f27aaf4bSChristian Brunner return r; 577f27aaf4bSChristian Brunner } 578f27aaf4bSChristian Brunner 579ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs) 580f27aaf4bSChristian Brunner { 581f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 582f27aaf4bSChristian Brunner 583ad32e9c0SJosh Durgin rbd_close(s->image); 584ad32e9c0SJosh Durgin rados_ioctx_destroy(s->io_ctx); 5857267c094SAnthony Liguori g_free(s->snap); 586ad32e9c0SJosh Durgin rados_shutdown(s->cluster); 587f27aaf4bSChristian Brunner } 588f27aaf4bSChristian Brunner 589d7331bedSStefan Hajnoczi static const AIOCBInfo rbd_aiocb_info = { 590f27aaf4bSChristian Brunner .aiocb_size = sizeof(RBDAIOCB), 591f27aaf4bSChristian Brunner }; 592f27aaf4bSChristian Brunner 593e04fb07fSStefan Hajnoczi static void rbd_finish_bh(void *opaque) 594f27aaf4bSChristian Brunner { 595e04fb07fSStefan Hajnoczi RADOSCB *rcb = opaque; 596e04fb07fSStefan Hajnoczi qemu_bh_delete(rcb->acb->bh); 597e04fb07fSStefan Hajnoczi qemu_rbd_complete_aio(rcb); 598ad32e9c0SJosh Durgin } 599ad32e9c0SJosh Durgin 600ad32e9c0SJosh Durgin /* 601ad32e9c0SJosh Durgin * This is the callback function for rbd_aio_read and _write 602ad32e9c0SJosh Durgin * 603ad32e9c0SJosh Durgin * Note: this function is being called from a non qemu thread so 604ad32e9c0SJosh Durgin * we need to be careful about what we do here. Generally we only 605e04fb07fSStefan Hajnoczi * schedule a BH, and do the rest of the io completion handling 606e04fb07fSStefan Hajnoczi * from rbd_finish_bh() which runs in a qemu context. 607ad32e9c0SJosh Durgin */ 608ad32e9c0SJosh Durgin static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb) 609ad32e9c0SJosh Durgin { 610e04fb07fSStefan Hajnoczi RBDAIOCB *acb = rcb->acb; 611e04fb07fSStefan Hajnoczi 612ad32e9c0SJosh Durgin rcb->ret = rbd_aio_get_return_value(c); 613ad32e9c0SJosh Durgin rbd_aio_release(c); 614f27aaf4bSChristian Brunner 615ea800191SStefan Hajnoczi acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs), 616ea800191SStefan Hajnoczi rbd_finish_bh, rcb); 617e04fb07fSStefan Hajnoczi qemu_bh_schedule(acb->bh); 618473c7f02SStefan Priebe } 619f27aaf4bSChristian Brunner 620787f3133SJosh Durgin static int rbd_aio_discard_wrapper(rbd_image_t image, 621787f3133SJosh Durgin uint64_t off, 622787f3133SJosh Durgin uint64_t len, 623787f3133SJosh Durgin rbd_completion_t comp) 624787f3133SJosh Durgin { 625787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 626787f3133SJosh Durgin return rbd_aio_discard(image, off, len, comp); 627787f3133SJosh Durgin #else 628787f3133SJosh Durgin return -ENOTSUP; 629787f3133SJosh Durgin #endif 630787f3133SJosh Durgin } 631787f3133SJosh Durgin 632dc7588c1SJosh Durgin static int rbd_aio_flush_wrapper(rbd_image_t image, 633dc7588c1SJosh Durgin rbd_completion_t comp) 634dc7588c1SJosh Durgin { 635dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH 636dc7588c1SJosh Durgin return rbd_aio_flush(image, comp); 637dc7588c1SJosh Durgin #else 638dc7588c1SJosh Durgin return -ENOTSUP; 639dc7588c1SJosh Durgin #endif 640dc7588c1SJosh Durgin } 641dc7588c1SJosh Durgin 6427c84b1b8SMarkus Armbruster static BlockAIOCB *rbd_start_aio(BlockDriverState *bs, 643f27aaf4bSChristian Brunner int64_t sector_num, 644f27aaf4bSChristian Brunner QEMUIOVector *qiov, 645f27aaf4bSChristian Brunner int nb_sectors, 646097310b5SMarkus Armbruster BlockCompletionFunc *cb, 647787f3133SJosh Durgin void *opaque, 648787f3133SJosh Durgin RBDAIOCmd cmd) 649f27aaf4bSChristian Brunner { 650f27aaf4bSChristian Brunner RBDAIOCB *acb; 6510f7a0237SKevin Wolf RADOSCB *rcb = NULL; 652ad32e9c0SJosh Durgin rbd_completion_t c; 653f27aaf4bSChristian Brunner int64_t off, size; 654f27aaf4bSChristian Brunner char *buf; 65551a13528SJosh Durgin int r; 656f27aaf4bSChristian Brunner 657f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 658f27aaf4bSChristian Brunner 659d7331bedSStefan Hajnoczi acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque); 660787f3133SJosh Durgin acb->cmd = cmd; 661f27aaf4bSChristian Brunner acb->qiov = qiov; 662dc7588c1SJosh Durgin if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) { 663787f3133SJosh Durgin acb->bounce = NULL; 664787f3133SJosh Durgin } else { 6650f7a0237SKevin Wolf acb->bounce = qemu_try_blockalign(bs, qiov->size); 6660f7a0237SKevin Wolf if (acb->bounce == NULL) { 6670f7a0237SKevin Wolf goto failed; 6680f7a0237SKevin Wolf } 669787f3133SJosh Durgin } 670f27aaf4bSChristian Brunner acb->ret = 0; 671f27aaf4bSChristian Brunner acb->error = 0; 672f27aaf4bSChristian Brunner acb->s = s; 673f27aaf4bSChristian Brunner acb->bh = NULL; 674f27aaf4bSChristian Brunner 675787f3133SJosh Durgin if (cmd == RBD_AIO_WRITE) { 676d5e6b161SMichael Tokarev qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); 677f27aaf4bSChristian Brunner } 678f27aaf4bSChristian Brunner 679f27aaf4bSChristian Brunner buf = acb->bounce; 680f27aaf4bSChristian Brunner 681f27aaf4bSChristian Brunner off = sector_num * BDRV_SECTOR_SIZE; 682f27aaf4bSChristian Brunner size = nb_sectors * BDRV_SECTOR_SIZE; 683f27aaf4bSChristian Brunner 6845839e53bSMarkus Armbruster rcb = g_new(RADOSCB, 1); 685f27aaf4bSChristian Brunner rcb->acb = acb; 686f27aaf4bSChristian Brunner rcb->buf = buf; 687f27aaf4bSChristian Brunner rcb->s = acb->s; 688ad32e9c0SJosh Durgin rcb->size = size; 68951a13528SJosh Durgin r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c); 69051a13528SJosh Durgin if (r < 0) { 69151a13528SJosh Durgin goto failed; 69251a13528SJosh Durgin } 693f27aaf4bSChristian Brunner 694787f3133SJosh Durgin switch (cmd) { 695787f3133SJosh Durgin case RBD_AIO_WRITE: 69651a13528SJosh Durgin r = rbd_aio_write(s->image, off, size, buf, c); 697787f3133SJosh Durgin break; 698787f3133SJosh Durgin case RBD_AIO_READ: 69951a13528SJosh Durgin r = rbd_aio_read(s->image, off, size, buf, c); 700787f3133SJosh Durgin break; 701787f3133SJosh Durgin case RBD_AIO_DISCARD: 702787f3133SJosh Durgin r = rbd_aio_discard_wrapper(s->image, off, size, c); 703787f3133SJosh Durgin break; 704dc7588c1SJosh Durgin case RBD_AIO_FLUSH: 705dc7588c1SJosh Durgin r = rbd_aio_flush_wrapper(s->image, c); 706dc7588c1SJosh Durgin break; 707787f3133SJosh Durgin default: 708787f3133SJosh Durgin r = -EINVAL; 70951a13528SJosh Durgin } 71051a13528SJosh Durgin 71151a13528SJosh Durgin if (r < 0) { 712405a2764SKevin Wolf goto failed_completion; 713f27aaf4bSChristian Brunner } 714f27aaf4bSChristian Brunner 715f27aaf4bSChristian Brunner return &acb->common; 71651a13528SJosh Durgin 717405a2764SKevin Wolf failed_completion: 718405a2764SKevin Wolf rbd_aio_release(c); 71951a13528SJosh Durgin failed: 7207267c094SAnthony Liguori g_free(rcb); 721405a2764SKevin Wolf qemu_vfree(acb->bounce); 7228007429aSFam Zheng qemu_aio_unref(acb); 72351a13528SJosh Durgin return NULL; 724f27aaf4bSChristian Brunner } 725f27aaf4bSChristian Brunner 7267c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs, 727ad32e9c0SJosh Durgin int64_t sector_num, 728ad32e9c0SJosh Durgin QEMUIOVector *qiov, 729f27aaf4bSChristian Brunner int nb_sectors, 730097310b5SMarkus Armbruster BlockCompletionFunc *cb, 731f27aaf4bSChristian Brunner void *opaque) 732f27aaf4bSChristian Brunner { 733787f3133SJosh Durgin return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque, 734787f3133SJosh Durgin RBD_AIO_READ); 735f27aaf4bSChristian Brunner } 736f27aaf4bSChristian Brunner 7377c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs, 738ad32e9c0SJosh Durgin int64_t sector_num, 739ad32e9c0SJosh Durgin QEMUIOVector *qiov, 740f27aaf4bSChristian Brunner int nb_sectors, 741097310b5SMarkus Armbruster BlockCompletionFunc *cb, 742f27aaf4bSChristian Brunner void *opaque) 743f27aaf4bSChristian Brunner { 744787f3133SJosh Durgin return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque, 745787f3133SJosh Durgin RBD_AIO_WRITE); 746f27aaf4bSChristian Brunner } 747f27aaf4bSChristian Brunner 748dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH 7497c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs, 750097310b5SMarkus Armbruster BlockCompletionFunc *cb, 751dc7588c1SJosh Durgin void *opaque) 752dc7588c1SJosh Durgin { 753dc7588c1SJosh Durgin return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH); 754dc7588c1SJosh Durgin } 755dc7588c1SJosh Durgin 756dc7588c1SJosh Durgin #else 757dc7588c1SJosh Durgin 7588b94ff85SPaolo Bonzini static int qemu_rbd_co_flush(BlockDriverState *bs) 7597a3f5fe9SSage Weil { 7607a3f5fe9SSage Weil #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1) 7617a3f5fe9SSage Weil /* rbd_flush added in 0.1.1 */ 7627a3f5fe9SSage Weil BDRVRBDState *s = bs->opaque; 7637a3f5fe9SSage Weil return rbd_flush(s->image); 7647a3f5fe9SSage Weil #else 7657a3f5fe9SSage Weil return 0; 7667a3f5fe9SSage Weil #endif 7677a3f5fe9SSage Weil } 768dc7588c1SJosh Durgin #endif 7697a3f5fe9SSage Weil 770ad32e9c0SJosh Durgin static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi) 771f27aaf4bSChristian Brunner { 772f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 773ad32e9c0SJosh Durgin rbd_image_info_t info; 774ad32e9c0SJosh Durgin int r; 775ad32e9c0SJosh Durgin 776ad32e9c0SJosh Durgin r = rbd_stat(s->image, &info, sizeof(info)); 777ad32e9c0SJosh Durgin if (r < 0) { 778ad32e9c0SJosh Durgin return r; 779ad32e9c0SJosh Durgin } 780ad32e9c0SJosh Durgin 781ad32e9c0SJosh Durgin bdi->cluster_size = info.obj_size; 782f27aaf4bSChristian Brunner return 0; 783f27aaf4bSChristian Brunner } 784f27aaf4bSChristian Brunner 785ad32e9c0SJosh Durgin static int64_t qemu_rbd_getlength(BlockDriverState *bs) 786f27aaf4bSChristian Brunner { 787f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 788ad32e9c0SJosh Durgin rbd_image_info_t info; 789ad32e9c0SJosh Durgin int r; 790f27aaf4bSChristian Brunner 791ad32e9c0SJosh Durgin r = rbd_stat(s->image, &info, sizeof(info)); 792ad32e9c0SJosh Durgin if (r < 0) { 793ad32e9c0SJosh Durgin return r; 794f27aaf4bSChristian Brunner } 795f27aaf4bSChristian Brunner 796ad32e9c0SJosh Durgin return info.size; 797ad32e9c0SJosh Durgin } 798ad32e9c0SJosh Durgin 79930cdc48cSJosh Durgin static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset) 80030cdc48cSJosh Durgin { 80130cdc48cSJosh Durgin BDRVRBDState *s = bs->opaque; 80230cdc48cSJosh Durgin int r; 80330cdc48cSJosh Durgin 80430cdc48cSJosh Durgin r = rbd_resize(s->image, offset); 80530cdc48cSJosh Durgin if (r < 0) { 80630cdc48cSJosh Durgin return r; 80730cdc48cSJosh Durgin } 80830cdc48cSJosh Durgin 80930cdc48cSJosh Durgin return 0; 81030cdc48cSJosh Durgin } 81130cdc48cSJosh Durgin 812ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs, 813ad32e9c0SJosh Durgin QEMUSnapshotInfo *sn_info) 814f27aaf4bSChristian Brunner { 815f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 816f27aaf4bSChristian Brunner int r; 817f27aaf4bSChristian Brunner 818f27aaf4bSChristian Brunner if (sn_info->name[0] == '\0') { 819f27aaf4bSChristian Brunner return -EINVAL; /* we need a name for rbd snapshots */ 820f27aaf4bSChristian Brunner } 821f27aaf4bSChristian Brunner 822f27aaf4bSChristian Brunner /* 823f27aaf4bSChristian Brunner * rbd snapshots are using the name as the user controlled unique identifier 824f27aaf4bSChristian Brunner * we can't use the rbd snapid for that purpose, as it can't be set 825f27aaf4bSChristian Brunner */ 826f27aaf4bSChristian Brunner if (sn_info->id_str[0] != '\0' && 827f27aaf4bSChristian Brunner strcmp(sn_info->id_str, sn_info->name) != 0) { 828f27aaf4bSChristian Brunner return -EINVAL; 829f27aaf4bSChristian Brunner } 830f27aaf4bSChristian Brunner 831f27aaf4bSChristian Brunner if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) { 832f27aaf4bSChristian Brunner return -ERANGE; 833f27aaf4bSChristian Brunner } 834f27aaf4bSChristian Brunner 835ad32e9c0SJosh Durgin r = rbd_snap_create(s->image, sn_info->name); 836f27aaf4bSChristian Brunner if (r < 0) { 837ad32e9c0SJosh Durgin error_report("failed to create snap: %s", strerror(-r)); 838f27aaf4bSChristian Brunner return r; 839f27aaf4bSChristian Brunner } 840f27aaf4bSChristian Brunner 841f27aaf4bSChristian Brunner return 0; 842f27aaf4bSChristian Brunner } 843f27aaf4bSChristian Brunner 844bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs, 845a89d89d3SWenchao Xia const char *snapshot_id, 846a89d89d3SWenchao Xia const char *snapshot_name, 847a89d89d3SWenchao Xia Error **errp) 848bd603247SGregory Farnum { 849bd603247SGregory Farnum BDRVRBDState *s = bs->opaque; 850bd603247SGregory Farnum int r; 851bd603247SGregory Farnum 852a89d89d3SWenchao Xia if (!snapshot_name) { 853a89d89d3SWenchao Xia error_setg(errp, "rbd need a valid snapshot name"); 854a89d89d3SWenchao Xia return -EINVAL; 855a89d89d3SWenchao Xia } 856a89d89d3SWenchao Xia 857a89d89d3SWenchao Xia /* If snapshot_id is specified, it must be equal to name, see 858a89d89d3SWenchao Xia qemu_rbd_snap_list() */ 859a89d89d3SWenchao Xia if (snapshot_id && strcmp(snapshot_id, snapshot_name)) { 860a89d89d3SWenchao Xia error_setg(errp, 861a89d89d3SWenchao Xia "rbd do not support snapshot id, it should be NULL or " 862a89d89d3SWenchao Xia "equal to snapshot name"); 863a89d89d3SWenchao Xia return -EINVAL; 864a89d89d3SWenchao Xia } 865a89d89d3SWenchao Xia 866bd603247SGregory Farnum r = rbd_snap_remove(s->image, snapshot_name); 867a89d89d3SWenchao Xia if (r < 0) { 868a89d89d3SWenchao Xia error_setg_errno(errp, -r, "Failed to remove the snapshot"); 869a89d89d3SWenchao Xia } 870bd603247SGregory Farnum return r; 871bd603247SGregory Farnum } 872bd603247SGregory Farnum 873bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs, 874bd603247SGregory Farnum const char *snapshot_name) 875bd603247SGregory Farnum { 876bd603247SGregory Farnum BDRVRBDState *s = bs->opaque; 877bd603247SGregory Farnum int r; 878bd603247SGregory Farnum 879bd603247SGregory Farnum r = rbd_snap_rollback(s->image, snapshot_name); 880bd603247SGregory Farnum return r; 881bd603247SGregory Farnum } 882bd603247SGregory Farnum 883ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs, 884ad32e9c0SJosh Durgin QEMUSnapshotInfo **psn_tab) 885f27aaf4bSChristian Brunner { 886f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 887f27aaf4bSChristian Brunner QEMUSnapshotInfo *sn_info, *sn_tab = NULL; 888ad32e9c0SJosh Durgin int i, snap_count; 889ad32e9c0SJosh Durgin rbd_snap_info_t *snaps; 890ad32e9c0SJosh Durgin int max_snaps = RBD_MAX_SNAPS; 891f27aaf4bSChristian Brunner 892ad32e9c0SJosh Durgin do { 89302c4f26bSMarkus Armbruster snaps = g_new(rbd_snap_info_t, max_snaps); 894ad32e9c0SJosh Durgin snap_count = rbd_snap_list(s->image, snaps, &max_snaps); 8959e6337d0SStefan Hajnoczi if (snap_count <= 0) { 8967267c094SAnthony Liguori g_free(snaps); 897f27aaf4bSChristian Brunner } 898ad32e9c0SJosh Durgin } while (snap_count == -ERANGE); 899f27aaf4bSChristian Brunner 900ad32e9c0SJosh Durgin if (snap_count <= 0) { 901b9c53290SJosh Durgin goto done; 902f27aaf4bSChristian Brunner } 903f27aaf4bSChristian Brunner 9045839e53bSMarkus Armbruster sn_tab = g_new0(QEMUSnapshotInfo, snap_count); 905f27aaf4bSChristian Brunner 906ad32e9c0SJosh Durgin for (i = 0; i < snap_count; i++) { 907ad32e9c0SJosh Durgin const char *snap_name = snaps[i].name; 908f27aaf4bSChristian Brunner 909f27aaf4bSChristian Brunner sn_info = sn_tab + i; 910f27aaf4bSChristian Brunner pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name); 911f27aaf4bSChristian Brunner pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name); 912f27aaf4bSChristian Brunner 913ad32e9c0SJosh Durgin sn_info->vm_state_size = snaps[i].size; 914f27aaf4bSChristian Brunner sn_info->date_sec = 0; 915f27aaf4bSChristian Brunner sn_info->date_nsec = 0; 916f27aaf4bSChristian Brunner sn_info->vm_clock_nsec = 0; 917f27aaf4bSChristian Brunner } 918ad32e9c0SJosh Durgin rbd_snap_list_end(snaps); 9199e6337d0SStefan Hajnoczi g_free(snaps); 920ad32e9c0SJosh Durgin 921b9c53290SJosh Durgin done: 922f27aaf4bSChristian Brunner *psn_tab = sn_tab; 923f27aaf4bSChristian Brunner return snap_count; 924f27aaf4bSChristian Brunner } 925f27aaf4bSChristian Brunner 926787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 9277c84b1b8SMarkus Armbruster static BlockAIOCB* qemu_rbd_aio_discard(BlockDriverState *bs, 928787f3133SJosh Durgin int64_t sector_num, 929787f3133SJosh Durgin int nb_sectors, 930097310b5SMarkus Armbruster BlockCompletionFunc *cb, 931787f3133SJosh Durgin void *opaque) 932787f3133SJosh Durgin { 933787f3133SJosh Durgin return rbd_start_aio(bs, sector_num, NULL, nb_sectors, cb, opaque, 934787f3133SJosh Durgin RBD_AIO_DISCARD); 935787f3133SJosh Durgin } 936787f3133SJosh Durgin #endif 937787f3133SJosh Durgin 938be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE 939be217884SAdam Crume static void qemu_rbd_invalidate_cache(BlockDriverState *bs, 940be217884SAdam Crume Error **errp) 941be217884SAdam Crume { 942be217884SAdam Crume BDRVRBDState *s = bs->opaque; 943be217884SAdam Crume int r = rbd_invalidate_cache(s->image); 944be217884SAdam Crume if (r < 0) { 945be217884SAdam Crume error_setg_errno(errp, -r, "Failed to invalidate the cache"); 946be217884SAdam Crume } 947be217884SAdam Crume } 948be217884SAdam Crume #endif 949be217884SAdam Crume 950bd0cf596SChunyan Liu static QemuOptsList qemu_rbd_create_opts = { 951bd0cf596SChunyan Liu .name = "rbd-create-opts", 952bd0cf596SChunyan Liu .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head), 953bd0cf596SChunyan Liu .desc = { 954f27aaf4bSChristian Brunner { 955f27aaf4bSChristian Brunner .name = BLOCK_OPT_SIZE, 956bd0cf596SChunyan Liu .type = QEMU_OPT_SIZE, 957f27aaf4bSChristian Brunner .help = "Virtual disk size" 958f27aaf4bSChristian Brunner }, 959f27aaf4bSChristian Brunner { 960f27aaf4bSChristian Brunner .name = BLOCK_OPT_CLUSTER_SIZE, 961bd0cf596SChunyan Liu .type = QEMU_OPT_SIZE, 962f27aaf4bSChristian Brunner .help = "RBD object size" 963f27aaf4bSChristian Brunner }, 964*60390a21SDaniel P. Berrange { 965*60390a21SDaniel P. Berrange .name = "password-secret", 966*60390a21SDaniel P. Berrange .type = QEMU_OPT_STRING, 967*60390a21SDaniel P. Berrange .help = "ID of secret providing the password", 968*60390a21SDaniel P. Berrange }, 969bd0cf596SChunyan Liu { /* end of list */ } 970bd0cf596SChunyan Liu } 971f27aaf4bSChristian Brunner }; 972f27aaf4bSChristian Brunner 973f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = { 974f27aaf4bSChristian Brunner .format_name = "rbd", 975f27aaf4bSChristian Brunner .instance_size = sizeof(BDRVRBDState), 976030be321SBenoît Canet .bdrv_needs_filename = true, 977ad32e9c0SJosh Durgin .bdrv_file_open = qemu_rbd_open, 978ad32e9c0SJosh Durgin .bdrv_close = qemu_rbd_close, 979c282e1fdSChunyan Liu .bdrv_create = qemu_rbd_create, 9803ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1, 981ad32e9c0SJosh Durgin .bdrv_get_info = qemu_rbd_getinfo, 982bd0cf596SChunyan Liu .create_opts = &qemu_rbd_create_opts, 983ad32e9c0SJosh Durgin .bdrv_getlength = qemu_rbd_getlength, 98430cdc48cSJosh Durgin .bdrv_truncate = qemu_rbd_truncate, 985f27aaf4bSChristian Brunner .protocol_name = "rbd", 986f27aaf4bSChristian Brunner 987ad32e9c0SJosh Durgin .bdrv_aio_readv = qemu_rbd_aio_readv, 988ad32e9c0SJosh Durgin .bdrv_aio_writev = qemu_rbd_aio_writev, 989dc7588c1SJosh Durgin 990dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH 991dc7588c1SJosh Durgin .bdrv_aio_flush = qemu_rbd_aio_flush, 992dc7588c1SJosh Durgin #else 993c68b89acSKevin Wolf .bdrv_co_flush_to_disk = qemu_rbd_co_flush, 994dc7588c1SJosh Durgin #endif 995f27aaf4bSChristian Brunner 996787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 997787f3133SJosh Durgin .bdrv_aio_discard = qemu_rbd_aio_discard, 998787f3133SJosh Durgin #endif 999787f3133SJosh Durgin 1000ad32e9c0SJosh Durgin .bdrv_snapshot_create = qemu_rbd_snap_create, 1001bd603247SGregory Farnum .bdrv_snapshot_delete = qemu_rbd_snap_remove, 1002ad32e9c0SJosh Durgin .bdrv_snapshot_list = qemu_rbd_snap_list, 1003bd603247SGregory Farnum .bdrv_snapshot_goto = qemu_rbd_snap_rollback, 1004be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE 1005be217884SAdam Crume .bdrv_invalidate_cache = qemu_rbd_invalidate_cache, 1006be217884SAdam Crume #endif 1007f27aaf4bSChristian Brunner }; 1008f27aaf4bSChristian Brunner 1009f27aaf4bSChristian Brunner static void bdrv_rbd_init(void) 1010f27aaf4bSChristian Brunner { 1011f27aaf4bSChristian Brunner bdrv_register(&bdrv_rbd); 1012f27aaf4bSChristian Brunner } 1013f27aaf4bSChristian Brunner 1014f27aaf4bSChristian Brunner block_init(bdrv_rbd_init); 1015