xref: /openbmc/qemu/include/block/block-io.h (revision 7a5951f6)
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_IO_H
25 #define BLOCK_IO_H
26 
27 #include "block-common.h"
28 
29 /*
30  * I/O API functions. These functions are thread-safe, and therefore
31  * can run in any thread as long as the thread has called
32  * aio_context_acquire/release().
33  *
34  * These functions can only call functions from I/O and Common categories,
35  * but can be invoked by GS, "I/O or GS" and I/O APIs.
36  *
37  * All functions in this category must use the macro
38  * IO_CODE();
39  * to catch when they are accidentally called by the wrong API.
40  */
41 
42 int co_wrapper_mixed_bdrv_rdlock
43 bdrv_pwrite_zeroes(BdrvChild *child, int64_t offset, int64_t bytes,
44                    BdrvRequestFlags flags);
45 
46 int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags flags);
47 
48 int co_wrapper_mixed_bdrv_rdlock
49 bdrv_pread(BdrvChild *child, int64_t offset, int64_t bytes, void *buf,
50            BdrvRequestFlags flags);
51 
52 int co_wrapper_mixed_bdrv_rdlock
53 bdrv_pwrite(BdrvChild *child, int64_t offset,int64_t bytes,
54             const void *buf, BdrvRequestFlags flags);
55 
56 int co_wrapper_mixed_bdrv_rdlock
57 bdrv_pwrite_sync(BdrvChild *child, int64_t offset, int64_t bytes,
58                  const void *buf, BdrvRequestFlags flags);
59 
60 int coroutine_fn bdrv_co_pwrite_sync(BdrvChild *child, int64_t offset,
61                                      int64_t bytes, const void *buf,
62                                      BdrvRequestFlags flags);
63 /*
64  * Efficiently zero a region of the disk image.  Note that this is a regular
65  * I/O request like read or write and should have a reasonable size.  This
66  * function is not suitable for zeroing the entire image in a single request
67  * because it may allocate memory for the entire region.
68  */
69 int coroutine_fn bdrv_co_pwrite_zeroes(BdrvChild *child, int64_t offset,
70                                        int64_t bytes, BdrvRequestFlags flags);
71 
72 int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact,
73                                   PreallocMode prealloc, BdrvRequestFlags flags,
74                                   Error **errp);
75 
76 int64_t bdrv_nb_sectors(BlockDriverState *bs);
77 int64_t bdrv_getlength(BlockDriverState *bs);
78 int64_t bdrv_get_allocated_file_size(BlockDriverState *bs);
79 BlockMeasureInfo *bdrv_measure(BlockDriver *drv, QemuOpts *opts,
80                                BlockDriverState *in_bs, Error **errp);
81 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr);
82 int coroutine_fn bdrv_co_delete_file(BlockDriverState *bs, Error **errp);
83 void coroutine_fn bdrv_co_delete_file_noerr(BlockDriverState *bs);
84 
85 
86 /* async block I/O */
87 void bdrv_aio_cancel(BlockAIOCB *acb);
88 void bdrv_aio_cancel_async(BlockAIOCB *acb);
89 
90 /* sg packet commands */
91 int coroutine_fn bdrv_co_ioctl(BlockDriverState *bs, int req, void *buf);
92 
93 /* Ensure contents are flushed to disk.  */
94 int coroutine_fn bdrv_co_flush(BlockDriverState *bs);
95 
96 int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset,
97                                   int64_t bytes);
98 bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs);
99 int bdrv_block_status(BlockDriverState *bs, int64_t offset,
100                       int64_t bytes, int64_t *pnum, int64_t *map,
101                       BlockDriverState **file);
102 
103 int coroutine_fn bdrv_co_block_status_above(BlockDriverState *bs,
104                                             BlockDriverState *base,
105                                             int64_t offset, int64_t bytes,
106                                             int64_t *pnum, int64_t *map,
107                                             BlockDriverState **file);
108 int bdrv_block_status_above(BlockDriverState *bs, BlockDriverState *base,
109                             int64_t offset, int64_t bytes, int64_t *pnum,
110                             int64_t *map, BlockDriverState **file);
111 
112 int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t offset,
113                                       int64_t bytes, int64_t *pnum);
114 int bdrv_is_allocated(BlockDriverState *bs, int64_t offset, int64_t bytes,
115                       int64_t *pnum);
116 
117 int coroutine_fn bdrv_co_is_allocated_above(BlockDriverState *top,
118                                             BlockDriverState *base,
119                                             bool include_base, int64_t offset,
120                                             int64_t bytes, int64_t *pnum);
121 int bdrv_is_allocated_above(BlockDriverState *top, BlockDriverState *base,
122                             bool include_base, int64_t offset, int64_t bytes,
123                             int64_t *pnum);
124 
125 int coroutine_fn bdrv_co_is_zero_fast(BlockDriverState *bs, int64_t offset,
126                                       int64_t bytes);
127 
128 int bdrv_can_set_read_only(BlockDriverState *bs, bool read_only,
129                            bool ignore_allow_rdw, Error **errp);
130 int bdrv_apply_auto_read_only(BlockDriverState *bs, const char *errmsg,
131                               Error **errp);
132 bool bdrv_is_read_only(BlockDriverState *bs);
133 bool bdrv_is_writable(BlockDriverState *bs);
134 bool bdrv_is_sg(BlockDriverState *bs);
135 int bdrv_get_flags(BlockDriverState *bs);
136 bool bdrv_is_inserted(BlockDriverState *bs);
137 void bdrv_lock_medium(BlockDriverState *bs, bool locked);
138 void bdrv_eject(BlockDriverState *bs, bool eject_flag);
139 const char *bdrv_get_format_name(BlockDriverState *bs);
140 
141 bool bdrv_supports_compressed_writes(BlockDriverState *bs);
142 const char *bdrv_get_node_name(const BlockDriverState *bs);
143 const char *bdrv_get_device_name(const BlockDriverState *bs);
144 const char *bdrv_get_device_or_node_name(const BlockDriverState *bs);
145 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi);
146 ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs,
147                                           Error **errp);
148 BlockStatsSpecific *bdrv_get_specific_stats(BlockDriverState *bs);
149 void bdrv_round_to_clusters(BlockDriverState *bs,
150                             int64_t offset, int64_t bytes,
151                             int64_t *cluster_offset,
152                             int64_t *cluster_bytes);
153 
154 void bdrv_get_backing_filename(BlockDriverState *bs,
155                                char *filename, int filename_size);
156 
157 int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
158                       int64_t pos, int size);
159 
160 int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
161                       int64_t pos, int size);
162 
163 /*
164  * Returns the alignment in bytes that is required so that no bounce buffer
165  * is required throughout the stack
166  */
167 size_t bdrv_min_mem_align(BlockDriverState *bs);
168 /* Returns optimal alignment in bytes for bounce buffer */
169 size_t bdrv_opt_mem_align(BlockDriverState *bs);
170 void *qemu_blockalign(BlockDriverState *bs, size_t size);
171 void *qemu_blockalign0(BlockDriverState *bs, size_t size);
172 void *qemu_try_blockalign(BlockDriverState *bs, size_t size);
173 void *qemu_try_blockalign0(BlockDriverState *bs, size_t size);
174 
175 void bdrv_enable_copy_on_read(BlockDriverState *bs);
176 void bdrv_disable_copy_on_read(BlockDriverState *bs);
177 
178 void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event);
179 
180 #define BLKDBG_EVENT(child, evt) \
181     do { \
182         if (child) { \
183             bdrv_debug_event(child->bs, evt); \
184         } \
185     } while (0)
186 
187 /**
188  * bdrv_get_aio_context:
189  *
190  * Returns: the currently bound #AioContext
191  */
192 AioContext *bdrv_get_aio_context(BlockDriverState *bs);
193 
194 AioContext *bdrv_child_get_parent_aio_context(BdrvChild *c);
195 
196 /**
197  * Move the current coroutine to the AioContext of @bs and return the old
198  * AioContext of the coroutine. Increase bs->in_flight so that draining @bs
199  * will wait for the operation to proceed until the corresponding
200  * bdrv_co_leave().
201  *
202  * Consequently, you can't call drain inside a bdrv_co_enter/leave() section as
203  * this will deadlock.
204  */
205 AioContext *coroutine_fn bdrv_co_enter(BlockDriverState *bs);
206 
207 /**
208  * Ends a section started by bdrv_co_enter(). Move the current coroutine back
209  * to old_ctx and decrease bs->in_flight again.
210  */
211 void coroutine_fn bdrv_co_leave(BlockDriverState *bs, AioContext *old_ctx);
212 
213 /**
214  * Transfer control to @co in the aio context of @bs
215  */
216 void bdrv_coroutine_enter(BlockDriverState *bs, Coroutine *co);
217 
218 AioContext *child_of_bds_get_parent_aio_context(BdrvChild *c);
219 
220 void bdrv_io_plug(BlockDriverState *bs);
221 void bdrv_io_unplug(BlockDriverState *bs);
222 
223 bool coroutine_fn bdrv_co_can_store_new_dirty_bitmap(BlockDriverState *bs,
224                                                      const char *name,
225                                                      uint32_t granularity,
226                                                      Error **errp);
227 bool co_wrapper bdrv_can_store_new_dirty_bitmap(BlockDriverState *bs,
228                                                 const char *name,
229                                                 uint32_t granularity,
230                                                 Error **errp);
231 
232 /**
233  *
234  * bdrv_co_copy_range:
235  *
236  * Do offloaded copy between two children. If the operation is not implemented
237  * by the driver, or if the backend storage doesn't support it, a negative
238  * error code will be returned.
239  *
240  * Note: block layer doesn't emulate or fallback to a bounce buffer approach
241  * because usually the caller shouldn't attempt offloaded copy any more (e.g.
242  * calling copy_file_range(2)) after the first error, thus it should fall back
243  * to a read+write path in the caller level.
244  *
245  * @src: Source child to copy data from
246  * @src_offset: offset in @src image to read data
247  * @dst: Destination child to copy data to
248  * @dst_offset: offset in @dst image to write data
249  * @bytes: number of bytes to copy
250  * @flags: request flags. Supported flags:
251  *         BDRV_REQ_ZERO_WRITE - treat the @src range as zero data and do zero
252  *                               write on @dst as if bdrv_co_pwrite_zeroes is
253  *                               called. Used to simplify caller code, or
254  *                               during BlockDriver.bdrv_co_copy_range_from()
255  *                               recursion.
256  *         BDRV_REQ_NO_SERIALISING - do not serialize with other overlapping
257  *                                   requests currently in flight.
258  *
259  * Returns: 0 if succeeded; negative error code if failed.
260  **/
261 int coroutine_fn bdrv_co_copy_range(BdrvChild *src, int64_t src_offset,
262                                     BdrvChild *dst, int64_t dst_offset,
263                                     int64_t bytes, BdrvRequestFlags read_flags,
264                                     BdrvRequestFlags write_flags);
265 
266 /*
267  * "I/O or GS" API functions. These functions can run without
268  * the BQL, but only in one specific iothread/main loop.
269  *
270  * More specifically, these functions use BDRV_POLL_WHILE(bs), which
271  * requires the caller to be either in the main thread and hold
272  * the BlockdriverState (bs) AioContext lock, or directly in the
273  * home thread that runs the bs AioContext. Calling them from
274  * another thread in another AioContext would cause deadlocks.
275  *
276  * Therefore, these functions are not proper I/O, because they
277  * can't run in *any* iothreads, but only in a specific one.
278  *
279  * These functions can call any function from I/O, Common and this
280  * categories, but must be invoked only by other "I/O or GS" and GS APIs.
281  *
282  * All functions in this category must use the macro
283  * IO_OR_GS_CODE();
284  * to catch when they are accidentally called by the wrong API.
285  */
286 
287 #define BDRV_POLL_WHILE(bs, cond) ({                       \
288     BlockDriverState *bs_ = (bs);                          \
289     IO_OR_GS_CODE();                                       \
290     AIO_WAIT_WHILE(bdrv_get_aio_context(bs_),              \
291                    cond); })
292 
293 void bdrv_drain(BlockDriverState *bs);
294 
295 int co_wrapper_mixed_bdrv_rdlock
296 bdrv_truncate(BdrvChild *child, int64_t offset, bool exact,
297               PreallocMode prealloc, BdrvRequestFlags flags, Error **errp);
298 
299 int co_wrapper_mixed_bdrv_rdlock
300 bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix);
301 
302 /* Invalidate any cached metadata used by image formats */
303 int co_wrapper_mixed_bdrv_rdlock
304 bdrv_invalidate_cache(BlockDriverState *bs, Error **errp);
305 
306 int co_wrapper_mixed_bdrv_rdlock bdrv_flush(BlockDriverState *bs);
307 
308 int co_wrapper_mixed_bdrv_rdlock
309 bdrv_pdiscard(BdrvChild *child, int64_t offset, int64_t bytes);
310 
311 int co_wrapper_mixed_bdrv_rdlock
312 bdrv_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);
313 
314 int co_wrapper_mixed_bdrv_rdlock
315 bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);
316 
317 /**
318  * bdrv_parent_drained_begin_single:
319  *
320  * Begin a quiesced section for the parent of @c.
321  */
322 void bdrv_parent_drained_begin_single(BdrvChild *c);
323 
324 /**
325  * bdrv_parent_drained_poll_single:
326  *
327  * Returns true if there is any pending activity to cease before @c can be
328  * called quiesced, false otherwise.
329  */
330 bool bdrv_parent_drained_poll_single(BdrvChild *c);
331 
332 /**
333  * bdrv_parent_drained_end_single:
334  *
335  * End a quiesced section for the parent of @c.
336  */
337 void bdrv_parent_drained_end_single(BdrvChild *c);
338 
339 /**
340  * bdrv_drain_poll:
341  *
342  * Poll for pending requests in @bs and its parents (except for @ignore_parent).
343  *
344  * If @ignore_bds_parents is true, parents that are BlockDriverStates must
345  * ignore the drain request because they will be drained separately (used for
346  * drain_all).
347  *
348  * This is part of bdrv_drained_begin.
349  */
350 bool bdrv_drain_poll(BlockDriverState *bs, BdrvChild *ignore_parent,
351                      bool ignore_bds_parents);
352 
353 /**
354  * bdrv_drained_begin:
355  *
356  * Begin a quiesced section for exclusive access to the BDS, by disabling
357  * external request sources including NBD server, block jobs, and device model.
358  *
359  * This function can be recursive.
360  */
361 void bdrv_drained_begin(BlockDriverState *bs);
362 
363 /**
364  * bdrv_do_drained_begin_quiesce:
365  *
366  * Quiesces a BDS like bdrv_drained_begin(), but does not wait for already
367  * running requests to complete.
368  */
369 void bdrv_do_drained_begin_quiesce(BlockDriverState *bs, BdrvChild *parent);
370 
371 /**
372  * bdrv_drained_end:
373  *
374  * End a quiescent section started by bdrv_drained_begin().
375  */
376 void bdrv_drained_end(BlockDriverState *bs);
377 
378 #endif /* BLOCK_IO_H */
379