Revert "Merge remote-tracking branch 'remotes/ehabkost/tags/x86-pull-request' into...
[qemu/qmp-unstable.git] / blockdev-nbd.c
blob22e95d17ee809f167df4a267acab6883b397b163
1 /*
2 * Serving QEMU block devices via NBD
4 * Copyright (c) 2012 Red Hat, Inc.
6 * Author: Paolo Bonzini <pbonzini@redhat.com>
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.
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"
23 static int server_fd = -1;
25 static void nbd_accept(void *opaque)
27 struct sockaddr_in addr;
28 socklen_t addr_len = sizeof(addr);
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);
37 void qmp_nbd_server_start(SocketAddress *addr, Error **errp)
39 if (server_fd != -1) {
40 error_setg(errp, "NBD server already running");
41 return;
44 server_fd = socket_listen(addr, errp);
45 if (server_fd != -1) {
46 qemu_set_fd_handler2(server_fd, NULL, nbd_accept, NULL, NULL);
50 /* Hook into the BlockDriverState notifiers to close the export when
51 * the file is closed.
53 typedef struct NBDCloseNotifier {
54 Notifier n;
55 NBDExport *exp;
56 QTAILQ_ENTRY(NBDCloseNotifier) next;
57 } NBDCloseNotifier;
59 static QTAILQ_HEAD(, NBDCloseNotifier) close_notifiers =
60 QTAILQ_HEAD_INITIALIZER(close_notifiers);
62 static void nbd_close_notifier(Notifier *n, void *data)
64 NBDCloseNotifier *cn = DO_UPCAST(NBDCloseNotifier, n, n);
66 notifier_remove(&cn->n);
67 QTAILQ_REMOVE(&close_notifiers, cn, next);
69 nbd_export_close(cn->exp);
70 nbd_export_put(cn->exp);
71 g_free(cn);
74 void qmp_nbd_server_add(const char *device, bool has_writable, bool writable,
75 Error **errp)
77 BlockBackend *blk;
78 NBDExport *exp;
79 NBDCloseNotifier *n;
81 if (server_fd == -1) {
82 error_setg(errp, "NBD server not running");
83 return;
86 if (nbd_export_find(device)) {
87 error_setg(errp, "NBD server already exporting device '%s'", device);
88 return;
91 blk = blk_by_name(device);
92 if (!blk) {
93 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
94 return;
96 if (!blk_is_inserted(blk)) {
97 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
98 return;
101 if (!has_writable) {
102 writable = false;
104 if (blk_is_read_only(blk)) {
105 writable = false;
108 exp = nbd_export_new(blk, 0, -1, writable ? 0 : NBD_FLAG_READ_ONLY, NULL);
110 nbd_export_set_name(exp, device);
112 n = g_new0(NBDCloseNotifier, 1);
113 n->n.notify = nbd_close_notifier;
114 n->exp = exp;
115 blk_add_close_notifier(blk, &n->n);
116 QTAILQ_INSERT_TAIL(&close_notifiers, n, next);
119 void qmp_nbd_server_stop(Error **errp)
121 while (!QTAILQ_EMPTY(&close_notifiers)) {
122 NBDCloseNotifier *cn = QTAILQ_FIRST(&close_notifiers);
123 nbd_close_notifier(&cn->n, nbd_export_get_blockdev(cn->exp));
126 if (server_fd != -1) {
127 qemu_set_fd_handler2(server_fd, NULL, NULL, NULL, NULL);
128 close(server_fd);
129 server_fd = -1;