root / block.c @ 57c83dac
History | View | Annotate | Download (107.1 kB)
1 | fc01f7e7 | bellard | /*
|
---|---|---|---|
2 | fc01f7e7 | bellard | * QEMU System Emulator block driver
|
3 | 5fafdf24 | ths | *
|
4 | fc01f7e7 | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | fc01f7e7 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | fc01f7e7 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | fc01f7e7 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | fc01f7e7 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | fc01f7e7 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | fc01f7e7 | bellard | * furnished to do so, subject to the following conditions:
|
12 | fc01f7e7 | bellard | *
|
13 | fc01f7e7 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | fc01f7e7 | bellard | * all copies or substantial portions of the Software.
|
15 | fc01f7e7 | bellard | *
|
16 | fc01f7e7 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | fc01f7e7 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | fc01f7e7 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | fc01f7e7 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | fc01f7e7 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | fc01f7e7 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | fc01f7e7 | bellard | * THE SOFTWARE.
|
23 | fc01f7e7 | bellard | */
|
24 | 3990d09a | blueswir1 | #include "config-host.h" |
25 | faf07963 | pbrook | #include "qemu-common.h" |
26 | 6d519a5f | Stefan Hajnoczi | #include "trace.h" |
27 | 376253ec | aliguori | #include "monitor.h" |
28 | ea2384d3 | bellard | #include "block_int.h" |
29 | 5efa9d5a | Anthony Liguori | #include "module.h" |
30 | f795e743 | Luiz Capitulino | #include "qjson.h" |
31 | 68485420 | Kevin Wolf | #include "qemu-coroutine.h" |
32 | b2023818 | Luiz Capitulino | #include "qmp-commands.h" |
33 | 0563e191 | Zhi Yong Wu | #include "qemu-timer.h" |
34 | fc01f7e7 | bellard | |
35 | 71e72a19 | Juan Quintela | #ifdef CONFIG_BSD
|
36 | 7674e7bf | bellard | #include <sys/types.h> |
37 | 7674e7bf | bellard | #include <sys/stat.h> |
38 | 7674e7bf | bellard | #include <sys/ioctl.h> |
39 | 72cf2d4f | Blue Swirl | #include <sys/queue.h> |
40 | c5e97233 | blueswir1 | #ifndef __DragonFly__
|
41 | 7674e7bf | bellard | #include <sys/disk.h> |
42 | 7674e7bf | bellard | #endif
|
43 | c5e97233 | blueswir1 | #endif
|
44 | 7674e7bf | bellard | |
45 | 49dc768d | aliguori | #ifdef _WIN32
|
46 | 49dc768d | aliguori | #include <windows.h> |
47 | 49dc768d | aliguori | #endif
|
48 | 49dc768d | aliguori | |
49 | 1c9805a3 | Stefan Hajnoczi | #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ |
50 | 1c9805a3 | Stefan Hajnoczi | |
51 | 470c0504 | Stefan Hajnoczi | typedef enum { |
52 | 470c0504 | Stefan Hajnoczi | BDRV_REQ_COPY_ON_READ = 0x1,
|
53 | 470c0504 | Stefan Hajnoczi | } BdrvRequestFlags; |
54 | 470c0504 | Stefan Hajnoczi | |
55 | 7d4b4ba5 | Markus Armbruster | static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load); |
56 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
|
57 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
58 | c87c0672 | aliguori | BlockDriverCompletionFunc *cb, void *opaque);
|
59 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
|
60 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
61 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque);
|
62 | f9f05dc5 | Kevin Wolf | static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, |
63 | f9f05dc5 | Kevin Wolf | int64_t sector_num, int nb_sectors,
|
64 | f9f05dc5 | Kevin Wolf | QEMUIOVector *iov); |
65 | f9f05dc5 | Kevin Wolf | static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, |
66 | f9f05dc5 | Kevin Wolf | int64_t sector_num, int nb_sectors,
|
67 | f9f05dc5 | Kevin Wolf | QEMUIOVector *iov); |
68 | c5fbe571 | Stefan Hajnoczi | static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, |
69 | 470c0504 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
|
70 | 470c0504 | Stefan Hajnoczi | BdrvRequestFlags flags); |
71 | 1c9805a3 | Stefan Hajnoczi | static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, |
72 | 1c9805a3 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
|
73 | b2a61371 | Stefan Hajnoczi | static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
|
74 | b2a61371 | Stefan Hajnoczi | int64_t sector_num, |
75 | b2a61371 | Stefan Hajnoczi | QEMUIOVector *qiov, |
76 | b2a61371 | Stefan Hajnoczi | int nb_sectors,
|
77 | b2a61371 | Stefan Hajnoczi | BlockDriverCompletionFunc *cb, |
78 | b2a61371 | Stefan Hajnoczi | void *opaque,
|
79 | 8c5873d6 | Stefan Hajnoczi | bool is_write);
|
80 | b2a61371 | Stefan Hajnoczi | static void coroutine_fn bdrv_co_do_rw(void *opaque); |
81 | ec530c81 | bellard | |
82 | 98f90dba | Zhi Yong Wu | static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors, |
83 | 98f90dba | Zhi Yong Wu | bool is_write, double elapsed_time, uint64_t *wait); |
84 | 98f90dba | Zhi Yong Wu | static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write, |
85 | 98f90dba | Zhi Yong Wu | double elapsed_time, uint64_t *wait);
|
86 | 98f90dba | Zhi Yong Wu | static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors, |
87 | 98f90dba | Zhi Yong Wu | bool is_write, int64_t *wait);
|
88 | 98f90dba | Zhi Yong Wu | |
89 | 1b7bdbc1 | Stefan Hajnoczi | static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
|
90 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_HEAD_INITIALIZER(bdrv_states); |
91 | 7ee930d0 | blueswir1 | |
92 | 8a22f02a | Stefan Hajnoczi | static QLIST_HEAD(, BlockDriver) bdrv_drivers =
|
93 | 8a22f02a | Stefan Hajnoczi | QLIST_HEAD_INITIALIZER(bdrv_drivers); |
94 | ea2384d3 | bellard | |
95 | f9092b10 | Markus Armbruster | /* The device to use for VM snapshots */
|
96 | f9092b10 | Markus Armbruster | static BlockDriverState *bs_snapshots;
|
97 | f9092b10 | Markus Armbruster | |
98 | eb852011 | Markus Armbruster | /* If non-zero, use only whitelisted block drivers */
|
99 | eb852011 | Markus Armbruster | static int use_bdrv_whitelist; |
100 | eb852011 | Markus Armbruster | |
101 | 9e0b22f4 | Stefan Hajnoczi | #ifdef _WIN32
|
102 | 9e0b22f4 | Stefan Hajnoczi | static int is_windows_drive_prefix(const char *filename) |
103 | 9e0b22f4 | Stefan Hajnoczi | { |
104 | 9e0b22f4 | Stefan Hajnoczi | return (((filename[0] >= 'a' && filename[0] <= 'z') || |
105 | 9e0b22f4 | Stefan Hajnoczi | (filename[0] >= 'A' && filename[0] <= 'Z')) && |
106 | 9e0b22f4 | Stefan Hajnoczi | filename[1] == ':'); |
107 | 9e0b22f4 | Stefan Hajnoczi | } |
108 | 9e0b22f4 | Stefan Hajnoczi | |
109 | 9e0b22f4 | Stefan Hajnoczi | int is_windows_drive(const char *filename) |
110 | 9e0b22f4 | Stefan Hajnoczi | { |
111 | 9e0b22f4 | Stefan Hajnoczi | if (is_windows_drive_prefix(filename) &&
|
112 | 9e0b22f4 | Stefan Hajnoczi | filename[2] == '\0') |
113 | 9e0b22f4 | Stefan Hajnoczi | return 1; |
114 | 9e0b22f4 | Stefan Hajnoczi | if (strstart(filename, "\\\\.\\", NULL) || |
115 | 9e0b22f4 | Stefan Hajnoczi | strstart(filename, "//./", NULL)) |
116 | 9e0b22f4 | Stefan Hajnoczi | return 1; |
117 | 9e0b22f4 | Stefan Hajnoczi | return 0; |
118 | 9e0b22f4 | Stefan Hajnoczi | } |
119 | 9e0b22f4 | Stefan Hajnoczi | #endif
|
120 | 9e0b22f4 | Stefan Hajnoczi | |
121 | 0563e191 | Zhi Yong Wu | /* throttling disk I/O limits */
|
122 | 98f90dba | Zhi Yong Wu | void bdrv_io_limits_disable(BlockDriverState *bs)
|
123 | 98f90dba | Zhi Yong Wu | { |
124 | 98f90dba | Zhi Yong Wu | bs->io_limits_enabled = false;
|
125 | 98f90dba | Zhi Yong Wu | |
126 | 98f90dba | Zhi Yong Wu | while (qemu_co_queue_next(&bs->throttled_reqs));
|
127 | 98f90dba | Zhi Yong Wu | |
128 | 98f90dba | Zhi Yong Wu | if (bs->block_timer) {
|
129 | 98f90dba | Zhi Yong Wu | qemu_del_timer(bs->block_timer); |
130 | 98f90dba | Zhi Yong Wu | qemu_free_timer(bs->block_timer); |
131 | 98f90dba | Zhi Yong Wu | bs->block_timer = NULL;
|
132 | 98f90dba | Zhi Yong Wu | } |
133 | 98f90dba | Zhi Yong Wu | |
134 | 98f90dba | Zhi Yong Wu | bs->slice_start = 0;
|
135 | 98f90dba | Zhi Yong Wu | bs->slice_end = 0;
|
136 | 98f90dba | Zhi Yong Wu | bs->slice_time = 0;
|
137 | 98f90dba | Zhi Yong Wu | memset(&bs->io_base, 0, sizeof(bs->io_base)); |
138 | 98f90dba | Zhi Yong Wu | } |
139 | 98f90dba | Zhi Yong Wu | |
140 | 0563e191 | Zhi Yong Wu | static void bdrv_block_timer(void *opaque) |
141 | 0563e191 | Zhi Yong Wu | { |
142 | 0563e191 | Zhi Yong Wu | BlockDriverState *bs = opaque; |
143 | 0563e191 | Zhi Yong Wu | |
144 | 0563e191 | Zhi Yong Wu | qemu_co_queue_next(&bs->throttled_reqs); |
145 | 0563e191 | Zhi Yong Wu | } |
146 | 0563e191 | Zhi Yong Wu | |
147 | 0563e191 | Zhi Yong Wu | void bdrv_io_limits_enable(BlockDriverState *bs)
|
148 | 0563e191 | Zhi Yong Wu | { |
149 | 0563e191 | Zhi Yong Wu | qemu_co_queue_init(&bs->throttled_reqs); |
150 | 0563e191 | Zhi Yong Wu | bs->block_timer = qemu_new_timer_ns(vm_clock, bdrv_block_timer, bs); |
151 | 0563e191 | Zhi Yong Wu | bs->slice_time = 5 * BLOCK_IO_SLICE_TIME;
|
152 | 0563e191 | Zhi Yong Wu | bs->slice_start = qemu_get_clock_ns(vm_clock); |
153 | 0563e191 | Zhi Yong Wu | bs->slice_end = bs->slice_start + bs->slice_time; |
154 | 0563e191 | Zhi Yong Wu | memset(&bs->io_base, 0, sizeof(bs->io_base)); |
155 | 0563e191 | Zhi Yong Wu | bs->io_limits_enabled = true;
|
156 | 0563e191 | Zhi Yong Wu | } |
157 | 0563e191 | Zhi Yong Wu | |
158 | 0563e191 | Zhi Yong Wu | bool bdrv_io_limits_enabled(BlockDriverState *bs)
|
159 | 0563e191 | Zhi Yong Wu | { |
160 | 0563e191 | Zhi Yong Wu | BlockIOLimit *io_limits = &bs->io_limits; |
161 | 0563e191 | Zhi Yong Wu | return io_limits->bps[BLOCK_IO_LIMIT_READ]
|
162 | 0563e191 | Zhi Yong Wu | || io_limits->bps[BLOCK_IO_LIMIT_WRITE] |
163 | 0563e191 | Zhi Yong Wu | || io_limits->bps[BLOCK_IO_LIMIT_TOTAL] |
164 | 0563e191 | Zhi Yong Wu | || io_limits->iops[BLOCK_IO_LIMIT_READ] |
165 | 0563e191 | Zhi Yong Wu | || io_limits->iops[BLOCK_IO_LIMIT_WRITE] |
166 | 0563e191 | Zhi Yong Wu | || io_limits->iops[BLOCK_IO_LIMIT_TOTAL]; |
167 | 0563e191 | Zhi Yong Wu | } |
168 | 0563e191 | Zhi Yong Wu | |
169 | 98f90dba | Zhi Yong Wu | static void bdrv_io_limits_intercept(BlockDriverState *bs, |
170 | 98f90dba | Zhi Yong Wu | bool is_write, int nb_sectors) |
171 | 98f90dba | Zhi Yong Wu | { |
172 | 98f90dba | Zhi Yong Wu | int64_t wait_time = -1;
|
173 | 98f90dba | Zhi Yong Wu | |
174 | 98f90dba | Zhi Yong Wu | if (!qemu_co_queue_empty(&bs->throttled_reqs)) {
|
175 | 98f90dba | Zhi Yong Wu | qemu_co_queue_wait(&bs->throttled_reqs); |
176 | 98f90dba | Zhi Yong Wu | } |
177 | 98f90dba | Zhi Yong Wu | |
178 | 98f90dba | Zhi Yong Wu | /* In fact, we hope to keep each request's timing, in FIFO mode. The next
|
179 | 98f90dba | Zhi Yong Wu | * throttled requests will not be dequeued until the current request is
|
180 | 98f90dba | Zhi Yong Wu | * allowed to be serviced. So if the current request still exceeds the
|
181 | 98f90dba | Zhi Yong Wu | * limits, it will be inserted to the head. All requests followed it will
|
182 | 98f90dba | Zhi Yong Wu | * be still in throttled_reqs queue.
|
183 | 98f90dba | Zhi Yong Wu | */
|
184 | 98f90dba | Zhi Yong Wu | |
185 | 98f90dba | Zhi Yong Wu | while (bdrv_exceed_io_limits(bs, nb_sectors, is_write, &wait_time)) {
|
186 | 98f90dba | Zhi Yong Wu | qemu_mod_timer(bs->block_timer, |
187 | 98f90dba | Zhi Yong Wu | wait_time + qemu_get_clock_ns(vm_clock)); |
188 | 98f90dba | Zhi Yong Wu | qemu_co_queue_wait_insert_head(&bs->throttled_reqs); |
189 | 98f90dba | Zhi Yong Wu | } |
190 | 98f90dba | Zhi Yong Wu | |
191 | 98f90dba | Zhi Yong Wu | qemu_co_queue_next(&bs->throttled_reqs); |
192 | 98f90dba | Zhi Yong Wu | } |
193 | 98f90dba | Zhi Yong Wu | |
194 | 9e0b22f4 | Stefan Hajnoczi | /* check if the path starts with "<protocol>:" */
|
195 | 9e0b22f4 | Stefan Hajnoczi | static int path_has_protocol(const char *path) |
196 | 9e0b22f4 | Stefan Hajnoczi | { |
197 | 9e0b22f4 | Stefan Hajnoczi | #ifdef _WIN32
|
198 | 9e0b22f4 | Stefan Hajnoczi | if (is_windows_drive(path) ||
|
199 | 9e0b22f4 | Stefan Hajnoczi | is_windows_drive_prefix(path)) { |
200 | 9e0b22f4 | Stefan Hajnoczi | return 0; |
201 | 9e0b22f4 | Stefan Hajnoczi | } |
202 | 9e0b22f4 | Stefan Hajnoczi | #endif
|
203 | 9e0b22f4 | Stefan Hajnoczi | |
204 | 9e0b22f4 | Stefan Hajnoczi | return strchr(path, ':') != NULL; |
205 | 9e0b22f4 | Stefan Hajnoczi | } |
206 | 9e0b22f4 | Stefan Hajnoczi | |
207 | 83f64091 | bellard | int path_is_absolute(const char *path) |
208 | 3b0d4f61 | bellard | { |
209 | 83f64091 | bellard | const char *p; |
210 | 21664424 | bellard | #ifdef _WIN32
|
211 | 21664424 | bellard | /* specific case for names like: "\\.\d:" */
|
212 | 21664424 | bellard | if (*path == '/' || *path == '\\') |
213 | 21664424 | bellard | return 1; |
214 | 21664424 | bellard | #endif
|
215 | 83f64091 | bellard | p = strchr(path, ':');
|
216 | 83f64091 | bellard | if (p)
|
217 | 83f64091 | bellard | p++; |
218 | 83f64091 | bellard | else
|
219 | 83f64091 | bellard | p = path; |
220 | 3b9f94e1 | bellard | #ifdef _WIN32
|
221 | 3b9f94e1 | bellard | return (*p == '/' || *p == '\\'); |
222 | 3b9f94e1 | bellard | #else
|
223 | 3b9f94e1 | bellard | return (*p == '/'); |
224 | 3b9f94e1 | bellard | #endif
|
225 | 3b0d4f61 | bellard | } |
226 | 3b0d4f61 | bellard | |
227 | 83f64091 | bellard | /* if filename is absolute, just copy it to dest. Otherwise, build a
|
228 | 83f64091 | bellard | path to it by considering it is relative to base_path. URL are
|
229 | 83f64091 | bellard | supported. */
|
230 | 83f64091 | bellard | void path_combine(char *dest, int dest_size, |
231 | 83f64091 | bellard | const char *base_path, |
232 | 83f64091 | bellard | const char *filename) |
233 | 3b0d4f61 | bellard | { |
234 | 83f64091 | bellard | const char *p, *p1; |
235 | 83f64091 | bellard | int len;
|
236 | 83f64091 | bellard | |
237 | 83f64091 | bellard | if (dest_size <= 0) |
238 | 83f64091 | bellard | return;
|
239 | 83f64091 | bellard | if (path_is_absolute(filename)) {
|
240 | 83f64091 | bellard | pstrcpy(dest, dest_size, filename); |
241 | 83f64091 | bellard | } else {
|
242 | 83f64091 | bellard | p = strchr(base_path, ':');
|
243 | 83f64091 | bellard | if (p)
|
244 | 83f64091 | bellard | p++; |
245 | 83f64091 | bellard | else
|
246 | 83f64091 | bellard | p = base_path; |
247 | 3b9f94e1 | bellard | p1 = strrchr(base_path, '/');
|
248 | 3b9f94e1 | bellard | #ifdef _WIN32
|
249 | 3b9f94e1 | bellard | { |
250 | 3b9f94e1 | bellard | const char *p2; |
251 | 3b9f94e1 | bellard | p2 = strrchr(base_path, '\\');
|
252 | 3b9f94e1 | bellard | if (!p1 || p2 > p1)
|
253 | 3b9f94e1 | bellard | p1 = p2; |
254 | 3b9f94e1 | bellard | } |
255 | 3b9f94e1 | bellard | #endif
|
256 | 83f64091 | bellard | if (p1)
|
257 | 83f64091 | bellard | p1++; |
258 | 83f64091 | bellard | else
|
259 | 83f64091 | bellard | p1 = base_path; |
260 | 83f64091 | bellard | if (p1 > p)
|
261 | 83f64091 | bellard | p = p1; |
262 | 83f64091 | bellard | len = p - base_path; |
263 | 83f64091 | bellard | if (len > dest_size - 1) |
264 | 83f64091 | bellard | len = dest_size - 1;
|
265 | 83f64091 | bellard | memcpy(dest, base_path, len); |
266 | 83f64091 | bellard | dest[len] = '\0';
|
267 | 83f64091 | bellard | pstrcat(dest, dest_size, filename); |
268 | 3b0d4f61 | bellard | } |
269 | 3b0d4f61 | bellard | } |
270 | 3b0d4f61 | bellard | |
271 | 5efa9d5a | Anthony Liguori | void bdrv_register(BlockDriver *bdrv)
|
272 | ea2384d3 | bellard | { |
273 | 8c5873d6 | Stefan Hajnoczi | /* Block drivers without coroutine functions need emulation */
|
274 | 8c5873d6 | Stefan Hajnoczi | if (!bdrv->bdrv_co_readv) {
|
275 | f9f05dc5 | Kevin Wolf | bdrv->bdrv_co_readv = bdrv_co_readv_em; |
276 | f9f05dc5 | Kevin Wolf | bdrv->bdrv_co_writev = bdrv_co_writev_em; |
277 | f9f05dc5 | Kevin Wolf | |
278 | f8c35c1d | Stefan Hajnoczi | /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if
|
279 | f8c35c1d | Stefan Hajnoczi | * the block driver lacks aio we need to emulate that too.
|
280 | f8c35c1d | Stefan Hajnoczi | */
|
281 | f9f05dc5 | Kevin Wolf | if (!bdrv->bdrv_aio_readv) {
|
282 | f9f05dc5 | Kevin Wolf | /* add AIO emulation layer */
|
283 | f9f05dc5 | Kevin Wolf | bdrv->bdrv_aio_readv = bdrv_aio_readv_em; |
284 | f9f05dc5 | Kevin Wolf | bdrv->bdrv_aio_writev = bdrv_aio_writev_em; |
285 | f9f05dc5 | Kevin Wolf | } |
286 | 83f64091 | bellard | } |
287 | b2e12bc6 | Christoph Hellwig | |
288 | 8a22f02a | Stefan Hajnoczi | QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); |
289 | ea2384d3 | bellard | } |
290 | b338082b | bellard | |
291 | b338082b | bellard | /* create a new block device (by default it is empty) */
|
292 | b338082b | bellard | BlockDriverState *bdrv_new(const char *device_name) |
293 | b338082b | bellard | { |
294 | 1b7bdbc1 | Stefan Hajnoczi | BlockDriverState *bs; |
295 | b338082b | bellard | |
296 | 7267c094 | Anthony Liguori | bs = g_malloc0(sizeof(BlockDriverState));
|
297 | b338082b | bellard | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
|
298 | ea2384d3 | bellard | if (device_name[0] != '\0') { |
299 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); |
300 | ea2384d3 | bellard | } |
301 | 28a7282a | Luiz Capitulino | bdrv_iostatus_disable(bs); |
302 | b338082b | bellard | return bs;
|
303 | b338082b | bellard | } |
304 | b338082b | bellard | |
305 | ea2384d3 | bellard | BlockDriver *bdrv_find_format(const char *format_name) |
306 | ea2384d3 | bellard | { |
307 | ea2384d3 | bellard | BlockDriver *drv1; |
308 | 8a22f02a | Stefan Hajnoczi | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
309 | 8a22f02a | Stefan Hajnoczi | if (!strcmp(drv1->format_name, format_name)) {
|
310 | ea2384d3 | bellard | return drv1;
|
311 | 8a22f02a | Stefan Hajnoczi | } |
312 | ea2384d3 | bellard | } |
313 | ea2384d3 | bellard | return NULL; |
314 | ea2384d3 | bellard | } |
315 | ea2384d3 | bellard | |
316 | eb852011 | Markus Armbruster | static int bdrv_is_whitelisted(BlockDriver *drv) |
317 | eb852011 | Markus Armbruster | { |
318 | eb852011 | Markus Armbruster | static const char *whitelist[] = { |
319 | eb852011 | Markus Armbruster | CONFIG_BDRV_WHITELIST |
320 | eb852011 | Markus Armbruster | }; |
321 | eb852011 | Markus Armbruster | const char **p; |
322 | eb852011 | Markus Armbruster | |
323 | eb852011 | Markus Armbruster | if (!whitelist[0]) |
324 | eb852011 | Markus Armbruster | return 1; /* no whitelist, anything goes */ |
325 | eb852011 | Markus Armbruster | |
326 | eb852011 | Markus Armbruster | for (p = whitelist; *p; p++) {
|
327 | eb852011 | Markus Armbruster | if (!strcmp(drv->format_name, *p)) {
|
328 | eb852011 | Markus Armbruster | return 1; |
329 | eb852011 | Markus Armbruster | } |
330 | eb852011 | Markus Armbruster | } |
331 | eb852011 | Markus Armbruster | return 0; |
332 | eb852011 | Markus Armbruster | } |
333 | eb852011 | Markus Armbruster | |
334 | eb852011 | Markus Armbruster | BlockDriver *bdrv_find_whitelisted_format(const char *format_name) |
335 | eb852011 | Markus Armbruster | { |
336 | eb852011 | Markus Armbruster | BlockDriver *drv = bdrv_find_format(format_name); |
337 | eb852011 | Markus Armbruster | return drv && bdrv_is_whitelisted(drv) ? drv : NULL; |
338 | eb852011 | Markus Armbruster | } |
339 | eb852011 | Markus Armbruster | |
340 | 0e7e1989 | Kevin Wolf | int bdrv_create(BlockDriver *drv, const char* filename, |
341 | 0e7e1989 | Kevin Wolf | QEMUOptionParameter *options) |
342 | ea2384d3 | bellard | { |
343 | ea2384d3 | bellard | if (!drv->bdrv_create)
|
344 | ea2384d3 | bellard | return -ENOTSUP;
|
345 | 0e7e1989 | Kevin Wolf | |
346 | 0e7e1989 | Kevin Wolf | return drv->bdrv_create(filename, options);
|
347 | ea2384d3 | bellard | } |
348 | ea2384d3 | bellard | |
349 | 84a12e66 | Christoph Hellwig | int bdrv_create_file(const char* filename, QEMUOptionParameter *options) |
350 | 84a12e66 | Christoph Hellwig | { |
351 | 84a12e66 | Christoph Hellwig | BlockDriver *drv; |
352 | 84a12e66 | Christoph Hellwig | |
353 | b50cbabc | MORITA Kazutaka | drv = bdrv_find_protocol(filename); |
354 | 84a12e66 | Christoph Hellwig | if (drv == NULL) { |
355 | 16905d71 | Stefan Hajnoczi | return -ENOENT;
|
356 | 84a12e66 | Christoph Hellwig | } |
357 | 84a12e66 | Christoph Hellwig | |
358 | 84a12e66 | Christoph Hellwig | return bdrv_create(drv, filename, options);
|
359 | 84a12e66 | Christoph Hellwig | } |
360 | 84a12e66 | Christoph Hellwig | |
361 | d5249393 | bellard | #ifdef _WIN32
|
362 | 95389c86 | bellard | void get_tmp_filename(char *filename, int size) |
363 | d5249393 | bellard | { |
364 | 3b9f94e1 | bellard | char temp_dir[MAX_PATH];
|
365 | 3b46e624 | ths | |
366 | 3b9f94e1 | bellard | GetTempPath(MAX_PATH, temp_dir); |
367 | 3b9f94e1 | bellard | GetTempFileName(temp_dir, "qem", 0, filename); |
368 | d5249393 | bellard | } |
369 | d5249393 | bellard | #else
|
370 | 95389c86 | bellard | void get_tmp_filename(char *filename, int size) |
371 | fc01f7e7 | bellard | { |
372 | 67b915a5 | bellard | int fd;
|
373 | 7ccfb2eb | blueswir1 | const char *tmpdir; |
374 | d5249393 | bellard | /* XXX: race condition possible */
|
375 | 0badc1ee | aurel32 | tmpdir = getenv("TMPDIR");
|
376 | 0badc1ee | aurel32 | if (!tmpdir)
|
377 | 0badc1ee | aurel32 | tmpdir = "/tmp";
|
378 | 0badc1ee | aurel32 | snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
|
379 | ea2384d3 | bellard | fd = mkstemp(filename); |
380 | ea2384d3 | bellard | close(fd); |
381 | ea2384d3 | bellard | } |
382 | d5249393 | bellard | #endif
|
383 | fc01f7e7 | bellard | |
384 | 84a12e66 | Christoph Hellwig | /*
|
385 | 84a12e66 | Christoph Hellwig | * Detect host devices. By convention, /dev/cdrom[N] is always
|
386 | 84a12e66 | Christoph Hellwig | * recognized as a host CDROM.
|
387 | 84a12e66 | Christoph Hellwig | */
|
388 | 84a12e66 | Christoph Hellwig | static BlockDriver *find_hdev_driver(const char *filename) |
389 | 84a12e66 | Christoph Hellwig | { |
390 | 84a12e66 | Christoph Hellwig | int score_max = 0, score; |
391 | 84a12e66 | Christoph Hellwig | BlockDriver *drv = NULL, *d;
|
392 | 84a12e66 | Christoph Hellwig | |
393 | 84a12e66 | Christoph Hellwig | QLIST_FOREACH(d, &bdrv_drivers, list) { |
394 | 84a12e66 | Christoph Hellwig | if (d->bdrv_probe_device) {
|
395 | 84a12e66 | Christoph Hellwig | score = d->bdrv_probe_device(filename); |
396 | 84a12e66 | Christoph Hellwig | if (score > score_max) {
|
397 | 84a12e66 | Christoph Hellwig | score_max = score; |
398 | 84a12e66 | Christoph Hellwig | drv = d; |
399 | 84a12e66 | Christoph Hellwig | } |
400 | 84a12e66 | Christoph Hellwig | } |
401 | 84a12e66 | Christoph Hellwig | } |
402 | 84a12e66 | Christoph Hellwig | |
403 | 84a12e66 | Christoph Hellwig | return drv;
|
404 | 84a12e66 | Christoph Hellwig | } |
405 | 84a12e66 | Christoph Hellwig | |
406 | b50cbabc | MORITA Kazutaka | BlockDriver *bdrv_find_protocol(const char *filename) |
407 | 83f64091 | bellard | { |
408 | 83f64091 | bellard | BlockDriver *drv1; |
409 | 83f64091 | bellard | char protocol[128]; |
410 | 1cec71e3 | Anthony Liguori | int len;
|
411 | 83f64091 | bellard | const char *p; |
412 | 19cb3738 | bellard | |
413 | 66f82cee | Kevin Wolf | /* TODO Drivers without bdrv_file_open must be specified explicitly */
|
414 | 66f82cee | Kevin Wolf | |
415 | 39508e7a | Christoph Hellwig | /*
|
416 | 39508e7a | Christoph Hellwig | * XXX(hch): we really should not let host device detection
|
417 | 39508e7a | Christoph Hellwig | * override an explicit protocol specification, but moving this
|
418 | 39508e7a | Christoph Hellwig | * later breaks access to device names with colons in them.
|
419 | 39508e7a | Christoph Hellwig | * Thanks to the brain-dead persistent naming schemes on udev-
|
420 | 39508e7a | Christoph Hellwig | * based Linux systems those actually are quite common.
|
421 | 39508e7a | Christoph Hellwig | */
|
422 | 39508e7a | Christoph Hellwig | drv1 = find_hdev_driver(filename); |
423 | 39508e7a | Christoph Hellwig | if (drv1) {
|
424 | 39508e7a | Christoph Hellwig | return drv1;
|
425 | 39508e7a | Christoph Hellwig | } |
426 | 39508e7a | Christoph Hellwig | |
427 | 9e0b22f4 | Stefan Hajnoczi | if (!path_has_protocol(filename)) {
|
428 | 39508e7a | Christoph Hellwig | return bdrv_find_format("file"); |
429 | 84a12e66 | Christoph Hellwig | } |
430 | 9e0b22f4 | Stefan Hajnoczi | p = strchr(filename, ':');
|
431 | 9e0b22f4 | Stefan Hajnoczi | assert(p != NULL);
|
432 | 1cec71e3 | Anthony Liguori | len = p - filename; |
433 | 1cec71e3 | Anthony Liguori | if (len > sizeof(protocol) - 1) |
434 | 1cec71e3 | Anthony Liguori | len = sizeof(protocol) - 1; |
435 | 1cec71e3 | Anthony Liguori | memcpy(protocol, filename, len); |
436 | 1cec71e3 | Anthony Liguori | protocol[len] = '\0';
|
437 | 8a22f02a | Stefan Hajnoczi | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
438 | 5fafdf24 | ths | if (drv1->protocol_name &&
|
439 | 8a22f02a | Stefan Hajnoczi | !strcmp(drv1->protocol_name, protocol)) { |
440 | 83f64091 | bellard | return drv1;
|
441 | 8a22f02a | Stefan Hajnoczi | } |
442 | 83f64091 | bellard | } |
443 | 83f64091 | bellard | return NULL; |
444 | 83f64091 | bellard | } |
445 | 83f64091 | bellard | |
446 | c98ac35d | Stefan Weil | static int find_image_format(const char *filename, BlockDriver **pdrv) |
447 | f3a5d3f8 | Christoph Hellwig | { |
448 | f3a5d3f8 | Christoph Hellwig | int ret, score, score_max;
|
449 | f3a5d3f8 | Christoph Hellwig | BlockDriver *drv1, *drv; |
450 | f3a5d3f8 | Christoph Hellwig | uint8_t buf[2048];
|
451 | f3a5d3f8 | Christoph Hellwig | BlockDriverState *bs; |
452 | f3a5d3f8 | Christoph Hellwig | |
453 | f5edb014 | Naphtali Sprei | ret = bdrv_file_open(&bs, filename, 0);
|
454 | c98ac35d | Stefan Weil | if (ret < 0) { |
455 | c98ac35d | Stefan Weil | *pdrv = NULL;
|
456 | c98ac35d | Stefan Weil | return ret;
|
457 | c98ac35d | Stefan Weil | } |
458 | f8ea0b00 | Nicholas Bellinger | |
459 | 08a00559 | Kevin Wolf | /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
|
460 | 08a00559 | Kevin Wolf | if (bs->sg || !bdrv_is_inserted(bs)) {
|
461 | 1a396859 | Nicholas A. Bellinger | bdrv_delete(bs); |
462 | c98ac35d | Stefan Weil | drv = bdrv_find_format("raw");
|
463 | c98ac35d | Stefan Weil | if (!drv) {
|
464 | c98ac35d | Stefan Weil | ret = -ENOENT; |
465 | c98ac35d | Stefan Weil | } |
466 | c98ac35d | Stefan Weil | *pdrv = drv; |
467 | c98ac35d | Stefan Weil | return ret;
|
468 | 1a396859 | Nicholas A. Bellinger | } |
469 | f8ea0b00 | Nicholas Bellinger | |
470 | 83f64091 | bellard | ret = bdrv_pread(bs, 0, buf, sizeof(buf)); |
471 | 83f64091 | bellard | bdrv_delete(bs); |
472 | 83f64091 | bellard | if (ret < 0) { |
473 | c98ac35d | Stefan Weil | *pdrv = NULL;
|
474 | c98ac35d | Stefan Weil | return ret;
|
475 | 83f64091 | bellard | } |
476 | 83f64091 | bellard | |
477 | ea2384d3 | bellard | score_max = 0;
|
478 | 84a12e66 | Christoph Hellwig | drv = NULL;
|
479 | 8a22f02a | Stefan Hajnoczi | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
480 | 83f64091 | bellard | if (drv1->bdrv_probe) {
|
481 | 83f64091 | bellard | score = drv1->bdrv_probe(buf, ret, filename); |
482 | 83f64091 | bellard | if (score > score_max) {
|
483 | 83f64091 | bellard | score_max = score; |
484 | 83f64091 | bellard | drv = drv1; |
485 | 83f64091 | bellard | } |
486 | 0849bf08 | bellard | } |
487 | fc01f7e7 | bellard | } |
488 | c98ac35d | Stefan Weil | if (!drv) {
|
489 | c98ac35d | Stefan Weil | ret = -ENOENT; |
490 | c98ac35d | Stefan Weil | } |
491 | c98ac35d | Stefan Weil | *pdrv = drv; |
492 | c98ac35d | Stefan Weil | return ret;
|
493 | ea2384d3 | bellard | } |
494 | ea2384d3 | bellard | |
495 | 51762288 | Stefan Hajnoczi | /**
|
496 | 51762288 | Stefan Hajnoczi | * Set the current 'total_sectors' value
|
497 | 51762288 | Stefan Hajnoczi | */
|
498 | 51762288 | Stefan Hajnoczi | static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) |
499 | 51762288 | Stefan Hajnoczi | { |
500 | 51762288 | Stefan Hajnoczi | BlockDriver *drv = bs->drv; |
501 | 51762288 | Stefan Hajnoczi | |
502 | 396759ad | Nicholas Bellinger | /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
|
503 | 396759ad | Nicholas Bellinger | if (bs->sg)
|
504 | 396759ad | Nicholas Bellinger | return 0; |
505 | 396759ad | Nicholas Bellinger | |
506 | 51762288 | Stefan Hajnoczi | /* query actual device if possible, otherwise just trust the hint */
|
507 | 51762288 | Stefan Hajnoczi | if (drv->bdrv_getlength) {
|
508 | 51762288 | Stefan Hajnoczi | int64_t length = drv->bdrv_getlength(bs); |
509 | 51762288 | Stefan Hajnoczi | if (length < 0) { |
510 | 51762288 | Stefan Hajnoczi | return length;
|
511 | 51762288 | Stefan Hajnoczi | } |
512 | 51762288 | Stefan Hajnoczi | hint = length >> BDRV_SECTOR_BITS; |
513 | 51762288 | Stefan Hajnoczi | } |
514 | 51762288 | Stefan Hajnoczi | |
515 | 51762288 | Stefan Hajnoczi | bs->total_sectors = hint; |
516 | 51762288 | Stefan Hajnoczi | return 0; |
517 | 51762288 | Stefan Hajnoczi | } |
518 | 51762288 | Stefan Hajnoczi | |
519 | c3993cdc | Stefan Hajnoczi | /**
|
520 | c3993cdc | Stefan Hajnoczi | * Set open flags for a given cache mode
|
521 | c3993cdc | Stefan Hajnoczi | *
|
522 | c3993cdc | Stefan Hajnoczi | * Return 0 on success, -1 if the cache mode was invalid.
|
523 | c3993cdc | Stefan Hajnoczi | */
|
524 | c3993cdc | Stefan Hajnoczi | int bdrv_parse_cache_flags(const char *mode, int *flags) |
525 | c3993cdc | Stefan Hajnoczi | { |
526 | c3993cdc | Stefan Hajnoczi | *flags &= ~BDRV_O_CACHE_MASK; |
527 | c3993cdc | Stefan Hajnoczi | |
528 | c3993cdc | Stefan Hajnoczi | if (!strcmp(mode, "off") || !strcmp(mode, "none")) { |
529 | c3993cdc | Stefan Hajnoczi | *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; |
530 | 92196b2f | Stefan Hajnoczi | } else if (!strcmp(mode, "directsync")) { |
531 | 92196b2f | Stefan Hajnoczi | *flags |= BDRV_O_NOCACHE; |
532 | c3993cdc | Stefan Hajnoczi | } else if (!strcmp(mode, "writeback")) { |
533 | c3993cdc | Stefan Hajnoczi | *flags |= BDRV_O_CACHE_WB; |
534 | c3993cdc | Stefan Hajnoczi | } else if (!strcmp(mode, "unsafe")) { |
535 | c3993cdc | Stefan Hajnoczi | *flags |= BDRV_O_CACHE_WB; |
536 | c3993cdc | Stefan Hajnoczi | *flags |= BDRV_O_NO_FLUSH; |
537 | c3993cdc | Stefan Hajnoczi | } else if (!strcmp(mode, "writethrough")) { |
538 | c3993cdc | Stefan Hajnoczi | /* this is the default */
|
539 | c3993cdc | Stefan Hajnoczi | } else {
|
540 | c3993cdc | Stefan Hajnoczi | return -1; |
541 | c3993cdc | Stefan Hajnoczi | } |
542 | c3993cdc | Stefan Hajnoczi | |
543 | c3993cdc | Stefan Hajnoczi | return 0; |
544 | c3993cdc | Stefan Hajnoczi | } |
545 | c3993cdc | Stefan Hajnoczi | |
546 | 53fec9d3 | Stefan Hajnoczi | /**
|
547 | 53fec9d3 | Stefan Hajnoczi | * The copy-on-read flag is actually a reference count so multiple users may
|
548 | 53fec9d3 | Stefan Hajnoczi | * use the feature without worrying about clobbering its previous state.
|
549 | 53fec9d3 | Stefan Hajnoczi | * Copy-on-read stays enabled until all users have called to disable it.
|
550 | 53fec9d3 | Stefan Hajnoczi | */
|
551 | 53fec9d3 | Stefan Hajnoczi | void bdrv_enable_copy_on_read(BlockDriverState *bs)
|
552 | 53fec9d3 | Stefan Hajnoczi | { |
553 | 53fec9d3 | Stefan Hajnoczi | bs->copy_on_read++; |
554 | 53fec9d3 | Stefan Hajnoczi | } |
555 | 53fec9d3 | Stefan Hajnoczi | |
556 | 53fec9d3 | Stefan Hajnoczi | void bdrv_disable_copy_on_read(BlockDriverState *bs)
|
557 | 53fec9d3 | Stefan Hajnoczi | { |
558 | 53fec9d3 | Stefan Hajnoczi | assert(bs->copy_on_read > 0);
|
559 | 53fec9d3 | Stefan Hajnoczi | bs->copy_on_read--; |
560 | 53fec9d3 | Stefan Hajnoczi | } |
561 | 53fec9d3 | Stefan Hajnoczi | |
562 | b6ce07aa | Kevin Wolf | /*
|
563 | 57915332 | Kevin Wolf | * Common part for opening disk images and files
|
564 | 57915332 | Kevin Wolf | */
|
565 | 57915332 | Kevin Wolf | static int bdrv_open_common(BlockDriverState *bs, const char *filename, |
566 | 57915332 | Kevin Wolf | int flags, BlockDriver *drv)
|
567 | 57915332 | Kevin Wolf | { |
568 | 57915332 | Kevin Wolf | int ret, open_flags;
|
569 | 57915332 | Kevin Wolf | |
570 | 57915332 | Kevin Wolf | assert(drv != NULL);
|
571 | 57915332 | Kevin Wolf | |
572 | 28dcee10 | Stefan Hajnoczi | trace_bdrv_open_common(bs, filename, flags, drv->format_name); |
573 | 28dcee10 | Stefan Hajnoczi | |
574 | 66f82cee | Kevin Wolf | bs->file = NULL;
|
575 | 51762288 | Stefan Hajnoczi | bs->total_sectors = 0;
|
576 | 57915332 | Kevin Wolf | bs->encrypted = 0;
|
577 | 57915332 | Kevin Wolf | bs->valid_key = 0;
|
578 | 03f541bd | Stefan Hajnoczi | bs->sg = 0;
|
579 | 57915332 | Kevin Wolf | bs->open_flags = flags; |
580 | 03f541bd | Stefan Hajnoczi | bs->growable = 0;
|
581 | 57915332 | Kevin Wolf | bs->buffer_alignment = 512;
|
582 | 57915332 | Kevin Wolf | |
583 | 53fec9d3 | Stefan Hajnoczi | assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */ |
584 | 53fec9d3 | Stefan Hajnoczi | if ((flags & BDRV_O_RDWR) && (flags & BDRV_O_COPY_ON_READ)) {
|
585 | 53fec9d3 | Stefan Hajnoczi | bdrv_enable_copy_on_read(bs); |
586 | 53fec9d3 | Stefan Hajnoczi | } |
587 | 53fec9d3 | Stefan Hajnoczi | |
588 | 57915332 | Kevin Wolf | pstrcpy(bs->filename, sizeof(bs->filename), filename);
|
589 | 03f541bd | Stefan Hajnoczi | bs->backing_file[0] = '\0'; |
590 | 57915332 | Kevin Wolf | |
591 | 57915332 | Kevin Wolf | if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
|
592 | 57915332 | Kevin Wolf | return -ENOTSUP;
|
593 | 57915332 | Kevin Wolf | } |
594 | 57915332 | Kevin Wolf | |
595 | 57915332 | Kevin Wolf | bs->drv = drv; |
596 | 7267c094 | Anthony Liguori | bs->opaque = g_malloc0(drv->instance_size); |
597 | 57915332 | Kevin Wolf | |
598 | 03f541bd | Stefan Hajnoczi | bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB); |
599 | 57915332 | Kevin Wolf | |
600 | 57915332 | Kevin Wolf | /*
|
601 | 57915332 | Kevin Wolf | * Clear flags that are internal to the block layer before opening the
|
602 | 57915332 | Kevin Wolf | * image.
|
603 | 57915332 | Kevin Wolf | */
|
604 | 57915332 | Kevin Wolf | open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); |
605 | 57915332 | Kevin Wolf | |
606 | 57915332 | Kevin Wolf | /*
|
607 | ebabb67a | Stefan Weil | * Snapshots should be writable.
|
608 | 57915332 | Kevin Wolf | */
|
609 | 57915332 | Kevin Wolf | if (bs->is_temporary) {
|
610 | 57915332 | Kevin Wolf | open_flags |= BDRV_O_RDWR; |
611 | 57915332 | Kevin Wolf | } |
612 | 57915332 | Kevin Wolf | |
613 | e7c63796 | Stefan Hajnoczi | bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR); |
614 | e7c63796 | Stefan Hajnoczi | |
615 | 66f82cee | Kevin Wolf | /* Open the image, either directly or using a protocol */
|
616 | 66f82cee | Kevin Wolf | if (drv->bdrv_file_open) {
|
617 | 66f82cee | Kevin Wolf | ret = drv->bdrv_file_open(bs, filename, open_flags); |
618 | 66f82cee | Kevin Wolf | } else {
|
619 | 66f82cee | Kevin Wolf | ret = bdrv_file_open(&bs->file, filename, open_flags); |
620 | 66f82cee | Kevin Wolf | if (ret >= 0) { |
621 | 66f82cee | Kevin Wolf | ret = drv->bdrv_open(bs, open_flags); |
622 | 66f82cee | Kevin Wolf | } |
623 | 66f82cee | Kevin Wolf | } |
624 | 66f82cee | Kevin Wolf | |
625 | 57915332 | Kevin Wolf | if (ret < 0) { |
626 | 57915332 | Kevin Wolf | goto free_and_fail;
|
627 | 57915332 | Kevin Wolf | } |
628 | 57915332 | Kevin Wolf | |
629 | 51762288 | Stefan Hajnoczi | ret = refresh_total_sectors(bs, bs->total_sectors); |
630 | 51762288 | Stefan Hajnoczi | if (ret < 0) { |
631 | 51762288 | Stefan Hajnoczi | goto free_and_fail;
|
632 | 57915332 | Kevin Wolf | } |
633 | 51762288 | Stefan Hajnoczi | |
634 | 57915332 | Kevin Wolf | #ifndef _WIN32
|
635 | 57915332 | Kevin Wolf | if (bs->is_temporary) {
|
636 | 57915332 | Kevin Wolf | unlink(filename); |
637 | 57915332 | Kevin Wolf | } |
638 | 57915332 | Kevin Wolf | #endif
|
639 | 57915332 | Kevin Wolf | return 0; |
640 | 57915332 | Kevin Wolf | |
641 | 57915332 | Kevin Wolf | free_and_fail:
|
642 | 66f82cee | Kevin Wolf | if (bs->file) {
|
643 | 66f82cee | Kevin Wolf | bdrv_delete(bs->file); |
644 | 66f82cee | Kevin Wolf | bs->file = NULL;
|
645 | 66f82cee | Kevin Wolf | } |
646 | 7267c094 | Anthony Liguori | g_free(bs->opaque); |
647 | 57915332 | Kevin Wolf | bs->opaque = NULL;
|
648 | 57915332 | Kevin Wolf | bs->drv = NULL;
|
649 | 57915332 | Kevin Wolf | return ret;
|
650 | 57915332 | Kevin Wolf | } |
651 | 57915332 | Kevin Wolf | |
652 | 57915332 | Kevin Wolf | /*
|
653 | b6ce07aa | Kevin Wolf | * Opens a file using a protocol (file, host_device, nbd, ...)
|
654 | b6ce07aa | Kevin Wolf | */
|
655 | 83f64091 | bellard | int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) |
656 | ea2384d3 | bellard | { |
657 | 83f64091 | bellard | BlockDriverState *bs; |
658 | 6db95603 | Christoph Hellwig | BlockDriver *drv; |
659 | 83f64091 | bellard | int ret;
|
660 | 83f64091 | bellard | |
661 | b50cbabc | MORITA Kazutaka | drv = bdrv_find_protocol(filename); |
662 | 6db95603 | Christoph Hellwig | if (!drv) {
|
663 | 6db95603 | Christoph Hellwig | return -ENOENT;
|
664 | 6db95603 | Christoph Hellwig | } |
665 | 6db95603 | Christoph Hellwig | |
666 | 83f64091 | bellard | bs = bdrv_new("");
|
667 | b6ce07aa | Kevin Wolf | ret = bdrv_open_common(bs, filename, flags, drv); |
668 | 83f64091 | bellard | if (ret < 0) { |
669 | 83f64091 | bellard | bdrv_delete(bs); |
670 | 83f64091 | bellard | return ret;
|
671 | 3b0d4f61 | bellard | } |
672 | 71d0770c | aliguori | bs->growable = 1;
|
673 | 83f64091 | bellard | *pbs = bs; |
674 | 83f64091 | bellard | return 0; |
675 | 83f64091 | bellard | } |
676 | 83f64091 | bellard | |
677 | b6ce07aa | Kevin Wolf | /*
|
678 | b6ce07aa | Kevin Wolf | * Opens a disk image (raw, qcow2, vmdk, ...)
|
679 | b6ce07aa | Kevin Wolf | */
|
680 | d6e9098e | Kevin Wolf | int bdrv_open(BlockDriverState *bs, const char *filename, int flags, |
681 | d6e9098e | Kevin Wolf | BlockDriver *drv) |
682 | ea2384d3 | bellard | { |
683 | b6ce07aa | Kevin Wolf | int ret;
|
684 | 2b572816 | Kevin Wolf | char tmp_filename[PATH_MAX];
|
685 | 712e7874 | bellard | |
686 | 83f64091 | bellard | if (flags & BDRV_O_SNAPSHOT) {
|
687 | ea2384d3 | bellard | BlockDriverState *bs1; |
688 | ea2384d3 | bellard | int64_t total_size; |
689 | 7c96d46e | aliguori | int is_protocol = 0; |
690 | 91a073a9 | Kevin Wolf | BlockDriver *bdrv_qcow2; |
691 | 91a073a9 | Kevin Wolf | QEMUOptionParameter *options; |
692 | b6ce07aa | Kevin Wolf | char backing_filename[PATH_MAX];
|
693 | 3b46e624 | ths | |
694 | ea2384d3 | bellard | /* if snapshot, we create a temporary backing file and open it
|
695 | ea2384d3 | bellard | instead of opening 'filename' directly */
|
696 | 33e3963e | bellard | |
697 | ea2384d3 | bellard | /* if there is a backing file, use it */
|
698 | ea2384d3 | bellard | bs1 = bdrv_new("");
|
699 | d6e9098e | Kevin Wolf | ret = bdrv_open(bs1, filename, 0, drv);
|
700 | 51d7c00c | aliguori | if (ret < 0) { |
701 | ea2384d3 | bellard | bdrv_delete(bs1); |
702 | 51d7c00c | aliguori | return ret;
|
703 | ea2384d3 | bellard | } |
704 | 3e82990b | Jes Sorensen | total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; |
705 | 7c96d46e | aliguori | |
706 | 7c96d46e | aliguori | if (bs1->drv && bs1->drv->protocol_name)
|
707 | 7c96d46e | aliguori | is_protocol = 1;
|
708 | 7c96d46e | aliguori | |
709 | ea2384d3 | bellard | bdrv_delete(bs1); |
710 | 3b46e624 | ths | |
711 | ea2384d3 | bellard | get_tmp_filename(tmp_filename, sizeof(tmp_filename));
|
712 | 7c96d46e | aliguori | |
713 | 7c96d46e | aliguori | /* Real path is meaningless for protocols */
|
714 | 7c96d46e | aliguori | if (is_protocol)
|
715 | 7c96d46e | aliguori | snprintf(backing_filename, sizeof(backing_filename),
|
716 | 7c96d46e | aliguori | "%s", filename);
|
717 | 114cdfa9 | Kirill A. Shutemov | else if (!realpath(filename, backing_filename)) |
718 | 114cdfa9 | Kirill A. Shutemov | return -errno;
|
719 | 7c96d46e | aliguori | |
720 | 91a073a9 | Kevin Wolf | bdrv_qcow2 = bdrv_find_format("qcow2");
|
721 | 91a073a9 | Kevin Wolf | options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); |
722 | 91a073a9 | Kevin Wolf | |
723 | 3e82990b | Jes Sorensen | set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size); |
724 | 91a073a9 | Kevin Wolf | set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); |
725 | 91a073a9 | Kevin Wolf | if (drv) {
|
726 | 91a073a9 | Kevin Wolf | set_option_parameter(options, BLOCK_OPT_BACKING_FMT, |
727 | 91a073a9 | Kevin Wolf | drv->format_name); |
728 | 91a073a9 | Kevin Wolf | } |
729 | 91a073a9 | Kevin Wolf | |
730 | 91a073a9 | Kevin Wolf | ret = bdrv_create(bdrv_qcow2, tmp_filename, options); |
731 | d748768c | Jan Kiszka | free_option_parameters(options); |
732 | 51d7c00c | aliguori | if (ret < 0) { |
733 | 51d7c00c | aliguori | return ret;
|
734 | ea2384d3 | bellard | } |
735 | 91a073a9 | Kevin Wolf | |
736 | ea2384d3 | bellard | filename = tmp_filename; |
737 | 91a073a9 | Kevin Wolf | drv = bdrv_qcow2; |
738 | ea2384d3 | bellard | bs->is_temporary = 1;
|
739 | ea2384d3 | bellard | } |
740 | 712e7874 | bellard | |
741 | b6ce07aa | Kevin Wolf | /* Find the right image format driver */
|
742 | 6db95603 | Christoph Hellwig | if (!drv) {
|
743 | c98ac35d | Stefan Weil | ret = find_image_format(filename, &drv); |
744 | 51d7c00c | aliguori | } |
745 | 6987307c | Christoph Hellwig | |
746 | 51d7c00c | aliguori | if (!drv) {
|
747 | 51d7c00c | aliguori | goto unlink_and_fail;
|
748 | ea2384d3 | bellard | } |
749 | b6ce07aa | Kevin Wolf | |
750 | b6ce07aa | Kevin Wolf | /* Open the image */
|
751 | b6ce07aa | Kevin Wolf | ret = bdrv_open_common(bs, filename, flags, drv); |
752 | b6ce07aa | Kevin Wolf | if (ret < 0) { |
753 | 6987307c | Christoph Hellwig | goto unlink_and_fail;
|
754 | 6987307c | Christoph Hellwig | } |
755 | 6987307c | Christoph Hellwig | |
756 | b6ce07aa | Kevin Wolf | /* If there is a backing file, use it */
|
757 | b6ce07aa | Kevin Wolf | if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') { |
758 | b6ce07aa | Kevin Wolf | char backing_filename[PATH_MAX];
|
759 | b6ce07aa | Kevin Wolf | int back_flags;
|
760 | b6ce07aa | Kevin Wolf | BlockDriver *back_drv = NULL;
|
761 | b6ce07aa | Kevin Wolf | |
762 | b6ce07aa | Kevin Wolf | bs->backing_hd = bdrv_new("");
|
763 | df2dbb4a | Stefan Hajnoczi | |
764 | df2dbb4a | Stefan Hajnoczi | if (path_has_protocol(bs->backing_file)) {
|
765 | df2dbb4a | Stefan Hajnoczi | pstrcpy(backing_filename, sizeof(backing_filename),
|
766 | df2dbb4a | Stefan Hajnoczi | bs->backing_file); |
767 | df2dbb4a | Stefan Hajnoczi | } else {
|
768 | df2dbb4a | Stefan Hajnoczi | path_combine(backing_filename, sizeof(backing_filename),
|
769 | df2dbb4a | Stefan Hajnoczi | filename, bs->backing_file); |
770 | df2dbb4a | Stefan Hajnoczi | } |
771 | df2dbb4a | Stefan Hajnoczi | |
772 | df2dbb4a | Stefan Hajnoczi | if (bs->backing_format[0] != '\0') { |
773 | b6ce07aa | Kevin Wolf | back_drv = bdrv_find_format(bs->backing_format); |
774 | df2dbb4a | Stefan Hajnoczi | } |
775 | b6ce07aa | Kevin Wolf | |
776 | b6ce07aa | Kevin Wolf | /* backing files always opened read-only */
|
777 | b6ce07aa | Kevin Wolf | back_flags = |
778 | b6ce07aa | Kevin Wolf | flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); |
779 | b6ce07aa | Kevin Wolf | |
780 | b6ce07aa | Kevin Wolf | ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv); |
781 | b6ce07aa | Kevin Wolf | if (ret < 0) { |
782 | b6ce07aa | Kevin Wolf | bdrv_close(bs); |
783 | b6ce07aa | Kevin Wolf | return ret;
|
784 | b6ce07aa | Kevin Wolf | } |
785 | b6ce07aa | Kevin Wolf | if (bs->is_temporary) {
|
786 | b6ce07aa | Kevin Wolf | bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR); |
787 | b6ce07aa | Kevin Wolf | } else {
|
788 | b6ce07aa | Kevin Wolf | /* base image inherits from "parent" */
|
789 | b6ce07aa | Kevin Wolf | bs->backing_hd->keep_read_only = bs->keep_read_only; |
790 | b6ce07aa | Kevin Wolf | } |
791 | b6ce07aa | Kevin Wolf | } |
792 | b6ce07aa | Kevin Wolf | |
793 | b6ce07aa | Kevin Wolf | if (!bdrv_key_required(bs)) {
|
794 | 7d4b4ba5 | Markus Armbruster | bdrv_dev_change_media_cb(bs, true);
|
795 | b6ce07aa | Kevin Wolf | } |
796 | b6ce07aa | Kevin Wolf | |
797 | 98f90dba | Zhi Yong Wu | /* throttling disk I/O limits */
|
798 | 98f90dba | Zhi Yong Wu | if (bs->io_limits_enabled) {
|
799 | 98f90dba | Zhi Yong Wu | bdrv_io_limits_enable(bs); |
800 | 98f90dba | Zhi Yong Wu | } |
801 | 98f90dba | Zhi Yong Wu | |
802 | b6ce07aa | Kevin Wolf | return 0; |
803 | b6ce07aa | Kevin Wolf | |
804 | b6ce07aa | Kevin Wolf | unlink_and_fail:
|
805 | b6ce07aa | Kevin Wolf | if (bs->is_temporary) {
|
806 | b6ce07aa | Kevin Wolf | unlink(filename); |
807 | b6ce07aa | Kevin Wolf | } |
808 | b6ce07aa | Kevin Wolf | return ret;
|
809 | b6ce07aa | Kevin Wolf | } |
810 | b6ce07aa | Kevin Wolf | |
811 | fc01f7e7 | bellard | void bdrv_close(BlockDriverState *bs)
|
812 | fc01f7e7 | bellard | { |
813 | 19cb3738 | bellard | if (bs->drv) {
|
814 | f9092b10 | Markus Armbruster | if (bs == bs_snapshots) {
|
815 | f9092b10 | Markus Armbruster | bs_snapshots = NULL;
|
816 | f9092b10 | Markus Armbruster | } |
817 | 557df6ac | Stefan Hajnoczi | if (bs->backing_hd) {
|
818 | ea2384d3 | bellard | bdrv_delete(bs->backing_hd); |
819 | 557df6ac | Stefan Hajnoczi | bs->backing_hd = NULL;
|
820 | 557df6ac | Stefan Hajnoczi | } |
821 | ea2384d3 | bellard | bs->drv->bdrv_close(bs); |
822 | 7267c094 | Anthony Liguori | g_free(bs->opaque); |
823 | ea2384d3 | bellard | #ifdef _WIN32
|
824 | ea2384d3 | bellard | if (bs->is_temporary) {
|
825 | ea2384d3 | bellard | unlink(bs->filename); |
826 | ea2384d3 | bellard | } |
827 | 67b915a5 | bellard | #endif
|
828 | ea2384d3 | bellard | bs->opaque = NULL;
|
829 | ea2384d3 | bellard | bs->drv = NULL;
|
830 | 53fec9d3 | Stefan Hajnoczi | bs->copy_on_read = 0;
|
831 | b338082b | bellard | |
832 | 66f82cee | Kevin Wolf | if (bs->file != NULL) { |
833 | 66f82cee | Kevin Wolf | bdrv_close(bs->file); |
834 | 66f82cee | Kevin Wolf | } |
835 | 66f82cee | Kevin Wolf | |
836 | 7d4b4ba5 | Markus Armbruster | bdrv_dev_change_media_cb(bs, false);
|
837 | b338082b | bellard | } |
838 | 98f90dba | Zhi Yong Wu | |
839 | 98f90dba | Zhi Yong Wu | /*throttling disk I/O limits*/
|
840 | 98f90dba | Zhi Yong Wu | if (bs->io_limits_enabled) {
|
841 | 98f90dba | Zhi Yong Wu | bdrv_io_limits_disable(bs); |
842 | 98f90dba | Zhi Yong Wu | } |
843 | b338082b | bellard | } |
844 | b338082b | bellard | |
845 | 2bc93fed | MORITA Kazutaka | void bdrv_close_all(void) |
846 | 2bc93fed | MORITA Kazutaka | { |
847 | 2bc93fed | MORITA Kazutaka | BlockDriverState *bs; |
848 | 2bc93fed | MORITA Kazutaka | |
849 | 2bc93fed | MORITA Kazutaka | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
850 | 2bc93fed | MORITA Kazutaka | bdrv_close(bs); |
851 | 2bc93fed | MORITA Kazutaka | } |
852 | 2bc93fed | MORITA Kazutaka | } |
853 | 2bc93fed | MORITA Kazutaka | |
854 | 922453bc | Stefan Hajnoczi | /*
|
855 | 922453bc | Stefan Hajnoczi | * Wait for pending requests to complete across all BlockDriverStates
|
856 | 922453bc | Stefan Hajnoczi | *
|
857 | 922453bc | Stefan Hajnoczi | * This function does not flush data to disk, use bdrv_flush_all() for that
|
858 | 922453bc | Stefan Hajnoczi | * after calling this function.
|
859 | 922453bc | Stefan Hajnoczi | */
|
860 | 922453bc | Stefan Hajnoczi | void bdrv_drain_all(void) |
861 | 922453bc | Stefan Hajnoczi | { |
862 | 922453bc | Stefan Hajnoczi | BlockDriverState *bs; |
863 | 922453bc | Stefan Hajnoczi | |
864 | 922453bc | Stefan Hajnoczi | qemu_aio_flush(); |
865 | 922453bc | Stefan Hajnoczi | |
866 | 922453bc | Stefan Hajnoczi | /* If requests are still pending there is a bug somewhere */
|
867 | 922453bc | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
868 | 922453bc | Stefan Hajnoczi | assert(QLIST_EMPTY(&bs->tracked_requests)); |
869 | 922453bc | Stefan Hajnoczi | assert(qemu_co_queue_empty(&bs->throttled_reqs)); |
870 | 922453bc | Stefan Hajnoczi | } |
871 | 922453bc | Stefan Hajnoczi | } |
872 | 922453bc | Stefan Hajnoczi | |
873 | d22b2f41 | Ryan Harper | /* make a BlockDriverState anonymous by removing from bdrv_state list.
|
874 | d22b2f41 | Ryan Harper | Also, NULL terminate the device_name to prevent double remove */
|
875 | d22b2f41 | Ryan Harper | void bdrv_make_anon(BlockDriverState *bs)
|
876 | d22b2f41 | Ryan Harper | { |
877 | d22b2f41 | Ryan Harper | if (bs->device_name[0] != '\0') { |
878 | d22b2f41 | Ryan Harper | QTAILQ_REMOVE(&bdrv_states, bs, list); |
879 | d22b2f41 | Ryan Harper | } |
880 | d22b2f41 | Ryan Harper | bs->device_name[0] = '\0'; |
881 | d22b2f41 | Ryan Harper | } |
882 | d22b2f41 | Ryan Harper | |
883 | b338082b | bellard | void bdrv_delete(BlockDriverState *bs)
|
884 | b338082b | bellard | { |
885 | fa879d62 | Markus Armbruster | assert(!bs->dev); |
886 | 18846dee | Markus Armbruster | |
887 | 1b7bdbc1 | Stefan Hajnoczi | /* remove from list, if necessary */
|
888 | d22b2f41 | Ryan Harper | bdrv_make_anon(bs); |
889 | 34c6f050 | aurel32 | |
890 | b338082b | bellard | bdrv_close(bs); |
891 | 66f82cee | Kevin Wolf | if (bs->file != NULL) { |
892 | 66f82cee | Kevin Wolf | bdrv_delete(bs->file); |
893 | 66f82cee | Kevin Wolf | } |
894 | 66f82cee | Kevin Wolf | |
895 | f9092b10 | Markus Armbruster | assert(bs != bs_snapshots); |
896 | 7267c094 | Anthony Liguori | g_free(bs); |
897 | fc01f7e7 | bellard | } |
898 | fc01f7e7 | bellard | |
899 | fa879d62 | Markus Armbruster | int bdrv_attach_dev(BlockDriverState *bs, void *dev) |
900 | fa879d62 | Markus Armbruster | /* TODO change to DeviceState *dev when all users are qdevified */
|
901 | 18846dee | Markus Armbruster | { |
902 | fa879d62 | Markus Armbruster | if (bs->dev) {
|
903 | 18846dee | Markus Armbruster | return -EBUSY;
|
904 | 18846dee | Markus Armbruster | } |
905 | fa879d62 | Markus Armbruster | bs->dev = dev; |
906 | 28a7282a | Luiz Capitulino | bdrv_iostatus_reset(bs); |
907 | 18846dee | Markus Armbruster | return 0; |
908 | 18846dee | Markus Armbruster | } |
909 | 18846dee | Markus Armbruster | |
910 | fa879d62 | Markus Armbruster | /* TODO qdevified devices don't use this, remove when devices are qdevified */
|
911 | fa879d62 | Markus Armbruster | void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev) |
912 | 18846dee | Markus Armbruster | { |
913 | fa879d62 | Markus Armbruster | if (bdrv_attach_dev(bs, dev) < 0) { |
914 | fa879d62 | Markus Armbruster | abort(); |
915 | fa879d62 | Markus Armbruster | } |
916 | fa879d62 | Markus Armbruster | } |
917 | fa879d62 | Markus Armbruster | |
918 | fa879d62 | Markus Armbruster | void bdrv_detach_dev(BlockDriverState *bs, void *dev) |
919 | fa879d62 | Markus Armbruster | /* TODO change to DeviceState *dev when all users are qdevified */
|
920 | fa879d62 | Markus Armbruster | { |
921 | fa879d62 | Markus Armbruster | assert(bs->dev == dev); |
922 | fa879d62 | Markus Armbruster | bs->dev = NULL;
|
923 | 0e49de52 | Markus Armbruster | bs->dev_ops = NULL;
|
924 | 0e49de52 | Markus Armbruster | bs->dev_opaque = NULL;
|
925 | 29e05f20 | Markus Armbruster | bs->buffer_alignment = 512;
|
926 | 18846dee | Markus Armbruster | } |
927 | 18846dee | Markus Armbruster | |
928 | fa879d62 | Markus Armbruster | /* TODO change to return DeviceState * when all users are qdevified */
|
929 | fa879d62 | Markus Armbruster | void *bdrv_get_attached_dev(BlockDriverState *bs)
|
930 | 18846dee | Markus Armbruster | { |
931 | fa879d62 | Markus Armbruster | return bs->dev;
|
932 | 18846dee | Markus Armbruster | } |
933 | 18846dee | Markus Armbruster | |
934 | 0e49de52 | Markus Armbruster | void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, |
935 | 0e49de52 | Markus Armbruster | void *opaque)
|
936 | 0e49de52 | Markus Armbruster | { |
937 | 0e49de52 | Markus Armbruster | bs->dev_ops = ops; |
938 | 0e49de52 | Markus Armbruster | bs->dev_opaque = opaque; |
939 | 2c6942fa | Markus Armbruster | if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) {
|
940 | 2c6942fa | Markus Armbruster | bs_snapshots = NULL;
|
941 | 2c6942fa | Markus Armbruster | } |
942 | 0e49de52 | Markus Armbruster | } |
943 | 0e49de52 | Markus Armbruster | |
944 | 7d4b4ba5 | Markus Armbruster | static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load) |
945 | 0e49de52 | Markus Armbruster | { |
946 | 145feb17 | Markus Armbruster | if (bs->dev_ops && bs->dev_ops->change_media_cb) {
|
947 | 7d4b4ba5 | Markus Armbruster | bs->dev_ops->change_media_cb(bs->dev_opaque, load); |
948 | 145feb17 | Markus Armbruster | } |
949 | 145feb17 | Markus Armbruster | } |
950 | 145feb17 | Markus Armbruster | |
951 | 2c6942fa | Markus Armbruster | bool bdrv_dev_has_removable_media(BlockDriverState *bs)
|
952 | 2c6942fa | Markus Armbruster | { |
953 | 2c6942fa | Markus Armbruster | return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb);
|
954 | 2c6942fa | Markus Armbruster | } |
955 | 2c6942fa | Markus Armbruster | |
956 | 025ccaa7 | Paolo Bonzini | void bdrv_dev_eject_request(BlockDriverState *bs, bool force) |
957 | 025ccaa7 | Paolo Bonzini | { |
958 | 025ccaa7 | Paolo Bonzini | if (bs->dev_ops && bs->dev_ops->eject_request_cb) {
|
959 | 025ccaa7 | Paolo Bonzini | bs->dev_ops->eject_request_cb(bs->dev_opaque, force); |
960 | 025ccaa7 | Paolo Bonzini | } |
961 | 025ccaa7 | Paolo Bonzini | } |
962 | 025ccaa7 | Paolo Bonzini | |
963 | e4def80b | Markus Armbruster | bool bdrv_dev_is_tray_open(BlockDriverState *bs)
|
964 | e4def80b | Markus Armbruster | { |
965 | e4def80b | Markus Armbruster | if (bs->dev_ops && bs->dev_ops->is_tray_open) {
|
966 | e4def80b | Markus Armbruster | return bs->dev_ops->is_tray_open(bs->dev_opaque);
|
967 | e4def80b | Markus Armbruster | } |
968 | e4def80b | Markus Armbruster | return false; |
969 | e4def80b | Markus Armbruster | } |
970 | e4def80b | Markus Armbruster | |
971 | 145feb17 | Markus Armbruster | static void bdrv_dev_resize_cb(BlockDriverState *bs) |
972 | 145feb17 | Markus Armbruster | { |
973 | 145feb17 | Markus Armbruster | if (bs->dev_ops && bs->dev_ops->resize_cb) {
|
974 | 145feb17 | Markus Armbruster | bs->dev_ops->resize_cb(bs->dev_opaque); |
975 | 0e49de52 | Markus Armbruster | } |
976 | 0e49de52 | Markus Armbruster | } |
977 | 0e49de52 | Markus Armbruster | |
978 | f107639a | Markus Armbruster | bool bdrv_dev_is_medium_locked(BlockDriverState *bs)
|
979 | f107639a | Markus Armbruster | { |
980 | f107639a | Markus Armbruster | if (bs->dev_ops && bs->dev_ops->is_medium_locked) {
|
981 | f107639a | Markus Armbruster | return bs->dev_ops->is_medium_locked(bs->dev_opaque);
|
982 | f107639a | Markus Armbruster | } |
983 | f107639a | Markus Armbruster | return false; |
984 | f107639a | Markus Armbruster | } |
985 | f107639a | Markus Armbruster | |
986 | e97fc193 | aliguori | /*
|
987 | e97fc193 | aliguori | * Run consistency checks on an image
|
988 | e97fc193 | aliguori | *
|
989 | e076f338 | Kevin Wolf | * Returns 0 if the check could be completed (it doesn't mean that the image is
|
990 | a1c7273b | Stefan Weil | * free of errors) or -errno when an internal error occurred. The results of the
|
991 | e076f338 | Kevin Wolf | * check are stored in res.
|
992 | e97fc193 | aliguori | */
|
993 | e076f338 | Kevin Wolf | int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
|
994 | e97fc193 | aliguori | { |
995 | e97fc193 | aliguori | if (bs->drv->bdrv_check == NULL) { |
996 | e97fc193 | aliguori | return -ENOTSUP;
|
997 | e97fc193 | aliguori | } |
998 | e97fc193 | aliguori | |
999 | e076f338 | Kevin Wolf | memset(res, 0, sizeof(*res)); |
1000 | 9ac228e0 | Kevin Wolf | return bs->drv->bdrv_check(bs, res);
|
1001 | e97fc193 | aliguori | } |
1002 | e97fc193 | aliguori | |
1003 | 8a426614 | Kevin Wolf | #define COMMIT_BUF_SECTORS 2048 |
1004 | 8a426614 | Kevin Wolf | |
1005 | 33e3963e | bellard | /* commit COW file into the raw image */
|
1006 | 33e3963e | bellard | int bdrv_commit(BlockDriverState *bs)
|
1007 | 33e3963e | bellard | { |
1008 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
1009 | ee181196 | Kevin Wolf | BlockDriver *backing_drv; |
1010 | 8a426614 | Kevin Wolf | int64_t sector, total_sectors; |
1011 | 8a426614 | Kevin Wolf | int n, ro, open_flags;
|
1012 | 4dca4b63 | Naphtali Sprei | int ret = 0, rw_ret = 0; |
1013 | 8a426614 | Kevin Wolf | uint8_t *buf; |
1014 | 4dca4b63 | Naphtali Sprei | char filename[1024]; |
1015 | 4dca4b63 | Naphtali Sprei | BlockDriverState *bs_rw, *bs_ro; |
1016 | 33e3963e | bellard | |
1017 | 19cb3738 | bellard | if (!drv)
|
1018 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1019 | 4dca4b63 | Naphtali Sprei | |
1020 | 4dca4b63 | Naphtali Sprei | if (!bs->backing_hd) {
|
1021 | 4dca4b63 | Naphtali Sprei | return -ENOTSUP;
|
1022 | 33e3963e | bellard | } |
1023 | 33e3963e | bellard | |
1024 | 4dca4b63 | Naphtali Sprei | if (bs->backing_hd->keep_read_only) {
|
1025 | 4dca4b63 | Naphtali Sprei | return -EACCES;
|
1026 | 4dca4b63 | Naphtali Sprei | } |
1027 | ee181196 | Kevin Wolf | |
1028 | 2d3735d3 | Stefan Hajnoczi | if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) {
|
1029 | 2d3735d3 | Stefan Hajnoczi | return -EBUSY;
|
1030 | 2d3735d3 | Stefan Hajnoczi | } |
1031 | 2d3735d3 | Stefan Hajnoczi | |
1032 | ee181196 | Kevin Wolf | backing_drv = bs->backing_hd->drv; |
1033 | 4dca4b63 | Naphtali Sprei | ro = bs->backing_hd->read_only; |
1034 | 4dca4b63 | Naphtali Sprei | strncpy(filename, bs->backing_hd->filename, sizeof(filename));
|
1035 | 4dca4b63 | Naphtali Sprei | open_flags = bs->backing_hd->open_flags; |
1036 | 4dca4b63 | Naphtali Sprei | |
1037 | 4dca4b63 | Naphtali Sprei | if (ro) {
|
1038 | 4dca4b63 | Naphtali Sprei | /* re-open as RW */
|
1039 | 4dca4b63 | Naphtali Sprei | bdrv_delete(bs->backing_hd); |
1040 | 4dca4b63 | Naphtali Sprei | bs->backing_hd = NULL;
|
1041 | 4dca4b63 | Naphtali Sprei | bs_rw = bdrv_new("");
|
1042 | ee181196 | Kevin Wolf | rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, |
1043 | ee181196 | Kevin Wolf | backing_drv); |
1044 | 4dca4b63 | Naphtali Sprei | if (rw_ret < 0) { |
1045 | 4dca4b63 | Naphtali Sprei | bdrv_delete(bs_rw); |
1046 | 4dca4b63 | Naphtali Sprei | /* try to re-open read-only */
|
1047 | 4dca4b63 | Naphtali Sprei | bs_ro = bdrv_new("");
|
1048 | ee181196 | Kevin Wolf | ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, |
1049 | ee181196 | Kevin Wolf | backing_drv); |
1050 | 4dca4b63 | Naphtali Sprei | if (ret < 0) { |
1051 | 4dca4b63 | Naphtali Sprei | bdrv_delete(bs_ro); |
1052 | 4dca4b63 | Naphtali Sprei | /* drive not functional anymore */
|
1053 | 4dca4b63 | Naphtali Sprei | bs->drv = NULL;
|
1054 | 4dca4b63 | Naphtali Sprei | return ret;
|
1055 | 4dca4b63 | Naphtali Sprei | } |
1056 | 4dca4b63 | Naphtali Sprei | bs->backing_hd = bs_ro; |
1057 | 4dca4b63 | Naphtali Sprei | return rw_ret;
|
1058 | 4dca4b63 | Naphtali Sprei | } |
1059 | 4dca4b63 | Naphtali Sprei | bs->backing_hd = bs_rw; |
1060 | ea2384d3 | bellard | } |
1061 | 33e3963e | bellard | |
1062 | 6ea44308 | Jan Kiszka | total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; |
1063 | 7267c094 | Anthony Liguori | buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); |
1064 | 8a426614 | Kevin Wolf | |
1065 | 8a426614 | Kevin Wolf | for (sector = 0; sector < total_sectors; sector += n) { |
1066 | 05c4af54 | Stefan Hajnoczi | if (bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
|
1067 | 8a426614 | Kevin Wolf | |
1068 | 8a426614 | Kevin Wolf | if (bdrv_read(bs, sector, buf, n) != 0) { |
1069 | 8a426614 | Kevin Wolf | ret = -EIO; |
1070 | 8a426614 | Kevin Wolf | goto ro_cleanup;
|
1071 | 8a426614 | Kevin Wolf | } |
1072 | 8a426614 | Kevin Wolf | |
1073 | 8a426614 | Kevin Wolf | if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { |
1074 | 8a426614 | Kevin Wolf | ret = -EIO; |
1075 | 8a426614 | Kevin Wolf | goto ro_cleanup;
|
1076 | 8a426614 | Kevin Wolf | } |
1077 | ea2384d3 | bellard | } |
1078 | 33e3963e | bellard | } |
1079 | 95389c86 | bellard | |
1080 | 1d44952f | Christoph Hellwig | if (drv->bdrv_make_empty) {
|
1081 | 1d44952f | Christoph Hellwig | ret = drv->bdrv_make_empty(bs); |
1082 | 1d44952f | Christoph Hellwig | bdrv_flush(bs); |
1083 | 1d44952f | Christoph Hellwig | } |
1084 | 95389c86 | bellard | |
1085 | 3f5075ae | Christoph Hellwig | /*
|
1086 | 3f5075ae | Christoph Hellwig | * Make sure all data we wrote to the backing device is actually
|
1087 | 3f5075ae | Christoph Hellwig | * stable on disk.
|
1088 | 3f5075ae | Christoph Hellwig | */
|
1089 | 3f5075ae | Christoph Hellwig | if (bs->backing_hd)
|
1090 | 3f5075ae | Christoph Hellwig | bdrv_flush(bs->backing_hd); |
1091 | 4dca4b63 | Naphtali Sprei | |
1092 | 4dca4b63 | Naphtali Sprei | ro_cleanup:
|
1093 | 7267c094 | Anthony Liguori | g_free(buf); |
1094 | 4dca4b63 | Naphtali Sprei | |
1095 | 4dca4b63 | Naphtali Sprei | if (ro) {
|
1096 | 4dca4b63 | Naphtali Sprei | /* re-open as RO */
|
1097 | 4dca4b63 | Naphtali Sprei | bdrv_delete(bs->backing_hd); |
1098 | 4dca4b63 | Naphtali Sprei | bs->backing_hd = NULL;
|
1099 | 4dca4b63 | Naphtali Sprei | bs_ro = bdrv_new("");
|
1100 | ee181196 | Kevin Wolf | ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, |
1101 | ee181196 | Kevin Wolf | backing_drv); |
1102 | 4dca4b63 | Naphtali Sprei | if (ret < 0) { |
1103 | 4dca4b63 | Naphtali Sprei | bdrv_delete(bs_ro); |
1104 | 4dca4b63 | Naphtali Sprei | /* drive not functional anymore */
|
1105 | 4dca4b63 | Naphtali Sprei | bs->drv = NULL;
|
1106 | 4dca4b63 | Naphtali Sprei | return ret;
|
1107 | 4dca4b63 | Naphtali Sprei | } |
1108 | 4dca4b63 | Naphtali Sprei | bs->backing_hd = bs_ro; |
1109 | 4dca4b63 | Naphtali Sprei | bs->backing_hd->keep_read_only = 0;
|
1110 | 4dca4b63 | Naphtali Sprei | } |
1111 | 4dca4b63 | Naphtali Sprei | |
1112 | 1d44952f | Christoph Hellwig | return ret;
|
1113 | 33e3963e | bellard | } |
1114 | 33e3963e | bellard | |
1115 | 6ab4b5ab | Markus Armbruster | void bdrv_commit_all(void) |
1116 | 6ab4b5ab | Markus Armbruster | { |
1117 | 6ab4b5ab | Markus Armbruster | BlockDriverState *bs; |
1118 | 6ab4b5ab | Markus Armbruster | |
1119 | 6ab4b5ab | Markus Armbruster | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
1120 | 6ab4b5ab | Markus Armbruster | bdrv_commit(bs); |
1121 | 6ab4b5ab | Markus Armbruster | } |
1122 | 6ab4b5ab | Markus Armbruster | } |
1123 | 6ab4b5ab | Markus Armbruster | |
1124 | dbffbdcf | Stefan Hajnoczi | struct BdrvTrackedRequest {
|
1125 | dbffbdcf | Stefan Hajnoczi | BlockDriverState *bs; |
1126 | dbffbdcf | Stefan Hajnoczi | int64_t sector_num; |
1127 | dbffbdcf | Stefan Hajnoczi | int nb_sectors;
|
1128 | dbffbdcf | Stefan Hajnoczi | bool is_write;
|
1129 | dbffbdcf | Stefan Hajnoczi | QLIST_ENTRY(BdrvTrackedRequest) list; |
1130 | 5f8b6491 | Stefan Hajnoczi | Coroutine *co; /* owner, used for deadlock detection */
|
1131 | f4658285 | Stefan Hajnoczi | CoQueue wait_queue; /* coroutines blocked on this request */
|
1132 | dbffbdcf | Stefan Hajnoczi | }; |
1133 | dbffbdcf | Stefan Hajnoczi | |
1134 | dbffbdcf | Stefan Hajnoczi | /**
|
1135 | dbffbdcf | Stefan Hajnoczi | * Remove an active request from the tracked requests list
|
1136 | dbffbdcf | Stefan Hajnoczi | *
|
1137 | dbffbdcf | Stefan Hajnoczi | * This function should be called when a tracked request is completing.
|
1138 | dbffbdcf | Stefan Hajnoczi | */
|
1139 | dbffbdcf | Stefan Hajnoczi | static void tracked_request_end(BdrvTrackedRequest *req) |
1140 | dbffbdcf | Stefan Hajnoczi | { |
1141 | dbffbdcf | Stefan Hajnoczi | QLIST_REMOVE(req, list); |
1142 | f4658285 | Stefan Hajnoczi | qemu_co_queue_restart_all(&req->wait_queue); |
1143 | dbffbdcf | Stefan Hajnoczi | } |
1144 | dbffbdcf | Stefan Hajnoczi | |
1145 | dbffbdcf | Stefan Hajnoczi | /**
|
1146 | dbffbdcf | Stefan Hajnoczi | * Add an active request to the tracked requests list
|
1147 | dbffbdcf | Stefan Hajnoczi | */
|
1148 | dbffbdcf | Stefan Hajnoczi | static void tracked_request_begin(BdrvTrackedRequest *req, |
1149 | dbffbdcf | Stefan Hajnoczi | BlockDriverState *bs, |
1150 | dbffbdcf | Stefan Hajnoczi | int64_t sector_num, |
1151 | dbffbdcf | Stefan Hajnoczi | int nb_sectors, bool is_write) |
1152 | dbffbdcf | Stefan Hajnoczi | { |
1153 | dbffbdcf | Stefan Hajnoczi | *req = (BdrvTrackedRequest){ |
1154 | dbffbdcf | Stefan Hajnoczi | .bs = bs, |
1155 | dbffbdcf | Stefan Hajnoczi | .sector_num = sector_num, |
1156 | dbffbdcf | Stefan Hajnoczi | .nb_sectors = nb_sectors, |
1157 | dbffbdcf | Stefan Hajnoczi | .is_write = is_write, |
1158 | 5f8b6491 | Stefan Hajnoczi | .co = qemu_coroutine_self(), |
1159 | dbffbdcf | Stefan Hajnoczi | }; |
1160 | dbffbdcf | Stefan Hajnoczi | |
1161 | f4658285 | Stefan Hajnoczi | qemu_co_queue_init(&req->wait_queue); |
1162 | f4658285 | Stefan Hajnoczi | |
1163 | dbffbdcf | Stefan Hajnoczi | QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); |
1164 | dbffbdcf | Stefan Hajnoczi | } |
1165 | dbffbdcf | Stefan Hajnoczi | |
1166 | d83947ac | Stefan Hajnoczi | /**
|
1167 | d83947ac | Stefan Hajnoczi | * Round a region to cluster boundaries
|
1168 | d83947ac | Stefan Hajnoczi | */
|
1169 | d83947ac | Stefan Hajnoczi | static void round_to_clusters(BlockDriverState *bs, |
1170 | d83947ac | Stefan Hajnoczi | int64_t sector_num, int nb_sectors,
|
1171 | d83947ac | Stefan Hajnoczi | int64_t *cluster_sector_num, |
1172 | d83947ac | Stefan Hajnoczi | int *cluster_nb_sectors)
|
1173 | d83947ac | Stefan Hajnoczi | { |
1174 | d83947ac | Stefan Hajnoczi | BlockDriverInfo bdi; |
1175 | d83947ac | Stefan Hajnoczi | |
1176 | d83947ac | Stefan Hajnoczi | if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { |
1177 | d83947ac | Stefan Hajnoczi | *cluster_sector_num = sector_num; |
1178 | d83947ac | Stefan Hajnoczi | *cluster_nb_sectors = nb_sectors; |
1179 | d83947ac | Stefan Hajnoczi | } else {
|
1180 | d83947ac | Stefan Hajnoczi | int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE; |
1181 | d83947ac | Stefan Hajnoczi | *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c); |
1182 | d83947ac | Stefan Hajnoczi | *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num + |
1183 | d83947ac | Stefan Hajnoczi | nb_sectors, c); |
1184 | d83947ac | Stefan Hajnoczi | } |
1185 | d83947ac | Stefan Hajnoczi | } |
1186 | d83947ac | Stefan Hajnoczi | |
1187 | f4658285 | Stefan Hajnoczi | static bool tracked_request_overlaps(BdrvTrackedRequest *req, |
1188 | f4658285 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors) {
|
1189 | d83947ac | Stefan Hajnoczi | /* aaaa bbbb */
|
1190 | d83947ac | Stefan Hajnoczi | if (sector_num >= req->sector_num + req->nb_sectors) {
|
1191 | d83947ac | Stefan Hajnoczi | return false; |
1192 | d83947ac | Stefan Hajnoczi | } |
1193 | d83947ac | Stefan Hajnoczi | /* bbbb aaaa */
|
1194 | d83947ac | Stefan Hajnoczi | if (req->sector_num >= sector_num + nb_sectors) {
|
1195 | d83947ac | Stefan Hajnoczi | return false; |
1196 | d83947ac | Stefan Hajnoczi | } |
1197 | d83947ac | Stefan Hajnoczi | return true; |
1198 | f4658285 | Stefan Hajnoczi | } |
1199 | f4658285 | Stefan Hajnoczi | |
1200 | f4658285 | Stefan Hajnoczi | static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs, |
1201 | f4658285 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors)
|
1202 | f4658285 | Stefan Hajnoczi | { |
1203 | f4658285 | Stefan Hajnoczi | BdrvTrackedRequest *req; |
1204 | d83947ac | Stefan Hajnoczi | int64_t cluster_sector_num; |
1205 | d83947ac | Stefan Hajnoczi | int cluster_nb_sectors;
|
1206 | f4658285 | Stefan Hajnoczi | bool retry;
|
1207 | f4658285 | Stefan Hajnoczi | |
1208 | d83947ac | Stefan Hajnoczi | /* If we touch the same cluster it counts as an overlap. This guarantees
|
1209 | d83947ac | Stefan Hajnoczi | * that allocating writes will be serialized and not race with each other
|
1210 | d83947ac | Stefan Hajnoczi | * for the same cluster. For example, in copy-on-read it ensures that the
|
1211 | d83947ac | Stefan Hajnoczi | * CoR read and write operations are atomic and guest writes cannot
|
1212 | d83947ac | Stefan Hajnoczi | * interleave between them.
|
1213 | d83947ac | Stefan Hajnoczi | */
|
1214 | d83947ac | Stefan Hajnoczi | round_to_clusters(bs, sector_num, nb_sectors, |
1215 | d83947ac | Stefan Hajnoczi | &cluster_sector_num, &cluster_nb_sectors); |
1216 | d83947ac | Stefan Hajnoczi | |
1217 | f4658285 | Stefan Hajnoczi | do {
|
1218 | f4658285 | Stefan Hajnoczi | retry = false;
|
1219 | f4658285 | Stefan Hajnoczi | QLIST_FOREACH(req, &bs->tracked_requests, list) { |
1220 | d83947ac | Stefan Hajnoczi | if (tracked_request_overlaps(req, cluster_sector_num,
|
1221 | d83947ac | Stefan Hajnoczi | cluster_nb_sectors)) { |
1222 | 5f8b6491 | Stefan Hajnoczi | /* Hitting this means there was a reentrant request, for
|
1223 | 5f8b6491 | Stefan Hajnoczi | * example, a block driver issuing nested requests. This must
|
1224 | 5f8b6491 | Stefan Hajnoczi | * never happen since it means deadlock.
|
1225 | 5f8b6491 | Stefan Hajnoczi | */
|
1226 | 5f8b6491 | Stefan Hajnoczi | assert(qemu_coroutine_self() != req->co); |
1227 | 5f8b6491 | Stefan Hajnoczi | |
1228 | f4658285 | Stefan Hajnoczi | qemu_co_queue_wait(&req->wait_queue); |
1229 | f4658285 | Stefan Hajnoczi | retry = true;
|
1230 | f4658285 | Stefan Hajnoczi | break;
|
1231 | f4658285 | Stefan Hajnoczi | } |
1232 | f4658285 | Stefan Hajnoczi | } |
1233 | f4658285 | Stefan Hajnoczi | } while (retry);
|
1234 | f4658285 | Stefan Hajnoczi | } |
1235 | f4658285 | Stefan Hajnoczi | |
1236 | 756e6736 | Kevin Wolf | /*
|
1237 | 756e6736 | Kevin Wolf | * Return values:
|
1238 | 756e6736 | Kevin Wolf | * 0 - success
|
1239 | 756e6736 | Kevin Wolf | * -EINVAL - backing format specified, but no file
|
1240 | 756e6736 | Kevin Wolf | * -ENOSPC - can't update the backing file because no space is left in the
|
1241 | 756e6736 | Kevin Wolf | * image file header
|
1242 | 756e6736 | Kevin Wolf | * -ENOTSUP - format driver doesn't support changing the backing file
|
1243 | 756e6736 | Kevin Wolf | */
|
1244 | 756e6736 | Kevin Wolf | int bdrv_change_backing_file(BlockDriverState *bs,
|
1245 | 756e6736 | Kevin Wolf | const char *backing_file, const char *backing_fmt) |
1246 | 756e6736 | Kevin Wolf | { |
1247 | 756e6736 | Kevin Wolf | BlockDriver *drv = bs->drv; |
1248 | 756e6736 | Kevin Wolf | |
1249 | 756e6736 | Kevin Wolf | if (drv->bdrv_change_backing_file != NULL) { |
1250 | 756e6736 | Kevin Wolf | return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
|
1251 | 756e6736 | Kevin Wolf | } else {
|
1252 | 756e6736 | Kevin Wolf | return -ENOTSUP;
|
1253 | 756e6736 | Kevin Wolf | } |
1254 | 756e6736 | Kevin Wolf | } |
1255 | 756e6736 | Kevin Wolf | |
1256 | 71d0770c | aliguori | static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, |
1257 | 71d0770c | aliguori | size_t size) |
1258 | 71d0770c | aliguori | { |
1259 | 71d0770c | aliguori | int64_t len; |
1260 | 71d0770c | aliguori | |
1261 | 71d0770c | aliguori | if (!bdrv_is_inserted(bs))
|
1262 | 71d0770c | aliguori | return -ENOMEDIUM;
|
1263 | 71d0770c | aliguori | |
1264 | 71d0770c | aliguori | if (bs->growable)
|
1265 | 71d0770c | aliguori | return 0; |
1266 | 71d0770c | aliguori | |
1267 | 71d0770c | aliguori | len = bdrv_getlength(bs); |
1268 | 71d0770c | aliguori | |
1269 | fbb7b4e0 | Kevin Wolf | if (offset < 0) |
1270 | fbb7b4e0 | Kevin Wolf | return -EIO;
|
1271 | fbb7b4e0 | Kevin Wolf | |
1272 | fbb7b4e0 | Kevin Wolf | if ((offset > len) || (len - offset < size))
|
1273 | 71d0770c | aliguori | return -EIO;
|
1274 | 71d0770c | aliguori | |
1275 | 71d0770c | aliguori | return 0; |
1276 | 71d0770c | aliguori | } |
1277 | 71d0770c | aliguori | |
1278 | 71d0770c | aliguori | static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, |
1279 | 71d0770c | aliguori | int nb_sectors)
|
1280 | 71d0770c | aliguori | { |
1281 | eb5a3165 | Jes Sorensen | return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
|
1282 | eb5a3165 | Jes Sorensen | nb_sectors * BDRV_SECTOR_SIZE); |
1283 | 71d0770c | aliguori | } |
1284 | 71d0770c | aliguori | |
1285 | 1c9805a3 | Stefan Hajnoczi | typedef struct RwCo { |
1286 | 1c9805a3 | Stefan Hajnoczi | BlockDriverState *bs; |
1287 | 1c9805a3 | Stefan Hajnoczi | int64_t sector_num; |
1288 | 1c9805a3 | Stefan Hajnoczi | int nb_sectors;
|
1289 | 1c9805a3 | Stefan Hajnoczi | QEMUIOVector *qiov; |
1290 | 1c9805a3 | Stefan Hajnoczi | bool is_write;
|
1291 | 1c9805a3 | Stefan Hajnoczi | int ret;
|
1292 | 1c9805a3 | Stefan Hajnoczi | } RwCo; |
1293 | 1c9805a3 | Stefan Hajnoczi | |
1294 | 1c9805a3 | Stefan Hajnoczi | static void coroutine_fn bdrv_rw_co_entry(void *opaque) |
1295 | fc01f7e7 | bellard | { |
1296 | 1c9805a3 | Stefan Hajnoczi | RwCo *rwco = opaque; |
1297 | ea2384d3 | bellard | |
1298 | 1c9805a3 | Stefan Hajnoczi | if (!rwco->is_write) {
|
1299 | 1c9805a3 | Stefan Hajnoczi | rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num, |
1300 | 470c0504 | Stefan Hajnoczi | rwco->nb_sectors, rwco->qiov, 0);
|
1301 | 1c9805a3 | Stefan Hajnoczi | } else {
|
1302 | 1c9805a3 | Stefan Hajnoczi | rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num, |
1303 | 1c9805a3 | Stefan Hajnoczi | rwco->nb_sectors, rwco->qiov); |
1304 | 1c9805a3 | Stefan Hajnoczi | } |
1305 | 1c9805a3 | Stefan Hajnoczi | } |
1306 | e7a8a783 | Kevin Wolf | |
1307 | 1c9805a3 | Stefan Hajnoczi | /*
|
1308 | 1c9805a3 | Stefan Hajnoczi | * Process a synchronous request using coroutines
|
1309 | 1c9805a3 | Stefan Hajnoczi | */
|
1310 | 1c9805a3 | Stefan Hajnoczi | static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, |
1311 | 1c9805a3 | Stefan Hajnoczi | int nb_sectors, bool is_write) |
1312 | 1c9805a3 | Stefan Hajnoczi | { |
1313 | 1c9805a3 | Stefan Hajnoczi | QEMUIOVector qiov; |
1314 | 1c9805a3 | Stefan Hajnoczi | struct iovec iov = {
|
1315 | 1c9805a3 | Stefan Hajnoczi | .iov_base = (void *)buf,
|
1316 | 1c9805a3 | Stefan Hajnoczi | .iov_len = nb_sectors * BDRV_SECTOR_SIZE, |
1317 | 1c9805a3 | Stefan Hajnoczi | }; |
1318 | 1c9805a3 | Stefan Hajnoczi | Coroutine *co; |
1319 | 1c9805a3 | Stefan Hajnoczi | RwCo rwco = { |
1320 | 1c9805a3 | Stefan Hajnoczi | .bs = bs, |
1321 | 1c9805a3 | Stefan Hajnoczi | .sector_num = sector_num, |
1322 | 1c9805a3 | Stefan Hajnoczi | .nb_sectors = nb_sectors, |
1323 | 1c9805a3 | Stefan Hajnoczi | .qiov = &qiov, |
1324 | 1c9805a3 | Stefan Hajnoczi | .is_write = is_write, |
1325 | 1c9805a3 | Stefan Hajnoczi | .ret = NOT_DONE, |
1326 | 1c9805a3 | Stefan Hajnoczi | }; |
1327 | e7a8a783 | Kevin Wolf | |
1328 | 1c9805a3 | Stefan Hajnoczi | qemu_iovec_init_external(&qiov, &iov, 1);
|
1329 | e7a8a783 | Kevin Wolf | |
1330 | 1c9805a3 | Stefan Hajnoczi | if (qemu_in_coroutine()) {
|
1331 | 1c9805a3 | Stefan Hajnoczi | /* Fast-path if already in coroutine context */
|
1332 | 1c9805a3 | Stefan Hajnoczi | bdrv_rw_co_entry(&rwco); |
1333 | 1c9805a3 | Stefan Hajnoczi | } else {
|
1334 | 1c9805a3 | Stefan Hajnoczi | co = qemu_coroutine_create(bdrv_rw_co_entry); |
1335 | 1c9805a3 | Stefan Hajnoczi | qemu_coroutine_enter(co, &rwco); |
1336 | 1c9805a3 | Stefan Hajnoczi | while (rwco.ret == NOT_DONE) {
|
1337 | 1c9805a3 | Stefan Hajnoczi | qemu_aio_wait(); |
1338 | 1c9805a3 | Stefan Hajnoczi | } |
1339 | 1c9805a3 | Stefan Hajnoczi | } |
1340 | 1c9805a3 | Stefan Hajnoczi | return rwco.ret;
|
1341 | 1c9805a3 | Stefan Hajnoczi | } |
1342 | b338082b | bellard | |
1343 | 1c9805a3 | Stefan Hajnoczi | /* return < 0 if error. See bdrv_write() for the return codes */
|
1344 | 1c9805a3 | Stefan Hajnoczi | int bdrv_read(BlockDriverState *bs, int64_t sector_num,
|
1345 | 1c9805a3 | Stefan Hajnoczi | uint8_t *buf, int nb_sectors)
|
1346 | 1c9805a3 | Stefan Hajnoczi | { |
1347 | 1c9805a3 | Stefan Hajnoczi | return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false); |
1348 | fc01f7e7 | bellard | } |
1349 | fc01f7e7 | bellard | |
1350 | 7cd1e32a | lirans@il.ibm.com | static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, |
1351 | a55eb92c | Jan Kiszka | int nb_sectors, int dirty) |
1352 | 7cd1e32a | lirans@il.ibm.com | { |
1353 | 7cd1e32a | lirans@il.ibm.com | int64_t start, end; |
1354 | c6d22830 | Jan Kiszka | unsigned long val, idx, bit; |
1355 | a55eb92c | Jan Kiszka | |
1356 | 6ea44308 | Jan Kiszka | start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; |
1357 | c6d22830 | Jan Kiszka | end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
|
1358 | a55eb92c | Jan Kiszka | |
1359 | a55eb92c | Jan Kiszka | for (; start <= end; start++) {
|
1360 | c6d22830 | Jan Kiszka | idx = start / (sizeof(unsigned long) * 8); |
1361 | c6d22830 | Jan Kiszka | bit = start % (sizeof(unsigned long) * 8); |
1362 | c6d22830 | Jan Kiszka | val = bs->dirty_bitmap[idx]; |
1363 | c6d22830 | Jan Kiszka | if (dirty) {
|
1364 | 6d59fec1 | Marcelo Tosatti | if (!(val & (1UL << bit))) { |
1365 | aaa0eb75 | Liran Schour | bs->dirty_count++; |
1366 | 6d59fec1 | Marcelo Tosatti | val |= 1UL << bit;
|
1367 | aaa0eb75 | Liran Schour | } |
1368 | c6d22830 | Jan Kiszka | } else {
|
1369 | 6d59fec1 | Marcelo Tosatti | if (val & (1UL << bit)) { |
1370 | aaa0eb75 | Liran Schour | bs->dirty_count--; |
1371 | 6d59fec1 | Marcelo Tosatti | val &= ~(1UL << bit);
|
1372 | aaa0eb75 | Liran Schour | } |
1373 | c6d22830 | Jan Kiszka | } |
1374 | c6d22830 | Jan Kiszka | bs->dirty_bitmap[idx] = val; |
1375 | 7cd1e32a | lirans@il.ibm.com | } |
1376 | 7cd1e32a | lirans@il.ibm.com | } |
1377 | 7cd1e32a | lirans@il.ibm.com | |
1378 | 5fafdf24 | ths | /* Return < 0 if error. Important errors are:
|
1379 | 19cb3738 | bellard | -EIO generic I/O error (may happen for all errors)
|
1380 | 19cb3738 | bellard | -ENOMEDIUM No media inserted.
|
1381 | 19cb3738 | bellard | -EINVAL Invalid sector number or nb_sectors
|
1382 | 19cb3738 | bellard | -EACCES Trying to write a read-only device
|
1383 | 19cb3738 | bellard | */
|
1384 | 5fafdf24 | ths | int bdrv_write(BlockDriverState *bs, int64_t sector_num,
|
1385 | fc01f7e7 | bellard | const uint8_t *buf, int nb_sectors) |
1386 | fc01f7e7 | bellard | { |
1387 | 1c9805a3 | Stefan Hajnoczi | return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true); |
1388 | 83f64091 | bellard | } |
1389 | 83f64091 | bellard | |
1390 | eda578e5 | aliguori | int bdrv_pread(BlockDriverState *bs, int64_t offset,
|
1391 | eda578e5 | aliguori | void *buf, int count1) |
1392 | 83f64091 | bellard | { |
1393 | 6ea44308 | Jan Kiszka | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
1394 | 83f64091 | bellard | int len, nb_sectors, count;
|
1395 | 83f64091 | bellard | int64_t sector_num; |
1396 | 9a8c4cce | Kevin Wolf | int ret;
|
1397 | 83f64091 | bellard | |
1398 | 83f64091 | bellard | count = count1; |
1399 | 83f64091 | bellard | /* first read to align to sector start */
|
1400 | 6ea44308 | Jan Kiszka | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
|
1401 | 83f64091 | bellard | if (len > count)
|
1402 | 83f64091 | bellard | len = count; |
1403 | 6ea44308 | Jan Kiszka | sector_num = offset >> BDRV_SECTOR_BITS; |
1404 | 83f64091 | bellard | if (len > 0) { |
1405 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
1406 | 9a8c4cce | Kevin Wolf | return ret;
|
1407 | 6ea44308 | Jan Kiszka | memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
|
1408 | 83f64091 | bellard | count -= len; |
1409 | 83f64091 | bellard | if (count == 0) |
1410 | 83f64091 | bellard | return count1;
|
1411 | 83f64091 | bellard | sector_num++; |
1412 | 83f64091 | bellard | buf += len; |
1413 | 83f64091 | bellard | } |
1414 | 83f64091 | bellard | |
1415 | 83f64091 | bellard | /* read the sectors "in place" */
|
1416 | 6ea44308 | Jan Kiszka | nb_sectors = count >> BDRV_SECTOR_BITS; |
1417 | 83f64091 | bellard | if (nb_sectors > 0) { |
1418 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) |
1419 | 9a8c4cce | Kevin Wolf | return ret;
|
1420 | 83f64091 | bellard | sector_num += nb_sectors; |
1421 | 6ea44308 | Jan Kiszka | len = nb_sectors << BDRV_SECTOR_BITS; |
1422 | 83f64091 | bellard | buf += len; |
1423 | 83f64091 | bellard | count -= len; |
1424 | 83f64091 | bellard | } |
1425 | 83f64091 | bellard | |
1426 | 83f64091 | bellard | /* add data from the last sector */
|
1427 | 83f64091 | bellard | if (count > 0) { |
1428 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
1429 | 9a8c4cce | Kevin Wolf | return ret;
|
1430 | 83f64091 | bellard | memcpy(buf, tmp_buf, count); |
1431 | 83f64091 | bellard | } |
1432 | 83f64091 | bellard | return count1;
|
1433 | 83f64091 | bellard | } |
1434 | 83f64091 | bellard | |
1435 | eda578e5 | aliguori | int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
|
1436 | eda578e5 | aliguori | const void *buf, int count1) |
1437 | 83f64091 | bellard | { |
1438 | 6ea44308 | Jan Kiszka | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
1439 | 83f64091 | bellard | int len, nb_sectors, count;
|
1440 | 83f64091 | bellard | int64_t sector_num; |
1441 | 9a8c4cce | Kevin Wolf | int ret;
|
1442 | 83f64091 | bellard | |
1443 | 83f64091 | bellard | count = count1; |
1444 | 83f64091 | bellard | /* first write to align to sector start */
|
1445 | 6ea44308 | Jan Kiszka | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
|
1446 | 83f64091 | bellard | if (len > count)
|
1447 | 83f64091 | bellard | len = count; |
1448 | 6ea44308 | Jan Kiszka | sector_num = offset >> BDRV_SECTOR_BITS; |
1449 | 83f64091 | bellard | if (len > 0) { |
1450 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
1451 | 9a8c4cce | Kevin Wolf | return ret;
|
1452 | 6ea44308 | Jan Kiszka | memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
|
1453 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
1454 | 9a8c4cce | Kevin Wolf | return ret;
|
1455 | 83f64091 | bellard | count -= len; |
1456 | 83f64091 | bellard | if (count == 0) |
1457 | 83f64091 | bellard | return count1;
|
1458 | 83f64091 | bellard | sector_num++; |
1459 | 83f64091 | bellard | buf += len; |
1460 | 83f64091 | bellard | } |
1461 | 83f64091 | bellard | |
1462 | 83f64091 | bellard | /* write the sectors "in place" */
|
1463 | 6ea44308 | Jan Kiszka | nb_sectors = count >> BDRV_SECTOR_BITS; |
1464 | 83f64091 | bellard | if (nb_sectors > 0) { |
1465 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) |
1466 | 9a8c4cce | Kevin Wolf | return ret;
|
1467 | 83f64091 | bellard | sector_num += nb_sectors; |
1468 | 6ea44308 | Jan Kiszka | len = nb_sectors << BDRV_SECTOR_BITS; |
1469 | 83f64091 | bellard | buf += len; |
1470 | 83f64091 | bellard | count -= len; |
1471 | 83f64091 | bellard | } |
1472 | 83f64091 | bellard | |
1473 | 83f64091 | bellard | /* add data from the last sector */
|
1474 | 83f64091 | bellard | if (count > 0) { |
1475 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
1476 | 9a8c4cce | Kevin Wolf | return ret;
|
1477 | 83f64091 | bellard | memcpy(tmp_buf, buf, count); |
1478 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
1479 | 9a8c4cce | Kevin Wolf | return ret;
|
1480 | 83f64091 | bellard | } |
1481 | 83f64091 | bellard | return count1;
|
1482 | 83f64091 | bellard | } |
1483 | 83f64091 | bellard | |
1484 | f08145fe | Kevin Wolf | /*
|
1485 | f08145fe | Kevin Wolf | * Writes to the file and ensures that no writes are reordered across this
|
1486 | f08145fe | Kevin Wolf | * request (acts as a barrier)
|
1487 | f08145fe | Kevin Wolf | *
|
1488 | f08145fe | Kevin Wolf | * Returns 0 on success, -errno in error cases.
|
1489 | f08145fe | Kevin Wolf | */
|
1490 | f08145fe | Kevin Wolf | int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
|
1491 | f08145fe | Kevin Wolf | const void *buf, int count) |
1492 | f08145fe | Kevin Wolf | { |
1493 | f08145fe | Kevin Wolf | int ret;
|
1494 | f08145fe | Kevin Wolf | |
1495 | f08145fe | Kevin Wolf | ret = bdrv_pwrite(bs, offset, buf, count); |
1496 | f08145fe | Kevin Wolf | if (ret < 0) { |
1497 | f08145fe | Kevin Wolf | return ret;
|
1498 | f08145fe | Kevin Wolf | } |
1499 | f08145fe | Kevin Wolf | |
1500 | 92196b2f | Stefan Hajnoczi | /* No flush needed for cache modes that use O_DSYNC */
|
1501 | 92196b2f | Stefan Hajnoczi | if ((bs->open_flags & BDRV_O_CACHE_WB) != 0) { |
1502 | f08145fe | Kevin Wolf | bdrv_flush(bs); |
1503 | f08145fe | Kevin Wolf | } |
1504 | f08145fe | Kevin Wolf | |
1505 | f08145fe | Kevin Wolf | return 0; |
1506 | f08145fe | Kevin Wolf | } |
1507 | f08145fe | Kevin Wolf | |
1508 | 470c0504 | Stefan Hajnoczi | static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, |
1509 | ab185921 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
|
1510 | ab185921 | Stefan Hajnoczi | { |
1511 | ab185921 | Stefan Hajnoczi | /* Perform I/O through a temporary buffer so that users who scribble over
|
1512 | ab185921 | Stefan Hajnoczi | * their read buffer while the operation is in progress do not end up
|
1513 | ab185921 | Stefan Hajnoczi | * modifying the image file. This is critical for zero-copy guest I/O
|
1514 | ab185921 | Stefan Hajnoczi | * where anything might happen inside guest memory.
|
1515 | ab185921 | Stefan Hajnoczi | */
|
1516 | ab185921 | Stefan Hajnoczi | void *bounce_buffer;
|
1517 | ab185921 | Stefan Hajnoczi | |
1518 | ab185921 | Stefan Hajnoczi | struct iovec iov;
|
1519 | ab185921 | Stefan Hajnoczi | QEMUIOVector bounce_qiov; |
1520 | ab185921 | Stefan Hajnoczi | int64_t cluster_sector_num; |
1521 | ab185921 | Stefan Hajnoczi | int cluster_nb_sectors;
|
1522 | ab185921 | Stefan Hajnoczi | size_t skip_bytes; |
1523 | ab185921 | Stefan Hajnoczi | int ret;
|
1524 | ab185921 | Stefan Hajnoczi | |
1525 | ab185921 | Stefan Hajnoczi | /* Cover entire cluster so no additional backing file I/O is required when
|
1526 | ab185921 | Stefan Hajnoczi | * allocating cluster in the image file.
|
1527 | ab185921 | Stefan Hajnoczi | */
|
1528 | ab185921 | Stefan Hajnoczi | round_to_clusters(bs, sector_num, nb_sectors, |
1529 | ab185921 | Stefan Hajnoczi | &cluster_sector_num, &cluster_nb_sectors); |
1530 | ab185921 | Stefan Hajnoczi | |
1531 | 470c0504 | Stefan Hajnoczi | trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, |
1532 | 470c0504 | Stefan Hajnoczi | cluster_sector_num, cluster_nb_sectors); |
1533 | ab185921 | Stefan Hajnoczi | |
1534 | ab185921 | Stefan Hajnoczi | iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE; |
1535 | ab185921 | Stefan Hajnoczi | iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len); |
1536 | ab185921 | Stefan Hajnoczi | qemu_iovec_init_external(&bounce_qiov, &iov, 1);
|
1537 | ab185921 | Stefan Hajnoczi | |
1538 | ab185921 | Stefan Hajnoczi | ret = bs->drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, |
1539 | ab185921 | Stefan Hajnoczi | &bounce_qiov); |
1540 | ab185921 | Stefan Hajnoczi | if (ret < 0) { |
1541 | ab185921 | Stefan Hajnoczi | goto err;
|
1542 | ab185921 | Stefan Hajnoczi | } |
1543 | ab185921 | Stefan Hajnoczi | |
1544 | ab185921 | Stefan Hajnoczi | ret = bs->drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, |
1545 | ab185921 | Stefan Hajnoczi | &bounce_qiov); |
1546 | ab185921 | Stefan Hajnoczi | if (ret < 0) { |
1547 | ab185921 | Stefan Hajnoczi | /* It might be okay to ignore write errors for guest requests. If this
|
1548 | ab185921 | Stefan Hajnoczi | * is a deliberate copy-on-read then we don't want to ignore the error.
|
1549 | ab185921 | Stefan Hajnoczi | * Simply report it in all cases.
|
1550 | ab185921 | Stefan Hajnoczi | */
|
1551 | ab185921 | Stefan Hajnoczi | goto err;
|
1552 | ab185921 | Stefan Hajnoczi | } |
1553 | ab185921 | Stefan Hajnoczi | |
1554 | ab185921 | Stefan Hajnoczi | skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE; |
1555 | ab185921 | Stefan Hajnoczi | qemu_iovec_from_buffer(qiov, bounce_buffer + skip_bytes, |
1556 | ab185921 | Stefan Hajnoczi | nb_sectors * BDRV_SECTOR_SIZE); |
1557 | ab185921 | Stefan Hajnoczi | |
1558 | ab185921 | Stefan Hajnoczi | err:
|
1559 | ab185921 | Stefan Hajnoczi | qemu_vfree(bounce_buffer); |
1560 | ab185921 | Stefan Hajnoczi | return ret;
|
1561 | ab185921 | Stefan Hajnoczi | } |
1562 | ab185921 | Stefan Hajnoczi | |
1563 | c5fbe571 | Stefan Hajnoczi | /*
|
1564 | c5fbe571 | Stefan Hajnoczi | * Handle a read request in coroutine context
|
1565 | c5fbe571 | Stefan Hajnoczi | */
|
1566 | c5fbe571 | Stefan Hajnoczi | static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, |
1567 | 470c0504 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
|
1568 | 470c0504 | Stefan Hajnoczi | BdrvRequestFlags flags) |
1569 | da1fa91d | Kevin Wolf | { |
1570 | da1fa91d | Kevin Wolf | BlockDriver *drv = bs->drv; |
1571 | dbffbdcf | Stefan Hajnoczi | BdrvTrackedRequest req; |
1572 | dbffbdcf | Stefan Hajnoczi | int ret;
|
1573 | da1fa91d | Kevin Wolf | |
1574 | da1fa91d | Kevin Wolf | if (!drv) {
|
1575 | da1fa91d | Kevin Wolf | return -ENOMEDIUM;
|
1576 | da1fa91d | Kevin Wolf | } |
1577 | da1fa91d | Kevin Wolf | if (bdrv_check_request(bs, sector_num, nb_sectors)) {
|
1578 | da1fa91d | Kevin Wolf | return -EIO;
|
1579 | da1fa91d | Kevin Wolf | } |
1580 | da1fa91d | Kevin Wolf | |
1581 | 98f90dba | Zhi Yong Wu | /* throttling disk read I/O */
|
1582 | 98f90dba | Zhi Yong Wu | if (bs->io_limits_enabled) {
|
1583 | 98f90dba | Zhi Yong Wu | bdrv_io_limits_intercept(bs, false, nb_sectors);
|
1584 | 98f90dba | Zhi Yong Wu | } |
1585 | 98f90dba | Zhi Yong Wu | |
1586 | f4658285 | Stefan Hajnoczi | if (bs->copy_on_read) {
|
1587 | 470c0504 | Stefan Hajnoczi | flags |= BDRV_REQ_COPY_ON_READ; |
1588 | 470c0504 | Stefan Hajnoczi | } |
1589 | 470c0504 | Stefan Hajnoczi | if (flags & BDRV_REQ_COPY_ON_READ) {
|
1590 | 470c0504 | Stefan Hajnoczi | bs->copy_on_read_in_flight++; |
1591 | 470c0504 | Stefan Hajnoczi | } |
1592 | 470c0504 | Stefan Hajnoczi | |
1593 | 470c0504 | Stefan Hajnoczi | if (bs->copy_on_read_in_flight) {
|
1594 | f4658285 | Stefan Hajnoczi | wait_for_overlapping_requests(bs, sector_num, nb_sectors); |
1595 | f4658285 | Stefan Hajnoczi | } |
1596 | f4658285 | Stefan Hajnoczi | |
1597 | dbffbdcf | Stefan Hajnoczi | tracked_request_begin(&req, bs, sector_num, nb_sectors, false);
|
1598 | ab185921 | Stefan Hajnoczi | |
1599 | 470c0504 | Stefan Hajnoczi | if (flags & BDRV_REQ_COPY_ON_READ) {
|
1600 | ab185921 | Stefan Hajnoczi | int pnum;
|
1601 | ab185921 | Stefan Hajnoczi | |
1602 | ab185921 | Stefan Hajnoczi | ret = bdrv_co_is_allocated(bs, sector_num, nb_sectors, &pnum); |
1603 | ab185921 | Stefan Hajnoczi | if (ret < 0) { |
1604 | ab185921 | Stefan Hajnoczi | goto out;
|
1605 | ab185921 | Stefan Hajnoczi | } |
1606 | ab185921 | Stefan Hajnoczi | |
1607 | ab185921 | Stefan Hajnoczi | if (!ret || pnum != nb_sectors) {
|
1608 | 470c0504 | Stefan Hajnoczi | ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov); |
1609 | ab185921 | Stefan Hajnoczi | goto out;
|
1610 | ab185921 | Stefan Hajnoczi | } |
1611 | ab185921 | Stefan Hajnoczi | } |
1612 | ab185921 | Stefan Hajnoczi | |
1613 | dbffbdcf | Stefan Hajnoczi | ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); |
1614 | ab185921 | Stefan Hajnoczi | |
1615 | ab185921 | Stefan Hajnoczi | out:
|
1616 | dbffbdcf | Stefan Hajnoczi | tracked_request_end(&req); |
1617 | 470c0504 | Stefan Hajnoczi | |
1618 | 470c0504 | Stefan Hajnoczi | if (flags & BDRV_REQ_COPY_ON_READ) {
|
1619 | 470c0504 | Stefan Hajnoczi | bs->copy_on_read_in_flight--; |
1620 | 470c0504 | Stefan Hajnoczi | } |
1621 | 470c0504 | Stefan Hajnoczi | |
1622 | dbffbdcf | Stefan Hajnoczi | return ret;
|
1623 | da1fa91d | Kevin Wolf | } |
1624 | da1fa91d | Kevin Wolf | |
1625 | c5fbe571 | Stefan Hajnoczi | int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
|
1626 | da1fa91d | Kevin Wolf | int nb_sectors, QEMUIOVector *qiov)
|
1627 | da1fa91d | Kevin Wolf | { |
1628 | c5fbe571 | Stefan Hajnoczi | trace_bdrv_co_readv(bs, sector_num, nb_sectors); |
1629 | da1fa91d | Kevin Wolf | |
1630 | 470c0504 | Stefan Hajnoczi | return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0); |
1631 | 470c0504 | Stefan Hajnoczi | } |
1632 | 470c0504 | Stefan Hajnoczi | |
1633 | 470c0504 | Stefan Hajnoczi | int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
|
1634 | 470c0504 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
|
1635 | 470c0504 | Stefan Hajnoczi | { |
1636 | 470c0504 | Stefan Hajnoczi | trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors); |
1637 | 470c0504 | Stefan Hajnoczi | |
1638 | 470c0504 | Stefan Hajnoczi | return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov,
|
1639 | 470c0504 | Stefan Hajnoczi | BDRV_REQ_COPY_ON_READ); |
1640 | c5fbe571 | Stefan Hajnoczi | } |
1641 | c5fbe571 | Stefan Hajnoczi | |
1642 | c5fbe571 | Stefan Hajnoczi | /*
|
1643 | c5fbe571 | Stefan Hajnoczi | * Handle a write request in coroutine context
|
1644 | c5fbe571 | Stefan Hajnoczi | */
|
1645 | c5fbe571 | Stefan Hajnoczi | static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, |
1646 | c5fbe571 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
|
1647 | c5fbe571 | Stefan Hajnoczi | { |
1648 | c5fbe571 | Stefan Hajnoczi | BlockDriver *drv = bs->drv; |
1649 | dbffbdcf | Stefan Hajnoczi | BdrvTrackedRequest req; |
1650 | 6b7cb247 | Stefan Hajnoczi | int ret;
|
1651 | da1fa91d | Kevin Wolf | |
1652 | da1fa91d | Kevin Wolf | if (!bs->drv) {
|
1653 | da1fa91d | Kevin Wolf | return -ENOMEDIUM;
|
1654 | da1fa91d | Kevin Wolf | } |
1655 | da1fa91d | Kevin Wolf | if (bs->read_only) {
|
1656 | da1fa91d | Kevin Wolf | return -EACCES;
|
1657 | da1fa91d | Kevin Wolf | } |
1658 | da1fa91d | Kevin Wolf | if (bdrv_check_request(bs, sector_num, nb_sectors)) {
|
1659 | da1fa91d | Kevin Wolf | return -EIO;
|
1660 | da1fa91d | Kevin Wolf | } |
1661 | da1fa91d | Kevin Wolf | |
1662 | 98f90dba | Zhi Yong Wu | /* throttling disk write I/O */
|
1663 | 98f90dba | Zhi Yong Wu | if (bs->io_limits_enabled) {
|
1664 | 98f90dba | Zhi Yong Wu | bdrv_io_limits_intercept(bs, true, nb_sectors);
|
1665 | 98f90dba | Zhi Yong Wu | } |
1666 | 98f90dba | Zhi Yong Wu | |
1667 | 470c0504 | Stefan Hajnoczi | if (bs->copy_on_read_in_flight) {
|
1668 | f4658285 | Stefan Hajnoczi | wait_for_overlapping_requests(bs, sector_num, nb_sectors); |
1669 | f4658285 | Stefan Hajnoczi | } |
1670 | f4658285 | Stefan Hajnoczi | |
1671 | dbffbdcf | Stefan Hajnoczi | tracked_request_begin(&req, bs, sector_num, nb_sectors, true);
|
1672 | dbffbdcf | Stefan Hajnoczi | |
1673 | 6b7cb247 | Stefan Hajnoczi | ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); |
1674 | 6b7cb247 | Stefan Hajnoczi | |
1675 | da1fa91d | Kevin Wolf | if (bs->dirty_bitmap) {
|
1676 | da1fa91d | Kevin Wolf | set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
|
1677 | da1fa91d | Kevin Wolf | } |
1678 | da1fa91d | Kevin Wolf | |
1679 | da1fa91d | Kevin Wolf | if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { |
1680 | da1fa91d | Kevin Wolf | bs->wr_highest_sector = sector_num + nb_sectors - 1;
|
1681 | da1fa91d | Kevin Wolf | } |
1682 | da1fa91d | Kevin Wolf | |
1683 | dbffbdcf | Stefan Hajnoczi | tracked_request_end(&req); |
1684 | dbffbdcf | Stefan Hajnoczi | |
1685 | 6b7cb247 | Stefan Hajnoczi | return ret;
|
1686 | da1fa91d | Kevin Wolf | } |
1687 | da1fa91d | Kevin Wolf | |
1688 | c5fbe571 | Stefan Hajnoczi | int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
|
1689 | c5fbe571 | Stefan Hajnoczi | int nb_sectors, QEMUIOVector *qiov)
|
1690 | c5fbe571 | Stefan Hajnoczi | { |
1691 | c5fbe571 | Stefan Hajnoczi | trace_bdrv_co_writev(bs, sector_num, nb_sectors); |
1692 | c5fbe571 | Stefan Hajnoczi | |
1693 | c5fbe571 | Stefan Hajnoczi | return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov);
|
1694 | c5fbe571 | Stefan Hajnoczi | } |
1695 | c5fbe571 | Stefan Hajnoczi | |
1696 | 83f64091 | bellard | /**
|
1697 | 83f64091 | bellard | * Truncate file to 'offset' bytes (needed only for file protocols)
|
1698 | 83f64091 | bellard | */
|
1699 | 83f64091 | bellard | int bdrv_truncate(BlockDriverState *bs, int64_t offset)
|
1700 | 83f64091 | bellard | { |
1701 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
1702 | 51762288 | Stefan Hajnoczi | int ret;
|
1703 | 83f64091 | bellard | if (!drv)
|
1704 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1705 | 83f64091 | bellard | if (!drv->bdrv_truncate)
|
1706 | 83f64091 | bellard | return -ENOTSUP;
|
1707 | 59f2689d | Naphtali Sprei | if (bs->read_only)
|
1708 | 59f2689d | Naphtali Sprei | return -EACCES;
|
1709 | 8591675f | Marcelo Tosatti | if (bdrv_in_use(bs))
|
1710 | 8591675f | Marcelo Tosatti | return -EBUSY;
|
1711 | 51762288 | Stefan Hajnoczi | ret = drv->bdrv_truncate(bs, offset); |
1712 | 51762288 | Stefan Hajnoczi | if (ret == 0) { |
1713 | 51762288 | Stefan Hajnoczi | ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); |
1714 | 145feb17 | Markus Armbruster | bdrv_dev_resize_cb(bs); |
1715 | 51762288 | Stefan Hajnoczi | } |
1716 | 51762288 | Stefan Hajnoczi | return ret;
|
1717 | 83f64091 | bellard | } |
1718 | 83f64091 | bellard | |
1719 | 83f64091 | bellard | /**
|
1720 | 4a1d5e1f | Fam Zheng | * Length of a allocated file in bytes. Sparse files are counted by actual
|
1721 | 4a1d5e1f | Fam Zheng | * allocated space. Return < 0 if error or unknown.
|
1722 | 4a1d5e1f | Fam Zheng | */
|
1723 | 4a1d5e1f | Fam Zheng | int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) |
1724 | 4a1d5e1f | Fam Zheng | { |
1725 | 4a1d5e1f | Fam Zheng | BlockDriver *drv = bs->drv; |
1726 | 4a1d5e1f | Fam Zheng | if (!drv) {
|
1727 | 4a1d5e1f | Fam Zheng | return -ENOMEDIUM;
|
1728 | 4a1d5e1f | Fam Zheng | } |
1729 | 4a1d5e1f | Fam Zheng | if (drv->bdrv_get_allocated_file_size) {
|
1730 | 4a1d5e1f | Fam Zheng | return drv->bdrv_get_allocated_file_size(bs);
|
1731 | 4a1d5e1f | Fam Zheng | } |
1732 | 4a1d5e1f | Fam Zheng | if (bs->file) {
|
1733 | 4a1d5e1f | Fam Zheng | return bdrv_get_allocated_file_size(bs->file);
|
1734 | 4a1d5e1f | Fam Zheng | } |
1735 | 4a1d5e1f | Fam Zheng | return -ENOTSUP;
|
1736 | 4a1d5e1f | Fam Zheng | } |
1737 | 4a1d5e1f | Fam Zheng | |
1738 | 4a1d5e1f | Fam Zheng | /**
|
1739 | 83f64091 | bellard | * Length of a file in bytes. Return < 0 if error or unknown.
|
1740 | 83f64091 | bellard | */
|
1741 | 83f64091 | bellard | int64_t bdrv_getlength(BlockDriverState *bs) |
1742 | 83f64091 | bellard | { |
1743 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
1744 | 83f64091 | bellard | if (!drv)
|
1745 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1746 | 51762288 | Stefan Hajnoczi | |
1747 | 2c6942fa | Markus Armbruster | if (bs->growable || bdrv_dev_has_removable_media(bs)) {
|
1748 | 46a4e4e6 | Stefan Hajnoczi | if (drv->bdrv_getlength) {
|
1749 | 46a4e4e6 | Stefan Hajnoczi | return drv->bdrv_getlength(bs);
|
1750 | 46a4e4e6 | Stefan Hajnoczi | } |
1751 | 83f64091 | bellard | } |
1752 | 46a4e4e6 | Stefan Hajnoczi | return bs->total_sectors * BDRV_SECTOR_SIZE;
|
1753 | fc01f7e7 | bellard | } |
1754 | fc01f7e7 | bellard | |
1755 | 19cb3738 | bellard | /* return 0 as number of sectors if no device present or error */
|
1756 | 96b8f136 | ths | void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
|
1757 | fc01f7e7 | bellard | { |
1758 | 19cb3738 | bellard | int64_t length; |
1759 | 19cb3738 | bellard | length = bdrv_getlength(bs); |
1760 | 19cb3738 | bellard | if (length < 0) |
1761 | 19cb3738 | bellard | length = 0;
|
1762 | 19cb3738 | bellard | else
|
1763 | 6ea44308 | Jan Kiszka | length = length >> BDRV_SECTOR_BITS; |
1764 | 19cb3738 | bellard | *nb_sectors_ptr = length; |
1765 | fc01f7e7 | bellard | } |
1766 | cf98951b | bellard | |
1767 | f3d54fc4 | aliguori | struct partition {
|
1768 | f3d54fc4 | aliguori | uint8_t boot_ind; /* 0x80 - active */
|
1769 | f3d54fc4 | aliguori | uint8_t head; /* starting head */
|
1770 | f3d54fc4 | aliguori | uint8_t sector; /* starting sector */
|
1771 | f3d54fc4 | aliguori | uint8_t cyl; /* starting cylinder */
|
1772 | f3d54fc4 | aliguori | uint8_t sys_ind; /* What partition type */
|
1773 | f3d54fc4 | aliguori | uint8_t end_head; /* end head */
|
1774 | f3d54fc4 | aliguori | uint8_t end_sector; /* end sector */
|
1775 | f3d54fc4 | aliguori | uint8_t end_cyl; /* end cylinder */
|
1776 | f3d54fc4 | aliguori | uint32_t start_sect; /* starting sector counting from 0 */
|
1777 | f3d54fc4 | aliguori | uint32_t nr_sects; /* nr of sectors in partition */
|
1778 | 541dc0d4 | Stefan Weil | } QEMU_PACKED; |
1779 | f3d54fc4 | aliguori | |
1780 | f3d54fc4 | aliguori | /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
|
1781 | f3d54fc4 | aliguori | static int guess_disk_lchs(BlockDriverState *bs, |
1782 | f3d54fc4 | aliguori | int *pcylinders, int *pheads, int *psectors) |
1783 | f3d54fc4 | aliguori | { |
1784 | eb5a3165 | Jes Sorensen | uint8_t buf[BDRV_SECTOR_SIZE]; |
1785 | f3d54fc4 | aliguori | int ret, i, heads, sectors, cylinders;
|
1786 | f3d54fc4 | aliguori | struct partition *p;
|
1787 | f3d54fc4 | aliguori | uint32_t nr_sects; |
1788 | a38131b6 | blueswir1 | uint64_t nb_sectors; |
1789 | f3d54fc4 | aliguori | |
1790 | f3d54fc4 | aliguori | bdrv_get_geometry(bs, &nb_sectors); |
1791 | f3d54fc4 | aliguori | |
1792 | f3d54fc4 | aliguori | ret = bdrv_read(bs, 0, buf, 1); |
1793 | f3d54fc4 | aliguori | if (ret < 0) |
1794 | f3d54fc4 | aliguori | return -1; |
1795 | f3d54fc4 | aliguori | /* test msdos magic */
|
1796 | f3d54fc4 | aliguori | if (buf[510] != 0x55 || buf[511] != 0xaa) |
1797 | f3d54fc4 | aliguori | return -1; |
1798 | f3d54fc4 | aliguori | for(i = 0; i < 4; i++) { |
1799 | f3d54fc4 | aliguori | p = ((struct partition *)(buf + 0x1be)) + i; |
1800 | f3d54fc4 | aliguori | nr_sects = le32_to_cpu(p->nr_sects); |
1801 | f3d54fc4 | aliguori | if (nr_sects && p->end_head) {
|
1802 | f3d54fc4 | aliguori | /* We make the assumption that the partition terminates on
|
1803 | f3d54fc4 | aliguori | a cylinder boundary */
|
1804 | f3d54fc4 | aliguori | heads = p->end_head + 1;
|
1805 | f3d54fc4 | aliguori | sectors = p->end_sector & 63;
|
1806 | f3d54fc4 | aliguori | if (sectors == 0) |
1807 | f3d54fc4 | aliguori | continue;
|
1808 | f3d54fc4 | aliguori | cylinders = nb_sectors / (heads * sectors); |
1809 | f3d54fc4 | aliguori | if (cylinders < 1 || cylinders > 16383) |
1810 | f3d54fc4 | aliguori | continue;
|
1811 | f3d54fc4 | aliguori | *pheads = heads; |
1812 | f3d54fc4 | aliguori | *psectors = sectors; |
1813 | f3d54fc4 | aliguori | *pcylinders = cylinders; |
1814 | f3d54fc4 | aliguori | #if 0
|
1815 | f3d54fc4 | aliguori | printf("guessed geometry: LCHS=%d %d %d\n",
|
1816 | f3d54fc4 | aliguori | cylinders, heads, sectors);
|
1817 | f3d54fc4 | aliguori | #endif
|
1818 | f3d54fc4 | aliguori | return 0; |
1819 | f3d54fc4 | aliguori | } |
1820 | f3d54fc4 | aliguori | } |
1821 | f3d54fc4 | aliguori | return -1; |
1822 | f3d54fc4 | aliguori | } |
1823 | f3d54fc4 | aliguori | |
1824 | f3d54fc4 | aliguori | void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) |
1825 | f3d54fc4 | aliguori | { |
1826 | f3d54fc4 | aliguori | int translation, lba_detected = 0; |
1827 | f3d54fc4 | aliguori | int cylinders, heads, secs;
|
1828 | a38131b6 | blueswir1 | uint64_t nb_sectors; |
1829 | f3d54fc4 | aliguori | |
1830 | f3d54fc4 | aliguori | /* if a geometry hint is available, use it */
|
1831 | f3d54fc4 | aliguori | bdrv_get_geometry(bs, &nb_sectors); |
1832 | f3d54fc4 | aliguori | bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); |
1833 | f3d54fc4 | aliguori | translation = bdrv_get_translation_hint(bs); |
1834 | f3d54fc4 | aliguori | if (cylinders != 0) { |
1835 | f3d54fc4 | aliguori | *pcyls = cylinders; |
1836 | f3d54fc4 | aliguori | *pheads = heads; |
1837 | f3d54fc4 | aliguori | *psecs = secs; |
1838 | f3d54fc4 | aliguori | } else {
|
1839 | f3d54fc4 | aliguori | if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { |
1840 | f3d54fc4 | aliguori | if (heads > 16) { |
1841 | f3d54fc4 | aliguori | /* if heads > 16, it means that a BIOS LBA
|
1842 | f3d54fc4 | aliguori | translation was active, so the default
|
1843 | f3d54fc4 | aliguori | hardware geometry is OK */
|
1844 | f3d54fc4 | aliguori | lba_detected = 1;
|
1845 | f3d54fc4 | aliguori | goto default_geometry;
|
1846 | f3d54fc4 | aliguori | } else {
|
1847 | f3d54fc4 | aliguori | *pcyls = cylinders; |
1848 | f3d54fc4 | aliguori | *pheads = heads; |
1849 | f3d54fc4 | aliguori | *psecs = secs; |
1850 | f3d54fc4 | aliguori | /* disable any translation to be in sync with
|
1851 | f3d54fc4 | aliguori | the logical geometry */
|
1852 | f3d54fc4 | aliguori | if (translation == BIOS_ATA_TRANSLATION_AUTO) {
|
1853 | f3d54fc4 | aliguori | bdrv_set_translation_hint(bs, |
1854 | f3d54fc4 | aliguori | BIOS_ATA_TRANSLATION_NONE); |
1855 | f3d54fc4 | aliguori | } |
1856 | f3d54fc4 | aliguori | } |
1857 | f3d54fc4 | aliguori | } else {
|
1858 | f3d54fc4 | aliguori | default_geometry:
|
1859 | f3d54fc4 | aliguori | /* if no geometry, use a standard physical disk geometry */
|
1860 | f3d54fc4 | aliguori | cylinders = nb_sectors / (16 * 63); |
1861 | f3d54fc4 | aliguori | |
1862 | f3d54fc4 | aliguori | if (cylinders > 16383) |
1863 | f3d54fc4 | aliguori | cylinders = 16383;
|
1864 | f3d54fc4 | aliguori | else if (cylinders < 2) |
1865 | f3d54fc4 | aliguori | cylinders = 2;
|
1866 | f3d54fc4 | aliguori | *pcyls = cylinders; |
1867 | f3d54fc4 | aliguori | *pheads = 16;
|
1868 | f3d54fc4 | aliguori | *psecs = 63;
|
1869 | f3d54fc4 | aliguori | if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { |
1870 | f3d54fc4 | aliguori | if ((*pcyls * *pheads) <= 131072) { |
1871 | f3d54fc4 | aliguori | bdrv_set_translation_hint(bs, |
1872 | f3d54fc4 | aliguori | BIOS_ATA_TRANSLATION_LARGE); |
1873 | f3d54fc4 | aliguori | } else {
|
1874 | f3d54fc4 | aliguori | bdrv_set_translation_hint(bs, |
1875 | f3d54fc4 | aliguori | BIOS_ATA_TRANSLATION_LBA); |
1876 | f3d54fc4 | aliguori | } |
1877 | f3d54fc4 | aliguori | } |
1878 | f3d54fc4 | aliguori | } |
1879 | f3d54fc4 | aliguori | bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); |
1880 | f3d54fc4 | aliguori | } |
1881 | f3d54fc4 | aliguori | } |
1882 | f3d54fc4 | aliguori | |
1883 | 5fafdf24 | ths | void bdrv_set_geometry_hint(BlockDriverState *bs,
|
1884 | b338082b | bellard | int cyls, int heads, int secs) |
1885 | b338082b | bellard | { |
1886 | b338082b | bellard | bs->cyls = cyls; |
1887 | b338082b | bellard | bs->heads = heads; |
1888 | b338082b | bellard | bs->secs = secs; |
1889 | b338082b | bellard | } |
1890 | b338082b | bellard | |
1891 | 46d4767d | bellard | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
1892 | 46d4767d | bellard | { |
1893 | 46d4767d | bellard | bs->translation = translation; |
1894 | 46d4767d | bellard | } |
1895 | 46d4767d | bellard | |
1896 | 5fafdf24 | ths | void bdrv_get_geometry_hint(BlockDriverState *bs,
|
1897 | b338082b | bellard | int *pcyls, int *pheads, int *psecs) |
1898 | b338082b | bellard | { |
1899 | b338082b | bellard | *pcyls = bs->cyls; |
1900 | b338082b | bellard | *pheads = bs->heads; |
1901 | b338082b | bellard | *psecs = bs->secs; |
1902 | b338082b | bellard | } |
1903 | b338082b | bellard | |
1904 | 0563e191 | Zhi Yong Wu | /* throttling disk io limits */
|
1905 | 0563e191 | Zhi Yong Wu | void bdrv_set_io_limits(BlockDriverState *bs,
|
1906 | 0563e191 | Zhi Yong Wu | BlockIOLimit *io_limits) |
1907 | 0563e191 | Zhi Yong Wu | { |
1908 | 0563e191 | Zhi Yong Wu | bs->io_limits = *io_limits; |
1909 | 0563e191 | Zhi Yong Wu | bs->io_limits_enabled = bdrv_io_limits_enabled(bs); |
1910 | 0563e191 | Zhi Yong Wu | } |
1911 | 0563e191 | Zhi Yong Wu | |
1912 | 5bbdbb46 | Blue Swirl | /* Recognize floppy formats */
|
1913 | 5bbdbb46 | Blue Swirl | typedef struct FDFormat { |
1914 | 5bbdbb46 | Blue Swirl | FDriveType drive; |
1915 | 5bbdbb46 | Blue Swirl | uint8_t last_sect; |
1916 | 5bbdbb46 | Blue Swirl | uint8_t max_track; |
1917 | 5bbdbb46 | Blue Swirl | uint8_t max_head; |
1918 | 5bbdbb46 | Blue Swirl | } FDFormat; |
1919 | 5bbdbb46 | Blue Swirl | |
1920 | 5bbdbb46 | Blue Swirl | static const FDFormat fd_formats[] = { |
1921 | 5bbdbb46 | Blue Swirl | /* First entry is default format */
|
1922 | 5bbdbb46 | Blue Swirl | /* 1.44 MB 3"1/2 floppy disks */
|
1923 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 18, 80, 1, }, |
1924 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 20, 80, 1, }, |
1925 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 21, 80, 1, }, |
1926 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 21, 82, 1, }, |
1927 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 21, 83, 1, }, |
1928 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 22, 80, 1, }, |
1929 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 23, 80, 1, }, |
1930 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 24, 80, 1, }, |
1931 | 5bbdbb46 | Blue Swirl | /* 2.88 MB 3"1/2 floppy disks */
|
1932 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_288, 36, 80, 1, }, |
1933 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_288, 39, 80, 1, }, |
1934 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_288, 40, 80, 1, }, |
1935 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_288, 44, 80, 1, }, |
1936 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_288, 48, 80, 1, }, |
1937 | 5bbdbb46 | Blue Swirl | /* 720 kB 3"1/2 floppy disks */
|
1938 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 9, 80, 1, }, |
1939 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 10, 80, 1, }, |
1940 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 10, 82, 1, }, |
1941 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 10, 83, 1, }, |
1942 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 13, 80, 1, }, |
1943 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 14, 80, 1, }, |
1944 | 5bbdbb46 | Blue Swirl | /* 1.2 MB 5"1/4 floppy disks */
|
1945 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 15, 80, 1, }, |
1946 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 18, 80, 1, }, |
1947 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 18, 82, 1, }, |
1948 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 18, 83, 1, }, |
1949 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 20, 80, 1, }, |
1950 | 5bbdbb46 | Blue Swirl | /* 720 kB 5"1/4 floppy disks */
|
1951 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 9, 80, 1, }, |
1952 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 11, 80, 1, }, |
1953 | 5bbdbb46 | Blue Swirl | /* 360 kB 5"1/4 floppy disks */
|
1954 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 9, 40, 1, }, |
1955 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 9, 40, 0, }, |
1956 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 10, 41, 1, }, |
1957 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 10, 42, 1, }, |
1958 | 5bbdbb46 | Blue Swirl | /* 320 kB 5"1/4 floppy disks */
|
1959 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 8, 40, 1, }, |
1960 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 8, 40, 0, }, |
1961 | 5bbdbb46 | Blue Swirl | /* 360 kB must match 5"1/4 better than 3"1/2... */
|
1962 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 9, 80, 0, }, |
1963 | 5bbdbb46 | Blue Swirl | /* end */
|
1964 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_NONE, -1, -1, 0, }, |
1965 | 5bbdbb46 | Blue Swirl | }; |
1966 | 5bbdbb46 | Blue Swirl | |
1967 | 5bbdbb46 | Blue Swirl | void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads, |
1968 | 5bbdbb46 | Blue Swirl | int *max_track, int *last_sect, |
1969 | 5bbdbb46 | Blue Swirl | FDriveType drive_in, FDriveType *drive) |
1970 | 5bbdbb46 | Blue Swirl | { |
1971 | 5bbdbb46 | Blue Swirl | const FDFormat *parse;
|
1972 | 5bbdbb46 | Blue Swirl | uint64_t nb_sectors, size; |
1973 | 5bbdbb46 | Blue Swirl | int i, first_match, match;
|
1974 | 5bbdbb46 | Blue Swirl | |
1975 | 5bbdbb46 | Blue Swirl | bdrv_get_geometry_hint(bs, nb_heads, max_track, last_sect); |
1976 | 5bbdbb46 | Blue Swirl | if (*nb_heads != 0 && *max_track != 0 && *last_sect != 0) { |
1977 | 5bbdbb46 | Blue Swirl | /* User defined disk */
|
1978 | 5bbdbb46 | Blue Swirl | } else {
|
1979 | 5bbdbb46 | Blue Swirl | bdrv_get_geometry(bs, &nb_sectors); |
1980 | 5bbdbb46 | Blue Swirl | match = -1;
|
1981 | 5bbdbb46 | Blue Swirl | first_match = -1;
|
1982 | 5bbdbb46 | Blue Swirl | for (i = 0; ; i++) { |
1983 | 5bbdbb46 | Blue Swirl | parse = &fd_formats[i]; |
1984 | 5bbdbb46 | Blue Swirl | if (parse->drive == FDRIVE_DRV_NONE) {
|
1985 | 5bbdbb46 | Blue Swirl | break;
|
1986 | 5bbdbb46 | Blue Swirl | } |
1987 | 5bbdbb46 | Blue Swirl | if (drive_in == parse->drive ||
|
1988 | 5bbdbb46 | Blue Swirl | drive_in == FDRIVE_DRV_NONE) { |
1989 | 5bbdbb46 | Blue Swirl | size = (parse->max_head + 1) * parse->max_track *
|
1990 | 5bbdbb46 | Blue Swirl | parse->last_sect; |
1991 | 5bbdbb46 | Blue Swirl | if (nb_sectors == size) {
|
1992 | 5bbdbb46 | Blue Swirl | match = i; |
1993 | 5bbdbb46 | Blue Swirl | break;
|
1994 | 5bbdbb46 | Blue Swirl | } |
1995 | 5bbdbb46 | Blue Swirl | if (first_match == -1) { |
1996 | 5bbdbb46 | Blue Swirl | first_match = i; |
1997 | 5bbdbb46 | Blue Swirl | } |
1998 | 5bbdbb46 | Blue Swirl | } |
1999 | 5bbdbb46 | Blue Swirl | } |
2000 | 5bbdbb46 | Blue Swirl | if (match == -1) { |
2001 | 5bbdbb46 | Blue Swirl | if (first_match == -1) { |
2002 | 5bbdbb46 | Blue Swirl | match = 1;
|
2003 | 5bbdbb46 | Blue Swirl | } else {
|
2004 | 5bbdbb46 | Blue Swirl | match = first_match; |
2005 | 5bbdbb46 | Blue Swirl | } |
2006 | 5bbdbb46 | Blue Swirl | parse = &fd_formats[match]; |
2007 | 5bbdbb46 | Blue Swirl | } |
2008 | 5bbdbb46 | Blue Swirl | *nb_heads = parse->max_head + 1;
|
2009 | 5bbdbb46 | Blue Swirl | *max_track = parse->max_track; |
2010 | 5bbdbb46 | Blue Swirl | *last_sect = parse->last_sect; |
2011 | 5bbdbb46 | Blue Swirl | *drive = parse->drive; |
2012 | 5bbdbb46 | Blue Swirl | } |
2013 | 5bbdbb46 | Blue Swirl | } |
2014 | 5bbdbb46 | Blue Swirl | |
2015 | 46d4767d | bellard | int bdrv_get_translation_hint(BlockDriverState *bs)
|
2016 | 46d4767d | bellard | { |
2017 | 46d4767d | bellard | return bs->translation;
|
2018 | 46d4767d | bellard | } |
2019 | 46d4767d | bellard | |
2020 | abd7f68d | Markus Armbruster | void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
|
2021 | abd7f68d | Markus Armbruster | BlockErrorAction on_write_error) |
2022 | abd7f68d | Markus Armbruster | { |
2023 | abd7f68d | Markus Armbruster | bs->on_read_error = on_read_error; |
2024 | abd7f68d | Markus Armbruster | bs->on_write_error = on_write_error; |
2025 | abd7f68d | Markus Armbruster | } |
2026 | abd7f68d | Markus Armbruster | |
2027 | abd7f68d | Markus Armbruster | BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
|
2028 | abd7f68d | Markus Armbruster | { |
2029 | abd7f68d | Markus Armbruster | return is_read ? bs->on_read_error : bs->on_write_error;
|
2030 | abd7f68d | Markus Armbruster | } |
2031 | abd7f68d | Markus Armbruster | |
2032 | b338082b | bellard | int bdrv_is_read_only(BlockDriverState *bs)
|
2033 | b338082b | bellard | { |
2034 | b338082b | bellard | return bs->read_only;
|
2035 | b338082b | bellard | } |
2036 | b338082b | bellard | |
2037 | 985a03b0 | ths | int bdrv_is_sg(BlockDriverState *bs)
|
2038 | 985a03b0 | ths | { |
2039 | 985a03b0 | ths | return bs->sg;
|
2040 | 985a03b0 | ths | } |
2041 | 985a03b0 | ths | |
2042 | e900a7b7 | Christoph Hellwig | int bdrv_enable_write_cache(BlockDriverState *bs)
|
2043 | e900a7b7 | Christoph Hellwig | { |
2044 | e900a7b7 | Christoph Hellwig | return bs->enable_write_cache;
|
2045 | e900a7b7 | Christoph Hellwig | } |
2046 | e900a7b7 | Christoph Hellwig | |
2047 | ea2384d3 | bellard | int bdrv_is_encrypted(BlockDriverState *bs)
|
2048 | ea2384d3 | bellard | { |
2049 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted)
|
2050 | ea2384d3 | bellard | return 1; |
2051 | ea2384d3 | bellard | return bs->encrypted;
|
2052 | ea2384d3 | bellard | } |
2053 | ea2384d3 | bellard | |
2054 | c0f4ce77 | aliguori | int bdrv_key_required(BlockDriverState *bs)
|
2055 | c0f4ce77 | aliguori | { |
2056 | c0f4ce77 | aliguori | BlockDriverState *backing_hd = bs->backing_hd; |
2057 | c0f4ce77 | aliguori | |
2058 | c0f4ce77 | aliguori | if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
|
2059 | c0f4ce77 | aliguori | return 1; |
2060 | c0f4ce77 | aliguori | return (bs->encrypted && !bs->valid_key);
|
2061 | c0f4ce77 | aliguori | } |
2062 | c0f4ce77 | aliguori | |
2063 | ea2384d3 | bellard | int bdrv_set_key(BlockDriverState *bs, const char *key) |
2064 | ea2384d3 | bellard | { |
2065 | ea2384d3 | bellard | int ret;
|
2066 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted) {
|
2067 | ea2384d3 | bellard | ret = bdrv_set_key(bs->backing_hd, key); |
2068 | ea2384d3 | bellard | if (ret < 0) |
2069 | ea2384d3 | bellard | return ret;
|
2070 | ea2384d3 | bellard | if (!bs->encrypted)
|
2071 | ea2384d3 | bellard | return 0; |
2072 | ea2384d3 | bellard | } |
2073 | fd04a2ae | Shahar Havivi | if (!bs->encrypted) {
|
2074 | fd04a2ae | Shahar Havivi | return -EINVAL;
|
2075 | fd04a2ae | Shahar Havivi | } else if (!bs->drv || !bs->drv->bdrv_set_key) { |
2076 | fd04a2ae | Shahar Havivi | return -ENOMEDIUM;
|
2077 | fd04a2ae | Shahar Havivi | } |
2078 | c0f4ce77 | aliguori | ret = bs->drv->bdrv_set_key(bs, key); |
2079 | bb5fc20f | aliguori | if (ret < 0) { |
2080 | bb5fc20f | aliguori | bs->valid_key = 0;
|
2081 | bb5fc20f | aliguori | } else if (!bs->valid_key) { |
2082 | bb5fc20f | aliguori | bs->valid_key = 1;
|
2083 | bb5fc20f | aliguori | /* call the change callback now, we skipped it on open */
|
2084 | 7d4b4ba5 | Markus Armbruster | bdrv_dev_change_media_cb(bs, true);
|
2085 | bb5fc20f | aliguori | } |
2086 | c0f4ce77 | aliguori | return ret;
|
2087 | ea2384d3 | bellard | } |
2088 | ea2384d3 | bellard | |
2089 | ea2384d3 | bellard | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) |
2090 | ea2384d3 | bellard | { |
2091 | 19cb3738 | bellard | if (!bs->drv) {
|
2092 | ea2384d3 | bellard | buf[0] = '\0'; |
2093 | ea2384d3 | bellard | } else {
|
2094 | ea2384d3 | bellard | pstrcpy(buf, buf_size, bs->drv->format_name); |
2095 | ea2384d3 | bellard | } |
2096 | ea2384d3 | bellard | } |
2097 | ea2384d3 | bellard | |
2098 | 5fafdf24 | ths | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
2099 | ea2384d3 | bellard | void *opaque)
|
2100 | ea2384d3 | bellard | { |
2101 | ea2384d3 | bellard | BlockDriver *drv; |
2102 | ea2384d3 | bellard | |
2103 | 8a22f02a | Stefan Hajnoczi | QLIST_FOREACH(drv, &bdrv_drivers, list) { |
2104 | ea2384d3 | bellard | it(opaque, drv->format_name); |
2105 | ea2384d3 | bellard | } |
2106 | ea2384d3 | bellard | } |
2107 | ea2384d3 | bellard | |
2108 | b338082b | bellard | BlockDriverState *bdrv_find(const char *name) |
2109 | b338082b | bellard | { |
2110 | b338082b | bellard | BlockDriverState *bs; |
2111 | b338082b | bellard | |
2112 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
2113 | 1b7bdbc1 | Stefan Hajnoczi | if (!strcmp(name, bs->device_name)) {
|
2114 | b338082b | bellard | return bs;
|
2115 | 1b7bdbc1 | Stefan Hajnoczi | } |
2116 | b338082b | bellard | } |
2117 | b338082b | bellard | return NULL; |
2118 | b338082b | bellard | } |
2119 | b338082b | bellard | |
2120 | 2f399b0a | Markus Armbruster | BlockDriverState *bdrv_next(BlockDriverState *bs) |
2121 | 2f399b0a | Markus Armbruster | { |
2122 | 2f399b0a | Markus Armbruster | if (!bs) {
|
2123 | 2f399b0a | Markus Armbruster | return QTAILQ_FIRST(&bdrv_states);
|
2124 | 2f399b0a | Markus Armbruster | } |
2125 | 2f399b0a | Markus Armbruster | return QTAILQ_NEXT(bs, list);
|
2126 | 2f399b0a | Markus Armbruster | } |
2127 | 2f399b0a | Markus Armbruster | |
2128 | 51de9760 | aliguori | void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) |
2129 | 81d0912d | bellard | { |
2130 | 81d0912d | bellard | BlockDriverState *bs; |
2131 | 81d0912d | bellard | |
2132 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
2133 | 51de9760 | aliguori | it(opaque, bs); |
2134 | 81d0912d | bellard | } |
2135 | 81d0912d | bellard | } |
2136 | 81d0912d | bellard | |
2137 | ea2384d3 | bellard | const char *bdrv_get_device_name(BlockDriverState *bs) |
2138 | ea2384d3 | bellard | { |
2139 | ea2384d3 | bellard | return bs->device_name;
|
2140 | ea2384d3 | bellard | } |
2141 | ea2384d3 | bellard | |
2142 | c6ca28d6 | aliguori | void bdrv_flush_all(void) |
2143 | c6ca28d6 | aliguori | { |
2144 | c6ca28d6 | aliguori | BlockDriverState *bs; |
2145 | c6ca28d6 | aliguori | |
2146 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
2147 | c602a489 | Markus Armbruster | if (!bdrv_is_read_only(bs) && bdrv_is_inserted(bs)) {
|
2148 | c6ca28d6 | aliguori | bdrv_flush(bs); |
2149 | 1b7bdbc1 | Stefan Hajnoczi | } |
2150 | 1b7bdbc1 | Stefan Hajnoczi | } |
2151 | c6ca28d6 | aliguori | } |
2152 | c6ca28d6 | aliguori | |
2153 | f2feebbd | Kevin Wolf | int bdrv_has_zero_init(BlockDriverState *bs)
|
2154 | f2feebbd | Kevin Wolf | { |
2155 | f2feebbd | Kevin Wolf | assert(bs->drv); |
2156 | f2feebbd | Kevin Wolf | |
2157 | 336c1c12 | Kevin Wolf | if (bs->drv->bdrv_has_zero_init) {
|
2158 | 336c1c12 | Kevin Wolf | return bs->drv->bdrv_has_zero_init(bs);
|
2159 | f2feebbd | Kevin Wolf | } |
2160 | f2feebbd | Kevin Wolf | |
2161 | f2feebbd | Kevin Wolf | return 1; |
2162 | f2feebbd | Kevin Wolf | } |
2163 | f2feebbd | Kevin Wolf | |
2164 | 376ae3f1 | Stefan Hajnoczi | typedef struct BdrvCoIsAllocatedData { |
2165 | 376ae3f1 | Stefan Hajnoczi | BlockDriverState *bs; |
2166 | 376ae3f1 | Stefan Hajnoczi | int64_t sector_num; |
2167 | 376ae3f1 | Stefan Hajnoczi | int nb_sectors;
|
2168 | 376ae3f1 | Stefan Hajnoczi | int *pnum;
|
2169 | 376ae3f1 | Stefan Hajnoczi | int ret;
|
2170 | 376ae3f1 | Stefan Hajnoczi | bool done;
|
2171 | 376ae3f1 | Stefan Hajnoczi | } BdrvCoIsAllocatedData; |
2172 | 376ae3f1 | Stefan Hajnoczi | |
2173 | f58c7b35 | ths | /*
|
2174 | f58c7b35 | ths | * Returns true iff the specified sector is present in the disk image. Drivers
|
2175 | f58c7b35 | ths | * not implementing the functionality are assumed to not support backing files,
|
2176 | f58c7b35 | ths | * hence all their sectors are reported as allocated.
|
2177 | f58c7b35 | ths | *
|
2178 | bd9533e3 | Stefan Hajnoczi | * If 'sector_num' is beyond the end of the disk image the return value is 0
|
2179 | bd9533e3 | Stefan Hajnoczi | * and 'pnum' is set to 0.
|
2180 | bd9533e3 | Stefan Hajnoczi | *
|
2181 | f58c7b35 | ths | * 'pnum' is set to the number of sectors (including and immediately following
|
2182 | f58c7b35 | ths | * the specified sector) that are known to be in the same
|
2183 | f58c7b35 | ths | * allocated/unallocated state.
|
2184 | f58c7b35 | ths | *
|
2185 | bd9533e3 | Stefan Hajnoczi | * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
|
2186 | bd9533e3 | Stefan Hajnoczi | * beyond the end of the disk image it will be clamped.
|
2187 | f58c7b35 | ths | */
|
2188 | 060f51c9 | Stefan Hajnoczi | int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t sector_num,
|
2189 | 060f51c9 | Stefan Hajnoczi | int nb_sectors, int *pnum) |
2190 | f58c7b35 | ths | { |
2191 | bd9533e3 | Stefan Hajnoczi | int64_t n; |
2192 | bd9533e3 | Stefan Hajnoczi | |
2193 | bd9533e3 | Stefan Hajnoczi | if (sector_num >= bs->total_sectors) {
|
2194 | bd9533e3 | Stefan Hajnoczi | *pnum = 0;
|
2195 | bd9533e3 | Stefan Hajnoczi | return 0; |
2196 | bd9533e3 | Stefan Hajnoczi | } |
2197 | bd9533e3 | Stefan Hajnoczi | |
2198 | bd9533e3 | Stefan Hajnoczi | n = bs->total_sectors - sector_num; |
2199 | bd9533e3 | Stefan Hajnoczi | if (n < nb_sectors) {
|
2200 | bd9533e3 | Stefan Hajnoczi | nb_sectors = n; |
2201 | bd9533e3 | Stefan Hajnoczi | } |
2202 | bd9533e3 | Stefan Hajnoczi | |
2203 | 6aebab14 | Stefan Hajnoczi | if (!bs->drv->bdrv_co_is_allocated) {
|
2204 | bd9533e3 | Stefan Hajnoczi | *pnum = nb_sectors; |
2205 | f58c7b35 | ths | return 1; |
2206 | f58c7b35 | ths | } |
2207 | 6aebab14 | Stefan Hajnoczi | |
2208 | 060f51c9 | Stefan Hajnoczi | return bs->drv->bdrv_co_is_allocated(bs, sector_num, nb_sectors, pnum);
|
2209 | 060f51c9 | Stefan Hajnoczi | } |
2210 | 060f51c9 | Stefan Hajnoczi | |
2211 | 060f51c9 | Stefan Hajnoczi | /* Coroutine wrapper for bdrv_is_allocated() */
|
2212 | 060f51c9 | Stefan Hajnoczi | static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque) |
2213 | 060f51c9 | Stefan Hajnoczi | { |
2214 | 060f51c9 | Stefan Hajnoczi | BdrvCoIsAllocatedData *data = opaque; |
2215 | 060f51c9 | Stefan Hajnoczi | BlockDriverState *bs = data->bs; |
2216 | 060f51c9 | Stefan Hajnoczi | |
2217 | 060f51c9 | Stefan Hajnoczi | data->ret = bdrv_co_is_allocated(bs, data->sector_num, data->nb_sectors, |
2218 | 060f51c9 | Stefan Hajnoczi | data->pnum); |
2219 | 060f51c9 | Stefan Hajnoczi | data->done = true;
|
2220 | 060f51c9 | Stefan Hajnoczi | } |
2221 | 060f51c9 | Stefan Hajnoczi | |
2222 | 060f51c9 | Stefan Hajnoczi | /*
|
2223 | 060f51c9 | Stefan Hajnoczi | * Synchronous wrapper around bdrv_co_is_allocated().
|
2224 | 060f51c9 | Stefan Hajnoczi | *
|
2225 | 060f51c9 | Stefan Hajnoczi | * See bdrv_co_is_allocated() for details.
|
2226 | 060f51c9 | Stefan Hajnoczi | */
|
2227 | 060f51c9 | Stefan Hajnoczi | int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, |
2228 | 060f51c9 | Stefan Hajnoczi | int *pnum)
|
2229 | 060f51c9 | Stefan Hajnoczi | { |
2230 | 6aebab14 | Stefan Hajnoczi | Coroutine *co; |
2231 | 6aebab14 | Stefan Hajnoczi | BdrvCoIsAllocatedData data = { |
2232 | 6aebab14 | Stefan Hajnoczi | .bs = bs, |
2233 | 6aebab14 | Stefan Hajnoczi | .sector_num = sector_num, |
2234 | 6aebab14 | Stefan Hajnoczi | .nb_sectors = nb_sectors, |
2235 | 6aebab14 | Stefan Hajnoczi | .pnum = pnum, |
2236 | 6aebab14 | Stefan Hajnoczi | .done = false,
|
2237 | 6aebab14 | Stefan Hajnoczi | }; |
2238 | 6aebab14 | Stefan Hajnoczi | |
2239 | 6aebab14 | Stefan Hajnoczi | co = qemu_coroutine_create(bdrv_is_allocated_co_entry); |
2240 | 6aebab14 | Stefan Hajnoczi | qemu_coroutine_enter(co, &data); |
2241 | 6aebab14 | Stefan Hajnoczi | while (!data.done) {
|
2242 | 6aebab14 | Stefan Hajnoczi | qemu_aio_wait(); |
2243 | 6aebab14 | Stefan Hajnoczi | } |
2244 | 6aebab14 | Stefan Hajnoczi | return data.ret;
|
2245 | f58c7b35 | ths | } |
2246 | f58c7b35 | ths | |
2247 | 2582bfed | Luiz Capitulino | void bdrv_mon_event(const BlockDriverState *bdrv, |
2248 | 2582bfed | Luiz Capitulino | BlockMonEventAction action, int is_read)
|
2249 | 2582bfed | Luiz Capitulino | { |
2250 | 2582bfed | Luiz Capitulino | QObject *data; |
2251 | 2582bfed | Luiz Capitulino | const char *action_str; |
2252 | 2582bfed | Luiz Capitulino | |
2253 | 2582bfed | Luiz Capitulino | switch (action) {
|
2254 | 2582bfed | Luiz Capitulino | case BDRV_ACTION_REPORT:
|
2255 | 2582bfed | Luiz Capitulino | action_str = "report";
|
2256 | 2582bfed | Luiz Capitulino | break;
|
2257 | 2582bfed | Luiz Capitulino | case BDRV_ACTION_IGNORE:
|
2258 | 2582bfed | Luiz Capitulino | action_str = "ignore";
|
2259 | 2582bfed | Luiz Capitulino | break;
|
2260 | 2582bfed | Luiz Capitulino | case BDRV_ACTION_STOP:
|
2261 | 2582bfed | Luiz Capitulino | action_str = "stop";
|
2262 | 2582bfed | Luiz Capitulino | break;
|
2263 | 2582bfed | Luiz Capitulino | default:
|
2264 | 2582bfed | Luiz Capitulino | abort(); |
2265 | 2582bfed | Luiz Capitulino | } |
2266 | 2582bfed | Luiz Capitulino | |
2267 | 2582bfed | Luiz Capitulino | data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
|
2268 | 2582bfed | Luiz Capitulino | bdrv->device_name, |
2269 | 2582bfed | Luiz Capitulino | action_str, |
2270 | 2582bfed | Luiz Capitulino | is_read ? "read" : "write"); |
2271 | 2582bfed | Luiz Capitulino | monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data); |
2272 | 2582bfed | Luiz Capitulino | |
2273 | 2582bfed | Luiz Capitulino | qobject_decref(data); |
2274 | 2582bfed | Luiz Capitulino | } |
2275 | 2582bfed | Luiz Capitulino | |
2276 | b2023818 | Luiz Capitulino | BlockInfoList *qmp_query_block(Error **errp) |
2277 | b338082b | bellard | { |
2278 | b2023818 | Luiz Capitulino | BlockInfoList *head = NULL, *cur_item = NULL; |
2279 | b338082b | bellard | BlockDriverState *bs; |
2280 | b338082b | bellard | |
2281 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
2282 | b2023818 | Luiz Capitulino | BlockInfoList *info = g_malloc0(sizeof(*info));
|
2283 | d15e5465 | Luiz Capitulino | |
2284 | b2023818 | Luiz Capitulino | info->value = g_malloc0(sizeof(*info->value));
|
2285 | b2023818 | Luiz Capitulino | info->value->device = g_strdup(bs->device_name); |
2286 | b2023818 | Luiz Capitulino | info->value->type = g_strdup("unknown");
|
2287 | b2023818 | Luiz Capitulino | info->value->locked = bdrv_dev_is_medium_locked(bs); |
2288 | b2023818 | Luiz Capitulino | info->value->removable = bdrv_dev_has_removable_media(bs); |
2289 | d15e5465 | Luiz Capitulino | |
2290 | e4def80b | Markus Armbruster | if (bdrv_dev_has_removable_media(bs)) {
|
2291 | b2023818 | Luiz Capitulino | info->value->has_tray_open = true;
|
2292 | b2023818 | Luiz Capitulino | info->value->tray_open = bdrv_dev_is_tray_open(bs); |
2293 | e4def80b | Markus Armbruster | } |
2294 | f04ef601 | Luiz Capitulino | |
2295 | f04ef601 | Luiz Capitulino | if (bdrv_iostatus_is_enabled(bs)) {
|
2296 | b2023818 | Luiz Capitulino | info->value->has_io_status = true;
|
2297 | b2023818 | Luiz Capitulino | info->value->io_status = bs->iostatus; |
2298 | f04ef601 | Luiz Capitulino | } |
2299 | f04ef601 | Luiz Capitulino | |
2300 | 19cb3738 | bellard | if (bs->drv) {
|
2301 | b2023818 | Luiz Capitulino | info->value->has_inserted = true;
|
2302 | b2023818 | Luiz Capitulino | info->value->inserted = g_malloc0(sizeof(*info->value->inserted));
|
2303 | b2023818 | Luiz Capitulino | info->value->inserted->file = g_strdup(bs->filename); |
2304 | b2023818 | Luiz Capitulino | info->value->inserted->ro = bs->read_only; |
2305 | b2023818 | Luiz Capitulino | info->value->inserted->drv = g_strdup(bs->drv->format_name); |
2306 | b2023818 | Luiz Capitulino | info->value->inserted->encrypted = bs->encrypted; |
2307 | b2023818 | Luiz Capitulino | if (bs->backing_file[0]) { |
2308 | b2023818 | Luiz Capitulino | info->value->inserted->has_backing_file = true;
|
2309 | b2023818 | Luiz Capitulino | info->value->inserted->backing_file = g_strdup(bs->backing_file); |
2310 | 376253ec | aliguori | } |
2311 | 727f005e | Zhi Yong Wu | |
2312 | 727f005e | Zhi Yong Wu | if (bs->io_limits_enabled) {
|
2313 | 727f005e | Zhi Yong Wu | info->value->inserted->bps = |
2314 | 727f005e | Zhi Yong Wu | bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; |
2315 | 727f005e | Zhi Yong Wu | info->value->inserted->bps_rd = |
2316 | 727f005e | Zhi Yong Wu | bs->io_limits.bps[BLOCK_IO_LIMIT_READ]; |
2317 | 727f005e | Zhi Yong Wu | info->value->inserted->bps_wr = |
2318 | 727f005e | Zhi Yong Wu | bs->io_limits.bps[BLOCK_IO_LIMIT_WRITE]; |
2319 | 727f005e | Zhi Yong Wu | info->value->inserted->iops = |
2320 | 727f005e | Zhi Yong Wu | bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; |
2321 | 727f005e | Zhi Yong Wu | info->value->inserted->iops_rd = |
2322 | 727f005e | Zhi Yong Wu | bs->io_limits.iops[BLOCK_IO_LIMIT_READ]; |
2323 | 727f005e | Zhi Yong Wu | info->value->inserted->iops_wr = |
2324 | 727f005e | Zhi Yong Wu | bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE]; |
2325 | 727f005e | Zhi Yong Wu | } |
2326 | b2023818 | Luiz Capitulino | } |
2327 | d15e5465 | Luiz Capitulino | |
2328 | b2023818 | Luiz Capitulino | /* XXX: waiting for the qapi to support GSList */
|
2329 | b2023818 | Luiz Capitulino | if (!cur_item) {
|
2330 | b2023818 | Luiz Capitulino | head = cur_item = info; |
2331 | b2023818 | Luiz Capitulino | } else {
|
2332 | b2023818 | Luiz Capitulino | cur_item->next = info; |
2333 | b2023818 | Luiz Capitulino | cur_item = info; |
2334 | b338082b | bellard | } |
2335 | b338082b | bellard | } |
2336 | d15e5465 | Luiz Capitulino | |
2337 | b2023818 | Luiz Capitulino | return head;
|
2338 | b338082b | bellard | } |
2339 | a36e69dd | ths | |
2340 | f11f57e4 | Luiz Capitulino | /* Consider exposing this as a full fledged QMP command */
|
2341 | f11f57e4 | Luiz Capitulino | static BlockStats *qmp_query_blockstat(const BlockDriverState *bs, Error **errp) |
2342 | f11f57e4 | Luiz Capitulino | { |
2343 | f11f57e4 | Luiz Capitulino | BlockStats *s; |
2344 | f11f57e4 | Luiz Capitulino | |
2345 | f11f57e4 | Luiz Capitulino | s = g_malloc0(sizeof(*s));
|
2346 | f11f57e4 | Luiz Capitulino | |
2347 | f11f57e4 | Luiz Capitulino | if (bs->device_name[0]) { |
2348 | f11f57e4 | Luiz Capitulino | s->has_device = true;
|
2349 | f11f57e4 | Luiz Capitulino | s->device = g_strdup(bs->device_name); |
2350 | 294cc35f | Kevin Wolf | } |
2351 | 294cc35f | Kevin Wolf | |
2352 | f11f57e4 | Luiz Capitulino | s->stats = g_malloc0(sizeof(*s->stats));
|
2353 | f11f57e4 | Luiz Capitulino | s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ]; |
2354 | f11f57e4 | Luiz Capitulino | s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE]; |
2355 | f11f57e4 | Luiz Capitulino | s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ]; |
2356 | f11f57e4 | Luiz Capitulino | s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE]; |
2357 | f11f57e4 | Luiz Capitulino | s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE; |
2358 | f11f57e4 | Luiz Capitulino | s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH]; |
2359 | f11f57e4 | Luiz Capitulino | s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE]; |
2360 | f11f57e4 | Luiz Capitulino | s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ]; |
2361 | f11f57e4 | Luiz Capitulino | s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH]; |
2362 | f11f57e4 | Luiz Capitulino | |
2363 | 294cc35f | Kevin Wolf | if (bs->file) {
|
2364 | f11f57e4 | Luiz Capitulino | s->has_parent = true;
|
2365 | f11f57e4 | Luiz Capitulino | s->parent = qmp_query_blockstat(bs->file, NULL);
|
2366 | 294cc35f | Kevin Wolf | } |
2367 | 294cc35f | Kevin Wolf | |
2368 | f11f57e4 | Luiz Capitulino | return s;
|
2369 | 294cc35f | Kevin Wolf | } |
2370 | 294cc35f | Kevin Wolf | |
2371 | f11f57e4 | Luiz Capitulino | BlockStatsList *qmp_query_blockstats(Error **errp) |
2372 | 218a536a | Luiz Capitulino | { |
2373 | f11f57e4 | Luiz Capitulino | BlockStatsList *head = NULL, *cur_item = NULL; |
2374 | a36e69dd | ths | BlockDriverState *bs; |
2375 | a36e69dd | ths | |
2376 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
2377 | f11f57e4 | Luiz Capitulino | BlockStatsList *info = g_malloc0(sizeof(*info));
|
2378 | f11f57e4 | Luiz Capitulino | info->value = qmp_query_blockstat(bs, NULL);
|
2379 | f11f57e4 | Luiz Capitulino | |
2380 | f11f57e4 | Luiz Capitulino | /* XXX: waiting for the qapi to support GSList */
|
2381 | f11f57e4 | Luiz Capitulino | if (!cur_item) {
|
2382 | f11f57e4 | Luiz Capitulino | head = cur_item = info; |
2383 | f11f57e4 | Luiz Capitulino | } else {
|
2384 | f11f57e4 | Luiz Capitulino | cur_item->next = info; |
2385 | f11f57e4 | Luiz Capitulino | cur_item = info; |
2386 | f11f57e4 | Luiz Capitulino | } |
2387 | a36e69dd | ths | } |
2388 | 218a536a | Luiz Capitulino | |
2389 | f11f57e4 | Luiz Capitulino | return head;
|
2390 | a36e69dd | ths | } |
2391 | ea2384d3 | bellard | |
2392 | 045df330 | aliguori | const char *bdrv_get_encrypted_filename(BlockDriverState *bs) |
2393 | 045df330 | aliguori | { |
2394 | 045df330 | aliguori | if (bs->backing_hd && bs->backing_hd->encrypted)
|
2395 | 045df330 | aliguori | return bs->backing_file;
|
2396 | 045df330 | aliguori | else if (bs->encrypted) |
2397 | 045df330 | aliguori | return bs->filename;
|
2398 | 045df330 | aliguori | else
|
2399 | 045df330 | aliguori | return NULL; |
2400 | 045df330 | aliguori | } |
2401 | 045df330 | aliguori | |
2402 | 5fafdf24 | ths | void bdrv_get_backing_filename(BlockDriverState *bs,
|
2403 | 83f64091 | bellard | char *filename, int filename_size) |
2404 | 83f64091 | bellard | { |
2405 | 3574c608 | Kevin Wolf | pstrcpy(filename, filename_size, bs->backing_file); |
2406 | 83f64091 | bellard | } |
2407 | 83f64091 | bellard | |
2408 | 5fafdf24 | ths | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
|
2409 | faea38e7 | bellard | const uint8_t *buf, int nb_sectors) |
2410 | faea38e7 | bellard | { |
2411 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
2412 | faea38e7 | bellard | if (!drv)
|
2413 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2414 | faea38e7 | bellard | if (!drv->bdrv_write_compressed)
|
2415 | faea38e7 | bellard | return -ENOTSUP;
|
2416 | fbb7b4e0 | Kevin Wolf | if (bdrv_check_request(bs, sector_num, nb_sectors))
|
2417 | fbb7b4e0 | Kevin Wolf | return -EIO;
|
2418 | a55eb92c | Jan Kiszka | |
2419 | c6d22830 | Jan Kiszka | if (bs->dirty_bitmap) {
|
2420 | 7cd1e32a | lirans@il.ibm.com | set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
|
2421 | 7cd1e32a | lirans@il.ibm.com | } |
2422 | a55eb92c | Jan Kiszka | |
2423 | faea38e7 | bellard | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
|
2424 | faea38e7 | bellard | } |
2425 | 3b46e624 | ths | |
2426 | faea38e7 | bellard | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
|
2427 | faea38e7 | bellard | { |
2428 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
2429 | faea38e7 | bellard | if (!drv)
|
2430 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2431 | faea38e7 | bellard | if (!drv->bdrv_get_info)
|
2432 | faea38e7 | bellard | return -ENOTSUP;
|
2433 | faea38e7 | bellard | memset(bdi, 0, sizeof(*bdi)); |
2434 | faea38e7 | bellard | return drv->bdrv_get_info(bs, bdi);
|
2435 | faea38e7 | bellard | } |
2436 | faea38e7 | bellard | |
2437 | 45566e9c | Christoph Hellwig | int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, |
2438 | 45566e9c | Christoph Hellwig | int64_t pos, int size)
|
2439 | 178e08a5 | aliguori | { |
2440 | 178e08a5 | aliguori | BlockDriver *drv = bs->drv; |
2441 | 178e08a5 | aliguori | if (!drv)
|
2442 | 178e08a5 | aliguori | return -ENOMEDIUM;
|
2443 | 7cdb1f6d | MORITA Kazutaka | if (drv->bdrv_save_vmstate)
|
2444 | 7cdb1f6d | MORITA Kazutaka | return drv->bdrv_save_vmstate(bs, buf, pos, size);
|
2445 | 7cdb1f6d | MORITA Kazutaka | if (bs->file)
|
2446 | 7cdb1f6d | MORITA Kazutaka | return bdrv_save_vmstate(bs->file, buf, pos, size);
|
2447 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
2448 | 178e08a5 | aliguori | } |
2449 | 178e08a5 | aliguori | |
2450 | 45566e9c | Christoph Hellwig | int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
|
2451 | 45566e9c | Christoph Hellwig | int64_t pos, int size)
|
2452 | 178e08a5 | aliguori | { |
2453 | 178e08a5 | aliguori | BlockDriver *drv = bs->drv; |
2454 | 178e08a5 | aliguori | if (!drv)
|
2455 | 178e08a5 | aliguori | return -ENOMEDIUM;
|
2456 | 7cdb1f6d | MORITA Kazutaka | if (drv->bdrv_load_vmstate)
|
2457 | 7cdb1f6d | MORITA Kazutaka | return drv->bdrv_load_vmstate(bs, buf, pos, size);
|
2458 | 7cdb1f6d | MORITA Kazutaka | if (bs->file)
|
2459 | 7cdb1f6d | MORITA Kazutaka | return bdrv_load_vmstate(bs->file, buf, pos, size);
|
2460 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
2461 | 178e08a5 | aliguori | } |
2462 | 178e08a5 | aliguori | |
2463 | 8b9b0cc2 | Kevin Wolf | void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
|
2464 | 8b9b0cc2 | Kevin Wolf | { |
2465 | 8b9b0cc2 | Kevin Wolf | BlockDriver *drv = bs->drv; |
2466 | 8b9b0cc2 | Kevin Wolf | |
2467 | 8b9b0cc2 | Kevin Wolf | if (!drv || !drv->bdrv_debug_event) {
|
2468 | 8b9b0cc2 | Kevin Wolf | return;
|
2469 | 8b9b0cc2 | Kevin Wolf | } |
2470 | 8b9b0cc2 | Kevin Wolf | |
2471 | 8b9b0cc2 | Kevin Wolf | return drv->bdrv_debug_event(bs, event);
|
2472 | 8b9b0cc2 | Kevin Wolf | |
2473 | 8b9b0cc2 | Kevin Wolf | } |
2474 | 8b9b0cc2 | Kevin Wolf | |
2475 | faea38e7 | bellard | /**************************************************************/
|
2476 | faea38e7 | bellard | /* handling of snapshots */
|
2477 | faea38e7 | bellard | |
2478 | feeee5ac | Miguel Di Ciurcio Filho | int bdrv_can_snapshot(BlockDriverState *bs)
|
2479 | feeee5ac | Miguel Di Ciurcio Filho | { |
2480 | feeee5ac | Miguel Di Ciurcio Filho | BlockDriver *drv = bs->drv; |
2481 | 07b70bfb | Markus Armbruster | if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
|
2482 | feeee5ac | Miguel Di Ciurcio Filho | return 0; |
2483 | feeee5ac | Miguel Di Ciurcio Filho | } |
2484 | feeee5ac | Miguel Di Ciurcio Filho | |
2485 | feeee5ac | Miguel Di Ciurcio Filho | if (!drv->bdrv_snapshot_create) {
|
2486 | feeee5ac | Miguel Di Ciurcio Filho | if (bs->file != NULL) { |
2487 | feeee5ac | Miguel Di Ciurcio Filho | return bdrv_can_snapshot(bs->file);
|
2488 | feeee5ac | Miguel Di Ciurcio Filho | } |
2489 | feeee5ac | Miguel Di Ciurcio Filho | return 0; |
2490 | feeee5ac | Miguel Di Ciurcio Filho | } |
2491 | feeee5ac | Miguel Di Ciurcio Filho | |
2492 | feeee5ac | Miguel Di Ciurcio Filho | return 1; |
2493 | feeee5ac | Miguel Di Ciurcio Filho | } |
2494 | feeee5ac | Miguel Di Ciurcio Filho | |
2495 | 199630b6 | Blue Swirl | int bdrv_is_snapshot(BlockDriverState *bs)
|
2496 | 199630b6 | Blue Swirl | { |
2497 | 199630b6 | Blue Swirl | return !!(bs->open_flags & BDRV_O_SNAPSHOT);
|
2498 | 199630b6 | Blue Swirl | } |
2499 | 199630b6 | Blue Swirl | |
2500 | f9092b10 | Markus Armbruster | BlockDriverState *bdrv_snapshots(void)
|
2501 | f9092b10 | Markus Armbruster | { |
2502 | f9092b10 | Markus Armbruster | BlockDriverState *bs; |
2503 | f9092b10 | Markus Armbruster | |
2504 | 3ac906f7 | Markus Armbruster | if (bs_snapshots) {
|
2505 | f9092b10 | Markus Armbruster | return bs_snapshots;
|
2506 | 3ac906f7 | Markus Armbruster | } |
2507 | f9092b10 | Markus Armbruster | |
2508 | f9092b10 | Markus Armbruster | bs = NULL;
|
2509 | f9092b10 | Markus Armbruster | while ((bs = bdrv_next(bs))) {
|
2510 | f9092b10 | Markus Armbruster | if (bdrv_can_snapshot(bs)) {
|
2511 | 3ac906f7 | Markus Armbruster | bs_snapshots = bs; |
2512 | 3ac906f7 | Markus Armbruster | return bs;
|
2513 | f9092b10 | Markus Armbruster | } |
2514 | f9092b10 | Markus Armbruster | } |
2515 | f9092b10 | Markus Armbruster | return NULL; |
2516 | f9092b10 | Markus Armbruster | } |
2517 | f9092b10 | Markus Armbruster | |
2518 | 5fafdf24 | ths | int bdrv_snapshot_create(BlockDriverState *bs,
|
2519 | faea38e7 | bellard | QEMUSnapshotInfo *sn_info) |
2520 | faea38e7 | bellard | { |
2521 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
2522 | faea38e7 | bellard | if (!drv)
|
2523 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2524 | 7cdb1f6d | MORITA Kazutaka | if (drv->bdrv_snapshot_create)
|
2525 | 7cdb1f6d | MORITA Kazutaka | return drv->bdrv_snapshot_create(bs, sn_info);
|
2526 | 7cdb1f6d | MORITA Kazutaka | if (bs->file)
|
2527 | 7cdb1f6d | MORITA Kazutaka | return bdrv_snapshot_create(bs->file, sn_info);
|
2528 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
2529 | faea38e7 | bellard | } |
2530 | faea38e7 | bellard | |
2531 | 5fafdf24 | ths | int bdrv_snapshot_goto(BlockDriverState *bs,
|
2532 | faea38e7 | bellard | const char *snapshot_id) |
2533 | faea38e7 | bellard | { |
2534 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
2535 | 7cdb1f6d | MORITA Kazutaka | int ret, open_ret;
|
2536 | 7cdb1f6d | MORITA Kazutaka | |
2537 | faea38e7 | bellard | if (!drv)
|
2538 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2539 | 7cdb1f6d | MORITA Kazutaka | if (drv->bdrv_snapshot_goto)
|
2540 | 7cdb1f6d | MORITA Kazutaka | return drv->bdrv_snapshot_goto(bs, snapshot_id);
|
2541 | 7cdb1f6d | MORITA Kazutaka | |
2542 | 7cdb1f6d | MORITA Kazutaka | if (bs->file) {
|
2543 | 7cdb1f6d | MORITA Kazutaka | drv->bdrv_close(bs); |
2544 | 7cdb1f6d | MORITA Kazutaka | ret = bdrv_snapshot_goto(bs->file, snapshot_id); |
2545 | 7cdb1f6d | MORITA Kazutaka | open_ret = drv->bdrv_open(bs, bs->open_flags); |
2546 | 7cdb1f6d | MORITA Kazutaka | if (open_ret < 0) { |
2547 | 7cdb1f6d | MORITA Kazutaka | bdrv_delete(bs->file); |
2548 | 7cdb1f6d | MORITA Kazutaka | bs->drv = NULL;
|
2549 | 7cdb1f6d | MORITA Kazutaka | return open_ret;
|
2550 | 7cdb1f6d | MORITA Kazutaka | } |
2551 | 7cdb1f6d | MORITA Kazutaka | return ret;
|
2552 | 7cdb1f6d | MORITA Kazutaka | } |
2553 | 7cdb1f6d | MORITA Kazutaka | |
2554 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
2555 | faea38e7 | bellard | } |
2556 | faea38e7 | bellard | |
2557 | faea38e7 | bellard | int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) |
2558 | faea38e7 | bellard | { |
2559 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
2560 | faea38e7 | bellard | if (!drv)
|
2561 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2562 | 7cdb1f6d | MORITA Kazutaka | if (drv->bdrv_snapshot_delete)
|
2563 | 7cdb1f6d | MORITA Kazutaka | return drv->bdrv_snapshot_delete(bs, snapshot_id);
|
2564 | 7cdb1f6d | MORITA Kazutaka | if (bs->file)
|
2565 | 7cdb1f6d | MORITA Kazutaka | return bdrv_snapshot_delete(bs->file, snapshot_id);
|
2566 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
2567 | faea38e7 | bellard | } |
2568 | faea38e7 | bellard | |
2569 | 5fafdf24 | ths | int bdrv_snapshot_list(BlockDriverState *bs,
|
2570 | faea38e7 | bellard | QEMUSnapshotInfo **psn_info) |
2571 | faea38e7 | bellard | { |
2572 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
2573 | faea38e7 | bellard | if (!drv)
|
2574 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2575 | 7cdb1f6d | MORITA Kazutaka | if (drv->bdrv_snapshot_list)
|
2576 | 7cdb1f6d | MORITA Kazutaka | return drv->bdrv_snapshot_list(bs, psn_info);
|
2577 | 7cdb1f6d | MORITA Kazutaka | if (bs->file)
|
2578 | 7cdb1f6d | MORITA Kazutaka | return bdrv_snapshot_list(bs->file, psn_info);
|
2579 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
2580 | faea38e7 | bellard | } |
2581 | faea38e7 | bellard | |
2582 | 51ef6727 | edison | int bdrv_snapshot_load_tmp(BlockDriverState *bs,
|
2583 | 51ef6727 | edison | const char *snapshot_name) |
2584 | 51ef6727 | edison | { |
2585 | 51ef6727 | edison | BlockDriver *drv = bs->drv; |
2586 | 51ef6727 | edison | if (!drv) {
|
2587 | 51ef6727 | edison | return -ENOMEDIUM;
|
2588 | 51ef6727 | edison | } |
2589 | 51ef6727 | edison | if (!bs->read_only) {
|
2590 | 51ef6727 | edison | return -EINVAL;
|
2591 | 51ef6727 | edison | } |
2592 | 51ef6727 | edison | if (drv->bdrv_snapshot_load_tmp) {
|
2593 | 51ef6727 | edison | return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
|
2594 | 51ef6727 | edison | } |
2595 | 51ef6727 | edison | return -ENOTSUP;
|
2596 | 51ef6727 | edison | } |
2597 | 51ef6727 | edison | |
2598 | e8a6bb9c | Marcelo Tosatti | BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, |
2599 | e8a6bb9c | Marcelo Tosatti | const char *backing_file) |
2600 | e8a6bb9c | Marcelo Tosatti | { |
2601 | e8a6bb9c | Marcelo Tosatti | if (!bs->drv) {
|
2602 | e8a6bb9c | Marcelo Tosatti | return NULL; |
2603 | e8a6bb9c | Marcelo Tosatti | } |
2604 | e8a6bb9c | Marcelo Tosatti | |
2605 | e8a6bb9c | Marcelo Tosatti | if (bs->backing_hd) {
|
2606 | e8a6bb9c | Marcelo Tosatti | if (strcmp(bs->backing_file, backing_file) == 0) { |
2607 | e8a6bb9c | Marcelo Tosatti | return bs->backing_hd;
|
2608 | e8a6bb9c | Marcelo Tosatti | } else {
|
2609 | e8a6bb9c | Marcelo Tosatti | return bdrv_find_backing_image(bs->backing_hd, backing_file);
|
2610 | e8a6bb9c | Marcelo Tosatti | } |
2611 | e8a6bb9c | Marcelo Tosatti | } |
2612 | e8a6bb9c | Marcelo Tosatti | |
2613 | e8a6bb9c | Marcelo Tosatti | return NULL; |
2614 | e8a6bb9c | Marcelo Tosatti | } |
2615 | e8a6bb9c | Marcelo Tosatti | |
2616 | faea38e7 | bellard | #define NB_SUFFIXES 4 |
2617 | faea38e7 | bellard | |
2618 | faea38e7 | bellard | char *get_human_readable_size(char *buf, int buf_size, int64_t size) |
2619 | faea38e7 | bellard | { |
2620 | faea38e7 | bellard | static const char suffixes[NB_SUFFIXES] = "KMGT"; |
2621 | faea38e7 | bellard | int64_t base; |
2622 | faea38e7 | bellard | int i;
|
2623 | faea38e7 | bellard | |
2624 | faea38e7 | bellard | if (size <= 999) { |
2625 | faea38e7 | bellard | snprintf(buf, buf_size, "%" PRId64, size);
|
2626 | faea38e7 | bellard | } else {
|
2627 | faea38e7 | bellard | base = 1024;
|
2628 | faea38e7 | bellard | for(i = 0; i < NB_SUFFIXES; i++) { |
2629 | faea38e7 | bellard | if (size < (10 * base)) { |
2630 | 5fafdf24 | ths | snprintf(buf, buf_size, "%0.1f%c",
|
2631 | faea38e7 | bellard | (double)size / base,
|
2632 | faea38e7 | bellard | suffixes[i]); |
2633 | faea38e7 | bellard | break;
|
2634 | faea38e7 | bellard | } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { |
2635 | 5fafdf24 | ths | snprintf(buf, buf_size, "%" PRId64 "%c", |
2636 | faea38e7 | bellard | ((size + (base >> 1)) / base),
|
2637 | faea38e7 | bellard | suffixes[i]); |
2638 | faea38e7 | bellard | break;
|
2639 | faea38e7 | bellard | } |
2640 | faea38e7 | bellard | base = base * 1024;
|
2641 | faea38e7 | bellard | } |
2642 | faea38e7 | bellard | } |
2643 | faea38e7 | bellard | return buf;
|
2644 | faea38e7 | bellard | } |
2645 | faea38e7 | bellard | |
2646 | faea38e7 | bellard | char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) |
2647 | faea38e7 | bellard | { |
2648 | faea38e7 | bellard | char buf1[128], date_buf[128], clock_buf[128]; |
2649 | 3b9f94e1 | bellard | #ifdef _WIN32
|
2650 | 3b9f94e1 | bellard | struct tm *ptm;
|
2651 | 3b9f94e1 | bellard | #else
|
2652 | faea38e7 | bellard | struct tm tm;
|
2653 | 3b9f94e1 | bellard | #endif
|
2654 | faea38e7 | bellard | time_t ti; |
2655 | faea38e7 | bellard | int64_t secs; |
2656 | faea38e7 | bellard | |
2657 | faea38e7 | bellard | if (!sn) {
|
2658 | 5fafdf24 | ths | snprintf(buf, buf_size, |
2659 | 5fafdf24 | ths | "%-10s%-20s%7s%20s%15s",
|
2660 | faea38e7 | bellard | "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); |
2661 | faea38e7 | bellard | } else {
|
2662 | faea38e7 | bellard | ti = sn->date_sec; |
2663 | 3b9f94e1 | bellard | #ifdef _WIN32
|
2664 | 3b9f94e1 | bellard | ptm = localtime(&ti); |
2665 | 3b9f94e1 | bellard | strftime(date_buf, sizeof(date_buf),
|
2666 | 3b9f94e1 | bellard | "%Y-%m-%d %H:%M:%S", ptm);
|
2667 | 3b9f94e1 | bellard | #else
|
2668 | faea38e7 | bellard | localtime_r(&ti, &tm); |
2669 | faea38e7 | bellard | strftime(date_buf, sizeof(date_buf),
|
2670 | faea38e7 | bellard | "%Y-%m-%d %H:%M:%S", &tm);
|
2671 | 3b9f94e1 | bellard | #endif
|
2672 | faea38e7 | bellard | secs = sn->vm_clock_nsec / 1000000000;
|
2673 | faea38e7 | bellard | snprintf(clock_buf, sizeof(clock_buf),
|
2674 | faea38e7 | bellard | "%02d:%02d:%02d.%03d",
|
2675 | faea38e7 | bellard | (int)(secs / 3600), |
2676 | faea38e7 | bellard | (int)((secs / 60) % 60), |
2677 | 5fafdf24 | ths | (int)(secs % 60), |
2678 | faea38e7 | bellard | (int)((sn->vm_clock_nsec / 1000000) % 1000)); |
2679 | faea38e7 | bellard | snprintf(buf, buf_size, |
2680 | 5fafdf24 | ths | "%-10s%-20s%7s%20s%15s",
|
2681 | faea38e7 | bellard | sn->id_str, sn->name, |
2682 | faea38e7 | bellard | get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
|
2683 | faea38e7 | bellard | date_buf, |
2684 | faea38e7 | bellard | clock_buf); |
2685 | faea38e7 | bellard | } |
2686 | faea38e7 | bellard | return buf;
|
2687 | faea38e7 | bellard | } |
2688 | faea38e7 | bellard | |
2689 | ea2384d3 | bellard | /**************************************************************/
|
2690 | 83f64091 | bellard | /* async I/Os */
|
2691 | ea2384d3 | bellard | |
2692 | 3b69e4b9 | aliguori | BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, |
2693 | f141eafe | aliguori | QEMUIOVector *qiov, int nb_sectors,
|
2694 | 3b69e4b9 | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
2695 | 3b69e4b9 | aliguori | { |
2696 | bbf0a440 | Stefan Hajnoczi | trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); |
2697 | bbf0a440 | Stefan Hajnoczi | |
2698 | b2a61371 | Stefan Hajnoczi | return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors,
|
2699 | 8c5873d6 | Stefan Hajnoczi | cb, opaque, false);
|
2700 | ea2384d3 | bellard | } |
2701 | ea2384d3 | bellard | |
2702 | f141eafe | aliguori | BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, |
2703 | f141eafe | aliguori | QEMUIOVector *qiov, int nb_sectors,
|
2704 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
2705 | ea2384d3 | bellard | { |
2706 | bbf0a440 | Stefan Hajnoczi | trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); |
2707 | bbf0a440 | Stefan Hajnoczi | |
2708 | 1a6e115b | Stefan Hajnoczi | return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors,
|
2709 | 8c5873d6 | Stefan Hajnoczi | cb, opaque, true);
|
2710 | 83f64091 | bellard | } |
2711 | 83f64091 | bellard | |
2712 | 40b4f539 | Kevin Wolf | |
2713 | 40b4f539 | Kevin Wolf | typedef struct MultiwriteCB { |
2714 | 40b4f539 | Kevin Wolf | int error;
|
2715 | 40b4f539 | Kevin Wolf | int num_requests;
|
2716 | 40b4f539 | Kevin Wolf | int num_callbacks;
|
2717 | 40b4f539 | Kevin Wolf | struct {
|
2718 | 40b4f539 | Kevin Wolf | BlockDriverCompletionFunc *cb; |
2719 | 40b4f539 | Kevin Wolf | void *opaque;
|
2720 | 40b4f539 | Kevin Wolf | QEMUIOVector *free_qiov; |
2721 | 40b4f539 | Kevin Wolf | void *free_buf;
|
2722 | 40b4f539 | Kevin Wolf | } callbacks[]; |
2723 | 40b4f539 | Kevin Wolf | } MultiwriteCB; |
2724 | 40b4f539 | Kevin Wolf | |
2725 | 40b4f539 | Kevin Wolf | static void multiwrite_user_cb(MultiwriteCB *mcb) |
2726 | 40b4f539 | Kevin Wolf | { |
2727 | 40b4f539 | Kevin Wolf | int i;
|
2728 | 40b4f539 | Kevin Wolf | |
2729 | 40b4f539 | Kevin Wolf | for (i = 0; i < mcb->num_callbacks; i++) { |
2730 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); |
2731 | 1e1ea48d | Stefan Hajnoczi | if (mcb->callbacks[i].free_qiov) {
|
2732 | 1e1ea48d | Stefan Hajnoczi | qemu_iovec_destroy(mcb->callbacks[i].free_qiov); |
2733 | 1e1ea48d | Stefan Hajnoczi | } |
2734 | 7267c094 | Anthony Liguori | g_free(mcb->callbacks[i].free_qiov); |
2735 | f8a83245 | Herve Poussineau | qemu_vfree(mcb->callbacks[i].free_buf); |
2736 | 40b4f539 | Kevin Wolf | } |
2737 | 40b4f539 | Kevin Wolf | } |
2738 | 40b4f539 | Kevin Wolf | |
2739 | 40b4f539 | Kevin Wolf | static void multiwrite_cb(void *opaque, int ret) |
2740 | 40b4f539 | Kevin Wolf | { |
2741 | 40b4f539 | Kevin Wolf | MultiwriteCB *mcb = opaque; |
2742 | 40b4f539 | Kevin Wolf | |
2743 | 6d519a5f | Stefan Hajnoczi | trace_multiwrite_cb(mcb, ret); |
2744 | 6d519a5f | Stefan Hajnoczi | |
2745 | cb6d3ca0 | Kevin Wolf | if (ret < 0 && !mcb->error) { |
2746 | 40b4f539 | Kevin Wolf | mcb->error = ret; |
2747 | 40b4f539 | Kevin Wolf | } |
2748 | 40b4f539 | Kevin Wolf | |
2749 | 40b4f539 | Kevin Wolf | mcb->num_requests--; |
2750 | 40b4f539 | Kevin Wolf | if (mcb->num_requests == 0) { |
2751 | de189a1b | Kevin Wolf | multiwrite_user_cb(mcb); |
2752 | 7267c094 | Anthony Liguori | g_free(mcb); |
2753 | 40b4f539 | Kevin Wolf | } |
2754 | 40b4f539 | Kevin Wolf | } |
2755 | 40b4f539 | Kevin Wolf | |
2756 | 40b4f539 | Kevin Wolf | static int multiwrite_req_compare(const void *a, const void *b) |
2757 | 40b4f539 | Kevin Wolf | { |
2758 | 77be4366 | Christoph Hellwig | const BlockRequest *req1 = a, *req2 = b;
|
2759 | 77be4366 | Christoph Hellwig | |
2760 | 77be4366 | Christoph Hellwig | /*
|
2761 | 77be4366 | Christoph Hellwig | * Note that we can't simply subtract req2->sector from req1->sector
|
2762 | 77be4366 | Christoph Hellwig | * here as that could overflow the return value.
|
2763 | 77be4366 | Christoph Hellwig | */
|
2764 | 77be4366 | Christoph Hellwig | if (req1->sector > req2->sector) {
|
2765 | 77be4366 | Christoph Hellwig | return 1; |
2766 | 77be4366 | Christoph Hellwig | } else if (req1->sector < req2->sector) { |
2767 | 77be4366 | Christoph Hellwig | return -1; |
2768 | 77be4366 | Christoph Hellwig | } else {
|
2769 | 77be4366 | Christoph Hellwig | return 0; |
2770 | 77be4366 | Christoph Hellwig | } |
2771 | 40b4f539 | Kevin Wolf | } |
2772 | 40b4f539 | Kevin Wolf | |
2773 | 40b4f539 | Kevin Wolf | /*
|
2774 | 40b4f539 | Kevin Wolf | * Takes a bunch of requests and tries to merge them. Returns the number of
|
2775 | 40b4f539 | Kevin Wolf | * requests that remain after merging.
|
2776 | 40b4f539 | Kevin Wolf | */
|
2777 | 40b4f539 | Kevin Wolf | static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, |
2778 | 40b4f539 | Kevin Wolf | int num_reqs, MultiwriteCB *mcb)
|
2779 | 40b4f539 | Kevin Wolf | { |
2780 | 40b4f539 | Kevin Wolf | int i, outidx;
|
2781 | 40b4f539 | Kevin Wolf | |
2782 | 40b4f539 | Kevin Wolf | // Sort requests by start sector
|
2783 | 40b4f539 | Kevin Wolf | qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
|
2784 | 40b4f539 | Kevin Wolf | |
2785 | 40b4f539 | Kevin Wolf | // Check if adjacent requests touch the same clusters. If so, combine them,
|
2786 | 40b4f539 | Kevin Wolf | // filling up gaps with zero sectors.
|
2787 | 40b4f539 | Kevin Wolf | outidx = 0;
|
2788 | 40b4f539 | Kevin Wolf | for (i = 1; i < num_reqs; i++) { |
2789 | 40b4f539 | Kevin Wolf | int merge = 0; |
2790 | 40b4f539 | Kevin Wolf | int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; |
2791 | 40b4f539 | Kevin Wolf | |
2792 | 40b4f539 | Kevin Wolf | // This handles the cases that are valid for all block drivers, namely
|
2793 | 40b4f539 | Kevin Wolf | // exactly sequential writes and overlapping writes.
|
2794 | 40b4f539 | Kevin Wolf | if (reqs[i].sector <= oldreq_last) {
|
2795 | 40b4f539 | Kevin Wolf | merge = 1;
|
2796 | 40b4f539 | Kevin Wolf | } |
2797 | 40b4f539 | Kevin Wolf | |
2798 | 40b4f539 | Kevin Wolf | // The block driver may decide that it makes sense to combine requests
|
2799 | 40b4f539 | Kevin Wolf | // even if there is a gap of some sectors between them. In this case,
|
2800 | 40b4f539 | Kevin Wolf | // the gap is filled with zeros (therefore only applicable for yet
|
2801 | 40b4f539 | Kevin Wolf | // unused space in format like qcow2).
|
2802 | 40b4f539 | Kevin Wolf | if (!merge && bs->drv->bdrv_merge_requests) {
|
2803 | 40b4f539 | Kevin Wolf | merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]); |
2804 | 40b4f539 | Kevin Wolf | } |
2805 | 40b4f539 | Kevin Wolf | |
2806 | e2a305fb | Christoph Hellwig | if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { |
2807 | e2a305fb | Christoph Hellwig | merge = 0;
|
2808 | e2a305fb | Christoph Hellwig | } |
2809 | e2a305fb | Christoph Hellwig | |
2810 | 40b4f539 | Kevin Wolf | if (merge) {
|
2811 | 40b4f539 | Kevin Wolf | size_t size; |
2812 | 7267c094 | Anthony Liguori | QEMUIOVector *qiov = g_malloc0(sizeof(*qiov));
|
2813 | 40b4f539 | Kevin Wolf | qemu_iovec_init(qiov, |
2814 | 40b4f539 | Kevin Wolf | reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
|
2815 | 40b4f539 | Kevin Wolf | |
2816 | 40b4f539 | Kevin Wolf | // Add the first request to the merged one. If the requests are
|
2817 | 40b4f539 | Kevin Wolf | // overlapping, drop the last sectors of the first request.
|
2818 | 40b4f539 | Kevin Wolf | size = (reqs[i].sector - reqs[outidx].sector) << 9;
|
2819 | 40b4f539 | Kevin Wolf | qemu_iovec_concat(qiov, reqs[outidx].qiov, size); |
2820 | 40b4f539 | Kevin Wolf | |
2821 | 40b4f539 | Kevin Wolf | // We might need to add some zeros between the two requests
|
2822 | 40b4f539 | Kevin Wolf | if (reqs[i].sector > oldreq_last) {
|
2823 | 40b4f539 | Kevin Wolf | size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
|
2824 | 40b4f539 | Kevin Wolf | uint8_t *buf = qemu_blockalign(bs, zero_bytes); |
2825 | 40b4f539 | Kevin Wolf | memset(buf, 0, zero_bytes);
|
2826 | 40b4f539 | Kevin Wolf | qemu_iovec_add(qiov, buf, zero_bytes); |
2827 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].free_buf = buf; |
2828 | 40b4f539 | Kevin Wolf | } |
2829 | 40b4f539 | Kevin Wolf | |
2830 | 40b4f539 | Kevin Wolf | // Add the second request
|
2831 | 40b4f539 | Kevin Wolf | qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size); |
2832 | 40b4f539 | Kevin Wolf | |
2833 | cbf1dff2 | Kevin Wolf | reqs[outidx].nb_sectors = qiov->size >> 9;
|
2834 | 40b4f539 | Kevin Wolf | reqs[outidx].qiov = qiov; |
2835 | 40b4f539 | Kevin Wolf | |
2836 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].free_qiov = reqs[outidx].qiov; |
2837 | 40b4f539 | Kevin Wolf | } else {
|
2838 | 40b4f539 | Kevin Wolf | outidx++; |
2839 | 40b4f539 | Kevin Wolf | reqs[outidx].sector = reqs[i].sector; |
2840 | 40b4f539 | Kevin Wolf | reqs[outidx].nb_sectors = reqs[i].nb_sectors; |
2841 | 40b4f539 | Kevin Wolf | reqs[outidx].qiov = reqs[i].qiov; |
2842 | 40b4f539 | Kevin Wolf | } |
2843 | 40b4f539 | Kevin Wolf | } |
2844 | 40b4f539 | Kevin Wolf | |
2845 | 40b4f539 | Kevin Wolf | return outidx + 1; |
2846 | 40b4f539 | Kevin Wolf | } |
2847 | 40b4f539 | Kevin Wolf | |
2848 | 40b4f539 | Kevin Wolf | /*
|
2849 | 40b4f539 | Kevin Wolf | * Submit multiple AIO write requests at once.
|
2850 | 40b4f539 | Kevin Wolf | *
|
2851 | 40b4f539 | Kevin Wolf | * On success, the function returns 0 and all requests in the reqs array have
|
2852 | 40b4f539 | Kevin Wolf | * been submitted. In error case this function returns -1, and any of the
|
2853 | 40b4f539 | Kevin Wolf | * requests may or may not be submitted yet. In particular, this means that the
|
2854 | 40b4f539 | Kevin Wolf | * callback will be called for some of the requests, for others it won't. The
|
2855 | 40b4f539 | Kevin Wolf | * caller must check the error field of the BlockRequest to wait for the right
|
2856 | 40b4f539 | Kevin Wolf | * callbacks (if error != 0, no callback will be called).
|
2857 | 40b4f539 | Kevin Wolf | *
|
2858 | 40b4f539 | Kevin Wolf | * The implementation may modify the contents of the reqs array, e.g. to merge
|
2859 | 40b4f539 | Kevin Wolf | * requests. However, the fields opaque and error are left unmodified as they
|
2860 | 40b4f539 | Kevin Wolf | * are used to signal failure for a single request to the caller.
|
2861 | 40b4f539 | Kevin Wolf | */
|
2862 | 40b4f539 | Kevin Wolf | int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) |
2863 | 40b4f539 | Kevin Wolf | { |
2864 | 40b4f539 | Kevin Wolf | MultiwriteCB *mcb; |
2865 | 40b4f539 | Kevin Wolf | int i;
|
2866 | 40b4f539 | Kevin Wolf | |
2867 | 301db7c2 | Ryan Harper | /* don't submit writes if we don't have a medium */
|
2868 | 301db7c2 | Ryan Harper | if (bs->drv == NULL) { |
2869 | 301db7c2 | Ryan Harper | for (i = 0; i < num_reqs; i++) { |
2870 | 301db7c2 | Ryan Harper | reqs[i].error = -ENOMEDIUM; |
2871 | 301db7c2 | Ryan Harper | } |
2872 | 301db7c2 | Ryan Harper | return -1; |
2873 | 301db7c2 | Ryan Harper | } |
2874 | 301db7c2 | Ryan Harper | |
2875 | 40b4f539 | Kevin Wolf | if (num_reqs == 0) { |
2876 | 40b4f539 | Kevin Wolf | return 0; |
2877 | 40b4f539 | Kevin Wolf | } |
2878 | 40b4f539 | Kevin Wolf | |
2879 | 40b4f539 | Kevin Wolf | // Create MultiwriteCB structure
|
2880 | 7267c094 | Anthony Liguori | mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); |
2881 | 40b4f539 | Kevin Wolf | mcb->num_requests = 0;
|
2882 | 40b4f539 | Kevin Wolf | mcb->num_callbacks = num_reqs; |
2883 | 40b4f539 | Kevin Wolf | |
2884 | 40b4f539 | Kevin Wolf | for (i = 0; i < num_reqs; i++) { |
2885 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].cb = reqs[i].cb; |
2886 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].opaque = reqs[i].opaque; |
2887 | 40b4f539 | Kevin Wolf | } |
2888 | 40b4f539 | Kevin Wolf | |
2889 | 40b4f539 | Kevin Wolf | // Check for mergable requests
|
2890 | 40b4f539 | Kevin Wolf | num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); |
2891 | 40b4f539 | Kevin Wolf | |
2892 | 6d519a5f | Stefan Hajnoczi | trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); |
2893 | 6d519a5f | Stefan Hajnoczi | |
2894 | df9309fb | Paolo Bonzini | /* Run the aio requests. */
|
2895 | df9309fb | Paolo Bonzini | mcb->num_requests = num_reqs; |
2896 | 40b4f539 | Kevin Wolf | for (i = 0; i < num_reqs; i++) { |
2897 | ad54ae80 | Paolo Bonzini | bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, |
2898 | 40b4f539 | Kevin Wolf | reqs[i].nb_sectors, multiwrite_cb, mcb); |
2899 | 40b4f539 | Kevin Wolf | } |
2900 | 40b4f539 | Kevin Wolf | |
2901 | 40b4f539 | Kevin Wolf | return 0; |
2902 | 40b4f539 | Kevin Wolf | } |
2903 | 40b4f539 | Kevin Wolf | |
2904 | 83f64091 | bellard | void bdrv_aio_cancel(BlockDriverAIOCB *acb)
|
2905 | 83f64091 | bellard | { |
2906 | 6bbff9a0 | aliguori | acb->pool->cancel(acb); |
2907 | 83f64091 | bellard | } |
2908 | 83f64091 | bellard | |
2909 | 98f90dba | Zhi Yong Wu | /* block I/O throttling */
|
2910 | 98f90dba | Zhi Yong Wu | static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors, |
2911 | 98f90dba | Zhi Yong Wu | bool is_write, double elapsed_time, uint64_t *wait) |
2912 | 98f90dba | Zhi Yong Wu | { |
2913 | 98f90dba | Zhi Yong Wu | uint64_t bps_limit = 0;
|
2914 | 98f90dba | Zhi Yong Wu | double bytes_limit, bytes_base, bytes_res;
|
2915 | 98f90dba | Zhi Yong Wu | double slice_time, wait_time;
|
2916 | 98f90dba | Zhi Yong Wu | |
2917 | 98f90dba | Zhi Yong Wu | if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) {
|
2918 | 98f90dba | Zhi Yong Wu | bps_limit = bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; |
2919 | 98f90dba | Zhi Yong Wu | } else if (bs->io_limits.bps[is_write]) { |
2920 | 98f90dba | Zhi Yong Wu | bps_limit = bs->io_limits.bps[is_write]; |
2921 | 98f90dba | Zhi Yong Wu | } else {
|
2922 | 98f90dba | Zhi Yong Wu | if (wait) {
|
2923 | 98f90dba | Zhi Yong Wu | *wait = 0;
|
2924 | 98f90dba | Zhi Yong Wu | } |
2925 | 98f90dba | Zhi Yong Wu | |
2926 | 98f90dba | Zhi Yong Wu | return false; |
2927 | 98f90dba | Zhi Yong Wu | } |
2928 | 98f90dba | Zhi Yong Wu | |
2929 | 98f90dba | Zhi Yong Wu | slice_time = bs->slice_end - bs->slice_start; |
2930 | 98f90dba | Zhi Yong Wu | slice_time /= (NANOSECONDS_PER_SECOND); |
2931 | 98f90dba | Zhi Yong Wu | bytes_limit = bps_limit * slice_time; |
2932 | 98f90dba | Zhi Yong Wu | bytes_base = bs->nr_bytes[is_write] - bs->io_base.bytes[is_write]; |
2933 | 98f90dba | Zhi Yong Wu | if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) {
|
2934 | 98f90dba | Zhi Yong Wu | bytes_base += bs->nr_bytes[!is_write] - bs->io_base.bytes[!is_write]; |
2935 | 98f90dba | Zhi Yong Wu | } |
2936 | 98f90dba | Zhi Yong Wu | |
2937 | 98f90dba | Zhi Yong Wu | /* bytes_base: the bytes of data which have been read/written; and
|
2938 | 98f90dba | Zhi Yong Wu | * it is obtained from the history statistic info.
|
2939 | 98f90dba | Zhi Yong Wu | * bytes_res: the remaining bytes of data which need to be read/written.
|
2940 | 98f90dba | Zhi Yong Wu | * (bytes_base + bytes_res) / bps_limit: used to calcuate
|
2941 | 98f90dba | Zhi Yong Wu | * the total time for completing reading/writting all data.
|
2942 | 98f90dba | Zhi Yong Wu | */
|
2943 | 98f90dba | Zhi Yong Wu | bytes_res = (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
|
2944 | 98f90dba | Zhi Yong Wu | |
2945 | 98f90dba | Zhi Yong Wu | if (bytes_base + bytes_res <= bytes_limit) {
|
2946 | 98f90dba | Zhi Yong Wu | if (wait) {
|
2947 | 98f90dba | Zhi Yong Wu | *wait = 0;
|
2948 | 98f90dba | Zhi Yong Wu | } |
2949 | 98f90dba | Zhi Yong Wu | |
2950 | 98f90dba | Zhi Yong Wu | return false; |
2951 | 98f90dba | Zhi Yong Wu | } |
2952 | 98f90dba | Zhi Yong Wu | |
2953 | 98f90dba | Zhi Yong Wu | /* Calc approx time to dispatch */
|
2954 | 98f90dba | Zhi Yong Wu | wait_time = (bytes_base + bytes_res) / bps_limit - elapsed_time; |
2955 | 98f90dba | Zhi Yong Wu | |
2956 | 98f90dba | Zhi Yong Wu | /* When the I/O rate at runtime exceeds the limits,
|
2957 | 98f90dba | Zhi Yong Wu | * bs->slice_end need to be extended in order that the current statistic
|
2958 | 98f90dba | Zhi Yong Wu | * info can be kept until the timer fire, so it is increased and tuned
|
2959 | 98f90dba | Zhi Yong Wu | * based on the result of experiment.
|
2960 | 98f90dba | Zhi Yong Wu | */
|
2961 | 98f90dba | Zhi Yong Wu | bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10;
|
2962 | 98f90dba | Zhi Yong Wu | bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME;
|
2963 | 98f90dba | Zhi Yong Wu | if (wait) {
|
2964 | 98f90dba | Zhi Yong Wu | *wait = wait_time * BLOCK_IO_SLICE_TIME * 10;
|
2965 | 98f90dba | Zhi Yong Wu | } |
2966 | 98f90dba | Zhi Yong Wu | |
2967 | 98f90dba | Zhi Yong Wu | return true; |
2968 | 98f90dba | Zhi Yong Wu | } |
2969 | 98f90dba | Zhi Yong Wu | |
2970 | 98f90dba | Zhi Yong Wu | static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write, |
2971 | 98f90dba | Zhi Yong Wu | double elapsed_time, uint64_t *wait)
|
2972 | 98f90dba | Zhi Yong Wu | { |
2973 | 98f90dba | Zhi Yong Wu | uint64_t iops_limit = 0;
|
2974 | 98f90dba | Zhi Yong Wu | double ios_limit, ios_base;
|
2975 | 98f90dba | Zhi Yong Wu | double slice_time, wait_time;
|
2976 | 98f90dba | Zhi Yong Wu | |
2977 | 98f90dba | Zhi Yong Wu | if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) {
|
2978 | 98f90dba | Zhi Yong Wu | iops_limit = bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; |
2979 | 98f90dba | Zhi Yong Wu | } else if (bs->io_limits.iops[is_write]) { |
2980 | 98f90dba | Zhi Yong Wu | iops_limit = bs->io_limits.iops[is_write]; |
2981 | 98f90dba | Zhi Yong Wu | } else {
|
2982 | 98f90dba | Zhi Yong Wu | if (wait) {
|
2983 | 98f90dba | Zhi Yong Wu | *wait = 0;
|
2984 | 98f90dba | Zhi Yong Wu | } |
2985 | 98f90dba | Zhi Yong Wu | |
2986 | 98f90dba | Zhi Yong Wu | return false; |
2987 | 98f90dba | Zhi Yong Wu | } |
2988 | 98f90dba | Zhi Yong Wu | |
2989 | 98f90dba | Zhi Yong Wu | slice_time = bs->slice_end - bs->slice_start; |
2990 | 98f90dba | Zhi Yong Wu | slice_time /= (NANOSECONDS_PER_SECOND); |
2991 | 98f90dba | Zhi Yong Wu | ios_limit = iops_limit * slice_time; |
2992 | 98f90dba | Zhi Yong Wu | ios_base = bs->nr_ops[is_write] - bs->io_base.ios[is_write]; |
2993 | 98f90dba | Zhi Yong Wu | if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) {
|
2994 | 98f90dba | Zhi Yong Wu | ios_base += bs->nr_ops[!is_write] - bs->io_base.ios[!is_write]; |
2995 | 98f90dba | Zhi Yong Wu | } |
2996 | 98f90dba | Zhi Yong Wu | |
2997 | 98f90dba | Zhi Yong Wu | if (ios_base + 1 <= ios_limit) { |
2998 | 98f90dba | Zhi Yong Wu | if (wait) {
|
2999 | 98f90dba | Zhi Yong Wu | *wait = 0;
|
3000 | 98f90dba | Zhi Yong Wu | } |
3001 | 98f90dba | Zhi Yong Wu | |
3002 | 98f90dba | Zhi Yong Wu | return false; |
3003 | 98f90dba | Zhi Yong Wu | } |
3004 | 98f90dba | Zhi Yong Wu | |
3005 | 98f90dba | Zhi Yong Wu | /* Calc approx time to dispatch */
|
3006 | 98f90dba | Zhi Yong Wu | wait_time = (ios_base + 1) / iops_limit;
|
3007 | 98f90dba | Zhi Yong Wu | if (wait_time > elapsed_time) {
|
3008 | 98f90dba | Zhi Yong Wu | wait_time = wait_time - elapsed_time; |
3009 | 98f90dba | Zhi Yong Wu | } else {
|
3010 | 98f90dba | Zhi Yong Wu | wait_time = 0;
|
3011 | 98f90dba | Zhi Yong Wu | } |
3012 | 98f90dba | Zhi Yong Wu | |
3013 | 98f90dba | Zhi Yong Wu | bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10;
|
3014 | 98f90dba | Zhi Yong Wu | bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME;
|
3015 | 98f90dba | Zhi Yong Wu | if (wait) {
|
3016 | 98f90dba | Zhi Yong Wu | *wait = wait_time * BLOCK_IO_SLICE_TIME * 10;
|
3017 | 98f90dba | Zhi Yong Wu | } |
3018 | 98f90dba | Zhi Yong Wu | |
3019 | 98f90dba | Zhi Yong Wu | return true; |
3020 | 98f90dba | Zhi Yong Wu | } |
3021 | 98f90dba | Zhi Yong Wu | |
3022 | 98f90dba | Zhi Yong Wu | static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors, |
3023 | 98f90dba | Zhi Yong Wu | bool is_write, int64_t *wait)
|
3024 | 98f90dba | Zhi Yong Wu | { |
3025 | 98f90dba | Zhi Yong Wu | int64_t now, max_wait; |
3026 | 98f90dba | Zhi Yong Wu | uint64_t bps_wait = 0, iops_wait = 0; |
3027 | 98f90dba | Zhi Yong Wu | double elapsed_time;
|
3028 | 98f90dba | Zhi Yong Wu | int bps_ret, iops_ret;
|
3029 | 98f90dba | Zhi Yong Wu | |
3030 | 98f90dba | Zhi Yong Wu | now = qemu_get_clock_ns(vm_clock); |
3031 | 98f90dba | Zhi Yong Wu | if ((bs->slice_start < now)
|
3032 | 98f90dba | Zhi Yong Wu | && (bs->slice_end > now)) { |
3033 | 98f90dba | Zhi Yong Wu | bs->slice_end = now + bs->slice_time; |
3034 | 98f90dba | Zhi Yong Wu | } else {
|
3035 | 98f90dba | Zhi Yong Wu | bs->slice_time = 5 * BLOCK_IO_SLICE_TIME;
|
3036 | 98f90dba | Zhi Yong Wu | bs->slice_start = now; |
3037 | 98f90dba | Zhi Yong Wu | bs->slice_end = now + bs->slice_time; |
3038 | 98f90dba | Zhi Yong Wu | |
3039 | 98f90dba | Zhi Yong Wu | bs->io_base.bytes[is_write] = bs->nr_bytes[is_write]; |
3040 | 98f90dba | Zhi Yong Wu | bs->io_base.bytes[!is_write] = bs->nr_bytes[!is_write]; |
3041 | 98f90dba | Zhi Yong Wu | |
3042 | 98f90dba | Zhi Yong Wu | bs->io_base.ios[is_write] = bs->nr_ops[is_write]; |
3043 | 98f90dba | Zhi Yong Wu | bs->io_base.ios[!is_write] = bs->nr_ops[!is_write]; |
3044 | 98f90dba | Zhi Yong Wu | } |
3045 | 98f90dba | Zhi Yong Wu | |
3046 | 98f90dba | Zhi Yong Wu | elapsed_time = now - bs->slice_start; |
3047 | 98f90dba | Zhi Yong Wu | elapsed_time /= (NANOSECONDS_PER_SECOND); |
3048 | 98f90dba | Zhi Yong Wu | |
3049 | 98f90dba | Zhi Yong Wu | bps_ret = bdrv_exceed_bps_limits(bs, nb_sectors, |
3050 | 98f90dba | Zhi Yong Wu | is_write, elapsed_time, &bps_wait); |
3051 | 98f90dba | Zhi Yong Wu | iops_ret = bdrv_exceed_iops_limits(bs, is_write, |
3052 | 98f90dba | Zhi Yong Wu | elapsed_time, &iops_wait); |
3053 | 98f90dba | Zhi Yong Wu | if (bps_ret || iops_ret) {
|
3054 | 98f90dba | Zhi Yong Wu | max_wait = bps_wait > iops_wait ? bps_wait : iops_wait; |
3055 | 98f90dba | Zhi Yong Wu | if (wait) {
|
3056 | 98f90dba | Zhi Yong Wu | *wait = max_wait; |
3057 | 98f90dba | Zhi Yong Wu | } |
3058 | 98f90dba | Zhi Yong Wu | |
3059 | 98f90dba | Zhi Yong Wu | now = qemu_get_clock_ns(vm_clock); |
3060 | 98f90dba | Zhi Yong Wu | if (bs->slice_end < now + max_wait) {
|
3061 | 98f90dba | Zhi Yong Wu | bs->slice_end = now + max_wait; |
3062 | 98f90dba | Zhi Yong Wu | } |
3063 | 98f90dba | Zhi Yong Wu | |
3064 | 98f90dba | Zhi Yong Wu | return true; |
3065 | 98f90dba | Zhi Yong Wu | } |
3066 | 98f90dba | Zhi Yong Wu | |
3067 | 98f90dba | Zhi Yong Wu | if (wait) {
|
3068 | 98f90dba | Zhi Yong Wu | *wait = 0;
|
3069 | 98f90dba | Zhi Yong Wu | } |
3070 | 98f90dba | Zhi Yong Wu | |
3071 | 98f90dba | Zhi Yong Wu | return false; |
3072 | 98f90dba | Zhi Yong Wu | } |
3073 | ce1a14dc | pbrook | |
3074 | 83f64091 | bellard | /**************************************************************/
|
3075 | 83f64091 | bellard | /* async block device emulation */
|
3076 | 83f64091 | bellard | |
3077 | c16b5a2c | Christoph Hellwig | typedef struct BlockDriverAIOCBSync { |
3078 | c16b5a2c | Christoph Hellwig | BlockDriverAIOCB common; |
3079 | c16b5a2c | Christoph Hellwig | QEMUBH *bh; |
3080 | c16b5a2c | Christoph Hellwig | int ret;
|
3081 | c16b5a2c | Christoph Hellwig | /* vector translation state */
|
3082 | c16b5a2c | Christoph Hellwig | QEMUIOVector *qiov; |
3083 | c16b5a2c | Christoph Hellwig | uint8_t *bounce; |
3084 | c16b5a2c | Christoph Hellwig | int is_write;
|
3085 | c16b5a2c | Christoph Hellwig | } BlockDriverAIOCBSync; |
3086 | c16b5a2c | Christoph Hellwig | |
3087 | c16b5a2c | Christoph Hellwig | static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) |
3088 | c16b5a2c | Christoph Hellwig | { |
3089 | b666d239 | Kevin Wolf | BlockDriverAIOCBSync *acb = |
3090 | b666d239 | Kevin Wolf | container_of(blockacb, BlockDriverAIOCBSync, common); |
3091 | 6a7ad299 | Dor Laor | qemu_bh_delete(acb->bh); |
3092 | 36afc451 | Avi Kivity | acb->bh = NULL;
|
3093 | c16b5a2c | Christoph Hellwig | qemu_aio_release(acb); |
3094 | c16b5a2c | Christoph Hellwig | } |
3095 | c16b5a2c | Christoph Hellwig | |
3096 | c16b5a2c | Christoph Hellwig | static AIOPool bdrv_em_aio_pool = {
|
3097 | c16b5a2c | Christoph Hellwig | .aiocb_size = sizeof(BlockDriverAIOCBSync),
|
3098 | c16b5a2c | Christoph Hellwig | .cancel = bdrv_aio_cancel_em, |
3099 | c16b5a2c | Christoph Hellwig | }; |
3100 | c16b5a2c | Christoph Hellwig | |
3101 | ce1a14dc | pbrook | static void bdrv_aio_bh_cb(void *opaque) |
3102 | 83f64091 | bellard | { |
3103 | ce1a14dc | pbrook | BlockDriverAIOCBSync *acb = opaque; |
3104 | f141eafe | aliguori | |
3105 | f141eafe | aliguori | if (!acb->is_write)
|
3106 | f141eafe | aliguori | qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); |
3107 | ceb42de8 | aliguori | qemu_vfree(acb->bounce); |
3108 | ce1a14dc | pbrook | acb->common.cb(acb->common.opaque, acb->ret); |
3109 | 6a7ad299 | Dor Laor | qemu_bh_delete(acb->bh); |
3110 | 36afc451 | Avi Kivity | acb->bh = NULL;
|
3111 | ce1a14dc | pbrook | qemu_aio_release(acb); |
3112 | 83f64091 | bellard | } |
3113 | beac80cd | bellard | |
3114 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
|
3115 | f141eafe | aliguori | int64_t sector_num, |
3116 | f141eafe | aliguori | QEMUIOVector *qiov, |
3117 | f141eafe | aliguori | int nb_sectors,
|
3118 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, |
3119 | f141eafe | aliguori | void *opaque,
|
3120 | f141eafe | aliguori | int is_write)
|
3121 | f141eafe | aliguori | |
3122 | 83f64091 | bellard | { |
3123 | ce1a14dc | pbrook | BlockDriverAIOCBSync *acb; |
3124 | ce1a14dc | pbrook | |
3125 | c16b5a2c | Christoph Hellwig | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); |
3126 | f141eafe | aliguori | acb->is_write = is_write; |
3127 | f141eafe | aliguori | acb->qiov = qiov; |
3128 | e268ca52 | aliguori | acb->bounce = qemu_blockalign(bs, qiov->size); |
3129 | 3f3aace8 | Paolo Bonzini | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
3130 | f141eafe | aliguori | |
3131 | f141eafe | aliguori | if (is_write) {
|
3132 | f141eafe | aliguori | qemu_iovec_to_buffer(acb->qiov, acb->bounce); |
3133 | 1ed20acf | Stefan Hajnoczi | acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors); |
3134 | f141eafe | aliguori | } else {
|
3135 | 1ed20acf | Stefan Hajnoczi | acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors); |
3136 | f141eafe | aliguori | } |
3137 | f141eafe | aliguori | |
3138 | ce1a14dc | pbrook | qemu_bh_schedule(acb->bh); |
3139 | f141eafe | aliguori | |
3140 | ce1a14dc | pbrook | return &acb->common;
|
3141 | beac80cd | bellard | } |
3142 | beac80cd | bellard | |
3143 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
|
3144 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
3145 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
3146 | beac80cd | bellard | { |
3147 | f141eafe | aliguori | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); |
3148 | f141eafe | aliguori | } |
3149 | 83f64091 | bellard | |
3150 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
|
3151 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
3152 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
3153 | f141eafe | aliguori | { |
3154 | f141eafe | aliguori | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); |
3155 | beac80cd | bellard | } |
3156 | beac80cd | bellard | |
3157 | 68485420 | Kevin Wolf | |
3158 | 68485420 | Kevin Wolf | typedef struct BlockDriverAIOCBCoroutine { |
3159 | 68485420 | Kevin Wolf | BlockDriverAIOCB common; |
3160 | 68485420 | Kevin Wolf | BlockRequest req; |
3161 | 68485420 | Kevin Wolf | bool is_write;
|
3162 | 68485420 | Kevin Wolf | QEMUBH* bh; |
3163 | 68485420 | Kevin Wolf | } BlockDriverAIOCBCoroutine; |
3164 | 68485420 | Kevin Wolf | |
3165 | 68485420 | Kevin Wolf | static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) |
3166 | 68485420 | Kevin Wolf | { |
3167 | 68485420 | Kevin Wolf | qemu_aio_flush(); |
3168 | 68485420 | Kevin Wolf | } |
3169 | 68485420 | Kevin Wolf | |
3170 | 68485420 | Kevin Wolf | static AIOPool bdrv_em_co_aio_pool = {
|
3171 | 68485420 | Kevin Wolf | .aiocb_size = sizeof(BlockDriverAIOCBCoroutine),
|
3172 | 68485420 | Kevin Wolf | .cancel = bdrv_aio_co_cancel_em, |
3173 | 68485420 | Kevin Wolf | }; |
3174 | 68485420 | Kevin Wolf | |
3175 | 35246a68 | Paolo Bonzini | static void bdrv_co_em_bh(void *opaque) |
3176 | 68485420 | Kevin Wolf | { |
3177 | 68485420 | Kevin Wolf | BlockDriverAIOCBCoroutine *acb = opaque; |
3178 | 68485420 | Kevin Wolf | |
3179 | 68485420 | Kevin Wolf | acb->common.cb(acb->common.opaque, acb->req.error); |
3180 | 68485420 | Kevin Wolf | qemu_bh_delete(acb->bh); |
3181 | 68485420 | Kevin Wolf | qemu_aio_release(acb); |
3182 | 68485420 | Kevin Wolf | } |
3183 | 68485420 | Kevin Wolf | |
3184 | b2a61371 | Stefan Hajnoczi | /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */
|
3185 | b2a61371 | Stefan Hajnoczi | static void coroutine_fn bdrv_co_do_rw(void *opaque) |
3186 | b2a61371 | Stefan Hajnoczi | { |
3187 | b2a61371 | Stefan Hajnoczi | BlockDriverAIOCBCoroutine *acb = opaque; |
3188 | b2a61371 | Stefan Hajnoczi | BlockDriverState *bs = acb->common.bs; |
3189 | b2a61371 | Stefan Hajnoczi | |
3190 | b2a61371 | Stefan Hajnoczi | if (!acb->is_write) {
|
3191 | b2a61371 | Stefan Hajnoczi | acb->req.error = bdrv_co_do_readv(bs, acb->req.sector, |
3192 | 470c0504 | Stefan Hajnoczi | acb->req.nb_sectors, acb->req.qiov, 0);
|
3193 | b2a61371 | Stefan Hajnoczi | } else {
|
3194 | b2a61371 | Stefan Hajnoczi | acb->req.error = bdrv_co_do_writev(bs, acb->req.sector, |
3195 | b2a61371 | Stefan Hajnoczi | acb->req.nb_sectors, acb->req.qiov); |
3196 | b2a61371 | Stefan Hajnoczi | } |
3197 | b2a61371 | Stefan Hajnoczi | |
3198 | 35246a68 | Paolo Bonzini | acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); |
3199 | b2a61371 | Stefan Hajnoczi | qemu_bh_schedule(acb->bh); |
3200 | b2a61371 | Stefan Hajnoczi | } |
3201 | b2a61371 | Stefan Hajnoczi | |
3202 | 68485420 | Kevin Wolf | static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
|
3203 | 68485420 | Kevin Wolf | int64_t sector_num, |
3204 | 68485420 | Kevin Wolf | QEMUIOVector *qiov, |
3205 | 68485420 | Kevin Wolf | int nb_sectors,
|
3206 | 68485420 | Kevin Wolf | BlockDriverCompletionFunc *cb, |
3207 | 68485420 | Kevin Wolf | void *opaque,
|
3208 | 8c5873d6 | Stefan Hajnoczi | bool is_write)
|
3209 | 68485420 | Kevin Wolf | { |
3210 | 68485420 | Kevin Wolf | Coroutine *co; |
3211 | 68485420 | Kevin Wolf | BlockDriverAIOCBCoroutine *acb; |
3212 | 68485420 | Kevin Wolf | |
3213 | 68485420 | Kevin Wolf | acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); |
3214 | 68485420 | Kevin Wolf | acb->req.sector = sector_num; |
3215 | 68485420 | Kevin Wolf | acb->req.nb_sectors = nb_sectors; |
3216 | 68485420 | Kevin Wolf | acb->req.qiov = qiov; |
3217 | 68485420 | Kevin Wolf | acb->is_write = is_write; |
3218 | 68485420 | Kevin Wolf | |
3219 | 8c5873d6 | Stefan Hajnoczi | co = qemu_coroutine_create(bdrv_co_do_rw); |
3220 | 68485420 | Kevin Wolf | qemu_coroutine_enter(co, acb); |
3221 | 68485420 | Kevin Wolf | |
3222 | 68485420 | Kevin Wolf | return &acb->common;
|
3223 | 68485420 | Kevin Wolf | } |
3224 | 68485420 | Kevin Wolf | |
3225 | 07f07615 | Paolo Bonzini | static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) |
3226 | b2e12bc6 | Christoph Hellwig | { |
3227 | 07f07615 | Paolo Bonzini | BlockDriverAIOCBCoroutine *acb = opaque; |
3228 | 07f07615 | Paolo Bonzini | BlockDriverState *bs = acb->common.bs; |
3229 | b2e12bc6 | Christoph Hellwig | |
3230 | 07f07615 | Paolo Bonzini | acb->req.error = bdrv_co_flush(bs); |
3231 | 07f07615 | Paolo Bonzini | acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); |
3232 | b2e12bc6 | Christoph Hellwig | qemu_bh_schedule(acb->bh); |
3233 | b2e12bc6 | Christoph Hellwig | } |
3234 | b2e12bc6 | Christoph Hellwig | |
3235 | 07f07615 | Paolo Bonzini | BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, |
3236 | 016f5cf6 | Alexander Graf | BlockDriverCompletionFunc *cb, void *opaque)
|
3237 | 016f5cf6 | Alexander Graf | { |
3238 | 07f07615 | Paolo Bonzini | trace_bdrv_aio_flush(bs, opaque); |
3239 | 016f5cf6 | Alexander Graf | |
3240 | 07f07615 | Paolo Bonzini | Coroutine *co; |
3241 | 07f07615 | Paolo Bonzini | BlockDriverAIOCBCoroutine *acb; |
3242 | 016f5cf6 | Alexander Graf | |
3243 | 07f07615 | Paolo Bonzini | acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); |
3244 | 07f07615 | Paolo Bonzini | co = qemu_coroutine_create(bdrv_aio_flush_co_entry); |
3245 | 07f07615 | Paolo Bonzini | qemu_coroutine_enter(co, acb); |
3246 | 016f5cf6 | Alexander Graf | |
3247 | 016f5cf6 | Alexander Graf | return &acb->common;
|
3248 | 016f5cf6 | Alexander Graf | } |
3249 | 016f5cf6 | Alexander Graf | |
3250 | 4265d620 | Paolo Bonzini | static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque) |
3251 | 4265d620 | Paolo Bonzini | { |
3252 | 4265d620 | Paolo Bonzini | BlockDriverAIOCBCoroutine *acb = opaque; |
3253 | 4265d620 | Paolo Bonzini | BlockDriverState *bs = acb->common.bs; |
3254 | 4265d620 | Paolo Bonzini | |
3255 | 4265d620 | Paolo Bonzini | acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors); |
3256 | 4265d620 | Paolo Bonzini | acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); |
3257 | 4265d620 | Paolo Bonzini | qemu_bh_schedule(acb->bh); |
3258 | 4265d620 | Paolo Bonzini | } |
3259 | 4265d620 | Paolo Bonzini | |
3260 | 4265d620 | Paolo Bonzini | BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, |
3261 | 4265d620 | Paolo Bonzini | int64_t sector_num, int nb_sectors,
|
3262 | 4265d620 | Paolo Bonzini | BlockDriverCompletionFunc *cb, void *opaque)
|
3263 | 4265d620 | Paolo Bonzini | { |
3264 | 4265d620 | Paolo Bonzini | Coroutine *co; |
3265 | 4265d620 | Paolo Bonzini | BlockDriverAIOCBCoroutine *acb; |
3266 | 4265d620 | Paolo Bonzini | |
3267 | 4265d620 | Paolo Bonzini | trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque); |
3268 | 4265d620 | Paolo Bonzini | |
3269 | 4265d620 | Paolo Bonzini | acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); |
3270 | 4265d620 | Paolo Bonzini | acb->req.sector = sector_num; |
3271 | 4265d620 | Paolo Bonzini | acb->req.nb_sectors = nb_sectors; |
3272 | 4265d620 | Paolo Bonzini | co = qemu_coroutine_create(bdrv_aio_discard_co_entry); |
3273 | 4265d620 | Paolo Bonzini | qemu_coroutine_enter(co, acb); |
3274 | 4265d620 | Paolo Bonzini | |
3275 | 4265d620 | Paolo Bonzini | return &acb->common;
|
3276 | 4265d620 | Paolo Bonzini | } |
3277 | 4265d620 | Paolo Bonzini | |
3278 | ea2384d3 | bellard | void bdrv_init(void) |
3279 | ea2384d3 | bellard | { |
3280 | 5efa9d5a | Anthony Liguori | module_call_init(MODULE_INIT_BLOCK); |
3281 | ea2384d3 | bellard | } |
3282 | ce1a14dc | pbrook | |
3283 | eb852011 | Markus Armbruster | void bdrv_init_with_whitelist(void) |
3284 | eb852011 | Markus Armbruster | { |
3285 | eb852011 | Markus Armbruster | use_bdrv_whitelist = 1;
|
3286 | eb852011 | Markus Armbruster | bdrv_init(); |
3287 | eb852011 | Markus Armbruster | } |
3288 | eb852011 | Markus Armbruster | |
3289 | c16b5a2c | Christoph Hellwig | void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
|
3290 | c16b5a2c | Christoph Hellwig | BlockDriverCompletionFunc *cb, void *opaque)
|
3291 | ce1a14dc | pbrook | { |
3292 | ce1a14dc | pbrook | BlockDriverAIOCB *acb; |
3293 | ce1a14dc | pbrook | |
3294 | 6bbff9a0 | aliguori | if (pool->free_aiocb) {
|
3295 | 6bbff9a0 | aliguori | acb = pool->free_aiocb; |
3296 | 6bbff9a0 | aliguori | pool->free_aiocb = acb->next; |
3297 | ce1a14dc | pbrook | } else {
|
3298 | 7267c094 | Anthony Liguori | acb = g_malloc0(pool->aiocb_size); |
3299 | 6bbff9a0 | aliguori | acb->pool = pool; |
3300 | ce1a14dc | pbrook | } |
3301 | ce1a14dc | pbrook | acb->bs = bs; |
3302 | ce1a14dc | pbrook | acb->cb = cb; |
3303 | ce1a14dc | pbrook | acb->opaque = opaque; |
3304 | ce1a14dc | pbrook | return acb;
|
3305 | ce1a14dc | pbrook | } |
3306 | ce1a14dc | pbrook | |
3307 | ce1a14dc | pbrook | void qemu_aio_release(void *p) |
3308 | ce1a14dc | pbrook | { |
3309 | 6bbff9a0 | aliguori | BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; |
3310 | 6bbff9a0 | aliguori | AIOPool *pool = acb->pool; |
3311 | 6bbff9a0 | aliguori | acb->next = pool->free_aiocb; |
3312 | 6bbff9a0 | aliguori | pool->free_aiocb = acb; |
3313 | ce1a14dc | pbrook | } |
3314 | 19cb3738 | bellard | |
3315 | 19cb3738 | bellard | /**************************************************************/
|
3316 | f9f05dc5 | Kevin Wolf | /* Coroutine block device emulation */
|
3317 | f9f05dc5 | Kevin Wolf | |
3318 | f9f05dc5 | Kevin Wolf | typedef struct CoroutineIOCompletion { |
3319 | f9f05dc5 | Kevin Wolf | Coroutine *coroutine; |
3320 | f9f05dc5 | Kevin Wolf | int ret;
|
3321 | f9f05dc5 | Kevin Wolf | } CoroutineIOCompletion; |
3322 | f9f05dc5 | Kevin Wolf | |
3323 | f9f05dc5 | Kevin Wolf | static void bdrv_co_io_em_complete(void *opaque, int ret) |
3324 | f9f05dc5 | Kevin Wolf | { |
3325 | f9f05dc5 | Kevin Wolf | CoroutineIOCompletion *co = opaque; |
3326 | f9f05dc5 | Kevin Wolf | |
3327 | f9f05dc5 | Kevin Wolf | co->ret = ret; |
3328 | f9f05dc5 | Kevin Wolf | qemu_coroutine_enter(co->coroutine, NULL);
|
3329 | f9f05dc5 | Kevin Wolf | } |
3330 | f9f05dc5 | Kevin Wolf | |
3331 | f9f05dc5 | Kevin Wolf | static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, |
3332 | f9f05dc5 | Kevin Wolf | int nb_sectors, QEMUIOVector *iov,
|
3333 | f9f05dc5 | Kevin Wolf | bool is_write)
|
3334 | f9f05dc5 | Kevin Wolf | { |
3335 | f9f05dc5 | Kevin Wolf | CoroutineIOCompletion co = { |
3336 | f9f05dc5 | Kevin Wolf | .coroutine = qemu_coroutine_self(), |
3337 | f9f05dc5 | Kevin Wolf | }; |
3338 | f9f05dc5 | Kevin Wolf | BlockDriverAIOCB *acb; |
3339 | f9f05dc5 | Kevin Wolf | |
3340 | f9f05dc5 | Kevin Wolf | if (is_write) {
|
3341 | a652d160 | Stefan Hajnoczi | acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors, |
3342 | a652d160 | Stefan Hajnoczi | bdrv_co_io_em_complete, &co); |
3343 | f9f05dc5 | Kevin Wolf | } else {
|
3344 | a652d160 | Stefan Hajnoczi | acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors, |
3345 | a652d160 | Stefan Hajnoczi | bdrv_co_io_em_complete, &co); |
3346 | f9f05dc5 | Kevin Wolf | } |
3347 | f9f05dc5 | Kevin Wolf | |
3348 | 59370aaa | Stefan Hajnoczi | trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb); |
3349 | f9f05dc5 | Kevin Wolf | if (!acb) {
|
3350 | f9f05dc5 | Kevin Wolf | return -EIO;
|
3351 | f9f05dc5 | Kevin Wolf | } |
3352 | f9f05dc5 | Kevin Wolf | qemu_coroutine_yield(); |
3353 | f9f05dc5 | Kevin Wolf | |
3354 | f9f05dc5 | Kevin Wolf | return co.ret;
|
3355 | f9f05dc5 | Kevin Wolf | } |
3356 | f9f05dc5 | Kevin Wolf | |
3357 | f9f05dc5 | Kevin Wolf | static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, |
3358 | f9f05dc5 | Kevin Wolf | int64_t sector_num, int nb_sectors,
|
3359 | f9f05dc5 | Kevin Wolf | QEMUIOVector *iov) |
3360 | f9f05dc5 | Kevin Wolf | { |
3361 | f9f05dc5 | Kevin Wolf | return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); |
3362 | f9f05dc5 | Kevin Wolf | } |
3363 | f9f05dc5 | Kevin Wolf | |
3364 | f9f05dc5 | Kevin Wolf | static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, |
3365 | f9f05dc5 | Kevin Wolf | int64_t sector_num, int nb_sectors,
|
3366 | f9f05dc5 | Kevin Wolf | QEMUIOVector *iov) |
3367 | f9f05dc5 | Kevin Wolf | { |
3368 | f9f05dc5 | Kevin Wolf | return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); |
3369 | f9f05dc5 | Kevin Wolf | } |
3370 | f9f05dc5 | Kevin Wolf | |
3371 | 07f07615 | Paolo Bonzini | static void coroutine_fn bdrv_flush_co_entry(void *opaque) |
3372 | e7a8a783 | Kevin Wolf | { |
3373 | 07f07615 | Paolo Bonzini | RwCo *rwco = opaque; |
3374 | 07f07615 | Paolo Bonzini | |
3375 | 07f07615 | Paolo Bonzini | rwco->ret = bdrv_co_flush(rwco->bs); |
3376 | 07f07615 | Paolo Bonzini | } |
3377 | 07f07615 | Paolo Bonzini | |
3378 | 07f07615 | Paolo Bonzini | int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
|
3379 | 07f07615 | Paolo Bonzini | { |
3380 | eb489bb1 | Kevin Wolf | int ret;
|
3381 | eb489bb1 | Kevin Wolf | |
3382 | ca716364 | Kevin Wolf | if (!bs->drv) {
|
3383 | 07f07615 | Paolo Bonzini | return 0; |
3384 | eb489bb1 | Kevin Wolf | } |
3385 | eb489bb1 | Kevin Wolf | |
3386 | ca716364 | Kevin Wolf | /* Write back cached data to the OS even with cache=unsafe */
|
3387 | eb489bb1 | Kevin Wolf | if (bs->drv->bdrv_co_flush_to_os) {
|
3388 | eb489bb1 | Kevin Wolf | ret = bs->drv->bdrv_co_flush_to_os(bs); |
3389 | eb489bb1 | Kevin Wolf | if (ret < 0) { |
3390 | eb489bb1 | Kevin Wolf | return ret;
|
3391 | eb489bb1 | Kevin Wolf | } |
3392 | eb489bb1 | Kevin Wolf | } |
3393 | eb489bb1 | Kevin Wolf | |
3394 | ca716364 | Kevin Wolf | /* But don't actually force it to the disk with cache=unsafe */
|
3395 | ca716364 | Kevin Wolf | if (bs->open_flags & BDRV_O_NO_FLUSH) {
|
3396 | ca716364 | Kevin Wolf | return 0; |
3397 | ca716364 | Kevin Wolf | } |
3398 | ca716364 | Kevin Wolf | |
3399 | eb489bb1 | Kevin Wolf | if (bs->drv->bdrv_co_flush_to_disk) {
|
3400 | c68b89ac | Kevin Wolf | return bs->drv->bdrv_co_flush_to_disk(bs);
|
3401 | 07f07615 | Paolo Bonzini | } else if (bs->drv->bdrv_aio_flush) { |
3402 | 07f07615 | Paolo Bonzini | BlockDriverAIOCB *acb; |
3403 | 07f07615 | Paolo Bonzini | CoroutineIOCompletion co = { |
3404 | 07f07615 | Paolo Bonzini | .coroutine = qemu_coroutine_self(), |
3405 | 07f07615 | Paolo Bonzini | }; |
3406 | 07f07615 | Paolo Bonzini | |
3407 | 07f07615 | Paolo Bonzini | acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); |
3408 | 07f07615 | Paolo Bonzini | if (acb == NULL) { |
3409 | 07f07615 | Paolo Bonzini | return -EIO;
|
3410 | 07f07615 | Paolo Bonzini | } else {
|
3411 | 07f07615 | Paolo Bonzini | qemu_coroutine_yield(); |
3412 | 07f07615 | Paolo Bonzini | return co.ret;
|
3413 | 07f07615 | Paolo Bonzini | } |
3414 | 07f07615 | Paolo Bonzini | } else {
|
3415 | 07f07615 | Paolo Bonzini | /*
|
3416 | 07f07615 | Paolo Bonzini | * Some block drivers always operate in either writethrough or unsafe
|
3417 | 07f07615 | Paolo Bonzini | * mode and don't support bdrv_flush therefore. Usually qemu doesn't
|
3418 | 07f07615 | Paolo Bonzini | * know how the server works (because the behaviour is hardcoded or
|
3419 | 07f07615 | Paolo Bonzini | * depends on server-side configuration), so we can't ensure that
|
3420 | 07f07615 | Paolo Bonzini | * everything is safe on disk. Returning an error doesn't work because
|
3421 | 07f07615 | Paolo Bonzini | * that would break guests even if the server operates in writethrough
|
3422 | 07f07615 | Paolo Bonzini | * mode.
|
3423 | 07f07615 | Paolo Bonzini | *
|
3424 | 07f07615 | Paolo Bonzini | * Let's hope the user knows what he's doing.
|
3425 | 07f07615 | Paolo Bonzini | */
|
3426 | 07f07615 | Paolo Bonzini | return 0; |
3427 | 07f07615 | Paolo Bonzini | } |
3428 | 07f07615 | Paolo Bonzini | } |
3429 | 07f07615 | Paolo Bonzini | |
3430 | 0f15423c | Anthony Liguori | void bdrv_invalidate_cache(BlockDriverState *bs)
|
3431 | 0f15423c | Anthony Liguori | { |
3432 | 0f15423c | Anthony Liguori | if (bs->drv && bs->drv->bdrv_invalidate_cache) {
|
3433 | 0f15423c | Anthony Liguori | bs->drv->bdrv_invalidate_cache(bs); |
3434 | 0f15423c | Anthony Liguori | } |
3435 | 0f15423c | Anthony Liguori | } |
3436 | 0f15423c | Anthony Liguori | |
3437 | 0f15423c | Anthony Liguori | void bdrv_invalidate_cache_all(void) |
3438 | 0f15423c | Anthony Liguori | { |
3439 | 0f15423c | Anthony Liguori | BlockDriverState *bs; |
3440 | 0f15423c | Anthony Liguori | |
3441 | 0f15423c | Anthony Liguori | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
3442 | 0f15423c | Anthony Liguori | bdrv_invalidate_cache(bs); |
3443 | 0f15423c | Anthony Liguori | } |
3444 | 0f15423c | Anthony Liguori | } |
3445 | 0f15423c | Anthony Liguori | |
3446 | 07f07615 | Paolo Bonzini | int bdrv_flush(BlockDriverState *bs)
|
3447 | 07f07615 | Paolo Bonzini | { |
3448 | 07f07615 | Paolo Bonzini | Coroutine *co; |
3449 | 07f07615 | Paolo Bonzini | RwCo rwco = { |
3450 | 07f07615 | Paolo Bonzini | .bs = bs, |
3451 | 07f07615 | Paolo Bonzini | .ret = NOT_DONE, |
3452 | e7a8a783 | Kevin Wolf | }; |
3453 | e7a8a783 | Kevin Wolf | |
3454 | 07f07615 | Paolo Bonzini | if (qemu_in_coroutine()) {
|
3455 | 07f07615 | Paolo Bonzini | /* Fast-path if already in coroutine context */
|
3456 | 07f07615 | Paolo Bonzini | bdrv_flush_co_entry(&rwco); |
3457 | 07f07615 | Paolo Bonzini | } else {
|
3458 | 07f07615 | Paolo Bonzini | co = qemu_coroutine_create(bdrv_flush_co_entry); |
3459 | 07f07615 | Paolo Bonzini | qemu_coroutine_enter(co, &rwco); |
3460 | 07f07615 | Paolo Bonzini | while (rwco.ret == NOT_DONE) {
|
3461 | 07f07615 | Paolo Bonzini | qemu_aio_wait(); |
3462 | 07f07615 | Paolo Bonzini | } |
3463 | e7a8a783 | Kevin Wolf | } |
3464 | 07f07615 | Paolo Bonzini | |
3465 | 07f07615 | Paolo Bonzini | return rwco.ret;
|
3466 | e7a8a783 | Kevin Wolf | } |
3467 | e7a8a783 | Kevin Wolf | |
3468 | 4265d620 | Paolo Bonzini | static void coroutine_fn bdrv_discard_co_entry(void *opaque) |
3469 | 4265d620 | Paolo Bonzini | { |
3470 | 4265d620 | Paolo Bonzini | RwCo *rwco = opaque; |
3471 | 4265d620 | Paolo Bonzini | |
3472 | 4265d620 | Paolo Bonzini | rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors); |
3473 | 4265d620 | Paolo Bonzini | } |
3474 | 4265d620 | Paolo Bonzini | |
3475 | 4265d620 | Paolo Bonzini | int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
|
3476 | 4265d620 | Paolo Bonzini | int nb_sectors)
|
3477 | 4265d620 | Paolo Bonzini | { |
3478 | 4265d620 | Paolo Bonzini | if (!bs->drv) {
|
3479 | 4265d620 | Paolo Bonzini | return -ENOMEDIUM;
|
3480 | 4265d620 | Paolo Bonzini | } else if (bdrv_check_request(bs, sector_num, nb_sectors)) { |
3481 | 4265d620 | Paolo Bonzini | return -EIO;
|
3482 | 4265d620 | Paolo Bonzini | } else if (bs->read_only) { |
3483 | 4265d620 | Paolo Bonzini | return -EROFS;
|
3484 | 4265d620 | Paolo Bonzini | } else if (bs->drv->bdrv_co_discard) { |
3485 | 4265d620 | Paolo Bonzini | return bs->drv->bdrv_co_discard(bs, sector_num, nb_sectors);
|
3486 | 4265d620 | Paolo Bonzini | } else if (bs->drv->bdrv_aio_discard) { |
3487 | 4265d620 | Paolo Bonzini | BlockDriverAIOCB *acb; |
3488 | 4265d620 | Paolo Bonzini | CoroutineIOCompletion co = { |
3489 | 4265d620 | Paolo Bonzini | .coroutine = qemu_coroutine_self(), |
3490 | 4265d620 | Paolo Bonzini | }; |
3491 | 4265d620 | Paolo Bonzini | |
3492 | 4265d620 | Paolo Bonzini | acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors, |
3493 | 4265d620 | Paolo Bonzini | bdrv_co_io_em_complete, &co); |
3494 | 4265d620 | Paolo Bonzini | if (acb == NULL) { |
3495 | 4265d620 | Paolo Bonzini | return -EIO;
|
3496 | 4265d620 | Paolo Bonzini | } else {
|
3497 | 4265d620 | Paolo Bonzini | qemu_coroutine_yield(); |
3498 | 4265d620 | Paolo Bonzini | return co.ret;
|
3499 | 4265d620 | Paolo Bonzini | } |
3500 | 4265d620 | Paolo Bonzini | } else {
|
3501 | 4265d620 | Paolo Bonzini | return 0; |
3502 | 4265d620 | Paolo Bonzini | } |
3503 | 4265d620 | Paolo Bonzini | } |
3504 | 4265d620 | Paolo Bonzini | |
3505 | 4265d620 | Paolo Bonzini | int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) |
3506 | 4265d620 | Paolo Bonzini | { |
3507 | 4265d620 | Paolo Bonzini | Coroutine *co; |
3508 | 4265d620 | Paolo Bonzini | RwCo rwco = { |
3509 | 4265d620 | Paolo Bonzini | .bs = bs, |
3510 | 4265d620 | Paolo Bonzini | .sector_num = sector_num, |
3511 | 4265d620 | Paolo Bonzini | .nb_sectors = nb_sectors, |
3512 | 4265d620 | Paolo Bonzini | .ret = NOT_DONE, |
3513 | 4265d620 | Paolo Bonzini | }; |
3514 | 4265d620 | Paolo Bonzini | |
3515 | 4265d620 | Paolo Bonzini | if (qemu_in_coroutine()) {
|
3516 | 4265d620 | Paolo Bonzini | /* Fast-path if already in coroutine context */
|
3517 | 4265d620 | Paolo Bonzini | bdrv_discard_co_entry(&rwco); |
3518 | 4265d620 | Paolo Bonzini | } else {
|
3519 | 4265d620 | Paolo Bonzini | co = qemu_coroutine_create(bdrv_discard_co_entry); |
3520 | 4265d620 | Paolo Bonzini | qemu_coroutine_enter(co, &rwco); |
3521 | 4265d620 | Paolo Bonzini | while (rwco.ret == NOT_DONE) {
|
3522 | 4265d620 | Paolo Bonzini | qemu_aio_wait(); |
3523 | 4265d620 | Paolo Bonzini | } |
3524 | 4265d620 | Paolo Bonzini | } |
3525 | 4265d620 | Paolo Bonzini | |
3526 | 4265d620 | Paolo Bonzini | return rwco.ret;
|
3527 | 4265d620 | Paolo Bonzini | } |
3528 | 4265d620 | Paolo Bonzini | |
3529 | f9f05dc5 | Kevin Wolf | /**************************************************************/
|
3530 | 19cb3738 | bellard | /* removable device support */
|
3531 | 19cb3738 | bellard | |
3532 | 19cb3738 | bellard | /**
|
3533 | 19cb3738 | bellard | * Return TRUE if the media is present
|
3534 | 19cb3738 | bellard | */
|
3535 | 19cb3738 | bellard | int bdrv_is_inserted(BlockDriverState *bs)
|
3536 | 19cb3738 | bellard | { |
3537 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
3538 | a1aff5bf | Markus Armbruster | |
3539 | 19cb3738 | bellard | if (!drv)
|
3540 | 19cb3738 | bellard | return 0; |
3541 | 19cb3738 | bellard | if (!drv->bdrv_is_inserted)
|
3542 | a1aff5bf | Markus Armbruster | return 1; |
3543 | a1aff5bf | Markus Armbruster | return drv->bdrv_is_inserted(bs);
|
3544 | 19cb3738 | bellard | } |
3545 | 19cb3738 | bellard | |
3546 | 19cb3738 | bellard | /**
|
3547 | 8e49ca46 | Markus Armbruster | * Return whether the media changed since the last call to this
|
3548 | 8e49ca46 | Markus Armbruster | * function, or -ENOTSUP if we don't know. Most drivers don't know.
|
3549 | 19cb3738 | bellard | */
|
3550 | 19cb3738 | bellard | int bdrv_media_changed(BlockDriverState *bs)
|
3551 | 19cb3738 | bellard | { |
3552 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
3553 | 19cb3738 | bellard | |
3554 | 8e49ca46 | Markus Armbruster | if (drv && drv->bdrv_media_changed) {
|
3555 | 8e49ca46 | Markus Armbruster | return drv->bdrv_media_changed(bs);
|
3556 | 8e49ca46 | Markus Armbruster | } |
3557 | 8e49ca46 | Markus Armbruster | return -ENOTSUP;
|
3558 | 19cb3738 | bellard | } |
3559 | 19cb3738 | bellard | |
3560 | 19cb3738 | bellard | /**
|
3561 | 19cb3738 | bellard | * If eject_flag is TRUE, eject the media. Otherwise, close the tray
|
3562 | 19cb3738 | bellard | */
|
3563 | fdec4404 | Markus Armbruster | void bdrv_eject(BlockDriverState *bs, int eject_flag) |
3564 | 19cb3738 | bellard | { |
3565 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
3566 | 19cb3738 | bellard | |
3567 | 822e1cd1 | Markus Armbruster | if (drv && drv->bdrv_eject) {
|
3568 | 822e1cd1 | Markus Armbruster | drv->bdrv_eject(bs, eject_flag); |
3569 | 19cb3738 | bellard | } |
3570 | 19cb3738 | bellard | } |
3571 | 19cb3738 | bellard | |
3572 | 19cb3738 | bellard | /**
|
3573 | 19cb3738 | bellard | * Lock or unlock the media (if it is locked, the user won't be able
|
3574 | 19cb3738 | bellard | * to eject it manually).
|
3575 | 19cb3738 | bellard | */
|
3576 | 025e849a | Markus Armbruster | void bdrv_lock_medium(BlockDriverState *bs, bool locked) |
3577 | 19cb3738 | bellard | { |
3578 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
3579 | 19cb3738 | bellard | |
3580 | 025e849a | Markus Armbruster | trace_bdrv_lock_medium(bs, locked); |
3581 | b8c6d095 | Stefan Hajnoczi | |
3582 | 025e849a | Markus Armbruster | if (drv && drv->bdrv_lock_medium) {
|
3583 | 025e849a | Markus Armbruster | drv->bdrv_lock_medium(bs, locked); |
3584 | 19cb3738 | bellard | } |
3585 | 19cb3738 | bellard | } |
3586 | 985a03b0 | ths | |
3587 | 985a03b0 | ths | /* needed for generic scsi interface */
|
3588 | 985a03b0 | ths | |
3589 | 985a03b0 | ths | int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
3590 | 985a03b0 | ths | { |
3591 | 985a03b0 | ths | BlockDriver *drv = bs->drv; |
3592 | 985a03b0 | ths | |
3593 | 985a03b0 | ths | if (drv && drv->bdrv_ioctl)
|
3594 | 985a03b0 | ths | return drv->bdrv_ioctl(bs, req, buf);
|
3595 | 985a03b0 | ths | return -ENOTSUP;
|
3596 | 985a03b0 | ths | } |
3597 | 7d780669 | aliguori | |
3598 | 221f715d | aliguori | BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, |
3599 | 221f715d | aliguori | unsigned long int req, void *buf, |
3600 | 221f715d | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
3601 | 7d780669 | aliguori | { |
3602 | 221f715d | aliguori | BlockDriver *drv = bs->drv; |
3603 | 7d780669 | aliguori | |
3604 | 221f715d | aliguori | if (drv && drv->bdrv_aio_ioctl)
|
3605 | 221f715d | aliguori | return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
|
3606 | 221f715d | aliguori | return NULL; |
3607 | 7d780669 | aliguori | } |
3608 | e268ca52 | aliguori | |
3609 | 7b6f9300 | Markus Armbruster | void bdrv_set_buffer_alignment(BlockDriverState *bs, int align) |
3610 | 7b6f9300 | Markus Armbruster | { |
3611 | 7b6f9300 | Markus Armbruster | bs->buffer_alignment = align; |
3612 | 7b6f9300 | Markus Armbruster | } |
3613 | 7cd1e32a | lirans@il.ibm.com | |
3614 | e268ca52 | aliguori | void *qemu_blockalign(BlockDriverState *bs, size_t size)
|
3615 | e268ca52 | aliguori | { |
3616 | e268ca52 | aliguori | return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); |
3617 | e268ca52 | aliguori | } |
3618 | 7cd1e32a | lirans@il.ibm.com | |
3619 | 7cd1e32a | lirans@il.ibm.com | void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) |
3620 | 7cd1e32a | lirans@il.ibm.com | { |
3621 | 7cd1e32a | lirans@il.ibm.com | int64_t bitmap_size; |
3622 | a55eb92c | Jan Kiszka | |
3623 | aaa0eb75 | Liran Schour | bs->dirty_count = 0;
|
3624 | a55eb92c | Jan Kiszka | if (enable) {
|
3625 | c6d22830 | Jan Kiszka | if (!bs->dirty_bitmap) {
|
3626 | c6d22830 | Jan Kiszka | bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + |
3627 | c6d22830 | Jan Kiszka | BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; |
3628 | c6d22830 | Jan Kiszka | bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
|
3629 | a55eb92c | Jan Kiszka | |
3630 | 7267c094 | Anthony Liguori | bs->dirty_bitmap = g_malloc0(bitmap_size); |
3631 | a55eb92c | Jan Kiszka | } |
3632 | 7cd1e32a | lirans@il.ibm.com | } else {
|
3633 | c6d22830 | Jan Kiszka | if (bs->dirty_bitmap) {
|
3634 | 7267c094 | Anthony Liguori | g_free(bs->dirty_bitmap); |
3635 | c6d22830 | Jan Kiszka | bs->dirty_bitmap = NULL;
|
3636 | a55eb92c | Jan Kiszka | } |
3637 | 7cd1e32a | lirans@il.ibm.com | } |
3638 | 7cd1e32a | lirans@il.ibm.com | } |
3639 | 7cd1e32a | lirans@il.ibm.com | |
3640 | 7cd1e32a | lirans@il.ibm.com | int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
|
3641 | 7cd1e32a | lirans@il.ibm.com | { |
3642 | 6ea44308 | Jan Kiszka | int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; |
3643 | a55eb92c | Jan Kiszka | |
3644 | c6d22830 | Jan Kiszka | if (bs->dirty_bitmap &&
|
3645 | c6d22830 | Jan Kiszka | (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { |
3646 | 6d59fec1 | Marcelo Tosatti | return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] & |
3647 | 6d59fec1 | Marcelo Tosatti | (1UL << (chunk % (sizeof(unsigned long) * 8)))); |
3648 | 7cd1e32a | lirans@il.ibm.com | } else {
|
3649 | 7cd1e32a | lirans@il.ibm.com | return 0; |
3650 | 7cd1e32a | lirans@il.ibm.com | } |
3651 | 7cd1e32a | lirans@il.ibm.com | } |
3652 | 7cd1e32a | lirans@il.ibm.com | |
3653 | a55eb92c | Jan Kiszka | void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
|
3654 | a55eb92c | Jan Kiszka | int nr_sectors)
|
3655 | 7cd1e32a | lirans@il.ibm.com | { |
3656 | 7cd1e32a | lirans@il.ibm.com | set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
|
3657 | 7cd1e32a | lirans@il.ibm.com | } |
3658 | aaa0eb75 | Liran Schour | |
3659 | aaa0eb75 | Liran Schour | int64_t bdrv_get_dirty_count(BlockDriverState *bs) |
3660 | aaa0eb75 | Liran Schour | { |
3661 | aaa0eb75 | Liran Schour | return bs->dirty_count;
|
3662 | aaa0eb75 | Liran Schour | } |
3663 | f88e1a42 | Jes Sorensen | |
3664 | db593f25 | Marcelo Tosatti | void bdrv_set_in_use(BlockDriverState *bs, int in_use) |
3665 | db593f25 | Marcelo Tosatti | { |
3666 | db593f25 | Marcelo Tosatti | assert(bs->in_use != in_use); |
3667 | db593f25 | Marcelo Tosatti | bs->in_use = in_use; |
3668 | db593f25 | Marcelo Tosatti | } |
3669 | db593f25 | Marcelo Tosatti | |
3670 | db593f25 | Marcelo Tosatti | int bdrv_in_use(BlockDriverState *bs)
|
3671 | db593f25 | Marcelo Tosatti | { |
3672 | db593f25 | Marcelo Tosatti | return bs->in_use;
|
3673 | db593f25 | Marcelo Tosatti | } |
3674 | db593f25 | Marcelo Tosatti | |
3675 | 28a7282a | Luiz Capitulino | void bdrv_iostatus_enable(BlockDriverState *bs)
|
3676 | 28a7282a | Luiz Capitulino | { |
3677 | d6bf279e | Luiz Capitulino | bs->iostatus_enabled = true;
|
3678 | 58e21ef5 | Luiz Capitulino | bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; |
3679 | 28a7282a | Luiz Capitulino | } |
3680 | 28a7282a | Luiz Capitulino | |
3681 | 28a7282a | Luiz Capitulino | /* The I/O status is only enabled if the drive explicitly
|
3682 | 28a7282a | Luiz Capitulino | * enables it _and_ the VM is configured to stop on errors */
|
3683 | 28a7282a | Luiz Capitulino | bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) |
3684 | 28a7282a | Luiz Capitulino | { |
3685 | d6bf279e | Luiz Capitulino | return (bs->iostatus_enabled &&
|
3686 | 28a7282a | Luiz Capitulino | (bs->on_write_error == BLOCK_ERR_STOP_ENOSPC || |
3687 | 28a7282a | Luiz Capitulino | bs->on_write_error == BLOCK_ERR_STOP_ANY || |
3688 | 28a7282a | Luiz Capitulino | bs->on_read_error == BLOCK_ERR_STOP_ANY)); |
3689 | 28a7282a | Luiz Capitulino | } |
3690 | 28a7282a | Luiz Capitulino | |
3691 | 28a7282a | Luiz Capitulino | void bdrv_iostatus_disable(BlockDriverState *bs)
|
3692 | 28a7282a | Luiz Capitulino | { |
3693 | d6bf279e | Luiz Capitulino | bs->iostatus_enabled = false;
|
3694 | 28a7282a | Luiz Capitulino | } |
3695 | 28a7282a | Luiz Capitulino | |
3696 | 28a7282a | Luiz Capitulino | void bdrv_iostatus_reset(BlockDriverState *bs)
|
3697 | 28a7282a | Luiz Capitulino | { |
3698 | 28a7282a | Luiz Capitulino | if (bdrv_iostatus_is_enabled(bs)) {
|
3699 | 58e21ef5 | Luiz Capitulino | bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; |
3700 | 28a7282a | Luiz Capitulino | } |
3701 | 28a7282a | Luiz Capitulino | } |
3702 | 28a7282a | Luiz Capitulino | |
3703 | 28a7282a | Luiz Capitulino | /* XXX: Today this is set by device models because it makes the implementation
|
3704 | 28a7282a | Luiz Capitulino | quite simple. However, the block layer knows about the error, so it's
|
3705 | 28a7282a | Luiz Capitulino | possible to implement this without device models being involved */
|
3706 | 28a7282a | Luiz Capitulino | void bdrv_iostatus_set_err(BlockDriverState *bs, int error) |
3707 | 28a7282a | Luiz Capitulino | { |
3708 | 58e21ef5 | Luiz Capitulino | if (bdrv_iostatus_is_enabled(bs) &&
|
3709 | 58e21ef5 | Luiz Capitulino | bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { |
3710 | 28a7282a | Luiz Capitulino | assert(error >= 0);
|
3711 | 58e21ef5 | Luiz Capitulino | bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : |
3712 | 58e21ef5 | Luiz Capitulino | BLOCK_DEVICE_IO_STATUS_FAILED; |
3713 | 28a7282a | Luiz Capitulino | } |
3714 | 28a7282a | Luiz Capitulino | } |
3715 | 28a7282a | Luiz Capitulino | |
3716 | a597e79c | Christoph Hellwig | void
|
3717 | a597e79c | Christoph Hellwig | bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, |
3718 | a597e79c | Christoph Hellwig | enum BlockAcctType type)
|
3719 | a597e79c | Christoph Hellwig | { |
3720 | a597e79c | Christoph Hellwig | assert(type < BDRV_MAX_IOTYPE); |
3721 | a597e79c | Christoph Hellwig | |
3722 | a597e79c | Christoph Hellwig | cookie->bytes = bytes; |
3723 | c488c7f6 | Christoph Hellwig | cookie->start_time_ns = get_clock(); |
3724 | a597e79c | Christoph Hellwig | cookie->type = type; |
3725 | a597e79c | Christoph Hellwig | } |
3726 | a597e79c | Christoph Hellwig | |
3727 | a597e79c | Christoph Hellwig | void
|
3728 | a597e79c | Christoph Hellwig | bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) |
3729 | a597e79c | Christoph Hellwig | { |
3730 | a597e79c | Christoph Hellwig | assert(cookie->type < BDRV_MAX_IOTYPE); |
3731 | a597e79c | Christoph Hellwig | |
3732 | a597e79c | Christoph Hellwig | bs->nr_bytes[cookie->type] += cookie->bytes; |
3733 | a597e79c | Christoph Hellwig | bs->nr_ops[cookie->type]++; |
3734 | c488c7f6 | Christoph Hellwig | bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; |
3735 | a597e79c | Christoph Hellwig | } |
3736 | a597e79c | Christoph Hellwig | |
3737 | f88e1a42 | Jes Sorensen | int bdrv_img_create(const char *filename, const char *fmt, |
3738 | f88e1a42 | Jes Sorensen | const char *base_filename, const char *base_fmt, |
3739 | f88e1a42 | Jes Sorensen | char *options, uint64_t img_size, int flags) |
3740 | f88e1a42 | Jes Sorensen | { |
3741 | f88e1a42 | Jes Sorensen | QEMUOptionParameter *param = NULL, *create_options = NULL; |
3742 | d220894e | Kevin Wolf | QEMUOptionParameter *backing_fmt, *backing_file, *size; |
3743 | f88e1a42 | Jes Sorensen | BlockDriverState *bs = NULL;
|
3744 | f88e1a42 | Jes Sorensen | BlockDriver *drv, *proto_drv; |
3745 | 96df67d1 | Stefan Hajnoczi | BlockDriver *backing_drv = NULL;
|
3746 | f88e1a42 | Jes Sorensen | int ret = 0; |
3747 | f88e1a42 | Jes Sorensen | |
3748 | f88e1a42 | Jes Sorensen | /* Find driver and parse its options */
|
3749 | f88e1a42 | Jes Sorensen | drv = bdrv_find_format(fmt); |
3750 | f88e1a42 | Jes Sorensen | if (!drv) {
|
3751 | f88e1a42 | Jes Sorensen | error_report("Unknown file format '%s'", fmt);
|
3752 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3753 | f88e1a42 | Jes Sorensen | goto out;
|
3754 | f88e1a42 | Jes Sorensen | } |
3755 | f88e1a42 | Jes Sorensen | |
3756 | f88e1a42 | Jes Sorensen | proto_drv = bdrv_find_protocol(filename); |
3757 | f88e1a42 | Jes Sorensen | if (!proto_drv) {
|
3758 | f88e1a42 | Jes Sorensen | error_report("Unknown protocol '%s'", filename);
|
3759 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3760 | f88e1a42 | Jes Sorensen | goto out;
|
3761 | f88e1a42 | Jes Sorensen | } |
3762 | f88e1a42 | Jes Sorensen | |
3763 | f88e1a42 | Jes Sorensen | create_options = append_option_parameters(create_options, |
3764 | f88e1a42 | Jes Sorensen | drv->create_options); |
3765 | f88e1a42 | Jes Sorensen | create_options = append_option_parameters(create_options, |
3766 | f88e1a42 | Jes Sorensen | proto_drv->create_options); |
3767 | f88e1a42 | Jes Sorensen | |
3768 | f88e1a42 | Jes Sorensen | /* Create parameter list with default values */
|
3769 | f88e1a42 | Jes Sorensen | param = parse_option_parameters("", create_options, param);
|
3770 | f88e1a42 | Jes Sorensen | |
3771 | f88e1a42 | Jes Sorensen | set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); |
3772 | f88e1a42 | Jes Sorensen | |
3773 | f88e1a42 | Jes Sorensen | /* Parse -o options */
|
3774 | f88e1a42 | Jes Sorensen | if (options) {
|
3775 | f88e1a42 | Jes Sorensen | param = parse_option_parameters(options, create_options, param); |
3776 | f88e1a42 | Jes Sorensen | if (param == NULL) { |
3777 | f88e1a42 | Jes Sorensen | error_report("Invalid options for file format '%s'.", fmt);
|
3778 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3779 | f88e1a42 | Jes Sorensen | goto out;
|
3780 | f88e1a42 | Jes Sorensen | } |
3781 | f88e1a42 | Jes Sorensen | } |
3782 | f88e1a42 | Jes Sorensen | |
3783 | f88e1a42 | Jes Sorensen | if (base_filename) {
|
3784 | f88e1a42 | Jes Sorensen | if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
|
3785 | f88e1a42 | Jes Sorensen | base_filename)) { |
3786 | f88e1a42 | Jes Sorensen | error_report("Backing file not supported for file format '%s'",
|
3787 | f88e1a42 | Jes Sorensen | fmt); |
3788 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3789 | f88e1a42 | Jes Sorensen | goto out;
|
3790 | f88e1a42 | Jes Sorensen | } |
3791 | f88e1a42 | Jes Sorensen | } |
3792 | f88e1a42 | Jes Sorensen | |
3793 | f88e1a42 | Jes Sorensen | if (base_fmt) {
|
3794 | f88e1a42 | Jes Sorensen | if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) {
|
3795 | f88e1a42 | Jes Sorensen | error_report("Backing file format not supported for file "
|
3796 | f88e1a42 | Jes Sorensen | "format '%s'", fmt);
|
3797 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3798 | f88e1a42 | Jes Sorensen | goto out;
|
3799 | f88e1a42 | Jes Sorensen | } |
3800 | f88e1a42 | Jes Sorensen | } |
3801 | f88e1a42 | Jes Sorensen | |
3802 | 792da93a | Jes Sorensen | backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); |
3803 | 792da93a | Jes Sorensen | if (backing_file && backing_file->value.s) {
|
3804 | 792da93a | Jes Sorensen | if (!strcmp(filename, backing_file->value.s)) {
|
3805 | 792da93a | Jes Sorensen | error_report("Error: Trying to create an image with the "
|
3806 | 792da93a | Jes Sorensen | "same filename as the backing file");
|
3807 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3808 | 792da93a | Jes Sorensen | goto out;
|
3809 | 792da93a | Jes Sorensen | } |
3810 | 792da93a | Jes Sorensen | } |
3811 | 792da93a | Jes Sorensen | |
3812 | f88e1a42 | Jes Sorensen | backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); |
3813 | f88e1a42 | Jes Sorensen | if (backing_fmt && backing_fmt->value.s) {
|
3814 | 96df67d1 | Stefan Hajnoczi | backing_drv = bdrv_find_format(backing_fmt->value.s); |
3815 | 96df67d1 | Stefan Hajnoczi | if (!backing_drv) {
|
3816 | f88e1a42 | Jes Sorensen | error_report("Unknown backing file format '%s'",
|
3817 | f88e1a42 | Jes Sorensen | backing_fmt->value.s); |
3818 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3819 | f88e1a42 | Jes Sorensen | goto out;
|
3820 | f88e1a42 | Jes Sorensen | } |
3821 | f88e1a42 | Jes Sorensen | } |
3822 | f88e1a42 | Jes Sorensen | |
3823 | f88e1a42 | Jes Sorensen | // The size for the image must always be specified, with one exception:
|
3824 | f88e1a42 | Jes Sorensen | // If we are using a backing file, we can obtain the size from there
|
3825 | d220894e | Kevin Wolf | size = get_option_parameter(param, BLOCK_OPT_SIZE); |
3826 | d220894e | Kevin Wolf | if (size && size->value.n == -1) { |
3827 | f88e1a42 | Jes Sorensen | if (backing_file && backing_file->value.s) {
|
3828 | f88e1a42 | Jes Sorensen | uint64_t size; |
3829 | f88e1a42 | Jes Sorensen | char buf[32]; |
3830 | f88e1a42 | Jes Sorensen | |
3831 | f88e1a42 | Jes Sorensen | bs = bdrv_new("");
|
3832 | f88e1a42 | Jes Sorensen | |
3833 | 96df67d1 | Stefan Hajnoczi | ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv); |
3834 | f88e1a42 | Jes Sorensen | if (ret < 0) { |
3835 | 96df67d1 | Stefan Hajnoczi | error_report("Could not open '%s'", backing_file->value.s);
|
3836 | f88e1a42 | Jes Sorensen | goto out;
|
3837 | f88e1a42 | Jes Sorensen | } |
3838 | f88e1a42 | Jes Sorensen | bdrv_get_geometry(bs, &size); |
3839 | f88e1a42 | Jes Sorensen | size *= 512;
|
3840 | f88e1a42 | Jes Sorensen | |
3841 | f88e1a42 | Jes Sorensen | snprintf(buf, sizeof(buf), "%" PRId64, size); |
3842 | f88e1a42 | Jes Sorensen | set_option_parameter(param, BLOCK_OPT_SIZE, buf); |
3843 | f88e1a42 | Jes Sorensen | } else {
|
3844 | f88e1a42 | Jes Sorensen | error_report("Image creation needs a size parameter");
|
3845 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3846 | f88e1a42 | Jes Sorensen | goto out;
|
3847 | f88e1a42 | Jes Sorensen | } |
3848 | f88e1a42 | Jes Sorensen | } |
3849 | f88e1a42 | Jes Sorensen | |
3850 | f88e1a42 | Jes Sorensen | printf("Formatting '%s', fmt=%s ", filename, fmt);
|
3851 | f88e1a42 | Jes Sorensen | print_option_parameters(param); |
3852 | f88e1a42 | Jes Sorensen | puts("");
|
3853 | f88e1a42 | Jes Sorensen | |
3854 | f88e1a42 | Jes Sorensen | ret = bdrv_create(drv, filename, param); |
3855 | f88e1a42 | Jes Sorensen | |
3856 | f88e1a42 | Jes Sorensen | if (ret < 0) { |
3857 | f88e1a42 | Jes Sorensen | if (ret == -ENOTSUP) {
|
3858 | f88e1a42 | Jes Sorensen | error_report("Formatting or formatting option not supported for "
|
3859 | f88e1a42 | Jes Sorensen | "file format '%s'", fmt);
|
3860 | f88e1a42 | Jes Sorensen | } else if (ret == -EFBIG) { |
3861 | f88e1a42 | Jes Sorensen | error_report("The image size is too large for file format '%s'",
|
3862 | f88e1a42 | Jes Sorensen | fmt); |
3863 | f88e1a42 | Jes Sorensen | } else {
|
3864 | f88e1a42 | Jes Sorensen | error_report("%s: error while creating %s: %s", filename, fmt,
|
3865 | f88e1a42 | Jes Sorensen | strerror(-ret)); |
3866 | f88e1a42 | Jes Sorensen | } |
3867 | f88e1a42 | Jes Sorensen | } |
3868 | f88e1a42 | Jes Sorensen | |
3869 | f88e1a42 | Jes Sorensen | out:
|
3870 | f88e1a42 | Jes Sorensen | free_option_parameters(create_options); |
3871 | f88e1a42 | Jes Sorensen | free_option_parameters(param); |
3872 | f88e1a42 | Jes Sorensen | |
3873 | f88e1a42 | Jes Sorensen | if (bs) {
|
3874 | f88e1a42 | Jes Sorensen | bdrv_delete(bs); |
3875 | f88e1a42 | Jes Sorensen | } |
3876 | 4f70f249 | Jes Sorensen | |
3877 | 4f70f249 | Jes Sorensen | return ret;
|
3878 | f88e1a42 | Jes Sorensen | } |
3879 | eeec61f2 | Stefan Hajnoczi | |
3880 | eeec61f2 | Stefan Hajnoczi | void *block_job_create(const BlockJobType *job_type, BlockDriverState *bs, |
3881 | eeec61f2 | Stefan Hajnoczi | BlockDriverCompletionFunc *cb, void *opaque)
|
3882 | eeec61f2 | Stefan Hajnoczi | { |
3883 | eeec61f2 | Stefan Hajnoczi | BlockJob *job; |
3884 | eeec61f2 | Stefan Hajnoczi | |
3885 | eeec61f2 | Stefan Hajnoczi | if (bs->job || bdrv_in_use(bs)) {
|
3886 | eeec61f2 | Stefan Hajnoczi | return NULL; |
3887 | eeec61f2 | Stefan Hajnoczi | } |
3888 | eeec61f2 | Stefan Hajnoczi | bdrv_set_in_use(bs, 1);
|
3889 | eeec61f2 | Stefan Hajnoczi | |
3890 | eeec61f2 | Stefan Hajnoczi | job = g_malloc0(job_type->instance_size); |
3891 | eeec61f2 | Stefan Hajnoczi | job->job_type = job_type; |
3892 | eeec61f2 | Stefan Hajnoczi | job->bs = bs; |
3893 | eeec61f2 | Stefan Hajnoczi | job->cb = cb; |
3894 | eeec61f2 | Stefan Hajnoczi | job->opaque = opaque; |
3895 | eeec61f2 | Stefan Hajnoczi | bs->job = job; |
3896 | eeec61f2 | Stefan Hajnoczi | return job;
|
3897 | eeec61f2 | Stefan Hajnoczi | } |
3898 | eeec61f2 | Stefan Hajnoczi | |
3899 | eeec61f2 | Stefan Hajnoczi | void block_job_complete(BlockJob *job, int ret) |
3900 | eeec61f2 | Stefan Hajnoczi | { |
3901 | eeec61f2 | Stefan Hajnoczi | BlockDriverState *bs = job->bs; |
3902 | eeec61f2 | Stefan Hajnoczi | |
3903 | eeec61f2 | Stefan Hajnoczi | assert(bs->job == job); |
3904 | eeec61f2 | Stefan Hajnoczi | job->cb(job->opaque, ret); |
3905 | eeec61f2 | Stefan Hajnoczi | bs->job = NULL;
|
3906 | eeec61f2 | Stefan Hajnoczi | g_free(job); |
3907 | eeec61f2 | Stefan Hajnoczi | bdrv_set_in_use(bs, 0);
|
3908 | eeec61f2 | Stefan Hajnoczi | } |
3909 | eeec61f2 | Stefan Hajnoczi | |
3910 | eeec61f2 | Stefan Hajnoczi | int block_job_set_speed(BlockJob *job, int64_t value)
|
3911 | eeec61f2 | Stefan Hajnoczi | { |
3912 | eeec61f2 | Stefan Hajnoczi | if (!job->job_type->set_speed) {
|
3913 | eeec61f2 | Stefan Hajnoczi | return -ENOTSUP;
|
3914 | eeec61f2 | Stefan Hajnoczi | } |
3915 | eeec61f2 | Stefan Hajnoczi | return job->job_type->set_speed(job, value);
|
3916 | eeec61f2 | Stefan Hajnoczi | } |
3917 | eeec61f2 | Stefan Hajnoczi | |
3918 | eeec61f2 | Stefan Hajnoczi | void block_job_cancel(BlockJob *job)
|
3919 | eeec61f2 | Stefan Hajnoczi | { |
3920 | eeec61f2 | Stefan Hajnoczi | job->cancelled = true;
|
3921 | eeec61f2 | Stefan Hajnoczi | } |
3922 | eeec61f2 | Stefan Hajnoczi | |
3923 | eeec61f2 | Stefan Hajnoczi | bool block_job_is_cancelled(BlockJob *job)
|
3924 | eeec61f2 | Stefan Hajnoczi | { |
3925 | eeec61f2 | Stefan Hajnoczi | return job->cancelled;
|
3926 | eeec61f2 | Stefan Hajnoczi | } |