1a2c4c3b1SEmanuele Giuseppe Esposito /*
2a2c4c3b1SEmanuele Giuseppe Esposito  * QEMU Block backends
3a2c4c3b1SEmanuele Giuseppe Esposito  *
4a2c4c3b1SEmanuele Giuseppe Esposito  * Copyright (C) 2014-2016 Red Hat, Inc.
5a2c4c3b1SEmanuele Giuseppe Esposito  *
6a2c4c3b1SEmanuele Giuseppe Esposito  * Authors:
7a2c4c3b1SEmanuele Giuseppe Esposito  *  Markus Armbruster <armbru@redhat.com>,
8a2c4c3b1SEmanuele Giuseppe Esposito  *
9a2c4c3b1SEmanuele Giuseppe Esposito  * This work is licensed under the terms of the GNU LGPL, version 2.1
10a2c4c3b1SEmanuele Giuseppe Esposito  * or later.  See the COPYING.LIB file in the top-level directory.
11a2c4c3b1SEmanuele Giuseppe Esposito  */
12a2c4c3b1SEmanuele Giuseppe Esposito 
13a2c4c3b1SEmanuele Giuseppe Esposito #ifndef BLOCK_BACKEND_IO_H
14a2c4c3b1SEmanuele Giuseppe Esposito #define BLOCK_BACKEND_IO_H
15a2c4c3b1SEmanuele Giuseppe Esposito 
16a2c4c3b1SEmanuele Giuseppe Esposito #include "block-backend-common.h"
17a2c4c3b1SEmanuele Giuseppe Esposito 
18a2c4c3b1SEmanuele Giuseppe Esposito /*
19a2c4c3b1SEmanuele Giuseppe Esposito  * I/O API functions. These functions are thread-safe.
20a2c4c3b1SEmanuele Giuseppe Esposito  *
21a2c4c3b1SEmanuele Giuseppe Esposito  * See include/block/block-io.h for more information about
22a2c4c3b1SEmanuele Giuseppe Esposito  * the I/O API.
23a2c4c3b1SEmanuele Giuseppe Esposito  */
24a2c4c3b1SEmanuele Giuseppe Esposito 
25a2c4c3b1SEmanuele Giuseppe Esposito const char *blk_name(const BlockBackend *blk);
26a2c4c3b1SEmanuele Giuseppe Esposito 
27a2c4c3b1SEmanuele Giuseppe Esposito BlockDriverState *blk_bs(BlockBackend *blk);
28a2c4c3b1SEmanuele Giuseppe Esposito 
29a2c4c3b1SEmanuele Giuseppe Esposito void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow);
30a2c4c3b1SEmanuele Giuseppe Esposito void blk_set_allow_aio_context_change(BlockBackend *blk, bool allow);
31a2c4c3b1SEmanuele Giuseppe Esposito void blk_set_disable_request_queuing(BlockBackend *blk, bool disable);
32a2c4c3b1SEmanuele Giuseppe Esposito bool blk_iostatus_is_enabled(const BlockBackend *blk);
33a2c4c3b1SEmanuele Giuseppe Esposito 
34a2c4c3b1SEmanuele Giuseppe Esposito char *blk_get_attached_dev_id(BlockBackend *blk);
35a2c4c3b1SEmanuele Giuseppe Esposito 
36a2c4c3b1SEmanuele Giuseppe Esposito BlockAIOCB *blk_aio_pwrite_zeroes(BlockBackend *blk, int64_t offset,
37a2c4c3b1SEmanuele Giuseppe Esposito                                   int64_t bytes, BdrvRequestFlags flags,
38a2c4c3b1SEmanuele Giuseppe Esposito                                   BlockCompletionFunc *cb, void *opaque);
39a2c4c3b1SEmanuele Giuseppe Esposito 
40a2c4c3b1SEmanuele Giuseppe Esposito BlockAIOCB *blk_aio_preadv(BlockBackend *blk, int64_t offset,
41a2c4c3b1SEmanuele Giuseppe Esposito                            QEMUIOVector *qiov, BdrvRequestFlags flags,
42a2c4c3b1SEmanuele Giuseppe Esposito                            BlockCompletionFunc *cb, void *opaque);
43a2c4c3b1SEmanuele Giuseppe Esposito BlockAIOCB *blk_aio_pwritev(BlockBackend *blk, int64_t offset,
44a2c4c3b1SEmanuele Giuseppe Esposito                             QEMUIOVector *qiov, BdrvRequestFlags flags,
45a2c4c3b1SEmanuele Giuseppe Esposito                             BlockCompletionFunc *cb, void *opaque);
46a2c4c3b1SEmanuele Giuseppe Esposito BlockAIOCB *blk_aio_flush(BlockBackend *blk,
47a2c4c3b1SEmanuele Giuseppe Esposito                           BlockCompletionFunc *cb, void *opaque);
48a2c4c3b1SEmanuele Giuseppe Esposito BlockAIOCB *blk_aio_pdiscard(BlockBackend *blk, int64_t offset, int64_t bytes,
49a2c4c3b1SEmanuele Giuseppe Esposito                              BlockCompletionFunc *cb, void *opaque);
50a2c4c3b1SEmanuele Giuseppe Esposito void blk_aio_cancel_async(BlockAIOCB *acb);
51a2c4c3b1SEmanuele Giuseppe Esposito BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
52a2c4c3b1SEmanuele Giuseppe Esposito                           BlockCompletionFunc *cb, void *opaque);
53a2c4c3b1SEmanuele Giuseppe Esposito 
54a2c4c3b1SEmanuele Giuseppe Esposito void blk_inc_in_flight(BlockBackend *blk);
55a2c4c3b1SEmanuele Giuseppe Esposito void blk_dec_in_flight(BlockBackend *blk);
56a2c4c3b1SEmanuele Giuseppe Esposito bool blk_is_inserted(BlockBackend *blk);
57a2c4c3b1SEmanuele Giuseppe Esposito bool blk_is_available(BlockBackend *blk);
58a2c4c3b1SEmanuele Giuseppe Esposito void blk_lock_medium(BlockBackend *blk, bool locked);
59a2c4c3b1SEmanuele Giuseppe Esposito void blk_eject(BlockBackend *blk, bool eject_flag);
60a2c4c3b1SEmanuele Giuseppe Esposito int64_t blk_getlength(BlockBackend *blk);
61a2c4c3b1SEmanuele Giuseppe Esposito void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr);
62a2c4c3b1SEmanuele Giuseppe Esposito int64_t blk_nb_sectors(BlockBackend *blk);
63a2c4c3b1SEmanuele Giuseppe Esposito void *blk_try_blockalign(BlockBackend *blk, size_t size);
64a2c4c3b1SEmanuele Giuseppe Esposito void *blk_blockalign(BlockBackend *blk, size_t size);
65a2c4c3b1SEmanuele Giuseppe Esposito bool blk_is_writable(BlockBackend *blk);
66a2c4c3b1SEmanuele Giuseppe Esposito bool blk_enable_write_cache(BlockBackend *blk);
67a2c4c3b1SEmanuele Giuseppe Esposito BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read);
68a2c4c3b1SEmanuele Giuseppe Esposito BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
69a2c4c3b1SEmanuele Giuseppe Esposito                                       int error);
70a2c4c3b1SEmanuele Giuseppe Esposito void blk_error_action(BlockBackend *blk, BlockErrorAction action,
71a2c4c3b1SEmanuele Giuseppe Esposito                       bool is_read, int error);
72a2c4c3b1SEmanuele Giuseppe Esposito void blk_iostatus_set_err(BlockBackend *blk, int error);
73a2c4c3b1SEmanuele Giuseppe Esposito int blk_get_max_iov(BlockBackend *blk);
74a2c4c3b1SEmanuele Giuseppe Esposito int blk_get_max_hw_iov(BlockBackend *blk);
75a2c4c3b1SEmanuele Giuseppe Esposito 
76a2c4c3b1SEmanuele Giuseppe Esposito void blk_io_plug(BlockBackend *blk);
77a2c4c3b1SEmanuele Giuseppe Esposito void blk_io_unplug(BlockBackend *blk);
78a2c4c3b1SEmanuele Giuseppe Esposito AioContext *blk_get_aio_context(BlockBackend *blk);
79a2c4c3b1SEmanuele Giuseppe Esposito BlockAcctStats *blk_get_stats(BlockBackend *blk);
80a2c4c3b1SEmanuele Giuseppe Esposito void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
81a2c4c3b1SEmanuele Giuseppe Esposito                   BlockCompletionFunc *cb, void *opaque);
82a2c4c3b1SEmanuele Giuseppe Esposito BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
83a2c4c3b1SEmanuele Giuseppe Esposito                                   BlockCompletionFunc *cb,
84a2c4c3b1SEmanuele Giuseppe Esposito                                   void *opaque, int ret);
85a2c4c3b1SEmanuele Giuseppe Esposito 
86a2c4c3b1SEmanuele Giuseppe Esposito uint32_t blk_get_request_alignment(BlockBackend *blk);
87a2c4c3b1SEmanuele Giuseppe Esposito uint32_t blk_get_max_transfer(BlockBackend *blk);
88a2c4c3b1SEmanuele Giuseppe Esposito uint64_t blk_get_max_hw_transfer(BlockBackend *blk);
89a2c4c3b1SEmanuele Giuseppe Esposito 
90a2c4c3b1SEmanuele Giuseppe Esposito int coroutine_fn blk_co_copy_range(BlockBackend *blk_in, int64_t off_in,
91a2c4c3b1SEmanuele Giuseppe Esposito                                    BlockBackend *blk_out, int64_t off_out,
92a2c4c3b1SEmanuele Giuseppe Esposito                                    int64_t bytes, BdrvRequestFlags read_flags,
93a2c4c3b1SEmanuele Giuseppe Esposito                                    BdrvRequestFlags write_flags);
94a2c4c3b1SEmanuele Giuseppe Esposito 
95a2c4c3b1SEmanuele Giuseppe Esposito 
96a2c4c3b1SEmanuele Giuseppe Esposito /*
97a2c4c3b1SEmanuele Giuseppe Esposito  * "I/O or GS" API functions. These functions can run without
98a2c4c3b1SEmanuele Giuseppe Esposito  * the BQL, but only in one specific iothread/main loop.
99a2c4c3b1SEmanuele Giuseppe Esposito  *
100a2c4c3b1SEmanuele Giuseppe Esposito  * See include/block/block-io.h for more information about
101a2c4c3b1SEmanuele Giuseppe Esposito  * the "I/O or GS" API.
102a2c4c3b1SEmanuele Giuseppe Esposito  */
103a2c4c3b1SEmanuele Giuseppe Esposito 
104facbaad9SAlberto Faria int generated_co_wrapper blk_pread(BlockBackend *blk, int64_t offset,
105facbaad9SAlberto Faria                                    int64_t bytes, void *buf,
1063b35d454SAlberto Faria                                    BdrvRequestFlags flags);
107facbaad9SAlberto Faria int generated_co_wrapper blk_pwrite(BlockBackend *blk, int64_t offset,
108facbaad9SAlberto Faria                                     int64_t bytes, const void *buf,
109facbaad9SAlberto Faria                                     BdrvRequestFlags flags);
110d1d3fc3dSAlberto Faria int generated_co_wrapper blk_preadv_part(BlockBackend *blk, int64_t offset,
111d1d3fc3dSAlberto Faria                                          int64_t bytes, QEMUIOVector *qiov,
112d1d3fc3dSAlberto Faria                                          size_t qiov_offset,
113d1d3fc3dSAlberto Faria                                          BdrvRequestFlags flags);
114d1d3fc3dSAlberto Faria int coroutine_fn blk_co_preadv_part(BlockBackend *blk, int64_t offset,
115d1d3fc3dSAlberto Faria                                     int64_t bytes, QEMUIOVector *qiov,
116d1d3fc3dSAlberto Faria                                     size_t qiov_offset, BdrvRequestFlags flags);
1177c8cd723SAlberto Faria int generated_co_wrapper blk_preadv(BlockBackend *blk, int64_t offset,
1187c8cd723SAlberto Faria                                     int64_t bytes, QEMUIOVector *qiov,
1197c8cd723SAlberto Faria                                     BdrvRequestFlags flags);
120a2c4c3b1SEmanuele Giuseppe Esposito int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset,
121a2c4c3b1SEmanuele Giuseppe Esposito                                int64_t bytes, QEMUIOVector *qiov,
122a2c4c3b1SEmanuele Giuseppe Esposito                                BdrvRequestFlags flags);
12309cca043SAlberto Faria int generated_co_wrapper blk_pwritev_part(BlockBackend *blk, int64_t offset,
12409cca043SAlberto Faria                                           int64_t bytes, QEMUIOVector *qiov,
12509cca043SAlberto Faria                                           size_t qiov_offset,
12609cca043SAlberto Faria                                           BdrvRequestFlags flags);
127a2c4c3b1SEmanuele Giuseppe Esposito int coroutine_fn blk_co_pwritev_part(BlockBackend *blk, int64_t offset,
128a2c4c3b1SEmanuele Giuseppe Esposito                                      int64_t bytes,
129a2c4c3b1SEmanuele Giuseppe Esposito                                      QEMUIOVector *qiov, size_t qiov_offset,
130a2c4c3b1SEmanuele Giuseppe Esposito                                      BdrvRequestFlags flags);
1317c8cd723SAlberto Faria int generated_co_wrapper blk_pwritev(BlockBackend *blk, int64_t offset,
1327c8cd723SAlberto Faria                                      int64_t bytes, QEMUIOVector *qiov,
1337c8cd723SAlberto Faria                                      BdrvRequestFlags flags);
134a2c4c3b1SEmanuele Giuseppe Esposito int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset,
135a2c4c3b1SEmanuele Giuseppe Esposito                                 int64_t bytes, QEMUIOVector *qiov,
136a2c4c3b1SEmanuele Giuseppe Esposito                                 BdrvRequestFlags flags);
137a2c4c3b1SEmanuele Giuseppe Esposito 
138a2c4c3b1SEmanuele Giuseppe Esposito static inline int coroutine_fn blk_co_pread(BlockBackend *blk, int64_t offset,
139a2c4c3b1SEmanuele Giuseppe Esposito                                             int64_t bytes, void *buf,
140a2c4c3b1SEmanuele Giuseppe Esposito                                             BdrvRequestFlags flags)
141a2c4c3b1SEmanuele Giuseppe Esposito {
142a2c4c3b1SEmanuele Giuseppe Esposito     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
14337868b2aSEmanuele Giuseppe Esposito     IO_OR_GS_CODE();
144a2c4c3b1SEmanuele Giuseppe Esposito 
145a2c4c3b1SEmanuele Giuseppe Esposito     assert(bytes <= SIZE_MAX);
146a2c4c3b1SEmanuele Giuseppe Esposito 
147a2c4c3b1SEmanuele Giuseppe Esposito     return blk_co_preadv(blk, offset, bytes, &qiov, flags);
148a2c4c3b1SEmanuele Giuseppe Esposito }
149a2c4c3b1SEmanuele Giuseppe Esposito 
150a2c4c3b1SEmanuele Giuseppe Esposito static inline int coroutine_fn blk_co_pwrite(BlockBackend *blk, int64_t offset,
1517d252ba5SAlberto Faria                                              int64_t bytes, const void *buf,
152a2c4c3b1SEmanuele Giuseppe Esposito                                              BdrvRequestFlags flags)
153a2c4c3b1SEmanuele Giuseppe Esposito {
154a2c4c3b1SEmanuele Giuseppe Esposito     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
15537868b2aSEmanuele Giuseppe Esposito     IO_OR_GS_CODE();
156a2c4c3b1SEmanuele Giuseppe Esposito 
157a2c4c3b1SEmanuele Giuseppe Esposito     assert(bytes <= SIZE_MAX);
158a2c4c3b1SEmanuele Giuseppe Esposito 
159a2c4c3b1SEmanuele Giuseppe Esposito     return blk_co_pwritev(blk, offset, bytes, &qiov, flags);
160a2c4c3b1SEmanuele Giuseppe Esposito }
161a2c4c3b1SEmanuele Giuseppe Esposito 
162*50db162dSAlberto Faria int generated_co_wrapper blk_pdiscard(BlockBackend *blk, int64_t offset,
163*50db162dSAlberto Faria                                       int64_t bytes);
164a2c4c3b1SEmanuele Giuseppe Esposito int coroutine_fn blk_co_pdiscard(BlockBackend *blk, int64_t offset,
165a2c4c3b1SEmanuele Giuseppe Esposito                                  int64_t bytes);
166a2c4c3b1SEmanuele Giuseppe Esposito 
167a2c4c3b1SEmanuele Giuseppe Esposito int coroutine_fn blk_co_flush(BlockBackend *blk);
168a2c4c3b1SEmanuele Giuseppe Esposito int blk_flush(BlockBackend *blk);
169a2c4c3b1SEmanuele Giuseppe Esposito 
170a2c4c3b1SEmanuele Giuseppe Esposito int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf);
171a2c4c3b1SEmanuele Giuseppe Esposito 
1722c9715faSAlberto Faria int generated_co_wrapper blk_pwrite_compressed(BlockBackend *blk,
1732c9715faSAlberto Faria                                                int64_t offset, int64_t bytes,
1740cadf2c8SAlberto Faria                                                const void *buf);
1752c9715faSAlberto Faria int coroutine_fn blk_co_pwrite_compressed(BlockBackend *blk, int64_t offset,
1762c9715faSAlberto Faria                                           int64_t bytes, const void *buf);
1771c95dc91SAlberto Faria int generated_co_wrapper blk_pwrite_zeroes(BlockBackend *blk, int64_t offset,
1781c95dc91SAlberto Faria                                            int64_t bytes,
1791c95dc91SAlberto Faria                                            BdrvRequestFlags flags);
180a2c4c3b1SEmanuele Giuseppe Esposito int coroutine_fn blk_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
181a2c4c3b1SEmanuele Giuseppe Esposito                                       int64_t bytes, BdrvRequestFlags flags);
182a2c4c3b1SEmanuele Giuseppe Esposito int blk_truncate(BlockBackend *blk, int64_t offset, bool exact,
183a2c4c3b1SEmanuele Giuseppe Esposito                  PreallocMode prealloc, BdrvRequestFlags flags, Error **errp);
184a2c4c3b1SEmanuele Giuseppe Esposito 
185a2c4c3b1SEmanuele Giuseppe Esposito #endif /* BLOCK_BACKEND_IO_H */
186