Merge branch 'qemu-cvs'
[qemu-kvm/fedora.git] / block.c
blobf6a11802dd24d82f22caca54a5550cf71ef75488
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, count = 0;
570 len = nb_sectors * 512;
571 do {
572 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len - count);
573 if (ret < 0) {
574 printf("bdrv_write ret=%d\n", ret);
575 return ret;
577 count += ret;
578 buf += ret;
579 } while (count != len);
580 bs->wr_bytes += (unsigned) len;
581 bs->wr_ops ++;
582 return 0;
584 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
587 static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
588 uint8_t *buf, int count1)
590 uint8_t tmp_buf[SECTOR_SIZE];
591 int len, nb_sectors, count;
592 int64_t sector_num;
594 count = count1;
595 /* first read to align to sector start */
596 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
597 if (len > count)
598 len = count;
599 sector_num = offset >> SECTOR_BITS;
600 if (len > 0) {
601 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
602 return -EIO;
603 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
604 count -= len;
605 if (count == 0)
606 return count1;
607 sector_num++;
608 buf += len;
611 /* read the sectors "in place" */
612 nb_sectors = count >> SECTOR_BITS;
613 if (nb_sectors > 0) {
614 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
615 return -EIO;
616 sector_num += nb_sectors;
617 len = nb_sectors << SECTOR_BITS;
618 buf += len;
619 count -= len;
622 /* add data from the last sector */
623 if (count > 0) {
624 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
625 return -EIO;
626 memcpy(buf, tmp_buf, count);
628 return count1;
631 static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
632 const uint8_t *buf, int count1)
634 uint8_t tmp_buf[SECTOR_SIZE];
635 int len, nb_sectors, count;
636 int64_t sector_num;
638 count = count1;
639 /* first write to align to sector start */
640 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
641 if (len > count)
642 len = count;
643 sector_num = offset >> SECTOR_BITS;
644 if (len > 0) {
645 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
646 return -EIO;
647 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
648 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
649 return -EIO;
650 count -= len;
651 if (count == 0)
652 return count1;
653 sector_num++;
654 buf += len;
657 /* write the sectors "in place" */
658 nb_sectors = count >> SECTOR_BITS;
659 if (nb_sectors > 0) {
660 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
661 return -EIO;
662 sector_num += nb_sectors;
663 len = nb_sectors << SECTOR_BITS;
664 buf += len;
665 count -= len;
668 /* add data from the last sector */
669 if (count > 0) {
670 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
671 return -EIO;
672 memcpy(tmp_buf, buf, count);
673 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
674 return -EIO;
676 return count1;
680 * Read with byte offsets (needed only for file protocols)
682 int bdrv_pread(BlockDriverState *bs, int64_t offset,
683 void *buf1, int count1)
685 BlockDriver *drv = bs->drv;
687 if (!drv)
688 return -ENOMEDIUM;
689 if (!drv->bdrv_pread)
690 return bdrv_pread_em(bs, offset, buf1, count1);
691 return drv->bdrv_pread(bs, offset, buf1, count1);
695 * Write with byte offsets (needed only for file protocols)
697 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
698 const void *buf1, int count1)
700 BlockDriver *drv = bs->drv;
702 if (!drv)
703 return -ENOMEDIUM;
704 if (!drv->bdrv_pwrite)
705 return bdrv_pwrite_em(bs, offset, buf1, count1);
706 return drv->bdrv_pwrite(bs, offset, buf1, count1);
710 * Truncate file to 'offset' bytes (needed only for file protocols)
712 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
714 BlockDriver *drv = bs->drv;
715 if (!drv)
716 return -ENOMEDIUM;
717 if (!drv->bdrv_truncate)
718 return -ENOTSUP;
719 return drv->bdrv_truncate(bs, offset);
723 * Length of a file in bytes. Return < 0 if error or unknown.
725 int64_t bdrv_getlength(BlockDriverState *bs)
727 BlockDriver *drv = bs->drv;
728 if (!drv)
729 return -ENOMEDIUM;
730 if (!drv->bdrv_getlength) {
731 /* legacy mode */
732 return bs->total_sectors * SECTOR_SIZE;
734 return drv->bdrv_getlength(bs);
737 /* return 0 as number of sectors if no device present or error */
738 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
740 int64_t length;
741 length = bdrv_getlength(bs);
742 if (length < 0)
743 length = 0;
744 else
745 length = length >> SECTOR_BITS;
746 *nb_sectors_ptr = length;
749 struct partition {
750 uint8_t boot_ind; /* 0x80 - active */
751 uint8_t head; /* starting head */
752 uint8_t sector; /* starting sector */
753 uint8_t cyl; /* starting cylinder */
754 uint8_t sys_ind; /* What partition type */
755 uint8_t end_head; /* end head */
756 uint8_t end_sector; /* end sector */
757 uint8_t end_cyl; /* end cylinder */
758 uint32_t start_sect; /* starting sector counting from 0 */
759 uint32_t nr_sects; /* nr of sectors in partition */
760 } __attribute__((packed));
762 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
763 static int guess_disk_lchs(BlockDriverState *bs,
764 int *pcylinders, int *pheads, int *psectors)
766 uint8_t *buf;
767 int ret, i, heads, sectors, cylinders;
768 struct partition *p;
769 uint32_t nr_sects;
770 uint64_t nb_sectors;
772 buf = qemu_memalign(512, 512);
773 if (buf == NULL)
774 return -1;
776 bdrv_get_geometry(bs, &nb_sectors);
778 ret = bdrv_read(bs, 0, buf, 1);
779 if (ret < 0)
780 return -1;
781 /* test msdos magic */
782 if (buf[510] != 0x55 || buf[511] != 0xaa) {
783 qemu_free(buf);
784 return -1;
786 for(i = 0; i < 4; i++) {
787 p = ((struct partition *)(buf + 0x1be)) + i;
788 nr_sects = le32_to_cpu(p->nr_sects);
789 if (nr_sects && p->end_head) {
790 /* We make the assumption that the partition terminates on
791 a cylinder boundary */
792 heads = p->end_head + 1;
793 sectors = p->end_sector & 63;
794 if (sectors == 0)
795 continue;
796 cylinders = nb_sectors / (heads * sectors);
797 if (cylinders < 1 || cylinders > 16383)
798 continue;
799 *pheads = heads;
800 *psectors = sectors;
801 *pcylinders = cylinders;
802 #if 0
803 printf("guessed geometry: LCHS=%d %d %d\n",
804 cylinders, heads, sectors);
805 #endif
806 qemu_free(buf);
807 return 0;
810 qemu_free(buf);
811 return -1;
814 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
816 int translation, lba_detected = 0;
817 int cylinders, heads, secs;
818 uint64_t nb_sectors;
820 /* if a geometry hint is available, use it */
821 bdrv_get_geometry(bs, &nb_sectors);
822 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
823 translation = bdrv_get_translation_hint(bs);
824 if (cylinders != 0) {
825 *pcyls = cylinders;
826 *pheads = heads;
827 *psecs = secs;
828 } else {
829 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
830 if (heads > 16) {
831 /* if heads > 16, it means that a BIOS LBA
832 translation was active, so the default
833 hardware geometry is OK */
834 lba_detected = 1;
835 goto default_geometry;
836 } else {
837 *pcyls = cylinders;
838 *pheads = heads;
839 *psecs = secs;
840 /* disable any translation to be in sync with
841 the logical geometry */
842 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
843 bdrv_set_translation_hint(bs,
844 BIOS_ATA_TRANSLATION_NONE);
847 } else {
848 default_geometry:
849 /* if no geometry, use a standard physical disk geometry */
850 cylinders = nb_sectors / (16 * 63);
852 if (cylinders > 16383)
853 cylinders = 16383;
854 else if (cylinders < 2)
855 cylinders = 2;
856 *pcyls = cylinders;
857 *pheads = 16;
858 *psecs = 63;
859 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
860 if ((*pcyls * *pheads) <= 131072) {
861 bdrv_set_translation_hint(bs,
862 BIOS_ATA_TRANSLATION_LARGE);
863 } else {
864 bdrv_set_translation_hint(bs,
865 BIOS_ATA_TRANSLATION_LBA);
869 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
873 void bdrv_set_geometry_hint(BlockDriverState *bs,
874 int cyls, int heads, int secs)
876 bs->cyls = cyls;
877 bs->heads = heads;
878 bs->secs = secs;
881 void bdrv_set_type_hint(BlockDriverState *bs, int type)
883 bs->type = type;
884 bs->removable = ((type == BDRV_TYPE_CDROM ||
885 type == BDRV_TYPE_FLOPPY));
888 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
890 bs->translation = translation;
893 void bdrv_get_geometry_hint(BlockDriverState *bs,
894 int *pcyls, int *pheads, int *psecs)
896 *pcyls = bs->cyls;
897 *pheads = bs->heads;
898 *psecs = bs->secs;
901 int bdrv_get_type_hint(BlockDriverState *bs)
903 return bs->type;
906 int bdrv_get_translation_hint(BlockDriverState *bs)
908 return bs->translation;
911 int bdrv_is_removable(BlockDriverState *bs)
913 return bs->removable;
916 int bdrv_is_read_only(BlockDriverState *bs)
918 return bs->read_only;
921 int bdrv_is_sg(BlockDriverState *bs)
923 return bs->sg;
926 /* XXX: no longer used */
927 void bdrv_set_change_cb(BlockDriverState *bs,
928 void (*change_cb)(void *opaque), void *opaque)
930 bs->change_cb = change_cb;
931 bs->change_opaque = opaque;
934 int bdrv_is_encrypted(BlockDriverState *bs)
936 if (bs->backing_hd && bs->backing_hd->encrypted)
937 return 1;
938 return bs->encrypted;
941 int bdrv_set_key(BlockDriverState *bs, const char *key)
943 int ret;
944 if (bs->backing_hd && bs->backing_hd->encrypted) {
945 ret = bdrv_set_key(bs->backing_hd, key);
946 if (ret < 0)
947 return ret;
948 if (!bs->encrypted)
949 return 0;
951 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
952 return -1;
953 return bs->drv->bdrv_set_key(bs, key);
956 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
958 if (!bs->drv) {
959 buf[0] = '\0';
960 } else {
961 pstrcpy(buf, buf_size, bs->drv->format_name);
965 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
966 void *opaque)
968 BlockDriver *drv;
970 for (drv = first_drv; drv != NULL; drv = drv->next) {
971 it(opaque, drv->format_name);
975 BlockDriverState *bdrv_find(const char *name)
977 BlockDriverState *bs;
979 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
980 if (!strcmp(name, bs->device_name))
981 return bs;
983 return NULL;
986 void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
988 BlockDriverState *bs;
990 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
991 it(opaque, bs->device_name);
995 const char *bdrv_get_device_name(BlockDriverState *bs)
997 return bs->device_name;
1000 void bdrv_flush(BlockDriverState *bs)
1002 if (bs->drv->bdrv_flush)
1003 bs->drv->bdrv_flush(bs);
1004 if (bs->backing_hd)
1005 bdrv_flush(bs->backing_hd);
1008 void bdrv_iterate_writeable(void (*it)(BlockDriverState *bs))
1010 BlockDriverState *bs;
1012 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1013 if (bs->drv && !bdrv_is_read_only(bs) &&
1014 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1015 it(bs);
1018 void bdrv_flush_all(void)
1020 BlockDriverState *bs;
1022 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1023 if (bs->drv && !bdrv_is_read_only(bs) &&
1024 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1025 bdrv_flush(bs);
1029 * Returns true iff the specified sector is present in the disk image. Drivers
1030 * not implementing the functionality are assumed to not support backing files,
1031 * hence all their sectors are reported as allocated.
1033 * 'pnum' is set to the number of sectors (including and immediately following
1034 * the specified sector) that are known to be in the same
1035 * allocated/unallocated state.
1037 * 'nb_sectors' is the max value 'pnum' should be set to.
1039 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1040 int *pnum)
1042 int64_t n;
1043 if (!bs->drv->bdrv_is_allocated) {
1044 if (sector_num >= bs->total_sectors) {
1045 *pnum = 0;
1046 return 0;
1048 n = bs->total_sectors - sector_num;
1049 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1050 return 1;
1052 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1055 void bdrv_info(void)
1057 BlockDriverState *bs;
1059 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1060 term_printf("%s:", bs->device_name);
1061 term_printf(" type=");
1062 switch(bs->type) {
1063 case BDRV_TYPE_HD:
1064 term_printf("hd");
1065 break;
1066 case BDRV_TYPE_CDROM:
1067 term_printf("cdrom");
1068 break;
1069 case BDRV_TYPE_FLOPPY:
1070 term_printf("floppy");
1071 break;
1073 term_printf(" removable=%d", bs->removable);
1074 if (bs->removable) {
1075 term_printf(" locked=%d", bs->locked);
1077 if (bs->drv) {
1078 term_printf(" file=");
1079 term_print_filename(bs->filename);
1080 if (bs->backing_file[0] != '\0') {
1081 term_printf(" backing_file=");
1082 term_print_filename(bs->backing_file);
1084 term_printf(" ro=%d", bs->read_only);
1085 term_printf(" drv=%s", bs->drv->format_name);
1086 if (bs->encrypted)
1087 term_printf(" encrypted");
1088 } else {
1089 term_printf(" [not inserted]");
1091 term_printf("\n");
1095 /* The "info blockstats" command. */
1096 void bdrv_info_stats (void)
1098 BlockDriverState *bs;
1099 BlockDriverInfo bdi;
1101 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1102 term_printf ("%s:"
1103 " rd_bytes=%" PRIu64
1104 " wr_bytes=%" PRIu64
1105 " rd_operations=%" PRIu64
1106 " wr_operations=%" PRIu64
1108 bs->device_name,
1109 bs->rd_bytes, bs->wr_bytes,
1110 bs->rd_ops, bs->wr_ops);
1111 if (bdrv_get_info(bs, &bdi) == 0)
1112 term_printf(" high=%" PRId64
1113 " bytes_free=%" PRId64,
1114 bdi.highest_alloc, bdi.num_free_bytes);
1115 term_printf("\n");
1119 void bdrv_get_backing_filename(BlockDriverState *bs,
1120 char *filename, int filename_size)
1122 if (!bs->backing_hd) {
1123 pstrcpy(filename, filename_size, "");
1124 } else {
1125 pstrcpy(filename, filename_size, bs->backing_file);
1129 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1130 const uint8_t *buf, int nb_sectors)
1132 BlockDriver *drv = bs->drv;
1133 if (!drv)
1134 return -ENOMEDIUM;
1135 if (!drv->bdrv_write_compressed)
1136 return -ENOTSUP;
1137 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1140 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1142 BlockDriver *drv = bs->drv;
1143 if (!drv)
1144 return -ENOMEDIUM;
1145 if (!drv->bdrv_get_info)
1146 return -ENOTSUP;
1147 memset(bdi, 0, sizeof(*bdi));
1148 return drv->bdrv_get_info(bs, bdi);
1151 /**************************************************************/
1152 /* handling of snapshots */
1154 int bdrv_snapshot_create(BlockDriverState *bs,
1155 QEMUSnapshotInfo *sn_info)
1157 BlockDriver *drv = bs->drv;
1158 if (!drv)
1159 return -ENOMEDIUM;
1160 if (!drv->bdrv_snapshot_create)
1161 return -ENOTSUP;
1162 return drv->bdrv_snapshot_create(bs, sn_info);
1165 int bdrv_snapshot_goto(BlockDriverState *bs,
1166 const char *snapshot_id)
1168 BlockDriver *drv = bs->drv;
1169 if (!drv)
1170 return -ENOMEDIUM;
1171 if (!drv->bdrv_snapshot_goto)
1172 return -ENOTSUP;
1173 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1176 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1178 BlockDriver *drv = bs->drv;
1179 if (!drv)
1180 return -ENOMEDIUM;
1181 if (!drv->bdrv_snapshot_delete)
1182 return -ENOTSUP;
1183 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1186 int bdrv_snapshot_list(BlockDriverState *bs,
1187 QEMUSnapshotInfo **psn_info)
1189 BlockDriver *drv = bs->drv;
1190 if (!drv)
1191 return -ENOMEDIUM;
1192 if (!drv->bdrv_snapshot_list)
1193 return -ENOTSUP;
1194 return drv->bdrv_snapshot_list(bs, psn_info);
1197 #define NB_SUFFIXES 4
1199 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1201 static const char suffixes[NB_SUFFIXES] = "KMGT";
1202 int64_t base;
1203 int i;
1205 if (size <= 999) {
1206 snprintf(buf, buf_size, "%" PRId64, size);
1207 } else {
1208 base = 1024;
1209 for(i = 0; i < NB_SUFFIXES; i++) {
1210 if (size < (10 * base)) {
1211 snprintf(buf, buf_size, "%0.1f%c",
1212 (double)size / base,
1213 suffixes[i]);
1214 break;
1215 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1216 snprintf(buf, buf_size, "%" PRId64 "%c",
1217 ((size + (base >> 1)) / base),
1218 suffixes[i]);
1219 break;
1221 base = base * 1024;
1224 return buf;
1227 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1229 char buf1[128], date_buf[128], clock_buf[128];
1230 #ifdef _WIN32
1231 struct tm *ptm;
1232 #else
1233 struct tm tm;
1234 #endif
1235 time_t ti;
1236 int64_t secs;
1238 if (!sn) {
1239 snprintf(buf, buf_size,
1240 "%-10s%-20s%7s%20s%15s",
1241 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1242 } else {
1243 ti = sn->date_sec;
1244 #ifdef _WIN32
1245 ptm = localtime(&ti);
1246 strftime(date_buf, sizeof(date_buf),
1247 "%Y-%m-%d %H:%M:%S", ptm);
1248 #else
1249 localtime_r(&ti, &tm);
1250 strftime(date_buf, sizeof(date_buf),
1251 "%Y-%m-%d %H:%M:%S", &tm);
1252 #endif
1253 secs = sn->vm_clock_nsec / 1000000000;
1254 snprintf(clock_buf, sizeof(clock_buf),
1255 "%02d:%02d:%02d.%03d",
1256 (int)(secs / 3600),
1257 (int)((secs / 60) % 60),
1258 (int)(secs % 60),
1259 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1260 snprintf(buf, buf_size,
1261 "%-10s%-20s%7s%20s%15s",
1262 sn->id_str, sn->name,
1263 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1264 date_buf,
1265 clock_buf);
1267 return buf;
1271 /**************************************************************/
1272 /* async I/Os */
1274 typedef struct VectorTranslationState {
1275 QEMUIOVector *iov;
1276 uint8_t *bounce;
1277 int is_write;
1278 BlockDriverAIOCB *aiocb;
1279 BlockDriverAIOCB *this_aiocb;
1280 } VectorTranslationState;
1282 static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
1284 VectorTranslationState *s = opaque;
1286 if (!s->is_write) {
1287 qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
1289 qemu_free(s->bounce);
1290 s->this_aiocb->cb(s->this_aiocb->opaque, ret);
1291 qemu_aio_release(s->this_aiocb);
1294 static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1295 int64_t sector_num,
1296 QEMUIOVector *iov,
1297 int nb_sectors,
1298 BlockDriverCompletionFunc *cb,
1299 void *opaque,
1300 int is_write)
1303 VectorTranslationState *s = qemu_mallocz(sizeof(*s));
1304 BlockDriverAIOCB *aiocb = qemu_aio_get(bs, cb, opaque);
1306 s->this_aiocb = aiocb;
1307 s->iov = iov;
1308 s->bounce = qemu_memalign(512, nb_sectors * 512);
1309 s->is_write = is_write;
1310 if (is_write) {
1311 qemu_iovec_to_buffer(s->iov, s->bounce);
1312 s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
1313 bdrv_aio_rw_vector_cb, s);
1314 } else {
1315 s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors,
1316 bdrv_aio_rw_vector_cb, s);
1318 return aiocb;
1321 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1322 QEMUIOVector *iov, int nb_sectors,
1323 BlockDriverCompletionFunc *cb, void *opaque)
1325 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1326 cb, opaque, 0);
1329 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1330 QEMUIOVector *iov, int nb_sectors,
1331 BlockDriverCompletionFunc *cb, void *opaque)
1333 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1334 cb, opaque, 1);
1337 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1338 uint8_t *buf, int nb_sectors,
1339 BlockDriverCompletionFunc *cb, void *opaque)
1341 BlockDriver *drv = bs->drv;
1342 BlockDriverAIOCB *ret;
1344 if (!drv)
1345 return NULL;
1347 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1349 if (ret) {
1350 /* Update stats even though technically transfer has not happened. */
1351 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1352 bs->rd_ops ++;
1355 return ret;
1358 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1359 const uint8_t *buf, int nb_sectors,
1360 BlockDriverCompletionFunc *cb, void *opaque)
1362 BlockDriver *drv = bs->drv;
1363 BlockDriverAIOCB *ret;
1365 if (!drv)
1366 return NULL;
1367 if (bs->read_only)
1368 return NULL;
1370 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1372 if (ret) {
1373 /* Update stats even though technically transfer has not happened. */
1374 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1375 bs->wr_ops ++;
1378 return ret;
1381 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1383 BlockDriver *drv = acb->bs->drv;
1385 if (acb->cb == bdrv_aio_rw_vector_cb) {
1386 VectorTranslationState *s = acb->opaque;
1387 acb = s->aiocb;
1390 drv->bdrv_aio_cancel(acb);
1394 /**************************************************************/
1395 /* async block device emulation */
1397 static void bdrv_aio_bh_cb(void *opaque)
1399 BlockDriverAIOCBSync *acb = opaque;
1400 acb->common.cb(acb->common.opaque, acb->ret);
1401 qemu_aio_release(acb);
1404 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1405 int64_t sector_num, uint8_t *buf, int nb_sectors,
1406 BlockDriverCompletionFunc *cb, void *opaque)
1408 BlockDriverAIOCBSync *acb;
1409 int ret;
1411 acb = qemu_aio_get(bs, cb, opaque);
1412 if (!acb->bh)
1413 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1414 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1415 acb->ret = ret;
1416 qemu_bh_schedule(acb->bh);
1417 return &acb->common;
1420 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1421 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1422 BlockDriverCompletionFunc *cb, void *opaque)
1424 BlockDriverAIOCBSync *acb;
1425 int ret;
1427 acb = qemu_aio_get(bs, cb, opaque);
1428 if (!acb->bh)
1429 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1430 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1431 acb->ret = ret;
1432 qemu_bh_schedule(acb->bh);
1433 return &acb->common;
1436 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1438 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1439 qemu_bh_cancel(acb->bh);
1440 qemu_aio_release(acb);
1443 /**************************************************************/
1444 /* sync block device emulation */
1446 static void bdrv_rw_em_cb(void *opaque, int ret)
1448 *(int *)opaque = ret;
1451 #define NOT_DONE 0x7fffffff
1453 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1454 uint8_t *buf, int nb_sectors)
1456 int async_ret;
1457 BlockDriverAIOCB *acb;
1459 async_ret = NOT_DONE;
1460 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1461 bdrv_rw_em_cb, &async_ret);
1462 if (acb == NULL)
1463 return -1;
1465 while (async_ret == NOT_DONE) {
1466 qemu_aio_wait();
1469 return async_ret;
1472 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1473 const uint8_t *buf, int nb_sectors)
1475 int async_ret;
1476 BlockDriverAIOCB *acb;
1478 async_ret = NOT_DONE;
1479 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1480 bdrv_rw_em_cb, &async_ret);
1481 if (acb == NULL)
1482 return -1;
1483 while (async_ret == NOT_DONE) {
1484 qemu_aio_wait();
1486 return async_ret;
1489 void bdrv_init(void)
1491 bdrv_register(&bdrv_raw);
1492 bdrv_register(&bdrv_host_device);
1493 #ifndef _WIN32
1494 bdrv_register(&bdrv_cow);
1495 #endif
1496 bdrv_register(&bdrv_qcow);
1497 bdrv_register(&bdrv_vmdk);
1498 bdrv_register(&bdrv_cloop);
1499 bdrv_register(&bdrv_dmg);
1500 bdrv_register(&bdrv_bochs);
1501 bdrv_register(&bdrv_vpc);
1502 bdrv_register(&bdrv_vvfat);
1503 bdrv_register(&bdrv_qcow2);
1504 bdrv_register(&bdrv_parallels);
1505 bdrv_register(&bdrv_nbd);
1508 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1509 void *opaque)
1511 BlockDriver *drv;
1512 BlockDriverAIOCB *acb;
1514 drv = bs->drv;
1515 if (drv->free_aiocb) {
1516 acb = drv->free_aiocb;
1517 drv->free_aiocb = acb->next;
1518 } else {
1519 acb = qemu_mallocz(drv->aiocb_size);
1520 if (!acb)
1521 return NULL;
1523 acb->bs = bs;
1524 acb->cb = cb;
1525 acb->opaque = opaque;
1526 return acb;
1529 void qemu_aio_release(void *p)
1531 BlockDriverAIOCB *acb = p;
1532 BlockDriver *drv = acb->bs->drv;
1533 acb->next = drv->free_aiocb;
1534 drv->free_aiocb = acb;
1537 /**************************************************************/
1538 /* removable device support */
1541 * Return TRUE if the media is present
1543 int bdrv_is_inserted(BlockDriverState *bs)
1545 BlockDriver *drv = bs->drv;
1546 int ret;
1547 if (!drv)
1548 return 0;
1549 if (!drv->bdrv_is_inserted)
1550 return 1;
1551 ret = drv->bdrv_is_inserted(bs);
1552 return ret;
1556 * Return TRUE if the media changed since the last call to this
1557 * function. It is currently only used for floppy disks
1559 int bdrv_media_changed(BlockDriverState *bs)
1561 BlockDriver *drv = bs->drv;
1562 int ret;
1564 if (!drv || !drv->bdrv_media_changed)
1565 ret = -ENOTSUP;
1566 else
1567 ret = drv->bdrv_media_changed(bs);
1568 if (ret == -ENOTSUP)
1569 ret = bs->media_changed;
1570 bs->media_changed = 0;
1571 return ret;
1575 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1577 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1579 BlockDriver *drv = bs->drv;
1580 int ret;
1582 if (!drv || !drv->bdrv_eject) {
1583 ret = -ENOTSUP;
1584 } else {
1585 ret = drv->bdrv_eject(bs, eject_flag);
1587 if (ret == -ENOTSUP) {
1588 if (eject_flag)
1589 bdrv_close(bs);
1593 int bdrv_is_locked(BlockDriverState *bs)
1595 return bs->locked;
1599 * Lock or unlock the media (if it is locked, the user won't be able
1600 * to eject it manually).
1602 void bdrv_set_locked(BlockDriverState *bs, int locked)
1604 BlockDriver *drv = bs->drv;
1606 bs->locked = locked;
1607 if (drv && drv->bdrv_set_locked) {
1608 drv->bdrv_set_locked(bs, locked);
1612 /* needed for generic scsi interface */
1614 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1616 BlockDriver *drv = bs->drv;
1618 if (drv && drv->bdrv_ioctl)
1619 return drv->bdrv_ioctl(bs, req, buf);
1620 return -ENOTSUP;