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