block/export: Add query-block-exports
[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 "qapi/qapi-events-block-export.h"
23 #include "qemu/id.h"
24
25 static const BlockExportDriver *blk_exp_drivers[] = {
26 &blk_exp_nbd,
27 };
28
29 /* Only accessed from the main thread */
30 static QLIST_HEAD(, BlockExport) block_exports =
31 QLIST_HEAD_INITIALIZER(block_exports);
32
33 BlockExport *blk_exp_find(const char *id)
34 {
35 BlockExport *exp;
36
37 QLIST_FOREACH(exp, &block_exports, next) {
38 if (strcmp(id, exp->id) == 0) {
39 return exp;
40 }
41 }
42
43 return NULL;
44 }
45
46 static const BlockExportDriver *blk_exp_find_driver(BlockExportType type)
47 {
48 int i;
49
50 for (i = 0; i < ARRAY_SIZE(blk_exp_drivers); i++) {
51 if (blk_exp_drivers[i]->type == type) {
52 return blk_exp_drivers[i];
53 }
54 }
55 return NULL;
56 }
57
58 BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
59 {
60 const BlockExportDriver *drv;
61 BlockExport *exp = NULL;
62 BlockDriverState *bs;
63 BlockBackend *blk;
64 AioContext *ctx;
65 int ret;
66
67 if (!id_wellformed(export->id)) {
68 error_setg(errp, "Invalid block export id");
69 return NULL;
70 }
71 if (blk_exp_find(export->id)) {
72 error_setg(errp, "Block export id '%s' is already in use", export->id);
73 return NULL;
74 }
75
76 drv = blk_exp_find_driver(export->type);
77 if (!drv) {
78 error_setg(errp, "No driver found for the requested export type");
79 return NULL;
80 }
81
82 bs = bdrv_lookup_bs(NULL, export->node_name, errp);
83 if (!bs) {
84 return NULL;
85 }
86
87 ctx = bdrv_get_aio_context(bs);
88 aio_context_acquire(ctx);
89
90 /*
91 * Block exports are used for non-shared storage migration. Make sure
92 * that BDRV_O_INACTIVE is cleared and the image is ready for write
93 * access since the export could be available before migration handover.
94 * ctx was acquired in the caller.
95 */
96 bdrv_invalidate_cache(bs, NULL);
97
98 blk = blk_new(ctx, BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
99 ret = blk_insert_bs(blk, bs, errp);
100 if (ret < 0) {
101 goto fail;
102 }
103
104 if (!export->has_writethrough) {
105 export->writethrough = false;
106 }
107 blk_set_enable_write_cache(blk, !export->writethrough);
108
109 assert(drv->instance_size >= sizeof(BlockExport));
110 exp = g_malloc0(drv->instance_size);
111 *exp = (BlockExport) {
112 .drv = drv,
113 .refcount = 1,
114 .user_owned = true,
115 .id = g_strdup(export->id),
116 .ctx = ctx,
117 .blk = blk,
118 };
119
120 ret = drv->create(exp, export, errp);
121 if (ret < 0) {
122 goto fail;
123 }
124
125 assert(exp->blk != NULL);
126
127 QLIST_INSERT_HEAD(&block_exports, exp, next);
128
129 aio_context_release(ctx);
130 return exp;
131
132 fail:
133 blk_unref(blk);
134 aio_context_release(ctx);
135 if (exp) {
136 g_free(exp->id);
137 g_free(exp);
138 }
139 return NULL;
140 }
141
142 /* Callers must hold exp->ctx lock */
143 void blk_exp_ref(BlockExport *exp)
144 {
145 assert(exp->refcount > 0);
146 exp->refcount++;
147 }
148
149 /* Runs in the main thread */
150 static void blk_exp_delete_bh(void *opaque)
151 {
152 BlockExport *exp = opaque;
153 AioContext *aio_context = exp->ctx;
154
155 aio_context_acquire(aio_context);
156
157 assert(exp->refcount == 0);
158 QLIST_REMOVE(exp, next);
159 exp->drv->delete(exp);
160 blk_unref(exp->blk);
161 qapi_event_send_block_export_deleted(exp->id);
162 g_free(exp->id);
163 g_free(exp);
164
165 aio_context_release(aio_context);
166 }
167
168 /* Callers must hold exp->ctx lock */
169 void blk_exp_unref(BlockExport *exp)
170 {
171 assert(exp->refcount > 0);
172 if (--exp->refcount == 0) {
173 /* Touch the block_exports list only in the main thread */
174 aio_bh_schedule_oneshot(qemu_get_aio_context(), blk_exp_delete_bh,
175 exp);
176 }
177 }
178
179 /*
180 * Drops the user reference to the export and requests that all client
181 * connections and other internally held references start to shut down. When
182 * the function returns, there may still be active references while the export
183 * is in the process of shutting down.
184 *
185 * Acquires exp->ctx internally. Callers must *not* hold the lock.
186 */
187 void blk_exp_request_shutdown(BlockExport *exp)
188 {
189 AioContext *aio_context = exp->ctx;
190
191 aio_context_acquire(aio_context);
192
193 /*
194 * If the user doesn't own the export any more, it is already shutting
195 * down. We must not call .request_shutdown and decrease the refcount a
196 * second time.
197 */
198 if (!exp->user_owned) {
199 goto out;
200 }
201
202 exp->drv->request_shutdown(exp);
203
204 assert(exp->user_owned);
205 exp->user_owned = false;
206 blk_exp_unref(exp);
207
208 out:
209 aio_context_release(aio_context);
210 }
211
212 /*
213 * Returns whether a block export of the given type exists.
214 * type == BLOCK_EXPORT_TYPE__MAX checks for an export of any type.
215 */
216 static bool blk_exp_has_type(BlockExportType type)
217 {
218 BlockExport *exp;
219
220 if (type == BLOCK_EXPORT_TYPE__MAX) {
221 return !QLIST_EMPTY(&block_exports);
222 }
223
224 QLIST_FOREACH(exp, &block_exports, next) {
225 if (exp->drv->type == type) {
226 return true;
227 }
228 }
229
230 return false;
231 }
232
233 /* type == BLOCK_EXPORT_TYPE__MAX for all types */
234 void blk_exp_close_all_type(BlockExportType type)
235 {
236 BlockExport *exp, *next;
237
238 assert(in_aio_context_home_thread(qemu_get_aio_context()));
239
240 QLIST_FOREACH_SAFE(exp, &block_exports, next, next) {
241 if (type != BLOCK_EXPORT_TYPE__MAX && exp->drv->type != type) {
242 continue;
243 }
244 blk_exp_request_shutdown(exp);
245 }
246
247 AIO_WAIT_WHILE(NULL, blk_exp_has_type(type));
248 }
249
250 void blk_exp_close_all(void)
251 {
252 blk_exp_close_all_type(BLOCK_EXPORT_TYPE__MAX);
253 }
254
255 void qmp_block_export_add(BlockExportOptions *export, Error **errp)
256 {
257 blk_exp_add(export, errp);
258 }
259
260 void qmp_block_export_del(const char *id,
261 bool has_mode, BlockExportRemoveMode mode,
262 Error **errp)
263 {
264 ERRP_GUARD();
265 BlockExport *exp;
266
267 exp = blk_exp_find(id);
268 if (exp == NULL) {
269 error_setg(errp, "Export '%s' is not found", id);
270 return;
271 }
272 if (!exp->user_owned) {
273 error_setg(errp, "Export '%s' is already shutting down", id);
274 return;
275 }
276
277 if (!has_mode) {
278 mode = BLOCK_EXPORT_REMOVE_MODE_SAFE;
279 }
280 if (mode == BLOCK_EXPORT_REMOVE_MODE_SAFE && exp->refcount > 1) {
281 error_setg(errp, "export '%s' still in use", exp->id);
282 error_append_hint(errp, "Use mode='hard' to force client "
283 "disconnect\n");
284 return;
285 }
286
287 blk_exp_request_shutdown(exp);
288 }
289
290 BlockExportInfoList *qmp_query_block_exports(Error **errp)
291 {
292 BlockExportInfoList *head = NULL, **p_next = &head;
293 BlockExport *exp;
294
295 QLIST_FOREACH(exp, &block_exports, next) {
296 BlockExportInfoList *entry = g_new0(BlockExportInfoList, 1);
297 BlockExportInfo *info = g_new(BlockExportInfo, 1);
298 *info = (BlockExportInfo) {
299 .id = g_strdup(exp->id),
300 .type = exp->drv->type,
301 .node_name = g_strdup(bdrv_get_node_name(blk_bs(exp->blk))),
302 .shutting_down = !exp->user_owned,
303 };
304
305 entry->value = info;
306 *p_next = entry;
307 p_next = &entry->next;
308 }
309
310 return head;
311 }