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