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  * @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.
91  *
92  */
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);
99 /**
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.
116  *
117  */
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);
124 /*
125  * mirror_start:
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.
148  *
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.
153  */
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,
159                   bool zero_target,
160                   BlockdevOnError on_source_error,
161                   BlockdevOnError on_target_error,
162                   bool unmap, const char *filter_node_name,
163                   MirrorCopyMode copy_mode, Error **errp);
164 
165 /*
166  * backup_job_create:
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).
184  *
185  * Create a backup operation on @bs.  Clusters in @bs are written to @target
186  * until the job is cancelled or manually completed.
187  */
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,
193                             bool compress,
194                             const char *filter_node_name,
195                             BackupPerf *perf,
196                             BlockdevOnError on_source_error,
197                             BlockdevOnError on_target_error,
198                             int creation_flags,
199                             BlockCompletionFunc *cb, void *opaque,
200                             JobTxn *txn, Error **errp);
201 
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);
208 
209 void GRAPH_WRLOCK bdrv_root_unref_child(BdrvChild *child);
210 
211 void GRAPH_RDLOCK bdrv_get_cumulative_perm(BlockDriverState *bs, uint64_t *perm,
212                                            uint64_t *shared_perm);
213 
214 /**
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.
218  */
219 int GRAPH_RDLOCK
220 bdrv_child_try_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared,
221                         Error **errp);
222 
223 /**
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.
230  */
231 int GRAPH_RDLOCK
232 bdrv_child_refresh_perms(BlockDriverState *bs, BdrvChild *c, Error **errp);
233 
234 bool GRAPH_RDLOCK bdrv_recurse_can_replace(BlockDriverState *bs,
235                                            BlockDriverState *to_replace);
236 
237 /*
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.
241  */
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);
246 
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);
251 
252 void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup);
253 
254 void bdrv_set_monitor_owned(BlockDriverState *bs);
255 
256 void blockdev_close_all_bdrv_states(void);
257 
258 BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp);
259 
260 /**
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)
264  */
265 int coroutine_fn bdrv_co_create_opts_simple(BlockDriver *drv,
266                                             const char *filename,
267                                             QemuOpts *opts,
268                                             Error **errp);
269 
270 BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
271                                            const char *name,
272                                            BlockDriverState **pbs,
273                                            Error **errp);
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,
278                                            bool release,
279                                            BlockDriverState **bitmap_bs,
280                                            Error **errp);
281 
282 
283 BlockDriverState * GRAPH_RDLOCK
284 bdrv_skip_implicit_filters(BlockDriverState *bs);
285 
286 /**
287  * bdrv_add_aio_context_notifier:
288  *
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.
292  *
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.
296  */
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);
300 
301 /**
302  * bdrv_remove_aio_context_notifier:
303  *
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().
307  */
308 void bdrv_remove_aio_context_notifier(BlockDriverState *bs,
309                                       void (*aio_context_attached)(AioContext *,
310                                                                    void *),
311                                       void (*aio_context_detached)(void *),
312                                       void *opaque);
313 
314 /**
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.
317  *
318  * NOTE: this is an internal helper for bdrv_close() *only*. No one else
319  * should call it.
320  */
321 void bdrv_drain_all_end_quiesce(BlockDriverState *bs);
322 
323 #endif /* BLOCK_INT_GLOBAL_STATE_H */
324