Remove OpenBIOS ESP patch. Merged upstream.
[qemu/mini2440.git] / block.c
blobd81451058e50eff0980995566c729ed09c1ca49a
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "vl.h"
25 #include "block_int.h"
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
35 #define SECTOR_BITS 9
36 #define SECTOR_SIZE (1 << SECTOR_BITS)
38 typedef struct BlockDriverAIOCBSync {
39 BlockDriverAIOCB common;
40 QEMUBH *bh;
41 int ret;
42 } BlockDriverAIOCBSync;
44 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
45 int64_t sector_num, uint8_t *buf, int nb_sectors,
46 BlockDriverCompletionFunc *cb, void *opaque);
47 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
48 int64_t sector_num, const uint8_t *buf, int nb_sectors,
49 BlockDriverCompletionFunc *cb, void *opaque);
50 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
51 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
52 uint8_t *buf, int nb_sectors);
53 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
54 const uint8_t *buf, int nb_sectors);
56 static BlockDriverState *bdrv_first;
57 static BlockDriver *first_drv;
59 #ifdef _WIN32
60 #define PATH_SEP '\\'
61 #else
62 #define PATH_SEP '/'
63 #endif
65 int path_is_absolute(const char *path)
67 const char *p;
68 p = strchr(path, ':');
69 if (p)
70 p++;
71 else
72 p = path;
73 return (*p == PATH_SEP);
76 /* if filename is absolute, just copy it to dest. Otherwise, build a
77 path to it by considering it is relative to base_path. URL are
78 supported. */
79 void path_combine(char *dest, int dest_size,
80 const char *base_path,
81 const char *filename)
83 const char *p, *p1;
84 int len;
86 if (dest_size <= 0)
87 return;
88 if (path_is_absolute(filename)) {
89 pstrcpy(dest, dest_size, filename);
90 } else {
91 p = strchr(base_path, ':');
92 if (p)
93 p++;
94 else
95 p = base_path;
96 p1 = strrchr(base_path, PATH_SEP);
97 if (p1)
98 p1++;
99 else
100 p1 = base_path;
101 if (p1 > p)
102 p = p1;
103 len = p - base_path;
104 if (len > dest_size - 1)
105 len = dest_size - 1;
106 memcpy(dest, base_path, len);
107 dest[len] = '\0';
108 pstrcat(dest, dest_size, filename);
113 void bdrv_register(BlockDriver *bdrv)
115 if (!bdrv->bdrv_aio_read) {
116 /* add AIO emulation layer */
117 bdrv->bdrv_aio_read = bdrv_aio_read_em;
118 bdrv->bdrv_aio_write = bdrv_aio_write_em;
119 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
120 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
121 } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
122 /* add synchronous IO emulation layer */
123 bdrv->bdrv_read = bdrv_read_em;
124 bdrv->bdrv_write = bdrv_write_em;
126 bdrv->next = first_drv;
127 first_drv = bdrv;
130 /* create a new block device (by default it is empty) */
131 BlockDriverState *bdrv_new(const char *device_name)
133 BlockDriverState **pbs, *bs;
135 bs = qemu_mallocz(sizeof(BlockDriverState));
136 if(!bs)
137 return NULL;
138 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
139 if (device_name[0] != '\0') {
140 /* insert at the end */
141 pbs = &bdrv_first;
142 while (*pbs != NULL)
143 pbs = &(*pbs)->next;
144 *pbs = bs;
146 return bs;
149 BlockDriver *bdrv_find_format(const char *format_name)
151 BlockDriver *drv1;
152 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
153 if (!strcmp(drv1->format_name, format_name))
154 return drv1;
156 return NULL;
159 int bdrv_create(BlockDriver *drv,
160 const char *filename, int64_t size_in_sectors,
161 const char *backing_file, int flags)
163 if (!drv->bdrv_create)
164 return -ENOTSUP;
165 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
168 #ifdef _WIN32
169 void get_tmp_filename(char *filename, int size)
171 tmpnam(filename);
173 #else
174 void get_tmp_filename(char *filename, int size)
176 int fd;
177 /* XXX: race condition possible */
178 pstrcpy(filename, size, "/tmp/vl.XXXXXX");
179 fd = mkstemp(filename);
180 close(fd);
182 #endif
184 static BlockDriver *find_protocol(const char *filename)
186 BlockDriver *drv1;
187 char protocol[128];
188 int len;
189 const char *p;
190 p = strchr(filename, ':');
191 if (!p)
192 return &bdrv_raw;
193 len = p - filename;
194 if (len > sizeof(protocol) - 1)
195 len = sizeof(protocol) - 1;
196 #ifdef _WIN32
197 if (len == 1) {
198 /* specific win32 case for driver letters */
199 return &bdrv_raw;
201 #endif
202 memcpy(protocol, filename, len);
203 protocol[len] = '\0';
204 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
205 if (drv1->protocol_name &&
206 !strcmp(drv1->protocol_name, protocol))
207 return drv1;
209 return NULL;
212 /* XXX: force raw format if block or character device ? It would
213 simplify the BSD case */
214 static BlockDriver *find_image_format(const char *filename)
216 int ret, score, score_max;
217 BlockDriver *drv1, *drv;
218 uint8_t buf[2048];
219 BlockDriverState *bs;
221 drv = find_protocol(filename);
222 /* no need to test disk image formats for vvfat or host specific
223 devices */
224 if (drv == &bdrv_vvfat)
225 return drv;
226 if (strstart(filename, "/dev/", NULL))
227 return &bdrv_raw;
229 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
230 if (ret < 0)
231 return NULL;
232 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
233 bdrv_delete(bs);
234 if (ret < 0) {
235 return NULL;
238 score_max = 0;
239 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
240 if (drv1->bdrv_probe) {
241 score = drv1->bdrv_probe(buf, ret, filename);
242 if (score > score_max) {
243 score_max = score;
244 drv = drv1;
248 return drv;
251 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
253 BlockDriverState *bs;
254 int ret;
256 bs = bdrv_new("");
257 if (!bs)
258 return -ENOMEM;
259 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
260 if (ret < 0) {
261 bdrv_delete(bs);
262 return ret;
264 *pbs = bs;
265 return 0;
268 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
270 return bdrv_open2(bs, filename, flags, NULL);
273 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
274 BlockDriver *drv)
276 int ret, open_flags;
277 char tmp_filename[1024];
278 char backing_filename[1024];
280 bs->read_only = 0;
281 bs->is_temporary = 0;
282 bs->encrypted = 0;
284 if (flags & BDRV_O_SNAPSHOT) {
285 BlockDriverState *bs1;
286 int64_t total_size;
288 /* if snapshot, we create a temporary backing file and open it
289 instead of opening 'filename' directly */
291 /* if there is a backing file, use it */
292 bs1 = bdrv_new("");
293 if (!bs1) {
294 return -ENOMEM;
296 if (bdrv_open(bs1, filename, 0) < 0) {
297 bdrv_delete(bs1);
298 return -1;
300 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
301 bdrv_delete(bs1);
303 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
304 if (bdrv_create(&bdrv_qcow2, tmp_filename,
305 total_size, filename, 0) < 0) {
306 return -1;
308 filename = tmp_filename;
309 bs->is_temporary = 1;
312 pstrcpy(bs->filename, sizeof(bs->filename), filename);
313 if (flags & BDRV_O_FILE) {
314 drv = find_protocol(filename);
315 if (!drv)
316 return -ENOENT;
317 } else {
318 if (!drv) {
319 drv = find_image_format(filename);
320 if (!drv)
321 return -1;
324 bs->drv = drv;
325 bs->opaque = qemu_mallocz(drv->instance_size);
326 if (bs->opaque == NULL && drv->instance_size > 0)
327 return -1;
328 /* Note: for compatibility, we open disk image files as RDWR, and
329 RDONLY as fallback */
330 if (!(flags & BDRV_O_FILE))
331 open_flags = BDRV_O_RDWR;
332 else
333 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
334 ret = drv->bdrv_open(bs, filename, open_flags);
335 if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
336 ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
337 bs->read_only = 1;
339 if (ret < 0) {
340 qemu_free(bs->opaque);
341 return ret;
343 if (drv->bdrv_getlength) {
344 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
346 #ifndef _WIN32
347 if (bs->is_temporary) {
348 unlink(filename);
350 #endif
351 if (bs->backing_file[0] != '\0') {
352 /* if there is a backing file, use it */
353 bs->backing_hd = bdrv_new("");
354 if (!bs->backing_hd) {
355 fail:
356 bdrv_close(bs);
357 return -1;
359 path_combine(backing_filename, sizeof(backing_filename),
360 filename, bs->backing_file);
361 if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
362 goto fail;
365 bs->inserted = 1;
367 /* call the change callback */
368 if (bs->change_cb)
369 bs->change_cb(bs->change_opaque);
371 return 0;
374 void bdrv_close(BlockDriverState *bs)
376 if (bs->inserted) {
377 if (bs->backing_hd)
378 bdrv_delete(bs->backing_hd);
379 bs->drv->bdrv_close(bs);
380 qemu_free(bs->opaque);
381 #ifdef _WIN32
382 if (bs->is_temporary) {
383 unlink(bs->filename);
385 #endif
386 bs->opaque = NULL;
387 bs->drv = NULL;
388 bs->inserted = 0;
390 /* call the change callback */
391 if (bs->change_cb)
392 bs->change_cb(bs->change_opaque);
396 void bdrv_delete(BlockDriverState *bs)
398 /* XXX: remove the driver list */
399 bdrv_close(bs);
400 qemu_free(bs);
403 /* commit COW file into the raw image */
404 int bdrv_commit(BlockDriverState *bs)
406 int64_t i, total_sectors;
407 int n, j;
408 unsigned char sector[512];
410 if (!bs->inserted)
411 return -ENOENT;
413 if (bs->read_only) {
414 return -EACCES;
417 if (!bs->backing_hd) {
418 return -ENOTSUP;
421 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
422 for (i = 0; i < total_sectors;) {
423 if (bs->drv->bdrv_is_allocated(bs, i, 65536, &n)) {
424 for(j = 0; j < n; j++) {
425 if (bdrv_read(bs, i, sector, 1) != 0) {
426 return -EIO;
429 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
430 return -EIO;
432 i++;
434 } else {
435 i += n;
439 if (bs->drv->bdrv_make_empty)
440 return bs->drv->bdrv_make_empty(bs);
442 return 0;
445 /* return < 0 if error */
446 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
447 uint8_t *buf, int nb_sectors)
449 BlockDriver *drv = bs->drv;
451 if (!bs->inserted)
452 return -1;
454 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
455 memcpy(buf, bs->boot_sector_data, 512);
456 sector_num++;
457 nb_sectors--;
458 buf += 512;
459 if (nb_sectors == 0)
460 return 0;
462 if (drv->bdrv_pread) {
463 int ret, len;
464 len = nb_sectors * 512;
465 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
466 if (ret < 0)
467 return ret;
468 else if (ret != len)
469 return -EIO;
470 else
471 return 0;
472 } else {
473 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
477 /* return < 0 if error */
478 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
479 const uint8_t *buf, int nb_sectors)
481 BlockDriver *drv = bs->drv;
482 if (!bs->inserted)
483 return -1;
484 if (bs->read_only)
485 return -1;
486 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
487 memcpy(bs->boot_sector_data, buf, 512);
489 if (drv->bdrv_pwrite) {
490 int ret, len;
491 len = nb_sectors * 512;
492 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
493 if (ret < 0)
494 return ret;
495 else if (ret != len)
496 return -EIO;
497 else
498 return 0;
499 } else {
500 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
504 /* not necessary now */
505 static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
506 uint8_t *buf, int count1)
508 uint8_t tmp_buf[SECTOR_SIZE];
509 int len, nb_sectors, count;
510 int64_t sector_num;
512 count = count1;
513 /* first read to align to sector start */
514 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
515 if (len > count)
516 len = count;
517 sector_num = offset >> SECTOR_BITS;
518 if (len > 0) {
519 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
520 return -EIO;
521 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
522 count -= len;
523 if (count == 0)
524 return count1;
525 sector_num++;
526 buf += len;
529 /* read the sectors "in place" */
530 nb_sectors = count >> SECTOR_BITS;
531 if (nb_sectors > 0) {
532 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
533 return -EIO;
534 sector_num += nb_sectors;
535 len = nb_sectors << SECTOR_BITS;
536 buf += len;
537 count -= len;
540 /* add data from the last sector */
541 if (count > 0) {
542 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
543 return -EIO;
544 memcpy(buf, tmp_buf, count);
546 return count1;
549 static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
550 const uint8_t *buf, int count1)
552 uint8_t tmp_buf[SECTOR_SIZE];
553 int len, nb_sectors, count;
554 int64_t sector_num;
556 count = count1;
557 /* first write to align to sector start */
558 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
559 if (len > count)
560 len = count;
561 sector_num = offset >> SECTOR_BITS;
562 if (len > 0) {
563 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
564 return -EIO;
565 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
566 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
567 return -EIO;
568 count -= len;
569 if (count == 0)
570 return count1;
571 sector_num++;
572 buf += len;
575 /* write the sectors "in place" */
576 nb_sectors = count >> SECTOR_BITS;
577 if (nb_sectors > 0) {
578 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
579 return -EIO;
580 sector_num += nb_sectors;
581 len = nb_sectors << SECTOR_BITS;
582 buf += len;
583 count -= len;
586 /* add data from the last sector */
587 if (count > 0) {
588 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
589 return -EIO;
590 memcpy(tmp_buf, buf, count);
591 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
592 return -EIO;
594 return count1;
598 * Read with byte offsets (needed only for file protocols)
600 int bdrv_pread(BlockDriverState *bs, int64_t offset,
601 void *buf1, int count1)
603 BlockDriver *drv = bs->drv;
605 if (!drv)
606 return -ENOENT;
607 if (!drv->bdrv_pread)
608 return bdrv_pread_em(bs, offset, buf1, count1);
609 return drv->bdrv_pread(bs, offset, buf1, count1);
612 /**
613 * Write with byte offsets (needed only for file protocols)
615 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
616 const void *buf1, int count1)
618 BlockDriver *drv = bs->drv;
620 if (!drv)
621 return -ENOENT;
622 if (!drv->bdrv_pwrite)
623 return bdrv_pwrite_em(bs, offset, buf1, count1);
624 return drv->bdrv_pwrite(bs, offset, buf1, count1);
628 * Truncate file to 'offset' bytes (needed only for file protocols)
630 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
632 BlockDriver *drv = bs->drv;
633 if (!drv)
634 return -ENOENT;
635 if (!drv->bdrv_truncate)
636 return -ENOTSUP;
637 return drv->bdrv_truncate(bs, offset);
641 * Length of a file in bytes. Return < 0 if error or unknown.
643 int64_t bdrv_getlength(BlockDriverState *bs)
645 BlockDriver *drv = bs->drv;
646 if (!drv)
647 return -ENOENT;
648 if (!drv->bdrv_getlength) {
649 /* legacy mode */
650 return bs->total_sectors * SECTOR_SIZE;
652 return drv->bdrv_getlength(bs);
655 void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
657 *nb_sectors_ptr = bs->total_sectors;
660 /* force a given boot sector. */
661 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
663 bs->boot_sector_enabled = 1;
664 if (size > 512)
665 size = 512;
666 memcpy(bs->boot_sector_data, data, size);
667 memset(bs->boot_sector_data + size, 0, 512 - size);
670 void bdrv_set_geometry_hint(BlockDriverState *bs,
671 int cyls, int heads, int secs)
673 bs->cyls = cyls;
674 bs->heads = heads;
675 bs->secs = secs;
678 void bdrv_set_type_hint(BlockDriverState *bs, int type)
680 bs->type = type;
681 bs->removable = ((type == BDRV_TYPE_CDROM ||
682 type == BDRV_TYPE_FLOPPY));
685 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
687 bs->translation = translation;
690 void bdrv_get_geometry_hint(BlockDriverState *bs,
691 int *pcyls, int *pheads, int *psecs)
693 *pcyls = bs->cyls;
694 *pheads = bs->heads;
695 *psecs = bs->secs;
698 int bdrv_get_type_hint(BlockDriverState *bs)
700 return bs->type;
703 int bdrv_get_translation_hint(BlockDriverState *bs)
705 return bs->translation;
708 int bdrv_is_removable(BlockDriverState *bs)
710 return bs->removable;
713 int bdrv_is_read_only(BlockDriverState *bs)
715 return bs->read_only;
718 int bdrv_is_inserted(BlockDriverState *bs)
720 return bs->inserted;
723 int bdrv_is_locked(BlockDriverState *bs)
725 return bs->locked;
728 void bdrv_set_locked(BlockDriverState *bs, int locked)
730 bs->locked = locked;
733 void bdrv_set_change_cb(BlockDriverState *bs,
734 void (*change_cb)(void *opaque), void *opaque)
736 bs->change_cb = change_cb;
737 bs->change_opaque = opaque;
740 int bdrv_is_encrypted(BlockDriverState *bs)
742 if (bs->backing_hd && bs->backing_hd->encrypted)
743 return 1;
744 return bs->encrypted;
747 int bdrv_set_key(BlockDriverState *bs, const char *key)
749 int ret;
750 if (bs->backing_hd && bs->backing_hd->encrypted) {
751 ret = bdrv_set_key(bs->backing_hd, key);
752 if (ret < 0)
753 return ret;
754 if (!bs->encrypted)
755 return 0;
757 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
758 return -1;
759 return bs->drv->bdrv_set_key(bs, key);
762 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
764 if (!bs->inserted || !bs->drv) {
765 buf[0] = '\0';
766 } else {
767 pstrcpy(buf, buf_size, bs->drv->format_name);
771 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
772 void *opaque)
774 BlockDriver *drv;
776 for (drv = first_drv; drv != NULL; drv = drv->next) {
777 it(opaque, drv->format_name);
781 BlockDriverState *bdrv_find(const char *name)
783 BlockDriverState *bs;
785 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
786 if (!strcmp(name, bs->device_name))
787 return bs;
789 return NULL;
792 void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
794 BlockDriverState *bs;
796 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
797 it(opaque, bs->device_name);
801 const char *bdrv_get_device_name(BlockDriverState *bs)
803 return bs->device_name;
806 void bdrv_flush(BlockDriverState *bs)
808 if (bs->drv->bdrv_flush)
809 bs->drv->bdrv_flush(bs);
810 if (bs->backing_hd)
811 bdrv_flush(bs->backing_hd);
814 void bdrv_info(void)
816 BlockDriverState *bs;
818 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
819 term_printf("%s:", bs->device_name);
820 term_printf(" type=");
821 switch(bs->type) {
822 case BDRV_TYPE_HD:
823 term_printf("hd");
824 break;
825 case BDRV_TYPE_CDROM:
826 term_printf("cdrom");
827 break;
828 case BDRV_TYPE_FLOPPY:
829 term_printf("floppy");
830 break;
832 term_printf(" removable=%d", bs->removable);
833 if (bs->removable) {
834 term_printf(" locked=%d", bs->locked);
836 if (bs->inserted) {
837 term_printf(" file=%s", bs->filename);
838 if (bs->backing_file[0] != '\0')
839 term_printf(" backing_file=%s", bs->backing_file);
840 term_printf(" ro=%d", bs->read_only);
841 term_printf(" drv=%s", bs->drv->format_name);
842 if (bs->encrypted)
843 term_printf(" encrypted");
844 } else {
845 term_printf(" [not inserted]");
847 term_printf("\n");
851 void bdrv_get_backing_filename(BlockDriverState *bs,
852 char *filename, int filename_size)
854 if (!bs->backing_hd) {
855 pstrcpy(filename, filename_size, "");
856 } else {
857 pstrcpy(filename, filename_size, bs->backing_file);
861 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
862 const uint8_t *buf, int nb_sectors)
864 BlockDriver *drv = bs->drv;
865 if (!drv)
866 return -ENOENT;
867 if (!drv->bdrv_write_compressed)
868 return -ENOTSUP;
869 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
872 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
874 BlockDriver *drv = bs->drv;
875 if (!drv)
876 return -ENOENT;
877 if (!drv->bdrv_get_info)
878 return -ENOTSUP;
879 memset(bdi, 0, sizeof(*bdi));
880 return drv->bdrv_get_info(bs, bdi);
883 /**************************************************************/
884 /* handling of snapshots */
886 int bdrv_snapshot_create(BlockDriverState *bs,
887 QEMUSnapshotInfo *sn_info)
889 BlockDriver *drv = bs->drv;
890 if (!drv)
891 return -ENOENT;
892 if (!drv->bdrv_snapshot_create)
893 return -ENOTSUP;
894 return drv->bdrv_snapshot_create(bs, sn_info);
897 int bdrv_snapshot_goto(BlockDriverState *bs,
898 const char *snapshot_id)
900 BlockDriver *drv = bs->drv;
901 if (!drv)
902 return -ENOENT;
903 if (!drv->bdrv_snapshot_goto)
904 return -ENOTSUP;
905 return drv->bdrv_snapshot_goto(bs, snapshot_id);
908 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
910 BlockDriver *drv = bs->drv;
911 if (!drv)
912 return -ENOENT;
913 if (!drv->bdrv_snapshot_delete)
914 return -ENOTSUP;
915 return drv->bdrv_snapshot_delete(bs, snapshot_id);
918 int bdrv_snapshot_list(BlockDriverState *bs,
919 QEMUSnapshotInfo **psn_info)
921 BlockDriver *drv = bs->drv;
922 if (!drv)
923 return -ENOENT;
924 if (!drv->bdrv_snapshot_list)
925 return -ENOTSUP;
926 return drv->bdrv_snapshot_list(bs, psn_info);
929 #define NB_SUFFIXES 4
931 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
933 static const char suffixes[NB_SUFFIXES] = "KMGT";
934 int64_t base;
935 int i;
937 if (size <= 999) {
938 snprintf(buf, buf_size, "%" PRId64, size);
939 } else {
940 base = 1024;
941 for(i = 0; i < NB_SUFFIXES; i++) {
942 if (size < (10 * base)) {
943 snprintf(buf, buf_size, "%0.1f%c",
944 (double)size / base,
945 suffixes[i]);
946 break;
947 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
948 snprintf(buf, buf_size, "%" PRId64 "%c",
949 ((size + (base >> 1)) / base),
950 suffixes[i]);
951 break;
953 base = base * 1024;
956 return buf;
959 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
961 char buf1[128], date_buf[128], clock_buf[128];
962 struct tm tm;
963 time_t ti;
964 int64_t secs;
966 if (!sn) {
967 snprintf(buf, buf_size,
968 "%-10s%-20s%7s%20s%15s",
969 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
970 } else {
971 ti = sn->date_sec;
972 #ifndef _WIN32
973 localtime_r(&ti, &tm);
974 #endif
975 strftime(date_buf, sizeof(date_buf),
976 "%Y-%m-%d %H:%M:%S", &tm);
977 secs = sn->vm_clock_nsec / 1000000000;
978 snprintf(clock_buf, sizeof(clock_buf),
979 "%02d:%02d:%02d.%03d",
980 (int)(secs / 3600),
981 (int)((secs / 60) % 60),
982 (int)(secs % 60),
983 (int)((sn->vm_clock_nsec / 1000000) % 1000));
984 snprintf(buf, buf_size,
985 "%-10s%-20s%7s%20s%15s",
986 sn->id_str, sn->name,
987 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
988 date_buf,
989 clock_buf);
991 return buf;
995 /**************************************************************/
996 /* async I/Os */
998 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
999 uint8_t *buf, int nb_sectors,
1000 BlockDriverCompletionFunc *cb, void *opaque)
1002 BlockDriver *drv = bs->drv;
1004 if (!bs->inserted)
1005 return NULL;
1007 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1008 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1009 memcpy(buf, bs->boot_sector_data, 512);
1010 sector_num++;
1011 nb_sectors--;
1012 buf += 512;
1015 return drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1018 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1019 const uint8_t *buf, int nb_sectors,
1020 BlockDriverCompletionFunc *cb, void *opaque)
1022 BlockDriver *drv = bs->drv;
1024 if (!bs->inserted)
1025 return NULL;
1026 if (bs->read_only)
1027 return NULL;
1028 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1029 memcpy(bs->boot_sector_data, buf, 512);
1032 return drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1035 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1037 BlockDriver *drv = acb->bs->drv;
1039 drv->bdrv_aio_cancel(acb);
1043 /**************************************************************/
1044 /* async block device emulation */
1046 #ifdef QEMU_TOOL
1047 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1048 int64_t sector_num, uint8_t *buf, int nb_sectors,
1049 BlockDriverCompletionFunc *cb, void *opaque)
1051 int ret;
1052 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1053 cb(opaque, ret);
1054 return NULL;
1057 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1058 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1059 BlockDriverCompletionFunc *cb, void *opaque)
1061 int ret;
1062 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1063 cb(opaque, ret);
1064 return NULL;
1067 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
1070 #else
1071 static void bdrv_aio_bh_cb(void *opaque)
1073 BlockDriverAIOCBSync *acb = opaque;
1074 acb->common.cb(acb->common.opaque, acb->ret);
1075 qemu_aio_release(acb);
1078 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1079 int64_t sector_num, uint8_t *buf, int nb_sectors,
1080 BlockDriverCompletionFunc *cb, void *opaque)
1082 BlockDriverAIOCBSync *acb;
1083 int ret;
1085 acb = qemu_aio_get(bs, cb, opaque);
1086 if (!acb->bh)
1087 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1088 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1089 acb->ret = ret;
1090 qemu_bh_schedule(acb->bh);
1091 return &acb->common;
1094 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1095 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1096 BlockDriverCompletionFunc *cb, void *opaque)
1098 BlockDriverAIOCBSync *acb;
1099 int ret;
1101 acb = qemu_aio_get(bs, cb, opaque);
1102 if (!acb->bh)
1103 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1104 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1105 acb->ret = ret;
1106 qemu_bh_schedule(acb->bh);
1107 return &acb->common;
1110 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1112 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1113 qemu_bh_cancel(acb->bh);
1114 qemu_aio_release(acb);
1116 #endif /* !QEMU_TOOL */
1118 /**************************************************************/
1119 /* sync block device emulation */
1121 static void bdrv_rw_em_cb(void *opaque, int ret)
1123 *(int *)opaque = ret;
1126 #define NOT_DONE 0x7fffffff
1128 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1129 uint8_t *buf, int nb_sectors)
1131 int async_ret;
1132 BlockDriverAIOCB *acb;
1134 async_ret = NOT_DONE;
1135 qemu_aio_wait_start();
1136 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1137 bdrv_rw_em_cb, &async_ret);
1138 if (acb == NULL) {
1139 qemu_aio_wait_end();
1140 return -1;
1142 while (async_ret == NOT_DONE) {
1143 qemu_aio_wait();
1145 qemu_aio_wait_end();
1146 return async_ret;
1149 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1150 const uint8_t *buf, int nb_sectors)
1152 int async_ret;
1153 BlockDriverAIOCB *acb;
1155 async_ret = NOT_DONE;
1156 qemu_aio_wait_start();
1157 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1158 bdrv_rw_em_cb, &async_ret);
1159 if (acb == NULL) {
1160 qemu_aio_wait_end();
1161 return -1;
1163 while (async_ret == NOT_DONE) {
1164 qemu_aio_wait();
1166 qemu_aio_wait_end();
1167 return async_ret;
1170 void bdrv_init(void)
1172 bdrv_register(&bdrv_raw);
1173 #ifndef _WIN32
1174 bdrv_register(&bdrv_cow);
1175 #endif
1176 bdrv_register(&bdrv_qcow);
1177 bdrv_register(&bdrv_vmdk);
1178 bdrv_register(&bdrv_cloop);
1179 bdrv_register(&bdrv_dmg);
1180 bdrv_register(&bdrv_bochs);
1181 bdrv_register(&bdrv_vpc);
1182 bdrv_register(&bdrv_vvfat);
1183 bdrv_register(&bdrv_qcow2);
1186 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1187 void *opaque)
1189 BlockDriver *drv;
1190 BlockDriverAIOCB *acb;
1192 drv = bs->drv;
1193 if (drv->free_aiocb) {
1194 acb = drv->free_aiocb;
1195 drv->free_aiocb = acb->next;
1196 } else {
1197 acb = qemu_mallocz(drv->aiocb_size);
1198 if (!acb)
1199 return NULL;
1201 acb->bs = bs;
1202 acb->cb = cb;
1203 acb->opaque = opaque;
1204 return acb;
1207 void qemu_aio_release(void *p)
1209 BlockDriverAIOCB *acb = p;
1210 BlockDriver *drv = acb->bs->drv;
1211 acb->next = drv->free_aiocb;
1212 drv->free_aiocb = acb;