xref: /openbmc/qemu/blockdev-nbd.c (revision b45c03f5)
1 /*
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 
12 #include "sysemu/blockdev.h"
13 #include "sysemu/block-backend.h"
14 #include "hw/block/block.h"
15 #include "qapi/qmp/qerror.h"
16 #include "sysemu/sysemu.h"
17 #include "qmp-commands.h"
18 #include "trace.h"
19 #include "block/nbd.h"
20 #include "qemu/sockets.h"
21 
22 static int server_fd = -1;
23 
24 static 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);
30     if (fd >= 0 && !nbd_client_new(NULL, fd, nbd_client_put)) {
31         shutdown(fd, 2);
32         close(fd);
33     }
34 }
35 
36 void 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) {
45         qemu_set_fd_handler(server_fd, nbd_accept, NULL, NULL);
46     }
47 }
48 
49 /*
50  * Hook into the BlockBackend notifiers to close the export when the
51  * backend is closed.
52  */
53 typedef struct NBDCloseNotifier {
54     Notifier n;
55     NBDExport *exp;
56     QTAILQ_ENTRY(NBDCloseNotifier) next;
57 } NBDCloseNotifier;
58 
59 static QTAILQ_HEAD(, NBDCloseNotifier) close_notifiers =
60     QTAILQ_HEAD_INITIALIZER(close_notifiers);
61 
62 static 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 
74 void qmp_nbd_server_add(const char *device, bool has_writable, bool writable,
75                         Error **errp)
76 {
77     BlockBackend *blk;
78     NBDExport *exp;
79     NBDCloseNotifier *n;
80 
81     if (server_fd == -1) {
82         error_setg(errp, "NBD server not running");
83         return;
84     }
85 
86     if (nbd_export_find(device)) {
87         error_setg(errp, "NBD server already exporting device '%s'", device);
88         return;
89     }
90 
91     blk = blk_by_name(device);
92     if (!blk) {
93         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
94                   "Device '%s' not found", device);
95         return;
96     }
97     if (!blk_is_inserted(blk)) {
98         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
99         return;
100     }
101 
102     if (!has_writable) {
103         writable = false;
104     }
105     if (blk_is_read_only(blk)) {
106         writable = false;
107     }
108 
109     exp = nbd_export_new(blk, 0, -1, writable ? 0 : NBD_FLAG_READ_ONLY, NULL,
110                          errp);
111     if (!exp) {
112         return;
113     }
114 
115     nbd_export_set_name(exp, device);
116 
117     n = g_new0(NBDCloseNotifier, 1);
118     n->n.notify = nbd_close_notifier;
119     n->exp = exp;
120     blk_add_close_notifier(blk, &n->n);
121     QTAILQ_INSERT_TAIL(&close_notifiers, n, next);
122 }
123 
124 void 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 
131     if (server_fd != -1) {
132         qemu_set_fd_handler(server_fd, NULL, NULL, NULL);
133         close(server_fd);
134         server_fd = -1;
135     }
136 }
137