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