blob: bcdd18b3f61620e39035fb798004b97fc8638b59 [file] [log] [blame]
Paolo Bonzini6dd844d2012-08-22 16:43:07 +02001/*
2 * Serving QEMU block devices via NBD
3 *
4 * Copyright (c) 2012 Red Hat, Inc.
5 *
6 * Author: Paolo Bonzini <pbonzini@redhat.com>
7 *
8 * This work is licensed under the terms of the GNU GPL, version 2 or
9 * later. See the COPYING file in the top-level directory.
10 */
11
Paolo Bonzini9c17d612012-12-17 18:20:04 +010012#include "sysemu/blockdev.h"
Max Reitze1401772014-11-18 12:21:17 +010013#include "sysemu/block-backend.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010014#include "hw/block/block.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010015#include "qapi/qmp/qerror.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010016#include "sysemu/sysemu.h"
Paolo Bonzini6dd844d2012-08-22 16:43:07 +020017#include "qmp-commands.h"
18#include "trace.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010019#include "block/nbd.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010020#include "qemu/sockets.h"
Paolo Bonzini6dd844d2012-08-22 16:43:07 +020021
22static int server_fd = -1;
23
24static void nbd_accept(void *opaque)
25{
26 struct sockaddr_in addr;
27 socklen_t addr_len = sizeof(addr);
28
29 int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
Hani Benhabiles36af5992014-05-13 00:35:15 +010030 if (fd >= 0 && !nbd_client_new(NULL, fd, nbd_client_put)) {
Hani Benhabiles27e5eae2014-05-31 22:39:42 +010031 shutdown(fd, 2);
Hani Benhabiles36af5992014-05-13 00:35:15 +010032 close(fd);
Paolo Bonzini6dd844d2012-08-22 16:43:07 +020033 }
34}
35
36void qmp_nbd_server_start(SocketAddress *addr, Error **errp)
37{
38 if (server_fd != -1) {
39 error_setg(errp, "NBD server already running");
40 return;
41 }
42
43 server_fd = socket_listen(addr, errp);
44 if (server_fd != -1) {
Fam Zheng82e1cc42015-06-04 14:45:18 +080045 qemu_set_fd_handler(server_fd, nbd_accept, NULL, NULL);
Paolo Bonzini6dd844d2012-08-22 16:43:07 +020046 }
47}
48
Markus Armbruster78fa62f2015-03-25 09:18:48 +010049/*
50 * Hook into the BlockBackend notifiers to close the export when the
51 * backend is closed.
Paolo Bonzini6dd844d2012-08-22 16:43:07 +020052 */
53typedef struct NBDCloseNotifier {
54 Notifier n;
55 NBDExport *exp;
56 QTAILQ_ENTRY(NBDCloseNotifier) next;
57} NBDCloseNotifier;
58
59static QTAILQ_HEAD(, NBDCloseNotifier) close_notifiers =
60 QTAILQ_HEAD_INITIALIZER(close_notifiers);
61
62static void nbd_close_notifier(Notifier *n, void *data)
63{
64 NBDCloseNotifier *cn = DO_UPCAST(NBDCloseNotifier, n, n);
65
66 notifier_remove(&cn->n);
67 QTAILQ_REMOVE(&close_notifiers, cn, next);
68
69 nbd_export_close(cn->exp);
70 nbd_export_put(cn->exp);
71 g_free(cn);
72}
73
Paolo Bonzini6dd844d2012-08-22 16:43:07 +020074void qmp_nbd_server_add(const char *device, bool has_writable, bool writable,
75 Error **errp)
76{
Max Reitze1401772014-11-18 12:21:17 +010077 BlockBackend *blk;
Paolo Bonzini6dd844d2012-08-22 16:43:07 +020078 NBDExport *exp;
79 NBDCloseNotifier *n;
80
Paolo Bonzini17b6be42012-11-12 14:25:17 +010081 if (server_fd == -1) {
82 error_setg(errp, "NBD server not running");
83 return;
84 }
85
Paolo Bonzini6dd844d2012-08-22 16:43:07 +020086 if (nbd_export_find(device)) {
87 error_setg(errp, "NBD server already exporting device '%s'", device);
88 return;
89 }
90
Max Reitze1401772014-11-18 12:21:17 +010091 blk = blk_by_name(device);
92 if (!blk) {
Markus Armbruster75158eb2015-03-16 08:57:47 +010093 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
94 "Device '%s' not found", device);
Paolo Bonzini6dd844d2012-08-22 16:43:07 +020095 return;
96 }
Max Reitze1401772014-11-18 12:21:17 +010097 if (!blk_is_inserted(blk)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +010098 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Hani Benhabiles60fe4fa2014-05-18 11:50:04 +010099 return;
100 }
Paolo Bonzini6dd844d2012-08-22 16:43:07 +0200101
Paolo Bonzinie6444732012-11-04 12:43:37 +0100102 if (!has_writable) {
Michal Privoznikf3313d22012-11-28 11:46:39 +0100103 writable = false;
Paolo Bonzinie6444732012-11-04 12:43:37 +0100104 }
Max Reitze1401772014-11-18 12:21:17 +0100105 if (blk_is_read_only(blk)) {
Paolo Bonzinie6444732012-11-04 12:43:37 +0100106 writable = false;
107 }
108
Max Reitz98f44bb2015-02-25 13:08:21 -0500109 exp = nbd_export_new(blk, 0, -1, writable ? 0 : NBD_FLAG_READ_ONLY, NULL,
110 errp);
111 if (!exp) {
112 return;
113 }
Paolo Bonzini6dd844d2012-08-22 16:43:07 +0200114
115 nbd_export_set_name(exp, device);
Paolo Bonzini6dd844d2012-08-22 16:43:07 +0200116
Markus Armbruster5839e532014-08-19 10:31:08 +0200117 n = g_new0(NBDCloseNotifier, 1);
Paolo Bonzini6dd844d2012-08-22 16:43:07 +0200118 n->n.notify = nbd_close_notifier;
119 n->exp = exp;
Max Reitze1401772014-11-18 12:21:17 +0100120 blk_add_close_notifier(blk, &n->n);
Paolo Bonzini6dd844d2012-08-22 16:43:07 +0200121 QTAILQ_INSERT_TAIL(&close_notifiers, n, next);
122}
123
124void qmp_nbd_server_stop(Error **errp)
125{
126 while (!QTAILQ_EMPTY(&close_notifiers)) {
127 NBDCloseNotifier *cn = QTAILQ_FIRST(&close_notifiers);
128 nbd_close_notifier(&cn->n, nbd_export_get_blockdev(cn->exp));
129 }
130
Paolo Bonzinifc6467e2012-11-12 14:12:54 +0100131 if (server_fd != -1) {
Fam Zheng82e1cc42015-06-04 14:45:18 +0800132 qemu_set_fd_handler(server_fd, NULL, NULL, NULL);
Paolo Bonzinifc6467e2012-11-12 14:12:54 +0100133 close(server_fd);
134 server_fd = -1;
135 }
Paolo Bonzini6dd844d2012-08-22 16:43:07 +0200136}