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" 38019d6b8fSAnthony Liguori 3986f8cdf3SVladimir Sementsov-Ogievskiy #include "qapi/qapi-visit-sockets.h" 4086f8cdf3SVladimir Sementsov-Ogievskiy #include "qapi/qmp/qstring.h" 4186f8cdf3SVladimir Sementsov-Ogievskiy 4286f8cdf3SVladimir Sementsov-Ogievskiy #include "block/qdict.h" 4386f8cdf3SVladimir Sementsov-Ogievskiy #include "block/nbd.h" 4486f8cdf3SVladimir Sementsov-Ogievskiy #include "block/block_int.h" 4586f8cdf3SVladimir Sementsov-Ogievskiy 461d45f8b5SLaurent Vivier #define EN_OPTSTR ":exportname=" 4786f8cdf3SVladimir Sementsov-Ogievskiy #define MAX_NBD_REQUESTS 16 4886f8cdf3SVladimir Sementsov-Ogievskiy 4986f8cdf3SVladimir Sementsov-Ogievskiy #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ (uint64_t)(intptr_t)(bs)) 5086f8cdf3SVladimir Sementsov-Ogievskiy #define INDEX_TO_HANDLE(bs, index) ((index) ^ (uint64_t)(intptr_t)(bs)) 5186f8cdf3SVladimir Sementsov-Ogievskiy 5286f8cdf3SVladimir Sementsov-Ogievskiy typedef struct { 5386f8cdf3SVladimir Sementsov-Ogievskiy Coroutine *coroutine; 5486f8cdf3SVladimir Sementsov-Ogievskiy uint64_t offset; /* original offset of the request */ 5586f8cdf3SVladimir Sementsov-Ogievskiy bool receiving; /* waiting for connection_co? */ 5686f8cdf3SVladimir Sementsov-Ogievskiy } NBDClientRequest; 5786f8cdf3SVladimir Sementsov-Ogievskiy 58a34b1e5eSVladimir Sementsov-Ogievskiy typedef enum NBDClientState { 59f7651539SVladimir Sementsov-Ogievskiy NBD_CLIENT_CONNECTING_WAIT, 60f7651539SVladimir Sementsov-Ogievskiy NBD_CLIENT_CONNECTING_NOWAIT, 61a34b1e5eSVladimir Sementsov-Ogievskiy NBD_CLIENT_CONNECTED, 62a34b1e5eSVladimir Sementsov-Ogievskiy NBD_CLIENT_QUIT 63a34b1e5eSVladimir Sementsov-Ogievskiy } NBDClientState; 64a34b1e5eSVladimir Sementsov-Ogievskiy 65611ae1d7SVladimir Sementsov-Ogievskiy typedef struct BDRVNBDState { 6686f8cdf3SVladimir Sementsov-Ogievskiy QIOChannelSocket *sioc; /* The master data channel */ 6786f8cdf3SVladimir Sementsov-Ogievskiy QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */ 6886f8cdf3SVladimir Sementsov-Ogievskiy NBDExportInfo info; 6986f8cdf3SVladimir Sementsov-Ogievskiy 7086f8cdf3SVladimir Sementsov-Ogievskiy CoMutex send_mutex; 7186f8cdf3SVladimir Sementsov-Ogievskiy CoQueue free_sema; 7286f8cdf3SVladimir Sementsov-Ogievskiy Coroutine *connection_co; 7378c81a3fSMax Reitz Coroutine *teardown_co; 74f7651539SVladimir Sementsov-Ogievskiy QemuCoSleepState *connection_co_sleep_ns_state; 75f7651539SVladimir Sementsov-Ogievskiy bool drained; 76f7651539SVladimir Sementsov-Ogievskiy bool wait_drained_end; 7786f8cdf3SVladimir Sementsov-Ogievskiy int in_flight; 78a34b1e5eSVladimir Sementsov-Ogievskiy NBDClientState state; 79f7651539SVladimir Sementsov-Ogievskiy int connect_status; 80f7651539SVladimir Sementsov-Ogievskiy Error *connect_err; 81f7651539SVladimir Sementsov-Ogievskiy bool wait_in_flight; 8286f8cdf3SVladimir Sementsov-Ogievskiy 8386f8cdf3SVladimir Sementsov-Ogievskiy NBDClientRequest requests[MAX_NBD_REQUESTS]; 8486f8cdf3SVladimir Sementsov-Ogievskiy NBDReply reply; 8586f8cdf3SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 8603504d05SMax Reitz 878f071c9dSVladimir Sementsov-Ogievskiy /* Connection parameters */ 888f071c9dSVladimir Sementsov-Ogievskiy uint32_t reconnect_delay; 8962cf396bSMarkus Armbruster SocketAddress *saddr; 90491d6c7cSMax Reitz char *export, *tlscredsid; 918f071c9dSVladimir Sementsov-Ogievskiy QCryptoTLSCreds *tlscreds; 928f071c9dSVladimir Sementsov-Ogievskiy const char *hostname; 938f071c9dSVladimir Sementsov-Ogievskiy char *x_dirty_bitmap; 94019d6b8fSAnthony Liguori } BDRVNBDState; 95019d6b8fSAnthony Liguori 96fa35591bSVladimir Sementsov-Ogievskiy static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr, 97fa35591bSVladimir Sementsov-Ogievskiy Error **errp); 98fa35591bSVladimir Sementsov-Ogievskiy static int nbd_client_handshake(BlockDriverState *bs, QIOChannelSocket *sioc, 99fa35591bSVladimir Sementsov-Ogievskiy Error **errp); 100f7651539SVladimir Sementsov-Ogievskiy 1017f493662SPan Nengyuan static void nbd_clear_bdrvstate(BDRVNBDState *s) 1027f493662SPan Nengyuan { 1037f493662SPan Nengyuan object_unref(OBJECT(s->tlscreds)); 1047f493662SPan Nengyuan qapi_free_SocketAddress(s->saddr); 1057f493662SPan Nengyuan s->saddr = NULL; 1067f493662SPan Nengyuan g_free(s->export); 1077f493662SPan Nengyuan s->export = NULL; 1087f493662SPan Nengyuan g_free(s->tlscredsid); 1097f493662SPan Nengyuan s->tlscredsid = NULL; 1107f493662SPan Nengyuan g_free(s->x_dirty_bitmap); 1117f493662SPan Nengyuan s->x_dirty_bitmap = NULL; 1127f493662SPan Nengyuan } 1137f493662SPan Nengyuan 114a34b1e5eSVladimir Sementsov-Ogievskiy static void nbd_channel_error(BDRVNBDState *s, int ret) 115a34b1e5eSVladimir Sementsov-Ogievskiy { 116f7651539SVladimir Sementsov-Ogievskiy if (ret == -EIO) { 117f7651539SVladimir Sementsov-Ogievskiy if (s->state == NBD_CLIENT_CONNECTED) { 118f7651539SVladimir Sementsov-Ogievskiy s->state = s->reconnect_delay ? NBD_CLIENT_CONNECTING_WAIT : 119f7651539SVladimir Sementsov-Ogievskiy NBD_CLIENT_CONNECTING_NOWAIT; 120f7651539SVladimir Sementsov-Ogievskiy } 121f7651539SVladimir Sementsov-Ogievskiy } else { 122f7651539SVladimir Sementsov-Ogievskiy if (s->state == NBD_CLIENT_CONNECTED) { 123f7651539SVladimir Sementsov-Ogievskiy qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); 124f7651539SVladimir Sementsov-Ogievskiy } 125a34b1e5eSVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_QUIT; 126a34b1e5eSVladimir Sementsov-Ogievskiy } 127f7651539SVladimir Sementsov-Ogievskiy } 128a34b1e5eSVladimir Sementsov-Ogievskiy 129611ae1d7SVladimir Sementsov-Ogievskiy static void nbd_recv_coroutines_wake_all(BDRVNBDState *s) 13086f8cdf3SVladimir Sementsov-Ogievskiy { 13186f8cdf3SVladimir Sementsov-Ogievskiy int i; 13286f8cdf3SVladimir Sementsov-Ogievskiy 13386f8cdf3SVladimir Sementsov-Ogievskiy for (i = 0; i < MAX_NBD_REQUESTS; i++) { 13486f8cdf3SVladimir Sementsov-Ogievskiy NBDClientRequest *req = &s->requests[i]; 13586f8cdf3SVladimir Sementsov-Ogievskiy 13686f8cdf3SVladimir Sementsov-Ogievskiy if (req->coroutine && req->receiving) { 13786f8cdf3SVladimir Sementsov-Ogievskiy aio_co_wake(req->coroutine); 13886f8cdf3SVladimir Sementsov-Ogievskiy } 13986f8cdf3SVladimir Sementsov-Ogievskiy } 14086f8cdf3SVladimir Sementsov-Ogievskiy } 14186f8cdf3SVladimir Sementsov-Ogievskiy 14286f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_client_detach_aio_context(BlockDriverState *bs) 14386f8cdf3SVladimir Sementsov-Ogievskiy { 144611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 145611ae1d7SVladimir Sementsov-Ogievskiy 146611ae1d7SVladimir Sementsov-Ogievskiy qio_channel_detach_aio_context(QIO_CHANNEL(s->ioc)); 14786f8cdf3SVladimir Sementsov-Ogievskiy } 14886f8cdf3SVladimir Sementsov-Ogievskiy 14986f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_client_attach_aio_context_bh(void *opaque) 15086f8cdf3SVladimir Sementsov-Ogievskiy { 15186f8cdf3SVladimir Sementsov-Ogievskiy BlockDriverState *bs = opaque; 152611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 15386f8cdf3SVladimir Sementsov-Ogievskiy 15486f8cdf3SVladimir Sementsov-Ogievskiy /* 15586f8cdf3SVladimir Sementsov-Ogievskiy * The node is still drained, so we know the coroutine has yielded in 15686f8cdf3SVladimir Sementsov-Ogievskiy * nbd_read_eof(), the only place where bs->in_flight can reach 0, or it is 15786f8cdf3SVladimir Sementsov-Ogievskiy * entered for the first time. Both places are safe for entering the 15886f8cdf3SVladimir Sementsov-Ogievskiy * coroutine. 15986f8cdf3SVladimir Sementsov-Ogievskiy */ 160611ae1d7SVladimir Sementsov-Ogievskiy qemu_aio_coroutine_enter(bs->aio_context, s->connection_co); 16186f8cdf3SVladimir Sementsov-Ogievskiy bdrv_dec_in_flight(bs); 16286f8cdf3SVladimir Sementsov-Ogievskiy } 16386f8cdf3SVladimir Sementsov-Ogievskiy 16486f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_client_attach_aio_context(BlockDriverState *bs, 16586f8cdf3SVladimir Sementsov-Ogievskiy AioContext *new_context) 16686f8cdf3SVladimir Sementsov-Ogievskiy { 167611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 168611ae1d7SVladimir Sementsov-Ogievskiy 169f7651539SVladimir Sementsov-Ogievskiy /* 170f7651539SVladimir Sementsov-Ogievskiy * s->connection_co is either yielded from nbd_receive_reply or from 171f7651539SVladimir Sementsov-Ogievskiy * nbd_co_reconnect_loop() 172f7651539SVladimir Sementsov-Ogievskiy */ 173f7651539SVladimir Sementsov-Ogievskiy if (s->state == NBD_CLIENT_CONNECTED) { 174611ae1d7SVladimir Sementsov-Ogievskiy qio_channel_attach_aio_context(QIO_CHANNEL(s->ioc), new_context); 175f7651539SVladimir Sementsov-Ogievskiy } 17686f8cdf3SVladimir Sementsov-Ogievskiy 17786f8cdf3SVladimir Sementsov-Ogievskiy bdrv_inc_in_flight(bs); 17886f8cdf3SVladimir Sementsov-Ogievskiy 17986f8cdf3SVladimir Sementsov-Ogievskiy /* 18086f8cdf3SVladimir Sementsov-Ogievskiy * Need to wait here for the BH to run because the BH must run while the 18186f8cdf3SVladimir Sementsov-Ogievskiy * node is still drained. 18286f8cdf3SVladimir Sementsov-Ogievskiy */ 18386f8cdf3SVladimir Sementsov-Ogievskiy aio_wait_bh_oneshot(new_context, nbd_client_attach_aio_context_bh, bs); 18486f8cdf3SVladimir Sementsov-Ogievskiy } 18586f8cdf3SVladimir Sementsov-Ogievskiy 186f7651539SVladimir Sementsov-Ogievskiy static void coroutine_fn nbd_client_co_drain_begin(BlockDriverState *bs) 187f7651539SVladimir Sementsov-Ogievskiy { 188f7651539SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 189f7651539SVladimir Sementsov-Ogievskiy 190f7651539SVladimir Sementsov-Ogievskiy s->drained = true; 191f7651539SVladimir Sementsov-Ogievskiy if (s->connection_co_sleep_ns_state) { 192f7651539SVladimir Sementsov-Ogievskiy qemu_co_sleep_wake(s->connection_co_sleep_ns_state); 193f7651539SVladimir Sementsov-Ogievskiy } 194f7651539SVladimir Sementsov-Ogievskiy } 195f7651539SVladimir Sementsov-Ogievskiy 196f7651539SVladimir Sementsov-Ogievskiy static void coroutine_fn nbd_client_co_drain_end(BlockDriverState *bs) 197f7651539SVladimir Sementsov-Ogievskiy { 198f7651539SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 199f7651539SVladimir Sementsov-Ogievskiy 200f7651539SVladimir Sementsov-Ogievskiy s->drained = false; 201f7651539SVladimir Sementsov-Ogievskiy if (s->wait_drained_end) { 202f7651539SVladimir Sementsov-Ogievskiy s->wait_drained_end = false; 203f7651539SVladimir Sementsov-Ogievskiy aio_co_wake(s->connection_co); 204f7651539SVladimir Sementsov-Ogievskiy } 205f7651539SVladimir Sementsov-Ogievskiy } 206f7651539SVladimir Sementsov-Ogievskiy 20786f8cdf3SVladimir Sementsov-Ogievskiy 20886f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_teardown_connection(BlockDriverState *bs) 20986f8cdf3SVladimir Sementsov-Ogievskiy { 210611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 21186f8cdf3SVladimir Sementsov-Ogievskiy 212f7651539SVladimir Sementsov-Ogievskiy if (s->state == NBD_CLIENT_CONNECTED) { 21386f8cdf3SVladimir Sementsov-Ogievskiy /* finish any pending coroutines */ 214f7651539SVladimir Sementsov-Ogievskiy assert(s->ioc); 215f7651539SVladimir Sementsov-Ogievskiy qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); 216f7651539SVladimir Sementsov-Ogievskiy } 217f7651539SVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_QUIT; 218f7651539SVladimir Sementsov-Ogievskiy if (s->connection_co) { 219f7651539SVladimir Sementsov-Ogievskiy if (s->connection_co_sleep_ns_state) { 220f7651539SVladimir Sementsov-Ogievskiy qemu_co_sleep_wake(s->connection_co_sleep_ns_state); 221f7651539SVladimir Sementsov-Ogievskiy } 222f7651539SVladimir Sementsov-Ogievskiy } 22378c81a3fSMax Reitz if (qemu_in_coroutine()) { 22478c81a3fSMax Reitz s->teardown_co = qemu_coroutine_self(); 22578c81a3fSMax Reitz /* connection_co resumes us when it terminates */ 22678c81a3fSMax Reitz qemu_coroutine_yield(); 22778c81a3fSMax Reitz s->teardown_co = NULL; 22878c81a3fSMax Reitz } else { 229611ae1d7SVladimir Sementsov-Ogievskiy BDRV_POLL_WHILE(bs, s->connection_co); 230f7651539SVladimir Sementsov-Ogievskiy } 23178c81a3fSMax Reitz assert(!s->connection_co); 23278c81a3fSMax Reitz } 23386f8cdf3SVladimir Sementsov-Ogievskiy 234f7651539SVladimir Sementsov-Ogievskiy static bool nbd_client_connecting(BDRVNBDState *s) 235f7651539SVladimir Sementsov-Ogievskiy { 236f7651539SVladimir Sementsov-Ogievskiy return s->state == NBD_CLIENT_CONNECTING_WAIT || 237f7651539SVladimir Sementsov-Ogievskiy s->state == NBD_CLIENT_CONNECTING_NOWAIT; 238f7651539SVladimir Sementsov-Ogievskiy } 239f7651539SVladimir Sementsov-Ogievskiy 240f7651539SVladimir Sementsov-Ogievskiy static bool nbd_client_connecting_wait(BDRVNBDState *s) 241f7651539SVladimir Sementsov-Ogievskiy { 242f7651539SVladimir Sementsov-Ogievskiy return s->state == NBD_CLIENT_CONNECTING_WAIT; 243f7651539SVladimir Sementsov-Ogievskiy } 244f7651539SVladimir Sementsov-Ogievskiy 245f7651539SVladimir Sementsov-Ogievskiy static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s) 246f7651539SVladimir Sementsov-Ogievskiy { 247fa35591bSVladimir Sementsov-Ogievskiy int ret; 248f7651539SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 249fa35591bSVladimir Sementsov-Ogievskiy QIOChannelSocket *sioc; 250f7651539SVladimir Sementsov-Ogievskiy 251f7651539SVladimir Sementsov-Ogievskiy if (!nbd_client_connecting(s)) { 252f7651539SVladimir Sementsov-Ogievskiy return; 253f7651539SVladimir Sementsov-Ogievskiy } 254f7651539SVladimir Sementsov-Ogievskiy 255f7651539SVladimir Sementsov-Ogievskiy /* Wait for completion of all in-flight requests */ 256f7651539SVladimir Sementsov-Ogievskiy 257f7651539SVladimir Sementsov-Ogievskiy qemu_co_mutex_lock(&s->send_mutex); 258f7651539SVladimir Sementsov-Ogievskiy 259f7651539SVladimir Sementsov-Ogievskiy while (s->in_flight > 0) { 260f7651539SVladimir Sementsov-Ogievskiy qemu_co_mutex_unlock(&s->send_mutex); 261f7651539SVladimir Sementsov-Ogievskiy nbd_recv_coroutines_wake_all(s); 262f7651539SVladimir Sementsov-Ogievskiy s->wait_in_flight = true; 263f7651539SVladimir Sementsov-Ogievskiy qemu_coroutine_yield(); 264f7651539SVladimir Sementsov-Ogievskiy s->wait_in_flight = false; 265f7651539SVladimir Sementsov-Ogievskiy qemu_co_mutex_lock(&s->send_mutex); 266f7651539SVladimir Sementsov-Ogievskiy } 267f7651539SVladimir Sementsov-Ogievskiy 268f7651539SVladimir Sementsov-Ogievskiy qemu_co_mutex_unlock(&s->send_mutex); 269f7651539SVladimir Sementsov-Ogievskiy 270f7651539SVladimir Sementsov-Ogievskiy if (!nbd_client_connecting(s)) { 271f7651539SVladimir Sementsov-Ogievskiy return; 272f7651539SVladimir Sementsov-Ogievskiy } 273f7651539SVladimir Sementsov-Ogievskiy 274f7651539SVladimir Sementsov-Ogievskiy /* 275f7651539SVladimir Sementsov-Ogievskiy * Now we are sure that nobody is accessing the channel, and no one will 276f7651539SVladimir Sementsov-Ogievskiy * try until we set the state to CONNECTED. 277f7651539SVladimir Sementsov-Ogievskiy */ 278f7651539SVladimir Sementsov-Ogievskiy 279f7651539SVladimir Sementsov-Ogievskiy /* Finalize previous connection if any */ 280f7651539SVladimir Sementsov-Ogievskiy if (s->ioc) { 281f7651539SVladimir Sementsov-Ogievskiy nbd_client_detach_aio_context(s->bs); 282611ae1d7SVladimir Sementsov-Ogievskiy object_unref(OBJECT(s->sioc)); 283611ae1d7SVladimir Sementsov-Ogievskiy s->sioc = NULL; 284611ae1d7SVladimir Sementsov-Ogievskiy object_unref(OBJECT(s->ioc)); 285611ae1d7SVladimir Sementsov-Ogievskiy s->ioc = NULL; 28686f8cdf3SVladimir Sementsov-Ogievskiy } 28786f8cdf3SVladimir Sementsov-Ogievskiy 288fa35591bSVladimir Sementsov-Ogievskiy sioc = nbd_establish_connection(s->saddr, &local_err); 289fa35591bSVladimir Sementsov-Ogievskiy if (!sioc) { 290fa35591bSVladimir Sementsov-Ogievskiy ret = -ECONNREFUSED; 291fa35591bSVladimir Sementsov-Ogievskiy goto out; 292fa35591bSVladimir Sementsov-Ogievskiy } 293fa35591bSVladimir Sementsov-Ogievskiy 294*dd1ec1a4SVladimir Sementsov-Ogievskiy bdrv_dec_in_flight(s->bs); 295*dd1ec1a4SVladimir Sementsov-Ogievskiy 296fa35591bSVladimir Sementsov-Ogievskiy ret = nbd_client_handshake(s->bs, sioc, &local_err); 297fa35591bSVladimir Sementsov-Ogievskiy 298*dd1ec1a4SVladimir Sementsov-Ogievskiy if (s->drained) { 299*dd1ec1a4SVladimir Sementsov-Ogievskiy s->wait_drained_end = true; 300*dd1ec1a4SVladimir Sementsov-Ogievskiy while (s->drained) { 301*dd1ec1a4SVladimir Sementsov-Ogievskiy /* 302*dd1ec1a4SVladimir Sementsov-Ogievskiy * We may be entered once from nbd_client_attach_aio_context_bh 303*dd1ec1a4SVladimir Sementsov-Ogievskiy * and then from nbd_client_co_drain_end. So here is a loop. 304*dd1ec1a4SVladimir Sementsov-Ogievskiy */ 305*dd1ec1a4SVladimir Sementsov-Ogievskiy qemu_coroutine_yield(); 306*dd1ec1a4SVladimir Sementsov-Ogievskiy } 307*dd1ec1a4SVladimir Sementsov-Ogievskiy } 308*dd1ec1a4SVladimir Sementsov-Ogievskiy bdrv_inc_in_flight(s->bs); 309*dd1ec1a4SVladimir Sementsov-Ogievskiy 310fa35591bSVladimir Sementsov-Ogievskiy out: 311fa35591bSVladimir Sementsov-Ogievskiy s->connect_status = ret; 312f7651539SVladimir Sementsov-Ogievskiy error_free(s->connect_err); 313f7651539SVladimir Sementsov-Ogievskiy s->connect_err = NULL; 314f7651539SVladimir Sementsov-Ogievskiy error_propagate(&s->connect_err, local_err); 315f7651539SVladimir Sementsov-Ogievskiy 316fa35591bSVladimir Sementsov-Ogievskiy if (ret >= 0) { 317f7651539SVladimir Sementsov-Ogievskiy /* successfully connected */ 318f7651539SVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_CONNECTED; 319f7651539SVladimir Sementsov-Ogievskiy qemu_co_queue_restart_all(&s->free_sema); 320f7651539SVladimir Sementsov-Ogievskiy } 321fa35591bSVladimir Sementsov-Ogievskiy } 322f7651539SVladimir Sementsov-Ogievskiy 323f7651539SVladimir Sementsov-Ogievskiy static coroutine_fn void nbd_co_reconnect_loop(BDRVNBDState *s) 324f7651539SVladimir Sementsov-Ogievskiy { 325f7651539SVladimir Sementsov-Ogievskiy uint64_t start_time_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 326f7651539SVladimir Sementsov-Ogievskiy uint64_t delay_ns = s->reconnect_delay * NANOSECONDS_PER_SECOND; 327f7651539SVladimir Sementsov-Ogievskiy uint64_t timeout = 1 * NANOSECONDS_PER_SECOND; 328f7651539SVladimir Sementsov-Ogievskiy uint64_t max_timeout = 16 * NANOSECONDS_PER_SECOND; 329f7651539SVladimir Sementsov-Ogievskiy 330f7651539SVladimir Sementsov-Ogievskiy nbd_reconnect_attempt(s); 331f7651539SVladimir Sementsov-Ogievskiy 332f7651539SVladimir Sementsov-Ogievskiy while (nbd_client_connecting(s)) { 333f7651539SVladimir Sementsov-Ogievskiy if (s->state == NBD_CLIENT_CONNECTING_WAIT && 334f7651539SVladimir Sementsov-Ogievskiy qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start_time_ns > delay_ns) 335f7651539SVladimir Sementsov-Ogievskiy { 336f7651539SVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_CONNECTING_NOWAIT; 337f7651539SVladimir Sementsov-Ogievskiy qemu_co_queue_restart_all(&s->free_sema); 338f7651539SVladimir Sementsov-Ogievskiy } 339f7651539SVladimir Sementsov-Ogievskiy 340f7651539SVladimir Sementsov-Ogievskiy qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME, timeout, 341f7651539SVladimir Sementsov-Ogievskiy &s->connection_co_sleep_ns_state); 342f7651539SVladimir Sementsov-Ogievskiy if (s->drained) { 343f7651539SVladimir Sementsov-Ogievskiy bdrv_dec_in_flight(s->bs); 344f7651539SVladimir Sementsov-Ogievskiy s->wait_drained_end = true; 345f7651539SVladimir Sementsov-Ogievskiy while (s->drained) { 346f7651539SVladimir Sementsov-Ogievskiy /* 347f7651539SVladimir Sementsov-Ogievskiy * We may be entered once from nbd_client_attach_aio_context_bh 348f7651539SVladimir Sementsov-Ogievskiy * and then from nbd_client_co_drain_end. So here is a loop. 349f7651539SVladimir Sementsov-Ogievskiy */ 350f7651539SVladimir Sementsov-Ogievskiy qemu_coroutine_yield(); 351f7651539SVladimir Sementsov-Ogievskiy } 352f7651539SVladimir Sementsov-Ogievskiy bdrv_inc_in_flight(s->bs); 353f7651539SVladimir Sementsov-Ogievskiy } 354f7651539SVladimir Sementsov-Ogievskiy if (timeout < max_timeout) { 355f7651539SVladimir Sementsov-Ogievskiy timeout *= 2; 356f7651539SVladimir Sementsov-Ogievskiy } 357f7651539SVladimir Sementsov-Ogievskiy 358f7651539SVladimir Sementsov-Ogievskiy nbd_reconnect_attempt(s); 359f7651539SVladimir Sementsov-Ogievskiy } 360f7651539SVladimir Sementsov-Ogievskiy } 361f7651539SVladimir Sementsov-Ogievskiy 36286f8cdf3SVladimir Sementsov-Ogievskiy static coroutine_fn void nbd_connection_entry(void *opaque) 36386f8cdf3SVladimir Sementsov-Ogievskiy { 364611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = opaque; 36586f8cdf3SVladimir Sementsov-Ogievskiy uint64_t i; 36686f8cdf3SVladimir Sementsov-Ogievskiy int ret = 0; 36786f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 36886f8cdf3SVladimir Sementsov-Ogievskiy 369a34b1e5eSVladimir Sementsov-Ogievskiy while (s->state != NBD_CLIENT_QUIT) { 37086f8cdf3SVladimir Sementsov-Ogievskiy /* 37186f8cdf3SVladimir Sementsov-Ogievskiy * The NBD client can only really be considered idle when it has 37286f8cdf3SVladimir Sementsov-Ogievskiy * yielded from qio_channel_readv_all_eof(), waiting for data. This is 37386f8cdf3SVladimir Sementsov-Ogievskiy * the point where the additional scheduled coroutine entry happens 37486f8cdf3SVladimir Sementsov-Ogievskiy * after nbd_client_attach_aio_context(). 37586f8cdf3SVladimir Sementsov-Ogievskiy * 37686f8cdf3SVladimir Sementsov-Ogievskiy * Therefore we keep an additional in_flight reference all the time and 37786f8cdf3SVladimir Sementsov-Ogievskiy * only drop it temporarily here. 37886f8cdf3SVladimir Sementsov-Ogievskiy */ 379f7651539SVladimir Sementsov-Ogievskiy 380f7651539SVladimir Sementsov-Ogievskiy if (nbd_client_connecting(s)) { 381f7651539SVladimir Sementsov-Ogievskiy nbd_co_reconnect_loop(s); 382f7651539SVladimir Sementsov-Ogievskiy } 383f7651539SVladimir Sementsov-Ogievskiy 384f7651539SVladimir Sementsov-Ogievskiy if (s->state != NBD_CLIENT_CONNECTED) { 385f7651539SVladimir Sementsov-Ogievskiy continue; 386f7651539SVladimir Sementsov-Ogievskiy } 387f7651539SVladimir Sementsov-Ogievskiy 38886f8cdf3SVladimir Sementsov-Ogievskiy assert(s->reply.handle == 0); 38986f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_receive_reply(s->bs, s->ioc, &s->reply, &local_err); 39086f8cdf3SVladimir Sementsov-Ogievskiy 39186f8cdf3SVladimir Sementsov-Ogievskiy if (local_err) { 39286f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_read_reply_entry_fail(ret, error_get_pretty(local_err)); 39386f8cdf3SVladimir Sementsov-Ogievskiy error_free(local_err); 394f7651539SVladimir Sementsov-Ogievskiy local_err = NULL; 39586f8cdf3SVladimir Sementsov-Ogievskiy } 39686f8cdf3SVladimir Sementsov-Ogievskiy if (ret <= 0) { 397a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, ret ? ret : -EIO); 398f7651539SVladimir Sementsov-Ogievskiy continue; 39986f8cdf3SVladimir Sementsov-Ogievskiy } 40086f8cdf3SVladimir Sementsov-Ogievskiy 40186f8cdf3SVladimir Sementsov-Ogievskiy /* 40286f8cdf3SVladimir Sementsov-Ogievskiy * There's no need for a mutex on the receive side, because the 40386f8cdf3SVladimir Sementsov-Ogievskiy * handler acts as a synchronization point and ensures that only 40486f8cdf3SVladimir Sementsov-Ogievskiy * one coroutine is called until the reply finishes. 40586f8cdf3SVladimir Sementsov-Ogievskiy */ 40686f8cdf3SVladimir Sementsov-Ogievskiy i = HANDLE_TO_INDEX(s, s->reply.handle); 40786f8cdf3SVladimir Sementsov-Ogievskiy if (i >= MAX_NBD_REQUESTS || 40886f8cdf3SVladimir Sementsov-Ogievskiy !s->requests[i].coroutine || 40986f8cdf3SVladimir Sementsov-Ogievskiy !s->requests[i].receiving || 41086f8cdf3SVladimir Sementsov-Ogievskiy (nbd_reply_is_structured(&s->reply) && !s->info.structured_reply)) 41186f8cdf3SVladimir Sementsov-Ogievskiy { 412a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, -EINVAL); 413f7651539SVladimir Sementsov-Ogievskiy continue; 41486f8cdf3SVladimir Sementsov-Ogievskiy } 41586f8cdf3SVladimir Sementsov-Ogievskiy 41686f8cdf3SVladimir Sementsov-Ogievskiy /* 41786f8cdf3SVladimir Sementsov-Ogievskiy * We're woken up again by the request itself. Note that there 41886f8cdf3SVladimir Sementsov-Ogievskiy * is no race between yielding and reentering connection_co. This 41986f8cdf3SVladimir Sementsov-Ogievskiy * is because: 42086f8cdf3SVladimir Sementsov-Ogievskiy * 42186f8cdf3SVladimir Sementsov-Ogievskiy * - if the request runs on the same AioContext, it is only 42286f8cdf3SVladimir Sementsov-Ogievskiy * entered after we yield 42386f8cdf3SVladimir Sementsov-Ogievskiy * 42486f8cdf3SVladimir Sementsov-Ogievskiy * - if the request runs on a different AioContext, reentering 42586f8cdf3SVladimir Sementsov-Ogievskiy * connection_co happens through a bottom half, which can only 42686f8cdf3SVladimir Sementsov-Ogievskiy * run after we yield. 42786f8cdf3SVladimir Sementsov-Ogievskiy */ 42886f8cdf3SVladimir Sementsov-Ogievskiy aio_co_wake(s->requests[i].coroutine); 42986f8cdf3SVladimir Sementsov-Ogievskiy qemu_coroutine_yield(); 43086f8cdf3SVladimir Sementsov-Ogievskiy } 43186f8cdf3SVladimir Sementsov-Ogievskiy 432f7651539SVladimir Sementsov-Ogievskiy qemu_co_queue_restart_all(&s->free_sema); 43386f8cdf3SVladimir Sementsov-Ogievskiy nbd_recv_coroutines_wake_all(s); 43486f8cdf3SVladimir Sementsov-Ogievskiy bdrv_dec_in_flight(s->bs); 43586f8cdf3SVladimir Sementsov-Ogievskiy 43686f8cdf3SVladimir Sementsov-Ogievskiy s->connection_co = NULL; 437f7651539SVladimir Sementsov-Ogievskiy if (s->ioc) { 438f7651539SVladimir Sementsov-Ogievskiy nbd_client_detach_aio_context(s->bs); 439f7651539SVladimir Sementsov-Ogievskiy object_unref(OBJECT(s->sioc)); 440f7651539SVladimir Sementsov-Ogievskiy s->sioc = NULL; 441f7651539SVladimir Sementsov-Ogievskiy object_unref(OBJECT(s->ioc)); 442f7651539SVladimir Sementsov-Ogievskiy s->ioc = NULL; 443f7651539SVladimir Sementsov-Ogievskiy } 444f7651539SVladimir Sementsov-Ogievskiy 44578c81a3fSMax Reitz if (s->teardown_co) { 44678c81a3fSMax Reitz aio_co_wake(s->teardown_co); 44778c81a3fSMax Reitz } 44886f8cdf3SVladimir Sementsov-Ogievskiy aio_wait_kick(); 44986f8cdf3SVladimir Sementsov-Ogievskiy } 45086f8cdf3SVladimir Sementsov-Ogievskiy 45186f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_co_send_request(BlockDriverState *bs, 45286f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest *request, 45386f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector *qiov) 45486f8cdf3SVladimir Sementsov-Ogievskiy { 455611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 456a34b1e5eSVladimir Sementsov-Ogievskiy int rc, i = -1; 45786f8cdf3SVladimir Sementsov-Ogievskiy 45886f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_mutex_lock(&s->send_mutex); 459f7651539SVladimir Sementsov-Ogievskiy while (s->in_flight == MAX_NBD_REQUESTS || nbd_client_connecting_wait(s)) { 46086f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_queue_wait(&s->free_sema, &s->send_mutex); 46186f8cdf3SVladimir Sementsov-Ogievskiy } 462a34b1e5eSVladimir Sementsov-Ogievskiy 463a34b1e5eSVladimir Sementsov-Ogievskiy if (s->state != NBD_CLIENT_CONNECTED) { 464a34b1e5eSVladimir Sementsov-Ogievskiy rc = -EIO; 465a34b1e5eSVladimir Sementsov-Ogievskiy goto err; 466a34b1e5eSVladimir Sementsov-Ogievskiy } 467a34b1e5eSVladimir Sementsov-Ogievskiy 46886f8cdf3SVladimir Sementsov-Ogievskiy s->in_flight++; 46986f8cdf3SVladimir Sementsov-Ogievskiy 47086f8cdf3SVladimir Sementsov-Ogievskiy for (i = 0; i < MAX_NBD_REQUESTS; i++) { 47186f8cdf3SVladimir Sementsov-Ogievskiy if (s->requests[i].coroutine == NULL) { 47286f8cdf3SVladimir Sementsov-Ogievskiy break; 47386f8cdf3SVladimir Sementsov-Ogievskiy } 47486f8cdf3SVladimir Sementsov-Ogievskiy } 47586f8cdf3SVladimir Sementsov-Ogievskiy 47686f8cdf3SVladimir Sementsov-Ogievskiy g_assert(qemu_in_coroutine()); 47786f8cdf3SVladimir Sementsov-Ogievskiy assert(i < MAX_NBD_REQUESTS); 47886f8cdf3SVladimir Sementsov-Ogievskiy 47986f8cdf3SVladimir Sementsov-Ogievskiy s->requests[i].coroutine = qemu_coroutine_self(); 48086f8cdf3SVladimir Sementsov-Ogievskiy s->requests[i].offset = request->from; 48186f8cdf3SVladimir Sementsov-Ogievskiy s->requests[i].receiving = false; 48286f8cdf3SVladimir Sementsov-Ogievskiy 48386f8cdf3SVladimir Sementsov-Ogievskiy request->handle = INDEX_TO_HANDLE(s, i); 48486f8cdf3SVladimir Sementsov-Ogievskiy 48586f8cdf3SVladimir Sementsov-Ogievskiy assert(s->ioc); 48686f8cdf3SVladimir Sementsov-Ogievskiy 48786f8cdf3SVladimir Sementsov-Ogievskiy if (qiov) { 48886f8cdf3SVladimir Sementsov-Ogievskiy qio_channel_set_cork(s->ioc, true); 48986f8cdf3SVladimir Sementsov-Ogievskiy rc = nbd_send_request(s->ioc, request); 490a34b1e5eSVladimir Sementsov-Ogievskiy if (rc >= 0 && s->state == NBD_CLIENT_CONNECTED) { 49186f8cdf3SVladimir Sementsov-Ogievskiy if (qio_channel_writev_all(s->ioc, qiov->iov, qiov->niov, 49286f8cdf3SVladimir Sementsov-Ogievskiy NULL) < 0) { 49386f8cdf3SVladimir Sementsov-Ogievskiy rc = -EIO; 49486f8cdf3SVladimir Sementsov-Ogievskiy } 49586f8cdf3SVladimir Sementsov-Ogievskiy } else if (rc >= 0) { 49686f8cdf3SVladimir Sementsov-Ogievskiy rc = -EIO; 49786f8cdf3SVladimir Sementsov-Ogievskiy } 49886f8cdf3SVladimir Sementsov-Ogievskiy qio_channel_set_cork(s->ioc, false); 49986f8cdf3SVladimir Sementsov-Ogievskiy } else { 50086f8cdf3SVladimir Sementsov-Ogievskiy rc = nbd_send_request(s->ioc, request); 50186f8cdf3SVladimir Sementsov-Ogievskiy } 50286f8cdf3SVladimir Sementsov-Ogievskiy 50386f8cdf3SVladimir Sementsov-Ogievskiy err: 50486f8cdf3SVladimir Sementsov-Ogievskiy if (rc < 0) { 505a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, rc); 506a34b1e5eSVladimir Sementsov-Ogievskiy if (i != -1) { 50786f8cdf3SVladimir Sementsov-Ogievskiy s->requests[i].coroutine = NULL; 50886f8cdf3SVladimir Sementsov-Ogievskiy s->in_flight--; 509a34b1e5eSVladimir Sementsov-Ogievskiy } 510f7651539SVladimir Sementsov-Ogievskiy if (s->in_flight == 0 && s->wait_in_flight) { 511f7651539SVladimir Sementsov-Ogievskiy aio_co_wake(s->connection_co); 512f7651539SVladimir Sementsov-Ogievskiy } else { 51386f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_queue_next(&s->free_sema); 51486f8cdf3SVladimir Sementsov-Ogievskiy } 515f7651539SVladimir Sementsov-Ogievskiy } 51686f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_mutex_unlock(&s->send_mutex); 51786f8cdf3SVladimir Sementsov-Ogievskiy return rc; 51886f8cdf3SVladimir Sementsov-Ogievskiy } 51986f8cdf3SVladimir Sementsov-Ogievskiy 52086f8cdf3SVladimir Sementsov-Ogievskiy static inline uint16_t payload_advance16(uint8_t **payload) 52186f8cdf3SVladimir Sementsov-Ogievskiy { 52286f8cdf3SVladimir Sementsov-Ogievskiy *payload += 2; 52386f8cdf3SVladimir Sementsov-Ogievskiy return lduw_be_p(*payload - 2); 52486f8cdf3SVladimir Sementsov-Ogievskiy } 52586f8cdf3SVladimir Sementsov-Ogievskiy 52686f8cdf3SVladimir Sementsov-Ogievskiy static inline uint32_t payload_advance32(uint8_t **payload) 52786f8cdf3SVladimir Sementsov-Ogievskiy { 52886f8cdf3SVladimir Sementsov-Ogievskiy *payload += 4; 52986f8cdf3SVladimir Sementsov-Ogievskiy return ldl_be_p(*payload - 4); 53086f8cdf3SVladimir Sementsov-Ogievskiy } 53186f8cdf3SVladimir Sementsov-Ogievskiy 53286f8cdf3SVladimir Sementsov-Ogievskiy static inline uint64_t payload_advance64(uint8_t **payload) 53386f8cdf3SVladimir Sementsov-Ogievskiy { 53486f8cdf3SVladimir Sementsov-Ogievskiy *payload += 8; 53586f8cdf3SVladimir Sementsov-Ogievskiy return ldq_be_p(*payload - 8); 53686f8cdf3SVladimir Sementsov-Ogievskiy } 53786f8cdf3SVladimir Sementsov-Ogievskiy 538611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_parse_offset_hole_payload(BDRVNBDState *s, 53986f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk, 54086f8cdf3SVladimir Sementsov-Ogievskiy uint8_t *payload, uint64_t orig_offset, 54186f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector *qiov, Error **errp) 54286f8cdf3SVladimir Sementsov-Ogievskiy { 54386f8cdf3SVladimir Sementsov-Ogievskiy uint64_t offset; 54486f8cdf3SVladimir Sementsov-Ogievskiy uint32_t hole_size; 54586f8cdf3SVladimir Sementsov-Ogievskiy 54686f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length != sizeof(offset) + sizeof(hole_size)) { 54786f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: invalid payload for " 54886f8cdf3SVladimir Sementsov-Ogievskiy "NBD_REPLY_TYPE_OFFSET_HOLE"); 54986f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 55086f8cdf3SVladimir Sementsov-Ogievskiy } 55186f8cdf3SVladimir Sementsov-Ogievskiy 55286f8cdf3SVladimir Sementsov-Ogievskiy offset = payload_advance64(&payload); 55386f8cdf3SVladimir Sementsov-Ogievskiy hole_size = payload_advance32(&payload); 55486f8cdf3SVladimir Sementsov-Ogievskiy 55586f8cdf3SVladimir Sementsov-Ogievskiy if (!hole_size || offset < orig_offset || hole_size > qiov->size || 55686f8cdf3SVladimir Sementsov-Ogievskiy offset > orig_offset + qiov->size - hole_size) { 55786f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent chunk exceeding requested" 55886f8cdf3SVladimir Sementsov-Ogievskiy " region"); 55986f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 56086f8cdf3SVladimir Sementsov-Ogievskiy } 561611ae1d7SVladimir Sementsov-Ogievskiy if (s->info.min_block && 562611ae1d7SVladimir Sementsov-Ogievskiy !QEMU_IS_ALIGNED(hole_size, s->info.min_block)) { 56386f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_structured_read_compliance("hole"); 56486f8cdf3SVladimir Sementsov-Ogievskiy } 56586f8cdf3SVladimir Sementsov-Ogievskiy 56686f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_memset(qiov, offset - orig_offset, 0, hole_size); 56786f8cdf3SVladimir Sementsov-Ogievskiy 56886f8cdf3SVladimir Sementsov-Ogievskiy return 0; 56986f8cdf3SVladimir Sementsov-Ogievskiy } 57086f8cdf3SVladimir Sementsov-Ogievskiy 57186f8cdf3SVladimir Sementsov-Ogievskiy /* 57286f8cdf3SVladimir Sementsov-Ogievskiy * nbd_parse_blockstatus_payload 57386f8cdf3SVladimir Sementsov-Ogievskiy * Based on our request, we expect only one extent in reply, for the 57486f8cdf3SVladimir Sementsov-Ogievskiy * base:allocation context. 57586f8cdf3SVladimir Sementsov-Ogievskiy */ 576611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_parse_blockstatus_payload(BDRVNBDState *s, 57786f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk, 57886f8cdf3SVladimir Sementsov-Ogievskiy uint8_t *payload, uint64_t orig_length, 57986f8cdf3SVladimir Sementsov-Ogievskiy NBDExtent *extent, Error **errp) 58086f8cdf3SVladimir Sementsov-Ogievskiy { 58186f8cdf3SVladimir Sementsov-Ogievskiy uint32_t context_id; 58286f8cdf3SVladimir Sementsov-Ogievskiy 58386f8cdf3SVladimir Sementsov-Ogievskiy /* The server succeeded, so it must have sent [at least] one extent */ 58486f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length < sizeof(context_id) + sizeof(*extent)) { 58586f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: invalid payload for " 58686f8cdf3SVladimir Sementsov-Ogievskiy "NBD_REPLY_TYPE_BLOCK_STATUS"); 58786f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 58886f8cdf3SVladimir Sementsov-Ogievskiy } 58986f8cdf3SVladimir Sementsov-Ogievskiy 59086f8cdf3SVladimir Sementsov-Ogievskiy context_id = payload_advance32(&payload); 591611ae1d7SVladimir Sementsov-Ogievskiy if (s->info.context_id != context_id) { 59286f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: unexpected context id %d for " 59386f8cdf3SVladimir Sementsov-Ogievskiy "NBD_REPLY_TYPE_BLOCK_STATUS, when negotiated context " 59486f8cdf3SVladimir Sementsov-Ogievskiy "id is %d", context_id, 595611ae1d7SVladimir Sementsov-Ogievskiy s->info.context_id); 59686f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 59786f8cdf3SVladimir Sementsov-Ogievskiy } 59886f8cdf3SVladimir Sementsov-Ogievskiy 59986f8cdf3SVladimir Sementsov-Ogievskiy extent->length = payload_advance32(&payload); 60086f8cdf3SVladimir Sementsov-Ogievskiy extent->flags = payload_advance32(&payload); 60186f8cdf3SVladimir Sementsov-Ogievskiy 60286f8cdf3SVladimir Sementsov-Ogievskiy if (extent->length == 0) { 60386f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent status chunk with " 60486f8cdf3SVladimir Sementsov-Ogievskiy "zero length"); 60586f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 60686f8cdf3SVladimir Sementsov-Ogievskiy } 60786f8cdf3SVladimir Sementsov-Ogievskiy 60886f8cdf3SVladimir Sementsov-Ogievskiy /* 60986f8cdf3SVladimir Sementsov-Ogievskiy * A server sending unaligned block status is in violation of the 61086f8cdf3SVladimir Sementsov-Ogievskiy * protocol, but as qemu-nbd 3.1 is such a server (at least for 61186f8cdf3SVladimir Sementsov-Ogievskiy * POSIX files that are not a multiple of 512 bytes, since qemu 61286f8cdf3SVladimir Sementsov-Ogievskiy * rounds files up to 512-byte multiples but lseek(SEEK_HOLE) 61386f8cdf3SVladimir Sementsov-Ogievskiy * still sees an implicit hole beyond the real EOF), it's nicer to 61486f8cdf3SVladimir Sementsov-Ogievskiy * work around the misbehaving server. If the request included 61586f8cdf3SVladimir Sementsov-Ogievskiy * more than the final unaligned block, truncate it back to an 61686f8cdf3SVladimir Sementsov-Ogievskiy * aligned result; if the request was only the final block, round 61786f8cdf3SVladimir Sementsov-Ogievskiy * up to the full block and change the status to fully-allocated 61886f8cdf3SVladimir Sementsov-Ogievskiy * (always a safe status, even if it loses information). 61986f8cdf3SVladimir Sementsov-Ogievskiy */ 620611ae1d7SVladimir Sementsov-Ogievskiy if (s->info.min_block && !QEMU_IS_ALIGNED(extent->length, 621611ae1d7SVladimir Sementsov-Ogievskiy s->info.min_block)) { 62286f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_parse_blockstatus_compliance("extent length is unaligned"); 623611ae1d7SVladimir Sementsov-Ogievskiy if (extent->length > s->info.min_block) { 62486f8cdf3SVladimir Sementsov-Ogievskiy extent->length = QEMU_ALIGN_DOWN(extent->length, 625611ae1d7SVladimir Sementsov-Ogievskiy s->info.min_block); 62686f8cdf3SVladimir Sementsov-Ogievskiy } else { 627611ae1d7SVladimir Sementsov-Ogievskiy extent->length = s->info.min_block; 62886f8cdf3SVladimir Sementsov-Ogievskiy extent->flags = 0; 62986f8cdf3SVladimir Sementsov-Ogievskiy } 63086f8cdf3SVladimir Sementsov-Ogievskiy } 63186f8cdf3SVladimir Sementsov-Ogievskiy 63286f8cdf3SVladimir Sementsov-Ogievskiy /* 63386f8cdf3SVladimir Sementsov-Ogievskiy * We used NBD_CMD_FLAG_REQ_ONE, so the server should not have 63486f8cdf3SVladimir Sementsov-Ogievskiy * sent us any more than one extent, nor should it have included 63586f8cdf3SVladimir Sementsov-Ogievskiy * status beyond our request in that extent. However, it's easy 63686f8cdf3SVladimir Sementsov-Ogievskiy * enough to ignore the server's noncompliance without killing the 63786f8cdf3SVladimir Sementsov-Ogievskiy * connection; just ignore trailing extents, and clamp things to 63886f8cdf3SVladimir Sementsov-Ogievskiy * the length of our request. 63986f8cdf3SVladimir Sementsov-Ogievskiy */ 64086f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length > sizeof(context_id) + sizeof(*extent)) { 64186f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_parse_blockstatus_compliance("more than one extent"); 64286f8cdf3SVladimir Sementsov-Ogievskiy } 64386f8cdf3SVladimir Sementsov-Ogievskiy if (extent->length > orig_length) { 64486f8cdf3SVladimir Sementsov-Ogievskiy extent->length = orig_length; 64586f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_parse_blockstatus_compliance("extent length too large"); 64686f8cdf3SVladimir Sementsov-Ogievskiy } 64786f8cdf3SVladimir Sementsov-Ogievskiy 64886f8cdf3SVladimir Sementsov-Ogievskiy return 0; 64986f8cdf3SVladimir Sementsov-Ogievskiy } 65086f8cdf3SVladimir Sementsov-Ogievskiy 65186f8cdf3SVladimir Sementsov-Ogievskiy /* 65286f8cdf3SVladimir Sementsov-Ogievskiy * nbd_parse_error_payload 65386f8cdf3SVladimir Sementsov-Ogievskiy * on success @errp contains message describing nbd error reply 65486f8cdf3SVladimir Sementsov-Ogievskiy */ 65586f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_parse_error_payload(NBDStructuredReplyChunk *chunk, 65686f8cdf3SVladimir Sementsov-Ogievskiy uint8_t *payload, int *request_ret, 65786f8cdf3SVladimir Sementsov-Ogievskiy Error **errp) 65886f8cdf3SVladimir Sementsov-Ogievskiy { 65986f8cdf3SVladimir Sementsov-Ogievskiy uint32_t error; 66086f8cdf3SVladimir Sementsov-Ogievskiy uint16_t message_size; 66186f8cdf3SVladimir Sementsov-Ogievskiy 66286f8cdf3SVladimir Sementsov-Ogievskiy assert(chunk->type & (1 << 15)); 66386f8cdf3SVladimir Sementsov-Ogievskiy 66486f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length < sizeof(error) + sizeof(message_size)) { 66586f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, 66686f8cdf3SVladimir Sementsov-Ogievskiy "Protocol error: invalid payload for structured error"); 66786f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 66886f8cdf3SVladimir Sementsov-Ogievskiy } 66986f8cdf3SVladimir Sementsov-Ogievskiy 67086f8cdf3SVladimir Sementsov-Ogievskiy error = nbd_errno_to_system_errno(payload_advance32(&payload)); 67186f8cdf3SVladimir Sementsov-Ogievskiy if (error == 0) { 67286f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent structured error chunk " 67386f8cdf3SVladimir Sementsov-Ogievskiy "with error = 0"); 67486f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 67586f8cdf3SVladimir Sementsov-Ogievskiy } 67686f8cdf3SVladimir Sementsov-Ogievskiy 67786f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = -error; 67886f8cdf3SVladimir Sementsov-Ogievskiy message_size = payload_advance16(&payload); 67986f8cdf3SVladimir Sementsov-Ogievskiy 68086f8cdf3SVladimir Sementsov-Ogievskiy if (message_size > chunk->length - sizeof(error) - sizeof(message_size)) { 68186f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent structured error chunk " 68286f8cdf3SVladimir Sementsov-Ogievskiy "with incorrect message size"); 68386f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 68486f8cdf3SVladimir Sementsov-Ogievskiy } 68586f8cdf3SVladimir Sementsov-Ogievskiy 68686f8cdf3SVladimir Sementsov-Ogievskiy /* TODO: Add a trace point to mention the server complaint */ 68786f8cdf3SVladimir Sementsov-Ogievskiy 68886f8cdf3SVladimir Sementsov-Ogievskiy /* TODO handle ERROR_OFFSET */ 68986f8cdf3SVladimir Sementsov-Ogievskiy 69086f8cdf3SVladimir Sementsov-Ogievskiy return 0; 69186f8cdf3SVladimir Sementsov-Ogievskiy } 69286f8cdf3SVladimir Sementsov-Ogievskiy 693611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_co_receive_offset_data_payload(BDRVNBDState *s, 69486f8cdf3SVladimir Sementsov-Ogievskiy uint64_t orig_offset, 69586f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector *qiov, Error **errp) 69686f8cdf3SVladimir Sementsov-Ogievskiy { 69786f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector sub_qiov; 69886f8cdf3SVladimir Sementsov-Ogievskiy uint64_t offset; 69986f8cdf3SVladimir Sementsov-Ogievskiy size_t data_size; 70086f8cdf3SVladimir Sementsov-Ogievskiy int ret; 70186f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk = &s->reply.structured; 70286f8cdf3SVladimir Sementsov-Ogievskiy 70386f8cdf3SVladimir Sementsov-Ogievskiy assert(nbd_reply_is_structured(&s->reply)); 70486f8cdf3SVladimir Sementsov-Ogievskiy 70586f8cdf3SVladimir Sementsov-Ogievskiy /* The NBD spec requires at least one byte of payload */ 70686f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length <= sizeof(offset)) { 70786f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: invalid payload for " 70886f8cdf3SVladimir Sementsov-Ogievskiy "NBD_REPLY_TYPE_OFFSET_DATA"); 70986f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 71086f8cdf3SVladimir Sementsov-Ogievskiy } 71186f8cdf3SVladimir Sementsov-Ogievskiy 71286f8cdf3SVladimir Sementsov-Ogievskiy if (nbd_read64(s->ioc, &offset, "OFFSET_DATA offset", errp) < 0) { 71386f8cdf3SVladimir Sementsov-Ogievskiy return -EIO; 71486f8cdf3SVladimir Sementsov-Ogievskiy } 71586f8cdf3SVladimir Sementsov-Ogievskiy 71686f8cdf3SVladimir Sementsov-Ogievskiy data_size = chunk->length - sizeof(offset); 71786f8cdf3SVladimir Sementsov-Ogievskiy assert(data_size); 71886f8cdf3SVladimir Sementsov-Ogievskiy if (offset < orig_offset || data_size > qiov->size || 71986f8cdf3SVladimir Sementsov-Ogievskiy offset > orig_offset + qiov->size - data_size) { 72086f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent chunk exceeding requested" 72186f8cdf3SVladimir Sementsov-Ogievskiy " region"); 72286f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 72386f8cdf3SVladimir Sementsov-Ogievskiy } 72486f8cdf3SVladimir Sementsov-Ogievskiy if (s->info.min_block && !QEMU_IS_ALIGNED(data_size, s->info.min_block)) { 72586f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_structured_read_compliance("data"); 72686f8cdf3SVladimir Sementsov-Ogievskiy } 72786f8cdf3SVladimir Sementsov-Ogievskiy 72886f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_init(&sub_qiov, qiov->niov); 72986f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_concat(&sub_qiov, qiov, offset - orig_offset, data_size); 73086f8cdf3SVladimir Sementsov-Ogievskiy ret = qio_channel_readv_all(s->ioc, sub_qiov.iov, sub_qiov.niov, errp); 73186f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_destroy(&sub_qiov); 73286f8cdf3SVladimir Sementsov-Ogievskiy 73386f8cdf3SVladimir Sementsov-Ogievskiy return ret < 0 ? -EIO : 0; 73486f8cdf3SVladimir Sementsov-Ogievskiy } 73586f8cdf3SVladimir Sementsov-Ogievskiy 73686f8cdf3SVladimir Sementsov-Ogievskiy #define NBD_MAX_MALLOC_PAYLOAD 1000 73786f8cdf3SVladimir Sementsov-Ogievskiy static coroutine_fn int nbd_co_receive_structured_payload( 738611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s, void **payload, Error **errp) 73986f8cdf3SVladimir Sementsov-Ogievskiy { 74086f8cdf3SVladimir Sementsov-Ogievskiy int ret; 74186f8cdf3SVladimir Sementsov-Ogievskiy uint32_t len; 74286f8cdf3SVladimir Sementsov-Ogievskiy 74386f8cdf3SVladimir Sementsov-Ogievskiy assert(nbd_reply_is_structured(&s->reply)); 74486f8cdf3SVladimir Sementsov-Ogievskiy 74586f8cdf3SVladimir Sementsov-Ogievskiy len = s->reply.structured.length; 74686f8cdf3SVladimir Sementsov-Ogievskiy 74786f8cdf3SVladimir Sementsov-Ogievskiy if (len == 0) { 74886f8cdf3SVladimir Sementsov-Ogievskiy return 0; 74986f8cdf3SVladimir Sementsov-Ogievskiy } 75086f8cdf3SVladimir Sementsov-Ogievskiy 75186f8cdf3SVladimir Sementsov-Ogievskiy if (payload == NULL) { 75286f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Unexpected structured payload"); 75386f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 75486f8cdf3SVladimir Sementsov-Ogievskiy } 75586f8cdf3SVladimir Sementsov-Ogievskiy 75686f8cdf3SVladimir Sementsov-Ogievskiy if (len > NBD_MAX_MALLOC_PAYLOAD) { 75786f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Payload too large"); 75886f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 75986f8cdf3SVladimir Sementsov-Ogievskiy } 76086f8cdf3SVladimir Sementsov-Ogievskiy 76186f8cdf3SVladimir Sementsov-Ogievskiy *payload = g_new(char, len); 76286f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_read(s->ioc, *payload, len, "structured payload", errp); 76386f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 76486f8cdf3SVladimir Sementsov-Ogievskiy g_free(*payload); 76586f8cdf3SVladimir Sementsov-Ogievskiy *payload = NULL; 76686f8cdf3SVladimir Sementsov-Ogievskiy return ret; 76786f8cdf3SVladimir Sementsov-Ogievskiy } 76886f8cdf3SVladimir Sementsov-Ogievskiy 76986f8cdf3SVladimir Sementsov-Ogievskiy return 0; 77086f8cdf3SVladimir Sementsov-Ogievskiy } 77186f8cdf3SVladimir Sementsov-Ogievskiy 77286f8cdf3SVladimir Sementsov-Ogievskiy /* 77386f8cdf3SVladimir Sementsov-Ogievskiy * nbd_co_do_receive_one_chunk 77486f8cdf3SVladimir Sementsov-Ogievskiy * for simple reply: 77586f8cdf3SVladimir Sementsov-Ogievskiy * set request_ret to received reply error 77686f8cdf3SVladimir Sementsov-Ogievskiy * if qiov is not NULL: read payload to @qiov 77786f8cdf3SVladimir Sementsov-Ogievskiy * for structured reply chunk: 77886f8cdf3SVladimir Sementsov-Ogievskiy * if error chunk: read payload, set @request_ret, do not set @payload 77986f8cdf3SVladimir Sementsov-Ogievskiy * else if offset_data chunk: read payload data to @qiov, do not set @payload 78086f8cdf3SVladimir Sementsov-Ogievskiy * else: read payload to @payload 78186f8cdf3SVladimir Sementsov-Ogievskiy * 78286f8cdf3SVladimir Sementsov-Ogievskiy * If function fails, @errp contains corresponding error message, and the 78386f8cdf3SVladimir Sementsov-Ogievskiy * connection with the server is suspect. If it returns 0, then the 78486f8cdf3SVladimir Sementsov-Ogievskiy * transaction succeeded (although @request_ret may be a negative errno 78586f8cdf3SVladimir Sementsov-Ogievskiy * corresponding to the server's error reply), and errp is unchanged. 78686f8cdf3SVladimir Sementsov-Ogievskiy */ 78786f8cdf3SVladimir Sementsov-Ogievskiy static coroutine_fn int nbd_co_do_receive_one_chunk( 788611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s, uint64_t handle, bool only_structured, 78986f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, QEMUIOVector *qiov, void **payload, Error **errp) 79086f8cdf3SVladimir Sementsov-Ogievskiy { 79186f8cdf3SVladimir Sementsov-Ogievskiy int ret; 79286f8cdf3SVladimir Sementsov-Ogievskiy int i = HANDLE_TO_INDEX(s, handle); 79386f8cdf3SVladimir Sementsov-Ogievskiy void *local_payload = NULL; 79486f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk; 79586f8cdf3SVladimir Sementsov-Ogievskiy 79686f8cdf3SVladimir Sementsov-Ogievskiy if (payload) { 79786f8cdf3SVladimir Sementsov-Ogievskiy *payload = NULL; 79886f8cdf3SVladimir Sementsov-Ogievskiy } 79986f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = 0; 80086f8cdf3SVladimir Sementsov-Ogievskiy 80186f8cdf3SVladimir Sementsov-Ogievskiy /* Wait until we're woken up by nbd_connection_entry. */ 80286f8cdf3SVladimir Sementsov-Ogievskiy s->requests[i].receiving = true; 80386f8cdf3SVladimir Sementsov-Ogievskiy qemu_coroutine_yield(); 80486f8cdf3SVladimir Sementsov-Ogievskiy s->requests[i].receiving = false; 805a34b1e5eSVladimir Sementsov-Ogievskiy if (s->state != NBD_CLIENT_CONNECTED) { 80686f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Connection closed"); 80786f8cdf3SVladimir Sementsov-Ogievskiy return -EIO; 80886f8cdf3SVladimir Sementsov-Ogievskiy } 80986f8cdf3SVladimir Sementsov-Ogievskiy assert(s->ioc); 81086f8cdf3SVladimir Sementsov-Ogievskiy 81186f8cdf3SVladimir Sementsov-Ogievskiy assert(s->reply.handle == handle); 81286f8cdf3SVladimir Sementsov-Ogievskiy 81386f8cdf3SVladimir Sementsov-Ogievskiy if (nbd_reply_is_simple(&s->reply)) { 81486f8cdf3SVladimir Sementsov-Ogievskiy if (only_structured) { 81586f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: simple reply when structured " 81686f8cdf3SVladimir Sementsov-Ogievskiy "reply chunk was expected"); 81786f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 81886f8cdf3SVladimir Sementsov-Ogievskiy } 81986f8cdf3SVladimir Sementsov-Ogievskiy 82086f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = -nbd_errno_to_system_errno(s->reply.simple.error); 82186f8cdf3SVladimir Sementsov-Ogievskiy if (*request_ret < 0 || !qiov) { 82286f8cdf3SVladimir Sementsov-Ogievskiy return 0; 82386f8cdf3SVladimir Sementsov-Ogievskiy } 82486f8cdf3SVladimir Sementsov-Ogievskiy 82586f8cdf3SVladimir Sementsov-Ogievskiy return qio_channel_readv_all(s->ioc, qiov->iov, qiov->niov, 82686f8cdf3SVladimir Sementsov-Ogievskiy errp) < 0 ? -EIO : 0; 82786f8cdf3SVladimir Sementsov-Ogievskiy } 82886f8cdf3SVladimir Sementsov-Ogievskiy 82986f8cdf3SVladimir Sementsov-Ogievskiy /* handle structured reply chunk */ 83086f8cdf3SVladimir Sementsov-Ogievskiy assert(s->info.structured_reply); 83186f8cdf3SVladimir Sementsov-Ogievskiy chunk = &s->reply.structured; 83286f8cdf3SVladimir Sementsov-Ogievskiy 83386f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->type == NBD_REPLY_TYPE_NONE) { 83486f8cdf3SVladimir Sementsov-Ogievskiy if (!(chunk->flags & NBD_REPLY_FLAG_DONE)) { 83586f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk without" 83686f8cdf3SVladimir Sementsov-Ogievskiy " NBD_REPLY_FLAG_DONE flag set"); 83786f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 83886f8cdf3SVladimir Sementsov-Ogievskiy } 83986f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length) { 84086f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk with" 84186f8cdf3SVladimir Sementsov-Ogievskiy " nonzero length"); 84286f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 84386f8cdf3SVladimir Sementsov-Ogievskiy } 84486f8cdf3SVladimir Sementsov-Ogievskiy return 0; 84586f8cdf3SVladimir Sementsov-Ogievskiy } 84686f8cdf3SVladimir Sementsov-Ogievskiy 84786f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->type == NBD_REPLY_TYPE_OFFSET_DATA) { 84886f8cdf3SVladimir Sementsov-Ogievskiy if (!qiov) { 84986f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Unexpected NBD_REPLY_TYPE_OFFSET_DATA chunk"); 85086f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 85186f8cdf3SVladimir Sementsov-Ogievskiy } 85286f8cdf3SVladimir Sementsov-Ogievskiy 85386f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_receive_offset_data_payload(s, s->requests[i].offset, 85486f8cdf3SVladimir Sementsov-Ogievskiy qiov, errp); 85586f8cdf3SVladimir Sementsov-Ogievskiy } 85686f8cdf3SVladimir Sementsov-Ogievskiy 85786f8cdf3SVladimir Sementsov-Ogievskiy if (nbd_reply_type_is_error(chunk->type)) { 85886f8cdf3SVladimir Sementsov-Ogievskiy payload = &local_payload; 85986f8cdf3SVladimir Sementsov-Ogievskiy } 86086f8cdf3SVladimir Sementsov-Ogievskiy 86186f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_co_receive_structured_payload(s, payload, errp); 86286f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 86386f8cdf3SVladimir Sementsov-Ogievskiy return ret; 86486f8cdf3SVladimir Sementsov-Ogievskiy } 86586f8cdf3SVladimir Sementsov-Ogievskiy 86686f8cdf3SVladimir Sementsov-Ogievskiy if (nbd_reply_type_is_error(chunk->type)) { 86786f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_parse_error_payload(chunk, local_payload, request_ret, errp); 86886f8cdf3SVladimir Sementsov-Ogievskiy g_free(local_payload); 86986f8cdf3SVladimir Sementsov-Ogievskiy return ret; 87086f8cdf3SVladimir Sementsov-Ogievskiy } 87186f8cdf3SVladimir Sementsov-Ogievskiy 87286f8cdf3SVladimir Sementsov-Ogievskiy return 0; 87386f8cdf3SVladimir Sementsov-Ogievskiy } 87486f8cdf3SVladimir Sementsov-Ogievskiy 87586f8cdf3SVladimir Sementsov-Ogievskiy /* 87686f8cdf3SVladimir Sementsov-Ogievskiy * nbd_co_receive_one_chunk 87786f8cdf3SVladimir Sementsov-Ogievskiy * Read reply, wake up connection_co and set s->quit if needed. 87886f8cdf3SVladimir Sementsov-Ogievskiy * Return value is a fatal error code or normal nbd reply error code 87986f8cdf3SVladimir Sementsov-Ogievskiy */ 88086f8cdf3SVladimir Sementsov-Ogievskiy static coroutine_fn int nbd_co_receive_one_chunk( 881611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s, uint64_t handle, bool only_structured, 88286f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, QEMUIOVector *qiov, NBDReply *reply, void **payload, 88386f8cdf3SVladimir Sementsov-Ogievskiy Error **errp) 88486f8cdf3SVladimir Sementsov-Ogievskiy { 88586f8cdf3SVladimir Sementsov-Ogievskiy int ret = nbd_co_do_receive_one_chunk(s, handle, only_structured, 88686f8cdf3SVladimir Sementsov-Ogievskiy request_ret, qiov, payload, errp); 88786f8cdf3SVladimir Sementsov-Ogievskiy 88886f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 8895cf42b1cSEric Blake memset(reply, 0, sizeof(*reply)); 890a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, ret); 89186f8cdf3SVladimir Sementsov-Ogievskiy } else { 89286f8cdf3SVladimir Sementsov-Ogievskiy /* For assert at loop start in nbd_connection_entry */ 89386f8cdf3SVladimir Sementsov-Ogievskiy *reply = s->reply; 89486f8cdf3SVladimir Sementsov-Ogievskiy } 895f7651539SVladimir Sementsov-Ogievskiy s->reply.handle = 0; 89686f8cdf3SVladimir Sementsov-Ogievskiy 897f7651539SVladimir Sementsov-Ogievskiy if (s->connection_co && !s->wait_in_flight) { 898f7651539SVladimir Sementsov-Ogievskiy /* 899f7651539SVladimir Sementsov-Ogievskiy * We must check s->wait_in_flight, because we may entered by 900f7651539SVladimir Sementsov-Ogievskiy * nbd_recv_coroutines_wake_all(), in this case we should not 901f7651539SVladimir Sementsov-Ogievskiy * wake connection_co here, it will woken by last request. 902f7651539SVladimir Sementsov-Ogievskiy */ 90386f8cdf3SVladimir Sementsov-Ogievskiy aio_co_wake(s->connection_co); 90486f8cdf3SVladimir Sementsov-Ogievskiy } 90586f8cdf3SVladimir Sementsov-Ogievskiy 90686f8cdf3SVladimir Sementsov-Ogievskiy return ret; 90786f8cdf3SVladimir Sementsov-Ogievskiy } 90886f8cdf3SVladimir Sementsov-Ogievskiy 90986f8cdf3SVladimir Sementsov-Ogievskiy typedef struct NBDReplyChunkIter { 91086f8cdf3SVladimir Sementsov-Ogievskiy int ret; 91186f8cdf3SVladimir Sementsov-Ogievskiy int request_ret; 91286f8cdf3SVladimir Sementsov-Ogievskiy Error *err; 91386f8cdf3SVladimir Sementsov-Ogievskiy bool done, only_structured; 91486f8cdf3SVladimir Sementsov-Ogievskiy } NBDReplyChunkIter; 91586f8cdf3SVladimir Sementsov-Ogievskiy 91686f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_iter_channel_error(NBDReplyChunkIter *iter, 91786f8cdf3SVladimir Sementsov-Ogievskiy int ret, Error **local_err) 91886f8cdf3SVladimir Sementsov-Ogievskiy { 919d9366135SVladimir Sementsov-Ogievskiy assert(local_err && *local_err); 92086f8cdf3SVladimir Sementsov-Ogievskiy assert(ret < 0); 92186f8cdf3SVladimir Sementsov-Ogievskiy 92286f8cdf3SVladimir Sementsov-Ogievskiy if (!iter->ret) { 92386f8cdf3SVladimir Sementsov-Ogievskiy iter->ret = ret; 92486f8cdf3SVladimir Sementsov-Ogievskiy error_propagate(&iter->err, *local_err); 92586f8cdf3SVladimir Sementsov-Ogievskiy } else { 92686f8cdf3SVladimir Sementsov-Ogievskiy error_free(*local_err); 92786f8cdf3SVladimir Sementsov-Ogievskiy } 92886f8cdf3SVladimir Sementsov-Ogievskiy 92986f8cdf3SVladimir Sementsov-Ogievskiy *local_err = NULL; 93086f8cdf3SVladimir Sementsov-Ogievskiy } 93186f8cdf3SVladimir Sementsov-Ogievskiy 93286f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_iter_request_error(NBDReplyChunkIter *iter, int ret) 93386f8cdf3SVladimir Sementsov-Ogievskiy { 93486f8cdf3SVladimir Sementsov-Ogievskiy assert(ret < 0); 93586f8cdf3SVladimir Sementsov-Ogievskiy 93686f8cdf3SVladimir Sementsov-Ogievskiy if (!iter->request_ret) { 93786f8cdf3SVladimir Sementsov-Ogievskiy iter->request_ret = ret; 93886f8cdf3SVladimir Sementsov-Ogievskiy } 93986f8cdf3SVladimir Sementsov-Ogievskiy } 94086f8cdf3SVladimir Sementsov-Ogievskiy 94186f8cdf3SVladimir Sementsov-Ogievskiy /* 94286f8cdf3SVladimir Sementsov-Ogievskiy * NBD_FOREACH_REPLY_CHUNK 94386f8cdf3SVladimir Sementsov-Ogievskiy * The pointer stored in @payload requires g_free() to free it. 94486f8cdf3SVladimir Sementsov-Ogievskiy */ 94586f8cdf3SVladimir Sementsov-Ogievskiy #define NBD_FOREACH_REPLY_CHUNK(s, iter, handle, structured, \ 94686f8cdf3SVladimir Sementsov-Ogievskiy qiov, reply, payload) \ 94786f8cdf3SVladimir Sementsov-Ogievskiy for (iter = (NBDReplyChunkIter) { .only_structured = structured }; \ 94886f8cdf3SVladimir Sementsov-Ogievskiy nbd_reply_chunk_iter_receive(s, &iter, handle, qiov, reply, payload);) 94986f8cdf3SVladimir Sementsov-Ogievskiy 95086f8cdf3SVladimir Sementsov-Ogievskiy /* 95186f8cdf3SVladimir Sementsov-Ogievskiy * nbd_reply_chunk_iter_receive 95286f8cdf3SVladimir Sementsov-Ogievskiy * The pointer stored in @payload requires g_free() to free it. 95386f8cdf3SVladimir Sementsov-Ogievskiy */ 954611ae1d7SVladimir Sementsov-Ogievskiy static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s, 95586f8cdf3SVladimir Sementsov-Ogievskiy NBDReplyChunkIter *iter, 95686f8cdf3SVladimir Sementsov-Ogievskiy uint64_t handle, 95786f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector *qiov, NBDReply *reply, 95886f8cdf3SVladimir Sementsov-Ogievskiy void **payload) 95986f8cdf3SVladimir Sementsov-Ogievskiy { 96086f8cdf3SVladimir Sementsov-Ogievskiy int ret, request_ret; 96186f8cdf3SVladimir Sementsov-Ogievskiy NBDReply local_reply; 96286f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk; 96386f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 964a34b1e5eSVladimir Sementsov-Ogievskiy if (s->state != NBD_CLIENT_CONNECTED) { 96586f8cdf3SVladimir Sementsov-Ogievskiy error_setg(&local_err, "Connection closed"); 96686f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(iter, -EIO, &local_err); 96786f8cdf3SVladimir Sementsov-Ogievskiy goto break_loop; 96886f8cdf3SVladimir Sementsov-Ogievskiy } 96986f8cdf3SVladimir Sementsov-Ogievskiy 97086f8cdf3SVladimir Sementsov-Ogievskiy if (iter->done) { 97186f8cdf3SVladimir Sementsov-Ogievskiy /* Previous iteration was last. */ 97286f8cdf3SVladimir Sementsov-Ogievskiy goto break_loop; 97386f8cdf3SVladimir Sementsov-Ogievskiy } 97486f8cdf3SVladimir Sementsov-Ogievskiy 97586f8cdf3SVladimir Sementsov-Ogievskiy if (reply == NULL) { 97686f8cdf3SVladimir Sementsov-Ogievskiy reply = &local_reply; 97786f8cdf3SVladimir Sementsov-Ogievskiy } 97886f8cdf3SVladimir Sementsov-Ogievskiy 97986f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_co_receive_one_chunk(s, handle, iter->only_structured, 98086f8cdf3SVladimir Sementsov-Ogievskiy &request_ret, qiov, reply, payload, 98186f8cdf3SVladimir Sementsov-Ogievskiy &local_err); 98286f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 98386f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(iter, ret, &local_err); 98486f8cdf3SVladimir Sementsov-Ogievskiy } else if (request_ret < 0) { 98586f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_request_error(iter, request_ret); 98686f8cdf3SVladimir Sementsov-Ogievskiy } 98786f8cdf3SVladimir Sementsov-Ogievskiy 98886f8cdf3SVladimir Sementsov-Ogievskiy /* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */ 989a34b1e5eSVladimir Sementsov-Ogievskiy if (nbd_reply_is_simple(reply) || s->state != NBD_CLIENT_CONNECTED) { 99086f8cdf3SVladimir Sementsov-Ogievskiy goto break_loop; 99186f8cdf3SVladimir Sementsov-Ogievskiy } 99286f8cdf3SVladimir Sementsov-Ogievskiy 99386f8cdf3SVladimir Sementsov-Ogievskiy chunk = &reply->structured; 99486f8cdf3SVladimir Sementsov-Ogievskiy iter->only_structured = true; 99586f8cdf3SVladimir Sementsov-Ogievskiy 99686f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->type == NBD_REPLY_TYPE_NONE) { 99786f8cdf3SVladimir Sementsov-Ogievskiy /* NBD_REPLY_FLAG_DONE is already checked in nbd_co_receive_one_chunk */ 99886f8cdf3SVladimir Sementsov-Ogievskiy assert(chunk->flags & NBD_REPLY_FLAG_DONE); 99986f8cdf3SVladimir Sementsov-Ogievskiy goto break_loop; 100086f8cdf3SVladimir Sementsov-Ogievskiy } 100186f8cdf3SVladimir Sementsov-Ogievskiy 100286f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->flags & NBD_REPLY_FLAG_DONE) { 100386f8cdf3SVladimir Sementsov-Ogievskiy /* This iteration is last. */ 100486f8cdf3SVladimir Sementsov-Ogievskiy iter->done = true; 100586f8cdf3SVladimir Sementsov-Ogievskiy } 100686f8cdf3SVladimir Sementsov-Ogievskiy 100786f8cdf3SVladimir Sementsov-Ogievskiy /* Execute the loop body */ 100886f8cdf3SVladimir Sementsov-Ogievskiy return true; 100986f8cdf3SVladimir Sementsov-Ogievskiy 101086f8cdf3SVladimir Sementsov-Ogievskiy break_loop: 101186f8cdf3SVladimir Sementsov-Ogievskiy s->requests[HANDLE_TO_INDEX(s, handle)].coroutine = NULL; 101286f8cdf3SVladimir Sementsov-Ogievskiy 101386f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_mutex_lock(&s->send_mutex); 101486f8cdf3SVladimir Sementsov-Ogievskiy s->in_flight--; 1015f7651539SVladimir Sementsov-Ogievskiy if (s->in_flight == 0 && s->wait_in_flight) { 1016f7651539SVladimir Sementsov-Ogievskiy aio_co_wake(s->connection_co); 1017f7651539SVladimir Sementsov-Ogievskiy } else { 101886f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_queue_next(&s->free_sema); 1019f7651539SVladimir Sementsov-Ogievskiy } 102086f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_mutex_unlock(&s->send_mutex); 102186f8cdf3SVladimir Sementsov-Ogievskiy 102286f8cdf3SVladimir Sementsov-Ogievskiy return false; 102386f8cdf3SVladimir Sementsov-Ogievskiy } 102486f8cdf3SVladimir Sementsov-Ogievskiy 1025611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_co_receive_return_code(BDRVNBDState *s, uint64_t handle, 102686f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, Error **errp) 102786f8cdf3SVladimir Sementsov-Ogievskiy { 102886f8cdf3SVladimir Sementsov-Ogievskiy NBDReplyChunkIter iter; 102986f8cdf3SVladimir Sementsov-Ogievskiy 103086f8cdf3SVladimir Sementsov-Ogievskiy NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, NULL, NULL) { 103186f8cdf3SVladimir Sementsov-Ogievskiy /* nbd_reply_chunk_iter_receive does all the work */ 103286f8cdf3SVladimir Sementsov-Ogievskiy } 103386f8cdf3SVladimir Sementsov-Ogievskiy 103486f8cdf3SVladimir Sementsov-Ogievskiy error_propagate(errp, iter.err); 103586f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = iter.request_ret; 103686f8cdf3SVladimir Sementsov-Ogievskiy return iter.ret; 103786f8cdf3SVladimir Sementsov-Ogievskiy } 103886f8cdf3SVladimir Sementsov-Ogievskiy 1039611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_t handle, 104086f8cdf3SVladimir Sementsov-Ogievskiy uint64_t offset, QEMUIOVector *qiov, 104186f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, Error **errp) 104286f8cdf3SVladimir Sementsov-Ogievskiy { 104386f8cdf3SVladimir Sementsov-Ogievskiy NBDReplyChunkIter iter; 104486f8cdf3SVladimir Sementsov-Ogievskiy NBDReply reply; 104586f8cdf3SVladimir Sementsov-Ogievskiy void *payload = NULL; 104686f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 104786f8cdf3SVladimir Sementsov-Ogievskiy 104886f8cdf3SVladimir Sementsov-Ogievskiy NBD_FOREACH_REPLY_CHUNK(s, iter, handle, s->info.structured_reply, 104986f8cdf3SVladimir Sementsov-Ogievskiy qiov, &reply, &payload) 105086f8cdf3SVladimir Sementsov-Ogievskiy { 105186f8cdf3SVladimir Sementsov-Ogievskiy int ret; 105286f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk = &reply.structured; 105386f8cdf3SVladimir Sementsov-Ogievskiy 105486f8cdf3SVladimir Sementsov-Ogievskiy assert(nbd_reply_is_structured(&reply)); 105586f8cdf3SVladimir Sementsov-Ogievskiy 105686f8cdf3SVladimir Sementsov-Ogievskiy switch (chunk->type) { 105786f8cdf3SVladimir Sementsov-Ogievskiy case NBD_REPLY_TYPE_OFFSET_DATA: 105886f8cdf3SVladimir Sementsov-Ogievskiy /* 105986f8cdf3SVladimir Sementsov-Ogievskiy * special cased in nbd_co_receive_one_chunk, data is already 106086f8cdf3SVladimir Sementsov-Ogievskiy * in qiov 106186f8cdf3SVladimir Sementsov-Ogievskiy */ 106286f8cdf3SVladimir Sementsov-Ogievskiy break; 106386f8cdf3SVladimir Sementsov-Ogievskiy case NBD_REPLY_TYPE_OFFSET_HOLE: 106486f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_parse_offset_hole_payload(s, &reply.structured, payload, 106586f8cdf3SVladimir Sementsov-Ogievskiy offset, qiov, &local_err); 106686f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1067a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, ret); 106886f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, ret, &local_err); 106986f8cdf3SVladimir Sementsov-Ogievskiy } 107086f8cdf3SVladimir Sementsov-Ogievskiy break; 107186f8cdf3SVladimir Sementsov-Ogievskiy default: 107286f8cdf3SVladimir Sementsov-Ogievskiy if (!nbd_reply_type_is_error(chunk->type)) { 107386f8cdf3SVladimir Sementsov-Ogievskiy /* not allowed reply type */ 1074a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, -EINVAL); 107586f8cdf3SVladimir Sementsov-Ogievskiy error_setg(&local_err, 107686f8cdf3SVladimir Sementsov-Ogievskiy "Unexpected reply type: %d (%s) for CMD_READ", 107786f8cdf3SVladimir Sementsov-Ogievskiy chunk->type, nbd_reply_type_lookup(chunk->type)); 107886f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, -EINVAL, &local_err); 107986f8cdf3SVladimir Sementsov-Ogievskiy } 108086f8cdf3SVladimir Sementsov-Ogievskiy } 108186f8cdf3SVladimir Sementsov-Ogievskiy 108286f8cdf3SVladimir Sementsov-Ogievskiy g_free(payload); 108386f8cdf3SVladimir Sementsov-Ogievskiy payload = NULL; 108486f8cdf3SVladimir Sementsov-Ogievskiy } 108586f8cdf3SVladimir Sementsov-Ogievskiy 108686f8cdf3SVladimir Sementsov-Ogievskiy error_propagate(errp, iter.err); 108786f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = iter.request_ret; 108886f8cdf3SVladimir Sementsov-Ogievskiy return iter.ret; 108986f8cdf3SVladimir Sementsov-Ogievskiy } 109086f8cdf3SVladimir Sementsov-Ogievskiy 1091611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_co_receive_blockstatus_reply(BDRVNBDState *s, 109286f8cdf3SVladimir Sementsov-Ogievskiy uint64_t handle, uint64_t length, 109386f8cdf3SVladimir Sementsov-Ogievskiy NBDExtent *extent, 109486f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, Error **errp) 109586f8cdf3SVladimir Sementsov-Ogievskiy { 109686f8cdf3SVladimir Sementsov-Ogievskiy NBDReplyChunkIter iter; 109786f8cdf3SVladimir Sementsov-Ogievskiy NBDReply reply; 109886f8cdf3SVladimir Sementsov-Ogievskiy void *payload = NULL; 109986f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 110086f8cdf3SVladimir Sementsov-Ogievskiy bool received = false; 110186f8cdf3SVladimir Sementsov-Ogievskiy 110286f8cdf3SVladimir Sementsov-Ogievskiy assert(!extent->length); 110386f8cdf3SVladimir Sementsov-Ogievskiy NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, &reply, &payload) { 110486f8cdf3SVladimir Sementsov-Ogievskiy int ret; 110586f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk = &reply.structured; 110686f8cdf3SVladimir Sementsov-Ogievskiy 110786f8cdf3SVladimir Sementsov-Ogievskiy assert(nbd_reply_is_structured(&reply)); 110886f8cdf3SVladimir Sementsov-Ogievskiy 110986f8cdf3SVladimir Sementsov-Ogievskiy switch (chunk->type) { 111086f8cdf3SVladimir Sementsov-Ogievskiy case NBD_REPLY_TYPE_BLOCK_STATUS: 111186f8cdf3SVladimir Sementsov-Ogievskiy if (received) { 1112a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, -EINVAL); 111386f8cdf3SVladimir Sementsov-Ogievskiy error_setg(&local_err, "Several BLOCK_STATUS chunks in reply"); 111486f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, -EINVAL, &local_err); 111586f8cdf3SVladimir Sementsov-Ogievskiy } 111686f8cdf3SVladimir Sementsov-Ogievskiy received = true; 111786f8cdf3SVladimir Sementsov-Ogievskiy 111886f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_parse_blockstatus_payload(s, &reply.structured, 111986f8cdf3SVladimir Sementsov-Ogievskiy payload, length, extent, 112086f8cdf3SVladimir Sementsov-Ogievskiy &local_err); 112186f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1122a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, ret); 112386f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, ret, &local_err); 112486f8cdf3SVladimir Sementsov-Ogievskiy } 112586f8cdf3SVladimir Sementsov-Ogievskiy break; 112686f8cdf3SVladimir Sementsov-Ogievskiy default: 112786f8cdf3SVladimir Sementsov-Ogievskiy if (!nbd_reply_type_is_error(chunk->type)) { 1128a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, -EINVAL); 112986f8cdf3SVladimir Sementsov-Ogievskiy error_setg(&local_err, 113086f8cdf3SVladimir Sementsov-Ogievskiy "Unexpected reply type: %d (%s) " 113186f8cdf3SVladimir Sementsov-Ogievskiy "for CMD_BLOCK_STATUS", 113286f8cdf3SVladimir Sementsov-Ogievskiy chunk->type, nbd_reply_type_lookup(chunk->type)); 113386f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, -EINVAL, &local_err); 113486f8cdf3SVladimir Sementsov-Ogievskiy } 113586f8cdf3SVladimir Sementsov-Ogievskiy } 113686f8cdf3SVladimir Sementsov-Ogievskiy 113786f8cdf3SVladimir Sementsov-Ogievskiy g_free(payload); 113886f8cdf3SVladimir Sementsov-Ogievskiy payload = NULL; 113986f8cdf3SVladimir Sementsov-Ogievskiy } 114086f8cdf3SVladimir Sementsov-Ogievskiy 114186f8cdf3SVladimir Sementsov-Ogievskiy if (!extent->length && !iter.request_ret) { 114286f8cdf3SVladimir Sementsov-Ogievskiy error_setg(&local_err, "Server did not reply with any status extents"); 114386f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, -EIO, &local_err); 114486f8cdf3SVladimir Sementsov-Ogievskiy } 114586f8cdf3SVladimir Sementsov-Ogievskiy 114686f8cdf3SVladimir Sementsov-Ogievskiy error_propagate(errp, iter.err); 114786f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = iter.request_ret; 114886f8cdf3SVladimir Sementsov-Ogievskiy return iter.ret; 114986f8cdf3SVladimir Sementsov-Ogievskiy } 115086f8cdf3SVladimir Sementsov-Ogievskiy 115186f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_co_request(BlockDriverState *bs, NBDRequest *request, 115286f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector *write_qiov) 115386f8cdf3SVladimir Sementsov-Ogievskiy { 115486f8cdf3SVladimir Sementsov-Ogievskiy int ret, request_ret; 115586f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 1156611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 115786f8cdf3SVladimir Sementsov-Ogievskiy 115886f8cdf3SVladimir Sementsov-Ogievskiy assert(request->type != NBD_CMD_READ); 115986f8cdf3SVladimir Sementsov-Ogievskiy if (write_qiov) { 116086f8cdf3SVladimir Sementsov-Ogievskiy assert(request->type == NBD_CMD_WRITE); 116186f8cdf3SVladimir Sementsov-Ogievskiy assert(request->len == iov_size(write_qiov->iov, write_qiov->niov)); 116286f8cdf3SVladimir Sementsov-Ogievskiy } else { 116386f8cdf3SVladimir Sementsov-Ogievskiy assert(request->type != NBD_CMD_WRITE); 116486f8cdf3SVladimir Sementsov-Ogievskiy } 1165f7651539SVladimir Sementsov-Ogievskiy 1166f7651539SVladimir Sementsov-Ogievskiy do { 116786f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_co_send_request(bs, request, write_qiov); 116886f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1169f7651539SVladimir Sementsov-Ogievskiy continue; 117086f8cdf3SVladimir Sementsov-Ogievskiy } 117186f8cdf3SVladimir Sementsov-Ogievskiy 1172611ae1d7SVladimir Sementsov-Ogievskiy ret = nbd_co_receive_return_code(s, request->handle, 117386f8cdf3SVladimir Sementsov-Ogievskiy &request_ret, &local_err); 117486f8cdf3SVladimir Sementsov-Ogievskiy if (local_err) { 1175f7651539SVladimir Sementsov-Ogievskiy trace_nbd_co_request_fail(request->from, request->len, 1176f7651539SVladimir Sementsov-Ogievskiy request->handle, request->flags, 1177f7651539SVladimir Sementsov-Ogievskiy request->type, 117886f8cdf3SVladimir Sementsov-Ogievskiy nbd_cmd_lookup(request->type), 117986f8cdf3SVladimir Sementsov-Ogievskiy ret, error_get_pretty(local_err)); 118086f8cdf3SVladimir Sementsov-Ogievskiy error_free(local_err); 1181f7651539SVladimir Sementsov-Ogievskiy local_err = NULL; 118286f8cdf3SVladimir Sementsov-Ogievskiy } 1183f7651539SVladimir Sementsov-Ogievskiy } while (ret < 0 && nbd_client_connecting_wait(s)); 1184f7651539SVladimir Sementsov-Ogievskiy 118586f8cdf3SVladimir Sementsov-Ogievskiy return ret ? ret : request_ret; 118686f8cdf3SVladimir Sementsov-Ogievskiy } 118786f8cdf3SVladimir Sementsov-Ogievskiy 118886f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_client_co_preadv(BlockDriverState *bs, uint64_t offset, 118986f8cdf3SVladimir Sementsov-Ogievskiy uint64_t bytes, QEMUIOVector *qiov, int flags) 119086f8cdf3SVladimir Sementsov-Ogievskiy { 119186f8cdf3SVladimir Sementsov-Ogievskiy int ret, request_ret; 119286f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 1193611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 119486f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 119586f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_READ, 119686f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 119786f8cdf3SVladimir Sementsov-Ogievskiy .len = bytes, 119886f8cdf3SVladimir Sementsov-Ogievskiy }; 119986f8cdf3SVladimir Sementsov-Ogievskiy 120086f8cdf3SVladimir Sementsov-Ogievskiy assert(bytes <= NBD_MAX_BUFFER_SIZE); 120186f8cdf3SVladimir Sementsov-Ogievskiy assert(!flags); 120286f8cdf3SVladimir Sementsov-Ogievskiy 120386f8cdf3SVladimir Sementsov-Ogievskiy if (!bytes) { 120486f8cdf3SVladimir Sementsov-Ogievskiy return 0; 120586f8cdf3SVladimir Sementsov-Ogievskiy } 120686f8cdf3SVladimir Sementsov-Ogievskiy /* 120786f8cdf3SVladimir Sementsov-Ogievskiy * Work around the fact that the block layer doesn't do 120886f8cdf3SVladimir Sementsov-Ogievskiy * byte-accurate sizing yet - if the read exceeds the server's 120986f8cdf3SVladimir Sementsov-Ogievskiy * advertised size because the block layer rounded size up, then 121086f8cdf3SVladimir Sementsov-Ogievskiy * truncate the request to the server and tail-pad with zero. 121186f8cdf3SVladimir Sementsov-Ogievskiy */ 1212611ae1d7SVladimir Sementsov-Ogievskiy if (offset >= s->info.size) { 121386f8cdf3SVladimir Sementsov-Ogievskiy assert(bytes < BDRV_SECTOR_SIZE); 121486f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_memset(qiov, 0, 0, bytes); 121586f8cdf3SVladimir Sementsov-Ogievskiy return 0; 121686f8cdf3SVladimir Sementsov-Ogievskiy } 1217611ae1d7SVladimir Sementsov-Ogievskiy if (offset + bytes > s->info.size) { 1218611ae1d7SVladimir Sementsov-Ogievskiy uint64_t slop = offset + bytes - s->info.size; 121986f8cdf3SVladimir Sementsov-Ogievskiy 122086f8cdf3SVladimir Sementsov-Ogievskiy assert(slop < BDRV_SECTOR_SIZE); 122186f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_memset(qiov, bytes - slop, 0, slop); 122286f8cdf3SVladimir Sementsov-Ogievskiy request.len -= slop; 122386f8cdf3SVladimir Sementsov-Ogievskiy } 122486f8cdf3SVladimir Sementsov-Ogievskiy 1225f7651539SVladimir Sementsov-Ogievskiy do { 122686f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_co_send_request(bs, &request, NULL); 122786f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1228f7651539SVladimir Sementsov-Ogievskiy continue; 122986f8cdf3SVladimir Sementsov-Ogievskiy } 123086f8cdf3SVladimir Sementsov-Ogievskiy 1231611ae1d7SVladimir Sementsov-Ogievskiy ret = nbd_co_receive_cmdread_reply(s, request.handle, offset, qiov, 123286f8cdf3SVladimir Sementsov-Ogievskiy &request_ret, &local_err); 123386f8cdf3SVladimir Sementsov-Ogievskiy if (local_err) { 123486f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_co_request_fail(request.from, request.len, request.handle, 123586f8cdf3SVladimir Sementsov-Ogievskiy request.flags, request.type, 123686f8cdf3SVladimir Sementsov-Ogievskiy nbd_cmd_lookup(request.type), 123786f8cdf3SVladimir Sementsov-Ogievskiy ret, error_get_pretty(local_err)); 123886f8cdf3SVladimir Sementsov-Ogievskiy error_free(local_err); 1239f7651539SVladimir Sementsov-Ogievskiy local_err = NULL; 124086f8cdf3SVladimir Sementsov-Ogievskiy } 1241f7651539SVladimir Sementsov-Ogievskiy } while (ret < 0 && nbd_client_connecting_wait(s)); 1242f7651539SVladimir Sementsov-Ogievskiy 124386f8cdf3SVladimir Sementsov-Ogievskiy return ret ? ret : request_ret; 124486f8cdf3SVladimir Sementsov-Ogievskiy } 124586f8cdf3SVladimir Sementsov-Ogievskiy 124686f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_client_co_pwritev(BlockDriverState *bs, uint64_t offset, 124786f8cdf3SVladimir Sementsov-Ogievskiy uint64_t bytes, QEMUIOVector *qiov, int flags) 124886f8cdf3SVladimir Sementsov-Ogievskiy { 1249611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 125086f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 125186f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_WRITE, 125286f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 125386f8cdf3SVladimir Sementsov-Ogievskiy .len = bytes, 125486f8cdf3SVladimir Sementsov-Ogievskiy }; 125586f8cdf3SVladimir Sementsov-Ogievskiy 1256611ae1d7SVladimir Sementsov-Ogievskiy assert(!(s->info.flags & NBD_FLAG_READ_ONLY)); 125786f8cdf3SVladimir Sementsov-Ogievskiy if (flags & BDRV_REQ_FUA) { 1258611ae1d7SVladimir Sementsov-Ogievskiy assert(s->info.flags & NBD_FLAG_SEND_FUA); 125986f8cdf3SVladimir Sementsov-Ogievskiy request.flags |= NBD_CMD_FLAG_FUA; 126086f8cdf3SVladimir Sementsov-Ogievskiy } 126186f8cdf3SVladimir Sementsov-Ogievskiy 126286f8cdf3SVladimir Sementsov-Ogievskiy assert(bytes <= NBD_MAX_BUFFER_SIZE); 126386f8cdf3SVladimir Sementsov-Ogievskiy 126486f8cdf3SVladimir Sementsov-Ogievskiy if (!bytes) { 126586f8cdf3SVladimir Sementsov-Ogievskiy return 0; 126686f8cdf3SVladimir Sementsov-Ogievskiy } 126786f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_request(bs, &request, qiov); 126886f8cdf3SVladimir Sementsov-Ogievskiy } 126986f8cdf3SVladimir Sementsov-Ogievskiy 127086f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_client_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, 127186f8cdf3SVladimir Sementsov-Ogievskiy int bytes, BdrvRequestFlags flags) 127286f8cdf3SVladimir Sementsov-Ogievskiy { 1273611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 127486f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 127586f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_WRITE_ZEROES, 127686f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 127786f8cdf3SVladimir Sementsov-Ogievskiy .len = bytes, 127886f8cdf3SVladimir Sementsov-Ogievskiy }; 127986f8cdf3SVladimir Sementsov-Ogievskiy 1280611ae1d7SVladimir Sementsov-Ogievskiy assert(!(s->info.flags & NBD_FLAG_READ_ONLY)); 1281611ae1d7SVladimir Sementsov-Ogievskiy if (!(s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES)) { 128286f8cdf3SVladimir Sementsov-Ogievskiy return -ENOTSUP; 128386f8cdf3SVladimir Sementsov-Ogievskiy } 128486f8cdf3SVladimir Sementsov-Ogievskiy 128586f8cdf3SVladimir Sementsov-Ogievskiy if (flags & BDRV_REQ_FUA) { 1286611ae1d7SVladimir Sementsov-Ogievskiy assert(s->info.flags & NBD_FLAG_SEND_FUA); 128786f8cdf3SVladimir Sementsov-Ogievskiy request.flags |= NBD_CMD_FLAG_FUA; 128886f8cdf3SVladimir Sementsov-Ogievskiy } 128986f8cdf3SVladimir Sementsov-Ogievskiy if (!(flags & BDRV_REQ_MAY_UNMAP)) { 129086f8cdf3SVladimir Sementsov-Ogievskiy request.flags |= NBD_CMD_FLAG_NO_HOLE; 129186f8cdf3SVladimir Sementsov-Ogievskiy } 1292f061656cSEric Blake if (flags & BDRV_REQ_NO_FALLBACK) { 1293f061656cSEric Blake assert(s->info.flags & NBD_FLAG_SEND_FAST_ZERO); 1294f061656cSEric Blake request.flags |= NBD_CMD_FLAG_FAST_ZERO; 1295f061656cSEric Blake } 129686f8cdf3SVladimir Sementsov-Ogievskiy 129786f8cdf3SVladimir Sementsov-Ogievskiy if (!bytes) { 129886f8cdf3SVladimir Sementsov-Ogievskiy return 0; 129986f8cdf3SVladimir Sementsov-Ogievskiy } 130086f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_request(bs, &request, NULL); 130186f8cdf3SVladimir Sementsov-Ogievskiy } 130286f8cdf3SVladimir Sementsov-Ogievskiy 130386f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_client_co_flush(BlockDriverState *bs) 130486f8cdf3SVladimir Sementsov-Ogievskiy { 1305611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 130686f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { .type = NBD_CMD_FLUSH }; 130786f8cdf3SVladimir Sementsov-Ogievskiy 1308611ae1d7SVladimir Sementsov-Ogievskiy if (!(s->info.flags & NBD_FLAG_SEND_FLUSH)) { 130986f8cdf3SVladimir Sementsov-Ogievskiy return 0; 131086f8cdf3SVladimir Sementsov-Ogievskiy } 131186f8cdf3SVladimir Sementsov-Ogievskiy 131286f8cdf3SVladimir Sementsov-Ogievskiy request.from = 0; 131386f8cdf3SVladimir Sementsov-Ogievskiy request.len = 0; 131486f8cdf3SVladimir Sementsov-Ogievskiy 131586f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_request(bs, &request, NULL); 131686f8cdf3SVladimir Sementsov-Ogievskiy } 131786f8cdf3SVladimir Sementsov-Ogievskiy 131886f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_client_co_pdiscard(BlockDriverState *bs, int64_t offset, 131986f8cdf3SVladimir Sementsov-Ogievskiy int bytes) 132086f8cdf3SVladimir Sementsov-Ogievskiy { 1321611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 132286f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 132386f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_TRIM, 132486f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 132586f8cdf3SVladimir Sementsov-Ogievskiy .len = bytes, 132686f8cdf3SVladimir Sementsov-Ogievskiy }; 132786f8cdf3SVladimir Sementsov-Ogievskiy 1328611ae1d7SVladimir Sementsov-Ogievskiy assert(!(s->info.flags & NBD_FLAG_READ_ONLY)); 1329611ae1d7SVladimir Sementsov-Ogievskiy if (!(s->info.flags & NBD_FLAG_SEND_TRIM) || !bytes) { 133086f8cdf3SVladimir Sementsov-Ogievskiy return 0; 133186f8cdf3SVladimir Sementsov-Ogievskiy } 133286f8cdf3SVladimir Sementsov-Ogievskiy 133386f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_request(bs, &request, NULL); 133486f8cdf3SVladimir Sementsov-Ogievskiy } 133586f8cdf3SVladimir Sementsov-Ogievskiy 133686f8cdf3SVladimir Sementsov-Ogievskiy static int coroutine_fn nbd_client_co_block_status( 133786f8cdf3SVladimir Sementsov-Ogievskiy BlockDriverState *bs, bool want_zero, int64_t offset, int64_t bytes, 133886f8cdf3SVladimir Sementsov-Ogievskiy int64_t *pnum, int64_t *map, BlockDriverState **file) 133986f8cdf3SVladimir Sementsov-Ogievskiy { 134086f8cdf3SVladimir Sementsov-Ogievskiy int ret, request_ret; 134186f8cdf3SVladimir Sementsov-Ogievskiy NBDExtent extent = { 0 }; 1342611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 134386f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 134486f8cdf3SVladimir Sementsov-Ogievskiy 134586f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 134686f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_BLOCK_STATUS, 134786f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 13486bf792b4SVladimir Sementsov-Ogievskiy .len = MIN(QEMU_ALIGN_DOWN(INT_MAX, bs->bl.request_alignment), 1349611ae1d7SVladimir Sementsov-Ogievskiy MIN(bytes, s->info.size - offset)), 135086f8cdf3SVladimir Sementsov-Ogievskiy .flags = NBD_CMD_FLAG_REQ_ONE, 135186f8cdf3SVladimir Sementsov-Ogievskiy }; 135286f8cdf3SVladimir Sementsov-Ogievskiy 1353611ae1d7SVladimir Sementsov-Ogievskiy if (!s->info.base_allocation) { 135486f8cdf3SVladimir Sementsov-Ogievskiy *pnum = bytes; 135586f8cdf3SVladimir Sementsov-Ogievskiy *map = offset; 135686f8cdf3SVladimir Sementsov-Ogievskiy *file = bs; 135786f8cdf3SVladimir Sementsov-Ogievskiy return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; 135886f8cdf3SVladimir Sementsov-Ogievskiy } 135986f8cdf3SVladimir Sementsov-Ogievskiy 136086f8cdf3SVladimir Sementsov-Ogievskiy /* 136186f8cdf3SVladimir Sementsov-Ogievskiy * Work around the fact that the block layer doesn't do 136286f8cdf3SVladimir Sementsov-Ogievskiy * byte-accurate sizing yet - if the status request exceeds the 136386f8cdf3SVladimir Sementsov-Ogievskiy * server's advertised size because the block layer rounded size 136486f8cdf3SVladimir Sementsov-Ogievskiy * up, we truncated the request to the server (above), or are 136586f8cdf3SVladimir Sementsov-Ogievskiy * called on just the hole. 136686f8cdf3SVladimir Sementsov-Ogievskiy */ 1367611ae1d7SVladimir Sementsov-Ogievskiy if (offset >= s->info.size) { 136886f8cdf3SVladimir Sementsov-Ogievskiy *pnum = bytes; 136986f8cdf3SVladimir Sementsov-Ogievskiy assert(bytes < BDRV_SECTOR_SIZE); 137086f8cdf3SVladimir Sementsov-Ogievskiy /* Intentionally don't report offset_valid for the hole */ 137186f8cdf3SVladimir Sementsov-Ogievskiy return BDRV_BLOCK_ZERO; 137286f8cdf3SVladimir Sementsov-Ogievskiy } 137386f8cdf3SVladimir Sementsov-Ogievskiy 1374611ae1d7SVladimir Sementsov-Ogievskiy if (s->info.min_block) { 1375611ae1d7SVladimir Sementsov-Ogievskiy assert(QEMU_IS_ALIGNED(request.len, s->info.min_block)); 137686f8cdf3SVladimir Sementsov-Ogievskiy } 1377f7651539SVladimir Sementsov-Ogievskiy do { 137886f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_co_send_request(bs, &request, NULL); 137986f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1380f7651539SVladimir Sementsov-Ogievskiy continue; 138186f8cdf3SVladimir Sementsov-Ogievskiy } 138286f8cdf3SVladimir Sementsov-Ogievskiy 1383611ae1d7SVladimir Sementsov-Ogievskiy ret = nbd_co_receive_blockstatus_reply(s, request.handle, bytes, 1384f7651539SVladimir Sementsov-Ogievskiy &extent, &request_ret, 1385f7651539SVladimir Sementsov-Ogievskiy &local_err); 138686f8cdf3SVladimir Sementsov-Ogievskiy if (local_err) { 138786f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_co_request_fail(request.from, request.len, request.handle, 138886f8cdf3SVladimir Sementsov-Ogievskiy request.flags, request.type, 138986f8cdf3SVladimir Sementsov-Ogievskiy nbd_cmd_lookup(request.type), 139086f8cdf3SVladimir Sementsov-Ogievskiy ret, error_get_pretty(local_err)); 139186f8cdf3SVladimir Sementsov-Ogievskiy error_free(local_err); 1392f7651539SVladimir Sementsov-Ogievskiy local_err = NULL; 139386f8cdf3SVladimir Sementsov-Ogievskiy } 1394f7651539SVladimir Sementsov-Ogievskiy } while (ret < 0 && nbd_client_connecting_wait(s)); 1395f7651539SVladimir Sementsov-Ogievskiy 139686f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0 || request_ret < 0) { 139786f8cdf3SVladimir Sementsov-Ogievskiy return ret ? ret : request_ret; 139886f8cdf3SVladimir Sementsov-Ogievskiy } 139986f8cdf3SVladimir Sementsov-Ogievskiy 140086f8cdf3SVladimir Sementsov-Ogievskiy assert(extent.length); 140186f8cdf3SVladimir Sementsov-Ogievskiy *pnum = extent.length; 140286f8cdf3SVladimir Sementsov-Ogievskiy *map = offset; 140386f8cdf3SVladimir Sementsov-Ogievskiy *file = bs; 140486f8cdf3SVladimir Sementsov-Ogievskiy return (extent.flags & NBD_STATE_HOLE ? 0 : BDRV_BLOCK_DATA) | 140586f8cdf3SVladimir Sementsov-Ogievskiy (extent.flags & NBD_STATE_ZERO ? BDRV_BLOCK_ZERO : 0) | 140686f8cdf3SVladimir Sementsov-Ogievskiy BDRV_BLOCK_OFFSET_VALID; 140786f8cdf3SVladimir Sementsov-Ogievskiy } 140886f8cdf3SVladimir Sementsov-Ogievskiy 1409e99754b4SMaxim Levitsky static int nbd_client_reopen_prepare(BDRVReopenState *state, 1410e99754b4SMaxim Levitsky BlockReopenQueue *queue, Error **errp) 1411e99754b4SMaxim Levitsky { 1412e99754b4SMaxim Levitsky BDRVNBDState *s = (BDRVNBDState *)state->bs->opaque; 1413e99754b4SMaxim Levitsky 1414e99754b4SMaxim Levitsky if ((state->flags & BDRV_O_RDWR) && (s->info.flags & NBD_FLAG_READ_ONLY)) { 1415e99754b4SMaxim Levitsky error_setg(errp, "Can't reopen read-only NBD mount as read/write"); 1416e99754b4SMaxim Levitsky return -EACCES; 1417e99754b4SMaxim Levitsky } 1418e99754b4SMaxim Levitsky return 0; 1419e99754b4SMaxim Levitsky } 1420e99754b4SMaxim Levitsky 142186f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_client_close(BlockDriverState *bs) 142286f8cdf3SVladimir Sementsov-Ogievskiy { 1423611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 142486f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { .type = NBD_CMD_DISC }; 142586f8cdf3SVladimir Sementsov-Ogievskiy 1426f7651539SVladimir Sementsov-Ogievskiy if (s->ioc) { 1427611ae1d7SVladimir Sementsov-Ogievskiy nbd_send_request(s->ioc, &request); 1428f7651539SVladimir Sementsov-Ogievskiy } 142986f8cdf3SVladimir Sementsov-Ogievskiy 143086f8cdf3SVladimir Sementsov-Ogievskiy nbd_teardown_connection(bs); 143186f8cdf3SVladimir Sementsov-Ogievskiy } 143286f8cdf3SVladimir Sementsov-Ogievskiy 143386f8cdf3SVladimir Sementsov-Ogievskiy static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr, 143486f8cdf3SVladimir Sementsov-Ogievskiy Error **errp) 143586f8cdf3SVladimir Sementsov-Ogievskiy { 1436795d946dSVladimir Sementsov-Ogievskiy ERRP_GUARD(); 143786f8cdf3SVladimir Sementsov-Ogievskiy QIOChannelSocket *sioc; 143886f8cdf3SVladimir Sementsov-Ogievskiy 143986f8cdf3SVladimir Sementsov-Ogievskiy sioc = qio_channel_socket_new(); 144086f8cdf3SVladimir Sementsov-Ogievskiy qio_channel_set_name(QIO_CHANNEL(sioc), "nbd-client"); 144186f8cdf3SVladimir Sementsov-Ogievskiy 1442795d946dSVladimir Sementsov-Ogievskiy qio_channel_socket_connect_sync(sioc, saddr, errp); 1443795d946dSVladimir Sementsov-Ogievskiy if (*errp) { 144486f8cdf3SVladimir Sementsov-Ogievskiy object_unref(OBJECT(sioc)); 144586f8cdf3SVladimir Sementsov-Ogievskiy return NULL; 144686f8cdf3SVladimir Sementsov-Ogievskiy } 144786f8cdf3SVladimir Sementsov-Ogievskiy 144886f8cdf3SVladimir Sementsov-Ogievskiy qio_channel_set_delay(QIO_CHANNEL(sioc), false); 144986f8cdf3SVladimir Sementsov-Ogievskiy 145086f8cdf3SVladimir Sementsov-Ogievskiy return sioc; 145186f8cdf3SVladimir Sementsov-Ogievskiy } 145286f8cdf3SVladimir Sementsov-Ogievskiy 1453fa35591bSVladimir Sementsov-Ogievskiy /* nbd_client_handshake takes ownership on sioc. On failure it is unref'ed. */ 1454fa35591bSVladimir Sementsov-Ogievskiy static int nbd_client_handshake(BlockDriverState *bs, QIOChannelSocket *sioc, 1455fa35591bSVladimir Sementsov-Ogievskiy Error **errp) 145686f8cdf3SVladimir Sementsov-Ogievskiy { 1457611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 1458a8e2bb6aSVladimir Sementsov-Ogievskiy AioContext *aio_context = bdrv_get_aio_context(bs); 145986f8cdf3SVladimir Sementsov-Ogievskiy int ret; 146086f8cdf3SVladimir Sementsov-Ogievskiy 1461fa35591bSVladimir Sementsov-Ogievskiy trace_nbd_client_handshake(s->export); 1462a8e2bb6aSVladimir Sementsov-Ogievskiy qio_channel_set_blocking(QIO_CHANNEL(sioc), false, NULL); 1463a8e2bb6aSVladimir Sementsov-Ogievskiy qio_channel_attach_aio_context(QIO_CHANNEL(sioc), aio_context); 146486f8cdf3SVladimir Sementsov-Ogievskiy 1465611ae1d7SVladimir Sementsov-Ogievskiy s->info.request_sizes = true; 1466611ae1d7SVladimir Sementsov-Ogievskiy s->info.structured_reply = true; 1467611ae1d7SVladimir Sementsov-Ogievskiy s->info.base_allocation = true; 14688f071c9dSVladimir Sementsov-Ogievskiy s->info.x_dirty_bitmap = g_strdup(s->x_dirty_bitmap); 14698f071c9dSVladimir Sementsov-Ogievskiy s->info.name = g_strdup(s->export ?: ""); 14708f071c9dSVladimir Sementsov-Ogievskiy ret = nbd_receive_negotiate(aio_context, QIO_CHANNEL(sioc), s->tlscreds, 14718f071c9dSVladimir Sementsov-Ogievskiy s->hostname, &s->ioc, &s->info, errp); 1472611ae1d7SVladimir Sementsov-Ogievskiy g_free(s->info.x_dirty_bitmap); 1473611ae1d7SVladimir Sementsov-Ogievskiy g_free(s->info.name); 147486f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 147586f8cdf3SVladimir Sementsov-Ogievskiy object_unref(OBJECT(sioc)); 147686f8cdf3SVladimir Sementsov-Ogievskiy return ret; 147786f8cdf3SVladimir Sementsov-Ogievskiy } 14788f071c9dSVladimir Sementsov-Ogievskiy if (s->x_dirty_bitmap && !s->info.base_allocation) { 147986f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "requested x-dirty-bitmap %s not found", 14808f071c9dSVladimir Sementsov-Ogievskiy s->x_dirty_bitmap); 148186f8cdf3SVladimir Sementsov-Ogievskiy ret = -EINVAL; 148286f8cdf3SVladimir Sementsov-Ogievskiy goto fail; 148386f8cdf3SVladimir Sementsov-Ogievskiy } 1484611ae1d7SVladimir Sementsov-Ogievskiy if (s->info.flags & NBD_FLAG_READ_ONLY) { 148586f8cdf3SVladimir Sementsov-Ogievskiy ret = bdrv_apply_auto_read_only(bs, "NBD export is read-only", errp); 148686f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 148786f8cdf3SVladimir Sementsov-Ogievskiy goto fail; 148886f8cdf3SVladimir Sementsov-Ogievskiy } 148986f8cdf3SVladimir Sementsov-Ogievskiy } 1490611ae1d7SVladimir Sementsov-Ogievskiy if (s->info.flags & NBD_FLAG_SEND_FUA) { 149186f8cdf3SVladimir Sementsov-Ogievskiy bs->supported_write_flags = BDRV_REQ_FUA; 149286f8cdf3SVladimir Sementsov-Ogievskiy bs->supported_zero_flags |= BDRV_REQ_FUA; 149386f8cdf3SVladimir Sementsov-Ogievskiy } 1494611ae1d7SVladimir Sementsov-Ogievskiy if (s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES) { 149586f8cdf3SVladimir Sementsov-Ogievskiy bs->supported_zero_flags |= BDRV_REQ_MAY_UNMAP; 1496f061656cSEric Blake if (s->info.flags & NBD_FLAG_SEND_FAST_ZERO) { 1497f061656cSEric Blake bs->supported_zero_flags |= BDRV_REQ_NO_FALLBACK; 1498f061656cSEric Blake } 149986f8cdf3SVladimir Sementsov-Ogievskiy } 150086f8cdf3SVladimir Sementsov-Ogievskiy 1501611ae1d7SVladimir Sementsov-Ogievskiy s->sioc = sioc; 150286f8cdf3SVladimir Sementsov-Ogievskiy 1503611ae1d7SVladimir Sementsov-Ogievskiy if (!s->ioc) { 1504611ae1d7SVladimir Sementsov-Ogievskiy s->ioc = QIO_CHANNEL(sioc); 1505611ae1d7SVladimir Sementsov-Ogievskiy object_ref(OBJECT(s->ioc)); 150686f8cdf3SVladimir Sementsov-Ogievskiy } 150786f8cdf3SVladimir Sementsov-Ogievskiy 1508fa35591bSVladimir Sementsov-Ogievskiy trace_nbd_client_handshake_success(s->export); 150986f8cdf3SVladimir Sementsov-Ogievskiy 151086f8cdf3SVladimir Sementsov-Ogievskiy return 0; 151186f8cdf3SVladimir Sementsov-Ogievskiy 151286f8cdf3SVladimir Sementsov-Ogievskiy fail: 151386f8cdf3SVladimir Sementsov-Ogievskiy /* 1514a8e2bb6aSVladimir Sementsov-Ogievskiy * We have connected, but must fail for other reasons. 1515a8e2bb6aSVladimir Sementsov-Ogievskiy * Send NBD_CMD_DISC as a courtesy to the server. 151686f8cdf3SVladimir Sementsov-Ogievskiy */ 151786f8cdf3SVladimir Sementsov-Ogievskiy { 151886f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { .type = NBD_CMD_DISC }; 151986f8cdf3SVladimir Sementsov-Ogievskiy 1520611ae1d7SVladimir Sementsov-Ogievskiy nbd_send_request(s->ioc ?: QIO_CHANNEL(sioc), &request); 152186f8cdf3SVladimir Sementsov-Ogievskiy 152286f8cdf3SVladimir Sementsov-Ogievskiy object_unref(OBJECT(sioc)); 152386f8cdf3SVladimir Sementsov-Ogievskiy 152486f8cdf3SVladimir Sementsov-Ogievskiy return ret; 152586f8cdf3SVladimir Sementsov-Ogievskiy } 152686f8cdf3SVladimir Sementsov-Ogievskiy } 152786f8cdf3SVladimir Sementsov-Ogievskiy 15288f071c9dSVladimir Sementsov-Ogievskiy /* 15298f071c9dSVladimir Sementsov-Ogievskiy * Parse nbd_open options 15308f071c9dSVladimir Sementsov-Ogievskiy */ 153186f8cdf3SVladimir Sementsov-Ogievskiy 1532f53a1febSKevin Wolf static int nbd_parse_uri(const char *filename, QDict *options) 15331d7d2a9dSPaolo Bonzini { 15341d7d2a9dSPaolo Bonzini URI *uri; 15351d7d2a9dSPaolo Bonzini const char *p; 15361d7d2a9dSPaolo Bonzini QueryParams *qp = NULL; 15371d7d2a9dSPaolo Bonzini int ret = 0; 1538f53a1febSKevin Wolf bool is_unix; 15391d7d2a9dSPaolo Bonzini 15401d7d2a9dSPaolo Bonzini uri = uri_parse(filename); 15411d7d2a9dSPaolo Bonzini if (!uri) { 15421d7d2a9dSPaolo Bonzini return -EINVAL; 15431d7d2a9dSPaolo Bonzini } 15441d7d2a9dSPaolo Bonzini 15451d7d2a9dSPaolo Bonzini /* transport */ 1546f69165a8SMax Reitz if (!g_strcmp0(uri->scheme, "nbd")) { 1547f53a1febSKevin Wolf is_unix = false; 1548f69165a8SMax Reitz } else if (!g_strcmp0(uri->scheme, "nbd+tcp")) { 1549f53a1febSKevin Wolf is_unix = false; 1550f69165a8SMax Reitz } else if (!g_strcmp0(uri->scheme, "nbd+unix")) { 1551f53a1febSKevin Wolf is_unix = true; 15521d7d2a9dSPaolo Bonzini } else { 15531d7d2a9dSPaolo Bonzini ret = -EINVAL; 15541d7d2a9dSPaolo Bonzini goto out; 15551d7d2a9dSPaolo Bonzini } 15561d7d2a9dSPaolo Bonzini 15572485f22fSEric Blake p = uri->path ? uri->path : ""; 15582485f22fSEric Blake if (p[0] == '/') { 15592485f22fSEric Blake p++; 15602485f22fSEric Blake } 15611d7d2a9dSPaolo Bonzini if (p[0]) { 156246f5ac20SEric Blake qdict_put_str(options, "export", p); 15631d7d2a9dSPaolo Bonzini } 15641d7d2a9dSPaolo Bonzini 15651d7d2a9dSPaolo Bonzini qp = query_params_parse(uri->query); 1566f53a1febSKevin Wolf if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) { 15671d7d2a9dSPaolo Bonzini ret = -EINVAL; 15681d7d2a9dSPaolo Bonzini goto out; 15691d7d2a9dSPaolo Bonzini } 15701d7d2a9dSPaolo Bonzini 1571f53a1febSKevin Wolf if (is_unix) { 15721d7d2a9dSPaolo Bonzini /* nbd+unix:///export?socket=path */ 15731d7d2a9dSPaolo Bonzini if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) { 15741d7d2a9dSPaolo Bonzini ret = -EINVAL; 15751d7d2a9dSPaolo Bonzini goto out; 15761d7d2a9dSPaolo Bonzini } 157746f5ac20SEric Blake qdict_put_str(options, "server.type", "unix"); 157846f5ac20SEric Blake qdict_put_str(options, "server.path", qp->p[0].value); 15791d7d2a9dSPaolo Bonzini } else { 158023307908SJán Tomko QString *host; 1581f84d431bSMax Reitz char *port_str; 1582f84d431bSMax Reitz 1583bebbf7faSKevin Wolf /* nbd[+tcp]://host[:port]/export */ 15841d7d2a9dSPaolo Bonzini if (!uri->server) { 15851d7d2a9dSPaolo Bonzini ret = -EINVAL; 15861d7d2a9dSPaolo Bonzini goto out; 15871d7d2a9dSPaolo Bonzini } 1588f17c90beSKevin Wolf 158923307908SJán Tomko /* strip braces from literal IPv6 address */ 159023307908SJán Tomko if (uri->server[0] == '[') { 159123307908SJán Tomko host = qstring_from_substr(uri->server, 1, 1592ba891d68SMarkus Armbruster strlen(uri->server) - 1); 159323307908SJán Tomko } else { 159423307908SJán Tomko host = qstring_from_str(uri->server); 159523307908SJán Tomko } 159623307908SJán Tomko 159746f5ac20SEric Blake qdict_put_str(options, "server.type", "inet"); 15989445673eSMarkus Armbruster qdict_put(options, "server.host", host); 1599f84d431bSMax Reitz 1600f84d431bSMax Reitz port_str = g_strdup_printf("%d", uri->port ?: NBD_DEFAULT_PORT); 160146f5ac20SEric Blake qdict_put_str(options, "server.port", port_str); 1602f53a1febSKevin Wolf g_free(port_str); 16031d7d2a9dSPaolo Bonzini } 16041d7d2a9dSPaolo Bonzini 16051d7d2a9dSPaolo Bonzini out: 16061d7d2a9dSPaolo Bonzini if (qp) { 16071d7d2a9dSPaolo Bonzini query_params_free(qp); 16081d7d2a9dSPaolo Bonzini } 16091d7d2a9dSPaolo Bonzini uri_free(uri); 16101d7d2a9dSPaolo Bonzini return ret; 16111d7d2a9dSPaolo Bonzini } 16121d7d2a9dSPaolo Bonzini 161348c38e0bSMax Reitz static bool nbd_has_filename_options_conflict(QDict *options, Error **errp) 161448c38e0bSMax Reitz { 161548c38e0bSMax Reitz const QDictEntry *e; 161648c38e0bSMax Reitz 161748c38e0bSMax Reitz for (e = qdict_first(options); e; e = qdict_next(options, e)) { 161848c38e0bSMax Reitz if (!strcmp(e->key, "host") || 161948c38e0bSMax Reitz !strcmp(e->key, "port") || 162048c38e0bSMax Reitz !strcmp(e->key, "path") || 1621491d6c7cSMax Reitz !strcmp(e->key, "export") || 1622491d6c7cSMax Reitz strstart(e->key, "server.", NULL)) 162348c38e0bSMax Reitz { 162448c38e0bSMax Reitz error_setg(errp, "Option '%s' cannot be used with a file name", 162548c38e0bSMax Reitz e->key); 162648c38e0bSMax Reitz return true; 162748c38e0bSMax Reitz } 162848c38e0bSMax Reitz } 162948c38e0bSMax Reitz 163048c38e0bSMax Reitz return false; 163148c38e0bSMax Reitz } 163248c38e0bSMax Reitz 16336963a30dSKevin Wolf static void nbd_parse_filename(const char *filename, QDict *options, 16346963a30dSKevin Wolf Error **errp) 1635019d6b8fSAnthony Liguori { 1636df18c04eSEric Blake g_autofree char *file = NULL; 163733897dc7SNick Thomas char *export_name; 163833897dc7SNick Thomas const char *host_spec; 1639019d6b8fSAnthony Liguori const char *unixpath; 1640019d6b8fSAnthony Liguori 164148c38e0bSMax Reitz if (nbd_has_filename_options_conflict(options, errp)) { 1642681e7ad0SKevin Wolf return; 1643681e7ad0SKevin Wolf } 1644681e7ad0SKevin Wolf 16451d7d2a9dSPaolo Bonzini if (strstr(filename, "://")) { 16466963a30dSKevin Wolf int ret = nbd_parse_uri(filename, options); 16476963a30dSKevin Wolf if (ret < 0) { 16486963a30dSKevin Wolf error_setg(errp, "No valid URL specified"); 16496963a30dSKevin Wolf } 16506963a30dSKevin Wolf return; 16511d7d2a9dSPaolo Bonzini } 16521d7d2a9dSPaolo Bonzini 16537267c094SAnthony Liguori file = g_strdup(filename); 16541d45f8b5SLaurent Vivier 165533897dc7SNick Thomas export_name = strstr(file, EN_OPTSTR); 165633897dc7SNick Thomas if (export_name) { 165733897dc7SNick Thomas if (export_name[strlen(EN_OPTSTR)] == 0) { 1658df18c04eSEric Blake return; 16591d45f8b5SLaurent Vivier } 166033897dc7SNick Thomas export_name[0] = 0; /* truncate 'file' */ 166133897dc7SNick Thomas export_name += strlen(EN_OPTSTR); 1662f53a1febSKevin Wolf 166346f5ac20SEric Blake qdict_put_str(options, "export", export_name); 16641d45f8b5SLaurent Vivier } 16651d45f8b5SLaurent Vivier 166633897dc7SNick Thomas /* extract the host_spec - fail if it's not nbd:... */ 166733897dc7SNick Thomas if (!strstart(file, "nbd:", &host_spec)) { 16686963a30dSKevin Wolf error_setg(errp, "File name string for NBD must start with 'nbd:'"); 1669df18c04eSEric Blake return; 16701d45f8b5SLaurent Vivier } 1671019d6b8fSAnthony Liguori 1672f53a1febSKevin Wolf if (!*host_spec) { 1673df18c04eSEric Blake return; 1674f53a1febSKevin Wolf } 1675f53a1febSKevin Wolf 167633897dc7SNick Thomas /* are we a UNIX or TCP socket? */ 167733897dc7SNick Thomas if (strstart(host_spec, "unix:", &unixpath)) { 167846f5ac20SEric Blake qdict_put_str(options, "server.type", "unix"); 167946f5ac20SEric Blake qdict_put_str(options, "server.path", unixpath); 1680019d6b8fSAnthony Liguori } else { 16810785bd7aSMarkus Armbruster InetSocketAddress *addr = g_new(InetSocketAddress, 1); 1682f53a1febSKevin Wolf 16830785bd7aSMarkus Armbruster if (inet_parse(addr, host_spec, errp)) { 16840785bd7aSMarkus Armbruster goto out_inet; 1685f17c90beSKevin Wolf } 1686f53a1febSKevin Wolf 168746f5ac20SEric Blake qdict_put_str(options, "server.type", "inet"); 168846f5ac20SEric Blake qdict_put_str(options, "server.host", addr->host); 168946f5ac20SEric Blake qdict_put_str(options, "server.port", addr->port); 16900785bd7aSMarkus Armbruster out_inet: 1691f53a1febSKevin Wolf qapi_free_InetSocketAddress(addr); 16921d45f8b5SLaurent Vivier } 169333897dc7SNick Thomas } 1694f53a1febSKevin Wolf 1695491d6c7cSMax Reitz static bool nbd_process_legacy_socket_options(QDict *output_options, 1696491d6c7cSMax Reitz QemuOpts *legacy_opts, 1697491d6c7cSMax Reitz Error **errp) 1698f53a1febSKevin Wolf { 1699491d6c7cSMax Reitz const char *path = qemu_opt_get(legacy_opts, "path"); 1700491d6c7cSMax Reitz const char *host = qemu_opt_get(legacy_opts, "host"); 1701491d6c7cSMax Reitz const char *port = qemu_opt_get(legacy_opts, "port"); 1702491d6c7cSMax Reitz const QDictEntry *e; 1703f53a1febSKevin Wolf 1704491d6c7cSMax Reitz if (!path && !host && !port) { 1705491d6c7cSMax Reitz return true; 1706491d6c7cSMax Reitz } 170703504d05SMax Reitz 1708491d6c7cSMax Reitz for (e = qdict_first(output_options); e; e = qdict_next(output_options, e)) 1709491d6c7cSMax Reitz { 1710491d6c7cSMax Reitz if (strstart(e->key, "server.", NULL)) { 1711491d6c7cSMax Reitz error_setg(errp, "Cannot use 'server' and path/host/port at the " 1712491d6c7cSMax Reitz "same time"); 1713491d6c7cSMax Reitz return false; 1714491d6c7cSMax Reitz } 1715491d6c7cSMax Reitz } 1716491d6c7cSMax Reitz 1717491d6c7cSMax Reitz if (path && host) { 171882d73014SMax Reitz error_setg(errp, "path and host may not be used at the same time"); 1719491d6c7cSMax Reitz return false; 1720491d6c7cSMax Reitz } else if (path) { 1721491d6c7cSMax Reitz if (port) { 1722442045cbSMax Reitz error_setg(errp, "port may not be used without host"); 1723491d6c7cSMax Reitz return false; 1724442045cbSMax Reitz } 1725019d6b8fSAnthony Liguori 172646f5ac20SEric Blake qdict_put_str(output_options, "server.type", "unix"); 172746f5ac20SEric Blake qdict_put_str(output_options, "server.path", path); 1728491d6c7cSMax Reitz } else if (host) { 172946f5ac20SEric Blake qdict_put_str(output_options, "server.type", "inet"); 173046f5ac20SEric Blake qdict_put_str(output_options, "server.host", host); 173146f5ac20SEric Blake qdict_put_str(output_options, "server.port", 173246f5ac20SEric Blake port ?: stringify(NBD_DEFAULT_PORT)); 17337a5ed437SDaniel P. Berrange } 1734491d6c7cSMax Reitz 1735491d6c7cSMax Reitz return true; 1736491d6c7cSMax Reitz } 1737491d6c7cSMax Reitz 173862cf396bSMarkus Armbruster static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options, 17399445673eSMarkus Armbruster Error **errp) 1740491d6c7cSMax Reitz { 174162cf396bSMarkus Armbruster SocketAddress *saddr = NULL; 1742491d6c7cSMax Reitz QDict *addr = NULL; 1743491d6c7cSMax Reitz Visitor *iv = NULL; 1744491d6c7cSMax Reitz 1745491d6c7cSMax Reitz qdict_extract_subqdict(options, &addr, "server."); 1746491d6c7cSMax Reitz if (!qdict_size(addr)) { 1747491d6c7cSMax Reitz error_setg(errp, "NBD server address missing"); 1748491d6c7cSMax Reitz goto done; 1749491d6c7cSMax Reitz } 1750491d6c7cSMax Reitz 1751af91062eSMarkus Armbruster iv = qobject_input_visitor_new_flat_confused(addr, errp); 1752af91062eSMarkus Armbruster if (!iv) { 1753491d6c7cSMax Reitz goto done; 1754491d6c7cSMax Reitz } 1755491d6c7cSMax Reitz 1756af175e85SMarkus Armbruster if (!visit_type_SocketAddress(iv, NULL, &saddr, errp)) { 1757491d6c7cSMax Reitz goto done; 1758f53a1febSKevin Wolf } 1759f53a1febSKevin Wolf 1760491d6c7cSMax Reitz done: 1761cb3e7f08SMarc-André Lureau qobject_unref(addr); 1762491d6c7cSMax Reitz visit_free(iv); 17637a5ed437SDaniel P. Berrange return saddr; 1764f53a1febSKevin Wolf } 1765f53a1febSKevin Wolf 176675822a12SDaniel P. Berrange static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp) 176775822a12SDaniel P. Berrange { 176875822a12SDaniel P. Berrange Object *obj; 176975822a12SDaniel P. Berrange QCryptoTLSCreds *creds; 177075822a12SDaniel P. Berrange 177175822a12SDaniel P. Berrange obj = object_resolve_path_component( 177275822a12SDaniel P. Berrange object_get_objects_root(), id); 177375822a12SDaniel P. Berrange if (!obj) { 177475822a12SDaniel P. Berrange error_setg(errp, "No TLS credentials with id '%s'", 177575822a12SDaniel P. Berrange id); 177675822a12SDaniel P. Berrange return NULL; 177775822a12SDaniel P. Berrange } 177875822a12SDaniel P. Berrange creds = (QCryptoTLSCreds *) 177975822a12SDaniel P. Berrange object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS); 178075822a12SDaniel P. Berrange if (!creds) { 178175822a12SDaniel P. Berrange error_setg(errp, "Object with id '%s' is not TLS credentials", 178275822a12SDaniel P. Berrange id); 178375822a12SDaniel P. Berrange return NULL; 178475822a12SDaniel P. Berrange } 178575822a12SDaniel P. Berrange 178675822a12SDaniel P. Berrange if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) { 178775822a12SDaniel P. Berrange error_setg(errp, 178875822a12SDaniel P. Berrange "Expecting TLS credentials with a client endpoint"); 178975822a12SDaniel P. Berrange return NULL; 179075822a12SDaniel P. Berrange } 179175822a12SDaniel P. Berrange object_ref(obj); 179275822a12SDaniel P. Berrange return creds; 179375822a12SDaniel P. Berrange } 179475822a12SDaniel P. Berrange 179575822a12SDaniel P. Berrange 17967ccc44fdSMax Reitz static QemuOptsList nbd_runtime_opts = { 17977ccc44fdSMax Reitz .name = "nbd", 17987ccc44fdSMax Reitz .head = QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts.head), 17997ccc44fdSMax Reitz .desc = { 18007ccc44fdSMax Reitz { 18017ccc44fdSMax Reitz .name = "host", 18027ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 18037ccc44fdSMax Reitz .help = "TCP host to connect to", 18047ccc44fdSMax Reitz }, 18057ccc44fdSMax Reitz { 18067ccc44fdSMax Reitz .name = "port", 18077ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 18087ccc44fdSMax Reitz .help = "TCP port to connect to", 18097ccc44fdSMax Reitz }, 18107ccc44fdSMax Reitz { 18117ccc44fdSMax Reitz .name = "path", 18127ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 18137ccc44fdSMax Reitz .help = "Unix socket path to connect to", 18147ccc44fdSMax Reitz }, 18157ccc44fdSMax Reitz { 18167ccc44fdSMax Reitz .name = "export", 18177ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 18187ccc44fdSMax Reitz .help = "Name of the NBD export to open", 18197ccc44fdSMax Reitz }, 18207ccc44fdSMax Reitz { 18217ccc44fdSMax Reitz .name = "tls-creds", 18227ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 18237ccc44fdSMax Reitz .help = "ID of the TLS credentials to use", 18247ccc44fdSMax Reitz }, 1825216ee365SEric Blake { 1826216ee365SEric Blake .name = "x-dirty-bitmap", 1827216ee365SEric Blake .type = QEMU_OPT_STRING, 1828216ee365SEric Blake .help = "experimental: expose named dirty bitmap in place of " 1829216ee365SEric Blake "block status", 1830216ee365SEric Blake }, 1831b172ae2eSVladimir Sementsov-Ogievskiy { 1832b172ae2eSVladimir Sementsov-Ogievskiy .name = "reconnect-delay", 1833b172ae2eSVladimir Sementsov-Ogievskiy .type = QEMU_OPT_NUMBER, 1834b172ae2eSVladimir Sementsov-Ogievskiy .help = "On an unexpected disconnect, the nbd client tries to " 1835b172ae2eSVladimir Sementsov-Ogievskiy "connect again until succeeding or encountering a serious " 1836b172ae2eSVladimir Sementsov-Ogievskiy "error. During the first @reconnect-delay seconds, all " 1837b172ae2eSVladimir Sementsov-Ogievskiy "requests are paused and will be rerun on a successful " 1838b172ae2eSVladimir Sementsov-Ogievskiy "reconnect. After that time, any delayed requests and all " 1839b172ae2eSVladimir Sementsov-Ogievskiy "future requests before a successful reconnect will " 1840b172ae2eSVladimir Sementsov-Ogievskiy "immediately fail. Default 0", 1841b172ae2eSVladimir Sementsov-Ogievskiy }, 1842c4365735SMurilo Opsfelder Araujo { /* end of list */ } 18437ccc44fdSMax Reitz }, 18447ccc44fdSMax Reitz }; 18457ccc44fdSMax Reitz 18468f071c9dSVladimir Sementsov-Ogievskiy static int nbd_process_options(BlockDriverState *bs, QDict *options, 1847015a1036SMax Reitz Error **errp) 184833897dc7SNick Thomas { 184933897dc7SNick Thomas BDRVNBDState *s = bs->opaque; 18508f071c9dSVladimir Sementsov-Ogievskiy QemuOpts *opts; 185175822a12SDaniel P. Berrange int ret = -EINVAL; 1852ae255e52SPaolo Bonzini 18537ccc44fdSMax Reitz opts = qemu_opts_create(&nbd_runtime_opts, NULL, 0, &error_abort); 1854af175e85SMarkus Armbruster if (!qemu_opts_absorb_qdict(opts, options, errp)) { 18557ccc44fdSMax Reitz goto error; 18567ccc44fdSMax Reitz } 18577ccc44fdSMax Reitz 185862cf396bSMarkus Armbruster /* Translate @host, @port, and @path to a SocketAddress */ 1859491d6c7cSMax Reitz if (!nbd_process_legacy_socket_options(options, opts, errp)) { 186075822a12SDaniel P. Berrange goto error; 186175822a12SDaniel P. Berrange } 186275822a12SDaniel P. Berrange 1863491d6c7cSMax Reitz /* Pop the config into our state object. Exit if invalid. */ 1864491d6c7cSMax Reitz s->saddr = nbd_config(s, options, errp); 1865491d6c7cSMax Reitz if (!s->saddr) { 1866491d6c7cSMax Reitz goto error; 1867491d6c7cSMax Reitz } 1868491d6c7cSMax Reitz 1869491d6c7cSMax Reitz s->export = g_strdup(qemu_opt_get(opts, "export")); 187093676c88SEric Blake if (s->export && strlen(s->export) > NBD_MAX_STRING_SIZE) { 187193676c88SEric Blake error_setg(errp, "export name too long to send to server"); 187293676c88SEric Blake goto error; 187393676c88SEric Blake } 1874491d6c7cSMax Reitz 187503504d05SMax Reitz s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds")); 187603504d05SMax Reitz if (s->tlscredsid) { 18778f071c9dSVladimir Sementsov-Ogievskiy s->tlscreds = nbd_get_tls_creds(s->tlscredsid, errp); 18788f071c9dSVladimir Sementsov-Ogievskiy if (!s->tlscreds) { 187975822a12SDaniel P. Berrange goto error; 188075822a12SDaniel P. Berrange } 188175822a12SDaniel P. Berrange 1882ca0b64e5SMarkus Armbruster /* TODO SOCKET_ADDRESS_KIND_FD where fd has AF_INET or AF_INET6 */ 188362cf396bSMarkus Armbruster if (s->saddr->type != SOCKET_ADDRESS_TYPE_INET) { 188475822a12SDaniel P. Berrange error_setg(errp, "TLS only supported over IP sockets"); 188575822a12SDaniel P. Berrange goto error; 188675822a12SDaniel P. Berrange } 18878f071c9dSVladimir Sementsov-Ogievskiy s->hostname = s->saddr->u.inet.host; 188833897dc7SNick Thomas } 188933897dc7SNick Thomas 18908f071c9dSVladimir Sementsov-Ogievskiy s->x_dirty_bitmap = g_strdup(qemu_opt_get(opts, "x-dirty-bitmap")); 189193676c88SEric Blake if (s->x_dirty_bitmap && strlen(s->x_dirty_bitmap) > NBD_MAX_STRING_SIZE) { 189293676c88SEric Blake error_setg(errp, "x-dirty-bitmap query too long to send to server"); 189393676c88SEric Blake goto error; 189493676c88SEric Blake } 189593676c88SEric Blake 18968f071c9dSVladimir Sementsov-Ogievskiy s->reconnect_delay = qemu_opt_get_number(opts, "reconnect-delay", 0); 18978f071c9dSVladimir Sementsov-Ogievskiy 18988f071c9dSVladimir Sementsov-Ogievskiy ret = 0; 1899d42f78e9SVladimir Sementsov-Ogievskiy 190075822a12SDaniel P. Berrange error: 190103504d05SMax Reitz if (ret < 0) { 19027f493662SPan Nengyuan nbd_clear_bdrvstate(s); 190303504d05SMax Reitz } 19047ccc44fdSMax Reitz qemu_opts_del(opts); 190575822a12SDaniel P. Berrange return ret; 1906ae255e52SPaolo Bonzini } 1907d9b09f13SPaolo Bonzini 19088f071c9dSVladimir Sementsov-Ogievskiy static int nbd_open(BlockDriverState *bs, QDict *options, int flags, 19098f071c9dSVladimir Sementsov-Ogievskiy Error **errp) 19108f071c9dSVladimir Sementsov-Ogievskiy { 19118f071c9dSVladimir Sementsov-Ogievskiy int ret; 19128f071c9dSVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 1913fa35591bSVladimir Sementsov-Ogievskiy QIOChannelSocket *sioc; 19148f071c9dSVladimir Sementsov-Ogievskiy 19158f071c9dSVladimir Sementsov-Ogievskiy ret = nbd_process_options(bs, options, errp); 19168f071c9dSVladimir Sementsov-Ogievskiy if (ret < 0) { 19178f071c9dSVladimir Sementsov-Ogievskiy return ret; 19188f071c9dSVladimir Sementsov-Ogievskiy } 19198f071c9dSVladimir Sementsov-Ogievskiy 19208f071c9dSVladimir Sementsov-Ogievskiy s->bs = bs; 19218f071c9dSVladimir Sementsov-Ogievskiy qemu_co_mutex_init(&s->send_mutex); 19228f071c9dSVladimir Sementsov-Ogievskiy qemu_co_queue_init(&s->free_sema); 19238f071c9dSVladimir Sementsov-Ogievskiy 1924fa35591bSVladimir Sementsov-Ogievskiy /* 1925fa35591bSVladimir Sementsov-Ogievskiy * establish TCP connection, return error if it fails 1926fa35591bSVladimir Sementsov-Ogievskiy * TODO: Configurable retry-until-timeout behaviour. 1927fa35591bSVladimir Sementsov-Ogievskiy */ 1928fa35591bSVladimir Sementsov-Ogievskiy sioc = nbd_establish_connection(s->saddr, errp); 1929fa35591bSVladimir Sementsov-Ogievskiy if (!sioc) { 1930fa35591bSVladimir Sementsov-Ogievskiy return -ECONNREFUSED; 1931fa35591bSVladimir Sementsov-Ogievskiy } 1932fa35591bSVladimir Sementsov-Ogievskiy 1933fa35591bSVladimir Sementsov-Ogievskiy ret = nbd_client_handshake(bs, sioc, errp); 19348f071c9dSVladimir Sementsov-Ogievskiy if (ret < 0) { 19358198cf5eSPan Nengyuan nbd_clear_bdrvstate(s); 19368f071c9dSVladimir Sementsov-Ogievskiy return ret; 19378f071c9dSVladimir Sementsov-Ogievskiy } 19388f071c9dSVladimir Sementsov-Ogievskiy /* successfully connected */ 19398f071c9dSVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_CONNECTED; 19408f071c9dSVladimir Sementsov-Ogievskiy 19418f071c9dSVladimir Sementsov-Ogievskiy s->connection_co = qemu_coroutine_create(nbd_connection_entry, s); 19428f071c9dSVladimir Sementsov-Ogievskiy bdrv_inc_in_flight(bs); 19438f071c9dSVladimir Sementsov-Ogievskiy aio_co_schedule(bdrv_get_aio_context(bs), s->connection_co); 19448f071c9dSVladimir Sementsov-Ogievskiy 19458f071c9dSVladimir Sementsov-Ogievskiy return 0; 19468f071c9dSVladimir Sementsov-Ogievskiy } 19478f071c9dSVladimir Sementsov-Ogievskiy 19481486d04aSPaolo Bonzini static int nbd_co_flush(BlockDriverState *bs) 19491486d04aSPaolo Bonzini { 1950f53a829bSMax Reitz return nbd_client_co_flush(bs); 19511486d04aSPaolo Bonzini } 19521486d04aSPaolo Bonzini 1953fa21e6faSDenis V. Lunev static void nbd_refresh_limits(BlockDriverState *bs, Error **errp) 1954fa21e6faSDenis V. Lunev { 1955611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 1956fd8d372dSEric Blake uint32_t min = s->info.min_block; 1957081dd1feSEric Blake uint32_t max = MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE, s->info.max_block); 1958081dd1feSEric Blake 19597da537f7SEric Blake /* 19607da537f7SEric Blake * If the server did not advertise an alignment: 19617da537f7SEric Blake * - a size that is not sector-aligned implies that an alignment 19627da537f7SEric Blake * of 1 can be used to access those tail bytes 19637da537f7SEric Blake * - advertisement of block status requires an alignment of 1, so 19647da537f7SEric Blake * that we don't violate block layer constraints that block 19657da537f7SEric Blake * status is always aligned (as we can't control whether the 19667da537f7SEric Blake * server will report sub-sector extents, such as a hole at EOF 19677da537f7SEric Blake * on an unaligned POSIX file) 19687da537f7SEric Blake * - otherwise, assume the server is so old that we are safer avoiding 19697da537f7SEric Blake * sub-sector requests 19707da537f7SEric Blake */ 19717da537f7SEric Blake if (!min) { 19727da537f7SEric Blake min = (!QEMU_IS_ALIGNED(s->info.size, BDRV_SECTOR_SIZE) || 19737da537f7SEric Blake s->info.base_allocation) ? 1 : BDRV_SECTOR_SIZE; 19747da537f7SEric Blake } 19757da537f7SEric Blake 19767da537f7SEric Blake bs->bl.request_alignment = min; 1977714eb0dbSVladimir Sementsov-Ogievskiy bs->bl.max_pdiscard = QEMU_ALIGN_DOWN(INT_MAX, min); 1978081dd1feSEric Blake bs->bl.max_pwrite_zeroes = max; 1979081dd1feSEric Blake bs->bl.max_transfer = max; 1980081dd1feSEric Blake 1981081dd1feSEric Blake if (s->info.opt_block && 1982081dd1feSEric Blake s->info.opt_block > bs->bl.opt_transfer) { 1983081dd1feSEric Blake bs->bl.opt_transfer = s->info.opt_block; 1984081dd1feSEric Blake } 1985fa21e6faSDenis V. Lunev } 1986fa21e6faSDenis V. Lunev 1987019d6b8fSAnthony Liguori static void nbd_close(BlockDriverState *bs) 1988019d6b8fSAnthony Liguori { 198903504d05SMax Reitz BDRVNBDState *s = bs->opaque; 199003504d05SMax Reitz 1991f53a829bSMax Reitz nbd_client_close(bs); 19927f493662SPan Nengyuan nbd_clear_bdrvstate(s); 1993019d6b8fSAnthony Liguori } 1994019d6b8fSAnthony Liguori 1995a2b333c0SNir Soffer /* 1996a2b333c0SNir Soffer * NBD cannot truncate, but if the caller asks to truncate to the same size, or 1997a2b333c0SNir Soffer * to a smaller size with exact=false, there is no reason to fail the 1998a2b333c0SNir Soffer * operation. 1999a2b333c0SNir Soffer * 2000a2b333c0SNir Soffer * Preallocation mode is ignored since it does not seems useful to fail when 2001a2b333c0SNir Soffer * we never change anything. 2002a2b333c0SNir Soffer */ 2003a2b333c0SNir Soffer static int coroutine_fn nbd_co_truncate(BlockDriverState *bs, int64_t offset, 2004a2b333c0SNir Soffer bool exact, PreallocMode prealloc, 2005a2b333c0SNir Soffer BdrvRequestFlags flags, Error **errp) 2006a2b333c0SNir Soffer { 2007a2b333c0SNir Soffer BDRVNBDState *s = bs->opaque; 2008a2b333c0SNir Soffer 2009a2b333c0SNir Soffer if (offset != s->info.size && exact) { 2010a2b333c0SNir Soffer error_setg(errp, "Cannot resize NBD nodes"); 2011a2b333c0SNir Soffer return -ENOTSUP; 2012a2b333c0SNir Soffer } 2013a2b333c0SNir Soffer 2014a2b333c0SNir Soffer if (offset > s->info.size) { 2015a2b333c0SNir Soffer error_setg(errp, "Cannot grow NBD nodes"); 2016a2b333c0SNir Soffer return -EINVAL; 2017a2b333c0SNir Soffer } 2018a2b333c0SNir Soffer 2019a2b333c0SNir Soffer return 0; 2020a2b333c0SNir Soffer } 2021a2b333c0SNir Soffer 2022019d6b8fSAnthony Liguori static int64_t nbd_getlength(BlockDriverState *bs) 2023019d6b8fSAnthony Liguori { 2024019d6b8fSAnthony Liguori BDRVNBDState *s = bs->opaque; 2025019d6b8fSAnthony Liguori 2026611ae1d7SVladimir Sementsov-Ogievskiy return s->info.size; 2027019d6b8fSAnthony Liguori } 2028019d6b8fSAnthony Liguori 2029998b3a1eSMax Reitz static void nbd_refresh_filename(BlockDriverState *bs) 20302019d68bSMax Reitz { 203103504d05SMax Reitz BDRVNBDState *s = bs->opaque; 2032491d6c7cSMax Reitz const char *host = NULL, *port = NULL, *path = NULL; 20335c86bdf1SEric Blake size_t len = 0; 2034491d6c7cSMax Reitz 203562cf396bSMarkus Armbruster if (s->saddr->type == SOCKET_ADDRESS_TYPE_INET) { 20369445673eSMarkus Armbruster const InetSocketAddress *inet = &s->saddr->u.inet; 2037491d6c7cSMax Reitz if (!inet->has_ipv4 && !inet->has_ipv6 && !inet->has_to) { 2038491d6c7cSMax Reitz host = inet->host; 2039491d6c7cSMax Reitz port = inet->port; 2040491d6c7cSMax Reitz } 204162cf396bSMarkus Armbruster } else if (s->saddr->type == SOCKET_ADDRESS_TYPE_UNIX) { 20429445673eSMarkus Armbruster path = s->saddr->u.q_unix.path; 20439445673eSMarkus Armbruster } /* else can't represent as pseudo-filename */ 20442019d68bSMax Reitz 2045491d6c7cSMax Reitz if (path && s->export) { 20465c86bdf1SEric Blake len = snprintf(bs->exact_filename, sizeof(bs->exact_filename), 2047491d6c7cSMax Reitz "nbd+unix:///%s?socket=%s", s->export, path); 2048491d6c7cSMax Reitz } else if (path && !s->export) { 20495c86bdf1SEric Blake len = snprintf(bs->exact_filename, sizeof(bs->exact_filename), 2050491d6c7cSMax Reitz "nbd+unix://?socket=%s", path); 2051491d6c7cSMax Reitz } else if (host && s->export) { 20525c86bdf1SEric Blake len = snprintf(bs->exact_filename, sizeof(bs->exact_filename), 2053491d6c7cSMax Reitz "nbd://%s:%s/%s", host, port, s->export); 2054491d6c7cSMax Reitz } else if (host && !s->export) { 20555c86bdf1SEric Blake len = snprintf(bs->exact_filename, sizeof(bs->exact_filename), 2056491d6c7cSMax Reitz "nbd://%s:%s", host, port); 2057ec0de768SMax Reitz } 205800d69986SEric Blake if (len >= sizeof(bs->exact_filename)) { 20595c86bdf1SEric Blake /* Name is too long to represent exactly, so leave it empty. */ 20605c86bdf1SEric Blake bs->exact_filename[0] = '\0'; 20615c86bdf1SEric Blake } 20622019d68bSMax Reitz } 20632019d68bSMax Reitz 20648a6239c0SMax Reitz static char *nbd_dirname(BlockDriverState *bs, Error **errp) 20658a6239c0SMax Reitz { 20668a6239c0SMax Reitz /* The generic bdrv_dirname() implementation is able to work out some 20678a6239c0SMax Reitz * directory name for NBD nodes, but that would be wrong. So far there is no 20688a6239c0SMax Reitz * specification for how "export paths" would work, so NBD does not have 20698a6239c0SMax Reitz * directory names. */ 20708a6239c0SMax Reitz error_setg(errp, "Cannot generate a base directory for NBD nodes"); 20718a6239c0SMax Reitz return NULL; 20728a6239c0SMax Reitz } 20738a6239c0SMax Reitz 20742654267cSMax Reitz static const char *const nbd_strong_runtime_opts[] = { 20752654267cSMax Reitz "path", 20762654267cSMax Reitz "host", 20772654267cSMax Reitz "port", 20782654267cSMax Reitz "export", 20792654267cSMax Reitz "tls-creds", 20802654267cSMax Reitz "server.", 20812654267cSMax Reitz 20822654267cSMax Reitz NULL 20832654267cSMax Reitz }; 20842654267cSMax Reitz 2085019d6b8fSAnthony Liguori static BlockDriver bdrv_nbd = { 2086019d6b8fSAnthony Liguori .format_name = "nbd", 20871d7d2a9dSPaolo Bonzini .protocol_name = "nbd", 2088019d6b8fSAnthony Liguori .instance_size = sizeof(BDRVNBDState), 20896963a30dSKevin Wolf .bdrv_parse_filename = nbd_parse_filename, 20905a5e7f8cSMaxim Levitsky .bdrv_co_create_opts = bdrv_co_create_opts_simple, 20915a5e7f8cSMaxim Levitsky .create_opts = &bdrv_create_opts_simple, 209266f82ceeSKevin Wolf .bdrv_file_open = nbd_open, 2093e99754b4SMaxim Levitsky .bdrv_reopen_prepare = nbd_client_reopen_prepare, 209470c4fb26SEric Blake .bdrv_co_preadv = nbd_client_co_preadv, 209570c4fb26SEric Blake .bdrv_co_pwritev = nbd_client_co_pwritev, 2096fa778fffSEric Blake .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes, 2097019d6b8fSAnthony Liguori .bdrv_close = nbd_close, 20981486d04aSPaolo Bonzini .bdrv_co_flush_to_os = nbd_co_flush, 2099447e57c3SEric Blake .bdrv_co_pdiscard = nbd_client_co_pdiscard, 2100fa21e6faSDenis V. Lunev .bdrv_refresh_limits = nbd_refresh_limits, 2101a2b333c0SNir Soffer .bdrv_co_truncate = nbd_co_truncate, 2102019d6b8fSAnthony Liguori .bdrv_getlength = nbd_getlength, 210386f8cdf3SVladimir Sementsov-Ogievskiy .bdrv_detach_aio_context = nbd_client_detach_aio_context, 210486f8cdf3SVladimir Sementsov-Ogievskiy .bdrv_attach_aio_context = nbd_client_attach_aio_context, 2105f7651539SVladimir Sementsov-Ogievskiy .bdrv_co_drain_begin = nbd_client_co_drain_begin, 2106f7651539SVladimir Sementsov-Ogievskiy .bdrv_co_drain_end = nbd_client_co_drain_end, 21072019d68bSMax Reitz .bdrv_refresh_filename = nbd_refresh_filename, 210878a33ab5SVladimir Sementsov-Ogievskiy .bdrv_co_block_status = nbd_client_co_block_status, 21098a6239c0SMax Reitz .bdrv_dirname = nbd_dirname, 21102654267cSMax Reitz .strong_runtime_opts = nbd_strong_runtime_opts, 21111d7d2a9dSPaolo Bonzini }; 21121d7d2a9dSPaolo Bonzini 21131d7d2a9dSPaolo Bonzini static BlockDriver bdrv_nbd_tcp = { 21141d7d2a9dSPaolo Bonzini .format_name = "nbd", 21151d7d2a9dSPaolo Bonzini .protocol_name = "nbd+tcp", 21161d7d2a9dSPaolo Bonzini .instance_size = sizeof(BDRVNBDState), 21176963a30dSKevin Wolf .bdrv_parse_filename = nbd_parse_filename, 21185a5e7f8cSMaxim Levitsky .bdrv_co_create_opts = bdrv_co_create_opts_simple, 21195a5e7f8cSMaxim Levitsky .create_opts = &bdrv_create_opts_simple, 21201d7d2a9dSPaolo Bonzini .bdrv_file_open = nbd_open, 2121e99754b4SMaxim Levitsky .bdrv_reopen_prepare = nbd_client_reopen_prepare, 212270c4fb26SEric Blake .bdrv_co_preadv = nbd_client_co_preadv, 212370c4fb26SEric Blake .bdrv_co_pwritev = nbd_client_co_pwritev, 2124fa778fffSEric Blake .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes, 21251d7d2a9dSPaolo Bonzini .bdrv_close = nbd_close, 21261d7d2a9dSPaolo Bonzini .bdrv_co_flush_to_os = nbd_co_flush, 2127447e57c3SEric Blake .bdrv_co_pdiscard = nbd_client_co_pdiscard, 2128fa21e6faSDenis V. Lunev .bdrv_refresh_limits = nbd_refresh_limits, 2129a2b333c0SNir Soffer .bdrv_co_truncate = nbd_co_truncate, 21301d7d2a9dSPaolo Bonzini .bdrv_getlength = nbd_getlength, 213186f8cdf3SVladimir Sementsov-Ogievskiy .bdrv_detach_aio_context = nbd_client_detach_aio_context, 213286f8cdf3SVladimir Sementsov-Ogievskiy .bdrv_attach_aio_context = nbd_client_attach_aio_context, 2133f7651539SVladimir Sementsov-Ogievskiy .bdrv_co_drain_begin = nbd_client_co_drain_begin, 2134f7651539SVladimir Sementsov-Ogievskiy .bdrv_co_drain_end = nbd_client_co_drain_end, 21352019d68bSMax Reitz .bdrv_refresh_filename = nbd_refresh_filename, 213678a33ab5SVladimir Sementsov-Ogievskiy .bdrv_co_block_status = nbd_client_co_block_status, 21378a6239c0SMax Reitz .bdrv_dirname = nbd_dirname, 21382654267cSMax Reitz .strong_runtime_opts = nbd_strong_runtime_opts, 21391d7d2a9dSPaolo Bonzini }; 21401d7d2a9dSPaolo Bonzini 21411d7d2a9dSPaolo Bonzini static BlockDriver bdrv_nbd_unix = { 21421d7d2a9dSPaolo Bonzini .format_name = "nbd", 21431d7d2a9dSPaolo Bonzini .protocol_name = "nbd+unix", 21441d7d2a9dSPaolo Bonzini .instance_size = sizeof(BDRVNBDState), 21456963a30dSKevin Wolf .bdrv_parse_filename = nbd_parse_filename, 21465a5e7f8cSMaxim Levitsky .bdrv_co_create_opts = bdrv_co_create_opts_simple, 21475a5e7f8cSMaxim Levitsky .create_opts = &bdrv_create_opts_simple, 21481d7d2a9dSPaolo Bonzini .bdrv_file_open = nbd_open, 2149e99754b4SMaxim Levitsky .bdrv_reopen_prepare = nbd_client_reopen_prepare, 215070c4fb26SEric Blake .bdrv_co_preadv = nbd_client_co_preadv, 215170c4fb26SEric Blake .bdrv_co_pwritev = nbd_client_co_pwritev, 2152fa778fffSEric Blake .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes, 21531d7d2a9dSPaolo Bonzini .bdrv_close = nbd_close, 21541d7d2a9dSPaolo Bonzini .bdrv_co_flush_to_os = nbd_co_flush, 2155447e57c3SEric Blake .bdrv_co_pdiscard = nbd_client_co_pdiscard, 2156fa21e6faSDenis V. Lunev .bdrv_refresh_limits = nbd_refresh_limits, 2157a2b333c0SNir Soffer .bdrv_co_truncate = nbd_co_truncate, 21581d7d2a9dSPaolo Bonzini .bdrv_getlength = nbd_getlength, 215986f8cdf3SVladimir Sementsov-Ogievskiy .bdrv_detach_aio_context = nbd_client_detach_aio_context, 216086f8cdf3SVladimir Sementsov-Ogievskiy .bdrv_attach_aio_context = nbd_client_attach_aio_context, 2161f7651539SVladimir Sementsov-Ogievskiy .bdrv_co_drain_begin = nbd_client_co_drain_begin, 2162f7651539SVladimir Sementsov-Ogievskiy .bdrv_co_drain_end = nbd_client_co_drain_end, 21632019d68bSMax Reitz .bdrv_refresh_filename = nbd_refresh_filename, 216478a33ab5SVladimir Sementsov-Ogievskiy .bdrv_co_block_status = nbd_client_co_block_status, 21658a6239c0SMax Reitz .bdrv_dirname = nbd_dirname, 21662654267cSMax Reitz .strong_runtime_opts = nbd_strong_runtime_opts, 2167019d6b8fSAnthony Liguori }; 2168019d6b8fSAnthony Liguori 2169019d6b8fSAnthony Liguori static void bdrv_nbd_init(void) 2170019d6b8fSAnthony Liguori { 2171019d6b8fSAnthony Liguori bdrv_register(&bdrv_nbd); 21721d7d2a9dSPaolo Bonzini bdrv_register(&bdrv_nbd_tcp); 21731d7d2a9dSPaolo Bonzini bdrv_register(&bdrv_nbd_unix); 2174019d6b8fSAnthony Liguori } 2175019d6b8fSAnthony Liguori 2176019d6b8fSAnthony Liguori block_init(bdrv_nbd_init); 2177