]> git.proxmox.com Git - mirror_qemu.git/blame - blockdev-nbd.c
kvm-all: Sync dirty-bitmap from kvm before kvm destroy the corresponding dirty_bitmap
[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"
83c9089e 15#include "monitor/monitor.h"
7b1b5d19 16#include "qapi/qmp/qerror.h"
9c17d615 17#include "sysemu/sysemu.h"
6dd844db
PB
18#include "qmp-commands.h"
19#include "trace.h"
737e150e 20#include "block/nbd.h"
1de7afc9 21#include "qemu/sockets.h"
6dd844db
PB
22
23static int server_fd = -1;
24
25static 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);
36af5994 31 if (fd >= 0 && !nbd_client_new(NULL, fd, nbd_client_put)) {
27e5eae4 32 shutdown(fd, 2);
36af5994 33 close(fd);
6dd844db
PB
34 }
35}
36
37void 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_handler2(server_fd, NULL, nbd_accept, NULL, NULL);
47 }
48}
49
78fa62ff
MA
50/*
51 * Hook into the BlockBackend notifiers to close the export when the
52 * backend is closed.
6dd844db
PB
53 */
54typedef struct NBDCloseNotifier {
55 Notifier n;
56 NBDExport *exp;
57 QTAILQ_ENTRY(NBDCloseNotifier) next;
58} NBDCloseNotifier;
59
60static QTAILQ_HEAD(, NBDCloseNotifier) close_notifiers =
61 QTAILQ_HEAD_INITIALIZER(close_notifiers);
62
63static 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
6dd844db
PB
75void qmp_nbd_server_add(const char *device, bool has_writable, bool writable,
76 Error **errp)
77{
e140177d 78 BlockBackend *blk;
6dd844db
PB
79 NBDExport *exp;
80 NBDCloseNotifier *n;
81
17b6be4a
PB
82 if (server_fd == -1) {
83 error_setg(errp, "NBD server not running");
84 return;
85 }
86
6dd844db
PB
87 if (nbd_export_find(device)) {
88 error_setg(errp, "NBD server already exporting device '%s'", device);
89 return;
90 }
91
e140177d
HR
92 blk = blk_by_name(device);
93 if (!blk) {
6dd844db
PB
94 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
95 return;
96 }
e140177d 97 if (!blk_is_inserted(blk)) {
60fe4fac
HB
98 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
99 return;
100 }
6dd844db 101
e6444734 102 if (!has_writable) {
f3313d23 103 writable = false;
e6444734 104 }
e140177d 105 if (blk_is_read_only(blk)) {
e6444734
PB
106 writable = false;
107 }
108
98f44bbe
HR
109 exp = nbd_export_new(blk, 0, -1, writable ? 0 : NBD_FLAG_READ_ONLY, NULL,
110 errp);
111 if (!exp) {
112 return;
113 }
6dd844db
PB
114
115 nbd_export_set_name(exp, device);
6dd844db 116
5839e53b 117 n = g_new0(NBDCloseNotifier, 1);
6dd844db
PB
118 n->n.notify = nbd_close_notifier;
119 n->exp = exp;
e140177d 120 blk_add_close_notifier(blk, &n->n);
6dd844db
PB
121 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
fc6467ea
PB
131 if (server_fd != -1) {
132 qemu_set_fd_handler2(server_fd, NULL, NULL, NULL, NULL);
133 close(server_fd);
134 server_fd = -1;
135 }
6dd844db 136}