Paolo Bonzini | 6dd844d | 2012-08-22 16:43:07 +0200 | [diff] [blame] | 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 | |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 12 | #include "sysemu/blockdev.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 13 | #include "hw/block/block.h" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 14 | #include "monitor/monitor.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 15 | #include "qapi/qmp/qerror.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 16 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 6dd844d | 2012-08-22 16:43:07 +0200 | [diff] [blame] | 17 | #include "qmp-commands.h" |
| 18 | #include "trace.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 19 | #include "block/nbd.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 20 | #include "qemu/sockets.h" |
Paolo Bonzini | 6dd844d | 2012-08-22 16:43:07 +0200 | [diff] [blame] | 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) { |
| 31 | nbd_client_new(NULL, fd, nbd_client_put); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | void 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) { |
| 44 | qemu_set_fd_handler2(server_fd, NULL, nbd_accept, NULL, NULL); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /* Hook into the BlockDriverState notifiers to close the export when |
| 49 | * the file is closed. |
| 50 | */ |
| 51 | typedef struct NBDCloseNotifier { |
| 52 | Notifier n; |
| 53 | NBDExport *exp; |
| 54 | QTAILQ_ENTRY(NBDCloseNotifier) next; |
| 55 | } NBDCloseNotifier; |
| 56 | |
| 57 | static QTAILQ_HEAD(, NBDCloseNotifier) close_notifiers = |
| 58 | QTAILQ_HEAD_INITIALIZER(close_notifiers); |
| 59 | |
| 60 | static void nbd_close_notifier(Notifier *n, void *data) |
| 61 | { |
| 62 | NBDCloseNotifier *cn = DO_UPCAST(NBDCloseNotifier, n, n); |
| 63 | |
| 64 | notifier_remove(&cn->n); |
| 65 | QTAILQ_REMOVE(&close_notifiers, cn, next); |
| 66 | |
| 67 | nbd_export_close(cn->exp); |
| 68 | nbd_export_put(cn->exp); |
| 69 | g_free(cn); |
| 70 | } |
| 71 | |
Paolo Bonzini | 6dd844d | 2012-08-22 16:43:07 +0200 | [diff] [blame] | 72 | void qmp_nbd_server_add(const char *device, bool has_writable, bool writable, |
| 73 | Error **errp) |
| 74 | { |
| 75 | BlockDriverState *bs; |
| 76 | NBDExport *exp; |
| 77 | NBDCloseNotifier *n; |
| 78 | |
Paolo Bonzini | 17b6be4 | 2012-11-12 14:25:17 +0100 | [diff] [blame] | 79 | if (server_fd == -1) { |
| 80 | error_setg(errp, "NBD server not running"); |
| 81 | return; |
| 82 | } |
| 83 | |
Paolo Bonzini | 6dd844d | 2012-08-22 16:43:07 +0200 | [diff] [blame] | 84 | if (nbd_export_find(device)) { |
| 85 | error_setg(errp, "NBD server already exporting device '%s'", device); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | bs = bdrv_find(device); |
| 90 | if (!bs) { |
| 91 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
| 92 | return; |
| 93 | } |
| 94 | |
Paolo Bonzini | e644473 | 2012-11-04 12:43:37 +0100 | [diff] [blame] | 95 | if (!has_writable) { |
Michal Privoznik | f3313d2 | 2012-11-28 11:46:39 +0100 | [diff] [blame] | 96 | writable = false; |
Paolo Bonzini | e644473 | 2012-11-04 12:43:37 +0100 | [diff] [blame] | 97 | } |
| 98 | if (bdrv_is_read_only(bs)) { |
| 99 | writable = false; |
| 100 | } |
| 101 | |
Fam Zheng | 38b54b6 | 2013-08-23 09:14:50 +0800 | [diff] [blame] | 102 | exp = nbd_export_new(bs, 0, -1, writable ? 0 : NBD_FLAG_READ_ONLY, NULL); |
Paolo Bonzini | 6dd844d | 2012-08-22 16:43:07 +0200 | [diff] [blame] | 103 | |
| 104 | nbd_export_set_name(exp, device); |
Paolo Bonzini | 6dd844d | 2012-08-22 16:43:07 +0200 | [diff] [blame] | 105 | |
| 106 | n = g_malloc0(sizeof(NBDCloseNotifier)); |
| 107 | n->n.notify = nbd_close_notifier; |
| 108 | n->exp = exp; |
| 109 | bdrv_add_close_notifier(bs, &n->n); |
| 110 | QTAILQ_INSERT_TAIL(&close_notifiers, n, next); |
| 111 | } |
| 112 | |
| 113 | void qmp_nbd_server_stop(Error **errp) |
| 114 | { |
| 115 | while (!QTAILQ_EMPTY(&close_notifiers)) { |
| 116 | NBDCloseNotifier *cn = QTAILQ_FIRST(&close_notifiers); |
| 117 | nbd_close_notifier(&cn->n, nbd_export_get_blockdev(cn->exp)); |
| 118 | } |
| 119 | |
Paolo Bonzini | fc6467e | 2012-11-12 14:12:54 +0100 | [diff] [blame] | 120 | if (server_fd != -1) { |
| 121 | qemu_set_fd_handler2(server_fd, NULL, NULL, NULL, NULL); |
| 122 | close(server_fd); |
| 123 | server_fd = -1; |
| 124 | } |
Paolo Bonzini | 6dd844d | 2012-08-22 16:43:07 +0200 | [diff] [blame] | 125 | } |