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