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" |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 25 | #include "block_int.h" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 26 | |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 27 | #ifdef _BSD |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/ioctl.h> |
| 31 | #include <sys/queue.h> |
| 32 | #include <sys/disk.h> |
| 33 | #endif |
| 34 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 35 | #define SECTOR_BITS 9 |
| 36 | #define SECTOR_SIZE (1 << SECTOR_BITS) |
bellard | 3b0d4f6 | 2005-10-30 18:30:10 +0000 | [diff] [blame] | 37 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 38 | static int bdrv_aio_new_em(BlockDriverAIOCB *acb); |
| 39 | static int bdrv_aio_read_em(BlockDriverAIOCB *acb, int64_t sector_num, |
| 40 | uint8_t *buf, int nb_sectors); |
| 41 | static int bdrv_aio_write_em(BlockDriverAIOCB *acb, int64_t sector_num, |
| 42 | const uint8_t *buf, int nb_sectors); |
| 43 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb); |
| 44 | static void bdrv_aio_delete_em(BlockDriverAIOCB *acb); |
| 45 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
| 46 | uint8_t *buf, int nb_sectors); |
| 47 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
| 48 | const uint8_t *buf, int nb_sectors); |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 49 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 50 | static BlockDriverState *bdrv_first; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 51 | static BlockDriver *first_drv; |
| 52 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 53 | #ifdef _WIN32 |
| 54 | #define PATH_SEP '\\' |
| 55 | #else |
| 56 | #define PATH_SEP '/' |
bellard | 3b0d4f6 | 2005-10-30 18:30:10 +0000 | [diff] [blame] | 57 | #endif |
| 58 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 59 | int path_is_absolute(const char *path) |
| 60 | { |
| 61 | const char *p; |
| 62 | p = strchr(path, ':'); |
| 63 | if (p) |
| 64 | p++; |
| 65 | else |
| 66 | p = path; |
| 67 | return (*p == PATH_SEP); |
| 68 | } |
| 69 | |
| 70 | /* if filename is absolute, just copy it to dest. Otherwise, build a |
| 71 | path to it by considering it is relative to base_path. URL are |
| 72 | supported. */ |
| 73 | void path_combine(char *dest, int dest_size, |
| 74 | const char *base_path, |
| 75 | const char *filename) |
| 76 | { |
| 77 | const char *p, *p1; |
| 78 | int len; |
| 79 | |
| 80 | if (dest_size <= 0) |
| 81 | return; |
| 82 | if (path_is_absolute(filename)) { |
| 83 | pstrcpy(dest, dest_size, filename); |
| 84 | } else { |
| 85 | p = strchr(base_path, ':'); |
| 86 | if (p) |
| 87 | p++; |
| 88 | else |
| 89 | p = base_path; |
| 90 | p1 = strrchr(base_path, PATH_SEP); |
| 91 | if (p1) |
| 92 | p1++; |
| 93 | else |
| 94 | p1 = base_path; |
| 95 | if (p1 > p) |
| 96 | p = p1; |
| 97 | len = p - base_path; |
| 98 | if (len > dest_size - 1) |
| 99 | len = dest_size - 1; |
| 100 | memcpy(dest, base_path, len); |
| 101 | dest[len] = '\0'; |
| 102 | pstrcat(dest, dest_size, filename); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 107 | void bdrv_register(BlockDriver *bdrv) |
| 108 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 109 | if (!bdrv->bdrv_aio_new) { |
| 110 | /* add AIO emulation layer */ |
| 111 | bdrv->bdrv_aio_new = bdrv_aio_new_em; |
| 112 | bdrv->bdrv_aio_read = bdrv_aio_read_em; |
| 113 | bdrv->bdrv_aio_write = bdrv_aio_write_em; |
| 114 | bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em; |
| 115 | bdrv->bdrv_aio_delete = bdrv_aio_delete_em; |
| 116 | } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) { |
| 117 | /* add synchronous IO emulation layer */ |
| 118 | bdrv->bdrv_read = bdrv_read_em; |
| 119 | bdrv->bdrv_write = bdrv_write_em; |
| 120 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 121 | bdrv->next = first_drv; |
| 122 | first_drv = bdrv; |
| 123 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 124 | |
| 125 | /* create a new block device (by default it is empty) */ |
| 126 | BlockDriverState *bdrv_new(const char *device_name) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 127 | { |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 128 | BlockDriverState **pbs, *bs; |
| 129 | |
| 130 | bs = qemu_mallocz(sizeof(BlockDriverState)); |
| 131 | if(!bs) |
| 132 | return NULL; |
| 133 | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 134 | if (device_name[0] != '\0') { |
| 135 | /* insert at the end */ |
| 136 | pbs = &bdrv_first; |
| 137 | while (*pbs != NULL) |
| 138 | pbs = &(*pbs)->next; |
| 139 | *pbs = bs; |
| 140 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 141 | return bs; |
| 142 | } |
| 143 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 144 | BlockDriver *bdrv_find_format(const char *format_name) |
| 145 | { |
| 146 | BlockDriver *drv1; |
| 147 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
| 148 | if (!strcmp(drv1->format_name, format_name)) |
| 149 | return drv1; |
| 150 | } |
| 151 | return NULL; |
| 152 | } |
| 153 | |
| 154 | int bdrv_create(BlockDriver *drv, |
| 155 | const char *filename, int64_t size_in_sectors, |
| 156 | const char *backing_file, int flags) |
| 157 | { |
| 158 | if (!drv->bdrv_create) |
| 159 | return -ENOTSUP; |
| 160 | return drv->bdrv_create(filename, size_in_sectors, backing_file, flags); |
| 161 | } |
| 162 | |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 163 | #ifdef _WIN32 |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 164 | void get_tmp_filename(char *filename, int size) |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 165 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 166 | tmpnam(filename); |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 167 | } |
| 168 | #else |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 169 | void get_tmp_filename(char *filename, int size) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 170 | { |
| 171 | int fd; |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 172 | /* XXX: race condition possible */ |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 173 | pstrcpy(filename, size, "/tmp/vl.XXXXXX"); |
| 174 | fd = mkstemp(filename); |
| 175 | close(fd); |
| 176 | } |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 177 | #endif |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 178 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 179 | static BlockDriver *find_protocol(const char *filename) |
| 180 | { |
| 181 | BlockDriver *drv1; |
| 182 | char protocol[128]; |
| 183 | int len; |
| 184 | const char *p; |
| 185 | p = strchr(filename, ':'); |
| 186 | if (!p) |
| 187 | return &bdrv_raw; |
| 188 | len = p - filename; |
| 189 | if (len > sizeof(protocol) - 1) |
| 190 | len = sizeof(protocol) - 1; |
| 191 | #ifdef _WIN32 |
| 192 | if (len == 1) { |
| 193 | /* specific win32 case for driver letters */ |
| 194 | return &bdrv_raw; |
| 195 | } |
| 196 | #endif |
| 197 | memcpy(protocol, filename, len); |
| 198 | protocol[len] = '\0'; |
| 199 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
| 200 | if (drv1->protocol_name && |
| 201 | !strcmp(drv1->protocol_name, protocol)) |
| 202 | return drv1; |
| 203 | } |
| 204 | return NULL; |
| 205 | } |
| 206 | |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 207 | /* XXX: force raw format if block or character device ? It would |
| 208 | simplify the BSD case */ |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 209 | static BlockDriver *find_image_format(const char *filename) |
| 210 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 211 | int ret, score, score_max; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 212 | BlockDriver *drv1, *drv; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 213 | uint8_t buf[2048]; |
| 214 | BlockDriverState *bs; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 215 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 216 | drv = find_protocol(filename); |
| 217 | /* no need to test disk image formats for vvfat or host specific |
| 218 | devices */ |
| 219 | if (drv == &bdrv_vvfat) |
| 220 | return drv; |
| 221 | if (strstart(filename, "/dev/", NULL)) |
| 222 | return &bdrv_raw; |
| 223 | |
| 224 | ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY); |
| 225 | if (ret < 0) |
| 226 | return NULL; |
| 227 | ret = bdrv_pread(bs, 0, buf, sizeof(buf)); |
| 228 | bdrv_delete(bs); |
| 229 | if (ret < 0) { |
| 230 | return NULL; |
| 231 | } |
| 232 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 233 | score_max = 0; |
| 234 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 235 | if (drv1->bdrv_probe) { |
| 236 | score = drv1->bdrv_probe(buf, ret, filename); |
| 237 | if (score > score_max) { |
| 238 | score_max = score; |
| 239 | drv = drv1; |
| 240 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | return drv; |
| 244 | } |
| 245 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 246 | int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 247 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 248 | BlockDriverState *bs; |
| 249 | int ret; |
| 250 | |
| 251 | bs = bdrv_new(""); |
| 252 | if (!bs) |
| 253 | return -ENOMEM; |
| 254 | ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL); |
| 255 | if (ret < 0) { |
| 256 | bdrv_delete(bs); |
| 257 | return ret; |
bellard | 3b0d4f6 | 2005-10-30 18:30:10 +0000 | [diff] [blame] | 258 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 259 | *pbs = bs; |
| 260 | return 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 261 | } |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 262 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 263 | int bdrv_open(BlockDriverState *bs, const char *filename, int flags) |
| 264 | { |
| 265 | return bdrv_open2(bs, filename, flags, NULL); |
| 266 | } |
| 267 | |
| 268 | int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 269 | BlockDriver *drv) |
| 270 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 271 | int ret, open_flags; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 272 | char tmp_filename[1024]; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 273 | char backing_filename[1024]; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 274 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 275 | bs->read_only = 0; |
| 276 | bs->is_temporary = 0; |
| 277 | bs->encrypted = 0; |
bellard | 712e787 | 2005-04-28 21:09:32 +0000 | [diff] [blame] | 278 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 279 | if (flags & BDRV_O_SNAPSHOT) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 280 | BlockDriverState *bs1; |
| 281 | int64_t total_size; |
| 282 | |
| 283 | /* if snapshot, we create a temporary backing file and open it |
| 284 | instead of opening 'filename' directly */ |
| 285 | |
| 286 | /* if there is a backing file, use it */ |
| 287 | bs1 = bdrv_new(""); |
| 288 | if (!bs1) { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 289 | return -ENOMEM; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 290 | } |
| 291 | if (bdrv_open(bs1, filename, 0) < 0) { |
| 292 | bdrv_delete(bs1); |
| 293 | return -1; |
| 294 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 295 | total_size = bdrv_getlength(bs1) >> SECTOR_BITS; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 296 | bdrv_delete(bs1); |
| 297 | |
| 298 | get_tmp_filename(tmp_filename, sizeof(tmp_filename)); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 299 | if (bdrv_create(&bdrv_qcow, tmp_filename, |
| 300 | total_size, filename, 0) < 0) { |
| 301 | return -1; |
| 302 | } |
| 303 | filename = tmp_filename; |
| 304 | bs->is_temporary = 1; |
| 305 | } |
bellard | 712e787 | 2005-04-28 21:09:32 +0000 | [diff] [blame] | 306 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 307 | pstrcpy(bs->filename, sizeof(bs->filename), filename); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 308 | if (flags & BDRV_O_FILE) { |
| 309 | drv = find_protocol(filename); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 310 | if (!drv) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 311 | return -ENOENT; |
| 312 | } else { |
| 313 | if (!drv) { |
| 314 | drv = find_image_format(filename); |
| 315 | if (!drv) |
| 316 | return -1; |
| 317 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 318 | } |
| 319 | bs->drv = drv; |
| 320 | bs->opaque = qemu_mallocz(drv->instance_size); |
| 321 | if (bs->opaque == NULL && drv->instance_size > 0) |
| 322 | return -1; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 323 | /* Note: for compatibility, we open disk image files as RDWR, and |
| 324 | RDONLY as fallback */ |
| 325 | if (!(flags & BDRV_O_FILE)) |
| 326 | open_flags = BDRV_O_RDWR; |
| 327 | else |
| 328 | open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); |
| 329 | ret = drv->bdrv_open(bs, filename, open_flags); |
| 330 | if (ret == -EACCES && !(flags & BDRV_O_FILE)) { |
| 331 | ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY); |
| 332 | bs->read_only = 1; |
| 333 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 334 | if (ret < 0) { |
| 335 | qemu_free(bs->opaque); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 336 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 337 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 338 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 339 | #ifndef _WIN32 |
| 340 | if (bs->is_temporary) { |
| 341 | unlink(filename); |
| 342 | } |
| 343 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 344 | if (bs->backing_file[0] != '\0') { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 345 | /* if there is a backing file, use it */ |
| 346 | bs->backing_hd = bdrv_new(""); |
| 347 | if (!bs->backing_hd) { |
| 348 | fail: |
| 349 | bdrv_close(bs); |
| 350 | return -1; |
| 351 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 352 | path_combine(backing_filename, sizeof(backing_filename), |
| 353 | filename, bs->backing_file); |
| 354 | if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 355 | goto fail; |
| 356 | } |
| 357 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 358 | bs->inserted = 1; |
| 359 | |
| 360 | /* call the change callback */ |
| 361 | if (bs->change_cb) |
| 362 | bs->change_cb(bs->change_opaque); |
| 363 | |
| 364 | return 0; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void bdrv_close(BlockDriverState *bs) |
| 368 | { |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 369 | if (bs->inserted) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 370 | if (bs->backing_hd) |
| 371 | bdrv_delete(bs->backing_hd); |
| 372 | bs->drv->bdrv_close(bs); |
| 373 | qemu_free(bs->opaque); |
| 374 | #ifdef _WIN32 |
| 375 | if (bs->is_temporary) { |
| 376 | unlink(bs->filename); |
| 377 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 378 | #endif |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 379 | bs->opaque = NULL; |
| 380 | bs->drv = NULL; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 381 | bs->inserted = 0; |
| 382 | |
| 383 | /* call the change callback */ |
| 384 | if (bs->change_cb) |
| 385 | bs->change_cb(bs->change_opaque); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | void bdrv_delete(BlockDriverState *bs) |
| 390 | { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 391 | /* XXX: remove the driver list */ |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 392 | bdrv_close(bs); |
| 393 | qemu_free(bs); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 394 | } |
| 395 | |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 396 | /* commit COW file into the raw image */ |
| 397 | int bdrv_commit(BlockDriverState *bs) |
| 398 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 399 | int64_t i, total_sectors; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 400 | int n, j; |
| 401 | unsigned char sector[512]; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 402 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 403 | if (!bs->inserted) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 404 | return -ENOENT; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 405 | |
| 406 | if (bs->read_only) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 407 | return -EACCES; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 408 | } |
| 409 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 410 | if (!bs->backing_hd) { |
| 411 | return -ENOTSUP; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 412 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 413 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 414 | total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; |
| 415 | for (i = 0; i < total_sectors;) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 416 | if (bs->drv->bdrv_is_allocated(bs, i, 65536, &n)) { |
| 417 | for(j = 0; j < n; j++) { |
| 418 | if (bdrv_read(bs, i, sector, 1) != 0) { |
| 419 | return -EIO; |
| 420 | } |
| 421 | |
| 422 | if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { |
| 423 | return -EIO; |
| 424 | } |
| 425 | i++; |
| 426 | } |
| 427 | } else { |
| 428 | i += n; |
| 429 | } |
| 430 | } |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 431 | |
| 432 | if (bs->drv->bdrv_make_empty) |
| 433 | return bs->drv->bdrv_make_empty(bs); |
| 434 | |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 435 | return 0; |
| 436 | } |
| 437 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 438 | /* return < 0 if error */ |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 439 | int bdrv_read(BlockDriverState *bs, int64_t sector_num, |
| 440 | uint8_t *buf, int nb_sectors) |
| 441 | { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 442 | BlockDriver *drv = bs->drv; |
| 443 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 444 | if (!bs->inserted) |
| 445 | return -1; |
| 446 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 447 | if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { |
bellard | cf98951 | 2004-02-16 21:56:36 +0000 | [diff] [blame] | 448 | memcpy(buf, bs->boot_sector_data, 512); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 449 | sector_num++; |
| 450 | nb_sectors--; |
| 451 | buf += 512; |
| 452 | if (nb_sectors == 0) |
| 453 | return 0; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 454 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 455 | if (drv->bdrv_pread) { |
| 456 | int ret, len; |
| 457 | len = nb_sectors * 512; |
| 458 | ret = drv->bdrv_pread(bs, sector_num * 512, buf, len); |
| 459 | if (ret < 0) |
| 460 | return ret; |
| 461 | else if (ret != len) |
| 462 | return -EIO; |
| 463 | else |
| 464 | return 0; |
| 465 | } else { |
| 466 | return drv->bdrv_read(bs, sector_num, buf, nb_sectors); |
| 467 | } |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 468 | } |
| 469 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 470 | /* return < 0 if error */ |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 471 | int bdrv_write(BlockDriverState *bs, int64_t sector_num, |
| 472 | const uint8_t *buf, int nb_sectors) |
| 473 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 474 | BlockDriver *drv = bs->drv; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 475 | if (!bs->inserted) |
| 476 | return -1; |
bellard | 0849bf0 | 2003-06-30 23:17:31 +0000 | [diff] [blame] | 477 | if (bs->read_only) |
| 478 | return -1; |
bellard | 79639d4 | 2005-11-26 10:58:41 +0000 | [diff] [blame] | 479 | if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { |
| 480 | memcpy(bs->boot_sector_data, buf, 512); |
| 481 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 482 | if (drv->bdrv_pwrite) { |
| 483 | int ret, len; |
| 484 | len = nb_sectors * 512; |
| 485 | ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len); |
| 486 | if (ret < 0) |
| 487 | return ret; |
| 488 | else if (ret != len) |
| 489 | return -EIO; |
| 490 | else |
| 491 | return 0; |
| 492 | } else { |
| 493 | return drv->bdrv_write(bs, sector_num, buf, nb_sectors); |
| 494 | } |
| 495 | } |
| 496 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 497 | /* not necessary now */ |
| 498 | static int bdrv_pread_em(BlockDriverState *bs, int64_t offset, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame^] | 499 | uint8_t *buf, int count1) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 500 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 501 | uint8_t tmp_buf[SECTOR_SIZE]; |
| 502 | int len, nb_sectors, count; |
| 503 | int64_t sector_num; |
| 504 | |
| 505 | count = count1; |
| 506 | /* first read to align to sector start */ |
| 507 | len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); |
| 508 | if (len > count) |
| 509 | len = count; |
| 510 | sector_num = offset >> SECTOR_BITS; |
| 511 | if (len > 0) { |
| 512 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
| 513 | return -EIO; |
| 514 | memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len); |
| 515 | count -= len; |
| 516 | if (count == 0) |
| 517 | return count1; |
| 518 | sector_num++; |
| 519 | buf += len; |
| 520 | } |
| 521 | |
| 522 | /* read the sectors "in place" */ |
| 523 | nb_sectors = count >> SECTOR_BITS; |
| 524 | if (nb_sectors > 0) { |
| 525 | if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0) |
| 526 | return -EIO; |
| 527 | sector_num += nb_sectors; |
| 528 | len = nb_sectors << SECTOR_BITS; |
| 529 | buf += len; |
| 530 | count -= len; |
| 531 | } |
| 532 | |
| 533 | /* add data from the last sector */ |
| 534 | if (count > 0) { |
| 535 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
| 536 | return -EIO; |
| 537 | memcpy(buf, tmp_buf, count); |
| 538 | } |
| 539 | return count1; |
| 540 | } |
| 541 | |
| 542 | static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame^] | 543 | const uint8_t *buf, int count1) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 544 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 545 | uint8_t tmp_buf[SECTOR_SIZE]; |
| 546 | int len, nb_sectors, count; |
| 547 | int64_t sector_num; |
| 548 | |
| 549 | count = count1; |
| 550 | /* first write to align to sector start */ |
| 551 | len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); |
| 552 | if (len > count) |
| 553 | len = count; |
| 554 | sector_num = offset >> SECTOR_BITS; |
| 555 | if (len > 0) { |
| 556 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
| 557 | return -EIO; |
| 558 | memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len); |
| 559 | if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) |
| 560 | return -EIO; |
| 561 | count -= len; |
| 562 | if (count == 0) |
| 563 | return count1; |
| 564 | sector_num++; |
| 565 | buf += len; |
| 566 | } |
| 567 | |
| 568 | /* write the sectors "in place" */ |
| 569 | nb_sectors = count >> SECTOR_BITS; |
| 570 | if (nb_sectors > 0) { |
| 571 | if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0) |
| 572 | return -EIO; |
| 573 | sector_num += nb_sectors; |
| 574 | len = nb_sectors << SECTOR_BITS; |
| 575 | buf += len; |
| 576 | count -= len; |
| 577 | } |
| 578 | |
| 579 | /* add data from the last sector */ |
| 580 | if (count > 0) { |
| 581 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
| 582 | return -EIO; |
| 583 | memcpy(tmp_buf, buf, count); |
| 584 | if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) |
| 585 | return -EIO; |
| 586 | } |
| 587 | return count1; |
| 588 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 589 | |
| 590 | /** |
| 591 | * Read with byte offsets (needed only for file protocols) |
| 592 | */ |
| 593 | int bdrv_pread(BlockDriverState *bs, int64_t offset, |
| 594 | void *buf1, int count1) |
| 595 | { |
| 596 | BlockDriver *drv = bs->drv; |
| 597 | |
| 598 | if (!drv) |
| 599 | return -ENOENT; |
| 600 | if (!drv->bdrv_pread) |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame^] | 601 | return bdrv_pread_em(bs, offset, buf1, count1); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 602 | return drv->bdrv_pread(bs, offset, buf1, count1); |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Write with byte offsets (needed only for file protocols) |
| 607 | */ |
| 608 | int bdrv_pwrite(BlockDriverState *bs, int64_t offset, |
| 609 | const void *buf1, int count1) |
| 610 | { |
| 611 | BlockDriver *drv = bs->drv; |
| 612 | |
| 613 | if (!drv) |
| 614 | return -ENOENT; |
| 615 | if (!drv->bdrv_pwrite) |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame^] | 616 | return bdrv_pwrite_em(bs, offset, buf1, count1); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 617 | return drv->bdrv_pwrite(bs, offset, buf1, count1); |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * Truncate file to 'offset' bytes (needed only for file protocols) |
| 622 | */ |
| 623 | int bdrv_truncate(BlockDriverState *bs, int64_t offset) |
| 624 | { |
| 625 | BlockDriver *drv = bs->drv; |
| 626 | if (!drv) |
| 627 | return -ENOENT; |
| 628 | if (!drv->bdrv_truncate) |
| 629 | return -ENOTSUP; |
| 630 | return drv->bdrv_truncate(bs, offset); |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Length of a file in bytes. Return < 0 if error or unknown. |
| 635 | */ |
| 636 | int64_t bdrv_getlength(BlockDriverState *bs) |
| 637 | { |
| 638 | BlockDriver *drv = bs->drv; |
| 639 | if (!drv) |
| 640 | return -ENOENT; |
| 641 | if (!drv->bdrv_getlength) { |
| 642 | /* legacy mode */ |
| 643 | return bs->total_sectors * SECTOR_SIZE; |
| 644 | } |
| 645 | return drv->bdrv_getlength(bs); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr) |
| 649 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 650 | int64_t size; |
| 651 | size = bdrv_getlength(bs); |
| 652 | if (size < 0) |
| 653 | size = 0; |
| 654 | *nb_sectors_ptr = size >> SECTOR_BITS; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 655 | } |
bellard | cf98951 | 2004-02-16 21:56:36 +0000 | [diff] [blame] | 656 | |
| 657 | /* force a given boot sector. */ |
| 658 | void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size) |
| 659 | { |
| 660 | bs->boot_sector_enabled = 1; |
| 661 | if (size > 512) |
| 662 | size = 512; |
| 663 | memcpy(bs->boot_sector_data, data, size); |
| 664 | memset(bs->boot_sector_data + size, 0, 512 - size); |
| 665 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 666 | |
| 667 | void bdrv_set_geometry_hint(BlockDriverState *bs, |
| 668 | int cyls, int heads, int secs) |
| 669 | { |
| 670 | bs->cyls = cyls; |
| 671 | bs->heads = heads; |
| 672 | bs->secs = secs; |
| 673 | } |
| 674 | |
| 675 | void bdrv_set_type_hint(BlockDriverState *bs, int type) |
| 676 | { |
| 677 | bs->type = type; |
| 678 | bs->removable = ((type == BDRV_TYPE_CDROM || |
| 679 | type == BDRV_TYPE_FLOPPY)); |
| 680 | } |
| 681 | |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 682 | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
| 683 | { |
| 684 | bs->translation = translation; |
| 685 | } |
| 686 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 687 | void bdrv_get_geometry_hint(BlockDriverState *bs, |
| 688 | int *pcyls, int *pheads, int *psecs) |
| 689 | { |
| 690 | *pcyls = bs->cyls; |
| 691 | *pheads = bs->heads; |
| 692 | *psecs = bs->secs; |
| 693 | } |
| 694 | |
| 695 | int bdrv_get_type_hint(BlockDriverState *bs) |
| 696 | { |
| 697 | return bs->type; |
| 698 | } |
| 699 | |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 700 | int bdrv_get_translation_hint(BlockDriverState *bs) |
| 701 | { |
| 702 | return bs->translation; |
| 703 | } |
| 704 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 705 | int bdrv_is_removable(BlockDriverState *bs) |
| 706 | { |
| 707 | return bs->removable; |
| 708 | } |
| 709 | |
| 710 | int bdrv_is_read_only(BlockDriverState *bs) |
| 711 | { |
| 712 | return bs->read_only; |
| 713 | } |
| 714 | |
| 715 | int bdrv_is_inserted(BlockDriverState *bs) |
| 716 | { |
| 717 | return bs->inserted; |
| 718 | } |
| 719 | |
| 720 | int bdrv_is_locked(BlockDriverState *bs) |
| 721 | { |
| 722 | return bs->locked; |
| 723 | } |
| 724 | |
| 725 | void bdrv_set_locked(BlockDriverState *bs, int locked) |
| 726 | { |
| 727 | bs->locked = locked; |
| 728 | } |
| 729 | |
| 730 | void bdrv_set_change_cb(BlockDriverState *bs, |
| 731 | void (*change_cb)(void *opaque), void *opaque) |
| 732 | { |
| 733 | bs->change_cb = change_cb; |
| 734 | bs->change_opaque = opaque; |
| 735 | } |
| 736 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 737 | int bdrv_is_encrypted(BlockDriverState *bs) |
| 738 | { |
| 739 | if (bs->backing_hd && bs->backing_hd->encrypted) |
| 740 | return 1; |
| 741 | return bs->encrypted; |
| 742 | } |
| 743 | |
| 744 | int bdrv_set_key(BlockDriverState *bs, const char *key) |
| 745 | { |
| 746 | int ret; |
| 747 | if (bs->backing_hd && bs->backing_hd->encrypted) { |
| 748 | ret = bdrv_set_key(bs->backing_hd, key); |
| 749 | if (ret < 0) |
| 750 | return ret; |
| 751 | if (!bs->encrypted) |
| 752 | return 0; |
| 753 | } |
| 754 | if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key) |
| 755 | return -1; |
| 756 | return bs->drv->bdrv_set_key(bs, key); |
| 757 | } |
| 758 | |
| 759 | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) |
| 760 | { |
| 761 | if (!bs->inserted || !bs->drv) { |
| 762 | buf[0] = '\0'; |
| 763 | } else { |
| 764 | pstrcpy(buf, buf_size, bs->drv->format_name); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
| 769 | void *opaque) |
| 770 | { |
| 771 | BlockDriver *drv; |
| 772 | |
| 773 | for (drv = first_drv; drv != NULL; drv = drv->next) { |
| 774 | it(opaque, drv->format_name); |
| 775 | } |
| 776 | } |
| 777 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 778 | BlockDriverState *bdrv_find(const char *name) |
| 779 | { |
| 780 | BlockDriverState *bs; |
| 781 | |
| 782 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
| 783 | if (!strcmp(name, bs->device_name)) |
| 784 | return bs; |
| 785 | } |
| 786 | return NULL; |
| 787 | } |
| 788 | |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 789 | void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque) |
| 790 | { |
| 791 | BlockDriverState *bs; |
| 792 | |
| 793 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
| 794 | it(opaque, bs->device_name); |
| 795 | } |
| 796 | } |
| 797 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 798 | const char *bdrv_get_device_name(BlockDriverState *bs) |
| 799 | { |
| 800 | return bs->device_name; |
| 801 | } |
| 802 | |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 803 | void bdrv_flush(BlockDriverState *bs) |
| 804 | { |
| 805 | if (bs->drv->bdrv_flush) |
| 806 | bs->drv->bdrv_flush(bs); |
| 807 | if (bs->backing_hd) |
| 808 | bdrv_flush(bs->backing_hd); |
| 809 | } |
| 810 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 811 | void bdrv_info(void) |
| 812 | { |
| 813 | BlockDriverState *bs; |
| 814 | |
| 815 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
| 816 | term_printf("%s:", bs->device_name); |
| 817 | term_printf(" type="); |
| 818 | switch(bs->type) { |
| 819 | case BDRV_TYPE_HD: |
| 820 | term_printf("hd"); |
| 821 | break; |
| 822 | case BDRV_TYPE_CDROM: |
| 823 | term_printf("cdrom"); |
| 824 | break; |
| 825 | case BDRV_TYPE_FLOPPY: |
| 826 | term_printf("floppy"); |
| 827 | break; |
| 828 | } |
| 829 | term_printf(" removable=%d", bs->removable); |
| 830 | if (bs->removable) { |
| 831 | term_printf(" locked=%d", bs->locked); |
| 832 | } |
| 833 | if (bs->inserted) { |
| 834 | term_printf(" file=%s", bs->filename); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 835 | if (bs->backing_file[0] != '\0') |
| 836 | term_printf(" backing_file=%s", bs->backing_file); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 837 | term_printf(" ro=%d", bs->read_only); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 838 | term_printf(" drv=%s", bs->drv->format_name); |
| 839 | if (bs->encrypted) |
| 840 | term_printf(" encrypted"); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 841 | } else { |
| 842 | term_printf(" [not inserted]"); |
| 843 | } |
| 844 | term_printf("\n"); |
| 845 | } |
| 846 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 847 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 848 | void bdrv_get_backing_filename(BlockDriverState *bs, |
| 849 | char *filename, int filename_size) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 850 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 851 | if (!bs->backing_hd) { |
| 852 | pstrcpy(filename, filename_size, ""); |
| 853 | } else { |
| 854 | pstrcpy(filename, filename_size, bs->backing_file); |
| 855 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 856 | } |
| 857 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame^] | 858 | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, |
| 859 | const uint8_t *buf, int nb_sectors) |
| 860 | { |
| 861 | BlockDriver *drv = bs->drv; |
| 862 | if (!drv) |
| 863 | return -ENOENT; |
| 864 | if (!drv->bdrv_write_compressed) |
| 865 | return -ENOTSUP; |
| 866 | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); |
| 867 | } |
| 868 | |
| 869 | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
| 870 | { |
| 871 | BlockDriver *drv = bs->drv; |
| 872 | if (!drv) |
| 873 | return -ENOENT; |
| 874 | if (!drv->bdrv_get_info) |
| 875 | return -ENOTSUP; |
| 876 | memset(bdi, 0, sizeof(*bdi)); |
| 877 | return drv->bdrv_get_info(bs, bdi); |
| 878 | } |
| 879 | |
| 880 | /**************************************************************/ |
| 881 | /* handling of snapshots */ |
| 882 | |
| 883 | int bdrv_snapshot_create(BlockDriverState *bs, |
| 884 | QEMUSnapshotInfo *sn_info) |
| 885 | { |
| 886 | BlockDriver *drv = bs->drv; |
| 887 | if (!drv) |
| 888 | return -ENOENT; |
| 889 | if (!drv->bdrv_snapshot_create) |
| 890 | return -ENOTSUP; |
| 891 | return drv->bdrv_snapshot_create(bs, sn_info); |
| 892 | } |
| 893 | |
| 894 | int bdrv_snapshot_goto(BlockDriverState *bs, |
| 895 | const char *snapshot_id) |
| 896 | { |
| 897 | BlockDriver *drv = bs->drv; |
| 898 | if (!drv) |
| 899 | return -ENOENT; |
| 900 | if (!drv->bdrv_snapshot_goto) |
| 901 | return -ENOTSUP; |
| 902 | return drv->bdrv_snapshot_goto(bs, snapshot_id); |
| 903 | } |
| 904 | |
| 905 | int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) |
| 906 | { |
| 907 | BlockDriver *drv = bs->drv; |
| 908 | if (!drv) |
| 909 | return -ENOENT; |
| 910 | if (!drv->bdrv_snapshot_delete) |
| 911 | return -ENOTSUP; |
| 912 | return drv->bdrv_snapshot_delete(bs, snapshot_id); |
| 913 | } |
| 914 | |
| 915 | int bdrv_snapshot_list(BlockDriverState *bs, |
| 916 | QEMUSnapshotInfo **psn_info) |
| 917 | { |
| 918 | BlockDriver *drv = bs->drv; |
| 919 | if (!drv) |
| 920 | return -ENOENT; |
| 921 | if (!drv->bdrv_snapshot_list) |
| 922 | return -ENOTSUP; |
| 923 | return drv->bdrv_snapshot_list(bs, psn_info); |
| 924 | } |
| 925 | |
| 926 | #define NB_SUFFIXES 4 |
| 927 | |
| 928 | char *get_human_readable_size(char *buf, int buf_size, int64_t size) |
| 929 | { |
| 930 | static const char suffixes[NB_SUFFIXES] = "KMGT"; |
| 931 | int64_t base; |
| 932 | int i; |
| 933 | |
| 934 | if (size <= 999) { |
| 935 | snprintf(buf, buf_size, "%" PRId64, size); |
| 936 | } else { |
| 937 | base = 1024; |
| 938 | for(i = 0; i < NB_SUFFIXES; i++) { |
| 939 | if (size < (10 * base)) { |
| 940 | snprintf(buf, buf_size, "%0.1f%c", |
| 941 | (double)size / base, |
| 942 | suffixes[i]); |
| 943 | break; |
| 944 | } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { |
| 945 | snprintf(buf, buf_size, "%" PRId64 "%c", |
| 946 | ((size + (base >> 1)) / base), |
| 947 | suffixes[i]); |
| 948 | break; |
| 949 | } |
| 950 | base = base * 1024; |
| 951 | } |
| 952 | } |
| 953 | return buf; |
| 954 | } |
| 955 | |
| 956 | char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) |
| 957 | { |
| 958 | char buf1[128], date_buf[128], clock_buf[128]; |
| 959 | struct tm tm; |
| 960 | time_t ti; |
| 961 | int64_t secs; |
| 962 | |
| 963 | if (!sn) { |
| 964 | snprintf(buf, buf_size, |
| 965 | "%-10s%-20s%7s%20s%15s", |
| 966 | "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); |
| 967 | } else { |
| 968 | ti = sn->date_sec; |
| 969 | localtime_r(&ti, &tm); |
| 970 | strftime(date_buf, sizeof(date_buf), |
| 971 | "%Y-%m-%d %H:%M:%S", &tm); |
| 972 | secs = sn->vm_clock_nsec / 1000000000; |
| 973 | snprintf(clock_buf, sizeof(clock_buf), |
| 974 | "%02d:%02d:%02d.%03d", |
| 975 | (int)(secs / 3600), |
| 976 | (int)((secs / 60) % 60), |
| 977 | (int)(secs % 60), |
| 978 | (int)((sn->vm_clock_nsec / 1000000) % 1000)); |
| 979 | snprintf(buf, buf_size, |
| 980 | "%-10s%-20s%7s%20s%15s", |
| 981 | sn->id_str, sn->name, |
| 982 | get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), |
| 983 | date_buf, |
| 984 | clock_buf); |
| 985 | } |
| 986 | return buf; |
| 987 | } |
| 988 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 989 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 990 | /**************************************************************/ |
| 991 | /* async I/Os */ |
| 992 | |
| 993 | BlockDriverAIOCB *bdrv_aio_new(BlockDriverState *bs) |
| 994 | { |
| 995 | BlockDriver *drv = bs->drv; |
| 996 | BlockDriverAIOCB *acb; |
| 997 | acb = qemu_mallocz(sizeof(BlockDriverAIOCB)); |
| 998 | if (!acb) |
| 999 | return NULL; |
| 1000 | |
| 1001 | acb->bs = bs; |
| 1002 | if (drv->bdrv_aio_new(acb) < 0) { |
| 1003 | qemu_free(acb); |
| 1004 | return NULL; |
| 1005 | } |
| 1006 | return acb; |
| 1007 | } |
| 1008 | |
| 1009 | int bdrv_aio_read(BlockDriverAIOCB *acb, int64_t sector_num, |
| 1010 | uint8_t *buf, int nb_sectors, |
| 1011 | BlockDriverCompletionFunc *cb, void *opaque) |
| 1012 | { |
| 1013 | BlockDriverState *bs = acb->bs; |
| 1014 | BlockDriver *drv = bs->drv; |
| 1015 | |
| 1016 | if (!bs->inserted) |
| 1017 | return -1; |
| 1018 | |
| 1019 | /* XXX: we assume that nb_sectors == 0 is suppored by the async read */ |
| 1020 | if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { |
| 1021 | memcpy(buf, bs->boot_sector_data, 512); |
| 1022 | sector_num++; |
| 1023 | nb_sectors--; |
| 1024 | buf += 512; |
| 1025 | } |
| 1026 | |
| 1027 | acb->cb = cb; |
| 1028 | acb->cb_opaque = opaque; |
| 1029 | return drv->bdrv_aio_read(acb, sector_num, buf, nb_sectors); |
| 1030 | } |
| 1031 | |
| 1032 | int bdrv_aio_write(BlockDriverAIOCB *acb, int64_t sector_num, |
| 1033 | const uint8_t *buf, int nb_sectors, |
| 1034 | BlockDriverCompletionFunc *cb, void *opaque) |
| 1035 | { |
| 1036 | BlockDriverState *bs = acb->bs; |
| 1037 | BlockDriver *drv = bs->drv; |
| 1038 | |
| 1039 | if (!bs->inserted) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1040 | return -1; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1041 | if (bs->read_only) |
| 1042 | return -1; |
| 1043 | if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { |
| 1044 | memcpy(bs->boot_sector_data, buf, 512); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1045 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1046 | |
| 1047 | acb->cb = cb; |
| 1048 | acb->cb_opaque = opaque; |
| 1049 | return drv->bdrv_aio_write(acb, sector_num, buf, nb_sectors); |
| 1050 | } |
| 1051 | |
| 1052 | void bdrv_aio_cancel(BlockDriverAIOCB *acb) |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 1053 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1054 | BlockDriverState *bs = acb->bs; |
| 1055 | BlockDriver *drv = bs->drv; |
| 1056 | |
| 1057 | drv->bdrv_aio_cancel(acb); |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 1058 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1059 | |
| 1060 | void bdrv_aio_delete(BlockDriverAIOCB *acb) |
| 1061 | { |
| 1062 | BlockDriverState *bs = acb->bs; |
| 1063 | BlockDriver *drv = bs->drv; |
| 1064 | |
| 1065 | drv->bdrv_aio_delete(acb); |
| 1066 | qemu_free(acb); |
| 1067 | } |
| 1068 | |
| 1069 | /**************************************************************/ |
| 1070 | /* async block device emulation */ |
| 1071 | |
| 1072 | #ifdef QEMU_TOOL |
| 1073 | static int bdrv_aio_new_em(BlockDriverAIOCB *acb) |
| 1074 | { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1075 | return 0; |
| 1076 | } |
| 1077 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1078 | static int bdrv_aio_read_em(BlockDriverAIOCB *acb, int64_t sector_num, |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1079 | uint8_t *buf, int nb_sectors) |
| 1080 | { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1081 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1082 | ret = bdrv_read(acb->bs, sector_num, buf, nb_sectors); |
| 1083 | acb->cb(acb->cb_opaque, ret); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1084 | return 0; |
| 1085 | } |
| 1086 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1087 | static int bdrv_aio_write_em(BlockDriverAIOCB *acb, int64_t sector_num, |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1088 | const uint8_t *buf, int nb_sectors) |
| 1089 | { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1090 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1091 | ret = bdrv_write(acb->bs, sector_num, buf, nb_sectors); |
| 1092 | acb->cb(acb->cb_opaque, ret); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1093 | return 0; |
| 1094 | } |
| 1095 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1096 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1097 | { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1100 | static void bdrv_aio_delete_em(BlockDriverAIOCB *acb) |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 1101 | { |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 1102 | } |
| 1103 | #else |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1104 | typedef struct BlockDriverAIOCBSync { |
| 1105 | QEMUBH *bh; |
| 1106 | int ret; |
| 1107 | } BlockDriverAIOCBSync; |
| 1108 | |
| 1109 | static void bdrv_aio_bh_cb(void *opaque) |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 1110 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1111 | BlockDriverAIOCB *acb = opaque; |
| 1112 | BlockDriverAIOCBSync *acb1 = acb->opaque; |
| 1113 | acb->cb(acb->cb_opaque, acb1->ret); |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 1114 | } |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 1115 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1116 | static int bdrv_aio_new_em(BlockDriverAIOCB *acb) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1117 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1118 | BlockDriverAIOCBSync *acb1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1119 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1120 | acb1 = qemu_mallocz(sizeof(BlockDriverAIOCBSync)); |
| 1121 | if (!acb1) |
| 1122 | return -1; |
| 1123 | acb->opaque = acb1; |
| 1124 | acb1->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1125 | return 0; |
| 1126 | } |
| 1127 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1128 | static int bdrv_aio_read_em(BlockDriverAIOCB *acb, int64_t sector_num, |
| 1129 | uint8_t *buf, int nb_sectors) |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 1130 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1131 | BlockDriverAIOCBSync *acb1 = acb->opaque; |
| 1132 | int ret; |
| 1133 | |
| 1134 | ret = bdrv_read(acb->bs, sector_num, buf, nb_sectors); |
| 1135 | acb1->ret = ret; |
| 1136 | qemu_bh_schedule(acb1->bh); |
| 1137 | return 0; |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1140 | static int bdrv_aio_write_em(BlockDriverAIOCB *acb, int64_t sector_num, |
| 1141 | const uint8_t *buf, int nb_sectors) |
| 1142 | { |
| 1143 | BlockDriverAIOCBSync *acb1 = acb->opaque; |
| 1144 | int ret; |
| 1145 | |
| 1146 | ret = bdrv_write(acb->bs, sector_num, buf, nb_sectors); |
| 1147 | acb1->ret = ret; |
| 1148 | qemu_bh_schedule(acb1->bh); |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb) |
| 1153 | { |
| 1154 | BlockDriverAIOCBSync *acb1 = acb->opaque; |
| 1155 | qemu_bh_cancel(acb1->bh); |
| 1156 | } |
| 1157 | |
| 1158 | static void bdrv_aio_delete_em(BlockDriverAIOCB *acb) |
| 1159 | { |
| 1160 | BlockDriverAIOCBSync *acb1 = acb->opaque; |
| 1161 | qemu_bh_delete(acb1->bh); |
| 1162 | } |
| 1163 | #endif /* !QEMU_TOOL */ |
| 1164 | |
| 1165 | /**************************************************************/ |
| 1166 | /* sync block device emulation */ |
| 1167 | |
| 1168 | static void bdrv_rw_em_cb(void *opaque, int ret) |
| 1169 | { |
| 1170 | *(int *)opaque = ret; |
| 1171 | } |
| 1172 | |
| 1173 | #define NOT_DONE 0x7fffffff |
| 1174 | |
| 1175 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
| 1176 | uint8_t *buf, int nb_sectors) |
| 1177 | { |
| 1178 | int async_ret, ret; |
| 1179 | |
| 1180 | if (!bs->sync_aiocb) { |
| 1181 | bs->sync_aiocb = bdrv_aio_new(bs); |
| 1182 | if (!bs->sync_aiocb) |
| 1183 | return -1; |
| 1184 | } |
| 1185 | async_ret = NOT_DONE; |
| 1186 | qemu_aio_wait_start(); |
| 1187 | ret = bdrv_aio_read(bs->sync_aiocb, sector_num, buf, nb_sectors, |
| 1188 | bdrv_rw_em_cb, &async_ret); |
| 1189 | if (ret < 0) { |
| 1190 | qemu_aio_wait_end(); |
| 1191 | return ret; |
| 1192 | } |
| 1193 | while (async_ret == NOT_DONE) { |
| 1194 | qemu_aio_wait(); |
| 1195 | } |
| 1196 | qemu_aio_wait_end(); |
| 1197 | return async_ret; |
| 1198 | } |
| 1199 | |
| 1200 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
| 1201 | const uint8_t *buf, int nb_sectors) |
| 1202 | { |
| 1203 | int async_ret, ret; |
| 1204 | |
| 1205 | if (!bs->sync_aiocb) { |
| 1206 | bs->sync_aiocb = bdrv_aio_new(bs); |
| 1207 | if (!bs->sync_aiocb) |
| 1208 | return -1; |
| 1209 | } |
| 1210 | async_ret = NOT_DONE; |
| 1211 | qemu_aio_wait_start(); |
| 1212 | ret = bdrv_aio_write(bs->sync_aiocb, sector_num, buf, nb_sectors, |
| 1213 | bdrv_rw_em_cb, &async_ret); |
| 1214 | if (ret < 0) { |
| 1215 | qemu_aio_wait_end(); |
| 1216 | return ret; |
| 1217 | } |
| 1218 | while (async_ret == NOT_DONE) { |
| 1219 | qemu_aio_wait(); |
| 1220 | } |
| 1221 | qemu_aio_wait_end(); |
| 1222 | return async_ret; |
| 1223 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1224 | |
| 1225 | void bdrv_init(void) |
| 1226 | { |
| 1227 | bdrv_register(&bdrv_raw); |
| 1228 | #ifndef _WIN32 |
| 1229 | bdrv_register(&bdrv_cow); |
| 1230 | #endif |
| 1231 | bdrv_register(&bdrv_qcow); |
| 1232 | bdrv_register(&bdrv_vmdk); |
bellard | 3c56521 | 2004-09-29 21:29:14 +0000 | [diff] [blame] | 1233 | bdrv_register(&bdrv_cloop); |
bellard | 585d0ed | 2004-12-12 11:24:44 +0000 | [diff] [blame] | 1234 | bdrv_register(&bdrv_dmg); |
bellard | a8753c3 | 2005-04-26 21:34:00 +0000 | [diff] [blame] | 1235 | bdrv_register(&bdrv_bochs); |
bellard | 6a0f9e8 | 2005-04-27 20:17:58 +0000 | [diff] [blame] | 1236 | bdrv_register(&bdrv_vpc); |
bellard | 712e787 | 2005-04-28 21:09:32 +0000 | [diff] [blame] | 1237 | bdrv_register(&bdrv_vvfat); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame^] | 1238 | bdrv_register(&bdrv_qcow2); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1239 | } |