Fix a bug in init_assigned_device
[qemu-kvm/fedora.git] / block.c
blob307ed9254287c43ed3d83c9fc3b4952a915407b7
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_CACHE_MASK);
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 || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
404 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
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, open_flags) < 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 BlockDriverState *bs;
1037 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1038 if (bs->drv && !bdrv_is_read_only(bs) &&
1039 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1040 bdrv_flush(bs);
1044 * Returns true iff the specified sector is present in the disk image. Drivers
1045 * not implementing the functionality are assumed to not support backing files,
1046 * hence all their sectors are reported as allocated.
1048 * 'pnum' is set to the number of sectors (including and immediately following
1049 * the specified sector) that are known to be in the same
1050 * allocated/unallocated state.
1052 * 'nb_sectors' is the max value 'pnum' should be set to.
1054 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1055 int *pnum)
1057 int64_t n;
1058 if (!bs->drv->bdrv_is_allocated) {
1059 if (sector_num >= bs->total_sectors) {
1060 *pnum = 0;
1061 return 0;
1063 n = bs->total_sectors - sector_num;
1064 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1065 return 1;
1067 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1070 void bdrv_info(void)
1072 BlockDriverState *bs;
1074 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1075 term_printf("%s:", bs->device_name);
1076 term_printf(" type=");
1077 switch(bs->type) {
1078 case BDRV_TYPE_HD:
1079 term_printf("hd");
1080 break;
1081 case BDRV_TYPE_CDROM:
1082 term_printf("cdrom");
1083 break;
1084 case BDRV_TYPE_FLOPPY:
1085 term_printf("floppy");
1086 break;
1088 term_printf(" removable=%d", bs->removable);
1089 if (bs->removable) {
1090 term_printf(" locked=%d", bs->locked);
1092 if (bs->drv) {
1093 term_printf(" file=");
1094 term_print_filename(bs->filename);
1095 if (bs->backing_file[0] != '\0') {
1096 term_printf(" backing_file=");
1097 term_print_filename(bs->backing_file);
1099 term_printf(" ro=%d", bs->read_only);
1100 term_printf(" drv=%s", bs->drv->format_name);
1101 if (bs->encrypted)
1102 term_printf(" encrypted");
1103 } else {
1104 term_printf(" [not inserted]");
1106 term_printf("\n");
1110 /* The "info blockstats" command. */
1111 void bdrv_info_stats (void)
1113 BlockDriverState *bs;
1115 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1116 term_printf ("%s:"
1117 " rd_bytes=%" PRIu64
1118 " wr_bytes=%" PRIu64
1119 " rd_operations=%" PRIu64
1120 " wr_operations=%" PRIu64
1121 "\n",
1122 bs->device_name,
1123 bs->rd_bytes, bs->wr_bytes,
1124 bs->rd_ops, bs->wr_ops);
1128 void bdrv_get_backing_filename(BlockDriverState *bs,
1129 char *filename, int filename_size)
1131 if (!bs->backing_hd) {
1132 pstrcpy(filename, filename_size, "");
1133 } else {
1134 pstrcpy(filename, filename_size, bs->backing_file);
1138 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1139 const uint8_t *buf, int nb_sectors)
1141 BlockDriver *drv = bs->drv;
1142 if (!drv)
1143 return -ENOMEDIUM;
1144 if (!drv->bdrv_write_compressed)
1145 return -ENOTSUP;
1146 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1149 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1151 BlockDriver *drv = bs->drv;
1152 if (!drv)
1153 return -ENOMEDIUM;
1154 if (!drv->bdrv_get_info)
1155 return -ENOTSUP;
1156 memset(bdi, 0, sizeof(*bdi));
1157 return drv->bdrv_get_info(bs, bdi);
1160 /**************************************************************/
1161 /* handling of snapshots */
1163 int bdrv_snapshot_create(BlockDriverState *bs,
1164 QEMUSnapshotInfo *sn_info)
1166 BlockDriver *drv = bs->drv;
1167 if (!drv)
1168 return -ENOMEDIUM;
1169 if (!drv->bdrv_snapshot_create)
1170 return -ENOTSUP;
1171 return drv->bdrv_snapshot_create(bs, sn_info);
1174 int bdrv_snapshot_goto(BlockDriverState *bs,
1175 const char *snapshot_id)
1177 BlockDriver *drv = bs->drv;
1178 if (!drv)
1179 return -ENOMEDIUM;
1180 if (!drv->bdrv_snapshot_goto)
1181 return -ENOTSUP;
1182 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1185 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1187 BlockDriver *drv = bs->drv;
1188 if (!drv)
1189 return -ENOMEDIUM;
1190 if (!drv->bdrv_snapshot_delete)
1191 return -ENOTSUP;
1192 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1195 int bdrv_snapshot_list(BlockDriverState *bs,
1196 QEMUSnapshotInfo **psn_info)
1198 BlockDriver *drv = bs->drv;
1199 if (!drv)
1200 return -ENOMEDIUM;
1201 if (!drv->bdrv_snapshot_list)
1202 return -ENOTSUP;
1203 return drv->bdrv_snapshot_list(bs, psn_info);
1206 #define NB_SUFFIXES 4
1208 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1210 static const char suffixes[NB_SUFFIXES] = "KMGT";
1211 int64_t base;
1212 int i;
1214 if (size <= 999) {
1215 snprintf(buf, buf_size, "%" PRId64, size);
1216 } else {
1217 base = 1024;
1218 for(i = 0; i < NB_SUFFIXES; i++) {
1219 if (size < (10 * base)) {
1220 snprintf(buf, buf_size, "%0.1f%c",
1221 (double)size / base,
1222 suffixes[i]);
1223 break;
1224 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1225 snprintf(buf, buf_size, "%" PRId64 "%c",
1226 ((size + (base >> 1)) / base),
1227 suffixes[i]);
1228 break;
1230 base = base * 1024;
1233 return buf;
1236 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1238 char buf1[128], date_buf[128], clock_buf[128];
1239 #ifdef _WIN32
1240 struct tm *ptm;
1241 #else
1242 struct tm tm;
1243 #endif
1244 time_t ti;
1245 int64_t secs;
1247 if (!sn) {
1248 snprintf(buf, buf_size,
1249 "%-10s%-20s%7s%20s%15s",
1250 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1251 } else {
1252 ti = sn->date_sec;
1253 #ifdef _WIN32
1254 ptm = localtime(&ti);
1255 strftime(date_buf, sizeof(date_buf),
1256 "%Y-%m-%d %H:%M:%S", ptm);
1257 #else
1258 localtime_r(&ti, &tm);
1259 strftime(date_buf, sizeof(date_buf),
1260 "%Y-%m-%d %H:%M:%S", &tm);
1261 #endif
1262 secs = sn->vm_clock_nsec / 1000000000;
1263 snprintf(clock_buf, sizeof(clock_buf),
1264 "%02d:%02d:%02d.%03d",
1265 (int)(secs / 3600),
1266 (int)((secs / 60) % 60),
1267 (int)(secs % 60),
1268 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1269 snprintf(buf, buf_size,
1270 "%-10s%-20s%7s%20s%15s",
1271 sn->id_str, sn->name,
1272 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1273 date_buf,
1274 clock_buf);
1276 return buf;
1280 /**************************************************************/
1281 /* async I/Os */
1283 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1284 uint8_t *buf, int nb_sectors,
1285 BlockDriverCompletionFunc *cb, void *opaque)
1287 BlockDriver *drv = bs->drv;
1288 BlockDriverAIOCB *ret;
1290 if (!drv)
1291 return NULL;
1293 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1294 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1295 memcpy(buf, bs->boot_sector_data, 512);
1296 sector_num++;
1297 nb_sectors--;
1298 buf += 512;
1301 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1303 if (ret) {
1304 /* Update stats even though technically transfer has not happened. */
1305 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1306 bs->rd_ops ++;
1309 return ret;
1312 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1313 const uint8_t *buf, int nb_sectors,
1314 BlockDriverCompletionFunc *cb, void *opaque)
1316 BlockDriver *drv = bs->drv;
1317 BlockDriverAIOCB *ret;
1319 if (!drv)
1320 return NULL;
1321 if (bs->read_only)
1322 return NULL;
1323 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1324 memcpy(bs->boot_sector_data, buf, 512);
1327 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1329 if (ret) {
1330 /* Update stats even though technically transfer has not happened. */
1331 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1332 bs->wr_ops ++;
1335 return ret;
1338 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1340 BlockDriver *drv = acb->bs->drv;
1342 drv->bdrv_aio_cancel(acb);
1346 /**************************************************************/
1347 /* async block device emulation */
1349 static void bdrv_aio_bh_cb(void *opaque)
1351 BlockDriverAIOCBSync *acb = opaque;
1352 acb->common.cb(acb->common.opaque, acb->ret);
1353 qemu_aio_release(acb);
1356 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1357 int64_t sector_num, uint8_t *buf, int nb_sectors,
1358 BlockDriverCompletionFunc *cb, void *opaque)
1360 BlockDriverAIOCBSync *acb;
1361 int ret;
1363 acb = qemu_aio_get(bs, cb, opaque);
1364 if (!acb->bh)
1365 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1366 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1367 acb->ret = ret;
1368 qemu_bh_schedule(acb->bh);
1369 return &acb->common;
1372 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1373 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1374 BlockDriverCompletionFunc *cb, void *opaque)
1376 BlockDriverAIOCBSync *acb;
1377 int ret;
1379 acb = qemu_aio_get(bs, cb, opaque);
1380 if (!acb->bh)
1381 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1382 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1383 acb->ret = ret;
1384 qemu_bh_schedule(acb->bh);
1385 return &acb->common;
1388 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1390 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1391 qemu_bh_cancel(acb->bh);
1392 qemu_aio_release(acb);
1395 /**************************************************************/
1396 /* sync block device emulation */
1398 static void bdrv_rw_em_cb(void *opaque, int ret)
1400 *(int *)opaque = ret;
1403 #define NOT_DONE 0x7fffffff
1405 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1406 uint8_t *buf, int nb_sectors)
1408 int async_ret;
1409 BlockDriverAIOCB *acb;
1411 async_ret = NOT_DONE;
1412 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1413 bdrv_rw_em_cb, &async_ret);
1414 if (acb == NULL)
1415 return -1;
1417 while (async_ret == NOT_DONE) {
1418 qemu_aio_wait();
1421 return async_ret;
1424 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1425 const uint8_t *buf, int nb_sectors)
1427 int async_ret;
1428 BlockDriverAIOCB *acb;
1430 async_ret = NOT_DONE;
1431 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1432 bdrv_rw_em_cb, &async_ret);
1433 if (acb == NULL)
1434 return -1;
1435 while (async_ret == NOT_DONE) {
1436 qemu_aio_wait();
1438 return async_ret;
1441 void bdrv_init(void)
1443 bdrv_register(&bdrv_raw);
1444 bdrv_register(&bdrv_host_device);
1445 #ifndef _WIN32
1446 bdrv_register(&bdrv_cow);
1447 #endif
1448 bdrv_register(&bdrv_qcow);
1449 bdrv_register(&bdrv_vmdk);
1450 bdrv_register(&bdrv_cloop);
1451 bdrv_register(&bdrv_dmg);
1452 bdrv_register(&bdrv_bochs);
1453 bdrv_register(&bdrv_vpc);
1454 bdrv_register(&bdrv_vvfat);
1455 bdrv_register(&bdrv_qcow2);
1456 bdrv_register(&bdrv_parallels);
1457 bdrv_register(&bdrv_nbd);
1460 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1461 void *opaque)
1463 BlockDriver *drv;
1464 BlockDriverAIOCB *acb;
1466 drv = bs->drv;
1467 if (drv->free_aiocb) {
1468 acb = drv->free_aiocb;
1469 drv->free_aiocb = acb->next;
1470 } else {
1471 acb = qemu_mallocz(drv->aiocb_size);
1472 if (!acb)
1473 return NULL;
1475 acb->bs = bs;
1476 acb->cb = cb;
1477 acb->opaque = opaque;
1478 return acb;
1481 void qemu_aio_release(void *p)
1483 BlockDriverAIOCB *acb = p;
1484 BlockDriver *drv = acb->bs->drv;
1485 acb->next = drv->free_aiocb;
1486 drv->free_aiocb = acb;
1489 /**************************************************************/
1490 /* removable device support */
1493 * Return TRUE if the media is present
1495 int bdrv_is_inserted(BlockDriverState *bs)
1497 BlockDriver *drv = bs->drv;
1498 int ret;
1499 if (!drv)
1500 return 0;
1501 if (!drv->bdrv_is_inserted)
1502 return 1;
1503 ret = drv->bdrv_is_inserted(bs);
1504 return ret;
1508 * Return TRUE if the media changed since the last call to this
1509 * function. It is currently only used for floppy disks
1511 int bdrv_media_changed(BlockDriverState *bs)
1513 BlockDriver *drv = bs->drv;
1514 int ret;
1516 if (!drv || !drv->bdrv_media_changed)
1517 ret = -ENOTSUP;
1518 else
1519 ret = drv->bdrv_media_changed(bs);
1520 if (ret == -ENOTSUP)
1521 ret = bs->media_changed;
1522 bs->media_changed = 0;
1523 return ret;
1527 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1529 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1531 BlockDriver *drv = bs->drv;
1532 int ret;
1534 if (!drv || !drv->bdrv_eject) {
1535 ret = -ENOTSUP;
1536 } else {
1537 ret = drv->bdrv_eject(bs, eject_flag);
1539 if (ret == -ENOTSUP) {
1540 if (eject_flag)
1541 bdrv_close(bs);
1545 int bdrv_is_locked(BlockDriverState *bs)
1547 return bs->locked;
1551 * Lock or unlock the media (if it is locked, the user won't be able
1552 * to eject it manually).
1554 void bdrv_set_locked(BlockDriverState *bs, int locked)
1556 BlockDriver *drv = bs->drv;
1558 bs->locked = locked;
1559 if (drv && drv->bdrv_set_locked) {
1560 drv->bdrv_set_locked(bs, locked);
1564 /* needed for generic scsi interface */
1566 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1568 BlockDriver *drv = bs->drv;
1570 if (drv && drv->bdrv_ioctl)
1571 return drv->bdrv_ioctl(bs, req, buf);
1572 return -ENOTSUP;