Manipulate the gpe bits and send sci up the os
[qemu-kvm/fedora.git] / block.c
blob492a4d35eeb35a6ddfc77fea5d2e3eb198b504ba
1 /*
2 * QEMU System Emulator block driver
4 * Copyright (c) 2003 Fabrice Bellard
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 "qemu-common.h"
25 #ifndef QEMU_IMG
26 #include "console.h"
27 #endif
28 #include "block_int.h"
29 #include "osdep.h"
31 #ifdef _BSD
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/ioctl.h>
35 #include <sys/queue.h>
36 #include <sys/disk.h>
37 #endif
39 #define SECTOR_BITS 9
40 #define SECTOR_SIZE (1 << SECTOR_BITS)
42 typedef struct BlockDriverAIOCBSync {
43 BlockDriverAIOCB common;
44 QEMUBH *bh;
45 int ret;
46 } BlockDriverAIOCBSync;
48 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
49 int64_t sector_num, uint8_t *buf, int nb_sectors,
50 BlockDriverCompletionFunc *cb, void *opaque);
51 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
52 int64_t sector_num, const uint8_t *buf, int nb_sectors,
53 BlockDriverCompletionFunc *cb, void *opaque);
54 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
55 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
56 uint8_t *buf, int nb_sectors);
57 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
58 const uint8_t *buf, int nb_sectors);
60 BlockDriverState *bdrv_first;
61 static BlockDriver *first_drv;
63 int path_is_absolute(const char *path)
65 const char *p;
66 #ifdef _WIN32
67 /* specific case for names like: "\\.\d:" */
68 if (*path == '/' || *path == '\\')
69 return 1;
70 #endif
71 p = strchr(path, ':');
72 if (p)
73 p++;
74 else
75 p = path;
76 #ifdef _WIN32
77 return (*p == '/' || *p == '\\');
78 #else
79 return (*p == '/');
80 #endif
83 /* if filename is absolute, just copy it to dest. Otherwise, build a
84 path to it by considering it is relative to base_path. URL are
85 supported. */
86 void path_combine(char *dest, int dest_size,
87 const char *base_path,
88 const char *filename)
90 const char *p, *p1;
91 int len;
93 if (dest_size <= 0)
94 return;
95 if (path_is_absolute(filename)) {
96 pstrcpy(dest, dest_size, filename);
97 } else {
98 p = strchr(base_path, ':');
99 if (p)
100 p++;
101 else
102 p = base_path;
103 p1 = strrchr(base_path, '/');
104 #ifdef _WIN32
106 const char *p2;
107 p2 = strrchr(base_path, '\\');
108 if (!p1 || p2 > p1)
109 p1 = p2;
111 #endif
112 if (p1)
113 p1++;
114 else
115 p1 = base_path;
116 if (p1 > p)
117 p = p1;
118 len = p - base_path;
119 if (len > dest_size - 1)
120 len = dest_size - 1;
121 memcpy(dest, base_path, len);
122 dest[len] = '\0';
123 pstrcat(dest, dest_size, filename);
128 static void bdrv_register(BlockDriver *bdrv)
130 if (!bdrv->bdrv_aio_read) {
131 /* add AIO emulation layer */
132 bdrv->bdrv_aio_read = bdrv_aio_read_em;
133 bdrv->bdrv_aio_write = bdrv_aio_write_em;
134 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
135 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
136 } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
137 /* add synchronous IO emulation layer */
138 bdrv->bdrv_read = bdrv_read_em;
139 bdrv->bdrv_write = bdrv_write_em;
141 bdrv->next = first_drv;
142 first_drv = bdrv;
145 /* create a new block device (by default it is empty) */
146 BlockDriverState *bdrv_new(const char *device_name)
148 BlockDriverState **pbs, *bs;
150 bs = qemu_mallocz(sizeof(BlockDriverState));
151 if(!bs)
152 return NULL;
153 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
154 if (device_name[0] != '\0') {
155 /* insert at the end */
156 pbs = &bdrv_first;
157 while (*pbs != NULL)
158 pbs = &(*pbs)->next;
159 *pbs = bs;
161 return bs;
164 BlockDriver *bdrv_find_format(const char *format_name)
166 BlockDriver *drv1;
167 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
168 if (!strcmp(drv1->format_name, format_name))
169 return drv1;
171 return NULL;
174 int bdrv_create(BlockDriver *drv,
175 const char *filename, int64_t size_in_sectors,
176 const char *backing_file, int flags)
178 if (!drv->bdrv_create)
179 return -ENOTSUP;
180 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
183 #ifdef _WIN32
184 void get_tmp_filename(char *filename, int size)
186 char temp_dir[MAX_PATH];
188 GetTempPath(MAX_PATH, temp_dir);
189 GetTempFileName(temp_dir, "qem", 0, filename);
191 #else
192 void get_tmp_filename(char *filename, int size)
194 int fd;
195 /* XXX: race condition possible */
196 pstrcpy(filename, size, "/tmp/vl.XXXXXX");
197 fd = mkstemp(filename);
198 close(fd);
200 #endif
202 #ifdef _WIN32
203 static int is_windows_drive_prefix(const char *filename)
205 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
206 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
207 filename[1] == ':');
210 static int is_windows_drive(const char *filename)
212 if (is_windows_drive_prefix(filename) &&
213 filename[2] == '\0')
214 return 1;
215 if (strstart(filename, "\\\\.\\", NULL) ||
216 strstart(filename, "//./", NULL))
217 return 1;
218 return 0;
220 #endif
222 static BlockDriver *find_protocol(const char *filename)
224 BlockDriver *drv1;
225 char protocol[128];
226 int len;
227 const char *p;
229 #ifdef _WIN32
230 if (is_windows_drive(filename) ||
231 is_windows_drive_prefix(filename))
232 return &bdrv_raw;
233 #endif
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;
240 memcpy(protocol, filename, len);
241 protocol[len] = '\0';
242 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
243 if (drv1->protocol_name &&
244 !strcmp(drv1->protocol_name, protocol))
245 return drv1;
247 return NULL;
250 /* XXX: force raw format if block or character device ? It would
251 simplify the BSD case */
252 static BlockDriver *find_image_format(const char *filename)
254 int ret, score, score_max;
255 BlockDriver *drv1, *drv;
256 uint8_t buf[2048];
257 BlockDriverState *bs;
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
268 struct stat st;
269 if (stat(filename, &st) >= 0 &&
270 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
271 return &bdrv_host_device;
274 #endif
276 drv = find_protocol(filename);
277 /* no need to test disk image formats for vvfat */
278 if (drv == &bdrv_vvfat)
279 return drv;
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;
290 score_max = 0;
291 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
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;
300 return drv;
303 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
305 BlockDriverState *bs;
306 int ret;
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;
316 *pbs = bs;
317 return 0;
320 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
322 return bdrv_open2(bs, filename, flags, NULL);
325 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
326 BlockDriver *drv)
328 int ret, open_flags;
329 char tmp_filename[PATH_MAX];
330 char backing_filename[PATH_MAX];
332 bs->read_only = 0;
333 bs->is_temporary = 0;
334 bs->encrypted = 0;
336 if (flags & BDRV_O_SNAPSHOT) {
337 BlockDriverState *bs1;
338 int64_t total_size;
340 /* if snapshot, we create a temporary backing file and open it
341 instead of opening 'filename' directly */
343 /* if there is a backing file, use it */
344 bs1 = bdrv_new("");
345 if (!bs1) {
346 return -ENOMEM;
348 if (bdrv_open(bs1, filename, 0) < 0) {
349 bdrv_delete(bs1);
350 return -1;
352 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
353 bdrv_delete(bs1);
355 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
356 realpath(filename, backing_filename);
357 if (bdrv_create(&bdrv_qcow2, tmp_filename,
358 total_size, backing_filename, 0) < 0) {
359 return -1;
361 filename = tmp_filename;
362 bs->is_temporary = 1;
365 pstrcpy(bs->filename, sizeof(bs->filename), filename);
366 if (flags & BDRV_O_FILE) {
367 drv = find_protocol(filename);
368 if (!drv)
369 return -ENOENT;
370 } else {
371 if (!drv) {
372 drv = find_image_format(filename);
373 if (!drv)
374 return -1;
377 bs->drv = drv;
378 bs->opaque = qemu_mallocz(drv->instance_size);
379 if (bs->opaque == NULL && drv->instance_size > 0)
380 return -1;
381 /* Note: for compatibility, we open disk image files as RDWR, and
382 RDONLY as fallback */
383 if (!(flags & BDRV_O_FILE))
384 open_flags = BDRV_O_RDWR | (flags & BDRV_O_DIRECT);
385 else
386 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
387 ret = drv->bdrv_open(bs, filename, open_flags);
388 if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
389 ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
390 bs->read_only = 1;
392 if (ret < 0) {
393 qemu_free(bs->opaque);
394 bs->opaque = NULL;
395 bs->drv = NULL;
396 return ret;
398 if (drv->bdrv_getlength) {
399 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
401 #ifndef _WIN32
402 if (bs->is_temporary) {
403 unlink(filename);
405 #endif
406 if (bs->backing_file[0] != '\0') {
407 /* if there is a backing file, use it */
408 bs->backing_hd = bdrv_new("");
409 if (!bs->backing_hd) {
410 fail:
411 bdrv_close(bs);
412 return -ENOMEM;
414 path_combine(backing_filename, sizeof(backing_filename),
415 filename, bs->backing_file);
416 if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
417 goto fail;
420 /* call the change callback */
421 bs->media_changed = 1;
422 if (bs->change_cb)
423 bs->change_cb(bs->change_opaque);
425 return 0;
428 void bdrv_close(BlockDriverState *bs)
430 if (bs->drv) {
431 if (bs->backing_hd)
432 bdrv_delete(bs->backing_hd);
433 bs->drv->bdrv_close(bs);
434 qemu_free(bs->opaque);
435 #ifdef _WIN32
436 if (bs->is_temporary) {
437 unlink(bs->filename);
439 #endif
440 bs->opaque = NULL;
441 bs->drv = NULL;
443 /* call the change callback */
444 bs->media_changed = 1;
445 if (bs->change_cb)
446 bs->change_cb(bs->change_opaque);
450 void bdrv_delete(BlockDriverState *bs)
452 /* XXX: remove the driver list */
453 bdrv_close(bs);
454 qemu_free(bs);
457 /* commit COW file into the raw image */
458 int bdrv_commit(BlockDriverState *bs)
460 BlockDriver *drv = bs->drv;
461 int64_t i, total_sectors;
462 int n, j;
463 unsigned char sector[512];
465 if (!drv)
466 return -ENOMEDIUM;
468 if (bs->read_only) {
469 return -EACCES;
472 if (!bs->backing_hd) {
473 return -ENOTSUP;
476 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
477 for (i = 0; i < total_sectors;) {
478 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
479 for(j = 0; j < n; j++) {
480 if (bdrv_read(bs, i, sector, 1) != 0) {
481 return -EIO;
484 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
485 return -EIO;
487 i++;
489 } else {
490 i += n;
494 if (drv->bdrv_make_empty)
495 return drv->bdrv_make_empty(bs);
497 return 0;
500 /* return < 0 if error. See bdrv_write() for the return codes */
501 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
502 uint8_t *buf, int nb_sectors)
504 BlockDriver *drv = bs->drv;
506 if (!drv)
507 return -ENOMEDIUM;
509 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
510 memcpy(buf, bs->boot_sector_data, 512);
511 sector_num++;
512 nb_sectors--;
513 buf += 512;
514 if (nb_sectors == 0)
515 return 0;
517 if (drv->bdrv_pread) {
518 int ret, len;
519 len = nb_sectors * 512;
520 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
521 if (ret < 0)
522 return ret;
523 else if (ret != len)
524 return -EINVAL;
525 else {
526 bs->rd_bytes += (unsigned) len;
527 bs->rd_ops ++;
528 return 0;
530 } else {
531 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
535 /* Return < 0 if error. Important errors are:
536 -EIO generic I/O error (may happen for all errors)
537 -ENOMEDIUM No media inserted.
538 -EINVAL Invalid sector number or nb_sectors
539 -EACCES Trying to write a read-only device
541 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
542 const uint8_t *buf, int nb_sectors)
544 BlockDriver *drv = bs->drv;
545 if (!bs->drv)
546 return -ENOMEDIUM;
547 if (bs->read_only)
548 return -EACCES;
549 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
550 memcpy(bs->boot_sector_data, buf, 512);
552 if (drv->bdrv_pwrite) {
553 int ret, len;
554 len = nb_sectors * 512;
555 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
556 if (ret < 0)
557 return ret;
558 else if (ret != len)
559 return -EIO;
560 else {
561 bs->wr_bytes += (unsigned) len;
562 bs->wr_ops ++;
563 return 0;
565 } else {
566 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
570 static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
571 uint8_t *buf, int count1)
573 uint8_t tmp_buf[SECTOR_SIZE];
574 int len, nb_sectors, count;
575 int64_t sector_num;
577 count = count1;
578 /* first read to align to sector start */
579 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
580 if (len > count)
581 len = count;
582 sector_num = offset >> SECTOR_BITS;
583 if (len > 0) {
584 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
585 return -EIO;
586 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
587 count -= len;
588 if (count == 0)
589 return count1;
590 sector_num++;
591 buf += len;
594 /* read the sectors "in place" */
595 nb_sectors = count >> SECTOR_BITS;
596 if (nb_sectors > 0) {
597 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
598 return -EIO;
599 sector_num += nb_sectors;
600 len = nb_sectors << SECTOR_BITS;
601 buf += len;
602 count -= len;
605 /* add data from the last sector */
606 if (count > 0) {
607 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
608 return -EIO;
609 memcpy(buf, tmp_buf, count);
611 return count1;
614 static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
615 const uint8_t *buf, int count1)
617 uint8_t tmp_buf[SECTOR_SIZE];
618 int len, nb_sectors, count;
619 int64_t sector_num;
621 count = count1;
622 /* first write to align to sector start */
623 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
624 if (len > count)
625 len = count;
626 sector_num = offset >> SECTOR_BITS;
627 if (len > 0) {
628 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
629 return -EIO;
630 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
631 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
632 return -EIO;
633 count -= len;
634 if (count == 0)
635 return count1;
636 sector_num++;
637 buf += len;
640 /* write the sectors "in place" */
641 nb_sectors = count >> SECTOR_BITS;
642 if (nb_sectors > 0) {
643 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
644 return -EIO;
645 sector_num += nb_sectors;
646 len = nb_sectors << SECTOR_BITS;
647 buf += len;
648 count -= len;
651 /* add data from the last sector */
652 if (count > 0) {
653 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
654 return -EIO;
655 memcpy(tmp_buf, buf, count);
656 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
657 return -EIO;
659 return count1;
663 * Read with byte offsets (needed only for file protocols)
665 int bdrv_pread(BlockDriverState *bs, int64_t offset,
666 void *buf1, int count1)
668 BlockDriver *drv = bs->drv;
670 if (!drv)
671 return -ENOMEDIUM;
672 if (!drv->bdrv_pread)
673 return bdrv_pread_em(bs, offset, buf1, count1);
674 return drv->bdrv_pread(bs, offset, buf1, count1);
678 * Write with byte offsets (needed only for file protocols)
680 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
681 const void *buf1, int count1)
683 BlockDriver *drv = bs->drv;
685 if (!drv)
686 return -ENOMEDIUM;
687 if (!drv->bdrv_pwrite)
688 return bdrv_pwrite_em(bs, offset, buf1, count1);
689 return drv->bdrv_pwrite(bs, offset, buf1, count1);
693 * Truncate file to 'offset' bytes (needed only for file protocols)
695 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
697 BlockDriver *drv = bs->drv;
698 if (!drv)
699 return -ENOMEDIUM;
700 if (!drv->bdrv_truncate)
701 return -ENOTSUP;
702 return drv->bdrv_truncate(bs, offset);
706 * Length of a file in bytes. Return < 0 if error or unknown.
708 int64_t bdrv_getlength(BlockDriverState *bs)
710 BlockDriver *drv = bs->drv;
711 if (!drv)
712 return -ENOMEDIUM;
713 if (!drv->bdrv_getlength) {
714 /* legacy mode */
715 return bs->total_sectors * SECTOR_SIZE;
717 return drv->bdrv_getlength(bs);
720 /* return 0 as number of sectors if no device present or error */
721 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
723 int64_t length;
724 length = bdrv_getlength(bs);
725 if (length < 0)
726 length = 0;
727 else
728 length = length >> SECTOR_BITS;
729 *nb_sectors_ptr = length;
732 /* force a given boot sector. */
733 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
735 bs->boot_sector_enabled = 1;
736 if (size > 512)
737 size = 512;
738 memcpy(bs->boot_sector_data, data, size);
739 memset(bs->boot_sector_data + size, 0, 512 - size);
742 struct partition {
743 uint8_t boot_ind; /* 0x80 - active */
744 uint8_t head; /* starting head */
745 uint8_t sector; /* starting sector */
746 uint8_t cyl; /* starting cylinder */
747 uint8_t sys_ind; /* What partition type */
748 uint8_t end_head; /* end head */
749 uint8_t end_sector; /* end sector */
750 uint8_t end_cyl; /* end cylinder */
751 uint32_t start_sect; /* starting sector counting from 0 */
752 uint32_t nr_sects; /* nr of sectors in partition */
753 } __attribute__((packed));
755 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
756 static int guess_disk_lchs(BlockDriverState *bs,
757 int *pcylinders, int *pheads, int *psectors)
759 uint8_t *buf;
760 int ret, i, heads, sectors, cylinders;
761 struct partition *p;
762 uint32_t nr_sects;
763 int64_t nb_sectors;
765 buf = qemu_memalign(512, 512);
766 if (buf == NULL)
767 return -1;
769 bdrv_get_geometry(bs, &nb_sectors);
771 ret = bdrv_read(bs, 0, buf, 1);
772 if (ret < 0)
773 return -1;
774 /* test msdos magic */
775 if (buf[510] != 0x55 || buf[511] != 0xaa) {
776 qemu_free(buf);
777 return -1;
779 for(i = 0; i < 4; i++) {
780 p = ((struct partition *)(buf + 0x1be)) + i;
781 nr_sects = le32_to_cpu(p->nr_sects);
782 if (nr_sects && p->end_head) {
783 /* We make the assumption that the partition terminates on
784 a cylinder boundary */
785 heads = p->end_head + 1;
786 sectors = p->end_sector & 63;
787 if (sectors == 0)
788 continue;
789 cylinders = nb_sectors / (heads * sectors);
790 if (cylinders < 1 || cylinders > 16383)
791 continue;
792 *pheads = heads;
793 *psectors = sectors;
794 *pcylinders = cylinders;
795 #if 0
796 printf("guessed geometry: LCHS=%d %d %d\n",
797 cylinders, heads, sectors);
798 #endif
799 qemu_free(buf);
800 return 0;
803 qemu_free(buf);
804 return -1;
807 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
809 int translation, lba_detected = 0;
810 int cylinders, heads, secs;
811 int64_t nb_sectors;
813 /* if a geometry hint is available, use it */
814 bdrv_get_geometry(bs, &nb_sectors);
815 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
816 translation = bdrv_get_translation_hint(bs);
817 if (cylinders != 0) {
818 *pcyls = cylinders;
819 *pheads = heads;
820 *psecs = secs;
821 } else {
822 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
823 if (heads > 16) {
824 /* if heads > 16, it means that a BIOS LBA
825 translation was active, so the default
826 hardware geometry is OK */
827 lba_detected = 1;
828 goto default_geometry;
829 } else {
830 *pcyls = cylinders;
831 *pheads = heads;
832 *psecs = secs;
833 /* disable any translation to be in sync with
834 the logical geometry */
835 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
836 bdrv_set_translation_hint(bs,
837 BIOS_ATA_TRANSLATION_NONE);
840 } else {
841 default_geometry:
842 /* if no geometry, use a standard physical disk geometry */
843 cylinders = nb_sectors / (16 * 63);
845 if (cylinders > 16383)
846 cylinders = 16383;
847 else if (cylinders < 2)
848 cylinders = 2;
849 *pcyls = cylinders;
850 *pheads = 16;
851 *psecs = 63;
852 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
853 if ((*pcyls * *pheads) <= 131072) {
854 bdrv_set_translation_hint(bs,
855 BIOS_ATA_TRANSLATION_LARGE);
856 } else {
857 bdrv_set_translation_hint(bs,
858 BIOS_ATA_TRANSLATION_LBA);
862 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
866 void bdrv_set_geometry_hint(BlockDriverState *bs,
867 int cyls, int heads, int secs)
869 bs->cyls = cyls;
870 bs->heads = heads;
871 bs->secs = secs;
874 void bdrv_set_type_hint(BlockDriverState *bs, int type)
876 bs->type = type;
877 bs->removable = ((type == BDRV_TYPE_CDROM ||
878 type == BDRV_TYPE_FLOPPY));
881 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
883 bs->translation = translation;
886 void bdrv_get_geometry_hint(BlockDriverState *bs,
887 int *pcyls, int *pheads, int *psecs)
889 *pcyls = bs->cyls;
890 *pheads = bs->heads;
891 *psecs = bs->secs;
894 int bdrv_get_type_hint(BlockDriverState *bs)
896 return bs->type;
899 int bdrv_get_translation_hint(BlockDriverState *bs)
901 return bs->translation;
904 int bdrv_is_removable(BlockDriverState *bs)
906 return bs->removable;
909 int bdrv_is_read_only(BlockDriverState *bs)
911 return bs->read_only;
914 int bdrv_is_sg(BlockDriverState *bs)
916 return bs->sg;
919 /* XXX: no longer used */
920 void bdrv_set_change_cb(BlockDriverState *bs,
921 void (*change_cb)(void *opaque), void *opaque)
923 bs->change_cb = change_cb;
924 bs->change_opaque = opaque;
927 int bdrv_is_encrypted(BlockDriverState *bs)
929 if (bs->backing_hd && bs->backing_hd->encrypted)
930 return 1;
931 return bs->encrypted;
934 int bdrv_set_key(BlockDriverState *bs, const char *key)
936 int ret;
937 if (bs->backing_hd && bs->backing_hd->encrypted) {
938 ret = bdrv_set_key(bs->backing_hd, key);
939 if (ret < 0)
940 return ret;
941 if (!bs->encrypted)
942 return 0;
944 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
945 return -1;
946 return bs->drv->bdrv_set_key(bs, key);
949 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
951 if (!bs->drv) {
952 buf[0] = '\0';
953 } else {
954 pstrcpy(buf, buf_size, bs->drv->format_name);
958 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
959 void *opaque)
961 BlockDriver *drv;
963 for (drv = first_drv; drv != NULL; drv = drv->next) {
964 it(opaque, drv->format_name);
968 BlockDriverState *bdrv_find(const char *name)
970 BlockDriverState *bs;
972 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
973 if (!strcmp(name, bs->device_name))
974 return bs;
976 return NULL;
979 void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
981 BlockDriverState *bs;
983 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
984 it(opaque, bs->device_name);
988 const char *bdrv_get_device_name(BlockDriverState *bs)
990 return bs->device_name;
993 void bdrv_flush(BlockDriverState *bs)
995 if (bs->drv->bdrv_flush)
996 bs->drv->bdrv_flush(bs);
997 if (bs->backing_hd)
998 bdrv_flush(bs->backing_hd);
1001 void bdrv_iterate_writeable(void (*it)(BlockDriverState *bs))
1003 BlockDriverState *bs;
1005 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1006 if (bs->drv && !bdrv_is_read_only(bs) &&
1007 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1008 it(bs);
1011 void bdrv_flush_all(void)
1013 bdrv_iterate_writeable(bdrv_flush);
1016 #ifndef QEMU_IMG
1017 void bdrv_info(void)
1019 BlockDriverState *bs;
1021 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1022 term_printf("%s:", bs->device_name);
1023 term_printf(" type=");
1024 switch(bs->type) {
1025 case BDRV_TYPE_HD:
1026 term_printf("hd");
1027 break;
1028 case BDRV_TYPE_CDROM:
1029 term_printf("cdrom");
1030 break;
1031 case BDRV_TYPE_FLOPPY:
1032 term_printf("floppy");
1033 break;
1035 term_printf(" removable=%d", bs->removable);
1036 if (bs->removable) {
1037 term_printf(" locked=%d", bs->locked);
1039 if (bs->drv) {
1040 term_printf(" file=");
1041 term_print_filename(bs->filename);
1042 if (bs->backing_file[0] != '\0') {
1043 term_printf(" backing_file=");
1044 term_print_filename(bs->backing_file);
1046 term_printf(" ro=%d", bs->read_only);
1047 term_printf(" drv=%s", bs->drv->format_name);
1048 if (bs->encrypted)
1049 term_printf(" encrypted");
1050 } else {
1051 term_printf(" [not inserted]");
1053 term_printf("\n");
1057 /* The "info blockstats" command. */
1058 void bdrv_info_stats (void)
1060 BlockDriverState *bs;
1062 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1063 term_printf ("%s:"
1064 " rd_bytes=%" PRIu64
1065 " wr_bytes=%" PRIu64
1066 " rd_operations=%" PRIu64
1067 " wr_operations=%" PRIu64
1068 "\n",
1069 bs->device_name,
1070 bs->rd_bytes, bs->wr_bytes,
1071 bs->rd_ops, bs->wr_ops);
1074 #endif
1076 void bdrv_get_backing_filename(BlockDriverState *bs,
1077 char *filename, int filename_size)
1079 if (!bs->backing_hd) {
1080 pstrcpy(filename, filename_size, "");
1081 } else {
1082 pstrcpy(filename, filename_size, bs->backing_file);
1086 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1087 const uint8_t *buf, int nb_sectors)
1089 BlockDriver *drv = bs->drv;
1090 if (!drv)
1091 return -ENOMEDIUM;
1092 if (!drv->bdrv_write_compressed)
1093 return -ENOTSUP;
1094 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1097 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1099 BlockDriver *drv = bs->drv;
1100 if (!drv)
1101 return -ENOMEDIUM;
1102 if (!drv->bdrv_get_info)
1103 return -ENOTSUP;
1104 memset(bdi, 0, sizeof(*bdi));
1105 return drv->bdrv_get_info(bs, bdi);
1108 /**************************************************************/
1109 /* handling of snapshots */
1111 int bdrv_snapshot_create(BlockDriverState *bs,
1112 QEMUSnapshotInfo *sn_info)
1114 BlockDriver *drv = bs->drv;
1115 if (!drv)
1116 return -ENOMEDIUM;
1117 if (!drv->bdrv_snapshot_create)
1118 return -ENOTSUP;
1119 return drv->bdrv_snapshot_create(bs, sn_info);
1122 int bdrv_snapshot_goto(BlockDriverState *bs,
1123 const char *snapshot_id)
1125 BlockDriver *drv = bs->drv;
1126 if (!drv)
1127 return -ENOMEDIUM;
1128 if (!drv->bdrv_snapshot_goto)
1129 return -ENOTSUP;
1130 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1133 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1135 BlockDriver *drv = bs->drv;
1136 if (!drv)
1137 return -ENOMEDIUM;
1138 if (!drv->bdrv_snapshot_delete)
1139 return -ENOTSUP;
1140 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1143 int bdrv_snapshot_list(BlockDriverState *bs,
1144 QEMUSnapshotInfo **psn_info)
1146 BlockDriver *drv = bs->drv;
1147 if (!drv)
1148 return -ENOMEDIUM;
1149 if (!drv->bdrv_snapshot_list)
1150 return -ENOTSUP;
1151 return drv->bdrv_snapshot_list(bs, psn_info);
1154 #define NB_SUFFIXES 4
1156 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1158 static const char suffixes[NB_SUFFIXES] = "KMGT";
1159 int64_t base;
1160 int i;
1162 if (size <= 999) {
1163 snprintf(buf, buf_size, "%" PRId64, size);
1164 } else {
1165 base = 1024;
1166 for(i = 0; i < NB_SUFFIXES; i++) {
1167 if (size < (10 * base)) {
1168 snprintf(buf, buf_size, "%0.1f%c",
1169 (double)size / base,
1170 suffixes[i]);
1171 break;
1172 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1173 snprintf(buf, buf_size, "%" PRId64 "%c",
1174 ((size + (base >> 1)) / base),
1175 suffixes[i]);
1176 break;
1178 base = base * 1024;
1181 return buf;
1184 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1186 char buf1[128], date_buf[128], clock_buf[128];
1187 #ifdef _WIN32
1188 struct tm *ptm;
1189 #else
1190 struct tm tm;
1191 #endif
1192 time_t ti;
1193 int64_t secs;
1195 if (!sn) {
1196 snprintf(buf, buf_size,
1197 "%-10s%-20s%7s%20s%15s",
1198 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1199 } else {
1200 ti = sn->date_sec;
1201 #ifdef _WIN32
1202 ptm = localtime(&ti);
1203 strftime(date_buf, sizeof(date_buf),
1204 "%Y-%m-%d %H:%M:%S", ptm);
1205 #else
1206 localtime_r(&ti, &tm);
1207 strftime(date_buf, sizeof(date_buf),
1208 "%Y-%m-%d %H:%M:%S", &tm);
1209 #endif
1210 secs = sn->vm_clock_nsec / 1000000000;
1211 snprintf(clock_buf, sizeof(clock_buf),
1212 "%02d:%02d:%02d.%03d",
1213 (int)(secs / 3600),
1214 (int)((secs / 60) % 60),
1215 (int)(secs % 60),
1216 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1217 snprintf(buf, buf_size,
1218 "%-10s%-20s%7s%20s%15s",
1219 sn->id_str, sn->name,
1220 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1221 date_buf,
1222 clock_buf);
1224 return buf;
1228 /**************************************************************/
1229 /* async I/Os */
1231 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1232 uint8_t *buf, int nb_sectors,
1233 BlockDriverCompletionFunc *cb, void *opaque)
1235 BlockDriver *drv = bs->drv;
1236 BlockDriverAIOCB *ret;
1238 if (!drv)
1239 return NULL;
1241 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1242 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1243 memcpy(buf, bs->boot_sector_data, 512);
1244 sector_num++;
1245 nb_sectors--;
1246 buf += 512;
1249 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1251 if (ret) {
1252 /* Update stats even though technically transfer has not happened. */
1253 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1254 bs->rd_ops ++;
1257 return ret;
1260 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1261 const uint8_t *buf, int nb_sectors,
1262 BlockDriverCompletionFunc *cb, void *opaque)
1264 BlockDriver *drv = bs->drv;
1265 BlockDriverAIOCB *ret;
1267 if (!drv)
1268 return NULL;
1269 if (bs->read_only)
1270 return NULL;
1271 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1272 memcpy(bs->boot_sector_data, buf, 512);
1275 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1277 if (ret) {
1278 /* Update stats even though technically transfer has not happened. */
1279 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1280 bs->wr_ops ++;
1283 return ret;
1286 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1288 BlockDriver *drv = acb->bs->drv;
1290 drv->bdrv_aio_cancel(acb);
1294 /**************************************************************/
1295 /* async block device emulation */
1297 #ifdef QEMU_IMG
1298 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1299 int64_t sector_num, uint8_t *buf, int nb_sectors,
1300 BlockDriverCompletionFunc *cb, void *opaque)
1302 int ret;
1303 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1304 cb(opaque, ret);
1305 return NULL;
1308 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1309 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1310 BlockDriverCompletionFunc *cb, void *opaque)
1312 int ret;
1313 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1314 cb(opaque, ret);
1315 return NULL;
1318 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
1321 #else
1322 static void bdrv_aio_bh_cb(void *opaque)
1324 BlockDriverAIOCBSync *acb = opaque;
1325 acb->common.cb(acb->common.opaque, acb->ret);
1326 qemu_aio_release(acb);
1329 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1330 int64_t sector_num, uint8_t *buf, int nb_sectors,
1331 BlockDriverCompletionFunc *cb, void *opaque)
1333 BlockDriverAIOCBSync *acb;
1334 int ret;
1336 acb = qemu_aio_get(bs, cb, opaque);
1337 if (!acb->bh)
1338 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1339 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1340 acb->ret = ret;
1341 qemu_bh_schedule(acb->bh);
1342 return &acb->common;
1345 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1346 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1347 BlockDriverCompletionFunc *cb, void *opaque)
1349 BlockDriverAIOCBSync *acb;
1350 int ret;
1352 acb = qemu_aio_get(bs, cb, opaque);
1353 if (!acb->bh)
1354 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1355 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1356 acb->ret = ret;
1357 qemu_bh_schedule(acb->bh);
1358 return &acb->common;
1361 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1363 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1364 qemu_bh_cancel(acb->bh);
1365 qemu_aio_release(acb);
1367 #endif /* !QEMU_IMG */
1369 /**************************************************************/
1370 /* sync block device emulation */
1372 static void bdrv_rw_em_cb(void *opaque, int ret)
1374 *(int *)opaque = ret;
1377 #define NOT_DONE 0x7fffffff
1379 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1380 uint8_t *buf, int nb_sectors)
1382 int async_ret;
1383 BlockDriverAIOCB *acb;
1385 async_ret = NOT_DONE;
1386 qemu_aio_wait_start();
1387 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1388 bdrv_rw_em_cb, &async_ret);
1389 if (acb == NULL) {
1390 qemu_aio_wait_end();
1391 return -1;
1393 while (async_ret == NOT_DONE) {
1394 qemu_aio_wait();
1396 qemu_aio_wait_end();
1397 return async_ret;
1400 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1401 const uint8_t *buf, int nb_sectors)
1403 int async_ret;
1404 BlockDriverAIOCB *acb;
1406 async_ret = NOT_DONE;
1407 qemu_aio_wait_start();
1408 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1409 bdrv_rw_em_cb, &async_ret);
1410 if (acb == NULL) {
1411 qemu_aio_wait_end();
1412 return -1;
1414 while (async_ret == NOT_DONE) {
1415 qemu_aio_wait();
1417 qemu_aio_wait_end();
1418 return async_ret;
1421 void bdrv_init(void)
1423 bdrv_register(&bdrv_raw);
1424 bdrv_register(&bdrv_host_device);
1425 #ifndef _WIN32
1426 bdrv_register(&bdrv_cow);
1427 #endif
1428 bdrv_register(&bdrv_qcow);
1429 bdrv_register(&bdrv_vmdk);
1430 bdrv_register(&bdrv_cloop);
1431 bdrv_register(&bdrv_dmg);
1432 bdrv_register(&bdrv_bochs);
1433 bdrv_register(&bdrv_vpc);
1434 bdrv_register(&bdrv_vvfat);
1435 bdrv_register(&bdrv_qcow2);
1436 bdrv_register(&bdrv_parallels);
1439 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1440 void *opaque)
1442 BlockDriver *drv;
1443 BlockDriverAIOCB *acb;
1445 drv = bs->drv;
1446 if (drv->free_aiocb) {
1447 acb = drv->free_aiocb;
1448 drv->free_aiocb = acb->next;
1449 } else {
1450 acb = qemu_mallocz(drv->aiocb_size);
1451 if (!acb)
1452 return NULL;
1454 acb->bs = bs;
1455 acb->cb = cb;
1456 acb->opaque = opaque;
1457 return acb;
1460 void qemu_aio_release(void *p)
1462 BlockDriverAIOCB *acb = p;
1463 BlockDriver *drv = acb->bs->drv;
1464 acb->next = drv->free_aiocb;
1465 drv->free_aiocb = acb;
1468 /**************************************************************/
1469 /* removable device support */
1472 * Return TRUE if the media is present
1474 int bdrv_is_inserted(BlockDriverState *bs)
1476 BlockDriver *drv = bs->drv;
1477 int ret;
1478 if (!drv)
1479 return 0;
1480 if (!drv->bdrv_is_inserted)
1481 return 1;
1482 ret = drv->bdrv_is_inserted(bs);
1483 return ret;
1487 * Return TRUE if the media changed since the last call to this
1488 * function. It is currently only used for floppy disks
1490 int bdrv_media_changed(BlockDriverState *bs)
1492 BlockDriver *drv = bs->drv;
1493 int ret;
1495 if (!drv || !drv->bdrv_media_changed)
1496 ret = -ENOTSUP;
1497 else
1498 ret = drv->bdrv_media_changed(bs);
1499 if (ret == -ENOTSUP)
1500 ret = bs->media_changed;
1501 bs->media_changed = 0;
1502 return ret;
1506 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1508 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1510 BlockDriver *drv = bs->drv;
1511 int ret;
1513 if (!drv || !drv->bdrv_eject) {
1514 ret = -ENOTSUP;
1515 } else {
1516 ret = drv->bdrv_eject(bs, eject_flag);
1518 if (ret == -ENOTSUP) {
1519 if (eject_flag)
1520 bdrv_close(bs);
1524 int bdrv_is_locked(BlockDriverState *bs)
1526 return bs->locked;
1530 * Lock or unlock the media (if it is locked, the user won't be able
1531 * to eject it manually).
1533 void bdrv_set_locked(BlockDriverState *bs, int locked)
1535 BlockDriver *drv = bs->drv;
1537 bs->locked = locked;
1538 if (drv && drv->bdrv_set_locked) {
1539 drv->bdrv_set_locked(bs, locked);
1543 /* needed for generic scsi interface */
1545 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1547 BlockDriver *drv = bs->drv;
1549 if (drv && drv->bdrv_ioctl)
1550 return drv->bdrv_ioctl(bs, req, buf);
1551 return -ENOTSUP;