/src/qemu/block/export/export.c
Line | Count | Source |
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 | 0 | { |
52 | 0 | BlockExport *exp; |
53 | |
|
54 | 0 | QLIST_FOREACH(exp, &block_exports, next) { |
55 | 0 | if (strcmp(id, exp->id) == 0) { |
56 | 0 | return exp; |
57 | 0 | } |
58 | 0 | } |
59 | | |
60 | 0 | return NULL; |
61 | 0 | } |
62 | | |
63 | | static const BlockExportDriver *blk_exp_find_driver(BlockExportType type) |
64 | 0 | { |
65 | 0 | int i; |
66 | |
|
67 | 0 | for (i = 0; i < ARRAY_SIZE(blk_exp_drivers); i++) { |
68 | 0 | if (blk_exp_drivers[i]->type == type) { |
69 | 0 | return blk_exp_drivers[i]; |
70 | 0 | } |
71 | 0 | } |
72 | 0 | return NULL; |
73 | 0 | } |
74 | | |
75 | | BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp) |
76 | 0 | { |
77 | 0 | bool fixed_iothread = export->has_fixed_iothread && export->fixed_iothread; |
78 | 0 | bool allow_inactive = export->has_allow_inactive && export->allow_inactive; |
79 | 0 | bool multithread = export->iothread && |
80 | 0 | export->iothread->type == QTYPE_QLIST; |
81 | 0 | const BlockExportDriver *drv; |
82 | 0 | BlockExport *exp = NULL; |
83 | 0 | BlockDriverState *bs; |
84 | 0 | BlockBackend *blk = NULL; |
85 | 0 | AioContext *ctx; |
86 | 0 | AioContext **multithread_ctxs = NULL; |
87 | 0 | size_t multithread_count = 0; |
88 | 0 | uint64_t perm; |
89 | 0 | int ret; |
90 | |
|
91 | 0 | GLOBAL_STATE_CODE(); |
92 | |
|
93 | 0 | if (fixed_iothread && multithread) { |
94 | 0 | error_setg(errp, |
95 | 0 | "Cannot use fixed-iothread for a multi-threaded export"); |
96 | 0 | return NULL; |
97 | 0 | } |
98 | | |
99 | 0 | if (!id_wellformed(export->id)) { |
100 | 0 | error_setg(errp, "Invalid block export id"); |
101 | 0 | return NULL; |
102 | 0 | } |
103 | 0 | if (blk_exp_find(export->id)) { |
104 | 0 | error_setg(errp, "Block export id '%s' is already in use", export->id); |
105 | 0 | return NULL; |
106 | 0 | } |
107 | | |
108 | 0 | drv = blk_exp_find_driver(export->type); |
109 | 0 | if (!drv) { |
110 | 0 | error_setg(errp, "No driver found for the requested export type"); |
111 | 0 | return NULL; |
112 | 0 | } |
113 | | |
114 | 0 | bs = bdrv_lookup_bs(NULL, export->node_name, errp); |
115 | 0 | if (!bs) { |
116 | 0 | return NULL; |
117 | 0 | } |
118 | | |
119 | 0 | if (!export->has_writable) { |
120 | 0 | export->writable = false; |
121 | 0 | } |
122 | 0 | if (bdrv_is_read_only(bs) && export->writable) { |
123 | 0 | error_setg(errp, "Cannot export read-only node as writable"); |
124 | 0 | return NULL; |
125 | 0 | } |
126 | | |
127 | 0 | ctx = bdrv_get_aio_context(bs); |
128 | | |
129 | | /* Move the BDS to the target I/O thread, if it is a single one */ |
130 | 0 | if (export->iothread && !multithread) { |
131 | 0 | const char *iothread_id = export->iothread->u.single; |
132 | 0 | IOThread *iothread; |
133 | 0 | AioContext *new_ctx; |
134 | 0 | Error **set_context_errp; |
135 | |
|
136 | 0 | iothread = iothread_by_id(iothread_id); |
137 | 0 | if (!iothread) { |
138 | 0 | error_setg(errp, "iothread \"%s\" not found", iothread_id); |
139 | 0 | goto fail; |
140 | 0 | } |
141 | | |
142 | 0 | new_ctx = iothread_get_aio_context(iothread); |
143 | | |
144 | | /* Ignore errors with fixed-iothread=false */ |
145 | 0 | set_context_errp = fixed_iothread ? errp : NULL; |
146 | 0 | ret = bdrv_try_change_aio_context(bs, new_ctx, NULL, set_context_errp); |
147 | 0 | if (ret == 0) { |
148 | 0 | ctx = new_ctx; |
149 | 0 | } else if (fixed_iothread) { |
150 | 0 | goto fail; |
151 | 0 | } |
152 | 0 | } else if (multithread) { |
153 | 0 | strList *iothread_list = export->iothread->u.multi; |
154 | 0 | size_t i; |
155 | |
|
156 | 0 | multithread_count = 0; |
157 | 0 | for (strList *e = iothread_list; e; e = e->next) { |
158 | 0 | multithread_count++; |
159 | 0 | } |
160 | |
|
161 | 0 | if (multithread_count == 0) { |
162 | 0 | error_setg(errp, "The set of I/O threads must not be empty"); |
163 | 0 | return NULL; |
164 | 0 | } |
165 | | |
166 | 0 | multithread_ctxs = g_new(AioContext *, multithread_count); |
167 | 0 | i = 0; |
168 | 0 | for (strList *e = iothread_list; e; e = e->next) { |
169 | 0 | IOThread *iothread = iothread_by_id(e->value); |
170 | |
|
171 | 0 | if (!iothread) { |
172 | 0 | error_setg(errp, "iothread \"%s\" not found", e->value); |
173 | 0 | goto fail; |
174 | 0 | } |
175 | 0 | multithread_ctxs[i++] = iothread_get_aio_context(iothread); |
176 | 0 | } |
177 | 0 | assert(i == multithread_count); |
178 | 0 | } |
179 | | |
180 | 0 | bdrv_graph_rdlock_main_loop(); |
181 | 0 | if (allow_inactive) { |
182 | 0 | if (!drv->supports_inactive) { |
183 | 0 | error_setg(errp, "Export type does not support inactive exports"); |
184 | 0 | bdrv_graph_rdunlock_main_loop(); |
185 | 0 | goto fail; |
186 | 0 | } |
187 | 0 | } 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 | 0 | ret = bdrv_activate(bs, errp); |
194 | 0 | if (ret < 0) { |
195 | 0 | bdrv_graph_rdunlock_main_loop(); |
196 | 0 | goto fail; |
197 | 0 | } |
198 | 0 | } |
199 | 0 | bdrv_graph_rdunlock_main_loop(); |
200 | |
|
201 | 0 | perm = BLK_PERM_CONSISTENT_READ; |
202 | 0 | if (export->writable) { |
203 | 0 | perm |= BLK_PERM_WRITE; |
204 | 0 | } |
205 | |
|
206 | 0 | blk = blk_new(ctx, perm, BLK_PERM_ALL); |
207 | |
|
208 | 0 | if (!fixed_iothread) { |
209 | 0 | blk_set_allow_aio_context_change(blk, true); |
210 | 0 | } |
211 | 0 | if (allow_inactive) { |
212 | 0 | blk_set_force_allow_inactivate(blk); |
213 | 0 | } |
214 | |
|
215 | 0 | ret = blk_insert_bs(blk, bs, errp); |
216 | 0 | if (ret < 0) { |
217 | 0 | goto fail; |
218 | 0 | } |
219 | | |
220 | 0 | if (!export->has_writethrough) { |
221 | 0 | export->writethrough = false; |
222 | 0 | } |
223 | 0 | blk_set_enable_write_cache(blk, !export->writethrough); |
224 | |
|
225 | 0 | assert(drv->instance_size >= sizeof(BlockExport)); |
226 | 0 | exp = g_malloc0(drv->instance_size); |
227 | 0 | *exp = (BlockExport) { |
228 | 0 | .drv = drv, |
229 | 0 | .refcount = 1, |
230 | 0 | .user_owned = true, |
231 | 0 | .id = g_strdup(export->id), |
232 | 0 | .ctx = ctx, |
233 | 0 | .blk = blk, |
234 | 0 | }; |
235 | |
|
236 | 0 | ret = drv->create(exp, export, multithread_ctxs, multithread_count, errp); |
237 | 0 | if (ret < 0) { |
238 | 0 | goto fail; |
239 | 0 | } |
240 | | |
241 | 0 | assert(exp->blk != NULL); |
242 | |
|
243 | 0 | QLIST_INSERT_HEAD(&block_exports, exp, next); |
244 | 0 | g_free(multithread_ctxs); |
245 | 0 | return exp; |
246 | | |
247 | 0 | fail: |
248 | 0 | if (blk) { |
249 | 0 | blk_set_dev_ops(blk, NULL, NULL); |
250 | 0 | blk_unref(blk); |
251 | 0 | } |
252 | 0 | if (exp) { |
253 | 0 | g_free(exp->id); |
254 | 0 | g_free(exp); |
255 | 0 | } |
256 | 0 | g_free(multithread_ctxs); |
257 | 0 | return NULL; |
258 | 0 | } |
259 | | |
260 | | void blk_exp_ref(BlockExport *exp) |
261 | 0 | { |
262 | 0 | assert(qatomic_read(&exp->refcount) > 0); |
263 | 0 | qatomic_inc(&exp->refcount); |
264 | 0 | } |
265 | | |
266 | | /* Runs in the main thread */ |
267 | | static void blk_exp_delete_bh(void *opaque) |
268 | 0 | { |
269 | 0 | BlockExport *exp = opaque; |
270 | |
|
271 | 0 | assert(exp->refcount == 0); |
272 | 0 | QLIST_REMOVE(exp, next); |
273 | 0 | exp->drv->delete(exp); |
274 | 0 | blk_set_dev_ops(exp->blk, NULL, NULL); |
275 | 0 | blk_unref(exp->blk); |
276 | 0 | qapi_event_send_block_export_deleted(exp->id); |
277 | 0 | g_free(exp->id); |
278 | 0 | g_free(exp); |
279 | 0 | } |
280 | | |
281 | | void blk_exp_unref(BlockExport *exp) |
282 | 0 | { |
283 | 0 | assert(qatomic_read(&exp->refcount) > 0); |
284 | 0 | if (qatomic_fetch_dec(&exp->refcount) == 1) { |
285 | | /* Touch the block_exports list only in the main thread */ |
286 | 0 | aio_bh_schedule_oneshot(qemu_get_aio_context(), blk_exp_delete_bh, |
287 | 0 | exp); |
288 | 0 | } |
289 | 0 | } |
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 | 0 | { |
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 | 0 | if (!exp->user_owned) { |
305 | 0 | return; |
306 | 0 | } |
307 | | |
308 | 0 | exp->drv->request_shutdown(exp); |
309 | |
|
310 | 0 | assert(exp->user_owned); |
311 | 0 | exp->user_owned = false; |
312 | 0 | blk_exp_unref(exp); |
313 | 0 | } |
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 | 0 | { |
321 | 0 | BlockExport *exp; |
322 | |
|
323 | 0 | if (type == BLOCK_EXPORT_TYPE__MAX) { |
324 | 0 | return !QLIST_EMPTY(&block_exports); |
325 | 0 | } |
326 | | |
327 | 0 | QLIST_FOREACH(exp, &block_exports, next) { |
328 | 0 | if (exp->drv->type == type) { |
329 | 0 | return true; |
330 | 0 | } |
331 | 0 | } |
332 | | |
333 | 0 | return false; |
334 | 0 | } |
335 | | |
336 | | /* type == BLOCK_EXPORT_TYPE__MAX for all types */ |
337 | | void blk_exp_close_all_type(BlockExportType type) |
338 | 0 | { |
339 | 0 | BlockExport *exp, *next; |
340 | |
|
341 | 0 | assert(in_aio_context_home_thread(qemu_get_aio_context())); |
342 | |
|
343 | 0 | QLIST_FOREACH_SAFE(exp, &block_exports, next, next) { |
344 | 0 | if (type != BLOCK_EXPORT_TYPE__MAX && exp->drv->type != type) { |
345 | 0 | continue; |
346 | 0 | } |
347 | 0 | blk_exp_request_shutdown(exp); |
348 | 0 | } |
349 | |
|
350 | 0 | AIO_WAIT_WHILE_UNLOCKED(NULL, blk_exp_has_type(type)); |
351 | 0 | } |
352 | | |
353 | | void blk_exp_close_all(void) |
354 | 0 | { |
355 | 0 | blk_exp_close_all_type(BLOCK_EXPORT_TYPE__MAX); |
356 | 0 | } |
357 | | |
358 | | void qmp_block_export_add(BlockExportOptions *export, Error **errp) |
359 | 0 | { |
360 | 0 | blk_exp_add(export, errp); |
361 | 0 | } |
362 | | |
363 | | void qmp_block_export_del(const char *id, |
364 | | bool has_mode, BlockExportRemoveMode mode, |
365 | | Error **errp) |
366 | 0 | { |
367 | 0 | ERRP_GUARD(); |
368 | 0 | BlockExport *exp; |
369 | |
|
370 | 0 | exp = blk_exp_find(id); |
371 | 0 | if (exp == NULL) { |
372 | 0 | error_setg(errp, "Export '%s' is not found", id); |
373 | 0 | return; |
374 | 0 | } |
375 | 0 | if (!exp->user_owned) { |
376 | 0 | error_setg(errp, "Export '%s' is already shutting down", id); |
377 | 0 | return; |
378 | 0 | } |
379 | | |
380 | 0 | if (!has_mode) { |
381 | 0 | mode = BLOCK_EXPORT_REMOVE_MODE_SAFE; |
382 | 0 | } |
383 | 0 | if (mode == BLOCK_EXPORT_REMOVE_MODE_SAFE && |
384 | 0 | qatomic_read(&exp->refcount) > 1) { |
385 | 0 | error_setg(errp, "export '%s' still in use", exp->id); |
386 | 0 | error_append_hint(errp, "Use mode='hard' to force client " |
387 | 0 | "disconnect\n"); |
388 | 0 | return; |
389 | 0 | } |
390 | | |
391 | 0 | blk_exp_request_shutdown(exp); |
392 | 0 | } |
393 | | |
394 | | BlockExportInfoList *qmp_query_block_exports(Error **errp) |
395 | 0 | { |
396 | 0 | BlockExportInfoList *head = NULL, **tail = &head; |
397 | 0 | BlockExport *exp; |
398 | |
|
399 | 0 | QLIST_FOREACH(exp, &block_exports, next) { |
400 | 0 | BlockExportInfo *info = g_new(BlockExportInfo, 1); |
401 | 0 | *info = (BlockExportInfo) { |
402 | 0 | .id = g_strdup(exp->id), |
403 | 0 | .type = exp->drv->type, |
404 | 0 | .node_name = g_strdup(bdrv_get_node_name(blk_bs(exp->blk))), |
405 | 0 | .shutting_down = !exp->user_owned, |
406 | 0 | }; |
407 | |
|
408 | 0 | QAPI_LIST_APPEND(tail, info); |
409 | 0 | } |
410 | |
|
411 | 0 | return head; |
412 | 0 | } |