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