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 14ad32e9c0SJosh Durgin #include <inttypes.h> 15ad32e9c0SJosh Durgin 16f27aaf4bSChristian Brunner #include "qemu-common.h" 17f27aaf4bSChristian Brunner #include "qemu-error.h" 18f27aaf4bSChristian Brunner #include "block_int.h" 19f27aaf4bSChristian Brunner 20ad32e9c0SJosh Durgin #include <rbd/librbd.h> 21f27aaf4bSChristian Brunner 22f27aaf4bSChristian Brunner /* 23f27aaf4bSChristian Brunner * When specifying the image filename use: 24f27aaf4bSChristian Brunner * 25fab5cf59SJosh Durgin * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]] 26f27aaf4bSChristian Brunner * 279e1fbcdeSSage Weil * poolname must be the name of an existing rados pool. 28f27aaf4bSChristian Brunner * 299e1fbcdeSSage Weil * devicename is the name of the rbd image. 30f27aaf4bSChristian Brunner * 319e1fbcdeSSage Weil * Each option given is used to configure rados, and may be any valid 329e1fbcdeSSage Weil * Ceph option, "id", or "conf". 33fab5cf59SJosh Durgin * 349e1fbcdeSSage Weil * The "id" option indicates what user we should authenticate as to 359e1fbcdeSSage Weil * the Ceph cluster. If it is excluded we will use the Ceph default 369e1fbcdeSSage Weil * (normally 'admin'). 37f27aaf4bSChristian Brunner * 389e1fbcdeSSage Weil * The "conf" option specifies a Ceph configuration file to read. If 399e1fbcdeSSage Weil * it is not specified, we will read from the default Ceph locations 409e1fbcdeSSage Weil * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration 419e1fbcdeSSage Weil * file, specify conf=/dev/null. 42f27aaf4bSChristian Brunner * 439e1fbcdeSSage Weil * Configuration values containing :, @, or = can be escaped with a 449e1fbcdeSSage Weil * leading "\". 45f27aaf4bSChristian Brunner */ 46f27aaf4bSChristian Brunner 47787f3133SJosh Durgin /* rbd_aio_discard added in 0.1.2 */ 48787f3133SJosh Durgin #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2) 49787f3133SJosh Durgin #define LIBRBD_SUPPORTS_DISCARD 50787f3133SJosh Durgin #else 51787f3133SJosh Durgin #undef LIBRBD_SUPPORTS_DISCARD 52787f3133SJosh Durgin #endif 53787f3133SJosh Durgin 54f27aaf4bSChristian Brunner #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER) 55f27aaf4bSChristian Brunner 56ad32e9c0SJosh Durgin #define RBD_MAX_CONF_NAME_SIZE 128 57ad32e9c0SJosh Durgin #define RBD_MAX_CONF_VAL_SIZE 512 58ad32e9c0SJosh Durgin #define RBD_MAX_CONF_SIZE 1024 59ad32e9c0SJosh Durgin #define RBD_MAX_POOL_NAME_SIZE 128 60ad32e9c0SJosh Durgin #define RBD_MAX_SNAP_NAME_SIZE 128 61ad32e9c0SJosh Durgin #define RBD_MAX_SNAPS 100 62ad32e9c0SJosh Durgin 63787f3133SJosh Durgin typedef enum { 64787f3133SJosh Durgin RBD_AIO_READ, 65787f3133SJosh Durgin RBD_AIO_WRITE, 66787f3133SJosh Durgin RBD_AIO_DISCARD 67787f3133SJosh Durgin } RBDAIOCmd; 68787f3133SJosh Durgin 69f27aaf4bSChristian Brunner typedef struct RBDAIOCB { 70f27aaf4bSChristian Brunner BlockDriverAIOCB common; 71f27aaf4bSChristian Brunner QEMUBH *bh; 72f27aaf4bSChristian Brunner int ret; 73f27aaf4bSChristian Brunner QEMUIOVector *qiov; 74f27aaf4bSChristian Brunner char *bounce; 75787f3133SJosh Durgin RBDAIOCmd cmd; 76f27aaf4bSChristian Brunner int64_t sector_num; 77f27aaf4bSChristian Brunner int error; 78f27aaf4bSChristian Brunner struct BDRVRBDState *s; 79f27aaf4bSChristian Brunner int cancelled; 80f27aaf4bSChristian Brunner } RBDAIOCB; 81f27aaf4bSChristian Brunner 82f27aaf4bSChristian Brunner typedef struct RADOSCB { 83f27aaf4bSChristian Brunner int rcbid; 84f27aaf4bSChristian Brunner RBDAIOCB *acb; 85f27aaf4bSChristian Brunner struct BDRVRBDState *s; 86f27aaf4bSChristian Brunner int done; 87ad32e9c0SJosh Durgin int64_t size; 88f27aaf4bSChristian Brunner char *buf; 89f27aaf4bSChristian Brunner int ret; 90f27aaf4bSChristian Brunner } RADOSCB; 91f27aaf4bSChristian Brunner 92f27aaf4bSChristian Brunner #define RBD_FD_READ 0 93f27aaf4bSChristian Brunner #define RBD_FD_WRITE 1 94f27aaf4bSChristian Brunner 95f27aaf4bSChristian Brunner typedef struct BDRVRBDState { 96f27aaf4bSChristian Brunner int fds[2]; 97ad32e9c0SJosh Durgin rados_t cluster; 98ad32e9c0SJosh Durgin rados_ioctx_t io_ctx; 99ad32e9c0SJosh Durgin rbd_image_t image; 100ad32e9c0SJosh Durgin char name[RBD_MAX_IMAGE_NAME_SIZE]; 101f27aaf4bSChristian Brunner int qemu_aio_count; 102ad32e9c0SJosh Durgin char *snap; 103f27aaf4bSChristian Brunner int event_reader_pos; 104f27aaf4bSChristian Brunner RADOSCB *event_rcb; 105f27aaf4bSChristian Brunner } BDRVRBDState; 106f27aaf4bSChristian Brunner 107f27aaf4bSChristian Brunner static void rbd_aio_bh_cb(void *opaque); 108f27aaf4bSChristian Brunner 109ad32e9c0SJosh Durgin static int qemu_rbd_next_tok(char *dst, int dst_len, 110f27aaf4bSChristian Brunner char *src, char delim, 111f27aaf4bSChristian Brunner const char *name, 112f27aaf4bSChristian Brunner char **p) 113f27aaf4bSChristian Brunner { 114f27aaf4bSChristian Brunner int l; 115f27aaf4bSChristian Brunner char *end; 116f27aaf4bSChristian Brunner 117f27aaf4bSChristian Brunner *p = NULL; 118f27aaf4bSChristian Brunner 119f27aaf4bSChristian Brunner if (delim != '\0') { 12016a06b24SSage Weil for (end = src; *end; ++end) { 12116a06b24SSage Weil if (*end == delim) { 12216a06b24SSage Weil break; 12316a06b24SSage Weil } 12416a06b24SSage Weil if (*end == '\\' && end[1] != '\0') { 12516a06b24SSage Weil end++; 12616a06b24SSage Weil } 12716a06b24SSage Weil } 12816a06b24SSage Weil if (*end == delim) { 129f27aaf4bSChristian Brunner *p = end + 1; 130f27aaf4bSChristian Brunner *end = '\0'; 131f27aaf4bSChristian Brunner } 132f27aaf4bSChristian Brunner } 133f27aaf4bSChristian Brunner l = strlen(src); 134f27aaf4bSChristian Brunner if (l >= dst_len) { 135f27aaf4bSChristian Brunner error_report("%s too long", name); 136f27aaf4bSChristian Brunner return -EINVAL; 137f27aaf4bSChristian Brunner } else if (l == 0) { 138f27aaf4bSChristian Brunner error_report("%s too short", name); 139f27aaf4bSChristian Brunner return -EINVAL; 140f27aaf4bSChristian Brunner } 141f27aaf4bSChristian Brunner 142f27aaf4bSChristian Brunner pstrcpy(dst, dst_len, src); 143f27aaf4bSChristian Brunner 144f27aaf4bSChristian Brunner return 0; 145f27aaf4bSChristian Brunner } 146f27aaf4bSChristian Brunner 14716a06b24SSage Weil static void qemu_rbd_unescape(char *src) 14816a06b24SSage Weil { 14916a06b24SSage Weil char *p; 15016a06b24SSage Weil 15116a06b24SSage Weil for (p = src; *src; ++src, ++p) { 15216a06b24SSage Weil if (*src == '\\' && src[1] != '\0') { 15316a06b24SSage Weil src++; 15416a06b24SSage Weil } 15516a06b24SSage Weil *p = *src; 15616a06b24SSage Weil } 15716a06b24SSage Weil *p = '\0'; 15816a06b24SSage Weil } 15916a06b24SSage Weil 160ad32e9c0SJosh Durgin static int qemu_rbd_parsename(const char *filename, 161f27aaf4bSChristian Brunner char *pool, int pool_len, 162f27aaf4bSChristian Brunner char *snap, int snap_len, 163fab5cf59SJosh Durgin char *name, int name_len, 164fab5cf59SJosh Durgin char *conf, int conf_len) 165f27aaf4bSChristian Brunner { 166f27aaf4bSChristian Brunner const char *start; 167f27aaf4bSChristian Brunner char *p, *buf; 168f27aaf4bSChristian Brunner int ret; 169f27aaf4bSChristian Brunner 170f27aaf4bSChristian Brunner if (!strstart(filename, "rbd:", &start)) { 171f27aaf4bSChristian Brunner return -EINVAL; 172f27aaf4bSChristian Brunner } 173f27aaf4bSChristian Brunner 1747267c094SAnthony Liguori buf = g_strdup(start); 175f27aaf4bSChristian Brunner p = buf; 176fab5cf59SJosh Durgin *snap = '\0'; 177fab5cf59SJosh Durgin *conf = '\0'; 178f27aaf4bSChristian Brunner 179ad32e9c0SJosh Durgin ret = qemu_rbd_next_tok(pool, pool_len, p, '/', "pool name", &p); 180f27aaf4bSChristian Brunner if (ret < 0 || !p) { 181f27aaf4bSChristian Brunner ret = -EINVAL; 182f27aaf4bSChristian Brunner goto done; 183f27aaf4bSChristian Brunner } 18416a06b24SSage Weil qemu_rbd_unescape(pool); 185fab5cf59SJosh Durgin 186fab5cf59SJosh Durgin if (strchr(p, '@')) { 187ad32e9c0SJosh Durgin ret = qemu_rbd_next_tok(name, name_len, p, '@', "object name", &p); 188f27aaf4bSChristian Brunner if (ret < 0) { 189f27aaf4bSChristian Brunner goto done; 190f27aaf4bSChristian Brunner } 191fab5cf59SJosh Durgin ret = qemu_rbd_next_tok(snap, snap_len, p, ':', "snap name", &p); 19216a06b24SSage Weil qemu_rbd_unescape(snap); 193fab5cf59SJosh Durgin } else { 194fab5cf59SJosh Durgin ret = qemu_rbd_next_tok(name, name_len, p, ':', "object name", &p); 195fab5cf59SJosh Durgin } 19616a06b24SSage Weil qemu_rbd_unescape(name); 197fab5cf59SJosh Durgin if (ret < 0 || !p) { 198f27aaf4bSChristian Brunner goto done; 199f27aaf4bSChristian Brunner } 200f27aaf4bSChristian Brunner 201fab5cf59SJosh Durgin ret = qemu_rbd_next_tok(conf, conf_len, p, '\0', "configuration", &p); 202f27aaf4bSChristian Brunner 203f27aaf4bSChristian Brunner done: 2047267c094SAnthony Liguori g_free(buf); 205f27aaf4bSChristian Brunner return ret; 206f27aaf4bSChristian Brunner } 207f27aaf4bSChristian Brunner 2087c7e9df0SSage Weil static char *qemu_rbd_parse_clientname(const char *conf, char *clientname) 2097c7e9df0SSage Weil { 2107c7e9df0SSage Weil const char *p = conf; 2117c7e9df0SSage Weil 2127c7e9df0SSage Weil while (*p) { 2137c7e9df0SSage Weil int len; 2147c7e9df0SSage Weil const char *end = strchr(p, ':'); 2157c7e9df0SSage Weil 2167c7e9df0SSage Weil if (end) { 2177c7e9df0SSage Weil len = end - p; 2187c7e9df0SSage Weil } else { 2197c7e9df0SSage Weil len = strlen(p); 2207c7e9df0SSage Weil } 2217c7e9df0SSage Weil 2227c7e9df0SSage Weil if (strncmp(p, "id=", 3) == 0) { 2237c7e9df0SSage Weil len -= 3; 2247c7e9df0SSage Weil strncpy(clientname, p + 3, len); 2257c7e9df0SSage Weil clientname[len] = '\0'; 2267c7e9df0SSage Weil return clientname; 2277c7e9df0SSage Weil } 2287c7e9df0SSage Weil if (end == NULL) { 2297c7e9df0SSage Weil break; 2307c7e9df0SSage Weil } 2317c7e9df0SSage Weil p = end + 1; 2327c7e9df0SSage Weil } 2337c7e9df0SSage Weil return NULL; 2347c7e9df0SSage Weil } 2357c7e9df0SSage Weil 236fab5cf59SJosh Durgin static int qemu_rbd_set_conf(rados_t cluster, const char *conf) 237fab5cf59SJosh Durgin { 238fab5cf59SJosh Durgin char *p, *buf; 239fab5cf59SJosh Durgin char name[RBD_MAX_CONF_NAME_SIZE]; 240fab5cf59SJosh Durgin char value[RBD_MAX_CONF_VAL_SIZE]; 241fab5cf59SJosh Durgin int ret = 0; 242fab5cf59SJosh Durgin 2437267c094SAnthony Liguori buf = g_strdup(conf); 244fab5cf59SJosh Durgin p = buf; 245fab5cf59SJosh Durgin 246fab5cf59SJosh Durgin while (p) { 247fab5cf59SJosh Durgin ret = qemu_rbd_next_tok(name, sizeof(name), p, 248fab5cf59SJosh Durgin '=', "conf option name", &p); 249fab5cf59SJosh Durgin if (ret < 0) { 250fab5cf59SJosh Durgin break; 251fab5cf59SJosh Durgin } 25216a06b24SSage Weil qemu_rbd_unescape(name); 253fab5cf59SJosh Durgin 254fab5cf59SJosh Durgin if (!p) { 255fab5cf59SJosh Durgin error_report("conf option %s has no value", name); 256fab5cf59SJosh Durgin ret = -EINVAL; 257fab5cf59SJosh Durgin break; 258fab5cf59SJosh Durgin } 259fab5cf59SJosh Durgin 260fab5cf59SJosh Durgin ret = qemu_rbd_next_tok(value, sizeof(value), p, 261fab5cf59SJosh Durgin ':', "conf option value", &p); 262fab5cf59SJosh Durgin if (ret < 0) { 263fab5cf59SJosh Durgin break; 264fab5cf59SJosh Durgin } 26516a06b24SSage Weil qemu_rbd_unescape(value); 266fab5cf59SJosh Durgin 2677c7e9df0SSage Weil if (strcmp(name, "conf") == 0) { 2687c7e9df0SSage Weil ret = rados_conf_read_file(cluster, value); 2697c7e9df0SSage Weil if (ret < 0) { 2707c7e9df0SSage Weil error_report("error reading conf file %s", value); 2717c7e9df0SSage Weil break; 2727c7e9df0SSage Weil } 2737c7e9df0SSage Weil } else if (strcmp(name, "id") == 0) { 2747c7e9df0SSage Weil /* ignore, this is parsed by qemu_rbd_parse_clientname() */ 2757c7e9df0SSage Weil } else { 276fab5cf59SJosh Durgin ret = rados_conf_set(cluster, name, value); 277fab5cf59SJosh Durgin if (ret < 0) { 278fab5cf59SJosh Durgin error_report("invalid conf option %s", name); 279fab5cf59SJosh Durgin ret = -EINVAL; 280fab5cf59SJosh Durgin break; 281fab5cf59SJosh Durgin } 282fab5cf59SJosh Durgin } 283fab5cf59SJosh Durgin } 284fab5cf59SJosh Durgin 2857267c094SAnthony Liguori g_free(buf); 286fab5cf59SJosh Durgin return ret; 287fab5cf59SJosh Durgin } 288fab5cf59SJosh Durgin 289ad32e9c0SJosh Durgin static int qemu_rbd_create(const char *filename, QEMUOptionParameter *options) 290f27aaf4bSChristian Brunner { 291f27aaf4bSChristian Brunner int64_t bytes = 0; 292f27aaf4bSChristian Brunner int64_t objsize; 293ad32e9c0SJosh Durgin int obj_order = 0; 294ad32e9c0SJosh Durgin char pool[RBD_MAX_POOL_NAME_SIZE]; 295ad32e9c0SJosh Durgin char name[RBD_MAX_IMAGE_NAME_SIZE]; 296ad32e9c0SJosh Durgin char snap_buf[RBD_MAX_SNAP_NAME_SIZE]; 297fab5cf59SJosh Durgin char conf[RBD_MAX_CONF_SIZE]; 2987c7e9df0SSage Weil char clientname_buf[RBD_MAX_CONF_SIZE]; 2997c7e9df0SSage Weil char *clientname; 300ad32e9c0SJosh Durgin rados_t cluster; 301ad32e9c0SJosh Durgin rados_ioctx_t io_ctx; 302f27aaf4bSChristian Brunner int ret; 303f27aaf4bSChristian Brunner 304ad32e9c0SJosh Durgin if (qemu_rbd_parsename(filename, pool, sizeof(pool), 305f27aaf4bSChristian Brunner snap_buf, sizeof(snap_buf), 306fab5cf59SJosh Durgin name, sizeof(name), 307fab5cf59SJosh Durgin conf, sizeof(conf)) < 0) { 308f27aaf4bSChristian Brunner return -EINVAL; 309f27aaf4bSChristian Brunner } 310f27aaf4bSChristian Brunner 311f27aaf4bSChristian Brunner /* Read out options */ 312f27aaf4bSChristian Brunner while (options && options->name) { 313f27aaf4bSChristian Brunner if (!strcmp(options->name, BLOCK_OPT_SIZE)) { 314f27aaf4bSChristian Brunner bytes = options->value.n; 315f27aaf4bSChristian Brunner } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) { 316f27aaf4bSChristian Brunner if (options->value.n) { 317f27aaf4bSChristian Brunner objsize = options->value.n; 318f27aaf4bSChristian Brunner if ((objsize - 1) & objsize) { /* not a power of 2? */ 319f27aaf4bSChristian Brunner error_report("obj size needs to be power of 2"); 320f27aaf4bSChristian Brunner return -EINVAL; 321f27aaf4bSChristian Brunner } 322f27aaf4bSChristian Brunner if (objsize < 4096) { 323f27aaf4bSChristian Brunner error_report("obj size too small"); 324f27aaf4bSChristian Brunner return -EINVAL; 325f27aaf4bSChristian Brunner } 326f27aaf4bSChristian Brunner obj_order = ffs(objsize) - 1; 327f27aaf4bSChristian Brunner } 328f27aaf4bSChristian Brunner } 329f27aaf4bSChristian Brunner options++; 330f27aaf4bSChristian Brunner } 331f27aaf4bSChristian Brunner 3327c7e9df0SSage Weil clientname = qemu_rbd_parse_clientname(conf, clientname_buf); 3337c7e9df0SSage Weil if (rados_create(&cluster, clientname) < 0) { 334f27aaf4bSChristian Brunner error_report("error initializing"); 335f27aaf4bSChristian Brunner return -EIO; 336f27aaf4bSChristian Brunner } 337f27aaf4bSChristian Brunner 338fab5cf59SJosh Durgin if (strstr(conf, "conf=") == NULL) { 339f9fe18ecSSage Weil /* try default location, but ignore failure */ 340f9fe18ecSSage Weil rados_conf_read_file(cluster, NULL); 341fab5cf59SJosh Durgin } 342fab5cf59SJosh Durgin 343fab5cf59SJosh Durgin if (conf[0] != '\0' && 344fab5cf59SJosh Durgin qemu_rbd_set_conf(cluster, conf) < 0) { 345fab5cf59SJosh Durgin error_report("error setting config options"); 346fab5cf59SJosh Durgin rados_shutdown(cluster); 347fab5cf59SJosh Durgin return -EIO; 348fab5cf59SJosh Durgin } 349ad32e9c0SJosh Durgin 350ad32e9c0SJosh Durgin if (rados_connect(cluster) < 0) { 351ad32e9c0SJosh Durgin error_report("error connecting"); 352ad32e9c0SJosh Durgin rados_shutdown(cluster); 353ad32e9c0SJosh Durgin return -EIO; 354ad32e9c0SJosh Durgin } 355ad32e9c0SJosh Durgin 356ad32e9c0SJosh Durgin if (rados_ioctx_create(cluster, pool, &io_ctx) < 0) { 357f27aaf4bSChristian Brunner error_report("error opening pool %s", pool); 358ad32e9c0SJosh Durgin rados_shutdown(cluster); 359f27aaf4bSChristian Brunner return -EIO; 360f27aaf4bSChristian Brunner } 361f27aaf4bSChristian Brunner 362ad32e9c0SJosh Durgin ret = rbd_create(io_ctx, name, bytes, &obj_order); 363ad32e9c0SJosh Durgin rados_ioctx_destroy(io_ctx); 364ad32e9c0SJosh Durgin rados_shutdown(cluster); 365f27aaf4bSChristian Brunner 366f27aaf4bSChristian Brunner return ret; 367f27aaf4bSChristian Brunner } 368f27aaf4bSChristian Brunner 369f27aaf4bSChristian Brunner /* 370ad32e9c0SJosh Durgin * This aio completion is being called from qemu_rbd_aio_event_reader() 371ad32e9c0SJosh Durgin * and runs in qemu context. It schedules a bh, but just in case the aio 372f27aaf4bSChristian Brunner * was not cancelled before. 373f27aaf4bSChristian Brunner */ 374ad32e9c0SJosh Durgin static void qemu_rbd_complete_aio(RADOSCB *rcb) 375f27aaf4bSChristian Brunner { 376f27aaf4bSChristian Brunner RBDAIOCB *acb = rcb->acb; 377f27aaf4bSChristian Brunner int64_t r; 378f27aaf4bSChristian Brunner 379f27aaf4bSChristian Brunner if (acb->cancelled) { 380f27aaf4bSChristian Brunner qemu_vfree(acb->bounce); 381f27aaf4bSChristian Brunner qemu_aio_release(acb); 382f27aaf4bSChristian Brunner goto done; 383f27aaf4bSChristian Brunner } 384f27aaf4bSChristian Brunner 385f27aaf4bSChristian Brunner r = rcb->ret; 386f27aaf4bSChristian Brunner 387787f3133SJosh Durgin if (acb->cmd == RBD_AIO_WRITE || 388787f3133SJosh Durgin acb->cmd == RBD_AIO_DISCARD) { 389f27aaf4bSChristian Brunner if (r < 0) { 390f27aaf4bSChristian Brunner acb->ret = r; 391f27aaf4bSChristian Brunner acb->error = 1; 392f27aaf4bSChristian Brunner } else if (!acb->error) { 393ad32e9c0SJosh Durgin acb->ret = rcb->size; 394f27aaf4bSChristian Brunner } 395f27aaf4bSChristian Brunner } else { 396ad32e9c0SJosh Durgin if (r < 0) { 397ad32e9c0SJosh Durgin memset(rcb->buf, 0, rcb->size); 398f27aaf4bSChristian Brunner acb->ret = r; 399f27aaf4bSChristian Brunner acb->error = 1; 400ad32e9c0SJosh Durgin } else if (r < rcb->size) { 401ad32e9c0SJosh Durgin memset(rcb->buf + r, 0, rcb->size - r); 402f27aaf4bSChristian Brunner if (!acb->error) { 403ad32e9c0SJosh Durgin acb->ret = rcb->size; 404f27aaf4bSChristian Brunner } 405f27aaf4bSChristian Brunner } else if (!acb->error) { 406ad32e9c0SJosh Durgin acb->ret = r; 407f27aaf4bSChristian Brunner } 408f27aaf4bSChristian Brunner } 409f27aaf4bSChristian Brunner /* Note that acb->bh can be NULL in case where the aio was cancelled */ 410f27aaf4bSChristian Brunner acb->bh = qemu_bh_new(rbd_aio_bh_cb, acb); 411f27aaf4bSChristian Brunner qemu_bh_schedule(acb->bh); 412f27aaf4bSChristian Brunner done: 4137267c094SAnthony Liguori g_free(rcb); 414f27aaf4bSChristian Brunner } 415f27aaf4bSChristian Brunner 416f27aaf4bSChristian Brunner /* 417f27aaf4bSChristian Brunner * aio fd read handler. It runs in the qemu context and calls the 418f27aaf4bSChristian Brunner * completion handling of completed rados aio operations. 419f27aaf4bSChristian Brunner */ 420ad32e9c0SJosh Durgin static void qemu_rbd_aio_event_reader(void *opaque) 421f27aaf4bSChristian Brunner { 422f27aaf4bSChristian Brunner BDRVRBDState *s = opaque; 423f27aaf4bSChristian Brunner 424f27aaf4bSChristian Brunner ssize_t ret; 425f27aaf4bSChristian Brunner 426f27aaf4bSChristian Brunner do { 427f27aaf4bSChristian Brunner char *p = (char *)&s->event_rcb; 428f27aaf4bSChristian Brunner 429f27aaf4bSChristian Brunner /* now read the rcb pointer that was sent from a non qemu thread */ 430dfe80b07SSage Weil ret = read(s->fds[RBD_FD_READ], p + s->event_reader_pos, 431dfe80b07SSage Weil sizeof(s->event_rcb) - s->event_reader_pos); 432f27aaf4bSChristian Brunner if (ret > 0) { 433f27aaf4bSChristian Brunner s->event_reader_pos += ret; 434f27aaf4bSChristian Brunner if (s->event_reader_pos == sizeof(s->event_rcb)) { 435f27aaf4bSChristian Brunner s->event_reader_pos = 0; 436ad32e9c0SJosh Durgin qemu_rbd_complete_aio(s->event_rcb); 437f27aaf4bSChristian Brunner s->qemu_aio_count--; 438f27aaf4bSChristian Brunner } 439f27aaf4bSChristian Brunner } 440f27aaf4bSChristian Brunner } while (ret < 0 && errno == EINTR); 441f27aaf4bSChristian Brunner } 442f27aaf4bSChristian Brunner 443ad32e9c0SJosh Durgin static int qemu_rbd_aio_flush_cb(void *opaque) 444f27aaf4bSChristian Brunner { 445f27aaf4bSChristian Brunner BDRVRBDState *s = opaque; 446f27aaf4bSChristian Brunner 447f27aaf4bSChristian Brunner return (s->qemu_aio_count > 0); 448f27aaf4bSChristian Brunner } 449f27aaf4bSChristian Brunner 450ad32e9c0SJosh Durgin static int qemu_rbd_open(BlockDriverState *bs, const char *filename, int flags) 451f27aaf4bSChristian Brunner { 452f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 453ad32e9c0SJosh Durgin char pool[RBD_MAX_POOL_NAME_SIZE]; 454ad32e9c0SJosh Durgin char snap_buf[RBD_MAX_SNAP_NAME_SIZE]; 455fab5cf59SJosh Durgin char conf[RBD_MAX_CONF_SIZE]; 4567c7e9df0SSage Weil char clientname_buf[RBD_MAX_CONF_SIZE]; 4577c7e9df0SSage Weil char *clientname; 458f27aaf4bSChristian Brunner int r; 459f27aaf4bSChristian Brunner 460ad32e9c0SJosh Durgin if (qemu_rbd_parsename(filename, pool, sizeof(pool), 461f27aaf4bSChristian Brunner snap_buf, sizeof(snap_buf), 462fab5cf59SJosh Durgin s->name, sizeof(s->name), 463fab5cf59SJosh Durgin conf, sizeof(conf)) < 0) { 464f27aaf4bSChristian Brunner return -EINVAL; 465f27aaf4bSChristian Brunner } 466f27aaf4bSChristian Brunner 4677c7e9df0SSage Weil clientname = qemu_rbd_parse_clientname(conf, clientname_buf); 4687c7e9df0SSage Weil r = rados_create(&s->cluster, clientname); 469ad32e9c0SJosh Durgin if (r < 0) { 470f27aaf4bSChristian Brunner error_report("error initializing"); 471f27aaf4bSChristian Brunner return r; 472f27aaf4bSChristian Brunner } 473f27aaf4bSChristian Brunner 474eb93d5d9SSage Weil s->snap = NULL; 475eb93d5d9SSage Weil if (snap_buf[0] != '\0') { 476eb93d5d9SSage Weil s->snap = g_strdup(snap_buf); 477eb93d5d9SSage Weil } 478eb93d5d9SSage Weil 479*b11f38fcSJosh Durgin /* 480*b11f38fcSJosh Durgin * Fallback to more conservative semantics if setting cache 481*b11f38fcSJosh Durgin * options fails. Ignore errors from setting rbd_cache because the 482*b11f38fcSJosh Durgin * only possible error is that the option does not exist, and 483*b11f38fcSJosh Durgin * librbd defaults to no caching. If write through caching cannot 484*b11f38fcSJosh Durgin * be set up, fall back to no caching. 485*b11f38fcSJosh Durgin */ 486*b11f38fcSJosh Durgin if (flags & BDRV_O_NOCACHE) { 487*b11f38fcSJosh Durgin rados_conf_set(s->cluster, "rbd_cache", "false"); 488*b11f38fcSJosh Durgin } else { 489*b11f38fcSJosh Durgin rados_conf_set(s->cluster, "rbd_cache", "true"); 490*b11f38fcSJosh Durgin if (!(flags & BDRV_O_CACHE_WB)) { 491*b11f38fcSJosh Durgin r = rados_conf_set(s->cluster, "rbd_cache_max_dirty", "0"); 492*b11f38fcSJosh Durgin if (r < 0) { 493*b11f38fcSJosh Durgin rados_conf_set(s->cluster, "rbd_cache", "false"); 494*b11f38fcSJosh Durgin } 495*b11f38fcSJosh Durgin } 496*b11f38fcSJosh Durgin } 497*b11f38fcSJosh Durgin 498fab5cf59SJosh Durgin if (strstr(conf, "conf=") == NULL) { 499f9fe18ecSSage Weil /* try default location, but ignore failure */ 500f9fe18ecSSage Weil rados_conf_read_file(s->cluster, NULL); 501fab5cf59SJosh Durgin } 502fab5cf59SJosh Durgin 503fab5cf59SJosh Durgin if (conf[0] != '\0') { 504fab5cf59SJosh Durgin r = qemu_rbd_set_conf(s->cluster, conf); 505fab5cf59SJosh Durgin if (r < 0) { 506fab5cf59SJosh Durgin error_report("error setting config options"); 507eb93d5d9SSage Weil goto failed_shutdown; 508fab5cf59SJosh Durgin } 509fab5cf59SJosh Durgin } 510f27aaf4bSChristian Brunner 511ad32e9c0SJosh Durgin r = rados_connect(s->cluster); 512ad32e9c0SJosh Durgin if (r < 0) { 513ad32e9c0SJosh Durgin error_report("error connecting"); 514eb93d5d9SSage Weil goto failed_shutdown; 515ad32e9c0SJosh Durgin } 516ad32e9c0SJosh Durgin 517ad32e9c0SJosh Durgin r = rados_ioctx_create(s->cluster, pool, &s->io_ctx); 518ad32e9c0SJosh Durgin if (r < 0) { 519ad32e9c0SJosh Durgin error_report("error opening pool %s", pool); 520eb93d5d9SSage Weil goto failed_shutdown; 521ad32e9c0SJosh Durgin } 522ad32e9c0SJosh Durgin 523ad32e9c0SJosh Durgin r = rbd_open(s->io_ctx, s->name, &s->image, s->snap); 524ad32e9c0SJosh Durgin if (r < 0) { 525ad32e9c0SJosh Durgin error_report("error reading header from %s", s->name); 526eb93d5d9SSage Weil goto failed_open; 527ad32e9c0SJosh Durgin } 528ad32e9c0SJosh Durgin 529ad32e9c0SJosh Durgin bs->read_only = (s->snap != NULL); 530f27aaf4bSChristian Brunner 531f27aaf4bSChristian Brunner s->event_reader_pos = 0; 532f27aaf4bSChristian Brunner r = qemu_pipe(s->fds); 533f27aaf4bSChristian Brunner if (r < 0) { 534f27aaf4bSChristian Brunner error_report("error opening eventfd"); 535f27aaf4bSChristian Brunner goto failed; 536f27aaf4bSChristian Brunner } 537f27aaf4bSChristian Brunner fcntl(s->fds[0], F_SETFL, O_NONBLOCK); 538f27aaf4bSChristian Brunner fcntl(s->fds[1], F_SETFL, O_NONBLOCK); 539ad32e9c0SJosh Durgin qemu_aio_set_fd_handler(s->fds[RBD_FD_READ], qemu_rbd_aio_event_reader, 540bafbd6a1SPaolo Bonzini NULL, qemu_rbd_aio_flush_cb, s); 541f27aaf4bSChristian Brunner 542f27aaf4bSChristian Brunner 543f27aaf4bSChristian Brunner return 0; 544f27aaf4bSChristian Brunner 545f27aaf4bSChristian Brunner failed: 546ad32e9c0SJosh Durgin rbd_close(s->image); 547eb93d5d9SSage Weil failed_open: 548ad32e9c0SJosh Durgin rados_ioctx_destroy(s->io_ctx); 549eb93d5d9SSage Weil failed_shutdown: 550ad32e9c0SJosh Durgin rados_shutdown(s->cluster); 551eb93d5d9SSage Weil g_free(s->snap); 552f27aaf4bSChristian Brunner return r; 553f27aaf4bSChristian Brunner } 554f27aaf4bSChristian Brunner 555ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs) 556f27aaf4bSChristian Brunner { 557f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 558f27aaf4bSChristian Brunner 559f27aaf4bSChristian Brunner close(s->fds[0]); 560f27aaf4bSChristian Brunner close(s->fds[1]); 561bafbd6a1SPaolo Bonzini qemu_aio_set_fd_handler(s->fds[RBD_FD_READ], NULL, NULL, NULL, NULL); 562f27aaf4bSChristian Brunner 563ad32e9c0SJosh Durgin rbd_close(s->image); 564ad32e9c0SJosh Durgin rados_ioctx_destroy(s->io_ctx); 5657267c094SAnthony Liguori g_free(s->snap); 566ad32e9c0SJosh Durgin rados_shutdown(s->cluster); 567f27aaf4bSChristian Brunner } 568f27aaf4bSChristian Brunner 569f27aaf4bSChristian Brunner /* 570f27aaf4bSChristian Brunner * Cancel aio. Since we don't reference acb in a non qemu threads, 571f27aaf4bSChristian Brunner * it is safe to access it here. 572f27aaf4bSChristian Brunner */ 573ad32e9c0SJosh Durgin static void qemu_rbd_aio_cancel(BlockDriverAIOCB *blockacb) 574f27aaf4bSChristian Brunner { 575f27aaf4bSChristian Brunner RBDAIOCB *acb = (RBDAIOCB *) blockacb; 576f27aaf4bSChristian Brunner acb->cancelled = 1; 577f27aaf4bSChristian Brunner } 578f27aaf4bSChristian Brunner 579f27aaf4bSChristian Brunner static AIOPool rbd_aio_pool = { 580f27aaf4bSChristian Brunner .aiocb_size = sizeof(RBDAIOCB), 581ad32e9c0SJosh Durgin .cancel = qemu_rbd_aio_cancel, 582f27aaf4bSChristian Brunner }; 583f27aaf4bSChristian Brunner 584ad32e9c0SJosh Durgin static int qemu_rbd_send_pipe(BDRVRBDState *s, RADOSCB *rcb) 585f27aaf4bSChristian Brunner { 586ad32e9c0SJosh Durgin int ret = 0; 587f27aaf4bSChristian Brunner while (1) { 588f27aaf4bSChristian Brunner fd_set wfd; 589ad32e9c0SJosh Durgin int fd = s->fds[RBD_FD_WRITE]; 590f27aaf4bSChristian Brunner 591ad32e9c0SJosh Durgin /* send the op pointer to the qemu thread that is responsible 592ad32e9c0SJosh Durgin for the aio/op completion. Must do it in a qemu thread context */ 593f27aaf4bSChristian Brunner ret = write(fd, (void *)&rcb, sizeof(rcb)); 594f27aaf4bSChristian Brunner if (ret >= 0) { 595f27aaf4bSChristian Brunner break; 596f27aaf4bSChristian Brunner } 597f27aaf4bSChristian Brunner if (errno == EINTR) { 598f27aaf4bSChristian Brunner continue; 599f27aaf4bSChristian Brunner } 600f27aaf4bSChristian Brunner if (errno != EAGAIN) { 601f27aaf4bSChristian Brunner break; 602f27aaf4bSChristian Brunner } 603f27aaf4bSChristian Brunner 604f27aaf4bSChristian Brunner FD_ZERO(&wfd); 605f27aaf4bSChristian Brunner FD_SET(fd, &wfd); 606f27aaf4bSChristian Brunner do { 607f27aaf4bSChristian Brunner ret = select(fd + 1, NULL, &wfd, NULL, NULL); 608f27aaf4bSChristian Brunner } while (ret < 0 && errno == EINTR); 609f27aaf4bSChristian Brunner } 610f27aaf4bSChristian Brunner 611ad32e9c0SJosh Durgin return ret; 612ad32e9c0SJosh Durgin } 613ad32e9c0SJosh Durgin 614ad32e9c0SJosh Durgin /* 615ad32e9c0SJosh Durgin * This is the callback function for rbd_aio_read and _write 616ad32e9c0SJosh Durgin * 617ad32e9c0SJosh Durgin * Note: this function is being called from a non qemu thread so 618ad32e9c0SJosh Durgin * we need to be careful about what we do here. Generally we only 619ad32e9c0SJosh Durgin * write to the block notification pipe, and do the rest of the 620ad32e9c0SJosh Durgin * io completion handling from qemu_rbd_aio_event_reader() which 621ad32e9c0SJosh Durgin * runs in a qemu context. 622ad32e9c0SJosh Durgin */ 623ad32e9c0SJosh Durgin static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb) 624ad32e9c0SJosh Durgin { 625ad32e9c0SJosh Durgin int ret; 626ad32e9c0SJosh Durgin rcb->ret = rbd_aio_get_return_value(c); 627ad32e9c0SJosh Durgin rbd_aio_release(c); 628ad32e9c0SJosh Durgin ret = qemu_rbd_send_pipe(rcb->s, rcb); 629f27aaf4bSChristian Brunner if (ret < 0) { 630ad32e9c0SJosh Durgin error_report("failed writing to acb->s->fds"); 6317267c094SAnthony Liguori g_free(rcb); 632f27aaf4bSChristian Brunner } 633f27aaf4bSChristian Brunner } 634f27aaf4bSChristian Brunner 635ad32e9c0SJosh Durgin /* Callback when all queued rbd_aio requests are complete */ 636f27aaf4bSChristian Brunner 637f27aaf4bSChristian Brunner static void rbd_aio_bh_cb(void *opaque) 638f27aaf4bSChristian Brunner { 639f27aaf4bSChristian Brunner RBDAIOCB *acb = opaque; 640f27aaf4bSChristian Brunner 641787f3133SJosh Durgin if (acb->cmd == RBD_AIO_READ) { 642f27aaf4bSChristian Brunner qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); 643f27aaf4bSChristian Brunner } 644f27aaf4bSChristian Brunner qemu_vfree(acb->bounce); 645f27aaf4bSChristian Brunner acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret)); 646f27aaf4bSChristian Brunner qemu_bh_delete(acb->bh); 647f27aaf4bSChristian Brunner acb->bh = NULL; 648f27aaf4bSChristian Brunner 649f27aaf4bSChristian Brunner qemu_aio_release(acb); 650f27aaf4bSChristian Brunner } 651f27aaf4bSChristian Brunner 652787f3133SJosh Durgin static int rbd_aio_discard_wrapper(rbd_image_t image, 653787f3133SJosh Durgin uint64_t off, 654787f3133SJosh Durgin uint64_t len, 655787f3133SJosh Durgin rbd_completion_t comp) 656787f3133SJosh Durgin { 657787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 658787f3133SJosh Durgin return rbd_aio_discard(image, off, len, comp); 659787f3133SJosh Durgin #else 660787f3133SJosh Durgin return -ENOTSUP; 661787f3133SJosh Durgin #endif 662787f3133SJosh Durgin } 663787f3133SJosh Durgin 664787f3133SJosh Durgin static BlockDriverAIOCB *rbd_start_aio(BlockDriverState *bs, 665f27aaf4bSChristian Brunner int64_t sector_num, 666f27aaf4bSChristian Brunner QEMUIOVector *qiov, 667f27aaf4bSChristian Brunner int nb_sectors, 668f27aaf4bSChristian Brunner BlockDriverCompletionFunc *cb, 669787f3133SJosh Durgin void *opaque, 670787f3133SJosh Durgin RBDAIOCmd cmd) 671f27aaf4bSChristian Brunner { 672f27aaf4bSChristian Brunner RBDAIOCB *acb; 673f27aaf4bSChristian Brunner RADOSCB *rcb; 674ad32e9c0SJosh Durgin rbd_completion_t c; 675f27aaf4bSChristian Brunner int64_t off, size; 676f27aaf4bSChristian Brunner char *buf; 67751a13528SJosh Durgin int r; 678f27aaf4bSChristian Brunner 679f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 680f27aaf4bSChristian Brunner 681f27aaf4bSChristian Brunner acb = qemu_aio_get(&rbd_aio_pool, bs, cb, opaque); 682787f3133SJosh Durgin acb->cmd = cmd; 683f27aaf4bSChristian Brunner acb->qiov = qiov; 684787f3133SJosh Durgin if (cmd == RBD_AIO_DISCARD) { 685787f3133SJosh Durgin acb->bounce = NULL; 686787f3133SJosh Durgin } else { 687f27aaf4bSChristian Brunner acb->bounce = qemu_blockalign(bs, qiov->size); 688787f3133SJosh Durgin } 689f27aaf4bSChristian Brunner acb->ret = 0; 690f27aaf4bSChristian Brunner acb->error = 0; 691f27aaf4bSChristian Brunner acb->s = s; 692f27aaf4bSChristian Brunner acb->cancelled = 0; 693f27aaf4bSChristian Brunner acb->bh = NULL; 694f27aaf4bSChristian Brunner 695787f3133SJosh Durgin if (cmd == RBD_AIO_WRITE) { 696f27aaf4bSChristian Brunner qemu_iovec_to_buffer(acb->qiov, acb->bounce); 697f27aaf4bSChristian Brunner } 698f27aaf4bSChristian Brunner 699f27aaf4bSChristian Brunner buf = acb->bounce; 700f27aaf4bSChristian Brunner 701f27aaf4bSChristian Brunner off = sector_num * BDRV_SECTOR_SIZE; 702f27aaf4bSChristian Brunner size = nb_sectors * BDRV_SECTOR_SIZE; 703f27aaf4bSChristian Brunner 704ad32e9c0SJosh Durgin s->qemu_aio_count++; /* All the RADOSCB */ 705f27aaf4bSChristian Brunner 7067267c094SAnthony Liguori rcb = g_malloc(sizeof(RADOSCB)); 707f27aaf4bSChristian Brunner rcb->done = 0; 708f27aaf4bSChristian Brunner rcb->acb = acb; 709f27aaf4bSChristian Brunner rcb->buf = buf; 710f27aaf4bSChristian Brunner rcb->s = acb->s; 711ad32e9c0SJosh Durgin rcb->size = size; 71251a13528SJosh Durgin r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c); 71351a13528SJosh Durgin if (r < 0) { 71451a13528SJosh Durgin goto failed; 71551a13528SJosh Durgin } 716f27aaf4bSChristian Brunner 717787f3133SJosh Durgin switch (cmd) { 718787f3133SJosh Durgin case RBD_AIO_WRITE: 71951a13528SJosh Durgin r = rbd_aio_write(s->image, off, size, buf, c); 720787f3133SJosh Durgin break; 721787f3133SJosh Durgin case RBD_AIO_READ: 72251a13528SJosh Durgin r = rbd_aio_read(s->image, off, size, buf, c); 723787f3133SJosh Durgin break; 724787f3133SJosh Durgin case RBD_AIO_DISCARD: 725787f3133SJosh Durgin r = rbd_aio_discard_wrapper(s->image, off, size, c); 726787f3133SJosh Durgin break; 727787f3133SJosh Durgin default: 728787f3133SJosh Durgin r = -EINVAL; 72951a13528SJosh Durgin } 73051a13528SJosh Durgin 73151a13528SJosh Durgin if (r < 0) { 73251a13528SJosh Durgin goto failed; 733f27aaf4bSChristian Brunner } 734f27aaf4bSChristian Brunner 735f27aaf4bSChristian Brunner return &acb->common; 73651a13528SJosh Durgin 73751a13528SJosh Durgin failed: 7387267c094SAnthony Liguori g_free(rcb); 73951a13528SJosh Durgin s->qemu_aio_count--; 74051a13528SJosh Durgin qemu_aio_release(acb); 74151a13528SJosh Durgin return NULL; 742f27aaf4bSChristian Brunner } 743f27aaf4bSChristian Brunner 744ad32e9c0SJosh Durgin static BlockDriverAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs, 745ad32e9c0SJosh Durgin int64_t sector_num, 746ad32e9c0SJosh Durgin QEMUIOVector *qiov, 747f27aaf4bSChristian Brunner int nb_sectors, 748f27aaf4bSChristian Brunner BlockDriverCompletionFunc *cb, 749f27aaf4bSChristian Brunner void *opaque) 750f27aaf4bSChristian Brunner { 751787f3133SJosh Durgin return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque, 752787f3133SJosh Durgin RBD_AIO_READ); 753f27aaf4bSChristian Brunner } 754f27aaf4bSChristian Brunner 755ad32e9c0SJosh Durgin static BlockDriverAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs, 756ad32e9c0SJosh Durgin int64_t sector_num, 757ad32e9c0SJosh Durgin QEMUIOVector *qiov, 758f27aaf4bSChristian Brunner int nb_sectors, 759f27aaf4bSChristian Brunner BlockDriverCompletionFunc *cb, 760f27aaf4bSChristian Brunner void *opaque) 761f27aaf4bSChristian Brunner { 762787f3133SJosh Durgin return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque, 763787f3133SJosh Durgin RBD_AIO_WRITE); 764f27aaf4bSChristian Brunner } 765f27aaf4bSChristian Brunner 7668b94ff85SPaolo Bonzini static int qemu_rbd_co_flush(BlockDriverState *bs) 7677a3f5fe9SSage Weil { 7687a3f5fe9SSage Weil #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1) 7697a3f5fe9SSage Weil /* rbd_flush added in 0.1.1 */ 7707a3f5fe9SSage Weil BDRVRBDState *s = bs->opaque; 7717a3f5fe9SSage Weil return rbd_flush(s->image); 7727a3f5fe9SSage Weil #else 7737a3f5fe9SSage Weil return 0; 7747a3f5fe9SSage Weil #endif 7757a3f5fe9SSage Weil } 7767a3f5fe9SSage Weil 777ad32e9c0SJosh Durgin static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi) 778f27aaf4bSChristian Brunner { 779f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 780ad32e9c0SJosh Durgin rbd_image_info_t info; 781ad32e9c0SJosh Durgin int r; 782ad32e9c0SJosh Durgin 783ad32e9c0SJosh Durgin r = rbd_stat(s->image, &info, sizeof(info)); 784ad32e9c0SJosh Durgin if (r < 0) { 785ad32e9c0SJosh Durgin return r; 786ad32e9c0SJosh Durgin } 787ad32e9c0SJosh Durgin 788ad32e9c0SJosh Durgin bdi->cluster_size = info.obj_size; 789f27aaf4bSChristian Brunner return 0; 790f27aaf4bSChristian Brunner } 791f27aaf4bSChristian Brunner 792ad32e9c0SJosh Durgin static int64_t qemu_rbd_getlength(BlockDriverState *bs) 793f27aaf4bSChristian Brunner { 794f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 795ad32e9c0SJosh Durgin rbd_image_info_t info; 796ad32e9c0SJosh Durgin int r; 797f27aaf4bSChristian Brunner 798ad32e9c0SJosh Durgin r = rbd_stat(s->image, &info, sizeof(info)); 799ad32e9c0SJosh Durgin if (r < 0) { 800ad32e9c0SJosh Durgin return r; 801f27aaf4bSChristian Brunner } 802f27aaf4bSChristian Brunner 803ad32e9c0SJosh Durgin return info.size; 804ad32e9c0SJosh Durgin } 805ad32e9c0SJosh Durgin 80630cdc48cSJosh Durgin static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset) 80730cdc48cSJosh Durgin { 80830cdc48cSJosh Durgin BDRVRBDState *s = bs->opaque; 80930cdc48cSJosh Durgin int r; 81030cdc48cSJosh Durgin 81130cdc48cSJosh Durgin r = rbd_resize(s->image, offset); 81230cdc48cSJosh Durgin if (r < 0) { 81330cdc48cSJosh Durgin return r; 81430cdc48cSJosh Durgin } 81530cdc48cSJosh Durgin 81630cdc48cSJosh Durgin return 0; 81730cdc48cSJosh Durgin } 81830cdc48cSJosh Durgin 819ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs, 820ad32e9c0SJosh Durgin QEMUSnapshotInfo *sn_info) 821f27aaf4bSChristian Brunner { 822f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 823f27aaf4bSChristian Brunner int r; 824f27aaf4bSChristian Brunner 825f27aaf4bSChristian Brunner if (sn_info->name[0] == '\0') { 826f27aaf4bSChristian Brunner return -EINVAL; /* we need a name for rbd snapshots */ 827f27aaf4bSChristian Brunner } 828f27aaf4bSChristian Brunner 829f27aaf4bSChristian Brunner /* 830f27aaf4bSChristian Brunner * rbd snapshots are using the name as the user controlled unique identifier 831f27aaf4bSChristian Brunner * we can't use the rbd snapid for that purpose, as it can't be set 832f27aaf4bSChristian Brunner */ 833f27aaf4bSChristian Brunner if (sn_info->id_str[0] != '\0' && 834f27aaf4bSChristian Brunner strcmp(sn_info->id_str, sn_info->name) != 0) { 835f27aaf4bSChristian Brunner return -EINVAL; 836f27aaf4bSChristian Brunner } 837f27aaf4bSChristian Brunner 838f27aaf4bSChristian Brunner if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) { 839f27aaf4bSChristian Brunner return -ERANGE; 840f27aaf4bSChristian Brunner } 841f27aaf4bSChristian Brunner 842ad32e9c0SJosh Durgin r = rbd_snap_create(s->image, sn_info->name); 843f27aaf4bSChristian Brunner if (r < 0) { 844ad32e9c0SJosh Durgin error_report("failed to create snap: %s", strerror(-r)); 845f27aaf4bSChristian Brunner return r; 846f27aaf4bSChristian Brunner } 847f27aaf4bSChristian Brunner 848f27aaf4bSChristian Brunner return 0; 849f27aaf4bSChristian Brunner } 850f27aaf4bSChristian Brunner 851bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs, 852bd603247SGregory Farnum const char *snapshot_name) 853bd603247SGregory Farnum { 854bd603247SGregory Farnum BDRVRBDState *s = bs->opaque; 855bd603247SGregory Farnum int r; 856bd603247SGregory Farnum 857bd603247SGregory Farnum r = rbd_snap_remove(s->image, snapshot_name); 858bd603247SGregory Farnum return r; 859bd603247SGregory Farnum } 860bd603247SGregory Farnum 861bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs, 862bd603247SGregory Farnum const char *snapshot_name) 863bd603247SGregory Farnum { 864bd603247SGregory Farnum BDRVRBDState *s = bs->opaque; 865bd603247SGregory Farnum int r; 866bd603247SGregory Farnum 867bd603247SGregory Farnum r = rbd_snap_rollback(s->image, snapshot_name); 868bd603247SGregory Farnum return r; 869bd603247SGregory Farnum } 870bd603247SGregory Farnum 871ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs, 872ad32e9c0SJosh Durgin QEMUSnapshotInfo **psn_tab) 873f27aaf4bSChristian Brunner { 874f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque; 875f27aaf4bSChristian Brunner QEMUSnapshotInfo *sn_info, *sn_tab = NULL; 876ad32e9c0SJosh Durgin int i, snap_count; 877ad32e9c0SJosh Durgin rbd_snap_info_t *snaps; 878ad32e9c0SJosh Durgin int max_snaps = RBD_MAX_SNAPS; 879f27aaf4bSChristian Brunner 880ad32e9c0SJosh Durgin do { 8817267c094SAnthony Liguori snaps = g_malloc(sizeof(*snaps) * max_snaps); 882ad32e9c0SJosh Durgin snap_count = rbd_snap_list(s->image, snaps, &max_snaps); 883ad32e9c0SJosh Durgin if (snap_count < 0) { 8847267c094SAnthony Liguori g_free(snaps); 885f27aaf4bSChristian Brunner } 886ad32e9c0SJosh Durgin } while (snap_count == -ERANGE); 887f27aaf4bSChristian Brunner 888ad32e9c0SJosh Durgin if (snap_count <= 0) { 889b9c53290SJosh Durgin goto done; 890f27aaf4bSChristian Brunner } 891f27aaf4bSChristian Brunner 8927267c094SAnthony Liguori sn_tab = g_malloc0(snap_count * sizeof(QEMUSnapshotInfo)); 893f27aaf4bSChristian Brunner 894ad32e9c0SJosh Durgin for (i = 0; i < snap_count; i++) { 895ad32e9c0SJosh Durgin const char *snap_name = snaps[i].name; 896f27aaf4bSChristian Brunner 897f27aaf4bSChristian Brunner sn_info = sn_tab + i; 898f27aaf4bSChristian Brunner pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name); 899f27aaf4bSChristian Brunner pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name); 900f27aaf4bSChristian Brunner 901ad32e9c0SJosh Durgin sn_info->vm_state_size = snaps[i].size; 902f27aaf4bSChristian Brunner sn_info->date_sec = 0; 903f27aaf4bSChristian Brunner sn_info->date_nsec = 0; 904f27aaf4bSChristian Brunner sn_info->vm_clock_nsec = 0; 905f27aaf4bSChristian Brunner } 906ad32e9c0SJosh Durgin rbd_snap_list_end(snaps); 907ad32e9c0SJosh Durgin 908b9c53290SJosh Durgin done: 909f27aaf4bSChristian Brunner *psn_tab = sn_tab; 910f27aaf4bSChristian Brunner return snap_count; 911f27aaf4bSChristian Brunner } 912f27aaf4bSChristian Brunner 913787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 914787f3133SJosh Durgin static BlockDriverAIOCB* qemu_rbd_aio_discard(BlockDriverState *bs, 915787f3133SJosh Durgin int64_t sector_num, 916787f3133SJosh Durgin int nb_sectors, 917787f3133SJosh Durgin BlockDriverCompletionFunc *cb, 918787f3133SJosh Durgin void *opaque) 919787f3133SJosh Durgin { 920787f3133SJosh Durgin return rbd_start_aio(bs, sector_num, NULL, nb_sectors, cb, opaque, 921787f3133SJosh Durgin RBD_AIO_DISCARD); 922787f3133SJosh Durgin } 923787f3133SJosh Durgin #endif 924787f3133SJosh Durgin 925ad32e9c0SJosh Durgin static QEMUOptionParameter qemu_rbd_create_options[] = { 926f27aaf4bSChristian Brunner { 927f27aaf4bSChristian Brunner .name = BLOCK_OPT_SIZE, 928f27aaf4bSChristian Brunner .type = OPT_SIZE, 929f27aaf4bSChristian Brunner .help = "Virtual disk size" 930f27aaf4bSChristian Brunner }, 931f27aaf4bSChristian Brunner { 932f27aaf4bSChristian Brunner .name = BLOCK_OPT_CLUSTER_SIZE, 933f27aaf4bSChristian Brunner .type = OPT_SIZE, 934f27aaf4bSChristian Brunner .help = "RBD object size" 935f27aaf4bSChristian Brunner }, 936f27aaf4bSChristian Brunner {NULL} 937f27aaf4bSChristian Brunner }; 938f27aaf4bSChristian Brunner 939f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = { 940f27aaf4bSChristian Brunner .format_name = "rbd", 941f27aaf4bSChristian Brunner .instance_size = sizeof(BDRVRBDState), 942ad32e9c0SJosh Durgin .bdrv_file_open = qemu_rbd_open, 943ad32e9c0SJosh Durgin .bdrv_close = qemu_rbd_close, 944ad32e9c0SJosh Durgin .bdrv_create = qemu_rbd_create, 945ad32e9c0SJosh Durgin .bdrv_get_info = qemu_rbd_getinfo, 946ad32e9c0SJosh Durgin .create_options = qemu_rbd_create_options, 947ad32e9c0SJosh Durgin .bdrv_getlength = qemu_rbd_getlength, 94830cdc48cSJosh Durgin .bdrv_truncate = qemu_rbd_truncate, 949f27aaf4bSChristian Brunner .protocol_name = "rbd", 950f27aaf4bSChristian Brunner 951ad32e9c0SJosh Durgin .bdrv_aio_readv = qemu_rbd_aio_readv, 952ad32e9c0SJosh Durgin .bdrv_aio_writev = qemu_rbd_aio_writev, 953c68b89acSKevin Wolf .bdrv_co_flush_to_disk = qemu_rbd_co_flush, 954f27aaf4bSChristian Brunner 955787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD 956787f3133SJosh Durgin .bdrv_aio_discard = qemu_rbd_aio_discard, 957787f3133SJosh Durgin #endif 958787f3133SJosh Durgin 959ad32e9c0SJosh Durgin .bdrv_snapshot_create = qemu_rbd_snap_create, 960bd603247SGregory Farnum .bdrv_snapshot_delete = qemu_rbd_snap_remove, 961ad32e9c0SJosh Durgin .bdrv_snapshot_list = qemu_rbd_snap_list, 962bd603247SGregory Farnum .bdrv_snapshot_goto = qemu_rbd_snap_rollback, 963f27aaf4bSChristian Brunner }; 964f27aaf4bSChristian Brunner 965f27aaf4bSChristian Brunner static void bdrv_rbd_init(void) 966f27aaf4bSChristian Brunner { 967f27aaf4bSChristian Brunner bdrv_register(&bdrv_rbd); 968f27aaf4bSChristian Brunner } 969f27aaf4bSChristian Brunner 970f27aaf4bSChristian Brunner block_init(bdrv_rbd_init); 971