| 1 | /* |
| 2 | * QEMU System Emulator block driver |
| 3 | * |
| 4 | * Copyright (c) 2003 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | #ifndef BLOCK_GLOBAL_STATE_H |
| 25 | #define BLOCK_GLOBAL_STATE_H |
| 26 | |
| 27 | #include "qemu/aiocb.h" |
| 28 | #include "block/graph-lock.h" |
| 29 | #include "block/block-common.h" |
| 30 | #include "qemu/coroutine.h" |
| 31 | #include "qemu/transactions.h" |
| 32 | |
| 33 | /* |
| 34 | * Global state (GS) API. These functions run under the BQL. |
| 35 | * |
| 36 | * If a function modifies the graph, it also uses the graph lock to be sure it |
| 37 | * has unique access. The graph lock is needed together with BQL because of the |
| 38 | * thread-safe I/O API that concurrently runs and accesses the graph without |
| 39 | * the BQL. |
| 40 | * |
| 41 | * It is important to note that not all of these functions are |
| 42 | * necessarily limited to running under the BQL, but they would |
| 43 | * require additional auditing and many small thread-safety changes |
| 44 | * to move them into the I/O API. Often it's not worth doing that |
| 45 | * work since the APIs are only used with the BQL held at the |
| 46 | * moment, so they have been placed in the GS API (for now). |
| 47 | * |
| 48 | * These functions can call any function from this and other categories |
| 49 | * (I/O, "I/O or GS", Common), but must be invoked only by other GS APIs. |
| 50 | * |
| 51 | * All functions in this header must use the macro |
| 52 | * GLOBAL_STATE_CODE(); |
| 53 | * to catch when they are accidentally called without the BQL. |
| 54 | */ |
| 55 | |
| 56 | void bdrv_init(void); |
| 57 | BlockDriver *bdrv_find_protocol(const char *filename, |
| 58 | bool allow_protocol_prefix, |
| 59 | Error **errp); |
| 60 | BlockDriver *bdrv_find_format(const char *format_name); |
| 61 | |
| 62 | int coroutine_fn GRAPH_UNLOCKED |
| 63 | bdrv_co_create(BlockDriver *drv, const char *filename, QemuOpts *opts, |
| 64 | Error **errp); |
| 65 | |
| 66 | int co_wrapper bdrv_create(BlockDriver *drv, const char *filename, |
| 67 | QemuOpts *opts, Error **errp); |
| 68 | |
| 69 | int coroutine_fn GRAPH_UNLOCKED |
| 70 | bdrv_co_create_file(const char *filename, QemuOpts *opts, |
| 71 | bool allow_protocol_prefix, Error **errp); |
| 72 | |
| 73 | BlockDriverState *bdrv_new(void); |
| 74 | int bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top, |
| 75 | Error **errp); |
| 76 | |
| 77 | int GRAPH_WRLOCK |
| 78 | bdrv_replace_node(BlockDriverState *from, BlockDriverState *to, Error **errp); |
| 79 | |
| 80 | int GRAPH_UNLOCKED |
| 81 | bdrv_replace_child_bs(BdrvChild *child, BlockDriverState *new_bs, Error **errp); |
| 82 | BlockDriverState * GRAPH_UNLOCKED |
| 83 | bdrv_insert_node(BlockDriverState *bs, QDict *node_options, int flags, |
| 84 | Error **errp); |
| 85 | int bdrv_drop_filter(BlockDriverState *bs, Error **errp); |
| 86 | |
| 87 | BdrvChild * no_coroutine_fn GRAPH_UNLOCKED |
| 88 | bdrv_open_child(const char *filename, QDict *options, const char *bdref_key, |
| 89 | BlockDriverState *parent, const BdrvChildClass *child_class, |
| 90 | BdrvChildRole child_role, bool allow_none, Error **errp); |
| 91 | |
| 92 | BdrvChild * coroutine_fn no_co_wrapper |
| 93 | bdrv_co_open_child(const char *filename, QDict *options, const char *bdref_key, |
| 94 | BlockDriverState *parent, const BdrvChildClass *child_class, |
| 95 | BdrvChildRole child_role, bool allow_none, Error **errp); |
| 96 | |
| 97 | int GRAPH_UNLOCKED |
| 98 | bdrv_open_file_child(const char *filename, QDict *options, |
| 99 | const char *bdref_key, BlockDriverState *parent, |
| 100 | Error **errp); |
| 101 | |
| 102 | BlockDriverState * no_coroutine_fn |
| 103 | bdrv_open_blockdev_ref(BlockdevRef *ref, Error **errp); |
| 104 | |
| 105 | BlockDriverState * coroutine_fn no_co_wrapper |
| 106 | bdrv_co_open_blockdev_ref(BlockdevRef *ref, Error **errp); |
| 107 | |
| 108 | int GRAPH_WRLOCK |
| 109 | bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd, |
| 110 | Error **errp); |
| 111 | |
| 112 | int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options, |
| 113 | const char *bdref_key, Error **errp); |
| 114 | |
| 115 | BlockDriverState * no_coroutine_fn |
| 116 | bdrv_open(const char *filename, const char *reference, QDict *options, |
| 117 | int flags, Error **errp); |
| 118 | |
| 119 | BlockDriverState * coroutine_fn no_co_wrapper |
| 120 | bdrv_co_open(const char *filename, const char *reference, |
| 121 | QDict *options, int flags, Error **errp); |
| 122 | |
| 123 | BlockDriverState *bdrv_new_open_driver_opts(BlockDriver *drv, |
| 124 | const char *node_name, |
| 125 | QDict *options, int flags, |
| 126 | Error **errp); |
| 127 | BlockDriverState *bdrv_new_open_driver(BlockDriver *drv, const char *node_name, |
| 128 | int flags, Error **errp); |
| 129 | BlockReopenQueue * GRAPH_UNLOCKED |
| 130 | bdrv_reopen_queue(BlockReopenQueue *bs_queue, BlockDriverState *bs, |
| 131 | QDict *options, bool keep_old_opts); |
| 132 | void bdrv_reopen_queue_free(BlockReopenQueue *bs_queue); |
| 133 | int GRAPH_UNLOCKED |
| 134 | bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp); |
| 135 | int bdrv_reopen(BlockDriverState *bs, QDict *opts, bool keep_old_opts, |
| 136 | Error **errp); |
| 137 | int bdrv_reopen_set_read_only(BlockDriverState *bs, bool read_only, |
| 138 | Error **errp); |
| 139 | BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, |
| 140 | const char *backing_file); |
| 141 | void GRAPH_RDLOCK bdrv_refresh_filename(BlockDriverState *bs); |
| 142 | |
| 143 | void GRAPH_RDLOCK |
| 144 | bdrv_refresh_limits(BlockDriverState *bs, Transaction *tran, Error **errp); |
| 145 | |
| 146 | int bdrv_commit(BlockDriverState *bs); |
| 147 | int GRAPH_RDLOCK bdrv_make_empty(BdrvChild *c, Error **errp); |
| 148 | |
| 149 | void bdrv_register(BlockDriver *bdrv); |
| 150 | int GRAPH_UNLOCKED |
| 151 | bdrv_drop_intermediate(BlockDriverState *top, BlockDriverState *base, |
| 152 | const char *backing_file_str, |
| 153 | bool backing_mask_protocol); |
| 154 | |
| 155 | BlockDriverState * GRAPH_RDLOCK |
| 156 | bdrv_find_overlay(BlockDriverState *active, BlockDriverState *bs); |
| 157 | |
| 158 | BlockDriverState * GRAPH_RDLOCK bdrv_find_base(BlockDriverState *bs); |
| 159 | |
| 160 | int GRAPH_RDLOCK |
| 161 | bdrv_freeze_backing_chain(BlockDriverState *bs, BlockDriverState *base, |
| 162 | Error **errp); |
| 163 | void GRAPH_RDLOCK |
| 164 | bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *base); |
| 165 | |
| 166 | /* |
| 167 | * The units of offset and total_work_size may be chosen arbitrarily by the |
| 168 | * block driver; total_work_size may change during the course of the amendment |
| 169 | * operation |
| 170 | */ |
| 171 | typedef void BlockDriverAmendStatusCB(BlockDriverState *bs, int64_t offset, |
| 172 | int64_t total_work_size, void *opaque); |
| 173 | int GRAPH_RDLOCK |
| 174 | bdrv_amend_options(BlockDriverState *bs_new, QemuOpts *opts, |
| 175 | BlockDriverAmendStatusCB *status_cb, void *cb_opaque, |
| 176 | bool force, Error **errp); |
| 177 | |
| 178 | /* check if a named node can be replaced when doing drive-mirror */ |
| 179 | BlockDriverState * GRAPH_RDLOCK |
| 180 | check_to_replace_node(BlockDriverState *parent_bs, const char *node_name, |
| 181 | Error **errp); |
| 182 | |
| 183 | |
| 184 | bool GRAPH_RDLOCK bdrv_is_inactive(BlockDriverState *bs); |
| 185 | |
| 186 | int no_coroutine_fn GRAPH_RDLOCK |
| 187 | bdrv_activate(BlockDriverState *bs, Error **errp); |
| 188 | |
| 189 | int coroutine_fn no_co_wrapper_bdrv_rdlock |
| 190 | bdrv_co_activate(BlockDriverState *bs, Error **errp); |
| 191 | |
| 192 | int no_coroutine_fn GRAPH_RDLOCK |
| 193 | bdrv_inactivate(BlockDriverState *bs, Error **errp); |
| 194 | |
| 195 | void bdrv_activate_all(Error **errp); |
| 196 | int GRAPH_UNLOCKED bdrv_inactivate_all(void); |
| 197 | |
| 198 | int bdrv_flush_all(void); |
| 199 | void GRAPH_UNLOCKED bdrv_close_all(void); |
| 200 | void GRAPH_UNLOCKED bdrv_drain_all_begin(void); |
| 201 | void bdrv_drain_all_begin_nopoll(void); |
| 202 | void bdrv_drain_all_end(void); |
| 203 | void GRAPH_UNLOCKED bdrv_drain_all(void); |
| 204 | |
| 205 | void bdrv_aio_cancel(BlockAIOCB *acb); |
| 206 | |
| 207 | int bdrv_has_zero_init_1(BlockDriverState *bs); |
| 208 | int coroutine_mixed_fn GRAPH_RDLOCK bdrv_has_zero_init(BlockDriverState *bs); |
| 209 | BlockDriverState *bdrv_find_node(const char *node_name); |
| 210 | BlockDeviceInfoList *bdrv_named_nodes_list(bool flat, Error **errp); |
| 211 | XDbgBlockGraph * GRAPH_RDLOCK bdrv_get_xdbg_block_graph(Error **errp); |
| 212 | BlockDriverState *bdrv_lookup_bs(const char *device, |
| 213 | const char *node_name, |
| 214 | Error **errp); |
| 215 | bool GRAPH_RDLOCK |
| 216 | bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base); |
| 217 | |
| 218 | BlockDriverState *bdrv_next_node(BlockDriverState *bs); |
| 219 | BlockDriverState *bdrv_next_all_states(BlockDriverState *bs); |
| 220 | |
| 221 | typedef struct BdrvNextIterator { |
| 222 | enum { |
| 223 | BDRV_NEXT_BACKEND_ROOTS, |
| 224 | BDRV_NEXT_MONITOR_OWNED, |
| 225 | } phase; |
| 226 | BlockBackend *blk; |
| 227 | BlockDriverState *bs; |
| 228 | } BdrvNextIterator; |
| 229 | |
| 230 | BlockDriverState * GRAPH_RDLOCK bdrv_first(BdrvNextIterator *it); |
| 231 | BlockDriverState * GRAPH_RDLOCK bdrv_next(BdrvNextIterator *it); |
| 232 | void bdrv_next_cleanup(BdrvNextIterator *it); |
| 233 | |
| 234 | BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs); |
| 235 | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
| 236 | void *opaque, bool read_only); |
| 237 | |
| 238 | char * GRAPH_RDLOCK |
| 239 | bdrv_get_full_backing_filename(BlockDriverState *bs, Error **errp); |
| 240 | |
| 241 | char * GRAPH_RDLOCK bdrv_dirname(BlockDriverState *bs, Error **errp); |
| 242 | |
| 243 | void bdrv_img_create(const char *filename, const char *fmt, |
| 244 | const char *base_filename, const char *base_fmt, |
| 245 | char *options, uint64_t img_size, int flags, |
| 246 | bool quiet, Error **errp); |
| 247 | |
| 248 | void bdrv_ref(BlockDriverState *bs); |
| 249 | void no_coroutine_fn bdrv_unref(BlockDriverState *bs); |
| 250 | void coroutine_fn no_co_wrapper bdrv_co_unref(BlockDriverState *bs); |
| 251 | void GRAPH_WRLOCK bdrv_schedule_unref(BlockDriverState *bs); |
| 252 | |
| 253 | void GRAPH_WRLOCK |
| 254 | bdrv_unref_child(BlockDriverState *parent, BdrvChild *child); |
| 255 | |
| 256 | void coroutine_fn no_co_wrapper_bdrv_wrlock |
| 257 | bdrv_co_unref_child(BlockDriverState *parent, BdrvChild *child); |
| 258 | |
| 259 | BdrvChild * GRAPH_WRLOCK |
| 260 | bdrv_attach_child(BlockDriverState *parent_bs, |
| 261 | BlockDriverState *child_bs, |
| 262 | const char *child_name, |
| 263 | const BdrvChildClass *child_class, |
| 264 | BdrvChildRole child_role, |
| 265 | Error **errp); |
| 266 | |
| 267 | bool GRAPH_RDLOCK |
| 268 | bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp); |
| 269 | |
| 270 | void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason); |
| 271 | void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason); |
| 272 | void bdrv_op_block_all(BlockDriverState *bs, Error *reason); |
| 273 | void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason); |
| 274 | bool bdrv_op_blocker_is_empty(BlockDriverState *bs); |
| 275 | |
| 276 | int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event, |
| 277 | const char *tag); |
| 278 | int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag); |
| 279 | int bdrv_debug_resume(BlockDriverState *bs, const char *tag); |
| 280 | bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag); |
| 281 | |
| 282 | bool GRAPH_RDLOCK |
| 283 | bdrv_child_change_aio_context(BdrvChild *c, AioContext *ctx, |
| 284 | GHashTable *visited, Transaction *tran, |
| 285 | Error **errp); |
| 286 | int GRAPH_UNLOCKED |
| 287 | bdrv_try_change_aio_context(BlockDriverState *bs, AioContext *ctx, |
| 288 | BdrvChild *ignore_child, Error **errp); |
| 289 | int GRAPH_RDLOCK |
| 290 | bdrv_try_change_aio_context_locked(BlockDriverState *bs, AioContext *ctx, |
| 291 | BdrvChild *ignore_child, Error **errp); |
| 292 | |
| 293 | int GRAPH_RDLOCK bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz); |
| 294 | int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo); |
| 295 | |
| 296 | void GRAPH_WRLOCK |
| 297 | bdrv_add_child(BlockDriverState *parent, BlockDriverState *child, Error **errp); |
| 298 | |
| 299 | void GRAPH_WRLOCK |
| 300 | bdrv_del_child(BlockDriverState *parent, BdrvChild *child, Error **errp); |
| 301 | |
| 302 | /** |
| 303 | * |
| 304 | * bdrv_register_buf/bdrv_unregister_buf: |
| 305 | * |
| 306 | * Register/unregister a buffer for I/O. For example, VFIO drivers are |
| 307 | * interested to know the memory areas that would later be used for I/O, so |
| 308 | * that they can prepare IOMMU mapping etc., to get better performance. |
| 309 | * |
| 310 | * Buffers must not overlap and they must be unregistered with the same <host, |
| 311 | * size> values that they were registered with. |
| 312 | * |
| 313 | * Returns: true on success, false on failure |
| 314 | */ |
| 315 | bool bdrv_register_buf(BlockDriverState *bs, void *host, size_t size, |
| 316 | Error **errp); |
| 317 | void bdrv_unregister_buf(BlockDriverState *bs, void *host, size_t size); |
| 318 | |
| 319 | void bdrv_cancel_in_flight(BlockDriverState *bs); |
| 320 | |
| 321 | #endif /* BLOCK_GLOBAL_STATE_H */ |