1019d6b8fSAnthony Liguori /* 2019d6b8fSAnthony Liguori * QEMU Block driver for NBD 3019d6b8fSAnthony Liguori * 4f7651539SVladimir Sementsov-Ogievskiy * Copyright (c) 2019 Virtuozzo International GmbH. 586f8cdf3SVladimir Sementsov-Ogievskiy * Copyright (C) 2016 Red Hat, Inc. 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" 38fee091cdSLukas Straub #include "qemu/atomic.h" 39019d6b8fSAnthony Liguori 4086f8cdf3SVladimir Sementsov-Ogievskiy #include "qapi/qapi-visit-sockets.h" 4186f8cdf3SVladimir Sementsov-Ogievskiy #include "qapi/qmp/qstring.h" 421dc4718dSVladimir Sementsov-Ogievskiy #include "qapi/clone-visitor.h" 4386f8cdf3SVladimir Sementsov-Ogievskiy 4486f8cdf3SVladimir Sementsov-Ogievskiy #include "block/qdict.h" 4586f8cdf3SVladimir Sementsov-Ogievskiy #include "block/nbd.h" 4686f8cdf3SVladimir Sementsov-Ogievskiy #include "block/block_int.h" 47a71d597bSVladimir Sementsov-Ogievskiy #include "block/coroutines.h" 4886f8cdf3SVladimir Sementsov-Ogievskiy 49fee091cdSLukas Straub #include "qemu/yank.h" 50fee091cdSLukas Straub 511d45f8b5SLaurent Vivier #define EN_OPTSTR ":exportname=" 5286f8cdf3SVladimir Sementsov-Ogievskiy #define MAX_NBD_REQUESTS 16 5386f8cdf3SVladimir Sementsov-Ogievskiy 5486f8cdf3SVladimir Sementsov-Ogievskiy #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ (uint64_t)(intptr_t)(bs)) 5586f8cdf3SVladimir Sementsov-Ogievskiy #define INDEX_TO_HANDLE(bs, index) ((index) ^ (uint64_t)(intptr_t)(bs)) 5686f8cdf3SVladimir Sementsov-Ogievskiy 5786f8cdf3SVladimir Sementsov-Ogievskiy typedef struct { 5886f8cdf3SVladimir Sementsov-Ogievskiy Coroutine *coroutine; 5986f8cdf3SVladimir Sementsov-Ogievskiy uint64_t offset; /* original offset of the request */ 604ddb5d2fSVladimir Sementsov-Ogievskiy bool receiving; /* sleeping in the yield in nbd_receive_replies */ 6186f8cdf3SVladimir Sementsov-Ogievskiy } NBDClientRequest; 6286f8cdf3SVladimir Sementsov-Ogievskiy 63a34b1e5eSVladimir Sementsov-Ogievskiy typedef enum NBDClientState { 64f7651539SVladimir Sementsov-Ogievskiy NBD_CLIENT_CONNECTING_WAIT, 65f7651539SVladimir Sementsov-Ogievskiy NBD_CLIENT_CONNECTING_NOWAIT, 66a34b1e5eSVladimir Sementsov-Ogievskiy NBD_CLIENT_CONNECTED, 67a34b1e5eSVladimir Sementsov-Ogievskiy NBD_CLIENT_QUIT 68a34b1e5eSVladimir Sementsov-Ogievskiy } NBDClientState; 69a34b1e5eSVladimir Sementsov-Ogievskiy 70611ae1d7SVladimir Sementsov-Ogievskiy typedef struct BDRVNBDState { 7195a078eaSVladimir Sementsov-Ogievskiy QIOChannel *ioc; /* The current I/O channel */ 7286f8cdf3SVladimir Sementsov-Ogievskiy NBDExportInfo info; 7386f8cdf3SVladimir Sementsov-Ogievskiy 7486f8cdf3SVladimir Sementsov-Ogievskiy CoMutex send_mutex; 7586f8cdf3SVladimir Sementsov-Ogievskiy CoQueue free_sema; 764ddb5d2fSVladimir Sementsov-Ogievskiy 774ddb5d2fSVladimir Sementsov-Ogievskiy CoMutex receive_mutex; 7886f8cdf3SVladimir Sementsov-Ogievskiy int in_flight; 79a34b1e5eSVladimir Sementsov-Ogievskiy NBDClientState state; 8086f8cdf3SVladimir Sementsov-Ogievskiy 8146f56631SVladimir Sementsov-Ogievskiy QEMUTimer *reconnect_delay_timer; 82be16b8bfSVladimir Sementsov-Ogievskiy QEMUTimer *open_timer; 8346f56631SVladimir Sementsov-Ogievskiy 8486f8cdf3SVladimir Sementsov-Ogievskiy NBDClientRequest requests[MAX_NBD_REQUESTS]; 8586f8cdf3SVladimir Sementsov-Ogievskiy NBDReply reply; 8686f8cdf3SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 8703504d05SMax Reitz 888f071c9dSVladimir Sementsov-Ogievskiy /* Connection parameters */ 898f071c9dSVladimir Sementsov-Ogievskiy uint32_t reconnect_delay; 90be16b8bfSVladimir Sementsov-Ogievskiy uint32_t open_timeout; 9162cf396bSMarkus Armbruster SocketAddress *saddr; 92a0cd6d29SDaniel P. Berrangé char *export; 93a0cd6d29SDaniel P. Berrangé char *tlscredsid; 948f071c9dSVladimir Sementsov-Ogievskiy QCryptoTLSCreds *tlscreds; 95a0cd6d29SDaniel P. Berrangé char *tlshostname; 968f071c9dSVladimir Sementsov-Ogievskiy char *x_dirty_bitmap; 97dbc7b014SEric Blake bool alloc_depth; 981dc4718dSVladimir Sementsov-Ogievskiy 9990ddc64fSVladimir Sementsov-Ogievskiy NBDClientConnection *conn; 100019d6b8fSAnthony Liguori } BDRVNBDState; 101019d6b8fSAnthony Liguori 102fee091cdSLukas Straub static void nbd_yank(void *opaque); 103f7651539SVladimir Sementsov-Ogievskiy 104bbba1c37SVladimir Sementsov-Ogievskiy static void nbd_clear_bdrvstate(BlockDriverState *bs) 1057f493662SPan Nengyuan { 106bbba1c37SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 107e8b35bf5SRoman Kagan 108248d4701SVladimir Sementsov-Ogievskiy nbd_client_connection_release(s->conn); 109248d4701SVladimir Sementsov-Ogievskiy s->conn = NULL; 110bbba1c37SVladimir Sementsov-Ogievskiy 111bbba1c37SVladimir Sementsov-Ogievskiy yank_unregister_instance(BLOCKDEV_YANK_INSTANCE(bs->node_name)); 112bbba1c37SVladimir Sementsov-Ogievskiy 1138a39c381SHanna Reitz /* Must not leave timers behind that would access freed data */ 1148a39c381SHanna Reitz assert(!s->reconnect_delay_timer); 1158a39c381SHanna Reitz assert(!s->open_timer); 1168a39c381SHanna Reitz 1177f493662SPan Nengyuan object_unref(OBJECT(s->tlscreds)); 1187f493662SPan Nengyuan qapi_free_SocketAddress(s->saddr); 1197f493662SPan Nengyuan s->saddr = NULL; 1207f493662SPan Nengyuan g_free(s->export); 1217f493662SPan Nengyuan s->export = NULL; 1227f493662SPan Nengyuan g_free(s->tlscredsid); 1237f493662SPan Nengyuan s->tlscredsid = NULL; 124a0cd6d29SDaniel P. Berrangé g_free(s->tlshostname); 125a0cd6d29SDaniel P. Berrangé s->tlshostname = NULL; 1267f493662SPan Nengyuan g_free(s->x_dirty_bitmap); 1277f493662SPan Nengyuan s->x_dirty_bitmap = NULL; 1287f493662SPan Nengyuan } 1297f493662SPan Nengyuan 13091e0998fSVladimir Sementsov-Ogievskiy static bool nbd_client_connected(BDRVNBDState *s) 13191e0998fSVladimir Sementsov-Ogievskiy { 13291e0998fSVladimir Sementsov-Ogievskiy return qatomic_load_acquire(&s->state) == NBD_CLIENT_CONNECTED; 13391e0998fSVladimir Sementsov-Ogievskiy } 13491e0998fSVladimir Sementsov-Ogievskiy 1350c43c6fcSPaolo Bonzini static bool coroutine_fn nbd_recv_coroutine_wake_one(NBDClientRequest *req) 13604a953b2SVladimir Sementsov-Ogievskiy { 13704a953b2SVladimir Sementsov-Ogievskiy if (req->receiving) { 13804a953b2SVladimir Sementsov-Ogievskiy req->receiving = false; 13904a953b2SVladimir Sementsov-Ogievskiy aio_co_wake(req->coroutine); 14004a953b2SVladimir Sementsov-Ogievskiy return true; 14104a953b2SVladimir Sementsov-Ogievskiy } 14204a953b2SVladimir Sementsov-Ogievskiy 14304a953b2SVladimir Sementsov-Ogievskiy return false; 14404a953b2SVladimir Sementsov-Ogievskiy } 14504a953b2SVladimir Sementsov-Ogievskiy 1460c43c6fcSPaolo Bonzini static void coroutine_fn nbd_recv_coroutines_wake(BDRVNBDState *s, bool all) 1473bc0bd1fSVladimir Sementsov-Ogievskiy { 1483bc0bd1fSVladimir Sementsov-Ogievskiy int i; 1493bc0bd1fSVladimir Sementsov-Ogievskiy 1503bc0bd1fSVladimir Sementsov-Ogievskiy for (i = 0; i < MAX_NBD_REQUESTS; i++) { 15104a953b2SVladimir Sementsov-Ogievskiy if (nbd_recv_coroutine_wake_one(&s->requests[i]) && !all) { 15204a953b2SVladimir Sementsov-Ogievskiy return; 1533bc0bd1fSVladimir Sementsov-Ogievskiy } 1543bc0bd1fSVladimir Sementsov-Ogievskiy } 1553bc0bd1fSVladimir Sementsov-Ogievskiy } 1563bc0bd1fSVladimir Sementsov-Ogievskiy 1570c43c6fcSPaolo Bonzini static void coroutine_fn nbd_channel_error(BDRVNBDState *s, int ret) 158a34b1e5eSVladimir Sementsov-Ogievskiy { 159cb116da7SVladimir Sementsov-Ogievskiy if (nbd_client_connected(s)) { 160cb116da7SVladimir Sementsov-Ogievskiy qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); 161cb116da7SVladimir Sementsov-Ogievskiy } 162cb116da7SVladimir Sementsov-Ogievskiy 163f7651539SVladimir Sementsov-Ogievskiy if (ret == -EIO) { 16491e0998fSVladimir Sementsov-Ogievskiy if (nbd_client_connected(s)) { 165f7651539SVladimir Sementsov-Ogievskiy s->state = s->reconnect_delay ? NBD_CLIENT_CONNECTING_WAIT : 166f7651539SVladimir Sementsov-Ogievskiy NBD_CLIENT_CONNECTING_NOWAIT; 167f7651539SVladimir Sementsov-Ogievskiy } 168f7651539SVladimir Sementsov-Ogievskiy } else { 169a34b1e5eSVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_QUIT; 170a34b1e5eSVladimir Sementsov-Ogievskiy } 1714ddb5d2fSVladimir Sementsov-Ogievskiy 1724ddb5d2fSVladimir Sementsov-Ogievskiy nbd_recv_coroutines_wake(s, true); 173f7651539SVladimir Sementsov-Ogievskiy } 174a34b1e5eSVladimir Sementsov-Ogievskiy 17546f56631SVladimir Sementsov-Ogievskiy static void reconnect_delay_timer_del(BDRVNBDState *s) 17646f56631SVladimir Sementsov-Ogievskiy { 17746f56631SVladimir Sementsov-Ogievskiy if (s->reconnect_delay_timer) { 17846f56631SVladimir Sementsov-Ogievskiy timer_free(s->reconnect_delay_timer); 17946f56631SVladimir Sementsov-Ogievskiy s->reconnect_delay_timer = NULL; 18046f56631SVladimir Sementsov-Ogievskiy } 18146f56631SVladimir Sementsov-Ogievskiy } 18246f56631SVladimir Sementsov-Ogievskiy 18346f56631SVladimir Sementsov-Ogievskiy static void reconnect_delay_timer_cb(void *opaque) 18446f56631SVladimir Sementsov-Ogievskiy { 18546f56631SVladimir Sementsov-Ogievskiy BDRVNBDState *s = opaque; 18646f56631SVladimir Sementsov-Ogievskiy 187fee091cdSLukas Straub if (qatomic_load_acquire(&s->state) == NBD_CLIENT_CONNECTING_WAIT) { 18846f56631SVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_CONNECTING_NOWAIT; 1894ddb5d2fSVladimir Sementsov-Ogievskiy nbd_co_establish_connection_cancel(s->conn); 19046f56631SVladimir Sementsov-Ogievskiy while (qemu_co_enter_next(&s->free_sema, NULL)) { 19146f56631SVladimir Sementsov-Ogievskiy /* Resume all queued requests */ 19246f56631SVladimir Sementsov-Ogievskiy } 19346f56631SVladimir Sementsov-Ogievskiy } 19446f56631SVladimir Sementsov-Ogievskiy 19546f56631SVladimir Sementsov-Ogievskiy reconnect_delay_timer_del(s); 19646f56631SVladimir Sementsov-Ogievskiy } 19746f56631SVladimir Sementsov-Ogievskiy 19846f56631SVladimir Sementsov-Ogievskiy static void reconnect_delay_timer_init(BDRVNBDState *s, uint64_t expire_time_ns) 19946f56631SVladimir Sementsov-Ogievskiy { 200fee091cdSLukas Straub if (qatomic_load_acquire(&s->state) != NBD_CLIENT_CONNECTING_WAIT) { 20146f56631SVladimir Sementsov-Ogievskiy return; 20246f56631SVladimir Sementsov-Ogievskiy } 20346f56631SVladimir Sementsov-Ogievskiy 20446f56631SVladimir Sementsov-Ogievskiy assert(!s->reconnect_delay_timer); 20546f56631SVladimir Sementsov-Ogievskiy s->reconnect_delay_timer = aio_timer_new(bdrv_get_aio_context(s->bs), 20646f56631SVladimir Sementsov-Ogievskiy QEMU_CLOCK_REALTIME, 20746f56631SVladimir Sementsov-Ogievskiy SCALE_NS, 20846f56631SVladimir Sementsov-Ogievskiy reconnect_delay_timer_cb, s); 20946f56631SVladimir Sementsov-Ogievskiy timer_mod(s->reconnect_delay_timer, expire_time_ns); 21046f56631SVladimir Sementsov-Ogievskiy } 21146f56631SVladimir Sementsov-Ogievskiy 21286f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_teardown_connection(BlockDriverState *bs) 21386f8cdf3SVladimir Sementsov-Ogievskiy { 214611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 21586f8cdf3SVladimir Sementsov-Ogievskiy 2164ddb5d2fSVladimir Sementsov-Ogievskiy assert(!s->in_flight); 2174ddb5d2fSVladimir Sementsov-Ogievskiy 218fbeb3e63SVladimir Sementsov-Ogievskiy if (s->ioc) { 219f7651539SVladimir Sementsov-Ogievskiy qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); 2204ddb5d2fSVladimir Sementsov-Ogievskiy yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), 2214ddb5d2fSVladimir Sementsov-Ogievskiy nbd_yank, s->bs); 2224ddb5d2fSVladimir Sementsov-Ogievskiy object_unref(OBJECT(s->ioc)); 2234ddb5d2fSVladimir Sementsov-Ogievskiy s->ioc = NULL; 224f7651539SVladimir Sementsov-Ogievskiy } 225fbeb3e63SVladimir Sementsov-Ogievskiy 226f7651539SVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_QUIT; 22778c81a3fSMax Reitz } 22886f8cdf3SVladimir Sementsov-Ogievskiy 229be16b8bfSVladimir Sementsov-Ogievskiy static void open_timer_del(BDRVNBDState *s) 230be16b8bfSVladimir Sementsov-Ogievskiy { 231be16b8bfSVladimir Sementsov-Ogievskiy if (s->open_timer) { 232be16b8bfSVladimir Sementsov-Ogievskiy timer_free(s->open_timer); 233be16b8bfSVladimir Sementsov-Ogievskiy s->open_timer = NULL; 234be16b8bfSVladimir Sementsov-Ogievskiy } 235be16b8bfSVladimir Sementsov-Ogievskiy } 236be16b8bfSVladimir Sementsov-Ogievskiy 237be16b8bfSVladimir Sementsov-Ogievskiy static void open_timer_cb(void *opaque) 238be16b8bfSVladimir Sementsov-Ogievskiy { 239be16b8bfSVladimir Sementsov-Ogievskiy BDRVNBDState *s = opaque; 240be16b8bfSVladimir Sementsov-Ogievskiy 241be16b8bfSVladimir Sementsov-Ogievskiy nbd_co_establish_connection_cancel(s->conn); 242be16b8bfSVladimir Sementsov-Ogievskiy open_timer_del(s); 243be16b8bfSVladimir Sementsov-Ogievskiy } 244be16b8bfSVladimir Sementsov-Ogievskiy 245be16b8bfSVladimir Sementsov-Ogievskiy static void open_timer_init(BDRVNBDState *s, uint64_t expire_time_ns) 246be16b8bfSVladimir Sementsov-Ogievskiy { 247be16b8bfSVladimir Sementsov-Ogievskiy assert(!s->open_timer); 248be16b8bfSVladimir Sementsov-Ogievskiy s->open_timer = aio_timer_new(bdrv_get_aio_context(s->bs), 249be16b8bfSVladimir Sementsov-Ogievskiy QEMU_CLOCK_REALTIME, 250be16b8bfSVladimir Sementsov-Ogievskiy SCALE_NS, 251be16b8bfSVladimir Sementsov-Ogievskiy open_timer_cb, s); 252be16b8bfSVladimir Sementsov-Ogievskiy timer_mod(s->open_timer, expire_time_ns); 253be16b8bfSVladimir Sementsov-Ogievskiy } 254be16b8bfSVladimir Sementsov-Ogievskiy 255f7651539SVladimir Sementsov-Ogievskiy static bool nbd_client_connecting(BDRVNBDState *s) 256f7651539SVladimir Sementsov-Ogievskiy { 257fee091cdSLukas Straub NBDClientState state = qatomic_load_acquire(&s->state); 258fee091cdSLukas Straub return state == NBD_CLIENT_CONNECTING_WAIT || 259fee091cdSLukas Straub state == NBD_CLIENT_CONNECTING_NOWAIT; 260f7651539SVladimir Sementsov-Ogievskiy } 261f7651539SVladimir Sementsov-Ogievskiy 262f7651539SVladimir Sementsov-Ogievskiy static bool nbd_client_connecting_wait(BDRVNBDState *s) 263f7651539SVladimir Sementsov-Ogievskiy { 264fee091cdSLukas Straub return qatomic_load_acquire(&s->state) == NBD_CLIENT_CONNECTING_WAIT; 265f7651539SVladimir Sementsov-Ogievskiy } 266f7651539SVladimir Sementsov-Ogievskiy 267e9ba7788SVladimir Sementsov-Ogievskiy /* 268e9ba7788SVladimir Sementsov-Ogievskiy * Update @bs with information learned during a completed negotiation process. 269e9ba7788SVladimir Sementsov-Ogievskiy * Return failure if the server's advertised options are incompatible with the 270e9ba7788SVladimir Sementsov-Ogievskiy * client's needs. 271e9ba7788SVladimir Sementsov-Ogievskiy */ 272e9ba7788SVladimir Sementsov-Ogievskiy static int nbd_handle_updated_info(BlockDriverState *bs, Error **errp) 273e9ba7788SVladimir Sementsov-Ogievskiy { 274e9ba7788SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 275e9ba7788SVladimir Sementsov-Ogievskiy int ret; 276e9ba7788SVladimir Sementsov-Ogievskiy 277e9ba7788SVladimir Sementsov-Ogievskiy if (s->x_dirty_bitmap) { 278e9ba7788SVladimir Sementsov-Ogievskiy if (!s->info.base_allocation) { 279e9ba7788SVladimir Sementsov-Ogievskiy error_setg(errp, "requested x-dirty-bitmap %s not found", 280e9ba7788SVladimir Sementsov-Ogievskiy s->x_dirty_bitmap); 281e9ba7788SVladimir Sementsov-Ogievskiy return -EINVAL; 282e9ba7788SVladimir Sementsov-Ogievskiy } 283e9ba7788SVladimir Sementsov-Ogievskiy if (strcmp(s->x_dirty_bitmap, "qemu:allocation-depth") == 0) { 284e9ba7788SVladimir Sementsov-Ogievskiy s->alloc_depth = true; 285e9ba7788SVladimir Sementsov-Ogievskiy } 286e9ba7788SVladimir Sementsov-Ogievskiy } 287e9ba7788SVladimir Sementsov-Ogievskiy 288e9ba7788SVladimir Sementsov-Ogievskiy if (s->info.flags & NBD_FLAG_READ_ONLY) { 289e9ba7788SVladimir Sementsov-Ogievskiy ret = bdrv_apply_auto_read_only(bs, "NBD export is read-only", errp); 290e9ba7788SVladimir Sementsov-Ogievskiy if (ret < 0) { 291e9ba7788SVladimir Sementsov-Ogievskiy return ret; 292e9ba7788SVladimir Sementsov-Ogievskiy } 293e9ba7788SVladimir Sementsov-Ogievskiy } 294e9ba7788SVladimir Sementsov-Ogievskiy 295e9ba7788SVladimir Sementsov-Ogievskiy if (s->info.flags & NBD_FLAG_SEND_FUA) { 296e9ba7788SVladimir Sementsov-Ogievskiy bs->supported_write_flags = BDRV_REQ_FUA; 297e9ba7788SVladimir Sementsov-Ogievskiy bs->supported_zero_flags |= BDRV_REQ_FUA; 298e9ba7788SVladimir Sementsov-Ogievskiy } 299e9ba7788SVladimir Sementsov-Ogievskiy 300e9ba7788SVladimir Sementsov-Ogievskiy if (s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES) { 301e9ba7788SVladimir Sementsov-Ogievskiy bs->supported_zero_flags |= BDRV_REQ_MAY_UNMAP; 302e9ba7788SVladimir Sementsov-Ogievskiy if (s->info.flags & NBD_FLAG_SEND_FAST_ZERO) { 303e9ba7788SVladimir Sementsov-Ogievskiy bs->supported_zero_flags |= BDRV_REQ_NO_FALLBACK; 304e9ba7788SVladimir Sementsov-Ogievskiy } 305e9ba7788SVladimir Sementsov-Ogievskiy } 306e9ba7788SVladimir Sementsov-Ogievskiy 307e9ba7788SVladimir Sementsov-Ogievskiy trace_nbd_client_handshake_success(s->export); 308e9ba7788SVladimir Sementsov-Ogievskiy 309e9ba7788SVladimir Sementsov-Ogievskiy return 0; 310e9ba7788SVladimir Sementsov-Ogievskiy } 311e9ba7788SVladimir Sementsov-Ogievskiy 312a71d597bSVladimir Sementsov-Ogievskiy int coroutine_fn nbd_co_do_establish_connection(BlockDriverState *bs, 31351edbf53SVladimir Sementsov-Ogievskiy Error **errp) 31451edbf53SVladimir Sementsov-Ogievskiy { 31551edbf53SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 31651edbf53SVladimir Sementsov-Ogievskiy int ret; 3174ddb5d2fSVladimir Sementsov-Ogievskiy bool blocking = nbd_client_connecting_wait(s); 3181581a70dSEmanuele Giuseppe Esposito IO_CODE(); 31951edbf53SVladimir Sementsov-Ogievskiy 32051edbf53SVladimir Sementsov-Ogievskiy assert(!s->ioc); 32151edbf53SVladimir Sementsov-Ogievskiy 3224ddb5d2fSVladimir Sementsov-Ogievskiy s->ioc = nbd_co_establish_connection(s->conn, &s->info, blocking, errp); 32351edbf53SVladimir Sementsov-Ogievskiy if (!s->ioc) { 32451edbf53SVladimir Sementsov-Ogievskiy return -ECONNREFUSED; 32551edbf53SVladimir Sementsov-Ogievskiy } 32651edbf53SVladimir Sementsov-Ogievskiy 3270b9cd6b9SLukas Straub yank_register_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), nbd_yank, 3280b9cd6b9SLukas Straub bs); 3290b9cd6b9SLukas Straub 33051edbf53SVladimir Sementsov-Ogievskiy ret = nbd_handle_updated_info(s->bs, NULL); 33151edbf53SVladimir Sementsov-Ogievskiy if (ret < 0) { 33251edbf53SVladimir Sementsov-Ogievskiy /* 33351edbf53SVladimir Sementsov-Ogievskiy * We have connected, but must fail for other reasons. 33451edbf53SVladimir Sementsov-Ogievskiy * Send NBD_CMD_DISC as a courtesy to the server. 33551edbf53SVladimir Sementsov-Ogievskiy */ 33651edbf53SVladimir Sementsov-Ogievskiy NBDRequest request = { .type = NBD_CMD_DISC }; 33751edbf53SVladimir Sementsov-Ogievskiy 33851edbf53SVladimir Sementsov-Ogievskiy nbd_send_request(s->ioc, &request); 33951edbf53SVladimir Sementsov-Ogievskiy 3400b9cd6b9SLukas Straub yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), 3410b9cd6b9SLukas Straub nbd_yank, bs); 34251edbf53SVladimir Sementsov-Ogievskiy object_unref(OBJECT(s->ioc)); 34351edbf53SVladimir Sementsov-Ogievskiy s->ioc = NULL; 34451edbf53SVladimir Sementsov-Ogievskiy 34551edbf53SVladimir Sementsov-Ogievskiy return ret; 34651edbf53SVladimir Sementsov-Ogievskiy } 34751edbf53SVladimir Sementsov-Ogievskiy 34851edbf53SVladimir Sementsov-Ogievskiy qio_channel_set_blocking(s->ioc, false, NULL); 34951edbf53SVladimir Sementsov-Ogievskiy qio_channel_attach_aio_context(s->ioc, bdrv_get_aio_context(bs)); 35051edbf53SVladimir Sementsov-Ogievskiy 35151edbf53SVladimir Sementsov-Ogievskiy /* successfully connected */ 35251edbf53SVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_CONNECTED; 35351edbf53SVladimir Sementsov-Ogievskiy qemu_co_queue_restart_all(&s->free_sema); 35451edbf53SVladimir Sementsov-Ogievskiy 35551edbf53SVladimir Sementsov-Ogievskiy return 0; 35651edbf53SVladimir Sementsov-Ogievskiy } 35751edbf53SVladimir Sementsov-Ogievskiy 3584ddb5d2fSVladimir Sementsov-Ogievskiy /* called under s->send_mutex */ 359f7651539SVladimir Sementsov-Ogievskiy static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s) 360f7651539SVladimir Sementsov-Ogievskiy { 3614ddb5d2fSVladimir Sementsov-Ogievskiy assert(nbd_client_connecting(s)); 3624ddb5d2fSVladimir Sementsov-Ogievskiy assert(s->in_flight == 0); 363f7651539SVladimir Sementsov-Ogievskiy 3644ddb5d2fSVladimir Sementsov-Ogievskiy if (nbd_client_connecting_wait(s) && s->reconnect_delay && 3654ddb5d2fSVladimir Sementsov-Ogievskiy !s->reconnect_delay_timer) 3664ddb5d2fSVladimir Sementsov-Ogievskiy { 3674ddb5d2fSVladimir Sementsov-Ogievskiy /* 3684ddb5d2fSVladimir Sementsov-Ogievskiy * It's first reconnect attempt after switching to 3694ddb5d2fSVladimir Sementsov-Ogievskiy * NBD_CLIENT_CONNECTING_WAIT 3704ddb5d2fSVladimir Sementsov-Ogievskiy */ 3714ddb5d2fSVladimir Sementsov-Ogievskiy reconnect_delay_timer_init(s, 3724ddb5d2fSVladimir Sementsov-Ogievskiy qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 3734ddb5d2fSVladimir Sementsov-Ogievskiy s->reconnect_delay * NANOSECONDS_PER_SECOND); 374f7651539SVladimir Sementsov-Ogievskiy } 375f7651539SVladimir Sementsov-Ogievskiy 376f7651539SVladimir Sementsov-Ogievskiy /* 377f7651539SVladimir Sementsov-Ogievskiy * Now we are sure that nobody is accessing the channel, and no one will 378f7651539SVladimir Sementsov-Ogievskiy * try until we set the state to CONNECTED. 379f7651539SVladimir Sementsov-Ogievskiy */ 380f7651539SVladimir Sementsov-Ogievskiy 381f7651539SVladimir Sementsov-Ogievskiy /* Finalize previous connection if any */ 382f7651539SVladimir Sementsov-Ogievskiy if (s->ioc) { 3838a509afdSVladimir Sementsov-Ogievskiy qio_channel_detach_aio_context(QIO_CHANNEL(s->ioc)); 384fee091cdSLukas Straub yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), 385fee091cdSLukas Straub nbd_yank, s->bs); 386611ae1d7SVladimir Sementsov-Ogievskiy object_unref(OBJECT(s->ioc)); 387611ae1d7SVladimir Sementsov-Ogievskiy s->ioc = NULL; 38886f8cdf3SVladimir Sementsov-Ogievskiy } 38986f8cdf3SVladimir Sementsov-Ogievskiy 39051edbf53SVladimir Sementsov-Ogievskiy nbd_co_do_establish_connection(s->bs, NULL); 3913ce1fc16SHanna Reitz 3923ce1fc16SHanna Reitz /* 3933ce1fc16SHanna Reitz * The reconnect attempt is done (maybe successfully, maybe not), so 3943ce1fc16SHanna Reitz * we no longer need this timer. Delete it so it will not outlive 3953ce1fc16SHanna Reitz * this I/O request (so draining removes all timers). 3963ce1fc16SHanna Reitz */ 3973ce1fc16SHanna Reitz reconnect_delay_timer_del(s); 398fa35591bSVladimir Sementsov-Ogievskiy } 399f7651539SVladimir Sementsov-Ogievskiy 4004ddb5d2fSVladimir Sementsov-Ogievskiy static coroutine_fn int nbd_receive_replies(BDRVNBDState *s, uint64_t handle) 401f7651539SVladimir Sementsov-Ogievskiy { 4024ddb5d2fSVladimir Sementsov-Ogievskiy int ret; 4034ddb5d2fSVladimir Sementsov-Ogievskiy uint64_t ind = HANDLE_TO_INDEX(s, handle), ind2; 4044ddb5d2fSVladimir Sementsov-Ogievskiy QEMU_LOCK_GUARD(&s->receive_mutex); 405f7651539SVladimir Sementsov-Ogievskiy 4064ddb5d2fSVladimir Sementsov-Ogievskiy while (true) { 4074ddb5d2fSVladimir Sementsov-Ogievskiy if (s->reply.handle == handle) { 4084ddb5d2fSVladimir Sementsov-Ogievskiy /* We are done */ 4094ddb5d2fSVladimir Sementsov-Ogievskiy return 0; 410f7651539SVladimir Sementsov-Ogievskiy } 411f7651539SVladimir Sementsov-Ogievskiy 4124ddb5d2fSVladimir Sementsov-Ogievskiy if (s->reply.handle != 0) { 41386f8cdf3SVladimir Sementsov-Ogievskiy /* 4144ddb5d2fSVladimir Sementsov-Ogievskiy * Some other request is being handled now. It should already be 4154ddb5d2fSVladimir Sementsov-Ogievskiy * woken by whoever set s->reply.handle (or never wait in this 4164ddb5d2fSVladimir Sementsov-Ogievskiy * yield). So, we should not wake it here. 41786f8cdf3SVladimir Sementsov-Ogievskiy */ 4184ddb5d2fSVladimir Sementsov-Ogievskiy ind2 = HANDLE_TO_INDEX(s, s->reply.handle); 4194ddb5d2fSVladimir Sementsov-Ogievskiy assert(!s->requests[ind2].receiving); 42086f8cdf3SVladimir Sementsov-Ogievskiy 4214ddb5d2fSVladimir Sementsov-Ogievskiy s->requests[ind].receiving = true; 4224ddb5d2fSVladimir Sementsov-Ogievskiy qemu_co_mutex_unlock(&s->receive_mutex); 4234ddb5d2fSVladimir Sementsov-Ogievskiy 42486f8cdf3SVladimir Sementsov-Ogievskiy qemu_coroutine_yield(); 4254ddb5d2fSVladimir Sementsov-Ogievskiy /* 4264ddb5d2fSVladimir Sementsov-Ogievskiy * We may be woken for 3 reasons: 4274ddb5d2fSVladimir Sementsov-Ogievskiy * 1. From this function, executing in parallel coroutine, when our 4284ddb5d2fSVladimir Sementsov-Ogievskiy * handle is received. 4294ddb5d2fSVladimir Sementsov-Ogievskiy * 2. From nbd_channel_error(), when connection is lost. 4304ddb5d2fSVladimir Sementsov-Ogievskiy * 3. From nbd_co_receive_one_chunk(), when previous request is 4314ddb5d2fSVladimir Sementsov-Ogievskiy * finished and s->reply.handle set to 0. 4324ddb5d2fSVladimir Sementsov-Ogievskiy * Anyway, it's OK to lock the mutex and go to the next iteration. 4334ddb5d2fSVladimir Sementsov-Ogievskiy */ 4344ddb5d2fSVladimir Sementsov-Ogievskiy 4354ddb5d2fSVladimir Sementsov-Ogievskiy qemu_co_mutex_lock(&s->receive_mutex); 4364ddb5d2fSVladimir Sementsov-Ogievskiy assert(!s->requests[ind].receiving); 4374ddb5d2fSVladimir Sementsov-Ogievskiy continue; 43886f8cdf3SVladimir Sementsov-Ogievskiy } 43986f8cdf3SVladimir Sementsov-Ogievskiy 4404ddb5d2fSVladimir Sementsov-Ogievskiy /* We are under mutex and handle is 0. We have to do the dirty work. */ 4414ddb5d2fSVladimir Sementsov-Ogievskiy assert(s->reply.handle == 0); 4424ddb5d2fSVladimir Sementsov-Ogievskiy ret = nbd_receive_reply(s->bs, s->ioc, &s->reply, NULL); 4434ddb5d2fSVladimir Sementsov-Ogievskiy if (ret <= 0) { 4444ddb5d2fSVladimir Sementsov-Ogievskiy ret = ret ? ret : -EIO; 4454ddb5d2fSVladimir Sementsov-Ogievskiy nbd_channel_error(s, ret); 4464ddb5d2fSVladimir Sementsov-Ogievskiy return ret; 447f7651539SVladimir Sementsov-Ogievskiy } 4484ddb5d2fSVladimir Sementsov-Ogievskiy if (nbd_reply_is_structured(&s->reply) && !s->info.structured_reply) { 4494ddb5d2fSVladimir Sementsov-Ogievskiy nbd_channel_error(s, -EINVAL); 4504ddb5d2fSVladimir Sementsov-Ogievskiy return -EINVAL; 45178c81a3fSMax Reitz } 4528846b7d1SPaolo Bonzini ind2 = HANDLE_TO_INDEX(s, s->reply.handle); 4538846b7d1SPaolo Bonzini if (ind2 >= MAX_NBD_REQUESTS || !s->requests[ind2].coroutine) { 4548846b7d1SPaolo Bonzini nbd_channel_error(s, -EINVAL); 4558846b7d1SPaolo Bonzini return -EINVAL; 4568846b7d1SPaolo Bonzini } 4574ddb5d2fSVladimir Sementsov-Ogievskiy if (s->reply.handle == handle) { 4584ddb5d2fSVladimir Sementsov-Ogievskiy /* We are done */ 4594ddb5d2fSVladimir Sementsov-Ogievskiy return 0; 4604ddb5d2fSVladimir Sementsov-Ogievskiy } 4614ddb5d2fSVladimir Sementsov-Ogievskiy nbd_recv_coroutine_wake_one(&s->requests[ind2]); 4624ddb5d2fSVladimir Sementsov-Ogievskiy } 46386f8cdf3SVladimir Sementsov-Ogievskiy } 46486f8cdf3SVladimir Sementsov-Ogievskiy 4650c43c6fcSPaolo Bonzini static int coroutine_fn nbd_co_send_request(BlockDriverState *bs, 46686f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest *request, 46786f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector *qiov) 46886f8cdf3SVladimir Sementsov-Ogievskiy { 469611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 470a34b1e5eSVladimir Sementsov-Ogievskiy int rc, i = -1; 47186f8cdf3SVladimir Sementsov-Ogievskiy 47286f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_mutex_lock(&s->send_mutex); 4734ddb5d2fSVladimir Sementsov-Ogievskiy 4744ddb5d2fSVladimir Sementsov-Ogievskiy while (s->in_flight == MAX_NBD_REQUESTS || 4754ddb5d2fSVladimir Sementsov-Ogievskiy (!nbd_client_connected(s) && s->in_flight > 0)) 4764ddb5d2fSVladimir Sementsov-Ogievskiy { 47786f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_queue_wait(&s->free_sema, &s->send_mutex); 47886f8cdf3SVladimir Sementsov-Ogievskiy } 479a34b1e5eSVladimir Sementsov-Ogievskiy 4804ddb5d2fSVladimir Sementsov-Ogievskiy if (nbd_client_connecting(s)) { 4814ddb5d2fSVladimir Sementsov-Ogievskiy nbd_reconnect_attempt(s); 4824ddb5d2fSVladimir Sementsov-Ogievskiy } 4834ddb5d2fSVladimir Sementsov-Ogievskiy 48491e0998fSVladimir Sementsov-Ogievskiy if (!nbd_client_connected(s)) { 485a34b1e5eSVladimir Sementsov-Ogievskiy rc = -EIO; 486a34b1e5eSVladimir Sementsov-Ogievskiy goto err; 487a34b1e5eSVladimir Sementsov-Ogievskiy } 488a34b1e5eSVladimir Sementsov-Ogievskiy 48986f8cdf3SVladimir Sementsov-Ogievskiy s->in_flight++; 49086f8cdf3SVladimir Sementsov-Ogievskiy 49186f8cdf3SVladimir Sementsov-Ogievskiy for (i = 0; i < MAX_NBD_REQUESTS; i++) { 49286f8cdf3SVladimir Sementsov-Ogievskiy if (s->requests[i].coroutine == NULL) { 49386f8cdf3SVladimir Sementsov-Ogievskiy break; 49486f8cdf3SVladimir Sementsov-Ogievskiy } 49586f8cdf3SVladimir Sementsov-Ogievskiy } 49686f8cdf3SVladimir Sementsov-Ogievskiy 49786f8cdf3SVladimir Sementsov-Ogievskiy g_assert(qemu_in_coroutine()); 49886f8cdf3SVladimir Sementsov-Ogievskiy assert(i < MAX_NBD_REQUESTS); 49986f8cdf3SVladimir Sementsov-Ogievskiy 50086f8cdf3SVladimir Sementsov-Ogievskiy s->requests[i].coroutine = qemu_coroutine_self(); 50186f8cdf3SVladimir Sementsov-Ogievskiy s->requests[i].offset = request->from; 50286f8cdf3SVladimir Sementsov-Ogievskiy s->requests[i].receiving = false; 50386f8cdf3SVladimir Sementsov-Ogievskiy 50486f8cdf3SVladimir Sementsov-Ogievskiy request->handle = INDEX_TO_HANDLE(s, i); 50586f8cdf3SVladimir Sementsov-Ogievskiy 50686f8cdf3SVladimir Sementsov-Ogievskiy assert(s->ioc); 50786f8cdf3SVladimir Sementsov-Ogievskiy 50886f8cdf3SVladimir Sementsov-Ogievskiy if (qiov) { 50986f8cdf3SVladimir Sementsov-Ogievskiy qio_channel_set_cork(s->ioc, true); 51086f8cdf3SVladimir Sementsov-Ogievskiy rc = nbd_send_request(s->ioc, request); 511*172f5f1aSPaolo Bonzini if (rc >= 0) { 51286f8cdf3SVladimir Sementsov-Ogievskiy if (qio_channel_writev_all(s->ioc, qiov->iov, qiov->niov, 51386f8cdf3SVladimir Sementsov-Ogievskiy NULL) < 0) { 51486f8cdf3SVladimir Sementsov-Ogievskiy rc = -EIO; 51586f8cdf3SVladimir Sementsov-Ogievskiy } 51686f8cdf3SVladimir Sementsov-Ogievskiy } else if (rc >= 0) { 51786f8cdf3SVladimir Sementsov-Ogievskiy rc = -EIO; 51886f8cdf3SVladimir Sementsov-Ogievskiy } 51986f8cdf3SVladimir Sementsov-Ogievskiy qio_channel_set_cork(s->ioc, false); 52086f8cdf3SVladimir Sementsov-Ogievskiy } else { 52186f8cdf3SVladimir Sementsov-Ogievskiy rc = nbd_send_request(s->ioc, request); 52286f8cdf3SVladimir Sementsov-Ogievskiy } 52386f8cdf3SVladimir Sementsov-Ogievskiy 52486f8cdf3SVladimir Sementsov-Ogievskiy err: 52586f8cdf3SVladimir Sementsov-Ogievskiy if (rc < 0) { 526a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, rc); 527a34b1e5eSVladimir Sementsov-Ogievskiy if (i != -1) { 52886f8cdf3SVladimir Sementsov-Ogievskiy s->requests[i].coroutine = NULL; 52986f8cdf3SVladimir Sementsov-Ogievskiy s->in_flight--; 53086f8cdf3SVladimir Sementsov-Ogievskiy } 5316690302bSRao Lei qemu_co_queue_next(&s->free_sema); 532f7651539SVladimir Sementsov-Ogievskiy } 53386f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_mutex_unlock(&s->send_mutex); 53486f8cdf3SVladimir Sementsov-Ogievskiy return rc; 53586f8cdf3SVladimir Sementsov-Ogievskiy } 53686f8cdf3SVladimir Sementsov-Ogievskiy 53786f8cdf3SVladimir Sementsov-Ogievskiy static inline uint16_t payload_advance16(uint8_t **payload) 53886f8cdf3SVladimir Sementsov-Ogievskiy { 53986f8cdf3SVladimir Sementsov-Ogievskiy *payload += 2; 54086f8cdf3SVladimir Sementsov-Ogievskiy return lduw_be_p(*payload - 2); 54186f8cdf3SVladimir Sementsov-Ogievskiy } 54286f8cdf3SVladimir Sementsov-Ogievskiy 54386f8cdf3SVladimir Sementsov-Ogievskiy static inline uint32_t payload_advance32(uint8_t **payload) 54486f8cdf3SVladimir Sementsov-Ogievskiy { 54586f8cdf3SVladimir Sementsov-Ogievskiy *payload += 4; 54686f8cdf3SVladimir Sementsov-Ogievskiy return ldl_be_p(*payload - 4); 54786f8cdf3SVladimir Sementsov-Ogievskiy } 54886f8cdf3SVladimir Sementsov-Ogievskiy 54986f8cdf3SVladimir Sementsov-Ogievskiy static inline uint64_t payload_advance64(uint8_t **payload) 55086f8cdf3SVladimir Sementsov-Ogievskiy { 55186f8cdf3SVladimir Sementsov-Ogievskiy *payload += 8; 55286f8cdf3SVladimir Sementsov-Ogievskiy return ldq_be_p(*payload - 8); 55386f8cdf3SVladimir Sementsov-Ogievskiy } 55486f8cdf3SVladimir Sementsov-Ogievskiy 555611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_parse_offset_hole_payload(BDRVNBDState *s, 55686f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk, 55786f8cdf3SVladimir Sementsov-Ogievskiy uint8_t *payload, uint64_t orig_offset, 55886f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector *qiov, Error **errp) 55986f8cdf3SVladimir Sementsov-Ogievskiy { 56086f8cdf3SVladimir Sementsov-Ogievskiy uint64_t offset; 56186f8cdf3SVladimir Sementsov-Ogievskiy uint32_t hole_size; 56286f8cdf3SVladimir Sementsov-Ogievskiy 56386f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length != sizeof(offset) + sizeof(hole_size)) { 56486f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: invalid payload for " 56586f8cdf3SVladimir Sementsov-Ogievskiy "NBD_REPLY_TYPE_OFFSET_HOLE"); 56686f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 56786f8cdf3SVladimir Sementsov-Ogievskiy } 56886f8cdf3SVladimir Sementsov-Ogievskiy 56986f8cdf3SVladimir Sementsov-Ogievskiy offset = payload_advance64(&payload); 57086f8cdf3SVladimir Sementsov-Ogievskiy hole_size = payload_advance32(&payload); 57186f8cdf3SVladimir Sementsov-Ogievskiy 57286f8cdf3SVladimir Sementsov-Ogievskiy if (!hole_size || offset < orig_offset || hole_size > qiov->size || 57386f8cdf3SVladimir Sementsov-Ogievskiy offset > orig_offset + qiov->size - hole_size) { 57486f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent chunk exceeding requested" 57586f8cdf3SVladimir Sementsov-Ogievskiy " region"); 57686f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 57786f8cdf3SVladimir Sementsov-Ogievskiy } 578611ae1d7SVladimir Sementsov-Ogievskiy if (s->info.min_block && 579611ae1d7SVladimir Sementsov-Ogievskiy !QEMU_IS_ALIGNED(hole_size, s->info.min_block)) { 58086f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_structured_read_compliance("hole"); 58186f8cdf3SVladimir Sementsov-Ogievskiy } 58286f8cdf3SVladimir Sementsov-Ogievskiy 58386f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_memset(qiov, offset - orig_offset, 0, hole_size); 58486f8cdf3SVladimir Sementsov-Ogievskiy 58586f8cdf3SVladimir Sementsov-Ogievskiy return 0; 58686f8cdf3SVladimir Sementsov-Ogievskiy } 58786f8cdf3SVladimir Sementsov-Ogievskiy 58886f8cdf3SVladimir Sementsov-Ogievskiy /* 58986f8cdf3SVladimir Sementsov-Ogievskiy * nbd_parse_blockstatus_payload 59086f8cdf3SVladimir Sementsov-Ogievskiy * Based on our request, we expect only one extent in reply, for the 59186f8cdf3SVladimir Sementsov-Ogievskiy * base:allocation context. 59286f8cdf3SVladimir Sementsov-Ogievskiy */ 593611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_parse_blockstatus_payload(BDRVNBDState *s, 59486f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk, 59586f8cdf3SVladimir Sementsov-Ogievskiy uint8_t *payload, uint64_t orig_length, 59686f8cdf3SVladimir Sementsov-Ogievskiy NBDExtent *extent, Error **errp) 59786f8cdf3SVladimir Sementsov-Ogievskiy { 59886f8cdf3SVladimir Sementsov-Ogievskiy uint32_t context_id; 59986f8cdf3SVladimir Sementsov-Ogievskiy 60086f8cdf3SVladimir Sementsov-Ogievskiy /* The server succeeded, so it must have sent [at least] one extent */ 60186f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length < sizeof(context_id) + sizeof(*extent)) { 60286f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: invalid payload for " 60386f8cdf3SVladimir Sementsov-Ogievskiy "NBD_REPLY_TYPE_BLOCK_STATUS"); 60486f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 60586f8cdf3SVladimir Sementsov-Ogievskiy } 60686f8cdf3SVladimir Sementsov-Ogievskiy 60786f8cdf3SVladimir Sementsov-Ogievskiy context_id = payload_advance32(&payload); 608611ae1d7SVladimir Sementsov-Ogievskiy if (s->info.context_id != context_id) { 60986f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: unexpected context id %d for " 61086f8cdf3SVladimir Sementsov-Ogievskiy "NBD_REPLY_TYPE_BLOCK_STATUS, when negotiated context " 61186f8cdf3SVladimir Sementsov-Ogievskiy "id is %d", context_id, 612611ae1d7SVladimir Sementsov-Ogievskiy s->info.context_id); 61386f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 61486f8cdf3SVladimir Sementsov-Ogievskiy } 61586f8cdf3SVladimir Sementsov-Ogievskiy 61686f8cdf3SVladimir Sementsov-Ogievskiy extent->length = payload_advance32(&payload); 61786f8cdf3SVladimir Sementsov-Ogievskiy extent->flags = payload_advance32(&payload); 61886f8cdf3SVladimir Sementsov-Ogievskiy 61986f8cdf3SVladimir Sementsov-Ogievskiy if (extent->length == 0) { 62086f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent status chunk with " 62186f8cdf3SVladimir Sementsov-Ogievskiy "zero length"); 62286f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 62386f8cdf3SVladimir Sementsov-Ogievskiy } 62486f8cdf3SVladimir Sementsov-Ogievskiy 62586f8cdf3SVladimir Sementsov-Ogievskiy /* 62686f8cdf3SVladimir Sementsov-Ogievskiy * A server sending unaligned block status is in violation of the 62786f8cdf3SVladimir Sementsov-Ogievskiy * protocol, but as qemu-nbd 3.1 is such a server (at least for 62886f8cdf3SVladimir Sementsov-Ogievskiy * POSIX files that are not a multiple of 512 bytes, since qemu 62986f8cdf3SVladimir Sementsov-Ogievskiy * rounds files up to 512-byte multiples but lseek(SEEK_HOLE) 63086f8cdf3SVladimir Sementsov-Ogievskiy * still sees an implicit hole beyond the real EOF), it's nicer to 63186f8cdf3SVladimir Sementsov-Ogievskiy * work around the misbehaving server. If the request included 63286f8cdf3SVladimir Sementsov-Ogievskiy * more than the final unaligned block, truncate it back to an 63386f8cdf3SVladimir Sementsov-Ogievskiy * aligned result; if the request was only the final block, round 63486f8cdf3SVladimir Sementsov-Ogievskiy * up to the full block and change the status to fully-allocated 63586f8cdf3SVladimir Sementsov-Ogievskiy * (always a safe status, even if it loses information). 63686f8cdf3SVladimir Sementsov-Ogievskiy */ 637611ae1d7SVladimir Sementsov-Ogievskiy if (s->info.min_block && !QEMU_IS_ALIGNED(extent->length, 638611ae1d7SVladimir Sementsov-Ogievskiy s->info.min_block)) { 63986f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_parse_blockstatus_compliance("extent length is unaligned"); 640611ae1d7SVladimir Sementsov-Ogievskiy if (extent->length > s->info.min_block) { 64186f8cdf3SVladimir Sementsov-Ogievskiy extent->length = QEMU_ALIGN_DOWN(extent->length, 642611ae1d7SVladimir Sementsov-Ogievskiy s->info.min_block); 64386f8cdf3SVladimir Sementsov-Ogievskiy } else { 644611ae1d7SVladimir Sementsov-Ogievskiy extent->length = s->info.min_block; 64586f8cdf3SVladimir Sementsov-Ogievskiy extent->flags = 0; 64686f8cdf3SVladimir Sementsov-Ogievskiy } 64786f8cdf3SVladimir Sementsov-Ogievskiy } 64886f8cdf3SVladimir Sementsov-Ogievskiy 64986f8cdf3SVladimir Sementsov-Ogievskiy /* 65086f8cdf3SVladimir Sementsov-Ogievskiy * We used NBD_CMD_FLAG_REQ_ONE, so the server should not have 65186f8cdf3SVladimir Sementsov-Ogievskiy * sent us any more than one extent, nor should it have included 65286f8cdf3SVladimir Sementsov-Ogievskiy * status beyond our request in that extent. However, it's easy 65386f8cdf3SVladimir Sementsov-Ogievskiy * enough to ignore the server's noncompliance without killing the 65486f8cdf3SVladimir Sementsov-Ogievskiy * connection; just ignore trailing extents, and clamp things to 65586f8cdf3SVladimir Sementsov-Ogievskiy * the length of our request. 65686f8cdf3SVladimir Sementsov-Ogievskiy */ 65786f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length > sizeof(context_id) + sizeof(*extent)) { 65886f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_parse_blockstatus_compliance("more than one extent"); 65986f8cdf3SVladimir Sementsov-Ogievskiy } 66086f8cdf3SVladimir Sementsov-Ogievskiy if (extent->length > orig_length) { 66186f8cdf3SVladimir Sementsov-Ogievskiy extent->length = orig_length; 66286f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_parse_blockstatus_compliance("extent length too large"); 66386f8cdf3SVladimir Sementsov-Ogievskiy } 66486f8cdf3SVladimir Sementsov-Ogievskiy 665dbc7b014SEric Blake /* 666dbc7b014SEric Blake * HACK: if we are using x-dirty-bitmaps to access 667dbc7b014SEric Blake * qemu:allocation-depth, treat all depths > 2 the same as 2, 668dbc7b014SEric Blake * since nbd_client_co_block_status is only expecting the low two 669dbc7b014SEric Blake * bits to be set. 670dbc7b014SEric Blake */ 671dbc7b014SEric Blake if (s->alloc_depth && extent->flags > 2) { 672dbc7b014SEric Blake extent->flags = 2; 673dbc7b014SEric Blake } 674dbc7b014SEric Blake 67586f8cdf3SVladimir Sementsov-Ogievskiy return 0; 67686f8cdf3SVladimir Sementsov-Ogievskiy } 67786f8cdf3SVladimir Sementsov-Ogievskiy 67886f8cdf3SVladimir Sementsov-Ogievskiy /* 67986f8cdf3SVladimir Sementsov-Ogievskiy * nbd_parse_error_payload 68086f8cdf3SVladimir Sementsov-Ogievskiy * on success @errp contains message describing nbd error reply 68186f8cdf3SVladimir Sementsov-Ogievskiy */ 68286f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_parse_error_payload(NBDStructuredReplyChunk *chunk, 68386f8cdf3SVladimir Sementsov-Ogievskiy uint8_t *payload, int *request_ret, 68486f8cdf3SVladimir Sementsov-Ogievskiy Error **errp) 68586f8cdf3SVladimir Sementsov-Ogievskiy { 68686f8cdf3SVladimir Sementsov-Ogievskiy uint32_t error; 68786f8cdf3SVladimir Sementsov-Ogievskiy uint16_t message_size; 68886f8cdf3SVladimir Sementsov-Ogievskiy 68986f8cdf3SVladimir Sementsov-Ogievskiy assert(chunk->type & (1 << 15)); 69086f8cdf3SVladimir Sementsov-Ogievskiy 69186f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length < sizeof(error) + sizeof(message_size)) { 69286f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, 69386f8cdf3SVladimir Sementsov-Ogievskiy "Protocol error: invalid payload for structured error"); 69486f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 69586f8cdf3SVladimir Sementsov-Ogievskiy } 69686f8cdf3SVladimir Sementsov-Ogievskiy 69786f8cdf3SVladimir Sementsov-Ogievskiy error = nbd_errno_to_system_errno(payload_advance32(&payload)); 69886f8cdf3SVladimir Sementsov-Ogievskiy if (error == 0) { 69986f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent structured error chunk " 70086f8cdf3SVladimir Sementsov-Ogievskiy "with error = 0"); 70186f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 70286f8cdf3SVladimir Sementsov-Ogievskiy } 70386f8cdf3SVladimir Sementsov-Ogievskiy 70486f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = -error; 70586f8cdf3SVladimir Sementsov-Ogievskiy message_size = payload_advance16(&payload); 70686f8cdf3SVladimir Sementsov-Ogievskiy 70786f8cdf3SVladimir Sementsov-Ogievskiy if (message_size > chunk->length - sizeof(error) - sizeof(message_size)) { 70886f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent structured error chunk " 70986f8cdf3SVladimir Sementsov-Ogievskiy "with incorrect message size"); 71086f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 71186f8cdf3SVladimir Sementsov-Ogievskiy } 71286f8cdf3SVladimir Sementsov-Ogievskiy 71386f8cdf3SVladimir Sementsov-Ogievskiy /* TODO: Add a trace point to mention the server complaint */ 71486f8cdf3SVladimir Sementsov-Ogievskiy 71586f8cdf3SVladimir Sementsov-Ogievskiy /* TODO handle ERROR_OFFSET */ 71686f8cdf3SVladimir Sementsov-Ogievskiy 71786f8cdf3SVladimir Sementsov-Ogievskiy return 0; 71886f8cdf3SVladimir Sementsov-Ogievskiy } 71986f8cdf3SVladimir Sementsov-Ogievskiy 7200c43c6fcSPaolo Bonzini static int coroutine_fn 7210c43c6fcSPaolo Bonzini nbd_co_receive_offset_data_payload(BDRVNBDState *s, uint64_t orig_offset, 72286f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector *qiov, Error **errp) 72386f8cdf3SVladimir Sementsov-Ogievskiy { 72486f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector sub_qiov; 72586f8cdf3SVladimir Sementsov-Ogievskiy uint64_t offset; 72686f8cdf3SVladimir Sementsov-Ogievskiy size_t data_size; 72786f8cdf3SVladimir Sementsov-Ogievskiy int ret; 72886f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk = &s->reply.structured; 72986f8cdf3SVladimir Sementsov-Ogievskiy 73086f8cdf3SVladimir Sementsov-Ogievskiy assert(nbd_reply_is_structured(&s->reply)); 73186f8cdf3SVladimir Sementsov-Ogievskiy 73286f8cdf3SVladimir Sementsov-Ogievskiy /* The NBD spec requires at least one byte of payload */ 73386f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length <= sizeof(offset)) { 73486f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: invalid payload for " 73586f8cdf3SVladimir Sementsov-Ogievskiy "NBD_REPLY_TYPE_OFFSET_DATA"); 73686f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 73786f8cdf3SVladimir Sementsov-Ogievskiy } 73886f8cdf3SVladimir Sementsov-Ogievskiy 73986f8cdf3SVladimir Sementsov-Ogievskiy if (nbd_read64(s->ioc, &offset, "OFFSET_DATA offset", errp) < 0) { 74086f8cdf3SVladimir Sementsov-Ogievskiy return -EIO; 74186f8cdf3SVladimir Sementsov-Ogievskiy } 74286f8cdf3SVladimir Sementsov-Ogievskiy 74386f8cdf3SVladimir Sementsov-Ogievskiy data_size = chunk->length - sizeof(offset); 74486f8cdf3SVladimir Sementsov-Ogievskiy assert(data_size); 74586f8cdf3SVladimir Sementsov-Ogievskiy if (offset < orig_offset || data_size > qiov->size || 74686f8cdf3SVladimir Sementsov-Ogievskiy offset > orig_offset + qiov->size - data_size) { 74786f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent chunk exceeding requested" 74886f8cdf3SVladimir Sementsov-Ogievskiy " region"); 74986f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 75086f8cdf3SVladimir Sementsov-Ogievskiy } 75186f8cdf3SVladimir Sementsov-Ogievskiy if (s->info.min_block && !QEMU_IS_ALIGNED(data_size, s->info.min_block)) { 75286f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_structured_read_compliance("data"); 75386f8cdf3SVladimir Sementsov-Ogievskiy } 75486f8cdf3SVladimir Sementsov-Ogievskiy 75586f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_init(&sub_qiov, qiov->niov); 75686f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_concat(&sub_qiov, qiov, offset - orig_offset, data_size); 75786f8cdf3SVladimir Sementsov-Ogievskiy ret = qio_channel_readv_all(s->ioc, sub_qiov.iov, sub_qiov.niov, errp); 75886f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_destroy(&sub_qiov); 75986f8cdf3SVladimir Sementsov-Ogievskiy 76086f8cdf3SVladimir Sementsov-Ogievskiy return ret < 0 ? -EIO : 0; 76186f8cdf3SVladimir Sementsov-Ogievskiy } 76286f8cdf3SVladimir Sementsov-Ogievskiy 76386f8cdf3SVladimir Sementsov-Ogievskiy #define NBD_MAX_MALLOC_PAYLOAD 1000 76486f8cdf3SVladimir Sementsov-Ogievskiy static coroutine_fn int nbd_co_receive_structured_payload( 765611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s, void **payload, Error **errp) 76686f8cdf3SVladimir Sementsov-Ogievskiy { 76786f8cdf3SVladimir Sementsov-Ogievskiy int ret; 76886f8cdf3SVladimir Sementsov-Ogievskiy uint32_t len; 76986f8cdf3SVladimir Sementsov-Ogievskiy 77086f8cdf3SVladimir Sementsov-Ogievskiy assert(nbd_reply_is_structured(&s->reply)); 77186f8cdf3SVladimir Sementsov-Ogievskiy 77286f8cdf3SVladimir Sementsov-Ogievskiy len = s->reply.structured.length; 77386f8cdf3SVladimir Sementsov-Ogievskiy 77486f8cdf3SVladimir Sementsov-Ogievskiy if (len == 0) { 77586f8cdf3SVladimir Sementsov-Ogievskiy return 0; 77686f8cdf3SVladimir Sementsov-Ogievskiy } 77786f8cdf3SVladimir Sementsov-Ogievskiy 77886f8cdf3SVladimir Sementsov-Ogievskiy if (payload == NULL) { 77986f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Unexpected structured payload"); 78086f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 78186f8cdf3SVladimir Sementsov-Ogievskiy } 78286f8cdf3SVladimir Sementsov-Ogievskiy 78386f8cdf3SVladimir Sementsov-Ogievskiy if (len > NBD_MAX_MALLOC_PAYLOAD) { 78486f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Payload too large"); 78586f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 78686f8cdf3SVladimir Sementsov-Ogievskiy } 78786f8cdf3SVladimir Sementsov-Ogievskiy 78886f8cdf3SVladimir Sementsov-Ogievskiy *payload = g_new(char, len); 78986f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_read(s->ioc, *payload, len, "structured payload", errp); 79086f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 79186f8cdf3SVladimir Sementsov-Ogievskiy g_free(*payload); 79286f8cdf3SVladimir Sementsov-Ogievskiy *payload = NULL; 79386f8cdf3SVladimir Sementsov-Ogievskiy return ret; 79486f8cdf3SVladimir Sementsov-Ogievskiy } 79586f8cdf3SVladimir Sementsov-Ogievskiy 79686f8cdf3SVladimir Sementsov-Ogievskiy return 0; 79786f8cdf3SVladimir Sementsov-Ogievskiy } 79886f8cdf3SVladimir Sementsov-Ogievskiy 79986f8cdf3SVladimir Sementsov-Ogievskiy /* 80086f8cdf3SVladimir Sementsov-Ogievskiy * nbd_co_do_receive_one_chunk 80186f8cdf3SVladimir Sementsov-Ogievskiy * for simple reply: 80286f8cdf3SVladimir Sementsov-Ogievskiy * set request_ret to received reply error 80386f8cdf3SVladimir Sementsov-Ogievskiy * if qiov is not NULL: read payload to @qiov 80486f8cdf3SVladimir Sementsov-Ogievskiy * for structured reply chunk: 80586f8cdf3SVladimir Sementsov-Ogievskiy * if error chunk: read payload, set @request_ret, do not set @payload 80686f8cdf3SVladimir Sementsov-Ogievskiy * else if offset_data chunk: read payload data to @qiov, do not set @payload 80786f8cdf3SVladimir Sementsov-Ogievskiy * else: read payload to @payload 80886f8cdf3SVladimir Sementsov-Ogievskiy * 80986f8cdf3SVladimir Sementsov-Ogievskiy * If function fails, @errp contains corresponding error message, and the 81086f8cdf3SVladimir Sementsov-Ogievskiy * connection with the server is suspect. If it returns 0, then the 81186f8cdf3SVladimir Sementsov-Ogievskiy * transaction succeeded (although @request_ret may be a negative errno 81286f8cdf3SVladimir Sementsov-Ogievskiy * corresponding to the server's error reply), and errp is unchanged. 81386f8cdf3SVladimir Sementsov-Ogievskiy */ 81486f8cdf3SVladimir Sementsov-Ogievskiy static coroutine_fn int nbd_co_do_receive_one_chunk( 815611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s, uint64_t handle, bool only_structured, 81686f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, QEMUIOVector *qiov, void **payload, Error **errp) 81786f8cdf3SVladimir Sementsov-Ogievskiy { 81886f8cdf3SVladimir Sementsov-Ogievskiy int ret; 81986f8cdf3SVladimir Sementsov-Ogievskiy int i = HANDLE_TO_INDEX(s, handle); 82086f8cdf3SVladimir Sementsov-Ogievskiy void *local_payload = NULL; 82186f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk; 82286f8cdf3SVladimir Sementsov-Ogievskiy 82386f8cdf3SVladimir Sementsov-Ogievskiy if (payload) { 82486f8cdf3SVladimir Sementsov-Ogievskiy *payload = NULL; 82586f8cdf3SVladimir Sementsov-Ogievskiy } 82686f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = 0; 82786f8cdf3SVladimir Sementsov-Ogievskiy 828*172f5f1aSPaolo Bonzini ret = nbd_receive_replies(s, handle); 829*172f5f1aSPaolo Bonzini if (ret < 0) { 83086f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Connection closed"); 83186f8cdf3SVladimir Sementsov-Ogievskiy return -EIO; 83286f8cdf3SVladimir Sementsov-Ogievskiy } 83386f8cdf3SVladimir Sementsov-Ogievskiy assert(s->ioc); 83486f8cdf3SVladimir Sementsov-Ogievskiy 83586f8cdf3SVladimir Sementsov-Ogievskiy assert(s->reply.handle == handle); 83686f8cdf3SVladimir Sementsov-Ogievskiy 83786f8cdf3SVladimir Sementsov-Ogievskiy if (nbd_reply_is_simple(&s->reply)) { 83886f8cdf3SVladimir Sementsov-Ogievskiy if (only_structured) { 83986f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: simple reply when structured " 84086f8cdf3SVladimir Sementsov-Ogievskiy "reply chunk was expected"); 84186f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 84286f8cdf3SVladimir Sementsov-Ogievskiy } 84386f8cdf3SVladimir Sementsov-Ogievskiy 84486f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = -nbd_errno_to_system_errno(s->reply.simple.error); 84586f8cdf3SVladimir Sementsov-Ogievskiy if (*request_ret < 0 || !qiov) { 84686f8cdf3SVladimir Sementsov-Ogievskiy return 0; 84786f8cdf3SVladimir Sementsov-Ogievskiy } 84886f8cdf3SVladimir Sementsov-Ogievskiy 84986f8cdf3SVladimir Sementsov-Ogievskiy return qio_channel_readv_all(s->ioc, qiov->iov, qiov->niov, 85086f8cdf3SVladimir Sementsov-Ogievskiy errp) < 0 ? -EIO : 0; 85186f8cdf3SVladimir Sementsov-Ogievskiy } 85286f8cdf3SVladimir Sementsov-Ogievskiy 85386f8cdf3SVladimir Sementsov-Ogievskiy /* handle structured reply chunk */ 85486f8cdf3SVladimir Sementsov-Ogievskiy assert(s->info.structured_reply); 85586f8cdf3SVladimir Sementsov-Ogievskiy chunk = &s->reply.structured; 85686f8cdf3SVladimir Sementsov-Ogievskiy 85786f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->type == NBD_REPLY_TYPE_NONE) { 85886f8cdf3SVladimir Sementsov-Ogievskiy if (!(chunk->flags & NBD_REPLY_FLAG_DONE)) { 85986f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk without" 86086f8cdf3SVladimir Sementsov-Ogievskiy " NBD_REPLY_FLAG_DONE flag set"); 86186f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 86286f8cdf3SVladimir Sementsov-Ogievskiy } 86386f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length) { 86486f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk with" 86586f8cdf3SVladimir Sementsov-Ogievskiy " nonzero length"); 86686f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 86786f8cdf3SVladimir Sementsov-Ogievskiy } 86886f8cdf3SVladimir Sementsov-Ogievskiy return 0; 86986f8cdf3SVladimir Sementsov-Ogievskiy } 87086f8cdf3SVladimir Sementsov-Ogievskiy 87186f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->type == NBD_REPLY_TYPE_OFFSET_DATA) { 87286f8cdf3SVladimir Sementsov-Ogievskiy if (!qiov) { 87386f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Unexpected NBD_REPLY_TYPE_OFFSET_DATA chunk"); 87486f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 87586f8cdf3SVladimir Sementsov-Ogievskiy } 87686f8cdf3SVladimir Sementsov-Ogievskiy 87786f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_receive_offset_data_payload(s, s->requests[i].offset, 87886f8cdf3SVladimir Sementsov-Ogievskiy qiov, errp); 87986f8cdf3SVladimir Sementsov-Ogievskiy } 88086f8cdf3SVladimir Sementsov-Ogievskiy 88186f8cdf3SVladimir Sementsov-Ogievskiy if (nbd_reply_type_is_error(chunk->type)) { 88286f8cdf3SVladimir Sementsov-Ogievskiy payload = &local_payload; 88386f8cdf3SVladimir Sementsov-Ogievskiy } 88486f8cdf3SVladimir Sementsov-Ogievskiy 88586f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_co_receive_structured_payload(s, payload, errp); 88686f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 88786f8cdf3SVladimir Sementsov-Ogievskiy return ret; 88886f8cdf3SVladimir Sementsov-Ogievskiy } 88986f8cdf3SVladimir Sementsov-Ogievskiy 89086f8cdf3SVladimir Sementsov-Ogievskiy if (nbd_reply_type_is_error(chunk->type)) { 89186f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_parse_error_payload(chunk, local_payload, request_ret, errp); 89286f8cdf3SVladimir Sementsov-Ogievskiy g_free(local_payload); 89386f8cdf3SVladimir Sementsov-Ogievskiy return ret; 89486f8cdf3SVladimir Sementsov-Ogievskiy } 89586f8cdf3SVladimir Sementsov-Ogievskiy 89686f8cdf3SVladimir Sementsov-Ogievskiy return 0; 89786f8cdf3SVladimir Sementsov-Ogievskiy } 89886f8cdf3SVladimir Sementsov-Ogievskiy 89986f8cdf3SVladimir Sementsov-Ogievskiy /* 90086f8cdf3SVladimir Sementsov-Ogievskiy * nbd_co_receive_one_chunk 90186f8cdf3SVladimir Sementsov-Ogievskiy * Read reply, wake up connection_co and set s->quit if needed. 90286f8cdf3SVladimir Sementsov-Ogievskiy * Return value is a fatal error code or normal nbd reply error code 90386f8cdf3SVladimir Sementsov-Ogievskiy */ 90486f8cdf3SVladimir Sementsov-Ogievskiy static coroutine_fn int nbd_co_receive_one_chunk( 905611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s, uint64_t handle, bool only_structured, 90686f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, QEMUIOVector *qiov, NBDReply *reply, void **payload, 90786f8cdf3SVladimir Sementsov-Ogievskiy Error **errp) 90886f8cdf3SVladimir Sementsov-Ogievskiy { 90986f8cdf3SVladimir Sementsov-Ogievskiy int ret = nbd_co_do_receive_one_chunk(s, handle, only_structured, 91086f8cdf3SVladimir Sementsov-Ogievskiy request_ret, qiov, payload, errp); 91186f8cdf3SVladimir Sementsov-Ogievskiy 91286f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 9135cf42b1cSEric Blake memset(reply, 0, sizeof(*reply)); 914a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, ret); 91586f8cdf3SVladimir Sementsov-Ogievskiy } else { 91686f8cdf3SVladimir Sementsov-Ogievskiy /* For assert at loop start in nbd_connection_entry */ 91786f8cdf3SVladimir Sementsov-Ogievskiy *reply = s->reply; 91886f8cdf3SVladimir Sementsov-Ogievskiy } 919f7651539SVladimir Sementsov-Ogievskiy s->reply.handle = 0; 92086f8cdf3SVladimir Sementsov-Ogievskiy 9214ddb5d2fSVladimir Sementsov-Ogievskiy nbd_recv_coroutines_wake(s, false); 92286f8cdf3SVladimir Sementsov-Ogievskiy 92386f8cdf3SVladimir Sementsov-Ogievskiy return ret; 92486f8cdf3SVladimir Sementsov-Ogievskiy } 92586f8cdf3SVladimir Sementsov-Ogievskiy 92686f8cdf3SVladimir Sementsov-Ogievskiy typedef struct NBDReplyChunkIter { 92786f8cdf3SVladimir Sementsov-Ogievskiy int ret; 92886f8cdf3SVladimir Sementsov-Ogievskiy int request_ret; 92986f8cdf3SVladimir Sementsov-Ogievskiy Error *err; 93086f8cdf3SVladimir Sementsov-Ogievskiy bool done, only_structured; 93186f8cdf3SVladimir Sementsov-Ogievskiy } NBDReplyChunkIter; 93286f8cdf3SVladimir Sementsov-Ogievskiy 93386f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_iter_channel_error(NBDReplyChunkIter *iter, 93486f8cdf3SVladimir Sementsov-Ogievskiy int ret, Error **local_err) 93586f8cdf3SVladimir Sementsov-Ogievskiy { 936d9366135SVladimir Sementsov-Ogievskiy assert(local_err && *local_err); 93786f8cdf3SVladimir Sementsov-Ogievskiy assert(ret < 0); 93886f8cdf3SVladimir Sementsov-Ogievskiy 93986f8cdf3SVladimir Sementsov-Ogievskiy if (!iter->ret) { 94086f8cdf3SVladimir Sementsov-Ogievskiy iter->ret = ret; 94186f8cdf3SVladimir Sementsov-Ogievskiy error_propagate(&iter->err, *local_err); 94286f8cdf3SVladimir Sementsov-Ogievskiy } else { 94386f8cdf3SVladimir Sementsov-Ogievskiy error_free(*local_err); 94486f8cdf3SVladimir Sementsov-Ogievskiy } 94586f8cdf3SVladimir Sementsov-Ogievskiy 94686f8cdf3SVladimir Sementsov-Ogievskiy *local_err = NULL; 94786f8cdf3SVladimir Sementsov-Ogievskiy } 94886f8cdf3SVladimir Sementsov-Ogievskiy 94986f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_iter_request_error(NBDReplyChunkIter *iter, int ret) 95086f8cdf3SVladimir Sementsov-Ogievskiy { 95186f8cdf3SVladimir Sementsov-Ogievskiy assert(ret < 0); 95286f8cdf3SVladimir Sementsov-Ogievskiy 95386f8cdf3SVladimir Sementsov-Ogievskiy if (!iter->request_ret) { 95486f8cdf3SVladimir Sementsov-Ogievskiy iter->request_ret = ret; 95586f8cdf3SVladimir Sementsov-Ogievskiy } 95686f8cdf3SVladimir Sementsov-Ogievskiy } 95786f8cdf3SVladimir Sementsov-Ogievskiy 95886f8cdf3SVladimir Sementsov-Ogievskiy /* 95986f8cdf3SVladimir Sementsov-Ogievskiy * NBD_FOREACH_REPLY_CHUNK 96086f8cdf3SVladimir Sementsov-Ogievskiy * The pointer stored in @payload requires g_free() to free it. 96186f8cdf3SVladimir Sementsov-Ogievskiy */ 96286f8cdf3SVladimir Sementsov-Ogievskiy #define NBD_FOREACH_REPLY_CHUNK(s, iter, handle, structured, \ 96386f8cdf3SVladimir Sementsov-Ogievskiy qiov, reply, payload) \ 96486f8cdf3SVladimir Sementsov-Ogievskiy for (iter = (NBDReplyChunkIter) { .only_structured = structured }; \ 96586f8cdf3SVladimir Sementsov-Ogievskiy nbd_reply_chunk_iter_receive(s, &iter, handle, qiov, reply, payload);) 96686f8cdf3SVladimir Sementsov-Ogievskiy 96786f8cdf3SVladimir Sementsov-Ogievskiy /* 96886f8cdf3SVladimir Sementsov-Ogievskiy * nbd_reply_chunk_iter_receive 96986f8cdf3SVladimir Sementsov-Ogievskiy * The pointer stored in @payload requires g_free() to free it. 97086f8cdf3SVladimir Sementsov-Ogievskiy */ 971611ae1d7SVladimir Sementsov-Ogievskiy static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s, 97286f8cdf3SVladimir Sementsov-Ogievskiy NBDReplyChunkIter *iter, 97386f8cdf3SVladimir Sementsov-Ogievskiy uint64_t handle, 97486f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector *qiov, NBDReply *reply, 97586f8cdf3SVladimir Sementsov-Ogievskiy void **payload) 97686f8cdf3SVladimir Sementsov-Ogievskiy { 97786f8cdf3SVladimir Sementsov-Ogievskiy int ret, request_ret; 97886f8cdf3SVladimir Sementsov-Ogievskiy NBDReply local_reply; 97986f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk; 98086f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 98186f8cdf3SVladimir Sementsov-Ogievskiy 98286f8cdf3SVladimir Sementsov-Ogievskiy if (iter->done) { 98386f8cdf3SVladimir Sementsov-Ogievskiy /* Previous iteration was last. */ 98486f8cdf3SVladimir Sementsov-Ogievskiy goto break_loop; 98586f8cdf3SVladimir Sementsov-Ogievskiy } 98686f8cdf3SVladimir Sementsov-Ogievskiy 98786f8cdf3SVladimir Sementsov-Ogievskiy if (reply == NULL) { 98886f8cdf3SVladimir Sementsov-Ogievskiy reply = &local_reply; 98986f8cdf3SVladimir Sementsov-Ogievskiy } 99086f8cdf3SVladimir Sementsov-Ogievskiy 99186f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_co_receive_one_chunk(s, handle, iter->only_structured, 99286f8cdf3SVladimir Sementsov-Ogievskiy &request_ret, qiov, reply, payload, 99386f8cdf3SVladimir Sementsov-Ogievskiy &local_err); 99486f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 99586f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(iter, ret, &local_err); 99686f8cdf3SVladimir Sementsov-Ogievskiy } else if (request_ret < 0) { 99786f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_request_error(iter, request_ret); 99886f8cdf3SVladimir Sementsov-Ogievskiy } 99986f8cdf3SVladimir Sementsov-Ogievskiy 100086f8cdf3SVladimir Sementsov-Ogievskiy /* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */ 1001*172f5f1aSPaolo Bonzini if (nbd_reply_is_simple(reply) || iter->ret < 0) { 100286f8cdf3SVladimir Sementsov-Ogievskiy goto break_loop; 100386f8cdf3SVladimir Sementsov-Ogievskiy } 100486f8cdf3SVladimir Sementsov-Ogievskiy 100586f8cdf3SVladimir Sementsov-Ogievskiy chunk = &reply->structured; 100686f8cdf3SVladimir Sementsov-Ogievskiy iter->only_structured = true; 100786f8cdf3SVladimir Sementsov-Ogievskiy 100886f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->type == NBD_REPLY_TYPE_NONE) { 100986f8cdf3SVladimir Sementsov-Ogievskiy /* NBD_REPLY_FLAG_DONE is already checked in nbd_co_receive_one_chunk */ 101086f8cdf3SVladimir Sementsov-Ogievskiy assert(chunk->flags & NBD_REPLY_FLAG_DONE); 101186f8cdf3SVladimir Sementsov-Ogievskiy goto break_loop; 101286f8cdf3SVladimir Sementsov-Ogievskiy } 101386f8cdf3SVladimir Sementsov-Ogievskiy 101486f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->flags & NBD_REPLY_FLAG_DONE) { 101586f8cdf3SVladimir Sementsov-Ogievskiy /* This iteration is last. */ 101686f8cdf3SVladimir Sementsov-Ogievskiy iter->done = true; 101786f8cdf3SVladimir Sementsov-Ogievskiy } 101886f8cdf3SVladimir Sementsov-Ogievskiy 101986f8cdf3SVladimir Sementsov-Ogievskiy /* Execute the loop body */ 102086f8cdf3SVladimir Sementsov-Ogievskiy return true; 102186f8cdf3SVladimir Sementsov-Ogievskiy 102286f8cdf3SVladimir Sementsov-Ogievskiy break_loop: 102386f8cdf3SVladimir Sementsov-Ogievskiy s->requests[HANDLE_TO_INDEX(s, handle)].coroutine = NULL; 102486f8cdf3SVladimir Sementsov-Ogievskiy 102586f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_mutex_lock(&s->send_mutex); 102686f8cdf3SVladimir Sementsov-Ogievskiy s->in_flight--; 102786f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_queue_next(&s->free_sema); 102886f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_mutex_unlock(&s->send_mutex); 102986f8cdf3SVladimir Sementsov-Ogievskiy 103086f8cdf3SVladimir Sementsov-Ogievskiy return false; 103186f8cdf3SVladimir Sementsov-Ogievskiy } 103286f8cdf3SVladimir Sementsov-Ogievskiy 10330c43c6fcSPaolo Bonzini static int coroutine_fn nbd_co_receive_return_code(BDRVNBDState *s, uint64_t handle, 103486f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, Error **errp) 103586f8cdf3SVladimir Sementsov-Ogievskiy { 103686f8cdf3SVladimir Sementsov-Ogievskiy NBDReplyChunkIter iter; 103786f8cdf3SVladimir Sementsov-Ogievskiy 103886f8cdf3SVladimir Sementsov-Ogievskiy NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, NULL, NULL) { 103986f8cdf3SVladimir Sementsov-Ogievskiy /* nbd_reply_chunk_iter_receive does all the work */ 104086f8cdf3SVladimir Sementsov-Ogievskiy } 104186f8cdf3SVladimir Sementsov-Ogievskiy 104286f8cdf3SVladimir Sementsov-Ogievskiy error_propagate(errp, iter.err); 104386f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = iter.request_ret; 104486f8cdf3SVladimir Sementsov-Ogievskiy return iter.ret; 104586f8cdf3SVladimir Sementsov-Ogievskiy } 104686f8cdf3SVladimir Sementsov-Ogievskiy 10470c43c6fcSPaolo Bonzini static int coroutine_fn nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_t handle, 104886f8cdf3SVladimir Sementsov-Ogievskiy uint64_t offset, QEMUIOVector *qiov, 104986f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, Error **errp) 105086f8cdf3SVladimir Sementsov-Ogievskiy { 105186f8cdf3SVladimir Sementsov-Ogievskiy NBDReplyChunkIter iter; 105286f8cdf3SVladimir Sementsov-Ogievskiy NBDReply reply; 105386f8cdf3SVladimir Sementsov-Ogievskiy void *payload = NULL; 105486f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 105586f8cdf3SVladimir Sementsov-Ogievskiy 105686f8cdf3SVladimir Sementsov-Ogievskiy NBD_FOREACH_REPLY_CHUNK(s, iter, handle, s->info.structured_reply, 105786f8cdf3SVladimir Sementsov-Ogievskiy qiov, &reply, &payload) 105886f8cdf3SVladimir Sementsov-Ogievskiy { 105986f8cdf3SVladimir Sementsov-Ogievskiy int ret; 106086f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk = &reply.structured; 106186f8cdf3SVladimir Sementsov-Ogievskiy 106286f8cdf3SVladimir Sementsov-Ogievskiy assert(nbd_reply_is_structured(&reply)); 106386f8cdf3SVladimir Sementsov-Ogievskiy 106486f8cdf3SVladimir Sementsov-Ogievskiy switch (chunk->type) { 106586f8cdf3SVladimir Sementsov-Ogievskiy case NBD_REPLY_TYPE_OFFSET_DATA: 106686f8cdf3SVladimir Sementsov-Ogievskiy /* 106786f8cdf3SVladimir Sementsov-Ogievskiy * special cased in nbd_co_receive_one_chunk, data is already 106886f8cdf3SVladimir Sementsov-Ogievskiy * in qiov 106986f8cdf3SVladimir Sementsov-Ogievskiy */ 107086f8cdf3SVladimir Sementsov-Ogievskiy break; 107186f8cdf3SVladimir Sementsov-Ogievskiy case NBD_REPLY_TYPE_OFFSET_HOLE: 107286f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_parse_offset_hole_payload(s, &reply.structured, payload, 107386f8cdf3SVladimir Sementsov-Ogievskiy offset, qiov, &local_err); 107486f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1075a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, ret); 107686f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, ret, &local_err); 107786f8cdf3SVladimir Sementsov-Ogievskiy } 107886f8cdf3SVladimir Sementsov-Ogievskiy break; 107986f8cdf3SVladimir Sementsov-Ogievskiy default: 108086f8cdf3SVladimir Sementsov-Ogievskiy if (!nbd_reply_type_is_error(chunk->type)) { 108186f8cdf3SVladimir Sementsov-Ogievskiy /* not allowed reply type */ 1082a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, -EINVAL); 108386f8cdf3SVladimir Sementsov-Ogievskiy error_setg(&local_err, 108486f8cdf3SVladimir Sementsov-Ogievskiy "Unexpected reply type: %d (%s) for CMD_READ", 108586f8cdf3SVladimir Sementsov-Ogievskiy chunk->type, nbd_reply_type_lookup(chunk->type)); 108686f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, -EINVAL, &local_err); 108786f8cdf3SVladimir Sementsov-Ogievskiy } 108886f8cdf3SVladimir Sementsov-Ogievskiy } 108986f8cdf3SVladimir Sementsov-Ogievskiy 109086f8cdf3SVladimir Sementsov-Ogievskiy g_free(payload); 109186f8cdf3SVladimir Sementsov-Ogievskiy payload = NULL; 109286f8cdf3SVladimir Sementsov-Ogievskiy } 109386f8cdf3SVladimir Sementsov-Ogievskiy 109486f8cdf3SVladimir Sementsov-Ogievskiy error_propagate(errp, iter.err); 109586f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = iter.request_ret; 109686f8cdf3SVladimir Sementsov-Ogievskiy return iter.ret; 109786f8cdf3SVladimir Sementsov-Ogievskiy } 109886f8cdf3SVladimir Sementsov-Ogievskiy 10990c43c6fcSPaolo Bonzini static int coroutine_fn nbd_co_receive_blockstatus_reply(BDRVNBDState *s, 110086f8cdf3SVladimir Sementsov-Ogievskiy uint64_t handle, uint64_t length, 110186f8cdf3SVladimir Sementsov-Ogievskiy NBDExtent *extent, 110286f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, Error **errp) 110386f8cdf3SVladimir Sementsov-Ogievskiy { 110486f8cdf3SVladimir Sementsov-Ogievskiy NBDReplyChunkIter iter; 110586f8cdf3SVladimir Sementsov-Ogievskiy NBDReply reply; 110686f8cdf3SVladimir Sementsov-Ogievskiy void *payload = NULL; 110786f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 110886f8cdf3SVladimir Sementsov-Ogievskiy bool received = false; 110986f8cdf3SVladimir Sementsov-Ogievskiy 111086f8cdf3SVladimir Sementsov-Ogievskiy assert(!extent->length); 111186f8cdf3SVladimir Sementsov-Ogievskiy NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, &reply, &payload) { 111286f8cdf3SVladimir Sementsov-Ogievskiy int ret; 111386f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk = &reply.structured; 111486f8cdf3SVladimir Sementsov-Ogievskiy 111586f8cdf3SVladimir Sementsov-Ogievskiy assert(nbd_reply_is_structured(&reply)); 111686f8cdf3SVladimir Sementsov-Ogievskiy 111786f8cdf3SVladimir Sementsov-Ogievskiy switch (chunk->type) { 111886f8cdf3SVladimir Sementsov-Ogievskiy case NBD_REPLY_TYPE_BLOCK_STATUS: 111986f8cdf3SVladimir Sementsov-Ogievskiy if (received) { 1120a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, -EINVAL); 112186f8cdf3SVladimir Sementsov-Ogievskiy error_setg(&local_err, "Several BLOCK_STATUS chunks in reply"); 112286f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, -EINVAL, &local_err); 112386f8cdf3SVladimir Sementsov-Ogievskiy } 112486f8cdf3SVladimir Sementsov-Ogievskiy received = true; 112586f8cdf3SVladimir Sementsov-Ogievskiy 112686f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_parse_blockstatus_payload(s, &reply.structured, 112786f8cdf3SVladimir Sementsov-Ogievskiy payload, length, extent, 112886f8cdf3SVladimir Sementsov-Ogievskiy &local_err); 112986f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1130a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, ret); 113186f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, ret, &local_err); 113286f8cdf3SVladimir Sementsov-Ogievskiy } 113386f8cdf3SVladimir Sementsov-Ogievskiy break; 113486f8cdf3SVladimir Sementsov-Ogievskiy default: 113586f8cdf3SVladimir Sementsov-Ogievskiy if (!nbd_reply_type_is_error(chunk->type)) { 1136a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, -EINVAL); 113786f8cdf3SVladimir Sementsov-Ogievskiy error_setg(&local_err, 113886f8cdf3SVladimir Sementsov-Ogievskiy "Unexpected reply type: %d (%s) " 113986f8cdf3SVladimir Sementsov-Ogievskiy "for CMD_BLOCK_STATUS", 114086f8cdf3SVladimir Sementsov-Ogievskiy chunk->type, nbd_reply_type_lookup(chunk->type)); 114186f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, -EINVAL, &local_err); 114286f8cdf3SVladimir Sementsov-Ogievskiy } 114386f8cdf3SVladimir Sementsov-Ogievskiy } 114486f8cdf3SVladimir Sementsov-Ogievskiy 114586f8cdf3SVladimir Sementsov-Ogievskiy g_free(payload); 114686f8cdf3SVladimir Sementsov-Ogievskiy payload = NULL; 114786f8cdf3SVladimir Sementsov-Ogievskiy } 114886f8cdf3SVladimir Sementsov-Ogievskiy 114986f8cdf3SVladimir Sementsov-Ogievskiy if (!extent->length && !iter.request_ret) { 115086f8cdf3SVladimir Sementsov-Ogievskiy error_setg(&local_err, "Server did not reply with any status extents"); 115186f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, -EIO, &local_err); 115286f8cdf3SVladimir Sementsov-Ogievskiy } 115386f8cdf3SVladimir Sementsov-Ogievskiy 115486f8cdf3SVladimir Sementsov-Ogievskiy error_propagate(errp, iter.err); 115586f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = iter.request_ret; 115686f8cdf3SVladimir Sementsov-Ogievskiy return iter.ret; 115786f8cdf3SVladimir Sementsov-Ogievskiy } 115886f8cdf3SVladimir Sementsov-Ogievskiy 11590c43c6fcSPaolo Bonzini static int coroutine_fn nbd_co_request(BlockDriverState *bs, NBDRequest *request, 116086f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector *write_qiov) 116186f8cdf3SVladimir Sementsov-Ogievskiy { 116286f8cdf3SVladimir Sementsov-Ogievskiy int ret, request_ret; 116386f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 1164611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 116586f8cdf3SVladimir Sementsov-Ogievskiy 116686f8cdf3SVladimir Sementsov-Ogievskiy assert(request->type != NBD_CMD_READ); 116786f8cdf3SVladimir Sementsov-Ogievskiy if (write_qiov) { 116886f8cdf3SVladimir Sementsov-Ogievskiy assert(request->type == NBD_CMD_WRITE); 116986f8cdf3SVladimir Sementsov-Ogievskiy assert(request->len == iov_size(write_qiov->iov, write_qiov->niov)); 117086f8cdf3SVladimir Sementsov-Ogievskiy } else { 117186f8cdf3SVladimir Sementsov-Ogievskiy assert(request->type != NBD_CMD_WRITE); 117286f8cdf3SVladimir Sementsov-Ogievskiy } 1173f7651539SVladimir Sementsov-Ogievskiy 1174f7651539SVladimir Sementsov-Ogievskiy do { 117586f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_co_send_request(bs, request, write_qiov); 117686f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1177f7651539SVladimir Sementsov-Ogievskiy continue; 117886f8cdf3SVladimir Sementsov-Ogievskiy } 117986f8cdf3SVladimir Sementsov-Ogievskiy 1180611ae1d7SVladimir Sementsov-Ogievskiy ret = nbd_co_receive_return_code(s, request->handle, 118186f8cdf3SVladimir Sementsov-Ogievskiy &request_ret, &local_err); 118286f8cdf3SVladimir Sementsov-Ogievskiy if (local_err) { 1183f7651539SVladimir Sementsov-Ogievskiy trace_nbd_co_request_fail(request->from, request->len, 1184f7651539SVladimir Sementsov-Ogievskiy request->handle, request->flags, 1185f7651539SVladimir Sementsov-Ogievskiy request->type, 118686f8cdf3SVladimir Sementsov-Ogievskiy nbd_cmd_lookup(request->type), 118786f8cdf3SVladimir Sementsov-Ogievskiy ret, error_get_pretty(local_err)); 118886f8cdf3SVladimir Sementsov-Ogievskiy error_free(local_err); 1189f7651539SVladimir Sementsov-Ogievskiy local_err = NULL; 119086f8cdf3SVladimir Sementsov-Ogievskiy } 1191f7651539SVladimir Sementsov-Ogievskiy } while (ret < 0 && nbd_client_connecting_wait(s)); 1192f7651539SVladimir Sementsov-Ogievskiy 119386f8cdf3SVladimir Sementsov-Ogievskiy return ret ? ret : request_ret; 119486f8cdf3SVladimir Sementsov-Ogievskiy } 119586f8cdf3SVladimir Sementsov-Ogievskiy 11960c43c6fcSPaolo Bonzini static int coroutine_fn nbd_client_co_preadv(BlockDriverState *bs, int64_t offset, 1197f7ef38ddSVladimir Sementsov-Ogievskiy int64_t bytes, QEMUIOVector *qiov, 1198f7ef38ddSVladimir Sementsov-Ogievskiy BdrvRequestFlags flags) 119986f8cdf3SVladimir Sementsov-Ogievskiy { 120086f8cdf3SVladimir Sementsov-Ogievskiy int ret, request_ret; 120186f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 1202611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 120386f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 120486f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_READ, 120586f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 120686f8cdf3SVladimir Sementsov-Ogievskiy .len = bytes, 120786f8cdf3SVladimir Sementsov-Ogievskiy }; 120886f8cdf3SVladimir Sementsov-Ogievskiy 120986f8cdf3SVladimir Sementsov-Ogievskiy assert(bytes <= NBD_MAX_BUFFER_SIZE); 121086f8cdf3SVladimir Sementsov-Ogievskiy assert(!flags); 121186f8cdf3SVladimir Sementsov-Ogievskiy 121286f8cdf3SVladimir Sementsov-Ogievskiy if (!bytes) { 121386f8cdf3SVladimir Sementsov-Ogievskiy return 0; 121486f8cdf3SVladimir Sementsov-Ogievskiy } 121586f8cdf3SVladimir Sementsov-Ogievskiy /* 121686f8cdf3SVladimir Sementsov-Ogievskiy * Work around the fact that the block layer doesn't do 121786f8cdf3SVladimir Sementsov-Ogievskiy * byte-accurate sizing yet - if the read exceeds the server's 121886f8cdf3SVladimir Sementsov-Ogievskiy * advertised size because the block layer rounded size up, then 121986f8cdf3SVladimir Sementsov-Ogievskiy * truncate the request to the server and tail-pad with zero. 122086f8cdf3SVladimir Sementsov-Ogievskiy */ 1221611ae1d7SVladimir Sementsov-Ogievskiy if (offset >= s->info.size) { 122286f8cdf3SVladimir Sementsov-Ogievskiy assert(bytes < BDRV_SECTOR_SIZE); 122386f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_memset(qiov, 0, 0, bytes); 122486f8cdf3SVladimir Sementsov-Ogievskiy return 0; 122586f8cdf3SVladimir Sementsov-Ogievskiy } 1226611ae1d7SVladimir Sementsov-Ogievskiy if (offset + bytes > s->info.size) { 1227611ae1d7SVladimir Sementsov-Ogievskiy uint64_t slop = offset + bytes - s->info.size; 122886f8cdf3SVladimir Sementsov-Ogievskiy 122986f8cdf3SVladimir Sementsov-Ogievskiy assert(slop < BDRV_SECTOR_SIZE); 123086f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_memset(qiov, bytes - slop, 0, slop); 123186f8cdf3SVladimir Sementsov-Ogievskiy request.len -= slop; 123286f8cdf3SVladimir Sementsov-Ogievskiy } 123386f8cdf3SVladimir Sementsov-Ogievskiy 1234f7651539SVladimir Sementsov-Ogievskiy do { 123586f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_co_send_request(bs, &request, NULL); 123686f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1237f7651539SVladimir Sementsov-Ogievskiy continue; 123886f8cdf3SVladimir Sementsov-Ogievskiy } 123986f8cdf3SVladimir Sementsov-Ogievskiy 1240611ae1d7SVladimir Sementsov-Ogievskiy ret = nbd_co_receive_cmdread_reply(s, request.handle, offset, qiov, 124186f8cdf3SVladimir Sementsov-Ogievskiy &request_ret, &local_err); 124286f8cdf3SVladimir Sementsov-Ogievskiy if (local_err) { 124386f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_co_request_fail(request.from, request.len, request.handle, 124486f8cdf3SVladimir Sementsov-Ogievskiy request.flags, request.type, 124586f8cdf3SVladimir Sementsov-Ogievskiy nbd_cmd_lookup(request.type), 124686f8cdf3SVladimir Sementsov-Ogievskiy ret, error_get_pretty(local_err)); 124786f8cdf3SVladimir Sementsov-Ogievskiy error_free(local_err); 1248f7651539SVladimir Sementsov-Ogievskiy local_err = NULL; 124986f8cdf3SVladimir Sementsov-Ogievskiy } 1250f7651539SVladimir Sementsov-Ogievskiy } while (ret < 0 && nbd_client_connecting_wait(s)); 1251f7651539SVladimir Sementsov-Ogievskiy 125286f8cdf3SVladimir Sementsov-Ogievskiy return ret ? ret : request_ret; 125386f8cdf3SVladimir Sementsov-Ogievskiy } 125486f8cdf3SVladimir Sementsov-Ogievskiy 12550c43c6fcSPaolo Bonzini static int coroutine_fn nbd_client_co_pwritev(BlockDriverState *bs, int64_t offset, 1256e75abedaSVladimir Sementsov-Ogievskiy int64_t bytes, QEMUIOVector *qiov, 1257e75abedaSVladimir Sementsov-Ogievskiy BdrvRequestFlags flags) 125886f8cdf3SVladimir Sementsov-Ogievskiy { 1259611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 126086f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 126186f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_WRITE, 126286f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 126386f8cdf3SVladimir Sementsov-Ogievskiy .len = bytes, 126486f8cdf3SVladimir Sementsov-Ogievskiy }; 126586f8cdf3SVladimir Sementsov-Ogievskiy 1266611ae1d7SVladimir Sementsov-Ogievskiy assert(!(s->info.flags & NBD_FLAG_READ_ONLY)); 126786f8cdf3SVladimir Sementsov-Ogievskiy if (flags & BDRV_REQ_FUA) { 1268611ae1d7SVladimir Sementsov-Ogievskiy assert(s->info.flags & NBD_FLAG_SEND_FUA); 126986f8cdf3SVladimir Sementsov-Ogievskiy request.flags |= NBD_CMD_FLAG_FUA; 127086f8cdf3SVladimir Sementsov-Ogievskiy } 127186f8cdf3SVladimir Sementsov-Ogievskiy 127286f8cdf3SVladimir Sementsov-Ogievskiy assert(bytes <= NBD_MAX_BUFFER_SIZE); 127386f8cdf3SVladimir Sementsov-Ogievskiy 127486f8cdf3SVladimir Sementsov-Ogievskiy if (!bytes) { 127586f8cdf3SVladimir Sementsov-Ogievskiy return 0; 127686f8cdf3SVladimir Sementsov-Ogievskiy } 127786f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_request(bs, &request, qiov); 127886f8cdf3SVladimir Sementsov-Ogievskiy } 127986f8cdf3SVladimir Sementsov-Ogievskiy 12800c43c6fcSPaolo Bonzini static int coroutine_fn nbd_client_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, 1281f34b2bcfSVladimir Sementsov-Ogievskiy int64_t bytes, BdrvRequestFlags flags) 128286f8cdf3SVladimir Sementsov-Ogievskiy { 1283611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 128486f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 128586f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_WRITE_ZEROES, 128686f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 1287f34b2bcfSVladimir Sementsov-Ogievskiy .len = bytes, /* .len is uint32_t actually */ 128886f8cdf3SVladimir Sementsov-Ogievskiy }; 128986f8cdf3SVladimir Sementsov-Ogievskiy 1290f34b2bcfSVladimir Sementsov-Ogievskiy assert(bytes <= UINT32_MAX); /* rely on max_pwrite_zeroes */ 1291f34b2bcfSVladimir Sementsov-Ogievskiy 1292611ae1d7SVladimir Sementsov-Ogievskiy assert(!(s->info.flags & NBD_FLAG_READ_ONLY)); 1293611ae1d7SVladimir Sementsov-Ogievskiy if (!(s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES)) { 129486f8cdf3SVladimir Sementsov-Ogievskiy return -ENOTSUP; 129586f8cdf3SVladimir Sementsov-Ogievskiy } 129686f8cdf3SVladimir Sementsov-Ogievskiy 129786f8cdf3SVladimir Sementsov-Ogievskiy if (flags & BDRV_REQ_FUA) { 1298611ae1d7SVladimir Sementsov-Ogievskiy assert(s->info.flags & NBD_FLAG_SEND_FUA); 129986f8cdf3SVladimir Sementsov-Ogievskiy request.flags |= NBD_CMD_FLAG_FUA; 130086f8cdf3SVladimir Sementsov-Ogievskiy } 130186f8cdf3SVladimir Sementsov-Ogievskiy if (!(flags & BDRV_REQ_MAY_UNMAP)) { 130286f8cdf3SVladimir Sementsov-Ogievskiy request.flags |= NBD_CMD_FLAG_NO_HOLE; 130386f8cdf3SVladimir Sementsov-Ogievskiy } 1304f061656cSEric Blake if (flags & BDRV_REQ_NO_FALLBACK) { 1305f061656cSEric Blake assert(s->info.flags & NBD_FLAG_SEND_FAST_ZERO); 1306f061656cSEric Blake request.flags |= NBD_CMD_FLAG_FAST_ZERO; 1307f061656cSEric Blake } 130886f8cdf3SVladimir Sementsov-Ogievskiy 130986f8cdf3SVladimir Sementsov-Ogievskiy if (!bytes) { 131086f8cdf3SVladimir Sementsov-Ogievskiy return 0; 131186f8cdf3SVladimir Sementsov-Ogievskiy } 131286f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_request(bs, &request, NULL); 131386f8cdf3SVladimir Sementsov-Ogievskiy } 131486f8cdf3SVladimir Sementsov-Ogievskiy 13150c43c6fcSPaolo Bonzini static int coroutine_fn nbd_client_co_flush(BlockDriverState *bs) 131686f8cdf3SVladimir Sementsov-Ogievskiy { 1317611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 131886f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { .type = NBD_CMD_FLUSH }; 131986f8cdf3SVladimir Sementsov-Ogievskiy 1320611ae1d7SVladimir Sementsov-Ogievskiy if (!(s->info.flags & NBD_FLAG_SEND_FLUSH)) { 132186f8cdf3SVladimir Sementsov-Ogievskiy return 0; 132286f8cdf3SVladimir Sementsov-Ogievskiy } 132386f8cdf3SVladimir Sementsov-Ogievskiy 132486f8cdf3SVladimir Sementsov-Ogievskiy request.from = 0; 132586f8cdf3SVladimir Sementsov-Ogievskiy request.len = 0; 132686f8cdf3SVladimir Sementsov-Ogievskiy 132786f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_request(bs, &request, NULL); 132886f8cdf3SVladimir Sementsov-Ogievskiy } 132986f8cdf3SVladimir Sementsov-Ogievskiy 13300c43c6fcSPaolo Bonzini static int coroutine_fn nbd_client_co_pdiscard(BlockDriverState *bs, int64_t offset, 13310c802287SVladimir Sementsov-Ogievskiy int64_t bytes) 133286f8cdf3SVladimir Sementsov-Ogievskiy { 1333611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 133486f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 133586f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_TRIM, 133686f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 13370c802287SVladimir Sementsov-Ogievskiy .len = bytes, /* len is uint32_t */ 133886f8cdf3SVladimir Sementsov-Ogievskiy }; 133986f8cdf3SVladimir Sementsov-Ogievskiy 13400c802287SVladimir Sementsov-Ogievskiy assert(bytes <= UINT32_MAX); /* rely on max_pdiscard */ 13410c802287SVladimir Sementsov-Ogievskiy 1342611ae1d7SVladimir Sementsov-Ogievskiy assert(!(s->info.flags & NBD_FLAG_READ_ONLY)); 1343611ae1d7SVladimir Sementsov-Ogievskiy if (!(s->info.flags & NBD_FLAG_SEND_TRIM) || !bytes) { 134486f8cdf3SVladimir Sementsov-Ogievskiy return 0; 134586f8cdf3SVladimir Sementsov-Ogievskiy } 134686f8cdf3SVladimir Sementsov-Ogievskiy 134786f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_request(bs, &request, NULL); 134886f8cdf3SVladimir Sementsov-Ogievskiy } 134986f8cdf3SVladimir Sementsov-Ogievskiy 135086f8cdf3SVladimir Sementsov-Ogievskiy static int coroutine_fn nbd_client_co_block_status( 135186f8cdf3SVladimir Sementsov-Ogievskiy BlockDriverState *bs, bool want_zero, int64_t offset, int64_t bytes, 135286f8cdf3SVladimir Sementsov-Ogievskiy int64_t *pnum, int64_t *map, BlockDriverState **file) 135386f8cdf3SVladimir Sementsov-Ogievskiy { 135486f8cdf3SVladimir Sementsov-Ogievskiy int ret, request_ret; 135586f8cdf3SVladimir Sementsov-Ogievskiy NBDExtent extent = { 0 }; 1356611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 135786f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 135886f8cdf3SVladimir Sementsov-Ogievskiy 135986f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 136086f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_BLOCK_STATUS, 136186f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 13626bf792b4SVladimir Sementsov-Ogievskiy .len = MIN(QEMU_ALIGN_DOWN(INT_MAX, bs->bl.request_alignment), 1363611ae1d7SVladimir Sementsov-Ogievskiy MIN(bytes, s->info.size - offset)), 136486f8cdf3SVladimir Sementsov-Ogievskiy .flags = NBD_CMD_FLAG_REQ_ONE, 136586f8cdf3SVladimir Sementsov-Ogievskiy }; 136686f8cdf3SVladimir Sementsov-Ogievskiy 1367611ae1d7SVladimir Sementsov-Ogievskiy if (!s->info.base_allocation) { 136886f8cdf3SVladimir Sementsov-Ogievskiy *pnum = bytes; 136986f8cdf3SVladimir Sementsov-Ogievskiy *map = offset; 137086f8cdf3SVladimir Sementsov-Ogievskiy *file = bs; 137186f8cdf3SVladimir Sementsov-Ogievskiy return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; 137286f8cdf3SVladimir Sementsov-Ogievskiy } 137386f8cdf3SVladimir Sementsov-Ogievskiy 137486f8cdf3SVladimir Sementsov-Ogievskiy /* 137586f8cdf3SVladimir Sementsov-Ogievskiy * Work around the fact that the block layer doesn't do 137686f8cdf3SVladimir Sementsov-Ogievskiy * byte-accurate sizing yet - if the status request exceeds the 137786f8cdf3SVladimir Sementsov-Ogievskiy * server's advertised size because the block layer rounded size 137886f8cdf3SVladimir Sementsov-Ogievskiy * up, we truncated the request to the server (above), or are 137986f8cdf3SVladimir Sementsov-Ogievskiy * called on just the hole. 138086f8cdf3SVladimir Sementsov-Ogievskiy */ 1381611ae1d7SVladimir Sementsov-Ogievskiy if (offset >= s->info.size) { 138286f8cdf3SVladimir Sementsov-Ogievskiy *pnum = bytes; 138386f8cdf3SVladimir Sementsov-Ogievskiy assert(bytes < BDRV_SECTOR_SIZE); 138486f8cdf3SVladimir Sementsov-Ogievskiy /* Intentionally don't report offset_valid for the hole */ 138586f8cdf3SVladimir Sementsov-Ogievskiy return BDRV_BLOCK_ZERO; 138686f8cdf3SVladimir Sementsov-Ogievskiy } 138786f8cdf3SVladimir Sementsov-Ogievskiy 1388611ae1d7SVladimir Sementsov-Ogievskiy if (s->info.min_block) { 1389611ae1d7SVladimir Sementsov-Ogievskiy assert(QEMU_IS_ALIGNED(request.len, s->info.min_block)); 139086f8cdf3SVladimir Sementsov-Ogievskiy } 1391f7651539SVladimir Sementsov-Ogievskiy do { 139286f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_co_send_request(bs, &request, NULL); 139386f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1394f7651539SVladimir Sementsov-Ogievskiy continue; 139586f8cdf3SVladimir Sementsov-Ogievskiy } 139686f8cdf3SVladimir Sementsov-Ogievskiy 1397611ae1d7SVladimir Sementsov-Ogievskiy ret = nbd_co_receive_blockstatus_reply(s, request.handle, bytes, 1398f7651539SVladimir Sementsov-Ogievskiy &extent, &request_ret, 1399f7651539SVladimir Sementsov-Ogievskiy &local_err); 140086f8cdf3SVladimir Sementsov-Ogievskiy if (local_err) { 140186f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_co_request_fail(request.from, request.len, request.handle, 140286f8cdf3SVladimir Sementsov-Ogievskiy request.flags, request.type, 140386f8cdf3SVladimir Sementsov-Ogievskiy nbd_cmd_lookup(request.type), 140486f8cdf3SVladimir Sementsov-Ogievskiy ret, error_get_pretty(local_err)); 140586f8cdf3SVladimir Sementsov-Ogievskiy error_free(local_err); 1406f7651539SVladimir Sementsov-Ogievskiy local_err = NULL; 140786f8cdf3SVladimir Sementsov-Ogievskiy } 1408f7651539SVladimir Sementsov-Ogievskiy } while (ret < 0 && nbd_client_connecting_wait(s)); 1409f7651539SVladimir Sementsov-Ogievskiy 141086f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0 || request_ret < 0) { 141186f8cdf3SVladimir Sementsov-Ogievskiy return ret ? ret : request_ret; 141286f8cdf3SVladimir Sementsov-Ogievskiy } 141386f8cdf3SVladimir Sementsov-Ogievskiy 141486f8cdf3SVladimir Sementsov-Ogievskiy assert(extent.length); 141586f8cdf3SVladimir Sementsov-Ogievskiy *pnum = extent.length; 141686f8cdf3SVladimir Sementsov-Ogievskiy *map = offset; 141786f8cdf3SVladimir Sementsov-Ogievskiy *file = bs; 141886f8cdf3SVladimir Sementsov-Ogievskiy return (extent.flags & NBD_STATE_HOLE ? 0 : BDRV_BLOCK_DATA) | 141986f8cdf3SVladimir Sementsov-Ogievskiy (extent.flags & NBD_STATE_ZERO ? BDRV_BLOCK_ZERO : 0) | 142086f8cdf3SVladimir Sementsov-Ogievskiy BDRV_BLOCK_OFFSET_VALID; 142186f8cdf3SVladimir Sementsov-Ogievskiy } 142286f8cdf3SVladimir Sementsov-Ogievskiy 1423e99754b4SMaxim Levitsky static int nbd_client_reopen_prepare(BDRVReopenState *state, 1424e99754b4SMaxim Levitsky BlockReopenQueue *queue, Error **errp) 1425e99754b4SMaxim Levitsky { 1426e99754b4SMaxim Levitsky BDRVNBDState *s = (BDRVNBDState *)state->bs->opaque; 1427e99754b4SMaxim Levitsky 1428e99754b4SMaxim Levitsky if ((state->flags & BDRV_O_RDWR) && (s->info.flags & NBD_FLAG_READ_ONLY)) { 1429e99754b4SMaxim Levitsky error_setg(errp, "Can't reopen read-only NBD mount as read/write"); 1430e99754b4SMaxim Levitsky return -EACCES; 1431e99754b4SMaxim Levitsky } 1432e99754b4SMaxim Levitsky return 0; 1433e99754b4SMaxim Levitsky } 1434e99754b4SMaxim Levitsky 1435fee091cdSLukas Straub static void nbd_yank(void *opaque) 1436fee091cdSLukas Straub { 1437fee091cdSLukas Straub BlockDriverState *bs = opaque; 1438fee091cdSLukas Straub BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 1439fee091cdSLukas Straub 1440fee091cdSLukas Straub qatomic_store_release(&s->state, NBD_CLIENT_QUIT); 144195a078eaSVladimir Sementsov-Ogievskiy qio_channel_shutdown(QIO_CHANNEL(s->ioc), QIO_CHANNEL_SHUTDOWN_BOTH, NULL); 1442fee091cdSLukas Straub } 1443fee091cdSLukas Straub 144486f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_client_close(BlockDriverState *bs) 144586f8cdf3SVladimir Sementsov-Ogievskiy { 1446611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 144786f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { .type = NBD_CMD_DISC }; 144886f8cdf3SVladimir Sementsov-Ogievskiy 1449f7651539SVladimir Sementsov-Ogievskiy if (s->ioc) { 1450611ae1d7SVladimir Sementsov-Ogievskiy nbd_send_request(s->ioc, &request); 1451f7651539SVladimir Sementsov-Ogievskiy } 145286f8cdf3SVladimir Sementsov-Ogievskiy 145386f8cdf3SVladimir Sementsov-Ogievskiy nbd_teardown_connection(bs); 145486f8cdf3SVladimir Sementsov-Ogievskiy } 145586f8cdf3SVladimir Sementsov-Ogievskiy 145686f8cdf3SVladimir Sementsov-Ogievskiy 14578f071c9dSVladimir Sementsov-Ogievskiy /* 14588f071c9dSVladimir Sementsov-Ogievskiy * Parse nbd_open options 14598f071c9dSVladimir Sementsov-Ogievskiy */ 146086f8cdf3SVladimir Sementsov-Ogievskiy 1461f53a1febSKevin Wolf static int nbd_parse_uri(const char *filename, QDict *options) 14621d7d2a9dSPaolo Bonzini { 14631d7d2a9dSPaolo Bonzini URI *uri; 14641d7d2a9dSPaolo Bonzini const char *p; 14651d7d2a9dSPaolo Bonzini QueryParams *qp = NULL; 14661d7d2a9dSPaolo Bonzini int ret = 0; 1467f53a1febSKevin Wolf bool is_unix; 14681d7d2a9dSPaolo Bonzini 14691d7d2a9dSPaolo Bonzini uri = uri_parse(filename); 14701d7d2a9dSPaolo Bonzini if (!uri) { 14711d7d2a9dSPaolo Bonzini return -EINVAL; 14721d7d2a9dSPaolo Bonzini } 14731d7d2a9dSPaolo Bonzini 14741d7d2a9dSPaolo Bonzini /* transport */ 1475f69165a8SMax Reitz if (!g_strcmp0(uri->scheme, "nbd")) { 1476f53a1febSKevin Wolf is_unix = false; 1477f69165a8SMax Reitz } else if (!g_strcmp0(uri->scheme, "nbd+tcp")) { 1478f53a1febSKevin Wolf is_unix = false; 1479f69165a8SMax Reitz } else if (!g_strcmp0(uri->scheme, "nbd+unix")) { 1480f53a1febSKevin Wolf is_unix = true; 14811d7d2a9dSPaolo Bonzini } else { 14821d7d2a9dSPaolo Bonzini ret = -EINVAL; 14831d7d2a9dSPaolo Bonzini goto out; 14841d7d2a9dSPaolo Bonzini } 14851d7d2a9dSPaolo Bonzini 14862485f22fSEric Blake p = uri->path ? uri->path : ""; 14872485f22fSEric Blake if (p[0] == '/') { 14882485f22fSEric Blake p++; 14892485f22fSEric Blake } 14901d7d2a9dSPaolo Bonzini if (p[0]) { 149146f5ac20SEric Blake qdict_put_str(options, "export", p); 14921d7d2a9dSPaolo Bonzini } 14931d7d2a9dSPaolo Bonzini 14941d7d2a9dSPaolo Bonzini qp = query_params_parse(uri->query); 1495f53a1febSKevin Wolf if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) { 14961d7d2a9dSPaolo Bonzini ret = -EINVAL; 14971d7d2a9dSPaolo Bonzini goto out; 14981d7d2a9dSPaolo Bonzini } 14991d7d2a9dSPaolo Bonzini 1500f53a1febSKevin Wolf if (is_unix) { 15011d7d2a9dSPaolo Bonzini /* nbd+unix:///export?socket=path */ 15021d7d2a9dSPaolo Bonzini if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) { 15031d7d2a9dSPaolo Bonzini ret = -EINVAL; 15041d7d2a9dSPaolo Bonzini goto out; 15051d7d2a9dSPaolo Bonzini } 150646f5ac20SEric Blake qdict_put_str(options, "server.type", "unix"); 150746f5ac20SEric Blake qdict_put_str(options, "server.path", qp->p[0].value); 15081d7d2a9dSPaolo Bonzini } else { 150923307908SJán Tomko QString *host; 1510f84d431bSMax Reitz char *port_str; 1511f84d431bSMax Reitz 1512bebbf7faSKevin Wolf /* nbd[+tcp]://host[:port]/export */ 15131d7d2a9dSPaolo Bonzini if (!uri->server) { 15141d7d2a9dSPaolo Bonzini ret = -EINVAL; 15151d7d2a9dSPaolo Bonzini goto out; 15161d7d2a9dSPaolo Bonzini } 1517f17c90beSKevin Wolf 151823307908SJán Tomko /* strip braces from literal IPv6 address */ 151923307908SJán Tomko if (uri->server[0] == '[') { 152023307908SJán Tomko host = qstring_from_substr(uri->server, 1, 1521ba891d68SMarkus Armbruster strlen(uri->server) - 1); 152223307908SJán Tomko } else { 152323307908SJán Tomko host = qstring_from_str(uri->server); 152423307908SJán Tomko } 152523307908SJán Tomko 152646f5ac20SEric Blake qdict_put_str(options, "server.type", "inet"); 15279445673eSMarkus Armbruster qdict_put(options, "server.host", host); 1528f84d431bSMax Reitz 1529f84d431bSMax Reitz port_str = g_strdup_printf("%d", uri->port ?: NBD_DEFAULT_PORT); 153046f5ac20SEric Blake qdict_put_str(options, "server.port", port_str); 1531f53a1febSKevin Wolf g_free(port_str); 15321d7d2a9dSPaolo Bonzini } 15331d7d2a9dSPaolo Bonzini 15341d7d2a9dSPaolo Bonzini out: 15351d7d2a9dSPaolo Bonzini if (qp) { 15361d7d2a9dSPaolo Bonzini query_params_free(qp); 15371d7d2a9dSPaolo Bonzini } 15381d7d2a9dSPaolo Bonzini uri_free(uri); 15391d7d2a9dSPaolo Bonzini return ret; 15401d7d2a9dSPaolo Bonzini } 15411d7d2a9dSPaolo Bonzini 154248c38e0bSMax Reitz static bool nbd_has_filename_options_conflict(QDict *options, Error **errp) 154348c38e0bSMax Reitz { 154448c38e0bSMax Reitz const QDictEntry *e; 154548c38e0bSMax Reitz 154648c38e0bSMax Reitz for (e = qdict_first(options); e; e = qdict_next(options, e)) { 154748c38e0bSMax Reitz if (!strcmp(e->key, "host") || 154848c38e0bSMax Reitz !strcmp(e->key, "port") || 154948c38e0bSMax Reitz !strcmp(e->key, "path") || 1550491d6c7cSMax Reitz !strcmp(e->key, "export") || 1551491d6c7cSMax Reitz strstart(e->key, "server.", NULL)) 155248c38e0bSMax Reitz { 155348c38e0bSMax Reitz error_setg(errp, "Option '%s' cannot be used with a file name", 155448c38e0bSMax Reitz e->key); 155548c38e0bSMax Reitz return true; 155648c38e0bSMax Reitz } 155748c38e0bSMax Reitz } 155848c38e0bSMax Reitz 155948c38e0bSMax Reitz return false; 156048c38e0bSMax Reitz } 156148c38e0bSMax Reitz 15626963a30dSKevin Wolf static void nbd_parse_filename(const char *filename, QDict *options, 15636963a30dSKevin Wolf Error **errp) 1564019d6b8fSAnthony Liguori { 1565df18c04eSEric Blake g_autofree char *file = NULL; 156633897dc7SNick Thomas char *export_name; 156733897dc7SNick Thomas const char *host_spec; 1568019d6b8fSAnthony Liguori const char *unixpath; 1569019d6b8fSAnthony Liguori 157048c38e0bSMax Reitz if (nbd_has_filename_options_conflict(options, errp)) { 1571681e7ad0SKevin Wolf return; 1572681e7ad0SKevin Wolf } 1573681e7ad0SKevin Wolf 15741d7d2a9dSPaolo Bonzini if (strstr(filename, "://")) { 15756963a30dSKevin Wolf int ret = nbd_parse_uri(filename, options); 15766963a30dSKevin Wolf if (ret < 0) { 15776963a30dSKevin Wolf error_setg(errp, "No valid URL specified"); 15786963a30dSKevin Wolf } 15796963a30dSKevin Wolf return; 15801d7d2a9dSPaolo Bonzini } 15811d7d2a9dSPaolo Bonzini 15827267c094SAnthony Liguori file = g_strdup(filename); 15831d45f8b5SLaurent Vivier 158433897dc7SNick Thomas export_name = strstr(file, EN_OPTSTR); 158533897dc7SNick Thomas if (export_name) { 158633897dc7SNick Thomas if (export_name[strlen(EN_OPTSTR)] == 0) { 1587df18c04eSEric Blake return; 15881d45f8b5SLaurent Vivier } 158933897dc7SNick Thomas export_name[0] = 0; /* truncate 'file' */ 159033897dc7SNick Thomas export_name += strlen(EN_OPTSTR); 1591f53a1febSKevin Wolf 159246f5ac20SEric Blake qdict_put_str(options, "export", export_name); 15931d45f8b5SLaurent Vivier } 15941d45f8b5SLaurent Vivier 159533897dc7SNick Thomas /* extract the host_spec - fail if it's not nbd:... */ 159633897dc7SNick Thomas if (!strstart(file, "nbd:", &host_spec)) { 15976963a30dSKevin Wolf error_setg(errp, "File name string for NBD must start with 'nbd:'"); 1598df18c04eSEric Blake return; 15991d45f8b5SLaurent Vivier } 1600019d6b8fSAnthony Liguori 1601f53a1febSKevin Wolf if (!*host_spec) { 1602df18c04eSEric Blake return; 1603f53a1febSKevin Wolf } 1604f53a1febSKevin Wolf 160533897dc7SNick Thomas /* are we a UNIX or TCP socket? */ 160633897dc7SNick Thomas if (strstart(host_spec, "unix:", &unixpath)) { 160746f5ac20SEric Blake qdict_put_str(options, "server.type", "unix"); 160846f5ac20SEric Blake qdict_put_str(options, "server.path", unixpath); 1609019d6b8fSAnthony Liguori } else { 16100785bd7aSMarkus Armbruster InetSocketAddress *addr = g_new(InetSocketAddress, 1); 1611f53a1febSKevin Wolf 16120785bd7aSMarkus Armbruster if (inet_parse(addr, host_spec, errp)) { 16130785bd7aSMarkus Armbruster goto out_inet; 1614f17c90beSKevin Wolf } 1615f53a1febSKevin Wolf 161646f5ac20SEric Blake qdict_put_str(options, "server.type", "inet"); 161746f5ac20SEric Blake qdict_put_str(options, "server.host", addr->host); 161846f5ac20SEric Blake qdict_put_str(options, "server.port", addr->port); 16190785bd7aSMarkus Armbruster out_inet: 1620f53a1febSKevin Wolf qapi_free_InetSocketAddress(addr); 16211d45f8b5SLaurent Vivier } 162233897dc7SNick Thomas } 1623f53a1febSKevin Wolf 1624491d6c7cSMax Reitz static bool nbd_process_legacy_socket_options(QDict *output_options, 1625491d6c7cSMax Reitz QemuOpts *legacy_opts, 1626491d6c7cSMax Reitz Error **errp) 1627f53a1febSKevin Wolf { 1628491d6c7cSMax Reitz const char *path = qemu_opt_get(legacy_opts, "path"); 1629491d6c7cSMax Reitz const char *host = qemu_opt_get(legacy_opts, "host"); 1630491d6c7cSMax Reitz const char *port = qemu_opt_get(legacy_opts, "port"); 1631491d6c7cSMax Reitz const QDictEntry *e; 1632f53a1febSKevin Wolf 1633491d6c7cSMax Reitz if (!path && !host && !port) { 1634491d6c7cSMax Reitz return true; 1635491d6c7cSMax Reitz } 163603504d05SMax Reitz 1637491d6c7cSMax Reitz for (e = qdict_first(output_options); e; e = qdict_next(output_options, e)) 1638491d6c7cSMax Reitz { 1639491d6c7cSMax Reitz if (strstart(e->key, "server.", NULL)) { 1640491d6c7cSMax Reitz error_setg(errp, "Cannot use 'server' and path/host/port at the " 1641491d6c7cSMax Reitz "same time"); 1642491d6c7cSMax Reitz return false; 1643491d6c7cSMax Reitz } 1644491d6c7cSMax Reitz } 1645491d6c7cSMax Reitz 1646491d6c7cSMax Reitz if (path && host) { 164782d73014SMax Reitz error_setg(errp, "path and host may not be used at the same time"); 1648491d6c7cSMax Reitz return false; 1649491d6c7cSMax Reitz } else if (path) { 1650491d6c7cSMax Reitz if (port) { 1651442045cbSMax Reitz error_setg(errp, "port may not be used without host"); 1652491d6c7cSMax Reitz return false; 1653442045cbSMax Reitz } 1654019d6b8fSAnthony Liguori 165546f5ac20SEric Blake qdict_put_str(output_options, "server.type", "unix"); 165646f5ac20SEric Blake qdict_put_str(output_options, "server.path", path); 1657491d6c7cSMax Reitz } else if (host) { 165846f5ac20SEric Blake qdict_put_str(output_options, "server.type", "inet"); 165946f5ac20SEric Blake qdict_put_str(output_options, "server.host", host); 166046f5ac20SEric Blake qdict_put_str(output_options, "server.port", 166146f5ac20SEric Blake port ?: stringify(NBD_DEFAULT_PORT)); 16627a5ed437SDaniel P. Berrange } 1663491d6c7cSMax Reitz 1664491d6c7cSMax Reitz return true; 1665491d6c7cSMax Reitz } 1666491d6c7cSMax Reitz 166762cf396bSMarkus Armbruster static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options, 16689445673eSMarkus Armbruster Error **errp) 1669491d6c7cSMax Reitz { 167062cf396bSMarkus Armbruster SocketAddress *saddr = NULL; 1671491d6c7cSMax Reitz QDict *addr = NULL; 1672491d6c7cSMax Reitz Visitor *iv = NULL; 1673491d6c7cSMax Reitz 1674491d6c7cSMax Reitz qdict_extract_subqdict(options, &addr, "server."); 1675491d6c7cSMax Reitz if (!qdict_size(addr)) { 1676491d6c7cSMax Reitz error_setg(errp, "NBD server address missing"); 1677491d6c7cSMax Reitz goto done; 1678491d6c7cSMax Reitz } 1679491d6c7cSMax Reitz 1680af91062eSMarkus Armbruster iv = qobject_input_visitor_new_flat_confused(addr, errp); 1681af91062eSMarkus Armbruster if (!iv) { 1682491d6c7cSMax Reitz goto done; 1683491d6c7cSMax Reitz } 1684491d6c7cSMax Reitz 1685af175e85SMarkus Armbruster if (!visit_type_SocketAddress(iv, NULL, &saddr, errp)) { 1686491d6c7cSMax Reitz goto done; 1687f53a1febSKevin Wolf } 1688f53a1febSKevin Wolf 16896cc702beSVladimir Sementsov-Ogievskiy if (socket_address_parse_named_fd(saddr, errp) < 0) { 16906cc702beSVladimir Sementsov-Ogievskiy qapi_free_SocketAddress(saddr); 16916cc702beSVladimir Sementsov-Ogievskiy saddr = NULL; 16926cc702beSVladimir Sementsov-Ogievskiy goto done; 16936cc702beSVladimir Sementsov-Ogievskiy } 16946cc702beSVladimir Sementsov-Ogievskiy 1695491d6c7cSMax Reitz done: 1696cb3e7f08SMarc-André Lureau qobject_unref(addr); 1697491d6c7cSMax Reitz visit_free(iv); 16987a5ed437SDaniel P. Berrange return saddr; 1699f53a1febSKevin Wolf } 1700f53a1febSKevin Wolf 170175822a12SDaniel P. Berrange static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp) 170275822a12SDaniel P. Berrange { 170375822a12SDaniel P. Berrange Object *obj; 170475822a12SDaniel P. Berrange QCryptoTLSCreds *creds; 170575822a12SDaniel P. Berrange 170675822a12SDaniel P. Berrange obj = object_resolve_path_component( 170775822a12SDaniel P. Berrange object_get_objects_root(), id); 170875822a12SDaniel P. Berrange if (!obj) { 170975822a12SDaniel P. Berrange error_setg(errp, "No TLS credentials with id '%s'", 171075822a12SDaniel P. Berrange id); 171175822a12SDaniel P. Berrange return NULL; 171275822a12SDaniel P. Berrange } 171375822a12SDaniel P. Berrange creds = (QCryptoTLSCreds *) 171475822a12SDaniel P. Berrange object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS); 171575822a12SDaniel P. Berrange if (!creds) { 171675822a12SDaniel P. Berrange error_setg(errp, "Object with id '%s' is not TLS credentials", 171775822a12SDaniel P. Berrange id); 171875822a12SDaniel P. Berrange return NULL; 171975822a12SDaniel P. Berrange } 172075822a12SDaniel P. Berrange 17217b3b6168SPhilippe Mathieu-Daudé if (!qcrypto_tls_creds_check_endpoint(creds, 17227b3b6168SPhilippe Mathieu-Daudé QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT, 17237b3b6168SPhilippe Mathieu-Daudé errp)) { 172475822a12SDaniel P. Berrange return NULL; 172575822a12SDaniel P. Berrange } 172675822a12SDaniel P. Berrange object_ref(obj); 172775822a12SDaniel P. Berrange return creds; 172875822a12SDaniel P. Berrange } 172975822a12SDaniel P. Berrange 173075822a12SDaniel P. Berrange 17317ccc44fdSMax Reitz static QemuOptsList nbd_runtime_opts = { 17327ccc44fdSMax Reitz .name = "nbd", 17337ccc44fdSMax Reitz .head = QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts.head), 17347ccc44fdSMax Reitz .desc = { 17357ccc44fdSMax Reitz { 17367ccc44fdSMax Reitz .name = "host", 17377ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 17387ccc44fdSMax Reitz .help = "TCP host to connect to", 17397ccc44fdSMax Reitz }, 17407ccc44fdSMax Reitz { 17417ccc44fdSMax Reitz .name = "port", 17427ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 17437ccc44fdSMax Reitz .help = "TCP port to connect to", 17447ccc44fdSMax Reitz }, 17457ccc44fdSMax Reitz { 17467ccc44fdSMax Reitz .name = "path", 17477ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 17487ccc44fdSMax Reitz .help = "Unix socket path to connect to", 17497ccc44fdSMax Reitz }, 17507ccc44fdSMax Reitz { 17517ccc44fdSMax Reitz .name = "export", 17527ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 17537ccc44fdSMax Reitz .help = "Name of the NBD export to open", 17547ccc44fdSMax Reitz }, 17557ccc44fdSMax Reitz { 17567ccc44fdSMax Reitz .name = "tls-creds", 17577ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 17587ccc44fdSMax Reitz .help = "ID of the TLS credentials to use", 17597ccc44fdSMax Reitz }, 1760216ee365SEric Blake { 1761a0cd6d29SDaniel P. Berrangé .name = "tls-hostname", 1762a0cd6d29SDaniel P. Berrangé .type = QEMU_OPT_STRING, 1763a0cd6d29SDaniel P. Berrangé .help = "Override hostname for validating TLS x509 certificate", 1764a0cd6d29SDaniel P. Berrangé }, 1765a0cd6d29SDaniel P. Berrangé { 1766216ee365SEric Blake .name = "x-dirty-bitmap", 1767216ee365SEric Blake .type = QEMU_OPT_STRING, 1768216ee365SEric Blake .help = "experimental: expose named dirty bitmap in place of " 1769216ee365SEric Blake "block status", 1770216ee365SEric Blake }, 1771b172ae2eSVladimir Sementsov-Ogievskiy { 1772b172ae2eSVladimir Sementsov-Ogievskiy .name = "reconnect-delay", 1773b172ae2eSVladimir Sementsov-Ogievskiy .type = QEMU_OPT_NUMBER, 1774b172ae2eSVladimir Sementsov-Ogievskiy .help = "On an unexpected disconnect, the nbd client tries to " 1775b172ae2eSVladimir Sementsov-Ogievskiy "connect again until succeeding or encountering a serious " 1776b172ae2eSVladimir Sementsov-Ogievskiy "error. During the first @reconnect-delay seconds, all " 1777b172ae2eSVladimir Sementsov-Ogievskiy "requests are paused and will be rerun on a successful " 1778b172ae2eSVladimir Sementsov-Ogievskiy "reconnect. After that time, any delayed requests and all " 1779b172ae2eSVladimir Sementsov-Ogievskiy "future requests before a successful reconnect will " 1780b172ae2eSVladimir Sementsov-Ogievskiy "immediately fail. Default 0", 1781b172ae2eSVladimir Sementsov-Ogievskiy }, 1782be16b8bfSVladimir Sementsov-Ogievskiy { 1783be16b8bfSVladimir Sementsov-Ogievskiy .name = "open-timeout", 1784be16b8bfSVladimir Sementsov-Ogievskiy .type = QEMU_OPT_NUMBER, 1785be16b8bfSVladimir Sementsov-Ogievskiy .help = "In seconds. If zero, the nbd driver tries the connection " 1786be16b8bfSVladimir Sementsov-Ogievskiy "only once, and fails to open if the connection fails. " 1787be16b8bfSVladimir Sementsov-Ogievskiy "If non-zero, the nbd driver will repeat connection " 1788be16b8bfSVladimir Sementsov-Ogievskiy "attempts until successful or until @open-timeout seconds " 1789be16b8bfSVladimir Sementsov-Ogievskiy "have elapsed. Default 0", 1790be16b8bfSVladimir Sementsov-Ogievskiy }, 1791c4365735SMurilo Opsfelder Araujo { /* end of list */ } 17927ccc44fdSMax Reitz }, 17937ccc44fdSMax Reitz }; 17947ccc44fdSMax Reitz 17958f071c9dSVladimir Sementsov-Ogievskiy static int nbd_process_options(BlockDriverState *bs, QDict *options, 1796015a1036SMax Reitz Error **errp) 179733897dc7SNick Thomas { 179833897dc7SNick Thomas BDRVNBDState *s = bs->opaque; 17998f071c9dSVladimir Sementsov-Ogievskiy QemuOpts *opts; 180075822a12SDaniel P. Berrange int ret = -EINVAL; 1801ae255e52SPaolo Bonzini 18027ccc44fdSMax Reitz opts = qemu_opts_create(&nbd_runtime_opts, NULL, 0, &error_abort); 1803af175e85SMarkus Armbruster if (!qemu_opts_absorb_qdict(opts, options, errp)) { 18047ccc44fdSMax Reitz goto error; 18057ccc44fdSMax Reitz } 18067ccc44fdSMax Reitz 180762cf396bSMarkus Armbruster /* Translate @host, @port, and @path to a SocketAddress */ 1808491d6c7cSMax Reitz if (!nbd_process_legacy_socket_options(options, opts, errp)) { 180975822a12SDaniel P. Berrange goto error; 181075822a12SDaniel P. Berrange } 181175822a12SDaniel P. Berrange 1812491d6c7cSMax Reitz /* Pop the config into our state object. Exit if invalid. */ 1813491d6c7cSMax Reitz s->saddr = nbd_config(s, options, errp); 1814491d6c7cSMax Reitz if (!s->saddr) { 1815491d6c7cSMax Reitz goto error; 1816491d6c7cSMax Reitz } 1817491d6c7cSMax Reitz 1818491d6c7cSMax Reitz s->export = g_strdup(qemu_opt_get(opts, "export")); 181993676c88SEric Blake if (s->export && strlen(s->export) > NBD_MAX_STRING_SIZE) { 182093676c88SEric Blake error_setg(errp, "export name too long to send to server"); 182193676c88SEric Blake goto error; 182293676c88SEric Blake } 1823491d6c7cSMax Reitz 182403504d05SMax Reitz s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds")); 182503504d05SMax Reitz if (s->tlscredsid) { 18268f071c9dSVladimir Sementsov-Ogievskiy s->tlscreds = nbd_get_tls_creds(s->tlscredsid, errp); 18278f071c9dSVladimir Sementsov-Ogievskiy if (!s->tlscreds) { 182875822a12SDaniel P. Berrange goto error; 182975822a12SDaniel P. Berrange } 183075822a12SDaniel P. Berrange 1831a0cd6d29SDaniel P. Berrangé s->tlshostname = g_strdup(qemu_opt_get(opts, "tls-hostname")); 1832e8ae8b1aSDaniel P. Berrangé if (!s->tlshostname && 1833e8ae8b1aSDaniel P. Berrangé s->saddr->type == SOCKET_ADDRESS_TYPE_INET) { 1834a0cd6d29SDaniel P. Berrangé s->tlshostname = g_strdup(s->saddr->u.inet.host); 1835a0cd6d29SDaniel P. Berrangé } 183633897dc7SNick Thomas } 183733897dc7SNick Thomas 18388f071c9dSVladimir Sementsov-Ogievskiy s->x_dirty_bitmap = g_strdup(qemu_opt_get(opts, "x-dirty-bitmap")); 183993676c88SEric Blake if (s->x_dirty_bitmap && strlen(s->x_dirty_bitmap) > NBD_MAX_STRING_SIZE) { 184093676c88SEric Blake error_setg(errp, "x-dirty-bitmap query too long to send to server"); 184193676c88SEric Blake goto error; 184293676c88SEric Blake } 184393676c88SEric Blake 18448f071c9dSVladimir Sementsov-Ogievskiy s->reconnect_delay = qemu_opt_get_number(opts, "reconnect-delay", 0); 1845be16b8bfSVladimir Sementsov-Ogievskiy s->open_timeout = qemu_opt_get_number(opts, "open-timeout", 0); 18468f071c9dSVladimir Sementsov-Ogievskiy 18478f071c9dSVladimir Sementsov-Ogievskiy ret = 0; 1848d42f78e9SVladimir Sementsov-Ogievskiy 184975822a12SDaniel P. Berrange error: 18507ccc44fdSMax Reitz qemu_opts_del(opts); 185175822a12SDaniel P. Berrange return ret; 1852ae255e52SPaolo Bonzini } 1853d9b09f13SPaolo Bonzini 18548f071c9dSVladimir Sementsov-Ogievskiy static int nbd_open(BlockDriverState *bs, QDict *options, int flags, 18558f071c9dSVladimir Sementsov-Ogievskiy Error **errp) 18568f071c9dSVladimir Sementsov-Ogievskiy { 18578f071c9dSVladimir Sementsov-Ogievskiy int ret; 18588f071c9dSVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 18598f071c9dSVladimir Sementsov-Ogievskiy 18608f071c9dSVladimir Sementsov-Ogievskiy s->bs = bs; 18618f071c9dSVladimir Sementsov-Ogievskiy qemu_co_mutex_init(&s->send_mutex); 18628f071c9dSVladimir Sementsov-Ogievskiy qemu_co_queue_init(&s->free_sema); 18634ddb5d2fSVladimir Sementsov-Ogievskiy qemu_co_mutex_init(&s->receive_mutex); 18648f071c9dSVladimir Sementsov-Ogievskiy 1865fee091cdSLukas Straub if (!yank_register_instance(BLOCKDEV_YANK_INSTANCE(bs->node_name), errp)) { 1866fee091cdSLukas Straub return -EEXIST; 1867fee091cdSLukas Straub } 1868fee091cdSLukas Straub 1869bbba1c37SVladimir Sementsov-Ogievskiy ret = nbd_process_options(bs, options, errp); 1870bbba1c37SVladimir Sementsov-Ogievskiy if (ret < 0) { 1871bbba1c37SVladimir Sementsov-Ogievskiy goto fail; 1872bbba1c37SVladimir Sementsov-Ogievskiy } 1873bbba1c37SVladimir Sementsov-Ogievskiy 18746d2b0332SVladimir Sementsov-Ogievskiy s->conn = nbd_client_connection_new(s->saddr, true, s->export, 1875046f98d0SDaniel P. Berrangé s->x_dirty_bitmap, s->tlscreds, 1876046f98d0SDaniel P. Berrangé s->tlshostname); 1877e8b35bf5SRoman Kagan 1878be16b8bfSVladimir Sementsov-Ogievskiy if (s->open_timeout) { 1879be16b8bfSVladimir Sementsov-Ogievskiy nbd_client_connection_enable_retry(s->conn); 1880be16b8bfSVladimir Sementsov-Ogievskiy open_timer_init(s, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 1881be16b8bfSVladimir Sementsov-Ogievskiy s->open_timeout * NANOSECONDS_PER_SECOND); 1882be16b8bfSVladimir Sementsov-Ogievskiy } 1883be16b8bfSVladimir Sementsov-Ogievskiy 18844ddb5d2fSVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_CONNECTING_WAIT; 1885a71d597bSVladimir Sementsov-Ogievskiy ret = nbd_do_establish_connection(bs, errp); 18868f071c9dSVladimir Sementsov-Ogievskiy if (ret < 0) { 1887bbba1c37SVladimir Sementsov-Ogievskiy goto fail; 18888f071c9dSVladimir Sementsov-Ogievskiy } 18898f071c9dSVladimir Sementsov-Ogievskiy 1890717be964SHanna Reitz /* 1891717be964SHanna Reitz * The connect attempt is done, so we no longer need this timer. 1892717be964SHanna Reitz * Delete it, because we do not want it to be around when this node 1893717be964SHanna Reitz * is drained or closed. 1894717be964SHanna Reitz */ 1895717be964SHanna Reitz open_timer_del(s); 1896717be964SHanna Reitz 18974ddb5d2fSVladimir Sementsov-Ogievskiy nbd_client_connection_enable_retry(s->conn); 18988f071c9dSVladimir Sementsov-Ogievskiy 18998f071c9dSVladimir Sementsov-Ogievskiy return 0; 1900bbba1c37SVladimir Sementsov-Ogievskiy 1901bbba1c37SVladimir Sementsov-Ogievskiy fail: 1902717be964SHanna Reitz open_timer_del(s); 1903bbba1c37SVladimir Sementsov-Ogievskiy nbd_clear_bdrvstate(bs); 1904bbba1c37SVladimir Sementsov-Ogievskiy return ret; 19058f071c9dSVladimir Sementsov-Ogievskiy } 19068f071c9dSVladimir Sementsov-Ogievskiy 19070c43c6fcSPaolo Bonzini static int coroutine_fn nbd_co_flush(BlockDriverState *bs) 19081486d04aSPaolo Bonzini { 1909f53a829bSMax Reitz return nbd_client_co_flush(bs); 19101486d04aSPaolo Bonzini } 19111486d04aSPaolo Bonzini 1912fa21e6faSDenis V. Lunev static void nbd_refresh_limits(BlockDriverState *bs, Error **errp) 1913fa21e6faSDenis V. Lunev { 1914611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 1915fd8d372dSEric Blake uint32_t min = s->info.min_block; 1916081dd1feSEric Blake uint32_t max = MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE, s->info.max_block); 1917081dd1feSEric Blake 19187da537f7SEric Blake /* 19197da537f7SEric Blake * If the server did not advertise an alignment: 19207da537f7SEric Blake * - a size that is not sector-aligned implies that an alignment 19217da537f7SEric Blake * of 1 can be used to access those tail bytes 19227da537f7SEric Blake * - advertisement of block status requires an alignment of 1, so 19237da537f7SEric Blake * that we don't violate block layer constraints that block 19247da537f7SEric Blake * status is always aligned (as we can't control whether the 19257da537f7SEric Blake * server will report sub-sector extents, such as a hole at EOF 19267da537f7SEric Blake * on an unaligned POSIX file) 19277da537f7SEric Blake * - otherwise, assume the server is so old that we are safer avoiding 19287da537f7SEric Blake * sub-sector requests 19297da537f7SEric Blake */ 19307da537f7SEric Blake if (!min) { 19317da537f7SEric Blake min = (!QEMU_IS_ALIGNED(s->info.size, BDRV_SECTOR_SIZE) || 19327da537f7SEric Blake s->info.base_allocation) ? 1 : BDRV_SECTOR_SIZE; 19337da537f7SEric Blake } 19347da537f7SEric Blake 19357da537f7SEric Blake bs->bl.request_alignment = min; 1936714eb0dbSVladimir Sementsov-Ogievskiy bs->bl.max_pdiscard = QEMU_ALIGN_DOWN(INT_MAX, min); 1937081dd1feSEric Blake bs->bl.max_pwrite_zeroes = max; 1938081dd1feSEric Blake bs->bl.max_transfer = max; 1939081dd1feSEric Blake 1940081dd1feSEric Blake if (s->info.opt_block && 1941081dd1feSEric Blake s->info.opt_block > bs->bl.opt_transfer) { 1942081dd1feSEric Blake bs->bl.opt_transfer = s->info.opt_block; 1943081dd1feSEric Blake } 1944fa21e6faSDenis V. Lunev } 1945fa21e6faSDenis V. Lunev 1946019d6b8fSAnthony Liguori static void nbd_close(BlockDriverState *bs) 1947019d6b8fSAnthony Liguori { 1948f53a829bSMax Reitz nbd_client_close(bs); 1949bbba1c37SVladimir Sementsov-Ogievskiy nbd_clear_bdrvstate(bs); 1950019d6b8fSAnthony Liguori } 1951019d6b8fSAnthony Liguori 1952a2b333c0SNir Soffer /* 1953a2b333c0SNir Soffer * NBD cannot truncate, but if the caller asks to truncate to the same size, or 1954a2b333c0SNir Soffer * to a smaller size with exact=false, there is no reason to fail the 1955a2b333c0SNir Soffer * operation. 1956a2b333c0SNir Soffer * 1957a2b333c0SNir Soffer * Preallocation mode is ignored since it does not seems useful to fail when 1958a2b333c0SNir Soffer * we never change anything. 1959a2b333c0SNir Soffer */ 1960a2b333c0SNir Soffer static int coroutine_fn nbd_co_truncate(BlockDriverState *bs, int64_t offset, 1961a2b333c0SNir Soffer bool exact, PreallocMode prealloc, 1962a2b333c0SNir Soffer BdrvRequestFlags flags, Error **errp) 1963a2b333c0SNir Soffer { 1964a2b333c0SNir Soffer BDRVNBDState *s = bs->opaque; 1965a2b333c0SNir Soffer 1966a2b333c0SNir Soffer if (offset != s->info.size && exact) { 1967a2b333c0SNir Soffer error_setg(errp, "Cannot resize NBD nodes"); 1968a2b333c0SNir Soffer return -ENOTSUP; 1969a2b333c0SNir Soffer } 1970a2b333c0SNir Soffer 1971a2b333c0SNir Soffer if (offset > s->info.size) { 1972a2b333c0SNir Soffer error_setg(errp, "Cannot grow NBD nodes"); 1973a2b333c0SNir Soffer return -EINVAL; 1974a2b333c0SNir Soffer } 1975a2b333c0SNir Soffer 1976a2b333c0SNir Soffer return 0; 1977a2b333c0SNir Soffer } 1978a2b333c0SNir Soffer 1979019d6b8fSAnthony Liguori static int64_t nbd_getlength(BlockDriverState *bs) 1980019d6b8fSAnthony Liguori { 1981019d6b8fSAnthony Liguori BDRVNBDState *s = bs->opaque; 1982019d6b8fSAnthony Liguori 1983611ae1d7SVladimir Sementsov-Ogievskiy return s->info.size; 1984019d6b8fSAnthony Liguori } 1985019d6b8fSAnthony Liguori 1986998b3a1eSMax Reitz static void nbd_refresh_filename(BlockDriverState *bs) 19872019d68bSMax Reitz { 198803504d05SMax Reitz BDRVNBDState *s = bs->opaque; 1989491d6c7cSMax Reitz const char *host = NULL, *port = NULL, *path = NULL; 19905c86bdf1SEric Blake size_t len = 0; 1991491d6c7cSMax Reitz 199262cf396bSMarkus Armbruster if (s->saddr->type == SOCKET_ADDRESS_TYPE_INET) { 19939445673eSMarkus Armbruster const InetSocketAddress *inet = &s->saddr->u.inet; 1994491d6c7cSMax Reitz if (!inet->has_ipv4 && !inet->has_ipv6 && !inet->has_to) { 1995491d6c7cSMax Reitz host = inet->host; 1996491d6c7cSMax Reitz port = inet->port; 1997491d6c7cSMax Reitz } 199862cf396bSMarkus Armbruster } else if (s->saddr->type == SOCKET_ADDRESS_TYPE_UNIX) { 19999445673eSMarkus Armbruster path = s->saddr->u.q_unix.path; 20009445673eSMarkus Armbruster } /* else can't represent as pseudo-filename */ 20012019d68bSMax Reitz 2002491d6c7cSMax Reitz if (path && s->export) { 20035c86bdf1SEric Blake len = snprintf(bs->exact_filename, sizeof(bs->exact_filename), 2004491d6c7cSMax Reitz "nbd+unix:///%s?socket=%s", s->export, path); 2005491d6c7cSMax Reitz } else if (path && !s->export) { 20065c86bdf1SEric Blake len = snprintf(bs->exact_filename, sizeof(bs->exact_filename), 2007491d6c7cSMax Reitz "nbd+unix://?socket=%s", path); 2008491d6c7cSMax Reitz } else if (host && s->export) { 20095c86bdf1SEric Blake len = snprintf(bs->exact_filename, sizeof(bs->exact_filename), 2010491d6c7cSMax Reitz "nbd://%s:%s/%s", host, port, s->export); 2011491d6c7cSMax Reitz } else if (host && !s->export) { 20125c86bdf1SEric Blake len = snprintf(bs->exact_filename, sizeof(bs->exact_filename), 2013491d6c7cSMax Reitz "nbd://%s:%s", host, port); 2014ec0de768SMax Reitz } 201500d69986SEric Blake if (len >= sizeof(bs->exact_filename)) { 20165c86bdf1SEric Blake /* Name is too long to represent exactly, so leave it empty. */ 20175c86bdf1SEric Blake bs->exact_filename[0] = '\0'; 20185c86bdf1SEric Blake } 20192019d68bSMax Reitz } 20202019d68bSMax Reitz 20218a6239c0SMax Reitz static char *nbd_dirname(BlockDriverState *bs, Error **errp) 20228a6239c0SMax Reitz { 20238a6239c0SMax Reitz /* The generic bdrv_dirname() implementation is able to work out some 20248a6239c0SMax Reitz * directory name for NBD nodes, but that would be wrong. So far there is no 20258a6239c0SMax Reitz * specification for how "export paths" would work, so NBD does not have 20268a6239c0SMax Reitz * directory names. */ 20278a6239c0SMax Reitz error_setg(errp, "Cannot generate a base directory for NBD nodes"); 20288a6239c0SMax Reitz return NULL; 20298a6239c0SMax Reitz } 20308a6239c0SMax Reitz 20312654267cSMax Reitz static const char *const nbd_strong_runtime_opts[] = { 20322654267cSMax Reitz "path", 20332654267cSMax Reitz "host", 20342654267cSMax Reitz "port", 20352654267cSMax Reitz "export", 20362654267cSMax Reitz "tls-creds", 2037a0cd6d29SDaniel P. Berrangé "tls-hostname", 20382654267cSMax Reitz "server.", 20392654267cSMax Reitz 20402654267cSMax Reitz NULL 20412654267cSMax Reitz }; 20422654267cSMax Reitz 2043c4f7f24eSVladimir Sementsov-Ogievskiy static void nbd_cancel_in_flight(BlockDriverState *bs) 2044c4f7f24eSVladimir Sementsov-Ogievskiy { 2045c4f7f24eSVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 2046c4f7f24eSVladimir Sementsov-Ogievskiy 2047c4f7f24eSVladimir Sementsov-Ogievskiy reconnect_delay_timer_del(s); 2048c4f7f24eSVladimir Sementsov-Ogievskiy 2049c4f7f24eSVladimir Sementsov-Ogievskiy if (s->state == NBD_CLIENT_CONNECTING_WAIT) { 2050c4f7f24eSVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_CONNECTING_NOWAIT; 2051c4f7f24eSVladimir Sementsov-Ogievskiy qemu_co_queue_restart_all(&s->free_sema); 2052c4f7f24eSVladimir Sementsov-Ogievskiy } 20534ddb5d2fSVladimir Sementsov-Ogievskiy 20544ddb5d2fSVladimir Sementsov-Ogievskiy nbd_co_establish_connection_cancel(s->conn); 2055c4f7f24eSVladimir Sementsov-Ogievskiy } 2056c4f7f24eSVladimir Sementsov-Ogievskiy 2057e15f3a66SHanna Reitz static void nbd_attach_aio_context(BlockDriverState *bs, 2058e15f3a66SHanna Reitz AioContext *new_context) 2059e15f3a66SHanna Reitz { 2060e15f3a66SHanna Reitz BDRVNBDState *s = bs->opaque; 2061e15f3a66SHanna Reitz 2062e15f3a66SHanna Reitz /* The open_timer is used only during nbd_open() */ 2063e15f3a66SHanna Reitz assert(!s->open_timer); 2064e15f3a66SHanna Reitz 2065e15f3a66SHanna Reitz /* 2066e15f3a66SHanna Reitz * The reconnect_delay_timer is scheduled in I/O paths when the 2067e15f3a66SHanna Reitz * connection is lost, to cancel the reconnection attempt after a 2068e15f3a66SHanna Reitz * given time. Once this attempt is done (successfully or not), 2069e15f3a66SHanna Reitz * nbd_reconnect_attempt() ensures the timer is deleted before the 2070e15f3a66SHanna Reitz * respective I/O request is resumed. 2071e15f3a66SHanna Reitz * Since the AioContext can only be changed when a node is drained, 2072e15f3a66SHanna Reitz * the reconnect_delay_timer cannot be active here. 2073e15f3a66SHanna Reitz */ 2074e15f3a66SHanna Reitz assert(!s->reconnect_delay_timer); 2075e15f3a66SHanna Reitz 2076e15f3a66SHanna Reitz if (s->ioc) { 2077e15f3a66SHanna Reitz qio_channel_attach_aio_context(s->ioc, new_context); 2078e15f3a66SHanna Reitz } 2079e15f3a66SHanna Reitz } 2080e15f3a66SHanna Reitz 2081e15f3a66SHanna Reitz static void nbd_detach_aio_context(BlockDriverState *bs) 2082e15f3a66SHanna Reitz { 2083e15f3a66SHanna Reitz BDRVNBDState *s = bs->opaque; 2084e15f3a66SHanna Reitz 2085e15f3a66SHanna Reitz assert(!s->open_timer); 2086e15f3a66SHanna Reitz assert(!s->reconnect_delay_timer); 2087e15f3a66SHanna Reitz 2088e15f3a66SHanna Reitz if (s->ioc) { 2089e15f3a66SHanna Reitz qio_channel_detach_aio_context(s->ioc); 2090e15f3a66SHanna Reitz } 2091e15f3a66SHanna Reitz } 2092e15f3a66SHanna Reitz 2093019d6b8fSAnthony Liguori static BlockDriver bdrv_nbd = { 2094019d6b8fSAnthony Liguori .format_name = "nbd", 20951d7d2a9dSPaolo Bonzini .protocol_name = "nbd", 2096019d6b8fSAnthony Liguori .instance_size = sizeof(BDRVNBDState), 20976963a30dSKevin Wolf .bdrv_parse_filename = nbd_parse_filename, 20985a5e7f8cSMaxim Levitsky .bdrv_co_create_opts = bdrv_co_create_opts_simple, 20995a5e7f8cSMaxim Levitsky .create_opts = &bdrv_create_opts_simple, 210066f82ceeSKevin Wolf .bdrv_file_open = nbd_open, 2101e99754b4SMaxim Levitsky .bdrv_reopen_prepare = nbd_client_reopen_prepare, 210270c4fb26SEric Blake .bdrv_co_preadv = nbd_client_co_preadv, 210370c4fb26SEric Blake .bdrv_co_pwritev = nbd_client_co_pwritev, 2104fa778fffSEric Blake .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes, 2105019d6b8fSAnthony Liguori .bdrv_close = nbd_close, 21061486d04aSPaolo Bonzini .bdrv_co_flush_to_os = nbd_co_flush, 2107447e57c3SEric Blake .bdrv_co_pdiscard = nbd_client_co_pdiscard, 2108fa21e6faSDenis V. Lunev .bdrv_refresh_limits = nbd_refresh_limits, 2109a2b333c0SNir Soffer .bdrv_co_truncate = nbd_co_truncate, 2110019d6b8fSAnthony Liguori .bdrv_getlength = nbd_getlength, 21112019d68bSMax Reitz .bdrv_refresh_filename = nbd_refresh_filename, 211278a33ab5SVladimir Sementsov-Ogievskiy .bdrv_co_block_status = nbd_client_co_block_status, 21138a6239c0SMax Reitz .bdrv_dirname = nbd_dirname, 21142654267cSMax Reitz .strong_runtime_opts = nbd_strong_runtime_opts, 2115c4f7f24eSVladimir Sementsov-Ogievskiy .bdrv_cancel_in_flight = nbd_cancel_in_flight, 2116e15f3a66SHanna Reitz 2117e15f3a66SHanna Reitz .bdrv_attach_aio_context = nbd_attach_aio_context, 2118e15f3a66SHanna Reitz .bdrv_detach_aio_context = nbd_detach_aio_context, 21191d7d2a9dSPaolo Bonzini }; 21201d7d2a9dSPaolo Bonzini 21211d7d2a9dSPaolo Bonzini static BlockDriver bdrv_nbd_tcp = { 21221d7d2a9dSPaolo Bonzini .format_name = "nbd", 21231d7d2a9dSPaolo Bonzini .protocol_name = "nbd+tcp", 21241d7d2a9dSPaolo Bonzini .instance_size = sizeof(BDRVNBDState), 21256963a30dSKevin Wolf .bdrv_parse_filename = nbd_parse_filename, 21265a5e7f8cSMaxim Levitsky .bdrv_co_create_opts = bdrv_co_create_opts_simple, 21275a5e7f8cSMaxim Levitsky .create_opts = &bdrv_create_opts_simple, 21281d7d2a9dSPaolo Bonzini .bdrv_file_open = nbd_open, 2129e99754b4SMaxim Levitsky .bdrv_reopen_prepare = nbd_client_reopen_prepare, 213070c4fb26SEric Blake .bdrv_co_preadv = nbd_client_co_preadv, 213170c4fb26SEric Blake .bdrv_co_pwritev = nbd_client_co_pwritev, 2132fa778fffSEric Blake .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes, 21331d7d2a9dSPaolo Bonzini .bdrv_close = nbd_close, 21341d7d2a9dSPaolo Bonzini .bdrv_co_flush_to_os = nbd_co_flush, 2135447e57c3SEric Blake .bdrv_co_pdiscard = nbd_client_co_pdiscard, 2136fa21e6faSDenis V. Lunev .bdrv_refresh_limits = nbd_refresh_limits, 2137a2b333c0SNir Soffer .bdrv_co_truncate = nbd_co_truncate, 21381d7d2a9dSPaolo Bonzini .bdrv_getlength = nbd_getlength, 21392019d68bSMax Reitz .bdrv_refresh_filename = nbd_refresh_filename, 214078a33ab5SVladimir Sementsov-Ogievskiy .bdrv_co_block_status = nbd_client_co_block_status, 21418a6239c0SMax Reitz .bdrv_dirname = nbd_dirname, 21422654267cSMax Reitz .strong_runtime_opts = nbd_strong_runtime_opts, 2143c4f7f24eSVladimir Sementsov-Ogievskiy .bdrv_cancel_in_flight = nbd_cancel_in_flight, 2144e15f3a66SHanna Reitz 2145e15f3a66SHanna Reitz .bdrv_attach_aio_context = nbd_attach_aio_context, 2146e15f3a66SHanna Reitz .bdrv_detach_aio_context = nbd_detach_aio_context, 21471d7d2a9dSPaolo Bonzini }; 21481d7d2a9dSPaolo Bonzini 21491d7d2a9dSPaolo Bonzini static BlockDriver bdrv_nbd_unix = { 21501d7d2a9dSPaolo Bonzini .format_name = "nbd", 21511d7d2a9dSPaolo Bonzini .protocol_name = "nbd+unix", 21521d7d2a9dSPaolo Bonzini .instance_size = sizeof(BDRVNBDState), 21536963a30dSKevin Wolf .bdrv_parse_filename = nbd_parse_filename, 21545a5e7f8cSMaxim Levitsky .bdrv_co_create_opts = bdrv_co_create_opts_simple, 21555a5e7f8cSMaxim Levitsky .create_opts = &bdrv_create_opts_simple, 21561d7d2a9dSPaolo Bonzini .bdrv_file_open = nbd_open, 2157e99754b4SMaxim Levitsky .bdrv_reopen_prepare = nbd_client_reopen_prepare, 215870c4fb26SEric Blake .bdrv_co_preadv = nbd_client_co_preadv, 215970c4fb26SEric Blake .bdrv_co_pwritev = nbd_client_co_pwritev, 2160fa778fffSEric Blake .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes, 21611d7d2a9dSPaolo Bonzini .bdrv_close = nbd_close, 21621d7d2a9dSPaolo Bonzini .bdrv_co_flush_to_os = nbd_co_flush, 2163447e57c3SEric Blake .bdrv_co_pdiscard = nbd_client_co_pdiscard, 2164fa21e6faSDenis V. Lunev .bdrv_refresh_limits = nbd_refresh_limits, 2165a2b333c0SNir Soffer .bdrv_co_truncate = nbd_co_truncate, 21661d7d2a9dSPaolo Bonzini .bdrv_getlength = nbd_getlength, 21672019d68bSMax Reitz .bdrv_refresh_filename = nbd_refresh_filename, 216878a33ab5SVladimir Sementsov-Ogievskiy .bdrv_co_block_status = nbd_client_co_block_status, 21698a6239c0SMax Reitz .bdrv_dirname = nbd_dirname, 21702654267cSMax Reitz .strong_runtime_opts = nbd_strong_runtime_opts, 2171c4f7f24eSVladimir Sementsov-Ogievskiy .bdrv_cancel_in_flight = nbd_cancel_in_flight, 2172e15f3a66SHanna Reitz 2173e15f3a66SHanna Reitz .bdrv_attach_aio_context = nbd_attach_aio_context, 2174e15f3a66SHanna Reitz .bdrv_detach_aio_context = nbd_detach_aio_context, 2175019d6b8fSAnthony Liguori }; 2176019d6b8fSAnthony Liguori 2177019d6b8fSAnthony Liguori static void bdrv_nbd_init(void) 2178019d6b8fSAnthony Liguori { 2179019d6b8fSAnthony Liguori bdrv_register(&bdrv_nbd); 21801d7d2a9dSPaolo Bonzini bdrv_register(&bdrv_nbd_tcp); 21811d7d2a9dSPaolo Bonzini bdrv_register(&bdrv_nbd_unix); 2182019d6b8fSAnthony Liguori } 2183019d6b8fSAnthony Liguori 2184019d6b8fSAnthony Liguori block_init(bdrv_nbd_init); 2185