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
32 struct BlockDriverState {
33 int fd; /* if -1, only COW mappings */
34 int64_t total_sectors;
35 int read_only; /* if true, the media is read only */
36 int inserted; /* if true, the media is present */
37 int removable; /* if true, the media can be removed */
38 int locked; /* if true, the media cannot temporarily be ejected */
39 /* event callback when inserting/removing */
40 void (*change_cb)(void *opaque);
43 uint8_t *cow_bitmap; /* if non NULL, COW mappings are used first */
44 uint8_t *cow_bitmap_addr; /* mmap address of cow_bitmap */
47 int64_t cow_sectors_offset;
48 int boot_sector_enabled;
49 uint8_t boot_sector_data[512];
53 /* NOTE: the following infos are only hints for real hardware
54 drivers. They are not used by the block driver */
55 int cyls, heads, secs;
58 BlockDriverState *next;
61 static BlockDriverState *bdrv_first;
63 /* create a new block device (by default it is empty) */
64 BlockDriverState *bdrv_new(const char *device_name)
66 BlockDriverState **pbs, *bs;
68 bs = qemu_mallocz(sizeof(BlockDriverState));
71 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
72 /* insert at the end */
80 int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot)
84 struct cow_header_v2 cow_header;
86 char template[] = "/tmp/vl.XXXXXX";
94 bs->cow_bitmap = NULL;
95 pstrcpy(bs->filename, sizeof(bs->filename), filename);
97 /* open standard HD image */
99 fd = open(filename, O_RDWR | O_BINARY);
101 fd = open(filename, O_RDWR | O_LARGEFILE);
104 /* read only image on disk */
106 fd = open(filename, O_RDONLY | O_BINARY);
108 fd = open(filename, O_RDONLY | O_LARGEFILE);
119 /* see if it is a cow image */
120 if (read(fd, &cow_header, sizeof(cow_header)) != sizeof(cow_header)) {
121 fprintf(stderr, "%s: could not read header\n", filename);
125 if (be32_to_cpu(cow_header.magic) == COW_MAGIC &&
126 be32_to_cpu(cow_header.version) == COW_VERSION) {
127 /* cow image found */
128 size = cow_header.size;
129 #ifndef WORDS_BIGENDIAN
130 size = bswap64(size);
132 bs->total_sectors = size / 512;
136 if (cow_header.backing_file[0] != '\0') {
137 if (stat(cow_header.backing_file, &st) != 0) {
138 fprintf(stderr, "%s: could not find original disk image '%s'\n", filename, cow_header.backing_file);
141 if (st.st_mtime != be32_to_cpu(cow_header.mtime)) {
142 fprintf(stderr, "%s: original raw disk image '%s' does not match saved timestamp\n", filename, cow_header.backing_file);
145 fd = open(cow_header.backing_file, O_RDONLY | O_LARGEFILE);
150 /* mmap the bitmap */
151 bs->cow_bitmap_size = ((bs->total_sectors + 7) >> 3) + sizeof(cow_header);
152 bs->cow_bitmap_addr = mmap(get_mmap_addr(bs->cow_bitmap_size),
154 PROT_READ | PROT_WRITE,
155 MAP_SHARED, bs->cow_fd, 0);
156 if (bs->cow_bitmap_addr == MAP_FAILED)
158 bs->cow_bitmap = bs->cow_bitmap_addr + sizeof(cow_header);
159 bs->cow_sectors_offset = (bs->cow_bitmap_size + 511) & ~511;
164 /* standard raw image */
165 size = lseek64(fd, 0, SEEK_END);
166 bs->total_sectors = size / 512;
172 /* create a temporary COW file */
173 cow_fd = mkstemp64(template);
179 /* just need to allocate bitmap */
180 bs->cow_bitmap_size = (bs->total_sectors + 7) >> 3;
181 bs->cow_bitmap_addr = mmap(get_mmap_addr(bs->cow_bitmap_size),
183 PROT_READ | PROT_WRITE,
184 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
185 if (bs->cow_bitmap_addr == MAP_FAILED)
187 bs->cow_bitmap = bs->cow_bitmap_addr;
188 bs->cow_sectors_offset = 0;
194 /* call the change callback */
196 bs->change_cb(bs->change_opaque);
204 void bdrv_close(BlockDriverState *bs)
208 /* we unmap the mapping so that it is written to the COW file */
209 if (bs->cow_bitmap_addr)
210 munmap(bs->cow_bitmap_addr, bs->cow_bitmap_size);
218 /* call the change callback */
220 bs->change_cb(bs->change_opaque);
224 void bdrv_delete(BlockDriverState *bs)
230 static inline void set_bit(uint8_t *bitmap, int64_t bitnum)
232 bitmap[bitnum / 8] |= (1 << (bitnum%8));
235 static inline int is_bit_set(const uint8_t *bitmap, int64_t bitnum)
237 return !!(bitmap[bitnum / 8] & (1 << (bitnum%8)));
241 /* Return true if first block has been changed (ie. current version is
242 * in COW file). Set the number of continuous blocks for which that
244 static int is_changed(uint8_t *bitmap,
245 int64_t sector_num, int nb_sectors,
250 if (!bitmap || nb_sectors == 0) {
251 *num_same = nb_sectors;
255 changed = is_bit_set(bitmap, sector_num);
256 for (*num_same = 1; *num_same < nb_sectors; (*num_same)++) {
257 if (is_bit_set(bitmap, sector_num + *num_same) != changed)
264 /* commit COW file into the raw image */
265 int bdrv_commit(BlockDriverState *bs)
273 if (!bs->cow_bitmap) {
274 fprintf(stderr, "Already committed to %s\n", bs->filename);
279 fprintf(stderr, "Can't commit to %s: read-only\n", bs->filename);
283 cow_bitmap = bs->cow_bitmap;
284 for (i = 0; i < bs->total_sectors; i++) {
285 if (is_bit_set(cow_bitmap, i)) {
286 unsigned char sector[512];
287 if (bdrv_read(bs, i, sector, 1) != 0) {
288 fprintf(stderr, "Error reading sector %lli: aborting commit\n",
293 /* Make bdrv_write write to real file for a moment. */
294 bs->cow_bitmap = NULL;
295 if (bdrv_write(bs, i, sector, 1) != 0) {
296 fprintf(stderr, "Error writing sector %lli: aborting commit\n",
298 bs->cow_bitmap = cow_bitmap;
301 bs->cow_bitmap = cow_bitmap;
304 fprintf(stderr, "Committed snapshot to %s\n", bs->filename);
308 /* return -1 if error */
309 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
310 uint8_t *buf, int nb_sectors)
318 while (nb_sectors > 0) {
319 if (is_changed(bs->cow_bitmap, sector_num, nb_sectors, &n)) {
321 offset = bs->cow_sectors_offset;
322 } else if (sector_num == 0 && bs->boot_sector_enabled) {
323 memcpy(buf, bs->boot_sector_data, 512);
332 /* no file, just return empty sectors */
333 memset(buf, 0, n * 512);
335 offset += sector_num * 512;
336 lseek64(fd, offset, SEEK_SET);
337 ret = read(fd, buf, n * 512);
338 if (ret != n * 512) {
350 /* return -1 if error */
351 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
352 const uint8_t *buf, int nb_sectors)
355 int64_t offset, retl;
362 if (bs->cow_bitmap) {
364 offset = bs->cow_sectors_offset;
370 offset += sector_num * 512;
371 retl = lseek64(fd, offset, SEEK_SET);
375 ret = write(fd, buf, nb_sectors * 512);
376 if (ret != nb_sectors * 512) {
380 if (bs->cow_bitmap) {
381 for (i = 0; i < nb_sectors; i++)
382 set_bit(bs->cow_bitmap, sector_num + i);
387 void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
389 *nb_sectors_ptr = bs->total_sectors;
392 /* force a given boot sector. */
393 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
395 bs->boot_sector_enabled = 1;
398 memcpy(bs->boot_sector_data, data, size);
399 memset(bs->boot_sector_data + size, 0, 512 - size);
402 void bdrv_set_geometry_hint(BlockDriverState *bs,
403 int cyls, int heads, int secs)
410 void bdrv_set_type_hint(BlockDriverState *bs, int type)
413 bs->removable = ((type == BDRV_TYPE_CDROM ||
414 type == BDRV_TYPE_FLOPPY));
417 void bdrv_get_geometry_hint(BlockDriverState *bs,
418 int *pcyls, int *pheads, int *psecs)
425 int bdrv_get_type_hint(BlockDriverState *bs)
430 int bdrv_is_removable(BlockDriverState *bs)
432 return bs->removable;
435 int bdrv_is_read_only(BlockDriverState *bs)
437 return bs->read_only;
440 int bdrv_is_inserted(BlockDriverState *bs)
445 int bdrv_is_locked(BlockDriverState *bs)
450 void bdrv_set_locked(BlockDriverState *bs, int locked)
455 void bdrv_set_change_cb(BlockDriverState *bs,
456 void (*change_cb)(void *opaque), void *opaque)
458 bs->change_cb = change_cb;
459 bs->change_opaque = opaque;
462 BlockDriverState *bdrv_find(const char *name)
464 BlockDriverState *bs;
466 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
467 if (!strcmp(name, bs->device_name))
473 void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
475 BlockDriverState *bs;
477 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
478 it(opaque, bs->device_name);
484 BlockDriverState *bs;
486 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
487 term_printf("%s:", bs->device_name);
488 term_printf(" type=");
493 case BDRV_TYPE_CDROM:
494 term_printf("cdrom");
496 case BDRV_TYPE_FLOPPY:
497 term_printf("floppy");
500 term_printf(" removable=%d", bs->removable);
502 term_printf(" locked=%d", bs->locked);
505 term_printf(" file=%s", bs->filename);
506 term_printf(" ro=%d", bs->read_only);
508 term_printf(" [not inserted]");