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