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