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