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" |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 29 | #include "qemu-objects.h" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 30 | |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 31 | #ifdef CONFIG_BSD |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 32 | #include <sys/types.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/ioctl.h> |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 35 | #include <sys/queue.h> |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 36 | #ifndef __DragonFly__ |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 37 | #include <sys/disk.h> |
| 38 | #endif |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 39 | #endif |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 40 | |
aliguori | 49dc768 | 2009-03-08 16:26:59 +0000 | [diff] [blame] | 41 | #ifdef _WIN32 |
| 42 | #include <windows.h> |
| 43 | #endif |
| 44 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 45 | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, |
| 46 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
aliguori | c87c067 | 2009-04-07 18:43:20 +0000 | [diff] [blame] | 47 | BlockDriverCompletionFunc *cb, void *opaque); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 48 | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, |
| 49 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 50 | BlockDriverCompletionFunc *cb, void *opaque); |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 51 | static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, |
| 52 | BlockDriverCompletionFunc *cb, void *opaque); |
Alexander Graf | 016f5cf | 2010-05-26 17:51:49 +0200 | [diff] [blame] | 53 | static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs, |
| 54 | BlockDriverCompletionFunc *cb, void *opaque); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 55 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 56 | uint8_t *buf, int nb_sectors); |
| 57 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
| 58 | const uint8_t *buf, int nb_sectors); |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 59 | |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 60 | static QTAILQ_HEAD(, BlockDriverState) bdrv_states = |
| 61 | QTAILQ_HEAD_INITIALIZER(bdrv_states); |
blueswir1 | 7ee930d | 2008-09-17 19:04:14 +0000 | [diff] [blame] | 62 | |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 63 | static QLIST_HEAD(, BlockDriver) bdrv_drivers = |
| 64 | QLIST_HEAD_INITIALIZER(bdrv_drivers); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 65 | |
Markus Armbruster | f9092b1 | 2010-06-25 10:33:39 +0200 | [diff] [blame] | 66 | /* The device to use for VM snapshots */ |
| 67 | static BlockDriverState *bs_snapshots; |
| 68 | |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 69 | /* If non-zero, use only whitelisted block drivers */ |
| 70 | static int use_bdrv_whitelist; |
| 71 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 72 | int path_is_absolute(const char *path) |
| 73 | { |
| 74 | const char *p; |
bellard | 2166442 | 2007-01-07 18:22:37 +0000 | [diff] [blame] | 75 | #ifdef _WIN32 |
| 76 | /* specific case for names like: "\\.\d:" */ |
| 77 | if (*path == '/' || *path == '\\') |
| 78 | return 1; |
| 79 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 80 | p = strchr(path, ':'); |
| 81 | if (p) |
| 82 | p++; |
| 83 | else |
| 84 | p = path; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 85 | #ifdef _WIN32 |
| 86 | return (*p == '/' || *p == '\\'); |
| 87 | #else |
| 88 | return (*p == '/'); |
| 89 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /* if filename is absolute, just copy it to dest. Otherwise, build a |
| 93 | path to it by considering it is relative to base_path. URL are |
| 94 | supported. */ |
| 95 | void path_combine(char *dest, int dest_size, |
| 96 | const char *base_path, |
| 97 | const char *filename) |
| 98 | { |
| 99 | const char *p, *p1; |
| 100 | int len; |
| 101 | |
| 102 | if (dest_size <= 0) |
| 103 | return; |
| 104 | if (path_is_absolute(filename)) { |
| 105 | pstrcpy(dest, dest_size, filename); |
| 106 | } else { |
| 107 | p = strchr(base_path, ':'); |
| 108 | if (p) |
| 109 | p++; |
| 110 | else |
| 111 | p = base_path; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 112 | p1 = strrchr(base_path, '/'); |
| 113 | #ifdef _WIN32 |
| 114 | { |
| 115 | const char *p2; |
| 116 | p2 = strrchr(base_path, '\\'); |
| 117 | if (!p1 || p2 > p1) |
| 118 | p1 = p2; |
| 119 | } |
| 120 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 121 | if (p1) |
| 122 | p1++; |
| 123 | else |
| 124 | p1 = base_path; |
| 125 | if (p1 > p) |
| 126 | p = p1; |
| 127 | len = p - base_path; |
| 128 | if (len > dest_size - 1) |
| 129 | len = dest_size - 1; |
| 130 | memcpy(dest, base_path, len); |
| 131 | dest[len] = '\0'; |
| 132 | pstrcat(dest, dest_size, filename); |
| 133 | } |
| 134 | } |
| 135 | |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 136 | void bdrv_register(BlockDriver *bdrv) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 137 | { |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 138 | if (!bdrv->bdrv_aio_readv) { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 139 | /* add AIO emulation layer */ |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 140 | bdrv->bdrv_aio_readv = bdrv_aio_readv_em; |
| 141 | bdrv->bdrv_aio_writev = bdrv_aio_writev_em; |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 142 | } else if (!bdrv->bdrv_read) { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 143 | /* add synchronous IO emulation layer */ |
| 144 | bdrv->bdrv_read = bdrv_read_em; |
| 145 | bdrv->bdrv_write = bdrv_write_em; |
| 146 | } |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 147 | |
| 148 | if (!bdrv->bdrv_aio_flush) |
| 149 | bdrv->bdrv_aio_flush = bdrv_aio_flush_em; |
| 150 | |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 151 | QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 152 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 153 | |
| 154 | /* create a new block device (by default it is empty) */ |
| 155 | BlockDriverState *bdrv_new(const char *device_name) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 156 | { |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 157 | BlockDriverState *bs; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 158 | |
| 159 | bs = qemu_mallocz(sizeof(BlockDriverState)); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 160 | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 161 | if (device_name[0] != '\0') { |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 162 | QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 163 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 164 | return bs; |
| 165 | } |
| 166 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 167 | BlockDriver *bdrv_find_format(const char *format_name) |
| 168 | { |
| 169 | BlockDriver *drv1; |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 170 | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
| 171 | if (!strcmp(drv1->format_name, format_name)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 172 | return drv1; |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 173 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 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 | |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 211 | int bdrv_create_file(const char* filename, QEMUOptionParameter *options) |
| 212 | { |
| 213 | BlockDriver *drv; |
| 214 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 215 | drv = bdrv_find_protocol(filename); |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 216 | if (drv == NULL) { |
| 217 | drv = bdrv_find_format("file"); |
| 218 | } |
| 219 | |
| 220 | return bdrv_create(drv, filename, options); |
| 221 | } |
| 222 | |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 223 | #ifdef _WIN32 |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 224 | void get_tmp_filename(char *filename, int size) |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 225 | { |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 226 | char temp_dir[MAX_PATH]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 227 | |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 228 | GetTempPath(MAX_PATH, temp_dir); |
| 229 | GetTempFileName(temp_dir, "qem", 0, filename); |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 230 | } |
| 231 | #else |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 232 | void get_tmp_filename(char *filename, int size) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 233 | { |
| 234 | int fd; |
blueswir1 | 7ccfb2e | 2008-09-14 06:45:34 +0000 | [diff] [blame] | 235 | const char *tmpdir; |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 236 | /* XXX: race condition possible */ |
aurel32 | 0badc1e | 2008-03-10 00:05:34 +0000 | [diff] [blame] | 237 | tmpdir = getenv("TMPDIR"); |
| 238 | if (!tmpdir) |
| 239 | tmpdir = "/tmp"; |
| 240 | snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 241 | fd = mkstemp(filename); |
| 242 | close(fd); |
| 243 | } |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 244 | #endif |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 245 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 246 | #ifdef _WIN32 |
bellard | f45512f | 2006-08-23 21:40:13 +0000 | [diff] [blame] | 247 | static int is_windows_drive_prefix(const char *filename) |
| 248 | { |
| 249 | return (((filename[0] >= 'a' && filename[0] <= 'z') || |
| 250 | (filename[0] >= 'A' && filename[0] <= 'Z')) && |
| 251 | filename[1] == ':'); |
| 252 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 253 | |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 254 | int is_windows_drive(const char *filename) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 255 | { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 256 | if (is_windows_drive_prefix(filename) && |
bellard | f45512f | 2006-08-23 21:40:13 +0000 | [diff] [blame] | 257 | filename[2] == '\0') |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 258 | return 1; |
| 259 | if (strstart(filename, "\\\\.\\", NULL) || |
| 260 | strstart(filename, "//./", NULL)) |
| 261 | return 1; |
| 262 | return 0; |
| 263 | } |
| 264 | #endif |
| 265 | |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 266 | /* |
| 267 | * Detect host devices. By convention, /dev/cdrom[N] is always |
| 268 | * recognized as a host CDROM. |
| 269 | */ |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 270 | static BlockDriver *find_hdev_driver(const char *filename) |
| 271 | { |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 272 | int score_max = 0, score; |
| 273 | BlockDriver *drv = NULL, *d; |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 274 | |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 275 | QLIST_FOREACH(d, &bdrv_drivers, list) { |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 276 | if (d->bdrv_probe_device) { |
| 277 | score = d->bdrv_probe_device(filename); |
| 278 | if (score > score_max) { |
| 279 | score_max = score; |
| 280 | drv = d; |
| 281 | } |
| 282 | } |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 283 | } |
| 284 | |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 285 | return drv; |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 286 | } |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 287 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 288 | BlockDriver *bdrv_find_protocol(const char *filename) |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 289 | { |
| 290 | BlockDriver *drv1; |
| 291 | char protocol[128]; |
| 292 | int len; |
| 293 | const char *p; |
| 294 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 295 | /* TODO Drivers without bdrv_file_open must be specified explicitly */ |
| 296 | |
Christoph Hellwig | 39508e7 | 2010-06-23 12:25:17 +0200 | [diff] [blame] | 297 | /* |
| 298 | * XXX(hch): we really should not let host device detection |
| 299 | * override an explicit protocol specification, but moving this |
| 300 | * later breaks access to device names with colons in them. |
| 301 | * Thanks to the brain-dead persistent naming schemes on udev- |
| 302 | * based Linux systems those actually are quite common. |
| 303 | */ |
| 304 | drv1 = find_hdev_driver(filename); |
| 305 | if (drv1) { |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 306 | return drv1; |
| 307 | } |
Christoph Hellwig | 39508e7 | 2010-06-23 12:25:17 +0200 | [diff] [blame] | 308 | |
| 309 | #ifdef _WIN32 |
| 310 | if (is_windows_drive(filename) || |
| 311 | is_windows_drive_prefix(filename)) |
| 312 | return bdrv_find_format("file"); |
| 313 | #endif |
| 314 | |
| 315 | p = strchr(filename, ':'); |
| 316 | if (!p) { |
| 317 | return bdrv_find_format("file"); |
| 318 | } |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 319 | len = p - filename; |
| 320 | if (len > sizeof(protocol) - 1) |
| 321 | len = sizeof(protocol) - 1; |
| 322 | memcpy(protocol, filename, len); |
| 323 | protocol[len] = '\0'; |
| 324 | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
| 325 | if (drv1->protocol_name && |
| 326 | !strcmp(drv1->protocol_name, protocol)) { |
| 327 | return drv1; |
| 328 | } |
| 329 | } |
| 330 | return NULL; |
| 331 | } |
| 332 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 333 | static BlockDriver *find_image_format(const char *filename) |
| 334 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 335 | int ret, score, score_max; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 336 | BlockDriver *drv1, *drv; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 337 | uint8_t buf[2048]; |
| 338 | BlockDriverState *bs; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 339 | |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 340 | ret = bdrv_file_open(&bs, filename, 0); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 341 | if (ret < 0) |
| 342 | return NULL; |
Nicholas Bellinger | f8ea0b0 | 2010-05-17 09:45:57 -0700 | [diff] [blame] | 343 | |
Kevin Wolf | 08a0055 | 2010-06-01 18:37:31 +0200 | [diff] [blame] | 344 | /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ |
| 345 | if (bs->sg || !bdrv_is_inserted(bs)) { |
Nicholas A. Bellinger | 1a39685 | 2010-05-27 08:56:28 -0700 | [diff] [blame] | 346 | bdrv_delete(bs); |
Nicholas Bellinger | f8ea0b0 | 2010-05-17 09:45:57 -0700 | [diff] [blame] | 347 | return bdrv_find_format("raw"); |
Nicholas A. Bellinger | 1a39685 | 2010-05-27 08:56:28 -0700 | [diff] [blame] | 348 | } |
Nicholas Bellinger | f8ea0b0 | 2010-05-17 09:45:57 -0700 | [diff] [blame] | 349 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 350 | ret = bdrv_pread(bs, 0, buf, sizeof(buf)); |
| 351 | bdrv_delete(bs); |
| 352 | if (ret < 0) { |
| 353 | return NULL; |
| 354 | } |
| 355 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 356 | score_max = 0; |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 357 | drv = NULL; |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 358 | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 359 | if (drv1->bdrv_probe) { |
| 360 | score = drv1->bdrv_probe(buf, ret, filename); |
| 361 | if (score > score_max) { |
| 362 | score_max = score; |
| 363 | drv = drv1; |
| 364 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | return drv; |
| 368 | } |
| 369 | |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 370 | /** |
| 371 | * Set the current 'total_sectors' value |
| 372 | */ |
| 373 | static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) |
| 374 | { |
| 375 | BlockDriver *drv = bs->drv; |
| 376 | |
Nicholas Bellinger | 396759a | 2010-05-17 09:46:04 -0700 | [diff] [blame] | 377 | /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ |
| 378 | if (bs->sg) |
| 379 | return 0; |
| 380 | |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 381 | /* query actual device if possible, otherwise just trust the hint */ |
| 382 | if (drv->bdrv_getlength) { |
| 383 | int64_t length = drv->bdrv_getlength(bs); |
| 384 | if (length < 0) { |
| 385 | return length; |
| 386 | } |
| 387 | hint = length >> BDRV_SECTOR_BITS; |
| 388 | } |
| 389 | |
| 390 | bs->total_sectors = hint; |
| 391 | return 0; |
| 392 | } |
| 393 | |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 394 | /* |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 395 | * Common part for opening disk images and files |
| 396 | */ |
| 397 | static int bdrv_open_common(BlockDriverState *bs, const char *filename, |
| 398 | int flags, BlockDriver *drv) |
| 399 | { |
| 400 | int ret, open_flags; |
| 401 | |
| 402 | assert(drv != NULL); |
| 403 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 404 | bs->file = NULL; |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 405 | bs->total_sectors = 0; |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 406 | bs->encrypted = 0; |
| 407 | bs->valid_key = 0; |
| 408 | bs->open_flags = flags; |
| 409 | /* buffer_alignment defaulted to 512, drivers can change this value */ |
| 410 | bs->buffer_alignment = 512; |
| 411 | |
| 412 | pstrcpy(bs->filename, sizeof(bs->filename), filename); |
| 413 | |
| 414 | if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) { |
| 415 | return -ENOTSUP; |
| 416 | } |
| 417 | |
| 418 | bs->drv = drv; |
| 419 | bs->opaque = qemu_mallocz(drv->instance_size); |
| 420 | |
| 421 | /* |
| 422 | * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a |
| 423 | * write cache to the guest. We do need the fdatasync to flush |
| 424 | * out transactions for block allocations, and we maybe have a |
| 425 | * volatile write cache in our backing device to deal with. |
| 426 | */ |
| 427 | if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE)) |
| 428 | bs->enable_write_cache = 1; |
| 429 | |
| 430 | /* |
| 431 | * Clear flags that are internal to the block layer before opening the |
| 432 | * image. |
| 433 | */ |
| 434 | open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); |
| 435 | |
| 436 | /* |
| 437 | * Snapshots should be writeable. |
| 438 | */ |
| 439 | if (bs->is_temporary) { |
| 440 | open_flags |= BDRV_O_RDWR; |
| 441 | } |
| 442 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 443 | /* Open the image, either directly or using a protocol */ |
| 444 | if (drv->bdrv_file_open) { |
| 445 | ret = drv->bdrv_file_open(bs, filename, open_flags); |
| 446 | } else { |
| 447 | ret = bdrv_file_open(&bs->file, filename, open_flags); |
| 448 | if (ret >= 0) { |
| 449 | ret = drv->bdrv_open(bs, open_flags); |
| 450 | } |
| 451 | } |
| 452 | |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 453 | if (ret < 0) { |
| 454 | goto free_and_fail; |
| 455 | } |
| 456 | |
| 457 | bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR); |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 458 | |
| 459 | ret = refresh_total_sectors(bs, bs->total_sectors); |
| 460 | if (ret < 0) { |
| 461 | goto free_and_fail; |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 462 | } |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 463 | |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 464 | #ifndef _WIN32 |
| 465 | if (bs->is_temporary) { |
| 466 | unlink(filename); |
| 467 | } |
| 468 | #endif |
| 469 | return 0; |
| 470 | |
| 471 | free_and_fail: |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 472 | if (bs->file) { |
| 473 | bdrv_delete(bs->file); |
| 474 | bs->file = NULL; |
| 475 | } |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 476 | qemu_free(bs->opaque); |
| 477 | bs->opaque = NULL; |
| 478 | bs->drv = NULL; |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | /* |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 483 | * Opens a file using a protocol (file, host_device, nbd, ...) |
| 484 | */ |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 485 | int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 486 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 487 | BlockDriverState *bs; |
Christoph Hellwig | 6db9560 | 2010-04-05 16:53:57 +0200 | [diff] [blame] | 488 | BlockDriver *drv; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 489 | int ret; |
| 490 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 491 | drv = bdrv_find_protocol(filename); |
Christoph Hellwig | 6db9560 | 2010-04-05 16:53:57 +0200 | [diff] [blame] | 492 | if (!drv) { |
| 493 | return -ENOENT; |
| 494 | } |
| 495 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 496 | bs = bdrv_new(""); |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 497 | ret = bdrv_open_common(bs, filename, flags, drv); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 498 | if (ret < 0) { |
| 499 | bdrv_delete(bs); |
| 500 | return ret; |
bellard | 3b0d4f6 | 2005-10-30 18:30:10 +0000 | [diff] [blame] | 501 | } |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 502 | bs->growable = 1; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 503 | *pbs = bs; |
| 504 | return 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 505 | } |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 506 | |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 507 | /* |
| 508 | * Opens a disk image (raw, qcow2, vmdk, ...) |
| 509 | */ |
Kevin Wolf | d6e9098 | 2010-03-31 14:40:27 +0200 | [diff] [blame] | 510 | int bdrv_open(BlockDriverState *bs, const char *filename, int flags, |
| 511 | BlockDriver *drv) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 512 | { |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 513 | int ret; |
bellard | 712e787 | 2005-04-28 21:09:32 +0000 | [diff] [blame] | 514 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 515 | if (flags & BDRV_O_SNAPSHOT) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 516 | BlockDriverState *bs1; |
| 517 | int64_t total_size; |
aliguori | 7c96d46 | 2008-09-12 17:54:13 +0000 | [diff] [blame] | 518 | int is_protocol = 0; |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 519 | BlockDriver *bdrv_qcow2; |
| 520 | QEMUOptionParameter *options; |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 521 | char tmp_filename[PATH_MAX]; |
| 522 | char backing_filename[PATH_MAX]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 523 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 524 | /* if snapshot, we create a temporary backing file and open it |
| 525 | instead of opening 'filename' directly */ |
| 526 | |
| 527 | /* if there is a backing file, use it */ |
| 528 | bs1 = bdrv_new(""); |
Kevin Wolf | d6e9098 | 2010-03-31 14:40:27 +0200 | [diff] [blame] | 529 | ret = bdrv_open(bs1, filename, 0, drv); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 530 | if (ret < 0) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 531 | bdrv_delete(bs1); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 532 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 533 | } |
Jes Sorensen | 3e82990 | 2010-05-27 16:20:30 +0200 | [diff] [blame] | 534 | total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; |
aliguori | 7c96d46 | 2008-09-12 17:54:13 +0000 | [diff] [blame] | 535 | |
| 536 | if (bs1->drv && bs1->drv->protocol_name) |
| 537 | is_protocol = 1; |
| 538 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 539 | bdrv_delete(bs1); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 540 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 541 | get_tmp_filename(tmp_filename, sizeof(tmp_filename)); |
aliguori | 7c96d46 | 2008-09-12 17:54:13 +0000 | [diff] [blame] | 542 | |
| 543 | /* Real path is meaningless for protocols */ |
| 544 | if (is_protocol) |
| 545 | snprintf(backing_filename, sizeof(backing_filename), |
| 546 | "%s", filename); |
Kirill A. Shutemov | 114cdfa | 2009-12-25 18:19:22 +0000 | [diff] [blame] | 547 | else if (!realpath(filename, backing_filename)) |
| 548 | return -errno; |
aliguori | 7c96d46 | 2008-09-12 17:54:13 +0000 | [diff] [blame] | 549 | |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 550 | bdrv_qcow2 = bdrv_find_format("qcow2"); |
| 551 | options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); |
| 552 | |
Jes Sorensen | 3e82990 | 2010-05-27 16:20:30 +0200 | [diff] [blame] | 553 | set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size); |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 554 | set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); |
| 555 | if (drv) { |
| 556 | set_option_parameter(options, BLOCK_OPT_BACKING_FMT, |
| 557 | drv->format_name); |
| 558 | } |
| 559 | |
| 560 | ret = bdrv_create(bdrv_qcow2, tmp_filename, options); |
Jan Kiszka | d748768 | 2010-04-29 18:24:50 +0200 | [diff] [blame] | 561 | free_option_parameters(options); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 562 | if (ret < 0) { |
| 563 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 564 | } |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 565 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 566 | filename = tmp_filename; |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 567 | drv = bdrv_qcow2; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 568 | bs->is_temporary = 1; |
| 569 | } |
bellard | 712e787 | 2005-04-28 21:09:32 +0000 | [diff] [blame] | 570 | |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 571 | /* Find the right image format driver */ |
Christoph Hellwig | 6db9560 | 2010-04-05 16:53:57 +0200 | [diff] [blame] | 572 | if (!drv) { |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 573 | drv = find_image_format(filename); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 574 | } |
Christoph Hellwig | 6987307 | 2010-01-20 18:13:25 +0100 | [diff] [blame] | 575 | |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 576 | if (!drv) { |
| 577 | ret = -ENOENT; |
| 578 | goto unlink_and_fail; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 579 | } |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 580 | |
| 581 | /* Open the image */ |
| 582 | ret = bdrv_open_common(bs, filename, flags, drv); |
| 583 | if (ret < 0) { |
Christoph Hellwig | 6987307 | 2010-01-20 18:13:25 +0100 | [diff] [blame] | 584 | goto unlink_and_fail; |
| 585 | } |
| 586 | |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 587 | /* If there is a backing file, use it */ |
| 588 | if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') { |
| 589 | char backing_filename[PATH_MAX]; |
| 590 | int back_flags; |
| 591 | BlockDriver *back_drv = NULL; |
| 592 | |
| 593 | bs->backing_hd = bdrv_new(""); |
| 594 | path_combine(backing_filename, sizeof(backing_filename), |
| 595 | filename, bs->backing_file); |
| 596 | if (bs->backing_format[0] != '\0') |
| 597 | back_drv = bdrv_find_format(bs->backing_format); |
| 598 | |
| 599 | /* backing files always opened read-only */ |
| 600 | back_flags = |
| 601 | flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); |
| 602 | |
| 603 | ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv); |
| 604 | if (ret < 0) { |
| 605 | bdrv_close(bs); |
| 606 | return ret; |
| 607 | } |
| 608 | if (bs->is_temporary) { |
| 609 | bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR); |
| 610 | } else { |
| 611 | /* base image inherits from "parent" */ |
| 612 | bs->backing_hd->keep_read_only = bs->keep_read_only; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | if (!bdrv_key_required(bs)) { |
| 617 | /* call the change callback */ |
| 618 | bs->media_changed = 1; |
| 619 | if (bs->change_cb) |
| 620 | bs->change_cb(bs->change_opaque); |
| 621 | } |
| 622 | |
| 623 | return 0; |
| 624 | |
| 625 | unlink_and_fail: |
| 626 | if (bs->is_temporary) { |
| 627 | unlink(filename); |
| 628 | } |
| 629 | return ret; |
| 630 | } |
| 631 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 632 | void bdrv_close(BlockDriverState *bs) |
| 633 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 634 | if (bs->drv) { |
Markus Armbruster | f9092b1 | 2010-06-25 10:33:39 +0200 | [diff] [blame] | 635 | if (bs == bs_snapshots) { |
| 636 | bs_snapshots = NULL; |
| 637 | } |
Stefan Hajnoczi | 557df6a | 2010-04-17 10:49:06 +0100 | [diff] [blame] | 638 | if (bs->backing_hd) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 639 | bdrv_delete(bs->backing_hd); |
Stefan Hajnoczi | 557df6a | 2010-04-17 10:49:06 +0100 | [diff] [blame] | 640 | bs->backing_hd = NULL; |
| 641 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 642 | bs->drv->bdrv_close(bs); |
| 643 | qemu_free(bs->opaque); |
| 644 | #ifdef _WIN32 |
| 645 | if (bs->is_temporary) { |
| 646 | unlink(bs->filename); |
| 647 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 648 | #endif |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 649 | bs->opaque = NULL; |
| 650 | bs->drv = NULL; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 651 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 652 | if (bs->file != NULL) { |
| 653 | bdrv_close(bs->file); |
| 654 | } |
| 655 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 656 | /* call the change callback */ |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 657 | bs->media_changed = 1; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 658 | if (bs->change_cb) |
| 659 | bs->change_cb(bs->change_opaque); |
| 660 | } |
| 661 | } |
| 662 | |
MORITA Kazutaka | 2bc93fe | 2010-05-28 11:44:57 +0900 | [diff] [blame] | 663 | void bdrv_close_all(void) |
| 664 | { |
| 665 | BlockDriverState *bs; |
| 666 | |
| 667 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
| 668 | bdrv_close(bs); |
| 669 | } |
| 670 | } |
| 671 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 672 | void bdrv_delete(BlockDriverState *bs) |
| 673 | { |
Markus Armbruster | 18846de | 2010-06-29 16:58:30 +0200 | [diff] [blame] | 674 | assert(!bs->peer); |
| 675 | |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 676 | /* remove from list, if necessary */ |
| 677 | if (bs->device_name[0] != '\0') { |
| 678 | QTAILQ_REMOVE(&bdrv_states, bs, list); |
| 679 | } |
aurel32 | 34c6f05 | 2008-04-08 19:51:21 +0000 | [diff] [blame] | 680 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 681 | bdrv_close(bs); |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 682 | if (bs->file != NULL) { |
| 683 | bdrv_delete(bs->file); |
| 684 | } |
| 685 | |
Markus Armbruster | f9092b1 | 2010-06-25 10:33:39 +0200 | [diff] [blame] | 686 | assert(bs != bs_snapshots); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 687 | qemu_free(bs); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Markus Armbruster | 18846de | 2010-06-29 16:58:30 +0200 | [diff] [blame] | 690 | int bdrv_attach(BlockDriverState *bs, DeviceState *qdev) |
| 691 | { |
| 692 | if (bs->peer) { |
| 693 | return -EBUSY; |
| 694 | } |
| 695 | bs->peer = qdev; |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | void bdrv_detach(BlockDriverState *bs, DeviceState *qdev) |
| 700 | { |
| 701 | assert(bs->peer == qdev); |
| 702 | bs->peer = NULL; |
| 703 | } |
| 704 | |
| 705 | DeviceState *bdrv_get_attached(BlockDriverState *bs) |
| 706 | { |
| 707 | return bs->peer; |
| 708 | } |
| 709 | |
aliguori | e97fc19 | 2009-04-21 23:11:50 +0000 | [diff] [blame] | 710 | /* |
| 711 | * Run consistency checks on an image |
| 712 | * |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame^] | 713 | * Returns 0 if the check could be completed (it doesn't mean that the image is |
| 714 | * free of errors) or -errno when an internal error occured. The results of the |
| 715 | * check are stored in res. |
aliguori | e97fc19 | 2009-04-21 23:11:50 +0000 | [diff] [blame] | 716 | */ |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame^] | 717 | int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res) |
aliguori | e97fc19 | 2009-04-21 23:11:50 +0000 | [diff] [blame] | 718 | { |
| 719 | if (bs->drv->bdrv_check == NULL) { |
| 720 | return -ENOTSUP; |
| 721 | } |
| 722 | |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame^] | 723 | memset(res, 0, sizeof(*res)); |
| 724 | res->corruptions = bs->drv->bdrv_check(bs); |
| 725 | return res->corruptions < 0 ? res->corruptions : 0; |
aliguori | e97fc19 | 2009-04-21 23:11:50 +0000 | [diff] [blame] | 726 | } |
| 727 | |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 728 | /* commit COW file into the raw image */ |
| 729 | int bdrv_commit(BlockDriverState *bs) |
| 730 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 731 | BlockDriver *drv = bs->drv; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 732 | int64_t i, total_sectors; |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 733 | int n, j, ro, open_flags; |
| 734 | int ret = 0, rw_ret = 0; |
Jes Sorensen | eb5a316 | 2010-05-27 16:20:31 +0200 | [diff] [blame] | 735 | unsigned char sector[BDRV_SECTOR_SIZE]; |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 736 | char filename[1024]; |
| 737 | BlockDriverState *bs_rw, *bs_ro; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 738 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 739 | if (!drv) |
| 740 | return -ENOMEDIUM; |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 741 | |
| 742 | if (!bs->backing_hd) { |
| 743 | return -ENOTSUP; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 746 | if (bs->backing_hd->keep_read_only) { |
| 747 | return -EACCES; |
| 748 | } |
| 749 | |
| 750 | ro = bs->backing_hd->read_only; |
| 751 | strncpy(filename, bs->backing_hd->filename, sizeof(filename)); |
| 752 | open_flags = bs->backing_hd->open_flags; |
| 753 | |
| 754 | if (ro) { |
| 755 | /* re-open as RW */ |
| 756 | bdrv_delete(bs->backing_hd); |
| 757 | bs->backing_hd = NULL; |
| 758 | bs_rw = bdrv_new(""); |
Kevin Wolf | c334919 | 2010-05-06 16:34:56 +0200 | [diff] [blame] | 759 | rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, drv); |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 760 | if (rw_ret < 0) { |
| 761 | bdrv_delete(bs_rw); |
| 762 | /* try to re-open read-only */ |
| 763 | bs_ro = bdrv_new(""); |
Kevin Wolf | c334919 | 2010-05-06 16:34:56 +0200 | [diff] [blame] | 764 | ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv); |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 765 | if (ret < 0) { |
| 766 | bdrv_delete(bs_ro); |
| 767 | /* drive not functional anymore */ |
| 768 | bs->drv = NULL; |
| 769 | return ret; |
| 770 | } |
| 771 | bs->backing_hd = bs_ro; |
| 772 | return rw_ret; |
| 773 | } |
| 774 | bs->backing_hd = bs_rw; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 775 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 776 | |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 777 | total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 778 | for (i = 0; i < total_sectors;) { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 779 | if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 780 | for(j = 0; j < n; j++) { |
| 781 | if (bdrv_read(bs, i, sector, 1) != 0) { |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 782 | ret = -EIO; |
| 783 | goto ro_cleanup; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 787 | ret = -EIO; |
| 788 | goto ro_cleanup; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 789 | } |
| 790 | i++; |
| 791 | } |
| 792 | } else { |
| 793 | i += n; |
| 794 | } |
| 795 | } |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 796 | |
Christoph Hellwig | 1d44952 | 2010-01-17 12:32:30 +0100 | [diff] [blame] | 797 | if (drv->bdrv_make_empty) { |
| 798 | ret = drv->bdrv_make_empty(bs); |
| 799 | bdrv_flush(bs); |
| 800 | } |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 801 | |
Christoph Hellwig | 3f5075a | 2010-01-12 13:49:23 +0100 | [diff] [blame] | 802 | /* |
| 803 | * Make sure all data we wrote to the backing device is actually |
| 804 | * stable on disk. |
| 805 | */ |
| 806 | if (bs->backing_hd) |
| 807 | bdrv_flush(bs->backing_hd); |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 808 | |
| 809 | ro_cleanup: |
| 810 | |
| 811 | if (ro) { |
| 812 | /* re-open as RO */ |
| 813 | bdrv_delete(bs->backing_hd); |
| 814 | bs->backing_hd = NULL; |
| 815 | bs_ro = bdrv_new(""); |
Kevin Wolf | c334919 | 2010-05-06 16:34:56 +0200 | [diff] [blame] | 816 | ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv); |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 817 | if (ret < 0) { |
| 818 | bdrv_delete(bs_ro); |
| 819 | /* drive not functional anymore */ |
| 820 | bs->drv = NULL; |
| 821 | return ret; |
| 822 | } |
| 823 | bs->backing_hd = bs_ro; |
| 824 | bs->backing_hd->keep_read_only = 0; |
| 825 | } |
| 826 | |
Christoph Hellwig | 1d44952 | 2010-01-17 12:32:30 +0100 | [diff] [blame] | 827 | return ret; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Markus Armbruster | 6ab4b5a | 2010-06-02 18:55:18 +0200 | [diff] [blame] | 830 | void bdrv_commit_all(void) |
| 831 | { |
| 832 | BlockDriverState *bs; |
| 833 | |
| 834 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
| 835 | bdrv_commit(bs); |
| 836 | } |
| 837 | } |
| 838 | |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 839 | /* |
| 840 | * Return values: |
| 841 | * 0 - success |
| 842 | * -EINVAL - backing format specified, but no file |
| 843 | * -ENOSPC - can't update the backing file because no space is left in the |
| 844 | * image file header |
| 845 | * -ENOTSUP - format driver doesn't support changing the backing file |
| 846 | */ |
| 847 | int bdrv_change_backing_file(BlockDriverState *bs, |
| 848 | const char *backing_file, const char *backing_fmt) |
| 849 | { |
| 850 | BlockDriver *drv = bs->drv; |
| 851 | |
| 852 | if (drv->bdrv_change_backing_file != NULL) { |
| 853 | return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); |
| 854 | } else { |
| 855 | return -ENOTSUP; |
| 856 | } |
| 857 | } |
| 858 | |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 859 | static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, |
| 860 | size_t size) |
| 861 | { |
| 862 | int64_t len; |
| 863 | |
| 864 | if (!bdrv_is_inserted(bs)) |
| 865 | return -ENOMEDIUM; |
| 866 | |
| 867 | if (bs->growable) |
| 868 | return 0; |
| 869 | |
| 870 | len = bdrv_getlength(bs); |
| 871 | |
Kevin Wolf | fbb7b4e | 2009-05-08 14:47:24 +0200 | [diff] [blame] | 872 | if (offset < 0) |
| 873 | return -EIO; |
| 874 | |
| 875 | if ((offset > len) || (len - offset < size)) |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 876 | return -EIO; |
| 877 | |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, |
| 882 | int nb_sectors) |
| 883 | { |
Jes Sorensen | eb5a316 | 2010-05-27 16:20:31 +0200 | [diff] [blame] | 884 | return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, |
| 885 | nb_sectors * BDRV_SECTOR_SIZE); |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 886 | } |
| 887 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 888 | /* return < 0 if error. See bdrv_write() for the return codes */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 889 | int bdrv_read(BlockDriverState *bs, int64_t sector_num, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 890 | uint8_t *buf, int nb_sectors) |
| 891 | { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 892 | BlockDriver *drv = bs->drv; |
| 893 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 894 | if (!drv) |
| 895 | return -ENOMEDIUM; |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 896 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 897 | return -EIO; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 898 | |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 899 | return drv->bdrv_read(bs, sector_num, buf, nb_sectors); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 900 | } |
| 901 | |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 902 | static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 903 | int nb_sectors, int dirty) |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 904 | { |
| 905 | int64_t start, end; |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 906 | unsigned long val, idx, bit; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 907 | |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 908 | start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 909 | end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 910 | |
| 911 | for (; start <= end; start++) { |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 912 | idx = start / (sizeof(unsigned long) * 8); |
| 913 | bit = start % (sizeof(unsigned long) * 8); |
| 914 | val = bs->dirty_bitmap[idx]; |
| 915 | if (dirty) { |
Liran Schour | aaa0eb7 | 2010-01-26 10:31:48 +0200 | [diff] [blame] | 916 | if (!(val & (1 << bit))) { |
| 917 | bs->dirty_count++; |
| 918 | val |= 1 << bit; |
| 919 | } |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 920 | } else { |
Liran Schour | aaa0eb7 | 2010-01-26 10:31:48 +0200 | [diff] [blame] | 921 | if (val & (1 << bit)) { |
| 922 | bs->dirty_count--; |
| 923 | val &= ~(1 << bit); |
| 924 | } |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 925 | } |
| 926 | bs->dirty_bitmap[idx] = val; |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 930 | /* Return < 0 if error. Important errors are: |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 931 | -EIO generic I/O error (may happen for all errors) |
| 932 | -ENOMEDIUM No media inserted. |
| 933 | -EINVAL Invalid sector number or nb_sectors |
| 934 | -EACCES Trying to write a read-only device |
| 935 | */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 936 | int bdrv_write(BlockDriverState *bs, int64_t sector_num, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 937 | const uint8_t *buf, int nb_sectors) |
| 938 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 939 | BlockDriver *drv = bs->drv; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 940 | if (!bs->drv) |
| 941 | return -ENOMEDIUM; |
bellard | 0849bf0 | 2003-06-30 23:17:31 +0000 | [diff] [blame] | 942 | if (bs->read_only) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 943 | return -EACCES; |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 944 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 945 | return -EIO; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 946 | |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 947 | if (bs->dirty_bitmap) { |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 948 | set_dirty_bitmap(bs, sector_num, nb_sectors, 1); |
| 949 | } |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 950 | |
Kevin Wolf | 294cc35 | 2010-04-28 14:34:01 +0200 | [diff] [blame] | 951 | if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { |
| 952 | bs->wr_highest_sector = sector_num + nb_sectors - 1; |
| 953 | } |
| 954 | |
aliguori | 42fb280 | 2009-01-15 20:43:39 +0000 | [diff] [blame] | 955 | return drv->bdrv_write(bs, sector_num, buf, nb_sectors); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 956 | } |
| 957 | |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 958 | int bdrv_pread(BlockDriverState *bs, int64_t offset, |
| 959 | void *buf, int count1) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 960 | { |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 961 | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 962 | int len, nb_sectors, count; |
| 963 | int64_t sector_num; |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 964 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 965 | |
| 966 | count = count1; |
| 967 | /* first read to align to sector start */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 968 | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 969 | if (len > count) |
| 970 | len = count; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 971 | sector_num = offset >> BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 972 | if (len > 0) { |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 973 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
| 974 | return ret; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 975 | memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 976 | count -= len; |
| 977 | if (count == 0) |
| 978 | return count1; |
| 979 | sector_num++; |
| 980 | buf += len; |
| 981 | } |
| 982 | |
| 983 | /* read the sectors "in place" */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 984 | nb_sectors = count >> BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 985 | if (nb_sectors > 0) { |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 986 | if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) |
| 987 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 988 | sector_num += nb_sectors; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 989 | len = nb_sectors << BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 990 | buf += len; |
| 991 | count -= len; |
| 992 | } |
| 993 | |
| 994 | /* add data from the last sector */ |
| 995 | if (count > 0) { |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 996 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
| 997 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 998 | memcpy(buf, tmp_buf, count); |
| 999 | } |
| 1000 | return count1; |
| 1001 | } |
| 1002 | |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 1003 | int bdrv_pwrite(BlockDriverState *bs, int64_t offset, |
| 1004 | const void *buf, int count1) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1005 | { |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1006 | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1007 | int len, nb_sectors, count; |
| 1008 | int64_t sector_num; |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 1009 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1010 | |
| 1011 | count = count1; |
| 1012 | /* first write to align to sector start */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1013 | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1014 | if (len > count) |
| 1015 | len = count; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1016 | sector_num = offset >> BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1017 | if (len > 0) { |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 1018 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
| 1019 | return ret; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1020 | memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len); |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 1021 | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
| 1022 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1023 | count -= len; |
| 1024 | if (count == 0) |
| 1025 | return count1; |
| 1026 | sector_num++; |
| 1027 | buf += len; |
| 1028 | } |
| 1029 | |
| 1030 | /* write the sectors "in place" */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1031 | nb_sectors = count >> BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1032 | if (nb_sectors > 0) { |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 1033 | if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) |
| 1034 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1035 | sector_num += nb_sectors; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1036 | len = nb_sectors << BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1037 | buf += len; |
| 1038 | count -= len; |
| 1039 | } |
| 1040 | |
| 1041 | /* add data from the last sector */ |
| 1042 | if (count > 0) { |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 1043 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
| 1044 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1045 | memcpy(tmp_buf, buf, count); |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 1046 | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
| 1047 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1048 | } |
| 1049 | return count1; |
| 1050 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1051 | |
Kevin Wolf | f08145f | 2010-06-16 16:38:15 +0200 | [diff] [blame] | 1052 | /* |
| 1053 | * Writes to the file and ensures that no writes are reordered across this |
| 1054 | * request (acts as a barrier) |
| 1055 | * |
| 1056 | * Returns 0 on success, -errno in error cases. |
| 1057 | */ |
| 1058 | int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, |
| 1059 | const void *buf, int count) |
| 1060 | { |
| 1061 | int ret; |
| 1062 | |
| 1063 | ret = bdrv_pwrite(bs, offset, buf, count); |
| 1064 | if (ret < 0) { |
| 1065 | return ret; |
| 1066 | } |
| 1067 | |
| 1068 | /* No flush needed for cache=writethrough, it uses O_DSYNC */ |
| 1069 | if ((bs->open_flags & BDRV_O_CACHE_MASK) != 0) { |
| 1070 | bdrv_flush(bs); |
| 1071 | } |
| 1072 | |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
| 1076 | /* |
| 1077 | * Writes to the file and ensures that no writes are reordered across this |
| 1078 | * request (acts as a barrier) |
| 1079 | * |
| 1080 | * Returns 0 on success, -errno in error cases. |
| 1081 | */ |
| 1082 | int bdrv_write_sync(BlockDriverState *bs, int64_t sector_num, |
| 1083 | const uint8_t *buf, int nb_sectors) |
| 1084 | { |
| 1085 | return bdrv_pwrite_sync(bs, BDRV_SECTOR_SIZE * sector_num, |
| 1086 | buf, BDRV_SECTOR_SIZE * nb_sectors); |
| 1087 | } |
| 1088 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1089 | /** |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1090 | * Truncate file to 'offset' bytes (needed only for file protocols) |
| 1091 | */ |
| 1092 | int bdrv_truncate(BlockDriverState *bs, int64_t offset) |
| 1093 | { |
| 1094 | BlockDriver *drv = bs->drv; |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 1095 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1096 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1097 | return -ENOMEDIUM; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1098 | if (!drv->bdrv_truncate) |
| 1099 | return -ENOTSUP; |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 1100 | if (bs->read_only) |
| 1101 | return -EACCES; |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 1102 | ret = drv->bdrv_truncate(bs, offset); |
| 1103 | if (ret == 0) { |
| 1104 | ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); |
| 1105 | } |
| 1106 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | /** |
| 1110 | * Length of a file in bytes. Return < 0 if error or unknown. |
| 1111 | */ |
| 1112 | int64_t bdrv_getlength(BlockDriverState *bs) |
| 1113 | { |
| 1114 | BlockDriver *drv = bs->drv; |
| 1115 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1116 | return -ENOMEDIUM; |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 1117 | |
| 1118 | /* Fixed size devices use the total_sectors value for speed instead of |
| 1119 | issuing a length query (like lseek) on each call. Also, legacy block |
| 1120 | drivers don't provide a bdrv_getlength function and must use |
| 1121 | total_sectors. */ |
| 1122 | if (!bs->growable || !drv->bdrv_getlength) { |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1123 | return bs->total_sectors * BDRV_SECTOR_SIZE; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1124 | } |
| 1125 | return drv->bdrv_getlength(bs); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1128 | /* return 0 as number of sectors if no device present or error */ |
ths | 96b8f13 | 2007-12-17 01:35:20 +0000 | [diff] [blame] | 1129 | void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 1130 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1131 | int64_t length; |
| 1132 | length = bdrv_getlength(bs); |
| 1133 | if (length < 0) |
| 1134 | length = 0; |
| 1135 | else |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1136 | length = length >> BDRV_SECTOR_BITS; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1137 | *nb_sectors_ptr = length; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 1138 | } |
bellard | cf98951 | 2004-02-16 21:56:36 +0000 | [diff] [blame] | 1139 | |
aliguori | f3d54fc | 2008-11-25 21:50:24 +0000 | [diff] [blame] | 1140 | struct partition { |
| 1141 | uint8_t boot_ind; /* 0x80 - active */ |
| 1142 | uint8_t head; /* starting head */ |
| 1143 | uint8_t sector; /* starting sector */ |
| 1144 | uint8_t cyl; /* starting cylinder */ |
| 1145 | uint8_t sys_ind; /* What partition type */ |
| 1146 | uint8_t end_head; /* end head */ |
| 1147 | uint8_t end_sector; /* end sector */ |
| 1148 | uint8_t end_cyl; /* end cylinder */ |
| 1149 | uint32_t start_sect; /* starting sector counting from 0 */ |
| 1150 | uint32_t nr_sects; /* nr of sectors in partition */ |
| 1151 | } __attribute__((packed)); |
| 1152 | |
| 1153 | /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ |
| 1154 | static int guess_disk_lchs(BlockDriverState *bs, |
| 1155 | int *pcylinders, int *pheads, int *psectors) |
| 1156 | { |
Jes Sorensen | eb5a316 | 2010-05-27 16:20:31 +0200 | [diff] [blame] | 1157 | uint8_t buf[BDRV_SECTOR_SIZE]; |
aliguori | f3d54fc | 2008-11-25 21:50:24 +0000 | [diff] [blame] | 1158 | int ret, i, heads, sectors, cylinders; |
| 1159 | struct partition *p; |
| 1160 | uint32_t nr_sects; |
blueswir1 | a38131b | 2008-12-05 17:56:40 +0000 | [diff] [blame] | 1161 | uint64_t nb_sectors; |
aliguori | f3d54fc | 2008-11-25 21:50:24 +0000 | [diff] [blame] | 1162 | |
| 1163 | bdrv_get_geometry(bs, &nb_sectors); |
| 1164 | |
| 1165 | ret = bdrv_read(bs, 0, buf, 1); |
| 1166 | if (ret < 0) |
| 1167 | return -1; |
| 1168 | /* test msdos magic */ |
| 1169 | if (buf[510] != 0x55 || buf[511] != 0xaa) |
| 1170 | return -1; |
| 1171 | for(i = 0; i < 4; i++) { |
| 1172 | p = ((struct partition *)(buf + 0x1be)) + i; |
| 1173 | nr_sects = le32_to_cpu(p->nr_sects); |
| 1174 | if (nr_sects && p->end_head) { |
| 1175 | /* We make the assumption that the partition terminates on |
| 1176 | a cylinder boundary */ |
| 1177 | heads = p->end_head + 1; |
| 1178 | sectors = p->end_sector & 63; |
| 1179 | if (sectors == 0) |
| 1180 | continue; |
| 1181 | cylinders = nb_sectors / (heads * sectors); |
| 1182 | if (cylinders < 1 || cylinders > 16383) |
| 1183 | continue; |
| 1184 | *pheads = heads; |
| 1185 | *psectors = sectors; |
| 1186 | *pcylinders = cylinders; |
| 1187 | #if 0 |
| 1188 | printf("guessed geometry: LCHS=%d %d %d\n", |
| 1189 | cylinders, heads, sectors); |
| 1190 | #endif |
| 1191 | return 0; |
| 1192 | } |
| 1193 | } |
| 1194 | return -1; |
| 1195 | } |
| 1196 | |
| 1197 | void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) |
| 1198 | { |
| 1199 | int translation, lba_detected = 0; |
| 1200 | int cylinders, heads, secs; |
blueswir1 | a38131b | 2008-12-05 17:56:40 +0000 | [diff] [blame] | 1201 | uint64_t nb_sectors; |
aliguori | f3d54fc | 2008-11-25 21:50:24 +0000 | [diff] [blame] | 1202 | |
| 1203 | /* if a geometry hint is available, use it */ |
| 1204 | bdrv_get_geometry(bs, &nb_sectors); |
| 1205 | bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); |
| 1206 | translation = bdrv_get_translation_hint(bs); |
| 1207 | if (cylinders != 0) { |
| 1208 | *pcyls = cylinders; |
| 1209 | *pheads = heads; |
| 1210 | *psecs = secs; |
| 1211 | } else { |
| 1212 | if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { |
| 1213 | if (heads > 16) { |
| 1214 | /* if heads > 16, it means that a BIOS LBA |
| 1215 | translation was active, so the default |
| 1216 | hardware geometry is OK */ |
| 1217 | lba_detected = 1; |
| 1218 | goto default_geometry; |
| 1219 | } else { |
| 1220 | *pcyls = cylinders; |
| 1221 | *pheads = heads; |
| 1222 | *psecs = secs; |
| 1223 | /* disable any translation to be in sync with |
| 1224 | the logical geometry */ |
| 1225 | if (translation == BIOS_ATA_TRANSLATION_AUTO) { |
| 1226 | bdrv_set_translation_hint(bs, |
| 1227 | BIOS_ATA_TRANSLATION_NONE); |
| 1228 | } |
| 1229 | } |
| 1230 | } else { |
| 1231 | default_geometry: |
| 1232 | /* if no geometry, use a standard physical disk geometry */ |
| 1233 | cylinders = nb_sectors / (16 * 63); |
| 1234 | |
| 1235 | if (cylinders > 16383) |
| 1236 | cylinders = 16383; |
| 1237 | else if (cylinders < 2) |
| 1238 | cylinders = 2; |
| 1239 | *pcyls = cylinders; |
| 1240 | *pheads = 16; |
| 1241 | *psecs = 63; |
| 1242 | if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { |
| 1243 | if ((*pcyls * *pheads) <= 131072) { |
| 1244 | bdrv_set_translation_hint(bs, |
| 1245 | BIOS_ATA_TRANSLATION_LARGE); |
| 1246 | } else { |
| 1247 | bdrv_set_translation_hint(bs, |
| 1248 | BIOS_ATA_TRANSLATION_LBA); |
| 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); |
| 1253 | } |
| 1254 | } |
| 1255 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1256 | void bdrv_set_geometry_hint(BlockDriverState *bs, |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1257 | int cyls, int heads, int secs) |
| 1258 | { |
| 1259 | bs->cyls = cyls; |
| 1260 | bs->heads = heads; |
| 1261 | bs->secs = secs; |
| 1262 | } |
| 1263 | |
| 1264 | void bdrv_set_type_hint(BlockDriverState *bs, int type) |
| 1265 | { |
| 1266 | bs->type = type; |
| 1267 | bs->removable = ((type == BDRV_TYPE_CDROM || |
| 1268 | type == BDRV_TYPE_FLOPPY)); |
| 1269 | } |
| 1270 | |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 1271 | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
| 1272 | { |
| 1273 | bs->translation = translation; |
| 1274 | } |
| 1275 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1276 | void bdrv_get_geometry_hint(BlockDriverState *bs, |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1277 | int *pcyls, int *pheads, int *psecs) |
| 1278 | { |
| 1279 | *pcyls = bs->cyls; |
| 1280 | *pheads = bs->heads; |
| 1281 | *psecs = bs->secs; |
| 1282 | } |
| 1283 | |
| 1284 | int bdrv_get_type_hint(BlockDriverState *bs) |
| 1285 | { |
| 1286 | return bs->type; |
| 1287 | } |
| 1288 | |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 1289 | int bdrv_get_translation_hint(BlockDriverState *bs) |
| 1290 | { |
| 1291 | return bs->translation; |
| 1292 | } |
| 1293 | |
Markus Armbruster | abd7f68 | 2010-06-02 18:55:17 +0200 | [diff] [blame] | 1294 | void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error, |
| 1295 | BlockErrorAction on_write_error) |
| 1296 | { |
| 1297 | bs->on_read_error = on_read_error; |
| 1298 | bs->on_write_error = on_write_error; |
| 1299 | } |
| 1300 | |
| 1301 | BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read) |
| 1302 | { |
| 1303 | return is_read ? bs->on_read_error : bs->on_write_error; |
| 1304 | } |
| 1305 | |
Markus Armbruster | 7d0d695 | 2010-06-25 13:42:14 +0200 | [diff] [blame] | 1306 | void bdrv_set_removable(BlockDriverState *bs, int removable) |
| 1307 | { |
| 1308 | bs->removable = removable; |
| 1309 | if (removable && bs == bs_snapshots) { |
| 1310 | bs_snapshots = NULL; |
| 1311 | } |
| 1312 | } |
| 1313 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1314 | int bdrv_is_removable(BlockDriverState *bs) |
| 1315 | { |
| 1316 | return bs->removable; |
| 1317 | } |
| 1318 | |
| 1319 | int bdrv_is_read_only(BlockDriverState *bs) |
| 1320 | { |
| 1321 | return bs->read_only; |
| 1322 | } |
| 1323 | |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 1324 | int bdrv_is_sg(BlockDriverState *bs) |
| 1325 | { |
| 1326 | return bs->sg; |
| 1327 | } |
| 1328 | |
Christoph Hellwig | e900a7b | 2009-09-04 19:01:15 +0200 | [diff] [blame] | 1329 | int bdrv_enable_write_cache(BlockDriverState *bs) |
| 1330 | { |
| 1331 | return bs->enable_write_cache; |
| 1332 | } |
| 1333 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1334 | /* XXX: no longer used */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1335 | void bdrv_set_change_cb(BlockDriverState *bs, |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1336 | void (*change_cb)(void *opaque), void *opaque) |
| 1337 | { |
| 1338 | bs->change_cb = change_cb; |
| 1339 | bs->change_opaque = opaque; |
| 1340 | } |
| 1341 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1342 | int bdrv_is_encrypted(BlockDriverState *bs) |
| 1343 | { |
| 1344 | if (bs->backing_hd && bs->backing_hd->encrypted) |
| 1345 | return 1; |
| 1346 | return bs->encrypted; |
| 1347 | } |
| 1348 | |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 1349 | int bdrv_key_required(BlockDriverState *bs) |
| 1350 | { |
| 1351 | BlockDriverState *backing_hd = bs->backing_hd; |
| 1352 | |
| 1353 | if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) |
| 1354 | return 1; |
| 1355 | return (bs->encrypted && !bs->valid_key); |
| 1356 | } |
| 1357 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1358 | int bdrv_set_key(BlockDriverState *bs, const char *key) |
| 1359 | { |
| 1360 | int ret; |
| 1361 | if (bs->backing_hd && bs->backing_hd->encrypted) { |
| 1362 | ret = bdrv_set_key(bs->backing_hd, key); |
| 1363 | if (ret < 0) |
| 1364 | return ret; |
| 1365 | if (!bs->encrypted) |
| 1366 | return 0; |
| 1367 | } |
Shahar Havivi | fd04a2a | 2010-03-06 00:26:13 +0200 | [diff] [blame] | 1368 | if (!bs->encrypted) { |
| 1369 | return -EINVAL; |
| 1370 | } else if (!bs->drv || !bs->drv->bdrv_set_key) { |
| 1371 | return -ENOMEDIUM; |
| 1372 | } |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 1373 | ret = bs->drv->bdrv_set_key(bs, key); |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 1374 | if (ret < 0) { |
| 1375 | bs->valid_key = 0; |
| 1376 | } else if (!bs->valid_key) { |
| 1377 | bs->valid_key = 1; |
| 1378 | /* call the change callback now, we skipped it on open */ |
| 1379 | bs->media_changed = 1; |
| 1380 | if (bs->change_cb) |
| 1381 | bs->change_cb(bs->change_opaque); |
| 1382 | } |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 1383 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) |
| 1387 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1388 | if (!bs->drv) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1389 | buf[0] = '\0'; |
| 1390 | } else { |
| 1391 | pstrcpy(buf, buf_size, bs->drv->format_name); |
| 1392 | } |
| 1393 | } |
| 1394 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1395 | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1396 | void *opaque) |
| 1397 | { |
| 1398 | BlockDriver *drv; |
| 1399 | |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 1400 | QLIST_FOREACH(drv, &bdrv_drivers, list) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1401 | it(opaque, drv->format_name); |
| 1402 | } |
| 1403 | } |
| 1404 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1405 | BlockDriverState *bdrv_find(const char *name) |
| 1406 | { |
| 1407 | BlockDriverState *bs; |
| 1408 | |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 1409 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
| 1410 | if (!strcmp(name, bs->device_name)) { |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1411 | return bs; |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 1412 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1413 | } |
| 1414 | return NULL; |
| 1415 | } |
| 1416 | |
Markus Armbruster | 2f399b0 | 2010-06-02 18:55:20 +0200 | [diff] [blame] | 1417 | BlockDriverState *bdrv_next(BlockDriverState *bs) |
| 1418 | { |
| 1419 | if (!bs) { |
| 1420 | return QTAILQ_FIRST(&bdrv_states); |
| 1421 | } |
| 1422 | return QTAILQ_NEXT(bs, list); |
| 1423 | } |
| 1424 | |
aliguori | 51de976 | 2009-03-05 23:00:43 +0000 | [diff] [blame] | 1425 | void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 1426 | { |
| 1427 | BlockDriverState *bs; |
| 1428 | |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 1429 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
aliguori | 51de976 | 2009-03-05 23:00:43 +0000 | [diff] [blame] | 1430 | it(opaque, bs); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 1431 | } |
| 1432 | } |
| 1433 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1434 | const char *bdrv_get_device_name(BlockDriverState *bs) |
| 1435 | { |
| 1436 | return bs->device_name; |
| 1437 | } |
| 1438 | |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 1439 | void bdrv_flush(BlockDriverState *bs) |
| 1440 | { |
Alexander Graf | 016f5cf | 2010-05-26 17:51:49 +0200 | [diff] [blame] | 1441 | if (bs->open_flags & BDRV_O_NO_FLUSH) { |
| 1442 | return; |
| 1443 | } |
| 1444 | |
Christoph Hellwig | 3f5075a | 2010-01-12 13:49:23 +0100 | [diff] [blame] | 1445 | if (bs->drv && bs->drv->bdrv_flush) |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 1446 | bs->drv->bdrv_flush(bs); |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
aliguori | c6ca28d | 2008-10-06 13:55:43 +0000 | [diff] [blame] | 1449 | void bdrv_flush_all(void) |
| 1450 | { |
| 1451 | BlockDriverState *bs; |
| 1452 | |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 1453 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
| 1454 | if (bs->drv && !bdrv_is_read_only(bs) && |
| 1455 | (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) { |
aliguori | c6ca28d | 2008-10-06 13:55:43 +0000 | [diff] [blame] | 1456 | bdrv_flush(bs); |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 1457 | } |
| 1458 | } |
aliguori | c6ca28d | 2008-10-06 13:55:43 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Kevin Wolf | f2feebb | 2010-04-14 17:30:35 +0200 | [diff] [blame] | 1461 | int bdrv_has_zero_init(BlockDriverState *bs) |
| 1462 | { |
| 1463 | assert(bs->drv); |
| 1464 | |
| 1465 | if (bs->drv->no_zero_init) { |
| 1466 | return 0; |
| 1467 | } else if (bs->file) { |
| 1468 | return bdrv_has_zero_init(bs->file); |
| 1469 | } |
| 1470 | |
| 1471 | return 1; |
| 1472 | } |
| 1473 | |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1474 | /* |
| 1475 | * Returns true iff the specified sector is present in the disk image. Drivers |
| 1476 | * not implementing the functionality are assumed to not support backing files, |
| 1477 | * hence all their sectors are reported as allocated. |
| 1478 | * |
| 1479 | * 'pnum' is set to the number of sectors (including and immediately following |
| 1480 | * the specified sector) that are known to be in the same |
| 1481 | * allocated/unallocated state. |
| 1482 | * |
| 1483 | * 'nb_sectors' is the max value 'pnum' should be set to. |
| 1484 | */ |
| 1485 | int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, |
| 1486 | int *pnum) |
| 1487 | { |
| 1488 | int64_t n; |
| 1489 | if (!bs->drv->bdrv_is_allocated) { |
| 1490 | if (sector_num >= bs->total_sectors) { |
| 1491 | *pnum = 0; |
| 1492 | return 0; |
| 1493 | } |
| 1494 | n = bs->total_sectors - sector_num; |
| 1495 | *pnum = (n < nb_sectors) ? (n) : (nb_sectors); |
| 1496 | return 1; |
| 1497 | } |
| 1498 | return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); |
| 1499 | } |
| 1500 | |
Luiz Capitulino | 2582bfe | 2010-02-03 12:41:01 -0200 | [diff] [blame] | 1501 | void bdrv_mon_event(const BlockDriverState *bdrv, |
| 1502 | BlockMonEventAction action, int is_read) |
| 1503 | { |
| 1504 | QObject *data; |
| 1505 | const char *action_str; |
| 1506 | |
| 1507 | switch (action) { |
| 1508 | case BDRV_ACTION_REPORT: |
| 1509 | action_str = "report"; |
| 1510 | break; |
| 1511 | case BDRV_ACTION_IGNORE: |
| 1512 | action_str = "ignore"; |
| 1513 | break; |
| 1514 | case BDRV_ACTION_STOP: |
| 1515 | action_str = "stop"; |
| 1516 | break; |
| 1517 | default: |
| 1518 | abort(); |
| 1519 | } |
| 1520 | |
| 1521 | data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", |
| 1522 | bdrv->device_name, |
| 1523 | action_str, |
| 1524 | is_read ? "read" : "write"); |
| 1525 | monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data); |
| 1526 | |
| 1527 | qobject_decref(data); |
| 1528 | } |
| 1529 | |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1530 | static void bdrv_print_dict(QObject *obj, void *opaque) |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1531 | { |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1532 | QDict *bs_dict; |
| 1533 | Monitor *mon = opaque; |
| 1534 | |
| 1535 | bs_dict = qobject_to_qdict(obj); |
| 1536 | |
| 1537 | monitor_printf(mon, "%s: type=%s removable=%d", |
| 1538 | qdict_get_str(bs_dict, "device"), |
| 1539 | qdict_get_str(bs_dict, "type"), |
| 1540 | qdict_get_bool(bs_dict, "removable")); |
| 1541 | |
| 1542 | if (qdict_get_bool(bs_dict, "removable")) { |
| 1543 | monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked")); |
| 1544 | } |
| 1545 | |
| 1546 | if (qdict_haskey(bs_dict, "inserted")) { |
| 1547 | QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted")); |
| 1548 | |
| 1549 | monitor_printf(mon, " file="); |
| 1550 | monitor_print_filename(mon, qdict_get_str(qdict, "file")); |
| 1551 | if (qdict_haskey(qdict, "backing_file")) { |
| 1552 | monitor_printf(mon, " backing_file="); |
| 1553 | monitor_print_filename(mon, qdict_get_str(qdict, "backing_file")); |
| 1554 | } |
| 1555 | monitor_printf(mon, " ro=%d drv=%s encrypted=%d", |
| 1556 | qdict_get_bool(qdict, "ro"), |
| 1557 | qdict_get_str(qdict, "drv"), |
| 1558 | qdict_get_bool(qdict, "encrypted")); |
| 1559 | } else { |
| 1560 | monitor_printf(mon, " [not inserted]"); |
| 1561 | } |
| 1562 | |
| 1563 | monitor_printf(mon, "\n"); |
| 1564 | } |
| 1565 | |
| 1566 | void bdrv_info_print(Monitor *mon, const QObject *data) |
| 1567 | { |
| 1568 | qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon); |
| 1569 | } |
| 1570 | |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1571 | void bdrv_info(Monitor *mon, QObject **ret_data) |
| 1572 | { |
| 1573 | QList *bs_list; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1574 | BlockDriverState *bs; |
| 1575 | |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1576 | bs_list = qlist_new(); |
| 1577 | |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 1578 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1579 | QObject *bs_obj; |
| 1580 | const char *type = "unknown"; |
| 1581 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1582 | switch(bs->type) { |
| 1583 | case BDRV_TYPE_HD: |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1584 | type = "hd"; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1585 | break; |
| 1586 | case BDRV_TYPE_CDROM: |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1587 | type = "cdrom"; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1588 | break; |
| 1589 | case BDRV_TYPE_FLOPPY: |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1590 | type = "floppy"; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1591 | break; |
| 1592 | } |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1593 | |
| 1594 | bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, " |
| 1595 | "'removable': %i, 'locked': %i }", |
| 1596 | bs->device_name, type, bs->removable, |
| 1597 | bs->locked); |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1598 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1599 | if (bs->drv) { |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1600 | QObject *obj; |
| 1601 | QDict *bs_dict = qobject_to_qdict(bs_obj); |
| 1602 | |
| 1603 | obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, " |
| 1604 | "'encrypted': %i }", |
| 1605 | bs->filename, bs->read_only, |
| 1606 | bs->drv->format_name, |
| 1607 | bdrv_is_encrypted(bs)); |
ths | fef3074 | 2006-12-22 14:11:32 +0000 | [diff] [blame] | 1608 | if (bs->backing_file[0] != '\0') { |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1609 | QDict *qdict = qobject_to_qdict(obj); |
| 1610 | qdict_put(qdict, "backing_file", |
| 1611 | qstring_from_str(bs->backing_file)); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1612 | } |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1613 | |
| 1614 | qdict_put_obj(bs_dict, "inserted", obj); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1615 | } |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1616 | qlist_append_obj(bs_list, bs_obj); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1617 | } |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1618 | |
| 1619 | *ret_data = QOBJECT(bs_list); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1620 | } |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1621 | |
Luiz Capitulino | 218a536 | 2009-12-10 17:16:07 -0200 | [diff] [blame] | 1622 | static void bdrv_stats_iter(QObject *data, void *opaque) |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1623 | { |
Luiz Capitulino | 218a536 | 2009-12-10 17:16:07 -0200 | [diff] [blame] | 1624 | QDict *qdict; |
| 1625 | Monitor *mon = opaque; |
| 1626 | |
| 1627 | qdict = qobject_to_qdict(data); |
| 1628 | monitor_printf(mon, "%s:", qdict_get_str(qdict, "device")); |
| 1629 | |
| 1630 | qdict = qobject_to_qdict(qdict_get(qdict, "stats")); |
| 1631 | monitor_printf(mon, " rd_bytes=%" PRId64 |
| 1632 | " wr_bytes=%" PRId64 |
| 1633 | " rd_operations=%" PRId64 |
| 1634 | " wr_operations=%" PRId64 |
| 1635 | "\n", |
| 1636 | qdict_get_int(qdict, "rd_bytes"), |
| 1637 | qdict_get_int(qdict, "wr_bytes"), |
| 1638 | qdict_get_int(qdict, "rd_operations"), |
| 1639 | qdict_get_int(qdict, "wr_operations")); |
| 1640 | } |
| 1641 | |
| 1642 | void bdrv_stats_print(Monitor *mon, const QObject *data) |
| 1643 | { |
| 1644 | qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon); |
| 1645 | } |
| 1646 | |
Kevin Wolf | 294cc35 | 2010-04-28 14:34:01 +0200 | [diff] [blame] | 1647 | static QObject* bdrv_info_stats_bs(BlockDriverState *bs) |
| 1648 | { |
| 1649 | QObject *res; |
| 1650 | QDict *dict; |
| 1651 | |
| 1652 | res = qobject_from_jsonf("{ 'stats': {" |
| 1653 | "'rd_bytes': %" PRId64 "," |
| 1654 | "'wr_bytes': %" PRId64 "," |
| 1655 | "'rd_operations': %" PRId64 "," |
| 1656 | "'wr_operations': %" PRId64 "," |
| 1657 | "'wr_highest_offset': %" PRId64 |
| 1658 | "} }", |
| 1659 | bs->rd_bytes, bs->wr_bytes, |
| 1660 | bs->rd_ops, bs->wr_ops, |
Blue Swirl | 5ffbbc6 | 2010-06-14 18:55:33 +0000 | [diff] [blame] | 1661 | bs->wr_highest_sector * |
| 1662 | (uint64_t)BDRV_SECTOR_SIZE); |
Kevin Wolf | 294cc35 | 2010-04-28 14:34:01 +0200 | [diff] [blame] | 1663 | dict = qobject_to_qdict(res); |
| 1664 | |
| 1665 | if (*bs->device_name) { |
| 1666 | qdict_put(dict, "device", qstring_from_str(bs->device_name)); |
| 1667 | } |
| 1668 | |
| 1669 | if (bs->file) { |
| 1670 | QObject *parent = bdrv_info_stats_bs(bs->file); |
| 1671 | qdict_put_obj(dict, "parent", parent); |
| 1672 | } |
| 1673 | |
| 1674 | return res; |
| 1675 | } |
| 1676 | |
Luiz Capitulino | 218a536 | 2009-12-10 17:16:07 -0200 | [diff] [blame] | 1677 | void bdrv_info_stats(Monitor *mon, QObject **ret_data) |
| 1678 | { |
| 1679 | QObject *obj; |
| 1680 | QList *devices; |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1681 | BlockDriverState *bs; |
| 1682 | |
Luiz Capitulino | 218a536 | 2009-12-10 17:16:07 -0200 | [diff] [blame] | 1683 | devices = qlist_new(); |
| 1684 | |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 1685 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
Kevin Wolf | 294cc35 | 2010-04-28 14:34:01 +0200 | [diff] [blame] | 1686 | obj = bdrv_info_stats_bs(bs); |
Luiz Capitulino | 218a536 | 2009-12-10 17:16:07 -0200 | [diff] [blame] | 1687 | qlist_append_obj(devices, obj); |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1688 | } |
Luiz Capitulino | 218a536 | 2009-12-10 17:16:07 -0200 | [diff] [blame] | 1689 | |
| 1690 | *ret_data = QOBJECT(devices); |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1691 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1692 | |
aliguori | 045df33 | 2009-03-05 23:00:48 +0000 | [diff] [blame] | 1693 | const char *bdrv_get_encrypted_filename(BlockDriverState *bs) |
| 1694 | { |
| 1695 | if (bs->backing_hd && bs->backing_hd->encrypted) |
| 1696 | return bs->backing_file; |
| 1697 | else if (bs->encrypted) |
| 1698 | return bs->filename; |
| 1699 | else |
| 1700 | return NULL; |
| 1701 | } |
| 1702 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1703 | void bdrv_get_backing_filename(BlockDriverState *bs, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1704 | char *filename, int filename_size) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1705 | { |
Kevin Wolf | b783e40 | 2010-01-12 12:55:16 +0100 | [diff] [blame] | 1706 | if (!bs->backing_file) { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1707 | pstrcpy(filename, filename_size, ""); |
| 1708 | } else { |
| 1709 | pstrcpy(filename, filename_size, bs->backing_file); |
| 1710 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1713 | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1714 | const uint8_t *buf, int nb_sectors) |
| 1715 | { |
| 1716 | BlockDriver *drv = bs->drv; |
| 1717 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1718 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1719 | if (!drv->bdrv_write_compressed) |
| 1720 | return -ENOTSUP; |
Kevin Wolf | fbb7b4e | 2009-05-08 14:47:24 +0200 | [diff] [blame] | 1721 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 1722 | return -EIO; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1723 | |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 1724 | if (bs->dirty_bitmap) { |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 1725 | set_dirty_bitmap(bs, sector_num, nb_sectors, 1); |
| 1726 | } |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1727 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1728 | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); |
| 1729 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1730 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1731 | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
| 1732 | { |
| 1733 | BlockDriver *drv = bs->drv; |
| 1734 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1735 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1736 | if (!drv->bdrv_get_info) |
| 1737 | return -ENOTSUP; |
| 1738 | memset(bdi, 0, sizeof(*bdi)); |
| 1739 | return drv->bdrv_get_info(bs, bdi); |
| 1740 | } |
| 1741 | |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1742 | int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, |
| 1743 | int64_t pos, int size) |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1744 | { |
| 1745 | BlockDriver *drv = bs->drv; |
| 1746 | if (!drv) |
| 1747 | return -ENOMEDIUM; |
MORITA Kazutaka | 7cdb1f6 | 2010-05-28 11:44:58 +0900 | [diff] [blame] | 1748 | if (drv->bdrv_save_vmstate) |
| 1749 | return drv->bdrv_save_vmstate(bs, buf, pos, size); |
| 1750 | if (bs->file) |
| 1751 | return bdrv_save_vmstate(bs->file, buf, pos, size); |
| 1752 | return -ENOTSUP; |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1755 | int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, |
| 1756 | int64_t pos, int size) |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1757 | { |
| 1758 | BlockDriver *drv = bs->drv; |
| 1759 | if (!drv) |
| 1760 | return -ENOMEDIUM; |
MORITA Kazutaka | 7cdb1f6 | 2010-05-28 11:44:58 +0900 | [diff] [blame] | 1761 | if (drv->bdrv_load_vmstate) |
| 1762 | return drv->bdrv_load_vmstate(bs, buf, pos, size); |
| 1763 | if (bs->file) |
| 1764 | return bdrv_load_vmstate(bs->file, buf, pos, size); |
| 1765 | return -ENOTSUP; |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 1768 | void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) |
| 1769 | { |
| 1770 | BlockDriver *drv = bs->drv; |
| 1771 | |
| 1772 | if (!drv || !drv->bdrv_debug_event) { |
| 1773 | return; |
| 1774 | } |
| 1775 | |
| 1776 | return drv->bdrv_debug_event(bs, event); |
| 1777 | |
| 1778 | } |
| 1779 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1780 | /**************************************************************/ |
| 1781 | /* handling of snapshots */ |
| 1782 | |
Miguel Di Ciurcio Filho | feeee5a | 2010-06-08 10:40:55 -0300 | [diff] [blame] | 1783 | int bdrv_can_snapshot(BlockDriverState *bs) |
| 1784 | { |
| 1785 | BlockDriver *drv = bs->drv; |
| 1786 | if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) { |
| 1787 | return 0; |
| 1788 | } |
| 1789 | |
| 1790 | if (!drv->bdrv_snapshot_create) { |
| 1791 | if (bs->file != NULL) { |
| 1792 | return bdrv_can_snapshot(bs->file); |
| 1793 | } |
| 1794 | return 0; |
| 1795 | } |
| 1796 | |
| 1797 | return 1; |
| 1798 | } |
| 1799 | |
Markus Armbruster | f9092b1 | 2010-06-25 10:33:39 +0200 | [diff] [blame] | 1800 | BlockDriverState *bdrv_snapshots(void) |
| 1801 | { |
| 1802 | BlockDriverState *bs; |
| 1803 | |
Markus Armbruster | 3ac906f | 2010-07-01 09:30:38 +0200 | [diff] [blame] | 1804 | if (bs_snapshots) { |
Markus Armbruster | f9092b1 | 2010-06-25 10:33:39 +0200 | [diff] [blame] | 1805 | return bs_snapshots; |
Markus Armbruster | 3ac906f | 2010-07-01 09:30:38 +0200 | [diff] [blame] | 1806 | } |
Markus Armbruster | f9092b1 | 2010-06-25 10:33:39 +0200 | [diff] [blame] | 1807 | |
| 1808 | bs = NULL; |
| 1809 | while ((bs = bdrv_next(bs))) { |
| 1810 | if (bdrv_can_snapshot(bs)) { |
Markus Armbruster | 3ac906f | 2010-07-01 09:30:38 +0200 | [diff] [blame] | 1811 | bs_snapshots = bs; |
| 1812 | return bs; |
Markus Armbruster | f9092b1 | 2010-06-25 10:33:39 +0200 | [diff] [blame] | 1813 | } |
| 1814 | } |
| 1815 | return NULL; |
Markus Armbruster | f9092b1 | 2010-06-25 10:33:39 +0200 | [diff] [blame] | 1816 | } |
| 1817 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1818 | int bdrv_snapshot_create(BlockDriverState *bs, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1819 | QEMUSnapshotInfo *sn_info) |
| 1820 | { |
| 1821 | BlockDriver *drv = bs->drv; |
| 1822 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1823 | return -ENOMEDIUM; |
MORITA Kazutaka | 7cdb1f6 | 2010-05-28 11:44:58 +0900 | [diff] [blame] | 1824 | if (drv->bdrv_snapshot_create) |
| 1825 | return drv->bdrv_snapshot_create(bs, sn_info); |
| 1826 | if (bs->file) |
| 1827 | return bdrv_snapshot_create(bs->file, sn_info); |
| 1828 | return -ENOTSUP; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1831 | int bdrv_snapshot_goto(BlockDriverState *bs, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1832 | const char *snapshot_id) |
| 1833 | { |
| 1834 | BlockDriver *drv = bs->drv; |
MORITA Kazutaka | 7cdb1f6 | 2010-05-28 11:44:58 +0900 | [diff] [blame] | 1835 | int ret, open_ret; |
| 1836 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1837 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1838 | return -ENOMEDIUM; |
MORITA Kazutaka | 7cdb1f6 | 2010-05-28 11:44:58 +0900 | [diff] [blame] | 1839 | if (drv->bdrv_snapshot_goto) |
| 1840 | return drv->bdrv_snapshot_goto(bs, snapshot_id); |
| 1841 | |
| 1842 | if (bs->file) { |
| 1843 | drv->bdrv_close(bs); |
| 1844 | ret = bdrv_snapshot_goto(bs->file, snapshot_id); |
| 1845 | open_ret = drv->bdrv_open(bs, bs->open_flags); |
| 1846 | if (open_ret < 0) { |
| 1847 | bdrv_delete(bs->file); |
| 1848 | bs->drv = NULL; |
| 1849 | return open_ret; |
| 1850 | } |
| 1851 | return ret; |
| 1852 | } |
| 1853 | |
| 1854 | return -ENOTSUP; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) |
| 1858 | { |
| 1859 | BlockDriver *drv = bs->drv; |
| 1860 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1861 | return -ENOMEDIUM; |
MORITA Kazutaka | 7cdb1f6 | 2010-05-28 11:44:58 +0900 | [diff] [blame] | 1862 | if (drv->bdrv_snapshot_delete) |
| 1863 | return drv->bdrv_snapshot_delete(bs, snapshot_id); |
| 1864 | if (bs->file) |
| 1865 | return bdrv_snapshot_delete(bs->file, snapshot_id); |
| 1866 | return -ENOTSUP; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1869 | int bdrv_snapshot_list(BlockDriverState *bs, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1870 | QEMUSnapshotInfo **psn_info) |
| 1871 | { |
| 1872 | BlockDriver *drv = bs->drv; |
| 1873 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1874 | return -ENOMEDIUM; |
MORITA Kazutaka | 7cdb1f6 | 2010-05-28 11:44:58 +0900 | [diff] [blame] | 1875 | if (drv->bdrv_snapshot_list) |
| 1876 | return drv->bdrv_snapshot_list(bs, psn_info); |
| 1877 | if (bs->file) |
| 1878 | return bdrv_snapshot_list(bs->file, psn_info); |
| 1879 | return -ENOTSUP; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | #define NB_SUFFIXES 4 |
| 1883 | |
| 1884 | char *get_human_readable_size(char *buf, int buf_size, int64_t size) |
| 1885 | { |
| 1886 | static const char suffixes[NB_SUFFIXES] = "KMGT"; |
| 1887 | int64_t base; |
| 1888 | int i; |
| 1889 | |
| 1890 | if (size <= 999) { |
| 1891 | snprintf(buf, buf_size, "%" PRId64, size); |
| 1892 | } else { |
| 1893 | base = 1024; |
| 1894 | for(i = 0; i < NB_SUFFIXES; i++) { |
| 1895 | if (size < (10 * base)) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1896 | snprintf(buf, buf_size, "%0.1f%c", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1897 | (double)size / base, |
| 1898 | suffixes[i]); |
| 1899 | break; |
| 1900 | } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1901 | snprintf(buf, buf_size, "%" PRId64 "%c", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1902 | ((size + (base >> 1)) / base), |
| 1903 | suffixes[i]); |
| 1904 | break; |
| 1905 | } |
| 1906 | base = base * 1024; |
| 1907 | } |
| 1908 | } |
| 1909 | return buf; |
| 1910 | } |
| 1911 | |
| 1912 | char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) |
| 1913 | { |
| 1914 | char buf1[128], date_buf[128], clock_buf[128]; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 1915 | #ifdef _WIN32 |
| 1916 | struct tm *ptm; |
| 1917 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1918 | struct tm tm; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 1919 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1920 | time_t ti; |
| 1921 | int64_t secs; |
| 1922 | |
| 1923 | if (!sn) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1924 | snprintf(buf, buf_size, |
| 1925 | "%-10s%-20s%7s%20s%15s", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1926 | "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); |
| 1927 | } else { |
| 1928 | ti = sn->date_sec; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 1929 | #ifdef _WIN32 |
| 1930 | ptm = localtime(&ti); |
| 1931 | strftime(date_buf, sizeof(date_buf), |
| 1932 | "%Y-%m-%d %H:%M:%S", ptm); |
| 1933 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1934 | localtime_r(&ti, &tm); |
| 1935 | strftime(date_buf, sizeof(date_buf), |
| 1936 | "%Y-%m-%d %H:%M:%S", &tm); |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 1937 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1938 | secs = sn->vm_clock_nsec / 1000000000; |
| 1939 | snprintf(clock_buf, sizeof(clock_buf), |
| 1940 | "%02d:%02d:%02d.%03d", |
| 1941 | (int)(secs / 3600), |
| 1942 | (int)((secs / 60) % 60), |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1943 | (int)(secs % 60), |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1944 | (int)((sn->vm_clock_nsec / 1000000) % 1000)); |
| 1945 | snprintf(buf, buf_size, |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1946 | "%-10s%-20s%7s%20s%15s", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1947 | sn->id_str, sn->name, |
| 1948 | get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), |
| 1949 | date_buf, |
| 1950 | clock_buf); |
| 1951 | } |
| 1952 | return buf; |
| 1953 | } |
| 1954 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1955 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1956 | /**************************************************************/ |
| 1957 | /* async I/Os */ |
| 1958 | |
aliguori | 3b69e4b | 2009-01-22 16:59:24 +0000 | [diff] [blame] | 1959 | BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1960 | QEMUIOVector *qiov, int nb_sectors, |
aliguori | 3b69e4b | 2009-01-22 16:59:24 +0000 | [diff] [blame] | 1961 | BlockDriverCompletionFunc *cb, void *opaque) |
| 1962 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1963 | BlockDriver *drv = bs->drv; |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1964 | BlockDriverAIOCB *ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1965 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1966 | if (!drv) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1967 | return NULL; |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 1968 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 1969 | return NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1970 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1971 | ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors, |
| 1972 | cb, opaque); |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1973 | |
| 1974 | if (ret) { |
| 1975 | /* Update stats even though technically transfer has not happened. */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1976 | bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1977 | bs->rd_ops ++; |
| 1978 | } |
| 1979 | |
| 1980 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1983 | BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, |
| 1984 | QEMUIOVector *qiov, int nb_sectors, |
| 1985 | BlockDriverCompletionFunc *cb, void *opaque) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1986 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1987 | BlockDriver *drv = bs->drv; |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1988 | BlockDriverAIOCB *ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1989 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1990 | if (!drv) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1991 | return NULL; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1992 | if (bs->read_only) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 1993 | return NULL; |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 1994 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 1995 | return NULL; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1996 | |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 1997 | if (bs->dirty_bitmap) { |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 1998 | set_dirty_bitmap(bs, sector_num, nb_sectors, 1); |
| 1999 | } |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2000 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2001 | ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors, |
| 2002 | cb, opaque); |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 2003 | |
| 2004 | if (ret) { |
Kevin Wolf | 294cc35 | 2010-04-28 14:34:01 +0200 | [diff] [blame] | 2005 | /* Update stats even though technically transfer has not happened. */ |
| 2006 | bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; |
| 2007 | bs->wr_ops ++; |
| 2008 | if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { |
| 2009 | bs->wr_highest_sector = sector_num + nb_sectors - 1; |
| 2010 | } |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2016 | |
| 2017 | typedef struct MultiwriteCB { |
| 2018 | int error; |
| 2019 | int num_requests; |
| 2020 | int num_callbacks; |
| 2021 | struct { |
| 2022 | BlockDriverCompletionFunc *cb; |
| 2023 | void *opaque; |
| 2024 | QEMUIOVector *free_qiov; |
| 2025 | void *free_buf; |
| 2026 | } callbacks[]; |
| 2027 | } MultiwriteCB; |
| 2028 | |
| 2029 | static void multiwrite_user_cb(MultiwriteCB *mcb) |
| 2030 | { |
| 2031 | int i; |
| 2032 | |
| 2033 | for (i = 0; i < mcb->num_callbacks; i++) { |
| 2034 | mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); |
Stefan Hajnoczi | 1e1ea48 | 2010-04-21 20:35:45 +0100 | [diff] [blame] | 2035 | if (mcb->callbacks[i].free_qiov) { |
| 2036 | qemu_iovec_destroy(mcb->callbacks[i].free_qiov); |
| 2037 | } |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2038 | qemu_free(mcb->callbacks[i].free_qiov); |
Herve Poussineau | f8a8324 | 2010-01-24 21:23:56 +0000 | [diff] [blame] | 2039 | qemu_vfree(mcb->callbacks[i].free_buf); |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2040 | } |
| 2041 | } |
| 2042 | |
| 2043 | static void multiwrite_cb(void *opaque, int ret) |
| 2044 | { |
| 2045 | MultiwriteCB *mcb = opaque; |
| 2046 | |
Kevin Wolf | cb6d3ca | 2010-04-01 22:48:44 +0200 | [diff] [blame] | 2047 | if (ret < 0 && !mcb->error) { |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2048 | mcb->error = ret; |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | mcb->num_requests--; |
| 2052 | if (mcb->num_requests == 0) { |
Kevin Wolf | de189a1 | 2010-07-01 16:08:51 +0200 | [diff] [blame] | 2053 | multiwrite_user_cb(mcb); |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2054 | qemu_free(mcb); |
| 2055 | } |
| 2056 | } |
| 2057 | |
| 2058 | static int multiwrite_req_compare(const void *a, const void *b) |
| 2059 | { |
Christoph Hellwig | 77be436 | 2010-05-19 20:53:10 +0200 | [diff] [blame] | 2060 | const BlockRequest *req1 = a, *req2 = b; |
| 2061 | |
| 2062 | /* |
| 2063 | * Note that we can't simply subtract req2->sector from req1->sector |
| 2064 | * here as that could overflow the return value. |
| 2065 | */ |
| 2066 | if (req1->sector > req2->sector) { |
| 2067 | return 1; |
| 2068 | } else if (req1->sector < req2->sector) { |
| 2069 | return -1; |
| 2070 | } else { |
| 2071 | return 0; |
| 2072 | } |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | /* |
| 2076 | * Takes a bunch of requests and tries to merge them. Returns the number of |
| 2077 | * requests that remain after merging. |
| 2078 | */ |
| 2079 | static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, |
| 2080 | int num_reqs, MultiwriteCB *mcb) |
| 2081 | { |
| 2082 | int i, outidx; |
| 2083 | |
| 2084 | // Sort requests by start sector |
| 2085 | qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); |
| 2086 | |
| 2087 | // Check if adjacent requests touch the same clusters. If so, combine them, |
| 2088 | // filling up gaps with zero sectors. |
| 2089 | outidx = 0; |
| 2090 | for (i = 1; i < num_reqs; i++) { |
| 2091 | int merge = 0; |
| 2092 | int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; |
| 2093 | |
| 2094 | // This handles the cases that are valid for all block drivers, namely |
| 2095 | // exactly sequential writes and overlapping writes. |
| 2096 | if (reqs[i].sector <= oldreq_last) { |
| 2097 | merge = 1; |
| 2098 | } |
| 2099 | |
| 2100 | // The block driver may decide that it makes sense to combine requests |
| 2101 | // even if there is a gap of some sectors between them. In this case, |
| 2102 | // the gap is filled with zeros (therefore only applicable for yet |
| 2103 | // unused space in format like qcow2). |
| 2104 | if (!merge && bs->drv->bdrv_merge_requests) { |
| 2105 | merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]); |
| 2106 | } |
| 2107 | |
Christoph Hellwig | e2a305f | 2010-01-26 14:49:08 +0100 | [diff] [blame] | 2108 | if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { |
| 2109 | merge = 0; |
| 2110 | } |
| 2111 | |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2112 | if (merge) { |
| 2113 | size_t size; |
| 2114 | QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov)); |
| 2115 | qemu_iovec_init(qiov, |
| 2116 | reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); |
| 2117 | |
| 2118 | // Add the first request to the merged one. If the requests are |
| 2119 | // overlapping, drop the last sectors of the first request. |
| 2120 | size = (reqs[i].sector - reqs[outidx].sector) << 9; |
| 2121 | qemu_iovec_concat(qiov, reqs[outidx].qiov, size); |
| 2122 | |
| 2123 | // We might need to add some zeros between the two requests |
| 2124 | if (reqs[i].sector > oldreq_last) { |
| 2125 | size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9; |
| 2126 | uint8_t *buf = qemu_blockalign(bs, zero_bytes); |
| 2127 | memset(buf, 0, zero_bytes); |
| 2128 | qemu_iovec_add(qiov, buf, zero_bytes); |
| 2129 | mcb->callbacks[i].free_buf = buf; |
| 2130 | } |
| 2131 | |
| 2132 | // Add the second request |
| 2133 | qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size); |
| 2134 | |
Kevin Wolf | cbf1dff | 2010-05-21 11:09:42 +0200 | [diff] [blame] | 2135 | reqs[outidx].nb_sectors = qiov->size >> 9; |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2136 | reqs[outidx].qiov = qiov; |
| 2137 | |
| 2138 | mcb->callbacks[i].free_qiov = reqs[outidx].qiov; |
| 2139 | } else { |
| 2140 | outidx++; |
| 2141 | reqs[outidx].sector = reqs[i].sector; |
| 2142 | reqs[outidx].nb_sectors = reqs[i].nb_sectors; |
| 2143 | reqs[outidx].qiov = reqs[i].qiov; |
| 2144 | } |
| 2145 | } |
| 2146 | |
| 2147 | return outidx + 1; |
| 2148 | } |
| 2149 | |
| 2150 | /* |
| 2151 | * Submit multiple AIO write requests at once. |
| 2152 | * |
| 2153 | * On success, the function returns 0 and all requests in the reqs array have |
| 2154 | * been submitted. In error case this function returns -1, and any of the |
| 2155 | * requests may or may not be submitted yet. In particular, this means that the |
| 2156 | * callback will be called for some of the requests, for others it won't. The |
| 2157 | * caller must check the error field of the BlockRequest to wait for the right |
| 2158 | * callbacks (if error != 0, no callback will be called). |
| 2159 | * |
| 2160 | * The implementation may modify the contents of the reqs array, e.g. to merge |
| 2161 | * requests. However, the fields opaque and error are left unmodified as they |
| 2162 | * are used to signal failure for a single request to the caller. |
| 2163 | */ |
| 2164 | int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) |
| 2165 | { |
| 2166 | BlockDriverAIOCB *acb; |
| 2167 | MultiwriteCB *mcb; |
| 2168 | int i; |
| 2169 | |
| 2170 | if (num_reqs == 0) { |
| 2171 | return 0; |
| 2172 | } |
| 2173 | |
| 2174 | // Create MultiwriteCB structure |
| 2175 | mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); |
| 2176 | mcb->num_requests = 0; |
| 2177 | mcb->num_callbacks = num_reqs; |
| 2178 | |
| 2179 | for (i = 0; i < num_reqs; i++) { |
| 2180 | mcb->callbacks[i].cb = reqs[i].cb; |
| 2181 | mcb->callbacks[i].opaque = reqs[i].opaque; |
| 2182 | } |
| 2183 | |
| 2184 | // Check for mergable requests |
| 2185 | num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); |
| 2186 | |
Kevin Wolf | 453f9a1 | 2010-07-02 14:01:21 +0200 | [diff] [blame] | 2187 | /* |
| 2188 | * Run the aio requests. As soon as one request can't be submitted |
| 2189 | * successfully, fail all requests that are not yet submitted (we must |
| 2190 | * return failure for all requests anyway) |
| 2191 | * |
| 2192 | * num_requests cannot be set to the right value immediately: If |
| 2193 | * bdrv_aio_writev fails for some request, num_requests would be too high |
| 2194 | * and therefore multiwrite_cb() would never recognize the multiwrite |
| 2195 | * request as completed. We also cannot use the loop variable i to set it |
| 2196 | * when the first request fails because the callback may already have been |
| 2197 | * called for previously submitted requests. Thus, num_requests must be |
| 2198 | * incremented for each request that is submitted. |
| 2199 | * |
| 2200 | * The problem that callbacks may be called early also means that we need |
| 2201 | * to take care that num_requests doesn't become 0 before all requests are |
| 2202 | * submitted - multiwrite_cb() would consider the multiwrite request |
| 2203 | * completed. A dummy request that is "completed" by a manual call to |
| 2204 | * multiwrite_cb() takes care of this. |
| 2205 | */ |
| 2206 | mcb->num_requests = 1; |
| 2207 | |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2208 | for (i = 0; i < num_reqs; i++) { |
Kevin Wolf | 453f9a1 | 2010-07-02 14:01:21 +0200 | [diff] [blame] | 2209 | mcb->num_requests++; |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2210 | acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, |
| 2211 | reqs[i].nb_sectors, multiwrite_cb, mcb); |
| 2212 | |
| 2213 | if (acb == NULL) { |
| 2214 | // We can only fail the whole thing if no request has been |
| 2215 | // submitted yet. Otherwise we'll wait for the submitted AIOs to |
| 2216 | // complete and report the error in the callback. |
Kevin Wolf | 453f9a1 | 2010-07-02 14:01:21 +0200 | [diff] [blame] | 2217 | if (i == 0) { |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2218 | goto fail; |
| 2219 | } else { |
Kevin Wolf | 7eb58a6 | 2010-04-06 18:24:07 +0200 | [diff] [blame] | 2220 | multiwrite_cb(mcb, -EIO); |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2221 | break; |
| 2222 | } |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2223 | } |
| 2224 | } |
| 2225 | |
Kevin Wolf | 453f9a1 | 2010-07-02 14:01:21 +0200 | [diff] [blame] | 2226 | /* Complete the dummy request */ |
| 2227 | multiwrite_cb(mcb, 0); |
| 2228 | |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2229 | return 0; |
| 2230 | |
| 2231 | fail: |
Kevin Wolf | 453f9a1 | 2010-07-02 14:01:21 +0200 | [diff] [blame] | 2232 | for (i = 0; i < mcb->num_callbacks; i++) { |
| 2233 | reqs[i].error = -EIO; |
| 2234 | } |
Bruce Rogers | af47459 | 2010-05-13 15:14:33 -0600 | [diff] [blame] | 2235 | qemu_free(mcb); |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2236 | return -1; |
| 2237 | } |
| 2238 | |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 2239 | BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, |
| 2240 | BlockDriverCompletionFunc *cb, void *opaque) |
| 2241 | { |
| 2242 | BlockDriver *drv = bs->drv; |
| 2243 | |
Alexander Graf | 016f5cf | 2010-05-26 17:51:49 +0200 | [diff] [blame] | 2244 | if (bs->open_flags & BDRV_O_NO_FLUSH) { |
| 2245 | return bdrv_aio_noop_em(bs, cb, opaque); |
| 2246 | } |
| 2247 | |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 2248 | if (!drv) |
| 2249 | return NULL; |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 2250 | return drv->bdrv_aio_flush(bs, cb, opaque); |
| 2251 | } |
| 2252 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2253 | void bdrv_aio_cancel(BlockDriverAIOCB *acb) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2254 | { |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 2255 | acb->pool->cancel(acb); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2258 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2259 | /**************************************************************/ |
| 2260 | /* async block device emulation */ |
| 2261 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 2262 | typedef struct BlockDriverAIOCBSync { |
| 2263 | BlockDriverAIOCB common; |
| 2264 | QEMUBH *bh; |
| 2265 | int ret; |
| 2266 | /* vector translation state */ |
| 2267 | QEMUIOVector *qiov; |
| 2268 | uint8_t *bounce; |
| 2269 | int is_write; |
| 2270 | } BlockDriverAIOCBSync; |
| 2271 | |
| 2272 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) |
| 2273 | { |
Kevin Wolf | b666d23 | 2010-05-05 11:44:39 +0200 | [diff] [blame] | 2274 | BlockDriverAIOCBSync *acb = |
| 2275 | container_of(blockacb, BlockDriverAIOCBSync, common); |
Dor Laor | 6a7ad29 | 2009-06-01 12:07:23 +0300 | [diff] [blame] | 2276 | qemu_bh_delete(acb->bh); |
Avi Kivity | 36afc45 | 2009-06-23 16:20:36 +0300 | [diff] [blame] | 2277 | acb->bh = NULL; |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 2278 | qemu_aio_release(acb); |
| 2279 | } |
| 2280 | |
| 2281 | static AIOPool bdrv_em_aio_pool = { |
| 2282 | .aiocb_size = sizeof(BlockDriverAIOCBSync), |
| 2283 | .cancel = bdrv_aio_cancel_em, |
| 2284 | }; |
| 2285 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2286 | static void bdrv_aio_bh_cb(void *opaque) |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 2287 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2288 | BlockDriverAIOCBSync *acb = opaque; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2289 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2290 | if (!acb->is_write) |
| 2291 | qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); |
aliguori | ceb42de | 2009-04-07 18:43:28 +0000 | [diff] [blame] | 2292 | qemu_vfree(acb->bounce); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2293 | acb->common.cb(acb->common.opaque, acb->ret); |
Dor Laor | 6a7ad29 | 2009-06-01 12:07:23 +0300 | [diff] [blame] | 2294 | qemu_bh_delete(acb->bh); |
Avi Kivity | 36afc45 | 2009-06-23 16:20:36 +0300 | [diff] [blame] | 2295 | acb->bh = NULL; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2296 | qemu_aio_release(acb); |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 2297 | } |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 2298 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2299 | static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, |
| 2300 | int64_t sector_num, |
| 2301 | QEMUIOVector *qiov, |
| 2302 | int nb_sectors, |
| 2303 | BlockDriverCompletionFunc *cb, |
| 2304 | void *opaque, |
| 2305 | int is_write) |
| 2306 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2307 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2308 | BlockDriverAIOCBSync *acb; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2309 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 2310 | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2311 | acb->is_write = is_write; |
| 2312 | acb->qiov = qiov; |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 2313 | acb->bounce = qemu_blockalign(bs, qiov->size); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2314 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2315 | if (!acb->bh) |
| 2316 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2317 | |
| 2318 | if (is_write) { |
| 2319 | qemu_iovec_to_buffer(acb->qiov, acb->bounce); |
| 2320 | acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors); |
| 2321 | } else { |
| 2322 | acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors); |
| 2323 | } |
| 2324 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2325 | qemu_bh_schedule(acb->bh); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2326 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2327 | return &acb->common; |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2330 | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, |
| 2331 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2332 | BlockDriverCompletionFunc *cb, void *opaque) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2333 | { |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2334 | 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] | 2335 | } |
| 2336 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2337 | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, |
| 2338 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
| 2339 | BlockDriverCompletionFunc *cb, void *opaque) |
| 2340 | { |
| 2341 | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); |
| 2342 | } |
| 2343 | |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 2344 | static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, |
| 2345 | BlockDriverCompletionFunc *cb, void *opaque) |
| 2346 | { |
| 2347 | BlockDriverAIOCBSync *acb; |
| 2348 | |
| 2349 | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); |
| 2350 | acb->is_write = 1; /* don't bounce in the completion hadler */ |
| 2351 | acb->qiov = NULL; |
| 2352 | acb->bounce = NULL; |
| 2353 | acb->ret = 0; |
| 2354 | |
| 2355 | if (!acb->bh) |
| 2356 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
| 2357 | |
| 2358 | bdrv_flush(bs); |
| 2359 | qemu_bh_schedule(acb->bh); |
| 2360 | return &acb->common; |
| 2361 | } |
| 2362 | |
Alexander Graf | 016f5cf | 2010-05-26 17:51:49 +0200 | [diff] [blame] | 2363 | static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs, |
| 2364 | BlockDriverCompletionFunc *cb, void *opaque) |
| 2365 | { |
| 2366 | BlockDriverAIOCBSync *acb; |
| 2367 | |
| 2368 | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); |
| 2369 | acb->is_write = 1; /* don't bounce in the completion handler */ |
| 2370 | acb->qiov = NULL; |
| 2371 | acb->bounce = NULL; |
| 2372 | acb->ret = 0; |
| 2373 | |
| 2374 | if (!acb->bh) { |
| 2375 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
| 2376 | } |
| 2377 | |
| 2378 | qemu_bh_schedule(acb->bh); |
| 2379 | return &acb->common; |
| 2380 | } |
| 2381 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2382 | /**************************************************************/ |
| 2383 | /* sync block device emulation */ |
| 2384 | |
| 2385 | static void bdrv_rw_em_cb(void *opaque, int ret) |
| 2386 | { |
| 2387 | *(int *)opaque = ret; |
| 2388 | } |
| 2389 | |
| 2390 | #define NOT_DONE 0x7fffffff |
| 2391 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2392 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2393 | uint8_t *buf, int nb_sectors) |
| 2394 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2395 | int async_ret; |
| 2396 | BlockDriverAIOCB *acb; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2397 | struct iovec iov; |
| 2398 | QEMUIOVector qiov; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2399 | |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 2400 | async_context_push(); |
| 2401 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2402 | async_ret = NOT_DONE; |
blueswir1 | 3f4cb3d | 2009-04-13 16:31:01 +0000 | [diff] [blame] | 2403 | iov.iov_base = (void *)buf; |
Jes Sorensen | eb5a316 | 2010-05-27 16:20:31 +0200 | [diff] [blame] | 2404 | iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2405 | qemu_iovec_init_external(&qiov, &iov, 1); |
| 2406 | acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors, |
| 2407 | bdrv_rw_em_cb, &async_ret); |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 2408 | if (acb == NULL) { |
| 2409 | async_ret = -1; |
| 2410 | goto fail; |
| 2411 | } |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 2412 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2413 | while (async_ret == NOT_DONE) { |
| 2414 | qemu_aio_wait(); |
| 2415 | } |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 2416 | |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 2417 | |
| 2418 | fail: |
| 2419 | async_context_pop(); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2420 | return async_ret; |
| 2421 | } |
| 2422 | |
| 2423 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
| 2424 | const uint8_t *buf, int nb_sectors) |
| 2425 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2426 | int async_ret; |
| 2427 | BlockDriverAIOCB *acb; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2428 | struct iovec iov; |
| 2429 | QEMUIOVector qiov; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2430 | |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 2431 | async_context_push(); |
| 2432 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2433 | async_ret = NOT_DONE; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2434 | iov.iov_base = (void *)buf; |
Jes Sorensen | eb5a316 | 2010-05-27 16:20:31 +0200 | [diff] [blame] | 2435 | iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2436 | qemu_iovec_init_external(&qiov, &iov, 1); |
| 2437 | acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors, |
| 2438 | bdrv_rw_em_cb, &async_ret); |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 2439 | if (acb == NULL) { |
| 2440 | async_ret = -1; |
| 2441 | goto fail; |
| 2442 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2443 | while (async_ret == NOT_DONE) { |
| 2444 | qemu_aio_wait(); |
| 2445 | } |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 2446 | |
| 2447 | fail: |
| 2448 | async_context_pop(); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2449 | return async_ret; |
| 2450 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2451 | |
| 2452 | void bdrv_init(void) |
| 2453 | { |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 2454 | module_call_init(MODULE_INIT_BLOCK); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2455 | } |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2456 | |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 2457 | void bdrv_init_with_whitelist(void) |
| 2458 | { |
| 2459 | use_bdrv_whitelist = 1; |
| 2460 | bdrv_init(); |
| 2461 | } |
| 2462 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 2463 | void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, |
| 2464 | BlockDriverCompletionFunc *cb, void *opaque) |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 2465 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2466 | BlockDriverAIOCB *acb; |
| 2467 | |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 2468 | if (pool->free_aiocb) { |
| 2469 | acb = pool->free_aiocb; |
| 2470 | pool->free_aiocb = acb->next; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2471 | } else { |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 2472 | acb = qemu_mallocz(pool->aiocb_size); |
| 2473 | acb->pool = pool; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2474 | } |
| 2475 | acb->bs = bs; |
| 2476 | acb->cb = cb; |
| 2477 | acb->opaque = opaque; |
| 2478 | return acb; |
| 2479 | } |
| 2480 | |
| 2481 | void qemu_aio_release(void *p) |
| 2482 | { |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 2483 | BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; |
| 2484 | AIOPool *pool = acb->pool; |
| 2485 | acb->next = pool->free_aiocb; |
| 2486 | pool->free_aiocb = acb; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2487 | } |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 2488 | |
| 2489 | /**************************************************************/ |
| 2490 | /* removable device support */ |
| 2491 | |
| 2492 | /** |
| 2493 | * Return TRUE if the media is present |
| 2494 | */ |
| 2495 | int bdrv_is_inserted(BlockDriverState *bs) |
| 2496 | { |
| 2497 | BlockDriver *drv = bs->drv; |
| 2498 | int ret; |
| 2499 | if (!drv) |
| 2500 | return 0; |
| 2501 | if (!drv->bdrv_is_inserted) |
| 2502 | return 1; |
| 2503 | ret = drv->bdrv_is_inserted(bs); |
| 2504 | return ret; |
| 2505 | } |
| 2506 | |
| 2507 | /** |
| 2508 | * Return TRUE if the media changed since the last call to this |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2509 | * function. It is currently only used for floppy disks |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 2510 | */ |
| 2511 | int bdrv_media_changed(BlockDriverState *bs) |
| 2512 | { |
| 2513 | BlockDriver *drv = bs->drv; |
| 2514 | int ret; |
| 2515 | |
| 2516 | if (!drv || !drv->bdrv_media_changed) |
| 2517 | ret = -ENOTSUP; |
| 2518 | else |
| 2519 | ret = drv->bdrv_media_changed(bs); |
| 2520 | if (ret == -ENOTSUP) |
| 2521 | ret = bs->media_changed; |
| 2522 | bs->media_changed = 0; |
| 2523 | return ret; |
| 2524 | } |
| 2525 | |
| 2526 | /** |
| 2527 | * If eject_flag is TRUE, eject the media. Otherwise, close the tray |
| 2528 | */ |
Mark McLoughlin | aea2a33 | 2009-05-27 10:06:11 +0100 | [diff] [blame] | 2529 | int bdrv_eject(BlockDriverState *bs, int eject_flag) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 2530 | { |
| 2531 | BlockDriver *drv = bs->drv; |
| 2532 | int ret; |
| 2533 | |
Mark McLoughlin | aea2a33 | 2009-05-27 10:06:11 +0100 | [diff] [blame] | 2534 | if (bs->locked) { |
| 2535 | return -EBUSY; |
| 2536 | } |
| 2537 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 2538 | if (!drv || !drv->bdrv_eject) { |
| 2539 | ret = -ENOTSUP; |
| 2540 | } else { |
| 2541 | ret = drv->bdrv_eject(bs, eject_flag); |
| 2542 | } |
| 2543 | if (ret == -ENOTSUP) { |
| 2544 | if (eject_flag) |
| 2545 | bdrv_close(bs); |
Mark McLoughlin | aea2a33 | 2009-05-27 10:06:11 +0100 | [diff] [blame] | 2546 | ret = 0; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 2547 | } |
Mark McLoughlin | aea2a33 | 2009-05-27 10:06:11 +0100 | [diff] [blame] | 2548 | |
| 2549 | return ret; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
| 2552 | int bdrv_is_locked(BlockDriverState *bs) |
| 2553 | { |
| 2554 | return bs->locked; |
| 2555 | } |
| 2556 | |
| 2557 | /** |
| 2558 | * Lock or unlock the media (if it is locked, the user won't be able |
| 2559 | * to eject it manually). |
| 2560 | */ |
| 2561 | void bdrv_set_locked(BlockDriverState *bs, int locked) |
| 2562 | { |
| 2563 | BlockDriver *drv = bs->drv; |
| 2564 | |
| 2565 | bs->locked = locked; |
| 2566 | if (drv && drv->bdrv_set_locked) { |
| 2567 | drv->bdrv_set_locked(bs, locked); |
| 2568 | } |
| 2569 | } |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 2570 | |
| 2571 | /* needed for generic scsi interface */ |
| 2572 | |
| 2573 | int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
| 2574 | { |
| 2575 | BlockDriver *drv = bs->drv; |
| 2576 | |
| 2577 | if (drv && drv->bdrv_ioctl) |
| 2578 | return drv->bdrv_ioctl(bs, req, buf); |
| 2579 | return -ENOTSUP; |
| 2580 | } |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 2581 | |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 2582 | BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, |
| 2583 | unsigned long int req, void *buf, |
| 2584 | BlockDriverCompletionFunc *cb, void *opaque) |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 2585 | { |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 2586 | BlockDriver *drv = bs->drv; |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 2587 | |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 2588 | if (drv && drv->bdrv_aio_ioctl) |
| 2589 | return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); |
| 2590 | return NULL; |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 2591 | } |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 2592 | |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 2593 | |
| 2594 | |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 2595 | void *qemu_blockalign(BlockDriverState *bs, size_t size) |
| 2596 | { |
| 2597 | return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); |
| 2598 | } |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 2599 | |
| 2600 | void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) |
| 2601 | { |
| 2602 | int64_t bitmap_size; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2603 | |
Liran Schour | aaa0eb7 | 2010-01-26 10:31:48 +0200 | [diff] [blame] | 2604 | bs->dirty_count = 0; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2605 | if (enable) { |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 2606 | if (!bs->dirty_bitmap) { |
| 2607 | bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + |
| 2608 | BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; |
| 2609 | bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2610 | |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 2611 | bs->dirty_bitmap = qemu_mallocz(bitmap_size); |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2612 | } |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 2613 | } else { |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 2614 | if (bs->dirty_bitmap) { |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 2615 | qemu_free(bs->dirty_bitmap); |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 2616 | bs->dirty_bitmap = NULL; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2617 | } |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 2618 | } |
| 2619 | } |
| 2620 | |
| 2621 | int bdrv_get_dirty(BlockDriverState *bs, int64_t sector) |
| 2622 | { |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2623 | int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2624 | |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 2625 | if (bs->dirty_bitmap && |
| 2626 | (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { |
| 2627 | return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] & |
| 2628 | (1 << (chunk % (sizeof(unsigned long) * 8))); |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 2629 | } else { |
| 2630 | return 0; |
| 2631 | } |
| 2632 | } |
| 2633 | |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2634 | void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, |
| 2635 | int nr_sectors) |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 2636 | { |
| 2637 | set_dirty_bitmap(bs, cur_sector, nr_sectors, 0); |
| 2638 | } |
Liran Schour | aaa0eb7 | 2010-01-26 10:31:48 +0200 | [diff] [blame] | 2639 | |
| 2640 | int64_t bdrv_get_dirty_count(BlockDriverState *bs) |
| 2641 | { |
| 2642 | return bs->dirty_count; |
| 2643 | } |