kvm: configure: if all else fails, infer kernel version from .config
[kvm-userspace.git] / qemu / block.c
blobdc744dd313ac33287ef52cb672092e9d1a382c75
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 "config-host.h"
25 #ifdef _BSD
26 /* include native header before sys-queue.h */
27 #include <sys/queue.h>
28 #endif
30 #include "qemu-common.h"
31 #include "console.h"
32 #include "block_int.h"
33 #include "osdep.h"
35 #ifdef _BSD
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/ioctl.h>
39 #include <sys/disk.h>
40 #endif
42 #define SECTOR_BITS 9
43 #define SECTOR_SIZE (1 << SECTOR_BITS)
45 typedef struct BlockDriverAIOCBSync {
46 BlockDriverAIOCB common;
47 QEMUBH *bh;
48 int ret;
49 } BlockDriverAIOCBSync;
51 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
52 int64_t sector_num, uint8_t *buf, int nb_sectors,
53 BlockDriverCompletionFunc *cb, void *opaque);
54 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
55 int64_t sector_num, const uint8_t *buf, int nb_sectors,
56 BlockDriverCompletionFunc *cb, void *opaque);
57 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
58 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
59 uint8_t *buf, int nb_sectors);
60 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
61 const uint8_t *buf, int nb_sectors);
63 BlockDriverState *bdrv_first;
65 static BlockDriver *first_drv;
67 int path_is_absolute(const char *path)
69 const char *p;
70 #ifdef _WIN32
71 /* specific case for names like: "\\.\d:" */
72 if (*path == '/' || *path == '\\')
73 return 1;
74 #endif
75 p = strchr(path, ':');
76 if (p)
77 p++;
78 else
79 p = path;
80 #ifdef _WIN32
81 return (*p == '/' || *p == '\\');
82 #else
83 return (*p == '/');
84 #endif
87 /* if filename is absolute, just copy it to dest. Otherwise, build a
88 path to it by considering it is relative to base_path. URL are
89 supported. */
90 void path_combine(char *dest, int dest_size,
91 const char *base_path,
92 const char *filename)
94 const char *p, *p1;
95 int len;
97 if (dest_size <= 0)
98 return;
99 if (path_is_absolute(filename)) {
100 pstrcpy(dest, dest_size, filename);
101 } else {
102 p = strchr(base_path, ':');
103 if (p)
104 p++;
105 else
106 p = base_path;
107 p1 = strrchr(base_path, '/');
108 #ifdef _WIN32
110 const char *p2;
111 p2 = strrchr(base_path, '\\');
112 if (!p1 || p2 > p1)
113 p1 = p2;
115 #endif
116 if (p1)
117 p1++;
118 else
119 p1 = base_path;
120 if (p1 > p)
121 p = p1;
122 len = p - base_path;
123 if (len > dest_size - 1)
124 len = dest_size - 1;
125 memcpy(dest, base_path, len);
126 dest[len] = '\0';
127 pstrcat(dest, dest_size, filename);
132 static void bdrv_register(BlockDriver *bdrv)
134 if (!bdrv->bdrv_aio_read) {
135 /* add AIO emulation layer */
136 bdrv->bdrv_aio_read = bdrv_aio_read_em;
137 bdrv->bdrv_aio_write = bdrv_aio_write_em;
138 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
139 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
140 } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
141 /* add synchronous IO emulation layer */
142 bdrv->bdrv_read = bdrv_read_em;
143 bdrv->bdrv_write = bdrv_write_em;
145 bdrv->next = first_drv;
146 first_drv = bdrv;
149 /* create a new block device (by default it is empty) */
150 BlockDriverState *bdrv_new(const char *device_name)
152 BlockDriverState **pbs, *bs;
154 bs = qemu_mallocz(sizeof(BlockDriverState));
155 if(!bs)
156 return NULL;
157 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
158 if (device_name[0] != '\0') {
159 /* insert at the end */
160 pbs = &bdrv_first;
161 while (*pbs != NULL)
162 pbs = &(*pbs)->next;
163 *pbs = bs;
165 return bs;
168 BlockDriver *bdrv_find_format(const char *format_name)
170 BlockDriver *drv1;
171 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
172 if (!strcmp(drv1->format_name, format_name))
173 return drv1;
175 return NULL;
178 int bdrv_create(BlockDriver *drv,
179 const char *filename, int64_t size_in_sectors,
180 const char *backing_file, int flags)
182 if (!drv->bdrv_create)
183 return -ENOTSUP;
184 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
187 #ifdef _WIN32
188 void get_tmp_filename(char *filename, int size)
190 char temp_dir[MAX_PATH];
192 GetTempPath(MAX_PATH, temp_dir);
193 GetTempFileName(temp_dir, "qem", 0, filename);
195 #else
196 void get_tmp_filename(char *filename, int size)
198 int fd;
199 const char *tmpdir;
200 /* XXX: race condition possible */
201 tmpdir = getenv("TMPDIR");
202 if (!tmpdir)
203 tmpdir = "/tmp";
204 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
205 fd = mkstemp(filename);
206 close(fd);
208 #endif
210 #ifdef _WIN32
211 static int is_windows_drive_prefix(const char *filename)
213 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
214 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
215 filename[1] == ':');
218 static int is_windows_drive(const char *filename)
220 if (is_windows_drive_prefix(filename) &&
221 filename[2] == '\0')
222 return 1;
223 if (strstart(filename, "\\\\.\\", NULL) ||
224 strstart(filename, "//./", NULL))
225 return 1;
226 return 0;
228 #endif
230 static BlockDriver *find_protocol(const char *filename)
232 BlockDriver *drv1;
233 char protocol[128];
234 int len;
235 const char *p;
237 #ifdef _WIN32
238 if (is_windows_drive(filename) ||
239 is_windows_drive_prefix(filename))
240 return &bdrv_raw;
241 #endif
242 p = strchr(filename, ':');
243 if (!p)
244 return &bdrv_raw;
245 len = p - filename;
246 if (len > sizeof(protocol) - 1)
247 len = sizeof(protocol) - 1;
248 memcpy(protocol, filename, len);
249 protocol[len] = '\0';
250 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
251 if (drv1->protocol_name &&
252 !strcmp(drv1->protocol_name, protocol))
253 return drv1;
255 return NULL;
258 /* XXX: force raw format if block or character device ? It would
259 simplify the BSD case */
260 static BlockDriver *find_image_format(const char *filename)
262 int ret, score, score_max;
263 BlockDriver *drv1, *drv;
264 uint8_t buf[2048];
265 BlockDriverState *bs;
267 /* detect host devices. By convention, /dev/cdrom[N] is always
268 recognized as a host CDROM */
269 if (strstart(filename, "/dev/cdrom", NULL))
270 return &bdrv_host_device;
271 #ifdef _WIN32
272 if (is_windows_drive(filename))
273 return &bdrv_host_device;
274 #else
276 struct stat st;
277 if (stat(filename, &st) >= 0 &&
278 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
279 return &bdrv_host_device;
282 #endif
284 drv = find_protocol(filename);
285 /* no need to test disk image formats for vvfat */
286 if (drv == &bdrv_vvfat)
287 return drv;
289 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
290 if (ret < 0)
291 return NULL;
292 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
293 bdrv_delete(bs);
294 if (ret < 0) {
295 return NULL;
298 score_max = 0;
299 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
300 if (drv1->bdrv_probe) {
301 score = drv1->bdrv_probe(buf, ret, filename);
302 if (score > score_max) {
303 score_max = score;
304 drv = drv1;
308 return drv;
311 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
313 BlockDriverState *bs;
314 int ret;
316 bs = bdrv_new("");
317 if (!bs)
318 return -ENOMEM;
319 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
320 if (ret < 0) {
321 bdrv_delete(bs);
322 return ret;
324 *pbs = bs;
325 return 0;
328 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
330 return bdrv_open2(bs, filename, flags, NULL);
333 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
334 BlockDriver *drv)
336 int ret, open_flags;
337 char tmp_filename[PATH_MAX];
338 char backing_filename[PATH_MAX];
340 bs->read_only = 0;
341 bs->is_temporary = 0;
342 bs->encrypted = 0;
344 if (flags & BDRV_O_SNAPSHOT) {
345 BlockDriverState *bs1;
346 int64_t total_size;
347 int is_protocol = 0;
349 /* if snapshot, we create a temporary backing file and open it
350 instead of opening 'filename' directly */
352 /* if there is a backing file, use it */
353 bs1 = bdrv_new("");
354 if (!bs1) {
355 return -ENOMEM;
357 if (bdrv_open(bs1, filename, 0) < 0) {
358 bdrv_delete(bs1);
359 return -1;
361 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
363 if (bs1->drv && bs1->drv->protocol_name)
364 is_protocol = 1;
366 bdrv_delete(bs1);
368 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
370 /* Real path is meaningless for protocols */
371 if (is_protocol)
372 snprintf(backing_filename, sizeof(backing_filename),
373 "%s", filename);
374 else
375 realpath(filename, backing_filename);
377 if (bdrv_create(&bdrv_qcow2, tmp_filename,
378 total_size, backing_filename, 0) < 0) {
379 return -1;
381 filename = tmp_filename;
382 bs->is_temporary = 1;
385 pstrcpy(bs->filename, sizeof(bs->filename), filename);
386 if (flags & BDRV_O_FILE) {
387 drv = find_protocol(filename);
388 if (!drv)
389 return -ENOENT;
390 } else {
391 if (!drv) {
392 drv = find_image_format(filename);
393 if (!drv)
394 return -1;
397 bs->drv = drv;
398 bs->opaque = qemu_mallocz(drv->instance_size);
399 if (bs->opaque == NULL && drv->instance_size > 0)
400 return -1;
401 /* Note: for compatibility, we open disk image files as RDWR, and
402 RDONLY as fallback */
403 if (!(flags & BDRV_O_FILE))
404 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
405 else
406 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
407 ret = drv->bdrv_open(bs, filename, open_flags);
408 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
409 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
410 bs->read_only = 1;
412 if (ret < 0) {
413 qemu_free(bs->opaque);
414 bs->opaque = NULL;
415 bs->drv = NULL;
416 return ret;
418 if (drv->bdrv_getlength) {
419 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
421 #ifndef _WIN32
422 if (bs->is_temporary) {
423 unlink(filename);
425 #endif
426 if (bs->backing_file[0] != '\0') {
427 /* if there is a backing file, use it */
428 bs->backing_hd = bdrv_new("");
429 if (!bs->backing_hd) {
430 fail:
431 bdrv_close(bs);
432 return -ENOMEM;
434 path_combine(backing_filename, sizeof(backing_filename),
435 filename, bs->backing_file);
436 if (bdrv_open(bs->backing_hd, backing_filename, open_flags) < 0)
437 goto fail;
440 /* call the change callback */
441 bs->media_changed = 1;
442 if (bs->change_cb)
443 bs->change_cb(bs->change_opaque);
445 return 0;
448 void bdrv_close(BlockDriverState *bs)
450 if (bs->drv) {
451 if (bs->backing_hd)
452 bdrv_delete(bs->backing_hd);
453 bs->drv->bdrv_close(bs);
454 qemu_free(bs->opaque);
455 #ifdef _WIN32
456 if (bs->is_temporary) {
457 unlink(bs->filename);
459 #endif
460 bs->opaque = NULL;
461 bs->drv = NULL;
463 /* call the change callback */
464 bs->media_changed = 1;
465 if (bs->change_cb)
466 bs->change_cb(bs->change_opaque);
470 void bdrv_delete(BlockDriverState *bs)
472 BlockDriverState **pbs;
474 pbs = &bdrv_first;
475 while (*pbs != bs && *pbs != NULL)
476 pbs = &(*pbs)->next;
477 if (*pbs == bs)
478 *pbs = bs->next;
480 bdrv_close(bs);
481 qemu_free(bs);
484 /* commit COW file into the raw image */
485 int bdrv_commit(BlockDriverState *bs)
487 BlockDriver *drv = bs->drv;
488 int64_t i, total_sectors;
489 int n, j;
490 unsigned char sector[512];
492 if (!drv)
493 return -ENOMEDIUM;
495 if (bs->read_only) {
496 return -EACCES;
499 if (!bs->backing_hd) {
500 return -ENOTSUP;
503 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
504 for (i = 0; i < total_sectors;) {
505 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
506 for(j = 0; j < n; j++) {
507 if (bdrv_read(bs, i, sector, 1) != 0) {
508 return -EIO;
511 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
512 return -EIO;
514 i++;
516 } else {
517 i += n;
521 if (drv->bdrv_make_empty)
522 return drv->bdrv_make_empty(bs);
524 return 0;
527 /* return < 0 if error. See bdrv_write() for the return codes */
528 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
529 uint8_t *buf, int nb_sectors)
531 BlockDriver *drv = bs->drv;
533 if (!drv)
534 return -ENOMEDIUM;
536 if (drv->bdrv_pread) {
537 int ret, len;
538 len = nb_sectors * 512;
539 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
540 if (ret < 0)
541 return ret;
542 else if (ret != len)
543 return -EINVAL;
544 else {
545 bs->rd_bytes += (unsigned) len;
546 bs->rd_ops ++;
547 return 0;
549 } else {
550 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
554 /* Return < 0 if error. Important errors are:
555 -EIO generic I/O error (may happen for all errors)
556 -ENOMEDIUM No media inserted.
557 -EINVAL Invalid sector number or nb_sectors
558 -EACCES Trying to write a read-only device
560 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
561 const uint8_t *buf, int nb_sectors)
563 BlockDriver *drv = bs->drv;
564 if (!bs->drv)
565 return -ENOMEDIUM;
566 if (bs->read_only)
567 return -EACCES;
568 if (drv->bdrv_pwrite) {
569 int ret, len;
570 len = nb_sectors * 512;
571 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
572 if (ret < 0)
573 return ret;
574 else if (ret != len)
575 return -EIO;
576 else {
577 bs->wr_bytes += (unsigned) len;
578 bs->wr_ops ++;
579 return 0;
581 } else {
582 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
586 static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
587 uint8_t *buf, int count1)
589 uint8_t tmp_buf[SECTOR_SIZE];
590 int len, nb_sectors, count;
591 int64_t sector_num;
593 count = count1;
594 /* first read to align to sector start */
595 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
596 if (len > count)
597 len = count;
598 sector_num = offset >> SECTOR_BITS;
599 if (len > 0) {
600 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
601 return -EIO;
602 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
603 count -= len;
604 if (count == 0)
605 return count1;
606 sector_num++;
607 buf += len;
610 /* read the sectors "in place" */
611 nb_sectors = count >> SECTOR_BITS;
612 if (nb_sectors > 0) {
613 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
614 return -EIO;
615 sector_num += nb_sectors;
616 len = nb_sectors << SECTOR_BITS;
617 buf += len;
618 count -= len;
621 /* add data from the last sector */
622 if (count > 0) {
623 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
624 return -EIO;
625 memcpy(buf, tmp_buf, count);
627 return count1;
630 static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
631 const uint8_t *buf, int count1)
633 uint8_t tmp_buf[SECTOR_SIZE];
634 int len, nb_sectors, count;
635 int64_t sector_num;
637 count = count1;
638 /* first write to align to sector start */
639 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
640 if (len > count)
641 len = count;
642 sector_num = offset >> SECTOR_BITS;
643 if (len > 0) {
644 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
645 return -EIO;
646 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
647 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
648 return -EIO;
649 count -= len;
650 if (count == 0)
651 return count1;
652 sector_num++;
653 buf += len;
656 /* write the sectors "in place" */
657 nb_sectors = count >> SECTOR_BITS;
658 if (nb_sectors > 0) {
659 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
660 return -EIO;
661 sector_num += nb_sectors;
662 len = nb_sectors << SECTOR_BITS;
663 buf += len;
664 count -= len;
667 /* add data from the last sector */
668 if (count > 0) {
669 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
670 return -EIO;
671 memcpy(tmp_buf, buf, count);
672 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
673 return -EIO;
675 return count1;
679 * Read with byte offsets (needed only for file protocols)
681 int bdrv_pread(BlockDriverState *bs, int64_t offset,
682 void *buf1, int count1)
684 BlockDriver *drv = bs->drv;
686 if (!drv)
687 return -ENOMEDIUM;
688 if (!drv->bdrv_pread)
689 return bdrv_pread_em(bs, offset, buf1, count1);
690 return drv->bdrv_pread(bs, offset, buf1, count1);
694 * Write with byte offsets (needed only for file protocols)
696 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
697 const void *buf1, int count1)
699 BlockDriver *drv = bs->drv;
701 if (!drv)
702 return -ENOMEDIUM;
703 if (!drv->bdrv_pwrite)
704 return bdrv_pwrite_em(bs, offset, buf1, count1);
705 return drv->bdrv_pwrite(bs, offset, buf1, count1);
709 * Truncate file to 'offset' bytes (needed only for file protocols)
711 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
713 BlockDriver *drv = bs->drv;
714 if (!drv)
715 return -ENOMEDIUM;
716 if (!drv->bdrv_truncate)
717 return -ENOTSUP;
718 return drv->bdrv_truncate(bs, offset);
722 * Length of a file in bytes. Return < 0 if error or unknown.
724 int64_t bdrv_getlength(BlockDriverState *bs)
726 BlockDriver *drv = bs->drv;
727 if (!drv)
728 return -ENOMEDIUM;
729 if (!drv->bdrv_getlength) {
730 /* legacy mode */
731 return bs->total_sectors * SECTOR_SIZE;
733 return drv->bdrv_getlength(bs);
736 /* return 0 as number of sectors if no device present or error */
737 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
739 int64_t length;
740 length = bdrv_getlength(bs);
741 if (length < 0)
742 length = 0;
743 else
744 length = length >> SECTOR_BITS;
745 *nb_sectors_ptr = length;
748 struct partition {
749 uint8_t boot_ind; /* 0x80 - active */
750 uint8_t head; /* starting head */
751 uint8_t sector; /* starting sector */
752 uint8_t cyl; /* starting cylinder */
753 uint8_t sys_ind; /* What partition type */
754 uint8_t end_head; /* end head */
755 uint8_t end_sector; /* end sector */
756 uint8_t end_cyl; /* end cylinder */
757 uint32_t start_sect; /* starting sector counting from 0 */
758 uint32_t nr_sects; /* nr of sectors in partition */
759 } __attribute__((packed));
761 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
762 static int guess_disk_lchs(BlockDriverState *bs,
763 int *pcylinders, int *pheads, int *psectors)
765 uint8_t *buf;
766 int ret, i, heads, sectors, cylinders;
767 struct partition *p;
768 uint32_t nr_sects;
769 uint64_t nb_sectors;
771 buf = qemu_memalign(512, 512);
772 if (buf == NULL)
773 return -1;
775 bdrv_get_geometry(bs, &nb_sectors);
777 ret = bdrv_read(bs, 0, buf, 1);
778 if (ret < 0)
779 return -1;
780 /* test msdos magic */
781 if (buf[510] != 0x55 || buf[511] != 0xaa) {
782 qemu_free(buf);
783 return -1;
785 for(i = 0; i < 4; i++) {
786 p = ((struct partition *)(buf + 0x1be)) + i;
787 nr_sects = le32_to_cpu(p->nr_sects);
788 if (nr_sects && p->end_head) {
789 /* We make the assumption that the partition terminates on
790 a cylinder boundary */
791 heads = p->end_head + 1;
792 sectors = p->end_sector & 63;
793 if (sectors == 0)
794 continue;
795 cylinders = nb_sectors / (heads * sectors);
796 if (cylinders < 1 || cylinders > 16383)
797 continue;
798 *pheads = heads;
799 *psectors = sectors;
800 *pcylinders = cylinders;
801 #if 0
802 printf("guessed geometry: LCHS=%d %d %d\n",
803 cylinders, heads, sectors);
804 #endif
805 qemu_free(buf);
806 return 0;
809 qemu_free(buf);
810 return -1;
813 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
815 int translation, lba_detected = 0;
816 int cylinders, heads, secs;
817 uint64_t nb_sectors;
819 /* if a geometry hint is available, use it */
820 bdrv_get_geometry(bs, &nb_sectors);
821 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
822 translation = bdrv_get_translation_hint(bs);
823 if (cylinders != 0) {
824 *pcyls = cylinders;
825 *pheads = heads;
826 *psecs = secs;
827 } else {
828 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
829 if (heads > 16) {
830 /* if heads > 16, it means that a BIOS LBA
831 translation was active, so the default
832 hardware geometry is OK */
833 lba_detected = 1;
834 goto default_geometry;
835 } else {
836 *pcyls = cylinders;
837 *pheads = heads;
838 *psecs = secs;
839 /* disable any translation to be in sync with
840 the logical geometry */
841 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
842 bdrv_set_translation_hint(bs,
843 BIOS_ATA_TRANSLATION_NONE);
846 } else {
847 default_geometry:
848 /* if no geometry, use a standard physical disk geometry */
849 cylinders = nb_sectors / (16 * 63);
851 if (cylinders > 16383)
852 cylinders = 16383;
853 else if (cylinders < 2)
854 cylinders = 2;
855 *pcyls = cylinders;
856 *pheads = 16;
857 *psecs = 63;
858 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
859 if ((*pcyls * *pheads) <= 131072) {
860 bdrv_set_translation_hint(bs,
861 BIOS_ATA_TRANSLATION_LARGE);
862 } else {
863 bdrv_set_translation_hint(bs,
864 BIOS_ATA_TRANSLATION_LBA);
868 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
872 void bdrv_set_geometry_hint(BlockDriverState *bs,
873 int cyls, int heads, int secs)
875 bs->cyls = cyls;
876 bs->heads = heads;
877 bs->secs = secs;
880 void bdrv_set_type_hint(BlockDriverState *bs, int type)
882 bs->type = type;
883 bs->removable = ((type == BDRV_TYPE_CDROM ||
884 type == BDRV_TYPE_FLOPPY));
887 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
889 bs->translation = translation;
892 void bdrv_get_geometry_hint(BlockDriverState *bs,
893 int *pcyls, int *pheads, int *psecs)
895 *pcyls = bs->cyls;
896 *pheads = bs->heads;
897 *psecs = bs->secs;
900 int bdrv_get_type_hint(BlockDriverState *bs)
902 return bs->type;
905 int bdrv_get_translation_hint(BlockDriverState *bs)
907 return bs->translation;
910 int bdrv_is_removable(BlockDriverState *bs)
912 return bs->removable;
915 int bdrv_is_read_only(BlockDriverState *bs)
917 return bs->read_only;
920 int bdrv_is_sg(BlockDriverState *bs)
922 return bs->sg;
925 /* XXX: no longer used */
926 void bdrv_set_change_cb(BlockDriverState *bs,
927 void (*change_cb)(void *opaque), void *opaque)
929 bs->change_cb = change_cb;
930 bs->change_opaque = opaque;
933 int bdrv_is_encrypted(BlockDriverState *bs)
935 if (bs->backing_hd && bs->backing_hd->encrypted)
936 return 1;
937 return bs->encrypted;
940 int bdrv_set_key(BlockDriverState *bs, const char *key)
942 int ret;
943 if (bs->backing_hd && bs->backing_hd->encrypted) {
944 ret = bdrv_set_key(bs->backing_hd, key);
945 if (ret < 0)
946 return ret;
947 if (!bs->encrypted)
948 return 0;
950 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
951 return -1;
952 return bs->drv->bdrv_set_key(bs, key);
955 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
957 if (!bs->drv) {
958 buf[0] = '\0';
959 } else {
960 pstrcpy(buf, buf_size, bs->drv->format_name);
964 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
965 void *opaque)
967 BlockDriver *drv;
969 for (drv = first_drv; drv != NULL; drv = drv->next) {
970 it(opaque, drv->format_name);
974 BlockDriverState *bdrv_find(const char *name)
976 BlockDriverState *bs;
978 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
979 if (!strcmp(name, bs->device_name))
980 return bs;
982 return NULL;
985 void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
987 BlockDriverState *bs;
989 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
990 it(opaque, bs->device_name);
994 const char *bdrv_get_device_name(BlockDriverState *bs)
996 return bs->device_name;
999 void bdrv_flush(BlockDriverState *bs)
1001 if (bs->drv->bdrv_flush)
1002 bs->drv->bdrv_flush(bs);
1003 if (bs->backing_hd)
1004 bdrv_flush(bs->backing_hd);
1007 void bdrv_iterate_writeable(void (*it)(BlockDriverState *bs))
1009 BlockDriverState *bs;
1011 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1012 if (bs->drv && !bdrv_is_read_only(bs) &&
1013 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1014 it(bs);
1017 void bdrv_flush_all(void)
1019 BlockDriverState *bs;
1021 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1022 if (bs->drv && !bdrv_is_read_only(bs) &&
1023 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1024 bdrv_flush(bs);
1028 * Returns true iff the specified sector is present in the disk image. Drivers
1029 * not implementing the functionality are assumed to not support backing files,
1030 * hence all their sectors are reported as allocated.
1032 * 'pnum' is set to the number of sectors (including and immediately following
1033 * the specified sector) that are known to be in the same
1034 * allocated/unallocated state.
1036 * 'nb_sectors' is the max value 'pnum' should be set to.
1038 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1039 int *pnum)
1041 int64_t n;
1042 if (!bs->drv->bdrv_is_allocated) {
1043 if (sector_num >= bs->total_sectors) {
1044 *pnum = 0;
1045 return 0;
1047 n = bs->total_sectors - sector_num;
1048 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1049 return 1;
1051 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1054 void bdrv_info(void)
1056 BlockDriverState *bs;
1058 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1059 term_printf("%s:", bs->device_name);
1060 term_printf(" type=");
1061 switch(bs->type) {
1062 case BDRV_TYPE_HD:
1063 term_printf("hd");
1064 break;
1065 case BDRV_TYPE_CDROM:
1066 term_printf("cdrom");
1067 break;
1068 case BDRV_TYPE_FLOPPY:
1069 term_printf("floppy");
1070 break;
1072 term_printf(" removable=%d", bs->removable);
1073 if (bs->removable) {
1074 term_printf(" locked=%d", bs->locked);
1076 if (bs->drv) {
1077 term_printf(" file=");
1078 term_print_filename(bs->filename);
1079 if (bs->backing_file[0] != '\0') {
1080 term_printf(" backing_file=");
1081 term_print_filename(bs->backing_file);
1083 term_printf(" ro=%d", bs->read_only);
1084 term_printf(" drv=%s", bs->drv->format_name);
1085 if (bs->encrypted)
1086 term_printf(" encrypted");
1087 } else {
1088 term_printf(" [not inserted]");
1090 term_printf("\n");
1094 /* The "info blockstats" command. */
1095 void bdrv_info_stats (void)
1097 BlockDriverState *bs;
1099 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1100 term_printf ("%s:"
1101 " rd_bytes=%" PRIu64
1102 " wr_bytes=%" PRIu64
1103 " rd_operations=%" PRIu64
1104 " wr_operations=%" PRIu64
1105 "\n",
1106 bs->device_name,
1107 bs->rd_bytes, bs->wr_bytes,
1108 bs->rd_ops, bs->wr_ops);
1112 void bdrv_get_backing_filename(BlockDriverState *bs,
1113 char *filename, int filename_size)
1115 if (!bs->backing_hd) {
1116 pstrcpy(filename, filename_size, "");
1117 } else {
1118 pstrcpy(filename, filename_size, bs->backing_file);
1122 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1123 const uint8_t *buf, int nb_sectors)
1125 BlockDriver *drv = bs->drv;
1126 if (!drv)
1127 return -ENOMEDIUM;
1128 if (!drv->bdrv_write_compressed)
1129 return -ENOTSUP;
1130 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1133 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1135 BlockDriver *drv = bs->drv;
1136 if (!drv)
1137 return -ENOMEDIUM;
1138 if (!drv->bdrv_get_info)
1139 return -ENOTSUP;
1140 memset(bdi, 0, sizeof(*bdi));
1141 return drv->bdrv_get_info(bs, bdi);
1144 /**************************************************************/
1145 /* handling of snapshots */
1147 int bdrv_snapshot_create(BlockDriverState *bs,
1148 QEMUSnapshotInfo *sn_info)
1150 BlockDriver *drv = bs->drv;
1151 if (!drv)
1152 return -ENOMEDIUM;
1153 if (!drv->bdrv_snapshot_create)
1154 return -ENOTSUP;
1155 return drv->bdrv_snapshot_create(bs, sn_info);
1158 int bdrv_snapshot_goto(BlockDriverState *bs,
1159 const char *snapshot_id)
1161 BlockDriver *drv = bs->drv;
1162 if (!drv)
1163 return -ENOMEDIUM;
1164 if (!drv->bdrv_snapshot_goto)
1165 return -ENOTSUP;
1166 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1169 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1171 BlockDriver *drv = bs->drv;
1172 if (!drv)
1173 return -ENOMEDIUM;
1174 if (!drv->bdrv_snapshot_delete)
1175 return -ENOTSUP;
1176 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1179 int bdrv_snapshot_list(BlockDriverState *bs,
1180 QEMUSnapshotInfo **psn_info)
1182 BlockDriver *drv = bs->drv;
1183 if (!drv)
1184 return -ENOMEDIUM;
1185 if (!drv->bdrv_snapshot_list)
1186 return -ENOTSUP;
1187 return drv->bdrv_snapshot_list(bs, psn_info);
1190 #define NB_SUFFIXES 4
1192 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1194 static const char suffixes[NB_SUFFIXES] = "KMGT";
1195 int64_t base;
1196 int i;
1198 if (size <= 999) {
1199 snprintf(buf, buf_size, "%" PRId64, size);
1200 } else {
1201 base = 1024;
1202 for(i = 0; i < NB_SUFFIXES; i++) {
1203 if (size < (10 * base)) {
1204 snprintf(buf, buf_size, "%0.1f%c",
1205 (double)size / base,
1206 suffixes[i]);
1207 break;
1208 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1209 snprintf(buf, buf_size, "%" PRId64 "%c",
1210 ((size + (base >> 1)) / base),
1211 suffixes[i]);
1212 break;
1214 base = base * 1024;
1217 return buf;
1220 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1222 char buf1[128], date_buf[128], clock_buf[128];
1223 #ifdef _WIN32
1224 struct tm *ptm;
1225 #else
1226 struct tm tm;
1227 #endif
1228 time_t ti;
1229 int64_t secs;
1231 if (!sn) {
1232 snprintf(buf, buf_size,
1233 "%-10s%-20s%7s%20s%15s",
1234 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1235 } else {
1236 ti = sn->date_sec;
1237 #ifdef _WIN32
1238 ptm = localtime(&ti);
1239 strftime(date_buf, sizeof(date_buf),
1240 "%Y-%m-%d %H:%M:%S", ptm);
1241 #else
1242 localtime_r(&ti, &tm);
1243 strftime(date_buf, sizeof(date_buf),
1244 "%Y-%m-%d %H:%M:%S", &tm);
1245 #endif
1246 secs = sn->vm_clock_nsec / 1000000000;
1247 snprintf(clock_buf, sizeof(clock_buf),
1248 "%02d:%02d:%02d.%03d",
1249 (int)(secs / 3600),
1250 (int)((secs / 60) % 60),
1251 (int)(secs % 60),
1252 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1253 snprintf(buf, buf_size,
1254 "%-10s%-20s%7s%20s%15s",
1255 sn->id_str, sn->name,
1256 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1257 date_buf,
1258 clock_buf);
1260 return buf;
1264 /**************************************************************/
1265 /* async I/Os */
1267 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1268 uint8_t *buf, int nb_sectors,
1269 BlockDriverCompletionFunc *cb, void *opaque)
1271 BlockDriver *drv = bs->drv;
1272 BlockDriverAIOCB *ret;
1274 if (!drv)
1275 return NULL;
1277 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1279 if (ret) {
1280 /* Update stats even though technically transfer has not happened. */
1281 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1282 bs->rd_ops ++;
1285 return ret;
1288 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1289 const uint8_t *buf, int nb_sectors,
1290 BlockDriverCompletionFunc *cb, void *opaque)
1292 BlockDriver *drv = bs->drv;
1293 BlockDriverAIOCB *ret;
1295 if (!drv)
1296 return NULL;
1297 if (bs->read_only)
1298 return NULL;
1300 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1302 if (ret) {
1303 /* Update stats even though technically transfer has not happened. */
1304 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1305 bs->wr_ops ++;
1308 return ret;
1311 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1313 BlockDriver *drv = acb->bs->drv;
1315 drv->bdrv_aio_cancel(acb);
1319 /**************************************************************/
1320 /* async block device emulation */
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);
1368 /**************************************************************/
1369 /* sync block device emulation */
1371 static void bdrv_rw_em_cb(void *opaque, int ret)
1373 *(int *)opaque = ret;
1376 #define NOT_DONE 0x7fffffff
1378 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1379 uint8_t *buf, int nb_sectors)
1381 int async_ret;
1382 BlockDriverAIOCB *acb;
1384 async_ret = NOT_DONE;
1385 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1386 bdrv_rw_em_cb, &async_ret);
1387 if (acb == NULL)
1388 return -1;
1390 while (async_ret == NOT_DONE) {
1391 qemu_aio_wait();
1394 return async_ret;
1397 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1398 const uint8_t *buf, int nb_sectors)
1400 int async_ret;
1401 BlockDriverAIOCB *acb;
1403 async_ret = NOT_DONE;
1404 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1405 bdrv_rw_em_cb, &async_ret);
1406 if (acb == NULL)
1407 return -1;
1408 while (async_ret == NOT_DONE) {
1409 qemu_aio_wait();
1411 return async_ret;
1414 void bdrv_init(void)
1416 bdrv_register(&bdrv_raw);
1417 bdrv_register(&bdrv_host_device);
1418 #ifndef _WIN32
1419 bdrv_register(&bdrv_cow);
1420 #endif
1421 bdrv_register(&bdrv_qcow);
1422 bdrv_register(&bdrv_vmdk);
1423 bdrv_register(&bdrv_cloop);
1424 bdrv_register(&bdrv_dmg);
1425 bdrv_register(&bdrv_bochs);
1426 bdrv_register(&bdrv_vpc);
1427 bdrv_register(&bdrv_vvfat);
1428 bdrv_register(&bdrv_qcow2);
1429 bdrv_register(&bdrv_parallels);
1430 bdrv_register(&bdrv_nbd);
1433 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1434 void *opaque)
1436 BlockDriver *drv;
1437 BlockDriverAIOCB *acb;
1439 drv = bs->drv;
1440 if (drv->free_aiocb) {
1441 acb = drv->free_aiocb;
1442 drv->free_aiocb = acb->next;
1443 } else {
1444 acb = qemu_mallocz(drv->aiocb_size);
1445 if (!acb)
1446 return NULL;
1448 acb->bs = bs;
1449 acb->cb = cb;
1450 acb->opaque = opaque;
1451 return acb;
1454 void qemu_aio_release(void *p)
1456 BlockDriverAIOCB *acb = p;
1457 BlockDriver *drv = acb->bs->drv;
1458 acb->next = drv->free_aiocb;
1459 drv->free_aiocb = acb;
1462 /**************************************************************/
1463 /* removable device support */
1466 * Return TRUE if the media is present
1468 int bdrv_is_inserted(BlockDriverState *bs)
1470 BlockDriver *drv = bs->drv;
1471 int ret;
1472 if (!drv)
1473 return 0;
1474 if (!drv->bdrv_is_inserted)
1475 return 1;
1476 ret = drv->bdrv_is_inserted(bs);
1477 return ret;
1481 * Return TRUE if the media changed since the last call to this
1482 * function. It is currently only used for floppy disks
1484 int bdrv_media_changed(BlockDriverState *bs)
1486 BlockDriver *drv = bs->drv;
1487 int ret;
1489 if (!drv || !drv->bdrv_media_changed)
1490 ret = -ENOTSUP;
1491 else
1492 ret = drv->bdrv_media_changed(bs);
1493 if (ret == -ENOTSUP)
1494 ret = bs->media_changed;
1495 bs->media_changed = 0;
1496 return ret;
1500 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1502 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1504 BlockDriver *drv = bs->drv;
1505 int ret;
1507 if (!drv || !drv->bdrv_eject) {
1508 ret = -ENOTSUP;
1509 } else {
1510 ret = drv->bdrv_eject(bs, eject_flag);
1512 if (ret == -ENOTSUP) {
1513 if (eject_flag)
1514 bdrv_close(bs);
1518 int bdrv_is_locked(BlockDriverState *bs)
1520 return bs->locked;
1524 * Lock or unlock the media (if it is locked, the user won't be able
1525 * to eject it manually).
1527 void bdrv_set_locked(BlockDriverState *bs, int locked)
1529 BlockDriver *drv = bs->drv;
1531 bs->locked = locked;
1532 if (drv && drv->bdrv_set_locked) {
1533 drv->bdrv_set_locked(bs, locked);
1537 /* needed for generic scsi interface */
1539 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1541 BlockDriver *drv = bs->drv;
1543 if (drv && drv->bdrv_ioctl)
1544 return drv->bdrv_ioctl(bs, req, buf);
1545 return -ENOTSUP;