master
c 412 lines 10.7 KB
Raw
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 "system/block-backend.h"
18 #include "system/iothread.h"
19 #include "block/export.h"
20 #include "block/fuse.h"
21 #include "block/nbd.h"
22 #include "qapi/error.h"
23 #include "qapi/qapi-commands-block-export.h"
24 #include "qapi/qapi-events-block-export.h"
25 #include "qemu/id.h"
26 #ifdef CONFIG_VHOST_USER_BLK_SERVER
27 #include "vhost-user-blk-server.h"
28 #endif
29 #ifdef CONFIG_VDUSE_BLK_EXPORT
30 #include "vduse-blk.h"
31 #endif
32
33 static const BlockExportDriver *blk_exp_drivers[] = {
34 &blk_exp_nbd,
35 #ifdef CONFIG_VHOST_USER_BLK_SERVER
36 &blk_exp_vhost_user_blk,
37 #endif
38 #ifdef CONFIG_FUSE
39 &blk_exp_fuse,
40 #endif
41 #ifdef CONFIG_VDUSE_BLK_EXPORT
42 &blk_exp_vduse_blk,
43 #endif
44 };
45
46 /* Only accessed from the main thread */
47 static QLIST_HEAD(, BlockExport) block_exports =
48 QLIST_HEAD_INITIALIZER(block_exports);
49
50 BlockExport *blk_exp_find(const char *id)
51 {
52 BlockExport *exp;
53
54 QLIST_FOREACH(exp, &block_exports, next) {
55 if (strcmp(id, exp->id) == 0) {
56 return exp;
57 }
58 }
59
60 return NULL;
61 }
62
63 static const BlockExportDriver *blk_exp_find_driver(BlockExportType type)
64 {
65 int i;
66
67 for (i = 0; i < ARRAY_SIZE(blk_exp_drivers); i++) {
68 if (blk_exp_drivers[i]->type == type) {
69 return blk_exp_drivers[i];
70 }
71 }
72 return NULL;
73 }
74
75 BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
76 {
77 bool fixed_iothread = export->has_fixed_iothread && export->fixed_iothread;
78 bool allow_inactive = export->has_allow_inactive && export->allow_inactive;
79 bool multithread = export->iothread &&
80 export->iothread->type == QTYPE_QLIST;
81 const BlockExportDriver *drv;
82 BlockExport *exp = NULL;
83 BlockDriverState *bs;
84 BlockBackend *blk = NULL;
85 AioContext *ctx;
86 AioContext **multithread_ctxs = NULL;
87 size_t multithread_count = 0;
88 uint64_t perm;
89 int ret;
90
91 GLOBAL_STATE_CODE();
92
93 if (fixed_iothread && multithread) {
94 error_setg(errp,
95 "Cannot use fixed-iothread for a multi-threaded export");
96 return NULL;
97 }
98
99 if (!id_wellformed(export->id)) {
100 error_setg(errp, "Invalid block export id");
101 return NULL;
102 }
103 if (blk_exp_find(export->id)) {
104 error_setg(errp, "Block export id '%s' is already in use", export->id);
105 return NULL;
106 }
107
108 drv = blk_exp_find_driver(export->type);
109 if (!drv) {
110 error_setg(errp, "No driver found for the requested export type");
111 return NULL;
112 }
113
114 bs = bdrv_lookup_bs(NULL, export->node_name, errp);
115 if (!bs) {
116 return NULL;
117 }
118
119 if (!export->has_writable) {
120 export->writable = false;
121 }
122 if (bdrv_is_read_only(bs) && export->writable) {
123 error_setg(errp, "Cannot export read-only node as writable");
124 return NULL;
125 }
126
127 ctx = bdrv_get_aio_context(bs);
128
129 /* Move the BDS to the target I/O thread, if it is a single one */
130 if (export->iothread && !multithread) {
131 const char *iothread_id = export->iothread->u.single;
132 IOThread *iothread;
133 AioContext *new_ctx;
134 Error **set_context_errp;
135
136 iothread = iothread_by_id(iothread_id);
137 if (!iothread) {
138 error_setg(errp, "iothread \"%s\" not found", iothread_id);
139 goto fail;
140 }
141
142 new_ctx = iothread_get_aio_context(iothread);
143
144 /* Ignore errors with fixed-iothread=false */
145 set_context_errp = fixed_iothread ? errp : NULL;
146 ret = bdrv_try_change_aio_context(bs, new_ctx, NULL, set_context_errp);
147 if (ret == 0) {
148 ctx = new_ctx;
149 } else if (fixed_iothread) {
150 goto fail;
151 }
152 } else if (multithread) {
153 strList *iothread_list = export->iothread->u.multi;
154 size_t i;
155
156 multithread_count = 0;
157 for (strList *e = iothread_list; e; e = e->next) {
158 multithread_count++;
159 }
160
161 if (multithread_count == 0) {
162 error_setg(errp, "The set of I/O threads must not be empty");
163 return NULL;
164 }
165
166 multithread_ctxs = g_new(AioContext *, multithread_count);
167 i = 0;
168 for (strList *e = iothread_list; e; e = e->next) {
169 IOThread *iothread = iothread_by_id(e->value);
170
171 if (!iothread) {
172 error_setg(errp, "iothread \"%s\" not found", e->value);
173 goto fail;
174 }
175 multithread_ctxs[i++] = iothread_get_aio_context(iothread);
176 }
177 assert(i == multithread_count);
178 }
179
180 bdrv_graph_rdlock_main_loop();
181 if (allow_inactive) {
182 if (!drv->supports_inactive) {
183 error_setg(errp, "Export type does not support inactive exports");
184 bdrv_graph_rdunlock_main_loop();
185 goto fail;
186 }
187 } else {
188 /*
189 * Block exports are used for non-shared storage migration. Make sure
190 * that BDRV_O_INACTIVE is cleared and the image is ready for write
191 * access since the export could be available before migration handover.
192 */
193 ret = bdrv_activate(bs, errp);
194 if (ret < 0) {
195 bdrv_graph_rdunlock_main_loop();
196 goto fail;
197 }
198 }
199 bdrv_graph_rdunlock_main_loop();
200
201 perm = BLK_PERM_CONSISTENT_READ;
202 if (export->writable) {
203 perm |= BLK_PERM_WRITE;
204 }
205
206 blk = blk_new(ctx, perm, BLK_PERM_ALL);
207
208 if (!fixed_iothread) {
209 blk_set_allow_aio_context_change(blk, true);
210 }
211 if (allow_inactive) {
212 blk_set_force_allow_inactivate(blk);
213 }
214
215 ret = blk_insert_bs(blk, bs, errp);
216 if (ret < 0) {
217 goto fail;
218 }
219
220 if (!export->has_writethrough) {
221 export->writethrough = false;
222 }
223 blk_set_enable_write_cache(blk, !export->writethrough);
224
225 assert(drv->instance_size >= sizeof(BlockExport));
226 exp = g_malloc0(drv->instance_size);
227 *exp = (BlockExport) {
228 .drv = drv,
229 .refcount = 1,
230 .user_owned = true,
231 .id = g_strdup(export->id),
232 .ctx = ctx,
233 .blk = blk,
234 };
235
236 ret = drv->create(exp, export, multithread_ctxs, multithread_count, errp);
237 if (ret < 0) {
238 goto fail;
239 }
240
241 assert(exp->blk != NULL);
242
243 QLIST_INSERT_HEAD(&block_exports, exp, next);
244 g_free(multithread_ctxs);
245 return exp;
246
247 fail:
248 if (blk) {
249 blk_set_dev_ops(blk, NULL, NULL);
250 blk_unref(blk);
251 }
252 if (exp) {
253 g_free(exp->id);
254 g_free(exp);
255 }
256 g_free(multithread_ctxs);
257 return NULL;
258 }
259
260 void blk_exp_ref(BlockExport *exp)
261 {
262 assert(qatomic_read(&exp->refcount) > 0);
263 qatomic_inc(&exp->refcount);
264 }
265
266 /* Runs in the main thread */
267 static void blk_exp_delete_bh(void *opaque)
268 {
269 BlockExport *exp = opaque;
270
271 assert(exp->refcount == 0);
272 QLIST_REMOVE(exp, next);
273 exp->drv->delete(exp);
274 blk_set_dev_ops(exp->blk, NULL, NULL);
275 blk_unref(exp->blk);
276 qapi_event_send_block_export_deleted(exp->id);
277 g_free(exp->id);
278 g_free(exp);
279 }
280
281 void blk_exp_unref(BlockExport *exp)
282 {
283 assert(qatomic_read(&exp->refcount) > 0);
284 if (qatomic_fetch_dec(&exp->refcount) == 1) {
285 /* Touch the block_exports list only in the main thread */
286 aio_bh_schedule_oneshot(qemu_get_aio_context(), blk_exp_delete_bh,
287 exp);
288 }
289 }
290
291 /*
292 * Drops the user reference to the export and requests that all client
293 * connections and other internally held references start to shut down. When
294 * the function returns, there may still be active references while the export
295 * is in the process of shutting down.
296 */
297 void blk_exp_request_shutdown(BlockExport *exp)
298 {
299 /*
300 * If the user doesn't own the export any more, it is already shutting
301 * down. We must not call .request_shutdown and decrease the refcount a
302 * second time.
303 */
304 if (!exp->user_owned) {
305 return;
306 }
307
308 exp->drv->request_shutdown(exp);
309
310 assert(exp->user_owned);
311 exp->user_owned = false;
312 blk_exp_unref(exp);
313 }
314
315 /*
316 * Returns whether a block export of the given type exists.
317 * type == BLOCK_EXPORT_TYPE__MAX checks for an export of any type.
318 */
319 static bool blk_exp_has_type(BlockExportType type)
320 {
321 BlockExport *exp;
322
323 if (type == BLOCK_EXPORT_TYPE__MAX) {
324 return !QLIST_EMPTY(&block_exports);
325 }
326
327 QLIST_FOREACH(exp, &block_exports, next) {
328 if (exp->drv->type == type) {
329 return true;
330 }
331 }
332
333 return false;
334 }
335
336 /* type == BLOCK_EXPORT_TYPE__MAX for all types */
337 void blk_exp_close_all_type(BlockExportType type)
338 {
339 BlockExport *exp, *next;
340
341 assert(in_aio_context_home_thread(qemu_get_aio_context()));
342
343 QLIST_FOREACH_SAFE(exp, &block_exports, next, next) {
344 if (type != BLOCK_EXPORT_TYPE__MAX && exp->drv->type != type) {
345 continue;
346 }
347 blk_exp_request_shutdown(exp);
348 }
349
350 AIO_WAIT_WHILE_UNLOCKED(NULL, blk_exp_has_type(type));
351 }
352
353 void blk_exp_close_all(void)
354 {
355 blk_exp_close_all_type(BLOCK_EXPORT_TYPE__MAX);
356 }
357
358 void qmp_block_export_add(BlockExportOptions *export, Error **errp)
359 {
360 blk_exp_add(export, errp);
361 }
362
363 void qmp_block_export_del(const char *id,
364 bool has_mode, BlockExportRemoveMode mode,
365 Error **errp)
366 {
367 ERRP_GUARD();
368 BlockExport *exp;
369
370 exp = blk_exp_find(id);
371 if (exp == NULL) {
372 error_setg(errp, "Export '%s' is not found", id);
373 return;
374 }
375 if (!exp->user_owned) {
376 error_setg(errp, "Export '%s' is already shutting down", id);
377 return;
378 }
379
380 if (!has_mode) {
381 mode = BLOCK_EXPORT_REMOVE_MODE_SAFE;
382 }
383 if (mode == BLOCK_EXPORT_REMOVE_MODE_SAFE &&
384 qatomic_read(&exp->refcount) > 1) {
385 error_setg(errp, "export '%s' still in use", exp->id);
386 error_append_hint(errp, "Use mode='hard' to force client "
387 "disconnect\n");
388 return;
389 }
390
391 blk_exp_request_shutdown(exp);
392 }
393
394 BlockExportInfoList *qmp_query_block_exports(Error **errp)
395 {
396 BlockExportInfoList *head = NULL, **tail = &head;
397 BlockExport *exp;
398
399 QLIST_FOREACH(exp, &block_exports, next) {
400 BlockExportInfo *info = g_new(BlockExportInfo, 1);
401 *info = (BlockExportInfo) {
402 .id = g_strdup(exp->id),
403 .type = exp->drv->type,
404 .node_name = g_strdup(bdrv_get_node_name(blk_bs(exp->blk))),
405 .shutting_down = !exp->user_owned,
406 };
407
408 QAPI_LIST_APPEND(tail, info);
409 }
410
411 return head;
412 }