bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU System Emulator block driver |
| 3 | * |
| 4 | * Copyright (c) 2003 Fabrice Bellard |
| 5 | * |
| 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: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 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. |
| 23 | */ |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 24 | #include "vl.h" |
| 25 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 26 | #ifndef _WIN32 |
| 27 | #include <sys/mman.h> |
| 28 | #endif |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 29 | |
bellard | 00af2b2 | 2004-02-26 00:20:56 +0000 | [diff] [blame] | 30 | #include "cow.h" |
| 31 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 32 | struct BlockDriverState { |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 33 | int fd; /* if -1, only COW mappings */ |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 34 | int64_t total_sectors; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 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); |
| 41 | void *change_opaque; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 42 | |
| 43 | uint8_t *cow_bitmap; /* if non NULL, COW mappings are used first */ |
| 44 | uint8_t *cow_bitmap_addr; /* mmap address of cow_bitmap */ |
| 45 | int cow_bitmap_size; |
| 46 | int cow_fd; |
| 47 | int64_t cow_sectors_offset; |
bellard | cf98951 | 2004-02-16 21:56:36 +0000 | [diff] [blame] | 48 | int boot_sector_enabled; |
| 49 | uint8_t boot_sector_data[512]; |
| 50 | |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 51 | char filename[1024]; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 52 | |
| 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; |
| 56 | int type; |
| 57 | char device_name[32]; |
| 58 | BlockDriverState *next; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 61 | static BlockDriverState *bdrv_first; |
| 62 | |
| 63 | /* create a new block device (by default it is empty) */ |
| 64 | BlockDriverState *bdrv_new(const char *device_name) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 65 | { |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 66 | BlockDriverState **pbs, *bs; |
| 67 | |
| 68 | bs = qemu_mallocz(sizeof(BlockDriverState)); |
| 69 | if(!bs) |
| 70 | return NULL; |
| 71 | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); |
| 72 | /* insert at the end */ |
| 73 | pbs = &bdrv_first; |
| 74 | while (*pbs != NULL) |
| 75 | pbs = &(*pbs)->next; |
| 76 | *pbs = bs; |
| 77 | return bs; |
| 78 | } |
| 79 | |
| 80 | int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot) |
| 81 | { |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 82 | int fd; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 83 | int64_t size; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 84 | struct cow_header_v2 cow_header; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 85 | #ifndef _WIN32 |
| 86 | char template[] = "/tmp/vl.XXXXXX"; |
| 87 | int cow_fd; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 88 | struct stat st; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 89 | #endif |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 90 | |
bellard | 0849bf0 | 2003-06-30 23:17:31 +0000 | [diff] [blame] | 91 | bs->read_only = 0; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 92 | bs->fd = -1; |
| 93 | bs->cow_fd = -1; |
| 94 | bs->cow_bitmap = NULL; |
bellard | eba2af6 | 2004-06-19 17:23:39 +0000 | [diff] [blame] | 95 | pstrcpy(bs->filename, sizeof(bs->filename), filename); |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 96 | |
| 97 | /* open standard HD image */ |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 98 | #ifdef _WIN32 |
| 99 | fd = open(filename, O_RDWR | O_BINARY); |
| 100 | #else |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 101 | fd = open(filename, O_RDWR | O_LARGEFILE); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 102 | #endif |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 103 | if (fd < 0) { |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 104 | /* read only image on disk */ |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 105 | #ifdef _WIN32 |
| 106 | fd = open(filename, O_RDONLY | O_BINARY); |
| 107 | #else |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 108 | fd = open(filename, O_RDONLY | O_LARGEFILE); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 109 | #endif |
bellard | 0849bf0 | 2003-06-30 23:17:31 +0000 | [diff] [blame] | 110 | if (fd < 0) { |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 111 | perror(filename); |
| 112 | goto fail; |
bellard | 0849bf0 | 2003-06-30 23:17:31 +0000 | [diff] [blame] | 113 | } |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 114 | if (!snapshot) |
| 115 | bs->read_only = 1; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 116 | } |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 117 | bs->fd = fd; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 118 | |
| 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); |
| 122 | goto fail; |
| 123 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 124 | #ifndef _WIN32 |
| 125 | if (be32_to_cpu(cow_header.magic) == COW_MAGIC && |
| 126 | be32_to_cpu(cow_header.version) == COW_VERSION) { |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 127 | /* cow image found */ |
| 128 | size = cow_header.size; |
| 129 | #ifndef WORDS_BIGENDIAN |
| 130 | size = bswap64(size); |
| 131 | #endif |
| 132 | bs->total_sectors = size / 512; |
| 133 | |
| 134 | bs->cow_fd = fd; |
| 135 | bs->fd = -1; |
| 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); |
| 139 | goto fail; |
| 140 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 141 | if (st.st_mtime != be32_to_cpu(cow_header.mtime)) { |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 142 | fprintf(stderr, "%s: original raw disk image '%s' does not match saved timestamp\n", filename, cow_header.backing_file); |
| 143 | goto fail; |
| 144 | } |
| 145 | fd = open(cow_header.backing_file, O_RDONLY | O_LARGEFILE); |
| 146 | if (fd < 0) |
| 147 | goto fail; |
| 148 | bs->fd = fd; |
| 149 | } |
| 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), |
| 153 | bs->cow_bitmap_size, |
| 154 | PROT_READ | PROT_WRITE, |
| 155 | MAP_SHARED, bs->cow_fd, 0); |
| 156 | if (bs->cow_bitmap_addr == MAP_FAILED) |
| 157 | goto fail; |
| 158 | bs->cow_bitmap = bs->cow_bitmap_addr + sizeof(cow_header); |
| 159 | bs->cow_sectors_offset = (bs->cow_bitmap_size + 511) & ~511; |
| 160 | snapshot = 0; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 161 | } else |
| 162 | #endif |
| 163 | { |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 164 | /* standard raw image */ |
| 165 | size = lseek64(fd, 0, SEEK_END); |
| 166 | bs->total_sectors = size / 512; |
| 167 | bs->fd = fd; |
| 168 | } |
| 169 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 170 | #ifndef _WIN32 |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 171 | if (snapshot) { |
| 172 | /* create a temporary COW file */ |
bellard | 2b64948 | 2004-05-08 15:27:20 +0000 | [diff] [blame] | 173 | cow_fd = mkstemp64(template); |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 174 | if (cow_fd < 0) |
| 175 | goto fail; |
| 176 | bs->cow_fd = cow_fd; |
| 177 | unlink(template); |
| 178 | |
| 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), |
| 182 | bs->cow_bitmap_size, |
| 183 | PROT_READ | PROT_WRITE, |
| 184 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 185 | if (bs->cow_bitmap_addr == MAP_FAILED) |
| 186 | goto fail; |
| 187 | bs->cow_bitmap = bs->cow_bitmap_addr; |
| 188 | bs->cow_sectors_offset = 0; |
| 189 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 190 | #endif |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 191 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 192 | bs->inserted = 1; |
| 193 | |
| 194 | /* call the change callback */ |
| 195 | if (bs->change_cb) |
| 196 | bs->change_cb(bs->change_opaque); |
| 197 | |
| 198 | return 0; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 199 | fail: |
| 200 | bdrv_close(bs); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 201 | return -1; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void bdrv_close(BlockDriverState *bs) |
| 205 | { |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 206 | if (bs->inserted) { |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 207 | #ifndef _WIN32 |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 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); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 211 | #endif |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 212 | if (bs->cow_fd >= 0) |
| 213 | close(bs->cow_fd); |
| 214 | if (bs->fd >= 0) |
| 215 | close(bs->fd); |
| 216 | bs->inserted = 0; |
| 217 | |
| 218 | /* call the change callback */ |
| 219 | if (bs->change_cb) |
| 220 | bs->change_cb(bs->change_opaque); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | void bdrv_delete(BlockDriverState *bs) |
| 225 | { |
| 226 | bdrv_close(bs); |
| 227 | qemu_free(bs); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 228 | } |
| 229 | |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 230 | static inline void set_bit(uint8_t *bitmap, int64_t bitnum) |
| 231 | { |
| 232 | bitmap[bitnum / 8] |= (1 << (bitnum%8)); |
| 233 | } |
| 234 | |
| 235 | static inline int is_bit_set(const uint8_t *bitmap, int64_t bitnum) |
| 236 | { |
| 237 | return !!(bitmap[bitnum / 8] & (1 << (bitnum%8))); |
| 238 | } |
| 239 | |
| 240 | |
| 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 |
| 243 | * is true. */ |
| 244 | static int is_changed(uint8_t *bitmap, |
| 245 | int64_t sector_num, int nb_sectors, |
| 246 | int *num_same) |
| 247 | { |
| 248 | int changed; |
| 249 | |
| 250 | if (!bitmap || nb_sectors == 0) { |
| 251 | *num_same = nb_sectors; |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 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) |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | return changed; |
| 262 | } |
| 263 | |
| 264 | /* commit COW file into the raw image */ |
| 265 | int bdrv_commit(BlockDriverState *bs) |
| 266 | { |
| 267 | int64_t i; |
| 268 | uint8_t *cow_bitmap; |
| 269 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 270 | if (!bs->inserted) |
| 271 | return -1; |
| 272 | |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 273 | if (!bs->cow_bitmap) { |
| 274 | fprintf(stderr, "Already committed to %s\n", bs->filename); |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | if (bs->read_only) { |
| 279 | fprintf(stderr, "Can't commit to %s: read-only\n", bs->filename); |
| 280 | return -1; |
| 281 | } |
| 282 | |
| 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", |
| 289 | (long long)i); |
| 290 | return -1; |
| 291 | } |
| 292 | |
| 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", |
| 297 | (long long)i); |
| 298 | bs->cow_bitmap = cow_bitmap; |
| 299 | return -1; |
| 300 | } |
| 301 | bs->cow_bitmap = cow_bitmap; |
| 302 | } |
| 303 | } |
| 304 | fprintf(stderr, "Committed snapshot to %s\n", bs->filename); |
| 305 | return 0; |
| 306 | } |
| 307 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 308 | /* return -1 if error */ |
| 309 | int bdrv_read(BlockDriverState *bs, int64_t sector_num, |
| 310 | uint8_t *buf, int nb_sectors) |
| 311 | { |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 312 | int ret, n, fd; |
| 313 | int64_t offset; |
| 314 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 315 | if (!bs->inserted) |
| 316 | return -1; |
| 317 | |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 318 | while (nb_sectors > 0) { |
| 319 | if (is_changed(bs->cow_bitmap, sector_num, nb_sectors, &n)) { |
| 320 | fd = bs->cow_fd; |
| 321 | offset = bs->cow_sectors_offset; |
bellard | cf98951 | 2004-02-16 21:56:36 +0000 | [diff] [blame] | 322 | } else if (sector_num == 0 && bs->boot_sector_enabled) { |
| 323 | memcpy(buf, bs->boot_sector_data, 512); |
| 324 | n = 1; |
| 325 | goto next; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 326 | } else { |
| 327 | fd = bs->fd; |
| 328 | offset = 0; |
| 329 | } |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 330 | |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 331 | if (fd < 0) { |
| 332 | /* no file, just return empty sectors */ |
| 333 | memset(buf, 0, n * 512); |
| 334 | } else { |
| 335 | offset += sector_num * 512; |
| 336 | lseek64(fd, offset, SEEK_SET); |
| 337 | ret = read(fd, buf, n * 512); |
| 338 | if (ret != n * 512) { |
| 339 | return -1; |
| 340 | } |
| 341 | } |
bellard | cf98951 | 2004-02-16 21:56:36 +0000 | [diff] [blame] | 342 | next: |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 343 | nb_sectors -= n; |
| 344 | sector_num += n; |
| 345 | buf += n * 512; |
| 346 | } |
| 347 | return 0; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /* return -1 if error */ |
| 351 | int bdrv_write(BlockDriverState *bs, int64_t sector_num, |
| 352 | const uint8_t *buf, int nb_sectors) |
| 353 | { |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 354 | int ret, fd, i; |
| 355 | int64_t offset, retl; |
bellard | cf98951 | 2004-02-16 21:56:36 +0000 | [diff] [blame] | 356 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 357 | if (!bs->inserted) |
| 358 | return -1; |
bellard | 0849bf0 | 2003-06-30 23:17:31 +0000 | [diff] [blame] | 359 | if (bs->read_only) |
| 360 | return -1; |
| 361 | |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 362 | if (bs->cow_bitmap) { |
| 363 | fd = bs->cow_fd; |
| 364 | offset = bs->cow_sectors_offset; |
| 365 | } else { |
| 366 | fd = bs->fd; |
| 367 | offset = 0; |
| 368 | } |
| 369 | |
| 370 | offset += sector_num * 512; |
| 371 | retl = lseek64(fd, offset, SEEK_SET); |
| 372 | if (retl == -1) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 373 | return -1; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 374 | } |
| 375 | ret = write(fd, buf, nb_sectors * 512); |
| 376 | if (ret != nb_sectors * 512) { |
| 377 | return -1; |
| 378 | } |
| 379 | |
| 380 | if (bs->cow_bitmap) { |
| 381 | for (i = 0; i < nb_sectors; i++) |
| 382 | set_bit(bs->cow_bitmap, sector_num + i); |
| 383 | } |
| 384 | return 0; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr) |
| 388 | { |
| 389 | *nb_sectors_ptr = bs->total_sectors; |
| 390 | } |
bellard | cf98951 | 2004-02-16 21:56:36 +0000 | [diff] [blame] | 391 | |
| 392 | /* force a given boot sector. */ |
| 393 | void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size) |
| 394 | { |
| 395 | bs->boot_sector_enabled = 1; |
| 396 | if (size > 512) |
| 397 | size = 512; |
| 398 | memcpy(bs->boot_sector_data, data, size); |
| 399 | memset(bs->boot_sector_data + size, 0, 512 - size); |
| 400 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 401 | |
| 402 | void bdrv_set_geometry_hint(BlockDriverState *bs, |
| 403 | int cyls, int heads, int secs) |
| 404 | { |
| 405 | bs->cyls = cyls; |
| 406 | bs->heads = heads; |
| 407 | bs->secs = secs; |
| 408 | } |
| 409 | |
| 410 | void bdrv_set_type_hint(BlockDriverState *bs, int type) |
| 411 | { |
| 412 | bs->type = type; |
| 413 | bs->removable = ((type == BDRV_TYPE_CDROM || |
| 414 | type == BDRV_TYPE_FLOPPY)); |
| 415 | } |
| 416 | |
| 417 | void bdrv_get_geometry_hint(BlockDriverState *bs, |
| 418 | int *pcyls, int *pheads, int *psecs) |
| 419 | { |
| 420 | *pcyls = bs->cyls; |
| 421 | *pheads = bs->heads; |
| 422 | *psecs = bs->secs; |
| 423 | } |
| 424 | |
| 425 | int bdrv_get_type_hint(BlockDriverState *bs) |
| 426 | { |
| 427 | return bs->type; |
| 428 | } |
| 429 | |
| 430 | int bdrv_is_removable(BlockDriverState *bs) |
| 431 | { |
| 432 | return bs->removable; |
| 433 | } |
| 434 | |
| 435 | int bdrv_is_read_only(BlockDriverState *bs) |
| 436 | { |
| 437 | return bs->read_only; |
| 438 | } |
| 439 | |
| 440 | int bdrv_is_inserted(BlockDriverState *bs) |
| 441 | { |
| 442 | return bs->inserted; |
| 443 | } |
| 444 | |
| 445 | int bdrv_is_locked(BlockDriverState *bs) |
| 446 | { |
| 447 | return bs->locked; |
| 448 | } |
| 449 | |
| 450 | void bdrv_set_locked(BlockDriverState *bs, int locked) |
| 451 | { |
| 452 | bs->locked = locked; |
| 453 | } |
| 454 | |
| 455 | void bdrv_set_change_cb(BlockDriverState *bs, |
| 456 | void (*change_cb)(void *opaque), void *opaque) |
| 457 | { |
| 458 | bs->change_cb = change_cb; |
| 459 | bs->change_opaque = opaque; |
| 460 | } |
| 461 | |
| 462 | BlockDriverState *bdrv_find(const char *name) |
| 463 | { |
| 464 | BlockDriverState *bs; |
| 465 | |
| 466 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
| 467 | if (!strcmp(name, bs->device_name)) |
| 468 | return bs; |
| 469 | } |
| 470 | return NULL; |
| 471 | } |
| 472 | |
| 473 | void bdrv_info(void) |
| 474 | { |
| 475 | BlockDriverState *bs; |
| 476 | |
| 477 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
| 478 | term_printf("%s:", bs->device_name); |
| 479 | term_printf(" type="); |
| 480 | switch(bs->type) { |
| 481 | case BDRV_TYPE_HD: |
| 482 | term_printf("hd"); |
| 483 | break; |
| 484 | case BDRV_TYPE_CDROM: |
| 485 | term_printf("cdrom"); |
| 486 | break; |
| 487 | case BDRV_TYPE_FLOPPY: |
| 488 | term_printf("floppy"); |
| 489 | break; |
| 490 | } |
| 491 | term_printf(" removable=%d", bs->removable); |
| 492 | if (bs->removable) { |
| 493 | term_printf(" locked=%d", bs->locked); |
| 494 | } |
| 495 | if (bs->inserted) { |
| 496 | term_printf(" file=%s", bs->filename); |
| 497 | term_printf(" ro=%d", bs->read_only); |
| 498 | } else { |
| 499 | term_printf(" [not inserted]"); |
| 500 | } |
| 501 | term_printf("\n"); |
| 502 | } |
| 503 | } |