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