Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/block/nbd.c
Line
Count
Source
1
/*
2
 * QEMU Block driver for NBD
3
 *
4
 * Copyright (c) 2019 Virtuozzo International GmbH.
5
 * Copyright Red Hat
6
 * Copyright (C) 2008 Bull S.A.S.
7
 *     Author: Laurent Vivier <Laurent.Vivier@bull.net>
8
 *
9
 * Some parts:
10
 *    Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in
20
 * all copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
 * THE SOFTWARE.
29
 */
30
31
#include "qemu/osdep.h"
32
33
#include "trace.h"
34
#include "qemu/option.h"
35
#include "qemu/cutils.h"
36
#include "qemu/main-loop.h"
37
38
#include "qapi/qapi-visit-sockets.h"
39
#include "qobject/qstring.h"
40
#include "qapi/clone-visitor.h"
41
42
#include "block/qdict.h"
43
#include "block/nbd.h"
44
#include "block/block_int.h"
45
#include "block/coroutines.h"
46
47
#include "qemu/yank.h"
48
49
0
#define EN_OPTSTR ":exportname="
50
0
#define MAX_NBD_REQUESTS    16
51
52
0
#define COOKIE_TO_INDEX(cookie) ((cookie) - 1)
53
0
#define INDEX_TO_COOKIE(index)  ((index) + 1)
54
55
typedef struct {
56
    Coroutine *coroutine;
57
    uint64_t offset;        /* original offset of the request */
58
    bool receiving;         /* sleeping in the yield in nbd_receive_replies */
59
} NBDClientRequest;
60
61
typedef enum NBDClientState {
62
    NBD_CLIENT_CONNECTING_WAIT,
63
    NBD_CLIENT_CONNECTING_NOWAIT,
64
    NBD_CLIENT_CONNECTED,
65
    NBD_CLIENT_QUIT
66
} NBDClientState;
67
68
typedef struct BDRVNBDState {
69
    QIOChannel *ioc; /* The current I/O channel */
70
    NBDExportInfo info;
71
72
    /*
73
     * Protects state, free_sema, in_flight, requests[].coroutine,
74
     * reconnect_delay_timer.
75
     */
76
    QemuMutex requests_lock;
77
    NBDClientState state;
78
    CoQueue free_sema;
79
    unsigned in_flight;
80
    NBDClientRequest requests[MAX_NBD_REQUESTS];
81
    QEMUTimer *reconnect_delay_timer;
82
83
    /* Protects sending data on the socket.  */
84
    CoMutex send_mutex;
85
86
    /*
87
     * Protects receiving reply headers from the socket, as well as the
88
     * fields reply and requests[].receiving
89
     */
90
    CoMutex receive_mutex;
91
    NBDReply reply;
92
93
    QEMUTimer *open_timer;
94
95
    BlockDriverState *bs;
96
97
    /* Connection parameters */
98
    uint32_t reconnect_delay;
99
    uint32_t open_timeout;
100
    SocketAddress *saddr;
101
    char *export;
102
    char *tlscredsid;
103
    QCryptoTLSCreds *tlscreds;
104
    char *tlshostname;
105
    char *x_dirty_bitmap;
106
    bool alloc_depth;
107
108
    NBDClientConnection *conn;
109
} BDRVNBDState;
110
111
static void nbd_yank(void *opaque);
112
113
static void nbd_clear_bdrvstate(BlockDriverState *bs)
114
0
{
115
0
    BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
116
117
0
    nbd_client_connection_release(s->conn);
118
0
    s->conn = NULL;
119
120
0
    yank_unregister_instance(BLOCKDEV_YANK_INSTANCE(bs->node_name));
121
122
    /* Must not leave timers behind that would access freed data */
123
0
    assert(!s->reconnect_delay_timer);
124
0
    assert(!s->open_timer);
125
126
0
    object_unref(OBJECT(s->tlscreds));
127
0
    qapi_free_SocketAddress(s->saddr);
128
0
    s->saddr = NULL;
129
0
    g_free(s->export);
130
0
    s->export = NULL;
131
0
    g_free(s->tlscredsid);
132
0
    s->tlscredsid = NULL;
133
0
    g_free(s->tlshostname);
134
0
    s->tlshostname = NULL;
135
0
    g_free(s->x_dirty_bitmap);
136
0
    s->x_dirty_bitmap = NULL;
137
0
}
138
139
static NBDClientRequest *nbd_request_by_cookie(BDRVNBDState *s, uint64_t cookie,
140
                                               Error **errp)
141
0
{
142
0
    uint64_t ind = COOKIE_TO_INDEX(cookie);
143
144
0
    if (ind >= MAX_NBD_REQUESTS || !s->requests[ind].coroutine) {
145
0
        error_setg(errp, "unexpected cookie value");
146
0
        return NULL;
147
0
    }
148
149
0
    return &s->requests[ind];
150
0
}
151
152
/* Called with s->receive_mutex taken.  */
153
static bool coroutine_fn nbd_recv_coroutine_wake_one(NBDClientRequest *req)
154
0
{
155
0
    if (req->receiving) {
156
0
        req->receiving = false;
157
0
        aio_co_wake(req->coroutine);
158
0
        return true;
159
0
    }
160
161
0
    return false;
162
0
}
163
164
/* Called with s->receive_mutex taken. */
165
static void coroutine_fn nbd_recv_coroutines_wake(BDRVNBDState *s)
166
0
{
167
0
    int i;
168
169
0
    for (i = 0; i < MAX_NBD_REQUESTS; i++) {
170
0
        if (nbd_recv_coroutine_wake_one(&s->requests[i])) {
171
0
            return;
172
0
        }
173
0
    }
174
0
}
175
176
/* Called with s->requests_lock held.  */
177
static void coroutine_fn nbd_channel_error_locked(BDRVNBDState *s, int ret)
178
0
{
179
0
    if (s->state == NBD_CLIENT_CONNECTED) {
180
0
        qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
181
0
    }
182
183
0
    if (ret == -EIO) {
184
0
        if (s->state == NBD_CLIENT_CONNECTED) {
185
0
            s->state = s->reconnect_delay ? NBD_CLIENT_CONNECTING_WAIT :
186
0
                                            NBD_CLIENT_CONNECTING_NOWAIT;
187
0
        }
188
0
    } else {
189
0
        s->state = NBD_CLIENT_QUIT;
190
0
    }
191
0
}
192
193
static void coroutine_fn nbd_channel_error(BDRVNBDState *s, int ret)
194
0
{
195
0
    QEMU_LOCK_GUARD(&s->requests_lock);
196
0
    nbd_channel_error_locked(s, ret);
197
0
}
198
199
static void reconnect_delay_timer_del(BDRVNBDState *s)
200
0
{
201
0
    if (s->reconnect_delay_timer) {
202
0
        timer_free(s->reconnect_delay_timer);
203
0
        s->reconnect_delay_timer = NULL;
204
0
    }
205
0
}
206
207
static void reconnect_delay_timer_cb(void *opaque)
208
0
{
209
0
    BDRVNBDState *s = opaque;
210
211
0
    reconnect_delay_timer_del(s);
212
0
    WITH_QEMU_LOCK_GUARD(&s->requests_lock) {
213
0
        if (s->state != NBD_CLIENT_CONNECTING_WAIT) {
214
0
            return;
215
0
        }
216
0
        s->state = NBD_CLIENT_CONNECTING_NOWAIT;
217
0
    }
218
0
    nbd_co_establish_connection_cancel(s->conn);
219
0
}
220
221
static void reconnect_delay_timer_init(BDRVNBDState *s, uint64_t expire_time_ns)
222
0
{
223
0
    assert(!s->reconnect_delay_timer);
224
0
    s->reconnect_delay_timer = aio_timer_new(bdrv_get_aio_context(s->bs),
225
0
                                             QEMU_CLOCK_REALTIME,
226
0
                                             SCALE_NS,
227
0
                                             reconnect_delay_timer_cb, s);
228
0
    timer_mod(s->reconnect_delay_timer, expire_time_ns);
229
0
}
230
231
static void nbd_teardown_connection(BlockDriverState *bs)
232
0
{
233
0
    BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
234
235
0
    assert(!s->in_flight);
236
237
0
    if (s->ioc) {
238
0
        qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
239
0
        yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name),
240
0
                                 nbd_yank, s->bs);
241
0
        object_unref(OBJECT(s->ioc));
242
0
        s->ioc = NULL;
243
0
    }
244
245
0
    WITH_QEMU_LOCK_GUARD(&s->requests_lock) {
246
0
        s->state = NBD_CLIENT_QUIT;
247
0
    }
248
0
}
249
250
static void open_timer_del(BDRVNBDState *s)
251
0
{
252
0
    if (s->open_timer) {
253
0
        timer_free(s->open_timer);
254
0
        s->open_timer = NULL;
255
0
    }
256
0
}
257
258
static void open_timer_cb(void *opaque)
259
0
{
260
0
    BDRVNBDState *s = opaque;
261
262
0
    nbd_co_establish_connection_cancel(s->conn);
263
0
    open_timer_del(s);
264
0
}
265
266
static void open_timer_init(BDRVNBDState *s, uint64_t expire_time_ns)
267
0
{
268
0
    assert(!s->open_timer);
269
0
    s->open_timer = aio_timer_new(bdrv_get_aio_context(s->bs),
270
0
                                  QEMU_CLOCK_REALTIME,
271
0
                                  SCALE_NS,
272
0
                                  open_timer_cb, s);
273
0
    timer_mod(s->open_timer, expire_time_ns);
274
0
}
275
276
static bool nbd_client_will_reconnect(BDRVNBDState *s)
277
0
{
278
    /*
279
     * Called only after a socket error, so this is not performance sensitive.
280
     */
281
0
    QEMU_LOCK_GUARD(&s->requests_lock);
282
0
    return s->state == NBD_CLIENT_CONNECTING_WAIT;
283
0
}
284
285
/*
286
 * Update @bs with information learned during a completed negotiation process.
287
 * Return failure if the server's advertised options are incompatible with the
288
 * client's needs.
289
 */
290
static int coroutine_fn GRAPH_RDLOCK
291
nbd_handle_updated_info(BlockDriverState *bs, Error **errp)
292
0
{
293
0
    BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
294
0
    int ret;
295
296
0
    if (s->x_dirty_bitmap) {
297
0
        if (!s->info.base_allocation) {
298
0
            error_setg(errp, "requested x-dirty-bitmap %s not found",
299
0
                       s->x_dirty_bitmap);
300
0
            return -EINVAL;
301
0
        }
302
0
        if (strcmp(s->x_dirty_bitmap, "qemu:allocation-depth") == 0) {
303
0
            s->alloc_depth = true;
304
0
        }
305
0
    }
306
307
0
    if (s->info.flags & NBD_FLAG_READ_ONLY) {
308
0
        ret = bdrv_apply_auto_read_only(bs, "NBD export is read-only", errp);
309
0
        if (ret < 0) {
310
0
            return ret;
311
0
        }
312
0
    }
313
314
0
    if (s->info.flags & NBD_FLAG_SEND_FUA) {
315
0
        bs->supported_write_flags = BDRV_REQ_FUA;
316
0
        bs->supported_zero_flags |= BDRV_REQ_FUA;
317
0
    }
318
319
0
    if (s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES) {
320
0
        bs->supported_zero_flags |= BDRV_REQ_MAY_UNMAP;
321
0
        if (s->info.flags & NBD_FLAG_SEND_FAST_ZERO) {
322
0
            bs->supported_zero_flags |= BDRV_REQ_NO_FALLBACK;
323
0
        }
324
0
    }
325
326
0
    trace_nbd_client_handshake_success(s->export);
327
328
0
    return 0;
329
0
}
330
331
int coroutine_fn nbd_co_do_establish_connection(BlockDriverState *bs,
332
                                                bool blocking, Error **errp)
333
0
{
334
0
    BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
335
0
    int ret;
336
0
    IO_CODE();
337
338
0
    assert_bdrv_graph_readable();
339
0
    assert(!s->ioc);
340
341
0
    s->ioc = nbd_co_establish_connection(s->conn, &s->info, blocking, errp);
342
0
    if (!s->ioc) {
343
0
        return -ECONNREFUSED;
344
0
    }
345
346
0
    yank_register_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), nbd_yank,
347
0
                           bs);
348
349
0
    ret = nbd_handle_updated_info(s->bs, NULL);
350
0
    if (ret < 0) {
351
        /*
352
         * We have connected, but must fail for other reasons.
353
         * Send NBD_CMD_DISC as a courtesy to the server.
354
         */
355
0
        NBDRequest request = { .type = NBD_CMD_DISC, .mode = s->info.mode };
356
357
0
        nbd_send_request(s->ioc, &request);
358
359
0
        yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name),
360
0
                                 nbd_yank, bs);
361
0
        object_unref(OBJECT(s->ioc));
362
0
        s->ioc = NULL;
363
364
0
        return ret;
365
0
    }
366
367
0
    if (!qio_channel_set_blocking(s->ioc, false, errp)) {
368
0
        return -EINVAL;
369
0
    }
370
0
    qio_channel_set_follow_coroutine_ctx(s->ioc, true);
371
372
    /* successfully connected */
373
0
    WITH_QEMU_LOCK_GUARD(&s->requests_lock) {
374
0
        s->state = NBD_CLIENT_CONNECTED;
375
0
    }
376
377
0
    return 0;
378
0
}
379
380
/* Called with s->requests_lock held.  */
381
static bool nbd_client_connecting(BDRVNBDState *s)
382
0
{
383
0
    return s->state == NBD_CLIENT_CONNECTING_WAIT ||
384
0
        s->state == NBD_CLIENT_CONNECTING_NOWAIT;
385
0
}
386
387
/* Called with s->requests_lock taken.  */
388
static void coroutine_fn GRAPH_RDLOCK nbd_reconnect_attempt(BDRVNBDState *s)
389
0
{
390
0
    int ret;
391
0
    bool blocking = s->state == NBD_CLIENT_CONNECTING_WAIT;
392
393
    /*
394
     * Now we are sure that nobody is accessing the channel, and no one will
395
     * try until we set the state to CONNECTED.
396
     */
397
0
    assert(nbd_client_connecting(s));
398
0
    assert(s->in_flight == 1);
399
400
0
    trace_nbd_reconnect_attempt(s->bs->in_flight);
401
402
0
    if (blocking && !s->reconnect_delay_timer) {
403
        /*
404
         * It's the first reconnect attempt after switching to
405
         * NBD_CLIENT_CONNECTING_WAIT
406
         */
407
0
        g_assert(s->reconnect_delay);
408
0
        reconnect_delay_timer_init(s,
409
0
            qemu_clock_get_ns(QEMU_CLOCK_REALTIME) +
410
0
            s->reconnect_delay * NANOSECONDS_PER_SECOND);
411
0
    }
412
413
    /* Finalize previous connection if any */
414
0
    if (s->ioc) {
415
0
        yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name),
416
0
                                 nbd_yank, s->bs);
417
0
        object_unref(OBJECT(s->ioc));
418
0
        s->ioc = NULL;
419
0
    }
420
421
0
    qemu_mutex_unlock(&s->requests_lock);
422
0
    ret = nbd_co_do_establish_connection(s->bs, blocking, NULL);
423
0
    trace_nbd_reconnect_attempt_result(ret, s->bs->in_flight);
424
0
    qemu_mutex_lock(&s->requests_lock);
425
426
    /*
427
     * The reconnect attempt is done (maybe successfully, maybe not), so
428
     * we no longer need this timer.  Delete it so it will not outlive
429
     * this I/O request (so draining removes all timers).
430
     */
431
0
    reconnect_delay_timer_del(s);
432
0
}
433
434
static coroutine_fn int nbd_receive_replies(BDRVNBDState *s, uint64_t cookie,
435
                                            Error **errp)
436
0
{
437
0
    int ret;
438
0
    NBDClientRequest *req = nbd_request_by_cookie(s, cookie, &error_abort);
439
0
    NBDClientRequest *owner;
440
0
    QEMU_LOCK_GUARD(&s->receive_mutex);
441
442
0
    while (true) {
443
0
        if (s->reply.cookie == cookie) {
444
            /* We are done */
445
0
            return 0;
446
0
        }
447
448
0
        if (s->reply.cookie != 0) {
449
            /*
450
             * Some other request is being handled now. It should already be
451
             * woken by whoever set s->reply.cookie (or never wait in this
452
             * yield). So, we should not wake it here.
453
             */
454
0
            owner = nbd_request_by_cookie(s, s->reply.cookie, &error_abort);
455
0
            assert(!owner->receiving);
456
457
0
            req->receiving = true;
458
0
            qemu_co_mutex_unlock(&s->receive_mutex);
459
460
0
            qemu_coroutine_yield();
461
            /*
462
             * We may be woken for 2 reasons:
463
             * 1. From this function, executing in parallel coroutine, when our
464
             *    cookie is received.
465
             * 2. From nbd_co_receive_one_chunk(), when previous request is
466
             *    finished and s->reply.cookie set to 0.
467
             * Anyway, it's OK to lock the mutex and go to the next iteration.
468
             */
469
470
0
            qemu_co_mutex_lock(&s->receive_mutex);
471
0
            assert(!req->receiving);
472
0
            continue;
473
0
        }
474
475
        /* We are under mutex and cookie is 0. We have to do the dirty work. */
476
0
        assert(s->reply.cookie == 0);
477
0
        ret = nbd_receive_reply(s->bs, s->ioc, &s->reply, s->info.mode, errp);
478
0
        if (ret == 0) {
479
0
            ret = -EIO;
480
0
            error_setg(errp, "server dropped connection");
481
0
        }
482
0
        if (ret < 0) {
483
0
            goto err;
484
0
        }
485
0
        if (nbd_reply_is_structured(&s->reply) &&
486
0
            s->info.mode < NBD_MODE_STRUCTURED) {
487
0
            ret = -EINVAL;
488
0
            error_setg(errp, "unexpected structured reply");
489
0
            goto err;
490
0
        }
491
0
        owner = nbd_request_by_cookie(s, s->reply.cookie, errp);
492
0
        if (!owner) {
493
0
            ret = -EINVAL;
494
0
            goto err;
495
0
        }
496
0
        if (s->reply.cookie == cookie) {
497
            /* We are done */
498
0
            return 0;
499
0
        }
500
0
        nbd_recv_coroutine_wake_one(owner);
501
0
    }
502
503
0
err:
504
    /* Waiters look at this cookie, so do not leave a rejected one behind. */
505
0
    s->reply.cookie = 0;
506
0
    nbd_channel_error(s, ret);
507
508
0
    return ret;
509
0
}
510
511
static int coroutine_fn GRAPH_RDLOCK
512
nbd_co_send_request(BlockDriverState *bs, NBDRequest *request,
513
                    QEMUIOVector *qiov)
514
0
{
515
0
    BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
516
0
    int rc, i = -1;
517
518
0
    qemu_mutex_lock(&s->requests_lock);
519
0
    while (s->in_flight == MAX_NBD_REQUESTS ||
520
0
           (s->state != NBD_CLIENT_CONNECTED && s->in_flight > 0)) {
521
0
        qemu_co_queue_wait(&s->free_sema, &s->requests_lock);
522
0
    }
523
524
0
    s->in_flight++;
525
0
    if (s->state != NBD_CLIENT_CONNECTED) {
526
0
        if (nbd_client_connecting(s)) {
527
0
            nbd_reconnect_attempt(s);
528
0
            qemu_co_queue_restart_all(&s->free_sema);
529
0
        }
530
0
        if (s->state != NBD_CLIENT_CONNECTED) {
531
0
            rc = -EIO;
532
0
            goto err;
533
0
        }
534
0
    }
535
536
0
    for (i = 0; i < MAX_NBD_REQUESTS; i++) {
537
0
        if (s->requests[i].coroutine == NULL) {
538
0
            break;
539
0
        }
540
0
    }
541
542
0
    assert(i < MAX_NBD_REQUESTS);
543
0
    s->requests[i].coroutine = qemu_coroutine_self();
544
0
    s->requests[i].offset = request->from;
545
0
    s->requests[i].receiving = false;
546
0
    qemu_mutex_unlock(&s->requests_lock);
547
548
0
    qemu_co_mutex_lock(&s->send_mutex);
549
0
    request->cookie = INDEX_TO_COOKIE(i);
550
0
    request->mode = s->info.mode;
551
552
0
    assert(s->ioc);
553
554
0
    if (qiov) {
555
0
        qio_channel_set_cork(s->ioc, true);
556
0
        rc = nbd_send_request(s->ioc, request);
557
0
        if (rc >= 0 && qio_channel_writev_all(s->ioc, qiov->iov, qiov->niov,
558
0
                                              NULL) < 0) {
559
0
            rc = -EIO;
560
0
        }
561
0
        qio_channel_set_cork(s->ioc, false);
562
0
    } else {
563
0
        rc = nbd_send_request(s->ioc, request);
564
0
    }
565
0
    qemu_co_mutex_unlock(&s->send_mutex);
566
567
0
    if (rc < 0) {
568
0
        qemu_mutex_lock(&s->requests_lock);
569
0
err:
570
0
        nbd_channel_error_locked(s, rc);
571
0
        if (i != -1) {
572
0
            s->requests[i].coroutine = NULL;
573
0
        }
574
0
        s->in_flight--;
575
0
        qemu_co_queue_next(&s->free_sema);
576
0
        qemu_mutex_unlock(&s->requests_lock);
577
0
    }
578
0
    return rc;
579
0
}
580
581
/* Consume a field of payload, while swapping it in place to host-endian. */
582
static inline uint16_t payload_advance16(uint8_t **payload)
583
0
{
584
0
    uint16_t value = lduw_be_p(*payload);
585
0
    stw_he_p(*payload, value);
586
0
    *payload += 2;
587
0
    return value;
588
0
}
589
590
static inline uint32_t payload_advance32(uint8_t **payload)
591
0
{
592
0
    uint32_t value = ldl_be_p(*payload);
593
0
    stl_he_p(*payload, value);
594
0
    *payload += 4;
595
0
    return value;
596
0
}
597
598
static inline uint64_t payload_advance64(uint8_t **payload)
599
0
{
600
0
    uint64_t value = ldq_be_p(*payload);
601
0
    stq_he_p(*payload, value);
602
0
    *payload += 8;
603
0
    return value;
604
0
}
605
606
/*
607
 * nbd_parse_offset_hole_payload:
608
 * Parse a chunk for NBD_REPLY_TYPE_OFFSET_HOLE, updating payload in
609
 * place to a host-endian description of the region described.
610
 */
611
static int nbd_parse_offset_hole_payload(BDRVNBDState *s,
612
                                         NBDStructuredReplyChunk *chunk,
613
                                         uint8_t *payload, uint64_t orig_offset,
614
                                         QEMUIOVector *qiov, Error **errp)
615
0
{
616
0
    uint64_t offset;
617
0
    uint32_t hole_size;
618
619
0
    if (chunk->length != sizeof(offset) + sizeof(hole_size)) {
620
0
        error_setg(errp, "Protocol error: invalid payload for "
621
0
                         "NBD_REPLY_TYPE_OFFSET_HOLE");
622
0
        return -EINVAL;
623
0
    }
624
625
0
    offset = payload_advance64(&payload);
626
0
    hole_size = payload_advance32(&payload);
627
628
0
    if (!hole_size || offset < orig_offset || hole_size > qiov->size ||
629
0
        offset > orig_offset + qiov->size - hole_size) {
630
0
        error_setg(errp, "Protocol error: server sent chunk exceeding requested"
631
0
                         " region");
632
0
        return -EINVAL;
633
0
    }
634
0
    if (s->info.min_block &&
635
0
        !QEMU_IS_ALIGNED(hole_size, s->info.min_block)) {
636
0
        trace_nbd_structured_read_compliance("hole");
637
0
    }
638
639
0
    qemu_iovec_memset(qiov, offset - orig_offset, 0, hole_size);
640
641
0
    return 0;
642
0
}
643
644
/*
645
 * nbd_parse_blockstatus_payload
646
 * Based on our request, we expect only one extent in reply, for the
647
 * base:allocation context.
648
 */
649
static int nbd_parse_blockstatus_payload(BDRVNBDState *s,
650
                                         NBDStructuredReplyChunk *chunk,
651
                                         uint8_t *payload, bool wide,
652
                                         uint64_t orig_length,
653
                                         NBDExtent64 *extent, Error **errp)
654
0
{
655
0
    uint32_t context_id;
656
0
    uint32_t count;
657
0
    size_t ext_len = wide ? sizeof(*extent) : sizeof(NBDExtent32);
658
0
    size_t pay_len = sizeof(context_id) + wide * sizeof(count) + ext_len;
659
660
    /* The server succeeded, so it must have sent [at least] one extent */
661
0
    if (chunk->length < pay_len) {
662
0
        error_setg(errp, "Protocol error: invalid payload for "
663
0
                         "NBD_REPLY_TYPE_BLOCK_STATUS");
664
0
        return -EINVAL;
665
0
    }
666
667
0
    context_id = payload_advance32(&payload);
668
0
    if (s->info.context_id != context_id) {
669
0
        error_setg(errp, "Protocol error: unexpected context id %d for "
670
0
                         "NBD_REPLY_TYPE_BLOCK_STATUS, when negotiated context "
671
0
                         "id is %d", context_id,
672
0
                         s->info.context_id);
673
0
        return -EINVAL;
674
0
    }
675
676
0
    if (wide) {
677
0
        count = payload_advance32(&payload);
678
0
        extent->length = payload_advance64(&payload);
679
0
        extent->flags = payload_advance64(&payload);
680
0
    } else {
681
0
        count = 0;
682
0
        extent->length = payload_advance32(&payload);
683
0
        extent->flags = payload_advance32(&payload);
684
0
    }
685
686
0
    if (extent->length == 0) {
687
0
        error_setg(errp, "Protocol error: server sent status chunk with "
688
0
                   "zero length");
689
0
        return -EINVAL;
690
0
    }
691
692
    /*
693
     * A server sending unaligned block status is in violation of the
694
     * protocol, but as qemu-nbd 3.1 is such a server (at least for
695
     * POSIX files that are not a multiple of 512 bytes, since qemu
696
     * rounds files up to 512-byte multiples but lseek(SEEK_HOLE)
697
     * still sees an implicit hole beyond the real EOF), it's nicer to
698
     * work around the misbehaving server. If the request included
699
     * more than the final unaligned block, truncate it back to an
700
     * aligned result; if the request was only the final block, round
701
     * up to the full block and change the status to fully-allocated
702
     * (always a safe status, even if it loses information).
703
     */
704
0
    if (s->info.min_block && !QEMU_IS_ALIGNED(extent->length,
705
0
                                              s->info.min_block)) {
706
0
        trace_nbd_parse_blockstatus_compliance("extent length is unaligned");
707
0
        if (extent->length > s->info.min_block) {
708
0
            extent->length = QEMU_ALIGN_DOWN(extent->length,
709
0
                                             s->info.min_block);
710
0
        } else {
711
0
            extent->length = s->info.min_block;
712
0
            extent->flags = 0;
713
0
        }
714
0
    }
715
716
    /*
717
     * We used NBD_CMD_FLAG_REQ_ONE, so the server should not have
718
     * sent us any more than one extent, nor should it have included
719
     * status beyond our request in that extent. Furthermore, a wide
720
     * server should have replied with an accurate count (we left
721
     * count at 0 for a narrow server).  However, it's easy enough to
722
     * ignore the server's noncompliance without killing the
723
     * connection; just ignore trailing extents, and clamp things to
724
     * the length of our request.
725
     */
726
0
    if (count != wide || chunk->length > pay_len) {
727
0
        trace_nbd_parse_blockstatus_compliance("unexpected extent count");
728
0
    }
729
0
    if (extent->length > orig_length) {
730
0
        extent->length = orig_length;
731
0
        trace_nbd_parse_blockstatus_compliance("extent length too large");
732
0
    }
733
734
    /*
735
     * HACK: if we are using x-dirty-bitmaps to access
736
     * qemu:allocation-depth, treat all depths > 2 the same as 2,
737
     * since nbd_client_co_block_status is only expecting the low two
738
     * bits to be set.
739
     */
740
0
    if (s->alloc_depth && extent->flags > 2) {
741
0
        extent->flags = 2;
742
0
    }
743
744
0
    return 0;
745
0
}
746
747
/*
748
 * nbd_parse_error_payload
749
 * on success @errp contains message describing nbd error reply
750
 */
751
static int nbd_parse_error_payload(NBDStructuredReplyChunk *chunk,
752
                                   uint8_t *payload, int *request_ret,
753
                                   Error **errp)
754
0
{
755
0
    uint32_t error;
756
0
    uint16_t message_size;
757
758
0
    assert(chunk->type & (1 << 15));
759
760
0
    if (chunk->length < sizeof(error) + sizeof(message_size)) {
761
0
        error_setg(errp,
762
0
                   "Protocol error: invalid payload for structured error");
763
0
        return -EINVAL;
764
0
    }
765
766
0
    error = nbd_errno_to_system_errno(payload_advance32(&payload));
767
0
    if (error == 0) {
768
0
        error_setg(errp, "Protocol error: server sent structured error chunk "
769
0
                         "with error = 0");
770
0
        return -EINVAL;
771
0
    }
772
773
0
    *request_ret = -error;
774
0
    message_size = payload_advance16(&payload);
775
776
0
    if (message_size > chunk->length - sizeof(error) - sizeof(message_size)) {
777
0
        error_setg(errp, "Protocol error: server sent structured error chunk "
778
0
                         "with incorrect message size");
779
0
        return -EINVAL;
780
0
    }
781
782
    /* TODO: Add a trace point to mention the server complaint */
783
784
    /* TODO handle ERROR_OFFSET */
785
786
0
    return 0;
787
0
}
788
789
/*
790
 * nbd_co_receive_offset_data_payload:
791
 * Helper to nbd_co_do_receive_one_chunk when server replies with
792
 * structured reply to NBD_CMD_READ with an OFFSET_DATA chunk. On
793
 * success, synthesize an NBDStructuredReadHole into @payload
794
 * describing the same region that was populated into @qiov.
795
 */
796
static int coroutine_fn
797
nbd_co_receive_offset_data_payload(BDRVNBDState *s, uint64_t orig_offset,
798
                                   QEMUIOVector *qiov, void **payload,
799
                                   Error **errp)
800
0
{
801
0
    QEMUIOVector sub_qiov;
802
0
    uint64_t offset;
803
0
    size_t data_size;
804
0
    int ret;
805
0
    NBDStructuredReplyChunk *chunk = &s->reply.structured;
806
807
0
    assert(nbd_reply_is_structured(&s->reply));
808
0
    assert(payload);
809
810
    /* The NBD spec requires at least one byte of payload */
811
0
    if (chunk->length <= sizeof(offset)) {
812
0
        error_setg(errp, "Protocol error: invalid payload for "
813
0
                         "NBD_REPLY_TYPE_OFFSET_DATA");
814
0
        return -EINVAL;
815
0
    }
816
817
0
    if (nbd_read64(s->ioc, &offset, "OFFSET_DATA offset", errp) < 0) {
818
0
        return -EIO;
819
0
    }
820
821
0
    data_size = chunk->length - sizeof(offset);
822
0
    assert(data_size);
823
0
    if (offset < orig_offset || data_size > qiov->size ||
824
0
        offset > orig_offset + qiov->size - data_size) {
825
0
        error_setg(errp, "Protocol error: server sent chunk exceeding requested"
826
0
                         " region");
827
0
        return -EINVAL;
828
0
    }
829
0
    if (s->info.min_block && !QEMU_IS_ALIGNED(data_size, s->info.min_block)) {
830
0
        trace_nbd_structured_read_compliance("data");
831
0
    }
832
833
0
    qemu_iovec_init(&sub_qiov, qiov->niov);
834
0
    qemu_iovec_concat(&sub_qiov, qiov, offset - orig_offset, data_size);
835
0
    ret = qio_channel_readv_all(s->ioc, sub_qiov.iov, sub_qiov.niov, errp);
836
0
    qemu_iovec_destroy(&sub_qiov);
837
838
0
    if (ret == 0) {
839
        /*
840
         * Give the caller a struct describing the same region we
841
         * populated here, to allow for easier followup accounting
842
         * between data and hole reads.
843
         */
844
0
        NBDStructuredReadHole *synth = g_new(NBDStructuredReadHole, 1);
845
0
        synth->offset = offset;
846
0
        synth->length = data_size;
847
0
        *payload = synth;
848
0
    }
849
850
0
    return ret < 0 ? -EIO : 0;
851
0
}
852
853
0
#define NBD_MAX_MALLOC_PAYLOAD 1000
854
static coroutine_fn int nbd_co_receive_structured_payload(
855
        BDRVNBDState *s, void **payload, Error **errp)
856
0
{
857
0
    int ret;
858
0
    uint32_t len;
859
860
0
    assert(nbd_reply_is_structured(&s->reply));
861
862
0
    len = s->reply.structured.length;
863
864
0
    if (len == 0) {
865
0
        return 0;
866
0
    }
867
868
0
    if (payload == NULL) {
869
0
        error_setg(errp, "Unexpected structured payload");
870
0
        return -EINVAL;
871
0
    }
872
873
0
    if (len > NBD_MAX_MALLOC_PAYLOAD) {
874
0
        error_setg(errp, "Payload too large");
875
0
        return -EINVAL;
876
0
    }
877
878
0
    *payload = g_new(char, len);
879
0
    ret = nbd_read(s->ioc, *payload, len, "structured payload", errp);
880
0
    if (ret < 0) {
881
0
        g_free(*payload);
882
0
        *payload = NULL;
883
0
        return ret;
884
0
    }
885
886
0
    return 0;
887
0
}
888
889
/*
890
 * nbd_co_do_receive_one_chunk
891
 * @qiov is non-NULL for NBD_CMD_READ, NULL for all other commands.
892
 * for simple reply:
893
 *   set request_ret to received reply error
894
 *   if qiov is not NULL: read data to @qiov
895
 *   either way, @payload stays NULL
896
 * for structured reply chunk:
897
 *   if error chunk: read chunk payload, set @request_ret, @payload is NULL
898
 *   else if offset_data chunk and @qiov: read data to @qiov, allocate
899
 *     @payload as NBDStructuredReadHole describing region just populated
900
 *   else: read payload to @payload; caller checks size and contents
901
 *
902
 * If function fails, @errp contains corresponding error message, @payload
903
 * is NULL, and the connection with the server is suspect.  If it returns 0,
904
 * then the transaction succeeded (although @request_ret may be a negative
905
 * errno corresponding to the server's error reply), and errp is unchanged.
906
 */
907
static coroutine_fn int nbd_co_do_receive_one_chunk(
908
        BDRVNBDState *s, uint64_t cookie, bool only_structured,
909
        int *request_ret, QEMUIOVector *qiov, void **payload, Error **errp)
910
0
{
911
0
    ERRP_GUARD();
912
0
    int ret;
913
0
    void *local_payload = NULL;
914
0
    NBDStructuredReplyChunk *chunk;
915
916
0
    if (payload) {
917
0
        *payload = NULL;
918
0
    }
919
0
    *request_ret = 0;
920
921
0
    ret = nbd_receive_replies(s, cookie, errp);
922
0
    if (ret < 0) {
923
0
        error_prepend(errp, "Connection closed: ");
924
0
        return -EIO;
925
0
    }
926
0
    assert(s->ioc);
927
928
0
    assert(s->reply.cookie == cookie);
929
930
0
    if (nbd_reply_is_simple(&s->reply)) {
931
0
        if (only_structured) {
932
0
            error_setg(errp, "Protocol error: simple reply when structured "
933
0
                             "reply chunk was expected");
934
0
            return -EINVAL;
935
0
        }
936
937
0
        *request_ret = -nbd_errno_to_system_errno(s->reply.simple.error);
938
0
        if (*request_ret < 0 || !qiov) {
939
0
            return 0;
940
0
        }
941
942
0
        return qio_channel_readv_all(s->ioc, qiov->iov, qiov->niov,
943
0
                                     errp) < 0 ? -EIO : 0;
944
0
    }
945
946
    /* handle structured reply chunk */
947
0
    assert(s->info.mode >= NBD_MODE_STRUCTURED);
948
0
    chunk = &s->reply.structured;
949
950
0
    if (chunk->type == NBD_REPLY_TYPE_NONE) {
951
0
        if (!(chunk->flags & NBD_REPLY_FLAG_DONE)) {
952
0
            error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk without"
953
0
                       " NBD_REPLY_FLAG_DONE flag set");
954
0
            return -EINVAL;
955
0
        }
956
0
        if (chunk->length) {
957
0
            error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk with"
958
0
                       " nonzero length");
959
0
            return -EINVAL;
960
0
        }
961
0
        return 0;
962
0
    }
963
964
0
    if (chunk->type == NBD_REPLY_TYPE_OFFSET_DATA) {
965
0
        if (!qiov) {
966
0
            error_setg(errp, "Unexpected NBD_REPLY_TYPE_OFFSET_DATA chunk");
967
0
            return -EINVAL;
968
0
        }
969
970
0
        return nbd_co_receive_offset_data_payload(
971
0
                s, nbd_request_by_cookie(s, cookie, &error_abort)->offset,
972
0
                qiov, payload, errp);
973
0
    }
974
975
0
    if (nbd_reply_type_is_error(chunk->type)) {
976
0
        payload = &local_payload;
977
0
    }
978
979
0
    ret = nbd_co_receive_structured_payload(s, payload, errp);
980
0
    if (ret < 0) {
981
0
        return ret;
982
0
    }
983
984
0
    if (nbd_reply_type_is_error(chunk->type)) {
985
0
        ret = nbd_parse_error_payload(chunk, local_payload, request_ret, errp);
986
0
        g_free(local_payload);
987
0
        return ret;
988
0
    }
989
990
0
    return 0;
991
0
}
992
993
/*
994
 * nbd_co_receive_one_chunk
995
 * Read reply, wake up connection_co and set s->quit if needed.
996
 * Return value is a fatal error code or normal nbd reply error code
997
 */
998
static coroutine_fn int nbd_co_receive_one_chunk(
999
        BDRVNBDState *s, uint64_t cookie, bool only_structured,
1000
        int *request_ret, QEMUIOVector *qiov, NBDReply *reply, void **payload,
1001
        Error **errp)
1002
0
{
1003
0
    int ret = nbd_co_do_receive_one_chunk(s, cookie, only_structured,
1004
0
                                          request_ret, qiov, payload, errp);
1005
1006
0
    if (ret < 0) {
1007
0
        memset(reply, 0, sizeof(*reply));
1008
0
        nbd_channel_error(s, ret);
1009
0
    } else {
1010
        /* For assert at loop start in nbd_connection_entry */
1011
0
        *reply = s->reply;
1012
0
    }
1013
1014
0
    WITH_QEMU_LOCK_GUARD(&s->receive_mutex) {
1015
0
        s->reply.cookie = 0;
1016
0
        nbd_recv_coroutines_wake(s);
1017
0
    }
1018
1019
0
    return ret;
1020
0
}
1021
1022
typedef struct NBDReplyChunkIter {
1023
    int ret;
1024
    int request_ret;
1025
    Error *err;
1026
    bool done, only_structured;
1027
} NBDReplyChunkIter;
1028
1029
static void nbd_iter_channel_error(NBDReplyChunkIter *iter,
1030
                                   int ret, Error **local_err)
1031
0
{
1032
0
    assert(local_err && *local_err);
1033
0
    assert(ret < 0);
1034
1035
0
    if (!iter->ret) {
1036
0
        iter->ret = ret;
1037
0
        error_propagate(&iter->err, *local_err);
1038
0
    } else {
1039
0
        error_free(*local_err);
1040
0
    }
1041
1042
0
    *local_err = NULL;
1043
0
}
1044
1045
static void nbd_iter_request_error(NBDReplyChunkIter *iter, int ret)
1046
0
{
1047
0
    assert(ret < 0);
1048
1049
0
    if (!iter->request_ret) {
1050
0
        iter->request_ret = ret;
1051
0
    }
1052
0
}
1053
1054
/*
1055
 * NBD_FOREACH_REPLY_CHUNK
1056
 * The pointer stored in @payload requires g_free() to free it.
1057
 */
1058
#define NBD_FOREACH_REPLY_CHUNK(s, iter, cookie, structured, \
1059
                                qiov, reply, payload) \
1060
0
    for (iter = (NBDReplyChunkIter) { .only_structured = structured }; \
1061
0
         nbd_reply_chunk_iter_receive(s, &iter, cookie, qiov, reply, payload);)
1062
1063
/*
1064
 * nbd_reply_chunk_iter_receive
1065
 * The pointer stored in @payload requires g_free() to free it.
1066
 */
1067
static bool coroutine_fn nbd_reply_chunk_iter_receive(BDRVNBDState *s,
1068
                                                      NBDReplyChunkIter *iter,
1069
                                                      uint64_t cookie,
1070
                                                      QEMUIOVector *qiov,
1071
                                                      NBDReply *reply,
1072
                                                      void **payload)
1073
0
{
1074
0
    int ret, request_ret;
1075
0
    NBDReply local_reply;
1076
0
    NBDStructuredReplyChunk *chunk;
1077
0
    Error *local_err = NULL;
1078
1079
0
    if (iter->done) {
1080
        /* Previous iteration was last. */
1081
0
        goto break_loop;
1082
0
    }
1083
1084
0
    if (reply == NULL) {
1085
0
        reply = &local_reply;
1086
0
    }
1087
1088
0
    ret = nbd_co_receive_one_chunk(s, cookie, iter->only_structured,
1089
0
                                   &request_ret, qiov, reply, payload,
1090
0
                                   &local_err);
1091
0
    if (ret < 0) {
1092
0
        nbd_iter_channel_error(iter, ret, &local_err);
1093
0
    } else if (request_ret < 0) {
1094
0
        nbd_iter_request_error(iter, request_ret);
1095
0
    }
1096
1097
    /* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */
1098
0
    if (nbd_reply_is_simple(reply) || iter->ret < 0) {
1099
0
        goto break_loop;
1100
0
    }
1101
1102
0
    chunk = &reply->structured;
1103
0
    iter->only_structured = true;
1104
1105
0
    if (chunk->type == NBD_REPLY_TYPE_NONE) {
1106
        /* NBD_REPLY_FLAG_DONE is already checked in nbd_co_receive_one_chunk */
1107
0
        assert(chunk->flags & NBD_REPLY_FLAG_DONE);
1108
0
        goto break_loop;
1109
0
    }
1110
1111
0
    if (chunk->flags & NBD_REPLY_FLAG_DONE) {
1112
        /* This iteration is last. */
1113
0
        iter->done = true;
1114
0
    }
1115
1116
    /* Execute the loop body */
1117
0
    return true;
1118
1119
0
break_loop:
1120
0
    assert(payload == NULL || *payload == NULL);
1121
0
    qemu_mutex_lock(&s->requests_lock);
1122
0
    nbd_request_by_cookie(s, cookie, &error_abort)->coroutine = NULL;
1123
0
    s->in_flight--;
1124
0
    qemu_co_queue_next(&s->free_sema);
1125
0
    qemu_mutex_unlock(&s->requests_lock);
1126
1127
0
    return false;
1128
0
}
1129
1130
static int coroutine_fn
1131
nbd_co_receive_return_code(BDRVNBDState *s, uint64_t cookie,
1132
                           int *request_ret, Error **errp)
1133
0
{
1134
0
    NBDReplyChunkIter iter;
1135
1136
0
    NBD_FOREACH_REPLY_CHUNK(s, iter, cookie, false, NULL, NULL, NULL) {
1137
        /* nbd_reply_chunk_iter_receive does all the work */
1138
0
    }
1139
1140
0
    error_propagate(errp, iter.err);
1141
0
    *request_ret = iter.request_ret;
1142
0
    return iter.ret;
1143
0
}
1144
1145
static int coroutine_fn
1146
nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_t cookie,
1147
                             uint64_t offset, QEMUIOVector *qiov,
1148
                             int *request_ret, Error **errp)
1149
0
{
1150
0
    NBDReplyChunkIter iter;
1151
0
    NBDReply reply;
1152
0
    void *payload = NULL;
1153
0
    Error *local_err = NULL;
1154
0
    uint64_t highwater = offset;
1155
1156
0
    NBD_FOREACH_REPLY_CHUNK(s, iter, cookie,
1157
0
                            s->info.mode >= NBD_MODE_STRUCTURED,
1158
0
                            qiov, &reply, &payload)
1159
0
    {
1160
0
        int ret;
1161
0
        NBDStructuredReplyChunk *chunk = &reply.structured;
1162
0
        NBDStructuredReadHole *slice = NULL;
1163
1164
0
        assert(nbd_reply_is_structured(&reply));
1165
1166
0
        switch (chunk->type) {
1167
0
        case NBD_REPLY_TYPE_OFFSET_DATA:
1168
            /*
1169
             * special cased in nbd_co_receive_one_chunk, data is already
1170
             * in qiov, and payload is host-endian offset+length.
1171
             */
1172
0
            slice = payload;
1173
0
            break;
1174
0
        case NBD_REPLY_TYPE_OFFSET_HOLE:
1175
0
            ret = nbd_parse_offset_hole_payload(s, &reply.structured, payload,
1176
0
                                                offset, qiov, &local_err);
1177
0
            if (ret < 0) {
1178
0
                nbd_channel_error(s, ret);
1179
0
                nbd_iter_channel_error(&iter, ret, &local_err);
1180
0
            } else {
1181
                /* Successful parse swapped payload to host-endian. */
1182
0
                slice = payload;
1183
0
            }
1184
0
            break;
1185
0
        default:
1186
0
            if (!nbd_reply_type_is_error(chunk->type)) {
1187
                /* not allowed reply type */
1188
0
                nbd_channel_error(s, -EINVAL);
1189
0
                error_setg(&local_err,
1190
0
                           "Unexpected reply type: %d (%s) for CMD_READ",
1191
0
                           chunk->type, nbd_reply_type_lookup(chunk->type));
1192
0
                nbd_iter_channel_error(&iter, -EINVAL, &local_err);
1193
0
            }
1194
0
        }
1195
1196
        /* Initialize any region skipped by a malicious server. */
1197
0
        if (slice) {
1198
0
            if (slice->offset > highwater) {
1199
0
                qemu_iovec_memset(qiov, highwater - offset, 0,
1200
0
                                  slice->offset - highwater);
1201
0
            }
1202
0
            highwater = MAX(highwater, slice->offset + slice->length);
1203
0
        }
1204
1205
0
        g_free(payload);
1206
0
        payload = NULL;
1207
0
    }
1208
1209
    /* Initialize any trailing region not touched by server. */
1210
0
    if (iter.only_structured && iter.ret == 0 &&
1211
0
        highwater < offset + qiov->size) {
1212
0
        qemu_iovec_memset(qiov, highwater - offset, 0,
1213
0
                          offset + qiov->size - highwater);
1214
0
    }
1215
1216
0
    error_propagate(errp, iter.err);
1217
0
    *request_ret = iter.request_ret;
1218
0
    return iter.ret;
1219
0
}
1220
1221
static int coroutine_fn
1222
nbd_co_receive_blockstatus_reply(BDRVNBDState *s, uint64_t cookie,
1223
                                 uint64_t length, NBDExtent64 *extent,
1224
                                 int *request_ret, Error **errp)
1225
0
{
1226
0
    NBDReplyChunkIter iter;
1227
0
    NBDReply reply;
1228
0
    void *payload = NULL;
1229
0
    Error *local_err = NULL;
1230
0
    bool received = false;
1231
1232
0
    assert(!extent->length);
1233
0
    NBD_FOREACH_REPLY_CHUNK(s, iter, cookie, false, NULL, &reply, &payload) {
1234
0
        int ret;
1235
0
        NBDStructuredReplyChunk *chunk = &reply.structured;
1236
0
        bool wide;
1237
1238
0
        assert(nbd_reply_is_structured(&reply));
1239
1240
0
        switch (chunk->type) {
1241
0
        case NBD_REPLY_TYPE_BLOCK_STATUS_EXT:
1242
0
        case NBD_REPLY_TYPE_BLOCK_STATUS:
1243
0
            wide = chunk->type == NBD_REPLY_TYPE_BLOCK_STATUS_EXT;
1244
0
            if ((s->info.mode >= NBD_MODE_EXTENDED) != wide) {
1245
0
                trace_nbd_extended_headers_compliance("block_status");
1246
0
            }
1247
0
            if (received) {
1248
0
                nbd_channel_error(s, -EINVAL);
1249
0
                error_setg(&local_err, "Several BLOCK_STATUS chunks in reply");
1250
0
                nbd_iter_channel_error(&iter, -EINVAL, &local_err);
1251
0
            }
1252
0
            received = true;
1253
1254
0
            ret = nbd_parse_blockstatus_payload(
1255
0
                s, &reply.structured, payload, wide,
1256
0
                length, extent, &local_err);
1257
0
            if (ret < 0) {
1258
0
                nbd_channel_error(s, ret);
1259
0
                nbd_iter_channel_error(&iter, ret, &local_err);
1260
0
            }
1261
0
            break;
1262
0
        default:
1263
0
            if (!nbd_reply_type_is_error(chunk->type)) {
1264
0
                nbd_channel_error(s, -EINVAL);
1265
0
                error_setg(&local_err,
1266
0
                           "Unexpected reply type: %d (%s) "
1267
0
                           "for CMD_BLOCK_STATUS",
1268
0
                           chunk->type, nbd_reply_type_lookup(chunk->type));
1269
0
                nbd_iter_channel_error(&iter, -EINVAL, &local_err);
1270
0
            }
1271
0
        }
1272
1273
0
        g_free(payload);
1274
0
        payload = NULL;
1275
0
    }
1276
1277
0
    if (!extent->length && !iter.request_ret) {
1278
0
        error_setg(&local_err, "Server did not reply with any status extents");
1279
0
        nbd_iter_channel_error(&iter, -EIO, &local_err);
1280
0
    }
1281
1282
0
    error_propagate(errp, iter.err);
1283
0
    *request_ret = iter.request_ret;
1284
0
    return iter.ret;
1285
0
}
1286
1287
static int coroutine_fn GRAPH_RDLOCK
1288
nbd_co_request(BlockDriverState *bs, NBDRequest *request,
1289
               QEMUIOVector *write_qiov)
1290
0
{
1291
0
    int ret, request_ret;
1292
0
    Error *local_err = NULL;
1293
0
    BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1294
1295
0
    assert(request->type != NBD_CMD_READ);
1296
0
    if (write_qiov) {
1297
0
        assert(request->type == NBD_CMD_WRITE);
1298
0
        assert(request->len == iov_size(write_qiov->iov, write_qiov->niov));
1299
0
    } else {
1300
0
        assert(request->type != NBD_CMD_WRITE);
1301
0
    }
1302
1303
0
    do {
1304
0
        ret = nbd_co_send_request(bs, request, write_qiov);
1305
0
        if (ret < 0) {
1306
0
            continue;
1307
0
        }
1308
1309
0
        ret = nbd_co_receive_return_code(s, request->cookie,
1310
0
                                         &request_ret, &local_err);
1311
0
        if (local_err) {
1312
0
            trace_nbd_co_request_fail(request->from, request->len,
1313
0
                                      request->cookie, request->flags,
1314
0
                                      request->type,
1315
0
                                      nbd_cmd_lookup(request->type),
1316
0
                                      ret, error_get_pretty(local_err));
1317
0
            error_free(local_err);
1318
0
            local_err = NULL;
1319
0
        }
1320
0
    } while (ret < 0 && nbd_client_will_reconnect(s));
1321
1322
0
    return ret ? ret : request_ret;
1323
0
}
1324
1325
static int coroutine_fn GRAPH_RDLOCK
1326
nbd_client_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
1327
                     QEMUIOVector *qiov, BdrvRequestFlags flags)
1328
0
{
1329
0
    int ret, request_ret;
1330
0
    Error *local_err = NULL;
1331
0
    BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1332
0
    NBDRequest request = {
1333
0
        .type = NBD_CMD_READ,
1334
0
        .from = offset,
1335
0
        .len = bytes,
1336
0
    };
1337
1338
0
    assert(bytes <= NBD_MAX_BUFFER_SIZE);
1339
1340
0
    if (!bytes) {
1341
0
        return 0;
1342
0
    }
1343
    /*
1344
     * Work around the fact that the block layer doesn't do
1345
     * byte-accurate sizing yet - if the read exceeds the server's
1346
     * advertised size because the block layer rounded size up, then
1347
     * truncate the request to the server and tail-pad with zero.
1348
     */
1349
0
    if (offset >= s->info.size) {
1350
0
        assert(bytes < BDRV_SECTOR_SIZE);
1351
0
        qemu_iovec_memset(qiov, 0, 0, bytes);
1352
0
        return 0;
1353
0
    }
1354
0
    if (offset + bytes > s->info.size) {
1355
0
        uint64_t slop = offset + bytes - s->info.size;
1356
1357
0
        assert(slop < BDRV_SECTOR_SIZE);
1358
0
        qemu_iovec_memset(qiov, bytes - slop, 0, slop);
1359
0
        request.len -= slop;
1360
0
    }
1361
1362
0
    do {
1363
0
        ret = nbd_co_send_request(bs, &request, NULL);
1364
0
        if (ret < 0) {
1365
0
            continue;
1366
0
        }
1367
1368
0
        ret = nbd_co_receive_cmdread_reply(s, request.cookie, offset, qiov,
1369
0
                                           &request_ret, &local_err);
1370
0
        if (local_err) {
1371
0
            trace_nbd_co_request_fail(request.from, request.len, request.cookie,
1372
0
                                      request.flags, request.type,
1373
0
                                      nbd_cmd_lookup(request.type),
1374
0
                                      ret, error_get_pretty(local_err));
1375
0
            error_free(local_err);
1376
0
            local_err = NULL;
1377
0
        }
1378
0
    } while (ret < 0 && nbd_client_will_reconnect(s));
1379
1380
0
    return ret ? ret : request_ret;
1381
0
}
1382
1383
static int coroutine_fn GRAPH_RDLOCK
1384
nbd_client_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
1385
                      QEMUIOVector *qiov, BdrvRequestFlags flags)
1386
0
{
1387
0
    BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1388
0
    NBDRequest request = {
1389
0
        .type = NBD_CMD_WRITE,
1390
0
        .from = offset,
1391
0
        .len = bytes,
1392
0
    };
1393
1394
0
    assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1395
0
    if (flags & BDRV_REQ_FUA) {
1396
0
        assert(s->info.flags & NBD_FLAG_SEND_FUA);
1397
0
        request.flags |= NBD_CMD_FLAG_FUA;
1398
0
    }
1399
1400
0
    assert(bytes <= NBD_MAX_BUFFER_SIZE);
1401
1402
0
    if (!bytes) {
1403
0
        return 0;
1404
0
    }
1405
0
    return nbd_co_request(bs, &request, qiov);
1406
0
}
1407
1408
static int coroutine_fn GRAPH_RDLOCK
1409
nbd_client_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes,
1410
                            BdrvRequestFlags flags)
1411
0
{
1412
0
    BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1413
0
    NBDRequest request = {
1414
0
        .type = NBD_CMD_WRITE_ZEROES,
1415
0
        .from = offset,
1416
0
        .len = bytes,
1417
0
    };
1418
1419
    /* rely on max_pwrite_zeroes */
1420
0
    assert(bytes <= UINT32_MAX || s->info.mode >= NBD_MODE_EXTENDED);
1421
1422
0
    assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1423
0
    if (!(s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES)) {
1424
0
        return -ENOTSUP;
1425
0
    }
1426
1427
0
    if (flags & BDRV_REQ_FUA) {
1428
0
        assert(s->info.flags & NBD_FLAG_SEND_FUA);
1429
0
        request.flags |= NBD_CMD_FLAG_FUA;
1430
0
    }
1431
0
    if (!(flags & BDRV_REQ_MAY_UNMAP)) {
1432
0
        request.flags |= NBD_CMD_FLAG_NO_HOLE;
1433
0
    }
1434
0
    if (flags & BDRV_REQ_NO_FALLBACK) {
1435
0
        assert(s->info.flags & NBD_FLAG_SEND_FAST_ZERO);
1436
0
        request.flags |= NBD_CMD_FLAG_FAST_ZERO;
1437
0
    }
1438
1439
0
    if (!bytes) {
1440
0
        return 0;
1441
0
    }
1442
0
    return nbd_co_request(bs, &request, NULL);
1443
0
}
1444
1445
static int coroutine_fn GRAPH_RDLOCK nbd_client_co_flush(BlockDriverState *bs)
1446
0
{
1447
0
    BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1448
0
    NBDRequest request = { .type = NBD_CMD_FLUSH };
1449
1450
0
    if (!(s->info.flags & NBD_FLAG_SEND_FLUSH)) {
1451
0
        return 0;
1452
0
    }
1453
1454
0
    request.from = 0;
1455
0
    request.len = 0;
1456
1457
0
    return nbd_co_request(bs, &request, NULL);
1458
0
}
1459
1460
static int coroutine_fn GRAPH_RDLOCK
1461
nbd_client_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
1462
0
{
1463
0
    BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1464
0
    NBDRequest request = {
1465
0
        .type = NBD_CMD_TRIM,
1466
0
        .from = offset,
1467
0
        .len = bytes,
1468
0
    };
1469
1470
    /* rely on max_pdiscard */
1471
0
    assert(bytes <= UINT32_MAX || s->info.mode >= NBD_MODE_EXTENDED);
1472
1473
0
    assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1474
0
    if (!(s->info.flags & NBD_FLAG_SEND_TRIM) || !bytes) {
1475
0
        return 0;
1476
0
    }
1477
1478
0
    return nbd_co_request(bs, &request, NULL);
1479
0
}
1480
1481
static int coroutine_fn GRAPH_RDLOCK nbd_client_co_block_status(
1482
        BlockDriverState *bs, unsigned int mode, int64_t offset,
1483
        int64_t bytes, int64_t *pnum, int64_t *map, BlockDriverState **file)
1484
0
{
1485
0
    int ret, request_ret;
1486
0
    NBDExtent64 extent = { 0 };
1487
0
    BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1488
0
    Error *local_err = NULL;
1489
1490
0
    NBDRequest request = {
1491
0
        .type = NBD_CMD_BLOCK_STATUS,
1492
0
        .from = offset,
1493
0
        .len = MIN(bytes, s->info.size - offset),
1494
0
        .flags = NBD_CMD_FLAG_REQ_ONE,
1495
0
    };
1496
1497
0
    if (!s->info.base_allocation) {
1498
0
        *pnum = bytes;
1499
0
        *map = offset;
1500
0
        *file = bs;
1501
0
        return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
1502
0
    }
1503
0
    if (s->info.mode < NBD_MODE_EXTENDED) {
1504
0
        request.len = MIN(QEMU_ALIGN_DOWN(INT_MAX, bs->bl.request_alignment),
1505
0
                          request.len);
1506
0
    }
1507
1508
    /*
1509
     * Work around the fact that the block layer doesn't do
1510
     * byte-accurate sizing yet - if the status request exceeds the
1511
     * server's advertised size because the block layer rounded size
1512
     * up, we truncated the request to the server (above), or are
1513
     * called on just the hole.
1514
     */
1515
0
    if (offset >= s->info.size) {
1516
0
        *pnum = bytes;
1517
0
        assert(bytes < BDRV_SECTOR_SIZE);
1518
        /* Intentionally don't report offset_valid for the hole */
1519
0
        return BDRV_BLOCK_ZERO;
1520
0
    }
1521
1522
0
    if (s->info.min_block) {
1523
0
        assert(QEMU_IS_ALIGNED(request.len, s->info.min_block));
1524
0
    }
1525
0
    do {
1526
0
        ret = nbd_co_send_request(bs, &request, NULL);
1527
0
        if (ret < 0) {
1528
0
            continue;
1529
0
        }
1530
1531
0
        ret = nbd_co_receive_blockstatus_reply(s, request.cookie, bytes,
1532
0
                                               &extent, &request_ret,
1533
0
                                               &local_err);
1534
0
        if (local_err) {
1535
0
            trace_nbd_co_request_fail(request.from, request.len, request.cookie,
1536
0
                                      request.flags, request.type,
1537
0
                                      nbd_cmd_lookup(request.type),
1538
0
                                      ret, error_get_pretty(local_err));
1539
0
            error_free(local_err);
1540
0
            local_err = NULL;
1541
0
        }
1542
0
    } while (ret < 0 && nbd_client_will_reconnect(s));
1543
1544
0
    if (ret < 0 || request_ret < 0) {
1545
0
        return ret ? ret : request_ret;
1546
0
    }
1547
1548
0
    assert(extent.length);
1549
0
    *pnum = extent.length;
1550
0
    *map = offset;
1551
0
    *file = bs;
1552
0
    return (extent.flags & NBD_STATE_HOLE ? 0 : BDRV_BLOCK_DATA) |
1553
0
        (extent.flags & NBD_STATE_ZERO ? BDRV_BLOCK_ZERO : 0) |
1554
0
        BDRV_BLOCK_OFFSET_VALID;
1555
0
}
1556
1557
static int nbd_client_reopen_prepare(BDRVReopenState *state,
1558
                                     BlockReopenQueue *queue, Error **errp)
1559
0
{
1560
0
    BDRVNBDState *s = (BDRVNBDState *)state->bs->opaque;
1561
1562
0
    if ((state->flags & BDRV_O_RDWR) && (s->info.flags & NBD_FLAG_READ_ONLY)) {
1563
0
        error_setg(errp, "Can't reopen read-only NBD mount as read/write");
1564
0
        return -EACCES;
1565
0
    }
1566
0
    return 0;
1567
0
}
1568
1569
static void nbd_yank(void *opaque)
1570
0
{
1571
0
    BlockDriverState *bs = opaque;
1572
0
    BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1573
1574
0
    QEMU_LOCK_GUARD(&s->requests_lock);
1575
0
    qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
1576
0
    s->state = NBD_CLIENT_QUIT;
1577
0
}
1578
1579
static void nbd_client_close(BlockDriverState *bs)
1580
0
{
1581
0
    BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1582
0
    NBDRequest request = { .type = NBD_CMD_DISC, .mode = s->info.mode };
1583
1584
0
    if (s->ioc) {
1585
0
        nbd_send_request(s->ioc, &request);
1586
0
    }
1587
1588
0
    nbd_teardown_connection(bs);
1589
0
}
1590
1591
1592
/*
1593
 * Parse nbd_open options
1594
 */
1595
1596
static int nbd_parse_uri(const char *filename, QDict *options)
1597
0
{
1598
0
    g_autoptr(GUri) uri = g_uri_parse(filename, G_URI_FLAGS_NONE, NULL);
1599
0
    g_autoptr(GHashTable) qp = NULL;
1600
0
    const char *p;
1601
0
    int qp_n;
1602
0
    bool is_unix;
1603
0
    const char *uri_scheme, *uri_query, *uri_server;
1604
0
    int uri_port;
1605
1606
0
    if (!uri) {
1607
0
        return -EINVAL;
1608
0
    }
1609
1610
    /* transport */
1611
0
    uri_scheme = g_uri_get_scheme(uri);
1612
0
    if (!g_strcmp0(uri_scheme, "nbd")) {
1613
0
        is_unix = false;
1614
0
    } else if (!g_strcmp0(uri_scheme, "nbd+tcp")) {
1615
0
        is_unix = false;
1616
0
    } else if (!g_strcmp0(uri_scheme, "nbd+unix")) {
1617
0
        is_unix = true;
1618
0
    } else {
1619
0
        return -EINVAL;
1620
0
    }
1621
1622
0
    p = g_uri_get_path(uri) ?: "";
1623
0
    if (p[0] == '/') {
1624
0
        p++;
1625
0
    }
1626
0
    if (p[0]) {
1627
0
        qdict_put_str(options, "export", p);
1628
0
    }
1629
1630
0
    uri_query = g_uri_get_query(uri);
1631
0
    if (uri_query) {
1632
0
        qp = g_uri_parse_params(uri_query, -1, "&", G_URI_PARAMS_NONE, NULL);
1633
0
        if (!qp) {
1634
0
            return -EINVAL;
1635
0
        }
1636
0
        qp_n = g_hash_table_size(qp);
1637
0
        if (qp_n > 1 || (is_unix && !qp_n) || (!is_unix && qp_n)) {
1638
0
            return -EINVAL;
1639
0
        }
1640
0
     }
1641
1642
0
    uri_server = g_uri_get_host(uri);
1643
0
    if (uri_server && !uri_server[0]) {
1644
0
        uri_server = NULL;
1645
0
    }
1646
0
    uri_port = g_uri_get_port(uri);
1647
1648
0
    if (is_unix) {
1649
        /* nbd+unix:///export?socket=path */
1650
0
        const char *uri_socket = g_hash_table_lookup(qp, "socket");
1651
0
        if (uri_server || uri_port != -1 || !uri_socket) {
1652
0
            return -EINVAL;
1653
0
        }
1654
0
        qdict_put_str(options, "server.type", "unix");
1655
0
        qdict_put_str(options, "server.path", uri_socket);
1656
0
    } else {
1657
0
        char *port_str;
1658
1659
        /* nbd[+tcp]://host[:port]/export */
1660
0
        if (!uri_server) {
1661
0
            return -EINVAL;
1662
0
        }
1663
1664
0
        qdict_put_str(options, "server.type", "inet");
1665
0
        qdict_put_str(options, "server.host", uri_server);
1666
1667
0
        port_str = g_strdup_printf("%d", uri_port > 0 ? uri_port
1668
0
                                                      : NBD_DEFAULT_PORT);
1669
0
        qdict_put_str(options, "server.port", port_str);
1670
0
        g_free(port_str);
1671
0
    }
1672
1673
0
    return 0;
1674
0
}
1675
1676
static bool nbd_has_filename_options_conflict(QDict *options, Error **errp)
1677
0
{
1678
0
    const QDictEntry *e;
1679
1680
0
    for (e = qdict_first(options); e; e = qdict_next(options, e)) {
1681
0
        if (!strcmp(e->key, "host") ||
1682
0
            !strcmp(e->key, "port") ||
1683
0
            !strcmp(e->key, "path") ||
1684
0
            !strcmp(e->key, "export") ||
1685
0
            strstart(e->key, "server.", NULL))
1686
0
        {
1687
0
            error_setg(errp, "Option '%s' cannot be used with a file name",
1688
0
                       e->key);
1689
0
            return true;
1690
0
        }
1691
0
    }
1692
1693
0
    return false;
1694
0
}
1695
1696
static void nbd_parse_filename(const char *filename, QDict *options,
1697
                               Error **errp)
1698
0
{
1699
0
    g_autofree char *file = NULL;
1700
0
    char *export_name;
1701
0
    const char *host_spec;
1702
0
    const char *unixpath;
1703
1704
0
    if (nbd_has_filename_options_conflict(options, errp)) {
1705
0
        return;
1706
0
    }
1707
1708
0
    if (strstr(filename, "://")) {
1709
0
        int ret = nbd_parse_uri(filename, options);
1710
0
        if (ret < 0) {
1711
0
            error_setg(errp, "No valid URL specified");
1712
0
        }
1713
0
        return;
1714
0
    }
1715
1716
0
    file = g_strdup(filename);
1717
1718
0
    export_name = strstr(file, EN_OPTSTR);
1719
0
    if (export_name) {
1720
0
        if (export_name[strlen(EN_OPTSTR)] == 0) {
1721
0
            return;
1722
0
        }
1723
0
        export_name[0] = 0; /* truncate 'file' */
1724
0
        export_name += strlen(EN_OPTSTR);
1725
1726
0
        qdict_put_str(options, "export", export_name);
1727
0
    }
1728
1729
    /* extract the host_spec - fail if it's not nbd:... */
1730
0
    if (!strstart(file, "nbd:", &host_spec)) {
1731
0
        error_setg(errp, "File name string for NBD must start with 'nbd:'");
1732
0
        return;
1733
0
    }
1734
1735
0
    if (!*host_spec) {
1736
0
        return;
1737
0
    }
1738
1739
    /* are we a UNIX or TCP socket? */
1740
0
    if (strstart(host_spec, "unix:", &unixpath)) {
1741
0
        qdict_put_str(options, "server.type", "unix");
1742
0
        qdict_put_str(options, "server.path", unixpath);
1743
0
    } else {
1744
0
        InetSocketAddress *addr = g_new(InetSocketAddress, 1);
1745
1746
0
        if (inet_parse(addr, host_spec, errp)) {
1747
0
            goto out_inet;
1748
0
        }
1749
1750
0
        qdict_put_str(options, "server.type", "inet");
1751
0
        qdict_put_str(options, "server.host", addr->host);
1752
0
        qdict_put_str(options, "server.port", addr->port);
1753
0
    out_inet:
1754
0
        qapi_free_InetSocketAddress(addr);
1755
0
    }
1756
0
}
1757
1758
static bool nbd_process_legacy_socket_options(QDict *output_options,
1759
                                              QemuOpts *legacy_opts,
1760
                                              Error **errp)
1761
0
{
1762
0
    const char *path = qemu_opt_get(legacy_opts, "path");
1763
0
    const char *host = qemu_opt_get(legacy_opts, "host");
1764
0
    const char *port = qemu_opt_get(legacy_opts, "port");
1765
0
    const QDictEntry *e;
1766
1767
0
    if (!path && !host && !port) {
1768
0
        return true;
1769
0
    }
1770
1771
0
    for (e = qdict_first(output_options); e; e = qdict_next(output_options, e))
1772
0
    {
1773
0
        if (strstart(e->key, "server.", NULL)) {
1774
0
            error_setg(errp, "Cannot use 'server' and path/host/port at the "
1775
0
                       "same time");
1776
0
            return false;
1777
0
        }
1778
0
    }
1779
1780
0
    if (path && host) {
1781
0
        error_setg(errp, "path and host may not be used at the same time");
1782
0
        return false;
1783
0
    } else if (path) {
1784
0
        if (port) {
1785
0
            error_setg(errp, "port may not be used without host");
1786
0
            return false;
1787
0
        }
1788
1789
0
        qdict_put_str(output_options, "server.type", "unix");
1790
0
        qdict_put_str(output_options, "server.path", path);
1791
0
    } else if (host) {
1792
0
        qdict_put_str(output_options, "server.type", "inet");
1793
0
        qdict_put_str(output_options, "server.host", host);
1794
0
        qdict_put_str(output_options, "server.port",
1795
0
                      port ?: stringify(NBD_DEFAULT_PORT));
1796
0
    }
1797
1798
0
    return true;
1799
0
}
1800
1801
static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options,
1802
                                 Error **errp)
1803
0
{
1804
0
    SocketAddress *saddr = NULL;
1805
0
    QDict *addr = NULL;
1806
0
    Visitor *iv = NULL;
1807
1808
0
    qdict_extract_subqdict(options, &addr, "server.");
1809
0
    if (!qdict_size(addr)) {
1810
0
        error_setg(errp, "NBD server address missing");
1811
0
        goto done;
1812
0
    }
1813
1814
0
    iv = qobject_input_visitor_new_flat_confused(addr, errp);
1815
0
    if (!iv) {
1816
0
        goto done;
1817
0
    }
1818
1819
0
    if (!visit_type_SocketAddress(iv, NULL, &saddr, errp)) {
1820
0
        goto done;
1821
0
    }
1822
1823
0
    if (socket_address_parse_named_fd(saddr, errp) < 0) {
1824
0
        qapi_free_SocketAddress(saddr);
1825
0
        saddr = NULL;
1826
0
        goto done;
1827
0
    }
1828
1829
0
done:
1830
0
    qobject_unref(addr);
1831
0
    visit_free(iv);
1832
0
    return saddr;
1833
0
}
1834
1835
static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
1836
0
{
1837
0
    Object *obj;
1838
0
    QCryptoTLSCreds *creds;
1839
1840
0
    obj = object_resolve_path_component(
1841
0
        object_get_objects_root(), id);
1842
0
    if (!obj) {
1843
0
        error_setg(errp, "No TLS credentials with id '%s'",
1844
0
                   id);
1845
0
        return NULL;
1846
0
    }
1847
0
    creds = (QCryptoTLSCreds *)
1848
0
        object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
1849
0
    if (!creds) {
1850
0
        error_setg(errp, "Object with id '%s' is not TLS credentials",
1851
0
                   id);
1852
0
        return NULL;
1853
0
    }
1854
1855
0
    if (!qcrypto_tls_creds_check_endpoint(creds,
1856
0
                                          QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT,
1857
0
                                          errp)) {
1858
0
        return NULL;
1859
0
    }
1860
0
    object_ref(obj);
1861
0
    return creds;
1862
0
}
1863
1864
1865
static QemuOptsList nbd_runtime_opts = {
1866
    .name = "nbd",
1867
    .head = QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts.head),
1868
    .desc = {
1869
        {
1870
            .name = "host",
1871
            .type = QEMU_OPT_STRING,
1872
            .help = "TCP host to connect to",
1873
        },
1874
        {
1875
            .name = "port",
1876
            .type = QEMU_OPT_STRING,
1877
            .help = "TCP port to connect to",
1878
        },
1879
        {
1880
            .name = "path",
1881
            .type = QEMU_OPT_STRING,
1882
            .help = "Unix socket path to connect to",
1883
        },
1884
        {
1885
            .name = "export",
1886
            .type = QEMU_OPT_STRING,
1887
            .help = "Name of the NBD export to open",
1888
        },
1889
        {
1890
            .name = "tls-creds",
1891
            .type = QEMU_OPT_STRING,
1892
            .help = "ID of the TLS credentials to use",
1893
        },
1894
        {
1895
            .name = "tls-hostname",
1896
            .type = QEMU_OPT_STRING,
1897
            .help = "Override hostname for validating TLS x509 certificate",
1898
        },
1899
        {
1900
            .name = "x-dirty-bitmap",
1901
            .type = QEMU_OPT_STRING,
1902
            .help = "experimental: expose named dirty bitmap in place of "
1903
                    "block status",
1904
        },
1905
        {
1906
            .name = "reconnect-delay",
1907
            .type = QEMU_OPT_NUMBER,
1908
            .help = "On an unexpected disconnect, the nbd client tries to "
1909
                    "connect again until succeeding or encountering a serious "
1910
                    "error.  During the first @reconnect-delay seconds, all "
1911
                    "requests are paused and will be rerun on a successful "
1912
                    "reconnect. After that time, any delayed requests and all "
1913
                    "future requests before a successful reconnect will "
1914
                    "immediately fail. Default 0",
1915
        },
1916
        {
1917
            .name = "open-timeout",
1918
            .type = QEMU_OPT_NUMBER,
1919
            .help = "In seconds. If zero, the nbd driver tries the connection "
1920
                    "only once, and fails to open if the connection fails. "
1921
                    "If non-zero, the nbd driver will repeat connection "
1922
                    "attempts until successful or until @open-timeout seconds "
1923
                    "have elapsed. Default 0",
1924
        },
1925
        { /* end of list */ }
1926
    },
1927
};
1928
1929
static int nbd_process_options(BlockDriverState *bs, QDict *options,
1930
                               Error **errp)
1931
0
{
1932
0
    BDRVNBDState *s = bs->opaque;
1933
0
    QemuOpts *opts;
1934
0
    int ret = -EINVAL;
1935
1936
0
    opts = qemu_opts_create(&nbd_runtime_opts, NULL, 0, &error_abort);
1937
0
    if (!qemu_opts_absorb_qdict(opts, options, errp)) {
1938
0
        goto error;
1939
0
    }
1940
1941
    /* Translate @host, @port, and @path to a SocketAddress */
1942
0
    if (!nbd_process_legacy_socket_options(options, opts, errp)) {
1943
0
        goto error;
1944
0
    }
1945
1946
    /* Pop the config into our state object. Exit if invalid. */
1947
0
    s->saddr = nbd_config(s, options, errp);
1948
0
    if (!s->saddr) {
1949
0
        goto error;
1950
0
    }
1951
1952
0
    s->export = g_strdup(qemu_opt_get(opts, "export"));
1953
0
    if (s->export && strlen(s->export) > NBD_MAX_STRING_SIZE) {
1954
0
        error_setg(errp, "export name too long to send to server");
1955
0
        goto error;
1956
0
    }
1957
1958
0
    s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds"));
1959
0
    if (s->tlscredsid) {
1960
0
        s->tlscreds = nbd_get_tls_creds(s->tlscredsid, errp);
1961
0
        if (!s->tlscreds) {
1962
0
            goto error;
1963
0
        }
1964
1965
0
        s->tlshostname = g_strdup(qemu_opt_get(opts, "tls-hostname"));
1966
0
        if (!s->tlshostname &&
1967
0
            s->saddr->type == SOCKET_ADDRESS_TYPE_INET) {
1968
0
            s->tlshostname = g_strdup(s->saddr->u.inet.host);
1969
0
        }
1970
0
    }
1971
1972
0
    s->x_dirty_bitmap = g_strdup(qemu_opt_get(opts, "x-dirty-bitmap"));
1973
0
    if (s->x_dirty_bitmap && strlen(s->x_dirty_bitmap) > NBD_MAX_STRING_SIZE) {
1974
0
        error_setg(errp, "x-dirty-bitmap query too long to send to server");
1975
0
        goto error;
1976
0
    }
1977
1978
0
    s->reconnect_delay = qemu_opt_get_number(opts, "reconnect-delay", 0);
1979
0
    s->open_timeout = qemu_opt_get_number(opts, "open-timeout", 0);
1980
1981
0
    ret = 0;
1982
1983
0
 error:
1984
0
    qemu_opts_del(opts);
1985
0
    return ret;
1986
0
}
1987
1988
static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
1989
                    Error **errp)
1990
0
{
1991
0
    int ret;
1992
0
    BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1993
1994
0
    s->bs = bs;
1995
0
    qemu_mutex_init(&s->requests_lock);
1996
0
    qemu_co_queue_init(&s->free_sema);
1997
0
    qemu_co_mutex_init(&s->send_mutex);
1998
0
    qemu_co_mutex_init(&s->receive_mutex);
1999
2000
0
    if (!yank_register_instance(BLOCKDEV_YANK_INSTANCE(bs->node_name), errp)) {
2001
0
        return -EEXIST;
2002
0
    }
2003
2004
0
    ret = nbd_process_options(bs, options, errp);
2005
0
    if (ret < 0) {
2006
0
        goto fail;
2007
0
    }
2008
2009
0
    s->conn = nbd_client_connection_new(s->saddr, true, s->export,
2010
0
                                        s->x_dirty_bitmap, s->tlscreds,
2011
0
                                        s->tlshostname);
2012
2013
0
    if (s->open_timeout) {
2014
0
        nbd_client_connection_enable_retry(s->conn);
2015
0
        open_timer_init(s, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) +
2016
0
                        s->open_timeout * NANOSECONDS_PER_SECOND);
2017
0
    }
2018
2019
0
    s->state = NBD_CLIENT_CONNECTING_WAIT;
2020
0
    ret = nbd_do_establish_connection(bs, true, errp);
2021
0
    if (ret < 0) {
2022
0
        goto fail;
2023
0
    }
2024
2025
    /*
2026
     * The connect attempt is done, so we no longer need this timer.
2027
     * Delete it, because we do not want it to be around when this node
2028
     * is drained or closed.
2029
     */
2030
0
    open_timer_del(s);
2031
2032
0
    nbd_client_connection_enable_retry(s->conn);
2033
2034
0
    return 0;
2035
2036
0
fail:
2037
0
    open_timer_del(s);
2038
0
    nbd_clear_bdrvstate(bs);
2039
0
    return ret;
2040
0
}
2041
2042
static void nbd_refresh_limits(BlockDriverState *bs, Error **errp)
2043
0
{
2044
0
    BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
2045
0
    uint32_t min = s->info.min_block;
2046
0
    uint32_t max = MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE, s->info.max_block);
2047
2048
    /*
2049
     * If the server did not advertise an alignment:
2050
     * - a size that is not sector-aligned implies that an alignment
2051
     *   of 1 can be used to access those tail bytes
2052
     * - advertisement of block status requires an alignment of 1, so
2053
     *   that we don't violate block layer constraints that block
2054
     *   status is always aligned (as we can't control whether the
2055
     *   server will report sub-sector extents, such as a hole at EOF
2056
     *   on an unaligned POSIX file)
2057
     * - otherwise, assume the server is so old that we are safer avoiding
2058
     *   sub-sector requests
2059
     */
2060
0
    if (!min) {
2061
0
        min = (!QEMU_IS_ALIGNED(s->info.size, BDRV_SECTOR_SIZE) ||
2062
0
               s->info.base_allocation) ? 1 : BDRV_SECTOR_SIZE;
2063
0
    }
2064
2065
0
    bs->bl.request_alignment = min;
2066
0
    bs->bl.max_pdiscard = QEMU_ALIGN_DOWN(INT_MAX, min);
2067
0
    bs->bl.max_pwrite_zeroes = max;
2068
0
    bs->bl.max_transfer = max;
2069
2070
    /*
2071
     * Assume that if the server supports extended headers, it also
2072
     * supports unlimited size zero and trim commands.
2073
     */
2074
0
    if (s->info.mode >= NBD_MODE_EXTENDED) {
2075
0
        bs->bl.max_pdiscard = bs->bl.max_pwrite_zeroes = 0;
2076
0
    }
2077
2078
0
    if (s->info.opt_block &&
2079
0
        s->info.opt_block > bs->bl.opt_transfer) {
2080
0
        bs->bl.opt_transfer = s->info.opt_block;
2081
0
    }
2082
0
}
2083
2084
static void nbd_close(BlockDriverState *bs)
2085
0
{
2086
0
    nbd_client_close(bs);
2087
0
    nbd_clear_bdrvstate(bs);
2088
0
}
2089
2090
/*
2091
 * NBD cannot truncate, but if the caller asks to truncate to the same size, or
2092
 * to a smaller size with exact=false, there is no reason to fail the
2093
 * operation.
2094
 *
2095
 * Preallocation mode is ignored since it does not seems useful to fail when
2096
 * we never change anything.
2097
 */
2098
static int coroutine_fn nbd_co_truncate(BlockDriverState *bs, int64_t offset,
2099
                                        bool exact, PreallocMode prealloc,
2100
                                        BdrvRequestFlags flags, Error **errp)
2101
0
{
2102
0
    BDRVNBDState *s = bs->opaque;
2103
2104
0
    if (offset != s->info.size && exact) {
2105
0
        error_setg(errp, "Cannot resize NBD nodes");
2106
0
        return -ENOTSUP;
2107
0
    }
2108
2109
0
    if (offset > s->info.size) {
2110
0
        error_setg(errp, "Cannot grow NBD nodes");
2111
0
        return -EINVAL;
2112
0
    }
2113
2114
0
    return 0;
2115
0
}
2116
2117
static int64_t coroutine_fn nbd_co_getlength(BlockDriverState *bs)
2118
0
{
2119
0
    BDRVNBDState *s = bs->opaque;
2120
2121
0
    return s->info.size;
2122
0
}
2123
2124
static void nbd_refresh_filename(BlockDriverState *bs)
2125
0
{
2126
0
    BDRVNBDState *s = bs->opaque;
2127
0
    const char *host = NULL, *port = NULL, *path = NULL;
2128
0
    size_t len = 0;
2129
2130
0
    if (s->saddr->type == SOCKET_ADDRESS_TYPE_INET) {
2131
0
        const InetSocketAddress *inet = &s->saddr->u.inet;
2132
0
        if (!inet->has_ipv4 && !inet->has_ipv6 && !inet->has_to) {
2133
0
            host = inet->host;
2134
0
            port = inet->port;
2135
0
        }
2136
0
    } else if (s->saddr->type == SOCKET_ADDRESS_TYPE_UNIX) {
2137
0
        path = s->saddr->u.q_unix.path;
2138
0
    } /* else can't represent as pseudo-filename */
2139
2140
0
    if (path && s->export) {
2141
0
        len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2142
0
                       "nbd+unix:///%s?socket=%s", s->export, path);
2143
0
    } else if (path && !s->export) {
2144
0
        len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2145
0
                       "nbd+unix://?socket=%s", path);
2146
0
    } else if (host && s->export) {
2147
0
        len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2148
0
                       "nbd://%s:%s/%s", host, port, s->export);
2149
0
    } else if (host && !s->export) {
2150
0
        len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2151
0
                       "nbd://%s:%s", host, port);
2152
0
    }
2153
0
    if (len >= sizeof(bs->exact_filename)) {
2154
        /* Name is too long to represent exactly, so leave it empty. */
2155
0
        bs->exact_filename[0] = '\0';
2156
0
    }
2157
0
}
2158
2159
static char *nbd_dirname(BlockDriverState *bs, Error **errp)
2160
0
{
2161
    /* The generic bdrv_dirname() implementation is able to work out some
2162
     * directory name for NBD nodes, but that would be wrong. So far there is no
2163
     * specification for how "export paths" would work, so NBD does not have
2164
     * directory names. */
2165
0
    error_setg(errp, "Cannot generate a base directory for NBD nodes");
2166
0
    return NULL;
2167
0
}
2168
2169
static const char *const nbd_strong_runtime_opts[] = {
2170
    "path",
2171
    "host",
2172
    "port",
2173
    "export",
2174
    "tls-creds",
2175
    "tls-hostname",
2176
    "server.",
2177
2178
    NULL
2179
};
2180
2181
static void nbd_cancel_in_flight(BlockDriverState *bs)
2182
0
{
2183
0
    BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
2184
2185
0
    reconnect_delay_timer_del(s);
2186
2187
0
    qemu_mutex_lock(&s->requests_lock);
2188
0
    if (s->state == NBD_CLIENT_CONNECTING_WAIT) {
2189
0
        s->state = NBD_CLIENT_CONNECTING_NOWAIT;
2190
0
    }
2191
0
    qemu_mutex_unlock(&s->requests_lock);
2192
2193
0
    nbd_co_establish_connection_cancel(s->conn);
2194
0
}
2195
2196
static void nbd_attach_aio_context(BlockDriverState *bs,
2197
                                   AioContext *new_context)
2198
0
{
2199
0
    BDRVNBDState *s = bs->opaque;
2200
2201
    /* The open_timer is used only during nbd_open() */
2202
0
    assert(!s->open_timer);
2203
2204
    /*
2205
     * The reconnect_delay_timer is scheduled in I/O paths when the
2206
     * connection is lost, to cancel the reconnection attempt after a
2207
     * given time.  Once this attempt is done (successfully or not),
2208
     * nbd_reconnect_attempt() ensures the timer is deleted before the
2209
     * respective I/O request is resumed.
2210
     * Since the AioContext can only be changed when a node is drained,
2211
     * the reconnect_delay_timer cannot be active here.
2212
     */
2213
0
    assert(!s->reconnect_delay_timer);
2214
0
}
2215
2216
static void nbd_detach_aio_context(BlockDriverState *bs)
2217
0
{
2218
0
    BDRVNBDState *s = bs->opaque;
2219
2220
0
    assert(!s->open_timer);
2221
0
    assert(!s->reconnect_delay_timer);
2222
0
}
2223
2224
static BlockDriver bdrv_nbd = {
2225
    .format_name                = "nbd",
2226
    .protocol_name              = "nbd",
2227
    .instance_size              = sizeof(BDRVNBDState),
2228
    .bdrv_parse_filename        = nbd_parse_filename,
2229
    .bdrv_co_create_opts        = bdrv_co_create_opts_simple,
2230
    .create_opts                = &bdrv_create_opts_simple,
2231
    .bdrv_open                  = nbd_open,
2232
    .bdrv_reopen_prepare        = nbd_client_reopen_prepare,
2233
    .bdrv_co_preadv             = nbd_client_co_preadv,
2234
    .bdrv_co_pwritev            = nbd_client_co_pwritev,
2235
    .bdrv_co_pwrite_zeroes      = nbd_client_co_pwrite_zeroes,
2236
    .bdrv_close                 = nbd_close,
2237
    .bdrv_co_flush_to_os        = nbd_client_co_flush,
2238
    .bdrv_co_pdiscard           = nbd_client_co_pdiscard,
2239
    .bdrv_refresh_limits        = nbd_refresh_limits,
2240
    .bdrv_co_truncate           = nbd_co_truncate,
2241
    .bdrv_co_getlength          = nbd_co_getlength,
2242
    .bdrv_refresh_filename      = nbd_refresh_filename,
2243
    .bdrv_co_block_status       = nbd_client_co_block_status,
2244
    .bdrv_dirname               = nbd_dirname,
2245
    .strong_runtime_opts        = nbd_strong_runtime_opts,
2246
    .bdrv_cancel_in_flight      = nbd_cancel_in_flight,
2247
2248
    .bdrv_attach_aio_context    = nbd_attach_aio_context,
2249
    .bdrv_detach_aio_context    = nbd_detach_aio_context,
2250
};
2251
2252
static BlockDriver bdrv_nbd_tcp = {
2253
    .format_name                = "nbd",
2254
    .protocol_name              = "nbd+tcp",
2255
    .instance_size              = sizeof(BDRVNBDState),
2256
    .bdrv_parse_filename        = nbd_parse_filename,
2257
    .bdrv_co_create_opts        = bdrv_co_create_opts_simple,
2258
    .create_opts                = &bdrv_create_opts_simple,
2259
    .bdrv_open                  = nbd_open,
2260
    .bdrv_reopen_prepare        = nbd_client_reopen_prepare,
2261
    .bdrv_co_preadv             = nbd_client_co_preadv,
2262
    .bdrv_co_pwritev            = nbd_client_co_pwritev,
2263
    .bdrv_co_pwrite_zeroes      = nbd_client_co_pwrite_zeroes,
2264
    .bdrv_close                 = nbd_close,
2265
    .bdrv_co_flush_to_os        = nbd_client_co_flush,
2266
    .bdrv_co_pdiscard           = nbd_client_co_pdiscard,
2267
    .bdrv_refresh_limits        = nbd_refresh_limits,
2268
    .bdrv_co_truncate           = nbd_co_truncate,
2269
    .bdrv_co_getlength          = nbd_co_getlength,
2270
    .bdrv_refresh_filename      = nbd_refresh_filename,
2271
    .bdrv_co_block_status       = nbd_client_co_block_status,
2272
    .bdrv_dirname               = nbd_dirname,
2273
    .strong_runtime_opts        = nbd_strong_runtime_opts,
2274
    .bdrv_cancel_in_flight      = nbd_cancel_in_flight,
2275
2276
    .bdrv_attach_aio_context    = nbd_attach_aio_context,
2277
    .bdrv_detach_aio_context    = nbd_detach_aio_context,
2278
};
2279
2280
static BlockDriver bdrv_nbd_unix = {
2281
    .format_name                = "nbd",
2282
    .protocol_name              = "nbd+unix",
2283
    .instance_size              = sizeof(BDRVNBDState),
2284
    .bdrv_parse_filename        = nbd_parse_filename,
2285
    .bdrv_co_create_opts        = bdrv_co_create_opts_simple,
2286
    .create_opts                = &bdrv_create_opts_simple,
2287
    .bdrv_open                  = nbd_open,
2288
    .bdrv_reopen_prepare        = nbd_client_reopen_prepare,
2289
    .bdrv_co_preadv             = nbd_client_co_preadv,
2290
    .bdrv_co_pwritev            = nbd_client_co_pwritev,
2291
    .bdrv_co_pwrite_zeroes      = nbd_client_co_pwrite_zeroes,
2292
    .bdrv_close                 = nbd_close,
2293
    .bdrv_co_flush_to_os        = nbd_client_co_flush,
2294
    .bdrv_co_pdiscard           = nbd_client_co_pdiscard,
2295
    .bdrv_refresh_limits        = nbd_refresh_limits,
2296
    .bdrv_co_truncate           = nbd_co_truncate,
2297
    .bdrv_co_getlength          = nbd_co_getlength,
2298
    .bdrv_refresh_filename      = nbd_refresh_filename,
2299
    .bdrv_co_block_status       = nbd_client_co_block_status,
2300
    .bdrv_dirname               = nbd_dirname,
2301
    .strong_runtime_opts        = nbd_strong_runtime_opts,
2302
    .bdrv_cancel_in_flight      = nbd_cancel_in_flight,
2303
2304
    .bdrv_attach_aio_context    = nbd_attach_aio_context,
2305
    .bdrv_detach_aio_context    = nbd_detach_aio_context,
2306
};
2307
2308
static void bdrv_nbd_init(void)
2309
63
{
2310
63
    bdrv_register(&bdrv_nbd);
2311
63
    bdrv_register(&bdrv_nbd_tcp);
2312
63
    bdrv_register(&bdrv_nbd_unix);
2313
63
}
2314
2315
block_init(bdrv_nbd_init);