xref: /openbmc/qemu/block/export/export.c (revision 0c9b70d5900a5108e899edfdd6f3790f8cb6bdc2)
156ee8626SKevin Wolf /*
256ee8626SKevin Wolf  * Common block export infrastructure
356ee8626SKevin Wolf  *
456ee8626SKevin Wolf  * Copyright (c) 2012, 2020 Red Hat, Inc.
556ee8626SKevin Wolf  *
656ee8626SKevin Wolf  * Authors:
756ee8626SKevin Wolf  * Paolo Bonzini <pbonzini@redhat.com>
856ee8626SKevin Wolf  * Kevin Wolf <kwolf@redhat.com>
956ee8626SKevin Wolf  *
1056ee8626SKevin Wolf  * This work is licensed under the terms of the GNU GPL, version 2 or
1156ee8626SKevin Wolf  * later.  See the COPYING file in the top-level directory.
1256ee8626SKevin Wolf  */
1356ee8626SKevin Wolf 
1456ee8626SKevin Wolf #include "qemu/osdep.h"
1556ee8626SKevin Wolf 
169b562c64SKevin Wolf #include "block/block.h"
179b562c64SKevin Wolf #include "sysemu/block-backend.h"
18f51d23c8SStefan Hajnoczi #include "sysemu/iothread.h"
1956ee8626SKevin Wolf #include "block/export.h"
20*0c9b70d5SMax Reitz #include "block/fuse.h"
2156ee8626SKevin Wolf #include "block/nbd.h"
2256ee8626SKevin Wolf #include "qapi/error.h"
2356ee8626SKevin Wolf #include "qapi/qapi-commands-block-export.h"
241a9f7a80SKevin Wolf #include "qapi/qapi-events-block-export.h"
25d53be9ceSKevin Wolf #include "qemu/id.h"
26bc15e44cSStefan Hajnoczi #ifdef CONFIG_VHOST_USER_BLK_SERVER
273a213f83SStefan Hajnoczi #include "vhost-user-blk-server.h"
283a213f83SStefan Hajnoczi #endif
2956ee8626SKevin Wolf 
3056ee8626SKevin Wolf static const BlockExportDriver *blk_exp_drivers[] = {
3156ee8626SKevin Wolf     &blk_exp_nbd,
32bc15e44cSStefan Hajnoczi #ifdef CONFIG_VHOST_USER_BLK_SERVER
3390fc91d5SStefan Hajnoczi     &blk_exp_vhost_user_blk,
3490fc91d5SStefan Hajnoczi #endif
35*0c9b70d5SMax Reitz #ifdef CONFIG_FUSE
36*0c9b70d5SMax Reitz     &blk_exp_fuse,
37*0c9b70d5SMax Reitz #endif
3856ee8626SKevin Wolf };
3956ee8626SKevin Wolf 
40bc4ee65bSKevin Wolf /* Only accessed from the main thread */
41bc4ee65bSKevin Wolf static QLIST_HEAD(, BlockExport) block_exports =
42bc4ee65bSKevin Wolf     QLIST_HEAD_INITIALIZER(block_exports);
43bc4ee65bSKevin Wolf 
443c3bc462SKevin Wolf BlockExport *blk_exp_find(const char *id)
45d53be9ceSKevin Wolf {
46d53be9ceSKevin Wolf     BlockExport *exp;
47d53be9ceSKevin Wolf 
48d53be9ceSKevin Wolf     QLIST_FOREACH(exp, &block_exports, next) {
49d53be9ceSKevin Wolf         if (strcmp(id, exp->id) == 0) {
50d53be9ceSKevin Wolf             return exp;
51d53be9ceSKevin Wolf         }
52d53be9ceSKevin Wolf     }
53d53be9ceSKevin Wolf 
54d53be9ceSKevin Wolf     return NULL;
55d53be9ceSKevin Wolf }
56d53be9ceSKevin Wolf 
5756ee8626SKevin Wolf static const BlockExportDriver *blk_exp_find_driver(BlockExportType type)
5856ee8626SKevin Wolf {
5956ee8626SKevin Wolf     int i;
6056ee8626SKevin Wolf 
6156ee8626SKevin Wolf     for (i = 0; i < ARRAY_SIZE(blk_exp_drivers); i++) {
6256ee8626SKevin Wolf         if (blk_exp_drivers[i]->type == type) {
6356ee8626SKevin Wolf             return blk_exp_drivers[i];
6456ee8626SKevin Wolf         }
6556ee8626SKevin Wolf     }
6656ee8626SKevin Wolf     return NULL;
6756ee8626SKevin Wolf }
6856ee8626SKevin Wolf 
699b562c64SKevin Wolf BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
7056ee8626SKevin Wolf {
71f51d23c8SStefan Hajnoczi     bool fixed_iothread = export->has_fixed_iothread && export->fixed_iothread;
7256ee8626SKevin Wolf     const BlockExportDriver *drv;
73331170e0SKevin Wolf     BlockExport *exp = NULL;
74331170e0SKevin Wolf     BlockDriverState *bs;
75f51d23c8SStefan Hajnoczi     BlockBackend *blk = NULL;
76331170e0SKevin Wolf     AioContext *ctx;
7730dbc81dSKevin Wolf     uint64_t perm;
78a6ff7989SKevin Wolf     int ret;
7956ee8626SKevin Wolf 
80d53be9ceSKevin Wolf     if (!id_wellformed(export->id)) {
81d53be9ceSKevin Wolf         error_setg(errp, "Invalid block export id");
82d53be9ceSKevin Wolf         return NULL;
83d53be9ceSKevin Wolf     }
84d53be9ceSKevin Wolf     if (blk_exp_find(export->id)) {
85d53be9ceSKevin Wolf         error_setg(errp, "Block export id '%s' is already in use", export->id);
86d53be9ceSKevin Wolf         return NULL;
87d53be9ceSKevin Wolf     }
88d53be9ceSKevin Wolf 
8956ee8626SKevin Wolf     drv = blk_exp_find_driver(export->type);
9056ee8626SKevin Wolf     if (!drv) {
9156ee8626SKevin Wolf         error_setg(errp, "No driver found for the requested export type");
929b562c64SKevin Wolf         return NULL;
9356ee8626SKevin Wolf     }
9456ee8626SKevin Wolf 
95331170e0SKevin Wolf     bs = bdrv_lookup_bs(NULL, export->node_name, errp);
96331170e0SKevin Wolf     if (!bs) {
97331170e0SKevin Wolf         return NULL;
98331170e0SKevin Wolf     }
99331170e0SKevin Wolf 
10030dbc81dSKevin Wolf     if (!export->has_writable) {
10130dbc81dSKevin Wolf         export->writable = false;
10230dbc81dSKevin Wolf     }
10330dbc81dSKevin Wolf     if (bdrv_is_read_only(bs) && export->writable) {
10430dbc81dSKevin Wolf         error_setg(errp, "Cannot export read-only node as writable");
10530dbc81dSKevin Wolf         return NULL;
10630dbc81dSKevin Wolf     }
10730dbc81dSKevin Wolf 
108331170e0SKevin Wolf     ctx = bdrv_get_aio_context(bs);
109331170e0SKevin Wolf     aio_context_acquire(ctx);
110331170e0SKevin Wolf 
111f51d23c8SStefan Hajnoczi     if (export->has_iothread) {
112f51d23c8SStefan Hajnoczi         IOThread *iothread;
113f51d23c8SStefan Hajnoczi         AioContext *new_ctx;
114f51d23c8SStefan Hajnoczi 
115f51d23c8SStefan Hajnoczi         iothread = iothread_by_id(export->iothread);
116f51d23c8SStefan Hajnoczi         if (!iothread) {
117f51d23c8SStefan Hajnoczi             error_setg(errp, "iothread \"%s\" not found", export->iothread);
118f51d23c8SStefan Hajnoczi             goto fail;
119f51d23c8SStefan Hajnoczi         }
120f51d23c8SStefan Hajnoczi 
121f51d23c8SStefan Hajnoczi         new_ctx = iothread_get_aio_context(iothread);
122f51d23c8SStefan Hajnoczi 
123f51d23c8SStefan Hajnoczi         ret = bdrv_try_set_aio_context(bs, new_ctx, errp);
124f51d23c8SStefan Hajnoczi         if (ret == 0) {
125f51d23c8SStefan Hajnoczi             aio_context_release(ctx);
126f51d23c8SStefan Hajnoczi             aio_context_acquire(new_ctx);
127f51d23c8SStefan Hajnoczi             ctx = new_ctx;
128f51d23c8SStefan Hajnoczi         } else if (fixed_iothread) {
129f51d23c8SStefan Hajnoczi             goto fail;
130f51d23c8SStefan Hajnoczi         }
131f51d23c8SStefan Hajnoczi     }
132f51d23c8SStefan Hajnoczi 
133331170e0SKevin Wolf     /*
134331170e0SKevin Wolf      * Block exports are used for non-shared storage migration. Make sure
135331170e0SKevin Wolf      * that BDRV_O_INACTIVE is cleared and the image is ready for write
136331170e0SKevin Wolf      * access since the export could be available before migration handover.
137331170e0SKevin Wolf      * ctx was acquired in the caller.
138331170e0SKevin Wolf      */
139331170e0SKevin Wolf     bdrv_invalidate_cache(bs, NULL);
140331170e0SKevin Wolf 
14130dbc81dSKevin Wolf     perm = BLK_PERM_CONSISTENT_READ;
14230dbc81dSKevin Wolf     if (export->writable) {
14330dbc81dSKevin Wolf         perm |= BLK_PERM_WRITE;
14430dbc81dSKevin Wolf     }
14530dbc81dSKevin Wolf 
14630dbc81dSKevin Wolf     blk = blk_new(ctx, perm, BLK_PERM_ALL);
147f51d23c8SStefan Hajnoczi 
148f51d23c8SStefan Hajnoczi     if (!fixed_iothread) {
149f51d23c8SStefan Hajnoczi         blk_set_allow_aio_context_change(blk, true);
150f51d23c8SStefan Hajnoczi     }
151f51d23c8SStefan Hajnoczi 
152331170e0SKevin Wolf     ret = blk_insert_bs(blk, bs, errp);
153331170e0SKevin Wolf     if (ret < 0) {
154331170e0SKevin Wolf         goto fail;
155331170e0SKevin Wolf     }
156331170e0SKevin Wolf 
157331170e0SKevin Wolf     if (!export->has_writethrough) {
158331170e0SKevin Wolf         export->writethrough = false;
159331170e0SKevin Wolf     }
160331170e0SKevin Wolf     blk_set_enable_write_cache(blk, !export->writethrough);
161331170e0SKevin Wolf 
162a6ff7989SKevin Wolf     assert(drv->instance_size >= sizeof(BlockExport));
163a6ff7989SKevin Wolf     exp = g_malloc0(drv->instance_size);
164a6ff7989SKevin Wolf     *exp = (BlockExport) {
165a6ff7989SKevin Wolf         .drv        = drv,
166a6ff7989SKevin Wolf         .refcount   = 1,
1673859ad36SKevin Wolf         .user_owned = true,
168d53be9ceSKevin Wolf         .id         = g_strdup(export->id),
169331170e0SKevin Wolf         .ctx        = ctx,
170331170e0SKevin Wolf         .blk        = blk,
171a6ff7989SKevin Wolf     };
172a6ff7989SKevin Wolf 
173a6ff7989SKevin Wolf     ret = drv->create(exp, export, errp);
174a6ff7989SKevin Wolf     if (ret < 0) {
175331170e0SKevin Wolf         goto fail;
176a6ff7989SKevin Wolf     }
177a6ff7989SKevin Wolf 
17837a4f70cSKevin Wolf     assert(exp->blk != NULL);
17937a4f70cSKevin Wolf 
180bc4ee65bSKevin Wolf     QLIST_INSERT_HEAD(&block_exports, exp, next);
181331170e0SKevin Wolf 
182331170e0SKevin Wolf     aio_context_release(ctx);
183a6ff7989SKevin Wolf     return exp;
184331170e0SKevin Wolf 
185331170e0SKevin Wolf fail:
186331170e0SKevin Wolf     blk_unref(blk);
187331170e0SKevin Wolf     aio_context_release(ctx);
188331170e0SKevin Wolf     if (exp) {
189331170e0SKevin Wolf         g_free(exp->id);
190331170e0SKevin Wolf         g_free(exp);
191331170e0SKevin Wolf     }
192331170e0SKevin Wolf     return NULL;
1939b562c64SKevin Wolf }
1949b562c64SKevin Wolf 
1958612c686SKevin Wolf /* Callers must hold exp->ctx lock */
196c69de1beSKevin Wolf void blk_exp_ref(BlockExport *exp)
197c69de1beSKevin Wolf {
198c69de1beSKevin Wolf     assert(exp->refcount > 0);
199c69de1beSKevin Wolf     exp->refcount++;
200c69de1beSKevin Wolf }
201c69de1beSKevin Wolf 
202bc4ee65bSKevin Wolf /* Runs in the main thread */
203bc4ee65bSKevin Wolf static void blk_exp_delete_bh(void *opaque)
204bc4ee65bSKevin Wolf {
205bc4ee65bSKevin Wolf     BlockExport *exp = opaque;
206bc4ee65bSKevin Wolf     AioContext *aio_context = exp->ctx;
207bc4ee65bSKevin Wolf 
208bc4ee65bSKevin Wolf     aio_context_acquire(aio_context);
209bc4ee65bSKevin Wolf 
210bc4ee65bSKevin Wolf     assert(exp->refcount == 0);
211bc4ee65bSKevin Wolf     QLIST_REMOVE(exp, next);
212bc4ee65bSKevin Wolf     exp->drv->delete(exp);
21337a4f70cSKevin Wolf     blk_unref(exp->blk);
2141a9f7a80SKevin Wolf     qapi_event_send_block_export_deleted(exp->id);
215d53be9ceSKevin Wolf     g_free(exp->id);
216bc4ee65bSKevin Wolf     g_free(exp);
217bc4ee65bSKevin Wolf 
218bc4ee65bSKevin Wolf     aio_context_release(aio_context);
219bc4ee65bSKevin Wolf }
220bc4ee65bSKevin Wolf 
2218612c686SKevin Wolf /* Callers must hold exp->ctx lock */
222c69de1beSKevin Wolf void blk_exp_unref(BlockExport *exp)
223c69de1beSKevin Wolf {
224c69de1beSKevin Wolf     assert(exp->refcount > 0);
225c69de1beSKevin Wolf     if (--exp->refcount == 0) {
226bc4ee65bSKevin Wolf         /* Touch the block_exports list only in the main thread */
227bc4ee65bSKevin Wolf         aio_bh_schedule_oneshot(qemu_get_aio_context(), blk_exp_delete_bh,
228bc4ee65bSKevin Wolf                                 exp);
229c69de1beSKevin Wolf     }
230c69de1beSKevin Wolf }
231c69de1beSKevin Wolf 
232bc4ee65bSKevin Wolf /*
233bc4ee65bSKevin Wolf  * Drops the user reference to the export and requests that all client
234bc4ee65bSKevin Wolf  * connections and other internally held references start to shut down. When
235bc4ee65bSKevin Wolf  * the function returns, there may still be active references while the export
236bc4ee65bSKevin Wolf  * is in the process of shutting down.
237bc4ee65bSKevin Wolf  *
238bc4ee65bSKevin Wolf  * Acquires exp->ctx internally. Callers must *not* hold the lock.
239bc4ee65bSKevin Wolf  */
240bc4ee65bSKevin Wolf void blk_exp_request_shutdown(BlockExport *exp)
241bc4ee65bSKevin Wolf {
242bc4ee65bSKevin Wolf     AioContext *aio_context = exp->ctx;
243bc4ee65bSKevin Wolf 
244bc4ee65bSKevin Wolf     aio_context_acquire(aio_context);
2453c3bc462SKevin Wolf 
2463c3bc462SKevin Wolf     /*
2473c3bc462SKevin Wolf      * If the user doesn't own the export any more, it is already shutting
2483c3bc462SKevin Wolf      * down. We must not call .request_shutdown and decrease the refcount a
2493c3bc462SKevin Wolf      * second time.
2503c3bc462SKevin Wolf      */
2513c3bc462SKevin Wolf     if (!exp->user_owned) {
2523c3bc462SKevin Wolf         goto out;
2533c3bc462SKevin Wolf     }
2543c3bc462SKevin Wolf 
255bc4ee65bSKevin Wolf     exp->drv->request_shutdown(exp);
2563859ad36SKevin Wolf 
2573859ad36SKevin Wolf     assert(exp->user_owned);
2583859ad36SKevin Wolf     exp->user_owned = false;
2593859ad36SKevin Wolf     blk_exp_unref(exp);
2603859ad36SKevin Wolf 
2613c3bc462SKevin Wolf out:
262bc4ee65bSKevin Wolf     aio_context_release(aio_context);
263bc4ee65bSKevin Wolf }
264bc4ee65bSKevin Wolf 
265bc4ee65bSKevin Wolf /*
266bc4ee65bSKevin Wolf  * Returns whether a block export of the given type exists.
267bc4ee65bSKevin Wolf  * type == BLOCK_EXPORT_TYPE__MAX checks for an export of any type.
268bc4ee65bSKevin Wolf  */
269bc4ee65bSKevin Wolf static bool blk_exp_has_type(BlockExportType type)
270bc4ee65bSKevin Wolf {
271bc4ee65bSKevin Wolf     BlockExport *exp;
272bc4ee65bSKevin Wolf 
273bc4ee65bSKevin Wolf     if (type == BLOCK_EXPORT_TYPE__MAX) {
274bc4ee65bSKevin Wolf         return !QLIST_EMPTY(&block_exports);
275bc4ee65bSKevin Wolf     }
276bc4ee65bSKevin Wolf 
277bc4ee65bSKevin Wolf     QLIST_FOREACH(exp, &block_exports, next) {
278bc4ee65bSKevin Wolf         if (exp->drv->type == type) {
279bc4ee65bSKevin Wolf             return true;
280bc4ee65bSKevin Wolf         }
281bc4ee65bSKevin Wolf     }
282bc4ee65bSKevin Wolf 
283bc4ee65bSKevin Wolf     return false;
284bc4ee65bSKevin Wolf }
285bc4ee65bSKevin Wolf 
286bc4ee65bSKevin Wolf /* type == BLOCK_EXPORT_TYPE__MAX for all types */
287bc4ee65bSKevin Wolf void blk_exp_close_all_type(BlockExportType type)
288bc4ee65bSKevin Wolf {
289bc4ee65bSKevin Wolf     BlockExport *exp, *next;
290bc4ee65bSKevin Wolf 
291bc4ee65bSKevin Wolf     assert(in_aio_context_home_thread(qemu_get_aio_context()));
292bc4ee65bSKevin Wolf 
293bc4ee65bSKevin Wolf     QLIST_FOREACH_SAFE(exp, &block_exports, next, next) {
294bc4ee65bSKevin Wolf         if (type != BLOCK_EXPORT_TYPE__MAX && exp->drv->type != type) {
295bc4ee65bSKevin Wolf             continue;
296bc4ee65bSKevin Wolf         }
297bc4ee65bSKevin Wolf         blk_exp_request_shutdown(exp);
298bc4ee65bSKevin Wolf     }
299bc4ee65bSKevin Wolf 
300bc4ee65bSKevin Wolf     AIO_WAIT_WHILE(NULL, blk_exp_has_type(type));
301bc4ee65bSKevin Wolf }
302bc4ee65bSKevin Wolf 
303bc4ee65bSKevin Wolf void blk_exp_close_all(void)
304bc4ee65bSKevin Wolf {
305bc4ee65bSKevin Wolf     blk_exp_close_all_type(BLOCK_EXPORT_TYPE__MAX);
306bc4ee65bSKevin Wolf }
307bc4ee65bSKevin Wolf 
3089b562c64SKevin Wolf void qmp_block_export_add(BlockExportOptions *export, Error **errp)
3099b562c64SKevin Wolf {
3109b562c64SKevin Wolf     blk_exp_add(export, errp);
31156ee8626SKevin Wolf }
3123c3bc462SKevin Wolf 
3133c3bc462SKevin Wolf void qmp_block_export_del(const char *id,
3143c3bc462SKevin Wolf                           bool has_mode, BlockExportRemoveMode mode,
3153c3bc462SKevin Wolf                           Error **errp)
3163c3bc462SKevin Wolf {
3173c3bc462SKevin Wolf     ERRP_GUARD();
3183c3bc462SKevin Wolf     BlockExport *exp;
3193c3bc462SKevin Wolf 
3203c3bc462SKevin Wolf     exp = blk_exp_find(id);
3213c3bc462SKevin Wolf     if (exp == NULL) {
3223c3bc462SKevin Wolf         error_setg(errp, "Export '%s' is not found", id);
3233c3bc462SKevin Wolf         return;
3243c3bc462SKevin Wolf     }
3253c3bc462SKevin Wolf     if (!exp->user_owned) {
3263c3bc462SKevin Wolf         error_setg(errp, "Export '%s' is already shutting down", id);
3273c3bc462SKevin Wolf         return;
3283c3bc462SKevin Wolf     }
3293c3bc462SKevin Wolf 
3303c3bc462SKevin Wolf     if (!has_mode) {
3313c3bc462SKevin Wolf         mode = BLOCK_EXPORT_REMOVE_MODE_SAFE;
3323c3bc462SKevin Wolf     }
3333c3bc462SKevin Wolf     if (mode == BLOCK_EXPORT_REMOVE_MODE_SAFE && exp->refcount > 1) {
3343c3bc462SKevin Wolf         error_setg(errp, "export '%s' still in use", exp->id);
3353c3bc462SKevin Wolf         error_append_hint(errp, "Use mode='hard' to force client "
3363c3bc462SKevin Wolf                           "disconnect\n");
3373c3bc462SKevin Wolf         return;
3383c3bc462SKevin Wolf     }
3393c3bc462SKevin Wolf 
3403c3bc462SKevin Wolf     blk_exp_request_shutdown(exp);
3413c3bc462SKevin Wolf }
3428cade320SKevin Wolf 
3438cade320SKevin Wolf BlockExportInfoList *qmp_query_block_exports(Error **errp)
3448cade320SKevin Wolf {
3458cade320SKevin Wolf     BlockExportInfoList *head = NULL, **p_next = &head;
3468cade320SKevin Wolf     BlockExport *exp;
3478cade320SKevin Wolf 
3488cade320SKevin Wolf     QLIST_FOREACH(exp, &block_exports, next) {
3498cade320SKevin Wolf         BlockExportInfoList *entry = g_new0(BlockExportInfoList, 1);
3508cade320SKevin Wolf         BlockExportInfo *info = g_new(BlockExportInfo, 1);
3518cade320SKevin Wolf         *info = (BlockExportInfo) {
3528cade320SKevin Wolf             .id             = g_strdup(exp->id),
3538cade320SKevin Wolf             .type           = exp->drv->type,
3548cade320SKevin Wolf             .node_name      = g_strdup(bdrv_get_node_name(blk_bs(exp->blk))),
3558cade320SKevin Wolf             .shutting_down  = !exp->user_owned,
3568cade320SKevin Wolf         };
3578cade320SKevin Wolf 
3588cade320SKevin Wolf         entry->value = info;
3598cade320SKevin Wolf         *p_next = entry;
3608cade320SKevin Wolf         p_next = &entry->next;
3618cade320SKevin Wolf     }
3628cade320SKevin Wolf 
3638cade320SKevin Wolf     return head;
3648cade320SKevin Wolf }
365