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