2 * QEMU System Emulator block driver
4 * Copyright (c) 2003 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #ifndef BLOCK_INT_GLOBAL_STATE_H
26 #define BLOCK_INT_GLOBAL_STATE_H
28 #include "block/blockjob.h"
29 #include "block/block_int-common.h"
30 #include "qemu/hbitmap.h"
31 #include "qemu/main-loop.h"
34 * Global state (GS) API. These functions run under the BQL.
36 * See include/block/block-global-state.h for more information about
42 * @job_id: The id of the newly-created job, or %NULL to use the
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.
49 * @creation_flags: Flags that control the behavior of the Job lifetime.
50 * See @BlockJobCreateFlags
51 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
52 * @on_error: The action to take upon error.
53 * @filter_node_name: The node name that should be assigned to the filter
54 * driver that the stream job inserts into the graph above
55 * @bs. NULL means that a node name should be autogenerated.
56 * @errp: Error object.
58 * Start a streaming operation on @bs. Clusters that are unallocated
59 * in @bs, but allocated in any image between @base and @bs (both
60 * exclusive) will be written to @bs. At the end of a successful
61 * streaming job, the backing file of @bs will be changed to
62 * @backing_file_str in the written image and to @base in the live
65 void stream_start(const char *job_id
, BlockDriverState
*bs
,
66 BlockDriverState
*base
, const char *backing_file_str
,
67 BlockDriverState
*bottom
,
68 int creation_flags
, int64_t speed
,
69 BlockdevOnError on_error
,
70 const char *filter_node_name
,
75 * @job_id: The id of the newly-created job, or %NULL to use the
77 * @bs: Active block device.
78 * @top: Top block device to be committed.
79 * @base: Block device that will be written into, and become the new top.
80 * @creation_flags: Flags that control the behavior of the Job lifetime.
81 * See @BlockJobCreateFlags
82 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
83 * @on_error: The action to take upon error.
84 * @backing_file_str: String to use as the backing file in @top's overlay
85 * @backing_mask_protocol: Replace potential protocol name with 'raw' in
86 * 'backing file format' header
87 * @filter_node_name: The node name that should be assigned to the filter
88 * driver that the commit job inserts into the graph above @top. NULL means
89 * that a node name should be autogenerated.
90 * @errp: Error object.
93 void commit_start(const char *job_id
, BlockDriverState
*bs
,
94 BlockDriverState
*base
, BlockDriverState
*top
,
95 int creation_flags
, int64_t speed
,
96 BlockdevOnError on_error
, const char *backing_file_str
,
97 bool backing_mask_protocol
,
98 const char *filter_node_name
, Error
**errp
);
100 * commit_active_start:
101 * @job_id: The id of the newly-created job, or %NULL to use the
102 * device name of @bs.
103 * @bs: Active block device to be committed.
104 * @base: Block device that will be written into, and become the new top.
105 * @creation_flags: Flags that control the behavior of the Job lifetime.
106 * See @BlockJobCreateFlags
107 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
108 * @on_error: The action to take upon error.
109 * @filter_node_name: The node name that should be assigned to the filter
110 * driver that the commit job inserts into the graph above @bs. NULL means that
111 * a node name should be autogenerated.
112 * @cb: Completion function for the job.
113 * @opaque: Opaque pointer value passed to @cb.
114 * @auto_complete: Auto complete the job.
115 * @errp: Error object.
118 BlockJob
*commit_active_start(const char *job_id
, BlockDriverState
*bs
,
119 BlockDriverState
*base
, int creation_flags
,
120 int64_t speed
, BlockdevOnError on_error
,
121 const char *filter_node_name
,
122 BlockCompletionFunc
*cb
, void *opaque
,
123 bool auto_complete
, Error
**errp
);
126 * @job_id: The id of the newly-created job, or %NULL to use the
127 * device name of @bs.
128 * @bs: Block device to operate on.
129 * @target: Block device to write to.
130 * @replaces: Block graph node name to replace once the mirror is done. Can
131 * only be used when full mirroring is selected.
132 * @creation_flags: Flags that control the behavior of the Job lifetime.
133 * See @BlockJobCreateFlags
134 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
135 * @granularity: The chosen granularity for the dirty bitmap.
136 * @buf_size: The amount of data that can be in flight at one time.
137 * @mode: Whether to collapse all images in the chain to the target.
138 * @backing_mode: How to establish the target's backing chain after completion.
139 * @zero_target: Whether the target should be explicitly zero-initialized
140 * @on_source_error: The action to take upon error reading from the source.
141 * @on_target_error: The action to take upon error writing to the target.
142 * @unmap: Whether to unmap target where source sectors only contain zeroes.
143 * @filter_node_name: The node name that should be assigned to the filter
144 * driver that the mirror job inserts into the graph above @bs. NULL means that
145 * a node name should be autogenerated.
146 * @copy_mode: When to trigger writes to the target.
147 * @errp: Error object.
149 * Start a mirroring operation on @bs. Clusters that are allocated
150 * in @bs will be written to @target until the job is cancelled or
151 * manually completed. At the end of a successful mirroring job,
152 * @bs will be switched to read from @target.
154 void mirror_start(const char *job_id
, BlockDriverState
*bs
,
155 BlockDriverState
*target
, const char *replaces
,
156 int creation_flags
, int64_t speed
,
157 uint32_t granularity
, int64_t buf_size
,
158 MirrorSyncMode mode
, BlockMirrorBackingMode backing_mode
,
160 BlockdevOnError on_source_error
,
161 BlockdevOnError on_target_error
,
162 bool unmap
, const char *filter_node_name
,
163 MirrorCopyMode copy_mode
, Error
**errp
);
167 * @job_id: The id of the newly-created job, or %NULL to use the
168 * device name of @bs.
169 * @bs: Block device to operate on.
170 * @target: Block device to write to.
171 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
172 * @sync_mode: What parts of the disk image should be copied to the destination.
173 * @sync_bitmap: The dirty bitmap if sync_mode is 'bitmap' or 'incremental'
174 * @bitmap_mode: The bitmap synchronization policy to use.
175 * @perf: Performance options. All actual fields assumed to be present,
176 * all ".has_*" fields are ignored.
177 * @on_source_error: The action to take upon error reading from the source.
178 * @on_target_error: The action to take upon error writing to the target.
179 * @creation_flags: Flags that control the behavior of the Job lifetime.
180 * See @BlockJobCreateFlags
181 * @cb: Completion function for the job.
182 * @opaque: Opaque pointer value passed to @cb.
183 * @txn: Transaction that this job is part of (may be NULL).
185 * Create a backup operation on @bs. Clusters in @bs are written to @target
186 * until the job is cancelled or manually completed.
188 BlockJob
*backup_job_create(const char *job_id
, BlockDriverState
*bs
,
189 BlockDriverState
*target
, int64_t speed
,
190 MirrorSyncMode sync_mode
,
191 BdrvDirtyBitmap
*sync_bitmap
,
192 BitmapSyncMode bitmap_mode
,
194 const char *filter_node_name
,
196 BlockdevOnError on_source_error
,
197 BlockdevOnError on_target_error
,
199 BlockCompletionFunc
*cb
, void *opaque
,
200 JobTxn
*txn
, Error
**errp
);
202 BdrvChild
* GRAPH_WRLOCK
203 bdrv_root_attach_child(BlockDriverState
*child_bs
, const char *child_name
,
204 const BdrvChildClass
*child_class
,
205 BdrvChildRole child_role
,
206 uint64_t perm
, uint64_t shared_perm
,
207 void *opaque
, Error
**errp
);
209 void GRAPH_WRLOCK
bdrv_root_unref_child(BdrvChild
*child
);
211 void GRAPH_RDLOCK
bdrv_get_cumulative_perm(BlockDriverState
*bs
, uint64_t *perm
,
212 uint64_t *shared_perm
);
215 * Sets a BdrvChild's permissions. Avoid if the parent is a BDS; use
216 * bdrv_child_refresh_perms() instead and make the parent's
217 * .bdrv_child_perm() implementation return the correct values.
220 bdrv_child_try_set_perm(BdrvChild
*c
, uint64_t perm
, uint64_t shared
,
224 * Calls bs->drv->bdrv_child_perm() and updates the child's permission
225 * masks with the result.
226 * Drivers should invoke this function whenever an event occurs that
227 * makes their .bdrv_child_perm() implementation return different
228 * values than before, but which will not result in the block layer
229 * automatically refreshing the permissions.
232 bdrv_child_refresh_perms(BlockDriverState
*bs
, BdrvChild
*c
, Error
**errp
);
234 bool GRAPH_RDLOCK
bdrv_recurse_can_replace(BlockDriverState
*bs
,
235 BlockDriverState
*to_replace
);
238 * Default implementation for BlockDriver.bdrv_child_perm() that can
239 * be used by block filters and image formats, as long as they use the
240 * child_of_bds child class and set an appropriate BdrvChildRole.
242 void bdrv_default_perms(BlockDriverState
*bs
, BdrvChild
*c
,
243 BdrvChildRole role
, BlockReopenQueue
*reopen_queue
,
244 uint64_t perm
, uint64_t shared
,
245 uint64_t *nperm
, uint64_t *nshared
);
247 void blk_dev_change_media_cb(BlockBackend
*blk
, bool load
, Error
**errp
);
248 bool blk_dev_has_removable_media(BlockBackend
*blk
);
249 void blk_dev_eject_request(BlockBackend
*blk
, bool force
);
250 bool blk_dev_is_medium_locked(BlockBackend
*blk
);
252 void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap
*bitmap
, HBitmap
*backup
);
254 void bdrv_set_monitor_owned(BlockDriverState
*bs
);
256 void blockdev_close_all_bdrv_states(void);
258 BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
);
261 * Simple implementation of bdrv_co_create_opts for protocol drivers
262 * which only support creation via opening a file
263 * (usually existing raw storage device)
265 int coroutine_fn
bdrv_co_create_opts_simple(BlockDriver
*drv
,
266 const char *filename
,
270 BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
272 BlockDriverState
**pbs
,
274 BdrvDirtyBitmap
*block_dirty_bitmap_merge(const char *node
, const char *target
,
275 BlockDirtyBitmapOrStrList
*bms
,
276 HBitmap
**backup
, Error
**errp
);
277 BdrvDirtyBitmap
*block_dirty_bitmap_remove(const char *node
, const char *name
,
279 BlockDriverState
**bitmap_bs
,
283 BlockDriverState
* GRAPH_RDLOCK
284 bdrv_skip_implicit_filters(BlockDriverState
*bs
);
287 * bdrv_add_aio_context_notifier:
289 * If a long-running job intends to be always run in the same AioContext as a
290 * certain BDS, it may use this function to be notified of changes regarding the
291 * association of the BDS to an AioContext.
293 * attached_aio_context() is called after the target BDS has been attached to a
294 * new AioContext; detach_aio_context() is called before the target BDS is being
295 * detached from its old AioContext.
297 void bdrv_add_aio_context_notifier(BlockDriverState
*bs
,
298 void (*attached_aio_context
)(AioContext
*new_context
, void *opaque
),
299 void (*detach_aio_context
)(void *opaque
), void *opaque
);
302 * bdrv_remove_aio_context_notifier:
304 * Unsubscribe of change notifications regarding the BDS's AioContext. The
305 * parameters given here have to be the same as those given to
306 * bdrv_add_aio_context_notifier().
308 void bdrv_remove_aio_context_notifier(BlockDriverState
*bs
,
309 void (*aio_context_attached
)(AioContext
*,
311 void (*aio_context_detached
)(void *),
315 * End all quiescent sections started by bdrv_drain_all_begin(). This is
316 * needed when deleting a BDS before bdrv_drain_all_end() is called.
318 * NOTE: this is an internal helper for bdrv_close() *only*. No one else
321 void bdrv_drain_all_end_quiesce(BlockDriverState
*bs
);
323 #endif /* BLOCK_INT_GLOBAL_STATE_H */