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