]>
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" |
376253ec | 26 | #include "monitor.h" |
ea2384d3 | 27 | #include "block_int.h" |
5efa9d5a | 28 | #include "module.h" |
d15e5465 | 29 | #include "qemu-objects.h" |
fc01f7e7 | 30 | |
71e72a19 | 31 | #ifdef CONFIG_BSD |
7674e7bf FB |
32 | #include <sys/types.h> |
33 | #include <sys/stat.h> | |
34 | #include <sys/ioctl.h> | |
72cf2d4f | 35 | #include <sys/queue.h> |
c5e97233 | 36 | #ifndef __DragonFly__ |
7674e7bf FB |
37 | #include <sys/disk.h> |
38 | #endif | |
c5e97233 | 39 | #endif |
7674e7bf | 40 | |
49dc768d AL |
41 | #ifdef _WIN32 |
42 | #include <windows.h> | |
43 | #endif | |
44 | ||
f141eafe AL |
45 | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, |
46 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
c87c0672 | 47 | BlockDriverCompletionFunc *cb, void *opaque); |
f141eafe AL |
48 | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, |
49 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
ce1a14dc | 50 | BlockDriverCompletionFunc *cb, void *opaque); |
b2e12bc6 CH |
51 | static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, |
52 | BlockDriverCompletionFunc *cb, void *opaque); | |
016f5cf6 AG |
53 | static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs, |
54 | BlockDriverCompletionFunc *cb, void *opaque); | |
5fafdf24 | 55 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
83f64091 FB |
56 | uint8_t *buf, int nb_sectors); |
57 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, | |
58 | const uint8_t *buf, int nb_sectors); | |
ec530c81 | 59 | |
1b7bdbc1 SH |
60 | static QTAILQ_HEAD(, BlockDriverState) bdrv_states = |
61 | QTAILQ_HEAD_INITIALIZER(bdrv_states); | |
7ee930d0 | 62 | |
8a22f02a SH |
63 | static QLIST_HEAD(, BlockDriver) bdrv_drivers = |
64 | QLIST_HEAD_INITIALIZER(bdrv_drivers); | |
ea2384d3 | 65 | |
eb852011 MA |
66 | /* If non-zero, use only whitelisted block drivers */ |
67 | static int use_bdrv_whitelist; | |
68 | ||
83f64091 | 69 | int path_is_absolute(const char *path) |
3b0d4f61 | 70 | { |
83f64091 | 71 | const char *p; |
21664424 FB |
72 | #ifdef _WIN32 |
73 | /* specific case for names like: "\\.\d:" */ | |
74 | if (*path == '/' || *path == '\\') | |
75 | return 1; | |
76 | #endif | |
83f64091 FB |
77 | p = strchr(path, ':'); |
78 | if (p) | |
79 | p++; | |
80 | else | |
81 | p = path; | |
3b9f94e1 FB |
82 | #ifdef _WIN32 |
83 | return (*p == '/' || *p == '\\'); | |
84 | #else | |
85 | return (*p == '/'); | |
86 | #endif | |
3b0d4f61 FB |
87 | } |
88 | ||
83f64091 FB |
89 | /* if filename is absolute, just copy it to dest. Otherwise, build a |
90 | path to it by considering it is relative to base_path. URL are | |
91 | supported. */ | |
92 | void path_combine(char *dest, int dest_size, | |
93 | const char *base_path, | |
94 | const char *filename) | |
3b0d4f61 | 95 | { |
83f64091 FB |
96 | const char *p, *p1; |
97 | int len; | |
98 | ||
99 | if (dest_size <= 0) | |
100 | return; | |
101 | if (path_is_absolute(filename)) { | |
102 | pstrcpy(dest, dest_size, filename); | |
103 | } else { | |
104 | p = strchr(base_path, ':'); | |
105 | if (p) | |
106 | p++; | |
107 | else | |
108 | p = base_path; | |
3b9f94e1 FB |
109 | p1 = strrchr(base_path, '/'); |
110 | #ifdef _WIN32 | |
111 | { | |
112 | const char *p2; | |
113 | p2 = strrchr(base_path, '\\'); | |
114 | if (!p1 || p2 > p1) | |
115 | p1 = p2; | |
116 | } | |
117 | #endif | |
83f64091 FB |
118 | if (p1) |
119 | p1++; | |
120 | else | |
121 | p1 = base_path; | |
122 | if (p1 > p) | |
123 | p = p1; | |
124 | len = p - base_path; | |
125 | if (len > dest_size - 1) | |
126 | len = dest_size - 1; | |
127 | memcpy(dest, base_path, len); | |
128 | dest[len] = '\0'; | |
129 | pstrcat(dest, dest_size, filename); | |
3b0d4f61 | 130 | } |
3b0d4f61 FB |
131 | } |
132 | ||
5efa9d5a | 133 | void bdrv_register(BlockDriver *bdrv) |
ea2384d3 | 134 | { |
f141eafe | 135 | if (!bdrv->bdrv_aio_readv) { |
83f64091 | 136 | /* add AIO emulation layer */ |
f141eafe AL |
137 | bdrv->bdrv_aio_readv = bdrv_aio_readv_em; |
138 | bdrv->bdrv_aio_writev = bdrv_aio_writev_em; | |
eda578e5 | 139 | } else if (!bdrv->bdrv_read) { |
83f64091 FB |
140 | /* add synchronous IO emulation layer */ |
141 | bdrv->bdrv_read = bdrv_read_em; | |
142 | bdrv->bdrv_write = bdrv_write_em; | |
143 | } | |
b2e12bc6 CH |
144 | |
145 | if (!bdrv->bdrv_aio_flush) | |
146 | bdrv->bdrv_aio_flush = bdrv_aio_flush_em; | |
147 | ||
8a22f02a | 148 | QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); |
ea2384d3 | 149 | } |
b338082b FB |
150 | |
151 | /* create a new block device (by default it is empty) */ | |
152 | BlockDriverState *bdrv_new(const char *device_name) | |
153 | { | |
1b7bdbc1 | 154 | BlockDriverState *bs; |
b338082b FB |
155 | |
156 | bs = qemu_mallocz(sizeof(BlockDriverState)); | |
b338082b | 157 | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); |
ea2384d3 | 158 | if (device_name[0] != '\0') { |
1b7bdbc1 | 159 | QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); |
ea2384d3 | 160 | } |
b338082b FB |
161 | return bs; |
162 | } | |
163 | ||
ea2384d3 FB |
164 | BlockDriver *bdrv_find_format(const char *format_name) |
165 | { | |
166 | BlockDriver *drv1; | |
8a22f02a SH |
167 | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
168 | if (!strcmp(drv1->format_name, format_name)) { | |
ea2384d3 | 169 | return drv1; |
8a22f02a | 170 | } |
ea2384d3 FB |
171 | } |
172 | return NULL; | |
173 | } | |
174 | ||
eb852011 MA |
175 | static int bdrv_is_whitelisted(BlockDriver *drv) |
176 | { | |
177 | static const char *whitelist[] = { | |
178 | CONFIG_BDRV_WHITELIST | |
179 | }; | |
180 | const char **p; | |
181 | ||
182 | if (!whitelist[0]) | |
183 | return 1; /* no whitelist, anything goes */ | |
184 | ||
185 | for (p = whitelist; *p; p++) { | |
186 | if (!strcmp(drv->format_name, *p)) { | |
187 | return 1; | |
188 | } | |
189 | } | |
190 | return 0; | |
191 | } | |
192 | ||
193 | BlockDriver *bdrv_find_whitelisted_format(const char *format_name) | |
194 | { | |
195 | BlockDriver *drv = bdrv_find_format(format_name); | |
196 | return drv && bdrv_is_whitelisted(drv) ? drv : NULL; | |
197 | } | |
198 | ||
0e7e1989 KW |
199 | int bdrv_create(BlockDriver *drv, const char* filename, |
200 | QEMUOptionParameter *options) | |
ea2384d3 FB |
201 | { |
202 | if (!drv->bdrv_create) | |
203 | return -ENOTSUP; | |
0e7e1989 KW |
204 | |
205 | return drv->bdrv_create(filename, options); | |
ea2384d3 FB |
206 | } |
207 | ||
84a12e66 CH |
208 | int bdrv_create_file(const char* filename, QEMUOptionParameter *options) |
209 | { | |
210 | BlockDriver *drv; | |
211 | ||
b50cbabc | 212 | drv = bdrv_find_protocol(filename); |
84a12e66 CH |
213 | if (drv == NULL) { |
214 | drv = bdrv_find_format("file"); | |
215 | } | |
216 | ||
217 | return bdrv_create(drv, filename, options); | |
218 | } | |
219 | ||
d5249393 | 220 | #ifdef _WIN32 |
95389c86 | 221 | void get_tmp_filename(char *filename, int size) |
d5249393 | 222 | { |
3b9f94e1 | 223 | char temp_dir[MAX_PATH]; |
3b46e624 | 224 | |
3b9f94e1 FB |
225 | GetTempPath(MAX_PATH, temp_dir); |
226 | GetTempFileName(temp_dir, "qem", 0, filename); | |
d5249393 FB |
227 | } |
228 | #else | |
95389c86 | 229 | void get_tmp_filename(char *filename, int size) |
fc01f7e7 | 230 | { |
67b915a5 | 231 | int fd; |
7ccfb2eb | 232 | const char *tmpdir; |
d5249393 | 233 | /* XXX: race condition possible */ |
0badc1ee AJ |
234 | tmpdir = getenv("TMPDIR"); |
235 | if (!tmpdir) | |
236 | tmpdir = "/tmp"; | |
237 | snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); | |
ea2384d3 FB |
238 | fd = mkstemp(filename); |
239 | close(fd); | |
240 | } | |
d5249393 | 241 | #endif |
fc01f7e7 | 242 | |
19cb3738 | 243 | #ifdef _WIN32 |
f45512fe FB |
244 | static int is_windows_drive_prefix(const char *filename) |
245 | { | |
246 | return (((filename[0] >= 'a' && filename[0] <= 'z') || | |
247 | (filename[0] >= 'A' && filename[0] <= 'Z')) && | |
248 | filename[1] == ':'); | |
249 | } | |
3b46e624 | 250 | |
508c7cb3 | 251 | int is_windows_drive(const char *filename) |
19cb3738 | 252 | { |
5fafdf24 | 253 | if (is_windows_drive_prefix(filename) && |
f45512fe | 254 | filename[2] == '\0') |
19cb3738 FB |
255 | return 1; |
256 | if (strstart(filename, "\\\\.\\", NULL) || | |
257 | strstart(filename, "//./", NULL)) | |
258 | return 1; | |
259 | return 0; | |
260 | } | |
261 | #endif | |
262 | ||
84a12e66 CH |
263 | /* |
264 | * Detect host devices. By convention, /dev/cdrom[N] is always | |
265 | * recognized as a host CDROM. | |
266 | */ | |
267 | static BlockDriver *find_hdev_driver(const char *filename) | |
268 | { | |
269 | int score_max = 0, score; | |
270 | BlockDriver *drv = NULL, *d; | |
271 | ||
272 | QLIST_FOREACH(d, &bdrv_drivers, list) { | |
273 | if (d->bdrv_probe_device) { | |
274 | score = d->bdrv_probe_device(filename); | |
275 | if (score > score_max) { | |
276 | score_max = score; | |
277 | drv = d; | |
278 | } | |
279 | } | |
280 | } | |
281 | ||
282 | return drv; | |
283 | } | |
284 | ||
b50cbabc | 285 | BlockDriver *bdrv_find_protocol(const char *filename) |
83f64091 FB |
286 | { |
287 | BlockDriver *drv1; | |
288 | char protocol[128]; | |
1cec71e3 | 289 | int len; |
83f64091 | 290 | const char *p; |
20993081 | 291 | int is_drive; |
19cb3738 | 292 | |
66f82cee KW |
293 | /* TODO Drivers without bdrv_file_open must be specified explicitly */ |
294 | ||
19cb3738 | 295 | #ifdef _WIN32 |
20993081 KW |
296 | is_drive = is_windows_drive(filename) || |
297 | is_windows_drive_prefix(filename); | |
298 | #else | |
299 | is_drive = 0; | |
19cb3738 | 300 | #endif |
1cec71e3 | 301 | p = strchr(filename, ':'); |
20993081 | 302 | if (!p || is_drive) { |
84a12e66 CH |
303 | drv1 = find_hdev_driver(filename); |
304 | if (!drv1) { | |
305 | drv1 = bdrv_find_format("file"); | |
306 | } | |
307 | return drv1; | |
308 | } | |
1cec71e3 AL |
309 | len = p - filename; |
310 | if (len > sizeof(protocol) - 1) | |
311 | len = sizeof(protocol) - 1; | |
312 | memcpy(protocol, filename, len); | |
313 | protocol[len] = '\0'; | |
8a22f02a | 314 | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
5fafdf24 | 315 | if (drv1->protocol_name && |
8a22f02a | 316 | !strcmp(drv1->protocol_name, protocol)) { |
83f64091 | 317 | return drv1; |
8a22f02a | 318 | } |
83f64091 FB |
319 | } |
320 | return NULL; | |
321 | } | |
322 | ||
f3a5d3f8 CH |
323 | static BlockDriver *find_image_format(const char *filename) |
324 | { | |
325 | int ret, score, score_max; | |
326 | BlockDriver *drv1, *drv; | |
327 | uint8_t buf[2048]; | |
328 | BlockDriverState *bs; | |
329 | ||
f5edb014 | 330 | ret = bdrv_file_open(&bs, filename, 0); |
83f64091 FB |
331 | if (ret < 0) |
332 | return NULL; | |
f8ea0b00 | 333 | |
08a00559 KW |
334 | /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ |
335 | if (bs->sg || !bdrv_is_inserted(bs)) { | |
1a396859 | 336 | bdrv_delete(bs); |
f8ea0b00 | 337 | return bdrv_find_format("raw"); |
1a396859 | 338 | } |
f8ea0b00 | 339 | |
83f64091 FB |
340 | ret = bdrv_pread(bs, 0, buf, sizeof(buf)); |
341 | bdrv_delete(bs); | |
342 | if (ret < 0) { | |
343 | return NULL; | |
344 | } | |
345 | ||
ea2384d3 | 346 | score_max = 0; |
84a12e66 | 347 | drv = NULL; |
8a22f02a | 348 | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
83f64091 FB |
349 | if (drv1->bdrv_probe) { |
350 | score = drv1->bdrv_probe(buf, ret, filename); | |
351 | if (score > score_max) { | |
352 | score_max = score; | |
353 | drv = drv1; | |
354 | } | |
0849bf08 | 355 | } |
fc01f7e7 | 356 | } |
ea2384d3 FB |
357 | return drv; |
358 | } | |
359 | ||
51762288 SH |
360 | /** |
361 | * Set the current 'total_sectors' value | |
362 | */ | |
363 | static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) | |
364 | { | |
365 | BlockDriver *drv = bs->drv; | |
366 | ||
396759ad NB |
367 | /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ |
368 | if (bs->sg) | |
369 | return 0; | |
370 | ||
51762288 SH |
371 | /* query actual device if possible, otherwise just trust the hint */ |
372 | if (drv->bdrv_getlength) { | |
373 | int64_t length = drv->bdrv_getlength(bs); | |
374 | if (length < 0) { | |
375 | return length; | |
376 | } | |
377 | hint = length >> BDRV_SECTOR_BITS; | |
378 | } | |
379 | ||
380 | bs->total_sectors = hint; | |
381 | return 0; | |
382 | } | |
383 | ||
57915332 KW |
384 | /* |
385 | * Common part for opening disk images and files | |
386 | */ | |
387 | static int bdrv_open_common(BlockDriverState *bs, const char *filename, | |
388 | int flags, BlockDriver *drv) | |
389 | { | |
390 | int ret, open_flags; | |
391 | ||
392 | assert(drv != NULL); | |
393 | ||
66f82cee | 394 | bs->file = NULL; |
51762288 | 395 | bs->total_sectors = 0; |
57915332 KW |
396 | bs->is_temporary = 0; |
397 | bs->encrypted = 0; | |
398 | bs->valid_key = 0; | |
399 | bs->open_flags = flags; | |
400 | /* buffer_alignment defaulted to 512, drivers can change this value */ | |
401 | bs->buffer_alignment = 512; | |
402 | ||
403 | pstrcpy(bs->filename, sizeof(bs->filename), filename); | |
404 | ||
405 | if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) { | |
406 | return -ENOTSUP; | |
407 | } | |
408 | ||
409 | bs->drv = drv; | |
410 | bs->opaque = qemu_mallocz(drv->instance_size); | |
411 | ||
412 | /* | |
413 | * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a | |
414 | * write cache to the guest. We do need the fdatasync to flush | |
415 | * out transactions for block allocations, and we maybe have a | |
416 | * volatile write cache in our backing device to deal with. | |
417 | */ | |
418 | if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE)) | |
419 | bs->enable_write_cache = 1; | |
420 | ||
421 | /* | |
422 | * Clear flags that are internal to the block layer before opening the | |
423 | * image. | |
424 | */ | |
425 | open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); | |
426 | ||
427 | /* | |
428 | * Snapshots should be writeable. | |
429 | */ | |
430 | if (bs->is_temporary) { | |
431 | open_flags |= BDRV_O_RDWR; | |
432 | } | |
433 | ||
66f82cee KW |
434 | /* Open the image, either directly or using a protocol */ |
435 | if (drv->bdrv_file_open) { | |
436 | ret = drv->bdrv_file_open(bs, filename, open_flags); | |
437 | } else { | |
438 | ret = bdrv_file_open(&bs->file, filename, open_flags); | |
439 | if (ret >= 0) { | |
440 | ret = drv->bdrv_open(bs, open_flags); | |
441 | } | |
442 | } | |
443 | ||
57915332 KW |
444 | if (ret < 0) { |
445 | goto free_and_fail; | |
446 | } | |
447 | ||
448 | bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR); | |
51762288 SH |
449 | |
450 | ret = refresh_total_sectors(bs, bs->total_sectors); | |
451 | if (ret < 0) { | |
452 | goto free_and_fail; | |
57915332 | 453 | } |
51762288 | 454 | |
57915332 KW |
455 | #ifndef _WIN32 |
456 | if (bs->is_temporary) { | |
457 | unlink(filename); | |
458 | } | |
459 | #endif | |
460 | return 0; | |
461 | ||
462 | free_and_fail: | |
66f82cee KW |
463 | if (bs->file) { |
464 | bdrv_delete(bs->file); | |
465 | bs->file = NULL; | |
466 | } | |
57915332 KW |
467 | qemu_free(bs->opaque); |
468 | bs->opaque = NULL; | |
469 | bs->drv = NULL; | |
470 | return ret; | |
471 | } | |
472 | ||
b6ce07aa KW |
473 | /* |
474 | * Opens a file using a protocol (file, host_device, nbd, ...) | |
475 | */ | |
83f64091 | 476 | int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) |
ea2384d3 | 477 | { |
83f64091 | 478 | BlockDriverState *bs; |
6db95603 | 479 | BlockDriver *drv; |
83f64091 FB |
480 | int ret; |
481 | ||
b50cbabc | 482 | drv = bdrv_find_protocol(filename); |
6db95603 CH |
483 | if (!drv) { |
484 | return -ENOENT; | |
485 | } | |
486 | ||
83f64091 | 487 | bs = bdrv_new(""); |
b6ce07aa | 488 | ret = bdrv_open_common(bs, filename, flags, drv); |
83f64091 FB |
489 | if (ret < 0) { |
490 | bdrv_delete(bs); | |
491 | return ret; | |
3b0d4f61 | 492 | } |
71d0770c | 493 | bs->growable = 1; |
83f64091 FB |
494 | *pbs = bs; |
495 | return 0; | |
496 | } | |
497 | ||
b6ce07aa KW |
498 | /* |
499 | * Opens a disk image (raw, qcow2, vmdk, ...) | |
500 | */ | |
d6e9098e KW |
501 | int bdrv_open(BlockDriverState *bs, const char *filename, int flags, |
502 | BlockDriver *drv) | |
ea2384d3 | 503 | { |
b6ce07aa | 504 | int ret; |
712e7874 | 505 | |
83f64091 | 506 | if (flags & BDRV_O_SNAPSHOT) { |
ea2384d3 FB |
507 | BlockDriverState *bs1; |
508 | int64_t total_size; | |
7c96d46e | 509 | int is_protocol = 0; |
91a073a9 KW |
510 | BlockDriver *bdrv_qcow2; |
511 | QEMUOptionParameter *options; | |
b6ce07aa KW |
512 | char tmp_filename[PATH_MAX]; |
513 | char backing_filename[PATH_MAX]; | |
3b46e624 | 514 | |
ea2384d3 FB |
515 | /* if snapshot, we create a temporary backing file and open it |
516 | instead of opening 'filename' directly */ | |
33e3963e | 517 | |
ea2384d3 FB |
518 | /* if there is a backing file, use it */ |
519 | bs1 = bdrv_new(""); | |
d6e9098e | 520 | ret = bdrv_open(bs1, filename, 0, drv); |
51d7c00c | 521 | if (ret < 0) { |
ea2384d3 | 522 | bdrv_delete(bs1); |
51d7c00c | 523 | return ret; |
ea2384d3 | 524 | } |
3e82990b | 525 | total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; |
7c96d46e AL |
526 | |
527 | if (bs1->drv && bs1->drv->protocol_name) | |
528 | is_protocol = 1; | |
529 | ||
ea2384d3 | 530 | bdrv_delete(bs1); |
3b46e624 | 531 | |
ea2384d3 | 532 | get_tmp_filename(tmp_filename, sizeof(tmp_filename)); |
7c96d46e AL |
533 | |
534 | /* Real path is meaningless for protocols */ | |
535 | if (is_protocol) | |
536 | snprintf(backing_filename, sizeof(backing_filename), | |
537 | "%s", filename); | |
114cdfa9 KS |
538 | else if (!realpath(filename, backing_filename)) |
539 | return -errno; | |
7c96d46e | 540 | |
91a073a9 KW |
541 | bdrv_qcow2 = bdrv_find_format("qcow2"); |
542 | options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); | |
543 | ||
3e82990b | 544 | set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size); |
91a073a9 KW |
545 | set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); |
546 | if (drv) { | |
547 | set_option_parameter(options, BLOCK_OPT_BACKING_FMT, | |
548 | drv->format_name); | |
549 | } | |
550 | ||
551 | ret = bdrv_create(bdrv_qcow2, tmp_filename, options); | |
d748768c | 552 | free_option_parameters(options); |
51d7c00c AL |
553 | if (ret < 0) { |
554 | return ret; | |
ea2384d3 | 555 | } |
91a073a9 | 556 | |
ea2384d3 | 557 | filename = tmp_filename; |
91a073a9 | 558 | drv = bdrv_qcow2; |
ea2384d3 FB |
559 | bs->is_temporary = 1; |
560 | } | |
712e7874 | 561 | |
b6ce07aa | 562 | /* Find the right image format driver */ |
6db95603 | 563 | if (!drv) { |
84a12e66 | 564 | drv = find_image_format(filename); |
51d7c00c | 565 | } |
6987307c | 566 | |
51d7c00c AL |
567 | if (!drv) { |
568 | ret = -ENOENT; | |
569 | goto unlink_and_fail; | |
ea2384d3 | 570 | } |
b6ce07aa KW |
571 | |
572 | /* Open the image */ | |
573 | ret = bdrv_open_common(bs, filename, flags, drv); | |
574 | if (ret < 0) { | |
6987307c CH |
575 | goto unlink_and_fail; |
576 | } | |
577 | ||
b6ce07aa KW |
578 | /* If there is a backing file, use it */ |
579 | if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') { | |
580 | char backing_filename[PATH_MAX]; | |
581 | int back_flags; | |
582 | BlockDriver *back_drv = NULL; | |
583 | ||
584 | bs->backing_hd = bdrv_new(""); | |
585 | path_combine(backing_filename, sizeof(backing_filename), | |
586 | filename, bs->backing_file); | |
587 | if (bs->backing_format[0] != '\0') | |
588 | back_drv = bdrv_find_format(bs->backing_format); | |
589 | ||
590 | /* backing files always opened read-only */ | |
591 | back_flags = | |
592 | flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); | |
593 | ||
594 | ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv); | |
595 | if (ret < 0) { | |
596 | bdrv_close(bs); | |
597 | return ret; | |
598 | } | |
599 | if (bs->is_temporary) { | |
600 | bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR); | |
601 | } else { | |
602 | /* base image inherits from "parent" */ | |
603 | bs->backing_hd->keep_read_only = bs->keep_read_only; | |
604 | } | |
605 | } | |
606 | ||
607 | if (!bdrv_key_required(bs)) { | |
608 | /* call the change callback */ | |
609 | bs->media_changed = 1; | |
610 | if (bs->change_cb) | |
611 | bs->change_cb(bs->change_opaque); | |
612 | } | |
613 | ||
614 | return 0; | |
615 | ||
616 | unlink_and_fail: | |
617 | if (bs->is_temporary) { | |
618 | unlink(filename); | |
619 | } | |
620 | return ret; | |
621 | } | |
622 | ||
fc01f7e7 FB |
623 | void bdrv_close(BlockDriverState *bs) |
624 | { | |
19cb3738 | 625 | if (bs->drv) { |
557df6ac | 626 | if (bs->backing_hd) { |
ea2384d3 | 627 | bdrv_delete(bs->backing_hd); |
557df6ac SH |
628 | bs->backing_hd = NULL; |
629 | } | |
ea2384d3 FB |
630 | bs->drv->bdrv_close(bs); |
631 | qemu_free(bs->opaque); | |
632 | #ifdef _WIN32 | |
633 | if (bs->is_temporary) { | |
634 | unlink(bs->filename); | |
635 | } | |
67b915a5 | 636 | #endif |
ea2384d3 FB |
637 | bs->opaque = NULL; |
638 | bs->drv = NULL; | |
b338082b | 639 | |
66f82cee KW |
640 | if (bs->file != NULL) { |
641 | bdrv_close(bs->file); | |
642 | } | |
643 | ||
b338082b | 644 | /* call the change callback */ |
19cb3738 | 645 | bs->media_changed = 1; |
b338082b FB |
646 | if (bs->change_cb) |
647 | bs->change_cb(bs->change_opaque); | |
648 | } | |
649 | } | |
650 | ||
2bc93fed MK |
651 | void bdrv_close_all(void) |
652 | { | |
653 | BlockDriverState *bs; | |
654 | ||
655 | QTAILQ_FOREACH(bs, &bdrv_states, list) { | |
656 | bdrv_close(bs); | |
657 | } | |
658 | } | |
659 | ||
b338082b FB |
660 | void bdrv_delete(BlockDriverState *bs) |
661 | { | |
1b7bdbc1 SH |
662 | /* remove from list, if necessary */ |
663 | if (bs->device_name[0] != '\0') { | |
664 | QTAILQ_REMOVE(&bdrv_states, bs, list); | |
665 | } | |
34c6f050 | 666 | |
b338082b | 667 | bdrv_close(bs); |
66f82cee KW |
668 | if (bs->file != NULL) { |
669 | bdrv_delete(bs->file); | |
670 | } | |
671 | ||
b338082b | 672 | qemu_free(bs); |
fc01f7e7 FB |
673 | } |
674 | ||
e97fc193 AL |
675 | /* |
676 | * Run consistency checks on an image | |
677 | * | |
678 | * Returns the number of errors or -errno when an internal error occurs | |
679 | */ | |
680 | int bdrv_check(BlockDriverState *bs) | |
681 | { | |
682 | if (bs->drv->bdrv_check == NULL) { | |
683 | return -ENOTSUP; | |
684 | } | |
685 | ||
686 | return bs->drv->bdrv_check(bs); | |
687 | } | |
688 | ||
33e3963e FB |
689 | /* commit COW file into the raw image */ |
690 | int bdrv_commit(BlockDriverState *bs) | |
691 | { | |
19cb3738 | 692 | BlockDriver *drv = bs->drv; |
83f64091 | 693 | int64_t i, total_sectors; |
4dca4b63 NS |
694 | int n, j, ro, open_flags; |
695 | int ret = 0, rw_ret = 0; | |
eb5a3165 | 696 | unsigned char sector[BDRV_SECTOR_SIZE]; |
4dca4b63 NS |
697 | char filename[1024]; |
698 | BlockDriverState *bs_rw, *bs_ro; | |
33e3963e | 699 | |
19cb3738 FB |
700 | if (!drv) |
701 | return -ENOMEDIUM; | |
4dca4b63 NS |
702 | |
703 | if (!bs->backing_hd) { | |
704 | return -ENOTSUP; | |
33e3963e FB |
705 | } |
706 | ||
4dca4b63 NS |
707 | if (bs->backing_hd->keep_read_only) { |
708 | return -EACCES; | |
709 | } | |
710 | ||
711 | ro = bs->backing_hd->read_only; | |
712 | strncpy(filename, bs->backing_hd->filename, sizeof(filename)); | |
713 | open_flags = bs->backing_hd->open_flags; | |
714 | ||
715 | if (ro) { | |
716 | /* re-open as RW */ | |
717 | bdrv_delete(bs->backing_hd); | |
718 | bs->backing_hd = NULL; | |
719 | bs_rw = bdrv_new(""); | |
c3349197 | 720 | rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, drv); |
4dca4b63 NS |
721 | if (rw_ret < 0) { |
722 | bdrv_delete(bs_rw); | |
723 | /* try to re-open read-only */ | |
724 | bs_ro = bdrv_new(""); | |
c3349197 | 725 | ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv); |
4dca4b63 NS |
726 | if (ret < 0) { |
727 | bdrv_delete(bs_ro); | |
728 | /* drive not functional anymore */ | |
729 | bs->drv = NULL; | |
730 | return ret; | |
731 | } | |
732 | bs->backing_hd = bs_ro; | |
733 | return rw_ret; | |
734 | } | |
735 | bs->backing_hd = bs_rw; | |
ea2384d3 | 736 | } |
33e3963e | 737 | |
6ea44308 | 738 | total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; |
83f64091 | 739 | for (i = 0; i < total_sectors;) { |
19cb3738 | 740 | if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { |
ea2384d3 FB |
741 | for(j = 0; j < n; j++) { |
742 | if (bdrv_read(bs, i, sector, 1) != 0) { | |
4dca4b63 NS |
743 | ret = -EIO; |
744 | goto ro_cleanup; | |
ea2384d3 FB |
745 | } |
746 | ||
747 | if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { | |
4dca4b63 NS |
748 | ret = -EIO; |
749 | goto ro_cleanup; | |
ea2384d3 FB |
750 | } |
751 | i++; | |
33e3963e | 752 | } |
ea2384d3 FB |
753 | } else { |
754 | i += n; | |
755 | } | |
33e3963e | 756 | } |
95389c86 | 757 | |
1d44952f CH |
758 | if (drv->bdrv_make_empty) { |
759 | ret = drv->bdrv_make_empty(bs); | |
760 | bdrv_flush(bs); | |
761 | } | |
95389c86 | 762 | |
3f5075ae CH |
763 | /* |
764 | * Make sure all data we wrote to the backing device is actually | |
765 | * stable on disk. | |
766 | */ | |
767 | if (bs->backing_hd) | |
768 | bdrv_flush(bs->backing_hd); | |
4dca4b63 NS |
769 | |
770 | ro_cleanup: | |
771 | ||
772 | if (ro) { | |
773 | /* re-open as RO */ | |
774 | bdrv_delete(bs->backing_hd); | |
775 | bs->backing_hd = NULL; | |
776 | bs_ro = bdrv_new(""); | |
c3349197 | 777 | ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv); |
4dca4b63 NS |
778 | if (ret < 0) { |
779 | bdrv_delete(bs_ro); | |
780 | /* drive not functional anymore */ | |
781 | bs->drv = NULL; | |
782 | return ret; | |
783 | } | |
784 | bs->backing_hd = bs_ro; | |
785 | bs->backing_hd->keep_read_only = 0; | |
786 | } | |
787 | ||
1d44952f | 788 | return ret; |
33e3963e FB |
789 | } |
790 | ||
6ab4b5ab MA |
791 | void bdrv_commit_all(void) |
792 | { | |
793 | BlockDriverState *bs; | |
794 | ||
795 | QTAILQ_FOREACH(bs, &bdrv_states, list) { | |
796 | bdrv_commit(bs); | |
797 | } | |
798 | } | |
799 | ||
756e6736 KW |
800 | /* |
801 | * Return values: | |
802 | * 0 - success | |
803 | * -EINVAL - backing format specified, but no file | |
804 | * -ENOSPC - can't update the backing file because no space is left in the | |
805 | * image file header | |
806 | * -ENOTSUP - format driver doesn't support changing the backing file | |
807 | */ | |
808 | int bdrv_change_backing_file(BlockDriverState *bs, | |
809 | const char *backing_file, const char *backing_fmt) | |
810 | { | |
811 | BlockDriver *drv = bs->drv; | |
812 | ||
813 | if (drv->bdrv_change_backing_file != NULL) { | |
814 | return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); | |
815 | } else { | |
816 | return -ENOTSUP; | |
817 | } | |
818 | } | |
819 | ||
71d0770c AL |
820 | static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, |
821 | size_t size) | |
822 | { | |
823 | int64_t len; | |
824 | ||
825 | if (!bdrv_is_inserted(bs)) | |
826 | return -ENOMEDIUM; | |
827 | ||
828 | if (bs->growable) | |
829 | return 0; | |
830 | ||
831 | len = bdrv_getlength(bs); | |
832 | ||
fbb7b4e0 KW |
833 | if (offset < 0) |
834 | return -EIO; | |
835 | ||
836 | if ((offset > len) || (len - offset < size)) | |
71d0770c AL |
837 | return -EIO; |
838 | ||
839 | return 0; | |
840 | } | |
841 | ||
842 | static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, | |
843 | int nb_sectors) | |
844 | { | |
eb5a3165 JS |
845 | return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, |
846 | nb_sectors * BDRV_SECTOR_SIZE); | |
71d0770c AL |
847 | } |
848 | ||
19cb3738 | 849 | /* return < 0 if error. See bdrv_write() for the return codes */ |
5fafdf24 | 850 | int bdrv_read(BlockDriverState *bs, int64_t sector_num, |
fc01f7e7 FB |
851 | uint8_t *buf, int nb_sectors) |
852 | { | |
ea2384d3 FB |
853 | BlockDriver *drv = bs->drv; |
854 | ||
19cb3738 FB |
855 | if (!drv) |
856 | return -ENOMEDIUM; | |
71d0770c AL |
857 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
858 | return -EIO; | |
b338082b | 859 | |
eda578e5 | 860 | return drv->bdrv_read(bs, sector_num, buf, nb_sectors); |
fc01f7e7 FB |
861 | } |
862 | ||
7cd1e32a | 863 | static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, |
a55eb92c | 864 | int nb_sectors, int dirty) |
7cd1e32a LS |
865 | { |
866 | int64_t start, end; | |
c6d22830 | 867 | unsigned long val, idx, bit; |
a55eb92c | 868 | |
6ea44308 | 869 | start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; |
c6d22830 | 870 | end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK; |
a55eb92c JK |
871 | |
872 | for (; start <= end; start++) { | |
c6d22830 JK |
873 | idx = start / (sizeof(unsigned long) * 8); |
874 | bit = start % (sizeof(unsigned long) * 8); | |
875 | val = bs->dirty_bitmap[idx]; | |
876 | if (dirty) { | |
aaa0eb75 LS |
877 | if (!(val & (1 << bit))) { |
878 | bs->dirty_count++; | |
879 | val |= 1 << bit; | |
880 | } | |
c6d22830 | 881 | } else { |
aaa0eb75 LS |
882 | if (val & (1 << bit)) { |
883 | bs->dirty_count--; | |
884 | val &= ~(1 << bit); | |
885 | } | |
c6d22830 JK |
886 | } |
887 | bs->dirty_bitmap[idx] = val; | |
7cd1e32a LS |
888 | } |
889 | } | |
890 | ||
5fafdf24 | 891 | /* Return < 0 if error. Important errors are: |
19cb3738 FB |
892 | -EIO generic I/O error (may happen for all errors) |
893 | -ENOMEDIUM No media inserted. | |
894 | -EINVAL Invalid sector number or nb_sectors | |
895 | -EACCES Trying to write a read-only device | |
896 | */ | |
5fafdf24 | 897 | int bdrv_write(BlockDriverState *bs, int64_t sector_num, |
fc01f7e7 FB |
898 | const uint8_t *buf, int nb_sectors) |
899 | { | |
83f64091 | 900 | BlockDriver *drv = bs->drv; |
19cb3738 FB |
901 | if (!bs->drv) |
902 | return -ENOMEDIUM; | |
0849bf08 | 903 | if (bs->read_only) |
19cb3738 | 904 | return -EACCES; |
71d0770c AL |
905 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
906 | return -EIO; | |
a55eb92c | 907 | |
c6d22830 | 908 | if (bs->dirty_bitmap) { |
7cd1e32a LS |
909 | set_dirty_bitmap(bs, sector_num, nb_sectors, 1); |
910 | } | |
a55eb92c | 911 | |
294cc35f KW |
912 | if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { |
913 | bs->wr_highest_sector = sector_num + nb_sectors - 1; | |
914 | } | |
915 | ||
42fb2807 | 916 | return drv->bdrv_write(bs, sector_num, buf, nb_sectors); |
83f64091 FB |
917 | } |
918 | ||
eda578e5 AL |
919 | int bdrv_pread(BlockDriverState *bs, int64_t offset, |
920 | void *buf, int count1) | |
83f64091 | 921 | { |
6ea44308 | 922 | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
83f64091 FB |
923 | int len, nb_sectors, count; |
924 | int64_t sector_num; | |
9a8c4cce | 925 | int ret; |
83f64091 FB |
926 | |
927 | count = count1; | |
928 | /* first read to align to sector start */ | |
6ea44308 | 929 | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); |
83f64091 FB |
930 | if (len > count) |
931 | len = count; | |
6ea44308 | 932 | sector_num = offset >> BDRV_SECTOR_BITS; |
83f64091 | 933 | if (len > 0) { |
9a8c4cce KW |
934 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
935 | return ret; | |
6ea44308 | 936 | memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); |
83f64091 FB |
937 | count -= len; |
938 | if (count == 0) | |
939 | return count1; | |
940 | sector_num++; | |
941 | buf += len; | |
942 | } | |
943 | ||
944 | /* read the sectors "in place" */ | |
6ea44308 | 945 | nb_sectors = count >> BDRV_SECTOR_BITS; |
83f64091 | 946 | if (nb_sectors > 0) { |
9a8c4cce KW |
947 | if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) |
948 | return ret; | |
83f64091 | 949 | sector_num += nb_sectors; |
6ea44308 | 950 | len = nb_sectors << BDRV_SECTOR_BITS; |
83f64091 FB |
951 | buf += len; |
952 | count -= len; | |
953 | } | |
954 | ||
955 | /* add data from the last sector */ | |
956 | if (count > 0) { | |
9a8c4cce KW |
957 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
958 | return ret; | |
83f64091 FB |
959 | memcpy(buf, tmp_buf, count); |
960 | } | |
961 | return count1; | |
962 | } | |
963 | ||
eda578e5 AL |
964 | int bdrv_pwrite(BlockDriverState *bs, int64_t offset, |
965 | const void *buf, int count1) | |
83f64091 | 966 | { |
6ea44308 | 967 | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
83f64091 FB |
968 | int len, nb_sectors, count; |
969 | int64_t sector_num; | |
9a8c4cce | 970 | int ret; |
83f64091 FB |
971 | |
972 | count = count1; | |
973 | /* first write to align to sector start */ | |
6ea44308 | 974 | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); |
83f64091 FB |
975 | if (len > count) |
976 | len = count; | |
6ea44308 | 977 | sector_num = offset >> BDRV_SECTOR_BITS; |
83f64091 | 978 | if (len > 0) { |
9a8c4cce KW |
979 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
980 | return ret; | |
6ea44308 | 981 | memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len); |
9a8c4cce KW |
982 | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
983 | return ret; | |
83f64091 FB |
984 | count -= len; |
985 | if (count == 0) | |
986 | return count1; | |
987 | sector_num++; | |
988 | buf += len; | |
989 | } | |
990 | ||
991 | /* write the sectors "in place" */ | |
6ea44308 | 992 | nb_sectors = count >> BDRV_SECTOR_BITS; |
83f64091 | 993 | if (nb_sectors > 0) { |
9a8c4cce KW |
994 | if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) |
995 | return ret; | |
83f64091 | 996 | sector_num += nb_sectors; |
6ea44308 | 997 | len = nb_sectors << BDRV_SECTOR_BITS; |
83f64091 FB |
998 | buf += len; |
999 | count -= len; | |
1000 | } | |
1001 | ||
1002 | /* add data from the last sector */ | |
1003 | if (count > 0) { | |
9a8c4cce KW |
1004 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
1005 | return ret; | |
83f64091 | 1006 | memcpy(tmp_buf, buf, count); |
9a8c4cce KW |
1007 | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
1008 | return ret; | |
83f64091 FB |
1009 | } |
1010 | return count1; | |
1011 | } | |
83f64091 | 1012 | |
83f64091 FB |
1013 | /** |
1014 | * Truncate file to 'offset' bytes (needed only for file protocols) | |
1015 | */ | |
1016 | int bdrv_truncate(BlockDriverState *bs, int64_t offset) | |
1017 | { | |
1018 | BlockDriver *drv = bs->drv; | |
51762288 | 1019 | int ret; |
83f64091 | 1020 | if (!drv) |
19cb3738 | 1021 | return -ENOMEDIUM; |
83f64091 FB |
1022 | if (!drv->bdrv_truncate) |
1023 | return -ENOTSUP; | |
59f2689d NS |
1024 | if (bs->read_only) |
1025 | return -EACCES; | |
51762288 SH |
1026 | ret = drv->bdrv_truncate(bs, offset); |
1027 | if (ret == 0) { | |
1028 | ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); | |
1029 | } | |
1030 | return ret; | |
83f64091 FB |
1031 | } |
1032 | ||
1033 | /** | |
1034 | * Length of a file in bytes. Return < 0 if error or unknown. | |
1035 | */ | |
1036 | int64_t bdrv_getlength(BlockDriverState *bs) | |
1037 | { | |
1038 | BlockDriver *drv = bs->drv; | |
1039 | if (!drv) | |
19cb3738 | 1040 | return -ENOMEDIUM; |
51762288 SH |
1041 | |
1042 | /* Fixed size devices use the total_sectors value for speed instead of | |
1043 | issuing a length query (like lseek) on each call. Also, legacy block | |
1044 | drivers don't provide a bdrv_getlength function and must use | |
1045 | total_sectors. */ | |
1046 | if (!bs->growable || !drv->bdrv_getlength) { | |
6ea44308 | 1047 | return bs->total_sectors * BDRV_SECTOR_SIZE; |
83f64091 FB |
1048 | } |
1049 | return drv->bdrv_getlength(bs); | |
fc01f7e7 FB |
1050 | } |
1051 | ||
19cb3738 | 1052 | /* return 0 as number of sectors if no device present or error */ |
96b8f136 | 1053 | void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) |
fc01f7e7 | 1054 | { |
19cb3738 FB |
1055 | int64_t length; |
1056 | length = bdrv_getlength(bs); | |
1057 | if (length < 0) | |
1058 | length = 0; | |
1059 | else | |
6ea44308 | 1060 | length = length >> BDRV_SECTOR_BITS; |
19cb3738 | 1061 | *nb_sectors_ptr = length; |
fc01f7e7 | 1062 | } |
cf98951b | 1063 | |
f3d54fc4 AL |
1064 | struct partition { |
1065 | uint8_t boot_ind; /* 0x80 - active */ | |
1066 | uint8_t head; /* starting head */ | |
1067 | uint8_t sector; /* starting sector */ | |
1068 | uint8_t cyl; /* starting cylinder */ | |
1069 | uint8_t sys_ind; /* What partition type */ | |
1070 | uint8_t end_head; /* end head */ | |
1071 | uint8_t end_sector; /* end sector */ | |
1072 | uint8_t end_cyl; /* end cylinder */ | |
1073 | uint32_t start_sect; /* starting sector counting from 0 */ | |
1074 | uint32_t nr_sects; /* nr of sectors in partition */ | |
1075 | } __attribute__((packed)); | |
1076 | ||
1077 | /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ | |
1078 | static int guess_disk_lchs(BlockDriverState *bs, | |
1079 | int *pcylinders, int *pheads, int *psectors) | |
1080 | { | |
eb5a3165 | 1081 | uint8_t buf[BDRV_SECTOR_SIZE]; |
f3d54fc4 AL |
1082 | int ret, i, heads, sectors, cylinders; |
1083 | struct partition *p; | |
1084 | uint32_t nr_sects; | |
a38131b6 | 1085 | uint64_t nb_sectors; |
f3d54fc4 AL |
1086 | |
1087 | bdrv_get_geometry(bs, &nb_sectors); | |
1088 | ||
1089 | ret = bdrv_read(bs, 0, buf, 1); | |
1090 | if (ret < 0) | |
1091 | return -1; | |
1092 | /* test msdos magic */ | |
1093 | if (buf[510] != 0x55 || buf[511] != 0xaa) | |
1094 | return -1; | |
1095 | for(i = 0; i < 4; i++) { | |
1096 | p = ((struct partition *)(buf + 0x1be)) + i; | |
1097 | nr_sects = le32_to_cpu(p->nr_sects); | |
1098 | if (nr_sects && p->end_head) { | |
1099 | /* We make the assumption that the partition terminates on | |
1100 | a cylinder boundary */ | |
1101 | heads = p->end_head + 1; | |
1102 | sectors = p->end_sector & 63; | |
1103 | if (sectors == 0) | |
1104 | continue; | |
1105 | cylinders = nb_sectors / (heads * sectors); | |
1106 | if (cylinders < 1 || cylinders > 16383) | |
1107 | continue; | |
1108 | *pheads = heads; | |
1109 | *psectors = sectors; | |
1110 | *pcylinders = cylinders; | |
1111 | #if 0 | |
1112 | printf("guessed geometry: LCHS=%d %d %d\n", | |
1113 | cylinders, heads, sectors); | |
1114 | #endif | |
1115 | return 0; | |
1116 | } | |
1117 | } | |
1118 | return -1; | |
1119 | } | |
1120 | ||
1121 | void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) | |
1122 | { | |
1123 | int translation, lba_detected = 0; | |
1124 | int cylinders, heads, secs; | |
a38131b6 | 1125 | uint64_t nb_sectors; |
f3d54fc4 AL |
1126 | |
1127 | /* if a geometry hint is available, use it */ | |
1128 | bdrv_get_geometry(bs, &nb_sectors); | |
1129 | bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); | |
1130 | translation = bdrv_get_translation_hint(bs); | |
1131 | if (cylinders != 0) { | |
1132 | *pcyls = cylinders; | |
1133 | *pheads = heads; | |
1134 | *psecs = secs; | |
1135 | } else { | |
1136 | if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { | |
1137 | if (heads > 16) { | |
1138 | /* if heads > 16, it means that a BIOS LBA | |
1139 | translation was active, so the default | |
1140 | hardware geometry is OK */ | |
1141 | lba_detected = 1; | |
1142 | goto default_geometry; | |
1143 | } else { | |
1144 | *pcyls = cylinders; | |
1145 | *pheads = heads; | |
1146 | *psecs = secs; | |
1147 | /* disable any translation to be in sync with | |
1148 | the logical geometry */ | |
1149 | if (translation == BIOS_ATA_TRANSLATION_AUTO) { | |
1150 | bdrv_set_translation_hint(bs, | |
1151 | BIOS_ATA_TRANSLATION_NONE); | |
1152 | } | |
1153 | } | |
1154 | } else { | |
1155 | default_geometry: | |
1156 | /* if no geometry, use a standard physical disk geometry */ | |
1157 | cylinders = nb_sectors / (16 * 63); | |
1158 | ||
1159 | if (cylinders > 16383) | |
1160 | cylinders = 16383; | |
1161 | else if (cylinders < 2) | |
1162 | cylinders = 2; | |
1163 | *pcyls = cylinders; | |
1164 | *pheads = 16; | |
1165 | *psecs = 63; | |
1166 | if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { | |
1167 | if ((*pcyls * *pheads) <= 131072) { | |
1168 | bdrv_set_translation_hint(bs, | |
1169 | BIOS_ATA_TRANSLATION_LARGE); | |
1170 | } else { | |
1171 | bdrv_set_translation_hint(bs, | |
1172 | BIOS_ATA_TRANSLATION_LBA); | |
1173 | } | |
1174 | } | |
1175 | } | |
1176 | bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); | |
1177 | } | |
1178 | } | |
1179 | ||
5fafdf24 | 1180 | void bdrv_set_geometry_hint(BlockDriverState *bs, |
b338082b FB |
1181 | int cyls, int heads, int secs) |
1182 | { | |
1183 | bs->cyls = cyls; | |
1184 | bs->heads = heads; | |
1185 | bs->secs = secs; | |
1186 | } | |
1187 | ||
1188 | void bdrv_set_type_hint(BlockDriverState *bs, int type) | |
1189 | { | |
1190 | bs->type = type; | |
1191 | bs->removable = ((type == BDRV_TYPE_CDROM || | |
1192 | type == BDRV_TYPE_FLOPPY)); | |
1193 | } | |
1194 | ||
46d4767d FB |
1195 | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
1196 | { | |
1197 | bs->translation = translation; | |
1198 | } | |
1199 | ||
5fafdf24 | 1200 | void bdrv_get_geometry_hint(BlockDriverState *bs, |
b338082b FB |
1201 | int *pcyls, int *pheads, int *psecs) |
1202 | { | |
1203 | *pcyls = bs->cyls; | |
1204 | *pheads = bs->heads; | |
1205 | *psecs = bs->secs; | |
1206 | } | |
1207 | ||
1208 | int bdrv_get_type_hint(BlockDriverState *bs) | |
1209 | { | |
1210 | return bs->type; | |
1211 | } | |
1212 | ||
46d4767d FB |
1213 | int bdrv_get_translation_hint(BlockDriverState *bs) |
1214 | { | |
1215 | return bs->translation; | |
1216 | } | |
1217 | ||
abd7f68d MA |
1218 | void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error, |
1219 | BlockErrorAction on_write_error) | |
1220 | { | |
1221 | bs->on_read_error = on_read_error; | |
1222 | bs->on_write_error = on_write_error; | |
1223 | } | |
1224 | ||
1225 | BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read) | |
1226 | { | |
1227 | return is_read ? bs->on_read_error : bs->on_write_error; | |
1228 | } | |
1229 | ||
b338082b FB |
1230 | int bdrv_is_removable(BlockDriverState *bs) |
1231 | { | |
1232 | return bs->removable; | |
1233 | } | |
1234 | ||
1235 | int bdrv_is_read_only(BlockDriverState *bs) | |
1236 | { | |
1237 | return bs->read_only; | |
1238 | } | |
1239 | ||
985a03b0 TS |
1240 | int bdrv_is_sg(BlockDriverState *bs) |
1241 | { | |
1242 | return bs->sg; | |
1243 | } | |
1244 | ||
e900a7b7 CH |
1245 | int bdrv_enable_write_cache(BlockDriverState *bs) |
1246 | { | |
1247 | return bs->enable_write_cache; | |
1248 | } | |
1249 | ||
19cb3738 | 1250 | /* XXX: no longer used */ |
5fafdf24 | 1251 | void bdrv_set_change_cb(BlockDriverState *bs, |
b338082b FB |
1252 | void (*change_cb)(void *opaque), void *opaque) |
1253 | { | |
1254 | bs->change_cb = change_cb; | |
1255 | bs->change_opaque = opaque; | |
1256 | } | |
1257 | ||
ea2384d3 FB |
1258 | int bdrv_is_encrypted(BlockDriverState *bs) |
1259 | { | |
1260 | if (bs->backing_hd && bs->backing_hd->encrypted) | |
1261 | return 1; | |
1262 | return bs->encrypted; | |
1263 | } | |
1264 | ||
c0f4ce77 AL |
1265 | int bdrv_key_required(BlockDriverState *bs) |
1266 | { | |
1267 | BlockDriverState *backing_hd = bs->backing_hd; | |
1268 | ||
1269 | if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) | |
1270 | return 1; | |
1271 | return (bs->encrypted && !bs->valid_key); | |
1272 | } | |
1273 | ||
ea2384d3 FB |
1274 | int bdrv_set_key(BlockDriverState *bs, const char *key) |
1275 | { | |
1276 | int ret; | |
1277 | if (bs->backing_hd && bs->backing_hd->encrypted) { | |
1278 | ret = bdrv_set_key(bs->backing_hd, key); | |
1279 | if (ret < 0) | |
1280 | return ret; | |
1281 | if (!bs->encrypted) | |
1282 | return 0; | |
1283 | } | |
fd04a2ae SH |
1284 | if (!bs->encrypted) { |
1285 | return -EINVAL; | |
1286 | } else if (!bs->drv || !bs->drv->bdrv_set_key) { | |
1287 | return -ENOMEDIUM; | |
1288 | } | |
c0f4ce77 | 1289 | ret = bs->drv->bdrv_set_key(bs, key); |
bb5fc20f AL |
1290 | if (ret < 0) { |
1291 | bs->valid_key = 0; | |
1292 | } else if (!bs->valid_key) { | |
1293 | bs->valid_key = 1; | |
1294 | /* call the change callback now, we skipped it on open */ | |
1295 | bs->media_changed = 1; | |
1296 | if (bs->change_cb) | |
1297 | bs->change_cb(bs->change_opaque); | |
1298 | } | |
c0f4ce77 | 1299 | return ret; |
ea2384d3 FB |
1300 | } |
1301 | ||
1302 | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) | |
1303 | { | |
19cb3738 | 1304 | if (!bs->drv) { |
ea2384d3 FB |
1305 | buf[0] = '\0'; |
1306 | } else { | |
1307 | pstrcpy(buf, buf_size, bs->drv->format_name); | |
1308 | } | |
1309 | } | |
1310 | ||
5fafdf24 | 1311 | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
ea2384d3 FB |
1312 | void *opaque) |
1313 | { | |
1314 | BlockDriver *drv; | |
1315 | ||
8a22f02a | 1316 | QLIST_FOREACH(drv, &bdrv_drivers, list) { |
ea2384d3 FB |
1317 | it(opaque, drv->format_name); |
1318 | } | |
1319 | } | |
1320 | ||
b338082b FB |
1321 | BlockDriverState *bdrv_find(const char *name) |
1322 | { | |
1323 | BlockDriverState *bs; | |
1324 | ||
1b7bdbc1 SH |
1325 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
1326 | if (!strcmp(name, bs->device_name)) { | |
b338082b | 1327 | return bs; |
1b7bdbc1 | 1328 | } |
b338082b FB |
1329 | } |
1330 | return NULL; | |
1331 | } | |
1332 | ||
2f399b0a MA |
1333 | BlockDriverState *bdrv_next(BlockDriverState *bs) |
1334 | { | |
1335 | if (!bs) { | |
1336 | return QTAILQ_FIRST(&bdrv_states); | |
1337 | } | |
1338 | return QTAILQ_NEXT(bs, list); | |
1339 | } | |
1340 | ||
51de9760 | 1341 | void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) |
81d0912d FB |
1342 | { |
1343 | BlockDriverState *bs; | |
1344 | ||
1b7bdbc1 | 1345 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
51de9760 | 1346 | it(opaque, bs); |
81d0912d FB |
1347 | } |
1348 | } | |
1349 | ||
ea2384d3 FB |
1350 | const char *bdrv_get_device_name(BlockDriverState *bs) |
1351 | { | |
1352 | return bs->device_name; | |
1353 | } | |
1354 | ||
7a6cba61 PB |
1355 | void bdrv_flush(BlockDriverState *bs) |
1356 | { | |
016f5cf6 AG |
1357 | if (bs->open_flags & BDRV_O_NO_FLUSH) { |
1358 | return; | |
1359 | } | |
1360 | ||
3f5075ae | 1361 | if (bs->drv && bs->drv->bdrv_flush) |
7a6cba61 | 1362 | bs->drv->bdrv_flush(bs); |
7a6cba61 PB |
1363 | } |
1364 | ||
c6ca28d6 AL |
1365 | void bdrv_flush_all(void) |
1366 | { | |
1367 | BlockDriverState *bs; | |
1368 | ||
1b7bdbc1 SH |
1369 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
1370 | if (bs->drv && !bdrv_is_read_only(bs) && | |
1371 | (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) { | |
c6ca28d6 | 1372 | bdrv_flush(bs); |
1b7bdbc1 SH |
1373 | } |
1374 | } | |
c6ca28d6 AL |
1375 | } |
1376 | ||
f2feebbd KW |
1377 | int bdrv_has_zero_init(BlockDriverState *bs) |
1378 | { | |
1379 | assert(bs->drv); | |
1380 | ||
1381 | if (bs->drv->no_zero_init) { | |
1382 | return 0; | |
1383 | } else if (bs->file) { | |
1384 | return bdrv_has_zero_init(bs->file); | |
1385 | } | |
1386 | ||
1387 | return 1; | |
1388 | } | |
1389 | ||
f58c7b35 TS |
1390 | /* |
1391 | * Returns true iff the specified sector is present in the disk image. Drivers | |
1392 | * not implementing the functionality are assumed to not support backing files, | |
1393 | * hence all their sectors are reported as allocated. | |
1394 | * | |
1395 | * 'pnum' is set to the number of sectors (including and immediately following | |
1396 | * the specified sector) that are known to be in the same | |
1397 | * allocated/unallocated state. | |
1398 | * | |
1399 | * 'nb_sectors' is the max value 'pnum' should be set to. | |
1400 | */ | |
1401 | int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, | |
1402 | int *pnum) | |
1403 | { | |
1404 | int64_t n; | |
1405 | if (!bs->drv->bdrv_is_allocated) { | |
1406 | if (sector_num >= bs->total_sectors) { | |
1407 | *pnum = 0; | |
1408 | return 0; | |
1409 | } | |
1410 | n = bs->total_sectors - sector_num; | |
1411 | *pnum = (n < nb_sectors) ? (n) : (nb_sectors); | |
1412 | return 1; | |
1413 | } | |
1414 | return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); | |
1415 | } | |
1416 | ||
2582bfed LC |
1417 | void bdrv_mon_event(const BlockDriverState *bdrv, |
1418 | BlockMonEventAction action, int is_read) | |
1419 | { | |
1420 | QObject *data; | |
1421 | const char *action_str; | |
1422 | ||
1423 | switch (action) { | |
1424 | case BDRV_ACTION_REPORT: | |
1425 | action_str = "report"; | |
1426 | break; | |
1427 | case BDRV_ACTION_IGNORE: | |
1428 | action_str = "ignore"; | |
1429 | break; | |
1430 | case BDRV_ACTION_STOP: | |
1431 | action_str = "stop"; | |
1432 | break; | |
1433 | default: | |
1434 | abort(); | |
1435 | } | |
1436 | ||
1437 | data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", | |
1438 | bdrv->device_name, | |
1439 | action_str, | |
1440 | is_read ? "read" : "write"); | |
1441 | monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data); | |
1442 | ||
1443 | qobject_decref(data); | |
1444 | } | |
1445 | ||
d15e5465 | 1446 | static void bdrv_print_dict(QObject *obj, void *opaque) |
b338082b | 1447 | { |
d15e5465 LC |
1448 | QDict *bs_dict; |
1449 | Monitor *mon = opaque; | |
1450 | ||
1451 | bs_dict = qobject_to_qdict(obj); | |
1452 | ||
1453 | monitor_printf(mon, "%s: type=%s removable=%d", | |
1454 | qdict_get_str(bs_dict, "device"), | |
1455 | qdict_get_str(bs_dict, "type"), | |
1456 | qdict_get_bool(bs_dict, "removable")); | |
1457 | ||
1458 | if (qdict_get_bool(bs_dict, "removable")) { | |
1459 | monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked")); | |
1460 | } | |
1461 | ||
1462 | if (qdict_haskey(bs_dict, "inserted")) { | |
1463 | QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted")); | |
1464 | ||
1465 | monitor_printf(mon, " file="); | |
1466 | monitor_print_filename(mon, qdict_get_str(qdict, "file")); | |
1467 | if (qdict_haskey(qdict, "backing_file")) { | |
1468 | monitor_printf(mon, " backing_file="); | |
1469 | monitor_print_filename(mon, qdict_get_str(qdict, "backing_file")); | |
1470 | } | |
1471 | monitor_printf(mon, " ro=%d drv=%s encrypted=%d", | |
1472 | qdict_get_bool(qdict, "ro"), | |
1473 | qdict_get_str(qdict, "drv"), | |
1474 | qdict_get_bool(qdict, "encrypted")); | |
1475 | } else { | |
1476 | monitor_printf(mon, " [not inserted]"); | |
1477 | } | |
1478 | ||
1479 | monitor_printf(mon, "\n"); | |
1480 | } | |
1481 | ||
1482 | void bdrv_info_print(Monitor *mon, const QObject *data) | |
1483 | { | |
1484 | qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon); | |
1485 | } | |
1486 | ||
d15e5465 LC |
1487 | void bdrv_info(Monitor *mon, QObject **ret_data) |
1488 | { | |
1489 | QList *bs_list; | |
b338082b FB |
1490 | BlockDriverState *bs; |
1491 | ||
d15e5465 LC |
1492 | bs_list = qlist_new(); |
1493 | ||
1b7bdbc1 | 1494 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
d15e5465 LC |
1495 | QObject *bs_obj; |
1496 | const char *type = "unknown"; | |
1497 | ||
b338082b FB |
1498 | switch(bs->type) { |
1499 | case BDRV_TYPE_HD: | |
d15e5465 | 1500 | type = "hd"; |
b338082b FB |
1501 | break; |
1502 | case BDRV_TYPE_CDROM: | |
d15e5465 | 1503 | type = "cdrom"; |
b338082b FB |
1504 | break; |
1505 | case BDRV_TYPE_FLOPPY: | |
d15e5465 | 1506 | type = "floppy"; |
b338082b FB |
1507 | break; |
1508 | } | |
d15e5465 LC |
1509 | |
1510 | bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, " | |
1511 | "'removable': %i, 'locked': %i }", | |
1512 | bs->device_name, type, bs->removable, | |
1513 | bs->locked); | |
d15e5465 | 1514 | |
19cb3738 | 1515 | if (bs->drv) { |
d15e5465 LC |
1516 | QObject *obj; |
1517 | QDict *bs_dict = qobject_to_qdict(bs_obj); | |
1518 | ||
1519 | obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, " | |
1520 | "'encrypted': %i }", | |
1521 | bs->filename, bs->read_only, | |
1522 | bs->drv->format_name, | |
1523 | bdrv_is_encrypted(bs)); | |
fef30743 | 1524 | if (bs->backing_file[0] != '\0') { |
d15e5465 LC |
1525 | QDict *qdict = qobject_to_qdict(obj); |
1526 | qdict_put(qdict, "backing_file", | |
1527 | qstring_from_str(bs->backing_file)); | |
376253ec | 1528 | } |
d15e5465 LC |
1529 | |
1530 | qdict_put_obj(bs_dict, "inserted", obj); | |
b338082b | 1531 | } |
d15e5465 | 1532 | qlist_append_obj(bs_list, bs_obj); |
b338082b | 1533 | } |
d15e5465 LC |
1534 | |
1535 | *ret_data = QOBJECT(bs_list); | |
b338082b | 1536 | } |
a36e69dd | 1537 | |
218a536a | 1538 | static void bdrv_stats_iter(QObject *data, void *opaque) |
a36e69dd | 1539 | { |
218a536a LC |
1540 | QDict *qdict; |
1541 | Monitor *mon = opaque; | |
1542 | ||
1543 | qdict = qobject_to_qdict(data); | |
1544 | monitor_printf(mon, "%s:", qdict_get_str(qdict, "device")); | |
1545 | ||
1546 | qdict = qobject_to_qdict(qdict_get(qdict, "stats")); | |
1547 | monitor_printf(mon, " rd_bytes=%" PRId64 | |
1548 | " wr_bytes=%" PRId64 | |
1549 | " rd_operations=%" PRId64 | |
1550 | " wr_operations=%" PRId64 | |
1551 | "\n", | |
1552 | qdict_get_int(qdict, "rd_bytes"), | |
1553 | qdict_get_int(qdict, "wr_bytes"), | |
1554 | qdict_get_int(qdict, "rd_operations"), | |
1555 | qdict_get_int(qdict, "wr_operations")); | |
1556 | } | |
1557 | ||
1558 | void bdrv_stats_print(Monitor *mon, const QObject *data) | |
1559 | { | |
1560 | qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon); | |
1561 | } | |
1562 | ||
294cc35f KW |
1563 | static QObject* bdrv_info_stats_bs(BlockDriverState *bs) |
1564 | { | |
1565 | QObject *res; | |
1566 | QDict *dict; | |
1567 | ||
1568 | res = qobject_from_jsonf("{ 'stats': {" | |
1569 | "'rd_bytes': %" PRId64 "," | |
1570 | "'wr_bytes': %" PRId64 "," | |
1571 | "'rd_operations': %" PRId64 "," | |
1572 | "'wr_operations': %" PRId64 "," | |
1573 | "'wr_highest_offset': %" PRId64 | |
1574 | "} }", | |
1575 | bs->rd_bytes, bs->wr_bytes, | |
1576 | bs->rd_ops, bs->wr_ops, | |
5ffbbc67 BS |
1577 | bs->wr_highest_sector * |
1578 | (uint64_t)BDRV_SECTOR_SIZE); | |
294cc35f KW |
1579 | dict = qobject_to_qdict(res); |
1580 | ||
1581 | if (*bs->device_name) { | |
1582 | qdict_put(dict, "device", qstring_from_str(bs->device_name)); | |
1583 | } | |
1584 | ||
1585 | if (bs->file) { | |
1586 | QObject *parent = bdrv_info_stats_bs(bs->file); | |
1587 | qdict_put_obj(dict, "parent", parent); | |
1588 | } | |
1589 | ||
1590 | return res; | |
1591 | } | |
1592 | ||
218a536a LC |
1593 | void bdrv_info_stats(Monitor *mon, QObject **ret_data) |
1594 | { | |
1595 | QObject *obj; | |
1596 | QList *devices; | |
a36e69dd TS |
1597 | BlockDriverState *bs; |
1598 | ||
218a536a LC |
1599 | devices = qlist_new(); |
1600 | ||
1b7bdbc1 | 1601 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
294cc35f | 1602 | obj = bdrv_info_stats_bs(bs); |
218a536a | 1603 | qlist_append_obj(devices, obj); |
a36e69dd | 1604 | } |
218a536a LC |
1605 | |
1606 | *ret_data = QOBJECT(devices); | |
a36e69dd | 1607 | } |
ea2384d3 | 1608 | |
045df330 AL |
1609 | const char *bdrv_get_encrypted_filename(BlockDriverState *bs) |
1610 | { | |
1611 | if (bs->backing_hd && bs->backing_hd->encrypted) | |
1612 | return bs->backing_file; | |
1613 | else if (bs->encrypted) | |
1614 | return bs->filename; | |
1615 | else | |
1616 | return NULL; | |
1617 | } | |
1618 | ||
5fafdf24 | 1619 | void bdrv_get_backing_filename(BlockDriverState *bs, |
83f64091 FB |
1620 | char *filename, int filename_size) |
1621 | { | |
b783e409 | 1622 | if (!bs->backing_file) { |
83f64091 FB |
1623 | pstrcpy(filename, filename_size, ""); |
1624 | } else { | |
1625 | pstrcpy(filename, filename_size, bs->backing_file); | |
1626 | } | |
1627 | } | |
1628 | ||
5fafdf24 | 1629 | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, |
faea38e7 FB |
1630 | const uint8_t *buf, int nb_sectors) |
1631 | { | |
1632 | BlockDriver *drv = bs->drv; | |
1633 | if (!drv) | |
19cb3738 | 1634 | return -ENOMEDIUM; |
faea38e7 FB |
1635 | if (!drv->bdrv_write_compressed) |
1636 | return -ENOTSUP; | |
fbb7b4e0 KW |
1637 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
1638 | return -EIO; | |
a55eb92c | 1639 | |
c6d22830 | 1640 | if (bs->dirty_bitmap) { |
7cd1e32a LS |
1641 | set_dirty_bitmap(bs, sector_num, nb_sectors, 1); |
1642 | } | |
a55eb92c | 1643 | |
faea38e7 FB |
1644 | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); |
1645 | } | |
3b46e624 | 1646 | |
faea38e7 FB |
1647 | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
1648 | { | |
1649 | BlockDriver *drv = bs->drv; | |
1650 | if (!drv) | |
19cb3738 | 1651 | return -ENOMEDIUM; |
faea38e7 FB |
1652 | if (!drv->bdrv_get_info) |
1653 | return -ENOTSUP; | |
1654 | memset(bdi, 0, sizeof(*bdi)); | |
1655 | return drv->bdrv_get_info(bs, bdi); | |
1656 | } | |
1657 | ||
45566e9c CH |
1658 | int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, |
1659 | int64_t pos, int size) | |
178e08a5 AL |
1660 | { |
1661 | BlockDriver *drv = bs->drv; | |
1662 | if (!drv) | |
1663 | return -ENOMEDIUM; | |
7cdb1f6d MK |
1664 | if (drv->bdrv_save_vmstate) |
1665 | return drv->bdrv_save_vmstate(bs, buf, pos, size); | |
1666 | if (bs->file) | |
1667 | return bdrv_save_vmstate(bs->file, buf, pos, size); | |
1668 | return -ENOTSUP; | |
178e08a5 AL |
1669 | } |
1670 | ||
45566e9c CH |
1671 | int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, |
1672 | int64_t pos, int size) | |
178e08a5 AL |
1673 | { |
1674 | BlockDriver *drv = bs->drv; | |
1675 | if (!drv) | |
1676 | return -ENOMEDIUM; | |
7cdb1f6d MK |
1677 | if (drv->bdrv_load_vmstate) |
1678 | return drv->bdrv_load_vmstate(bs, buf, pos, size); | |
1679 | if (bs->file) | |
1680 | return bdrv_load_vmstate(bs->file, buf, pos, size); | |
1681 | return -ENOTSUP; | |
178e08a5 AL |
1682 | } |
1683 | ||
8b9b0cc2 KW |
1684 | void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) |
1685 | { | |
1686 | BlockDriver *drv = bs->drv; | |
1687 | ||
1688 | if (!drv || !drv->bdrv_debug_event) { | |
1689 | return; | |
1690 | } | |
1691 | ||
1692 | return drv->bdrv_debug_event(bs, event); | |
1693 | ||
1694 | } | |
1695 | ||
faea38e7 FB |
1696 | /**************************************************************/ |
1697 | /* handling of snapshots */ | |
1698 | ||
feeee5ac MDCF |
1699 | int bdrv_can_snapshot(BlockDriverState *bs) |
1700 | { | |
1701 | BlockDriver *drv = bs->drv; | |
1702 | if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) { | |
1703 | return 0; | |
1704 | } | |
1705 | ||
1706 | if (!drv->bdrv_snapshot_create) { | |
1707 | if (bs->file != NULL) { | |
1708 | return bdrv_can_snapshot(bs->file); | |
1709 | } | |
1710 | return 0; | |
1711 | } | |
1712 | ||
1713 | return 1; | |
1714 | } | |
1715 | ||
5fafdf24 | 1716 | int bdrv_snapshot_create(BlockDriverState *bs, |
faea38e7 FB |
1717 | QEMUSnapshotInfo *sn_info) |
1718 | { | |
1719 | BlockDriver *drv = bs->drv; | |
1720 | if (!drv) | |
19cb3738 | 1721 | return -ENOMEDIUM; |
7cdb1f6d MK |
1722 | if (drv->bdrv_snapshot_create) |
1723 | return drv->bdrv_snapshot_create(bs, sn_info); | |
1724 | if (bs->file) | |
1725 | return bdrv_snapshot_create(bs->file, sn_info); | |
1726 | return -ENOTSUP; | |
faea38e7 FB |
1727 | } |
1728 | ||
5fafdf24 | 1729 | int bdrv_snapshot_goto(BlockDriverState *bs, |
faea38e7 FB |
1730 | const char *snapshot_id) |
1731 | { | |
1732 | BlockDriver *drv = bs->drv; | |
7cdb1f6d MK |
1733 | int ret, open_ret; |
1734 | ||
faea38e7 | 1735 | if (!drv) |
19cb3738 | 1736 | return -ENOMEDIUM; |
7cdb1f6d MK |
1737 | if (drv->bdrv_snapshot_goto) |
1738 | return drv->bdrv_snapshot_goto(bs, snapshot_id); | |
1739 | ||
1740 | if (bs->file) { | |
1741 | drv->bdrv_close(bs); | |
1742 | ret = bdrv_snapshot_goto(bs->file, snapshot_id); | |
1743 | open_ret = drv->bdrv_open(bs, bs->open_flags); | |
1744 | if (open_ret < 0) { | |
1745 | bdrv_delete(bs->file); | |
1746 | bs->drv = NULL; | |
1747 | return open_ret; | |
1748 | } | |
1749 | return ret; | |
1750 | } | |
1751 | ||
1752 | return -ENOTSUP; | |
faea38e7 FB |
1753 | } |
1754 | ||
1755 | int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) | |
1756 | { | |
1757 | BlockDriver *drv = bs->drv; | |
1758 | if (!drv) | |
19cb3738 | 1759 | return -ENOMEDIUM; |
7cdb1f6d MK |
1760 | if (drv->bdrv_snapshot_delete) |
1761 | return drv->bdrv_snapshot_delete(bs, snapshot_id); | |
1762 | if (bs->file) | |
1763 | return bdrv_snapshot_delete(bs->file, snapshot_id); | |
1764 | return -ENOTSUP; | |
faea38e7 FB |
1765 | } |
1766 | ||
5fafdf24 | 1767 | int bdrv_snapshot_list(BlockDriverState *bs, |
faea38e7 FB |
1768 | QEMUSnapshotInfo **psn_info) |
1769 | { | |
1770 | BlockDriver *drv = bs->drv; | |
1771 | if (!drv) | |
19cb3738 | 1772 | return -ENOMEDIUM; |
7cdb1f6d MK |
1773 | if (drv->bdrv_snapshot_list) |
1774 | return drv->bdrv_snapshot_list(bs, psn_info); | |
1775 | if (bs->file) | |
1776 | return bdrv_snapshot_list(bs->file, psn_info); | |
1777 | return -ENOTSUP; | |
faea38e7 FB |
1778 | } |
1779 | ||
1780 | #define NB_SUFFIXES 4 | |
1781 | ||
1782 | char *get_human_readable_size(char *buf, int buf_size, int64_t size) | |
1783 | { | |
1784 | static const char suffixes[NB_SUFFIXES] = "KMGT"; | |
1785 | int64_t base; | |
1786 | int i; | |
1787 | ||
1788 | if (size <= 999) { | |
1789 | snprintf(buf, buf_size, "%" PRId64, size); | |
1790 | } else { | |
1791 | base = 1024; | |
1792 | for(i = 0; i < NB_SUFFIXES; i++) { | |
1793 | if (size < (10 * base)) { | |
5fafdf24 | 1794 | snprintf(buf, buf_size, "%0.1f%c", |
faea38e7 FB |
1795 | (double)size / base, |
1796 | suffixes[i]); | |
1797 | break; | |
1798 | } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { | |
5fafdf24 | 1799 | snprintf(buf, buf_size, "%" PRId64 "%c", |
faea38e7 FB |
1800 | ((size + (base >> 1)) / base), |
1801 | suffixes[i]); | |
1802 | break; | |
1803 | } | |
1804 | base = base * 1024; | |
1805 | } | |
1806 | } | |
1807 | return buf; | |
1808 | } | |
1809 | ||
1810 | char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) | |
1811 | { | |
1812 | char buf1[128], date_buf[128], clock_buf[128]; | |
3b9f94e1 FB |
1813 | #ifdef _WIN32 |
1814 | struct tm *ptm; | |
1815 | #else | |
faea38e7 | 1816 | struct tm tm; |
3b9f94e1 | 1817 | #endif |
faea38e7 FB |
1818 | time_t ti; |
1819 | int64_t secs; | |
1820 | ||
1821 | if (!sn) { | |
5fafdf24 TS |
1822 | snprintf(buf, buf_size, |
1823 | "%-10s%-20s%7s%20s%15s", | |
faea38e7 FB |
1824 | "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); |
1825 | } else { | |
1826 | ti = sn->date_sec; | |
3b9f94e1 FB |
1827 | #ifdef _WIN32 |
1828 | ptm = localtime(&ti); | |
1829 | strftime(date_buf, sizeof(date_buf), | |
1830 | "%Y-%m-%d %H:%M:%S", ptm); | |
1831 | #else | |
faea38e7 FB |
1832 | localtime_r(&ti, &tm); |
1833 | strftime(date_buf, sizeof(date_buf), | |
1834 | "%Y-%m-%d %H:%M:%S", &tm); | |
3b9f94e1 | 1835 | #endif |
faea38e7 FB |
1836 | secs = sn->vm_clock_nsec / 1000000000; |
1837 | snprintf(clock_buf, sizeof(clock_buf), | |
1838 | "%02d:%02d:%02d.%03d", | |
1839 | (int)(secs / 3600), | |
1840 | (int)((secs / 60) % 60), | |
5fafdf24 | 1841 | (int)(secs % 60), |
faea38e7 FB |
1842 | (int)((sn->vm_clock_nsec / 1000000) % 1000)); |
1843 | snprintf(buf, buf_size, | |
5fafdf24 | 1844 | "%-10s%-20s%7s%20s%15s", |
faea38e7 FB |
1845 | sn->id_str, sn->name, |
1846 | get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), | |
1847 | date_buf, | |
1848 | clock_buf); | |
1849 | } | |
1850 | return buf; | |
1851 | } | |
1852 | ||
83f64091 | 1853 | |
ea2384d3 | 1854 | /**************************************************************/ |
83f64091 | 1855 | /* async I/Os */ |
ea2384d3 | 1856 | |
3b69e4b9 | 1857 | BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, |
f141eafe | 1858 | QEMUIOVector *qiov, int nb_sectors, |
3b69e4b9 | 1859 | BlockDriverCompletionFunc *cb, void *opaque) |
83f64091 FB |
1860 | { |
1861 | BlockDriver *drv = bs->drv; | |
a36e69dd | 1862 | BlockDriverAIOCB *ret; |
83f64091 | 1863 | |
19cb3738 | 1864 | if (!drv) |
ce1a14dc | 1865 | return NULL; |
71d0770c AL |
1866 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
1867 | return NULL; | |
3b46e624 | 1868 | |
f141eafe AL |
1869 | ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors, |
1870 | cb, opaque); | |
a36e69dd TS |
1871 | |
1872 | if (ret) { | |
1873 | /* Update stats even though technically transfer has not happened. */ | |
6ea44308 | 1874 | bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; |
a36e69dd TS |
1875 | bs->rd_ops ++; |
1876 | } | |
1877 | ||
1878 | return ret; | |
ea2384d3 FB |
1879 | } |
1880 | ||
f141eafe AL |
1881 | BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, |
1882 | QEMUIOVector *qiov, int nb_sectors, | |
1883 | BlockDriverCompletionFunc *cb, void *opaque) | |
ea2384d3 | 1884 | { |
83f64091 | 1885 | BlockDriver *drv = bs->drv; |
a36e69dd | 1886 | BlockDriverAIOCB *ret; |
ea2384d3 | 1887 | |
19cb3738 | 1888 | if (!drv) |
ce1a14dc | 1889 | return NULL; |
83f64091 | 1890 | if (bs->read_only) |
ce1a14dc | 1891 | return NULL; |
71d0770c AL |
1892 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
1893 | return NULL; | |
83f64091 | 1894 | |
c6d22830 | 1895 | if (bs->dirty_bitmap) { |
7cd1e32a LS |
1896 | set_dirty_bitmap(bs, sector_num, nb_sectors, 1); |
1897 | } | |
a55eb92c | 1898 | |
f141eafe AL |
1899 | ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors, |
1900 | cb, opaque); | |
a36e69dd TS |
1901 | |
1902 | if (ret) { | |
294cc35f KW |
1903 | /* Update stats even though technically transfer has not happened. */ |
1904 | bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; | |
1905 | bs->wr_ops ++; | |
1906 | if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { | |
1907 | bs->wr_highest_sector = sector_num + nb_sectors - 1; | |
1908 | } | |
a36e69dd TS |
1909 | } |
1910 | ||
1911 | return ret; | |
83f64091 FB |
1912 | } |
1913 | ||
40b4f539 KW |
1914 | |
1915 | typedef struct MultiwriteCB { | |
1916 | int error; | |
1917 | int num_requests; | |
1918 | int num_callbacks; | |
1919 | struct { | |
1920 | BlockDriverCompletionFunc *cb; | |
1921 | void *opaque; | |
1922 | QEMUIOVector *free_qiov; | |
1923 | void *free_buf; | |
1924 | } callbacks[]; | |
1925 | } MultiwriteCB; | |
1926 | ||
1927 | static void multiwrite_user_cb(MultiwriteCB *mcb) | |
1928 | { | |
1929 | int i; | |
1930 | ||
1931 | for (i = 0; i < mcb->num_callbacks; i++) { | |
1932 | mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); | |
1e1ea48d SH |
1933 | if (mcb->callbacks[i].free_qiov) { |
1934 | qemu_iovec_destroy(mcb->callbacks[i].free_qiov); | |
1935 | } | |
40b4f539 | 1936 | qemu_free(mcb->callbacks[i].free_qiov); |
f8a83245 | 1937 | qemu_vfree(mcb->callbacks[i].free_buf); |
40b4f539 KW |
1938 | } |
1939 | } | |
1940 | ||
1941 | static void multiwrite_cb(void *opaque, int ret) | |
1942 | { | |
1943 | MultiwriteCB *mcb = opaque; | |
1944 | ||
cb6d3ca0 | 1945 | if (ret < 0 && !mcb->error) { |
40b4f539 KW |
1946 | mcb->error = ret; |
1947 | multiwrite_user_cb(mcb); | |
1948 | } | |
1949 | ||
1950 | mcb->num_requests--; | |
1951 | if (mcb->num_requests == 0) { | |
1952 | if (mcb->error == 0) { | |
1953 | multiwrite_user_cb(mcb); | |
1954 | } | |
1955 | qemu_free(mcb); | |
1956 | } | |
1957 | } | |
1958 | ||
1959 | static int multiwrite_req_compare(const void *a, const void *b) | |
1960 | { | |
77be4366 CH |
1961 | const BlockRequest *req1 = a, *req2 = b; |
1962 | ||
1963 | /* | |
1964 | * Note that we can't simply subtract req2->sector from req1->sector | |
1965 | * here as that could overflow the return value. | |
1966 | */ | |
1967 | if (req1->sector > req2->sector) { | |
1968 | return 1; | |
1969 | } else if (req1->sector < req2->sector) { | |
1970 | return -1; | |
1971 | } else { | |
1972 | return 0; | |
1973 | } | |
40b4f539 KW |
1974 | } |
1975 | ||
1976 | /* | |
1977 | * Takes a bunch of requests and tries to merge them. Returns the number of | |
1978 | * requests that remain after merging. | |
1979 | */ | |
1980 | static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, | |
1981 | int num_reqs, MultiwriteCB *mcb) | |
1982 | { | |
1983 | int i, outidx; | |
1984 | ||
1985 | // Sort requests by start sector | |
1986 | qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); | |
1987 | ||
1988 | // Check if adjacent requests touch the same clusters. If so, combine them, | |
1989 | // filling up gaps with zero sectors. | |
1990 | outidx = 0; | |
1991 | for (i = 1; i < num_reqs; i++) { | |
1992 | int merge = 0; | |
1993 | int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; | |
1994 | ||
1995 | // This handles the cases that are valid for all block drivers, namely | |
1996 | // exactly sequential writes and overlapping writes. | |
1997 | if (reqs[i].sector <= oldreq_last) { | |
1998 | merge = 1; | |
1999 | } | |
2000 | ||
2001 | // The block driver may decide that it makes sense to combine requests | |
2002 | // even if there is a gap of some sectors between them. In this case, | |
2003 | // the gap is filled with zeros (therefore only applicable for yet | |
2004 | // unused space in format like qcow2). | |
2005 | if (!merge && bs->drv->bdrv_merge_requests) { | |
2006 | merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]); | |
2007 | } | |
2008 | ||
e2a305fb CH |
2009 | if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { |
2010 | merge = 0; | |
2011 | } | |
2012 | ||
40b4f539 KW |
2013 | if (merge) { |
2014 | size_t size; | |
2015 | QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov)); | |
2016 | qemu_iovec_init(qiov, | |
2017 | reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); | |
2018 | ||
2019 | // Add the first request to the merged one. If the requests are | |
2020 | // overlapping, drop the last sectors of the first request. | |
2021 | size = (reqs[i].sector - reqs[outidx].sector) << 9; | |
2022 | qemu_iovec_concat(qiov, reqs[outidx].qiov, size); | |
2023 | ||
2024 | // We might need to add some zeros between the two requests | |
2025 | if (reqs[i].sector > oldreq_last) { | |
2026 | size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9; | |
2027 | uint8_t *buf = qemu_blockalign(bs, zero_bytes); | |
2028 | memset(buf, 0, zero_bytes); | |
2029 | qemu_iovec_add(qiov, buf, zero_bytes); | |
2030 | mcb->callbacks[i].free_buf = buf; | |
2031 | } | |
2032 | ||
2033 | // Add the second request | |
2034 | qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size); | |
2035 | ||
cbf1dff2 | 2036 | reqs[outidx].nb_sectors = qiov->size >> 9; |
40b4f539 KW |
2037 | reqs[outidx].qiov = qiov; |
2038 | ||
2039 | mcb->callbacks[i].free_qiov = reqs[outidx].qiov; | |
2040 | } else { | |
2041 | outidx++; | |
2042 | reqs[outidx].sector = reqs[i].sector; | |
2043 | reqs[outidx].nb_sectors = reqs[i].nb_sectors; | |
2044 | reqs[outidx].qiov = reqs[i].qiov; | |
2045 | } | |
2046 | } | |
2047 | ||
2048 | return outidx + 1; | |
2049 | } | |
2050 | ||
2051 | /* | |
2052 | * Submit multiple AIO write requests at once. | |
2053 | * | |
2054 | * On success, the function returns 0 and all requests in the reqs array have | |
2055 | * been submitted. In error case this function returns -1, and any of the | |
2056 | * requests may or may not be submitted yet. In particular, this means that the | |
2057 | * callback will be called for some of the requests, for others it won't. The | |
2058 | * caller must check the error field of the BlockRequest to wait for the right | |
2059 | * callbacks (if error != 0, no callback will be called). | |
2060 | * | |
2061 | * The implementation may modify the contents of the reqs array, e.g. to merge | |
2062 | * requests. However, the fields opaque and error are left unmodified as they | |
2063 | * are used to signal failure for a single request to the caller. | |
2064 | */ | |
2065 | int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) | |
2066 | { | |
2067 | BlockDriverAIOCB *acb; | |
2068 | MultiwriteCB *mcb; | |
2069 | int i; | |
2070 | ||
2071 | if (num_reqs == 0) { | |
2072 | return 0; | |
2073 | } | |
2074 | ||
2075 | // Create MultiwriteCB structure | |
2076 | mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); | |
2077 | mcb->num_requests = 0; | |
2078 | mcb->num_callbacks = num_reqs; | |
2079 | ||
2080 | for (i = 0; i < num_reqs; i++) { | |
2081 | mcb->callbacks[i].cb = reqs[i].cb; | |
2082 | mcb->callbacks[i].opaque = reqs[i].opaque; | |
2083 | } | |
2084 | ||
2085 | // Check for mergable requests | |
2086 | num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); | |
2087 | ||
2088 | // Run the aio requests | |
2089 | for (i = 0; i < num_reqs; i++) { | |
2090 | acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, | |
2091 | reqs[i].nb_sectors, multiwrite_cb, mcb); | |
2092 | ||
2093 | if (acb == NULL) { | |
2094 | // We can only fail the whole thing if no request has been | |
2095 | // submitted yet. Otherwise we'll wait for the submitted AIOs to | |
2096 | // complete and report the error in the callback. | |
2097 | if (mcb->num_requests == 0) { | |
0f0b604b | 2098 | reqs[i].error = -EIO; |
40b4f539 KW |
2099 | goto fail; |
2100 | } else { | |
7eb58a6c KW |
2101 | mcb->num_requests++; |
2102 | multiwrite_cb(mcb, -EIO); | |
40b4f539 KW |
2103 | break; |
2104 | } | |
2105 | } else { | |
2106 | mcb->num_requests++; | |
2107 | } | |
2108 | } | |
2109 | ||
2110 | return 0; | |
2111 | ||
2112 | fail: | |
af474591 | 2113 | qemu_free(mcb); |
40b4f539 KW |
2114 | return -1; |
2115 | } | |
2116 | ||
b2e12bc6 CH |
2117 | BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, |
2118 | BlockDriverCompletionFunc *cb, void *opaque) | |
2119 | { | |
2120 | BlockDriver *drv = bs->drv; | |
2121 | ||
016f5cf6 AG |
2122 | if (bs->open_flags & BDRV_O_NO_FLUSH) { |
2123 | return bdrv_aio_noop_em(bs, cb, opaque); | |
2124 | } | |
2125 | ||
b2e12bc6 CH |
2126 | if (!drv) |
2127 | return NULL; | |
b2e12bc6 CH |
2128 | return drv->bdrv_aio_flush(bs, cb, opaque); |
2129 | } | |
2130 | ||
83f64091 | 2131 | void bdrv_aio_cancel(BlockDriverAIOCB *acb) |
83f64091 | 2132 | { |
6bbff9a0 | 2133 | acb->pool->cancel(acb); |
83f64091 FB |
2134 | } |
2135 | ||
ce1a14dc | 2136 | |
83f64091 FB |
2137 | /**************************************************************/ |
2138 | /* async block device emulation */ | |
2139 | ||
c16b5a2c CH |
2140 | typedef struct BlockDriverAIOCBSync { |
2141 | BlockDriverAIOCB common; | |
2142 | QEMUBH *bh; | |
2143 | int ret; | |
2144 | /* vector translation state */ | |
2145 | QEMUIOVector *qiov; | |
2146 | uint8_t *bounce; | |
2147 | int is_write; | |
2148 | } BlockDriverAIOCBSync; | |
2149 | ||
2150 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) | |
2151 | { | |
b666d239 KW |
2152 | BlockDriverAIOCBSync *acb = |
2153 | container_of(blockacb, BlockDriverAIOCBSync, common); | |
6a7ad299 | 2154 | qemu_bh_delete(acb->bh); |
36afc451 | 2155 | acb->bh = NULL; |
c16b5a2c CH |
2156 | qemu_aio_release(acb); |
2157 | } | |
2158 | ||
2159 | static AIOPool bdrv_em_aio_pool = { | |
2160 | .aiocb_size = sizeof(BlockDriverAIOCBSync), | |
2161 | .cancel = bdrv_aio_cancel_em, | |
2162 | }; | |
2163 | ||
ce1a14dc | 2164 | static void bdrv_aio_bh_cb(void *opaque) |
83f64091 | 2165 | { |
ce1a14dc | 2166 | BlockDriverAIOCBSync *acb = opaque; |
f141eafe | 2167 | |
f141eafe AL |
2168 | if (!acb->is_write) |
2169 | qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); | |
ceb42de8 | 2170 | qemu_vfree(acb->bounce); |
ce1a14dc | 2171 | acb->common.cb(acb->common.opaque, acb->ret); |
6a7ad299 | 2172 | qemu_bh_delete(acb->bh); |
36afc451 | 2173 | acb->bh = NULL; |
ce1a14dc | 2174 | qemu_aio_release(acb); |
83f64091 | 2175 | } |
beac80cd | 2176 | |
f141eafe AL |
2177 | static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, |
2178 | int64_t sector_num, | |
2179 | QEMUIOVector *qiov, | |
2180 | int nb_sectors, | |
2181 | BlockDriverCompletionFunc *cb, | |
2182 | void *opaque, | |
2183 | int is_write) | |
2184 | ||
83f64091 | 2185 | { |
ce1a14dc | 2186 | BlockDriverAIOCBSync *acb; |
ce1a14dc | 2187 | |
c16b5a2c | 2188 | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); |
f141eafe AL |
2189 | acb->is_write = is_write; |
2190 | acb->qiov = qiov; | |
e268ca52 | 2191 | acb->bounce = qemu_blockalign(bs, qiov->size); |
f141eafe | 2192 | |
ce1a14dc PB |
2193 | if (!acb->bh) |
2194 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); | |
f141eafe AL |
2195 | |
2196 | if (is_write) { | |
2197 | qemu_iovec_to_buffer(acb->qiov, acb->bounce); | |
2198 | acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors); | |
2199 | } else { | |
2200 | acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors); | |
2201 | } | |
2202 | ||
ce1a14dc | 2203 | qemu_bh_schedule(acb->bh); |
f141eafe | 2204 | |
ce1a14dc | 2205 | return &acb->common; |
beac80cd FB |
2206 | } |
2207 | ||
f141eafe AL |
2208 | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, |
2209 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
ce1a14dc | 2210 | BlockDriverCompletionFunc *cb, void *opaque) |
beac80cd | 2211 | { |
f141eafe AL |
2212 | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); |
2213 | } | |
83f64091 | 2214 | |
f141eafe AL |
2215 | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, |
2216 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
2217 | BlockDriverCompletionFunc *cb, void *opaque) | |
2218 | { | |
2219 | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); | |
beac80cd | 2220 | } |
beac80cd | 2221 | |
b2e12bc6 CH |
2222 | static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, |
2223 | BlockDriverCompletionFunc *cb, void *opaque) | |
2224 | { | |
2225 | BlockDriverAIOCBSync *acb; | |
2226 | ||
2227 | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); | |
2228 | acb->is_write = 1; /* don't bounce in the completion hadler */ | |
2229 | acb->qiov = NULL; | |
2230 | acb->bounce = NULL; | |
2231 | acb->ret = 0; | |
2232 | ||
2233 | if (!acb->bh) | |
2234 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); | |
2235 | ||
2236 | bdrv_flush(bs); | |
2237 | qemu_bh_schedule(acb->bh); | |
2238 | return &acb->common; | |
2239 | } | |
2240 | ||
016f5cf6 AG |
2241 | static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs, |
2242 | BlockDriverCompletionFunc *cb, void *opaque) | |
2243 | { | |
2244 | BlockDriverAIOCBSync *acb; | |
2245 | ||
2246 | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); | |
2247 | acb->is_write = 1; /* don't bounce in the completion handler */ | |
2248 | acb->qiov = NULL; | |
2249 | acb->bounce = NULL; | |
2250 | acb->ret = 0; | |
2251 | ||
2252 | if (!acb->bh) { | |
2253 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); | |
2254 | } | |
2255 | ||
2256 | qemu_bh_schedule(acb->bh); | |
2257 | return &acb->common; | |
2258 | } | |
2259 | ||
83f64091 FB |
2260 | /**************************************************************/ |
2261 | /* sync block device emulation */ | |
ea2384d3 | 2262 | |
83f64091 FB |
2263 | static void bdrv_rw_em_cb(void *opaque, int ret) |
2264 | { | |
2265 | *(int *)opaque = ret; | |
ea2384d3 FB |
2266 | } |
2267 | ||
83f64091 FB |
2268 | #define NOT_DONE 0x7fffffff |
2269 | ||
5fafdf24 | 2270 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
83f64091 | 2271 | uint8_t *buf, int nb_sectors) |
7a6cba61 | 2272 | { |
ce1a14dc PB |
2273 | int async_ret; |
2274 | BlockDriverAIOCB *acb; | |
f141eafe AL |
2275 | struct iovec iov; |
2276 | QEMUIOVector qiov; | |
83f64091 | 2277 | |
65d6b3d8 KW |
2278 | async_context_push(); |
2279 | ||
83f64091 | 2280 | async_ret = NOT_DONE; |
3f4cb3d3 | 2281 | iov.iov_base = (void *)buf; |
eb5a3165 | 2282 | iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; |
f141eafe AL |
2283 | qemu_iovec_init_external(&qiov, &iov, 1); |
2284 | acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors, | |
2285 | bdrv_rw_em_cb, &async_ret); | |
65d6b3d8 KW |
2286 | if (acb == NULL) { |
2287 | async_ret = -1; | |
2288 | goto fail; | |
2289 | } | |
baf35cb9 | 2290 | |
83f64091 FB |
2291 | while (async_ret == NOT_DONE) { |
2292 | qemu_aio_wait(); | |
2293 | } | |
baf35cb9 | 2294 | |
65d6b3d8 KW |
2295 | |
2296 | fail: | |
2297 | async_context_pop(); | |
83f64091 | 2298 | return async_ret; |
7a6cba61 PB |
2299 | } |
2300 | ||
83f64091 FB |
2301 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
2302 | const uint8_t *buf, int nb_sectors) | |
2303 | { | |
ce1a14dc PB |
2304 | int async_ret; |
2305 | BlockDriverAIOCB *acb; | |
f141eafe AL |
2306 | struct iovec iov; |
2307 | QEMUIOVector qiov; | |
83f64091 | 2308 | |
65d6b3d8 KW |
2309 | async_context_push(); |
2310 | ||
83f64091 | 2311 | async_ret = NOT_DONE; |
f141eafe | 2312 | iov.iov_base = (void *)buf; |
eb5a3165 | 2313 | iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; |
f141eafe AL |
2314 | qemu_iovec_init_external(&qiov, &iov, 1); |
2315 | acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors, | |
2316 | bdrv_rw_em_cb, &async_ret); | |
65d6b3d8 KW |
2317 | if (acb == NULL) { |
2318 | async_ret = -1; | |
2319 | goto fail; | |
2320 | } | |
83f64091 FB |
2321 | while (async_ret == NOT_DONE) { |
2322 | qemu_aio_wait(); | |
2323 | } | |
65d6b3d8 KW |
2324 | |
2325 | fail: | |
2326 | async_context_pop(); | |
83f64091 FB |
2327 | return async_ret; |
2328 | } | |
ea2384d3 FB |
2329 | |
2330 | void bdrv_init(void) | |
2331 | { | |
5efa9d5a | 2332 | module_call_init(MODULE_INIT_BLOCK); |
ea2384d3 | 2333 | } |
ce1a14dc | 2334 | |
eb852011 MA |
2335 | void bdrv_init_with_whitelist(void) |
2336 | { | |
2337 | use_bdrv_whitelist = 1; | |
2338 | bdrv_init(); | |
2339 | } | |
2340 | ||
c16b5a2c CH |
2341 | void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, |
2342 | BlockDriverCompletionFunc *cb, void *opaque) | |
ce1a14dc | 2343 | { |
ce1a14dc PB |
2344 | BlockDriverAIOCB *acb; |
2345 | ||
6bbff9a0 AL |
2346 | if (pool->free_aiocb) { |
2347 | acb = pool->free_aiocb; | |
2348 | pool->free_aiocb = acb->next; | |
ce1a14dc | 2349 | } else { |
6bbff9a0 AL |
2350 | acb = qemu_mallocz(pool->aiocb_size); |
2351 | acb->pool = pool; | |
ce1a14dc PB |
2352 | } |
2353 | acb->bs = bs; | |
2354 | acb->cb = cb; | |
2355 | acb->opaque = opaque; | |
2356 | return acb; | |
2357 | } | |
2358 | ||
2359 | void qemu_aio_release(void *p) | |
2360 | { | |
6bbff9a0 AL |
2361 | BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; |
2362 | AIOPool *pool = acb->pool; | |
2363 | acb->next = pool->free_aiocb; | |
2364 | pool->free_aiocb = acb; | |
ce1a14dc | 2365 | } |
19cb3738 FB |
2366 | |
2367 | /**************************************************************/ | |
2368 | /* removable device support */ | |
2369 | ||
2370 | /** | |
2371 | * Return TRUE if the media is present | |
2372 | */ | |
2373 | int bdrv_is_inserted(BlockDriverState *bs) | |
2374 | { | |
2375 | BlockDriver *drv = bs->drv; | |
2376 | int ret; | |
2377 | if (!drv) | |
2378 | return 0; | |
2379 | if (!drv->bdrv_is_inserted) | |
2380 | return 1; | |
2381 | ret = drv->bdrv_is_inserted(bs); | |
2382 | return ret; | |
2383 | } | |
2384 | ||
2385 | /** | |
2386 | * Return TRUE if the media changed since the last call to this | |
5fafdf24 | 2387 | * function. It is currently only used for floppy disks |
19cb3738 FB |
2388 | */ |
2389 | int bdrv_media_changed(BlockDriverState *bs) | |
2390 | { | |
2391 | BlockDriver *drv = bs->drv; | |
2392 | int ret; | |
2393 | ||
2394 | if (!drv || !drv->bdrv_media_changed) | |
2395 | ret = -ENOTSUP; | |
2396 | else | |
2397 | ret = drv->bdrv_media_changed(bs); | |
2398 | if (ret == -ENOTSUP) | |
2399 | ret = bs->media_changed; | |
2400 | bs->media_changed = 0; | |
2401 | return ret; | |
2402 | } | |
2403 | ||
2404 | /** | |
2405 | * If eject_flag is TRUE, eject the media. Otherwise, close the tray | |
2406 | */ | |
aea2a33c | 2407 | int bdrv_eject(BlockDriverState *bs, int eject_flag) |
19cb3738 FB |
2408 | { |
2409 | BlockDriver *drv = bs->drv; | |
2410 | int ret; | |
2411 | ||
aea2a33c MM |
2412 | if (bs->locked) { |
2413 | return -EBUSY; | |
2414 | } | |
2415 | ||
19cb3738 FB |
2416 | if (!drv || !drv->bdrv_eject) { |
2417 | ret = -ENOTSUP; | |
2418 | } else { | |
2419 | ret = drv->bdrv_eject(bs, eject_flag); | |
2420 | } | |
2421 | if (ret == -ENOTSUP) { | |
2422 | if (eject_flag) | |
2423 | bdrv_close(bs); | |
aea2a33c | 2424 | ret = 0; |
19cb3738 | 2425 | } |
aea2a33c MM |
2426 | |
2427 | return ret; | |
19cb3738 FB |
2428 | } |
2429 | ||
2430 | int bdrv_is_locked(BlockDriverState *bs) | |
2431 | { | |
2432 | return bs->locked; | |
2433 | } | |
2434 | ||
2435 | /** | |
2436 | * Lock or unlock the media (if it is locked, the user won't be able | |
2437 | * to eject it manually). | |
2438 | */ | |
2439 | void bdrv_set_locked(BlockDriverState *bs, int locked) | |
2440 | { | |
2441 | BlockDriver *drv = bs->drv; | |
2442 | ||
2443 | bs->locked = locked; | |
2444 | if (drv && drv->bdrv_set_locked) { | |
2445 | drv->bdrv_set_locked(bs, locked); | |
2446 | } | |
2447 | } | |
985a03b0 TS |
2448 | |
2449 | /* needed for generic scsi interface */ | |
2450 | ||
2451 | int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) | |
2452 | { | |
2453 | BlockDriver *drv = bs->drv; | |
2454 | ||
2455 | if (drv && drv->bdrv_ioctl) | |
2456 | return drv->bdrv_ioctl(bs, req, buf); | |
2457 | return -ENOTSUP; | |
2458 | } | |
7d780669 | 2459 | |
221f715d AL |
2460 | BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, |
2461 | unsigned long int req, void *buf, | |
2462 | BlockDriverCompletionFunc *cb, void *opaque) | |
7d780669 | 2463 | { |
221f715d | 2464 | BlockDriver *drv = bs->drv; |
7d780669 | 2465 | |
221f715d AL |
2466 | if (drv && drv->bdrv_aio_ioctl) |
2467 | return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); | |
2468 | return NULL; | |
7d780669 | 2469 | } |
e268ca52 | 2470 | |
7cd1e32a LS |
2471 | |
2472 | ||
e268ca52 AL |
2473 | void *qemu_blockalign(BlockDriverState *bs, size_t size) |
2474 | { | |
2475 | return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); | |
2476 | } | |
7cd1e32a LS |
2477 | |
2478 | void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) | |
2479 | { | |
2480 | int64_t bitmap_size; | |
a55eb92c | 2481 | |
aaa0eb75 | 2482 | bs->dirty_count = 0; |
a55eb92c | 2483 | if (enable) { |
c6d22830 JK |
2484 | if (!bs->dirty_bitmap) { |
2485 | bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + | |
2486 | BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; | |
2487 | bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8; | |
a55eb92c | 2488 | |
7cd1e32a | 2489 | bs->dirty_bitmap = qemu_mallocz(bitmap_size); |
a55eb92c | 2490 | } |
7cd1e32a | 2491 | } else { |
c6d22830 | 2492 | if (bs->dirty_bitmap) { |
7cd1e32a | 2493 | qemu_free(bs->dirty_bitmap); |
c6d22830 | 2494 | bs->dirty_bitmap = NULL; |
a55eb92c | 2495 | } |
7cd1e32a LS |
2496 | } |
2497 | } | |
2498 | ||
2499 | int bdrv_get_dirty(BlockDriverState *bs, int64_t sector) | |
2500 | { | |
6ea44308 | 2501 | int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; |
a55eb92c | 2502 | |
c6d22830 JK |
2503 | if (bs->dirty_bitmap && |
2504 | (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { | |
2505 | return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] & | |
2506 | (1 << (chunk % (sizeof(unsigned long) * 8))); | |
7cd1e32a LS |
2507 | } else { |
2508 | return 0; | |
2509 | } | |
2510 | } | |
2511 | ||
a55eb92c JK |
2512 | void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, |
2513 | int nr_sectors) | |
7cd1e32a LS |
2514 | { |
2515 | set_dirty_bitmap(bs, cur_sector, nr_sectors, 0); | |
2516 | } | |
aaa0eb75 LS |
2517 | |
2518 | int64_t bdrv_get_dirty_count(BlockDriverState *bs) | |
2519 | { | |
2520 | return bs->dirty_count; | |
2521 | } |