Emanuele Giuseppe Esposito | ebc2752 | 2022-03-03 10:15:56 -0500 | [diff] [blame] | 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 | */ |
Markus Armbruster | ea9cea9 | 2022-05-06 15:49:11 +0200 | [diff] [blame] | 24 | |
Emanuele Giuseppe Esposito | ebc2752 | 2022-03-03 10:15:56 -0500 | [diff] [blame] | 25 | #ifndef BLOCK_INT_GLOBAL_STATE_H |
| 26 | #define BLOCK_INT_GLOBAL_STATE_H |
| 27 | |
Markus Armbruster | e2c1c34 | 2022-12-21 14:35:49 +0100 | [diff] [blame] | 28 | #include "block/blockjob.h" |
| 29 | #include "block/block_int-common.h" |
| 30 | #include "qemu/hbitmap.h" |
| 31 | #include "qemu/main-loop.h" |
Emanuele Giuseppe Esposito | ebc2752 | 2022-03-03 10:15:56 -0500 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Global state (GS) API. These functions run under the BQL. |
| 35 | * |
| 36 | * See include/block/block-global-state.h for more information about |
| 37 | * the GS API. |
| 38 | */ |
| 39 | |
| 40 | /** |
| 41 | * stream_start: |
| 42 | * @job_id: The id of the newly-created job, or %NULL to use the |
| 43 | * device name of @bs. |
| 44 | * @bs: Block device to operate on. |
| 45 | * @base: Block device that will become the new base, or %NULL to |
| 46 | * flatten the whole backing file chain onto @bs. |
| 47 | * @backing_file_str: The file name that will be written to @bs as the |
| 48 | * the new backing file if the job completes. Ignored if @base is %NULL. |
Peter Krempa | 72098a3 | 2023-12-05 18:14:42 +0100 | [diff] [blame] | 49 | * @backing_mask_protocol: Replace potential protocol name with 'raw' in |
| 50 | * 'backing file format' header |
Emanuele Giuseppe Esposito | ebc2752 | 2022-03-03 10:15:56 -0500 | [diff] [blame] | 51 | * @creation_flags: Flags that control the behavior of the Job lifetime. |
| 52 | * See @BlockJobCreateFlags |
| 53 | * @speed: The maximum speed, in bytes per second, or 0 for unlimited. |
| 54 | * @on_error: The action to take upon error. |
| 55 | * @filter_node_name: The node name that should be assigned to the filter |
| 56 | * driver that the stream job inserts into the graph above |
| 57 | * @bs. NULL means that a node name should be autogenerated. |
| 58 | * @errp: Error object. |
| 59 | * |
| 60 | * Start a streaming operation on @bs. Clusters that are unallocated |
| 61 | * in @bs, but allocated in any image between @base and @bs (both |
| 62 | * exclusive) will be written to @bs. At the end of a successful |
| 63 | * streaming job, the backing file of @bs will be changed to |
| 64 | * @backing_file_str in the written image and to @base in the live |
| 65 | * BlockDriverState. |
| 66 | */ |
| 67 | void stream_start(const char *job_id, BlockDriverState *bs, |
| 68 | BlockDriverState *base, const char *backing_file_str, |
Peter Krempa | 72098a3 | 2023-12-05 18:14:42 +0100 | [diff] [blame] | 69 | bool backing_mask_protocol, |
Emanuele Giuseppe Esposito | ebc2752 | 2022-03-03 10:15:56 -0500 | [diff] [blame] | 70 | BlockDriverState *bottom, |
| 71 | int creation_flags, int64_t speed, |
| 72 | BlockdevOnError on_error, |
| 73 | const char *filter_node_name, |
| 74 | Error **errp); |
| 75 | |
| 76 | /** |
| 77 | * commit_start: |
| 78 | * @job_id: The id of the newly-created job, or %NULL to use the |
| 79 | * device name of @bs. |
| 80 | * @bs: Active block device. |
| 81 | * @top: Top block device to be committed. |
| 82 | * @base: Block device that will be written into, and become the new top. |
| 83 | * @creation_flags: Flags that control the behavior of the Job lifetime. |
| 84 | * See @BlockJobCreateFlags |
| 85 | * @speed: The maximum speed, in bytes per second, or 0 for unlimited. |
| 86 | * @on_error: The action to take upon error. |
| 87 | * @backing_file_str: String to use as the backing file in @top's overlay |
Peter Krempa | 4b028cb | 2023-12-05 18:14:41 +0100 | [diff] [blame] | 88 | * @backing_mask_protocol: Replace potential protocol name with 'raw' in |
| 89 | * 'backing file format' header |
Emanuele Giuseppe Esposito | ebc2752 | 2022-03-03 10:15:56 -0500 | [diff] [blame] | 90 | * @filter_node_name: The node name that should be assigned to the filter |
| 91 | * driver that the commit job inserts into the graph above @top. NULL means |
| 92 | * that a node name should be autogenerated. |
| 93 | * @errp: Error object. |
| 94 | * |
| 95 | */ |
| 96 | void commit_start(const char *job_id, BlockDriverState *bs, |
| 97 | BlockDriverState *base, BlockDriverState *top, |
| 98 | int creation_flags, int64_t speed, |
| 99 | BlockdevOnError on_error, const char *backing_file_str, |
Peter Krempa | 4b028cb | 2023-12-05 18:14:41 +0100 | [diff] [blame] | 100 | bool backing_mask_protocol, |
Emanuele Giuseppe Esposito | ebc2752 | 2022-03-03 10:15:56 -0500 | [diff] [blame] | 101 | const char *filter_node_name, Error **errp); |
| 102 | /** |
| 103 | * commit_active_start: |
| 104 | * @job_id: The id of the newly-created job, or %NULL to use the |
| 105 | * device name of @bs. |
| 106 | * @bs: Active block device to be committed. |
| 107 | * @base: Block device that will be written into, and become the new top. |
| 108 | * @creation_flags: Flags that control the behavior of the Job lifetime. |
| 109 | * See @BlockJobCreateFlags |
| 110 | * @speed: The maximum speed, in bytes per second, or 0 for unlimited. |
| 111 | * @on_error: The action to take upon error. |
| 112 | * @filter_node_name: The node name that should be assigned to the filter |
| 113 | * driver that the commit job inserts into the graph above @bs. NULL means that |
| 114 | * a node name should be autogenerated. |
| 115 | * @cb: Completion function for the job. |
| 116 | * @opaque: Opaque pointer value passed to @cb. |
| 117 | * @auto_complete: Auto complete the job. |
| 118 | * @errp: Error object. |
| 119 | * |
| 120 | */ |
| 121 | BlockJob *commit_active_start(const char *job_id, BlockDriverState *bs, |
| 122 | BlockDriverState *base, int creation_flags, |
| 123 | int64_t speed, BlockdevOnError on_error, |
| 124 | const char *filter_node_name, |
| 125 | BlockCompletionFunc *cb, void *opaque, |
| 126 | bool auto_complete, Error **errp); |
| 127 | /* |
| 128 | * mirror_start: |
| 129 | * @job_id: The id of the newly-created job, or %NULL to use the |
| 130 | * device name of @bs. |
| 131 | * @bs: Block device to operate on. |
| 132 | * @target: Block device to write to. |
| 133 | * @replaces: Block graph node name to replace once the mirror is done. Can |
| 134 | * only be used when full mirroring is selected. |
| 135 | * @creation_flags: Flags that control the behavior of the Job lifetime. |
| 136 | * See @BlockJobCreateFlags |
| 137 | * @speed: The maximum speed, in bytes per second, or 0 for unlimited. |
| 138 | * @granularity: The chosen granularity for the dirty bitmap. |
| 139 | * @buf_size: The amount of data that can be in flight at one time. |
| 140 | * @mode: Whether to collapse all images in the chain to the target. |
| 141 | * @backing_mode: How to establish the target's backing chain after completion. |
| 142 | * @zero_target: Whether the target should be explicitly zero-initialized |
| 143 | * @on_source_error: The action to take upon error reading from the source. |
| 144 | * @on_target_error: The action to take upon error writing to the target. |
| 145 | * @unmap: Whether to unmap target where source sectors only contain zeroes. |
| 146 | * @filter_node_name: The node name that should be assigned to the filter |
| 147 | * driver that the mirror job inserts into the graph above @bs. NULL means that |
| 148 | * a node name should be autogenerated. |
| 149 | * @copy_mode: When to trigger writes to the target. |
| 150 | * @errp: Error object. |
| 151 | * |
| 152 | * Start a mirroring operation on @bs. Clusters that are allocated |
| 153 | * in @bs will be written to @target until the job is cancelled or |
| 154 | * manually completed. At the end of a successful mirroring job, |
| 155 | * @bs will be switched to read from @target. |
| 156 | */ |
| 157 | void mirror_start(const char *job_id, BlockDriverState *bs, |
| 158 | BlockDriverState *target, const char *replaces, |
| 159 | int creation_flags, int64_t speed, |
| 160 | uint32_t granularity, int64_t buf_size, |
| 161 | MirrorSyncMode mode, BlockMirrorBackingMode backing_mode, |
| 162 | bool zero_target, |
| 163 | BlockdevOnError on_source_error, |
| 164 | BlockdevOnError on_target_error, |
| 165 | bool unmap, const char *filter_node_name, |
| 166 | MirrorCopyMode copy_mode, Error **errp); |
| 167 | |
| 168 | /* |
| 169 | * backup_job_create: |
| 170 | * @job_id: The id of the newly-created job, or %NULL to use the |
| 171 | * device name of @bs. |
| 172 | * @bs: Block device to operate on. |
| 173 | * @target: Block device to write to. |
| 174 | * @speed: The maximum speed, in bytes per second, or 0 for unlimited. |
| 175 | * @sync_mode: What parts of the disk image should be copied to the destination. |
| 176 | * @sync_bitmap: The dirty bitmap if sync_mode is 'bitmap' or 'incremental' |
| 177 | * @bitmap_mode: The bitmap synchronization policy to use. |
| 178 | * @perf: Performance options. All actual fields assumed to be present, |
| 179 | * all ".has_*" fields are ignored. |
| 180 | * @on_source_error: The action to take upon error reading from the source. |
| 181 | * @on_target_error: The action to take upon error writing to the target. |
| 182 | * @creation_flags: Flags that control the behavior of the Job lifetime. |
| 183 | * See @BlockJobCreateFlags |
| 184 | * @cb: Completion function for the job. |
| 185 | * @opaque: Opaque pointer value passed to @cb. |
| 186 | * @txn: Transaction that this job is part of (may be NULL). |
| 187 | * |
| 188 | * Create a backup operation on @bs. Clusters in @bs are written to @target |
| 189 | * until the job is cancelled or manually completed. |
| 190 | */ |
| 191 | BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs, |
| 192 | BlockDriverState *target, int64_t speed, |
| 193 | MirrorSyncMode sync_mode, |
| 194 | BdrvDirtyBitmap *sync_bitmap, |
| 195 | BitmapSyncMode bitmap_mode, |
Vladimir Sementsov-Ogievskiy | 0fd05c8 | 2024-03-13 18:28:21 +0300 | [diff] [blame] | 196 | bool compress, bool discard_source, |
Emanuele Giuseppe Esposito | ebc2752 | 2022-03-03 10:15:56 -0500 | [diff] [blame] | 197 | const char *filter_node_name, |
| 198 | BackupPerf *perf, |
| 199 | BlockdevOnError on_source_error, |
| 200 | BlockdevOnError on_target_error, |
| 201 | int creation_flags, |
| 202 | BlockCompletionFunc *cb, void *opaque, |
| 203 | JobTxn *txn, Error **errp); |
| 204 | |
Kevin Wolf | 03b9eac | 2023-10-27 17:53:13 +0200 | [diff] [blame] | 205 | BdrvChild * GRAPH_WRLOCK |
| 206 | bdrv_root_attach_child(BlockDriverState *child_bs, const char *child_name, |
| 207 | const BdrvChildClass *child_class, |
| 208 | BdrvChildRole child_role, |
| 209 | uint64_t perm, uint64_t shared_perm, |
| 210 | void *opaque, Error **errp); |
| 211 | |
Kevin Wolf | ede01e4 | 2023-09-11 11:46:18 +0200 | [diff] [blame] | 212 | void GRAPH_WRLOCK bdrv_root_unref_child(BdrvChild *child); |
Emanuele Giuseppe Esposito | ebc2752 | 2022-03-03 10:15:56 -0500 | [diff] [blame] | 213 | |
Kevin Wolf | bce73bc | 2023-09-11 11:46:13 +0200 | [diff] [blame] | 214 | void GRAPH_RDLOCK bdrv_get_cumulative_perm(BlockDriverState *bs, uint64_t *perm, |
| 215 | uint64_t *shared_perm); |
Emanuele Giuseppe Esposito | ebc2752 | 2022-03-03 10:15:56 -0500 | [diff] [blame] | 216 | |
| 217 | /** |
| 218 | * Sets a BdrvChild's permissions. Avoid if the parent is a BDS; use |
| 219 | * bdrv_child_refresh_perms() instead and make the parent's |
| 220 | * .bdrv_child_perm() implementation return the correct values. |
| 221 | */ |
Kevin Wolf | 3804e3c | 2023-09-11 11:46:12 +0200 | [diff] [blame] | 222 | int GRAPH_RDLOCK |
| 223 | bdrv_child_try_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared, |
| 224 | Error **errp); |
Emanuele Giuseppe Esposito | ebc2752 | 2022-03-03 10:15:56 -0500 | [diff] [blame] | 225 | |
| 226 | /** |
| 227 | * Calls bs->drv->bdrv_child_perm() and updates the child's permission |
| 228 | * masks with the result. |
| 229 | * Drivers should invoke this function whenever an event occurs that |
| 230 | * makes their .bdrv_child_perm() implementation return different |
| 231 | * values than before, but which will not result in the block layer |
| 232 | * automatically refreshing the permissions. |
| 233 | */ |
Kevin Wolf | 3804e3c | 2023-09-11 11:46:12 +0200 | [diff] [blame] | 234 | int GRAPH_RDLOCK |
| 235 | bdrv_child_refresh_perms(BlockDriverState *bs, BdrvChild *c, Error **errp); |
Emanuele Giuseppe Esposito | ebc2752 | 2022-03-03 10:15:56 -0500 | [diff] [blame] | 236 | |
Kevin Wolf | 533c6e4 | 2023-05-04 13:57:49 +0200 | [diff] [blame] | 237 | bool GRAPH_RDLOCK bdrv_recurse_can_replace(BlockDriverState *bs, |
| 238 | BlockDriverState *to_replace); |
Emanuele Giuseppe Esposito | ebc2752 | 2022-03-03 10:15:56 -0500 | [diff] [blame] | 239 | |
| 240 | /* |
| 241 | * Default implementation for BlockDriver.bdrv_child_perm() that can |
| 242 | * be used by block filters and image formats, as long as they use the |
| 243 | * child_of_bds child class and set an appropriate BdrvChildRole. |
| 244 | */ |
| 245 | void bdrv_default_perms(BlockDriverState *bs, BdrvChild *c, |
| 246 | BdrvChildRole role, BlockReopenQueue *reopen_queue, |
| 247 | uint64_t perm, uint64_t shared, |
| 248 | uint64_t *nperm, uint64_t *nshared); |
| 249 | |
| 250 | void blk_dev_change_media_cb(BlockBackend *blk, bool load, Error **errp); |
| 251 | bool blk_dev_has_removable_media(BlockBackend *blk); |
| 252 | void blk_dev_eject_request(BlockBackend *blk, bool force); |
| 253 | bool blk_dev_is_medium_locked(BlockBackend *blk); |
| 254 | |
| 255 | void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup); |
| 256 | |
| 257 | void bdrv_set_monitor_owned(BlockDriverState *bs); |
| 258 | |
| 259 | void blockdev_close_all_bdrv_states(void); |
| 260 | |
| 261 | BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp); |
| 262 | |
| 263 | /** |
| 264 | * Simple implementation of bdrv_co_create_opts for protocol drivers |
| 265 | * which only support creation via opening a file |
| 266 | * (usually existing raw storage device) |
| 267 | */ |
| 268 | int coroutine_fn bdrv_co_create_opts_simple(BlockDriver *drv, |
| 269 | const char *filename, |
| 270 | QemuOpts *opts, |
| 271 | Error **errp); |
| 272 | |
| 273 | BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, |
| 274 | const char *name, |
| 275 | BlockDriverState **pbs, |
| 276 | Error **errp); |
| 277 | BdrvDirtyBitmap *block_dirty_bitmap_merge(const char *node, const char *target, |
Vladimir Sementsov-Ogievskiy | 1466ef6 | 2022-03-15 00:32:24 +0300 | [diff] [blame] | 278 | BlockDirtyBitmapOrStrList *bms, |
Emanuele Giuseppe Esposito | ebc2752 | 2022-03-03 10:15:56 -0500 | [diff] [blame] | 279 | HBitmap **backup, Error **errp); |
| 280 | BdrvDirtyBitmap *block_dirty_bitmap_remove(const char *node, const char *name, |
| 281 | bool release, |
| 282 | BlockDriverState **bitmap_bs, |
| 283 | Error **errp); |
| 284 | |
| 285 | |
Kevin Wolf | 430da83 | 2023-10-27 17:53:16 +0200 | [diff] [blame] | 286 | BlockDriverState * GRAPH_RDLOCK |
| 287 | bdrv_skip_implicit_filters(BlockDriverState *bs); |
Emanuele Giuseppe Esposito | ebc2752 | 2022-03-03 10:15:56 -0500 | [diff] [blame] | 288 | |
| 289 | /** |
| 290 | * bdrv_add_aio_context_notifier: |
| 291 | * |
| 292 | * If a long-running job intends to be always run in the same AioContext as a |
| 293 | * certain BDS, it may use this function to be notified of changes regarding the |
| 294 | * association of the BDS to an AioContext. |
| 295 | * |
| 296 | * attached_aio_context() is called after the target BDS has been attached to a |
| 297 | * new AioContext; detach_aio_context() is called before the target BDS is being |
| 298 | * detached from its old AioContext. |
| 299 | */ |
| 300 | void bdrv_add_aio_context_notifier(BlockDriverState *bs, |
| 301 | void (*attached_aio_context)(AioContext *new_context, void *opaque), |
| 302 | void (*detach_aio_context)(void *opaque), void *opaque); |
| 303 | |
| 304 | /** |
| 305 | * bdrv_remove_aio_context_notifier: |
| 306 | * |
| 307 | * Unsubscribe of change notifications regarding the BDS's AioContext. The |
| 308 | * parameters given here have to be the same as those given to |
| 309 | * bdrv_add_aio_context_notifier(). |
| 310 | */ |
| 311 | void bdrv_remove_aio_context_notifier(BlockDriverState *bs, |
| 312 | void (*aio_context_attached)(AioContext *, |
| 313 | void *), |
| 314 | void (*aio_context_detached)(void *), |
| 315 | void *opaque); |
| 316 | |
| 317 | /** |
| 318 | * End all quiescent sections started by bdrv_drain_all_begin(). This is |
| 319 | * needed when deleting a BDS before bdrv_drain_all_end() is called. |
| 320 | * |
| 321 | * NOTE: this is an internal helper for bdrv_close() *only*. No one else |
| 322 | * should call it. |
| 323 | */ |
| 324 | void bdrv_drain_all_end_quiesce(BlockDriverState *bs); |
| 325 | |
Markus Armbruster | ea9cea9 | 2022-05-06 15:49:11 +0200 | [diff] [blame] | 326 | #endif /* BLOCK_INT_GLOBAL_STATE_H */ |