xref: /openbmc/qemu/block/nbd.c (revision 2fc8ae1d)
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;
153dd3e8ac4SPaolo Bonzini     uint64_t i;
1547fe7b68bSPaolo Bonzini     int ret;
155ae255e52SPaolo Bonzini 
156ae255e52SPaolo Bonzini     if (s->reply.handle == 0) {
1577fe7b68bSPaolo Bonzini         /* No reply already in flight.  Fetch a header.  It is possible
1587fe7b68bSPaolo Bonzini          * that another thread has done the same thing in parallel, so
1597fe7b68bSPaolo Bonzini          * the socket is not readable anymore.
1607fe7b68bSPaolo Bonzini          */
1617fe7b68bSPaolo Bonzini         ret = nbd_receive_reply(s->sock, &s->reply);
1627fe7b68bSPaolo Bonzini         if (ret == -EAGAIN) {
1637fe7b68bSPaolo Bonzini             return;
1647fe7b68bSPaolo Bonzini         }
1657fe7b68bSPaolo Bonzini         if (ret < 0) {
166ae255e52SPaolo Bonzini             s->reply.handle = 0;
167ecda3447SPaolo Bonzini             goto fail;
168ae255e52SPaolo Bonzini         }
169ae255e52SPaolo Bonzini     }
170ae255e52SPaolo Bonzini 
171ae255e52SPaolo Bonzini     /* There's no need for a mutex on the receive side, because the
172ae255e52SPaolo Bonzini      * handler acts as a synchronization point and ensures that only
173ae255e52SPaolo Bonzini      * one coroutine is called until the reply finishes.  */
174ecda3447SPaolo Bonzini     i = HANDLE_TO_INDEX(s, s->reply.handle);
175dd3e8ac4SPaolo Bonzini     if (i >= MAX_NBD_REQUESTS) {
176dd3e8ac4SPaolo Bonzini         goto fail;
177dd3e8ac4SPaolo Bonzini     }
178dd3e8ac4SPaolo Bonzini 
179ecda3447SPaolo Bonzini     if (s->recv_coroutine[i]) {
180ecda3447SPaolo Bonzini         qemu_coroutine_enter(s->recv_coroutine[i], NULL);
181ecda3447SPaolo Bonzini         return;
182ecda3447SPaolo Bonzini     }
183ecda3447SPaolo Bonzini 
184ecda3447SPaolo Bonzini fail:
185ecda3447SPaolo Bonzini     for (i = 0; i < MAX_NBD_REQUESTS; i++) {
186ecda3447SPaolo Bonzini         if (s->recv_coroutine[i]) {
187ecda3447SPaolo Bonzini             qemu_coroutine_enter(s->recv_coroutine[i], NULL);
188ecda3447SPaolo Bonzini         }
189ae255e52SPaolo Bonzini     }
190ae255e52SPaolo Bonzini }
191ae255e52SPaolo Bonzini 
192ae255e52SPaolo Bonzini static void nbd_restart_write(void *opaque)
193ae255e52SPaolo Bonzini {
194ae255e52SPaolo Bonzini     BDRVNBDState *s = opaque;
195ecda3447SPaolo Bonzini     qemu_coroutine_enter(s->send_coroutine, NULL);
196ae255e52SPaolo Bonzini }
197ae255e52SPaolo Bonzini 
198ae255e52SPaolo Bonzini static int nbd_co_send_request(BDRVNBDState *s, struct nbd_request *request,
199*2fc8ae1dSMichael Tokarev                                QEMUIOVector *qiov, int offset)
200ae255e52SPaolo Bonzini {
201ae255e52SPaolo Bonzini     int rc, ret;
202ae255e52SPaolo Bonzini 
203ecda3447SPaolo Bonzini     qemu_co_mutex_lock(&s->send_mutex);
204ecda3447SPaolo Bonzini     s->send_coroutine = qemu_coroutine_self();
205ae255e52SPaolo Bonzini     qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, nbd_restart_write,
206bafbd6a1SPaolo Bonzini                             nbd_have_request, s);
207ae255e52SPaolo Bonzini     rc = nbd_send_request(s->sock, request);
208*2fc8ae1dSMichael Tokarev     if (rc >= 0 && qiov) {
209*2fc8ae1dSMichael Tokarev         ret = qemu_co_sendv(s->sock, qiov->iov, qiov->niov,
210*2fc8ae1dSMichael Tokarev                             offset, request->len);
211ae255e52SPaolo Bonzini         if (ret != request->len) {
212185b4338SPaolo Bonzini             return -EIO;
213ae255e52SPaolo Bonzini         }
214ae255e52SPaolo Bonzini     }
215ae255e52SPaolo Bonzini     qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, NULL,
216bafbd6a1SPaolo Bonzini                             nbd_have_request, s);
217ecda3447SPaolo Bonzini     s->send_coroutine = NULL;
218ecda3447SPaolo Bonzini     qemu_co_mutex_unlock(&s->send_mutex);
219ae255e52SPaolo Bonzini     return rc;
220ae255e52SPaolo Bonzini }
221ae255e52SPaolo Bonzini 
222ae255e52SPaolo Bonzini static void nbd_co_receive_reply(BDRVNBDState *s, struct nbd_request *request,
223ae255e52SPaolo Bonzini                                  struct nbd_reply *reply,
224*2fc8ae1dSMichael Tokarev                                  QEMUIOVector *qiov, int offset)
225ae255e52SPaolo Bonzini {
226ae255e52SPaolo Bonzini     int ret;
227ae255e52SPaolo Bonzini 
228ecda3447SPaolo Bonzini     /* Wait until we're woken up by the read handler.  TODO: perhaps
229ecda3447SPaolo Bonzini      * peek at the next reply and avoid yielding if it's ours?  */
230ae255e52SPaolo Bonzini     qemu_coroutine_yield();
231ae255e52SPaolo Bonzini     *reply = s->reply;
232ae255e52SPaolo Bonzini     if (reply->handle != request->handle) {
233ae255e52SPaolo Bonzini         reply->error = EIO;
234ae255e52SPaolo Bonzini     } else {
235*2fc8ae1dSMichael Tokarev         if (qiov && reply->error == 0) {
236*2fc8ae1dSMichael Tokarev             ret = qemu_co_recvv(s->sock, qiov->iov, qiov->niov,
237*2fc8ae1dSMichael Tokarev                                 offset, request->len);
238ae255e52SPaolo Bonzini             if (ret != request->len) {
239ae255e52SPaolo Bonzini                 reply->error = EIO;
240ae255e52SPaolo Bonzini             }
241ae255e52SPaolo Bonzini         }
242ae255e52SPaolo Bonzini 
243ae255e52SPaolo Bonzini         /* Tell the read handler to read another header.  */
244ae255e52SPaolo Bonzini         s->reply.handle = 0;
245ae255e52SPaolo Bonzini     }
246ae255e52SPaolo Bonzini }
247ae255e52SPaolo Bonzini 
248ae255e52SPaolo Bonzini static void nbd_coroutine_end(BDRVNBDState *s, struct nbd_request *request)
249ae255e52SPaolo Bonzini {
250ecda3447SPaolo Bonzini     int i = HANDLE_TO_INDEX(s, request->handle);
251ecda3447SPaolo Bonzini     s->recv_coroutine[i] = NULL;
252ecda3447SPaolo Bonzini     if (s->in_flight-- == MAX_NBD_REQUESTS) {
253ecda3447SPaolo Bonzini         qemu_co_mutex_unlock(&s->free_sema);
254ecda3447SPaolo Bonzini     }
255ae255e52SPaolo Bonzini }
256ae255e52SPaolo Bonzini 
25733897dc7SNick Thomas static int nbd_establish_connection(BlockDriverState *bs)
25833897dc7SNick Thomas {
25933897dc7SNick Thomas     BDRVNBDState *s = bs->opaque;
26033897dc7SNick Thomas     int sock;
26133897dc7SNick Thomas     int ret;
26233897dc7SNick Thomas     off_t size;
26333897dc7SNick Thomas     size_t blocksize;
26433897dc7SNick Thomas 
26533897dc7SNick Thomas     if (s->host_spec[0] == '/') {
26633897dc7SNick Thomas         sock = unix_socket_outgoing(s->host_spec);
26733897dc7SNick Thomas     } else {
26833897dc7SNick Thomas         sock = tcp_socket_outgoing_spec(s->host_spec);
26933897dc7SNick Thomas     }
27033897dc7SNick Thomas 
27133897dc7SNick Thomas     /* Failed to establish connection */
272fc19f8a0SPaolo Bonzini     if (sock < 0) {
27333897dc7SNick Thomas         logout("Failed to establish connection to NBD server\n");
27433897dc7SNick Thomas         return -errno;
27533897dc7SNick Thomas     }
27633897dc7SNick Thomas 
27733897dc7SNick Thomas     /* NBD handshake */
278b90fb4b8SPaolo Bonzini     ret = nbd_receive_negotiate(sock, s->export_name, &s->nbdflags, &size,
27933897dc7SNick Thomas                                 &blocksize);
280fc19f8a0SPaolo Bonzini     if (ret < 0) {
28133897dc7SNick Thomas         logout("Failed to negotiate with the NBD server\n");
28233897dc7SNick Thomas         closesocket(sock);
283185b4338SPaolo Bonzini         return ret;
28433897dc7SNick Thomas     }
28533897dc7SNick Thomas 
286ae255e52SPaolo Bonzini     /* Now that we're connected, set the socket to be non-blocking and
287ae255e52SPaolo Bonzini      * kick the reply mechanism.  */
28833897dc7SNick Thomas     socket_set_nonblock(sock);
289b3adf53aSKevin Wolf     qemu_aio_set_fd_handler(sock, nbd_reply_ready, NULL,
290bafbd6a1SPaolo Bonzini                             nbd_have_request, s);
29133897dc7SNick Thomas 
29233897dc7SNick Thomas     s->sock = sock;
29333897dc7SNick Thomas     s->size = size;
29433897dc7SNick Thomas     s->blocksize = blocksize;
29533897dc7SNick Thomas 
29633897dc7SNick Thomas     logout("Established connection with NBD server\n");
29733897dc7SNick Thomas     return 0;
29833897dc7SNick Thomas }
29933897dc7SNick Thomas 
30033897dc7SNick Thomas static void nbd_teardown_connection(BlockDriverState *bs)
30133897dc7SNick Thomas {
30233897dc7SNick Thomas     BDRVNBDState *s = bs->opaque;
30333897dc7SNick Thomas     struct nbd_request request;
30433897dc7SNick Thomas 
30533897dc7SNick Thomas     request.type = NBD_CMD_DISC;
30633897dc7SNick Thomas     request.from = 0;
30733897dc7SNick Thomas     request.len = 0;
30833897dc7SNick Thomas     nbd_send_request(s->sock, &request);
30933897dc7SNick Thomas 
310bafbd6a1SPaolo Bonzini     qemu_aio_set_fd_handler(s->sock, NULL, NULL, NULL, NULL);
31133897dc7SNick Thomas     closesocket(s->sock);
31233897dc7SNick Thomas }
31333897dc7SNick Thomas 
31433897dc7SNick Thomas static int nbd_open(BlockDriverState *bs, const char* filename, int flags)
31533897dc7SNick Thomas {
31633897dc7SNick Thomas     BDRVNBDState *s = bs->opaque;
31733897dc7SNick Thomas     int result;
31833897dc7SNick Thomas 
319ecda3447SPaolo Bonzini     qemu_co_mutex_init(&s->send_mutex);
320ecda3447SPaolo Bonzini     qemu_co_mutex_init(&s->free_sema);
321ae255e52SPaolo Bonzini 
32233897dc7SNick Thomas     /* Pop the config into our state object. Exit if invalid. */
32333897dc7SNick Thomas     result = nbd_config(s, filename, flags);
32433897dc7SNick Thomas     if (result != 0) {
32533897dc7SNick Thomas         return result;
32633897dc7SNick Thomas     }
32733897dc7SNick Thomas 
32833897dc7SNick Thomas     /* establish TCP connection, return error if it fails
32933897dc7SNick Thomas      * TODO: Configurable retry-until-timeout behaviour.
33033897dc7SNick Thomas      */
33133897dc7SNick Thomas     result = nbd_establish_connection(bs);
33233897dc7SNick Thomas 
33333897dc7SNick Thomas     return result;
33433897dc7SNick Thomas }
33533897dc7SNick Thomas 
336d9b09f13SPaolo Bonzini static int nbd_co_readv_1(BlockDriverState *bs, int64_t sector_num,
337d9b09f13SPaolo Bonzini                           int nb_sectors, QEMUIOVector *qiov,
338d9b09f13SPaolo Bonzini                           int offset)
339019d6b8fSAnthony Liguori {
340019d6b8fSAnthony Liguori     BDRVNBDState *s = bs->opaque;
341019d6b8fSAnthony Liguori     struct nbd_request request;
342019d6b8fSAnthony Liguori     struct nbd_reply reply;
343fc19f8a0SPaolo Bonzini     ssize_t ret;
344019d6b8fSAnthony Liguori 
345019d6b8fSAnthony Liguori     request.type = NBD_CMD_READ;
3463a93113aSDong Xu Wang     request.from = sector_num * 512;
347019d6b8fSAnthony Liguori     request.len = nb_sectors * 512;
348019d6b8fSAnthony Liguori 
349ae255e52SPaolo Bonzini     nbd_coroutine_start(s, &request);
350fc19f8a0SPaolo Bonzini     ret = nbd_co_send_request(s, &request, NULL, 0);
351fc19f8a0SPaolo Bonzini     if (ret < 0) {
352185b4338SPaolo Bonzini         reply.error = -ret;
353ae255e52SPaolo Bonzini     } else {
354*2fc8ae1dSMichael Tokarev         nbd_co_receive_reply(s, &request, &reply, qiov, offset);
355ae255e52SPaolo Bonzini     }
356ae255e52SPaolo Bonzini     nbd_coroutine_end(s, &request);
357019d6b8fSAnthony Liguori     return -reply.error;
358019d6b8fSAnthony Liguori 
359019d6b8fSAnthony Liguori }
360019d6b8fSAnthony Liguori 
361d9b09f13SPaolo Bonzini static int nbd_co_writev_1(BlockDriverState *bs, int64_t sector_num,
362d9b09f13SPaolo Bonzini                            int nb_sectors, QEMUIOVector *qiov,
363d9b09f13SPaolo Bonzini                            int offset)
364019d6b8fSAnthony Liguori {
365019d6b8fSAnthony Liguori     BDRVNBDState *s = bs->opaque;
366019d6b8fSAnthony Liguori     struct nbd_request request;
367019d6b8fSAnthony Liguori     struct nbd_reply reply;
368fc19f8a0SPaolo Bonzini     ssize_t ret;
369019d6b8fSAnthony Liguori 
370019d6b8fSAnthony Liguori     request.type = NBD_CMD_WRITE;
3712c7989a9SPaolo Bonzini     if (!bdrv_enable_write_cache(bs) && (s->nbdflags & NBD_FLAG_SEND_FUA)) {
3722c7989a9SPaolo Bonzini         request.type |= NBD_CMD_FLAG_FUA;
3732c7989a9SPaolo Bonzini     }
3742c7989a9SPaolo Bonzini 
3753a93113aSDong Xu Wang     request.from = sector_num * 512;
376019d6b8fSAnthony Liguori     request.len = nb_sectors * 512;
377019d6b8fSAnthony Liguori 
378ae255e52SPaolo Bonzini     nbd_coroutine_start(s, &request);
379*2fc8ae1dSMichael Tokarev     ret = nbd_co_send_request(s, &request, qiov, offset);
380fc19f8a0SPaolo Bonzini     if (ret < 0) {
381185b4338SPaolo Bonzini         reply.error = -ret;
382ae255e52SPaolo Bonzini     } else {
383ae255e52SPaolo Bonzini         nbd_co_receive_reply(s, &request, &reply, NULL, 0);
384ae255e52SPaolo Bonzini     }
385ae255e52SPaolo Bonzini     nbd_coroutine_end(s, &request);
386019d6b8fSAnthony Liguori     return -reply.error;
387e183ef75SPaolo Bonzini }
388e183ef75SPaolo Bonzini 
389d9b09f13SPaolo Bonzini /* qemu-nbd has a limit of slightly less than 1M per request.  Try to
390d9b09f13SPaolo Bonzini  * remain aligned to 4K. */
391d9b09f13SPaolo Bonzini #define NBD_MAX_SECTORS 2040
392d9b09f13SPaolo Bonzini 
393d9b09f13SPaolo Bonzini static int nbd_co_readv(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_readv_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_readv_1(bs, sector_num, nb_sectors, qiov, offset);
408d9b09f13SPaolo Bonzini }
409d9b09f13SPaolo Bonzini 
410d9b09f13SPaolo Bonzini static int nbd_co_writev(BlockDriverState *bs, int64_t sector_num,
411d9b09f13SPaolo Bonzini                          int nb_sectors, QEMUIOVector *qiov)
412d9b09f13SPaolo Bonzini {
413d9b09f13SPaolo Bonzini     int offset = 0;
414d9b09f13SPaolo Bonzini     int ret;
415d9b09f13SPaolo Bonzini     while (nb_sectors > NBD_MAX_SECTORS) {
416d9b09f13SPaolo Bonzini         ret = nbd_co_writev_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset);
417d9b09f13SPaolo Bonzini         if (ret < 0) {
418d9b09f13SPaolo Bonzini             return ret;
419d9b09f13SPaolo Bonzini         }
420d9b09f13SPaolo Bonzini         offset += NBD_MAX_SECTORS * 512;
421d9b09f13SPaolo Bonzini         sector_num += NBD_MAX_SECTORS;
422d9b09f13SPaolo Bonzini         nb_sectors -= NBD_MAX_SECTORS;
423d9b09f13SPaolo Bonzini     }
424d9b09f13SPaolo Bonzini     return nbd_co_writev_1(bs, sector_num, nb_sectors, qiov, offset);
425d9b09f13SPaolo Bonzini }
426d9b09f13SPaolo Bonzini 
4271486d04aSPaolo Bonzini static int nbd_co_flush(BlockDriverState *bs)
4281486d04aSPaolo Bonzini {
4291486d04aSPaolo Bonzini     BDRVNBDState *s = bs->opaque;
4301486d04aSPaolo Bonzini     struct nbd_request request;
4311486d04aSPaolo Bonzini     struct nbd_reply reply;
432fc19f8a0SPaolo Bonzini     ssize_t ret;
4331486d04aSPaolo Bonzini 
4341486d04aSPaolo Bonzini     if (!(s->nbdflags & NBD_FLAG_SEND_FLUSH)) {
4351486d04aSPaolo Bonzini         return 0;
4361486d04aSPaolo Bonzini     }
4371486d04aSPaolo Bonzini 
4381486d04aSPaolo Bonzini     request.type = NBD_CMD_FLUSH;
4391486d04aSPaolo Bonzini     if (s->nbdflags & NBD_FLAG_SEND_FUA) {
4401486d04aSPaolo Bonzini         request.type |= NBD_CMD_FLAG_FUA;
4411486d04aSPaolo Bonzini     }
4421486d04aSPaolo Bonzini 
4431486d04aSPaolo Bonzini     request.from = 0;
4441486d04aSPaolo Bonzini     request.len = 0;
4451486d04aSPaolo Bonzini 
4461486d04aSPaolo Bonzini     nbd_coroutine_start(s, &request);
447fc19f8a0SPaolo Bonzini     ret = nbd_co_send_request(s, &request, NULL, 0);
448fc19f8a0SPaolo Bonzini     if (ret < 0) {
449185b4338SPaolo Bonzini         reply.error = -ret;
4501486d04aSPaolo Bonzini     } else {
4511486d04aSPaolo Bonzini         nbd_co_receive_reply(s, &request, &reply, NULL, 0);
4521486d04aSPaolo Bonzini     }
4531486d04aSPaolo Bonzini     nbd_coroutine_end(s, &request);
4541486d04aSPaolo Bonzini     return -reply.error;
4551486d04aSPaolo Bonzini }
4561486d04aSPaolo Bonzini 
4577a706633SPaolo Bonzini static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num,
4587a706633SPaolo Bonzini                           int nb_sectors)
4597a706633SPaolo Bonzini {
4607a706633SPaolo Bonzini     BDRVNBDState *s = bs->opaque;
4617a706633SPaolo Bonzini     struct nbd_request request;
4627a706633SPaolo Bonzini     struct nbd_reply reply;
463fc19f8a0SPaolo Bonzini     ssize_t ret;
4647a706633SPaolo Bonzini 
4657a706633SPaolo Bonzini     if (!(s->nbdflags & NBD_FLAG_SEND_TRIM)) {
4667a706633SPaolo Bonzini         return 0;
4677a706633SPaolo Bonzini     }
4687a706633SPaolo Bonzini     request.type = NBD_CMD_TRIM;
4697a706633SPaolo Bonzini     request.from = sector_num * 512;;
4707a706633SPaolo Bonzini     request.len = nb_sectors * 512;
4717a706633SPaolo Bonzini 
4727a706633SPaolo Bonzini     nbd_coroutine_start(s, &request);
473fc19f8a0SPaolo Bonzini     ret = nbd_co_send_request(s, &request, NULL, 0);
474fc19f8a0SPaolo Bonzini     if (ret < 0) {
475185b4338SPaolo Bonzini         reply.error = -ret;
4767a706633SPaolo Bonzini     } else {
4777a706633SPaolo Bonzini         nbd_co_receive_reply(s, &request, &reply, NULL, 0);
4787a706633SPaolo Bonzini     }
4797a706633SPaolo Bonzini     nbd_coroutine_end(s, &request);
4807a706633SPaolo Bonzini     return -reply.error;
4817a706633SPaolo Bonzini }
4827a706633SPaolo Bonzini 
483019d6b8fSAnthony Liguori static void nbd_close(BlockDriverState *bs)
484019d6b8fSAnthony Liguori {
485d2d979c6SNick Thomas     BDRVNBDState *s = bs->opaque;
4867267c094SAnthony Liguori     g_free(s->export_name);
4877267c094SAnthony Liguori     g_free(s->host_spec);
488d2d979c6SNick Thomas 
48933897dc7SNick Thomas     nbd_teardown_connection(bs);
490019d6b8fSAnthony Liguori }
491019d6b8fSAnthony Liguori 
492019d6b8fSAnthony Liguori static int64_t nbd_getlength(BlockDriverState *bs)
493019d6b8fSAnthony Liguori {
494019d6b8fSAnthony Liguori     BDRVNBDState *s = bs->opaque;
495019d6b8fSAnthony Liguori 
496019d6b8fSAnthony Liguori     return s->size;
497019d6b8fSAnthony Liguori }
498019d6b8fSAnthony Liguori 
499019d6b8fSAnthony Liguori static BlockDriver bdrv_nbd = {
500019d6b8fSAnthony Liguori     .format_name         = "nbd",
501019d6b8fSAnthony Liguori     .instance_size       = sizeof(BDRVNBDState),
50266f82ceeSKevin Wolf     .bdrv_file_open      = nbd_open,
503ae255e52SPaolo Bonzini     .bdrv_co_readv       = nbd_co_readv,
504ae255e52SPaolo Bonzini     .bdrv_co_writev      = nbd_co_writev,
505019d6b8fSAnthony Liguori     .bdrv_close          = nbd_close,
5061486d04aSPaolo Bonzini     .bdrv_co_flush_to_os = nbd_co_flush,
5077a706633SPaolo Bonzini     .bdrv_co_discard     = nbd_co_discard,
508019d6b8fSAnthony Liguori     .bdrv_getlength      = nbd_getlength,
509019d6b8fSAnthony Liguori     .protocol_name       = "nbd",
510019d6b8fSAnthony Liguori };
511019d6b8fSAnthony Liguori 
512019d6b8fSAnthony Liguori static void bdrv_nbd_init(void)
513019d6b8fSAnthony Liguori {
514019d6b8fSAnthony Liguori     bdrv_register(&bdrv_nbd);
515019d6b8fSAnthony Liguori }
516019d6b8fSAnthony Liguori 
517019d6b8fSAnthony Liguori block_init(bdrv_nbd_init);
518