xref: /qemu/blockdev-nbd.c (revision d072cdf3)
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 "hw/block/block.h"
14 #include "monitor/monitor.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_handler2(server_fd, NULL, nbd_accept, NULL, NULL);
46     }
47 }
48 
49 /* Hook into the BlockDriverState notifiers to close the export when
50  * the file is closed.
51  */
52 typedef struct NBDCloseNotifier {
53     Notifier n;
54     NBDExport *exp;
55     QTAILQ_ENTRY(NBDCloseNotifier) next;
56 } NBDCloseNotifier;
57 
58 static QTAILQ_HEAD(, NBDCloseNotifier) close_notifiers =
59     QTAILQ_HEAD_INITIALIZER(close_notifiers);
60 
61 static void nbd_close_notifier(Notifier *n, void *data)
62 {
63     NBDCloseNotifier *cn = DO_UPCAST(NBDCloseNotifier, n, n);
64 
65     notifier_remove(&cn->n);
66     QTAILQ_REMOVE(&close_notifiers, cn, next);
67 
68     nbd_export_close(cn->exp);
69     nbd_export_put(cn->exp);
70     g_free(cn);
71 }
72 
73 void qmp_nbd_server_add(const char *device, bool has_writable, bool writable,
74                         Error **errp)
75 {
76     BlockDriverState *bs;
77     NBDExport *exp;
78     NBDCloseNotifier *n;
79 
80     if (server_fd == -1) {
81         error_setg(errp, "NBD server not running");
82         return;
83     }
84 
85     if (nbd_export_find(device)) {
86         error_setg(errp, "NBD server already exporting device '%s'", device);
87         return;
88     }
89 
90     bs = bdrv_find(device);
91     if (!bs) {
92         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
93         return;
94     }
95     if (!bdrv_is_inserted(bs)) {
96         error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
97         return;
98     }
99 
100     if (!has_writable) {
101         writable = false;
102     }
103     if (bdrv_is_read_only(bs)) {
104         writable = false;
105     }
106 
107     exp = nbd_export_new(bs, 0, -1, writable ? 0 : NBD_FLAG_READ_ONLY, NULL);
108 
109     nbd_export_set_name(exp, device);
110 
111     n = g_new0(NBDCloseNotifier, 1);
112     n->n.notify = nbd_close_notifier;
113     n->exp = exp;
114     bdrv_add_close_notifier(bs, &n->n);
115     QTAILQ_INSERT_TAIL(&close_notifiers, n, next);
116 }
117 
118 void qmp_nbd_server_stop(Error **errp)
119 {
120     while (!QTAILQ_EMPTY(&close_notifiers)) {
121         NBDCloseNotifier *cn = QTAILQ_FIRST(&close_notifiers);
122         nbd_close_notifier(&cn->n, nbd_export_get_blockdev(cn->exp));
123     }
124 
125     if (server_fd != -1) {
126         qemu_set_fd_handler2(server_fd, NULL, NULL, NULL, NULL);
127         close(server_fd);
128         server_fd = -1;
129     }
130 }
131