block/export: Add 'id' option to block-export-add
[qemu.git] / block / export / export.c
1 /*
2 * Common block export infrastructure
3 *
4 * Copyright (c) 2012, 2020 Red Hat, Inc.
5 *
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com>
8 * Kevin Wolf <kwolf@redhat.com>
9 *
10 * This work is licensed under the terms of the GNU GPL, version 2 or
11 * later. See the COPYING file in the top-level directory.
12 */
13
14 #include "qemu/osdep.h"
15
16 #include "block/block.h"
17 #include "sysemu/block-backend.h"
18 #include "block/export.h"
19 #include "block/nbd.h"
20 #include "qapi/error.h"
21 #include "qapi/qapi-commands-block-export.h"
22 #include "qemu/id.h"
23
24 static const BlockExportDriver *blk_exp_drivers[] = {
25 &blk_exp_nbd,
26 };
27
28 /* Only accessed from the main thread */
29 static QLIST_HEAD(, BlockExport) block_exports =
30 QLIST_HEAD_INITIALIZER(block_exports);
31
32 static BlockExport *blk_exp_find(const char *id)
33 {
34 BlockExport *exp;
35
36 QLIST_FOREACH(exp, &block_exports, next) {
37 if (strcmp(id, exp->id) == 0) {
38 return exp;
39 }
40 }
41
42 return NULL;
43 }
44
45 static const BlockExportDriver *blk_exp_find_driver(BlockExportType type)
46 {
47 int i;
48
49 for (i = 0; i < ARRAY_SIZE(blk_exp_drivers); i++) {
50 if (blk_exp_drivers[i]->type == type) {
51 return blk_exp_drivers[i];
52 }
53 }
54 return NULL;
55 }
56
57 BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
58 {
59 const BlockExportDriver *drv;
60 BlockExport *exp;
61 int ret;
62
63 if (!id_wellformed(export->id)) {
64 error_setg(errp, "Invalid block export id");
65 return NULL;
66 }
67 if (blk_exp_find(export->id)) {
68 error_setg(errp, "Block export id '%s' is already in use", export->id);
69 return NULL;
70 }
71
72 drv = blk_exp_find_driver(export->type);
73 if (!drv) {
74 error_setg(errp, "No driver found for the requested export type");
75 return NULL;
76 }
77
78 assert(drv->instance_size >= sizeof(BlockExport));
79 exp = g_malloc0(drv->instance_size);
80 *exp = (BlockExport) {
81 .drv = drv,
82 .refcount = 1,
83 .id = g_strdup(export->id),
84 };
85
86 ret = drv->create(exp, export, errp);
87 if (ret < 0) {
88 g_free(exp->id);
89 g_free(exp);
90 return NULL;
91 }
92
93 QLIST_INSERT_HEAD(&block_exports, exp, next);
94 return exp;
95 }
96
97 /* Callers must hold exp->ctx lock */
98 void blk_exp_ref(BlockExport *exp)
99 {
100 assert(exp->refcount > 0);
101 exp->refcount++;
102 }
103
104 /* Runs in the main thread */
105 static void blk_exp_delete_bh(void *opaque)
106 {
107 BlockExport *exp = opaque;
108 AioContext *aio_context = exp->ctx;
109
110 aio_context_acquire(aio_context);
111
112 assert(exp->refcount == 0);
113 QLIST_REMOVE(exp, next);
114 exp->drv->delete(exp);
115 g_free(exp->id);
116 g_free(exp);
117
118 aio_context_release(aio_context);
119 }
120
121 /* Callers must hold exp->ctx lock */
122 void blk_exp_unref(BlockExport *exp)
123 {
124 assert(exp->refcount > 0);
125 if (--exp->refcount == 0) {
126 /* Touch the block_exports list only in the main thread */
127 aio_bh_schedule_oneshot(qemu_get_aio_context(), blk_exp_delete_bh,
128 exp);
129 }
130 }
131
132 /*
133 * Drops the user reference to the export and requests that all client
134 * connections and other internally held references start to shut down. When
135 * the function returns, there may still be active references while the export
136 * is in the process of shutting down.
137 *
138 * Acquires exp->ctx internally. Callers must *not* hold the lock.
139 */
140 void blk_exp_request_shutdown(BlockExport *exp)
141 {
142 AioContext *aio_context = exp->ctx;
143
144 aio_context_acquire(aio_context);
145 exp->drv->request_shutdown(exp);
146 aio_context_release(aio_context);
147 }
148
149 /*
150 * Returns whether a block export of the given type exists.
151 * type == BLOCK_EXPORT_TYPE__MAX checks for an export of any type.
152 */
153 static bool blk_exp_has_type(BlockExportType type)
154 {
155 BlockExport *exp;
156
157 if (type == BLOCK_EXPORT_TYPE__MAX) {
158 return !QLIST_EMPTY(&block_exports);
159 }
160
161 QLIST_FOREACH(exp, &block_exports, next) {
162 if (exp->drv->type == type) {
163 return true;
164 }
165 }
166
167 return false;
168 }
169
170 /* type == BLOCK_EXPORT_TYPE__MAX for all types */
171 void blk_exp_close_all_type(BlockExportType type)
172 {
173 BlockExport *exp, *next;
174
175 assert(in_aio_context_home_thread(qemu_get_aio_context()));
176
177 QLIST_FOREACH_SAFE(exp, &block_exports, next, next) {
178 if (type != BLOCK_EXPORT_TYPE__MAX && exp->drv->type != type) {
179 continue;
180 }
181 blk_exp_request_shutdown(exp);
182 }
183
184 AIO_WAIT_WHILE(NULL, blk_exp_has_type(type));
185 }
186
187 void blk_exp_close_all(void)
188 {
189 blk_exp_close_all_type(BLOCK_EXPORT_TYPE__MAX);
190 }
191
192 void qmp_block_export_add(BlockExportOptions *export, Error **errp)
193 {
194 blk_exp_add(export, errp);
195 }