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 | */ |
blueswir1 | 3990d09 | 2008-12-05 17:53:21 +0000 | [diff] [blame] | 24 | #include "config-host.h" |
blueswir1 | 179a2c1 | 2009-03-08 08:23:32 +0000 | [diff] [blame] | 25 | #ifdef HOST_BSD |
blueswir1 | 3990d09 | 2008-12-05 17:53:21 +0000 | [diff] [blame] | 26 | /* include native header before sys-queue.h */ |
| 27 | #include <sys/queue.h> |
| 28 | #endif |
| 29 | |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 30 | #include "qemu-common.h" |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 31 | #include "monitor.h" |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 32 | #include "block_int.h" |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 33 | #include "module.h" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 34 | |
blueswir1 | 179a2c1 | 2009-03-08 08:23:32 +0000 | [diff] [blame] | 35 | #ifdef HOST_BSD |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 36 | #include <sys/types.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/ioctl.h> |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 39 | #ifndef __DragonFly__ |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 40 | #include <sys/disk.h> |
| 41 | #endif |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 42 | #endif |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 43 | |
aliguori | 49dc768 | 2009-03-08 16:26:59 +0000 | [diff] [blame] | 44 | #ifdef _WIN32 |
| 45 | #include <windows.h> |
| 46 | #endif |
| 47 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 48 | #define SECTOR_BITS 9 |
| 49 | #define SECTOR_SIZE (1 << SECTOR_BITS) |
bellard | 3b0d4f6 | 2005-10-30 18:30:10 +0000 | [diff] [blame] | 50 | |
bellard | 9076542 | 2006-08-07 19:10:16 +0000 | [diff] [blame] | 51 | typedef struct BlockDriverAIOCBSync { |
| 52 | BlockDriverAIOCB common; |
| 53 | QEMUBH *bh; |
| 54 | int ret; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 55 | /* vector translation state */ |
| 56 | QEMUIOVector *qiov; |
| 57 | uint8_t *bounce; |
| 58 | int is_write; |
bellard | 9076542 | 2006-08-07 19:10:16 +0000 | [diff] [blame] | 59 | } BlockDriverAIOCBSync; |
| 60 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 61 | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, |
| 62 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
aliguori | c87c067 | 2009-04-07 18:43:20 +0000 | [diff] [blame] | 63 | BlockDriverCompletionFunc *cb, void *opaque); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 64 | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, |
| 65 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 66 | BlockDriverCompletionFunc *cb, void *opaque); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 67 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 68 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 69 | uint8_t *buf, int nb_sectors); |
| 70 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
| 71 | const uint8_t *buf, int nb_sectors); |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 72 | |
blueswir1 | 7ee930d | 2008-09-17 19:04:14 +0000 | [diff] [blame] | 73 | BlockDriverState *bdrv_first; |
| 74 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 75 | static BlockDriver *first_drv; |
| 76 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 77 | int path_is_absolute(const char *path) |
| 78 | { |
| 79 | const char *p; |
bellard | 2166442 | 2007-01-07 18:22:37 +0000 | [diff] [blame] | 80 | #ifdef _WIN32 |
| 81 | /* specific case for names like: "\\.\d:" */ |
| 82 | if (*path == '/' || *path == '\\') |
| 83 | return 1; |
| 84 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 85 | p = strchr(path, ':'); |
| 86 | if (p) |
| 87 | p++; |
| 88 | else |
| 89 | p = path; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 90 | #ifdef _WIN32 |
| 91 | return (*p == '/' || *p == '\\'); |
| 92 | #else |
| 93 | return (*p == '/'); |
| 94 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* if filename is absolute, just copy it to dest. Otherwise, build a |
| 98 | path to it by considering it is relative to base_path. URL are |
| 99 | supported. */ |
| 100 | void path_combine(char *dest, int dest_size, |
| 101 | const char *base_path, |
| 102 | const char *filename) |
| 103 | { |
| 104 | const char *p, *p1; |
| 105 | int len; |
| 106 | |
| 107 | if (dest_size <= 0) |
| 108 | return; |
| 109 | if (path_is_absolute(filename)) { |
| 110 | pstrcpy(dest, dest_size, filename); |
| 111 | } else { |
| 112 | p = strchr(base_path, ':'); |
| 113 | if (p) |
| 114 | p++; |
| 115 | else |
| 116 | p = base_path; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 117 | p1 = strrchr(base_path, '/'); |
| 118 | #ifdef _WIN32 |
| 119 | { |
| 120 | const char *p2; |
| 121 | p2 = strrchr(base_path, '\\'); |
| 122 | if (!p1 || p2 > p1) |
| 123 | p1 = p2; |
| 124 | } |
| 125 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 126 | if (p1) |
| 127 | p1++; |
| 128 | else |
| 129 | p1 = base_path; |
| 130 | if (p1 > p) |
| 131 | p = p1; |
| 132 | len = p - base_path; |
| 133 | if (len > dest_size - 1) |
| 134 | len = dest_size - 1; |
| 135 | memcpy(dest, base_path, len); |
| 136 | dest[len] = '\0'; |
| 137 | pstrcat(dest, dest_size, filename); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 142 | void bdrv_register(BlockDriver *bdrv) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 143 | { |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 144 | if (!bdrv->bdrv_aio_readv) { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 145 | /* add AIO emulation layer */ |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 146 | bdrv->bdrv_aio_readv = bdrv_aio_readv_em; |
| 147 | bdrv->bdrv_aio_writev = bdrv_aio_writev_em; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 148 | bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em; |
bellard | 9076542 | 2006-08-07 19:10:16 +0000 | [diff] [blame] | 149 | bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync); |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 150 | } else if (!bdrv->bdrv_read) { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 151 | /* add synchronous IO emulation layer */ |
| 152 | bdrv->bdrv_read = bdrv_read_em; |
| 153 | bdrv->bdrv_write = bdrv_write_em; |
| 154 | } |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 155 | aio_pool_init(&bdrv->aio_pool, bdrv->aiocb_size, bdrv->bdrv_aio_cancel); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 156 | bdrv->next = first_drv; |
| 157 | first_drv = bdrv; |
| 158 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 159 | |
| 160 | /* create a new block device (by default it is empty) */ |
| 161 | BlockDriverState *bdrv_new(const char *device_name) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 162 | { |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 163 | BlockDriverState **pbs, *bs; |
| 164 | |
| 165 | bs = qemu_mallocz(sizeof(BlockDriverState)); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 166 | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 167 | if (device_name[0] != '\0') { |
| 168 | /* insert at the end */ |
| 169 | pbs = &bdrv_first; |
| 170 | while (*pbs != NULL) |
| 171 | pbs = &(*pbs)->next; |
| 172 | *pbs = bs; |
| 173 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 174 | return bs; |
| 175 | } |
| 176 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 177 | BlockDriver *bdrv_find_format(const char *format_name) |
| 178 | { |
| 179 | BlockDriver *drv1; |
| 180 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
| 181 | if (!strcmp(drv1->format_name, format_name)) |
| 182 | return drv1; |
| 183 | } |
| 184 | return NULL; |
| 185 | } |
| 186 | |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 187 | int bdrv_create(BlockDriver *drv, const char* filename, |
| 188 | QEMUOptionParameter *options) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 189 | { |
| 190 | if (!drv->bdrv_create) |
| 191 | return -ENOTSUP; |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 192 | |
| 193 | return drv->bdrv_create(filename, options); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 194 | } |
| 195 | |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 196 | #ifdef _WIN32 |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 197 | void get_tmp_filename(char *filename, int size) |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 198 | { |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 199 | char temp_dir[MAX_PATH]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 200 | |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 201 | GetTempPath(MAX_PATH, temp_dir); |
| 202 | GetTempFileName(temp_dir, "qem", 0, filename); |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 203 | } |
| 204 | #else |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 205 | void get_tmp_filename(char *filename, int size) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 206 | { |
| 207 | int fd; |
blueswir1 | 7ccfb2e | 2008-09-14 06:45:34 +0000 | [diff] [blame] | 208 | const char *tmpdir; |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 209 | /* XXX: race condition possible */ |
aurel32 | 0badc1e | 2008-03-10 00:05:34 +0000 | [diff] [blame] | 210 | tmpdir = getenv("TMPDIR"); |
| 211 | if (!tmpdir) |
| 212 | tmpdir = "/tmp"; |
| 213 | snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 214 | fd = mkstemp(filename); |
| 215 | close(fd); |
| 216 | } |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 217 | #endif |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 218 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 219 | #ifdef _WIN32 |
bellard | f45512f | 2006-08-23 21:40:13 +0000 | [diff] [blame] | 220 | static int is_windows_drive_prefix(const char *filename) |
| 221 | { |
| 222 | return (((filename[0] >= 'a' && filename[0] <= 'z') || |
| 223 | (filename[0] >= 'A' && filename[0] <= 'Z')) && |
| 224 | filename[1] == ':'); |
| 225 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 226 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 227 | static int is_windows_drive(const char *filename) |
| 228 | { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 229 | if (is_windows_drive_prefix(filename) && |
bellard | f45512f | 2006-08-23 21:40:13 +0000 | [diff] [blame] | 230 | filename[2] == '\0') |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 231 | return 1; |
| 232 | if (strstart(filename, "\\\\.\\", NULL) || |
| 233 | strstart(filename, "//./", NULL)) |
| 234 | return 1; |
| 235 | return 0; |
| 236 | } |
| 237 | #endif |
| 238 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 239 | static BlockDriver *find_protocol(const char *filename) |
| 240 | { |
| 241 | BlockDriver *drv1; |
| 242 | char protocol[128]; |
| 243 | int len; |
| 244 | const char *p; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 245 | |
| 246 | #ifdef _WIN32 |
bellard | f45512f | 2006-08-23 21:40:13 +0000 | [diff] [blame] | 247 | if (is_windows_drive(filename) || |
| 248 | is_windows_drive_prefix(filename)) |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 249 | return bdrv_find_format("raw"); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 250 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 251 | p = strchr(filename, ':'); |
| 252 | if (!p) |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 253 | return bdrv_find_format("raw"); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 254 | len = p - filename; |
| 255 | if (len > sizeof(protocol) - 1) |
| 256 | len = sizeof(protocol) - 1; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 257 | memcpy(protocol, filename, len); |
| 258 | protocol[len] = '\0'; |
| 259 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 260 | if (drv1->protocol_name && |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 261 | !strcmp(drv1->protocol_name, protocol)) |
| 262 | return drv1; |
| 263 | } |
| 264 | return NULL; |
| 265 | } |
| 266 | |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 267 | /* XXX: force raw format if block or character device ? It would |
| 268 | simplify the BSD case */ |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 269 | static BlockDriver *find_image_format(const char *filename) |
| 270 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 271 | int ret, score, score_max; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 272 | BlockDriver *drv1, *drv; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 273 | uint8_t buf[2048]; |
| 274 | BlockDriverState *bs; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 275 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 276 | /* detect host devices. By convention, /dev/cdrom[N] is always |
| 277 | recognized as a host CDROM */ |
| 278 | if (strstart(filename, "/dev/cdrom", NULL)) |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 279 | return bdrv_find_format("host_device"); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 280 | #ifdef _WIN32 |
| 281 | if (is_windows_drive(filename)) |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 282 | return bdrv_find_format("host_device"); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 283 | #else |
| 284 | { |
| 285 | struct stat st; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 286 | if (stat(filename, &st) >= 0 && |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 287 | (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 288 | return bdrv_find_format("host_device"); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | #endif |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 292 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 293 | drv = find_protocol(filename); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 294 | /* no need to test disk image formats for vvfat */ |
Anthony Liguori | c833ab7 | 2009-05-22 08:17:55 -0500 | [diff] [blame] | 295 | if (drv && strcmp(drv->format_name, "vvfat") == 0) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 296 | return drv; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 297 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 298 | ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY); |
| 299 | if (ret < 0) |
| 300 | return NULL; |
| 301 | ret = bdrv_pread(bs, 0, buf, sizeof(buf)); |
| 302 | bdrv_delete(bs); |
| 303 | if (ret < 0) { |
| 304 | return NULL; |
| 305 | } |
| 306 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 307 | score_max = 0; |
| 308 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 309 | if (drv1->bdrv_probe) { |
| 310 | score = drv1->bdrv_probe(buf, ret, filename); |
| 311 | if (score > score_max) { |
| 312 | score_max = score; |
| 313 | drv = drv1; |
| 314 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | return drv; |
| 318 | } |
| 319 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 320 | int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 321 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 322 | BlockDriverState *bs; |
| 323 | int ret; |
| 324 | |
| 325 | bs = bdrv_new(""); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 326 | ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL); |
| 327 | if (ret < 0) { |
| 328 | bdrv_delete(bs); |
| 329 | return ret; |
bellard | 3b0d4f6 | 2005-10-30 18:30:10 +0000 | [diff] [blame] | 330 | } |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 331 | bs->growable = 1; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 332 | *pbs = bs; |
| 333 | return 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 334 | } |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 335 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 336 | int bdrv_open(BlockDriverState *bs, const char *filename, int flags) |
| 337 | { |
| 338 | return bdrv_open2(bs, filename, flags, NULL); |
| 339 | } |
| 340 | |
| 341 | int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 342 | BlockDriver *drv) |
| 343 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 344 | int ret, open_flags; |
ths | eb5c851 | 2007-02-11 15:06:09 +0000 | [diff] [blame] | 345 | char tmp_filename[PATH_MAX]; |
| 346 | char backing_filename[PATH_MAX]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 347 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 348 | bs->read_only = 0; |
| 349 | bs->is_temporary = 0; |
| 350 | bs->encrypted = 0; |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 351 | bs->valid_key = 0; |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 352 | /* buffer_alignment defaulted to 512, drivers can change this value */ |
| 353 | bs->buffer_alignment = 512; |
bellard | 712e787 | 2005-04-28 21:09:32 +0000 | [diff] [blame] | 354 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 355 | if (flags & BDRV_O_SNAPSHOT) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 356 | BlockDriverState *bs1; |
| 357 | int64_t total_size; |
aliguori | 7c96d46 | 2008-09-12 17:54:13 +0000 | [diff] [blame] | 358 | int is_protocol = 0; |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame^] | 359 | BlockDriver *bdrv_qcow2; |
| 360 | QEMUOptionParameter *options; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 361 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 362 | /* if snapshot, we create a temporary backing file and open it |
| 363 | instead of opening 'filename' directly */ |
| 364 | |
| 365 | /* if there is a backing file, use it */ |
| 366 | bs1 = bdrv_new(""); |
aliguori | 5eb4563 | 2009-03-28 17:55:10 +0000 | [diff] [blame] | 367 | ret = bdrv_open2(bs1, filename, 0, drv); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 368 | if (ret < 0) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 369 | bdrv_delete(bs1); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 370 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 371 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 372 | total_size = bdrv_getlength(bs1) >> SECTOR_BITS; |
aliguori | 7c96d46 | 2008-09-12 17:54:13 +0000 | [diff] [blame] | 373 | |
| 374 | if (bs1->drv && bs1->drv->protocol_name) |
| 375 | is_protocol = 1; |
| 376 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 377 | bdrv_delete(bs1); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 378 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 379 | get_tmp_filename(tmp_filename, sizeof(tmp_filename)); |
aliguori | 7c96d46 | 2008-09-12 17:54:13 +0000 | [diff] [blame] | 380 | |
| 381 | /* Real path is meaningless for protocols */ |
| 382 | if (is_protocol) |
| 383 | snprintf(backing_filename, sizeof(backing_filename), |
| 384 | "%s", filename); |
| 385 | else |
| 386 | realpath(filename, backing_filename); |
| 387 | |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame^] | 388 | bdrv_qcow2 = bdrv_find_format("qcow2"); |
| 389 | options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); |
| 390 | |
| 391 | set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512); |
| 392 | set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); |
| 393 | if (drv) { |
| 394 | set_option_parameter(options, BLOCK_OPT_BACKING_FMT, |
| 395 | drv->format_name); |
| 396 | } |
| 397 | |
| 398 | ret = bdrv_create(bdrv_qcow2, tmp_filename, options); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 399 | if (ret < 0) { |
| 400 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 401 | } |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame^] | 402 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 403 | filename = tmp_filename; |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame^] | 404 | drv = bdrv_qcow2; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 405 | bs->is_temporary = 1; |
| 406 | } |
bellard | 712e787 | 2005-04-28 21:09:32 +0000 | [diff] [blame] | 407 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 408 | pstrcpy(bs->filename, sizeof(bs->filename), filename); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 409 | if (flags & BDRV_O_FILE) { |
| 410 | drv = find_protocol(filename); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 411 | } else if (!drv) { |
| 412 | drv = find_image_format(filename); |
| 413 | } |
| 414 | if (!drv) { |
| 415 | ret = -ENOENT; |
| 416 | goto unlink_and_fail; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 417 | } |
| 418 | bs->drv = drv; |
| 419 | bs->opaque = qemu_mallocz(drv->instance_size); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 420 | /* Note: for compatibility, we open disk image files as RDWR, and |
| 421 | RDONLY as fallback */ |
| 422 | if (!(flags & BDRV_O_FILE)) |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 423 | open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 424 | else |
| 425 | open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); |
| 426 | ret = drv->bdrv_open(bs, filename, open_flags); |
aurel32 | a0a8353 | 2008-10-13 21:08:34 +0000 | [diff] [blame] | 427 | if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) { |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 428 | ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 429 | bs->read_only = 1; |
| 430 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 431 | if (ret < 0) { |
| 432 | qemu_free(bs->opaque); |
bellard | 6b21b97 | 2006-08-23 21:14:37 +0000 | [diff] [blame] | 433 | bs->opaque = NULL; |
| 434 | bs->drv = NULL; |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 435 | unlink_and_fail: |
| 436 | if (bs->is_temporary) |
| 437 | unlink(filename); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 438 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 439 | } |
bellard | d15a771 | 2006-08-06 13:35:09 +0000 | [diff] [blame] | 440 | if (drv->bdrv_getlength) { |
| 441 | bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; |
| 442 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 443 | #ifndef _WIN32 |
| 444 | if (bs->is_temporary) { |
| 445 | unlink(filename); |
| 446 | } |
| 447 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 448 | if (bs->backing_file[0] != '\0') { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 449 | /* if there is a backing file, use it */ |
aliguori | 5eb4563 | 2009-03-28 17:55:10 +0000 | [diff] [blame] | 450 | BlockDriver *back_drv = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 451 | bs->backing_hd = bdrv_new(""); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 452 | path_combine(backing_filename, sizeof(backing_filename), |
| 453 | filename, bs->backing_file); |
aliguori | 5eb4563 | 2009-03-28 17:55:10 +0000 | [diff] [blame] | 454 | if (bs->backing_format[0] != '\0') |
| 455 | back_drv = bdrv_find_format(bs->backing_format); |
| 456 | ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags, |
| 457 | back_drv); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 458 | if (ret < 0) { |
| 459 | bdrv_close(bs); |
| 460 | return ret; |
| 461 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 462 | } |
| 463 | |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 464 | if (!bdrv_key_required(bs)) { |
| 465 | /* call the change callback */ |
| 466 | bs->media_changed = 1; |
| 467 | if (bs->change_cb) |
| 468 | bs->change_cb(bs->change_opaque); |
| 469 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 470 | return 0; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | void bdrv_close(BlockDriverState *bs) |
| 474 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 475 | if (bs->drv) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 476 | if (bs->backing_hd) |
| 477 | bdrv_delete(bs->backing_hd); |
| 478 | bs->drv->bdrv_close(bs); |
| 479 | qemu_free(bs->opaque); |
| 480 | #ifdef _WIN32 |
| 481 | if (bs->is_temporary) { |
| 482 | unlink(bs->filename); |
| 483 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 484 | #endif |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 485 | bs->opaque = NULL; |
| 486 | bs->drv = NULL; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 487 | |
| 488 | /* call the change callback */ |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 489 | bs->media_changed = 1; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 490 | if (bs->change_cb) |
| 491 | bs->change_cb(bs->change_opaque); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | void bdrv_delete(BlockDriverState *bs) |
| 496 | { |
aurel32 | 34c6f05 | 2008-04-08 19:51:21 +0000 | [diff] [blame] | 497 | BlockDriverState **pbs; |
| 498 | |
| 499 | pbs = &bdrv_first; |
| 500 | while (*pbs != bs && *pbs != NULL) |
| 501 | pbs = &(*pbs)->next; |
| 502 | if (*pbs == bs) |
| 503 | *pbs = bs->next; |
| 504 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 505 | bdrv_close(bs); |
| 506 | qemu_free(bs); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 507 | } |
| 508 | |
aliguori | e97fc19 | 2009-04-21 23:11:50 +0000 | [diff] [blame] | 509 | /* |
| 510 | * Run consistency checks on an image |
| 511 | * |
| 512 | * Returns the number of errors or -errno when an internal error occurs |
| 513 | */ |
| 514 | int bdrv_check(BlockDriverState *bs) |
| 515 | { |
| 516 | if (bs->drv->bdrv_check == NULL) { |
| 517 | return -ENOTSUP; |
| 518 | } |
| 519 | |
| 520 | return bs->drv->bdrv_check(bs); |
| 521 | } |
| 522 | |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 523 | /* commit COW file into the raw image */ |
| 524 | int bdrv_commit(BlockDriverState *bs) |
| 525 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 526 | BlockDriver *drv = bs->drv; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 527 | int64_t i, total_sectors; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 528 | int n, j; |
| 529 | unsigned char sector[512]; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 530 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 531 | if (!drv) |
| 532 | return -ENOMEDIUM; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 533 | |
| 534 | if (bs->read_only) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 535 | return -EACCES; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 536 | } |
| 537 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 538 | if (!bs->backing_hd) { |
| 539 | return -ENOTSUP; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 540 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 541 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 542 | total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; |
| 543 | for (i = 0; i < total_sectors;) { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 544 | if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 545 | for(j = 0; j < n; j++) { |
| 546 | if (bdrv_read(bs, i, sector, 1) != 0) { |
| 547 | return -EIO; |
| 548 | } |
| 549 | |
| 550 | if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { |
| 551 | return -EIO; |
| 552 | } |
| 553 | i++; |
| 554 | } |
| 555 | } else { |
| 556 | i += n; |
| 557 | } |
| 558 | } |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 559 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 560 | if (drv->bdrv_make_empty) |
| 561 | return drv->bdrv_make_empty(bs); |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 562 | |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 563 | return 0; |
| 564 | } |
| 565 | |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 566 | static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, |
| 567 | size_t size) |
| 568 | { |
| 569 | int64_t len; |
| 570 | |
| 571 | if (!bdrv_is_inserted(bs)) |
| 572 | return -ENOMEDIUM; |
| 573 | |
| 574 | if (bs->growable) |
| 575 | return 0; |
| 576 | |
| 577 | len = bdrv_getlength(bs); |
| 578 | |
Kevin Wolf | fbb7b4e | 2009-05-08 14:47:24 +0200 | [diff] [blame] | 579 | if (offset < 0) |
| 580 | return -EIO; |
| 581 | |
| 582 | if ((offset > len) || (len - offset < size)) |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 583 | return -EIO; |
| 584 | |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, |
| 589 | int nb_sectors) |
| 590 | { |
aliguori | 999dec5 | 2009-03-29 01:31:48 +0000 | [diff] [blame] | 591 | return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512); |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 592 | } |
| 593 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 594 | /* return < 0 if error. See bdrv_write() for the return codes */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 595 | int bdrv_read(BlockDriverState *bs, int64_t sector_num, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 596 | uint8_t *buf, int nb_sectors) |
| 597 | { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 598 | BlockDriver *drv = bs->drv; |
| 599 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 600 | if (!drv) |
| 601 | return -ENOMEDIUM; |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 602 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 603 | return -EIO; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 604 | |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 605 | return drv->bdrv_read(bs, sector_num, buf, nb_sectors); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 606 | } |
| 607 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 608 | /* Return < 0 if error. Important errors are: |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 609 | -EIO generic I/O error (may happen for all errors) |
| 610 | -ENOMEDIUM No media inserted. |
| 611 | -EINVAL Invalid sector number or nb_sectors |
| 612 | -EACCES Trying to write a read-only device |
| 613 | */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 614 | int bdrv_write(BlockDriverState *bs, int64_t sector_num, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 615 | const uint8_t *buf, int nb_sectors) |
| 616 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 617 | BlockDriver *drv = bs->drv; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 618 | if (!bs->drv) |
| 619 | return -ENOMEDIUM; |
bellard | 0849bf0 | 2003-06-30 23:17:31 +0000 | [diff] [blame] | 620 | if (bs->read_only) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 621 | return -EACCES; |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 622 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 623 | return -EIO; |
| 624 | |
aliguori | 42fb280 | 2009-01-15 20:43:39 +0000 | [diff] [blame] | 625 | return drv->bdrv_write(bs, sector_num, buf, nb_sectors); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 626 | } |
| 627 | |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 628 | int bdrv_pread(BlockDriverState *bs, int64_t offset, |
| 629 | void *buf, int count1) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 630 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 631 | uint8_t tmp_buf[SECTOR_SIZE]; |
| 632 | int len, nb_sectors, count; |
| 633 | int64_t sector_num; |
| 634 | |
| 635 | count = count1; |
| 636 | /* first read to align to sector start */ |
| 637 | len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); |
| 638 | if (len > count) |
| 639 | len = count; |
| 640 | sector_num = offset >> SECTOR_BITS; |
| 641 | if (len > 0) { |
| 642 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
| 643 | return -EIO; |
| 644 | memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len); |
| 645 | count -= len; |
| 646 | if (count == 0) |
| 647 | return count1; |
| 648 | sector_num++; |
| 649 | buf += len; |
| 650 | } |
| 651 | |
| 652 | /* read the sectors "in place" */ |
| 653 | nb_sectors = count >> SECTOR_BITS; |
| 654 | if (nb_sectors > 0) { |
| 655 | if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0) |
| 656 | return -EIO; |
| 657 | sector_num += nb_sectors; |
| 658 | len = nb_sectors << SECTOR_BITS; |
| 659 | buf += len; |
| 660 | count -= len; |
| 661 | } |
| 662 | |
| 663 | /* add data from the last sector */ |
| 664 | if (count > 0) { |
| 665 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
| 666 | return -EIO; |
| 667 | memcpy(buf, tmp_buf, count); |
| 668 | } |
| 669 | return count1; |
| 670 | } |
| 671 | |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 672 | int bdrv_pwrite(BlockDriverState *bs, int64_t offset, |
| 673 | const void *buf, int count1) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 674 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 675 | uint8_t tmp_buf[SECTOR_SIZE]; |
| 676 | int len, nb_sectors, count; |
| 677 | int64_t sector_num; |
| 678 | |
| 679 | count = count1; |
| 680 | /* first write to align to sector start */ |
| 681 | len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); |
| 682 | if (len > count) |
| 683 | len = count; |
| 684 | sector_num = offset >> SECTOR_BITS; |
| 685 | if (len > 0) { |
| 686 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
| 687 | return -EIO; |
| 688 | memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len); |
| 689 | if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) |
| 690 | return -EIO; |
| 691 | count -= len; |
| 692 | if (count == 0) |
| 693 | return count1; |
| 694 | sector_num++; |
| 695 | buf += len; |
| 696 | } |
| 697 | |
| 698 | /* write the sectors "in place" */ |
| 699 | nb_sectors = count >> SECTOR_BITS; |
| 700 | if (nb_sectors > 0) { |
| 701 | if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0) |
| 702 | return -EIO; |
| 703 | sector_num += nb_sectors; |
| 704 | len = nb_sectors << SECTOR_BITS; |
| 705 | buf += len; |
| 706 | count -= len; |
| 707 | } |
| 708 | |
| 709 | /* add data from the last sector */ |
| 710 | if (count > 0) { |
| 711 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
| 712 | return -EIO; |
| 713 | memcpy(tmp_buf, buf, count); |
| 714 | if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) |
| 715 | return -EIO; |
| 716 | } |
| 717 | return count1; |
| 718 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 719 | |
| 720 | /** |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 721 | * Truncate file to 'offset' bytes (needed only for file protocols) |
| 722 | */ |
| 723 | int bdrv_truncate(BlockDriverState *bs, int64_t offset) |
| 724 | { |
| 725 | BlockDriver *drv = bs->drv; |
| 726 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 727 | return -ENOMEDIUM; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 728 | if (!drv->bdrv_truncate) |
| 729 | return -ENOTSUP; |
| 730 | return drv->bdrv_truncate(bs, offset); |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * Length of a file in bytes. Return < 0 if error or unknown. |
| 735 | */ |
| 736 | int64_t bdrv_getlength(BlockDriverState *bs) |
| 737 | { |
| 738 | BlockDriver *drv = bs->drv; |
| 739 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 740 | return -ENOMEDIUM; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 741 | if (!drv->bdrv_getlength) { |
| 742 | /* legacy mode */ |
| 743 | return bs->total_sectors * SECTOR_SIZE; |
| 744 | } |
| 745 | return drv->bdrv_getlength(bs); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 746 | } |
| 747 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 748 | /* return 0 as number of sectors if no device present or error */ |
ths | 96b8f13 | 2007-12-17 01:35:20 +0000 | [diff] [blame] | 749 | void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 750 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 751 | int64_t length; |
| 752 | length = bdrv_getlength(bs); |
| 753 | if (length < 0) |
| 754 | length = 0; |
| 755 | else |
| 756 | length = length >> SECTOR_BITS; |
| 757 | *nb_sectors_ptr = length; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 758 | } |
bellard | cf98951 | 2004-02-16 21:56:36 +0000 | [diff] [blame] | 759 | |
aliguori | f3d54fc | 2008-11-25 21:50:24 +0000 | [diff] [blame] | 760 | struct partition { |
| 761 | uint8_t boot_ind; /* 0x80 - active */ |
| 762 | uint8_t head; /* starting head */ |
| 763 | uint8_t sector; /* starting sector */ |
| 764 | uint8_t cyl; /* starting cylinder */ |
| 765 | uint8_t sys_ind; /* What partition type */ |
| 766 | uint8_t end_head; /* end head */ |
| 767 | uint8_t end_sector; /* end sector */ |
| 768 | uint8_t end_cyl; /* end cylinder */ |
| 769 | uint32_t start_sect; /* starting sector counting from 0 */ |
| 770 | uint32_t nr_sects; /* nr of sectors in partition */ |
| 771 | } __attribute__((packed)); |
| 772 | |
| 773 | /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ |
| 774 | static int guess_disk_lchs(BlockDriverState *bs, |
| 775 | int *pcylinders, int *pheads, int *psectors) |
| 776 | { |
| 777 | uint8_t buf[512]; |
| 778 | int ret, i, heads, sectors, cylinders; |
| 779 | struct partition *p; |
| 780 | uint32_t nr_sects; |
blueswir1 | a38131b | 2008-12-05 17:56:40 +0000 | [diff] [blame] | 781 | uint64_t nb_sectors; |
aliguori | f3d54fc | 2008-11-25 21:50:24 +0000 | [diff] [blame] | 782 | |
| 783 | bdrv_get_geometry(bs, &nb_sectors); |
| 784 | |
| 785 | ret = bdrv_read(bs, 0, buf, 1); |
| 786 | if (ret < 0) |
| 787 | return -1; |
| 788 | /* test msdos magic */ |
| 789 | if (buf[510] != 0x55 || buf[511] != 0xaa) |
| 790 | return -1; |
| 791 | for(i = 0; i < 4; i++) { |
| 792 | p = ((struct partition *)(buf + 0x1be)) + i; |
| 793 | nr_sects = le32_to_cpu(p->nr_sects); |
| 794 | if (nr_sects && p->end_head) { |
| 795 | /* We make the assumption that the partition terminates on |
| 796 | a cylinder boundary */ |
| 797 | heads = p->end_head + 1; |
| 798 | sectors = p->end_sector & 63; |
| 799 | if (sectors == 0) |
| 800 | continue; |
| 801 | cylinders = nb_sectors / (heads * sectors); |
| 802 | if (cylinders < 1 || cylinders > 16383) |
| 803 | continue; |
| 804 | *pheads = heads; |
| 805 | *psectors = sectors; |
| 806 | *pcylinders = cylinders; |
| 807 | #if 0 |
| 808 | printf("guessed geometry: LCHS=%d %d %d\n", |
| 809 | cylinders, heads, sectors); |
| 810 | #endif |
| 811 | return 0; |
| 812 | } |
| 813 | } |
| 814 | return -1; |
| 815 | } |
| 816 | |
| 817 | void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) |
| 818 | { |
| 819 | int translation, lba_detected = 0; |
| 820 | int cylinders, heads, secs; |
blueswir1 | a38131b | 2008-12-05 17:56:40 +0000 | [diff] [blame] | 821 | uint64_t nb_sectors; |
aliguori | f3d54fc | 2008-11-25 21:50:24 +0000 | [diff] [blame] | 822 | |
| 823 | /* if a geometry hint is available, use it */ |
| 824 | bdrv_get_geometry(bs, &nb_sectors); |
| 825 | bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); |
| 826 | translation = bdrv_get_translation_hint(bs); |
| 827 | if (cylinders != 0) { |
| 828 | *pcyls = cylinders; |
| 829 | *pheads = heads; |
| 830 | *psecs = secs; |
| 831 | } else { |
| 832 | if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { |
| 833 | if (heads > 16) { |
| 834 | /* if heads > 16, it means that a BIOS LBA |
| 835 | translation was active, so the default |
| 836 | hardware geometry is OK */ |
| 837 | lba_detected = 1; |
| 838 | goto default_geometry; |
| 839 | } else { |
| 840 | *pcyls = cylinders; |
| 841 | *pheads = heads; |
| 842 | *psecs = secs; |
| 843 | /* disable any translation to be in sync with |
| 844 | the logical geometry */ |
| 845 | if (translation == BIOS_ATA_TRANSLATION_AUTO) { |
| 846 | bdrv_set_translation_hint(bs, |
| 847 | BIOS_ATA_TRANSLATION_NONE); |
| 848 | } |
| 849 | } |
| 850 | } else { |
| 851 | default_geometry: |
| 852 | /* if no geometry, use a standard physical disk geometry */ |
| 853 | cylinders = nb_sectors / (16 * 63); |
| 854 | |
| 855 | if (cylinders > 16383) |
| 856 | cylinders = 16383; |
| 857 | else if (cylinders < 2) |
| 858 | cylinders = 2; |
| 859 | *pcyls = cylinders; |
| 860 | *pheads = 16; |
| 861 | *psecs = 63; |
| 862 | if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { |
| 863 | if ((*pcyls * *pheads) <= 131072) { |
| 864 | bdrv_set_translation_hint(bs, |
| 865 | BIOS_ATA_TRANSLATION_LARGE); |
| 866 | } else { |
| 867 | bdrv_set_translation_hint(bs, |
| 868 | BIOS_ATA_TRANSLATION_LBA); |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); |
| 873 | } |
| 874 | } |
| 875 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 876 | void bdrv_set_geometry_hint(BlockDriverState *bs, |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 877 | int cyls, int heads, int secs) |
| 878 | { |
| 879 | bs->cyls = cyls; |
| 880 | bs->heads = heads; |
| 881 | bs->secs = secs; |
| 882 | } |
| 883 | |
| 884 | void bdrv_set_type_hint(BlockDriverState *bs, int type) |
| 885 | { |
| 886 | bs->type = type; |
| 887 | bs->removable = ((type == BDRV_TYPE_CDROM || |
| 888 | type == BDRV_TYPE_FLOPPY)); |
| 889 | } |
| 890 | |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 891 | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
| 892 | { |
| 893 | bs->translation = translation; |
| 894 | } |
| 895 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 896 | void bdrv_get_geometry_hint(BlockDriverState *bs, |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 897 | int *pcyls, int *pheads, int *psecs) |
| 898 | { |
| 899 | *pcyls = bs->cyls; |
| 900 | *pheads = bs->heads; |
| 901 | *psecs = bs->secs; |
| 902 | } |
| 903 | |
| 904 | int bdrv_get_type_hint(BlockDriverState *bs) |
| 905 | { |
| 906 | return bs->type; |
| 907 | } |
| 908 | |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 909 | int bdrv_get_translation_hint(BlockDriverState *bs) |
| 910 | { |
| 911 | return bs->translation; |
| 912 | } |
| 913 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 914 | int bdrv_is_removable(BlockDriverState *bs) |
| 915 | { |
| 916 | return bs->removable; |
| 917 | } |
| 918 | |
| 919 | int bdrv_is_read_only(BlockDriverState *bs) |
| 920 | { |
| 921 | return bs->read_only; |
| 922 | } |
| 923 | |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 924 | int bdrv_is_sg(BlockDriverState *bs) |
| 925 | { |
| 926 | return bs->sg; |
| 927 | } |
| 928 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 929 | /* XXX: no longer used */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 930 | void bdrv_set_change_cb(BlockDriverState *bs, |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 931 | void (*change_cb)(void *opaque), void *opaque) |
| 932 | { |
| 933 | bs->change_cb = change_cb; |
| 934 | bs->change_opaque = opaque; |
| 935 | } |
| 936 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 937 | int bdrv_is_encrypted(BlockDriverState *bs) |
| 938 | { |
| 939 | if (bs->backing_hd && bs->backing_hd->encrypted) |
| 940 | return 1; |
| 941 | return bs->encrypted; |
| 942 | } |
| 943 | |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 944 | int bdrv_key_required(BlockDriverState *bs) |
| 945 | { |
| 946 | BlockDriverState *backing_hd = bs->backing_hd; |
| 947 | |
| 948 | if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) |
| 949 | return 1; |
| 950 | return (bs->encrypted && !bs->valid_key); |
| 951 | } |
| 952 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 953 | int bdrv_set_key(BlockDriverState *bs, const char *key) |
| 954 | { |
| 955 | int ret; |
| 956 | if (bs->backing_hd && bs->backing_hd->encrypted) { |
| 957 | ret = bdrv_set_key(bs->backing_hd, key); |
| 958 | if (ret < 0) |
| 959 | return ret; |
| 960 | if (!bs->encrypted) |
| 961 | return 0; |
| 962 | } |
| 963 | if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key) |
| 964 | return -1; |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 965 | ret = bs->drv->bdrv_set_key(bs, key); |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 966 | if (ret < 0) { |
| 967 | bs->valid_key = 0; |
| 968 | } else if (!bs->valid_key) { |
| 969 | bs->valid_key = 1; |
| 970 | /* call the change callback now, we skipped it on open */ |
| 971 | bs->media_changed = 1; |
| 972 | if (bs->change_cb) |
| 973 | bs->change_cb(bs->change_opaque); |
| 974 | } |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 975 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) |
| 979 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 980 | if (!bs->drv) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 981 | buf[0] = '\0'; |
| 982 | } else { |
| 983 | pstrcpy(buf, buf_size, bs->drv->format_name); |
| 984 | } |
| 985 | } |
| 986 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 987 | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 988 | void *opaque) |
| 989 | { |
| 990 | BlockDriver *drv; |
| 991 | |
| 992 | for (drv = first_drv; drv != NULL; drv = drv->next) { |
| 993 | it(opaque, drv->format_name); |
| 994 | } |
| 995 | } |
| 996 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 997 | BlockDriverState *bdrv_find(const char *name) |
| 998 | { |
| 999 | BlockDriverState *bs; |
| 1000 | |
| 1001 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
| 1002 | if (!strcmp(name, bs->device_name)) |
| 1003 | return bs; |
| 1004 | } |
| 1005 | return NULL; |
| 1006 | } |
| 1007 | |
aliguori | 51de976 | 2009-03-05 23:00:43 +0000 | [diff] [blame] | 1008 | void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 1009 | { |
| 1010 | BlockDriverState *bs; |
| 1011 | |
| 1012 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
aliguori | 51de976 | 2009-03-05 23:00:43 +0000 | [diff] [blame] | 1013 | it(opaque, bs); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1017 | const char *bdrv_get_device_name(BlockDriverState *bs) |
| 1018 | { |
| 1019 | return bs->device_name; |
| 1020 | } |
| 1021 | |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 1022 | void bdrv_flush(BlockDriverState *bs) |
| 1023 | { |
aliguori | 081501d | 2009-03-29 01:31:51 +0000 | [diff] [blame] | 1024 | if (!bs->drv) |
| 1025 | return; |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 1026 | if (bs->drv->bdrv_flush) |
| 1027 | bs->drv->bdrv_flush(bs); |
| 1028 | if (bs->backing_hd) |
| 1029 | bdrv_flush(bs->backing_hd); |
| 1030 | } |
| 1031 | |
aliguori | c6ca28d | 2008-10-06 13:55:43 +0000 | [diff] [blame] | 1032 | void bdrv_flush_all(void) |
| 1033 | { |
| 1034 | BlockDriverState *bs; |
| 1035 | |
| 1036 | for (bs = bdrv_first; bs != NULL; bs = bs->next) |
| 1037 | if (bs->drv && !bdrv_is_read_only(bs) && |
| 1038 | (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) |
| 1039 | bdrv_flush(bs); |
| 1040 | } |
| 1041 | |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1042 | /* |
| 1043 | * Returns true iff the specified sector is present in the disk image. Drivers |
| 1044 | * not implementing the functionality are assumed to not support backing files, |
| 1045 | * hence all their sectors are reported as allocated. |
| 1046 | * |
| 1047 | * 'pnum' is set to the number of sectors (including and immediately following |
| 1048 | * the specified sector) that are known to be in the same |
| 1049 | * allocated/unallocated state. |
| 1050 | * |
| 1051 | * 'nb_sectors' is the max value 'pnum' should be set to. |
| 1052 | */ |
| 1053 | int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, |
| 1054 | int *pnum) |
| 1055 | { |
| 1056 | int64_t n; |
| 1057 | if (!bs->drv->bdrv_is_allocated) { |
| 1058 | if (sector_num >= bs->total_sectors) { |
| 1059 | *pnum = 0; |
| 1060 | return 0; |
| 1061 | } |
| 1062 | n = bs->total_sectors - sector_num; |
| 1063 | *pnum = (n < nb_sectors) ? (n) : (nb_sectors); |
| 1064 | return 1; |
| 1065 | } |
| 1066 | return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); |
| 1067 | } |
| 1068 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1069 | void bdrv_info(Monitor *mon) |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1070 | { |
| 1071 | BlockDriverState *bs; |
| 1072 | |
| 1073 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1074 | monitor_printf(mon, "%s:", bs->device_name); |
| 1075 | monitor_printf(mon, " type="); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1076 | switch(bs->type) { |
| 1077 | case BDRV_TYPE_HD: |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1078 | monitor_printf(mon, "hd"); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1079 | break; |
| 1080 | case BDRV_TYPE_CDROM: |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1081 | monitor_printf(mon, "cdrom"); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1082 | break; |
| 1083 | case BDRV_TYPE_FLOPPY: |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1084 | monitor_printf(mon, "floppy"); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1085 | break; |
| 1086 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1087 | monitor_printf(mon, " removable=%d", bs->removable); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1088 | if (bs->removable) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1089 | monitor_printf(mon, " locked=%d", bs->locked); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1090 | } |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1091 | if (bs->drv) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1092 | monitor_printf(mon, " file="); |
| 1093 | monitor_print_filename(mon, bs->filename); |
ths | fef3074 | 2006-12-22 14:11:32 +0000 | [diff] [blame] | 1094 | if (bs->backing_file[0] != '\0') { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1095 | monitor_printf(mon, " backing_file="); |
| 1096 | monitor_print_filename(mon, bs->backing_file); |
| 1097 | } |
| 1098 | monitor_printf(mon, " ro=%d", bs->read_only); |
| 1099 | monitor_printf(mon, " drv=%s", bs->drv->format_name); |
| 1100 | monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs)); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1101 | } else { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1102 | monitor_printf(mon, " [not inserted]"); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1103 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1104 | monitor_printf(mon, "\n"); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1105 | } |
| 1106 | } |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1107 | |
| 1108 | /* The "info blockstats" command. */ |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1109 | void bdrv_info_stats(Monitor *mon) |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1110 | { |
| 1111 | BlockDriverState *bs; |
| 1112 | |
| 1113 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1114 | monitor_printf(mon, "%s:" |
| 1115 | " rd_bytes=%" PRIu64 |
| 1116 | " wr_bytes=%" PRIu64 |
| 1117 | " rd_operations=%" PRIu64 |
| 1118 | " wr_operations=%" PRIu64 |
aliguori | ebf53fc | 2009-03-11 20:05:29 +0000 | [diff] [blame] | 1119 | "\n", |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1120 | bs->device_name, |
| 1121 | bs->rd_bytes, bs->wr_bytes, |
| 1122 | bs->rd_ops, bs->wr_ops); |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1123 | } |
| 1124 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1125 | |
aliguori | 045df33 | 2009-03-05 23:00:48 +0000 | [diff] [blame] | 1126 | const char *bdrv_get_encrypted_filename(BlockDriverState *bs) |
| 1127 | { |
| 1128 | if (bs->backing_hd && bs->backing_hd->encrypted) |
| 1129 | return bs->backing_file; |
| 1130 | else if (bs->encrypted) |
| 1131 | return bs->filename; |
| 1132 | else |
| 1133 | return NULL; |
| 1134 | } |
| 1135 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1136 | void bdrv_get_backing_filename(BlockDriverState *bs, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1137 | char *filename, int filename_size) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1138 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1139 | if (!bs->backing_hd) { |
| 1140 | pstrcpy(filename, filename_size, ""); |
| 1141 | } else { |
| 1142 | pstrcpy(filename, filename_size, bs->backing_file); |
| 1143 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1146 | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1147 | const uint8_t *buf, int nb_sectors) |
| 1148 | { |
| 1149 | BlockDriver *drv = bs->drv; |
| 1150 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1151 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1152 | if (!drv->bdrv_write_compressed) |
| 1153 | return -ENOTSUP; |
Kevin Wolf | fbb7b4e | 2009-05-08 14:47:24 +0200 | [diff] [blame] | 1154 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 1155 | return -EIO; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1156 | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); |
| 1157 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1158 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1159 | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
| 1160 | { |
| 1161 | BlockDriver *drv = bs->drv; |
| 1162 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1163 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1164 | if (!drv->bdrv_get_info) |
| 1165 | return -ENOTSUP; |
| 1166 | memset(bdi, 0, sizeof(*bdi)); |
| 1167 | return drv->bdrv_get_info(bs, bdi); |
| 1168 | } |
| 1169 | |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1170 | int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size) |
| 1171 | { |
| 1172 | BlockDriver *drv = bs->drv; |
| 1173 | if (!drv) |
| 1174 | return -ENOMEDIUM; |
| 1175 | if (!drv->bdrv_put_buffer) |
| 1176 | return -ENOTSUP; |
| 1177 | return drv->bdrv_put_buffer(bs, buf, pos, size); |
| 1178 | } |
| 1179 | |
| 1180 | int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size) |
| 1181 | { |
| 1182 | BlockDriver *drv = bs->drv; |
| 1183 | if (!drv) |
| 1184 | return -ENOMEDIUM; |
| 1185 | if (!drv->bdrv_get_buffer) |
| 1186 | return -ENOTSUP; |
| 1187 | return drv->bdrv_get_buffer(bs, buf, pos, size); |
| 1188 | } |
| 1189 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1190 | /**************************************************************/ |
| 1191 | /* handling of snapshots */ |
| 1192 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1193 | int bdrv_snapshot_create(BlockDriverState *bs, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1194 | QEMUSnapshotInfo *sn_info) |
| 1195 | { |
| 1196 | BlockDriver *drv = bs->drv; |
| 1197 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1198 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1199 | if (!drv->bdrv_snapshot_create) |
| 1200 | return -ENOTSUP; |
| 1201 | return drv->bdrv_snapshot_create(bs, sn_info); |
| 1202 | } |
| 1203 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1204 | int bdrv_snapshot_goto(BlockDriverState *bs, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1205 | const char *snapshot_id) |
| 1206 | { |
| 1207 | BlockDriver *drv = bs->drv; |
| 1208 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1209 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1210 | if (!drv->bdrv_snapshot_goto) |
| 1211 | return -ENOTSUP; |
| 1212 | return drv->bdrv_snapshot_goto(bs, snapshot_id); |
| 1213 | } |
| 1214 | |
| 1215 | int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) |
| 1216 | { |
| 1217 | BlockDriver *drv = bs->drv; |
| 1218 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1219 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1220 | if (!drv->bdrv_snapshot_delete) |
| 1221 | return -ENOTSUP; |
| 1222 | return drv->bdrv_snapshot_delete(bs, snapshot_id); |
| 1223 | } |
| 1224 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1225 | int bdrv_snapshot_list(BlockDriverState *bs, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1226 | QEMUSnapshotInfo **psn_info) |
| 1227 | { |
| 1228 | BlockDriver *drv = bs->drv; |
| 1229 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1230 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1231 | if (!drv->bdrv_snapshot_list) |
| 1232 | return -ENOTSUP; |
| 1233 | return drv->bdrv_snapshot_list(bs, psn_info); |
| 1234 | } |
| 1235 | |
| 1236 | #define NB_SUFFIXES 4 |
| 1237 | |
| 1238 | char *get_human_readable_size(char *buf, int buf_size, int64_t size) |
| 1239 | { |
| 1240 | static const char suffixes[NB_SUFFIXES] = "KMGT"; |
| 1241 | int64_t base; |
| 1242 | int i; |
| 1243 | |
| 1244 | if (size <= 999) { |
| 1245 | snprintf(buf, buf_size, "%" PRId64, size); |
| 1246 | } else { |
| 1247 | base = 1024; |
| 1248 | for(i = 0; i < NB_SUFFIXES; i++) { |
| 1249 | if (size < (10 * base)) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1250 | snprintf(buf, buf_size, "%0.1f%c", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1251 | (double)size / base, |
| 1252 | suffixes[i]); |
| 1253 | break; |
| 1254 | } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1255 | snprintf(buf, buf_size, "%" PRId64 "%c", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1256 | ((size + (base >> 1)) / base), |
| 1257 | suffixes[i]); |
| 1258 | break; |
| 1259 | } |
| 1260 | base = base * 1024; |
| 1261 | } |
| 1262 | } |
| 1263 | return buf; |
| 1264 | } |
| 1265 | |
| 1266 | char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) |
| 1267 | { |
| 1268 | char buf1[128], date_buf[128], clock_buf[128]; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 1269 | #ifdef _WIN32 |
| 1270 | struct tm *ptm; |
| 1271 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1272 | struct tm tm; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 1273 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1274 | time_t ti; |
| 1275 | int64_t secs; |
| 1276 | |
| 1277 | if (!sn) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1278 | snprintf(buf, buf_size, |
| 1279 | "%-10s%-20s%7s%20s%15s", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1280 | "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); |
| 1281 | } else { |
| 1282 | ti = sn->date_sec; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 1283 | #ifdef _WIN32 |
| 1284 | ptm = localtime(&ti); |
| 1285 | strftime(date_buf, sizeof(date_buf), |
| 1286 | "%Y-%m-%d %H:%M:%S", ptm); |
| 1287 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1288 | localtime_r(&ti, &tm); |
| 1289 | strftime(date_buf, sizeof(date_buf), |
| 1290 | "%Y-%m-%d %H:%M:%S", &tm); |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 1291 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1292 | secs = sn->vm_clock_nsec / 1000000000; |
| 1293 | snprintf(clock_buf, sizeof(clock_buf), |
| 1294 | "%02d:%02d:%02d.%03d", |
| 1295 | (int)(secs / 3600), |
| 1296 | (int)((secs / 60) % 60), |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1297 | (int)(secs % 60), |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1298 | (int)((sn->vm_clock_nsec / 1000000) % 1000)); |
| 1299 | snprintf(buf, buf_size, |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1300 | "%-10s%-20s%7s%20s%15s", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1301 | sn->id_str, sn->name, |
| 1302 | get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), |
| 1303 | date_buf, |
| 1304 | clock_buf); |
| 1305 | } |
| 1306 | return buf; |
| 1307 | } |
| 1308 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1309 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1310 | /**************************************************************/ |
| 1311 | /* async I/Os */ |
| 1312 | |
aliguori | 3b69e4b | 2009-01-22 16:59:24 +0000 | [diff] [blame] | 1313 | BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1314 | QEMUIOVector *qiov, int nb_sectors, |
aliguori | 3b69e4b | 2009-01-22 16:59:24 +0000 | [diff] [blame] | 1315 | BlockDriverCompletionFunc *cb, void *opaque) |
| 1316 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1317 | BlockDriver *drv = bs->drv; |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1318 | BlockDriverAIOCB *ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1319 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1320 | if (!drv) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1321 | return NULL; |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 1322 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 1323 | return NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1324 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1325 | ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors, |
| 1326 | cb, opaque); |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1327 | |
| 1328 | if (ret) { |
| 1329 | /* Update stats even though technically transfer has not happened. */ |
| 1330 | bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE; |
| 1331 | bs->rd_ops ++; |
| 1332 | } |
| 1333 | |
| 1334 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1337 | BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, |
| 1338 | QEMUIOVector *qiov, int nb_sectors, |
| 1339 | BlockDriverCompletionFunc *cb, void *opaque) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1340 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1341 | BlockDriver *drv = bs->drv; |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1342 | BlockDriverAIOCB *ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1343 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1344 | if (!drv) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1345 | return NULL; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1346 | if (bs->read_only) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1347 | return NULL; |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 1348 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 1349 | return NULL; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1350 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1351 | ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors, |
| 1352 | cb, opaque); |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1353 | |
| 1354 | if (ret) { |
| 1355 | /* Update stats even though technically transfer has not happened. */ |
| 1356 | bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE; |
| 1357 | bs->wr_ops ++; |
| 1358 | } |
| 1359 | |
| 1360 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | void bdrv_aio_cancel(BlockDriverAIOCB *acb) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1364 | { |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 1365 | acb->pool->cancel(acb); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1368 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1369 | /**************************************************************/ |
| 1370 | /* async block device emulation */ |
| 1371 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1372 | static void bdrv_aio_bh_cb(void *opaque) |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 1373 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1374 | BlockDriverAIOCBSync *acb = opaque; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1375 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1376 | if (!acb->is_write) |
| 1377 | qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); |
aliguori | ceb42de | 2009-04-07 18:43:28 +0000 | [diff] [blame] | 1378 | qemu_vfree(acb->bounce); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1379 | acb->common.cb(acb->common.opaque, acb->ret); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1380 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1381 | qemu_aio_release(acb); |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 1382 | } |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 1383 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1384 | static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, |
| 1385 | int64_t sector_num, |
| 1386 | QEMUIOVector *qiov, |
| 1387 | int nb_sectors, |
| 1388 | BlockDriverCompletionFunc *cb, |
| 1389 | void *opaque, |
| 1390 | int is_write) |
| 1391 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1392 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1393 | BlockDriverAIOCBSync *acb; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1394 | |
| 1395 | acb = qemu_aio_get(bs, cb, opaque); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1396 | acb->is_write = is_write; |
| 1397 | acb->qiov = qiov; |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 1398 | acb->bounce = qemu_blockalign(bs, qiov->size); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1399 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1400 | if (!acb->bh) |
| 1401 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1402 | |
| 1403 | if (is_write) { |
| 1404 | qemu_iovec_to_buffer(acb->qiov, acb->bounce); |
| 1405 | acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors); |
| 1406 | } else { |
| 1407 | acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors); |
| 1408 | } |
| 1409 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1410 | qemu_bh_schedule(acb->bh); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1411 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1412 | return &acb->common; |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1415 | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, |
| 1416 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1417 | BlockDriverCompletionFunc *cb, void *opaque) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1418 | { |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1419 | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1422 | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, |
| 1423 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
| 1424 | BlockDriverCompletionFunc *cb, void *opaque) |
| 1425 | { |
| 1426 | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); |
| 1427 | } |
| 1428 | |
| 1429 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1430 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1431 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1432 | BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb; |
| 1433 | qemu_bh_cancel(acb->bh); |
| 1434 | qemu_aio_release(acb); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1435 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1436 | |
| 1437 | /**************************************************************/ |
| 1438 | /* sync block device emulation */ |
| 1439 | |
| 1440 | static void bdrv_rw_em_cb(void *opaque, int ret) |
| 1441 | { |
| 1442 | *(int *)opaque = ret; |
| 1443 | } |
| 1444 | |
| 1445 | #define NOT_DONE 0x7fffffff |
| 1446 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1447 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1448 | uint8_t *buf, int nb_sectors) |
| 1449 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1450 | int async_ret; |
| 1451 | BlockDriverAIOCB *acb; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1452 | struct iovec iov; |
| 1453 | QEMUIOVector qiov; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1454 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1455 | async_ret = NOT_DONE; |
blueswir1 | 3f4cb3d | 2009-04-13 16:31:01 +0000 | [diff] [blame] | 1456 | iov.iov_base = (void *)buf; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1457 | iov.iov_len = nb_sectors * 512; |
| 1458 | qemu_iovec_init_external(&qiov, &iov, 1); |
| 1459 | acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors, |
| 1460 | bdrv_rw_em_cb, &async_ret); |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 1461 | if (acb == NULL) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1462 | return -1; |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 1463 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1464 | while (async_ret == NOT_DONE) { |
| 1465 | qemu_aio_wait(); |
| 1466 | } |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 1467 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1468 | return async_ret; |
| 1469 | } |
| 1470 | |
| 1471 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
| 1472 | const uint8_t *buf, int nb_sectors) |
| 1473 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1474 | int async_ret; |
| 1475 | BlockDriverAIOCB *acb; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1476 | struct iovec iov; |
| 1477 | QEMUIOVector qiov; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1478 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1479 | async_ret = NOT_DONE; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1480 | iov.iov_base = (void *)buf; |
| 1481 | iov.iov_len = nb_sectors * 512; |
| 1482 | qemu_iovec_init_external(&qiov, &iov, 1); |
| 1483 | acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors, |
| 1484 | bdrv_rw_em_cb, &async_ret); |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 1485 | if (acb == NULL) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1486 | return -1; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1487 | while (async_ret == NOT_DONE) { |
| 1488 | qemu_aio_wait(); |
| 1489 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1490 | return async_ret; |
| 1491 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1492 | |
| 1493 | void bdrv_init(void) |
| 1494 | { |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 1495 | module_call_init(MODULE_INIT_BLOCK); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1496 | } |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1497 | |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 1498 | void aio_pool_init(AIOPool *pool, int aiocb_size, |
| 1499 | void (*cancel)(BlockDriverAIOCB *acb)) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1500 | { |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 1501 | pool->aiocb_size = aiocb_size; |
| 1502 | pool->cancel = cancel; |
| 1503 | pool->free_aiocb = NULL; |
| 1504 | } |
| 1505 | |
| 1506 | void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs, |
| 1507 | BlockDriverCompletionFunc *cb, void *opaque) |
| 1508 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1509 | BlockDriverAIOCB *acb; |
| 1510 | |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 1511 | if (pool->free_aiocb) { |
| 1512 | acb = pool->free_aiocb; |
| 1513 | pool->free_aiocb = acb->next; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1514 | } else { |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 1515 | acb = qemu_mallocz(pool->aiocb_size); |
| 1516 | acb->pool = pool; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1517 | } |
| 1518 | acb->bs = bs; |
| 1519 | acb->cb = cb; |
| 1520 | acb->opaque = opaque; |
| 1521 | return acb; |
| 1522 | } |
| 1523 | |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 1524 | void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb, |
| 1525 | void *opaque) |
| 1526 | { |
| 1527 | return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque); |
| 1528 | } |
| 1529 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1530 | void qemu_aio_release(void *p) |
| 1531 | { |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 1532 | BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; |
| 1533 | AIOPool *pool = acb->pool; |
| 1534 | acb->next = pool->free_aiocb; |
| 1535 | pool->free_aiocb = acb; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1536 | } |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1537 | |
| 1538 | /**************************************************************/ |
| 1539 | /* removable device support */ |
| 1540 | |
| 1541 | /** |
| 1542 | * Return TRUE if the media is present |
| 1543 | */ |
| 1544 | int bdrv_is_inserted(BlockDriverState *bs) |
| 1545 | { |
| 1546 | BlockDriver *drv = bs->drv; |
| 1547 | int ret; |
| 1548 | if (!drv) |
| 1549 | return 0; |
| 1550 | if (!drv->bdrv_is_inserted) |
| 1551 | return 1; |
| 1552 | ret = drv->bdrv_is_inserted(bs); |
| 1553 | return ret; |
| 1554 | } |
| 1555 | |
| 1556 | /** |
| 1557 | * Return TRUE if the media changed since the last call to this |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1558 | * function. It is currently only used for floppy disks |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1559 | */ |
| 1560 | int bdrv_media_changed(BlockDriverState *bs) |
| 1561 | { |
| 1562 | BlockDriver *drv = bs->drv; |
| 1563 | int ret; |
| 1564 | |
| 1565 | if (!drv || !drv->bdrv_media_changed) |
| 1566 | ret = -ENOTSUP; |
| 1567 | else |
| 1568 | ret = drv->bdrv_media_changed(bs); |
| 1569 | if (ret == -ENOTSUP) |
| 1570 | ret = bs->media_changed; |
| 1571 | bs->media_changed = 0; |
| 1572 | return ret; |
| 1573 | } |
| 1574 | |
| 1575 | /** |
| 1576 | * If eject_flag is TRUE, eject the media. Otherwise, close the tray |
| 1577 | */ |
| 1578 | void bdrv_eject(BlockDriverState *bs, int eject_flag) |
| 1579 | { |
| 1580 | BlockDriver *drv = bs->drv; |
| 1581 | int ret; |
| 1582 | |
| 1583 | if (!drv || !drv->bdrv_eject) { |
| 1584 | ret = -ENOTSUP; |
| 1585 | } else { |
| 1586 | ret = drv->bdrv_eject(bs, eject_flag); |
| 1587 | } |
| 1588 | if (ret == -ENOTSUP) { |
| 1589 | if (eject_flag) |
| 1590 | bdrv_close(bs); |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | int bdrv_is_locked(BlockDriverState *bs) |
| 1595 | { |
| 1596 | return bs->locked; |
| 1597 | } |
| 1598 | |
| 1599 | /** |
| 1600 | * Lock or unlock the media (if it is locked, the user won't be able |
| 1601 | * to eject it manually). |
| 1602 | */ |
| 1603 | void bdrv_set_locked(BlockDriverState *bs, int locked) |
| 1604 | { |
| 1605 | BlockDriver *drv = bs->drv; |
| 1606 | |
| 1607 | bs->locked = locked; |
| 1608 | if (drv && drv->bdrv_set_locked) { |
| 1609 | drv->bdrv_set_locked(bs, locked); |
| 1610 | } |
| 1611 | } |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 1612 | |
| 1613 | /* needed for generic scsi interface */ |
| 1614 | |
| 1615 | int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
| 1616 | { |
| 1617 | BlockDriver *drv = bs->drv; |
| 1618 | |
| 1619 | if (drv && drv->bdrv_ioctl) |
| 1620 | return drv->bdrv_ioctl(bs, req, buf); |
| 1621 | return -ENOTSUP; |
| 1622 | } |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 1623 | |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 1624 | BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, |
| 1625 | unsigned long int req, void *buf, |
| 1626 | BlockDriverCompletionFunc *cb, void *opaque) |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 1627 | { |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 1628 | BlockDriver *drv = bs->drv; |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 1629 | |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 1630 | if (drv && drv->bdrv_aio_ioctl) |
| 1631 | return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); |
| 1632 | return NULL; |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 1633 | } |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 1634 | |
| 1635 | void *qemu_blockalign(BlockDriverState *bs, size_t size) |
| 1636 | { |
| 1637 | return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); |
| 1638 | } |