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