2 * Common block export infrastructure
4 * Copyright (c) 2012, 2020 Red Hat, Inc.
7 * Paolo Bonzini <pbonzini@redhat.com>
8 * Kevin Wolf <kwolf@redhat.com>
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.
14 #include "qemu/osdep.h"
16 #include "block/block.h"
17 #include "sysemu/block-backend.h"
18 #include "sysemu/iothread.h"
19 #include "block/export.h"
20 #include "block/nbd.h"
21 #include "qapi/error.h"
22 #include "qapi/qapi-commands-block-export.h"
23 #include "qapi/qapi-events-block-export.h"
25 #ifdef CONFIG_VHOST_USER_BLK_SERVER
26 #include "vhost-user-blk-server.h"
29 static const BlockExportDriver
*blk_exp_drivers
[] = {
31 #ifdef CONFIG_VHOST_USER_BLK_SERVER
32 &blk_exp_vhost_user_blk
,
36 /* Only accessed from the main thread */
37 static QLIST_HEAD(, BlockExport
) block_exports
=
38 QLIST_HEAD_INITIALIZER(block_exports
);
40 BlockExport
*blk_exp_find(const char *id
)
44 QLIST_FOREACH(exp
, &block_exports
, next
) {
45 if (strcmp(id
, exp
->id
) == 0) {
53 static const BlockExportDriver
*blk_exp_find_driver(BlockExportType type
)
57 for (i
= 0; i
< ARRAY_SIZE(blk_exp_drivers
); i
++) {
58 if (blk_exp_drivers
[i
]->type
== type
) {
59 return blk_exp_drivers
[i
];
65 BlockExport
*blk_exp_add(BlockExportOptions
*export
, Error
**errp
)
67 bool fixed_iothread
= export
->has_fixed_iothread
&& export
->fixed_iothread
;
68 const BlockExportDriver
*drv
;
69 BlockExport
*exp
= NULL
;
71 BlockBackend
*blk
= NULL
;
76 if (!id_wellformed(export
->id
)) {
77 error_setg(errp
, "Invalid block export id");
80 if (blk_exp_find(export
->id
)) {
81 error_setg(errp
, "Block export id '%s' is already in use", export
->id
);
85 drv
= blk_exp_find_driver(export
->type
);
87 error_setg(errp
, "No driver found for the requested export type");
91 bs
= bdrv_lookup_bs(NULL
, export
->node_name
, errp
);
96 if (!export
->has_writable
) {
97 export
->writable
= false;
99 if (bdrv_is_read_only(bs
) && export
->writable
) {
100 error_setg(errp
, "Cannot export read-only node as writable");
104 ctx
= bdrv_get_aio_context(bs
);
105 aio_context_acquire(ctx
);
107 if (export
->has_iothread
) {
111 iothread
= iothread_by_id(export
->iothread
);
113 error_setg(errp
, "iothread \"%s\" not found", export
->iothread
);
117 new_ctx
= iothread_get_aio_context(iothread
);
119 ret
= bdrv_try_set_aio_context(bs
, new_ctx
, errp
);
121 aio_context_release(ctx
);
122 aio_context_acquire(new_ctx
);
124 } else if (fixed_iothread
) {
130 * Block exports are used for non-shared storage migration. Make sure
131 * that BDRV_O_INACTIVE is cleared and the image is ready for write
132 * access since the export could be available before migration handover.
133 * ctx was acquired in the caller.
135 bdrv_invalidate_cache(bs
, NULL
);
137 perm
= BLK_PERM_CONSISTENT_READ
;
138 if (export
->writable
) {
139 perm
|= BLK_PERM_WRITE
;
142 blk
= blk_new(ctx
, perm
, BLK_PERM_ALL
);
144 if (!fixed_iothread
) {
145 blk_set_allow_aio_context_change(blk
, true);
148 ret
= blk_insert_bs(blk
, bs
, errp
);
153 if (!export
->has_writethrough
) {
154 export
->writethrough
= false;
156 blk_set_enable_write_cache(blk
, !export
->writethrough
);
158 assert(drv
->instance_size
>= sizeof(BlockExport
));
159 exp
= g_malloc0(drv
->instance_size
);
160 *exp
= (BlockExport
) {
164 .id
= g_strdup(export
->id
),
169 ret
= drv
->create(exp
, export
, errp
);
174 assert(exp
->blk
!= NULL
);
176 QLIST_INSERT_HEAD(&block_exports
, exp
, next
);
178 aio_context_release(ctx
);
183 aio_context_release(ctx
);
191 /* Callers must hold exp->ctx lock */
192 void blk_exp_ref(BlockExport
*exp
)
194 assert(exp
->refcount
> 0);
198 /* Runs in the main thread */
199 static void blk_exp_delete_bh(void *opaque
)
201 BlockExport
*exp
= opaque
;
202 AioContext
*aio_context
= exp
->ctx
;
204 aio_context_acquire(aio_context
);
206 assert(exp
->refcount
== 0);
207 QLIST_REMOVE(exp
, next
);
208 exp
->drv
->delete(exp
);
210 qapi_event_send_block_export_deleted(exp
->id
);
214 aio_context_release(aio_context
);
217 /* Callers must hold exp->ctx lock */
218 void blk_exp_unref(BlockExport
*exp
)
220 assert(exp
->refcount
> 0);
221 if (--exp
->refcount
== 0) {
222 /* Touch the block_exports list only in the main thread */
223 aio_bh_schedule_oneshot(qemu_get_aio_context(), blk_exp_delete_bh
,
229 * Drops the user reference to the export and requests that all client
230 * connections and other internally held references start to shut down. When
231 * the function returns, there may still be active references while the export
232 * is in the process of shutting down.
234 * Acquires exp->ctx internally. Callers must *not* hold the lock.
236 void blk_exp_request_shutdown(BlockExport
*exp
)
238 AioContext
*aio_context
= exp
->ctx
;
240 aio_context_acquire(aio_context
);
243 * If the user doesn't own the export any more, it is already shutting
244 * down. We must not call .request_shutdown and decrease the refcount a
247 if (!exp
->user_owned
) {
251 exp
->drv
->request_shutdown(exp
);
253 assert(exp
->user_owned
);
254 exp
->user_owned
= false;
258 aio_context_release(aio_context
);
262 * Returns whether a block export of the given type exists.
263 * type == BLOCK_EXPORT_TYPE__MAX checks for an export of any type.
265 static bool blk_exp_has_type(BlockExportType type
)
269 if (type
== BLOCK_EXPORT_TYPE__MAX
) {
270 return !QLIST_EMPTY(&block_exports
);
273 QLIST_FOREACH(exp
, &block_exports
, next
) {
274 if (exp
->drv
->type
== type
) {
282 /* type == BLOCK_EXPORT_TYPE__MAX for all types */
283 void blk_exp_close_all_type(BlockExportType type
)
285 BlockExport
*exp
, *next
;
287 assert(in_aio_context_home_thread(qemu_get_aio_context()));
289 QLIST_FOREACH_SAFE(exp
, &block_exports
, next
, next
) {
290 if (type
!= BLOCK_EXPORT_TYPE__MAX
&& exp
->drv
->type
!= type
) {
293 blk_exp_request_shutdown(exp
);
296 AIO_WAIT_WHILE(NULL
, blk_exp_has_type(type
));
299 void blk_exp_close_all(void)
301 blk_exp_close_all_type(BLOCK_EXPORT_TYPE__MAX
);
304 void qmp_block_export_add(BlockExportOptions
*export
, Error
**errp
)
306 blk_exp_add(export
, errp
);
309 void qmp_block_export_del(const char *id
,
310 bool has_mode
, BlockExportRemoveMode mode
,
316 exp
= blk_exp_find(id
);
318 error_setg(errp
, "Export '%s' is not found", id
);
321 if (!exp
->user_owned
) {
322 error_setg(errp
, "Export '%s' is already shutting down", id
);
327 mode
= BLOCK_EXPORT_REMOVE_MODE_SAFE
;
329 if (mode
== BLOCK_EXPORT_REMOVE_MODE_SAFE
&& exp
->refcount
> 1) {
330 error_setg(errp
, "export '%s' still in use", exp
->id
);
331 error_append_hint(errp
, "Use mode='hard' to force client "
336 blk_exp_request_shutdown(exp
);
339 BlockExportInfoList
*qmp_query_block_exports(Error
**errp
)
341 BlockExportInfoList
*head
= NULL
, **p_next
= &head
;
344 QLIST_FOREACH(exp
, &block_exports
, next
) {
345 BlockExportInfoList
*entry
= g_new0(BlockExportInfoList
, 1);
346 BlockExportInfo
*info
= g_new(BlockExportInfo
, 1);
347 *info
= (BlockExportInfo
) {
348 .id
= g_strdup(exp
->id
),
349 .type
= exp
->drv
->type
,
350 .node_name
= g_strdup(bdrv_get_node_name(blk_bs(exp
->blk
))),
351 .shutting_down
= !exp
->user_owned
,
356 p_next
= &entry
->next
;