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