nbd.c 18.7 KB
Newer Older
1 2 3 4
/*
 * QEMU Block driver for  NBD
 *
 * Copyright (C) 2008 Bull S.A.S.
M
malc 已提交
5
 *     Author: Laurent Vivier <Laurent.Vivier@bull.net>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * Some parts:
 *    Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "qemu-common.h"
30
#include "block/nbd.h"
31
#include "qemu/uri.h"
32
#include "block/block_int.h"
33 34
#include "qemu/module.h"
#include "qemu/sockets.h"
35 36
#include "qapi/qmp/qjson.h"
#include "qapi/qmp/qint.h"
37 38 39 40

#include <sys/types.h>
#include <unistd.h>

41 42
#define EN_OPTSTR ":exportname="

43 44 45 46 47 48 49 50 51
/* #define DEBUG_NBD */

#if defined(DEBUG_NBD)
#define logout(fmt, ...) \
                fprintf(stderr, "nbd\t%-24s" fmt, __func__, ##__VA_ARGS__)
#else
#define logout(fmt, ...) ((void)0)
#endif

52 53 54 55
#define MAX_NBD_REQUESTS	16
#define HANDLE_TO_INDEX(bs, handle) ((handle) ^ ((uint64_t)(intptr_t)bs))
#define INDEX_TO_HANDLE(bs, index)  ((index)  ^ ((uint64_t)(intptr_t)bs))

56 57
typedef struct BDRVNBDState {
    int sock;
P
Paolo Bonzini 已提交
58
    uint32_t nbdflags;
59 60
    off_t size;
    size_t blocksize;
61

62 63 64 65
    CoMutex send_mutex;
    CoMutex free_sema;
    Coroutine *send_coroutine;
    int in_flight;
66

67
    Coroutine *recv_coroutine[MAX_NBD_REQUESTS];
68 69
    struct nbd_reply reply;

70 71
    bool is_unix;
    QemuOpts *socket_opts;
72

73
    char *export_name; /* An NBD server may export several devices */
74 75
} BDRVNBDState;

76
static int nbd_parse_uri(const char *filename, QDict *options)
P
Paolo Bonzini 已提交
77 78 79 80 81
{
    URI *uri;
    const char *p;
    QueryParams *qp = NULL;
    int ret = 0;
82
    bool is_unix;
P
Paolo Bonzini 已提交
83 84 85 86 87 88 89 90

    uri = uri_parse(filename);
    if (!uri) {
        return -EINVAL;
    }

    /* transport */
    if (!strcmp(uri->scheme, "nbd")) {
91
        is_unix = false;
P
Paolo Bonzini 已提交
92
    } else if (!strcmp(uri->scheme, "nbd+tcp")) {
93
        is_unix = false;
P
Paolo Bonzini 已提交
94
    } else if (!strcmp(uri->scheme, "nbd+unix")) {
95
        is_unix = true;
P
Paolo Bonzini 已提交
96 97 98 99 100 101 102 103
    } else {
        ret = -EINVAL;
        goto out;
    }

    p = uri->path ? uri->path : "/";
    p += strspn(p, "/");
    if (p[0]) {
104
        qdict_put(options, "export", qstring_from_str(p));
P
Paolo Bonzini 已提交
105 106 107
    }

    qp = query_params_parse(uri->query);
108
    if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
P
Paolo Bonzini 已提交
109 110 111 112
        ret = -EINVAL;
        goto out;
    }

113
    if (is_unix) {
P
Paolo Bonzini 已提交
114 115 116 117 118
        /* nbd+unix:///export?socket=path */
        if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
            ret = -EINVAL;
            goto out;
        }
119
        qdict_put(options, "path", qstring_from_str(qp->p[0].value));
P
Paolo Bonzini 已提交
120
    } else {
121
        /* nbd[+tcp]://host[:port]/export */
P
Paolo Bonzini 已提交
122 123 124 125
        if (!uri->server) {
            ret = -EINVAL;
            goto out;
        }
126

127
        qdict_put(options, "host", qstring_from_str(uri->server));
128 129 130 131 132
        if (uri->port) {
            char* port_str = g_strdup_printf("%d", uri->port);
            qdict_put(options, "port", qstring_from_str(port_str));
            g_free(port_str);
        }
P
Paolo Bonzini 已提交
133 134 135 136 137 138 139 140 141 142
    }

out:
    if (qp) {
        query_params_free(qp);
    }
    uri_free(uri);
    return ret;
}

143 144
static void nbd_parse_filename(const char *filename, QDict *options,
                               Error **errp)
145
{
146
    char *file;
147 148
    char *export_name;
    const char *host_spec;
149 150
    const char *unixpath;

151 152 153 154 155 156 157 158 159
    if (qdict_haskey(options, "host")
        || qdict_haskey(options, "port")
        || qdict_haskey(options, "path"))
    {
        error_setg(errp, "host/port/path and a file name may not be specified "
                         "at the same time");
        return;
    }

P
Paolo Bonzini 已提交
160
    if (strstr(filename, "://")) {
161 162 163 164 165
        int ret = nbd_parse_uri(filename, options);
        if (ret < 0) {
            error_setg(errp, "No valid URL specified");
        }
        return;
P
Paolo Bonzini 已提交
166 167
    }

168
    file = g_strdup(filename);
169

170 171 172
    export_name = strstr(file, EN_OPTSTR);
    if (export_name) {
        if (export_name[strlen(EN_OPTSTR)] == 0) {
173 174
            goto out;
        }
175 176
        export_name[0] = 0; /* truncate 'file' */
        export_name += strlen(EN_OPTSTR);
177 178

        qdict_put(options, "export", qstring_from_str(export_name));
179 180
    }

181 182
    /* extract the host_spec - fail if it's not nbd:... */
    if (!strstart(file, "nbd:", &host_spec)) {
183
        error_setg(errp, "File name string for NBD must start with 'nbd:'");
184 185
        goto out;
    }
186

187 188 189 190
    if (!*host_spec) {
        goto out;
    }

191 192
    /* are we a UNIX or TCP socket? */
    if (strstart(host_spec, "unix:", &unixpath)) {
193
        qdict_put(options, "path", qstring_from_str(unixpath));
194
    } else {
195 196
        InetSocketAddress *addr = NULL;

197 198
        addr = inet_parse(host_spec, errp);
        if (error_is_set(errp)) {
199 200
            goto out;
        }
201

202 203 204 205
        qdict_put(options, "host", qstring_from_str(addr->host));
        qdict_put(options, "port", qstring_from_str(addr->port));
        qapi_free_InetSocketAddress(addr);
    }
206

207
out:
208
    g_free(file);
209 210 211 212 213 214 215
}

static int nbd_config(BDRVNBDState *s, QDict *options)
{
    Error *local_err = NULL;

    if (qdict_haskey(options, "path")) {
216 217 218 219 220
        if (qdict_haskey(options, "host")) {
            qerror_report(ERROR_CLASS_GENERIC_ERROR, "path and host may not "
                          "be used at the same time.");
            return -EINVAL;
        }
221 222 223 224 225
        s->is_unix = true;
    } else if (qdict_haskey(options, "host")) {
        s->is_unix = false;
    } else {
        return -EINVAL;
226
    }
227 228 229 230 231 232 233 234 235 236

    s->socket_opts = qemu_opts_create_nofail(&socket_optslist);

    qemu_opts_absorb_qdict(s->socket_opts, options, &local_err);
    if (error_is_set(&local_err)) {
        qerror_report_err(local_err);
        error_free(local_err);
        return -EINVAL;
    }

237 238 239 240
    if (!qemu_opt_get(s->socket_opts, "port")) {
        qemu_opt_set_number(s->socket_opts, "port", NBD_DEFAULT_PORT);
    }

241 242 243 244 245 246
    s->export_name = g_strdup(qdict_get_try_str(options, "export"));
    if (s->export_name) {
        qdict_del(options, "export");
    }

    return 0;
247
}
248

249

250 251
static void nbd_coroutine_start(BDRVNBDState *s, struct nbd_request *request)
{
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    int i;

    /* Poor man semaphore.  The free_sema is locked when no other request
     * can be accepted, and unlocked after receiving one reply.  */
    if (s->in_flight >= MAX_NBD_REQUESTS - 1) {
        qemu_co_mutex_lock(&s->free_sema);
        assert(s->in_flight < MAX_NBD_REQUESTS);
    }
    s->in_flight++;

    for (i = 0; i < MAX_NBD_REQUESTS; i++) {
        if (s->recv_coroutine[i] == NULL) {
            s->recv_coroutine[i] = qemu_coroutine_self();
            break;
        }
    }

    assert(i < MAX_NBD_REQUESTS);
    request->handle = INDEX_TO_HANDLE(s, i);
271 272 273 274 275 276
}

static int nbd_have_request(void *opaque)
{
    BDRVNBDState *s = opaque;

277
    return s->in_flight > 0;
278 279 280 281 282
}

static void nbd_reply_ready(void *opaque)
{
    BDRVNBDState *s = opaque;
283
    uint64_t i;
284
    int ret;
285 286

    if (s->reply.handle == 0) {
287 288 289 290 291 292 293 294 295
        /* No reply already in flight.  Fetch a header.  It is possible
         * that another thread has done the same thing in parallel, so
         * the socket is not readable anymore.
         */
        ret = nbd_receive_reply(s->sock, &s->reply);
        if (ret == -EAGAIN) {
            return;
        }
        if (ret < 0) {
296
            s->reply.handle = 0;
297
            goto fail;
298 299 300 301 302 303
        }
    }

    /* There's no need for a mutex on the receive side, because the
     * handler acts as a synchronization point and ensures that only
     * one coroutine is called until the reply finishes.  */
304
    i = HANDLE_TO_INDEX(s, s->reply.handle);
305 306 307 308
    if (i >= MAX_NBD_REQUESTS) {
        goto fail;
    }

309 310 311 312 313 314 315 316 317 318
    if (s->recv_coroutine[i]) {
        qemu_coroutine_enter(s->recv_coroutine[i], NULL);
        return;
    }

fail:
    for (i = 0; i < MAX_NBD_REQUESTS; i++) {
        if (s->recv_coroutine[i]) {
            qemu_coroutine_enter(s->recv_coroutine[i], NULL);
        }
319 320 321 322 323 324
    }
}

static void nbd_restart_write(void *opaque)
{
    BDRVNBDState *s = opaque;
325
    qemu_coroutine_enter(s->send_coroutine, NULL);
326 327 328
}

static int nbd_co_send_request(BDRVNBDState *s, struct nbd_request *request,
329
                               QEMUIOVector *qiov, int offset)
330 331 332
{
    int rc, ret;

333 334
    qemu_co_mutex_lock(&s->send_mutex);
    s->send_coroutine = qemu_coroutine_self();
335
    qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, nbd_restart_write,
336
                            nbd_have_request, s);
337 338 339
    if (qiov) {
        if (!s->is_unix) {
            socket_set_cork(s->sock, 1);
340
        }
341 342 343 344 345 346 347 348 349 350 351 352 353
        rc = nbd_send_request(s->sock, request);
        if (rc >= 0) {
            ret = qemu_co_sendv(s->sock, qiov->iov, qiov->niov,
                                offset, request->len);
            if (ret != request->len) {
                rc = -EIO;
            }
        }
        if (!s->is_unix) {
            socket_set_cork(s->sock, 0);
        }
    } else {
        rc = nbd_send_request(s->sock, request);
354 355
    }
    qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, NULL,
356
                            nbd_have_request, s);
357 358
    s->send_coroutine = NULL;
    qemu_co_mutex_unlock(&s->send_mutex);
359 360 361 362 363
    return rc;
}

static void nbd_co_receive_reply(BDRVNBDState *s, struct nbd_request *request,
                                 struct nbd_reply *reply,
364
                                 QEMUIOVector *qiov, int offset)
365 366 367
{
    int ret;

368 369
    /* Wait until we're woken up by the read handler.  TODO: perhaps
     * peek at the next reply and avoid yielding if it's ours?  */
370 371 372 373 374
    qemu_coroutine_yield();
    *reply = s->reply;
    if (reply->handle != request->handle) {
        reply->error = EIO;
    } else {
375 376 377
        if (qiov && reply->error == 0) {
            ret = qemu_co_recvv(s->sock, qiov->iov, qiov->niov,
                                offset, request->len);
378 379 380 381 382 383 384 385 386 387 388 389
            if (ret != request->len) {
                reply->error = EIO;
            }
        }

        /* Tell the read handler to read another header.  */
        s->reply.handle = 0;
    }
}

static void nbd_coroutine_end(BDRVNBDState *s, struct nbd_request *request)
{
390 391 392 393 394
    int i = HANDLE_TO_INDEX(s, request->handle);
    s->recv_coroutine[i] = NULL;
    if (s->in_flight-- == MAX_NBD_REQUESTS) {
        qemu_co_mutex_unlock(&s->free_sema);
    }
395 396
}

397 398 399 400 401 402 403
static int nbd_establish_connection(BlockDriverState *bs)
{
    BDRVNBDState *s = bs->opaque;
    int sock;
    int ret;
    off_t size;
    size_t blocksize;
404

405
    if (s->is_unix) {
406
        sock = unix_socket_outgoing(qemu_opt_get(s->socket_opts, "path"));
407
    } else {
408
        sock = tcp_socket_outgoing_opts(s->socket_opts);
S
Stefan Hajnoczi 已提交
409 410 411
        if (sock >= 0) {
            socket_set_nodelay(sock);
        }
412 413
    }

414
    /* Failed to establish connection */
415
    if (sock < 0) {
416 417
        logout("Failed to establish connection to NBD server\n");
        return -errno;
418
    }
419

420
    /* NBD handshake */
P
Paolo Bonzini 已提交
421
    ret = nbd_receive_negotiate(sock, s->export_name, &s->nbdflags, &size,
422
                                &blocksize);
423
    if (ret < 0) {
424 425
        logout("Failed to negotiate with the NBD server\n");
        closesocket(sock);
426
        return ret;
427
    }
428

429 430
    /* Now that we're connected, set the socket to be non-blocking and
     * kick the reply mechanism.  */
431
    qemu_set_nonblock(sock);
432
    qemu_aio_set_fd_handler(sock, nbd_reply_ready, NULL,
433
                            nbd_have_request, s);
434

435 436 437 438
    s->sock = sock;
    s->size = size;
    s->blocksize = blocksize;

439 440 441 442 443 444 445 446 447 448 449 450 451 452
    logout("Established connection with NBD server\n");
    return 0;
}

static void nbd_teardown_connection(BlockDriverState *bs)
{
    BDRVNBDState *s = bs->opaque;
    struct nbd_request request;

    request.type = NBD_CMD_DISC;
    request.from = 0;
    request.len = 0;
    nbd_send_request(s->sock, &request);

453
    qemu_aio_set_fd_handler(s->sock, NULL, NULL, NULL, NULL);
454 455 456
    closesocket(s->sock);
}

457
static int nbd_open(BlockDriverState *bs, QDict *options, int flags)
458 459 460 461
{
    BDRVNBDState *s = bs->opaque;
    int result;

462 463
    qemu_co_mutex_init(&s->send_mutex);
    qemu_co_mutex_init(&s->free_sema);
464

465
    /* Pop the config into our state object. Exit if invalid. */
466
    result = nbd_config(s, options);
467 468 469 470 471 472 473 474 475 476
    if (result != 0) {
        return result;
    }

    /* establish TCP connection, return error if it fails
     * TODO: Configurable retry-until-timeout behaviour.
     */
    result = nbd_establish_connection(bs);

    return result;
477 478
}

P
Paolo Bonzini 已提交
479 480 481
static int nbd_co_readv_1(BlockDriverState *bs, int64_t sector_num,
                          int nb_sectors, QEMUIOVector *qiov,
                          int offset)
482 483 484 485
{
    BDRVNBDState *s = bs->opaque;
    struct nbd_request request;
    struct nbd_reply reply;
486
    ssize_t ret;
487 488

    request.type = NBD_CMD_READ;
489
    request.from = sector_num * 512;
490 491
    request.len = nb_sectors * 512;

492
    nbd_coroutine_start(s, &request);
493 494
    ret = nbd_co_send_request(s, &request, NULL, 0);
    if (ret < 0) {
495
        reply.error = -ret;
496
    } else {
497
        nbd_co_receive_reply(s, &request, &reply, qiov, offset);
498 499 500
    }
    nbd_coroutine_end(s, &request);
    return -reply.error;
501 502 503

}

P
Paolo Bonzini 已提交
504 505 506
static int nbd_co_writev_1(BlockDriverState *bs, int64_t sector_num,
                           int nb_sectors, QEMUIOVector *qiov,
                           int offset)
507 508 509 510
{
    BDRVNBDState *s = bs->opaque;
    struct nbd_request request;
    struct nbd_reply reply;
511
    ssize_t ret;
512 513

    request.type = NBD_CMD_WRITE;
514 515 516 517
    if (!bdrv_enable_write_cache(bs) && (s->nbdflags & NBD_FLAG_SEND_FUA)) {
        request.type |= NBD_CMD_FLAG_FUA;
    }

518
    request.from = sector_num * 512;
519 520
    request.len = nb_sectors * 512;

521
    nbd_coroutine_start(s, &request);
522
    ret = nbd_co_send_request(s, &request, qiov, offset);
523
    if (ret < 0) {
524
        reply.error = -ret;
525 526 527 528 529
    } else {
        nbd_co_receive_reply(s, &request, &reply, NULL, 0);
    }
    nbd_coroutine_end(s, &request);
    return -reply.error;
530 531
}

P
Paolo Bonzini 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
/* qemu-nbd has a limit of slightly less than 1M per request.  Try to
 * remain aligned to 4K. */
#define NBD_MAX_SECTORS 2040

static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num,
                        int nb_sectors, QEMUIOVector *qiov)
{
    int offset = 0;
    int ret;
    while (nb_sectors > NBD_MAX_SECTORS) {
        ret = nbd_co_readv_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset);
        if (ret < 0) {
            return ret;
        }
        offset += NBD_MAX_SECTORS * 512;
        sector_num += NBD_MAX_SECTORS;
        nb_sectors -= NBD_MAX_SECTORS;
    }
    return nbd_co_readv_1(bs, sector_num, nb_sectors, qiov, offset);
}

static int nbd_co_writev(BlockDriverState *bs, int64_t sector_num,
                         int nb_sectors, QEMUIOVector *qiov)
{
    int offset = 0;
    int ret;
    while (nb_sectors > NBD_MAX_SECTORS) {
        ret = nbd_co_writev_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset);
        if (ret < 0) {
            return ret;
        }
        offset += NBD_MAX_SECTORS * 512;
        sector_num += NBD_MAX_SECTORS;
        nb_sectors -= NBD_MAX_SECTORS;
    }
    return nbd_co_writev_1(bs, sector_num, nb_sectors, qiov, offset);
}

P
Paolo Bonzini 已提交
570 571 572 573 574
static int nbd_co_flush(BlockDriverState *bs)
{
    BDRVNBDState *s = bs->opaque;
    struct nbd_request request;
    struct nbd_reply reply;
575
    ssize_t ret;
P
Paolo Bonzini 已提交
576 577 578 579 580 581 582 583 584 585 586 587 588 589

    if (!(s->nbdflags & NBD_FLAG_SEND_FLUSH)) {
        return 0;
    }

    request.type = NBD_CMD_FLUSH;
    if (s->nbdflags & NBD_FLAG_SEND_FUA) {
        request.type |= NBD_CMD_FLAG_FUA;
    }

    request.from = 0;
    request.len = 0;

    nbd_coroutine_start(s, &request);
590 591
    ret = nbd_co_send_request(s, &request, NULL, 0);
    if (ret < 0) {
592
        reply.error = -ret;
P
Paolo Bonzini 已提交
593 594 595 596 597 598 599
    } else {
        nbd_co_receive_reply(s, &request, &reply, NULL, 0);
    }
    nbd_coroutine_end(s, &request);
    return -reply.error;
}

P
Paolo Bonzini 已提交
600 601 602 603 604 605
static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num,
                          int nb_sectors)
{
    BDRVNBDState *s = bs->opaque;
    struct nbd_request request;
    struct nbd_reply reply;
606
    ssize_t ret;
P
Paolo Bonzini 已提交
607 608 609 610 611

    if (!(s->nbdflags & NBD_FLAG_SEND_TRIM)) {
        return 0;
    }
    request.type = NBD_CMD_TRIM;
D
Dong Xu Wang 已提交
612
    request.from = sector_num * 512;
P
Paolo Bonzini 已提交
613 614 615
    request.len = nb_sectors * 512;

    nbd_coroutine_start(s, &request);
616 617
    ret = nbd_co_send_request(s, &request, NULL, 0);
    if (ret < 0) {
618
        reply.error = -ret;
P
Paolo Bonzini 已提交
619 620 621 622 623 624 625
    } else {
        nbd_co_receive_reply(s, &request, &reply, NULL, 0);
    }
    nbd_coroutine_end(s, &request);
    return -reply.error;
}

626 627
static void nbd_close(BlockDriverState *bs)
{
628
    BDRVNBDState *s = bs->opaque;
629
    g_free(s->export_name);
630
    qemu_opts_del(s->socket_opts);
631

632
    nbd_teardown_connection(bs);
633 634 635 636 637 638 639 640 641
}

static int64_t nbd_getlength(BlockDriverState *bs)
{
    BDRVNBDState *s = bs->opaque;

    return s->size;
}

642
static BlockDriver bdrv_nbd = {
P
Paolo Bonzini 已提交
643
    .format_name         = "nbd",
P
Paolo Bonzini 已提交
644 645
    .protocol_name       = "nbd",
    .instance_size       = sizeof(BDRVNBDState),
646
    .bdrv_parse_filename = nbd_parse_filename,
P
Paolo Bonzini 已提交
647 648 649 650 651 652 653 654 655 656 657 658 659
    .bdrv_file_open      = nbd_open,
    .bdrv_co_readv       = nbd_co_readv,
    .bdrv_co_writev      = nbd_co_writev,
    .bdrv_close          = nbd_close,
    .bdrv_co_flush_to_os = nbd_co_flush,
    .bdrv_co_discard     = nbd_co_discard,
    .bdrv_getlength      = nbd_getlength,
};

static BlockDriver bdrv_nbd_tcp = {
    .format_name         = "nbd",
    .protocol_name       = "nbd+tcp",
    .instance_size       = sizeof(BDRVNBDState),
660
    .bdrv_parse_filename = nbd_parse_filename,
P
Paolo Bonzini 已提交
661 662 663 664 665 666 667 668 669 670 671 672
    .bdrv_file_open      = nbd_open,
    .bdrv_co_readv       = nbd_co_readv,
    .bdrv_co_writev      = nbd_co_writev,
    .bdrv_close          = nbd_close,
    .bdrv_co_flush_to_os = nbd_co_flush,
    .bdrv_co_discard     = nbd_co_discard,
    .bdrv_getlength      = nbd_getlength,
};

static BlockDriver bdrv_nbd_unix = {
    .format_name         = "nbd",
    .protocol_name       = "nbd+unix",
P
Paolo Bonzini 已提交
673
    .instance_size       = sizeof(BDRVNBDState),
674
    .bdrv_parse_filename = nbd_parse_filename,
P
Paolo Bonzini 已提交
675 676 677 678 679
    .bdrv_file_open      = nbd_open,
    .bdrv_co_readv       = nbd_co_readv,
    .bdrv_co_writev      = nbd_co_writev,
    .bdrv_close          = nbd_close,
    .bdrv_co_flush_to_os = nbd_co_flush,
P
Paolo Bonzini 已提交
680
    .bdrv_co_discard     = nbd_co_discard,
P
Paolo Bonzini 已提交
681
    .bdrv_getlength      = nbd_getlength,
682
};
683 684 685 686

static void bdrv_nbd_init(void)
{
    bdrv_register(&bdrv_nbd);
P
Paolo Bonzini 已提交
687 688
    bdrv_register(&bdrv_nbd_tcp);
    bdrv_register(&bdrv_nbd_unix);
689 690 691
}

block_init(bdrv_nbd_init);