1019d6b8fSAnthony Liguori /* 2019d6b8fSAnthony Liguori * QEMU Block driver for NBD 3019d6b8fSAnthony Liguori * 4f7651539SVladimir Sementsov-Ogievskiy * Copyright (c) 2019 Virtuozzo International GmbH. 522efd811SEric Blake * Copyright Red Hat 6019d6b8fSAnthony Liguori * Copyright (C) 2008 Bull S.A.S. 7019d6b8fSAnthony Liguori * Author: Laurent Vivier <Laurent.Vivier@bull.net> 8019d6b8fSAnthony Liguori * 9019d6b8fSAnthony Liguori * Some parts: 10019d6b8fSAnthony Liguori * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws> 11019d6b8fSAnthony Liguori * 12019d6b8fSAnthony Liguori * Permission is hereby granted, free of charge, to any person obtaining a copy 13019d6b8fSAnthony Liguori * of this software and associated documentation files (the "Software"), to deal 14019d6b8fSAnthony Liguori * in the Software without restriction, including without limitation the rights 15019d6b8fSAnthony Liguori * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16019d6b8fSAnthony Liguori * copies of the Software, and to permit persons to whom the Software is 17019d6b8fSAnthony Liguori * furnished to do so, subject to the following conditions: 18019d6b8fSAnthony Liguori * 19019d6b8fSAnthony Liguori * The above copyright notice and this permission notice shall be included in 20019d6b8fSAnthony Liguori * all copies or substantial portions of the Software. 21019d6b8fSAnthony Liguori * 22019d6b8fSAnthony Liguori * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23019d6b8fSAnthony Liguori * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24019d6b8fSAnthony Liguori * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 25019d6b8fSAnthony Liguori * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26019d6b8fSAnthony Liguori * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27019d6b8fSAnthony Liguori * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 28019d6b8fSAnthony Liguori * THE SOFTWARE. 29019d6b8fSAnthony Liguori */ 30019d6b8fSAnthony Liguori 3180c71a24SPeter Maydell #include "qemu/osdep.h" 3286f8cdf3SVladimir Sementsov-Ogievskiy 3386f8cdf3SVladimir Sementsov-Ogievskiy #include "trace.h" 341de7afc9SPaolo Bonzini #include "qemu/uri.h" 35922a01a0SMarkus Armbruster #include "qemu/option.h" 36f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 37db725815SMarkus Armbruster #include "qemu/main-loop.h" 38019d6b8fSAnthony Liguori 3986f8cdf3SVladimir Sementsov-Ogievskiy #include "qapi/qapi-visit-sockets.h" 4086f8cdf3SVladimir Sementsov-Ogievskiy #include "qapi/qmp/qstring.h" 411dc4718dSVladimir Sementsov-Ogievskiy #include "qapi/clone-visitor.h" 4286f8cdf3SVladimir Sementsov-Ogievskiy 4386f8cdf3SVladimir Sementsov-Ogievskiy #include "block/qdict.h" 4486f8cdf3SVladimir Sementsov-Ogievskiy #include "block/nbd.h" 4586f8cdf3SVladimir Sementsov-Ogievskiy #include "block/block_int.h" 46a71d597bSVladimir Sementsov-Ogievskiy #include "block/coroutines.h" 4786f8cdf3SVladimir Sementsov-Ogievskiy 48fee091cdSLukas Straub #include "qemu/yank.h" 49fee091cdSLukas Straub 501d45f8b5SLaurent Vivier #define EN_OPTSTR ":exportname=" 5186f8cdf3SVladimir Sementsov-Ogievskiy #define MAX_NBD_REQUESTS 16 5286f8cdf3SVladimir Sementsov-Ogievskiy 538cb98a72SEric Blake #define COOKIE_TO_INDEX(cookie) ((cookie) - 1) 548cb98a72SEric Blake #define INDEX_TO_COOKIE(index) ((index) + 1) 5586f8cdf3SVladimir Sementsov-Ogievskiy 5686f8cdf3SVladimir Sementsov-Ogievskiy typedef struct { 5786f8cdf3SVladimir Sementsov-Ogievskiy Coroutine *coroutine; 5886f8cdf3SVladimir Sementsov-Ogievskiy uint64_t offset; /* original offset of the request */ 594ddb5d2fSVladimir Sementsov-Ogievskiy bool receiving; /* sleeping in the yield in nbd_receive_replies */ 6086f8cdf3SVladimir Sementsov-Ogievskiy } NBDClientRequest; 6186f8cdf3SVladimir Sementsov-Ogievskiy 62a34b1e5eSVladimir Sementsov-Ogievskiy typedef enum NBDClientState { 63f7651539SVladimir Sementsov-Ogievskiy NBD_CLIENT_CONNECTING_WAIT, 64f7651539SVladimir Sementsov-Ogievskiy NBD_CLIENT_CONNECTING_NOWAIT, 65a34b1e5eSVladimir Sementsov-Ogievskiy NBD_CLIENT_CONNECTED, 66a34b1e5eSVladimir Sementsov-Ogievskiy NBD_CLIENT_QUIT 67a34b1e5eSVladimir Sementsov-Ogievskiy } NBDClientState; 68a34b1e5eSVladimir Sementsov-Ogievskiy 69611ae1d7SVladimir Sementsov-Ogievskiy typedef struct BDRVNBDState { 7095a078eaSVladimir Sementsov-Ogievskiy QIOChannel *ioc; /* The current I/O channel */ 7186f8cdf3SVladimir Sementsov-Ogievskiy NBDExportInfo info; 7286f8cdf3SVladimir Sementsov-Ogievskiy 73ee19d953SPaolo Bonzini /* 74dba5156cSPaolo Bonzini * Protects state, free_sema, in_flight, requests[].coroutine, 75ee19d953SPaolo Bonzini * reconnect_delay_timer. 76ee19d953SPaolo Bonzini */ 77ee19d953SPaolo Bonzini QemuMutex requests_lock; 78dba5156cSPaolo Bonzini NBDClientState state; 7986f8cdf3SVladimir Sementsov-Ogievskiy CoQueue free_sema; 801b8f7776SDenis V. Lunev unsigned in_flight; 81ee19d953SPaolo Bonzini NBDClientRequest requests[MAX_NBD_REQUESTS]; 82ee19d953SPaolo Bonzini QEMUTimer *reconnect_delay_timer; 83ee19d953SPaolo Bonzini 84620c5cb5SPaolo Bonzini /* Protects sending data on the socket. */ 85ee19d953SPaolo Bonzini CoMutex send_mutex; 86620c5cb5SPaolo Bonzini 87620c5cb5SPaolo Bonzini /* 88620c5cb5SPaolo Bonzini * Protects receiving reply headers from the socket, as well as the 89620c5cb5SPaolo Bonzini * fields reply and requests[].receiving 90620c5cb5SPaolo Bonzini */ 91ee19d953SPaolo Bonzini CoMutex receive_mutex; 92620c5cb5SPaolo Bonzini NBDReply reply; 9386f8cdf3SVladimir Sementsov-Ogievskiy 94be16b8bfSVladimir Sementsov-Ogievskiy QEMUTimer *open_timer; 9546f56631SVladimir Sementsov-Ogievskiy 9686f8cdf3SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 9703504d05SMax Reitz 988f071c9dSVladimir Sementsov-Ogievskiy /* Connection parameters */ 998f071c9dSVladimir Sementsov-Ogievskiy uint32_t reconnect_delay; 100be16b8bfSVladimir Sementsov-Ogievskiy uint32_t open_timeout; 10162cf396bSMarkus Armbruster SocketAddress *saddr; 102a0cd6d29SDaniel P. Berrangé char *export; 103a0cd6d29SDaniel P. Berrangé char *tlscredsid; 1048f071c9dSVladimir Sementsov-Ogievskiy QCryptoTLSCreds *tlscreds; 105a0cd6d29SDaniel P. Berrangé char *tlshostname; 1068f071c9dSVladimir Sementsov-Ogievskiy char *x_dirty_bitmap; 107dbc7b014SEric Blake bool alloc_depth; 1081dc4718dSVladimir Sementsov-Ogievskiy 10990ddc64fSVladimir Sementsov-Ogievskiy NBDClientConnection *conn; 110019d6b8fSAnthony Liguori } BDRVNBDState; 111019d6b8fSAnthony Liguori 112fee091cdSLukas Straub static void nbd_yank(void *opaque); 113f7651539SVladimir Sementsov-Ogievskiy 114bbba1c37SVladimir Sementsov-Ogievskiy static void nbd_clear_bdrvstate(BlockDriverState *bs) 1157f493662SPan Nengyuan { 116bbba1c37SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 117e8b35bf5SRoman Kagan 118248d4701SVladimir Sementsov-Ogievskiy nbd_client_connection_release(s->conn); 119248d4701SVladimir Sementsov-Ogievskiy s->conn = NULL; 120bbba1c37SVladimir Sementsov-Ogievskiy 121bbba1c37SVladimir Sementsov-Ogievskiy yank_unregister_instance(BLOCKDEV_YANK_INSTANCE(bs->node_name)); 122bbba1c37SVladimir Sementsov-Ogievskiy 1238a39c381SHanna Reitz /* Must not leave timers behind that would access freed data */ 1248a39c381SHanna Reitz assert(!s->reconnect_delay_timer); 1258a39c381SHanna Reitz assert(!s->open_timer); 1268a39c381SHanna Reitz 1277f493662SPan Nengyuan object_unref(OBJECT(s->tlscreds)); 1287f493662SPan Nengyuan qapi_free_SocketAddress(s->saddr); 1297f493662SPan Nengyuan s->saddr = NULL; 1307f493662SPan Nengyuan g_free(s->export); 1317f493662SPan Nengyuan s->export = NULL; 1327f493662SPan Nengyuan g_free(s->tlscredsid); 1337f493662SPan Nengyuan s->tlscredsid = NULL; 134a0cd6d29SDaniel P. Berrangé g_free(s->tlshostname); 135a0cd6d29SDaniel P. Berrangé s->tlshostname = NULL; 1367f493662SPan Nengyuan g_free(s->x_dirty_bitmap); 1377f493662SPan Nengyuan s->x_dirty_bitmap = NULL; 1387f493662SPan Nengyuan } 1397f493662SPan Nengyuan 140a80a9a1cSPaolo Bonzini /* Called with s->receive_mutex taken. */ 1410c43c6fcSPaolo Bonzini static bool coroutine_fn nbd_recv_coroutine_wake_one(NBDClientRequest *req) 14204a953b2SVladimir Sementsov-Ogievskiy { 14304a953b2SVladimir Sementsov-Ogievskiy if (req->receiving) { 14404a953b2SVladimir Sementsov-Ogievskiy req->receiving = false; 14504a953b2SVladimir Sementsov-Ogievskiy aio_co_wake(req->coroutine); 14604a953b2SVladimir Sementsov-Ogievskiy return true; 14704a953b2SVladimir Sementsov-Ogievskiy } 14804a953b2SVladimir Sementsov-Ogievskiy 14904a953b2SVladimir Sementsov-Ogievskiy return false; 15004a953b2SVladimir Sementsov-Ogievskiy } 15104a953b2SVladimir Sementsov-Ogievskiy 152a80a9a1cSPaolo Bonzini static void coroutine_fn nbd_recv_coroutines_wake(BDRVNBDState *s) 1533bc0bd1fSVladimir Sementsov-Ogievskiy { 1543bc0bd1fSVladimir Sementsov-Ogievskiy int i; 1553bc0bd1fSVladimir Sementsov-Ogievskiy 156a80a9a1cSPaolo Bonzini QEMU_LOCK_GUARD(&s->receive_mutex); 1573bc0bd1fSVladimir Sementsov-Ogievskiy for (i = 0; i < MAX_NBD_REQUESTS; i++) { 158a80a9a1cSPaolo Bonzini if (nbd_recv_coroutine_wake_one(&s->requests[i])) { 15904a953b2SVladimir Sementsov-Ogievskiy return; 1603bc0bd1fSVladimir Sementsov-Ogievskiy } 1613bc0bd1fSVladimir Sementsov-Ogievskiy } 1623bc0bd1fSVladimir Sementsov-Ogievskiy } 1633bc0bd1fSVladimir Sementsov-Ogievskiy 164dba5156cSPaolo Bonzini /* Called with s->requests_lock held. */ 165dba5156cSPaolo Bonzini static void coroutine_fn nbd_channel_error_locked(BDRVNBDState *s, int ret) 166a34b1e5eSVladimir Sementsov-Ogievskiy { 167dba5156cSPaolo Bonzini if (s->state == NBD_CLIENT_CONNECTED) { 168cb116da7SVladimir Sementsov-Ogievskiy qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); 169cb116da7SVladimir Sementsov-Ogievskiy } 170cb116da7SVladimir Sementsov-Ogievskiy 171f7651539SVladimir Sementsov-Ogievskiy if (ret == -EIO) { 172dba5156cSPaolo Bonzini if (s->state == NBD_CLIENT_CONNECTED) { 173f7651539SVladimir Sementsov-Ogievskiy s->state = s->reconnect_delay ? NBD_CLIENT_CONNECTING_WAIT : 174f7651539SVladimir Sementsov-Ogievskiy NBD_CLIENT_CONNECTING_NOWAIT; 175f7651539SVladimir Sementsov-Ogievskiy } 176f7651539SVladimir Sementsov-Ogievskiy } else { 177a34b1e5eSVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_QUIT; 178a34b1e5eSVladimir Sementsov-Ogievskiy } 179f7651539SVladimir Sementsov-Ogievskiy } 180a34b1e5eSVladimir Sementsov-Ogievskiy 181dba5156cSPaolo Bonzini static void coroutine_fn nbd_channel_error(BDRVNBDState *s, int ret) 182dba5156cSPaolo Bonzini { 183dba5156cSPaolo Bonzini QEMU_LOCK_GUARD(&s->requests_lock); 184dba5156cSPaolo Bonzini nbd_channel_error_locked(s, ret); 185dba5156cSPaolo Bonzini } 186dba5156cSPaolo Bonzini 18746f56631SVladimir Sementsov-Ogievskiy static void reconnect_delay_timer_del(BDRVNBDState *s) 18846f56631SVladimir Sementsov-Ogievskiy { 18946f56631SVladimir Sementsov-Ogievskiy if (s->reconnect_delay_timer) { 19046f56631SVladimir Sementsov-Ogievskiy timer_free(s->reconnect_delay_timer); 19146f56631SVladimir Sementsov-Ogievskiy s->reconnect_delay_timer = NULL; 19246f56631SVladimir Sementsov-Ogievskiy } 19346f56631SVladimir Sementsov-Ogievskiy } 19446f56631SVladimir Sementsov-Ogievskiy 19546f56631SVladimir Sementsov-Ogievskiy static void reconnect_delay_timer_cb(void *opaque) 19646f56631SVladimir Sementsov-Ogievskiy { 19746f56631SVladimir Sementsov-Ogievskiy BDRVNBDState *s = opaque; 19846f56631SVladimir Sementsov-Ogievskiy 19946f56631SVladimir Sementsov-Ogievskiy reconnect_delay_timer_del(s); 200dba5156cSPaolo Bonzini WITH_QEMU_LOCK_GUARD(&s->requests_lock) { 201dba5156cSPaolo Bonzini if (s->state != NBD_CLIENT_CONNECTING_WAIT) { 202dba5156cSPaolo Bonzini return; 203dba5156cSPaolo Bonzini } 204dba5156cSPaolo Bonzini s->state = NBD_CLIENT_CONNECTING_NOWAIT; 205dba5156cSPaolo Bonzini } 206dba5156cSPaolo Bonzini nbd_co_establish_connection_cancel(s->conn); 20746f56631SVladimir Sementsov-Ogievskiy } 20846f56631SVladimir Sementsov-Ogievskiy 20946f56631SVladimir Sementsov-Ogievskiy static void reconnect_delay_timer_init(BDRVNBDState *s, uint64_t expire_time_ns) 21046f56631SVladimir Sementsov-Ogievskiy { 21146f56631SVladimir Sementsov-Ogievskiy assert(!s->reconnect_delay_timer); 21246f56631SVladimir Sementsov-Ogievskiy s->reconnect_delay_timer = aio_timer_new(bdrv_get_aio_context(s->bs), 21346f56631SVladimir Sementsov-Ogievskiy QEMU_CLOCK_REALTIME, 21446f56631SVladimir Sementsov-Ogievskiy SCALE_NS, 21546f56631SVladimir Sementsov-Ogievskiy reconnect_delay_timer_cb, s); 21646f56631SVladimir Sementsov-Ogievskiy timer_mod(s->reconnect_delay_timer, expire_time_ns); 21746f56631SVladimir Sementsov-Ogievskiy } 21846f56631SVladimir Sementsov-Ogievskiy 21986f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_teardown_connection(BlockDriverState *bs) 22086f8cdf3SVladimir Sementsov-Ogievskiy { 221611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 22286f8cdf3SVladimir Sementsov-Ogievskiy 2234ddb5d2fSVladimir Sementsov-Ogievskiy assert(!s->in_flight); 2244ddb5d2fSVladimir Sementsov-Ogievskiy 225fbeb3e63SVladimir Sementsov-Ogievskiy if (s->ioc) { 226f7651539SVladimir Sementsov-Ogievskiy qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); 2274ddb5d2fSVladimir Sementsov-Ogievskiy yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), 2284ddb5d2fSVladimir Sementsov-Ogievskiy nbd_yank, s->bs); 2294ddb5d2fSVladimir Sementsov-Ogievskiy object_unref(OBJECT(s->ioc)); 2304ddb5d2fSVladimir Sementsov-Ogievskiy s->ioc = NULL; 231f7651539SVladimir Sementsov-Ogievskiy } 232fbeb3e63SVladimir Sementsov-Ogievskiy 233dba5156cSPaolo Bonzini WITH_QEMU_LOCK_GUARD(&s->requests_lock) { 234f7651539SVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_QUIT; 23578c81a3fSMax Reitz } 236dba5156cSPaolo Bonzini } 23786f8cdf3SVladimir Sementsov-Ogievskiy 238be16b8bfSVladimir Sementsov-Ogievskiy static void open_timer_del(BDRVNBDState *s) 239be16b8bfSVladimir Sementsov-Ogievskiy { 240be16b8bfSVladimir Sementsov-Ogievskiy if (s->open_timer) { 241be16b8bfSVladimir Sementsov-Ogievskiy timer_free(s->open_timer); 242be16b8bfSVladimir Sementsov-Ogievskiy s->open_timer = NULL; 243be16b8bfSVladimir Sementsov-Ogievskiy } 244be16b8bfSVladimir Sementsov-Ogievskiy } 245be16b8bfSVladimir Sementsov-Ogievskiy 246be16b8bfSVladimir Sementsov-Ogievskiy static void open_timer_cb(void *opaque) 247be16b8bfSVladimir Sementsov-Ogievskiy { 248be16b8bfSVladimir Sementsov-Ogievskiy BDRVNBDState *s = opaque; 249be16b8bfSVladimir Sementsov-Ogievskiy 250be16b8bfSVladimir Sementsov-Ogievskiy nbd_co_establish_connection_cancel(s->conn); 251be16b8bfSVladimir Sementsov-Ogievskiy open_timer_del(s); 252be16b8bfSVladimir Sementsov-Ogievskiy } 253be16b8bfSVladimir Sementsov-Ogievskiy 254be16b8bfSVladimir Sementsov-Ogievskiy static void open_timer_init(BDRVNBDState *s, uint64_t expire_time_ns) 255be16b8bfSVladimir Sementsov-Ogievskiy { 256be16b8bfSVladimir Sementsov-Ogievskiy assert(!s->open_timer); 257be16b8bfSVladimir Sementsov-Ogievskiy s->open_timer = aio_timer_new(bdrv_get_aio_context(s->bs), 258be16b8bfSVladimir Sementsov-Ogievskiy QEMU_CLOCK_REALTIME, 259be16b8bfSVladimir Sementsov-Ogievskiy SCALE_NS, 260be16b8bfSVladimir Sementsov-Ogievskiy open_timer_cb, s); 261be16b8bfSVladimir Sementsov-Ogievskiy timer_mod(s->open_timer, expire_time_ns); 262be16b8bfSVladimir Sementsov-Ogievskiy } 263be16b8bfSVladimir Sementsov-Ogievskiy 2648d45185cSPaolo Bonzini static bool nbd_client_will_reconnect(BDRVNBDState *s) 2658d45185cSPaolo Bonzini { 266dba5156cSPaolo Bonzini /* 267dba5156cSPaolo Bonzini * Called only after a socket error, so this is not performance sensitive. 268dba5156cSPaolo Bonzini */ 269dba5156cSPaolo Bonzini QEMU_LOCK_GUARD(&s->requests_lock); 270dba5156cSPaolo Bonzini return s->state == NBD_CLIENT_CONNECTING_WAIT; 2718d45185cSPaolo Bonzini } 272dba5156cSPaolo Bonzini 273e9ba7788SVladimir Sementsov-Ogievskiy /* 274e9ba7788SVladimir Sementsov-Ogievskiy * Update @bs with information learned during a completed negotiation process. 275e9ba7788SVladimir Sementsov-Ogievskiy * Return failure if the server's advertised options are incompatible with the 276e9ba7788SVladimir Sementsov-Ogievskiy * client's needs. 277e9ba7788SVladimir Sementsov-Ogievskiy */ 278e9ba7788SVladimir Sementsov-Ogievskiy static int nbd_handle_updated_info(BlockDriverState *bs, Error **errp) 279e9ba7788SVladimir Sementsov-Ogievskiy { 280e9ba7788SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 281e9ba7788SVladimir Sementsov-Ogievskiy int ret; 282e9ba7788SVladimir Sementsov-Ogievskiy 283e9ba7788SVladimir Sementsov-Ogievskiy if (s->x_dirty_bitmap) { 284e9ba7788SVladimir Sementsov-Ogievskiy if (!s->info.base_allocation) { 285e9ba7788SVladimir Sementsov-Ogievskiy error_setg(errp, "requested x-dirty-bitmap %s not found", 286e9ba7788SVladimir Sementsov-Ogievskiy s->x_dirty_bitmap); 287e9ba7788SVladimir Sementsov-Ogievskiy return -EINVAL; 288e9ba7788SVladimir Sementsov-Ogievskiy } 289e9ba7788SVladimir Sementsov-Ogievskiy if (strcmp(s->x_dirty_bitmap, "qemu:allocation-depth") == 0) { 290e9ba7788SVladimir Sementsov-Ogievskiy s->alloc_depth = true; 291e9ba7788SVladimir Sementsov-Ogievskiy } 292e9ba7788SVladimir Sementsov-Ogievskiy } 293e9ba7788SVladimir Sementsov-Ogievskiy 294e9ba7788SVladimir Sementsov-Ogievskiy if (s->info.flags & NBD_FLAG_READ_ONLY) { 295e9ba7788SVladimir Sementsov-Ogievskiy ret = bdrv_apply_auto_read_only(bs, "NBD export is read-only", errp); 296e9ba7788SVladimir Sementsov-Ogievskiy if (ret < 0) { 297e9ba7788SVladimir Sementsov-Ogievskiy return ret; 298e9ba7788SVladimir Sementsov-Ogievskiy } 299e9ba7788SVladimir Sementsov-Ogievskiy } 300e9ba7788SVladimir Sementsov-Ogievskiy 301e9ba7788SVladimir Sementsov-Ogievskiy if (s->info.flags & NBD_FLAG_SEND_FUA) { 302e9ba7788SVladimir Sementsov-Ogievskiy bs->supported_write_flags = BDRV_REQ_FUA; 303e9ba7788SVladimir Sementsov-Ogievskiy bs->supported_zero_flags |= BDRV_REQ_FUA; 304e9ba7788SVladimir Sementsov-Ogievskiy } 305e9ba7788SVladimir Sementsov-Ogievskiy 306e9ba7788SVladimir Sementsov-Ogievskiy if (s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES) { 307e9ba7788SVladimir Sementsov-Ogievskiy bs->supported_zero_flags |= BDRV_REQ_MAY_UNMAP; 308e9ba7788SVladimir Sementsov-Ogievskiy if (s->info.flags & NBD_FLAG_SEND_FAST_ZERO) { 309e9ba7788SVladimir Sementsov-Ogievskiy bs->supported_zero_flags |= BDRV_REQ_NO_FALLBACK; 310e9ba7788SVladimir Sementsov-Ogievskiy } 311e9ba7788SVladimir Sementsov-Ogievskiy } 312e9ba7788SVladimir Sementsov-Ogievskiy 313e9ba7788SVladimir Sementsov-Ogievskiy trace_nbd_client_handshake_success(s->export); 314e9ba7788SVladimir Sementsov-Ogievskiy 315e9ba7788SVladimir Sementsov-Ogievskiy return 0; 316e9ba7788SVladimir Sementsov-Ogievskiy } 317e9ba7788SVladimir Sementsov-Ogievskiy 318a71d597bSVladimir Sementsov-Ogievskiy int coroutine_fn nbd_co_do_establish_connection(BlockDriverState *bs, 3198610b449SPaolo Bonzini bool blocking, Error **errp) 32051edbf53SVladimir Sementsov-Ogievskiy { 32151edbf53SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 32251edbf53SVladimir Sementsov-Ogievskiy int ret; 3231581a70dSEmanuele Giuseppe Esposito IO_CODE(); 32451edbf53SVladimir Sementsov-Ogievskiy 32569aa0d37SEmanuele Giuseppe Esposito assert_bdrv_graph_readable(); 32651edbf53SVladimir Sementsov-Ogievskiy assert(!s->ioc); 32751edbf53SVladimir Sementsov-Ogievskiy 3284ddb5d2fSVladimir Sementsov-Ogievskiy s->ioc = nbd_co_establish_connection(s->conn, &s->info, blocking, errp); 32951edbf53SVladimir Sementsov-Ogievskiy if (!s->ioc) { 33051edbf53SVladimir Sementsov-Ogievskiy return -ECONNREFUSED; 33151edbf53SVladimir Sementsov-Ogievskiy } 33251edbf53SVladimir Sementsov-Ogievskiy 3330b9cd6b9SLukas Straub yank_register_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), nbd_yank, 3340b9cd6b9SLukas Straub bs); 3350b9cd6b9SLukas Straub 33651edbf53SVladimir Sementsov-Ogievskiy ret = nbd_handle_updated_info(s->bs, NULL); 33751edbf53SVladimir Sementsov-Ogievskiy if (ret < 0) { 33851edbf53SVladimir Sementsov-Ogievskiy /* 33951edbf53SVladimir Sementsov-Ogievskiy * We have connected, but must fail for other reasons. 34051edbf53SVladimir Sementsov-Ogievskiy * Send NBD_CMD_DISC as a courtesy to the server. 34151edbf53SVladimir Sementsov-Ogievskiy */ 34251edbf53SVladimir Sementsov-Ogievskiy NBDRequest request = { .type = NBD_CMD_DISC }; 34351edbf53SVladimir Sementsov-Ogievskiy 34451edbf53SVladimir Sementsov-Ogievskiy nbd_send_request(s->ioc, &request); 34551edbf53SVladimir Sementsov-Ogievskiy 3460b9cd6b9SLukas Straub yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), 3470b9cd6b9SLukas Straub nbd_yank, bs); 34851edbf53SVladimir Sementsov-Ogievskiy object_unref(OBJECT(s->ioc)); 34951edbf53SVladimir Sementsov-Ogievskiy s->ioc = NULL; 35051edbf53SVladimir Sementsov-Ogievskiy 35151edbf53SVladimir Sementsov-Ogievskiy return ret; 35251edbf53SVladimir Sementsov-Ogievskiy } 35351edbf53SVladimir Sementsov-Ogievskiy 35451edbf53SVladimir Sementsov-Ogievskiy qio_channel_set_blocking(s->ioc, false, NULL); 355*06e0f098SStefan Hajnoczi qio_channel_set_follow_coroutine_ctx(s->ioc, true); 35651edbf53SVladimir Sementsov-Ogievskiy 35751edbf53SVladimir Sementsov-Ogievskiy /* successfully connected */ 358dba5156cSPaolo Bonzini WITH_QEMU_LOCK_GUARD(&s->requests_lock) { 35951edbf53SVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_CONNECTED; 360dba5156cSPaolo Bonzini } 36151edbf53SVladimir Sementsov-Ogievskiy 36251edbf53SVladimir Sementsov-Ogievskiy return 0; 36351edbf53SVladimir Sementsov-Ogievskiy } 36451edbf53SVladimir Sementsov-Ogievskiy 365dba5156cSPaolo Bonzini /* Called with s->requests_lock held. */ 3668d45185cSPaolo Bonzini static bool nbd_client_connecting(BDRVNBDState *s) 3678d45185cSPaolo Bonzini { 368dba5156cSPaolo Bonzini return s->state == NBD_CLIENT_CONNECTING_WAIT || 369dba5156cSPaolo Bonzini s->state == NBD_CLIENT_CONNECTING_NOWAIT; 3708d45185cSPaolo Bonzini } 3718d45185cSPaolo Bonzini 372ee19d953SPaolo Bonzini /* Called with s->requests_lock taken. */ 37369aa0d37SEmanuele Giuseppe Esposito static void coroutine_fn GRAPH_RDLOCK nbd_reconnect_attempt(BDRVNBDState *s) 374f7651539SVladimir Sementsov-Ogievskiy { 3758bb100c9SDenis V. Lunev int ret; 376dba5156cSPaolo Bonzini bool blocking = s->state == NBD_CLIENT_CONNECTING_WAIT; 377f7651539SVladimir Sementsov-Ogievskiy 378f7651539SVladimir Sementsov-Ogievskiy /* 379f7651539SVladimir Sementsov-Ogievskiy * Now we are sure that nobody is accessing the channel, and no one will 380f7651539SVladimir Sementsov-Ogievskiy * try until we set the state to CONNECTED. 381f7651539SVladimir Sementsov-Ogievskiy */ 3828610b449SPaolo Bonzini assert(nbd_client_connecting(s)); 3838610b449SPaolo Bonzini assert(s->in_flight == 1); 3848610b449SPaolo Bonzini 3858bb100c9SDenis V. Lunev trace_nbd_reconnect_attempt(s->bs->in_flight); 3868bb100c9SDenis V. Lunev 3878610b449SPaolo Bonzini if (blocking && !s->reconnect_delay_timer) { 3888610b449SPaolo Bonzini /* 3898610b449SPaolo Bonzini * It's the first reconnect attempt after switching to 3908610b449SPaolo Bonzini * NBD_CLIENT_CONNECTING_WAIT 3918610b449SPaolo Bonzini */ 3928610b449SPaolo Bonzini g_assert(s->reconnect_delay); 3938610b449SPaolo Bonzini reconnect_delay_timer_init(s, 3948610b449SPaolo Bonzini qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 3958610b449SPaolo Bonzini s->reconnect_delay * NANOSECONDS_PER_SECOND); 3968610b449SPaolo Bonzini } 397f7651539SVladimir Sementsov-Ogievskiy 398f7651539SVladimir Sementsov-Ogievskiy /* Finalize previous connection if any */ 399f7651539SVladimir Sementsov-Ogievskiy if (s->ioc) { 400fee091cdSLukas Straub yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), 401fee091cdSLukas Straub nbd_yank, s->bs); 402611ae1d7SVladimir Sementsov-Ogievskiy object_unref(OBJECT(s->ioc)); 403611ae1d7SVladimir Sementsov-Ogievskiy s->ioc = NULL; 40486f8cdf3SVladimir Sementsov-Ogievskiy } 40586f8cdf3SVladimir Sementsov-Ogievskiy 406ee19d953SPaolo Bonzini qemu_mutex_unlock(&s->requests_lock); 4078bb100c9SDenis V. Lunev ret = nbd_co_do_establish_connection(s->bs, blocking, NULL); 4088bb100c9SDenis V. Lunev trace_nbd_reconnect_attempt_result(ret, s->bs->in_flight); 409ee19d953SPaolo Bonzini qemu_mutex_lock(&s->requests_lock); 4103ce1fc16SHanna Reitz 4113ce1fc16SHanna Reitz /* 4123ce1fc16SHanna Reitz * The reconnect attempt is done (maybe successfully, maybe not), so 4133ce1fc16SHanna Reitz * we no longer need this timer. Delete it so it will not outlive 4143ce1fc16SHanna Reitz * this I/O request (so draining removes all timers). 4153ce1fc16SHanna Reitz */ 4163ce1fc16SHanna Reitz reconnect_delay_timer_del(s); 417fa35591bSVladimir Sementsov-Ogievskiy } 418f7651539SVladimir Sementsov-Ogievskiy 41922efd811SEric Blake static coroutine_fn int nbd_receive_replies(BDRVNBDState *s, uint64_t cookie) 420f7651539SVladimir Sementsov-Ogievskiy { 4214ddb5d2fSVladimir Sementsov-Ogievskiy int ret; 4228cb98a72SEric Blake uint64_t ind = COOKIE_TO_INDEX(cookie), ind2; 4234ddb5d2fSVladimir Sementsov-Ogievskiy QEMU_LOCK_GUARD(&s->receive_mutex); 424f7651539SVladimir Sementsov-Ogievskiy 4254ddb5d2fSVladimir Sementsov-Ogievskiy while (true) { 42622efd811SEric Blake if (s->reply.cookie == cookie) { 4274ddb5d2fSVladimir Sementsov-Ogievskiy /* We are done */ 4284ddb5d2fSVladimir Sementsov-Ogievskiy return 0; 429f7651539SVladimir Sementsov-Ogievskiy } 430f7651539SVladimir Sementsov-Ogievskiy 43122efd811SEric Blake if (s->reply.cookie != 0) { 43286f8cdf3SVladimir Sementsov-Ogievskiy /* 4334ddb5d2fSVladimir Sementsov-Ogievskiy * Some other request is being handled now. It should already be 43422efd811SEric Blake * woken by whoever set s->reply.cookie (or never wait in this 4354ddb5d2fSVladimir Sementsov-Ogievskiy * yield). So, we should not wake it here. 43686f8cdf3SVladimir Sementsov-Ogievskiy */ 4378cb98a72SEric Blake ind2 = COOKIE_TO_INDEX(s->reply.cookie); 4384ddb5d2fSVladimir Sementsov-Ogievskiy assert(!s->requests[ind2].receiving); 43986f8cdf3SVladimir Sementsov-Ogievskiy 4404ddb5d2fSVladimir Sementsov-Ogievskiy s->requests[ind].receiving = true; 4414ddb5d2fSVladimir Sementsov-Ogievskiy qemu_co_mutex_unlock(&s->receive_mutex); 4424ddb5d2fSVladimir Sementsov-Ogievskiy 44386f8cdf3SVladimir Sementsov-Ogievskiy qemu_coroutine_yield(); 4444ddb5d2fSVladimir Sementsov-Ogievskiy /* 445a80a9a1cSPaolo Bonzini * We may be woken for 2 reasons: 4464ddb5d2fSVladimir Sementsov-Ogievskiy * 1. From this function, executing in parallel coroutine, when our 44722efd811SEric Blake * cookie is received. 448a80a9a1cSPaolo Bonzini * 2. From nbd_co_receive_one_chunk(), when previous request is 44922efd811SEric Blake * finished and s->reply.cookie set to 0. 4504ddb5d2fSVladimir Sementsov-Ogievskiy * Anyway, it's OK to lock the mutex and go to the next iteration. 4514ddb5d2fSVladimir Sementsov-Ogievskiy */ 4524ddb5d2fSVladimir Sementsov-Ogievskiy 4534ddb5d2fSVladimir Sementsov-Ogievskiy qemu_co_mutex_lock(&s->receive_mutex); 4544ddb5d2fSVladimir Sementsov-Ogievskiy assert(!s->requests[ind].receiving); 4554ddb5d2fSVladimir Sementsov-Ogievskiy continue; 45686f8cdf3SVladimir Sementsov-Ogievskiy } 45786f8cdf3SVladimir Sementsov-Ogievskiy 45822efd811SEric Blake /* We are under mutex and cookie is 0. We have to do the dirty work. */ 45922efd811SEric Blake assert(s->reply.cookie == 0); 4604ddb5d2fSVladimir Sementsov-Ogievskiy ret = nbd_receive_reply(s->bs, s->ioc, &s->reply, NULL); 4614ddb5d2fSVladimir Sementsov-Ogievskiy if (ret <= 0) { 4624ddb5d2fSVladimir Sementsov-Ogievskiy ret = ret ? ret : -EIO; 4634ddb5d2fSVladimir Sementsov-Ogievskiy nbd_channel_error(s, ret); 4644ddb5d2fSVladimir Sementsov-Ogievskiy return ret; 465f7651539SVladimir Sementsov-Ogievskiy } 4664ddb5d2fSVladimir Sementsov-Ogievskiy if (nbd_reply_is_structured(&s->reply) && !s->info.structured_reply) { 4674ddb5d2fSVladimir Sementsov-Ogievskiy nbd_channel_error(s, -EINVAL); 4684ddb5d2fSVladimir Sementsov-Ogievskiy return -EINVAL; 46978c81a3fSMax Reitz } 4708cb98a72SEric Blake ind2 = COOKIE_TO_INDEX(s->reply.cookie); 4718846b7d1SPaolo Bonzini if (ind2 >= MAX_NBD_REQUESTS || !s->requests[ind2].coroutine) { 4728846b7d1SPaolo Bonzini nbd_channel_error(s, -EINVAL); 4738846b7d1SPaolo Bonzini return -EINVAL; 4748846b7d1SPaolo Bonzini } 47522efd811SEric Blake if (s->reply.cookie == cookie) { 4764ddb5d2fSVladimir Sementsov-Ogievskiy /* We are done */ 4774ddb5d2fSVladimir Sementsov-Ogievskiy return 0; 4784ddb5d2fSVladimir Sementsov-Ogievskiy } 4794ddb5d2fSVladimir Sementsov-Ogievskiy nbd_recv_coroutine_wake_one(&s->requests[ind2]); 4804ddb5d2fSVladimir Sementsov-Ogievskiy } 48186f8cdf3SVladimir Sementsov-Ogievskiy } 48286f8cdf3SVladimir Sementsov-Ogievskiy 48369aa0d37SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK 48469aa0d37SEmanuele Giuseppe Esposito nbd_co_send_request(BlockDriverState *bs, NBDRequest *request, 48586f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector *qiov) 48686f8cdf3SVladimir Sementsov-Ogievskiy { 487611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 488a34b1e5eSVladimir Sementsov-Ogievskiy int rc, i = -1; 48986f8cdf3SVladimir Sementsov-Ogievskiy 490ee19d953SPaolo Bonzini qemu_mutex_lock(&s->requests_lock); 4914ddb5d2fSVladimir Sementsov-Ogievskiy while (s->in_flight == MAX_NBD_REQUESTS || 492dba5156cSPaolo Bonzini (s->state != NBD_CLIENT_CONNECTED && s->in_flight > 0)) { 493ee19d953SPaolo Bonzini qemu_co_queue_wait(&s->free_sema, &s->requests_lock); 49486f8cdf3SVladimir Sementsov-Ogievskiy } 495a34b1e5eSVladimir Sementsov-Ogievskiy 4968610b449SPaolo Bonzini s->in_flight++; 497dba5156cSPaolo Bonzini if (s->state != NBD_CLIENT_CONNECTED) { 4984ddb5d2fSVladimir Sementsov-Ogievskiy if (nbd_client_connecting(s)) { 4994ddb5d2fSVladimir Sementsov-Ogievskiy nbd_reconnect_attempt(s); 5008610b449SPaolo Bonzini qemu_co_queue_restart_all(&s->free_sema); 5014ddb5d2fSVladimir Sementsov-Ogievskiy } 502dba5156cSPaolo Bonzini if (s->state != NBD_CLIENT_CONNECTED) { 503a34b1e5eSVladimir Sementsov-Ogievskiy rc = -EIO; 504a34b1e5eSVladimir Sementsov-Ogievskiy goto err; 505a34b1e5eSVladimir Sementsov-Ogievskiy } 5068610b449SPaolo Bonzini } 50786f8cdf3SVladimir Sementsov-Ogievskiy 50886f8cdf3SVladimir Sementsov-Ogievskiy for (i = 0; i < MAX_NBD_REQUESTS; i++) { 50986f8cdf3SVladimir Sementsov-Ogievskiy if (s->requests[i].coroutine == NULL) { 51086f8cdf3SVladimir Sementsov-Ogievskiy break; 51186f8cdf3SVladimir Sementsov-Ogievskiy } 51286f8cdf3SVladimir Sementsov-Ogievskiy } 51386f8cdf3SVladimir Sementsov-Ogievskiy 51486f8cdf3SVladimir Sementsov-Ogievskiy assert(i < MAX_NBD_REQUESTS); 51586f8cdf3SVladimir Sementsov-Ogievskiy s->requests[i].coroutine = qemu_coroutine_self(); 51686f8cdf3SVladimir Sementsov-Ogievskiy s->requests[i].offset = request->from; 51786f8cdf3SVladimir Sementsov-Ogievskiy s->requests[i].receiving = false; 518ee19d953SPaolo Bonzini qemu_mutex_unlock(&s->requests_lock); 51986f8cdf3SVladimir Sementsov-Ogievskiy 520ee19d953SPaolo Bonzini qemu_co_mutex_lock(&s->send_mutex); 5218cb98a72SEric Blake request->cookie = INDEX_TO_COOKIE(i); 52286f8cdf3SVladimir Sementsov-Ogievskiy 52386f8cdf3SVladimir Sementsov-Ogievskiy assert(s->ioc); 52486f8cdf3SVladimir Sementsov-Ogievskiy 52586f8cdf3SVladimir Sementsov-Ogievskiy if (qiov) { 52686f8cdf3SVladimir Sementsov-Ogievskiy qio_channel_set_cork(s->ioc, true); 52786f8cdf3SVladimir Sementsov-Ogievskiy rc = nbd_send_request(s->ioc, request); 5282866ddd1SEric Blake if (rc >= 0 && qio_channel_writev_all(s->ioc, qiov->iov, qiov->niov, 52986f8cdf3SVladimir Sementsov-Ogievskiy NULL) < 0) { 53086f8cdf3SVladimir Sementsov-Ogievskiy rc = -EIO; 53186f8cdf3SVladimir Sementsov-Ogievskiy } 53286f8cdf3SVladimir Sementsov-Ogievskiy qio_channel_set_cork(s->ioc, false); 53386f8cdf3SVladimir Sementsov-Ogievskiy } else { 53486f8cdf3SVladimir Sementsov-Ogievskiy rc = nbd_send_request(s->ioc, request); 53586f8cdf3SVladimir Sementsov-Ogievskiy } 536ee19d953SPaolo Bonzini qemu_co_mutex_unlock(&s->send_mutex); 53786f8cdf3SVladimir Sementsov-Ogievskiy 53886f8cdf3SVladimir Sementsov-Ogievskiy if (rc < 0) { 539ee19d953SPaolo Bonzini qemu_mutex_lock(&s->requests_lock); 540ee19d953SPaolo Bonzini err: 541dba5156cSPaolo Bonzini nbd_channel_error_locked(s, rc); 542a34b1e5eSVladimir Sementsov-Ogievskiy if (i != -1) { 54386f8cdf3SVladimir Sementsov-Ogievskiy s->requests[i].coroutine = NULL; 54486f8cdf3SVladimir Sementsov-Ogievskiy } 5458610b449SPaolo Bonzini s->in_flight--; 5466690302bSRao Lei qemu_co_queue_next(&s->free_sema); 547ee19d953SPaolo Bonzini qemu_mutex_unlock(&s->requests_lock); 548f7651539SVladimir Sementsov-Ogievskiy } 54986f8cdf3SVladimir Sementsov-Ogievskiy return rc; 55086f8cdf3SVladimir Sementsov-Ogievskiy } 55186f8cdf3SVladimir Sementsov-Ogievskiy 55286f8cdf3SVladimir Sementsov-Ogievskiy static inline uint16_t payload_advance16(uint8_t **payload) 55386f8cdf3SVladimir Sementsov-Ogievskiy { 55486f8cdf3SVladimir Sementsov-Ogievskiy *payload += 2; 55586f8cdf3SVladimir Sementsov-Ogievskiy return lduw_be_p(*payload - 2); 55686f8cdf3SVladimir Sementsov-Ogievskiy } 55786f8cdf3SVladimir Sementsov-Ogievskiy 55886f8cdf3SVladimir Sementsov-Ogievskiy static inline uint32_t payload_advance32(uint8_t **payload) 55986f8cdf3SVladimir Sementsov-Ogievskiy { 56086f8cdf3SVladimir Sementsov-Ogievskiy *payload += 4; 56186f8cdf3SVladimir Sementsov-Ogievskiy return ldl_be_p(*payload - 4); 56286f8cdf3SVladimir Sementsov-Ogievskiy } 56386f8cdf3SVladimir Sementsov-Ogievskiy 56486f8cdf3SVladimir Sementsov-Ogievskiy static inline uint64_t payload_advance64(uint8_t **payload) 56586f8cdf3SVladimir Sementsov-Ogievskiy { 56686f8cdf3SVladimir Sementsov-Ogievskiy *payload += 8; 56786f8cdf3SVladimir Sementsov-Ogievskiy return ldq_be_p(*payload - 8); 56886f8cdf3SVladimir Sementsov-Ogievskiy } 56986f8cdf3SVladimir Sementsov-Ogievskiy 570611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_parse_offset_hole_payload(BDRVNBDState *s, 57186f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk, 57286f8cdf3SVladimir Sementsov-Ogievskiy uint8_t *payload, uint64_t orig_offset, 57386f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector *qiov, Error **errp) 57486f8cdf3SVladimir Sementsov-Ogievskiy { 57586f8cdf3SVladimir Sementsov-Ogievskiy uint64_t offset; 57686f8cdf3SVladimir Sementsov-Ogievskiy uint32_t hole_size; 57786f8cdf3SVladimir Sementsov-Ogievskiy 57886f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length != sizeof(offset) + sizeof(hole_size)) { 57986f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: invalid payload for " 58086f8cdf3SVladimir Sementsov-Ogievskiy "NBD_REPLY_TYPE_OFFSET_HOLE"); 58186f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 58286f8cdf3SVladimir Sementsov-Ogievskiy } 58386f8cdf3SVladimir Sementsov-Ogievskiy 58486f8cdf3SVladimir Sementsov-Ogievskiy offset = payload_advance64(&payload); 58586f8cdf3SVladimir Sementsov-Ogievskiy hole_size = payload_advance32(&payload); 58686f8cdf3SVladimir Sementsov-Ogievskiy 58786f8cdf3SVladimir Sementsov-Ogievskiy if (!hole_size || offset < orig_offset || hole_size > qiov->size || 58886f8cdf3SVladimir Sementsov-Ogievskiy offset > orig_offset + qiov->size - hole_size) { 58986f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent chunk exceeding requested" 59086f8cdf3SVladimir Sementsov-Ogievskiy " region"); 59186f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 59286f8cdf3SVladimir Sementsov-Ogievskiy } 593611ae1d7SVladimir Sementsov-Ogievskiy if (s->info.min_block && 594611ae1d7SVladimir Sementsov-Ogievskiy !QEMU_IS_ALIGNED(hole_size, s->info.min_block)) { 59586f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_structured_read_compliance("hole"); 59686f8cdf3SVladimir Sementsov-Ogievskiy } 59786f8cdf3SVladimir Sementsov-Ogievskiy 59886f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_memset(qiov, offset - orig_offset, 0, hole_size); 59986f8cdf3SVladimir Sementsov-Ogievskiy 60086f8cdf3SVladimir Sementsov-Ogievskiy return 0; 60186f8cdf3SVladimir Sementsov-Ogievskiy } 60286f8cdf3SVladimir Sementsov-Ogievskiy 60386f8cdf3SVladimir Sementsov-Ogievskiy /* 60486f8cdf3SVladimir Sementsov-Ogievskiy * nbd_parse_blockstatus_payload 60586f8cdf3SVladimir Sementsov-Ogievskiy * Based on our request, we expect only one extent in reply, for the 60686f8cdf3SVladimir Sementsov-Ogievskiy * base:allocation context. 60786f8cdf3SVladimir Sementsov-Ogievskiy */ 608611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_parse_blockstatus_payload(BDRVNBDState *s, 60986f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk, 61086f8cdf3SVladimir Sementsov-Ogievskiy uint8_t *payload, uint64_t orig_length, 61186f8cdf3SVladimir Sementsov-Ogievskiy NBDExtent *extent, Error **errp) 61286f8cdf3SVladimir Sementsov-Ogievskiy { 61386f8cdf3SVladimir Sementsov-Ogievskiy uint32_t context_id; 61486f8cdf3SVladimir Sementsov-Ogievskiy 61586f8cdf3SVladimir Sementsov-Ogievskiy /* The server succeeded, so it must have sent [at least] one extent */ 61686f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length < sizeof(context_id) + sizeof(*extent)) { 61786f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: invalid payload for " 61886f8cdf3SVladimir Sementsov-Ogievskiy "NBD_REPLY_TYPE_BLOCK_STATUS"); 61986f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 62086f8cdf3SVladimir Sementsov-Ogievskiy } 62186f8cdf3SVladimir Sementsov-Ogievskiy 62286f8cdf3SVladimir Sementsov-Ogievskiy context_id = payload_advance32(&payload); 623611ae1d7SVladimir Sementsov-Ogievskiy if (s->info.context_id != context_id) { 62486f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: unexpected context id %d for " 62586f8cdf3SVladimir Sementsov-Ogievskiy "NBD_REPLY_TYPE_BLOCK_STATUS, when negotiated context " 62686f8cdf3SVladimir Sementsov-Ogievskiy "id is %d", context_id, 627611ae1d7SVladimir Sementsov-Ogievskiy s->info.context_id); 62886f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 62986f8cdf3SVladimir Sementsov-Ogievskiy } 63086f8cdf3SVladimir Sementsov-Ogievskiy 63186f8cdf3SVladimir Sementsov-Ogievskiy extent->length = payload_advance32(&payload); 63286f8cdf3SVladimir Sementsov-Ogievskiy extent->flags = payload_advance32(&payload); 63386f8cdf3SVladimir Sementsov-Ogievskiy 63486f8cdf3SVladimir Sementsov-Ogievskiy if (extent->length == 0) { 63586f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent status chunk with " 63686f8cdf3SVladimir Sementsov-Ogievskiy "zero length"); 63786f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 63886f8cdf3SVladimir Sementsov-Ogievskiy } 63986f8cdf3SVladimir Sementsov-Ogievskiy 64086f8cdf3SVladimir Sementsov-Ogievskiy /* 64186f8cdf3SVladimir Sementsov-Ogievskiy * A server sending unaligned block status is in violation of the 64286f8cdf3SVladimir Sementsov-Ogievskiy * protocol, but as qemu-nbd 3.1 is such a server (at least for 64386f8cdf3SVladimir Sementsov-Ogievskiy * POSIX files that are not a multiple of 512 bytes, since qemu 64486f8cdf3SVladimir Sementsov-Ogievskiy * rounds files up to 512-byte multiples but lseek(SEEK_HOLE) 64586f8cdf3SVladimir Sementsov-Ogievskiy * still sees an implicit hole beyond the real EOF), it's nicer to 64686f8cdf3SVladimir Sementsov-Ogievskiy * work around the misbehaving server. If the request included 64786f8cdf3SVladimir Sementsov-Ogievskiy * more than the final unaligned block, truncate it back to an 64886f8cdf3SVladimir Sementsov-Ogievskiy * aligned result; if the request was only the final block, round 64986f8cdf3SVladimir Sementsov-Ogievskiy * up to the full block and change the status to fully-allocated 65086f8cdf3SVladimir Sementsov-Ogievskiy * (always a safe status, even if it loses information). 65186f8cdf3SVladimir Sementsov-Ogievskiy */ 652611ae1d7SVladimir Sementsov-Ogievskiy if (s->info.min_block && !QEMU_IS_ALIGNED(extent->length, 653611ae1d7SVladimir Sementsov-Ogievskiy s->info.min_block)) { 65486f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_parse_blockstatus_compliance("extent length is unaligned"); 655611ae1d7SVladimir Sementsov-Ogievskiy if (extent->length > s->info.min_block) { 65686f8cdf3SVladimir Sementsov-Ogievskiy extent->length = QEMU_ALIGN_DOWN(extent->length, 657611ae1d7SVladimir Sementsov-Ogievskiy s->info.min_block); 65886f8cdf3SVladimir Sementsov-Ogievskiy } else { 659611ae1d7SVladimir Sementsov-Ogievskiy extent->length = s->info.min_block; 66086f8cdf3SVladimir Sementsov-Ogievskiy extent->flags = 0; 66186f8cdf3SVladimir Sementsov-Ogievskiy } 66286f8cdf3SVladimir Sementsov-Ogievskiy } 66386f8cdf3SVladimir Sementsov-Ogievskiy 66486f8cdf3SVladimir Sementsov-Ogievskiy /* 66586f8cdf3SVladimir Sementsov-Ogievskiy * We used NBD_CMD_FLAG_REQ_ONE, so the server should not have 66686f8cdf3SVladimir Sementsov-Ogievskiy * sent us any more than one extent, nor should it have included 66786f8cdf3SVladimir Sementsov-Ogievskiy * status beyond our request in that extent. However, it's easy 66886f8cdf3SVladimir Sementsov-Ogievskiy * enough to ignore the server's noncompliance without killing the 66986f8cdf3SVladimir Sementsov-Ogievskiy * connection; just ignore trailing extents, and clamp things to 67086f8cdf3SVladimir Sementsov-Ogievskiy * the length of our request. 67186f8cdf3SVladimir Sementsov-Ogievskiy */ 67286f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length > sizeof(context_id) + sizeof(*extent)) { 67386f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_parse_blockstatus_compliance("more than one extent"); 67486f8cdf3SVladimir Sementsov-Ogievskiy } 67586f8cdf3SVladimir Sementsov-Ogievskiy if (extent->length > orig_length) { 67686f8cdf3SVladimir Sementsov-Ogievskiy extent->length = orig_length; 67786f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_parse_blockstatus_compliance("extent length too large"); 67886f8cdf3SVladimir Sementsov-Ogievskiy } 67986f8cdf3SVladimir Sementsov-Ogievskiy 680dbc7b014SEric Blake /* 681dbc7b014SEric Blake * HACK: if we are using x-dirty-bitmaps to access 682dbc7b014SEric Blake * qemu:allocation-depth, treat all depths > 2 the same as 2, 683dbc7b014SEric Blake * since nbd_client_co_block_status is only expecting the low two 684dbc7b014SEric Blake * bits to be set. 685dbc7b014SEric Blake */ 686dbc7b014SEric Blake if (s->alloc_depth && extent->flags > 2) { 687dbc7b014SEric Blake extent->flags = 2; 688dbc7b014SEric Blake } 689dbc7b014SEric Blake 69086f8cdf3SVladimir Sementsov-Ogievskiy return 0; 69186f8cdf3SVladimir Sementsov-Ogievskiy } 69286f8cdf3SVladimir Sementsov-Ogievskiy 69386f8cdf3SVladimir Sementsov-Ogievskiy /* 69486f8cdf3SVladimir Sementsov-Ogievskiy * nbd_parse_error_payload 69586f8cdf3SVladimir Sementsov-Ogievskiy * on success @errp contains message describing nbd error reply 69686f8cdf3SVladimir Sementsov-Ogievskiy */ 69786f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_parse_error_payload(NBDStructuredReplyChunk *chunk, 69886f8cdf3SVladimir Sementsov-Ogievskiy uint8_t *payload, int *request_ret, 69986f8cdf3SVladimir Sementsov-Ogievskiy Error **errp) 70086f8cdf3SVladimir Sementsov-Ogievskiy { 70186f8cdf3SVladimir Sementsov-Ogievskiy uint32_t error; 70286f8cdf3SVladimir Sementsov-Ogievskiy uint16_t message_size; 70386f8cdf3SVladimir Sementsov-Ogievskiy 70486f8cdf3SVladimir Sementsov-Ogievskiy assert(chunk->type & (1 << 15)); 70586f8cdf3SVladimir Sementsov-Ogievskiy 70686f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length < sizeof(error) + sizeof(message_size)) { 70786f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, 70886f8cdf3SVladimir Sementsov-Ogievskiy "Protocol error: invalid payload for structured error"); 70986f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 71086f8cdf3SVladimir Sementsov-Ogievskiy } 71186f8cdf3SVladimir Sementsov-Ogievskiy 71286f8cdf3SVladimir Sementsov-Ogievskiy error = nbd_errno_to_system_errno(payload_advance32(&payload)); 71386f8cdf3SVladimir Sementsov-Ogievskiy if (error == 0) { 71486f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent structured error chunk " 71586f8cdf3SVladimir Sementsov-Ogievskiy "with error = 0"); 71686f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 71786f8cdf3SVladimir Sementsov-Ogievskiy } 71886f8cdf3SVladimir Sementsov-Ogievskiy 71986f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = -error; 72086f8cdf3SVladimir Sementsov-Ogievskiy message_size = payload_advance16(&payload); 72186f8cdf3SVladimir Sementsov-Ogievskiy 72286f8cdf3SVladimir Sementsov-Ogievskiy if (message_size > chunk->length - sizeof(error) - sizeof(message_size)) { 72386f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent structured error chunk " 72486f8cdf3SVladimir Sementsov-Ogievskiy "with incorrect message size"); 72586f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 72686f8cdf3SVladimir Sementsov-Ogievskiy } 72786f8cdf3SVladimir Sementsov-Ogievskiy 72886f8cdf3SVladimir Sementsov-Ogievskiy /* TODO: Add a trace point to mention the server complaint */ 72986f8cdf3SVladimir Sementsov-Ogievskiy 73086f8cdf3SVladimir Sementsov-Ogievskiy /* TODO handle ERROR_OFFSET */ 73186f8cdf3SVladimir Sementsov-Ogievskiy 73286f8cdf3SVladimir Sementsov-Ogievskiy return 0; 73386f8cdf3SVladimir Sementsov-Ogievskiy } 73486f8cdf3SVladimir Sementsov-Ogievskiy 7350c43c6fcSPaolo Bonzini static int coroutine_fn 7360c43c6fcSPaolo Bonzini nbd_co_receive_offset_data_payload(BDRVNBDState *s, uint64_t orig_offset, 73786f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector *qiov, Error **errp) 73886f8cdf3SVladimir Sementsov-Ogievskiy { 73986f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector sub_qiov; 74086f8cdf3SVladimir Sementsov-Ogievskiy uint64_t offset; 74186f8cdf3SVladimir Sementsov-Ogievskiy size_t data_size; 74286f8cdf3SVladimir Sementsov-Ogievskiy int ret; 74386f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk = &s->reply.structured; 74486f8cdf3SVladimir Sementsov-Ogievskiy 74586f8cdf3SVladimir Sementsov-Ogievskiy assert(nbd_reply_is_structured(&s->reply)); 74686f8cdf3SVladimir Sementsov-Ogievskiy 74786f8cdf3SVladimir Sementsov-Ogievskiy /* The NBD spec requires at least one byte of payload */ 74886f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length <= sizeof(offset)) { 74986f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: invalid payload for " 75086f8cdf3SVladimir Sementsov-Ogievskiy "NBD_REPLY_TYPE_OFFSET_DATA"); 75186f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 75286f8cdf3SVladimir Sementsov-Ogievskiy } 75386f8cdf3SVladimir Sementsov-Ogievskiy 75486f8cdf3SVladimir Sementsov-Ogievskiy if (nbd_read64(s->ioc, &offset, "OFFSET_DATA offset", errp) < 0) { 75586f8cdf3SVladimir Sementsov-Ogievskiy return -EIO; 75686f8cdf3SVladimir Sementsov-Ogievskiy } 75786f8cdf3SVladimir Sementsov-Ogievskiy 75886f8cdf3SVladimir Sementsov-Ogievskiy data_size = chunk->length - sizeof(offset); 75986f8cdf3SVladimir Sementsov-Ogievskiy assert(data_size); 76086f8cdf3SVladimir Sementsov-Ogievskiy if (offset < orig_offset || data_size > qiov->size || 76186f8cdf3SVladimir Sementsov-Ogievskiy offset > orig_offset + qiov->size - data_size) { 76286f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent chunk exceeding requested" 76386f8cdf3SVladimir Sementsov-Ogievskiy " region"); 76486f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 76586f8cdf3SVladimir Sementsov-Ogievskiy } 76686f8cdf3SVladimir Sementsov-Ogievskiy if (s->info.min_block && !QEMU_IS_ALIGNED(data_size, s->info.min_block)) { 76786f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_structured_read_compliance("data"); 76886f8cdf3SVladimir Sementsov-Ogievskiy } 76986f8cdf3SVladimir Sementsov-Ogievskiy 77086f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_init(&sub_qiov, qiov->niov); 77186f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_concat(&sub_qiov, qiov, offset - orig_offset, data_size); 77286f8cdf3SVladimir Sementsov-Ogievskiy ret = qio_channel_readv_all(s->ioc, sub_qiov.iov, sub_qiov.niov, errp); 77386f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_destroy(&sub_qiov); 77486f8cdf3SVladimir Sementsov-Ogievskiy 77586f8cdf3SVladimir Sementsov-Ogievskiy return ret < 0 ? -EIO : 0; 77686f8cdf3SVladimir Sementsov-Ogievskiy } 77786f8cdf3SVladimir Sementsov-Ogievskiy 77886f8cdf3SVladimir Sementsov-Ogievskiy #define NBD_MAX_MALLOC_PAYLOAD 1000 77986f8cdf3SVladimir Sementsov-Ogievskiy static coroutine_fn int nbd_co_receive_structured_payload( 780611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s, void **payload, Error **errp) 78186f8cdf3SVladimir Sementsov-Ogievskiy { 78286f8cdf3SVladimir Sementsov-Ogievskiy int ret; 78386f8cdf3SVladimir Sementsov-Ogievskiy uint32_t len; 78486f8cdf3SVladimir Sementsov-Ogievskiy 78586f8cdf3SVladimir Sementsov-Ogievskiy assert(nbd_reply_is_structured(&s->reply)); 78686f8cdf3SVladimir Sementsov-Ogievskiy 78786f8cdf3SVladimir Sementsov-Ogievskiy len = s->reply.structured.length; 78886f8cdf3SVladimir Sementsov-Ogievskiy 78986f8cdf3SVladimir Sementsov-Ogievskiy if (len == 0) { 79086f8cdf3SVladimir Sementsov-Ogievskiy return 0; 79186f8cdf3SVladimir Sementsov-Ogievskiy } 79286f8cdf3SVladimir Sementsov-Ogievskiy 79386f8cdf3SVladimir Sementsov-Ogievskiy if (payload == NULL) { 79486f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Unexpected structured payload"); 79586f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 79686f8cdf3SVladimir Sementsov-Ogievskiy } 79786f8cdf3SVladimir Sementsov-Ogievskiy 79886f8cdf3SVladimir Sementsov-Ogievskiy if (len > NBD_MAX_MALLOC_PAYLOAD) { 79986f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Payload too large"); 80086f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 80186f8cdf3SVladimir Sementsov-Ogievskiy } 80286f8cdf3SVladimir Sementsov-Ogievskiy 80386f8cdf3SVladimir Sementsov-Ogievskiy *payload = g_new(char, len); 80486f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_read(s->ioc, *payload, len, "structured payload", errp); 80586f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 80686f8cdf3SVladimir Sementsov-Ogievskiy g_free(*payload); 80786f8cdf3SVladimir Sementsov-Ogievskiy *payload = NULL; 80886f8cdf3SVladimir Sementsov-Ogievskiy return ret; 80986f8cdf3SVladimir Sementsov-Ogievskiy } 81086f8cdf3SVladimir Sementsov-Ogievskiy 81186f8cdf3SVladimir Sementsov-Ogievskiy return 0; 81286f8cdf3SVladimir Sementsov-Ogievskiy } 81386f8cdf3SVladimir Sementsov-Ogievskiy 81486f8cdf3SVladimir Sementsov-Ogievskiy /* 81586f8cdf3SVladimir Sementsov-Ogievskiy * nbd_co_do_receive_one_chunk 81686f8cdf3SVladimir Sementsov-Ogievskiy * for simple reply: 81786f8cdf3SVladimir Sementsov-Ogievskiy * set request_ret to received reply error 81886f8cdf3SVladimir Sementsov-Ogievskiy * if qiov is not NULL: read payload to @qiov 81986f8cdf3SVladimir Sementsov-Ogievskiy * for structured reply chunk: 82086f8cdf3SVladimir Sementsov-Ogievskiy * if error chunk: read payload, set @request_ret, do not set @payload 82186f8cdf3SVladimir Sementsov-Ogievskiy * else if offset_data chunk: read payload data to @qiov, do not set @payload 82286f8cdf3SVladimir Sementsov-Ogievskiy * else: read payload to @payload 82386f8cdf3SVladimir Sementsov-Ogievskiy * 82486f8cdf3SVladimir Sementsov-Ogievskiy * If function fails, @errp contains corresponding error message, and the 82586f8cdf3SVladimir Sementsov-Ogievskiy * connection with the server is suspect. If it returns 0, then the 82686f8cdf3SVladimir Sementsov-Ogievskiy * transaction succeeded (although @request_ret may be a negative errno 82786f8cdf3SVladimir Sementsov-Ogievskiy * corresponding to the server's error reply), and errp is unchanged. 82886f8cdf3SVladimir Sementsov-Ogievskiy */ 82986f8cdf3SVladimir Sementsov-Ogievskiy static coroutine_fn int nbd_co_do_receive_one_chunk( 83022efd811SEric Blake BDRVNBDState *s, uint64_t cookie, bool only_structured, 83186f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, QEMUIOVector *qiov, void **payload, Error **errp) 83286f8cdf3SVladimir Sementsov-Ogievskiy { 83386f8cdf3SVladimir Sementsov-Ogievskiy int ret; 8348cb98a72SEric Blake int i = COOKIE_TO_INDEX(cookie); 83586f8cdf3SVladimir Sementsov-Ogievskiy void *local_payload = NULL; 83686f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk; 83786f8cdf3SVladimir Sementsov-Ogievskiy 83886f8cdf3SVladimir Sementsov-Ogievskiy if (payload) { 83986f8cdf3SVladimir Sementsov-Ogievskiy *payload = NULL; 84086f8cdf3SVladimir Sementsov-Ogievskiy } 84186f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = 0; 84286f8cdf3SVladimir Sementsov-Ogievskiy 84322efd811SEric Blake ret = nbd_receive_replies(s, cookie); 844172f5f1aSPaolo Bonzini if (ret < 0) { 84586f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Connection closed"); 84686f8cdf3SVladimir Sementsov-Ogievskiy return -EIO; 84786f8cdf3SVladimir Sementsov-Ogievskiy } 84886f8cdf3SVladimir Sementsov-Ogievskiy assert(s->ioc); 84986f8cdf3SVladimir Sementsov-Ogievskiy 85022efd811SEric Blake assert(s->reply.cookie == cookie); 85186f8cdf3SVladimir Sementsov-Ogievskiy 85286f8cdf3SVladimir Sementsov-Ogievskiy if (nbd_reply_is_simple(&s->reply)) { 85386f8cdf3SVladimir Sementsov-Ogievskiy if (only_structured) { 85486f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: simple reply when structured " 85586f8cdf3SVladimir Sementsov-Ogievskiy "reply chunk was expected"); 85686f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 85786f8cdf3SVladimir Sementsov-Ogievskiy } 85886f8cdf3SVladimir Sementsov-Ogievskiy 85986f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = -nbd_errno_to_system_errno(s->reply.simple.error); 86086f8cdf3SVladimir Sementsov-Ogievskiy if (*request_ret < 0 || !qiov) { 86186f8cdf3SVladimir Sementsov-Ogievskiy return 0; 86286f8cdf3SVladimir Sementsov-Ogievskiy } 86386f8cdf3SVladimir Sementsov-Ogievskiy 86486f8cdf3SVladimir Sementsov-Ogievskiy return qio_channel_readv_all(s->ioc, qiov->iov, qiov->niov, 86586f8cdf3SVladimir Sementsov-Ogievskiy errp) < 0 ? -EIO : 0; 86686f8cdf3SVladimir Sementsov-Ogievskiy } 86786f8cdf3SVladimir Sementsov-Ogievskiy 86886f8cdf3SVladimir Sementsov-Ogievskiy /* handle structured reply chunk */ 86986f8cdf3SVladimir Sementsov-Ogievskiy assert(s->info.structured_reply); 87086f8cdf3SVladimir Sementsov-Ogievskiy chunk = &s->reply.structured; 87186f8cdf3SVladimir Sementsov-Ogievskiy 87286f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->type == NBD_REPLY_TYPE_NONE) { 87386f8cdf3SVladimir Sementsov-Ogievskiy if (!(chunk->flags & NBD_REPLY_FLAG_DONE)) { 87486f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk without" 87586f8cdf3SVladimir Sementsov-Ogievskiy " NBD_REPLY_FLAG_DONE flag set"); 87686f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 87786f8cdf3SVladimir Sementsov-Ogievskiy } 87886f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length) { 87986f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk with" 88086f8cdf3SVladimir Sementsov-Ogievskiy " nonzero length"); 88186f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 88286f8cdf3SVladimir Sementsov-Ogievskiy } 88386f8cdf3SVladimir Sementsov-Ogievskiy return 0; 88486f8cdf3SVladimir Sementsov-Ogievskiy } 88586f8cdf3SVladimir Sementsov-Ogievskiy 88686f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->type == NBD_REPLY_TYPE_OFFSET_DATA) { 88786f8cdf3SVladimir Sementsov-Ogievskiy if (!qiov) { 88886f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Unexpected NBD_REPLY_TYPE_OFFSET_DATA chunk"); 88986f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 89086f8cdf3SVladimir Sementsov-Ogievskiy } 89186f8cdf3SVladimir Sementsov-Ogievskiy 89286f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_receive_offset_data_payload(s, s->requests[i].offset, 89386f8cdf3SVladimir Sementsov-Ogievskiy qiov, errp); 89486f8cdf3SVladimir Sementsov-Ogievskiy } 89586f8cdf3SVladimir Sementsov-Ogievskiy 89686f8cdf3SVladimir Sementsov-Ogievskiy if (nbd_reply_type_is_error(chunk->type)) { 89786f8cdf3SVladimir Sementsov-Ogievskiy payload = &local_payload; 89886f8cdf3SVladimir Sementsov-Ogievskiy } 89986f8cdf3SVladimir Sementsov-Ogievskiy 90086f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_co_receive_structured_payload(s, payload, errp); 90186f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 90286f8cdf3SVladimir Sementsov-Ogievskiy return ret; 90386f8cdf3SVladimir Sementsov-Ogievskiy } 90486f8cdf3SVladimir Sementsov-Ogievskiy 90586f8cdf3SVladimir Sementsov-Ogievskiy if (nbd_reply_type_is_error(chunk->type)) { 90686f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_parse_error_payload(chunk, local_payload, request_ret, errp); 90786f8cdf3SVladimir Sementsov-Ogievskiy g_free(local_payload); 90886f8cdf3SVladimir Sementsov-Ogievskiy return ret; 90986f8cdf3SVladimir Sementsov-Ogievskiy } 91086f8cdf3SVladimir Sementsov-Ogievskiy 91186f8cdf3SVladimir Sementsov-Ogievskiy return 0; 91286f8cdf3SVladimir Sementsov-Ogievskiy } 91386f8cdf3SVladimir Sementsov-Ogievskiy 91486f8cdf3SVladimir Sementsov-Ogievskiy /* 91586f8cdf3SVladimir Sementsov-Ogievskiy * nbd_co_receive_one_chunk 91686f8cdf3SVladimir Sementsov-Ogievskiy * Read reply, wake up connection_co and set s->quit if needed. 91786f8cdf3SVladimir Sementsov-Ogievskiy * Return value is a fatal error code or normal nbd reply error code 91886f8cdf3SVladimir Sementsov-Ogievskiy */ 91986f8cdf3SVladimir Sementsov-Ogievskiy static coroutine_fn int nbd_co_receive_one_chunk( 92022efd811SEric Blake BDRVNBDState *s, uint64_t cookie, bool only_structured, 92186f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, QEMUIOVector *qiov, NBDReply *reply, void **payload, 92286f8cdf3SVladimir Sementsov-Ogievskiy Error **errp) 92386f8cdf3SVladimir Sementsov-Ogievskiy { 92422efd811SEric Blake int ret = nbd_co_do_receive_one_chunk(s, cookie, only_structured, 92586f8cdf3SVladimir Sementsov-Ogievskiy request_ret, qiov, payload, errp); 92686f8cdf3SVladimir Sementsov-Ogievskiy 92786f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 9285cf42b1cSEric Blake memset(reply, 0, sizeof(*reply)); 929a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, ret); 93086f8cdf3SVladimir Sementsov-Ogievskiy } else { 93186f8cdf3SVladimir Sementsov-Ogievskiy /* For assert at loop start in nbd_connection_entry */ 93286f8cdf3SVladimir Sementsov-Ogievskiy *reply = s->reply; 93386f8cdf3SVladimir Sementsov-Ogievskiy } 93422efd811SEric Blake s->reply.cookie = 0; 93586f8cdf3SVladimir Sementsov-Ogievskiy 936a80a9a1cSPaolo Bonzini nbd_recv_coroutines_wake(s); 93786f8cdf3SVladimir Sementsov-Ogievskiy 93886f8cdf3SVladimir Sementsov-Ogievskiy return ret; 93986f8cdf3SVladimir Sementsov-Ogievskiy } 94086f8cdf3SVladimir Sementsov-Ogievskiy 94186f8cdf3SVladimir Sementsov-Ogievskiy typedef struct NBDReplyChunkIter { 94286f8cdf3SVladimir Sementsov-Ogievskiy int ret; 94386f8cdf3SVladimir Sementsov-Ogievskiy int request_ret; 94486f8cdf3SVladimir Sementsov-Ogievskiy Error *err; 94586f8cdf3SVladimir Sementsov-Ogievskiy bool done, only_structured; 94686f8cdf3SVladimir Sementsov-Ogievskiy } NBDReplyChunkIter; 94786f8cdf3SVladimir Sementsov-Ogievskiy 94886f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_iter_channel_error(NBDReplyChunkIter *iter, 94986f8cdf3SVladimir Sementsov-Ogievskiy int ret, Error **local_err) 95086f8cdf3SVladimir Sementsov-Ogievskiy { 951d9366135SVladimir Sementsov-Ogievskiy assert(local_err && *local_err); 95286f8cdf3SVladimir Sementsov-Ogievskiy assert(ret < 0); 95386f8cdf3SVladimir Sementsov-Ogievskiy 95486f8cdf3SVladimir Sementsov-Ogievskiy if (!iter->ret) { 95586f8cdf3SVladimir Sementsov-Ogievskiy iter->ret = ret; 95686f8cdf3SVladimir Sementsov-Ogievskiy error_propagate(&iter->err, *local_err); 95786f8cdf3SVladimir Sementsov-Ogievskiy } else { 95886f8cdf3SVladimir Sementsov-Ogievskiy error_free(*local_err); 95986f8cdf3SVladimir Sementsov-Ogievskiy } 96086f8cdf3SVladimir Sementsov-Ogievskiy 96186f8cdf3SVladimir Sementsov-Ogievskiy *local_err = NULL; 96286f8cdf3SVladimir Sementsov-Ogievskiy } 96386f8cdf3SVladimir Sementsov-Ogievskiy 96486f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_iter_request_error(NBDReplyChunkIter *iter, int ret) 96586f8cdf3SVladimir Sementsov-Ogievskiy { 96686f8cdf3SVladimir Sementsov-Ogievskiy assert(ret < 0); 96786f8cdf3SVladimir Sementsov-Ogievskiy 96886f8cdf3SVladimir Sementsov-Ogievskiy if (!iter->request_ret) { 96986f8cdf3SVladimir Sementsov-Ogievskiy iter->request_ret = ret; 97086f8cdf3SVladimir Sementsov-Ogievskiy } 97186f8cdf3SVladimir Sementsov-Ogievskiy } 97286f8cdf3SVladimir Sementsov-Ogievskiy 97386f8cdf3SVladimir Sementsov-Ogievskiy /* 97486f8cdf3SVladimir Sementsov-Ogievskiy * NBD_FOREACH_REPLY_CHUNK 97586f8cdf3SVladimir Sementsov-Ogievskiy * The pointer stored in @payload requires g_free() to free it. 97686f8cdf3SVladimir Sementsov-Ogievskiy */ 97722efd811SEric Blake #define NBD_FOREACH_REPLY_CHUNK(s, iter, cookie, structured, \ 97886f8cdf3SVladimir Sementsov-Ogievskiy qiov, reply, payload) \ 97986f8cdf3SVladimir Sementsov-Ogievskiy for (iter = (NBDReplyChunkIter) { .only_structured = structured }; \ 98022efd811SEric Blake nbd_reply_chunk_iter_receive(s, &iter, cookie, qiov, reply, payload);) 98186f8cdf3SVladimir Sementsov-Ogievskiy 98286f8cdf3SVladimir Sementsov-Ogievskiy /* 98386f8cdf3SVladimir Sementsov-Ogievskiy * nbd_reply_chunk_iter_receive 98486f8cdf3SVladimir Sementsov-Ogievskiy * The pointer stored in @payload requires g_free() to free it. 98586f8cdf3SVladimir Sementsov-Ogievskiy */ 9868e5a19dfSPaolo Bonzini static bool coroutine_fn nbd_reply_chunk_iter_receive(BDRVNBDState *s, 98786f8cdf3SVladimir Sementsov-Ogievskiy NBDReplyChunkIter *iter, 98822efd811SEric Blake uint64_t cookie, 9898e5a19dfSPaolo Bonzini QEMUIOVector *qiov, 9908e5a19dfSPaolo Bonzini NBDReply *reply, 99186f8cdf3SVladimir Sementsov-Ogievskiy void **payload) 99286f8cdf3SVladimir Sementsov-Ogievskiy { 99386f8cdf3SVladimir Sementsov-Ogievskiy int ret, request_ret; 99486f8cdf3SVladimir Sementsov-Ogievskiy NBDReply local_reply; 99586f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk; 99686f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 99786f8cdf3SVladimir Sementsov-Ogievskiy 99886f8cdf3SVladimir Sementsov-Ogievskiy if (iter->done) { 99986f8cdf3SVladimir Sementsov-Ogievskiy /* Previous iteration was last. */ 100086f8cdf3SVladimir Sementsov-Ogievskiy goto break_loop; 100186f8cdf3SVladimir Sementsov-Ogievskiy } 100286f8cdf3SVladimir Sementsov-Ogievskiy 100386f8cdf3SVladimir Sementsov-Ogievskiy if (reply == NULL) { 100486f8cdf3SVladimir Sementsov-Ogievskiy reply = &local_reply; 100586f8cdf3SVladimir Sementsov-Ogievskiy } 100686f8cdf3SVladimir Sementsov-Ogievskiy 100722efd811SEric Blake ret = nbd_co_receive_one_chunk(s, cookie, iter->only_structured, 100886f8cdf3SVladimir Sementsov-Ogievskiy &request_ret, qiov, reply, payload, 100986f8cdf3SVladimir Sementsov-Ogievskiy &local_err); 101086f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 101186f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(iter, ret, &local_err); 101286f8cdf3SVladimir Sementsov-Ogievskiy } else if (request_ret < 0) { 101386f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_request_error(iter, request_ret); 101486f8cdf3SVladimir Sementsov-Ogievskiy } 101586f8cdf3SVladimir Sementsov-Ogievskiy 101686f8cdf3SVladimir Sementsov-Ogievskiy /* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */ 1017172f5f1aSPaolo Bonzini if (nbd_reply_is_simple(reply) || iter->ret < 0) { 101886f8cdf3SVladimir Sementsov-Ogievskiy goto break_loop; 101986f8cdf3SVladimir Sementsov-Ogievskiy } 102086f8cdf3SVladimir Sementsov-Ogievskiy 102186f8cdf3SVladimir Sementsov-Ogievskiy chunk = &reply->structured; 102286f8cdf3SVladimir Sementsov-Ogievskiy iter->only_structured = true; 102386f8cdf3SVladimir Sementsov-Ogievskiy 102486f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->type == NBD_REPLY_TYPE_NONE) { 102586f8cdf3SVladimir Sementsov-Ogievskiy /* NBD_REPLY_FLAG_DONE is already checked in nbd_co_receive_one_chunk */ 102686f8cdf3SVladimir Sementsov-Ogievskiy assert(chunk->flags & NBD_REPLY_FLAG_DONE); 102786f8cdf3SVladimir Sementsov-Ogievskiy goto break_loop; 102886f8cdf3SVladimir Sementsov-Ogievskiy } 102986f8cdf3SVladimir Sementsov-Ogievskiy 103086f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->flags & NBD_REPLY_FLAG_DONE) { 103186f8cdf3SVladimir Sementsov-Ogievskiy /* This iteration is last. */ 103286f8cdf3SVladimir Sementsov-Ogievskiy iter->done = true; 103386f8cdf3SVladimir Sementsov-Ogievskiy } 103486f8cdf3SVladimir Sementsov-Ogievskiy 103586f8cdf3SVladimir Sementsov-Ogievskiy /* Execute the loop body */ 103686f8cdf3SVladimir Sementsov-Ogievskiy return true; 103786f8cdf3SVladimir Sementsov-Ogievskiy 103886f8cdf3SVladimir Sementsov-Ogievskiy break_loop: 1039ee19d953SPaolo Bonzini qemu_mutex_lock(&s->requests_lock); 10408cb98a72SEric Blake s->requests[COOKIE_TO_INDEX(cookie)].coroutine = NULL; 104186f8cdf3SVladimir Sementsov-Ogievskiy s->in_flight--; 104286f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_queue_next(&s->free_sema); 1043ee19d953SPaolo Bonzini qemu_mutex_unlock(&s->requests_lock); 104486f8cdf3SVladimir Sementsov-Ogievskiy 104586f8cdf3SVladimir Sementsov-Ogievskiy return false; 104686f8cdf3SVladimir Sementsov-Ogievskiy } 104786f8cdf3SVladimir Sementsov-Ogievskiy 104822efd811SEric Blake static int coroutine_fn 104922efd811SEric Blake nbd_co_receive_return_code(BDRVNBDState *s, uint64_t cookie, 105086f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, Error **errp) 105186f8cdf3SVladimir Sementsov-Ogievskiy { 105286f8cdf3SVladimir Sementsov-Ogievskiy NBDReplyChunkIter iter; 105386f8cdf3SVladimir Sementsov-Ogievskiy 105422efd811SEric Blake NBD_FOREACH_REPLY_CHUNK(s, iter, cookie, false, NULL, NULL, NULL) { 105586f8cdf3SVladimir Sementsov-Ogievskiy /* nbd_reply_chunk_iter_receive does all the work */ 105686f8cdf3SVladimir Sementsov-Ogievskiy } 105786f8cdf3SVladimir Sementsov-Ogievskiy 105886f8cdf3SVladimir Sementsov-Ogievskiy error_propagate(errp, iter.err); 105986f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = iter.request_ret; 106086f8cdf3SVladimir Sementsov-Ogievskiy return iter.ret; 106186f8cdf3SVladimir Sementsov-Ogievskiy } 106286f8cdf3SVladimir Sementsov-Ogievskiy 106322efd811SEric Blake static int coroutine_fn 106422efd811SEric Blake nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_t cookie, 106586f8cdf3SVladimir Sementsov-Ogievskiy uint64_t offset, QEMUIOVector *qiov, 106686f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, Error **errp) 106786f8cdf3SVladimir Sementsov-Ogievskiy { 106886f8cdf3SVladimir Sementsov-Ogievskiy NBDReplyChunkIter iter; 106986f8cdf3SVladimir Sementsov-Ogievskiy NBDReply reply; 107086f8cdf3SVladimir Sementsov-Ogievskiy void *payload = NULL; 107186f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 107286f8cdf3SVladimir Sementsov-Ogievskiy 107322efd811SEric Blake NBD_FOREACH_REPLY_CHUNK(s, iter, cookie, s->info.structured_reply, 107486f8cdf3SVladimir Sementsov-Ogievskiy qiov, &reply, &payload) 107586f8cdf3SVladimir Sementsov-Ogievskiy { 107686f8cdf3SVladimir Sementsov-Ogievskiy int ret; 107786f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk = &reply.structured; 107886f8cdf3SVladimir Sementsov-Ogievskiy 107986f8cdf3SVladimir Sementsov-Ogievskiy assert(nbd_reply_is_structured(&reply)); 108086f8cdf3SVladimir Sementsov-Ogievskiy 108186f8cdf3SVladimir Sementsov-Ogievskiy switch (chunk->type) { 108286f8cdf3SVladimir Sementsov-Ogievskiy case NBD_REPLY_TYPE_OFFSET_DATA: 108386f8cdf3SVladimir Sementsov-Ogievskiy /* 108486f8cdf3SVladimir Sementsov-Ogievskiy * special cased in nbd_co_receive_one_chunk, data is already 108586f8cdf3SVladimir Sementsov-Ogievskiy * in qiov 108686f8cdf3SVladimir Sementsov-Ogievskiy */ 108786f8cdf3SVladimir Sementsov-Ogievskiy break; 108886f8cdf3SVladimir Sementsov-Ogievskiy case NBD_REPLY_TYPE_OFFSET_HOLE: 108986f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_parse_offset_hole_payload(s, &reply.structured, payload, 109086f8cdf3SVladimir Sementsov-Ogievskiy offset, qiov, &local_err); 109186f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1092a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, ret); 109386f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, ret, &local_err); 109486f8cdf3SVladimir Sementsov-Ogievskiy } 109586f8cdf3SVladimir Sementsov-Ogievskiy break; 109686f8cdf3SVladimir Sementsov-Ogievskiy default: 109786f8cdf3SVladimir Sementsov-Ogievskiy if (!nbd_reply_type_is_error(chunk->type)) { 109886f8cdf3SVladimir Sementsov-Ogievskiy /* not allowed reply type */ 1099a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, -EINVAL); 110086f8cdf3SVladimir Sementsov-Ogievskiy error_setg(&local_err, 110186f8cdf3SVladimir Sementsov-Ogievskiy "Unexpected reply type: %d (%s) for CMD_READ", 110286f8cdf3SVladimir Sementsov-Ogievskiy chunk->type, nbd_reply_type_lookup(chunk->type)); 110386f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, -EINVAL, &local_err); 110486f8cdf3SVladimir Sementsov-Ogievskiy } 110586f8cdf3SVladimir Sementsov-Ogievskiy } 110686f8cdf3SVladimir Sementsov-Ogievskiy 110786f8cdf3SVladimir Sementsov-Ogievskiy g_free(payload); 110886f8cdf3SVladimir Sementsov-Ogievskiy payload = NULL; 110986f8cdf3SVladimir Sementsov-Ogievskiy } 111086f8cdf3SVladimir Sementsov-Ogievskiy 111186f8cdf3SVladimir Sementsov-Ogievskiy error_propagate(errp, iter.err); 111286f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = iter.request_ret; 111386f8cdf3SVladimir Sementsov-Ogievskiy return iter.ret; 111486f8cdf3SVladimir Sementsov-Ogievskiy } 111586f8cdf3SVladimir Sementsov-Ogievskiy 111622efd811SEric Blake static int coroutine_fn 111722efd811SEric Blake nbd_co_receive_blockstatus_reply(BDRVNBDState *s, uint64_t cookie, 111822efd811SEric Blake uint64_t length, NBDExtent *extent, 111986f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, Error **errp) 112086f8cdf3SVladimir Sementsov-Ogievskiy { 112186f8cdf3SVladimir Sementsov-Ogievskiy NBDReplyChunkIter iter; 112286f8cdf3SVladimir Sementsov-Ogievskiy NBDReply reply; 112386f8cdf3SVladimir Sementsov-Ogievskiy void *payload = NULL; 112486f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 112586f8cdf3SVladimir Sementsov-Ogievskiy bool received = false; 112686f8cdf3SVladimir Sementsov-Ogievskiy 112786f8cdf3SVladimir Sementsov-Ogievskiy assert(!extent->length); 112822efd811SEric Blake NBD_FOREACH_REPLY_CHUNK(s, iter, cookie, false, NULL, &reply, &payload) { 112986f8cdf3SVladimir Sementsov-Ogievskiy int ret; 113086f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk = &reply.structured; 113186f8cdf3SVladimir Sementsov-Ogievskiy 113286f8cdf3SVladimir Sementsov-Ogievskiy assert(nbd_reply_is_structured(&reply)); 113386f8cdf3SVladimir Sementsov-Ogievskiy 113486f8cdf3SVladimir Sementsov-Ogievskiy switch (chunk->type) { 113586f8cdf3SVladimir Sementsov-Ogievskiy case NBD_REPLY_TYPE_BLOCK_STATUS: 113686f8cdf3SVladimir Sementsov-Ogievskiy if (received) { 1137a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, -EINVAL); 113886f8cdf3SVladimir Sementsov-Ogievskiy error_setg(&local_err, "Several BLOCK_STATUS chunks in reply"); 113986f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, -EINVAL, &local_err); 114086f8cdf3SVladimir Sementsov-Ogievskiy } 114186f8cdf3SVladimir Sementsov-Ogievskiy received = true; 114286f8cdf3SVladimir Sementsov-Ogievskiy 114386f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_parse_blockstatus_payload(s, &reply.structured, 114486f8cdf3SVladimir Sementsov-Ogievskiy payload, length, extent, 114586f8cdf3SVladimir Sementsov-Ogievskiy &local_err); 114686f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1147a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, ret); 114886f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, ret, &local_err); 114986f8cdf3SVladimir Sementsov-Ogievskiy } 115086f8cdf3SVladimir Sementsov-Ogievskiy break; 115186f8cdf3SVladimir Sementsov-Ogievskiy default: 115286f8cdf3SVladimir Sementsov-Ogievskiy if (!nbd_reply_type_is_error(chunk->type)) { 1153a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, -EINVAL); 115486f8cdf3SVladimir Sementsov-Ogievskiy error_setg(&local_err, 115586f8cdf3SVladimir Sementsov-Ogievskiy "Unexpected reply type: %d (%s) " 115686f8cdf3SVladimir Sementsov-Ogievskiy "for CMD_BLOCK_STATUS", 115786f8cdf3SVladimir Sementsov-Ogievskiy chunk->type, nbd_reply_type_lookup(chunk->type)); 115886f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, -EINVAL, &local_err); 115986f8cdf3SVladimir Sementsov-Ogievskiy } 116086f8cdf3SVladimir Sementsov-Ogievskiy } 116186f8cdf3SVladimir Sementsov-Ogievskiy 116286f8cdf3SVladimir Sementsov-Ogievskiy g_free(payload); 116386f8cdf3SVladimir Sementsov-Ogievskiy payload = NULL; 116486f8cdf3SVladimir Sementsov-Ogievskiy } 116586f8cdf3SVladimir Sementsov-Ogievskiy 116686f8cdf3SVladimir Sementsov-Ogievskiy if (!extent->length && !iter.request_ret) { 116786f8cdf3SVladimir Sementsov-Ogievskiy error_setg(&local_err, "Server did not reply with any status extents"); 116886f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, -EIO, &local_err); 116986f8cdf3SVladimir Sementsov-Ogievskiy } 117086f8cdf3SVladimir Sementsov-Ogievskiy 117186f8cdf3SVladimir Sementsov-Ogievskiy error_propagate(errp, iter.err); 117286f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = iter.request_ret; 117386f8cdf3SVladimir Sementsov-Ogievskiy return iter.ret; 117486f8cdf3SVladimir Sementsov-Ogievskiy } 117586f8cdf3SVladimir Sementsov-Ogievskiy 117669aa0d37SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK 117769aa0d37SEmanuele Giuseppe Esposito nbd_co_request(BlockDriverState *bs, NBDRequest *request, 117886f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector *write_qiov) 117986f8cdf3SVladimir Sementsov-Ogievskiy { 118086f8cdf3SVladimir Sementsov-Ogievskiy int ret, request_ret; 118186f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 1182611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 118386f8cdf3SVladimir Sementsov-Ogievskiy 118486f8cdf3SVladimir Sementsov-Ogievskiy assert(request->type != NBD_CMD_READ); 118586f8cdf3SVladimir Sementsov-Ogievskiy if (write_qiov) { 118686f8cdf3SVladimir Sementsov-Ogievskiy assert(request->type == NBD_CMD_WRITE); 118786f8cdf3SVladimir Sementsov-Ogievskiy assert(request->len == iov_size(write_qiov->iov, write_qiov->niov)); 118886f8cdf3SVladimir Sementsov-Ogievskiy } else { 118986f8cdf3SVladimir Sementsov-Ogievskiy assert(request->type != NBD_CMD_WRITE); 119086f8cdf3SVladimir Sementsov-Ogievskiy } 1191f7651539SVladimir Sementsov-Ogievskiy 1192f7651539SVladimir Sementsov-Ogievskiy do { 119386f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_co_send_request(bs, request, write_qiov); 119486f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1195f7651539SVladimir Sementsov-Ogievskiy continue; 119686f8cdf3SVladimir Sementsov-Ogievskiy } 119786f8cdf3SVladimir Sementsov-Ogievskiy 119822efd811SEric Blake ret = nbd_co_receive_return_code(s, request->cookie, 119986f8cdf3SVladimir Sementsov-Ogievskiy &request_ret, &local_err); 120086f8cdf3SVladimir Sementsov-Ogievskiy if (local_err) { 1201f7651539SVladimir Sementsov-Ogievskiy trace_nbd_co_request_fail(request->from, request->len, 120222efd811SEric Blake request->cookie, request->flags, 1203f7651539SVladimir Sementsov-Ogievskiy request->type, 120486f8cdf3SVladimir Sementsov-Ogievskiy nbd_cmd_lookup(request->type), 120586f8cdf3SVladimir Sementsov-Ogievskiy ret, error_get_pretty(local_err)); 120686f8cdf3SVladimir Sementsov-Ogievskiy error_free(local_err); 1207f7651539SVladimir Sementsov-Ogievskiy local_err = NULL; 120886f8cdf3SVladimir Sementsov-Ogievskiy } 12098d45185cSPaolo Bonzini } while (ret < 0 && nbd_client_will_reconnect(s)); 1210f7651539SVladimir Sementsov-Ogievskiy 121186f8cdf3SVladimir Sementsov-Ogievskiy return ret ? ret : request_ret; 121286f8cdf3SVladimir Sementsov-Ogievskiy } 121386f8cdf3SVladimir Sementsov-Ogievskiy 121469aa0d37SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK 121569aa0d37SEmanuele Giuseppe Esposito nbd_client_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes, 121669aa0d37SEmanuele Giuseppe Esposito QEMUIOVector *qiov, BdrvRequestFlags flags) 121786f8cdf3SVladimir Sementsov-Ogievskiy { 121886f8cdf3SVladimir Sementsov-Ogievskiy int ret, request_ret; 121986f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 1220611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 122186f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 122286f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_READ, 122386f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 122486f8cdf3SVladimir Sementsov-Ogievskiy .len = bytes, 122586f8cdf3SVladimir Sementsov-Ogievskiy }; 122686f8cdf3SVladimir Sementsov-Ogievskiy 122786f8cdf3SVladimir Sementsov-Ogievskiy assert(bytes <= NBD_MAX_BUFFER_SIZE); 122886f8cdf3SVladimir Sementsov-Ogievskiy 122986f8cdf3SVladimir Sementsov-Ogievskiy if (!bytes) { 123086f8cdf3SVladimir Sementsov-Ogievskiy return 0; 123186f8cdf3SVladimir Sementsov-Ogievskiy } 123286f8cdf3SVladimir Sementsov-Ogievskiy /* 123386f8cdf3SVladimir Sementsov-Ogievskiy * Work around the fact that the block layer doesn't do 123486f8cdf3SVladimir Sementsov-Ogievskiy * byte-accurate sizing yet - if the read exceeds the server's 123586f8cdf3SVladimir Sementsov-Ogievskiy * advertised size because the block layer rounded size up, then 123686f8cdf3SVladimir Sementsov-Ogievskiy * truncate the request to the server and tail-pad with zero. 123786f8cdf3SVladimir Sementsov-Ogievskiy */ 1238611ae1d7SVladimir Sementsov-Ogievskiy if (offset >= s->info.size) { 123986f8cdf3SVladimir Sementsov-Ogievskiy assert(bytes < BDRV_SECTOR_SIZE); 124086f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_memset(qiov, 0, 0, bytes); 124186f8cdf3SVladimir Sementsov-Ogievskiy return 0; 124286f8cdf3SVladimir Sementsov-Ogievskiy } 1243611ae1d7SVladimir Sementsov-Ogievskiy if (offset + bytes > s->info.size) { 1244611ae1d7SVladimir Sementsov-Ogievskiy uint64_t slop = offset + bytes - s->info.size; 124586f8cdf3SVladimir Sementsov-Ogievskiy 124686f8cdf3SVladimir Sementsov-Ogievskiy assert(slop < BDRV_SECTOR_SIZE); 124786f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_memset(qiov, bytes - slop, 0, slop); 124886f8cdf3SVladimir Sementsov-Ogievskiy request.len -= slop; 124986f8cdf3SVladimir Sementsov-Ogievskiy } 125086f8cdf3SVladimir Sementsov-Ogievskiy 1251f7651539SVladimir Sementsov-Ogievskiy do { 125286f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_co_send_request(bs, &request, NULL); 125386f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1254f7651539SVladimir Sementsov-Ogievskiy continue; 125586f8cdf3SVladimir Sementsov-Ogievskiy } 125686f8cdf3SVladimir Sementsov-Ogievskiy 125722efd811SEric Blake ret = nbd_co_receive_cmdread_reply(s, request.cookie, offset, qiov, 125886f8cdf3SVladimir Sementsov-Ogievskiy &request_ret, &local_err); 125986f8cdf3SVladimir Sementsov-Ogievskiy if (local_err) { 126022efd811SEric Blake trace_nbd_co_request_fail(request.from, request.len, request.cookie, 126186f8cdf3SVladimir Sementsov-Ogievskiy request.flags, request.type, 126286f8cdf3SVladimir Sementsov-Ogievskiy nbd_cmd_lookup(request.type), 126386f8cdf3SVladimir Sementsov-Ogievskiy ret, error_get_pretty(local_err)); 126486f8cdf3SVladimir Sementsov-Ogievskiy error_free(local_err); 1265f7651539SVladimir Sementsov-Ogievskiy local_err = NULL; 126686f8cdf3SVladimir Sementsov-Ogievskiy } 12678d45185cSPaolo Bonzini } while (ret < 0 && nbd_client_will_reconnect(s)); 1268f7651539SVladimir Sementsov-Ogievskiy 126986f8cdf3SVladimir Sementsov-Ogievskiy return ret ? ret : request_ret; 127086f8cdf3SVladimir Sementsov-Ogievskiy } 127186f8cdf3SVladimir Sementsov-Ogievskiy 127269aa0d37SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK 127369aa0d37SEmanuele Giuseppe Esposito nbd_client_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes, 127469aa0d37SEmanuele Giuseppe Esposito QEMUIOVector *qiov, BdrvRequestFlags flags) 127586f8cdf3SVladimir Sementsov-Ogievskiy { 1276611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 127786f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 127886f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_WRITE, 127986f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 128086f8cdf3SVladimir Sementsov-Ogievskiy .len = bytes, 128186f8cdf3SVladimir Sementsov-Ogievskiy }; 128286f8cdf3SVladimir Sementsov-Ogievskiy 1283611ae1d7SVladimir Sementsov-Ogievskiy assert(!(s->info.flags & NBD_FLAG_READ_ONLY)); 128486f8cdf3SVladimir Sementsov-Ogievskiy if (flags & BDRV_REQ_FUA) { 1285611ae1d7SVladimir Sementsov-Ogievskiy assert(s->info.flags & NBD_FLAG_SEND_FUA); 128686f8cdf3SVladimir Sementsov-Ogievskiy request.flags |= NBD_CMD_FLAG_FUA; 128786f8cdf3SVladimir Sementsov-Ogievskiy } 128886f8cdf3SVladimir Sementsov-Ogievskiy 128986f8cdf3SVladimir Sementsov-Ogievskiy assert(bytes <= NBD_MAX_BUFFER_SIZE); 129086f8cdf3SVladimir Sementsov-Ogievskiy 129186f8cdf3SVladimir Sementsov-Ogievskiy if (!bytes) { 129286f8cdf3SVladimir Sementsov-Ogievskiy return 0; 129386f8cdf3SVladimir Sementsov-Ogievskiy } 129486f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_request(bs, &request, qiov); 129586f8cdf3SVladimir Sementsov-Ogievskiy } 129686f8cdf3SVladimir Sementsov-Ogievskiy 129769aa0d37SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK 129869aa0d37SEmanuele Giuseppe Esposito nbd_client_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes, 129969aa0d37SEmanuele Giuseppe Esposito BdrvRequestFlags flags) 130086f8cdf3SVladimir Sementsov-Ogievskiy { 1301611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 130286f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 130386f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_WRITE_ZEROES, 130486f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 1305f34b2bcfSVladimir Sementsov-Ogievskiy .len = bytes, /* .len is uint32_t actually */ 130686f8cdf3SVladimir Sementsov-Ogievskiy }; 130786f8cdf3SVladimir Sementsov-Ogievskiy 1308f34b2bcfSVladimir Sementsov-Ogievskiy assert(bytes <= UINT32_MAX); /* rely on max_pwrite_zeroes */ 1309f34b2bcfSVladimir Sementsov-Ogievskiy 1310611ae1d7SVladimir Sementsov-Ogievskiy assert(!(s->info.flags & NBD_FLAG_READ_ONLY)); 1311611ae1d7SVladimir Sementsov-Ogievskiy if (!(s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES)) { 131286f8cdf3SVladimir Sementsov-Ogievskiy return -ENOTSUP; 131386f8cdf3SVladimir Sementsov-Ogievskiy } 131486f8cdf3SVladimir Sementsov-Ogievskiy 131586f8cdf3SVladimir Sementsov-Ogievskiy if (flags & BDRV_REQ_FUA) { 1316611ae1d7SVladimir Sementsov-Ogievskiy assert(s->info.flags & NBD_FLAG_SEND_FUA); 131786f8cdf3SVladimir Sementsov-Ogievskiy request.flags |= NBD_CMD_FLAG_FUA; 131886f8cdf3SVladimir Sementsov-Ogievskiy } 131986f8cdf3SVladimir Sementsov-Ogievskiy if (!(flags & BDRV_REQ_MAY_UNMAP)) { 132086f8cdf3SVladimir Sementsov-Ogievskiy request.flags |= NBD_CMD_FLAG_NO_HOLE; 132186f8cdf3SVladimir Sementsov-Ogievskiy } 1322f061656cSEric Blake if (flags & BDRV_REQ_NO_FALLBACK) { 1323f061656cSEric Blake assert(s->info.flags & NBD_FLAG_SEND_FAST_ZERO); 1324f061656cSEric Blake request.flags |= NBD_CMD_FLAG_FAST_ZERO; 1325f061656cSEric Blake } 132686f8cdf3SVladimir Sementsov-Ogievskiy 132786f8cdf3SVladimir Sementsov-Ogievskiy if (!bytes) { 132886f8cdf3SVladimir Sementsov-Ogievskiy return 0; 132986f8cdf3SVladimir Sementsov-Ogievskiy } 133086f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_request(bs, &request, NULL); 133186f8cdf3SVladimir Sementsov-Ogievskiy } 133286f8cdf3SVladimir Sementsov-Ogievskiy 133369aa0d37SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK nbd_client_co_flush(BlockDriverState *bs) 133486f8cdf3SVladimir Sementsov-Ogievskiy { 1335611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 133686f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { .type = NBD_CMD_FLUSH }; 133786f8cdf3SVladimir Sementsov-Ogievskiy 1338611ae1d7SVladimir Sementsov-Ogievskiy if (!(s->info.flags & NBD_FLAG_SEND_FLUSH)) { 133986f8cdf3SVladimir Sementsov-Ogievskiy return 0; 134086f8cdf3SVladimir Sementsov-Ogievskiy } 134186f8cdf3SVladimir Sementsov-Ogievskiy 134286f8cdf3SVladimir Sementsov-Ogievskiy request.from = 0; 134386f8cdf3SVladimir Sementsov-Ogievskiy request.len = 0; 134486f8cdf3SVladimir Sementsov-Ogievskiy 134586f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_request(bs, &request, NULL); 134686f8cdf3SVladimir Sementsov-Ogievskiy } 134786f8cdf3SVladimir Sementsov-Ogievskiy 134869aa0d37SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK 134969aa0d37SEmanuele Giuseppe Esposito nbd_client_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes) 135086f8cdf3SVladimir Sementsov-Ogievskiy { 1351611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 135286f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 135386f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_TRIM, 135486f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 13550c802287SVladimir Sementsov-Ogievskiy .len = bytes, /* len is uint32_t */ 135686f8cdf3SVladimir Sementsov-Ogievskiy }; 135786f8cdf3SVladimir Sementsov-Ogievskiy 13580c802287SVladimir Sementsov-Ogievskiy assert(bytes <= UINT32_MAX); /* rely on max_pdiscard */ 13590c802287SVladimir Sementsov-Ogievskiy 1360611ae1d7SVladimir Sementsov-Ogievskiy assert(!(s->info.flags & NBD_FLAG_READ_ONLY)); 1361611ae1d7SVladimir Sementsov-Ogievskiy if (!(s->info.flags & NBD_FLAG_SEND_TRIM) || !bytes) { 136286f8cdf3SVladimir Sementsov-Ogievskiy return 0; 136386f8cdf3SVladimir Sementsov-Ogievskiy } 136486f8cdf3SVladimir Sementsov-Ogievskiy 136586f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_request(bs, &request, NULL); 136686f8cdf3SVladimir Sementsov-Ogievskiy } 136786f8cdf3SVladimir Sementsov-Ogievskiy 136869aa0d37SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK nbd_client_co_block_status( 136986f8cdf3SVladimir Sementsov-Ogievskiy BlockDriverState *bs, bool want_zero, int64_t offset, int64_t bytes, 137086f8cdf3SVladimir Sementsov-Ogievskiy int64_t *pnum, int64_t *map, BlockDriverState **file) 137186f8cdf3SVladimir Sementsov-Ogievskiy { 137286f8cdf3SVladimir Sementsov-Ogievskiy int ret, request_ret; 137386f8cdf3SVladimir Sementsov-Ogievskiy NBDExtent extent = { 0 }; 1374611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 137586f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 137686f8cdf3SVladimir Sementsov-Ogievskiy 137786f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 137886f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_BLOCK_STATUS, 137986f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 13806bf792b4SVladimir Sementsov-Ogievskiy .len = MIN(QEMU_ALIGN_DOWN(INT_MAX, bs->bl.request_alignment), 1381611ae1d7SVladimir Sementsov-Ogievskiy MIN(bytes, s->info.size - offset)), 138286f8cdf3SVladimir Sementsov-Ogievskiy .flags = NBD_CMD_FLAG_REQ_ONE, 138386f8cdf3SVladimir Sementsov-Ogievskiy }; 138486f8cdf3SVladimir Sementsov-Ogievskiy 1385611ae1d7SVladimir Sementsov-Ogievskiy if (!s->info.base_allocation) { 138686f8cdf3SVladimir Sementsov-Ogievskiy *pnum = bytes; 138786f8cdf3SVladimir Sementsov-Ogievskiy *map = offset; 138886f8cdf3SVladimir Sementsov-Ogievskiy *file = bs; 138986f8cdf3SVladimir Sementsov-Ogievskiy return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; 139086f8cdf3SVladimir Sementsov-Ogievskiy } 139186f8cdf3SVladimir Sementsov-Ogievskiy 139286f8cdf3SVladimir Sementsov-Ogievskiy /* 139386f8cdf3SVladimir Sementsov-Ogievskiy * Work around the fact that the block layer doesn't do 139486f8cdf3SVladimir Sementsov-Ogievskiy * byte-accurate sizing yet - if the status request exceeds the 139586f8cdf3SVladimir Sementsov-Ogievskiy * server's advertised size because the block layer rounded size 139686f8cdf3SVladimir Sementsov-Ogievskiy * up, we truncated the request to the server (above), or are 139786f8cdf3SVladimir Sementsov-Ogievskiy * called on just the hole. 139886f8cdf3SVladimir Sementsov-Ogievskiy */ 1399611ae1d7SVladimir Sementsov-Ogievskiy if (offset >= s->info.size) { 140086f8cdf3SVladimir Sementsov-Ogievskiy *pnum = bytes; 140186f8cdf3SVladimir Sementsov-Ogievskiy assert(bytes < BDRV_SECTOR_SIZE); 140286f8cdf3SVladimir Sementsov-Ogievskiy /* Intentionally don't report offset_valid for the hole */ 140386f8cdf3SVladimir Sementsov-Ogievskiy return BDRV_BLOCK_ZERO; 140486f8cdf3SVladimir Sementsov-Ogievskiy } 140586f8cdf3SVladimir Sementsov-Ogievskiy 1406611ae1d7SVladimir Sementsov-Ogievskiy if (s->info.min_block) { 1407611ae1d7SVladimir Sementsov-Ogievskiy assert(QEMU_IS_ALIGNED(request.len, s->info.min_block)); 140886f8cdf3SVladimir Sementsov-Ogievskiy } 1409f7651539SVladimir Sementsov-Ogievskiy do { 141086f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_co_send_request(bs, &request, NULL); 141186f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1412f7651539SVladimir Sementsov-Ogievskiy continue; 141386f8cdf3SVladimir Sementsov-Ogievskiy } 141486f8cdf3SVladimir Sementsov-Ogievskiy 141522efd811SEric Blake ret = nbd_co_receive_blockstatus_reply(s, request.cookie, bytes, 1416f7651539SVladimir Sementsov-Ogievskiy &extent, &request_ret, 1417f7651539SVladimir Sementsov-Ogievskiy &local_err); 141886f8cdf3SVladimir Sementsov-Ogievskiy if (local_err) { 141922efd811SEric Blake trace_nbd_co_request_fail(request.from, request.len, request.cookie, 142086f8cdf3SVladimir Sementsov-Ogievskiy request.flags, request.type, 142186f8cdf3SVladimir Sementsov-Ogievskiy nbd_cmd_lookup(request.type), 142286f8cdf3SVladimir Sementsov-Ogievskiy ret, error_get_pretty(local_err)); 142386f8cdf3SVladimir Sementsov-Ogievskiy error_free(local_err); 1424f7651539SVladimir Sementsov-Ogievskiy local_err = NULL; 142586f8cdf3SVladimir Sementsov-Ogievskiy } 14268d45185cSPaolo Bonzini } while (ret < 0 && nbd_client_will_reconnect(s)); 1427f7651539SVladimir Sementsov-Ogievskiy 142886f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0 || request_ret < 0) { 142986f8cdf3SVladimir Sementsov-Ogievskiy return ret ? ret : request_ret; 143086f8cdf3SVladimir Sementsov-Ogievskiy } 143186f8cdf3SVladimir Sementsov-Ogievskiy 143286f8cdf3SVladimir Sementsov-Ogievskiy assert(extent.length); 143386f8cdf3SVladimir Sementsov-Ogievskiy *pnum = extent.length; 143486f8cdf3SVladimir Sementsov-Ogievskiy *map = offset; 143586f8cdf3SVladimir Sementsov-Ogievskiy *file = bs; 143686f8cdf3SVladimir Sementsov-Ogievskiy return (extent.flags & NBD_STATE_HOLE ? 0 : BDRV_BLOCK_DATA) | 143786f8cdf3SVladimir Sementsov-Ogievskiy (extent.flags & NBD_STATE_ZERO ? BDRV_BLOCK_ZERO : 0) | 143886f8cdf3SVladimir Sementsov-Ogievskiy BDRV_BLOCK_OFFSET_VALID; 143986f8cdf3SVladimir Sementsov-Ogievskiy } 144086f8cdf3SVladimir Sementsov-Ogievskiy 1441e99754b4SMaxim Levitsky static int nbd_client_reopen_prepare(BDRVReopenState *state, 1442e99754b4SMaxim Levitsky BlockReopenQueue *queue, Error **errp) 1443e99754b4SMaxim Levitsky { 1444e99754b4SMaxim Levitsky BDRVNBDState *s = (BDRVNBDState *)state->bs->opaque; 1445e99754b4SMaxim Levitsky 1446e99754b4SMaxim Levitsky if ((state->flags & BDRV_O_RDWR) && (s->info.flags & NBD_FLAG_READ_ONLY)) { 1447e99754b4SMaxim Levitsky error_setg(errp, "Can't reopen read-only NBD mount as read/write"); 1448e99754b4SMaxim Levitsky return -EACCES; 1449e99754b4SMaxim Levitsky } 1450e99754b4SMaxim Levitsky return 0; 1451e99754b4SMaxim Levitsky } 1452e99754b4SMaxim Levitsky 1453fee091cdSLukas Straub static void nbd_yank(void *opaque) 1454fee091cdSLukas Straub { 1455fee091cdSLukas Straub BlockDriverState *bs = opaque; 1456fee091cdSLukas Straub BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 1457fee091cdSLukas Straub 1458dba5156cSPaolo Bonzini QEMU_LOCK_GUARD(&s->requests_lock); 14597d5b0d68SPhilippe Mathieu-Daudé qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); 1460dba5156cSPaolo Bonzini s->state = NBD_CLIENT_QUIT; 1461fee091cdSLukas Straub } 1462fee091cdSLukas Straub 146386f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_client_close(BlockDriverState *bs) 146486f8cdf3SVladimir Sementsov-Ogievskiy { 1465611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 146686f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { .type = NBD_CMD_DISC }; 146786f8cdf3SVladimir Sementsov-Ogievskiy 1468f7651539SVladimir Sementsov-Ogievskiy if (s->ioc) { 1469611ae1d7SVladimir Sementsov-Ogievskiy nbd_send_request(s->ioc, &request); 1470f7651539SVladimir Sementsov-Ogievskiy } 147186f8cdf3SVladimir Sementsov-Ogievskiy 147286f8cdf3SVladimir Sementsov-Ogievskiy nbd_teardown_connection(bs); 147386f8cdf3SVladimir Sementsov-Ogievskiy } 147486f8cdf3SVladimir Sementsov-Ogievskiy 147586f8cdf3SVladimir Sementsov-Ogievskiy 14768f071c9dSVladimir Sementsov-Ogievskiy /* 14778f071c9dSVladimir Sementsov-Ogievskiy * Parse nbd_open options 14788f071c9dSVladimir Sementsov-Ogievskiy */ 147986f8cdf3SVladimir Sementsov-Ogievskiy 1480f53a1febSKevin Wolf static int nbd_parse_uri(const char *filename, QDict *options) 14811d7d2a9dSPaolo Bonzini { 14821d7d2a9dSPaolo Bonzini URI *uri; 14831d7d2a9dSPaolo Bonzini const char *p; 14841d7d2a9dSPaolo Bonzini QueryParams *qp = NULL; 14851d7d2a9dSPaolo Bonzini int ret = 0; 1486f53a1febSKevin Wolf bool is_unix; 14871d7d2a9dSPaolo Bonzini 14881d7d2a9dSPaolo Bonzini uri = uri_parse(filename); 14891d7d2a9dSPaolo Bonzini if (!uri) { 14901d7d2a9dSPaolo Bonzini return -EINVAL; 14911d7d2a9dSPaolo Bonzini } 14921d7d2a9dSPaolo Bonzini 14931d7d2a9dSPaolo Bonzini /* transport */ 1494f69165a8SMax Reitz if (!g_strcmp0(uri->scheme, "nbd")) { 1495f53a1febSKevin Wolf is_unix = false; 1496f69165a8SMax Reitz } else if (!g_strcmp0(uri->scheme, "nbd+tcp")) { 1497f53a1febSKevin Wolf is_unix = false; 1498f69165a8SMax Reitz } else if (!g_strcmp0(uri->scheme, "nbd+unix")) { 1499f53a1febSKevin Wolf is_unix = true; 15001d7d2a9dSPaolo Bonzini } else { 15011d7d2a9dSPaolo Bonzini ret = -EINVAL; 15021d7d2a9dSPaolo Bonzini goto out; 15031d7d2a9dSPaolo Bonzini } 15041d7d2a9dSPaolo Bonzini 15052485f22fSEric Blake p = uri->path ? uri->path : ""; 15062485f22fSEric Blake if (p[0] == '/') { 15072485f22fSEric Blake p++; 15082485f22fSEric Blake } 15091d7d2a9dSPaolo Bonzini if (p[0]) { 151046f5ac20SEric Blake qdict_put_str(options, "export", p); 15111d7d2a9dSPaolo Bonzini } 15121d7d2a9dSPaolo Bonzini 15131d7d2a9dSPaolo Bonzini qp = query_params_parse(uri->query); 1514f53a1febSKevin Wolf if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) { 15151d7d2a9dSPaolo Bonzini ret = -EINVAL; 15161d7d2a9dSPaolo Bonzini goto out; 15171d7d2a9dSPaolo Bonzini } 15181d7d2a9dSPaolo Bonzini 1519f53a1febSKevin Wolf if (is_unix) { 15201d7d2a9dSPaolo Bonzini /* nbd+unix:///export?socket=path */ 15211d7d2a9dSPaolo Bonzini if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) { 15221d7d2a9dSPaolo Bonzini ret = -EINVAL; 15231d7d2a9dSPaolo Bonzini goto out; 15241d7d2a9dSPaolo Bonzini } 152546f5ac20SEric Blake qdict_put_str(options, "server.type", "unix"); 152646f5ac20SEric Blake qdict_put_str(options, "server.path", qp->p[0].value); 15271d7d2a9dSPaolo Bonzini } else { 152823307908SJán Tomko QString *host; 1529f84d431bSMax Reitz char *port_str; 1530f84d431bSMax Reitz 1531bebbf7faSKevin Wolf /* nbd[+tcp]://host[:port]/export */ 15321d7d2a9dSPaolo Bonzini if (!uri->server) { 15331d7d2a9dSPaolo Bonzini ret = -EINVAL; 15341d7d2a9dSPaolo Bonzini goto out; 15351d7d2a9dSPaolo Bonzini } 1536f17c90beSKevin Wolf 153723307908SJán Tomko /* strip braces from literal IPv6 address */ 153823307908SJán Tomko if (uri->server[0] == '[') { 153923307908SJán Tomko host = qstring_from_substr(uri->server, 1, 1540ba891d68SMarkus Armbruster strlen(uri->server) - 1); 154123307908SJán Tomko } else { 154223307908SJán Tomko host = qstring_from_str(uri->server); 154323307908SJán Tomko } 154423307908SJán Tomko 154546f5ac20SEric Blake qdict_put_str(options, "server.type", "inet"); 15469445673eSMarkus Armbruster qdict_put(options, "server.host", host); 1547f84d431bSMax Reitz 1548f84d431bSMax Reitz port_str = g_strdup_printf("%d", uri->port ?: NBD_DEFAULT_PORT); 154946f5ac20SEric Blake qdict_put_str(options, "server.port", port_str); 1550f53a1febSKevin Wolf g_free(port_str); 15511d7d2a9dSPaolo Bonzini } 15521d7d2a9dSPaolo Bonzini 15531d7d2a9dSPaolo Bonzini out: 15541d7d2a9dSPaolo Bonzini if (qp) { 15551d7d2a9dSPaolo Bonzini query_params_free(qp); 15561d7d2a9dSPaolo Bonzini } 15571d7d2a9dSPaolo Bonzini uri_free(uri); 15581d7d2a9dSPaolo Bonzini return ret; 15591d7d2a9dSPaolo Bonzini } 15601d7d2a9dSPaolo Bonzini 156148c38e0bSMax Reitz static bool nbd_has_filename_options_conflict(QDict *options, Error **errp) 156248c38e0bSMax Reitz { 156348c38e0bSMax Reitz const QDictEntry *e; 156448c38e0bSMax Reitz 156548c38e0bSMax Reitz for (e = qdict_first(options); e; e = qdict_next(options, e)) { 156648c38e0bSMax Reitz if (!strcmp(e->key, "host") || 156748c38e0bSMax Reitz !strcmp(e->key, "port") || 156848c38e0bSMax Reitz !strcmp(e->key, "path") || 1569491d6c7cSMax Reitz !strcmp(e->key, "export") || 1570491d6c7cSMax Reitz strstart(e->key, "server.", NULL)) 157148c38e0bSMax Reitz { 157248c38e0bSMax Reitz error_setg(errp, "Option '%s' cannot be used with a file name", 157348c38e0bSMax Reitz e->key); 157448c38e0bSMax Reitz return true; 157548c38e0bSMax Reitz } 157648c38e0bSMax Reitz } 157748c38e0bSMax Reitz 157848c38e0bSMax Reitz return false; 157948c38e0bSMax Reitz } 158048c38e0bSMax Reitz 15816963a30dSKevin Wolf static void nbd_parse_filename(const char *filename, QDict *options, 15826963a30dSKevin Wolf Error **errp) 1583019d6b8fSAnthony Liguori { 1584df18c04eSEric Blake g_autofree char *file = NULL; 158533897dc7SNick Thomas char *export_name; 158633897dc7SNick Thomas const char *host_spec; 1587019d6b8fSAnthony Liguori const char *unixpath; 1588019d6b8fSAnthony Liguori 158948c38e0bSMax Reitz if (nbd_has_filename_options_conflict(options, errp)) { 1590681e7ad0SKevin Wolf return; 1591681e7ad0SKevin Wolf } 1592681e7ad0SKevin Wolf 15931d7d2a9dSPaolo Bonzini if (strstr(filename, "://")) { 15946963a30dSKevin Wolf int ret = nbd_parse_uri(filename, options); 15956963a30dSKevin Wolf if (ret < 0) { 15966963a30dSKevin Wolf error_setg(errp, "No valid URL specified"); 15976963a30dSKevin Wolf } 15986963a30dSKevin Wolf return; 15991d7d2a9dSPaolo Bonzini } 16001d7d2a9dSPaolo Bonzini 16017267c094SAnthony Liguori file = g_strdup(filename); 16021d45f8b5SLaurent Vivier 160333897dc7SNick Thomas export_name = strstr(file, EN_OPTSTR); 160433897dc7SNick Thomas if (export_name) { 160533897dc7SNick Thomas if (export_name[strlen(EN_OPTSTR)] == 0) { 1606df18c04eSEric Blake return; 16071d45f8b5SLaurent Vivier } 160833897dc7SNick Thomas export_name[0] = 0; /* truncate 'file' */ 160933897dc7SNick Thomas export_name += strlen(EN_OPTSTR); 1610f53a1febSKevin Wolf 161146f5ac20SEric Blake qdict_put_str(options, "export", export_name); 16121d45f8b5SLaurent Vivier } 16131d45f8b5SLaurent Vivier 161433897dc7SNick Thomas /* extract the host_spec - fail if it's not nbd:... */ 161533897dc7SNick Thomas if (!strstart(file, "nbd:", &host_spec)) { 16166963a30dSKevin Wolf error_setg(errp, "File name string for NBD must start with 'nbd:'"); 1617df18c04eSEric Blake return; 16181d45f8b5SLaurent Vivier } 1619019d6b8fSAnthony Liguori 1620f53a1febSKevin Wolf if (!*host_spec) { 1621df18c04eSEric Blake return; 1622f53a1febSKevin Wolf } 1623f53a1febSKevin Wolf 162433897dc7SNick Thomas /* are we a UNIX or TCP socket? */ 162533897dc7SNick Thomas if (strstart(host_spec, "unix:", &unixpath)) { 162646f5ac20SEric Blake qdict_put_str(options, "server.type", "unix"); 162746f5ac20SEric Blake qdict_put_str(options, "server.path", unixpath); 1628019d6b8fSAnthony Liguori } else { 16290785bd7aSMarkus Armbruster InetSocketAddress *addr = g_new(InetSocketAddress, 1); 1630f53a1febSKevin Wolf 16310785bd7aSMarkus Armbruster if (inet_parse(addr, host_spec, errp)) { 16320785bd7aSMarkus Armbruster goto out_inet; 1633f17c90beSKevin Wolf } 1634f53a1febSKevin Wolf 163546f5ac20SEric Blake qdict_put_str(options, "server.type", "inet"); 163646f5ac20SEric Blake qdict_put_str(options, "server.host", addr->host); 163746f5ac20SEric Blake qdict_put_str(options, "server.port", addr->port); 16380785bd7aSMarkus Armbruster out_inet: 1639f53a1febSKevin Wolf qapi_free_InetSocketAddress(addr); 16401d45f8b5SLaurent Vivier } 164133897dc7SNick Thomas } 1642f53a1febSKevin Wolf 1643491d6c7cSMax Reitz static bool nbd_process_legacy_socket_options(QDict *output_options, 1644491d6c7cSMax Reitz QemuOpts *legacy_opts, 1645491d6c7cSMax Reitz Error **errp) 1646f53a1febSKevin Wolf { 1647491d6c7cSMax Reitz const char *path = qemu_opt_get(legacy_opts, "path"); 1648491d6c7cSMax Reitz const char *host = qemu_opt_get(legacy_opts, "host"); 1649491d6c7cSMax Reitz const char *port = qemu_opt_get(legacy_opts, "port"); 1650491d6c7cSMax Reitz const QDictEntry *e; 1651f53a1febSKevin Wolf 1652491d6c7cSMax Reitz if (!path && !host && !port) { 1653491d6c7cSMax Reitz return true; 1654491d6c7cSMax Reitz } 165503504d05SMax Reitz 1656491d6c7cSMax Reitz for (e = qdict_first(output_options); e; e = qdict_next(output_options, e)) 1657491d6c7cSMax Reitz { 1658491d6c7cSMax Reitz if (strstart(e->key, "server.", NULL)) { 1659491d6c7cSMax Reitz error_setg(errp, "Cannot use 'server' and path/host/port at the " 1660491d6c7cSMax Reitz "same time"); 1661491d6c7cSMax Reitz return false; 1662491d6c7cSMax Reitz } 1663491d6c7cSMax Reitz } 1664491d6c7cSMax Reitz 1665491d6c7cSMax Reitz if (path && host) { 166682d73014SMax Reitz error_setg(errp, "path and host may not be used at the same time"); 1667491d6c7cSMax Reitz return false; 1668491d6c7cSMax Reitz } else if (path) { 1669491d6c7cSMax Reitz if (port) { 1670442045cbSMax Reitz error_setg(errp, "port may not be used without host"); 1671491d6c7cSMax Reitz return false; 1672442045cbSMax Reitz } 1673019d6b8fSAnthony Liguori 167446f5ac20SEric Blake qdict_put_str(output_options, "server.type", "unix"); 167546f5ac20SEric Blake qdict_put_str(output_options, "server.path", path); 1676491d6c7cSMax Reitz } else if (host) { 167746f5ac20SEric Blake qdict_put_str(output_options, "server.type", "inet"); 167846f5ac20SEric Blake qdict_put_str(output_options, "server.host", host); 167946f5ac20SEric Blake qdict_put_str(output_options, "server.port", 168046f5ac20SEric Blake port ?: stringify(NBD_DEFAULT_PORT)); 16817a5ed437SDaniel P. Berrange } 1682491d6c7cSMax Reitz 1683491d6c7cSMax Reitz return true; 1684491d6c7cSMax Reitz } 1685491d6c7cSMax Reitz 168662cf396bSMarkus Armbruster static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options, 16879445673eSMarkus Armbruster Error **errp) 1688491d6c7cSMax Reitz { 168962cf396bSMarkus Armbruster SocketAddress *saddr = NULL; 1690491d6c7cSMax Reitz QDict *addr = NULL; 1691491d6c7cSMax Reitz Visitor *iv = NULL; 1692491d6c7cSMax Reitz 1693491d6c7cSMax Reitz qdict_extract_subqdict(options, &addr, "server."); 1694491d6c7cSMax Reitz if (!qdict_size(addr)) { 1695491d6c7cSMax Reitz error_setg(errp, "NBD server address missing"); 1696491d6c7cSMax Reitz goto done; 1697491d6c7cSMax Reitz } 1698491d6c7cSMax Reitz 1699af91062eSMarkus Armbruster iv = qobject_input_visitor_new_flat_confused(addr, errp); 1700af91062eSMarkus Armbruster if (!iv) { 1701491d6c7cSMax Reitz goto done; 1702491d6c7cSMax Reitz } 1703491d6c7cSMax Reitz 1704af175e85SMarkus Armbruster if (!visit_type_SocketAddress(iv, NULL, &saddr, errp)) { 1705491d6c7cSMax Reitz goto done; 1706f53a1febSKevin Wolf } 1707f53a1febSKevin Wolf 17086cc702beSVladimir Sementsov-Ogievskiy if (socket_address_parse_named_fd(saddr, errp) < 0) { 17096cc702beSVladimir Sementsov-Ogievskiy qapi_free_SocketAddress(saddr); 17106cc702beSVladimir Sementsov-Ogievskiy saddr = NULL; 17116cc702beSVladimir Sementsov-Ogievskiy goto done; 17126cc702beSVladimir Sementsov-Ogievskiy } 17136cc702beSVladimir Sementsov-Ogievskiy 1714491d6c7cSMax Reitz done: 1715cb3e7f08SMarc-André Lureau qobject_unref(addr); 1716491d6c7cSMax Reitz visit_free(iv); 17177a5ed437SDaniel P. Berrange return saddr; 1718f53a1febSKevin Wolf } 1719f53a1febSKevin Wolf 172075822a12SDaniel P. Berrange static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp) 172175822a12SDaniel P. Berrange { 172275822a12SDaniel P. Berrange Object *obj; 172375822a12SDaniel P. Berrange QCryptoTLSCreds *creds; 172475822a12SDaniel P. Berrange 172575822a12SDaniel P. Berrange obj = object_resolve_path_component( 172675822a12SDaniel P. Berrange object_get_objects_root(), id); 172775822a12SDaniel P. Berrange if (!obj) { 172875822a12SDaniel P. Berrange error_setg(errp, "No TLS credentials with id '%s'", 172975822a12SDaniel P. Berrange id); 173075822a12SDaniel P. Berrange return NULL; 173175822a12SDaniel P. Berrange } 173275822a12SDaniel P. Berrange creds = (QCryptoTLSCreds *) 173375822a12SDaniel P. Berrange object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS); 173475822a12SDaniel P. Berrange if (!creds) { 173575822a12SDaniel P. Berrange error_setg(errp, "Object with id '%s' is not TLS credentials", 173675822a12SDaniel P. Berrange id); 173775822a12SDaniel P. Berrange return NULL; 173875822a12SDaniel P. Berrange } 173975822a12SDaniel P. Berrange 17407b3b6168SPhilippe Mathieu-Daudé if (!qcrypto_tls_creds_check_endpoint(creds, 17417b3b6168SPhilippe Mathieu-Daudé QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT, 17427b3b6168SPhilippe Mathieu-Daudé errp)) { 174375822a12SDaniel P. Berrange return NULL; 174475822a12SDaniel P. Berrange } 174575822a12SDaniel P. Berrange object_ref(obj); 174675822a12SDaniel P. Berrange return creds; 174775822a12SDaniel P. Berrange } 174875822a12SDaniel P. Berrange 174975822a12SDaniel P. Berrange 17507ccc44fdSMax Reitz static QemuOptsList nbd_runtime_opts = { 17517ccc44fdSMax Reitz .name = "nbd", 17527ccc44fdSMax Reitz .head = QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts.head), 17537ccc44fdSMax Reitz .desc = { 17547ccc44fdSMax Reitz { 17557ccc44fdSMax Reitz .name = "host", 17567ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 17577ccc44fdSMax Reitz .help = "TCP host to connect to", 17587ccc44fdSMax Reitz }, 17597ccc44fdSMax Reitz { 17607ccc44fdSMax Reitz .name = "port", 17617ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 17627ccc44fdSMax Reitz .help = "TCP port to connect to", 17637ccc44fdSMax Reitz }, 17647ccc44fdSMax Reitz { 17657ccc44fdSMax Reitz .name = "path", 17667ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 17677ccc44fdSMax Reitz .help = "Unix socket path to connect to", 17687ccc44fdSMax Reitz }, 17697ccc44fdSMax Reitz { 17707ccc44fdSMax Reitz .name = "export", 17717ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 17727ccc44fdSMax Reitz .help = "Name of the NBD export to open", 17737ccc44fdSMax Reitz }, 17747ccc44fdSMax Reitz { 17757ccc44fdSMax Reitz .name = "tls-creds", 17767ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 17777ccc44fdSMax Reitz .help = "ID of the TLS credentials to use", 17787ccc44fdSMax Reitz }, 1779216ee365SEric Blake { 1780a0cd6d29SDaniel P. Berrangé .name = "tls-hostname", 1781a0cd6d29SDaniel P. Berrangé .type = QEMU_OPT_STRING, 1782a0cd6d29SDaniel P. Berrangé .help = "Override hostname for validating TLS x509 certificate", 1783a0cd6d29SDaniel P. Berrangé }, 1784a0cd6d29SDaniel P. Berrangé { 1785216ee365SEric Blake .name = "x-dirty-bitmap", 1786216ee365SEric Blake .type = QEMU_OPT_STRING, 1787216ee365SEric Blake .help = "experimental: expose named dirty bitmap in place of " 1788216ee365SEric Blake "block status", 1789216ee365SEric Blake }, 1790b172ae2eSVladimir Sementsov-Ogievskiy { 1791b172ae2eSVladimir Sementsov-Ogievskiy .name = "reconnect-delay", 1792b172ae2eSVladimir Sementsov-Ogievskiy .type = QEMU_OPT_NUMBER, 1793b172ae2eSVladimir Sementsov-Ogievskiy .help = "On an unexpected disconnect, the nbd client tries to " 1794b172ae2eSVladimir Sementsov-Ogievskiy "connect again until succeeding or encountering a serious " 1795b172ae2eSVladimir Sementsov-Ogievskiy "error. During the first @reconnect-delay seconds, all " 1796b172ae2eSVladimir Sementsov-Ogievskiy "requests are paused and will be rerun on a successful " 1797b172ae2eSVladimir Sementsov-Ogievskiy "reconnect. After that time, any delayed requests and all " 1798b172ae2eSVladimir Sementsov-Ogievskiy "future requests before a successful reconnect will " 1799b172ae2eSVladimir Sementsov-Ogievskiy "immediately fail. Default 0", 1800b172ae2eSVladimir Sementsov-Ogievskiy }, 1801be16b8bfSVladimir Sementsov-Ogievskiy { 1802be16b8bfSVladimir Sementsov-Ogievskiy .name = "open-timeout", 1803be16b8bfSVladimir Sementsov-Ogievskiy .type = QEMU_OPT_NUMBER, 1804be16b8bfSVladimir Sementsov-Ogievskiy .help = "In seconds. If zero, the nbd driver tries the connection " 1805be16b8bfSVladimir Sementsov-Ogievskiy "only once, and fails to open if the connection fails. " 1806be16b8bfSVladimir Sementsov-Ogievskiy "If non-zero, the nbd driver will repeat connection " 1807be16b8bfSVladimir Sementsov-Ogievskiy "attempts until successful or until @open-timeout seconds " 1808be16b8bfSVladimir Sementsov-Ogievskiy "have elapsed. Default 0", 1809be16b8bfSVladimir Sementsov-Ogievskiy }, 1810c4365735SMurilo Opsfelder Araujo { /* end of list */ } 18117ccc44fdSMax Reitz }, 18127ccc44fdSMax Reitz }; 18137ccc44fdSMax Reitz 18148f071c9dSVladimir Sementsov-Ogievskiy static int nbd_process_options(BlockDriverState *bs, QDict *options, 1815015a1036SMax Reitz Error **errp) 181633897dc7SNick Thomas { 181733897dc7SNick Thomas BDRVNBDState *s = bs->opaque; 18188f071c9dSVladimir Sementsov-Ogievskiy QemuOpts *opts; 181975822a12SDaniel P. Berrange int ret = -EINVAL; 1820ae255e52SPaolo Bonzini 18217ccc44fdSMax Reitz opts = qemu_opts_create(&nbd_runtime_opts, NULL, 0, &error_abort); 1822af175e85SMarkus Armbruster if (!qemu_opts_absorb_qdict(opts, options, errp)) { 18237ccc44fdSMax Reitz goto error; 18247ccc44fdSMax Reitz } 18257ccc44fdSMax Reitz 182662cf396bSMarkus Armbruster /* Translate @host, @port, and @path to a SocketAddress */ 1827491d6c7cSMax Reitz if (!nbd_process_legacy_socket_options(options, opts, errp)) { 182875822a12SDaniel P. Berrange goto error; 182975822a12SDaniel P. Berrange } 183075822a12SDaniel P. Berrange 1831491d6c7cSMax Reitz /* Pop the config into our state object. Exit if invalid. */ 1832491d6c7cSMax Reitz s->saddr = nbd_config(s, options, errp); 1833491d6c7cSMax Reitz if (!s->saddr) { 1834491d6c7cSMax Reitz goto error; 1835491d6c7cSMax Reitz } 1836491d6c7cSMax Reitz 1837491d6c7cSMax Reitz s->export = g_strdup(qemu_opt_get(opts, "export")); 183893676c88SEric Blake if (s->export && strlen(s->export) > NBD_MAX_STRING_SIZE) { 183993676c88SEric Blake error_setg(errp, "export name too long to send to server"); 184093676c88SEric Blake goto error; 184193676c88SEric Blake } 1842491d6c7cSMax Reitz 184303504d05SMax Reitz s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds")); 184403504d05SMax Reitz if (s->tlscredsid) { 18458f071c9dSVladimir Sementsov-Ogievskiy s->tlscreds = nbd_get_tls_creds(s->tlscredsid, errp); 18468f071c9dSVladimir Sementsov-Ogievskiy if (!s->tlscreds) { 184775822a12SDaniel P. Berrange goto error; 184875822a12SDaniel P. Berrange } 184975822a12SDaniel P. Berrange 1850a0cd6d29SDaniel P. Berrangé s->tlshostname = g_strdup(qemu_opt_get(opts, "tls-hostname")); 1851e8ae8b1aSDaniel P. Berrangé if (!s->tlshostname && 1852e8ae8b1aSDaniel P. Berrangé s->saddr->type == SOCKET_ADDRESS_TYPE_INET) { 1853a0cd6d29SDaniel P. Berrangé s->tlshostname = g_strdup(s->saddr->u.inet.host); 1854a0cd6d29SDaniel P. Berrangé } 185533897dc7SNick Thomas } 185633897dc7SNick Thomas 18578f071c9dSVladimir Sementsov-Ogievskiy s->x_dirty_bitmap = g_strdup(qemu_opt_get(opts, "x-dirty-bitmap")); 185893676c88SEric Blake if (s->x_dirty_bitmap && strlen(s->x_dirty_bitmap) > NBD_MAX_STRING_SIZE) { 185993676c88SEric Blake error_setg(errp, "x-dirty-bitmap query too long to send to server"); 186093676c88SEric Blake goto error; 186193676c88SEric Blake } 186293676c88SEric Blake 18638f071c9dSVladimir Sementsov-Ogievskiy s->reconnect_delay = qemu_opt_get_number(opts, "reconnect-delay", 0); 1864be16b8bfSVladimir Sementsov-Ogievskiy s->open_timeout = qemu_opt_get_number(opts, "open-timeout", 0); 18658f071c9dSVladimir Sementsov-Ogievskiy 18668f071c9dSVladimir Sementsov-Ogievskiy ret = 0; 1867d42f78e9SVladimir Sementsov-Ogievskiy 186875822a12SDaniel P. Berrange error: 18697ccc44fdSMax Reitz qemu_opts_del(opts); 187075822a12SDaniel P. Berrange return ret; 1871ae255e52SPaolo Bonzini } 1872d9b09f13SPaolo Bonzini 18738f071c9dSVladimir Sementsov-Ogievskiy static int nbd_open(BlockDriverState *bs, QDict *options, int flags, 18748f071c9dSVladimir Sementsov-Ogievskiy Error **errp) 18758f071c9dSVladimir Sementsov-Ogievskiy { 18768f071c9dSVladimir Sementsov-Ogievskiy int ret; 18778f071c9dSVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 18788f071c9dSVladimir Sementsov-Ogievskiy 18798f071c9dSVladimir Sementsov-Ogievskiy s->bs = bs; 1880ee19d953SPaolo Bonzini qemu_mutex_init(&s->requests_lock); 18818f071c9dSVladimir Sementsov-Ogievskiy qemu_co_queue_init(&s->free_sema); 1882ee19d953SPaolo Bonzini qemu_co_mutex_init(&s->send_mutex); 18834ddb5d2fSVladimir Sementsov-Ogievskiy qemu_co_mutex_init(&s->receive_mutex); 18848f071c9dSVladimir Sementsov-Ogievskiy 1885fee091cdSLukas Straub if (!yank_register_instance(BLOCKDEV_YANK_INSTANCE(bs->node_name), errp)) { 1886fee091cdSLukas Straub return -EEXIST; 1887fee091cdSLukas Straub } 1888fee091cdSLukas Straub 1889bbba1c37SVladimir Sementsov-Ogievskiy ret = nbd_process_options(bs, options, errp); 1890bbba1c37SVladimir Sementsov-Ogievskiy if (ret < 0) { 1891bbba1c37SVladimir Sementsov-Ogievskiy goto fail; 1892bbba1c37SVladimir Sementsov-Ogievskiy } 1893bbba1c37SVladimir Sementsov-Ogievskiy 18946d2b0332SVladimir Sementsov-Ogievskiy s->conn = nbd_client_connection_new(s->saddr, true, s->export, 1895046f98d0SDaniel P. Berrangé s->x_dirty_bitmap, s->tlscreds, 1896046f98d0SDaniel P. Berrangé s->tlshostname); 1897e8b35bf5SRoman Kagan 1898be16b8bfSVladimir Sementsov-Ogievskiy if (s->open_timeout) { 1899be16b8bfSVladimir Sementsov-Ogievskiy nbd_client_connection_enable_retry(s->conn); 1900be16b8bfSVladimir Sementsov-Ogievskiy open_timer_init(s, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 1901be16b8bfSVladimir Sementsov-Ogievskiy s->open_timeout * NANOSECONDS_PER_SECOND); 1902be16b8bfSVladimir Sementsov-Ogievskiy } 1903be16b8bfSVladimir Sementsov-Ogievskiy 19044ddb5d2fSVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_CONNECTING_WAIT; 19058610b449SPaolo Bonzini ret = nbd_do_establish_connection(bs, true, errp); 19068f071c9dSVladimir Sementsov-Ogievskiy if (ret < 0) { 1907bbba1c37SVladimir Sementsov-Ogievskiy goto fail; 19088f071c9dSVladimir Sementsov-Ogievskiy } 19098f071c9dSVladimir Sementsov-Ogievskiy 1910717be964SHanna Reitz /* 1911717be964SHanna Reitz * The connect attempt is done, so we no longer need this timer. 1912717be964SHanna Reitz * Delete it, because we do not want it to be around when this node 1913717be964SHanna Reitz * is drained or closed. 1914717be964SHanna Reitz */ 1915717be964SHanna Reitz open_timer_del(s); 1916717be964SHanna Reitz 19174ddb5d2fSVladimir Sementsov-Ogievskiy nbd_client_connection_enable_retry(s->conn); 19188f071c9dSVladimir Sementsov-Ogievskiy 19198f071c9dSVladimir Sementsov-Ogievskiy return 0; 1920bbba1c37SVladimir Sementsov-Ogievskiy 1921bbba1c37SVladimir Sementsov-Ogievskiy fail: 1922717be964SHanna Reitz open_timer_del(s); 1923bbba1c37SVladimir Sementsov-Ogievskiy nbd_clear_bdrvstate(bs); 1924bbba1c37SVladimir Sementsov-Ogievskiy return ret; 19258f071c9dSVladimir Sementsov-Ogievskiy } 19268f071c9dSVladimir Sementsov-Ogievskiy 1927fa21e6faSDenis V. Lunev static void nbd_refresh_limits(BlockDriverState *bs, Error **errp) 1928fa21e6faSDenis V. Lunev { 1929611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 1930fd8d372dSEric Blake uint32_t min = s->info.min_block; 1931081dd1feSEric Blake uint32_t max = MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE, s->info.max_block); 1932081dd1feSEric Blake 19337da537f7SEric Blake /* 19347da537f7SEric Blake * If the server did not advertise an alignment: 19357da537f7SEric Blake * - a size that is not sector-aligned implies that an alignment 19367da537f7SEric Blake * of 1 can be used to access those tail bytes 19377da537f7SEric Blake * - advertisement of block status requires an alignment of 1, so 19387da537f7SEric Blake * that we don't violate block layer constraints that block 19397da537f7SEric Blake * status is always aligned (as we can't control whether the 19407da537f7SEric Blake * server will report sub-sector extents, such as a hole at EOF 19417da537f7SEric Blake * on an unaligned POSIX file) 19427da537f7SEric Blake * - otherwise, assume the server is so old that we are safer avoiding 19437da537f7SEric Blake * sub-sector requests 19447da537f7SEric Blake */ 19457da537f7SEric Blake if (!min) { 19467da537f7SEric Blake min = (!QEMU_IS_ALIGNED(s->info.size, BDRV_SECTOR_SIZE) || 19477da537f7SEric Blake s->info.base_allocation) ? 1 : BDRV_SECTOR_SIZE; 19487da537f7SEric Blake } 19497da537f7SEric Blake 19507da537f7SEric Blake bs->bl.request_alignment = min; 1951714eb0dbSVladimir Sementsov-Ogievskiy bs->bl.max_pdiscard = QEMU_ALIGN_DOWN(INT_MAX, min); 1952081dd1feSEric Blake bs->bl.max_pwrite_zeroes = max; 1953081dd1feSEric Blake bs->bl.max_transfer = max; 1954081dd1feSEric Blake 1955081dd1feSEric Blake if (s->info.opt_block && 1956081dd1feSEric Blake s->info.opt_block > bs->bl.opt_transfer) { 1957081dd1feSEric Blake bs->bl.opt_transfer = s->info.opt_block; 1958081dd1feSEric Blake } 1959fa21e6faSDenis V. Lunev } 1960fa21e6faSDenis V. Lunev 1961019d6b8fSAnthony Liguori static void nbd_close(BlockDriverState *bs) 1962019d6b8fSAnthony Liguori { 1963f53a829bSMax Reitz nbd_client_close(bs); 1964bbba1c37SVladimir Sementsov-Ogievskiy nbd_clear_bdrvstate(bs); 1965019d6b8fSAnthony Liguori } 1966019d6b8fSAnthony Liguori 1967a2b333c0SNir Soffer /* 1968a2b333c0SNir Soffer * NBD cannot truncate, but if the caller asks to truncate to the same size, or 1969a2b333c0SNir Soffer * to a smaller size with exact=false, there is no reason to fail the 1970a2b333c0SNir Soffer * operation. 1971a2b333c0SNir Soffer * 1972a2b333c0SNir Soffer * Preallocation mode is ignored since it does not seems useful to fail when 1973a2b333c0SNir Soffer * we never change anything. 1974a2b333c0SNir Soffer */ 1975a2b333c0SNir Soffer static int coroutine_fn nbd_co_truncate(BlockDriverState *bs, int64_t offset, 1976a2b333c0SNir Soffer bool exact, PreallocMode prealloc, 1977a2b333c0SNir Soffer BdrvRequestFlags flags, Error **errp) 1978a2b333c0SNir Soffer { 1979a2b333c0SNir Soffer BDRVNBDState *s = bs->opaque; 1980a2b333c0SNir Soffer 1981a2b333c0SNir Soffer if (offset != s->info.size && exact) { 1982a2b333c0SNir Soffer error_setg(errp, "Cannot resize NBD nodes"); 1983a2b333c0SNir Soffer return -ENOTSUP; 1984a2b333c0SNir Soffer } 1985a2b333c0SNir Soffer 1986a2b333c0SNir Soffer if (offset > s->info.size) { 1987a2b333c0SNir Soffer error_setg(errp, "Cannot grow NBD nodes"); 1988a2b333c0SNir Soffer return -EINVAL; 1989a2b333c0SNir Soffer } 1990a2b333c0SNir Soffer 1991a2b333c0SNir Soffer return 0; 1992a2b333c0SNir Soffer } 1993a2b333c0SNir Soffer 1994c86422c5SEmanuele Giuseppe Esposito static int64_t coroutine_fn nbd_co_getlength(BlockDriverState *bs) 1995019d6b8fSAnthony Liguori { 1996019d6b8fSAnthony Liguori BDRVNBDState *s = bs->opaque; 1997019d6b8fSAnthony Liguori 1998611ae1d7SVladimir Sementsov-Ogievskiy return s->info.size; 1999019d6b8fSAnthony Liguori } 2000019d6b8fSAnthony Liguori 2001998b3a1eSMax Reitz static void nbd_refresh_filename(BlockDriverState *bs) 20022019d68bSMax Reitz { 200303504d05SMax Reitz BDRVNBDState *s = bs->opaque; 2004491d6c7cSMax Reitz const char *host = NULL, *port = NULL, *path = NULL; 20055c86bdf1SEric Blake size_t len = 0; 2006491d6c7cSMax Reitz 200762cf396bSMarkus Armbruster if (s->saddr->type == SOCKET_ADDRESS_TYPE_INET) { 20089445673eSMarkus Armbruster const InetSocketAddress *inet = &s->saddr->u.inet; 2009491d6c7cSMax Reitz if (!inet->has_ipv4 && !inet->has_ipv6 && !inet->has_to) { 2010491d6c7cSMax Reitz host = inet->host; 2011491d6c7cSMax Reitz port = inet->port; 2012491d6c7cSMax Reitz } 201362cf396bSMarkus Armbruster } else if (s->saddr->type == SOCKET_ADDRESS_TYPE_UNIX) { 20149445673eSMarkus Armbruster path = s->saddr->u.q_unix.path; 20159445673eSMarkus Armbruster } /* else can't represent as pseudo-filename */ 20162019d68bSMax Reitz 2017491d6c7cSMax Reitz if (path && s->export) { 20185c86bdf1SEric Blake len = snprintf(bs->exact_filename, sizeof(bs->exact_filename), 2019491d6c7cSMax Reitz "nbd+unix:///%s?socket=%s", s->export, path); 2020491d6c7cSMax Reitz } else if (path && !s->export) { 20215c86bdf1SEric Blake len = snprintf(bs->exact_filename, sizeof(bs->exact_filename), 2022491d6c7cSMax Reitz "nbd+unix://?socket=%s", path); 2023491d6c7cSMax Reitz } else if (host && s->export) { 20245c86bdf1SEric Blake len = snprintf(bs->exact_filename, sizeof(bs->exact_filename), 2025491d6c7cSMax Reitz "nbd://%s:%s/%s", host, port, s->export); 2026491d6c7cSMax Reitz } else if (host && !s->export) { 20275c86bdf1SEric Blake len = snprintf(bs->exact_filename, sizeof(bs->exact_filename), 2028491d6c7cSMax Reitz "nbd://%s:%s", host, port); 2029ec0de768SMax Reitz } 203000d69986SEric Blake if (len >= sizeof(bs->exact_filename)) { 20315c86bdf1SEric Blake /* Name is too long to represent exactly, so leave it empty. */ 20325c86bdf1SEric Blake bs->exact_filename[0] = '\0'; 20335c86bdf1SEric Blake } 20342019d68bSMax Reitz } 20352019d68bSMax Reitz 20368a6239c0SMax Reitz static char *nbd_dirname(BlockDriverState *bs, Error **errp) 20378a6239c0SMax Reitz { 20388a6239c0SMax Reitz /* The generic bdrv_dirname() implementation is able to work out some 20398a6239c0SMax Reitz * directory name for NBD nodes, but that would be wrong. So far there is no 20408a6239c0SMax Reitz * specification for how "export paths" would work, so NBD does not have 20418a6239c0SMax Reitz * directory names. */ 20428a6239c0SMax Reitz error_setg(errp, "Cannot generate a base directory for NBD nodes"); 20438a6239c0SMax Reitz return NULL; 20448a6239c0SMax Reitz } 20458a6239c0SMax Reitz 20462654267cSMax Reitz static const char *const nbd_strong_runtime_opts[] = { 20472654267cSMax Reitz "path", 20482654267cSMax Reitz "host", 20492654267cSMax Reitz "port", 20502654267cSMax Reitz "export", 20512654267cSMax Reitz "tls-creds", 2052a0cd6d29SDaniel P. Berrangé "tls-hostname", 20532654267cSMax Reitz "server.", 20542654267cSMax Reitz 20552654267cSMax Reitz NULL 20562654267cSMax Reitz }; 20572654267cSMax Reitz 2058c4f7f24eSVladimir Sementsov-Ogievskiy static void nbd_cancel_in_flight(BlockDriverState *bs) 2059c4f7f24eSVladimir Sementsov-Ogievskiy { 2060c4f7f24eSVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 2061c4f7f24eSVladimir Sementsov-Ogievskiy 2062c4f7f24eSVladimir Sementsov-Ogievskiy reconnect_delay_timer_del(s); 2063c4f7f24eSVladimir Sementsov-Ogievskiy 2064ee19d953SPaolo Bonzini qemu_mutex_lock(&s->requests_lock); 2065c4f7f24eSVladimir Sementsov-Ogievskiy if (s->state == NBD_CLIENT_CONNECTING_WAIT) { 2066c4f7f24eSVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_CONNECTING_NOWAIT; 2067c4f7f24eSVladimir Sementsov-Ogievskiy } 2068ee19d953SPaolo Bonzini qemu_mutex_unlock(&s->requests_lock); 20694ddb5d2fSVladimir Sementsov-Ogievskiy 20704ddb5d2fSVladimir Sementsov-Ogievskiy nbd_co_establish_connection_cancel(s->conn); 2071c4f7f24eSVladimir Sementsov-Ogievskiy } 2072c4f7f24eSVladimir Sementsov-Ogievskiy 2073e15f3a66SHanna Reitz static void nbd_attach_aio_context(BlockDriverState *bs, 2074e15f3a66SHanna Reitz AioContext *new_context) 2075e15f3a66SHanna Reitz { 2076e15f3a66SHanna Reitz BDRVNBDState *s = bs->opaque; 2077e15f3a66SHanna Reitz 2078e15f3a66SHanna Reitz /* The open_timer is used only during nbd_open() */ 2079e15f3a66SHanna Reitz assert(!s->open_timer); 2080e15f3a66SHanna Reitz 2081e15f3a66SHanna Reitz /* 2082e15f3a66SHanna Reitz * The reconnect_delay_timer is scheduled in I/O paths when the 2083e15f3a66SHanna Reitz * connection is lost, to cancel the reconnection attempt after a 2084e15f3a66SHanna Reitz * given time. Once this attempt is done (successfully or not), 2085e15f3a66SHanna Reitz * nbd_reconnect_attempt() ensures the timer is deleted before the 2086e15f3a66SHanna Reitz * respective I/O request is resumed. 2087e15f3a66SHanna Reitz * Since the AioContext can only be changed when a node is drained, 2088e15f3a66SHanna Reitz * the reconnect_delay_timer cannot be active here. 2089e15f3a66SHanna Reitz */ 2090e15f3a66SHanna Reitz assert(!s->reconnect_delay_timer); 2091e15f3a66SHanna Reitz } 2092e15f3a66SHanna Reitz 2093e15f3a66SHanna Reitz static void nbd_detach_aio_context(BlockDriverState *bs) 2094e15f3a66SHanna Reitz { 2095e15f3a66SHanna Reitz BDRVNBDState *s = bs->opaque; 2096e15f3a66SHanna Reitz 2097e15f3a66SHanna Reitz assert(!s->open_timer); 2098e15f3a66SHanna Reitz assert(!s->reconnect_delay_timer); 2099e15f3a66SHanna Reitz } 2100e15f3a66SHanna Reitz 2101019d6b8fSAnthony Liguori static BlockDriver bdrv_nbd = { 2102019d6b8fSAnthony Liguori .format_name = "nbd", 21031d7d2a9dSPaolo Bonzini .protocol_name = "nbd", 2104019d6b8fSAnthony Liguori .instance_size = sizeof(BDRVNBDState), 21056963a30dSKevin Wolf .bdrv_parse_filename = nbd_parse_filename, 21065a5e7f8cSMaxim Levitsky .bdrv_co_create_opts = bdrv_co_create_opts_simple, 21075a5e7f8cSMaxim Levitsky .create_opts = &bdrv_create_opts_simple, 210866f82ceeSKevin Wolf .bdrv_file_open = nbd_open, 2109e99754b4SMaxim Levitsky .bdrv_reopen_prepare = nbd_client_reopen_prepare, 211070c4fb26SEric Blake .bdrv_co_preadv = nbd_client_co_preadv, 211170c4fb26SEric Blake .bdrv_co_pwritev = nbd_client_co_pwritev, 2112fa778fffSEric Blake .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes, 2113019d6b8fSAnthony Liguori .bdrv_close = nbd_close, 21145d934513SKevin Wolf .bdrv_co_flush_to_os = nbd_client_co_flush, 2115447e57c3SEric Blake .bdrv_co_pdiscard = nbd_client_co_pdiscard, 2116fa21e6faSDenis V. Lunev .bdrv_refresh_limits = nbd_refresh_limits, 2117a2b333c0SNir Soffer .bdrv_co_truncate = nbd_co_truncate, 2118c86422c5SEmanuele Giuseppe Esposito .bdrv_co_getlength = nbd_co_getlength, 21192019d68bSMax Reitz .bdrv_refresh_filename = nbd_refresh_filename, 212078a33ab5SVladimir Sementsov-Ogievskiy .bdrv_co_block_status = nbd_client_co_block_status, 21218a6239c0SMax Reitz .bdrv_dirname = nbd_dirname, 21222654267cSMax Reitz .strong_runtime_opts = nbd_strong_runtime_opts, 2123c4f7f24eSVladimir Sementsov-Ogievskiy .bdrv_cancel_in_flight = nbd_cancel_in_flight, 2124e15f3a66SHanna Reitz 2125e15f3a66SHanna Reitz .bdrv_attach_aio_context = nbd_attach_aio_context, 2126e15f3a66SHanna Reitz .bdrv_detach_aio_context = nbd_detach_aio_context, 21271d7d2a9dSPaolo Bonzini }; 21281d7d2a9dSPaolo Bonzini 21291d7d2a9dSPaolo Bonzini static BlockDriver bdrv_nbd_tcp = { 21301d7d2a9dSPaolo Bonzini .format_name = "nbd", 21311d7d2a9dSPaolo Bonzini .protocol_name = "nbd+tcp", 21321d7d2a9dSPaolo Bonzini .instance_size = sizeof(BDRVNBDState), 21336963a30dSKevin Wolf .bdrv_parse_filename = nbd_parse_filename, 21345a5e7f8cSMaxim Levitsky .bdrv_co_create_opts = bdrv_co_create_opts_simple, 21355a5e7f8cSMaxim Levitsky .create_opts = &bdrv_create_opts_simple, 21361d7d2a9dSPaolo Bonzini .bdrv_file_open = nbd_open, 2137e99754b4SMaxim Levitsky .bdrv_reopen_prepare = nbd_client_reopen_prepare, 213870c4fb26SEric Blake .bdrv_co_preadv = nbd_client_co_preadv, 213970c4fb26SEric Blake .bdrv_co_pwritev = nbd_client_co_pwritev, 2140fa778fffSEric Blake .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes, 21411d7d2a9dSPaolo Bonzini .bdrv_close = nbd_close, 21425d934513SKevin Wolf .bdrv_co_flush_to_os = nbd_client_co_flush, 2143447e57c3SEric Blake .bdrv_co_pdiscard = nbd_client_co_pdiscard, 2144fa21e6faSDenis V. Lunev .bdrv_refresh_limits = nbd_refresh_limits, 2145a2b333c0SNir Soffer .bdrv_co_truncate = nbd_co_truncate, 2146c86422c5SEmanuele Giuseppe Esposito .bdrv_co_getlength = nbd_co_getlength, 21472019d68bSMax Reitz .bdrv_refresh_filename = nbd_refresh_filename, 214878a33ab5SVladimir Sementsov-Ogievskiy .bdrv_co_block_status = nbd_client_co_block_status, 21498a6239c0SMax Reitz .bdrv_dirname = nbd_dirname, 21502654267cSMax Reitz .strong_runtime_opts = nbd_strong_runtime_opts, 2151c4f7f24eSVladimir Sementsov-Ogievskiy .bdrv_cancel_in_flight = nbd_cancel_in_flight, 2152e15f3a66SHanna Reitz 2153e15f3a66SHanna Reitz .bdrv_attach_aio_context = nbd_attach_aio_context, 2154e15f3a66SHanna Reitz .bdrv_detach_aio_context = nbd_detach_aio_context, 21551d7d2a9dSPaolo Bonzini }; 21561d7d2a9dSPaolo Bonzini 21571d7d2a9dSPaolo Bonzini static BlockDriver bdrv_nbd_unix = { 21581d7d2a9dSPaolo Bonzini .format_name = "nbd", 21591d7d2a9dSPaolo Bonzini .protocol_name = "nbd+unix", 21601d7d2a9dSPaolo Bonzini .instance_size = sizeof(BDRVNBDState), 21616963a30dSKevin Wolf .bdrv_parse_filename = nbd_parse_filename, 21625a5e7f8cSMaxim Levitsky .bdrv_co_create_opts = bdrv_co_create_opts_simple, 21635a5e7f8cSMaxim Levitsky .create_opts = &bdrv_create_opts_simple, 21641d7d2a9dSPaolo Bonzini .bdrv_file_open = nbd_open, 2165e99754b4SMaxim Levitsky .bdrv_reopen_prepare = nbd_client_reopen_prepare, 216670c4fb26SEric Blake .bdrv_co_preadv = nbd_client_co_preadv, 216770c4fb26SEric Blake .bdrv_co_pwritev = nbd_client_co_pwritev, 2168fa778fffSEric Blake .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes, 21691d7d2a9dSPaolo Bonzini .bdrv_close = nbd_close, 21705d934513SKevin Wolf .bdrv_co_flush_to_os = nbd_client_co_flush, 2171447e57c3SEric Blake .bdrv_co_pdiscard = nbd_client_co_pdiscard, 2172fa21e6faSDenis V. Lunev .bdrv_refresh_limits = nbd_refresh_limits, 2173a2b333c0SNir Soffer .bdrv_co_truncate = nbd_co_truncate, 2174c86422c5SEmanuele Giuseppe Esposito .bdrv_co_getlength = nbd_co_getlength, 21752019d68bSMax Reitz .bdrv_refresh_filename = nbd_refresh_filename, 217678a33ab5SVladimir Sementsov-Ogievskiy .bdrv_co_block_status = nbd_client_co_block_status, 21778a6239c0SMax Reitz .bdrv_dirname = nbd_dirname, 21782654267cSMax Reitz .strong_runtime_opts = nbd_strong_runtime_opts, 2179c4f7f24eSVladimir Sementsov-Ogievskiy .bdrv_cancel_in_flight = nbd_cancel_in_flight, 2180e15f3a66SHanna Reitz 2181e15f3a66SHanna Reitz .bdrv_attach_aio_context = nbd_attach_aio_context, 2182e15f3a66SHanna Reitz .bdrv_detach_aio_context = nbd_detach_aio_context, 2183019d6b8fSAnthony Liguori }; 2184019d6b8fSAnthony Liguori 2185019d6b8fSAnthony Liguori static void bdrv_nbd_init(void) 2186019d6b8fSAnthony Liguori { 2187019d6b8fSAnthony Liguori bdrv_register(&bdrv_nbd); 21881d7d2a9dSPaolo Bonzini bdrv_register(&bdrv_nbd_tcp); 21891d7d2a9dSPaolo Bonzini bdrv_register(&bdrv_nbd_unix); 2190019d6b8fSAnthony Liguori } 2191019d6b8fSAnthony Liguori 2192019d6b8fSAnthony Liguori block_init(bdrv_nbd_init); 2193