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" 1856ee8626SKevin Wolf #include "block/export.h" 1956ee8626SKevin Wolf #include "block/nbd.h" 2056ee8626SKevin Wolf #include "qapi/error.h" 2156ee8626SKevin Wolf #include "qapi/qapi-commands-block-export.h" 221a9f7a80SKevin Wolf #include "qapi/qapi-events-block-export.h" 23d53be9ceSKevin Wolf #include "qemu/id.h" 24*3a213f83SStefan Hajnoczi #if defined(CONFIG_LINUX) && defined(CONFIG_VHOST_USER) 25*3a213f83SStefan Hajnoczi #include "vhost-user-blk-server.h" 26*3a213f83SStefan Hajnoczi #endif 2756ee8626SKevin Wolf 2856ee8626SKevin Wolf static const BlockExportDriver *blk_exp_drivers[] = { 2956ee8626SKevin Wolf &blk_exp_nbd, 30*3a213f83SStefan Hajnoczi #if defined(CONFIG_LINUX) && defined(CONFIG_VHOST_USER) 3190fc91d5SStefan Hajnoczi &blk_exp_vhost_user_blk, 3290fc91d5SStefan Hajnoczi #endif 3356ee8626SKevin Wolf }; 3456ee8626SKevin Wolf 35bc4ee65bSKevin Wolf /* Only accessed from the main thread */ 36bc4ee65bSKevin Wolf static QLIST_HEAD(, BlockExport) block_exports = 37bc4ee65bSKevin Wolf QLIST_HEAD_INITIALIZER(block_exports); 38bc4ee65bSKevin Wolf 393c3bc462SKevin Wolf BlockExport *blk_exp_find(const char *id) 40d53be9ceSKevin Wolf { 41d53be9ceSKevin Wolf BlockExport *exp; 42d53be9ceSKevin Wolf 43d53be9ceSKevin Wolf QLIST_FOREACH(exp, &block_exports, next) { 44d53be9ceSKevin Wolf if (strcmp(id, exp->id) == 0) { 45d53be9ceSKevin Wolf return exp; 46d53be9ceSKevin Wolf } 47d53be9ceSKevin Wolf } 48d53be9ceSKevin Wolf 49d53be9ceSKevin Wolf return NULL; 50d53be9ceSKevin Wolf } 51d53be9ceSKevin Wolf 5256ee8626SKevin Wolf static const BlockExportDriver *blk_exp_find_driver(BlockExportType type) 5356ee8626SKevin Wolf { 5456ee8626SKevin Wolf int i; 5556ee8626SKevin Wolf 5656ee8626SKevin Wolf for (i = 0; i < ARRAY_SIZE(blk_exp_drivers); i++) { 5756ee8626SKevin Wolf if (blk_exp_drivers[i]->type == type) { 5856ee8626SKevin Wolf return blk_exp_drivers[i]; 5956ee8626SKevin Wolf } 6056ee8626SKevin Wolf } 6156ee8626SKevin Wolf return NULL; 6256ee8626SKevin Wolf } 6356ee8626SKevin Wolf 649b562c64SKevin Wolf BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp) 6556ee8626SKevin Wolf { 6656ee8626SKevin Wolf const BlockExportDriver *drv; 67331170e0SKevin Wolf BlockExport *exp = NULL; 68331170e0SKevin Wolf BlockDriverState *bs; 69331170e0SKevin Wolf BlockBackend *blk; 70331170e0SKevin Wolf AioContext *ctx; 7130dbc81dSKevin Wolf uint64_t perm; 72a6ff7989SKevin Wolf int ret; 7356ee8626SKevin Wolf 74d53be9ceSKevin Wolf if (!id_wellformed(export->id)) { 75d53be9ceSKevin Wolf error_setg(errp, "Invalid block export id"); 76d53be9ceSKevin Wolf return NULL; 77d53be9ceSKevin Wolf } 78d53be9ceSKevin Wolf if (blk_exp_find(export->id)) { 79d53be9ceSKevin Wolf error_setg(errp, "Block export id '%s' is already in use", export->id); 80d53be9ceSKevin Wolf return NULL; 81d53be9ceSKevin Wolf } 82d53be9ceSKevin Wolf 8356ee8626SKevin Wolf drv = blk_exp_find_driver(export->type); 8456ee8626SKevin Wolf if (!drv) { 8556ee8626SKevin Wolf error_setg(errp, "No driver found for the requested export type"); 869b562c64SKevin Wolf return NULL; 8756ee8626SKevin Wolf } 8856ee8626SKevin Wolf 89331170e0SKevin Wolf bs = bdrv_lookup_bs(NULL, export->node_name, errp); 90331170e0SKevin Wolf if (!bs) { 91331170e0SKevin Wolf return NULL; 92331170e0SKevin Wolf } 93331170e0SKevin Wolf 9430dbc81dSKevin Wolf if (!export->has_writable) { 9530dbc81dSKevin Wolf export->writable = false; 9630dbc81dSKevin Wolf } 9730dbc81dSKevin Wolf if (bdrv_is_read_only(bs) && export->writable) { 9830dbc81dSKevin Wolf error_setg(errp, "Cannot export read-only node as writable"); 9930dbc81dSKevin Wolf return NULL; 10030dbc81dSKevin Wolf } 10130dbc81dSKevin Wolf 102331170e0SKevin Wolf ctx = bdrv_get_aio_context(bs); 103331170e0SKevin Wolf aio_context_acquire(ctx); 104331170e0SKevin Wolf 105331170e0SKevin Wolf /* 106331170e0SKevin Wolf * Block exports are used for non-shared storage migration. Make sure 107331170e0SKevin Wolf * that BDRV_O_INACTIVE is cleared and the image is ready for write 108331170e0SKevin Wolf * access since the export could be available before migration handover. 109331170e0SKevin Wolf * ctx was acquired in the caller. 110331170e0SKevin Wolf */ 111331170e0SKevin Wolf bdrv_invalidate_cache(bs, NULL); 112331170e0SKevin Wolf 11330dbc81dSKevin Wolf perm = BLK_PERM_CONSISTENT_READ; 11430dbc81dSKevin Wolf if (export->writable) { 11530dbc81dSKevin Wolf perm |= BLK_PERM_WRITE; 11630dbc81dSKevin Wolf } 11730dbc81dSKevin Wolf 11830dbc81dSKevin Wolf blk = blk_new(ctx, perm, BLK_PERM_ALL); 119331170e0SKevin Wolf ret = blk_insert_bs(blk, bs, errp); 120331170e0SKevin Wolf if (ret < 0) { 121331170e0SKevin Wolf goto fail; 122331170e0SKevin Wolf } 123331170e0SKevin Wolf 124331170e0SKevin Wolf if (!export->has_writethrough) { 125331170e0SKevin Wolf export->writethrough = false; 126331170e0SKevin Wolf } 127331170e0SKevin Wolf blk_set_enable_write_cache(blk, !export->writethrough); 128331170e0SKevin Wolf 129a6ff7989SKevin Wolf assert(drv->instance_size >= sizeof(BlockExport)); 130a6ff7989SKevin Wolf exp = g_malloc0(drv->instance_size); 131a6ff7989SKevin Wolf *exp = (BlockExport) { 132a6ff7989SKevin Wolf .drv = drv, 133a6ff7989SKevin Wolf .refcount = 1, 1343859ad36SKevin Wolf .user_owned = true, 135d53be9ceSKevin Wolf .id = g_strdup(export->id), 136331170e0SKevin Wolf .ctx = ctx, 137331170e0SKevin Wolf .blk = blk, 138a6ff7989SKevin Wolf }; 139a6ff7989SKevin Wolf 140a6ff7989SKevin Wolf ret = drv->create(exp, export, errp); 141a6ff7989SKevin Wolf if (ret < 0) { 142331170e0SKevin Wolf goto fail; 143a6ff7989SKevin Wolf } 144a6ff7989SKevin Wolf 14537a4f70cSKevin Wolf assert(exp->blk != NULL); 14637a4f70cSKevin Wolf 147bc4ee65bSKevin Wolf QLIST_INSERT_HEAD(&block_exports, exp, next); 148331170e0SKevin Wolf 149331170e0SKevin Wolf aio_context_release(ctx); 150a6ff7989SKevin Wolf return exp; 151331170e0SKevin Wolf 152331170e0SKevin Wolf fail: 153331170e0SKevin Wolf blk_unref(blk); 154331170e0SKevin Wolf aio_context_release(ctx); 155331170e0SKevin Wolf if (exp) { 156331170e0SKevin Wolf g_free(exp->id); 157331170e0SKevin Wolf g_free(exp); 158331170e0SKevin Wolf } 159331170e0SKevin Wolf return NULL; 1609b562c64SKevin Wolf } 1619b562c64SKevin Wolf 1628612c686SKevin Wolf /* Callers must hold exp->ctx lock */ 163c69de1beSKevin Wolf void blk_exp_ref(BlockExport *exp) 164c69de1beSKevin Wolf { 165c69de1beSKevin Wolf assert(exp->refcount > 0); 166c69de1beSKevin Wolf exp->refcount++; 167c69de1beSKevin Wolf } 168c69de1beSKevin Wolf 169bc4ee65bSKevin Wolf /* Runs in the main thread */ 170bc4ee65bSKevin Wolf static void blk_exp_delete_bh(void *opaque) 171bc4ee65bSKevin Wolf { 172bc4ee65bSKevin Wolf BlockExport *exp = opaque; 173bc4ee65bSKevin Wolf AioContext *aio_context = exp->ctx; 174bc4ee65bSKevin Wolf 175bc4ee65bSKevin Wolf aio_context_acquire(aio_context); 176bc4ee65bSKevin Wolf 177bc4ee65bSKevin Wolf assert(exp->refcount == 0); 178bc4ee65bSKevin Wolf QLIST_REMOVE(exp, next); 179bc4ee65bSKevin Wolf exp->drv->delete(exp); 18037a4f70cSKevin Wolf blk_unref(exp->blk); 1811a9f7a80SKevin Wolf qapi_event_send_block_export_deleted(exp->id); 182d53be9ceSKevin Wolf g_free(exp->id); 183bc4ee65bSKevin Wolf g_free(exp); 184bc4ee65bSKevin Wolf 185bc4ee65bSKevin Wolf aio_context_release(aio_context); 186bc4ee65bSKevin Wolf } 187bc4ee65bSKevin Wolf 1888612c686SKevin Wolf /* Callers must hold exp->ctx lock */ 189c69de1beSKevin Wolf void blk_exp_unref(BlockExport *exp) 190c69de1beSKevin Wolf { 191c69de1beSKevin Wolf assert(exp->refcount > 0); 192c69de1beSKevin Wolf if (--exp->refcount == 0) { 193bc4ee65bSKevin Wolf /* Touch the block_exports list only in the main thread */ 194bc4ee65bSKevin Wolf aio_bh_schedule_oneshot(qemu_get_aio_context(), blk_exp_delete_bh, 195bc4ee65bSKevin Wolf exp); 196c69de1beSKevin Wolf } 197c69de1beSKevin Wolf } 198c69de1beSKevin Wolf 199bc4ee65bSKevin Wolf /* 200bc4ee65bSKevin Wolf * Drops the user reference to the export and requests that all client 201bc4ee65bSKevin Wolf * connections and other internally held references start to shut down. When 202bc4ee65bSKevin Wolf * the function returns, there may still be active references while the export 203bc4ee65bSKevin Wolf * is in the process of shutting down. 204bc4ee65bSKevin Wolf * 205bc4ee65bSKevin Wolf * Acquires exp->ctx internally. Callers must *not* hold the lock. 206bc4ee65bSKevin Wolf */ 207bc4ee65bSKevin Wolf void blk_exp_request_shutdown(BlockExport *exp) 208bc4ee65bSKevin Wolf { 209bc4ee65bSKevin Wolf AioContext *aio_context = exp->ctx; 210bc4ee65bSKevin Wolf 211bc4ee65bSKevin Wolf aio_context_acquire(aio_context); 2123c3bc462SKevin Wolf 2133c3bc462SKevin Wolf /* 2143c3bc462SKevin Wolf * If the user doesn't own the export any more, it is already shutting 2153c3bc462SKevin Wolf * down. We must not call .request_shutdown and decrease the refcount a 2163c3bc462SKevin Wolf * second time. 2173c3bc462SKevin Wolf */ 2183c3bc462SKevin Wolf if (!exp->user_owned) { 2193c3bc462SKevin Wolf goto out; 2203c3bc462SKevin Wolf } 2213c3bc462SKevin Wolf 222bc4ee65bSKevin Wolf exp->drv->request_shutdown(exp); 2233859ad36SKevin Wolf 2243859ad36SKevin Wolf assert(exp->user_owned); 2253859ad36SKevin Wolf exp->user_owned = false; 2263859ad36SKevin Wolf blk_exp_unref(exp); 2273859ad36SKevin Wolf 2283c3bc462SKevin Wolf out: 229bc4ee65bSKevin Wolf aio_context_release(aio_context); 230bc4ee65bSKevin Wolf } 231bc4ee65bSKevin Wolf 232bc4ee65bSKevin Wolf /* 233bc4ee65bSKevin Wolf * Returns whether a block export of the given type exists. 234bc4ee65bSKevin Wolf * type == BLOCK_EXPORT_TYPE__MAX checks for an export of any type. 235bc4ee65bSKevin Wolf */ 236bc4ee65bSKevin Wolf static bool blk_exp_has_type(BlockExportType type) 237bc4ee65bSKevin Wolf { 238bc4ee65bSKevin Wolf BlockExport *exp; 239bc4ee65bSKevin Wolf 240bc4ee65bSKevin Wolf if (type == BLOCK_EXPORT_TYPE__MAX) { 241bc4ee65bSKevin Wolf return !QLIST_EMPTY(&block_exports); 242bc4ee65bSKevin Wolf } 243bc4ee65bSKevin Wolf 244bc4ee65bSKevin Wolf QLIST_FOREACH(exp, &block_exports, next) { 245bc4ee65bSKevin Wolf if (exp->drv->type == type) { 246bc4ee65bSKevin Wolf return true; 247bc4ee65bSKevin Wolf } 248bc4ee65bSKevin Wolf } 249bc4ee65bSKevin Wolf 250bc4ee65bSKevin Wolf return false; 251bc4ee65bSKevin Wolf } 252bc4ee65bSKevin Wolf 253bc4ee65bSKevin Wolf /* type == BLOCK_EXPORT_TYPE__MAX for all types */ 254bc4ee65bSKevin Wolf void blk_exp_close_all_type(BlockExportType type) 255bc4ee65bSKevin Wolf { 256bc4ee65bSKevin Wolf BlockExport *exp, *next; 257bc4ee65bSKevin Wolf 258bc4ee65bSKevin Wolf assert(in_aio_context_home_thread(qemu_get_aio_context())); 259bc4ee65bSKevin Wolf 260bc4ee65bSKevin Wolf QLIST_FOREACH_SAFE(exp, &block_exports, next, next) { 261bc4ee65bSKevin Wolf if (type != BLOCK_EXPORT_TYPE__MAX && exp->drv->type != type) { 262bc4ee65bSKevin Wolf continue; 263bc4ee65bSKevin Wolf } 264bc4ee65bSKevin Wolf blk_exp_request_shutdown(exp); 265bc4ee65bSKevin Wolf } 266bc4ee65bSKevin Wolf 267bc4ee65bSKevin Wolf AIO_WAIT_WHILE(NULL, blk_exp_has_type(type)); 268bc4ee65bSKevin Wolf } 269bc4ee65bSKevin Wolf 270bc4ee65bSKevin Wolf void blk_exp_close_all(void) 271bc4ee65bSKevin Wolf { 272bc4ee65bSKevin Wolf blk_exp_close_all_type(BLOCK_EXPORT_TYPE__MAX); 273bc4ee65bSKevin Wolf } 274bc4ee65bSKevin Wolf 2759b562c64SKevin Wolf void qmp_block_export_add(BlockExportOptions *export, Error **errp) 2769b562c64SKevin Wolf { 2779b562c64SKevin Wolf blk_exp_add(export, errp); 27856ee8626SKevin Wolf } 2793c3bc462SKevin Wolf 2803c3bc462SKevin Wolf void qmp_block_export_del(const char *id, 2813c3bc462SKevin Wolf bool has_mode, BlockExportRemoveMode mode, 2823c3bc462SKevin Wolf Error **errp) 2833c3bc462SKevin Wolf { 2843c3bc462SKevin Wolf ERRP_GUARD(); 2853c3bc462SKevin Wolf BlockExport *exp; 2863c3bc462SKevin Wolf 2873c3bc462SKevin Wolf exp = blk_exp_find(id); 2883c3bc462SKevin Wolf if (exp == NULL) { 2893c3bc462SKevin Wolf error_setg(errp, "Export '%s' is not found", id); 2903c3bc462SKevin Wolf return; 2913c3bc462SKevin Wolf } 2923c3bc462SKevin Wolf if (!exp->user_owned) { 2933c3bc462SKevin Wolf error_setg(errp, "Export '%s' is already shutting down", id); 2943c3bc462SKevin Wolf return; 2953c3bc462SKevin Wolf } 2963c3bc462SKevin Wolf 2973c3bc462SKevin Wolf if (!has_mode) { 2983c3bc462SKevin Wolf mode = BLOCK_EXPORT_REMOVE_MODE_SAFE; 2993c3bc462SKevin Wolf } 3003c3bc462SKevin Wolf if (mode == BLOCK_EXPORT_REMOVE_MODE_SAFE && exp->refcount > 1) { 3013c3bc462SKevin Wolf error_setg(errp, "export '%s' still in use", exp->id); 3023c3bc462SKevin Wolf error_append_hint(errp, "Use mode='hard' to force client " 3033c3bc462SKevin Wolf "disconnect\n"); 3043c3bc462SKevin Wolf return; 3053c3bc462SKevin Wolf } 3063c3bc462SKevin Wolf 3073c3bc462SKevin Wolf blk_exp_request_shutdown(exp); 3083c3bc462SKevin Wolf } 3098cade320SKevin Wolf 3108cade320SKevin Wolf BlockExportInfoList *qmp_query_block_exports(Error **errp) 3118cade320SKevin Wolf { 3128cade320SKevin Wolf BlockExportInfoList *head = NULL, **p_next = &head; 3138cade320SKevin Wolf BlockExport *exp; 3148cade320SKevin Wolf 3158cade320SKevin Wolf QLIST_FOREACH(exp, &block_exports, next) { 3168cade320SKevin Wolf BlockExportInfoList *entry = g_new0(BlockExportInfoList, 1); 3178cade320SKevin Wolf BlockExportInfo *info = g_new(BlockExportInfo, 1); 3188cade320SKevin Wolf *info = (BlockExportInfo) { 3198cade320SKevin Wolf .id = g_strdup(exp->id), 3208cade320SKevin Wolf .type = exp->drv->type, 3218cade320SKevin Wolf .node_name = g_strdup(bdrv_get_node_name(blk_bs(exp->blk))), 3228cade320SKevin Wolf .shutting_down = !exp->user_owned, 3238cade320SKevin Wolf }; 3248cade320SKevin Wolf 3258cade320SKevin Wolf entry->value = info; 3268cade320SKevin Wolf *p_next = entry; 3278cade320SKevin Wolf p_next = &entry->next; 3288cade320SKevin Wolf } 3298cade320SKevin Wolf 3308cade320SKevin Wolf return head; 3318cade320SKevin Wolf } 332