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 3256ee8626SKevin Wolf /* Creates and starts a new block export */ 33a6ff7989SKevin Wolf int (*create)(BlockExport *, BlockExportOptions *, Error **); 34c69de1beSKevin Wolf 35c69de1beSKevin Wolf /* 36c69de1beSKevin Wolf * Frees a removed block export. This function is only called after all 37c69de1beSKevin Wolf * references have been dropped. 38c69de1beSKevin Wolf */ 39c69de1beSKevin Wolf void (*delete)(BlockExport *); 40bc4ee65bSKevin Wolf 41bc4ee65bSKevin Wolf /* 42bc4ee65bSKevin Wolf * Start to disconnect all clients and drop other references held 43bc4ee65bSKevin Wolf * internally by the export driver. When the function returns, there may 44bc4ee65bSKevin Wolf * still be active references while the export is in the process of 45bc4ee65bSKevin Wolf * shutting down. 46bc4ee65bSKevin Wolf */ 47bc4ee65bSKevin Wolf void (*request_shutdown)(BlockExport *); 4856ee8626SKevin Wolf } BlockExportDriver; 4956ee8626SKevin Wolf 5056ee8626SKevin Wolf struct BlockExport { 5156ee8626SKevin Wolf const BlockExportDriver *drv; 52c69de1beSKevin Wolf 53d53be9ceSKevin Wolf /* Unique identifier for the export */ 54d53be9ceSKevin Wolf char *id; 55d53be9ceSKevin Wolf 56c69de1beSKevin Wolf /* 57c69de1beSKevin Wolf * Reference count for this block export. This includes strong references 58c69de1beSKevin Wolf * both from the owner (qemu-nbd or the monitor) and clients connected to 59c69de1beSKevin Wolf * the export. 60*3d499a43SStefan Hajnoczi * 61*3d499a43SStefan Hajnoczi * Use atomics to access this field. 62c69de1beSKevin Wolf */ 63c69de1beSKevin Wolf int refcount; 648612c686SKevin Wolf 653859ad36SKevin Wolf /* 663859ad36SKevin Wolf * True if one of the references in refcount belongs to the user. After the 673859ad36SKevin Wolf * user has dropped their reference, they may not e.g. remove the same 683859ad36SKevin Wolf * export a second time (which would decrease the refcount without having 693859ad36SKevin Wolf * it incremented first). 703859ad36SKevin Wolf */ 713859ad36SKevin Wolf bool user_owned; 723859ad36SKevin Wolf 738612c686SKevin Wolf /* The AioContext whose lock protects this BlockExport object. */ 748612c686SKevin Wolf AioContext *ctx; 75bc4ee65bSKevin Wolf 7637a4f70cSKevin Wolf /* The block device to export */ 7737a4f70cSKevin Wolf BlockBackend *blk; 7837a4f70cSKevin Wolf 79bc4ee65bSKevin Wolf /* List entry for block_exports */ 80bc4ee65bSKevin Wolf QLIST_ENTRY(BlockExport) next; 8156ee8626SKevin Wolf }; 8256ee8626SKevin Wolf 839b562c64SKevin Wolf BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp); 843c3bc462SKevin Wolf BlockExport *blk_exp_find(const char *id); 85c69de1beSKevin Wolf void blk_exp_ref(BlockExport *exp); 86c69de1beSKevin Wolf void blk_exp_unref(BlockExport *exp); 87bc4ee65bSKevin Wolf void blk_exp_request_shutdown(BlockExport *exp); 88bc4ee65bSKevin Wolf void blk_exp_close_all(void); 89bc4ee65bSKevin Wolf void blk_exp_close_all_type(BlockExportType type); 909b562c64SKevin Wolf 9156ee8626SKevin Wolf #endif 92