]> git.proxmox.com Git - mirror_qemu.git/blame - blockdev-nbd.c
nbd: Always call "close_fn" in nbd_client_new
[mirror_qemu.git] / blockdev-nbd.c
CommitLineData
6dd844db
PB
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
9c17d615 12#include "sysemu/blockdev.h"
e140177d 13#include "sysemu/block-backend.h"
0d09e41a 14#include "hw/block/block.h"
7b1b5d19 15#include "qapi/qmp/qerror.h"
9c17d615 16#include "sysemu/sysemu.h"
6dd844db
PB
17#include "qmp-commands.h"
18#include "trace.h"
737e150e 19#include "block/nbd.h"
1de7afc9 20#include "qemu/sockets.h"
6dd844db
PB
21
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);
ee7d7aab
FZ
30 if (fd >= 0) {
31 nbd_client_new(NULL, fd, nbd_client_put);
6dd844db
PB
32 }
33}
34
35void qmp_nbd_server_start(SocketAddress *addr, Error **errp)
36{
37 if (server_fd != -1) {
38 error_setg(errp, "NBD server already running");
39 return;
40 }
41
42 server_fd = socket_listen(addr, errp);
43 if (server_fd != -1) {
82e1cc4b 44 qemu_set_fd_handler(server_fd, nbd_accept, NULL, NULL);
6dd844db
PB
45 }
46}
47
78fa62ff
MA
48/*
49 * Hook into the BlockBackend notifiers to close the export when the
50 * backend is closed.
6dd844db
PB
51 */
52typedef struct NBDCloseNotifier {
53 Notifier n;
54 NBDExport *exp;
55 QTAILQ_ENTRY(NBDCloseNotifier) next;
56} NBDCloseNotifier;
57
58static QTAILQ_HEAD(, NBDCloseNotifier) close_notifiers =
59 QTAILQ_HEAD_INITIALIZER(close_notifiers);
60
61static 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
6dd844db
PB
73void qmp_nbd_server_add(const char *device, bool has_writable, bool writable,
74 Error **errp)
75{
e140177d 76 BlockBackend *blk;
6dd844db
PB
77 NBDExport *exp;
78 NBDCloseNotifier *n;
79
17b6be4a
PB
80 if (server_fd == -1) {
81 error_setg(errp, "NBD server not running");
82 return;
83 }
84
6dd844db
PB
85 if (nbd_export_find(device)) {
86 error_setg(errp, "NBD server already exporting device '%s'", device);
87 return;
88 }
89
e140177d
HR
90 blk = blk_by_name(device);
91 if (!blk) {
75158ebb
MA
92 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
93 "Device '%s' not found", device);
6dd844db
PB
94 return;
95 }
e140177d 96 if (!blk_is_inserted(blk)) {
c6bd8c70 97 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
60fe4fac
HB
98 return;
99 }
6dd844db 100
e6444734 101 if (!has_writable) {
f3313d23 102 writable = false;
e6444734 103 }
e140177d 104 if (blk_is_read_only(blk)) {
e6444734
PB
105 writable = false;
106 }
107
98f44bbe
HR
108 exp = nbd_export_new(blk, 0, -1, writable ? 0 : NBD_FLAG_READ_ONLY, NULL,
109 errp);
110 if (!exp) {
111 return;
112 }
6dd844db
PB
113
114 nbd_export_set_name(exp, device);
6dd844db 115
5839e53b 116 n = g_new0(NBDCloseNotifier, 1);
6dd844db
PB
117 n->n.notify = nbd_close_notifier;
118 n->exp = exp;
e140177d 119 blk_add_close_notifier(blk, &n->n);
6dd844db
PB
120 QTAILQ_INSERT_TAIL(&close_notifiers, n, next);
121}
122
123void qmp_nbd_server_stop(Error **errp)
124{
125 while (!QTAILQ_EMPTY(&close_notifiers)) {
126 NBDCloseNotifier *cn = QTAILQ_FIRST(&close_notifiers);
127 nbd_close_notifier(&cn->n, nbd_export_get_blockdev(cn->exp));
128 }
129
fc6467ea 130 if (server_fd != -1) {
82e1cc4b 131 qemu_set_fd_handler(server_fd, NULL, NULL, NULL);
fc6467ea
PB
132 close(server_fd);
133 server_fd = -1;
134 }
6dd844db 135}