]>
Commit | Line | Data |
---|---|---|
fc01f7e7 FB |
1 | /* |
2 | * QEMU System Emulator block driver | |
3 | * | |
4 | * Copyright (c) 2003 Fabrice Bellard | |
5 | * | |
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 | */ | |
fc01f7e7 | 24 | #include "vl.h" |
ea2384d3 | 25 | #include "block_int.h" |
fc01f7e7 | 26 | |
7674e7bf FB |
27 | #ifdef _BSD |
28 | #include <sys/types.h> | |
29 | #include <sys/stat.h> | |
30 | #include <sys/ioctl.h> | |
31 | #include <sys/queue.h> | |
32 | #include <sys/disk.h> | |
33 | #endif | |
34 | ||
83f64091 FB |
35 | #define SECTOR_BITS 9 |
36 | #define SECTOR_SIZE (1 << SECTOR_BITS) | |
37 | ||
38 | static int bdrv_aio_new_em(BlockDriverAIOCB *acb); | |
39 | static int bdrv_aio_read_em(BlockDriverAIOCB *acb, int64_t sector_num, | |
40 | uint8_t *buf, int nb_sectors); | |
41 | static int bdrv_aio_write_em(BlockDriverAIOCB *acb, int64_t sector_num, | |
42 | const uint8_t *buf, int nb_sectors); | |
43 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb); | |
44 | static void bdrv_aio_delete_em(BlockDriverAIOCB *acb); | |
45 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, | |
46 | uint8_t *buf, int nb_sectors); | |
47 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, | |
48 | const uint8_t *buf, int nb_sectors); | |
ec530c81 | 49 | |
b338082b | 50 | static BlockDriverState *bdrv_first; |
ea2384d3 FB |
51 | static BlockDriver *first_drv; |
52 | ||
83f64091 FB |
53 | #ifdef _WIN32 |
54 | #define PATH_SEP '\\' | |
55 | #else | |
56 | #define PATH_SEP '/' | |
57 | #endif | |
3b0d4f61 | 58 | |
83f64091 | 59 | int path_is_absolute(const char *path) |
3b0d4f61 | 60 | { |
83f64091 FB |
61 | const char *p; |
62 | p = strchr(path, ':'); | |
63 | if (p) | |
64 | p++; | |
65 | else | |
66 | p = path; | |
67 | return (*p == PATH_SEP); | |
3b0d4f61 FB |
68 | } |
69 | ||
83f64091 FB |
70 | /* if filename is absolute, just copy it to dest. Otherwise, build a |
71 | path to it by considering it is relative to base_path. URL are | |
72 | supported. */ | |
73 | void path_combine(char *dest, int dest_size, | |
74 | const char *base_path, | |
75 | const char *filename) | |
3b0d4f61 | 76 | { |
83f64091 FB |
77 | const char *p, *p1; |
78 | int len; | |
79 | ||
80 | if (dest_size <= 0) | |
81 | return; | |
82 | if (path_is_absolute(filename)) { | |
83 | pstrcpy(dest, dest_size, filename); | |
84 | } else { | |
85 | p = strchr(base_path, ':'); | |
86 | if (p) | |
87 | p++; | |
88 | else | |
89 | p = base_path; | |
90 | p1 = strrchr(base_path, PATH_SEP); | |
91 | if (p1) | |
92 | p1++; | |
93 | else | |
94 | p1 = base_path; | |
95 | if (p1 > p) | |
96 | p = p1; | |
97 | len = p - base_path; | |
98 | if (len > dest_size - 1) | |
99 | len = dest_size - 1; | |
100 | memcpy(dest, base_path, len); | |
101 | dest[len] = '\0'; | |
102 | pstrcat(dest, dest_size, filename); | |
3b0d4f61 | 103 | } |
3b0d4f61 FB |
104 | } |
105 | ||
3b0d4f61 | 106 | |
ea2384d3 FB |
107 | void bdrv_register(BlockDriver *bdrv) |
108 | { | |
83f64091 FB |
109 | if (!bdrv->bdrv_aio_new) { |
110 | /* add AIO emulation layer */ | |
111 | bdrv->bdrv_aio_new = bdrv_aio_new_em; | |
112 | bdrv->bdrv_aio_read = bdrv_aio_read_em; | |
113 | bdrv->bdrv_aio_write = bdrv_aio_write_em; | |
114 | bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em; | |
115 | bdrv->bdrv_aio_delete = bdrv_aio_delete_em; | |
116 | } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) { | |
117 | /* add synchronous IO emulation layer */ | |
118 | bdrv->bdrv_read = bdrv_read_em; | |
119 | bdrv->bdrv_write = bdrv_write_em; | |
120 | } | |
ea2384d3 FB |
121 | bdrv->next = first_drv; |
122 | first_drv = bdrv; | |
123 | } | |
b338082b FB |
124 | |
125 | /* create a new block device (by default it is empty) */ | |
126 | BlockDriverState *bdrv_new(const char *device_name) | |
127 | { | |
128 | BlockDriverState **pbs, *bs; | |
129 | ||
130 | bs = qemu_mallocz(sizeof(BlockDriverState)); | |
131 | if(!bs) | |
132 | return NULL; | |
133 | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); | |
ea2384d3 FB |
134 | if (device_name[0] != '\0') { |
135 | /* insert at the end */ | |
136 | pbs = &bdrv_first; | |
137 | while (*pbs != NULL) | |
138 | pbs = &(*pbs)->next; | |
139 | *pbs = bs; | |
140 | } | |
b338082b FB |
141 | return bs; |
142 | } | |
143 | ||
ea2384d3 FB |
144 | BlockDriver *bdrv_find_format(const char *format_name) |
145 | { | |
146 | BlockDriver *drv1; | |
147 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { | |
148 | if (!strcmp(drv1->format_name, format_name)) | |
149 | return drv1; | |
150 | } | |
151 | return NULL; | |
152 | } | |
153 | ||
154 | int bdrv_create(BlockDriver *drv, | |
155 | const char *filename, int64_t size_in_sectors, | |
156 | const char *backing_file, int flags) | |
157 | { | |
158 | if (!drv->bdrv_create) | |
159 | return -ENOTSUP; | |
160 | return drv->bdrv_create(filename, size_in_sectors, backing_file, flags); | |
161 | } | |
162 | ||
d5249393 | 163 | #ifdef _WIN32 |
95389c86 | 164 | void get_tmp_filename(char *filename, int size) |
d5249393 | 165 | { |
83f64091 | 166 | tmpnam(filename); |
d5249393 FB |
167 | } |
168 | #else | |
95389c86 | 169 | void get_tmp_filename(char *filename, int size) |
fc01f7e7 | 170 | { |
67b915a5 | 171 | int fd; |
d5249393 | 172 | /* XXX: race condition possible */ |
ea2384d3 FB |
173 | pstrcpy(filename, size, "/tmp/vl.XXXXXX"); |
174 | fd = mkstemp(filename); | |
175 | close(fd); | |
176 | } | |
d5249393 | 177 | #endif |
fc01f7e7 | 178 | |
83f64091 FB |
179 | static BlockDriver *find_protocol(const char *filename) |
180 | { | |
181 | BlockDriver *drv1; | |
182 | char protocol[128]; | |
183 | int len; | |
184 | const char *p; | |
185 | p = strchr(filename, ':'); | |
186 | if (!p) | |
187 | return &bdrv_raw; | |
188 | len = p - filename; | |
189 | if (len > sizeof(protocol) - 1) | |
190 | len = sizeof(protocol) - 1; | |
191 | #ifdef _WIN32 | |
192 | if (len == 1) { | |
193 | /* specific win32 case for driver letters */ | |
194 | return &bdrv_raw; | |
195 | } | |
196 | #endif | |
197 | memcpy(protocol, filename, len); | |
198 | protocol[len] = '\0'; | |
199 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { | |
200 | if (drv1->protocol_name && | |
201 | !strcmp(drv1->protocol_name, protocol)) | |
202 | return drv1; | |
203 | } | |
204 | return NULL; | |
205 | } | |
206 | ||
7674e7bf FB |
207 | /* XXX: force raw format if block or character device ? It would |
208 | simplify the BSD case */ | |
ea2384d3 FB |
209 | static BlockDriver *find_image_format(const char *filename) |
210 | { | |
83f64091 | 211 | int ret, score, score_max; |
ea2384d3 | 212 | BlockDriver *drv1, *drv; |
83f64091 FB |
213 | uint8_t buf[2048]; |
214 | BlockDriverState *bs; | |
ea2384d3 | 215 | |
83f64091 FB |
216 | drv = find_protocol(filename); |
217 | /* no need to test disk image formats for vvfat or host specific | |
218 | devices */ | |
219 | if (drv == &bdrv_vvfat) | |
220 | return drv; | |
221 | if (strstart(filename, "/dev/", NULL)) | |
222 | return &bdrv_raw; | |
223 | ||
224 | ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY); | |
225 | if (ret < 0) | |
226 | return NULL; | |
227 | ret = bdrv_pread(bs, 0, buf, sizeof(buf)); | |
228 | bdrv_delete(bs); | |
229 | if (ret < 0) { | |
230 | return NULL; | |
231 | } | |
232 | ||
ea2384d3 FB |
233 | score_max = 0; |
234 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { | |
83f64091 FB |
235 | if (drv1->bdrv_probe) { |
236 | score = drv1->bdrv_probe(buf, ret, filename); | |
237 | if (score > score_max) { | |
238 | score_max = score; | |
239 | drv = drv1; | |
240 | } | |
0849bf08 | 241 | } |
fc01f7e7 | 242 | } |
ea2384d3 FB |
243 | return drv; |
244 | } | |
245 | ||
83f64091 | 246 | int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) |
ea2384d3 | 247 | { |
83f64091 FB |
248 | BlockDriverState *bs; |
249 | int ret; | |
250 | ||
251 | bs = bdrv_new(""); | |
252 | if (!bs) | |
253 | return -ENOMEM; | |
254 | ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL); | |
255 | if (ret < 0) { | |
256 | bdrv_delete(bs); | |
257 | return ret; | |
3b0d4f61 | 258 | } |
83f64091 FB |
259 | *pbs = bs; |
260 | return 0; | |
261 | } | |
262 | ||
263 | int bdrv_open(BlockDriverState *bs, const char *filename, int flags) | |
264 | { | |
265 | return bdrv_open2(bs, filename, flags, NULL); | |
ea2384d3 FB |
266 | } |
267 | ||
83f64091 | 268 | int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, |
ea2384d3 FB |
269 | BlockDriver *drv) |
270 | { | |
83f64091 | 271 | int ret, open_flags; |
ea2384d3 | 272 | char tmp_filename[1024]; |
83f64091 | 273 | char backing_filename[1024]; |
ea2384d3 FB |
274 | |
275 | bs->read_only = 0; | |
276 | bs->is_temporary = 0; | |
277 | bs->encrypted = 0; | |
712e7874 | 278 | |
83f64091 | 279 | if (flags & BDRV_O_SNAPSHOT) { |
ea2384d3 FB |
280 | BlockDriverState *bs1; |
281 | int64_t total_size; | |
282 | ||
283 | /* if snapshot, we create a temporary backing file and open it | |
284 | instead of opening 'filename' directly */ | |
33e3963e | 285 | |
ea2384d3 FB |
286 | /* if there is a backing file, use it */ |
287 | bs1 = bdrv_new(""); | |
288 | if (!bs1) { | |
83f64091 | 289 | return -ENOMEM; |
ea2384d3 FB |
290 | } |
291 | if (bdrv_open(bs1, filename, 0) < 0) { | |
292 | bdrv_delete(bs1); | |
293 | return -1; | |
294 | } | |
83f64091 | 295 | total_size = bdrv_getlength(bs1) >> SECTOR_BITS; |
ea2384d3 FB |
296 | bdrv_delete(bs1); |
297 | ||
298 | get_tmp_filename(tmp_filename, sizeof(tmp_filename)); | |
d15a771d | 299 | if (bdrv_create(&bdrv_qcow2, tmp_filename, |
ea2384d3 FB |
300 | total_size, filename, 0) < 0) { |
301 | return -1; | |
302 | } | |
303 | filename = tmp_filename; | |
304 | bs->is_temporary = 1; | |
305 | } | |
712e7874 | 306 | |
ea2384d3 | 307 | pstrcpy(bs->filename, sizeof(bs->filename), filename); |
83f64091 FB |
308 | if (flags & BDRV_O_FILE) { |
309 | drv = find_protocol(filename); | |
ea2384d3 | 310 | if (!drv) |
83f64091 FB |
311 | return -ENOENT; |
312 | } else { | |
313 | if (!drv) { | |
314 | drv = find_image_format(filename); | |
315 | if (!drv) | |
316 | return -1; | |
317 | } | |
ea2384d3 FB |
318 | } |
319 | bs->drv = drv; | |
320 | bs->opaque = qemu_mallocz(drv->instance_size); | |
321 | if (bs->opaque == NULL && drv->instance_size > 0) | |
322 | return -1; | |
83f64091 FB |
323 | /* Note: for compatibility, we open disk image files as RDWR, and |
324 | RDONLY as fallback */ | |
325 | if (!(flags & BDRV_O_FILE)) | |
326 | open_flags = BDRV_O_RDWR; | |
327 | else | |
328 | open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); | |
329 | ret = drv->bdrv_open(bs, filename, open_flags); | |
330 | if (ret == -EACCES && !(flags & BDRV_O_FILE)) { | |
331 | ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY); | |
332 | bs->read_only = 1; | |
333 | } | |
ea2384d3 FB |
334 | if (ret < 0) { |
335 | qemu_free(bs->opaque); | |
83f64091 | 336 | return ret; |
33e3963e | 337 | } |
d15a771d FB |
338 | if (drv->bdrv_getlength) { |
339 | bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; | |
340 | } | |
67b915a5 | 341 | #ifndef _WIN32 |
ea2384d3 FB |
342 | if (bs->is_temporary) { |
343 | unlink(filename); | |
344 | } | |
345 | #endif | |
83f64091 | 346 | if (bs->backing_file[0] != '\0') { |
ea2384d3 FB |
347 | /* if there is a backing file, use it */ |
348 | bs->backing_hd = bdrv_new(""); | |
349 | if (!bs->backing_hd) { | |
350 | fail: | |
351 | bdrv_close(bs); | |
352 | return -1; | |
33e3963e | 353 | } |
83f64091 FB |
354 | path_combine(backing_filename, sizeof(backing_filename), |
355 | filename, bs->backing_file); | |
356 | if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0) | |
33e3963e | 357 | goto fail; |
33e3963e FB |
358 | } |
359 | ||
b338082b FB |
360 | bs->inserted = 1; |
361 | ||
362 | /* call the change callback */ | |
363 | if (bs->change_cb) | |
364 | bs->change_cb(bs->change_opaque); | |
365 | ||
366 | return 0; | |
fc01f7e7 FB |
367 | } |
368 | ||
369 | void bdrv_close(BlockDriverState *bs) | |
370 | { | |
b338082b | 371 | if (bs->inserted) { |
ea2384d3 FB |
372 | if (bs->backing_hd) |
373 | bdrv_delete(bs->backing_hd); | |
374 | bs->drv->bdrv_close(bs); | |
375 | qemu_free(bs->opaque); | |
376 | #ifdef _WIN32 | |
377 | if (bs->is_temporary) { | |
378 | unlink(bs->filename); | |
379 | } | |
67b915a5 | 380 | #endif |
ea2384d3 FB |
381 | bs->opaque = NULL; |
382 | bs->drv = NULL; | |
b338082b FB |
383 | bs->inserted = 0; |
384 | ||
385 | /* call the change callback */ | |
386 | if (bs->change_cb) | |
387 | bs->change_cb(bs->change_opaque); | |
388 | } | |
389 | } | |
390 | ||
391 | void bdrv_delete(BlockDriverState *bs) | |
392 | { | |
ea2384d3 | 393 | /* XXX: remove the driver list */ |
b338082b FB |
394 | bdrv_close(bs); |
395 | qemu_free(bs); | |
fc01f7e7 FB |
396 | } |
397 | ||
33e3963e FB |
398 | /* commit COW file into the raw image */ |
399 | int bdrv_commit(BlockDriverState *bs) | |
400 | { | |
83f64091 | 401 | int64_t i, total_sectors; |
ea2384d3 FB |
402 | int n, j; |
403 | unsigned char sector[512]; | |
33e3963e | 404 | |
b338082b | 405 | if (!bs->inserted) |
ea2384d3 | 406 | return -ENOENT; |
33e3963e FB |
407 | |
408 | if (bs->read_only) { | |
ea2384d3 | 409 | return -EACCES; |
33e3963e FB |
410 | } |
411 | ||
ea2384d3 FB |
412 | if (!bs->backing_hd) { |
413 | return -ENOTSUP; | |
414 | } | |
33e3963e | 415 | |
83f64091 FB |
416 | total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; |
417 | for (i = 0; i < total_sectors;) { | |
ea2384d3 FB |
418 | if (bs->drv->bdrv_is_allocated(bs, i, 65536, &n)) { |
419 | for(j = 0; j < n; j++) { | |
420 | if (bdrv_read(bs, i, sector, 1) != 0) { | |
421 | return -EIO; | |
422 | } | |
423 | ||
424 | if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { | |
425 | return -EIO; | |
426 | } | |
427 | i++; | |
33e3963e | 428 | } |
ea2384d3 FB |
429 | } else { |
430 | i += n; | |
431 | } | |
33e3963e | 432 | } |
95389c86 FB |
433 | |
434 | if (bs->drv->bdrv_make_empty) | |
435 | return bs->drv->bdrv_make_empty(bs); | |
436 | ||
33e3963e FB |
437 | return 0; |
438 | } | |
439 | ||
83f64091 | 440 | /* return < 0 if error */ |
fc01f7e7 FB |
441 | int bdrv_read(BlockDriverState *bs, int64_t sector_num, |
442 | uint8_t *buf, int nb_sectors) | |
443 | { | |
ea2384d3 FB |
444 | BlockDriver *drv = bs->drv; |
445 | ||
b338082b FB |
446 | if (!bs->inserted) |
447 | return -1; | |
448 | ||
83f64091 | 449 | if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { |
cf98951b | 450 | memcpy(buf, bs->boot_sector_data, 512); |
83f64091 FB |
451 | sector_num++; |
452 | nb_sectors--; | |
453 | buf += 512; | |
454 | if (nb_sectors == 0) | |
455 | return 0; | |
456 | } | |
457 | if (drv->bdrv_pread) { | |
458 | int ret, len; | |
459 | len = nb_sectors * 512; | |
460 | ret = drv->bdrv_pread(bs, sector_num * 512, buf, len); | |
461 | if (ret < 0) | |
462 | return ret; | |
463 | else if (ret != len) | |
464 | return -EIO; | |
465 | else | |
466 | return 0; | |
467 | } else { | |
468 | return drv->bdrv_read(bs, sector_num, buf, nb_sectors); | |
33e3963e | 469 | } |
fc01f7e7 FB |
470 | } |
471 | ||
83f64091 | 472 | /* return < 0 if error */ |
fc01f7e7 FB |
473 | int bdrv_write(BlockDriverState *bs, int64_t sector_num, |
474 | const uint8_t *buf, int nb_sectors) | |
475 | { | |
83f64091 | 476 | BlockDriver *drv = bs->drv; |
b338082b FB |
477 | if (!bs->inserted) |
478 | return -1; | |
0849bf08 FB |
479 | if (bs->read_only) |
480 | return -1; | |
79639d42 FB |
481 | if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { |
482 | memcpy(bs->boot_sector_data, buf, 512); | |
483 | } | |
83f64091 FB |
484 | if (drv->bdrv_pwrite) { |
485 | int ret, len; | |
486 | len = nb_sectors * 512; | |
487 | ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len); | |
488 | if (ret < 0) | |
489 | return ret; | |
490 | else if (ret != len) | |
491 | return -EIO; | |
492 | else | |
493 | return 0; | |
494 | } else { | |
495 | return drv->bdrv_write(bs, sector_num, buf, nb_sectors); | |
496 | } | |
497 | } | |
498 | ||
83f64091 FB |
499 | /* not necessary now */ |
500 | static int bdrv_pread_em(BlockDriverState *bs, int64_t offset, | |
faea38e7 | 501 | uint8_t *buf, int count1) |
83f64091 | 502 | { |
83f64091 FB |
503 | uint8_t tmp_buf[SECTOR_SIZE]; |
504 | int len, nb_sectors, count; | |
505 | int64_t sector_num; | |
506 | ||
507 | count = count1; | |
508 | /* first read to align to sector start */ | |
509 | len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); | |
510 | if (len > count) | |
511 | len = count; | |
512 | sector_num = offset >> SECTOR_BITS; | |
513 | if (len > 0) { | |
514 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) | |
515 | return -EIO; | |
516 | memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len); | |
517 | count -= len; | |
518 | if (count == 0) | |
519 | return count1; | |
520 | sector_num++; | |
521 | buf += len; | |
522 | } | |
523 | ||
524 | /* read the sectors "in place" */ | |
525 | nb_sectors = count >> SECTOR_BITS; | |
526 | if (nb_sectors > 0) { | |
527 | if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0) | |
528 | return -EIO; | |
529 | sector_num += nb_sectors; | |
530 | len = nb_sectors << SECTOR_BITS; | |
531 | buf += len; | |
532 | count -= len; | |
533 | } | |
534 | ||
535 | /* add data from the last sector */ | |
536 | if (count > 0) { | |
537 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) | |
538 | return -EIO; | |
539 | memcpy(buf, tmp_buf, count); | |
540 | } | |
541 | return count1; | |
542 | } | |
543 | ||
544 | static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset, | |
faea38e7 | 545 | const uint8_t *buf, int count1) |
83f64091 | 546 | { |
83f64091 FB |
547 | uint8_t tmp_buf[SECTOR_SIZE]; |
548 | int len, nb_sectors, count; | |
549 | int64_t sector_num; | |
550 | ||
551 | count = count1; | |
552 | /* first write to align to sector start */ | |
553 | len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); | |
554 | if (len > count) | |
555 | len = count; | |
556 | sector_num = offset >> SECTOR_BITS; | |
557 | if (len > 0) { | |
558 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) | |
559 | return -EIO; | |
560 | memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len); | |
561 | if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) | |
562 | return -EIO; | |
563 | count -= len; | |
564 | if (count == 0) | |
565 | return count1; | |
566 | sector_num++; | |
567 | buf += len; | |
568 | } | |
569 | ||
570 | /* write the sectors "in place" */ | |
571 | nb_sectors = count >> SECTOR_BITS; | |
572 | if (nb_sectors > 0) { | |
573 | if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0) | |
574 | return -EIO; | |
575 | sector_num += nb_sectors; | |
576 | len = nb_sectors << SECTOR_BITS; | |
577 | buf += len; | |
578 | count -= len; | |
579 | } | |
580 | ||
581 | /* add data from the last sector */ | |
582 | if (count > 0) { | |
583 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) | |
584 | return -EIO; | |
585 | memcpy(tmp_buf, buf, count); | |
586 | if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) | |
587 | return -EIO; | |
588 | } | |
589 | return count1; | |
590 | } | |
83f64091 FB |
591 | |
592 | /** | |
593 | * Read with byte offsets (needed only for file protocols) | |
594 | */ | |
595 | int bdrv_pread(BlockDriverState *bs, int64_t offset, | |
596 | void *buf1, int count1) | |
597 | { | |
598 | BlockDriver *drv = bs->drv; | |
599 | ||
600 | if (!drv) | |
601 | return -ENOENT; | |
602 | if (!drv->bdrv_pread) | |
faea38e7 | 603 | return bdrv_pread_em(bs, offset, buf1, count1); |
83f64091 FB |
604 | return drv->bdrv_pread(bs, offset, buf1, count1); |
605 | } | |
606 | ||
607 | /** | |
608 | * Write with byte offsets (needed only for file protocols) | |
609 | */ | |
610 | int bdrv_pwrite(BlockDriverState *bs, int64_t offset, | |
611 | const void *buf1, int count1) | |
612 | { | |
613 | BlockDriver *drv = bs->drv; | |
614 | ||
615 | if (!drv) | |
616 | return -ENOENT; | |
617 | if (!drv->bdrv_pwrite) | |
faea38e7 | 618 | return bdrv_pwrite_em(bs, offset, buf1, count1); |
83f64091 FB |
619 | return drv->bdrv_pwrite(bs, offset, buf1, count1); |
620 | } | |
621 | ||
622 | /** | |
623 | * Truncate file to 'offset' bytes (needed only for file protocols) | |
624 | */ | |
625 | int bdrv_truncate(BlockDriverState *bs, int64_t offset) | |
626 | { | |
627 | BlockDriver *drv = bs->drv; | |
628 | if (!drv) | |
629 | return -ENOENT; | |
630 | if (!drv->bdrv_truncate) | |
631 | return -ENOTSUP; | |
632 | return drv->bdrv_truncate(bs, offset); | |
633 | } | |
634 | ||
635 | /** | |
636 | * Length of a file in bytes. Return < 0 if error or unknown. | |
637 | */ | |
638 | int64_t bdrv_getlength(BlockDriverState *bs) | |
639 | { | |
640 | BlockDriver *drv = bs->drv; | |
641 | if (!drv) | |
642 | return -ENOENT; | |
643 | if (!drv->bdrv_getlength) { | |
644 | /* legacy mode */ | |
645 | return bs->total_sectors * SECTOR_SIZE; | |
646 | } | |
647 | return drv->bdrv_getlength(bs); | |
fc01f7e7 FB |
648 | } |
649 | ||
650 | void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr) | |
651 | { | |
d15a771d | 652 | *nb_sectors_ptr = bs->total_sectors; |
fc01f7e7 | 653 | } |
cf98951b FB |
654 | |
655 | /* force a given boot sector. */ | |
656 | void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size) | |
657 | { | |
658 | bs->boot_sector_enabled = 1; | |
659 | if (size > 512) | |
660 | size = 512; | |
661 | memcpy(bs->boot_sector_data, data, size); | |
662 | memset(bs->boot_sector_data + size, 0, 512 - size); | |
663 | } | |
b338082b FB |
664 | |
665 | void bdrv_set_geometry_hint(BlockDriverState *bs, | |
666 | int cyls, int heads, int secs) | |
667 | { | |
668 | bs->cyls = cyls; | |
669 | bs->heads = heads; | |
670 | bs->secs = secs; | |
671 | } | |
672 | ||
673 | void bdrv_set_type_hint(BlockDriverState *bs, int type) | |
674 | { | |
675 | bs->type = type; | |
676 | bs->removable = ((type == BDRV_TYPE_CDROM || | |
677 | type == BDRV_TYPE_FLOPPY)); | |
678 | } | |
679 | ||
46d4767d FB |
680 | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
681 | { | |
682 | bs->translation = translation; | |
683 | } | |
684 | ||
b338082b FB |
685 | void bdrv_get_geometry_hint(BlockDriverState *bs, |
686 | int *pcyls, int *pheads, int *psecs) | |
687 | { | |
688 | *pcyls = bs->cyls; | |
689 | *pheads = bs->heads; | |
690 | *psecs = bs->secs; | |
691 | } | |
692 | ||
693 | int bdrv_get_type_hint(BlockDriverState *bs) | |
694 | { | |
695 | return bs->type; | |
696 | } | |
697 | ||
46d4767d FB |
698 | int bdrv_get_translation_hint(BlockDriverState *bs) |
699 | { | |
700 | return bs->translation; | |
701 | } | |
702 | ||
b338082b FB |
703 | int bdrv_is_removable(BlockDriverState *bs) |
704 | { | |
705 | return bs->removable; | |
706 | } | |
707 | ||
708 | int bdrv_is_read_only(BlockDriverState *bs) | |
709 | { | |
710 | return bs->read_only; | |
711 | } | |
712 | ||
713 | int bdrv_is_inserted(BlockDriverState *bs) | |
714 | { | |
715 | return bs->inserted; | |
716 | } | |
717 | ||
718 | int bdrv_is_locked(BlockDriverState *bs) | |
719 | { | |
720 | return bs->locked; | |
721 | } | |
722 | ||
723 | void bdrv_set_locked(BlockDriverState *bs, int locked) | |
724 | { | |
725 | bs->locked = locked; | |
726 | } | |
727 | ||
728 | void bdrv_set_change_cb(BlockDriverState *bs, | |
729 | void (*change_cb)(void *opaque), void *opaque) | |
730 | { | |
731 | bs->change_cb = change_cb; | |
732 | bs->change_opaque = opaque; | |
733 | } | |
734 | ||
ea2384d3 FB |
735 | int bdrv_is_encrypted(BlockDriverState *bs) |
736 | { | |
737 | if (bs->backing_hd && bs->backing_hd->encrypted) | |
738 | return 1; | |
739 | return bs->encrypted; | |
740 | } | |
741 | ||
742 | int bdrv_set_key(BlockDriverState *bs, const char *key) | |
743 | { | |
744 | int ret; | |
745 | if (bs->backing_hd && bs->backing_hd->encrypted) { | |
746 | ret = bdrv_set_key(bs->backing_hd, key); | |
747 | if (ret < 0) | |
748 | return ret; | |
749 | if (!bs->encrypted) | |
750 | return 0; | |
751 | } | |
752 | if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key) | |
753 | return -1; | |
754 | return bs->drv->bdrv_set_key(bs, key); | |
755 | } | |
756 | ||
757 | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) | |
758 | { | |
759 | if (!bs->inserted || !bs->drv) { | |
760 | buf[0] = '\0'; | |
761 | } else { | |
762 | pstrcpy(buf, buf_size, bs->drv->format_name); | |
763 | } | |
764 | } | |
765 | ||
766 | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), | |
767 | void *opaque) | |
768 | { | |
769 | BlockDriver *drv; | |
770 | ||
771 | for (drv = first_drv; drv != NULL; drv = drv->next) { | |
772 | it(opaque, drv->format_name); | |
773 | } | |
774 | } | |
775 | ||
b338082b FB |
776 | BlockDriverState *bdrv_find(const char *name) |
777 | { | |
778 | BlockDriverState *bs; | |
779 | ||
780 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { | |
781 | if (!strcmp(name, bs->device_name)) | |
782 | return bs; | |
783 | } | |
784 | return NULL; | |
785 | } | |
786 | ||
81d0912d FB |
787 | void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque) |
788 | { | |
789 | BlockDriverState *bs; | |
790 | ||
791 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { | |
792 | it(opaque, bs->device_name); | |
793 | } | |
794 | } | |
795 | ||
ea2384d3 FB |
796 | const char *bdrv_get_device_name(BlockDriverState *bs) |
797 | { | |
798 | return bs->device_name; | |
799 | } | |
800 | ||
7a6cba61 PB |
801 | void bdrv_flush(BlockDriverState *bs) |
802 | { | |
803 | if (bs->drv->bdrv_flush) | |
804 | bs->drv->bdrv_flush(bs); | |
805 | if (bs->backing_hd) | |
806 | bdrv_flush(bs->backing_hd); | |
807 | } | |
808 | ||
b338082b FB |
809 | void bdrv_info(void) |
810 | { | |
811 | BlockDriverState *bs; | |
812 | ||
813 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { | |
814 | term_printf("%s:", bs->device_name); | |
815 | term_printf(" type="); | |
816 | switch(bs->type) { | |
817 | case BDRV_TYPE_HD: | |
818 | term_printf("hd"); | |
819 | break; | |
820 | case BDRV_TYPE_CDROM: | |
821 | term_printf("cdrom"); | |
822 | break; | |
823 | case BDRV_TYPE_FLOPPY: | |
824 | term_printf("floppy"); | |
825 | break; | |
826 | } | |
827 | term_printf(" removable=%d", bs->removable); | |
828 | if (bs->removable) { | |
829 | term_printf(" locked=%d", bs->locked); | |
830 | } | |
831 | if (bs->inserted) { | |
832 | term_printf(" file=%s", bs->filename); | |
ea2384d3 FB |
833 | if (bs->backing_file[0] != '\0') |
834 | term_printf(" backing_file=%s", bs->backing_file); | |
b338082b | 835 | term_printf(" ro=%d", bs->read_only); |
ea2384d3 FB |
836 | term_printf(" drv=%s", bs->drv->format_name); |
837 | if (bs->encrypted) | |
838 | term_printf(" encrypted"); | |
b338082b FB |
839 | } else { |
840 | term_printf(" [not inserted]"); | |
841 | } | |
842 | term_printf("\n"); | |
843 | } | |
844 | } | |
ea2384d3 | 845 | |
83f64091 FB |
846 | void bdrv_get_backing_filename(BlockDriverState *bs, |
847 | char *filename, int filename_size) | |
848 | { | |
849 | if (!bs->backing_hd) { | |
850 | pstrcpy(filename, filename_size, ""); | |
851 | } else { | |
852 | pstrcpy(filename, filename_size, bs->backing_file); | |
853 | } | |
854 | } | |
855 | ||
faea38e7 FB |
856 | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, |
857 | const uint8_t *buf, int nb_sectors) | |
858 | { | |
859 | BlockDriver *drv = bs->drv; | |
860 | if (!drv) | |
861 | return -ENOENT; | |
862 | if (!drv->bdrv_write_compressed) | |
863 | return -ENOTSUP; | |
864 | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); | |
865 | } | |
866 | ||
867 | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) | |
868 | { | |
869 | BlockDriver *drv = bs->drv; | |
870 | if (!drv) | |
871 | return -ENOENT; | |
872 | if (!drv->bdrv_get_info) | |
873 | return -ENOTSUP; | |
874 | memset(bdi, 0, sizeof(*bdi)); | |
875 | return drv->bdrv_get_info(bs, bdi); | |
876 | } | |
877 | ||
878 | /**************************************************************/ | |
879 | /* handling of snapshots */ | |
880 | ||
881 | int bdrv_snapshot_create(BlockDriverState *bs, | |
882 | QEMUSnapshotInfo *sn_info) | |
883 | { | |
884 | BlockDriver *drv = bs->drv; | |
885 | if (!drv) | |
886 | return -ENOENT; | |
887 | if (!drv->bdrv_snapshot_create) | |
888 | return -ENOTSUP; | |
889 | return drv->bdrv_snapshot_create(bs, sn_info); | |
890 | } | |
891 | ||
892 | int bdrv_snapshot_goto(BlockDriverState *bs, | |
893 | const char *snapshot_id) | |
894 | { | |
895 | BlockDriver *drv = bs->drv; | |
896 | if (!drv) | |
897 | return -ENOENT; | |
898 | if (!drv->bdrv_snapshot_goto) | |
899 | return -ENOTSUP; | |
900 | return drv->bdrv_snapshot_goto(bs, snapshot_id); | |
901 | } | |
902 | ||
903 | int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) | |
904 | { | |
905 | BlockDriver *drv = bs->drv; | |
906 | if (!drv) | |
907 | return -ENOENT; | |
908 | if (!drv->bdrv_snapshot_delete) | |
909 | return -ENOTSUP; | |
910 | return drv->bdrv_snapshot_delete(bs, snapshot_id); | |
911 | } | |
912 | ||
913 | int bdrv_snapshot_list(BlockDriverState *bs, | |
914 | QEMUSnapshotInfo **psn_info) | |
915 | { | |
916 | BlockDriver *drv = bs->drv; | |
917 | if (!drv) | |
918 | return -ENOENT; | |
919 | if (!drv->bdrv_snapshot_list) | |
920 | return -ENOTSUP; | |
921 | return drv->bdrv_snapshot_list(bs, psn_info); | |
922 | } | |
923 | ||
924 | #define NB_SUFFIXES 4 | |
925 | ||
926 | char *get_human_readable_size(char *buf, int buf_size, int64_t size) | |
927 | { | |
928 | static const char suffixes[NB_SUFFIXES] = "KMGT"; | |
929 | int64_t base; | |
930 | int i; | |
931 | ||
932 | if (size <= 999) { | |
933 | snprintf(buf, buf_size, "%" PRId64, size); | |
934 | } else { | |
935 | base = 1024; | |
936 | for(i = 0; i < NB_SUFFIXES; i++) { | |
937 | if (size < (10 * base)) { | |
938 | snprintf(buf, buf_size, "%0.1f%c", | |
939 | (double)size / base, | |
940 | suffixes[i]); | |
941 | break; | |
942 | } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { | |
943 | snprintf(buf, buf_size, "%" PRId64 "%c", | |
944 | ((size + (base >> 1)) / base), | |
945 | suffixes[i]); | |
946 | break; | |
947 | } | |
948 | base = base * 1024; | |
949 | } | |
950 | } | |
951 | return buf; | |
952 | } | |
953 | ||
954 | char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) | |
955 | { | |
956 | char buf1[128], date_buf[128], clock_buf[128]; | |
957 | struct tm tm; | |
958 | time_t ti; | |
959 | int64_t secs; | |
960 | ||
961 | if (!sn) { | |
962 | snprintf(buf, buf_size, | |
963 | "%-10s%-20s%7s%20s%15s", | |
964 | "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); | |
965 | } else { | |
966 | ti = sn->date_sec; | |
967 | localtime_r(&ti, &tm); | |
968 | strftime(date_buf, sizeof(date_buf), | |
969 | "%Y-%m-%d %H:%M:%S", &tm); | |
970 | secs = sn->vm_clock_nsec / 1000000000; | |
971 | snprintf(clock_buf, sizeof(clock_buf), | |
972 | "%02d:%02d:%02d.%03d", | |
973 | (int)(secs / 3600), | |
974 | (int)((secs / 60) % 60), | |
975 | (int)(secs % 60), | |
976 | (int)((sn->vm_clock_nsec / 1000000) % 1000)); | |
977 | snprintf(buf, buf_size, | |
978 | "%-10s%-20s%7s%20s%15s", | |
979 | sn->id_str, sn->name, | |
980 | get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), | |
981 | date_buf, | |
982 | clock_buf); | |
983 | } | |
984 | return buf; | |
985 | } | |
986 | ||
83f64091 | 987 | |
ea2384d3 | 988 | /**************************************************************/ |
83f64091 | 989 | /* async I/Os */ |
ea2384d3 | 990 | |
83f64091 FB |
991 | BlockDriverAIOCB *bdrv_aio_new(BlockDriverState *bs) |
992 | { | |
993 | BlockDriver *drv = bs->drv; | |
994 | BlockDriverAIOCB *acb; | |
995 | acb = qemu_mallocz(sizeof(BlockDriverAIOCB)); | |
996 | if (!acb) | |
997 | return NULL; | |
998 | ||
999 | acb->bs = bs; | |
1000 | if (drv->bdrv_aio_new(acb) < 0) { | |
1001 | qemu_free(acb); | |
1002 | return NULL; | |
1003 | } | |
1004 | return acb; | |
1005 | } | |
ea2384d3 | 1006 | |
83f64091 FB |
1007 | int bdrv_aio_read(BlockDriverAIOCB *acb, int64_t sector_num, |
1008 | uint8_t *buf, int nb_sectors, | |
1009 | BlockDriverCompletionFunc *cb, void *opaque) | |
ea2384d3 | 1010 | { |
83f64091 FB |
1011 | BlockDriverState *bs = acb->bs; |
1012 | BlockDriver *drv = bs->drv; | |
1013 | ||
1014 | if (!bs->inserted) | |
1015 | return -1; | |
1016 | ||
1017 | /* XXX: we assume that nb_sectors == 0 is suppored by the async read */ | |
1018 | if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { | |
1019 | memcpy(buf, bs->boot_sector_data, 512); | |
1020 | sector_num++; | |
1021 | nb_sectors--; | |
1022 | buf += 512; | |
1023 | } | |
1024 | ||
1025 | acb->cb = cb; | |
1026 | acb->cb_opaque = opaque; | |
1027 | return drv->bdrv_aio_read(acb, sector_num, buf, nb_sectors); | |
ea2384d3 FB |
1028 | } |
1029 | ||
83f64091 FB |
1030 | int bdrv_aio_write(BlockDriverAIOCB *acb, int64_t sector_num, |
1031 | const uint8_t *buf, int nb_sectors, | |
1032 | BlockDriverCompletionFunc *cb, void *opaque) | |
ea2384d3 | 1033 | { |
83f64091 FB |
1034 | BlockDriverState *bs = acb->bs; |
1035 | BlockDriver *drv = bs->drv; | |
ea2384d3 | 1036 | |
83f64091 | 1037 | if (!bs->inserted) |
ea2384d3 | 1038 | return -1; |
83f64091 FB |
1039 | if (bs->read_only) |
1040 | return -1; | |
1041 | if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { | |
1042 | memcpy(bs->boot_sector_data, buf, 512); | |
ea2384d3 | 1043 | } |
83f64091 FB |
1044 | |
1045 | acb->cb = cb; | |
1046 | acb->cb_opaque = opaque; | |
1047 | return drv->bdrv_aio_write(acb, sector_num, buf, nb_sectors); | |
1048 | } | |
1049 | ||
1050 | void bdrv_aio_cancel(BlockDriverAIOCB *acb) | |
7674e7bf | 1051 | { |
83f64091 FB |
1052 | BlockDriverState *bs = acb->bs; |
1053 | BlockDriver *drv = bs->drv; | |
1054 | ||
1055 | drv->bdrv_aio_cancel(acb); | |
7674e7bf | 1056 | } |
83f64091 FB |
1057 | |
1058 | void bdrv_aio_delete(BlockDriverAIOCB *acb) | |
1059 | { | |
1060 | BlockDriverState *bs = acb->bs; | |
1061 | BlockDriver *drv = bs->drv; | |
1062 | ||
1063 | drv->bdrv_aio_delete(acb); | |
1064 | qemu_free(acb); | |
1065 | } | |
1066 | ||
1067 | /**************************************************************/ | |
1068 | /* async block device emulation */ | |
1069 | ||
1070 | #ifdef QEMU_TOOL | |
1071 | static int bdrv_aio_new_em(BlockDriverAIOCB *acb) | |
1072 | { | |
ea2384d3 FB |
1073 | return 0; |
1074 | } | |
1075 | ||
83f64091 | 1076 | static int bdrv_aio_read_em(BlockDriverAIOCB *acb, int64_t sector_num, |
ea2384d3 FB |
1077 | uint8_t *buf, int nb_sectors) |
1078 | { | |
ea2384d3 | 1079 | int ret; |
83f64091 FB |
1080 | ret = bdrv_read(acb->bs, sector_num, buf, nb_sectors); |
1081 | acb->cb(acb->cb_opaque, ret); | |
ea2384d3 FB |
1082 | return 0; |
1083 | } | |
1084 | ||
83f64091 | 1085 | static int bdrv_aio_write_em(BlockDriverAIOCB *acb, int64_t sector_num, |
ea2384d3 FB |
1086 | const uint8_t *buf, int nb_sectors) |
1087 | { | |
ea2384d3 | 1088 | int ret; |
83f64091 FB |
1089 | ret = bdrv_write(acb->bs, sector_num, buf, nb_sectors); |
1090 | acb->cb(acb->cb_opaque, ret); | |
ea2384d3 FB |
1091 | return 0; |
1092 | } | |
1093 | ||
83f64091 | 1094 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb) |
ea2384d3 | 1095 | { |
ea2384d3 FB |
1096 | } |
1097 | ||
83f64091 | 1098 | static void bdrv_aio_delete_em(BlockDriverAIOCB *acb) |
beac80cd | 1099 | { |
83f64091 FB |
1100 | } |
1101 | #else | |
1102 | typedef struct BlockDriverAIOCBSync { | |
1103 | QEMUBH *bh; | |
1104 | int ret; | |
1105 | } BlockDriverAIOCBSync; | |
beac80cd | 1106 | |
83f64091 FB |
1107 | static void bdrv_aio_bh_cb(void *opaque) |
1108 | { | |
1109 | BlockDriverAIOCB *acb = opaque; | |
1110 | BlockDriverAIOCBSync *acb1 = acb->opaque; | |
1111 | acb->cb(acb->cb_opaque, acb1->ret); | |
1112 | } | |
beac80cd | 1113 | |
83f64091 FB |
1114 | static int bdrv_aio_new_em(BlockDriverAIOCB *acb) |
1115 | { | |
1116 | BlockDriverAIOCBSync *acb1; | |
beac80cd | 1117 | |
83f64091 FB |
1118 | acb1 = qemu_mallocz(sizeof(BlockDriverAIOCBSync)); |
1119 | if (!acb1) | |
1120 | return -1; | |
1121 | acb->opaque = acb1; | |
1122 | acb1->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); | |
1123 | return 0; | |
1124 | } | |
beac80cd | 1125 | |
83f64091 FB |
1126 | static int bdrv_aio_read_em(BlockDriverAIOCB *acb, int64_t sector_num, |
1127 | uint8_t *buf, int nb_sectors) | |
1128 | { | |
1129 | BlockDriverAIOCBSync *acb1 = acb->opaque; | |
1130 | int ret; | |
1131 | ||
1132 | ret = bdrv_read(acb->bs, sector_num, buf, nb_sectors); | |
1133 | acb1->ret = ret; | |
1134 | qemu_bh_schedule(acb1->bh); | |
1135 | return 0; | |
beac80cd FB |
1136 | } |
1137 | ||
83f64091 FB |
1138 | static int bdrv_aio_write_em(BlockDriverAIOCB *acb, int64_t sector_num, |
1139 | const uint8_t *buf, int nb_sectors) | |
beac80cd | 1140 | { |
83f64091 FB |
1141 | BlockDriverAIOCBSync *acb1 = acb->opaque; |
1142 | int ret; | |
1143 | ||
1144 | ret = bdrv_write(acb->bs, sector_num, buf, nb_sectors); | |
1145 | acb1->ret = ret; | |
1146 | qemu_bh_schedule(acb1->bh); | |
1147 | return 0; | |
beac80cd | 1148 | } |
83f64091 FB |
1149 | |
1150 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb) | |
beac80cd | 1151 | { |
83f64091 FB |
1152 | BlockDriverAIOCBSync *acb1 = acb->opaque; |
1153 | qemu_bh_cancel(acb1->bh); | |
beac80cd | 1154 | } |
beac80cd | 1155 | |
83f64091 | 1156 | static void bdrv_aio_delete_em(BlockDriverAIOCB *acb) |
ea2384d3 | 1157 | { |
83f64091 FB |
1158 | BlockDriverAIOCBSync *acb1 = acb->opaque; |
1159 | qemu_bh_delete(acb1->bh); | |
1160 | } | |
1161 | #endif /* !QEMU_TOOL */ | |
ea2384d3 | 1162 | |
83f64091 FB |
1163 | /**************************************************************/ |
1164 | /* sync block device emulation */ | |
ea2384d3 | 1165 | |
83f64091 FB |
1166 | static void bdrv_rw_em_cb(void *opaque, int ret) |
1167 | { | |
1168 | *(int *)opaque = ret; | |
ea2384d3 FB |
1169 | } |
1170 | ||
83f64091 FB |
1171 | #define NOT_DONE 0x7fffffff |
1172 | ||
1173 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, | |
1174 | uint8_t *buf, int nb_sectors) | |
7a6cba61 | 1175 | { |
83f64091 FB |
1176 | int async_ret, ret; |
1177 | ||
1178 | if (!bs->sync_aiocb) { | |
1179 | bs->sync_aiocb = bdrv_aio_new(bs); | |
1180 | if (!bs->sync_aiocb) | |
1181 | return -1; | |
1182 | } | |
1183 | async_ret = NOT_DONE; | |
1184 | qemu_aio_wait_start(); | |
1185 | ret = bdrv_aio_read(bs->sync_aiocb, sector_num, buf, nb_sectors, | |
1186 | bdrv_rw_em_cb, &async_ret); | |
1187 | if (ret < 0) { | |
1188 | qemu_aio_wait_end(); | |
1189 | return ret; | |
1190 | } | |
1191 | while (async_ret == NOT_DONE) { | |
1192 | qemu_aio_wait(); | |
1193 | } | |
1194 | qemu_aio_wait_end(); | |
1195 | return async_ret; | |
7a6cba61 PB |
1196 | } |
1197 | ||
83f64091 FB |
1198 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
1199 | const uint8_t *buf, int nb_sectors) | |
1200 | { | |
1201 | int async_ret, ret; | |
1202 | ||
1203 | if (!bs->sync_aiocb) { | |
1204 | bs->sync_aiocb = bdrv_aio_new(bs); | |
1205 | if (!bs->sync_aiocb) | |
1206 | return -1; | |
1207 | } | |
1208 | async_ret = NOT_DONE; | |
1209 | qemu_aio_wait_start(); | |
1210 | ret = bdrv_aio_write(bs->sync_aiocb, sector_num, buf, nb_sectors, | |
1211 | bdrv_rw_em_cb, &async_ret); | |
1212 | if (ret < 0) { | |
1213 | qemu_aio_wait_end(); | |
1214 | return ret; | |
1215 | } | |
1216 | while (async_ret == NOT_DONE) { | |
1217 | qemu_aio_wait(); | |
1218 | } | |
1219 | qemu_aio_wait_end(); | |
1220 | return async_ret; | |
1221 | } | |
ea2384d3 FB |
1222 | |
1223 | void bdrv_init(void) | |
1224 | { | |
1225 | bdrv_register(&bdrv_raw); | |
1226 | #ifndef _WIN32 | |
1227 | bdrv_register(&bdrv_cow); | |
1228 | #endif | |
1229 | bdrv_register(&bdrv_qcow); | |
1230 | bdrv_register(&bdrv_vmdk); | |
3c56521b | 1231 | bdrv_register(&bdrv_cloop); |
585d0ed9 | 1232 | bdrv_register(&bdrv_dmg); |
a8753c34 | 1233 | bdrv_register(&bdrv_bochs); |
6a0f9e82 | 1234 | bdrv_register(&bdrv_vpc); |
712e7874 | 1235 | bdrv_register(&bdrv_vvfat); |
faea38e7 | 1236 | bdrv_register(&bdrv_qcow2); |
ea2384d3 | 1237 | } |