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