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" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 27 | #include "monitor/monitor.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 28 | #include "block/block_int.h" |
| 29 | #include "block/blockjob.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 30 | #include "qemu/module.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 31 | #include "qapi/qmp/qjson.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 32 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 33 | #include "qemu/notify.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 34 | #include "block/coroutine.h" |
Luiz Capitulino | b202381 | 2011-09-21 17:16:47 -0300 | [diff] [blame] | 35 | #include "qmp-commands.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 36 | #include "qemu/timer.h" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 37 | |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 38 | #ifdef CONFIG_BSD |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 39 | #include <sys/types.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <sys/ioctl.h> |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 42 | #include <sys/queue.h> |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 43 | #ifndef __DragonFly__ |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 44 | #include <sys/disk.h> |
| 45 | #endif |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 46 | #endif |
bellard | 7674e7b | 2005-04-26 21:59:26 +0000 | [diff] [blame] | 47 | |
aliguori | 49dc768 | 2009-03-08 16:26:59 +0000 | [diff] [blame] | 48 | #ifdef _WIN32 |
| 49 | #include <windows.h> |
| 50 | #endif |
| 51 | |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 52 | struct BdrvDirtyBitmap { |
| 53 | HBitmap *bitmap; |
| 54 | QLIST_ENTRY(BdrvDirtyBitmap) list; |
| 55 | }; |
| 56 | |
Stefan Hajnoczi | 1c9805a | 2011-10-13 13:08:22 +0100 | [diff] [blame] | 57 | #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ |
| 58 | |
Markus Armbruster | 7d4b4ba | 2011-09-06 18:58:59 +0200 | [diff] [blame] | 59 | static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 60 | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, |
| 61 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
aliguori | c87c067 | 2009-04-07 18:43:20 +0000 | [diff] [blame] | 62 | BlockDriverCompletionFunc *cb, void *opaque); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 63 | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, |
| 64 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 65 | BlockDriverCompletionFunc *cb, void *opaque); |
Kevin Wolf | f9f05dc | 2011-07-15 13:50:26 +0200 | [diff] [blame] | 66 | static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, |
| 67 | int64_t sector_num, int nb_sectors, |
| 68 | QEMUIOVector *iov); |
| 69 | static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, |
| 70 | int64_t sector_num, int nb_sectors, |
| 71 | QEMUIOVector *iov); |
Stefan Hajnoczi | c5fbe57 | 2011-10-05 17:17:03 +0100 | [diff] [blame] | 72 | static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, |
Stefan Hajnoczi | 470c050 | 2012-01-18 14:40:42 +0000 | [diff] [blame] | 73 | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, |
| 74 | BdrvRequestFlags flags); |
Stefan Hajnoczi | 1c9805a | 2011-10-13 13:08:22 +0100 | [diff] [blame] | 75 | static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, |
Stefan Hajnoczi | f08f2dd | 2012-02-07 13:27:25 +0000 | [diff] [blame] | 76 | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, |
| 77 | BdrvRequestFlags flags); |
Stefan Hajnoczi | b2a6137 | 2011-10-13 13:08:23 +0100 | [diff] [blame] | 78 | static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, |
| 79 | int64_t sector_num, |
| 80 | QEMUIOVector *qiov, |
| 81 | int nb_sectors, |
Paolo Bonzini | d20d9b7 | 2013-11-22 13:39:44 +0100 | [diff] [blame] | 82 | BdrvRequestFlags flags, |
Stefan Hajnoczi | b2a6137 | 2011-10-13 13:08:23 +0100 | [diff] [blame] | 83 | BlockDriverCompletionFunc *cb, |
| 84 | void *opaque, |
Stefan Hajnoczi | 8c5873d | 2011-10-13 21:09:28 +0100 | [diff] [blame] | 85 | bool is_write); |
Stefan Hajnoczi | b2a6137 | 2011-10-13 13:08:23 +0100 | [diff] [blame] | 86 | static void coroutine_fn bdrv_co_do_rw(void *opaque); |
Kevin Wolf | 621f058 | 2012-03-20 15:12:58 +0100 | [diff] [blame] | 87 | static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, |
Peter Lieven | aa7bfbf | 2013-10-24 12:06:51 +0200 | [diff] [blame] | 88 | int64_t sector_num, int nb_sectors, BdrvRequestFlags flags); |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 89 | |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 90 | static QTAILQ_HEAD(, BlockDriverState) bdrv_states = |
| 91 | QTAILQ_HEAD_INITIALIZER(bdrv_states); |
blueswir1 | 7ee930d | 2008-09-17 19:04:14 +0000 | [diff] [blame] | 92 | |
Benoît Canet | dc364f4 | 2014-01-23 21:31:32 +0100 | [diff] [blame^] | 93 | static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states = |
| 94 | QTAILQ_HEAD_INITIALIZER(graph_bdrv_states); |
| 95 | |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 96 | static QLIST_HEAD(, BlockDriver) bdrv_drivers = |
| 97 | QLIST_HEAD_INITIALIZER(bdrv_drivers); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 98 | |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 99 | /* If non-zero, use only whitelisted block drivers */ |
| 100 | static int use_bdrv_whitelist; |
| 101 | |
Stefan Hajnoczi | 9e0b22f | 2010-12-09 11:53:00 +0000 | [diff] [blame] | 102 | #ifdef _WIN32 |
| 103 | static int is_windows_drive_prefix(const char *filename) |
| 104 | { |
| 105 | return (((filename[0] >= 'a' && filename[0] <= 'z') || |
| 106 | (filename[0] >= 'A' && filename[0] <= 'Z')) && |
| 107 | filename[1] == ':'); |
| 108 | } |
| 109 | |
| 110 | int is_windows_drive(const char *filename) |
| 111 | { |
| 112 | if (is_windows_drive_prefix(filename) && |
| 113 | filename[2] == '\0') |
| 114 | return 1; |
| 115 | if (strstart(filename, "\\\\.\\", NULL) || |
| 116 | strstart(filename, "//./", NULL)) |
| 117 | return 1; |
| 118 | return 0; |
| 119 | } |
| 120 | #endif |
| 121 | |
Zhi Yong Wu | 0563e19 | 2011-11-03 16:57:25 +0800 | [diff] [blame] | 122 | /* throttling disk I/O limits */ |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 123 | void bdrv_set_io_limits(BlockDriverState *bs, |
| 124 | ThrottleConfig *cfg) |
| 125 | { |
| 126 | int i; |
| 127 | |
| 128 | throttle_config(&bs->throttle_state, cfg); |
| 129 | |
| 130 | for (i = 0; i < 2; i++) { |
| 131 | qemu_co_enter_next(&bs->throttled_reqs[i]); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /* this function drain all the throttled IOs */ |
| 136 | static bool bdrv_start_throttled_reqs(BlockDriverState *bs) |
| 137 | { |
| 138 | bool drained = false; |
| 139 | bool enabled = bs->io_limits_enabled; |
| 140 | int i; |
| 141 | |
| 142 | bs->io_limits_enabled = false; |
| 143 | |
| 144 | for (i = 0; i < 2; i++) { |
| 145 | while (qemu_co_enter_next(&bs->throttled_reqs[i])) { |
| 146 | drained = true; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | bs->io_limits_enabled = enabled; |
| 151 | |
| 152 | return drained; |
| 153 | } |
| 154 | |
Zhi Yong Wu | 98f90db | 2011-11-08 13:00:14 +0800 | [diff] [blame] | 155 | void bdrv_io_limits_disable(BlockDriverState *bs) |
| 156 | { |
| 157 | bs->io_limits_enabled = false; |
| 158 | |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 159 | bdrv_start_throttled_reqs(bs); |
Zhi Yong Wu | 98f90db | 2011-11-08 13:00:14 +0800 | [diff] [blame] | 160 | |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 161 | throttle_destroy(&bs->throttle_state); |
Zhi Yong Wu | 98f90db | 2011-11-08 13:00:14 +0800 | [diff] [blame] | 162 | } |
| 163 | |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 164 | static void bdrv_throttle_read_timer_cb(void *opaque) |
Zhi Yong Wu | 0563e19 | 2011-11-03 16:57:25 +0800 | [diff] [blame] | 165 | { |
| 166 | BlockDriverState *bs = opaque; |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 167 | qemu_co_enter_next(&bs->throttled_reqs[0]); |
Zhi Yong Wu | 0563e19 | 2011-11-03 16:57:25 +0800 | [diff] [blame] | 168 | } |
| 169 | |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 170 | static void bdrv_throttle_write_timer_cb(void *opaque) |
| 171 | { |
| 172 | BlockDriverState *bs = opaque; |
| 173 | qemu_co_enter_next(&bs->throttled_reqs[1]); |
| 174 | } |
| 175 | |
| 176 | /* should be called before bdrv_set_io_limits if a limit is set */ |
Zhi Yong Wu | 0563e19 | 2011-11-03 16:57:25 +0800 | [diff] [blame] | 177 | void bdrv_io_limits_enable(BlockDriverState *bs) |
| 178 | { |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 179 | assert(!bs->io_limits_enabled); |
| 180 | throttle_init(&bs->throttle_state, |
| 181 | QEMU_CLOCK_VIRTUAL, |
| 182 | bdrv_throttle_read_timer_cb, |
| 183 | bdrv_throttle_write_timer_cb, |
| 184 | bs); |
Zhi Yong Wu | 0563e19 | 2011-11-03 16:57:25 +0800 | [diff] [blame] | 185 | bs->io_limits_enabled = true; |
| 186 | } |
| 187 | |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 188 | /* This function makes an IO wait if needed |
| 189 | * |
| 190 | * @nb_sectors: the number of sectors of the IO |
| 191 | * @is_write: is the IO a write |
| 192 | */ |
Zhi Yong Wu | 98f90db | 2011-11-08 13:00:14 +0800 | [diff] [blame] | 193 | static void bdrv_io_limits_intercept(BlockDriverState *bs, |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 194 | int nb_sectors, |
| 195 | bool is_write) |
Zhi Yong Wu | 98f90db | 2011-11-08 13:00:14 +0800 | [diff] [blame] | 196 | { |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 197 | /* does this io must wait */ |
| 198 | bool must_wait = throttle_schedule_timer(&bs->throttle_state, is_write); |
Zhi Yong Wu | 98f90db | 2011-11-08 13:00:14 +0800 | [diff] [blame] | 199 | |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 200 | /* if must wait or any request of this type throttled queue the IO */ |
| 201 | if (must_wait || |
| 202 | !qemu_co_queue_empty(&bs->throttled_reqs[is_write])) { |
| 203 | qemu_co_queue_wait(&bs->throttled_reqs[is_write]); |
Zhi Yong Wu | 98f90db | 2011-11-08 13:00:14 +0800 | [diff] [blame] | 204 | } |
| 205 | |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 206 | /* the IO will be executed, do the accounting */ |
| 207 | throttle_account(&bs->throttle_state, |
| 208 | is_write, |
| 209 | nb_sectors * BDRV_SECTOR_SIZE); |
Zhi Yong Wu | 98f90db | 2011-11-08 13:00:14 +0800 | [diff] [blame] | 210 | |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 211 | /* if the next request must wait -> do nothing */ |
| 212 | if (throttle_schedule_timer(&bs->throttle_state, is_write)) { |
| 213 | return; |
Zhi Yong Wu | 98f90db | 2011-11-08 13:00:14 +0800 | [diff] [blame] | 214 | } |
| 215 | |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 216 | /* else queue next request for execution */ |
| 217 | qemu_co_queue_next(&bs->throttled_reqs[is_write]); |
Zhi Yong Wu | 98f90db | 2011-11-08 13:00:14 +0800 | [diff] [blame] | 218 | } |
| 219 | |
Stefan Hajnoczi | 9e0b22f | 2010-12-09 11:53:00 +0000 | [diff] [blame] | 220 | /* check if the path starts with "<protocol>:" */ |
| 221 | static int path_has_protocol(const char *path) |
| 222 | { |
Paolo Bonzini | 947995c | 2012-05-08 16:51:48 +0200 | [diff] [blame] | 223 | const char *p; |
| 224 | |
Stefan Hajnoczi | 9e0b22f | 2010-12-09 11:53:00 +0000 | [diff] [blame] | 225 | #ifdef _WIN32 |
| 226 | if (is_windows_drive(path) || |
| 227 | is_windows_drive_prefix(path)) { |
| 228 | return 0; |
| 229 | } |
Paolo Bonzini | 947995c | 2012-05-08 16:51:48 +0200 | [diff] [blame] | 230 | p = path + strcspn(path, ":/\\"); |
| 231 | #else |
| 232 | p = path + strcspn(path, ":/"); |
Stefan Hajnoczi | 9e0b22f | 2010-12-09 11:53:00 +0000 | [diff] [blame] | 233 | #endif |
| 234 | |
Paolo Bonzini | 947995c | 2012-05-08 16:51:48 +0200 | [diff] [blame] | 235 | return *p == ':'; |
Stefan Hajnoczi | 9e0b22f | 2010-12-09 11:53:00 +0000 | [diff] [blame] | 236 | } |
| 237 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 238 | int path_is_absolute(const char *path) |
| 239 | { |
bellard | 2166442 | 2007-01-07 18:22:37 +0000 | [diff] [blame] | 240 | #ifdef _WIN32 |
| 241 | /* specific case for names like: "\\.\d:" */ |
Paolo Bonzini | f53f4da | 2012-05-08 16:51:47 +0200 | [diff] [blame] | 242 | if (is_windows_drive(path) || is_windows_drive_prefix(path)) { |
bellard | 2166442 | 2007-01-07 18:22:37 +0000 | [diff] [blame] | 243 | return 1; |
Paolo Bonzini | f53f4da | 2012-05-08 16:51:47 +0200 | [diff] [blame] | 244 | } |
| 245 | return (*path == '/' || *path == '\\'); |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 246 | #else |
Paolo Bonzini | f53f4da | 2012-05-08 16:51:47 +0200 | [diff] [blame] | 247 | return (*path == '/'); |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 248 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /* if filename is absolute, just copy it to dest. Otherwise, build a |
| 252 | path to it by considering it is relative to base_path. URL are |
| 253 | supported. */ |
| 254 | void path_combine(char *dest, int dest_size, |
| 255 | const char *base_path, |
| 256 | const char *filename) |
| 257 | { |
| 258 | const char *p, *p1; |
| 259 | int len; |
| 260 | |
| 261 | if (dest_size <= 0) |
| 262 | return; |
| 263 | if (path_is_absolute(filename)) { |
| 264 | pstrcpy(dest, dest_size, filename); |
| 265 | } else { |
| 266 | p = strchr(base_path, ':'); |
| 267 | if (p) |
| 268 | p++; |
| 269 | else |
| 270 | p = base_path; |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 271 | p1 = strrchr(base_path, '/'); |
| 272 | #ifdef _WIN32 |
| 273 | { |
| 274 | const char *p2; |
| 275 | p2 = strrchr(base_path, '\\'); |
| 276 | if (!p1 || p2 > p1) |
| 277 | p1 = p2; |
| 278 | } |
| 279 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 280 | if (p1) |
| 281 | p1++; |
| 282 | else |
| 283 | p1 = base_path; |
| 284 | if (p1 > p) |
| 285 | p = p1; |
| 286 | len = p - base_path; |
| 287 | if (len > dest_size - 1) |
| 288 | len = dest_size - 1; |
| 289 | memcpy(dest, base_path, len); |
| 290 | dest[len] = '\0'; |
| 291 | pstrcat(dest, dest_size, filename); |
| 292 | } |
| 293 | } |
| 294 | |
Paolo Bonzini | dc5a137 | 2012-05-08 16:51:50 +0200 | [diff] [blame] | 295 | void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz) |
| 296 | { |
| 297 | if (bs->backing_file[0] == '\0' || path_has_protocol(bs->backing_file)) { |
| 298 | pstrcpy(dest, sz, bs->backing_file); |
| 299 | } else { |
| 300 | path_combine(dest, sz, bs->filename, bs->backing_file); |
| 301 | } |
| 302 | } |
| 303 | |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 304 | void bdrv_register(BlockDriver *bdrv) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 305 | { |
Stefan Hajnoczi | 8c5873d | 2011-10-13 21:09:28 +0100 | [diff] [blame] | 306 | /* Block drivers without coroutine functions need emulation */ |
| 307 | if (!bdrv->bdrv_co_readv) { |
Kevin Wolf | f9f05dc | 2011-07-15 13:50:26 +0200 | [diff] [blame] | 308 | bdrv->bdrv_co_readv = bdrv_co_readv_em; |
| 309 | bdrv->bdrv_co_writev = bdrv_co_writev_em; |
| 310 | |
Stefan Hajnoczi | f8c35c1 | 2011-10-13 21:09:31 +0100 | [diff] [blame] | 311 | /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if |
| 312 | * the block driver lacks aio we need to emulate that too. |
| 313 | */ |
Kevin Wolf | f9f05dc | 2011-07-15 13:50:26 +0200 | [diff] [blame] | 314 | if (!bdrv->bdrv_aio_readv) { |
| 315 | /* add AIO emulation layer */ |
| 316 | bdrv->bdrv_aio_readv = bdrv_aio_readv_em; |
| 317 | bdrv->bdrv_aio_writev = bdrv_aio_writev_em; |
Kevin Wolf | f9f05dc | 2011-07-15 13:50:26 +0200 | [diff] [blame] | 318 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 319 | } |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 320 | |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 321 | QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 322 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 323 | |
| 324 | /* create a new block device (by default it is empty) */ |
| 325 | BlockDriverState *bdrv_new(const char *device_name) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 326 | { |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 327 | BlockDriverState *bs; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 328 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 329 | bs = g_malloc0(sizeof(BlockDriverState)); |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 330 | QLIST_INIT(&bs->dirty_bitmaps); |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 331 | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 332 | if (device_name[0] != '\0') { |
Benoît Canet | dc364f4 | 2014-01-23 21:31:32 +0100 | [diff] [blame^] | 333 | QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 334 | } |
Luiz Capitulino | 28a7282 | 2011-09-26 17:43:50 -0300 | [diff] [blame] | 335 | bdrv_iostatus_disable(bs); |
Paolo Bonzini | d7d512f | 2012-08-23 11:20:36 +0200 | [diff] [blame] | 336 | notifier_list_init(&bs->close_notifiers); |
Stefan Hajnoczi | d616b22 | 2013-06-24 17:13:10 +0200 | [diff] [blame] | 337 | notifier_with_return_list_init(&bs->before_write_notifiers); |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 338 | qemu_co_queue_init(&bs->throttled_reqs[0]); |
| 339 | qemu_co_queue_init(&bs->throttled_reqs[1]); |
Fam Zheng | 9fcb025 | 2013-08-23 09:14:46 +0800 | [diff] [blame] | 340 | bs->refcnt = 1; |
Paolo Bonzini | d7d512f | 2012-08-23 11:20:36 +0200 | [diff] [blame] | 341 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 342 | return bs; |
| 343 | } |
| 344 | |
Paolo Bonzini | d7d512f | 2012-08-23 11:20:36 +0200 | [diff] [blame] | 345 | void bdrv_add_close_notifier(BlockDriverState *bs, Notifier *notify) |
| 346 | { |
| 347 | notifier_list_add(&bs->close_notifiers, notify); |
| 348 | } |
| 349 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 350 | BlockDriver *bdrv_find_format(const char *format_name) |
| 351 | { |
| 352 | BlockDriver *drv1; |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 353 | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
| 354 | if (!strcmp(drv1->format_name, format_name)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 355 | return drv1; |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 356 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 357 | } |
| 358 | return NULL; |
| 359 | } |
| 360 | |
Fam Zheng | b64ec4e | 2013-05-29 19:35:40 +0800 | [diff] [blame] | 361 | static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only) |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 362 | { |
Fam Zheng | b64ec4e | 2013-05-29 19:35:40 +0800 | [diff] [blame] | 363 | static const char *whitelist_rw[] = { |
| 364 | CONFIG_BDRV_RW_WHITELIST |
| 365 | }; |
| 366 | static const char *whitelist_ro[] = { |
| 367 | CONFIG_BDRV_RO_WHITELIST |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 368 | }; |
| 369 | const char **p; |
| 370 | |
Fam Zheng | b64ec4e | 2013-05-29 19:35:40 +0800 | [diff] [blame] | 371 | if (!whitelist_rw[0] && !whitelist_ro[0]) { |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 372 | return 1; /* no whitelist, anything goes */ |
Fam Zheng | b64ec4e | 2013-05-29 19:35:40 +0800 | [diff] [blame] | 373 | } |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 374 | |
Fam Zheng | b64ec4e | 2013-05-29 19:35:40 +0800 | [diff] [blame] | 375 | for (p = whitelist_rw; *p; p++) { |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 376 | if (!strcmp(drv->format_name, *p)) { |
| 377 | return 1; |
| 378 | } |
| 379 | } |
Fam Zheng | b64ec4e | 2013-05-29 19:35:40 +0800 | [diff] [blame] | 380 | if (read_only) { |
| 381 | for (p = whitelist_ro; *p; p++) { |
| 382 | if (!strcmp(drv->format_name, *p)) { |
| 383 | return 1; |
| 384 | } |
| 385 | } |
| 386 | } |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 387 | return 0; |
| 388 | } |
| 389 | |
Fam Zheng | b64ec4e | 2013-05-29 19:35:40 +0800 | [diff] [blame] | 390 | BlockDriver *bdrv_find_whitelisted_format(const char *format_name, |
| 391 | bool read_only) |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 392 | { |
| 393 | BlockDriver *drv = bdrv_find_format(format_name); |
Fam Zheng | b64ec4e | 2013-05-29 19:35:40 +0800 | [diff] [blame] | 394 | return drv && bdrv_is_whitelisted(drv, read_only) ? drv : NULL; |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 395 | } |
| 396 | |
Zhi Yong Wu | 5b7e154 | 2012-05-07 16:50:42 +0800 | [diff] [blame] | 397 | typedef struct CreateCo { |
| 398 | BlockDriver *drv; |
| 399 | char *filename; |
| 400 | QEMUOptionParameter *options; |
| 401 | int ret; |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 402 | Error *err; |
Zhi Yong Wu | 5b7e154 | 2012-05-07 16:50:42 +0800 | [diff] [blame] | 403 | } CreateCo; |
| 404 | |
| 405 | static void coroutine_fn bdrv_create_co_entry(void *opaque) |
| 406 | { |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 407 | Error *local_err = NULL; |
| 408 | int ret; |
| 409 | |
Zhi Yong Wu | 5b7e154 | 2012-05-07 16:50:42 +0800 | [diff] [blame] | 410 | CreateCo *cco = opaque; |
| 411 | assert(cco->drv); |
| 412 | |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 413 | ret = cco->drv->bdrv_create(cco->filename, cco->options, &local_err); |
| 414 | if (error_is_set(&local_err)) { |
| 415 | error_propagate(&cco->err, local_err); |
| 416 | } |
| 417 | cco->ret = ret; |
Zhi Yong Wu | 5b7e154 | 2012-05-07 16:50:42 +0800 | [diff] [blame] | 418 | } |
| 419 | |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 420 | int bdrv_create(BlockDriver *drv, const char* filename, |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 421 | QEMUOptionParameter *options, Error **errp) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 422 | { |
Zhi Yong Wu | 5b7e154 | 2012-05-07 16:50:42 +0800 | [diff] [blame] | 423 | int ret; |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 424 | |
Zhi Yong Wu | 5b7e154 | 2012-05-07 16:50:42 +0800 | [diff] [blame] | 425 | Coroutine *co; |
| 426 | CreateCo cco = { |
| 427 | .drv = drv, |
| 428 | .filename = g_strdup(filename), |
| 429 | .options = options, |
| 430 | .ret = NOT_DONE, |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 431 | .err = NULL, |
Zhi Yong Wu | 5b7e154 | 2012-05-07 16:50:42 +0800 | [diff] [blame] | 432 | }; |
| 433 | |
| 434 | if (!drv->bdrv_create) { |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 435 | error_setg(errp, "Driver '%s' does not support image creation", drv->format_name); |
Luiz Capitulino | 80168bf | 2012-10-17 16:45:25 -0300 | [diff] [blame] | 436 | ret = -ENOTSUP; |
| 437 | goto out; |
Zhi Yong Wu | 5b7e154 | 2012-05-07 16:50:42 +0800 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | if (qemu_in_coroutine()) { |
| 441 | /* Fast-path if already in coroutine context */ |
| 442 | bdrv_create_co_entry(&cco); |
| 443 | } else { |
| 444 | co = qemu_coroutine_create(bdrv_create_co_entry); |
| 445 | qemu_coroutine_enter(co, &cco); |
| 446 | while (cco.ret == NOT_DONE) { |
| 447 | qemu_aio_wait(); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | ret = cco.ret; |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 452 | if (ret < 0) { |
| 453 | if (error_is_set(&cco.err)) { |
| 454 | error_propagate(errp, cco.err); |
| 455 | } else { |
| 456 | error_setg_errno(errp, -ret, "Could not create image"); |
| 457 | } |
| 458 | } |
Zhi Yong Wu | 5b7e154 | 2012-05-07 16:50:42 +0800 | [diff] [blame] | 459 | |
Luiz Capitulino | 80168bf | 2012-10-17 16:45:25 -0300 | [diff] [blame] | 460 | out: |
| 461 | g_free(cco.filename); |
Zhi Yong Wu | 5b7e154 | 2012-05-07 16:50:42 +0800 | [diff] [blame] | 462 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 465 | int bdrv_create_file(const char* filename, QEMUOptionParameter *options, |
| 466 | Error **errp) |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 467 | { |
| 468 | BlockDriver *drv; |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 469 | Error *local_err = NULL; |
| 470 | int ret; |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 471 | |
Kevin Wolf | 9828962 | 2013-07-10 15:47:39 +0200 | [diff] [blame] | 472 | drv = bdrv_find_protocol(filename, true); |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 473 | if (drv == NULL) { |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 474 | error_setg(errp, "Could not find protocol for file '%s'", filename); |
Stefan Hajnoczi | 16905d7 | 2010-11-30 15:14:14 +0000 | [diff] [blame] | 475 | return -ENOENT; |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 476 | } |
| 477 | |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 478 | ret = bdrv_create(drv, filename, options, &local_err); |
| 479 | if (error_is_set(&local_err)) { |
| 480 | error_propagate(errp, local_err); |
| 481 | } |
| 482 | return ret; |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 483 | } |
| 484 | |
Jim Meyering | eba2505 | 2012-05-28 09:27:54 +0200 | [diff] [blame] | 485 | /* |
| 486 | * Create a uniquely-named empty temporary file. |
| 487 | * Return 0 upon success, otherwise a negative errno value. |
| 488 | */ |
| 489 | int get_tmp_filename(char *filename, int size) |
| 490 | { |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 491 | #ifdef _WIN32 |
bellard | 3b9f94e | 2007-01-07 17:27:07 +0000 | [diff] [blame] | 492 | char temp_dir[MAX_PATH]; |
Jim Meyering | eba2505 | 2012-05-28 09:27:54 +0200 | [diff] [blame] | 493 | /* GetTempFileName requires that its output buffer (4th param) |
| 494 | have length MAX_PATH or greater. */ |
| 495 | assert(size >= MAX_PATH); |
| 496 | return (GetTempPath(MAX_PATH, temp_dir) |
| 497 | && GetTempFileName(temp_dir, "qem", 0, filename) |
| 498 | ? 0 : -GetLastError()); |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 499 | #else |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 500 | int fd; |
blueswir1 | 7ccfb2e | 2008-09-14 06:45:34 +0000 | [diff] [blame] | 501 | const char *tmpdir; |
aurel32 | 0badc1e | 2008-03-10 00:05:34 +0000 | [diff] [blame] | 502 | tmpdir = getenv("TMPDIR"); |
| 503 | if (!tmpdir) |
| 504 | tmpdir = "/tmp"; |
Jim Meyering | eba2505 | 2012-05-28 09:27:54 +0200 | [diff] [blame] | 505 | if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) { |
| 506 | return -EOVERFLOW; |
| 507 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 508 | fd = mkstemp(filename); |
Dunrong Huang | fe235a0 | 2012-09-05 21:26:22 +0800 | [diff] [blame] | 509 | if (fd < 0) { |
| 510 | return -errno; |
| 511 | } |
| 512 | if (close(fd) != 0) { |
| 513 | unlink(filename); |
Jim Meyering | eba2505 | 2012-05-28 09:27:54 +0200 | [diff] [blame] | 514 | return -errno; |
| 515 | } |
| 516 | return 0; |
bellard | d524939 | 2004-08-03 21:14:23 +0000 | [diff] [blame] | 517 | #endif |
Jim Meyering | eba2505 | 2012-05-28 09:27:54 +0200 | [diff] [blame] | 518 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 519 | |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 520 | /* |
| 521 | * Detect host devices. By convention, /dev/cdrom[N] is always |
| 522 | * recognized as a host CDROM. |
| 523 | */ |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 524 | static BlockDriver *find_hdev_driver(const char *filename) |
| 525 | { |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 526 | int score_max = 0, score; |
| 527 | BlockDriver *drv = NULL, *d; |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 528 | |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 529 | QLIST_FOREACH(d, &bdrv_drivers, list) { |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 530 | if (d->bdrv_probe_device) { |
| 531 | score = d->bdrv_probe_device(filename); |
| 532 | if (score > score_max) { |
| 533 | score_max = score; |
| 534 | drv = d; |
| 535 | } |
| 536 | } |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 537 | } |
| 538 | |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 539 | return drv; |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 540 | } |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 541 | |
Kevin Wolf | 9828962 | 2013-07-10 15:47:39 +0200 | [diff] [blame] | 542 | BlockDriver *bdrv_find_protocol(const char *filename, |
| 543 | bool allow_protocol_prefix) |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 544 | { |
| 545 | BlockDriver *drv1; |
| 546 | char protocol[128]; |
| 547 | int len; |
| 548 | const char *p; |
| 549 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 550 | /* TODO Drivers without bdrv_file_open must be specified explicitly */ |
| 551 | |
Christoph Hellwig | 39508e7 | 2010-06-23 12:25:17 +0200 | [diff] [blame] | 552 | /* |
| 553 | * XXX(hch): we really should not let host device detection |
| 554 | * override an explicit protocol specification, but moving this |
| 555 | * later breaks access to device names with colons in them. |
| 556 | * Thanks to the brain-dead persistent naming schemes on udev- |
| 557 | * based Linux systems those actually are quite common. |
| 558 | */ |
| 559 | drv1 = find_hdev_driver(filename); |
| 560 | if (drv1) { |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 561 | return drv1; |
| 562 | } |
Christoph Hellwig | 39508e7 | 2010-06-23 12:25:17 +0200 | [diff] [blame] | 563 | |
Kevin Wolf | 9828962 | 2013-07-10 15:47:39 +0200 | [diff] [blame] | 564 | if (!path_has_protocol(filename) || !allow_protocol_prefix) { |
Christoph Hellwig | 39508e7 | 2010-06-23 12:25:17 +0200 | [diff] [blame] | 565 | return bdrv_find_format("file"); |
| 566 | } |
Kevin Wolf | 9828962 | 2013-07-10 15:47:39 +0200 | [diff] [blame] | 567 | |
Stefan Hajnoczi | 9e0b22f | 2010-12-09 11:53:00 +0000 | [diff] [blame] | 568 | p = strchr(filename, ':'); |
| 569 | assert(p != NULL); |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 570 | len = p - filename; |
| 571 | if (len > sizeof(protocol) - 1) |
| 572 | len = sizeof(protocol) - 1; |
| 573 | memcpy(protocol, filename, len); |
| 574 | protocol[len] = '\0'; |
| 575 | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
| 576 | if (drv1->protocol_name && |
| 577 | !strcmp(drv1->protocol_name, protocol)) { |
| 578 | return drv1; |
| 579 | } |
| 580 | } |
| 581 | return NULL; |
| 582 | } |
| 583 | |
Kevin Wolf | f500a6d | 2012-11-12 17:35:27 +0100 | [diff] [blame] | 584 | static int find_image_format(BlockDriverState *bs, const char *filename, |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 585 | BlockDriver **pdrv, Error **errp) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 586 | { |
Kevin Wolf | f500a6d | 2012-11-12 17:35:27 +0100 | [diff] [blame] | 587 | int score, score_max; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 588 | BlockDriver *drv1, *drv; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 589 | uint8_t buf[2048]; |
Kevin Wolf | f500a6d | 2012-11-12 17:35:27 +0100 | [diff] [blame] | 590 | int ret = 0; |
Nicholas Bellinger | f8ea0b0 | 2010-05-17 09:45:57 -0700 | [diff] [blame] | 591 | |
Kevin Wolf | 08a0055 | 2010-06-01 18:37:31 +0200 | [diff] [blame] | 592 | /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ |
Paolo Bonzini | 8e89559 | 2013-01-10 15:39:27 +0100 | [diff] [blame] | 593 | if (bs->sg || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) { |
Stefan Weil | c98ac35 | 2010-07-21 21:51:51 +0200 | [diff] [blame] | 594 | drv = bdrv_find_format("raw"); |
| 595 | if (!drv) { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 596 | error_setg(errp, "Could not find raw image format"); |
Stefan Weil | c98ac35 | 2010-07-21 21:51:51 +0200 | [diff] [blame] | 597 | ret = -ENOENT; |
| 598 | } |
| 599 | *pdrv = drv; |
| 600 | return ret; |
Nicholas A. Bellinger | 1a39685 | 2010-05-27 08:56:28 -0700 | [diff] [blame] | 601 | } |
Nicholas Bellinger | f8ea0b0 | 2010-05-17 09:45:57 -0700 | [diff] [blame] | 602 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 603 | ret = bdrv_pread(bs, 0, buf, sizeof(buf)); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 604 | if (ret < 0) { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 605 | error_setg_errno(errp, -ret, "Could not read image for determining its " |
| 606 | "format"); |
Stefan Weil | c98ac35 | 2010-07-21 21:51:51 +0200 | [diff] [blame] | 607 | *pdrv = NULL; |
| 608 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 609 | } |
| 610 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 611 | score_max = 0; |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 612 | drv = NULL; |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 613 | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 614 | if (drv1->bdrv_probe) { |
| 615 | score = drv1->bdrv_probe(buf, ret, filename); |
| 616 | if (score > score_max) { |
| 617 | score_max = score; |
| 618 | drv = drv1; |
| 619 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
Stefan Weil | c98ac35 | 2010-07-21 21:51:51 +0200 | [diff] [blame] | 622 | if (!drv) { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 623 | error_setg(errp, "Could not determine image format: No compatible " |
| 624 | "driver found"); |
Stefan Weil | c98ac35 | 2010-07-21 21:51:51 +0200 | [diff] [blame] | 625 | ret = -ENOENT; |
| 626 | } |
| 627 | *pdrv = drv; |
| 628 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 631 | /** |
| 632 | * Set the current 'total_sectors' value |
| 633 | */ |
| 634 | static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) |
| 635 | { |
| 636 | BlockDriver *drv = bs->drv; |
| 637 | |
Nicholas Bellinger | 396759a | 2010-05-17 09:46:04 -0700 | [diff] [blame] | 638 | /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ |
| 639 | if (bs->sg) |
| 640 | return 0; |
| 641 | |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 642 | /* query actual device if possible, otherwise just trust the hint */ |
| 643 | if (drv->bdrv_getlength) { |
| 644 | int64_t length = drv->bdrv_getlength(bs); |
| 645 | if (length < 0) { |
| 646 | return length; |
| 647 | } |
Fam Zheng | 7e38200 | 2013-11-06 19:48:06 +0800 | [diff] [blame] | 648 | hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE); |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | bs->total_sectors = hint; |
| 652 | return 0; |
| 653 | } |
| 654 | |
Stefan Hajnoczi | c3993cd | 2011-08-04 12:26:51 +0100 | [diff] [blame] | 655 | /** |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 656 | * Set open flags for a given discard mode |
| 657 | * |
| 658 | * Return 0 on success, -1 if the discard mode was invalid. |
| 659 | */ |
| 660 | int bdrv_parse_discard_flags(const char *mode, int *flags) |
| 661 | { |
| 662 | *flags &= ~BDRV_O_UNMAP; |
| 663 | |
| 664 | if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) { |
| 665 | /* do nothing */ |
| 666 | } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) { |
| 667 | *flags |= BDRV_O_UNMAP; |
| 668 | } else { |
| 669 | return -1; |
| 670 | } |
| 671 | |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | /** |
Stefan Hajnoczi | c3993cd | 2011-08-04 12:26:51 +0100 | [diff] [blame] | 676 | * Set open flags for a given cache mode |
| 677 | * |
| 678 | * Return 0 on success, -1 if the cache mode was invalid. |
| 679 | */ |
| 680 | int bdrv_parse_cache_flags(const char *mode, int *flags) |
| 681 | { |
| 682 | *flags &= ~BDRV_O_CACHE_MASK; |
| 683 | |
| 684 | if (!strcmp(mode, "off") || !strcmp(mode, "none")) { |
| 685 | *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; |
Stefan Hajnoczi | 92196b2 | 2011-08-04 12:26:52 +0100 | [diff] [blame] | 686 | } else if (!strcmp(mode, "directsync")) { |
| 687 | *flags |= BDRV_O_NOCACHE; |
Stefan Hajnoczi | c3993cd | 2011-08-04 12:26:51 +0100 | [diff] [blame] | 688 | } else if (!strcmp(mode, "writeback")) { |
| 689 | *flags |= BDRV_O_CACHE_WB; |
| 690 | } else if (!strcmp(mode, "unsafe")) { |
| 691 | *flags |= BDRV_O_CACHE_WB; |
| 692 | *flags |= BDRV_O_NO_FLUSH; |
| 693 | } else if (!strcmp(mode, "writethrough")) { |
| 694 | /* this is the default */ |
| 695 | } else { |
| 696 | return -1; |
| 697 | } |
| 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | |
Stefan Hajnoczi | 53fec9d | 2011-11-28 16:08:47 +0000 | [diff] [blame] | 702 | /** |
| 703 | * The copy-on-read flag is actually a reference count so multiple users may |
| 704 | * use the feature without worrying about clobbering its previous state. |
| 705 | * Copy-on-read stays enabled until all users have called to disable it. |
| 706 | */ |
| 707 | void bdrv_enable_copy_on_read(BlockDriverState *bs) |
| 708 | { |
| 709 | bs->copy_on_read++; |
| 710 | } |
| 711 | |
| 712 | void bdrv_disable_copy_on_read(BlockDriverState *bs) |
| 713 | { |
| 714 | assert(bs->copy_on_read > 0); |
| 715 | bs->copy_on_read--; |
| 716 | } |
| 717 | |
Kevin Wolf | 7b27245 | 2012-11-12 17:05:39 +0100 | [diff] [blame] | 718 | static int bdrv_open_flags(BlockDriverState *bs, int flags) |
| 719 | { |
| 720 | int open_flags = flags | BDRV_O_CACHE_WB; |
| 721 | |
| 722 | /* |
| 723 | * Clear flags that are internal to the block layer before opening the |
| 724 | * image. |
| 725 | */ |
| 726 | open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); |
| 727 | |
| 728 | /* |
| 729 | * Snapshots should be writable. |
| 730 | */ |
| 731 | if (bs->is_temporary) { |
| 732 | open_flags |= BDRV_O_RDWR; |
| 733 | } |
| 734 | |
| 735 | return open_flags; |
| 736 | } |
| 737 | |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 738 | /* |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 739 | * Common part for opening disk images and files |
Kevin Wolf | b6ad491 | 2013-03-15 10:35:04 +0100 | [diff] [blame] | 740 | * |
| 741 | * Removes all processed options from *options. |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 742 | */ |
Kevin Wolf | f500a6d | 2012-11-12 17:35:27 +0100 | [diff] [blame] | 743 | static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file, |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 744 | QDict *options, int flags, BlockDriver *drv, Error **errp) |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 745 | { |
| 746 | int ret, open_flags; |
Kevin Wolf | 035fccd | 2013-04-09 14:34:19 +0200 | [diff] [blame] | 747 | const char *filename; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 748 | Error *local_err = NULL; |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 749 | |
| 750 | assert(drv != NULL); |
Paolo Bonzini | 6405875 | 2012-05-08 16:51:49 +0200 | [diff] [blame] | 751 | assert(bs->file == NULL); |
Kevin Wolf | 707ff82 | 2013-03-06 12:20:31 +0100 | [diff] [blame] | 752 | assert(options != NULL && bs->options != options); |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 753 | |
Kevin Wolf | 4567367 | 2013-04-22 17:48:40 +0200 | [diff] [blame] | 754 | if (file != NULL) { |
| 755 | filename = file->filename; |
| 756 | } else { |
| 757 | filename = qdict_get_try_str(options, "filename"); |
| 758 | } |
| 759 | |
| 760 | trace_bdrv_open_common(bs, filename ?: "", flags, drv->format_name); |
Stefan Hajnoczi | 28dcee1 | 2011-09-22 20:14:12 +0100 | [diff] [blame] | 761 | |
Kevin Wolf | 5d186eb | 2013-03-27 17:28:18 +0100 | [diff] [blame] | 762 | /* bdrv_open() with directly using a protocol as drv. This layer is already |
| 763 | * opened, so assign it to bs (while file becomes a closed BlockDriverState) |
| 764 | * and return immediately. */ |
| 765 | if (file != NULL && drv->bdrv_file_open) { |
| 766 | bdrv_swap(file, bs); |
| 767 | return 0; |
| 768 | } |
| 769 | |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 770 | bs->open_flags = flags; |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 771 | bs->buffer_alignment = 512; |
Asias He | 0d51b4d | 2013-08-22 15:24:14 +0800 | [diff] [blame] | 772 | bs->zero_beyond_eof = true; |
Fam Zheng | b64ec4e | 2013-05-29 19:35:40 +0800 | [diff] [blame] | 773 | open_flags = bdrv_open_flags(bs, flags); |
| 774 | bs->read_only = !(open_flags & BDRV_O_RDWR); |
| 775 | |
| 776 | if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) { |
Kevin Wolf | 8f94a6e | 2013-10-10 11:45:55 +0200 | [diff] [blame] | 777 | error_setg(errp, |
| 778 | !bs->read_only && bdrv_is_whitelisted(drv, true) |
| 779 | ? "Driver '%s' can only be used for read-only devices" |
| 780 | : "Driver '%s' is not whitelisted", |
| 781 | drv->format_name); |
Fam Zheng | b64ec4e | 2013-05-29 19:35:40 +0800 | [diff] [blame] | 782 | return -ENOTSUP; |
| 783 | } |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 784 | |
Stefan Hajnoczi | 53fec9d | 2011-11-28 16:08:47 +0000 | [diff] [blame] | 785 | assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */ |
Kevin Wolf | 0ebd24e | 2013-09-19 15:12:18 +0200 | [diff] [blame] | 786 | if (flags & BDRV_O_COPY_ON_READ) { |
| 787 | if (!bs->read_only) { |
| 788 | bdrv_enable_copy_on_read(bs); |
| 789 | } else { |
| 790 | error_setg(errp, "Can't use copy-on-read on read-only device"); |
| 791 | return -EINVAL; |
| 792 | } |
Stefan Hajnoczi | 53fec9d | 2011-11-28 16:08:47 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Kevin Wolf | c2ad1b0 | 2013-03-18 16:40:51 +0100 | [diff] [blame] | 795 | if (filename != NULL) { |
| 796 | pstrcpy(bs->filename, sizeof(bs->filename), filename); |
| 797 | } else { |
| 798 | bs->filename[0] = '\0'; |
| 799 | } |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 800 | |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 801 | bs->drv = drv; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 802 | bs->opaque = g_malloc0(drv->instance_size); |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 803 | |
Stefan Hajnoczi | 03f541b | 2011-10-27 10:54:28 +0100 | [diff] [blame] | 804 | bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB); |
Stefan Hajnoczi | e7c6379 | 2011-10-27 10:54:27 +0100 | [diff] [blame] | 805 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 806 | /* Open the image, either directly or using a protocol */ |
| 807 | if (drv->bdrv_file_open) { |
Kevin Wolf | 5d186eb | 2013-03-27 17:28:18 +0100 | [diff] [blame] | 808 | assert(file == NULL); |
Benoît Canet | 030be32 | 2013-09-24 17:07:04 +0200 | [diff] [blame] | 809 | assert(!drv->bdrv_needs_filename || filename != NULL); |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 810 | ret = drv->bdrv_file_open(bs, options, open_flags, &local_err); |
Kevin Wolf | f500a6d | 2012-11-12 17:35:27 +0100 | [diff] [blame] | 811 | } else { |
Kevin Wolf | 2af5ef7 | 2013-04-09 13:19:18 +0200 | [diff] [blame] | 812 | if (file == NULL) { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 813 | error_setg(errp, "Can't use '%s' as a block driver for the " |
| 814 | "protocol level", drv->format_name); |
Kevin Wolf | 2af5ef7 | 2013-04-09 13:19:18 +0200 | [diff] [blame] | 815 | ret = -EINVAL; |
| 816 | goto free_and_fail; |
| 817 | } |
Kevin Wolf | f500a6d | 2012-11-12 17:35:27 +0100 | [diff] [blame] | 818 | bs->file = file; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 819 | ret = drv->bdrv_open(bs, options, open_flags, &local_err); |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 820 | } |
| 821 | |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 822 | if (ret < 0) { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 823 | if (error_is_set(&local_err)) { |
| 824 | error_propagate(errp, local_err); |
Dunrong Huang | 2fa9aa5 | 2013-09-24 18:14:01 +0800 | [diff] [blame] | 825 | } else if (bs->filename[0]) { |
| 826 | error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename); |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 827 | } else { |
| 828 | error_setg_errno(errp, -ret, "Could not open image"); |
| 829 | } |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 830 | goto free_and_fail; |
| 831 | } |
| 832 | |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 833 | ret = refresh_total_sectors(bs, bs->total_sectors); |
| 834 | if (ret < 0) { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 835 | error_setg_errno(errp, -ret, "Could not refresh total sector count"); |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 836 | goto free_and_fail; |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 837 | } |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 838 | |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 839 | #ifndef _WIN32 |
| 840 | if (bs->is_temporary) { |
Dunrong Huang | d4cea8d | 2013-10-03 01:31:27 +0800 | [diff] [blame] | 841 | assert(bs->filename[0] != '\0'); |
| 842 | unlink(bs->filename); |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 843 | } |
| 844 | #endif |
| 845 | return 0; |
| 846 | |
| 847 | free_and_fail: |
Kevin Wolf | f500a6d | 2012-11-12 17:35:27 +0100 | [diff] [blame] | 848 | bs->file = NULL; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 849 | g_free(bs->opaque); |
Kevin Wolf | 5791533 | 2010-04-14 15:24:50 +0200 | [diff] [blame] | 850 | bs->opaque = NULL; |
| 851 | bs->drv = NULL; |
| 852 | return ret; |
| 853 | } |
| 854 | |
| 855 | /* |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 856 | * Opens a file using a protocol (file, host_device, nbd, ...) |
Kevin Wolf | 787e4a8 | 2013-03-06 11:52:48 +0100 | [diff] [blame] | 857 | * |
| 858 | * options is a QDict of options to pass to the block drivers, or NULL for an |
| 859 | * empty set of options. The reference to the QDict belongs to the block layer |
| 860 | * after the call (even on failure), so if the caller intends to reuse the |
| 861 | * dictionary, it needs to use QINCREF() before calling bdrv_file_open. |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 862 | */ |
Kevin Wolf | 787e4a8 | 2013-03-06 11:52:48 +0100 | [diff] [blame] | 863 | int bdrv_file_open(BlockDriverState **pbs, const char *filename, |
Max Reitz | 72daa72 | 2013-12-20 19:28:08 +0100 | [diff] [blame] | 864 | const char *reference, QDict *options, int flags, |
| 865 | Error **errp) |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 866 | { |
Max Reitz | 72daa72 | 2013-12-20 19:28:08 +0100 | [diff] [blame] | 867 | BlockDriverState *bs = NULL; |
Christoph Hellwig | 6db9560 | 2010-04-05 16:53:57 +0200 | [diff] [blame] | 868 | BlockDriver *drv; |
Kevin Wolf | c2ad1b0 | 2013-03-18 16:40:51 +0100 | [diff] [blame] | 869 | const char *drvname; |
Kevin Wolf | 9828962 | 2013-07-10 15:47:39 +0200 | [diff] [blame] | 870 | bool allow_protocol_prefix = false; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 871 | Error *local_err = NULL; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 872 | int ret; |
| 873 | |
Kevin Wolf | 707ff82 | 2013-03-06 12:20:31 +0100 | [diff] [blame] | 874 | /* NULL means an empty set of options */ |
| 875 | if (options == NULL) { |
| 876 | options = qdict_new(); |
bellard | 3b0d4f6 | 2005-10-30 18:30:10 +0000 | [diff] [blame] | 877 | } |
Kevin Wolf | 707ff82 | 2013-03-06 12:20:31 +0100 | [diff] [blame] | 878 | |
Max Reitz | 72daa72 | 2013-12-20 19:28:08 +0100 | [diff] [blame] | 879 | if (reference) { |
| 880 | if (filename || qdict_size(options)) { |
| 881 | error_setg(errp, "Cannot reference an existing block device with " |
| 882 | "additional options or a new filename"); |
| 883 | return -EINVAL; |
| 884 | } |
| 885 | QDECREF(options); |
| 886 | |
| 887 | bs = bdrv_find(reference); |
| 888 | if (!bs) { |
| 889 | error_setg(errp, "Cannot find block device '%s'", reference); |
| 890 | return -ENODEV; |
| 891 | } |
| 892 | bdrv_ref(bs); |
| 893 | *pbs = bs; |
| 894 | return 0; |
| 895 | } |
| 896 | |
Kevin Wolf | 707ff82 | 2013-03-06 12:20:31 +0100 | [diff] [blame] | 897 | bs = bdrv_new(""); |
| 898 | bs->options = options; |
| 899 | options = qdict_clone_shallow(options); |
| 900 | |
Kevin Wolf | 035fccd | 2013-04-09 14:34:19 +0200 | [diff] [blame] | 901 | /* Fetch the file name from the options QDict if necessary */ |
| 902 | if (!filename) { |
| 903 | filename = qdict_get_try_str(options, "filename"); |
| 904 | } else if (filename && !qdict_haskey(options, "filename")) { |
| 905 | qdict_put(options, "filename", qstring_from_str(filename)); |
Kevin Wolf | 9828962 | 2013-07-10 15:47:39 +0200 | [diff] [blame] | 906 | allow_protocol_prefix = true; |
Kevin Wolf | 035fccd | 2013-04-09 14:34:19 +0200 | [diff] [blame] | 907 | } else { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 908 | error_setg(errp, "Can't specify 'file' and 'filename' options at the " |
| 909 | "same time"); |
Kevin Wolf | 035fccd | 2013-04-09 14:34:19 +0200 | [diff] [blame] | 910 | ret = -EINVAL; |
| 911 | goto fail; |
| 912 | } |
| 913 | |
Kevin Wolf | c2ad1b0 | 2013-03-18 16:40:51 +0100 | [diff] [blame] | 914 | /* Find the right block driver */ |
| 915 | drvname = qdict_get_try_str(options, "driver"); |
| 916 | if (drvname) { |
Kevin Wolf | 8f94a6e | 2013-10-10 11:45:55 +0200 | [diff] [blame] | 917 | drv = bdrv_find_format(drvname); |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 918 | if (!drv) { |
| 919 | error_setg(errp, "Unknown driver '%s'", drvname); |
| 920 | } |
Kevin Wolf | c2ad1b0 | 2013-03-18 16:40:51 +0100 | [diff] [blame] | 921 | qdict_del(options, "driver"); |
| 922 | } else if (filename) { |
Kevin Wolf | 9828962 | 2013-07-10 15:47:39 +0200 | [diff] [blame] | 923 | drv = bdrv_find_protocol(filename, allow_protocol_prefix); |
| 924 | if (!drv) { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 925 | error_setg(errp, "Unknown protocol"); |
Kevin Wolf | 9828962 | 2013-07-10 15:47:39 +0200 | [diff] [blame] | 926 | } |
Kevin Wolf | c2ad1b0 | 2013-03-18 16:40:51 +0100 | [diff] [blame] | 927 | } else { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 928 | error_setg(errp, "Must specify either driver or file"); |
Kevin Wolf | c2ad1b0 | 2013-03-18 16:40:51 +0100 | [diff] [blame] | 929 | drv = NULL; |
| 930 | } |
| 931 | |
| 932 | if (!drv) { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 933 | /* errp has been set already */ |
Kevin Wolf | c2ad1b0 | 2013-03-18 16:40:51 +0100 | [diff] [blame] | 934 | ret = -ENOENT; |
| 935 | goto fail; |
| 936 | } |
| 937 | |
| 938 | /* Parse the filename and open it */ |
| 939 | if (drv->bdrv_parse_filename && filename) { |
Kevin Wolf | 6963a30 | 2013-03-15 18:47:22 +0100 | [diff] [blame] | 940 | drv->bdrv_parse_filename(filename, options, &local_err); |
| 941 | if (error_is_set(&local_err)) { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 942 | error_propagate(errp, local_err); |
Kevin Wolf | 6963a30 | 2013-03-15 18:47:22 +0100 | [diff] [blame] | 943 | ret = -EINVAL; |
| 944 | goto fail; |
| 945 | } |
Kevin Wolf | 56d1b4d | 2013-04-12 20:02:37 +0200 | [diff] [blame] | 946 | qdict_del(options, "filename"); |
Benoît Canet | 030be32 | 2013-09-24 17:07:04 +0200 | [diff] [blame] | 947 | } else if (drv->bdrv_needs_filename && !filename) { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 948 | error_setg(errp, "The '%s' block driver requires a file name", |
| 949 | drv->format_name); |
Kevin Wolf | c2ad1b0 | 2013-03-18 16:40:51 +0100 | [diff] [blame] | 950 | ret = -EINVAL; |
| 951 | goto fail; |
Kevin Wolf | 6963a30 | 2013-03-15 18:47:22 +0100 | [diff] [blame] | 952 | } |
| 953 | |
Max Reitz | 505d758 | 2013-12-20 19:28:13 +0100 | [diff] [blame] | 954 | if (!drv->bdrv_file_open) { |
| 955 | ret = bdrv_open(bs, filename, options, flags, drv, &local_err); |
| 956 | options = NULL; |
| 957 | } else { |
| 958 | ret = bdrv_open_common(bs, NULL, options, flags, drv, &local_err); |
| 959 | } |
Kevin Wolf | 707ff82 | 2013-03-06 12:20:31 +0100 | [diff] [blame] | 960 | if (ret < 0) { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 961 | error_propagate(errp, local_err); |
Kevin Wolf | 707ff82 | 2013-03-06 12:20:31 +0100 | [diff] [blame] | 962 | goto fail; |
| 963 | } |
| 964 | |
| 965 | /* Check if any unknown options were used */ |
Max Reitz | 505d758 | 2013-12-20 19:28:13 +0100 | [diff] [blame] | 966 | if (options && (qdict_size(options) != 0)) { |
Kevin Wolf | 707ff82 | 2013-03-06 12:20:31 +0100 | [diff] [blame] | 967 | const QDictEntry *entry = qdict_first(options); |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 968 | error_setg(errp, "Block protocol '%s' doesn't support the option '%s'", |
| 969 | drv->format_name, entry->key); |
Kevin Wolf | 707ff82 | 2013-03-06 12:20:31 +0100 | [diff] [blame] | 970 | ret = -EINVAL; |
| 971 | goto fail; |
| 972 | } |
| 973 | QDECREF(options); |
| 974 | |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 975 | bs->growable = 1; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 976 | *pbs = bs; |
| 977 | return 0; |
Kevin Wolf | 707ff82 | 2013-03-06 12:20:31 +0100 | [diff] [blame] | 978 | |
| 979 | fail: |
| 980 | QDECREF(options); |
| 981 | if (!bs->drv) { |
| 982 | QDECREF(bs->options); |
| 983 | } |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 984 | bdrv_unref(bs); |
Kevin Wolf | 707ff82 | 2013-03-06 12:20:31 +0100 | [diff] [blame] | 985 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 986 | } |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 987 | |
Kevin Wolf | 31ca6d0 | 2013-03-28 15:29:24 +0100 | [diff] [blame] | 988 | /* |
| 989 | * Opens the backing file for a BlockDriverState if not yet open |
| 990 | * |
| 991 | * options is a QDict of options to pass to the block drivers, or NULL for an |
| 992 | * empty set of options. The reference to the QDict is transferred to this |
| 993 | * function (even on failure), so if the caller intends to reuse the dictionary, |
| 994 | * it needs to use QINCREF() before calling bdrv_file_open. |
| 995 | */ |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 996 | int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp) |
Paolo Bonzini | 9156df1 | 2012-10-18 16:49:17 +0200 | [diff] [blame] | 997 | { |
| 998 | char backing_filename[PATH_MAX]; |
| 999 | int back_flags, ret; |
| 1000 | BlockDriver *back_drv = NULL; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1001 | Error *local_err = NULL; |
Paolo Bonzini | 9156df1 | 2012-10-18 16:49:17 +0200 | [diff] [blame] | 1002 | |
| 1003 | if (bs->backing_hd != NULL) { |
Kevin Wolf | 31ca6d0 | 2013-03-28 15:29:24 +0100 | [diff] [blame] | 1004 | QDECREF(options); |
Paolo Bonzini | 9156df1 | 2012-10-18 16:49:17 +0200 | [diff] [blame] | 1005 | return 0; |
| 1006 | } |
| 1007 | |
Kevin Wolf | 31ca6d0 | 2013-03-28 15:29:24 +0100 | [diff] [blame] | 1008 | /* NULL means an empty set of options */ |
| 1009 | if (options == NULL) { |
| 1010 | options = qdict_new(); |
| 1011 | } |
| 1012 | |
Paolo Bonzini | 9156df1 | 2012-10-18 16:49:17 +0200 | [diff] [blame] | 1013 | bs->open_flags &= ~BDRV_O_NO_BACKING; |
Kevin Wolf | 1cb6f50 | 2013-04-12 20:27:07 +0200 | [diff] [blame] | 1014 | if (qdict_haskey(options, "file.filename")) { |
| 1015 | backing_filename[0] = '\0'; |
| 1016 | } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) { |
Kevin Wolf | 31ca6d0 | 2013-03-28 15:29:24 +0100 | [diff] [blame] | 1017 | QDECREF(options); |
Paolo Bonzini | 9156df1 | 2012-10-18 16:49:17 +0200 | [diff] [blame] | 1018 | return 0; |
Fam Zheng | dbecebd | 2013-09-22 20:05:06 +0800 | [diff] [blame] | 1019 | } else { |
| 1020 | bdrv_get_full_backing_filename(bs, backing_filename, |
| 1021 | sizeof(backing_filename)); |
Paolo Bonzini | 9156df1 | 2012-10-18 16:49:17 +0200 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | bs->backing_hd = bdrv_new(""); |
Paolo Bonzini | 9156df1 | 2012-10-18 16:49:17 +0200 | [diff] [blame] | 1025 | |
| 1026 | if (bs->backing_format[0] != '\0') { |
| 1027 | back_drv = bdrv_find_format(bs->backing_format); |
| 1028 | } |
| 1029 | |
| 1030 | /* backing files always opened read-only */ |
Thibaut LAURENT | 87a5deb | 2013-10-25 02:15:07 +0200 | [diff] [blame] | 1031 | back_flags = bs->open_flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | |
| 1032 | BDRV_O_COPY_ON_READ); |
Paolo Bonzini | 9156df1 | 2012-10-18 16:49:17 +0200 | [diff] [blame] | 1033 | |
Kevin Wolf | 31ca6d0 | 2013-03-28 15:29:24 +0100 | [diff] [blame] | 1034 | ret = bdrv_open(bs->backing_hd, |
| 1035 | *backing_filename ? backing_filename : NULL, options, |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1036 | back_flags, back_drv, &local_err); |
Paolo Bonzini | 9156df1 | 2012-10-18 16:49:17 +0200 | [diff] [blame] | 1037 | if (ret < 0) { |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 1038 | bdrv_unref(bs->backing_hd); |
Paolo Bonzini | 9156df1 | 2012-10-18 16:49:17 +0200 | [diff] [blame] | 1039 | bs->backing_hd = NULL; |
| 1040 | bs->open_flags |= BDRV_O_NO_BACKING; |
Fam Zheng | b04b6b6 | 2013-11-08 11:26:49 +0800 | [diff] [blame] | 1041 | error_setg(errp, "Could not open backing file: %s", |
| 1042 | error_get_pretty(local_err)); |
| 1043 | error_free(local_err); |
Paolo Bonzini | 9156df1 | 2012-10-18 16:49:17 +0200 | [diff] [blame] | 1044 | return ret; |
| 1045 | } |
Peter Feiner | d80ac65 | 2014-01-08 19:43:25 +0000 | [diff] [blame] | 1046 | |
| 1047 | if (bs->backing_hd->file) { |
| 1048 | pstrcpy(bs->backing_file, sizeof(bs->backing_file), |
| 1049 | bs->backing_hd->file->filename); |
| 1050 | } |
| 1051 | |
Paolo Bonzini | 9156df1 | 2012-10-18 16:49:17 +0200 | [diff] [blame] | 1052 | return 0; |
| 1053 | } |
| 1054 | |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 1055 | /* |
Max Reitz | da557aa | 2013-12-20 19:28:11 +0100 | [diff] [blame] | 1056 | * Opens a disk image whose options are given as BlockdevRef in another block |
| 1057 | * device's options. |
| 1058 | * |
| 1059 | * If force_raw is true, bdrv_file_open() will be used, thereby preventing any |
| 1060 | * image format auto-detection. If it is false and a filename is given, |
| 1061 | * bdrv_open() will be used for auto-detection. |
| 1062 | * |
| 1063 | * If allow_none is true, no image will be opened if filename is false and no |
| 1064 | * BlockdevRef is given. *pbs will remain unchanged and 0 will be returned. |
| 1065 | * |
| 1066 | * bdrev_key specifies the key for the image's BlockdevRef in the options QDict. |
| 1067 | * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict |
| 1068 | * itself, all options starting with "${bdref_key}." are considered part of the |
| 1069 | * BlockdevRef. |
| 1070 | * |
| 1071 | * The BlockdevRef will be removed from the options QDict. |
| 1072 | */ |
| 1073 | int bdrv_open_image(BlockDriverState **pbs, const char *filename, |
| 1074 | QDict *options, const char *bdref_key, int flags, |
| 1075 | bool force_raw, bool allow_none, Error **errp) |
| 1076 | { |
| 1077 | QDict *image_options; |
| 1078 | int ret; |
| 1079 | char *bdref_key_dot; |
| 1080 | const char *reference; |
| 1081 | |
| 1082 | bdref_key_dot = g_strdup_printf("%s.", bdref_key); |
| 1083 | qdict_extract_subqdict(options, &image_options, bdref_key_dot); |
| 1084 | g_free(bdref_key_dot); |
| 1085 | |
| 1086 | reference = qdict_get_try_str(options, bdref_key); |
| 1087 | if (!filename && !reference && !qdict_size(image_options)) { |
| 1088 | if (allow_none) { |
| 1089 | ret = 0; |
| 1090 | } else { |
| 1091 | error_setg(errp, "A block device must be specified for \"%s\"", |
| 1092 | bdref_key); |
| 1093 | ret = -EINVAL; |
| 1094 | } |
| 1095 | goto done; |
| 1096 | } |
| 1097 | |
| 1098 | if (filename && !force_raw) { |
| 1099 | /* If a filename is given and the block driver should be detected |
| 1100 | automatically (instead of using none), use bdrv_open() in order to do |
| 1101 | that auto-detection. */ |
| 1102 | BlockDriverState *bs; |
| 1103 | |
| 1104 | if (reference) { |
| 1105 | error_setg(errp, "Cannot reference an existing block device while " |
| 1106 | "giving a filename"); |
| 1107 | ret = -EINVAL; |
| 1108 | goto done; |
| 1109 | } |
| 1110 | |
| 1111 | bs = bdrv_new(""); |
| 1112 | ret = bdrv_open(bs, filename, image_options, flags, NULL, errp); |
| 1113 | if (ret < 0) { |
| 1114 | bdrv_unref(bs); |
| 1115 | } else { |
| 1116 | *pbs = bs; |
| 1117 | } |
| 1118 | } else { |
| 1119 | ret = bdrv_file_open(pbs, filename, reference, image_options, flags, |
| 1120 | errp); |
| 1121 | } |
| 1122 | |
| 1123 | done: |
| 1124 | qdict_del(options, bdref_key); |
| 1125 | return ret; |
| 1126 | } |
| 1127 | |
| 1128 | /* |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 1129 | * Opens a disk image (raw, qcow2, vmdk, ...) |
Kevin Wolf | de9c0ce | 2013-03-15 10:35:02 +0100 | [diff] [blame] | 1130 | * |
| 1131 | * options is a QDict of options to pass to the block drivers, or NULL for an |
| 1132 | * empty set of options. The reference to the QDict belongs to the block layer |
| 1133 | * after the call (even on failure), so if the caller intends to reuse the |
| 1134 | * dictionary, it needs to use QINCREF() before calling bdrv_open. |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 1135 | */ |
Kevin Wolf | de9c0ce | 2013-03-15 10:35:02 +0100 | [diff] [blame] | 1136 | int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1137 | int flags, BlockDriver *drv, Error **errp) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1138 | { |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 1139 | int ret; |
Stefan Weil | 89c9bc3 | 2012-11-22 07:25:48 +0100 | [diff] [blame] | 1140 | /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ |
| 1141 | char tmp_filename[PATH_MAX + 1]; |
Kevin Wolf | f500a6d | 2012-11-12 17:35:27 +0100 | [diff] [blame] | 1142 | BlockDriverState *file = NULL; |
Kevin Wolf | 74fe54f | 2013-07-09 11:09:02 +0200 | [diff] [blame] | 1143 | const char *drvname; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1144 | Error *local_err = NULL; |
bellard | 712e787 | 2005-04-28 21:09:32 +0000 | [diff] [blame] | 1145 | |
Kevin Wolf | de9c0ce | 2013-03-15 10:35:02 +0100 | [diff] [blame] | 1146 | /* NULL means an empty set of options */ |
| 1147 | if (options == NULL) { |
| 1148 | options = qdict_new(); |
| 1149 | } |
| 1150 | |
| 1151 | bs->options = options; |
Kevin Wolf | b6ad491 | 2013-03-15 10:35:04 +0100 | [diff] [blame] | 1152 | options = qdict_clone_shallow(options); |
Kevin Wolf | de9c0ce | 2013-03-15 10:35:02 +0100 | [diff] [blame] | 1153 | |
| 1154 | /* For snapshot=on, create a temporary qcow2 overlay */ |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1155 | if (flags & BDRV_O_SNAPSHOT) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1156 | BlockDriverState *bs1; |
| 1157 | int64_t total_size; |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 1158 | BlockDriver *bdrv_qcow2; |
Kevin Wolf | 08b392e | 2013-03-18 16:17:44 +0100 | [diff] [blame] | 1159 | QEMUOptionParameter *create_options; |
Kevin Wolf | 9fd3171 | 2013-11-14 15:37:12 +0100 | [diff] [blame] | 1160 | QDict *snapshot_options; |
Kevin Wolf | c2ad1b0 | 2013-03-18 16:40:51 +0100 | [diff] [blame] | 1161 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1162 | /* if snapshot, we create a temporary backing file and open it |
| 1163 | instead of opening 'filename' directly */ |
| 1164 | |
Kevin Wolf | 9fd3171 | 2013-11-14 15:37:12 +0100 | [diff] [blame] | 1165 | /* Get the required size from the image */ |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1166 | bs1 = bdrv_new(""); |
Kevin Wolf | 9fd3171 | 2013-11-14 15:37:12 +0100 | [diff] [blame] | 1167 | QINCREF(options); |
Kevin Wolf | c9fbb99 | 2013-11-28 11:58:02 +0100 | [diff] [blame] | 1168 | ret = bdrv_open(bs1, filename, options, BDRV_O_NO_BACKING, |
| 1169 | drv, &local_err); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 1170 | if (ret < 0) { |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 1171 | bdrv_unref(bs1); |
Kevin Wolf | de9c0ce | 2013-03-15 10:35:02 +0100 | [diff] [blame] | 1172 | goto fail; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1173 | } |
Jes Sorensen | 3e82990 | 2010-05-27 16:20:30 +0200 | [diff] [blame] | 1174 | total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; |
aliguori | 7c96d46 | 2008-09-12 17:54:13 +0000 | [diff] [blame] | 1175 | |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 1176 | bdrv_unref(bs1); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1177 | |
Kevin Wolf | 9fd3171 | 2013-11-14 15:37:12 +0100 | [diff] [blame] | 1178 | /* Create the temporary image */ |
Jim Meyering | eba2505 | 2012-05-28 09:27:54 +0200 | [diff] [blame] | 1179 | ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename)); |
| 1180 | if (ret < 0) { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1181 | error_setg_errno(errp, -ret, "Could not get temporary filename"); |
Kevin Wolf | de9c0ce | 2013-03-15 10:35:02 +0100 | [diff] [blame] | 1182 | goto fail; |
Jim Meyering | eba2505 | 2012-05-28 09:27:54 +0200 | [diff] [blame] | 1183 | } |
aliguori | 7c96d46 | 2008-09-12 17:54:13 +0000 | [diff] [blame] | 1184 | |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 1185 | bdrv_qcow2 = bdrv_find_format("qcow2"); |
Kevin Wolf | 08b392e | 2013-03-18 16:17:44 +0100 | [diff] [blame] | 1186 | create_options = parse_option_parameters("", bdrv_qcow2->create_options, |
| 1187 | NULL); |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 1188 | |
Kevin Wolf | 08b392e | 2013-03-18 16:17:44 +0100 | [diff] [blame] | 1189 | set_option_parameter_int(create_options, BLOCK_OPT_SIZE, total_size); |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 1190 | |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 1191 | ret = bdrv_create(bdrv_qcow2, tmp_filename, create_options, &local_err); |
Kevin Wolf | 08b392e | 2013-03-18 16:17:44 +0100 | [diff] [blame] | 1192 | free_option_parameters(create_options); |
aliguori | 51d7c00 | 2009-03-05 23:00:29 +0000 | [diff] [blame] | 1193 | if (ret < 0) { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1194 | error_setg_errno(errp, -ret, "Could not create temporary overlay " |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 1195 | "'%s': %s", tmp_filename, |
| 1196 | error_get_pretty(local_err)); |
| 1197 | error_free(local_err); |
| 1198 | local_err = NULL; |
Kevin Wolf | de9c0ce | 2013-03-15 10:35:02 +0100 | [diff] [blame] | 1199 | goto fail; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1200 | } |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 1201 | |
Kevin Wolf | 9fd3171 | 2013-11-14 15:37:12 +0100 | [diff] [blame] | 1202 | /* Prepare a new options QDict for the temporary file, where user |
| 1203 | * options refer to the backing file */ |
| 1204 | if (filename) { |
| 1205 | qdict_put(options, "file.filename", qstring_from_str(filename)); |
| 1206 | } |
| 1207 | if (drv) { |
| 1208 | qdict_put(options, "driver", qstring_from_str(drv->format_name)); |
| 1209 | } |
| 1210 | |
| 1211 | snapshot_options = qdict_new(); |
| 1212 | qdict_put(snapshot_options, "backing", options); |
| 1213 | qdict_flatten(snapshot_options); |
| 1214 | |
| 1215 | bs->options = snapshot_options; |
| 1216 | options = qdict_clone_shallow(bs->options); |
| 1217 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1218 | filename = tmp_filename; |
Kevin Wolf | 91a073a | 2009-05-27 14:48:06 +0200 | [diff] [blame] | 1219 | drv = bdrv_qcow2; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1220 | bs->is_temporary = 1; |
| 1221 | } |
bellard | 712e787 | 2005-04-28 21:09:32 +0000 | [diff] [blame] | 1222 | |
Kevin Wolf | f500a6d | 2012-11-12 17:35:27 +0100 | [diff] [blame] | 1223 | /* Open image file without format layer */ |
Jeff Cody | be028ad | 2012-09-20 15:13:17 -0400 | [diff] [blame] | 1224 | if (flags & BDRV_O_RDWR) { |
| 1225 | flags |= BDRV_O_ALLOW_RDWR; |
| 1226 | } |
| 1227 | |
Max Reitz | 054963f | 2013-12-20 19:28:12 +0100 | [diff] [blame] | 1228 | ret = bdrv_open_image(&file, filename, options, "file", |
| 1229 | bdrv_open_flags(bs, flags | BDRV_O_UNMAP), true, true, |
| 1230 | &local_err); |
| 1231 | if (ret < 0) { |
| 1232 | goto fail; |
Kevin Wolf | f500a6d | 2012-11-12 17:35:27 +0100 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | /* Find the right image format driver */ |
Kevin Wolf | 74fe54f | 2013-07-09 11:09:02 +0200 | [diff] [blame] | 1236 | drvname = qdict_get_try_str(options, "driver"); |
| 1237 | if (drvname) { |
Kevin Wolf | 8f94a6e | 2013-10-10 11:45:55 +0200 | [diff] [blame] | 1238 | drv = bdrv_find_format(drvname); |
Kevin Wolf | 74fe54f | 2013-07-09 11:09:02 +0200 | [diff] [blame] | 1239 | qdict_del(options, "driver"); |
Kevin Wolf | 06d22aa | 2013-08-08 17:44:52 +0200 | [diff] [blame] | 1240 | if (!drv) { |
| 1241 | error_setg(errp, "Invalid driver: '%s'", drvname); |
| 1242 | ret = -EINVAL; |
| 1243 | goto unlink_and_fail; |
| 1244 | } |
Kevin Wolf | 74fe54f | 2013-07-09 11:09:02 +0200 | [diff] [blame] | 1245 | } |
| 1246 | |
Kevin Wolf | f500a6d | 2012-11-12 17:35:27 +0100 | [diff] [blame] | 1247 | if (!drv) { |
Max Reitz | 2a05cbe | 2013-12-20 19:28:10 +0100 | [diff] [blame] | 1248 | if (file) { |
| 1249 | ret = find_image_format(file, filename, &drv, &local_err); |
| 1250 | } else { |
| 1251 | error_setg(errp, "Must specify either driver or file"); |
| 1252 | ret = -EINVAL; |
| 1253 | goto unlink_and_fail; |
| 1254 | } |
Kevin Wolf | f500a6d | 2012-11-12 17:35:27 +0100 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | if (!drv) { |
| 1258 | goto unlink_and_fail; |
| 1259 | } |
| 1260 | |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 1261 | /* Open the image */ |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1262 | ret = bdrv_open_common(bs, file, options, flags, drv, &local_err); |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 1263 | if (ret < 0) { |
Christoph Hellwig | 6987307 | 2010-01-20 18:13:25 +0100 | [diff] [blame] | 1264 | goto unlink_and_fail; |
| 1265 | } |
| 1266 | |
Max Reitz | 2a05cbe | 2013-12-20 19:28:10 +0100 | [diff] [blame] | 1267 | if (file && (bs->file != file)) { |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 1268 | bdrv_unref(file); |
Kevin Wolf | f500a6d | 2012-11-12 17:35:27 +0100 | [diff] [blame] | 1269 | file = NULL; |
| 1270 | } |
| 1271 | |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 1272 | /* If there is a backing file, use it */ |
Paolo Bonzini | 9156df1 | 2012-10-18 16:49:17 +0200 | [diff] [blame] | 1273 | if ((flags & BDRV_O_NO_BACKING) == 0) { |
Kevin Wolf | 31ca6d0 | 2013-03-28 15:29:24 +0100 | [diff] [blame] | 1274 | QDict *backing_options; |
| 1275 | |
Benoît Canet | 5726d87 | 2013-09-25 13:30:01 +0200 | [diff] [blame] | 1276 | qdict_extract_subqdict(options, &backing_options, "backing."); |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1277 | ret = bdrv_open_backing_file(bs, backing_options, &local_err); |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 1278 | if (ret < 0) { |
Kevin Wolf | b6ad491 | 2013-03-15 10:35:04 +0100 | [diff] [blame] | 1279 | goto close_and_fail; |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 1280 | } |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 1281 | } |
| 1282 | |
Kevin Wolf | b6ad491 | 2013-03-15 10:35:04 +0100 | [diff] [blame] | 1283 | /* Check if any unknown options were used */ |
| 1284 | if (qdict_size(options) != 0) { |
| 1285 | const QDictEntry *entry = qdict_first(options); |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1286 | error_setg(errp, "Block format '%s' used by device '%s' doesn't " |
| 1287 | "support the option '%s'", drv->format_name, bs->device_name, |
| 1288 | entry->key); |
Kevin Wolf | b6ad491 | 2013-03-15 10:35:04 +0100 | [diff] [blame] | 1289 | |
| 1290 | ret = -EINVAL; |
| 1291 | goto close_and_fail; |
| 1292 | } |
| 1293 | QDECREF(options); |
| 1294 | |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 1295 | if (!bdrv_key_required(bs)) { |
Markus Armbruster | 7d4b4ba | 2011-09-06 18:58:59 +0200 | [diff] [blame] | 1296 | bdrv_dev_change_media_cb(bs, true); |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | return 0; |
| 1300 | |
| 1301 | unlink_and_fail: |
Kevin Wolf | f500a6d | 2012-11-12 17:35:27 +0100 | [diff] [blame] | 1302 | if (file != NULL) { |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 1303 | bdrv_unref(file); |
Kevin Wolf | f500a6d | 2012-11-12 17:35:27 +0100 | [diff] [blame] | 1304 | } |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 1305 | if (bs->is_temporary) { |
| 1306 | unlink(filename); |
| 1307 | } |
Kevin Wolf | de9c0ce | 2013-03-15 10:35:02 +0100 | [diff] [blame] | 1308 | fail: |
| 1309 | QDECREF(bs->options); |
Kevin Wolf | b6ad491 | 2013-03-15 10:35:04 +0100 | [diff] [blame] | 1310 | QDECREF(options); |
Kevin Wolf | de9c0ce | 2013-03-15 10:35:02 +0100 | [diff] [blame] | 1311 | bs->options = NULL; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1312 | if (error_is_set(&local_err)) { |
| 1313 | error_propagate(errp, local_err); |
| 1314 | } |
Kevin Wolf | b6ad491 | 2013-03-15 10:35:04 +0100 | [diff] [blame] | 1315 | return ret; |
Kevin Wolf | de9c0ce | 2013-03-15 10:35:02 +0100 | [diff] [blame] | 1316 | |
Kevin Wolf | b6ad491 | 2013-03-15 10:35:04 +0100 | [diff] [blame] | 1317 | close_and_fail: |
| 1318 | bdrv_close(bs); |
| 1319 | QDECREF(options); |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1320 | if (error_is_set(&local_err)) { |
| 1321 | error_propagate(errp, local_err); |
| 1322 | } |
Kevin Wolf | b6ce07a | 2010-04-12 16:37:13 +0200 | [diff] [blame] | 1323 | return ret; |
| 1324 | } |
| 1325 | |
Jeff Cody | e971aa1 | 2012-09-20 15:13:19 -0400 | [diff] [blame] | 1326 | typedef struct BlockReopenQueueEntry { |
| 1327 | bool prepared; |
| 1328 | BDRVReopenState state; |
| 1329 | QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry; |
| 1330 | } BlockReopenQueueEntry; |
| 1331 | |
| 1332 | /* |
| 1333 | * Adds a BlockDriverState to a simple queue for an atomic, transactional |
| 1334 | * reopen of multiple devices. |
| 1335 | * |
| 1336 | * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT |
| 1337 | * already performed, or alternatively may be NULL a new BlockReopenQueue will |
| 1338 | * be created and initialized. This newly created BlockReopenQueue should be |
| 1339 | * passed back in for subsequent calls that are intended to be of the same |
| 1340 | * atomic 'set'. |
| 1341 | * |
| 1342 | * bs is the BlockDriverState to add to the reopen queue. |
| 1343 | * |
| 1344 | * flags contains the open flags for the associated bs |
| 1345 | * |
| 1346 | * returns a pointer to bs_queue, which is either the newly allocated |
| 1347 | * bs_queue, or the existing bs_queue being used. |
| 1348 | * |
| 1349 | */ |
| 1350 | BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, |
| 1351 | BlockDriverState *bs, int flags) |
| 1352 | { |
| 1353 | assert(bs != NULL); |
| 1354 | |
| 1355 | BlockReopenQueueEntry *bs_entry; |
| 1356 | if (bs_queue == NULL) { |
| 1357 | bs_queue = g_new0(BlockReopenQueue, 1); |
| 1358 | QSIMPLEQ_INIT(bs_queue); |
| 1359 | } |
| 1360 | |
| 1361 | if (bs->file) { |
| 1362 | bdrv_reopen_queue(bs_queue, bs->file, flags); |
| 1363 | } |
| 1364 | |
| 1365 | bs_entry = g_new0(BlockReopenQueueEntry, 1); |
| 1366 | QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry); |
| 1367 | |
| 1368 | bs_entry->state.bs = bs; |
| 1369 | bs_entry->state.flags = flags; |
| 1370 | |
| 1371 | return bs_queue; |
| 1372 | } |
| 1373 | |
| 1374 | /* |
| 1375 | * Reopen multiple BlockDriverStates atomically & transactionally. |
| 1376 | * |
| 1377 | * The queue passed in (bs_queue) must have been built up previous |
| 1378 | * via bdrv_reopen_queue(). |
| 1379 | * |
| 1380 | * Reopens all BDS specified in the queue, with the appropriate |
| 1381 | * flags. All devices are prepared for reopen, and failure of any |
| 1382 | * device will cause all device changes to be abandonded, and intermediate |
| 1383 | * data cleaned up. |
| 1384 | * |
| 1385 | * If all devices prepare successfully, then the changes are committed |
| 1386 | * to all devices. |
| 1387 | * |
| 1388 | */ |
| 1389 | int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp) |
| 1390 | { |
| 1391 | int ret = -1; |
| 1392 | BlockReopenQueueEntry *bs_entry, *next; |
| 1393 | Error *local_err = NULL; |
| 1394 | |
| 1395 | assert(bs_queue != NULL); |
| 1396 | |
| 1397 | bdrv_drain_all(); |
| 1398 | |
| 1399 | QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { |
| 1400 | if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) { |
| 1401 | error_propagate(errp, local_err); |
| 1402 | goto cleanup; |
| 1403 | } |
| 1404 | bs_entry->prepared = true; |
| 1405 | } |
| 1406 | |
| 1407 | /* If we reach this point, we have success and just need to apply the |
| 1408 | * changes |
| 1409 | */ |
| 1410 | QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { |
| 1411 | bdrv_reopen_commit(&bs_entry->state); |
| 1412 | } |
| 1413 | |
| 1414 | ret = 0; |
| 1415 | |
| 1416 | cleanup: |
| 1417 | QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { |
| 1418 | if (ret && bs_entry->prepared) { |
| 1419 | bdrv_reopen_abort(&bs_entry->state); |
| 1420 | } |
| 1421 | g_free(bs_entry); |
| 1422 | } |
| 1423 | g_free(bs_queue); |
| 1424 | return ret; |
| 1425 | } |
| 1426 | |
| 1427 | |
| 1428 | /* Reopen a single BlockDriverState with the specified flags. */ |
| 1429 | int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp) |
| 1430 | { |
| 1431 | int ret = -1; |
| 1432 | Error *local_err = NULL; |
| 1433 | BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, bdrv_flags); |
| 1434 | |
| 1435 | ret = bdrv_reopen_multiple(queue, &local_err); |
| 1436 | if (local_err != NULL) { |
| 1437 | error_propagate(errp, local_err); |
| 1438 | } |
| 1439 | return ret; |
| 1440 | } |
| 1441 | |
| 1442 | |
| 1443 | /* |
| 1444 | * Prepares a BlockDriverState for reopen. All changes are staged in the |
| 1445 | * 'opaque' field of the BDRVReopenState, which is used and allocated by |
| 1446 | * the block driver layer .bdrv_reopen_prepare() |
| 1447 | * |
| 1448 | * bs is the BlockDriverState to reopen |
| 1449 | * flags are the new open flags |
| 1450 | * queue is the reopen queue |
| 1451 | * |
| 1452 | * Returns 0 on success, non-zero on error. On error errp will be set |
| 1453 | * as well. |
| 1454 | * |
| 1455 | * On failure, bdrv_reopen_abort() will be called to clean up any data. |
| 1456 | * It is the responsibility of the caller to then call the abort() or |
| 1457 | * commit() for any other BDS that have been left in a prepare() state |
| 1458 | * |
| 1459 | */ |
| 1460 | int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, |
| 1461 | Error **errp) |
| 1462 | { |
| 1463 | int ret = -1; |
| 1464 | Error *local_err = NULL; |
| 1465 | BlockDriver *drv; |
| 1466 | |
| 1467 | assert(reopen_state != NULL); |
| 1468 | assert(reopen_state->bs->drv != NULL); |
| 1469 | drv = reopen_state->bs->drv; |
| 1470 | |
| 1471 | /* if we are to stay read-only, do not allow permission change |
| 1472 | * to r/w */ |
| 1473 | if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) && |
| 1474 | reopen_state->flags & BDRV_O_RDWR) { |
| 1475 | error_set(errp, QERR_DEVICE_IS_READ_ONLY, |
| 1476 | reopen_state->bs->device_name); |
| 1477 | goto error; |
| 1478 | } |
| 1479 | |
| 1480 | |
| 1481 | ret = bdrv_flush(reopen_state->bs); |
| 1482 | if (ret) { |
| 1483 | error_set(errp, ERROR_CLASS_GENERIC_ERROR, "Error (%s) flushing drive", |
| 1484 | strerror(-ret)); |
| 1485 | goto error; |
| 1486 | } |
| 1487 | |
| 1488 | if (drv->bdrv_reopen_prepare) { |
| 1489 | ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err); |
| 1490 | if (ret) { |
| 1491 | if (local_err != NULL) { |
| 1492 | error_propagate(errp, local_err); |
| 1493 | } else { |
Luiz Capitulino | d8b6895 | 2013-06-10 11:29:27 -0400 | [diff] [blame] | 1494 | error_setg(errp, "failed while preparing to reopen image '%s'", |
| 1495 | reopen_state->bs->filename); |
Jeff Cody | e971aa1 | 2012-09-20 15:13:19 -0400 | [diff] [blame] | 1496 | } |
| 1497 | goto error; |
| 1498 | } |
| 1499 | } else { |
| 1500 | /* It is currently mandatory to have a bdrv_reopen_prepare() |
| 1501 | * handler for each supported drv. */ |
| 1502 | error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, |
| 1503 | drv->format_name, reopen_state->bs->device_name, |
| 1504 | "reopening of file"); |
| 1505 | ret = -1; |
| 1506 | goto error; |
| 1507 | } |
| 1508 | |
| 1509 | ret = 0; |
| 1510 | |
| 1511 | error: |
| 1512 | return ret; |
| 1513 | } |
| 1514 | |
| 1515 | /* |
| 1516 | * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and |
| 1517 | * makes them final by swapping the staging BlockDriverState contents into |
| 1518 | * the active BlockDriverState contents. |
| 1519 | */ |
| 1520 | void bdrv_reopen_commit(BDRVReopenState *reopen_state) |
| 1521 | { |
| 1522 | BlockDriver *drv; |
| 1523 | |
| 1524 | assert(reopen_state != NULL); |
| 1525 | drv = reopen_state->bs->drv; |
| 1526 | assert(drv != NULL); |
| 1527 | |
| 1528 | /* If there are any driver level actions to take */ |
| 1529 | if (drv->bdrv_reopen_commit) { |
| 1530 | drv->bdrv_reopen_commit(reopen_state); |
| 1531 | } |
| 1532 | |
| 1533 | /* set BDS specific flags now */ |
| 1534 | reopen_state->bs->open_flags = reopen_state->flags; |
| 1535 | reopen_state->bs->enable_write_cache = !!(reopen_state->flags & |
| 1536 | BDRV_O_CACHE_WB); |
| 1537 | reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR); |
| 1538 | } |
| 1539 | |
| 1540 | /* |
| 1541 | * Abort the reopen, and delete and free the staged changes in |
| 1542 | * reopen_state |
| 1543 | */ |
| 1544 | void bdrv_reopen_abort(BDRVReopenState *reopen_state) |
| 1545 | { |
| 1546 | BlockDriver *drv; |
| 1547 | |
| 1548 | assert(reopen_state != NULL); |
| 1549 | drv = reopen_state->bs->drv; |
| 1550 | assert(drv != NULL); |
| 1551 | |
| 1552 | if (drv->bdrv_reopen_abort) { |
| 1553 | drv->bdrv_reopen_abort(reopen_state); |
| 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 1558 | void bdrv_close(BlockDriverState *bs) |
| 1559 | { |
Paolo Bonzini | 3cbc002 | 2012-10-19 11:36:48 +0200 | [diff] [blame] | 1560 | if (bs->job) { |
| 1561 | block_job_cancel_sync(bs->job); |
| 1562 | } |
Stefan Hajnoczi | 58fda17 | 2013-07-02 15:36:25 +0200 | [diff] [blame] | 1563 | bdrv_drain_all(); /* complete I/O */ |
| 1564 | bdrv_flush(bs); |
| 1565 | bdrv_drain_all(); /* in case flush left pending I/O */ |
Paolo Bonzini | d7d512f | 2012-08-23 11:20:36 +0200 | [diff] [blame] | 1566 | notifier_list_notify(&bs->close_notifiers, bs); |
Kevin Wolf | 7094f12 | 2012-04-11 11:06:37 +0200 | [diff] [blame] | 1567 | |
Paolo Bonzini | 3cbc002 | 2012-10-19 11:36:48 +0200 | [diff] [blame] | 1568 | if (bs->drv) { |
Stefan Hajnoczi | 557df6a | 2010-04-17 10:49:06 +0100 | [diff] [blame] | 1569 | if (bs->backing_hd) { |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 1570 | bdrv_unref(bs->backing_hd); |
Stefan Hajnoczi | 557df6a | 2010-04-17 10:49:06 +0100 | [diff] [blame] | 1571 | bs->backing_hd = NULL; |
| 1572 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1573 | bs->drv->bdrv_close(bs); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1574 | g_free(bs->opaque); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1575 | #ifdef _WIN32 |
| 1576 | if (bs->is_temporary) { |
| 1577 | unlink(bs->filename); |
| 1578 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1579 | #endif |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1580 | bs->opaque = NULL; |
| 1581 | bs->drv = NULL; |
Stefan Hajnoczi | 53fec9d | 2011-11-28 16:08:47 +0000 | [diff] [blame] | 1582 | bs->copy_on_read = 0; |
Paolo Bonzini | a275fa4 | 2012-05-08 16:51:43 +0200 | [diff] [blame] | 1583 | bs->backing_file[0] = '\0'; |
| 1584 | bs->backing_format[0] = '\0'; |
Paolo Bonzini | 6405875 | 2012-05-08 16:51:49 +0200 | [diff] [blame] | 1585 | bs->total_sectors = 0; |
| 1586 | bs->encrypted = 0; |
| 1587 | bs->valid_key = 0; |
| 1588 | bs->sg = 0; |
| 1589 | bs->growable = 0; |
Asias He | 0d51b4d | 2013-08-22 15:24:14 +0800 | [diff] [blame] | 1590 | bs->zero_beyond_eof = false; |
Kevin Wolf | de9c0ce | 2013-03-15 10:35:02 +0100 | [diff] [blame] | 1591 | QDECREF(bs->options); |
| 1592 | bs->options = NULL; |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1593 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 1594 | if (bs->file != NULL) { |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 1595 | bdrv_unref(bs->file); |
Paolo Bonzini | 0ac9377 | 2012-05-08 16:51:44 +0200 | [diff] [blame] | 1596 | bs->file = NULL; |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 1597 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1598 | } |
Zhi Yong Wu | 98f90db | 2011-11-08 13:00:14 +0800 | [diff] [blame] | 1599 | |
Pavel Hrdina | 9ca1115 | 2012-08-09 12:44:48 +0200 | [diff] [blame] | 1600 | bdrv_dev_change_media_cb(bs, false); |
| 1601 | |
Zhi Yong Wu | 98f90db | 2011-11-08 13:00:14 +0800 | [diff] [blame] | 1602 | /*throttling disk I/O limits*/ |
| 1603 | if (bs->io_limits_enabled) { |
| 1604 | bdrv_io_limits_disable(bs); |
| 1605 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
MORITA Kazutaka | 2bc93fe | 2010-05-28 11:44:57 +0900 | [diff] [blame] | 1608 | void bdrv_close_all(void) |
| 1609 | { |
| 1610 | BlockDriverState *bs; |
| 1611 | |
Benoît Canet | dc364f4 | 2014-01-23 21:31:32 +0100 | [diff] [blame^] | 1612 | QTAILQ_FOREACH(bs, &bdrv_states, device_list) { |
MORITA Kazutaka | 2bc93fe | 2010-05-28 11:44:57 +0900 | [diff] [blame] | 1613 | bdrv_close(bs); |
| 1614 | } |
| 1615 | } |
| 1616 | |
Stefan Hajnoczi | 88266f5 | 2013-04-11 15:41:13 +0200 | [diff] [blame] | 1617 | /* Check if any requests are in-flight (including throttled requests) */ |
| 1618 | static bool bdrv_requests_pending(BlockDriverState *bs) |
| 1619 | { |
| 1620 | if (!QLIST_EMPTY(&bs->tracked_requests)) { |
| 1621 | return true; |
| 1622 | } |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 1623 | if (!qemu_co_queue_empty(&bs->throttled_reqs[0])) { |
| 1624 | return true; |
| 1625 | } |
| 1626 | if (!qemu_co_queue_empty(&bs->throttled_reqs[1])) { |
Stefan Hajnoczi | 88266f5 | 2013-04-11 15:41:13 +0200 | [diff] [blame] | 1627 | return true; |
| 1628 | } |
| 1629 | if (bs->file && bdrv_requests_pending(bs->file)) { |
| 1630 | return true; |
| 1631 | } |
| 1632 | if (bs->backing_hd && bdrv_requests_pending(bs->backing_hd)) { |
| 1633 | return true; |
| 1634 | } |
| 1635 | return false; |
| 1636 | } |
| 1637 | |
| 1638 | static bool bdrv_requests_pending_all(void) |
| 1639 | { |
| 1640 | BlockDriverState *bs; |
Benoît Canet | dc364f4 | 2014-01-23 21:31:32 +0100 | [diff] [blame^] | 1641 | QTAILQ_FOREACH(bs, &bdrv_states, device_list) { |
Stefan Hajnoczi | 88266f5 | 2013-04-11 15:41:13 +0200 | [diff] [blame] | 1642 | if (bdrv_requests_pending(bs)) { |
| 1643 | return true; |
| 1644 | } |
| 1645 | } |
| 1646 | return false; |
| 1647 | } |
| 1648 | |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 1649 | /* |
| 1650 | * Wait for pending requests to complete across all BlockDriverStates |
| 1651 | * |
| 1652 | * This function does not flush data to disk, use bdrv_flush_all() for that |
| 1653 | * after calling this function. |
Zhi Yong Wu | 4c355d5 | 2012-04-12 14:00:57 +0200 | [diff] [blame] | 1654 | * |
| 1655 | * Note that completion of an asynchronous I/O operation can trigger any |
| 1656 | * number of other I/O operations on other devices---for example a coroutine |
| 1657 | * can be arbitrarily complex and a constant flow of I/O can come until the |
| 1658 | * coroutine is complete. Because of this, it is not possible to have a |
| 1659 | * function to drain a single device's I/O queue. |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 1660 | */ |
| 1661 | void bdrv_drain_all(void) |
| 1662 | { |
Stefan Hajnoczi | 88266f5 | 2013-04-11 15:41:13 +0200 | [diff] [blame] | 1663 | /* Always run first iteration so any pending completion BHs run */ |
| 1664 | bool busy = true; |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 1665 | BlockDriverState *bs; |
| 1666 | |
Stefan Hajnoczi | 88266f5 | 2013-04-11 15:41:13 +0200 | [diff] [blame] | 1667 | while (busy) { |
Benoît Canet | dc364f4 | 2014-01-23 21:31:32 +0100 | [diff] [blame^] | 1668 | QTAILQ_FOREACH(bs, &bdrv_states, device_list) { |
Stefan Hajnoczi | 0b06ef3 | 2013-11-26 16:18:00 +0100 | [diff] [blame] | 1669 | bdrv_start_throttled_reqs(bs); |
Zhi Yong Wu | 4c355d5 | 2012-04-12 14:00:57 +0200 | [diff] [blame] | 1670 | } |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 1671 | |
Stefan Hajnoczi | 88266f5 | 2013-04-11 15:41:13 +0200 | [diff] [blame] | 1672 | busy = bdrv_requests_pending_all(); |
| 1673 | busy |= aio_poll(qemu_get_aio_context(), busy); |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 1674 | } |
| 1675 | } |
| 1676 | |
Benoît Canet | dc364f4 | 2014-01-23 21:31:32 +0100 | [diff] [blame^] | 1677 | /* make a BlockDriverState anonymous by removing from bdrv_state and |
| 1678 | * graph_bdrv_state list. |
Ryan Harper | d22b2f4 | 2011-03-29 20:51:47 -0500 | [diff] [blame] | 1679 | Also, NULL terminate the device_name to prevent double remove */ |
| 1680 | void bdrv_make_anon(BlockDriverState *bs) |
| 1681 | { |
| 1682 | if (bs->device_name[0] != '\0') { |
Benoît Canet | dc364f4 | 2014-01-23 21:31:32 +0100 | [diff] [blame^] | 1683 | QTAILQ_REMOVE(&bdrv_states, bs, device_list); |
Ryan Harper | d22b2f4 | 2011-03-29 20:51:47 -0500 | [diff] [blame] | 1684 | } |
| 1685 | bs->device_name[0] = '\0'; |
Benoît Canet | dc364f4 | 2014-01-23 21:31:32 +0100 | [diff] [blame^] | 1686 | if (bs->node_name[0] != '\0') { |
| 1687 | QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list); |
| 1688 | } |
| 1689 | bs->node_name[0] = '\0'; |
Ryan Harper | d22b2f4 | 2011-03-29 20:51:47 -0500 | [diff] [blame] | 1690 | } |
| 1691 | |
Paolo Bonzini | e023b2e | 2012-05-08 16:51:41 +0200 | [diff] [blame] | 1692 | static void bdrv_rebind(BlockDriverState *bs) |
| 1693 | { |
| 1694 | if (bs->drv && bs->drv->bdrv_rebind) { |
| 1695 | bs->drv->bdrv_rebind(bs); |
| 1696 | } |
| 1697 | } |
| 1698 | |
Paolo Bonzini | 4ddc07c | 2012-06-14 16:55:02 +0200 | [diff] [blame] | 1699 | static void bdrv_move_feature_fields(BlockDriverState *bs_dest, |
| 1700 | BlockDriverState *bs_src) |
| 1701 | { |
| 1702 | /* move some fields that need to stay attached to the device */ |
| 1703 | bs_dest->open_flags = bs_src->open_flags; |
| 1704 | |
| 1705 | /* dev info */ |
| 1706 | bs_dest->dev_ops = bs_src->dev_ops; |
| 1707 | bs_dest->dev_opaque = bs_src->dev_opaque; |
| 1708 | bs_dest->dev = bs_src->dev; |
| 1709 | bs_dest->buffer_alignment = bs_src->buffer_alignment; |
| 1710 | bs_dest->copy_on_read = bs_src->copy_on_read; |
| 1711 | |
| 1712 | bs_dest->enable_write_cache = bs_src->enable_write_cache; |
| 1713 | |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 1714 | /* i/o throttled req */ |
| 1715 | memcpy(&bs_dest->throttle_state, |
| 1716 | &bs_src->throttle_state, |
| 1717 | sizeof(ThrottleState)); |
| 1718 | bs_dest->throttled_reqs[0] = bs_src->throttled_reqs[0]; |
| 1719 | bs_dest->throttled_reqs[1] = bs_src->throttled_reqs[1]; |
Paolo Bonzini | 4ddc07c | 2012-06-14 16:55:02 +0200 | [diff] [blame] | 1720 | bs_dest->io_limits_enabled = bs_src->io_limits_enabled; |
| 1721 | |
Paolo Bonzini | 4ddc07c | 2012-06-14 16:55:02 +0200 | [diff] [blame] | 1722 | /* r/w error */ |
| 1723 | bs_dest->on_read_error = bs_src->on_read_error; |
| 1724 | bs_dest->on_write_error = bs_src->on_write_error; |
| 1725 | |
| 1726 | /* i/o status */ |
| 1727 | bs_dest->iostatus_enabled = bs_src->iostatus_enabled; |
| 1728 | bs_dest->iostatus = bs_src->iostatus; |
| 1729 | |
| 1730 | /* dirty bitmap */ |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 1731 | bs_dest->dirty_bitmaps = bs_src->dirty_bitmaps; |
Paolo Bonzini | 4ddc07c | 2012-06-14 16:55:02 +0200 | [diff] [blame] | 1732 | |
Fam Zheng | 9fcb025 | 2013-08-23 09:14:46 +0800 | [diff] [blame] | 1733 | /* reference count */ |
| 1734 | bs_dest->refcnt = bs_src->refcnt; |
| 1735 | |
Paolo Bonzini | 4ddc07c | 2012-06-14 16:55:02 +0200 | [diff] [blame] | 1736 | /* job */ |
| 1737 | bs_dest->in_use = bs_src->in_use; |
| 1738 | bs_dest->job = bs_src->job; |
| 1739 | |
| 1740 | /* keep the same entry in bdrv_states */ |
| 1741 | pstrcpy(bs_dest->device_name, sizeof(bs_dest->device_name), |
| 1742 | bs_src->device_name); |
Benoît Canet | dc364f4 | 2014-01-23 21:31:32 +0100 | [diff] [blame^] | 1743 | bs_dest->device_list = bs_src->device_list; |
| 1744 | |
| 1745 | /* keep the same entry in graph_bdrv_states |
| 1746 | * We do want to swap name but don't want to swap linked list entries |
| 1747 | */ |
| 1748 | bs_dest->node_list = bs_src->node_list; |
Paolo Bonzini | 4ddc07c | 2012-06-14 16:55:02 +0200 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | /* |
| 1752 | * Swap bs contents for two image chains while they are live, |
| 1753 | * while keeping required fields on the BlockDriverState that is |
| 1754 | * actually attached to a device. |
| 1755 | * |
| 1756 | * This will modify the BlockDriverState fields, and swap contents |
| 1757 | * between bs_new and bs_old. Both bs_new and bs_old are modified. |
| 1758 | * |
| 1759 | * bs_new is required to be anonymous. |
| 1760 | * |
| 1761 | * This function does not create any image files. |
| 1762 | */ |
| 1763 | void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old) |
| 1764 | { |
| 1765 | BlockDriverState tmp; |
| 1766 | |
| 1767 | /* bs_new must be anonymous and shouldn't have anything fancy enabled */ |
| 1768 | assert(bs_new->device_name[0] == '\0'); |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 1769 | assert(QLIST_EMPTY(&bs_new->dirty_bitmaps)); |
Paolo Bonzini | 4ddc07c | 2012-06-14 16:55:02 +0200 | [diff] [blame] | 1770 | assert(bs_new->job == NULL); |
| 1771 | assert(bs_new->dev == NULL); |
| 1772 | assert(bs_new->in_use == 0); |
| 1773 | assert(bs_new->io_limits_enabled == false); |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 1774 | assert(!throttle_have_timer(&bs_new->throttle_state)); |
Paolo Bonzini | 4ddc07c | 2012-06-14 16:55:02 +0200 | [diff] [blame] | 1775 | |
| 1776 | tmp = *bs_new; |
| 1777 | *bs_new = *bs_old; |
| 1778 | *bs_old = tmp; |
| 1779 | |
| 1780 | /* there are some fields that should not be swapped, move them back */ |
| 1781 | bdrv_move_feature_fields(&tmp, bs_old); |
| 1782 | bdrv_move_feature_fields(bs_old, bs_new); |
| 1783 | bdrv_move_feature_fields(bs_new, &tmp); |
| 1784 | |
| 1785 | /* bs_new shouldn't be in bdrv_states even after the swap! */ |
| 1786 | assert(bs_new->device_name[0] == '\0'); |
| 1787 | |
| 1788 | /* Check a few fields that should remain attached to the device */ |
| 1789 | assert(bs_new->dev == NULL); |
| 1790 | assert(bs_new->job == NULL); |
| 1791 | assert(bs_new->in_use == 0); |
| 1792 | assert(bs_new->io_limits_enabled == false); |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 1793 | assert(!throttle_have_timer(&bs_new->throttle_state)); |
Paolo Bonzini | 4ddc07c | 2012-06-14 16:55:02 +0200 | [diff] [blame] | 1794 | |
| 1795 | bdrv_rebind(bs_new); |
| 1796 | bdrv_rebind(bs_old); |
| 1797 | } |
| 1798 | |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1799 | /* |
| 1800 | * Add new bs contents at the top of an image chain while the chain is |
| 1801 | * live, while keeping required fields on the top layer. |
| 1802 | * |
| 1803 | * This will modify the BlockDriverState fields, and swap contents |
| 1804 | * between bs_new and bs_top. Both bs_new and bs_top are modified. |
| 1805 | * |
Jeff Cody | f6801b8 | 2012-03-27 16:30:19 -0400 | [diff] [blame] | 1806 | * bs_new is required to be anonymous. |
| 1807 | * |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1808 | * This function does not create any image files. |
| 1809 | */ |
| 1810 | void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top) |
| 1811 | { |
Paolo Bonzini | 4ddc07c | 2012-06-14 16:55:02 +0200 | [diff] [blame] | 1812 | bdrv_swap(bs_new, bs_top); |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1813 | |
| 1814 | /* The contents of 'tmp' will become bs_top, as we are |
| 1815 | * swapping bs_new and bs_top contents. */ |
Paolo Bonzini | 4ddc07c | 2012-06-14 16:55:02 +0200 | [diff] [blame] | 1816 | bs_top->backing_hd = bs_new; |
| 1817 | bs_top->open_flags &= ~BDRV_O_NO_BACKING; |
| 1818 | pstrcpy(bs_top->backing_file, sizeof(bs_top->backing_file), |
| 1819 | bs_new->filename); |
| 1820 | pstrcpy(bs_top->backing_format, sizeof(bs_top->backing_format), |
| 1821 | bs_new->drv ? bs_new->drv->format_name : ""); |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1822 | } |
| 1823 | |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 1824 | static void bdrv_delete(BlockDriverState *bs) |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 1825 | { |
Markus Armbruster | fa879d6 | 2011-08-03 15:07:40 +0200 | [diff] [blame] | 1826 | assert(!bs->dev); |
Paolo Bonzini | 3e91465 | 2012-03-30 13:17:11 +0200 | [diff] [blame] | 1827 | assert(!bs->job); |
| 1828 | assert(!bs->in_use); |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 1829 | assert(!bs->refcnt); |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 1830 | assert(QLIST_EMPTY(&bs->dirty_bitmaps)); |
Markus Armbruster | 18846de | 2010-06-29 16:58:30 +0200 | [diff] [blame] | 1831 | |
Stefan Hajnoczi | e1b5c52 | 2013-06-27 15:32:26 +0200 | [diff] [blame] | 1832 | bdrv_close(bs); |
| 1833 | |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 1834 | /* remove from list, if necessary */ |
Ryan Harper | d22b2f4 | 2011-03-29 20:51:47 -0500 | [diff] [blame] | 1835 | bdrv_make_anon(bs); |
aurel32 | 34c6f05 | 2008-04-08 19:51:21 +0000 | [diff] [blame] | 1836 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1837 | g_free(bs); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
Markus Armbruster | fa879d6 | 2011-08-03 15:07:40 +0200 | [diff] [blame] | 1840 | int bdrv_attach_dev(BlockDriverState *bs, void *dev) |
| 1841 | /* TODO change to DeviceState *dev when all users are qdevified */ |
Markus Armbruster | 18846de | 2010-06-29 16:58:30 +0200 | [diff] [blame] | 1842 | { |
Markus Armbruster | fa879d6 | 2011-08-03 15:07:40 +0200 | [diff] [blame] | 1843 | if (bs->dev) { |
Markus Armbruster | 18846de | 2010-06-29 16:58:30 +0200 | [diff] [blame] | 1844 | return -EBUSY; |
| 1845 | } |
Markus Armbruster | fa879d6 | 2011-08-03 15:07:40 +0200 | [diff] [blame] | 1846 | bs->dev = dev; |
Luiz Capitulino | 28a7282 | 2011-09-26 17:43:50 -0300 | [diff] [blame] | 1847 | bdrv_iostatus_reset(bs); |
Markus Armbruster | 18846de | 2010-06-29 16:58:30 +0200 | [diff] [blame] | 1848 | return 0; |
| 1849 | } |
| 1850 | |
Markus Armbruster | fa879d6 | 2011-08-03 15:07:40 +0200 | [diff] [blame] | 1851 | /* TODO qdevified devices don't use this, remove when devices are qdevified */ |
| 1852 | void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev) |
Markus Armbruster | 18846de | 2010-06-29 16:58:30 +0200 | [diff] [blame] | 1853 | { |
Markus Armbruster | fa879d6 | 2011-08-03 15:07:40 +0200 | [diff] [blame] | 1854 | if (bdrv_attach_dev(bs, dev) < 0) { |
| 1855 | abort(); |
| 1856 | } |
| 1857 | } |
| 1858 | |
| 1859 | void bdrv_detach_dev(BlockDriverState *bs, void *dev) |
| 1860 | /* TODO change to DeviceState *dev when all users are qdevified */ |
| 1861 | { |
| 1862 | assert(bs->dev == dev); |
| 1863 | bs->dev = NULL; |
Markus Armbruster | 0e49de5 | 2011-08-03 15:07:41 +0200 | [diff] [blame] | 1864 | bs->dev_ops = NULL; |
| 1865 | bs->dev_opaque = NULL; |
Markus Armbruster | 29e05f2 | 2011-09-06 18:58:57 +0200 | [diff] [blame] | 1866 | bs->buffer_alignment = 512; |
Markus Armbruster | 18846de | 2010-06-29 16:58:30 +0200 | [diff] [blame] | 1867 | } |
| 1868 | |
Markus Armbruster | fa879d6 | 2011-08-03 15:07:40 +0200 | [diff] [blame] | 1869 | /* TODO change to return DeviceState * when all users are qdevified */ |
| 1870 | void *bdrv_get_attached_dev(BlockDriverState *bs) |
Markus Armbruster | 18846de | 2010-06-29 16:58:30 +0200 | [diff] [blame] | 1871 | { |
Markus Armbruster | fa879d6 | 2011-08-03 15:07:40 +0200 | [diff] [blame] | 1872 | return bs->dev; |
Markus Armbruster | 18846de | 2010-06-29 16:58:30 +0200 | [diff] [blame] | 1873 | } |
| 1874 | |
Markus Armbruster | 0e49de5 | 2011-08-03 15:07:41 +0200 | [diff] [blame] | 1875 | void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, |
| 1876 | void *opaque) |
| 1877 | { |
| 1878 | bs->dev_ops = ops; |
| 1879 | bs->dev_opaque = opaque; |
| 1880 | } |
| 1881 | |
Paolo Bonzini | 32c81a4 | 2012-09-28 17:22:58 +0200 | [diff] [blame] | 1882 | void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv, |
| 1883 | enum MonitorEvent ev, |
| 1884 | BlockErrorAction action, bool is_read) |
Luiz Capitulino | 329c0a4 | 2012-01-25 16:59:43 -0200 | [diff] [blame] | 1885 | { |
| 1886 | QObject *data; |
| 1887 | const char *action_str; |
| 1888 | |
| 1889 | switch (action) { |
| 1890 | case BDRV_ACTION_REPORT: |
| 1891 | action_str = "report"; |
| 1892 | break; |
| 1893 | case BDRV_ACTION_IGNORE: |
| 1894 | action_str = "ignore"; |
| 1895 | break; |
| 1896 | case BDRV_ACTION_STOP: |
| 1897 | action_str = "stop"; |
| 1898 | break; |
| 1899 | default: |
| 1900 | abort(); |
| 1901 | } |
| 1902 | |
| 1903 | data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", |
| 1904 | bdrv->device_name, |
| 1905 | action_str, |
| 1906 | is_read ? "read" : "write"); |
Paolo Bonzini | 32c81a4 | 2012-09-28 17:22:58 +0200 | [diff] [blame] | 1907 | monitor_protocol_event(ev, data); |
Luiz Capitulino | 329c0a4 | 2012-01-25 16:59:43 -0200 | [diff] [blame] | 1908 | |
| 1909 | qobject_decref(data); |
| 1910 | } |
| 1911 | |
Luiz Capitulino | 6f382ed | 2012-02-14 13:41:13 -0200 | [diff] [blame] | 1912 | static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected) |
| 1913 | { |
| 1914 | QObject *data; |
| 1915 | |
| 1916 | data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }", |
| 1917 | bdrv_get_device_name(bs), ejected); |
| 1918 | monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data); |
| 1919 | |
| 1920 | qobject_decref(data); |
| 1921 | } |
| 1922 | |
Markus Armbruster | 7d4b4ba | 2011-09-06 18:58:59 +0200 | [diff] [blame] | 1923 | static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load) |
Markus Armbruster | 0e49de5 | 2011-08-03 15:07:41 +0200 | [diff] [blame] | 1924 | { |
Markus Armbruster | 145feb1 | 2011-08-03 15:07:42 +0200 | [diff] [blame] | 1925 | if (bs->dev_ops && bs->dev_ops->change_media_cb) { |
Luiz Capitulino | 6f382ed | 2012-02-14 13:41:13 -0200 | [diff] [blame] | 1926 | bool tray_was_closed = !bdrv_dev_is_tray_open(bs); |
Markus Armbruster | 7d4b4ba | 2011-09-06 18:58:59 +0200 | [diff] [blame] | 1927 | bs->dev_ops->change_media_cb(bs->dev_opaque, load); |
Luiz Capitulino | 6f382ed | 2012-02-14 13:41:13 -0200 | [diff] [blame] | 1928 | if (tray_was_closed) { |
| 1929 | /* tray open */ |
| 1930 | bdrv_emit_qmp_eject_event(bs, true); |
| 1931 | } |
| 1932 | if (load) { |
| 1933 | /* tray close */ |
| 1934 | bdrv_emit_qmp_eject_event(bs, false); |
| 1935 | } |
Markus Armbruster | 145feb1 | 2011-08-03 15:07:42 +0200 | [diff] [blame] | 1936 | } |
| 1937 | } |
| 1938 | |
Markus Armbruster | 2c6942f | 2011-09-06 18:58:51 +0200 | [diff] [blame] | 1939 | bool bdrv_dev_has_removable_media(BlockDriverState *bs) |
| 1940 | { |
| 1941 | return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb); |
| 1942 | } |
| 1943 | |
Paolo Bonzini | 025ccaa | 2011-11-07 17:50:13 +0100 | [diff] [blame] | 1944 | void bdrv_dev_eject_request(BlockDriverState *bs, bool force) |
| 1945 | { |
| 1946 | if (bs->dev_ops && bs->dev_ops->eject_request_cb) { |
| 1947 | bs->dev_ops->eject_request_cb(bs->dev_opaque, force); |
| 1948 | } |
| 1949 | } |
| 1950 | |
Markus Armbruster | e4def80 | 2011-09-06 18:58:53 +0200 | [diff] [blame] | 1951 | bool bdrv_dev_is_tray_open(BlockDriverState *bs) |
| 1952 | { |
| 1953 | if (bs->dev_ops && bs->dev_ops->is_tray_open) { |
| 1954 | return bs->dev_ops->is_tray_open(bs->dev_opaque); |
| 1955 | } |
| 1956 | return false; |
| 1957 | } |
| 1958 | |
Markus Armbruster | 145feb1 | 2011-08-03 15:07:42 +0200 | [diff] [blame] | 1959 | static void bdrv_dev_resize_cb(BlockDriverState *bs) |
| 1960 | { |
| 1961 | if (bs->dev_ops && bs->dev_ops->resize_cb) { |
| 1962 | bs->dev_ops->resize_cb(bs->dev_opaque); |
Markus Armbruster | 0e49de5 | 2011-08-03 15:07:41 +0200 | [diff] [blame] | 1963 | } |
| 1964 | } |
| 1965 | |
Markus Armbruster | f107639 | 2011-09-06 18:58:46 +0200 | [diff] [blame] | 1966 | bool bdrv_dev_is_medium_locked(BlockDriverState *bs) |
| 1967 | { |
| 1968 | if (bs->dev_ops && bs->dev_ops->is_medium_locked) { |
| 1969 | return bs->dev_ops->is_medium_locked(bs->dev_opaque); |
| 1970 | } |
| 1971 | return false; |
| 1972 | } |
| 1973 | |
aliguori | e97fc19 | 2009-04-21 23:11:50 +0000 | [diff] [blame] | 1974 | /* |
| 1975 | * Run consistency checks on an image |
| 1976 | * |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 1977 | * 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] | 1978 | * 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] | 1979 | * check are stored in res. |
aliguori | e97fc19 | 2009-04-21 23:11:50 +0000 | [diff] [blame] | 1980 | */ |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 1981 | int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix) |
aliguori | e97fc19 | 2009-04-21 23:11:50 +0000 | [diff] [blame] | 1982 | { |
| 1983 | if (bs->drv->bdrv_check == NULL) { |
| 1984 | return -ENOTSUP; |
| 1985 | } |
| 1986 | |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 1987 | memset(res, 0, sizeof(*res)); |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 1988 | return bs->drv->bdrv_check(bs, res, fix); |
aliguori | e97fc19 | 2009-04-21 23:11:50 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
Kevin Wolf | 8a42661 | 2010-07-16 17:17:01 +0200 | [diff] [blame] | 1991 | #define COMMIT_BUF_SECTORS 2048 |
| 1992 | |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 1993 | /* commit COW file into the raw image */ |
| 1994 | int bdrv_commit(BlockDriverState *bs) |
| 1995 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1996 | BlockDriver *drv = bs->drv; |
Kevin Wolf | 8a42661 | 2010-07-16 17:17:01 +0200 | [diff] [blame] | 1997 | int64_t sector, total_sectors; |
| 1998 | int n, ro, open_flags; |
Jeff Cody | 0bce597 | 2012-09-20 15:13:34 -0400 | [diff] [blame] | 1999 | int ret = 0; |
Kevin Wolf | 8a42661 | 2010-07-16 17:17:01 +0200 | [diff] [blame] | 2000 | uint8_t *buf; |
Jim Meyering | c2cba3d | 2012-10-04 13:09:46 +0200 | [diff] [blame] | 2001 | char filename[PATH_MAX]; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 2002 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 2003 | if (!drv) |
| 2004 | return -ENOMEDIUM; |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 2005 | |
| 2006 | if (!bs->backing_hd) { |
| 2007 | return -ENOTSUP; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
Stefan Hajnoczi | 2d3735d | 2012-01-18 14:40:41 +0000 | [diff] [blame] | 2010 | if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) { |
| 2011 | return -EBUSY; |
| 2012 | } |
| 2013 | |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 2014 | ro = bs->backing_hd->read_only; |
Jim Meyering | c2cba3d | 2012-10-04 13:09:46 +0200 | [diff] [blame] | 2015 | /* Use pstrcpy (not strncpy): filename must be NUL-terminated. */ |
| 2016 | pstrcpy(filename, sizeof(filename), bs->backing_hd->filename); |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 2017 | open_flags = bs->backing_hd->open_flags; |
| 2018 | |
| 2019 | if (ro) { |
Jeff Cody | 0bce597 | 2012-09-20 15:13:34 -0400 | [diff] [blame] | 2020 | if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) { |
| 2021 | return -EACCES; |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 2022 | } |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 2023 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2024 | |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2025 | total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2026 | buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2027 | |
Kevin Wolf | 8a42661 | 2010-07-16 17:17:01 +0200 | [diff] [blame] | 2028 | for (sector = 0; sector < total_sectors; sector += n) { |
Paolo Bonzini | d663640 | 2013-09-04 19:00:25 +0200 | [diff] [blame] | 2029 | ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n); |
| 2030 | if (ret < 0) { |
| 2031 | goto ro_cleanup; |
| 2032 | } |
| 2033 | if (ret) { |
Kevin Wolf | 8a42661 | 2010-07-16 17:17:01 +0200 | [diff] [blame] | 2034 | if (bdrv_read(bs, sector, buf, n) != 0) { |
| 2035 | ret = -EIO; |
| 2036 | goto ro_cleanup; |
| 2037 | } |
| 2038 | |
| 2039 | if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { |
| 2040 | ret = -EIO; |
| 2041 | goto ro_cleanup; |
| 2042 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2043 | } |
| 2044 | } |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 2045 | |
Christoph Hellwig | 1d44952 | 2010-01-17 12:32:30 +0100 | [diff] [blame] | 2046 | if (drv->bdrv_make_empty) { |
| 2047 | ret = drv->bdrv_make_empty(bs); |
| 2048 | bdrv_flush(bs); |
| 2049 | } |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 2050 | |
Christoph Hellwig | 3f5075a | 2010-01-12 13:49:23 +0100 | [diff] [blame] | 2051 | /* |
| 2052 | * Make sure all data we wrote to the backing device is actually |
| 2053 | * stable on disk. |
| 2054 | */ |
| 2055 | if (bs->backing_hd) |
| 2056 | bdrv_flush(bs->backing_hd); |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 2057 | |
| 2058 | ro_cleanup: |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2059 | g_free(buf); |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 2060 | |
| 2061 | if (ro) { |
Jeff Cody | 0bce597 | 2012-09-20 15:13:34 -0400 | [diff] [blame] | 2062 | /* ignoring error return here */ |
| 2063 | bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL); |
Naphtali Sprei | 4dca4b6 | 2010-02-14 13:39:18 +0200 | [diff] [blame] | 2064 | } |
| 2065 | |
Christoph Hellwig | 1d44952 | 2010-01-17 12:32:30 +0100 | [diff] [blame] | 2066 | return ret; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Stefan Hajnoczi | e887749 | 2012-03-05 18:10:11 +0000 | [diff] [blame] | 2069 | int bdrv_commit_all(void) |
Markus Armbruster | 6ab4b5a | 2010-06-02 18:55:18 +0200 | [diff] [blame] | 2070 | { |
| 2071 | BlockDriverState *bs; |
| 2072 | |
Benoît Canet | dc364f4 | 2014-01-23 21:31:32 +0100 | [diff] [blame^] | 2073 | QTAILQ_FOREACH(bs, &bdrv_states, device_list) { |
Jeff Cody | 272d2d8 | 2013-02-26 09:55:48 -0500 | [diff] [blame] | 2074 | if (bs->drv && bs->backing_hd) { |
| 2075 | int ret = bdrv_commit(bs); |
| 2076 | if (ret < 0) { |
| 2077 | return ret; |
| 2078 | } |
Stefan Hajnoczi | e887749 | 2012-03-05 18:10:11 +0000 | [diff] [blame] | 2079 | } |
Markus Armbruster | 6ab4b5a | 2010-06-02 18:55:18 +0200 | [diff] [blame] | 2080 | } |
Stefan Hajnoczi | e887749 | 2012-03-05 18:10:11 +0000 | [diff] [blame] | 2081 | return 0; |
Markus Armbruster | 6ab4b5a | 2010-06-02 18:55:18 +0200 | [diff] [blame] | 2082 | } |
| 2083 | |
Stefan Hajnoczi | dbffbdc | 2011-11-17 13:40:27 +0000 | [diff] [blame] | 2084 | /** |
| 2085 | * Remove an active request from the tracked requests list |
| 2086 | * |
| 2087 | * This function should be called when a tracked request is completing. |
| 2088 | */ |
| 2089 | static void tracked_request_end(BdrvTrackedRequest *req) |
| 2090 | { |
| 2091 | QLIST_REMOVE(req, list); |
Stefan Hajnoczi | f465828 | 2011-11-17 13:40:29 +0000 | [diff] [blame] | 2092 | qemu_co_queue_restart_all(&req->wait_queue); |
Stefan Hajnoczi | dbffbdc | 2011-11-17 13:40:27 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
| 2095 | /** |
| 2096 | * Add an active request to the tracked requests list |
| 2097 | */ |
| 2098 | static void tracked_request_begin(BdrvTrackedRequest *req, |
| 2099 | BlockDriverState *bs, |
| 2100 | int64_t sector_num, |
| 2101 | int nb_sectors, bool is_write) |
| 2102 | { |
| 2103 | *req = (BdrvTrackedRequest){ |
| 2104 | .bs = bs, |
| 2105 | .sector_num = sector_num, |
| 2106 | .nb_sectors = nb_sectors, |
| 2107 | .is_write = is_write, |
Stefan Hajnoczi | 5f8b649 | 2011-11-30 12:23:42 +0000 | [diff] [blame] | 2108 | .co = qemu_coroutine_self(), |
Stefan Hajnoczi | dbffbdc | 2011-11-17 13:40:27 +0000 | [diff] [blame] | 2109 | }; |
| 2110 | |
Stefan Hajnoczi | f465828 | 2011-11-17 13:40:29 +0000 | [diff] [blame] | 2111 | qemu_co_queue_init(&req->wait_queue); |
| 2112 | |
Stefan Hajnoczi | dbffbdc | 2011-11-17 13:40:27 +0000 | [diff] [blame] | 2113 | QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); |
| 2114 | } |
| 2115 | |
Stefan Hajnoczi | d83947a | 2011-11-23 11:47:56 +0000 | [diff] [blame] | 2116 | /** |
| 2117 | * Round a region to cluster boundaries |
| 2118 | */ |
Paolo Bonzini | 343bded | 2013-01-21 17:09:42 +0100 | [diff] [blame] | 2119 | void bdrv_round_to_clusters(BlockDriverState *bs, |
| 2120 | int64_t sector_num, int nb_sectors, |
| 2121 | int64_t *cluster_sector_num, |
| 2122 | int *cluster_nb_sectors) |
Stefan Hajnoczi | d83947a | 2011-11-23 11:47:56 +0000 | [diff] [blame] | 2123 | { |
| 2124 | BlockDriverInfo bdi; |
| 2125 | |
| 2126 | if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { |
| 2127 | *cluster_sector_num = sector_num; |
| 2128 | *cluster_nb_sectors = nb_sectors; |
| 2129 | } else { |
| 2130 | int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE; |
| 2131 | *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c); |
| 2132 | *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num + |
| 2133 | nb_sectors, c); |
| 2134 | } |
| 2135 | } |
| 2136 | |
Stefan Hajnoczi | f465828 | 2011-11-17 13:40:29 +0000 | [diff] [blame] | 2137 | static bool tracked_request_overlaps(BdrvTrackedRequest *req, |
| 2138 | int64_t sector_num, int nb_sectors) { |
Stefan Hajnoczi | d83947a | 2011-11-23 11:47:56 +0000 | [diff] [blame] | 2139 | /* aaaa bbbb */ |
| 2140 | if (sector_num >= req->sector_num + req->nb_sectors) { |
| 2141 | return false; |
| 2142 | } |
| 2143 | /* bbbb aaaa */ |
| 2144 | if (req->sector_num >= sector_num + nb_sectors) { |
| 2145 | return false; |
| 2146 | } |
| 2147 | return true; |
Stefan Hajnoczi | f465828 | 2011-11-17 13:40:29 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs, |
| 2151 | int64_t sector_num, int nb_sectors) |
| 2152 | { |
| 2153 | BdrvTrackedRequest *req; |
Stefan Hajnoczi | d83947a | 2011-11-23 11:47:56 +0000 | [diff] [blame] | 2154 | int64_t cluster_sector_num; |
| 2155 | int cluster_nb_sectors; |
Stefan Hajnoczi | f465828 | 2011-11-17 13:40:29 +0000 | [diff] [blame] | 2156 | bool retry; |
| 2157 | |
Stefan Hajnoczi | d83947a | 2011-11-23 11:47:56 +0000 | [diff] [blame] | 2158 | /* If we touch the same cluster it counts as an overlap. This guarantees |
| 2159 | * that allocating writes will be serialized and not race with each other |
| 2160 | * for the same cluster. For example, in copy-on-read it ensures that the |
| 2161 | * CoR read and write operations are atomic and guest writes cannot |
| 2162 | * interleave between them. |
| 2163 | */ |
Paolo Bonzini | 343bded | 2013-01-21 17:09:42 +0100 | [diff] [blame] | 2164 | bdrv_round_to_clusters(bs, sector_num, nb_sectors, |
| 2165 | &cluster_sector_num, &cluster_nb_sectors); |
Stefan Hajnoczi | d83947a | 2011-11-23 11:47:56 +0000 | [diff] [blame] | 2166 | |
Stefan Hajnoczi | f465828 | 2011-11-17 13:40:29 +0000 | [diff] [blame] | 2167 | do { |
| 2168 | retry = false; |
| 2169 | QLIST_FOREACH(req, &bs->tracked_requests, list) { |
Stefan Hajnoczi | d83947a | 2011-11-23 11:47:56 +0000 | [diff] [blame] | 2170 | if (tracked_request_overlaps(req, cluster_sector_num, |
| 2171 | cluster_nb_sectors)) { |
Stefan Hajnoczi | 5f8b649 | 2011-11-30 12:23:42 +0000 | [diff] [blame] | 2172 | /* Hitting this means there was a reentrant request, for |
| 2173 | * example, a block driver issuing nested requests. This must |
| 2174 | * never happen since it means deadlock. |
| 2175 | */ |
| 2176 | assert(qemu_coroutine_self() != req->co); |
| 2177 | |
Stefan Hajnoczi | f465828 | 2011-11-17 13:40:29 +0000 | [diff] [blame] | 2178 | qemu_co_queue_wait(&req->wait_queue); |
| 2179 | retry = true; |
| 2180 | break; |
| 2181 | } |
| 2182 | } |
| 2183 | } while (retry); |
| 2184 | } |
| 2185 | |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 2186 | /* |
| 2187 | * Return values: |
| 2188 | * 0 - success |
| 2189 | * -EINVAL - backing format specified, but no file |
| 2190 | * -ENOSPC - can't update the backing file because no space is left in the |
| 2191 | * image file header |
| 2192 | * -ENOTSUP - format driver doesn't support changing the backing file |
| 2193 | */ |
| 2194 | int bdrv_change_backing_file(BlockDriverState *bs, |
| 2195 | const char *backing_file, const char *backing_fmt) |
| 2196 | { |
| 2197 | BlockDriver *drv = bs->drv; |
Paolo Bonzini | 469ef35 | 2012-04-12 14:01:02 +0200 | [diff] [blame] | 2198 | int ret; |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 2199 | |
Paolo Bonzini | 5f37779 | 2012-04-12 14:01:01 +0200 | [diff] [blame] | 2200 | /* Backing file format doesn't make sense without a backing file */ |
| 2201 | if (backing_fmt && !backing_file) { |
| 2202 | return -EINVAL; |
| 2203 | } |
| 2204 | |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 2205 | if (drv->bdrv_change_backing_file != NULL) { |
Paolo Bonzini | 469ef35 | 2012-04-12 14:01:02 +0200 | [diff] [blame] | 2206 | ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 2207 | } else { |
Paolo Bonzini | 469ef35 | 2012-04-12 14:01:02 +0200 | [diff] [blame] | 2208 | ret = -ENOTSUP; |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 2209 | } |
Paolo Bonzini | 469ef35 | 2012-04-12 14:01:02 +0200 | [diff] [blame] | 2210 | |
| 2211 | if (ret == 0) { |
| 2212 | pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); |
| 2213 | pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); |
| 2214 | } |
| 2215 | return ret; |
Kevin Wolf | 756e673 | 2010-01-12 12:55:17 +0100 | [diff] [blame] | 2216 | } |
| 2217 | |
Jeff Cody | 6ebdcee | 2012-09-27 13:29:12 -0400 | [diff] [blame] | 2218 | /* |
| 2219 | * Finds the image layer in the chain that has 'bs' as its backing file. |
| 2220 | * |
| 2221 | * active is the current topmost image. |
| 2222 | * |
| 2223 | * Returns NULL if bs is not found in active's image chain, |
| 2224 | * or if active == bs. |
| 2225 | */ |
| 2226 | BlockDriverState *bdrv_find_overlay(BlockDriverState *active, |
| 2227 | BlockDriverState *bs) |
| 2228 | { |
| 2229 | BlockDriverState *overlay = NULL; |
| 2230 | BlockDriverState *intermediate; |
| 2231 | |
| 2232 | assert(active != NULL); |
| 2233 | assert(bs != NULL); |
| 2234 | |
| 2235 | /* if bs is the same as active, then by definition it has no overlay |
| 2236 | */ |
| 2237 | if (active == bs) { |
| 2238 | return NULL; |
| 2239 | } |
| 2240 | |
| 2241 | intermediate = active; |
| 2242 | while (intermediate->backing_hd) { |
| 2243 | if (intermediate->backing_hd == bs) { |
| 2244 | overlay = intermediate; |
| 2245 | break; |
| 2246 | } |
| 2247 | intermediate = intermediate->backing_hd; |
| 2248 | } |
| 2249 | |
| 2250 | return overlay; |
| 2251 | } |
| 2252 | |
| 2253 | typedef struct BlkIntermediateStates { |
| 2254 | BlockDriverState *bs; |
| 2255 | QSIMPLEQ_ENTRY(BlkIntermediateStates) entry; |
| 2256 | } BlkIntermediateStates; |
| 2257 | |
| 2258 | |
| 2259 | /* |
| 2260 | * Drops images above 'base' up to and including 'top', and sets the image |
| 2261 | * above 'top' to have base as its backing file. |
| 2262 | * |
| 2263 | * Requires that the overlay to 'top' is opened r/w, so that the backing file |
| 2264 | * information in 'bs' can be properly updated. |
| 2265 | * |
| 2266 | * E.g., this will convert the following chain: |
| 2267 | * bottom <- base <- intermediate <- top <- active |
| 2268 | * |
| 2269 | * to |
| 2270 | * |
| 2271 | * bottom <- base <- active |
| 2272 | * |
| 2273 | * It is allowed for bottom==base, in which case it converts: |
| 2274 | * |
| 2275 | * base <- intermediate <- top <- active |
| 2276 | * |
| 2277 | * to |
| 2278 | * |
| 2279 | * base <- active |
| 2280 | * |
| 2281 | * Error conditions: |
| 2282 | * if active == top, that is considered an error |
| 2283 | * |
| 2284 | */ |
| 2285 | int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top, |
| 2286 | BlockDriverState *base) |
| 2287 | { |
| 2288 | BlockDriverState *intermediate; |
| 2289 | BlockDriverState *base_bs = NULL; |
| 2290 | BlockDriverState *new_top_bs = NULL; |
| 2291 | BlkIntermediateStates *intermediate_state, *next; |
| 2292 | int ret = -EIO; |
| 2293 | |
| 2294 | QSIMPLEQ_HEAD(states_to_delete, BlkIntermediateStates) states_to_delete; |
| 2295 | QSIMPLEQ_INIT(&states_to_delete); |
| 2296 | |
| 2297 | if (!top->drv || !base->drv) { |
| 2298 | goto exit; |
| 2299 | } |
| 2300 | |
| 2301 | new_top_bs = bdrv_find_overlay(active, top); |
| 2302 | |
| 2303 | if (new_top_bs == NULL) { |
| 2304 | /* we could not find the image above 'top', this is an error */ |
| 2305 | goto exit; |
| 2306 | } |
| 2307 | |
| 2308 | /* special case of new_top_bs->backing_hd already pointing to base - nothing |
| 2309 | * to do, no intermediate images */ |
| 2310 | if (new_top_bs->backing_hd == base) { |
| 2311 | ret = 0; |
| 2312 | goto exit; |
| 2313 | } |
| 2314 | |
| 2315 | intermediate = top; |
| 2316 | |
| 2317 | /* now we will go down through the list, and add each BDS we find |
| 2318 | * into our deletion queue, until we hit the 'base' |
| 2319 | */ |
| 2320 | while (intermediate) { |
| 2321 | intermediate_state = g_malloc0(sizeof(BlkIntermediateStates)); |
| 2322 | intermediate_state->bs = intermediate; |
| 2323 | QSIMPLEQ_INSERT_TAIL(&states_to_delete, intermediate_state, entry); |
| 2324 | |
| 2325 | if (intermediate->backing_hd == base) { |
| 2326 | base_bs = intermediate->backing_hd; |
| 2327 | break; |
| 2328 | } |
| 2329 | intermediate = intermediate->backing_hd; |
| 2330 | } |
| 2331 | if (base_bs == NULL) { |
| 2332 | /* something went wrong, we did not end at the base. safely |
| 2333 | * unravel everything, and exit with error */ |
| 2334 | goto exit; |
| 2335 | } |
| 2336 | |
| 2337 | /* success - we can delete the intermediate states, and link top->base */ |
| 2338 | ret = bdrv_change_backing_file(new_top_bs, base_bs->filename, |
| 2339 | base_bs->drv ? base_bs->drv->format_name : ""); |
| 2340 | if (ret) { |
| 2341 | goto exit; |
| 2342 | } |
| 2343 | new_top_bs->backing_hd = base_bs; |
| 2344 | |
| 2345 | |
| 2346 | QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { |
| 2347 | /* so that bdrv_close() does not recursively close the chain */ |
| 2348 | intermediate_state->bs->backing_hd = NULL; |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 2349 | bdrv_unref(intermediate_state->bs); |
Jeff Cody | 6ebdcee | 2012-09-27 13:29:12 -0400 | [diff] [blame] | 2350 | } |
| 2351 | ret = 0; |
| 2352 | |
| 2353 | exit: |
| 2354 | QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { |
| 2355 | g_free(intermediate_state); |
| 2356 | } |
| 2357 | return ret; |
| 2358 | } |
| 2359 | |
| 2360 | |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 2361 | static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, |
| 2362 | size_t size) |
| 2363 | { |
| 2364 | int64_t len; |
| 2365 | |
| 2366 | if (!bdrv_is_inserted(bs)) |
| 2367 | return -ENOMEDIUM; |
| 2368 | |
| 2369 | if (bs->growable) |
| 2370 | return 0; |
| 2371 | |
| 2372 | len = bdrv_getlength(bs); |
| 2373 | |
Kevin Wolf | fbb7b4e | 2009-05-08 14:47:24 +0200 | [diff] [blame] | 2374 | if (offset < 0) |
| 2375 | return -EIO; |
| 2376 | |
| 2377 | if ((offset > len) || (len - offset < size)) |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 2378 | return -EIO; |
| 2379 | |
| 2380 | return 0; |
| 2381 | } |
| 2382 | |
| 2383 | static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, |
| 2384 | int nb_sectors) |
| 2385 | { |
Jes Sorensen | eb5a316 | 2010-05-27 16:20:31 +0200 | [diff] [blame] | 2386 | return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, |
| 2387 | nb_sectors * BDRV_SECTOR_SIZE); |
aliguori | 71d0770 | 2009-03-03 17:37:16 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
Stefan Hajnoczi | 1c9805a | 2011-10-13 13:08:22 +0100 | [diff] [blame] | 2390 | typedef struct RwCo { |
| 2391 | BlockDriverState *bs; |
| 2392 | int64_t sector_num; |
| 2393 | int nb_sectors; |
| 2394 | QEMUIOVector *qiov; |
| 2395 | bool is_write; |
| 2396 | int ret; |
Peter Lieven | 4105eaa | 2013-07-11 14:16:22 +0200 | [diff] [blame] | 2397 | BdrvRequestFlags flags; |
Stefan Hajnoczi | 1c9805a | 2011-10-13 13:08:22 +0100 | [diff] [blame] | 2398 | } RwCo; |
| 2399 | |
| 2400 | static void coroutine_fn bdrv_rw_co_entry(void *opaque) |
| 2401 | { |
| 2402 | RwCo *rwco = opaque; |
| 2403 | |
| 2404 | if (!rwco->is_write) { |
| 2405 | rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num, |
Peter Lieven | 4105eaa | 2013-07-11 14:16:22 +0200 | [diff] [blame] | 2406 | rwco->nb_sectors, rwco->qiov, |
| 2407 | rwco->flags); |
Stefan Hajnoczi | 1c9805a | 2011-10-13 13:08:22 +0100 | [diff] [blame] | 2408 | } else { |
| 2409 | rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num, |
Peter Lieven | 4105eaa | 2013-07-11 14:16:22 +0200 | [diff] [blame] | 2410 | rwco->nb_sectors, rwco->qiov, |
| 2411 | rwco->flags); |
Stefan Hajnoczi | 1c9805a | 2011-10-13 13:08:22 +0100 | [diff] [blame] | 2412 | } |
| 2413 | } |
| 2414 | |
| 2415 | /* |
Kevin Wolf | 8d3b1a2 | 2013-04-05 21:27:55 +0200 | [diff] [blame] | 2416 | * Process a vectored synchronous request using coroutines |
Stefan Hajnoczi | 1c9805a | 2011-10-13 13:08:22 +0100 | [diff] [blame] | 2417 | */ |
Kevin Wolf | 8d3b1a2 | 2013-04-05 21:27:55 +0200 | [diff] [blame] | 2418 | static int bdrv_rwv_co(BlockDriverState *bs, int64_t sector_num, |
Peter Lieven | 4105eaa | 2013-07-11 14:16:22 +0200 | [diff] [blame] | 2419 | QEMUIOVector *qiov, bool is_write, |
| 2420 | BdrvRequestFlags flags) |
Stefan Hajnoczi | 1c9805a | 2011-10-13 13:08:22 +0100 | [diff] [blame] | 2421 | { |
Stefan Hajnoczi | 1c9805a | 2011-10-13 13:08:22 +0100 | [diff] [blame] | 2422 | Coroutine *co; |
| 2423 | RwCo rwco = { |
| 2424 | .bs = bs, |
| 2425 | .sector_num = sector_num, |
Kevin Wolf | 8d3b1a2 | 2013-04-05 21:27:55 +0200 | [diff] [blame] | 2426 | .nb_sectors = qiov->size >> BDRV_SECTOR_BITS, |
| 2427 | .qiov = qiov, |
Stefan Hajnoczi | 1c9805a | 2011-10-13 13:08:22 +0100 | [diff] [blame] | 2428 | .is_write = is_write, |
| 2429 | .ret = NOT_DONE, |
Peter Lieven | 4105eaa | 2013-07-11 14:16:22 +0200 | [diff] [blame] | 2430 | .flags = flags, |
Stefan Hajnoczi | 1c9805a | 2011-10-13 13:08:22 +0100 | [diff] [blame] | 2431 | }; |
Kevin Wolf | 8d3b1a2 | 2013-04-05 21:27:55 +0200 | [diff] [blame] | 2432 | assert((qiov->size & (BDRV_SECTOR_SIZE - 1)) == 0); |
Stefan Hajnoczi | 1c9805a | 2011-10-13 13:08:22 +0100 | [diff] [blame] | 2433 | |
Zhi Yong Wu | 498e386 | 2012-04-02 18:59:34 +0800 | [diff] [blame] | 2434 | /** |
| 2435 | * In sync call context, when the vcpu is blocked, this throttling timer |
| 2436 | * will not fire; so the I/O throttling function has to be disabled here |
| 2437 | * if it has been enabled. |
| 2438 | */ |
| 2439 | if (bs->io_limits_enabled) { |
| 2440 | fprintf(stderr, "Disabling I/O throttling on '%s' due " |
| 2441 | "to synchronous I/O.\n", bdrv_get_device_name(bs)); |
| 2442 | bdrv_io_limits_disable(bs); |
| 2443 | } |
| 2444 | |
Stefan Hajnoczi | 1c9805a | 2011-10-13 13:08:22 +0100 | [diff] [blame] | 2445 | if (qemu_in_coroutine()) { |
| 2446 | /* Fast-path if already in coroutine context */ |
| 2447 | bdrv_rw_co_entry(&rwco); |
| 2448 | } else { |
| 2449 | co = qemu_coroutine_create(bdrv_rw_co_entry); |
| 2450 | qemu_coroutine_enter(co, &rwco); |
| 2451 | while (rwco.ret == NOT_DONE) { |
| 2452 | qemu_aio_wait(); |
| 2453 | } |
| 2454 | } |
| 2455 | return rwco.ret; |
| 2456 | } |
| 2457 | |
Kevin Wolf | 8d3b1a2 | 2013-04-05 21:27:55 +0200 | [diff] [blame] | 2458 | /* |
| 2459 | * Process a synchronous request using coroutines |
| 2460 | */ |
| 2461 | static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, |
Peter Lieven | 4105eaa | 2013-07-11 14:16:22 +0200 | [diff] [blame] | 2462 | int nb_sectors, bool is_write, BdrvRequestFlags flags) |
Kevin Wolf | 8d3b1a2 | 2013-04-05 21:27:55 +0200 | [diff] [blame] | 2463 | { |
| 2464 | QEMUIOVector qiov; |
| 2465 | struct iovec iov = { |
| 2466 | .iov_base = (void *)buf, |
| 2467 | .iov_len = nb_sectors * BDRV_SECTOR_SIZE, |
| 2468 | }; |
| 2469 | |
| 2470 | qemu_iovec_init_external(&qiov, &iov, 1); |
Peter Lieven | 4105eaa | 2013-07-11 14:16:22 +0200 | [diff] [blame] | 2471 | return bdrv_rwv_co(bs, sector_num, &qiov, is_write, flags); |
Kevin Wolf | 8d3b1a2 | 2013-04-05 21:27:55 +0200 | [diff] [blame] | 2472 | } |
| 2473 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 2474 | /* return < 0 if error. See bdrv_write() for the return codes */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2475 | int bdrv_read(BlockDriverState *bs, int64_t sector_num, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 2476 | uint8_t *buf, int nb_sectors) |
| 2477 | { |
Peter Lieven | 4105eaa | 2013-07-11 14:16:22 +0200 | [diff] [blame] | 2478 | return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false, 0); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
Markus Armbruster | 07d27a4 | 2012-06-29 17:34:29 +0200 | [diff] [blame] | 2481 | /* Just like bdrv_read(), but with I/O throttling temporarily disabled */ |
| 2482 | int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num, |
| 2483 | uint8_t *buf, int nb_sectors) |
| 2484 | { |
| 2485 | bool enabled; |
| 2486 | int ret; |
| 2487 | |
| 2488 | enabled = bs->io_limits_enabled; |
| 2489 | bs->io_limits_enabled = false; |
Peter Lieven | 4e7395e | 2013-07-18 10:37:32 +0200 | [diff] [blame] | 2490 | ret = bdrv_read(bs, sector_num, buf, nb_sectors); |
Markus Armbruster | 07d27a4 | 2012-06-29 17:34:29 +0200 | [diff] [blame] | 2491 | bs->io_limits_enabled = enabled; |
| 2492 | return ret; |
| 2493 | } |
| 2494 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2495 | /* Return < 0 if error. Important errors are: |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 2496 | -EIO generic I/O error (may happen for all errors) |
| 2497 | -ENOMEDIUM No media inserted. |
| 2498 | -EINVAL Invalid sector number or nb_sectors |
| 2499 | -EACCES Trying to write a read-only device |
| 2500 | */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2501 | int bdrv_write(BlockDriverState *bs, int64_t sector_num, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 2502 | const uint8_t *buf, int nb_sectors) |
| 2503 | { |
Peter Lieven | 4105eaa | 2013-07-11 14:16:22 +0200 | [diff] [blame] | 2504 | return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true, 0); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
Kevin Wolf | 8d3b1a2 | 2013-04-05 21:27:55 +0200 | [diff] [blame] | 2507 | int bdrv_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov) |
| 2508 | { |
Peter Lieven | 4105eaa | 2013-07-11 14:16:22 +0200 | [diff] [blame] | 2509 | return bdrv_rwv_co(bs, sector_num, qiov, true, 0); |
| 2510 | } |
| 2511 | |
Peter Lieven | aa7bfbf | 2013-10-24 12:06:51 +0200 | [diff] [blame] | 2512 | int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num, |
| 2513 | int nb_sectors, BdrvRequestFlags flags) |
Peter Lieven | 4105eaa | 2013-07-11 14:16:22 +0200 | [diff] [blame] | 2514 | { |
| 2515 | return bdrv_rw_co(bs, sector_num, NULL, nb_sectors, true, |
Peter Lieven | aa7bfbf | 2013-10-24 12:06:51 +0200 | [diff] [blame] | 2516 | BDRV_REQ_ZERO_WRITE | flags); |
Kevin Wolf | 8d3b1a2 | 2013-04-05 21:27:55 +0200 | [diff] [blame] | 2517 | } |
| 2518 | |
Peter Lieven | d75cbb5 | 2013-10-24 12:07:03 +0200 | [diff] [blame] | 2519 | /* |
| 2520 | * Completely zero out a block device with the help of bdrv_write_zeroes. |
| 2521 | * The operation is sped up by checking the block status and only writing |
| 2522 | * zeroes to the device if they currently do not return zeroes. Optional |
| 2523 | * flags are passed through to bdrv_write_zeroes (e.g. BDRV_REQ_MAY_UNMAP). |
| 2524 | * |
| 2525 | * Returns < 0 on error, 0 on success. For error codes see bdrv_write(). |
| 2526 | */ |
| 2527 | int bdrv_make_zero(BlockDriverState *bs, BdrvRequestFlags flags) |
| 2528 | { |
| 2529 | int64_t target_size = bdrv_getlength(bs) / BDRV_SECTOR_SIZE; |
| 2530 | int64_t ret, nb_sectors, sector_num = 0; |
| 2531 | int n; |
| 2532 | |
| 2533 | for (;;) { |
| 2534 | nb_sectors = target_size - sector_num; |
| 2535 | if (nb_sectors <= 0) { |
| 2536 | return 0; |
| 2537 | } |
| 2538 | if (nb_sectors > INT_MAX) { |
| 2539 | nb_sectors = INT_MAX; |
| 2540 | } |
| 2541 | ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &n); |
Peter Lieven | 3d94ce6 | 2013-12-12 13:57:05 +0100 | [diff] [blame] | 2542 | if (ret < 0) { |
| 2543 | error_report("error getting block status at sector %" PRId64 ": %s", |
| 2544 | sector_num, strerror(-ret)); |
| 2545 | return ret; |
| 2546 | } |
Peter Lieven | d75cbb5 | 2013-10-24 12:07:03 +0200 | [diff] [blame] | 2547 | if (ret & BDRV_BLOCK_ZERO) { |
| 2548 | sector_num += n; |
| 2549 | continue; |
| 2550 | } |
| 2551 | ret = bdrv_write_zeroes(bs, sector_num, n, flags); |
| 2552 | if (ret < 0) { |
| 2553 | error_report("error writing zeroes at sector %" PRId64 ": %s", |
| 2554 | sector_num, strerror(-ret)); |
| 2555 | return ret; |
| 2556 | } |
| 2557 | sector_num += n; |
| 2558 | } |
| 2559 | } |
| 2560 | |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 2561 | int bdrv_pread(BlockDriverState *bs, int64_t offset, |
| 2562 | void *buf, int count1) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2563 | { |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2564 | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2565 | int len, nb_sectors, count; |
| 2566 | int64_t sector_num; |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 2567 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2568 | |
| 2569 | count = count1; |
| 2570 | /* first read to align to sector start */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2571 | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2572 | if (len > count) |
| 2573 | len = count; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2574 | sector_num = offset >> BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2575 | if (len > 0) { |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 2576 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
| 2577 | return ret; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2578 | memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2579 | count -= len; |
| 2580 | if (count == 0) |
| 2581 | return count1; |
| 2582 | sector_num++; |
| 2583 | buf += len; |
| 2584 | } |
| 2585 | |
| 2586 | /* read the sectors "in place" */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2587 | nb_sectors = count >> BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2588 | if (nb_sectors > 0) { |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 2589 | if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) |
| 2590 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2591 | sector_num += nb_sectors; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2592 | len = nb_sectors << BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2593 | buf += len; |
| 2594 | count -= len; |
| 2595 | } |
| 2596 | |
| 2597 | /* add data from the last sector */ |
| 2598 | if (count > 0) { |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 2599 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
| 2600 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2601 | memcpy(buf, tmp_buf, count); |
| 2602 | } |
| 2603 | return count1; |
| 2604 | } |
| 2605 | |
Kevin Wolf | 8d3b1a2 | 2013-04-05 21:27:55 +0200 | [diff] [blame] | 2606 | int bdrv_pwritev(BlockDriverState *bs, int64_t offset, QEMUIOVector *qiov) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2607 | { |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2608 | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2609 | int len, nb_sectors, count; |
| 2610 | int64_t sector_num; |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 2611 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2612 | |
Kevin Wolf | 8d3b1a2 | 2013-04-05 21:27:55 +0200 | [diff] [blame] | 2613 | count = qiov->size; |
| 2614 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2615 | /* first write to align to sector start */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2616 | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2617 | if (len > count) |
| 2618 | len = count; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2619 | sector_num = offset >> BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2620 | if (len > 0) { |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 2621 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
| 2622 | return ret; |
Kevin Wolf | 8d3b1a2 | 2013-04-05 21:27:55 +0200 | [diff] [blame] | 2623 | qemu_iovec_to_buf(qiov, 0, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), |
| 2624 | len); |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 2625 | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
| 2626 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2627 | count -= len; |
| 2628 | if (count == 0) |
Kevin Wolf | 8d3b1a2 | 2013-04-05 21:27:55 +0200 | [diff] [blame] | 2629 | return qiov->size; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2630 | sector_num++; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
| 2633 | /* write the sectors "in place" */ |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2634 | nb_sectors = count >> BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2635 | if (nb_sectors > 0) { |
Kevin Wolf | 8d3b1a2 | 2013-04-05 21:27:55 +0200 | [diff] [blame] | 2636 | QEMUIOVector qiov_inplace; |
| 2637 | |
| 2638 | qemu_iovec_init(&qiov_inplace, qiov->niov); |
| 2639 | qemu_iovec_concat(&qiov_inplace, qiov, len, |
| 2640 | nb_sectors << BDRV_SECTOR_BITS); |
| 2641 | ret = bdrv_writev(bs, sector_num, &qiov_inplace); |
| 2642 | qemu_iovec_destroy(&qiov_inplace); |
| 2643 | if (ret < 0) { |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 2644 | return ret; |
Kevin Wolf | 8d3b1a2 | 2013-04-05 21:27:55 +0200 | [diff] [blame] | 2645 | } |
| 2646 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2647 | sector_num += nb_sectors; |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 2648 | len = nb_sectors << BDRV_SECTOR_BITS; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2649 | count -= len; |
| 2650 | } |
| 2651 | |
| 2652 | /* add data from the last sector */ |
| 2653 | if (count > 0) { |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 2654 | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
| 2655 | return ret; |
Kevin Wolf | 8d3b1a2 | 2013-04-05 21:27:55 +0200 | [diff] [blame] | 2656 | qemu_iovec_to_buf(qiov, qiov->size - count, tmp_buf, count); |
Kevin Wolf | 9a8c4cc | 2010-01-20 15:03:02 +0100 | [diff] [blame] | 2657 | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
| 2658 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2659 | } |
Kevin Wolf | 8d3b1a2 | 2013-04-05 21:27:55 +0200 | [diff] [blame] | 2660 | return qiov->size; |
| 2661 | } |
| 2662 | |
| 2663 | int bdrv_pwrite(BlockDriverState *bs, int64_t offset, |
| 2664 | const void *buf, int count1) |
| 2665 | { |
| 2666 | QEMUIOVector qiov; |
| 2667 | struct iovec iov = { |
| 2668 | .iov_base = (void *) buf, |
| 2669 | .iov_len = count1, |
| 2670 | }; |
| 2671 | |
| 2672 | qemu_iovec_init_external(&qiov, &iov, 1); |
| 2673 | return bdrv_pwritev(bs, offset, &qiov); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2674 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 2675 | |
Kevin Wolf | f08145f | 2010-06-16 16:38:15 +0200 | [diff] [blame] | 2676 | /* |
| 2677 | * Writes to the file and ensures that no writes are reordered across this |
| 2678 | * request (acts as a barrier) |
| 2679 | * |
| 2680 | * Returns 0 on success, -errno in error cases. |
| 2681 | */ |
| 2682 | int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, |
| 2683 | const void *buf, int count) |
| 2684 | { |
| 2685 | int ret; |
| 2686 | |
| 2687 | ret = bdrv_pwrite(bs, offset, buf, count); |
| 2688 | if (ret < 0) { |
| 2689 | return ret; |
| 2690 | } |
| 2691 | |
Paolo Bonzini | f05fa4a | 2012-06-06 00:04:49 +0200 | [diff] [blame] | 2692 | /* No flush needed for cache modes that already do it */ |
| 2693 | if (bs->enable_write_cache) { |
Kevin Wolf | f08145f | 2010-06-16 16:38:15 +0200 | [diff] [blame] | 2694 | bdrv_flush(bs); |
| 2695 | } |
| 2696 | |
| 2697 | return 0; |
| 2698 | } |
| 2699 | |
Stefan Hajnoczi | 470c050 | 2012-01-18 14:40:42 +0000 | [diff] [blame] | 2700 | static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, |
Stefan Hajnoczi | ab18592 | 2011-11-17 13:40:31 +0000 | [diff] [blame] | 2701 | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) |
| 2702 | { |
| 2703 | /* Perform I/O through a temporary buffer so that users who scribble over |
| 2704 | * their read buffer while the operation is in progress do not end up |
| 2705 | * modifying the image file. This is critical for zero-copy guest I/O |
| 2706 | * where anything might happen inside guest memory. |
| 2707 | */ |
| 2708 | void *bounce_buffer; |
| 2709 | |
Stefan Hajnoczi | 79c053b | 2012-02-07 13:27:26 +0000 | [diff] [blame] | 2710 | BlockDriver *drv = bs->drv; |
Stefan Hajnoczi | ab18592 | 2011-11-17 13:40:31 +0000 | [diff] [blame] | 2711 | struct iovec iov; |
| 2712 | QEMUIOVector bounce_qiov; |
| 2713 | int64_t cluster_sector_num; |
| 2714 | int cluster_nb_sectors; |
| 2715 | size_t skip_bytes; |
| 2716 | int ret; |
| 2717 | |
| 2718 | /* Cover entire cluster so no additional backing file I/O is required when |
| 2719 | * allocating cluster in the image file. |
| 2720 | */ |
Paolo Bonzini | 343bded | 2013-01-21 17:09:42 +0100 | [diff] [blame] | 2721 | bdrv_round_to_clusters(bs, sector_num, nb_sectors, |
| 2722 | &cluster_sector_num, &cluster_nb_sectors); |
Stefan Hajnoczi | ab18592 | 2011-11-17 13:40:31 +0000 | [diff] [blame] | 2723 | |
Stefan Hajnoczi | 470c050 | 2012-01-18 14:40:42 +0000 | [diff] [blame] | 2724 | trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, |
| 2725 | cluster_sector_num, cluster_nb_sectors); |
Stefan Hajnoczi | ab18592 | 2011-11-17 13:40:31 +0000 | [diff] [blame] | 2726 | |
| 2727 | iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE; |
| 2728 | iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len); |
| 2729 | qemu_iovec_init_external(&bounce_qiov, &iov, 1); |
| 2730 | |
Stefan Hajnoczi | 79c053b | 2012-02-07 13:27:26 +0000 | [diff] [blame] | 2731 | ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, |
| 2732 | &bounce_qiov); |
Stefan Hajnoczi | ab18592 | 2011-11-17 13:40:31 +0000 | [diff] [blame] | 2733 | if (ret < 0) { |
| 2734 | goto err; |
| 2735 | } |
| 2736 | |
Stefan Hajnoczi | 79c053b | 2012-02-07 13:27:26 +0000 | [diff] [blame] | 2737 | if (drv->bdrv_co_write_zeroes && |
| 2738 | buffer_is_zero(bounce_buffer, iov.iov_len)) { |
Kevin Wolf | 621f058 | 2012-03-20 15:12:58 +0100 | [diff] [blame] | 2739 | ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num, |
Peter Lieven | aa7bfbf | 2013-10-24 12:06:51 +0200 | [diff] [blame] | 2740 | cluster_nb_sectors, 0); |
Stefan Hajnoczi | 79c053b | 2012-02-07 13:27:26 +0000 | [diff] [blame] | 2741 | } else { |
Paolo Bonzini | f05fa4a | 2012-06-06 00:04:49 +0200 | [diff] [blame] | 2742 | /* This does not change the data on the disk, it is not necessary |
| 2743 | * to flush even in cache=writethrough mode. |
| 2744 | */ |
Stefan Hajnoczi | 79c053b | 2012-02-07 13:27:26 +0000 | [diff] [blame] | 2745 | ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, |
Stefan Hajnoczi | ab18592 | 2011-11-17 13:40:31 +0000 | [diff] [blame] | 2746 | &bounce_qiov); |
Stefan Hajnoczi | 79c053b | 2012-02-07 13:27:26 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
Stefan Hajnoczi | ab18592 | 2011-11-17 13:40:31 +0000 | [diff] [blame] | 2749 | if (ret < 0) { |
| 2750 | /* It might be okay to ignore write errors for guest requests. If this |
| 2751 | * is a deliberate copy-on-read then we don't want to ignore the error. |
| 2752 | * Simply report it in all cases. |
| 2753 | */ |
| 2754 | goto err; |
| 2755 | } |
| 2756 | |
| 2757 | skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE; |
Michael Tokarev | 0339614 | 2012-06-07 20:17:55 +0400 | [diff] [blame] | 2758 | qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes, |
| 2759 | nb_sectors * BDRV_SECTOR_SIZE); |
Stefan Hajnoczi | ab18592 | 2011-11-17 13:40:31 +0000 | [diff] [blame] | 2760 | |
| 2761 | err: |
| 2762 | qemu_vfree(bounce_buffer); |
| 2763 | return ret; |
| 2764 | } |
| 2765 | |
Stefan Hajnoczi | c5fbe57 | 2011-10-05 17:17:03 +0100 | [diff] [blame] | 2766 | /* |
| 2767 | * Handle a read request in coroutine context |
| 2768 | */ |
| 2769 | static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, |
Stefan Hajnoczi | 470c050 | 2012-01-18 14:40:42 +0000 | [diff] [blame] | 2770 | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, |
| 2771 | BdrvRequestFlags flags) |
Kevin Wolf | da1fa91 | 2011-07-14 17:27:13 +0200 | [diff] [blame] | 2772 | { |
| 2773 | BlockDriver *drv = bs->drv; |
Stefan Hajnoczi | dbffbdc | 2011-11-17 13:40:27 +0000 | [diff] [blame] | 2774 | BdrvTrackedRequest req; |
| 2775 | int ret; |
Kevin Wolf | da1fa91 | 2011-07-14 17:27:13 +0200 | [diff] [blame] | 2776 | |
Kevin Wolf | da1fa91 | 2011-07-14 17:27:13 +0200 | [diff] [blame] | 2777 | if (!drv) { |
| 2778 | return -ENOMEDIUM; |
| 2779 | } |
| 2780 | if (bdrv_check_request(bs, sector_num, nb_sectors)) { |
| 2781 | return -EIO; |
| 2782 | } |
| 2783 | |
Stefan Hajnoczi | f465828 | 2011-11-17 13:40:29 +0000 | [diff] [blame] | 2784 | if (bs->copy_on_read) { |
Stefan Hajnoczi | 470c050 | 2012-01-18 14:40:42 +0000 | [diff] [blame] | 2785 | flags |= BDRV_REQ_COPY_ON_READ; |
| 2786 | } |
| 2787 | if (flags & BDRV_REQ_COPY_ON_READ) { |
| 2788 | bs->copy_on_read_in_flight++; |
| 2789 | } |
| 2790 | |
| 2791 | if (bs->copy_on_read_in_flight) { |
Stefan Hajnoczi | f465828 | 2011-11-17 13:40:29 +0000 | [diff] [blame] | 2792 | wait_for_overlapping_requests(bs, sector_num, nb_sectors); |
| 2793 | } |
| 2794 | |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 2795 | /* throttling disk I/O */ |
| 2796 | if (bs->io_limits_enabled) { |
| 2797 | bdrv_io_limits_intercept(bs, nb_sectors, false); |
| 2798 | } |
| 2799 | |
Stefan Hajnoczi | dbffbdc | 2011-11-17 13:40:27 +0000 | [diff] [blame] | 2800 | tracked_request_begin(&req, bs, sector_num, nb_sectors, false); |
Stefan Hajnoczi | ab18592 | 2011-11-17 13:40:31 +0000 | [diff] [blame] | 2801 | |
Stefan Hajnoczi | 470c050 | 2012-01-18 14:40:42 +0000 | [diff] [blame] | 2802 | if (flags & BDRV_REQ_COPY_ON_READ) { |
Stefan Hajnoczi | ab18592 | 2011-11-17 13:40:31 +0000 | [diff] [blame] | 2803 | int pnum; |
| 2804 | |
Paolo Bonzini | bdad13b | 2013-09-04 19:00:22 +0200 | [diff] [blame] | 2805 | ret = bdrv_is_allocated(bs, sector_num, nb_sectors, &pnum); |
Stefan Hajnoczi | ab18592 | 2011-11-17 13:40:31 +0000 | [diff] [blame] | 2806 | if (ret < 0) { |
| 2807 | goto out; |
| 2808 | } |
| 2809 | |
| 2810 | if (!ret || pnum != nb_sectors) { |
Stefan Hajnoczi | 470c050 | 2012-01-18 14:40:42 +0000 | [diff] [blame] | 2811 | ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov); |
Stefan Hajnoczi | ab18592 | 2011-11-17 13:40:31 +0000 | [diff] [blame] | 2812 | goto out; |
| 2813 | } |
| 2814 | } |
| 2815 | |
MORITA Kazutaka | 893a8f6 | 2013-08-06 09:53:40 +0800 | [diff] [blame] | 2816 | if (!(bs->zero_beyond_eof && bs->growable)) { |
| 2817 | ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); |
| 2818 | } else { |
| 2819 | /* Read zeros after EOF of growable BDSes */ |
| 2820 | int64_t len, total_sectors, max_nb_sectors; |
| 2821 | |
| 2822 | len = bdrv_getlength(bs); |
| 2823 | if (len < 0) { |
| 2824 | ret = len; |
| 2825 | goto out; |
| 2826 | } |
| 2827 | |
Fam Zheng | d055a1f | 2013-09-26 19:55:33 +0800 | [diff] [blame] | 2828 | total_sectors = DIV_ROUND_UP(len, BDRV_SECTOR_SIZE); |
MORITA Kazutaka | 893a8f6 | 2013-08-06 09:53:40 +0800 | [diff] [blame] | 2829 | max_nb_sectors = MAX(0, total_sectors - sector_num); |
| 2830 | if (max_nb_sectors > 0) { |
| 2831 | ret = drv->bdrv_co_readv(bs, sector_num, |
| 2832 | MIN(nb_sectors, max_nb_sectors), qiov); |
| 2833 | } else { |
| 2834 | ret = 0; |
| 2835 | } |
| 2836 | |
| 2837 | /* Reading beyond end of file is supposed to produce zeroes */ |
| 2838 | if (ret == 0 && total_sectors < sector_num + nb_sectors) { |
| 2839 | uint64_t offset = MAX(0, total_sectors - sector_num); |
| 2840 | uint64_t bytes = (sector_num + nb_sectors - offset) * |
| 2841 | BDRV_SECTOR_SIZE; |
| 2842 | qemu_iovec_memset(qiov, offset * BDRV_SECTOR_SIZE, 0, bytes); |
| 2843 | } |
| 2844 | } |
Stefan Hajnoczi | ab18592 | 2011-11-17 13:40:31 +0000 | [diff] [blame] | 2845 | |
| 2846 | out: |
Stefan Hajnoczi | dbffbdc | 2011-11-17 13:40:27 +0000 | [diff] [blame] | 2847 | tracked_request_end(&req); |
Stefan Hajnoczi | 470c050 | 2012-01-18 14:40:42 +0000 | [diff] [blame] | 2848 | |
| 2849 | if (flags & BDRV_REQ_COPY_ON_READ) { |
| 2850 | bs->copy_on_read_in_flight--; |
| 2851 | } |
| 2852 | |
Stefan Hajnoczi | dbffbdc | 2011-11-17 13:40:27 +0000 | [diff] [blame] | 2853 | return ret; |
Kevin Wolf | da1fa91 | 2011-07-14 17:27:13 +0200 | [diff] [blame] | 2854 | } |
| 2855 | |
Stefan Hajnoczi | c5fbe57 | 2011-10-05 17:17:03 +0100 | [diff] [blame] | 2856 | int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, |
Kevin Wolf | da1fa91 | 2011-07-14 17:27:13 +0200 | [diff] [blame] | 2857 | int nb_sectors, QEMUIOVector *qiov) |
| 2858 | { |
Stefan Hajnoczi | c5fbe57 | 2011-10-05 17:17:03 +0100 | [diff] [blame] | 2859 | trace_bdrv_co_readv(bs, sector_num, nb_sectors); |
Kevin Wolf | da1fa91 | 2011-07-14 17:27:13 +0200 | [diff] [blame] | 2860 | |
Stefan Hajnoczi | 470c050 | 2012-01-18 14:40:42 +0000 | [diff] [blame] | 2861 | return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0); |
| 2862 | } |
| 2863 | |
| 2864 | int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs, |
| 2865 | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) |
| 2866 | { |
| 2867 | trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors); |
| 2868 | |
| 2869 | return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, |
| 2870 | BDRV_REQ_COPY_ON_READ); |
Stefan Hajnoczi | c5fbe57 | 2011-10-05 17:17:03 +0100 | [diff] [blame] | 2871 | } |
| 2872 | |
Peter Lieven | c31cb70 | 2013-10-24 12:06:58 +0200 | [diff] [blame] | 2873 | /* if no limit is specified in the BlockLimits use a default |
| 2874 | * of 32768 512-byte sectors (16 MiB) per request. |
| 2875 | */ |
| 2876 | #define MAX_WRITE_ZEROES_DEFAULT 32768 |
| 2877 | |
Stefan Hajnoczi | f08f2dd | 2012-02-07 13:27:25 +0000 | [diff] [blame] | 2878 | static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, |
Peter Lieven | aa7bfbf | 2013-10-24 12:06:51 +0200 | [diff] [blame] | 2879 | int64_t sector_num, int nb_sectors, BdrvRequestFlags flags) |
Stefan Hajnoczi | f08f2dd | 2012-02-07 13:27:25 +0000 | [diff] [blame] | 2880 | { |
| 2881 | BlockDriver *drv = bs->drv; |
| 2882 | QEMUIOVector qiov; |
Peter Lieven | c31cb70 | 2013-10-24 12:06:58 +0200 | [diff] [blame] | 2883 | struct iovec iov = {0}; |
| 2884 | int ret = 0; |
Stefan Hajnoczi | f08f2dd | 2012-02-07 13:27:25 +0000 | [diff] [blame] | 2885 | |
Peter Lieven | c31cb70 | 2013-10-24 12:06:58 +0200 | [diff] [blame] | 2886 | int max_write_zeroes = bs->bl.max_write_zeroes ? |
| 2887 | bs->bl.max_write_zeroes : MAX_WRITE_ZEROES_DEFAULT; |
Kevin Wolf | 621f058 | 2012-03-20 15:12:58 +0100 | [diff] [blame] | 2888 | |
Peter Lieven | c31cb70 | 2013-10-24 12:06:58 +0200 | [diff] [blame] | 2889 | while (nb_sectors > 0 && !ret) { |
| 2890 | int num = nb_sectors; |
| 2891 | |
Paolo Bonzini | b8d71c0 | 2013-11-22 13:39:48 +0100 | [diff] [blame] | 2892 | /* Align request. Block drivers can expect the "bulk" of the request |
| 2893 | * to be aligned. |
| 2894 | */ |
| 2895 | if (bs->bl.write_zeroes_alignment |
| 2896 | && num > bs->bl.write_zeroes_alignment) { |
| 2897 | if (sector_num % bs->bl.write_zeroes_alignment != 0) { |
| 2898 | /* Make a small request up to the first aligned sector. */ |
Peter Lieven | c31cb70 | 2013-10-24 12:06:58 +0200 | [diff] [blame] | 2899 | num = bs->bl.write_zeroes_alignment; |
Paolo Bonzini | b8d71c0 | 2013-11-22 13:39:48 +0100 | [diff] [blame] | 2900 | num -= sector_num % bs->bl.write_zeroes_alignment; |
| 2901 | } else if ((sector_num + num) % bs->bl.write_zeroes_alignment != 0) { |
| 2902 | /* Shorten the request to the last aligned sector. num cannot |
| 2903 | * underflow because num > bs->bl.write_zeroes_alignment. |
| 2904 | */ |
| 2905 | num -= (sector_num + num) % bs->bl.write_zeroes_alignment; |
Peter Lieven | c31cb70 | 2013-10-24 12:06:58 +0200 | [diff] [blame] | 2906 | } |
Kevin Wolf | 621f058 | 2012-03-20 15:12:58 +0100 | [diff] [blame] | 2907 | } |
Peter Lieven | c31cb70 | 2013-10-24 12:06:58 +0200 | [diff] [blame] | 2908 | |
| 2909 | /* limit request size */ |
| 2910 | if (num > max_write_zeroes) { |
| 2911 | num = max_write_zeroes; |
| 2912 | } |
| 2913 | |
| 2914 | ret = -ENOTSUP; |
| 2915 | /* First try the efficient write zeroes operation */ |
| 2916 | if (drv->bdrv_co_write_zeroes) { |
| 2917 | ret = drv->bdrv_co_write_zeroes(bs, sector_num, num, flags); |
| 2918 | } |
| 2919 | |
| 2920 | if (ret == -ENOTSUP) { |
| 2921 | /* Fall back to bounce buffer if write zeroes is unsupported */ |
| 2922 | iov.iov_len = num * BDRV_SECTOR_SIZE; |
| 2923 | if (iov.iov_base == NULL) { |
Paolo Bonzini | b8d71c0 | 2013-11-22 13:39:48 +0100 | [diff] [blame] | 2924 | iov.iov_base = qemu_blockalign(bs, num * BDRV_SECTOR_SIZE); |
| 2925 | memset(iov.iov_base, 0, num * BDRV_SECTOR_SIZE); |
Peter Lieven | c31cb70 | 2013-10-24 12:06:58 +0200 | [diff] [blame] | 2926 | } |
| 2927 | qemu_iovec_init_external(&qiov, &iov, 1); |
| 2928 | |
| 2929 | ret = drv->bdrv_co_writev(bs, sector_num, num, &qiov); |
Paolo Bonzini | b8d71c0 | 2013-11-22 13:39:48 +0100 | [diff] [blame] | 2930 | |
| 2931 | /* Keep bounce buffer around if it is big enough for all |
| 2932 | * all future requests. |
| 2933 | */ |
| 2934 | if (num < max_write_zeroes) { |
| 2935 | qemu_vfree(iov.iov_base); |
| 2936 | iov.iov_base = NULL; |
| 2937 | } |
Peter Lieven | c31cb70 | 2013-10-24 12:06:58 +0200 | [diff] [blame] | 2938 | } |
| 2939 | |
| 2940 | sector_num += num; |
| 2941 | nb_sectors -= num; |
Stefan Hajnoczi | f08f2dd | 2012-02-07 13:27:25 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
Stefan Hajnoczi | f08f2dd | 2012-02-07 13:27:25 +0000 | [diff] [blame] | 2944 | qemu_vfree(iov.iov_base); |
| 2945 | return ret; |
| 2946 | } |
| 2947 | |
Stefan Hajnoczi | c5fbe57 | 2011-10-05 17:17:03 +0100 | [diff] [blame] | 2948 | /* |
| 2949 | * Handle a write request in coroutine context |
| 2950 | */ |
| 2951 | static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, |
Stefan Hajnoczi | f08f2dd | 2012-02-07 13:27:25 +0000 | [diff] [blame] | 2952 | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, |
| 2953 | BdrvRequestFlags flags) |
Stefan Hajnoczi | c5fbe57 | 2011-10-05 17:17:03 +0100 | [diff] [blame] | 2954 | { |
| 2955 | BlockDriver *drv = bs->drv; |
Stefan Hajnoczi | dbffbdc | 2011-11-17 13:40:27 +0000 | [diff] [blame] | 2956 | BdrvTrackedRequest req; |
Stefan Hajnoczi | 6b7cb24 | 2011-10-13 13:08:24 +0100 | [diff] [blame] | 2957 | int ret; |
Kevin Wolf | da1fa91 | 2011-07-14 17:27:13 +0200 | [diff] [blame] | 2958 | |
| 2959 | if (!bs->drv) { |
| 2960 | return -ENOMEDIUM; |
| 2961 | } |
| 2962 | if (bs->read_only) { |
| 2963 | return -EACCES; |
| 2964 | } |
| 2965 | if (bdrv_check_request(bs, sector_num, nb_sectors)) { |
| 2966 | return -EIO; |
| 2967 | } |
| 2968 | |
Stefan Hajnoczi | 470c050 | 2012-01-18 14:40:42 +0000 | [diff] [blame] | 2969 | if (bs->copy_on_read_in_flight) { |
Stefan Hajnoczi | f465828 | 2011-11-17 13:40:29 +0000 | [diff] [blame] | 2970 | wait_for_overlapping_requests(bs, sector_num, nb_sectors); |
| 2971 | } |
| 2972 | |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 2973 | /* throttling disk I/O */ |
| 2974 | if (bs->io_limits_enabled) { |
| 2975 | bdrv_io_limits_intercept(bs, nb_sectors, true); |
| 2976 | } |
| 2977 | |
Stefan Hajnoczi | dbffbdc | 2011-11-17 13:40:27 +0000 | [diff] [blame] | 2978 | tracked_request_begin(&req, bs, sector_num, nb_sectors, true); |
| 2979 | |
Stefan Hajnoczi | d616b22 | 2013-06-24 17:13:10 +0200 | [diff] [blame] | 2980 | ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req); |
| 2981 | |
| 2982 | if (ret < 0) { |
| 2983 | /* Do nothing, write notifier decided to fail this request */ |
| 2984 | } else if (flags & BDRV_REQ_ZERO_WRITE) { |
Peter Lieven | aa7bfbf | 2013-10-24 12:06:51 +0200 | [diff] [blame] | 2985 | ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors, flags); |
Stefan Hajnoczi | f08f2dd | 2012-02-07 13:27:25 +0000 | [diff] [blame] | 2986 | } else { |
| 2987 | ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); |
| 2988 | } |
Stefan Hajnoczi | 6b7cb24 | 2011-10-13 13:08:24 +0100 | [diff] [blame] | 2989 | |
Paolo Bonzini | f05fa4a | 2012-06-06 00:04:49 +0200 | [diff] [blame] | 2990 | if (ret == 0 && !bs->enable_write_cache) { |
| 2991 | ret = bdrv_co_flush(bs); |
| 2992 | } |
| 2993 | |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 2994 | bdrv_set_dirty(bs, sector_num, nb_sectors); |
Kevin Wolf | da1fa91 | 2011-07-14 17:27:13 +0200 | [diff] [blame] | 2995 | |
| 2996 | if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { |
| 2997 | bs->wr_highest_sector = sector_num + nb_sectors - 1; |
| 2998 | } |
Paolo Bonzini | df2a6f2 | 2013-09-04 19:00:21 +0200 | [diff] [blame] | 2999 | if (bs->growable && ret >= 0) { |
| 3000 | bs->total_sectors = MAX(bs->total_sectors, sector_num + nb_sectors); |
| 3001 | } |
Kevin Wolf | da1fa91 | 2011-07-14 17:27:13 +0200 | [diff] [blame] | 3002 | |
Stefan Hajnoczi | dbffbdc | 2011-11-17 13:40:27 +0000 | [diff] [blame] | 3003 | tracked_request_end(&req); |
| 3004 | |
Stefan Hajnoczi | 6b7cb24 | 2011-10-13 13:08:24 +0100 | [diff] [blame] | 3005 | return ret; |
Kevin Wolf | da1fa91 | 2011-07-14 17:27:13 +0200 | [diff] [blame] | 3006 | } |
| 3007 | |
Stefan Hajnoczi | c5fbe57 | 2011-10-05 17:17:03 +0100 | [diff] [blame] | 3008 | int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, |
| 3009 | int nb_sectors, QEMUIOVector *qiov) |
| 3010 | { |
| 3011 | trace_bdrv_co_writev(bs, sector_num, nb_sectors); |
| 3012 | |
Stefan Hajnoczi | f08f2dd | 2012-02-07 13:27:25 +0000 | [diff] [blame] | 3013 | return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0); |
| 3014 | } |
| 3015 | |
| 3016 | int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs, |
Peter Lieven | aa7bfbf | 2013-10-24 12:06:51 +0200 | [diff] [blame] | 3017 | int64_t sector_num, int nb_sectors, |
| 3018 | BdrvRequestFlags flags) |
Stefan Hajnoczi | f08f2dd | 2012-02-07 13:27:25 +0000 | [diff] [blame] | 3019 | { |
Paolo Bonzini | 94d6ff2 | 2013-11-22 13:39:45 +0100 | [diff] [blame] | 3020 | trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors, flags); |
Stefan Hajnoczi | f08f2dd | 2012-02-07 13:27:25 +0000 | [diff] [blame] | 3021 | |
Peter Lieven | d32f35c | 2013-10-24 12:06:52 +0200 | [diff] [blame] | 3022 | if (!(bs->open_flags & BDRV_O_UNMAP)) { |
| 3023 | flags &= ~BDRV_REQ_MAY_UNMAP; |
| 3024 | } |
| 3025 | |
Stefan Hajnoczi | f08f2dd | 2012-02-07 13:27:25 +0000 | [diff] [blame] | 3026 | return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL, |
Peter Lieven | aa7bfbf | 2013-10-24 12:06:51 +0200 | [diff] [blame] | 3027 | BDRV_REQ_ZERO_WRITE | flags); |
Stefan Hajnoczi | c5fbe57 | 2011-10-05 17:17:03 +0100 | [diff] [blame] | 3028 | } |
| 3029 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3030 | /** |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3031 | * Truncate file to 'offset' bytes (needed only for file protocols) |
| 3032 | */ |
| 3033 | int bdrv_truncate(BlockDriverState *bs, int64_t offset) |
| 3034 | { |
| 3035 | BlockDriver *drv = bs->drv; |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 3036 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3037 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 3038 | return -ENOMEDIUM; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3039 | if (!drv->bdrv_truncate) |
| 3040 | return -ENOTSUP; |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 3041 | if (bs->read_only) |
| 3042 | return -EACCES; |
Marcelo Tosatti | 8591675 | 2011-01-26 12:12:35 -0200 | [diff] [blame] | 3043 | if (bdrv_in_use(bs)) |
| 3044 | return -EBUSY; |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 3045 | ret = drv->bdrv_truncate(bs, offset); |
| 3046 | if (ret == 0) { |
| 3047 | ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); |
Markus Armbruster | 145feb1 | 2011-08-03 15:07:42 +0200 | [diff] [blame] | 3048 | bdrv_dev_resize_cb(bs); |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 3049 | } |
| 3050 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3051 | } |
| 3052 | |
| 3053 | /** |
Fam Zheng | 4a1d5e1 | 2011-07-12 19:56:39 +0800 | [diff] [blame] | 3054 | * Length of a allocated file in bytes. Sparse files are counted by actual |
| 3055 | * allocated space. Return < 0 if error or unknown. |
| 3056 | */ |
| 3057 | int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) |
| 3058 | { |
| 3059 | BlockDriver *drv = bs->drv; |
| 3060 | if (!drv) { |
| 3061 | return -ENOMEDIUM; |
| 3062 | } |
| 3063 | if (drv->bdrv_get_allocated_file_size) { |
| 3064 | return drv->bdrv_get_allocated_file_size(bs); |
| 3065 | } |
| 3066 | if (bs->file) { |
| 3067 | return bdrv_get_allocated_file_size(bs->file); |
| 3068 | } |
| 3069 | return -ENOTSUP; |
| 3070 | } |
| 3071 | |
| 3072 | /** |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3073 | * Length of a file in bytes. Return < 0 if error or unknown. |
| 3074 | */ |
| 3075 | int64_t bdrv_getlength(BlockDriverState *bs) |
| 3076 | { |
| 3077 | BlockDriver *drv = bs->drv; |
| 3078 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 3079 | return -ENOMEDIUM; |
Stefan Hajnoczi | 5176228 | 2010-04-19 16:56:41 +0100 | [diff] [blame] | 3080 | |
Kevin Wolf | b94a261 | 2013-10-29 12:18:58 +0100 | [diff] [blame] | 3081 | if (drv->has_variable_length) { |
| 3082 | int ret = refresh_total_sectors(bs, bs->total_sectors); |
| 3083 | if (ret < 0) { |
| 3084 | return ret; |
Stefan Hajnoczi | 46a4e4e | 2011-03-29 20:04:41 +0100 | [diff] [blame] | 3085 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3086 | } |
Stefan Hajnoczi | 46a4e4e | 2011-03-29 20:04:41 +0100 | [diff] [blame] | 3087 | return bs->total_sectors * BDRV_SECTOR_SIZE; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 3088 | } |
| 3089 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 3090 | /* return 0 as number of sectors if no device present or error */ |
ths | 96b8f13 | 2007-12-17 01:35:20 +0000 | [diff] [blame] | 3091 | void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 3092 | { |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 3093 | int64_t length; |
| 3094 | length = bdrv_getlength(bs); |
| 3095 | if (length < 0) |
| 3096 | length = 0; |
| 3097 | else |
Jan Kiszka | 6ea4430 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 3098 | length = length >> BDRV_SECTOR_BITS; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 3099 | *nb_sectors_ptr = length; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 3100 | } |
bellard | cf98951 | 2004-02-16 21:56:36 +0000 | [diff] [blame] | 3101 | |
Paolo Bonzini | ff06f5f | 2012-09-28 17:22:54 +0200 | [diff] [blame] | 3102 | void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_error, |
| 3103 | BlockdevOnError on_write_error) |
Markus Armbruster | abd7f68 | 2010-06-02 18:55:17 +0200 | [diff] [blame] | 3104 | { |
| 3105 | bs->on_read_error = on_read_error; |
| 3106 | bs->on_write_error = on_write_error; |
| 3107 | } |
| 3108 | |
Paolo Bonzini | 1ceee0d | 2012-09-28 17:22:56 +0200 | [diff] [blame] | 3109 | BlockdevOnError bdrv_get_on_error(BlockDriverState *bs, bool is_read) |
Markus Armbruster | abd7f68 | 2010-06-02 18:55:17 +0200 | [diff] [blame] | 3110 | { |
| 3111 | return is_read ? bs->on_read_error : bs->on_write_error; |
| 3112 | } |
| 3113 | |
Paolo Bonzini | 3e1caa5 | 2012-09-28 17:22:57 +0200 | [diff] [blame] | 3114 | BlockErrorAction bdrv_get_error_action(BlockDriverState *bs, bool is_read, int error) |
| 3115 | { |
| 3116 | BlockdevOnError on_err = is_read ? bs->on_read_error : bs->on_write_error; |
| 3117 | |
| 3118 | switch (on_err) { |
| 3119 | case BLOCKDEV_ON_ERROR_ENOSPC: |
| 3120 | return (error == ENOSPC) ? BDRV_ACTION_STOP : BDRV_ACTION_REPORT; |
| 3121 | case BLOCKDEV_ON_ERROR_STOP: |
| 3122 | return BDRV_ACTION_STOP; |
| 3123 | case BLOCKDEV_ON_ERROR_REPORT: |
| 3124 | return BDRV_ACTION_REPORT; |
| 3125 | case BLOCKDEV_ON_ERROR_IGNORE: |
| 3126 | return BDRV_ACTION_IGNORE; |
| 3127 | default: |
| 3128 | abort(); |
| 3129 | } |
| 3130 | } |
| 3131 | |
| 3132 | /* This is done by device models because, while the block layer knows |
| 3133 | * about the error, it does not know whether an operation comes from |
| 3134 | * the device or the block layer (from a job, for example). |
| 3135 | */ |
| 3136 | void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action, |
| 3137 | bool is_read, int error) |
| 3138 | { |
| 3139 | assert(error >= 0); |
Paolo Bonzini | 32c81a4 | 2012-09-28 17:22:58 +0200 | [diff] [blame] | 3140 | bdrv_emit_qmp_error_event(bs, QEVENT_BLOCK_IO_ERROR, action, is_read); |
Paolo Bonzini | 3e1caa5 | 2012-09-28 17:22:57 +0200 | [diff] [blame] | 3141 | if (action == BDRV_ACTION_STOP) { |
| 3142 | vm_stop(RUN_STATE_IO_ERROR); |
| 3143 | bdrv_iostatus_set_err(bs, error); |
| 3144 | } |
| 3145 | } |
| 3146 | |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 3147 | int bdrv_is_read_only(BlockDriverState *bs) |
| 3148 | { |
| 3149 | return bs->read_only; |
| 3150 | } |
| 3151 | |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 3152 | int bdrv_is_sg(BlockDriverState *bs) |
| 3153 | { |
| 3154 | return bs->sg; |
| 3155 | } |
| 3156 | |
Christoph Hellwig | e900a7b | 2009-09-04 19:01:15 +0200 | [diff] [blame] | 3157 | int bdrv_enable_write_cache(BlockDriverState *bs) |
| 3158 | { |
| 3159 | return bs->enable_write_cache; |
| 3160 | } |
| 3161 | |
Paolo Bonzini | 425b014 | 2012-06-06 00:04:52 +0200 | [diff] [blame] | 3162 | void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce) |
| 3163 | { |
| 3164 | bs->enable_write_cache = wce; |
Jeff Cody | 55b110f | 2012-09-20 15:13:18 -0400 | [diff] [blame] | 3165 | |
| 3166 | /* so a reopen() will preserve wce */ |
| 3167 | if (wce) { |
| 3168 | bs->open_flags |= BDRV_O_CACHE_WB; |
| 3169 | } else { |
| 3170 | bs->open_flags &= ~BDRV_O_CACHE_WB; |
| 3171 | } |
Paolo Bonzini | 425b014 | 2012-06-06 00:04:52 +0200 | [diff] [blame] | 3172 | } |
| 3173 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3174 | int bdrv_is_encrypted(BlockDriverState *bs) |
| 3175 | { |
| 3176 | if (bs->backing_hd && bs->backing_hd->encrypted) |
| 3177 | return 1; |
| 3178 | return bs->encrypted; |
| 3179 | } |
| 3180 | |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 3181 | int bdrv_key_required(BlockDriverState *bs) |
| 3182 | { |
| 3183 | BlockDriverState *backing_hd = bs->backing_hd; |
| 3184 | |
| 3185 | if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) |
| 3186 | return 1; |
| 3187 | return (bs->encrypted && !bs->valid_key); |
| 3188 | } |
| 3189 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3190 | int bdrv_set_key(BlockDriverState *bs, const char *key) |
| 3191 | { |
| 3192 | int ret; |
| 3193 | if (bs->backing_hd && bs->backing_hd->encrypted) { |
| 3194 | ret = bdrv_set_key(bs->backing_hd, key); |
| 3195 | if (ret < 0) |
| 3196 | return ret; |
| 3197 | if (!bs->encrypted) |
| 3198 | return 0; |
| 3199 | } |
Shahar Havivi | fd04a2a | 2010-03-06 00:26:13 +0200 | [diff] [blame] | 3200 | if (!bs->encrypted) { |
| 3201 | return -EINVAL; |
| 3202 | } else if (!bs->drv || !bs->drv->bdrv_set_key) { |
| 3203 | return -ENOMEDIUM; |
| 3204 | } |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 3205 | ret = bs->drv->bdrv_set_key(bs, key); |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 3206 | if (ret < 0) { |
| 3207 | bs->valid_key = 0; |
| 3208 | } else if (!bs->valid_key) { |
| 3209 | bs->valid_key = 1; |
| 3210 | /* call the change callback now, we skipped it on open */ |
Markus Armbruster | 7d4b4ba | 2011-09-06 18:58:59 +0200 | [diff] [blame] | 3211 | bdrv_dev_change_media_cb(bs, true); |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 3212 | } |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 3213 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3214 | } |
| 3215 | |
Markus Armbruster | f8d6bba | 2012-06-13 10:11:48 +0200 | [diff] [blame] | 3216 | const char *bdrv_get_format_name(BlockDriverState *bs) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3217 | { |
Markus Armbruster | f8d6bba | 2012-06-13 10:11:48 +0200 | [diff] [blame] | 3218 | return bs->drv ? bs->drv->format_name : NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3219 | } |
| 3220 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3221 | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3222 | void *opaque) |
| 3223 | { |
| 3224 | BlockDriver *drv; |
| 3225 | |
Stefan Hajnoczi | 8a22f02 | 2010-04-13 10:29:33 +0100 | [diff] [blame] | 3226 | QLIST_FOREACH(drv, &bdrv_drivers, list) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3227 | it(opaque, drv->format_name); |
| 3228 | } |
| 3229 | } |
| 3230 | |
Benoît Canet | dc364f4 | 2014-01-23 21:31:32 +0100 | [diff] [blame^] | 3231 | /* This function is to find block backend bs */ |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 3232 | BlockDriverState *bdrv_find(const char *name) |
| 3233 | { |
| 3234 | BlockDriverState *bs; |
| 3235 | |
Benoît Canet | dc364f4 | 2014-01-23 21:31:32 +0100 | [diff] [blame^] | 3236 | QTAILQ_FOREACH(bs, &bdrv_states, device_list) { |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 3237 | if (!strcmp(name, bs->device_name)) { |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 3238 | return bs; |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 3239 | } |
bellard | b338082 | 2004-03-14 21:38:54 +0000 | [diff] [blame] | 3240 | } |
| 3241 | return NULL; |
| 3242 | } |
| 3243 | |
Benoît Canet | dc364f4 | 2014-01-23 21:31:32 +0100 | [diff] [blame^] | 3244 | /* This function is to find a node in the bs graph */ |
| 3245 | BlockDriverState *bdrv_find_node(const char *node_name) |
| 3246 | { |
| 3247 | BlockDriverState *bs; |
| 3248 | |
| 3249 | assert(node_name); |
| 3250 | |
| 3251 | QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) { |
| 3252 | if (!strcmp(node_name, bs->node_name)) { |
| 3253 | return bs; |
| 3254 | } |
| 3255 | } |
| 3256 | return NULL; |
| 3257 | } |
| 3258 | |
Markus Armbruster | 2f399b0 | 2010-06-02 18:55:20 +0200 | [diff] [blame] | 3259 | BlockDriverState *bdrv_next(BlockDriverState *bs) |
| 3260 | { |
| 3261 | if (!bs) { |
| 3262 | return QTAILQ_FIRST(&bdrv_states); |
| 3263 | } |
Benoît Canet | dc364f4 | 2014-01-23 21:31:32 +0100 | [diff] [blame^] | 3264 | return QTAILQ_NEXT(bs, device_list); |
Markus Armbruster | 2f399b0 | 2010-06-02 18:55:20 +0200 | [diff] [blame] | 3265 | } |
| 3266 | |
aliguori | 51de976 | 2009-03-05 23:00:43 +0000 | [diff] [blame] | 3267 | void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3268 | { |
| 3269 | BlockDriverState *bs; |
| 3270 | |
Benoît Canet | dc364f4 | 2014-01-23 21:31:32 +0100 | [diff] [blame^] | 3271 | QTAILQ_FOREACH(bs, &bdrv_states, device_list) { |
aliguori | 51de976 | 2009-03-05 23:00:43 +0000 | [diff] [blame] | 3272 | it(opaque, bs); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3273 | } |
| 3274 | } |
| 3275 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3276 | const char *bdrv_get_device_name(BlockDriverState *bs) |
| 3277 | { |
| 3278 | return bs->device_name; |
| 3279 | } |
| 3280 | |
Markus Armbruster | c843328 | 2012-06-05 16:49:24 +0200 | [diff] [blame] | 3281 | int bdrv_get_flags(BlockDriverState *bs) |
| 3282 | { |
| 3283 | return bs->open_flags; |
| 3284 | } |
| 3285 | |
Kevin Wolf | f0f0fdf | 2013-07-05 13:48:01 +0200 | [diff] [blame] | 3286 | int bdrv_flush_all(void) |
aliguori | c6ca28d | 2008-10-06 13:55:43 +0000 | [diff] [blame] | 3287 | { |
| 3288 | BlockDriverState *bs; |
Kevin Wolf | f0f0fdf | 2013-07-05 13:48:01 +0200 | [diff] [blame] | 3289 | int result = 0; |
aliguori | c6ca28d | 2008-10-06 13:55:43 +0000 | [diff] [blame] | 3290 | |
Benoît Canet | dc364f4 | 2014-01-23 21:31:32 +0100 | [diff] [blame^] | 3291 | QTAILQ_FOREACH(bs, &bdrv_states, device_list) { |
Kevin Wolf | f0f0fdf | 2013-07-05 13:48:01 +0200 | [diff] [blame] | 3292 | int ret = bdrv_flush(bs); |
| 3293 | if (ret < 0 && !result) { |
| 3294 | result = ret; |
| 3295 | } |
Stefan Hajnoczi | 1b7bdbc | 2010-04-10 07:02:42 +0100 | [diff] [blame] | 3296 | } |
Kevin Wolf | f0f0fdf | 2013-07-05 13:48:01 +0200 | [diff] [blame] | 3297 | |
| 3298 | return result; |
aliguori | c6ca28d | 2008-10-06 13:55:43 +0000 | [diff] [blame] | 3299 | } |
| 3300 | |
Peter Lieven | 3ac2162 | 2013-06-28 12:47:42 +0200 | [diff] [blame] | 3301 | int bdrv_has_zero_init_1(BlockDriverState *bs) |
| 3302 | { |
| 3303 | return 1; |
| 3304 | } |
| 3305 | |
Kevin Wolf | f2feebb | 2010-04-14 17:30:35 +0200 | [diff] [blame] | 3306 | int bdrv_has_zero_init(BlockDriverState *bs) |
| 3307 | { |
| 3308 | assert(bs->drv); |
| 3309 | |
Paolo Bonzini | 11212d8 | 2013-09-04 19:00:27 +0200 | [diff] [blame] | 3310 | /* If BS is a copy on write image, it is initialized to |
| 3311 | the contents of the base image, which may not be zeroes. */ |
| 3312 | if (bs->backing_hd) { |
| 3313 | return 0; |
| 3314 | } |
Kevin Wolf | 336c1c1 | 2010-07-28 11:26:29 +0200 | [diff] [blame] | 3315 | if (bs->drv->bdrv_has_zero_init) { |
| 3316 | return bs->drv->bdrv_has_zero_init(bs); |
Kevin Wolf | f2feebb | 2010-04-14 17:30:35 +0200 | [diff] [blame] | 3317 | } |
| 3318 | |
Peter Lieven | 3ac2162 | 2013-06-28 12:47:42 +0200 | [diff] [blame] | 3319 | /* safe default */ |
| 3320 | return 0; |
Kevin Wolf | f2feebb | 2010-04-14 17:30:35 +0200 | [diff] [blame] | 3321 | } |
| 3322 | |
Peter Lieven | 4ce7869 | 2013-10-24 12:06:54 +0200 | [diff] [blame] | 3323 | bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs) |
| 3324 | { |
| 3325 | BlockDriverInfo bdi; |
| 3326 | |
| 3327 | if (bs->backing_hd) { |
| 3328 | return false; |
| 3329 | } |
| 3330 | |
| 3331 | if (bdrv_get_info(bs, &bdi) == 0) { |
| 3332 | return bdi.unallocated_blocks_are_zero; |
| 3333 | } |
| 3334 | |
| 3335 | return false; |
| 3336 | } |
| 3337 | |
| 3338 | bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs) |
| 3339 | { |
| 3340 | BlockDriverInfo bdi; |
| 3341 | |
| 3342 | if (bs->backing_hd || !(bs->open_flags & BDRV_O_UNMAP)) { |
| 3343 | return false; |
| 3344 | } |
| 3345 | |
| 3346 | if (bdrv_get_info(bs, &bdi) == 0) { |
| 3347 | return bdi.can_write_zeroes_with_unmap; |
| 3348 | } |
| 3349 | |
| 3350 | return false; |
| 3351 | } |
| 3352 | |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 3353 | typedef struct BdrvCoGetBlockStatusData { |
Stefan Hajnoczi | 376ae3f | 2011-11-14 12:44:19 +0000 | [diff] [blame] | 3354 | BlockDriverState *bs; |
Miroslav Rezanina | b35b2bb | 2013-02-13 09:09:39 +0100 | [diff] [blame] | 3355 | BlockDriverState *base; |
Stefan Hajnoczi | 376ae3f | 2011-11-14 12:44:19 +0000 | [diff] [blame] | 3356 | int64_t sector_num; |
| 3357 | int nb_sectors; |
| 3358 | int *pnum; |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 3359 | int64_t ret; |
Stefan Hajnoczi | 376ae3f | 2011-11-14 12:44:19 +0000 | [diff] [blame] | 3360 | bool done; |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 3361 | } BdrvCoGetBlockStatusData; |
Stefan Hajnoczi | 376ae3f | 2011-11-14 12:44:19 +0000 | [diff] [blame] | 3362 | |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 3363 | /* |
| 3364 | * Returns true iff the specified sector is present in the disk image. Drivers |
| 3365 | * not implementing the functionality are assumed to not support backing files, |
| 3366 | * hence all their sectors are reported as allocated. |
| 3367 | * |
Stefan Hajnoczi | bd9533e | 2011-11-29 13:49:51 +0000 | [diff] [blame] | 3368 | * If 'sector_num' is beyond the end of the disk image the return value is 0 |
| 3369 | * and 'pnum' is set to 0. |
| 3370 | * |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 3371 | * 'pnum' is set to the number of sectors (including and immediately following |
| 3372 | * the specified sector) that are known to be in the same |
| 3373 | * allocated/unallocated state. |
| 3374 | * |
Stefan Hajnoczi | bd9533e | 2011-11-29 13:49:51 +0000 | [diff] [blame] | 3375 | * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes |
| 3376 | * beyond the end of the disk image it will be clamped. |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 3377 | */ |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 3378 | static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs, |
| 3379 | int64_t sector_num, |
| 3380 | int nb_sectors, int *pnum) |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 3381 | { |
Paolo Bonzini | 617ccb4 | 2013-09-04 19:00:23 +0200 | [diff] [blame] | 3382 | int64_t length; |
Stefan Hajnoczi | bd9533e | 2011-11-29 13:49:51 +0000 | [diff] [blame] | 3383 | int64_t n; |
Paolo Bonzini | 5daa74a | 2013-09-04 19:00:38 +0200 | [diff] [blame] | 3384 | int64_t ret, ret2; |
Stefan Hajnoczi | bd9533e | 2011-11-29 13:49:51 +0000 | [diff] [blame] | 3385 | |
Paolo Bonzini | 617ccb4 | 2013-09-04 19:00:23 +0200 | [diff] [blame] | 3386 | length = bdrv_getlength(bs); |
| 3387 | if (length < 0) { |
| 3388 | return length; |
| 3389 | } |
| 3390 | |
| 3391 | if (sector_num >= (length >> BDRV_SECTOR_BITS)) { |
Stefan Hajnoczi | bd9533e | 2011-11-29 13:49:51 +0000 | [diff] [blame] | 3392 | *pnum = 0; |
| 3393 | return 0; |
| 3394 | } |
| 3395 | |
| 3396 | n = bs->total_sectors - sector_num; |
| 3397 | if (n < nb_sectors) { |
| 3398 | nb_sectors = n; |
| 3399 | } |
| 3400 | |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 3401 | if (!bs->drv->bdrv_co_get_block_status) { |
Stefan Hajnoczi | bd9533e | 2011-11-29 13:49:51 +0000 | [diff] [blame] | 3402 | *pnum = nb_sectors; |
Paolo Bonzini | 918e92d | 2013-09-04 19:00:37 +0200 | [diff] [blame] | 3403 | ret = BDRV_BLOCK_DATA; |
| 3404 | if (bs->drv->protocol_name) { |
| 3405 | ret |= BDRV_BLOCK_OFFSET_VALID | (sector_num * BDRV_SECTOR_SIZE); |
| 3406 | } |
| 3407 | return ret; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 3408 | } |
Stefan Hajnoczi | 6aebab1 | 2011-11-14 12:44:25 +0000 | [diff] [blame] | 3409 | |
Paolo Bonzini | 415b5b0 | 2013-09-04 19:00:31 +0200 | [diff] [blame] | 3410 | ret = bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, pnum); |
| 3411 | if (ret < 0) { |
Peter Lieven | 3e0a233 | 2013-09-24 15:35:08 +0200 | [diff] [blame] | 3412 | *pnum = 0; |
Paolo Bonzini | 415b5b0 | 2013-09-04 19:00:31 +0200 | [diff] [blame] | 3413 | return ret; |
| 3414 | } |
| 3415 | |
Peter Lieven | 92bc50a | 2013-10-08 14:43:14 +0200 | [diff] [blame] | 3416 | if (ret & BDRV_BLOCK_RAW) { |
| 3417 | assert(ret & BDRV_BLOCK_OFFSET_VALID); |
| 3418 | return bdrv_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS, |
| 3419 | *pnum, pnum); |
| 3420 | } |
| 3421 | |
Peter Lieven | c3d8688 | 2013-10-24 12:07:04 +0200 | [diff] [blame] | 3422 | if (!(ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO)) { |
| 3423 | if (bdrv_unallocated_blocks_are_zero(bs)) { |
Paolo Bonzini | f0ad571 | 2013-09-04 19:00:32 +0200 | [diff] [blame] | 3424 | ret |= BDRV_BLOCK_ZERO; |
Peter Lieven | 1f9db22 | 2013-09-24 15:35:09 +0200 | [diff] [blame] | 3425 | } else if (bs->backing_hd) { |
Paolo Bonzini | f0ad571 | 2013-09-04 19:00:32 +0200 | [diff] [blame] | 3426 | BlockDriverState *bs2 = bs->backing_hd; |
| 3427 | int64_t length2 = bdrv_getlength(bs2); |
| 3428 | if (length2 >= 0 && sector_num >= (length2 >> BDRV_SECTOR_BITS)) { |
| 3429 | ret |= BDRV_BLOCK_ZERO; |
| 3430 | } |
| 3431 | } |
Paolo Bonzini | 415b5b0 | 2013-09-04 19:00:31 +0200 | [diff] [blame] | 3432 | } |
Paolo Bonzini | 5daa74a | 2013-09-04 19:00:38 +0200 | [diff] [blame] | 3433 | |
| 3434 | if (bs->file && |
| 3435 | (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) && |
| 3436 | (ret & BDRV_BLOCK_OFFSET_VALID)) { |
| 3437 | ret2 = bdrv_co_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS, |
| 3438 | *pnum, pnum); |
| 3439 | if (ret2 >= 0) { |
| 3440 | /* Ignore errors. This is just providing extra information, it |
| 3441 | * is useful but not necessary. |
| 3442 | */ |
| 3443 | ret |= (ret2 & BDRV_BLOCK_ZERO); |
| 3444 | } |
| 3445 | } |
| 3446 | |
Paolo Bonzini | 415b5b0 | 2013-09-04 19:00:31 +0200 | [diff] [blame] | 3447 | return ret; |
Stefan Hajnoczi | 060f51c | 2011-11-14 12:44:26 +0000 | [diff] [blame] | 3448 | } |
| 3449 | |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 3450 | /* Coroutine wrapper for bdrv_get_block_status() */ |
| 3451 | static void coroutine_fn bdrv_get_block_status_co_entry(void *opaque) |
Stefan Hajnoczi | 060f51c | 2011-11-14 12:44:26 +0000 | [diff] [blame] | 3452 | { |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 3453 | BdrvCoGetBlockStatusData *data = opaque; |
Stefan Hajnoczi | 060f51c | 2011-11-14 12:44:26 +0000 | [diff] [blame] | 3454 | BlockDriverState *bs = data->bs; |
| 3455 | |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 3456 | data->ret = bdrv_co_get_block_status(bs, data->sector_num, data->nb_sectors, |
| 3457 | data->pnum); |
Stefan Hajnoczi | 060f51c | 2011-11-14 12:44:26 +0000 | [diff] [blame] | 3458 | data->done = true; |
| 3459 | } |
| 3460 | |
| 3461 | /* |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 3462 | * Synchronous wrapper around bdrv_co_get_block_status(). |
Stefan Hajnoczi | 060f51c | 2011-11-14 12:44:26 +0000 | [diff] [blame] | 3463 | * |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 3464 | * See bdrv_co_get_block_status() for details. |
Stefan Hajnoczi | 060f51c | 2011-11-14 12:44:26 +0000 | [diff] [blame] | 3465 | */ |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 3466 | int64_t bdrv_get_block_status(BlockDriverState *bs, int64_t sector_num, |
| 3467 | int nb_sectors, int *pnum) |
Stefan Hajnoczi | 060f51c | 2011-11-14 12:44:26 +0000 | [diff] [blame] | 3468 | { |
Stefan Hajnoczi | 6aebab1 | 2011-11-14 12:44:25 +0000 | [diff] [blame] | 3469 | Coroutine *co; |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 3470 | BdrvCoGetBlockStatusData data = { |
Stefan Hajnoczi | 6aebab1 | 2011-11-14 12:44:25 +0000 | [diff] [blame] | 3471 | .bs = bs, |
| 3472 | .sector_num = sector_num, |
| 3473 | .nb_sectors = nb_sectors, |
| 3474 | .pnum = pnum, |
| 3475 | .done = false, |
| 3476 | }; |
| 3477 | |
Paolo Bonzini | bdad13b | 2013-09-04 19:00:22 +0200 | [diff] [blame] | 3478 | if (qemu_in_coroutine()) { |
| 3479 | /* Fast-path if already in coroutine context */ |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 3480 | bdrv_get_block_status_co_entry(&data); |
Paolo Bonzini | bdad13b | 2013-09-04 19:00:22 +0200 | [diff] [blame] | 3481 | } else { |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 3482 | co = qemu_coroutine_create(bdrv_get_block_status_co_entry); |
Paolo Bonzini | bdad13b | 2013-09-04 19:00:22 +0200 | [diff] [blame] | 3483 | qemu_coroutine_enter(co, &data); |
| 3484 | while (!data.done) { |
| 3485 | qemu_aio_wait(); |
| 3486 | } |
Stefan Hajnoczi | 6aebab1 | 2011-11-14 12:44:25 +0000 | [diff] [blame] | 3487 | } |
| 3488 | return data.ret; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 3489 | } |
| 3490 | |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 3491 | int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, |
| 3492 | int nb_sectors, int *pnum) |
| 3493 | { |
Paolo Bonzini | 4333bb7 | 2013-09-04 19:00:29 +0200 | [diff] [blame] | 3494 | int64_t ret = bdrv_get_block_status(bs, sector_num, nb_sectors, pnum); |
| 3495 | if (ret < 0) { |
| 3496 | return ret; |
| 3497 | } |
| 3498 | return |
| 3499 | (ret & BDRV_BLOCK_DATA) || |
| 3500 | ((ret & BDRV_BLOCK_ZERO) && !bdrv_has_zero_init(bs)); |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 3501 | } |
| 3502 | |
Paolo Bonzini | 188a7bb | 2012-05-08 16:52:01 +0200 | [diff] [blame] | 3503 | /* |
| 3504 | * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP] |
| 3505 | * |
| 3506 | * Return true if the given sector is allocated in any image between |
| 3507 | * BASE and TOP (inclusive). BASE can be NULL to check if the given |
| 3508 | * sector is allocated in any image of the chain. Return false otherwise. |
| 3509 | * |
| 3510 | * 'pnum' is set to the number of sectors (including and immediately following |
| 3511 | * the specified sector) that are known to be in the same |
| 3512 | * allocated/unallocated state. |
| 3513 | * |
| 3514 | */ |
Paolo Bonzini | 4f57863 | 2013-09-04 19:00:24 +0200 | [diff] [blame] | 3515 | int bdrv_is_allocated_above(BlockDriverState *top, |
| 3516 | BlockDriverState *base, |
| 3517 | int64_t sector_num, |
| 3518 | int nb_sectors, int *pnum) |
Paolo Bonzini | 188a7bb | 2012-05-08 16:52:01 +0200 | [diff] [blame] | 3519 | { |
| 3520 | BlockDriverState *intermediate; |
| 3521 | int ret, n = nb_sectors; |
| 3522 | |
| 3523 | intermediate = top; |
| 3524 | while (intermediate && intermediate != base) { |
| 3525 | int pnum_inter; |
Paolo Bonzini | bdad13b | 2013-09-04 19:00:22 +0200 | [diff] [blame] | 3526 | ret = bdrv_is_allocated(intermediate, sector_num, nb_sectors, |
| 3527 | &pnum_inter); |
Paolo Bonzini | 188a7bb | 2012-05-08 16:52:01 +0200 | [diff] [blame] | 3528 | if (ret < 0) { |
| 3529 | return ret; |
| 3530 | } else if (ret) { |
| 3531 | *pnum = pnum_inter; |
| 3532 | return 1; |
| 3533 | } |
| 3534 | |
| 3535 | /* |
| 3536 | * [sector_num, nb_sectors] is unallocated on top but intermediate |
| 3537 | * might have |
| 3538 | * |
| 3539 | * [sector_num+x, nr_sectors] allocated. |
| 3540 | */ |
Vishvananda Ishaya | 63ba17d | 2013-01-24 10:02:08 -0800 | [diff] [blame] | 3541 | if (n > pnum_inter && |
| 3542 | (intermediate == top || |
| 3543 | sector_num + pnum_inter < intermediate->total_sectors)) { |
Paolo Bonzini | 188a7bb | 2012-05-08 16:52:01 +0200 | [diff] [blame] | 3544 | n = pnum_inter; |
| 3545 | } |
| 3546 | |
| 3547 | intermediate = intermediate->backing_hd; |
| 3548 | } |
| 3549 | |
| 3550 | *pnum = n; |
| 3551 | return 0; |
| 3552 | } |
| 3553 | |
aliguori | 045df33 | 2009-03-05 23:00:48 +0000 | [diff] [blame] | 3554 | const char *bdrv_get_encrypted_filename(BlockDriverState *bs) |
| 3555 | { |
| 3556 | if (bs->backing_hd && bs->backing_hd->encrypted) |
| 3557 | return bs->backing_file; |
| 3558 | else if (bs->encrypted) |
| 3559 | return bs->filename; |
| 3560 | else |
| 3561 | return NULL; |
| 3562 | } |
| 3563 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3564 | void bdrv_get_backing_filename(BlockDriverState *bs, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3565 | char *filename, int filename_size) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3566 | { |
Kevin Wolf | 3574c60 | 2011-10-26 11:02:11 +0200 | [diff] [blame] | 3567 | pstrcpy(filename, filename_size, bs->backing_file); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3568 | } |
| 3569 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3570 | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 3571 | const uint8_t *buf, int nb_sectors) |
| 3572 | { |
| 3573 | BlockDriver *drv = bs->drv; |
| 3574 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 3575 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 3576 | if (!drv->bdrv_write_compressed) |
| 3577 | return -ENOTSUP; |
Kevin Wolf | fbb7b4e | 2009-05-08 14:47:24 +0200 | [diff] [blame] | 3578 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
| 3579 | return -EIO; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 3580 | |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 3581 | assert(QLIST_EMPTY(&bs->dirty_bitmaps)); |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 3582 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 3583 | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); |
| 3584 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3585 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 3586 | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
| 3587 | { |
| 3588 | BlockDriver *drv = bs->drv; |
| 3589 | if (!drv) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 3590 | return -ENOMEDIUM; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 3591 | if (!drv->bdrv_get_info) |
| 3592 | return -ENOTSUP; |
| 3593 | memset(bdi, 0, sizeof(*bdi)); |
| 3594 | return drv->bdrv_get_info(bs, bdi); |
| 3595 | } |
| 3596 | |
Max Reitz | eae041f | 2013-10-09 10:46:16 +0200 | [diff] [blame] | 3597 | ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs) |
| 3598 | { |
| 3599 | BlockDriver *drv = bs->drv; |
| 3600 | if (drv && drv->bdrv_get_specific_info) { |
| 3601 | return drv->bdrv_get_specific_info(bs); |
| 3602 | } |
| 3603 | return NULL; |
| 3604 | } |
| 3605 | |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 3606 | int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, |
| 3607 | int64_t pos, int size) |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 3608 | { |
Kevin Wolf | cf8074b | 2013-04-05 21:27:53 +0200 | [diff] [blame] | 3609 | QEMUIOVector qiov; |
| 3610 | struct iovec iov = { |
| 3611 | .iov_base = (void *) buf, |
| 3612 | .iov_len = size, |
| 3613 | }; |
| 3614 | |
| 3615 | qemu_iovec_init_external(&qiov, &iov, 1); |
| 3616 | return bdrv_writev_vmstate(bs, &qiov, pos); |
| 3617 | } |
| 3618 | |
| 3619 | int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos) |
| 3620 | { |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 3621 | BlockDriver *drv = bs->drv; |
Kevin Wolf | cf8074b | 2013-04-05 21:27:53 +0200 | [diff] [blame] | 3622 | |
| 3623 | if (!drv) { |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 3624 | return -ENOMEDIUM; |
Kevin Wolf | cf8074b | 2013-04-05 21:27:53 +0200 | [diff] [blame] | 3625 | } else if (drv->bdrv_save_vmstate) { |
| 3626 | return drv->bdrv_save_vmstate(bs, qiov, pos); |
| 3627 | } else if (bs->file) { |
| 3628 | return bdrv_writev_vmstate(bs->file, qiov, pos); |
| 3629 | } |
| 3630 | |
MORITA Kazutaka | 7cdb1f6 | 2010-05-28 11:44:58 +0900 | [diff] [blame] | 3631 | return -ENOTSUP; |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 3634 | int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, |
| 3635 | int64_t pos, int size) |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 3636 | { |
| 3637 | BlockDriver *drv = bs->drv; |
| 3638 | if (!drv) |
| 3639 | return -ENOMEDIUM; |
MORITA Kazutaka | 7cdb1f6 | 2010-05-28 11:44:58 +0900 | [diff] [blame] | 3640 | if (drv->bdrv_load_vmstate) |
| 3641 | return drv->bdrv_load_vmstate(bs, buf, pos, size); |
| 3642 | if (bs->file) |
| 3643 | return bdrv_load_vmstate(bs->file, buf, pos, size); |
| 3644 | return -ENOTSUP; |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 3647 | void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) |
| 3648 | { |
Kevin Wolf | bf736fe | 2013-06-05 15:17:55 +0200 | [diff] [blame] | 3649 | if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) { |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 3650 | return; |
| 3651 | } |
| 3652 | |
Kevin Wolf | bf736fe | 2013-06-05 15:17:55 +0200 | [diff] [blame] | 3653 | bs->drv->bdrv_debug_event(bs, event); |
Kevin Wolf | 41c695c | 2012-12-06 14:32:58 +0100 | [diff] [blame] | 3654 | } |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 3655 | |
Kevin Wolf | 41c695c | 2012-12-06 14:32:58 +0100 | [diff] [blame] | 3656 | int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event, |
| 3657 | const char *tag) |
| 3658 | { |
| 3659 | while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) { |
| 3660 | bs = bs->file; |
| 3661 | } |
| 3662 | |
| 3663 | if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) { |
| 3664 | return bs->drv->bdrv_debug_breakpoint(bs, event, tag); |
| 3665 | } |
| 3666 | |
| 3667 | return -ENOTSUP; |
| 3668 | } |
| 3669 | |
Fam Zheng | 4cc70e9 | 2013-11-20 10:01:54 +0800 | [diff] [blame] | 3670 | int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag) |
| 3671 | { |
| 3672 | while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) { |
| 3673 | bs = bs->file; |
| 3674 | } |
| 3675 | |
| 3676 | if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) { |
| 3677 | return bs->drv->bdrv_debug_remove_breakpoint(bs, tag); |
| 3678 | } |
| 3679 | |
| 3680 | return -ENOTSUP; |
| 3681 | } |
| 3682 | |
Kevin Wolf | 41c695c | 2012-12-06 14:32:58 +0100 | [diff] [blame] | 3683 | int bdrv_debug_resume(BlockDriverState *bs, const char *tag) |
| 3684 | { |
| 3685 | while (bs && bs->drv && !bs->drv->bdrv_debug_resume) { |
| 3686 | bs = bs->file; |
| 3687 | } |
| 3688 | |
| 3689 | if (bs && bs->drv && bs->drv->bdrv_debug_resume) { |
| 3690 | return bs->drv->bdrv_debug_resume(bs, tag); |
| 3691 | } |
| 3692 | |
| 3693 | return -ENOTSUP; |
| 3694 | } |
| 3695 | |
| 3696 | bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag) |
| 3697 | { |
| 3698 | while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) { |
| 3699 | bs = bs->file; |
| 3700 | } |
| 3701 | |
| 3702 | if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) { |
| 3703 | return bs->drv->bdrv_debug_is_suspended(bs, tag); |
| 3704 | } |
| 3705 | |
| 3706 | return false; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 3707 | } |
| 3708 | |
Blue Swirl | 199630b | 2010-07-25 20:49:34 +0000 | [diff] [blame] | 3709 | int bdrv_is_snapshot(BlockDriverState *bs) |
| 3710 | { |
| 3711 | return !!(bs->open_flags & BDRV_O_SNAPSHOT); |
| 3712 | } |
| 3713 | |
Jeff Cody | b1b1d78 | 2012-10-16 15:49:09 -0400 | [diff] [blame] | 3714 | /* backing_file can either be relative, or absolute, or a protocol. If it is |
| 3715 | * relative, it must be relative to the chain. So, passing in bs->filename |
| 3716 | * from a BDS as backing_file should not be done, as that may be relative to |
| 3717 | * the CWD rather than the chain. */ |
Marcelo Tosatti | e8a6bb9 | 2012-01-18 14:40:51 +0000 | [diff] [blame] | 3718 | BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, |
| 3719 | const char *backing_file) |
| 3720 | { |
Jeff Cody | b1b1d78 | 2012-10-16 15:49:09 -0400 | [diff] [blame] | 3721 | char *filename_full = NULL; |
| 3722 | char *backing_file_full = NULL; |
| 3723 | char *filename_tmp = NULL; |
| 3724 | int is_protocol = 0; |
| 3725 | BlockDriverState *curr_bs = NULL; |
| 3726 | BlockDriverState *retval = NULL; |
| 3727 | |
| 3728 | if (!bs || !bs->drv || !backing_file) { |
Marcelo Tosatti | e8a6bb9 | 2012-01-18 14:40:51 +0000 | [diff] [blame] | 3729 | return NULL; |
| 3730 | } |
| 3731 | |
Jeff Cody | b1b1d78 | 2012-10-16 15:49:09 -0400 | [diff] [blame] | 3732 | filename_full = g_malloc(PATH_MAX); |
| 3733 | backing_file_full = g_malloc(PATH_MAX); |
| 3734 | filename_tmp = g_malloc(PATH_MAX); |
| 3735 | |
| 3736 | is_protocol = path_has_protocol(backing_file); |
| 3737 | |
| 3738 | for (curr_bs = bs; curr_bs->backing_hd; curr_bs = curr_bs->backing_hd) { |
| 3739 | |
| 3740 | /* If either of the filename paths is actually a protocol, then |
| 3741 | * compare unmodified paths; otherwise make paths relative */ |
| 3742 | if (is_protocol || path_has_protocol(curr_bs->backing_file)) { |
| 3743 | if (strcmp(backing_file, curr_bs->backing_file) == 0) { |
| 3744 | retval = curr_bs->backing_hd; |
| 3745 | break; |
| 3746 | } |
Marcelo Tosatti | e8a6bb9 | 2012-01-18 14:40:51 +0000 | [diff] [blame] | 3747 | } else { |
Jeff Cody | b1b1d78 | 2012-10-16 15:49:09 -0400 | [diff] [blame] | 3748 | /* If not an absolute filename path, make it relative to the current |
| 3749 | * image's filename path */ |
| 3750 | path_combine(filename_tmp, PATH_MAX, curr_bs->filename, |
| 3751 | backing_file); |
| 3752 | |
| 3753 | /* We are going to compare absolute pathnames */ |
| 3754 | if (!realpath(filename_tmp, filename_full)) { |
| 3755 | continue; |
| 3756 | } |
| 3757 | |
| 3758 | /* We need to make sure the backing filename we are comparing against |
| 3759 | * is relative to the current image filename (or absolute) */ |
| 3760 | path_combine(filename_tmp, PATH_MAX, curr_bs->filename, |
| 3761 | curr_bs->backing_file); |
| 3762 | |
| 3763 | if (!realpath(filename_tmp, backing_file_full)) { |
| 3764 | continue; |
| 3765 | } |
| 3766 | |
| 3767 | if (strcmp(backing_file_full, filename_full) == 0) { |
| 3768 | retval = curr_bs->backing_hd; |
| 3769 | break; |
| 3770 | } |
Marcelo Tosatti | e8a6bb9 | 2012-01-18 14:40:51 +0000 | [diff] [blame] | 3771 | } |
| 3772 | } |
| 3773 | |
Jeff Cody | b1b1d78 | 2012-10-16 15:49:09 -0400 | [diff] [blame] | 3774 | g_free(filename_full); |
| 3775 | g_free(backing_file_full); |
| 3776 | g_free(filename_tmp); |
| 3777 | return retval; |
Marcelo Tosatti | e8a6bb9 | 2012-01-18 14:40:51 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
Benoît Canet | f198fd1 | 2012-08-02 10:22:47 +0200 | [diff] [blame] | 3780 | int bdrv_get_backing_file_depth(BlockDriverState *bs) |
| 3781 | { |
| 3782 | if (!bs->drv) { |
| 3783 | return 0; |
| 3784 | } |
| 3785 | |
| 3786 | if (!bs->backing_hd) { |
| 3787 | return 0; |
| 3788 | } |
| 3789 | |
| 3790 | return 1 + bdrv_get_backing_file_depth(bs->backing_hd); |
| 3791 | } |
| 3792 | |
Jeff Cody | 79fac56 | 2012-09-27 13:29:15 -0400 | [diff] [blame] | 3793 | BlockDriverState *bdrv_find_base(BlockDriverState *bs) |
| 3794 | { |
| 3795 | BlockDriverState *curr_bs = NULL; |
| 3796 | |
| 3797 | if (!bs) { |
| 3798 | return NULL; |
| 3799 | } |
| 3800 | |
| 3801 | curr_bs = bs; |
| 3802 | |
| 3803 | while (curr_bs->backing_hd) { |
| 3804 | curr_bs = curr_bs->backing_hd; |
| 3805 | } |
| 3806 | return curr_bs; |
| 3807 | } |
| 3808 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3809 | /**************************************************************/ |
| 3810 | /* async I/Os */ |
| 3811 | |
aliguori | 3b69e4b | 2009-01-22 16:59:24 +0000 | [diff] [blame] | 3812 | BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 3813 | QEMUIOVector *qiov, int nb_sectors, |
aliguori | 3b69e4b | 2009-01-22 16:59:24 +0000 | [diff] [blame] | 3814 | BlockDriverCompletionFunc *cb, void *opaque) |
| 3815 | { |
Stefan Hajnoczi | bbf0a44 | 2010-10-05 14:28:53 +0100 | [diff] [blame] | 3816 | trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); |
| 3817 | |
Paolo Bonzini | d20d9b7 | 2013-11-22 13:39:44 +0100 | [diff] [blame] | 3818 | return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0, |
Stefan Hajnoczi | 8c5873d | 2011-10-13 21:09:28 +0100 | [diff] [blame] | 3819 | cb, opaque, false); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3820 | } |
| 3821 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 3822 | BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, |
| 3823 | QEMUIOVector *qiov, int nb_sectors, |
| 3824 | BlockDriverCompletionFunc *cb, void *opaque) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3825 | { |
Stefan Hajnoczi | bbf0a44 | 2010-10-05 14:28:53 +0100 | [diff] [blame] | 3826 | trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); |
| 3827 | |
Paolo Bonzini | d20d9b7 | 2013-11-22 13:39:44 +0100 | [diff] [blame] | 3828 | return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0, |
Stefan Hajnoczi | 8c5873d | 2011-10-13 21:09:28 +0100 | [diff] [blame] | 3829 | cb, opaque, true); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3830 | } |
| 3831 | |
Paolo Bonzini | d5ef94d | 2013-11-22 13:39:46 +0100 | [diff] [blame] | 3832 | BlockDriverAIOCB *bdrv_aio_write_zeroes(BlockDriverState *bs, |
| 3833 | int64_t sector_num, int nb_sectors, BdrvRequestFlags flags, |
| 3834 | BlockDriverCompletionFunc *cb, void *opaque) |
| 3835 | { |
| 3836 | trace_bdrv_aio_write_zeroes(bs, sector_num, nb_sectors, flags, opaque); |
| 3837 | |
| 3838 | return bdrv_co_aio_rw_vector(bs, sector_num, NULL, nb_sectors, |
| 3839 | BDRV_REQ_ZERO_WRITE | flags, |
| 3840 | cb, opaque, true); |
| 3841 | } |
| 3842 | |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 3843 | |
| 3844 | typedef struct MultiwriteCB { |
| 3845 | int error; |
| 3846 | int num_requests; |
| 3847 | int num_callbacks; |
| 3848 | struct { |
| 3849 | BlockDriverCompletionFunc *cb; |
| 3850 | void *opaque; |
| 3851 | QEMUIOVector *free_qiov; |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 3852 | } callbacks[]; |
| 3853 | } MultiwriteCB; |
| 3854 | |
| 3855 | static void multiwrite_user_cb(MultiwriteCB *mcb) |
| 3856 | { |
| 3857 | int i; |
| 3858 | |
| 3859 | for (i = 0; i < mcb->num_callbacks; i++) { |
| 3860 | mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); |
Stefan Hajnoczi | 1e1ea48 | 2010-04-21 20:35:45 +0100 | [diff] [blame] | 3861 | if (mcb->callbacks[i].free_qiov) { |
| 3862 | qemu_iovec_destroy(mcb->callbacks[i].free_qiov); |
| 3863 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 3864 | g_free(mcb->callbacks[i].free_qiov); |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 3865 | } |
| 3866 | } |
| 3867 | |
| 3868 | static void multiwrite_cb(void *opaque, int ret) |
| 3869 | { |
| 3870 | MultiwriteCB *mcb = opaque; |
| 3871 | |
Stefan Hajnoczi | 6d519a5 | 2010-05-22 18:15:08 +0100 | [diff] [blame] | 3872 | trace_multiwrite_cb(mcb, ret); |
| 3873 | |
Kevin Wolf | cb6d3ca | 2010-04-01 22:48:44 +0200 | [diff] [blame] | 3874 | if (ret < 0 && !mcb->error) { |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 3875 | mcb->error = ret; |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 3876 | } |
| 3877 | |
| 3878 | mcb->num_requests--; |
| 3879 | if (mcb->num_requests == 0) { |
Kevin Wolf | de189a1 | 2010-07-01 16:08:51 +0200 | [diff] [blame] | 3880 | multiwrite_user_cb(mcb); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 3881 | g_free(mcb); |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 3882 | } |
| 3883 | } |
| 3884 | |
| 3885 | static int multiwrite_req_compare(const void *a, const void *b) |
| 3886 | { |
Christoph Hellwig | 77be436 | 2010-05-19 20:53:10 +0200 | [diff] [blame] | 3887 | const BlockRequest *req1 = a, *req2 = b; |
| 3888 | |
| 3889 | /* |
| 3890 | * Note that we can't simply subtract req2->sector from req1->sector |
| 3891 | * here as that could overflow the return value. |
| 3892 | */ |
| 3893 | if (req1->sector > req2->sector) { |
| 3894 | return 1; |
| 3895 | } else if (req1->sector < req2->sector) { |
| 3896 | return -1; |
| 3897 | } else { |
| 3898 | return 0; |
| 3899 | } |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 3900 | } |
| 3901 | |
| 3902 | /* |
| 3903 | * Takes a bunch of requests and tries to merge them. Returns the number of |
| 3904 | * requests that remain after merging. |
| 3905 | */ |
| 3906 | static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, |
| 3907 | int num_reqs, MultiwriteCB *mcb) |
| 3908 | { |
| 3909 | int i, outidx; |
| 3910 | |
| 3911 | // Sort requests by start sector |
| 3912 | qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); |
| 3913 | |
| 3914 | // Check if adjacent requests touch the same clusters. If so, combine them, |
| 3915 | // filling up gaps with zero sectors. |
| 3916 | outidx = 0; |
| 3917 | for (i = 1; i < num_reqs; i++) { |
| 3918 | int merge = 0; |
| 3919 | int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; |
| 3920 | |
Paolo Bonzini | b6a127a | 2012-02-21 16:43:52 +0100 | [diff] [blame] | 3921 | // Handle exactly sequential writes and overlapping writes. |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 3922 | if (reqs[i].sector <= oldreq_last) { |
| 3923 | merge = 1; |
| 3924 | } |
| 3925 | |
Christoph Hellwig | e2a305f | 2010-01-26 14:49:08 +0100 | [diff] [blame] | 3926 | if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { |
| 3927 | merge = 0; |
| 3928 | } |
| 3929 | |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 3930 | if (merge) { |
| 3931 | size_t size; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 3932 | QEMUIOVector *qiov = g_malloc0(sizeof(*qiov)); |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 3933 | qemu_iovec_init(qiov, |
| 3934 | reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); |
| 3935 | |
| 3936 | // Add the first request to the merged one. If the requests are |
| 3937 | // overlapping, drop the last sectors of the first request. |
| 3938 | size = (reqs[i].sector - reqs[outidx].sector) << 9; |
Michael Tokarev | 1b093c4 | 2012-03-12 21:28:06 +0400 | [diff] [blame] | 3939 | qemu_iovec_concat(qiov, reqs[outidx].qiov, 0, size); |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 3940 | |
Paolo Bonzini | b6a127a | 2012-02-21 16:43:52 +0100 | [diff] [blame] | 3941 | // We should need to add any zeros between the two requests |
| 3942 | assert (reqs[i].sector <= oldreq_last); |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 3943 | |
| 3944 | // Add the second request |
Michael Tokarev | 1b093c4 | 2012-03-12 21:28:06 +0400 | [diff] [blame] | 3945 | qemu_iovec_concat(qiov, reqs[i].qiov, 0, reqs[i].qiov->size); |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 3946 | |
Kevin Wolf | cbf1dff | 2010-05-21 11:09:42 +0200 | [diff] [blame] | 3947 | reqs[outidx].nb_sectors = qiov->size >> 9; |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 3948 | reqs[outidx].qiov = qiov; |
| 3949 | |
| 3950 | mcb->callbacks[i].free_qiov = reqs[outidx].qiov; |
| 3951 | } else { |
| 3952 | outidx++; |
| 3953 | reqs[outidx].sector = reqs[i].sector; |
| 3954 | reqs[outidx].nb_sectors = reqs[i].nb_sectors; |
| 3955 | reqs[outidx].qiov = reqs[i].qiov; |
| 3956 | } |
| 3957 | } |
| 3958 | |
| 3959 | return outidx + 1; |
| 3960 | } |
| 3961 | |
| 3962 | /* |
| 3963 | * Submit multiple AIO write requests at once. |
| 3964 | * |
| 3965 | * On success, the function returns 0 and all requests in the reqs array have |
| 3966 | * been submitted. In error case this function returns -1, and any of the |
| 3967 | * requests may or may not be submitted yet. In particular, this means that the |
| 3968 | * callback will be called for some of the requests, for others it won't. The |
| 3969 | * caller must check the error field of the BlockRequest to wait for the right |
| 3970 | * callbacks (if error != 0, no callback will be called). |
| 3971 | * |
| 3972 | * The implementation may modify the contents of the reqs array, e.g. to merge |
| 3973 | * requests. However, the fields opaque and error are left unmodified as they |
| 3974 | * are used to signal failure for a single request to the caller. |
| 3975 | */ |
| 3976 | int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) |
| 3977 | { |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 3978 | MultiwriteCB *mcb; |
| 3979 | int i; |
| 3980 | |
Ryan Harper | 301db7c | 2011-03-07 10:01:04 -0600 | [diff] [blame] | 3981 | /* don't submit writes if we don't have a medium */ |
| 3982 | if (bs->drv == NULL) { |
| 3983 | for (i = 0; i < num_reqs; i++) { |
| 3984 | reqs[i].error = -ENOMEDIUM; |
| 3985 | } |
| 3986 | return -1; |
| 3987 | } |
| 3988 | |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 3989 | if (num_reqs == 0) { |
| 3990 | return 0; |
| 3991 | } |
| 3992 | |
| 3993 | // Create MultiwriteCB structure |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 3994 | mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 3995 | mcb->num_requests = 0; |
| 3996 | mcb->num_callbacks = num_reqs; |
| 3997 | |
| 3998 | for (i = 0; i < num_reqs; i++) { |
| 3999 | mcb->callbacks[i].cb = reqs[i].cb; |
| 4000 | mcb->callbacks[i].opaque = reqs[i].opaque; |
| 4001 | } |
| 4002 | |
| 4003 | // Check for mergable requests |
| 4004 | num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); |
| 4005 | |
Stefan Hajnoczi | 6d519a5 | 2010-05-22 18:15:08 +0100 | [diff] [blame] | 4006 | trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); |
| 4007 | |
Paolo Bonzini | df9309f | 2011-11-14 17:50:50 +0100 | [diff] [blame] | 4008 | /* Run the aio requests. */ |
| 4009 | mcb->num_requests = num_reqs; |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 4010 | for (i = 0; i < num_reqs; i++) { |
Paolo Bonzini | d20d9b7 | 2013-11-22 13:39:44 +0100 | [diff] [blame] | 4011 | bdrv_co_aio_rw_vector(bs, reqs[i].sector, reqs[i].qiov, |
| 4012 | reqs[i].nb_sectors, reqs[i].flags, |
| 4013 | multiwrite_cb, mcb, |
| 4014 | true); |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 4015 | } |
| 4016 | |
| 4017 | return 0; |
Kevin Wolf | 40b4f53 | 2009-09-09 17:53:37 +0200 | [diff] [blame] | 4018 | } |
| 4019 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 4020 | void bdrv_aio_cancel(BlockDriverAIOCB *acb) |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 4021 | { |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 4022 | acb->aiocb_info->cancel(acb); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
| 4025 | /**************************************************************/ |
| 4026 | /* async block device emulation */ |
| 4027 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 4028 | typedef struct BlockDriverAIOCBSync { |
| 4029 | BlockDriverAIOCB common; |
| 4030 | QEMUBH *bh; |
| 4031 | int ret; |
| 4032 | /* vector translation state */ |
| 4033 | QEMUIOVector *qiov; |
| 4034 | uint8_t *bounce; |
| 4035 | int is_write; |
| 4036 | } BlockDriverAIOCBSync; |
| 4037 | |
| 4038 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) |
| 4039 | { |
Kevin Wolf | b666d23 | 2010-05-05 11:44:39 +0200 | [diff] [blame] | 4040 | BlockDriverAIOCBSync *acb = |
| 4041 | container_of(blockacb, BlockDriverAIOCBSync, common); |
Dor Laor | 6a7ad29 | 2009-06-01 12:07:23 +0300 | [diff] [blame] | 4042 | qemu_bh_delete(acb->bh); |
Avi Kivity | 36afc45 | 2009-06-23 16:20:36 +0300 | [diff] [blame] | 4043 | acb->bh = NULL; |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 4044 | qemu_aio_release(acb); |
| 4045 | } |
| 4046 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 4047 | static const AIOCBInfo bdrv_em_aiocb_info = { |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 4048 | .aiocb_size = sizeof(BlockDriverAIOCBSync), |
| 4049 | .cancel = bdrv_aio_cancel_em, |
| 4050 | }; |
| 4051 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 4052 | static void bdrv_aio_bh_cb(void *opaque) |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 4053 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 4054 | BlockDriverAIOCBSync *acb = opaque; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 4055 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 4056 | if (!acb->is_write) |
Michael Tokarev | 0339614 | 2012-06-07 20:17:55 +0400 | [diff] [blame] | 4057 | qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); |
aliguori | ceb42de | 2009-04-07 18:43:28 +0000 | [diff] [blame] | 4058 | qemu_vfree(acb->bounce); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 4059 | acb->common.cb(acb->common.opaque, acb->ret); |
Dor Laor | 6a7ad29 | 2009-06-01 12:07:23 +0300 | [diff] [blame] | 4060 | qemu_bh_delete(acb->bh); |
Avi Kivity | 36afc45 | 2009-06-23 16:20:36 +0300 | [diff] [blame] | 4061 | acb->bh = NULL; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 4062 | qemu_aio_release(acb); |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 4063 | } |
bellard | beac80c | 2006-06-26 20:08:57 +0000 | [diff] [blame] | 4064 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 4065 | static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, |
| 4066 | int64_t sector_num, |
| 4067 | QEMUIOVector *qiov, |
| 4068 | int nb_sectors, |
| 4069 | BlockDriverCompletionFunc *cb, |
| 4070 | void *opaque, |
| 4071 | int is_write) |
| 4072 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 4073 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 4074 | BlockDriverAIOCBSync *acb; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 4075 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 4076 | acb = qemu_aio_get(&bdrv_em_aiocb_info, bs, cb, opaque); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 4077 | acb->is_write = is_write; |
| 4078 | acb->qiov = qiov; |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 4079 | acb->bounce = qemu_blockalign(bs, qiov->size); |
Paolo Bonzini | 3f3aace | 2011-11-14 17:50:54 +0100 | [diff] [blame] | 4080 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 4081 | |
| 4082 | if (is_write) { |
Michael Tokarev | d5e6b16 | 2012-06-07 20:21:06 +0400 | [diff] [blame] | 4083 | qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); |
Stefan Hajnoczi | 1ed20ac | 2011-10-13 13:08:21 +0100 | [diff] [blame] | 4084 | acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 4085 | } else { |
Stefan Hajnoczi | 1ed20ac | 2011-10-13 13:08:21 +0100 | [diff] [blame] | 4086 | acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 4087 | } |
| 4088 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 4089 | qemu_bh_schedule(acb->bh); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 4090 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 4091 | return &acb->common; |
pbrook | 7a6cba6 | 2006-06-04 11:39:07 +0000 | [diff] [blame] | 4092 | } |
| 4093 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 4094 | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, |
| 4095 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 4096 | BlockDriverCompletionFunc *cb, void *opaque) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 4097 | { |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 4098 | 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] | 4099 | } |
| 4100 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 4101 | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, |
| 4102 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
| 4103 | BlockDriverCompletionFunc *cb, void *opaque) |
| 4104 | { |
| 4105 | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); |
| 4106 | } |
| 4107 | |
Kevin Wolf | 6848542 | 2011-06-30 10:05:46 +0200 | [diff] [blame] | 4108 | |
| 4109 | typedef struct BlockDriverAIOCBCoroutine { |
| 4110 | BlockDriverAIOCB common; |
| 4111 | BlockRequest req; |
| 4112 | bool is_write; |
Kevin Wolf | d318aea | 2012-11-13 16:35:08 +0100 | [diff] [blame] | 4113 | bool *done; |
Kevin Wolf | 6848542 | 2011-06-30 10:05:46 +0200 | [diff] [blame] | 4114 | QEMUBH* bh; |
| 4115 | } BlockDriverAIOCBCoroutine; |
| 4116 | |
| 4117 | static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) |
| 4118 | { |
Kevin Wolf | d318aea | 2012-11-13 16:35:08 +0100 | [diff] [blame] | 4119 | BlockDriverAIOCBCoroutine *acb = |
| 4120 | container_of(blockacb, BlockDriverAIOCBCoroutine, common); |
| 4121 | bool done = false; |
| 4122 | |
| 4123 | acb->done = &done; |
| 4124 | while (!done) { |
| 4125 | qemu_aio_wait(); |
| 4126 | } |
Kevin Wolf | 6848542 | 2011-06-30 10:05:46 +0200 | [diff] [blame] | 4127 | } |
| 4128 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 4129 | static const AIOCBInfo bdrv_em_co_aiocb_info = { |
Kevin Wolf | 6848542 | 2011-06-30 10:05:46 +0200 | [diff] [blame] | 4130 | .aiocb_size = sizeof(BlockDriverAIOCBCoroutine), |
| 4131 | .cancel = bdrv_aio_co_cancel_em, |
| 4132 | }; |
| 4133 | |
Paolo Bonzini | 35246a6 | 2011-10-14 10:41:29 +0200 | [diff] [blame] | 4134 | static void bdrv_co_em_bh(void *opaque) |
Kevin Wolf | 6848542 | 2011-06-30 10:05:46 +0200 | [diff] [blame] | 4135 | { |
| 4136 | BlockDriverAIOCBCoroutine *acb = opaque; |
| 4137 | |
| 4138 | acb->common.cb(acb->common.opaque, acb->req.error); |
Kevin Wolf | d318aea | 2012-11-13 16:35:08 +0100 | [diff] [blame] | 4139 | |
| 4140 | if (acb->done) { |
| 4141 | *acb->done = true; |
| 4142 | } |
| 4143 | |
Kevin Wolf | 6848542 | 2011-06-30 10:05:46 +0200 | [diff] [blame] | 4144 | qemu_bh_delete(acb->bh); |
| 4145 | qemu_aio_release(acb); |
| 4146 | } |
| 4147 | |
Stefan Hajnoczi | b2a6137 | 2011-10-13 13:08:23 +0100 | [diff] [blame] | 4148 | /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */ |
| 4149 | static void coroutine_fn bdrv_co_do_rw(void *opaque) |
| 4150 | { |
| 4151 | BlockDriverAIOCBCoroutine *acb = opaque; |
| 4152 | BlockDriverState *bs = acb->common.bs; |
| 4153 | |
| 4154 | if (!acb->is_write) { |
| 4155 | acb->req.error = bdrv_co_do_readv(bs, acb->req.sector, |
Paolo Bonzini | d20d9b7 | 2013-11-22 13:39:44 +0100 | [diff] [blame] | 4156 | acb->req.nb_sectors, acb->req.qiov, acb->req.flags); |
Stefan Hajnoczi | b2a6137 | 2011-10-13 13:08:23 +0100 | [diff] [blame] | 4157 | } else { |
| 4158 | acb->req.error = bdrv_co_do_writev(bs, acb->req.sector, |
Paolo Bonzini | d20d9b7 | 2013-11-22 13:39:44 +0100 | [diff] [blame] | 4159 | acb->req.nb_sectors, acb->req.qiov, acb->req.flags); |
Stefan Hajnoczi | b2a6137 | 2011-10-13 13:08:23 +0100 | [diff] [blame] | 4160 | } |
| 4161 | |
Paolo Bonzini | 35246a6 | 2011-10-14 10:41:29 +0200 | [diff] [blame] | 4162 | acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); |
Stefan Hajnoczi | b2a6137 | 2011-10-13 13:08:23 +0100 | [diff] [blame] | 4163 | qemu_bh_schedule(acb->bh); |
| 4164 | } |
| 4165 | |
Kevin Wolf | 6848542 | 2011-06-30 10:05:46 +0200 | [diff] [blame] | 4166 | static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, |
| 4167 | int64_t sector_num, |
| 4168 | QEMUIOVector *qiov, |
| 4169 | int nb_sectors, |
Paolo Bonzini | d20d9b7 | 2013-11-22 13:39:44 +0100 | [diff] [blame] | 4170 | BdrvRequestFlags flags, |
Kevin Wolf | 6848542 | 2011-06-30 10:05:46 +0200 | [diff] [blame] | 4171 | BlockDriverCompletionFunc *cb, |
| 4172 | void *opaque, |
Stefan Hajnoczi | 8c5873d | 2011-10-13 21:09:28 +0100 | [diff] [blame] | 4173 | bool is_write) |
Kevin Wolf | 6848542 | 2011-06-30 10:05:46 +0200 | [diff] [blame] | 4174 | { |
| 4175 | Coroutine *co; |
| 4176 | BlockDriverAIOCBCoroutine *acb; |
| 4177 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 4178 | acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); |
Kevin Wolf | 6848542 | 2011-06-30 10:05:46 +0200 | [diff] [blame] | 4179 | acb->req.sector = sector_num; |
| 4180 | acb->req.nb_sectors = nb_sectors; |
| 4181 | acb->req.qiov = qiov; |
Paolo Bonzini | d20d9b7 | 2013-11-22 13:39:44 +0100 | [diff] [blame] | 4182 | acb->req.flags = flags; |
Kevin Wolf | 6848542 | 2011-06-30 10:05:46 +0200 | [diff] [blame] | 4183 | acb->is_write = is_write; |
Kevin Wolf | d318aea | 2012-11-13 16:35:08 +0100 | [diff] [blame] | 4184 | acb->done = NULL; |
Kevin Wolf | 6848542 | 2011-06-30 10:05:46 +0200 | [diff] [blame] | 4185 | |
Stefan Hajnoczi | 8c5873d | 2011-10-13 21:09:28 +0100 | [diff] [blame] | 4186 | co = qemu_coroutine_create(bdrv_co_do_rw); |
Kevin Wolf | 6848542 | 2011-06-30 10:05:46 +0200 | [diff] [blame] | 4187 | qemu_coroutine_enter(co, acb); |
| 4188 | |
| 4189 | return &acb->common; |
| 4190 | } |
| 4191 | |
Paolo Bonzini | 07f0761 | 2011-10-17 12:32:12 +0200 | [diff] [blame] | 4192 | static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 4193 | { |
Paolo Bonzini | 07f0761 | 2011-10-17 12:32:12 +0200 | [diff] [blame] | 4194 | BlockDriverAIOCBCoroutine *acb = opaque; |
| 4195 | BlockDriverState *bs = acb->common.bs; |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 4196 | |
Paolo Bonzini | 07f0761 | 2011-10-17 12:32:12 +0200 | [diff] [blame] | 4197 | acb->req.error = bdrv_co_flush(bs); |
| 4198 | acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 4199 | qemu_bh_schedule(acb->bh); |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 4200 | } |
| 4201 | |
Paolo Bonzini | 07f0761 | 2011-10-17 12:32:12 +0200 | [diff] [blame] | 4202 | BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, |
Alexander Graf | 016f5cf | 2010-05-26 17:51:49 +0200 | [diff] [blame] | 4203 | BlockDriverCompletionFunc *cb, void *opaque) |
| 4204 | { |
Paolo Bonzini | 07f0761 | 2011-10-17 12:32:12 +0200 | [diff] [blame] | 4205 | trace_bdrv_aio_flush(bs, opaque); |
Alexander Graf | 016f5cf | 2010-05-26 17:51:49 +0200 | [diff] [blame] | 4206 | |
Paolo Bonzini | 07f0761 | 2011-10-17 12:32:12 +0200 | [diff] [blame] | 4207 | Coroutine *co; |
| 4208 | BlockDriverAIOCBCoroutine *acb; |
Alexander Graf | 016f5cf | 2010-05-26 17:51:49 +0200 | [diff] [blame] | 4209 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 4210 | acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); |
Kevin Wolf | d318aea | 2012-11-13 16:35:08 +0100 | [diff] [blame] | 4211 | acb->done = NULL; |
| 4212 | |
Paolo Bonzini | 07f0761 | 2011-10-17 12:32:12 +0200 | [diff] [blame] | 4213 | co = qemu_coroutine_create(bdrv_aio_flush_co_entry); |
| 4214 | qemu_coroutine_enter(co, acb); |
Alexander Graf | 016f5cf | 2010-05-26 17:51:49 +0200 | [diff] [blame] | 4215 | |
Alexander Graf | 016f5cf | 2010-05-26 17:51:49 +0200 | [diff] [blame] | 4216 | return &acb->common; |
| 4217 | } |
| 4218 | |
Paolo Bonzini | 4265d62 | 2011-10-17 12:32:14 +0200 | [diff] [blame] | 4219 | static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque) |
| 4220 | { |
| 4221 | BlockDriverAIOCBCoroutine *acb = opaque; |
| 4222 | BlockDriverState *bs = acb->common.bs; |
| 4223 | |
| 4224 | acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors); |
| 4225 | acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); |
| 4226 | qemu_bh_schedule(acb->bh); |
| 4227 | } |
| 4228 | |
| 4229 | BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, |
| 4230 | int64_t sector_num, int nb_sectors, |
| 4231 | BlockDriverCompletionFunc *cb, void *opaque) |
| 4232 | { |
| 4233 | Coroutine *co; |
| 4234 | BlockDriverAIOCBCoroutine *acb; |
| 4235 | |
| 4236 | trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque); |
| 4237 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 4238 | acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); |
Paolo Bonzini | 4265d62 | 2011-10-17 12:32:14 +0200 | [diff] [blame] | 4239 | acb->req.sector = sector_num; |
| 4240 | acb->req.nb_sectors = nb_sectors; |
Kevin Wolf | d318aea | 2012-11-13 16:35:08 +0100 | [diff] [blame] | 4241 | acb->done = NULL; |
Paolo Bonzini | 4265d62 | 2011-10-17 12:32:14 +0200 | [diff] [blame] | 4242 | co = qemu_coroutine_create(bdrv_aio_discard_co_entry); |
| 4243 | qemu_coroutine_enter(co, acb); |
| 4244 | |
| 4245 | return &acb->common; |
| 4246 | } |
| 4247 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 4248 | void bdrv_init(void) |
| 4249 | { |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 4250 | module_call_init(MODULE_INIT_BLOCK); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 4251 | } |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 4252 | |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 4253 | void bdrv_init_with_whitelist(void) |
| 4254 | { |
| 4255 | use_bdrv_whitelist = 1; |
| 4256 | bdrv_init(); |
| 4257 | } |
| 4258 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 4259 | void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs, |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 4260 | BlockDriverCompletionFunc *cb, void *opaque) |
aliguori | 6bbff9a | 2009-03-20 18:25:59 +0000 | [diff] [blame] | 4261 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 4262 | BlockDriverAIOCB *acb; |
| 4263 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 4264 | acb = g_slice_alloc(aiocb_info->aiocb_size); |
| 4265 | acb->aiocb_info = aiocb_info; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 4266 | acb->bs = bs; |
| 4267 | acb->cb = cb; |
| 4268 | acb->opaque = opaque; |
| 4269 | return acb; |
| 4270 | } |
| 4271 | |
| 4272 | void qemu_aio_release(void *p) |
| 4273 | { |
Stefan Hajnoczi | d37c975 | 2012-10-31 16:34:36 +0100 | [diff] [blame] | 4274 | BlockDriverAIOCB *acb = p; |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 4275 | g_slice_free1(acb->aiocb_info->aiocb_size, acb); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 4276 | } |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 4277 | |
| 4278 | /**************************************************************/ |
Kevin Wolf | f9f05dc | 2011-07-15 13:50:26 +0200 | [diff] [blame] | 4279 | /* Coroutine block device emulation */ |
| 4280 | |
| 4281 | typedef struct CoroutineIOCompletion { |
| 4282 | Coroutine *coroutine; |
| 4283 | int ret; |
| 4284 | } CoroutineIOCompletion; |
| 4285 | |
| 4286 | static void bdrv_co_io_em_complete(void *opaque, int ret) |
| 4287 | { |
| 4288 | CoroutineIOCompletion *co = opaque; |
| 4289 | |
| 4290 | co->ret = ret; |
| 4291 | qemu_coroutine_enter(co->coroutine, NULL); |
| 4292 | } |
| 4293 | |
| 4294 | static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, |
| 4295 | int nb_sectors, QEMUIOVector *iov, |
| 4296 | bool is_write) |
| 4297 | { |
| 4298 | CoroutineIOCompletion co = { |
| 4299 | .coroutine = qemu_coroutine_self(), |
| 4300 | }; |
| 4301 | BlockDriverAIOCB *acb; |
| 4302 | |
| 4303 | if (is_write) { |
Stefan Hajnoczi | a652d16 | 2011-10-05 17:17:02 +0100 | [diff] [blame] | 4304 | acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors, |
| 4305 | bdrv_co_io_em_complete, &co); |
Kevin Wolf | f9f05dc | 2011-07-15 13:50:26 +0200 | [diff] [blame] | 4306 | } else { |
Stefan Hajnoczi | a652d16 | 2011-10-05 17:17:02 +0100 | [diff] [blame] | 4307 | acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors, |
| 4308 | bdrv_co_io_em_complete, &co); |
Kevin Wolf | f9f05dc | 2011-07-15 13:50:26 +0200 | [diff] [blame] | 4309 | } |
| 4310 | |
Stefan Hajnoczi | 59370aa | 2011-09-30 17:34:58 +0100 | [diff] [blame] | 4311 | 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] | 4312 | if (!acb) { |
| 4313 | return -EIO; |
| 4314 | } |
| 4315 | qemu_coroutine_yield(); |
| 4316 | |
| 4317 | return co.ret; |
| 4318 | } |
| 4319 | |
| 4320 | static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, |
| 4321 | int64_t sector_num, int nb_sectors, |
| 4322 | QEMUIOVector *iov) |
| 4323 | { |
| 4324 | return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); |
| 4325 | } |
| 4326 | |
| 4327 | static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, |
| 4328 | int64_t sector_num, int nb_sectors, |
| 4329 | QEMUIOVector *iov) |
| 4330 | { |
| 4331 | return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); |
| 4332 | } |
| 4333 | |
Paolo Bonzini | 07f0761 | 2011-10-17 12:32:12 +0200 | [diff] [blame] | 4334 | static void coroutine_fn bdrv_flush_co_entry(void *opaque) |
Kevin Wolf | e7a8a78 | 2011-07-15 16:05:00 +0200 | [diff] [blame] | 4335 | { |
Paolo Bonzini | 07f0761 | 2011-10-17 12:32:12 +0200 | [diff] [blame] | 4336 | RwCo *rwco = opaque; |
Kevin Wolf | e7a8a78 | 2011-07-15 16:05:00 +0200 | [diff] [blame] | 4337 | |
Paolo Bonzini | 07f0761 | 2011-10-17 12:32:12 +0200 | [diff] [blame] | 4338 | rwco->ret = bdrv_co_flush(rwco->bs); |
| 4339 | } |
| 4340 | |
| 4341 | int coroutine_fn bdrv_co_flush(BlockDriverState *bs) |
| 4342 | { |
Kevin Wolf | eb489bb | 2011-11-10 18:10:11 +0100 | [diff] [blame] | 4343 | int ret; |
| 4344 | |
Paolo Bonzini | 29cdb25 | 2012-03-12 18:26:01 +0100 | [diff] [blame] | 4345 | if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { |
Paolo Bonzini | 07f0761 | 2011-10-17 12:32:12 +0200 | [diff] [blame] | 4346 | return 0; |
Kevin Wolf | eb489bb | 2011-11-10 18:10:11 +0100 | [diff] [blame] | 4347 | } |
| 4348 | |
Kevin Wolf | ca71636 | 2011-11-10 18:13:59 +0100 | [diff] [blame] | 4349 | /* Write back cached data to the OS even with cache=unsafe */ |
Kevin Wolf | bf736fe | 2013-06-05 15:17:55 +0200 | [diff] [blame] | 4350 | BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_OS); |
Kevin Wolf | eb489bb | 2011-11-10 18:10:11 +0100 | [diff] [blame] | 4351 | if (bs->drv->bdrv_co_flush_to_os) { |
| 4352 | ret = bs->drv->bdrv_co_flush_to_os(bs); |
| 4353 | if (ret < 0) { |
| 4354 | return ret; |
| 4355 | } |
| 4356 | } |
| 4357 | |
Kevin Wolf | ca71636 | 2011-11-10 18:13:59 +0100 | [diff] [blame] | 4358 | /* But don't actually force it to the disk with cache=unsafe */ |
| 4359 | if (bs->open_flags & BDRV_O_NO_FLUSH) { |
Kevin Wolf | d4c8232 | 2012-08-15 12:52:45 +0200 | [diff] [blame] | 4360 | goto flush_parent; |
Kevin Wolf | ca71636 | 2011-11-10 18:13:59 +0100 | [diff] [blame] | 4361 | } |
| 4362 | |
Kevin Wolf | bf736fe | 2013-06-05 15:17:55 +0200 | [diff] [blame] | 4363 | BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK); |
Kevin Wolf | eb489bb | 2011-11-10 18:10:11 +0100 | [diff] [blame] | 4364 | if (bs->drv->bdrv_co_flush_to_disk) { |
Paolo Bonzini | 29cdb25 | 2012-03-12 18:26:01 +0100 | [diff] [blame] | 4365 | ret = bs->drv->bdrv_co_flush_to_disk(bs); |
Paolo Bonzini | 07f0761 | 2011-10-17 12:32:12 +0200 | [diff] [blame] | 4366 | } else if (bs->drv->bdrv_aio_flush) { |
| 4367 | BlockDriverAIOCB *acb; |
| 4368 | CoroutineIOCompletion co = { |
| 4369 | .coroutine = qemu_coroutine_self(), |
| 4370 | }; |
| 4371 | |
| 4372 | acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); |
| 4373 | if (acb == NULL) { |
Paolo Bonzini | 29cdb25 | 2012-03-12 18:26:01 +0100 | [diff] [blame] | 4374 | ret = -EIO; |
Paolo Bonzini | 07f0761 | 2011-10-17 12:32:12 +0200 | [diff] [blame] | 4375 | } else { |
| 4376 | qemu_coroutine_yield(); |
Paolo Bonzini | 29cdb25 | 2012-03-12 18:26:01 +0100 | [diff] [blame] | 4377 | ret = co.ret; |
Paolo Bonzini | 07f0761 | 2011-10-17 12:32:12 +0200 | [diff] [blame] | 4378 | } |
Paolo Bonzini | 07f0761 | 2011-10-17 12:32:12 +0200 | [diff] [blame] | 4379 | } else { |
| 4380 | /* |
| 4381 | * Some block drivers always operate in either writethrough or unsafe |
| 4382 | * mode and don't support bdrv_flush therefore. Usually qemu doesn't |
| 4383 | * know how the server works (because the behaviour is hardcoded or |
| 4384 | * depends on server-side configuration), so we can't ensure that |
| 4385 | * everything is safe on disk. Returning an error doesn't work because |
| 4386 | * that would break guests even if the server operates in writethrough |
| 4387 | * mode. |
| 4388 | * |
| 4389 | * Let's hope the user knows what he's doing. |
| 4390 | */ |
Paolo Bonzini | 29cdb25 | 2012-03-12 18:26:01 +0100 | [diff] [blame] | 4391 | ret = 0; |
Kevin Wolf | e7a8a78 | 2011-07-15 16:05:00 +0200 | [diff] [blame] | 4392 | } |
Paolo Bonzini | 29cdb25 | 2012-03-12 18:26:01 +0100 | [diff] [blame] | 4393 | if (ret < 0) { |
| 4394 | return ret; |
| 4395 | } |
| 4396 | |
| 4397 | /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH |
| 4398 | * in the case of cache=unsafe, so there are no useless flushes. |
| 4399 | */ |
Kevin Wolf | d4c8232 | 2012-08-15 12:52:45 +0200 | [diff] [blame] | 4400 | flush_parent: |
Paolo Bonzini | 29cdb25 | 2012-03-12 18:26:01 +0100 | [diff] [blame] | 4401 | return bdrv_co_flush(bs->file); |
Paolo Bonzini | 07f0761 | 2011-10-17 12:32:12 +0200 | [diff] [blame] | 4402 | } |
| 4403 | |
Anthony Liguori | 0f15423 | 2011-11-14 15:09:45 -0600 | [diff] [blame] | 4404 | void bdrv_invalidate_cache(BlockDriverState *bs) |
| 4405 | { |
| 4406 | if (bs->drv && bs->drv->bdrv_invalidate_cache) { |
| 4407 | bs->drv->bdrv_invalidate_cache(bs); |
| 4408 | } |
| 4409 | } |
| 4410 | |
| 4411 | void bdrv_invalidate_cache_all(void) |
| 4412 | { |
| 4413 | BlockDriverState *bs; |
| 4414 | |
Benoît Canet | dc364f4 | 2014-01-23 21:31:32 +0100 | [diff] [blame^] | 4415 | QTAILQ_FOREACH(bs, &bdrv_states, device_list) { |
Anthony Liguori | 0f15423 | 2011-11-14 15:09:45 -0600 | [diff] [blame] | 4416 | bdrv_invalidate_cache(bs); |
| 4417 | } |
| 4418 | } |
| 4419 | |
Benoît Canet | 0778926 | 2012-03-23 08:36:49 +0100 | [diff] [blame] | 4420 | void bdrv_clear_incoming_migration_all(void) |
| 4421 | { |
| 4422 | BlockDriverState *bs; |
| 4423 | |
Benoît Canet | dc364f4 | 2014-01-23 21:31:32 +0100 | [diff] [blame^] | 4424 | QTAILQ_FOREACH(bs, &bdrv_states, device_list) { |
Benoît Canet | 0778926 | 2012-03-23 08:36:49 +0100 | [diff] [blame] | 4425 | bs->open_flags = bs->open_flags & ~(BDRV_O_INCOMING); |
| 4426 | } |
| 4427 | } |
| 4428 | |
Paolo Bonzini | 07f0761 | 2011-10-17 12:32:12 +0200 | [diff] [blame] | 4429 | int bdrv_flush(BlockDriverState *bs) |
| 4430 | { |
| 4431 | Coroutine *co; |
| 4432 | RwCo rwco = { |
| 4433 | .bs = bs, |
| 4434 | .ret = NOT_DONE, |
| 4435 | }; |
| 4436 | |
| 4437 | if (qemu_in_coroutine()) { |
| 4438 | /* Fast-path if already in coroutine context */ |
| 4439 | bdrv_flush_co_entry(&rwco); |
| 4440 | } else { |
| 4441 | co = qemu_coroutine_create(bdrv_flush_co_entry); |
| 4442 | qemu_coroutine_enter(co, &rwco); |
| 4443 | while (rwco.ret == NOT_DONE) { |
| 4444 | qemu_aio_wait(); |
| 4445 | } |
| 4446 | } |
| 4447 | |
| 4448 | return rwco.ret; |
Kevin Wolf | e7a8a78 | 2011-07-15 16:05:00 +0200 | [diff] [blame] | 4449 | } |
| 4450 | |
Paolo Bonzini | 4265d62 | 2011-10-17 12:32:14 +0200 | [diff] [blame] | 4451 | static void coroutine_fn bdrv_discard_co_entry(void *opaque) |
| 4452 | { |
| 4453 | RwCo *rwco = opaque; |
| 4454 | |
| 4455 | rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors); |
| 4456 | } |
| 4457 | |
Peter Lieven | 6f14da5 | 2013-10-24 12:06:59 +0200 | [diff] [blame] | 4458 | /* if no limit is specified in the BlockLimits use a default |
| 4459 | * of 32768 512-byte sectors (16 MiB) per request. |
| 4460 | */ |
| 4461 | #define MAX_DISCARD_DEFAULT 32768 |
| 4462 | |
Paolo Bonzini | 4265d62 | 2011-10-17 12:32:14 +0200 | [diff] [blame] | 4463 | int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, |
| 4464 | int nb_sectors) |
| 4465 | { |
Paolo Bonzini | d51e9fe | 2013-11-22 13:39:43 +0100 | [diff] [blame] | 4466 | int max_discard; |
| 4467 | |
Paolo Bonzini | 4265d62 | 2011-10-17 12:32:14 +0200 | [diff] [blame] | 4468 | if (!bs->drv) { |
| 4469 | return -ENOMEDIUM; |
| 4470 | } else if (bdrv_check_request(bs, sector_num, nb_sectors)) { |
| 4471 | return -EIO; |
| 4472 | } else if (bs->read_only) { |
| 4473 | return -EROFS; |
Paolo Bonzini | df702c9 | 2013-01-14 16:26:58 +0100 | [diff] [blame] | 4474 | } |
| 4475 | |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 4476 | bdrv_reset_dirty(bs, sector_num, nb_sectors); |
Paolo Bonzini | df702c9 | 2013-01-14 16:26:58 +0100 | [diff] [blame] | 4477 | |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 4478 | /* Do nothing if disabled. */ |
| 4479 | if (!(bs->open_flags & BDRV_O_UNMAP)) { |
| 4480 | return 0; |
| 4481 | } |
| 4482 | |
Paolo Bonzini | d51e9fe | 2013-11-22 13:39:43 +0100 | [diff] [blame] | 4483 | if (!bs->drv->bdrv_co_discard && !bs->drv->bdrv_aio_discard) { |
Paolo Bonzini | 4265d62 | 2011-10-17 12:32:14 +0200 | [diff] [blame] | 4484 | return 0; |
| 4485 | } |
Paolo Bonzini | d51e9fe | 2013-11-22 13:39:43 +0100 | [diff] [blame] | 4486 | |
| 4487 | max_discard = bs->bl.max_discard ? bs->bl.max_discard : MAX_DISCARD_DEFAULT; |
| 4488 | while (nb_sectors > 0) { |
| 4489 | int ret; |
| 4490 | int num = nb_sectors; |
| 4491 | |
| 4492 | /* align request */ |
| 4493 | if (bs->bl.discard_alignment && |
| 4494 | num >= bs->bl.discard_alignment && |
| 4495 | sector_num % bs->bl.discard_alignment) { |
| 4496 | if (num > bs->bl.discard_alignment) { |
| 4497 | num = bs->bl.discard_alignment; |
| 4498 | } |
| 4499 | num -= sector_num % bs->bl.discard_alignment; |
| 4500 | } |
| 4501 | |
| 4502 | /* limit request size */ |
| 4503 | if (num > max_discard) { |
| 4504 | num = max_discard; |
| 4505 | } |
| 4506 | |
| 4507 | if (bs->drv->bdrv_co_discard) { |
| 4508 | ret = bs->drv->bdrv_co_discard(bs, sector_num, num); |
| 4509 | } else { |
| 4510 | BlockDriverAIOCB *acb; |
| 4511 | CoroutineIOCompletion co = { |
| 4512 | .coroutine = qemu_coroutine_self(), |
| 4513 | }; |
| 4514 | |
| 4515 | acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors, |
| 4516 | bdrv_co_io_em_complete, &co); |
| 4517 | if (acb == NULL) { |
| 4518 | return -EIO; |
| 4519 | } else { |
| 4520 | qemu_coroutine_yield(); |
| 4521 | ret = co.ret; |
| 4522 | } |
| 4523 | } |
Paolo Bonzini | 7ce2101 | 2013-11-22 13:39:47 +0100 | [diff] [blame] | 4524 | if (ret && ret != -ENOTSUP) { |
Paolo Bonzini | d51e9fe | 2013-11-22 13:39:43 +0100 | [diff] [blame] | 4525 | return ret; |
| 4526 | } |
| 4527 | |
| 4528 | sector_num += num; |
| 4529 | nb_sectors -= num; |
| 4530 | } |
| 4531 | return 0; |
Paolo Bonzini | 4265d62 | 2011-10-17 12:32:14 +0200 | [diff] [blame] | 4532 | } |
| 4533 | |
| 4534 | int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) |
| 4535 | { |
| 4536 | Coroutine *co; |
| 4537 | RwCo rwco = { |
| 4538 | .bs = bs, |
| 4539 | .sector_num = sector_num, |
| 4540 | .nb_sectors = nb_sectors, |
| 4541 | .ret = NOT_DONE, |
| 4542 | }; |
| 4543 | |
| 4544 | if (qemu_in_coroutine()) { |
| 4545 | /* Fast-path if already in coroutine context */ |
| 4546 | bdrv_discard_co_entry(&rwco); |
| 4547 | } else { |
| 4548 | co = qemu_coroutine_create(bdrv_discard_co_entry); |
| 4549 | qemu_coroutine_enter(co, &rwco); |
| 4550 | while (rwco.ret == NOT_DONE) { |
| 4551 | qemu_aio_wait(); |
| 4552 | } |
| 4553 | } |
| 4554 | |
| 4555 | return rwco.ret; |
| 4556 | } |
| 4557 | |
Kevin Wolf | f9f05dc | 2011-07-15 13:50:26 +0200 | [diff] [blame] | 4558 | /**************************************************************/ |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 4559 | /* removable device support */ |
| 4560 | |
| 4561 | /** |
| 4562 | * Return TRUE if the media is present |
| 4563 | */ |
| 4564 | int bdrv_is_inserted(BlockDriverState *bs) |
| 4565 | { |
| 4566 | BlockDriver *drv = bs->drv; |
Markus Armbruster | a1aff5b | 2011-09-06 18:58:41 +0200 | [diff] [blame] | 4567 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 4568 | if (!drv) |
| 4569 | return 0; |
| 4570 | if (!drv->bdrv_is_inserted) |
Markus Armbruster | a1aff5b | 2011-09-06 18:58:41 +0200 | [diff] [blame] | 4571 | return 1; |
| 4572 | return drv->bdrv_is_inserted(bs); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 4573 | } |
| 4574 | |
| 4575 | /** |
Markus Armbruster | 8e49ca4 | 2011-08-03 15:08:08 +0200 | [diff] [blame] | 4576 | * Return whether the media changed since the last call to this |
| 4577 | * function, or -ENOTSUP if we don't know. Most drivers don't know. |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 4578 | */ |
| 4579 | int bdrv_media_changed(BlockDriverState *bs) |
| 4580 | { |
| 4581 | BlockDriver *drv = bs->drv; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 4582 | |
Markus Armbruster | 8e49ca4 | 2011-08-03 15:08:08 +0200 | [diff] [blame] | 4583 | if (drv && drv->bdrv_media_changed) { |
| 4584 | return drv->bdrv_media_changed(bs); |
| 4585 | } |
| 4586 | return -ENOTSUP; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 4587 | } |
| 4588 | |
| 4589 | /** |
| 4590 | * If eject_flag is TRUE, eject the media. Otherwise, close the tray |
| 4591 | */ |
Luiz Capitulino | f36f394 | 2012-02-03 16:24:53 -0200 | [diff] [blame] | 4592 | void bdrv_eject(BlockDriverState *bs, bool eject_flag) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 4593 | { |
| 4594 | BlockDriver *drv = bs->drv; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 4595 | |
Markus Armbruster | 822e1cd | 2011-07-20 18:23:42 +0200 | [diff] [blame] | 4596 | if (drv && drv->bdrv_eject) { |
| 4597 | drv->bdrv_eject(bs, eject_flag); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 4598 | } |
Luiz Capitulino | 6f382ed | 2012-02-14 13:41:13 -0200 | [diff] [blame] | 4599 | |
| 4600 | if (bs->device_name[0] != '\0') { |
| 4601 | bdrv_emit_qmp_eject_event(bs, eject_flag); |
| 4602 | } |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 4603 | } |
| 4604 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 4605 | /** |
| 4606 | * Lock or unlock the media (if it is locked, the user won't be able |
| 4607 | * to eject it manually). |
| 4608 | */ |
Markus Armbruster | 025e849 | 2011-09-06 18:58:47 +0200 | [diff] [blame] | 4609 | void bdrv_lock_medium(BlockDriverState *bs, bool locked) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 4610 | { |
| 4611 | BlockDriver *drv = bs->drv; |
| 4612 | |
Markus Armbruster | 025e849 | 2011-09-06 18:58:47 +0200 | [diff] [blame] | 4613 | trace_bdrv_lock_medium(bs, locked); |
Stefan Hajnoczi | b8c6d09 | 2011-03-29 20:04:40 +0100 | [diff] [blame] | 4614 | |
Markus Armbruster | 025e849 | 2011-09-06 18:58:47 +0200 | [diff] [blame] | 4615 | if (drv && drv->bdrv_lock_medium) { |
| 4616 | drv->bdrv_lock_medium(bs, locked); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 4617 | } |
| 4618 | } |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 4619 | |
| 4620 | /* needed for generic scsi interface */ |
| 4621 | |
| 4622 | int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
| 4623 | { |
| 4624 | BlockDriver *drv = bs->drv; |
| 4625 | |
| 4626 | if (drv && drv->bdrv_ioctl) |
| 4627 | return drv->bdrv_ioctl(bs, req, buf); |
| 4628 | return -ENOTSUP; |
| 4629 | } |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 4630 | |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 4631 | BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, |
| 4632 | unsigned long int req, void *buf, |
| 4633 | BlockDriverCompletionFunc *cb, void *opaque) |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 4634 | { |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 4635 | BlockDriver *drv = bs->drv; |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 4636 | |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 4637 | if (drv && drv->bdrv_aio_ioctl) |
| 4638 | return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); |
| 4639 | return NULL; |
aliguori | 7d78066 | 2009-03-12 19:57:08 +0000 | [diff] [blame] | 4640 | } |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 4641 | |
Markus Armbruster | 7b6f930 | 2011-09-06 18:58:56 +0200 | [diff] [blame] | 4642 | void bdrv_set_buffer_alignment(BlockDriverState *bs, int align) |
| 4643 | { |
| 4644 | bs->buffer_alignment = align; |
| 4645 | } |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 4646 | |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 4647 | void *qemu_blockalign(BlockDriverState *bs, size_t size) |
| 4648 | { |
| 4649 | return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); |
| 4650 | } |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 4651 | |
Stefan Hajnoczi | c53b1c5 | 2013-01-11 16:41:27 +0100 | [diff] [blame] | 4652 | /* |
| 4653 | * Check if all memory in this vector is sector aligned. |
| 4654 | */ |
| 4655 | bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov) |
| 4656 | { |
| 4657 | int i; |
| 4658 | |
| 4659 | for (i = 0; i < qiov->niov; i++) { |
| 4660 | if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) { |
| 4661 | return false; |
| 4662 | } |
| 4663 | } |
| 4664 | |
| 4665 | return true; |
| 4666 | } |
| 4667 | |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 4668 | BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, int granularity) |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 4669 | { |
| 4670 | int64_t bitmap_size; |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 4671 | BdrvDirtyBitmap *bitmap; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 4672 | |
Paolo Bonzini | 50717e9 | 2013-01-21 17:09:45 +0100 | [diff] [blame] | 4673 | assert((granularity & (granularity - 1)) == 0); |
| 4674 | |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 4675 | granularity >>= BDRV_SECTOR_BITS; |
| 4676 | assert(granularity); |
| 4677 | bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS); |
| 4678 | bitmap = g_malloc0(sizeof(BdrvDirtyBitmap)); |
| 4679 | bitmap->bitmap = hbitmap_alloc(bitmap_size, ffs(granularity) - 1); |
| 4680 | QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list); |
| 4681 | return bitmap; |
| 4682 | } |
| 4683 | |
| 4684 | void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) |
| 4685 | { |
| 4686 | BdrvDirtyBitmap *bm, *next; |
| 4687 | QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) { |
| 4688 | if (bm == bitmap) { |
| 4689 | QLIST_REMOVE(bitmap, list); |
| 4690 | hbitmap_free(bitmap->bitmap); |
| 4691 | g_free(bitmap); |
| 4692 | return; |
Jan Kiszka | a55eb92 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 4693 | } |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 4694 | } |
| 4695 | } |
| 4696 | |
Fam Zheng | 21b5683 | 2013-11-13 18:29:44 +0800 | [diff] [blame] | 4697 | BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs) |
| 4698 | { |
| 4699 | BdrvDirtyBitmap *bm; |
| 4700 | BlockDirtyInfoList *list = NULL; |
| 4701 | BlockDirtyInfoList **plist = &list; |
| 4702 | |
| 4703 | QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { |
| 4704 | BlockDirtyInfo *info = g_malloc0(sizeof(BlockDirtyInfo)); |
| 4705 | BlockDirtyInfoList *entry = g_malloc0(sizeof(BlockDirtyInfoList)); |
| 4706 | info->count = bdrv_get_dirty_count(bs, bm); |
| 4707 | info->granularity = |
| 4708 | ((int64_t) BDRV_SECTOR_SIZE << hbitmap_granularity(bm->bitmap)); |
| 4709 | entry->value = info; |
| 4710 | *plist = entry; |
| 4711 | plist = &entry->next; |
| 4712 | } |
| 4713 | |
| 4714 | return list; |
| 4715 | } |
| 4716 | |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 4717 | int bdrv_get_dirty(BlockDriverState *bs, BdrvDirtyBitmap *bitmap, int64_t sector) |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 4718 | { |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 4719 | if (bitmap) { |
| 4720 | return hbitmap_get(bitmap->bitmap, sector); |
lirans@il.ibm.com | 7cd1e32 | 2009-11-02 15:40:41 +0200 | [diff] [blame] | 4721 | } else { |
| 4722 | return 0; |
| 4723 | } |
| 4724 | } |
| 4725 | |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 4726 | void bdrv_dirty_iter_init(BlockDriverState *bs, |
| 4727 | BdrvDirtyBitmap *bitmap, HBitmapIter *hbi) |
Paolo Bonzini | 1755da1 | 2012-10-18 16:49:18 +0200 | [diff] [blame] | 4728 | { |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 4729 | hbitmap_iter_init(hbi, bitmap->bitmap, 0); |
Paolo Bonzini | 1755da1 | 2012-10-18 16:49:18 +0200 | [diff] [blame] | 4730 | } |
| 4731 | |
| 4732 | void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, |
| 4733 | int nr_sectors) |
| 4734 | { |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 4735 | BdrvDirtyBitmap *bitmap; |
| 4736 | QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { |
| 4737 | hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors); |
Paolo Bonzini | 8f0720e | 2013-01-21 17:09:41 +0100 | [diff] [blame] | 4738 | } |
Liran Schour | aaa0eb7 | 2010-01-26 10:31:48 +0200 | [diff] [blame] | 4739 | } |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4740 | |
Fam Zheng | e4654d2 | 2013-11-13 18:29:43 +0800 | [diff] [blame] | 4741 | void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, int nr_sectors) |
| 4742 | { |
| 4743 | BdrvDirtyBitmap *bitmap; |
| 4744 | QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { |
| 4745 | hbitmap_reset(bitmap->bitmap, cur_sector, nr_sectors); |
| 4746 | } |
| 4747 | } |
| 4748 | |
| 4749 | int64_t bdrv_get_dirty_count(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) |
| 4750 | { |
| 4751 | return hbitmap_count(bitmap->bitmap); |
| 4752 | } |
| 4753 | |
Fam Zheng | 9fcb025 | 2013-08-23 09:14:46 +0800 | [diff] [blame] | 4754 | /* Get a reference to bs */ |
| 4755 | void bdrv_ref(BlockDriverState *bs) |
| 4756 | { |
| 4757 | bs->refcnt++; |
| 4758 | } |
| 4759 | |
| 4760 | /* Release a previously grabbed reference to bs. |
| 4761 | * If after releasing, reference count is zero, the BlockDriverState is |
| 4762 | * deleted. */ |
| 4763 | void bdrv_unref(BlockDriverState *bs) |
| 4764 | { |
| 4765 | assert(bs->refcnt > 0); |
| 4766 | if (--bs->refcnt == 0) { |
| 4767 | bdrv_delete(bs); |
| 4768 | } |
| 4769 | } |
| 4770 | |
Marcelo Tosatti | db593f2 | 2011-01-26 12:12:34 -0200 | [diff] [blame] | 4771 | void bdrv_set_in_use(BlockDriverState *bs, int in_use) |
| 4772 | { |
| 4773 | assert(bs->in_use != in_use); |
| 4774 | bs->in_use = in_use; |
| 4775 | } |
| 4776 | |
| 4777 | int bdrv_in_use(BlockDriverState *bs) |
| 4778 | { |
| 4779 | return bs->in_use; |
| 4780 | } |
| 4781 | |
Luiz Capitulino | 28a7282 | 2011-09-26 17:43:50 -0300 | [diff] [blame] | 4782 | void bdrv_iostatus_enable(BlockDriverState *bs) |
| 4783 | { |
Luiz Capitulino | d6bf279 | 2011-10-14 17:11:23 -0300 | [diff] [blame] | 4784 | bs->iostatus_enabled = true; |
Luiz Capitulino | 58e21ef | 2011-10-14 17:22:24 -0300 | [diff] [blame] | 4785 | bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; |
Luiz Capitulino | 28a7282 | 2011-09-26 17:43:50 -0300 | [diff] [blame] | 4786 | } |
| 4787 | |
| 4788 | /* The I/O status is only enabled if the drive explicitly |
| 4789 | * enables it _and_ the VM is configured to stop on errors */ |
| 4790 | bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) |
| 4791 | { |
Luiz Capitulino | d6bf279 | 2011-10-14 17:11:23 -0300 | [diff] [blame] | 4792 | return (bs->iostatus_enabled && |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 4793 | (bs->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC || |
| 4794 | bs->on_write_error == BLOCKDEV_ON_ERROR_STOP || |
| 4795 | bs->on_read_error == BLOCKDEV_ON_ERROR_STOP)); |
Luiz Capitulino | 28a7282 | 2011-09-26 17:43:50 -0300 | [diff] [blame] | 4796 | } |
| 4797 | |
| 4798 | void bdrv_iostatus_disable(BlockDriverState *bs) |
| 4799 | { |
Luiz Capitulino | d6bf279 | 2011-10-14 17:11:23 -0300 | [diff] [blame] | 4800 | bs->iostatus_enabled = false; |
Luiz Capitulino | 28a7282 | 2011-09-26 17:43:50 -0300 | [diff] [blame] | 4801 | } |
| 4802 | |
| 4803 | void bdrv_iostatus_reset(BlockDriverState *bs) |
| 4804 | { |
| 4805 | if (bdrv_iostatus_is_enabled(bs)) { |
Luiz Capitulino | 58e21ef | 2011-10-14 17:22:24 -0300 | [diff] [blame] | 4806 | bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; |
Paolo Bonzini | 3bd293c | 2012-10-18 16:49:27 +0200 | [diff] [blame] | 4807 | if (bs->job) { |
| 4808 | block_job_iostatus_reset(bs->job); |
| 4809 | } |
Luiz Capitulino | 28a7282 | 2011-09-26 17:43:50 -0300 | [diff] [blame] | 4810 | } |
| 4811 | } |
| 4812 | |
Luiz Capitulino | 28a7282 | 2011-09-26 17:43:50 -0300 | [diff] [blame] | 4813 | void bdrv_iostatus_set_err(BlockDriverState *bs, int error) |
| 4814 | { |
Paolo Bonzini | 3e1caa5 | 2012-09-28 17:22:57 +0200 | [diff] [blame] | 4815 | assert(bdrv_iostatus_is_enabled(bs)); |
| 4816 | if (bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { |
Luiz Capitulino | 58e21ef | 2011-10-14 17:22:24 -0300 | [diff] [blame] | 4817 | bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : |
| 4818 | BLOCK_DEVICE_IO_STATUS_FAILED; |
Luiz Capitulino | 28a7282 | 2011-09-26 17:43:50 -0300 | [diff] [blame] | 4819 | } |
| 4820 | } |
| 4821 | |
Christoph Hellwig | a597e79 | 2011-08-25 08:26:01 +0200 | [diff] [blame] | 4822 | void |
| 4823 | bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, |
| 4824 | enum BlockAcctType type) |
| 4825 | { |
| 4826 | assert(type < BDRV_MAX_IOTYPE); |
| 4827 | |
| 4828 | cookie->bytes = bytes; |
Christoph Hellwig | c488c7f | 2011-08-25 08:26:10 +0200 | [diff] [blame] | 4829 | cookie->start_time_ns = get_clock(); |
Christoph Hellwig | a597e79 | 2011-08-25 08:26:01 +0200 | [diff] [blame] | 4830 | cookie->type = type; |
| 4831 | } |
| 4832 | |
| 4833 | void |
| 4834 | bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) |
| 4835 | { |
| 4836 | assert(cookie->type < BDRV_MAX_IOTYPE); |
| 4837 | |
| 4838 | bs->nr_bytes[cookie->type] += cookie->bytes; |
| 4839 | bs->nr_ops[cookie->type]++; |
Christoph Hellwig | c488c7f | 2011-08-25 08:26:10 +0200 | [diff] [blame] | 4840 | bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; |
Christoph Hellwig | a597e79 | 2011-08-25 08:26:01 +0200 | [diff] [blame] | 4841 | } |
| 4842 | |
Luiz Capitulino | d92ada2 | 2012-11-30 10:52:09 -0200 | [diff] [blame] | 4843 | void bdrv_img_create(const char *filename, const char *fmt, |
| 4844 | const char *base_filename, const char *base_fmt, |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 4845 | char *options, uint64_t img_size, int flags, |
| 4846 | Error **errp, bool quiet) |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4847 | { |
| 4848 | QEMUOptionParameter *param = NULL, *create_options = NULL; |
Kevin Wolf | d220894 | 2011-06-01 14:03:31 +0200 | [diff] [blame] | 4849 | QEMUOptionParameter *backing_fmt, *backing_file, *size; |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4850 | BlockDriver *drv, *proto_drv; |
Stefan Hajnoczi | 96df67d | 2011-01-24 09:32:20 +0000 | [diff] [blame] | 4851 | BlockDriver *backing_drv = NULL; |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 4852 | Error *local_err = NULL; |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4853 | int ret = 0; |
| 4854 | |
| 4855 | /* Find driver and parse its options */ |
| 4856 | drv = bdrv_find_format(fmt); |
| 4857 | if (!drv) { |
Luiz Capitulino | 71c7981 | 2012-11-30 10:52:04 -0200 | [diff] [blame] | 4858 | error_setg(errp, "Unknown file format '%s'", fmt); |
Luiz Capitulino | d92ada2 | 2012-11-30 10:52:09 -0200 | [diff] [blame] | 4859 | return; |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4860 | } |
| 4861 | |
Kevin Wolf | 9828962 | 2013-07-10 15:47:39 +0200 | [diff] [blame] | 4862 | proto_drv = bdrv_find_protocol(filename, true); |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4863 | if (!proto_drv) { |
Luiz Capitulino | 71c7981 | 2012-11-30 10:52:04 -0200 | [diff] [blame] | 4864 | error_setg(errp, "Unknown protocol '%s'", filename); |
Luiz Capitulino | d92ada2 | 2012-11-30 10:52:09 -0200 | [diff] [blame] | 4865 | return; |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4866 | } |
| 4867 | |
| 4868 | create_options = append_option_parameters(create_options, |
| 4869 | drv->create_options); |
| 4870 | create_options = append_option_parameters(create_options, |
| 4871 | proto_drv->create_options); |
| 4872 | |
| 4873 | /* Create parameter list with default values */ |
| 4874 | param = parse_option_parameters("", create_options, param); |
| 4875 | |
| 4876 | set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); |
| 4877 | |
| 4878 | /* Parse -o options */ |
| 4879 | if (options) { |
| 4880 | param = parse_option_parameters(options, create_options, param); |
| 4881 | if (param == NULL) { |
Luiz Capitulino | 71c7981 | 2012-11-30 10:52:04 -0200 | [diff] [blame] | 4882 | error_setg(errp, "Invalid options for file format '%s'.", fmt); |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4883 | goto out; |
| 4884 | } |
| 4885 | } |
| 4886 | |
| 4887 | if (base_filename) { |
| 4888 | if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, |
| 4889 | base_filename)) { |
Luiz Capitulino | 71c7981 | 2012-11-30 10:52:04 -0200 | [diff] [blame] | 4890 | error_setg(errp, "Backing file not supported for file format '%s'", |
| 4891 | fmt); |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4892 | goto out; |
| 4893 | } |
| 4894 | } |
| 4895 | |
| 4896 | if (base_fmt) { |
| 4897 | if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { |
Luiz Capitulino | 71c7981 | 2012-11-30 10:52:04 -0200 | [diff] [blame] | 4898 | error_setg(errp, "Backing file format not supported for file " |
| 4899 | "format '%s'", fmt); |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4900 | goto out; |
| 4901 | } |
| 4902 | } |
| 4903 | |
Jes Sorensen | 792da93 | 2010-12-16 13:52:17 +0100 | [diff] [blame] | 4904 | backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); |
| 4905 | if (backing_file && backing_file->value.s) { |
| 4906 | if (!strcmp(filename, backing_file->value.s)) { |
Luiz Capitulino | 71c7981 | 2012-11-30 10:52:04 -0200 | [diff] [blame] | 4907 | error_setg(errp, "Error: Trying to create an image with the " |
| 4908 | "same filename as the backing file"); |
Jes Sorensen | 792da93 | 2010-12-16 13:52:17 +0100 | [diff] [blame] | 4909 | goto out; |
| 4910 | } |
| 4911 | } |
| 4912 | |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4913 | backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); |
| 4914 | if (backing_fmt && backing_fmt->value.s) { |
Stefan Hajnoczi | 96df67d | 2011-01-24 09:32:20 +0000 | [diff] [blame] | 4915 | backing_drv = bdrv_find_format(backing_fmt->value.s); |
| 4916 | if (!backing_drv) { |
Luiz Capitulino | 71c7981 | 2012-11-30 10:52:04 -0200 | [diff] [blame] | 4917 | error_setg(errp, "Unknown backing file format '%s'", |
| 4918 | backing_fmt->value.s); |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4919 | goto out; |
| 4920 | } |
| 4921 | } |
| 4922 | |
| 4923 | // The size for the image must always be specified, with one exception: |
| 4924 | // 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] | 4925 | size = get_option_parameter(param, BLOCK_OPT_SIZE); |
| 4926 | if (size && size->value.n == -1) { |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4927 | if (backing_file && backing_file->value.s) { |
Max Reitz | 66f6b81 | 2013-12-03 14:57:52 +0100 | [diff] [blame] | 4928 | BlockDriverState *bs; |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4929 | uint64_t size; |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4930 | char buf[32]; |
Paolo Bonzini | 63090da | 2012-04-12 14:01:03 +0200 | [diff] [blame] | 4931 | int back_flags; |
| 4932 | |
| 4933 | /* backing files always opened read-only */ |
| 4934 | back_flags = |
| 4935 | flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4936 | |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4937 | bs = bdrv_new(""); |
| 4938 | |
Kevin Wolf | de9c0ce | 2013-03-15 10:35:02 +0100 | [diff] [blame] | 4939 | ret = bdrv_open(bs, backing_file->value.s, NULL, back_flags, |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 4940 | backing_drv, &local_err); |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4941 | if (ret < 0) { |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 4942 | error_setg_errno(errp, -ret, "Could not open '%s': %s", |
| 4943 | backing_file->value.s, |
| 4944 | error_get_pretty(local_err)); |
| 4945 | error_free(local_err); |
| 4946 | local_err = NULL; |
Max Reitz | 66f6b81 | 2013-12-03 14:57:52 +0100 | [diff] [blame] | 4947 | bdrv_unref(bs); |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4948 | goto out; |
| 4949 | } |
| 4950 | bdrv_get_geometry(bs, &size); |
| 4951 | size *= 512; |
| 4952 | |
| 4953 | snprintf(buf, sizeof(buf), "%" PRId64, size); |
| 4954 | set_option_parameter(param, BLOCK_OPT_SIZE, buf); |
Max Reitz | 66f6b81 | 2013-12-03 14:57:52 +0100 | [diff] [blame] | 4955 | |
| 4956 | bdrv_unref(bs); |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4957 | } else { |
Luiz Capitulino | 71c7981 | 2012-11-30 10:52:04 -0200 | [diff] [blame] | 4958 | error_setg(errp, "Image creation needs a size parameter"); |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4959 | goto out; |
| 4960 | } |
| 4961 | } |
| 4962 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 4963 | if (!quiet) { |
| 4964 | printf("Formatting '%s', fmt=%s ", filename, fmt); |
| 4965 | print_option_parameters(param); |
| 4966 | puts(""); |
| 4967 | } |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 4968 | ret = bdrv_create(drv, filename, param, &local_err); |
| 4969 | if (ret == -EFBIG) { |
| 4970 | /* This is generally a better message than whatever the driver would |
| 4971 | * deliver (especially because of the cluster_size_hint), since that |
| 4972 | * is most probably not much different from "image too large". */ |
| 4973 | const char *cluster_size_hint = ""; |
| 4974 | if (get_option_parameter(create_options, BLOCK_OPT_CLUSTER_SIZE)) { |
| 4975 | cluster_size_hint = " (try using a larger cluster size)"; |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4976 | } |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 4977 | error_setg(errp, "The image size is too large for file format '%s'" |
| 4978 | "%s", fmt, cluster_size_hint); |
| 4979 | error_free(local_err); |
| 4980 | local_err = NULL; |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4981 | } |
| 4982 | |
| 4983 | out: |
| 4984 | free_option_parameters(create_options); |
| 4985 | free_option_parameters(param); |
| 4986 | |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 4987 | if (error_is_set(&local_err)) { |
| 4988 | error_propagate(errp, local_err); |
| 4989 | } |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 4990 | } |
Stefan Hajnoczi | 85d126f | 2013-03-07 13:41:48 +0100 | [diff] [blame] | 4991 | |
| 4992 | AioContext *bdrv_get_aio_context(BlockDriverState *bs) |
| 4993 | { |
| 4994 | /* Currently BlockDriverState always uses the main loop AioContext */ |
| 4995 | return qemu_get_aio_context(); |
| 4996 | } |
Stefan Hajnoczi | d616b22 | 2013-06-24 17:13:10 +0200 | [diff] [blame] | 4997 | |
| 4998 | void bdrv_add_before_write_notifier(BlockDriverState *bs, |
| 4999 | NotifierWithReturn *notifier) |
| 5000 | { |
| 5001 | notifier_with_return_list_add(&bs->before_write_notifiers, notifier); |
| 5002 | } |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 5003 | |
| 5004 | int bdrv_amend_options(BlockDriverState *bs, QEMUOptionParameter *options) |
| 5005 | { |
| 5006 | if (bs->drv->bdrv_amend_options == NULL) { |
| 5007 | return -ENOTSUP; |
| 5008 | } |
| 5009 | return bs->drv->bdrv_amend_options(bs, options); |
| 5010 | } |
Benoît Canet | f6186f4 | 2013-10-02 14:33:48 +0200 | [diff] [blame] | 5011 | |
| 5012 | ExtSnapshotPerm bdrv_check_ext_snapshot(BlockDriverState *bs) |
| 5013 | { |
| 5014 | if (bs->drv->bdrv_check_ext_snapshot) { |
| 5015 | return bs->drv->bdrv_check_ext_snapshot(bs); |
| 5016 | } |
| 5017 | |
| 5018 | if (bs->file && bs->file->drv && bs->file->drv->bdrv_check_ext_snapshot) { |
| 5019 | return bs->file->drv->bdrv_check_ext_snapshot(bs); |
| 5020 | } |
| 5021 | |
| 5022 | /* external snapshots are allowed by default */ |
| 5023 | return EXT_SNAPSHOT_ALLOWED; |
| 5024 | } |
| 5025 | |
| 5026 | ExtSnapshotPerm bdrv_check_ext_snapshot_forbidden(BlockDriverState *bs) |
| 5027 | { |
| 5028 | return EXT_SNAPSHOT_FORBIDDEN; |
| 5029 | } |