13b491a90SEmanuele Giuseppe Esposito /* 23b491a90SEmanuele Giuseppe Esposito * QEMU System Emulator block driver 33b491a90SEmanuele Giuseppe Esposito * 43b491a90SEmanuele Giuseppe Esposito * Copyright (c) 2003 Fabrice Bellard 53b491a90SEmanuele Giuseppe Esposito * 63b491a90SEmanuele Giuseppe Esposito * Permission is hereby granted, free of charge, to any person obtaining a copy 73b491a90SEmanuele Giuseppe Esposito * of this software and associated documentation files (the "Software"), to deal 83b491a90SEmanuele Giuseppe Esposito * in the Software without restriction, including without limitation the rights 93b491a90SEmanuele Giuseppe Esposito * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 103b491a90SEmanuele Giuseppe Esposito * copies of the Software, and to permit persons to whom the Software is 113b491a90SEmanuele Giuseppe Esposito * furnished to do so, subject to the following conditions: 123b491a90SEmanuele Giuseppe Esposito * 133b491a90SEmanuele Giuseppe Esposito * The above copyright notice and this permission notice shall be included in 143b491a90SEmanuele Giuseppe Esposito * all copies or substantial portions of the Software. 153b491a90SEmanuele Giuseppe Esposito * 163b491a90SEmanuele Giuseppe Esposito * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 173b491a90SEmanuele Giuseppe Esposito * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 183b491a90SEmanuele Giuseppe Esposito * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 193b491a90SEmanuele Giuseppe Esposito * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 203b491a90SEmanuele Giuseppe Esposito * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 213b491a90SEmanuele Giuseppe Esposito * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 223b491a90SEmanuele Giuseppe Esposito * THE SOFTWARE. 233b491a90SEmanuele Giuseppe Esposito */ 243b491a90SEmanuele Giuseppe Esposito #ifndef BLOCK_IO_H 253b491a90SEmanuele Giuseppe Esposito #define BLOCK_IO_H 263b491a90SEmanuele Giuseppe Esposito 27e2c1c34fSMarkus Armbruster #include "block/aio-wait.h" 28e2c1c34fSMarkus Armbruster #include "block/block-common.h" 29e2c1c34fSMarkus Armbruster #include "qemu/coroutine.h" 30e2c1c34fSMarkus Armbruster #include "qemu/iov.h" 313b491a90SEmanuele Giuseppe Esposito 323b491a90SEmanuele Giuseppe Esposito /* 333b491a90SEmanuele Giuseppe Esposito * I/O API functions. These functions are thread-safe, and therefore 34b49f4755SStefan Hajnoczi * can run in any thread. 353b491a90SEmanuele Giuseppe Esposito * 363b491a90SEmanuele Giuseppe Esposito * These functions can only call functions from I/O and Common categories, 373b491a90SEmanuele Giuseppe Esposito * but can be invoked by GS, "I/O or GS" and I/O APIs. 383b491a90SEmanuele Giuseppe Esposito * 393b491a90SEmanuele Giuseppe Esposito * All functions in this category must use the macro 403b491a90SEmanuele Giuseppe Esposito * IO_CODE(); 413b491a90SEmanuele Giuseppe Esposito * to catch when they are accidentally called by the wrong API. 423b491a90SEmanuele Giuseppe Esposito */ 433b491a90SEmanuele Giuseppe Esposito 4490830f59SEmanuele Giuseppe Esposito int co_wrapper_mixed_bdrv_rdlock 4590830f59SEmanuele Giuseppe Esposito bdrv_pwrite_zeroes(BdrvChild *child, int64_t offset, int64_t bytes, 4653fb7844SAlberto Faria BdrvRequestFlags flags); 4790830f59SEmanuele Giuseppe Esposito 481d39c709SAlberto Faria int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags flags); 4990830f59SEmanuele Giuseppe Esposito 5090830f59SEmanuele Giuseppe Esposito int co_wrapper_mixed_bdrv_rdlock 5190830f59SEmanuele Giuseppe Esposito bdrv_pread(BdrvChild *child, int64_t offset, int64_t bytes, void *buf, 521d39c709SAlberto Faria BdrvRequestFlags flags); 5390830f59SEmanuele Giuseppe Esposito 5490830f59SEmanuele Giuseppe Esposito int co_wrapper_mixed_bdrv_rdlock 5590830f59SEmanuele Giuseppe Esposito bdrv_pwrite(BdrvChild *child, int64_t offset,int64_t bytes, 5690830f59SEmanuele Giuseppe Esposito const void *buf, BdrvRequestFlags flags); 5790830f59SEmanuele Giuseppe Esposito 5890830f59SEmanuele Giuseppe Esposito int co_wrapper_mixed_bdrv_rdlock 5990830f59SEmanuele Giuseppe Esposito bdrv_pwrite_sync(BdrvChild *child, int64_t offset, int64_t bytes, 6090830f59SEmanuele Giuseppe Esposito const void *buf, BdrvRequestFlags flags); 6190830f59SEmanuele Giuseppe Esposito 62b24a4c41SKevin Wolf int coroutine_fn GRAPH_RDLOCK 63b24a4c41SKevin Wolf bdrv_co_pwrite_sync(BdrvChild *child, int64_t offset, int64_t bytes, 64b24a4c41SKevin Wolf const void *buf, BdrvRequestFlags flags); 65b24a4c41SKevin Wolf 663b491a90SEmanuele Giuseppe Esposito /* 673b491a90SEmanuele Giuseppe Esposito * Efficiently zero a region of the disk image. Note that this is a regular 683b491a90SEmanuele Giuseppe Esposito * I/O request like read or write and should have a reasonable size. This 693b491a90SEmanuele Giuseppe Esposito * function is not suitable for zeroing the entire image in a single request 703b491a90SEmanuele Giuseppe Esposito * because it may allocate memory for the entire region. 713b491a90SEmanuele Giuseppe Esposito */ 72abaf8b75SKevin Wolf int coroutine_fn GRAPH_RDLOCK 73abaf8b75SKevin Wolf bdrv_co_pwrite_zeroes(BdrvChild *child, int64_t offset, int64_t bytes, 74abaf8b75SKevin Wolf BdrvRequestFlags flags); 753b491a90SEmanuele Giuseppe Esposito 76c2b8e315SKevin Wolf int coroutine_fn GRAPH_RDLOCK 77c2b8e315SKevin Wolf bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact, 78c2b8e315SKevin Wolf PreallocMode prealloc, BdrvRequestFlags flags, Error **errp); 793b491a90SEmanuele Giuseppe Esposito 808ab8140aSKevin Wolf int64_t coroutine_fn GRAPH_RDLOCK bdrv_co_nb_sectors(BlockDriverState *bs); 8181f730d4SPaolo Bonzini int64_t coroutine_mixed_fn bdrv_nb_sectors(BlockDriverState *bs); 82c86422c5SEmanuele Giuseppe Esposito 838ab8140aSKevin Wolf int64_t coroutine_fn GRAPH_RDLOCK bdrv_co_getlength(BlockDriverState *bs); 848ab8140aSKevin Wolf int64_t co_wrapper_mixed_bdrv_rdlock bdrv_getlength(BlockDriverState *bs); 85c86422c5SEmanuele Giuseppe Esposito 86de335638SEmanuele Giuseppe Esposito int64_t coroutine_fn GRAPH_RDLOCK 87de335638SEmanuele Giuseppe Esposito bdrv_co_get_allocated_file_size(BlockDriverState *bs); 88de335638SEmanuele Giuseppe Esposito 89de335638SEmanuele Giuseppe Esposito int64_t co_wrapper_bdrv_rdlock 90de335638SEmanuele Giuseppe Esposito bdrv_get_allocated_file_size(BlockDriverState *bs); 9182618d7bSEmanuele Giuseppe Esposito 923b491a90SEmanuele Giuseppe Esposito BlockMeasureInfo *bdrv_measure(BlockDriver *drv, QemuOpts *opts, 933b491a90SEmanuele Giuseppe Esposito BlockDriverState *in_bs, Error **errp); 94d8fbf9aaSKevin Wolf 9548aef794SKevin Wolf int coroutine_fn GRAPH_RDLOCK 9648aef794SKevin Wolf bdrv_co_delete_file(BlockDriverState *bs, Error **errp); 9748aef794SKevin Wolf 9848aef794SKevin Wolf void coroutine_fn GRAPH_RDLOCK 9948aef794SKevin Wolf bdrv_co_delete_file_noerr(BlockDriverState *bs); 1003b491a90SEmanuele Giuseppe Esposito 1013b491a90SEmanuele Giuseppe Esposito 1023b491a90SEmanuele Giuseppe Esposito /* async block I/O */ 1033b491a90SEmanuele Giuseppe Esposito void bdrv_aio_cancel_async(BlockAIOCB *acb); 1043b491a90SEmanuele Giuseppe Esposito 1053b491a90SEmanuele Giuseppe Esposito /* sg packet commands */ 10626c518abSKevin Wolf int coroutine_fn GRAPH_RDLOCK 10726c518abSKevin Wolf bdrv_co_ioctl(BlockDriverState *bs, int req, void *buf); 1083b491a90SEmanuele Giuseppe Esposito 1093b491a90SEmanuele Giuseppe Esposito /* Ensure contents are flushed to disk. */ 11088095349SEmanuele Giuseppe Esposito int coroutine_fn GRAPH_RDLOCK bdrv_co_flush(BlockDriverState *bs); 1113b491a90SEmanuele Giuseppe Esposito 1129a5a1c62SEmanuele Giuseppe Esposito int coroutine_fn GRAPH_RDLOCK bdrv_co_pdiscard(BdrvChild *child, int64_t offset, 11316bb776fSAlberto Faria int64_t bytes); 1149a5a1c62SEmanuele Giuseppe Esposito 1156d43eaa3SSam Li /* Report zone information of zone block device. */ 1166d43eaa3SSam Li int coroutine_fn GRAPH_RDLOCK bdrv_co_zone_report(BlockDriverState *bs, 1176d43eaa3SSam Li int64_t offset, 1186d43eaa3SSam Li unsigned int *nr_zones, 1196d43eaa3SSam Li BlockZoneDescriptor *zones); 1206d43eaa3SSam Li int coroutine_fn GRAPH_RDLOCK bdrv_co_zone_mgmt(BlockDriverState *bs, 1216d43eaa3SSam Li BlockZoneOp op, 1226d43eaa3SSam Li int64_t offset, int64_t len); 1234751d09aSSam Li int coroutine_fn GRAPH_RDLOCK bdrv_co_zone_append(BlockDriverState *bs, 1244751d09aSSam Li int64_t *offset, 1254751d09aSSam Li QEMUIOVector *qiov, 1264751d09aSSam Li BdrvRequestFlags flags); 1276d43eaa3SSam Li 1283b491a90SEmanuele Giuseppe Esposito bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs); 1291b88457eSPaolo Bonzini 1301b88457eSPaolo Bonzini int coroutine_fn GRAPH_RDLOCK 1311b88457eSPaolo Bonzini bdrv_co_block_status(BlockDriverState *bs, int64_t offset, int64_t bytes, 1321b88457eSPaolo Bonzini int64_t *pnum, int64_t *map, BlockDriverState **file); 1331b88457eSPaolo Bonzini int co_wrapper_mixed_bdrv_rdlock 1341b88457eSPaolo Bonzini bdrv_block_status(BlockDriverState *bs, int64_t offset, int64_t bytes, 1351b88457eSPaolo Bonzini int64_t *pnum, int64_t *map, BlockDriverState **file); 1367b52a921SEmanuele Giuseppe Esposito 1377ff9579eSKevin Wolf int coroutine_fn GRAPH_RDLOCK 1387ff9579eSKevin Wolf bdrv_co_block_status_above(BlockDriverState *bs, BlockDriverState *base, 1397ff9579eSKevin Wolf int64_t offset, int64_t bytes, int64_t *pnum, 1407ff9579eSKevin Wolf int64_t *map, BlockDriverState **file); 1411b88457eSPaolo Bonzini int co_wrapper_mixed_bdrv_rdlock 1421b88457eSPaolo Bonzini bdrv_block_status_above(BlockDriverState *bs, BlockDriverState *base, 1433b491a90SEmanuele Giuseppe Esposito int64_t offset, int64_t bytes, int64_t *pnum, 1443b491a90SEmanuele Giuseppe Esposito int64_t *map, BlockDriverState **file); 1457b52a921SEmanuele Giuseppe Esposito 1467ff9579eSKevin Wolf int coroutine_fn GRAPH_RDLOCK 1477ff9579eSKevin Wolf bdrv_co_is_allocated(BlockDriverState *bs, int64_t offset, int64_t bytes, 1487ff9579eSKevin Wolf int64_t *pnum); 149578ffa9fSPaolo Bonzini int co_wrapper_mixed_bdrv_rdlock 150578ffa9fSPaolo Bonzini bdrv_is_allocated(BlockDriverState *bs, int64_t offset, 151578ffa9fSPaolo Bonzini int64_t bytes, int64_t *pnum); 1527b52a921SEmanuele Giuseppe Esposito 1537ff9579eSKevin Wolf int coroutine_fn GRAPH_RDLOCK 1547ff9579eSKevin Wolf bdrv_co_is_allocated_above(BlockDriverState *top, BlockDriverState *base, 1557ff9579eSKevin Wolf bool include_base, int64_t offset, int64_t bytes, 1567ff9579eSKevin Wolf int64_t *pnum); 157578ffa9fSPaolo Bonzini int co_wrapper_mixed_bdrv_rdlock 158578ffa9fSPaolo Bonzini bdrv_is_allocated_above(BlockDriverState *bs, BlockDriverState *base, 159578ffa9fSPaolo Bonzini bool include_base, int64_t offset, 160578ffa9fSPaolo Bonzini int64_t bytes, int64_t *pnum); 1617b52a921SEmanuele Giuseppe Esposito 162abaf8b75SKevin Wolf int coroutine_fn GRAPH_RDLOCK 163abaf8b75SKevin Wolf bdrv_co_is_zero_fast(BlockDriverState *bs, int64_t offset, int64_t bytes); 1643b491a90SEmanuele Giuseppe Esposito 165018f9deaSKevin Wolf int GRAPH_RDLOCK 166018f9deaSKevin Wolf bdrv_apply_auto_read_only(BlockDriverState *bs, const char *errmsg, 1673b491a90SEmanuele Giuseppe Esposito Error **errp); 168018f9deaSKevin Wolf 1693b491a90SEmanuele Giuseppe Esposito bool bdrv_is_read_only(BlockDriverState *bs); 1703b491a90SEmanuele Giuseppe Esposito bool bdrv_is_writable(BlockDriverState *bs); 1713b491a90SEmanuele Giuseppe Esposito bool bdrv_is_sg(BlockDriverState *bs); 17215aee7acSHanna Reitz int bdrv_get_flags(BlockDriverState *bs); 1731e97be91SEmanuele Giuseppe Esposito 174c73ff92cSEmanuele Giuseppe Esposito bool coroutine_fn GRAPH_RDLOCK bdrv_co_is_inserted(BlockDriverState *bs); 175c73ff92cSEmanuele Giuseppe Esposito bool co_wrapper_bdrv_rdlock bdrv_is_inserted(BlockDriverState *bs); 1761e97be91SEmanuele Giuseppe Esposito 17779a292e5SKevin Wolf void coroutine_fn GRAPH_RDLOCK 17879a292e5SKevin Wolf bdrv_co_lock_medium(BlockDriverState *bs, bool locked); 17979a292e5SKevin Wolf 18079a292e5SKevin Wolf void coroutine_fn GRAPH_RDLOCK 18179a292e5SKevin Wolf bdrv_co_eject(BlockDriverState *bs, bool eject_flag); 1822531b390SEmanuele Giuseppe Esposito 1833b491a90SEmanuele Giuseppe Esposito const char *bdrv_get_format_name(BlockDriverState *bs); 1843b491a90SEmanuele Giuseppe Esposito 185f5a3a270SKevin Wolf bool GRAPH_RDLOCK bdrv_supports_compressed_writes(BlockDriverState *bs); 1863b491a90SEmanuele Giuseppe Esposito const char *bdrv_get_node_name(const BlockDriverState *bs); 1874026f1c4SKevin Wolf 1884026f1c4SKevin Wolf const char * GRAPH_RDLOCK 1894026f1c4SKevin Wolf bdrv_get_device_name(const BlockDriverState *bs); 1904026f1c4SKevin Wolf 1914026f1c4SKevin Wolf const char * GRAPH_RDLOCK 1924026f1c4SKevin Wolf bdrv_get_device_or_node_name(const BlockDriverState *bs); 1933d47eb0aSEmanuele Giuseppe Esposito 194a00e70c0SEmanuele Giuseppe Esposito int coroutine_fn GRAPH_RDLOCK 195a00e70c0SEmanuele Giuseppe Esposito bdrv_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi); 196a00e70c0SEmanuele Giuseppe Esposito 197a00e70c0SEmanuele Giuseppe Esposito int co_wrapper_mixed_bdrv_rdlock 198a00e70c0SEmanuele Giuseppe Esposito bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi); 1993d47eb0aSEmanuele Giuseppe Esposito 2003574499aSKevin Wolf ImageInfoSpecific * GRAPH_RDLOCK 2013574499aSKevin Wolf bdrv_get_specific_info(BlockDriverState *bs, Error **errp); 2023574499aSKevin Wolf 2033b491a90SEmanuele Giuseppe Esposito BlockStatsSpecific *bdrv_get_specific_stats(BlockDriverState *bs); 204fc6b211fSAndrey Drobyshev void bdrv_round_to_subclusters(BlockDriverState *bs, 2053b491a90SEmanuele Giuseppe Esposito int64_t offset, int64_t bytes, 2063b491a90SEmanuele Giuseppe Esposito int64_t *cluster_offset, 2073b491a90SEmanuele Giuseppe Esposito int64_t *cluster_bytes); 2083b491a90SEmanuele Giuseppe Esposito 2093b491a90SEmanuele Giuseppe Esposito void bdrv_get_backing_filename(BlockDriverState *bs, 2103b491a90SEmanuele Giuseppe Esposito char *filename, int filename_size); 2113b491a90SEmanuele Giuseppe Esposito 212e2dd2737SKevin Wolf int coroutine_fn GRAPH_RDLOCK 213e2dd2737SKevin Wolf bdrv_co_change_backing_file(BlockDriverState *bs, const char *backing_file, 214e2dd2737SKevin Wolf const char *backing_fmt, bool warn); 215e2dd2737SKevin Wolf 216e2dd2737SKevin Wolf int co_wrapper_bdrv_rdlock 217e2dd2737SKevin Wolf bdrv_change_backing_file(BlockDriverState *bs, const char *backing_file, 218e2dd2737SKevin Wolf const char *backing_fmt, bool warn); 219e2dd2737SKevin Wolf 2203b491a90SEmanuele Giuseppe Esposito int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 2213b491a90SEmanuele Giuseppe Esposito int64_t pos, int size); 2223b491a90SEmanuele Giuseppe Esposito 2233b491a90SEmanuele Giuseppe Esposito int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 2243b491a90SEmanuele Giuseppe Esposito int64_t pos, int size); 2253b491a90SEmanuele Giuseppe Esposito 2263b491a90SEmanuele Giuseppe Esposito /* 2273b491a90SEmanuele Giuseppe Esposito * Returns the alignment in bytes that is required so that no bounce buffer 2283b491a90SEmanuele Giuseppe Esposito * is required throughout the stack 2293b491a90SEmanuele Giuseppe Esposito */ 2303b491a90SEmanuele Giuseppe Esposito size_t bdrv_min_mem_align(BlockDriverState *bs); 2313b491a90SEmanuele Giuseppe Esposito /* Returns optimal alignment in bytes for bounce buffer */ 2323b491a90SEmanuele Giuseppe Esposito size_t bdrv_opt_mem_align(BlockDriverState *bs); 2333b491a90SEmanuele Giuseppe Esposito void *qemu_blockalign(BlockDriverState *bs, size_t size); 2343b491a90SEmanuele Giuseppe Esposito void *qemu_blockalign0(BlockDriverState *bs, size_t size); 2353b491a90SEmanuele Giuseppe Esposito void *qemu_try_blockalign(BlockDriverState *bs, size_t size); 2363b491a90SEmanuele Giuseppe Esposito void *qemu_try_blockalign0(BlockDriverState *bs, size_t size); 2373b491a90SEmanuele Giuseppe Esposito 2383b491a90SEmanuele Giuseppe Esposito void bdrv_enable_copy_on_read(BlockDriverState *bs); 2393b491a90SEmanuele Giuseppe Esposito void bdrv_disable_copy_on_read(BlockDriverState *bs); 2403b491a90SEmanuele Giuseppe Esposito 241cb2bfaa4SEmanuele Giuseppe Esposito void coroutine_fn GRAPH_RDLOCK 242cb2bfaa4SEmanuele Giuseppe Esposito bdrv_co_debug_event(BlockDriverState *bs, BlkdebugEvent event); 243cb2bfaa4SEmanuele Giuseppe Esposito 244cb2bfaa4SEmanuele Giuseppe Esposito void co_wrapper_mixed_bdrv_rdlock 245cb2bfaa4SEmanuele Giuseppe Esposito bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event); 2463b491a90SEmanuele Giuseppe Esposito 24717362398SPaolo Bonzini #define BLKDBG_CO_EVENT(child, evt) \ 24817362398SPaolo Bonzini do { \ 24917362398SPaolo Bonzini if (child) { \ 25017362398SPaolo Bonzini bdrv_co_debug_event(child->bs, evt); \ 25117362398SPaolo Bonzini } \ 25217362398SPaolo Bonzini } while (0) 25317362398SPaolo Bonzini 2543b491a90SEmanuele Giuseppe Esposito #define BLKDBG_EVENT(child, evt) \ 2553b491a90SEmanuele Giuseppe Esposito do { \ 2563b491a90SEmanuele Giuseppe Esposito if (child) { \ 2573b491a90SEmanuele Giuseppe Esposito bdrv_debug_event(child->bs, evt); \ 2583b491a90SEmanuele Giuseppe Esposito } \ 2593b491a90SEmanuele Giuseppe Esposito } while (0) 2603b491a90SEmanuele Giuseppe Esposito 2613b491a90SEmanuele Giuseppe Esposito /** 2623b491a90SEmanuele Giuseppe Esposito * bdrv_get_aio_context: 2633b491a90SEmanuele Giuseppe Esposito * 2643b491a90SEmanuele Giuseppe Esposito * Returns: the currently bound #AioContext 2653b491a90SEmanuele Giuseppe Esposito */ 2663b491a90SEmanuele Giuseppe Esposito AioContext *bdrv_get_aio_context(BlockDriverState *bs); 2673b491a90SEmanuele Giuseppe Esposito 268d5f8d79cSHanna Reitz AioContext *bdrv_child_get_parent_aio_context(BdrvChild *c); 269d5f8d79cSHanna Reitz 2703b491a90SEmanuele Giuseppe Esposito /** 2713b491a90SEmanuele Giuseppe Esposito * Move the current coroutine to the AioContext of @bs and return the old 2723b491a90SEmanuele Giuseppe Esposito * AioContext of the coroutine. Increase bs->in_flight so that draining @bs 2733b491a90SEmanuele Giuseppe Esposito * will wait for the operation to proceed until the corresponding 2743b491a90SEmanuele Giuseppe Esposito * bdrv_co_leave(). 2753b491a90SEmanuele Giuseppe Esposito * 2763b491a90SEmanuele Giuseppe Esposito * Consequently, you can't call drain inside a bdrv_co_enter/leave() section as 2773b491a90SEmanuele Giuseppe Esposito * this will deadlock. 2783b491a90SEmanuele Giuseppe Esposito */ 2793b491a90SEmanuele Giuseppe Esposito AioContext *coroutine_fn bdrv_co_enter(BlockDriverState *bs); 2803b491a90SEmanuele Giuseppe Esposito 2813b491a90SEmanuele Giuseppe Esposito /** 2823b491a90SEmanuele Giuseppe Esposito * Ends a section started by bdrv_co_enter(). Move the current coroutine back 2833b491a90SEmanuele Giuseppe Esposito * to old_ctx and decrease bs->in_flight again. 2843b491a90SEmanuele Giuseppe Esposito */ 2853b491a90SEmanuele Giuseppe Esposito void coroutine_fn bdrv_co_leave(BlockDriverState *bs, AioContext *old_ctx); 2863b491a90SEmanuele Giuseppe Esposito 2873b491a90SEmanuele Giuseppe Esposito AioContext *child_of_bds_get_parent_aio_context(BdrvChild *c); 2883b491a90SEmanuele Giuseppe Esposito 289167f748dSKevin Wolf bool coroutine_fn GRAPH_RDLOCK 290167f748dSKevin Wolf bdrv_co_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name, 291167f748dSKevin Wolf uint32_t granularity, Error **errp); 292167f748dSKevin Wolf bool co_wrapper_bdrv_rdlock 293167f748dSKevin Wolf bdrv_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name, 294167f748dSKevin Wolf uint32_t granularity, Error **errp); 2953b491a90SEmanuele Giuseppe Esposito 2963b491a90SEmanuele Giuseppe Esposito /** 2973b491a90SEmanuele Giuseppe Esposito * 2983b491a90SEmanuele Giuseppe Esposito * bdrv_co_copy_range: 2993b491a90SEmanuele Giuseppe Esposito * 3003b491a90SEmanuele Giuseppe Esposito * Do offloaded copy between two children. If the operation is not implemented 3013b491a90SEmanuele Giuseppe Esposito * by the driver, or if the backend storage doesn't support it, a negative 3023b491a90SEmanuele Giuseppe Esposito * error code will be returned. 3033b491a90SEmanuele Giuseppe Esposito * 3043b491a90SEmanuele Giuseppe Esposito * Note: block layer doesn't emulate or fallback to a bounce buffer approach 3053b491a90SEmanuele Giuseppe Esposito * because usually the caller shouldn't attempt offloaded copy any more (e.g. 3063b491a90SEmanuele Giuseppe Esposito * calling copy_file_range(2)) after the first error, thus it should fall back 3073b491a90SEmanuele Giuseppe Esposito * to a read+write path in the caller level. 3083b491a90SEmanuele Giuseppe Esposito * 3093b491a90SEmanuele Giuseppe Esposito * @src: Source child to copy data from 3103b491a90SEmanuele Giuseppe Esposito * @src_offset: offset in @src image to read data 3113b491a90SEmanuele Giuseppe Esposito * @dst: Destination child to copy data to 3123b491a90SEmanuele Giuseppe Esposito * @dst_offset: offset in @dst image to write data 3133b491a90SEmanuele Giuseppe Esposito * @bytes: number of bytes to copy 3143b491a90SEmanuele Giuseppe Esposito * @flags: request flags. Supported flags: 3153b491a90SEmanuele Giuseppe Esposito * BDRV_REQ_ZERO_WRITE - treat the @src range as zero data and do zero 3163b491a90SEmanuele Giuseppe Esposito * write on @dst as if bdrv_co_pwrite_zeroes is 3173b491a90SEmanuele Giuseppe Esposito * called. Used to simplify caller code, or 3183b491a90SEmanuele Giuseppe Esposito * during BlockDriver.bdrv_co_copy_range_from() 3193b491a90SEmanuele Giuseppe Esposito * recursion. 3203b491a90SEmanuele Giuseppe Esposito * BDRV_REQ_NO_SERIALISING - do not serialize with other overlapping 3213b491a90SEmanuele Giuseppe Esposito * requests currently in flight. 3223b491a90SEmanuele Giuseppe Esposito * 3233b491a90SEmanuele Giuseppe Esposito * Returns: 0 if succeeded; negative error code if failed. 3243b491a90SEmanuele Giuseppe Esposito **/ 325742bf09bSEmanuele Giuseppe Esposito int coroutine_fn GRAPH_RDLOCK 326742bf09bSEmanuele Giuseppe Esposito bdrv_co_copy_range(BdrvChild *src, int64_t src_offset, 3273b491a90SEmanuele Giuseppe Esposito BdrvChild *dst, int64_t dst_offset, 3283b491a90SEmanuele Giuseppe Esposito int64_t bytes, BdrvRequestFlags read_flags, 3293b491a90SEmanuele Giuseppe Esposito BdrvRequestFlags write_flags); 3303b491a90SEmanuele Giuseppe Esposito 3313b491a90SEmanuele Giuseppe Esposito /* 3323b491a90SEmanuele Giuseppe Esposito * "I/O or GS" API functions. These functions can run without 3333b491a90SEmanuele Giuseppe Esposito * the BQL, but only in one specific iothread/main loop. 3343b491a90SEmanuele Giuseppe Esposito * 335*23c983c8SStefan Hajnoczi * More specifically, these functions use BDRV_POLL_WHILE(bs), which requires 336*23c983c8SStefan Hajnoczi * the caller to be either in the main thread or directly in the home thread 337*23c983c8SStefan Hajnoczi * that runs the bs AioContext. Calling them from another thread in another 338*23c983c8SStefan Hajnoczi * AioContext would cause deadlocks. 3393b491a90SEmanuele Giuseppe Esposito * 3403b491a90SEmanuele Giuseppe Esposito * Therefore, these functions are not proper I/O, because they 3413b491a90SEmanuele Giuseppe Esposito * can't run in *any* iothreads, but only in a specific one. 3423b491a90SEmanuele Giuseppe Esposito * 3433b491a90SEmanuele Giuseppe Esposito * These functions can call any function from I/O, Common and this 3443b491a90SEmanuele Giuseppe Esposito * categories, but must be invoked only by other "I/O or GS" and GS APIs. 3453b491a90SEmanuele Giuseppe Esposito * 3463b491a90SEmanuele Giuseppe Esposito * All functions in this category must use the macro 3473b491a90SEmanuele Giuseppe Esposito * IO_OR_GS_CODE(); 3483b491a90SEmanuele Giuseppe Esposito * to catch when they are accidentally called by the wrong API. 3493b491a90SEmanuele Giuseppe Esposito */ 3503b491a90SEmanuele Giuseppe Esposito 3513b491a90SEmanuele Giuseppe Esposito #define BDRV_POLL_WHILE(bs, cond) ({ \ 3523b491a90SEmanuele Giuseppe Esposito BlockDriverState *bs_ = (bs); \ 353384a48fbSEmanuele Giuseppe Esposito IO_OR_GS_CODE(); \ 3543b491a90SEmanuele Giuseppe Esposito AIO_WAIT_WHILE(bdrv_get_aio_context(bs_), \ 3553b491a90SEmanuele Giuseppe Esposito cond); }) 3563b491a90SEmanuele Giuseppe Esposito 3573b491a90SEmanuele Giuseppe Esposito void bdrv_drain(BlockDriverState *bs); 3583b491a90SEmanuele Giuseppe Esposito 35990830f59SEmanuele Giuseppe Esposito int co_wrapper_mixed_bdrv_rdlock 3603b491a90SEmanuele Giuseppe Esposito bdrv_truncate(BdrvChild *child, int64_t offset, bool exact, 3613b491a90SEmanuele Giuseppe Esposito PreallocMode prealloc, BdrvRequestFlags flags, Error **errp); 3623b491a90SEmanuele Giuseppe Esposito 36390830f59SEmanuele Giuseppe Esposito int co_wrapper_mixed_bdrv_rdlock 36490830f59SEmanuele Giuseppe Esposito bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix); 3653b491a90SEmanuele Giuseppe Esposito 3663b491a90SEmanuele Giuseppe Esposito /* Invalidate any cached metadata used by image formats */ 36790830f59SEmanuele Giuseppe Esposito int co_wrapper_mixed_bdrv_rdlock 36890830f59SEmanuele Giuseppe Esposito bdrv_invalidate_cache(BlockDriverState *bs, Error **errp); 36990830f59SEmanuele Giuseppe Esposito 37090830f59SEmanuele Giuseppe Esposito int co_wrapper_mixed_bdrv_rdlock bdrv_flush(BlockDriverState *bs); 37190830f59SEmanuele Giuseppe Esposito 37290830f59SEmanuele Giuseppe Esposito int co_wrapper_mixed_bdrv_rdlock 37390830f59SEmanuele Giuseppe Esposito bdrv_pdiscard(BdrvChild *child, int64_t offset, int64_t bytes); 37490830f59SEmanuele Giuseppe Esposito 37590830f59SEmanuele Giuseppe Esposito int co_wrapper_mixed_bdrv_rdlock 3763b491a90SEmanuele Giuseppe Esposito bdrv_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos); 37790830f59SEmanuele Giuseppe Esposito 37890830f59SEmanuele Giuseppe Esposito int co_wrapper_mixed_bdrv_rdlock 3793b491a90SEmanuele Giuseppe Esposito bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos); 3803b491a90SEmanuele Giuseppe Esposito 3813b491a90SEmanuele Giuseppe Esposito /** 3823b491a90SEmanuele Giuseppe Esposito * bdrv_parent_drained_begin_single: 3833b491a90SEmanuele Giuseppe Esposito * 384606ed756SKevin Wolf * Begin a quiesced section for the parent of @c. 3853b491a90SEmanuele Giuseppe Esposito */ 386d05ab380SEmanuele Giuseppe Esposito void GRAPH_RDLOCK bdrv_parent_drained_begin_single(BdrvChild *c); 3873b491a90SEmanuele Giuseppe Esposito 3883b491a90SEmanuele Giuseppe Esposito /** 38923987471SKevin Wolf * bdrv_parent_drained_poll_single: 39023987471SKevin Wolf * 39123987471SKevin Wolf * Returns true if there is any pending activity to cease before @c can be 39223987471SKevin Wolf * called quiesced, false otherwise. 39323987471SKevin Wolf */ 394d05ab380SEmanuele Giuseppe Esposito bool GRAPH_RDLOCK bdrv_parent_drained_poll_single(BdrvChild *c); 39523987471SKevin Wolf 39623987471SKevin Wolf /** 3973b491a90SEmanuele Giuseppe Esposito * bdrv_parent_drained_end_single: 3983b491a90SEmanuele Giuseppe Esposito * 3993b491a90SEmanuele Giuseppe Esposito * End a quiesced section for the parent of @c. 4003b491a90SEmanuele Giuseppe Esposito */ 401d05ab380SEmanuele Giuseppe Esposito void GRAPH_RDLOCK bdrv_parent_drained_end_single(BdrvChild *c); 4023b491a90SEmanuele Giuseppe Esposito 4033b491a90SEmanuele Giuseppe Esposito /** 4043b491a90SEmanuele Giuseppe Esposito * bdrv_drain_poll: 4053b491a90SEmanuele Giuseppe Esposito * 406299403aeSKevin Wolf * Poll for pending requests in @bs and its parents (except for @ignore_parent). 4073b491a90SEmanuele Giuseppe Esposito * 4083b491a90SEmanuele Giuseppe Esposito * If @ignore_bds_parents is true, parents that are BlockDriverStates must 4093b491a90SEmanuele Giuseppe Esposito * ignore the drain request because they will be drained separately (used for 4103b491a90SEmanuele Giuseppe Esposito * drain_all). 4113b491a90SEmanuele Giuseppe Esposito * 4123b491a90SEmanuele Giuseppe Esposito * This is part of bdrv_drained_begin. 4133b491a90SEmanuele Giuseppe Esposito */ 414d05ab380SEmanuele Giuseppe Esposito bool GRAPH_RDLOCK 415d05ab380SEmanuele Giuseppe Esposito bdrv_drain_poll(BlockDriverState *bs, BdrvChild *ignore_parent, 416299403aeSKevin Wolf bool ignore_bds_parents); 4173b491a90SEmanuele Giuseppe Esposito 4183b491a90SEmanuele Giuseppe Esposito /** 4193b491a90SEmanuele Giuseppe Esposito * bdrv_drained_begin: 4203b491a90SEmanuele Giuseppe Esposito * 4213b491a90SEmanuele Giuseppe Esposito * Begin a quiesced section for exclusive access to the BDS, by disabling 4223b491a90SEmanuele Giuseppe Esposito * external request sources including NBD server, block jobs, and device model. 4233b491a90SEmanuele Giuseppe Esposito * 424d05ab380SEmanuele Giuseppe Esposito * This function can only be invoked by the main loop or a coroutine 425d05ab380SEmanuele Giuseppe Esposito * (regardless of the AioContext where it is running). 426d05ab380SEmanuele Giuseppe Esposito * If the coroutine is running in an Iothread AioContext, this function will 427d05ab380SEmanuele Giuseppe Esposito * just schedule a BH to run in the main loop. 428d05ab380SEmanuele Giuseppe Esposito * However, it cannot be directly called by an Iothread. 429d05ab380SEmanuele Giuseppe Esposito * 4303b491a90SEmanuele Giuseppe Esposito * This function can be recursive. 4313b491a90SEmanuele Giuseppe Esposito */ 4323b491a90SEmanuele Giuseppe Esposito void bdrv_drained_begin(BlockDriverState *bs); 4333b491a90SEmanuele Giuseppe Esposito 4343b491a90SEmanuele Giuseppe Esposito /** 4353b491a90SEmanuele Giuseppe Esposito * bdrv_do_drained_begin_quiesce: 4363b491a90SEmanuele Giuseppe Esposito * 4373b491a90SEmanuele Giuseppe Esposito * Quiesces a BDS like bdrv_drained_begin(), but does not wait for already 4383b491a90SEmanuele Giuseppe Esposito * running requests to complete. 4393b491a90SEmanuele Giuseppe Esposito */ 440a82a3bd1SKevin Wolf void bdrv_do_drained_begin_quiesce(BlockDriverState *bs, BdrvChild *parent); 4413b491a90SEmanuele Giuseppe Esposito 4423b491a90SEmanuele Giuseppe Esposito /** 4433b491a90SEmanuele Giuseppe Esposito * bdrv_drained_end: 4443b491a90SEmanuele Giuseppe Esposito * 4453b491a90SEmanuele Giuseppe Esposito * End a quiescent section started by bdrv_drained_begin(). 446d05ab380SEmanuele Giuseppe Esposito * 447d05ab380SEmanuele Giuseppe Esposito * This function can only be invoked by the main loop or a coroutine 448d05ab380SEmanuele Giuseppe Esposito * (regardless of the AioContext where it is running). 449d05ab380SEmanuele Giuseppe Esposito * If the coroutine is running in an Iothread AioContext, this function will 450d05ab380SEmanuele Giuseppe Esposito * just schedule a BH to run in the main loop. 451d05ab380SEmanuele Giuseppe Esposito * However, it cannot be directly called by an Iothread. 4523b491a90SEmanuele Giuseppe Esposito */ 4533b491a90SEmanuele Giuseppe Esposito void bdrv_drained_end(BlockDriverState *bs); 4543b491a90SEmanuele Giuseppe Esposito 4553b491a90SEmanuele Giuseppe Esposito #endif /* BLOCK_IO_H */ 456