Add missing declaration for bdrv_flush_all
[qemu-kvm/amd-iommu.git] / block.c
blobb1650c01dbee851b02523074d59632abf52aa1c5
1 /*
2 * QEMU System Emulator block driver
4 * Copyright (c) 2003 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "qemu-common.h"
25 #ifndef QEMU_IMG
26 #include "console.h"
27 #endif
28 #include "block_int.h"
30 #ifdef _BSD
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/ioctl.h>
34 #include <sys/queue.h>
35 #include <sys/disk.h>
36 #endif
38 #define SECTOR_BITS 9
39 #define SECTOR_SIZE (1 << SECTOR_BITS)
41 typedef struct BlockDriverAIOCBSync {
42 BlockDriverAIOCB common;
43 QEMUBH *bh;
44 int ret;
45 } BlockDriverAIOCBSync;
47 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
48 int64_t sector_num, uint8_t *buf, int nb_sectors,
49 BlockDriverCompletionFunc *cb, void *opaque);
50 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
51 int64_t sector_num, const uint8_t *buf, int nb_sectors,
52 BlockDriverCompletionFunc *cb, void *opaque);
53 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
54 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
55 uint8_t *buf, int nb_sectors);
56 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
57 const uint8_t *buf, int nb_sectors);
59 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 /* XXX: race condition possible */
195 pstrcpy(filename, size, "/tmp/vl.XXXXXX");
196 fd = mkstemp(filename);
197 close(fd);
199 #endif
201 #ifdef _WIN32
202 static int is_windows_drive_prefix(const char *filename)
204 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
205 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
206 filename[1] == ':');
209 static int is_windows_drive(const char *filename)
211 if (is_windows_drive_prefix(filename) &&
212 filename[2] == '\0')
213 return 1;
214 if (strstart(filename, "\\\\.\\", NULL) ||
215 strstart(filename, "//./", NULL))
216 return 1;
217 return 0;
219 #endif
221 static BlockDriver *find_protocol(const char *filename)
223 BlockDriver *drv1;
224 char protocol[128];
225 int len;
226 const char *p;
228 #ifdef _WIN32
229 if (is_windows_drive(filename) ||
230 is_windows_drive_prefix(filename))
231 return &bdrv_raw;
232 #endif
233 p = strchr(filename, ':');
234 if (!p)
235 return &bdrv_raw;
236 len = p - filename;
237 if (len > sizeof(protocol) - 1)
238 len = sizeof(protocol) - 1;
239 memcpy(protocol, filename, len);
240 protocol[len] = '\0';
241 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
242 if (drv1->protocol_name &&
243 !strcmp(drv1->protocol_name, protocol))
244 return drv1;
246 return NULL;
249 /* XXX: force raw format if block or character device ? It would
250 simplify the BSD case */
251 static BlockDriver *find_image_format(const char *filename)
253 int ret, score, score_max;
254 BlockDriver *drv1, *drv;
255 uint8_t buf[2048];
256 BlockDriverState *bs;
258 /* detect host devices. By convention, /dev/cdrom[N] is always
259 recognized as a host CDROM */
260 if (strstart(filename, "/dev/cdrom", NULL))
261 return &bdrv_host_device;
262 #ifdef _WIN32
263 if (is_windows_drive(filename))
264 return &bdrv_host_device;
265 #else
267 struct stat st;
268 if (stat(filename, &st) >= 0 &&
269 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
270 return &bdrv_host_device;
273 #endif
275 drv = find_protocol(filename);
276 /* no need to test disk image formats for vvfat */
277 if (drv == &bdrv_vvfat)
278 return drv;
280 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
281 if (ret < 0)
282 return NULL;
283 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
284 bdrv_delete(bs);
285 if (ret < 0) {
286 return NULL;
289 score_max = 0;
290 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
291 if (drv1->bdrv_probe) {
292 score = drv1->bdrv_probe(buf, ret, filename);
293 if (score > score_max) {
294 score_max = score;
295 drv = drv1;
299 return drv;
302 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
304 BlockDriverState *bs;
305 int ret;
307 bs = bdrv_new("");
308 if (!bs)
309 return -ENOMEM;
310 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
311 if (ret < 0) {
312 bdrv_delete(bs);
313 return ret;
315 *pbs = bs;
316 return 0;
319 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
321 return bdrv_open2(bs, filename, flags, NULL);
324 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
325 BlockDriver *drv)
327 int ret, open_flags;
328 char tmp_filename[PATH_MAX];
329 char backing_filename[PATH_MAX];
331 bs->read_only = 0;
332 bs->is_temporary = 0;
333 bs->encrypted = 0;
335 if (flags & BDRV_O_SNAPSHOT) {
336 BlockDriverState *bs1;
337 int64_t total_size;
339 /* if snapshot, we create a temporary backing file and open it
340 instead of opening 'filename' directly */
342 /* if there is a backing file, use it */
343 bs1 = bdrv_new("");
344 if (!bs1) {
345 return -ENOMEM;
347 if (bdrv_open(bs1, filename, 0) < 0) {
348 bdrv_delete(bs1);
349 return -1;
351 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
352 bdrv_delete(bs1);
354 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
355 realpath(filename, backing_filename);
356 if (bdrv_create(&bdrv_qcow2, tmp_filename,
357 total_size, backing_filename, 0) < 0) {
358 return -1;
360 filename = tmp_filename;
361 bs->is_temporary = 1;
364 pstrcpy(bs->filename, sizeof(bs->filename), filename);
365 if (flags & BDRV_O_FILE) {
366 drv = find_protocol(filename);
367 if (!drv)
368 return -ENOENT;
369 } else {
370 if (!drv) {
371 drv = find_image_format(filename);
372 if (!drv)
373 return -1;
376 bs->drv = drv;
377 bs->opaque = qemu_mallocz(drv->instance_size);
378 if (bs->opaque == NULL && drv->instance_size > 0)
379 return -1;
380 /* Note: for compatibility, we open disk image files as RDWR, and
381 RDONLY as fallback */
382 if (!(flags & BDRV_O_FILE))
383 open_flags = BDRV_O_RDWR;
384 else
385 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
386 ret = drv->bdrv_open(bs, filename, open_flags);
387 if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
388 ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
389 bs->read_only = 1;
391 if (ret < 0) {
392 qemu_free(bs->opaque);
393 bs->opaque = NULL;
394 bs->drv = NULL;
395 return ret;
397 if (drv->bdrv_getlength) {
398 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
400 #ifndef _WIN32
401 if (bs->is_temporary) {
402 unlink(filename);
404 #endif
405 if (bs->backing_file[0] != '\0') {
406 /* if there is a backing file, use it */
407 bs->backing_hd = bdrv_new("");
408 if (!bs->backing_hd) {
409 fail:
410 bdrv_close(bs);
411 return -ENOMEM;
413 path_combine(backing_filename, sizeof(backing_filename),
414 filename, bs->backing_file);
415 if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
416 goto fail;
419 /* call the change callback */
420 bs->media_changed = 1;
421 if (bs->change_cb)
422 bs->change_cb(bs->change_opaque);
424 return 0;
427 void bdrv_close(BlockDriverState *bs)
429 if (bs->drv) {
430 if (bs->backing_hd)
431 bdrv_delete(bs->backing_hd);
432 bs->drv->bdrv_close(bs);
433 qemu_free(bs->opaque);
434 #ifdef _WIN32
435 if (bs->is_temporary) {
436 unlink(bs->filename);
438 #endif
439 bs->opaque = NULL;
440 bs->drv = NULL;
442 /* call the change callback */
443 bs->media_changed = 1;
444 if (bs->change_cb)
445 bs->change_cb(bs->change_opaque);
449 void bdrv_delete(BlockDriverState *bs)
451 /* XXX: remove the driver list */
452 bdrv_close(bs);
453 qemu_free(bs);
456 /* commit COW file into the raw image */
457 int bdrv_commit(BlockDriverState *bs)
459 BlockDriver *drv = bs->drv;
460 int64_t i, total_sectors;
461 int n, j;
462 unsigned char sector[512];
464 if (!drv)
465 return -ENOMEDIUM;
467 if (bs->read_only) {
468 return -EACCES;
471 if (!bs->backing_hd) {
472 return -ENOTSUP;
475 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
476 for (i = 0; i < total_sectors;) {
477 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
478 for(j = 0; j < n; j++) {
479 if (bdrv_read(bs, i, sector, 1) != 0) {
480 return -EIO;
483 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
484 return -EIO;
486 i++;
488 } else {
489 i += n;
493 if (drv->bdrv_make_empty)
494 return drv->bdrv_make_empty(bs);
496 return 0;
499 /* return < 0 if error. See bdrv_write() for the return codes */
500 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
501 uint8_t *buf, int nb_sectors)
503 BlockDriver *drv = bs->drv;
505 if (!drv)
506 return -ENOMEDIUM;
508 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
509 memcpy(buf, bs->boot_sector_data, 512);
510 sector_num++;
511 nb_sectors--;
512 buf += 512;
513 if (nb_sectors == 0)
514 return 0;
516 if (drv->bdrv_pread) {
517 int ret, len;
518 len = nb_sectors * 512;
519 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
520 if (ret < 0)
521 return ret;
522 else if (ret != len)
523 return -EINVAL;
524 else {
525 bs->rd_bytes += (unsigned) len;
526 bs->rd_ops ++;
527 return 0;
529 } else {
530 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
534 /* Return < 0 if error. Important errors are:
535 -EIO generic I/O error (may happen for all errors)
536 -ENOMEDIUM No media inserted.
537 -EINVAL Invalid sector number or nb_sectors
538 -EACCES Trying to write a read-only device
540 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
541 const uint8_t *buf, int nb_sectors)
543 BlockDriver *drv = bs->drv;
544 if (!bs->drv)
545 return -ENOMEDIUM;
546 if (bs->read_only)
547 return -EACCES;
548 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
549 memcpy(bs->boot_sector_data, buf, 512);
551 if (drv->bdrv_pwrite) {
552 int ret, len;
553 len = nb_sectors * 512;
554 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
555 if (ret < 0)
556 return ret;
557 else if (ret != len)
558 return -EIO;
559 else {
560 bs->wr_bytes += (unsigned) len;
561 bs->wr_ops ++;
562 return 0;
564 } else {
565 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
569 static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
570 uint8_t *buf, int count1)
572 uint8_t tmp_buf[SECTOR_SIZE];
573 int len, nb_sectors, count;
574 int64_t sector_num;
576 count = count1;
577 /* first read to align to sector start */
578 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
579 if (len > count)
580 len = count;
581 sector_num = offset >> SECTOR_BITS;
582 if (len > 0) {
583 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
584 return -EIO;
585 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
586 count -= len;
587 if (count == 0)
588 return count1;
589 sector_num++;
590 buf += len;
593 /* read the sectors "in place" */
594 nb_sectors = count >> SECTOR_BITS;
595 if (nb_sectors > 0) {
596 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
597 return -EIO;
598 sector_num += nb_sectors;
599 len = nb_sectors << SECTOR_BITS;
600 buf += len;
601 count -= len;
604 /* add data from the last sector */
605 if (count > 0) {
606 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
607 return -EIO;
608 memcpy(buf, tmp_buf, count);
610 return count1;
613 static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
614 const uint8_t *buf, int count1)
616 uint8_t tmp_buf[SECTOR_SIZE];
617 int len, nb_sectors, count;
618 int64_t sector_num;
620 count = count1;
621 /* first write to align to sector start */
622 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
623 if (len > count)
624 len = count;
625 sector_num = offset >> SECTOR_BITS;
626 if (len > 0) {
627 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
628 return -EIO;
629 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
630 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
631 return -EIO;
632 count -= len;
633 if (count == 0)
634 return count1;
635 sector_num++;
636 buf += len;
639 /* write the sectors "in place" */
640 nb_sectors = count >> SECTOR_BITS;
641 if (nb_sectors > 0) {
642 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
643 return -EIO;
644 sector_num += nb_sectors;
645 len = nb_sectors << SECTOR_BITS;
646 buf += len;
647 count -= len;
650 /* add data from the last sector */
651 if (count > 0) {
652 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
653 return -EIO;
654 memcpy(tmp_buf, buf, count);
655 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
656 return -EIO;
658 return count1;
662 * Read with byte offsets (needed only for file protocols)
664 int bdrv_pread(BlockDriverState *bs, int64_t offset,
665 void *buf1, int count1)
667 BlockDriver *drv = bs->drv;
669 if (!drv)
670 return -ENOMEDIUM;
671 if (!drv->bdrv_pread)
672 return bdrv_pread_em(bs, offset, buf1, count1);
673 return drv->bdrv_pread(bs, offset, buf1, count1);
677 * Write with byte offsets (needed only for file protocols)
679 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
680 const void *buf1, int count1)
682 BlockDriver *drv = bs->drv;
684 if (!drv)
685 return -ENOMEDIUM;
686 if (!drv->bdrv_pwrite)
687 return bdrv_pwrite_em(bs, offset, buf1, count1);
688 return drv->bdrv_pwrite(bs, offset, buf1, count1);
692 * Truncate file to 'offset' bytes (needed only for file protocols)
694 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
696 BlockDriver *drv = bs->drv;
697 if (!drv)
698 return -ENOMEDIUM;
699 if (!drv->bdrv_truncate)
700 return -ENOTSUP;
701 return drv->bdrv_truncate(bs, offset);
705 * Length of a file in bytes. Return < 0 if error or unknown.
707 int64_t bdrv_getlength(BlockDriverState *bs)
709 BlockDriver *drv = bs->drv;
710 if (!drv)
711 return -ENOMEDIUM;
712 if (!drv->bdrv_getlength) {
713 /* legacy mode */
714 return bs->total_sectors * SECTOR_SIZE;
716 return drv->bdrv_getlength(bs);
719 /* return 0 as number of sectors if no device present or error */
720 void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
722 int64_t length;
723 length = bdrv_getlength(bs);
724 if (length < 0)
725 length = 0;
726 else
727 length = length >> SECTOR_BITS;
728 *nb_sectors_ptr = length;
731 /* force a given boot sector. */
732 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
734 bs->boot_sector_enabled = 1;
735 if (size > 512)
736 size = 512;
737 memcpy(bs->boot_sector_data, data, size);
738 memset(bs->boot_sector_data + size, 0, 512 - size);
741 struct partition {
742 uint8_t boot_ind; /* 0x80 - active */
743 uint8_t head; /* starting head */
744 uint8_t sector; /* starting sector */
745 uint8_t cyl; /* starting cylinder */
746 uint8_t sys_ind; /* What partition type */
747 uint8_t end_head; /* end head */
748 uint8_t end_sector; /* end sector */
749 uint8_t end_cyl; /* end cylinder */
750 uint32_t start_sect; /* starting sector counting from 0 */
751 uint32_t nr_sects; /* nr of sectors in partition */
752 } __attribute__((packed));
754 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
755 static int guess_disk_lchs(BlockDriverState *bs,
756 int *pcylinders, int *pheads, int *psectors)
758 uint8_t buf[512];
759 int ret, i, heads, sectors, cylinders;
760 struct partition *p;
761 uint32_t nr_sects;
762 int64_t nb_sectors;
764 bdrv_get_geometry(bs, &nb_sectors);
766 ret = bdrv_read(bs, 0, buf, 1);
767 if (ret < 0)
768 return -1;
769 /* test msdos magic */
770 if (buf[510] != 0x55 || buf[511] != 0xaa)
771 return -1;
772 for(i = 0; i < 4; i++) {
773 p = ((struct partition *)(buf + 0x1be)) + i;
774 nr_sects = le32_to_cpu(p->nr_sects);
775 if (nr_sects && p->end_head) {
776 /* We make the assumption that the partition terminates on
777 a cylinder boundary */
778 heads = p->end_head + 1;
779 sectors = p->end_sector & 63;
780 if (sectors == 0)
781 continue;
782 cylinders = nb_sectors / (heads * sectors);
783 if (cylinders < 1 || cylinders > 16383)
784 continue;
785 *pheads = heads;
786 *psectors = sectors;
787 *pcylinders = cylinders;
788 #if 0
789 printf("guessed geometry: LCHS=%d %d %d\n",
790 cylinders, heads, sectors);
791 #endif
792 return 0;
795 return -1;
798 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
800 int translation, lba_detected = 0;
801 int cylinders, heads, secs;
802 int64_t nb_sectors;
804 /* if a geometry hint is available, use it */
805 bdrv_get_geometry(bs, &nb_sectors);
806 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
807 translation = bdrv_get_translation_hint(bs);
808 if (cylinders != 0) {
809 *pcyls = cylinders;
810 *pheads = heads;
811 *psecs = secs;
812 } else {
813 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
814 if (heads > 16) {
815 /* if heads > 16, it means that a BIOS LBA
816 translation was active, so the default
817 hardware geometry is OK */
818 lba_detected = 1;
819 goto default_geometry;
820 } else {
821 *pcyls = cylinders;
822 *pheads = heads;
823 *psecs = secs;
824 /* disable any translation to be in sync with
825 the logical geometry */
826 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
827 bdrv_set_translation_hint(bs,
828 BIOS_ATA_TRANSLATION_NONE);
831 } else {
832 default_geometry:
833 /* if no geometry, use a standard physical disk geometry */
834 cylinders = nb_sectors / (16 * 63);
836 if (cylinders > 16383)
837 cylinders = 16383;
838 else if (cylinders < 2)
839 cylinders = 2;
840 *pcyls = cylinders;
841 *pheads = 16;
842 *psecs = 63;
843 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
844 if ((*pcyls * *pheads) <= 131072) {
845 bdrv_set_translation_hint(bs,
846 BIOS_ATA_TRANSLATION_LARGE);
847 } else {
848 bdrv_set_translation_hint(bs,
849 BIOS_ATA_TRANSLATION_LBA);
853 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
857 void bdrv_set_geometry_hint(BlockDriverState *bs,
858 int cyls, int heads, int secs)
860 bs->cyls = cyls;
861 bs->heads = heads;
862 bs->secs = secs;
865 void bdrv_set_type_hint(BlockDriverState *bs, int type)
867 bs->type = type;
868 bs->removable = ((type == BDRV_TYPE_CDROM ||
869 type == BDRV_TYPE_FLOPPY));
872 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
874 bs->translation = translation;
877 void bdrv_get_geometry_hint(BlockDriverState *bs,
878 int *pcyls, int *pheads, int *psecs)
880 *pcyls = bs->cyls;
881 *pheads = bs->heads;
882 *psecs = bs->secs;
885 int bdrv_get_type_hint(BlockDriverState *bs)
887 return bs->type;
890 int bdrv_get_translation_hint(BlockDriverState *bs)
892 return bs->translation;
895 int bdrv_is_removable(BlockDriverState *bs)
897 return bs->removable;
900 int bdrv_is_read_only(BlockDriverState *bs)
902 return bs->read_only;
905 /* XXX: no longer used */
906 void bdrv_set_change_cb(BlockDriverState *bs,
907 void (*change_cb)(void *opaque), void *opaque)
909 bs->change_cb = change_cb;
910 bs->change_opaque = opaque;
913 int bdrv_is_encrypted(BlockDriverState *bs)
915 if (bs->backing_hd && bs->backing_hd->encrypted)
916 return 1;
917 return bs->encrypted;
920 int bdrv_set_key(BlockDriverState *bs, const char *key)
922 int ret;
923 if (bs->backing_hd && bs->backing_hd->encrypted) {
924 ret = bdrv_set_key(bs->backing_hd, key);
925 if (ret < 0)
926 return ret;
927 if (!bs->encrypted)
928 return 0;
930 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
931 return -1;
932 return bs->drv->bdrv_set_key(bs, key);
935 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
937 if (!bs->drv) {
938 buf[0] = '\0';
939 } else {
940 pstrcpy(buf, buf_size, bs->drv->format_name);
944 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
945 void *opaque)
947 BlockDriver *drv;
949 for (drv = first_drv; drv != NULL; drv = drv->next) {
950 it(opaque, drv->format_name);
954 BlockDriverState *bdrv_find(const char *name)
956 BlockDriverState *bs;
958 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
959 if (!strcmp(name, bs->device_name))
960 return bs;
962 return NULL;
965 void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
967 BlockDriverState *bs;
969 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
970 it(opaque, bs->device_name);
974 const char *bdrv_get_device_name(BlockDriverState *bs)
976 return bs->device_name;
979 void bdrv_flush(BlockDriverState *bs)
981 if (bs->drv->bdrv_flush)
982 bs->drv->bdrv_flush(bs);
983 if (bs->backing_hd)
984 bdrv_flush(bs->backing_hd);
987 void bdrv_iterate_writeable(void (*it)(BlockDriverState *bs))
989 BlockDriverState *bs;
991 for (bs = bdrv_first; bs != NULL; bs = bs->next)
992 if (bs->drv && !bdrv_is_read_only(bs) &&
993 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
994 it(bs);
997 void bdrv_flush_all(void)
999 bdrv_iterate_writeable(bdrv_flush);
1002 #ifndef QEMU_IMG
1003 void bdrv_info(void)
1005 BlockDriverState *bs;
1007 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1008 term_printf("%s:", bs->device_name);
1009 term_printf(" type=");
1010 switch(bs->type) {
1011 case BDRV_TYPE_HD:
1012 term_printf("hd");
1013 break;
1014 case BDRV_TYPE_CDROM:
1015 term_printf("cdrom");
1016 break;
1017 case BDRV_TYPE_FLOPPY:
1018 term_printf("floppy");
1019 break;
1021 term_printf(" removable=%d", bs->removable);
1022 if (bs->removable) {
1023 term_printf(" locked=%d", bs->locked);
1025 if (bs->drv) {
1026 term_printf(" file=");
1027 term_print_filename(bs->filename);
1028 if (bs->backing_file[0] != '\0') {
1029 term_printf(" backing_file=");
1030 term_print_filename(bs->backing_file);
1032 term_printf(" ro=%d", bs->read_only);
1033 term_printf(" drv=%s", bs->drv->format_name);
1034 if (bs->encrypted)
1035 term_printf(" encrypted");
1036 } else {
1037 term_printf(" [not inserted]");
1039 term_printf("\n");
1043 /* The "info blockstats" command. */
1044 void bdrv_info_stats (void)
1046 BlockDriverState *bs;
1048 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1049 term_printf ("%s:"
1050 " rd_bytes=%" PRIu64
1051 " wr_bytes=%" PRIu64
1052 " rd_operations=%" PRIu64
1053 " wr_operations=%" PRIu64
1054 "\n",
1055 bs->device_name,
1056 bs->rd_bytes, bs->wr_bytes,
1057 bs->rd_ops, bs->wr_ops);
1060 #endif
1062 void bdrv_get_backing_filename(BlockDriverState *bs,
1063 char *filename, int filename_size)
1065 if (!bs->backing_hd) {
1066 pstrcpy(filename, filename_size, "");
1067 } else {
1068 pstrcpy(filename, filename_size, bs->backing_file);
1072 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1073 const uint8_t *buf, int nb_sectors)
1075 BlockDriver *drv = bs->drv;
1076 if (!drv)
1077 return -ENOMEDIUM;
1078 if (!drv->bdrv_write_compressed)
1079 return -ENOTSUP;
1080 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1083 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1085 BlockDriver *drv = bs->drv;
1086 if (!drv)
1087 return -ENOMEDIUM;
1088 if (!drv->bdrv_get_info)
1089 return -ENOTSUP;
1090 memset(bdi, 0, sizeof(*bdi));
1091 return drv->bdrv_get_info(bs, bdi);
1094 /**************************************************************/
1095 /* handling of snapshots */
1097 int bdrv_snapshot_create(BlockDriverState *bs,
1098 QEMUSnapshotInfo *sn_info)
1100 BlockDriver *drv = bs->drv;
1101 if (!drv)
1102 return -ENOMEDIUM;
1103 if (!drv->bdrv_snapshot_create)
1104 return -ENOTSUP;
1105 return drv->bdrv_snapshot_create(bs, sn_info);
1108 int bdrv_snapshot_goto(BlockDriverState *bs,
1109 const char *snapshot_id)
1111 BlockDriver *drv = bs->drv;
1112 if (!drv)
1113 return -ENOMEDIUM;
1114 if (!drv->bdrv_snapshot_goto)
1115 return -ENOTSUP;
1116 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1119 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1121 BlockDriver *drv = bs->drv;
1122 if (!drv)
1123 return -ENOMEDIUM;
1124 if (!drv->bdrv_snapshot_delete)
1125 return -ENOTSUP;
1126 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1129 int bdrv_snapshot_list(BlockDriverState *bs,
1130 QEMUSnapshotInfo **psn_info)
1132 BlockDriver *drv = bs->drv;
1133 if (!drv)
1134 return -ENOMEDIUM;
1135 if (!drv->bdrv_snapshot_list)
1136 return -ENOTSUP;
1137 return drv->bdrv_snapshot_list(bs, psn_info);
1140 #define NB_SUFFIXES 4
1142 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1144 static const char suffixes[NB_SUFFIXES] = "KMGT";
1145 int64_t base;
1146 int i;
1148 if (size <= 999) {
1149 snprintf(buf, buf_size, "%" PRId64, size);
1150 } else {
1151 base = 1024;
1152 for(i = 0; i < NB_SUFFIXES; i++) {
1153 if (size < (10 * base)) {
1154 snprintf(buf, buf_size, "%0.1f%c",
1155 (double)size / base,
1156 suffixes[i]);
1157 break;
1158 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1159 snprintf(buf, buf_size, "%" PRId64 "%c",
1160 ((size + (base >> 1)) / base),
1161 suffixes[i]);
1162 break;
1164 base = base * 1024;
1167 return buf;
1170 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1172 char buf1[128], date_buf[128], clock_buf[128];
1173 #ifdef _WIN32
1174 struct tm *ptm;
1175 #else
1176 struct tm tm;
1177 #endif
1178 time_t ti;
1179 int64_t secs;
1181 if (!sn) {
1182 snprintf(buf, buf_size,
1183 "%-10s%-20s%7s%20s%15s",
1184 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1185 } else {
1186 ti = sn->date_sec;
1187 #ifdef _WIN32
1188 ptm = localtime(&ti);
1189 strftime(date_buf, sizeof(date_buf),
1190 "%Y-%m-%d %H:%M:%S", ptm);
1191 #else
1192 localtime_r(&ti, &tm);
1193 strftime(date_buf, sizeof(date_buf),
1194 "%Y-%m-%d %H:%M:%S", &tm);
1195 #endif
1196 secs = sn->vm_clock_nsec / 1000000000;
1197 snprintf(clock_buf, sizeof(clock_buf),
1198 "%02d:%02d:%02d.%03d",
1199 (int)(secs / 3600),
1200 (int)((secs / 60) % 60),
1201 (int)(secs % 60),
1202 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1203 snprintf(buf, buf_size,
1204 "%-10s%-20s%7s%20s%15s",
1205 sn->id_str, sn->name,
1206 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1207 date_buf,
1208 clock_buf);
1210 return buf;
1214 /**************************************************************/
1215 /* async I/Os */
1217 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1218 uint8_t *buf, int nb_sectors,
1219 BlockDriverCompletionFunc *cb, void *opaque)
1221 BlockDriver *drv = bs->drv;
1222 BlockDriverAIOCB *ret;
1224 if (!drv)
1225 return NULL;
1227 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1228 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1229 memcpy(buf, bs->boot_sector_data, 512);
1230 sector_num++;
1231 nb_sectors--;
1232 buf += 512;
1235 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1237 if (ret) {
1238 /* Update stats even though technically transfer has not happened. */
1239 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1240 bs->rd_ops ++;
1243 return ret;
1246 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1247 const uint8_t *buf, int nb_sectors,
1248 BlockDriverCompletionFunc *cb, void *opaque)
1250 BlockDriver *drv = bs->drv;
1251 BlockDriverAIOCB *ret;
1253 if (!drv)
1254 return NULL;
1255 if (bs->read_only)
1256 return NULL;
1257 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1258 memcpy(bs->boot_sector_data, buf, 512);
1261 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1263 if (ret) {
1264 /* Update stats even though technically transfer has not happened. */
1265 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1266 bs->wr_ops ++;
1269 return ret;
1272 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1274 BlockDriver *drv = acb->bs->drv;
1276 drv->bdrv_aio_cancel(acb);
1280 /**************************************************************/
1281 /* async block device emulation */
1283 #ifdef QEMU_IMG
1284 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1285 int64_t sector_num, uint8_t *buf, int nb_sectors,
1286 BlockDriverCompletionFunc *cb, void *opaque)
1288 int ret;
1289 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1290 cb(opaque, ret);
1291 return NULL;
1294 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1295 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1296 BlockDriverCompletionFunc *cb, void *opaque)
1298 int ret;
1299 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1300 cb(opaque, ret);
1301 return NULL;
1304 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
1307 #else
1308 static void bdrv_aio_bh_cb(void *opaque)
1310 BlockDriverAIOCBSync *acb = opaque;
1311 acb->common.cb(acb->common.opaque, acb->ret);
1312 qemu_aio_release(acb);
1315 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1316 int64_t sector_num, uint8_t *buf, int nb_sectors,
1317 BlockDriverCompletionFunc *cb, void *opaque)
1319 BlockDriverAIOCBSync *acb;
1320 int ret;
1322 acb = qemu_aio_get(bs, cb, opaque);
1323 if (!acb->bh)
1324 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1325 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1326 acb->ret = ret;
1327 qemu_bh_schedule(acb->bh);
1328 return &acb->common;
1331 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1332 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1333 BlockDriverCompletionFunc *cb, void *opaque)
1335 BlockDriverAIOCBSync *acb;
1336 int ret;
1338 acb = qemu_aio_get(bs, cb, opaque);
1339 if (!acb->bh)
1340 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1341 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1342 acb->ret = ret;
1343 qemu_bh_schedule(acb->bh);
1344 return &acb->common;
1347 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1349 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1350 qemu_bh_cancel(acb->bh);
1351 qemu_aio_release(acb);
1353 #endif /* !QEMU_IMG */
1355 /**************************************************************/
1356 /* sync block device emulation */
1358 static void bdrv_rw_em_cb(void *opaque, int ret)
1360 *(int *)opaque = ret;
1363 #define NOT_DONE 0x7fffffff
1365 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1366 uint8_t *buf, int nb_sectors)
1368 int async_ret;
1369 BlockDriverAIOCB *acb;
1371 async_ret = NOT_DONE;
1372 qemu_aio_wait_start();
1373 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1374 bdrv_rw_em_cb, &async_ret);
1375 if (acb == NULL) {
1376 qemu_aio_wait_end();
1377 return -1;
1379 while (async_ret == NOT_DONE) {
1380 qemu_aio_wait();
1382 qemu_aio_wait_end();
1383 return async_ret;
1386 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1387 const uint8_t *buf, int nb_sectors)
1389 int async_ret;
1390 BlockDriverAIOCB *acb;
1392 async_ret = NOT_DONE;
1393 qemu_aio_wait_start();
1394 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1395 bdrv_rw_em_cb, &async_ret);
1396 if (acb == NULL) {
1397 qemu_aio_wait_end();
1398 return -1;
1400 while (async_ret == NOT_DONE) {
1401 qemu_aio_wait();
1403 qemu_aio_wait_end();
1404 return async_ret;
1407 void bdrv_init(void)
1409 bdrv_register(&bdrv_raw);
1410 bdrv_register(&bdrv_host_device);
1411 #ifndef _WIN32
1412 bdrv_register(&bdrv_cow);
1413 #endif
1414 bdrv_register(&bdrv_qcow);
1415 bdrv_register(&bdrv_vmdk);
1416 bdrv_register(&bdrv_cloop);
1417 bdrv_register(&bdrv_dmg);
1418 bdrv_register(&bdrv_bochs);
1419 bdrv_register(&bdrv_vpc);
1420 bdrv_register(&bdrv_vvfat);
1421 bdrv_register(&bdrv_qcow2);
1422 bdrv_register(&bdrv_parallels);
1425 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1426 void *opaque)
1428 BlockDriver *drv;
1429 BlockDriverAIOCB *acb;
1431 drv = bs->drv;
1432 if (drv->free_aiocb) {
1433 acb = drv->free_aiocb;
1434 drv->free_aiocb = acb->next;
1435 } else {
1436 acb = qemu_mallocz(drv->aiocb_size);
1437 if (!acb)
1438 return NULL;
1440 acb->bs = bs;
1441 acb->cb = cb;
1442 acb->opaque = opaque;
1443 return acb;
1446 void qemu_aio_release(void *p)
1448 BlockDriverAIOCB *acb = p;
1449 BlockDriver *drv = acb->bs->drv;
1450 acb->next = drv->free_aiocb;
1451 drv->free_aiocb = acb;
1454 /**************************************************************/
1455 /* removable device support */
1458 * Return TRUE if the media is present
1460 int bdrv_is_inserted(BlockDriverState *bs)
1462 BlockDriver *drv = bs->drv;
1463 int ret;
1464 if (!drv)
1465 return 0;
1466 if (!drv->bdrv_is_inserted)
1467 return 1;
1468 ret = drv->bdrv_is_inserted(bs);
1469 return ret;
1473 * Return TRUE if the media changed since the last call to this
1474 * function. It is currently only used for floppy disks
1476 int bdrv_media_changed(BlockDriverState *bs)
1478 BlockDriver *drv = bs->drv;
1479 int ret;
1481 if (!drv || !drv->bdrv_media_changed)
1482 ret = -ENOTSUP;
1483 else
1484 ret = drv->bdrv_media_changed(bs);
1485 if (ret == -ENOTSUP)
1486 ret = bs->media_changed;
1487 bs->media_changed = 0;
1488 return ret;
1492 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1494 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1496 BlockDriver *drv = bs->drv;
1497 int ret;
1499 if (!drv || !drv->bdrv_eject) {
1500 ret = -ENOTSUP;
1501 } else {
1502 ret = drv->bdrv_eject(bs, eject_flag);
1504 if (ret == -ENOTSUP) {
1505 if (eject_flag)
1506 bdrv_close(bs);
1510 int bdrv_is_locked(BlockDriverState *bs)
1512 return bs->locked;
1516 * Lock or unlock the media (if it is locked, the user won't be able
1517 * to eject it manually).
1519 void bdrv_set_locked(BlockDriverState *bs, int locked)
1521 BlockDriver *drv = bs->drv;
1523 bs->locked = locked;
1524 if (drv && drv->bdrv_set_locked) {
1525 drv->bdrv_set_locked(bs, locked);