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