Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Block driver for RADOS (Ceph) |
| 3 | * |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 4 | * Copyright (C) 2010-2011 Christian Brunner <chb@muc.de>, |
| 5 | * Josh Durgin <josh.durgin@dreamhost.com> |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 6 | * |
| 7 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 8 | * the COPYING file in the top-level directory. |
| 9 | * |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 10 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 11 | * GNU GPL, version 2 or (at your option) any later version. |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 12 | */ |
| 13 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 14 | #include "qemu/osdep.h" |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 15 | |
Markus Armbruster | 2836284 | 2017-03-28 10:56:08 +0200 | [diff] [blame] | 16 | #include <rbd/librbd.h> |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 17 | #include "qapi/error.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 18 | #include "qemu/error-report.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 19 | #include "qemu/module.h" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 20 | #include "qemu/option.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 21 | #include "block/block_int.h" |
Max Reitz | 609f45e | 2018-06-14 21:14:28 +0200 | [diff] [blame] | 22 | #include "block/qdict.h" |
Daniel P. Berrange | 60390a2 | 2016-01-21 14:19:19 +0000 | [diff] [blame] | 23 | #include "crypto/secret.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 24 | #include "qemu/cutils.h" |
Pavel Dovgalyuk | e4ec5ad | 2019-09-17 14:58:19 +0300 | [diff] [blame] | 25 | #include "sysemu/replay.h" |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 26 | #include "qapi/qmp/qstring.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 27 | #include "qapi/qmp/qdict.h" |
Eric Blake | e98c696 | 2017-03-31 10:27:30 -0500 | [diff] [blame] | 28 | #include "qapi/qmp/qjson.h" |
Markus Armbruster | 47e6b29 | 2018-02-01 12:18:38 +0100 | [diff] [blame] | 29 | #include "qapi/qmp/qlist.h" |
Kevin Wolf | 4bfb274 | 2018-02-15 20:58:24 +0100 | [diff] [blame] | 30 | #include "qapi/qobject-input-visitor.h" |
| 31 | #include "qapi/qapi-visit-block-core.h" |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 32 | |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 33 | /* |
| 34 | * When specifying the image filename use: |
| 35 | * |
Josh Durgin | fab5cf5 | 2011-05-26 16:07:32 -0700 | [diff] [blame] | 36 | * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]] |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 37 | * |
Sage Weil | 9e1fbcd | 2011-09-15 14:11:10 -0700 | [diff] [blame] | 38 | * poolname must be the name of an existing rados pool. |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 39 | * |
Sage Weil | 9e1fbcd | 2011-09-15 14:11:10 -0700 | [diff] [blame] | 40 | * devicename is the name of the rbd image. |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 41 | * |
Sage Weil | 9e1fbcd | 2011-09-15 14:11:10 -0700 | [diff] [blame] | 42 | * Each option given is used to configure rados, and may be any valid |
| 43 | * Ceph option, "id", or "conf". |
Josh Durgin | fab5cf5 | 2011-05-26 16:07:32 -0700 | [diff] [blame] | 44 | * |
Sage Weil | 9e1fbcd | 2011-09-15 14:11:10 -0700 | [diff] [blame] | 45 | * The "id" option indicates what user we should authenticate as to |
| 46 | * the Ceph cluster. If it is excluded we will use the Ceph default |
| 47 | * (normally 'admin'). |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 48 | * |
Sage Weil | 9e1fbcd | 2011-09-15 14:11:10 -0700 | [diff] [blame] | 49 | * The "conf" option specifies a Ceph configuration file to read. If |
| 50 | * it is not specified, we will read from the default Ceph locations |
| 51 | * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration |
| 52 | * file, specify conf=/dev/null. |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 53 | * |
Sage Weil | 9e1fbcd | 2011-09-15 14:11:10 -0700 | [diff] [blame] | 54 | * Configuration values containing :, @, or = can be escaped with a |
| 55 | * leading "\". |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 56 | */ |
| 57 | |
Josh Durgin | 787f313 | 2012-04-30 23:16:45 -0700 | [diff] [blame] | 58 | /* rbd_aio_discard added in 0.1.2 */ |
| 59 | #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2) |
| 60 | #define LIBRBD_SUPPORTS_DISCARD |
| 61 | #else |
| 62 | #undef LIBRBD_SUPPORTS_DISCARD |
| 63 | #endif |
| 64 | |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 65 | #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER) |
| 66 | |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 67 | #define RBD_MAX_SNAPS 100 |
| 68 | |
tianqing | 1d393bd | 2017-02-21 14:50:03 +0800 | [diff] [blame] | 69 | /* The LIBRBD_SUPPORTS_IOVEC is defined in librbd.h */ |
| 70 | #ifdef LIBRBD_SUPPORTS_IOVEC |
| 71 | #define LIBRBD_USE_IOVEC 1 |
| 72 | #else |
| 73 | #define LIBRBD_USE_IOVEC 0 |
| 74 | #endif |
| 75 | |
Josh Durgin | 787f313 | 2012-04-30 23:16:45 -0700 | [diff] [blame] | 76 | typedef enum { |
| 77 | RBD_AIO_READ, |
| 78 | RBD_AIO_WRITE, |
Josh Durgin | dc7588c | 2013-03-29 13:03:23 -0700 | [diff] [blame] | 79 | RBD_AIO_DISCARD, |
| 80 | RBD_AIO_FLUSH |
Josh Durgin | 787f313 | 2012-04-30 23:16:45 -0700 | [diff] [blame] | 81 | } RBDAIOCmd; |
| 82 | |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 83 | typedef struct RBDAIOCB { |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 84 | BlockAIOCB common; |
Stefan Priebe | 08448d5 | 2012-11-20 13:44:55 +0100 | [diff] [blame] | 85 | int64_t ret; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 86 | QEMUIOVector *qiov; |
| 87 | char *bounce; |
Josh Durgin | 787f313 | 2012-04-30 23:16:45 -0700 | [diff] [blame] | 88 | RBDAIOCmd cmd; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 89 | int error; |
| 90 | struct BDRVRBDState *s; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 91 | } RBDAIOCB; |
| 92 | |
| 93 | typedef struct RADOSCB { |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 94 | RBDAIOCB *acb; |
| 95 | struct BDRVRBDState *s; |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 96 | int64_t size; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 97 | char *buf; |
Stefan Priebe | 08448d5 | 2012-11-20 13:44:55 +0100 | [diff] [blame] | 98 | int64_t ret; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 99 | } RADOSCB; |
| 100 | |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 101 | typedef struct BDRVRBDState { |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 102 | rados_t cluster; |
| 103 | rados_ioctx_t io_ctx; |
| 104 | rbd_image_t image; |
Jeff Cody | 80b61a2 | 2017-04-07 16:55:31 -0400 | [diff] [blame] | 105 | char *image_name; |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 106 | char *snap; |
Florian Florensa | 19ae9ae | 2020-01-10 12:15:13 +0100 | [diff] [blame] | 107 | char *namespace; |
Stefano Garzarella | d24f802 | 2019-05-09 16:59:27 +0200 | [diff] [blame] | 108 | uint64_t image_size; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 109 | } BDRVRBDState; |
| 110 | |
Kevin Wolf | aa045c2 | 2018-02-16 18:48:25 +0100 | [diff] [blame] | 111 | static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx, |
| 112 | BlockdevOptionsRbd *opts, bool cache, |
| 113 | const char *keypairs, const char *secretid, |
| 114 | Error **errp); |
| 115 | |
Markus Armbruster | 730b00b | 2017-03-28 10:56:01 +0200 | [diff] [blame] | 116 | static char *qemu_rbd_next_tok(char *src, char delim, char **p) |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 117 | { |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 118 | char *end; |
| 119 | |
| 120 | *p = NULL; |
| 121 | |
Markus Armbruster | 8efb339 | 2017-03-28 10:56:02 +0200 | [diff] [blame] | 122 | for (end = src; *end; ++end) { |
Sage Weil | 16a06b2 | 2011-09-19 13:35:26 -0700 | [diff] [blame] | 123 | if (*end == delim) { |
Markus Armbruster | 8efb339 | 2017-03-28 10:56:02 +0200 | [diff] [blame] | 124 | break; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 125 | } |
Markus Armbruster | 8efb339 | 2017-03-28 10:56:02 +0200 | [diff] [blame] | 126 | if (*end == '\\' && end[1] != '\0') { |
| 127 | end++; |
| 128 | } |
| 129 | } |
| 130 | if (*end == delim) { |
| 131 | *p = end + 1; |
| 132 | *end = '\0'; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 133 | } |
Jeff Cody | 7830f90 | 2017-02-24 10:30:33 -0500 | [diff] [blame] | 134 | return src; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Sage Weil | 16a06b2 | 2011-09-19 13:35:26 -0700 | [diff] [blame] | 137 | static void qemu_rbd_unescape(char *src) |
| 138 | { |
| 139 | char *p; |
| 140 | |
| 141 | for (p = src; *src; ++src, ++p) { |
| 142 | if (*src == '\\' && src[1] != '\0') { |
| 143 | src++; |
| 144 | } |
| 145 | *p = *src; |
| 146 | } |
| 147 | *p = '\0'; |
| 148 | } |
| 149 | |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 150 | static void qemu_rbd_parse_filename(const char *filename, QDict *options, |
| 151 | Error **errp) |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 152 | { |
| 153 | const char *start; |
Eric Blake | e98c696 | 2017-03-31 10:27:30 -0500 | [diff] [blame] | 154 | char *p, *buf; |
| 155 | QList *keypairs = NULL; |
Florian Florensa | 19ae9ae | 2020-01-10 12:15:13 +0100 | [diff] [blame] | 156 | char *found_str, *image_name; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 157 | |
| 158 | if (!strstart(filename, "rbd:", &start)) { |
Markus Armbruster | d61563b | 2014-05-16 11:00:11 +0200 | [diff] [blame] | 159 | error_setg(errp, "File name must start with 'rbd:'"); |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 160 | return; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 163 | buf = g_strdup(start); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 164 | p = buf; |
| 165 | |
Markus Armbruster | 730b00b | 2017-03-28 10:56:01 +0200 | [diff] [blame] | 166 | found_str = qemu_rbd_next_tok(p, '/', &p); |
Jeff Cody | 7830f90 | 2017-02-24 10:30:33 -0500 | [diff] [blame] | 167 | if (!p) { |
Jeff Cody | 7830f90 | 2017-02-24 10:30:33 -0500 | [diff] [blame] | 168 | error_setg(errp, "Pool name is required"); |
| 169 | goto done; |
| 170 | } |
| 171 | qemu_rbd_unescape(found_str); |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 172 | qdict_put_str(options, "pool", found_str); |
Josh Durgin | fab5cf5 | 2011-05-26 16:07:32 -0700 | [diff] [blame] | 173 | |
| 174 | if (strchr(p, '@')) { |
Florian Florensa | 19ae9ae | 2020-01-10 12:15:13 +0100 | [diff] [blame] | 175 | image_name = qemu_rbd_next_tok(p, '@', &p); |
Jeff Cody | 7830f90 | 2017-02-24 10:30:33 -0500 | [diff] [blame] | 176 | |
Markus Armbruster | 730b00b | 2017-03-28 10:56:01 +0200 | [diff] [blame] | 177 | found_str = qemu_rbd_next_tok(p, ':', &p); |
Jeff Cody | 7830f90 | 2017-02-24 10:30:33 -0500 | [diff] [blame] | 178 | qemu_rbd_unescape(found_str); |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 179 | qdict_put_str(options, "snapshot", found_str); |
Josh Durgin | fab5cf5 | 2011-05-26 16:07:32 -0700 | [diff] [blame] | 180 | } else { |
Florian Florensa | 19ae9ae | 2020-01-10 12:15:13 +0100 | [diff] [blame] | 181 | image_name = qemu_rbd_next_tok(p, ':', &p); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 182 | } |
Florian Florensa | 19ae9ae | 2020-01-10 12:15:13 +0100 | [diff] [blame] | 183 | /* Check for namespace in the image_name */ |
| 184 | if (strchr(image_name, '/')) { |
| 185 | found_str = qemu_rbd_next_tok(image_name, '/', &image_name); |
| 186 | qemu_rbd_unescape(found_str); |
| 187 | qdict_put_str(options, "namespace", found_str); |
| 188 | } else { |
| 189 | qdict_put_str(options, "namespace", ""); |
| 190 | } |
| 191 | qemu_rbd_unescape(image_name); |
| 192 | qdict_put_str(options, "image", image_name); |
Jeff Cody | 7830f90 | 2017-02-24 10:30:33 -0500 | [diff] [blame] | 193 | if (!p) { |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 194 | goto done; |
| 195 | } |
| 196 | |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 197 | /* The following are essentially all key/value pairs, and we treat |
| 198 | * 'id' and 'conf' a bit special. Key/value pairs may be in any order. */ |
| 199 | while (p) { |
| 200 | char *name, *value; |
Markus Armbruster | 730b00b | 2017-03-28 10:56:01 +0200 | [diff] [blame] | 201 | name = qemu_rbd_next_tok(p, '=', &p); |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 202 | if (!p) { |
| 203 | error_setg(errp, "conf option %s has no value", name); |
| 204 | break; |
| 205 | } |
| 206 | |
| 207 | qemu_rbd_unescape(name); |
| 208 | |
Markus Armbruster | 730b00b | 2017-03-28 10:56:01 +0200 | [diff] [blame] | 209 | value = qemu_rbd_next_tok(p, ':', &p); |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 210 | qemu_rbd_unescape(value); |
| 211 | |
| 212 | if (!strcmp(name, "conf")) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 213 | qdict_put_str(options, "conf", value); |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 214 | } else if (!strcmp(name, "id")) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 215 | qdict_put_str(options, "user", value); |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 216 | } else { |
Eric Blake | e98c696 | 2017-03-31 10:27:30 -0500 | [diff] [blame] | 217 | /* |
| 218 | * We pass these internally to qemu_rbd_set_keypairs(), so |
| 219 | * we can get away with the simpler list of [ "key1", |
| 220 | * "value1", "key2", "value2" ] rather than a raw dict |
| 221 | * { "key1": "value1", "key2": "value2" } where we can't |
| 222 | * guarantee order, or even a more correct but complex |
| 223 | * [ { "key1": "value1" }, { "key2": "value2" } ] |
| 224 | */ |
| 225 | if (!keypairs) { |
| 226 | keypairs = qlist_new(); |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 227 | } |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 228 | qlist_append_str(keypairs, name); |
| 229 | qlist_append_str(keypairs, value); |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Eric Blake | e98c696 | 2017-03-31 10:27:30 -0500 | [diff] [blame] | 233 | if (keypairs) { |
| 234 | qdict_put(options, "=keyvalue-pairs", |
| 235 | qobject_to_json(QOBJECT(keypairs))); |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 236 | } |
| 237 | |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 238 | done: |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 239 | g_free(buf); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 240 | qobject_unref(keypairs); |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 241 | return; |
Sage Weil | 7c7e9df | 2011-09-07 09:28:04 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Daniel P. Berrange | 60390a2 | 2016-01-21 14:19:19 +0000 | [diff] [blame] | 244 | |
Eric Blake | e8e16d4 | 2018-04-24 14:25:04 -0500 | [diff] [blame] | 245 | static void qemu_rbd_refresh_limits(BlockDriverState *bs, Error **errp) |
| 246 | { |
| 247 | /* XXX Does RBD support AIO on less than 512-byte alignment? */ |
| 248 | bs->bl.request_alignment = 512; |
| 249 | } |
| 250 | |
| 251 | |
Markus Armbruster | d083f95 | 2018-06-14 21:14:43 +0200 | [diff] [blame] | 252 | static int qemu_rbd_set_auth(rados_t cluster, BlockdevOptionsRbd *opts, |
Daniel P. Berrange | 60390a2 | 2016-01-21 14:19:19 +0000 | [diff] [blame] | 253 | Error **errp) |
| 254 | { |
Markus Armbruster | d083f95 | 2018-06-14 21:14:43 +0200 | [diff] [blame] | 255 | char *key, *acr; |
Markus Armbruster | a3699de | 2018-06-14 21:14:42 +0200 | [diff] [blame] | 256 | int r; |
| 257 | GString *accu; |
| 258 | RbdAuthModeList *auth; |
| 259 | |
Markus Armbruster | d083f95 | 2018-06-14 21:14:43 +0200 | [diff] [blame] | 260 | if (opts->key_secret) { |
| 261 | key = qcrypto_secret_lookup_as_base64(opts->key_secret, errp); |
| 262 | if (!key) { |
| 263 | return -EIO; |
Markus Armbruster | a3699de | 2018-06-14 21:14:42 +0200 | [diff] [blame] | 264 | } |
Markus Armbruster | d083f95 | 2018-06-14 21:14:43 +0200 | [diff] [blame] | 265 | r = rados_conf_set(cluster, "key", key); |
| 266 | g_free(key); |
| 267 | if (r < 0) { |
| 268 | error_setg_errno(errp, -r, "Could not set 'key'"); |
| 269 | return r; |
| 270 | } |
Daniel P. Berrange | 60390a2 | 2016-01-21 14:19:19 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Markus Armbruster | a3699de | 2018-06-14 21:14:42 +0200 | [diff] [blame] | 273 | if (opts->has_auth_client_required) { |
| 274 | accu = g_string_new(""); |
| 275 | for (auth = opts->auth_client_required; auth; auth = auth->next) { |
| 276 | if (accu->str[0]) { |
| 277 | g_string_append_c(accu, ';'); |
| 278 | } |
| 279 | g_string_append(accu, RbdAuthMode_str(auth->value)); |
| 280 | } |
| 281 | acr = g_string_free(accu, FALSE); |
| 282 | r = rados_conf_set(cluster, "auth_client_required", acr); |
| 283 | g_free(acr); |
| 284 | if (r < 0) { |
| 285 | error_setg_errno(errp, -r, |
| 286 | "Could not set 'auth_client_required'"); |
| 287 | return r; |
| 288 | } |
Daniel P. Berrange | 60390a2 | 2016-01-21 14:19:19 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Daniel P. Berrange | 60390a2 | 2016-01-21 14:19:19 +0000 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | |
Eric Blake | e98c696 | 2017-03-31 10:27:30 -0500 | [diff] [blame] | 294 | static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json, |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 295 | Error **errp) |
Josh Durgin | fab5cf5 | 2011-05-26 16:07:32 -0700 | [diff] [blame] | 296 | { |
Eric Blake | e98c696 | 2017-03-31 10:27:30 -0500 | [diff] [blame] | 297 | QList *keypairs; |
| 298 | QString *name; |
| 299 | QString *value; |
| 300 | const char *key; |
| 301 | size_t remaining; |
Josh Durgin | fab5cf5 | 2011-05-26 16:07:32 -0700 | [diff] [blame] | 302 | int ret = 0; |
| 303 | |
Eric Blake | e98c696 | 2017-03-31 10:27:30 -0500 | [diff] [blame] | 304 | if (!keypairs_json) { |
| 305 | return ret; |
| 306 | } |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 307 | keypairs = qobject_to(QList, |
| 308 | qobject_from_json(keypairs_json, &error_abort)); |
Eric Blake | e98c696 | 2017-03-31 10:27:30 -0500 | [diff] [blame] | 309 | remaining = qlist_size(keypairs) / 2; |
| 310 | assert(remaining); |
Josh Durgin | fab5cf5 | 2011-05-26 16:07:32 -0700 | [diff] [blame] | 311 | |
Eric Blake | e98c696 | 2017-03-31 10:27:30 -0500 | [diff] [blame] | 312 | while (remaining--) { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 313 | name = qobject_to(QString, qlist_pop(keypairs)); |
| 314 | value = qobject_to(QString, qlist_pop(keypairs)); |
Eric Blake | e98c696 | 2017-03-31 10:27:30 -0500 | [diff] [blame] | 315 | assert(name && value); |
| 316 | key = qstring_get_str(name); |
Josh Durgin | fab5cf5 | 2011-05-26 16:07:32 -0700 | [diff] [blame] | 317 | |
Eric Blake | e98c696 | 2017-03-31 10:27:30 -0500 | [diff] [blame] | 318 | ret = rados_conf_set(cluster, key, qstring_get_str(value)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 319 | qobject_unref(value); |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 320 | if (ret < 0) { |
Eric Blake | e98c696 | 2017-03-31 10:27:30 -0500 | [diff] [blame] | 321 | error_setg_errno(errp, -ret, "invalid conf option %s", key); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 322 | qobject_unref(name); |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 323 | ret = -EINVAL; |
| 324 | break; |
Josh Durgin | fab5cf5 | 2011-05-26 16:07:32 -0700 | [diff] [blame] | 325 | } |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 326 | qobject_unref(name); |
Josh Durgin | fab5cf5 | 2011-05-26 16:07:32 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 329 | qobject_unref(keypairs); |
Josh Durgin | fab5cf5 | 2011-05-26 16:07:32 -0700 | [diff] [blame] | 330 | return ret; |
| 331 | } |
| 332 | |
tianqing | 1d393bd | 2017-02-21 14:50:03 +0800 | [diff] [blame] | 333 | static void qemu_rbd_memset(RADOSCB *rcb, int64_t offs) |
| 334 | { |
| 335 | if (LIBRBD_USE_IOVEC) { |
| 336 | RBDAIOCB *acb = rcb->acb; |
| 337 | iov_memset(acb->qiov->iov, acb->qiov->niov, offs, 0, |
| 338 | acb->qiov->size - offs); |
| 339 | } else { |
| 340 | memset(rcb->buf + offs, 0, rcb->size - offs); |
| 341 | } |
| 342 | } |
| 343 | |
Markus Armbruster | d083f95 | 2018-06-14 21:14:43 +0200 | [diff] [blame] | 344 | /* FIXME Deprecate and remove keypairs or make it available in QMP. */ |
Kevin Wolf | 1bebea3 | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 345 | static int qemu_rbd_do_create(BlockdevCreateOptions *options, |
| 346 | const char *keypairs, const char *password_secret, |
| 347 | Error **errp) |
| 348 | { |
| 349 | BlockdevCreateOptionsRbd *opts = &options->u.rbd; |
| 350 | rados_t cluster; |
| 351 | rados_ioctx_t io_ctx; |
| 352 | int obj_order = 0; |
| 353 | int ret; |
| 354 | |
| 355 | assert(options->driver == BLOCKDEV_DRIVER_RBD); |
| 356 | if (opts->location->has_snapshot) { |
| 357 | error_setg(errp, "Can't use snapshot name for image creation"); |
| 358 | return -EINVAL; |
| 359 | } |
| 360 | |
Kevin Wolf | 1bebea3 | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 361 | if (opts->has_cluster_size) { |
| 362 | int64_t objsize = opts->cluster_size; |
| 363 | if ((objsize - 1) & objsize) { /* not a power of 2? */ |
| 364 | error_setg(errp, "obj size needs to be power of 2"); |
| 365 | return -EINVAL; |
| 366 | } |
| 367 | if (objsize < 4096) { |
| 368 | error_setg(errp, "obj size too small"); |
| 369 | return -EINVAL; |
| 370 | } |
| 371 | obj_order = ctz32(objsize); |
| 372 | } |
| 373 | |
Kevin Wolf | aa045c2 | 2018-02-16 18:48:25 +0100 | [diff] [blame] | 374 | ret = qemu_rbd_connect(&cluster, &io_ctx, opts->location, false, keypairs, |
| 375 | password_secret, errp); |
Kevin Wolf | 1bebea3 | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 376 | if (ret < 0) { |
Kevin Wolf | 1bebea3 | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 377 | return ret; |
| 378 | } |
| 379 | |
Kevin Wolf | 1bebea3 | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 380 | ret = rbd_create(io_ctx, opts->location->image, opts->size, &obj_order); |
| 381 | if (ret < 0) { |
| 382 | error_setg_errno(errp, -ret, "error rbd create"); |
Kevin Wolf | aa045c2 | 2018-02-16 18:48:25 +0100 | [diff] [blame] | 383 | goto out; |
Kevin Wolf | 1bebea3 | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 384 | } |
| 385 | |
Kevin Wolf | 1bebea3 | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 386 | ret = 0; |
Kevin Wolf | aa045c2 | 2018-02-16 18:48:25 +0100 | [diff] [blame] | 387 | out: |
| 388 | rados_ioctx_destroy(io_ctx); |
Kevin Wolf | 1bebea3 | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 389 | rados_shutdown(cluster); |
| 390 | return ret; |
| 391 | } |
| 392 | |
| 393 | static int qemu_rbd_co_create(BlockdevCreateOptions *options, Error **errp) |
| 394 | { |
| 395 | return qemu_rbd_do_create(options, NULL, NULL, errp); |
| 396 | } |
| 397 | |
Maxim Levitsky | b92902d | 2020-03-26 03:12:17 +0200 | [diff] [blame] | 398 | static int coroutine_fn qemu_rbd_co_create_opts(BlockDriver *drv, |
| 399 | const char *filename, |
Stefan Hajnoczi | efc75e2 | 2018-01-18 13:43:45 +0100 | [diff] [blame] | 400 | QemuOpts *opts, |
| 401 | Error **errp) |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 402 | { |
Kevin Wolf | 1bebea3 | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 403 | BlockdevCreateOptions *create_options; |
| 404 | BlockdevCreateOptionsRbd *rbd_opts; |
| 405 | BlockdevOptionsRbd *loc; |
Markus Armbruster | d61563b | 2014-05-16 11:00:11 +0200 | [diff] [blame] | 406 | Error *local_err = NULL; |
Kevin Wolf | 1bebea3 | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 407 | const char *keypairs, *password_secret; |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 408 | QDict *options = NULL; |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 409 | int ret = 0; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 410 | |
Kevin Wolf | 1bebea3 | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 411 | create_options = g_new0(BlockdevCreateOptions, 1); |
| 412 | create_options->driver = BLOCKDEV_DRIVER_RBD; |
| 413 | rbd_opts = &create_options->u.rbd; |
| 414 | |
| 415 | rbd_opts->location = g_new0(BlockdevOptionsRbd, 1); |
| 416 | |
| 417 | password_secret = qemu_opt_get(opts, "password-secret"); |
Daniel P. Berrange | 60390a2 | 2016-01-21 14:19:19 +0000 | [diff] [blame] | 418 | |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 419 | /* Read out options */ |
Kevin Wolf | 1bebea3 | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 420 | rbd_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
| 421 | BDRV_SECTOR_SIZE); |
| 422 | rbd_opts->cluster_size = qemu_opt_get_size_del(opts, |
| 423 | BLOCK_OPT_CLUSTER_SIZE, 0); |
| 424 | rbd_opts->has_cluster_size = (rbd_opts->cluster_size != 0); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 425 | |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 426 | options = qdict_new(); |
| 427 | qemu_rbd_parse_filename(filename, options, &local_err); |
| 428 | if (local_err) { |
| 429 | ret = -EINVAL; |
| 430 | error_propagate(errp, local_err); |
| 431 | goto exit; |
| 432 | } |
| 433 | |
Markus Armbruster | 129c7d1 | 2017-03-30 19:43:12 +0200 | [diff] [blame] | 434 | /* |
| 435 | * Caution: while qdict_get_try_str() is fine, getting non-string |
| 436 | * types would require more care. When @options come from -blockdev |
| 437 | * or blockdev_add, its members are typed according to the QAPI |
| 438 | * schema, but when they come from -drive, they're all QString. |
| 439 | */ |
Kevin Wolf | 1bebea3 | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 440 | loc = rbd_opts->location; |
Florian Florensa | 19ae9ae | 2020-01-10 12:15:13 +0100 | [diff] [blame] | 441 | loc->pool = g_strdup(qdict_get_try_str(options, "pool")); |
| 442 | loc->conf = g_strdup(qdict_get_try_str(options, "conf")); |
| 443 | loc->has_conf = !!loc->conf; |
| 444 | loc->user = g_strdup(qdict_get_try_str(options, "user")); |
| 445 | loc->has_user = !!loc->user; |
| 446 | loc->q_namespace = g_strdup(qdict_get_try_str(options, "namespace")); |
| 447 | loc->image = g_strdup(qdict_get_try_str(options, "image")); |
| 448 | keypairs = qdict_get_try_str(options, "=keyvalue-pairs"); |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 449 | |
Kevin Wolf | 1bebea3 | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 450 | ret = qemu_rbd_do_create(create_options, keypairs, password_secret, errp); |
Vikhyat Umrao | 87cd3d2 | 2016-05-09 13:21:59 +0530 | [diff] [blame] | 451 | if (ret < 0) { |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 452 | goto exit; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 453 | } |
| 454 | |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 455 | exit: |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 456 | qobject_unref(options); |
Kevin Wolf | 1bebea3 | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 457 | qapi_free_BlockdevCreateOptions(create_options); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 458 | return ret; |
| 459 | } |
| 460 | |
| 461 | /* |
Stefan Hajnoczi | e04fb07 | 2013-12-05 16:38:33 +0100 | [diff] [blame] | 462 | * This aio completion is being called from rbd_finish_bh() and runs in qemu |
| 463 | * BH context. |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 464 | */ |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 465 | static void qemu_rbd_complete_aio(RADOSCB *rcb) |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 466 | { |
| 467 | RBDAIOCB *acb = rcb->acb; |
| 468 | int64_t r; |
| 469 | |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 470 | r = rcb->ret; |
| 471 | |
Josh Durgin | dc7588c | 2013-03-29 13:03:23 -0700 | [diff] [blame] | 472 | if (acb->cmd != RBD_AIO_READ) { |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 473 | if (r < 0) { |
| 474 | acb->ret = r; |
| 475 | acb->error = 1; |
| 476 | } else if (!acb->error) { |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 477 | acb->ret = rcb->size; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 478 | } |
| 479 | } else { |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 480 | if (r < 0) { |
tianqing | 1d393bd | 2017-02-21 14:50:03 +0800 | [diff] [blame] | 481 | qemu_rbd_memset(rcb, 0); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 482 | acb->ret = r; |
| 483 | acb->error = 1; |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 484 | } else if (r < rcb->size) { |
tianqing | 1d393bd | 2017-02-21 14:50:03 +0800 | [diff] [blame] | 485 | qemu_rbd_memset(rcb, r); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 486 | if (!acb->error) { |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 487 | acb->ret = rcb->size; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 488 | } |
| 489 | } else if (!acb->error) { |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 490 | acb->ret = r; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 491 | } |
| 492 | } |
Stefan Hajnoczi | e04fb07 | 2013-12-05 16:38:33 +0100 | [diff] [blame] | 493 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 494 | g_free(rcb); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 495 | |
tianqing | 1d393bd | 2017-02-21 14:50:03 +0800 | [diff] [blame] | 496 | if (!LIBRBD_USE_IOVEC) { |
| 497 | if (acb->cmd == RBD_AIO_READ) { |
| 498 | qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); |
| 499 | } |
| 500 | qemu_vfree(acb->bounce); |
Stefan Hajnoczi | e04fb07 | 2013-12-05 16:38:33 +0100 | [diff] [blame] | 501 | } |
tianqing | 1d393bd | 2017-02-21 14:50:03 +0800 | [diff] [blame] | 502 | |
Stefan Hajnoczi | e04fb07 | 2013-12-05 16:38:33 +0100 | [diff] [blame] | 503 | acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret)); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 504 | |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 505 | qemu_aio_unref(acb); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 506 | } |
| 507 | |
Kevin Wolf | 4bfb274 | 2018-02-15 20:58:24 +0100 | [diff] [blame] | 508 | static char *qemu_rbd_mon_host(BlockdevOptionsRbd *opts, Error **errp) |
Jeff Cody | 0a55679 | 2017-02-27 12:36:46 -0500 | [diff] [blame] | 509 | { |
Kevin Wolf | 4bfb274 | 2018-02-15 20:58:24 +0100 | [diff] [blame] | 510 | const char **vals; |
Markus Armbruster | 2836284 | 2017-03-28 10:56:08 +0200 | [diff] [blame] | 511 | const char *host, *port; |
| 512 | char *rados_str; |
Kevin Wolf | 4bfb274 | 2018-02-15 20:58:24 +0100 | [diff] [blame] | 513 | InetSocketAddressBaseList *p; |
| 514 | int i, cnt; |
Jeff Cody | 0a55679 | 2017-02-27 12:36:46 -0500 | [diff] [blame] | 515 | |
Kevin Wolf | 4bfb274 | 2018-02-15 20:58:24 +0100 | [diff] [blame] | 516 | if (!opts->has_server) { |
| 517 | return NULL; |
| 518 | } |
| 519 | |
| 520 | for (cnt = 0, p = opts->server; p; p = p->next) { |
| 521 | cnt++; |
| 522 | } |
| 523 | |
| 524 | vals = g_new(const char *, cnt + 1); |
| 525 | |
| 526 | for (i = 0, p = opts->server; p; p = p->next, i++) { |
| 527 | host = p->value->host; |
| 528 | port = p->value->port; |
Jeff Cody | 0a55679 | 2017-02-27 12:36:46 -0500 | [diff] [blame] | 529 | |
Markus Armbruster | 2836284 | 2017-03-28 10:56:08 +0200 | [diff] [blame] | 530 | if (strchr(host, ':')) { |
Kevin Wolf | 4bfb274 | 2018-02-15 20:58:24 +0100 | [diff] [blame] | 531 | vals[i] = g_strdup_printf("[%s]:%s", host, port); |
Jeff Cody | 0a55679 | 2017-02-27 12:36:46 -0500 | [diff] [blame] | 532 | } else { |
Kevin Wolf | 4bfb274 | 2018-02-15 20:58:24 +0100 | [diff] [blame] | 533 | vals[i] = g_strdup_printf("%s:%s", host, port); |
Jeff Cody | 0a55679 | 2017-02-27 12:36:46 -0500 | [diff] [blame] | 534 | } |
Jeff Cody | 0a55679 | 2017-02-27 12:36:46 -0500 | [diff] [blame] | 535 | } |
Markus Armbruster | 2836284 | 2017-03-28 10:56:08 +0200 | [diff] [blame] | 536 | vals[i] = NULL; |
Jeff Cody | 0a55679 | 2017-02-27 12:36:46 -0500 | [diff] [blame] | 537 | |
Markus Armbruster | 2836284 | 2017-03-28 10:56:08 +0200 | [diff] [blame] | 538 | rados_str = i ? g_strjoinv(";", (char **)vals) : NULL; |
Markus Armbruster | 2836284 | 2017-03-28 10:56:08 +0200 | [diff] [blame] | 539 | g_strfreev((char **)vals); |
Jeff Cody | 0a55679 | 2017-02-27 12:36:46 -0500 | [diff] [blame] | 540 | return rados_str; |
| 541 | } |
| 542 | |
Kevin Wolf | 3d9136f | 2018-02-15 19:13:47 +0100 | [diff] [blame] | 543 | static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx, |
Kevin Wolf | 4bfb274 | 2018-02-15 20:58:24 +0100 | [diff] [blame] | 544 | BlockdevOptionsRbd *opts, bool cache, |
Kevin Wolf | 4ff4504 | 2018-02-15 20:31:04 +0100 | [diff] [blame] | 545 | const char *keypairs, const char *secretid, |
| 546 | Error **errp) |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 547 | { |
Jeff Cody | 0a55679 | 2017-02-27 12:36:46 -0500 | [diff] [blame] | 548 | char *mon_host = NULL; |
Kevin Wolf | 3d9136f | 2018-02-15 19:13:47 +0100 | [diff] [blame] | 549 | Error *local_err = NULL; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 550 | int r; |
| 551 | |
Markus Armbruster | d083f95 | 2018-06-14 21:14:43 +0200 | [diff] [blame] | 552 | if (secretid) { |
| 553 | if (opts->key_secret) { |
| 554 | error_setg(errp, |
| 555 | "Legacy 'password-secret' clashes with 'key-secret'"); |
| 556 | return -EINVAL; |
| 557 | } |
| 558 | opts->key_secret = g_strdup(secretid); |
| 559 | opts->has_key_secret = true; |
| 560 | } |
| 561 | |
Kevin Wolf | 4bfb274 | 2018-02-15 20:58:24 +0100 | [diff] [blame] | 562 | mon_host = qemu_rbd_mon_host(opts, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 563 | if (local_err) { |
Markus Armbruster | d61563b | 2014-05-16 11:00:11 +0200 | [diff] [blame] | 564 | error_propagate(errp, local_err); |
Markus Armbruster | 2836284 | 2017-03-28 10:56:08 +0200 | [diff] [blame] | 565 | r = -EINVAL; |
| 566 | goto failed_opts; |
Kevin Wolf | a9ccedc | 2013-04-12 18:05:35 +0200 | [diff] [blame] | 567 | } |
| 568 | |
Kevin Wolf | 4bfb274 | 2018-02-15 20:58:24 +0100 | [diff] [blame] | 569 | r = rados_create(cluster, opts->user); |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 570 | if (r < 0) { |
Vikhyat Umrao | 87cd3d2 | 2016-05-09 13:21:59 +0530 | [diff] [blame] | 571 | error_setg_errno(errp, -r, "error initializing"); |
Kevin Wolf | c3ca988 | 2013-04-25 15:59:27 +0200 | [diff] [blame] | 572 | goto failed_opts; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 573 | } |
| 574 | |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 575 | /* try default location when conf=NULL, but ignore failure */ |
Kevin Wolf | 4bfb274 | 2018-02-15 20:58:24 +0100 | [diff] [blame] | 576 | r = rados_conf_read_file(*cluster, opts->conf); |
| 577 | if (opts->has_conf && r < 0) { |
| 578 | error_setg_errno(errp, -r, "error reading conf file %s", opts->conf); |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 579 | goto failed_shutdown; |
Josh Durgin | 99a3c89 | 2015-06-10 20:28:45 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Kevin Wolf | 3d9136f | 2018-02-15 19:13:47 +0100 | [diff] [blame] | 582 | r = qemu_rbd_set_keypairs(*cluster, keypairs, errp); |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 583 | if (r < 0) { |
| 584 | goto failed_shutdown; |
Josh Durgin | 99a3c89 | 2015-06-10 20:28:45 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Jeff Cody | 0a55679 | 2017-02-27 12:36:46 -0500 | [diff] [blame] | 587 | if (mon_host) { |
Kevin Wolf | 3d9136f | 2018-02-15 19:13:47 +0100 | [diff] [blame] | 588 | r = rados_conf_set(*cluster, "mon_host", mon_host); |
Jeff Cody | 0a55679 | 2017-02-27 12:36:46 -0500 | [diff] [blame] | 589 | if (r < 0) { |
| 590 | goto failed_shutdown; |
| 591 | } |
| 592 | } |
| 593 | |
Markus Armbruster | d083f95 | 2018-06-14 21:14:43 +0200 | [diff] [blame] | 594 | r = qemu_rbd_set_auth(*cluster, opts, errp); |
| 595 | if (r < 0) { |
Daniel P. Berrange | 60390a2 | 2016-01-21 14:19:19 +0000 | [diff] [blame] | 596 | goto failed_shutdown; |
| 597 | } |
| 598 | |
Josh Durgin | b11f38f | 2012-05-17 13:42:29 -0700 | [diff] [blame] | 599 | /* |
| 600 | * Fallback to more conservative semantics if setting cache |
| 601 | * options fails. Ignore errors from setting rbd_cache because the |
| 602 | * only possible error is that the option does not exist, and |
| 603 | * librbd defaults to no caching. If write through caching cannot |
| 604 | * be set up, fall back to no caching. |
| 605 | */ |
Kevin Wolf | 3d9136f | 2018-02-15 19:13:47 +0100 | [diff] [blame] | 606 | if (cache) { |
| 607 | rados_conf_set(*cluster, "rbd_cache", "true"); |
Josh Durgin | b11f38f | 2012-05-17 13:42:29 -0700 | [diff] [blame] | 608 | } else { |
Kevin Wolf | 3d9136f | 2018-02-15 19:13:47 +0100 | [diff] [blame] | 609 | rados_conf_set(*cluster, "rbd_cache", "false"); |
Josh Durgin | b11f38f | 2012-05-17 13:42:29 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Kevin Wolf | 3d9136f | 2018-02-15 19:13:47 +0100 | [diff] [blame] | 612 | r = rados_connect(*cluster); |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 613 | if (r < 0) { |
Vikhyat Umrao | 87cd3d2 | 2016-05-09 13:21:59 +0530 | [diff] [blame] | 614 | error_setg_errno(errp, -r, "error connecting"); |
Sage Weil | eb93d5d | 2011-09-07 09:28:06 -0700 | [diff] [blame] | 615 | goto failed_shutdown; |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Kevin Wolf | 4bfb274 | 2018-02-15 20:58:24 +0100 | [diff] [blame] | 618 | r = rados_ioctx_create(*cluster, opts->pool, io_ctx); |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 619 | if (r < 0) { |
Kevin Wolf | 4bfb274 | 2018-02-15 20:58:24 +0100 | [diff] [blame] | 620 | error_setg_errno(errp, -r, "error opening pool %s", opts->pool); |
Sage Weil | eb93d5d | 2011-09-07 09:28:06 -0700 | [diff] [blame] | 621 | goto failed_shutdown; |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 622 | } |
Florian Florensa | 19ae9ae | 2020-01-10 12:15:13 +0100 | [diff] [blame] | 623 | /* |
| 624 | * Set the namespace after opening the io context on the pool, |
| 625 | * if nspace == NULL or if nspace == "", it is just as we did nothing |
| 626 | */ |
| 627 | rados_ioctx_set_namespace(*io_ctx, opts->q_namespace); |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 628 | |
Kevin Wolf | 3d9136f | 2018-02-15 19:13:47 +0100 | [diff] [blame] | 629 | return 0; |
| 630 | |
| 631 | failed_shutdown: |
| 632 | rados_shutdown(*cluster); |
Kevin Wolf | 3d9136f | 2018-02-15 19:13:47 +0100 | [diff] [blame] | 633 | failed_opts: |
Kevin Wolf | 3d9136f | 2018-02-15 19:13:47 +0100 | [diff] [blame] | 634 | g_free(mon_host); |
| 635 | return r; |
| 636 | } |
| 637 | |
Jeff Cody | f24b03b | 2018-09-11 18:32:30 -0400 | [diff] [blame] | 638 | static int qemu_rbd_convert_options(QDict *options, BlockdevOptionsRbd **opts, |
| 639 | Error **errp) |
| 640 | { |
| 641 | Visitor *v; |
Jeff Cody | f24b03b | 2018-09-11 18:32:30 -0400 | [diff] [blame] | 642 | |
| 643 | /* Convert the remaining options into a QAPI object */ |
| 644 | v = qobject_input_visitor_new_flat_confused(options, errp); |
| 645 | if (!v) { |
| 646 | return -EINVAL; |
| 647 | } |
| 648 | |
Markus Armbruster | b11a093 | 2020-07-07 18:06:07 +0200 | [diff] [blame] | 649 | visit_type_BlockdevOptionsRbd(v, NULL, opts, errp); |
Jeff Cody | f24b03b | 2018-09-11 18:32:30 -0400 | [diff] [blame] | 650 | visit_free(v); |
Markus Armbruster | b11a093 | 2020-07-07 18:06:07 +0200 | [diff] [blame] | 651 | if (!opts) { |
Jeff Cody | f24b03b | 2018-09-11 18:32:30 -0400 | [diff] [blame] | 652 | return -EINVAL; |
| 653 | } |
| 654 | |
| 655 | return 0; |
| 656 | } |
| 657 | |
Jeff Cody | 084d1d1 | 2018-09-11 18:32:31 -0400 | [diff] [blame] | 658 | static int qemu_rbd_attempt_legacy_options(QDict *options, |
| 659 | BlockdevOptionsRbd **opts, |
| 660 | char **keypairs) |
| 661 | { |
| 662 | char *filename; |
| 663 | int r; |
| 664 | |
| 665 | filename = g_strdup(qdict_get_try_str(options, "filename")); |
| 666 | if (!filename) { |
| 667 | return -EINVAL; |
| 668 | } |
| 669 | qdict_del(options, "filename"); |
| 670 | |
| 671 | qemu_rbd_parse_filename(filename, options, NULL); |
| 672 | |
| 673 | /* keypairs freed by caller */ |
| 674 | *keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs")); |
| 675 | if (*keypairs) { |
| 676 | qdict_del(options, "=keyvalue-pairs"); |
| 677 | } |
| 678 | |
| 679 | r = qemu_rbd_convert_options(options, opts, NULL); |
| 680 | |
| 681 | g_free(filename); |
| 682 | return r; |
| 683 | } |
| 684 | |
Kevin Wolf | 3d9136f | 2018-02-15 19:13:47 +0100 | [diff] [blame] | 685 | static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags, |
| 686 | Error **errp) |
| 687 | { |
| 688 | BDRVRBDState *s = bs->opaque; |
Kevin Wolf | 4bfb274 | 2018-02-15 20:58:24 +0100 | [diff] [blame] | 689 | BlockdevOptionsRbd *opts = NULL; |
Jeff Cody | bfb15b4 | 2018-04-04 11:40:45 -0400 | [diff] [blame] | 690 | const QDictEntry *e; |
Kevin Wolf | 3d9136f | 2018-02-15 19:13:47 +0100 | [diff] [blame] | 691 | Error *local_err = NULL; |
Kevin Wolf | 4ff4504 | 2018-02-15 20:31:04 +0100 | [diff] [blame] | 692 | char *keypairs, *secretid; |
Kevin Wolf | 3d9136f | 2018-02-15 19:13:47 +0100 | [diff] [blame] | 693 | int r; |
| 694 | |
Kevin Wolf | 4ff4504 | 2018-02-15 20:31:04 +0100 | [diff] [blame] | 695 | keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs")); |
| 696 | if (keypairs) { |
| 697 | qdict_del(options, "=keyvalue-pairs"); |
| 698 | } |
| 699 | |
| 700 | secretid = g_strdup(qdict_get_try_str(options, "password-secret")); |
| 701 | if (secretid) { |
| 702 | qdict_del(options, "password-secret"); |
| 703 | } |
| 704 | |
Jeff Cody | f24b03b | 2018-09-11 18:32:30 -0400 | [diff] [blame] | 705 | r = qemu_rbd_convert_options(options, &opts, &local_err); |
Kevin Wolf | 4bfb274 | 2018-02-15 20:58:24 +0100 | [diff] [blame] | 706 | if (local_err) { |
Jeff Cody | 084d1d1 | 2018-09-11 18:32:31 -0400 | [diff] [blame] | 707 | /* If keypairs are present, that means some options are present in |
| 708 | * the modern option format. Don't attempt to parse legacy option |
| 709 | * formats, as we won't support mixed usage. */ |
| 710 | if (keypairs) { |
| 711 | error_propagate(errp, local_err); |
| 712 | goto out; |
| 713 | } |
| 714 | |
| 715 | /* If the initial attempt to convert and process the options failed, |
| 716 | * we may be attempting to open an image file that has the rbd options |
| 717 | * specified in the older format consisting of all key/value pairs |
| 718 | * encoded in the filename. Go ahead and attempt to parse the |
| 719 | * filename, and see if we can pull out the required options. */ |
| 720 | r = qemu_rbd_attempt_legacy_options(options, &opts, &keypairs); |
| 721 | if (r < 0) { |
| 722 | /* Propagate the original error, not the legacy parsing fallback |
| 723 | * error, as the latter was just a best-effort attempt. */ |
| 724 | error_propagate(errp, local_err); |
| 725 | goto out; |
| 726 | } |
| 727 | /* Take care whenever deciding to actually deprecate; once this ability |
| 728 | * is removed, we will not be able to open any images with legacy-styled |
| 729 | * backing image strings. */ |
Markus Armbruster | 5197f44 | 2018-10-17 10:26:27 +0200 | [diff] [blame] | 730 | warn_report("RBD options encoded in the filename as keyvalue pairs " |
| 731 | "is deprecated"); |
Kevin Wolf | 4bfb274 | 2018-02-15 20:58:24 +0100 | [diff] [blame] | 732 | } |
| 733 | |
Jeff Cody | bfb15b4 | 2018-04-04 11:40:45 -0400 | [diff] [blame] | 734 | /* Remove the processed options from the QDict (the visitor processes |
| 735 | * _all_ options in the QDict) */ |
| 736 | while ((e = qdict_first(options))) { |
| 737 | qdict_del(options, e->key); |
| 738 | } |
| 739 | |
Kevin Wolf | d41a558 | 2018-02-16 18:54:52 +0100 | [diff] [blame] | 740 | r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts, |
| 741 | !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp); |
Kevin Wolf | 3d9136f | 2018-02-15 19:13:47 +0100 | [diff] [blame] | 742 | if (r < 0) { |
Kevin Wolf | 4ff4504 | 2018-02-15 20:31:04 +0100 | [diff] [blame] | 743 | goto out; |
Kevin Wolf | 3d9136f | 2018-02-15 19:13:47 +0100 | [diff] [blame] | 744 | } |
| 745 | |
Kevin Wolf | d41a558 | 2018-02-16 18:54:52 +0100 | [diff] [blame] | 746 | s->snap = g_strdup(opts->snapshot); |
| 747 | s->image_name = g_strdup(opts->image); |
| 748 | |
Jeff Cody | e2b8247 | 2017-04-07 16:55:26 -0400 | [diff] [blame] | 749 | /* rbd_open is always r/w */ |
Jeff Cody | 80b61a2 | 2017-04-07 16:55:31 -0400 | [diff] [blame] | 750 | r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap); |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 751 | if (r < 0) { |
Jeff Cody | 80b61a2 | 2017-04-07 16:55:31 -0400 | [diff] [blame] | 752 | error_setg_errno(errp, -r, "error reading header from %s", |
| 753 | s->image_name); |
Sage Weil | eb93d5d | 2011-09-07 09:28:06 -0700 | [diff] [blame] | 754 | goto failed_open; |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 755 | } |
| 756 | |
Stefano Garzarella | d24f802 | 2019-05-09 16:59:27 +0200 | [diff] [blame] | 757 | r = rbd_get_size(s->image, &s->image_size); |
| 758 | if (r < 0) { |
| 759 | error_setg_errno(errp, -r, "error getting image size from %s", |
| 760 | s->image_name); |
| 761 | rbd_close(s->image); |
| 762 | goto failed_open; |
| 763 | } |
| 764 | |
Jeff Cody | e2b8247 | 2017-04-07 16:55:26 -0400 | [diff] [blame] | 765 | /* If we are using an rbd snapshot, we must be r/o, otherwise |
| 766 | * leave as-is */ |
| 767 | if (s->snap != NULL) { |
Kevin Wolf | eaa2410 | 2018-10-12 11:27:41 +0200 | [diff] [blame] | 768 | r = bdrv_apply_auto_read_only(bs, "rbd snapshots are read-only", errp); |
| 769 | if (r < 0) { |
| 770 | rbd_close(s->image); |
| 771 | goto failed_open; |
Jeff Cody | e2b8247 | 2017-04-07 16:55:26 -0400 | [diff] [blame] | 772 | } |
| 773 | } |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 774 | |
Eric Blake | 2f98910 | 2020-04-28 15:29:00 -0500 | [diff] [blame] | 775 | /* When extending regular files, we get zeros from the OS */ |
| 776 | bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE; |
| 777 | |
Kevin Wolf | 4ff4504 | 2018-02-15 20:31:04 +0100 | [diff] [blame] | 778 | r = 0; |
| 779 | goto out; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 780 | |
Sage Weil | eb93d5d | 2011-09-07 09:28:06 -0700 | [diff] [blame] | 781 | failed_open: |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 782 | rados_ioctx_destroy(s->io_ctx); |
Sage Weil | eb93d5d | 2011-09-07 09:28:06 -0700 | [diff] [blame] | 783 | g_free(s->snap); |
Jeff Cody | 80b61a2 | 2017-04-07 16:55:31 -0400 | [diff] [blame] | 784 | g_free(s->image_name); |
Kevin Wolf | 3d9136f | 2018-02-15 19:13:47 +0100 | [diff] [blame] | 785 | rados_shutdown(s->cluster); |
Kevin Wolf | 4ff4504 | 2018-02-15 20:31:04 +0100 | [diff] [blame] | 786 | out: |
Kevin Wolf | 4bfb274 | 2018-02-15 20:58:24 +0100 | [diff] [blame] | 787 | qapi_free_BlockdevOptionsRbd(opts); |
Kevin Wolf | 4ff4504 | 2018-02-15 20:31:04 +0100 | [diff] [blame] | 788 | g_free(keypairs); |
| 789 | g_free(secretid); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 790 | return r; |
| 791 | } |
| 792 | |
Jeff Cody | 56e7cf8 | 2017-04-07 16:55:32 -0400 | [diff] [blame] | 793 | |
| 794 | /* Since RBD is currently always opened R/W via the API, |
| 795 | * we just need to check if we are using a snapshot or not, in |
| 796 | * order to determine if we will allow it to be R/W */ |
| 797 | static int qemu_rbd_reopen_prepare(BDRVReopenState *state, |
| 798 | BlockReopenQueue *queue, Error **errp) |
| 799 | { |
| 800 | BDRVRBDState *s = state->bs->opaque; |
| 801 | int ret = 0; |
| 802 | |
| 803 | if (s->snap && state->flags & BDRV_O_RDWR) { |
| 804 | error_setg(errp, |
| 805 | "Cannot change node '%s' to r/w when using RBD snapshot", |
| 806 | bdrv_get_device_or_node_name(state->bs)); |
| 807 | ret = -EINVAL; |
| 808 | } |
| 809 | |
| 810 | return ret; |
| 811 | } |
| 812 | |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 813 | static void qemu_rbd_close(BlockDriverState *bs) |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 814 | { |
| 815 | BDRVRBDState *s = bs->opaque; |
| 816 | |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 817 | rbd_close(s->image); |
| 818 | rados_ioctx_destroy(s->io_ctx); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 819 | g_free(s->snap); |
Jeff Cody | 80b61a2 | 2017-04-07 16:55:31 -0400 | [diff] [blame] | 820 | g_free(s->image_name); |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 821 | rados_shutdown(s->cluster); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 822 | } |
| 823 | |
Stefano Garzarella | d24f802 | 2019-05-09 16:59:27 +0200 | [diff] [blame] | 824 | /* Resize the RBD image and update the 'image_size' with the current size */ |
| 825 | static int qemu_rbd_resize(BlockDriverState *bs, uint64_t size) |
| 826 | { |
| 827 | BDRVRBDState *s = bs->opaque; |
| 828 | int r; |
| 829 | |
| 830 | r = rbd_resize(s->image, size); |
| 831 | if (r < 0) { |
| 832 | return r; |
| 833 | } |
| 834 | |
| 835 | s->image_size = size; |
| 836 | |
| 837 | return 0; |
| 838 | } |
| 839 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 840 | static const AIOCBInfo rbd_aiocb_info = { |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 841 | .aiocb_size = sizeof(RBDAIOCB), |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 842 | }; |
| 843 | |
Stefan Hajnoczi | e04fb07 | 2013-12-05 16:38:33 +0100 | [diff] [blame] | 844 | static void rbd_finish_bh(void *opaque) |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 845 | { |
Stefan Hajnoczi | e04fb07 | 2013-12-05 16:38:33 +0100 | [diff] [blame] | 846 | RADOSCB *rcb = opaque; |
Stefan Hajnoczi | e04fb07 | 2013-12-05 16:38:33 +0100 | [diff] [blame] | 847 | qemu_rbd_complete_aio(rcb); |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | /* |
| 851 | * This is the callback function for rbd_aio_read and _write |
| 852 | * |
| 853 | * Note: this function is being called from a non qemu thread so |
| 854 | * we need to be careful about what we do here. Generally we only |
Stefan Hajnoczi | e04fb07 | 2013-12-05 16:38:33 +0100 | [diff] [blame] | 855 | * schedule a BH, and do the rest of the io completion handling |
| 856 | * from rbd_finish_bh() which runs in a qemu context. |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 857 | */ |
| 858 | static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb) |
| 859 | { |
Stefan Hajnoczi | e04fb07 | 2013-12-05 16:38:33 +0100 | [diff] [blame] | 860 | RBDAIOCB *acb = rcb->acb; |
| 861 | |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 862 | rcb->ret = rbd_aio_get_return_value(c); |
| 863 | rbd_aio_release(c); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 864 | |
Pavel Dovgalyuk | e4ec5ad | 2019-09-17 14:58:19 +0300 | [diff] [blame] | 865 | replay_bh_schedule_oneshot_event(bdrv_get_aio_context(acb->common.bs), |
| 866 | rbd_finish_bh, rcb); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 867 | } |
| 868 | |
Josh Durgin | 787f313 | 2012-04-30 23:16:45 -0700 | [diff] [blame] | 869 | static int rbd_aio_discard_wrapper(rbd_image_t image, |
| 870 | uint64_t off, |
| 871 | uint64_t len, |
| 872 | rbd_completion_t comp) |
| 873 | { |
| 874 | #ifdef LIBRBD_SUPPORTS_DISCARD |
| 875 | return rbd_aio_discard(image, off, len, comp); |
| 876 | #else |
| 877 | return -ENOTSUP; |
| 878 | #endif |
| 879 | } |
| 880 | |
Josh Durgin | dc7588c | 2013-03-29 13:03:23 -0700 | [diff] [blame] | 881 | static int rbd_aio_flush_wrapper(rbd_image_t image, |
| 882 | rbd_completion_t comp) |
| 883 | { |
| 884 | #ifdef LIBRBD_SUPPORTS_AIO_FLUSH |
| 885 | return rbd_aio_flush(image, comp); |
| 886 | #else |
| 887 | return -ENOTSUP; |
| 888 | #endif |
| 889 | } |
| 890 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 891 | static BlockAIOCB *rbd_start_aio(BlockDriverState *bs, |
Eric Blake | 7bbca9e | 2016-07-15 17:22:56 -0600 | [diff] [blame] | 892 | int64_t off, |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 893 | QEMUIOVector *qiov, |
Eric Blake | 7bbca9e | 2016-07-15 17:22:56 -0600 | [diff] [blame] | 894 | int64_t size, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 895 | BlockCompletionFunc *cb, |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 896 | void *opaque, |
| 897 | RBDAIOCmd cmd) |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 898 | { |
| 899 | RBDAIOCB *acb; |
Kevin Wolf | 0f7a023 | 2014-05-21 18:11:48 +0200 | [diff] [blame] | 900 | RADOSCB *rcb = NULL; |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 901 | rbd_completion_t c; |
Josh Durgin | 51a1352 | 2011-05-26 16:07:33 -0700 | [diff] [blame] | 902 | int r; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 903 | |
| 904 | BDRVRBDState *s = bs->opaque; |
| 905 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 906 | acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque); |
Josh Durgin | 787f313 | 2012-04-30 23:16:45 -0700 | [diff] [blame] | 907 | acb->cmd = cmd; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 908 | acb->qiov = qiov; |
Eric Blake | 7bbca9e | 2016-07-15 17:22:56 -0600 | [diff] [blame] | 909 | assert(!qiov || qiov->size == size); |
tianqing | 1d393bd | 2017-02-21 14:50:03 +0800 | [diff] [blame] | 910 | |
| 911 | rcb = g_new(RADOSCB, 1); |
| 912 | |
| 913 | if (!LIBRBD_USE_IOVEC) { |
| 914 | if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) { |
| 915 | acb->bounce = NULL; |
| 916 | } else { |
| 917 | acb->bounce = qemu_try_blockalign(bs, qiov->size); |
| 918 | if (acb->bounce == NULL) { |
| 919 | goto failed; |
| 920 | } |
Kevin Wolf | 0f7a023 | 2014-05-21 18:11:48 +0200 | [diff] [blame] | 921 | } |
tianqing | 1d393bd | 2017-02-21 14:50:03 +0800 | [diff] [blame] | 922 | if (cmd == RBD_AIO_WRITE) { |
| 923 | qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); |
| 924 | } |
| 925 | rcb->buf = acb->bounce; |
Josh Durgin | 787f313 | 2012-04-30 23:16:45 -0700 | [diff] [blame] | 926 | } |
tianqing | 1d393bd | 2017-02-21 14:50:03 +0800 | [diff] [blame] | 927 | |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 928 | acb->ret = 0; |
| 929 | acb->error = 0; |
| 930 | acb->s = s; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 931 | |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 932 | rcb->acb = acb; |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 933 | rcb->s = acb->s; |
| 934 | rcb->size = size; |
Josh Durgin | 51a1352 | 2011-05-26 16:07:33 -0700 | [diff] [blame] | 935 | r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c); |
| 936 | if (r < 0) { |
| 937 | goto failed; |
| 938 | } |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 939 | |
Josh Durgin | 787f313 | 2012-04-30 23:16:45 -0700 | [diff] [blame] | 940 | switch (cmd) { |
Stefano Garzarella | d24f802 | 2019-05-09 16:59:27 +0200 | [diff] [blame] | 941 | case RBD_AIO_WRITE: { |
| 942 | /* |
| 943 | * RBD APIs don't allow us to write more than actual size, so in order |
| 944 | * to support growing images, we resize the image before write |
| 945 | * operations that exceed the current size. |
| 946 | */ |
| 947 | if (off + size > s->image_size) { |
| 948 | r = qemu_rbd_resize(bs, off + size); |
| 949 | if (r < 0) { |
| 950 | goto failed_completion; |
| 951 | } |
| 952 | } |
tianqing | 1d393bd | 2017-02-21 14:50:03 +0800 | [diff] [blame] | 953 | #ifdef LIBRBD_SUPPORTS_IOVEC |
| 954 | r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c); |
| 955 | #else |
| 956 | r = rbd_aio_write(s->image, off, size, rcb->buf, c); |
| 957 | #endif |
Josh Durgin | 787f313 | 2012-04-30 23:16:45 -0700 | [diff] [blame] | 958 | break; |
Stefano Garzarella | d24f802 | 2019-05-09 16:59:27 +0200 | [diff] [blame] | 959 | } |
Josh Durgin | 787f313 | 2012-04-30 23:16:45 -0700 | [diff] [blame] | 960 | case RBD_AIO_READ: |
tianqing | 1d393bd | 2017-02-21 14:50:03 +0800 | [diff] [blame] | 961 | #ifdef LIBRBD_SUPPORTS_IOVEC |
| 962 | r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c); |
| 963 | #else |
| 964 | r = rbd_aio_read(s->image, off, size, rcb->buf, c); |
| 965 | #endif |
Josh Durgin | 787f313 | 2012-04-30 23:16:45 -0700 | [diff] [blame] | 966 | break; |
| 967 | case RBD_AIO_DISCARD: |
| 968 | r = rbd_aio_discard_wrapper(s->image, off, size, c); |
| 969 | break; |
Josh Durgin | dc7588c | 2013-03-29 13:03:23 -0700 | [diff] [blame] | 970 | case RBD_AIO_FLUSH: |
| 971 | r = rbd_aio_flush_wrapper(s->image, c); |
| 972 | break; |
Josh Durgin | 787f313 | 2012-04-30 23:16:45 -0700 | [diff] [blame] | 973 | default: |
| 974 | r = -EINVAL; |
Josh Durgin | 51a1352 | 2011-05-26 16:07:33 -0700 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | if (r < 0) { |
Kevin Wolf | 405a276 | 2014-06-05 16:19:26 +0200 | [diff] [blame] | 978 | goto failed_completion; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 979 | } |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 980 | return &acb->common; |
Josh Durgin | 51a1352 | 2011-05-26 16:07:33 -0700 | [diff] [blame] | 981 | |
Kevin Wolf | 405a276 | 2014-06-05 16:19:26 +0200 | [diff] [blame] | 982 | failed_completion: |
| 983 | rbd_aio_release(c); |
Josh Durgin | 51a1352 | 2011-05-26 16:07:33 -0700 | [diff] [blame] | 984 | failed: |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 985 | g_free(rcb); |
tianqing | 1d393bd | 2017-02-21 14:50:03 +0800 | [diff] [blame] | 986 | if (!LIBRBD_USE_IOVEC) { |
| 987 | qemu_vfree(acb->bounce); |
| 988 | } |
| 989 | |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 990 | qemu_aio_unref(acb); |
Josh Durgin | 51a1352 | 2011-05-26 16:07:33 -0700 | [diff] [blame] | 991 | return NULL; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 992 | } |
| 993 | |
Eric Blake | e8e16d4 | 2018-04-24 14:25:04 -0500 | [diff] [blame] | 994 | static BlockAIOCB *qemu_rbd_aio_preadv(BlockDriverState *bs, |
| 995 | uint64_t offset, uint64_t bytes, |
| 996 | QEMUIOVector *qiov, int flags, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 997 | BlockCompletionFunc *cb, |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 998 | void *opaque) |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 999 | { |
Eric Blake | e8e16d4 | 2018-04-24 14:25:04 -0500 | [diff] [blame] | 1000 | return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque, |
| 1001 | RBD_AIO_READ); |
| 1002 | } |
| 1003 | |
| 1004 | static BlockAIOCB *qemu_rbd_aio_pwritev(BlockDriverState *bs, |
| 1005 | uint64_t offset, uint64_t bytes, |
| 1006 | QEMUIOVector *qiov, int flags, |
| 1007 | BlockCompletionFunc *cb, |
| 1008 | void *opaque) |
| 1009 | { |
| 1010 | return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque, |
Josh Durgin | 787f313 | 2012-04-30 23:16:45 -0700 | [diff] [blame] | 1011 | RBD_AIO_WRITE); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1012 | } |
| 1013 | |
Josh Durgin | dc7588c | 2013-03-29 13:03:23 -0700 | [diff] [blame] | 1014 | #ifdef LIBRBD_SUPPORTS_AIO_FLUSH |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 1015 | static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 1016 | BlockCompletionFunc *cb, |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 1017 | void *opaque) |
Josh Durgin | dc7588c | 2013-03-29 13:03:23 -0700 | [diff] [blame] | 1018 | { |
| 1019 | return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH); |
| 1020 | } |
| 1021 | |
| 1022 | #else |
| 1023 | |
Paolo Bonzini | 8b94ff8 | 2011-10-20 13:16:24 +0200 | [diff] [blame] | 1024 | static int qemu_rbd_co_flush(BlockDriverState *bs) |
Sage Weil | 7a3f5fe | 2011-09-15 14:11:11 -0700 | [diff] [blame] | 1025 | { |
| 1026 | #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1) |
| 1027 | /* rbd_flush added in 0.1.1 */ |
| 1028 | BDRVRBDState *s = bs->opaque; |
| 1029 | return rbd_flush(s->image); |
| 1030 | #else |
| 1031 | return 0; |
| 1032 | #endif |
| 1033 | } |
Josh Durgin | dc7588c | 2013-03-29 13:03:23 -0700 | [diff] [blame] | 1034 | #endif |
Sage Weil | 7a3f5fe | 2011-09-15 14:11:11 -0700 | [diff] [blame] | 1035 | |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 1036 | static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi) |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1037 | { |
| 1038 | BDRVRBDState *s = bs->opaque; |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 1039 | rbd_image_info_t info; |
| 1040 | int r; |
| 1041 | |
| 1042 | r = rbd_stat(s->image, &info, sizeof(info)); |
| 1043 | if (r < 0) { |
| 1044 | return r; |
| 1045 | } |
| 1046 | |
| 1047 | bdi->cluster_size = info.obj_size; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1048 | return 0; |
| 1049 | } |
| 1050 | |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 1051 | static int64_t qemu_rbd_getlength(BlockDriverState *bs) |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1052 | { |
| 1053 | BDRVRBDState *s = bs->opaque; |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 1054 | rbd_image_info_t info; |
| 1055 | int r; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1056 | |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 1057 | r = rbd_stat(s->image, &info, sizeof(info)); |
| 1058 | if (r < 0) { |
| 1059 | return r; |
| 1060 | } |
| 1061 | |
| 1062 | return info.size; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1063 | } |
| 1064 | |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 1065 | static int coroutine_fn qemu_rbd_co_truncate(BlockDriverState *bs, |
| 1066 | int64_t offset, |
Max Reitz | c80d8b0 | 2019-09-18 11:51:40 +0200 | [diff] [blame] | 1067 | bool exact, |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 1068 | PreallocMode prealloc, |
Kevin Wolf | 92b9279 | 2020-04-24 14:54:39 +0200 | [diff] [blame] | 1069 | BdrvRequestFlags flags, |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 1070 | Error **errp) |
Josh Durgin | 30cdc48 | 2011-05-26 16:07:34 -0700 | [diff] [blame] | 1071 | { |
Josh Durgin | 30cdc48 | 2011-05-26 16:07:34 -0700 | [diff] [blame] | 1072 | int r; |
| 1073 | |
Max Reitz | 8243ccb | 2017-06-13 22:20:52 +0200 | [diff] [blame] | 1074 | if (prealloc != PREALLOC_MODE_OFF) { |
| 1075 | error_setg(errp, "Unsupported preallocation mode '%s'", |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 1076 | PreallocMode_str(prealloc)); |
Max Reitz | 8243ccb | 2017-06-13 22:20:52 +0200 | [diff] [blame] | 1077 | return -ENOTSUP; |
| 1078 | } |
| 1079 | |
Stefano Garzarella | d24f802 | 2019-05-09 16:59:27 +0200 | [diff] [blame] | 1080 | r = qemu_rbd_resize(bs, offset); |
Josh Durgin | 30cdc48 | 2011-05-26 16:07:34 -0700 | [diff] [blame] | 1081 | if (r < 0) { |
Max Reitz | f59adb3 | 2017-03-28 22:51:29 +0200 | [diff] [blame] | 1082 | error_setg_errno(errp, -r, "Failed to resize file"); |
Josh Durgin | 30cdc48 | 2011-05-26 16:07:34 -0700 | [diff] [blame] | 1083 | return r; |
| 1084 | } |
| 1085 | |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 1089 | static int qemu_rbd_snap_create(BlockDriverState *bs, |
| 1090 | QEMUSnapshotInfo *sn_info) |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1091 | { |
| 1092 | BDRVRBDState *s = bs->opaque; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1093 | int r; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1094 | |
| 1095 | if (sn_info->name[0] == '\0') { |
| 1096 | return -EINVAL; /* we need a name for rbd snapshots */ |
| 1097 | } |
| 1098 | |
| 1099 | /* |
| 1100 | * rbd snapshots are using the name as the user controlled unique identifier |
| 1101 | * we can't use the rbd snapid for that purpose, as it can't be set |
| 1102 | */ |
| 1103 | if (sn_info->id_str[0] != '\0' && |
| 1104 | strcmp(sn_info->id_str, sn_info->name) != 0) { |
| 1105 | return -EINVAL; |
| 1106 | } |
| 1107 | |
| 1108 | if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) { |
| 1109 | return -ERANGE; |
| 1110 | } |
| 1111 | |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 1112 | r = rbd_snap_create(s->image, sn_info->name); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1113 | if (r < 0) { |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 1114 | error_report("failed to create snap: %s", strerror(-r)); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1115 | return r; |
| 1116 | } |
| 1117 | |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1118 | return 0; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1119 | } |
| 1120 | |
Gregory Farnum | bd60324 | 2012-01-11 11:53:52 -0800 | [diff] [blame] | 1121 | static int qemu_rbd_snap_remove(BlockDriverState *bs, |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 1122 | const char *snapshot_id, |
| 1123 | const char *snapshot_name, |
| 1124 | Error **errp) |
Gregory Farnum | bd60324 | 2012-01-11 11:53:52 -0800 | [diff] [blame] | 1125 | { |
| 1126 | BDRVRBDState *s = bs->opaque; |
| 1127 | int r; |
| 1128 | |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 1129 | if (!snapshot_name) { |
| 1130 | error_setg(errp, "rbd need a valid snapshot name"); |
| 1131 | return -EINVAL; |
| 1132 | } |
| 1133 | |
| 1134 | /* If snapshot_id is specified, it must be equal to name, see |
| 1135 | qemu_rbd_snap_list() */ |
| 1136 | if (snapshot_id && strcmp(snapshot_id, snapshot_name)) { |
| 1137 | error_setg(errp, |
| 1138 | "rbd do not support snapshot id, it should be NULL or " |
| 1139 | "equal to snapshot name"); |
| 1140 | return -EINVAL; |
| 1141 | } |
| 1142 | |
Gregory Farnum | bd60324 | 2012-01-11 11:53:52 -0800 | [diff] [blame] | 1143 | r = rbd_snap_remove(s->image, snapshot_name); |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 1144 | if (r < 0) { |
| 1145 | error_setg_errno(errp, -r, "Failed to remove the snapshot"); |
| 1146 | } |
Gregory Farnum | bd60324 | 2012-01-11 11:53:52 -0800 | [diff] [blame] | 1147 | return r; |
| 1148 | } |
| 1149 | |
| 1150 | static int qemu_rbd_snap_rollback(BlockDriverState *bs, |
| 1151 | const char *snapshot_name) |
| 1152 | { |
| 1153 | BDRVRBDState *s = bs->opaque; |
Gregory Farnum | bd60324 | 2012-01-11 11:53:52 -0800 | [diff] [blame] | 1154 | |
Eduardo Habkost | 9be3859 | 2016-06-13 18:57:58 -0300 | [diff] [blame] | 1155 | return rbd_snap_rollback(s->image, snapshot_name); |
Gregory Farnum | bd60324 | 2012-01-11 11:53:52 -0800 | [diff] [blame] | 1156 | } |
| 1157 | |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 1158 | static int qemu_rbd_snap_list(BlockDriverState *bs, |
| 1159 | QEMUSnapshotInfo **psn_tab) |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1160 | { |
| 1161 | BDRVRBDState *s = bs->opaque; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1162 | QEMUSnapshotInfo *sn_info, *sn_tab = NULL; |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 1163 | int i, snap_count; |
| 1164 | rbd_snap_info_t *snaps; |
| 1165 | int max_snaps = RBD_MAX_SNAPS; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1166 | |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 1167 | do { |
Markus Armbruster | 02c4f26 | 2014-08-19 10:31:09 +0200 | [diff] [blame] | 1168 | snaps = g_new(rbd_snap_info_t, max_snaps); |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 1169 | snap_count = rbd_snap_list(s->image, snaps, &max_snaps); |
Stefan Hajnoczi | 9e6337d | 2013-09-25 16:00:48 +0200 | [diff] [blame] | 1170 | if (snap_count <= 0) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1171 | g_free(snaps); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1172 | } |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 1173 | } while (snap_count == -ERANGE); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1174 | |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 1175 | if (snap_count <= 0) { |
Josh Durgin | b9c5329 | 2011-12-06 17:05:10 -0800 | [diff] [blame] | 1176 | goto done; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1177 | } |
| 1178 | |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 1179 | sn_tab = g_new0(QEMUSnapshotInfo, snap_count); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1180 | |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 1181 | for (i = 0; i < snap_count; i++) { |
| 1182 | const char *snap_name = snaps[i].name; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1183 | |
| 1184 | sn_info = sn_tab + i; |
| 1185 | pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name); |
| 1186 | pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name); |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1187 | |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 1188 | sn_info->vm_state_size = snaps[i].size; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1189 | sn_info->date_sec = 0; |
| 1190 | sn_info->date_nsec = 0; |
| 1191 | sn_info->vm_clock_nsec = 0; |
| 1192 | } |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 1193 | rbd_snap_list_end(snaps); |
Stefan Hajnoczi | 9e6337d | 2013-09-25 16:00:48 +0200 | [diff] [blame] | 1194 | g_free(snaps); |
Josh Durgin | ad32e9c | 2011-05-26 16:07:31 -0700 | [diff] [blame] | 1195 | |
Josh Durgin | b9c5329 | 2011-12-06 17:05:10 -0800 | [diff] [blame] | 1196 | done: |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1197 | *psn_tab = sn_tab; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1198 | return snap_count; |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1199 | } |
| 1200 | |
Josh Durgin | 787f313 | 2012-04-30 23:16:45 -0700 | [diff] [blame] | 1201 | #ifdef LIBRBD_SUPPORTS_DISCARD |
Eric Blake | 4da444a | 2016-07-15 17:22:57 -0600 | [diff] [blame] | 1202 | static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs, |
| 1203 | int64_t offset, |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 1204 | int bytes, |
Eric Blake | 4da444a | 2016-07-15 17:22:57 -0600 | [diff] [blame] | 1205 | BlockCompletionFunc *cb, |
| 1206 | void *opaque) |
Josh Durgin | 787f313 | 2012-04-30 23:16:45 -0700 | [diff] [blame] | 1207 | { |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 1208 | return rbd_start_aio(bs, offset, NULL, bytes, cb, opaque, |
Josh Durgin | 787f313 | 2012-04-30 23:16:45 -0700 | [diff] [blame] | 1209 | RBD_AIO_DISCARD); |
| 1210 | } |
| 1211 | #endif |
| 1212 | |
Adam Crume | be21788 | 2014-10-09 11:44:32 -0700 | [diff] [blame] | 1213 | #ifdef LIBRBD_SUPPORTS_INVALIDATE |
Paolo Bonzini | 2b148f3 | 2018-03-01 17:36:18 +0100 | [diff] [blame] | 1214 | static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs, |
| 1215 | Error **errp) |
Adam Crume | be21788 | 2014-10-09 11:44:32 -0700 | [diff] [blame] | 1216 | { |
| 1217 | BDRVRBDState *s = bs->opaque; |
| 1218 | int r = rbd_invalidate_cache(s->image); |
| 1219 | if (r < 0) { |
| 1220 | error_setg_errno(errp, -r, "Failed to invalidate the cache"); |
| 1221 | } |
| 1222 | } |
| 1223 | #endif |
| 1224 | |
Chunyan Liu | bd0cf59 | 2014-06-05 17:21:04 +0800 | [diff] [blame] | 1225 | static QemuOptsList qemu_rbd_create_opts = { |
| 1226 | .name = "rbd-create-opts", |
| 1227 | .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head), |
| 1228 | .desc = { |
| 1229 | { |
| 1230 | .name = BLOCK_OPT_SIZE, |
| 1231 | .type = QEMU_OPT_SIZE, |
| 1232 | .help = "Virtual disk size" |
| 1233 | }, |
| 1234 | { |
| 1235 | .name = BLOCK_OPT_CLUSTER_SIZE, |
| 1236 | .type = QEMU_OPT_SIZE, |
| 1237 | .help = "RBD object size" |
| 1238 | }, |
Daniel P. Berrange | 60390a2 | 2016-01-21 14:19:19 +0000 | [diff] [blame] | 1239 | { |
| 1240 | .name = "password-secret", |
| 1241 | .type = QEMU_OPT_STRING, |
| 1242 | .help = "ID of secret providing the password", |
| 1243 | }, |
Chunyan Liu | bd0cf59 | 2014-06-05 17:21:04 +0800 | [diff] [blame] | 1244 | { /* end of list */ } |
| 1245 | } |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1246 | }; |
| 1247 | |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 1248 | static const char *const qemu_rbd_strong_runtime_opts[] = { |
| 1249 | "pool", |
Stefano Garzarella | 7bae7c8 | 2020-09-14 21:05:53 +0200 | [diff] [blame] | 1250 | "namespace", |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 1251 | "image", |
| 1252 | "conf", |
| 1253 | "snapshot", |
| 1254 | "user", |
| 1255 | "server.", |
| 1256 | "password-secret", |
| 1257 | |
| 1258 | NULL |
| 1259 | }; |
| 1260 | |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1261 | static BlockDriver bdrv_rbd = { |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 1262 | .format_name = "rbd", |
| 1263 | .instance_size = sizeof(BDRVRBDState), |
| 1264 | .bdrv_parse_filename = qemu_rbd_parse_filename, |
Eric Blake | e8e16d4 | 2018-04-24 14:25:04 -0500 | [diff] [blame] | 1265 | .bdrv_refresh_limits = qemu_rbd_refresh_limits, |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 1266 | .bdrv_file_open = qemu_rbd_open, |
| 1267 | .bdrv_close = qemu_rbd_close, |
Jeff Cody | 56e7cf8 | 2017-04-07 16:55:32 -0400 | [diff] [blame] | 1268 | .bdrv_reopen_prepare = qemu_rbd_reopen_prepare, |
Kevin Wolf | 1bebea3 | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 1269 | .bdrv_co_create = qemu_rbd_co_create, |
Stefan Hajnoczi | efc75e2 | 2018-01-18 13:43:45 +0100 | [diff] [blame] | 1270 | .bdrv_co_create_opts = qemu_rbd_co_create_opts, |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 1271 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
| 1272 | .bdrv_get_info = qemu_rbd_getinfo, |
| 1273 | .create_opts = &qemu_rbd_create_opts, |
| 1274 | .bdrv_getlength = qemu_rbd_getlength, |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 1275 | .bdrv_co_truncate = qemu_rbd_co_truncate, |
Jeff Cody | c7cacb3 | 2017-02-26 17:50:42 -0500 | [diff] [blame] | 1276 | .protocol_name = "rbd", |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1277 | |
Eric Blake | e8e16d4 | 2018-04-24 14:25:04 -0500 | [diff] [blame] | 1278 | .bdrv_aio_preadv = qemu_rbd_aio_preadv, |
| 1279 | .bdrv_aio_pwritev = qemu_rbd_aio_pwritev, |
Josh Durgin | dc7588c | 2013-03-29 13:03:23 -0700 | [diff] [blame] | 1280 | |
| 1281 | #ifdef LIBRBD_SUPPORTS_AIO_FLUSH |
| 1282 | .bdrv_aio_flush = qemu_rbd_aio_flush, |
| 1283 | #else |
Kevin Wolf | c68b89a | 2011-11-10 17:25:44 +0100 | [diff] [blame] | 1284 | .bdrv_co_flush_to_disk = qemu_rbd_co_flush, |
Josh Durgin | dc7588c | 2013-03-29 13:03:23 -0700 | [diff] [blame] | 1285 | #endif |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1286 | |
Josh Durgin | 787f313 | 2012-04-30 23:16:45 -0700 | [diff] [blame] | 1287 | #ifdef LIBRBD_SUPPORTS_DISCARD |
Eric Blake | 4da444a | 2016-07-15 17:22:57 -0600 | [diff] [blame] | 1288 | .bdrv_aio_pdiscard = qemu_rbd_aio_pdiscard, |
Josh Durgin | 787f313 | 2012-04-30 23:16:45 -0700 | [diff] [blame] | 1289 | #endif |
| 1290 | |
Kevin Wolf | c68b89a | 2011-11-10 17:25:44 +0100 | [diff] [blame] | 1291 | .bdrv_snapshot_create = qemu_rbd_snap_create, |
Gregory Farnum | bd60324 | 2012-01-11 11:53:52 -0800 | [diff] [blame] | 1292 | .bdrv_snapshot_delete = qemu_rbd_snap_remove, |
Kevin Wolf | c68b89a | 2011-11-10 17:25:44 +0100 | [diff] [blame] | 1293 | .bdrv_snapshot_list = qemu_rbd_snap_list, |
Gregory Farnum | bd60324 | 2012-01-11 11:53:52 -0800 | [diff] [blame] | 1294 | .bdrv_snapshot_goto = qemu_rbd_snap_rollback, |
Adam Crume | be21788 | 2014-10-09 11:44:32 -0700 | [diff] [blame] | 1295 | #ifdef LIBRBD_SUPPORTS_INVALIDATE |
Paolo Bonzini | 2b148f3 | 2018-03-01 17:36:18 +0100 | [diff] [blame] | 1296 | .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache, |
Adam Crume | be21788 | 2014-10-09 11:44:32 -0700 | [diff] [blame] | 1297 | #endif |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 1298 | |
| 1299 | .strong_runtime_opts = qemu_rbd_strong_runtime_opts, |
Christian Brunner | f27aaf4 | 2010-12-06 20:53:01 +0100 | [diff] [blame] | 1300 | }; |
| 1301 | |
| 1302 | static void bdrv_rbd_init(void) |
| 1303 | { |
| 1304 | bdrv_register(&bdrv_rbd); |
| 1305 | } |
| 1306 | |
| 1307 | block_init(bdrv_rbd_init); |