]> git.proxmox.com Git - qemu.git/blob - block/nbd.c
Merge remote-tracking branch 'luiz/queue/qmp' into staging
[qemu.git] / block / nbd.c
1 /*
2 * QEMU Block driver for NBD
3 *
4 * Copyright (C) 2008 Bull S.A.S.
5 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
6 *
7 * Some parts:
8 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
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
26 * THE SOFTWARE.
27 */
28
29 #include "qemu-common.h"
30 #include "block/nbd.h"
31 #include "qemu/uri.h"
32 #include "block/block_int.h"
33 #include "qemu/module.h"
34 #include "qemu/sockets.h"
35 #include "qapi/qmp/qjson.h"
36 #include "qapi/qmp/qint.h"
37
38 #include <sys/types.h>
39 #include <unistd.h>
40
41 #define EN_OPTSTR ":exportname="
42
43 /* #define DEBUG_NBD */
44
45 #if defined(DEBUG_NBD)
46 #define logout(fmt, ...) \
47 fprintf(stderr, "nbd\t%-24s" fmt, __func__, ##__VA_ARGS__)
48 #else
49 #define logout(fmt, ...) ((void)0)
50 #endif
51
52 #define MAX_NBD_REQUESTS 16
53 #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ ((uint64_t)(intptr_t)bs))
54 #define INDEX_TO_HANDLE(bs, index) ((index) ^ ((uint64_t)(intptr_t)bs))
55
56 typedef struct BDRVNBDState {
57 int sock;
58 uint32_t nbdflags;
59 off_t size;
60 size_t blocksize;
61
62 CoMutex send_mutex;
63 CoMutex free_sema;
64 Coroutine *send_coroutine;
65 int in_flight;
66
67 Coroutine *recv_coroutine[MAX_NBD_REQUESTS];
68 struct nbd_reply reply;
69
70 bool is_unix;
71 QemuOpts *socket_opts;
72
73 char *export_name; /* An NBD server may export several devices */
74 } BDRVNBDState;
75
76 static int nbd_parse_uri(const char *filename, QDict *options)
77 {
78 URI *uri;
79 const char *p;
80 QueryParams *qp = NULL;
81 int ret = 0;
82 bool is_unix;
83
84 uri = uri_parse(filename);
85 if (!uri) {
86 return -EINVAL;
87 }
88
89 /* transport */
90 if (!strcmp(uri->scheme, "nbd")) {
91 is_unix = false;
92 } else if (!strcmp(uri->scheme, "nbd+tcp")) {
93 is_unix = false;
94 } else if (!strcmp(uri->scheme, "nbd+unix")) {
95 is_unix = true;
96 } else {
97 ret = -EINVAL;
98 goto out;
99 }
100
101 p = uri->path ? uri->path : "/";
102 p += strspn(p, "/");
103 if (p[0]) {
104 qdict_put(options, "export", qstring_from_str(p));
105 }
106
107 qp = query_params_parse(uri->query);
108 if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
109 ret = -EINVAL;
110 goto out;
111 }
112
113 if (is_unix) {
114 /* nbd+unix:///export?socket=path */
115 if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
116 ret = -EINVAL;
117 goto out;
118 }
119 qdict_put(options, "path", qstring_from_str(qp->p[0].value));
120 } else {
121 /* nbd[+tcp]://host[:port]/export */
122 if (!uri->server) {
123 ret = -EINVAL;
124 goto out;
125 }
126
127 qdict_put(options, "host", qstring_from_str(uri->server));
128 if (uri->port) {
129 char* port_str = g_strdup_printf("%d", uri->port);
130 qdict_put(options, "port", qstring_from_str(port_str));
131 g_free(port_str);
132 }
133 }
134
135 out:
136 if (qp) {
137 query_params_free(qp);
138 }
139 uri_free(uri);
140 return ret;
141 }
142
143 static void nbd_parse_filename(const char *filename, QDict *options,
144 Error **errp)
145 {
146 char *file;
147 char *export_name;
148 const char *host_spec;
149 const char *unixpath;
150
151 if (qdict_haskey(options, "host")
152 || qdict_haskey(options, "port")
153 || qdict_haskey(options, "path"))
154 {
155 error_setg(errp, "host/port/path and a file name may not be specified "
156 "at the same time");
157 return;
158 }
159
160 if (strstr(filename, "://")) {
161 int ret = nbd_parse_uri(filename, options);
162 if (ret < 0) {
163 error_setg(errp, "No valid URL specified");
164 }
165 return;
166 }
167
168 file = g_strdup(filename);
169
170 export_name = strstr(file, EN_OPTSTR);
171 if (export_name) {
172 if (export_name[strlen(EN_OPTSTR)] == 0) {
173 goto out;
174 }
175 export_name[0] = 0; /* truncate 'file' */
176 export_name += strlen(EN_OPTSTR);
177
178 qdict_put(options, "export", qstring_from_str(export_name));
179 }
180
181 /* extract the host_spec - fail if it's not nbd:... */
182 if (!strstart(file, "nbd:", &host_spec)) {
183 error_setg(errp, "File name string for NBD must start with 'nbd:'");
184 goto out;
185 }
186
187 if (!*host_spec) {
188 goto out;
189 }
190
191 /* are we a UNIX or TCP socket? */
192 if (strstart(host_spec, "unix:", &unixpath)) {
193 qdict_put(options, "path", qstring_from_str(unixpath));
194 } else {
195 InetSocketAddress *addr = NULL;
196
197 addr = inet_parse(host_spec, errp);
198 if (error_is_set(errp)) {
199 goto out;
200 }
201
202 qdict_put(options, "host", qstring_from_str(addr->host));
203 qdict_put(options, "port", qstring_from_str(addr->port));
204 qapi_free_InetSocketAddress(addr);
205 }
206
207 out:
208 g_free(file);
209 }
210
211 static int nbd_config(BDRVNBDState *s, QDict *options)
212 {
213 Error *local_err = NULL;
214
215 if (qdict_haskey(options, "path")) {
216 if (qdict_haskey(options, "host")) {
217 qerror_report(ERROR_CLASS_GENERIC_ERROR, "path and host may not "
218 "be used at the same time.");
219 return -EINVAL;
220 }
221 s->is_unix = true;
222 } else if (qdict_haskey(options, "host")) {
223 s->is_unix = false;
224 } else {
225 return -EINVAL;
226 }
227
228 s->socket_opts = qemu_opts_create_nofail(&socket_optslist);
229
230 qemu_opts_absorb_qdict(s->socket_opts, options, &local_err);
231 if (error_is_set(&local_err)) {
232 qerror_report_err(local_err);
233 error_free(local_err);
234 return -EINVAL;
235 }
236
237 if (!qemu_opt_get(s->socket_opts, "port")) {
238 qemu_opt_set_number(s->socket_opts, "port", NBD_DEFAULT_PORT);
239 }
240
241 s->export_name = g_strdup(qdict_get_try_str(options, "export"));
242 if (s->export_name) {
243 qdict_del(options, "export");
244 }
245
246 return 0;
247 }
248
249
250 static void nbd_coroutine_start(BDRVNBDState *s, struct nbd_request *request)
251 {
252 int i;
253
254 /* Poor man semaphore. The free_sema is locked when no other request
255 * can be accepted, and unlocked after receiving one reply. */
256 if (s->in_flight >= MAX_NBD_REQUESTS - 1) {
257 qemu_co_mutex_lock(&s->free_sema);
258 assert(s->in_flight < MAX_NBD_REQUESTS);
259 }
260 s->in_flight++;
261
262 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
263 if (s->recv_coroutine[i] == NULL) {
264 s->recv_coroutine[i] = qemu_coroutine_self();
265 break;
266 }
267 }
268
269 assert(i < MAX_NBD_REQUESTS);
270 request->handle = INDEX_TO_HANDLE(s, i);
271 }
272
273 static int nbd_have_request(void *opaque)
274 {
275 BDRVNBDState *s = opaque;
276
277 return s->in_flight > 0;
278 }
279
280 static void nbd_reply_ready(void *opaque)
281 {
282 BDRVNBDState *s = opaque;
283 uint64_t i;
284 int ret;
285
286 if (s->reply.handle == 0) {
287 /* No reply already in flight. Fetch a header. It is possible
288 * that another thread has done the same thing in parallel, so
289 * the socket is not readable anymore.
290 */
291 ret = nbd_receive_reply(s->sock, &s->reply);
292 if (ret == -EAGAIN) {
293 return;
294 }
295 if (ret < 0) {
296 s->reply.handle = 0;
297 goto fail;
298 }
299 }
300
301 /* There's no need for a mutex on the receive side, because the
302 * handler acts as a synchronization point and ensures that only
303 * one coroutine is called until the reply finishes. */
304 i = HANDLE_TO_INDEX(s, s->reply.handle);
305 if (i >= MAX_NBD_REQUESTS) {
306 goto fail;
307 }
308
309 if (s->recv_coroutine[i]) {
310 qemu_coroutine_enter(s->recv_coroutine[i], NULL);
311 return;
312 }
313
314 fail:
315 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
316 if (s->recv_coroutine[i]) {
317 qemu_coroutine_enter(s->recv_coroutine[i], NULL);
318 }
319 }
320 }
321
322 static void nbd_restart_write(void *opaque)
323 {
324 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 int rc, ret;
332
333 qemu_co_mutex_lock(&s->send_mutex);
334 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 rc = nbd_send_request(s->sock, request);
338 if (rc >= 0 && qiov) {
339 ret = qemu_co_sendv(s->sock, qiov->iov, qiov->niov,
340 offset, request->len);
341 if (ret != request->len) {
342 return -EIO;
343 }
344 }
345 qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, NULL,
346 nbd_have_request, s);
347 s->send_coroutine = NULL;
348 qemu_co_mutex_unlock(&s->send_mutex);
349 return rc;
350 }
351
352 static void nbd_co_receive_reply(BDRVNBDState *s, struct nbd_request *request,
353 struct nbd_reply *reply,
354 QEMUIOVector *qiov, int offset)
355 {
356 int ret;
357
358 /* Wait until we're woken up by the read handler. TODO: perhaps
359 * peek at the next reply and avoid yielding if it's ours? */
360 qemu_coroutine_yield();
361 *reply = s->reply;
362 if (reply->handle != request->handle) {
363 reply->error = EIO;
364 } else {
365 if (qiov && reply->error == 0) {
366 ret = qemu_co_recvv(s->sock, qiov->iov, qiov->niov,
367 offset, request->len);
368 if (ret != request->len) {
369 reply->error = EIO;
370 }
371 }
372
373 /* Tell the read handler to read another header. */
374 s->reply.handle = 0;
375 }
376 }
377
378 static void nbd_coroutine_end(BDRVNBDState *s, struct nbd_request *request)
379 {
380 int i = HANDLE_TO_INDEX(s, request->handle);
381 s->recv_coroutine[i] = NULL;
382 if (s->in_flight-- == MAX_NBD_REQUESTS) {
383 qemu_co_mutex_unlock(&s->free_sema);
384 }
385 }
386
387 static int nbd_establish_connection(BlockDriverState *bs)
388 {
389 BDRVNBDState *s = bs->opaque;
390 int sock;
391 int ret;
392 off_t size;
393 size_t blocksize;
394
395 if (s->is_unix) {
396 sock = unix_socket_outgoing(qemu_opt_get(s->socket_opts, "path"));
397 } else {
398 sock = tcp_socket_outgoing_opts(s->socket_opts);
399 }
400
401 /* Failed to establish connection */
402 if (sock < 0) {
403 logout("Failed to establish connection to NBD server\n");
404 return -errno;
405 }
406
407 /* NBD handshake */
408 ret = nbd_receive_negotiate(sock, s->export_name, &s->nbdflags, &size,
409 &blocksize);
410 if (ret < 0) {
411 logout("Failed to negotiate with the NBD server\n");
412 closesocket(sock);
413 return ret;
414 }
415
416 /* Now that we're connected, set the socket to be non-blocking and
417 * kick the reply mechanism. */
418 qemu_set_nonblock(sock);
419 qemu_aio_set_fd_handler(sock, nbd_reply_ready, NULL,
420 nbd_have_request, s);
421
422 s->sock = sock;
423 s->size = size;
424 s->blocksize = blocksize;
425
426 logout("Established connection with NBD server\n");
427 return 0;
428 }
429
430 static void nbd_teardown_connection(BlockDriverState *bs)
431 {
432 BDRVNBDState *s = bs->opaque;
433 struct nbd_request request;
434
435 request.type = NBD_CMD_DISC;
436 request.from = 0;
437 request.len = 0;
438 nbd_send_request(s->sock, &request);
439
440 qemu_aio_set_fd_handler(s->sock, NULL, NULL, NULL, NULL);
441 closesocket(s->sock);
442 }
443
444 static int nbd_open(BlockDriverState *bs, const char* filename,
445 QDict *options, int flags)
446 {
447 BDRVNBDState *s = bs->opaque;
448 int result;
449
450 qemu_co_mutex_init(&s->send_mutex);
451 qemu_co_mutex_init(&s->free_sema);
452
453 /* Pop the config into our state object. Exit if invalid. */
454 result = nbd_config(s, options);
455 if (result != 0) {
456 return result;
457 }
458
459 /* establish TCP connection, return error if it fails
460 * TODO: Configurable retry-until-timeout behaviour.
461 */
462 result = nbd_establish_connection(bs);
463
464 return result;
465 }
466
467 static int nbd_co_readv_1(BlockDriverState *bs, int64_t sector_num,
468 int nb_sectors, QEMUIOVector *qiov,
469 int offset)
470 {
471 BDRVNBDState *s = bs->opaque;
472 struct nbd_request request;
473 struct nbd_reply reply;
474 ssize_t ret;
475
476 request.type = NBD_CMD_READ;
477 request.from = sector_num * 512;
478 request.len = nb_sectors * 512;
479
480 nbd_coroutine_start(s, &request);
481 ret = nbd_co_send_request(s, &request, NULL, 0);
482 if (ret < 0) {
483 reply.error = -ret;
484 } else {
485 nbd_co_receive_reply(s, &request, &reply, qiov, offset);
486 }
487 nbd_coroutine_end(s, &request);
488 return -reply.error;
489
490 }
491
492 static int nbd_co_writev_1(BlockDriverState *bs, int64_t sector_num,
493 int nb_sectors, QEMUIOVector *qiov,
494 int offset)
495 {
496 BDRVNBDState *s = bs->opaque;
497 struct nbd_request request;
498 struct nbd_reply reply;
499 ssize_t ret;
500
501 request.type = NBD_CMD_WRITE;
502 if (!bdrv_enable_write_cache(bs) && (s->nbdflags & NBD_FLAG_SEND_FUA)) {
503 request.type |= NBD_CMD_FLAG_FUA;
504 }
505
506 request.from = sector_num * 512;
507 request.len = nb_sectors * 512;
508
509 nbd_coroutine_start(s, &request);
510 ret = nbd_co_send_request(s, &request, qiov, offset);
511 if (ret < 0) {
512 reply.error = -ret;
513 } else {
514 nbd_co_receive_reply(s, &request, &reply, NULL, 0);
515 }
516 nbd_coroutine_end(s, &request);
517 return -reply.error;
518 }
519
520 /* qemu-nbd has a limit of slightly less than 1M per request. Try to
521 * remain aligned to 4K. */
522 #define NBD_MAX_SECTORS 2040
523
524 static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num,
525 int nb_sectors, QEMUIOVector *qiov)
526 {
527 int offset = 0;
528 int ret;
529 while (nb_sectors > NBD_MAX_SECTORS) {
530 ret = nbd_co_readv_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset);
531 if (ret < 0) {
532 return ret;
533 }
534 offset += NBD_MAX_SECTORS * 512;
535 sector_num += NBD_MAX_SECTORS;
536 nb_sectors -= NBD_MAX_SECTORS;
537 }
538 return nbd_co_readv_1(bs, sector_num, nb_sectors, qiov, offset);
539 }
540
541 static int nbd_co_writev(BlockDriverState *bs, int64_t sector_num,
542 int nb_sectors, QEMUIOVector *qiov)
543 {
544 int offset = 0;
545 int ret;
546 while (nb_sectors > NBD_MAX_SECTORS) {
547 ret = nbd_co_writev_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset);
548 if (ret < 0) {
549 return ret;
550 }
551 offset += NBD_MAX_SECTORS * 512;
552 sector_num += NBD_MAX_SECTORS;
553 nb_sectors -= NBD_MAX_SECTORS;
554 }
555 return nbd_co_writev_1(bs, sector_num, nb_sectors, qiov, offset);
556 }
557
558 static int nbd_co_flush(BlockDriverState *bs)
559 {
560 BDRVNBDState *s = bs->opaque;
561 struct nbd_request request;
562 struct nbd_reply reply;
563 ssize_t ret;
564
565 if (!(s->nbdflags & NBD_FLAG_SEND_FLUSH)) {
566 return 0;
567 }
568
569 request.type = NBD_CMD_FLUSH;
570 if (s->nbdflags & NBD_FLAG_SEND_FUA) {
571 request.type |= NBD_CMD_FLAG_FUA;
572 }
573
574 request.from = 0;
575 request.len = 0;
576
577 nbd_coroutine_start(s, &request);
578 ret = nbd_co_send_request(s, &request, NULL, 0);
579 if (ret < 0) {
580 reply.error = -ret;
581 } else {
582 nbd_co_receive_reply(s, &request, &reply, NULL, 0);
583 }
584 nbd_coroutine_end(s, &request);
585 return -reply.error;
586 }
587
588 static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num,
589 int nb_sectors)
590 {
591 BDRVNBDState *s = bs->opaque;
592 struct nbd_request request;
593 struct nbd_reply reply;
594 ssize_t ret;
595
596 if (!(s->nbdflags & NBD_FLAG_SEND_TRIM)) {
597 return 0;
598 }
599 request.type = NBD_CMD_TRIM;
600 request.from = sector_num * 512;;
601 request.len = nb_sectors * 512;
602
603 nbd_coroutine_start(s, &request);
604 ret = nbd_co_send_request(s, &request, NULL, 0);
605 if (ret < 0) {
606 reply.error = -ret;
607 } else {
608 nbd_co_receive_reply(s, &request, &reply, NULL, 0);
609 }
610 nbd_coroutine_end(s, &request);
611 return -reply.error;
612 }
613
614 static void nbd_close(BlockDriverState *bs)
615 {
616 BDRVNBDState *s = bs->opaque;
617 g_free(s->export_name);
618 qemu_opts_del(s->socket_opts);
619
620 nbd_teardown_connection(bs);
621 }
622
623 static int64_t nbd_getlength(BlockDriverState *bs)
624 {
625 BDRVNBDState *s = bs->opaque;
626
627 return s->size;
628 }
629
630 static BlockDriver bdrv_nbd = {
631 .format_name = "nbd",
632 .protocol_name = "nbd",
633 .instance_size = sizeof(BDRVNBDState),
634 .bdrv_parse_filename = nbd_parse_filename,
635 .bdrv_file_open = nbd_open,
636 .bdrv_co_readv = nbd_co_readv,
637 .bdrv_co_writev = nbd_co_writev,
638 .bdrv_close = nbd_close,
639 .bdrv_co_flush_to_os = nbd_co_flush,
640 .bdrv_co_discard = nbd_co_discard,
641 .bdrv_getlength = nbd_getlength,
642 };
643
644 static BlockDriver bdrv_nbd_tcp = {
645 .format_name = "nbd",
646 .protocol_name = "nbd+tcp",
647 .instance_size = sizeof(BDRVNBDState),
648 .bdrv_parse_filename = nbd_parse_filename,
649 .bdrv_file_open = nbd_open,
650 .bdrv_co_readv = nbd_co_readv,
651 .bdrv_co_writev = nbd_co_writev,
652 .bdrv_close = nbd_close,
653 .bdrv_co_flush_to_os = nbd_co_flush,
654 .bdrv_co_discard = nbd_co_discard,
655 .bdrv_getlength = nbd_getlength,
656 };
657
658 static BlockDriver bdrv_nbd_unix = {
659 .format_name = "nbd",
660 .protocol_name = "nbd+unix",
661 .instance_size = sizeof(BDRVNBDState),
662 .bdrv_parse_filename = nbd_parse_filename,
663 .bdrv_file_open = nbd_open,
664 .bdrv_co_readv = nbd_co_readv,
665 .bdrv_co_writev = nbd_co_writev,
666 .bdrv_close = nbd_close,
667 .bdrv_co_flush_to_os = nbd_co_flush,
668 .bdrv_co_discard = nbd_co_discard,
669 .bdrv_getlength = nbd_getlength,
670 };
671
672 static void bdrv_nbd_init(void)
673 {
674 bdrv_register(&bdrv_nbd);
675 bdrv_register(&bdrv_nbd_tcp);
676 bdrv_register(&bdrv_nbd_unix);
677 }
678
679 block_init(bdrv_nbd_init);