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 
18 /*
19  * I/O API functions. These functions are thread-safe.
20  *
21  * See include/block/block-io.h for more information about
22  * the I/O API.
23  */
24 
25 const char *blk_name(const BlockBackend *blk);
26 
27 BlockDriverState *blk_bs(BlockBackend *blk);
28 
29 void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow);
30 void blk_set_allow_aio_context_change(BlockBackend *blk, bool allow);
31 void blk_set_disable_request_queuing(BlockBackend *blk, bool disable);
32 bool blk_iostatus_is_enabled(const BlockBackend *blk);
33 
34 char *blk_get_attached_dev_id(BlockBackend *blk);
35 
36 BlockAIOCB *blk_aio_pwrite_zeroes(BlockBackend *blk, int64_t offset,
37                                   int64_t bytes, BdrvRequestFlags flags,
38                                   BlockCompletionFunc *cb, void *opaque);
39 
40 BlockAIOCB *blk_aio_preadv(BlockBackend *blk, int64_t offset,
41                            QEMUIOVector *qiov, BdrvRequestFlags flags,
42                            BlockCompletionFunc *cb, void *opaque);
43 BlockAIOCB *blk_aio_pwritev(BlockBackend *blk, int64_t offset,
44                             QEMUIOVector *qiov, BdrvRequestFlags flags,
45                             BlockCompletionFunc *cb, void *opaque);
46 BlockAIOCB *blk_aio_flush(BlockBackend *blk,
47                           BlockCompletionFunc *cb, void *opaque);
48 BlockAIOCB *blk_aio_pdiscard(BlockBackend *blk, int64_t offset, int64_t bytes,
49                              BlockCompletionFunc *cb, void *opaque);
50 void blk_aio_cancel_async(BlockAIOCB *acb);
51 BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
52                           BlockCompletionFunc *cb, void *opaque);
53 
54 void blk_inc_in_flight(BlockBackend *blk);
55 void blk_dec_in_flight(BlockBackend *blk);
56 bool blk_is_inserted(BlockBackend *blk);
57 bool blk_is_available(BlockBackend *blk);
58 void blk_lock_medium(BlockBackend *blk, bool locked);
59 void blk_eject(BlockBackend *blk, bool eject_flag);
60 int64_t blk_getlength(BlockBackend *blk);
61 void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr);
62 int64_t blk_nb_sectors(BlockBackend *blk);
63 void *blk_try_blockalign(BlockBackend *blk, size_t size);
64 void *blk_blockalign(BlockBackend *blk, size_t size);
65 bool blk_is_writable(BlockBackend *blk);
66 bool blk_enable_write_cache(BlockBackend *blk);
67 BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read);
68 BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
69                                       int error);
70 void blk_error_action(BlockBackend *blk, BlockErrorAction action,
71                       bool is_read, int error);
72 void blk_iostatus_set_err(BlockBackend *blk, int error);
73 int blk_get_max_iov(BlockBackend *blk);
74 int blk_get_max_hw_iov(BlockBackend *blk);
75 
76 void blk_io_plug(BlockBackend *blk);
77 void blk_io_unplug(BlockBackend *blk);
78 AioContext *blk_get_aio_context(BlockBackend *blk);
79 BlockAcctStats *blk_get_stats(BlockBackend *blk);
80 void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
81                   BlockCompletionFunc *cb, void *opaque);
82 BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
83                                   BlockCompletionFunc *cb,
84                                   void *opaque, int ret);
85 
86 uint32_t blk_get_request_alignment(BlockBackend *blk);
87 uint32_t blk_get_max_transfer(BlockBackend *blk);
88 uint64_t blk_get_max_hw_transfer(BlockBackend *blk);
89 
90 int coroutine_fn blk_co_copy_range(BlockBackend *blk_in, int64_t off_in,
91                                    BlockBackend *blk_out, int64_t off_out,
92                                    int64_t bytes, BdrvRequestFlags read_flags,
93                                    BdrvRequestFlags write_flags);
94 
95 int coroutine_fn blk_co_block_status_above(BlockBackend *blk,
96                                            BlockDriverState *base,
97                                            int64_t offset, int64_t bytes,
98                                            int64_t *pnum, int64_t *map,
99                                            BlockDriverState **file);
100 int coroutine_fn blk_co_is_allocated_above(BlockBackend *blk,
101                                            BlockDriverState *base,
102                                            bool include_base, int64_t offset,
103                                            int64_t bytes, int64_t *pnum);
104 
105 /*
106  * "I/O or GS" API functions. These functions can run without
107  * the BQL, but only in one specific iothread/main loop.
108  *
109  * See include/block/block-io.h for more information about
110  * the "I/O or GS" API.
111  */
112 
113 int co_wrapper_mixed blk_pread(BlockBackend *blk, int64_t offset,
114                                int64_t bytes, void *buf,
115                                BdrvRequestFlags flags);
116 int coroutine_fn blk_co_pread(BlockBackend *blk, int64_t offset, int64_t bytes,
117                               void *buf, BdrvRequestFlags flags);
118 
119 int co_wrapper_mixed blk_preadv(BlockBackend *blk, int64_t offset,
120                                 int64_t bytes, QEMUIOVector *qiov,
121                                 BdrvRequestFlags flags);
122 int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset,
123                                int64_t bytes, QEMUIOVector *qiov,
124                                BdrvRequestFlags flags);
125 
126 int co_wrapper_mixed blk_preadv_part(BlockBackend *blk, int64_t offset,
127                                      int64_t bytes, QEMUIOVector *qiov,
128                                      size_t qiov_offset,
129                                      BdrvRequestFlags flags);
130 int coroutine_fn blk_co_preadv_part(BlockBackend *blk, int64_t offset,
131                                     int64_t bytes, QEMUIOVector *qiov,
132                                     size_t qiov_offset, BdrvRequestFlags flags);
133 
134 int co_wrapper_mixed blk_pwrite(BlockBackend *blk, int64_t offset,
135                                 int64_t bytes, const void *buf,
136                                 BdrvRequestFlags flags);
137 int coroutine_fn blk_co_pwrite(BlockBackend *blk, int64_t offset, int64_t bytes,
138                                const void *buf, BdrvRequestFlags flags);
139 
140 int co_wrapper_mixed blk_pwritev(BlockBackend *blk, int64_t offset,
141                                  int64_t bytes, QEMUIOVector *qiov,
142                                  BdrvRequestFlags flags);
143 int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset,
144                                 int64_t bytes, QEMUIOVector *qiov,
145                                 BdrvRequestFlags flags);
146 
147 int co_wrapper_mixed blk_pwritev_part(BlockBackend *blk, int64_t offset,
148                                       int64_t bytes, QEMUIOVector *qiov,
149                                       size_t qiov_offset,
150                                       BdrvRequestFlags flags);
151 int coroutine_fn blk_co_pwritev_part(BlockBackend *blk, int64_t offset,
152                                      int64_t bytes,
153                                      QEMUIOVector *qiov, size_t qiov_offset,
154                                      BdrvRequestFlags flags);
155 
156 int co_wrapper_mixed blk_pwrite_compressed(BlockBackend *blk,
157                                            int64_t offset, int64_t bytes,
158                                            const void *buf);
159 int coroutine_fn blk_co_pwrite_compressed(BlockBackend *blk, int64_t offset,
160                                           int64_t bytes, const void *buf);
161 
162 int co_wrapper_mixed blk_pwrite_zeroes(BlockBackend *blk, int64_t offset,
163                                        int64_t bytes,
164                                        BdrvRequestFlags flags);
165 int coroutine_fn blk_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
166                                       int64_t bytes, BdrvRequestFlags flags);
167 
168 int co_wrapper_mixed blk_pdiscard(BlockBackend *blk, int64_t offset,
169                                   int64_t bytes);
170 int coroutine_fn blk_co_pdiscard(BlockBackend *blk, int64_t offset,
171                                  int64_t bytes);
172 
173 int co_wrapper_mixed blk_flush(BlockBackend *blk);
174 int coroutine_fn blk_co_flush(BlockBackend *blk);
175 
176 int co_wrapper_mixed blk_ioctl(BlockBackend *blk, unsigned long int req,
177                                void *buf);
178 int coroutine_fn blk_co_ioctl(BlockBackend *blk, unsigned long int req,
179                               void *buf);
180 
181 int co_wrapper_mixed blk_truncate(BlockBackend *blk, int64_t offset,
182                                   bool exact, PreallocMode prealloc,
183                                   BdrvRequestFlags flags, Error **errp);
184 int coroutine_fn blk_co_truncate(BlockBackend *blk, int64_t offset, bool exact,
185                                  PreallocMode prealloc, BdrvRequestFlags flags,
186                                  Error **errp);
187 
188 #endif /* BLOCK_BACKEND_IO_H */
189