xref: /openbmc/qemu/include/block/block_int-io.h (revision b14df228)
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_INT_IO_H
25 #define BLOCK_INT_IO_H
26 
27 #include "block_int-common.h"
28 
29 /*
30  * I/O API functions. These functions are thread-safe.
31  *
32  * See include/block/block-io.h for more information about
33  * the I/O API.
34  */
35 
36 int coroutine_fn bdrv_co_preadv_snapshot(BdrvChild *child,
37     int64_t offset, int64_t bytes, QEMUIOVector *qiov, size_t qiov_offset);
38 int coroutine_fn bdrv_co_snapshot_block_status(BlockDriverState *bs,
39     bool want_zero, int64_t offset, int64_t bytes, int64_t *pnum,
40     int64_t *map, BlockDriverState **file);
41 int coroutine_fn bdrv_co_pdiscard_snapshot(BlockDriverState *bs,
42     int64_t offset, int64_t bytes);
43 
44 
45 int coroutine_fn bdrv_co_preadv(BdrvChild *child,
46     int64_t offset, int64_t bytes, QEMUIOVector *qiov,
47     BdrvRequestFlags flags);
48 int coroutine_fn bdrv_co_preadv_part(BdrvChild *child,
49     int64_t offset, int64_t bytes,
50     QEMUIOVector *qiov, size_t qiov_offset, BdrvRequestFlags flags);
51 int coroutine_fn bdrv_co_pwritev(BdrvChild *child,
52     int64_t offset, int64_t bytes, QEMUIOVector *qiov,
53     BdrvRequestFlags flags);
54 int coroutine_fn bdrv_co_pwritev_part(BdrvChild *child,
55     int64_t offset, int64_t bytes,
56     QEMUIOVector *qiov, size_t qiov_offset, BdrvRequestFlags flags);
57 
58 static inline int coroutine_fn bdrv_co_pread(BdrvChild *child,
59     int64_t offset, int64_t bytes, void *buf, BdrvRequestFlags flags)
60 {
61     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
62     IO_CODE();
63 
64     return bdrv_co_preadv(child, offset, bytes, &qiov, flags);
65 }
66 
67 static inline int coroutine_fn bdrv_co_pwrite(BdrvChild *child,
68     int64_t offset, int64_t bytes, const void *buf, BdrvRequestFlags flags)
69 {
70     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
71     IO_CODE();
72 
73     return bdrv_co_pwritev(child, offset, bytes, &qiov, flags);
74 }
75 
76 bool coroutine_fn bdrv_make_request_serialising(BdrvTrackedRequest *req,
77                                                 uint64_t align);
78 BdrvTrackedRequest *coroutine_fn bdrv_co_get_self_request(BlockDriverState *bs);
79 
80 BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size,
81                             const char *filename);
82 
83 /**
84  * bdrv_wakeup:
85  * @bs: The BlockDriverState for which an I/O operation has been completed.
86  *
87  * Wake up the main thread if it is waiting on BDRV_POLL_WHILE.  During
88  * synchronous I/O on a BlockDriverState that is attached to another
89  * I/O thread, the main thread lets the I/O thread's event loop run,
90  * waiting for the I/O operation to complete.  A bdrv_wakeup will wake
91  * up the main thread if necessary.
92  *
93  * Manual calls to bdrv_wakeup are rarely necessary, because
94  * bdrv_dec_in_flight already calls it.
95  */
96 void bdrv_wakeup(BlockDriverState *bs);
97 
98 const char *bdrv_get_parent_name(const BlockDriverState *bs);
99 bool blk_dev_has_tray(BlockBackend *blk);
100 bool blk_dev_is_tray_open(BlockBackend *blk);
101 
102 void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes);
103 
104 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out);
105 void bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest,
106                                       const BdrvDirtyBitmap *src,
107                                       HBitmap **backup, bool lock);
108 
109 void bdrv_inc_in_flight(BlockDriverState *bs);
110 void bdrv_dec_in_flight(BlockDriverState *bs);
111 
112 int coroutine_fn bdrv_co_copy_range_from(BdrvChild *src, int64_t src_offset,
113                                          BdrvChild *dst, int64_t dst_offset,
114                                          int64_t bytes,
115                                          BdrvRequestFlags read_flags,
116                                          BdrvRequestFlags write_flags);
117 int coroutine_fn bdrv_co_copy_range_to(BdrvChild *src, int64_t src_offset,
118                                        BdrvChild *dst, int64_t dst_offset,
119                                        int64_t bytes,
120                                        BdrvRequestFlags read_flags,
121                                        BdrvRequestFlags write_flags);
122 
123 int refresh_total_sectors(BlockDriverState *bs, int64_t hint);
124 
125 BdrvChild *bdrv_cow_child(BlockDriverState *bs);
126 BdrvChild *bdrv_filter_child(BlockDriverState *bs);
127 BdrvChild *bdrv_filter_or_cow_child(BlockDriverState *bs);
128 BdrvChild *bdrv_primary_child(BlockDriverState *bs);
129 BlockDriverState *bdrv_skip_filters(BlockDriverState *bs);
130 BlockDriverState *bdrv_backing_chain_next(BlockDriverState *bs);
131 
132 static inline BlockDriverState *bdrv_cow_bs(BlockDriverState *bs)
133 {
134     IO_CODE();
135     return child_bs(bdrv_cow_child(bs));
136 }
137 
138 static inline BlockDriverState *bdrv_filter_bs(BlockDriverState *bs)
139 {
140     IO_CODE();
141     return child_bs(bdrv_filter_child(bs));
142 }
143 
144 static inline BlockDriverState *bdrv_filter_or_cow_bs(BlockDriverState *bs)
145 {
146     IO_CODE();
147     return child_bs(bdrv_filter_or_cow_child(bs));
148 }
149 
150 static inline BlockDriverState *bdrv_primary_bs(BlockDriverState *bs)
151 {
152     IO_CODE();
153     return child_bs(bdrv_primary_child(bs));
154 }
155 
156 /**
157  * Check whether the given offset is in the cached block-status data
158  * region.
159  *
160  * If it is, and @pnum is not NULL, *pnum is set to
161  * `bsc.data_end - offset`, i.e. how many bytes, starting from
162  * @offset, are data (according to the cache).
163  * Otherwise, *pnum is not touched.
164  */
165 bool bdrv_bsc_is_data(BlockDriverState *bs, int64_t offset, int64_t *pnum);
166 
167 /**
168  * If [offset, offset + bytes) overlaps with the currently cached
169  * block-status region, invalidate the cache.
170  *
171  * (To be used by I/O paths that cause data regions to be zero or
172  * holes.)
173  */
174 void bdrv_bsc_invalidate_range(BlockDriverState *bs,
175                                int64_t offset, int64_t bytes);
176 
177 /**
178  * Mark the range [offset, offset + bytes) as a data region.
179  */
180 void bdrv_bsc_fill(BlockDriverState *bs, int64_t offset, int64_t bytes);
181 
182 
183 /*
184  * "I/O or GS" API functions. These functions can run without
185  * the BQL, but only in one specific iothread/main loop.
186  *
187  * See include/block/block-io.h for more information about
188  * the "I/O or GS" API.
189  */
190 
191 void bdrv_apply_subtree_drain(BdrvChild *child, BlockDriverState *new_parent);
192 void bdrv_unapply_subtree_drain(BdrvChild *child, BlockDriverState *old_parent);
193 
194 #endif /* BLOCK_INT_IO_H */
195