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