156ee8626SKevin Wolf /* 256ee8626SKevin Wolf * Declarations for block exports 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 #ifndef BLOCK_EXPORT_H 1556ee8626SKevin Wolf #define BLOCK_EXPORT_H 1656ee8626SKevin Wolf 1756ee8626SKevin Wolf #include "qapi/qapi-types-block-export.h" 18bc4ee65bSKevin Wolf #include "qemu/queue.h" 1956ee8626SKevin Wolf 2056ee8626SKevin Wolf typedef struct BlockExport BlockExport; 2156ee8626SKevin Wolf 2256ee8626SKevin Wolf typedef struct BlockExportDriver { 2356ee8626SKevin Wolf /* The export type that this driver services */ 2456ee8626SKevin Wolf BlockExportType type; 2556ee8626SKevin Wolf 26a6ff7989SKevin Wolf /* 27a6ff7989SKevin Wolf * The size of the driver-specific state that contains BlockExport as its 28a6ff7989SKevin Wolf * first field. 29a6ff7989SKevin Wolf */ 30a6ff7989SKevin Wolf size_t instance_size; 31a6ff7989SKevin Wolf 32*1600ef01SKevin Wolf /* True if the export type supports running on an inactive node */ 33*1600ef01SKevin Wolf bool supports_inactive; 34*1600ef01SKevin Wolf 3556ee8626SKevin Wolf /* Creates and starts a new block export */ 36a6ff7989SKevin Wolf int (*create)(BlockExport *, BlockExportOptions *, Error **); 37c69de1beSKevin Wolf 38c69de1beSKevin Wolf /* 39c69de1beSKevin Wolf * Frees a removed block export. This function is only called after all 40c69de1beSKevin Wolf * references have been dropped. 41c69de1beSKevin Wolf */ 42c69de1beSKevin Wolf void (*delete)(BlockExport *); 43bc4ee65bSKevin Wolf 44bc4ee65bSKevin Wolf /* 45bc4ee65bSKevin Wolf * Start to disconnect all clients and drop other references held 46bc4ee65bSKevin Wolf * internally by the export driver. When the function returns, there may 47bc4ee65bSKevin Wolf * still be active references while the export is in the process of 48bc4ee65bSKevin Wolf * shutting down. 49bc4ee65bSKevin Wolf */ 50bc4ee65bSKevin Wolf void (*request_shutdown)(BlockExport *); 5156ee8626SKevin Wolf } BlockExportDriver; 5256ee8626SKevin Wolf 5356ee8626SKevin Wolf struct BlockExport { 5456ee8626SKevin Wolf const BlockExportDriver *drv; 55c69de1beSKevin Wolf 56d53be9ceSKevin Wolf /* Unique identifier for the export */ 57d53be9ceSKevin Wolf char *id; 58d53be9ceSKevin Wolf 59c69de1beSKevin Wolf /* 60c69de1beSKevin Wolf * Reference count for this block export. This includes strong references 61c69de1beSKevin Wolf * both from the owner (qemu-nbd or the monitor) and clients connected to 62c69de1beSKevin Wolf * the export. 633d499a43SStefan Hajnoczi * 643d499a43SStefan Hajnoczi * Use atomics to access this field. 65c69de1beSKevin Wolf */ 66c69de1beSKevin Wolf int refcount; 678612c686SKevin Wolf 683859ad36SKevin Wolf /* 693859ad36SKevin Wolf * True if one of the references in refcount belongs to the user. After the 703859ad36SKevin Wolf * user has dropped their reference, they may not e.g. remove the same 713859ad36SKevin Wolf * export a second time (which would decrease the refcount without having 723859ad36SKevin Wolf * it incremented first). 733859ad36SKevin Wolf */ 743859ad36SKevin Wolf bool user_owned; 753859ad36SKevin Wolf 768612c686SKevin Wolf /* The AioContext whose lock protects this BlockExport object. */ 778612c686SKevin Wolf AioContext *ctx; 78bc4ee65bSKevin Wolf 7937a4f70cSKevin Wolf /* The block device to export */ 8037a4f70cSKevin Wolf BlockBackend *blk; 8137a4f70cSKevin Wolf 82bc4ee65bSKevin Wolf /* List entry for block_exports */ 83bc4ee65bSKevin Wolf QLIST_ENTRY(BlockExport) next; 8456ee8626SKevin Wolf }; 8556ee8626SKevin Wolf 869b562c64SKevin Wolf BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp); 873c3bc462SKevin Wolf BlockExport *blk_exp_find(const char *id); 88c69de1beSKevin Wolf void blk_exp_ref(BlockExport *exp); 89c69de1beSKevin Wolf void blk_exp_unref(BlockExport *exp); 90bc4ee65bSKevin Wolf void blk_exp_request_shutdown(BlockExport *exp); 91bc4ee65bSKevin Wolf void blk_exp_close_all(void); 92bc4ee65bSKevin Wolf void blk_exp_close_all_type(BlockExportType type); 939b562c64SKevin Wolf 9456ee8626SKevin Wolf #endif 95