Merge branch 'qemu-cvs'
[qemu-kvm/fedora.git] / block.c
blob08727ece2013994b6b1c78713135314ddd4e5217
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 #include "console.h"
26 #include "block_int.h"
27 #include "osdep.h"
29 #ifdef _BSD
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/ioctl.h>
33 #include <sys/queue.h>
34 #include <sys/disk.h>
35 #endif
37 #define SECTOR_BITS 9
38 #define SECTOR_SIZE (1 << SECTOR_BITS)
40 typedef struct BlockDriverAIOCBSync {
41 BlockDriverAIOCB common;
42 QEMUBH *bh;
43 int ret;
44 } BlockDriverAIOCBSync;
46 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
47 int64_t sector_num, uint8_t *buf, int nb_sectors,
48 BlockDriverCompletionFunc *cb, void *opaque);
49 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
50 int64_t sector_num, const uint8_t *buf, int nb_sectors,
51 BlockDriverCompletionFunc *cb, void *opaque);
52 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
53 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
54 uint8_t *buf, int nb_sectors);
55 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
56 const uint8_t *buf, int nb_sectors);
58 BlockDriverState *bdrv_first;
60 static BlockDriver *first_drv;
62 int path_is_absolute(const char *path)
64 const char *p;
65 #ifdef _WIN32
66 /* specific case for names like: "\\.\d:" */
67 if (*path == '/' || *path == '\\')
68 return 1;
69 #endif
70 p = strchr(path, ':');
71 if (p)
72 p++;
73 else
74 p = path;
75 #ifdef _WIN32
76 return (*p == '/' || *p == '\\');
77 #else
78 return (*p == '/');
79 #endif
82 /* if filename is absolute, just copy it to dest. Otherwise, build a
83 path to it by considering it is relative to base_path. URL are
84 supported. */
85 void path_combine(char *dest, int dest_size,
86 const char *base_path,
87 const char *filename)
89 const char *p, *p1;
90 int len;
92 if (dest_size <= 0)
93 return;
94 if (path_is_absolute(filename)) {
95 pstrcpy(dest, dest_size, filename);
96 } else {
97 p = strchr(base_path, ':');
98 if (p)
99 p++;
100 else
101 p = base_path;
102 p1 = strrchr(base_path, '/');
103 #ifdef _WIN32
105 const char *p2;
106 p2 = strrchr(base_path, '\\');
107 if (!p1 || p2 > p1)
108 p1 = p2;
110 #endif
111 if (p1)
112 p1++;
113 else
114 p1 = base_path;
115 if (p1 > p)
116 p = p1;
117 len = p - base_path;
118 if (len > dest_size - 1)
119 len = dest_size - 1;
120 memcpy(dest, base_path, len);
121 dest[len] = '\0';
122 pstrcat(dest, dest_size, filename);
127 static void bdrv_register(BlockDriver *bdrv)
129 if (!bdrv->bdrv_aio_read) {
130 /* add AIO emulation layer */
131 bdrv->bdrv_aio_read = bdrv_aio_read_em;
132 bdrv->bdrv_aio_write = bdrv_aio_write_em;
133 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
134 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
135 } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
136 /* add synchronous IO emulation layer */
137 bdrv->bdrv_read = bdrv_read_em;
138 bdrv->bdrv_write = bdrv_write_em;
140 bdrv->next = first_drv;
141 first_drv = bdrv;
144 /* create a new block device (by default it is empty) */
145 BlockDriverState *bdrv_new(const char *device_name)
147 BlockDriverState **pbs, *bs;
149 bs = qemu_mallocz(sizeof(BlockDriverState));
150 if(!bs)
151 return NULL;
152 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
153 if (device_name[0] != '\0') {
154 /* insert at the end */
155 pbs = &bdrv_first;
156 while (*pbs != NULL)
157 pbs = &(*pbs)->next;
158 *pbs = bs;
160 return bs;
163 BlockDriver *bdrv_find_format(const char *format_name)
165 BlockDriver *drv1;
166 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
167 if (!strcmp(drv1->format_name, format_name))
168 return drv1;
170 return NULL;
173 int bdrv_create(BlockDriver *drv,
174 const char *filename, int64_t size_in_sectors,
175 const char *backing_file, int flags)
177 if (!drv->bdrv_create)
178 return -ENOTSUP;
179 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
182 #ifdef _WIN32
183 void get_tmp_filename(char *filename, int size)
185 char temp_dir[MAX_PATH];
187 GetTempPath(MAX_PATH, temp_dir);
188 GetTempFileName(temp_dir, "qem", 0, filename);
190 #else
191 void get_tmp_filename(char *filename, int size)
193 int fd;
194 const char *tmpdir;
195 /* XXX: race condition possible */
196 tmpdir = getenv("TMPDIR");
197 if (!tmpdir)
198 tmpdir = "/tmp";
199 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
200 fd = mkstemp(filename);
201 close(fd);
203 #endif
205 #ifdef _WIN32
206 static int is_windows_drive_prefix(const char *filename)
208 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
209 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
210 filename[1] == ':');
213 static int is_windows_drive(const char *filename)
215 if (is_windows_drive_prefix(filename) &&
216 filename[2] == '\0')
217 return 1;
218 if (strstart(filename, "\\\\.\\", NULL) ||
219 strstart(filename, "//./", NULL))
220 return 1;
221 return 0;
223 #endif
225 static BlockDriver *find_protocol(const char *filename)
227 BlockDriver *drv1;
228 char protocol[128];
229 int len;
230 const char *p;
232 #ifdef _WIN32
233 if (is_windows_drive(filename) ||
234 is_windows_drive_prefix(filename))
235 return &bdrv_raw;
236 #endif
237 p = strchr(filename, ':');
238 if (!p)
239 return &bdrv_raw;
240 len = p - filename;
241 if (len > sizeof(protocol) - 1)
242 len = sizeof(protocol) - 1;
243 memcpy(protocol, filename, len);
244 protocol[len] = '\0';
245 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
246 if (drv1->protocol_name &&
247 !strcmp(drv1->protocol_name, protocol))
248 return drv1;
250 return NULL;
253 /* XXX: force raw format if block or character device ? It would
254 simplify the BSD case */
255 static BlockDriver *find_image_format(const char *filename)
257 int ret, score, score_max;
258 BlockDriver *drv1, *drv;
259 uint8_t buf[2048];
260 BlockDriverState *bs;
262 /* detect host devices. By convention, /dev/cdrom[N] is always
263 recognized as a host CDROM */
264 if (strstart(filename, "/dev/cdrom", NULL))
265 return &bdrv_host_device;
266 #ifdef _WIN32
267 if (is_windows_drive(filename))
268 return &bdrv_host_device;
269 #else
271 struct stat st;
272 if (stat(filename, &st) >= 0 &&
273 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
274 return &bdrv_host_device;
277 #endif
279 drv = find_protocol(filename);
280 /* no need to test disk image formats for vvfat */
281 if (drv == &bdrv_vvfat)
282 return drv;
284 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
285 if (ret < 0)
286 return NULL;
287 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
288 bdrv_delete(bs);
289 if (ret < 0) {
290 return NULL;
293 score_max = 0;
294 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
295 if (drv1->bdrv_probe) {
296 score = drv1->bdrv_probe(buf, ret, filename);
297 if (score > score_max) {
298 score_max = score;
299 drv = drv1;
303 return drv;
306 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
308 BlockDriverState *bs;
309 int ret;
311 bs = bdrv_new("");
312 if (!bs)
313 return -ENOMEM;
314 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
315 if (ret < 0) {
316 bdrv_delete(bs);
317 return ret;
319 *pbs = bs;
320 return 0;
323 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
325 return bdrv_open2(bs, filename, flags, NULL);
328 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
329 BlockDriver *drv)
331 int ret, open_flags;
332 char tmp_filename[PATH_MAX];
333 char backing_filename[PATH_MAX];
335 bs->read_only = 0;
336 bs->is_temporary = 0;
337 bs->encrypted = 0;
339 if (flags & BDRV_O_SNAPSHOT) {
340 BlockDriverState *bs1;
341 int64_t total_size;
342 int is_protocol = 0;
344 /* if snapshot, we create a temporary backing file and open it
345 instead of opening 'filename' directly */
347 /* if there is a backing file, use it */
348 bs1 = bdrv_new("");
349 if (!bs1) {
350 return -ENOMEM;
352 if (bdrv_open(bs1, filename, 0) < 0) {
353 bdrv_delete(bs1);
354 return -1;
356 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
358 if (bs1->drv && bs1->drv->protocol_name)
359 is_protocol = 1;
361 bdrv_delete(bs1);
363 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
365 /* Real path is meaningless for protocols */
366 if (is_protocol)
367 snprintf(backing_filename, sizeof(backing_filename),
368 "%s", filename);
369 else
370 realpath(filename, backing_filename);
372 if (bdrv_create(&bdrv_qcow2, tmp_filename,
373 total_size, backing_filename, 0) < 0) {
374 return -1;
376 filename = tmp_filename;
377 bs->is_temporary = 1;
380 pstrcpy(bs->filename, sizeof(bs->filename), filename);
381 if (flags & BDRV_O_FILE) {
382 drv = find_protocol(filename);
383 if (!drv)
384 return -ENOENT;
385 } else {
386 if (!drv) {
387 drv = find_image_format(filename);
388 if (!drv)
389 return -1;
392 bs->drv = drv;
393 bs->opaque = qemu_mallocz(drv->instance_size);
394 if (bs->opaque == NULL && drv->instance_size > 0)
395 return -1;
396 /* Note: for compatibility, we open disk image files as RDWR, and
397 RDONLY as fallback */
398 if (!(flags & BDRV_O_FILE))
399 open_flags = BDRV_O_RDWR | (flags & BDRV_O_DIRECT);
400 else
401 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
402 ret = drv->bdrv_open(bs, filename, open_flags);
403 if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
404 ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
405 bs->read_only = 1;
407 if (ret < 0) {
408 qemu_free(bs->opaque);
409 bs->opaque = NULL;
410 bs->drv = NULL;
411 return ret;
413 if (drv->bdrv_getlength) {
414 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
416 #ifndef _WIN32
417 if (bs->is_temporary) {
418 unlink(filename);
420 #endif
421 if (bs->backing_file[0] != '\0') {
422 /* if there is a backing file, use it */
423 bs->backing_hd = bdrv_new("");
424 if (!bs->backing_hd) {
425 fail:
426 bdrv_close(bs);
427 return -ENOMEM;
429 path_combine(backing_filename, sizeof(backing_filename),
430 filename, bs->backing_file);
431 if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
432 goto fail;
435 /* call the change callback */
436 bs->media_changed = 1;
437 if (bs->change_cb)
438 bs->change_cb(bs->change_opaque);
440 return 0;
443 void bdrv_close(BlockDriverState *bs)
445 if (bs->drv) {
446 if (bs->backing_hd)
447 bdrv_delete(bs->backing_hd);
448 bs->drv->bdrv_close(bs);
449 qemu_free(bs->opaque);
450 #ifdef _WIN32
451 if (bs->is_temporary) {
452 unlink(bs->filename);
454 #endif
455 bs->opaque = NULL;
456 bs->drv = NULL;
458 /* call the change callback */
459 bs->media_changed = 1;
460 if (bs->change_cb)
461 bs->change_cb(bs->change_opaque);
465 void bdrv_delete(BlockDriverState *bs)
467 BlockDriverState **pbs;
469 pbs = &bdrv_first;
470 while (*pbs != bs && *pbs != NULL)
471 pbs = &(*pbs)->next;
472 if (*pbs == bs)
473 *pbs = bs->next;
475 bdrv_close(bs);
476 qemu_free(bs);
479 /* commit COW file into the raw image */
480 int bdrv_commit(BlockDriverState *bs)
482 BlockDriver *drv = bs->drv;
483 int64_t i, total_sectors;
484 int n, j;
485 unsigned char sector[512];
487 if (!drv)
488 return -ENOMEDIUM;
490 if (bs->read_only) {
491 return -EACCES;
494 if (!bs->backing_hd) {
495 return -ENOTSUP;
498 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
499 for (i = 0; i < total_sectors;) {
500 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
501 for(j = 0; j < n; j++) {
502 if (bdrv_read(bs, i, sector, 1) != 0) {
503 return -EIO;
506 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
507 return -EIO;
509 i++;
511 } else {
512 i += n;
516 if (drv->bdrv_make_empty)
517 return drv->bdrv_make_empty(bs);
519 return 0;
522 /* return < 0 if error. See bdrv_write() for the return codes */
523 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
524 uint8_t *buf, int nb_sectors)
526 BlockDriver *drv = bs->drv;
528 if (!drv)
529 return -ENOMEDIUM;
531 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
532 memcpy(buf, bs->boot_sector_data, 512);
533 sector_num++;
534 nb_sectors--;
535 buf += 512;
536 if (nb_sectors == 0)
537 return 0;
539 if (drv->bdrv_pread) {
540 int ret, len;
541 len = nb_sectors * 512;
542 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
543 if (ret < 0)
544 return ret;
545 else if (ret != len)
546 return -EINVAL;
547 else {
548 bs->rd_bytes += (unsigned) len;
549 bs->rd_ops ++;
550 return 0;
552 } else {
553 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
557 /* Return < 0 if error. Important errors are:
558 -EIO generic I/O error (may happen for all errors)
559 -ENOMEDIUM No media inserted.
560 -EINVAL Invalid sector number or nb_sectors
561 -EACCES Trying to write a read-only device
563 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
564 const uint8_t *buf, int nb_sectors)
566 BlockDriver *drv = bs->drv;
567 if (!bs->drv)
568 return -ENOMEDIUM;
569 if (bs->read_only)
570 return -EACCES;
571 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
572 memcpy(bs->boot_sector_data, buf, 512);
574 if (drv->bdrv_pwrite) {
575 int ret, len;
576 len = nb_sectors * 512;
577 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
578 if (ret < 0)
579 return ret;
580 else if (ret != len)
581 return -EIO;
582 else {
583 bs->wr_bytes += (unsigned) len;
584 bs->wr_ops ++;
585 return 0;
587 } else {
588 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
592 static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
593 uint8_t *buf, int count1)
595 uint8_t tmp_buf[SECTOR_SIZE];
596 int len, nb_sectors, count;
597 int64_t sector_num;
599 count = count1;
600 /* first read to align to sector start */
601 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
602 if (len > count)
603 len = count;
604 sector_num = offset >> SECTOR_BITS;
605 if (len > 0) {
606 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
607 return -EIO;
608 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
609 count -= len;
610 if (count == 0)
611 return count1;
612 sector_num++;
613 buf += len;
616 /* read the sectors "in place" */
617 nb_sectors = count >> SECTOR_BITS;
618 if (nb_sectors > 0) {
619 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
620 return -EIO;
621 sector_num += nb_sectors;
622 len = nb_sectors << SECTOR_BITS;
623 buf += len;
624 count -= len;
627 /* add data from the last sector */
628 if (count > 0) {
629 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
630 return -EIO;
631 memcpy(buf, tmp_buf, count);
633 return count1;
636 static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
637 const uint8_t *buf, int count1)
639 uint8_t tmp_buf[SECTOR_SIZE];
640 int len, nb_sectors, count;
641 int64_t sector_num;
643 count = count1;
644 /* first write to align to sector start */
645 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
646 if (len > count)
647 len = count;
648 sector_num = offset >> SECTOR_BITS;
649 if (len > 0) {
650 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
651 return -EIO;
652 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
653 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
654 return -EIO;
655 count -= len;
656 if (count == 0)
657 return count1;
658 sector_num++;
659 buf += len;
662 /* write the sectors "in place" */
663 nb_sectors = count >> SECTOR_BITS;
664 if (nb_sectors > 0) {
665 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
666 return -EIO;
667 sector_num += nb_sectors;
668 len = nb_sectors << SECTOR_BITS;
669 buf += len;
670 count -= len;
673 /* add data from the last sector */
674 if (count > 0) {
675 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
676 return -EIO;
677 memcpy(tmp_buf, buf, count);
678 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
679 return -EIO;
681 return count1;
685 * Read with byte offsets (needed only for file protocols)
687 int bdrv_pread(BlockDriverState *bs, int64_t offset,
688 void *buf1, int count1)
690 BlockDriver *drv = bs->drv;
692 if (!drv)
693 return -ENOMEDIUM;
694 if (!drv->bdrv_pread)
695 return bdrv_pread_em(bs, offset, buf1, count1);
696 return drv->bdrv_pread(bs, offset, buf1, count1);
700 * Write with byte offsets (needed only for file protocols)
702 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
703 const void *buf1, int count1)
705 BlockDriver *drv = bs->drv;
707 if (!drv)
708 return -ENOMEDIUM;
709 if (!drv->bdrv_pwrite)
710 return bdrv_pwrite_em(bs, offset, buf1, count1);
711 return drv->bdrv_pwrite(bs, offset, buf1, count1);
715 * Truncate file to 'offset' bytes (needed only for file protocols)
717 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
719 BlockDriver *drv = bs->drv;
720 if (!drv)
721 return -ENOMEDIUM;
722 if (!drv->bdrv_truncate)
723 return -ENOTSUP;
724 return drv->bdrv_truncate(bs, offset);
728 * Length of a file in bytes. Return < 0 if error or unknown.
730 int64_t bdrv_getlength(BlockDriverState *bs)
732 BlockDriver *drv = bs->drv;
733 if (!drv)
734 return -ENOMEDIUM;
735 if (!drv->bdrv_getlength) {
736 /* legacy mode */
737 return bs->total_sectors * SECTOR_SIZE;
739 return drv->bdrv_getlength(bs);
742 /* return 0 as number of sectors if no device present or error */
743 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
745 int64_t length;
746 length = bdrv_getlength(bs);
747 if (length < 0)
748 length = 0;
749 else
750 length = length >> SECTOR_BITS;
751 *nb_sectors_ptr = length;
754 /* force a given boot sector. */
755 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
757 bs->boot_sector_enabled = 1;
758 if (size > 512)
759 size = 512;
760 memcpy(bs->boot_sector_data, data, size);
761 memset(bs->boot_sector_data + size, 0, 512 - size);
764 struct partition {
765 uint8_t boot_ind; /* 0x80 - active */
766 uint8_t head; /* starting head */
767 uint8_t sector; /* starting sector */
768 uint8_t cyl; /* starting cylinder */
769 uint8_t sys_ind; /* What partition type */
770 uint8_t end_head; /* end head */
771 uint8_t end_sector; /* end sector */
772 uint8_t end_cyl; /* end cylinder */
773 uint32_t start_sect; /* starting sector counting from 0 */
774 uint32_t nr_sects; /* nr of sectors in partition */
775 } __attribute__((packed));
777 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
778 static int guess_disk_lchs(BlockDriverState *bs,
779 int *pcylinders, int *pheads, int *psectors)
781 uint8_t *buf;
782 int ret, i, heads, sectors, cylinders;
783 struct partition *p;
784 uint32_t nr_sects;
785 int64_t nb_sectors;
787 buf = qemu_memalign(512, 512);
788 if (buf == NULL)
789 return -1;
791 bdrv_get_geometry(bs, &nb_sectors);
793 ret = bdrv_read(bs, 0, buf, 1);
794 if (ret < 0)
795 return -1;
796 /* test msdos magic */
797 if (buf[510] != 0x55 || buf[511] != 0xaa) {
798 qemu_free(buf);
799 return -1;
801 for(i = 0; i < 4; i++) {
802 p = ((struct partition *)(buf + 0x1be)) + i;
803 nr_sects = le32_to_cpu(p->nr_sects);
804 if (nr_sects && p->end_head) {
805 /* We make the assumption that the partition terminates on
806 a cylinder boundary */
807 heads = p->end_head + 1;
808 sectors = p->end_sector & 63;
809 if (sectors == 0)
810 continue;
811 cylinders = nb_sectors / (heads * sectors);
812 if (cylinders < 1 || cylinders > 16383)
813 continue;
814 *pheads = heads;
815 *psectors = sectors;
816 *pcylinders = cylinders;
817 #if 0
818 printf("guessed geometry: LCHS=%d %d %d\n",
819 cylinders, heads, sectors);
820 #endif
821 qemu_free(buf);
822 return 0;
825 qemu_free(buf);
826 return -1;
829 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
831 int translation, lba_detected = 0;
832 int cylinders, heads, secs;
833 int64_t nb_sectors;
835 /* if a geometry hint is available, use it */
836 bdrv_get_geometry(bs, &nb_sectors);
837 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
838 translation = bdrv_get_translation_hint(bs);
839 if (cylinders != 0) {
840 *pcyls = cylinders;
841 *pheads = heads;
842 *psecs = secs;
843 } else {
844 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
845 if (heads > 16) {
846 /* if heads > 16, it means that a BIOS LBA
847 translation was active, so the default
848 hardware geometry is OK */
849 lba_detected = 1;
850 goto default_geometry;
851 } else {
852 *pcyls = cylinders;
853 *pheads = heads;
854 *psecs = secs;
855 /* disable any translation to be in sync with
856 the logical geometry */
857 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
858 bdrv_set_translation_hint(bs,
859 BIOS_ATA_TRANSLATION_NONE);
862 } else {
863 default_geometry:
864 /* if no geometry, use a standard physical disk geometry */
865 cylinders = nb_sectors / (16 * 63);
867 if (cylinders > 16383)
868 cylinders = 16383;
869 else if (cylinders < 2)
870 cylinders = 2;
871 *pcyls = cylinders;
872 *pheads = 16;
873 *psecs = 63;
874 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
875 if ((*pcyls * *pheads) <= 131072) {
876 bdrv_set_translation_hint(bs,
877 BIOS_ATA_TRANSLATION_LARGE);
878 } else {
879 bdrv_set_translation_hint(bs,
880 BIOS_ATA_TRANSLATION_LBA);
884 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
888 void bdrv_set_geometry_hint(BlockDriverState *bs,
889 int cyls, int heads, int secs)
891 bs->cyls = cyls;
892 bs->heads = heads;
893 bs->secs = secs;
896 void bdrv_set_type_hint(BlockDriverState *bs, int type)
898 bs->type = type;
899 bs->removable = ((type == BDRV_TYPE_CDROM ||
900 type == BDRV_TYPE_FLOPPY));
903 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
905 bs->translation = translation;
908 void bdrv_get_geometry_hint(BlockDriverState *bs,
909 int *pcyls, int *pheads, int *psecs)
911 *pcyls = bs->cyls;
912 *pheads = bs->heads;
913 *psecs = bs->secs;
916 int bdrv_get_type_hint(BlockDriverState *bs)
918 return bs->type;
921 int bdrv_get_translation_hint(BlockDriverState *bs)
923 return bs->translation;
926 int bdrv_is_removable(BlockDriverState *bs)
928 return bs->removable;
931 int bdrv_is_read_only(BlockDriverState *bs)
933 return bs->read_only;
936 int bdrv_is_sg(BlockDriverState *bs)
938 return bs->sg;
941 /* XXX: no longer used */
942 void bdrv_set_change_cb(BlockDriverState *bs,
943 void (*change_cb)(void *opaque), void *opaque)
945 bs->change_cb = change_cb;
946 bs->change_opaque = opaque;
949 int bdrv_is_encrypted(BlockDriverState *bs)
951 if (bs->backing_hd && bs->backing_hd->encrypted)
952 return 1;
953 return bs->encrypted;
956 int bdrv_set_key(BlockDriverState *bs, const char *key)
958 int ret;
959 if (bs->backing_hd && bs->backing_hd->encrypted) {
960 ret = bdrv_set_key(bs->backing_hd, key);
961 if (ret < 0)
962 return ret;
963 if (!bs->encrypted)
964 return 0;
966 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
967 return -1;
968 return bs->drv->bdrv_set_key(bs, key);
971 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
973 if (!bs->drv) {
974 buf[0] = '\0';
975 } else {
976 pstrcpy(buf, buf_size, bs->drv->format_name);
980 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
981 void *opaque)
983 BlockDriver *drv;
985 for (drv = first_drv; drv != NULL; drv = drv->next) {
986 it(opaque, drv->format_name);
990 BlockDriverState *bdrv_find(const char *name)
992 BlockDriverState *bs;
994 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
995 if (!strcmp(name, bs->device_name))
996 return bs;
998 return NULL;
1001 void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
1003 BlockDriverState *bs;
1005 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1006 it(opaque, bs->device_name);
1010 const char *bdrv_get_device_name(BlockDriverState *bs)
1012 return bs->device_name;
1015 void bdrv_flush(BlockDriverState *bs)
1017 if (bs->drv->bdrv_flush)
1018 bs->drv->bdrv_flush(bs);
1019 if (bs->backing_hd)
1020 bdrv_flush(bs->backing_hd);
1023 void bdrv_iterate_writeable(void (*it)(BlockDriverState *bs))
1025 BlockDriverState *bs;
1027 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1028 if (bs->drv && !bdrv_is_read_only(bs) &&
1029 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1030 it(bs);
1033 void bdrv_flush_all(void)
1035 bdrv_iterate_writeable(bdrv_flush);
1039 * Returns true iff the specified sector is present in the disk image. Drivers
1040 * not implementing the functionality are assumed to not support backing files,
1041 * hence all their sectors are reported as allocated.
1043 * 'pnum' is set to the number of sectors (including and immediately following
1044 * the specified sector) that are known to be in the same
1045 * allocated/unallocated state.
1047 * 'nb_sectors' is the max value 'pnum' should be set to.
1049 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1050 int *pnum)
1052 int64_t n;
1053 if (!bs->drv->bdrv_is_allocated) {
1054 if (sector_num >= bs->total_sectors) {
1055 *pnum = 0;
1056 return 0;
1058 n = bs->total_sectors - sector_num;
1059 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1060 return 1;
1062 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1065 void bdrv_info(void)
1067 BlockDriverState *bs;
1069 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1070 term_printf("%s:", bs->device_name);
1071 term_printf(" type=");
1072 switch(bs->type) {
1073 case BDRV_TYPE_HD:
1074 term_printf("hd");
1075 break;
1076 case BDRV_TYPE_CDROM:
1077 term_printf("cdrom");
1078 break;
1079 case BDRV_TYPE_FLOPPY:
1080 term_printf("floppy");
1081 break;
1083 term_printf(" removable=%d", bs->removable);
1084 if (bs->removable) {
1085 term_printf(" locked=%d", bs->locked);
1087 if (bs->drv) {
1088 term_printf(" file=");
1089 term_print_filename(bs->filename);
1090 if (bs->backing_file[0] != '\0') {
1091 term_printf(" backing_file=");
1092 term_print_filename(bs->backing_file);
1094 term_printf(" ro=%d", bs->read_only);
1095 term_printf(" drv=%s", bs->drv->format_name);
1096 if (bs->encrypted)
1097 term_printf(" encrypted");
1098 } else {
1099 term_printf(" [not inserted]");
1101 term_printf("\n");
1105 /* The "info blockstats" command. */
1106 void bdrv_info_stats (void)
1108 BlockDriverState *bs;
1110 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1111 term_printf ("%s:"
1112 " rd_bytes=%" PRIu64
1113 " wr_bytes=%" PRIu64
1114 " rd_operations=%" PRIu64
1115 " wr_operations=%" PRIu64
1116 "\n",
1117 bs->device_name,
1118 bs->rd_bytes, bs->wr_bytes,
1119 bs->rd_ops, bs->wr_ops);
1123 void bdrv_get_backing_filename(BlockDriverState *bs,
1124 char *filename, int filename_size)
1126 if (!bs->backing_hd) {
1127 pstrcpy(filename, filename_size, "");
1128 } else {
1129 pstrcpy(filename, filename_size, bs->backing_file);
1133 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1134 const uint8_t *buf, int nb_sectors)
1136 BlockDriver *drv = bs->drv;
1137 if (!drv)
1138 return -ENOMEDIUM;
1139 if (!drv->bdrv_write_compressed)
1140 return -ENOTSUP;
1141 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1144 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1146 BlockDriver *drv = bs->drv;
1147 if (!drv)
1148 return -ENOMEDIUM;
1149 if (!drv->bdrv_get_info)
1150 return -ENOTSUP;
1151 memset(bdi, 0, sizeof(*bdi));
1152 return drv->bdrv_get_info(bs, bdi);
1155 /**************************************************************/
1156 /* handling of snapshots */
1158 int bdrv_snapshot_create(BlockDriverState *bs,
1159 QEMUSnapshotInfo *sn_info)
1161 BlockDriver *drv = bs->drv;
1162 if (!drv)
1163 return -ENOMEDIUM;
1164 if (!drv->bdrv_snapshot_create)
1165 return -ENOTSUP;
1166 return drv->bdrv_snapshot_create(bs, sn_info);
1169 int bdrv_snapshot_goto(BlockDriverState *bs,
1170 const char *snapshot_id)
1172 BlockDriver *drv = bs->drv;
1173 if (!drv)
1174 return -ENOMEDIUM;
1175 if (!drv->bdrv_snapshot_goto)
1176 return -ENOTSUP;
1177 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1180 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1182 BlockDriver *drv = bs->drv;
1183 if (!drv)
1184 return -ENOMEDIUM;
1185 if (!drv->bdrv_snapshot_delete)
1186 return -ENOTSUP;
1187 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1190 int bdrv_snapshot_list(BlockDriverState *bs,
1191 QEMUSnapshotInfo **psn_info)
1193 BlockDriver *drv = bs->drv;
1194 if (!drv)
1195 return -ENOMEDIUM;
1196 if (!drv->bdrv_snapshot_list)
1197 return -ENOTSUP;
1198 return drv->bdrv_snapshot_list(bs, psn_info);
1201 #define NB_SUFFIXES 4
1203 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1205 static const char suffixes[NB_SUFFIXES] = "KMGT";
1206 int64_t base;
1207 int i;
1209 if (size <= 999) {
1210 snprintf(buf, buf_size, "%" PRId64, size);
1211 } else {
1212 base = 1024;
1213 for(i = 0; i < NB_SUFFIXES; i++) {
1214 if (size < (10 * base)) {
1215 snprintf(buf, buf_size, "%0.1f%c",
1216 (double)size / base,
1217 suffixes[i]);
1218 break;
1219 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1220 snprintf(buf, buf_size, "%" PRId64 "%c",
1221 ((size + (base >> 1)) / base),
1222 suffixes[i]);
1223 break;
1225 base = base * 1024;
1228 return buf;
1231 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1233 char buf1[128], date_buf[128], clock_buf[128];
1234 #ifdef _WIN32
1235 struct tm *ptm;
1236 #else
1237 struct tm tm;
1238 #endif
1239 time_t ti;
1240 int64_t secs;
1242 if (!sn) {
1243 snprintf(buf, buf_size,
1244 "%-10s%-20s%7s%20s%15s",
1245 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1246 } else {
1247 ti = sn->date_sec;
1248 #ifdef _WIN32
1249 ptm = localtime(&ti);
1250 strftime(date_buf, sizeof(date_buf),
1251 "%Y-%m-%d %H:%M:%S", ptm);
1252 #else
1253 localtime_r(&ti, &tm);
1254 strftime(date_buf, sizeof(date_buf),
1255 "%Y-%m-%d %H:%M:%S", &tm);
1256 #endif
1257 secs = sn->vm_clock_nsec / 1000000000;
1258 snprintf(clock_buf, sizeof(clock_buf),
1259 "%02d:%02d:%02d.%03d",
1260 (int)(secs / 3600),
1261 (int)((secs / 60) % 60),
1262 (int)(secs % 60),
1263 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1264 snprintf(buf, buf_size,
1265 "%-10s%-20s%7s%20s%15s",
1266 sn->id_str, sn->name,
1267 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1268 date_buf,
1269 clock_buf);
1271 return buf;
1275 /**************************************************************/
1276 /* async I/Os */
1278 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1279 uint8_t *buf, int nb_sectors,
1280 BlockDriverCompletionFunc *cb, void *opaque)
1282 BlockDriver *drv = bs->drv;
1283 BlockDriverAIOCB *ret;
1285 if (!drv)
1286 return NULL;
1288 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1289 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1290 memcpy(buf, bs->boot_sector_data, 512);
1291 sector_num++;
1292 nb_sectors--;
1293 buf += 512;
1296 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1298 if (ret) {
1299 /* Update stats even though technically transfer has not happened. */
1300 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1301 bs->rd_ops ++;
1304 return ret;
1307 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1308 const uint8_t *buf, int nb_sectors,
1309 BlockDriverCompletionFunc *cb, void *opaque)
1311 BlockDriver *drv = bs->drv;
1312 BlockDriverAIOCB *ret;
1314 if (!drv)
1315 return NULL;
1316 if (bs->read_only)
1317 return NULL;
1318 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1319 memcpy(bs->boot_sector_data, buf, 512);
1322 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1324 if (ret) {
1325 /* Update stats even though technically transfer has not happened. */
1326 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1327 bs->wr_ops ++;
1330 return ret;
1333 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1335 BlockDriver *drv = acb->bs->drv;
1337 drv->bdrv_aio_cancel(acb);
1341 /**************************************************************/
1342 /* async block device emulation */
1344 static void bdrv_aio_bh_cb(void *opaque)
1346 BlockDriverAIOCBSync *acb = opaque;
1347 acb->common.cb(acb->common.opaque, acb->ret);
1348 qemu_aio_release(acb);
1351 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1352 int64_t sector_num, uint8_t *buf, int nb_sectors,
1353 BlockDriverCompletionFunc *cb, void *opaque)
1355 BlockDriverAIOCBSync *acb;
1356 int ret;
1358 acb = qemu_aio_get(bs, cb, opaque);
1359 if (!acb->bh)
1360 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1361 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1362 acb->ret = ret;
1363 qemu_bh_schedule(acb->bh);
1364 return &acb->common;
1367 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1368 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1369 BlockDriverCompletionFunc *cb, void *opaque)
1371 BlockDriverAIOCBSync *acb;
1372 int ret;
1374 acb = qemu_aio_get(bs, cb, opaque);
1375 if (!acb->bh)
1376 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1377 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1378 acb->ret = ret;
1379 qemu_bh_schedule(acb->bh);
1380 return &acb->common;
1383 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1385 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1386 qemu_bh_cancel(acb->bh);
1387 qemu_aio_release(acb);
1390 /**************************************************************/
1391 /* sync block device emulation */
1393 static void bdrv_rw_em_cb(void *opaque, int ret)
1395 *(int *)opaque = ret;
1398 #define NOT_DONE 0x7fffffff
1400 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1401 uint8_t *buf, int nb_sectors)
1403 int async_ret;
1404 BlockDriverAIOCB *acb;
1406 async_ret = NOT_DONE;
1407 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1408 bdrv_rw_em_cb, &async_ret);
1409 if (acb == NULL)
1410 return -1;
1412 while (async_ret == NOT_DONE) {
1413 qemu_aio_wait();
1416 return async_ret;
1419 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1420 const uint8_t *buf, int nb_sectors)
1422 int async_ret;
1423 BlockDriverAIOCB *acb;
1425 async_ret = NOT_DONE;
1426 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1427 bdrv_rw_em_cb, &async_ret);
1428 if (acb == NULL)
1429 return -1;
1430 while (async_ret == NOT_DONE) {
1431 qemu_aio_wait();
1433 return async_ret;
1436 void bdrv_init(void)
1438 bdrv_register(&bdrv_raw);
1439 bdrv_register(&bdrv_host_device);
1440 #ifndef _WIN32
1441 bdrv_register(&bdrv_cow);
1442 #endif
1443 bdrv_register(&bdrv_qcow);
1444 bdrv_register(&bdrv_vmdk);
1445 bdrv_register(&bdrv_cloop);
1446 bdrv_register(&bdrv_dmg);
1447 bdrv_register(&bdrv_bochs);
1448 bdrv_register(&bdrv_vpc);
1449 bdrv_register(&bdrv_vvfat);
1450 bdrv_register(&bdrv_qcow2);
1451 bdrv_register(&bdrv_parallels);
1452 bdrv_register(&bdrv_nbd);
1455 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1456 void *opaque)
1458 BlockDriver *drv;
1459 BlockDriverAIOCB *acb;
1461 drv = bs->drv;
1462 if (drv->free_aiocb) {
1463 acb = drv->free_aiocb;
1464 drv->free_aiocb = acb->next;
1465 } else {
1466 acb = qemu_mallocz(drv->aiocb_size);
1467 if (!acb)
1468 return NULL;
1470 acb->bs = bs;
1471 acb->cb = cb;
1472 acb->opaque = opaque;
1473 return acb;
1476 void qemu_aio_release(void *p)
1478 BlockDriverAIOCB *acb = p;
1479 BlockDriver *drv = acb->bs->drv;
1480 acb->next = drv->free_aiocb;
1481 drv->free_aiocb = acb;
1484 /**************************************************************/
1485 /* removable device support */
1488 * Return TRUE if the media is present
1490 int bdrv_is_inserted(BlockDriverState *bs)
1492 BlockDriver *drv = bs->drv;
1493 int ret;
1494 if (!drv)
1495 return 0;
1496 if (!drv->bdrv_is_inserted)
1497 return 1;
1498 ret = drv->bdrv_is_inserted(bs);
1499 return ret;
1503 * Return TRUE if the media changed since the last call to this
1504 * function. It is currently only used for floppy disks
1506 int bdrv_media_changed(BlockDriverState *bs)
1508 BlockDriver *drv = bs->drv;
1509 int ret;
1511 if (!drv || !drv->bdrv_media_changed)
1512 ret = -ENOTSUP;
1513 else
1514 ret = drv->bdrv_media_changed(bs);
1515 if (ret == -ENOTSUP)
1516 ret = bs->media_changed;
1517 bs->media_changed = 0;
1518 return ret;
1522 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1524 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1526 BlockDriver *drv = bs->drv;
1527 int ret;
1529 if (!drv || !drv->bdrv_eject) {
1530 ret = -ENOTSUP;
1531 } else {
1532 ret = drv->bdrv_eject(bs, eject_flag);
1534 if (ret == -ENOTSUP) {
1535 if (eject_flag)
1536 bdrv_close(bs);
1540 int bdrv_is_locked(BlockDriverState *bs)
1542 return bs->locked;
1546 * Lock or unlock the media (if it is locked, the user won't be able
1547 * to eject it manually).
1549 void bdrv_set_locked(BlockDriverState *bs, int locked)
1551 BlockDriver *drv = bs->drv;
1553 bs->locked = locked;
1554 if (drv && drv->bdrv_set_locked) {
1555 drv->bdrv_set_locked(bs, locked);
1559 /* needed for generic scsi interface */
1561 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1563 BlockDriver *drv = bs->drv;
1565 if (drv && drv->bdrv_ioctl)
1566 return drv->bdrv_ioctl(bs, req, buf);
1567 return -ENOTSUP;