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"
27 static BlockDriverState *bdrv_first;
28 static BlockDriver *first_drv;
30 void bdrv_register(BlockDriver *bdrv)
32 bdrv->next = first_drv;
36 /* create a new block device (by default it is empty) */
37 BlockDriverState *bdrv_new(const char *device_name)
39 BlockDriverState **pbs, *bs;
41 bs = qemu_mallocz(sizeof(BlockDriverState));
44 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
45 if (device_name[0] != '\0') {
46 /* insert at the end */
55 BlockDriver *bdrv_find_format(const char *format_name)
58 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
59 if (!strcmp(drv1->format_name, format_name))
65 int bdrv_create(BlockDriver *drv,
66 const char *filename, int64_t size_in_sectors,
67 const char *backing_file, int flags)
69 if (!drv->bdrv_create)
71 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
74 /* XXX: race condition possible */
75 static void get_tmp_filename(char *filename, int size)
78 pstrcpy(filename, size, "/tmp/vl.XXXXXX");
79 fd = mkstemp(filename);
83 static BlockDriver *find_image_format(const char *filename)
85 int fd, ret, score, score_max;
86 BlockDriver *drv1, *drv;
89 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
92 ret = read(fd, buf, sizeof(buf));
101 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
102 score = drv1->bdrv_probe(buf, ret, filename);
103 if (score > score_max) {
111 int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot)
113 return bdrv_open2(bs, filename, snapshot, NULL);
116 int bdrv_open2(BlockDriverState *bs, const char *filename, int snapshot,
120 char tmp_filename[1024];
123 bs->is_temporary = 0;
127 BlockDriverState *bs1;
130 /* if snapshot, we create a temporary backing file and open it
131 instead of opening 'filename' directly */
133 /* if there is a backing file, use it */
138 if (bdrv_open(bs1, filename, 0) < 0) {
142 total_size = bs1->total_sectors;
145 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
146 /* XXX: use cow for linux as it is more efficient ? */
147 if (bdrv_create(&bdrv_qcow, tmp_filename,
148 total_size, filename, 0) < 0) {
151 filename = tmp_filename;
152 bs->is_temporary = 1;
155 pstrcpy(bs->filename, sizeof(bs->filename), filename);
157 drv = find_image_format(filename);
162 bs->opaque = qemu_mallocz(drv->instance_size);
163 if (bs->opaque == NULL && drv->instance_size > 0)
166 ret = drv->bdrv_open(bs, filename);
168 qemu_free(bs->opaque);
172 if (bs->is_temporary) {
176 if (bs->backing_file[0] != '\0' && drv->bdrv_is_allocated) {
177 /* if there is a backing file, use it */
178 bs->backing_hd = bdrv_new("");
179 if (!bs->backing_hd) {
184 if (bdrv_open(bs->backing_hd, bs->backing_file, 0) < 0)
190 /* call the change callback */
192 bs->change_cb(bs->change_opaque);
197 void bdrv_close(BlockDriverState *bs)
201 bdrv_delete(bs->backing_hd);
202 bs->drv->bdrv_close(bs);
203 qemu_free(bs->opaque);
205 if (bs->is_temporary) {
206 unlink(bs->filename);
213 /* call the change callback */
215 bs->change_cb(bs->change_opaque);
219 void bdrv_delete(BlockDriverState *bs)
221 /* XXX: remove the driver list */
226 /* commit COW file into the raw image */
227 int bdrv_commit(BlockDriverState *bs)
231 unsigned char sector[512];
240 if (!bs->backing_hd) {
244 for (i = 0; i < bs->total_sectors;) {
245 if (bs->drv->bdrv_is_allocated(bs, i, 65536, &n)) {
246 for(j = 0; j < n; j++) {
247 if (bdrv_read(bs, i, sector, 1) != 0) {
251 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
263 /* return -1 if error */
264 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
265 uint8_t *buf, int nb_sectors)
268 BlockDriver *drv = bs->drv;
273 while (nb_sectors > 0) {
274 if (sector_num == 0 && bs->boot_sector_enabled) {
275 memcpy(buf, bs->boot_sector_data, 512);
277 } else if (bs->backing_hd) {
278 if (drv->bdrv_is_allocated(bs, sector_num, nb_sectors, &n)) {
279 ret = drv->bdrv_read(bs, sector_num, buf, n);
283 /* read from the base image */
284 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
289 ret = drv->bdrv_read(bs, sector_num, buf, nb_sectors);
292 /* no need to loop */
302 /* return -1 if error */
303 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
304 const uint8_t *buf, int nb_sectors)
310 return bs->drv->bdrv_write(bs, sector_num, buf, nb_sectors);
313 void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
315 *nb_sectors_ptr = bs->total_sectors;
318 /* force a given boot sector. */
319 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
321 bs->boot_sector_enabled = 1;
324 memcpy(bs->boot_sector_data, data, size);
325 memset(bs->boot_sector_data + size, 0, 512 - size);
328 void bdrv_set_geometry_hint(BlockDriverState *bs,
329 int cyls, int heads, int secs)
336 void bdrv_set_type_hint(BlockDriverState *bs, int type)
339 bs->removable = ((type == BDRV_TYPE_CDROM ||
340 type == BDRV_TYPE_FLOPPY));
343 void bdrv_get_geometry_hint(BlockDriverState *bs,
344 int *pcyls, int *pheads, int *psecs)
351 int bdrv_get_type_hint(BlockDriverState *bs)
356 int bdrv_is_removable(BlockDriverState *bs)
358 return bs->removable;
361 int bdrv_is_read_only(BlockDriverState *bs)
363 return bs->read_only;
366 int bdrv_is_inserted(BlockDriverState *bs)
371 int bdrv_is_locked(BlockDriverState *bs)
376 void bdrv_set_locked(BlockDriverState *bs, int locked)
381 void bdrv_set_change_cb(BlockDriverState *bs,
382 void (*change_cb)(void *opaque), void *opaque)
384 bs->change_cb = change_cb;
385 bs->change_opaque = opaque;
388 int bdrv_is_encrypted(BlockDriverState *bs)
390 if (bs->backing_hd && bs->backing_hd->encrypted)
392 return bs->encrypted;
395 int bdrv_set_key(BlockDriverState *bs, const char *key)
398 if (bs->backing_hd && bs->backing_hd->encrypted) {
399 ret = bdrv_set_key(bs->backing_hd, key);
405 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
407 return bs->drv->bdrv_set_key(bs, key);
410 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
412 if (!bs->inserted || !bs->drv) {
415 pstrcpy(buf, buf_size, bs->drv->format_name);
419 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
424 for (drv = first_drv; drv != NULL; drv = drv->next) {
425 it(opaque, drv->format_name);
429 BlockDriverState *bdrv_find(const char *name)
431 BlockDriverState *bs;
433 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
434 if (!strcmp(name, bs->device_name))
440 void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
442 BlockDriverState *bs;
444 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
445 it(opaque, bs->device_name);
449 const char *bdrv_get_device_name(BlockDriverState *bs)
451 return bs->device_name;
456 BlockDriverState *bs;
458 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
459 term_printf("%s:", bs->device_name);
460 term_printf(" type=");
465 case BDRV_TYPE_CDROM:
466 term_printf("cdrom");
468 case BDRV_TYPE_FLOPPY:
469 term_printf("floppy");
472 term_printf(" removable=%d", bs->removable);
474 term_printf(" locked=%d", bs->locked);
477 term_printf(" file=%s", bs->filename);
478 if (bs->backing_file[0] != '\0')
479 term_printf(" backing_file=%s", bs->backing_file);
480 term_printf(" ro=%d", bs->read_only);
481 term_printf(" drv=%s", bs->drv->format_name);
483 term_printf(" encrypted");
485 term_printf(" [not inserted]");
492 /**************************************************************/
493 /* RAW block driver */
495 typedef struct BDRVRawState {
499 static int raw_probe(const uint8_t *buf, int buf_size, const char *filename)
501 return 1; /* maybe */
504 static int raw_open(BlockDriverState *bs, const char *filename)
506 BDRVRawState *s = bs->opaque;
510 fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
512 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
517 size = lseek64(fd, 0, SEEK_END);
518 bs->total_sectors = size / 512;
523 static int raw_read(BlockDriverState *bs, int64_t sector_num,
524 uint8_t *buf, int nb_sectors)
526 BDRVRawState *s = bs->opaque;
529 lseek64(s->fd, sector_num * 512, SEEK_SET);
530 ret = read(s->fd, buf, nb_sectors * 512);
531 if (ret != nb_sectors * 512)
536 static int raw_write(BlockDriverState *bs, int64_t sector_num,
537 const uint8_t *buf, int nb_sectors)
539 BDRVRawState *s = bs->opaque;
542 lseek64(s->fd, sector_num * 512, SEEK_SET);
543 ret = write(s->fd, buf, nb_sectors * 512);
544 if (ret != nb_sectors * 512)
549 static int raw_close(BlockDriverState *bs)
551 BDRVRawState *s = bs->opaque;
555 static int raw_create(const char *filename, int64_t total_size,
556 const char *backing_file, int flags)
560 if (flags || backing_file)
563 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
567 ftruncate64(fd, total_size * 512);
572 BlockDriver bdrv_raw = {
574 sizeof(BDRVRawState),
585 bdrv_register(&bdrv_raw);
587 bdrv_register(&bdrv_cow);
589 bdrv_register(&bdrv_qcow);
590 bdrv_register(&bdrv_vmdk);