1019d6b8fSAnthony Liguori /* 2019d6b8fSAnthony Liguori * QEMU Block driver for NBD 3019d6b8fSAnthony Liguori * 4f7651539SVladimir Sementsov-Ogievskiy * Copyright (c) 2019 Virtuozzo International GmbH. 586f8cdf3SVladimir Sementsov-Ogievskiy * Copyright (C) 2016 Red Hat, Inc. 6019d6b8fSAnthony Liguori * Copyright (C) 2008 Bull S.A.S. 7019d6b8fSAnthony Liguori * Author: Laurent Vivier <Laurent.Vivier@bull.net> 8019d6b8fSAnthony Liguori * 9019d6b8fSAnthony Liguori * Some parts: 10019d6b8fSAnthony Liguori * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws> 11019d6b8fSAnthony Liguori * 12019d6b8fSAnthony Liguori * Permission is hereby granted, free of charge, to any person obtaining a copy 13019d6b8fSAnthony Liguori * of this software and associated documentation files (the "Software"), to deal 14019d6b8fSAnthony Liguori * in the Software without restriction, including without limitation the rights 15019d6b8fSAnthony Liguori * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16019d6b8fSAnthony Liguori * copies of the Software, and to permit persons to whom the Software is 17019d6b8fSAnthony Liguori * furnished to do so, subject to the following conditions: 18019d6b8fSAnthony Liguori * 19019d6b8fSAnthony Liguori * The above copyright notice and this permission notice shall be included in 20019d6b8fSAnthony Liguori * all copies or substantial portions of the Software. 21019d6b8fSAnthony Liguori * 22019d6b8fSAnthony Liguori * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23019d6b8fSAnthony Liguori * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24019d6b8fSAnthony Liguori * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 25019d6b8fSAnthony Liguori * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26019d6b8fSAnthony Liguori * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27019d6b8fSAnthony Liguori * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 28019d6b8fSAnthony Liguori * THE SOFTWARE. 29019d6b8fSAnthony Liguori */ 30019d6b8fSAnthony Liguori 3180c71a24SPeter Maydell #include "qemu/osdep.h" 3286f8cdf3SVladimir Sementsov-Ogievskiy 3386f8cdf3SVladimir Sementsov-Ogievskiy #include "trace.h" 341de7afc9SPaolo Bonzini #include "qemu/uri.h" 35922a01a0SMarkus Armbruster #include "qemu/option.h" 36f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 37db725815SMarkus Armbruster #include "qemu/main-loop.h" 38fee091cdSLukas Straub #include "qemu/atomic.h" 39019d6b8fSAnthony Liguori 4086f8cdf3SVladimir Sementsov-Ogievskiy #include "qapi/qapi-visit-sockets.h" 4186f8cdf3SVladimir Sementsov-Ogievskiy #include "qapi/qmp/qstring.h" 421dc4718dSVladimir Sementsov-Ogievskiy #include "qapi/clone-visitor.h" 4386f8cdf3SVladimir Sementsov-Ogievskiy 4486f8cdf3SVladimir Sementsov-Ogievskiy #include "block/qdict.h" 4586f8cdf3SVladimir Sementsov-Ogievskiy #include "block/nbd.h" 4686f8cdf3SVladimir Sementsov-Ogievskiy #include "block/block_int.h" 47a71d597bSVladimir Sementsov-Ogievskiy #include "block/coroutines.h" 4886f8cdf3SVladimir Sementsov-Ogievskiy 49fee091cdSLukas Straub #include "qemu/yank.h" 50fee091cdSLukas Straub 511d45f8b5SLaurent Vivier #define EN_OPTSTR ":exportname=" 5286f8cdf3SVladimir Sementsov-Ogievskiy #define MAX_NBD_REQUESTS 16 5386f8cdf3SVladimir Sementsov-Ogievskiy 5486f8cdf3SVladimir Sementsov-Ogievskiy #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ (uint64_t)(intptr_t)(bs)) 5586f8cdf3SVladimir Sementsov-Ogievskiy #define INDEX_TO_HANDLE(bs, index) ((index) ^ (uint64_t)(intptr_t)(bs)) 5686f8cdf3SVladimir Sementsov-Ogievskiy 5786f8cdf3SVladimir Sementsov-Ogievskiy typedef struct { 5886f8cdf3SVladimir Sementsov-Ogievskiy Coroutine *coroutine; 5986f8cdf3SVladimir Sementsov-Ogievskiy uint64_t offset; /* original offset of the request */ 6086f8cdf3SVladimir Sementsov-Ogievskiy bool receiving; /* waiting for connection_co? */ 6186f8cdf3SVladimir Sementsov-Ogievskiy } NBDClientRequest; 6286f8cdf3SVladimir Sementsov-Ogievskiy 63a34b1e5eSVladimir Sementsov-Ogievskiy typedef enum NBDClientState { 64f7651539SVladimir Sementsov-Ogievskiy NBD_CLIENT_CONNECTING_WAIT, 65f7651539SVladimir Sementsov-Ogievskiy NBD_CLIENT_CONNECTING_NOWAIT, 66a34b1e5eSVladimir Sementsov-Ogievskiy NBD_CLIENT_CONNECTED, 67a34b1e5eSVladimir Sementsov-Ogievskiy NBD_CLIENT_QUIT 68a34b1e5eSVladimir Sementsov-Ogievskiy } NBDClientState; 69a34b1e5eSVladimir Sementsov-Ogievskiy 70611ae1d7SVladimir Sementsov-Ogievskiy typedef struct BDRVNBDState { 7195a078eaSVladimir Sementsov-Ogievskiy QIOChannel *ioc; /* The current I/O channel */ 7286f8cdf3SVladimir Sementsov-Ogievskiy NBDExportInfo info; 7386f8cdf3SVladimir Sementsov-Ogievskiy 7486f8cdf3SVladimir Sementsov-Ogievskiy CoMutex send_mutex; 7586f8cdf3SVladimir Sementsov-Ogievskiy CoQueue free_sema; 7686f8cdf3SVladimir Sementsov-Ogievskiy Coroutine *connection_co; 7778c81a3fSMax Reitz Coroutine *teardown_co; 7829a6ea24SPaolo Bonzini QemuCoSleep reconnect_sleep; 79f7651539SVladimir Sementsov-Ogievskiy bool drained; 80f7651539SVladimir Sementsov-Ogievskiy bool wait_drained_end; 8186f8cdf3SVladimir Sementsov-Ogievskiy int in_flight; 82a34b1e5eSVladimir Sementsov-Ogievskiy NBDClientState state; 83f7651539SVladimir Sementsov-Ogievskiy bool wait_in_flight; 8486f8cdf3SVladimir Sementsov-Ogievskiy 8546f56631SVladimir Sementsov-Ogievskiy QEMUTimer *reconnect_delay_timer; 8646f56631SVladimir Sementsov-Ogievskiy 8786f8cdf3SVladimir Sementsov-Ogievskiy NBDClientRequest requests[MAX_NBD_REQUESTS]; 8886f8cdf3SVladimir Sementsov-Ogievskiy NBDReply reply; 8986f8cdf3SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 9003504d05SMax Reitz 918f071c9dSVladimir Sementsov-Ogievskiy /* Connection parameters */ 928f071c9dSVladimir Sementsov-Ogievskiy uint32_t reconnect_delay; 9362cf396bSMarkus Armbruster SocketAddress *saddr; 94491d6c7cSMax Reitz char *export, *tlscredsid; 958f071c9dSVladimir Sementsov-Ogievskiy QCryptoTLSCreds *tlscreds; 968f071c9dSVladimir Sementsov-Ogievskiy const char *hostname; 978f071c9dSVladimir Sementsov-Ogievskiy char *x_dirty_bitmap; 98dbc7b014SEric Blake bool alloc_depth; 991dc4718dSVladimir Sementsov-Ogievskiy 10090ddc64fSVladimir Sementsov-Ogievskiy NBDClientConnection *conn; 101019d6b8fSAnthony Liguori } BDRVNBDState; 102019d6b8fSAnthony Liguori 103fee091cdSLukas Straub static void nbd_yank(void *opaque); 104f7651539SVladimir Sementsov-Ogievskiy 105bbba1c37SVladimir Sementsov-Ogievskiy static void nbd_clear_bdrvstate(BlockDriverState *bs) 1067f493662SPan Nengyuan { 107bbba1c37SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 108e8b35bf5SRoman Kagan 109248d4701SVladimir Sementsov-Ogievskiy nbd_client_connection_release(s->conn); 110248d4701SVladimir Sementsov-Ogievskiy s->conn = NULL; 111bbba1c37SVladimir Sementsov-Ogievskiy 112bbba1c37SVladimir Sementsov-Ogievskiy yank_unregister_instance(BLOCKDEV_YANK_INSTANCE(bs->node_name)); 113bbba1c37SVladimir Sementsov-Ogievskiy 1147f493662SPan Nengyuan object_unref(OBJECT(s->tlscreds)); 1157f493662SPan Nengyuan qapi_free_SocketAddress(s->saddr); 1167f493662SPan Nengyuan s->saddr = NULL; 1177f493662SPan Nengyuan g_free(s->export); 1187f493662SPan Nengyuan s->export = NULL; 1197f493662SPan Nengyuan g_free(s->tlscredsid); 1207f493662SPan Nengyuan s->tlscredsid = NULL; 1217f493662SPan Nengyuan g_free(s->x_dirty_bitmap); 1227f493662SPan Nengyuan s->x_dirty_bitmap = NULL; 1237f493662SPan Nengyuan } 1247f493662SPan Nengyuan 12591e0998fSVladimir Sementsov-Ogievskiy static bool nbd_client_connected(BDRVNBDState *s) 12691e0998fSVladimir Sementsov-Ogievskiy { 12791e0998fSVladimir Sementsov-Ogievskiy return qatomic_load_acquire(&s->state) == NBD_CLIENT_CONNECTED; 12891e0998fSVladimir Sementsov-Ogievskiy } 12991e0998fSVladimir Sementsov-Ogievskiy 130a34b1e5eSVladimir Sementsov-Ogievskiy static void nbd_channel_error(BDRVNBDState *s, int ret) 131a34b1e5eSVladimir Sementsov-Ogievskiy { 132f7651539SVladimir Sementsov-Ogievskiy if (ret == -EIO) { 13391e0998fSVladimir Sementsov-Ogievskiy if (nbd_client_connected(s)) { 134f7651539SVladimir Sementsov-Ogievskiy s->state = s->reconnect_delay ? NBD_CLIENT_CONNECTING_WAIT : 135f7651539SVladimir Sementsov-Ogievskiy NBD_CLIENT_CONNECTING_NOWAIT; 136f7651539SVladimir Sementsov-Ogievskiy } 137f7651539SVladimir Sementsov-Ogievskiy } else { 13891e0998fSVladimir Sementsov-Ogievskiy if (nbd_client_connected(s)) { 139f7651539SVladimir Sementsov-Ogievskiy qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); 140f7651539SVladimir Sementsov-Ogievskiy } 141a34b1e5eSVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_QUIT; 142a34b1e5eSVladimir Sementsov-Ogievskiy } 143f7651539SVladimir Sementsov-Ogievskiy } 144a34b1e5eSVladimir Sementsov-Ogievskiy 145611ae1d7SVladimir Sementsov-Ogievskiy static void nbd_recv_coroutines_wake_all(BDRVNBDState *s) 14686f8cdf3SVladimir Sementsov-Ogievskiy { 14786f8cdf3SVladimir Sementsov-Ogievskiy int i; 14886f8cdf3SVladimir Sementsov-Ogievskiy 14986f8cdf3SVladimir Sementsov-Ogievskiy for (i = 0; i < MAX_NBD_REQUESTS; i++) { 15086f8cdf3SVladimir Sementsov-Ogievskiy NBDClientRequest *req = &s->requests[i]; 15186f8cdf3SVladimir Sementsov-Ogievskiy 15286f8cdf3SVladimir Sementsov-Ogievskiy if (req->coroutine && req->receiving) { 153bbfb7c2fSVladimir Sementsov-Ogievskiy req->receiving = false; 15486f8cdf3SVladimir Sementsov-Ogievskiy aio_co_wake(req->coroutine); 15586f8cdf3SVladimir Sementsov-Ogievskiy } 15686f8cdf3SVladimir Sementsov-Ogievskiy } 15786f8cdf3SVladimir Sementsov-Ogievskiy } 15886f8cdf3SVladimir Sementsov-Ogievskiy 15946f56631SVladimir Sementsov-Ogievskiy static void reconnect_delay_timer_del(BDRVNBDState *s) 16046f56631SVladimir Sementsov-Ogievskiy { 16146f56631SVladimir Sementsov-Ogievskiy if (s->reconnect_delay_timer) { 16246f56631SVladimir Sementsov-Ogievskiy timer_free(s->reconnect_delay_timer); 16346f56631SVladimir Sementsov-Ogievskiy s->reconnect_delay_timer = NULL; 16446f56631SVladimir Sementsov-Ogievskiy } 16546f56631SVladimir Sementsov-Ogievskiy } 16646f56631SVladimir Sementsov-Ogievskiy 16746f56631SVladimir Sementsov-Ogievskiy static void reconnect_delay_timer_cb(void *opaque) 16846f56631SVladimir Sementsov-Ogievskiy { 16946f56631SVladimir Sementsov-Ogievskiy BDRVNBDState *s = opaque; 17046f56631SVladimir Sementsov-Ogievskiy 171fee091cdSLukas Straub if (qatomic_load_acquire(&s->state) == NBD_CLIENT_CONNECTING_WAIT) { 17246f56631SVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_CONNECTING_NOWAIT; 17346f56631SVladimir Sementsov-Ogievskiy while (qemu_co_enter_next(&s->free_sema, NULL)) { 17446f56631SVladimir Sementsov-Ogievskiy /* Resume all queued requests */ 17546f56631SVladimir Sementsov-Ogievskiy } 17646f56631SVladimir Sementsov-Ogievskiy } 17746f56631SVladimir Sementsov-Ogievskiy 17846f56631SVladimir Sementsov-Ogievskiy reconnect_delay_timer_del(s); 17946f56631SVladimir Sementsov-Ogievskiy } 18046f56631SVladimir Sementsov-Ogievskiy 18146f56631SVladimir Sementsov-Ogievskiy static void reconnect_delay_timer_init(BDRVNBDState *s, uint64_t expire_time_ns) 18246f56631SVladimir Sementsov-Ogievskiy { 183fee091cdSLukas Straub if (qatomic_load_acquire(&s->state) != NBD_CLIENT_CONNECTING_WAIT) { 18446f56631SVladimir Sementsov-Ogievskiy return; 18546f56631SVladimir Sementsov-Ogievskiy } 18646f56631SVladimir Sementsov-Ogievskiy 18746f56631SVladimir Sementsov-Ogievskiy assert(!s->reconnect_delay_timer); 18846f56631SVladimir Sementsov-Ogievskiy s->reconnect_delay_timer = aio_timer_new(bdrv_get_aio_context(s->bs), 18946f56631SVladimir Sementsov-Ogievskiy QEMU_CLOCK_REALTIME, 19046f56631SVladimir Sementsov-Ogievskiy SCALE_NS, 19146f56631SVladimir Sementsov-Ogievskiy reconnect_delay_timer_cb, s); 19246f56631SVladimir Sementsov-Ogievskiy timer_mod(s->reconnect_delay_timer, expire_time_ns); 19346f56631SVladimir Sementsov-Ogievskiy } 19446f56631SVladimir Sementsov-Ogievskiy 19586f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_client_detach_aio_context(BlockDriverState *bs) 19686f8cdf3SVladimir Sementsov-Ogievskiy { 197611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 198611ae1d7SVladimir Sementsov-Ogievskiy 19946f56631SVladimir Sementsov-Ogievskiy /* Timer is deleted in nbd_client_co_drain_begin() */ 20046f56631SVladimir Sementsov-Ogievskiy assert(!s->reconnect_delay_timer); 2013b5e4db6SRoman Kagan /* 2023b5e4db6SRoman Kagan * If reconnect is in progress we may have no ->ioc. It will be 2033b5e4db6SRoman Kagan * re-instantiated in the proper aio context once the connection is 2043b5e4db6SRoman Kagan * reestablished. 2053b5e4db6SRoman Kagan */ 2063b5e4db6SRoman Kagan if (s->ioc) { 207611ae1d7SVladimir Sementsov-Ogievskiy qio_channel_detach_aio_context(QIO_CHANNEL(s->ioc)); 20886f8cdf3SVladimir Sementsov-Ogievskiy } 2093b5e4db6SRoman Kagan } 21086f8cdf3SVladimir Sementsov-Ogievskiy 21186f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_client_attach_aio_context_bh(void *opaque) 21286f8cdf3SVladimir Sementsov-Ogievskiy { 21386f8cdf3SVladimir Sementsov-Ogievskiy BlockDriverState *bs = opaque; 214611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 21586f8cdf3SVladimir Sementsov-Ogievskiy 216ddde5ee7SRoman Kagan if (s->connection_co) { 21786f8cdf3SVladimir Sementsov-Ogievskiy /* 21886f8cdf3SVladimir Sementsov-Ogievskiy * The node is still drained, so we know the coroutine has yielded in 219ddde5ee7SRoman Kagan * nbd_read_eof(), the only place where bs->in_flight can reach 0, or 220ddde5ee7SRoman Kagan * it is entered for the first time. Both places are safe for entering 221ddde5ee7SRoman Kagan * the coroutine. 22286f8cdf3SVladimir Sementsov-Ogievskiy */ 223611ae1d7SVladimir Sementsov-Ogievskiy qemu_aio_coroutine_enter(bs->aio_context, s->connection_co); 224ddde5ee7SRoman Kagan } 22586f8cdf3SVladimir Sementsov-Ogievskiy bdrv_dec_in_flight(bs); 22686f8cdf3SVladimir Sementsov-Ogievskiy } 22786f8cdf3SVladimir Sementsov-Ogievskiy 22886f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_client_attach_aio_context(BlockDriverState *bs, 22986f8cdf3SVladimir Sementsov-Ogievskiy AioContext *new_context) 23086f8cdf3SVladimir Sementsov-Ogievskiy { 231611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 232611ae1d7SVladimir Sementsov-Ogievskiy 233f7651539SVladimir Sementsov-Ogievskiy /* 234f7651539SVladimir Sementsov-Ogievskiy * s->connection_co is either yielded from nbd_receive_reply or from 235f7651539SVladimir Sementsov-Ogievskiy * nbd_co_reconnect_loop() 236f7651539SVladimir Sementsov-Ogievskiy */ 23791e0998fSVladimir Sementsov-Ogievskiy if (nbd_client_connected(s)) { 238611ae1d7SVladimir Sementsov-Ogievskiy qio_channel_attach_aio_context(QIO_CHANNEL(s->ioc), new_context); 239f7651539SVladimir Sementsov-Ogievskiy } 24086f8cdf3SVladimir Sementsov-Ogievskiy 24186f8cdf3SVladimir Sementsov-Ogievskiy bdrv_inc_in_flight(bs); 24286f8cdf3SVladimir Sementsov-Ogievskiy 24386f8cdf3SVladimir Sementsov-Ogievskiy /* 24486f8cdf3SVladimir Sementsov-Ogievskiy * Need to wait here for the BH to run because the BH must run while the 24586f8cdf3SVladimir Sementsov-Ogievskiy * node is still drained. 24686f8cdf3SVladimir Sementsov-Ogievskiy */ 24786f8cdf3SVladimir Sementsov-Ogievskiy aio_wait_bh_oneshot(new_context, nbd_client_attach_aio_context_bh, bs); 24886f8cdf3SVladimir Sementsov-Ogievskiy } 24986f8cdf3SVladimir Sementsov-Ogievskiy 250f7651539SVladimir Sementsov-Ogievskiy static void coroutine_fn nbd_client_co_drain_begin(BlockDriverState *bs) 251f7651539SVladimir Sementsov-Ogievskiy { 252f7651539SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 253f7651539SVladimir Sementsov-Ogievskiy 254f7651539SVladimir Sementsov-Ogievskiy s->drained = true; 25529a6ea24SPaolo Bonzini qemu_co_sleep_wake(&s->reconnect_sleep); 2561dc4718dSVladimir Sementsov-Ogievskiy 25790ddc64fSVladimir Sementsov-Ogievskiy nbd_co_establish_connection_cancel(s->conn); 2588c517de2SVladimir Sementsov-Ogievskiy 25946f56631SVladimir Sementsov-Ogievskiy reconnect_delay_timer_del(s); 26046f56631SVladimir Sementsov-Ogievskiy 261fee091cdSLukas Straub if (qatomic_load_acquire(&s->state) == NBD_CLIENT_CONNECTING_WAIT) { 2628c517de2SVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_CONNECTING_NOWAIT; 2638c517de2SVladimir Sementsov-Ogievskiy qemu_co_queue_restart_all(&s->free_sema); 2648c517de2SVladimir Sementsov-Ogievskiy } 265f7651539SVladimir Sementsov-Ogievskiy } 266f7651539SVladimir Sementsov-Ogievskiy 267f7651539SVladimir Sementsov-Ogievskiy static void coroutine_fn nbd_client_co_drain_end(BlockDriverState *bs) 268f7651539SVladimir Sementsov-Ogievskiy { 269f7651539SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 270f7651539SVladimir Sementsov-Ogievskiy 271f7651539SVladimir Sementsov-Ogievskiy s->drained = false; 272f7651539SVladimir Sementsov-Ogievskiy if (s->wait_drained_end) { 273f7651539SVladimir Sementsov-Ogievskiy s->wait_drained_end = false; 274f7651539SVladimir Sementsov-Ogievskiy aio_co_wake(s->connection_co); 275f7651539SVladimir Sementsov-Ogievskiy } 276f7651539SVladimir Sementsov-Ogievskiy } 277f7651539SVladimir Sementsov-Ogievskiy 27886f8cdf3SVladimir Sementsov-Ogievskiy 27986f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_teardown_connection(BlockDriverState *bs) 28086f8cdf3SVladimir Sementsov-Ogievskiy { 281611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 28286f8cdf3SVladimir Sementsov-Ogievskiy 283fbeb3e63SVladimir Sementsov-Ogievskiy if (s->ioc) { 28486f8cdf3SVladimir Sementsov-Ogievskiy /* finish any pending coroutines */ 285f7651539SVladimir Sementsov-Ogievskiy qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); 286f7651539SVladimir Sementsov-Ogievskiy } 287fbeb3e63SVladimir Sementsov-Ogievskiy 288f7651539SVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_QUIT; 289f7651539SVladimir Sementsov-Ogievskiy if (s->connection_co) { 29029a6ea24SPaolo Bonzini qemu_co_sleep_wake(&s->reconnect_sleep); 29190ddc64fSVladimir Sementsov-Ogievskiy nbd_co_establish_connection_cancel(s->conn); 292f7651539SVladimir Sementsov-Ogievskiy } 29378c81a3fSMax Reitz if (qemu_in_coroutine()) { 29478c81a3fSMax Reitz s->teardown_co = qemu_coroutine_self(); 29578c81a3fSMax Reitz /* connection_co resumes us when it terminates */ 29678c81a3fSMax Reitz qemu_coroutine_yield(); 29778c81a3fSMax Reitz s->teardown_co = NULL; 29878c81a3fSMax Reitz } else { 299611ae1d7SVladimir Sementsov-Ogievskiy BDRV_POLL_WHILE(bs, s->connection_co); 300f7651539SVladimir Sementsov-Ogievskiy } 30178c81a3fSMax Reitz assert(!s->connection_co); 30278c81a3fSMax Reitz } 30386f8cdf3SVladimir Sementsov-Ogievskiy 304f7651539SVladimir Sementsov-Ogievskiy static bool nbd_client_connecting(BDRVNBDState *s) 305f7651539SVladimir Sementsov-Ogievskiy { 306fee091cdSLukas Straub NBDClientState state = qatomic_load_acquire(&s->state); 307fee091cdSLukas Straub return state == NBD_CLIENT_CONNECTING_WAIT || 308fee091cdSLukas Straub state == NBD_CLIENT_CONNECTING_NOWAIT; 309f7651539SVladimir Sementsov-Ogievskiy } 310f7651539SVladimir Sementsov-Ogievskiy 311f7651539SVladimir Sementsov-Ogievskiy static bool nbd_client_connecting_wait(BDRVNBDState *s) 312f7651539SVladimir Sementsov-Ogievskiy { 313fee091cdSLukas Straub return qatomic_load_acquire(&s->state) == NBD_CLIENT_CONNECTING_WAIT; 314f7651539SVladimir Sementsov-Ogievskiy } 315f7651539SVladimir Sementsov-Ogievskiy 316e9ba7788SVladimir Sementsov-Ogievskiy /* 317e9ba7788SVladimir Sementsov-Ogievskiy * Update @bs with information learned during a completed negotiation process. 318e9ba7788SVladimir Sementsov-Ogievskiy * Return failure if the server's advertised options are incompatible with the 319e9ba7788SVladimir Sementsov-Ogievskiy * client's needs. 320e9ba7788SVladimir Sementsov-Ogievskiy */ 321e9ba7788SVladimir Sementsov-Ogievskiy static int nbd_handle_updated_info(BlockDriverState *bs, Error **errp) 322e9ba7788SVladimir Sementsov-Ogievskiy { 323e9ba7788SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 324e9ba7788SVladimir Sementsov-Ogievskiy int ret; 325e9ba7788SVladimir Sementsov-Ogievskiy 326e9ba7788SVladimir Sementsov-Ogievskiy if (s->x_dirty_bitmap) { 327e9ba7788SVladimir Sementsov-Ogievskiy if (!s->info.base_allocation) { 328e9ba7788SVladimir Sementsov-Ogievskiy error_setg(errp, "requested x-dirty-bitmap %s not found", 329e9ba7788SVladimir Sementsov-Ogievskiy s->x_dirty_bitmap); 330e9ba7788SVladimir Sementsov-Ogievskiy return -EINVAL; 331e9ba7788SVladimir Sementsov-Ogievskiy } 332e9ba7788SVladimir Sementsov-Ogievskiy if (strcmp(s->x_dirty_bitmap, "qemu:allocation-depth") == 0) { 333e9ba7788SVladimir Sementsov-Ogievskiy s->alloc_depth = true; 334e9ba7788SVladimir Sementsov-Ogievskiy } 335e9ba7788SVladimir Sementsov-Ogievskiy } 336e9ba7788SVladimir Sementsov-Ogievskiy 337e9ba7788SVladimir Sementsov-Ogievskiy if (s->info.flags & NBD_FLAG_READ_ONLY) { 338e9ba7788SVladimir Sementsov-Ogievskiy ret = bdrv_apply_auto_read_only(bs, "NBD export is read-only", errp); 339e9ba7788SVladimir Sementsov-Ogievskiy if (ret < 0) { 340e9ba7788SVladimir Sementsov-Ogievskiy return ret; 341e9ba7788SVladimir Sementsov-Ogievskiy } 342e9ba7788SVladimir Sementsov-Ogievskiy } 343e9ba7788SVladimir Sementsov-Ogievskiy 344e9ba7788SVladimir Sementsov-Ogievskiy if (s->info.flags & NBD_FLAG_SEND_FUA) { 345e9ba7788SVladimir Sementsov-Ogievskiy bs->supported_write_flags = BDRV_REQ_FUA; 346e9ba7788SVladimir Sementsov-Ogievskiy bs->supported_zero_flags |= BDRV_REQ_FUA; 347e9ba7788SVladimir Sementsov-Ogievskiy } 348e9ba7788SVladimir Sementsov-Ogievskiy 349e9ba7788SVladimir Sementsov-Ogievskiy if (s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES) { 350e9ba7788SVladimir Sementsov-Ogievskiy bs->supported_zero_flags |= BDRV_REQ_MAY_UNMAP; 351e9ba7788SVladimir Sementsov-Ogievskiy if (s->info.flags & NBD_FLAG_SEND_FAST_ZERO) { 352e9ba7788SVladimir Sementsov-Ogievskiy bs->supported_zero_flags |= BDRV_REQ_NO_FALLBACK; 353e9ba7788SVladimir Sementsov-Ogievskiy } 354e9ba7788SVladimir Sementsov-Ogievskiy } 355e9ba7788SVladimir Sementsov-Ogievskiy 356e9ba7788SVladimir Sementsov-Ogievskiy trace_nbd_client_handshake_success(s->export); 357e9ba7788SVladimir Sementsov-Ogievskiy 358e9ba7788SVladimir Sementsov-Ogievskiy return 0; 359e9ba7788SVladimir Sementsov-Ogievskiy } 360e9ba7788SVladimir Sementsov-Ogievskiy 361a71d597bSVladimir Sementsov-Ogievskiy int coroutine_fn nbd_co_do_establish_connection(BlockDriverState *bs, 36251edbf53SVladimir Sementsov-Ogievskiy Error **errp) 36351edbf53SVladimir Sementsov-Ogievskiy { 36451edbf53SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 36551edbf53SVladimir Sementsov-Ogievskiy int ret; 36651edbf53SVladimir Sementsov-Ogievskiy 36751edbf53SVladimir Sementsov-Ogievskiy assert(!s->ioc); 36851edbf53SVladimir Sementsov-Ogievskiy 36997cf8925SVladimir Sementsov-Ogievskiy s->ioc = nbd_co_establish_connection(s->conn, &s->info, true, errp); 37051edbf53SVladimir Sementsov-Ogievskiy if (!s->ioc) { 37151edbf53SVladimir Sementsov-Ogievskiy return -ECONNREFUSED; 37251edbf53SVladimir Sementsov-Ogievskiy } 37351edbf53SVladimir Sementsov-Ogievskiy 37451edbf53SVladimir Sementsov-Ogievskiy ret = nbd_handle_updated_info(s->bs, NULL); 37551edbf53SVladimir Sementsov-Ogievskiy if (ret < 0) { 37651edbf53SVladimir Sementsov-Ogievskiy /* 37751edbf53SVladimir Sementsov-Ogievskiy * We have connected, but must fail for other reasons. 37851edbf53SVladimir Sementsov-Ogievskiy * Send NBD_CMD_DISC as a courtesy to the server. 37951edbf53SVladimir Sementsov-Ogievskiy */ 38051edbf53SVladimir Sementsov-Ogievskiy NBDRequest request = { .type = NBD_CMD_DISC }; 38151edbf53SVladimir Sementsov-Ogievskiy 38251edbf53SVladimir Sementsov-Ogievskiy nbd_send_request(s->ioc, &request); 38351edbf53SVladimir Sementsov-Ogievskiy 38451edbf53SVladimir Sementsov-Ogievskiy object_unref(OBJECT(s->ioc)); 38551edbf53SVladimir Sementsov-Ogievskiy s->ioc = NULL; 38651edbf53SVladimir Sementsov-Ogievskiy 38751edbf53SVladimir Sementsov-Ogievskiy return ret; 38851edbf53SVladimir Sementsov-Ogievskiy } 38951edbf53SVladimir Sementsov-Ogievskiy 39051edbf53SVladimir Sementsov-Ogievskiy qio_channel_set_blocking(s->ioc, false, NULL); 39151edbf53SVladimir Sementsov-Ogievskiy qio_channel_attach_aio_context(s->ioc, bdrv_get_aio_context(bs)); 39251edbf53SVladimir Sementsov-Ogievskiy 39351edbf53SVladimir Sementsov-Ogievskiy yank_register_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), nbd_yank, 39451edbf53SVladimir Sementsov-Ogievskiy bs); 39551edbf53SVladimir Sementsov-Ogievskiy 39651edbf53SVladimir Sementsov-Ogievskiy /* successfully connected */ 39751edbf53SVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_CONNECTED; 39851edbf53SVladimir Sementsov-Ogievskiy qemu_co_queue_restart_all(&s->free_sema); 39951edbf53SVladimir Sementsov-Ogievskiy 40051edbf53SVladimir Sementsov-Ogievskiy return 0; 40151edbf53SVladimir Sementsov-Ogievskiy } 40251edbf53SVladimir Sementsov-Ogievskiy 403f7651539SVladimir Sementsov-Ogievskiy static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s) 404f7651539SVladimir Sementsov-Ogievskiy { 405f7651539SVladimir Sementsov-Ogievskiy if (!nbd_client_connecting(s)) { 406f7651539SVladimir Sementsov-Ogievskiy return; 407f7651539SVladimir Sementsov-Ogievskiy } 408f7651539SVladimir Sementsov-Ogievskiy 409f7651539SVladimir Sementsov-Ogievskiy /* Wait for completion of all in-flight requests */ 410f7651539SVladimir Sementsov-Ogievskiy 411f7651539SVladimir Sementsov-Ogievskiy qemu_co_mutex_lock(&s->send_mutex); 412f7651539SVladimir Sementsov-Ogievskiy 413f7651539SVladimir Sementsov-Ogievskiy while (s->in_flight > 0) { 414f7651539SVladimir Sementsov-Ogievskiy qemu_co_mutex_unlock(&s->send_mutex); 415f7651539SVladimir Sementsov-Ogievskiy nbd_recv_coroutines_wake_all(s); 416f7651539SVladimir Sementsov-Ogievskiy s->wait_in_flight = true; 417f7651539SVladimir Sementsov-Ogievskiy qemu_coroutine_yield(); 418f7651539SVladimir Sementsov-Ogievskiy s->wait_in_flight = false; 419f7651539SVladimir Sementsov-Ogievskiy qemu_co_mutex_lock(&s->send_mutex); 420f7651539SVladimir Sementsov-Ogievskiy } 421f7651539SVladimir Sementsov-Ogievskiy 422f7651539SVladimir Sementsov-Ogievskiy qemu_co_mutex_unlock(&s->send_mutex); 423f7651539SVladimir Sementsov-Ogievskiy 424f7651539SVladimir Sementsov-Ogievskiy if (!nbd_client_connecting(s)) { 425f7651539SVladimir Sementsov-Ogievskiy return; 426f7651539SVladimir Sementsov-Ogievskiy } 427f7651539SVladimir Sementsov-Ogievskiy 428f7651539SVladimir Sementsov-Ogievskiy /* 429f7651539SVladimir Sementsov-Ogievskiy * Now we are sure that nobody is accessing the channel, and no one will 430f7651539SVladimir Sementsov-Ogievskiy * try until we set the state to CONNECTED. 431f7651539SVladimir Sementsov-Ogievskiy */ 432f7651539SVladimir Sementsov-Ogievskiy 433f7651539SVladimir Sementsov-Ogievskiy /* Finalize previous connection if any */ 434f7651539SVladimir Sementsov-Ogievskiy if (s->ioc) { 4358a509afdSVladimir Sementsov-Ogievskiy qio_channel_detach_aio_context(QIO_CHANNEL(s->ioc)); 436fee091cdSLukas Straub yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), 437fee091cdSLukas Straub nbd_yank, s->bs); 438611ae1d7SVladimir Sementsov-Ogievskiy object_unref(OBJECT(s->ioc)); 439611ae1d7SVladimir Sementsov-Ogievskiy s->ioc = NULL; 44086f8cdf3SVladimir Sementsov-Ogievskiy } 44186f8cdf3SVladimir Sementsov-Ogievskiy 44251edbf53SVladimir Sementsov-Ogievskiy nbd_co_do_establish_connection(s->bs, NULL); 443fa35591bSVladimir Sementsov-Ogievskiy } 444f7651539SVladimir Sementsov-Ogievskiy 445f7651539SVladimir Sementsov-Ogievskiy static coroutine_fn void nbd_co_reconnect_loop(BDRVNBDState *s) 446f7651539SVladimir Sementsov-Ogievskiy { 447f7651539SVladimir Sementsov-Ogievskiy uint64_t timeout = 1 * NANOSECONDS_PER_SECOND; 448f7651539SVladimir Sementsov-Ogievskiy uint64_t max_timeout = 16 * NANOSECONDS_PER_SECOND; 449f7651539SVladimir Sementsov-Ogievskiy 450fee091cdSLukas Straub if (qatomic_load_acquire(&s->state) == NBD_CLIENT_CONNECTING_WAIT) { 45146f56631SVladimir Sementsov-Ogievskiy reconnect_delay_timer_init(s, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 45246f56631SVladimir Sementsov-Ogievskiy s->reconnect_delay * NANOSECONDS_PER_SECOND); 45346f56631SVladimir Sementsov-Ogievskiy } 45446f56631SVladimir Sementsov-Ogievskiy 455f7651539SVladimir Sementsov-Ogievskiy nbd_reconnect_attempt(s); 456f7651539SVladimir Sementsov-Ogievskiy 457f7651539SVladimir Sementsov-Ogievskiy while (nbd_client_connecting(s)) { 458f7651539SVladimir Sementsov-Ogievskiy if (s->drained) { 459f7651539SVladimir Sementsov-Ogievskiy bdrv_dec_in_flight(s->bs); 460f7651539SVladimir Sementsov-Ogievskiy s->wait_drained_end = true; 461f7651539SVladimir Sementsov-Ogievskiy while (s->drained) { 462f7651539SVladimir Sementsov-Ogievskiy /* 463f7651539SVladimir Sementsov-Ogievskiy * We may be entered once from nbd_client_attach_aio_context_bh 464f7651539SVladimir Sementsov-Ogievskiy * and then from nbd_client_co_drain_end. So here is a loop. 465f7651539SVladimir Sementsov-Ogievskiy */ 466f7651539SVladimir Sementsov-Ogievskiy qemu_coroutine_yield(); 467f7651539SVladimir Sementsov-Ogievskiy } 468f7651539SVladimir Sementsov-Ogievskiy bdrv_inc_in_flight(s->bs); 46912c75e20SVladimir Sementsov-Ogievskiy } else { 47029a6ea24SPaolo Bonzini qemu_co_sleep_ns_wakeable(&s->reconnect_sleep, 47129a6ea24SPaolo Bonzini QEMU_CLOCK_REALTIME, timeout); 47299d72dbaSVladimir Sementsov-Ogievskiy if (s->drained) { 47399d72dbaSVladimir Sementsov-Ogievskiy continue; 47499d72dbaSVladimir Sementsov-Ogievskiy } 475f7651539SVladimir Sementsov-Ogievskiy if (timeout < max_timeout) { 476f7651539SVladimir Sementsov-Ogievskiy timeout *= 2; 477f7651539SVladimir Sementsov-Ogievskiy } 47812c75e20SVladimir Sementsov-Ogievskiy } 479f7651539SVladimir Sementsov-Ogievskiy 480f7651539SVladimir Sementsov-Ogievskiy nbd_reconnect_attempt(s); 481f7651539SVladimir Sementsov-Ogievskiy } 48246f56631SVladimir Sementsov-Ogievskiy 48346f56631SVladimir Sementsov-Ogievskiy reconnect_delay_timer_del(s); 484f7651539SVladimir Sementsov-Ogievskiy } 485f7651539SVladimir Sementsov-Ogievskiy 48686f8cdf3SVladimir Sementsov-Ogievskiy static coroutine_fn void nbd_connection_entry(void *opaque) 48786f8cdf3SVladimir Sementsov-Ogievskiy { 488611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = opaque; 48986f8cdf3SVladimir Sementsov-Ogievskiy uint64_t i; 49086f8cdf3SVladimir Sementsov-Ogievskiy int ret = 0; 49186f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 49286f8cdf3SVladimir Sementsov-Ogievskiy 493fee091cdSLukas Straub while (qatomic_load_acquire(&s->state) != NBD_CLIENT_QUIT) { 49486f8cdf3SVladimir Sementsov-Ogievskiy /* 49586f8cdf3SVladimir Sementsov-Ogievskiy * The NBD client can only really be considered idle when it has 49686f8cdf3SVladimir Sementsov-Ogievskiy * yielded from qio_channel_readv_all_eof(), waiting for data. This is 49786f8cdf3SVladimir Sementsov-Ogievskiy * the point where the additional scheduled coroutine entry happens 49886f8cdf3SVladimir Sementsov-Ogievskiy * after nbd_client_attach_aio_context(). 49986f8cdf3SVladimir Sementsov-Ogievskiy * 50086f8cdf3SVladimir Sementsov-Ogievskiy * Therefore we keep an additional in_flight reference all the time and 50186f8cdf3SVladimir Sementsov-Ogievskiy * only drop it temporarily here. 50286f8cdf3SVladimir Sementsov-Ogievskiy */ 503f7651539SVladimir Sementsov-Ogievskiy 504f7651539SVladimir Sementsov-Ogievskiy if (nbd_client_connecting(s)) { 505f7651539SVladimir Sementsov-Ogievskiy nbd_co_reconnect_loop(s); 506f7651539SVladimir Sementsov-Ogievskiy } 507f7651539SVladimir Sementsov-Ogievskiy 50891e0998fSVladimir Sementsov-Ogievskiy if (!nbd_client_connected(s)) { 509f7651539SVladimir Sementsov-Ogievskiy continue; 510f7651539SVladimir Sementsov-Ogievskiy } 511f7651539SVladimir Sementsov-Ogievskiy 51286f8cdf3SVladimir Sementsov-Ogievskiy assert(s->reply.handle == 0); 51386f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_receive_reply(s->bs, s->ioc, &s->reply, &local_err); 51486f8cdf3SVladimir Sementsov-Ogievskiy 51586f8cdf3SVladimir Sementsov-Ogievskiy if (local_err) { 51686f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_read_reply_entry_fail(ret, error_get_pretty(local_err)); 51786f8cdf3SVladimir Sementsov-Ogievskiy error_free(local_err); 518f7651539SVladimir Sementsov-Ogievskiy local_err = NULL; 51986f8cdf3SVladimir Sementsov-Ogievskiy } 52086f8cdf3SVladimir Sementsov-Ogievskiy if (ret <= 0) { 521a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, ret ? ret : -EIO); 522f7651539SVladimir Sementsov-Ogievskiy continue; 52386f8cdf3SVladimir Sementsov-Ogievskiy } 52486f8cdf3SVladimir Sementsov-Ogievskiy 52586f8cdf3SVladimir Sementsov-Ogievskiy /* 52686f8cdf3SVladimir Sementsov-Ogievskiy * There's no need for a mutex on the receive side, because the 52786f8cdf3SVladimir Sementsov-Ogievskiy * handler acts as a synchronization point and ensures that only 52886f8cdf3SVladimir Sementsov-Ogievskiy * one coroutine is called until the reply finishes. 52986f8cdf3SVladimir Sementsov-Ogievskiy */ 53086f8cdf3SVladimir Sementsov-Ogievskiy i = HANDLE_TO_INDEX(s, s->reply.handle); 53186f8cdf3SVladimir Sementsov-Ogievskiy if (i >= MAX_NBD_REQUESTS || 53286f8cdf3SVladimir Sementsov-Ogievskiy !s->requests[i].coroutine || 53386f8cdf3SVladimir Sementsov-Ogievskiy !s->requests[i].receiving || 53486f8cdf3SVladimir Sementsov-Ogievskiy (nbd_reply_is_structured(&s->reply) && !s->info.structured_reply)) 53586f8cdf3SVladimir Sementsov-Ogievskiy { 536a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, -EINVAL); 537f7651539SVladimir Sementsov-Ogievskiy continue; 53886f8cdf3SVladimir Sementsov-Ogievskiy } 53986f8cdf3SVladimir Sementsov-Ogievskiy 54086f8cdf3SVladimir Sementsov-Ogievskiy /* 54186f8cdf3SVladimir Sementsov-Ogievskiy * We're woken up again by the request itself. Note that there 54286f8cdf3SVladimir Sementsov-Ogievskiy * is no race between yielding and reentering connection_co. This 54386f8cdf3SVladimir Sementsov-Ogievskiy * is because: 54486f8cdf3SVladimir Sementsov-Ogievskiy * 54586f8cdf3SVladimir Sementsov-Ogievskiy * - if the request runs on the same AioContext, it is only 54686f8cdf3SVladimir Sementsov-Ogievskiy * entered after we yield 54786f8cdf3SVladimir Sementsov-Ogievskiy * 54886f8cdf3SVladimir Sementsov-Ogievskiy * - if the request runs on a different AioContext, reentering 54986f8cdf3SVladimir Sementsov-Ogievskiy * connection_co happens through a bottom half, which can only 55086f8cdf3SVladimir Sementsov-Ogievskiy * run after we yield. 55186f8cdf3SVladimir Sementsov-Ogievskiy */ 552bbfb7c2fSVladimir Sementsov-Ogievskiy s->requests[i].receiving = false; 55386f8cdf3SVladimir Sementsov-Ogievskiy aio_co_wake(s->requests[i].coroutine); 55486f8cdf3SVladimir Sementsov-Ogievskiy qemu_coroutine_yield(); 55586f8cdf3SVladimir Sementsov-Ogievskiy } 55686f8cdf3SVladimir Sementsov-Ogievskiy 557f7651539SVladimir Sementsov-Ogievskiy qemu_co_queue_restart_all(&s->free_sema); 55886f8cdf3SVladimir Sementsov-Ogievskiy nbd_recv_coroutines_wake_all(s); 55986f8cdf3SVladimir Sementsov-Ogievskiy bdrv_dec_in_flight(s->bs); 56086f8cdf3SVladimir Sementsov-Ogievskiy 56186f8cdf3SVladimir Sementsov-Ogievskiy s->connection_co = NULL; 562f7651539SVladimir Sementsov-Ogievskiy if (s->ioc) { 5638a509afdSVladimir Sementsov-Ogievskiy qio_channel_detach_aio_context(QIO_CHANNEL(s->ioc)); 564fee091cdSLukas Straub yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), 565fee091cdSLukas Straub nbd_yank, s->bs); 566f7651539SVladimir Sementsov-Ogievskiy object_unref(OBJECT(s->ioc)); 567f7651539SVladimir Sementsov-Ogievskiy s->ioc = NULL; 568f7651539SVladimir Sementsov-Ogievskiy } 569f7651539SVladimir Sementsov-Ogievskiy 57078c81a3fSMax Reitz if (s->teardown_co) { 57178c81a3fSMax Reitz aio_co_wake(s->teardown_co); 57278c81a3fSMax Reitz } 57386f8cdf3SVladimir Sementsov-Ogievskiy aio_wait_kick(); 57486f8cdf3SVladimir Sementsov-Ogievskiy } 57586f8cdf3SVladimir Sementsov-Ogievskiy 57686f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_co_send_request(BlockDriverState *bs, 57786f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest *request, 57886f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector *qiov) 57986f8cdf3SVladimir Sementsov-Ogievskiy { 580611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 581a34b1e5eSVladimir Sementsov-Ogievskiy int rc, i = -1; 58286f8cdf3SVladimir Sementsov-Ogievskiy 58386f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_mutex_lock(&s->send_mutex); 584f7651539SVladimir Sementsov-Ogievskiy while (s->in_flight == MAX_NBD_REQUESTS || nbd_client_connecting_wait(s)) { 58586f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_queue_wait(&s->free_sema, &s->send_mutex); 58686f8cdf3SVladimir Sementsov-Ogievskiy } 587a34b1e5eSVladimir Sementsov-Ogievskiy 58891e0998fSVladimir Sementsov-Ogievskiy if (!nbd_client_connected(s)) { 589a34b1e5eSVladimir Sementsov-Ogievskiy rc = -EIO; 590a34b1e5eSVladimir Sementsov-Ogievskiy goto err; 591a34b1e5eSVladimir Sementsov-Ogievskiy } 592a34b1e5eSVladimir Sementsov-Ogievskiy 59386f8cdf3SVladimir Sementsov-Ogievskiy s->in_flight++; 59486f8cdf3SVladimir Sementsov-Ogievskiy 59586f8cdf3SVladimir Sementsov-Ogievskiy for (i = 0; i < MAX_NBD_REQUESTS; i++) { 59686f8cdf3SVladimir Sementsov-Ogievskiy if (s->requests[i].coroutine == NULL) { 59786f8cdf3SVladimir Sementsov-Ogievskiy break; 59886f8cdf3SVladimir Sementsov-Ogievskiy } 59986f8cdf3SVladimir Sementsov-Ogievskiy } 60086f8cdf3SVladimir Sementsov-Ogievskiy 60186f8cdf3SVladimir Sementsov-Ogievskiy g_assert(qemu_in_coroutine()); 60286f8cdf3SVladimir Sementsov-Ogievskiy assert(i < MAX_NBD_REQUESTS); 60386f8cdf3SVladimir Sementsov-Ogievskiy 60486f8cdf3SVladimir Sementsov-Ogievskiy s->requests[i].coroutine = qemu_coroutine_self(); 60586f8cdf3SVladimir Sementsov-Ogievskiy s->requests[i].offset = request->from; 60686f8cdf3SVladimir Sementsov-Ogievskiy s->requests[i].receiving = false; 60786f8cdf3SVladimir Sementsov-Ogievskiy 60886f8cdf3SVladimir Sementsov-Ogievskiy request->handle = INDEX_TO_HANDLE(s, i); 60986f8cdf3SVladimir Sementsov-Ogievskiy 61086f8cdf3SVladimir Sementsov-Ogievskiy assert(s->ioc); 61186f8cdf3SVladimir Sementsov-Ogievskiy 61286f8cdf3SVladimir Sementsov-Ogievskiy if (qiov) { 61386f8cdf3SVladimir Sementsov-Ogievskiy qio_channel_set_cork(s->ioc, true); 61486f8cdf3SVladimir Sementsov-Ogievskiy rc = nbd_send_request(s->ioc, request); 61591e0998fSVladimir Sementsov-Ogievskiy if (nbd_client_connected(s) && rc >= 0) { 61686f8cdf3SVladimir Sementsov-Ogievskiy if (qio_channel_writev_all(s->ioc, qiov->iov, qiov->niov, 61786f8cdf3SVladimir Sementsov-Ogievskiy NULL) < 0) { 61886f8cdf3SVladimir Sementsov-Ogievskiy rc = -EIO; 61986f8cdf3SVladimir Sementsov-Ogievskiy } 62086f8cdf3SVladimir Sementsov-Ogievskiy } else if (rc >= 0) { 62186f8cdf3SVladimir Sementsov-Ogievskiy rc = -EIO; 62286f8cdf3SVladimir Sementsov-Ogievskiy } 62386f8cdf3SVladimir Sementsov-Ogievskiy qio_channel_set_cork(s->ioc, false); 62486f8cdf3SVladimir Sementsov-Ogievskiy } else { 62586f8cdf3SVladimir Sementsov-Ogievskiy rc = nbd_send_request(s->ioc, request); 62686f8cdf3SVladimir Sementsov-Ogievskiy } 62786f8cdf3SVladimir Sementsov-Ogievskiy 62886f8cdf3SVladimir Sementsov-Ogievskiy err: 62986f8cdf3SVladimir Sementsov-Ogievskiy if (rc < 0) { 630a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, rc); 631a34b1e5eSVladimir Sementsov-Ogievskiy if (i != -1) { 63286f8cdf3SVladimir Sementsov-Ogievskiy s->requests[i].coroutine = NULL; 63386f8cdf3SVladimir Sementsov-Ogievskiy s->in_flight--; 634a34b1e5eSVladimir Sementsov-Ogievskiy } 635f7651539SVladimir Sementsov-Ogievskiy if (s->in_flight == 0 && s->wait_in_flight) { 636f7651539SVladimir Sementsov-Ogievskiy aio_co_wake(s->connection_co); 637f7651539SVladimir Sementsov-Ogievskiy } else { 63886f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_queue_next(&s->free_sema); 63986f8cdf3SVladimir Sementsov-Ogievskiy } 640f7651539SVladimir Sementsov-Ogievskiy } 64186f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_mutex_unlock(&s->send_mutex); 64286f8cdf3SVladimir Sementsov-Ogievskiy return rc; 64386f8cdf3SVladimir Sementsov-Ogievskiy } 64486f8cdf3SVladimir Sementsov-Ogievskiy 64586f8cdf3SVladimir Sementsov-Ogievskiy static inline uint16_t payload_advance16(uint8_t **payload) 64686f8cdf3SVladimir Sementsov-Ogievskiy { 64786f8cdf3SVladimir Sementsov-Ogievskiy *payload += 2; 64886f8cdf3SVladimir Sementsov-Ogievskiy return lduw_be_p(*payload - 2); 64986f8cdf3SVladimir Sementsov-Ogievskiy } 65086f8cdf3SVladimir Sementsov-Ogievskiy 65186f8cdf3SVladimir Sementsov-Ogievskiy static inline uint32_t payload_advance32(uint8_t **payload) 65286f8cdf3SVladimir Sementsov-Ogievskiy { 65386f8cdf3SVladimir Sementsov-Ogievskiy *payload += 4; 65486f8cdf3SVladimir Sementsov-Ogievskiy return ldl_be_p(*payload - 4); 65586f8cdf3SVladimir Sementsov-Ogievskiy } 65686f8cdf3SVladimir Sementsov-Ogievskiy 65786f8cdf3SVladimir Sementsov-Ogievskiy static inline uint64_t payload_advance64(uint8_t **payload) 65886f8cdf3SVladimir Sementsov-Ogievskiy { 65986f8cdf3SVladimir Sementsov-Ogievskiy *payload += 8; 66086f8cdf3SVladimir Sementsov-Ogievskiy return ldq_be_p(*payload - 8); 66186f8cdf3SVladimir Sementsov-Ogievskiy } 66286f8cdf3SVladimir Sementsov-Ogievskiy 663611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_parse_offset_hole_payload(BDRVNBDState *s, 66486f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk, 66586f8cdf3SVladimir Sementsov-Ogievskiy uint8_t *payload, uint64_t orig_offset, 66686f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector *qiov, Error **errp) 66786f8cdf3SVladimir Sementsov-Ogievskiy { 66886f8cdf3SVladimir Sementsov-Ogievskiy uint64_t offset; 66986f8cdf3SVladimir Sementsov-Ogievskiy uint32_t hole_size; 67086f8cdf3SVladimir Sementsov-Ogievskiy 67186f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length != sizeof(offset) + sizeof(hole_size)) { 67286f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: invalid payload for " 67386f8cdf3SVladimir Sementsov-Ogievskiy "NBD_REPLY_TYPE_OFFSET_HOLE"); 67486f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 67586f8cdf3SVladimir Sementsov-Ogievskiy } 67686f8cdf3SVladimir Sementsov-Ogievskiy 67786f8cdf3SVladimir Sementsov-Ogievskiy offset = payload_advance64(&payload); 67886f8cdf3SVladimir Sementsov-Ogievskiy hole_size = payload_advance32(&payload); 67986f8cdf3SVladimir Sementsov-Ogievskiy 68086f8cdf3SVladimir Sementsov-Ogievskiy if (!hole_size || offset < orig_offset || hole_size > qiov->size || 68186f8cdf3SVladimir Sementsov-Ogievskiy offset > orig_offset + qiov->size - hole_size) { 68286f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent chunk exceeding requested" 68386f8cdf3SVladimir Sementsov-Ogievskiy " region"); 68486f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 68586f8cdf3SVladimir Sementsov-Ogievskiy } 686611ae1d7SVladimir Sementsov-Ogievskiy if (s->info.min_block && 687611ae1d7SVladimir Sementsov-Ogievskiy !QEMU_IS_ALIGNED(hole_size, s->info.min_block)) { 68886f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_structured_read_compliance("hole"); 68986f8cdf3SVladimir Sementsov-Ogievskiy } 69086f8cdf3SVladimir Sementsov-Ogievskiy 69186f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_memset(qiov, offset - orig_offset, 0, hole_size); 69286f8cdf3SVladimir Sementsov-Ogievskiy 69386f8cdf3SVladimir Sementsov-Ogievskiy return 0; 69486f8cdf3SVladimir Sementsov-Ogievskiy } 69586f8cdf3SVladimir Sementsov-Ogievskiy 69686f8cdf3SVladimir Sementsov-Ogievskiy /* 69786f8cdf3SVladimir Sementsov-Ogievskiy * nbd_parse_blockstatus_payload 69886f8cdf3SVladimir Sementsov-Ogievskiy * Based on our request, we expect only one extent in reply, for the 69986f8cdf3SVladimir Sementsov-Ogievskiy * base:allocation context. 70086f8cdf3SVladimir Sementsov-Ogievskiy */ 701611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_parse_blockstatus_payload(BDRVNBDState *s, 70286f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk, 70386f8cdf3SVladimir Sementsov-Ogievskiy uint8_t *payload, uint64_t orig_length, 70486f8cdf3SVladimir Sementsov-Ogievskiy NBDExtent *extent, Error **errp) 70586f8cdf3SVladimir Sementsov-Ogievskiy { 70686f8cdf3SVladimir Sementsov-Ogievskiy uint32_t context_id; 70786f8cdf3SVladimir Sementsov-Ogievskiy 70886f8cdf3SVladimir Sementsov-Ogievskiy /* The server succeeded, so it must have sent [at least] one extent */ 70986f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length < sizeof(context_id) + sizeof(*extent)) { 71086f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: invalid payload for " 71186f8cdf3SVladimir Sementsov-Ogievskiy "NBD_REPLY_TYPE_BLOCK_STATUS"); 71286f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 71386f8cdf3SVladimir Sementsov-Ogievskiy } 71486f8cdf3SVladimir Sementsov-Ogievskiy 71586f8cdf3SVladimir Sementsov-Ogievskiy context_id = payload_advance32(&payload); 716611ae1d7SVladimir Sementsov-Ogievskiy if (s->info.context_id != context_id) { 71786f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: unexpected context id %d for " 71886f8cdf3SVladimir Sementsov-Ogievskiy "NBD_REPLY_TYPE_BLOCK_STATUS, when negotiated context " 71986f8cdf3SVladimir Sementsov-Ogievskiy "id is %d", context_id, 720611ae1d7SVladimir Sementsov-Ogievskiy s->info.context_id); 72186f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 72286f8cdf3SVladimir Sementsov-Ogievskiy } 72386f8cdf3SVladimir Sementsov-Ogievskiy 72486f8cdf3SVladimir Sementsov-Ogievskiy extent->length = payload_advance32(&payload); 72586f8cdf3SVladimir Sementsov-Ogievskiy extent->flags = payload_advance32(&payload); 72686f8cdf3SVladimir Sementsov-Ogievskiy 72786f8cdf3SVladimir Sementsov-Ogievskiy if (extent->length == 0) { 72886f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent status chunk with " 72986f8cdf3SVladimir Sementsov-Ogievskiy "zero length"); 73086f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 73186f8cdf3SVladimir Sementsov-Ogievskiy } 73286f8cdf3SVladimir Sementsov-Ogievskiy 73386f8cdf3SVladimir Sementsov-Ogievskiy /* 73486f8cdf3SVladimir Sementsov-Ogievskiy * A server sending unaligned block status is in violation of the 73586f8cdf3SVladimir Sementsov-Ogievskiy * protocol, but as qemu-nbd 3.1 is such a server (at least for 73686f8cdf3SVladimir Sementsov-Ogievskiy * POSIX files that are not a multiple of 512 bytes, since qemu 73786f8cdf3SVladimir Sementsov-Ogievskiy * rounds files up to 512-byte multiples but lseek(SEEK_HOLE) 73886f8cdf3SVladimir Sementsov-Ogievskiy * still sees an implicit hole beyond the real EOF), it's nicer to 73986f8cdf3SVladimir Sementsov-Ogievskiy * work around the misbehaving server. If the request included 74086f8cdf3SVladimir Sementsov-Ogievskiy * more than the final unaligned block, truncate it back to an 74186f8cdf3SVladimir Sementsov-Ogievskiy * aligned result; if the request was only the final block, round 74286f8cdf3SVladimir Sementsov-Ogievskiy * up to the full block and change the status to fully-allocated 74386f8cdf3SVladimir Sementsov-Ogievskiy * (always a safe status, even if it loses information). 74486f8cdf3SVladimir Sementsov-Ogievskiy */ 745611ae1d7SVladimir Sementsov-Ogievskiy if (s->info.min_block && !QEMU_IS_ALIGNED(extent->length, 746611ae1d7SVladimir Sementsov-Ogievskiy s->info.min_block)) { 74786f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_parse_blockstatus_compliance("extent length is unaligned"); 748611ae1d7SVladimir Sementsov-Ogievskiy if (extent->length > s->info.min_block) { 74986f8cdf3SVladimir Sementsov-Ogievskiy extent->length = QEMU_ALIGN_DOWN(extent->length, 750611ae1d7SVladimir Sementsov-Ogievskiy s->info.min_block); 75186f8cdf3SVladimir Sementsov-Ogievskiy } else { 752611ae1d7SVladimir Sementsov-Ogievskiy extent->length = s->info.min_block; 75386f8cdf3SVladimir Sementsov-Ogievskiy extent->flags = 0; 75486f8cdf3SVladimir Sementsov-Ogievskiy } 75586f8cdf3SVladimir Sementsov-Ogievskiy } 75686f8cdf3SVladimir Sementsov-Ogievskiy 75786f8cdf3SVladimir Sementsov-Ogievskiy /* 75886f8cdf3SVladimir Sementsov-Ogievskiy * We used NBD_CMD_FLAG_REQ_ONE, so the server should not have 75986f8cdf3SVladimir Sementsov-Ogievskiy * sent us any more than one extent, nor should it have included 76086f8cdf3SVladimir Sementsov-Ogievskiy * status beyond our request in that extent. However, it's easy 76186f8cdf3SVladimir Sementsov-Ogievskiy * enough to ignore the server's noncompliance without killing the 76286f8cdf3SVladimir Sementsov-Ogievskiy * connection; just ignore trailing extents, and clamp things to 76386f8cdf3SVladimir Sementsov-Ogievskiy * the length of our request. 76486f8cdf3SVladimir Sementsov-Ogievskiy */ 76586f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length > sizeof(context_id) + sizeof(*extent)) { 76686f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_parse_blockstatus_compliance("more than one extent"); 76786f8cdf3SVladimir Sementsov-Ogievskiy } 76886f8cdf3SVladimir Sementsov-Ogievskiy if (extent->length > orig_length) { 76986f8cdf3SVladimir Sementsov-Ogievskiy extent->length = orig_length; 77086f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_parse_blockstatus_compliance("extent length too large"); 77186f8cdf3SVladimir Sementsov-Ogievskiy } 77286f8cdf3SVladimir Sementsov-Ogievskiy 773dbc7b014SEric Blake /* 774dbc7b014SEric Blake * HACK: if we are using x-dirty-bitmaps to access 775dbc7b014SEric Blake * qemu:allocation-depth, treat all depths > 2 the same as 2, 776dbc7b014SEric Blake * since nbd_client_co_block_status is only expecting the low two 777dbc7b014SEric Blake * bits to be set. 778dbc7b014SEric Blake */ 779dbc7b014SEric Blake if (s->alloc_depth && extent->flags > 2) { 780dbc7b014SEric Blake extent->flags = 2; 781dbc7b014SEric Blake } 782dbc7b014SEric Blake 78386f8cdf3SVladimir Sementsov-Ogievskiy return 0; 78486f8cdf3SVladimir Sementsov-Ogievskiy } 78586f8cdf3SVladimir Sementsov-Ogievskiy 78686f8cdf3SVladimir Sementsov-Ogievskiy /* 78786f8cdf3SVladimir Sementsov-Ogievskiy * nbd_parse_error_payload 78886f8cdf3SVladimir Sementsov-Ogievskiy * on success @errp contains message describing nbd error reply 78986f8cdf3SVladimir Sementsov-Ogievskiy */ 79086f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_parse_error_payload(NBDStructuredReplyChunk *chunk, 79186f8cdf3SVladimir Sementsov-Ogievskiy uint8_t *payload, int *request_ret, 79286f8cdf3SVladimir Sementsov-Ogievskiy Error **errp) 79386f8cdf3SVladimir Sementsov-Ogievskiy { 79486f8cdf3SVladimir Sementsov-Ogievskiy uint32_t error; 79586f8cdf3SVladimir Sementsov-Ogievskiy uint16_t message_size; 79686f8cdf3SVladimir Sementsov-Ogievskiy 79786f8cdf3SVladimir Sementsov-Ogievskiy assert(chunk->type & (1 << 15)); 79886f8cdf3SVladimir Sementsov-Ogievskiy 79986f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length < sizeof(error) + sizeof(message_size)) { 80086f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, 80186f8cdf3SVladimir Sementsov-Ogievskiy "Protocol error: invalid payload for structured error"); 80286f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 80386f8cdf3SVladimir Sementsov-Ogievskiy } 80486f8cdf3SVladimir Sementsov-Ogievskiy 80586f8cdf3SVladimir Sementsov-Ogievskiy error = nbd_errno_to_system_errno(payload_advance32(&payload)); 80686f8cdf3SVladimir Sementsov-Ogievskiy if (error == 0) { 80786f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent structured error chunk " 80886f8cdf3SVladimir Sementsov-Ogievskiy "with error = 0"); 80986f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 81086f8cdf3SVladimir Sementsov-Ogievskiy } 81186f8cdf3SVladimir Sementsov-Ogievskiy 81286f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = -error; 81386f8cdf3SVladimir Sementsov-Ogievskiy message_size = payload_advance16(&payload); 81486f8cdf3SVladimir Sementsov-Ogievskiy 81586f8cdf3SVladimir Sementsov-Ogievskiy if (message_size > chunk->length - sizeof(error) - sizeof(message_size)) { 81686f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent structured error chunk " 81786f8cdf3SVladimir Sementsov-Ogievskiy "with incorrect message size"); 81886f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 81986f8cdf3SVladimir Sementsov-Ogievskiy } 82086f8cdf3SVladimir Sementsov-Ogievskiy 82186f8cdf3SVladimir Sementsov-Ogievskiy /* TODO: Add a trace point to mention the server complaint */ 82286f8cdf3SVladimir Sementsov-Ogievskiy 82386f8cdf3SVladimir Sementsov-Ogievskiy /* TODO handle ERROR_OFFSET */ 82486f8cdf3SVladimir Sementsov-Ogievskiy 82586f8cdf3SVladimir Sementsov-Ogievskiy return 0; 82686f8cdf3SVladimir Sementsov-Ogievskiy } 82786f8cdf3SVladimir Sementsov-Ogievskiy 828611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_co_receive_offset_data_payload(BDRVNBDState *s, 82986f8cdf3SVladimir Sementsov-Ogievskiy uint64_t orig_offset, 83086f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector *qiov, Error **errp) 83186f8cdf3SVladimir Sementsov-Ogievskiy { 83286f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector sub_qiov; 83386f8cdf3SVladimir Sementsov-Ogievskiy uint64_t offset; 83486f8cdf3SVladimir Sementsov-Ogievskiy size_t data_size; 83586f8cdf3SVladimir Sementsov-Ogievskiy int ret; 83686f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk = &s->reply.structured; 83786f8cdf3SVladimir Sementsov-Ogievskiy 83886f8cdf3SVladimir Sementsov-Ogievskiy assert(nbd_reply_is_structured(&s->reply)); 83986f8cdf3SVladimir Sementsov-Ogievskiy 84086f8cdf3SVladimir Sementsov-Ogievskiy /* The NBD spec requires at least one byte of payload */ 84186f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length <= sizeof(offset)) { 84286f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: invalid payload for " 84386f8cdf3SVladimir Sementsov-Ogievskiy "NBD_REPLY_TYPE_OFFSET_DATA"); 84486f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 84586f8cdf3SVladimir Sementsov-Ogievskiy } 84686f8cdf3SVladimir Sementsov-Ogievskiy 84786f8cdf3SVladimir Sementsov-Ogievskiy if (nbd_read64(s->ioc, &offset, "OFFSET_DATA offset", errp) < 0) { 84886f8cdf3SVladimir Sementsov-Ogievskiy return -EIO; 84986f8cdf3SVladimir Sementsov-Ogievskiy } 85086f8cdf3SVladimir Sementsov-Ogievskiy 85186f8cdf3SVladimir Sementsov-Ogievskiy data_size = chunk->length - sizeof(offset); 85286f8cdf3SVladimir Sementsov-Ogievskiy assert(data_size); 85386f8cdf3SVladimir Sementsov-Ogievskiy if (offset < orig_offset || data_size > qiov->size || 85486f8cdf3SVladimir Sementsov-Ogievskiy offset > orig_offset + qiov->size - data_size) { 85586f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: server sent chunk exceeding requested" 85686f8cdf3SVladimir Sementsov-Ogievskiy " region"); 85786f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 85886f8cdf3SVladimir Sementsov-Ogievskiy } 85986f8cdf3SVladimir Sementsov-Ogievskiy if (s->info.min_block && !QEMU_IS_ALIGNED(data_size, s->info.min_block)) { 86086f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_structured_read_compliance("data"); 86186f8cdf3SVladimir Sementsov-Ogievskiy } 86286f8cdf3SVladimir Sementsov-Ogievskiy 86386f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_init(&sub_qiov, qiov->niov); 86486f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_concat(&sub_qiov, qiov, offset - orig_offset, data_size); 86586f8cdf3SVladimir Sementsov-Ogievskiy ret = qio_channel_readv_all(s->ioc, sub_qiov.iov, sub_qiov.niov, errp); 86686f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_destroy(&sub_qiov); 86786f8cdf3SVladimir Sementsov-Ogievskiy 86886f8cdf3SVladimir Sementsov-Ogievskiy return ret < 0 ? -EIO : 0; 86986f8cdf3SVladimir Sementsov-Ogievskiy } 87086f8cdf3SVladimir Sementsov-Ogievskiy 87186f8cdf3SVladimir Sementsov-Ogievskiy #define NBD_MAX_MALLOC_PAYLOAD 1000 87286f8cdf3SVladimir Sementsov-Ogievskiy static coroutine_fn int nbd_co_receive_structured_payload( 873611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s, void **payload, Error **errp) 87486f8cdf3SVladimir Sementsov-Ogievskiy { 87586f8cdf3SVladimir Sementsov-Ogievskiy int ret; 87686f8cdf3SVladimir Sementsov-Ogievskiy uint32_t len; 87786f8cdf3SVladimir Sementsov-Ogievskiy 87886f8cdf3SVladimir Sementsov-Ogievskiy assert(nbd_reply_is_structured(&s->reply)); 87986f8cdf3SVladimir Sementsov-Ogievskiy 88086f8cdf3SVladimir Sementsov-Ogievskiy len = s->reply.structured.length; 88186f8cdf3SVladimir Sementsov-Ogievskiy 88286f8cdf3SVladimir Sementsov-Ogievskiy if (len == 0) { 88386f8cdf3SVladimir Sementsov-Ogievskiy return 0; 88486f8cdf3SVladimir Sementsov-Ogievskiy } 88586f8cdf3SVladimir Sementsov-Ogievskiy 88686f8cdf3SVladimir Sementsov-Ogievskiy if (payload == NULL) { 88786f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Unexpected structured payload"); 88886f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 88986f8cdf3SVladimir Sementsov-Ogievskiy } 89086f8cdf3SVladimir Sementsov-Ogievskiy 89186f8cdf3SVladimir Sementsov-Ogievskiy if (len > NBD_MAX_MALLOC_PAYLOAD) { 89286f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Payload too large"); 89386f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 89486f8cdf3SVladimir Sementsov-Ogievskiy } 89586f8cdf3SVladimir Sementsov-Ogievskiy 89686f8cdf3SVladimir Sementsov-Ogievskiy *payload = g_new(char, len); 89786f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_read(s->ioc, *payload, len, "structured payload", errp); 89886f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 89986f8cdf3SVladimir Sementsov-Ogievskiy g_free(*payload); 90086f8cdf3SVladimir Sementsov-Ogievskiy *payload = NULL; 90186f8cdf3SVladimir Sementsov-Ogievskiy return ret; 90286f8cdf3SVladimir Sementsov-Ogievskiy } 90386f8cdf3SVladimir Sementsov-Ogievskiy 90486f8cdf3SVladimir Sementsov-Ogievskiy return 0; 90586f8cdf3SVladimir Sementsov-Ogievskiy } 90686f8cdf3SVladimir Sementsov-Ogievskiy 90786f8cdf3SVladimir Sementsov-Ogievskiy /* 90886f8cdf3SVladimir Sementsov-Ogievskiy * nbd_co_do_receive_one_chunk 90986f8cdf3SVladimir Sementsov-Ogievskiy * for simple reply: 91086f8cdf3SVladimir Sementsov-Ogievskiy * set request_ret to received reply error 91186f8cdf3SVladimir Sementsov-Ogievskiy * if qiov is not NULL: read payload to @qiov 91286f8cdf3SVladimir Sementsov-Ogievskiy * for structured reply chunk: 91386f8cdf3SVladimir Sementsov-Ogievskiy * if error chunk: read payload, set @request_ret, do not set @payload 91486f8cdf3SVladimir Sementsov-Ogievskiy * else if offset_data chunk: read payload data to @qiov, do not set @payload 91586f8cdf3SVladimir Sementsov-Ogievskiy * else: read payload to @payload 91686f8cdf3SVladimir Sementsov-Ogievskiy * 91786f8cdf3SVladimir Sementsov-Ogievskiy * If function fails, @errp contains corresponding error message, and the 91886f8cdf3SVladimir Sementsov-Ogievskiy * connection with the server is suspect. If it returns 0, then the 91986f8cdf3SVladimir Sementsov-Ogievskiy * transaction succeeded (although @request_ret may be a negative errno 92086f8cdf3SVladimir Sementsov-Ogievskiy * corresponding to the server's error reply), and errp is unchanged. 92186f8cdf3SVladimir Sementsov-Ogievskiy */ 92286f8cdf3SVladimir Sementsov-Ogievskiy static coroutine_fn int nbd_co_do_receive_one_chunk( 923611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s, uint64_t handle, bool only_structured, 92486f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, QEMUIOVector *qiov, void **payload, Error **errp) 92586f8cdf3SVladimir Sementsov-Ogievskiy { 92686f8cdf3SVladimir Sementsov-Ogievskiy int ret; 92786f8cdf3SVladimir Sementsov-Ogievskiy int i = HANDLE_TO_INDEX(s, handle); 92886f8cdf3SVladimir Sementsov-Ogievskiy void *local_payload = NULL; 92986f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk; 93086f8cdf3SVladimir Sementsov-Ogievskiy 93186f8cdf3SVladimir Sementsov-Ogievskiy if (payload) { 93286f8cdf3SVladimir Sementsov-Ogievskiy *payload = NULL; 93386f8cdf3SVladimir Sementsov-Ogievskiy } 93486f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = 0; 93586f8cdf3SVladimir Sementsov-Ogievskiy 93686f8cdf3SVladimir Sementsov-Ogievskiy /* Wait until we're woken up by nbd_connection_entry. */ 93786f8cdf3SVladimir Sementsov-Ogievskiy s->requests[i].receiving = true; 93886f8cdf3SVladimir Sementsov-Ogievskiy qemu_coroutine_yield(); 939bbfb7c2fSVladimir Sementsov-Ogievskiy assert(!s->requests[i].receiving); 94091e0998fSVladimir Sementsov-Ogievskiy if (!nbd_client_connected(s)) { 94186f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Connection closed"); 94286f8cdf3SVladimir Sementsov-Ogievskiy return -EIO; 94386f8cdf3SVladimir Sementsov-Ogievskiy } 94486f8cdf3SVladimir Sementsov-Ogievskiy assert(s->ioc); 94586f8cdf3SVladimir Sementsov-Ogievskiy 94686f8cdf3SVladimir Sementsov-Ogievskiy assert(s->reply.handle == handle); 94786f8cdf3SVladimir Sementsov-Ogievskiy 94886f8cdf3SVladimir Sementsov-Ogievskiy if (nbd_reply_is_simple(&s->reply)) { 94986f8cdf3SVladimir Sementsov-Ogievskiy if (only_structured) { 95086f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: simple reply when structured " 95186f8cdf3SVladimir Sementsov-Ogievskiy "reply chunk was expected"); 95286f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 95386f8cdf3SVladimir Sementsov-Ogievskiy } 95486f8cdf3SVladimir Sementsov-Ogievskiy 95586f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = -nbd_errno_to_system_errno(s->reply.simple.error); 95686f8cdf3SVladimir Sementsov-Ogievskiy if (*request_ret < 0 || !qiov) { 95786f8cdf3SVladimir Sementsov-Ogievskiy return 0; 95886f8cdf3SVladimir Sementsov-Ogievskiy } 95986f8cdf3SVladimir Sementsov-Ogievskiy 96086f8cdf3SVladimir Sementsov-Ogievskiy return qio_channel_readv_all(s->ioc, qiov->iov, qiov->niov, 96186f8cdf3SVladimir Sementsov-Ogievskiy errp) < 0 ? -EIO : 0; 96286f8cdf3SVladimir Sementsov-Ogievskiy } 96386f8cdf3SVladimir Sementsov-Ogievskiy 96486f8cdf3SVladimir Sementsov-Ogievskiy /* handle structured reply chunk */ 96586f8cdf3SVladimir Sementsov-Ogievskiy assert(s->info.structured_reply); 96686f8cdf3SVladimir Sementsov-Ogievskiy chunk = &s->reply.structured; 96786f8cdf3SVladimir Sementsov-Ogievskiy 96886f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->type == NBD_REPLY_TYPE_NONE) { 96986f8cdf3SVladimir Sementsov-Ogievskiy if (!(chunk->flags & NBD_REPLY_FLAG_DONE)) { 97086f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk without" 97186f8cdf3SVladimir Sementsov-Ogievskiy " NBD_REPLY_FLAG_DONE flag set"); 97286f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 97386f8cdf3SVladimir Sementsov-Ogievskiy } 97486f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->length) { 97586f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk with" 97686f8cdf3SVladimir Sementsov-Ogievskiy " nonzero length"); 97786f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 97886f8cdf3SVladimir Sementsov-Ogievskiy } 97986f8cdf3SVladimir Sementsov-Ogievskiy return 0; 98086f8cdf3SVladimir Sementsov-Ogievskiy } 98186f8cdf3SVladimir Sementsov-Ogievskiy 98286f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->type == NBD_REPLY_TYPE_OFFSET_DATA) { 98386f8cdf3SVladimir Sementsov-Ogievskiy if (!qiov) { 98486f8cdf3SVladimir Sementsov-Ogievskiy error_setg(errp, "Unexpected NBD_REPLY_TYPE_OFFSET_DATA chunk"); 98586f8cdf3SVladimir Sementsov-Ogievskiy return -EINVAL; 98686f8cdf3SVladimir Sementsov-Ogievskiy } 98786f8cdf3SVladimir Sementsov-Ogievskiy 98886f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_receive_offset_data_payload(s, s->requests[i].offset, 98986f8cdf3SVladimir Sementsov-Ogievskiy qiov, errp); 99086f8cdf3SVladimir Sementsov-Ogievskiy } 99186f8cdf3SVladimir Sementsov-Ogievskiy 99286f8cdf3SVladimir Sementsov-Ogievskiy if (nbd_reply_type_is_error(chunk->type)) { 99386f8cdf3SVladimir Sementsov-Ogievskiy payload = &local_payload; 99486f8cdf3SVladimir Sementsov-Ogievskiy } 99586f8cdf3SVladimir Sementsov-Ogievskiy 99686f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_co_receive_structured_payload(s, payload, errp); 99786f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 99886f8cdf3SVladimir Sementsov-Ogievskiy return ret; 99986f8cdf3SVladimir Sementsov-Ogievskiy } 100086f8cdf3SVladimir Sementsov-Ogievskiy 100186f8cdf3SVladimir Sementsov-Ogievskiy if (nbd_reply_type_is_error(chunk->type)) { 100286f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_parse_error_payload(chunk, local_payload, request_ret, errp); 100386f8cdf3SVladimir Sementsov-Ogievskiy g_free(local_payload); 100486f8cdf3SVladimir Sementsov-Ogievskiy return ret; 100586f8cdf3SVladimir Sementsov-Ogievskiy } 100686f8cdf3SVladimir Sementsov-Ogievskiy 100786f8cdf3SVladimir Sementsov-Ogievskiy return 0; 100886f8cdf3SVladimir Sementsov-Ogievskiy } 100986f8cdf3SVladimir Sementsov-Ogievskiy 101086f8cdf3SVladimir Sementsov-Ogievskiy /* 101186f8cdf3SVladimir Sementsov-Ogievskiy * nbd_co_receive_one_chunk 101286f8cdf3SVladimir Sementsov-Ogievskiy * Read reply, wake up connection_co and set s->quit if needed. 101386f8cdf3SVladimir Sementsov-Ogievskiy * Return value is a fatal error code or normal nbd reply error code 101486f8cdf3SVladimir Sementsov-Ogievskiy */ 101586f8cdf3SVladimir Sementsov-Ogievskiy static coroutine_fn int nbd_co_receive_one_chunk( 1016611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s, uint64_t handle, bool only_structured, 101786f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, QEMUIOVector *qiov, NBDReply *reply, void **payload, 101886f8cdf3SVladimir Sementsov-Ogievskiy Error **errp) 101986f8cdf3SVladimir Sementsov-Ogievskiy { 102086f8cdf3SVladimir Sementsov-Ogievskiy int ret = nbd_co_do_receive_one_chunk(s, handle, only_structured, 102186f8cdf3SVladimir Sementsov-Ogievskiy request_ret, qiov, payload, errp); 102286f8cdf3SVladimir Sementsov-Ogievskiy 102386f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 10245cf42b1cSEric Blake memset(reply, 0, sizeof(*reply)); 1025a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, ret); 102686f8cdf3SVladimir Sementsov-Ogievskiy } else { 102786f8cdf3SVladimir Sementsov-Ogievskiy /* For assert at loop start in nbd_connection_entry */ 102886f8cdf3SVladimir Sementsov-Ogievskiy *reply = s->reply; 102986f8cdf3SVladimir Sementsov-Ogievskiy } 1030f7651539SVladimir Sementsov-Ogievskiy s->reply.handle = 0; 103186f8cdf3SVladimir Sementsov-Ogievskiy 1032f7651539SVladimir Sementsov-Ogievskiy if (s->connection_co && !s->wait_in_flight) { 1033f7651539SVladimir Sementsov-Ogievskiy /* 1034f7651539SVladimir Sementsov-Ogievskiy * We must check s->wait_in_flight, because we may entered by 1035f7651539SVladimir Sementsov-Ogievskiy * nbd_recv_coroutines_wake_all(), in this case we should not 1036f7651539SVladimir Sementsov-Ogievskiy * wake connection_co here, it will woken by last request. 1037f7651539SVladimir Sementsov-Ogievskiy */ 103886f8cdf3SVladimir Sementsov-Ogievskiy aio_co_wake(s->connection_co); 103986f8cdf3SVladimir Sementsov-Ogievskiy } 104086f8cdf3SVladimir Sementsov-Ogievskiy 104186f8cdf3SVladimir Sementsov-Ogievskiy return ret; 104286f8cdf3SVladimir Sementsov-Ogievskiy } 104386f8cdf3SVladimir Sementsov-Ogievskiy 104486f8cdf3SVladimir Sementsov-Ogievskiy typedef struct NBDReplyChunkIter { 104586f8cdf3SVladimir Sementsov-Ogievskiy int ret; 104686f8cdf3SVladimir Sementsov-Ogievskiy int request_ret; 104786f8cdf3SVladimir Sementsov-Ogievskiy Error *err; 104886f8cdf3SVladimir Sementsov-Ogievskiy bool done, only_structured; 104986f8cdf3SVladimir Sementsov-Ogievskiy } NBDReplyChunkIter; 105086f8cdf3SVladimir Sementsov-Ogievskiy 105186f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_iter_channel_error(NBDReplyChunkIter *iter, 105286f8cdf3SVladimir Sementsov-Ogievskiy int ret, Error **local_err) 105386f8cdf3SVladimir Sementsov-Ogievskiy { 1054d9366135SVladimir Sementsov-Ogievskiy assert(local_err && *local_err); 105586f8cdf3SVladimir Sementsov-Ogievskiy assert(ret < 0); 105686f8cdf3SVladimir Sementsov-Ogievskiy 105786f8cdf3SVladimir Sementsov-Ogievskiy if (!iter->ret) { 105886f8cdf3SVladimir Sementsov-Ogievskiy iter->ret = ret; 105986f8cdf3SVladimir Sementsov-Ogievskiy error_propagate(&iter->err, *local_err); 106086f8cdf3SVladimir Sementsov-Ogievskiy } else { 106186f8cdf3SVladimir Sementsov-Ogievskiy error_free(*local_err); 106286f8cdf3SVladimir Sementsov-Ogievskiy } 106386f8cdf3SVladimir Sementsov-Ogievskiy 106486f8cdf3SVladimir Sementsov-Ogievskiy *local_err = NULL; 106586f8cdf3SVladimir Sementsov-Ogievskiy } 106686f8cdf3SVladimir Sementsov-Ogievskiy 106786f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_iter_request_error(NBDReplyChunkIter *iter, int ret) 106886f8cdf3SVladimir Sementsov-Ogievskiy { 106986f8cdf3SVladimir Sementsov-Ogievskiy assert(ret < 0); 107086f8cdf3SVladimir Sementsov-Ogievskiy 107186f8cdf3SVladimir Sementsov-Ogievskiy if (!iter->request_ret) { 107286f8cdf3SVladimir Sementsov-Ogievskiy iter->request_ret = ret; 107386f8cdf3SVladimir Sementsov-Ogievskiy } 107486f8cdf3SVladimir Sementsov-Ogievskiy } 107586f8cdf3SVladimir Sementsov-Ogievskiy 107686f8cdf3SVladimir Sementsov-Ogievskiy /* 107786f8cdf3SVladimir Sementsov-Ogievskiy * NBD_FOREACH_REPLY_CHUNK 107886f8cdf3SVladimir Sementsov-Ogievskiy * The pointer stored in @payload requires g_free() to free it. 107986f8cdf3SVladimir Sementsov-Ogievskiy */ 108086f8cdf3SVladimir Sementsov-Ogievskiy #define NBD_FOREACH_REPLY_CHUNK(s, iter, handle, structured, \ 108186f8cdf3SVladimir Sementsov-Ogievskiy qiov, reply, payload) \ 108286f8cdf3SVladimir Sementsov-Ogievskiy for (iter = (NBDReplyChunkIter) { .only_structured = structured }; \ 108386f8cdf3SVladimir Sementsov-Ogievskiy nbd_reply_chunk_iter_receive(s, &iter, handle, qiov, reply, payload);) 108486f8cdf3SVladimir Sementsov-Ogievskiy 108586f8cdf3SVladimir Sementsov-Ogievskiy /* 108686f8cdf3SVladimir Sementsov-Ogievskiy * nbd_reply_chunk_iter_receive 108786f8cdf3SVladimir Sementsov-Ogievskiy * The pointer stored in @payload requires g_free() to free it. 108886f8cdf3SVladimir Sementsov-Ogievskiy */ 1089611ae1d7SVladimir Sementsov-Ogievskiy static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s, 109086f8cdf3SVladimir Sementsov-Ogievskiy NBDReplyChunkIter *iter, 109186f8cdf3SVladimir Sementsov-Ogievskiy uint64_t handle, 109286f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector *qiov, NBDReply *reply, 109386f8cdf3SVladimir Sementsov-Ogievskiy void **payload) 109486f8cdf3SVladimir Sementsov-Ogievskiy { 109586f8cdf3SVladimir Sementsov-Ogievskiy int ret, request_ret; 109686f8cdf3SVladimir Sementsov-Ogievskiy NBDReply local_reply; 109786f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk; 109886f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 109991e0998fSVladimir Sementsov-Ogievskiy if (!nbd_client_connected(s)) { 110086f8cdf3SVladimir Sementsov-Ogievskiy error_setg(&local_err, "Connection closed"); 110186f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(iter, -EIO, &local_err); 110286f8cdf3SVladimir Sementsov-Ogievskiy goto break_loop; 110386f8cdf3SVladimir Sementsov-Ogievskiy } 110486f8cdf3SVladimir Sementsov-Ogievskiy 110586f8cdf3SVladimir Sementsov-Ogievskiy if (iter->done) { 110686f8cdf3SVladimir Sementsov-Ogievskiy /* Previous iteration was last. */ 110786f8cdf3SVladimir Sementsov-Ogievskiy goto break_loop; 110886f8cdf3SVladimir Sementsov-Ogievskiy } 110986f8cdf3SVladimir Sementsov-Ogievskiy 111086f8cdf3SVladimir Sementsov-Ogievskiy if (reply == NULL) { 111186f8cdf3SVladimir Sementsov-Ogievskiy reply = &local_reply; 111286f8cdf3SVladimir Sementsov-Ogievskiy } 111386f8cdf3SVladimir Sementsov-Ogievskiy 111486f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_co_receive_one_chunk(s, handle, iter->only_structured, 111586f8cdf3SVladimir Sementsov-Ogievskiy &request_ret, qiov, reply, payload, 111686f8cdf3SVladimir Sementsov-Ogievskiy &local_err); 111786f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 111886f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(iter, ret, &local_err); 111986f8cdf3SVladimir Sementsov-Ogievskiy } else if (request_ret < 0) { 112086f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_request_error(iter, request_ret); 112186f8cdf3SVladimir Sementsov-Ogievskiy } 112286f8cdf3SVladimir Sementsov-Ogievskiy 112386f8cdf3SVladimir Sementsov-Ogievskiy /* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */ 112491e0998fSVladimir Sementsov-Ogievskiy if (nbd_reply_is_simple(reply) || !nbd_client_connected(s)) { 112586f8cdf3SVladimir Sementsov-Ogievskiy goto break_loop; 112686f8cdf3SVladimir Sementsov-Ogievskiy } 112786f8cdf3SVladimir Sementsov-Ogievskiy 112886f8cdf3SVladimir Sementsov-Ogievskiy chunk = &reply->structured; 112986f8cdf3SVladimir Sementsov-Ogievskiy iter->only_structured = true; 113086f8cdf3SVladimir Sementsov-Ogievskiy 113186f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->type == NBD_REPLY_TYPE_NONE) { 113286f8cdf3SVladimir Sementsov-Ogievskiy /* NBD_REPLY_FLAG_DONE is already checked in nbd_co_receive_one_chunk */ 113386f8cdf3SVladimir Sementsov-Ogievskiy assert(chunk->flags & NBD_REPLY_FLAG_DONE); 113486f8cdf3SVladimir Sementsov-Ogievskiy goto break_loop; 113586f8cdf3SVladimir Sementsov-Ogievskiy } 113686f8cdf3SVladimir Sementsov-Ogievskiy 113786f8cdf3SVladimir Sementsov-Ogievskiy if (chunk->flags & NBD_REPLY_FLAG_DONE) { 113886f8cdf3SVladimir Sementsov-Ogievskiy /* This iteration is last. */ 113986f8cdf3SVladimir Sementsov-Ogievskiy iter->done = true; 114086f8cdf3SVladimir Sementsov-Ogievskiy } 114186f8cdf3SVladimir Sementsov-Ogievskiy 114286f8cdf3SVladimir Sementsov-Ogievskiy /* Execute the loop body */ 114386f8cdf3SVladimir Sementsov-Ogievskiy return true; 114486f8cdf3SVladimir Sementsov-Ogievskiy 114586f8cdf3SVladimir Sementsov-Ogievskiy break_loop: 114686f8cdf3SVladimir Sementsov-Ogievskiy s->requests[HANDLE_TO_INDEX(s, handle)].coroutine = NULL; 114786f8cdf3SVladimir Sementsov-Ogievskiy 114886f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_mutex_lock(&s->send_mutex); 114986f8cdf3SVladimir Sementsov-Ogievskiy s->in_flight--; 1150f7651539SVladimir Sementsov-Ogievskiy if (s->in_flight == 0 && s->wait_in_flight) { 1151f7651539SVladimir Sementsov-Ogievskiy aio_co_wake(s->connection_co); 1152f7651539SVladimir Sementsov-Ogievskiy } else { 115386f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_queue_next(&s->free_sema); 1154f7651539SVladimir Sementsov-Ogievskiy } 115586f8cdf3SVladimir Sementsov-Ogievskiy qemu_co_mutex_unlock(&s->send_mutex); 115686f8cdf3SVladimir Sementsov-Ogievskiy 115786f8cdf3SVladimir Sementsov-Ogievskiy return false; 115886f8cdf3SVladimir Sementsov-Ogievskiy } 115986f8cdf3SVladimir Sementsov-Ogievskiy 1160611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_co_receive_return_code(BDRVNBDState *s, uint64_t handle, 116186f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, Error **errp) 116286f8cdf3SVladimir Sementsov-Ogievskiy { 116386f8cdf3SVladimir Sementsov-Ogievskiy NBDReplyChunkIter iter; 116486f8cdf3SVladimir Sementsov-Ogievskiy 116586f8cdf3SVladimir Sementsov-Ogievskiy NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, NULL, NULL) { 116686f8cdf3SVladimir Sementsov-Ogievskiy /* nbd_reply_chunk_iter_receive does all the work */ 116786f8cdf3SVladimir Sementsov-Ogievskiy } 116886f8cdf3SVladimir Sementsov-Ogievskiy 116986f8cdf3SVladimir Sementsov-Ogievskiy error_propagate(errp, iter.err); 117086f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = iter.request_ret; 117186f8cdf3SVladimir Sementsov-Ogievskiy return iter.ret; 117286f8cdf3SVladimir Sementsov-Ogievskiy } 117386f8cdf3SVladimir Sementsov-Ogievskiy 1174611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_t handle, 117586f8cdf3SVladimir Sementsov-Ogievskiy uint64_t offset, QEMUIOVector *qiov, 117686f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, Error **errp) 117786f8cdf3SVladimir Sementsov-Ogievskiy { 117886f8cdf3SVladimir Sementsov-Ogievskiy NBDReplyChunkIter iter; 117986f8cdf3SVladimir Sementsov-Ogievskiy NBDReply reply; 118086f8cdf3SVladimir Sementsov-Ogievskiy void *payload = NULL; 118186f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 118286f8cdf3SVladimir Sementsov-Ogievskiy 118386f8cdf3SVladimir Sementsov-Ogievskiy NBD_FOREACH_REPLY_CHUNK(s, iter, handle, s->info.structured_reply, 118486f8cdf3SVladimir Sementsov-Ogievskiy qiov, &reply, &payload) 118586f8cdf3SVladimir Sementsov-Ogievskiy { 118686f8cdf3SVladimir Sementsov-Ogievskiy int ret; 118786f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk = &reply.structured; 118886f8cdf3SVladimir Sementsov-Ogievskiy 118986f8cdf3SVladimir Sementsov-Ogievskiy assert(nbd_reply_is_structured(&reply)); 119086f8cdf3SVladimir Sementsov-Ogievskiy 119186f8cdf3SVladimir Sementsov-Ogievskiy switch (chunk->type) { 119286f8cdf3SVladimir Sementsov-Ogievskiy case NBD_REPLY_TYPE_OFFSET_DATA: 119386f8cdf3SVladimir Sementsov-Ogievskiy /* 119486f8cdf3SVladimir Sementsov-Ogievskiy * special cased in nbd_co_receive_one_chunk, data is already 119586f8cdf3SVladimir Sementsov-Ogievskiy * in qiov 119686f8cdf3SVladimir Sementsov-Ogievskiy */ 119786f8cdf3SVladimir Sementsov-Ogievskiy break; 119886f8cdf3SVladimir Sementsov-Ogievskiy case NBD_REPLY_TYPE_OFFSET_HOLE: 119986f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_parse_offset_hole_payload(s, &reply.structured, payload, 120086f8cdf3SVladimir Sementsov-Ogievskiy offset, qiov, &local_err); 120186f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1202a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, ret); 120386f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, ret, &local_err); 120486f8cdf3SVladimir Sementsov-Ogievskiy } 120586f8cdf3SVladimir Sementsov-Ogievskiy break; 120686f8cdf3SVladimir Sementsov-Ogievskiy default: 120786f8cdf3SVladimir Sementsov-Ogievskiy if (!nbd_reply_type_is_error(chunk->type)) { 120886f8cdf3SVladimir Sementsov-Ogievskiy /* not allowed reply type */ 1209a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, -EINVAL); 121086f8cdf3SVladimir Sementsov-Ogievskiy error_setg(&local_err, 121186f8cdf3SVladimir Sementsov-Ogievskiy "Unexpected reply type: %d (%s) for CMD_READ", 121286f8cdf3SVladimir Sementsov-Ogievskiy chunk->type, nbd_reply_type_lookup(chunk->type)); 121386f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, -EINVAL, &local_err); 121486f8cdf3SVladimir Sementsov-Ogievskiy } 121586f8cdf3SVladimir Sementsov-Ogievskiy } 121686f8cdf3SVladimir Sementsov-Ogievskiy 121786f8cdf3SVladimir Sementsov-Ogievskiy g_free(payload); 121886f8cdf3SVladimir Sementsov-Ogievskiy payload = NULL; 121986f8cdf3SVladimir Sementsov-Ogievskiy } 122086f8cdf3SVladimir Sementsov-Ogievskiy 122186f8cdf3SVladimir Sementsov-Ogievskiy error_propagate(errp, iter.err); 122286f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = iter.request_ret; 122386f8cdf3SVladimir Sementsov-Ogievskiy return iter.ret; 122486f8cdf3SVladimir Sementsov-Ogievskiy } 122586f8cdf3SVladimir Sementsov-Ogievskiy 1226611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_co_receive_blockstatus_reply(BDRVNBDState *s, 122786f8cdf3SVladimir Sementsov-Ogievskiy uint64_t handle, uint64_t length, 122886f8cdf3SVladimir Sementsov-Ogievskiy NBDExtent *extent, 122986f8cdf3SVladimir Sementsov-Ogievskiy int *request_ret, Error **errp) 123086f8cdf3SVladimir Sementsov-Ogievskiy { 123186f8cdf3SVladimir Sementsov-Ogievskiy NBDReplyChunkIter iter; 123286f8cdf3SVladimir Sementsov-Ogievskiy NBDReply reply; 123386f8cdf3SVladimir Sementsov-Ogievskiy void *payload = NULL; 123486f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 123586f8cdf3SVladimir Sementsov-Ogievskiy bool received = false; 123686f8cdf3SVladimir Sementsov-Ogievskiy 123786f8cdf3SVladimir Sementsov-Ogievskiy assert(!extent->length); 123886f8cdf3SVladimir Sementsov-Ogievskiy NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, &reply, &payload) { 123986f8cdf3SVladimir Sementsov-Ogievskiy int ret; 124086f8cdf3SVladimir Sementsov-Ogievskiy NBDStructuredReplyChunk *chunk = &reply.structured; 124186f8cdf3SVladimir Sementsov-Ogievskiy 124286f8cdf3SVladimir Sementsov-Ogievskiy assert(nbd_reply_is_structured(&reply)); 124386f8cdf3SVladimir Sementsov-Ogievskiy 124486f8cdf3SVladimir Sementsov-Ogievskiy switch (chunk->type) { 124586f8cdf3SVladimir Sementsov-Ogievskiy case NBD_REPLY_TYPE_BLOCK_STATUS: 124686f8cdf3SVladimir Sementsov-Ogievskiy if (received) { 1247a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, -EINVAL); 124886f8cdf3SVladimir Sementsov-Ogievskiy error_setg(&local_err, "Several BLOCK_STATUS chunks in reply"); 124986f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, -EINVAL, &local_err); 125086f8cdf3SVladimir Sementsov-Ogievskiy } 125186f8cdf3SVladimir Sementsov-Ogievskiy received = true; 125286f8cdf3SVladimir Sementsov-Ogievskiy 125386f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_parse_blockstatus_payload(s, &reply.structured, 125486f8cdf3SVladimir Sementsov-Ogievskiy payload, length, extent, 125586f8cdf3SVladimir Sementsov-Ogievskiy &local_err); 125686f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1257a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, ret); 125886f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, ret, &local_err); 125986f8cdf3SVladimir Sementsov-Ogievskiy } 126086f8cdf3SVladimir Sementsov-Ogievskiy break; 126186f8cdf3SVladimir Sementsov-Ogievskiy default: 126286f8cdf3SVladimir Sementsov-Ogievskiy if (!nbd_reply_type_is_error(chunk->type)) { 1263a34b1e5eSVladimir Sementsov-Ogievskiy nbd_channel_error(s, -EINVAL); 126486f8cdf3SVladimir Sementsov-Ogievskiy error_setg(&local_err, 126586f8cdf3SVladimir Sementsov-Ogievskiy "Unexpected reply type: %d (%s) " 126686f8cdf3SVladimir Sementsov-Ogievskiy "for CMD_BLOCK_STATUS", 126786f8cdf3SVladimir Sementsov-Ogievskiy chunk->type, nbd_reply_type_lookup(chunk->type)); 126886f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, -EINVAL, &local_err); 126986f8cdf3SVladimir Sementsov-Ogievskiy } 127086f8cdf3SVladimir Sementsov-Ogievskiy } 127186f8cdf3SVladimir Sementsov-Ogievskiy 127286f8cdf3SVladimir Sementsov-Ogievskiy g_free(payload); 127386f8cdf3SVladimir Sementsov-Ogievskiy payload = NULL; 127486f8cdf3SVladimir Sementsov-Ogievskiy } 127586f8cdf3SVladimir Sementsov-Ogievskiy 127686f8cdf3SVladimir Sementsov-Ogievskiy if (!extent->length && !iter.request_ret) { 127786f8cdf3SVladimir Sementsov-Ogievskiy error_setg(&local_err, "Server did not reply with any status extents"); 127886f8cdf3SVladimir Sementsov-Ogievskiy nbd_iter_channel_error(&iter, -EIO, &local_err); 127986f8cdf3SVladimir Sementsov-Ogievskiy } 128086f8cdf3SVladimir Sementsov-Ogievskiy 128186f8cdf3SVladimir Sementsov-Ogievskiy error_propagate(errp, iter.err); 128286f8cdf3SVladimir Sementsov-Ogievskiy *request_ret = iter.request_ret; 128386f8cdf3SVladimir Sementsov-Ogievskiy return iter.ret; 128486f8cdf3SVladimir Sementsov-Ogievskiy } 128586f8cdf3SVladimir Sementsov-Ogievskiy 128686f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_co_request(BlockDriverState *bs, NBDRequest *request, 128786f8cdf3SVladimir Sementsov-Ogievskiy QEMUIOVector *write_qiov) 128886f8cdf3SVladimir Sementsov-Ogievskiy { 128986f8cdf3SVladimir Sementsov-Ogievskiy int ret, request_ret; 129086f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 1291611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 129286f8cdf3SVladimir Sementsov-Ogievskiy 129386f8cdf3SVladimir Sementsov-Ogievskiy assert(request->type != NBD_CMD_READ); 129486f8cdf3SVladimir Sementsov-Ogievskiy if (write_qiov) { 129586f8cdf3SVladimir Sementsov-Ogievskiy assert(request->type == NBD_CMD_WRITE); 129686f8cdf3SVladimir Sementsov-Ogievskiy assert(request->len == iov_size(write_qiov->iov, write_qiov->niov)); 129786f8cdf3SVladimir Sementsov-Ogievskiy } else { 129886f8cdf3SVladimir Sementsov-Ogievskiy assert(request->type != NBD_CMD_WRITE); 129986f8cdf3SVladimir Sementsov-Ogievskiy } 1300f7651539SVladimir Sementsov-Ogievskiy 1301f7651539SVladimir Sementsov-Ogievskiy do { 130286f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_co_send_request(bs, request, write_qiov); 130386f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1304f7651539SVladimir Sementsov-Ogievskiy continue; 130586f8cdf3SVladimir Sementsov-Ogievskiy } 130686f8cdf3SVladimir Sementsov-Ogievskiy 1307611ae1d7SVladimir Sementsov-Ogievskiy ret = nbd_co_receive_return_code(s, request->handle, 130886f8cdf3SVladimir Sementsov-Ogievskiy &request_ret, &local_err); 130986f8cdf3SVladimir Sementsov-Ogievskiy if (local_err) { 1310f7651539SVladimir Sementsov-Ogievskiy trace_nbd_co_request_fail(request->from, request->len, 1311f7651539SVladimir Sementsov-Ogievskiy request->handle, request->flags, 1312f7651539SVladimir Sementsov-Ogievskiy request->type, 131386f8cdf3SVladimir Sementsov-Ogievskiy nbd_cmd_lookup(request->type), 131486f8cdf3SVladimir Sementsov-Ogievskiy ret, error_get_pretty(local_err)); 131586f8cdf3SVladimir Sementsov-Ogievskiy error_free(local_err); 1316f7651539SVladimir Sementsov-Ogievskiy local_err = NULL; 131786f8cdf3SVladimir Sementsov-Ogievskiy } 1318f7651539SVladimir Sementsov-Ogievskiy } while (ret < 0 && nbd_client_connecting_wait(s)); 1319f7651539SVladimir Sementsov-Ogievskiy 132086f8cdf3SVladimir Sementsov-Ogievskiy return ret ? ret : request_ret; 132186f8cdf3SVladimir Sementsov-Ogievskiy } 132286f8cdf3SVladimir Sementsov-Ogievskiy 132386f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_client_co_preadv(BlockDriverState *bs, uint64_t offset, 132486f8cdf3SVladimir Sementsov-Ogievskiy uint64_t bytes, QEMUIOVector *qiov, int flags) 132586f8cdf3SVladimir Sementsov-Ogievskiy { 132686f8cdf3SVladimir Sementsov-Ogievskiy int ret, request_ret; 132786f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 1328611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 132986f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 133086f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_READ, 133186f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 133286f8cdf3SVladimir Sementsov-Ogievskiy .len = bytes, 133386f8cdf3SVladimir Sementsov-Ogievskiy }; 133486f8cdf3SVladimir Sementsov-Ogievskiy 133586f8cdf3SVladimir Sementsov-Ogievskiy assert(bytes <= NBD_MAX_BUFFER_SIZE); 133686f8cdf3SVladimir Sementsov-Ogievskiy assert(!flags); 133786f8cdf3SVladimir Sementsov-Ogievskiy 133886f8cdf3SVladimir Sementsov-Ogievskiy if (!bytes) { 133986f8cdf3SVladimir Sementsov-Ogievskiy return 0; 134086f8cdf3SVladimir Sementsov-Ogievskiy } 134186f8cdf3SVladimir Sementsov-Ogievskiy /* 134286f8cdf3SVladimir Sementsov-Ogievskiy * Work around the fact that the block layer doesn't do 134386f8cdf3SVladimir Sementsov-Ogievskiy * byte-accurate sizing yet - if the read exceeds the server's 134486f8cdf3SVladimir Sementsov-Ogievskiy * advertised size because the block layer rounded size up, then 134586f8cdf3SVladimir Sementsov-Ogievskiy * truncate the request to the server and tail-pad with zero. 134686f8cdf3SVladimir Sementsov-Ogievskiy */ 1347611ae1d7SVladimir Sementsov-Ogievskiy if (offset >= s->info.size) { 134886f8cdf3SVladimir Sementsov-Ogievskiy assert(bytes < BDRV_SECTOR_SIZE); 134986f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_memset(qiov, 0, 0, bytes); 135086f8cdf3SVladimir Sementsov-Ogievskiy return 0; 135186f8cdf3SVladimir Sementsov-Ogievskiy } 1352611ae1d7SVladimir Sementsov-Ogievskiy if (offset + bytes > s->info.size) { 1353611ae1d7SVladimir Sementsov-Ogievskiy uint64_t slop = offset + bytes - s->info.size; 135486f8cdf3SVladimir Sementsov-Ogievskiy 135586f8cdf3SVladimir Sementsov-Ogievskiy assert(slop < BDRV_SECTOR_SIZE); 135686f8cdf3SVladimir Sementsov-Ogievskiy qemu_iovec_memset(qiov, bytes - slop, 0, slop); 135786f8cdf3SVladimir Sementsov-Ogievskiy request.len -= slop; 135886f8cdf3SVladimir Sementsov-Ogievskiy } 135986f8cdf3SVladimir Sementsov-Ogievskiy 1360f7651539SVladimir Sementsov-Ogievskiy do { 136186f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_co_send_request(bs, &request, NULL); 136286f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1363f7651539SVladimir Sementsov-Ogievskiy continue; 136486f8cdf3SVladimir Sementsov-Ogievskiy } 136586f8cdf3SVladimir Sementsov-Ogievskiy 1366611ae1d7SVladimir Sementsov-Ogievskiy ret = nbd_co_receive_cmdread_reply(s, request.handle, offset, qiov, 136786f8cdf3SVladimir Sementsov-Ogievskiy &request_ret, &local_err); 136886f8cdf3SVladimir Sementsov-Ogievskiy if (local_err) { 136986f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_co_request_fail(request.from, request.len, request.handle, 137086f8cdf3SVladimir Sementsov-Ogievskiy request.flags, request.type, 137186f8cdf3SVladimir Sementsov-Ogievskiy nbd_cmd_lookup(request.type), 137286f8cdf3SVladimir Sementsov-Ogievskiy ret, error_get_pretty(local_err)); 137386f8cdf3SVladimir Sementsov-Ogievskiy error_free(local_err); 1374f7651539SVladimir Sementsov-Ogievskiy local_err = NULL; 137586f8cdf3SVladimir Sementsov-Ogievskiy } 1376f7651539SVladimir Sementsov-Ogievskiy } while (ret < 0 && nbd_client_connecting_wait(s)); 1377f7651539SVladimir Sementsov-Ogievskiy 137886f8cdf3SVladimir Sementsov-Ogievskiy return ret ? ret : request_ret; 137986f8cdf3SVladimir Sementsov-Ogievskiy } 138086f8cdf3SVladimir Sementsov-Ogievskiy 138186f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_client_co_pwritev(BlockDriverState *bs, uint64_t offset, 138286f8cdf3SVladimir Sementsov-Ogievskiy uint64_t bytes, QEMUIOVector *qiov, int flags) 138386f8cdf3SVladimir Sementsov-Ogievskiy { 1384611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 138586f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 138686f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_WRITE, 138786f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 138886f8cdf3SVladimir Sementsov-Ogievskiy .len = bytes, 138986f8cdf3SVladimir Sementsov-Ogievskiy }; 139086f8cdf3SVladimir Sementsov-Ogievskiy 1391611ae1d7SVladimir Sementsov-Ogievskiy assert(!(s->info.flags & NBD_FLAG_READ_ONLY)); 139286f8cdf3SVladimir Sementsov-Ogievskiy if (flags & BDRV_REQ_FUA) { 1393611ae1d7SVladimir Sementsov-Ogievskiy assert(s->info.flags & NBD_FLAG_SEND_FUA); 139486f8cdf3SVladimir Sementsov-Ogievskiy request.flags |= NBD_CMD_FLAG_FUA; 139586f8cdf3SVladimir Sementsov-Ogievskiy } 139686f8cdf3SVladimir Sementsov-Ogievskiy 139786f8cdf3SVladimir Sementsov-Ogievskiy assert(bytes <= NBD_MAX_BUFFER_SIZE); 139886f8cdf3SVladimir Sementsov-Ogievskiy 139986f8cdf3SVladimir Sementsov-Ogievskiy if (!bytes) { 140086f8cdf3SVladimir Sementsov-Ogievskiy return 0; 140186f8cdf3SVladimir Sementsov-Ogievskiy } 140286f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_request(bs, &request, qiov); 140386f8cdf3SVladimir Sementsov-Ogievskiy } 140486f8cdf3SVladimir Sementsov-Ogievskiy 140586f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_client_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, 140686f8cdf3SVladimir Sementsov-Ogievskiy int bytes, BdrvRequestFlags flags) 140786f8cdf3SVladimir Sementsov-Ogievskiy { 1408611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 140986f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 141086f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_WRITE_ZEROES, 141186f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 141286f8cdf3SVladimir Sementsov-Ogievskiy .len = bytes, 141386f8cdf3SVladimir Sementsov-Ogievskiy }; 141486f8cdf3SVladimir Sementsov-Ogievskiy 1415611ae1d7SVladimir Sementsov-Ogievskiy assert(!(s->info.flags & NBD_FLAG_READ_ONLY)); 1416611ae1d7SVladimir Sementsov-Ogievskiy if (!(s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES)) { 141786f8cdf3SVladimir Sementsov-Ogievskiy return -ENOTSUP; 141886f8cdf3SVladimir Sementsov-Ogievskiy } 141986f8cdf3SVladimir Sementsov-Ogievskiy 142086f8cdf3SVladimir Sementsov-Ogievskiy if (flags & BDRV_REQ_FUA) { 1421611ae1d7SVladimir Sementsov-Ogievskiy assert(s->info.flags & NBD_FLAG_SEND_FUA); 142286f8cdf3SVladimir Sementsov-Ogievskiy request.flags |= NBD_CMD_FLAG_FUA; 142386f8cdf3SVladimir Sementsov-Ogievskiy } 142486f8cdf3SVladimir Sementsov-Ogievskiy if (!(flags & BDRV_REQ_MAY_UNMAP)) { 142586f8cdf3SVladimir Sementsov-Ogievskiy request.flags |= NBD_CMD_FLAG_NO_HOLE; 142686f8cdf3SVladimir Sementsov-Ogievskiy } 1427f061656cSEric Blake if (flags & BDRV_REQ_NO_FALLBACK) { 1428f061656cSEric Blake assert(s->info.flags & NBD_FLAG_SEND_FAST_ZERO); 1429f061656cSEric Blake request.flags |= NBD_CMD_FLAG_FAST_ZERO; 1430f061656cSEric Blake } 143186f8cdf3SVladimir Sementsov-Ogievskiy 143286f8cdf3SVladimir Sementsov-Ogievskiy if (!bytes) { 143386f8cdf3SVladimir Sementsov-Ogievskiy return 0; 143486f8cdf3SVladimir Sementsov-Ogievskiy } 143586f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_request(bs, &request, NULL); 143686f8cdf3SVladimir Sementsov-Ogievskiy } 143786f8cdf3SVladimir Sementsov-Ogievskiy 143886f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_client_co_flush(BlockDriverState *bs) 143986f8cdf3SVladimir Sementsov-Ogievskiy { 1440611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 144186f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { .type = NBD_CMD_FLUSH }; 144286f8cdf3SVladimir Sementsov-Ogievskiy 1443611ae1d7SVladimir Sementsov-Ogievskiy if (!(s->info.flags & NBD_FLAG_SEND_FLUSH)) { 144486f8cdf3SVladimir Sementsov-Ogievskiy return 0; 144586f8cdf3SVladimir Sementsov-Ogievskiy } 144686f8cdf3SVladimir Sementsov-Ogievskiy 144786f8cdf3SVladimir Sementsov-Ogievskiy request.from = 0; 144886f8cdf3SVladimir Sementsov-Ogievskiy request.len = 0; 144986f8cdf3SVladimir Sementsov-Ogievskiy 145086f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_request(bs, &request, NULL); 145186f8cdf3SVladimir Sementsov-Ogievskiy } 145286f8cdf3SVladimir Sementsov-Ogievskiy 145386f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_client_co_pdiscard(BlockDriverState *bs, int64_t offset, 145486f8cdf3SVladimir Sementsov-Ogievskiy int bytes) 145586f8cdf3SVladimir Sementsov-Ogievskiy { 1456611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 145786f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 145886f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_TRIM, 145986f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 146086f8cdf3SVladimir Sementsov-Ogievskiy .len = bytes, 146186f8cdf3SVladimir Sementsov-Ogievskiy }; 146286f8cdf3SVladimir Sementsov-Ogievskiy 1463611ae1d7SVladimir Sementsov-Ogievskiy assert(!(s->info.flags & NBD_FLAG_READ_ONLY)); 1464611ae1d7SVladimir Sementsov-Ogievskiy if (!(s->info.flags & NBD_FLAG_SEND_TRIM) || !bytes) { 146586f8cdf3SVladimir Sementsov-Ogievskiy return 0; 146686f8cdf3SVladimir Sementsov-Ogievskiy } 146786f8cdf3SVladimir Sementsov-Ogievskiy 146886f8cdf3SVladimir Sementsov-Ogievskiy return nbd_co_request(bs, &request, NULL); 146986f8cdf3SVladimir Sementsov-Ogievskiy } 147086f8cdf3SVladimir Sementsov-Ogievskiy 147186f8cdf3SVladimir Sementsov-Ogievskiy static int coroutine_fn nbd_client_co_block_status( 147286f8cdf3SVladimir Sementsov-Ogievskiy BlockDriverState *bs, bool want_zero, int64_t offset, int64_t bytes, 147386f8cdf3SVladimir Sementsov-Ogievskiy int64_t *pnum, int64_t *map, BlockDriverState **file) 147486f8cdf3SVladimir Sementsov-Ogievskiy { 147586f8cdf3SVladimir Sementsov-Ogievskiy int ret, request_ret; 147686f8cdf3SVladimir Sementsov-Ogievskiy NBDExtent extent = { 0 }; 1477611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 147886f8cdf3SVladimir Sementsov-Ogievskiy Error *local_err = NULL; 147986f8cdf3SVladimir Sementsov-Ogievskiy 148086f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { 148186f8cdf3SVladimir Sementsov-Ogievskiy .type = NBD_CMD_BLOCK_STATUS, 148286f8cdf3SVladimir Sementsov-Ogievskiy .from = offset, 14836bf792b4SVladimir Sementsov-Ogievskiy .len = MIN(QEMU_ALIGN_DOWN(INT_MAX, bs->bl.request_alignment), 1484611ae1d7SVladimir Sementsov-Ogievskiy MIN(bytes, s->info.size - offset)), 148586f8cdf3SVladimir Sementsov-Ogievskiy .flags = NBD_CMD_FLAG_REQ_ONE, 148686f8cdf3SVladimir Sementsov-Ogievskiy }; 148786f8cdf3SVladimir Sementsov-Ogievskiy 1488611ae1d7SVladimir Sementsov-Ogievskiy if (!s->info.base_allocation) { 148986f8cdf3SVladimir Sementsov-Ogievskiy *pnum = bytes; 149086f8cdf3SVladimir Sementsov-Ogievskiy *map = offset; 149186f8cdf3SVladimir Sementsov-Ogievskiy *file = bs; 149286f8cdf3SVladimir Sementsov-Ogievskiy return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; 149386f8cdf3SVladimir Sementsov-Ogievskiy } 149486f8cdf3SVladimir Sementsov-Ogievskiy 149586f8cdf3SVladimir Sementsov-Ogievskiy /* 149686f8cdf3SVladimir Sementsov-Ogievskiy * Work around the fact that the block layer doesn't do 149786f8cdf3SVladimir Sementsov-Ogievskiy * byte-accurate sizing yet - if the status request exceeds the 149886f8cdf3SVladimir Sementsov-Ogievskiy * server's advertised size because the block layer rounded size 149986f8cdf3SVladimir Sementsov-Ogievskiy * up, we truncated the request to the server (above), or are 150086f8cdf3SVladimir Sementsov-Ogievskiy * called on just the hole. 150186f8cdf3SVladimir Sementsov-Ogievskiy */ 1502611ae1d7SVladimir Sementsov-Ogievskiy if (offset >= s->info.size) { 150386f8cdf3SVladimir Sementsov-Ogievskiy *pnum = bytes; 150486f8cdf3SVladimir Sementsov-Ogievskiy assert(bytes < BDRV_SECTOR_SIZE); 150586f8cdf3SVladimir Sementsov-Ogievskiy /* Intentionally don't report offset_valid for the hole */ 150686f8cdf3SVladimir Sementsov-Ogievskiy return BDRV_BLOCK_ZERO; 150786f8cdf3SVladimir Sementsov-Ogievskiy } 150886f8cdf3SVladimir Sementsov-Ogievskiy 1509611ae1d7SVladimir Sementsov-Ogievskiy if (s->info.min_block) { 1510611ae1d7SVladimir Sementsov-Ogievskiy assert(QEMU_IS_ALIGNED(request.len, s->info.min_block)); 151186f8cdf3SVladimir Sementsov-Ogievskiy } 1512f7651539SVladimir Sementsov-Ogievskiy do { 151386f8cdf3SVladimir Sementsov-Ogievskiy ret = nbd_co_send_request(bs, &request, NULL); 151486f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0) { 1515f7651539SVladimir Sementsov-Ogievskiy continue; 151686f8cdf3SVladimir Sementsov-Ogievskiy } 151786f8cdf3SVladimir Sementsov-Ogievskiy 1518611ae1d7SVladimir Sementsov-Ogievskiy ret = nbd_co_receive_blockstatus_reply(s, request.handle, bytes, 1519f7651539SVladimir Sementsov-Ogievskiy &extent, &request_ret, 1520f7651539SVladimir Sementsov-Ogievskiy &local_err); 152186f8cdf3SVladimir Sementsov-Ogievskiy if (local_err) { 152286f8cdf3SVladimir Sementsov-Ogievskiy trace_nbd_co_request_fail(request.from, request.len, request.handle, 152386f8cdf3SVladimir Sementsov-Ogievskiy request.flags, request.type, 152486f8cdf3SVladimir Sementsov-Ogievskiy nbd_cmd_lookup(request.type), 152586f8cdf3SVladimir Sementsov-Ogievskiy ret, error_get_pretty(local_err)); 152686f8cdf3SVladimir Sementsov-Ogievskiy error_free(local_err); 1527f7651539SVladimir Sementsov-Ogievskiy local_err = NULL; 152886f8cdf3SVladimir Sementsov-Ogievskiy } 1529f7651539SVladimir Sementsov-Ogievskiy } while (ret < 0 && nbd_client_connecting_wait(s)); 1530f7651539SVladimir Sementsov-Ogievskiy 153186f8cdf3SVladimir Sementsov-Ogievskiy if (ret < 0 || request_ret < 0) { 153286f8cdf3SVladimir Sementsov-Ogievskiy return ret ? ret : request_ret; 153386f8cdf3SVladimir Sementsov-Ogievskiy } 153486f8cdf3SVladimir Sementsov-Ogievskiy 153586f8cdf3SVladimir Sementsov-Ogievskiy assert(extent.length); 153686f8cdf3SVladimir Sementsov-Ogievskiy *pnum = extent.length; 153786f8cdf3SVladimir Sementsov-Ogievskiy *map = offset; 153886f8cdf3SVladimir Sementsov-Ogievskiy *file = bs; 153986f8cdf3SVladimir Sementsov-Ogievskiy return (extent.flags & NBD_STATE_HOLE ? 0 : BDRV_BLOCK_DATA) | 154086f8cdf3SVladimir Sementsov-Ogievskiy (extent.flags & NBD_STATE_ZERO ? BDRV_BLOCK_ZERO : 0) | 154186f8cdf3SVladimir Sementsov-Ogievskiy BDRV_BLOCK_OFFSET_VALID; 154286f8cdf3SVladimir Sementsov-Ogievskiy } 154386f8cdf3SVladimir Sementsov-Ogievskiy 1544e99754b4SMaxim Levitsky static int nbd_client_reopen_prepare(BDRVReopenState *state, 1545e99754b4SMaxim Levitsky BlockReopenQueue *queue, Error **errp) 1546e99754b4SMaxim Levitsky { 1547e99754b4SMaxim Levitsky BDRVNBDState *s = (BDRVNBDState *)state->bs->opaque; 1548e99754b4SMaxim Levitsky 1549e99754b4SMaxim Levitsky if ((state->flags & BDRV_O_RDWR) && (s->info.flags & NBD_FLAG_READ_ONLY)) { 1550e99754b4SMaxim Levitsky error_setg(errp, "Can't reopen read-only NBD mount as read/write"); 1551e99754b4SMaxim Levitsky return -EACCES; 1552e99754b4SMaxim Levitsky } 1553e99754b4SMaxim Levitsky return 0; 1554e99754b4SMaxim Levitsky } 1555e99754b4SMaxim Levitsky 1556fee091cdSLukas Straub static void nbd_yank(void *opaque) 1557fee091cdSLukas Straub { 1558fee091cdSLukas Straub BlockDriverState *bs = opaque; 1559fee091cdSLukas Straub BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 1560fee091cdSLukas Straub 1561fee091cdSLukas Straub qatomic_store_release(&s->state, NBD_CLIENT_QUIT); 156295a078eaSVladimir Sementsov-Ogievskiy qio_channel_shutdown(QIO_CHANNEL(s->ioc), QIO_CHANNEL_SHUTDOWN_BOTH, NULL); 1563fee091cdSLukas Straub } 1564fee091cdSLukas Straub 156586f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_client_close(BlockDriverState *bs) 156686f8cdf3SVladimir Sementsov-Ogievskiy { 1567611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 156886f8cdf3SVladimir Sementsov-Ogievskiy NBDRequest request = { .type = NBD_CMD_DISC }; 156986f8cdf3SVladimir Sementsov-Ogievskiy 1570f7651539SVladimir Sementsov-Ogievskiy if (s->ioc) { 1571611ae1d7SVladimir Sementsov-Ogievskiy nbd_send_request(s->ioc, &request); 1572f7651539SVladimir Sementsov-Ogievskiy } 157386f8cdf3SVladimir Sementsov-Ogievskiy 157486f8cdf3SVladimir Sementsov-Ogievskiy nbd_teardown_connection(bs); 157586f8cdf3SVladimir Sementsov-Ogievskiy } 157686f8cdf3SVladimir Sementsov-Ogievskiy 157786f8cdf3SVladimir Sementsov-Ogievskiy 15788f071c9dSVladimir Sementsov-Ogievskiy /* 15798f071c9dSVladimir Sementsov-Ogievskiy * Parse nbd_open options 15808f071c9dSVladimir Sementsov-Ogievskiy */ 158186f8cdf3SVladimir Sementsov-Ogievskiy 1582f53a1febSKevin Wolf static int nbd_parse_uri(const char *filename, QDict *options) 15831d7d2a9dSPaolo Bonzini { 15841d7d2a9dSPaolo Bonzini URI *uri; 15851d7d2a9dSPaolo Bonzini const char *p; 15861d7d2a9dSPaolo Bonzini QueryParams *qp = NULL; 15871d7d2a9dSPaolo Bonzini int ret = 0; 1588f53a1febSKevin Wolf bool is_unix; 15891d7d2a9dSPaolo Bonzini 15901d7d2a9dSPaolo Bonzini uri = uri_parse(filename); 15911d7d2a9dSPaolo Bonzini if (!uri) { 15921d7d2a9dSPaolo Bonzini return -EINVAL; 15931d7d2a9dSPaolo Bonzini } 15941d7d2a9dSPaolo Bonzini 15951d7d2a9dSPaolo Bonzini /* transport */ 1596f69165a8SMax Reitz if (!g_strcmp0(uri->scheme, "nbd")) { 1597f53a1febSKevin Wolf is_unix = false; 1598f69165a8SMax Reitz } else if (!g_strcmp0(uri->scheme, "nbd+tcp")) { 1599f53a1febSKevin Wolf is_unix = false; 1600f69165a8SMax Reitz } else if (!g_strcmp0(uri->scheme, "nbd+unix")) { 1601f53a1febSKevin Wolf is_unix = true; 16021d7d2a9dSPaolo Bonzini } else { 16031d7d2a9dSPaolo Bonzini ret = -EINVAL; 16041d7d2a9dSPaolo Bonzini goto out; 16051d7d2a9dSPaolo Bonzini } 16061d7d2a9dSPaolo Bonzini 16072485f22fSEric Blake p = uri->path ? uri->path : ""; 16082485f22fSEric Blake if (p[0] == '/') { 16092485f22fSEric Blake p++; 16102485f22fSEric Blake } 16111d7d2a9dSPaolo Bonzini if (p[0]) { 161246f5ac20SEric Blake qdict_put_str(options, "export", p); 16131d7d2a9dSPaolo Bonzini } 16141d7d2a9dSPaolo Bonzini 16151d7d2a9dSPaolo Bonzini qp = query_params_parse(uri->query); 1616f53a1febSKevin Wolf if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) { 16171d7d2a9dSPaolo Bonzini ret = -EINVAL; 16181d7d2a9dSPaolo Bonzini goto out; 16191d7d2a9dSPaolo Bonzini } 16201d7d2a9dSPaolo Bonzini 1621f53a1febSKevin Wolf if (is_unix) { 16221d7d2a9dSPaolo Bonzini /* nbd+unix:///export?socket=path */ 16231d7d2a9dSPaolo Bonzini if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) { 16241d7d2a9dSPaolo Bonzini ret = -EINVAL; 16251d7d2a9dSPaolo Bonzini goto out; 16261d7d2a9dSPaolo Bonzini } 162746f5ac20SEric Blake qdict_put_str(options, "server.type", "unix"); 162846f5ac20SEric Blake qdict_put_str(options, "server.path", qp->p[0].value); 16291d7d2a9dSPaolo Bonzini } else { 163023307908SJán Tomko QString *host; 1631f84d431bSMax Reitz char *port_str; 1632f84d431bSMax Reitz 1633bebbf7faSKevin Wolf /* nbd[+tcp]://host[:port]/export */ 16341d7d2a9dSPaolo Bonzini if (!uri->server) { 16351d7d2a9dSPaolo Bonzini ret = -EINVAL; 16361d7d2a9dSPaolo Bonzini goto out; 16371d7d2a9dSPaolo Bonzini } 1638f17c90beSKevin Wolf 163923307908SJán Tomko /* strip braces from literal IPv6 address */ 164023307908SJán Tomko if (uri->server[0] == '[') { 164123307908SJán Tomko host = qstring_from_substr(uri->server, 1, 1642ba891d68SMarkus Armbruster strlen(uri->server) - 1); 164323307908SJán Tomko } else { 164423307908SJán Tomko host = qstring_from_str(uri->server); 164523307908SJán Tomko } 164623307908SJán Tomko 164746f5ac20SEric Blake qdict_put_str(options, "server.type", "inet"); 16489445673eSMarkus Armbruster qdict_put(options, "server.host", host); 1649f84d431bSMax Reitz 1650f84d431bSMax Reitz port_str = g_strdup_printf("%d", uri->port ?: NBD_DEFAULT_PORT); 165146f5ac20SEric Blake qdict_put_str(options, "server.port", port_str); 1652f53a1febSKevin Wolf g_free(port_str); 16531d7d2a9dSPaolo Bonzini } 16541d7d2a9dSPaolo Bonzini 16551d7d2a9dSPaolo Bonzini out: 16561d7d2a9dSPaolo Bonzini if (qp) { 16571d7d2a9dSPaolo Bonzini query_params_free(qp); 16581d7d2a9dSPaolo Bonzini } 16591d7d2a9dSPaolo Bonzini uri_free(uri); 16601d7d2a9dSPaolo Bonzini return ret; 16611d7d2a9dSPaolo Bonzini } 16621d7d2a9dSPaolo Bonzini 166348c38e0bSMax Reitz static bool nbd_has_filename_options_conflict(QDict *options, Error **errp) 166448c38e0bSMax Reitz { 166548c38e0bSMax Reitz const QDictEntry *e; 166648c38e0bSMax Reitz 166748c38e0bSMax Reitz for (e = qdict_first(options); e; e = qdict_next(options, e)) { 166848c38e0bSMax Reitz if (!strcmp(e->key, "host") || 166948c38e0bSMax Reitz !strcmp(e->key, "port") || 167048c38e0bSMax Reitz !strcmp(e->key, "path") || 1671491d6c7cSMax Reitz !strcmp(e->key, "export") || 1672491d6c7cSMax Reitz strstart(e->key, "server.", NULL)) 167348c38e0bSMax Reitz { 167448c38e0bSMax Reitz error_setg(errp, "Option '%s' cannot be used with a file name", 167548c38e0bSMax Reitz e->key); 167648c38e0bSMax Reitz return true; 167748c38e0bSMax Reitz } 167848c38e0bSMax Reitz } 167948c38e0bSMax Reitz 168048c38e0bSMax Reitz return false; 168148c38e0bSMax Reitz } 168248c38e0bSMax Reitz 16836963a30dSKevin Wolf static void nbd_parse_filename(const char *filename, QDict *options, 16846963a30dSKevin Wolf Error **errp) 1685019d6b8fSAnthony Liguori { 1686df18c04eSEric Blake g_autofree char *file = NULL; 168733897dc7SNick Thomas char *export_name; 168833897dc7SNick Thomas const char *host_spec; 1689019d6b8fSAnthony Liguori const char *unixpath; 1690019d6b8fSAnthony Liguori 169148c38e0bSMax Reitz if (nbd_has_filename_options_conflict(options, errp)) { 1692681e7ad0SKevin Wolf return; 1693681e7ad0SKevin Wolf } 1694681e7ad0SKevin Wolf 16951d7d2a9dSPaolo Bonzini if (strstr(filename, "://")) { 16966963a30dSKevin Wolf int ret = nbd_parse_uri(filename, options); 16976963a30dSKevin Wolf if (ret < 0) { 16986963a30dSKevin Wolf error_setg(errp, "No valid URL specified"); 16996963a30dSKevin Wolf } 17006963a30dSKevin Wolf return; 17011d7d2a9dSPaolo Bonzini } 17021d7d2a9dSPaolo Bonzini 17037267c094SAnthony Liguori file = g_strdup(filename); 17041d45f8b5SLaurent Vivier 170533897dc7SNick Thomas export_name = strstr(file, EN_OPTSTR); 170633897dc7SNick Thomas if (export_name) { 170733897dc7SNick Thomas if (export_name[strlen(EN_OPTSTR)] == 0) { 1708df18c04eSEric Blake return; 17091d45f8b5SLaurent Vivier } 171033897dc7SNick Thomas export_name[0] = 0; /* truncate 'file' */ 171133897dc7SNick Thomas export_name += strlen(EN_OPTSTR); 1712f53a1febSKevin Wolf 171346f5ac20SEric Blake qdict_put_str(options, "export", export_name); 17141d45f8b5SLaurent Vivier } 17151d45f8b5SLaurent Vivier 171633897dc7SNick Thomas /* extract the host_spec - fail if it's not nbd:... */ 171733897dc7SNick Thomas if (!strstart(file, "nbd:", &host_spec)) { 17186963a30dSKevin Wolf error_setg(errp, "File name string for NBD must start with 'nbd:'"); 1719df18c04eSEric Blake return; 17201d45f8b5SLaurent Vivier } 1721019d6b8fSAnthony Liguori 1722f53a1febSKevin Wolf if (!*host_spec) { 1723df18c04eSEric Blake return; 1724f53a1febSKevin Wolf } 1725f53a1febSKevin Wolf 172633897dc7SNick Thomas /* are we a UNIX or TCP socket? */ 172733897dc7SNick Thomas if (strstart(host_spec, "unix:", &unixpath)) { 172846f5ac20SEric Blake qdict_put_str(options, "server.type", "unix"); 172946f5ac20SEric Blake qdict_put_str(options, "server.path", unixpath); 1730019d6b8fSAnthony Liguori } else { 17310785bd7aSMarkus Armbruster InetSocketAddress *addr = g_new(InetSocketAddress, 1); 1732f53a1febSKevin Wolf 17330785bd7aSMarkus Armbruster if (inet_parse(addr, host_spec, errp)) { 17340785bd7aSMarkus Armbruster goto out_inet; 1735f17c90beSKevin Wolf } 1736f53a1febSKevin Wolf 173746f5ac20SEric Blake qdict_put_str(options, "server.type", "inet"); 173846f5ac20SEric Blake qdict_put_str(options, "server.host", addr->host); 173946f5ac20SEric Blake qdict_put_str(options, "server.port", addr->port); 17400785bd7aSMarkus Armbruster out_inet: 1741f53a1febSKevin Wolf qapi_free_InetSocketAddress(addr); 17421d45f8b5SLaurent Vivier } 174333897dc7SNick Thomas } 1744f53a1febSKevin Wolf 1745491d6c7cSMax Reitz static bool nbd_process_legacy_socket_options(QDict *output_options, 1746491d6c7cSMax Reitz QemuOpts *legacy_opts, 1747491d6c7cSMax Reitz Error **errp) 1748f53a1febSKevin Wolf { 1749491d6c7cSMax Reitz const char *path = qemu_opt_get(legacy_opts, "path"); 1750491d6c7cSMax Reitz const char *host = qemu_opt_get(legacy_opts, "host"); 1751491d6c7cSMax Reitz const char *port = qemu_opt_get(legacy_opts, "port"); 1752491d6c7cSMax Reitz const QDictEntry *e; 1753f53a1febSKevin Wolf 1754491d6c7cSMax Reitz if (!path && !host && !port) { 1755491d6c7cSMax Reitz return true; 1756491d6c7cSMax Reitz } 175703504d05SMax Reitz 1758491d6c7cSMax Reitz for (e = qdict_first(output_options); e; e = qdict_next(output_options, e)) 1759491d6c7cSMax Reitz { 1760491d6c7cSMax Reitz if (strstart(e->key, "server.", NULL)) { 1761491d6c7cSMax Reitz error_setg(errp, "Cannot use 'server' and path/host/port at the " 1762491d6c7cSMax Reitz "same time"); 1763491d6c7cSMax Reitz return false; 1764491d6c7cSMax Reitz } 1765491d6c7cSMax Reitz } 1766491d6c7cSMax Reitz 1767491d6c7cSMax Reitz if (path && host) { 176882d73014SMax Reitz error_setg(errp, "path and host may not be used at the same time"); 1769491d6c7cSMax Reitz return false; 1770491d6c7cSMax Reitz } else if (path) { 1771491d6c7cSMax Reitz if (port) { 1772442045cbSMax Reitz error_setg(errp, "port may not be used without host"); 1773491d6c7cSMax Reitz return false; 1774442045cbSMax Reitz } 1775019d6b8fSAnthony Liguori 177646f5ac20SEric Blake qdict_put_str(output_options, "server.type", "unix"); 177746f5ac20SEric Blake qdict_put_str(output_options, "server.path", path); 1778491d6c7cSMax Reitz } else if (host) { 177946f5ac20SEric Blake qdict_put_str(output_options, "server.type", "inet"); 178046f5ac20SEric Blake qdict_put_str(output_options, "server.host", host); 178146f5ac20SEric Blake qdict_put_str(output_options, "server.port", 178246f5ac20SEric Blake port ?: stringify(NBD_DEFAULT_PORT)); 17837a5ed437SDaniel P. Berrange } 1784491d6c7cSMax Reitz 1785491d6c7cSMax Reitz return true; 1786491d6c7cSMax Reitz } 1787491d6c7cSMax Reitz 178862cf396bSMarkus Armbruster static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options, 17899445673eSMarkus Armbruster Error **errp) 1790491d6c7cSMax Reitz { 179162cf396bSMarkus Armbruster SocketAddress *saddr = NULL; 1792491d6c7cSMax Reitz QDict *addr = NULL; 1793491d6c7cSMax Reitz Visitor *iv = NULL; 1794491d6c7cSMax Reitz 1795491d6c7cSMax Reitz qdict_extract_subqdict(options, &addr, "server."); 1796491d6c7cSMax Reitz if (!qdict_size(addr)) { 1797491d6c7cSMax Reitz error_setg(errp, "NBD server address missing"); 1798491d6c7cSMax Reitz goto done; 1799491d6c7cSMax Reitz } 1800491d6c7cSMax Reitz 1801af91062eSMarkus Armbruster iv = qobject_input_visitor_new_flat_confused(addr, errp); 1802af91062eSMarkus Armbruster if (!iv) { 1803491d6c7cSMax Reitz goto done; 1804491d6c7cSMax Reitz } 1805491d6c7cSMax Reitz 1806af175e85SMarkus Armbruster if (!visit_type_SocketAddress(iv, NULL, &saddr, errp)) { 1807491d6c7cSMax Reitz goto done; 1808f53a1febSKevin Wolf } 1809f53a1febSKevin Wolf 18106cc702beSVladimir Sementsov-Ogievskiy if (socket_address_parse_named_fd(saddr, errp) < 0) { 18116cc702beSVladimir Sementsov-Ogievskiy qapi_free_SocketAddress(saddr); 18126cc702beSVladimir Sementsov-Ogievskiy saddr = NULL; 18136cc702beSVladimir Sementsov-Ogievskiy goto done; 18146cc702beSVladimir Sementsov-Ogievskiy } 18156cc702beSVladimir Sementsov-Ogievskiy 1816491d6c7cSMax Reitz done: 1817cb3e7f08SMarc-André Lureau qobject_unref(addr); 1818491d6c7cSMax Reitz visit_free(iv); 18197a5ed437SDaniel P. Berrange return saddr; 1820f53a1febSKevin Wolf } 1821f53a1febSKevin Wolf 182275822a12SDaniel P. Berrange static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp) 182375822a12SDaniel P. Berrange { 182475822a12SDaniel P. Berrange Object *obj; 182575822a12SDaniel P. Berrange QCryptoTLSCreds *creds; 182675822a12SDaniel P. Berrange 182775822a12SDaniel P. Berrange obj = object_resolve_path_component( 182875822a12SDaniel P. Berrange object_get_objects_root(), id); 182975822a12SDaniel P. Berrange if (!obj) { 183075822a12SDaniel P. Berrange error_setg(errp, "No TLS credentials with id '%s'", 183175822a12SDaniel P. Berrange id); 183275822a12SDaniel P. Berrange return NULL; 183375822a12SDaniel P. Berrange } 183475822a12SDaniel P. Berrange creds = (QCryptoTLSCreds *) 183575822a12SDaniel P. Berrange object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS); 183675822a12SDaniel P. Berrange if (!creds) { 183775822a12SDaniel P. Berrange error_setg(errp, "Object with id '%s' is not TLS credentials", 183875822a12SDaniel P. Berrange id); 183975822a12SDaniel P. Berrange return NULL; 184075822a12SDaniel P. Berrange } 184175822a12SDaniel P. Berrange 1842*7b3b6168SPhilippe Mathieu-Daudé if (!qcrypto_tls_creds_check_endpoint(creds, 1843*7b3b6168SPhilippe Mathieu-Daudé QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT, 1844*7b3b6168SPhilippe Mathieu-Daudé errp)) { 184575822a12SDaniel P. Berrange return NULL; 184675822a12SDaniel P. Berrange } 184775822a12SDaniel P. Berrange object_ref(obj); 184875822a12SDaniel P. Berrange return creds; 184975822a12SDaniel P. Berrange } 185075822a12SDaniel P. Berrange 185175822a12SDaniel P. Berrange 18527ccc44fdSMax Reitz static QemuOptsList nbd_runtime_opts = { 18537ccc44fdSMax Reitz .name = "nbd", 18547ccc44fdSMax Reitz .head = QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts.head), 18557ccc44fdSMax Reitz .desc = { 18567ccc44fdSMax Reitz { 18577ccc44fdSMax Reitz .name = "host", 18587ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 18597ccc44fdSMax Reitz .help = "TCP host to connect to", 18607ccc44fdSMax Reitz }, 18617ccc44fdSMax Reitz { 18627ccc44fdSMax Reitz .name = "port", 18637ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 18647ccc44fdSMax Reitz .help = "TCP port to connect to", 18657ccc44fdSMax Reitz }, 18667ccc44fdSMax Reitz { 18677ccc44fdSMax Reitz .name = "path", 18687ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 18697ccc44fdSMax Reitz .help = "Unix socket path to connect to", 18707ccc44fdSMax Reitz }, 18717ccc44fdSMax Reitz { 18727ccc44fdSMax Reitz .name = "export", 18737ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 18747ccc44fdSMax Reitz .help = "Name of the NBD export to open", 18757ccc44fdSMax Reitz }, 18767ccc44fdSMax Reitz { 18777ccc44fdSMax Reitz .name = "tls-creds", 18787ccc44fdSMax Reitz .type = QEMU_OPT_STRING, 18797ccc44fdSMax Reitz .help = "ID of the TLS credentials to use", 18807ccc44fdSMax Reitz }, 1881216ee365SEric Blake { 1882216ee365SEric Blake .name = "x-dirty-bitmap", 1883216ee365SEric Blake .type = QEMU_OPT_STRING, 1884216ee365SEric Blake .help = "experimental: expose named dirty bitmap in place of " 1885216ee365SEric Blake "block status", 1886216ee365SEric Blake }, 1887b172ae2eSVladimir Sementsov-Ogievskiy { 1888b172ae2eSVladimir Sementsov-Ogievskiy .name = "reconnect-delay", 1889b172ae2eSVladimir Sementsov-Ogievskiy .type = QEMU_OPT_NUMBER, 1890b172ae2eSVladimir Sementsov-Ogievskiy .help = "On an unexpected disconnect, the nbd client tries to " 1891b172ae2eSVladimir Sementsov-Ogievskiy "connect again until succeeding or encountering a serious " 1892b172ae2eSVladimir Sementsov-Ogievskiy "error. During the first @reconnect-delay seconds, all " 1893b172ae2eSVladimir Sementsov-Ogievskiy "requests are paused and will be rerun on a successful " 1894b172ae2eSVladimir Sementsov-Ogievskiy "reconnect. After that time, any delayed requests and all " 1895b172ae2eSVladimir Sementsov-Ogievskiy "future requests before a successful reconnect will " 1896b172ae2eSVladimir Sementsov-Ogievskiy "immediately fail. Default 0", 1897b172ae2eSVladimir Sementsov-Ogievskiy }, 1898c4365735SMurilo Opsfelder Araujo { /* end of list */ } 18997ccc44fdSMax Reitz }, 19007ccc44fdSMax Reitz }; 19017ccc44fdSMax Reitz 19028f071c9dSVladimir Sementsov-Ogievskiy static int nbd_process_options(BlockDriverState *bs, QDict *options, 1903015a1036SMax Reitz Error **errp) 190433897dc7SNick Thomas { 190533897dc7SNick Thomas BDRVNBDState *s = bs->opaque; 19068f071c9dSVladimir Sementsov-Ogievskiy QemuOpts *opts; 190775822a12SDaniel P. Berrange int ret = -EINVAL; 1908ae255e52SPaolo Bonzini 19097ccc44fdSMax Reitz opts = qemu_opts_create(&nbd_runtime_opts, NULL, 0, &error_abort); 1910af175e85SMarkus Armbruster if (!qemu_opts_absorb_qdict(opts, options, errp)) { 19117ccc44fdSMax Reitz goto error; 19127ccc44fdSMax Reitz } 19137ccc44fdSMax Reitz 191462cf396bSMarkus Armbruster /* Translate @host, @port, and @path to a SocketAddress */ 1915491d6c7cSMax Reitz if (!nbd_process_legacy_socket_options(options, opts, errp)) { 191675822a12SDaniel P. Berrange goto error; 191775822a12SDaniel P. Berrange } 191875822a12SDaniel P. Berrange 1919491d6c7cSMax Reitz /* Pop the config into our state object. Exit if invalid. */ 1920491d6c7cSMax Reitz s->saddr = nbd_config(s, options, errp); 1921491d6c7cSMax Reitz if (!s->saddr) { 1922491d6c7cSMax Reitz goto error; 1923491d6c7cSMax Reitz } 1924491d6c7cSMax Reitz 1925491d6c7cSMax Reitz s->export = g_strdup(qemu_opt_get(opts, "export")); 192693676c88SEric Blake if (s->export && strlen(s->export) > NBD_MAX_STRING_SIZE) { 192793676c88SEric Blake error_setg(errp, "export name too long to send to server"); 192893676c88SEric Blake goto error; 192993676c88SEric Blake } 1930491d6c7cSMax Reitz 193103504d05SMax Reitz s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds")); 193203504d05SMax Reitz if (s->tlscredsid) { 19338f071c9dSVladimir Sementsov-Ogievskiy s->tlscreds = nbd_get_tls_creds(s->tlscredsid, errp); 19348f071c9dSVladimir Sementsov-Ogievskiy if (!s->tlscreds) { 193575822a12SDaniel P. Berrange goto error; 193675822a12SDaniel P. Berrange } 193775822a12SDaniel P. Berrange 1938ca0b64e5SMarkus Armbruster /* TODO SOCKET_ADDRESS_KIND_FD where fd has AF_INET or AF_INET6 */ 193962cf396bSMarkus Armbruster if (s->saddr->type != SOCKET_ADDRESS_TYPE_INET) { 194075822a12SDaniel P. Berrange error_setg(errp, "TLS only supported over IP sockets"); 194175822a12SDaniel P. Berrange goto error; 194275822a12SDaniel P. Berrange } 19438f071c9dSVladimir Sementsov-Ogievskiy s->hostname = s->saddr->u.inet.host; 194433897dc7SNick Thomas } 194533897dc7SNick Thomas 19468f071c9dSVladimir Sementsov-Ogievskiy s->x_dirty_bitmap = g_strdup(qemu_opt_get(opts, "x-dirty-bitmap")); 194793676c88SEric Blake if (s->x_dirty_bitmap && strlen(s->x_dirty_bitmap) > NBD_MAX_STRING_SIZE) { 194893676c88SEric Blake error_setg(errp, "x-dirty-bitmap query too long to send to server"); 194993676c88SEric Blake goto error; 195093676c88SEric Blake } 195193676c88SEric Blake 19528f071c9dSVladimir Sementsov-Ogievskiy s->reconnect_delay = qemu_opt_get_number(opts, "reconnect-delay", 0); 19538f071c9dSVladimir Sementsov-Ogievskiy 19548f071c9dSVladimir Sementsov-Ogievskiy ret = 0; 1955d42f78e9SVladimir Sementsov-Ogievskiy 195675822a12SDaniel P. Berrange error: 19577ccc44fdSMax Reitz qemu_opts_del(opts); 195875822a12SDaniel P. Berrange return ret; 1959ae255e52SPaolo Bonzini } 1960d9b09f13SPaolo Bonzini 19618f071c9dSVladimir Sementsov-Ogievskiy static int nbd_open(BlockDriverState *bs, QDict *options, int flags, 19628f071c9dSVladimir Sementsov-Ogievskiy Error **errp) 19638f071c9dSVladimir Sementsov-Ogievskiy { 19648f071c9dSVladimir Sementsov-Ogievskiy int ret; 19658f071c9dSVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 19668f071c9dSVladimir Sementsov-Ogievskiy 19678f071c9dSVladimir Sementsov-Ogievskiy s->bs = bs; 19688f071c9dSVladimir Sementsov-Ogievskiy qemu_co_mutex_init(&s->send_mutex); 19698f071c9dSVladimir Sementsov-Ogievskiy qemu_co_queue_init(&s->free_sema); 19708f071c9dSVladimir Sementsov-Ogievskiy 1971fee091cdSLukas Straub if (!yank_register_instance(BLOCKDEV_YANK_INSTANCE(bs->node_name), errp)) { 1972fee091cdSLukas Straub return -EEXIST; 1973fee091cdSLukas Straub } 1974fee091cdSLukas Straub 1975bbba1c37SVladimir Sementsov-Ogievskiy ret = nbd_process_options(bs, options, errp); 1976bbba1c37SVladimir Sementsov-Ogievskiy if (ret < 0) { 1977bbba1c37SVladimir Sementsov-Ogievskiy goto fail; 1978bbba1c37SVladimir Sementsov-Ogievskiy } 1979bbba1c37SVladimir Sementsov-Ogievskiy 19806d2b0332SVladimir Sementsov-Ogievskiy s->conn = nbd_client_connection_new(s->saddr, true, s->export, 19816d2b0332SVladimir Sementsov-Ogievskiy s->x_dirty_bitmap, s->tlscreds); 1982e8b35bf5SRoman Kagan 1983a71d597bSVladimir Sementsov-Ogievskiy /* TODO: Configurable retry-until-timeout behaviour. */ 1984a71d597bSVladimir Sementsov-Ogievskiy ret = nbd_do_establish_connection(bs, errp); 19858f071c9dSVladimir Sementsov-Ogievskiy if (ret < 0) { 1986bbba1c37SVladimir Sementsov-Ogievskiy goto fail; 19878f071c9dSVladimir Sementsov-Ogievskiy } 19888f071c9dSVladimir Sementsov-Ogievskiy 19898f071c9dSVladimir Sementsov-Ogievskiy s->connection_co = qemu_coroutine_create(nbd_connection_entry, s); 19908f071c9dSVladimir Sementsov-Ogievskiy bdrv_inc_in_flight(bs); 19918f071c9dSVladimir Sementsov-Ogievskiy aio_co_schedule(bdrv_get_aio_context(bs), s->connection_co); 19928f071c9dSVladimir Sementsov-Ogievskiy 19938f071c9dSVladimir Sementsov-Ogievskiy return 0; 1994bbba1c37SVladimir Sementsov-Ogievskiy 1995bbba1c37SVladimir Sementsov-Ogievskiy fail: 1996bbba1c37SVladimir Sementsov-Ogievskiy nbd_clear_bdrvstate(bs); 1997bbba1c37SVladimir Sementsov-Ogievskiy return ret; 19988f071c9dSVladimir Sementsov-Ogievskiy } 19998f071c9dSVladimir Sementsov-Ogievskiy 20001486d04aSPaolo Bonzini static int nbd_co_flush(BlockDriverState *bs) 20011486d04aSPaolo Bonzini { 2002f53a829bSMax Reitz return nbd_client_co_flush(bs); 20031486d04aSPaolo Bonzini } 20041486d04aSPaolo Bonzini 2005fa21e6faSDenis V. Lunev static void nbd_refresh_limits(BlockDriverState *bs, Error **errp) 2006fa21e6faSDenis V. Lunev { 2007611ae1d7SVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 2008fd8d372dSEric Blake uint32_t min = s->info.min_block; 2009081dd1feSEric Blake uint32_t max = MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE, s->info.max_block); 2010081dd1feSEric Blake 20117da537f7SEric Blake /* 20127da537f7SEric Blake * If the server did not advertise an alignment: 20137da537f7SEric Blake * - a size that is not sector-aligned implies that an alignment 20147da537f7SEric Blake * of 1 can be used to access those tail bytes 20157da537f7SEric Blake * - advertisement of block status requires an alignment of 1, so 20167da537f7SEric Blake * that we don't violate block layer constraints that block 20177da537f7SEric Blake * status is always aligned (as we can't control whether the 20187da537f7SEric Blake * server will report sub-sector extents, such as a hole at EOF 20197da537f7SEric Blake * on an unaligned POSIX file) 20207da537f7SEric Blake * - otherwise, assume the server is so old that we are safer avoiding 20217da537f7SEric Blake * sub-sector requests 20227da537f7SEric Blake */ 20237da537f7SEric Blake if (!min) { 20247da537f7SEric Blake min = (!QEMU_IS_ALIGNED(s->info.size, BDRV_SECTOR_SIZE) || 20257da537f7SEric Blake s->info.base_allocation) ? 1 : BDRV_SECTOR_SIZE; 20267da537f7SEric Blake } 20277da537f7SEric Blake 20287da537f7SEric Blake bs->bl.request_alignment = min; 2029714eb0dbSVladimir Sementsov-Ogievskiy bs->bl.max_pdiscard = QEMU_ALIGN_DOWN(INT_MAX, min); 2030081dd1feSEric Blake bs->bl.max_pwrite_zeroes = max; 2031081dd1feSEric Blake bs->bl.max_transfer = max; 2032081dd1feSEric Blake 2033081dd1feSEric Blake if (s->info.opt_block && 2034081dd1feSEric Blake s->info.opt_block > bs->bl.opt_transfer) { 2035081dd1feSEric Blake bs->bl.opt_transfer = s->info.opt_block; 2036081dd1feSEric Blake } 2037fa21e6faSDenis V. Lunev } 2038fa21e6faSDenis V. Lunev 2039019d6b8fSAnthony Liguori static void nbd_close(BlockDriverState *bs) 2040019d6b8fSAnthony Liguori { 2041f53a829bSMax Reitz nbd_client_close(bs); 2042bbba1c37SVladimir Sementsov-Ogievskiy nbd_clear_bdrvstate(bs); 2043019d6b8fSAnthony Liguori } 2044019d6b8fSAnthony Liguori 2045a2b333c0SNir Soffer /* 2046a2b333c0SNir Soffer * NBD cannot truncate, but if the caller asks to truncate to the same size, or 2047a2b333c0SNir Soffer * to a smaller size with exact=false, there is no reason to fail the 2048a2b333c0SNir Soffer * operation. 2049a2b333c0SNir Soffer * 2050a2b333c0SNir Soffer * Preallocation mode is ignored since it does not seems useful to fail when 2051a2b333c0SNir Soffer * we never change anything. 2052a2b333c0SNir Soffer */ 2053a2b333c0SNir Soffer static int coroutine_fn nbd_co_truncate(BlockDriverState *bs, int64_t offset, 2054a2b333c0SNir Soffer bool exact, PreallocMode prealloc, 2055a2b333c0SNir Soffer BdrvRequestFlags flags, Error **errp) 2056a2b333c0SNir Soffer { 2057a2b333c0SNir Soffer BDRVNBDState *s = bs->opaque; 2058a2b333c0SNir Soffer 2059a2b333c0SNir Soffer if (offset != s->info.size && exact) { 2060a2b333c0SNir Soffer error_setg(errp, "Cannot resize NBD nodes"); 2061a2b333c0SNir Soffer return -ENOTSUP; 2062a2b333c0SNir Soffer } 2063a2b333c0SNir Soffer 2064a2b333c0SNir Soffer if (offset > s->info.size) { 2065a2b333c0SNir Soffer error_setg(errp, "Cannot grow NBD nodes"); 2066a2b333c0SNir Soffer return -EINVAL; 2067a2b333c0SNir Soffer } 2068a2b333c0SNir Soffer 2069a2b333c0SNir Soffer return 0; 2070a2b333c0SNir Soffer } 2071a2b333c0SNir Soffer 2072019d6b8fSAnthony Liguori static int64_t nbd_getlength(BlockDriverState *bs) 2073019d6b8fSAnthony Liguori { 2074019d6b8fSAnthony Liguori BDRVNBDState *s = bs->opaque; 2075019d6b8fSAnthony Liguori 2076611ae1d7SVladimir Sementsov-Ogievskiy return s->info.size; 2077019d6b8fSAnthony Liguori } 2078019d6b8fSAnthony Liguori 2079998b3a1eSMax Reitz static void nbd_refresh_filename(BlockDriverState *bs) 20802019d68bSMax Reitz { 208103504d05SMax Reitz BDRVNBDState *s = bs->opaque; 2082491d6c7cSMax Reitz const char *host = NULL, *port = NULL, *path = NULL; 20835c86bdf1SEric Blake size_t len = 0; 2084491d6c7cSMax Reitz 208562cf396bSMarkus Armbruster if (s->saddr->type == SOCKET_ADDRESS_TYPE_INET) { 20869445673eSMarkus Armbruster const InetSocketAddress *inet = &s->saddr->u.inet; 2087491d6c7cSMax Reitz if (!inet->has_ipv4 && !inet->has_ipv6 && !inet->has_to) { 2088491d6c7cSMax Reitz host = inet->host; 2089491d6c7cSMax Reitz port = inet->port; 2090491d6c7cSMax Reitz } 209162cf396bSMarkus Armbruster } else if (s->saddr->type == SOCKET_ADDRESS_TYPE_UNIX) { 20929445673eSMarkus Armbruster path = s->saddr->u.q_unix.path; 20939445673eSMarkus Armbruster } /* else can't represent as pseudo-filename */ 20942019d68bSMax Reitz 2095491d6c7cSMax Reitz if (path && s->export) { 20965c86bdf1SEric Blake len = snprintf(bs->exact_filename, sizeof(bs->exact_filename), 2097491d6c7cSMax Reitz "nbd+unix:///%s?socket=%s", s->export, path); 2098491d6c7cSMax Reitz } else if (path && !s->export) { 20995c86bdf1SEric Blake len = snprintf(bs->exact_filename, sizeof(bs->exact_filename), 2100491d6c7cSMax Reitz "nbd+unix://?socket=%s", path); 2101491d6c7cSMax Reitz } else if (host && s->export) { 21025c86bdf1SEric Blake len = snprintf(bs->exact_filename, sizeof(bs->exact_filename), 2103491d6c7cSMax Reitz "nbd://%s:%s/%s", host, port, s->export); 2104491d6c7cSMax Reitz } else if (host && !s->export) { 21055c86bdf1SEric Blake len = snprintf(bs->exact_filename, sizeof(bs->exact_filename), 2106491d6c7cSMax Reitz "nbd://%s:%s", host, port); 2107ec0de768SMax Reitz } 210800d69986SEric Blake if (len >= sizeof(bs->exact_filename)) { 21095c86bdf1SEric Blake /* Name is too long to represent exactly, so leave it empty. */ 21105c86bdf1SEric Blake bs->exact_filename[0] = '\0'; 21115c86bdf1SEric Blake } 21122019d68bSMax Reitz } 21132019d68bSMax Reitz 21148a6239c0SMax Reitz static char *nbd_dirname(BlockDriverState *bs, Error **errp) 21158a6239c0SMax Reitz { 21168a6239c0SMax Reitz /* The generic bdrv_dirname() implementation is able to work out some 21178a6239c0SMax Reitz * directory name for NBD nodes, but that would be wrong. So far there is no 21188a6239c0SMax Reitz * specification for how "export paths" would work, so NBD does not have 21198a6239c0SMax Reitz * directory names. */ 21208a6239c0SMax Reitz error_setg(errp, "Cannot generate a base directory for NBD nodes"); 21218a6239c0SMax Reitz return NULL; 21228a6239c0SMax Reitz } 21238a6239c0SMax Reitz 21242654267cSMax Reitz static const char *const nbd_strong_runtime_opts[] = { 21252654267cSMax Reitz "path", 21262654267cSMax Reitz "host", 21272654267cSMax Reitz "port", 21282654267cSMax Reitz "export", 21292654267cSMax Reitz "tls-creds", 21302654267cSMax Reitz "server.", 21312654267cSMax Reitz 21322654267cSMax Reitz NULL 21332654267cSMax Reitz }; 21342654267cSMax Reitz 2135c4f7f24eSVladimir Sementsov-Ogievskiy static void nbd_cancel_in_flight(BlockDriverState *bs) 2136c4f7f24eSVladimir Sementsov-Ogievskiy { 2137c4f7f24eSVladimir Sementsov-Ogievskiy BDRVNBDState *s = (BDRVNBDState *)bs->opaque; 2138c4f7f24eSVladimir Sementsov-Ogievskiy 2139c4f7f24eSVladimir Sementsov-Ogievskiy reconnect_delay_timer_del(s); 2140c4f7f24eSVladimir Sementsov-Ogievskiy 2141c4f7f24eSVladimir Sementsov-Ogievskiy if (s->state == NBD_CLIENT_CONNECTING_WAIT) { 2142c4f7f24eSVladimir Sementsov-Ogievskiy s->state = NBD_CLIENT_CONNECTING_NOWAIT; 2143c4f7f24eSVladimir Sementsov-Ogievskiy qemu_co_queue_restart_all(&s->free_sema); 2144c4f7f24eSVladimir Sementsov-Ogievskiy } 2145c4f7f24eSVladimir Sementsov-Ogievskiy } 2146c4f7f24eSVladimir Sementsov-Ogievskiy 2147019d6b8fSAnthony Liguori static BlockDriver bdrv_nbd = { 2148019d6b8fSAnthony Liguori .format_name = "nbd", 21491d7d2a9dSPaolo Bonzini .protocol_name = "nbd", 2150019d6b8fSAnthony Liguori .instance_size = sizeof(BDRVNBDState), 21516963a30dSKevin Wolf .bdrv_parse_filename = nbd_parse_filename, 21525a5e7f8cSMaxim Levitsky .bdrv_co_create_opts = bdrv_co_create_opts_simple, 21535a5e7f8cSMaxim Levitsky .create_opts = &bdrv_create_opts_simple, 215466f82ceeSKevin Wolf .bdrv_file_open = nbd_open, 2155e99754b4SMaxim Levitsky .bdrv_reopen_prepare = nbd_client_reopen_prepare, 215670c4fb26SEric Blake .bdrv_co_preadv = nbd_client_co_preadv, 215770c4fb26SEric Blake .bdrv_co_pwritev = nbd_client_co_pwritev, 2158fa778fffSEric Blake .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes, 2159019d6b8fSAnthony Liguori .bdrv_close = nbd_close, 21601486d04aSPaolo Bonzini .bdrv_co_flush_to_os = nbd_co_flush, 2161447e57c3SEric Blake .bdrv_co_pdiscard = nbd_client_co_pdiscard, 2162fa21e6faSDenis V. Lunev .bdrv_refresh_limits = nbd_refresh_limits, 2163a2b333c0SNir Soffer .bdrv_co_truncate = nbd_co_truncate, 2164019d6b8fSAnthony Liguori .bdrv_getlength = nbd_getlength, 216586f8cdf3SVladimir Sementsov-Ogievskiy .bdrv_detach_aio_context = nbd_client_detach_aio_context, 216686f8cdf3SVladimir Sementsov-Ogievskiy .bdrv_attach_aio_context = nbd_client_attach_aio_context, 2167f7651539SVladimir Sementsov-Ogievskiy .bdrv_co_drain_begin = nbd_client_co_drain_begin, 2168f7651539SVladimir Sementsov-Ogievskiy .bdrv_co_drain_end = nbd_client_co_drain_end, 21692019d68bSMax Reitz .bdrv_refresh_filename = nbd_refresh_filename, 217078a33ab5SVladimir Sementsov-Ogievskiy .bdrv_co_block_status = nbd_client_co_block_status, 21718a6239c0SMax Reitz .bdrv_dirname = nbd_dirname, 21722654267cSMax Reitz .strong_runtime_opts = nbd_strong_runtime_opts, 2173c4f7f24eSVladimir Sementsov-Ogievskiy .bdrv_cancel_in_flight = nbd_cancel_in_flight, 21741d7d2a9dSPaolo Bonzini }; 21751d7d2a9dSPaolo Bonzini 21761d7d2a9dSPaolo Bonzini static BlockDriver bdrv_nbd_tcp = { 21771d7d2a9dSPaolo Bonzini .format_name = "nbd", 21781d7d2a9dSPaolo Bonzini .protocol_name = "nbd+tcp", 21791d7d2a9dSPaolo Bonzini .instance_size = sizeof(BDRVNBDState), 21806963a30dSKevin Wolf .bdrv_parse_filename = nbd_parse_filename, 21815a5e7f8cSMaxim Levitsky .bdrv_co_create_opts = bdrv_co_create_opts_simple, 21825a5e7f8cSMaxim Levitsky .create_opts = &bdrv_create_opts_simple, 21831d7d2a9dSPaolo Bonzini .bdrv_file_open = nbd_open, 2184e99754b4SMaxim Levitsky .bdrv_reopen_prepare = nbd_client_reopen_prepare, 218570c4fb26SEric Blake .bdrv_co_preadv = nbd_client_co_preadv, 218670c4fb26SEric Blake .bdrv_co_pwritev = nbd_client_co_pwritev, 2187fa778fffSEric Blake .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes, 21881d7d2a9dSPaolo Bonzini .bdrv_close = nbd_close, 21891d7d2a9dSPaolo Bonzini .bdrv_co_flush_to_os = nbd_co_flush, 2190447e57c3SEric Blake .bdrv_co_pdiscard = nbd_client_co_pdiscard, 2191fa21e6faSDenis V. Lunev .bdrv_refresh_limits = nbd_refresh_limits, 2192a2b333c0SNir Soffer .bdrv_co_truncate = nbd_co_truncate, 21931d7d2a9dSPaolo Bonzini .bdrv_getlength = nbd_getlength, 219486f8cdf3SVladimir Sementsov-Ogievskiy .bdrv_detach_aio_context = nbd_client_detach_aio_context, 219586f8cdf3SVladimir Sementsov-Ogievskiy .bdrv_attach_aio_context = nbd_client_attach_aio_context, 2196f7651539SVladimir Sementsov-Ogievskiy .bdrv_co_drain_begin = nbd_client_co_drain_begin, 2197f7651539SVladimir Sementsov-Ogievskiy .bdrv_co_drain_end = nbd_client_co_drain_end, 21982019d68bSMax Reitz .bdrv_refresh_filename = nbd_refresh_filename, 219978a33ab5SVladimir Sementsov-Ogievskiy .bdrv_co_block_status = nbd_client_co_block_status, 22008a6239c0SMax Reitz .bdrv_dirname = nbd_dirname, 22012654267cSMax Reitz .strong_runtime_opts = nbd_strong_runtime_opts, 2202c4f7f24eSVladimir Sementsov-Ogievskiy .bdrv_cancel_in_flight = nbd_cancel_in_flight, 22031d7d2a9dSPaolo Bonzini }; 22041d7d2a9dSPaolo Bonzini 22051d7d2a9dSPaolo Bonzini static BlockDriver bdrv_nbd_unix = { 22061d7d2a9dSPaolo Bonzini .format_name = "nbd", 22071d7d2a9dSPaolo Bonzini .protocol_name = "nbd+unix", 22081d7d2a9dSPaolo Bonzini .instance_size = sizeof(BDRVNBDState), 22096963a30dSKevin Wolf .bdrv_parse_filename = nbd_parse_filename, 22105a5e7f8cSMaxim Levitsky .bdrv_co_create_opts = bdrv_co_create_opts_simple, 22115a5e7f8cSMaxim Levitsky .create_opts = &bdrv_create_opts_simple, 22121d7d2a9dSPaolo Bonzini .bdrv_file_open = nbd_open, 2213e99754b4SMaxim Levitsky .bdrv_reopen_prepare = nbd_client_reopen_prepare, 221470c4fb26SEric Blake .bdrv_co_preadv = nbd_client_co_preadv, 221570c4fb26SEric Blake .bdrv_co_pwritev = nbd_client_co_pwritev, 2216fa778fffSEric Blake .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes, 22171d7d2a9dSPaolo Bonzini .bdrv_close = nbd_close, 22181d7d2a9dSPaolo Bonzini .bdrv_co_flush_to_os = nbd_co_flush, 2219447e57c3SEric Blake .bdrv_co_pdiscard = nbd_client_co_pdiscard, 2220fa21e6faSDenis V. Lunev .bdrv_refresh_limits = nbd_refresh_limits, 2221a2b333c0SNir Soffer .bdrv_co_truncate = nbd_co_truncate, 22221d7d2a9dSPaolo Bonzini .bdrv_getlength = nbd_getlength, 222386f8cdf3SVladimir Sementsov-Ogievskiy .bdrv_detach_aio_context = nbd_client_detach_aio_context, 222486f8cdf3SVladimir Sementsov-Ogievskiy .bdrv_attach_aio_context = nbd_client_attach_aio_context, 2225f7651539SVladimir Sementsov-Ogievskiy .bdrv_co_drain_begin = nbd_client_co_drain_begin, 2226f7651539SVladimir Sementsov-Ogievskiy .bdrv_co_drain_end = nbd_client_co_drain_end, 22272019d68bSMax Reitz .bdrv_refresh_filename = nbd_refresh_filename, 222878a33ab5SVladimir Sementsov-Ogievskiy .bdrv_co_block_status = nbd_client_co_block_status, 22298a6239c0SMax Reitz .bdrv_dirname = nbd_dirname, 22302654267cSMax Reitz .strong_runtime_opts = nbd_strong_runtime_opts, 2231c4f7f24eSVladimir Sementsov-Ogievskiy .bdrv_cancel_in_flight = nbd_cancel_in_flight, 2232019d6b8fSAnthony Liguori }; 2233019d6b8fSAnthony Liguori 2234019d6b8fSAnthony Liguori static void bdrv_nbd_init(void) 2235019d6b8fSAnthony Liguori { 2236019d6b8fSAnthony Liguori bdrv_register(&bdrv_nbd); 22371d7d2a9dSPaolo Bonzini bdrv_register(&bdrv_nbd_tcp); 22381d7d2a9dSPaolo Bonzini bdrv_register(&bdrv_nbd_unix); 2239019d6b8fSAnthony Liguori } 2240019d6b8fSAnthony Liguori 2241019d6b8fSAnthony Liguori block_init(bdrv_nbd_init); 2242