xref: /openbmc/qemu/block/nbd.c (revision 7f493662)
1019d6b8fSAnthony Liguori /*
2019d6b8fSAnthony Liguori  * QEMU Block driver for  NBD
3019d6b8fSAnthony Liguori  *
4f7651539SVladimir Sementsov-Ogievskiy  * Copyright (c) 2019 Virtuozzo International GmbH.
586f8cdf3SVladimir Sementsov-Ogievskiy  * Copyright (C) 2016 Red Hat, Inc.
6019d6b8fSAnthony Liguori  * Copyright (C) 2008 Bull S.A.S.
7019d6b8fSAnthony Liguori  *     Author: Laurent Vivier <Laurent.Vivier@bull.net>
8019d6b8fSAnthony Liguori  *
9019d6b8fSAnthony Liguori  * Some parts:
10019d6b8fSAnthony Liguori  *    Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
11019d6b8fSAnthony Liguori  *
12019d6b8fSAnthony Liguori  * Permission is hereby granted, free of charge, to any person obtaining a copy
13019d6b8fSAnthony Liguori  * of this software and associated documentation files (the "Software"), to deal
14019d6b8fSAnthony Liguori  * in the Software without restriction, including without limitation the rights
15019d6b8fSAnthony Liguori  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16019d6b8fSAnthony Liguori  * copies of the Software, and to permit persons to whom the Software is
17019d6b8fSAnthony Liguori  * furnished to do so, subject to the following conditions:
18019d6b8fSAnthony Liguori  *
19019d6b8fSAnthony Liguori  * The above copyright notice and this permission notice shall be included in
20019d6b8fSAnthony Liguori  * all copies or substantial portions of the Software.
21019d6b8fSAnthony Liguori  *
22019d6b8fSAnthony Liguori  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23019d6b8fSAnthony Liguori  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24019d6b8fSAnthony Liguori  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25019d6b8fSAnthony Liguori  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26019d6b8fSAnthony Liguori  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27019d6b8fSAnthony Liguori  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28019d6b8fSAnthony Liguori  * THE SOFTWARE.
29019d6b8fSAnthony Liguori  */
30019d6b8fSAnthony Liguori 
3180c71a24SPeter Maydell #include "qemu/osdep.h"
3286f8cdf3SVladimir Sementsov-Ogievskiy 
3386f8cdf3SVladimir Sementsov-Ogievskiy #include "trace.h"
341de7afc9SPaolo Bonzini #include "qemu/uri.h"
35922a01a0SMarkus Armbruster #include "qemu/option.h"
36f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
37db725815SMarkus Armbruster #include "qemu/main-loop.h"
38019d6b8fSAnthony Liguori 
3986f8cdf3SVladimir Sementsov-Ogievskiy #include "qapi/qapi-visit-sockets.h"
4086f8cdf3SVladimir Sementsov-Ogievskiy #include "qapi/qmp/qstring.h"
4186f8cdf3SVladimir Sementsov-Ogievskiy 
4286f8cdf3SVladimir Sementsov-Ogievskiy #include "block/qdict.h"
4386f8cdf3SVladimir Sementsov-Ogievskiy #include "block/nbd.h"
4486f8cdf3SVladimir Sementsov-Ogievskiy #include "block/block_int.h"
4586f8cdf3SVladimir Sementsov-Ogievskiy 
461d45f8b5SLaurent Vivier #define EN_OPTSTR ":exportname="
4786f8cdf3SVladimir Sementsov-Ogievskiy #define MAX_NBD_REQUESTS    16
4886f8cdf3SVladimir Sementsov-Ogievskiy 
4986f8cdf3SVladimir Sementsov-Ogievskiy #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ (uint64_t)(intptr_t)(bs))
5086f8cdf3SVladimir Sementsov-Ogievskiy #define INDEX_TO_HANDLE(bs, index)  ((index)  ^ (uint64_t)(intptr_t)(bs))
5186f8cdf3SVladimir Sementsov-Ogievskiy 
5286f8cdf3SVladimir Sementsov-Ogievskiy typedef struct {
5386f8cdf3SVladimir Sementsov-Ogievskiy     Coroutine *coroutine;
5486f8cdf3SVladimir Sementsov-Ogievskiy     uint64_t offset;        /* original offset of the request */
5586f8cdf3SVladimir Sementsov-Ogievskiy     bool receiving;         /* waiting for connection_co? */
5686f8cdf3SVladimir Sementsov-Ogievskiy } NBDClientRequest;
5786f8cdf3SVladimir Sementsov-Ogievskiy 
58a34b1e5eSVladimir Sementsov-Ogievskiy typedef enum NBDClientState {
59f7651539SVladimir Sementsov-Ogievskiy     NBD_CLIENT_CONNECTING_WAIT,
60f7651539SVladimir Sementsov-Ogievskiy     NBD_CLIENT_CONNECTING_NOWAIT,
61a34b1e5eSVladimir Sementsov-Ogievskiy     NBD_CLIENT_CONNECTED,
62a34b1e5eSVladimir Sementsov-Ogievskiy     NBD_CLIENT_QUIT
63a34b1e5eSVladimir Sementsov-Ogievskiy } NBDClientState;
64a34b1e5eSVladimir Sementsov-Ogievskiy 
65611ae1d7SVladimir Sementsov-Ogievskiy typedef struct BDRVNBDState {
6686f8cdf3SVladimir Sementsov-Ogievskiy     QIOChannelSocket *sioc; /* The master data channel */
6786f8cdf3SVladimir Sementsov-Ogievskiy     QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */
6886f8cdf3SVladimir Sementsov-Ogievskiy     NBDExportInfo info;
6986f8cdf3SVladimir Sementsov-Ogievskiy 
7086f8cdf3SVladimir Sementsov-Ogievskiy     CoMutex send_mutex;
7186f8cdf3SVladimir Sementsov-Ogievskiy     CoQueue free_sema;
7286f8cdf3SVladimir Sementsov-Ogievskiy     Coroutine *connection_co;
7378c81a3fSMax Reitz     Coroutine *teardown_co;
74f7651539SVladimir Sementsov-Ogievskiy     QemuCoSleepState *connection_co_sleep_ns_state;
75f7651539SVladimir Sementsov-Ogievskiy     bool drained;
76f7651539SVladimir Sementsov-Ogievskiy     bool wait_drained_end;
7786f8cdf3SVladimir Sementsov-Ogievskiy     int in_flight;
78a34b1e5eSVladimir Sementsov-Ogievskiy     NBDClientState state;
79f7651539SVladimir Sementsov-Ogievskiy     int connect_status;
80f7651539SVladimir Sementsov-Ogievskiy     Error *connect_err;
81f7651539SVladimir Sementsov-Ogievskiy     bool wait_in_flight;
8286f8cdf3SVladimir Sementsov-Ogievskiy 
8386f8cdf3SVladimir Sementsov-Ogievskiy     NBDClientRequest requests[MAX_NBD_REQUESTS];
8486f8cdf3SVladimir Sementsov-Ogievskiy     NBDReply reply;
8586f8cdf3SVladimir Sementsov-Ogievskiy     BlockDriverState *bs;
8603504d05SMax Reitz 
878f071c9dSVladimir Sementsov-Ogievskiy     /* Connection parameters */
888f071c9dSVladimir Sementsov-Ogievskiy     uint32_t reconnect_delay;
8962cf396bSMarkus Armbruster     SocketAddress *saddr;
90491d6c7cSMax Reitz     char *export, *tlscredsid;
918f071c9dSVladimir Sementsov-Ogievskiy     QCryptoTLSCreds *tlscreds;
928f071c9dSVladimir Sementsov-Ogievskiy     const char *hostname;
938f071c9dSVladimir Sementsov-Ogievskiy     char *x_dirty_bitmap;
94019d6b8fSAnthony Liguori } BDRVNBDState;
95019d6b8fSAnthony Liguori 
96f7651539SVladimir Sementsov-Ogievskiy static int nbd_client_connect(BlockDriverState *bs, Error **errp);
97f7651539SVladimir Sementsov-Ogievskiy 
98*7f493662SPan Nengyuan static void nbd_clear_bdrvstate(BDRVNBDState *s)
99*7f493662SPan Nengyuan {
100*7f493662SPan Nengyuan     object_unref(OBJECT(s->tlscreds));
101*7f493662SPan Nengyuan     qapi_free_SocketAddress(s->saddr);
102*7f493662SPan Nengyuan     s->saddr = NULL;
103*7f493662SPan Nengyuan     g_free(s->export);
104*7f493662SPan Nengyuan     s->export = NULL;
105*7f493662SPan Nengyuan     g_free(s->tlscredsid);
106*7f493662SPan Nengyuan     s->tlscredsid = NULL;
107*7f493662SPan Nengyuan     g_free(s->x_dirty_bitmap);
108*7f493662SPan Nengyuan     s->x_dirty_bitmap = NULL;
109*7f493662SPan Nengyuan }
110*7f493662SPan Nengyuan 
111a34b1e5eSVladimir Sementsov-Ogievskiy static void nbd_channel_error(BDRVNBDState *s, int ret)
112a34b1e5eSVladimir Sementsov-Ogievskiy {
113f7651539SVladimir Sementsov-Ogievskiy     if (ret == -EIO) {
114f7651539SVladimir Sementsov-Ogievskiy         if (s->state == NBD_CLIENT_CONNECTED) {
115f7651539SVladimir Sementsov-Ogievskiy             s->state = s->reconnect_delay ? NBD_CLIENT_CONNECTING_WAIT :
116f7651539SVladimir Sementsov-Ogievskiy                                             NBD_CLIENT_CONNECTING_NOWAIT;
117f7651539SVladimir Sementsov-Ogievskiy         }
118f7651539SVladimir Sementsov-Ogievskiy     } else {
119f7651539SVladimir Sementsov-Ogievskiy         if (s->state == NBD_CLIENT_CONNECTED) {
120f7651539SVladimir Sementsov-Ogievskiy             qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
121f7651539SVladimir Sementsov-Ogievskiy         }
122a34b1e5eSVladimir Sementsov-Ogievskiy         s->state = NBD_CLIENT_QUIT;
123a34b1e5eSVladimir Sementsov-Ogievskiy     }
124f7651539SVladimir Sementsov-Ogievskiy }
125a34b1e5eSVladimir Sementsov-Ogievskiy 
126611ae1d7SVladimir Sementsov-Ogievskiy static void nbd_recv_coroutines_wake_all(BDRVNBDState *s)
12786f8cdf3SVladimir Sementsov-Ogievskiy {
12886f8cdf3SVladimir Sementsov-Ogievskiy     int i;
12986f8cdf3SVladimir Sementsov-Ogievskiy 
13086f8cdf3SVladimir Sementsov-Ogievskiy     for (i = 0; i < MAX_NBD_REQUESTS; i++) {
13186f8cdf3SVladimir Sementsov-Ogievskiy         NBDClientRequest *req = &s->requests[i];
13286f8cdf3SVladimir Sementsov-Ogievskiy 
13386f8cdf3SVladimir Sementsov-Ogievskiy         if (req->coroutine && req->receiving) {
13486f8cdf3SVladimir Sementsov-Ogievskiy             aio_co_wake(req->coroutine);
13586f8cdf3SVladimir Sementsov-Ogievskiy         }
13686f8cdf3SVladimir Sementsov-Ogievskiy     }
13786f8cdf3SVladimir Sementsov-Ogievskiy }
13886f8cdf3SVladimir Sementsov-Ogievskiy 
13986f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_client_detach_aio_context(BlockDriverState *bs)
14086f8cdf3SVladimir Sementsov-Ogievskiy {
141611ae1d7SVladimir Sementsov-Ogievskiy     BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
142611ae1d7SVladimir Sementsov-Ogievskiy 
143611ae1d7SVladimir Sementsov-Ogievskiy     qio_channel_detach_aio_context(QIO_CHANNEL(s->ioc));
14486f8cdf3SVladimir Sementsov-Ogievskiy }
14586f8cdf3SVladimir Sementsov-Ogievskiy 
14686f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_client_attach_aio_context_bh(void *opaque)
14786f8cdf3SVladimir Sementsov-Ogievskiy {
14886f8cdf3SVladimir Sementsov-Ogievskiy     BlockDriverState *bs = opaque;
149611ae1d7SVladimir Sementsov-Ogievskiy     BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
15086f8cdf3SVladimir Sementsov-Ogievskiy 
15186f8cdf3SVladimir Sementsov-Ogievskiy     /*
15286f8cdf3SVladimir Sementsov-Ogievskiy      * The node is still drained, so we know the coroutine has yielded in
15386f8cdf3SVladimir Sementsov-Ogievskiy      * nbd_read_eof(), the only place where bs->in_flight can reach 0, or it is
15486f8cdf3SVladimir Sementsov-Ogievskiy      * entered for the first time. Both places are safe for entering the
15586f8cdf3SVladimir Sementsov-Ogievskiy      * coroutine.
15686f8cdf3SVladimir Sementsov-Ogievskiy      */
157611ae1d7SVladimir Sementsov-Ogievskiy     qemu_aio_coroutine_enter(bs->aio_context, s->connection_co);
15886f8cdf3SVladimir Sementsov-Ogievskiy     bdrv_dec_in_flight(bs);
15986f8cdf3SVladimir Sementsov-Ogievskiy }
16086f8cdf3SVladimir Sementsov-Ogievskiy 
16186f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_client_attach_aio_context(BlockDriverState *bs,
16286f8cdf3SVladimir Sementsov-Ogievskiy                                           AioContext *new_context)
16386f8cdf3SVladimir Sementsov-Ogievskiy {
164611ae1d7SVladimir Sementsov-Ogievskiy     BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
165611ae1d7SVladimir Sementsov-Ogievskiy 
166f7651539SVladimir Sementsov-Ogievskiy     /*
167f7651539SVladimir Sementsov-Ogievskiy      * s->connection_co is either yielded from nbd_receive_reply or from
168f7651539SVladimir Sementsov-Ogievskiy      * nbd_co_reconnect_loop()
169f7651539SVladimir Sementsov-Ogievskiy      */
170f7651539SVladimir Sementsov-Ogievskiy     if (s->state == NBD_CLIENT_CONNECTED) {
171611ae1d7SVladimir Sementsov-Ogievskiy         qio_channel_attach_aio_context(QIO_CHANNEL(s->ioc), new_context);
172f7651539SVladimir Sementsov-Ogievskiy     }
17386f8cdf3SVladimir Sementsov-Ogievskiy 
17486f8cdf3SVladimir Sementsov-Ogievskiy     bdrv_inc_in_flight(bs);
17586f8cdf3SVladimir Sementsov-Ogievskiy 
17686f8cdf3SVladimir Sementsov-Ogievskiy     /*
17786f8cdf3SVladimir Sementsov-Ogievskiy      * Need to wait here for the BH to run because the BH must run while the
17886f8cdf3SVladimir Sementsov-Ogievskiy      * node is still drained.
17986f8cdf3SVladimir Sementsov-Ogievskiy      */
18086f8cdf3SVladimir Sementsov-Ogievskiy     aio_wait_bh_oneshot(new_context, nbd_client_attach_aio_context_bh, bs);
18186f8cdf3SVladimir Sementsov-Ogievskiy }
18286f8cdf3SVladimir Sementsov-Ogievskiy 
183f7651539SVladimir Sementsov-Ogievskiy static void coroutine_fn nbd_client_co_drain_begin(BlockDriverState *bs)
184f7651539SVladimir Sementsov-Ogievskiy {
185f7651539SVladimir Sementsov-Ogievskiy     BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
186f7651539SVladimir Sementsov-Ogievskiy 
187f7651539SVladimir Sementsov-Ogievskiy     s->drained = true;
188f7651539SVladimir Sementsov-Ogievskiy     if (s->connection_co_sleep_ns_state) {
189f7651539SVladimir Sementsov-Ogievskiy         qemu_co_sleep_wake(s->connection_co_sleep_ns_state);
190f7651539SVladimir Sementsov-Ogievskiy     }
191f7651539SVladimir Sementsov-Ogievskiy }
192f7651539SVladimir Sementsov-Ogievskiy 
193f7651539SVladimir Sementsov-Ogievskiy static void coroutine_fn nbd_client_co_drain_end(BlockDriverState *bs)
194f7651539SVladimir Sementsov-Ogievskiy {
195f7651539SVladimir Sementsov-Ogievskiy     BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
196f7651539SVladimir Sementsov-Ogievskiy 
197f7651539SVladimir Sementsov-Ogievskiy     s->drained = false;
198f7651539SVladimir Sementsov-Ogievskiy     if (s->wait_drained_end) {
199f7651539SVladimir Sementsov-Ogievskiy         s->wait_drained_end = false;
200f7651539SVladimir Sementsov-Ogievskiy         aio_co_wake(s->connection_co);
201f7651539SVladimir Sementsov-Ogievskiy     }
202f7651539SVladimir Sementsov-Ogievskiy }
203f7651539SVladimir Sementsov-Ogievskiy 
20486f8cdf3SVladimir Sementsov-Ogievskiy 
20586f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_teardown_connection(BlockDriverState *bs)
20686f8cdf3SVladimir Sementsov-Ogievskiy {
207611ae1d7SVladimir Sementsov-Ogievskiy     BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
20886f8cdf3SVladimir Sementsov-Ogievskiy 
209f7651539SVladimir Sementsov-Ogievskiy     if (s->state == NBD_CLIENT_CONNECTED) {
21086f8cdf3SVladimir Sementsov-Ogievskiy         /* finish any pending coroutines */
211f7651539SVladimir Sementsov-Ogievskiy         assert(s->ioc);
212f7651539SVladimir Sementsov-Ogievskiy         qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
213f7651539SVladimir Sementsov-Ogievskiy     }
214f7651539SVladimir Sementsov-Ogievskiy     s->state = NBD_CLIENT_QUIT;
215f7651539SVladimir Sementsov-Ogievskiy     if (s->connection_co) {
216f7651539SVladimir Sementsov-Ogievskiy         if (s->connection_co_sleep_ns_state) {
217f7651539SVladimir Sementsov-Ogievskiy             qemu_co_sleep_wake(s->connection_co_sleep_ns_state);
218f7651539SVladimir Sementsov-Ogievskiy         }
219f7651539SVladimir Sementsov-Ogievskiy     }
22078c81a3fSMax Reitz     if (qemu_in_coroutine()) {
22178c81a3fSMax Reitz         s->teardown_co = qemu_coroutine_self();
22278c81a3fSMax Reitz         /* connection_co resumes us when it terminates */
22378c81a3fSMax Reitz         qemu_coroutine_yield();
22478c81a3fSMax Reitz         s->teardown_co = NULL;
22578c81a3fSMax Reitz     } else {
226611ae1d7SVladimir Sementsov-Ogievskiy         BDRV_POLL_WHILE(bs, s->connection_co);
227f7651539SVladimir Sementsov-Ogievskiy     }
22878c81a3fSMax Reitz     assert(!s->connection_co);
22978c81a3fSMax Reitz }
23086f8cdf3SVladimir Sementsov-Ogievskiy 
231f7651539SVladimir Sementsov-Ogievskiy static bool nbd_client_connecting(BDRVNBDState *s)
232f7651539SVladimir Sementsov-Ogievskiy {
233f7651539SVladimir Sementsov-Ogievskiy     return s->state == NBD_CLIENT_CONNECTING_WAIT ||
234f7651539SVladimir Sementsov-Ogievskiy         s->state == NBD_CLIENT_CONNECTING_NOWAIT;
235f7651539SVladimir Sementsov-Ogievskiy }
236f7651539SVladimir Sementsov-Ogievskiy 
237f7651539SVladimir Sementsov-Ogievskiy static bool nbd_client_connecting_wait(BDRVNBDState *s)
238f7651539SVladimir Sementsov-Ogievskiy {
239f7651539SVladimir Sementsov-Ogievskiy     return s->state == NBD_CLIENT_CONNECTING_WAIT;
240f7651539SVladimir Sementsov-Ogievskiy }
241f7651539SVladimir Sementsov-Ogievskiy 
242f7651539SVladimir Sementsov-Ogievskiy static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s)
243f7651539SVladimir Sementsov-Ogievskiy {
244f7651539SVladimir Sementsov-Ogievskiy     Error *local_err = NULL;
245f7651539SVladimir Sementsov-Ogievskiy 
246f7651539SVladimir Sementsov-Ogievskiy     if (!nbd_client_connecting(s)) {
247f7651539SVladimir Sementsov-Ogievskiy         return;
248f7651539SVladimir Sementsov-Ogievskiy     }
249f7651539SVladimir Sementsov-Ogievskiy 
250f7651539SVladimir Sementsov-Ogievskiy     /* Wait for completion of all in-flight requests */
251f7651539SVladimir Sementsov-Ogievskiy 
252f7651539SVladimir Sementsov-Ogievskiy     qemu_co_mutex_lock(&s->send_mutex);
253f7651539SVladimir Sementsov-Ogievskiy 
254f7651539SVladimir Sementsov-Ogievskiy     while (s->in_flight > 0) {
255f7651539SVladimir Sementsov-Ogievskiy         qemu_co_mutex_unlock(&s->send_mutex);
256f7651539SVladimir Sementsov-Ogievskiy         nbd_recv_coroutines_wake_all(s);
257f7651539SVladimir Sementsov-Ogievskiy         s->wait_in_flight = true;
258f7651539SVladimir Sementsov-Ogievskiy         qemu_coroutine_yield();
259f7651539SVladimir Sementsov-Ogievskiy         s->wait_in_flight = false;
260f7651539SVladimir Sementsov-Ogievskiy         qemu_co_mutex_lock(&s->send_mutex);
261f7651539SVladimir Sementsov-Ogievskiy     }
262f7651539SVladimir Sementsov-Ogievskiy 
263f7651539SVladimir Sementsov-Ogievskiy     qemu_co_mutex_unlock(&s->send_mutex);
264f7651539SVladimir Sementsov-Ogievskiy 
265f7651539SVladimir Sementsov-Ogievskiy     if (!nbd_client_connecting(s)) {
266f7651539SVladimir Sementsov-Ogievskiy         return;
267f7651539SVladimir Sementsov-Ogievskiy     }
268f7651539SVladimir Sementsov-Ogievskiy 
269f7651539SVladimir Sementsov-Ogievskiy     /*
270f7651539SVladimir Sementsov-Ogievskiy      * Now we are sure that nobody is accessing the channel, and no one will
271f7651539SVladimir Sementsov-Ogievskiy      * try until we set the state to CONNECTED.
272f7651539SVladimir Sementsov-Ogievskiy      */
273f7651539SVladimir Sementsov-Ogievskiy 
274f7651539SVladimir Sementsov-Ogievskiy     /* Finalize previous connection if any */
275f7651539SVladimir Sementsov-Ogievskiy     if (s->ioc) {
276f7651539SVladimir Sementsov-Ogievskiy         nbd_client_detach_aio_context(s->bs);
277611ae1d7SVladimir Sementsov-Ogievskiy         object_unref(OBJECT(s->sioc));
278611ae1d7SVladimir Sementsov-Ogievskiy         s->sioc = NULL;
279611ae1d7SVladimir Sementsov-Ogievskiy         object_unref(OBJECT(s->ioc));
280611ae1d7SVladimir Sementsov-Ogievskiy         s->ioc = NULL;
28186f8cdf3SVladimir Sementsov-Ogievskiy     }
28286f8cdf3SVladimir Sementsov-Ogievskiy 
283f7651539SVladimir Sementsov-Ogievskiy     s->connect_status = nbd_client_connect(s->bs, &local_err);
284f7651539SVladimir Sementsov-Ogievskiy     error_free(s->connect_err);
285f7651539SVladimir Sementsov-Ogievskiy     s->connect_err = NULL;
286f7651539SVladimir Sementsov-Ogievskiy     error_propagate(&s->connect_err, local_err);
287f7651539SVladimir Sementsov-Ogievskiy 
288f7651539SVladimir Sementsov-Ogievskiy     if (s->connect_status < 0) {
289f7651539SVladimir Sementsov-Ogievskiy         /* failed attempt */
290f7651539SVladimir Sementsov-Ogievskiy         return;
291f7651539SVladimir Sementsov-Ogievskiy     }
292f7651539SVladimir Sementsov-Ogievskiy 
293f7651539SVladimir Sementsov-Ogievskiy     /* successfully connected */
294f7651539SVladimir Sementsov-Ogievskiy     s->state = NBD_CLIENT_CONNECTED;
295f7651539SVladimir Sementsov-Ogievskiy     qemu_co_queue_restart_all(&s->free_sema);
296f7651539SVladimir Sementsov-Ogievskiy }
297f7651539SVladimir Sementsov-Ogievskiy 
298f7651539SVladimir Sementsov-Ogievskiy static coroutine_fn void nbd_co_reconnect_loop(BDRVNBDState *s)
299f7651539SVladimir Sementsov-Ogievskiy {
300f7651539SVladimir Sementsov-Ogievskiy     uint64_t start_time_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
301f7651539SVladimir Sementsov-Ogievskiy     uint64_t delay_ns = s->reconnect_delay * NANOSECONDS_PER_SECOND;
302f7651539SVladimir Sementsov-Ogievskiy     uint64_t timeout = 1 * NANOSECONDS_PER_SECOND;
303f7651539SVladimir Sementsov-Ogievskiy     uint64_t max_timeout = 16 * NANOSECONDS_PER_SECOND;
304f7651539SVladimir Sementsov-Ogievskiy 
305f7651539SVladimir Sementsov-Ogievskiy     nbd_reconnect_attempt(s);
306f7651539SVladimir Sementsov-Ogievskiy 
307f7651539SVladimir Sementsov-Ogievskiy     while (nbd_client_connecting(s)) {
308f7651539SVladimir Sementsov-Ogievskiy         if (s->state == NBD_CLIENT_CONNECTING_WAIT &&
309f7651539SVladimir Sementsov-Ogievskiy             qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start_time_ns > delay_ns)
310f7651539SVladimir Sementsov-Ogievskiy         {
311f7651539SVladimir Sementsov-Ogievskiy             s->state = NBD_CLIENT_CONNECTING_NOWAIT;
312f7651539SVladimir Sementsov-Ogievskiy             qemu_co_queue_restart_all(&s->free_sema);
313f7651539SVladimir Sementsov-Ogievskiy         }
314f7651539SVladimir Sementsov-Ogievskiy 
315f7651539SVladimir Sementsov-Ogievskiy         qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME, timeout,
316f7651539SVladimir Sementsov-Ogievskiy                                   &s->connection_co_sleep_ns_state);
317f7651539SVladimir Sementsov-Ogievskiy         if (s->drained) {
318f7651539SVladimir Sementsov-Ogievskiy             bdrv_dec_in_flight(s->bs);
319f7651539SVladimir Sementsov-Ogievskiy             s->wait_drained_end = true;
320f7651539SVladimir Sementsov-Ogievskiy             while (s->drained) {
321f7651539SVladimir Sementsov-Ogievskiy                 /*
322f7651539SVladimir Sementsov-Ogievskiy                  * We may be entered once from nbd_client_attach_aio_context_bh
323f7651539SVladimir Sementsov-Ogievskiy                  * and then from nbd_client_co_drain_end. So here is a loop.
324f7651539SVladimir Sementsov-Ogievskiy                  */
325f7651539SVladimir Sementsov-Ogievskiy                 qemu_coroutine_yield();
326f7651539SVladimir Sementsov-Ogievskiy             }
327f7651539SVladimir Sementsov-Ogievskiy             bdrv_inc_in_flight(s->bs);
328f7651539SVladimir Sementsov-Ogievskiy         }
329f7651539SVladimir Sementsov-Ogievskiy         if (timeout < max_timeout) {
330f7651539SVladimir Sementsov-Ogievskiy             timeout *= 2;
331f7651539SVladimir Sementsov-Ogievskiy         }
332f7651539SVladimir Sementsov-Ogievskiy 
333f7651539SVladimir Sementsov-Ogievskiy         nbd_reconnect_attempt(s);
334f7651539SVladimir Sementsov-Ogievskiy     }
335f7651539SVladimir Sementsov-Ogievskiy }
336f7651539SVladimir Sementsov-Ogievskiy 
33786f8cdf3SVladimir Sementsov-Ogievskiy static coroutine_fn void nbd_connection_entry(void *opaque)
33886f8cdf3SVladimir Sementsov-Ogievskiy {
339611ae1d7SVladimir Sementsov-Ogievskiy     BDRVNBDState *s = opaque;
34086f8cdf3SVladimir Sementsov-Ogievskiy     uint64_t i;
34186f8cdf3SVladimir Sementsov-Ogievskiy     int ret = 0;
34286f8cdf3SVladimir Sementsov-Ogievskiy     Error *local_err = NULL;
34386f8cdf3SVladimir Sementsov-Ogievskiy 
344a34b1e5eSVladimir Sementsov-Ogievskiy     while (s->state != NBD_CLIENT_QUIT) {
34586f8cdf3SVladimir Sementsov-Ogievskiy         /*
34686f8cdf3SVladimir Sementsov-Ogievskiy          * The NBD client can only really be considered idle when it has
34786f8cdf3SVladimir Sementsov-Ogievskiy          * yielded from qio_channel_readv_all_eof(), waiting for data. This is
34886f8cdf3SVladimir Sementsov-Ogievskiy          * the point where the additional scheduled coroutine entry happens
34986f8cdf3SVladimir Sementsov-Ogievskiy          * after nbd_client_attach_aio_context().
35086f8cdf3SVladimir Sementsov-Ogievskiy          *
35186f8cdf3SVladimir Sementsov-Ogievskiy          * Therefore we keep an additional in_flight reference all the time and
35286f8cdf3SVladimir Sementsov-Ogievskiy          * only drop it temporarily here.
35386f8cdf3SVladimir Sementsov-Ogievskiy          */
354f7651539SVladimir Sementsov-Ogievskiy 
355f7651539SVladimir Sementsov-Ogievskiy         if (nbd_client_connecting(s)) {
356f7651539SVladimir Sementsov-Ogievskiy             nbd_co_reconnect_loop(s);
357f7651539SVladimir Sementsov-Ogievskiy         }
358f7651539SVladimir Sementsov-Ogievskiy 
359f7651539SVladimir Sementsov-Ogievskiy         if (s->state != NBD_CLIENT_CONNECTED) {
360f7651539SVladimir Sementsov-Ogievskiy             continue;
361f7651539SVladimir Sementsov-Ogievskiy         }
362f7651539SVladimir Sementsov-Ogievskiy 
36386f8cdf3SVladimir Sementsov-Ogievskiy         assert(s->reply.handle == 0);
36486f8cdf3SVladimir Sementsov-Ogievskiy         ret = nbd_receive_reply(s->bs, s->ioc, &s->reply, &local_err);
36586f8cdf3SVladimir Sementsov-Ogievskiy 
36686f8cdf3SVladimir Sementsov-Ogievskiy         if (local_err) {
36786f8cdf3SVladimir Sementsov-Ogievskiy             trace_nbd_read_reply_entry_fail(ret, error_get_pretty(local_err));
36886f8cdf3SVladimir Sementsov-Ogievskiy             error_free(local_err);
369f7651539SVladimir Sementsov-Ogievskiy             local_err = NULL;
37086f8cdf3SVladimir Sementsov-Ogievskiy         }
37186f8cdf3SVladimir Sementsov-Ogievskiy         if (ret <= 0) {
372a34b1e5eSVladimir Sementsov-Ogievskiy             nbd_channel_error(s, ret ? ret : -EIO);
373f7651539SVladimir Sementsov-Ogievskiy             continue;
37486f8cdf3SVladimir Sementsov-Ogievskiy         }
37586f8cdf3SVladimir Sementsov-Ogievskiy 
37686f8cdf3SVladimir Sementsov-Ogievskiy         /*
37786f8cdf3SVladimir Sementsov-Ogievskiy          * There's no need for a mutex on the receive side, because the
37886f8cdf3SVladimir Sementsov-Ogievskiy          * handler acts as a synchronization point and ensures that only
37986f8cdf3SVladimir Sementsov-Ogievskiy          * one coroutine is called until the reply finishes.
38086f8cdf3SVladimir Sementsov-Ogievskiy          */
38186f8cdf3SVladimir Sementsov-Ogievskiy         i = HANDLE_TO_INDEX(s, s->reply.handle);
38286f8cdf3SVladimir Sementsov-Ogievskiy         if (i >= MAX_NBD_REQUESTS ||
38386f8cdf3SVladimir Sementsov-Ogievskiy             !s->requests[i].coroutine ||
38486f8cdf3SVladimir Sementsov-Ogievskiy             !s->requests[i].receiving ||
38586f8cdf3SVladimir Sementsov-Ogievskiy             (nbd_reply_is_structured(&s->reply) && !s->info.structured_reply))
38686f8cdf3SVladimir Sementsov-Ogievskiy         {
387a34b1e5eSVladimir Sementsov-Ogievskiy             nbd_channel_error(s, -EINVAL);
388f7651539SVladimir Sementsov-Ogievskiy             continue;
38986f8cdf3SVladimir Sementsov-Ogievskiy         }
39086f8cdf3SVladimir Sementsov-Ogievskiy 
39186f8cdf3SVladimir Sementsov-Ogievskiy         /*
39286f8cdf3SVladimir Sementsov-Ogievskiy          * We're woken up again by the request itself.  Note that there
39386f8cdf3SVladimir Sementsov-Ogievskiy          * is no race between yielding and reentering connection_co.  This
39486f8cdf3SVladimir Sementsov-Ogievskiy          * is because:
39586f8cdf3SVladimir Sementsov-Ogievskiy          *
39686f8cdf3SVladimir Sementsov-Ogievskiy          * - if the request runs on the same AioContext, it is only
39786f8cdf3SVladimir Sementsov-Ogievskiy          *   entered after we yield
39886f8cdf3SVladimir Sementsov-Ogievskiy          *
39986f8cdf3SVladimir Sementsov-Ogievskiy          * - if the request runs on a different AioContext, reentering
40086f8cdf3SVladimir Sementsov-Ogievskiy          *   connection_co happens through a bottom half, which can only
40186f8cdf3SVladimir Sementsov-Ogievskiy          *   run after we yield.
40286f8cdf3SVladimir Sementsov-Ogievskiy          */
40386f8cdf3SVladimir Sementsov-Ogievskiy         aio_co_wake(s->requests[i].coroutine);
40486f8cdf3SVladimir Sementsov-Ogievskiy         qemu_coroutine_yield();
40586f8cdf3SVladimir Sementsov-Ogievskiy     }
40686f8cdf3SVladimir Sementsov-Ogievskiy 
407f7651539SVladimir Sementsov-Ogievskiy     qemu_co_queue_restart_all(&s->free_sema);
40886f8cdf3SVladimir Sementsov-Ogievskiy     nbd_recv_coroutines_wake_all(s);
40986f8cdf3SVladimir Sementsov-Ogievskiy     bdrv_dec_in_flight(s->bs);
41086f8cdf3SVladimir Sementsov-Ogievskiy 
41186f8cdf3SVladimir Sementsov-Ogievskiy     s->connection_co = NULL;
412f7651539SVladimir Sementsov-Ogievskiy     if (s->ioc) {
413f7651539SVladimir Sementsov-Ogievskiy         nbd_client_detach_aio_context(s->bs);
414f7651539SVladimir Sementsov-Ogievskiy         object_unref(OBJECT(s->sioc));
415f7651539SVladimir Sementsov-Ogievskiy         s->sioc = NULL;
416f7651539SVladimir Sementsov-Ogievskiy         object_unref(OBJECT(s->ioc));
417f7651539SVladimir Sementsov-Ogievskiy         s->ioc = NULL;
418f7651539SVladimir Sementsov-Ogievskiy     }
419f7651539SVladimir Sementsov-Ogievskiy 
42078c81a3fSMax Reitz     if (s->teardown_co) {
42178c81a3fSMax Reitz         aio_co_wake(s->teardown_co);
42278c81a3fSMax Reitz     }
42386f8cdf3SVladimir Sementsov-Ogievskiy     aio_wait_kick();
42486f8cdf3SVladimir Sementsov-Ogievskiy }
42586f8cdf3SVladimir Sementsov-Ogievskiy 
42686f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_co_send_request(BlockDriverState *bs,
42786f8cdf3SVladimir Sementsov-Ogievskiy                                NBDRequest *request,
42886f8cdf3SVladimir Sementsov-Ogievskiy                                QEMUIOVector *qiov)
42986f8cdf3SVladimir Sementsov-Ogievskiy {
430611ae1d7SVladimir Sementsov-Ogievskiy     BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
431a34b1e5eSVladimir Sementsov-Ogievskiy     int rc, i = -1;
43286f8cdf3SVladimir Sementsov-Ogievskiy 
43386f8cdf3SVladimir Sementsov-Ogievskiy     qemu_co_mutex_lock(&s->send_mutex);
434f7651539SVladimir Sementsov-Ogievskiy     while (s->in_flight == MAX_NBD_REQUESTS || nbd_client_connecting_wait(s)) {
43586f8cdf3SVladimir Sementsov-Ogievskiy         qemu_co_queue_wait(&s->free_sema, &s->send_mutex);
43686f8cdf3SVladimir Sementsov-Ogievskiy     }
437a34b1e5eSVladimir Sementsov-Ogievskiy 
438a34b1e5eSVladimir Sementsov-Ogievskiy     if (s->state != NBD_CLIENT_CONNECTED) {
439a34b1e5eSVladimir Sementsov-Ogievskiy         rc = -EIO;
440a34b1e5eSVladimir Sementsov-Ogievskiy         goto err;
441a34b1e5eSVladimir Sementsov-Ogievskiy     }
442a34b1e5eSVladimir Sementsov-Ogievskiy 
44386f8cdf3SVladimir Sementsov-Ogievskiy     s->in_flight++;
44486f8cdf3SVladimir Sementsov-Ogievskiy 
44586f8cdf3SVladimir Sementsov-Ogievskiy     for (i = 0; i < MAX_NBD_REQUESTS; i++) {
44686f8cdf3SVladimir Sementsov-Ogievskiy         if (s->requests[i].coroutine == NULL) {
44786f8cdf3SVladimir Sementsov-Ogievskiy             break;
44886f8cdf3SVladimir Sementsov-Ogievskiy         }
44986f8cdf3SVladimir Sementsov-Ogievskiy     }
45086f8cdf3SVladimir Sementsov-Ogievskiy 
45186f8cdf3SVladimir Sementsov-Ogievskiy     g_assert(qemu_in_coroutine());
45286f8cdf3SVladimir Sementsov-Ogievskiy     assert(i < MAX_NBD_REQUESTS);
45386f8cdf3SVladimir Sementsov-Ogievskiy 
45486f8cdf3SVladimir Sementsov-Ogievskiy     s->requests[i].coroutine = qemu_coroutine_self();
45586f8cdf3SVladimir Sementsov-Ogievskiy     s->requests[i].offset = request->from;
45686f8cdf3SVladimir Sementsov-Ogievskiy     s->requests[i].receiving = false;
45786f8cdf3SVladimir Sementsov-Ogievskiy 
45886f8cdf3SVladimir Sementsov-Ogievskiy     request->handle = INDEX_TO_HANDLE(s, i);
45986f8cdf3SVladimir Sementsov-Ogievskiy 
46086f8cdf3SVladimir Sementsov-Ogievskiy     assert(s->ioc);
46186f8cdf3SVladimir Sementsov-Ogievskiy 
46286f8cdf3SVladimir Sementsov-Ogievskiy     if (qiov) {
46386f8cdf3SVladimir Sementsov-Ogievskiy         qio_channel_set_cork(s->ioc, true);
46486f8cdf3SVladimir Sementsov-Ogievskiy         rc = nbd_send_request(s->ioc, request);
465a34b1e5eSVladimir Sementsov-Ogievskiy         if (rc >= 0 && s->state == NBD_CLIENT_CONNECTED) {
46686f8cdf3SVladimir Sementsov-Ogievskiy             if (qio_channel_writev_all(s->ioc, qiov->iov, qiov->niov,
46786f8cdf3SVladimir Sementsov-Ogievskiy                                        NULL) < 0) {
46886f8cdf3SVladimir Sementsov-Ogievskiy                 rc = -EIO;
46986f8cdf3SVladimir Sementsov-Ogievskiy             }
47086f8cdf3SVladimir Sementsov-Ogievskiy         } else if (rc >= 0) {
47186f8cdf3SVladimir Sementsov-Ogievskiy             rc = -EIO;
47286f8cdf3SVladimir Sementsov-Ogievskiy         }
47386f8cdf3SVladimir Sementsov-Ogievskiy         qio_channel_set_cork(s->ioc, false);
47486f8cdf3SVladimir Sementsov-Ogievskiy     } else {
47586f8cdf3SVladimir Sementsov-Ogievskiy         rc = nbd_send_request(s->ioc, request);
47686f8cdf3SVladimir Sementsov-Ogievskiy     }
47786f8cdf3SVladimir Sementsov-Ogievskiy 
47886f8cdf3SVladimir Sementsov-Ogievskiy err:
47986f8cdf3SVladimir Sementsov-Ogievskiy     if (rc < 0) {
480a34b1e5eSVladimir Sementsov-Ogievskiy         nbd_channel_error(s, rc);
481a34b1e5eSVladimir Sementsov-Ogievskiy         if (i != -1) {
48286f8cdf3SVladimir Sementsov-Ogievskiy             s->requests[i].coroutine = NULL;
48386f8cdf3SVladimir Sementsov-Ogievskiy             s->in_flight--;
484a34b1e5eSVladimir Sementsov-Ogievskiy         }
485f7651539SVladimir Sementsov-Ogievskiy         if (s->in_flight == 0 && s->wait_in_flight) {
486f7651539SVladimir Sementsov-Ogievskiy             aio_co_wake(s->connection_co);
487f7651539SVladimir Sementsov-Ogievskiy         } else {
48886f8cdf3SVladimir Sementsov-Ogievskiy             qemu_co_queue_next(&s->free_sema);
48986f8cdf3SVladimir Sementsov-Ogievskiy         }
490f7651539SVladimir Sementsov-Ogievskiy     }
49186f8cdf3SVladimir Sementsov-Ogievskiy     qemu_co_mutex_unlock(&s->send_mutex);
49286f8cdf3SVladimir Sementsov-Ogievskiy     return rc;
49386f8cdf3SVladimir Sementsov-Ogievskiy }
49486f8cdf3SVladimir Sementsov-Ogievskiy 
49586f8cdf3SVladimir Sementsov-Ogievskiy static inline uint16_t payload_advance16(uint8_t **payload)
49686f8cdf3SVladimir Sementsov-Ogievskiy {
49786f8cdf3SVladimir Sementsov-Ogievskiy     *payload += 2;
49886f8cdf3SVladimir Sementsov-Ogievskiy     return lduw_be_p(*payload - 2);
49986f8cdf3SVladimir Sementsov-Ogievskiy }
50086f8cdf3SVladimir Sementsov-Ogievskiy 
50186f8cdf3SVladimir Sementsov-Ogievskiy static inline uint32_t payload_advance32(uint8_t **payload)
50286f8cdf3SVladimir Sementsov-Ogievskiy {
50386f8cdf3SVladimir Sementsov-Ogievskiy     *payload += 4;
50486f8cdf3SVladimir Sementsov-Ogievskiy     return ldl_be_p(*payload - 4);
50586f8cdf3SVladimir Sementsov-Ogievskiy }
50686f8cdf3SVladimir Sementsov-Ogievskiy 
50786f8cdf3SVladimir Sementsov-Ogievskiy static inline uint64_t payload_advance64(uint8_t **payload)
50886f8cdf3SVladimir Sementsov-Ogievskiy {
50986f8cdf3SVladimir Sementsov-Ogievskiy     *payload += 8;
51086f8cdf3SVladimir Sementsov-Ogievskiy     return ldq_be_p(*payload - 8);
51186f8cdf3SVladimir Sementsov-Ogievskiy }
51286f8cdf3SVladimir Sementsov-Ogievskiy 
513611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_parse_offset_hole_payload(BDRVNBDState *s,
51486f8cdf3SVladimir Sementsov-Ogievskiy                                          NBDStructuredReplyChunk *chunk,
51586f8cdf3SVladimir Sementsov-Ogievskiy                                          uint8_t *payload, uint64_t orig_offset,
51686f8cdf3SVladimir Sementsov-Ogievskiy                                          QEMUIOVector *qiov, Error **errp)
51786f8cdf3SVladimir Sementsov-Ogievskiy {
51886f8cdf3SVladimir Sementsov-Ogievskiy     uint64_t offset;
51986f8cdf3SVladimir Sementsov-Ogievskiy     uint32_t hole_size;
52086f8cdf3SVladimir Sementsov-Ogievskiy 
52186f8cdf3SVladimir Sementsov-Ogievskiy     if (chunk->length != sizeof(offset) + sizeof(hole_size)) {
52286f8cdf3SVladimir Sementsov-Ogievskiy         error_setg(errp, "Protocol error: invalid payload for "
52386f8cdf3SVladimir Sementsov-Ogievskiy                          "NBD_REPLY_TYPE_OFFSET_HOLE");
52486f8cdf3SVladimir Sementsov-Ogievskiy         return -EINVAL;
52586f8cdf3SVladimir Sementsov-Ogievskiy     }
52686f8cdf3SVladimir Sementsov-Ogievskiy 
52786f8cdf3SVladimir Sementsov-Ogievskiy     offset = payload_advance64(&payload);
52886f8cdf3SVladimir Sementsov-Ogievskiy     hole_size = payload_advance32(&payload);
52986f8cdf3SVladimir Sementsov-Ogievskiy 
53086f8cdf3SVladimir Sementsov-Ogievskiy     if (!hole_size || offset < orig_offset || hole_size > qiov->size ||
53186f8cdf3SVladimir Sementsov-Ogievskiy         offset > orig_offset + qiov->size - hole_size) {
53286f8cdf3SVladimir Sementsov-Ogievskiy         error_setg(errp, "Protocol error: server sent chunk exceeding requested"
53386f8cdf3SVladimir Sementsov-Ogievskiy                          " region");
53486f8cdf3SVladimir Sementsov-Ogievskiy         return -EINVAL;
53586f8cdf3SVladimir Sementsov-Ogievskiy     }
536611ae1d7SVladimir Sementsov-Ogievskiy     if (s->info.min_block &&
537611ae1d7SVladimir Sementsov-Ogievskiy         !QEMU_IS_ALIGNED(hole_size, s->info.min_block)) {
53886f8cdf3SVladimir Sementsov-Ogievskiy         trace_nbd_structured_read_compliance("hole");
53986f8cdf3SVladimir Sementsov-Ogievskiy     }
54086f8cdf3SVladimir Sementsov-Ogievskiy 
54186f8cdf3SVladimir Sementsov-Ogievskiy     qemu_iovec_memset(qiov, offset - orig_offset, 0, hole_size);
54286f8cdf3SVladimir Sementsov-Ogievskiy 
54386f8cdf3SVladimir Sementsov-Ogievskiy     return 0;
54486f8cdf3SVladimir Sementsov-Ogievskiy }
54586f8cdf3SVladimir Sementsov-Ogievskiy 
54686f8cdf3SVladimir Sementsov-Ogievskiy /*
54786f8cdf3SVladimir Sementsov-Ogievskiy  * nbd_parse_blockstatus_payload
54886f8cdf3SVladimir Sementsov-Ogievskiy  * Based on our request, we expect only one extent in reply, for the
54986f8cdf3SVladimir Sementsov-Ogievskiy  * base:allocation context.
55086f8cdf3SVladimir Sementsov-Ogievskiy  */
551611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_parse_blockstatus_payload(BDRVNBDState *s,
55286f8cdf3SVladimir Sementsov-Ogievskiy                                          NBDStructuredReplyChunk *chunk,
55386f8cdf3SVladimir Sementsov-Ogievskiy                                          uint8_t *payload, uint64_t orig_length,
55486f8cdf3SVladimir Sementsov-Ogievskiy                                          NBDExtent *extent, Error **errp)
55586f8cdf3SVladimir Sementsov-Ogievskiy {
55686f8cdf3SVladimir Sementsov-Ogievskiy     uint32_t context_id;
55786f8cdf3SVladimir Sementsov-Ogievskiy 
55886f8cdf3SVladimir Sementsov-Ogievskiy     /* The server succeeded, so it must have sent [at least] one extent */
55986f8cdf3SVladimir Sementsov-Ogievskiy     if (chunk->length < sizeof(context_id) + sizeof(*extent)) {
56086f8cdf3SVladimir Sementsov-Ogievskiy         error_setg(errp, "Protocol error: invalid payload for "
56186f8cdf3SVladimir Sementsov-Ogievskiy                          "NBD_REPLY_TYPE_BLOCK_STATUS");
56286f8cdf3SVladimir Sementsov-Ogievskiy         return -EINVAL;
56386f8cdf3SVladimir Sementsov-Ogievskiy     }
56486f8cdf3SVladimir Sementsov-Ogievskiy 
56586f8cdf3SVladimir Sementsov-Ogievskiy     context_id = payload_advance32(&payload);
566611ae1d7SVladimir Sementsov-Ogievskiy     if (s->info.context_id != context_id) {
56786f8cdf3SVladimir Sementsov-Ogievskiy         error_setg(errp, "Protocol error: unexpected context id %d for "
56886f8cdf3SVladimir Sementsov-Ogievskiy                          "NBD_REPLY_TYPE_BLOCK_STATUS, when negotiated context "
56986f8cdf3SVladimir Sementsov-Ogievskiy                          "id is %d", context_id,
570611ae1d7SVladimir Sementsov-Ogievskiy                          s->info.context_id);
57186f8cdf3SVladimir Sementsov-Ogievskiy         return -EINVAL;
57286f8cdf3SVladimir Sementsov-Ogievskiy     }
57386f8cdf3SVladimir Sementsov-Ogievskiy 
57486f8cdf3SVladimir Sementsov-Ogievskiy     extent->length = payload_advance32(&payload);
57586f8cdf3SVladimir Sementsov-Ogievskiy     extent->flags = payload_advance32(&payload);
57686f8cdf3SVladimir Sementsov-Ogievskiy 
57786f8cdf3SVladimir Sementsov-Ogievskiy     if (extent->length == 0) {
57886f8cdf3SVladimir Sementsov-Ogievskiy         error_setg(errp, "Protocol error: server sent status chunk with "
57986f8cdf3SVladimir Sementsov-Ogievskiy                    "zero length");
58086f8cdf3SVladimir Sementsov-Ogievskiy         return -EINVAL;
58186f8cdf3SVladimir Sementsov-Ogievskiy     }
58286f8cdf3SVladimir Sementsov-Ogievskiy 
58386f8cdf3SVladimir Sementsov-Ogievskiy     /*
58486f8cdf3SVladimir Sementsov-Ogievskiy      * A server sending unaligned block status is in violation of the
58586f8cdf3SVladimir Sementsov-Ogievskiy      * protocol, but as qemu-nbd 3.1 is such a server (at least for
58686f8cdf3SVladimir Sementsov-Ogievskiy      * POSIX files that are not a multiple of 512 bytes, since qemu
58786f8cdf3SVladimir Sementsov-Ogievskiy      * rounds files up to 512-byte multiples but lseek(SEEK_HOLE)
58886f8cdf3SVladimir Sementsov-Ogievskiy      * still sees an implicit hole beyond the real EOF), it's nicer to
58986f8cdf3SVladimir Sementsov-Ogievskiy      * work around the misbehaving server. If the request included
59086f8cdf3SVladimir Sementsov-Ogievskiy      * more than the final unaligned block, truncate it back to an
59186f8cdf3SVladimir Sementsov-Ogievskiy      * aligned result; if the request was only the final block, round
59286f8cdf3SVladimir Sementsov-Ogievskiy      * up to the full block and change the status to fully-allocated
59386f8cdf3SVladimir Sementsov-Ogievskiy      * (always a safe status, even if it loses information).
59486f8cdf3SVladimir Sementsov-Ogievskiy      */
595611ae1d7SVladimir Sementsov-Ogievskiy     if (s->info.min_block && !QEMU_IS_ALIGNED(extent->length,
596611ae1d7SVladimir Sementsov-Ogievskiy                                                    s->info.min_block)) {
59786f8cdf3SVladimir Sementsov-Ogievskiy         trace_nbd_parse_blockstatus_compliance("extent length is unaligned");
598611ae1d7SVladimir Sementsov-Ogievskiy         if (extent->length > s->info.min_block) {
59986f8cdf3SVladimir Sementsov-Ogievskiy             extent->length = QEMU_ALIGN_DOWN(extent->length,
600611ae1d7SVladimir Sementsov-Ogievskiy                                              s->info.min_block);
60186f8cdf3SVladimir Sementsov-Ogievskiy         } else {
602611ae1d7SVladimir Sementsov-Ogievskiy             extent->length = s->info.min_block;
60386f8cdf3SVladimir Sementsov-Ogievskiy             extent->flags = 0;
60486f8cdf3SVladimir Sementsov-Ogievskiy         }
60586f8cdf3SVladimir Sementsov-Ogievskiy     }
60686f8cdf3SVladimir Sementsov-Ogievskiy 
60786f8cdf3SVladimir Sementsov-Ogievskiy     /*
60886f8cdf3SVladimir Sementsov-Ogievskiy      * We used NBD_CMD_FLAG_REQ_ONE, so the server should not have
60986f8cdf3SVladimir Sementsov-Ogievskiy      * sent us any more than one extent, nor should it have included
61086f8cdf3SVladimir Sementsov-Ogievskiy      * status beyond our request in that extent. However, it's easy
61186f8cdf3SVladimir Sementsov-Ogievskiy      * enough to ignore the server's noncompliance without killing the
61286f8cdf3SVladimir Sementsov-Ogievskiy      * connection; just ignore trailing extents, and clamp things to
61386f8cdf3SVladimir Sementsov-Ogievskiy      * the length of our request.
61486f8cdf3SVladimir Sementsov-Ogievskiy      */
61586f8cdf3SVladimir Sementsov-Ogievskiy     if (chunk->length > sizeof(context_id) + sizeof(*extent)) {
61686f8cdf3SVladimir Sementsov-Ogievskiy         trace_nbd_parse_blockstatus_compliance("more than one extent");
61786f8cdf3SVladimir Sementsov-Ogievskiy     }
61886f8cdf3SVladimir Sementsov-Ogievskiy     if (extent->length > orig_length) {
61986f8cdf3SVladimir Sementsov-Ogievskiy         extent->length = orig_length;
62086f8cdf3SVladimir Sementsov-Ogievskiy         trace_nbd_parse_blockstatus_compliance("extent length too large");
62186f8cdf3SVladimir Sementsov-Ogievskiy     }
62286f8cdf3SVladimir Sementsov-Ogievskiy 
62386f8cdf3SVladimir Sementsov-Ogievskiy     return 0;
62486f8cdf3SVladimir Sementsov-Ogievskiy }
62586f8cdf3SVladimir Sementsov-Ogievskiy 
62686f8cdf3SVladimir Sementsov-Ogievskiy /*
62786f8cdf3SVladimir Sementsov-Ogievskiy  * nbd_parse_error_payload
62886f8cdf3SVladimir Sementsov-Ogievskiy  * on success @errp contains message describing nbd error reply
62986f8cdf3SVladimir Sementsov-Ogievskiy  */
63086f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_parse_error_payload(NBDStructuredReplyChunk *chunk,
63186f8cdf3SVladimir Sementsov-Ogievskiy                                    uint8_t *payload, int *request_ret,
63286f8cdf3SVladimir Sementsov-Ogievskiy                                    Error **errp)
63386f8cdf3SVladimir Sementsov-Ogievskiy {
63486f8cdf3SVladimir Sementsov-Ogievskiy     uint32_t error;
63586f8cdf3SVladimir Sementsov-Ogievskiy     uint16_t message_size;
63686f8cdf3SVladimir Sementsov-Ogievskiy 
63786f8cdf3SVladimir Sementsov-Ogievskiy     assert(chunk->type & (1 << 15));
63886f8cdf3SVladimir Sementsov-Ogievskiy 
63986f8cdf3SVladimir Sementsov-Ogievskiy     if (chunk->length < sizeof(error) + sizeof(message_size)) {
64086f8cdf3SVladimir Sementsov-Ogievskiy         error_setg(errp,
64186f8cdf3SVladimir Sementsov-Ogievskiy                    "Protocol error: invalid payload for structured error");
64286f8cdf3SVladimir Sementsov-Ogievskiy         return -EINVAL;
64386f8cdf3SVladimir Sementsov-Ogievskiy     }
64486f8cdf3SVladimir Sementsov-Ogievskiy 
64586f8cdf3SVladimir Sementsov-Ogievskiy     error = nbd_errno_to_system_errno(payload_advance32(&payload));
64686f8cdf3SVladimir Sementsov-Ogievskiy     if (error == 0) {
64786f8cdf3SVladimir Sementsov-Ogievskiy         error_setg(errp, "Protocol error: server sent structured error chunk "
64886f8cdf3SVladimir Sementsov-Ogievskiy                          "with error = 0");
64986f8cdf3SVladimir Sementsov-Ogievskiy         return -EINVAL;
65086f8cdf3SVladimir Sementsov-Ogievskiy     }
65186f8cdf3SVladimir Sementsov-Ogievskiy 
65286f8cdf3SVladimir Sementsov-Ogievskiy     *request_ret = -error;
65386f8cdf3SVladimir Sementsov-Ogievskiy     message_size = payload_advance16(&payload);
65486f8cdf3SVladimir Sementsov-Ogievskiy 
65586f8cdf3SVladimir Sementsov-Ogievskiy     if (message_size > chunk->length - sizeof(error) - sizeof(message_size)) {
65686f8cdf3SVladimir Sementsov-Ogievskiy         error_setg(errp, "Protocol error: server sent structured error chunk "
65786f8cdf3SVladimir Sementsov-Ogievskiy                          "with incorrect message size");
65886f8cdf3SVladimir Sementsov-Ogievskiy         return -EINVAL;
65986f8cdf3SVladimir Sementsov-Ogievskiy     }
66086f8cdf3SVladimir Sementsov-Ogievskiy 
66186f8cdf3SVladimir Sementsov-Ogievskiy     /* TODO: Add a trace point to mention the server complaint */
66286f8cdf3SVladimir Sementsov-Ogievskiy 
66386f8cdf3SVladimir Sementsov-Ogievskiy     /* TODO handle ERROR_OFFSET */
66486f8cdf3SVladimir Sementsov-Ogievskiy 
66586f8cdf3SVladimir Sementsov-Ogievskiy     return 0;
66686f8cdf3SVladimir Sementsov-Ogievskiy }
66786f8cdf3SVladimir Sementsov-Ogievskiy 
668611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_co_receive_offset_data_payload(BDRVNBDState *s,
66986f8cdf3SVladimir Sementsov-Ogievskiy                                               uint64_t orig_offset,
67086f8cdf3SVladimir Sementsov-Ogievskiy                                               QEMUIOVector *qiov, Error **errp)
67186f8cdf3SVladimir Sementsov-Ogievskiy {
67286f8cdf3SVladimir Sementsov-Ogievskiy     QEMUIOVector sub_qiov;
67386f8cdf3SVladimir Sementsov-Ogievskiy     uint64_t offset;
67486f8cdf3SVladimir Sementsov-Ogievskiy     size_t data_size;
67586f8cdf3SVladimir Sementsov-Ogievskiy     int ret;
67686f8cdf3SVladimir Sementsov-Ogievskiy     NBDStructuredReplyChunk *chunk = &s->reply.structured;
67786f8cdf3SVladimir Sementsov-Ogievskiy 
67886f8cdf3SVladimir Sementsov-Ogievskiy     assert(nbd_reply_is_structured(&s->reply));
67986f8cdf3SVladimir Sementsov-Ogievskiy 
68086f8cdf3SVladimir Sementsov-Ogievskiy     /* The NBD spec requires at least one byte of payload */
68186f8cdf3SVladimir Sementsov-Ogievskiy     if (chunk->length <= sizeof(offset)) {
68286f8cdf3SVladimir Sementsov-Ogievskiy         error_setg(errp, "Protocol error: invalid payload for "
68386f8cdf3SVladimir Sementsov-Ogievskiy                          "NBD_REPLY_TYPE_OFFSET_DATA");
68486f8cdf3SVladimir Sementsov-Ogievskiy         return -EINVAL;
68586f8cdf3SVladimir Sementsov-Ogievskiy     }
68686f8cdf3SVladimir Sementsov-Ogievskiy 
68786f8cdf3SVladimir Sementsov-Ogievskiy     if (nbd_read64(s->ioc, &offset, "OFFSET_DATA offset", errp) < 0) {
68886f8cdf3SVladimir Sementsov-Ogievskiy         return -EIO;
68986f8cdf3SVladimir Sementsov-Ogievskiy     }
69086f8cdf3SVladimir Sementsov-Ogievskiy 
69186f8cdf3SVladimir Sementsov-Ogievskiy     data_size = chunk->length - sizeof(offset);
69286f8cdf3SVladimir Sementsov-Ogievskiy     assert(data_size);
69386f8cdf3SVladimir Sementsov-Ogievskiy     if (offset < orig_offset || data_size > qiov->size ||
69486f8cdf3SVladimir Sementsov-Ogievskiy         offset > orig_offset + qiov->size - data_size) {
69586f8cdf3SVladimir Sementsov-Ogievskiy         error_setg(errp, "Protocol error: server sent chunk exceeding requested"
69686f8cdf3SVladimir Sementsov-Ogievskiy                          " region");
69786f8cdf3SVladimir Sementsov-Ogievskiy         return -EINVAL;
69886f8cdf3SVladimir Sementsov-Ogievskiy     }
69986f8cdf3SVladimir Sementsov-Ogievskiy     if (s->info.min_block && !QEMU_IS_ALIGNED(data_size, s->info.min_block)) {
70086f8cdf3SVladimir Sementsov-Ogievskiy         trace_nbd_structured_read_compliance("data");
70186f8cdf3SVladimir Sementsov-Ogievskiy     }
70286f8cdf3SVladimir Sementsov-Ogievskiy 
70386f8cdf3SVladimir Sementsov-Ogievskiy     qemu_iovec_init(&sub_qiov, qiov->niov);
70486f8cdf3SVladimir Sementsov-Ogievskiy     qemu_iovec_concat(&sub_qiov, qiov, offset - orig_offset, data_size);
70586f8cdf3SVladimir Sementsov-Ogievskiy     ret = qio_channel_readv_all(s->ioc, sub_qiov.iov, sub_qiov.niov, errp);
70686f8cdf3SVladimir Sementsov-Ogievskiy     qemu_iovec_destroy(&sub_qiov);
70786f8cdf3SVladimir Sementsov-Ogievskiy 
70886f8cdf3SVladimir Sementsov-Ogievskiy     return ret < 0 ? -EIO : 0;
70986f8cdf3SVladimir Sementsov-Ogievskiy }
71086f8cdf3SVladimir Sementsov-Ogievskiy 
71186f8cdf3SVladimir Sementsov-Ogievskiy #define NBD_MAX_MALLOC_PAYLOAD 1000
71286f8cdf3SVladimir Sementsov-Ogievskiy static coroutine_fn int nbd_co_receive_structured_payload(
713611ae1d7SVladimir Sementsov-Ogievskiy         BDRVNBDState *s, void **payload, Error **errp)
71486f8cdf3SVladimir Sementsov-Ogievskiy {
71586f8cdf3SVladimir Sementsov-Ogievskiy     int ret;
71686f8cdf3SVladimir Sementsov-Ogievskiy     uint32_t len;
71786f8cdf3SVladimir Sementsov-Ogievskiy 
71886f8cdf3SVladimir Sementsov-Ogievskiy     assert(nbd_reply_is_structured(&s->reply));
71986f8cdf3SVladimir Sementsov-Ogievskiy 
72086f8cdf3SVladimir Sementsov-Ogievskiy     len = s->reply.structured.length;
72186f8cdf3SVladimir Sementsov-Ogievskiy 
72286f8cdf3SVladimir Sementsov-Ogievskiy     if (len == 0) {
72386f8cdf3SVladimir Sementsov-Ogievskiy         return 0;
72486f8cdf3SVladimir Sementsov-Ogievskiy     }
72586f8cdf3SVladimir Sementsov-Ogievskiy 
72686f8cdf3SVladimir Sementsov-Ogievskiy     if (payload == NULL) {
72786f8cdf3SVladimir Sementsov-Ogievskiy         error_setg(errp, "Unexpected structured payload");
72886f8cdf3SVladimir Sementsov-Ogievskiy         return -EINVAL;
72986f8cdf3SVladimir Sementsov-Ogievskiy     }
73086f8cdf3SVladimir Sementsov-Ogievskiy 
73186f8cdf3SVladimir Sementsov-Ogievskiy     if (len > NBD_MAX_MALLOC_PAYLOAD) {
73286f8cdf3SVladimir Sementsov-Ogievskiy         error_setg(errp, "Payload too large");
73386f8cdf3SVladimir Sementsov-Ogievskiy         return -EINVAL;
73486f8cdf3SVladimir Sementsov-Ogievskiy     }
73586f8cdf3SVladimir Sementsov-Ogievskiy 
73686f8cdf3SVladimir Sementsov-Ogievskiy     *payload = g_new(char, len);
73786f8cdf3SVladimir Sementsov-Ogievskiy     ret = nbd_read(s->ioc, *payload, len, "structured payload", errp);
73886f8cdf3SVladimir Sementsov-Ogievskiy     if (ret < 0) {
73986f8cdf3SVladimir Sementsov-Ogievskiy         g_free(*payload);
74086f8cdf3SVladimir Sementsov-Ogievskiy         *payload = NULL;
74186f8cdf3SVladimir Sementsov-Ogievskiy         return ret;
74286f8cdf3SVladimir Sementsov-Ogievskiy     }
74386f8cdf3SVladimir Sementsov-Ogievskiy 
74486f8cdf3SVladimir Sementsov-Ogievskiy     return 0;
74586f8cdf3SVladimir Sementsov-Ogievskiy }
74686f8cdf3SVladimir Sementsov-Ogievskiy 
74786f8cdf3SVladimir Sementsov-Ogievskiy /*
74886f8cdf3SVladimir Sementsov-Ogievskiy  * nbd_co_do_receive_one_chunk
74986f8cdf3SVladimir Sementsov-Ogievskiy  * for simple reply:
75086f8cdf3SVladimir Sementsov-Ogievskiy  *   set request_ret to received reply error
75186f8cdf3SVladimir Sementsov-Ogievskiy  *   if qiov is not NULL: read payload to @qiov
75286f8cdf3SVladimir Sementsov-Ogievskiy  * for structured reply chunk:
75386f8cdf3SVladimir Sementsov-Ogievskiy  *   if error chunk: read payload, set @request_ret, do not set @payload
75486f8cdf3SVladimir Sementsov-Ogievskiy  *   else if offset_data chunk: read payload data to @qiov, do not set @payload
75586f8cdf3SVladimir Sementsov-Ogievskiy  *   else: read payload to @payload
75686f8cdf3SVladimir Sementsov-Ogievskiy  *
75786f8cdf3SVladimir Sementsov-Ogievskiy  * If function fails, @errp contains corresponding error message, and the
75886f8cdf3SVladimir Sementsov-Ogievskiy  * connection with the server is suspect.  If it returns 0, then the
75986f8cdf3SVladimir Sementsov-Ogievskiy  * transaction succeeded (although @request_ret may be a negative errno
76086f8cdf3SVladimir Sementsov-Ogievskiy  * corresponding to the server's error reply), and errp is unchanged.
76186f8cdf3SVladimir Sementsov-Ogievskiy  */
76286f8cdf3SVladimir Sementsov-Ogievskiy static coroutine_fn int nbd_co_do_receive_one_chunk(
763611ae1d7SVladimir Sementsov-Ogievskiy         BDRVNBDState *s, uint64_t handle, bool only_structured,
76486f8cdf3SVladimir Sementsov-Ogievskiy         int *request_ret, QEMUIOVector *qiov, void **payload, Error **errp)
76586f8cdf3SVladimir Sementsov-Ogievskiy {
76686f8cdf3SVladimir Sementsov-Ogievskiy     int ret;
76786f8cdf3SVladimir Sementsov-Ogievskiy     int i = HANDLE_TO_INDEX(s, handle);
76886f8cdf3SVladimir Sementsov-Ogievskiy     void *local_payload = NULL;
76986f8cdf3SVladimir Sementsov-Ogievskiy     NBDStructuredReplyChunk *chunk;
77086f8cdf3SVladimir Sementsov-Ogievskiy 
77186f8cdf3SVladimir Sementsov-Ogievskiy     if (payload) {
77286f8cdf3SVladimir Sementsov-Ogievskiy         *payload = NULL;
77386f8cdf3SVladimir Sementsov-Ogievskiy     }
77486f8cdf3SVladimir Sementsov-Ogievskiy     *request_ret = 0;
77586f8cdf3SVladimir Sementsov-Ogievskiy 
77686f8cdf3SVladimir Sementsov-Ogievskiy     /* Wait until we're woken up by nbd_connection_entry.  */
77786f8cdf3SVladimir Sementsov-Ogievskiy     s->requests[i].receiving = true;
77886f8cdf3SVladimir Sementsov-Ogievskiy     qemu_coroutine_yield();
77986f8cdf3SVladimir Sementsov-Ogievskiy     s->requests[i].receiving = false;
780a34b1e5eSVladimir Sementsov-Ogievskiy     if (s->state != NBD_CLIENT_CONNECTED) {
78186f8cdf3SVladimir Sementsov-Ogievskiy         error_setg(errp, "Connection closed");
78286f8cdf3SVladimir Sementsov-Ogievskiy         return -EIO;
78386f8cdf3SVladimir Sementsov-Ogievskiy     }
78486f8cdf3SVladimir Sementsov-Ogievskiy     assert(s->ioc);
78586f8cdf3SVladimir Sementsov-Ogievskiy 
78686f8cdf3SVladimir Sementsov-Ogievskiy     assert(s->reply.handle == handle);
78786f8cdf3SVladimir Sementsov-Ogievskiy 
78886f8cdf3SVladimir Sementsov-Ogievskiy     if (nbd_reply_is_simple(&s->reply)) {
78986f8cdf3SVladimir Sementsov-Ogievskiy         if (only_structured) {
79086f8cdf3SVladimir Sementsov-Ogievskiy             error_setg(errp, "Protocol error: simple reply when structured "
79186f8cdf3SVladimir Sementsov-Ogievskiy                              "reply chunk was expected");
79286f8cdf3SVladimir Sementsov-Ogievskiy             return -EINVAL;
79386f8cdf3SVladimir Sementsov-Ogievskiy         }
79486f8cdf3SVladimir Sementsov-Ogievskiy 
79586f8cdf3SVladimir Sementsov-Ogievskiy         *request_ret = -nbd_errno_to_system_errno(s->reply.simple.error);
79686f8cdf3SVladimir Sementsov-Ogievskiy         if (*request_ret < 0 || !qiov) {
79786f8cdf3SVladimir Sementsov-Ogievskiy             return 0;
79886f8cdf3SVladimir Sementsov-Ogievskiy         }
79986f8cdf3SVladimir Sementsov-Ogievskiy 
80086f8cdf3SVladimir Sementsov-Ogievskiy         return qio_channel_readv_all(s->ioc, qiov->iov, qiov->niov,
80186f8cdf3SVladimir Sementsov-Ogievskiy                                      errp) < 0 ? -EIO : 0;
80286f8cdf3SVladimir Sementsov-Ogievskiy     }
80386f8cdf3SVladimir Sementsov-Ogievskiy 
80486f8cdf3SVladimir Sementsov-Ogievskiy     /* handle structured reply chunk */
80586f8cdf3SVladimir Sementsov-Ogievskiy     assert(s->info.structured_reply);
80686f8cdf3SVladimir Sementsov-Ogievskiy     chunk = &s->reply.structured;
80786f8cdf3SVladimir Sementsov-Ogievskiy 
80886f8cdf3SVladimir Sementsov-Ogievskiy     if (chunk->type == NBD_REPLY_TYPE_NONE) {
80986f8cdf3SVladimir Sementsov-Ogievskiy         if (!(chunk->flags & NBD_REPLY_FLAG_DONE)) {
81086f8cdf3SVladimir Sementsov-Ogievskiy             error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk without"
81186f8cdf3SVladimir Sementsov-Ogievskiy                        " NBD_REPLY_FLAG_DONE flag set");
81286f8cdf3SVladimir Sementsov-Ogievskiy             return -EINVAL;
81386f8cdf3SVladimir Sementsov-Ogievskiy         }
81486f8cdf3SVladimir Sementsov-Ogievskiy         if (chunk->length) {
81586f8cdf3SVladimir Sementsov-Ogievskiy             error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk with"
81686f8cdf3SVladimir Sementsov-Ogievskiy                        " nonzero length");
81786f8cdf3SVladimir Sementsov-Ogievskiy             return -EINVAL;
81886f8cdf3SVladimir Sementsov-Ogievskiy         }
81986f8cdf3SVladimir Sementsov-Ogievskiy         return 0;
82086f8cdf3SVladimir Sementsov-Ogievskiy     }
82186f8cdf3SVladimir Sementsov-Ogievskiy 
82286f8cdf3SVladimir Sementsov-Ogievskiy     if (chunk->type == NBD_REPLY_TYPE_OFFSET_DATA) {
82386f8cdf3SVladimir Sementsov-Ogievskiy         if (!qiov) {
82486f8cdf3SVladimir Sementsov-Ogievskiy             error_setg(errp, "Unexpected NBD_REPLY_TYPE_OFFSET_DATA chunk");
82586f8cdf3SVladimir Sementsov-Ogievskiy             return -EINVAL;
82686f8cdf3SVladimir Sementsov-Ogievskiy         }
82786f8cdf3SVladimir Sementsov-Ogievskiy 
82886f8cdf3SVladimir Sementsov-Ogievskiy         return nbd_co_receive_offset_data_payload(s, s->requests[i].offset,
82986f8cdf3SVladimir Sementsov-Ogievskiy                                                   qiov, errp);
83086f8cdf3SVladimir Sementsov-Ogievskiy     }
83186f8cdf3SVladimir Sementsov-Ogievskiy 
83286f8cdf3SVladimir Sementsov-Ogievskiy     if (nbd_reply_type_is_error(chunk->type)) {
83386f8cdf3SVladimir Sementsov-Ogievskiy         payload = &local_payload;
83486f8cdf3SVladimir Sementsov-Ogievskiy     }
83586f8cdf3SVladimir Sementsov-Ogievskiy 
83686f8cdf3SVladimir Sementsov-Ogievskiy     ret = nbd_co_receive_structured_payload(s, payload, errp);
83786f8cdf3SVladimir Sementsov-Ogievskiy     if (ret < 0) {
83886f8cdf3SVladimir Sementsov-Ogievskiy         return ret;
83986f8cdf3SVladimir Sementsov-Ogievskiy     }
84086f8cdf3SVladimir Sementsov-Ogievskiy 
84186f8cdf3SVladimir Sementsov-Ogievskiy     if (nbd_reply_type_is_error(chunk->type)) {
84286f8cdf3SVladimir Sementsov-Ogievskiy         ret = nbd_parse_error_payload(chunk, local_payload, request_ret, errp);
84386f8cdf3SVladimir Sementsov-Ogievskiy         g_free(local_payload);
84486f8cdf3SVladimir Sementsov-Ogievskiy         return ret;
84586f8cdf3SVladimir Sementsov-Ogievskiy     }
84686f8cdf3SVladimir Sementsov-Ogievskiy 
84786f8cdf3SVladimir Sementsov-Ogievskiy     return 0;
84886f8cdf3SVladimir Sementsov-Ogievskiy }
84986f8cdf3SVladimir Sementsov-Ogievskiy 
85086f8cdf3SVladimir Sementsov-Ogievskiy /*
85186f8cdf3SVladimir Sementsov-Ogievskiy  * nbd_co_receive_one_chunk
85286f8cdf3SVladimir Sementsov-Ogievskiy  * Read reply, wake up connection_co and set s->quit if needed.
85386f8cdf3SVladimir Sementsov-Ogievskiy  * Return value is a fatal error code or normal nbd reply error code
85486f8cdf3SVladimir Sementsov-Ogievskiy  */
85586f8cdf3SVladimir Sementsov-Ogievskiy static coroutine_fn int nbd_co_receive_one_chunk(
856611ae1d7SVladimir Sementsov-Ogievskiy         BDRVNBDState *s, uint64_t handle, bool only_structured,
85786f8cdf3SVladimir Sementsov-Ogievskiy         int *request_ret, QEMUIOVector *qiov, NBDReply *reply, void **payload,
85886f8cdf3SVladimir Sementsov-Ogievskiy         Error **errp)
85986f8cdf3SVladimir Sementsov-Ogievskiy {
86086f8cdf3SVladimir Sementsov-Ogievskiy     int ret = nbd_co_do_receive_one_chunk(s, handle, only_structured,
86186f8cdf3SVladimir Sementsov-Ogievskiy                                           request_ret, qiov, payload, errp);
86286f8cdf3SVladimir Sementsov-Ogievskiy 
86386f8cdf3SVladimir Sementsov-Ogievskiy     if (ret < 0) {
8645cf42b1cSEric Blake         memset(reply, 0, sizeof(*reply));
865a34b1e5eSVladimir Sementsov-Ogievskiy         nbd_channel_error(s, ret);
86686f8cdf3SVladimir Sementsov-Ogievskiy     } else {
86786f8cdf3SVladimir Sementsov-Ogievskiy         /* For assert at loop start in nbd_connection_entry */
86886f8cdf3SVladimir Sementsov-Ogievskiy         *reply = s->reply;
86986f8cdf3SVladimir Sementsov-Ogievskiy     }
870f7651539SVladimir Sementsov-Ogievskiy     s->reply.handle = 0;
87186f8cdf3SVladimir Sementsov-Ogievskiy 
872f7651539SVladimir Sementsov-Ogievskiy     if (s->connection_co && !s->wait_in_flight) {
873f7651539SVladimir Sementsov-Ogievskiy         /*
874f7651539SVladimir Sementsov-Ogievskiy          * We must check s->wait_in_flight, because we may entered by
875f7651539SVladimir Sementsov-Ogievskiy          * nbd_recv_coroutines_wake_all(), in this case we should not
876f7651539SVladimir Sementsov-Ogievskiy          * wake connection_co here, it will woken by last request.
877f7651539SVladimir Sementsov-Ogievskiy          */
87886f8cdf3SVladimir Sementsov-Ogievskiy         aio_co_wake(s->connection_co);
87986f8cdf3SVladimir Sementsov-Ogievskiy     }
88086f8cdf3SVladimir Sementsov-Ogievskiy 
88186f8cdf3SVladimir Sementsov-Ogievskiy     return ret;
88286f8cdf3SVladimir Sementsov-Ogievskiy }
88386f8cdf3SVladimir Sementsov-Ogievskiy 
88486f8cdf3SVladimir Sementsov-Ogievskiy typedef struct NBDReplyChunkIter {
88586f8cdf3SVladimir Sementsov-Ogievskiy     int ret;
88686f8cdf3SVladimir Sementsov-Ogievskiy     int request_ret;
88786f8cdf3SVladimir Sementsov-Ogievskiy     Error *err;
88886f8cdf3SVladimir Sementsov-Ogievskiy     bool done, only_structured;
88986f8cdf3SVladimir Sementsov-Ogievskiy } NBDReplyChunkIter;
89086f8cdf3SVladimir Sementsov-Ogievskiy 
89186f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_iter_channel_error(NBDReplyChunkIter *iter,
89286f8cdf3SVladimir Sementsov-Ogievskiy                                    int ret, Error **local_err)
89386f8cdf3SVladimir Sementsov-Ogievskiy {
894d9366135SVladimir Sementsov-Ogievskiy     assert(local_err && *local_err);
89586f8cdf3SVladimir Sementsov-Ogievskiy     assert(ret < 0);
89686f8cdf3SVladimir Sementsov-Ogievskiy 
89786f8cdf3SVladimir Sementsov-Ogievskiy     if (!iter->ret) {
89886f8cdf3SVladimir Sementsov-Ogievskiy         iter->ret = ret;
89986f8cdf3SVladimir Sementsov-Ogievskiy         error_propagate(&iter->err, *local_err);
90086f8cdf3SVladimir Sementsov-Ogievskiy     } else {
90186f8cdf3SVladimir Sementsov-Ogievskiy         error_free(*local_err);
90286f8cdf3SVladimir Sementsov-Ogievskiy     }
90386f8cdf3SVladimir Sementsov-Ogievskiy 
90486f8cdf3SVladimir Sementsov-Ogievskiy     *local_err = NULL;
90586f8cdf3SVladimir Sementsov-Ogievskiy }
90686f8cdf3SVladimir Sementsov-Ogievskiy 
90786f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_iter_request_error(NBDReplyChunkIter *iter, int ret)
90886f8cdf3SVladimir Sementsov-Ogievskiy {
90986f8cdf3SVladimir Sementsov-Ogievskiy     assert(ret < 0);
91086f8cdf3SVladimir Sementsov-Ogievskiy 
91186f8cdf3SVladimir Sementsov-Ogievskiy     if (!iter->request_ret) {
91286f8cdf3SVladimir Sementsov-Ogievskiy         iter->request_ret = ret;
91386f8cdf3SVladimir Sementsov-Ogievskiy     }
91486f8cdf3SVladimir Sementsov-Ogievskiy }
91586f8cdf3SVladimir Sementsov-Ogievskiy 
91686f8cdf3SVladimir Sementsov-Ogievskiy /*
91786f8cdf3SVladimir Sementsov-Ogievskiy  * NBD_FOREACH_REPLY_CHUNK
91886f8cdf3SVladimir Sementsov-Ogievskiy  * The pointer stored in @payload requires g_free() to free it.
91986f8cdf3SVladimir Sementsov-Ogievskiy  */
92086f8cdf3SVladimir Sementsov-Ogievskiy #define NBD_FOREACH_REPLY_CHUNK(s, iter, handle, structured, \
92186f8cdf3SVladimir Sementsov-Ogievskiy                                 qiov, reply, payload) \
92286f8cdf3SVladimir Sementsov-Ogievskiy     for (iter = (NBDReplyChunkIter) { .only_structured = structured }; \
92386f8cdf3SVladimir Sementsov-Ogievskiy          nbd_reply_chunk_iter_receive(s, &iter, handle, qiov, reply, payload);)
92486f8cdf3SVladimir Sementsov-Ogievskiy 
92586f8cdf3SVladimir Sementsov-Ogievskiy /*
92686f8cdf3SVladimir Sementsov-Ogievskiy  * nbd_reply_chunk_iter_receive
92786f8cdf3SVladimir Sementsov-Ogievskiy  * The pointer stored in @payload requires g_free() to free it.
92886f8cdf3SVladimir Sementsov-Ogievskiy  */
929611ae1d7SVladimir Sementsov-Ogievskiy static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s,
93086f8cdf3SVladimir Sementsov-Ogievskiy                                          NBDReplyChunkIter *iter,
93186f8cdf3SVladimir Sementsov-Ogievskiy                                          uint64_t handle,
93286f8cdf3SVladimir Sementsov-Ogievskiy                                          QEMUIOVector *qiov, NBDReply *reply,
93386f8cdf3SVladimir Sementsov-Ogievskiy                                          void **payload)
93486f8cdf3SVladimir Sementsov-Ogievskiy {
93586f8cdf3SVladimir Sementsov-Ogievskiy     int ret, request_ret;
93686f8cdf3SVladimir Sementsov-Ogievskiy     NBDReply local_reply;
93786f8cdf3SVladimir Sementsov-Ogievskiy     NBDStructuredReplyChunk *chunk;
93886f8cdf3SVladimir Sementsov-Ogievskiy     Error *local_err = NULL;
939a34b1e5eSVladimir Sementsov-Ogievskiy     if (s->state != NBD_CLIENT_CONNECTED) {
94086f8cdf3SVladimir Sementsov-Ogievskiy         error_setg(&local_err, "Connection closed");
94186f8cdf3SVladimir Sementsov-Ogievskiy         nbd_iter_channel_error(iter, -EIO, &local_err);
94286f8cdf3SVladimir Sementsov-Ogievskiy         goto break_loop;
94386f8cdf3SVladimir Sementsov-Ogievskiy     }
94486f8cdf3SVladimir Sementsov-Ogievskiy 
94586f8cdf3SVladimir Sementsov-Ogievskiy     if (iter->done) {
94686f8cdf3SVladimir Sementsov-Ogievskiy         /* Previous iteration was last. */
94786f8cdf3SVladimir Sementsov-Ogievskiy         goto break_loop;
94886f8cdf3SVladimir Sementsov-Ogievskiy     }
94986f8cdf3SVladimir Sementsov-Ogievskiy 
95086f8cdf3SVladimir Sementsov-Ogievskiy     if (reply == NULL) {
95186f8cdf3SVladimir Sementsov-Ogievskiy         reply = &local_reply;
95286f8cdf3SVladimir Sementsov-Ogievskiy     }
95386f8cdf3SVladimir Sementsov-Ogievskiy 
95486f8cdf3SVladimir Sementsov-Ogievskiy     ret = nbd_co_receive_one_chunk(s, handle, iter->only_structured,
95586f8cdf3SVladimir Sementsov-Ogievskiy                                    &request_ret, qiov, reply, payload,
95686f8cdf3SVladimir Sementsov-Ogievskiy                                    &local_err);
95786f8cdf3SVladimir Sementsov-Ogievskiy     if (ret < 0) {
95886f8cdf3SVladimir Sementsov-Ogievskiy         nbd_iter_channel_error(iter, ret, &local_err);
95986f8cdf3SVladimir Sementsov-Ogievskiy     } else if (request_ret < 0) {
96086f8cdf3SVladimir Sementsov-Ogievskiy         nbd_iter_request_error(iter, request_ret);
96186f8cdf3SVladimir Sementsov-Ogievskiy     }
96286f8cdf3SVladimir Sementsov-Ogievskiy 
96386f8cdf3SVladimir Sementsov-Ogievskiy     /* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */
964a34b1e5eSVladimir Sementsov-Ogievskiy     if (nbd_reply_is_simple(reply) || s->state != NBD_CLIENT_CONNECTED) {
96586f8cdf3SVladimir Sementsov-Ogievskiy         goto break_loop;
96686f8cdf3SVladimir Sementsov-Ogievskiy     }
96786f8cdf3SVladimir Sementsov-Ogievskiy 
96886f8cdf3SVladimir Sementsov-Ogievskiy     chunk = &reply->structured;
96986f8cdf3SVladimir Sementsov-Ogievskiy     iter->only_structured = true;
97086f8cdf3SVladimir Sementsov-Ogievskiy 
97186f8cdf3SVladimir Sementsov-Ogievskiy     if (chunk->type == NBD_REPLY_TYPE_NONE) {
97286f8cdf3SVladimir Sementsov-Ogievskiy         /* NBD_REPLY_FLAG_DONE is already checked in nbd_co_receive_one_chunk */
97386f8cdf3SVladimir Sementsov-Ogievskiy         assert(chunk->flags & NBD_REPLY_FLAG_DONE);
97486f8cdf3SVladimir Sementsov-Ogievskiy         goto break_loop;
97586f8cdf3SVladimir Sementsov-Ogievskiy     }
97686f8cdf3SVladimir Sementsov-Ogievskiy 
97786f8cdf3SVladimir Sementsov-Ogievskiy     if (chunk->flags & NBD_REPLY_FLAG_DONE) {
97886f8cdf3SVladimir Sementsov-Ogievskiy         /* This iteration is last. */
97986f8cdf3SVladimir Sementsov-Ogievskiy         iter->done = true;
98086f8cdf3SVladimir Sementsov-Ogievskiy     }
98186f8cdf3SVladimir Sementsov-Ogievskiy 
98286f8cdf3SVladimir Sementsov-Ogievskiy     /* Execute the loop body */
98386f8cdf3SVladimir Sementsov-Ogievskiy     return true;
98486f8cdf3SVladimir Sementsov-Ogievskiy 
98586f8cdf3SVladimir Sementsov-Ogievskiy break_loop:
98686f8cdf3SVladimir Sementsov-Ogievskiy     s->requests[HANDLE_TO_INDEX(s, handle)].coroutine = NULL;
98786f8cdf3SVladimir Sementsov-Ogievskiy 
98886f8cdf3SVladimir Sementsov-Ogievskiy     qemu_co_mutex_lock(&s->send_mutex);
98986f8cdf3SVladimir Sementsov-Ogievskiy     s->in_flight--;
990f7651539SVladimir Sementsov-Ogievskiy     if (s->in_flight == 0 && s->wait_in_flight) {
991f7651539SVladimir Sementsov-Ogievskiy         aio_co_wake(s->connection_co);
992f7651539SVladimir Sementsov-Ogievskiy     } else {
99386f8cdf3SVladimir Sementsov-Ogievskiy         qemu_co_queue_next(&s->free_sema);
994f7651539SVladimir Sementsov-Ogievskiy     }
99586f8cdf3SVladimir Sementsov-Ogievskiy     qemu_co_mutex_unlock(&s->send_mutex);
99686f8cdf3SVladimir Sementsov-Ogievskiy 
99786f8cdf3SVladimir Sementsov-Ogievskiy     return false;
99886f8cdf3SVladimir Sementsov-Ogievskiy }
99986f8cdf3SVladimir Sementsov-Ogievskiy 
1000611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_co_receive_return_code(BDRVNBDState *s, uint64_t handle,
100186f8cdf3SVladimir Sementsov-Ogievskiy                                       int *request_ret, Error **errp)
100286f8cdf3SVladimir Sementsov-Ogievskiy {
100386f8cdf3SVladimir Sementsov-Ogievskiy     NBDReplyChunkIter iter;
100486f8cdf3SVladimir Sementsov-Ogievskiy 
100586f8cdf3SVladimir Sementsov-Ogievskiy     NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, NULL, NULL) {
100686f8cdf3SVladimir Sementsov-Ogievskiy         /* nbd_reply_chunk_iter_receive does all the work */
100786f8cdf3SVladimir Sementsov-Ogievskiy     }
100886f8cdf3SVladimir Sementsov-Ogievskiy 
100986f8cdf3SVladimir Sementsov-Ogievskiy     error_propagate(errp, iter.err);
101086f8cdf3SVladimir Sementsov-Ogievskiy     *request_ret = iter.request_ret;
101186f8cdf3SVladimir Sementsov-Ogievskiy     return iter.ret;
101286f8cdf3SVladimir Sementsov-Ogievskiy }
101386f8cdf3SVladimir Sementsov-Ogievskiy 
1014611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_t handle,
101586f8cdf3SVladimir Sementsov-Ogievskiy                                         uint64_t offset, QEMUIOVector *qiov,
101686f8cdf3SVladimir Sementsov-Ogievskiy                                         int *request_ret, Error **errp)
101786f8cdf3SVladimir Sementsov-Ogievskiy {
101886f8cdf3SVladimir Sementsov-Ogievskiy     NBDReplyChunkIter iter;
101986f8cdf3SVladimir Sementsov-Ogievskiy     NBDReply reply;
102086f8cdf3SVladimir Sementsov-Ogievskiy     void *payload = NULL;
102186f8cdf3SVladimir Sementsov-Ogievskiy     Error *local_err = NULL;
102286f8cdf3SVladimir Sementsov-Ogievskiy 
102386f8cdf3SVladimir Sementsov-Ogievskiy     NBD_FOREACH_REPLY_CHUNK(s, iter, handle, s->info.structured_reply,
102486f8cdf3SVladimir Sementsov-Ogievskiy                             qiov, &reply, &payload)
102586f8cdf3SVladimir Sementsov-Ogievskiy     {
102686f8cdf3SVladimir Sementsov-Ogievskiy         int ret;
102786f8cdf3SVladimir Sementsov-Ogievskiy         NBDStructuredReplyChunk *chunk = &reply.structured;
102886f8cdf3SVladimir Sementsov-Ogievskiy 
102986f8cdf3SVladimir Sementsov-Ogievskiy         assert(nbd_reply_is_structured(&reply));
103086f8cdf3SVladimir Sementsov-Ogievskiy 
103186f8cdf3SVladimir Sementsov-Ogievskiy         switch (chunk->type) {
103286f8cdf3SVladimir Sementsov-Ogievskiy         case NBD_REPLY_TYPE_OFFSET_DATA:
103386f8cdf3SVladimir Sementsov-Ogievskiy             /*
103486f8cdf3SVladimir Sementsov-Ogievskiy              * special cased in nbd_co_receive_one_chunk, data is already
103586f8cdf3SVladimir Sementsov-Ogievskiy              * in qiov
103686f8cdf3SVladimir Sementsov-Ogievskiy              */
103786f8cdf3SVladimir Sementsov-Ogievskiy             break;
103886f8cdf3SVladimir Sementsov-Ogievskiy         case NBD_REPLY_TYPE_OFFSET_HOLE:
103986f8cdf3SVladimir Sementsov-Ogievskiy             ret = nbd_parse_offset_hole_payload(s, &reply.structured, payload,
104086f8cdf3SVladimir Sementsov-Ogievskiy                                                 offset, qiov, &local_err);
104186f8cdf3SVladimir Sementsov-Ogievskiy             if (ret < 0) {
1042a34b1e5eSVladimir Sementsov-Ogievskiy                 nbd_channel_error(s, ret);
104386f8cdf3SVladimir Sementsov-Ogievskiy                 nbd_iter_channel_error(&iter, ret, &local_err);
104486f8cdf3SVladimir Sementsov-Ogievskiy             }
104586f8cdf3SVladimir Sementsov-Ogievskiy             break;
104686f8cdf3SVladimir Sementsov-Ogievskiy         default:
104786f8cdf3SVladimir Sementsov-Ogievskiy             if (!nbd_reply_type_is_error(chunk->type)) {
104886f8cdf3SVladimir Sementsov-Ogievskiy                 /* not allowed reply type */
1049a34b1e5eSVladimir Sementsov-Ogievskiy                 nbd_channel_error(s, -EINVAL);
105086f8cdf3SVladimir Sementsov-Ogievskiy                 error_setg(&local_err,
105186f8cdf3SVladimir Sementsov-Ogievskiy                            "Unexpected reply type: %d (%s) for CMD_READ",
105286f8cdf3SVladimir Sementsov-Ogievskiy                            chunk->type, nbd_reply_type_lookup(chunk->type));
105386f8cdf3SVladimir Sementsov-Ogievskiy                 nbd_iter_channel_error(&iter, -EINVAL, &local_err);
105486f8cdf3SVladimir Sementsov-Ogievskiy             }
105586f8cdf3SVladimir Sementsov-Ogievskiy         }
105686f8cdf3SVladimir Sementsov-Ogievskiy 
105786f8cdf3SVladimir Sementsov-Ogievskiy         g_free(payload);
105886f8cdf3SVladimir Sementsov-Ogievskiy         payload = NULL;
105986f8cdf3SVladimir Sementsov-Ogievskiy     }
106086f8cdf3SVladimir Sementsov-Ogievskiy 
106186f8cdf3SVladimir Sementsov-Ogievskiy     error_propagate(errp, iter.err);
106286f8cdf3SVladimir Sementsov-Ogievskiy     *request_ret = iter.request_ret;
106386f8cdf3SVladimir Sementsov-Ogievskiy     return iter.ret;
106486f8cdf3SVladimir Sementsov-Ogievskiy }
106586f8cdf3SVladimir Sementsov-Ogievskiy 
1066611ae1d7SVladimir Sementsov-Ogievskiy static int nbd_co_receive_blockstatus_reply(BDRVNBDState *s,
106786f8cdf3SVladimir Sementsov-Ogievskiy                                             uint64_t handle, uint64_t length,
106886f8cdf3SVladimir Sementsov-Ogievskiy                                             NBDExtent *extent,
106986f8cdf3SVladimir Sementsov-Ogievskiy                                             int *request_ret, Error **errp)
107086f8cdf3SVladimir Sementsov-Ogievskiy {
107186f8cdf3SVladimir Sementsov-Ogievskiy     NBDReplyChunkIter iter;
107286f8cdf3SVladimir Sementsov-Ogievskiy     NBDReply reply;
107386f8cdf3SVladimir Sementsov-Ogievskiy     void *payload = NULL;
107486f8cdf3SVladimir Sementsov-Ogievskiy     Error *local_err = NULL;
107586f8cdf3SVladimir Sementsov-Ogievskiy     bool received = false;
107686f8cdf3SVladimir Sementsov-Ogievskiy 
107786f8cdf3SVladimir Sementsov-Ogievskiy     assert(!extent->length);
107886f8cdf3SVladimir Sementsov-Ogievskiy     NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, &reply, &payload) {
107986f8cdf3SVladimir Sementsov-Ogievskiy         int ret;
108086f8cdf3SVladimir Sementsov-Ogievskiy         NBDStructuredReplyChunk *chunk = &reply.structured;
108186f8cdf3SVladimir Sementsov-Ogievskiy 
108286f8cdf3SVladimir Sementsov-Ogievskiy         assert(nbd_reply_is_structured(&reply));
108386f8cdf3SVladimir Sementsov-Ogievskiy 
108486f8cdf3SVladimir Sementsov-Ogievskiy         switch (chunk->type) {
108586f8cdf3SVladimir Sementsov-Ogievskiy         case NBD_REPLY_TYPE_BLOCK_STATUS:
108686f8cdf3SVladimir Sementsov-Ogievskiy             if (received) {
1087a34b1e5eSVladimir Sementsov-Ogievskiy                 nbd_channel_error(s, -EINVAL);
108886f8cdf3SVladimir Sementsov-Ogievskiy                 error_setg(&local_err, "Several BLOCK_STATUS chunks in reply");
108986f8cdf3SVladimir Sementsov-Ogievskiy                 nbd_iter_channel_error(&iter, -EINVAL, &local_err);
109086f8cdf3SVladimir Sementsov-Ogievskiy             }
109186f8cdf3SVladimir Sementsov-Ogievskiy             received = true;
109286f8cdf3SVladimir Sementsov-Ogievskiy 
109386f8cdf3SVladimir Sementsov-Ogievskiy             ret = nbd_parse_blockstatus_payload(s, &reply.structured,
109486f8cdf3SVladimir Sementsov-Ogievskiy                                                 payload, length, extent,
109586f8cdf3SVladimir Sementsov-Ogievskiy                                                 &local_err);
109686f8cdf3SVladimir Sementsov-Ogievskiy             if (ret < 0) {
1097a34b1e5eSVladimir Sementsov-Ogievskiy                 nbd_channel_error(s, ret);
109886f8cdf3SVladimir Sementsov-Ogievskiy                 nbd_iter_channel_error(&iter, ret, &local_err);
109986f8cdf3SVladimir Sementsov-Ogievskiy             }
110086f8cdf3SVladimir Sementsov-Ogievskiy             break;
110186f8cdf3SVladimir Sementsov-Ogievskiy         default:
110286f8cdf3SVladimir Sementsov-Ogievskiy             if (!nbd_reply_type_is_error(chunk->type)) {
1103a34b1e5eSVladimir Sementsov-Ogievskiy                 nbd_channel_error(s, -EINVAL);
110486f8cdf3SVladimir Sementsov-Ogievskiy                 error_setg(&local_err,
110586f8cdf3SVladimir Sementsov-Ogievskiy                            "Unexpected reply type: %d (%s) "
110686f8cdf3SVladimir Sementsov-Ogievskiy                            "for CMD_BLOCK_STATUS",
110786f8cdf3SVladimir Sementsov-Ogievskiy                            chunk->type, nbd_reply_type_lookup(chunk->type));
110886f8cdf3SVladimir Sementsov-Ogievskiy                 nbd_iter_channel_error(&iter, -EINVAL, &local_err);
110986f8cdf3SVladimir Sementsov-Ogievskiy             }
111086f8cdf3SVladimir Sementsov-Ogievskiy         }
111186f8cdf3SVladimir Sementsov-Ogievskiy 
111286f8cdf3SVladimir Sementsov-Ogievskiy         g_free(payload);
111386f8cdf3SVladimir Sementsov-Ogievskiy         payload = NULL;
111486f8cdf3SVladimir Sementsov-Ogievskiy     }
111586f8cdf3SVladimir Sementsov-Ogievskiy 
111686f8cdf3SVladimir Sementsov-Ogievskiy     if (!extent->length && !iter.request_ret) {
111786f8cdf3SVladimir Sementsov-Ogievskiy         error_setg(&local_err, "Server did not reply with any status extents");
111886f8cdf3SVladimir Sementsov-Ogievskiy         nbd_iter_channel_error(&iter, -EIO, &local_err);
111986f8cdf3SVladimir Sementsov-Ogievskiy     }
112086f8cdf3SVladimir Sementsov-Ogievskiy 
112186f8cdf3SVladimir Sementsov-Ogievskiy     error_propagate(errp, iter.err);
112286f8cdf3SVladimir Sementsov-Ogievskiy     *request_ret = iter.request_ret;
112386f8cdf3SVladimir Sementsov-Ogievskiy     return iter.ret;
112486f8cdf3SVladimir Sementsov-Ogievskiy }
112586f8cdf3SVladimir Sementsov-Ogievskiy 
112686f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_co_request(BlockDriverState *bs, NBDRequest *request,
112786f8cdf3SVladimir Sementsov-Ogievskiy                           QEMUIOVector *write_qiov)
112886f8cdf3SVladimir Sementsov-Ogievskiy {
112986f8cdf3SVladimir Sementsov-Ogievskiy     int ret, request_ret;
113086f8cdf3SVladimir Sementsov-Ogievskiy     Error *local_err = NULL;
1131611ae1d7SVladimir Sementsov-Ogievskiy     BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
113286f8cdf3SVladimir Sementsov-Ogievskiy 
113386f8cdf3SVladimir Sementsov-Ogievskiy     assert(request->type != NBD_CMD_READ);
113486f8cdf3SVladimir Sementsov-Ogievskiy     if (write_qiov) {
113586f8cdf3SVladimir Sementsov-Ogievskiy         assert(request->type == NBD_CMD_WRITE);
113686f8cdf3SVladimir Sementsov-Ogievskiy         assert(request->len == iov_size(write_qiov->iov, write_qiov->niov));
113786f8cdf3SVladimir Sementsov-Ogievskiy     } else {
113886f8cdf3SVladimir Sementsov-Ogievskiy         assert(request->type != NBD_CMD_WRITE);
113986f8cdf3SVladimir Sementsov-Ogievskiy     }
1140f7651539SVladimir Sementsov-Ogievskiy 
1141f7651539SVladimir Sementsov-Ogievskiy     do {
114286f8cdf3SVladimir Sementsov-Ogievskiy         ret = nbd_co_send_request(bs, request, write_qiov);
114386f8cdf3SVladimir Sementsov-Ogievskiy         if (ret < 0) {
1144f7651539SVladimir Sementsov-Ogievskiy             continue;
114586f8cdf3SVladimir Sementsov-Ogievskiy         }
114686f8cdf3SVladimir Sementsov-Ogievskiy 
1147611ae1d7SVladimir Sementsov-Ogievskiy         ret = nbd_co_receive_return_code(s, request->handle,
114886f8cdf3SVladimir Sementsov-Ogievskiy                                          &request_ret, &local_err);
114986f8cdf3SVladimir Sementsov-Ogievskiy         if (local_err) {
1150f7651539SVladimir Sementsov-Ogievskiy             trace_nbd_co_request_fail(request->from, request->len,
1151f7651539SVladimir Sementsov-Ogievskiy                                       request->handle, request->flags,
1152f7651539SVladimir Sementsov-Ogievskiy                                       request->type,
115386f8cdf3SVladimir Sementsov-Ogievskiy                                       nbd_cmd_lookup(request->type),
115486f8cdf3SVladimir Sementsov-Ogievskiy                                       ret, error_get_pretty(local_err));
115586f8cdf3SVladimir Sementsov-Ogievskiy             error_free(local_err);
1156f7651539SVladimir Sementsov-Ogievskiy             local_err = NULL;
115786f8cdf3SVladimir Sementsov-Ogievskiy         }
1158f7651539SVladimir Sementsov-Ogievskiy     } while (ret < 0 && nbd_client_connecting_wait(s));
1159f7651539SVladimir Sementsov-Ogievskiy 
116086f8cdf3SVladimir Sementsov-Ogievskiy     return ret ? ret : request_ret;
116186f8cdf3SVladimir Sementsov-Ogievskiy }
116286f8cdf3SVladimir Sementsov-Ogievskiy 
116386f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_client_co_preadv(BlockDriverState *bs, uint64_t offset,
116486f8cdf3SVladimir Sementsov-Ogievskiy                                 uint64_t bytes, QEMUIOVector *qiov, int flags)
116586f8cdf3SVladimir Sementsov-Ogievskiy {
116686f8cdf3SVladimir Sementsov-Ogievskiy     int ret, request_ret;
116786f8cdf3SVladimir Sementsov-Ogievskiy     Error *local_err = NULL;
1168611ae1d7SVladimir Sementsov-Ogievskiy     BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
116986f8cdf3SVladimir Sementsov-Ogievskiy     NBDRequest request = {
117086f8cdf3SVladimir Sementsov-Ogievskiy         .type = NBD_CMD_READ,
117186f8cdf3SVladimir Sementsov-Ogievskiy         .from = offset,
117286f8cdf3SVladimir Sementsov-Ogievskiy         .len = bytes,
117386f8cdf3SVladimir Sementsov-Ogievskiy     };
117486f8cdf3SVladimir Sementsov-Ogievskiy 
117586f8cdf3SVladimir Sementsov-Ogievskiy     assert(bytes <= NBD_MAX_BUFFER_SIZE);
117686f8cdf3SVladimir Sementsov-Ogievskiy     assert(!flags);
117786f8cdf3SVladimir Sementsov-Ogievskiy 
117886f8cdf3SVladimir Sementsov-Ogievskiy     if (!bytes) {
117986f8cdf3SVladimir Sementsov-Ogievskiy         return 0;
118086f8cdf3SVladimir Sementsov-Ogievskiy     }
118186f8cdf3SVladimir Sementsov-Ogievskiy     /*
118286f8cdf3SVladimir Sementsov-Ogievskiy      * Work around the fact that the block layer doesn't do
118386f8cdf3SVladimir Sementsov-Ogievskiy      * byte-accurate sizing yet - if the read exceeds the server's
118486f8cdf3SVladimir Sementsov-Ogievskiy      * advertised size because the block layer rounded size up, then
118586f8cdf3SVladimir Sementsov-Ogievskiy      * truncate the request to the server and tail-pad with zero.
118686f8cdf3SVladimir Sementsov-Ogievskiy      */
1187611ae1d7SVladimir Sementsov-Ogievskiy     if (offset >= s->info.size) {
118886f8cdf3SVladimir Sementsov-Ogievskiy         assert(bytes < BDRV_SECTOR_SIZE);
118986f8cdf3SVladimir Sementsov-Ogievskiy         qemu_iovec_memset(qiov, 0, 0, bytes);
119086f8cdf3SVladimir Sementsov-Ogievskiy         return 0;
119186f8cdf3SVladimir Sementsov-Ogievskiy     }
1192611ae1d7SVladimir Sementsov-Ogievskiy     if (offset + bytes > s->info.size) {
1193611ae1d7SVladimir Sementsov-Ogievskiy         uint64_t slop = offset + bytes - s->info.size;
119486f8cdf3SVladimir Sementsov-Ogievskiy 
119586f8cdf3SVladimir Sementsov-Ogievskiy         assert(slop < BDRV_SECTOR_SIZE);
119686f8cdf3SVladimir Sementsov-Ogievskiy         qemu_iovec_memset(qiov, bytes - slop, 0, slop);
119786f8cdf3SVladimir Sementsov-Ogievskiy         request.len -= slop;
119886f8cdf3SVladimir Sementsov-Ogievskiy     }
119986f8cdf3SVladimir Sementsov-Ogievskiy 
1200f7651539SVladimir Sementsov-Ogievskiy     do {
120186f8cdf3SVladimir Sementsov-Ogievskiy         ret = nbd_co_send_request(bs, &request, NULL);
120286f8cdf3SVladimir Sementsov-Ogievskiy         if (ret < 0) {
1203f7651539SVladimir Sementsov-Ogievskiy             continue;
120486f8cdf3SVladimir Sementsov-Ogievskiy         }
120586f8cdf3SVladimir Sementsov-Ogievskiy 
1206611ae1d7SVladimir Sementsov-Ogievskiy         ret = nbd_co_receive_cmdread_reply(s, request.handle, offset, qiov,
120786f8cdf3SVladimir Sementsov-Ogievskiy                                            &request_ret, &local_err);
120886f8cdf3SVladimir Sementsov-Ogievskiy         if (local_err) {
120986f8cdf3SVladimir Sementsov-Ogievskiy             trace_nbd_co_request_fail(request.from, request.len, request.handle,
121086f8cdf3SVladimir Sementsov-Ogievskiy                                       request.flags, request.type,
121186f8cdf3SVladimir Sementsov-Ogievskiy                                       nbd_cmd_lookup(request.type),
121286f8cdf3SVladimir Sementsov-Ogievskiy                                       ret, error_get_pretty(local_err));
121386f8cdf3SVladimir Sementsov-Ogievskiy             error_free(local_err);
1214f7651539SVladimir Sementsov-Ogievskiy             local_err = NULL;
121586f8cdf3SVladimir Sementsov-Ogievskiy         }
1216f7651539SVladimir Sementsov-Ogievskiy     } while (ret < 0 && nbd_client_connecting_wait(s));
1217f7651539SVladimir Sementsov-Ogievskiy 
121886f8cdf3SVladimir Sementsov-Ogievskiy     return ret ? ret : request_ret;
121986f8cdf3SVladimir Sementsov-Ogievskiy }
122086f8cdf3SVladimir Sementsov-Ogievskiy 
122186f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_client_co_pwritev(BlockDriverState *bs, uint64_t offset,
122286f8cdf3SVladimir Sementsov-Ogievskiy                                  uint64_t bytes, QEMUIOVector *qiov, int flags)
122386f8cdf3SVladimir Sementsov-Ogievskiy {
1224611ae1d7SVladimir Sementsov-Ogievskiy     BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
122586f8cdf3SVladimir Sementsov-Ogievskiy     NBDRequest request = {
122686f8cdf3SVladimir Sementsov-Ogievskiy         .type = NBD_CMD_WRITE,
122786f8cdf3SVladimir Sementsov-Ogievskiy         .from = offset,
122886f8cdf3SVladimir Sementsov-Ogievskiy         .len = bytes,
122986f8cdf3SVladimir Sementsov-Ogievskiy     };
123086f8cdf3SVladimir Sementsov-Ogievskiy 
1231611ae1d7SVladimir Sementsov-Ogievskiy     assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
123286f8cdf3SVladimir Sementsov-Ogievskiy     if (flags & BDRV_REQ_FUA) {
1233611ae1d7SVladimir Sementsov-Ogievskiy         assert(s->info.flags & NBD_FLAG_SEND_FUA);
123486f8cdf3SVladimir Sementsov-Ogievskiy         request.flags |= NBD_CMD_FLAG_FUA;
123586f8cdf3SVladimir Sementsov-Ogievskiy     }
123686f8cdf3SVladimir Sementsov-Ogievskiy 
123786f8cdf3SVladimir Sementsov-Ogievskiy     assert(bytes <= NBD_MAX_BUFFER_SIZE);
123886f8cdf3SVladimir Sementsov-Ogievskiy 
123986f8cdf3SVladimir Sementsov-Ogievskiy     if (!bytes) {
124086f8cdf3SVladimir Sementsov-Ogievskiy         return 0;
124186f8cdf3SVladimir Sementsov-Ogievskiy     }
124286f8cdf3SVladimir Sementsov-Ogievskiy     return nbd_co_request(bs, &request, qiov);
124386f8cdf3SVladimir Sementsov-Ogievskiy }
124486f8cdf3SVladimir Sementsov-Ogievskiy 
124586f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_client_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
124686f8cdf3SVladimir Sementsov-Ogievskiy                                        int bytes, BdrvRequestFlags flags)
124786f8cdf3SVladimir Sementsov-Ogievskiy {
1248611ae1d7SVladimir Sementsov-Ogievskiy     BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
124986f8cdf3SVladimir Sementsov-Ogievskiy     NBDRequest request = {
125086f8cdf3SVladimir Sementsov-Ogievskiy         .type = NBD_CMD_WRITE_ZEROES,
125186f8cdf3SVladimir Sementsov-Ogievskiy         .from = offset,
125286f8cdf3SVladimir Sementsov-Ogievskiy         .len = bytes,
125386f8cdf3SVladimir Sementsov-Ogievskiy     };
125486f8cdf3SVladimir Sementsov-Ogievskiy 
1255611ae1d7SVladimir Sementsov-Ogievskiy     assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1256611ae1d7SVladimir Sementsov-Ogievskiy     if (!(s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES)) {
125786f8cdf3SVladimir Sementsov-Ogievskiy         return -ENOTSUP;
125886f8cdf3SVladimir Sementsov-Ogievskiy     }
125986f8cdf3SVladimir Sementsov-Ogievskiy 
126086f8cdf3SVladimir Sementsov-Ogievskiy     if (flags & BDRV_REQ_FUA) {
1261611ae1d7SVladimir Sementsov-Ogievskiy         assert(s->info.flags & NBD_FLAG_SEND_FUA);
126286f8cdf3SVladimir Sementsov-Ogievskiy         request.flags |= NBD_CMD_FLAG_FUA;
126386f8cdf3SVladimir Sementsov-Ogievskiy     }
126486f8cdf3SVladimir Sementsov-Ogievskiy     if (!(flags & BDRV_REQ_MAY_UNMAP)) {
126586f8cdf3SVladimir Sementsov-Ogievskiy         request.flags |= NBD_CMD_FLAG_NO_HOLE;
126686f8cdf3SVladimir Sementsov-Ogievskiy     }
1267f061656cSEric Blake     if (flags & BDRV_REQ_NO_FALLBACK) {
1268f061656cSEric Blake         assert(s->info.flags & NBD_FLAG_SEND_FAST_ZERO);
1269f061656cSEric Blake         request.flags |= NBD_CMD_FLAG_FAST_ZERO;
1270f061656cSEric Blake     }
127186f8cdf3SVladimir Sementsov-Ogievskiy 
127286f8cdf3SVladimir Sementsov-Ogievskiy     if (!bytes) {
127386f8cdf3SVladimir Sementsov-Ogievskiy         return 0;
127486f8cdf3SVladimir Sementsov-Ogievskiy     }
127586f8cdf3SVladimir Sementsov-Ogievskiy     return nbd_co_request(bs, &request, NULL);
127686f8cdf3SVladimir Sementsov-Ogievskiy }
127786f8cdf3SVladimir Sementsov-Ogievskiy 
127886f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_client_co_flush(BlockDriverState *bs)
127986f8cdf3SVladimir Sementsov-Ogievskiy {
1280611ae1d7SVladimir Sementsov-Ogievskiy     BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
128186f8cdf3SVladimir Sementsov-Ogievskiy     NBDRequest request = { .type = NBD_CMD_FLUSH };
128286f8cdf3SVladimir Sementsov-Ogievskiy 
1283611ae1d7SVladimir Sementsov-Ogievskiy     if (!(s->info.flags & NBD_FLAG_SEND_FLUSH)) {
128486f8cdf3SVladimir Sementsov-Ogievskiy         return 0;
128586f8cdf3SVladimir Sementsov-Ogievskiy     }
128686f8cdf3SVladimir Sementsov-Ogievskiy 
128786f8cdf3SVladimir Sementsov-Ogievskiy     request.from = 0;
128886f8cdf3SVladimir Sementsov-Ogievskiy     request.len = 0;
128986f8cdf3SVladimir Sementsov-Ogievskiy 
129086f8cdf3SVladimir Sementsov-Ogievskiy     return nbd_co_request(bs, &request, NULL);
129186f8cdf3SVladimir Sementsov-Ogievskiy }
129286f8cdf3SVladimir Sementsov-Ogievskiy 
129386f8cdf3SVladimir Sementsov-Ogievskiy static int nbd_client_co_pdiscard(BlockDriverState *bs, int64_t offset,
129486f8cdf3SVladimir Sementsov-Ogievskiy                                   int bytes)
129586f8cdf3SVladimir Sementsov-Ogievskiy {
1296611ae1d7SVladimir Sementsov-Ogievskiy     BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
129786f8cdf3SVladimir Sementsov-Ogievskiy     NBDRequest request = {
129886f8cdf3SVladimir Sementsov-Ogievskiy         .type = NBD_CMD_TRIM,
129986f8cdf3SVladimir Sementsov-Ogievskiy         .from = offset,
130086f8cdf3SVladimir Sementsov-Ogievskiy         .len = bytes,
130186f8cdf3SVladimir Sementsov-Ogievskiy     };
130286f8cdf3SVladimir Sementsov-Ogievskiy 
1303611ae1d7SVladimir Sementsov-Ogievskiy     assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1304611ae1d7SVladimir Sementsov-Ogievskiy     if (!(s->info.flags & NBD_FLAG_SEND_TRIM) || !bytes) {
130586f8cdf3SVladimir Sementsov-Ogievskiy         return 0;
130686f8cdf3SVladimir Sementsov-Ogievskiy     }
130786f8cdf3SVladimir Sementsov-Ogievskiy 
130886f8cdf3SVladimir Sementsov-Ogievskiy     return nbd_co_request(bs, &request, NULL);
130986f8cdf3SVladimir Sementsov-Ogievskiy }
131086f8cdf3SVladimir Sementsov-Ogievskiy 
131186f8cdf3SVladimir Sementsov-Ogievskiy static int coroutine_fn nbd_client_co_block_status(
131286f8cdf3SVladimir Sementsov-Ogievskiy         BlockDriverState *bs, bool want_zero, int64_t offset, int64_t bytes,
131386f8cdf3SVladimir Sementsov-Ogievskiy         int64_t *pnum, int64_t *map, BlockDriverState **file)
131486f8cdf3SVladimir Sementsov-Ogievskiy {
131586f8cdf3SVladimir Sementsov-Ogievskiy     int ret, request_ret;
131686f8cdf3SVladimir Sementsov-Ogievskiy     NBDExtent extent = { 0 };
1317611ae1d7SVladimir Sementsov-Ogievskiy     BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
131886f8cdf3SVladimir Sementsov-Ogievskiy     Error *local_err = NULL;
131986f8cdf3SVladimir Sementsov-Ogievskiy 
132086f8cdf3SVladimir Sementsov-Ogievskiy     NBDRequest request = {
132186f8cdf3SVladimir Sementsov-Ogievskiy         .type = NBD_CMD_BLOCK_STATUS,
132286f8cdf3SVladimir Sementsov-Ogievskiy         .from = offset,
132386f8cdf3SVladimir Sementsov-Ogievskiy         .len = MIN(MIN_NON_ZERO(QEMU_ALIGN_DOWN(INT_MAX,
132486f8cdf3SVladimir Sementsov-Ogievskiy                                                 bs->bl.request_alignment),
1325611ae1d7SVladimir Sementsov-Ogievskiy                                 s->info.max_block),
1326611ae1d7SVladimir Sementsov-Ogievskiy                    MIN(bytes, s->info.size - offset)),
132786f8cdf3SVladimir Sementsov-Ogievskiy         .flags = NBD_CMD_FLAG_REQ_ONE,
132886f8cdf3SVladimir Sementsov-Ogievskiy     };
132986f8cdf3SVladimir Sementsov-Ogievskiy 
1330611ae1d7SVladimir Sementsov-Ogievskiy     if (!s->info.base_allocation) {
133186f8cdf3SVladimir Sementsov-Ogievskiy         *pnum = bytes;
133286f8cdf3SVladimir Sementsov-Ogievskiy         *map = offset;
133386f8cdf3SVladimir Sementsov-Ogievskiy         *file = bs;
133486f8cdf3SVladimir Sementsov-Ogievskiy         return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
133586f8cdf3SVladimir Sementsov-Ogievskiy     }
133686f8cdf3SVladimir Sementsov-Ogievskiy 
133786f8cdf3SVladimir Sementsov-Ogievskiy     /*
133886f8cdf3SVladimir Sementsov-Ogievskiy      * Work around the fact that the block layer doesn't do
133986f8cdf3SVladimir Sementsov-Ogievskiy      * byte-accurate sizing yet - if the status request exceeds the
134086f8cdf3SVladimir Sementsov-Ogievskiy      * server's advertised size because the block layer rounded size
134186f8cdf3SVladimir Sementsov-Ogievskiy      * up, we truncated the request to the server (above), or are
134286f8cdf3SVladimir Sementsov-Ogievskiy      * called on just the hole.
134386f8cdf3SVladimir Sementsov-Ogievskiy      */
1344611ae1d7SVladimir Sementsov-Ogievskiy     if (offset >= s->info.size) {
134586f8cdf3SVladimir Sementsov-Ogievskiy         *pnum = bytes;
134686f8cdf3SVladimir Sementsov-Ogievskiy         assert(bytes < BDRV_SECTOR_SIZE);
134786f8cdf3SVladimir Sementsov-Ogievskiy         /* Intentionally don't report offset_valid for the hole */
134886f8cdf3SVladimir Sementsov-Ogievskiy         return BDRV_BLOCK_ZERO;
134986f8cdf3SVladimir Sementsov-Ogievskiy     }
135086f8cdf3SVladimir Sementsov-Ogievskiy 
1351611ae1d7SVladimir Sementsov-Ogievskiy     if (s->info.min_block) {
1352611ae1d7SVladimir Sementsov-Ogievskiy         assert(QEMU_IS_ALIGNED(request.len, s->info.min_block));
135386f8cdf3SVladimir Sementsov-Ogievskiy     }
1354f7651539SVladimir Sementsov-Ogievskiy     do {
135586f8cdf3SVladimir Sementsov-Ogievskiy         ret = nbd_co_send_request(bs, &request, NULL);
135686f8cdf3SVladimir Sementsov-Ogievskiy         if (ret < 0) {
1357f7651539SVladimir Sementsov-Ogievskiy             continue;
135886f8cdf3SVladimir Sementsov-Ogievskiy         }
135986f8cdf3SVladimir Sementsov-Ogievskiy 
1360611ae1d7SVladimir Sementsov-Ogievskiy         ret = nbd_co_receive_blockstatus_reply(s, request.handle, bytes,
1361f7651539SVladimir Sementsov-Ogievskiy                                                &extent, &request_ret,
1362f7651539SVladimir Sementsov-Ogievskiy                                                &local_err);
136386f8cdf3SVladimir Sementsov-Ogievskiy         if (local_err) {
136486f8cdf3SVladimir Sementsov-Ogievskiy             trace_nbd_co_request_fail(request.from, request.len, request.handle,
136586f8cdf3SVladimir Sementsov-Ogievskiy                                       request.flags, request.type,
136686f8cdf3SVladimir Sementsov-Ogievskiy                                       nbd_cmd_lookup(request.type),
136786f8cdf3SVladimir Sementsov-Ogievskiy                                       ret, error_get_pretty(local_err));
136886f8cdf3SVladimir Sementsov-Ogievskiy             error_free(local_err);
1369f7651539SVladimir Sementsov-Ogievskiy             local_err = NULL;
137086f8cdf3SVladimir Sementsov-Ogievskiy         }
1371f7651539SVladimir Sementsov-Ogievskiy     } while (ret < 0 && nbd_client_connecting_wait(s));
1372f7651539SVladimir Sementsov-Ogievskiy 
137386f8cdf3SVladimir Sementsov-Ogievskiy     if (ret < 0 || request_ret < 0) {
137486f8cdf3SVladimir Sementsov-Ogievskiy         return ret ? ret : request_ret;
137586f8cdf3SVladimir Sementsov-Ogievskiy     }
137686f8cdf3SVladimir Sementsov-Ogievskiy 
137786f8cdf3SVladimir Sementsov-Ogievskiy     assert(extent.length);
137886f8cdf3SVladimir Sementsov-Ogievskiy     *pnum = extent.length;
137986f8cdf3SVladimir Sementsov-Ogievskiy     *map = offset;
138086f8cdf3SVladimir Sementsov-Ogievskiy     *file = bs;
138186f8cdf3SVladimir Sementsov-Ogievskiy     return (extent.flags & NBD_STATE_HOLE ? 0 : BDRV_BLOCK_DATA) |
138286f8cdf3SVladimir Sementsov-Ogievskiy         (extent.flags & NBD_STATE_ZERO ? BDRV_BLOCK_ZERO : 0) |
138386f8cdf3SVladimir Sementsov-Ogievskiy         BDRV_BLOCK_OFFSET_VALID;
138486f8cdf3SVladimir Sementsov-Ogievskiy }
138586f8cdf3SVladimir Sementsov-Ogievskiy 
1386e99754b4SMaxim Levitsky static int nbd_client_reopen_prepare(BDRVReopenState *state,
1387e99754b4SMaxim Levitsky                                      BlockReopenQueue *queue, Error **errp)
1388e99754b4SMaxim Levitsky {
1389e99754b4SMaxim Levitsky     BDRVNBDState *s = (BDRVNBDState *)state->bs->opaque;
1390e99754b4SMaxim Levitsky 
1391e99754b4SMaxim Levitsky     if ((state->flags & BDRV_O_RDWR) && (s->info.flags & NBD_FLAG_READ_ONLY)) {
1392e99754b4SMaxim Levitsky         error_setg(errp, "Can't reopen read-only NBD mount as read/write");
1393e99754b4SMaxim Levitsky         return -EACCES;
1394e99754b4SMaxim Levitsky     }
1395e99754b4SMaxim Levitsky     return 0;
1396e99754b4SMaxim Levitsky }
1397e99754b4SMaxim Levitsky 
139886f8cdf3SVladimir Sementsov-Ogievskiy static void nbd_client_close(BlockDriverState *bs)
139986f8cdf3SVladimir Sementsov-Ogievskiy {
1400611ae1d7SVladimir Sementsov-Ogievskiy     BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
140186f8cdf3SVladimir Sementsov-Ogievskiy     NBDRequest request = { .type = NBD_CMD_DISC };
140286f8cdf3SVladimir Sementsov-Ogievskiy 
1403f7651539SVladimir Sementsov-Ogievskiy     if (s->ioc) {
1404611ae1d7SVladimir Sementsov-Ogievskiy         nbd_send_request(s->ioc, &request);
1405f7651539SVladimir Sementsov-Ogievskiy     }
140686f8cdf3SVladimir Sementsov-Ogievskiy 
140786f8cdf3SVladimir Sementsov-Ogievskiy     nbd_teardown_connection(bs);
140886f8cdf3SVladimir Sementsov-Ogievskiy }
140986f8cdf3SVladimir Sementsov-Ogievskiy 
141086f8cdf3SVladimir Sementsov-Ogievskiy static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr,
141186f8cdf3SVladimir Sementsov-Ogievskiy                                                   Error **errp)
141286f8cdf3SVladimir Sementsov-Ogievskiy {
141386f8cdf3SVladimir Sementsov-Ogievskiy     QIOChannelSocket *sioc;
141486f8cdf3SVladimir Sementsov-Ogievskiy     Error *local_err = NULL;
141586f8cdf3SVladimir Sementsov-Ogievskiy 
141686f8cdf3SVladimir Sementsov-Ogievskiy     sioc = qio_channel_socket_new();
141786f8cdf3SVladimir Sementsov-Ogievskiy     qio_channel_set_name(QIO_CHANNEL(sioc), "nbd-client");
141886f8cdf3SVladimir Sementsov-Ogievskiy 
141986f8cdf3SVladimir Sementsov-Ogievskiy     qio_channel_socket_connect_sync(sioc, saddr, &local_err);
142086f8cdf3SVladimir Sementsov-Ogievskiy     if (local_err) {
142186f8cdf3SVladimir Sementsov-Ogievskiy         object_unref(OBJECT(sioc));
142286f8cdf3SVladimir Sementsov-Ogievskiy         error_propagate(errp, local_err);
142386f8cdf3SVladimir Sementsov-Ogievskiy         return NULL;
142486f8cdf3SVladimir Sementsov-Ogievskiy     }
142586f8cdf3SVladimir Sementsov-Ogievskiy 
142686f8cdf3SVladimir Sementsov-Ogievskiy     qio_channel_set_delay(QIO_CHANNEL(sioc), false);
142786f8cdf3SVladimir Sementsov-Ogievskiy 
142886f8cdf3SVladimir Sementsov-Ogievskiy     return sioc;
142986f8cdf3SVladimir Sementsov-Ogievskiy }
143086f8cdf3SVladimir Sementsov-Ogievskiy 
14318f071c9dSVladimir Sementsov-Ogievskiy static int nbd_client_connect(BlockDriverState *bs, Error **errp)
143286f8cdf3SVladimir Sementsov-Ogievskiy {
1433611ae1d7SVladimir Sementsov-Ogievskiy     BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1434a8e2bb6aSVladimir Sementsov-Ogievskiy     AioContext *aio_context = bdrv_get_aio_context(bs);
143586f8cdf3SVladimir Sementsov-Ogievskiy     int ret;
143686f8cdf3SVladimir Sementsov-Ogievskiy 
143786f8cdf3SVladimir Sementsov-Ogievskiy     /*
143886f8cdf3SVladimir Sementsov-Ogievskiy      * establish TCP connection, return error if it fails
143986f8cdf3SVladimir Sementsov-Ogievskiy      * TODO: Configurable retry-until-timeout behaviour.
144086f8cdf3SVladimir Sementsov-Ogievskiy      */
14418f071c9dSVladimir Sementsov-Ogievskiy     QIOChannelSocket *sioc = nbd_establish_connection(s->saddr, errp);
144286f8cdf3SVladimir Sementsov-Ogievskiy 
144386f8cdf3SVladimir Sementsov-Ogievskiy     if (!sioc) {
144486f8cdf3SVladimir Sementsov-Ogievskiy         return -ECONNREFUSED;
144586f8cdf3SVladimir Sementsov-Ogievskiy     }
144686f8cdf3SVladimir Sementsov-Ogievskiy 
144786f8cdf3SVladimir Sementsov-Ogievskiy     /* NBD handshake */
14488f071c9dSVladimir Sementsov-Ogievskiy     trace_nbd_client_connect(s->export);
1449a8e2bb6aSVladimir Sementsov-Ogievskiy     qio_channel_set_blocking(QIO_CHANNEL(sioc), false, NULL);
1450a8e2bb6aSVladimir Sementsov-Ogievskiy     qio_channel_attach_aio_context(QIO_CHANNEL(sioc), aio_context);
145186f8cdf3SVladimir Sementsov-Ogievskiy 
1452611ae1d7SVladimir Sementsov-Ogievskiy     s->info.request_sizes = true;
1453611ae1d7SVladimir Sementsov-Ogievskiy     s->info.structured_reply = true;
1454611ae1d7SVladimir Sementsov-Ogievskiy     s->info.base_allocation = true;
14558f071c9dSVladimir Sementsov-Ogievskiy     s->info.x_dirty_bitmap = g_strdup(s->x_dirty_bitmap);
14568f071c9dSVladimir Sementsov-Ogievskiy     s->info.name = g_strdup(s->export ?: "");
14578f071c9dSVladimir Sementsov-Ogievskiy     ret = nbd_receive_negotiate(aio_context, QIO_CHANNEL(sioc), s->tlscreds,
14588f071c9dSVladimir Sementsov-Ogievskiy                                 s->hostname, &s->ioc, &s->info, errp);
1459611ae1d7SVladimir Sementsov-Ogievskiy     g_free(s->info.x_dirty_bitmap);
1460611ae1d7SVladimir Sementsov-Ogievskiy     g_free(s->info.name);
146186f8cdf3SVladimir Sementsov-Ogievskiy     if (ret < 0) {
146286f8cdf3SVladimir Sementsov-Ogievskiy         object_unref(OBJECT(sioc));
146386f8cdf3SVladimir Sementsov-Ogievskiy         return ret;
146486f8cdf3SVladimir Sementsov-Ogievskiy     }
14658f071c9dSVladimir Sementsov-Ogievskiy     if (s->x_dirty_bitmap && !s->info.base_allocation) {
146686f8cdf3SVladimir Sementsov-Ogievskiy         error_setg(errp, "requested x-dirty-bitmap %s not found",
14678f071c9dSVladimir Sementsov-Ogievskiy                    s->x_dirty_bitmap);
146886f8cdf3SVladimir Sementsov-Ogievskiy         ret = -EINVAL;
146986f8cdf3SVladimir Sementsov-Ogievskiy         goto fail;
147086f8cdf3SVladimir Sementsov-Ogievskiy     }
1471611ae1d7SVladimir Sementsov-Ogievskiy     if (s->info.flags & NBD_FLAG_READ_ONLY) {
147286f8cdf3SVladimir Sementsov-Ogievskiy         ret = bdrv_apply_auto_read_only(bs, "NBD export is read-only", errp);
147386f8cdf3SVladimir Sementsov-Ogievskiy         if (ret < 0) {
147486f8cdf3SVladimir Sementsov-Ogievskiy             goto fail;
147586f8cdf3SVladimir Sementsov-Ogievskiy         }
147686f8cdf3SVladimir Sementsov-Ogievskiy     }
1477611ae1d7SVladimir Sementsov-Ogievskiy     if (s->info.flags & NBD_FLAG_SEND_FUA) {
147886f8cdf3SVladimir Sementsov-Ogievskiy         bs->supported_write_flags = BDRV_REQ_FUA;
147986f8cdf3SVladimir Sementsov-Ogievskiy         bs->supported_zero_flags |= BDRV_REQ_FUA;
148086f8cdf3SVladimir Sementsov-Ogievskiy     }
1481611ae1d7SVladimir Sementsov-Ogievskiy     if (s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES) {
148286f8cdf3SVladimir Sementsov-Ogievskiy         bs->supported_zero_flags |= BDRV_REQ_MAY_UNMAP;
1483f061656cSEric Blake         if (s->info.flags & NBD_FLAG_SEND_FAST_ZERO) {
1484f061656cSEric Blake             bs->supported_zero_flags |= BDRV_REQ_NO_FALLBACK;
1485f061656cSEric Blake         }
148686f8cdf3SVladimir Sementsov-Ogievskiy     }
148786f8cdf3SVladimir Sementsov-Ogievskiy 
1488611ae1d7SVladimir Sementsov-Ogievskiy     s->sioc = sioc;
148986f8cdf3SVladimir Sementsov-Ogievskiy 
1490611ae1d7SVladimir Sementsov-Ogievskiy     if (!s->ioc) {
1491611ae1d7SVladimir Sementsov-Ogievskiy         s->ioc = QIO_CHANNEL(sioc);
1492611ae1d7SVladimir Sementsov-Ogievskiy         object_ref(OBJECT(s->ioc));
149386f8cdf3SVladimir Sementsov-Ogievskiy     }
149486f8cdf3SVladimir Sementsov-Ogievskiy 
14958f071c9dSVladimir Sementsov-Ogievskiy     trace_nbd_client_connect_success(s->export);
149686f8cdf3SVladimir Sementsov-Ogievskiy 
149786f8cdf3SVladimir Sementsov-Ogievskiy     return 0;
149886f8cdf3SVladimir Sementsov-Ogievskiy 
149986f8cdf3SVladimir Sementsov-Ogievskiy  fail:
150086f8cdf3SVladimir Sementsov-Ogievskiy     /*
1501a8e2bb6aSVladimir Sementsov-Ogievskiy      * We have connected, but must fail for other reasons.
1502a8e2bb6aSVladimir Sementsov-Ogievskiy      * Send NBD_CMD_DISC as a courtesy to the server.
150386f8cdf3SVladimir Sementsov-Ogievskiy      */
150486f8cdf3SVladimir Sementsov-Ogievskiy     {
150586f8cdf3SVladimir Sementsov-Ogievskiy         NBDRequest request = { .type = NBD_CMD_DISC };
150686f8cdf3SVladimir Sementsov-Ogievskiy 
1507611ae1d7SVladimir Sementsov-Ogievskiy         nbd_send_request(s->ioc ?: QIO_CHANNEL(sioc), &request);
150886f8cdf3SVladimir Sementsov-Ogievskiy 
150986f8cdf3SVladimir Sementsov-Ogievskiy         object_unref(OBJECT(sioc));
151086f8cdf3SVladimir Sementsov-Ogievskiy 
151186f8cdf3SVladimir Sementsov-Ogievskiy         return ret;
151286f8cdf3SVladimir Sementsov-Ogievskiy     }
151386f8cdf3SVladimir Sementsov-Ogievskiy }
151486f8cdf3SVladimir Sementsov-Ogievskiy 
15158f071c9dSVladimir Sementsov-Ogievskiy /*
15168f071c9dSVladimir Sementsov-Ogievskiy  * Parse nbd_open options
15178f071c9dSVladimir Sementsov-Ogievskiy  */
151886f8cdf3SVladimir Sementsov-Ogievskiy 
1519f53a1febSKevin Wolf static int nbd_parse_uri(const char *filename, QDict *options)
15201d7d2a9dSPaolo Bonzini {
15211d7d2a9dSPaolo Bonzini     URI *uri;
15221d7d2a9dSPaolo Bonzini     const char *p;
15231d7d2a9dSPaolo Bonzini     QueryParams *qp = NULL;
15241d7d2a9dSPaolo Bonzini     int ret = 0;
1525f53a1febSKevin Wolf     bool is_unix;
15261d7d2a9dSPaolo Bonzini 
15271d7d2a9dSPaolo Bonzini     uri = uri_parse(filename);
15281d7d2a9dSPaolo Bonzini     if (!uri) {
15291d7d2a9dSPaolo Bonzini         return -EINVAL;
15301d7d2a9dSPaolo Bonzini     }
15311d7d2a9dSPaolo Bonzini 
15321d7d2a9dSPaolo Bonzini     /* transport */
1533f69165a8SMax Reitz     if (!g_strcmp0(uri->scheme, "nbd")) {
1534f53a1febSKevin Wolf         is_unix = false;
1535f69165a8SMax Reitz     } else if (!g_strcmp0(uri->scheme, "nbd+tcp")) {
1536f53a1febSKevin Wolf         is_unix = false;
1537f69165a8SMax Reitz     } else if (!g_strcmp0(uri->scheme, "nbd+unix")) {
1538f53a1febSKevin Wolf         is_unix = true;
15391d7d2a9dSPaolo Bonzini     } else {
15401d7d2a9dSPaolo Bonzini         ret = -EINVAL;
15411d7d2a9dSPaolo Bonzini         goto out;
15421d7d2a9dSPaolo Bonzini     }
15431d7d2a9dSPaolo Bonzini 
15442485f22fSEric Blake     p = uri->path ? uri->path : "";
15452485f22fSEric Blake     if (p[0] == '/') {
15462485f22fSEric Blake         p++;
15472485f22fSEric Blake     }
15481d7d2a9dSPaolo Bonzini     if (p[0]) {
154946f5ac20SEric Blake         qdict_put_str(options, "export", p);
15501d7d2a9dSPaolo Bonzini     }
15511d7d2a9dSPaolo Bonzini 
15521d7d2a9dSPaolo Bonzini     qp = query_params_parse(uri->query);
1553f53a1febSKevin Wolf     if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
15541d7d2a9dSPaolo Bonzini         ret = -EINVAL;
15551d7d2a9dSPaolo Bonzini         goto out;
15561d7d2a9dSPaolo Bonzini     }
15571d7d2a9dSPaolo Bonzini 
1558f53a1febSKevin Wolf     if (is_unix) {
15591d7d2a9dSPaolo Bonzini         /* nbd+unix:///export?socket=path */
15601d7d2a9dSPaolo Bonzini         if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
15611d7d2a9dSPaolo Bonzini             ret = -EINVAL;
15621d7d2a9dSPaolo Bonzini             goto out;
15631d7d2a9dSPaolo Bonzini         }
156446f5ac20SEric Blake         qdict_put_str(options, "server.type", "unix");
156546f5ac20SEric Blake         qdict_put_str(options, "server.path", qp->p[0].value);
15661d7d2a9dSPaolo Bonzini     } else {
156723307908SJán Tomko         QString *host;
1568f84d431bSMax Reitz         char *port_str;
1569f84d431bSMax Reitz 
1570bebbf7faSKevin Wolf         /* nbd[+tcp]://host[:port]/export */
15711d7d2a9dSPaolo Bonzini         if (!uri->server) {
15721d7d2a9dSPaolo Bonzini             ret = -EINVAL;
15731d7d2a9dSPaolo Bonzini             goto out;
15741d7d2a9dSPaolo Bonzini         }
1575f17c90beSKevin Wolf 
157623307908SJán Tomko         /* strip braces from literal IPv6 address */
157723307908SJán Tomko         if (uri->server[0] == '[') {
157823307908SJán Tomko             host = qstring_from_substr(uri->server, 1,
1579ba891d68SMarkus Armbruster                                        strlen(uri->server) - 1);
158023307908SJán Tomko         } else {
158123307908SJán Tomko             host = qstring_from_str(uri->server);
158223307908SJán Tomko         }
158323307908SJán Tomko 
158446f5ac20SEric Blake         qdict_put_str(options, "server.type", "inet");
15859445673eSMarkus Armbruster         qdict_put(options, "server.host", host);
1586f84d431bSMax Reitz 
1587f84d431bSMax Reitz         port_str = g_strdup_printf("%d", uri->port ?: NBD_DEFAULT_PORT);
158846f5ac20SEric Blake         qdict_put_str(options, "server.port", port_str);
1589f53a1febSKevin Wolf         g_free(port_str);
15901d7d2a9dSPaolo Bonzini     }
15911d7d2a9dSPaolo Bonzini 
15921d7d2a9dSPaolo Bonzini out:
15931d7d2a9dSPaolo Bonzini     if (qp) {
15941d7d2a9dSPaolo Bonzini         query_params_free(qp);
15951d7d2a9dSPaolo Bonzini     }
15961d7d2a9dSPaolo Bonzini     uri_free(uri);
15971d7d2a9dSPaolo Bonzini     return ret;
15981d7d2a9dSPaolo Bonzini }
15991d7d2a9dSPaolo Bonzini 
160048c38e0bSMax Reitz static bool nbd_has_filename_options_conflict(QDict *options, Error **errp)
160148c38e0bSMax Reitz {
160248c38e0bSMax Reitz     const QDictEntry *e;
160348c38e0bSMax Reitz 
160448c38e0bSMax Reitz     for (e = qdict_first(options); e; e = qdict_next(options, e)) {
160548c38e0bSMax Reitz         if (!strcmp(e->key, "host") ||
160648c38e0bSMax Reitz             !strcmp(e->key, "port") ||
160748c38e0bSMax Reitz             !strcmp(e->key, "path") ||
1608491d6c7cSMax Reitz             !strcmp(e->key, "export") ||
1609491d6c7cSMax Reitz             strstart(e->key, "server.", NULL))
161048c38e0bSMax Reitz         {
161148c38e0bSMax Reitz             error_setg(errp, "Option '%s' cannot be used with a file name",
161248c38e0bSMax Reitz                        e->key);
161348c38e0bSMax Reitz             return true;
161448c38e0bSMax Reitz         }
161548c38e0bSMax Reitz     }
161648c38e0bSMax Reitz 
161748c38e0bSMax Reitz     return false;
161848c38e0bSMax Reitz }
161948c38e0bSMax Reitz 
16206963a30dSKevin Wolf static void nbd_parse_filename(const char *filename, QDict *options,
16216963a30dSKevin Wolf                                Error **errp)
1622019d6b8fSAnthony Liguori {
1623df18c04eSEric Blake     g_autofree char *file = NULL;
162433897dc7SNick Thomas     char *export_name;
162533897dc7SNick Thomas     const char *host_spec;
1626019d6b8fSAnthony Liguori     const char *unixpath;
1627019d6b8fSAnthony Liguori 
162848c38e0bSMax Reitz     if (nbd_has_filename_options_conflict(options, errp)) {
1629681e7ad0SKevin Wolf         return;
1630681e7ad0SKevin Wolf     }
1631681e7ad0SKevin Wolf 
16321d7d2a9dSPaolo Bonzini     if (strstr(filename, "://")) {
16336963a30dSKevin Wolf         int ret = nbd_parse_uri(filename, options);
16346963a30dSKevin Wolf         if (ret < 0) {
16356963a30dSKevin Wolf             error_setg(errp, "No valid URL specified");
16366963a30dSKevin Wolf         }
16376963a30dSKevin Wolf         return;
16381d7d2a9dSPaolo Bonzini     }
16391d7d2a9dSPaolo Bonzini 
16407267c094SAnthony Liguori     file = g_strdup(filename);
16411d45f8b5SLaurent Vivier 
164233897dc7SNick Thomas     export_name = strstr(file, EN_OPTSTR);
164333897dc7SNick Thomas     if (export_name) {
164433897dc7SNick Thomas         if (export_name[strlen(EN_OPTSTR)] == 0) {
1645df18c04eSEric Blake             return;
16461d45f8b5SLaurent Vivier         }
164733897dc7SNick Thomas         export_name[0] = 0; /* truncate 'file' */
164833897dc7SNick Thomas         export_name += strlen(EN_OPTSTR);
1649f53a1febSKevin Wolf 
165046f5ac20SEric Blake         qdict_put_str(options, "export", export_name);
16511d45f8b5SLaurent Vivier     }
16521d45f8b5SLaurent Vivier 
165333897dc7SNick Thomas     /* extract the host_spec - fail if it's not nbd:... */
165433897dc7SNick Thomas     if (!strstart(file, "nbd:", &host_spec)) {
16556963a30dSKevin Wolf         error_setg(errp, "File name string for NBD must start with 'nbd:'");
1656df18c04eSEric Blake         return;
16571d45f8b5SLaurent Vivier     }
1658019d6b8fSAnthony Liguori 
1659f53a1febSKevin Wolf     if (!*host_spec) {
1660df18c04eSEric Blake         return;
1661f53a1febSKevin Wolf     }
1662f53a1febSKevin Wolf 
166333897dc7SNick Thomas     /* are we a UNIX or TCP socket? */
166433897dc7SNick Thomas     if (strstart(host_spec, "unix:", &unixpath)) {
166546f5ac20SEric Blake         qdict_put_str(options, "server.type", "unix");
166646f5ac20SEric Blake         qdict_put_str(options, "server.path", unixpath);
1667019d6b8fSAnthony Liguori     } else {
16680785bd7aSMarkus Armbruster         InetSocketAddress *addr = g_new(InetSocketAddress, 1);
1669f53a1febSKevin Wolf 
16700785bd7aSMarkus Armbruster         if (inet_parse(addr, host_spec, errp)) {
16710785bd7aSMarkus Armbruster             goto out_inet;
1672f17c90beSKevin Wolf         }
1673f53a1febSKevin Wolf 
167446f5ac20SEric Blake         qdict_put_str(options, "server.type", "inet");
167546f5ac20SEric Blake         qdict_put_str(options, "server.host", addr->host);
167646f5ac20SEric Blake         qdict_put_str(options, "server.port", addr->port);
16770785bd7aSMarkus Armbruster     out_inet:
1678f53a1febSKevin Wolf         qapi_free_InetSocketAddress(addr);
16791d45f8b5SLaurent Vivier     }
168033897dc7SNick Thomas }
1681f53a1febSKevin Wolf 
1682491d6c7cSMax Reitz static bool nbd_process_legacy_socket_options(QDict *output_options,
1683491d6c7cSMax Reitz                                               QemuOpts *legacy_opts,
1684491d6c7cSMax Reitz                                               Error **errp)
1685f53a1febSKevin Wolf {
1686491d6c7cSMax Reitz     const char *path = qemu_opt_get(legacy_opts, "path");
1687491d6c7cSMax Reitz     const char *host = qemu_opt_get(legacy_opts, "host");
1688491d6c7cSMax Reitz     const char *port = qemu_opt_get(legacy_opts, "port");
1689491d6c7cSMax Reitz     const QDictEntry *e;
1690f53a1febSKevin Wolf 
1691491d6c7cSMax Reitz     if (!path && !host && !port) {
1692491d6c7cSMax Reitz         return true;
1693491d6c7cSMax Reitz     }
169403504d05SMax Reitz 
1695491d6c7cSMax Reitz     for (e = qdict_first(output_options); e; e = qdict_next(output_options, e))
1696491d6c7cSMax Reitz     {
1697491d6c7cSMax Reitz         if (strstart(e->key, "server.", NULL)) {
1698491d6c7cSMax Reitz             error_setg(errp, "Cannot use 'server' and path/host/port at the "
1699491d6c7cSMax Reitz                        "same time");
1700491d6c7cSMax Reitz             return false;
1701491d6c7cSMax Reitz         }
1702491d6c7cSMax Reitz     }
1703491d6c7cSMax Reitz 
1704491d6c7cSMax Reitz     if (path && host) {
170582d73014SMax Reitz         error_setg(errp, "path and host may not be used at the same time");
1706491d6c7cSMax Reitz         return false;
1707491d6c7cSMax Reitz     } else if (path) {
1708491d6c7cSMax Reitz         if (port) {
1709442045cbSMax Reitz             error_setg(errp, "port may not be used without host");
1710491d6c7cSMax Reitz             return false;
1711442045cbSMax Reitz         }
1712019d6b8fSAnthony Liguori 
171346f5ac20SEric Blake         qdict_put_str(output_options, "server.type", "unix");
171446f5ac20SEric Blake         qdict_put_str(output_options, "server.path", path);
1715491d6c7cSMax Reitz     } else if (host) {
171646f5ac20SEric Blake         qdict_put_str(output_options, "server.type", "inet");
171746f5ac20SEric Blake         qdict_put_str(output_options, "server.host", host);
171846f5ac20SEric Blake         qdict_put_str(output_options, "server.port",
171946f5ac20SEric Blake                       port ?: stringify(NBD_DEFAULT_PORT));
17207a5ed437SDaniel P. Berrange     }
1721491d6c7cSMax Reitz 
1722491d6c7cSMax Reitz     return true;
1723491d6c7cSMax Reitz }
1724491d6c7cSMax Reitz 
172562cf396bSMarkus Armbruster static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options,
17269445673eSMarkus Armbruster                                  Error **errp)
1727491d6c7cSMax Reitz {
172862cf396bSMarkus Armbruster     SocketAddress *saddr = NULL;
1729491d6c7cSMax Reitz     QDict *addr = NULL;
1730491d6c7cSMax Reitz     Visitor *iv = NULL;
1731491d6c7cSMax Reitz     Error *local_err = NULL;
1732491d6c7cSMax Reitz 
1733491d6c7cSMax Reitz     qdict_extract_subqdict(options, &addr, "server.");
1734491d6c7cSMax Reitz     if (!qdict_size(addr)) {
1735491d6c7cSMax Reitz         error_setg(errp, "NBD server address missing");
1736491d6c7cSMax Reitz         goto done;
1737491d6c7cSMax Reitz     }
1738491d6c7cSMax Reitz 
1739af91062eSMarkus Armbruster     iv = qobject_input_visitor_new_flat_confused(addr, errp);
1740af91062eSMarkus Armbruster     if (!iv) {
1741491d6c7cSMax Reitz         goto done;
1742491d6c7cSMax Reitz     }
1743491d6c7cSMax Reitz 
174462cf396bSMarkus Armbruster     visit_type_SocketAddress(iv, NULL, &saddr, &local_err);
1745491d6c7cSMax Reitz     if (local_err) {
1746491d6c7cSMax Reitz         error_propagate(errp, local_err);
1747491d6c7cSMax Reitz         goto done;
1748f53a1febSKevin Wolf     }
1749f53a1febSKevin Wolf 
1750491d6c7cSMax Reitz done:
1751cb3e7f08SMarc-André Lureau     qobject_unref(addr);
1752491d6c7cSMax Reitz     visit_free(iv);
17537a5ed437SDaniel P. Berrange     return saddr;
1754f53a1febSKevin Wolf }
1755f53a1febSKevin Wolf 
175675822a12SDaniel P. Berrange static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
175775822a12SDaniel P. Berrange {
175875822a12SDaniel P. Berrange     Object *obj;
175975822a12SDaniel P. Berrange     QCryptoTLSCreds *creds;
176075822a12SDaniel P. Berrange 
176175822a12SDaniel P. Berrange     obj = object_resolve_path_component(
176275822a12SDaniel P. Berrange         object_get_objects_root(), id);
176375822a12SDaniel P. Berrange     if (!obj) {
176475822a12SDaniel P. Berrange         error_setg(errp, "No TLS credentials with id '%s'",
176575822a12SDaniel P. Berrange                    id);
176675822a12SDaniel P. Berrange         return NULL;
176775822a12SDaniel P. Berrange     }
176875822a12SDaniel P. Berrange     creds = (QCryptoTLSCreds *)
176975822a12SDaniel P. Berrange         object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
177075822a12SDaniel P. Berrange     if (!creds) {
177175822a12SDaniel P. Berrange         error_setg(errp, "Object with id '%s' is not TLS credentials",
177275822a12SDaniel P. Berrange                    id);
177375822a12SDaniel P. Berrange         return NULL;
177475822a12SDaniel P. Berrange     }
177575822a12SDaniel P. Berrange 
177675822a12SDaniel P. Berrange     if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
177775822a12SDaniel P. Berrange         error_setg(errp,
177875822a12SDaniel P. Berrange                    "Expecting TLS credentials with a client endpoint");
177975822a12SDaniel P. Berrange         return NULL;
178075822a12SDaniel P. Berrange     }
178175822a12SDaniel P. Berrange     object_ref(obj);
178275822a12SDaniel P. Berrange     return creds;
178375822a12SDaniel P. Berrange }
178475822a12SDaniel P. Berrange 
178575822a12SDaniel P. Berrange 
17867ccc44fdSMax Reitz static QemuOptsList nbd_runtime_opts = {
17877ccc44fdSMax Reitz     .name = "nbd",
17887ccc44fdSMax Reitz     .head = QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts.head),
17897ccc44fdSMax Reitz     .desc = {
17907ccc44fdSMax Reitz         {
17917ccc44fdSMax Reitz             .name = "host",
17927ccc44fdSMax Reitz             .type = QEMU_OPT_STRING,
17937ccc44fdSMax Reitz             .help = "TCP host to connect to",
17947ccc44fdSMax Reitz         },
17957ccc44fdSMax Reitz         {
17967ccc44fdSMax Reitz             .name = "port",
17977ccc44fdSMax Reitz             .type = QEMU_OPT_STRING,
17987ccc44fdSMax Reitz             .help = "TCP port to connect to",
17997ccc44fdSMax Reitz         },
18007ccc44fdSMax Reitz         {
18017ccc44fdSMax Reitz             .name = "path",
18027ccc44fdSMax Reitz             .type = QEMU_OPT_STRING,
18037ccc44fdSMax Reitz             .help = "Unix socket path to connect to",
18047ccc44fdSMax Reitz         },
18057ccc44fdSMax Reitz         {
18067ccc44fdSMax Reitz             .name = "export",
18077ccc44fdSMax Reitz             .type = QEMU_OPT_STRING,
18087ccc44fdSMax Reitz             .help = "Name of the NBD export to open",
18097ccc44fdSMax Reitz         },
18107ccc44fdSMax Reitz         {
18117ccc44fdSMax Reitz             .name = "tls-creds",
18127ccc44fdSMax Reitz             .type = QEMU_OPT_STRING,
18137ccc44fdSMax Reitz             .help = "ID of the TLS credentials to use",
18147ccc44fdSMax Reitz         },
1815216ee365SEric Blake         {
1816216ee365SEric Blake             .name = "x-dirty-bitmap",
1817216ee365SEric Blake             .type = QEMU_OPT_STRING,
1818216ee365SEric Blake             .help = "experimental: expose named dirty bitmap in place of "
1819216ee365SEric Blake                     "block status",
1820216ee365SEric Blake         },
1821b172ae2eSVladimir Sementsov-Ogievskiy         {
1822b172ae2eSVladimir Sementsov-Ogievskiy             .name = "reconnect-delay",
1823b172ae2eSVladimir Sementsov-Ogievskiy             .type = QEMU_OPT_NUMBER,
1824b172ae2eSVladimir Sementsov-Ogievskiy             .help = "On an unexpected disconnect, the nbd client tries to "
1825b172ae2eSVladimir Sementsov-Ogievskiy                     "connect again until succeeding or encountering a serious "
1826b172ae2eSVladimir Sementsov-Ogievskiy                     "error.  During the first @reconnect-delay seconds, all "
1827b172ae2eSVladimir Sementsov-Ogievskiy                     "requests are paused and will be rerun on a successful "
1828b172ae2eSVladimir Sementsov-Ogievskiy                     "reconnect. After that time, any delayed requests and all "
1829b172ae2eSVladimir Sementsov-Ogievskiy                     "future requests before a successful reconnect will "
1830b172ae2eSVladimir Sementsov-Ogievskiy                     "immediately fail. Default 0",
1831b172ae2eSVladimir Sementsov-Ogievskiy         },
1832c4365735SMurilo Opsfelder Araujo         { /* end of list */ }
18337ccc44fdSMax Reitz     },
18347ccc44fdSMax Reitz };
18357ccc44fdSMax Reitz 
18368f071c9dSVladimir Sementsov-Ogievskiy static int nbd_process_options(BlockDriverState *bs, QDict *options,
1837015a1036SMax Reitz                                Error **errp)
183833897dc7SNick Thomas {
183933897dc7SNick Thomas     BDRVNBDState *s = bs->opaque;
18408f071c9dSVladimir Sementsov-Ogievskiy     QemuOpts *opts;
18417ccc44fdSMax Reitz     Error *local_err = NULL;
184275822a12SDaniel P. Berrange     int ret = -EINVAL;
1843ae255e52SPaolo Bonzini 
18447ccc44fdSMax Reitz     opts = qemu_opts_create(&nbd_runtime_opts, NULL, 0, &error_abort);
18457ccc44fdSMax Reitz     qemu_opts_absorb_qdict(opts, options, &local_err);
18467ccc44fdSMax Reitz     if (local_err) {
18477ccc44fdSMax Reitz         error_propagate(errp, local_err);
18487ccc44fdSMax Reitz         goto error;
18497ccc44fdSMax Reitz     }
18507ccc44fdSMax Reitz 
185162cf396bSMarkus Armbruster     /* Translate @host, @port, and @path to a SocketAddress */
1852491d6c7cSMax Reitz     if (!nbd_process_legacy_socket_options(options, opts, errp)) {
185375822a12SDaniel P. Berrange         goto error;
185475822a12SDaniel P. Berrange     }
185575822a12SDaniel P. Berrange 
1856491d6c7cSMax Reitz     /* Pop the config into our state object. Exit if invalid. */
1857491d6c7cSMax Reitz     s->saddr = nbd_config(s, options, errp);
1858491d6c7cSMax Reitz     if (!s->saddr) {
1859491d6c7cSMax Reitz         goto error;
1860491d6c7cSMax Reitz     }
1861491d6c7cSMax Reitz 
1862491d6c7cSMax Reitz     s->export = g_strdup(qemu_opt_get(opts, "export"));
186393676c88SEric Blake     if (s->export && strlen(s->export) > NBD_MAX_STRING_SIZE) {
186493676c88SEric Blake         error_setg(errp, "export name too long to send to server");
186593676c88SEric Blake         goto error;
186693676c88SEric Blake     }
1867491d6c7cSMax Reitz 
186803504d05SMax Reitz     s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds"));
186903504d05SMax Reitz     if (s->tlscredsid) {
18708f071c9dSVladimir Sementsov-Ogievskiy         s->tlscreds = nbd_get_tls_creds(s->tlscredsid, errp);
18718f071c9dSVladimir Sementsov-Ogievskiy         if (!s->tlscreds) {
187275822a12SDaniel P. Berrange             goto error;
187375822a12SDaniel P. Berrange         }
187475822a12SDaniel P. Berrange 
1875ca0b64e5SMarkus Armbruster         /* TODO SOCKET_ADDRESS_KIND_FD where fd has AF_INET or AF_INET6 */
187662cf396bSMarkus Armbruster         if (s->saddr->type != SOCKET_ADDRESS_TYPE_INET) {
187775822a12SDaniel P. Berrange             error_setg(errp, "TLS only supported over IP sockets");
187875822a12SDaniel P. Berrange             goto error;
187975822a12SDaniel P. Berrange         }
18808f071c9dSVladimir Sementsov-Ogievskiy         s->hostname = s->saddr->u.inet.host;
188133897dc7SNick Thomas     }
188233897dc7SNick Thomas 
18838f071c9dSVladimir Sementsov-Ogievskiy     s->x_dirty_bitmap = g_strdup(qemu_opt_get(opts, "x-dirty-bitmap"));
188493676c88SEric Blake     if (s->x_dirty_bitmap && strlen(s->x_dirty_bitmap) > NBD_MAX_STRING_SIZE) {
188593676c88SEric Blake         error_setg(errp, "x-dirty-bitmap query too long to send to server");
188693676c88SEric Blake         goto error;
188793676c88SEric Blake     }
188893676c88SEric Blake 
18898f071c9dSVladimir Sementsov-Ogievskiy     s->reconnect_delay = qemu_opt_get_number(opts, "reconnect-delay", 0);
18908f071c9dSVladimir Sementsov-Ogievskiy 
18918f071c9dSVladimir Sementsov-Ogievskiy     ret = 0;
1892d42f78e9SVladimir Sementsov-Ogievskiy 
189375822a12SDaniel P. Berrange  error:
189403504d05SMax Reitz     if (ret < 0) {
1895*7f493662SPan Nengyuan         nbd_clear_bdrvstate(s);
189603504d05SMax Reitz     }
18977ccc44fdSMax Reitz     qemu_opts_del(opts);
189875822a12SDaniel P. Berrange     return ret;
1899ae255e52SPaolo Bonzini }
1900d9b09f13SPaolo Bonzini 
19018f071c9dSVladimir Sementsov-Ogievskiy static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
19028f071c9dSVladimir Sementsov-Ogievskiy                     Error **errp)
19038f071c9dSVladimir Sementsov-Ogievskiy {
19048f071c9dSVladimir Sementsov-Ogievskiy     int ret;
19058f071c9dSVladimir Sementsov-Ogievskiy     BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
19068f071c9dSVladimir Sementsov-Ogievskiy 
19078f071c9dSVladimir Sementsov-Ogievskiy     ret = nbd_process_options(bs, options, errp);
19088f071c9dSVladimir Sementsov-Ogievskiy     if (ret < 0) {
19098f071c9dSVladimir Sementsov-Ogievskiy         return ret;
19108f071c9dSVladimir Sementsov-Ogievskiy     }
19118f071c9dSVladimir Sementsov-Ogievskiy 
19128f071c9dSVladimir Sementsov-Ogievskiy     s->bs = bs;
19138f071c9dSVladimir Sementsov-Ogievskiy     qemu_co_mutex_init(&s->send_mutex);
19148f071c9dSVladimir Sementsov-Ogievskiy     qemu_co_queue_init(&s->free_sema);
19158f071c9dSVladimir Sementsov-Ogievskiy 
19168f071c9dSVladimir Sementsov-Ogievskiy     ret = nbd_client_connect(bs, errp);
19178f071c9dSVladimir Sementsov-Ogievskiy     if (ret < 0) {
19188f071c9dSVladimir Sementsov-Ogievskiy         return ret;
19198f071c9dSVladimir Sementsov-Ogievskiy     }
19208f071c9dSVladimir Sementsov-Ogievskiy     /* successfully connected */
19218f071c9dSVladimir Sementsov-Ogievskiy     s->state = NBD_CLIENT_CONNECTED;
19228f071c9dSVladimir Sementsov-Ogievskiy 
19238f071c9dSVladimir Sementsov-Ogievskiy     s->connection_co = qemu_coroutine_create(nbd_connection_entry, s);
19248f071c9dSVladimir Sementsov-Ogievskiy     bdrv_inc_in_flight(bs);
19258f071c9dSVladimir Sementsov-Ogievskiy     aio_co_schedule(bdrv_get_aio_context(bs), s->connection_co);
19268f071c9dSVladimir Sementsov-Ogievskiy 
19278f071c9dSVladimir Sementsov-Ogievskiy     return 0;
19288f071c9dSVladimir Sementsov-Ogievskiy }
19298f071c9dSVladimir Sementsov-Ogievskiy 
19301486d04aSPaolo Bonzini static int nbd_co_flush(BlockDriverState *bs)
19311486d04aSPaolo Bonzini {
1932f53a829bSMax Reitz     return nbd_client_co_flush(bs);
19331486d04aSPaolo Bonzini }
19341486d04aSPaolo Bonzini 
1935fa21e6faSDenis V. Lunev static void nbd_refresh_limits(BlockDriverState *bs, Error **errp)
1936fa21e6faSDenis V. Lunev {
1937611ae1d7SVladimir Sementsov-Ogievskiy     BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1938fd8d372dSEric Blake     uint32_t min = s->info.min_block;
1939081dd1feSEric Blake     uint32_t max = MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE, s->info.max_block);
1940081dd1feSEric Blake 
19417da537f7SEric Blake     /*
19427da537f7SEric Blake      * If the server did not advertise an alignment:
19437da537f7SEric Blake      * - a size that is not sector-aligned implies that an alignment
19447da537f7SEric Blake      *   of 1 can be used to access those tail bytes
19457da537f7SEric Blake      * - advertisement of block status requires an alignment of 1, so
19467da537f7SEric Blake      *   that we don't violate block layer constraints that block
19477da537f7SEric Blake      *   status is always aligned (as we can't control whether the
19487da537f7SEric Blake      *   server will report sub-sector extents, such as a hole at EOF
19497da537f7SEric Blake      *   on an unaligned POSIX file)
19507da537f7SEric Blake      * - otherwise, assume the server is so old that we are safer avoiding
19517da537f7SEric Blake      *   sub-sector requests
19527da537f7SEric Blake      */
19537da537f7SEric Blake     if (!min) {
19547da537f7SEric Blake         min = (!QEMU_IS_ALIGNED(s->info.size, BDRV_SECTOR_SIZE) ||
19557da537f7SEric Blake                s->info.base_allocation) ? 1 : BDRV_SECTOR_SIZE;
19567da537f7SEric Blake     }
19577da537f7SEric Blake 
19587da537f7SEric Blake     bs->bl.request_alignment = min;
1959081dd1feSEric Blake     bs->bl.max_pdiscard = max;
1960081dd1feSEric Blake     bs->bl.max_pwrite_zeroes = max;
1961081dd1feSEric Blake     bs->bl.max_transfer = max;
1962081dd1feSEric Blake 
1963081dd1feSEric Blake     if (s->info.opt_block &&
1964081dd1feSEric Blake         s->info.opt_block > bs->bl.opt_transfer) {
1965081dd1feSEric Blake         bs->bl.opt_transfer = s->info.opt_block;
1966081dd1feSEric Blake     }
1967fa21e6faSDenis V. Lunev }
1968fa21e6faSDenis V. Lunev 
1969019d6b8fSAnthony Liguori static void nbd_close(BlockDriverState *bs)
1970019d6b8fSAnthony Liguori {
197103504d05SMax Reitz     BDRVNBDState *s = bs->opaque;
197203504d05SMax Reitz 
1973f53a829bSMax Reitz     nbd_client_close(bs);
1974*7f493662SPan Nengyuan     nbd_clear_bdrvstate(s);
1975019d6b8fSAnthony Liguori }
1976019d6b8fSAnthony Liguori 
1977019d6b8fSAnthony Liguori static int64_t nbd_getlength(BlockDriverState *bs)
1978019d6b8fSAnthony Liguori {
1979019d6b8fSAnthony Liguori     BDRVNBDState *s = bs->opaque;
1980019d6b8fSAnthony Liguori 
1981611ae1d7SVladimir Sementsov-Ogievskiy     return s->info.size;
1982019d6b8fSAnthony Liguori }
1983019d6b8fSAnthony Liguori 
1984998b3a1eSMax Reitz static void nbd_refresh_filename(BlockDriverState *bs)
19852019d68bSMax Reitz {
198603504d05SMax Reitz     BDRVNBDState *s = bs->opaque;
1987491d6c7cSMax Reitz     const char *host = NULL, *port = NULL, *path = NULL;
1988491d6c7cSMax Reitz 
198962cf396bSMarkus Armbruster     if (s->saddr->type == SOCKET_ADDRESS_TYPE_INET) {
19909445673eSMarkus Armbruster         const InetSocketAddress *inet = &s->saddr->u.inet;
1991491d6c7cSMax Reitz         if (!inet->has_ipv4 && !inet->has_ipv6 && !inet->has_to) {
1992491d6c7cSMax Reitz             host = inet->host;
1993491d6c7cSMax Reitz             port = inet->port;
1994491d6c7cSMax Reitz         }
199562cf396bSMarkus Armbruster     } else if (s->saddr->type == SOCKET_ADDRESS_TYPE_UNIX) {
19969445673eSMarkus Armbruster         path = s->saddr->u.q_unix.path;
19979445673eSMarkus Armbruster     } /* else can't represent as pseudo-filename */
19982019d68bSMax Reitz 
1999491d6c7cSMax Reitz     if (path && s->export) {
2000ec0de768SMax Reitz         snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2001491d6c7cSMax Reitz                  "nbd+unix:///%s?socket=%s", s->export, path);
2002491d6c7cSMax Reitz     } else if (path && !s->export) {
2003ec0de768SMax Reitz         snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2004491d6c7cSMax Reitz                  "nbd+unix://?socket=%s", path);
2005491d6c7cSMax Reitz     } else if (host && s->export) {
2006ec0de768SMax Reitz         snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2007491d6c7cSMax Reitz                  "nbd://%s:%s/%s", host, port, s->export);
2008491d6c7cSMax Reitz     } else if (host && !s->export) {
2009ec0de768SMax Reitz         snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2010491d6c7cSMax Reitz                  "nbd://%s:%s", host, port);
2011ec0de768SMax Reitz     }
20122019d68bSMax Reitz }
20132019d68bSMax Reitz 
20148a6239c0SMax Reitz static char *nbd_dirname(BlockDriverState *bs, Error **errp)
20158a6239c0SMax Reitz {
20168a6239c0SMax Reitz     /* The generic bdrv_dirname() implementation is able to work out some
20178a6239c0SMax Reitz      * directory name for NBD nodes, but that would be wrong. So far there is no
20188a6239c0SMax Reitz      * specification for how "export paths" would work, so NBD does not have
20198a6239c0SMax Reitz      * directory names. */
20208a6239c0SMax Reitz     error_setg(errp, "Cannot generate a base directory for NBD nodes");
20218a6239c0SMax Reitz     return NULL;
20228a6239c0SMax Reitz }
20238a6239c0SMax Reitz 
20242654267cSMax Reitz static const char *const nbd_strong_runtime_opts[] = {
20252654267cSMax Reitz     "path",
20262654267cSMax Reitz     "host",
20272654267cSMax Reitz     "port",
20282654267cSMax Reitz     "export",
20292654267cSMax Reitz     "tls-creds",
20302654267cSMax Reitz     "server.",
20312654267cSMax Reitz 
20322654267cSMax Reitz     NULL
20332654267cSMax Reitz };
20342654267cSMax Reitz 
2035019d6b8fSAnthony Liguori static BlockDriver bdrv_nbd = {
2036019d6b8fSAnthony Liguori     .format_name                = "nbd",
20371d7d2a9dSPaolo Bonzini     .protocol_name              = "nbd",
2038019d6b8fSAnthony Liguori     .instance_size              = sizeof(BDRVNBDState),
20396963a30dSKevin Wolf     .bdrv_parse_filename        = nbd_parse_filename,
204066f82ceeSKevin Wolf     .bdrv_file_open             = nbd_open,
2041e99754b4SMaxim Levitsky     .bdrv_reopen_prepare        = nbd_client_reopen_prepare,
204270c4fb26SEric Blake     .bdrv_co_preadv             = nbd_client_co_preadv,
204370c4fb26SEric Blake     .bdrv_co_pwritev            = nbd_client_co_pwritev,
2044fa778fffSEric Blake     .bdrv_co_pwrite_zeroes      = nbd_client_co_pwrite_zeroes,
2045019d6b8fSAnthony Liguori     .bdrv_close                 = nbd_close,
20461486d04aSPaolo Bonzini     .bdrv_co_flush_to_os        = nbd_co_flush,
2047447e57c3SEric Blake     .bdrv_co_pdiscard           = nbd_client_co_pdiscard,
2048fa21e6faSDenis V. Lunev     .bdrv_refresh_limits        = nbd_refresh_limits,
2049019d6b8fSAnthony Liguori     .bdrv_getlength             = nbd_getlength,
205086f8cdf3SVladimir Sementsov-Ogievskiy     .bdrv_detach_aio_context    = nbd_client_detach_aio_context,
205186f8cdf3SVladimir Sementsov-Ogievskiy     .bdrv_attach_aio_context    = nbd_client_attach_aio_context,
2052f7651539SVladimir Sementsov-Ogievskiy     .bdrv_co_drain_begin        = nbd_client_co_drain_begin,
2053f7651539SVladimir Sementsov-Ogievskiy     .bdrv_co_drain_end          = nbd_client_co_drain_end,
20542019d68bSMax Reitz     .bdrv_refresh_filename      = nbd_refresh_filename,
205578a33ab5SVladimir Sementsov-Ogievskiy     .bdrv_co_block_status       = nbd_client_co_block_status,
20568a6239c0SMax Reitz     .bdrv_dirname               = nbd_dirname,
20572654267cSMax Reitz     .strong_runtime_opts        = nbd_strong_runtime_opts,
20581d7d2a9dSPaolo Bonzini };
20591d7d2a9dSPaolo Bonzini 
20601d7d2a9dSPaolo Bonzini static BlockDriver bdrv_nbd_tcp = {
20611d7d2a9dSPaolo Bonzini     .format_name                = "nbd",
20621d7d2a9dSPaolo Bonzini     .protocol_name              = "nbd+tcp",
20631d7d2a9dSPaolo Bonzini     .instance_size              = sizeof(BDRVNBDState),
20646963a30dSKevin Wolf     .bdrv_parse_filename        = nbd_parse_filename,
20651d7d2a9dSPaolo Bonzini     .bdrv_file_open             = nbd_open,
2066e99754b4SMaxim Levitsky     .bdrv_reopen_prepare        = nbd_client_reopen_prepare,
206770c4fb26SEric Blake     .bdrv_co_preadv             = nbd_client_co_preadv,
206870c4fb26SEric Blake     .bdrv_co_pwritev            = nbd_client_co_pwritev,
2069fa778fffSEric Blake     .bdrv_co_pwrite_zeroes      = nbd_client_co_pwrite_zeroes,
20701d7d2a9dSPaolo Bonzini     .bdrv_close                 = nbd_close,
20711d7d2a9dSPaolo Bonzini     .bdrv_co_flush_to_os        = nbd_co_flush,
2072447e57c3SEric Blake     .bdrv_co_pdiscard           = nbd_client_co_pdiscard,
2073fa21e6faSDenis V. Lunev     .bdrv_refresh_limits        = nbd_refresh_limits,
20741d7d2a9dSPaolo Bonzini     .bdrv_getlength             = nbd_getlength,
207586f8cdf3SVladimir Sementsov-Ogievskiy     .bdrv_detach_aio_context    = nbd_client_detach_aio_context,
207686f8cdf3SVladimir Sementsov-Ogievskiy     .bdrv_attach_aio_context    = nbd_client_attach_aio_context,
2077f7651539SVladimir Sementsov-Ogievskiy     .bdrv_co_drain_begin        = nbd_client_co_drain_begin,
2078f7651539SVladimir Sementsov-Ogievskiy     .bdrv_co_drain_end          = nbd_client_co_drain_end,
20792019d68bSMax Reitz     .bdrv_refresh_filename      = nbd_refresh_filename,
208078a33ab5SVladimir Sementsov-Ogievskiy     .bdrv_co_block_status       = nbd_client_co_block_status,
20818a6239c0SMax Reitz     .bdrv_dirname               = nbd_dirname,
20822654267cSMax Reitz     .strong_runtime_opts        = nbd_strong_runtime_opts,
20831d7d2a9dSPaolo Bonzini };
20841d7d2a9dSPaolo Bonzini 
20851d7d2a9dSPaolo Bonzini static BlockDriver bdrv_nbd_unix = {
20861d7d2a9dSPaolo Bonzini     .format_name                = "nbd",
20871d7d2a9dSPaolo Bonzini     .protocol_name              = "nbd+unix",
20881d7d2a9dSPaolo Bonzini     .instance_size              = sizeof(BDRVNBDState),
20896963a30dSKevin Wolf     .bdrv_parse_filename        = nbd_parse_filename,
20901d7d2a9dSPaolo Bonzini     .bdrv_file_open             = nbd_open,
2091e99754b4SMaxim Levitsky     .bdrv_reopen_prepare        = nbd_client_reopen_prepare,
209270c4fb26SEric Blake     .bdrv_co_preadv             = nbd_client_co_preadv,
209370c4fb26SEric Blake     .bdrv_co_pwritev            = nbd_client_co_pwritev,
2094fa778fffSEric Blake     .bdrv_co_pwrite_zeroes      = nbd_client_co_pwrite_zeroes,
20951d7d2a9dSPaolo Bonzini     .bdrv_close                 = nbd_close,
20961d7d2a9dSPaolo Bonzini     .bdrv_co_flush_to_os        = nbd_co_flush,
2097447e57c3SEric Blake     .bdrv_co_pdiscard           = nbd_client_co_pdiscard,
2098fa21e6faSDenis V. Lunev     .bdrv_refresh_limits        = nbd_refresh_limits,
20991d7d2a9dSPaolo Bonzini     .bdrv_getlength             = nbd_getlength,
210086f8cdf3SVladimir Sementsov-Ogievskiy     .bdrv_detach_aio_context    = nbd_client_detach_aio_context,
210186f8cdf3SVladimir Sementsov-Ogievskiy     .bdrv_attach_aio_context    = nbd_client_attach_aio_context,
2102f7651539SVladimir Sementsov-Ogievskiy     .bdrv_co_drain_begin        = nbd_client_co_drain_begin,
2103f7651539SVladimir Sementsov-Ogievskiy     .bdrv_co_drain_end          = nbd_client_co_drain_end,
21042019d68bSMax Reitz     .bdrv_refresh_filename      = nbd_refresh_filename,
210578a33ab5SVladimir Sementsov-Ogievskiy     .bdrv_co_block_status       = nbd_client_co_block_status,
21068a6239c0SMax Reitz     .bdrv_dirname               = nbd_dirname,
21072654267cSMax Reitz     .strong_runtime_opts        = nbd_strong_runtime_opts,
2108019d6b8fSAnthony Liguori };
2109019d6b8fSAnthony Liguori 
2110019d6b8fSAnthony Liguori static void bdrv_nbd_init(void)
2111019d6b8fSAnthony Liguori {
2112019d6b8fSAnthony Liguori     bdrv_register(&bdrv_nbd);
21131d7d2a9dSPaolo Bonzini     bdrv_register(&bdrv_nbd_tcp);
21141d7d2a9dSPaolo Bonzini     bdrv_register(&bdrv_nbd_unix);
2115019d6b8fSAnthony Liguori }
2116019d6b8fSAnthony Liguori 
2117019d6b8fSAnthony Liguori block_init(bdrv_nbd_init);
2118