kvm: libkvm: add deassign ioctl
[qemu-kvm/fedora.git] / block.c
blob4c556ec280f277b3a0fb84cff2ec015174113e38
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 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
156 if (device_name[0] != '\0') {
157 /* insert at the end */
158 pbs = &bdrv_first;
159 while (*pbs != NULL)
160 pbs = &(*pbs)->next;
161 *pbs = bs;
163 return bs;
166 BlockDriver *bdrv_find_format(const char *format_name)
168 BlockDriver *drv1;
169 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
170 if (!strcmp(drv1->format_name, format_name))
171 return drv1;
173 return NULL;
176 int bdrv_create(BlockDriver *drv,
177 const char *filename, int64_t size_in_sectors,
178 const char *backing_file, int flags)
180 if (!drv->bdrv_create)
181 return -ENOTSUP;
182 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
185 #ifdef _WIN32
186 void get_tmp_filename(char *filename, int size)
188 char temp_dir[MAX_PATH];
190 GetTempPath(MAX_PATH, temp_dir);
191 GetTempFileName(temp_dir, "qem", 0, filename);
193 #else
194 void get_tmp_filename(char *filename, int size)
196 int fd;
197 const char *tmpdir;
198 /* XXX: race condition possible */
199 tmpdir = getenv("TMPDIR");
200 if (!tmpdir)
201 tmpdir = "/tmp";
202 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
203 fd = mkstemp(filename);
204 close(fd);
206 #endif
208 #ifdef _WIN32
209 static int is_windows_drive_prefix(const char *filename)
211 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
212 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
213 filename[1] == ':');
216 static int is_windows_drive(const char *filename)
218 if (is_windows_drive_prefix(filename) &&
219 filename[2] == '\0')
220 return 1;
221 if (strstart(filename, "\\\\.\\", NULL) ||
222 strstart(filename, "//./", NULL))
223 return 1;
224 return 0;
226 #endif
228 static BlockDriver *find_protocol(const char *filename)
230 BlockDriver *drv1;
231 char protocol[128];
232 int len;
233 const char *p;
235 #ifdef _WIN32
236 if (is_windows_drive(filename) ||
237 is_windows_drive_prefix(filename))
238 return &bdrv_raw;
239 #endif
240 p = strchr(filename, ':');
241 if (!p)
242 return &bdrv_raw;
243 len = p - filename;
244 if (len > sizeof(protocol) - 1)
245 len = sizeof(protocol) - 1;
246 memcpy(protocol, filename, len);
247 protocol[len] = '\0';
248 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
249 if (drv1->protocol_name &&
250 !strcmp(drv1->protocol_name, protocol))
251 return drv1;
253 return NULL;
256 /* XXX: force raw format if block or character device ? It would
257 simplify the BSD case */
258 static BlockDriver *find_image_format(const char *filename)
260 int ret, score, score_max;
261 BlockDriver *drv1, *drv;
262 uint8_t buf[2048];
263 BlockDriverState *bs;
265 /* detect host devices. By convention, /dev/cdrom[N] is always
266 recognized as a host CDROM */
267 if (strstart(filename, "/dev/cdrom", NULL))
268 return &bdrv_host_device;
269 #ifdef _WIN32
270 if (is_windows_drive(filename))
271 return &bdrv_host_device;
272 #else
274 struct stat st;
275 if (stat(filename, &st) >= 0 &&
276 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
277 return &bdrv_host_device;
280 #endif
282 drv = find_protocol(filename);
283 /* no need to test disk image formats for vvfat */
284 if (drv == &bdrv_vvfat)
285 return drv;
287 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
288 if (ret < 0)
289 return NULL;
290 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
291 bdrv_delete(bs);
292 if (ret < 0) {
293 return NULL;
296 score_max = 0;
297 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
298 if (drv1->bdrv_probe) {
299 score = drv1->bdrv_probe(buf, ret, filename);
300 if (score > score_max) {
301 score_max = score;
302 drv = drv1;
306 return drv;
309 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
311 BlockDriverState *bs;
312 int ret;
314 bs = bdrv_new("");
315 if (!bs)
316 return -ENOMEM;
317 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
318 if (ret < 0) {
319 bdrv_delete(bs);
320 return ret;
322 *pbs = bs;
323 return 0;
326 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
328 return bdrv_open2(bs, filename, flags, NULL);
331 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
332 BlockDriver *drv)
334 int ret, open_flags;
335 char tmp_filename[PATH_MAX];
336 char backing_filename[PATH_MAX];
338 bs->read_only = 0;
339 bs->is_temporary = 0;
340 bs->encrypted = 0;
342 if (flags & BDRV_O_SNAPSHOT) {
343 BlockDriverState *bs1;
344 int64_t total_size;
345 int is_protocol = 0;
347 /* if snapshot, we create a temporary backing file and open it
348 instead of opening 'filename' directly */
350 /* if there is a backing file, use it */
351 bs1 = bdrv_new("");
352 if (!bs1) {
353 return -ENOMEM;
355 if (bdrv_open(bs1, filename, 0) < 0) {
356 bdrv_delete(bs1);
357 return -1;
359 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
361 if (bs1->drv && bs1->drv->protocol_name)
362 is_protocol = 1;
364 bdrv_delete(bs1);
366 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
368 /* Real path is meaningless for protocols */
369 if (is_protocol)
370 snprintf(backing_filename, sizeof(backing_filename),
371 "%s", filename);
372 else
373 realpath(filename, backing_filename);
375 if (bdrv_create(&bdrv_qcow2, tmp_filename,
376 total_size, backing_filename, 0) < 0) {
377 return -1;
379 filename = tmp_filename;
380 bs->is_temporary = 1;
383 pstrcpy(bs->filename, sizeof(bs->filename), filename);
384 if (flags & BDRV_O_FILE) {
385 drv = find_protocol(filename);
386 if (!drv)
387 return -ENOENT;
388 } else {
389 if (!drv) {
390 drv = find_image_format(filename);
391 if (!drv)
392 return -1;
395 bs->drv = drv;
396 bs->opaque = qemu_mallocz(drv->instance_size);
397 /* Note: for compatibility, we open disk image files as RDWR, and
398 RDONLY as fallback */
399 if (!(flags & BDRV_O_FILE))
400 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
401 else
402 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
403 ret = drv->bdrv_open(bs, filename, open_flags);
404 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
405 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
406 bs->read_only = 1;
408 if (ret < 0) {
409 qemu_free(bs->opaque);
410 bs->opaque = NULL;
411 bs->drv = NULL;
412 return ret;
414 if (drv->bdrv_getlength) {
415 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
417 #ifndef _WIN32
418 if (bs->is_temporary) {
419 unlink(filename);
421 #endif
422 if (bs->backing_file[0] != '\0') {
423 /* if there is a backing file, use it */
424 bs->backing_hd = bdrv_new("");
425 if (!bs->backing_hd) {
426 fail:
427 bdrv_close(bs);
428 return -ENOMEM;
430 path_combine(backing_filename, sizeof(backing_filename),
431 filename, bs->backing_file);
432 if (bdrv_open(bs->backing_hd, backing_filename, open_flags) < 0)
433 goto fail;
436 /* call the change callback */
437 bs->media_changed = 1;
438 if (bs->change_cb)
439 bs->change_cb(bs->change_opaque);
441 return 0;
444 void bdrv_close(BlockDriverState *bs)
446 if (bs->drv) {
447 if (bs->backing_hd)
448 bdrv_delete(bs->backing_hd);
449 bs->drv->bdrv_close(bs);
450 qemu_free(bs->opaque);
451 #ifdef _WIN32
452 if (bs->is_temporary) {
453 unlink(bs->filename);
455 #endif
456 bs->opaque = NULL;
457 bs->drv = NULL;
459 /* call the change callback */
460 bs->media_changed = 1;
461 if (bs->change_cb)
462 bs->change_cb(bs->change_opaque);
466 void bdrv_delete(BlockDriverState *bs)
468 BlockDriverState **pbs;
470 pbs = &bdrv_first;
471 while (*pbs != bs && *pbs != NULL)
472 pbs = &(*pbs)->next;
473 if (*pbs == bs)
474 *pbs = bs->next;
476 bdrv_close(bs);
477 qemu_free(bs);
480 /* commit COW file into the raw image */
481 int bdrv_commit(BlockDriverState *bs)
483 BlockDriver *drv = bs->drv;
484 int64_t i, total_sectors;
485 int n, j;
486 unsigned char sector[512];
488 if (!drv)
489 return -ENOMEDIUM;
491 if (bs->read_only) {
492 return -EACCES;
495 if (!bs->backing_hd) {
496 return -ENOTSUP;
499 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
500 for (i = 0; i < total_sectors;) {
501 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
502 for(j = 0; j < n; j++) {
503 if (bdrv_read(bs, i, sector, 1) != 0) {
504 return -EIO;
507 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
508 return -EIO;
510 i++;
512 } else {
513 i += n;
517 if (drv->bdrv_make_empty)
518 return drv->bdrv_make_empty(bs);
520 return 0;
523 /* return < 0 if error. See bdrv_write() for the return codes */
524 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
525 uint8_t *buf, int nb_sectors)
527 BlockDriver *drv = bs->drv;
529 if (!drv)
530 return -ENOMEDIUM;
532 if (drv->bdrv_pread) {
533 int ret, len;
534 len = nb_sectors * 512;
535 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
536 if (ret < 0)
537 return ret;
538 else if (ret != len)
539 return -EINVAL;
540 else {
541 bs->rd_bytes += (unsigned) len;
542 bs->rd_ops ++;
543 return 0;
545 } else {
546 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
550 /* Return < 0 if error. Important errors are:
551 -EIO generic I/O error (may happen for all errors)
552 -ENOMEDIUM No media inserted.
553 -EINVAL Invalid sector number or nb_sectors
554 -EACCES Trying to write a read-only device
556 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
557 const uint8_t *buf, int nb_sectors)
559 BlockDriver *drv = bs->drv;
560 if (!bs->drv)
561 return -ENOMEDIUM;
562 if (bs->read_only)
563 return -EACCES;
564 if (drv->bdrv_pwrite) {
565 int ret, len, count = 0;
566 len = nb_sectors * 512;
567 do {
568 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len - count);
569 if (ret < 0) {
570 printf("bdrv_write ret=%d\n", ret);
571 return ret;
573 count += ret;
574 buf += ret;
575 } while (count != len);
576 bs->wr_bytes += (unsigned) len;
577 bs->wr_ops ++;
578 return 0;
580 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
583 static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
584 uint8_t *buf, int count1)
586 uint8_t tmp_buf[SECTOR_SIZE];
587 int len, nb_sectors, count;
588 int64_t sector_num;
590 count = count1;
591 /* first read to align to sector start */
592 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
593 if (len > count)
594 len = count;
595 sector_num = offset >> SECTOR_BITS;
596 if (len > 0) {
597 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
598 return -EIO;
599 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
600 count -= len;
601 if (count == 0)
602 return count1;
603 sector_num++;
604 buf += len;
607 /* read the sectors "in place" */
608 nb_sectors = count >> SECTOR_BITS;
609 if (nb_sectors > 0) {
610 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
611 return -EIO;
612 sector_num += nb_sectors;
613 len = nb_sectors << SECTOR_BITS;
614 buf += len;
615 count -= len;
618 /* add data from the last sector */
619 if (count > 0) {
620 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
621 return -EIO;
622 memcpy(buf, tmp_buf, count);
624 return count1;
627 static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
628 const uint8_t *buf, int count1)
630 uint8_t tmp_buf[SECTOR_SIZE];
631 int len, nb_sectors, count;
632 int64_t sector_num;
634 count = count1;
635 /* first write to align to sector start */
636 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
637 if (len > count)
638 len = count;
639 sector_num = offset >> SECTOR_BITS;
640 if (len > 0) {
641 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
642 return -EIO;
643 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
644 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
645 return -EIO;
646 count -= len;
647 if (count == 0)
648 return count1;
649 sector_num++;
650 buf += len;
653 /* write the sectors "in place" */
654 nb_sectors = count >> SECTOR_BITS;
655 if (nb_sectors > 0) {
656 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
657 return -EIO;
658 sector_num += nb_sectors;
659 len = nb_sectors << SECTOR_BITS;
660 buf += len;
661 count -= len;
664 /* add data from the last sector */
665 if (count > 0) {
666 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
667 return -EIO;
668 memcpy(tmp_buf, buf, count);
669 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
670 return -EIO;
672 return count1;
676 * Read with byte offsets (needed only for file protocols)
678 int bdrv_pread(BlockDriverState *bs, int64_t offset,
679 void *buf1, int count1)
681 BlockDriver *drv = bs->drv;
683 if (!drv)
684 return -ENOMEDIUM;
685 if (!drv->bdrv_pread)
686 return bdrv_pread_em(bs, offset, buf1, count1);
687 return drv->bdrv_pread(bs, offset, buf1, count1);
691 * Write with byte offsets (needed only for file protocols)
693 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
694 const void *buf1, int count1)
696 BlockDriver *drv = bs->drv;
698 if (!drv)
699 return -ENOMEDIUM;
700 if (!drv->bdrv_pwrite)
701 return bdrv_pwrite_em(bs, offset, buf1, count1);
702 return drv->bdrv_pwrite(bs, offset, buf1, count1);
706 * Truncate file to 'offset' bytes (needed only for file protocols)
708 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
710 BlockDriver *drv = bs->drv;
711 if (!drv)
712 return -ENOMEDIUM;
713 if (!drv->bdrv_truncate)
714 return -ENOTSUP;
715 return drv->bdrv_truncate(bs, offset);
719 * Length of a file in bytes. Return < 0 if error or unknown.
721 int64_t bdrv_getlength(BlockDriverState *bs)
723 BlockDriver *drv = bs->drv;
724 if (!drv)
725 return -ENOMEDIUM;
726 if (!drv->bdrv_getlength) {
727 /* legacy mode */
728 return bs->total_sectors * SECTOR_SIZE;
730 return drv->bdrv_getlength(bs);
733 /* return 0 as number of sectors if no device present or error */
734 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
736 int64_t length;
737 length = bdrv_getlength(bs);
738 if (length < 0)
739 length = 0;
740 else
741 length = length >> SECTOR_BITS;
742 *nb_sectors_ptr = length;
745 struct partition {
746 uint8_t boot_ind; /* 0x80 - active */
747 uint8_t head; /* starting head */
748 uint8_t sector; /* starting sector */
749 uint8_t cyl; /* starting cylinder */
750 uint8_t sys_ind; /* What partition type */
751 uint8_t end_head; /* end head */
752 uint8_t end_sector; /* end sector */
753 uint8_t end_cyl; /* end cylinder */
754 uint32_t start_sect; /* starting sector counting from 0 */
755 uint32_t nr_sects; /* nr of sectors in partition */
756 } __attribute__((packed));
758 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
759 static int guess_disk_lchs(BlockDriverState *bs,
760 int *pcylinders, int *pheads, int *psectors)
762 uint8_t *buf;
763 int ret, i, heads, sectors, cylinders;
764 struct partition *p;
765 uint32_t nr_sects;
766 uint64_t nb_sectors;
768 buf = qemu_memalign(512, 512);
769 if (buf == NULL)
770 return -1;
772 bdrv_get_geometry(bs, &nb_sectors);
774 ret = bdrv_read(bs, 0, buf, 1);
775 if (ret < 0)
776 return -1;
777 /* test msdos magic */
778 if (buf[510] != 0x55 || buf[511] != 0xaa) {
779 qemu_free(buf);
780 return -1;
782 for(i = 0; i < 4; i++) {
783 p = ((struct partition *)(buf + 0x1be)) + i;
784 nr_sects = le32_to_cpu(p->nr_sects);
785 if (nr_sects && p->end_head) {
786 /* We make the assumption that the partition terminates on
787 a cylinder boundary */
788 heads = p->end_head + 1;
789 sectors = p->end_sector & 63;
790 if (sectors == 0)
791 continue;
792 cylinders = nb_sectors / (heads * sectors);
793 if (cylinders < 1 || cylinders > 16383)
794 continue;
795 *pheads = heads;
796 *psectors = sectors;
797 *pcylinders = cylinders;
798 #if 0
799 printf("guessed geometry: LCHS=%d %d %d\n",
800 cylinders, heads, sectors);
801 #endif
802 qemu_free(buf);
803 return 0;
806 qemu_free(buf);
807 return -1;
810 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
812 int translation, lba_detected = 0;
813 int cylinders, heads, secs;
814 uint64_t nb_sectors;
816 /* if a geometry hint is available, use it */
817 bdrv_get_geometry(bs, &nb_sectors);
818 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
819 translation = bdrv_get_translation_hint(bs);
820 if (cylinders != 0) {
821 *pcyls = cylinders;
822 *pheads = heads;
823 *psecs = secs;
824 } else {
825 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
826 if (heads > 16) {
827 /* if heads > 16, it means that a BIOS LBA
828 translation was active, so the default
829 hardware geometry is OK */
830 lba_detected = 1;
831 goto default_geometry;
832 } else {
833 *pcyls = cylinders;
834 *pheads = heads;
835 *psecs = secs;
836 /* disable any translation to be in sync with
837 the logical geometry */
838 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
839 bdrv_set_translation_hint(bs,
840 BIOS_ATA_TRANSLATION_NONE);
843 } else {
844 default_geometry:
845 /* if no geometry, use a standard physical disk geometry */
846 cylinders = nb_sectors / (16 * 63);
848 if (cylinders > 16383)
849 cylinders = 16383;
850 else if (cylinders < 2)
851 cylinders = 2;
852 *pcyls = cylinders;
853 *pheads = 16;
854 *psecs = 63;
855 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
856 if ((*pcyls * *pheads) <= 131072) {
857 bdrv_set_translation_hint(bs,
858 BIOS_ATA_TRANSLATION_LARGE);
859 } else {
860 bdrv_set_translation_hint(bs,
861 BIOS_ATA_TRANSLATION_LBA);
865 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
869 void bdrv_set_geometry_hint(BlockDriverState *bs,
870 int cyls, int heads, int secs)
872 bs->cyls = cyls;
873 bs->heads = heads;
874 bs->secs = secs;
877 void bdrv_set_type_hint(BlockDriverState *bs, int type)
879 bs->type = type;
880 bs->removable = ((type == BDRV_TYPE_CDROM ||
881 type == BDRV_TYPE_FLOPPY));
884 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
886 bs->translation = translation;
889 void bdrv_get_geometry_hint(BlockDriverState *bs,
890 int *pcyls, int *pheads, int *psecs)
892 *pcyls = bs->cyls;
893 *pheads = bs->heads;
894 *psecs = bs->secs;
897 int bdrv_get_type_hint(BlockDriverState *bs)
899 return bs->type;
902 int bdrv_get_translation_hint(BlockDriverState *bs)
904 return bs->translation;
907 int bdrv_is_removable(BlockDriverState *bs)
909 return bs->removable;
912 int bdrv_is_read_only(BlockDriverState *bs)
914 return bs->read_only;
917 int bdrv_is_sg(BlockDriverState *bs)
919 return bs->sg;
922 /* XXX: no longer used */
923 void bdrv_set_change_cb(BlockDriverState *bs,
924 void (*change_cb)(void *opaque), void *opaque)
926 bs->change_cb = change_cb;
927 bs->change_opaque = opaque;
930 int bdrv_is_encrypted(BlockDriverState *bs)
932 if (bs->backing_hd && bs->backing_hd->encrypted)
933 return 1;
934 return bs->encrypted;
937 int bdrv_set_key(BlockDriverState *bs, const char *key)
939 int ret;
940 if (bs->backing_hd && bs->backing_hd->encrypted) {
941 ret = bdrv_set_key(bs->backing_hd, key);
942 if (ret < 0)
943 return ret;
944 if (!bs->encrypted)
945 return 0;
947 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
948 return -1;
949 return bs->drv->bdrv_set_key(bs, key);
952 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
954 if (!bs->drv) {
955 buf[0] = '\0';
956 } else {
957 pstrcpy(buf, buf_size, bs->drv->format_name);
961 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
962 void *opaque)
964 BlockDriver *drv;
966 for (drv = first_drv; drv != NULL; drv = drv->next) {
967 it(opaque, drv->format_name);
971 BlockDriverState *bdrv_find(const char *name)
973 BlockDriverState *bs;
975 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
976 if (!strcmp(name, bs->device_name))
977 return bs;
979 return NULL;
982 void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
984 BlockDriverState *bs;
986 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
987 it(opaque, bs->device_name);
991 const char *bdrv_get_device_name(BlockDriverState *bs)
993 return bs->device_name;
996 void bdrv_flush(BlockDriverState *bs)
998 if (bs->drv->bdrv_flush)
999 bs->drv->bdrv_flush(bs);
1000 if (bs->backing_hd)
1001 bdrv_flush(bs->backing_hd);
1004 void bdrv_flush_all(void)
1006 BlockDriverState *bs;
1008 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1009 if (bs->drv && !bdrv_is_read_only(bs) &&
1010 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1011 bdrv_flush(bs);
1015 * Returns true iff the specified sector is present in the disk image. Drivers
1016 * not implementing the functionality are assumed to not support backing files,
1017 * hence all their sectors are reported as allocated.
1019 * 'pnum' is set to the number of sectors (including and immediately following
1020 * the specified sector) that are known to be in the same
1021 * allocated/unallocated state.
1023 * 'nb_sectors' is the max value 'pnum' should be set to.
1025 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1026 int *pnum)
1028 int64_t n;
1029 if (!bs->drv->bdrv_is_allocated) {
1030 if (sector_num >= bs->total_sectors) {
1031 *pnum = 0;
1032 return 0;
1034 n = bs->total_sectors - sector_num;
1035 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1036 return 1;
1038 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1041 void bdrv_info(void)
1043 BlockDriverState *bs;
1045 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1046 term_printf("%s:", bs->device_name);
1047 term_printf(" type=");
1048 switch(bs->type) {
1049 case BDRV_TYPE_HD:
1050 term_printf("hd");
1051 break;
1052 case BDRV_TYPE_CDROM:
1053 term_printf("cdrom");
1054 break;
1055 case BDRV_TYPE_FLOPPY:
1056 term_printf("floppy");
1057 break;
1059 term_printf(" removable=%d", bs->removable);
1060 if (bs->removable) {
1061 term_printf(" locked=%d", bs->locked);
1063 if (bs->drv) {
1064 term_printf(" file=");
1065 term_print_filename(bs->filename);
1066 if (bs->backing_file[0] != '\0') {
1067 term_printf(" backing_file=");
1068 term_print_filename(bs->backing_file);
1070 term_printf(" ro=%d", bs->read_only);
1071 term_printf(" drv=%s", bs->drv->format_name);
1072 if (bs->encrypted)
1073 term_printf(" encrypted");
1074 } else {
1075 term_printf(" [not inserted]");
1077 term_printf("\n");
1081 /* The "info blockstats" command. */
1082 void bdrv_info_stats (void)
1084 BlockDriverState *bs;
1085 BlockDriverInfo bdi;
1087 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1088 term_printf ("%s:"
1089 " rd_bytes=%" PRIu64
1090 " wr_bytes=%" PRIu64
1091 " rd_operations=%" PRIu64
1092 " wr_operations=%" PRIu64
1094 bs->device_name,
1095 bs->rd_bytes, bs->wr_bytes,
1096 bs->rd_ops, bs->wr_ops);
1097 if (bdrv_get_info(bs, &bdi) == 0)
1098 term_printf(" high=%" PRId64
1099 " bytes_free=%" PRId64,
1100 bdi.highest_alloc, bdi.num_free_bytes);
1101 term_printf("\n");
1105 void bdrv_get_backing_filename(BlockDriverState *bs,
1106 char *filename, int filename_size)
1108 if (!bs->backing_hd) {
1109 pstrcpy(filename, filename_size, "");
1110 } else {
1111 pstrcpy(filename, filename_size, bs->backing_file);
1115 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1116 const uint8_t *buf, int nb_sectors)
1118 BlockDriver *drv = bs->drv;
1119 if (!drv)
1120 return -ENOMEDIUM;
1121 if (!drv->bdrv_write_compressed)
1122 return -ENOTSUP;
1123 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1126 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1128 BlockDriver *drv = bs->drv;
1129 if (!drv)
1130 return -ENOMEDIUM;
1131 if (!drv->bdrv_get_info)
1132 return -ENOTSUP;
1133 memset(bdi, 0, sizeof(*bdi));
1134 return drv->bdrv_get_info(bs, bdi);
1137 /**************************************************************/
1138 /* handling of snapshots */
1140 int bdrv_snapshot_create(BlockDriverState *bs,
1141 QEMUSnapshotInfo *sn_info)
1143 BlockDriver *drv = bs->drv;
1144 if (!drv)
1145 return -ENOMEDIUM;
1146 if (!drv->bdrv_snapshot_create)
1147 return -ENOTSUP;
1148 return drv->bdrv_snapshot_create(bs, sn_info);
1151 int bdrv_snapshot_goto(BlockDriverState *bs,
1152 const char *snapshot_id)
1154 BlockDriver *drv = bs->drv;
1155 if (!drv)
1156 return -ENOMEDIUM;
1157 if (!drv->bdrv_snapshot_goto)
1158 return -ENOTSUP;
1159 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1162 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1164 BlockDriver *drv = bs->drv;
1165 if (!drv)
1166 return -ENOMEDIUM;
1167 if (!drv->bdrv_snapshot_delete)
1168 return -ENOTSUP;
1169 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1172 int bdrv_snapshot_list(BlockDriverState *bs,
1173 QEMUSnapshotInfo **psn_info)
1175 BlockDriver *drv = bs->drv;
1176 if (!drv)
1177 return -ENOMEDIUM;
1178 if (!drv->bdrv_snapshot_list)
1179 return -ENOTSUP;
1180 return drv->bdrv_snapshot_list(bs, psn_info);
1183 #define NB_SUFFIXES 4
1185 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1187 static const char suffixes[NB_SUFFIXES] = "KMGT";
1188 int64_t base;
1189 int i;
1191 if (size <= 999) {
1192 snprintf(buf, buf_size, "%" PRId64, size);
1193 } else {
1194 base = 1024;
1195 for(i = 0; i < NB_SUFFIXES; i++) {
1196 if (size < (10 * base)) {
1197 snprintf(buf, buf_size, "%0.1f%c",
1198 (double)size / base,
1199 suffixes[i]);
1200 break;
1201 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1202 snprintf(buf, buf_size, "%" PRId64 "%c",
1203 ((size + (base >> 1)) / base),
1204 suffixes[i]);
1205 break;
1207 base = base * 1024;
1210 return buf;
1213 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1215 char buf1[128], date_buf[128], clock_buf[128];
1216 #ifdef _WIN32
1217 struct tm *ptm;
1218 #else
1219 struct tm tm;
1220 #endif
1221 time_t ti;
1222 int64_t secs;
1224 if (!sn) {
1225 snprintf(buf, buf_size,
1226 "%-10s%-20s%7s%20s%15s",
1227 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1228 } else {
1229 ti = sn->date_sec;
1230 #ifdef _WIN32
1231 ptm = localtime(&ti);
1232 strftime(date_buf, sizeof(date_buf),
1233 "%Y-%m-%d %H:%M:%S", ptm);
1234 #else
1235 localtime_r(&ti, &tm);
1236 strftime(date_buf, sizeof(date_buf),
1237 "%Y-%m-%d %H:%M:%S", &tm);
1238 #endif
1239 secs = sn->vm_clock_nsec / 1000000000;
1240 snprintf(clock_buf, sizeof(clock_buf),
1241 "%02d:%02d:%02d.%03d",
1242 (int)(secs / 3600),
1243 (int)((secs / 60) % 60),
1244 (int)(secs % 60),
1245 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1246 snprintf(buf, buf_size,
1247 "%-10s%-20s%7s%20s%15s",
1248 sn->id_str, sn->name,
1249 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1250 date_buf,
1251 clock_buf);
1253 return buf;
1257 /**************************************************************/
1258 /* async I/Os */
1260 typedef struct VectorTranslationState {
1261 QEMUIOVector *iov;
1262 uint8_t *bounce;
1263 int is_write;
1264 BlockDriverAIOCB *aiocb;
1265 BlockDriverAIOCB *this_aiocb;
1266 } VectorTranslationState;
1268 static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
1270 VectorTranslationState *s = opaque;
1272 if (!s->is_write) {
1273 qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
1275 qemu_free(s->bounce);
1276 s->this_aiocb->cb(s->this_aiocb->opaque, ret);
1277 qemu_aio_release(s->this_aiocb);
1280 static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1281 int64_t sector_num,
1282 QEMUIOVector *iov,
1283 int nb_sectors,
1284 BlockDriverCompletionFunc *cb,
1285 void *opaque,
1286 int is_write)
1289 VectorTranslationState *s = qemu_mallocz(sizeof(*s));
1290 BlockDriverAIOCB *aiocb = qemu_aio_get(bs, cb, opaque);
1292 s->this_aiocb = aiocb;
1293 s->iov = iov;
1294 s->bounce = qemu_memalign(512, nb_sectors * 512);
1295 s->is_write = is_write;
1296 if (is_write) {
1297 qemu_iovec_to_buffer(s->iov, s->bounce);
1298 s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
1299 bdrv_aio_rw_vector_cb, s);
1300 } else {
1301 s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors,
1302 bdrv_aio_rw_vector_cb, s);
1304 return aiocb;
1307 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1308 QEMUIOVector *iov, int nb_sectors,
1309 BlockDriverCompletionFunc *cb, void *opaque)
1311 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1312 cb, opaque, 0);
1315 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1316 QEMUIOVector *iov, int nb_sectors,
1317 BlockDriverCompletionFunc *cb, void *opaque)
1319 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1320 cb, opaque, 1);
1323 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1324 uint8_t *buf, int nb_sectors,
1325 BlockDriverCompletionFunc *cb, void *opaque)
1327 BlockDriver *drv = bs->drv;
1328 BlockDriverAIOCB *ret;
1330 if (!drv)
1331 return NULL;
1333 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1335 if (ret) {
1336 /* Update stats even though technically transfer has not happened. */
1337 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1338 bs->rd_ops ++;
1341 return ret;
1344 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1345 const uint8_t *buf, int nb_sectors,
1346 BlockDriverCompletionFunc *cb, void *opaque)
1348 BlockDriver *drv = bs->drv;
1349 BlockDriverAIOCB *ret;
1351 if (!drv)
1352 return NULL;
1353 if (bs->read_only)
1354 return NULL;
1356 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1358 if (ret) {
1359 /* Update stats even though technically transfer has not happened. */
1360 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1361 bs->wr_ops ++;
1364 return ret;
1367 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1369 BlockDriver *drv = acb->bs->drv;
1371 if (acb->cb == bdrv_aio_rw_vector_cb) {
1372 VectorTranslationState *s = acb->opaque;
1373 acb = s->aiocb;
1376 drv->bdrv_aio_cancel(acb);
1380 /**************************************************************/
1381 /* async block device emulation */
1383 static void bdrv_aio_bh_cb(void *opaque)
1385 BlockDriverAIOCBSync *acb = opaque;
1386 acb->common.cb(acb->common.opaque, acb->ret);
1387 qemu_aio_release(acb);
1390 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1391 int64_t sector_num, uint8_t *buf, int nb_sectors,
1392 BlockDriverCompletionFunc *cb, void *opaque)
1394 BlockDriverAIOCBSync *acb;
1395 int ret;
1397 acb = qemu_aio_get(bs, cb, opaque);
1398 if (!acb->bh)
1399 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1400 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1401 acb->ret = ret;
1402 qemu_bh_schedule(acb->bh);
1403 return &acb->common;
1406 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1407 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1408 BlockDriverCompletionFunc *cb, void *opaque)
1410 BlockDriverAIOCBSync *acb;
1411 int ret;
1413 acb = qemu_aio_get(bs, cb, opaque);
1414 if (!acb->bh)
1415 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1416 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1417 acb->ret = ret;
1418 qemu_bh_schedule(acb->bh);
1419 return &acb->common;
1422 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1424 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1425 qemu_bh_cancel(acb->bh);
1426 qemu_aio_release(acb);
1429 /**************************************************************/
1430 /* sync block device emulation */
1432 static void bdrv_rw_em_cb(void *opaque, int ret)
1434 *(int *)opaque = ret;
1437 #define NOT_DONE 0x7fffffff
1439 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1440 uint8_t *buf, int nb_sectors)
1442 int async_ret;
1443 BlockDriverAIOCB *acb;
1445 async_ret = NOT_DONE;
1446 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1447 bdrv_rw_em_cb, &async_ret);
1448 if (acb == NULL)
1449 return -1;
1451 while (async_ret == NOT_DONE) {
1452 qemu_aio_wait();
1455 return async_ret;
1458 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1459 const uint8_t *buf, int nb_sectors)
1461 int async_ret;
1462 BlockDriverAIOCB *acb;
1464 async_ret = NOT_DONE;
1465 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1466 bdrv_rw_em_cb, &async_ret);
1467 if (acb == NULL)
1468 return -1;
1469 while (async_ret == NOT_DONE) {
1470 qemu_aio_wait();
1472 return async_ret;
1475 void bdrv_init(void)
1477 bdrv_register(&bdrv_raw);
1478 bdrv_register(&bdrv_host_device);
1479 #ifndef _WIN32
1480 bdrv_register(&bdrv_cow);
1481 #endif
1482 bdrv_register(&bdrv_qcow);
1483 bdrv_register(&bdrv_vmdk);
1484 bdrv_register(&bdrv_cloop);
1485 bdrv_register(&bdrv_dmg);
1486 bdrv_register(&bdrv_bochs);
1487 bdrv_register(&bdrv_vpc);
1488 bdrv_register(&bdrv_vvfat);
1489 bdrv_register(&bdrv_qcow2);
1490 bdrv_register(&bdrv_parallels);
1491 bdrv_register(&bdrv_nbd);
1494 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1495 void *opaque)
1497 BlockDriver *drv;
1498 BlockDriverAIOCB *acb;
1500 drv = bs->drv;
1501 if (drv->free_aiocb) {
1502 acb = drv->free_aiocb;
1503 drv->free_aiocb = acb->next;
1504 } else {
1505 acb = qemu_mallocz(drv->aiocb_size);
1507 acb->bs = bs;
1508 acb->cb = cb;
1509 acb->opaque = opaque;
1510 return acb;
1513 void qemu_aio_release(void *p)
1515 BlockDriverAIOCB *acb = p;
1516 BlockDriver *drv = acb->bs->drv;
1517 acb->next = drv->free_aiocb;
1518 drv->free_aiocb = acb;
1521 /**************************************************************/
1522 /* removable device support */
1525 * Return TRUE if the media is present
1527 int bdrv_is_inserted(BlockDriverState *bs)
1529 BlockDriver *drv = bs->drv;
1530 int ret;
1531 if (!drv)
1532 return 0;
1533 if (!drv->bdrv_is_inserted)
1534 return 1;
1535 ret = drv->bdrv_is_inserted(bs);
1536 return ret;
1540 * Return TRUE if the media changed since the last call to this
1541 * function. It is currently only used for floppy disks
1543 int bdrv_media_changed(BlockDriverState *bs)
1545 BlockDriver *drv = bs->drv;
1546 int ret;
1548 if (!drv || !drv->bdrv_media_changed)
1549 ret = -ENOTSUP;
1550 else
1551 ret = drv->bdrv_media_changed(bs);
1552 if (ret == -ENOTSUP)
1553 ret = bs->media_changed;
1554 bs->media_changed = 0;
1555 return ret;
1559 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1561 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1563 BlockDriver *drv = bs->drv;
1564 int ret;
1566 if (!drv || !drv->bdrv_eject) {
1567 ret = -ENOTSUP;
1568 } else {
1569 ret = drv->bdrv_eject(bs, eject_flag);
1571 if (ret == -ENOTSUP) {
1572 if (eject_flag)
1573 bdrv_close(bs);
1577 int bdrv_is_locked(BlockDriverState *bs)
1579 return bs->locked;
1583 * Lock or unlock the media (if it is locked, the user won't be able
1584 * to eject it manually).
1586 void bdrv_set_locked(BlockDriverState *bs, int locked)
1588 BlockDriver *drv = bs->drv;
1590 bs->locked = locked;
1591 if (drv && drv->bdrv_set_locked) {
1592 drv->bdrv_set_locked(bs, locked);
1596 /* needed for generic scsi interface */
1598 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1600 BlockDriver *drv = bs->drv;
1602 if (drv && drv->bdrv_ioctl)
1603 return drv->bdrv_ioctl(bs, req, buf);
1604 return -ENOTSUP;