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