1019d6b8fSAnthony Liguori /* 2019d6b8fSAnthony Liguori * QEMU Block driver for NBD 3019d6b8fSAnthony Liguori * 4019d6b8fSAnthony Liguori * Copyright (C) 2008 Bull S.A.S. 5019d6b8fSAnthony Liguori * Author: Laurent Vivier <Laurent.Vivier@bull.net> 6019d6b8fSAnthony Liguori * 7019d6b8fSAnthony Liguori * Some parts: 8019d6b8fSAnthony Liguori * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws> 9019d6b8fSAnthony Liguori * 10019d6b8fSAnthony Liguori * Permission is hereby granted, free of charge, to any person obtaining a copy 11019d6b8fSAnthony Liguori * of this software and associated documentation files (the "Software"), to deal 12019d6b8fSAnthony Liguori * in the Software without restriction, including without limitation the rights 13019d6b8fSAnthony Liguori * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14019d6b8fSAnthony Liguori * copies of the Software, and to permit persons to whom the Software is 15019d6b8fSAnthony Liguori * furnished to do so, subject to the following conditions: 16019d6b8fSAnthony Liguori * 17019d6b8fSAnthony Liguori * The above copyright notice and this permission notice shall be included in 18019d6b8fSAnthony Liguori * all copies or substantial portions of the Software. 19019d6b8fSAnthony Liguori * 20019d6b8fSAnthony Liguori * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21019d6b8fSAnthony Liguori * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22019d6b8fSAnthony Liguori * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23019d6b8fSAnthony Liguori * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24019d6b8fSAnthony Liguori * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25019d6b8fSAnthony Liguori * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26019d6b8fSAnthony Liguori * THE SOFTWARE. 27019d6b8fSAnthony Liguori */ 28019d6b8fSAnthony Liguori 29019d6b8fSAnthony Liguori #include "qemu-common.h" 30019d6b8fSAnthony Liguori #include "nbd.h" 31ab359cd1SMarkus Armbruster #include "block_int.h" 32019d6b8fSAnthony Liguori #include "module.h" 3333897dc7SNick Thomas #include "qemu_socket.h" 34019d6b8fSAnthony Liguori 35019d6b8fSAnthony Liguori #include <sys/types.h> 36019d6b8fSAnthony Liguori #include <unistd.h> 37019d6b8fSAnthony Liguori 381d45f8b5SLaurent Vivier #define EN_OPTSTR ":exportname=" 391d45f8b5SLaurent Vivier 4033897dc7SNick Thomas /* #define DEBUG_NBD */ 4133897dc7SNick Thomas 4233897dc7SNick Thomas #if defined(DEBUG_NBD) 4333897dc7SNick Thomas #define logout(fmt, ...) \ 4433897dc7SNick Thomas fprintf(stderr, "nbd\t%-24s" fmt, __func__, ##__VA_ARGS__) 4533897dc7SNick Thomas #else 4633897dc7SNick Thomas #define logout(fmt, ...) ((void)0) 4733897dc7SNick Thomas #endif 4833897dc7SNick Thomas 49ecda3447SPaolo Bonzini #define MAX_NBD_REQUESTS 16 50ecda3447SPaolo Bonzini #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ ((uint64_t)(intptr_t)bs)) 51ecda3447SPaolo Bonzini #define INDEX_TO_HANDLE(bs, index) ((index) ^ ((uint64_t)(intptr_t)bs)) 52ecda3447SPaolo Bonzini 53019d6b8fSAnthony Liguori typedef struct BDRVNBDState { 54019d6b8fSAnthony Liguori int sock; 55b90fb4b8SPaolo Bonzini uint32_t nbdflags; 56019d6b8fSAnthony Liguori off_t size; 57019d6b8fSAnthony Liguori size_t blocksize; 5833897dc7SNick Thomas char *export_name; /* An NBD server may export several devices */ 5933897dc7SNick Thomas 60ecda3447SPaolo Bonzini CoMutex send_mutex; 61ecda3447SPaolo Bonzini CoMutex free_sema; 62ecda3447SPaolo Bonzini Coroutine *send_coroutine; 63ecda3447SPaolo Bonzini int in_flight; 64ae255e52SPaolo Bonzini 65ecda3447SPaolo Bonzini Coroutine *recv_coroutine[MAX_NBD_REQUESTS]; 66ae255e52SPaolo Bonzini struct nbd_reply reply; 67ae255e52SPaolo Bonzini 6833897dc7SNick Thomas /* If it begins with '/', this is a UNIX domain socket. Otherwise, 6933897dc7SNick Thomas * it's a string of the form <hostname|ip4|\[ip6\]>:port 7033897dc7SNick Thomas */ 7133897dc7SNick Thomas char *host_spec; 72019d6b8fSAnthony Liguori } BDRVNBDState; 73019d6b8fSAnthony Liguori 7433897dc7SNick Thomas static int nbd_config(BDRVNBDState *s, const char *filename, int flags) 75019d6b8fSAnthony Liguori { 761d45f8b5SLaurent Vivier char *file; 7733897dc7SNick Thomas char *export_name; 7833897dc7SNick Thomas const char *host_spec; 79019d6b8fSAnthony Liguori const char *unixpath; 801d45f8b5SLaurent Vivier int err = -EINVAL; 81019d6b8fSAnthony Liguori 827267c094SAnthony Liguori file = g_strdup(filename); 831d45f8b5SLaurent Vivier 8433897dc7SNick Thomas export_name = strstr(file, EN_OPTSTR); 8533897dc7SNick Thomas if (export_name) { 8633897dc7SNick Thomas if (export_name[strlen(EN_OPTSTR)] == 0) { 871d45f8b5SLaurent Vivier goto out; 881d45f8b5SLaurent Vivier } 8933897dc7SNick Thomas export_name[0] = 0; /* truncate 'file' */ 9033897dc7SNick Thomas export_name += strlen(EN_OPTSTR); 917267c094SAnthony Liguori s->export_name = g_strdup(export_name); 921d45f8b5SLaurent Vivier } 931d45f8b5SLaurent Vivier 9433897dc7SNick Thomas /* extract the host_spec - fail if it's not nbd:... */ 9533897dc7SNick Thomas if (!strstart(file, "nbd:", &host_spec)) { 961d45f8b5SLaurent Vivier goto out; 971d45f8b5SLaurent Vivier } 98019d6b8fSAnthony Liguori 9933897dc7SNick Thomas /* are we a UNIX or TCP socket? */ 10033897dc7SNick Thomas if (strstart(host_spec, "unix:", &unixpath)) { 10133897dc7SNick Thomas if (unixpath[0] != '/') { /* We demand an absolute path*/ 1021d45f8b5SLaurent Vivier goto out; 1031d45f8b5SLaurent Vivier } 1047267c094SAnthony Liguori s->host_spec = g_strdup(unixpath); 105019d6b8fSAnthony Liguori } else { 1067267c094SAnthony Liguori s->host_spec = g_strdup(host_spec); 1071d45f8b5SLaurent Vivier } 1081d45f8b5SLaurent Vivier 1091d45f8b5SLaurent Vivier err = 0; 110019d6b8fSAnthony Liguori 1111d45f8b5SLaurent Vivier out: 1127267c094SAnthony Liguori g_free(file); 11333897dc7SNick Thomas if (err != 0) { 1147267c094SAnthony Liguori g_free(s->export_name); 1157267c094SAnthony Liguori g_free(s->host_spec); 11633897dc7SNick Thomas } 1171d45f8b5SLaurent Vivier return err; 118019d6b8fSAnthony Liguori } 119019d6b8fSAnthony Liguori 120ae255e52SPaolo Bonzini static void nbd_coroutine_start(BDRVNBDState *s, struct nbd_request *request) 121ae255e52SPaolo Bonzini { 122ecda3447SPaolo Bonzini int i; 123ecda3447SPaolo Bonzini 124ecda3447SPaolo Bonzini /* Poor man semaphore. The free_sema is locked when no other request 125ecda3447SPaolo Bonzini * can be accepted, and unlocked after receiving one reply. */ 126ecda3447SPaolo Bonzini if (s->in_flight >= MAX_NBD_REQUESTS - 1) { 127ecda3447SPaolo Bonzini qemu_co_mutex_lock(&s->free_sema); 128ecda3447SPaolo Bonzini assert(s->in_flight < MAX_NBD_REQUESTS); 129ecda3447SPaolo Bonzini } 130ecda3447SPaolo Bonzini s->in_flight++; 131ecda3447SPaolo Bonzini 132ecda3447SPaolo Bonzini for (i = 0; i < MAX_NBD_REQUESTS; i++) { 133ecda3447SPaolo Bonzini if (s->recv_coroutine[i] == NULL) { 134ecda3447SPaolo Bonzini s->recv_coroutine[i] = qemu_coroutine_self(); 135ecda3447SPaolo Bonzini break; 136ecda3447SPaolo Bonzini } 137ecda3447SPaolo Bonzini } 138ecda3447SPaolo Bonzini 139ecda3447SPaolo Bonzini assert(i < MAX_NBD_REQUESTS); 140ecda3447SPaolo Bonzini request->handle = INDEX_TO_HANDLE(s, i); 141ae255e52SPaolo Bonzini } 142ae255e52SPaolo Bonzini 143ae255e52SPaolo Bonzini static int nbd_have_request(void *opaque) 144ae255e52SPaolo Bonzini { 145ae255e52SPaolo Bonzini BDRVNBDState *s = opaque; 146ae255e52SPaolo Bonzini 147ecda3447SPaolo Bonzini return s->in_flight > 0; 148ae255e52SPaolo Bonzini } 149ae255e52SPaolo Bonzini 150ae255e52SPaolo Bonzini static void nbd_reply_ready(void *opaque) 151ae255e52SPaolo Bonzini { 152ae255e52SPaolo Bonzini BDRVNBDState *s = opaque; 153ecda3447SPaolo Bonzini int i; 154ae255e52SPaolo Bonzini 155ae255e52SPaolo Bonzini if (s->reply.handle == 0) { 156ae255e52SPaolo Bonzini /* No reply already in flight. Fetch a header. */ 157ae255e52SPaolo Bonzini if (nbd_receive_reply(s->sock, &s->reply) < 0) { 158ae255e52SPaolo Bonzini s->reply.handle = 0; 159ecda3447SPaolo Bonzini goto fail; 160ae255e52SPaolo Bonzini } 161ae255e52SPaolo Bonzini } 162ae255e52SPaolo Bonzini 163ae255e52SPaolo Bonzini /* There's no need for a mutex on the receive side, because the 164ae255e52SPaolo Bonzini * handler acts as a synchronization point and ensures that only 165ae255e52SPaolo Bonzini * one coroutine is called until the reply finishes. */ 166ecda3447SPaolo Bonzini i = HANDLE_TO_INDEX(s, s->reply.handle); 167ecda3447SPaolo Bonzini if (s->recv_coroutine[i]) { 168ecda3447SPaolo Bonzini qemu_coroutine_enter(s->recv_coroutine[i], NULL); 169ecda3447SPaolo Bonzini return; 170ecda3447SPaolo Bonzini } 171ecda3447SPaolo Bonzini 172ecda3447SPaolo Bonzini fail: 173ecda3447SPaolo Bonzini for (i = 0; i < MAX_NBD_REQUESTS; i++) { 174ecda3447SPaolo Bonzini if (s->recv_coroutine[i]) { 175ecda3447SPaolo Bonzini qemu_coroutine_enter(s->recv_coroutine[i], NULL); 176ecda3447SPaolo Bonzini } 177ae255e52SPaolo Bonzini } 178ae255e52SPaolo Bonzini } 179ae255e52SPaolo Bonzini 180ae255e52SPaolo Bonzini static void nbd_restart_write(void *opaque) 181ae255e52SPaolo Bonzini { 182ae255e52SPaolo Bonzini BDRVNBDState *s = opaque; 183ecda3447SPaolo Bonzini qemu_coroutine_enter(s->send_coroutine, NULL); 184ae255e52SPaolo Bonzini } 185ae255e52SPaolo Bonzini 186ae255e52SPaolo Bonzini static int nbd_co_send_request(BDRVNBDState *s, struct nbd_request *request, 187ae255e52SPaolo Bonzini struct iovec *iov, int offset) 188ae255e52SPaolo Bonzini { 189ae255e52SPaolo Bonzini int rc, ret; 190ae255e52SPaolo Bonzini 191ecda3447SPaolo Bonzini qemu_co_mutex_lock(&s->send_mutex); 192ecda3447SPaolo Bonzini s->send_coroutine = qemu_coroutine_self(); 193ae255e52SPaolo Bonzini qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, nbd_restart_write, 194ae255e52SPaolo Bonzini nbd_have_request, NULL, s); 195ae255e52SPaolo Bonzini rc = nbd_send_request(s->sock, request); 196ae255e52SPaolo Bonzini if (rc != -1 && iov) { 197ae255e52SPaolo Bonzini ret = qemu_co_sendv(s->sock, iov, request->len, offset); 198ae255e52SPaolo Bonzini if (ret != request->len) { 199ae255e52SPaolo Bonzini errno = -EIO; 200ae255e52SPaolo Bonzini rc = -1; 201ae255e52SPaolo Bonzini } 202ae255e52SPaolo Bonzini } 203ae255e52SPaolo Bonzini qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, NULL, 204ae255e52SPaolo Bonzini nbd_have_request, NULL, s); 205ecda3447SPaolo Bonzini s->send_coroutine = NULL; 206ecda3447SPaolo Bonzini qemu_co_mutex_unlock(&s->send_mutex); 207ae255e52SPaolo Bonzini return rc; 208ae255e52SPaolo Bonzini } 209ae255e52SPaolo Bonzini 210ae255e52SPaolo Bonzini static void nbd_co_receive_reply(BDRVNBDState *s, struct nbd_request *request, 211ae255e52SPaolo Bonzini struct nbd_reply *reply, 212ae255e52SPaolo Bonzini struct iovec *iov, int offset) 213ae255e52SPaolo Bonzini { 214ae255e52SPaolo Bonzini int ret; 215ae255e52SPaolo Bonzini 216ecda3447SPaolo Bonzini /* Wait until we're woken up by the read handler. TODO: perhaps 217ecda3447SPaolo Bonzini * peek at the next reply and avoid yielding if it's ours? */ 218ae255e52SPaolo Bonzini qemu_coroutine_yield(); 219ae255e52SPaolo Bonzini *reply = s->reply; 220ae255e52SPaolo Bonzini if (reply->handle != request->handle) { 221ae255e52SPaolo Bonzini reply->error = EIO; 222ae255e52SPaolo Bonzini } else { 223ae255e52SPaolo Bonzini if (iov && reply->error == 0) { 224ae255e52SPaolo Bonzini ret = qemu_co_recvv(s->sock, iov, request->len, offset); 225ae255e52SPaolo Bonzini if (ret != request->len) { 226ae255e52SPaolo Bonzini reply->error = EIO; 227ae255e52SPaolo Bonzini } 228ae255e52SPaolo Bonzini } 229ae255e52SPaolo Bonzini 230ae255e52SPaolo Bonzini /* Tell the read handler to read another header. */ 231ae255e52SPaolo Bonzini s->reply.handle = 0; 232ae255e52SPaolo Bonzini } 233ae255e52SPaolo Bonzini } 234ae255e52SPaolo Bonzini 235ae255e52SPaolo Bonzini static void nbd_coroutine_end(BDRVNBDState *s, struct nbd_request *request) 236ae255e52SPaolo Bonzini { 237ecda3447SPaolo Bonzini int i = HANDLE_TO_INDEX(s, request->handle); 238ecda3447SPaolo Bonzini s->recv_coroutine[i] = NULL; 239ecda3447SPaolo Bonzini if (s->in_flight-- == MAX_NBD_REQUESTS) { 240ecda3447SPaolo Bonzini qemu_co_mutex_unlock(&s->free_sema); 241ecda3447SPaolo Bonzini } 242ae255e52SPaolo Bonzini } 243ae255e52SPaolo Bonzini 24433897dc7SNick Thomas static int nbd_establish_connection(BlockDriverState *bs) 24533897dc7SNick Thomas { 24633897dc7SNick Thomas BDRVNBDState *s = bs->opaque; 24733897dc7SNick Thomas int sock; 24833897dc7SNick Thomas int ret; 24933897dc7SNick Thomas off_t size; 25033897dc7SNick Thomas size_t blocksize; 25133897dc7SNick Thomas 25233897dc7SNick Thomas if (s->host_spec[0] == '/') { 25333897dc7SNick Thomas sock = unix_socket_outgoing(s->host_spec); 25433897dc7SNick Thomas } else { 25533897dc7SNick Thomas sock = tcp_socket_outgoing_spec(s->host_spec); 25633897dc7SNick Thomas } 25733897dc7SNick Thomas 25833897dc7SNick Thomas /* Failed to establish connection */ 25933897dc7SNick Thomas if (sock == -1) { 26033897dc7SNick Thomas logout("Failed to establish connection to NBD server\n"); 26133897dc7SNick Thomas return -errno; 26233897dc7SNick Thomas } 26333897dc7SNick Thomas 26433897dc7SNick Thomas /* NBD handshake */ 265b90fb4b8SPaolo Bonzini ret = nbd_receive_negotiate(sock, s->export_name, &s->nbdflags, &size, 26633897dc7SNick Thomas &blocksize); 26733897dc7SNick Thomas if (ret == -1) { 26833897dc7SNick Thomas logout("Failed to negotiate with the NBD server\n"); 26933897dc7SNick Thomas closesocket(sock); 27033897dc7SNick Thomas return -errno; 27133897dc7SNick Thomas } 27233897dc7SNick Thomas 273ae255e52SPaolo Bonzini /* Now that we're connected, set the socket to be non-blocking and 274ae255e52SPaolo Bonzini * kick the reply mechanism. */ 27533897dc7SNick Thomas socket_set_nonblock(sock); 276ae255e52SPaolo Bonzini qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, NULL, 277ae255e52SPaolo Bonzini nbd_have_request, NULL, s); 27833897dc7SNick Thomas 27933897dc7SNick Thomas s->sock = sock; 28033897dc7SNick Thomas s->size = size; 28133897dc7SNick Thomas s->blocksize = blocksize; 28233897dc7SNick Thomas 28333897dc7SNick Thomas logout("Established connection with NBD server\n"); 28433897dc7SNick Thomas return 0; 28533897dc7SNick Thomas } 28633897dc7SNick Thomas 28733897dc7SNick Thomas static void nbd_teardown_connection(BlockDriverState *bs) 28833897dc7SNick Thomas { 28933897dc7SNick Thomas BDRVNBDState *s = bs->opaque; 29033897dc7SNick Thomas struct nbd_request request; 29133897dc7SNick Thomas 29233897dc7SNick Thomas request.type = NBD_CMD_DISC; 29333897dc7SNick Thomas request.from = 0; 29433897dc7SNick Thomas request.len = 0; 29533897dc7SNick Thomas nbd_send_request(s->sock, &request); 29633897dc7SNick Thomas 297ae255e52SPaolo Bonzini qemu_aio_set_fd_handler(s->sock, NULL, NULL, NULL, NULL, NULL); 29833897dc7SNick Thomas closesocket(s->sock); 29933897dc7SNick Thomas } 30033897dc7SNick Thomas 30133897dc7SNick Thomas static int nbd_open(BlockDriverState *bs, const char* filename, int flags) 30233897dc7SNick Thomas { 30333897dc7SNick Thomas BDRVNBDState *s = bs->opaque; 30433897dc7SNick Thomas int result; 30533897dc7SNick Thomas 306ecda3447SPaolo Bonzini qemu_co_mutex_init(&s->send_mutex); 307ecda3447SPaolo Bonzini qemu_co_mutex_init(&s->free_sema); 308ae255e52SPaolo Bonzini 30933897dc7SNick Thomas /* Pop the config into our state object. Exit if invalid. */ 31033897dc7SNick Thomas result = nbd_config(s, filename, flags); 31133897dc7SNick Thomas if (result != 0) { 31233897dc7SNick Thomas return result; 31333897dc7SNick Thomas } 31433897dc7SNick Thomas 31533897dc7SNick Thomas /* establish TCP connection, return error if it fails 31633897dc7SNick Thomas * TODO: Configurable retry-until-timeout behaviour. 31733897dc7SNick Thomas */ 31833897dc7SNick Thomas result = nbd_establish_connection(bs); 31933897dc7SNick Thomas 32033897dc7SNick Thomas return result; 32133897dc7SNick Thomas } 32233897dc7SNick Thomas 323d9b09f13SPaolo Bonzini static int nbd_co_readv_1(BlockDriverState *bs, int64_t sector_num, 324d9b09f13SPaolo Bonzini int nb_sectors, QEMUIOVector *qiov, 325d9b09f13SPaolo Bonzini int offset) 326019d6b8fSAnthony Liguori { 327019d6b8fSAnthony Liguori BDRVNBDState *s = bs->opaque; 328019d6b8fSAnthony Liguori struct nbd_request request; 329019d6b8fSAnthony Liguori struct nbd_reply reply; 330019d6b8fSAnthony Liguori 331019d6b8fSAnthony Liguori request.type = NBD_CMD_READ; 3323a93113aSDong Xu Wang request.from = sector_num * 512; 333019d6b8fSAnthony Liguori request.len = nb_sectors * 512; 334019d6b8fSAnthony Liguori 335ae255e52SPaolo Bonzini nbd_coroutine_start(s, &request); 336ae255e52SPaolo Bonzini if (nbd_co_send_request(s, &request, NULL, 0) == -1) { 337ae255e52SPaolo Bonzini reply.error = errno; 338ae255e52SPaolo Bonzini } else { 339d9b09f13SPaolo Bonzini nbd_co_receive_reply(s, &request, &reply, qiov->iov, offset); 340ae255e52SPaolo Bonzini } 341ae255e52SPaolo Bonzini nbd_coroutine_end(s, &request); 342019d6b8fSAnthony Liguori return -reply.error; 343019d6b8fSAnthony Liguori 344019d6b8fSAnthony Liguori } 345019d6b8fSAnthony Liguori 346d9b09f13SPaolo Bonzini static int nbd_co_writev_1(BlockDriverState *bs, int64_t sector_num, 347d9b09f13SPaolo Bonzini int nb_sectors, QEMUIOVector *qiov, 348d9b09f13SPaolo Bonzini int offset) 349019d6b8fSAnthony Liguori { 350019d6b8fSAnthony Liguori BDRVNBDState *s = bs->opaque; 351019d6b8fSAnthony Liguori struct nbd_request request; 352019d6b8fSAnthony Liguori struct nbd_reply reply; 353019d6b8fSAnthony Liguori 354019d6b8fSAnthony Liguori request.type = NBD_CMD_WRITE; 355*2c7989a9SPaolo Bonzini if (!bdrv_enable_write_cache(bs) && (s->nbdflags & NBD_FLAG_SEND_FUA)) { 356*2c7989a9SPaolo Bonzini request.type |= NBD_CMD_FLAG_FUA; 357*2c7989a9SPaolo Bonzini } 358*2c7989a9SPaolo Bonzini 3593a93113aSDong Xu Wang request.from = sector_num * 512; 360019d6b8fSAnthony Liguori request.len = nb_sectors * 512; 361019d6b8fSAnthony Liguori 362ae255e52SPaolo Bonzini nbd_coroutine_start(s, &request); 363d9b09f13SPaolo Bonzini if (nbd_co_send_request(s, &request, qiov->iov, offset) == -1) { 364ae255e52SPaolo Bonzini reply.error = errno; 365ae255e52SPaolo Bonzini } else { 366ae255e52SPaolo Bonzini nbd_co_receive_reply(s, &request, &reply, NULL, 0); 367ae255e52SPaolo Bonzini } 368ae255e52SPaolo Bonzini nbd_coroutine_end(s, &request); 369019d6b8fSAnthony Liguori return -reply.error; 370e183ef75SPaolo Bonzini } 371e183ef75SPaolo Bonzini 372d9b09f13SPaolo Bonzini /* qemu-nbd has a limit of slightly less than 1M per request. Try to 373d9b09f13SPaolo Bonzini * remain aligned to 4K. */ 374d9b09f13SPaolo Bonzini #define NBD_MAX_SECTORS 2040 375d9b09f13SPaolo Bonzini 376d9b09f13SPaolo Bonzini static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num, 377d9b09f13SPaolo Bonzini int nb_sectors, QEMUIOVector *qiov) 378d9b09f13SPaolo Bonzini { 379d9b09f13SPaolo Bonzini int offset = 0; 380d9b09f13SPaolo Bonzini int ret; 381d9b09f13SPaolo Bonzini while (nb_sectors > NBD_MAX_SECTORS) { 382d9b09f13SPaolo Bonzini ret = nbd_co_readv_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset); 383d9b09f13SPaolo Bonzini if (ret < 0) { 384d9b09f13SPaolo Bonzini return ret; 385d9b09f13SPaolo Bonzini } 386d9b09f13SPaolo Bonzini offset += NBD_MAX_SECTORS * 512; 387d9b09f13SPaolo Bonzini sector_num += NBD_MAX_SECTORS; 388d9b09f13SPaolo Bonzini nb_sectors -= NBD_MAX_SECTORS; 389d9b09f13SPaolo Bonzini } 390d9b09f13SPaolo Bonzini return nbd_co_readv_1(bs, sector_num, nb_sectors, qiov, offset); 391d9b09f13SPaolo Bonzini } 392d9b09f13SPaolo Bonzini 393d9b09f13SPaolo Bonzini static int nbd_co_writev(BlockDriverState *bs, int64_t sector_num, 394d9b09f13SPaolo Bonzini int nb_sectors, QEMUIOVector *qiov) 395d9b09f13SPaolo Bonzini { 396d9b09f13SPaolo Bonzini int offset = 0; 397d9b09f13SPaolo Bonzini int ret; 398d9b09f13SPaolo Bonzini while (nb_sectors > NBD_MAX_SECTORS) { 399d9b09f13SPaolo Bonzini ret = nbd_co_writev_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset); 400d9b09f13SPaolo Bonzini if (ret < 0) { 401d9b09f13SPaolo Bonzini return ret; 402d9b09f13SPaolo Bonzini } 403d9b09f13SPaolo Bonzini offset += NBD_MAX_SECTORS * 512; 404d9b09f13SPaolo Bonzini sector_num += NBD_MAX_SECTORS; 405d9b09f13SPaolo Bonzini nb_sectors -= NBD_MAX_SECTORS; 406d9b09f13SPaolo Bonzini } 407d9b09f13SPaolo Bonzini return nbd_co_writev_1(bs, sector_num, nb_sectors, qiov, offset); 408d9b09f13SPaolo Bonzini } 409d9b09f13SPaolo Bonzini 410019d6b8fSAnthony Liguori static void nbd_close(BlockDriverState *bs) 411019d6b8fSAnthony Liguori { 412d2d979c6SNick Thomas BDRVNBDState *s = bs->opaque; 4137267c094SAnthony Liguori g_free(s->export_name); 4147267c094SAnthony Liguori g_free(s->host_spec); 415d2d979c6SNick Thomas 41633897dc7SNick Thomas nbd_teardown_connection(bs); 417019d6b8fSAnthony Liguori } 418019d6b8fSAnthony Liguori 419019d6b8fSAnthony Liguori static int64_t nbd_getlength(BlockDriverState *bs) 420019d6b8fSAnthony Liguori { 421019d6b8fSAnthony Liguori BDRVNBDState *s = bs->opaque; 422019d6b8fSAnthony Liguori 423019d6b8fSAnthony Liguori return s->size; 424019d6b8fSAnthony Liguori } 425019d6b8fSAnthony Liguori 426019d6b8fSAnthony Liguori static BlockDriver bdrv_nbd = { 427019d6b8fSAnthony Liguori .format_name = "nbd", 428019d6b8fSAnthony Liguori .instance_size = sizeof(BDRVNBDState), 42966f82ceeSKevin Wolf .bdrv_file_open = nbd_open, 430ae255e52SPaolo Bonzini .bdrv_co_readv = nbd_co_readv, 431ae255e52SPaolo Bonzini .bdrv_co_writev = nbd_co_writev, 432019d6b8fSAnthony Liguori .bdrv_close = nbd_close, 433019d6b8fSAnthony Liguori .bdrv_getlength = nbd_getlength, 434019d6b8fSAnthony Liguori .protocol_name = "nbd", 435019d6b8fSAnthony Liguori }; 436019d6b8fSAnthony Liguori 437019d6b8fSAnthony Liguori static void bdrv_nbd_init(void) 438019d6b8fSAnthony Liguori { 439019d6b8fSAnthony Liguori bdrv_register(&bdrv_nbd); 440019d6b8fSAnthony Liguori } 441019d6b8fSAnthony Liguori 442019d6b8fSAnthony Liguori block_init(bdrv_nbd_init); 443