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" |
Stefan Hajnoczi | 6d519a5 | 2010-05-22 18:15:08 +0100 | [diff] [blame] | 26 | #include "trace.h" |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 27 | #include "monitor.h" |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 28 | #include "block_int.h" |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 29 | #include "module.h" |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 30 | #include "qemu-objects.h" |
Kevin Wolf | 6848542 | 2011-06-30 10:05:46 +0200 | [diff] [blame] | 31 | #include "qemu-coroutine.h" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 32 | |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 33 | #ifdef CONFIG_BSD |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 34 | #include <sys/types.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <sys/ioctl.h> |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 37 | #include <sys/queue.h> |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 38 | #ifndef __DragonFly__ |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 39 | #include <sys/disk.h> |
| 40 | #endif |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 41 | #endif |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 42 | |
aliguori | 49dc768 | 2009-03-08 16:26:59 +0000 | [diff] [blame] | 43 | #ifdef _WIN32 |
| 44 | #include <windows.h> |
| 45 | #endif |
| 46 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 47 | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, |
| 48 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
aliguori | c87c067 | 2009-04-07 18:43:20 +0000 | [diff] [blame] | 49 | BlockDriverCompletionFunc *cb, void *opaque); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 50 | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, |
| 51 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 52 | BlockDriverCompletionFunc *cb, void *opaque); |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 53 | static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, |
| 54 | BlockDriverCompletionFunc *cb, void *opaque); |
Alexander Graf | 016f5cf | 2010-05-26 17:51:49 +0200 | [diff] [blame] | 55 | static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs, |
| 56 | BlockDriverCompletionFunc *cb, void *opaque); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 57 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 58 | uint8_t *buf, int nb_sectors); |
| 59 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
| 60 | const uint8_t *buf, int nb_sectors); |
Kevin Wolf | 6848542 | 2011-06-30 10:05:46 +0200 | [diff] [blame] | 61 | static BlockDriverAIOCB *bdrv_co_aio_readv_em(BlockDriverState *bs, |
| 62 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
| 63 | BlockDriverCompletionFunc *cb, void *opaque); |
| 64 | static BlockDriverAIOCB *bdrv_co_aio_writev_em(BlockDriverState *bs, |
| 65 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
| 66 | BlockDriverCompletionFunc *cb, void *opaque); |
Kevin Wolf | f9f05dc | 2011-07-15 13:50:26 +0200 | [diff] [blame] | 67 | static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, |
| 68 | int64_t sector_num, int nb_sectors, |
| 69 | QEMUIOVector *iov); |
| 70 | static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, |
| 71 | int64_t sector_num, int nb_sectors, |
| 72 | QEMUIOVector *iov); |
Kevin Wolf | e7a8a78 | 2011-07-15 16:05:00 +0200 | [diff] [blame] | 73 | static int coroutine_fn bdrv_co_flush_em(BlockDriverState *bs); |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 74 | |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 75 | static QTAILQ_HEAD(, BlockDriverState) bdrv_states = |
| 76 | QTAILQ_HEAD_INITIALIZER(bdrv_states); |
blueswir1 | 7ee930d | 2008-09-17 19:04:14 +0000 | [diff] [blame] | 77 | |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 78 | static QLIST_HEAD(, BlockDriver) bdrv_drivers = |
| 79 | QLIST_HEAD_INITIALIZER(bdrv_drivers); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 80 | |
Markus Armbruster | f9092b1 | 2010-06-25 10:33:39 +0200 | [diff] [blame] | 81 | /* The device to use for VM snapshots */ |
| 82 | static BlockDriverState *bs_snapshots; |
| 83 | |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 84 | /* If non-zero, use only whitelisted block drivers */ |
| 85 | static int use_bdrv_whitelist; |
| 86 | |
Stefan Hajnoczi | 9e0b22f | 2010-12-09 11:53:00 +0000 | [diff] [blame] | 87 | #ifdef _WIN32 |
| 88 | static int is_windows_drive_prefix(const char *filename) |
| 89 | { |
| 90 | return (((filename[0] >= 'a' && filename[0] <= 'z') || |
| 91 | (filename[0] >= 'A' && filename[0] <= 'Z')) && |
| 92 | filename[1] == ':'); |
| 93 | } |
| 94 | |
| 95 | int is_windows_drive(const char *filename) |
| 96 | { |
| 97 | if (is_windows_drive_prefix(filename) && |
| 98 | filename[2] == '\0') |
| 99 | return 1; |
| 100 | if (strstart(filename, "\\\\.\\", NULL) || |
| 101 | strstart(filename, "//./", NULL)) |
| 102 | return 1; |
| 103 | return 0; |
| 104 | } |
| 105 | #endif |
| 106 | |
| 107 | /* check if the path starts with "<protocol>:" */ |
| 108 | static int path_has_protocol(const char *path) |
| 109 | { |
| 110 | #ifdef _WIN32 |
| 111 | if (is_windows_drive(path) || |
| 112 | is_windows_drive_prefix(path)) { |
| 113 | return 0; |
| 114 | } |
| 115 | #endif |
| 116 | |
| 117 | return strchr(path, ':') != NULL; |
| 118 | } |
| 119 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 120 | int path_is_absolute(const char *path) |
| 121 | { |
| 122 | const char *p; |
bellard | 2166442 | 2007-01-07 18:22:37 +0000 | [diff] [blame] | 123 | #ifdef _WIN32 |
| 124 | /* specific case for names like: "\\.\d:" */ |
| 125 | if (*path == '/' || *path == '\\') |
| 126 | return 1; |
| 127 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 128 | p = strchr(path, ':'); |
| 129 | if (p) |
| 130 | p++; |
| 131 | else |
| 132 | p = path; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 133 | #ifdef _WIN32 |
| 134 | return (*p == '/' || *p == '\\'); |
| 135 | #else |
| 136 | return (*p == '/'); |
| 137 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* if filename is absolute, just copy it to dest. Otherwise, build a |
| 141 | path to it by considering it is relative to base_path. URL are |
| 142 | supported. */ |
| 143 | void path_combine(char *dest, int dest_size, |
| 144 | const char *base_path, |
| 145 | const char *filename) |
| 146 | { |
| 147 | const char *p, *p1; |
| 148 | int len; |
| 149 | |
| 150 | if (dest_size <= 0) |
| 151 | return; |
| 152 | if (path_is_absolute(filename)) { |
| 153 | pstrcpy(dest, dest_size, filename); |
| 154 | } else { |
| 155 | p = strchr(base_path, ':'); |
| 156 | if (p) |
| 157 | p++; |
| 158 | else |
| 159 | p = base_path; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 160 | p1 = strrchr(base_path, '/'); |
| 161 | #ifdef _WIN32 |
| 162 | { |
| 163 | const char *p2; |
| 164 | p2 = strrchr(base_path, '\\'); |
| 165 | if (!p1 || p2 > p1) |
| 166 | p1 = p2; |
| 167 | } |
| 168 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 169 | if (p1) |
| 170 | p1++; |
| 171 | else |
| 172 | p1 = base_path; |
| 173 | if (p1 > p) |
| 174 | p = p1; |
| 175 | len = p - base_path; |
| 176 | if (len > dest_size - 1) |
| 177 | len = dest_size - 1; |
| 178 | memcpy(dest, base_path, len); |
| 179 | dest[len] = '\0'; |
| 180 | pstrcat(dest, dest_size, filename); |
| 181 | } |
| 182 | } |
| 183 | |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 184 | void bdrv_register(BlockDriver *bdrv) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 185 | { |
Kevin Wolf | 6848542 | 2011-06-30 10:05:46 +0200 | [diff] [blame] | 186 | if (bdrv->bdrv_co_readv) { |
| 187 | /* Emulate AIO by coroutines, and sync by AIO */ |
| 188 | bdrv->bdrv_aio_readv = bdrv_co_aio_readv_em; |
| 189 | bdrv->bdrv_aio_writev = bdrv_co_aio_writev_em; |
| 190 | bdrv->bdrv_read = bdrv_read_em; |
| 191 | bdrv->bdrv_write = bdrv_write_em; |
Kevin Wolf | f9f05dc | 2011-07-15 13:50:26 +0200 | [diff] [blame] | 192 | } else { |
| 193 | bdrv->bdrv_co_readv = bdrv_co_readv_em; |
| 194 | bdrv->bdrv_co_writev = bdrv_co_writev_em; |
| 195 | |
| 196 | if (!bdrv->bdrv_aio_readv) { |
| 197 | /* add AIO emulation layer */ |
| 198 | bdrv->bdrv_aio_readv = bdrv_aio_readv_em; |
| 199 | bdrv->bdrv_aio_writev = bdrv_aio_writev_em; |
| 200 | } else if (!bdrv->bdrv_read) { |
| 201 | /* add synchronous IO emulation layer */ |
| 202 | bdrv->bdrv_read = bdrv_read_em; |
| 203 | bdrv->bdrv_write = bdrv_write_em; |
| 204 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 205 | } |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 206 | |
| 207 | if (!bdrv->bdrv_aio_flush) |
| 208 | bdrv->bdrv_aio_flush = bdrv_aio_flush_em; |
| 209 | |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 210 | QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 211 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 212 | |
| 213 | /* create a new block device (by default it is empty) */ |
| 214 | BlockDriverState *bdrv_new(const char *device_name) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 215 | { |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 216 | BlockDriverState *bs; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 217 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 218 | bs = g_malloc0(sizeof(BlockDriverState)); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 219 | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 220 | if (device_name[0] != '\0') { |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 221 | QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 222 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 223 | return bs; |
| 224 | } |
| 225 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 226 | BlockDriver *bdrv_find_format(const char *format_name) |
| 227 | { |
| 228 | BlockDriver *drv1; |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 229 | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
| 230 | if (!strcmp(drv1->format_name, format_name)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 231 | return drv1; |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 232 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 233 | } |
| 234 | return NULL; |
| 235 | } |
| 236 | |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 237 | static int bdrv_is_whitelisted(BlockDriver *drv) |
| 238 | { |
| 239 | static const char *whitelist[] = { |
| 240 | CONFIG_BDRV_WHITELIST |
| 241 | }; |
| 242 | const char **p; |
| 243 | |
| 244 | if (!whitelist[0]) |
| 245 | return 1; /* no whitelist, anything goes */ |
| 246 | |
| 247 | for (p = whitelist; *p; p++) { |
| 248 | if (!strcmp(drv->format_name, *p)) { |
| 249 | return 1; |
| 250 | } |
| 251 | } |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | BlockDriver *bdrv_find_whitelisted_format(const char *format_name) |
| 256 | { |
| 257 | BlockDriver *drv = bdrv_find_format(format_name); |
| 258 | return drv && bdrv_is_whitelisted(drv) ? drv : NULL; |
| 259 | } |
| 260 | |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 261 | int bdrv_create(BlockDriver *drv, const char* filename, |
| 262 | QEMUOptionParameter *options) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 263 | { |
| 264 | if (!drv->bdrv_create) |
| 265 | return -ENOTSUP; |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 266 | |
| 267 | return drv->bdrv_create(filename, options); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 270 | int bdrv_create_file(const char* filename, QEMUOptionParameter *options) |
| 271 | { |
| 272 | BlockDriver *drv; |
| 273 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 274 | drv = bdrv_find_protocol(filename); |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 275 | if (drv == NULL) { |
Stefan Hajnoczi | 16905d7 | 2010-11-30 15:14:14 +0000 | [diff] [blame] | 276 | return -ENOENT; |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | return bdrv_create(drv, filename, options); |
| 280 | } |
| 281 | |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 282 | #ifdef _WIN32 |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 283 | void get_tmp_filename(char *filename, int size) |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 284 | { |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 285 | char temp_dir[MAX_PATH]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 286 | |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 287 | GetTempPath(MAX_PATH, temp_dir); |
| 288 | GetTempFileName(temp_dir, "qem", 0, filename); |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 289 | } |
| 290 | #else |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 291 | void get_tmp_filename(char *filename, int size) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 292 | { |
| 293 | int fd; |
blueswir1 | 7ccfb2e | 2008-09-14 06:45:34 +0000 | [diff] [blame] | 294 | const char *tmpdir; |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 295 | /* XXX: race condition possible */ |
aurel32 | 0badc1e | 2008-03-10 00:05:34 +0000 | [diff] [blame] | 296 | tmpdir = getenv("TMPDIR"); |
| 297 | if (!tmpdir) |
| 298 | tmpdir = "/tmp"; |
| 299 | snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 300 | fd = mkstemp(filename); |
| 301 | close(fd); |
| 302 | } |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 303 | #endif |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 304 | |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 305 | /* |
| 306 | * Detect host devices. By convention, /dev/cdrom[N] is always |
| 307 | * recognized as a host CDROM. |
| 308 | */ |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 309 | static BlockDriver *find_hdev_driver(const char *filename) |
| 310 | { |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 311 | int score_max = 0, score; |
| 312 | BlockDriver *drv = NULL, *d; |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 313 | |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 314 | QLIST_FOREACH(d, &bdrv_drivers, list) { |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 315 | if (d->bdrv_probe_device) { |
| 316 | score = d->bdrv_probe_device(filename); |
| 317 | if (score > score_max) { |
| 318 | score_max = score; |
| 319 | drv = d; |
| 320 | } |
| 321 | } |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 324 | return drv; |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 325 | } |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 326 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 327 | BlockDriver *bdrv_find_protocol(const char *filename) |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 328 | { |
| 329 | BlockDriver *drv1; |
| 330 | char protocol[128]; |
| 331 | int len; |
| 332 | const char *p; |
| 333 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 334 | /* TODO Drivers without bdrv_file_open must be specified explicitly */ |
| 335 | |
Christoph Hellwig | 39508e7 | 2010-06-23 12:25:17 +0200 | [diff] [blame] | 336 | /* |
| 337 | * XXX(hch): we really should not let host device detection |
| 338 | * override an explicit protocol specification, but moving this |
| 339 | * later breaks access to device names with colons in them. |
| 340 | * Thanks to the brain-dead persistent naming schemes on udev- |
| 341 | * based Linux systems those actually are quite common. |
| 342 | */ |
| 343 | drv1 = find_hdev_driver(filename); |
| 344 | if (drv1) { |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 345 | return drv1; |
| 346 | } |
Christoph Hellwig | 39508e7 | 2010-06-23 12:25:17 +0200 | [diff] [blame] | 347 | |
Stefan Hajnoczi | 9e0b22f | 2010-12-09 11:53:00 +0000 | [diff] [blame] | 348 | if (!path_has_protocol(filename)) { |
Christoph Hellwig | 39508e7 | 2010-06-23 12:25:17 +0200 | [diff] [blame] | 349 | return bdrv_find_format("file"); |
| 350 | } |
Stefan Hajnoczi | 9e0b22f | 2010-12-09 11:53:00 +0000 | [diff] [blame] | 351 | p = strchr(filename, ':'); |
| 352 | assert(p != NULL); |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 353 | len = p - filename; |
| 354 | if (len > sizeof(protocol) - 1) |
| 355 | len = sizeof(protocol) - 1; |
| 356 | memcpy(protocol, filename, len); |
| 357 | protocol[len] = '\0'; |
| 358 | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
| 359 | if (drv1->protocol_name && |
| 360 | !strcmp(drv1->protocol_name, protocol)) { |
| 361 | return drv1; |
| 362 | } |
| 363 | } |
| 364 | return NULL; |
| 365 | } |
| 366 | |
Stefan Weil | c98ac35 | 2010-07-21 21:51:51 +0200 | [diff] [blame] | 367 | static int find_image_format(const char *filename, BlockDriver **pdrv) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 368 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 369 | int ret, score, score_max; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 370 | BlockDriver *drv1, *drv; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 371 | uint8_t buf[2048]; |
| 372 | BlockDriverState *bs; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 373 | |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 374 | ret = bdrv_file_open(&bs, filename, 0); |
Stefan Weil | c98ac35 | 2010-07-21 21:51:51 +0200 | [diff] [blame] | 375 | if (ret < 0) { |
| 376 | *pdrv = NULL; |
| 377 | return ret; |
| 378 | } |
Nicholas Bellinger | f8ea0b0 | 2010-05-17 09:45:57 -0700 | [diff] [blame] | 379 | |
Kevin Wolf | 08a0055 | 2010-06-01 18:37:31 +0200 | [diff] [blame] | 380 | /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ |
| 381 | if (bs->sg || !bdrv_is_inserted(bs)) { |
Nicholas A. Bellinger | 1a39685 | 2010-05-27 08:56:28 -0700 | [diff] [blame] | 382 | bdrv_delete(bs); |
Stefan Weil | c98ac35 | 2010-07-21 21:51:51 +0200 | [diff] [blame] | 383 | drv = bdrv_find_format("raw"); |
| 384 | if (!drv) { |
| 385 | ret = -ENOENT; |
| 386 | } |
| 387 | *pdrv = drv; |
| 388 | return ret; |
Nicholas A. Bellinger | 1a39685 | 2010-05-27 08:56:28 -0700 | [diff] [blame] | 389 | } |
Nicholas Bellinger | f8ea0b0 | 2010-05-17 09:45:57 -0700 | [diff] [blame] | 390 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 391 | ret = bdrv_pread(bs, 0, buf, sizeof(buf)); |
| 392 | bdrv_delete(bs); |
| 393 | if (ret < 0) { |
Stefan Weil | c98ac35 | 2010-07-21 21:51:51 +0200 | [diff] [blame] | 394 | *pdrv = NULL; |
| 395 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 396 | } |
| 397 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 398 | score_max = 0; |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 399 | drv = NULL; |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 400 | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 401 | if (drv1->bdrv_probe) { |
| 402 | score = drv1->bdrv_probe(buf, ret, filename); |
| 403 | if (score > score_max) { |
| 404 | score_max = score; |
| 405 | drv = drv1; |
| 406 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 407 | } |
| 408 | } |
Stefan Weil | c98ac35 | 2010-07-21 21:51:51 +0200 | [diff] [blame] | 409 | if (!drv) { |
| 410 | ret = -ENOENT; |
| 411 | } |
| 412 | *pdrv = drv; |
| 413 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 416 | /** |
| 417 | * Set the current 'total_sectors' value |
| 418 | */ |
| 419 | static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) |
| 420 | { |
| 421 | BlockDriver *drv = bs->drv; |
| 422 | |
Nicholas Bellinger | 396759a | 2010-05-17 09:46:04 -0700 | [diff] [blame] | 423 | /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ |
| 424 | if (bs->sg) |
| 425 | return 0; |
| 426 | |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 427 | /* query actual device if possible, otherwise just trust the hint */ |
| 428 | if (drv->bdrv_getlength) { |
| 429 | int64_t length = drv->bdrv_getlength(bs); |
| 430 | if (length < 0) { |
| 431 | return length; |
| 432 | } |
| 433 | hint = length >> BDRV_SECTOR_BITS; |
| 434 | } |
| 435 | |
| 436 | bs->total_sectors = hint; |
| 437 | return 0; |
| 438 | } |
| 439 | |
Stefan Hajnoczi | c3993cd | 2011-08-04 12:26:51 +0100 | [diff] [blame] | 440 | /** |
| 441 | * Set open flags for a given cache mode |
| 442 | * |
| 443 | * Return 0 on success, -1 if the cache mode was invalid. |
| 444 | */ |
| 445 | int bdrv_parse_cache_flags(const char *mode, int *flags) |
| 446 | { |
| 447 | *flags &= ~BDRV_O_CACHE_MASK; |
| 448 | |
| 449 | if (!strcmp(mode, "off") || !strcmp(mode, "none")) { |
| 450 | *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; |
Stefan Hajnoczi | 92196b2 | 2011-08-04 12:26:52 +0100 | [diff] [blame] | 451 | } else if (!strcmp(mode, "directsync")) { |
| 452 | *flags |= BDRV_O_NOCACHE; |
Stefan Hajnoczi | c3993cd | 2011-08-04 12:26:51 +0100 | [diff] [blame] | 453 | } else if (!strcmp(mode, "writeback")) { |
| 454 | *flags |= BDRV_O_CACHE_WB; |
| 455 | } else if (!strcmp(mode, "unsafe")) { |
| 456 | *flags |= BDRV_O_CACHE_WB; |
| 457 | *flags |= BDRV_O_NO_FLUSH; |
| 458 | } else if (!strcmp(mode, "writethrough")) { |
| 459 | /* this is the default */ |
| 460 | } else { |
| 461 | return -1; |
| 462 | } |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 467 | /* |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 468 | * Common part for opening disk images and files |
| 469 | */ |
| 470 | static int bdrv_open_common(BlockDriverState *bs, const char *filename, |
| 471 | int flags, BlockDriver *drv) |
| 472 | { |
| 473 | int ret, open_flags; |
| 474 | |
| 475 | assert(drv != NULL); |
| 476 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 477 | bs->file = NULL; |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 478 | bs->total_sectors = 0; |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 479 | bs->encrypted = 0; |
| 480 | bs->valid_key = 0; |
| 481 | bs->open_flags = flags; |
| 482 | /* buffer_alignment defaulted to 512, drivers can change this value */ |
| 483 | bs->buffer_alignment = 512; |
| 484 | |
| 485 | pstrcpy(bs->filename, sizeof(bs->filename), filename); |
| 486 | |
| 487 | if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) { |
| 488 | return -ENOTSUP; |
| 489 | } |
| 490 | |
| 491 | bs->drv = drv; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 492 | bs->opaque = g_malloc0(drv->instance_size); |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 493 | |
Christoph Hellwig | a659979 | 2011-05-17 18:04:06 +0200 | [diff] [blame] | 494 | if (flags & BDRV_O_CACHE_WB) |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 495 | bs->enable_write_cache = 1; |
| 496 | |
| 497 | /* |
| 498 | * Clear flags that are internal to the block layer before opening the |
| 499 | * image. |
| 500 | */ |
| 501 | open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); |
| 502 | |
| 503 | /* |
Stefan Weil | ebabb67 | 2011-04-26 10:29:36 +0200 | [diff] [blame] | 504 | * Snapshots should be writable. |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 505 | */ |
| 506 | if (bs->is_temporary) { |
| 507 | open_flags |= BDRV_O_RDWR; |
| 508 | } |
| 509 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 510 | /* Open the image, either directly or using a protocol */ |
| 511 | if (drv->bdrv_file_open) { |
| 512 | ret = drv->bdrv_file_open(bs, filename, open_flags); |
| 513 | } else { |
| 514 | ret = bdrv_file_open(&bs->file, filename, open_flags); |
| 515 | if (ret >= 0) { |
| 516 | ret = drv->bdrv_open(bs, open_flags); |
| 517 | } |
| 518 | } |
| 519 | |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 520 | if (ret < 0) { |
| 521 | goto free_and_fail; |
| 522 | } |
| 523 | |
| 524 | bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR); |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 525 | |
| 526 | ret = refresh_total_sectors(bs, bs->total_sectors); |
| 527 | if (ret < 0) { |
| 528 | goto free_and_fail; |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 529 | } |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 530 | |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 531 | #ifndef _WIN32 |
| 532 | if (bs->is_temporary) { |
| 533 | unlink(filename); |
| 534 | } |
| 535 | #endif |
| 536 | return 0; |
| 537 | |
| 538 | free_and_fail: |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 539 | if (bs->file) { |
| 540 | bdrv_delete(bs->file); |
| 541 | bs->file = NULL; |
| 542 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 543 | g_free(bs->opaque); |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 544 | bs->opaque = NULL; |
| 545 | bs->drv = NULL; |
| 546 | return ret; |
| 547 | } |
| 548 | |
| 549 | /* |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 550 | * Opens a file using a protocol (file, host_device, nbd, ...) |
| 551 | */ |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 552 | int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 553 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 554 | BlockDriverState *bs; |
Christoph Hellwig | 6db9560 | 2010-04-05 16:53:57 +0200 | [diff] [blame] | 555 | BlockDriver *drv; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 556 | int ret; |
| 557 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 558 | drv = bdrv_find_protocol(filename); |
Christoph Hellwig | 6db9560 | 2010-04-05 16:53:57 +0200 | [diff] [blame] | 559 | if (!drv) { |
| 560 | return -ENOENT; |
| 561 | } |
| 562 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 563 | bs = bdrv_new(""); |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 564 | ret = bdrv_open_common(bs, filename, flags, drv); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 565 | if (ret < 0) { |
| 566 | bdrv_delete(bs); |
| 567 | return ret; |
bellard | 3b0d4f6 | 2005-10-30 18:30:10 +0000 | [diff] [blame] | 568 | } |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 569 | bs->growable = 1; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 570 | *pbs = bs; |
| 571 | return 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 572 | } |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 573 | |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 574 | /* |
| 575 | * Opens a disk image (raw, qcow2, vmdk, ...) |
| 576 | */ |
Kevin Wolf | d6e9098 | 2010-03-31 14:40:27 +0200 | [diff] [blame] | 577 | int bdrv_open(BlockDriverState *bs, const char *filename, int flags, |
| 578 | BlockDriver *drv) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 579 | { |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 580 | int ret; |
bellard | 712e787 | 2005-04-28 21:09:32 +0000 | [diff] [blame] | 581 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 582 | if (flags & BDRV_O_SNAPSHOT) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 583 | BlockDriverState *bs1; |
| 584 | int64_t total_size; |
aliguori | 7c96d46 | 2008-09-12 17:54:13 +0000 | [diff] [blame] | 585 | int is_protocol = 0; |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 586 | BlockDriver *bdrv_qcow2; |
| 587 | QEMUOptionParameter *options; |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 588 | char tmp_filename[PATH_MAX]; |
| 589 | char backing_filename[PATH_MAX]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 590 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 591 | /* if snapshot, we create a temporary backing file and open it |
| 592 | instead of opening 'filename' directly */ |
| 593 | |
| 594 | /* if there is a backing file, use it */ |
| 595 | bs1 = bdrv_new(""); |
Kevin Wolf | d6e9098 | 2010-03-31 14:40:27 +0200 | [diff] [blame] | 596 | ret = bdrv_open(bs1, filename, 0, drv); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 597 | if (ret < 0) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 598 | bdrv_delete(bs1); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 599 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 600 | } |
Jes Sorensen | 3e82990 | 2010-05-27 16:20:30 +0200 | [diff] [blame] | 601 | total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; |
aliguori | 7c96d46 | 2008-09-12 17:54:13 +0000 | [diff] [blame] | 602 | |
| 603 | if (bs1->drv && bs1->drv->protocol_name) |
| 604 | is_protocol = 1; |
| 605 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 606 | bdrv_delete(bs1); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 607 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 608 | get_tmp_filename(tmp_filename, sizeof(tmp_filename)); |
aliguori | 7c96d46 | 2008-09-12 17:54:13 +0000 | [diff] [blame] | 609 | |
| 610 | /* Real path is meaningless for protocols */ |
| 611 | if (is_protocol) |
| 612 | snprintf(backing_filename, sizeof(backing_filename), |
| 613 | "%s", filename); |
Kirill A. Shutemov | 114cdfa | 2009-12-25 18:19:22 +0000 | [diff] [blame] | 614 | else if (!realpath(filename, backing_filename)) |
| 615 | return -errno; |
aliguori | 7c96d46 | 2008-09-12 17:54:13 +0000 | [diff] [blame] | 616 | |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 617 | bdrv_qcow2 = bdrv_find_format("qcow2"); |
| 618 | options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); |
| 619 | |
Jes Sorensen | 3e82990 | 2010-05-27 16:20:30 +0200 | [diff] [blame] | 620 | set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size); |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 621 | set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); |
| 622 | if (drv) { |
| 623 | set_option_parameter(options, BLOCK_OPT_BACKING_FMT, |
| 624 | drv->format_name); |
| 625 | } |
| 626 | |
| 627 | ret = bdrv_create(bdrv_qcow2, tmp_filename, options); |
Jan Kiszka | d748768 | 2010-04-29 18:24:50 +0200 | [diff] [blame] | 628 | free_option_parameters(options); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 629 | if (ret < 0) { |
| 630 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 631 | } |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 632 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 633 | filename = tmp_filename; |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 634 | drv = bdrv_qcow2; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 635 | bs->is_temporary = 1; |
| 636 | } |
bellard | 712e787 | 2005-04-28 21:09:32 +0000 | [diff] [blame] | 637 | |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 638 | /* Find the right image format driver */ |
Christoph Hellwig | 6db9560 | 2010-04-05 16:53:57 +0200 | [diff] [blame] | 639 | if (!drv) { |
Stefan Weil | c98ac35 | 2010-07-21 21:51:51 +0200 | [diff] [blame] | 640 | ret = find_image_format(filename, &drv); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 641 | } |
Christoph Hellwig | 6987307 | 2010-01-20 18:13:25 +0100 | [diff] [blame] | 642 | |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 643 | if (!drv) { |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 644 | goto unlink_and_fail; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 645 | } |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 646 | |
| 647 | /* Open the image */ |
| 648 | ret = bdrv_open_common(bs, filename, flags, drv); |
| 649 | if (ret < 0) { |
Christoph Hellwig | 6987307 | 2010-01-20 18:13:25 +0100 | [diff] [blame] | 650 | goto unlink_and_fail; |
| 651 | } |
| 652 | |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 653 | /* If there is a backing file, use it */ |
| 654 | if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') { |
| 655 | char backing_filename[PATH_MAX]; |
| 656 | int back_flags; |
| 657 | BlockDriver *back_drv = NULL; |
| 658 | |
| 659 | bs->backing_hd = bdrv_new(""); |
Stefan Hajnoczi | df2dbb4 | 2010-12-02 16:54:13 +0000 | [diff] [blame] | 660 | |
| 661 | if (path_has_protocol(bs->backing_file)) { |
| 662 | pstrcpy(backing_filename, sizeof(backing_filename), |
| 663 | bs->backing_file); |
| 664 | } else { |
| 665 | path_combine(backing_filename, sizeof(backing_filename), |
| 666 | filename, bs->backing_file); |
| 667 | } |
| 668 | |
| 669 | if (bs->backing_format[0] != '\0') { |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 670 | back_drv = bdrv_find_format(bs->backing_format); |
Stefan Hajnoczi | df2dbb4 | 2010-12-02 16:54:13 +0000 | [diff] [blame] | 671 | } |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 672 | |
| 673 | /* backing files always opened read-only */ |
| 674 | back_flags = |
| 675 | flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); |
| 676 | |
| 677 | ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv); |
| 678 | if (ret < 0) { |
| 679 | bdrv_close(bs); |
| 680 | return ret; |
| 681 | } |
| 682 | if (bs->is_temporary) { |
| 683 | bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR); |
| 684 | } else { |
| 685 | /* base image inherits from "parent" */ |
| 686 | bs->backing_hd->keep_read_only = bs->keep_read_only; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | if (!bdrv_key_required(bs)) { |
| 691 | /* call the change callback */ |
| 692 | bs->media_changed = 1; |
| 693 | if (bs->change_cb) |
Christoph Hellwig | db97ee6 | 2011-01-24 13:32:41 +0100 | [diff] [blame] | 694 | bs->change_cb(bs->change_opaque, CHANGE_MEDIA); |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | return 0; |
| 698 | |
| 699 | unlink_and_fail: |
| 700 | if (bs->is_temporary) { |
| 701 | unlink(filename); |
| 702 | } |
| 703 | return ret; |
| 704 | } |
| 705 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 706 | void bdrv_close(BlockDriverState *bs) |
| 707 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 708 | if (bs->drv) { |
Markus Armbruster | f9092b1 | 2010-06-25 10:33:39 +0200 | [diff] [blame] | 709 | if (bs == bs_snapshots) { |
| 710 | bs_snapshots = NULL; |
| 711 | } |
Stefan Hajnoczi | 557df6a | 2010-04-17 10:49:06 +0100 | [diff] [blame] | 712 | if (bs->backing_hd) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 713 | bdrv_delete(bs->backing_hd); |
Stefan Hajnoczi | 557df6a | 2010-04-17 10:49:06 +0100 | [diff] [blame] | 714 | bs->backing_hd = NULL; |
| 715 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 716 | bs->drv->bdrv_close(bs); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 717 | g_free(bs->opaque); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 718 | #ifdef _WIN32 |
| 719 | if (bs->is_temporary) { |
| 720 | unlink(bs->filename); |
| 721 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 722 | #endif |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 723 | bs->opaque = NULL; |
| 724 | bs->drv = NULL; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 725 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 726 | if (bs->file != NULL) { |
| 727 | bdrv_close(bs->file); |
| 728 | } |
| 729 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 730 | /* call the change callback */ |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 731 | bs->media_changed = 1; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 732 | if (bs->change_cb) |
Christoph Hellwig | db97ee6 | 2011-01-24 13:32:41 +0100 | [diff] [blame] | 733 | bs->change_cb(bs->change_opaque, CHANGE_MEDIA); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
MORITA Kazutaka | 2bc93fe | 2010-05-28 11:44:57 +0900 | [diff] [blame] | 737 | void bdrv_close_all(void) |
| 738 | { |
| 739 | BlockDriverState *bs; |
| 740 | |
| 741 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
| 742 | bdrv_close(bs); |
| 743 | } |
| 744 | } |
| 745 | |
Ryan Harper | d22b2f4 | 2011-03-29 20:51:47 -0500 | [diff] [blame] | 746 | /* make a BlockDriverState anonymous by removing from bdrv_state list. |
| 747 | Also, NULL terminate the device_name to prevent double remove */ |
| 748 | void bdrv_make_anon(BlockDriverState *bs) |
| 749 | { |
| 750 | if (bs->device_name[0] != '\0') { |
| 751 | QTAILQ_REMOVE(&bdrv_states, bs, list); |
| 752 | } |
| 753 | bs->device_name[0] = '\0'; |
| 754 | } |
| 755 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 756 | void bdrv_delete(BlockDriverState *bs) |
| 757 | { |
Markus Armbruster | 18846de | 2010-06-29 16:58:30 +0200 | [diff] [blame] | 758 | assert(!bs->peer); |
| 759 | |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 760 | /* remove from list, if necessary */ |
Ryan Harper | d22b2f4 | 2011-03-29 20:51:47 -0500 | [diff] [blame] | 761 | bdrv_make_anon(bs); |
aurel32 | 34c6f05 | 2008-04-08 19:51:21 +0000 | [diff] [blame] | 762 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 763 | bdrv_close(bs); |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 764 | if (bs->file != NULL) { |
| 765 | bdrv_delete(bs->file); |
| 766 | } |
| 767 | |
Markus Armbruster | f9092b1 | 2010-06-25 10:33:39 +0200 | [diff] [blame] | 768 | assert(bs != bs_snapshots); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 769 | g_free(bs); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Markus Armbruster | 18846de | 2010-06-29 16:58:30 +0200 | [diff] [blame] | 772 | int bdrv_attach(BlockDriverState *bs, DeviceState *qdev) |
| 773 | { |
| 774 | if (bs->peer) { |
| 775 | return -EBUSY; |
| 776 | } |
| 777 | bs->peer = qdev; |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | void bdrv_detach(BlockDriverState *bs, DeviceState *qdev) |
| 782 | { |
| 783 | assert(bs->peer == qdev); |
| 784 | bs->peer = NULL; |
Markus Armbruster | a19712b | 2011-07-20 18:23:36 +0200 | [diff] [blame] | 785 | bs->change_cb = NULL; |
| 786 | bs->change_opaque = NULL; |
Markus Armbruster | 18846de | 2010-06-29 16:58:30 +0200 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | DeviceState *bdrv_get_attached(BlockDriverState *bs) |
| 790 | { |
| 791 | return bs->peer; |
| 792 | } |
| 793 | |
aliguori | e97fc19 | 2009-04-21 23:11:50 +0000 | [diff] [blame] | 794 | /* |
| 795 | * Run consistency checks on an image |
| 796 | * |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 797 | * Returns 0 if the check could be completed (it doesn't mean that the image is |
Stefan Weil | a1c7273 | 2011-04-28 17:20:38 +0200 | [diff] [blame] | 798 | * free of errors) or -errno when an internal error occurred. The results of the |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 799 | * check are stored in res. |
aliguori | e97fc19 | 2009-04-21 23:11:50 +0000 | [diff] [blame] | 800 | */ |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 801 | int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res) |
aliguori | e97fc19 | 2009-04-21 23:11:50 +0000 | [diff] [blame] | 802 | { |
| 803 | if (bs->drv->bdrv_check == NULL) { |
| 804 | return -ENOTSUP; |
| 805 | } |
| 806 | |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 807 | memset(res, 0, sizeof(*res)); |
Kevin Wolf | 9ac228e | 2010-06-29 12:37:54 +0200 | [diff] [blame] | 808 | return bs->drv->bdrv_check(bs, res); |
aliguori | e97fc19 | 2009-04-21 23:11:50 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Kevin Wolf | 8a42661 | 2010-07-16 17:17:01 +0200 | [diff] [blame] | 811 | #define COMMIT_BUF_SECTORS 2048 |
| 812 | |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 813 | /* commit COW file into the raw image */ |
| 814 | int bdrv_commit(BlockDriverState *bs) |
| 815 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 816 | BlockDriver *drv = bs->drv; |
Kevin Wolf | ee18119 | 2010-08-05 13:05:22 +0200 | [diff] [blame] | 817 | BlockDriver *backing_drv; |
Kevin Wolf | 8a42661 | 2010-07-16 17:17:01 +0200 | [diff] [blame] | 818 | int64_t sector, total_sectors; |
| 819 | int n, ro, open_flags; |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 820 | int ret = 0, rw_ret = 0; |
Kevin Wolf | 8a42661 | 2010-07-16 17:17:01 +0200 | [diff] [blame] | 821 | uint8_t *buf; |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 822 | char filename[1024]; |
| 823 | BlockDriverState *bs_rw, *bs_ro; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 824 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 825 | if (!drv) |
| 826 | return -ENOMEDIUM; |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 827 | |
| 828 | if (!bs->backing_hd) { |
| 829 | return -ENOTSUP; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 832 | if (bs->backing_hd->keep_read_only) { |
| 833 | return -EACCES; |
| 834 | } |
Kevin Wolf | ee18119 | 2010-08-05 13:05:22 +0200 | [diff] [blame] | 835 | |
| 836 | backing_drv = bs->backing_hd->drv; |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 837 | ro = bs->backing_hd->read_only; |
| 838 | strncpy(filename, bs->backing_hd->filename, sizeof(filename)); |
| 839 | open_flags = bs->backing_hd->open_flags; |
| 840 | |
| 841 | if (ro) { |
| 842 | /* re-open as RW */ |
| 843 | bdrv_delete(bs->backing_hd); |
| 844 | bs->backing_hd = NULL; |
| 845 | bs_rw = bdrv_new(""); |
Kevin Wolf | ee18119 | 2010-08-05 13:05:22 +0200 | [diff] [blame] | 846 | rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, |
| 847 | backing_drv); |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 848 | if (rw_ret < 0) { |
| 849 | bdrv_delete(bs_rw); |
| 850 | /* try to re-open read-only */ |
| 851 | bs_ro = bdrv_new(""); |
Kevin Wolf | ee18119 | 2010-08-05 13:05:22 +0200 | [diff] [blame] | 852 | ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, |
| 853 | backing_drv); |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 854 | if (ret < 0) { |
| 855 | bdrv_delete(bs_ro); |
| 856 | /* drive not functional anymore */ |
| 857 | bs->drv = NULL; |
| 858 | return ret; |
| 859 | } |
| 860 | bs->backing_hd = bs_ro; |
| 861 | return rw_ret; |
| 862 | } |
| 863 | bs->backing_hd = bs_rw; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 864 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 865 | |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 866 | total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 867 | buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 868 | |
Kevin Wolf | 8a42661 | 2010-07-16 17:17:01 +0200 | [diff] [blame] | 869 | for (sector = 0; sector < total_sectors; sector += n) { |
| 870 | if (drv->bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) { |
| 871 | |
| 872 | if (bdrv_read(bs, sector, buf, n) != 0) { |
| 873 | ret = -EIO; |
| 874 | goto ro_cleanup; |
| 875 | } |
| 876 | |
| 877 | if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { |
| 878 | ret = -EIO; |
| 879 | goto ro_cleanup; |
| 880 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 881 | } |
| 882 | } |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 883 | |
Christoph Hellwig | 1d44952 | 2010-01-17 12:32:30 +0100 | [diff] [blame] | 884 | if (drv->bdrv_make_empty) { |
| 885 | ret = drv->bdrv_make_empty(bs); |
| 886 | bdrv_flush(bs); |
| 887 | } |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 888 | |
Christoph Hellwig | 3f5075a | 2010-01-12 13:49:23 +0100 | [diff] [blame] | 889 | /* |
| 890 | * Make sure all data we wrote to the backing device is actually |
| 891 | * stable on disk. |
| 892 | */ |
| 893 | if (bs->backing_hd) |
| 894 | bdrv_flush(bs->backing_hd); |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 895 | |
| 896 | ro_cleanup: |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 897 | g_free(buf); |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 898 | |
| 899 | if (ro) { |
| 900 | /* re-open as RO */ |
| 901 | bdrv_delete(bs->backing_hd); |
| 902 | bs->backing_hd = NULL; |
| 903 | bs_ro = bdrv_new(""); |
Kevin Wolf | ee18119 | 2010-08-05 13:05:22 +0200 | [diff] [blame] | 904 | ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, |
| 905 | backing_drv); |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 906 | if (ret < 0) { |
| 907 | bdrv_delete(bs_ro); |
| 908 | /* drive not functional anymore */ |
| 909 | bs->drv = NULL; |
| 910 | return ret; |
| 911 | } |
| 912 | bs->backing_hd = bs_ro; |
| 913 | bs->backing_hd->keep_read_only = 0; |
| 914 | } |
| 915 | |
Christoph Hellwig | 1d44952 | 2010-01-17 12:32:30 +0100 | [diff] [blame] | 916 | return ret; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Markus Armbruster | 6ab4b5a | 2010-06-02 18:55:18 +0200 | [diff] [blame] | 919 | void bdrv_commit_all(void) |
| 920 | { |
| 921 | BlockDriverState *bs; |
| 922 | |
| 923 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
| 924 | bdrv_commit(bs); |
| 925 | } |
| 926 | } |
| 927 | |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 928 | /* |
| 929 | * Return values: |
| 930 | * 0 - success |
| 931 | * -EINVAL - backing format specified, but no file |
| 932 | * -ENOSPC - can't update the backing file because no space is left in the |
| 933 | * image file header |
| 934 | * -ENOTSUP - format driver doesn't support changing the backing file |
| 935 | */ |
| 936 | int bdrv_change_backing_file(BlockDriverState *bs, |
| 937 | const char *backing_file, const char *backing_fmt) |
| 938 | { |
| 939 | BlockDriver *drv = bs->drv; |
| 940 | |
| 941 | if (drv->bdrv_change_backing_file != NULL) { |
| 942 | return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); |
| 943 | } else { |
| 944 | return -ENOTSUP; |
| 945 | } |
| 946 | } |
| 947 | |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 948 | static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, |
| 949 | size_t size) |
| 950 | { |
| 951 | int64_t len; |
| 952 | |
| 953 | if (!bdrv_is_inserted(bs)) |
| 954 | return -ENOMEDIUM; |
| 955 | |
| 956 | if (bs->growable) |
| 957 | return 0; |
| 958 | |
| 959 | len = bdrv_getlength(bs); |
| 960 | |
Kevin Wolf | fbb7b4e | 2009-05-08 14:47:24 +0200 | [diff] [blame] | 961 | if (offset < 0) |
| 962 | return -EIO; |
| 963 | |
| 964 | if ((offset > len) || (len - offset < size)) |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 965 | return -EIO; |
| 966 | |
| 967 | return 0; |
| 968 | } |
| 969 | |
| 970 | static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, |
| 971 | int nb_sectors) |
| 972 | { |
Jes Sorensen | eb5a316 | 2010-05-27 16:20:31 +0200 | [diff] [blame] | 973 | return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, |
| 974 | nb_sectors * BDRV_SECTOR_SIZE); |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Kevin Wolf | e7a8a78 | 2011-07-15 16:05:00 +0200 | [diff] [blame] | 977 | static inline bool bdrv_has_async_rw(BlockDriver *drv) |
| 978 | { |
| 979 | return drv->bdrv_co_readv != bdrv_co_readv_em |
| 980 | || drv->bdrv_aio_readv != bdrv_aio_readv_em; |
| 981 | } |
| 982 | |
| 983 | static inline bool bdrv_has_async_flush(BlockDriver *drv) |
| 984 | { |
| 985 | return drv->bdrv_aio_flush != bdrv_aio_flush_em; |
| 986 | } |
| 987 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 988 | /* return < 0 if error. See bdrv_write() for the return codes */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 989 | int bdrv_read(BlockDriverState *bs, int64_t sector_num, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 990 | uint8_t *buf, int nb_sectors) |
| 991 | { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 992 | BlockDriver *drv = bs->drv; |
| 993 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 994 | if (!drv) |
| 995 | return -ENOMEDIUM; |
Kevin Wolf | e7a8a78 | 2011-07-15 16:05:00 +0200 | [diff] [blame] | 996 | |
| 997 | if (bdrv_has_async_rw(drv) && qemu_in_coroutine()) { |
| 998 | QEMUIOVector qiov; |
| 999 | struct iovec iov = { |
| 1000 | .iov_base = (void *)buf, |
| 1001 | .iov_len = nb_sectors * BDRV_SECTOR_SIZE, |
| 1002 | }; |
| 1003 | |
| 1004 | qemu_iovec_init_external(&qiov, &iov, 1); |
| 1005 | return bdrv_co_readv(bs, sector_num, nb_sectors, &qiov); |
| 1006 | } |
| 1007 | |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 1008 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 1009 | return -EIO; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1010 | |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 1011 | return drv->bdrv_read(bs, sector_num, buf, nb_sectors); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 1014 | static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1015 | int nb_sectors, int dirty) |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 1016 | { |
| 1017 | int64_t start, end; |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 1018 | unsigned long val, idx, bit; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1019 | |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1020 | start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 1021 | end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1022 | |
| 1023 | for (; start <= end; start++) { |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 1024 | idx = start / (sizeof(unsigned long) * 8); |
| 1025 | bit = start % (sizeof(unsigned long) * 8); |
| 1026 | val = bs->dirty_bitmap[idx]; |
| 1027 | if (dirty) { |
Marcelo Tosatti | 6d59fec | 2010-11-08 17:02:54 -0200 | [diff] [blame] | 1028 | if (!(val & (1UL << bit))) { |
Liran Schour | aaa0eb7 | 2010-01-26 10:31:48 +0200 | [diff] [blame] | 1029 | bs->dirty_count++; |
Marcelo Tosatti | 6d59fec | 2010-11-08 17:02:54 -0200 | [diff] [blame] | 1030 | val |= 1UL << bit; |
Liran Schour | aaa0eb7 | 2010-01-26 10:31:48 +0200 | [diff] [blame] | 1031 | } |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 1032 | } else { |
Marcelo Tosatti | 6d59fec | 2010-11-08 17:02:54 -0200 | [diff] [blame] | 1033 | if (val & (1UL << bit)) { |
Liran Schour | aaa0eb7 | 2010-01-26 10:31:48 +0200 | [diff] [blame] | 1034 | bs->dirty_count--; |
Marcelo Tosatti | 6d59fec | 2010-11-08 17:02:54 -0200 | [diff] [blame] | 1035 | val &= ~(1UL << bit); |
Liran Schour | aaa0eb7 | 2010-01-26 10:31:48 +0200 | [diff] [blame] | 1036 | } |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 1037 | } |
| 1038 | bs->dirty_bitmap[idx] = val; |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 1039 | } |
| 1040 | } |
| 1041 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1042 | /* Return < 0 if error. Important errors are: |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1043 | -EIO generic I/O error (may happen for all errors) |
| 1044 | -ENOMEDIUM No media inserted. |
| 1045 | -EINVAL Invalid sector number or nb_sectors |
| 1046 | -EACCES Trying to write a read-only device |
| 1047 | */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1048 | int bdrv_write(BlockDriverState *bs, int64_t sector_num, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 1049 | const uint8_t *buf, int nb_sectors) |
| 1050 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1051 | BlockDriver *drv = bs->drv; |
Kevin Wolf | e7a8a78 | 2011-07-15 16:05:00 +0200 | [diff] [blame] | 1052 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1053 | if (!bs->drv) |
| 1054 | return -ENOMEDIUM; |
Kevin Wolf | e7a8a78 | 2011-07-15 16:05:00 +0200 | [diff] [blame] | 1055 | |
| 1056 | if (bdrv_has_async_rw(drv) && qemu_in_coroutine()) { |
| 1057 | QEMUIOVector qiov; |
| 1058 | struct iovec iov = { |
| 1059 | .iov_base = (void *)buf, |
| 1060 | .iov_len = nb_sectors * BDRV_SECTOR_SIZE, |
| 1061 | }; |
| 1062 | |
| 1063 | qemu_iovec_init_external(&qiov, &iov, 1); |
| 1064 | return bdrv_co_writev(bs, sector_num, nb_sectors, &qiov); |
| 1065 | } |
| 1066 | |
bellard | 0849bf0 | 2003-06-30 23:17:31 +0000 | [diff] [blame] | 1067 | if (bs->read_only) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1068 | return -EACCES; |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 1069 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 1070 | return -EIO; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1071 | |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 1072 | if (bs->dirty_bitmap) { |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 1073 | set_dirty_bitmap(bs, sector_num, nb_sectors, 1); |
| 1074 | } |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1075 | |
Kevin Wolf | 294cc35 | 2010-04-28 14:34:01 +0200 | [diff] [blame] | 1076 | if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { |
| 1077 | bs->wr_highest_sector = sector_num + nb_sectors - 1; |
| 1078 | } |
| 1079 | |
aliguori | 42fb280 | 2009-01-15 20:43:39 +0000 | [diff] [blame] | 1080 | return drv->bdrv_write(bs, sector_num, buf, nb_sectors); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 1083 | int bdrv_pread(BlockDriverState *bs, int64_t offset, |
| 1084 | void *buf, int count1) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1085 | { |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1086 | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1087 | int len, nb_sectors, count; |
| 1088 | int64_t sector_num; |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 1089 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1090 | |
| 1091 | count = count1; |
| 1092 | /* first read to align to sector start */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1093 | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1094 | if (len > count) |
| 1095 | len = count; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1096 | sector_num = offset >> BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1097 | if (len > 0) { |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 1098 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
| 1099 | return ret; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1100 | memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1101 | count -= len; |
| 1102 | if (count == 0) |
| 1103 | return count1; |
| 1104 | sector_num++; |
| 1105 | buf += len; |
| 1106 | } |
| 1107 | |
| 1108 | /* read the sectors "in place" */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1109 | nb_sectors = count >> BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1110 | if (nb_sectors > 0) { |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 1111 | if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) |
| 1112 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1113 | sector_num += nb_sectors; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1114 | len = nb_sectors << BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1115 | buf += len; |
| 1116 | count -= len; |
| 1117 | } |
| 1118 | |
| 1119 | /* add data from the last sector */ |
| 1120 | if (count > 0) { |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 1121 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
| 1122 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1123 | memcpy(buf, tmp_buf, count); |
| 1124 | } |
| 1125 | return count1; |
| 1126 | } |
| 1127 | |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 1128 | int bdrv_pwrite(BlockDriverState *bs, int64_t offset, |
| 1129 | const void *buf, int count1) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1130 | { |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1131 | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1132 | int len, nb_sectors, count; |
| 1133 | int64_t sector_num; |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 1134 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1135 | |
| 1136 | count = count1; |
| 1137 | /* first write to align to sector start */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1138 | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1139 | if (len > count) |
| 1140 | len = count; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1141 | sector_num = offset >> BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1142 | if (len > 0) { |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 1143 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
| 1144 | return ret; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1145 | memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len); |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 1146 | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
| 1147 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1148 | count -= len; |
| 1149 | if (count == 0) |
| 1150 | return count1; |
| 1151 | sector_num++; |
| 1152 | buf += len; |
| 1153 | } |
| 1154 | |
| 1155 | /* write the sectors "in place" */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1156 | nb_sectors = count >> BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1157 | if (nb_sectors > 0) { |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 1158 | if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) |
| 1159 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1160 | sector_num += nb_sectors; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1161 | len = nb_sectors << BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1162 | buf += len; |
| 1163 | count -= len; |
| 1164 | } |
| 1165 | |
| 1166 | /* add data from the last sector */ |
| 1167 | if (count > 0) { |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 1168 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
| 1169 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1170 | memcpy(tmp_buf, buf, count); |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 1171 | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
| 1172 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1173 | } |
| 1174 | return count1; |
| 1175 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1176 | |
Kevin Wolf | f08145f | 2010-06-16 16:38:15 +0200 | [diff] [blame] | 1177 | /* |
| 1178 | * Writes to the file and ensures that no writes are reordered across this |
| 1179 | * request (acts as a barrier) |
| 1180 | * |
| 1181 | * Returns 0 on success, -errno in error cases. |
| 1182 | */ |
| 1183 | int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, |
| 1184 | const void *buf, int count) |
| 1185 | { |
| 1186 | int ret; |
| 1187 | |
| 1188 | ret = bdrv_pwrite(bs, offset, buf, count); |
| 1189 | if (ret < 0) { |
| 1190 | return ret; |
| 1191 | } |
| 1192 | |
Stefan Hajnoczi | 92196b2 | 2011-08-04 12:26:52 +0100 | [diff] [blame] | 1193 | /* No flush needed for cache modes that use O_DSYNC */ |
| 1194 | if ((bs->open_flags & BDRV_O_CACHE_WB) != 0) { |
Kevin Wolf | f08145f | 2010-06-16 16:38:15 +0200 | [diff] [blame] | 1195 | bdrv_flush(bs); |
| 1196 | } |
| 1197 | |
| 1198 | return 0; |
| 1199 | } |
| 1200 | |
Kevin Wolf | da1fa91 | 2011-07-14 17:27:13 +0200 | [diff] [blame] | 1201 | int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, |
| 1202 | int nb_sectors, QEMUIOVector *qiov) |
| 1203 | { |
| 1204 | BlockDriver *drv = bs->drv; |
| 1205 | |
| 1206 | trace_bdrv_co_readv(bs, sector_num, nb_sectors); |
| 1207 | |
| 1208 | if (!drv) { |
| 1209 | return -ENOMEDIUM; |
| 1210 | } |
| 1211 | if (bdrv_check_request(bs, sector_num, nb_sectors)) { |
| 1212 | return -EIO; |
| 1213 | } |
| 1214 | |
| 1215 | return drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); |
| 1216 | } |
| 1217 | |
| 1218 | int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, |
| 1219 | int nb_sectors, QEMUIOVector *qiov) |
| 1220 | { |
| 1221 | BlockDriver *drv = bs->drv; |
| 1222 | |
| 1223 | trace_bdrv_co_writev(bs, sector_num, nb_sectors); |
| 1224 | |
| 1225 | if (!bs->drv) { |
| 1226 | return -ENOMEDIUM; |
| 1227 | } |
| 1228 | if (bs->read_only) { |
| 1229 | return -EACCES; |
| 1230 | } |
| 1231 | if (bdrv_check_request(bs, sector_num, nb_sectors)) { |
| 1232 | return -EIO; |
| 1233 | } |
| 1234 | |
| 1235 | if (bs->dirty_bitmap) { |
| 1236 | set_dirty_bitmap(bs, sector_num, nb_sectors, 1); |
| 1237 | } |
| 1238 | |
| 1239 | if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { |
| 1240 | bs->wr_highest_sector = sector_num + nb_sectors - 1; |
| 1241 | } |
| 1242 | |
| 1243 | return drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); |
| 1244 | } |
| 1245 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1246 | /** |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1247 | * Truncate file to 'offset' bytes (needed only for file protocols) |
| 1248 | */ |
| 1249 | int bdrv_truncate(BlockDriverState *bs, int64_t offset) |
| 1250 | { |
| 1251 | BlockDriver *drv = bs->drv; |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 1252 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1253 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1254 | return -ENOMEDIUM; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1255 | if (!drv->bdrv_truncate) |
| 1256 | return -ENOTSUP; |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 1257 | if (bs->read_only) |
| 1258 | return -EACCES; |
Marcelo Tosatti | 8591675 | 2011-01-26 12:12:35 -0200 | [diff] [blame] | 1259 | if (bdrv_in_use(bs)) |
| 1260 | return -EBUSY; |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 1261 | ret = drv->bdrv_truncate(bs, offset); |
| 1262 | if (ret == 0) { |
| 1263 | ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); |
Christoph Hellwig | db97ee6 | 2011-01-24 13:32:41 +0100 | [diff] [blame] | 1264 | if (bs->change_cb) { |
| 1265 | bs->change_cb(bs->change_opaque, CHANGE_SIZE); |
| 1266 | } |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 1267 | } |
| 1268 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | /** |
Fam Zheng | 4a1d5e1 | 2011-07-12 19:56:39 +0800 | [diff] [blame] | 1272 | * Length of a allocated file in bytes. Sparse files are counted by actual |
| 1273 | * allocated space. Return < 0 if error or unknown. |
| 1274 | */ |
| 1275 | int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) |
| 1276 | { |
| 1277 | BlockDriver *drv = bs->drv; |
| 1278 | if (!drv) { |
| 1279 | return -ENOMEDIUM; |
| 1280 | } |
| 1281 | if (drv->bdrv_get_allocated_file_size) { |
| 1282 | return drv->bdrv_get_allocated_file_size(bs); |
| 1283 | } |
| 1284 | if (bs->file) { |
| 1285 | return bdrv_get_allocated_file_size(bs->file); |
| 1286 | } |
| 1287 | return -ENOTSUP; |
| 1288 | } |
| 1289 | |
| 1290 | /** |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1291 | * Length of a file in bytes. Return < 0 if error or unknown. |
| 1292 | */ |
| 1293 | int64_t bdrv_getlength(BlockDriverState *bs) |
| 1294 | { |
| 1295 | BlockDriver *drv = bs->drv; |
| 1296 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1297 | return -ENOMEDIUM; |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 1298 | |
Stefan Hajnoczi | 46a4e4e | 2011-03-29 20:04:41 +0100 | [diff] [blame] | 1299 | if (bs->growable || bs->removable) { |
| 1300 | if (drv->bdrv_getlength) { |
| 1301 | return drv->bdrv_getlength(bs); |
| 1302 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1303 | } |
Stefan Hajnoczi | 46a4e4e | 2011-03-29 20:04:41 +0100 | [diff] [blame] | 1304 | return bs->total_sectors * BDRV_SECTOR_SIZE; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1307 | /* return 0 as number of sectors if no device present or error */ |
ths | 96b8f13 | 2007-12-17 01:35:20 +0000 | [diff] [blame] | 1308 | void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 1309 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1310 | int64_t length; |
| 1311 | length = bdrv_getlength(bs); |
| 1312 | if (length < 0) |
| 1313 | length = 0; |
| 1314 | else |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 1315 | length = length >> BDRV_SECTOR_BITS; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1316 | *nb_sectors_ptr = length; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 1317 | } |
bellard | cf98951 | 2004-02-16 21:56:36 +0000 | [diff] [blame] | 1318 | |
aliguori | f3d54fc | 2008-11-25 21:50:24 +0000 | [diff] [blame] | 1319 | struct partition { |
| 1320 | uint8_t boot_ind; /* 0x80 - active */ |
| 1321 | uint8_t head; /* starting head */ |
| 1322 | uint8_t sector; /* starting sector */ |
| 1323 | uint8_t cyl; /* starting cylinder */ |
| 1324 | uint8_t sys_ind; /* What partition type */ |
| 1325 | uint8_t end_head; /* end head */ |
| 1326 | uint8_t end_sector; /* end sector */ |
| 1327 | uint8_t end_cyl; /* end cylinder */ |
| 1328 | uint32_t start_sect; /* starting sector counting from 0 */ |
| 1329 | uint32_t nr_sects; /* nr of sectors in partition */ |
| 1330 | } __attribute__((packed)); |
| 1331 | |
| 1332 | /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ |
| 1333 | static int guess_disk_lchs(BlockDriverState *bs, |
| 1334 | int *pcylinders, int *pheads, int *psectors) |
| 1335 | { |
Jes Sorensen | eb5a316 | 2010-05-27 16:20:31 +0200 | [diff] [blame] | 1336 | uint8_t buf[BDRV_SECTOR_SIZE]; |
aliguori | f3d54fc | 2008-11-25 21:50:24 +0000 | [diff] [blame] | 1337 | int ret, i, heads, sectors, cylinders; |
| 1338 | struct partition *p; |
| 1339 | uint32_t nr_sects; |
blueswir1 | a38131b | 2008-12-05 17:56:40 +0000 | [diff] [blame] | 1340 | uint64_t nb_sectors; |
aliguori | f3d54fc | 2008-11-25 21:50:24 +0000 | [diff] [blame] | 1341 | |
| 1342 | bdrv_get_geometry(bs, &nb_sectors); |
| 1343 | |
| 1344 | ret = bdrv_read(bs, 0, buf, 1); |
| 1345 | if (ret < 0) |
| 1346 | return -1; |
| 1347 | /* test msdos magic */ |
| 1348 | if (buf[510] != 0x55 || buf[511] != 0xaa) |
| 1349 | return -1; |
| 1350 | for(i = 0; i < 4; i++) { |
| 1351 | p = ((struct partition *)(buf + 0x1be)) + i; |
| 1352 | nr_sects = le32_to_cpu(p->nr_sects); |
| 1353 | if (nr_sects && p->end_head) { |
| 1354 | /* We make the assumption that the partition terminates on |
| 1355 | a cylinder boundary */ |
| 1356 | heads = p->end_head + 1; |
| 1357 | sectors = p->end_sector & 63; |
| 1358 | if (sectors == 0) |
| 1359 | continue; |
| 1360 | cylinders = nb_sectors / (heads * sectors); |
| 1361 | if (cylinders < 1 || cylinders > 16383) |
| 1362 | continue; |
| 1363 | *pheads = heads; |
| 1364 | *psectors = sectors; |
| 1365 | *pcylinders = cylinders; |
| 1366 | #if 0 |
| 1367 | printf("guessed geometry: LCHS=%d %d %d\n", |
| 1368 | cylinders, heads, sectors); |
| 1369 | #endif |
| 1370 | return 0; |
| 1371 | } |
| 1372 | } |
| 1373 | return -1; |
| 1374 | } |
| 1375 | |
| 1376 | void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) |
| 1377 | { |
| 1378 | int translation, lba_detected = 0; |
| 1379 | int cylinders, heads, secs; |
blueswir1 | a38131b | 2008-12-05 17:56:40 +0000 | [diff] [blame] | 1380 | uint64_t nb_sectors; |
aliguori | f3d54fc | 2008-11-25 21:50:24 +0000 | [diff] [blame] | 1381 | |
| 1382 | /* if a geometry hint is available, use it */ |
| 1383 | bdrv_get_geometry(bs, &nb_sectors); |
| 1384 | bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); |
| 1385 | translation = bdrv_get_translation_hint(bs); |
| 1386 | if (cylinders != 0) { |
| 1387 | *pcyls = cylinders; |
| 1388 | *pheads = heads; |
| 1389 | *psecs = secs; |
| 1390 | } else { |
| 1391 | if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { |
| 1392 | if (heads > 16) { |
| 1393 | /* if heads > 16, it means that a BIOS LBA |
| 1394 | translation was active, so the default |
| 1395 | hardware geometry is OK */ |
| 1396 | lba_detected = 1; |
| 1397 | goto default_geometry; |
| 1398 | } else { |
| 1399 | *pcyls = cylinders; |
| 1400 | *pheads = heads; |
| 1401 | *psecs = secs; |
| 1402 | /* disable any translation to be in sync with |
| 1403 | the logical geometry */ |
| 1404 | if (translation == BIOS_ATA_TRANSLATION_AUTO) { |
| 1405 | bdrv_set_translation_hint(bs, |
| 1406 | BIOS_ATA_TRANSLATION_NONE); |
| 1407 | } |
| 1408 | } |
| 1409 | } else { |
| 1410 | default_geometry: |
| 1411 | /* if no geometry, use a standard physical disk geometry */ |
| 1412 | cylinders = nb_sectors / (16 * 63); |
| 1413 | |
| 1414 | if (cylinders > 16383) |
| 1415 | cylinders = 16383; |
| 1416 | else if (cylinders < 2) |
| 1417 | cylinders = 2; |
| 1418 | *pcyls = cylinders; |
| 1419 | *pheads = 16; |
| 1420 | *psecs = 63; |
| 1421 | if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { |
| 1422 | if ((*pcyls * *pheads) <= 131072) { |
| 1423 | bdrv_set_translation_hint(bs, |
| 1424 | BIOS_ATA_TRANSLATION_LARGE); |
| 1425 | } else { |
| 1426 | bdrv_set_translation_hint(bs, |
| 1427 | BIOS_ATA_TRANSLATION_LBA); |
| 1428 | } |
| 1429 | } |
| 1430 | } |
| 1431 | bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); |
| 1432 | } |
| 1433 | } |
| 1434 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1435 | void bdrv_set_geometry_hint(BlockDriverState *bs, |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1436 | int cyls, int heads, int secs) |
| 1437 | { |
| 1438 | bs->cyls = cyls; |
| 1439 | bs->heads = heads; |
| 1440 | bs->secs = secs; |
| 1441 | } |
| 1442 | |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 1443 | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
| 1444 | { |
| 1445 | bs->translation = translation; |
| 1446 | } |
| 1447 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1448 | void bdrv_get_geometry_hint(BlockDriverState *bs, |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1449 | int *pcyls, int *pheads, int *psecs) |
| 1450 | { |
| 1451 | *pcyls = bs->cyls; |
| 1452 | *pheads = bs->heads; |
| 1453 | *psecs = bs->secs; |
| 1454 | } |
| 1455 | |
Blue Swirl | 5bbdbb4 | 2011-02-12 20:43:32 +0000 | [diff] [blame] | 1456 | /* Recognize floppy formats */ |
| 1457 | typedef struct FDFormat { |
| 1458 | FDriveType drive; |
| 1459 | uint8_t last_sect; |
| 1460 | uint8_t max_track; |
| 1461 | uint8_t max_head; |
| 1462 | } FDFormat; |
| 1463 | |
| 1464 | static const FDFormat fd_formats[] = { |
| 1465 | /* First entry is default format */ |
| 1466 | /* 1.44 MB 3"1/2 floppy disks */ |
| 1467 | { FDRIVE_DRV_144, 18, 80, 1, }, |
| 1468 | { FDRIVE_DRV_144, 20, 80, 1, }, |
| 1469 | { FDRIVE_DRV_144, 21, 80, 1, }, |
| 1470 | { FDRIVE_DRV_144, 21, 82, 1, }, |
| 1471 | { FDRIVE_DRV_144, 21, 83, 1, }, |
| 1472 | { FDRIVE_DRV_144, 22, 80, 1, }, |
| 1473 | { FDRIVE_DRV_144, 23, 80, 1, }, |
| 1474 | { FDRIVE_DRV_144, 24, 80, 1, }, |
| 1475 | /* 2.88 MB 3"1/2 floppy disks */ |
| 1476 | { FDRIVE_DRV_288, 36, 80, 1, }, |
| 1477 | { FDRIVE_DRV_288, 39, 80, 1, }, |
| 1478 | { FDRIVE_DRV_288, 40, 80, 1, }, |
| 1479 | { FDRIVE_DRV_288, 44, 80, 1, }, |
| 1480 | { FDRIVE_DRV_288, 48, 80, 1, }, |
| 1481 | /* 720 kB 3"1/2 floppy disks */ |
| 1482 | { FDRIVE_DRV_144, 9, 80, 1, }, |
| 1483 | { FDRIVE_DRV_144, 10, 80, 1, }, |
| 1484 | { FDRIVE_DRV_144, 10, 82, 1, }, |
| 1485 | { FDRIVE_DRV_144, 10, 83, 1, }, |
| 1486 | { FDRIVE_DRV_144, 13, 80, 1, }, |
| 1487 | { FDRIVE_DRV_144, 14, 80, 1, }, |
| 1488 | /* 1.2 MB 5"1/4 floppy disks */ |
| 1489 | { FDRIVE_DRV_120, 15, 80, 1, }, |
| 1490 | { FDRIVE_DRV_120, 18, 80, 1, }, |
| 1491 | { FDRIVE_DRV_120, 18, 82, 1, }, |
| 1492 | { FDRIVE_DRV_120, 18, 83, 1, }, |
| 1493 | { FDRIVE_DRV_120, 20, 80, 1, }, |
| 1494 | /* 720 kB 5"1/4 floppy disks */ |
| 1495 | { FDRIVE_DRV_120, 9, 80, 1, }, |
| 1496 | { FDRIVE_DRV_120, 11, 80, 1, }, |
| 1497 | /* 360 kB 5"1/4 floppy disks */ |
| 1498 | { FDRIVE_DRV_120, 9, 40, 1, }, |
| 1499 | { FDRIVE_DRV_120, 9, 40, 0, }, |
| 1500 | { FDRIVE_DRV_120, 10, 41, 1, }, |
| 1501 | { FDRIVE_DRV_120, 10, 42, 1, }, |
| 1502 | /* 320 kB 5"1/4 floppy disks */ |
| 1503 | { FDRIVE_DRV_120, 8, 40, 1, }, |
| 1504 | { FDRIVE_DRV_120, 8, 40, 0, }, |
| 1505 | /* 360 kB must match 5"1/4 better than 3"1/2... */ |
| 1506 | { FDRIVE_DRV_144, 9, 80, 0, }, |
| 1507 | /* end */ |
| 1508 | { FDRIVE_DRV_NONE, -1, -1, 0, }, |
| 1509 | }; |
| 1510 | |
| 1511 | void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads, |
| 1512 | int *max_track, int *last_sect, |
| 1513 | FDriveType drive_in, FDriveType *drive) |
| 1514 | { |
| 1515 | const FDFormat *parse; |
| 1516 | uint64_t nb_sectors, size; |
| 1517 | int i, first_match, match; |
| 1518 | |
| 1519 | bdrv_get_geometry_hint(bs, nb_heads, max_track, last_sect); |
| 1520 | if (*nb_heads != 0 && *max_track != 0 && *last_sect != 0) { |
| 1521 | /* User defined disk */ |
| 1522 | } else { |
| 1523 | bdrv_get_geometry(bs, &nb_sectors); |
| 1524 | match = -1; |
| 1525 | first_match = -1; |
| 1526 | for (i = 0; ; i++) { |
| 1527 | parse = &fd_formats[i]; |
| 1528 | if (parse->drive == FDRIVE_DRV_NONE) { |
| 1529 | break; |
| 1530 | } |
| 1531 | if (drive_in == parse->drive || |
| 1532 | drive_in == FDRIVE_DRV_NONE) { |
| 1533 | size = (parse->max_head + 1) * parse->max_track * |
| 1534 | parse->last_sect; |
| 1535 | if (nb_sectors == size) { |
| 1536 | match = i; |
| 1537 | break; |
| 1538 | } |
| 1539 | if (first_match == -1) { |
| 1540 | first_match = i; |
| 1541 | } |
| 1542 | } |
| 1543 | } |
| 1544 | if (match == -1) { |
| 1545 | if (first_match == -1) { |
| 1546 | match = 1; |
| 1547 | } else { |
| 1548 | match = first_match; |
| 1549 | } |
| 1550 | parse = &fd_formats[match]; |
| 1551 | } |
| 1552 | *nb_heads = parse->max_head + 1; |
| 1553 | *max_track = parse->max_track; |
| 1554 | *last_sect = parse->last_sect; |
| 1555 | *drive = parse->drive; |
| 1556 | } |
| 1557 | } |
| 1558 | |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 1559 | int bdrv_get_translation_hint(BlockDriverState *bs) |
| 1560 | { |
| 1561 | return bs->translation; |
| 1562 | } |
| 1563 | |
Markus Armbruster | abd7f68 | 2010-06-02 18:55:17 +0200 | [diff] [blame] | 1564 | void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error, |
| 1565 | BlockErrorAction on_write_error) |
| 1566 | { |
| 1567 | bs->on_read_error = on_read_error; |
| 1568 | bs->on_write_error = on_write_error; |
| 1569 | } |
| 1570 | |
| 1571 | BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read) |
| 1572 | { |
| 1573 | return is_read ? bs->on_read_error : bs->on_write_error; |
| 1574 | } |
| 1575 | |
Markus Armbruster | 7d0d695 | 2010-06-25 13:42:14 +0200 | [diff] [blame] | 1576 | void bdrv_set_removable(BlockDriverState *bs, int removable) |
| 1577 | { |
| 1578 | bs->removable = removable; |
| 1579 | if (removable && bs == bs_snapshots) { |
| 1580 | bs_snapshots = NULL; |
| 1581 | } |
| 1582 | } |
| 1583 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1584 | int bdrv_is_removable(BlockDriverState *bs) |
| 1585 | { |
| 1586 | return bs->removable; |
| 1587 | } |
| 1588 | |
| 1589 | int bdrv_is_read_only(BlockDriverState *bs) |
| 1590 | { |
| 1591 | return bs->read_only; |
| 1592 | } |
| 1593 | |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 1594 | int bdrv_is_sg(BlockDriverState *bs) |
| 1595 | { |
| 1596 | return bs->sg; |
| 1597 | } |
| 1598 | |
Christoph Hellwig | e900a7b | 2009-09-04 19:01:15 +0200 | [diff] [blame] | 1599 | int bdrv_enable_write_cache(BlockDriverState *bs) |
| 1600 | { |
| 1601 | return bs->enable_write_cache; |
| 1602 | } |
| 1603 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1604 | /* XXX: no longer used */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1605 | void bdrv_set_change_cb(BlockDriverState *bs, |
Christoph Hellwig | db97ee6 | 2011-01-24 13:32:41 +0100 | [diff] [blame] | 1606 | void (*change_cb)(void *opaque, int reason), |
| 1607 | void *opaque) |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1608 | { |
| 1609 | bs->change_cb = change_cb; |
| 1610 | bs->change_opaque = opaque; |
| 1611 | } |
| 1612 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1613 | int bdrv_is_encrypted(BlockDriverState *bs) |
| 1614 | { |
| 1615 | if (bs->backing_hd && bs->backing_hd->encrypted) |
| 1616 | return 1; |
| 1617 | return bs->encrypted; |
| 1618 | } |
| 1619 | |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 1620 | int bdrv_key_required(BlockDriverState *bs) |
| 1621 | { |
| 1622 | BlockDriverState *backing_hd = bs->backing_hd; |
| 1623 | |
| 1624 | if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) |
| 1625 | return 1; |
| 1626 | return (bs->encrypted && !bs->valid_key); |
| 1627 | } |
| 1628 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1629 | int bdrv_set_key(BlockDriverState *bs, const char *key) |
| 1630 | { |
| 1631 | int ret; |
| 1632 | if (bs->backing_hd && bs->backing_hd->encrypted) { |
| 1633 | ret = bdrv_set_key(bs->backing_hd, key); |
| 1634 | if (ret < 0) |
| 1635 | return ret; |
| 1636 | if (!bs->encrypted) |
| 1637 | return 0; |
| 1638 | } |
Shahar Havivi | fd04a2a | 2010-03-06 00:26:13 +0200 | [diff] [blame] | 1639 | if (!bs->encrypted) { |
| 1640 | return -EINVAL; |
| 1641 | } else if (!bs->drv || !bs->drv->bdrv_set_key) { |
| 1642 | return -ENOMEDIUM; |
| 1643 | } |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 1644 | ret = bs->drv->bdrv_set_key(bs, key); |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 1645 | if (ret < 0) { |
| 1646 | bs->valid_key = 0; |
| 1647 | } else if (!bs->valid_key) { |
| 1648 | bs->valid_key = 1; |
| 1649 | /* call the change callback now, we skipped it on open */ |
| 1650 | bs->media_changed = 1; |
| 1651 | if (bs->change_cb) |
Christoph Hellwig | db97ee6 | 2011-01-24 13:32:41 +0100 | [diff] [blame] | 1652 | bs->change_cb(bs->change_opaque, CHANGE_MEDIA); |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 1653 | } |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 1654 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) |
| 1658 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1659 | if (!bs->drv) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1660 | buf[0] = '\0'; |
| 1661 | } else { |
| 1662 | pstrcpy(buf, buf_size, bs->drv->format_name); |
| 1663 | } |
| 1664 | } |
| 1665 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1666 | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1667 | void *opaque) |
| 1668 | { |
| 1669 | BlockDriver *drv; |
| 1670 | |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 1671 | QLIST_FOREACH(drv, &bdrv_drivers, list) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1672 | it(opaque, drv->format_name); |
| 1673 | } |
| 1674 | } |
| 1675 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1676 | BlockDriverState *bdrv_find(const char *name) |
| 1677 | { |
| 1678 | BlockDriverState *bs; |
| 1679 | |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 1680 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
| 1681 | if (!strcmp(name, bs->device_name)) { |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1682 | return bs; |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 1683 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1684 | } |
| 1685 | return NULL; |
| 1686 | } |
| 1687 | |
Markus Armbruster | 2f399b0 | 2010-06-02 18:55:20 +0200 | [diff] [blame] | 1688 | BlockDriverState *bdrv_next(BlockDriverState *bs) |
| 1689 | { |
| 1690 | if (!bs) { |
| 1691 | return QTAILQ_FIRST(&bdrv_states); |
| 1692 | } |
| 1693 | return QTAILQ_NEXT(bs, list); |
| 1694 | } |
| 1695 | |
aliguori | 51de976 | 2009-03-05 23:00:43 +0000 | [diff] [blame] | 1696 | void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 1697 | { |
| 1698 | BlockDriverState *bs; |
| 1699 | |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 1700 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
aliguori | 51de976 | 2009-03-05 23:00:43 +0000 | [diff] [blame] | 1701 | it(opaque, bs); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 1702 | } |
| 1703 | } |
| 1704 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1705 | const char *bdrv_get_device_name(BlockDriverState *bs) |
| 1706 | { |
| 1707 | return bs->device_name; |
| 1708 | } |
| 1709 | |
Kevin Wolf | 205ef79 | 2010-10-21 16:43:43 +0200 | [diff] [blame] | 1710 | int bdrv_flush(BlockDriverState *bs) |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 1711 | { |
Alexander Graf | 016f5cf | 2010-05-26 17:51:49 +0200 | [diff] [blame] | 1712 | if (bs->open_flags & BDRV_O_NO_FLUSH) { |
Kevin Wolf | 205ef79 | 2010-10-21 16:43:43 +0200 | [diff] [blame] | 1713 | return 0; |
Alexander Graf | 016f5cf | 2010-05-26 17:51:49 +0200 | [diff] [blame] | 1714 | } |
| 1715 | |
Kevin Wolf | e7a8a78 | 2011-07-15 16:05:00 +0200 | [diff] [blame] | 1716 | if (bs->drv && bdrv_has_async_flush(bs->drv) && qemu_in_coroutine()) { |
| 1717 | return bdrv_co_flush_em(bs); |
| 1718 | } |
| 1719 | |
Kevin Wolf | 205ef79 | 2010-10-21 16:43:43 +0200 | [diff] [blame] | 1720 | if (bs->drv && bs->drv->bdrv_flush) { |
| 1721 | return bs->drv->bdrv_flush(bs); |
| 1722 | } |
| 1723 | |
| 1724 | /* |
| 1725 | * Some block drivers always operate in either writethrough or unsafe mode |
| 1726 | * and don't support bdrv_flush therefore. Usually qemu doesn't know how |
| 1727 | * the server works (because the behaviour is hardcoded or depends on |
| 1728 | * server-side configuration), so we can't ensure that everything is safe |
| 1729 | * on disk. Returning an error doesn't work because that would break guests |
| 1730 | * even if the server operates in writethrough mode. |
| 1731 | * |
| 1732 | * Let's hope the user knows what he's doing. |
| 1733 | */ |
| 1734 | return 0; |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
aliguori | c6ca28d | 2008-10-06 13:55:43 +0000 | [diff] [blame] | 1737 | void bdrv_flush_all(void) |
| 1738 | { |
| 1739 | BlockDriverState *bs; |
| 1740 | |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 1741 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
| 1742 | if (bs->drv && !bdrv_is_read_only(bs) && |
| 1743 | (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) { |
aliguori | c6ca28d | 2008-10-06 13:55:43 +0000 | [diff] [blame] | 1744 | bdrv_flush(bs); |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 1745 | } |
| 1746 | } |
aliguori | c6ca28d | 2008-10-06 13:55:43 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
Kevin Wolf | f2feebb | 2010-04-14 17:30:35 +0200 | [diff] [blame] | 1749 | int bdrv_has_zero_init(BlockDriverState *bs) |
| 1750 | { |
| 1751 | assert(bs->drv); |
| 1752 | |
Kevin Wolf | 336c1c1 | 2010-07-28 11:26:29 +0200 | [diff] [blame] | 1753 | if (bs->drv->bdrv_has_zero_init) { |
| 1754 | return bs->drv->bdrv_has_zero_init(bs); |
Kevin Wolf | f2feebb | 2010-04-14 17:30:35 +0200 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | return 1; |
| 1758 | } |
| 1759 | |
Christoph Hellwig | bb8bf76 | 2010-12-16 19:36:31 +0100 | [diff] [blame] | 1760 | int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) |
| 1761 | { |
| 1762 | if (!bs->drv) { |
| 1763 | return -ENOMEDIUM; |
| 1764 | } |
| 1765 | if (!bs->drv->bdrv_discard) { |
| 1766 | return 0; |
| 1767 | } |
| 1768 | return bs->drv->bdrv_discard(bs, sector_num, nb_sectors); |
| 1769 | } |
| 1770 | |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1771 | /* |
| 1772 | * Returns true iff the specified sector is present in the disk image. Drivers |
| 1773 | * not implementing the functionality are assumed to not support backing files, |
| 1774 | * hence all their sectors are reported as allocated. |
| 1775 | * |
| 1776 | * 'pnum' is set to the number of sectors (including and immediately following |
| 1777 | * the specified sector) that are known to be in the same |
| 1778 | * allocated/unallocated state. |
| 1779 | * |
| 1780 | * 'nb_sectors' is the max value 'pnum' should be set to. |
| 1781 | */ |
| 1782 | int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, |
| 1783 | int *pnum) |
| 1784 | { |
| 1785 | int64_t n; |
| 1786 | if (!bs->drv->bdrv_is_allocated) { |
| 1787 | if (sector_num >= bs->total_sectors) { |
| 1788 | *pnum = 0; |
| 1789 | return 0; |
| 1790 | } |
| 1791 | n = bs->total_sectors - sector_num; |
| 1792 | *pnum = (n < nb_sectors) ? (n) : (nb_sectors); |
| 1793 | return 1; |
| 1794 | } |
| 1795 | return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); |
| 1796 | } |
| 1797 | |
Luiz Capitulino | 2582bfe | 2010-02-03 12:41:01 -0200 | [diff] [blame] | 1798 | void bdrv_mon_event(const BlockDriverState *bdrv, |
| 1799 | BlockMonEventAction action, int is_read) |
| 1800 | { |
| 1801 | QObject *data; |
| 1802 | const char *action_str; |
| 1803 | |
| 1804 | switch (action) { |
| 1805 | case BDRV_ACTION_REPORT: |
| 1806 | action_str = "report"; |
| 1807 | break; |
| 1808 | case BDRV_ACTION_IGNORE: |
| 1809 | action_str = "ignore"; |
| 1810 | break; |
| 1811 | case BDRV_ACTION_STOP: |
| 1812 | action_str = "stop"; |
| 1813 | break; |
| 1814 | default: |
| 1815 | abort(); |
| 1816 | } |
| 1817 | |
| 1818 | data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", |
| 1819 | bdrv->device_name, |
| 1820 | action_str, |
| 1821 | is_read ? "read" : "write"); |
| 1822 | monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data); |
| 1823 | |
| 1824 | qobject_decref(data); |
| 1825 | } |
| 1826 | |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1827 | static void bdrv_print_dict(QObject *obj, void *opaque) |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1828 | { |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1829 | QDict *bs_dict; |
| 1830 | Monitor *mon = opaque; |
| 1831 | |
| 1832 | bs_dict = qobject_to_qdict(obj); |
| 1833 | |
Markus Armbruster | d8aeeb3 | 2011-05-16 15:04:55 +0200 | [diff] [blame] | 1834 | monitor_printf(mon, "%s: removable=%d", |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1835 | qdict_get_str(bs_dict, "device"), |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1836 | qdict_get_bool(bs_dict, "removable")); |
| 1837 | |
| 1838 | if (qdict_get_bool(bs_dict, "removable")) { |
| 1839 | monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked")); |
| 1840 | } |
| 1841 | |
| 1842 | if (qdict_haskey(bs_dict, "inserted")) { |
| 1843 | QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted")); |
| 1844 | |
| 1845 | monitor_printf(mon, " file="); |
| 1846 | monitor_print_filename(mon, qdict_get_str(qdict, "file")); |
| 1847 | if (qdict_haskey(qdict, "backing_file")) { |
| 1848 | monitor_printf(mon, " backing_file="); |
| 1849 | monitor_print_filename(mon, qdict_get_str(qdict, "backing_file")); |
| 1850 | } |
| 1851 | monitor_printf(mon, " ro=%d drv=%s encrypted=%d", |
| 1852 | qdict_get_bool(qdict, "ro"), |
| 1853 | qdict_get_str(qdict, "drv"), |
| 1854 | qdict_get_bool(qdict, "encrypted")); |
| 1855 | } else { |
| 1856 | monitor_printf(mon, " [not inserted]"); |
| 1857 | } |
| 1858 | |
| 1859 | monitor_printf(mon, "\n"); |
| 1860 | } |
| 1861 | |
| 1862 | void bdrv_info_print(Monitor *mon, const QObject *data) |
| 1863 | { |
| 1864 | qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon); |
| 1865 | } |
| 1866 | |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1867 | void bdrv_info(Monitor *mon, QObject **ret_data) |
| 1868 | { |
| 1869 | QList *bs_list; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1870 | BlockDriverState *bs; |
| 1871 | |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1872 | bs_list = qlist_new(); |
| 1873 | |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 1874 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1875 | QObject *bs_obj; |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1876 | |
Markus Armbruster | d8aeeb3 | 2011-05-16 15:04:55 +0200 | [diff] [blame] | 1877 | bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', " |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1878 | "'removable': %i, 'locked': %i }", |
Markus Armbruster | d8aeeb3 | 2011-05-16 15:04:55 +0200 | [diff] [blame] | 1879 | bs->device_name, bs->removable, |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1880 | bs->locked); |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1881 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1882 | if (bs->drv) { |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1883 | QObject *obj; |
| 1884 | QDict *bs_dict = qobject_to_qdict(bs_obj); |
| 1885 | |
| 1886 | obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, " |
| 1887 | "'encrypted': %i }", |
| 1888 | bs->filename, bs->read_only, |
| 1889 | bs->drv->format_name, |
| 1890 | bdrv_is_encrypted(bs)); |
ths | fef3074 | 2006-12-22 14:11:32 +0000 | [diff] [blame] | 1891 | if (bs->backing_file[0] != '\0') { |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1892 | QDict *qdict = qobject_to_qdict(obj); |
| 1893 | qdict_put(qdict, "backing_file", |
| 1894 | qstring_from_str(bs->backing_file)); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1895 | } |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1896 | |
| 1897 | qdict_put_obj(bs_dict, "inserted", obj); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1898 | } |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1899 | qlist_append_obj(bs_list, bs_obj); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1900 | } |
Luiz Capitulino | d15e546 | 2009-12-10 17:16:06 -0200 | [diff] [blame] | 1901 | |
| 1902 | *ret_data = QOBJECT(bs_list); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1903 | } |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1904 | |
Luiz Capitulino | 218a536 | 2009-12-10 17:16:07 -0200 | [diff] [blame] | 1905 | static void bdrv_stats_iter(QObject *data, void *opaque) |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1906 | { |
Luiz Capitulino | 218a536 | 2009-12-10 17:16:07 -0200 | [diff] [blame] | 1907 | QDict *qdict; |
| 1908 | Monitor *mon = opaque; |
| 1909 | |
| 1910 | qdict = qobject_to_qdict(data); |
| 1911 | monitor_printf(mon, "%s:", qdict_get_str(qdict, "device")); |
| 1912 | |
| 1913 | qdict = qobject_to_qdict(qdict_get(qdict, "stats")); |
| 1914 | monitor_printf(mon, " rd_bytes=%" PRId64 |
| 1915 | " wr_bytes=%" PRId64 |
| 1916 | " rd_operations=%" PRId64 |
| 1917 | " wr_operations=%" PRId64 |
Christoph Hellwig | e8045d6 | 2011-08-22 00:25:58 +0200 | [diff] [blame^] | 1918 | " flush_operations=%" PRId64 |
Luiz Capitulino | 218a536 | 2009-12-10 17:16:07 -0200 | [diff] [blame] | 1919 | "\n", |
| 1920 | qdict_get_int(qdict, "rd_bytes"), |
| 1921 | qdict_get_int(qdict, "wr_bytes"), |
| 1922 | qdict_get_int(qdict, "rd_operations"), |
Christoph Hellwig | e8045d6 | 2011-08-22 00:25:58 +0200 | [diff] [blame^] | 1923 | qdict_get_int(qdict, "wr_operations"), |
| 1924 | qdict_get_int(qdict, "flush_operations")); |
Luiz Capitulino | 218a536 | 2009-12-10 17:16:07 -0200 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | void bdrv_stats_print(Monitor *mon, const QObject *data) |
| 1928 | { |
| 1929 | qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon); |
| 1930 | } |
| 1931 | |
Kevin Wolf | 294cc35 | 2010-04-28 14:34:01 +0200 | [diff] [blame] | 1932 | static QObject* bdrv_info_stats_bs(BlockDriverState *bs) |
| 1933 | { |
| 1934 | QObject *res; |
| 1935 | QDict *dict; |
| 1936 | |
| 1937 | res = qobject_from_jsonf("{ 'stats': {" |
| 1938 | "'rd_bytes': %" PRId64 "," |
| 1939 | "'wr_bytes': %" PRId64 "," |
| 1940 | "'rd_operations': %" PRId64 "," |
| 1941 | "'wr_operations': %" PRId64 "," |
Christoph Hellwig | e8045d6 | 2011-08-22 00:25:58 +0200 | [diff] [blame^] | 1942 | "'wr_highest_offset': %" PRId64 "," |
| 1943 | "'flush_operations': %" PRId64 |
Kevin Wolf | 294cc35 | 2010-04-28 14:34:01 +0200 | [diff] [blame] | 1944 | "} }", |
Christoph Hellwig | e8045d6 | 2011-08-22 00:25:58 +0200 | [diff] [blame^] | 1945 | bs->rd_bytes, |
| 1946 | bs->wr_bytes, |
| 1947 | bs->rd_ops, |
| 1948 | bs->wr_ops, |
Blue Swirl | 5ffbbc6 | 2010-06-14 18:55:33 +0000 | [diff] [blame] | 1949 | bs->wr_highest_sector * |
Christoph Hellwig | e8045d6 | 2011-08-22 00:25:58 +0200 | [diff] [blame^] | 1950 | (uint64_t)BDRV_SECTOR_SIZE, |
| 1951 | bs->flush_ops); |
Kevin Wolf | 294cc35 | 2010-04-28 14:34:01 +0200 | [diff] [blame] | 1952 | dict = qobject_to_qdict(res); |
| 1953 | |
| 1954 | if (*bs->device_name) { |
| 1955 | qdict_put(dict, "device", qstring_from_str(bs->device_name)); |
| 1956 | } |
| 1957 | |
| 1958 | if (bs->file) { |
| 1959 | QObject *parent = bdrv_info_stats_bs(bs->file); |
| 1960 | qdict_put_obj(dict, "parent", parent); |
| 1961 | } |
| 1962 | |
| 1963 | return res; |
| 1964 | } |
| 1965 | |
Luiz Capitulino | 218a536 | 2009-12-10 17:16:07 -0200 | [diff] [blame] | 1966 | void bdrv_info_stats(Monitor *mon, QObject **ret_data) |
| 1967 | { |
| 1968 | QObject *obj; |
| 1969 | QList *devices; |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1970 | BlockDriverState *bs; |
| 1971 | |
Luiz Capitulino | 218a536 | 2009-12-10 17:16:07 -0200 | [diff] [blame] | 1972 | devices = qlist_new(); |
| 1973 | |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 1974 | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
Kevin Wolf | 294cc35 | 2010-04-28 14:34:01 +0200 | [diff] [blame] | 1975 | obj = bdrv_info_stats_bs(bs); |
Luiz Capitulino | 218a536 | 2009-12-10 17:16:07 -0200 | [diff] [blame] | 1976 | qlist_append_obj(devices, obj); |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1977 | } |
Luiz Capitulino | 218a536 | 2009-12-10 17:16:07 -0200 | [diff] [blame] | 1978 | |
| 1979 | *ret_data = QOBJECT(devices); |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1980 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1981 | |
aliguori | 045df33 | 2009-03-05 23:00:48 +0000 | [diff] [blame] | 1982 | const char *bdrv_get_encrypted_filename(BlockDriverState *bs) |
| 1983 | { |
| 1984 | if (bs->backing_hd && bs->backing_hd->encrypted) |
| 1985 | return bs->backing_file; |
| 1986 | else if (bs->encrypted) |
| 1987 | return bs->filename; |
| 1988 | else |
| 1989 | return NULL; |
| 1990 | } |
| 1991 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1992 | void bdrv_get_backing_filename(BlockDriverState *bs, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1993 | char *filename, int filename_size) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1994 | { |
Kevin Wolf | b783e40 | 2010-01-12 12:55:16 +0100 | [diff] [blame] | 1995 | if (!bs->backing_file) { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1996 | pstrcpy(filename, filename_size, ""); |
| 1997 | } else { |
| 1998 | pstrcpy(filename, filename_size, bs->backing_file); |
| 1999 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2002 | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2003 | const uint8_t *buf, int nb_sectors) |
| 2004 | { |
| 2005 | BlockDriver *drv = bs->drv; |
| 2006 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 2007 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2008 | if (!drv->bdrv_write_compressed) |
| 2009 | return -ENOTSUP; |
Kevin Wolf | fbb7b4e | 2009-05-08 14:47:24 +0200 | [diff] [blame] | 2010 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 2011 | return -EIO; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2012 | |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 2013 | if (bs->dirty_bitmap) { |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 2014 | set_dirty_bitmap(bs, sector_num, nb_sectors, 1); |
| 2015 | } |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2016 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2017 | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); |
| 2018 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2019 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2020 | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
| 2021 | { |
| 2022 | BlockDriver *drv = bs->drv; |
| 2023 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 2024 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2025 | if (!drv->bdrv_get_info) |
| 2026 | return -ENOTSUP; |
| 2027 | memset(bdi, 0, sizeof(*bdi)); |
| 2028 | return drv->bdrv_get_info(bs, bdi); |
| 2029 | } |
| 2030 | |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 2031 | int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, |
| 2032 | int64_t pos, int size) |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 2033 | { |
| 2034 | BlockDriver *drv = bs->drv; |
| 2035 | if (!drv) |
| 2036 | return -ENOMEDIUM; |
MORITA Kazutaka | 7cdb1f6 | 2010-05-28 11:44:58 +0900 | [diff] [blame] | 2037 | if (drv->bdrv_save_vmstate) |
| 2038 | return drv->bdrv_save_vmstate(bs, buf, pos, size); |
| 2039 | if (bs->file) |
| 2040 | return bdrv_save_vmstate(bs->file, buf, pos, size); |
| 2041 | return -ENOTSUP; |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 2044 | int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, |
| 2045 | int64_t pos, int size) |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 2046 | { |
| 2047 | BlockDriver *drv = bs->drv; |
| 2048 | if (!drv) |
| 2049 | return -ENOMEDIUM; |
MORITA Kazutaka | 7cdb1f6 | 2010-05-28 11:44:58 +0900 | [diff] [blame] | 2050 | if (drv->bdrv_load_vmstate) |
| 2051 | return drv->bdrv_load_vmstate(bs, buf, pos, size); |
| 2052 | if (bs->file) |
| 2053 | return bdrv_load_vmstate(bs->file, buf, pos, size); |
| 2054 | return -ENOTSUP; |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 2057 | void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) |
| 2058 | { |
| 2059 | BlockDriver *drv = bs->drv; |
| 2060 | |
| 2061 | if (!drv || !drv->bdrv_debug_event) { |
| 2062 | return; |
| 2063 | } |
| 2064 | |
| 2065 | return drv->bdrv_debug_event(bs, event); |
| 2066 | |
| 2067 | } |
| 2068 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2069 | /**************************************************************/ |
| 2070 | /* handling of snapshots */ |
| 2071 | |
Miguel Di Ciurcio Filho | feeee5a | 2010-06-08 10:40:55 -0300 | [diff] [blame] | 2072 | int bdrv_can_snapshot(BlockDriverState *bs) |
| 2073 | { |
| 2074 | BlockDriver *drv = bs->drv; |
| 2075 | if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) { |
| 2076 | return 0; |
| 2077 | } |
| 2078 | |
| 2079 | if (!drv->bdrv_snapshot_create) { |
| 2080 | if (bs->file != NULL) { |
| 2081 | return bdrv_can_snapshot(bs->file); |
| 2082 | } |
| 2083 | return 0; |
| 2084 | } |
| 2085 | |
| 2086 | return 1; |
| 2087 | } |
| 2088 | |
Blue Swirl | 199630b | 2010-07-25 20:49:34 +0000 | [diff] [blame] | 2089 | int bdrv_is_snapshot(BlockDriverState *bs) |
| 2090 | { |
| 2091 | return !!(bs->open_flags & BDRV_O_SNAPSHOT); |
| 2092 | } |
| 2093 | |
Markus Armbruster | f9092b1 | 2010-06-25 10:33:39 +0200 | [diff] [blame] | 2094 | BlockDriverState *bdrv_snapshots(void) |
| 2095 | { |
| 2096 | BlockDriverState *bs; |
| 2097 | |
Markus Armbruster | 3ac906f | 2010-07-01 09:30:38 +0200 | [diff] [blame] | 2098 | if (bs_snapshots) { |
Markus Armbruster | f9092b1 | 2010-06-25 10:33:39 +0200 | [diff] [blame] | 2099 | return bs_snapshots; |
Markus Armbruster | 3ac906f | 2010-07-01 09:30:38 +0200 | [diff] [blame] | 2100 | } |
Markus Armbruster | f9092b1 | 2010-06-25 10:33:39 +0200 | [diff] [blame] | 2101 | |
| 2102 | bs = NULL; |
| 2103 | while ((bs = bdrv_next(bs))) { |
| 2104 | if (bdrv_can_snapshot(bs)) { |
Markus Armbruster | 3ac906f | 2010-07-01 09:30:38 +0200 | [diff] [blame] | 2105 | bs_snapshots = bs; |
| 2106 | return bs; |
Markus Armbruster | f9092b1 | 2010-06-25 10:33:39 +0200 | [diff] [blame] | 2107 | } |
| 2108 | } |
| 2109 | return NULL; |
Markus Armbruster | f9092b1 | 2010-06-25 10:33:39 +0200 | [diff] [blame] | 2110 | } |
| 2111 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2112 | int bdrv_snapshot_create(BlockDriverState *bs, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2113 | QEMUSnapshotInfo *sn_info) |
| 2114 | { |
| 2115 | BlockDriver *drv = bs->drv; |
| 2116 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 2117 | return -ENOMEDIUM; |
MORITA Kazutaka | 7cdb1f6 | 2010-05-28 11:44:58 +0900 | [diff] [blame] | 2118 | if (drv->bdrv_snapshot_create) |
| 2119 | return drv->bdrv_snapshot_create(bs, sn_info); |
| 2120 | if (bs->file) |
| 2121 | return bdrv_snapshot_create(bs->file, sn_info); |
| 2122 | return -ENOTSUP; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2125 | int bdrv_snapshot_goto(BlockDriverState *bs, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2126 | const char *snapshot_id) |
| 2127 | { |
| 2128 | BlockDriver *drv = bs->drv; |
MORITA Kazutaka | 7cdb1f6 | 2010-05-28 11:44:58 +0900 | [diff] [blame] | 2129 | int ret, open_ret; |
| 2130 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2131 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 2132 | return -ENOMEDIUM; |
MORITA Kazutaka | 7cdb1f6 | 2010-05-28 11:44:58 +0900 | [diff] [blame] | 2133 | if (drv->bdrv_snapshot_goto) |
| 2134 | return drv->bdrv_snapshot_goto(bs, snapshot_id); |
| 2135 | |
| 2136 | if (bs->file) { |
| 2137 | drv->bdrv_close(bs); |
| 2138 | ret = bdrv_snapshot_goto(bs->file, snapshot_id); |
| 2139 | open_ret = drv->bdrv_open(bs, bs->open_flags); |
| 2140 | if (open_ret < 0) { |
| 2141 | bdrv_delete(bs->file); |
| 2142 | bs->drv = NULL; |
| 2143 | return open_ret; |
| 2144 | } |
| 2145 | return ret; |
| 2146 | } |
| 2147 | |
| 2148 | return -ENOTSUP; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) |
| 2152 | { |
| 2153 | BlockDriver *drv = bs->drv; |
| 2154 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 2155 | return -ENOMEDIUM; |
MORITA Kazutaka | 7cdb1f6 | 2010-05-28 11:44:58 +0900 | [diff] [blame] | 2156 | if (drv->bdrv_snapshot_delete) |
| 2157 | return drv->bdrv_snapshot_delete(bs, snapshot_id); |
| 2158 | if (bs->file) |
| 2159 | return bdrv_snapshot_delete(bs->file, snapshot_id); |
| 2160 | return -ENOTSUP; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2163 | int bdrv_snapshot_list(BlockDriverState *bs, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2164 | QEMUSnapshotInfo **psn_info) |
| 2165 | { |
| 2166 | BlockDriver *drv = bs->drv; |
| 2167 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 2168 | return -ENOMEDIUM; |
MORITA Kazutaka | 7cdb1f6 | 2010-05-28 11:44:58 +0900 | [diff] [blame] | 2169 | if (drv->bdrv_snapshot_list) |
| 2170 | return drv->bdrv_snapshot_list(bs, psn_info); |
| 2171 | if (bs->file) |
| 2172 | return bdrv_snapshot_list(bs->file, psn_info); |
| 2173 | return -ENOTSUP; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2174 | } |
| 2175 | |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2176 | int bdrv_snapshot_load_tmp(BlockDriverState *bs, |
| 2177 | const char *snapshot_name) |
| 2178 | { |
| 2179 | BlockDriver *drv = bs->drv; |
| 2180 | if (!drv) { |
| 2181 | return -ENOMEDIUM; |
| 2182 | } |
| 2183 | if (!bs->read_only) { |
| 2184 | return -EINVAL; |
| 2185 | } |
| 2186 | if (drv->bdrv_snapshot_load_tmp) { |
| 2187 | return drv->bdrv_snapshot_load_tmp(bs, snapshot_name); |
| 2188 | } |
| 2189 | return -ENOTSUP; |
| 2190 | } |
| 2191 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2192 | #define NB_SUFFIXES 4 |
| 2193 | |
| 2194 | char *get_human_readable_size(char *buf, int buf_size, int64_t size) |
| 2195 | { |
| 2196 | static const char suffixes[NB_SUFFIXES] = "KMGT"; |
| 2197 | int64_t base; |
| 2198 | int i; |
| 2199 | |
| 2200 | if (size <= 999) { |
| 2201 | snprintf(buf, buf_size, "%" PRId64, size); |
| 2202 | } else { |
| 2203 | base = 1024; |
| 2204 | for(i = 0; i < NB_SUFFIXES; i++) { |
| 2205 | if (size < (10 * base)) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2206 | snprintf(buf, buf_size, "%0.1f%c", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2207 | (double)size / base, |
| 2208 | suffixes[i]); |
| 2209 | break; |
| 2210 | } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2211 | snprintf(buf, buf_size, "%" PRId64 "%c", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2212 | ((size + (base >> 1)) / base), |
| 2213 | suffixes[i]); |
| 2214 | break; |
| 2215 | } |
| 2216 | base = base * 1024; |
| 2217 | } |
| 2218 | } |
| 2219 | return buf; |
| 2220 | } |
| 2221 | |
| 2222 | char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) |
| 2223 | { |
| 2224 | char buf1[128], date_buf[128], clock_buf[128]; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 2225 | #ifdef _WIN32 |
| 2226 | struct tm *ptm; |
| 2227 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2228 | struct tm tm; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 2229 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2230 | time_t ti; |
| 2231 | int64_t secs; |
| 2232 | |
| 2233 | if (!sn) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2234 | snprintf(buf, buf_size, |
| 2235 | "%-10s%-20s%7s%20s%15s", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2236 | "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); |
| 2237 | } else { |
| 2238 | ti = sn->date_sec; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 2239 | #ifdef _WIN32 |
| 2240 | ptm = localtime(&ti); |
| 2241 | strftime(date_buf, sizeof(date_buf), |
| 2242 | "%Y-%m-%d %H:%M:%S", ptm); |
| 2243 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2244 | localtime_r(&ti, &tm); |
| 2245 | strftime(date_buf, sizeof(date_buf), |
| 2246 | "%Y-%m-%d %H:%M:%S", &tm); |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 2247 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2248 | secs = sn->vm_clock_nsec / 1000000000; |
| 2249 | snprintf(clock_buf, sizeof(clock_buf), |
| 2250 | "%02d:%02d:%02d.%03d", |
| 2251 | (int)(secs / 3600), |
| 2252 | (int)((secs / 60) % 60), |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2253 | (int)(secs % 60), |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2254 | (int)((sn->vm_clock_nsec / 1000000) % 1000)); |
| 2255 | snprintf(buf, buf_size, |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2256 | "%-10s%-20s%7s%20s%15s", |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2257 | sn->id_str, sn->name, |
| 2258 | get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), |
| 2259 | date_buf, |
| 2260 | clock_buf); |
| 2261 | } |
| 2262 | return buf; |
| 2263 | } |
| 2264 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2265 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2266 | /**************************************************************/ |
| 2267 | /* async I/Os */ |
| 2268 | |
aliguori | 3b69e4b | 2009-01-22 16:59:24 +0000 | [diff] [blame] | 2269 | BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2270 | QEMUIOVector *qiov, int nb_sectors, |
aliguori | 3b69e4b | 2009-01-22 16:59:24 +0000 | [diff] [blame] | 2271 | BlockDriverCompletionFunc *cb, void *opaque) |
| 2272 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2273 | BlockDriver *drv = bs->drv; |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 2274 | BlockDriverAIOCB *ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2275 | |
Stefan Hajnoczi | bbf0a44 | 2010-10-05 14:28:53 +0100 | [diff] [blame] | 2276 | trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); |
| 2277 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 2278 | if (!drv) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2279 | return NULL; |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 2280 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 2281 | return NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2282 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2283 | ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors, |
| 2284 | cb, opaque); |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 2285 | |
| 2286 | if (ret) { |
Robert Wang | d62b5de | 2011-08-19 17:17:12 +0800 | [diff] [blame] | 2287 | /* Update stats even though technically transfer has not happened. */ |
| 2288 | bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; |
| 2289 | bs->rd_ops++; |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
Marcelo Tosatti | 4dcafbb | 2010-11-08 17:02:55 -0200 | [diff] [blame] | 2295 | typedef struct BlockCompleteData { |
| 2296 | BlockDriverCompletionFunc *cb; |
| 2297 | void *opaque; |
| 2298 | BlockDriverState *bs; |
| 2299 | int64_t sector_num; |
| 2300 | int nb_sectors; |
| 2301 | } BlockCompleteData; |
| 2302 | |
| 2303 | static void block_complete_cb(void *opaque, int ret) |
| 2304 | { |
| 2305 | BlockCompleteData *b = opaque; |
| 2306 | |
| 2307 | if (b->bs->dirty_bitmap) { |
| 2308 | set_dirty_bitmap(b->bs, b->sector_num, b->nb_sectors, 1); |
| 2309 | } |
| 2310 | b->cb(b->opaque, ret); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2311 | g_free(b); |
Marcelo Tosatti | 4dcafbb | 2010-11-08 17:02:55 -0200 | [diff] [blame] | 2312 | } |
| 2313 | |
| 2314 | static BlockCompleteData *blk_dirty_cb_alloc(BlockDriverState *bs, |
| 2315 | int64_t sector_num, |
| 2316 | int nb_sectors, |
| 2317 | BlockDriverCompletionFunc *cb, |
| 2318 | void *opaque) |
| 2319 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2320 | BlockCompleteData *blkdata = g_malloc0(sizeof(BlockCompleteData)); |
Marcelo Tosatti | 4dcafbb | 2010-11-08 17:02:55 -0200 | [diff] [blame] | 2321 | |
| 2322 | blkdata->bs = bs; |
| 2323 | blkdata->cb = cb; |
| 2324 | blkdata->opaque = opaque; |
| 2325 | blkdata->sector_num = sector_num; |
| 2326 | blkdata->nb_sectors = nb_sectors; |
| 2327 | |
| 2328 | return blkdata; |
| 2329 | } |
| 2330 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2331 | BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, |
| 2332 | QEMUIOVector *qiov, int nb_sectors, |
| 2333 | BlockDriverCompletionFunc *cb, void *opaque) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2334 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2335 | BlockDriver *drv = bs->drv; |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 2336 | BlockDriverAIOCB *ret; |
Marcelo Tosatti | 4dcafbb | 2010-11-08 17:02:55 -0200 | [diff] [blame] | 2337 | BlockCompleteData *blk_cb_data; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2338 | |
Stefan Hajnoczi | bbf0a44 | 2010-10-05 14:28:53 +0100 | [diff] [blame] | 2339 | trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); |
| 2340 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 2341 | if (!drv) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2342 | return NULL; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2343 | if (bs->read_only) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2344 | return NULL; |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 2345 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 2346 | return NULL; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2347 | |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 2348 | if (bs->dirty_bitmap) { |
Marcelo Tosatti | 4dcafbb | 2010-11-08 17:02:55 -0200 | [diff] [blame] | 2349 | blk_cb_data = blk_dirty_cb_alloc(bs, sector_num, nb_sectors, cb, |
| 2350 | opaque); |
| 2351 | cb = &block_complete_cb; |
| 2352 | opaque = blk_cb_data; |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 2353 | } |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2354 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2355 | ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors, |
| 2356 | cb, opaque); |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 2357 | |
| 2358 | if (ret) { |
Kevin Wolf | 294cc35 | 2010-04-28 14:34:01 +0200 | [diff] [blame] | 2359 | /* Update stats even though technically transfer has not happened. */ |
| 2360 | bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; |
| 2361 | bs->wr_ops ++; |
| 2362 | if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { |
| 2363 | bs->wr_highest_sector = sector_num + nb_sectors - 1; |
| 2364 | } |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
| 2367 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2370 | |
| 2371 | typedef struct MultiwriteCB { |
| 2372 | int error; |
| 2373 | int num_requests; |
| 2374 | int num_callbacks; |
| 2375 | struct { |
| 2376 | BlockDriverCompletionFunc *cb; |
| 2377 | void *opaque; |
| 2378 | QEMUIOVector *free_qiov; |
| 2379 | void *free_buf; |
| 2380 | } callbacks[]; |
| 2381 | } MultiwriteCB; |
| 2382 | |
| 2383 | static void multiwrite_user_cb(MultiwriteCB *mcb) |
| 2384 | { |
| 2385 | int i; |
| 2386 | |
| 2387 | for (i = 0; i < mcb->num_callbacks; i++) { |
| 2388 | mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); |
Stefan Hajnoczi | 1e1ea48 | 2010-04-21 20:35:45 +0100 | [diff] [blame] | 2389 | if (mcb->callbacks[i].free_qiov) { |
| 2390 | qemu_iovec_destroy(mcb->callbacks[i].free_qiov); |
| 2391 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2392 | g_free(mcb->callbacks[i].free_qiov); |
Herve Poussineau | f8a8324 | 2010-01-24 21:23:56 +0000 | [diff] [blame] | 2393 | qemu_vfree(mcb->callbacks[i].free_buf); |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2394 | } |
| 2395 | } |
| 2396 | |
| 2397 | static void multiwrite_cb(void *opaque, int ret) |
| 2398 | { |
| 2399 | MultiwriteCB *mcb = opaque; |
| 2400 | |
Stefan Hajnoczi | 6d519a5 | 2010-05-22 18:15:08 +0100 | [diff] [blame] | 2401 | trace_multiwrite_cb(mcb, ret); |
| 2402 | |
Kevin Wolf | cb6d3ca | 2010-04-01 22:48:44 +0200 | [diff] [blame] | 2403 | if (ret < 0 && !mcb->error) { |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2404 | mcb->error = ret; |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | mcb->num_requests--; |
| 2408 | if (mcb->num_requests == 0) { |
Kevin Wolf | de189a1 | 2010-07-01 16:08:51 +0200 | [diff] [blame] | 2409 | multiwrite_user_cb(mcb); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2410 | g_free(mcb); |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2411 | } |
| 2412 | } |
| 2413 | |
| 2414 | static int multiwrite_req_compare(const void *a, const void *b) |
| 2415 | { |
Christoph Hellwig | 77be436 | 2010-05-19 20:53:10 +0200 | [diff] [blame] | 2416 | const BlockRequest *req1 = a, *req2 = b; |
| 2417 | |
| 2418 | /* |
| 2419 | * Note that we can't simply subtract req2->sector from req1->sector |
| 2420 | * here as that could overflow the return value. |
| 2421 | */ |
| 2422 | if (req1->sector > req2->sector) { |
| 2423 | return 1; |
| 2424 | } else if (req1->sector < req2->sector) { |
| 2425 | return -1; |
| 2426 | } else { |
| 2427 | return 0; |
| 2428 | } |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | /* |
| 2432 | * Takes a bunch of requests and tries to merge them. Returns the number of |
| 2433 | * requests that remain after merging. |
| 2434 | */ |
| 2435 | static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, |
| 2436 | int num_reqs, MultiwriteCB *mcb) |
| 2437 | { |
| 2438 | int i, outidx; |
| 2439 | |
| 2440 | // Sort requests by start sector |
| 2441 | qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); |
| 2442 | |
| 2443 | // Check if adjacent requests touch the same clusters. If so, combine them, |
| 2444 | // filling up gaps with zero sectors. |
| 2445 | outidx = 0; |
| 2446 | for (i = 1; i < num_reqs; i++) { |
| 2447 | int merge = 0; |
| 2448 | int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; |
| 2449 | |
| 2450 | // This handles the cases that are valid for all block drivers, namely |
| 2451 | // exactly sequential writes and overlapping writes. |
| 2452 | if (reqs[i].sector <= oldreq_last) { |
| 2453 | merge = 1; |
| 2454 | } |
| 2455 | |
| 2456 | // The block driver may decide that it makes sense to combine requests |
| 2457 | // even if there is a gap of some sectors between them. In this case, |
| 2458 | // the gap is filled with zeros (therefore only applicable for yet |
| 2459 | // unused space in format like qcow2). |
| 2460 | if (!merge && bs->drv->bdrv_merge_requests) { |
| 2461 | merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]); |
| 2462 | } |
| 2463 | |
Christoph Hellwig | e2a305f | 2010-01-26 14:49:08 +0100 | [diff] [blame] | 2464 | if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { |
| 2465 | merge = 0; |
| 2466 | } |
| 2467 | |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2468 | if (merge) { |
| 2469 | size_t size; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2470 | QEMUIOVector *qiov = g_malloc0(sizeof(*qiov)); |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2471 | qemu_iovec_init(qiov, |
| 2472 | reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); |
| 2473 | |
| 2474 | // Add the first request to the merged one. If the requests are |
| 2475 | // overlapping, drop the last sectors of the first request. |
| 2476 | size = (reqs[i].sector - reqs[outidx].sector) << 9; |
| 2477 | qemu_iovec_concat(qiov, reqs[outidx].qiov, size); |
| 2478 | |
| 2479 | // We might need to add some zeros between the two requests |
| 2480 | if (reqs[i].sector > oldreq_last) { |
| 2481 | size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9; |
| 2482 | uint8_t *buf = qemu_blockalign(bs, zero_bytes); |
| 2483 | memset(buf, 0, zero_bytes); |
| 2484 | qemu_iovec_add(qiov, buf, zero_bytes); |
| 2485 | mcb->callbacks[i].free_buf = buf; |
| 2486 | } |
| 2487 | |
| 2488 | // Add the second request |
| 2489 | qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size); |
| 2490 | |
Kevin Wolf | cbf1dff | 2010-05-21 11:09:42 +0200 | [diff] [blame] | 2491 | reqs[outidx].nb_sectors = qiov->size >> 9; |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2492 | reqs[outidx].qiov = qiov; |
| 2493 | |
| 2494 | mcb->callbacks[i].free_qiov = reqs[outidx].qiov; |
| 2495 | } else { |
| 2496 | outidx++; |
| 2497 | reqs[outidx].sector = reqs[i].sector; |
| 2498 | reqs[outidx].nb_sectors = reqs[i].nb_sectors; |
| 2499 | reqs[outidx].qiov = reqs[i].qiov; |
| 2500 | } |
| 2501 | } |
| 2502 | |
| 2503 | return outidx + 1; |
| 2504 | } |
| 2505 | |
| 2506 | /* |
| 2507 | * Submit multiple AIO write requests at once. |
| 2508 | * |
| 2509 | * On success, the function returns 0 and all requests in the reqs array have |
| 2510 | * been submitted. In error case this function returns -1, and any of the |
| 2511 | * requests may or may not be submitted yet. In particular, this means that the |
| 2512 | * callback will be called for some of the requests, for others it won't. The |
| 2513 | * caller must check the error field of the BlockRequest to wait for the right |
| 2514 | * callbacks (if error != 0, no callback will be called). |
| 2515 | * |
| 2516 | * The implementation may modify the contents of the reqs array, e.g. to merge |
| 2517 | * requests. However, the fields opaque and error are left unmodified as they |
| 2518 | * are used to signal failure for a single request to the caller. |
| 2519 | */ |
| 2520 | int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) |
| 2521 | { |
| 2522 | BlockDriverAIOCB *acb; |
| 2523 | MultiwriteCB *mcb; |
| 2524 | int i; |
| 2525 | |
Ryan Harper | 301db7c | 2011-03-07 10:01:04 -0600 | [diff] [blame] | 2526 | /* don't submit writes if we don't have a medium */ |
| 2527 | if (bs->drv == NULL) { |
| 2528 | for (i = 0; i < num_reqs; i++) { |
| 2529 | reqs[i].error = -ENOMEDIUM; |
| 2530 | } |
| 2531 | return -1; |
| 2532 | } |
| 2533 | |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2534 | if (num_reqs == 0) { |
| 2535 | return 0; |
| 2536 | } |
| 2537 | |
| 2538 | // Create MultiwriteCB structure |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2539 | mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2540 | mcb->num_requests = 0; |
| 2541 | mcb->num_callbacks = num_reqs; |
| 2542 | |
| 2543 | for (i = 0; i < num_reqs; i++) { |
| 2544 | mcb->callbacks[i].cb = reqs[i].cb; |
| 2545 | mcb->callbacks[i].opaque = reqs[i].opaque; |
| 2546 | } |
| 2547 | |
| 2548 | // Check for mergable requests |
| 2549 | num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); |
| 2550 | |
Stefan Hajnoczi | 6d519a5 | 2010-05-22 18:15:08 +0100 | [diff] [blame] | 2551 | trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); |
| 2552 | |
Kevin Wolf | 453f9a1 | 2010-07-02 14:01:21 +0200 | [diff] [blame] | 2553 | /* |
| 2554 | * Run the aio requests. As soon as one request can't be submitted |
| 2555 | * successfully, fail all requests that are not yet submitted (we must |
| 2556 | * return failure for all requests anyway) |
| 2557 | * |
| 2558 | * num_requests cannot be set to the right value immediately: If |
| 2559 | * bdrv_aio_writev fails for some request, num_requests would be too high |
| 2560 | * and therefore multiwrite_cb() would never recognize the multiwrite |
| 2561 | * request as completed. We also cannot use the loop variable i to set it |
| 2562 | * when the first request fails because the callback may already have been |
| 2563 | * called for previously submitted requests. Thus, num_requests must be |
| 2564 | * incremented for each request that is submitted. |
| 2565 | * |
| 2566 | * The problem that callbacks may be called early also means that we need |
| 2567 | * to take care that num_requests doesn't become 0 before all requests are |
| 2568 | * submitted - multiwrite_cb() would consider the multiwrite request |
| 2569 | * completed. A dummy request that is "completed" by a manual call to |
| 2570 | * multiwrite_cb() takes care of this. |
| 2571 | */ |
| 2572 | mcb->num_requests = 1; |
| 2573 | |
Stefan Hajnoczi | 6d519a5 | 2010-05-22 18:15:08 +0100 | [diff] [blame] | 2574 | // Run the aio requests |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2575 | for (i = 0; i < num_reqs; i++) { |
Kevin Wolf | 453f9a1 | 2010-07-02 14:01:21 +0200 | [diff] [blame] | 2576 | mcb->num_requests++; |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2577 | acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, |
| 2578 | reqs[i].nb_sectors, multiwrite_cb, mcb); |
| 2579 | |
| 2580 | if (acb == NULL) { |
| 2581 | // We can only fail the whole thing if no request has been |
| 2582 | // submitted yet. Otherwise we'll wait for the submitted AIOs to |
| 2583 | // complete and report the error in the callback. |
Kevin Wolf | 453f9a1 | 2010-07-02 14:01:21 +0200 | [diff] [blame] | 2584 | if (i == 0) { |
Stefan Hajnoczi | 6d519a5 | 2010-05-22 18:15:08 +0100 | [diff] [blame] | 2585 | trace_bdrv_aio_multiwrite_earlyfail(mcb); |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2586 | goto fail; |
| 2587 | } else { |
Stefan Hajnoczi | 6d519a5 | 2010-05-22 18:15:08 +0100 | [diff] [blame] | 2588 | trace_bdrv_aio_multiwrite_latefail(mcb, i); |
Kevin Wolf | 7eb58a6 | 2010-04-06 18:24:07 +0200 | [diff] [blame] | 2589 | multiwrite_cb(mcb, -EIO); |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2590 | break; |
| 2591 | } |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2592 | } |
| 2593 | } |
| 2594 | |
Kevin Wolf | 453f9a1 | 2010-07-02 14:01:21 +0200 | [diff] [blame] | 2595 | /* Complete the dummy request */ |
| 2596 | multiwrite_cb(mcb, 0); |
| 2597 | |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2598 | return 0; |
| 2599 | |
| 2600 | fail: |
Kevin Wolf | 453f9a1 | 2010-07-02 14:01:21 +0200 | [diff] [blame] | 2601 | for (i = 0; i < mcb->num_callbacks; i++) { |
| 2602 | reqs[i].error = -EIO; |
| 2603 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2604 | g_free(mcb); |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 2605 | return -1; |
| 2606 | } |
| 2607 | |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 2608 | BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, |
| 2609 | BlockDriverCompletionFunc *cb, void *opaque) |
| 2610 | { |
| 2611 | BlockDriver *drv = bs->drv; |
| 2612 | |
Stefan Hajnoczi | a13aac0 | 2011-03-07 07:58:04 +0000 | [diff] [blame] | 2613 | trace_bdrv_aio_flush(bs, opaque); |
| 2614 | |
Christoph Hellwig | e8045d6 | 2011-08-22 00:25:58 +0200 | [diff] [blame^] | 2615 | bs->flush_ops++; |
| 2616 | |
Alexander Graf | 016f5cf | 2010-05-26 17:51:49 +0200 | [diff] [blame] | 2617 | if (bs->open_flags & BDRV_O_NO_FLUSH) { |
| 2618 | return bdrv_aio_noop_em(bs, cb, opaque); |
| 2619 | } |
| 2620 | |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 2621 | if (!drv) |
| 2622 | return NULL; |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 2623 | return drv->bdrv_aio_flush(bs, cb, opaque); |
| 2624 | } |
| 2625 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2626 | void bdrv_aio_cancel(BlockDriverAIOCB *acb) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2627 | { |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 2628 | acb->pool->cancel(acb); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2629 | } |
| 2630 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2631 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2632 | /**************************************************************/ |
| 2633 | /* async block device emulation */ |
| 2634 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 2635 | typedef struct BlockDriverAIOCBSync { |
| 2636 | BlockDriverAIOCB common; |
| 2637 | QEMUBH *bh; |
| 2638 | int ret; |
| 2639 | /* vector translation state */ |
| 2640 | QEMUIOVector *qiov; |
| 2641 | uint8_t *bounce; |
| 2642 | int is_write; |
| 2643 | } BlockDriverAIOCBSync; |
| 2644 | |
| 2645 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) |
| 2646 | { |
Kevin Wolf | b666d23 | 2010-05-05 11:44:39 +0200 | [diff] [blame] | 2647 | BlockDriverAIOCBSync *acb = |
| 2648 | container_of(blockacb, BlockDriverAIOCBSync, common); |
Dor Laor | 6a7ad29 | 2009-06-01 12:07:23 +0300 | [diff] [blame] | 2649 | qemu_bh_delete(acb->bh); |
Avi Kivity | 36afc45 | 2009-06-23 16:20:36 +0300 | [diff] [blame] | 2650 | acb->bh = NULL; |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 2651 | qemu_aio_release(acb); |
| 2652 | } |
| 2653 | |
| 2654 | static AIOPool bdrv_em_aio_pool = { |
| 2655 | .aiocb_size = sizeof(BlockDriverAIOCBSync), |
| 2656 | .cancel = bdrv_aio_cancel_em, |
| 2657 | }; |
| 2658 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2659 | static void bdrv_aio_bh_cb(void *opaque) |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 2660 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2661 | BlockDriverAIOCBSync *acb = opaque; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2662 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2663 | if (!acb->is_write) |
| 2664 | qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); |
aliguori | ceb42de | 2009-04-07 18:43:28 +0000 | [diff] [blame] | 2665 | qemu_vfree(acb->bounce); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2666 | acb->common.cb(acb->common.opaque, acb->ret); |
Dor Laor | 6a7ad29 | 2009-06-01 12:07:23 +0300 | [diff] [blame] | 2667 | qemu_bh_delete(acb->bh); |
Avi Kivity | 36afc45 | 2009-06-23 16:20:36 +0300 | [diff] [blame] | 2668 | acb->bh = NULL; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2669 | qemu_aio_release(acb); |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 2670 | } |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 2671 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2672 | static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, |
| 2673 | int64_t sector_num, |
| 2674 | QEMUIOVector *qiov, |
| 2675 | int nb_sectors, |
| 2676 | BlockDriverCompletionFunc *cb, |
| 2677 | void *opaque, |
| 2678 | int is_write) |
| 2679 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2680 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2681 | BlockDriverAIOCBSync *acb; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2682 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 2683 | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2684 | acb->is_write = is_write; |
| 2685 | acb->qiov = qiov; |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 2686 | acb->bounce = qemu_blockalign(bs, qiov->size); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2687 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2688 | if (!acb->bh) |
| 2689 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2690 | |
| 2691 | if (is_write) { |
| 2692 | qemu_iovec_to_buffer(acb->qiov, acb->bounce); |
| 2693 | acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors); |
| 2694 | } else { |
| 2695 | acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors); |
| 2696 | } |
| 2697 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2698 | qemu_bh_schedule(acb->bh); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2699 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2700 | return &acb->common; |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2703 | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, |
| 2704 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2705 | BlockDriverCompletionFunc *cb, void *opaque) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2706 | { |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2707 | 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] | 2708 | } |
| 2709 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2710 | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, |
| 2711 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
| 2712 | BlockDriverCompletionFunc *cb, void *opaque) |
| 2713 | { |
| 2714 | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); |
| 2715 | } |
| 2716 | |
Kevin Wolf | 6848542 | 2011-06-30 10:05:46 +0200 | [diff] [blame] | 2717 | |
| 2718 | typedef struct BlockDriverAIOCBCoroutine { |
| 2719 | BlockDriverAIOCB common; |
| 2720 | BlockRequest req; |
| 2721 | bool is_write; |
| 2722 | QEMUBH* bh; |
| 2723 | } BlockDriverAIOCBCoroutine; |
| 2724 | |
| 2725 | static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) |
| 2726 | { |
| 2727 | qemu_aio_flush(); |
| 2728 | } |
| 2729 | |
| 2730 | static AIOPool bdrv_em_co_aio_pool = { |
| 2731 | .aiocb_size = sizeof(BlockDriverAIOCBCoroutine), |
| 2732 | .cancel = bdrv_aio_co_cancel_em, |
| 2733 | }; |
| 2734 | |
| 2735 | static void bdrv_co_rw_bh(void *opaque) |
| 2736 | { |
| 2737 | BlockDriverAIOCBCoroutine *acb = opaque; |
| 2738 | |
| 2739 | acb->common.cb(acb->common.opaque, acb->req.error); |
| 2740 | qemu_bh_delete(acb->bh); |
| 2741 | qemu_aio_release(acb); |
| 2742 | } |
| 2743 | |
| 2744 | static void coroutine_fn bdrv_co_rw(void *opaque) |
| 2745 | { |
| 2746 | BlockDriverAIOCBCoroutine *acb = opaque; |
| 2747 | BlockDriverState *bs = acb->common.bs; |
| 2748 | |
| 2749 | if (!acb->is_write) { |
| 2750 | acb->req.error = bs->drv->bdrv_co_readv(bs, acb->req.sector, |
| 2751 | acb->req.nb_sectors, acb->req.qiov); |
| 2752 | } else { |
| 2753 | acb->req.error = bs->drv->bdrv_co_writev(bs, acb->req.sector, |
| 2754 | acb->req.nb_sectors, acb->req.qiov); |
| 2755 | } |
| 2756 | |
| 2757 | acb->bh = qemu_bh_new(bdrv_co_rw_bh, acb); |
| 2758 | qemu_bh_schedule(acb->bh); |
| 2759 | } |
| 2760 | |
| 2761 | static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, |
| 2762 | int64_t sector_num, |
| 2763 | QEMUIOVector *qiov, |
| 2764 | int nb_sectors, |
| 2765 | BlockDriverCompletionFunc *cb, |
| 2766 | void *opaque, |
| 2767 | bool is_write) |
| 2768 | { |
| 2769 | Coroutine *co; |
| 2770 | BlockDriverAIOCBCoroutine *acb; |
| 2771 | |
| 2772 | acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); |
| 2773 | acb->req.sector = sector_num; |
| 2774 | acb->req.nb_sectors = nb_sectors; |
| 2775 | acb->req.qiov = qiov; |
| 2776 | acb->is_write = is_write; |
| 2777 | |
| 2778 | co = qemu_coroutine_create(bdrv_co_rw); |
| 2779 | qemu_coroutine_enter(co, acb); |
| 2780 | |
| 2781 | return &acb->common; |
| 2782 | } |
| 2783 | |
| 2784 | static BlockDriverAIOCB *bdrv_co_aio_readv_em(BlockDriverState *bs, |
| 2785 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
| 2786 | BlockDriverCompletionFunc *cb, void *opaque) |
| 2787 | { |
| 2788 | return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, |
| 2789 | false); |
| 2790 | } |
| 2791 | |
| 2792 | static BlockDriverAIOCB *bdrv_co_aio_writev_em(BlockDriverState *bs, |
| 2793 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
| 2794 | BlockDriverCompletionFunc *cb, void *opaque) |
| 2795 | { |
| 2796 | return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, |
| 2797 | true); |
| 2798 | } |
| 2799 | |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 2800 | static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, |
| 2801 | BlockDriverCompletionFunc *cb, void *opaque) |
| 2802 | { |
| 2803 | BlockDriverAIOCBSync *acb; |
| 2804 | |
| 2805 | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); |
| 2806 | acb->is_write = 1; /* don't bounce in the completion hadler */ |
| 2807 | acb->qiov = NULL; |
| 2808 | acb->bounce = NULL; |
| 2809 | acb->ret = 0; |
| 2810 | |
| 2811 | if (!acb->bh) |
| 2812 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
| 2813 | |
| 2814 | bdrv_flush(bs); |
| 2815 | qemu_bh_schedule(acb->bh); |
| 2816 | return &acb->common; |
| 2817 | } |
| 2818 | |
Alexander Graf | 016f5cf | 2010-05-26 17:51:49 +0200 | [diff] [blame] | 2819 | static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs, |
| 2820 | BlockDriverCompletionFunc *cb, void *opaque) |
| 2821 | { |
| 2822 | BlockDriverAIOCBSync *acb; |
| 2823 | |
| 2824 | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); |
| 2825 | acb->is_write = 1; /* don't bounce in the completion handler */ |
| 2826 | acb->qiov = NULL; |
| 2827 | acb->bounce = NULL; |
| 2828 | acb->ret = 0; |
| 2829 | |
| 2830 | if (!acb->bh) { |
| 2831 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
| 2832 | } |
| 2833 | |
| 2834 | qemu_bh_schedule(acb->bh); |
| 2835 | return &acb->common; |
| 2836 | } |
| 2837 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2838 | /**************************************************************/ |
| 2839 | /* sync block device emulation */ |
| 2840 | |
| 2841 | static void bdrv_rw_em_cb(void *opaque, int ret) |
| 2842 | { |
| 2843 | *(int *)opaque = ret; |
| 2844 | } |
| 2845 | |
| 2846 | #define NOT_DONE 0x7fffffff |
| 2847 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2848 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2849 | uint8_t *buf, int nb_sectors) |
| 2850 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2851 | int async_ret; |
| 2852 | BlockDriverAIOCB *acb; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2853 | struct iovec iov; |
| 2854 | QEMUIOVector qiov; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2855 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2856 | async_ret = NOT_DONE; |
blueswir1 | 3f4cb3d | 2009-04-13 16:31:01 +0000 | [diff] [blame] | 2857 | iov.iov_base = (void *)buf; |
Jes Sorensen | eb5a316 | 2010-05-27 16:20:31 +0200 | [diff] [blame] | 2858 | iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2859 | qemu_iovec_init_external(&qiov, &iov, 1); |
| 2860 | acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors, |
| 2861 | bdrv_rw_em_cb, &async_ret); |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 2862 | if (acb == NULL) { |
| 2863 | async_ret = -1; |
| 2864 | goto fail; |
| 2865 | } |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 2866 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2867 | while (async_ret == NOT_DONE) { |
| 2868 | qemu_aio_wait(); |
| 2869 | } |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 2870 | |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 2871 | |
| 2872 | fail: |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2873 | return async_ret; |
| 2874 | } |
| 2875 | |
| 2876 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
| 2877 | const uint8_t *buf, int nb_sectors) |
| 2878 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2879 | int async_ret; |
| 2880 | BlockDriverAIOCB *acb; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2881 | struct iovec iov; |
| 2882 | QEMUIOVector qiov; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2883 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2884 | async_ret = NOT_DONE; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2885 | iov.iov_base = (void *)buf; |
Jes Sorensen | eb5a316 | 2010-05-27 16:20:31 +0200 | [diff] [blame] | 2886 | iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 2887 | qemu_iovec_init_external(&qiov, &iov, 1); |
| 2888 | acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors, |
| 2889 | bdrv_rw_em_cb, &async_ret); |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 2890 | if (acb == NULL) { |
| 2891 | async_ret = -1; |
| 2892 | goto fail; |
| 2893 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2894 | while (async_ret == NOT_DONE) { |
| 2895 | qemu_aio_wait(); |
| 2896 | } |
Kevin Wolf | 65d6b3d | 2009-10-22 17:54:39 +0200 | [diff] [blame] | 2897 | |
| 2898 | fail: |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2899 | return async_ret; |
| 2900 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2901 | |
| 2902 | void bdrv_init(void) |
| 2903 | { |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 2904 | module_call_init(MODULE_INIT_BLOCK); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2905 | } |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2906 | |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 2907 | void bdrv_init_with_whitelist(void) |
| 2908 | { |
| 2909 | use_bdrv_whitelist = 1; |
| 2910 | bdrv_init(); |
| 2911 | } |
| 2912 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 2913 | void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, |
| 2914 | BlockDriverCompletionFunc *cb, void *opaque) |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 2915 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2916 | BlockDriverAIOCB *acb; |
| 2917 | |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 2918 | if (pool->free_aiocb) { |
| 2919 | acb = pool->free_aiocb; |
| 2920 | pool->free_aiocb = acb->next; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2921 | } else { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2922 | acb = g_malloc0(pool->aiocb_size); |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 2923 | acb->pool = pool; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2924 | } |
| 2925 | acb->bs = bs; |
| 2926 | acb->cb = cb; |
| 2927 | acb->opaque = opaque; |
| 2928 | return acb; |
| 2929 | } |
| 2930 | |
| 2931 | void qemu_aio_release(void *p) |
| 2932 | { |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 2933 | BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; |
| 2934 | AIOPool *pool = acb->pool; |
| 2935 | acb->next = pool->free_aiocb; |
| 2936 | pool->free_aiocb = acb; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 2937 | } |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 2938 | |
| 2939 | /**************************************************************/ |
Kevin Wolf | f9f05dc | 2011-07-15 13:50:26 +0200 | [diff] [blame] | 2940 | /* Coroutine block device emulation */ |
| 2941 | |
| 2942 | typedef struct CoroutineIOCompletion { |
| 2943 | Coroutine *coroutine; |
| 2944 | int ret; |
| 2945 | } CoroutineIOCompletion; |
| 2946 | |
| 2947 | static void bdrv_co_io_em_complete(void *opaque, int ret) |
| 2948 | { |
| 2949 | CoroutineIOCompletion *co = opaque; |
| 2950 | |
| 2951 | co->ret = ret; |
| 2952 | qemu_coroutine_enter(co->coroutine, NULL); |
| 2953 | } |
| 2954 | |
| 2955 | static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, |
| 2956 | int nb_sectors, QEMUIOVector *iov, |
| 2957 | bool is_write) |
| 2958 | { |
| 2959 | CoroutineIOCompletion co = { |
| 2960 | .coroutine = qemu_coroutine_self(), |
| 2961 | }; |
| 2962 | BlockDriverAIOCB *acb; |
| 2963 | |
| 2964 | if (is_write) { |
| 2965 | acb = bdrv_aio_writev(bs, sector_num, iov, nb_sectors, |
| 2966 | bdrv_co_io_em_complete, &co); |
| 2967 | } else { |
| 2968 | acb = bdrv_aio_readv(bs, sector_num, iov, nb_sectors, |
| 2969 | bdrv_co_io_em_complete, &co); |
| 2970 | } |
| 2971 | |
| 2972 | trace_bdrv_co_io(is_write, acb); |
| 2973 | if (!acb) { |
| 2974 | return -EIO; |
| 2975 | } |
| 2976 | qemu_coroutine_yield(); |
| 2977 | |
| 2978 | return co.ret; |
| 2979 | } |
| 2980 | |
| 2981 | static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, |
| 2982 | int64_t sector_num, int nb_sectors, |
| 2983 | QEMUIOVector *iov) |
| 2984 | { |
| 2985 | return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); |
| 2986 | } |
| 2987 | |
| 2988 | static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, |
| 2989 | int64_t sector_num, int nb_sectors, |
| 2990 | QEMUIOVector *iov) |
| 2991 | { |
| 2992 | return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); |
| 2993 | } |
| 2994 | |
Kevin Wolf | e7a8a78 | 2011-07-15 16:05:00 +0200 | [diff] [blame] | 2995 | static int coroutine_fn bdrv_co_flush_em(BlockDriverState *bs) |
| 2996 | { |
| 2997 | CoroutineIOCompletion co = { |
| 2998 | .coroutine = qemu_coroutine_self(), |
| 2999 | }; |
| 3000 | BlockDriverAIOCB *acb; |
| 3001 | |
| 3002 | acb = bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); |
| 3003 | if (!acb) { |
| 3004 | return -EIO; |
| 3005 | } |
| 3006 | qemu_coroutine_yield(); |
| 3007 | return co.ret; |
| 3008 | } |
| 3009 | |
Kevin Wolf | f9f05dc | 2011-07-15 13:50:26 +0200 | [diff] [blame] | 3010 | /**************************************************************/ |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 3011 | /* removable device support */ |
| 3012 | |
| 3013 | /** |
| 3014 | * Return TRUE if the media is present |
| 3015 | */ |
| 3016 | int bdrv_is_inserted(BlockDriverState *bs) |
| 3017 | { |
| 3018 | BlockDriver *drv = bs->drv; |
| 3019 | int ret; |
| 3020 | if (!drv) |
| 3021 | return 0; |
| 3022 | if (!drv->bdrv_is_inserted) |
Markus Armbruster | 4be9762 | 2010-07-27 14:02:01 +0200 | [diff] [blame] | 3023 | return !bs->tray_open; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 3024 | ret = drv->bdrv_is_inserted(bs); |
| 3025 | return ret; |
| 3026 | } |
| 3027 | |
| 3028 | /** |
| 3029 | * Return TRUE if the media changed since the last call to this |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3030 | * function. It is currently only used for floppy disks |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 3031 | */ |
| 3032 | int bdrv_media_changed(BlockDriverState *bs) |
| 3033 | { |
| 3034 | BlockDriver *drv = bs->drv; |
| 3035 | int ret; |
| 3036 | |
| 3037 | if (!drv || !drv->bdrv_media_changed) |
| 3038 | ret = -ENOTSUP; |
| 3039 | else |
| 3040 | ret = drv->bdrv_media_changed(bs); |
| 3041 | if (ret == -ENOTSUP) |
| 3042 | ret = bs->media_changed; |
| 3043 | bs->media_changed = 0; |
| 3044 | return ret; |
| 3045 | } |
| 3046 | |
| 3047 | /** |
| 3048 | * If eject_flag is TRUE, eject the media. Otherwise, close the tray |
| 3049 | */ |
Mark McLoughlin | aea2a33 | 2009-05-27 10:06:11 +0100 | [diff] [blame] | 3050 | int bdrv_eject(BlockDriverState *bs, int eject_flag) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 3051 | { |
| 3052 | BlockDriver *drv = bs->drv; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 3053 | |
Markus Armbruster | 49aa46b | 2011-07-20 18:23:43 +0200 | [diff] [blame] | 3054 | if (eject_flag && bs->locked) { |
Mark McLoughlin | aea2a33 | 2009-05-27 10:06:11 +0100 | [diff] [blame] | 3055 | return -EBUSY; |
| 3056 | } |
| 3057 | |
Markus Armbruster | 822e1cd | 2011-07-20 18:23:42 +0200 | [diff] [blame] | 3058 | if (drv && drv->bdrv_eject) { |
| 3059 | drv->bdrv_eject(bs, eject_flag); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 3060 | } |
Markus Armbruster | 822e1cd | 2011-07-20 18:23:42 +0200 | [diff] [blame] | 3061 | bs->tray_open = eject_flag; |
| 3062 | return 0; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 3063 | } |
| 3064 | |
| 3065 | int bdrv_is_locked(BlockDriverState *bs) |
| 3066 | { |
| 3067 | return bs->locked; |
| 3068 | } |
| 3069 | |
| 3070 | /** |
| 3071 | * Lock or unlock the media (if it is locked, the user won't be able |
| 3072 | * to eject it manually). |
| 3073 | */ |
| 3074 | void bdrv_set_locked(BlockDriverState *bs, int locked) |
| 3075 | { |
| 3076 | BlockDriver *drv = bs->drv; |
| 3077 | |
Stefan Hajnoczi | b8c6d09 | 2011-03-29 20:04:40 +0100 | [diff] [blame] | 3078 | trace_bdrv_set_locked(bs, locked); |
| 3079 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 3080 | bs->locked = locked; |
| 3081 | if (drv && drv->bdrv_set_locked) { |
| 3082 | drv->bdrv_set_locked(bs, locked); |
| 3083 | } |
| 3084 | } |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 3085 | |
| 3086 | /* needed for generic scsi interface */ |
| 3087 | |
| 3088 | int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
| 3089 | { |
| 3090 | BlockDriver *drv = bs->drv; |
| 3091 | |
| 3092 | if (drv && drv->bdrv_ioctl) |
| 3093 | return drv->bdrv_ioctl(bs, req, buf); |
| 3094 | return -ENOTSUP; |
| 3095 | } |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 3096 | |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 3097 | BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, |
| 3098 | unsigned long int req, void *buf, |
| 3099 | BlockDriverCompletionFunc *cb, void *opaque) |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 3100 | { |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 3101 | BlockDriver *drv = bs->drv; |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 3102 | |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 3103 | if (drv && drv->bdrv_aio_ioctl) |
| 3104 | return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); |
| 3105 | return NULL; |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 3106 | } |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 3107 | |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 3108 | |
| 3109 | |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 3110 | void *qemu_blockalign(BlockDriverState *bs, size_t size) |
| 3111 | { |
| 3112 | return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); |
| 3113 | } |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 3114 | |
| 3115 | void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) |
| 3116 | { |
| 3117 | int64_t bitmap_size; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 3118 | |
Liran Schour | aaa0eb7 | 2010-01-26 10:31:48 +0200 | [diff] [blame] | 3119 | bs->dirty_count = 0; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 3120 | if (enable) { |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 3121 | if (!bs->dirty_bitmap) { |
| 3122 | bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + |
| 3123 | BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; |
| 3124 | bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 3125 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 3126 | bs->dirty_bitmap = g_malloc0(bitmap_size); |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 3127 | } |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 3128 | } else { |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 3129 | if (bs->dirty_bitmap) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 3130 | g_free(bs->dirty_bitmap); |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 3131 | bs->dirty_bitmap = NULL; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 3132 | } |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 3133 | } |
| 3134 | } |
| 3135 | |
| 3136 | int bdrv_get_dirty(BlockDriverState *bs, int64_t sector) |
| 3137 | { |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 3138 | int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 3139 | |
Jan Kiszka | c6d2283 | 2009-11-30 18:21:20 +0100 | [diff] [blame] | 3140 | if (bs->dirty_bitmap && |
| 3141 | (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { |
Marcelo Tosatti | 6d59fec | 2010-11-08 17:02:54 -0200 | [diff] [blame] | 3142 | return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] & |
| 3143 | (1UL << (chunk % (sizeof(unsigned long) * 8)))); |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 3144 | } else { |
| 3145 | return 0; |
| 3146 | } |
| 3147 | } |
| 3148 | |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 3149 | void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, |
| 3150 | int nr_sectors) |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 3151 | { |
| 3152 | set_dirty_bitmap(bs, cur_sector, nr_sectors, 0); |
| 3153 | } |
Liran Schour | aaa0eb7 | 2010-01-26 10:31:48 +0200 | [diff] [blame] | 3154 | |
| 3155 | int64_t bdrv_get_dirty_count(BlockDriverState *bs) |
| 3156 | { |
| 3157 | return bs->dirty_count; |
| 3158 | } |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 3159 | |
Marcelo Tosatti | db593f2 | 2011-01-26 12:12:34 -0200 | [diff] [blame] | 3160 | void bdrv_set_in_use(BlockDriverState *bs, int in_use) |
| 3161 | { |
| 3162 | assert(bs->in_use != in_use); |
| 3163 | bs->in_use = in_use; |
| 3164 | } |
| 3165 | |
| 3166 | int bdrv_in_use(BlockDriverState *bs) |
| 3167 | { |
| 3168 | return bs->in_use; |
| 3169 | } |
| 3170 | |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 3171 | int bdrv_img_create(const char *filename, const char *fmt, |
| 3172 | const char *base_filename, const char *base_fmt, |
| 3173 | char *options, uint64_t img_size, int flags) |
| 3174 | { |
| 3175 | QEMUOptionParameter *param = NULL, *create_options = NULL; |
Kevin Wolf | d220894 | 2011-06-01 14:03:31 +0200 | [diff] [blame] | 3176 | QEMUOptionParameter *backing_fmt, *backing_file, *size; |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 3177 | BlockDriverState *bs = NULL; |
| 3178 | BlockDriver *drv, *proto_drv; |
Stefan Hajnoczi | 96df67d | 2011-01-24 09:32:20 +0000 | [diff] [blame] | 3179 | BlockDriver *backing_drv = NULL; |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 3180 | int ret = 0; |
| 3181 | |
| 3182 | /* Find driver and parse its options */ |
| 3183 | drv = bdrv_find_format(fmt); |
| 3184 | if (!drv) { |
| 3185 | error_report("Unknown file format '%s'", fmt); |
Jes Sorensen | 4f70f24 | 2010-12-16 13:52:18 +0100 | [diff] [blame] | 3186 | ret = -EINVAL; |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 3187 | goto out; |
| 3188 | } |
| 3189 | |
| 3190 | proto_drv = bdrv_find_protocol(filename); |
| 3191 | if (!proto_drv) { |
| 3192 | error_report("Unknown protocol '%s'", filename); |
Jes Sorensen | 4f70f24 | 2010-12-16 13:52:18 +0100 | [diff] [blame] | 3193 | ret = -EINVAL; |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 3194 | goto out; |
| 3195 | } |
| 3196 | |
| 3197 | create_options = append_option_parameters(create_options, |
| 3198 | drv->create_options); |
| 3199 | create_options = append_option_parameters(create_options, |
| 3200 | proto_drv->create_options); |
| 3201 | |
| 3202 | /* Create parameter list with default values */ |
| 3203 | param = parse_option_parameters("", create_options, param); |
| 3204 | |
| 3205 | set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); |
| 3206 | |
| 3207 | /* Parse -o options */ |
| 3208 | if (options) { |
| 3209 | param = parse_option_parameters(options, create_options, param); |
| 3210 | if (param == NULL) { |
| 3211 | error_report("Invalid options for file format '%s'.", fmt); |
Jes Sorensen | 4f70f24 | 2010-12-16 13:52:18 +0100 | [diff] [blame] | 3212 | ret = -EINVAL; |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 3213 | goto out; |
| 3214 | } |
| 3215 | } |
| 3216 | |
| 3217 | if (base_filename) { |
| 3218 | if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, |
| 3219 | base_filename)) { |
| 3220 | error_report("Backing file not supported for file format '%s'", |
| 3221 | fmt); |
Jes Sorensen | 4f70f24 | 2010-12-16 13:52:18 +0100 | [diff] [blame] | 3222 | ret = -EINVAL; |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 3223 | goto out; |
| 3224 | } |
| 3225 | } |
| 3226 | |
| 3227 | if (base_fmt) { |
| 3228 | if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { |
| 3229 | error_report("Backing file format not supported for file " |
| 3230 | "format '%s'", fmt); |
Jes Sorensen | 4f70f24 | 2010-12-16 13:52:18 +0100 | [diff] [blame] | 3231 | ret = -EINVAL; |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 3232 | goto out; |
| 3233 | } |
| 3234 | } |
| 3235 | |
Jes Sorensen | 792da93 | 2010-12-16 13:52:17 +0100 | [diff] [blame] | 3236 | backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); |
| 3237 | if (backing_file && backing_file->value.s) { |
| 3238 | if (!strcmp(filename, backing_file->value.s)) { |
| 3239 | error_report("Error: Trying to create an image with the " |
| 3240 | "same filename as the backing file"); |
Jes Sorensen | 4f70f24 | 2010-12-16 13:52:18 +0100 | [diff] [blame] | 3241 | ret = -EINVAL; |
Jes Sorensen | 792da93 | 2010-12-16 13:52:17 +0100 | [diff] [blame] | 3242 | goto out; |
| 3243 | } |
| 3244 | } |
| 3245 | |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 3246 | backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); |
| 3247 | if (backing_fmt && backing_fmt->value.s) { |
Stefan Hajnoczi | 96df67d | 2011-01-24 09:32:20 +0000 | [diff] [blame] | 3248 | backing_drv = bdrv_find_format(backing_fmt->value.s); |
| 3249 | if (!backing_drv) { |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 3250 | error_report("Unknown backing file format '%s'", |
| 3251 | backing_fmt->value.s); |
Jes Sorensen | 4f70f24 | 2010-12-16 13:52:18 +0100 | [diff] [blame] | 3252 | ret = -EINVAL; |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 3253 | goto out; |
| 3254 | } |
| 3255 | } |
| 3256 | |
| 3257 | // The size for the image must always be specified, with one exception: |
| 3258 | // If we are using a backing file, we can obtain the size from there |
Kevin Wolf | d220894 | 2011-06-01 14:03:31 +0200 | [diff] [blame] | 3259 | size = get_option_parameter(param, BLOCK_OPT_SIZE); |
| 3260 | if (size && size->value.n == -1) { |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 3261 | if (backing_file && backing_file->value.s) { |
| 3262 | uint64_t size; |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 3263 | char buf[32]; |
| 3264 | |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 3265 | bs = bdrv_new(""); |
| 3266 | |
Stefan Hajnoczi | 96df67d | 2011-01-24 09:32:20 +0000 | [diff] [blame] | 3267 | ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv); |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 3268 | if (ret < 0) { |
Stefan Hajnoczi | 96df67d | 2011-01-24 09:32:20 +0000 | [diff] [blame] | 3269 | error_report("Could not open '%s'", backing_file->value.s); |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 3270 | goto out; |
| 3271 | } |
| 3272 | bdrv_get_geometry(bs, &size); |
| 3273 | size *= 512; |
| 3274 | |
| 3275 | snprintf(buf, sizeof(buf), "%" PRId64, size); |
| 3276 | set_option_parameter(param, BLOCK_OPT_SIZE, buf); |
| 3277 | } else { |
| 3278 | error_report("Image creation needs a size parameter"); |
Jes Sorensen | 4f70f24 | 2010-12-16 13:52:18 +0100 | [diff] [blame] | 3279 | ret = -EINVAL; |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 3280 | goto out; |
| 3281 | } |
| 3282 | } |
| 3283 | |
| 3284 | printf("Formatting '%s', fmt=%s ", filename, fmt); |
| 3285 | print_option_parameters(param); |
| 3286 | puts(""); |
| 3287 | |
| 3288 | ret = bdrv_create(drv, filename, param); |
| 3289 | |
| 3290 | if (ret < 0) { |
| 3291 | if (ret == -ENOTSUP) { |
| 3292 | error_report("Formatting or formatting option not supported for " |
| 3293 | "file format '%s'", fmt); |
| 3294 | } else if (ret == -EFBIG) { |
| 3295 | error_report("The image size is too large for file format '%s'", |
| 3296 | fmt); |
| 3297 | } else { |
| 3298 | error_report("%s: error while creating %s: %s", filename, fmt, |
| 3299 | strerror(-ret)); |
| 3300 | } |
| 3301 | } |
| 3302 | |
| 3303 | out: |
| 3304 | free_option_parameters(create_options); |
| 3305 | free_option_parameters(param); |
| 3306 | |
| 3307 | if (bs) { |
| 3308 | bdrv_delete(bs); |
| 3309 | } |
Jes Sorensen | 4f70f24 | 2010-12-16 13:52:18 +0100 | [diff] [blame] | 3310 | |
| 3311 | return ret; |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 3312 | } |