]>
git.proxmox.com Git - qemu.git/blob - block/nbd.c
2 * QEMU Block driver for NBD
4 * Copyright (C) 2008 Bull S.A.S.
5 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
8 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 #include "qemu-common.h"
31 #include "block_int.h"
33 #include "qemu_socket.h"
35 #include <sys/types.h>
38 #define EN_OPTSTR ":exportname="
40 /* #define DEBUG_NBD */
42 #if defined(DEBUG_NBD)
43 #define logout(fmt, ...) \
44 fprintf(stderr, "nbd\t%-24s" fmt, __func__, ##__VA_ARGS__)
46 #define logout(fmt, ...) ((void)0)
49 typedef struct BDRVNBDState
{
54 char *export_name
; /* An NBD server may export several devices */
59 struct nbd_reply reply
;
61 /* If it begins with '/', this is a UNIX domain socket. Otherwise,
62 * it's a string of the form <hostname|ip4|\[ip6\]>:port
67 static int nbd_config(BDRVNBDState
*s
, const char *filename
, int flags
)
71 const char *host_spec
;
75 file
= g_strdup(filename
);
77 export_name
= strstr(file
, EN_OPTSTR
);
79 if (export_name
[strlen(EN_OPTSTR
)] == 0) {
82 export_name
[0] = 0; /* truncate 'file' */
83 export_name
+= strlen(EN_OPTSTR
);
84 s
->export_name
= g_strdup(export_name
);
87 /* extract the host_spec - fail if it's not nbd:... */
88 if (!strstart(file
, "nbd:", &host_spec
)) {
92 /* are we a UNIX or TCP socket? */
93 if (strstart(host_spec
, "unix:", &unixpath
)) {
94 if (unixpath
[0] != '/') { /* We demand an absolute path*/
97 s
->host_spec
= g_strdup(unixpath
);
99 s
->host_spec
= g_strdup(host_spec
);
107 g_free(s
->export_name
);
108 g_free(s
->host_spec
);
113 static void nbd_coroutine_start(BDRVNBDState
*s
, struct nbd_request
*request
)
115 qemu_co_mutex_lock(&s
->mutex
);
116 s
->coroutine
= qemu_coroutine_self();
117 request
->handle
= (uint64_t)(intptr_t)s
;
120 static int nbd_have_request(void *opaque
)
122 BDRVNBDState
*s
= opaque
;
124 return !!s
->coroutine
;
127 static void nbd_reply_ready(void *opaque
)
129 BDRVNBDState
*s
= opaque
;
131 if (s
->reply
.handle
== 0) {
132 /* No reply already in flight. Fetch a header. */
133 if (nbd_receive_reply(s
->sock
, &s
->reply
) < 0) {
138 /* There's no need for a mutex on the receive side, because the
139 * handler acts as a synchronization point and ensures that only
140 * one coroutine is called until the reply finishes. */
142 qemu_coroutine_enter(s
->coroutine
, NULL
);
146 static void nbd_restart_write(void *opaque
)
148 BDRVNBDState
*s
= opaque
;
149 qemu_coroutine_enter(s
->coroutine
, NULL
);
152 static int nbd_co_send_request(BDRVNBDState
*s
, struct nbd_request
*request
,
153 struct iovec
*iov
, int offset
)
157 qemu_aio_set_fd_handler(s
->sock
, nbd_reply_ready
, nbd_restart_write
,
158 nbd_have_request
, NULL
, s
);
159 rc
= nbd_send_request(s
->sock
, request
);
160 if (rc
!= -1 && iov
) {
161 ret
= qemu_co_sendv(s
->sock
, iov
, request
->len
, offset
);
162 if (ret
!= request
->len
) {
167 qemu_aio_set_fd_handler(s
->sock
, nbd_reply_ready
, NULL
,
168 nbd_have_request
, NULL
, s
);
172 static void nbd_co_receive_reply(BDRVNBDState
*s
, struct nbd_request
*request
,
173 struct nbd_reply
*reply
,
174 struct iovec
*iov
, int offset
)
178 /* Wait until we're woken up by the read handler. */
179 qemu_coroutine_yield();
181 if (reply
->handle
!= request
->handle
) {
184 if (iov
&& reply
->error
== 0) {
185 ret
= qemu_co_recvv(s
->sock
, iov
, request
->len
, offset
);
186 if (ret
!= request
->len
) {
191 /* Tell the read handler to read another header. */
196 static void nbd_coroutine_end(BDRVNBDState
*s
, struct nbd_request
*request
)
199 qemu_co_mutex_unlock(&s
->mutex
);
202 static int nbd_establish_connection(BlockDriverState
*bs
)
204 BDRVNBDState
*s
= bs
->opaque
;
210 if (s
->host_spec
[0] == '/') {
211 sock
= unix_socket_outgoing(s
->host_spec
);
213 sock
= tcp_socket_outgoing_spec(s
->host_spec
);
216 /* Failed to establish connection */
218 logout("Failed to establish connection to NBD server\n");
223 ret
= nbd_receive_negotiate(sock
, s
->export_name
, &s
->nbdflags
, &size
,
226 logout("Failed to negotiate with the NBD server\n");
231 /* Now that we're connected, set the socket to be non-blocking and
232 * kick the reply mechanism. */
233 socket_set_nonblock(sock
);
234 qemu_aio_set_fd_handler(s
->sock
, nbd_reply_ready
, NULL
,
235 nbd_have_request
, NULL
, s
);
239 s
->blocksize
= blocksize
;
241 logout("Established connection with NBD server\n");
245 static void nbd_teardown_connection(BlockDriverState
*bs
)
247 BDRVNBDState
*s
= bs
->opaque
;
248 struct nbd_request request
;
250 request
.type
= NBD_CMD_DISC
;
253 nbd_send_request(s
->sock
, &request
);
255 qemu_aio_set_fd_handler(s
->sock
, NULL
, NULL
, NULL
, NULL
, NULL
);
256 closesocket(s
->sock
);
259 static int nbd_open(BlockDriverState
*bs
, const char* filename
, int flags
)
261 BDRVNBDState
*s
= bs
->opaque
;
264 qemu_co_mutex_init(&s
->mutex
);
266 /* Pop the config into our state object. Exit if invalid. */
267 result
= nbd_config(s
, filename
, flags
);
272 /* establish TCP connection, return error if it fails
273 * TODO: Configurable retry-until-timeout behaviour.
275 result
= nbd_establish_connection(bs
);
280 static int nbd_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
281 int nb_sectors
, QEMUIOVector
*qiov
)
283 BDRVNBDState
*s
= bs
->opaque
;
284 struct nbd_request request
;
285 struct nbd_reply reply
;
287 request
.type
= NBD_CMD_READ
;
288 request
.from
= sector_num
* 512;
289 request
.len
= nb_sectors
* 512;
291 nbd_coroutine_start(s
, &request
);
292 if (nbd_co_send_request(s
, &request
, NULL
, 0) == -1) {
295 nbd_co_receive_reply(s
, &request
, &reply
, qiov
->iov
, 0);
297 nbd_coroutine_end(s
, &request
);
302 static int nbd_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
303 int nb_sectors
, QEMUIOVector
*qiov
)
305 BDRVNBDState
*s
= bs
->opaque
;
306 struct nbd_request request
;
307 struct nbd_reply reply
;
309 request
.type
= NBD_CMD_WRITE
;
310 request
.from
= sector_num
* 512;
311 request
.len
= nb_sectors
* 512;
313 nbd_coroutine_start(s
, &request
);
314 if (nbd_co_send_request(s
, &request
, qiov
->iov
, 0) == -1) {
317 nbd_co_receive_reply(s
, &request
, &reply
, NULL
, 0);
319 nbd_coroutine_end(s
, &request
);
323 static void nbd_close(BlockDriverState
*bs
)
325 BDRVNBDState
*s
= bs
->opaque
;
326 g_free(s
->export_name
);
327 g_free(s
->host_spec
);
329 nbd_teardown_connection(bs
);
332 static int64_t nbd_getlength(BlockDriverState
*bs
)
334 BDRVNBDState
*s
= bs
->opaque
;
339 static BlockDriver bdrv_nbd
= {
340 .format_name
= "nbd",
341 .instance_size
= sizeof(BDRVNBDState
),
342 .bdrv_file_open
= nbd_open
,
343 .bdrv_co_readv
= nbd_co_readv
,
344 .bdrv_co_writev
= nbd_co_writev
,
345 .bdrv_close
= nbd_close
,
346 .bdrv_getlength
= nbd_getlength
,
347 .protocol_name
= "nbd",
350 static void bdrv_nbd_init(void)
352 bdrv_register(&bdrv_nbd
);
355 block_init(bdrv_nbd_init
);