Device-assignment: free device if hotplug fails
[qemu-kvm/fedora.git] / block.c
blobd9c0af5e50239c472e8254e32c3eb0b3a68fc23e
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 (drv->bdrv_pread) {
532 int ret, len;
533 len = nb_sectors * 512;
534 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
535 if (ret < 0)
536 return ret;
537 else if (ret != len)
538 return -EINVAL;
539 else {
540 bs->rd_bytes += (unsigned) len;
541 bs->rd_ops ++;
542 return 0;
544 } else {
545 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
549 /* Return < 0 if error. Important errors are:
550 -EIO generic I/O error (may happen for all errors)
551 -ENOMEDIUM No media inserted.
552 -EINVAL Invalid sector number or nb_sectors
553 -EACCES Trying to write a read-only device
555 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
556 const uint8_t *buf, int nb_sectors)
558 BlockDriver *drv = bs->drv;
559 if (!bs->drv)
560 return -ENOMEDIUM;
561 if (bs->read_only)
562 return -EACCES;
563 if (drv->bdrv_pwrite) {
564 int ret, len;
565 len = nb_sectors * 512;
566 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
567 if (ret < 0)
568 return ret;
569 else if (ret != len)
570 return -EIO;
571 else {
572 bs->wr_bytes += (unsigned) len;
573 bs->wr_ops ++;
574 return 0;
576 } else {
577 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
581 static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
582 uint8_t *buf, int count1)
584 uint8_t tmp_buf[SECTOR_SIZE];
585 int len, nb_sectors, count;
586 int64_t sector_num;
588 count = count1;
589 /* first read to align to sector start */
590 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
591 if (len > count)
592 len = count;
593 sector_num = offset >> SECTOR_BITS;
594 if (len > 0) {
595 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
596 return -EIO;
597 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
598 count -= len;
599 if (count == 0)
600 return count1;
601 sector_num++;
602 buf += len;
605 /* read the sectors "in place" */
606 nb_sectors = count >> SECTOR_BITS;
607 if (nb_sectors > 0) {
608 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
609 return -EIO;
610 sector_num += nb_sectors;
611 len = nb_sectors << SECTOR_BITS;
612 buf += len;
613 count -= len;
616 /* add data from the last sector */
617 if (count > 0) {
618 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
619 return -EIO;
620 memcpy(buf, tmp_buf, count);
622 return count1;
625 static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
626 const uint8_t *buf, int count1)
628 uint8_t tmp_buf[SECTOR_SIZE];
629 int len, nb_sectors, count;
630 int64_t sector_num;
632 count = count1;
633 /* first write to align to sector start */
634 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
635 if (len > count)
636 len = count;
637 sector_num = offset >> SECTOR_BITS;
638 if (len > 0) {
639 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
640 return -EIO;
641 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
642 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
643 return -EIO;
644 count -= len;
645 if (count == 0)
646 return count1;
647 sector_num++;
648 buf += len;
651 /* write the sectors "in place" */
652 nb_sectors = count >> SECTOR_BITS;
653 if (nb_sectors > 0) {
654 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
655 return -EIO;
656 sector_num += nb_sectors;
657 len = nb_sectors << SECTOR_BITS;
658 buf += len;
659 count -= len;
662 /* add data from the last sector */
663 if (count > 0) {
664 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
665 return -EIO;
666 memcpy(tmp_buf, buf, count);
667 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
668 return -EIO;
670 return count1;
674 * Read with byte offsets (needed only for file protocols)
676 int bdrv_pread(BlockDriverState *bs, int64_t offset,
677 void *buf1, int count1)
679 BlockDriver *drv = bs->drv;
681 if (!drv)
682 return -ENOMEDIUM;
683 if (!drv->bdrv_pread)
684 return bdrv_pread_em(bs, offset, buf1, count1);
685 return drv->bdrv_pread(bs, offset, buf1, count1);
689 * Write with byte offsets (needed only for file protocols)
691 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
692 const void *buf1, int count1)
694 BlockDriver *drv = bs->drv;
696 if (!drv)
697 return -ENOMEDIUM;
698 if (!drv->bdrv_pwrite)
699 return bdrv_pwrite_em(bs, offset, buf1, count1);
700 return drv->bdrv_pwrite(bs, offset, buf1, count1);
704 * Truncate file to 'offset' bytes (needed only for file protocols)
706 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
708 BlockDriver *drv = bs->drv;
709 if (!drv)
710 return -ENOMEDIUM;
711 if (!drv->bdrv_truncate)
712 return -ENOTSUP;
713 return drv->bdrv_truncate(bs, offset);
717 * Length of a file in bytes. Return < 0 if error or unknown.
719 int64_t bdrv_getlength(BlockDriverState *bs)
721 BlockDriver *drv = bs->drv;
722 if (!drv)
723 return -ENOMEDIUM;
724 if (!drv->bdrv_getlength) {
725 /* legacy mode */
726 return bs->total_sectors * SECTOR_SIZE;
728 return drv->bdrv_getlength(bs);
731 /* return 0 as number of sectors if no device present or error */
732 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
734 int64_t length;
735 length = bdrv_getlength(bs);
736 if (length < 0)
737 length = 0;
738 else
739 length = length >> SECTOR_BITS;
740 *nb_sectors_ptr = length;
743 struct partition {
744 uint8_t boot_ind; /* 0x80 - active */
745 uint8_t head; /* starting head */
746 uint8_t sector; /* starting sector */
747 uint8_t cyl; /* starting cylinder */
748 uint8_t sys_ind; /* What partition type */
749 uint8_t end_head; /* end head */
750 uint8_t end_sector; /* end sector */
751 uint8_t end_cyl; /* end cylinder */
752 uint32_t start_sect; /* starting sector counting from 0 */
753 uint32_t nr_sects; /* nr of sectors in partition */
754 } __attribute__((packed));
756 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
757 static int guess_disk_lchs(BlockDriverState *bs,
758 int *pcylinders, int *pheads, int *psectors)
760 uint8_t *buf;
761 int ret, i, heads, sectors, cylinders;
762 struct partition *p;
763 uint32_t nr_sects;
764 int64_t nb_sectors;
766 buf = qemu_memalign(512, 512);
767 if (buf == NULL)
768 return -1;
770 bdrv_get_geometry(bs, &nb_sectors);
772 ret = bdrv_read(bs, 0, buf, 1);
773 if (ret < 0)
774 return -1;
775 /* test msdos magic */
776 if (buf[510] != 0x55 || buf[511] != 0xaa) {
777 qemu_free(buf);
778 return -1;
780 for(i = 0; i < 4; i++) {
781 p = ((struct partition *)(buf + 0x1be)) + i;
782 nr_sects = le32_to_cpu(p->nr_sects);
783 if (nr_sects && p->end_head) {
784 /* We make the assumption that the partition terminates on
785 a cylinder boundary */
786 heads = p->end_head + 1;
787 sectors = p->end_sector & 63;
788 if (sectors == 0)
789 continue;
790 cylinders = nb_sectors / (heads * sectors);
791 if (cylinders < 1 || cylinders > 16383)
792 continue;
793 *pheads = heads;
794 *psectors = sectors;
795 *pcylinders = cylinders;
796 #if 0
797 printf("guessed geometry: LCHS=%d %d %d\n",
798 cylinders, heads, sectors);
799 #endif
800 qemu_free(buf);
801 return 0;
804 qemu_free(buf);
805 return -1;
808 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
810 int translation, lba_detected = 0;
811 int cylinders, heads, secs;
812 int64_t nb_sectors;
814 /* if a geometry hint is available, use it */
815 bdrv_get_geometry(bs, &nb_sectors);
816 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
817 translation = bdrv_get_translation_hint(bs);
818 if (cylinders != 0) {
819 *pcyls = cylinders;
820 *pheads = heads;
821 *psecs = secs;
822 } else {
823 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
824 if (heads > 16) {
825 /* if heads > 16, it means that a BIOS LBA
826 translation was active, so the default
827 hardware geometry is OK */
828 lba_detected = 1;
829 goto default_geometry;
830 } else {
831 *pcyls = cylinders;
832 *pheads = heads;
833 *psecs = secs;
834 /* disable any translation to be in sync with
835 the logical geometry */
836 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
837 bdrv_set_translation_hint(bs,
838 BIOS_ATA_TRANSLATION_NONE);
841 } else {
842 default_geometry:
843 /* if no geometry, use a standard physical disk geometry */
844 cylinders = nb_sectors / (16 * 63);
846 if (cylinders > 16383)
847 cylinders = 16383;
848 else if (cylinders < 2)
849 cylinders = 2;
850 *pcyls = cylinders;
851 *pheads = 16;
852 *psecs = 63;
853 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
854 if ((*pcyls * *pheads) <= 131072) {
855 bdrv_set_translation_hint(bs,
856 BIOS_ATA_TRANSLATION_LARGE);
857 } else {
858 bdrv_set_translation_hint(bs,
859 BIOS_ATA_TRANSLATION_LBA);
863 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
867 void bdrv_set_geometry_hint(BlockDriverState *bs,
868 int cyls, int heads, int secs)
870 bs->cyls = cyls;
871 bs->heads = heads;
872 bs->secs = secs;
875 void bdrv_set_type_hint(BlockDriverState *bs, int type)
877 bs->type = type;
878 bs->removable = ((type == BDRV_TYPE_CDROM ||
879 type == BDRV_TYPE_FLOPPY));
882 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
884 bs->translation = translation;
887 void bdrv_get_geometry_hint(BlockDriverState *bs,
888 int *pcyls, int *pheads, int *psecs)
890 *pcyls = bs->cyls;
891 *pheads = bs->heads;
892 *psecs = bs->secs;
895 int bdrv_get_type_hint(BlockDriverState *bs)
897 return bs->type;
900 int bdrv_get_translation_hint(BlockDriverState *bs)
902 return bs->translation;
905 int bdrv_is_removable(BlockDriverState *bs)
907 return bs->removable;
910 int bdrv_is_read_only(BlockDriverState *bs)
912 return bs->read_only;
915 int bdrv_is_sg(BlockDriverState *bs)
917 return bs->sg;
920 /* XXX: no longer used */
921 void bdrv_set_change_cb(BlockDriverState *bs,
922 void (*change_cb)(void *opaque), void *opaque)
924 bs->change_cb = change_cb;
925 bs->change_opaque = opaque;
928 int bdrv_is_encrypted(BlockDriverState *bs)
930 if (bs->backing_hd && bs->backing_hd->encrypted)
931 return 1;
932 return bs->encrypted;
935 int bdrv_set_key(BlockDriverState *bs, const char *key)
937 int ret;
938 if (bs->backing_hd && bs->backing_hd->encrypted) {
939 ret = bdrv_set_key(bs->backing_hd, key);
940 if (ret < 0)
941 return ret;
942 if (!bs->encrypted)
943 return 0;
945 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
946 return -1;
947 return bs->drv->bdrv_set_key(bs, key);
950 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
952 if (!bs->drv) {
953 buf[0] = '\0';
954 } else {
955 pstrcpy(buf, buf_size, bs->drv->format_name);
959 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
960 void *opaque)
962 BlockDriver *drv;
964 for (drv = first_drv; drv != NULL; drv = drv->next) {
965 it(opaque, drv->format_name);
969 BlockDriverState *bdrv_find(const char *name)
971 BlockDriverState *bs;
973 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
974 if (!strcmp(name, bs->device_name))
975 return bs;
977 return NULL;
980 void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
982 BlockDriverState *bs;
984 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
985 it(opaque, bs->device_name);
989 const char *bdrv_get_device_name(BlockDriverState *bs)
991 return bs->device_name;
994 void bdrv_flush(BlockDriverState *bs)
996 if (bs->drv->bdrv_flush)
997 bs->drv->bdrv_flush(bs);
998 if (bs->backing_hd)
999 bdrv_flush(bs->backing_hd);
1002 void bdrv_iterate_writeable(void (*it)(BlockDriverState *bs))
1004 BlockDriverState *bs;
1006 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1007 if (bs->drv && !bdrv_is_read_only(bs) &&
1008 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1009 it(bs);
1012 void bdrv_flush_all(void)
1014 BlockDriverState *bs;
1016 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1017 if (bs->drv && !bdrv_is_read_only(bs) &&
1018 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1019 bdrv_flush(bs);
1023 * Returns true iff the specified sector is present in the disk image. Drivers
1024 * not implementing the functionality are assumed to not support backing files,
1025 * hence all their sectors are reported as allocated.
1027 * 'pnum' is set to the number of sectors (including and immediately following
1028 * the specified sector) that are known to be in the same
1029 * allocated/unallocated state.
1031 * 'nb_sectors' is the max value 'pnum' should be set to.
1033 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1034 int *pnum)
1036 int64_t n;
1037 if (!bs->drv->bdrv_is_allocated) {
1038 if (sector_num >= bs->total_sectors) {
1039 *pnum = 0;
1040 return 0;
1042 n = bs->total_sectors - sector_num;
1043 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1044 return 1;
1046 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1049 void bdrv_info(void)
1051 BlockDriverState *bs;
1053 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1054 term_printf("%s:", bs->device_name);
1055 term_printf(" type=");
1056 switch(bs->type) {
1057 case BDRV_TYPE_HD:
1058 term_printf("hd");
1059 break;
1060 case BDRV_TYPE_CDROM:
1061 term_printf("cdrom");
1062 break;
1063 case BDRV_TYPE_FLOPPY:
1064 term_printf("floppy");
1065 break;
1067 term_printf(" removable=%d", bs->removable);
1068 if (bs->removable) {
1069 term_printf(" locked=%d", bs->locked);
1071 if (bs->drv) {
1072 term_printf(" file=");
1073 term_print_filename(bs->filename);
1074 if (bs->backing_file[0] != '\0') {
1075 term_printf(" backing_file=");
1076 term_print_filename(bs->backing_file);
1078 term_printf(" ro=%d", bs->read_only);
1079 term_printf(" drv=%s", bs->drv->format_name);
1080 if (bs->encrypted)
1081 term_printf(" encrypted");
1082 } else {
1083 term_printf(" [not inserted]");
1085 term_printf("\n");
1089 /* The "info blockstats" command. */
1090 void bdrv_info_stats (void)
1092 BlockDriverState *bs;
1094 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1095 term_printf ("%s:"
1096 " rd_bytes=%" PRIu64
1097 " wr_bytes=%" PRIu64
1098 " rd_operations=%" PRIu64
1099 " wr_operations=%" PRIu64
1100 "\n",
1101 bs->device_name,
1102 bs->rd_bytes, bs->wr_bytes,
1103 bs->rd_ops, bs->wr_ops);
1107 void bdrv_get_backing_filename(BlockDriverState *bs,
1108 char *filename, int filename_size)
1110 if (!bs->backing_hd) {
1111 pstrcpy(filename, filename_size, "");
1112 } else {
1113 pstrcpy(filename, filename_size, bs->backing_file);
1117 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1118 const uint8_t *buf, int nb_sectors)
1120 BlockDriver *drv = bs->drv;
1121 if (!drv)
1122 return -ENOMEDIUM;
1123 if (!drv->bdrv_write_compressed)
1124 return -ENOTSUP;
1125 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1128 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1130 BlockDriver *drv = bs->drv;
1131 if (!drv)
1132 return -ENOMEDIUM;
1133 if (!drv->bdrv_get_info)
1134 return -ENOTSUP;
1135 memset(bdi, 0, sizeof(*bdi));
1136 return drv->bdrv_get_info(bs, bdi);
1139 /**************************************************************/
1140 /* handling of snapshots */
1142 int bdrv_snapshot_create(BlockDriverState *bs,
1143 QEMUSnapshotInfo *sn_info)
1145 BlockDriver *drv = bs->drv;
1146 if (!drv)
1147 return -ENOMEDIUM;
1148 if (!drv->bdrv_snapshot_create)
1149 return -ENOTSUP;
1150 return drv->bdrv_snapshot_create(bs, sn_info);
1153 int bdrv_snapshot_goto(BlockDriverState *bs,
1154 const char *snapshot_id)
1156 BlockDriver *drv = bs->drv;
1157 if (!drv)
1158 return -ENOMEDIUM;
1159 if (!drv->bdrv_snapshot_goto)
1160 return -ENOTSUP;
1161 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1164 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1166 BlockDriver *drv = bs->drv;
1167 if (!drv)
1168 return -ENOMEDIUM;
1169 if (!drv->bdrv_snapshot_delete)
1170 return -ENOTSUP;
1171 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1174 int bdrv_snapshot_list(BlockDriverState *bs,
1175 QEMUSnapshotInfo **psn_info)
1177 BlockDriver *drv = bs->drv;
1178 if (!drv)
1179 return -ENOMEDIUM;
1180 if (!drv->bdrv_snapshot_list)
1181 return -ENOTSUP;
1182 return drv->bdrv_snapshot_list(bs, psn_info);
1185 #define NB_SUFFIXES 4
1187 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1189 static const char suffixes[NB_SUFFIXES] = "KMGT";
1190 int64_t base;
1191 int i;
1193 if (size <= 999) {
1194 snprintf(buf, buf_size, "%" PRId64, size);
1195 } else {
1196 base = 1024;
1197 for(i = 0; i < NB_SUFFIXES; i++) {
1198 if (size < (10 * base)) {
1199 snprintf(buf, buf_size, "%0.1f%c",
1200 (double)size / base,
1201 suffixes[i]);
1202 break;
1203 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1204 snprintf(buf, buf_size, "%" PRId64 "%c",
1205 ((size + (base >> 1)) / base),
1206 suffixes[i]);
1207 break;
1209 base = base * 1024;
1212 return buf;
1215 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1217 char buf1[128], date_buf[128], clock_buf[128];
1218 #ifdef _WIN32
1219 struct tm *ptm;
1220 #else
1221 struct tm tm;
1222 #endif
1223 time_t ti;
1224 int64_t secs;
1226 if (!sn) {
1227 snprintf(buf, buf_size,
1228 "%-10s%-20s%7s%20s%15s",
1229 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1230 } else {
1231 ti = sn->date_sec;
1232 #ifdef _WIN32
1233 ptm = localtime(&ti);
1234 strftime(date_buf, sizeof(date_buf),
1235 "%Y-%m-%d %H:%M:%S", ptm);
1236 #else
1237 localtime_r(&ti, &tm);
1238 strftime(date_buf, sizeof(date_buf),
1239 "%Y-%m-%d %H:%M:%S", &tm);
1240 #endif
1241 secs = sn->vm_clock_nsec / 1000000000;
1242 snprintf(clock_buf, sizeof(clock_buf),
1243 "%02d:%02d:%02d.%03d",
1244 (int)(secs / 3600),
1245 (int)((secs / 60) % 60),
1246 (int)(secs % 60),
1247 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1248 snprintf(buf, buf_size,
1249 "%-10s%-20s%7s%20s%15s",
1250 sn->id_str, sn->name,
1251 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1252 date_buf,
1253 clock_buf);
1255 return buf;
1259 /**************************************************************/
1260 /* async I/Os */
1262 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1263 uint8_t *buf, int nb_sectors,
1264 BlockDriverCompletionFunc *cb, void *opaque)
1266 BlockDriver *drv = bs->drv;
1267 BlockDriverAIOCB *ret;
1269 if (!drv)
1270 return NULL;
1272 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1274 if (ret) {
1275 /* Update stats even though technically transfer has not happened. */
1276 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1277 bs->rd_ops ++;
1280 return ret;
1283 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1284 const 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;
1292 if (bs->read_only)
1293 return NULL;
1295 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1297 if (ret) {
1298 /* Update stats even though technically transfer has not happened. */
1299 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1300 bs->wr_ops ++;
1303 return ret;
1306 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1308 BlockDriver *drv = acb->bs->drv;
1310 drv->bdrv_aio_cancel(acb);
1314 /**************************************************************/
1315 /* async block device emulation */
1317 static void bdrv_aio_bh_cb(void *opaque)
1319 BlockDriverAIOCBSync *acb = opaque;
1320 acb->common.cb(acb->common.opaque, acb->ret);
1321 qemu_aio_release(acb);
1324 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1325 int64_t sector_num, uint8_t *buf, int nb_sectors,
1326 BlockDriverCompletionFunc *cb, void *opaque)
1328 BlockDriverAIOCBSync *acb;
1329 int ret;
1331 acb = qemu_aio_get(bs, cb, opaque);
1332 if (!acb->bh)
1333 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1334 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1335 acb->ret = ret;
1336 qemu_bh_schedule(acb->bh);
1337 return &acb->common;
1340 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1341 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1342 BlockDriverCompletionFunc *cb, void *opaque)
1344 BlockDriverAIOCBSync *acb;
1345 int ret;
1347 acb = qemu_aio_get(bs, cb, opaque);
1348 if (!acb->bh)
1349 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1350 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1351 acb->ret = ret;
1352 qemu_bh_schedule(acb->bh);
1353 return &acb->common;
1356 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1358 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1359 qemu_bh_cancel(acb->bh);
1360 qemu_aio_release(acb);
1363 /**************************************************************/
1364 /* sync block device emulation */
1366 static void bdrv_rw_em_cb(void *opaque, int ret)
1368 *(int *)opaque = ret;
1371 #define NOT_DONE 0x7fffffff
1373 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1374 uint8_t *buf, int nb_sectors)
1376 int async_ret;
1377 BlockDriverAIOCB *acb;
1379 async_ret = NOT_DONE;
1380 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1381 bdrv_rw_em_cb, &async_ret);
1382 if (acb == NULL)
1383 return -1;
1385 while (async_ret == NOT_DONE) {
1386 qemu_aio_wait();
1389 return async_ret;
1392 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1393 const uint8_t *buf, int nb_sectors)
1395 int async_ret;
1396 BlockDriverAIOCB *acb;
1398 async_ret = NOT_DONE;
1399 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1400 bdrv_rw_em_cb, &async_ret);
1401 if (acb == NULL)
1402 return -1;
1403 while (async_ret == NOT_DONE) {
1404 qemu_aio_wait();
1406 return async_ret;
1409 void bdrv_init(void)
1411 bdrv_register(&bdrv_raw);
1412 bdrv_register(&bdrv_host_device);
1413 #ifndef _WIN32
1414 bdrv_register(&bdrv_cow);
1415 #endif
1416 bdrv_register(&bdrv_qcow);
1417 bdrv_register(&bdrv_vmdk);
1418 bdrv_register(&bdrv_cloop);
1419 bdrv_register(&bdrv_dmg);
1420 bdrv_register(&bdrv_bochs);
1421 bdrv_register(&bdrv_vpc);
1422 bdrv_register(&bdrv_vvfat);
1423 bdrv_register(&bdrv_qcow2);
1424 bdrv_register(&bdrv_parallels);
1425 bdrv_register(&bdrv_nbd);
1428 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1429 void *opaque)
1431 BlockDriver *drv;
1432 BlockDriverAIOCB *acb;
1434 drv = bs->drv;
1435 if (drv->free_aiocb) {
1436 acb = drv->free_aiocb;
1437 drv->free_aiocb = acb->next;
1438 } else {
1439 acb = qemu_mallocz(drv->aiocb_size);
1440 if (!acb)
1441 return NULL;
1443 acb->bs = bs;
1444 acb->cb = cb;
1445 acb->opaque = opaque;
1446 return acb;
1449 void qemu_aio_release(void *p)
1451 BlockDriverAIOCB *acb = p;
1452 BlockDriver *drv = acb->bs->drv;
1453 acb->next = drv->free_aiocb;
1454 drv->free_aiocb = acb;
1457 /**************************************************************/
1458 /* removable device support */
1461 * Return TRUE if the media is present
1463 int bdrv_is_inserted(BlockDriverState *bs)
1465 BlockDriver *drv = bs->drv;
1466 int ret;
1467 if (!drv)
1468 return 0;
1469 if (!drv->bdrv_is_inserted)
1470 return 1;
1471 ret = drv->bdrv_is_inserted(bs);
1472 return ret;
1476 * Return TRUE if the media changed since the last call to this
1477 * function. It is currently only used for floppy disks
1479 int bdrv_media_changed(BlockDriverState *bs)
1481 BlockDriver *drv = bs->drv;
1482 int ret;
1484 if (!drv || !drv->bdrv_media_changed)
1485 ret = -ENOTSUP;
1486 else
1487 ret = drv->bdrv_media_changed(bs);
1488 if (ret == -ENOTSUP)
1489 ret = bs->media_changed;
1490 bs->media_changed = 0;
1491 return ret;
1495 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1497 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1499 BlockDriver *drv = bs->drv;
1500 int ret;
1502 if (!drv || !drv->bdrv_eject) {
1503 ret = -ENOTSUP;
1504 } else {
1505 ret = drv->bdrv_eject(bs, eject_flag);
1507 if (ret == -ENOTSUP) {
1508 if (eject_flag)
1509 bdrv_close(bs);
1513 int bdrv_is_locked(BlockDriverState *bs)
1515 return bs->locked;
1519 * Lock or unlock the media (if it is locked, the user won't be able
1520 * to eject it manually).
1522 void bdrv_set_locked(BlockDriverState *bs, int locked)
1524 BlockDriver *drv = bs->drv;
1526 bs->locked = locked;
1527 if (drv && drv->bdrv_set_locked) {
1528 drv->bdrv_set_locked(bs, locked);
1532 /* needed for generic scsi interface */
1534 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1536 BlockDriver *drv = bs->drv;
1538 if (drv && drv->bdrv_ioctl)
1539 return drv->bdrv_ioctl(bs, req, buf);
1540 return -ENOTSUP;