]>
git.proxmox.com Git - qemu.git/blob - block/nbd.c
48bbecacaf942dc74b4cbb64bde05fffd56ecaaa
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 #define MAX_NBD_REQUESTS 16
50 #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ ((uint64_t)(intptr_t)bs))
51 #define INDEX_TO_HANDLE(bs, index) ((index) ^ ((uint64_t)(intptr_t)bs))
53 typedef struct BDRVNBDState
{
61 Coroutine
*send_coroutine
;
64 Coroutine
*recv_coroutine
[MAX_NBD_REQUESTS
];
65 struct nbd_reply reply
;
69 char *export_name
; /* An NBD server may export several devices */
72 static int nbd_config(BDRVNBDState
*s
, const char *filename
)
76 const char *host_spec
;
80 file
= g_strdup(filename
);
82 export_name
= strstr(file
, EN_OPTSTR
);
84 if (export_name
[strlen(EN_OPTSTR
)] == 0) {
87 export_name
[0] = 0; /* truncate 'file' */
88 export_name
+= strlen(EN_OPTSTR
);
89 s
->export_name
= g_strdup(export_name
);
92 /* extract the host_spec - fail if it's not nbd:... */
93 if (!strstart(file
, "nbd:", &host_spec
)) {
97 /* are we a UNIX or TCP socket? */
98 if (strstart(host_spec
, "unix:", &unixpath
)) {
100 s
->host_spec
= g_strdup(unixpath
);
103 s
->host_spec
= g_strdup(host_spec
);
111 g_free(s
->export_name
);
112 g_free(s
->host_spec
);
117 static void nbd_coroutine_start(BDRVNBDState
*s
, struct nbd_request
*request
)
121 /* Poor man semaphore. The free_sema is locked when no other request
122 * can be accepted, and unlocked after receiving one reply. */
123 if (s
->in_flight
>= MAX_NBD_REQUESTS
- 1) {
124 qemu_co_mutex_lock(&s
->free_sema
);
125 assert(s
->in_flight
< MAX_NBD_REQUESTS
);
129 for (i
= 0; i
< MAX_NBD_REQUESTS
; i
++) {
130 if (s
->recv_coroutine
[i
] == NULL
) {
131 s
->recv_coroutine
[i
] = qemu_coroutine_self();
136 assert(i
< MAX_NBD_REQUESTS
);
137 request
->handle
= INDEX_TO_HANDLE(s
, i
);
140 static int nbd_have_request(void *opaque
)
142 BDRVNBDState
*s
= opaque
;
144 return s
->in_flight
> 0;
147 static void nbd_reply_ready(void *opaque
)
149 BDRVNBDState
*s
= opaque
;
153 if (s
->reply
.handle
== 0) {
154 /* No reply already in flight. Fetch a header. It is possible
155 * that another thread has done the same thing in parallel, so
156 * the socket is not readable anymore.
158 ret
= nbd_receive_reply(s
->sock
, &s
->reply
);
159 if (ret
== -EAGAIN
) {
168 /* There's no need for a mutex on the receive side, because the
169 * handler acts as a synchronization point and ensures that only
170 * one coroutine is called until the reply finishes. */
171 i
= HANDLE_TO_INDEX(s
, s
->reply
.handle
);
172 if (i
>= MAX_NBD_REQUESTS
) {
176 if (s
->recv_coroutine
[i
]) {
177 qemu_coroutine_enter(s
->recv_coroutine
[i
], NULL
);
182 for (i
= 0; i
< MAX_NBD_REQUESTS
; i
++) {
183 if (s
->recv_coroutine
[i
]) {
184 qemu_coroutine_enter(s
->recv_coroutine
[i
], NULL
);
189 static void nbd_restart_write(void *opaque
)
191 BDRVNBDState
*s
= opaque
;
192 qemu_coroutine_enter(s
->send_coroutine
, NULL
);
195 static int nbd_co_send_request(BDRVNBDState
*s
, struct nbd_request
*request
,
196 QEMUIOVector
*qiov
, int offset
)
200 qemu_co_mutex_lock(&s
->send_mutex
);
201 s
->send_coroutine
= qemu_coroutine_self();
202 qemu_aio_set_fd_handler(s
->sock
, nbd_reply_ready
, nbd_restart_write
,
203 nbd_have_request
, s
);
204 rc
= nbd_send_request(s
->sock
, request
);
205 if (rc
>= 0 && qiov
) {
206 ret
= qemu_co_sendv(s
->sock
, qiov
->iov
, qiov
->niov
,
207 offset
, request
->len
);
208 if (ret
!= request
->len
) {
212 qemu_aio_set_fd_handler(s
->sock
, nbd_reply_ready
, NULL
,
213 nbd_have_request
, s
);
214 s
->send_coroutine
= NULL
;
215 qemu_co_mutex_unlock(&s
->send_mutex
);
219 static void nbd_co_receive_reply(BDRVNBDState
*s
, struct nbd_request
*request
,
220 struct nbd_reply
*reply
,
221 QEMUIOVector
*qiov
, int offset
)
225 /* Wait until we're woken up by the read handler. TODO: perhaps
226 * peek at the next reply and avoid yielding if it's ours? */
227 qemu_coroutine_yield();
229 if (reply
->handle
!= request
->handle
) {
232 if (qiov
&& reply
->error
== 0) {
233 ret
= qemu_co_recvv(s
->sock
, qiov
->iov
, qiov
->niov
,
234 offset
, request
->len
);
235 if (ret
!= request
->len
) {
240 /* Tell the read handler to read another header. */
245 static void nbd_coroutine_end(BDRVNBDState
*s
, struct nbd_request
*request
)
247 int i
= HANDLE_TO_INDEX(s
, request
->handle
);
248 s
->recv_coroutine
[i
] = NULL
;
249 if (s
->in_flight
-- == MAX_NBD_REQUESTS
) {
250 qemu_co_mutex_unlock(&s
->free_sema
);
254 static int nbd_establish_connection(BlockDriverState
*bs
)
256 BDRVNBDState
*s
= bs
->opaque
;
263 sock
= unix_socket_outgoing(s
->host_spec
);
265 sock
= tcp_socket_outgoing_spec(s
->host_spec
);
268 /* Failed to establish connection */
270 logout("Failed to establish connection to NBD server\n");
275 ret
= nbd_receive_negotiate(sock
, s
->export_name
, &s
->nbdflags
, &size
,
278 logout("Failed to negotiate with the NBD server\n");
283 /* Now that we're connected, set the socket to be non-blocking and
284 * kick the reply mechanism. */
285 socket_set_nonblock(sock
);
286 qemu_aio_set_fd_handler(sock
, nbd_reply_ready
, NULL
,
287 nbd_have_request
, s
);
291 s
->blocksize
= blocksize
;
293 logout("Established connection with NBD server\n");
297 static void nbd_teardown_connection(BlockDriverState
*bs
)
299 BDRVNBDState
*s
= bs
->opaque
;
300 struct nbd_request request
;
302 request
.type
= NBD_CMD_DISC
;
305 nbd_send_request(s
->sock
, &request
);
307 qemu_aio_set_fd_handler(s
->sock
, NULL
, NULL
, NULL
, NULL
);
308 closesocket(s
->sock
);
311 static int nbd_open(BlockDriverState
*bs
, const char* filename
, int flags
)
313 BDRVNBDState
*s
= bs
->opaque
;
316 qemu_co_mutex_init(&s
->send_mutex
);
317 qemu_co_mutex_init(&s
->free_sema
);
319 /* Pop the config into our state object. Exit if invalid. */
320 result
= nbd_config(s
, filename
);
325 /* establish TCP connection, return error if it fails
326 * TODO: Configurable retry-until-timeout behaviour.
328 result
= nbd_establish_connection(bs
);
333 static int nbd_co_readv_1(BlockDriverState
*bs
, int64_t sector_num
,
334 int nb_sectors
, QEMUIOVector
*qiov
,
337 BDRVNBDState
*s
= bs
->opaque
;
338 struct nbd_request request
;
339 struct nbd_reply reply
;
342 request
.type
= NBD_CMD_READ
;
343 request
.from
= sector_num
* 512;
344 request
.len
= nb_sectors
* 512;
346 nbd_coroutine_start(s
, &request
);
347 ret
= nbd_co_send_request(s
, &request
, NULL
, 0);
351 nbd_co_receive_reply(s
, &request
, &reply
, qiov
, offset
);
353 nbd_coroutine_end(s
, &request
);
358 static int nbd_co_writev_1(BlockDriverState
*bs
, int64_t sector_num
,
359 int nb_sectors
, QEMUIOVector
*qiov
,
362 BDRVNBDState
*s
= bs
->opaque
;
363 struct nbd_request request
;
364 struct nbd_reply reply
;
367 request
.type
= NBD_CMD_WRITE
;
368 if (!bdrv_enable_write_cache(bs
) && (s
->nbdflags
& NBD_FLAG_SEND_FUA
)) {
369 request
.type
|= NBD_CMD_FLAG_FUA
;
372 request
.from
= sector_num
* 512;
373 request
.len
= nb_sectors
* 512;
375 nbd_coroutine_start(s
, &request
);
376 ret
= nbd_co_send_request(s
, &request
, qiov
, offset
);
380 nbd_co_receive_reply(s
, &request
, &reply
, NULL
, 0);
382 nbd_coroutine_end(s
, &request
);
386 /* qemu-nbd has a limit of slightly less than 1M per request. Try to
387 * remain aligned to 4K. */
388 #define NBD_MAX_SECTORS 2040
390 static int nbd_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
391 int nb_sectors
, QEMUIOVector
*qiov
)
395 while (nb_sectors
> NBD_MAX_SECTORS
) {
396 ret
= nbd_co_readv_1(bs
, sector_num
, NBD_MAX_SECTORS
, qiov
, offset
);
400 offset
+= NBD_MAX_SECTORS
* 512;
401 sector_num
+= NBD_MAX_SECTORS
;
402 nb_sectors
-= NBD_MAX_SECTORS
;
404 return nbd_co_readv_1(bs
, sector_num
, nb_sectors
, qiov
, offset
);
407 static int nbd_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
408 int nb_sectors
, QEMUIOVector
*qiov
)
412 while (nb_sectors
> NBD_MAX_SECTORS
) {
413 ret
= nbd_co_writev_1(bs
, sector_num
, NBD_MAX_SECTORS
, qiov
, offset
);
417 offset
+= NBD_MAX_SECTORS
* 512;
418 sector_num
+= NBD_MAX_SECTORS
;
419 nb_sectors
-= NBD_MAX_SECTORS
;
421 return nbd_co_writev_1(bs
, sector_num
, nb_sectors
, qiov
, offset
);
424 static int nbd_co_flush(BlockDriverState
*bs
)
426 BDRVNBDState
*s
= bs
->opaque
;
427 struct nbd_request request
;
428 struct nbd_reply reply
;
431 if (!(s
->nbdflags
& NBD_FLAG_SEND_FLUSH
)) {
435 request
.type
= NBD_CMD_FLUSH
;
436 if (s
->nbdflags
& NBD_FLAG_SEND_FUA
) {
437 request
.type
|= NBD_CMD_FLAG_FUA
;
443 nbd_coroutine_start(s
, &request
);
444 ret
= nbd_co_send_request(s
, &request
, NULL
, 0);
448 nbd_co_receive_reply(s
, &request
, &reply
, NULL
, 0);
450 nbd_coroutine_end(s
, &request
);
454 static int nbd_co_discard(BlockDriverState
*bs
, int64_t sector_num
,
457 BDRVNBDState
*s
= bs
->opaque
;
458 struct nbd_request request
;
459 struct nbd_reply reply
;
462 if (!(s
->nbdflags
& NBD_FLAG_SEND_TRIM
)) {
465 request
.type
= NBD_CMD_TRIM
;
466 request
.from
= sector_num
* 512;;
467 request
.len
= nb_sectors
* 512;
469 nbd_coroutine_start(s
, &request
);
470 ret
= nbd_co_send_request(s
, &request
, NULL
, 0);
474 nbd_co_receive_reply(s
, &request
, &reply
, NULL
, 0);
476 nbd_coroutine_end(s
, &request
);
480 static void nbd_close(BlockDriverState
*bs
)
482 BDRVNBDState
*s
= bs
->opaque
;
483 g_free(s
->export_name
);
484 g_free(s
->host_spec
);
486 nbd_teardown_connection(bs
);
489 static int64_t nbd_getlength(BlockDriverState
*bs
)
491 BDRVNBDState
*s
= bs
->opaque
;
496 static BlockDriver bdrv_nbd
= {
497 .format_name
= "nbd",
498 .instance_size
= sizeof(BDRVNBDState
),
499 .bdrv_file_open
= nbd_open
,
500 .bdrv_co_readv
= nbd_co_readv
,
501 .bdrv_co_writev
= nbd_co_writev
,
502 .bdrv_close
= nbd_close
,
503 .bdrv_co_flush_to_os
= nbd_co_flush
,
504 .bdrv_co_discard
= nbd_co_discard
,
505 .bdrv_getlength
= nbd_getlength
,
506 .protocol_name
= "nbd",
509 static void bdrv_nbd_init(void)
511 bdrv_register(&bdrv_nbd
);
514 block_init(bdrv_nbd_init
);