]>
git.proxmox.com Git - qemu.git/blob - block.c
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
25 #include "block_int.h"
28 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #include <sys/queue.h>
35 static BlockDriverState
*bdrv_first
;
36 static BlockDriver
*first_drv
;
38 void bdrv_register(BlockDriver
*bdrv
)
40 bdrv
->next
= first_drv
;
44 /* create a new block device (by default it is empty) */
45 BlockDriverState
*bdrv_new(const char *device_name
)
47 BlockDriverState
**pbs
, *bs
;
49 bs
= qemu_mallocz(sizeof(BlockDriverState
));
52 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
53 if (device_name
[0] != '\0') {
54 /* insert at the end */
63 BlockDriver
*bdrv_find_format(const char *format_name
)
66 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
67 if (!strcmp(drv1
->format_name
, format_name
))
73 int bdrv_create(BlockDriver
*drv
,
74 const char *filename
, int64_t size_in_sectors
,
75 const char *backing_file
, int flags
)
77 if (!drv
->bdrv_create
)
79 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
83 static void get_tmp_filename(char *filename
, int size
)
85 /* XXX: find a better function */
89 static void get_tmp_filename(char *filename
, int size
)
92 /* XXX: race condition possible */
93 pstrcpy(filename
, size
, "/tmp/vl.XXXXXX");
94 fd
= mkstemp(filename
);
99 /* XXX: force raw format if block or character device ? It would
100 simplify the BSD case */
101 static BlockDriver
*find_image_format(const char *filename
)
103 int fd
, ret
, score
, score_max
;
104 BlockDriver
*drv1
, *drv
;
106 size_t bufsize
= 1024;
108 fd
= open(filename
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
111 #ifdef DIOCGSECTORSIZE
113 unsigned int sectorsize
= 512;
114 if (!ioctl(fd
, DIOCGSECTORSIZE
, §orsize
) &&
115 sectorsize
> bufsize
)
116 bufsize
= sectorsize
;
119 buf
= malloc(bufsize
);
122 ret
= read(fd
, buf
, bufsize
);
132 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
133 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
134 if (score
> score_max
) {
143 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int snapshot
)
145 return bdrv_open2(bs
, filename
, snapshot
, NULL
);
148 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int snapshot
,
152 char tmp_filename
[1024];
155 bs
->is_temporary
= 0;
159 BlockDriverState
*bs1
;
162 /* if snapshot, we create a temporary backing file and open it
163 instead of opening 'filename' directly */
165 /* if there is a backing file, use it */
170 if (bdrv_open(bs1
, filename
, 0) < 0) {
174 total_size
= bs1
->total_sectors
;
177 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
178 /* XXX: use cow for linux as it is more efficient ? */
179 if (bdrv_create(&bdrv_qcow
, tmp_filename
,
180 total_size
, filename
, 0) < 0) {
183 filename
= tmp_filename
;
184 bs
->is_temporary
= 1;
187 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
189 drv
= find_image_format(filename
);
194 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
195 if (bs
->opaque
== NULL
&& drv
->instance_size
> 0)
198 ret
= drv
->bdrv_open(bs
, filename
);
200 qemu_free(bs
->opaque
);
204 if (bs
->is_temporary
) {
208 if (bs
->backing_file
[0] != '\0' && drv
->bdrv_is_allocated
) {
209 /* if there is a backing file, use it */
210 bs
->backing_hd
= bdrv_new("");
211 if (!bs
->backing_hd
) {
216 if (bdrv_open(bs
->backing_hd
, bs
->backing_file
, 0) < 0)
222 /* call the change callback */
224 bs
->change_cb(bs
->change_opaque
);
229 void bdrv_close(BlockDriverState
*bs
)
233 bdrv_delete(bs
->backing_hd
);
234 bs
->drv
->bdrv_close(bs
);
235 qemu_free(bs
->opaque
);
237 if (bs
->is_temporary
) {
238 unlink(bs
->filename
);
245 /* call the change callback */
247 bs
->change_cb(bs
->change_opaque
);
251 void bdrv_delete(BlockDriverState
*bs
)
253 /* XXX: remove the driver list */
258 /* commit COW file into the raw image */
259 int bdrv_commit(BlockDriverState
*bs
)
263 unsigned char sector
[512];
272 if (!bs
->backing_hd
) {
276 for (i
= 0; i
< bs
->total_sectors
;) {
277 if (bs
->drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
278 for(j
= 0; j
< n
; j
++) {
279 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
283 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
295 /* return -1 if error */
296 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
297 uint8_t *buf
, int nb_sectors
)
300 BlockDriver
*drv
= bs
->drv
;
305 while (nb_sectors
> 0) {
306 if (sector_num
== 0 && bs
->boot_sector_enabled
) {
307 memcpy(buf
, bs
->boot_sector_data
, 512);
309 } else if (bs
->backing_hd
) {
310 if (drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, &n
)) {
311 ret
= drv
->bdrv_read(bs
, sector_num
, buf
, n
);
315 /* read from the base image */
316 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n
);
321 ret
= drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
324 /* no need to loop */
334 /* return -1 if error */
335 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
336 const uint8_t *buf
, int nb_sectors
)
342 return bs
->drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
345 void bdrv_get_geometry(BlockDriverState
*bs
, int64_t *nb_sectors_ptr
)
347 *nb_sectors_ptr
= bs
->total_sectors
;
350 /* force a given boot sector. */
351 void bdrv_set_boot_sector(BlockDriverState
*bs
, const uint8_t *data
, int size
)
353 bs
->boot_sector_enabled
= 1;
356 memcpy(bs
->boot_sector_data
, data
, size
);
357 memset(bs
->boot_sector_data
+ size
, 0, 512 - size
);
360 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
361 int cyls
, int heads
, int secs
)
368 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
371 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
372 type
== BDRV_TYPE_FLOPPY
));
375 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
377 bs
->translation
= translation
;
380 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
381 int *pcyls
, int *pheads
, int *psecs
)
388 int bdrv_get_type_hint(BlockDriverState
*bs
)
393 int bdrv_get_translation_hint(BlockDriverState
*bs
)
395 return bs
->translation
;
398 int bdrv_is_removable(BlockDriverState
*bs
)
400 return bs
->removable
;
403 int bdrv_is_read_only(BlockDriverState
*bs
)
405 return bs
->read_only
;
408 int bdrv_is_inserted(BlockDriverState
*bs
)
413 int bdrv_is_locked(BlockDriverState
*bs
)
418 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
423 void bdrv_set_change_cb(BlockDriverState
*bs
,
424 void (*change_cb
)(void *opaque
), void *opaque
)
426 bs
->change_cb
= change_cb
;
427 bs
->change_opaque
= opaque
;
430 int bdrv_is_encrypted(BlockDriverState
*bs
)
432 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
434 return bs
->encrypted
;
437 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
440 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
441 ret
= bdrv_set_key(bs
->backing_hd
, key
);
447 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
449 return bs
->drv
->bdrv_set_key(bs
, key
);
452 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
454 if (!bs
->inserted
|| !bs
->drv
) {
457 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
461 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
466 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
467 it(opaque
, drv
->format_name
);
471 BlockDriverState
*bdrv_find(const char *name
)
473 BlockDriverState
*bs
;
475 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
476 if (!strcmp(name
, bs
->device_name
))
482 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
484 BlockDriverState
*bs
;
486 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
487 it(opaque
, bs
->device_name
);
491 const char *bdrv_get_device_name(BlockDriverState
*bs
)
493 return bs
->device_name
;
498 BlockDriverState
*bs
;
500 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
501 term_printf("%s:", bs
->device_name
);
502 term_printf(" type=");
507 case BDRV_TYPE_CDROM
:
508 term_printf("cdrom");
510 case BDRV_TYPE_FLOPPY
:
511 term_printf("floppy");
514 term_printf(" removable=%d", bs
->removable
);
516 term_printf(" locked=%d", bs
->locked
);
519 term_printf(" file=%s", bs
->filename
);
520 if (bs
->backing_file
[0] != '\0')
521 term_printf(" backing_file=%s", bs
->backing_file
);
522 term_printf(" ro=%d", bs
->read_only
);
523 term_printf(" drv=%s", bs
->drv
->format_name
);
525 term_printf(" encrypted");
527 term_printf(" [not inserted]");
534 /**************************************************************/
535 /* RAW block driver */
537 typedef struct BDRVRawState
{
541 static int raw_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
543 return 1; /* maybe */
546 static int raw_open(BlockDriverState
*bs
, const char *filename
)
548 BDRVRawState
*s
= bs
->opaque
;
555 fd
= open(filename
, O_RDWR
| O_BINARY
| O_LARGEFILE
);
557 fd
= open(filename
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
563 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
564 #ifdef DIOCGMEDIASIZE
565 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
567 size
= lseek(fd
, 0LL, SEEK_END
);
571 size
= lseek(fd
, 0, SEEK_END
);
574 /* On Windows hosts it can happen that we're unable to get file size
575 for CD-ROM raw device (it's inherent limitation of the CDFS driver). */
577 size
= LONG_LONG_MAX
;
579 bs
->total_sectors
= size
/ 512;
584 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
585 uint8_t *buf
, int nb_sectors
)
587 BDRVRawState
*s
= bs
->opaque
;
590 lseek(s
->fd
, sector_num
* 512, SEEK_SET
);
591 ret
= read(s
->fd
, buf
, nb_sectors
* 512);
592 if (ret
!= nb_sectors
* 512)
597 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
598 const uint8_t *buf
, int nb_sectors
)
600 BDRVRawState
*s
= bs
->opaque
;
603 lseek(s
->fd
, sector_num
* 512, SEEK_SET
);
604 ret
= write(s
->fd
, buf
, nb_sectors
* 512);
605 if (ret
!= nb_sectors
* 512)
610 static void raw_close(BlockDriverState
*bs
)
612 BDRVRawState
*s
= bs
->opaque
;
616 static int raw_create(const char *filename
, int64_t total_size
,
617 const char *backing_file
, int flags
)
621 if (flags
|| backing_file
)
624 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
628 ftruncate(fd
, total_size
* 512);
633 BlockDriver bdrv_raw
= {
635 sizeof(BDRVRawState
),
646 bdrv_register(&bdrv_raw
);
648 bdrv_register(&bdrv_cow
);
650 bdrv_register(&bdrv_qcow
);
651 bdrv_register(&bdrv_vmdk
);
652 bdrv_register(&bdrv_cloop
);
653 bdrv_register(&bdrv_dmg
);
654 bdrv_register(&bdrv_bochs
);
655 bdrv_register(&bdrv_vpc
);