xref: /qemu/blockdev-nbd.c (revision 4629ed1e)
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 "monitor/monitor.h"
16 #include "qapi/qmp/qerror.h"
17 #include "sysemu/sysemu.h"
18 #include "qmp-commands.h"
19 #include "trace.h"
20 #include "block/nbd.h"
21 #include "qemu/sockets.h"
22 
23 static int server_fd = -1;
24 
25 static void nbd_accept(void *opaque)
26 {
27     struct sockaddr_in addr;
28     socklen_t addr_len = sizeof(addr);
29 
30     int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
31     if (fd >= 0 && !nbd_client_new(NULL, fd, nbd_client_put)) {
32         shutdown(fd, 2);
33         close(fd);
34     }
35 }
36 
37 void qmp_nbd_server_start(SocketAddress *addr, Error **errp)
38 {
39     if (server_fd != -1) {
40         error_setg(errp, "NBD server already running");
41         return;
42     }
43 
44     server_fd = socket_listen(addr, errp);
45     if (server_fd != -1) {
46         qemu_set_fd_handler(server_fd, nbd_accept, NULL, NULL);
47     }
48 }
49 
50 /*
51  * Hook into the BlockBackend notifiers to close the export when the
52  * backend is closed.
53  */
54 typedef struct NBDCloseNotifier {
55     Notifier n;
56     NBDExport *exp;
57     QTAILQ_ENTRY(NBDCloseNotifier) next;
58 } NBDCloseNotifier;
59 
60 static QTAILQ_HEAD(, NBDCloseNotifier) close_notifiers =
61     QTAILQ_HEAD_INITIALIZER(close_notifiers);
62 
63 static void nbd_close_notifier(Notifier *n, void *data)
64 {
65     NBDCloseNotifier *cn = DO_UPCAST(NBDCloseNotifier, n, n);
66 
67     notifier_remove(&cn->n);
68     QTAILQ_REMOVE(&close_notifiers, cn, next);
69 
70     nbd_export_close(cn->exp);
71     nbd_export_put(cn->exp);
72     g_free(cn);
73 }
74 
75 void qmp_nbd_server_add(const char *device, bool has_writable, bool writable,
76                         Error **errp)
77 {
78     BlockBackend *blk;
79     NBDExport *exp;
80     NBDCloseNotifier *n;
81 
82     if (server_fd == -1) {
83         error_setg(errp, "NBD server not running");
84         return;
85     }
86 
87     if (nbd_export_find(device)) {
88         error_setg(errp, "NBD server already exporting device '%s'", device);
89         return;
90     }
91 
92     blk = blk_by_name(device);
93     if (!blk) {
94         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
95                   "Device '%s' not found", device);
96         return;
97     }
98     if (!blk_is_inserted(blk)) {
99         error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
100         return;
101     }
102 
103     if (!has_writable) {
104         writable = false;
105     }
106     if (blk_is_read_only(blk)) {
107         writable = false;
108     }
109 
110     exp = nbd_export_new(blk, 0, -1, writable ? 0 : NBD_FLAG_READ_ONLY, NULL,
111                          errp);
112     if (!exp) {
113         return;
114     }
115 
116     nbd_export_set_name(exp, device);
117 
118     n = g_new0(NBDCloseNotifier, 1);
119     n->n.notify = nbd_close_notifier;
120     n->exp = exp;
121     blk_add_close_notifier(blk, &n->n);
122     QTAILQ_INSERT_TAIL(&close_notifiers, n, next);
123 }
124 
125 void qmp_nbd_server_stop(Error **errp)
126 {
127     while (!QTAILQ_EMPTY(&close_notifiers)) {
128         NBDCloseNotifier *cn = QTAILQ_FIRST(&close_notifiers);
129         nbd_close_notifier(&cn->n, nbd_export_get_blockdev(cn->exp));
130     }
131 
132     if (server_fd != -1) {
133         qemu_set_fd_handler(server_fd, NULL, NULL, NULL);
134         close(server_fd);
135         server_fd = -1;
136     }
137 }
138