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" 200c9b70d5SMax 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 292a2359b8SXie Yongji #ifdef CONFIG_VDUSE_BLK_EXPORT 302a2359b8SXie Yongji #include "vduse-blk.h" 312a2359b8SXie Yongji #endif 3256ee8626SKevin Wolf 3356ee8626SKevin Wolf static const BlockExportDriver *blk_exp_drivers[] = { 3456ee8626SKevin Wolf &blk_exp_nbd, 35bc15e44cSStefan Hajnoczi #ifdef CONFIG_VHOST_USER_BLK_SERVER 3690fc91d5SStefan Hajnoczi &blk_exp_vhost_user_blk, 3790fc91d5SStefan Hajnoczi #endif 380c9b70d5SMax Reitz #ifdef CONFIG_FUSE 390c9b70d5SMax Reitz &blk_exp_fuse, 400c9b70d5SMax Reitz #endif 412a2359b8SXie Yongji #ifdef CONFIG_VDUSE_BLK_EXPORT 422a2359b8SXie Yongji &blk_exp_vduse_blk, 432a2359b8SXie Yongji #endif 4456ee8626SKevin Wolf }; 4556ee8626SKevin Wolf 46bc4ee65bSKevin Wolf /* Only accessed from the main thread */ 47bc4ee65bSKevin Wolf static QLIST_HEAD(, BlockExport) block_exports = 48bc4ee65bSKevin Wolf QLIST_HEAD_INITIALIZER(block_exports); 49bc4ee65bSKevin Wolf 503c3bc462SKevin Wolf BlockExport *blk_exp_find(const char *id) 51d53be9ceSKevin Wolf { 52d53be9ceSKevin Wolf BlockExport *exp; 53d53be9ceSKevin Wolf 54d53be9ceSKevin Wolf QLIST_FOREACH(exp, &block_exports, next) { 55d53be9ceSKevin Wolf if (strcmp(id, exp->id) == 0) { 56d53be9ceSKevin Wolf return exp; 57d53be9ceSKevin Wolf } 58d53be9ceSKevin Wolf } 59d53be9ceSKevin Wolf 60d53be9ceSKevin Wolf return NULL; 61d53be9ceSKevin Wolf } 62d53be9ceSKevin Wolf 6356ee8626SKevin Wolf static const BlockExportDriver *blk_exp_find_driver(BlockExportType type) 6456ee8626SKevin Wolf { 6556ee8626SKevin Wolf int i; 6656ee8626SKevin Wolf 6756ee8626SKevin Wolf for (i = 0; i < ARRAY_SIZE(blk_exp_drivers); i++) { 6856ee8626SKevin Wolf if (blk_exp_drivers[i]->type == type) { 6956ee8626SKevin Wolf return blk_exp_drivers[i]; 7056ee8626SKevin Wolf } 7156ee8626SKevin Wolf } 7256ee8626SKevin Wolf return NULL; 7356ee8626SKevin Wolf } 7456ee8626SKevin Wolf 759b562c64SKevin Wolf BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp) 7656ee8626SKevin Wolf { 77f51d23c8SStefan Hajnoczi bool fixed_iothread = export->has_fixed_iothread && export->fixed_iothread; 7856ee8626SKevin Wolf const BlockExportDriver *drv; 79331170e0SKevin Wolf BlockExport *exp = NULL; 80331170e0SKevin Wolf BlockDriverState *bs; 81f51d23c8SStefan Hajnoczi BlockBackend *blk = NULL; 82331170e0SKevin Wolf AioContext *ctx; 8330dbc81dSKevin Wolf uint64_t perm; 84a6ff7989SKevin Wolf int ret; 8556ee8626SKevin Wolf 86*2b3912f1SKevin Wolf GLOBAL_STATE_CODE(); 87*2b3912f1SKevin Wolf 88d53be9ceSKevin Wolf if (!id_wellformed(export->id)) { 89d53be9ceSKevin Wolf error_setg(errp, "Invalid block export id"); 90d53be9ceSKevin Wolf return NULL; 91d53be9ceSKevin Wolf } 92d53be9ceSKevin Wolf if (blk_exp_find(export->id)) { 93d53be9ceSKevin Wolf error_setg(errp, "Block export id '%s' is already in use", export->id); 94d53be9ceSKevin Wolf return NULL; 95d53be9ceSKevin Wolf } 96d53be9ceSKevin Wolf 9756ee8626SKevin Wolf drv = blk_exp_find_driver(export->type); 9856ee8626SKevin Wolf if (!drv) { 9956ee8626SKevin Wolf error_setg(errp, "No driver found for the requested export type"); 1009b562c64SKevin Wolf return NULL; 10156ee8626SKevin Wolf } 10256ee8626SKevin Wolf 103331170e0SKevin Wolf bs = bdrv_lookup_bs(NULL, export->node_name, errp); 104331170e0SKevin Wolf if (!bs) { 105331170e0SKevin Wolf return NULL; 106331170e0SKevin Wolf } 107331170e0SKevin Wolf 10830dbc81dSKevin Wolf if (!export->has_writable) { 10930dbc81dSKevin Wolf export->writable = false; 11030dbc81dSKevin Wolf } 11130dbc81dSKevin Wolf if (bdrv_is_read_only(bs) && export->writable) { 11230dbc81dSKevin Wolf error_setg(errp, "Cannot export read-only node as writable"); 11330dbc81dSKevin Wolf return NULL; 11430dbc81dSKevin Wolf } 11530dbc81dSKevin Wolf 116331170e0SKevin Wolf ctx = bdrv_get_aio_context(bs); 117331170e0SKevin Wolf aio_context_acquire(ctx); 118331170e0SKevin Wolf 11954fde4ffSMarkus Armbruster if (export->iothread) { 120f51d23c8SStefan Hajnoczi IOThread *iothread; 121f51d23c8SStefan Hajnoczi AioContext *new_ctx; 1228573823fSMax Reitz Error **set_context_errp; 123f51d23c8SStefan Hajnoczi 124f51d23c8SStefan Hajnoczi iothread = iothread_by_id(export->iothread); 125f51d23c8SStefan Hajnoczi if (!iothread) { 126f51d23c8SStefan Hajnoczi error_setg(errp, "iothread \"%s\" not found", export->iothread); 127f51d23c8SStefan Hajnoczi goto fail; 128f51d23c8SStefan Hajnoczi } 129f51d23c8SStefan Hajnoczi 130f51d23c8SStefan Hajnoczi new_ctx = iothread_get_aio_context(iothread); 131f51d23c8SStefan Hajnoczi 1328573823fSMax Reitz /* Ignore errors with fixed-iothread=false */ 1338573823fSMax Reitz set_context_errp = fixed_iothread ? errp : NULL; 134142e6907SEmanuele Giuseppe Esposito ret = bdrv_try_change_aio_context(bs, new_ctx, NULL, set_context_errp); 135f51d23c8SStefan Hajnoczi if (ret == 0) { 136f51d23c8SStefan Hajnoczi aio_context_release(ctx); 137f51d23c8SStefan Hajnoczi aio_context_acquire(new_ctx); 138f51d23c8SStefan Hajnoczi ctx = new_ctx; 139f51d23c8SStefan Hajnoczi } else if (fixed_iothread) { 140f51d23c8SStefan Hajnoczi goto fail; 141f51d23c8SStefan Hajnoczi } 142f51d23c8SStefan Hajnoczi } 143f51d23c8SStefan Hajnoczi 144331170e0SKevin Wolf /* 145331170e0SKevin Wolf * Block exports are used for non-shared storage migration. Make sure 146331170e0SKevin Wolf * that BDRV_O_INACTIVE is cleared and the image is ready for write 147331170e0SKevin Wolf * access since the export could be available before migration handover. 148331170e0SKevin Wolf * ctx was acquired in the caller. 149331170e0SKevin Wolf */ 150*2b3912f1SKevin Wolf bdrv_graph_rdlock_main_loop(); 151a94750d9SEmanuele Giuseppe Esposito bdrv_activate(bs, NULL); 152*2b3912f1SKevin Wolf bdrv_graph_rdunlock_main_loop(); 153331170e0SKevin Wolf 15430dbc81dSKevin Wolf perm = BLK_PERM_CONSISTENT_READ; 15530dbc81dSKevin Wolf if (export->writable) { 15630dbc81dSKevin Wolf perm |= BLK_PERM_WRITE; 15730dbc81dSKevin Wolf } 15830dbc81dSKevin Wolf 15930dbc81dSKevin Wolf blk = blk_new(ctx, perm, BLK_PERM_ALL); 160f51d23c8SStefan Hajnoczi 161f51d23c8SStefan Hajnoczi if (!fixed_iothread) { 162f51d23c8SStefan Hajnoczi blk_set_allow_aio_context_change(blk, true); 163f51d23c8SStefan Hajnoczi } 164f51d23c8SStefan Hajnoczi 165331170e0SKevin Wolf ret = blk_insert_bs(blk, bs, errp); 166331170e0SKevin Wolf if (ret < 0) { 167331170e0SKevin Wolf goto fail; 168331170e0SKevin Wolf } 169331170e0SKevin Wolf 170331170e0SKevin Wolf if (!export->has_writethrough) { 171331170e0SKevin Wolf export->writethrough = false; 172331170e0SKevin Wolf } 173331170e0SKevin Wolf blk_set_enable_write_cache(blk, !export->writethrough); 174331170e0SKevin Wolf 175a6ff7989SKevin Wolf assert(drv->instance_size >= sizeof(BlockExport)); 176a6ff7989SKevin Wolf exp = g_malloc0(drv->instance_size); 177a6ff7989SKevin Wolf *exp = (BlockExport) { 178a6ff7989SKevin Wolf .drv = drv, 179a6ff7989SKevin Wolf .refcount = 1, 1803859ad36SKevin Wolf .user_owned = true, 181d53be9ceSKevin Wolf .id = g_strdup(export->id), 182331170e0SKevin Wolf .ctx = ctx, 183331170e0SKevin Wolf .blk = blk, 184a6ff7989SKevin Wolf }; 185a6ff7989SKevin Wolf 186a6ff7989SKevin Wolf ret = drv->create(exp, export, errp); 187a6ff7989SKevin Wolf if (ret < 0) { 188331170e0SKevin Wolf goto fail; 189a6ff7989SKevin Wolf } 190a6ff7989SKevin Wolf 19137a4f70cSKevin Wolf assert(exp->blk != NULL); 19237a4f70cSKevin Wolf 193bc4ee65bSKevin Wolf QLIST_INSERT_HEAD(&block_exports, exp, next); 194331170e0SKevin Wolf 195331170e0SKevin Wolf aio_context_release(ctx); 196a6ff7989SKevin Wolf return exp; 197331170e0SKevin Wolf 198331170e0SKevin Wolf fail: 199a1845637SKevin Wolf if (blk) { 200a1845637SKevin Wolf blk_set_dev_ops(blk, NULL, NULL); 201331170e0SKevin Wolf blk_unref(blk); 202a1845637SKevin Wolf } 203331170e0SKevin Wolf aio_context_release(ctx); 204331170e0SKevin Wolf if (exp) { 205331170e0SKevin Wolf g_free(exp->id); 206331170e0SKevin Wolf g_free(exp); 207331170e0SKevin Wolf } 208331170e0SKevin Wolf return NULL; 2099b562c64SKevin Wolf } 2109b562c64SKevin Wolf 211c69de1beSKevin Wolf void blk_exp_ref(BlockExport *exp) 212c69de1beSKevin Wolf { 2133d499a43SStefan Hajnoczi assert(qatomic_read(&exp->refcount) > 0); 2143d499a43SStefan Hajnoczi qatomic_inc(&exp->refcount); 215c69de1beSKevin Wolf } 216c69de1beSKevin Wolf 217bc4ee65bSKevin Wolf /* Runs in the main thread */ 218bc4ee65bSKevin Wolf static void blk_exp_delete_bh(void *opaque) 219bc4ee65bSKevin Wolf { 220bc4ee65bSKevin Wolf BlockExport *exp = opaque; 221bc4ee65bSKevin Wolf AioContext *aio_context = exp->ctx; 222bc4ee65bSKevin Wolf 223bc4ee65bSKevin Wolf aio_context_acquire(aio_context); 224bc4ee65bSKevin Wolf 225bc4ee65bSKevin Wolf assert(exp->refcount == 0); 226bc4ee65bSKevin Wolf QLIST_REMOVE(exp, next); 227bc4ee65bSKevin Wolf exp->drv->delete(exp); 228de79b526SStefan Hajnoczi blk_set_dev_ops(exp->blk, NULL, NULL); 22937a4f70cSKevin Wolf blk_unref(exp->blk); 2301a9f7a80SKevin Wolf qapi_event_send_block_export_deleted(exp->id); 231d53be9ceSKevin Wolf g_free(exp->id); 232bc4ee65bSKevin Wolf g_free(exp); 233bc4ee65bSKevin Wolf 234bc4ee65bSKevin Wolf aio_context_release(aio_context); 235bc4ee65bSKevin Wolf } 236bc4ee65bSKevin Wolf 237c69de1beSKevin Wolf void blk_exp_unref(BlockExport *exp) 238c69de1beSKevin Wolf { 2393d499a43SStefan Hajnoczi assert(qatomic_read(&exp->refcount) > 0); 2403d499a43SStefan Hajnoczi if (qatomic_fetch_dec(&exp->refcount) == 1) { 241bc4ee65bSKevin Wolf /* Touch the block_exports list only in the main thread */ 242bc4ee65bSKevin Wolf aio_bh_schedule_oneshot(qemu_get_aio_context(), blk_exp_delete_bh, 243bc4ee65bSKevin Wolf exp); 244c69de1beSKevin Wolf } 245c69de1beSKevin Wolf } 246c69de1beSKevin Wolf 247bc4ee65bSKevin Wolf /* 248bc4ee65bSKevin Wolf * Drops the user reference to the export and requests that all client 249bc4ee65bSKevin Wolf * connections and other internally held references start to shut down. When 250bc4ee65bSKevin Wolf * the function returns, there may still be active references while the export 251bc4ee65bSKevin Wolf * is in the process of shutting down. 252bc4ee65bSKevin Wolf * 253bc4ee65bSKevin Wolf * Acquires exp->ctx internally. Callers must *not* hold the lock. 254bc4ee65bSKevin Wolf */ 255bc4ee65bSKevin Wolf void blk_exp_request_shutdown(BlockExport *exp) 256bc4ee65bSKevin Wolf { 257bc4ee65bSKevin Wolf AioContext *aio_context = exp->ctx; 258bc4ee65bSKevin Wolf 259bc4ee65bSKevin Wolf aio_context_acquire(aio_context); 2603c3bc462SKevin Wolf 2613c3bc462SKevin Wolf /* 2623c3bc462SKevin Wolf * If the user doesn't own the export any more, it is already shutting 2633c3bc462SKevin Wolf * down. We must not call .request_shutdown and decrease the refcount a 2643c3bc462SKevin Wolf * second time. 2653c3bc462SKevin Wolf */ 2663c3bc462SKevin Wolf if (!exp->user_owned) { 2673c3bc462SKevin Wolf goto out; 2683c3bc462SKevin Wolf } 2693c3bc462SKevin Wolf 270bc4ee65bSKevin Wolf exp->drv->request_shutdown(exp); 2713859ad36SKevin Wolf 2723859ad36SKevin Wolf assert(exp->user_owned); 2733859ad36SKevin Wolf exp->user_owned = false; 2743859ad36SKevin Wolf blk_exp_unref(exp); 2753859ad36SKevin Wolf 2763c3bc462SKevin Wolf out: 277bc4ee65bSKevin Wolf aio_context_release(aio_context); 278bc4ee65bSKevin Wolf } 279bc4ee65bSKevin Wolf 280bc4ee65bSKevin Wolf /* 281bc4ee65bSKevin Wolf * Returns whether a block export of the given type exists. 282bc4ee65bSKevin Wolf * type == BLOCK_EXPORT_TYPE__MAX checks for an export of any type. 283bc4ee65bSKevin Wolf */ 284bc4ee65bSKevin Wolf static bool blk_exp_has_type(BlockExportType type) 285bc4ee65bSKevin Wolf { 286bc4ee65bSKevin Wolf BlockExport *exp; 287bc4ee65bSKevin Wolf 288bc4ee65bSKevin Wolf if (type == BLOCK_EXPORT_TYPE__MAX) { 289bc4ee65bSKevin Wolf return !QLIST_EMPTY(&block_exports); 290bc4ee65bSKevin Wolf } 291bc4ee65bSKevin Wolf 292bc4ee65bSKevin Wolf QLIST_FOREACH(exp, &block_exports, next) { 293bc4ee65bSKevin Wolf if (exp->drv->type == type) { 294bc4ee65bSKevin Wolf return true; 295bc4ee65bSKevin Wolf } 296bc4ee65bSKevin Wolf } 297bc4ee65bSKevin Wolf 298bc4ee65bSKevin Wolf return false; 299bc4ee65bSKevin Wolf } 300bc4ee65bSKevin Wolf 301bc4ee65bSKevin Wolf /* type == BLOCK_EXPORT_TYPE__MAX for all types */ 302bc4ee65bSKevin Wolf void blk_exp_close_all_type(BlockExportType type) 303bc4ee65bSKevin Wolf { 304bc4ee65bSKevin Wolf BlockExport *exp, *next; 305bc4ee65bSKevin Wolf 306bc4ee65bSKevin Wolf assert(in_aio_context_home_thread(qemu_get_aio_context())); 307bc4ee65bSKevin Wolf 308bc4ee65bSKevin Wolf QLIST_FOREACH_SAFE(exp, &block_exports, next, next) { 309bc4ee65bSKevin Wolf if (type != BLOCK_EXPORT_TYPE__MAX && exp->drv->type != type) { 310bc4ee65bSKevin Wolf continue; 311bc4ee65bSKevin Wolf } 312bc4ee65bSKevin Wolf blk_exp_request_shutdown(exp); 313bc4ee65bSKevin Wolf } 314bc4ee65bSKevin Wolf 315e5568a66SStefan Hajnoczi AIO_WAIT_WHILE_UNLOCKED(NULL, blk_exp_has_type(type)); 316bc4ee65bSKevin Wolf } 317bc4ee65bSKevin Wolf 318bc4ee65bSKevin Wolf void blk_exp_close_all(void) 319bc4ee65bSKevin Wolf { 320bc4ee65bSKevin Wolf blk_exp_close_all_type(BLOCK_EXPORT_TYPE__MAX); 321bc4ee65bSKevin Wolf } 322bc4ee65bSKevin Wolf 3239b562c64SKevin Wolf void qmp_block_export_add(BlockExportOptions *export, Error **errp) 3249b562c64SKevin Wolf { 3259b562c64SKevin Wolf blk_exp_add(export, errp); 32656ee8626SKevin Wolf } 3273c3bc462SKevin Wolf 3283c3bc462SKevin Wolf void qmp_block_export_del(const char *id, 3293c3bc462SKevin Wolf bool has_mode, BlockExportRemoveMode mode, 3303c3bc462SKevin Wolf Error **errp) 3313c3bc462SKevin Wolf { 3323c3bc462SKevin Wolf ERRP_GUARD(); 3333c3bc462SKevin Wolf BlockExport *exp; 3343c3bc462SKevin Wolf 3353c3bc462SKevin Wolf exp = blk_exp_find(id); 3363c3bc462SKevin Wolf if (exp == NULL) { 3373c3bc462SKevin Wolf error_setg(errp, "Export '%s' is not found", id); 3383c3bc462SKevin Wolf return; 3393c3bc462SKevin Wolf } 3403c3bc462SKevin Wolf if (!exp->user_owned) { 3413c3bc462SKevin Wolf error_setg(errp, "Export '%s' is already shutting down", id); 3423c3bc462SKevin Wolf return; 3433c3bc462SKevin Wolf } 3443c3bc462SKevin Wolf 3453c3bc462SKevin Wolf if (!has_mode) { 3463c3bc462SKevin Wolf mode = BLOCK_EXPORT_REMOVE_MODE_SAFE; 3473c3bc462SKevin Wolf } 3483d499a43SStefan Hajnoczi if (mode == BLOCK_EXPORT_REMOVE_MODE_SAFE && 3493d499a43SStefan Hajnoczi qatomic_read(&exp->refcount) > 1) { 3503c3bc462SKevin Wolf error_setg(errp, "export '%s' still in use", exp->id); 3513c3bc462SKevin Wolf error_append_hint(errp, "Use mode='hard' to force client " 3523c3bc462SKevin Wolf "disconnect\n"); 3533c3bc462SKevin Wolf return; 3543c3bc462SKevin Wolf } 3553c3bc462SKevin Wolf 3563c3bc462SKevin Wolf blk_exp_request_shutdown(exp); 3573c3bc462SKevin Wolf } 3588cade320SKevin Wolf 3598cade320SKevin Wolf BlockExportInfoList *qmp_query_block_exports(Error **errp) 3608cade320SKevin Wolf { 361c3033fd3SEric Blake BlockExportInfoList *head = NULL, **tail = &head; 3628cade320SKevin Wolf BlockExport *exp; 3638cade320SKevin Wolf 3648cade320SKevin Wolf QLIST_FOREACH(exp, &block_exports, next) { 3658cade320SKevin Wolf BlockExportInfo *info = g_new(BlockExportInfo, 1); 3668cade320SKevin Wolf *info = (BlockExportInfo) { 3678cade320SKevin Wolf .id = g_strdup(exp->id), 3688cade320SKevin Wolf .type = exp->drv->type, 3698cade320SKevin Wolf .node_name = g_strdup(bdrv_get_node_name(blk_bs(exp->blk))), 3708cade320SKevin Wolf .shutting_down = !exp->user_owned, 3718cade320SKevin Wolf }; 3728cade320SKevin Wolf 373c3033fd3SEric Blake QAPI_LIST_APPEND(tail, info); 3748cade320SKevin Wolf } 3758cade320SKevin Wolf 3768cade320SKevin Wolf return head; 3778cade320SKevin Wolf } 378