]>
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 | ||
145feb17 | 47 | static void bdrv_dev_change_media_cb(BlockDriverState *bs); |
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)) { | |
145feb17 | 691 | bdrv_dev_change_media_cb(bs); |
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 | ||
145feb17 | 727 | bdrv_dev_change_media_cb(bs); |
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; | |
18846dee MA |
791 | } |
792 | ||
fa879d62 MA |
793 | /* TODO change to return DeviceState * when all users are qdevified */ |
794 | void *bdrv_get_attached_dev(BlockDriverState *bs) | |
18846dee | 795 | { |
fa879d62 | 796 | return bs->dev; |
18846dee MA |
797 | } |
798 | ||
0e49de52 MA |
799 | void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, |
800 | void *opaque) | |
801 | { | |
802 | bs->dev_ops = ops; | |
803 | bs->dev_opaque = opaque; | |
2c6942fa MA |
804 | if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) { |
805 | bs_snapshots = NULL; | |
806 | } | |
0e49de52 MA |
807 | } |
808 | ||
145feb17 | 809 | static void bdrv_dev_change_media_cb(BlockDriverState *bs) |
0e49de52 | 810 | { |
145feb17 MA |
811 | if (bs->dev_ops && bs->dev_ops->change_media_cb) { |
812 | bs->dev_ops->change_media_cb(bs->dev_opaque); | |
813 | } | |
814 | } | |
815 | ||
2c6942fa MA |
816 | bool bdrv_dev_has_removable_media(BlockDriverState *bs) |
817 | { | |
818 | return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb); | |
819 | } | |
820 | ||
e4def80b MA |
821 | bool bdrv_dev_is_tray_open(BlockDriverState *bs) |
822 | { | |
823 | if (bs->dev_ops && bs->dev_ops->is_tray_open) { | |
824 | return bs->dev_ops->is_tray_open(bs->dev_opaque); | |
825 | } | |
826 | return false; | |
827 | } | |
828 | ||
145feb17 MA |
829 | static void bdrv_dev_resize_cb(BlockDriverState *bs) |
830 | { | |
831 | if (bs->dev_ops && bs->dev_ops->resize_cb) { | |
832 | bs->dev_ops->resize_cb(bs->dev_opaque); | |
0e49de52 MA |
833 | } |
834 | } | |
835 | ||
f107639a MA |
836 | bool bdrv_dev_is_medium_locked(BlockDriverState *bs) |
837 | { | |
838 | if (bs->dev_ops && bs->dev_ops->is_medium_locked) { | |
839 | return bs->dev_ops->is_medium_locked(bs->dev_opaque); | |
840 | } | |
841 | return false; | |
842 | } | |
843 | ||
e97fc193 AL |
844 | /* |
845 | * Run consistency checks on an image | |
846 | * | |
e076f338 | 847 | * Returns 0 if the check could be completed (it doesn't mean that the image is |
a1c7273b | 848 | * free of errors) or -errno when an internal error occurred. The results of the |
e076f338 | 849 | * check are stored in res. |
e97fc193 | 850 | */ |
e076f338 | 851 | int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res) |
e97fc193 AL |
852 | { |
853 | if (bs->drv->bdrv_check == NULL) { | |
854 | return -ENOTSUP; | |
855 | } | |
856 | ||
e076f338 | 857 | memset(res, 0, sizeof(*res)); |
9ac228e0 | 858 | return bs->drv->bdrv_check(bs, res); |
e97fc193 AL |
859 | } |
860 | ||
8a426614 KW |
861 | #define COMMIT_BUF_SECTORS 2048 |
862 | ||
33e3963e FB |
863 | /* commit COW file into the raw image */ |
864 | int bdrv_commit(BlockDriverState *bs) | |
865 | { | |
19cb3738 | 866 | BlockDriver *drv = bs->drv; |
ee181196 | 867 | BlockDriver *backing_drv; |
8a426614 KW |
868 | int64_t sector, total_sectors; |
869 | int n, ro, open_flags; | |
4dca4b63 | 870 | int ret = 0, rw_ret = 0; |
8a426614 | 871 | uint8_t *buf; |
4dca4b63 NS |
872 | char filename[1024]; |
873 | BlockDriverState *bs_rw, *bs_ro; | |
33e3963e | 874 | |
19cb3738 FB |
875 | if (!drv) |
876 | return -ENOMEDIUM; | |
4dca4b63 NS |
877 | |
878 | if (!bs->backing_hd) { | |
879 | return -ENOTSUP; | |
33e3963e FB |
880 | } |
881 | ||
4dca4b63 NS |
882 | if (bs->backing_hd->keep_read_only) { |
883 | return -EACCES; | |
884 | } | |
ee181196 KW |
885 | |
886 | backing_drv = bs->backing_hd->drv; | |
4dca4b63 NS |
887 | ro = bs->backing_hd->read_only; |
888 | strncpy(filename, bs->backing_hd->filename, sizeof(filename)); | |
889 | open_flags = bs->backing_hd->open_flags; | |
890 | ||
891 | if (ro) { | |
892 | /* re-open as RW */ | |
893 | bdrv_delete(bs->backing_hd); | |
894 | bs->backing_hd = NULL; | |
895 | bs_rw = bdrv_new(""); | |
ee181196 KW |
896 | rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, |
897 | backing_drv); | |
4dca4b63 NS |
898 | if (rw_ret < 0) { |
899 | bdrv_delete(bs_rw); | |
900 | /* try to re-open read-only */ | |
901 | bs_ro = bdrv_new(""); | |
ee181196 KW |
902 | ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, |
903 | backing_drv); | |
4dca4b63 NS |
904 | if (ret < 0) { |
905 | bdrv_delete(bs_ro); | |
906 | /* drive not functional anymore */ | |
907 | bs->drv = NULL; | |
908 | return ret; | |
909 | } | |
910 | bs->backing_hd = bs_ro; | |
911 | return rw_ret; | |
912 | } | |
913 | bs->backing_hd = bs_rw; | |
ea2384d3 | 914 | } |
33e3963e | 915 | |
6ea44308 | 916 | total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; |
7267c094 | 917 | buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); |
8a426614 KW |
918 | |
919 | for (sector = 0; sector < total_sectors; sector += n) { | |
920 | if (drv->bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) { | |
921 | ||
922 | if (bdrv_read(bs, sector, buf, n) != 0) { | |
923 | ret = -EIO; | |
924 | goto ro_cleanup; | |
925 | } | |
926 | ||
927 | if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { | |
928 | ret = -EIO; | |
929 | goto ro_cleanup; | |
930 | } | |
ea2384d3 | 931 | } |
33e3963e | 932 | } |
95389c86 | 933 | |
1d44952f CH |
934 | if (drv->bdrv_make_empty) { |
935 | ret = drv->bdrv_make_empty(bs); | |
936 | bdrv_flush(bs); | |
937 | } | |
95389c86 | 938 | |
3f5075ae CH |
939 | /* |
940 | * Make sure all data we wrote to the backing device is actually | |
941 | * stable on disk. | |
942 | */ | |
943 | if (bs->backing_hd) | |
944 | bdrv_flush(bs->backing_hd); | |
4dca4b63 NS |
945 | |
946 | ro_cleanup: | |
7267c094 | 947 | g_free(buf); |
4dca4b63 NS |
948 | |
949 | if (ro) { | |
950 | /* re-open as RO */ | |
951 | bdrv_delete(bs->backing_hd); | |
952 | bs->backing_hd = NULL; | |
953 | bs_ro = bdrv_new(""); | |
ee181196 KW |
954 | ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, |
955 | backing_drv); | |
4dca4b63 NS |
956 | if (ret < 0) { |
957 | bdrv_delete(bs_ro); | |
958 | /* drive not functional anymore */ | |
959 | bs->drv = NULL; | |
960 | return ret; | |
961 | } | |
962 | bs->backing_hd = bs_ro; | |
963 | bs->backing_hd->keep_read_only = 0; | |
964 | } | |
965 | ||
1d44952f | 966 | return ret; |
33e3963e FB |
967 | } |
968 | ||
6ab4b5ab MA |
969 | void bdrv_commit_all(void) |
970 | { | |
971 | BlockDriverState *bs; | |
972 | ||
973 | QTAILQ_FOREACH(bs, &bdrv_states, list) { | |
974 | bdrv_commit(bs); | |
975 | } | |
976 | } | |
977 | ||
756e6736 KW |
978 | /* |
979 | * Return values: | |
980 | * 0 - success | |
981 | * -EINVAL - backing format specified, but no file | |
982 | * -ENOSPC - can't update the backing file because no space is left in the | |
983 | * image file header | |
984 | * -ENOTSUP - format driver doesn't support changing the backing file | |
985 | */ | |
986 | int bdrv_change_backing_file(BlockDriverState *bs, | |
987 | const char *backing_file, const char *backing_fmt) | |
988 | { | |
989 | BlockDriver *drv = bs->drv; | |
990 | ||
991 | if (drv->bdrv_change_backing_file != NULL) { | |
992 | return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); | |
993 | } else { | |
994 | return -ENOTSUP; | |
995 | } | |
996 | } | |
997 | ||
71d0770c AL |
998 | static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, |
999 | size_t size) | |
1000 | { | |
1001 | int64_t len; | |
1002 | ||
1003 | if (!bdrv_is_inserted(bs)) | |
1004 | return -ENOMEDIUM; | |
1005 | ||
1006 | if (bs->growable) | |
1007 | return 0; | |
1008 | ||
1009 | len = bdrv_getlength(bs); | |
1010 | ||
fbb7b4e0 KW |
1011 | if (offset < 0) |
1012 | return -EIO; | |
1013 | ||
1014 | if ((offset > len) || (len - offset < size)) | |
71d0770c AL |
1015 | return -EIO; |
1016 | ||
1017 | return 0; | |
1018 | } | |
1019 | ||
1020 | static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, | |
1021 | int nb_sectors) | |
1022 | { | |
eb5a3165 JS |
1023 | return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, |
1024 | nb_sectors * BDRV_SECTOR_SIZE); | |
71d0770c AL |
1025 | } |
1026 | ||
e7a8a783 KW |
1027 | static inline bool bdrv_has_async_rw(BlockDriver *drv) |
1028 | { | |
1029 | return drv->bdrv_co_readv != bdrv_co_readv_em | |
1030 | || drv->bdrv_aio_readv != bdrv_aio_readv_em; | |
1031 | } | |
1032 | ||
1033 | static inline bool bdrv_has_async_flush(BlockDriver *drv) | |
1034 | { | |
1035 | return drv->bdrv_aio_flush != bdrv_aio_flush_em; | |
1036 | } | |
1037 | ||
19cb3738 | 1038 | /* return < 0 if error. See bdrv_write() for the return codes */ |
5fafdf24 | 1039 | int bdrv_read(BlockDriverState *bs, int64_t sector_num, |
fc01f7e7 FB |
1040 | uint8_t *buf, int nb_sectors) |
1041 | { | |
ea2384d3 FB |
1042 | BlockDriver *drv = bs->drv; |
1043 | ||
19cb3738 FB |
1044 | if (!drv) |
1045 | return -ENOMEDIUM; | |
e7a8a783 KW |
1046 | |
1047 | if (bdrv_has_async_rw(drv) && qemu_in_coroutine()) { | |
1048 | QEMUIOVector qiov; | |
1049 | struct iovec iov = { | |
1050 | .iov_base = (void *)buf, | |
1051 | .iov_len = nb_sectors * BDRV_SECTOR_SIZE, | |
1052 | }; | |
1053 | ||
1054 | qemu_iovec_init_external(&qiov, &iov, 1); | |
1055 | return bdrv_co_readv(bs, sector_num, nb_sectors, &qiov); | |
1056 | } | |
1057 | ||
71d0770c AL |
1058 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
1059 | return -EIO; | |
b338082b | 1060 | |
eda578e5 | 1061 | return drv->bdrv_read(bs, sector_num, buf, nb_sectors); |
fc01f7e7 FB |
1062 | } |
1063 | ||
7cd1e32a | 1064 | static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, |
a55eb92c | 1065 | int nb_sectors, int dirty) |
7cd1e32a | 1066 | { |
1067 | int64_t start, end; | |
c6d22830 | 1068 | unsigned long val, idx, bit; |
a55eb92c | 1069 | |
6ea44308 | 1070 | start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; |
c6d22830 | 1071 | end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK; |
a55eb92c JK |
1072 | |
1073 | for (; start <= end; start++) { | |
c6d22830 JK |
1074 | idx = start / (sizeof(unsigned long) * 8); |
1075 | bit = start % (sizeof(unsigned long) * 8); | |
1076 | val = bs->dirty_bitmap[idx]; | |
1077 | if (dirty) { | |
6d59fec1 | 1078 | if (!(val & (1UL << bit))) { |
aaa0eb75 | 1079 | bs->dirty_count++; |
6d59fec1 | 1080 | val |= 1UL << bit; |
aaa0eb75 | 1081 | } |
c6d22830 | 1082 | } else { |
6d59fec1 | 1083 | if (val & (1UL << bit)) { |
aaa0eb75 | 1084 | bs->dirty_count--; |
6d59fec1 | 1085 | val &= ~(1UL << bit); |
aaa0eb75 | 1086 | } |
c6d22830 JK |
1087 | } |
1088 | bs->dirty_bitmap[idx] = val; | |
7cd1e32a | 1089 | } |
1090 | } | |
1091 | ||
5fafdf24 | 1092 | /* Return < 0 if error. Important errors are: |
19cb3738 FB |
1093 | -EIO generic I/O error (may happen for all errors) |
1094 | -ENOMEDIUM No media inserted. | |
1095 | -EINVAL Invalid sector number or nb_sectors | |
1096 | -EACCES Trying to write a read-only device | |
1097 | */ | |
5fafdf24 | 1098 | int bdrv_write(BlockDriverState *bs, int64_t sector_num, |
fc01f7e7 FB |
1099 | const uint8_t *buf, int nb_sectors) |
1100 | { | |
83f64091 | 1101 | BlockDriver *drv = bs->drv; |
e7a8a783 | 1102 | |
19cb3738 FB |
1103 | if (!bs->drv) |
1104 | return -ENOMEDIUM; | |
e7a8a783 KW |
1105 | |
1106 | if (bdrv_has_async_rw(drv) && qemu_in_coroutine()) { | |
1107 | QEMUIOVector qiov; | |
1108 | struct iovec iov = { | |
1109 | .iov_base = (void *)buf, | |
1110 | .iov_len = nb_sectors * BDRV_SECTOR_SIZE, | |
1111 | }; | |
1112 | ||
1113 | qemu_iovec_init_external(&qiov, &iov, 1); | |
1114 | return bdrv_co_writev(bs, sector_num, nb_sectors, &qiov); | |
1115 | } | |
1116 | ||
0849bf08 | 1117 | if (bs->read_only) |
19cb3738 | 1118 | return -EACCES; |
71d0770c AL |
1119 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
1120 | return -EIO; | |
a55eb92c | 1121 | |
c6d22830 | 1122 | if (bs->dirty_bitmap) { |
7cd1e32a | 1123 | set_dirty_bitmap(bs, sector_num, nb_sectors, 1); |
1124 | } | |
a55eb92c | 1125 | |
294cc35f KW |
1126 | if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { |
1127 | bs->wr_highest_sector = sector_num + nb_sectors - 1; | |
1128 | } | |
1129 | ||
42fb2807 | 1130 | return drv->bdrv_write(bs, sector_num, buf, nb_sectors); |
83f64091 FB |
1131 | } |
1132 | ||
eda578e5 AL |
1133 | int bdrv_pread(BlockDriverState *bs, int64_t offset, |
1134 | void *buf, int count1) | |
83f64091 | 1135 | { |
6ea44308 | 1136 | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
83f64091 FB |
1137 | int len, nb_sectors, count; |
1138 | int64_t sector_num; | |
9a8c4cce | 1139 | int ret; |
83f64091 FB |
1140 | |
1141 | count = count1; | |
1142 | /* first read to align to sector start */ | |
6ea44308 | 1143 | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); |
83f64091 FB |
1144 | if (len > count) |
1145 | len = count; | |
6ea44308 | 1146 | sector_num = offset >> BDRV_SECTOR_BITS; |
83f64091 | 1147 | if (len > 0) { |
9a8c4cce KW |
1148 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
1149 | return ret; | |
6ea44308 | 1150 | memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); |
83f64091 FB |
1151 | count -= len; |
1152 | if (count == 0) | |
1153 | return count1; | |
1154 | sector_num++; | |
1155 | buf += len; | |
1156 | } | |
1157 | ||
1158 | /* read the sectors "in place" */ | |
6ea44308 | 1159 | nb_sectors = count >> BDRV_SECTOR_BITS; |
83f64091 | 1160 | if (nb_sectors > 0) { |
9a8c4cce KW |
1161 | if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) |
1162 | return ret; | |
83f64091 | 1163 | sector_num += nb_sectors; |
6ea44308 | 1164 | len = nb_sectors << BDRV_SECTOR_BITS; |
83f64091 FB |
1165 | buf += len; |
1166 | count -= len; | |
1167 | } | |
1168 | ||
1169 | /* add data from the last sector */ | |
1170 | if (count > 0) { | |
9a8c4cce KW |
1171 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
1172 | return ret; | |
83f64091 FB |
1173 | memcpy(buf, tmp_buf, count); |
1174 | } | |
1175 | return count1; | |
1176 | } | |
1177 | ||
eda578e5 AL |
1178 | int bdrv_pwrite(BlockDriverState *bs, int64_t offset, |
1179 | const void *buf, int count1) | |
83f64091 | 1180 | { |
6ea44308 | 1181 | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
83f64091 FB |
1182 | int len, nb_sectors, count; |
1183 | int64_t sector_num; | |
9a8c4cce | 1184 | int ret; |
83f64091 FB |
1185 | |
1186 | count = count1; | |
1187 | /* first write to align to sector start */ | |
6ea44308 | 1188 | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); |
83f64091 FB |
1189 | if (len > count) |
1190 | len = count; | |
6ea44308 | 1191 | sector_num = offset >> BDRV_SECTOR_BITS; |
83f64091 | 1192 | if (len > 0) { |
9a8c4cce KW |
1193 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
1194 | return ret; | |
6ea44308 | 1195 | memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len); |
9a8c4cce KW |
1196 | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
1197 | return ret; | |
83f64091 FB |
1198 | count -= len; |
1199 | if (count == 0) | |
1200 | return count1; | |
1201 | sector_num++; | |
1202 | buf += len; | |
1203 | } | |
1204 | ||
1205 | /* write the sectors "in place" */ | |
6ea44308 | 1206 | nb_sectors = count >> BDRV_SECTOR_BITS; |
83f64091 | 1207 | if (nb_sectors > 0) { |
9a8c4cce KW |
1208 | if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) |
1209 | return ret; | |
83f64091 | 1210 | sector_num += nb_sectors; |
6ea44308 | 1211 | len = nb_sectors << BDRV_SECTOR_BITS; |
83f64091 FB |
1212 | buf += len; |
1213 | count -= len; | |
1214 | } | |
1215 | ||
1216 | /* add data from the last sector */ | |
1217 | if (count > 0) { | |
9a8c4cce KW |
1218 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
1219 | return ret; | |
83f64091 | 1220 | memcpy(tmp_buf, buf, count); |
9a8c4cce KW |
1221 | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
1222 | return ret; | |
83f64091 FB |
1223 | } |
1224 | return count1; | |
1225 | } | |
83f64091 | 1226 | |
f08145fe KW |
1227 | /* |
1228 | * Writes to the file and ensures that no writes are reordered across this | |
1229 | * request (acts as a barrier) | |
1230 | * | |
1231 | * Returns 0 on success, -errno in error cases. | |
1232 | */ | |
1233 | int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, | |
1234 | const void *buf, int count) | |
1235 | { | |
1236 | int ret; | |
1237 | ||
1238 | ret = bdrv_pwrite(bs, offset, buf, count); | |
1239 | if (ret < 0) { | |
1240 | return ret; | |
1241 | } | |
1242 | ||
92196b2f SH |
1243 | /* No flush needed for cache modes that use O_DSYNC */ |
1244 | if ((bs->open_flags & BDRV_O_CACHE_WB) != 0) { | |
f08145fe KW |
1245 | bdrv_flush(bs); |
1246 | } | |
1247 | ||
1248 | return 0; | |
1249 | } | |
1250 | ||
da1fa91d KW |
1251 | int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, |
1252 | int nb_sectors, QEMUIOVector *qiov) | |
1253 | { | |
1254 | BlockDriver *drv = bs->drv; | |
1255 | ||
1256 | trace_bdrv_co_readv(bs, sector_num, nb_sectors); | |
1257 | ||
1258 | if (!drv) { | |
1259 | return -ENOMEDIUM; | |
1260 | } | |
1261 | if (bdrv_check_request(bs, sector_num, nb_sectors)) { | |
1262 | return -EIO; | |
1263 | } | |
1264 | ||
1265 | return drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); | |
1266 | } | |
1267 | ||
1268 | int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, | |
1269 | int nb_sectors, QEMUIOVector *qiov) | |
1270 | { | |
1271 | BlockDriver *drv = bs->drv; | |
1272 | ||
1273 | trace_bdrv_co_writev(bs, sector_num, nb_sectors); | |
1274 | ||
1275 | if (!bs->drv) { | |
1276 | return -ENOMEDIUM; | |
1277 | } | |
1278 | if (bs->read_only) { | |
1279 | return -EACCES; | |
1280 | } | |
1281 | if (bdrv_check_request(bs, sector_num, nb_sectors)) { | |
1282 | return -EIO; | |
1283 | } | |
1284 | ||
1285 | if (bs->dirty_bitmap) { | |
1286 | set_dirty_bitmap(bs, sector_num, nb_sectors, 1); | |
1287 | } | |
1288 | ||
1289 | if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { | |
1290 | bs->wr_highest_sector = sector_num + nb_sectors - 1; | |
1291 | } | |
1292 | ||
1293 | return drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); | |
1294 | } | |
1295 | ||
83f64091 FB |
1296 | /** |
1297 | * Truncate file to 'offset' bytes (needed only for file protocols) | |
1298 | */ | |
1299 | int bdrv_truncate(BlockDriverState *bs, int64_t offset) | |
1300 | { | |
1301 | BlockDriver *drv = bs->drv; | |
51762288 | 1302 | int ret; |
83f64091 | 1303 | if (!drv) |
19cb3738 | 1304 | return -ENOMEDIUM; |
83f64091 FB |
1305 | if (!drv->bdrv_truncate) |
1306 | return -ENOTSUP; | |
59f2689d NS |
1307 | if (bs->read_only) |
1308 | return -EACCES; | |
8591675f MT |
1309 | if (bdrv_in_use(bs)) |
1310 | return -EBUSY; | |
51762288 SH |
1311 | ret = drv->bdrv_truncate(bs, offset); |
1312 | if (ret == 0) { | |
1313 | ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); | |
145feb17 | 1314 | bdrv_dev_resize_cb(bs); |
51762288 SH |
1315 | } |
1316 | return ret; | |
83f64091 FB |
1317 | } |
1318 | ||
4a1d5e1f FZ |
1319 | /** |
1320 | * Length of a allocated file in bytes. Sparse files are counted by actual | |
1321 | * allocated space. Return < 0 if error or unknown. | |
1322 | */ | |
1323 | int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) | |
1324 | { | |
1325 | BlockDriver *drv = bs->drv; | |
1326 | if (!drv) { | |
1327 | return -ENOMEDIUM; | |
1328 | } | |
1329 | if (drv->bdrv_get_allocated_file_size) { | |
1330 | return drv->bdrv_get_allocated_file_size(bs); | |
1331 | } | |
1332 | if (bs->file) { | |
1333 | return bdrv_get_allocated_file_size(bs->file); | |
1334 | } | |
1335 | return -ENOTSUP; | |
1336 | } | |
1337 | ||
83f64091 FB |
1338 | /** |
1339 | * Length of a file in bytes. Return < 0 if error or unknown. | |
1340 | */ | |
1341 | int64_t bdrv_getlength(BlockDriverState *bs) | |
1342 | { | |
1343 | BlockDriver *drv = bs->drv; | |
1344 | if (!drv) | |
19cb3738 | 1345 | return -ENOMEDIUM; |
51762288 | 1346 | |
2c6942fa | 1347 | if (bs->growable || bdrv_dev_has_removable_media(bs)) { |
46a4e4e6 SH |
1348 | if (drv->bdrv_getlength) { |
1349 | return drv->bdrv_getlength(bs); | |
1350 | } | |
83f64091 | 1351 | } |
46a4e4e6 | 1352 | return bs->total_sectors * BDRV_SECTOR_SIZE; |
fc01f7e7 FB |
1353 | } |
1354 | ||
19cb3738 | 1355 | /* return 0 as number of sectors if no device present or error */ |
96b8f136 | 1356 | void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) |
fc01f7e7 | 1357 | { |
19cb3738 FB |
1358 | int64_t length; |
1359 | length = bdrv_getlength(bs); | |
1360 | if (length < 0) | |
1361 | length = 0; | |
1362 | else | |
6ea44308 | 1363 | length = length >> BDRV_SECTOR_BITS; |
19cb3738 | 1364 | *nb_sectors_ptr = length; |
fc01f7e7 | 1365 | } |
cf98951b | 1366 | |
f3d54fc4 AL |
1367 | struct partition { |
1368 | uint8_t boot_ind; /* 0x80 - active */ | |
1369 | uint8_t head; /* starting head */ | |
1370 | uint8_t sector; /* starting sector */ | |
1371 | uint8_t cyl; /* starting cylinder */ | |
1372 | uint8_t sys_ind; /* What partition type */ | |
1373 | uint8_t end_head; /* end head */ | |
1374 | uint8_t end_sector; /* end sector */ | |
1375 | uint8_t end_cyl; /* end cylinder */ | |
1376 | uint32_t start_sect; /* starting sector counting from 0 */ | |
1377 | uint32_t nr_sects; /* nr of sectors in partition */ | |
541dc0d4 | 1378 | } QEMU_PACKED; |
f3d54fc4 AL |
1379 | |
1380 | /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ | |
1381 | static int guess_disk_lchs(BlockDriverState *bs, | |
1382 | int *pcylinders, int *pheads, int *psectors) | |
1383 | { | |
eb5a3165 | 1384 | uint8_t buf[BDRV_SECTOR_SIZE]; |
f3d54fc4 AL |
1385 | int ret, i, heads, sectors, cylinders; |
1386 | struct partition *p; | |
1387 | uint32_t nr_sects; | |
a38131b6 | 1388 | uint64_t nb_sectors; |
f3d54fc4 AL |
1389 | |
1390 | bdrv_get_geometry(bs, &nb_sectors); | |
1391 | ||
1392 | ret = bdrv_read(bs, 0, buf, 1); | |
1393 | if (ret < 0) | |
1394 | return -1; | |
1395 | /* test msdos magic */ | |
1396 | if (buf[510] != 0x55 || buf[511] != 0xaa) | |
1397 | return -1; | |
1398 | for(i = 0; i < 4; i++) { | |
1399 | p = ((struct partition *)(buf + 0x1be)) + i; | |
1400 | nr_sects = le32_to_cpu(p->nr_sects); | |
1401 | if (nr_sects && p->end_head) { | |
1402 | /* We make the assumption that the partition terminates on | |
1403 | a cylinder boundary */ | |
1404 | heads = p->end_head + 1; | |
1405 | sectors = p->end_sector & 63; | |
1406 | if (sectors == 0) | |
1407 | continue; | |
1408 | cylinders = nb_sectors / (heads * sectors); | |
1409 | if (cylinders < 1 || cylinders > 16383) | |
1410 | continue; | |
1411 | *pheads = heads; | |
1412 | *psectors = sectors; | |
1413 | *pcylinders = cylinders; | |
1414 | #if 0 | |
1415 | printf("guessed geometry: LCHS=%d %d %d\n", | |
1416 | cylinders, heads, sectors); | |
1417 | #endif | |
1418 | return 0; | |
1419 | } | |
1420 | } | |
1421 | return -1; | |
1422 | } | |
1423 | ||
1424 | void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) | |
1425 | { | |
1426 | int translation, lba_detected = 0; | |
1427 | int cylinders, heads, secs; | |
a38131b6 | 1428 | uint64_t nb_sectors; |
f3d54fc4 AL |
1429 | |
1430 | /* if a geometry hint is available, use it */ | |
1431 | bdrv_get_geometry(bs, &nb_sectors); | |
1432 | bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); | |
1433 | translation = bdrv_get_translation_hint(bs); | |
1434 | if (cylinders != 0) { | |
1435 | *pcyls = cylinders; | |
1436 | *pheads = heads; | |
1437 | *psecs = secs; | |
1438 | } else { | |
1439 | if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { | |
1440 | if (heads > 16) { | |
1441 | /* if heads > 16, it means that a BIOS LBA | |
1442 | translation was active, so the default | |
1443 | hardware geometry is OK */ | |
1444 | lba_detected = 1; | |
1445 | goto default_geometry; | |
1446 | } else { | |
1447 | *pcyls = cylinders; | |
1448 | *pheads = heads; | |
1449 | *psecs = secs; | |
1450 | /* disable any translation to be in sync with | |
1451 | the logical geometry */ | |
1452 | if (translation == BIOS_ATA_TRANSLATION_AUTO) { | |
1453 | bdrv_set_translation_hint(bs, | |
1454 | BIOS_ATA_TRANSLATION_NONE); | |
1455 | } | |
1456 | } | |
1457 | } else { | |
1458 | default_geometry: | |
1459 | /* if no geometry, use a standard physical disk geometry */ | |
1460 | cylinders = nb_sectors / (16 * 63); | |
1461 | ||
1462 | if (cylinders > 16383) | |
1463 | cylinders = 16383; | |
1464 | else if (cylinders < 2) | |
1465 | cylinders = 2; | |
1466 | *pcyls = cylinders; | |
1467 | *pheads = 16; | |
1468 | *psecs = 63; | |
1469 | if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { | |
1470 | if ((*pcyls * *pheads) <= 131072) { | |
1471 | bdrv_set_translation_hint(bs, | |
1472 | BIOS_ATA_TRANSLATION_LARGE); | |
1473 | } else { | |
1474 | bdrv_set_translation_hint(bs, | |
1475 | BIOS_ATA_TRANSLATION_LBA); | |
1476 | } | |
1477 | } | |
1478 | } | |
1479 | bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); | |
1480 | } | |
1481 | } | |
1482 | ||
5fafdf24 | 1483 | void bdrv_set_geometry_hint(BlockDriverState *bs, |
b338082b FB |
1484 | int cyls, int heads, int secs) |
1485 | { | |
1486 | bs->cyls = cyls; | |
1487 | bs->heads = heads; | |
1488 | bs->secs = secs; | |
1489 | } | |
1490 | ||
46d4767d FB |
1491 | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
1492 | { | |
1493 | bs->translation = translation; | |
1494 | } | |
1495 | ||
5fafdf24 | 1496 | void bdrv_get_geometry_hint(BlockDriverState *bs, |
b338082b FB |
1497 | int *pcyls, int *pheads, int *psecs) |
1498 | { | |
1499 | *pcyls = bs->cyls; | |
1500 | *pheads = bs->heads; | |
1501 | *psecs = bs->secs; | |
1502 | } | |
1503 | ||
5bbdbb46 BS |
1504 | /* Recognize floppy formats */ |
1505 | typedef struct FDFormat { | |
1506 | FDriveType drive; | |
1507 | uint8_t last_sect; | |
1508 | uint8_t max_track; | |
1509 | uint8_t max_head; | |
1510 | } FDFormat; | |
1511 | ||
1512 | static const FDFormat fd_formats[] = { | |
1513 | /* First entry is default format */ | |
1514 | /* 1.44 MB 3"1/2 floppy disks */ | |
1515 | { FDRIVE_DRV_144, 18, 80, 1, }, | |
1516 | { FDRIVE_DRV_144, 20, 80, 1, }, | |
1517 | { FDRIVE_DRV_144, 21, 80, 1, }, | |
1518 | { FDRIVE_DRV_144, 21, 82, 1, }, | |
1519 | { FDRIVE_DRV_144, 21, 83, 1, }, | |
1520 | { FDRIVE_DRV_144, 22, 80, 1, }, | |
1521 | { FDRIVE_DRV_144, 23, 80, 1, }, | |
1522 | { FDRIVE_DRV_144, 24, 80, 1, }, | |
1523 | /* 2.88 MB 3"1/2 floppy disks */ | |
1524 | { FDRIVE_DRV_288, 36, 80, 1, }, | |
1525 | { FDRIVE_DRV_288, 39, 80, 1, }, | |
1526 | { FDRIVE_DRV_288, 40, 80, 1, }, | |
1527 | { FDRIVE_DRV_288, 44, 80, 1, }, | |
1528 | { FDRIVE_DRV_288, 48, 80, 1, }, | |
1529 | /* 720 kB 3"1/2 floppy disks */ | |
1530 | { FDRIVE_DRV_144, 9, 80, 1, }, | |
1531 | { FDRIVE_DRV_144, 10, 80, 1, }, | |
1532 | { FDRIVE_DRV_144, 10, 82, 1, }, | |
1533 | { FDRIVE_DRV_144, 10, 83, 1, }, | |
1534 | { FDRIVE_DRV_144, 13, 80, 1, }, | |
1535 | { FDRIVE_DRV_144, 14, 80, 1, }, | |
1536 | /* 1.2 MB 5"1/4 floppy disks */ | |
1537 | { FDRIVE_DRV_120, 15, 80, 1, }, | |
1538 | { FDRIVE_DRV_120, 18, 80, 1, }, | |
1539 | { FDRIVE_DRV_120, 18, 82, 1, }, | |
1540 | { FDRIVE_DRV_120, 18, 83, 1, }, | |
1541 | { FDRIVE_DRV_120, 20, 80, 1, }, | |
1542 | /* 720 kB 5"1/4 floppy disks */ | |
1543 | { FDRIVE_DRV_120, 9, 80, 1, }, | |
1544 | { FDRIVE_DRV_120, 11, 80, 1, }, | |
1545 | /* 360 kB 5"1/4 floppy disks */ | |
1546 | { FDRIVE_DRV_120, 9, 40, 1, }, | |
1547 | { FDRIVE_DRV_120, 9, 40, 0, }, | |
1548 | { FDRIVE_DRV_120, 10, 41, 1, }, | |
1549 | { FDRIVE_DRV_120, 10, 42, 1, }, | |
1550 | /* 320 kB 5"1/4 floppy disks */ | |
1551 | { FDRIVE_DRV_120, 8, 40, 1, }, | |
1552 | { FDRIVE_DRV_120, 8, 40, 0, }, | |
1553 | /* 360 kB must match 5"1/4 better than 3"1/2... */ | |
1554 | { FDRIVE_DRV_144, 9, 80, 0, }, | |
1555 | /* end */ | |
1556 | { FDRIVE_DRV_NONE, -1, -1, 0, }, | |
1557 | }; | |
1558 | ||
1559 | void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads, | |
1560 | int *max_track, int *last_sect, | |
1561 | FDriveType drive_in, FDriveType *drive) | |
1562 | { | |
1563 | const FDFormat *parse; | |
1564 | uint64_t nb_sectors, size; | |
1565 | int i, first_match, match; | |
1566 | ||
1567 | bdrv_get_geometry_hint(bs, nb_heads, max_track, last_sect); | |
1568 | if (*nb_heads != 0 && *max_track != 0 && *last_sect != 0) { | |
1569 | /* User defined disk */ | |
1570 | } else { | |
1571 | bdrv_get_geometry(bs, &nb_sectors); | |
1572 | match = -1; | |
1573 | first_match = -1; | |
1574 | for (i = 0; ; i++) { | |
1575 | parse = &fd_formats[i]; | |
1576 | if (parse->drive == FDRIVE_DRV_NONE) { | |
1577 | break; | |
1578 | } | |
1579 | if (drive_in == parse->drive || | |
1580 | drive_in == FDRIVE_DRV_NONE) { | |
1581 | size = (parse->max_head + 1) * parse->max_track * | |
1582 | parse->last_sect; | |
1583 | if (nb_sectors == size) { | |
1584 | match = i; | |
1585 | break; | |
1586 | } | |
1587 | if (first_match == -1) { | |
1588 | first_match = i; | |
1589 | } | |
1590 | } | |
1591 | } | |
1592 | if (match == -1) { | |
1593 | if (first_match == -1) { | |
1594 | match = 1; | |
1595 | } else { | |
1596 | match = first_match; | |
1597 | } | |
1598 | parse = &fd_formats[match]; | |
1599 | } | |
1600 | *nb_heads = parse->max_head + 1; | |
1601 | *max_track = parse->max_track; | |
1602 | *last_sect = parse->last_sect; | |
1603 | *drive = parse->drive; | |
1604 | } | |
1605 | } | |
1606 | ||
46d4767d FB |
1607 | int bdrv_get_translation_hint(BlockDriverState *bs) |
1608 | { | |
1609 | return bs->translation; | |
1610 | } | |
1611 | ||
abd7f68d MA |
1612 | void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error, |
1613 | BlockErrorAction on_write_error) | |
1614 | { | |
1615 | bs->on_read_error = on_read_error; | |
1616 | bs->on_write_error = on_write_error; | |
1617 | } | |
1618 | ||
1619 | BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read) | |
1620 | { | |
1621 | return is_read ? bs->on_read_error : bs->on_write_error; | |
1622 | } | |
1623 | ||
b338082b FB |
1624 | int bdrv_is_read_only(BlockDriverState *bs) |
1625 | { | |
1626 | return bs->read_only; | |
1627 | } | |
1628 | ||
985a03b0 TS |
1629 | int bdrv_is_sg(BlockDriverState *bs) |
1630 | { | |
1631 | return bs->sg; | |
1632 | } | |
1633 | ||
e900a7b7 CH |
1634 | int bdrv_enable_write_cache(BlockDriverState *bs) |
1635 | { | |
1636 | return bs->enable_write_cache; | |
1637 | } | |
1638 | ||
ea2384d3 FB |
1639 | int bdrv_is_encrypted(BlockDriverState *bs) |
1640 | { | |
1641 | if (bs->backing_hd && bs->backing_hd->encrypted) | |
1642 | return 1; | |
1643 | return bs->encrypted; | |
1644 | } | |
1645 | ||
c0f4ce77 AL |
1646 | int bdrv_key_required(BlockDriverState *bs) |
1647 | { | |
1648 | BlockDriverState *backing_hd = bs->backing_hd; | |
1649 | ||
1650 | if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) | |
1651 | return 1; | |
1652 | return (bs->encrypted && !bs->valid_key); | |
1653 | } | |
1654 | ||
ea2384d3 FB |
1655 | int bdrv_set_key(BlockDriverState *bs, const char *key) |
1656 | { | |
1657 | int ret; | |
1658 | if (bs->backing_hd && bs->backing_hd->encrypted) { | |
1659 | ret = bdrv_set_key(bs->backing_hd, key); | |
1660 | if (ret < 0) | |
1661 | return ret; | |
1662 | if (!bs->encrypted) | |
1663 | return 0; | |
1664 | } | |
fd04a2ae SH |
1665 | if (!bs->encrypted) { |
1666 | return -EINVAL; | |
1667 | } else if (!bs->drv || !bs->drv->bdrv_set_key) { | |
1668 | return -ENOMEDIUM; | |
1669 | } | |
c0f4ce77 | 1670 | ret = bs->drv->bdrv_set_key(bs, key); |
bb5fc20f AL |
1671 | if (ret < 0) { |
1672 | bs->valid_key = 0; | |
1673 | } else if (!bs->valid_key) { | |
1674 | bs->valid_key = 1; | |
1675 | /* call the change callback now, we skipped it on open */ | |
145feb17 | 1676 | bdrv_dev_change_media_cb(bs); |
bb5fc20f | 1677 | } |
c0f4ce77 | 1678 | return ret; |
ea2384d3 FB |
1679 | } |
1680 | ||
1681 | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) | |
1682 | { | |
19cb3738 | 1683 | if (!bs->drv) { |
ea2384d3 FB |
1684 | buf[0] = '\0'; |
1685 | } else { | |
1686 | pstrcpy(buf, buf_size, bs->drv->format_name); | |
1687 | } | |
1688 | } | |
1689 | ||
5fafdf24 | 1690 | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
ea2384d3 FB |
1691 | void *opaque) |
1692 | { | |
1693 | BlockDriver *drv; | |
1694 | ||
8a22f02a | 1695 | QLIST_FOREACH(drv, &bdrv_drivers, list) { |
ea2384d3 FB |
1696 | it(opaque, drv->format_name); |
1697 | } | |
1698 | } | |
1699 | ||
b338082b FB |
1700 | BlockDriverState *bdrv_find(const char *name) |
1701 | { | |
1702 | BlockDriverState *bs; | |
1703 | ||
1b7bdbc1 SH |
1704 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
1705 | if (!strcmp(name, bs->device_name)) { | |
b338082b | 1706 | return bs; |
1b7bdbc1 | 1707 | } |
b338082b FB |
1708 | } |
1709 | return NULL; | |
1710 | } | |
1711 | ||
2f399b0a MA |
1712 | BlockDriverState *bdrv_next(BlockDriverState *bs) |
1713 | { | |
1714 | if (!bs) { | |
1715 | return QTAILQ_FIRST(&bdrv_states); | |
1716 | } | |
1717 | return QTAILQ_NEXT(bs, list); | |
1718 | } | |
1719 | ||
51de9760 | 1720 | void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) |
81d0912d FB |
1721 | { |
1722 | BlockDriverState *bs; | |
1723 | ||
1b7bdbc1 | 1724 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
51de9760 | 1725 | it(opaque, bs); |
81d0912d FB |
1726 | } |
1727 | } | |
1728 | ||
ea2384d3 FB |
1729 | const char *bdrv_get_device_name(BlockDriverState *bs) |
1730 | { | |
1731 | return bs->device_name; | |
1732 | } | |
1733 | ||
205ef796 | 1734 | int bdrv_flush(BlockDriverState *bs) |
7a6cba61 | 1735 | { |
016f5cf6 | 1736 | if (bs->open_flags & BDRV_O_NO_FLUSH) { |
205ef796 KW |
1737 | return 0; |
1738 | } | |
1739 | ||
e7a8a783 KW |
1740 | if (bs->drv && bdrv_has_async_flush(bs->drv) && qemu_in_coroutine()) { |
1741 | return bdrv_co_flush_em(bs); | |
1742 | } | |
1743 | ||
205ef796 KW |
1744 | if (bs->drv && bs->drv->bdrv_flush) { |
1745 | return bs->drv->bdrv_flush(bs); | |
016f5cf6 AG |
1746 | } |
1747 | ||
205ef796 KW |
1748 | /* |
1749 | * Some block drivers always operate in either writethrough or unsafe mode | |
1750 | * and don't support bdrv_flush therefore. Usually qemu doesn't know how | |
1751 | * the server works (because the behaviour is hardcoded or depends on | |
1752 | * server-side configuration), so we can't ensure that everything is safe | |
1753 | * on disk. Returning an error doesn't work because that would break guests | |
1754 | * even if the server operates in writethrough mode. | |
1755 | * | |
1756 | * Let's hope the user knows what he's doing. | |
1757 | */ | |
1758 | return 0; | |
7a6cba61 PB |
1759 | } |
1760 | ||
c6ca28d6 AL |
1761 | void bdrv_flush_all(void) |
1762 | { | |
1763 | BlockDriverState *bs; | |
1764 | ||
1b7bdbc1 | 1765 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
c602a489 | 1766 | if (!bdrv_is_read_only(bs) && bdrv_is_inserted(bs)) { |
c6ca28d6 | 1767 | bdrv_flush(bs); |
1b7bdbc1 SH |
1768 | } |
1769 | } | |
c6ca28d6 AL |
1770 | } |
1771 | ||
f2feebbd KW |
1772 | int bdrv_has_zero_init(BlockDriverState *bs) |
1773 | { | |
1774 | assert(bs->drv); | |
1775 | ||
336c1c12 KW |
1776 | if (bs->drv->bdrv_has_zero_init) { |
1777 | return bs->drv->bdrv_has_zero_init(bs); | |
f2feebbd KW |
1778 | } |
1779 | ||
1780 | return 1; | |
1781 | } | |
1782 | ||
bb8bf76f CH |
1783 | int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) |
1784 | { | |
1785 | if (!bs->drv) { | |
1786 | return -ENOMEDIUM; | |
1787 | } | |
1788 | if (!bs->drv->bdrv_discard) { | |
1789 | return 0; | |
1790 | } | |
1791 | return bs->drv->bdrv_discard(bs, sector_num, nb_sectors); | |
1792 | } | |
1793 | ||
f58c7b35 TS |
1794 | /* |
1795 | * Returns true iff the specified sector is present in the disk image. Drivers | |
1796 | * not implementing the functionality are assumed to not support backing files, | |
1797 | * hence all their sectors are reported as allocated. | |
1798 | * | |
1799 | * 'pnum' is set to the number of sectors (including and immediately following | |
1800 | * the specified sector) that are known to be in the same | |
1801 | * allocated/unallocated state. | |
1802 | * | |
1803 | * 'nb_sectors' is the max value 'pnum' should be set to. | |
1804 | */ | |
1805 | int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, | |
1806 | int *pnum) | |
1807 | { | |
1808 | int64_t n; | |
1809 | if (!bs->drv->bdrv_is_allocated) { | |
1810 | if (sector_num >= bs->total_sectors) { | |
1811 | *pnum = 0; | |
1812 | return 0; | |
1813 | } | |
1814 | n = bs->total_sectors - sector_num; | |
1815 | *pnum = (n < nb_sectors) ? (n) : (nb_sectors); | |
1816 | return 1; | |
1817 | } | |
1818 | return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); | |
1819 | } | |
1820 | ||
2582bfed LC |
1821 | void bdrv_mon_event(const BlockDriverState *bdrv, |
1822 | BlockMonEventAction action, int is_read) | |
1823 | { | |
1824 | QObject *data; | |
1825 | const char *action_str; | |
1826 | ||
1827 | switch (action) { | |
1828 | case BDRV_ACTION_REPORT: | |
1829 | action_str = "report"; | |
1830 | break; | |
1831 | case BDRV_ACTION_IGNORE: | |
1832 | action_str = "ignore"; | |
1833 | break; | |
1834 | case BDRV_ACTION_STOP: | |
1835 | action_str = "stop"; | |
1836 | break; | |
1837 | default: | |
1838 | abort(); | |
1839 | } | |
1840 | ||
1841 | data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", | |
1842 | bdrv->device_name, | |
1843 | action_str, | |
1844 | is_read ? "read" : "write"); | |
1845 | monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data); | |
1846 | ||
1847 | qobject_decref(data); | |
1848 | } | |
1849 | ||
d15e5465 | 1850 | static void bdrv_print_dict(QObject *obj, void *opaque) |
b338082b | 1851 | { |
d15e5465 LC |
1852 | QDict *bs_dict; |
1853 | Monitor *mon = opaque; | |
1854 | ||
1855 | bs_dict = qobject_to_qdict(obj); | |
1856 | ||
d8aeeb31 | 1857 | monitor_printf(mon, "%s: removable=%d", |
d15e5465 | 1858 | qdict_get_str(bs_dict, "device"), |
d15e5465 LC |
1859 | qdict_get_bool(bs_dict, "removable")); |
1860 | ||
1861 | if (qdict_get_bool(bs_dict, "removable")) { | |
1862 | monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked")); | |
e4def80b MA |
1863 | monitor_printf(mon, " tray-open=%d", |
1864 | qdict_get_bool(bs_dict, "tray-open")); | |
d15e5465 | 1865 | } |
d15e5465 LC |
1866 | if (qdict_haskey(bs_dict, "inserted")) { |
1867 | QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted")); | |
1868 | ||
1869 | monitor_printf(mon, " file="); | |
1870 | monitor_print_filename(mon, qdict_get_str(qdict, "file")); | |
1871 | if (qdict_haskey(qdict, "backing_file")) { | |
1872 | monitor_printf(mon, " backing_file="); | |
1873 | monitor_print_filename(mon, qdict_get_str(qdict, "backing_file")); | |
1874 | } | |
1875 | monitor_printf(mon, " ro=%d drv=%s encrypted=%d", | |
1876 | qdict_get_bool(qdict, "ro"), | |
1877 | qdict_get_str(qdict, "drv"), | |
1878 | qdict_get_bool(qdict, "encrypted")); | |
1879 | } else { | |
1880 | monitor_printf(mon, " [not inserted]"); | |
1881 | } | |
1882 | ||
1883 | monitor_printf(mon, "\n"); | |
1884 | } | |
1885 | ||
1886 | void bdrv_info_print(Monitor *mon, const QObject *data) | |
1887 | { | |
1888 | qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon); | |
1889 | } | |
1890 | ||
d15e5465 LC |
1891 | void bdrv_info(Monitor *mon, QObject **ret_data) |
1892 | { | |
1893 | QList *bs_list; | |
b338082b FB |
1894 | BlockDriverState *bs; |
1895 | ||
d15e5465 LC |
1896 | bs_list = qlist_new(); |
1897 | ||
1b7bdbc1 | 1898 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
d15e5465 | 1899 | QObject *bs_obj; |
e4def80b | 1900 | QDict *bs_dict; |
d15e5465 | 1901 | |
d8aeeb31 | 1902 | bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', " |
d15e5465 | 1903 | "'removable': %i, 'locked': %i }", |
2c6942fa MA |
1904 | bs->device_name, |
1905 | bdrv_dev_has_removable_media(bs), | |
f107639a | 1906 | bdrv_dev_is_medium_locked(bs)); |
e4def80b | 1907 | bs_dict = qobject_to_qdict(bs_obj); |
d15e5465 | 1908 | |
e4def80b MA |
1909 | if (bdrv_dev_has_removable_media(bs)) { |
1910 | qdict_put(bs_dict, "tray-open", | |
1911 | qbool_from_int(bdrv_dev_is_tray_open(bs))); | |
1912 | } | |
19cb3738 | 1913 | if (bs->drv) { |
d15e5465 | 1914 | QObject *obj; |
d15e5465 LC |
1915 | |
1916 | obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, " | |
1917 | "'encrypted': %i }", | |
1918 | bs->filename, bs->read_only, | |
1919 | bs->drv->format_name, | |
1920 | bdrv_is_encrypted(bs)); | |
fef30743 | 1921 | if (bs->backing_file[0] != '\0') { |
d15e5465 LC |
1922 | QDict *qdict = qobject_to_qdict(obj); |
1923 | qdict_put(qdict, "backing_file", | |
1924 | qstring_from_str(bs->backing_file)); | |
376253ec | 1925 | } |
d15e5465 LC |
1926 | |
1927 | qdict_put_obj(bs_dict, "inserted", obj); | |
b338082b | 1928 | } |
d15e5465 | 1929 | qlist_append_obj(bs_list, bs_obj); |
b338082b | 1930 | } |
d15e5465 LC |
1931 | |
1932 | *ret_data = QOBJECT(bs_list); | |
b338082b | 1933 | } |
a36e69dd | 1934 | |
218a536a | 1935 | static void bdrv_stats_iter(QObject *data, void *opaque) |
a36e69dd | 1936 | { |
218a536a LC |
1937 | QDict *qdict; |
1938 | Monitor *mon = opaque; | |
1939 | ||
1940 | qdict = qobject_to_qdict(data); | |
1941 | monitor_printf(mon, "%s:", qdict_get_str(qdict, "device")); | |
1942 | ||
1943 | qdict = qobject_to_qdict(qdict_get(qdict, "stats")); | |
1944 | monitor_printf(mon, " rd_bytes=%" PRId64 | |
1945 | " wr_bytes=%" PRId64 | |
1946 | " rd_operations=%" PRId64 | |
1947 | " wr_operations=%" PRId64 | |
e8045d67 | 1948 | " flush_operations=%" PRId64 |
c488c7f6 CH |
1949 | " wr_total_time_ns=%" PRId64 |
1950 | " rd_total_time_ns=%" PRId64 | |
1951 | " flush_total_time_ns=%" PRId64 | |
218a536a LC |
1952 | "\n", |
1953 | qdict_get_int(qdict, "rd_bytes"), | |
1954 | qdict_get_int(qdict, "wr_bytes"), | |
1955 | qdict_get_int(qdict, "rd_operations"), | |
e8045d67 | 1956 | qdict_get_int(qdict, "wr_operations"), |
c488c7f6 CH |
1957 | qdict_get_int(qdict, "flush_operations"), |
1958 | qdict_get_int(qdict, "wr_total_time_ns"), | |
1959 | qdict_get_int(qdict, "rd_total_time_ns"), | |
1960 | qdict_get_int(qdict, "flush_total_time_ns")); | |
218a536a LC |
1961 | } |
1962 | ||
1963 | void bdrv_stats_print(Monitor *mon, const QObject *data) | |
1964 | { | |
1965 | qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon); | |
1966 | } | |
1967 | ||
294cc35f KW |
1968 | static QObject* bdrv_info_stats_bs(BlockDriverState *bs) |
1969 | { | |
1970 | QObject *res; | |
1971 | QDict *dict; | |
1972 | ||
1973 | res = qobject_from_jsonf("{ 'stats': {" | |
1974 | "'rd_bytes': %" PRId64 "," | |
1975 | "'wr_bytes': %" PRId64 "," | |
1976 | "'rd_operations': %" PRId64 "," | |
1977 | "'wr_operations': %" PRId64 "," | |
e8045d67 | 1978 | "'wr_highest_offset': %" PRId64 "," |
c488c7f6 CH |
1979 | "'flush_operations': %" PRId64 "," |
1980 | "'wr_total_time_ns': %" PRId64 "," | |
1981 | "'rd_total_time_ns': %" PRId64 "," | |
1982 | "'flush_total_time_ns': %" PRId64 | |
294cc35f | 1983 | "} }", |
a597e79c CH |
1984 | bs->nr_bytes[BDRV_ACCT_READ], |
1985 | bs->nr_bytes[BDRV_ACCT_WRITE], | |
1986 | bs->nr_ops[BDRV_ACCT_READ], | |
1987 | bs->nr_ops[BDRV_ACCT_WRITE], | |
5ffbbc67 | 1988 | bs->wr_highest_sector * |
e8045d67 | 1989 | (uint64_t)BDRV_SECTOR_SIZE, |
c488c7f6 CH |
1990 | bs->nr_ops[BDRV_ACCT_FLUSH], |
1991 | bs->total_time_ns[BDRV_ACCT_WRITE], | |
1992 | bs->total_time_ns[BDRV_ACCT_READ], | |
1993 | bs->total_time_ns[BDRV_ACCT_FLUSH]); | |
294cc35f KW |
1994 | dict = qobject_to_qdict(res); |
1995 | ||
1996 | if (*bs->device_name) { | |
1997 | qdict_put(dict, "device", qstring_from_str(bs->device_name)); | |
1998 | } | |
1999 | ||
2000 | if (bs->file) { | |
2001 | QObject *parent = bdrv_info_stats_bs(bs->file); | |
2002 | qdict_put_obj(dict, "parent", parent); | |
2003 | } | |
2004 | ||
2005 | return res; | |
2006 | } | |
2007 | ||
218a536a LC |
2008 | void bdrv_info_stats(Monitor *mon, QObject **ret_data) |
2009 | { | |
2010 | QObject *obj; | |
2011 | QList *devices; | |
a36e69dd TS |
2012 | BlockDriverState *bs; |
2013 | ||
218a536a LC |
2014 | devices = qlist_new(); |
2015 | ||
1b7bdbc1 | 2016 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
294cc35f | 2017 | obj = bdrv_info_stats_bs(bs); |
218a536a | 2018 | qlist_append_obj(devices, obj); |
a36e69dd | 2019 | } |
218a536a LC |
2020 | |
2021 | *ret_data = QOBJECT(devices); | |
a36e69dd | 2022 | } |
ea2384d3 | 2023 | |
045df330 AL |
2024 | const char *bdrv_get_encrypted_filename(BlockDriverState *bs) |
2025 | { | |
2026 | if (bs->backing_hd && bs->backing_hd->encrypted) | |
2027 | return bs->backing_file; | |
2028 | else if (bs->encrypted) | |
2029 | return bs->filename; | |
2030 | else | |
2031 | return NULL; | |
2032 | } | |
2033 | ||
5fafdf24 | 2034 | void bdrv_get_backing_filename(BlockDriverState *bs, |
83f64091 FB |
2035 | char *filename, int filename_size) |
2036 | { | |
b783e409 | 2037 | if (!bs->backing_file) { |
83f64091 FB |
2038 | pstrcpy(filename, filename_size, ""); |
2039 | } else { | |
2040 | pstrcpy(filename, filename_size, bs->backing_file); | |
2041 | } | |
2042 | } | |
2043 | ||
5fafdf24 | 2044 | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, |
faea38e7 FB |
2045 | const uint8_t *buf, int nb_sectors) |
2046 | { | |
2047 | BlockDriver *drv = bs->drv; | |
2048 | if (!drv) | |
19cb3738 | 2049 | return -ENOMEDIUM; |
faea38e7 FB |
2050 | if (!drv->bdrv_write_compressed) |
2051 | return -ENOTSUP; | |
fbb7b4e0 KW |
2052 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
2053 | return -EIO; | |
a55eb92c | 2054 | |
c6d22830 | 2055 | if (bs->dirty_bitmap) { |
7cd1e32a | 2056 | set_dirty_bitmap(bs, sector_num, nb_sectors, 1); |
2057 | } | |
a55eb92c | 2058 | |
faea38e7 FB |
2059 | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); |
2060 | } | |
3b46e624 | 2061 | |
faea38e7 FB |
2062 | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
2063 | { | |
2064 | BlockDriver *drv = bs->drv; | |
2065 | if (!drv) | |
19cb3738 | 2066 | return -ENOMEDIUM; |
faea38e7 FB |
2067 | if (!drv->bdrv_get_info) |
2068 | return -ENOTSUP; | |
2069 | memset(bdi, 0, sizeof(*bdi)); | |
2070 | return drv->bdrv_get_info(bs, bdi); | |
2071 | } | |
2072 | ||
45566e9c CH |
2073 | int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, |
2074 | int64_t pos, int size) | |
178e08a5 AL |
2075 | { |
2076 | BlockDriver *drv = bs->drv; | |
2077 | if (!drv) | |
2078 | return -ENOMEDIUM; | |
7cdb1f6d MK |
2079 | if (drv->bdrv_save_vmstate) |
2080 | return drv->bdrv_save_vmstate(bs, buf, pos, size); | |
2081 | if (bs->file) | |
2082 | return bdrv_save_vmstate(bs->file, buf, pos, size); | |
2083 | return -ENOTSUP; | |
178e08a5 AL |
2084 | } |
2085 | ||
45566e9c CH |
2086 | int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, |
2087 | int64_t pos, int size) | |
178e08a5 AL |
2088 | { |
2089 | BlockDriver *drv = bs->drv; | |
2090 | if (!drv) | |
2091 | return -ENOMEDIUM; | |
7cdb1f6d MK |
2092 | if (drv->bdrv_load_vmstate) |
2093 | return drv->bdrv_load_vmstate(bs, buf, pos, size); | |
2094 | if (bs->file) | |
2095 | return bdrv_load_vmstate(bs->file, buf, pos, size); | |
2096 | return -ENOTSUP; | |
178e08a5 AL |
2097 | } |
2098 | ||
8b9b0cc2 KW |
2099 | void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) |
2100 | { | |
2101 | BlockDriver *drv = bs->drv; | |
2102 | ||
2103 | if (!drv || !drv->bdrv_debug_event) { | |
2104 | return; | |
2105 | } | |
2106 | ||
2107 | return drv->bdrv_debug_event(bs, event); | |
2108 | ||
2109 | } | |
2110 | ||
faea38e7 FB |
2111 | /**************************************************************/ |
2112 | /* handling of snapshots */ | |
2113 | ||
feeee5ac MDCF |
2114 | int bdrv_can_snapshot(BlockDriverState *bs) |
2115 | { | |
2116 | BlockDriver *drv = bs->drv; | |
07b70bfb | 2117 | if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { |
feeee5ac MDCF |
2118 | return 0; |
2119 | } | |
2120 | ||
2121 | if (!drv->bdrv_snapshot_create) { | |
2122 | if (bs->file != NULL) { | |
2123 | return bdrv_can_snapshot(bs->file); | |
2124 | } | |
2125 | return 0; | |
2126 | } | |
2127 | ||
2128 | return 1; | |
2129 | } | |
2130 | ||
199630b6 BS |
2131 | int bdrv_is_snapshot(BlockDriverState *bs) |
2132 | { | |
2133 | return !!(bs->open_flags & BDRV_O_SNAPSHOT); | |
2134 | } | |
2135 | ||
f9092b10 MA |
2136 | BlockDriverState *bdrv_snapshots(void) |
2137 | { | |
2138 | BlockDriverState *bs; | |
2139 | ||
3ac906f7 | 2140 | if (bs_snapshots) { |
f9092b10 | 2141 | return bs_snapshots; |
3ac906f7 | 2142 | } |
f9092b10 MA |
2143 | |
2144 | bs = NULL; | |
2145 | while ((bs = bdrv_next(bs))) { | |
2146 | if (bdrv_can_snapshot(bs)) { | |
3ac906f7 MA |
2147 | bs_snapshots = bs; |
2148 | return bs; | |
f9092b10 MA |
2149 | } |
2150 | } | |
2151 | return NULL; | |
f9092b10 MA |
2152 | } |
2153 | ||
5fafdf24 | 2154 | int bdrv_snapshot_create(BlockDriverState *bs, |
faea38e7 FB |
2155 | QEMUSnapshotInfo *sn_info) |
2156 | { | |
2157 | BlockDriver *drv = bs->drv; | |
2158 | if (!drv) | |
19cb3738 | 2159 | return -ENOMEDIUM; |
7cdb1f6d MK |
2160 | if (drv->bdrv_snapshot_create) |
2161 | return drv->bdrv_snapshot_create(bs, sn_info); | |
2162 | if (bs->file) | |
2163 | return bdrv_snapshot_create(bs->file, sn_info); | |
2164 | return -ENOTSUP; | |
faea38e7 FB |
2165 | } |
2166 | ||
5fafdf24 | 2167 | int bdrv_snapshot_goto(BlockDriverState *bs, |
faea38e7 FB |
2168 | const char *snapshot_id) |
2169 | { | |
2170 | BlockDriver *drv = bs->drv; | |
7cdb1f6d MK |
2171 | int ret, open_ret; |
2172 | ||
faea38e7 | 2173 | if (!drv) |
19cb3738 | 2174 | return -ENOMEDIUM; |
7cdb1f6d MK |
2175 | if (drv->bdrv_snapshot_goto) |
2176 | return drv->bdrv_snapshot_goto(bs, snapshot_id); | |
2177 | ||
2178 | if (bs->file) { | |
2179 | drv->bdrv_close(bs); | |
2180 | ret = bdrv_snapshot_goto(bs->file, snapshot_id); | |
2181 | open_ret = drv->bdrv_open(bs, bs->open_flags); | |
2182 | if (open_ret < 0) { | |
2183 | bdrv_delete(bs->file); | |
2184 | bs->drv = NULL; | |
2185 | return open_ret; | |
2186 | } | |
2187 | return ret; | |
2188 | } | |
2189 | ||
2190 | return -ENOTSUP; | |
faea38e7 FB |
2191 | } |
2192 | ||
2193 | int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) | |
2194 | { | |
2195 | BlockDriver *drv = bs->drv; | |
2196 | if (!drv) | |
19cb3738 | 2197 | return -ENOMEDIUM; |
7cdb1f6d MK |
2198 | if (drv->bdrv_snapshot_delete) |
2199 | return drv->bdrv_snapshot_delete(bs, snapshot_id); | |
2200 | if (bs->file) | |
2201 | return bdrv_snapshot_delete(bs->file, snapshot_id); | |
2202 | return -ENOTSUP; | |
faea38e7 FB |
2203 | } |
2204 | ||
5fafdf24 | 2205 | int bdrv_snapshot_list(BlockDriverState *bs, |
faea38e7 FB |
2206 | QEMUSnapshotInfo **psn_info) |
2207 | { | |
2208 | BlockDriver *drv = bs->drv; | |
2209 | if (!drv) | |
19cb3738 | 2210 | return -ENOMEDIUM; |
7cdb1f6d MK |
2211 | if (drv->bdrv_snapshot_list) |
2212 | return drv->bdrv_snapshot_list(bs, psn_info); | |
2213 | if (bs->file) | |
2214 | return bdrv_snapshot_list(bs->file, psn_info); | |
2215 | return -ENOTSUP; | |
faea38e7 FB |
2216 | } |
2217 | ||
51ef6727 | 2218 | int bdrv_snapshot_load_tmp(BlockDriverState *bs, |
2219 | const char *snapshot_name) | |
2220 | { | |
2221 | BlockDriver *drv = bs->drv; | |
2222 | if (!drv) { | |
2223 | return -ENOMEDIUM; | |
2224 | } | |
2225 | if (!bs->read_only) { | |
2226 | return -EINVAL; | |
2227 | } | |
2228 | if (drv->bdrv_snapshot_load_tmp) { | |
2229 | return drv->bdrv_snapshot_load_tmp(bs, snapshot_name); | |
2230 | } | |
2231 | return -ENOTSUP; | |
2232 | } | |
2233 | ||
faea38e7 FB |
2234 | #define NB_SUFFIXES 4 |
2235 | ||
2236 | char *get_human_readable_size(char *buf, int buf_size, int64_t size) | |
2237 | { | |
2238 | static const char suffixes[NB_SUFFIXES] = "KMGT"; | |
2239 | int64_t base; | |
2240 | int i; | |
2241 | ||
2242 | if (size <= 999) { | |
2243 | snprintf(buf, buf_size, "%" PRId64, size); | |
2244 | } else { | |
2245 | base = 1024; | |
2246 | for(i = 0; i < NB_SUFFIXES; i++) { | |
2247 | if (size < (10 * base)) { | |
5fafdf24 | 2248 | snprintf(buf, buf_size, "%0.1f%c", |
faea38e7 FB |
2249 | (double)size / base, |
2250 | suffixes[i]); | |
2251 | break; | |
2252 | } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { | |
5fafdf24 | 2253 | snprintf(buf, buf_size, "%" PRId64 "%c", |
faea38e7 FB |
2254 | ((size + (base >> 1)) / base), |
2255 | suffixes[i]); | |
2256 | break; | |
2257 | } | |
2258 | base = base * 1024; | |
2259 | } | |
2260 | } | |
2261 | return buf; | |
2262 | } | |
2263 | ||
2264 | char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) | |
2265 | { | |
2266 | char buf1[128], date_buf[128], clock_buf[128]; | |
3b9f94e1 FB |
2267 | #ifdef _WIN32 |
2268 | struct tm *ptm; | |
2269 | #else | |
faea38e7 | 2270 | struct tm tm; |
3b9f94e1 | 2271 | #endif |
faea38e7 FB |
2272 | time_t ti; |
2273 | int64_t secs; | |
2274 | ||
2275 | if (!sn) { | |
5fafdf24 TS |
2276 | snprintf(buf, buf_size, |
2277 | "%-10s%-20s%7s%20s%15s", | |
faea38e7 FB |
2278 | "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); |
2279 | } else { | |
2280 | ti = sn->date_sec; | |
3b9f94e1 FB |
2281 | #ifdef _WIN32 |
2282 | ptm = localtime(&ti); | |
2283 | strftime(date_buf, sizeof(date_buf), | |
2284 | "%Y-%m-%d %H:%M:%S", ptm); | |
2285 | #else | |
faea38e7 FB |
2286 | localtime_r(&ti, &tm); |
2287 | strftime(date_buf, sizeof(date_buf), | |
2288 | "%Y-%m-%d %H:%M:%S", &tm); | |
3b9f94e1 | 2289 | #endif |
faea38e7 FB |
2290 | secs = sn->vm_clock_nsec / 1000000000; |
2291 | snprintf(clock_buf, sizeof(clock_buf), | |
2292 | "%02d:%02d:%02d.%03d", | |
2293 | (int)(secs / 3600), | |
2294 | (int)((secs / 60) % 60), | |
5fafdf24 | 2295 | (int)(secs % 60), |
faea38e7 FB |
2296 | (int)((sn->vm_clock_nsec / 1000000) % 1000)); |
2297 | snprintf(buf, buf_size, | |
5fafdf24 | 2298 | "%-10s%-20s%7s%20s%15s", |
faea38e7 FB |
2299 | sn->id_str, sn->name, |
2300 | get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), | |
2301 | date_buf, | |
2302 | clock_buf); | |
2303 | } | |
2304 | return buf; | |
2305 | } | |
2306 | ||
ea2384d3 | 2307 | /**************************************************************/ |
83f64091 | 2308 | /* async I/Os */ |
ea2384d3 | 2309 | |
3b69e4b9 | 2310 | BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, |
f141eafe | 2311 | QEMUIOVector *qiov, int nb_sectors, |
3b69e4b9 | 2312 | BlockDriverCompletionFunc *cb, void *opaque) |
83f64091 FB |
2313 | { |
2314 | BlockDriver *drv = bs->drv; | |
83f64091 | 2315 | |
bbf0a440 SH |
2316 | trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); |
2317 | ||
19cb3738 | 2318 | if (!drv) |
ce1a14dc | 2319 | return NULL; |
71d0770c AL |
2320 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
2321 | return NULL; | |
3b46e624 | 2322 | |
a597e79c CH |
2323 | return drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors, |
2324 | cb, opaque); | |
ea2384d3 FB |
2325 | } |
2326 | ||
4dcafbb1 MT |
2327 | typedef struct BlockCompleteData { |
2328 | BlockDriverCompletionFunc *cb; | |
2329 | void *opaque; | |
2330 | BlockDriverState *bs; | |
2331 | int64_t sector_num; | |
2332 | int nb_sectors; | |
2333 | } BlockCompleteData; | |
2334 | ||
2335 | static void block_complete_cb(void *opaque, int ret) | |
2336 | { | |
2337 | BlockCompleteData *b = opaque; | |
2338 | ||
2339 | if (b->bs->dirty_bitmap) { | |
2340 | set_dirty_bitmap(b->bs, b->sector_num, b->nb_sectors, 1); | |
2341 | } | |
2342 | b->cb(b->opaque, ret); | |
7267c094 | 2343 | g_free(b); |
4dcafbb1 MT |
2344 | } |
2345 | ||
2346 | static BlockCompleteData *blk_dirty_cb_alloc(BlockDriverState *bs, | |
2347 | int64_t sector_num, | |
2348 | int nb_sectors, | |
2349 | BlockDriverCompletionFunc *cb, | |
2350 | void *opaque) | |
2351 | { | |
7267c094 | 2352 | BlockCompleteData *blkdata = g_malloc0(sizeof(BlockCompleteData)); |
4dcafbb1 MT |
2353 | |
2354 | blkdata->bs = bs; | |
2355 | blkdata->cb = cb; | |
2356 | blkdata->opaque = opaque; | |
2357 | blkdata->sector_num = sector_num; | |
2358 | blkdata->nb_sectors = nb_sectors; | |
2359 | ||
2360 | return blkdata; | |
2361 | } | |
2362 | ||
f141eafe AL |
2363 | BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, |
2364 | QEMUIOVector *qiov, int nb_sectors, | |
2365 | BlockDriverCompletionFunc *cb, void *opaque) | |
ea2384d3 | 2366 | { |
83f64091 | 2367 | BlockDriver *drv = bs->drv; |
a36e69dd | 2368 | BlockDriverAIOCB *ret; |
4dcafbb1 | 2369 | BlockCompleteData *blk_cb_data; |
ea2384d3 | 2370 | |
bbf0a440 SH |
2371 | trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); |
2372 | ||
19cb3738 | 2373 | if (!drv) |
ce1a14dc | 2374 | return NULL; |
83f64091 | 2375 | if (bs->read_only) |
ce1a14dc | 2376 | return NULL; |
71d0770c AL |
2377 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
2378 | return NULL; | |
83f64091 | 2379 | |
c6d22830 | 2380 | if (bs->dirty_bitmap) { |
4dcafbb1 MT |
2381 | blk_cb_data = blk_dirty_cb_alloc(bs, sector_num, nb_sectors, cb, |
2382 | opaque); | |
2383 | cb = &block_complete_cb; | |
2384 | opaque = blk_cb_data; | |
7cd1e32a | 2385 | } |
a55eb92c | 2386 | |
f141eafe AL |
2387 | ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors, |
2388 | cb, opaque); | |
a36e69dd TS |
2389 | |
2390 | if (ret) { | |
294cc35f KW |
2391 | if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { |
2392 | bs->wr_highest_sector = sector_num + nb_sectors - 1; | |
2393 | } | |
a36e69dd TS |
2394 | } |
2395 | ||
2396 | return ret; | |
83f64091 FB |
2397 | } |
2398 | ||
40b4f539 KW |
2399 | |
2400 | typedef struct MultiwriteCB { | |
2401 | int error; | |
2402 | int num_requests; | |
2403 | int num_callbacks; | |
2404 | struct { | |
2405 | BlockDriverCompletionFunc *cb; | |
2406 | void *opaque; | |
2407 | QEMUIOVector *free_qiov; | |
2408 | void *free_buf; | |
2409 | } callbacks[]; | |
2410 | } MultiwriteCB; | |
2411 | ||
2412 | static void multiwrite_user_cb(MultiwriteCB *mcb) | |
2413 | { | |
2414 | int i; | |
2415 | ||
2416 | for (i = 0; i < mcb->num_callbacks; i++) { | |
2417 | mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); | |
1e1ea48d SH |
2418 | if (mcb->callbacks[i].free_qiov) { |
2419 | qemu_iovec_destroy(mcb->callbacks[i].free_qiov); | |
2420 | } | |
7267c094 | 2421 | g_free(mcb->callbacks[i].free_qiov); |
f8a83245 | 2422 | qemu_vfree(mcb->callbacks[i].free_buf); |
40b4f539 KW |
2423 | } |
2424 | } | |
2425 | ||
2426 | static void multiwrite_cb(void *opaque, int ret) | |
2427 | { | |
2428 | MultiwriteCB *mcb = opaque; | |
2429 | ||
6d519a5f SH |
2430 | trace_multiwrite_cb(mcb, ret); |
2431 | ||
cb6d3ca0 | 2432 | if (ret < 0 && !mcb->error) { |
40b4f539 | 2433 | mcb->error = ret; |
40b4f539 KW |
2434 | } |
2435 | ||
2436 | mcb->num_requests--; | |
2437 | if (mcb->num_requests == 0) { | |
de189a1b | 2438 | multiwrite_user_cb(mcb); |
7267c094 | 2439 | g_free(mcb); |
40b4f539 KW |
2440 | } |
2441 | } | |
2442 | ||
2443 | static int multiwrite_req_compare(const void *a, const void *b) | |
2444 | { | |
77be4366 CH |
2445 | const BlockRequest *req1 = a, *req2 = b; |
2446 | ||
2447 | /* | |
2448 | * Note that we can't simply subtract req2->sector from req1->sector | |
2449 | * here as that could overflow the return value. | |
2450 | */ | |
2451 | if (req1->sector > req2->sector) { | |
2452 | return 1; | |
2453 | } else if (req1->sector < req2->sector) { | |
2454 | return -1; | |
2455 | } else { | |
2456 | return 0; | |
2457 | } | |
40b4f539 KW |
2458 | } |
2459 | ||
2460 | /* | |
2461 | * Takes a bunch of requests and tries to merge them. Returns the number of | |
2462 | * requests that remain after merging. | |
2463 | */ | |
2464 | static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, | |
2465 | int num_reqs, MultiwriteCB *mcb) | |
2466 | { | |
2467 | int i, outidx; | |
2468 | ||
2469 | // Sort requests by start sector | |
2470 | qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); | |
2471 | ||
2472 | // Check if adjacent requests touch the same clusters. If so, combine them, | |
2473 | // filling up gaps with zero sectors. | |
2474 | outidx = 0; | |
2475 | for (i = 1; i < num_reqs; i++) { | |
2476 | int merge = 0; | |
2477 | int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; | |
2478 | ||
2479 | // This handles the cases that are valid for all block drivers, namely | |
2480 | // exactly sequential writes and overlapping writes. | |
2481 | if (reqs[i].sector <= oldreq_last) { | |
2482 | merge = 1; | |
2483 | } | |
2484 | ||
2485 | // The block driver may decide that it makes sense to combine requests | |
2486 | // even if there is a gap of some sectors between them. In this case, | |
2487 | // the gap is filled with zeros (therefore only applicable for yet | |
2488 | // unused space in format like qcow2). | |
2489 | if (!merge && bs->drv->bdrv_merge_requests) { | |
2490 | merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]); | |
2491 | } | |
2492 | ||
e2a305fb CH |
2493 | if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { |
2494 | merge = 0; | |
2495 | } | |
2496 | ||
40b4f539 KW |
2497 | if (merge) { |
2498 | size_t size; | |
7267c094 | 2499 | QEMUIOVector *qiov = g_malloc0(sizeof(*qiov)); |
40b4f539 KW |
2500 | qemu_iovec_init(qiov, |
2501 | reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); | |
2502 | ||
2503 | // Add the first request to the merged one. If the requests are | |
2504 | // overlapping, drop the last sectors of the first request. | |
2505 | size = (reqs[i].sector - reqs[outidx].sector) << 9; | |
2506 | qemu_iovec_concat(qiov, reqs[outidx].qiov, size); | |
2507 | ||
2508 | // We might need to add some zeros between the two requests | |
2509 | if (reqs[i].sector > oldreq_last) { | |
2510 | size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9; | |
2511 | uint8_t *buf = qemu_blockalign(bs, zero_bytes); | |
2512 | memset(buf, 0, zero_bytes); | |
2513 | qemu_iovec_add(qiov, buf, zero_bytes); | |
2514 | mcb->callbacks[i].free_buf = buf; | |
2515 | } | |
2516 | ||
2517 | // Add the second request | |
2518 | qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size); | |
2519 | ||
cbf1dff2 | 2520 | reqs[outidx].nb_sectors = qiov->size >> 9; |
40b4f539 KW |
2521 | reqs[outidx].qiov = qiov; |
2522 | ||
2523 | mcb->callbacks[i].free_qiov = reqs[outidx].qiov; | |
2524 | } else { | |
2525 | outidx++; | |
2526 | reqs[outidx].sector = reqs[i].sector; | |
2527 | reqs[outidx].nb_sectors = reqs[i].nb_sectors; | |
2528 | reqs[outidx].qiov = reqs[i].qiov; | |
2529 | } | |
2530 | } | |
2531 | ||
2532 | return outidx + 1; | |
2533 | } | |
2534 | ||
2535 | /* | |
2536 | * Submit multiple AIO write requests at once. | |
2537 | * | |
2538 | * On success, the function returns 0 and all requests in the reqs array have | |
2539 | * been submitted. In error case this function returns -1, and any of the | |
2540 | * requests may or may not be submitted yet. In particular, this means that the | |
2541 | * callback will be called for some of the requests, for others it won't. The | |
2542 | * caller must check the error field of the BlockRequest to wait for the right | |
2543 | * callbacks (if error != 0, no callback will be called). | |
2544 | * | |
2545 | * The implementation may modify the contents of the reqs array, e.g. to merge | |
2546 | * requests. However, the fields opaque and error are left unmodified as they | |
2547 | * are used to signal failure for a single request to the caller. | |
2548 | */ | |
2549 | int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) | |
2550 | { | |
2551 | BlockDriverAIOCB *acb; | |
2552 | MultiwriteCB *mcb; | |
2553 | int i; | |
2554 | ||
301db7c2 RH |
2555 | /* don't submit writes if we don't have a medium */ |
2556 | if (bs->drv == NULL) { | |
2557 | for (i = 0; i < num_reqs; i++) { | |
2558 | reqs[i].error = -ENOMEDIUM; | |
2559 | } | |
2560 | return -1; | |
2561 | } | |
2562 | ||
40b4f539 KW |
2563 | if (num_reqs == 0) { |
2564 | return 0; | |
2565 | } | |
2566 | ||
2567 | // Create MultiwriteCB structure | |
7267c094 | 2568 | mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); |
40b4f539 KW |
2569 | mcb->num_requests = 0; |
2570 | mcb->num_callbacks = num_reqs; | |
2571 | ||
2572 | for (i = 0; i < num_reqs; i++) { | |
2573 | mcb->callbacks[i].cb = reqs[i].cb; | |
2574 | mcb->callbacks[i].opaque = reqs[i].opaque; | |
2575 | } | |
2576 | ||
2577 | // Check for mergable requests | |
2578 | num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); | |
2579 | ||
6d519a5f SH |
2580 | trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); |
2581 | ||
453f9a16 KW |
2582 | /* |
2583 | * Run the aio requests. As soon as one request can't be submitted | |
2584 | * successfully, fail all requests that are not yet submitted (we must | |
2585 | * return failure for all requests anyway) | |
2586 | * | |
2587 | * num_requests cannot be set to the right value immediately: If | |
2588 | * bdrv_aio_writev fails for some request, num_requests would be too high | |
2589 | * and therefore multiwrite_cb() would never recognize the multiwrite | |
2590 | * request as completed. We also cannot use the loop variable i to set it | |
2591 | * when the first request fails because the callback may already have been | |
2592 | * called for previously submitted requests. Thus, num_requests must be | |
2593 | * incremented for each request that is submitted. | |
2594 | * | |
2595 | * The problem that callbacks may be called early also means that we need | |
2596 | * to take care that num_requests doesn't become 0 before all requests are | |
2597 | * submitted - multiwrite_cb() would consider the multiwrite request | |
2598 | * completed. A dummy request that is "completed" by a manual call to | |
2599 | * multiwrite_cb() takes care of this. | |
2600 | */ | |
2601 | mcb->num_requests = 1; | |
2602 | ||
6d519a5f | 2603 | // Run the aio requests |
40b4f539 | 2604 | for (i = 0; i < num_reqs; i++) { |
453f9a16 | 2605 | mcb->num_requests++; |
40b4f539 KW |
2606 | acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, |
2607 | reqs[i].nb_sectors, multiwrite_cb, mcb); | |
2608 | ||
2609 | if (acb == NULL) { | |
2610 | // We can only fail the whole thing if no request has been | |
2611 | // submitted yet. Otherwise we'll wait for the submitted AIOs to | |
2612 | // complete and report the error in the callback. | |
453f9a16 | 2613 | if (i == 0) { |
6d519a5f | 2614 | trace_bdrv_aio_multiwrite_earlyfail(mcb); |
40b4f539 KW |
2615 | goto fail; |
2616 | } else { | |
6d519a5f | 2617 | trace_bdrv_aio_multiwrite_latefail(mcb, i); |
7eb58a6c | 2618 | multiwrite_cb(mcb, -EIO); |
40b4f539 KW |
2619 | break; |
2620 | } | |
40b4f539 KW |
2621 | } |
2622 | } | |
2623 | ||
453f9a16 KW |
2624 | /* Complete the dummy request */ |
2625 | multiwrite_cb(mcb, 0); | |
2626 | ||
40b4f539 KW |
2627 | return 0; |
2628 | ||
2629 | fail: | |
453f9a16 KW |
2630 | for (i = 0; i < mcb->num_callbacks; i++) { |
2631 | reqs[i].error = -EIO; | |
2632 | } | |
7267c094 | 2633 | g_free(mcb); |
40b4f539 KW |
2634 | return -1; |
2635 | } | |
2636 | ||
b2e12bc6 CH |
2637 | BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, |
2638 | BlockDriverCompletionFunc *cb, void *opaque) | |
2639 | { | |
2640 | BlockDriver *drv = bs->drv; | |
2641 | ||
a13aac04 SH |
2642 | trace_bdrv_aio_flush(bs, opaque); |
2643 | ||
016f5cf6 AG |
2644 | if (bs->open_flags & BDRV_O_NO_FLUSH) { |
2645 | return bdrv_aio_noop_em(bs, cb, opaque); | |
2646 | } | |
2647 | ||
b2e12bc6 CH |
2648 | if (!drv) |
2649 | return NULL; | |
b2e12bc6 CH |
2650 | return drv->bdrv_aio_flush(bs, cb, opaque); |
2651 | } | |
2652 | ||
83f64091 | 2653 | void bdrv_aio_cancel(BlockDriverAIOCB *acb) |
83f64091 | 2654 | { |
6bbff9a0 | 2655 | acb->pool->cancel(acb); |
83f64091 FB |
2656 | } |
2657 | ||
ce1a14dc | 2658 | |
83f64091 FB |
2659 | /**************************************************************/ |
2660 | /* async block device emulation */ | |
2661 | ||
c16b5a2c CH |
2662 | typedef struct BlockDriverAIOCBSync { |
2663 | BlockDriverAIOCB common; | |
2664 | QEMUBH *bh; | |
2665 | int ret; | |
2666 | /* vector translation state */ | |
2667 | QEMUIOVector *qiov; | |
2668 | uint8_t *bounce; | |
2669 | int is_write; | |
2670 | } BlockDriverAIOCBSync; | |
2671 | ||
2672 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) | |
2673 | { | |
b666d239 KW |
2674 | BlockDriverAIOCBSync *acb = |
2675 | container_of(blockacb, BlockDriverAIOCBSync, common); | |
6a7ad299 | 2676 | qemu_bh_delete(acb->bh); |
36afc451 | 2677 | acb->bh = NULL; |
c16b5a2c CH |
2678 | qemu_aio_release(acb); |
2679 | } | |
2680 | ||
2681 | static AIOPool bdrv_em_aio_pool = { | |
2682 | .aiocb_size = sizeof(BlockDriverAIOCBSync), | |
2683 | .cancel = bdrv_aio_cancel_em, | |
2684 | }; | |
2685 | ||
ce1a14dc | 2686 | static void bdrv_aio_bh_cb(void *opaque) |
83f64091 | 2687 | { |
ce1a14dc | 2688 | BlockDriverAIOCBSync *acb = opaque; |
f141eafe | 2689 | |
f141eafe AL |
2690 | if (!acb->is_write) |
2691 | qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); | |
ceb42de8 | 2692 | qemu_vfree(acb->bounce); |
ce1a14dc | 2693 | acb->common.cb(acb->common.opaque, acb->ret); |
6a7ad299 | 2694 | qemu_bh_delete(acb->bh); |
36afc451 | 2695 | acb->bh = NULL; |
ce1a14dc | 2696 | qemu_aio_release(acb); |
83f64091 | 2697 | } |
beac80cd | 2698 | |
f141eafe AL |
2699 | static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, |
2700 | int64_t sector_num, | |
2701 | QEMUIOVector *qiov, | |
2702 | int nb_sectors, | |
2703 | BlockDriverCompletionFunc *cb, | |
2704 | void *opaque, | |
2705 | int is_write) | |
2706 | ||
83f64091 | 2707 | { |
ce1a14dc | 2708 | BlockDriverAIOCBSync *acb; |
ce1a14dc | 2709 | |
c16b5a2c | 2710 | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); |
f141eafe AL |
2711 | acb->is_write = is_write; |
2712 | acb->qiov = qiov; | |
e268ca52 | 2713 | acb->bounce = qemu_blockalign(bs, qiov->size); |
f141eafe | 2714 | |
ce1a14dc PB |
2715 | if (!acb->bh) |
2716 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); | |
f141eafe AL |
2717 | |
2718 | if (is_write) { | |
2719 | qemu_iovec_to_buffer(acb->qiov, acb->bounce); | |
2720 | acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors); | |
2721 | } else { | |
2722 | acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors); | |
2723 | } | |
2724 | ||
ce1a14dc | 2725 | qemu_bh_schedule(acb->bh); |
f141eafe | 2726 | |
ce1a14dc | 2727 | return &acb->common; |
beac80cd FB |
2728 | } |
2729 | ||
f141eafe AL |
2730 | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, |
2731 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
ce1a14dc | 2732 | BlockDriverCompletionFunc *cb, void *opaque) |
beac80cd | 2733 | { |
f141eafe AL |
2734 | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); |
2735 | } | |
83f64091 | 2736 | |
f141eafe AL |
2737 | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, |
2738 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
2739 | BlockDriverCompletionFunc *cb, void *opaque) | |
2740 | { | |
2741 | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); | |
beac80cd | 2742 | } |
beac80cd | 2743 | |
68485420 KW |
2744 | |
2745 | typedef struct BlockDriverAIOCBCoroutine { | |
2746 | BlockDriverAIOCB common; | |
2747 | BlockRequest req; | |
2748 | bool is_write; | |
2749 | QEMUBH* bh; | |
2750 | } BlockDriverAIOCBCoroutine; | |
2751 | ||
2752 | static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) | |
2753 | { | |
2754 | qemu_aio_flush(); | |
2755 | } | |
2756 | ||
2757 | static AIOPool bdrv_em_co_aio_pool = { | |
2758 | .aiocb_size = sizeof(BlockDriverAIOCBCoroutine), | |
2759 | .cancel = bdrv_aio_co_cancel_em, | |
2760 | }; | |
2761 | ||
2762 | static void bdrv_co_rw_bh(void *opaque) | |
2763 | { | |
2764 | BlockDriverAIOCBCoroutine *acb = opaque; | |
2765 | ||
2766 | acb->common.cb(acb->common.opaque, acb->req.error); | |
2767 | qemu_bh_delete(acb->bh); | |
2768 | qemu_aio_release(acb); | |
2769 | } | |
2770 | ||
2771 | static void coroutine_fn bdrv_co_rw(void *opaque) | |
2772 | { | |
2773 | BlockDriverAIOCBCoroutine *acb = opaque; | |
2774 | BlockDriverState *bs = acb->common.bs; | |
2775 | ||
2776 | if (!acb->is_write) { | |
2777 | acb->req.error = bs->drv->bdrv_co_readv(bs, acb->req.sector, | |
2778 | acb->req.nb_sectors, acb->req.qiov); | |
2779 | } else { | |
2780 | acb->req.error = bs->drv->bdrv_co_writev(bs, acb->req.sector, | |
2781 | acb->req.nb_sectors, acb->req.qiov); | |
2782 | } | |
2783 | ||
2784 | acb->bh = qemu_bh_new(bdrv_co_rw_bh, acb); | |
2785 | qemu_bh_schedule(acb->bh); | |
2786 | } | |
2787 | ||
2788 | static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, | |
2789 | int64_t sector_num, | |
2790 | QEMUIOVector *qiov, | |
2791 | int nb_sectors, | |
2792 | BlockDriverCompletionFunc *cb, | |
2793 | void *opaque, | |
2794 | bool is_write) | |
2795 | { | |
2796 | Coroutine *co; | |
2797 | BlockDriverAIOCBCoroutine *acb; | |
2798 | ||
2799 | acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); | |
2800 | acb->req.sector = sector_num; | |
2801 | acb->req.nb_sectors = nb_sectors; | |
2802 | acb->req.qiov = qiov; | |
2803 | acb->is_write = is_write; | |
2804 | ||
2805 | co = qemu_coroutine_create(bdrv_co_rw); | |
2806 | qemu_coroutine_enter(co, acb); | |
2807 | ||
2808 | return &acb->common; | |
2809 | } | |
2810 | ||
2811 | static BlockDriverAIOCB *bdrv_co_aio_readv_em(BlockDriverState *bs, | |
2812 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
2813 | BlockDriverCompletionFunc *cb, void *opaque) | |
2814 | { | |
2815 | return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, | |
2816 | false); | |
2817 | } | |
2818 | ||
2819 | static BlockDriverAIOCB *bdrv_co_aio_writev_em(BlockDriverState *bs, | |
2820 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
2821 | BlockDriverCompletionFunc *cb, void *opaque) | |
2822 | { | |
2823 | return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, | |
2824 | true); | |
2825 | } | |
2826 | ||
b2e12bc6 CH |
2827 | static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, |
2828 | BlockDriverCompletionFunc *cb, void *opaque) | |
2829 | { | |
2830 | BlockDriverAIOCBSync *acb; | |
2831 | ||
2832 | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); | |
2833 | acb->is_write = 1; /* don't bounce in the completion hadler */ | |
2834 | acb->qiov = NULL; | |
2835 | acb->bounce = NULL; | |
2836 | acb->ret = 0; | |
2837 | ||
2838 | if (!acb->bh) | |
2839 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); | |
2840 | ||
2841 | bdrv_flush(bs); | |
2842 | qemu_bh_schedule(acb->bh); | |
2843 | return &acb->common; | |
2844 | } | |
2845 | ||
016f5cf6 AG |
2846 | static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs, |
2847 | BlockDriverCompletionFunc *cb, void *opaque) | |
2848 | { | |
2849 | BlockDriverAIOCBSync *acb; | |
2850 | ||
2851 | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); | |
2852 | acb->is_write = 1; /* don't bounce in the completion handler */ | |
2853 | acb->qiov = NULL; | |
2854 | acb->bounce = NULL; | |
2855 | acb->ret = 0; | |
2856 | ||
2857 | if (!acb->bh) { | |
2858 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); | |
2859 | } | |
2860 | ||
2861 | qemu_bh_schedule(acb->bh); | |
2862 | return &acb->common; | |
2863 | } | |
2864 | ||
83f64091 FB |
2865 | /**************************************************************/ |
2866 | /* sync block device emulation */ | |
ea2384d3 | 2867 | |
83f64091 FB |
2868 | static void bdrv_rw_em_cb(void *opaque, int ret) |
2869 | { | |
2870 | *(int *)opaque = ret; | |
ea2384d3 FB |
2871 | } |
2872 | ||
83f64091 FB |
2873 | #define NOT_DONE 0x7fffffff |
2874 | ||
5fafdf24 | 2875 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
83f64091 | 2876 | uint8_t *buf, int nb_sectors) |
7a6cba61 | 2877 | { |
ce1a14dc PB |
2878 | int async_ret; |
2879 | BlockDriverAIOCB *acb; | |
f141eafe AL |
2880 | struct iovec iov; |
2881 | QEMUIOVector qiov; | |
83f64091 | 2882 | |
83f64091 | 2883 | async_ret = NOT_DONE; |
3f4cb3d3 | 2884 | iov.iov_base = (void *)buf; |
eb5a3165 | 2885 | iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; |
f141eafe AL |
2886 | qemu_iovec_init_external(&qiov, &iov, 1); |
2887 | acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors, | |
2888 | bdrv_rw_em_cb, &async_ret); | |
65d6b3d8 KW |
2889 | if (acb == NULL) { |
2890 | async_ret = -1; | |
2891 | goto fail; | |
2892 | } | |
baf35cb9 | 2893 | |
83f64091 FB |
2894 | while (async_ret == NOT_DONE) { |
2895 | qemu_aio_wait(); | |
2896 | } | |
baf35cb9 | 2897 | |
65d6b3d8 KW |
2898 | |
2899 | fail: | |
83f64091 | 2900 | return async_ret; |
7a6cba61 PB |
2901 | } |
2902 | ||
83f64091 FB |
2903 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
2904 | const uint8_t *buf, int nb_sectors) | |
2905 | { | |
ce1a14dc PB |
2906 | int async_ret; |
2907 | BlockDriverAIOCB *acb; | |
f141eafe AL |
2908 | struct iovec iov; |
2909 | QEMUIOVector qiov; | |
83f64091 | 2910 | |
83f64091 | 2911 | async_ret = NOT_DONE; |
f141eafe | 2912 | iov.iov_base = (void *)buf; |
eb5a3165 | 2913 | iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; |
f141eafe AL |
2914 | qemu_iovec_init_external(&qiov, &iov, 1); |
2915 | acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors, | |
2916 | bdrv_rw_em_cb, &async_ret); | |
65d6b3d8 KW |
2917 | if (acb == NULL) { |
2918 | async_ret = -1; | |
2919 | goto fail; | |
2920 | } | |
83f64091 FB |
2921 | while (async_ret == NOT_DONE) { |
2922 | qemu_aio_wait(); | |
2923 | } | |
65d6b3d8 KW |
2924 | |
2925 | fail: | |
83f64091 FB |
2926 | return async_ret; |
2927 | } | |
ea2384d3 FB |
2928 | |
2929 | void bdrv_init(void) | |
2930 | { | |
5efa9d5a | 2931 | module_call_init(MODULE_INIT_BLOCK); |
ea2384d3 | 2932 | } |
ce1a14dc | 2933 | |
eb852011 MA |
2934 | void bdrv_init_with_whitelist(void) |
2935 | { | |
2936 | use_bdrv_whitelist = 1; | |
2937 | bdrv_init(); | |
2938 | } | |
2939 | ||
c16b5a2c CH |
2940 | void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, |
2941 | BlockDriverCompletionFunc *cb, void *opaque) | |
ce1a14dc | 2942 | { |
ce1a14dc PB |
2943 | BlockDriverAIOCB *acb; |
2944 | ||
6bbff9a0 AL |
2945 | if (pool->free_aiocb) { |
2946 | acb = pool->free_aiocb; | |
2947 | pool->free_aiocb = acb->next; | |
ce1a14dc | 2948 | } else { |
7267c094 | 2949 | acb = g_malloc0(pool->aiocb_size); |
6bbff9a0 | 2950 | acb->pool = pool; |
ce1a14dc PB |
2951 | } |
2952 | acb->bs = bs; | |
2953 | acb->cb = cb; | |
2954 | acb->opaque = opaque; | |
2955 | return acb; | |
2956 | } | |
2957 | ||
2958 | void qemu_aio_release(void *p) | |
2959 | { | |
6bbff9a0 AL |
2960 | BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; |
2961 | AIOPool *pool = acb->pool; | |
2962 | acb->next = pool->free_aiocb; | |
2963 | pool->free_aiocb = acb; | |
ce1a14dc | 2964 | } |
19cb3738 | 2965 | |
f9f05dc5 KW |
2966 | /**************************************************************/ |
2967 | /* Coroutine block device emulation */ | |
2968 | ||
2969 | typedef struct CoroutineIOCompletion { | |
2970 | Coroutine *coroutine; | |
2971 | int ret; | |
2972 | } CoroutineIOCompletion; | |
2973 | ||
2974 | static void bdrv_co_io_em_complete(void *opaque, int ret) | |
2975 | { | |
2976 | CoroutineIOCompletion *co = opaque; | |
2977 | ||
2978 | co->ret = ret; | |
2979 | qemu_coroutine_enter(co->coroutine, NULL); | |
2980 | } | |
2981 | ||
2982 | static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, | |
2983 | int nb_sectors, QEMUIOVector *iov, | |
2984 | bool is_write) | |
2985 | { | |
2986 | CoroutineIOCompletion co = { | |
2987 | .coroutine = qemu_coroutine_self(), | |
2988 | }; | |
2989 | BlockDriverAIOCB *acb; | |
2990 | ||
2991 | if (is_write) { | |
2992 | acb = bdrv_aio_writev(bs, sector_num, iov, nb_sectors, | |
2993 | bdrv_co_io_em_complete, &co); | |
2994 | } else { | |
2995 | acb = bdrv_aio_readv(bs, sector_num, iov, nb_sectors, | |
2996 | bdrv_co_io_em_complete, &co); | |
2997 | } | |
2998 | ||
2999 | trace_bdrv_co_io(is_write, acb); | |
3000 | if (!acb) { | |
3001 | return -EIO; | |
3002 | } | |
3003 | qemu_coroutine_yield(); | |
3004 | ||
3005 | return co.ret; | |
3006 | } | |
3007 | ||
3008 | static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, | |
3009 | int64_t sector_num, int nb_sectors, | |
3010 | QEMUIOVector *iov) | |
3011 | { | |
3012 | return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); | |
3013 | } | |
3014 | ||
3015 | static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, | |
3016 | int64_t sector_num, int nb_sectors, | |
3017 | QEMUIOVector *iov) | |
3018 | { | |
3019 | return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); | |
3020 | } | |
3021 | ||
e7a8a783 KW |
3022 | static int coroutine_fn bdrv_co_flush_em(BlockDriverState *bs) |
3023 | { | |
3024 | CoroutineIOCompletion co = { | |
3025 | .coroutine = qemu_coroutine_self(), | |
3026 | }; | |
3027 | BlockDriverAIOCB *acb; | |
3028 | ||
3029 | acb = bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); | |
3030 | if (!acb) { | |
3031 | return -EIO; | |
3032 | } | |
3033 | qemu_coroutine_yield(); | |
3034 | return co.ret; | |
3035 | } | |
3036 | ||
19cb3738 FB |
3037 | /**************************************************************/ |
3038 | /* removable device support */ | |
3039 | ||
3040 | /** | |
3041 | * Return TRUE if the media is present | |
3042 | */ | |
3043 | int bdrv_is_inserted(BlockDriverState *bs) | |
3044 | { | |
3045 | BlockDriver *drv = bs->drv; | |
a1aff5bf | 3046 | |
19cb3738 FB |
3047 | if (!drv) |
3048 | return 0; | |
3049 | if (!drv->bdrv_is_inserted) | |
a1aff5bf MA |
3050 | return 1; |
3051 | return drv->bdrv_is_inserted(bs); | |
19cb3738 FB |
3052 | } |
3053 | ||
3054 | /** | |
8e49ca46 MA |
3055 | * Return whether the media changed since the last call to this |
3056 | * function, or -ENOTSUP if we don't know. Most drivers don't know. | |
19cb3738 FB |
3057 | */ |
3058 | int bdrv_media_changed(BlockDriverState *bs) | |
3059 | { | |
3060 | BlockDriver *drv = bs->drv; | |
19cb3738 | 3061 | |
8e49ca46 MA |
3062 | if (drv && drv->bdrv_media_changed) { |
3063 | return drv->bdrv_media_changed(bs); | |
3064 | } | |
3065 | return -ENOTSUP; | |
19cb3738 FB |
3066 | } |
3067 | ||
3068 | /** | |
3069 | * If eject_flag is TRUE, eject the media. Otherwise, close the tray | |
3070 | */ | |
fdec4404 | 3071 | void bdrv_eject(BlockDriverState *bs, int eject_flag) |
19cb3738 FB |
3072 | { |
3073 | BlockDriver *drv = bs->drv; | |
19cb3738 | 3074 | |
822e1cd1 MA |
3075 | if (drv && drv->bdrv_eject) { |
3076 | drv->bdrv_eject(bs, eject_flag); | |
19cb3738 FB |
3077 | } |
3078 | } | |
3079 | ||
19cb3738 FB |
3080 | /** |
3081 | * Lock or unlock the media (if it is locked, the user won't be able | |
3082 | * to eject it manually). | |
3083 | */ | |
025e849a | 3084 | void bdrv_lock_medium(BlockDriverState *bs, bool locked) |
19cb3738 FB |
3085 | { |
3086 | BlockDriver *drv = bs->drv; | |
3087 | ||
025e849a | 3088 | trace_bdrv_lock_medium(bs, locked); |
b8c6d095 | 3089 | |
025e849a MA |
3090 | if (drv && drv->bdrv_lock_medium) { |
3091 | drv->bdrv_lock_medium(bs, locked); | |
19cb3738 FB |
3092 | } |
3093 | } | |
985a03b0 TS |
3094 | |
3095 | /* needed for generic scsi interface */ | |
3096 | ||
3097 | int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) | |
3098 | { | |
3099 | BlockDriver *drv = bs->drv; | |
3100 | ||
3101 | if (drv && drv->bdrv_ioctl) | |
3102 | return drv->bdrv_ioctl(bs, req, buf); | |
3103 | return -ENOTSUP; | |
3104 | } | |
7d780669 | 3105 | |
221f715d AL |
3106 | BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, |
3107 | unsigned long int req, void *buf, | |
3108 | BlockDriverCompletionFunc *cb, void *opaque) | |
7d780669 | 3109 | { |
221f715d | 3110 | BlockDriver *drv = bs->drv; |
7d780669 | 3111 | |
221f715d AL |
3112 | if (drv && drv->bdrv_aio_ioctl) |
3113 | return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); | |
3114 | return NULL; | |
7d780669 | 3115 | } |
e268ca52 | 3116 | |
7b6f9300 MA |
3117 | void bdrv_set_buffer_alignment(BlockDriverState *bs, int align) |
3118 | { | |
3119 | bs->buffer_alignment = align; | |
3120 | } | |
7cd1e32a | 3121 | |
e268ca52 AL |
3122 | void *qemu_blockalign(BlockDriverState *bs, size_t size) |
3123 | { | |
3124 | return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); | |
3125 | } | |
7cd1e32a | 3126 | |
3127 | void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) | |
3128 | { | |
3129 | int64_t bitmap_size; | |
a55eb92c | 3130 | |
aaa0eb75 | 3131 | bs->dirty_count = 0; |
a55eb92c | 3132 | if (enable) { |
c6d22830 JK |
3133 | if (!bs->dirty_bitmap) { |
3134 | bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + | |
3135 | BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; | |
3136 | bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8; | |
a55eb92c | 3137 | |
7267c094 | 3138 | bs->dirty_bitmap = g_malloc0(bitmap_size); |
a55eb92c | 3139 | } |
7cd1e32a | 3140 | } else { |
c6d22830 | 3141 | if (bs->dirty_bitmap) { |
7267c094 | 3142 | g_free(bs->dirty_bitmap); |
c6d22830 | 3143 | bs->dirty_bitmap = NULL; |
a55eb92c | 3144 | } |
7cd1e32a | 3145 | } |
3146 | } | |
3147 | ||
3148 | int bdrv_get_dirty(BlockDriverState *bs, int64_t sector) | |
3149 | { | |
6ea44308 | 3150 | int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; |
a55eb92c | 3151 | |
c6d22830 JK |
3152 | if (bs->dirty_bitmap && |
3153 | (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { | |
6d59fec1 MT |
3154 | return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] & |
3155 | (1UL << (chunk % (sizeof(unsigned long) * 8)))); | |
7cd1e32a | 3156 | } else { |
3157 | return 0; | |
3158 | } | |
3159 | } | |
3160 | ||
a55eb92c JK |
3161 | void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, |
3162 | int nr_sectors) | |
7cd1e32a | 3163 | { |
3164 | set_dirty_bitmap(bs, cur_sector, nr_sectors, 0); | |
3165 | } | |
aaa0eb75 LS |
3166 | |
3167 | int64_t bdrv_get_dirty_count(BlockDriverState *bs) | |
3168 | { | |
3169 | return bs->dirty_count; | |
3170 | } | |
f88e1a42 | 3171 | |
db593f25 MT |
3172 | void bdrv_set_in_use(BlockDriverState *bs, int in_use) |
3173 | { | |
3174 | assert(bs->in_use != in_use); | |
3175 | bs->in_use = in_use; | |
3176 | } | |
3177 | ||
3178 | int bdrv_in_use(BlockDriverState *bs) | |
3179 | { | |
3180 | return bs->in_use; | |
3181 | } | |
3182 | ||
a597e79c CH |
3183 | void |
3184 | bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, | |
3185 | enum BlockAcctType type) | |
3186 | { | |
3187 | assert(type < BDRV_MAX_IOTYPE); | |
3188 | ||
3189 | cookie->bytes = bytes; | |
c488c7f6 | 3190 | cookie->start_time_ns = get_clock(); |
a597e79c CH |
3191 | cookie->type = type; |
3192 | } | |
3193 | ||
3194 | void | |
3195 | bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) | |
3196 | { | |
3197 | assert(cookie->type < BDRV_MAX_IOTYPE); | |
3198 | ||
3199 | bs->nr_bytes[cookie->type] += cookie->bytes; | |
3200 | bs->nr_ops[cookie->type]++; | |
c488c7f6 | 3201 | bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; |
a597e79c CH |
3202 | } |
3203 | ||
f88e1a42 JS |
3204 | int bdrv_img_create(const char *filename, const char *fmt, |
3205 | const char *base_filename, const char *base_fmt, | |
3206 | char *options, uint64_t img_size, int flags) | |
3207 | { | |
3208 | QEMUOptionParameter *param = NULL, *create_options = NULL; | |
d220894e | 3209 | QEMUOptionParameter *backing_fmt, *backing_file, *size; |
f88e1a42 JS |
3210 | BlockDriverState *bs = NULL; |
3211 | BlockDriver *drv, *proto_drv; | |
96df67d1 | 3212 | BlockDriver *backing_drv = NULL; |
f88e1a42 JS |
3213 | int ret = 0; |
3214 | ||
3215 | /* Find driver and parse its options */ | |
3216 | drv = bdrv_find_format(fmt); | |
3217 | if (!drv) { | |
3218 | error_report("Unknown file format '%s'", fmt); | |
4f70f249 | 3219 | ret = -EINVAL; |
f88e1a42 JS |
3220 | goto out; |
3221 | } | |
3222 | ||
3223 | proto_drv = bdrv_find_protocol(filename); | |
3224 | if (!proto_drv) { | |
3225 | error_report("Unknown protocol '%s'", filename); | |
4f70f249 | 3226 | ret = -EINVAL; |
f88e1a42 JS |
3227 | goto out; |
3228 | } | |
3229 | ||
3230 | create_options = append_option_parameters(create_options, | |
3231 | drv->create_options); | |
3232 | create_options = append_option_parameters(create_options, | |
3233 | proto_drv->create_options); | |
3234 | ||
3235 | /* Create parameter list with default values */ | |
3236 | param = parse_option_parameters("", create_options, param); | |
3237 | ||
3238 | set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); | |
3239 | ||
3240 | /* Parse -o options */ | |
3241 | if (options) { | |
3242 | param = parse_option_parameters(options, create_options, param); | |
3243 | if (param == NULL) { | |
3244 | error_report("Invalid options for file format '%s'.", fmt); | |
4f70f249 | 3245 | ret = -EINVAL; |
f88e1a42 JS |
3246 | goto out; |
3247 | } | |
3248 | } | |
3249 | ||
3250 | if (base_filename) { | |
3251 | if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, | |
3252 | base_filename)) { | |
3253 | error_report("Backing file not supported for file format '%s'", | |
3254 | fmt); | |
4f70f249 | 3255 | ret = -EINVAL; |
f88e1a42 JS |
3256 | goto out; |
3257 | } | |
3258 | } | |
3259 | ||
3260 | if (base_fmt) { | |
3261 | if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { | |
3262 | error_report("Backing file format not supported for file " | |
3263 | "format '%s'", fmt); | |
4f70f249 | 3264 | ret = -EINVAL; |
f88e1a42 JS |
3265 | goto out; |
3266 | } | |
3267 | } | |
3268 | ||
792da93a JS |
3269 | backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); |
3270 | if (backing_file && backing_file->value.s) { | |
3271 | if (!strcmp(filename, backing_file->value.s)) { | |
3272 | error_report("Error: Trying to create an image with the " | |
3273 | "same filename as the backing file"); | |
4f70f249 | 3274 | ret = -EINVAL; |
792da93a JS |
3275 | goto out; |
3276 | } | |
3277 | } | |
3278 | ||
f88e1a42 JS |
3279 | backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); |
3280 | if (backing_fmt && backing_fmt->value.s) { | |
96df67d1 SH |
3281 | backing_drv = bdrv_find_format(backing_fmt->value.s); |
3282 | if (!backing_drv) { | |
f88e1a42 JS |
3283 | error_report("Unknown backing file format '%s'", |
3284 | backing_fmt->value.s); | |
4f70f249 | 3285 | ret = -EINVAL; |
f88e1a42 JS |
3286 | goto out; |
3287 | } | |
3288 | } | |
3289 | ||
3290 | // The size for the image must always be specified, with one exception: | |
3291 | // If we are using a backing file, we can obtain the size from there | |
d220894e KW |
3292 | size = get_option_parameter(param, BLOCK_OPT_SIZE); |
3293 | if (size && size->value.n == -1) { | |
f88e1a42 JS |
3294 | if (backing_file && backing_file->value.s) { |
3295 | uint64_t size; | |
f88e1a42 JS |
3296 | char buf[32]; |
3297 | ||
f88e1a42 JS |
3298 | bs = bdrv_new(""); |
3299 | ||
96df67d1 | 3300 | ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv); |
f88e1a42 | 3301 | if (ret < 0) { |
96df67d1 | 3302 | error_report("Could not open '%s'", backing_file->value.s); |
f88e1a42 JS |
3303 | goto out; |
3304 | } | |
3305 | bdrv_get_geometry(bs, &size); | |
3306 | size *= 512; | |
3307 | ||
3308 | snprintf(buf, sizeof(buf), "%" PRId64, size); | |
3309 | set_option_parameter(param, BLOCK_OPT_SIZE, buf); | |
3310 | } else { | |
3311 | error_report("Image creation needs a size parameter"); | |
4f70f249 | 3312 | ret = -EINVAL; |
f88e1a42 JS |
3313 | goto out; |
3314 | } | |
3315 | } | |
3316 | ||
3317 | printf("Formatting '%s', fmt=%s ", filename, fmt); | |
3318 | print_option_parameters(param); | |
3319 | puts(""); | |
3320 | ||
3321 | ret = bdrv_create(drv, filename, param); | |
3322 | ||
3323 | if (ret < 0) { | |
3324 | if (ret == -ENOTSUP) { | |
3325 | error_report("Formatting or formatting option not supported for " | |
3326 | "file format '%s'", fmt); | |
3327 | } else if (ret == -EFBIG) { | |
3328 | error_report("The image size is too large for file format '%s'", | |
3329 | fmt); | |
3330 | } else { | |
3331 | error_report("%s: error while creating %s: %s", filename, fmt, | |
3332 | strerror(-ret)); | |
3333 | } | |
3334 | } | |
3335 | ||
3336 | out: | |
3337 | free_option_parameters(create_options); | |
3338 | free_option_parameters(param); | |
3339 | ||
3340 | if (bs) { | |
3341 | bdrv_delete(bs); | |
3342 | } | |
4f70f249 JS |
3343 | |
3344 | return ret; | |
f88e1a42 | 3345 | } |