1 /*
2  * QEMU Block backends
3  *
4  * Copyright (C) 2014-2016 Red Hat, Inc.
5  *
6  * Authors:
7  *  Markus Armbruster <armbru@redhat.com>,
8  *
9  * This work is licensed under the terms of the GNU LGPL, version 2.1
10  * or later.  See the COPYING.LIB file in the top-level directory.
11  */
12 
13 #ifndef BLOCK_BACKEND_IO_H
14 #define BLOCK_BACKEND_IO_H
15 
16 #include "block-backend-common.h"
17 #include "block/accounting.h"
18 
19 /*
20  * I/O API functions. These functions are thread-safe.
21  *
22  * See include/block/block-io.h for more information about
23  * the I/O API.
24  */
25 
26 const char *blk_name(const BlockBackend *blk);
27 
28 BlockDriverState *blk_bs(BlockBackend *blk);
29 
30 void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow);
31 void blk_set_allow_aio_context_change(BlockBackend *blk, bool allow);
32 void blk_set_disable_request_queuing(BlockBackend *blk, bool disable);
33 bool blk_iostatus_is_enabled(const BlockBackend *blk);
34 
35 char *blk_get_attached_dev_id(BlockBackend *blk);
36 
37 BlockAIOCB *blk_aio_pwrite_zeroes(BlockBackend *blk, int64_t offset,
38                                   int64_t bytes, BdrvRequestFlags flags,
39                                   BlockCompletionFunc *cb, void *opaque);
40 
41 BlockAIOCB *blk_aio_preadv(BlockBackend *blk, int64_t offset,
42                            QEMUIOVector *qiov, BdrvRequestFlags flags,
43                            BlockCompletionFunc *cb, void *opaque);
44 BlockAIOCB *blk_aio_pwritev(BlockBackend *blk, int64_t offset,
45                             QEMUIOVector *qiov, BdrvRequestFlags flags,
46                             BlockCompletionFunc *cb, void *opaque);
47 BlockAIOCB *blk_aio_flush(BlockBackend *blk,
48                           BlockCompletionFunc *cb, void *opaque);
49 BlockAIOCB *blk_aio_zone_report(BlockBackend *blk, int64_t offset,
50                                 unsigned int *nr_zones,
51                                 BlockZoneDescriptor *zones,
52                                 BlockCompletionFunc *cb, void *opaque);
53 BlockAIOCB *blk_aio_zone_mgmt(BlockBackend *blk, BlockZoneOp op,
54                               int64_t offset, int64_t len,
55                               BlockCompletionFunc *cb, void *opaque);
56 BlockAIOCB *blk_aio_zone_append(BlockBackend *blk, int64_t *offset,
57                                 QEMUIOVector *qiov, BdrvRequestFlags flags,
58                                 BlockCompletionFunc *cb, void *opaque);
59 BlockAIOCB *blk_aio_pdiscard(BlockBackend *blk, int64_t offset, int64_t bytes,
60                              BlockCompletionFunc *cb, void *opaque);
61 void blk_aio_cancel_async(BlockAIOCB *acb);
62 BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
63                           BlockCompletionFunc *cb, void *opaque);
64 
65 void blk_inc_in_flight(BlockBackend *blk);
66 void blk_dec_in_flight(BlockBackend *blk);
67 
68 bool coroutine_fn GRAPH_RDLOCK blk_co_is_inserted(BlockBackend *blk);
69 bool co_wrapper_mixed_bdrv_rdlock blk_is_inserted(BlockBackend *blk);
70 
71 bool coroutine_fn GRAPH_RDLOCK blk_co_is_available(BlockBackend *blk);
72 bool co_wrapper_mixed_bdrv_rdlock blk_is_available(BlockBackend *blk);
73 
74 void coroutine_fn blk_co_lock_medium(BlockBackend *blk, bool locked);
75 void co_wrapper blk_lock_medium(BlockBackend *blk, bool locked);
76 
77 void coroutine_fn blk_co_eject(BlockBackend *blk, bool eject_flag);
78 void co_wrapper blk_eject(BlockBackend *blk, bool eject_flag);
79 
80 int64_t coroutine_fn blk_co_getlength(BlockBackend *blk);
81 int64_t co_wrapper_mixed blk_getlength(BlockBackend *blk);
82 
83 void coroutine_fn blk_co_get_geometry(BlockBackend *blk,
84                                       uint64_t *nb_sectors_ptr);
85 void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr);
86 
87 int64_t coroutine_fn blk_co_nb_sectors(BlockBackend *blk);
88 int64_t blk_nb_sectors(BlockBackend *blk);
89 
90 void *blk_try_blockalign(BlockBackend *blk, size_t size);
91 void *blk_blockalign(BlockBackend *blk, size_t size);
92 bool blk_is_writable(BlockBackend *blk);
93 bool blk_enable_write_cache(BlockBackend *blk);
94 BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read);
95 BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
96                                       int error);
97 void blk_error_action(BlockBackend *blk, BlockErrorAction action,
98                       bool is_read, int error);
99 void blk_iostatus_set_err(BlockBackend *blk, int error);
100 int blk_get_max_iov(BlockBackend *blk);
101 int blk_get_max_hw_iov(BlockBackend *blk);
102 
103 void blk_io_plug(void);
104 void blk_io_unplug(void);
105 void blk_io_plug_call(void (*fn)(void *), void *opaque);
106 
107 AioContext *blk_get_aio_context(BlockBackend *blk);
108 BlockAcctStats *blk_get_stats(BlockBackend *blk);
109 void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
110                   BlockCompletionFunc *cb, void *opaque);
111 BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
112                                   BlockCompletionFunc *cb,
113                                   void *opaque, int ret);
114 
115 uint32_t blk_get_request_alignment(BlockBackend *blk);
116 uint32_t blk_get_max_transfer(BlockBackend *blk);
117 uint64_t blk_get_max_hw_transfer(BlockBackend *blk);
118 
119 int coroutine_fn blk_co_copy_range(BlockBackend *blk_in, int64_t off_in,
120                                    BlockBackend *blk_out, int64_t off_out,
121                                    int64_t bytes, BdrvRequestFlags read_flags,
122                                    BdrvRequestFlags write_flags);
123 
124 int coroutine_fn blk_co_block_status_above(BlockBackend *blk,
125                                            BlockDriverState *base,
126                                            int64_t offset, int64_t bytes,
127                                            int64_t *pnum, int64_t *map,
128                                            BlockDriverState **file);
129 int coroutine_fn blk_co_is_allocated_above(BlockBackend *blk,
130                                            BlockDriverState *base,
131                                            bool include_base, int64_t offset,
132                                            int64_t bytes, int64_t *pnum);
133 
134 /*
135  * "I/O or GS" API functions. These functions can run without
136  * the BQL, but only in one specific iothread/main loop.
137  *
138  * See include/block/block-io.h for more information about
139  * the "I/O or GS" API.
140  */
141 
142 int co_wrapper_mixed blk_pread(BlockBackend *blk, int64_t offset,
143                                int64_t bytes, void *buf,
144                                BdrvRequestFlags flags);
145 int coroutine_fn blk_co_pread(BlockBackend *blk, int64_t offset, int64_t bytes,
146                               void *buf, BdrvRequestFlags flags);
147 
148 int co_wrapper_mixed blk_preadv(BlockBackend *blk, int64_t offset,
149                                 int64_t bytes, QEMUIOVector *qiov,
150                                 BdrvRequestFlags flags);
151 int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset,
152                                int64_t bytes, QEMUIOVector *qiov,
153                                BdrvRequestFlags flags);
154 
155 int co_wrapper_mixed blk_preadv_part(BlockBackend *blk, int64_t offset,
156                                      int64_t bytes, QEMUIOVector *qiov,
157                                      size_t qiov_offset,
158                                      BdrvRequestFlags flags);
159 int coroutine_fn blk_co_preadv_part(BlockBackend *blk, int64_t offset,
160                                     int64_t bytes, QEMUIOVector *qiov,
161                                     size_t qiov_offset, BdrvRequestFlags flags);
162 
163 int co_wrapper_mixed blk_pwrite(BlockBackend *blk, int64_t offset,
164                                 int64_t bytes, const void *buf,
165                                 BdrvRequestFlags flags);
166 int coroutine_fn blk_co_pwrite(BlockBackend *blk, int64_t offset, int64_t bytes,
167                                const void *buf, BdrvRequestFlags flags);
168 
169 int co_wrapper_mixed blk_pwritev(BlockBackend *blk, int64_t offset,
170                                  int64_t bytes, QEMUIOVector *qiov,
171                                  BdrvRequestFlags flags);
172 int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset,
173                                 int64_t bytes, QEMUIOVector *qiov,
174                                 BdrvRequestFlags flags);
175 
176 int co_wrapper_mixed blk_pwritev_part(BlockBackend *blk, int64_t offset,
177                                       int64_t bytes, QEMUIOVector *qiov,
178                                       size_t qiov_offset,
179                                       BdrvRequestFlags flags);
180 int coroutine_fn blk_co_pwritev_part(BlockBackend *blk, int64_t offset,
181                                      int64_t bytes,
182                                      QEMUIOVector *qiov, size_t qiov_offset,
183                                      BdrvRequestFlags flags);
184 
185 int co_wrapper_mixed blk_pwrite_compressed(BlockBackend *blk,
186                                            int64_t offset, int64_t bytes,
187                                            const void *buf);
188 int coroutine_fn blk_co_pwrite_compressed(BlockBackend *blk, int64_t offset,
189                                           int64_t bytes, const void *buf);
190 
191 int co_wrapper_mixed blk_pwrite_zeroes(BlockBackend *blk, int64_t offset,
192                                        int64_t bytes,
193                                        BdrvRequestFlags flags);
194 int coroutine_fn blk_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
195                                       int64_t bytes, BdrvRequestFlags flags);
196 
197 int coroutine_fn blk_co_zone_report(BlockBackend *blk, int64_t offset,
198                                     unsigned int *nr_zones,
199                                     BlockZoneDescriptor *zones);
200 int co_wrapper_mixed blk_zone_report(BlockBackend *blk, int64_t offset,
201                                          unsigned int *nr_zones,
202                                          BlockZoneDescriptor *zones);
203 int coroutine_fn blk_co_zone_mgmt(BlockBackend *blk, BlockZoneOp op,
204                                   int64_t offset, int64_t len);
205 int co_wrapper_mixed blk_zone_mgmt(BlockBackend *blk, BlockZoneOp op,
206                                        int64_t offset, int64_t len);
207 int coroutine_fn blk_co_zone_append(BlockBackend *blk, int64_t *offset,
208                                     QEMUIOVector *qiov,
209                                     BdrvRequestFlags flags);
210 int co_wrapper_mixed blk_zone_append(BlockBackend *blk, int64_t *offset,
211                                          QEMUIOVector *qiov,
212                                          BdrvRequestFlags flags);
213 
214 int co_wrapper_mixed blk_pdiscard(BlockBackend *blk, int64_t offset,
215                                   int64_t bytes);
216 int coroutine_fn blk_co_pdiscard(BlockBackend *blk, int64_t offset,
217                                  int64_t bytes);
218 
219 int co_wrapper_mixed blk_flush(BlockBackend *blk);
220 int coroutine_fn blk_co_flush(BlockBackend *blk);
221 
222 int co_wrapper_mixed blk_ioctl(BlockBackend *blk, unsigned long int req,
223                                void *buf);
224 int coroutine_fn blk_co_ioctl(BlockBackend *blk, unsigned long int req,
225                               void *buf);
226 
227 int co_wrapper_mixed blk_truncate(BlockBackend *blk, int64_t offset,
228                                   bool exact, PreallocMode prealloc,
229                                   BdrvRequestFlags flags, Error **errp);
230 int coroutine_fn blk_co_truncate(BlockBackend *blk, int64_t offset, bool exact,
231                                  PreallocMode prealloc, BdrvRequestFlags flags,
232                                  Error **errp);
233 
234 #endif /* BLOCK_BACKEND_IO_H */
235