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