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" |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 25 | #include "qemu-common.h" |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 26 | #include "monitor.h" |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 27 | #include "block_int.h" |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 28 | #include "module.h" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 29 | |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 30 | #ifdef CONFIG_BSD |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <sys/ioctl.h> |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 34 | #include <sys/queue.h> |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 35 | #ifndef __DragonFly__ |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 36 | #include <sys/disk.h> |
| 37 | #endif |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 38 | #endif |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 39 | |
aliguori | 49dc768 | 2009-03-08 16:26:59 +0000 | [diff] [blame] | 40 | #ifdef _WIN32 |
| 41 | #include <windows.h> |
| 42 | #endif |
| 43 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 44 | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, |
| 45 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
aliguori | c87c067 | 2009-04-07 18:43:20 +0000 | [diff] [blame] | 46 | BlockDriverCompletionFunc *cb, void *opaque); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 47 | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, |
| 48 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 49 | BlockDriverCompletionFunc *cb, void *opaque); |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 50 | static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, |
| 51 | BlockDriverCompletionFunc *cb, void *opaque); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 52 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 53 | uint8_t *buf, int nb_sectors); |
| 54 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
| 55 | const uint8_t *buf, int nb_sectors); |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 56 | |
blueswir1 | 7ee930d | 2008-09-17 19:04:14 +0000 | [diff] [blame] | 57 | BlockDriverState *bdrv_first; |
| 58 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 59 | static BlockDriver *first_drv; |
| 60 | |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 61 | /* If non-zero, use only whitelisted block drivers */ |
| 62 | static int use_bdrv_whitelist; |
| 63 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 64 | int path_is_absolute(const char *path) |
| 65 | { |
| 66 | const char *p; |
bellard | 2166442 | 2007-01-07 18:22:37 +0000 | [diff] [blame] | 67 | #ifdef _WIN32 |
| 68 | /* specific case for names like: "\\.\d:" */ |
| 69 | if (*path == '/' || *path == '\\') |
| 70 | return 1; |
| 71 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 72 | p = strchr(path, ':'); |
| 73 | if (p) |
| 74 | p++; |
| 75 | else |
| 76 | p = path; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 77 | #ifdef _WIN32 |
| 78 | return (*p == '/' || *p == '\\'); |
| 79 | #else |
| 80 | return (*p == '/'); |
| 81 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* if filename is absolute, just copy it to dest. Otherwise, build a |
| 85 | path to it by considering it is relative to base_path. URL are |
| 86 | supported. */ |
| 87 | void path_combine(char *dest, int dest_size, |
| 88 | const char *base_path, |
| 89 | const char *filename) |
| 90 | { |
| 91 | const char *p, *p1; |
| 92 | int len; |
| 93 | |
| 94 | if (dest_size <= 0) |
| 95 | return; |
| 96 | if (path_is_absolute(filename)) { |
| 97 | pstrcpy(dest, dest_size, filename); |
| 98 | } else { |
| 99 | p = strchr(base_path, ':'); |
| 100 | if (p) |
| 101 | p++; |
| 102 | else |
| 103 | p = base_path; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 104 | p1 = strrchr(base_path, '/'); |
| 105 | #ifdef _WIN32 |
| 106 | { |
| 107 | const char *p2; |
| 108 | p2 = strrchr(base_path, '\\'); |
| 109 | if (!p1 || p2 > p1) |
| 110 | p1 = p2; |
| 111 | } |
| 112 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 113 | if (p1) |
| 114 | p1++; |
| 115 | else |
| 116 | p1 = base_path; |
| 117 | if (p1 > p) |
| 118 | p = p1; |
| 119 | len = p - base_path; |
| 120 | if (len > dest_size - 1) |
| 121 | len = dest_size - 1; |
| 122 | memcpy(dest, base_path, len); |
| 123 | dest[len] = '\0'; |
| 124 | pstrcat(dest, dest_size, filename); |
| 125 | } |
| 126 | } |
| 127 | |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 128 | void bdrv_register(BlockDriver *bdrv) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 129 | { |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 130 | if (!bdrv->bdrv_aio_readv) { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 131 | /* add AIO emulation layer */ |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 132 | bdrv->bdrv_aio_readv = bdrv_aio_readv_em; |
| 133 | bdrv->bdrv_aio_writev = bdrv_aio_writev_em; |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 134 | } else if (!bdrv->bdrv_read) { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 135 | /* add synchronous IO emulation layer */ |
| 136 | bdrv->bdrv_read = bdrv_read_em; |
| 137 | bdrv->bdrv_write = bdrv_write_em; |
| 138 | } |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 139 | |
| 140 | if (!bdrv->bdrv_aio_flush) |
| 141 | bdrv->bdrv_aio_flush = bdrv_aio_flush_em; |
| 142 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 143 | bdrv->next = first_drv; |
| 144 | first_drv = bdrv; |
| 145 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 146 | |
| 147 | /* create a new block device (by default it is empty) */ |
| 148 | BlockDriverState *bdrv_new(const char *device_name) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 149 | { |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 150 | BlockDriverState **pbs, *bs; |
| 151 | |
| 152 | bs = qemu_mallocz(sizeof(BlockDriverState)); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 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 | |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 174 | static int bdrv_is_whitelisted(BlockDriver *drv) |
| 175 | { |
| 176 | static const char *whitelist[] = { |
| 177 | CONFIG_BDRV_WHITELIST |
| 178 | }; |
| 179 | const char **p; |
| 180 | |
| 181 | if (!whitelist[0]) |
| 182 | return 1; /* no whitelist, anything goes */ |
| 183 | |
| 184 | for (p = whitelist; *p; p++) { |
| 185 | if (!strcmp(drv->format_name, *p)) { |
| 186 | return 1; |
| 187 | } |
| 188 | } |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | BlockDriver *bdrv_find_whitelisted_format(const char *format_name) |
| 193 | { |
| 194 | BlockDriver *drv = bdrv_find_format(format_name); |
| 195 | return drv && bdrv_is_whitelisted(drv) ? drv : NULL; |
| 196 | } |
| 197 | |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 198 | int bdrv_create(BlockDriver *drv, const char* filename, |
| 199 | QEMUOptionParameter *options) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 200 | { |
| 201 | if (!drv->bdrv_create) |
| 202 | return -ENOTSUP; |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 203 | |
| 204 | return drv->bdrv_create(filename, options); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 205 | } |
| 206 | |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 207 | #ifdef _WIN32 |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 208 | void get_tmp_filename(char *filename, int size) |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 209 | { |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 210 | char temp_dir[MAX_PATH]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 211 | |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 212 | GetTempPath(MAX_PATH, temp_dir); |
| 213 | GetTempFileName(temp_dir, "qem", 0, filename); |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 214 | } |
| 215 | #else |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 216 | void get_tmp_filename(char *filename, int size) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 217 | { |
| 218 | int fd; |
blueswir1 | 7ccfb2e | 2008-09-14 06:45:34 +0000 | [diff] [blame] | 219 | const char *tmpdir; |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 220 | /* XXX: race condition possible */ |
aurel32 | 0badc1e | 2008-03-10 00:05:34 +0000 | [diff] [blame] | 221 | tmpdir = getenv("TMPDIR"); |
| 222 | if (!tmpdir) |
| 223 | tmpdir = "/tmp"; |
| 224 | snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 225 | fd = mkstemp(filename); |
| 226 | close(fd); |
| 227 | } |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 228 | #endif |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 229 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 230 | #ifdef _WIN32 |
bellard | f45512f | 2006-08-23 21:40:13 +0000 | [diff] [blame] | 231 | static int is_windows_drive_prefix(const char *filename) |
| 232 | { |
| 233 | return (((filename[0] >= 'a' && filename[0] <= 'z') || |
| 234 | (filename[0] >= 'A' && filename[0] <= 'Z')) && |
| 235 | filename[1] == ':'); |
| 236 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 237 | |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 238 | int is_windows_drive(const char *filename) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 239 | { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 240 | if (is_windows_drive_prefix(filename) && |
bellard | f45512f | 2006-08-23 21:40:13 +0000 | [diff] [blame] | 241 | filename[2] == '\0') |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 242 | return 1; |
| 243 | if (strstart(filename, "\\\\.\\", NULL) || |
| 244 | strstart(filename, "//./", NULL)) |
| 245 | return 1; |
| 246 | return 0; |
| 247 | } |
| 248 | #endif |
| 249 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 250 | static BlockDriver *find_protocol(const char *filename) |
| 251 | { |
| 252 | BlockDriver *drv1; |
| 253 | char protocol[128]; |
Anthony Liguori | 1cec71e | 2009-07-02 08:12:26 -0500 | [diff] [blame] | 254 | int len; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 255 | const char *p; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 256 | |
| 257 | #ifdef _WIN32 |
bellard | f45512f | 2006-08-23 21:40:13 +0000 | [diff] [blame] | 258 | if (is_windows_drive(filename) || |
| 259 | is_windows_drive_prefix(filename)) |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 260 | return bdrv_find_format("raw"); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 261 | #endif |
Anthony Liguori | 1cec71e | 2009-07-02 08:12:26 -0500 | [diff] [blame] | 262 | p = strchr(filename, ':'); |
| 263 | if (!p) |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 264 | return bdrv_find_format("raw"); |
Anthony Liguori | 1cec71e | 2009-07-02 08:12:26 -0500 | [diff] [blame] | 265 | len = p - filename; |
| 266 | if (len > sizeof(protocol) - 1) |
| 267 | len = sizeof(protocol) - 1; |
| 268 | memcpy(protocol, filename, len); |
| 269 | protocol[len] = '\0'; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 270 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 271 | if (drv1->protocol_name && |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 272 | !strcmp(drv1->protocol_name, protocol)) |
| 273 | return drv1; |
| 274 | } |
| 275 | return NULL; |
| 276 | } |
| 277 | |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 278 | /* |
| 279 | * Detect host devices. By convention, /dev/cdrom[N] is always |
| 280 | * recognized as a host CDROM. |
| 281 | */ |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 282 | static BlockDriver *find_hdev_driver(const char *filename) |
| 283 | { |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 284 | int score_max = 0, score; |
| 285 | BlockDriver *drv = NULL, *d; |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 286 | |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 287 | for (d = first_drv; d; d = d->next) { |
| 288 | if (d->bdrv_probe_device) { |
| 289 | score = d->bdrv_probe_device(filename); |
| 290 | if (score > score_max) { |
| 291 | score_max = score; |
| 292 | drv = d; |
| 293 | } |
| 294 | } |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 295 | } |
| 296 | |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 297 | return drv; |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 298 | } |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 299 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 300 | static BlockDriver *find_image_format(const char *filename) |
| 301 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 302 | int ret, score, score_max; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 303 | BlockDriver *drv1, *drv; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 304 | uint8_t buf[2048]; |
| 305 | BlockDriverState *bs; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 306 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 307 | drv = find_protocol(filename); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 308 | /* no need to test disk image formats for vvfat */ |
Anthony Liguori | c833ab7 | 2009-05-22 08:17:55 -0500 | [diff] [blame] | 309 | if (drv && strcmp(drv->format_name, "vvfat") == 0) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 310 | return drv; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 311 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 312 | ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY); |
| 313 | if (ret < 0) |
| 314 | return NULL; |
| 315 | ret = bdrv_pread(bs, 0, buf, sizeof(buf)); |
| 316 | bdrv_delete(bs); |
| 317 | if (ret < 0) { |
| 318 | return NULL; |
| 319 | } |
| 320 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 321 | score_max = 0; |
| 322 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 323 | if (drv1->bdrv_probe) { |
| 324 | score = drv1->bdrv_probe(buf, ret, filename); |
| 325 | if (score > score_max) { |
| 326 | score_max = score; |
| 327 | drv = drv1; |
| 328 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | return drv; |
| 332 | } |
| 333 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 334 | int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 335 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 336 | BlockDriverState *bs; |
| 337 | int ret; |
| 338 | |
| 339 | bs = bdrv_new(""); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 340 | ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL); |
| 341 | if (ret < 0) { |
| 342 | bdrv_delete(bs); |
| 343 | return ret; |
bellard | 3b0d4f6 | 2005-10-30 18:30:10 +0000 | [diff] [blame] | 344 | } |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 345 | bs->growable = 1; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 346 | *pbs = bs; |
| 347 | return 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 348 | } |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 349 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 350 | int bdrv_open(BlockDriverState *bs, const char *filename, int flags) |
| 351 | { |
| 352 | return bdrv_open2(bs, filename, flags, NULL); |
| 353 | } |
| 354 | |
| 355 | int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 356 | BlockDriver *drv) |
| 357 | { |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 358 | int ret, open_flags, try_rw; |
ths | eb5c851 | 2007-02-11 15:06:09 +0000 | [diff] [blame] | 359 | char tmp_filename[PATH_MAX]; |
| 360 | char backing_filename[PATH_MAX]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 361 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 362 | bs->is_temporary = 0; |
| 363 | bs->encrypted = 0; |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 364 | bs->valid_key = 0; |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 365 | /* buffer_alignment defaulted to 512, drivers can change this value */ |
| 366 | bs->buffer_alignment = 512; |
bellard | 712e787 | 2005-04-28 21:09:32 +0000 | [diff] [blame] | 367 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 368 | if (flags & BDRV_O_SNAPSHOT) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 369 | BlockDriverState *bs1; |
| 370 | int64_t total_size; |
aliguori | 7c96d46 | 2008-09-12 17:54:13 +0000 | [diff] [blame] | 371 | int is_protocol = 0; |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 372 | BlockDriver *bdrv_qcow2; |
| 373 | QEMUOptionParameter *options; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 374 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 375 | /* if snapshot, we create a temporary backing file and open it |
| 376 | instead of opening 'filename' directly */ |
| 377 | |
| 378 | /* if there is a backing file, use it */ |
| 379 | bs1 = bdrv_new(""); |
aliguori | 5eb4563 | 2009-03-28 17:55:10 +0000 | [diff] [blame] | 380 | ret = bdrv_open2(bs1, filename, 0, drv); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 381 | if (ret < 0) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 382 | bdrv_delete(bs1); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 383 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 384 | } |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 385 | total_size = bdrv_getlength(bs1) >> BDRV_SECTOR_BITS; |
aliguori | 7c96d46 | 2008-09-12 17:54:13 +0000 | [diff] [blame] | 386 | |
| 387 | if (bs1->drv && bs1->drv->protocol_name) |
| 388 | is_protocol = 1; |
| 389 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 390 | bdrv_delete(bs1); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 391 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 392 | get_tmp_filename(tmp_filename, sizeof(tmp_filename)); |
aliguori | 7c96d46 | 2008-09-12 17:54:13 +0000 | [diff] [blame] | 393 | |
| 394 | /* Real path is meaningless for protocols */ |
| 395 | if (is_protocol) |
| 396 | snprintf(backing_filename, sizeof(backing_filename), |
| 397 | "%s", filename); |
| 398 | else |
| 399 | realpath(filename, backing_filename); |
| 400 | |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 401 | bdrv_qcow2 = bdrv_find_format("qcow2"); |
| 402 | options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); |
| 403 | |
| 404 | set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512); |
| 405 | set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); |
| 406 | if (drv) { |
| 407 | set_option_parameter(options, BLOCK_OPT_BACKING_FMT, |
| 408 | drv->format_name); |
| 409 | } |
| 410 | |
| 411 | ret = bdrv_create(bdrv_qcow2, tmp_filename, options); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 412 | if (ret < 0) { |
| 413 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 414 | } |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 415 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 416 | filename = tmp_filename; |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 417 | drv = bdrv_qcow2; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 418 | bs->is_temporary = 1; |
| 419 | } |
bellard | 712e787 | 2005-04-28 21:09:32 +0000 | [diff] [blame] | 420 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 421 | pstrcpy(bs->filename, sizeof(bs->filename), filename); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 422 | if (flags & BDRV_O_FILE) { |
| 423 | drv = find_protocol(filename); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 424 | } else if (!drv) { |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 425 | drv = find_hdev_driver(filename); |
| 426 | if (!drv) { |
| 427 | drv = find_image_format(filename); |
| 428 | } |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 429 | } |
| 430 | if (!drv) { |
| 431 | ret = -ENOENT; |
| 432 | goto unlink_and_fail; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 433 | } |
| 434 | bs->drv = drv; |
| 435 | bs->opaque = qemu_mallocz(drv->instance_size); |
Christoph Hellwig | e900a7b | 2009-09-04 19:01:15 +0200 | [diff] [blame] | 436 | |
| 437 | /* |
| 438 | * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a |
| 439 | * write cache to the guest. We do need the fdatasync to flush |
| 440 | * out transactions for block allocations, and we maybe have a |
| 441 | * volatile write cache in our backing device to deal with. |
| 442 | */ |
| 443 | if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE)) |
| 444 | bs->enable_write_cache = 1; |
| 445 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 446 | /* Note: for compatibility, we open disk image files as RDWR, and |
| 447 | RDONLY as fallback */ |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 448 | try_rw = !bs->read_only || bs->is_temporary; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 449 | if (!(flags & BDRV_O_FILE)) |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 450 | open_flags = (try_rw ? BDRV_O_RDWR : 0) | |
| 451 | (flags & (BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO)); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 452 | else |
| 453 | open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 454 | if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) |
| 455 | ret = -ENOTSUP; |
| 456 | else |
| 457 | ret = drv->bdrv_open(bs, filename, open_flags); |
aurel32 | a0a8353 | 2008-10-13 21:08:34 +0000 | [diff] [blame] | 458 | if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) { |
Anthony Liguori | 1cec71e | 2009-07-02 08:12:26 -0500 | [diff] [blame] | 459 | ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 460 | bs->read_only = 1; |
| 461 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 462 | if (ret < 0) { |
| 463 | qemu_free(bs->opaque); |
bellard | 6b21b97 | 2006-08-23 21:14:37 +0000 | [diff] [blame] | 464 | bs->opaque = NULL; |
| 465 | bs->drv = NULL; |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 466 | unlink_and_fail: |
| 467 | if (bs->is_temporary) |
| 468 | unlink(filename); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 469 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 470 | } |
bellard | d15a771 | 2006-08-06 13:35:09 +0000 | [diff] [blame] | 471 | if (drv->bdrv_getlength) { |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 472 | bs->total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; |
bellard | d15a771 | 2006-08-06 13:35:09 +0000 | [diff] [blame] | 473 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 474 | #ifndef _WIN32 |
| 475 | if (bs->is_temporary) { |
| 476 | unlink(filename); |
| 477 | } |
| 478 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 479 | if (bs->backing_file[0] != '\0') { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 480 | /* if there is a backing file, use it */ |
aliguori | 5eb4563 | 2009-03-28 17:55:10 +0000 | [diff] [blame] | 481 | BlockDriver *back_drv = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 482 | bs->backing_hd = bdrv_new(""); |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 483 | /* pass on read_only property to the backing_hd */ |
| 484 | bs->backing_hd->read_only = bs->read_only; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 485 | path_combine(backing_filename, sizeof(backing_filename), |
| 486 | filename, bs->backing_file); |
aliguori | 5eb4563 | 2009-03-28 17:55:10 +0000 | [diff] [blame] | 487 | if (bs->backing_format[0] != '\0') |
| 488 | back_drv = bdrv_find_format(bs->backing_format); |
| 489 | ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags, |
| 490 | back_drv); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 491 | if (ret < 0) { |
| 492 | bdrv_close(bs); |
| 493 | return ret; |
| 494 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 495 | } |
| 496 | |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 497 | if (!bdrv_key_required(bs)) { |
| 498 | /* call the change callback */ |
| 499 | bs->media_changed = 1; |
| 500 | if (bs->change_cb) |
| 501 | bs->change_cb(bs->change_opaque); |
| 502 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 503 | return 0; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | void bdrv_close(BlockDriverState *bs) |
| 507 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 508 | if (bs->drv) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 509 | if (bs->backing_hd) |
| 510 | bdrv_delete(bs->backing_hd); |
| 511 | bs->drv->bdrv_close(bs); |
| 512 | qemu_free(bs->opaque); |
| 513 | #ifdef _WIN32 |
| 514 | if (bs->is_temporary) { |
| 515 | unlink(bs->filename); |
| 516 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 517 | #endif |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 518 | bs->opaque = NULL; |
| 519 | bs->drv = NULL; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 520 | |
| 521 | /* call the change callback */ |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 522 | bs->media_changed = 1; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 523 | if (bs->change_cb) |
| 524 | bs->change_cb(bs->change_opaque); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | void bdrv_delete(BlockDriverState *bs) |
| 529 | { |
aurel32 | 34c6f05 | 2008-04-08 19:51:21 +0000 | [diff] [blame] | 530 | BlockDriverState **pbs; |
| 531 | |
| 532 | pbs = &bdrv_first; |
| 533 | while (*pbs != bs && *pbs != NULL) |
| 534 | pbs = &(*pbs)->next; |
| 535 | if (*pbs == bs) |
| 536 | *pbs = bs->next; |
| 537 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 538 | bdrv_close(bs); |
| 539 | qemu_free(bs); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 540 | } |
| 541 | |
aliguori | e97fc19 | 2009-04-21 23:11:50 +0000 | [diff] [blame] | 542 | /* |
| 543 | * Run consistency checks on an image |
| 544 | * |
| 545 | * Returns the number of errors or -errno when an internal error occurs |
| 546 | */ |
| 547 | int bdrv_check(BlockDriverState *bs) |
| 548 | { |
| 549 | if (bs->drv->bdrv_check == NULL) { |
| 550 | return -ENOTSUP; |
| 551 | } |
| 552 | |
| 553 | return bs->drv->bdrv_check(bs); |
| 554 | } |
| 555 | |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 556 | /* commit COW file into the raw image */ |
| 557 | int bdrv_commit(BlockDriverState *bs) |
| 558 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 559 | BlockDriver *drv = bs->drv; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 560 | int64_t i, total_sectors; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 561 | int n, j; |
| 562 | unsigned char sector[512]; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 563 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 564 | if (!drv) |
| 565 | return -ENOMEDIUM; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 566 | |
| 567 | if (bs->read_only) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 568 | return -EACCES; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 569 | } |
| 570 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 571 | if (!bs->backing_hd) { |
| 572 | return -ENOTSUP; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 573 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 574 | |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 575 | total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 576 | for (i = 0; i < total_sectors;) { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 577 | if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 578 | for(j = 0; j < n; j++) { |
| 579 | if (bdrv_read(bs, i, sector, 1) != 0) { |
| 580 | return -EIO; |
| 581 | } |
| 582 | |
| 583 | if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { |
| 584 | return -EIO; |
| 585 | } |
| 586 | i++; |
| 587 | } |
| 588 | } else { |
| 589 | i += n; |
| 590 | } |
| 591 | } |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 592 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 593 | if (drv->bdrv_make_empty) |
| 594 | return drv->bdrv_make_empty(bs); |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 595 | |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 596 | return 0; |
| 597 | } |
| 598 | |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 599 | static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, |
| 600 | size_t size) |
| 601 | { |
| 602 | int64_t len; |
| 603 | |
| 604 | if (!bdrv_is_inserted(bs)) |
| 605 | return -ENOMEDIUM; |
| 606 | |
| 607 | if (bs->growable) |
| 608 | return 0; |
| 609 | |
| 610 | len = bdrv_getlength(bs); |
| 611 | |
Kevin Wolf | fbb7b4e | 2009-05-08 14:47:24 +0200 | [diff] [blame] | 612 | if (offset < 0) |
| 613 | return -EIO; |
| 614 | |
| 615 | if ((offset > len) || (len - offset < size)) |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 616 | return -EIO; |
| 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, |
| 622 | int nb_sectors) |
| 623 | { |
aliguori | 999dec5 | 2009-03-29 01:31:48 +0000 | [diff] [blame] | 624 | return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512); |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 625 | } |
| 626 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 627 | /* return < 0 if error. See bdrv_write() for the return codes */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 628 | int bdrv_read(BlockDriverState *bs, int64_t sector_num, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 629 | uint8_t *buf, int nb_sectors) |
| 630 | { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 631 | BlockDriver *drv = bs->drv; |
| 632 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 633 | if (!drv) |
| 634 | return -ENOMEDIUM; |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 635 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 636 | return -EIO; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 637 | |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 638 | return drv->bdrv_read(bs, sector_num, buf, nb_sectors); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 639 | } |
| 640 | |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 641 | static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 642 | int nb_sectors, int dirty) |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 643 | { |
| 644 | int64_t start, end; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 645 | |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 646 | start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; |
| 647 | end = (sector_num + nb_sectors) / BDRV_SECTORS_PER_DIRTY_CHUNK; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 648 | |
| 649 | for (; start <= end; start++) { |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 650 | bs->dirty_bitmap[start] = dirty; |
| 651 | } |
| 652 | } |
| 653 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 654 | /* Return < 0 if error. Important errors are: |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 655 | -EIO generic I/O error (may happen for all errors) |
| 656 | -ENOMEDIUM No media inserted. |
| 657 | -EINVAL Invalid sector number or nb_sectors |
| 658 | -EACCES Trying to write a read-only device |
| 659 | */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 660 | int bdrv_write(BlockDriverState *bs, int64_t sector_num, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 661 | const uint8_t *buf, int nb_sectors) |
| 662 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 663 | BlockDriver *drv = bs->drv; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 664 | if (!bs->drv) |
| 665 | return -ENOMEDIUM; |
bellard | 0849bf0 | 2003-06-30 23:17:31 +0000 | [diff] [blame] | 666 | if (bs->read_only) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 667 | return -EACCES; |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 668 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 669 | return -EIO; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 670 | |
| 671 | if (bs->dirty_tracking) { |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 672 | set_dirty_bitmap(bs, sector_num, nb_sectors, 1); |
| 673 | } |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 674 | |
aliguori | 42fb280 | 2009-01-15 20:43:39 +0000 | [diff] [blame] | 675 | return drv->bdrv_write(bs, sector_num, buf, nb_sectors); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 676 | } |
| 677 | |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 678 | int bdrv_pread(BlockDriverState *bs, int64_t offset, |
| 679 | void *buf, int count1) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 680 | { |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 681 | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 682 | int len, nb_sectors, count; |
| 683 | int64_t sector_num; |
| 684 | |
| 685 | count = count1; |
| 686 | /* first read to align to sector start */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 687 | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 688 | if (len > count) |
| 689 | len = count; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 690 | sector_num = offset >> BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 691 | if (len > 0) { |
| 692 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
| 693 | return -EIO; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 694 | memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 695 | count -= len; |
| 696 | if (count == 0) |
| 697 | return count1; |
| 698 | sector_num++; |
| 699 | buf += len; |
| 700 | } |
| 701 | |
| 702 | /* read the sectors "in place" */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 703 | nb_sectors = count >> BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 704 | if (nb_sectors > 0) { |
| 705 | if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0) |
| 706 | return -EIO; |
| 707 | sector_num += nb_sectors; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 708 | len = nb_sectors << BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 709 | buf += len; |
| 710 | count -= len; |
| 711 | } |
| 712 | |
| 713 | /* add data from the last sector */ |
| 714 | if (count > 0) { |
| 715 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
| 716 | return -EIO; |
| 717 | memcpy(buf, tmp_buf, count); |
| 718 | } |
| 719 | return count1; |
| 720 | } |
| 721 | |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 722 | int bdrv_pwrite(BlockDriverState *bs, int64_t offset, |
| 723 | const void *buf, int count1) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 724 | { |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 725 | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 726 | int len, nb_sectors, count; |
| 727 | int64_t sector_num; |
| 728 | |
| 729 | count = count1; |
| 730 | /* first write to align to sector start */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 731 | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 732 | if (len > count) |
| 733 | len = count; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 734 | sector_num = offset >> BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 735 | if (len > 0) { |
| 736 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
| 737 | return -EIO; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 738 | memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 739 | if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) |
| 740 | return -EIO; |
| 741 | count -= len; |
| 742 | if (count == 0) |
| 743 | return count1; |
| 744 | sector_num++; |
| 745 | buf += len; |
| 746 | } |
| 747 | |
| 748 | /* write the sectors "in place" */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 749 | nb_sectors = count >> BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 750 | if (nb_sectors > 0) { |
| 751 | if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0) |
| 752 | return -EIO; |
| 753 | sector_num += nb_sectors; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 754 | len = nb_sectors << BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 755 | buf += len; |
| 756 | count -= len; |
| 757 | } |
| 758 | |
| 759 | /* add data from the last sector */ |
| 760 | if (count > 0) { |
| 761 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
| 762 | return -EIO; |
| 763 | memcpy(tmp_buf, buf, count); |
| 764 | if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) |
| 765 | return -EIO; |
| 766 | } |
| 767 | return count1; |
| 768 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 769 | |
| 770 | /** |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 771 | * Truncate file to 'offset' bytes (needed only for file protocols) |
| 772 | */ |
| 773 | int bdrv_truncate(BlockDriverState *bs, int64_t offset) |
| 774 | { |
| 775 | BlockDriver *drv = bs->drv; |
| 776 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 777 | return -ENOMEDIUM; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 778 | if (!drv->bdrv_truncate) |
| 779 | return -ENOTSUP; |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 780 | if (bs->read_only) |
| 781 | return -EACCES; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 782 | return drv->bdrv_truncate(bs, offset); |
| 783 | } |
| 784 | |
| 785 | /** |
| 786 | * Length of a file in bytes. Return < 0 if error or unknown. |
| 787 | */ |
| 788 | int64_t bdrv_getlength(BlockDriverState *bs) |
| 789 | { |
| 790 | BlockDriver *drv = bs->drv; |
| 791 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 792 | return -ENOMEDIUM; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 793 | if (!drv->bdrv_getlength) { |
| 794 | /* legacy mode */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 795 | return bs->total_sectors * BDRV_SECTOR_SIZE; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 796 | } |
| 797 | return drv->bdrv_getlength(bs); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 798 | } |
| 799 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 800 | /* return 0 as number of sectors if no device present or error */ |
ths | 96b8f13 | 2007-12-17 01:35:20 +0000 | [diff] [blame] | 801 | void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 802 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 803 | int64_t length; |
| 804 | length = bdrv_getlength(bs); |
| 805 | if (length < 0) |
| 806 | length = 0; |
| 807 | else |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 808 | length = length >> BDRV_SECTOR_BITS; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 809 | *nb_sectors_ptr = length; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 810 | } |
bellard | cf98951 | 2004-02-16 21:56:36 +0000 | [diff] [blame] | 811 | |
aliguori | f3d54fc | 2008-11-25 21:50:24 +0000 | [diff] [blame] | 812 | struct partition { |
| 813 | uint8_t boot_ind; /* 0x80 - active */ |
| 814 | uint8_t head; /* starting head */ |
| 815 | uint8_t sector; /* starting sector */ |
| 816 | uint8_t cyl; /* starting cylinder */ |
| 817 | uint8_t sys_ind; /* What partition type */ |
| 818 | uint8_t end_head; /* end head */ |
| 819 | uint8_t end_sector; /* end sector */ |
| 820 | uint8_t end_cyl; /* end cylinder */ |
| 821 | uint32_t start_sect; /* starting sector counting from 0 */ |
| 822 | uint32_t nr_sects; /* nr of sectors in partition */ |
| 823 | } __attribute__((packed)); |
| 824 | |
| 825 | /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ |
| 826 | static int guess_disk_lchs(BlockDriverState *bs, |
| 827 | int *pcylinders, int *pheads, int *psectors) |
| 828 | { |
| 829 | uint8_t buf[512]; |
| 830 | int ret, i, heads, sectors, cylinders; |
| 831 | struct partition *p; |
| 832 | uint32_t nr_sects; |
blueswir1 | a38131b | 2008-12-05 17:56:40 +0000 | [diff] [blame] | 833 | uint64_t nb_sectors; |
aliguori | f3d54fc | 2008-11-25 21:50:24 +0000 | [diff] [blame] | 834 | |
| 835 | bdrv_get_geometry(bs, &nb_sectors); |
| 836 | |
| 837 | ret = bdrv_read(bs, 0, buf, 1); |
| 838 | if (ret < 0) |
| 839 | return -1; |
| 840 | /* test msdos magic */ |
| 841 | if (buf[510] != 0x55 || buf[511] != 0xaa) |
| 842 | return -1; |
| 843 | for(i = 0; i < 4; i++) { |
| 844 | p = ((struct partition *)(buf + 0x1be)) + i; |
| 845 | nr_sects = le32_to_cpu(p->nr_sects); |
| 846 | if (nr_sects && p->end_head) { |
| 847 | /* We make the assumption that the partition terminates on |
| 848 | a cylinder boundary */ |
| 849 | heads = p->end_head + 1; |
| 850 | sectors = p->end_sector & 63; |
| 851 | if (sectors == 0) |
| 852 | continue; |
| 853 | cylinders = nb_sectors / (heads * sectors); |
| 854 | if (cylinders < 1 || cylinders > 16383) |
| 855 | continue; |
| 856 | *pheads = heads; |
| 857 | *psectors = sectors; |
| 858 | *pcylinders = cylinders; |
| 859 | #if 0 |
| 860 | printf("guessed geometry: LCHS=%d %d %d\n", |
| 861 | cylinders, heads, sectors); |
| 862 | #endif |
| 863 | return 0; |
| 864 | } |
| 865 | } |
| 866 | return -1; |
| 867 | } |
| 868 | |
| 869 | void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) |
| 870 | { |
| 871 | int translation, lba_detected = 0; |
| 872 | int cylinders, heads, secs; |
blueswir1 | a38131b | 2008-12-05 17:56:40 +0000 | [diff] [blame] | 873 | uint64_t nb_sectors; |
aliguori | f3d54fc | 2008-11-25 21:50:24 +0000 | [diff] [blame] | 874 | |
| 875 | /* if a geometry hint is available, use it */ |
| 876 | bdrv_get_geometry(bs, &nb_sectors); |
| 877 | bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); |
| 878 | translation = bdrv_get_translation_hint(bs); |
| 879 | if (cylinders != 0) { |
| 880 | *pcyls = cylinders; |
| 881 | *pheads = heads; |
| 882 | *psecs = secs; |
| 883 | } else { |
| 884 | if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { |
| 885 | if (heads > 16) { |
| 886 | /* if heads > 16, it means that a BIOS LBA |
| 887 | translation was active, so the default |
| 888 | hardware geometry is OK */ |
| 889 | lba_detected = 1; |
| 890 | goto default_geometry; |
| 891 | } else { |
| 892 | *pcyls = cylinders; |
| 893 | *pheads = heads; |
| 894 | *psecs = secs; |
| 895 | /* disable any translation to be in sync with |
| 896 | the logical geometry */ |
| 897 | if (translation == BIOS_ATA_TRANSLATION_AUTO) { |
| 898 | bdrv_set_translation_hint(bs, |
| 899 | BIOS_ATA_TRANSLATION_NONE); |
| 900 | } |
| 901 | } |
| 902 | } else { |
| 903 | default_geometry: |
| 904 | /* if no geometry, use a standard physical disk geometry */ |
| 905 | cylinders = nb_sectors / (16 * 63); |
| 906 | |
| 907 | if (cylinders > 16383) |
| 908 | cylinders = 16383; |
| 909 | else if (cylinders < 2) |
| 910 | cylinders = 2; |
| 911 | *pcyls = cylinders; |
| 912 | *pheads = 16; |
| 913 | *psecs = 63; |
| 914 | if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { |
| 915 | if ((*pcyls * *pheads) <= 131072) { |
| 916 | bdrv_set_translation_hint(bs, |
| 917 | BIOS_ATA_TRANSLATION_LARGE); |
| 918 | } else { |
| 919 | bdrv_set_translation_hint(bs, |
| 920 | BIOS_ATA_TRANSLATION_LBA); |
| 921 | } |
| 922 | } |
| 923 | } |
| 924 | bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); |
| 925 | } |
| 926 | } |
| 927 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 928 | void bdrv_set_geometry_hint(BlockDriverState *bs, |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 929 | int cyls, int heads, int secs) |
| 930 | { |
| 931 | bs->cyls = cyls; |
| 932 | bs->heads = heads; |
| 933 | bs->secs = secs; |
| 934 | } |
| 935 | |
| 936 | void bdrv_set_type_hint(BlockDriverState *bs, int type) |
| 937 | { |
| 938 | bs->type = type; |
| 939 | bs->removable = ((type == BDRV_TYPE_CDROM || |
| 940 | type == BDRV_TYPE_FLOPPY)); |
| 941 | } |
| 942 | |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 943 | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
| 944 | { |
| 945 | bs->translation = translation; |
| 946 | } |
| 947 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 948 | void bdrv_get_geometry_hint(BlockDriverState *bs, |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 949 | int *pcyls, int *pheads, int *psecs) |
| 950 | { |
| 951 | *pcyls = bs->cyls; |
| 952 | *pheads = bs->heads; |
| 953 | *psecs = bs->secs; |
| 954 | } |
| 955 | |
| 956 | int bdrv_get_type_hint(BlockDriverState *bs) |
| 957 | { |
| 958 | return bs->type; |
| 959 | } |
| 960 | |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 961 | int bdrv_get_translation_hint(BlockDriverState *bs) |
| 962 | { |
| 963 | return bs->translation; |
| 964 | } |
| 965 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 966 | int bdrv_is_removable(BlockDriverState *bs) |
| 967 | { |
| 968 | return bs->removable; |
| 969 | } |
| 970 | |
| 971 | int bdrv_is_read_only(BlockDriverState *bs) |
| 972 | { |
| 973 | return bs->read_only; |
| 974 | } |
| 975 | |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 976 | int bdrv_set_read_only(BlockDriverState *bs, int read_only) |
| 977 | { |
| 978 | int ret = bs->read_only; |
| 979 | bs->read_only = read_only; |
| 980 | return ret; |
| 981 | } |
| 982 | |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 983 | int bdrv_is_sg(BlockDriverState *bs) |
| 984 | { |
| 985 | return bs->sg; |
| 986 | } |
| 987 | |
Christoph Hellwig | e900a7b | 2009-09-04 19:01:15 +0200 | [diff] [blame] | 988 | int bdrv_enable_write_cache(BlockDriverState *bs) |
| 989 | { |
| 990 | return bs->enable_write_cache; |
| 991 | } |
| 992 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 993 | /* XXX: no longer used */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 994 | void bdrv_set_change_cb(BlockDriverState *bs, |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 995 | void (*change_cb)(void *opaque), void *opaque) |
| 996 | { |
| 997 | bs->change_cb = change_cb; |
| 998 | bs->change_opaque = opaque; |
| 999 | } |
| 1000 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1001 | int bdrv_is_encrypted(BlockDriverState *bs) |
| 1002 | { |
| 1003 | if (bs->backing_hd && bs->backing_hd->encrypted) |
| 1004 | return 1; |
| 1005 | return bs->encrypted; |
| 1006 | } |
| 1007 | |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 1008 | int bdrv_key_required(BlockDriverState *bs) |
| 1009 | { |
| 1010 | BlockDriverState *backing_hd = bs->backing_hd; |
| 1011 | |
| 1012 | if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) |
| 1013 | return 1; |
| 1014 | return (bs->encrypted && !bs->valid_key); |
| 1015 | } |
| 1016 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1017 | int bdrv_set_key(BlockDriverState *bs, const char *key) |
| 1018 | { |
| 1019 | int ret; |
| 1020 | if (bs->backing_hd && bs->backing_hd->encrypted) { |
| 1021 | ret = bdrv_set_key(bs->backing_hd, key); |
| 1022 | if (ret < 0) |
| 1023 | return ret; |
| 1024 | if (!bs->encrypted) |
| 1025 | return 0; |
| 1026 | } |
| 1027 | if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key) |
| 1028 | return -1; |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 1029 | ret = bs->drv->bdrv_set_key(bs, key); |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 1030 | if (ret < 0) { |
| 1031 | bs->valid_key = 0; |
| 1032 | } else if (!bs->valid_key) { |
| 1033 | bs->valid_key = 1; |
| 1034 | /* call the change callback now, we skipped it on open */ |
| 1035 | bs->media_changed = 1; |
| 1036 | if (bs->change_cb) |
| 1037 | bs->change_cb(bs->change_opaque); |
| 1038 | } |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 1039 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) |
| 1043 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1044 | if (!bs->drv) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1045 | buf[0] = '\0'; |
| 1046 | } else { |
| 1047 | pstrcpy(buf, buf_size, bs->drv->format_name); |
| 1048 | } |
| 1049 | } |
| 1050 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1051 | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1052 | void *opaque) |
| 1053 | { |
| 1054 | BlockDriver *drv; |
| 1055 | |
| 1056 | for (drv = first_drv; drv != NULL; drv = drv->next) { |
| 1057 | it(opaque, drv->format_name); |
| 1058 | } |
| 1059 | } |
| 1060 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1061 | BlockDriverState *bdrv_find(const char *name) |
| 1062 | { |
| 1063 | BlockDriverState *bs; |
| 1064 | |
| 1065 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
| 1066 | if (!strcmp(name, bs->device_name)) |
| 1067 | return bs; |
| 1068 | } |
| 1069 | return NULL; |
| 1070 | } |
| 1071 | |
aliguori | 51de976 | 2009-03-05 23:00:43 +0000 | [diff] [blame] | 1072 | void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 1073 | { |
| 1074 | BlockDriverState *bs; |
| 1075 | |
| 1076 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
aliguori | 51de976 | 2009-03-05 23:00:43 +0000 | [diff] [blame] | 1077 | it(opaque, bs); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1081 | const char *bdrv_get_device_name(BlockDriverState *bs) |
| 1082 | { |
| 1083 | return bs->device_name; |
| 1084 | } |
| 1085 | |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 1086 | void bdrv_flush(BlockDriverState *bs) |
| 1087 | { |
aliguori | 081501d | 2009-03-29 01:31:51 +0000 | [diff] [blame] | 1088 | if (!bs->drv) |
| 1089 | return; |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 1090 | if (bs->drv->bdrv_flush) |
| 1091 | bs->drv->bdrv_flush(bs); |
| 1092 | if (bs->backing_hd) |
| 1093 | bdrv_flush(bs->backing_hd); |
| 1094 | } |
| 1095 | |
aliguori | c6ca28d | 2008-10-06 13:55:43 +0000 | [diff] [blame] | 1096 | void bdrv_flush_all(void) |
| 1097 | { |
| 1098 | BlockDriverState *bs; |
| 1099 | |
| 1100 | for (bs = bdrv_first; bs != NULL; bs = bs->next) |
| 1101 | if (bs->drv && !bdrv_is_read_only(bs) && |
| 1102 | (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) |
| 1103 | bdrv_flush(bs); |
| 1104 | } |
| 1105 | |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1106 | /* |
| 1107 | * Returns true iff the specified sector is present in the disk image. Drivers |
| 1108 | * not implementing the functionality are assumed to not support backing files, |
| 1109 | * hence all their sectors are reported as allocated. |
| 1110 | * |
| 1111 | * 'pnum' is set to the number of sectors (including and immediately following |
| 1112 | * the specified sector) that are known to be in the same |
| 1113 | * allocated/unallocated state. |
| 1114 | * |
| 1115 | * 'nb_sectors' is the max value 'pnum' should be set to. |
| 1116 | */ |
| 1117 | int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, |
| 1118 | int *pnum) |
| 1119 | { |
| 1120 | int64_t n; |
| 1121 | if (!bs->drv->bdrv_is_allocated) { |
| 1122 | if (sector_num >= bs->total_sectors) { |
| 1123 | *pnum = 0; |
| 1124 | return 0; |
| 1125 | } |
| 1126 | n = bs->total_sectors - sector_num; |
| 1127 | *pnum = (n < nb_sectors) ? (n) : (nb_sectors); |
| 1128 | return 1; |
| 1129 | } |
| 1130 | return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); |
| 1131 | } |
| 1132 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1133 | void bdrv_info(Monitor *mon) |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1134 | { |
| 1135 | BlockDriverState *bs; |
| 1136 | |
| 1137 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1138 | monitor_printf(mon, "%s:", bs->device_name); |
| 1139 | monitor_printf(mon, " type="); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1140 | switch(bs->type) { |
| 1141 | case BDRV_TYPE_HD: |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1142 | monitor_printf(mon, "hd"); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1143 | break; |
| 1144 | case BDRV_TYPE_CDROM: |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1145 | monitor_printf(mon, "cdrom"); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1146 | break; |
| 1147 | case BDRV_TYPE_FLOPPY: |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1148 | monitor_printf(mon, "floppy"); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1149 | break; |
| 1150 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1151 | monitor_printf(mon, " removable=%d", bs->removable); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1152 | if (bs->removable) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1153 | monitor_printf(mon, " locked=%d", bs->locked); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1154 | } |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1155 | if (bs->drv) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1156 | monitor_printf(mon, " file="); |
| 1157 | monitor_print_filename(mon, bs->filename); |
ths | fef3074 | 2006-12-22 14:11:32 +0000 | [diff] [blame] | 1158 | if (bs->backing_file[0] != '\0') { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1159 | monitor_printf(mon, " backing_file="); |
| 1160 | monitor_print_filename(mon, bs->backing_file); |
| 1161 | } |
| 1162 | monitor_printf(mon, " ro=%d", bs->read_only); |
| 1163 | monitor_printf(mon, " drv=%s", bs->drv->format_name); |
| 1164 | monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs)); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1165 | } else { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1166 | monitor_printf(mon, " [not inserted]"); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1167 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1168 | monitor_printf(mon, "\n"); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1169 | } |
| 1170 | } |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1171 | |
| 1172 | /* The "info blockstats" command. */ |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1173 | void bdrv_info_stats(Monitor *mon) |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1174 | { |
| 1175 | BlockDriverState *bs; |
| 1176 | |
| 1177 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1178 | monitor_printf(mon, "%s:" |
| 1179 | " rd_bytes=%" PRIu64 |
| 1180 | " wr_bytes=%" PRIu64 |
| 1181 | " rd_operations=%" PRIu64 |
| 1182 | " wr_operations=%" PRIu64 |
aliguori | ebf53fc | 2009-03-11 20:05:29 +0000 | [diff] [blame] | 1183 | "\n", |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1184 | bs->device_name, |
| 1185 | bs->rd_bytes, bs->wr_bytes, |
| 1186 | bs->rd_ops, bs->wr_ops); |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1187 | } |
| 1188 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1189 | |
aliguori | 045df33 | 2009-03-05 23:00:48 +0000 | [diff] [blame] | 1190 | const char *bdrv_get_encrypted_filename(BlockDriverState *bs) |
| 1191 | { |
| 1192 | if (bs->backing_hd && bs->backing_hd->encrypted) |
| 1193 | return bs->backing_file; |
| 1194 | else if (bs->encrypted) |
| 1195 | return bs->filename; |
| 1196 | else |
| 1197 | return NULL; |
| 1198 | } |
| 1199 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1200 | void bdrv_get_backing_filename(BlockDriverState *bs, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1201 | char *filename, int filename_size) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1202 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1203 | if (!bs->backing_hd) { |
| 1204 | pstrcpy(filename, filename_size, ""); |
| 1205 | } else { |
| 1206 | pstrcpy(filename, filename_size, bs->backing_file); |
| 1207 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1210 | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1211 | const uint8_t *buf, int nb_sectors) |
| 1212 | { |
| 1213 | BlockDriver *drv = bs->drv; |
| 1214 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1215 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1216 | if (!drv->bdrv_write_compressed) |
| 1217 | return -ENOTSUP; |
Kevin Wolf | fbb7b4e | 2009-05-08 14:47:24 +0200 | [diff] [blame] | 1218 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 1219 | return -EIO; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1220 | |
| 1221 | if (bs->dirty_tracking) { |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 1222 | set_dirty_bitmap(bs, sector_num, nb_sectors, 1); |
| 1223 | } |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1224 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1225 | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); |
| 1226 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1227 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1228 | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
| 1229 | { |
| 1230 | BlockDriver *drv = bs->drv; |
| 1231 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1232 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1233 | if (!drv->bdrv_get_info) |
| 1234 | return -ENOTSUP; |
| 1235 | memset(bdi, 0, sizeof(*bdi)); |
| 1236 | return drv->bdrv_get_info(bs, bdi); |
| 1237 | } |
| 1238 | |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1239 | int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, |
| 1240 | int64_t pos, int size) |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1241 | { |
| 1242 | BlockDriver *drv = bs->drv; |
| 1243 | if (!drv) |
| 1244 | return -ENOMEDIUM; |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1245 | if (!drv->bdrv_save_vmstate) |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1246 | return -ENOTSUP; |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1247 | return drv->bdrv_save_vmstate(bs, buf, pos, size); |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1250 | int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, |
| 1251 | int64_t pos, int size) |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1252 | { |
| 1253 | BlockDriver *drv = bs->drv; |
| 1254 | if (!drv) |
| 1255 | return -ENOMEDIUM; |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1256 | if (!drv->bdrv_load_vmstate) |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1257 | return -ENOTSUP; |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1258 | return drv->bdrv_load_vmstate(bs, buf, pos, size); |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1261 | /**************************************************************/ |
| 1262 | /* handling of snapshots */ |
| 1263 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1264 | int bdrv_snapshot_create(BlockDriverState *bs, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1265 | QEMUSnapshotInfo *sn_info) |
| 1266 | { |
| 1267 | BlockDriver *drv = bs->drv; |
| 1268 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1269 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1270 | if (!drv->bdrv_snapshot_create) |
| 1271 | return -ENOTSUP; |
| 1272 | return drv->bdrv_snapshot_create(bs, sn_info); |
| 1273 | } |
| 1274 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1275 | int bdrv_snapshot_goto(BlockDriverState *bs, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1276 | const char *snapshot_id) |
| 1277 | { |
| 1278 | BlockDriver *drv = bs->drv; |
| 1279 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1280 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1281 | if (!drv->bdrv_snapshot_goto) |
| 1282 | return -ENOTSUP; |
| 1283 | return drv->bdrv_snapshot_goto(bs, snapshot_id); |
| 1284 | } |
| 1285 | |
| 1286 | int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) |
| 1287 | { |
| 1288 | BlockDriver *drv = bs->drv; |
| 1289 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1290 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1291 | if (!drv->bdrv_snapshot_delete) |
| 1292 | return -ENOTSUP; |
| 1293 | return drv->bdrv_snapshot_delete(bs, snapshot_id); |
| 1294 | } |
| 1295 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1296 | int bdrv_snapshot_list(BlockDriverState *bs, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1297 | QEMUSnapshotInfo **psn_info) |
| 1298 | { |
| 1299 | BlockDriver *drv = bs->drv; |
| 1300 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1301 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1302 | if (!drv->bdrv_snapshot_list) |
| 1303 | return -ENOTSUP; |
| 1304 | return drv->bdrv_snapshot_list(bs, psn_info); |
| 1305 | } |
| 1306 | |
| 1307 | #define NB_SUFFIXES 4 |
| 1308 | |
| 1309 | char *get_human_readable_size(char *buf, int buf_size, int64_t size) |
| 1310 | { |
| 1311 | static const char suffixes[NB_SUFFIXES] = "KMGT"; |
| 1312 | int64_t base; |
| 1313 | int i; |
| 1314 | |
| 1315 | if (size <= 999) { |
| 1316 | snprintf(buf, buf_size, "%" PRId64, size); |
| 1317 | } else { |
| 1318 | base = 1024; |
| 1319 | for(i = 0; i < NB_SUFFIXES; i++) { |
| 1320 | if (size < (10 * base)) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1321 | snprintf(buf, buf_size, "%0.1f%c", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1322 | (double)size / base, |
| 1323 | suffixes[i]); |
| 1324 | break; |
| 1325 | } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1326 | snprintf(buf, buf_size, "%" PRId64 "%c", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1327 | ((size + (base >> 1)) / base), |
| 1328 | suffixes[i]); |
| 1329 | break; |
| 1330 | } |
| 1331 | base = base * 1024; |
| 1332 | } |
| 1333 | } |
| 1334 | return buf; |
| 1335 | } |
| 1336 | |
| 1337 | char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) |
| 1338 | { |
| 1339 | char buf1[128], date_buf[128], clock_buf[128]; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 1340 | #ifdef _WIN32 |
| 1341 | struct tm *ptm; |
| 1342 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1343 | struct tm tm; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 1344 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1345 | time_t ti; |
| 1346 | int64_t secs; |
| 1347 | |
| 1348 | if (!sn) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1349 | snprintf(buf, buf_size, |
| 1350 | "%-10s%-20s%7s%20s%15s", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1351 | "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); |
| 1352 | } else { |
| 1353 | ti = sn->date_sec; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 1354 | #ifdef _WIN32 |
| 1355 | ptm = localtime(&ti); |
| 1356 | strftime(date_buf, sizeof(date_buf), |
| 1357 | "%Y-%m-%d %H:%M:%S", ptm); |
| 1358 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1359 | localtime_r(&ti, &tm); |
| 1360 | strftime(date_buf, sizeof(date_buf), |
| 1361 | "%Y-%m-%d %H:%M:%S", &tm); |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 1362 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1363 | secs = sn->vm_clock_nsec / 1000000000; |
| 1364 | snprintf(clock_buf, sizeof(clock_buf), |
| 1365 | "%02d:%02d:%02d.%03d", |
| 1366 | (int)(secs / 3600), |
| 1367 | (int)((secs / 60) % 60), |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1368 | (int)(secs % 60), |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1369 | (int)((sn->vm_clock_nsec / 1000000) % 1000)); |
| 1370 | snprintf(buf, buf_size, |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1371 | "%-10s%-20s%7s%20s%15s", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1372 | sn->id_str, sn->name, |
| 1373 | get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), |
| 1374 | date_buf, |
| 1375 | clock_buf); |
| 1376 | } |
| 1377 | return buf; |
| 1378 | } |
| 1379 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1380 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1381 | /**************************************************************/ |
| 1382 | /* async I/Os */ |
| 1383 | |
aliguori | 3b69e4b | 2009-01-22 16:59:24 +0000 | [diff] [blame] | 1384 | BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1385 | QEMUIOVector *qiov, int nb_sectors, |
aliguori | 3b69e4b | 2009-01-22 16:59:24 +0000 | [diff] [blame] | 1386 | BlockDriverCompletionFunc *cb, void *opaque) |
| 1387 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1388 | BlockDriver *drv = bs->drv; |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1389 | BlockDriverAIOCB *ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1390 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1391 | if (!drv) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1392 | return NULL; |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 1393 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 1394 | return NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1395 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1396 | ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors, |
| 1397 | cb, opaque); |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1398 | |
| 1399 | if (ret) { |
| 1400 | /* Update stats even though technically transfer has not happened. */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 1401 | bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1402 | bs->rd_ops ++; |
| 1403 | } |
| 1404 | |
| 1405 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1408 | BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, |
| 1409 | QEMUIOVector *qiov, int nb_sectors, |
| 1410 | BlockDriverCompletionFunc *cb, void *opaque) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1411 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1412 | BlockDriver *drv = bs->drv; |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1413 | BlockDriverAIOCB *ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1414 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1415 | if (!drv) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1416 | return NULL; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1417 | if (bs->read_only) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1418 | return NULL; |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 1419 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 1420 | return NULL; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1421 | |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1422 | if (bs->dirty_tracking) { |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 1423 | set_dirty_bitmap(bs, sector_num, nb_sectors, 1); |
| 1424 | } |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1425 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1426 | ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors, |
| 1427 | cb, opaque); |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1428 | |
| 1429 | if (ret) { |
| 1430 | /* Update stats even though technically transfer has not happened. */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 1431 | bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1432 | bs->wr_ops ++; |
| 1433 | } |
| 1434 | |
| 1435 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 1438 | |
| 1439 | typedef struct MultiwriteCB { |
| 1440 | int error; |
| 1441 | int num_requests; |
| 1442 | int num_callbacks; |
| 1443 | struct { |
| 1444 | BlockDriverCompletionFunc *cb; |
| 1445 | void *opaque; |
| 1446 | QEMUIOVector *free_qiov; |
| 1447 | void *free_buf; |
| 1448 | } callbacks[]; |
| 1449 | } MultiwriteCB; |
| 1450 | |
| 1451 | static void multiwrite_user_cb(MultiwriteCB *mcb) |
| 1452 | { |
| 1453 | int i; |
| 1454 | |
| 1455 | for (i = 0; i < mcb->num_callbacks; i++) { |
| 1456 | mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); |
| 1457 | qemu_free(mcb->callbacks[i].free_qiov); |
| 1458 | qemu_free(mcb->callbacks[i].free_buf); |
| 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | static void multiwrite_cb(void *opaque, int ret) |
| 1463 | { |
| 1464 | MultiwriteCB *mcb = opaque; |
| 1465 | |
| 1466 | if (ret < 0) { |
| 1467 | mcb->error = ret; |
| 1468 | multiwrite_user_cb(mcb); |
| 1469 | } |
| 1470 | |
| 1471 | mcb->num_requests--; |
| 1472 | if (mcb->num_requests == 0) { |
| 1473 | if (mcb->error == 0) { |
| 1474 | multiwrite_user_cb(mcb); |
| 1475 | } |
| 1476 | qemu_free(mcb); |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | static int multiwrite_req_compare(const void *a, const void *b) |
| 1481 | { |
| 1482 | return (((BlockRequest*) a)->sector - ((BlockRequest*) b)->sector); |
| 1483 | } |
| 1484 | |
| 1485 | /* |
| 1486 | * Takes a bunch of requests and tries to merge them. Returns the number of |
| 1487 | * requests that remain after merging. |
| 1488 | */ |
| 1489 | static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, |
| 1490 | int num_reqs, MultiwriteCB *mcb) |
| 1491 | { |
| 1492 | int i, outidx; |
| 1493 | |
| 1494 | // Sort requests by start sector |
| 1495 | qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); |
| 1496 | |
| 1497 | // Check if adjacent requests touch the same clusters. If so, combine them, |
| 1498 | // filling up gaps with zero sectors. |
| 1499 | outidx = 0; |
| 1500 | for (i = 1; i < num_reqs; i++) { |
| 1501 | int merge = 0; |
| 1502 | int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; |
| 1503 | |
| 1504 | // This handles the cases that are valid for all block drivers, namely |
| 1505 | // exactly sequential writes and overlapping writes. |
| 1506 | if (reqs[i].sector <= oldreq_last) { |
| 1507 | merge = 1; |
| 1508 | } |
| 1509 | |
| 1510 | // The block driver may decide that it makes sense to combine requests |
| 1511 | // even if there is a gap of some sectors between them. In this case, |
| 1512 | // the gap is filled with zeros (therefore only applicable for yet |
| 1513 | // unused space in format like qcow2). |
| 1514 | if (!merge && bs->drv->bdrv_merge_requests) { |
| 1515 | merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]); |
| 1516 | } |
| 1517 | |
| 1518 | if (merge) { |
| 1519 | size_t size; |
| 1520 | QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov)); |
| 1521 | qemu_iovec_init(qiov, |
| 1522 | reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); |
| 1523 | |
| 1524 | // Add the first request to the merged one. If the requests are |
| 1525 | // overlapping, drop the last sectors of the first request. |
| 1526 | size = (reqs[i].sector - reqs[outidx].sector) << 9; |
| 1527 | qemu_iovec_concat(qiov, reqs[outidx].qiov, size); |
| 1528 | |
| 1529 | // We might need to add some zeros between the two requests |
| 1530 | if (reqs[i].sector > oldreq_last) { |
| 1531 | size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9; |
| 1532 | uint8_t *buf = qemu_blockalign(bs, zero_bytes); |
| 1533 | memset(buf, 0, zero_bytes); |
| 1534 | qemu_iovec_add(qiov, buf, zero_bytes); |
| 1535 | mcb->callbacks[i].free_buf = buf; |
| 1536 | } |
| 1537 | |
| 1538 | // Add the second request |
| 1539 | qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size); |
| 1540 | |
| 1541 | reqs[outidx].nb_sectors += reqs[i].nb_sectors; |
| 1542 | reqs[outidx].qiov = qiov; |
| 1543 | |
| 1544 | mcb->callbacks[i].free_qiov = reqs[outidx].qiov; |
| 1545 | } else { |
| 1546 | outidx++; |
| 1547 | reqs[outidx].sector = reqs[i].sector; |
| 1548 | reqs[outidx].nb_sectors = reqs[i].nb_sectors; |
| 1549 | reqs[outidx].qiov = reqs[i].qiov; |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | return outidx + 1; |
| 1554 | } |
| 1555 | |
| 1556 | /* |
| 1557 | * Submit multiple AIO write requests at once. |
| 1558 | * |
| 1559 | * On success, the function returns 0 and all requests in the reqs array have |
| 1560 | * been submitted. In error case this function returns -1, and any of the |
| 1561 | * requests may or may not be submitted yet. In particular, this means that the |
| 1562 | * callback will be called for some of the requests, for others it won't. The |
| 1563 | * caller must check the error field of the BlockRequest to wait for the right |
| 1564 | * callbacks (if error != 0, no callback will be called). |
| 1565 | * |
| 1566 | * The implementation may modify the contents of the reqs array, e.g. to merge |
| 1567 | * requests. However, the fields opaque and error are left unmodified as they |
| 1568 | * are used to signal failure for a single request to the caller. |
| 1569 | */ |
| 1570 | int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) |
| 1571 | { |
| 1572 | BlockDriverAIOCB *acb; |
| 1573 | MultiwriteCB *mcb; |
| 1574 | int i; |
| 1575 | |
| 1576 | if (num_reqs == 0) { |
| 1577 | return 0; |
| 1578 | } |
| 1579 | |
| 1580 | // Create MultiwriteCB structure |
| 1581 | mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); |
| 1582 | mcb->num_requests = 0; |
| 1583 | mcb->num_callbacks = num_reqs; |
| 1584 | |
| 1585 | for (i = 0; i < num_reqs; i++) { |
| 1586 | mcb->callbacks[i].cb = reqs[i].cb; |
| 1587 | mcb->callbacks[i].opaque = reqs[i].opaque; |
| 1588 | } |
| 1589 | |
| 1590 | // Check for mergable requests |
| 1591 | num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); |
| 1592 | |
| 1593 | // Run the aio requests |
| 1594 | for (i = 0; i < num_reqs; i++) { |
| 1595 | acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, |
| 1596 | reqs[i].nb_sectors, multiwrite_cb, mcb); |
| 1597 | |
| 1598 | if (acb == NULL) { |
| 1599 | // We can only fail the whole thing if no request has been |
| 1600 | // submitted yet. Otherwise we'll wait for the submitted AIOs to |
| 1601 | // complete and report the error in the callback. |
| 1602 | if (mcb->num_requests == 0) { |
| 1603 | reqs[i].error = EIO; |
| 1604 | goto fail; |
| 1605 | } else { |
| 1606 | mcb->error = EIO; |
| 1607 | break; |
| 1608 | } |
| 1609 | } else { |
| 1610 | mcb->num_requests++; |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | return 0; |
| 1615 | |
| 1616 | fail: |
| 1617 | free(mcb); |
| 1618 | return -1; |
| 1619 | } |
| 1620 | |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 1621 | BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, |
| 1622 | BlockDriverCompletionFunc *cb, void *opaque) |
| 1623 | { |
| 1624 | BlockDriver *drv = bs->drv; |
| 1625 | |
| 1626 | if (!drv) |
| 1627 | return NULL; |
| 1628 | |
| 1629 | /* |
| 1630 | * Note that unlike bdrv_flush the driver is reponsible for flushing a |
| 1631 | * backing image if it exists. |
| 1632 | */ |
| 1633 | return drv->bdrv_aio_flush(bs, cb, opaque); |
| 1634 | } |
| 1635 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1636 | void bdrv_aio_cancel(BlockDriverAIOCB *acb) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1637 | { |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 1638 | acb->pool->cancel(acb); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1641 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1642 | /**************************************************************/ |
| 1643 | /* async block device emulation */ |
| 1644 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 1645 | typedef struct BlockDriverAIOCBSync { |
| 1646 | BlockDriverAIOCB common; |
| 1647 | QEMUBH *bh; |
| 1648 | int ret; |
| 1649 | /* vector translation state */ |
| 1650 | QEMUIOVector *qiov; |
| 1651 | uint8_t *bounce; |
| 1652 | int is_write; |
| 1653 | } BlockDriverAIOCBSync; |
| 1654 | |
| 1655 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) |
| 1656 | { |
| 1657 | BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb; |
Dor Laor | 6a7ad29 | 2009-06-01 12:07:23 +0300 | [diff] [blame] | 1658 | qemu_bh_delete(acb->bh); |
Avi Kivity | 36afc45 | 2009-06-23 16:20:36 +0300 | [diff] [blame] | 1659 | acb->bh = NULL; |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 1660 | qemu_aio_release(acb); |
| 1661 | } |
| 1662 | |
| 1663 | static AIOPool bdrv_em_aio_pool = { |
| 1664 | .aiocb_size = sizeof(BlockDriverAIOCBSync), |
| 1665 | .cancel = bdrv_aio_cancel_em, |
| 1666 | }; |
| 1667 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1668 | static void bdrv_aio_bh_cb(void *opaque) |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 1669 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1670 | BlockDriverAIOCBSync *acb = opaque; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1671 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1672 | if (!acb->is_write) |
| 1673 | qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); |
aliguori | ceb42de | 2009-04-07 18:43:28 +0000 | [diff] [blame] | 1674 | qemu_vfree(acb->bounce); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1675 | acb->common.cb(acb->common.opaque, acb->ret); |
Dor Laor | 6a7ad29 | 2009-06-01 12:07:23 +0300 | [diff] [blame] | 1676 | qemu_bh_delete(acb->bh); |
Avi Kivity | 36afc45 | 2009-06-23 16:20:36 +0300 | [diff] [blame] | 1677 | acb->bh = NULL; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1678 | qemu_aio_release(acb); |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 1679 | } |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 1680 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1681 | static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, |
| 1682 | int64_t sector_num, |
| 1683 | QEMUIOVector *qiov, |
| 1684 | int nb_sectors, |
| 1685 | BlockDriverCompletionFunc *cb, |
| 1686 | void *opaque, |
| 1687 | int is_write) |
| 1688 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1689 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1690 | BlockDriverAIOCBSync *acb; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1691 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 1692 | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1693 | acb->is_write = is_write; |
| 1694 | acb->qiov = qiov; |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 1695 | acb->bounce = qemu_blockalign(bs, qiov->size); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1696 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1697 | if (!acb->bh) |
| 1698 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1699 | |
| 1700 | if (is_write) { |
| 1701 | qemu_iovec_to_buffer(acb->qiov, acb->bounce); |
| 1702 | acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors); |
| 1703 | } else { |
| 1704 | acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors); |
| 1705 | } |
| 1706 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1707 | qemu_bh_schedule(acb->bh); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1708 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1709 | return &acb->common; |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1712 | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, |
| 1713 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1714 | BlockDriverCompletionFunc *cb, void *opaque) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1715 | { |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1716 | 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] | 1717 | } |
| 1718 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1719 | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, |
| 1720 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
| 1721 | BlockDriverCompletionFunc *cb, void *opaque) |
| 1722 | { |
| 1723 | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); |
| 1724 | } |
| 1725 | |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 1726 | static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, |
| 1727 | BlockDriverCompletionFunc *cb, void *opaque) |
| 1728 | { |
| 1729 | BlockDriverAIOCBSync *acb; |
| 1730 | |
| 1731 | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); |
| 1732 | acb->is_write = 1; /* don't bounce in the completion hadler */ |
| 1733 | acb->qiov = NULL; |
| 1734 | acb->bounce = NULL; |
| 1735 | acb->ret = 0; |
| 1736 | |
| 1737 | if (!acb->bh) |
| 1738 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
| 1739 | |
| 1740 | bdrv_flush(bs); |
| 1741 | qemu_bh_schedule(acb->bh); |
| 1742 | return &acb->common; |
| 1743 | } |
| 1744 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1745 | /**************************************************************/ |
| 1746 | /* sync block device emulation */ |
| 1747 | |
| 1748 | static void bdrv_rw_em_cb(void *opaque, int ret) |
| 1749 | { |
| 1750 | *(int *)opaque = ret; |
| 1751 | } |
| 1752 | |
| 1753 | #define NOT_DONE 0x7fffffff |
| 1754 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1755 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1756 | uint8_t *buf, int nb_sectors) |
| 1757 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1758 | int async_ret; |
| 1759 | BlockDriverAIOCB *acb; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1760 | struct iovec iov; |
| 1761 | QEMUIOVector qiov; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1762 | |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 1763 | async_context_push(); |
| 1764 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1765 | async_ret = NOT_DONE; |
blueswir1 | 3f4cb3d | 2009-04-13 16:31:01 +0000 | [diff] [blame] | 1766 | iov.iov_base = (void *)buf; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1767 | iov.iov_len = nb_sectors * 512; |
| 1768 | qemu_iovec_init_external(&qiov, &iov, 1); |
| 1769 | acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors, |
| 1770 | bdrv_rw_em_cb, &async_ret); |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 1771 | if (acb == NULL) { |
| 1772 | async_ret = -1; |
| 1773 | goto fail; |
| 1774 | } |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 1775 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1776 | while (async_ret == NOT_DONE) { |
| 1777 | qemu_aio_wait(); |
| 1778 | } |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 1779 | |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 1780 | |
| 1781 | fail: |
| 1782 | async_context_pop(); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1783 | return async_ret; |
| 1784 | } |
| 1785 | |
| 1786 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
| 1787 | const uint8_t *buf, int nb_sectors) |
| 1788 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1789 | int async_ret; |
| 1790 | BlockDriverAIOCB *acb; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1791 | struct iovec iov; |
| 1792 | QEMUIOVector qiov; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1793 | |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 1794 | async_context_push(); |
| 1795 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1796 | async_ret = NOT_DONE; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1797 | iov.iov_base = (void *)buf; |
| 1798 | iov.iov_len = nb_sectors * 512; |
| 1799 | qemu_iovec_init_external(&qiov, &iov, 1); |
| 1800 | acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors, |
| 1801 | bdrv_rw_em_cb, &async_ret); |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 1802 | if (acb == NULL) { |
| 1803 | async_ret = -1; |
| 1804 | goto fail; |
| 1805 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1806 | while (async_ret == NOT_DONE) { |
| 1807 | qemu_aio_wait(); |
| 1808 | } |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 1809 | |
| 1810 | fail: |
| 1811 | async_context_pop(); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1812 | return async_ret; |
| 1813 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1814 | |
| 1815 | void bdrv_init(void) |
| 1816 | { |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 1817 | module_call_init(MODULE_INIT_BLOCK); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1818 | } |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1819 | |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 1820 | void bdrv_init_with_whitelist(void) |
| 1821 | { |
| 1822 | use_bdrv_whitelist = 1; |
| 1823 | bdrv_init(); |
| 1824 | } |
| 1825 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 1826 | void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, |
| 1827 | BlockDriverCompletionFunc *cb, void *opaque) |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 1828 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1829 | BlockDriverAIOCB *acb; |
| 1830 | |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 1831 | if (pool->free_aiocb) { |
| 1832 | acb = pool->free_aiocb; |
| 1833 | pool->free_aiocb = acb->next; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1834 | } else { |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 1835 | acb = qemu_mallocz(pool->aiocb_size); |
| 1836 | acb->pool = pool; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1837 | } |
| 1838 | acb->bs = bs; |
| 1839 | acb->cb = cb; |
| 1840 | acb->opaque = opaque; |
| 1841 | return acb; |
| 1842 | } |
| 1843 | |
| 1844 | void qemu_aio_release(void *p) |
| 1845 | { |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 1846 | BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; |
| 1847 | AIOPool *pool = acb->pool; |
| 1848 | acb->next = pool->free_aiocb; |
| 1849 | pool->free_aiocb = acb; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1850 | } |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1851 | |
| 1852 | /**************************************************************/ |
| 1853 | /* removable device support */ |
| 1854 | |
| 1855 | /** |
| 1856 | * Return TRUE if the media is present |
| 1857 | */ |
| 1858 | int bdrv_is_inserted(BlockDriverState *bs) |
| 1859 | { |
| 1860 | BlockDriver *drv = bs->drv; |
| 1861 | int ret; |
| 1862 | if (!drv) |
| 1863 | return 0; |
| 1864 | if (!drv->bdrv_is_inserted) |
| 1865 | return 1; |
| 1866 | ret = drv->bdrv_is_inserted(bs); |
| 1867 | return ret; |
| 1868 | } |
| 1869 | |
| 1870 | /** |
| 1871 | * Return TRUE if the media changed since the last call to this |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1872 | * function. It is currently only used for floppy disks |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1873 | */ |
| 1874 | int bdrv_media_changed(BlockDriverState *bs) |
| 1875 | { |
| 1876 | BlockDriver *drv = bs->drv; |
| 1877 | int ret; |
| 1878 | |
| 1879 | if (!drv || !drv->bdrv_media_changed) |
| 1880 | ret = -ENOTSUP; |
| 1881 | else |
| 1882 | ret = drv->bdrv_media_changed(bs); |
| 1883 | if (ret == -ENOTSUP) |
| 1884 | ret = bs->media_changed; |
| 1885 | bs->media_changed = 0; |
| 1886 | return ret; |
| 1887 | } |
| 1888 | |
| 1889 | /** |
| 1890 | * If eject_flag is TRUE, eject the media. Otherwise, close the tray |
| 1891 | */ |
Mark McLoughlin | aea2a33 | 2009-05-27 10:06:11 +0100 | [diff] [blame] | 1892 | int bdrv_eject(BlockDriverState *bs, int eject_flag) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1893 | { |
| 1894 | BlockDriver *drv = bs->drv; |
| 1895 | int ret; |
| 1896 | |
Mark McLoughlin | aea2a33 | 2009-05-27 10:06:11 +0100 | [diff] [blame] | 1897 | if (bs->locked) { |
| 1898 | return -EBUSY; |
| 1899 | } |
| 1900 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1901 | if (!drv || !drv->bdrv_eject) { |
| 1902 | ret = -ENOTSUP; |
| 1903 | } else { |
| 1904 | ret = drv->bdrv_eject(bs, eject_flag); |
| 1905 | } |
| 1906 | if (ret == -ENOTSUP) { |
| 1907 | if (eject_flag) |
| 1908 | bdrv_close(bs); |
Mark McLoughlin | aea2a33 | 2009-05-27 10:06:11 +0100 | [diff] [blame] | 1909 | ret = 0; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1910 | } |
Mark McLoughlin | aea2a33 | 2009-05-27 10:06:11 +0100 | [diff] [blame] | 1911 | |
| 1912 | return ret; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | int bdrv_is_locked(BlockDriverState *bs) |
| 1916 | { |
| 1917 | return bs->locked; |
| 1918 | } |
| 1919 | |
| 1920 | /** |
| 1921 | * Lock or unlock the media (if it is locked, the user won't be able |
| 1922 | * to eject it manually). |
| 1923 | */ |
| 1924 | void bdrv_set_locked(BlockDriverState *bs, int locked) |
| 1925 | { |
| 1926 | BlockDriver *drv = bs->drv; |
| 1927 | |
| 1928 | bs->locked = locked; |
| 1929 | if (drv && drv->bdrv_set_locked) { |
| 1930 | drv->bdrv_set_locked(bs, locked); |
| 1931 | } |
| 1932 | } |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 1933 | |
| 1934 | /* needed for generic scsi interface */ |
| 1935 | |
| 1936 | int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
| 1937 | { |
| 1938 | BlockDriver *drv = bs->drv; |
| 1939 | |
| 1940 | if (drv && drv->bdrv_ioctl) |
| 1941 | return drv->bdrv_ioctl(bs, req, buf); |
| 1942 | return -ENOTSUP; |
| 1943 | } |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 1944 | |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 1945 | BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, |
| 1946 | unsigned long int req, void *buf, |
| 1947 | BlockDriverCompletionFunc *cb, void *opaque) |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 1948 | { |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 1949 | BlockDriver *drv = bs->drv; |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 1950 | |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 1951 | if (drv && drv->bdrv_aio_ioctl) |
| 1952 | return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); |
| 1953 | return NULL; |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 1954 | } |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 1955 | |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 1956 | |
| 1957 | |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 1958 | void *qemu_blockalign(BlockDriverState *bs, size_t size) |
| 1959 | { |
| 1960 | return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); |
| 1961 | } |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 1962 | |
| 1963 | void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) |
| 1964 | { |
| 1965 | int64_t bitmap_size; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1966 | |
| 1967 | if (enable) { |
| 1968 | if (bs->dirty_tracking == 0) { |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 1969 | int64_t i; |
| 1970 | uint8_t test; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1971 | |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 1972 | bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS); |
| 1973 | bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK; |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 1974 | bitmap_size++; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1975 | |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 1976 | bs->dirty_bitmap = qemu_mallocz(bitmap_size); |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1977 | |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 1978 | bs->dirty_tracking = enable; |
| 1979 | for(i = 0; i < bitmap_size; i++) test = bs->dirty_bitmap[i]; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1980 | } |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 1981 | } else { |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1982 | if (bs->dirty_tracking != 0) { |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 1983 | qemu_free(bs->dirty_bitmap); |
| 1984 | bs->dirty_tracking = enable; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1985 | } |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | int bdrv_get_dirty(BlockDriverState *bs, int64_t sector) |
| 1990 | { |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 1991 | int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1992 | |
| 1993 | if (bs->dirty_bitmap != NULL && |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame^] | 1994 | (sector << BDRV_SECTOR_BITS) <= bdrv_getlength(bs)) { |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 1995 | return bs->dirty_bitmap[chunk]; |
| 1996 | } else { |
| 1997 | return 0; |
| 1998 | } |
| 1999 | } |
| 2000 | |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2001 | void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, |
| 2002 | int nr_sectors) |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 2003 | { |
| 2004 | set_dirty_bitmap(bs, cur_sector, nr_sectors, 0); |
| 2005 | } |